blob: 9bae16b8455651814ebc817c7f92c503dffd805d [file] [log] [blame]
Allen.Wang4a0948d2021-12-15 13:41:18 +08001#!/bin/bash
2#
3# Power Control tool
4# Enable/disable AC relay
5# On/off System by step moter to press power key
6
7export PATH=$PATH:/usr/sbin:/usr/libexec
8
Potin Lai501f4c72022-06-13 13:23:57 +08009# shellcheck source=meta-facebook/meta-bletchley/recipes-bletchley/plat-tools/files/bletchley-common-functions
10source /usr/libexec/bletchley-common-functions
11
Allen.Wang4a0948d2021-12-15 13:41:18 +080012DELAY_POWER_ON="0.5"
13DELAY_POWER_OFF="5"
Potin Lai27083c32022-06-02 19:13:24 +080014DELAY_POWER_RECOVERY_MODE="10"
Allen.Wang4a0948d2021-12-15 13:41:18 +080015POWER_BTN_TIMEOUT_CNT=60
16
Potin Laibb91c1b2022-04-28 15:03:05 +080017REV_EVT="EVT"
18REV_DVT="DVT"
19REV_UNKNOW="UNKNOW"
20
21DBUS_HOST_ST_ON="xyz.openbmc_project.State.Host.HostState.Running"
22DBUS_HOST_ST_OFF="xyz.openbmc_project.State.Host.HostState.Off"
23
24HOST_ST_UNKNOW="Unknow"
25HOST_ST_ON="On"
26HOST_ST_OFF="Off"
Potin Lai27083c32022-06-02 19:13:24 +080027HOST_ST_SLEEP="Sleep"
28HOST_ST_DFU="DFU"
29HOST_ST_RECOVERY="Recovery"
Potin Laibb91c1b2022-04-28 15:03:05 +080030HOST_AC_ON="AC On"
31HOST_AC_OFF="AC Off"
32
Potin Lai27083c32022-06-02 19:13:24 +080033ACTION_ON="on"
34ACTION_OFF="off"
35ACTION_DFU="dfu"
36ACTION_RECOVERY="recovery"
Potin Lai2320b0e2022-06-28 10:38:37 +080037ACTION_CYCLE="cycle"
38ACTION_RESET="reset"
Potin Lai27083c32022-06-02 19:13:24 +080039ACTION_AC_ON="ac-on"
40ACTION_AC_OFF="ac-off"
41ACTION_STATUS="status"
42
43VALID_SLED_ACTIONS="
44 $ACTION_ON
45 $ACTION_OFF
46 $ACTION_AC_ON
47 $ACTION_AC_OFF
48 $ACTION_STATUS
49 $ACTION_DFU
50 $ACTION_RECOVERY
Potin Lai2320b0e2022-06-28 10:38:37 +080051 $ACTION_CYCLE
52 $ACTION_RESET
Potin Lai27083c32022-06-02 19:13:24 +080053"
54
55function is_valid_sled_action()
56{
57 local ACTION=$1
58 for i in $VALID_SLED_ACTIONS
59 do
60 if [ "$i" = "$ACTION" ]; then
61 return 0
62 fi
63 done
64 return 1
65}
66
67function get_board_rev()
68{
Potin Laibb91c1b2022-04-28 15:03:05 +080069 local rev_id0
70 local rev_id1
71 local rev_id2
72 local rev_val
73
74 rev_id0=$(get_gpio "REV_ID0")
75 rev_id1=$(get_gpio "REV_ID1")
76 rev_id2=$(get_gpio "REV_ID2")
77 rev_val=$((rev_id0+(rev_id1<<1)+(rev_id2<<2)))
78
79 case $rev_val in
80 0)
81 echo "$REV_EVT"
82 ;;
83 1)
84 echo "$REV_DVT"
85 ;;
86 *)
87 echo "$REV_UNKNOW"
88 return 1
89 ;;
90 esac
91
92 return 0
93}
94
Allen.Wang4a0948d2021-12-15 13:41:18 +080095#Switch pull low while it be touched
Potin Lai27083c32022-06-02 19:13:24 +080096function wait_for_switch()
97{
Allen.Wang4a0948d2021-12-15 13:41:18 +080098 TARGET_PIN=$1
99 TARGET_SWITCH=1
100 TIME_CNT=0
101 while [ "$TARGET_SWITCH" -eq 1 ] ;do
102 TARGET_SWITCH=$(get_gpio "$TARGET_PIN")
103 sleep 0.1
104 TIME_CNT=$(( TIME_CNT +1))
105 if [ $TIME_CNT -gt $POWER_BTN_TIMEOUT_CNT ];then
106 echo "Error: Too long to get target switch, force exit" >&2
107 break
108 fi
109 done
110}
111
Potin Lai27083c32022-06-02 19:13:24 +0800112function trigger_power_button()
113{
Allen.Wang4a0948d2021-12-15 13:41:18 +0800114 local sled_num=$1
Allen.Wang6af0dff2021-12-28 20:23:05 +0800115 local delay_time=$2
Allen.Wang4a0948d2021-12-15 13:41:18 +0800116
117 #SLED{N}_MS_DETECT1 (initial position)
118 GPIO_DETECT_PIN1="SLED${sled_num}_MS_DETECT1"
119 #SLED{N}_MS_DETECT0 (MAC position)
120 GPIO_DETECT_PIN0="SLED${sled_num}_MS_DETECT0"
121
122 echo "Motor go forward to press Power key"
123 motor-ctrl "sled${sled_num}" f >/dev/null
124 wait_for_switch "${GPIO_DETECT_PIN0}"
125 motor-ctrl "sled${sled_num}" s >/dev/null
126
127 if [ "$(get_gpio "$GPIO_DETECT_PIN0")" -eq 0 ];then
128 echo "Power key switch triggered"
129 echo "Press power key for Sled${1} ${delay_time} seconds..."
130 sleep "$delay_time"
131 else
Allen.Wang6af0dff2021-12-28 20:23:05 +0800132 echo "Power key switch not trigger, back motor to initial position"
Allen.Wang4a0948d2021-12-15 13:41:18 +0800133 fi
134
135 motor-ctrl "sled${sled_num}" r >/dev/null
136 wait_for_switch "${GPIO_DETECT_PIN1}"
137 motor-ctrl "sled${sled_num}" s >/dev/null
138 if [ "$(get_gpio "$GPIO_DETECT_PIN1")" -eq 0 ];then
139 echo "Motor reverse to initial position successful"
140 else
Allen.Wang6af0dff2021-12-28 20:23:05 +0800141 echo "Initial position switch not trigger, force stop motor"
Allen.Wang4a0948d2021-12-15 13:41:18 +0800142 fi
143}
144
Potin Lai27083c32022-06-02 19:13:24 +0800145function release_power_button()
146{
Potin Laidb5648e2022-02-16 00:57:55 +0800147 local sled_num=$1
148 GPIO_DETECT_PIN1="SLED${sled_num}_MS_DETECT1"
149
150 if [ "$(get_gpio "$GPIO_DETECT_PIN1")" -eq 0 ]; then
151 echo "Motor at initial position already"
152 return 0
153 fi
154
155 motor-ctrl "sled${sled_num}" r >/dev/null
156 wait_for_switch "${GPIO_DETECT_PIN1}"
157 motor-ctrl "sled${sled_num}" s >/dev/null
158 if [ "$(get_gpio "$GPIO_DETECT_PIN1")" -eq 0 ];then
159 echo "Motor reverse to initial position successful"
160 return 0
161 fi
162
163 echo "Error: Initial position switch not trigger"
164 return 1
165}
166
Potin Lai27083c32022-06-02 19:13:24 +0800167function press_power_button()
168{
Potin Laidb5648e2022-02-16 00:57:55 +0800169 local sled_num=$1
170
171 GPIO_DETECT_PIN0="SLED${sled_num}_MS_DETECT0"
172
173 echo "Motor go forward to press Power button"
174 motor-ctrl "sled${sled_num}" f >/dev/null
175 wait_for_switch "${GPIO_DETECT_PIN0}"
176 motor-ctrl "sled${sled_num}" s >/dev/null
177
178 if [ "$(get_gpio "$GPIO_DETECT_PIN0")" -eq 0 ];then
179 echo "Power button switch triggered"
180 return 0
181 fi
182
183 echo "Error: Power button switch not trigger"
184 return 1
185}
186
Allen.Wang4a0948d2021-12-15 13:41:18 +0800187#Get i2c bus number for sledN
Potin Lai27083c32022-06-02 19:13:24 +0800188function get_bus_num()
189{
Allen.Wang4a0948d2021-12-15 13:41:18 +0800190 SLED_NUM=$1
191 local bus=0
Allen.Wang5ff992e2022-01-04 21:00:38 +0800192 #Mapping Sled number 1~6 to i2c bus number 0~5
193 if [[ "$SLED_NUM" = [1-6] ]]; then
194 bus=$(( SLED_NUM - 1 ))
Allen.Wang4a0948d2021-12-15 13:41:18 +0800195 fi
196 echo "$bus"
197}
198
Potin Lai27083c32022-06-02 19:13:24 +0800199function get_ac_status()
200{
Allen.Wang4a0948d2021-12-15 13:41:18 +0800201 i2c_bus=$(get_bus_num "$1")
202 p1_output_reg=$(i2cget -f -y "$i2c_bus" 0x76 0x03)
203 p1_config_reg=$(i2cget -f -y "$i2c_bus" 0x76 0x07)
204 host_pwr="$(( (p1_output_reg & 0x80)>>7 ))"
205 is_output="$(( (~p1_config_reg & 0x80)>>7 ))"
206
207 if [ "$(( host_pwr & is_output ))" -eq 1 ];then
Potin Laibb91c1b2022-04-28 15:03:05 +0800208 echo "$HOST_AC_ON"
Allen.Wang4a0948d2021-12-15 13:41:18 +0800209 else
Potin Laibb91c1b2022-04-28 15:03:05 +0800210 echo "$HOST_AC_OFF"
Allen.Wang4a0948d2021-12-15 13:41:18 +0800211 fi
212}
213
Potin Laibb91c1b2022-04-28 15:03:05 +0800214function get_host_status_dbus()
215{
216 local sled_num=$1
217 local object="/xyz/openbmc_project/state/host${sled_num}"
218 local service="xyz.openbmc_project.State.Host${sled_num}"
219 local interface="xyz.openbmc_project.State.Host"
220 local property="CurrentHostState"
221 local host_state
222
223 host_state=$(busctl get-property "$service" "$object" "$interface" "$property" | cut -d '"' -f2)
224
225 if [ "$host_state" = "$DBUS_HOST_ST_ON" ]; then
226 echo "$HOST_ST_ON"
227 elif [ "$host_state" = "$DBUS_HOST_ST_OFF" ]; then
228 echo "$HOST_ST_OFF"
229 else
230 echo "$HOST_ST_UNKNOW"
231 return 1
232 fi
233
234 return 0
235}
236
Potin Laib8f52ae2022-09-19 14:20:58 +0800237function get_addr_from_dts_aliases()
238{
239 local node_address
240 node_address=$(awk -F '@' '{printf $2}' /sys/firmware/devicetree/base/aliases/"$1")
241 echo "$node_address"
242}
243
Potin Laibb91c1b2022-04-28 15:03:05 +0800244function get_host_status_mdio()
Potin Laidc251662022-04-01 11:16:50 +0800245{
Potin Lai27083c32022-06-02 19:13:24 +0800246 local SLED_NUM=$1
Potin Laib8f52ae2022-09-19 14:20:58 +0800247 local MDIO_BUS
248
249 MDIO_BUS="$(get_addr_from_dts_aliases mdio0).mdio-1"
Potin Laibb91c1b2022-04-28 15:03:05 +0800250
Potin Laidc251662022-04-01 11:16:50 +0800251 declare -a PORT_MAP=(0 3 2 1 7 6 5)
252
253 # check /dev/mem
254 if ! create_dev_mem; then
255 return 1
256 fi
257
Potin Lai27083c32022-06-02 19:13:24 +0800258 local CHECK_CNT=0
259 local MDIO_ERR_CNT=0
260 local CUR_HOST_ST=$HOST_ST_UNKNOW
261 local SLED_LAST_ACTION
262
263 if [ -f /tmp/sled"${SLED_NUM}"-last-action ]; then
264 SLED_LAST_ACTION=$(cat /tmp/sled"${SLED_NUM}"-last-action)
265 fi
Potin Laidc251662022-04-01 11:16:50 +0800266
267 while true
268 do
Potin Laib8f52ae2022-09-19 14:20:58 +0800269 if POST_ST_VAL=$(mdio "$MDIO_BUS" phy "${PORT_MAP[SLED_NUM]}" 0); then
Potin Lai27083c32022-06-02 19:13:24 +0800270 if [ $((POST_ST_VAL&16#0800)) -eq $((16#0000)) ]; then
271 case $SLED_LAST_ACTION in
272 "$ACTION_DFU")
273 TMP_HOST_ST="$HOST_ST_DFU"
274 ;;
275 *)
276 TMP_HOST_ST="$HOST_ST_OFF"
277 ;;
278 esac
279 elif [ $((POST_ST_VAL&16#0A00)) -eq $((16#0A00)) ]; then
Potin Laidc251662022-04-01 11:16:50 +0800280 TMP_HOST_ST="$HOST_ST_ON"
Potin Lai27083c32022-06-02 19:13:24 +0800281 case $SLED_LAST_ACTION in
282 "$ACTION_RECOVERY")
283 TMP_HOST_ST="$HOST_ST_RECOVERY"
284 ;;
285 *)
286 TMP_HOST_ST="$HOST_ST_ON"
287 ;;
288 esac
289 elif [ $((POST_ST_VAL&16#0900)) -eq $((16#0900)) ]; then
290 TMP_HOST_ST="$HOST_ST_SLEEP"
Potin Laidc251662022-04-01 11:16:50 +0800291 else
Potin Lai27083c32022-06-02 19:13:24 +0800292 TMP_HOST_ST="$HOST_ST_UNKNOW"
Potin Laidc251662022-04-01 11:16:50 +0800293 fi
294
295 if [ "$CUR_HOST_ST" == "$TMP_HOST_ST" ]; then
296 CHECK_CNT=$((CHECK_CNT+1))
297 else
298 CUR_HOST_ST=$TMP_HOST_ST
299 CHECK_CNT=0
300 fi
301
302 if [ "$CHECK_CNT" -ge 5 ]; then
303 echo "$CUR_HOST_ST"
304 break
305 fi
306 else
307 MDIO_ERR_CNT=$((MDIO_ERR_CNT+1))
308 if [ "$MDIO_ERR_CNT" -ge 5 ]; then
309 echo "$HOST_ST_UNKNOW"
310 return 1
311 fi
312 fi
313 done
314
315 return 0
316}
317
Potin Laibb91c1b2022-04-28 15:03:05 +0800318function get_host_status()
319{
320 local sled_num=$1
Potin Laibb91c1b2022-04-28 15:03:05 +0800321
Potin Lai27083c32022-06-02 19:13:24 +0800322 if [ "$(get_ac_status "$SLED_NUM")" == "$HOST_AC_OFF" ];then
323 echo "$HOST_AC_OFF"
324 return 0
325 fi
Potin Laibb91c1b2022-04-28 15:03:05 +0800326
Potin Lai27083c32022-06-02 19:13:24 +0800327 if [ "$(get_board_rev)" = "$REV_EVT" ]; then
Potin Laibb91c1b2022-04-28 15:03:05 +0800328 get_host_status_dbus "$sled_num"
329 else
330 get_host_status_mdio "$sled_num"
331 fi
Potin Laibb91c1b2022-04-28 15:03:05 +0800332 return $?
333}
334
Potin Lai2320b0e2022-06-28 10:38:37 +0800335function do_action_reset()
336{
337 # 1. Power off
338 # 2. Power on
339
340 local SLED_NUM=$1
341 local CUR_ST=$2
342
343 if [ "$CUR_ST" != "$HOST_ST_OFF" ]; then
344 do_action_off "$SLED_NUM"
345 else
346 echo "sled${SLED_NUM}: already powered off"
347 fi
348
349 sleep 3
350 do_action_on "$SLED_NUM"
351}
352
353function do_action_cycle()
354{
355 # 1. AC off
356 # 2. AC on
357 # 3. Power on
358
359 local SLED_NUM=$1
360
361 do_action_ac_off "$SLED_NUM"
362 sleep 3
363 do_action_ac_on "$SLED_NUM"
364 sleep 3
365 do_action_on "$SLED_NUM"
366}
367
Potin Lai27083c32022-06-02 19:13:24 +0800368function do_action_ac_on()
369{
370 local SLED_NUM=$1
371 echo "sled${SLED_NUM}: turn on AC"
372 set_gpio "power-host${SLED_NUM}" 1
373 echo "$ACTION_AC_ON" > "/tmp/sled${SLED_NUM}-last-action"
374}
375
376function do_action_ac_off()
377{
378 local SLED_NUM=$1
379 echo "sled${SLED_NUM}: turn off AC"
380 set_gpio "power-host${SLED_NUM}" 0
381 echo "$ACTION_AC_OFF" > "/tmp/sled${SLED_NUM}-last-action"
382}
383
384function do_action_on()
385{
386 local SLED_NUM=$1
387 echo "sled${SLED_NUM}: power on host"
388 trigger_power_button "$SLED_NUM" "$DELAY_POWER_ON"
Potin Laiaafe1872022-08-19 06:56:49 +0000389 sleep 10 # Mac mini need about 10 second to stable link status
Potin Lai27083c32022-06-02 19:13:24 +0800390 echo "$ACTION_ON" > "/tmp/sled${SLED_NUM}-last-action"
391}
392
393function do_action_off()
394{
395 local SLED_NUM=$1
396 echo "sled${SLED_NUM}: power off host"
397 trigger_power_button "$SLED_NUM" "$DELAY_POWER_OFF"
398 echo "$ACTION_OFF" > "/tmp/sled${SLED_NUM}-last-action"
399}
400
401function do_action_recovery()
402{
403 local SLED_NUM=$1
404 echo "sled${SLED_NUM}: trigger host recovery mode"
405 trigger_power_button "$SLED_NUM" "$DELAY_POWER_RECOVERY_MODE"
406 echo "$ACTION_RECOVERY" > "/tmp/sled${SLED_NUM}-last-action"
407}
408
409function do_action_dfu()
410{
411 local SLED_NUM=$1
412 echo "sled${SLED_NUM}: trigger host dfu mode"
413
414 # turn ac off, and hold for 25 seconds
415 do_action_ac_off "$SLED_NUM"
416 sleep 25
417
418 # press power button
419 echo "SLED$SLED_NUM: pressing power button"
420 if ! press_power_button "$SLED_NUM"; then
421 echo "SLED$SLED_NUM: press power button failed"
422 echo "SLED$SLED_NUM: releasing power button"
423 release_power_button "$SLED_NUM"
424 exit 1
425 fi
426 sleep 1
427
428 # turn ac on
429 echo "SLED$SLED_NUM: turn ac-on"
430 do_action_ac_on "$SLED_NUM"
431 sleep 3
432
433 # release power button
434 echo "SLED$SLED_NUM: releasing host power button"
435 if ! release_power_button "$SLED_NUM"; then
436 echo "SLED$SLED_NUM: release power button failed"
437 exit 1
438 fi
439 echo "$ACTION_DFU" > "/tmp/sled${SLED_NUM}-last-action"
440}
441
442function host_state_on_action_handler()
443{
444 local SLED_NUM=$1
445 local ACTION=$2
446
447 case $ACTION in
448 "$ACTION_OFF")
449 do_action_off "$SLED_NUM"
450 ;;
451 "$ACTION_AC_OFF")
452 do_action_ac_off "$SLED_NUM"
453 ;;
Potin Lai2320b0e2022-06-28 10:38:37 +0800454 "$ACTION_RESET")
455 do_action_reset "$SLED_NUM" "$HOST_ST_ON"
456 ;;
457 "$ACTION_CYCLE")
458 do_action_cycle "$SLED_NUM"
459 ;;
Potin Lai27083c32022-06-02 19:13:24 +0800460 *)
461 echo "Invalid action ($ACTION) for current host state (On)"
462 return 1
463 ;;
464 esac
465}
466
467function host_state_sleep_action_handler()
468{
469 local SLED_NUM=$1
470 local ACTION=$2
471
472 case $ACTION in
473 "$ACTION_ON")
474 do_action_on "$SLED_NUM"
475 ;;
476 "$ACTION_OFF")
477 do_action_off "$SLED_NUM"
478 ;;
479 "$ACTION_AC_OFF")
480 do_action_ac_off "$SLED_NUM"
481 ;;
Potin Lai2320b0e2022-06-28 10:38:37 +0800482 "$ACTION_RESET")
483 do_action_reset "$SLED_NUM" "$HOST_ST_ON"
484 ;;
485 "$ACTION_CYCLE")
486 do_action_cycle "$SLED_NUM"
487 ;;
Potin Lai27083c32022-06-02 19:13:24 +0800488 *)
489 echo "Invalid action ($ACTION) for current host state (Sleep)"
490 return 1
491 ;;
492 esac
493}
494
495function host_state_off_action_handler()
496{
497 local SLED_NUM=$1
498 local ACTION=$2
499
500 case $ACTION in
501 "$ACTION_ON")
502 do_action_on "$SLED_NUM"
503 ;;
504 "$ACTION_RECOVERY")
505 do_action_recovery "$SLED_NUM"
506 ;;
507 "$ACTION_AC_OFF")
508 do_action_ac_off "$SLED_NUM"
509 ;;
Potin Lai2320b0e2022-06-28 10:38:37 +0800510 "$ACTION_RESET")
511 do_action_reset "$SLED_NUM" "$HOST_ST_ON"
512 ;;
513 "$ACTION_CYCLE")
514 do_action_cycle "$SLED_NUM"
515 ;;
Potin Lai27083c32022-06-02 19:13:24 +0800516 *)
517 echo "Invalid action ($ACTION) for current host state (Off)"
518 return 1
519 ;;
520 esac
521}
522
523function host_state_ac_off_action_handler()
524{
525 local SLED_NUM=$1
526 local ACTION=$2
527
528 case $ACTION in
529 "$ACTION_AC_ON")
530 do_action_ac_on "$SLED_NUM"
531 ;;
532 "$ACTION_DFU")
533 do_action_dfu "$SLED_NUM"
534 ;;
535 "$ACTION_AC_OFF")
536 echo "sled${SLED_NUM}: already ac off"
537 return 1
538 ;;
Potin Lai2320b0e2022-06-28 10:38:37 +0800539 "$ACTION_CYCLE")
540 do_action_reset "$SLED_NUM"
541 ;;
Potin Lai27083c32022-06-02 19:13:24 +0800542 *)
543 echo "Invalid action ($ACTION) for current host state (AC Off)"
544 return 1
545 ;;
546 esac
547}
548
549function host_state_ac_on_action_handler()
550{
551 local SLED_NUM=$1
552 local ACTION=$2
553
554 case $ACTION in
555 "$ACTION_AC_OFF")
556 do_action_ac_off "$SLED_NUM"
557 ;;
Potin Lai2320b0e2022-06-28 10:38:37 +0800558 "$ACTION_CYCLE")
559 do_action_cycle "$SLED_NUM"
560 ;;
Potin Lai27083c32022-06-02 19:13:24 +0800561 *)
562 echo "sled${SLED_NUM}: already ac on"
563 return 1
564 ;;
565 esac
566}
567
568function host_state_recovery_action_handler()
569{
570 local SLED_NUM=$1
571 local ACTION=$2
572
573 case $ACTION in
574 "$ACTION_OFF")
575 do_action_off "$SLED_NUM"
576 ;;
577 "$ACTION_AC_OFF")
578 do_action_ac_off "$SLED_NUM"
579 ;;
Potin Lai2320b0e2022-06-28 10:38:37 +0800580 "$ACTION_RESET")
581 do_action_reset "$SLED_NUM" "$HOST_ST_ON"
582 ;;
583 "$ACTION_CYCLE")
584 do_action_cycle "$SLED_NUM"
585 ;;
Potin Lai27083c32022-06-02 19:13:24 +0800586 *)
587 echo "Invalid action ($ACTION) for current host state (Recovery)"
588 return 1
589 ;;
590 esac
591}
592
593function host_state_dfu_action_handler()
594{
595 local SLED_NUM=$1
596 local ACTION=$2
597
598 case $ACTION in
599 "$ACTION_AC_OFF")
600 do_action_ac_off "$SLED_NUM"
601 ;;
Potin Lai2320b0e2022-06-28 10:38:37 +0800602 "$ACTION_CYCLE")
603 do_action_cycle "$SLED_NUM"
604 ;;
Potin Lai27083c32022-06-02 19:13:24 +0800605 *)
606 echo "Invalid action ($ACTION) for current host state (DFU)"
607 return 1
608 ;;
609 esac
610}
611
Potin Laidc251662022-04-01 11:16:50 +0800612function create_dev_mem()
613{
614 CHECK_CNT=0
615 while true
616 do
617 CHECK_CNT=$((CHECK_CNT+1))
618 if [ -c /dev/mem ]; then
619 # /dev/mem already exist
620 return 0
621 elif mknod /dev/mem c 1 1; then
622 # mknod success
623 return 0
624 elif [ "$CHECK_CNT" -ge 5 ]; then
625 break
626 fi
627 sleep 1
628 done
629
630 echo "create /dev/mem failed"
631 return 1
632}
633
Allen.Wang4a0948d2021-12-15 13:41:18 +0800634function show_usage(){
Potin Lai27083c32022-06-02 19:13:24 +0800635 echo "Usage: power-ctrl [sled1 | sled2 | sled3 | sled4 | sled5 | sled6] [$VALID_SLED_ACTIONS]"
Allen.Wang4a0948d2021-12-15 13:41:18 +0800636 echo " power-ctrl chassis-cycle"
637}
638
Potin Lai27083c32022-06-02 19:13:24 +0800639
Allen.Wang4a0948d2021-12-15 13:41:18 +0800640if [ $# -eq 1 ]; then
641 if [ "$1" = "chassis-cycle" ];then
642 echo "chassis cycle...."
643 i2cset -y -f 12 0x11 0xd9 c
644 exit 0
645 else
646 echo "Invalid argument: [ $1 ]"
647 show_usage
648 exit 1;
649 fi
650fi
651
652if [ $# -gt 2 ]; then
653 echo "Too many arguments"
654 show_usage
655 exit 1;
656fi
657
Allen.Wang5ff992e2022-01-04 21:00:38 +0800658if [[ "$1" =~ ^(sled[1-6]{1})$ ]]; then
Allen.Wang4a0948d2021-12-15 13:41:18 +0800659 SLED=$1
660 ACTION=$2
661 SLED_NUM=${SLED:4}
662else
663 echo "invalid sled name: ${1}"
664 show_usage
665 exit 1;
666fi
667
668#Check if sled is present
Potin Lai501f4c72022-06-13 13:23:57 +0800669if ! is_sled_present "${SLED_NUM}"; then
Allen.Wang4a0948d2021-12-15 13:41:18 +0800670 echo "${SLED} is not present!"
671 exit 1
Potin Lai27083c32022-06-02 19:13:24 +0800672elif ! is_valid_sled_action "$ACTION"; then
673 echo "Unknown action: $ACTION"
674 show_usage
675 exit 1
Allen.Wang4a0948d2021-12-15 13:41:18 +0800676fi
677
Potin Laia8d258f2022-06-14 18:35:10 +0800678if [ "$ACTION" = "$ACTION_AC_ON" ]; then
679 if [ "$(get_ac_status "$SLED_NUM")" = "$HOST_AC_OFF" ]; then
680 do_action_ac_on "$SLED_NUM"
681 fi
682elif [ "$ACTION" = "$ACTION_AC_OFF" ]; then
683 if [ "$(get_ac_status "$SLED_NUM")" != "$HOST_AC_OFF" ]; then
684 do_action_ac_off "$SLED_NUM"
685 fi
686elif [ "$ACTION" = "$ACTION_STATUS" ];then
687 HOST_CURR_STATUS=$(get_host_status "$SLED_NUM")
Potin Lai27083c32022-06-02 19:13:24 +0800688 echo "$HOST_CURR_STATUS"
Allen.Wang4a0948d2021-12-15 13:41:18 +0800689else
Potin Laia8d258f2022-06-14 18:35:10 +0800690 HOST_CURR_STATUS=$(get_host_status "$SLED_NUM")
Potin Lai27083c32022-06-02 19:13:24 +0800691 case $HOST_CURR_STATUS in
692 "$HOST_AC_OFF")
693 host_state_ac_off_action_handler "$SLED_NUM" "$ACTION"
694 ;;
695 "$HOST_AC_ON")
696 host_state_ac_on_action_handler "$SLED_NUM" "$ACTION"
697 ;;
698 "$HOST_ST_OFF")
699 host_state_off_action_handler "$SLED_NUM" "$ACTION"
700 ;;
701 "$HOST_ST_ON")
702 host_state_on_action_handler "$SLED_NUM" "$ACTION"
703 ;;
704 "$HOST_ST_SLEEP")
705 host_state_sleep_action_handler "$SLED_NUM" "$ACTION"
706 ;;
707 "$HOST_ST_DFU")
708 host_state_dfu_action_handler "$SLED_NUM" "$ACTION"
709 ;;
710 "$HOST_ST_RECOVERY")
711 host_state_recovery_action_handler "$SLED_NUM" "$ACTION"
712 ;;
713 esac
Allen.Wang4a0948d2021-12-15 13:41:18 +0800714fi