blob: 7a6a96c8dd61be3e3e34807132e03402c4952e5f [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
237function get_host_status_mdio()
Potin Laidc251662022-04-01 11:16:50 +0800238{
Potin Lai27083c32022-06-02 19:13:24 +0800239 local SLED_NUM=$1
240 local MDIO_BUS=0
Potin Laibb91c1b2022-04-28 15:03:05 +0800241
Potin Laidc251662022-04-01 11:16:50 +0800242 declare -a PORT_MAP=(0 3 2 1 7 6 5)
243
244 # check /dev/mem
245 if ! create_dev_mem; then
246 return 1
247 fi
248
Potin Lai27083c32022-06-02 19:13:24 +0800249 local CHECK_CNT=0
250 local MDIO_ERR_CNT=0
251 local CUR_HOST_ST=$HOST_ST_UNKNOW
252 local SLED_LAST_ACTION
253
254 if [ -f /tmp/sled"${SLED_NUM}"-last-action ]; then
255 SLED_LAST_ACTION=$(cat /tmp/sled"${SLED_NUM}"-last-action)
256 fi
Potin Laidc251662022-04-01 11:16:50 +0800257
258 while true
259 do
260 if POST_ST_VAL=$(mdio-util c22 r $MDIO_BUS "${PORT_MAP[SLED_NUM]}" 0); then
Potin Lai27083c32022-06-02 19:13:24 +0800261 if [ $((POST_ST_VAL&16#0800)) -eq $((16#0000)) ]; then
262 case $SLED_LAST_ACTION in
263 "$ACTION_DFU")
264 TMP_HOST_ST="$HOST_ST_DFU"
265 ;;
266 *)
267 TMP_HOST_ST="$HOST_ST_OFF"
268 ;;
269 esac
270 elif [ $((POST_ST_VAL&16#0A00)) -eq $((16#0A00)) ]; then
Potin Laidc251662022-04-01 11:16:50 +0800271 TMP_HOST_ST="$HOST_ST_ON"
Potin Lai27083c32022-06-02 19:13:24 +0800272 case $SLED_LAST_ACTION in
273 "$ACTION_RECOVERY")
274 TMP_HOST_ST="$HOST_ST_RECOVERY"
275 ;;
276 *)
277 TMP_HOST_ST="$HOST_ST_ON"
278 ;;
279 esac
280 elif [ $((POST_ST_VAL&16#0900)) -eq $((16#0900)) ]; then
281 TMP_HOST_ST="$HOST_ST_SLEEP"
Potin Laidc251662022-04-01 11:16:50 +0800282 else
Potin Lai27083c32022-06-02 19:13:24 +0800283 TMP_HOST_ST="$HOST_ST_UNKNOW"
Potin Laidc251662022-04-01 11:16:50 +0800284 fi
285
286 if [ "$CUR_HOST_ST" == "$TMP_HOST_ST" ]; then
287 CHECK_CNT=$((CHECK_CNT+1))
288 else
289 CUR_HOST_ST=$TMP_HOST_ST
290 CHECK_CNT=0
291 fi
292
293 if [ "$CHECK_CNT" -ge 5 ]; then
294 echo "$CUR_HOST_ST"
295 break
296 fi
297 else
298 MDIO_ERR_CNT=$((MDIO_ERR_CNT+1))
299 if [ "$MDIO_ERR_CNT" -ge 5 ]; then
300 echo "$HOST_ST_UNKNOW"
301 return 1
302 fi
303 fi
304 done
305
306 return 0
307}
308
Potin Laibb91c1b2022-04-28 15:03:05 +0800309function get_host_status()
310{
311 local sled_num=$1
Potin Laibb91c1b2022-04-28 15:03:05 +0800312
Potin Lai27083c32022-06-02 19:13:24 +0800313 if [ "$(get_ac_status "$SLED_NUM")" == "$HOST_AC_OFF" ];then
314 echo "$HOST_AC_OFF"
315 return 0
316 fi
Potin Laibb91c1b2022-04-28 15:03:05 +0800317
Potin Lai27083c32022-06-02 19:13:24 +0800318 if [ "$(get_board_rev)" = "$REV_EVT" ]; then
Potin Laibb91c1b2022-04-28 15:03:05 +0800319 get_host_status_dbus "$sled_num"
320 else
321 get_host_status_mdio "$sled_num"
322 fi
Potin Laibb91c1b2022-04-28 15:03:05 +0800323 return $?
324}
325
Potin Lai2320b0e2022-06-28 10:38:37 +0800326function do_action_reset()
327{
328 # 1. Power off
329 # 2. Power on
330
331 local SLED_NUM=$1
332 local CUR_ST=$2
333
334 if [ "$CUR_ST" != "$HOST_ST_OFF" ]; then
335 do_action_off "$SLED_NUM"
336 else
337 echo "sled${SLED_NUM}: already powered off"
338 fi
339
340 sleep 3
341 do_action_on "$SLED_NUM"
342}
343
344function do_action_cycle()
345{
346 # 1. AC off
347 # 2. AC on
348 # 3. Power on
349
350 local SLED_NUM=$1
351
352 do_action_ac_off "$SLED_NUM"
353 sleep 3
354 do_action_ac_on "$SLED_NUM"
355 sleep 3
356 do_action_on "$SLED_NUM"
357}
358
Potin Lai27083c32022-06-02 19:13:24 +0800359function do_action_ac_on()
360{
361 local SLED_NUM=$1
362 echo "sled${SLED_NUM}: turn on AC"
363 set_gpio "power-host${SLED_NUM}" 1
364 echo "$ACTION_AC_ON" > "/tmp/sled${SLED_NUM}-last-action"
365}
366
367function do_action_ac_off()
368{
369 local SLED_NUM=$1
370 echo "sled${SLED_NUM}: turn off AC"
371 set_gpio "power-host${SLED_NUM}" 0
372 echo "$ACTION_AC_OFF" > "/tmp/sled${SLED_NUM}-last-action"
373}
374
375function do_action_on()
376{
377 local SLED_NUM=$1
378 echo "sled${SLED_NUM}: power on host"
379 trigger_power_button "$SLED_NUM" "$DELAY_POWER_ON"
Potin Laiaafe1872022-08-19 06:56:49 +0000380 sleep 10 # Mac mini need about 10 second to stable link status
Potin Lai27083c32022-06-02 19:13:24 +0800381 echo "$ACTION_ON" > "/tmp/sled${SLED_NUM}-last-action"
382}
383
384function do_action_off()
385{
386 local SLED_NUM=$1
387 echo "sled${SLED_NUM}: power off host"
388 trigger_power_button "$SLED_NUM" "$DELAY_POWER_OFF"
389 echo "$ACTION_OFF" > "/tmp/sled${SLED_NUM}-last-action"
390}
391
392function do_action_recovery()
393{
394 local SLED_NUM=$1
395 echo "sled${SLED_NUM}: trigger host recovery mode"
396 trigger_power_button "$SLED_NUM" "$DELAY_POWER_RECOVERY_MODE"
397 echo "$ACTION_RECOVERY" > "/tmp/sled${SLED_NUM}-last-action"
398}
399
400function do_action_dfu()
401{
402 local SLED_NUM=$1
403 echo "sled${SLED_NUM}: trigger host dfu mode"
404
405 # turn ac off, and hold for 25 seconds
406 do_action_ac_off "$SLED_NUM"
407 sleep 25
408
409 # press power button
410 echo "SLED$SLED_NUM: pressing power button"
411 if ! press_power_button "$SLED_NUM"; then
412 echo "SLED$SLED_NUM: press power button failed"
413 echo "SLED$SLED_NUM: releasing power button"
414 release_power_button "$SLED_NUM"
415 exit 1
416 fi
417 sleep 1
418
419 # turn ac on
420 echo "SLED$SLED_NUM: turn ac-on"
421 do_action_ac_on "$SLED_NUM"
422 sleep 3
423
424 # release power button
425 echo "SLED$SLED_NUM: releasing host power button"
426 if ! release_power_button "$SLED_NUM"; then
427 echo "SLED$SLED_NUM: release power button failed"
428 exit 1
429 fi
430 echo "$ACTION_DFU" > "/tmp/sled${SLED_NUM}-last-action"
431}
432
433function host_state_on_action_handler()
434{
435 local SLED_NUM=$1
436 local ACTION=$2
437
438 case $ACTION in
439 "$ACTION_OFF")
440 do_action_off "$SLED_NUM"
441 ;;
442 "$ACTION_AC_OFF")
443 do_action_ac_off "$SLED_NUM"
444 ;;
Potin Lai2320b0e2022-06-28 10:38:37 +0800445 "$ACTION_RESET")
446 do_action_reset "$SLED_NUM" "$HOST_ST_ON"
447 ;;
448 "$ACTION_CYCLE")
449 do_action_cycle "$SLED_NUM"
450 ;;
Potin Lai27083c32022-06-02 19:13:24 +0800451 *)
452 echo "Invalid action ($ACTION) for current host state (On)"
453 return 1
454 ;;
455 esac
456}
457
458function host_state_sleep_action_handler()
459{
460 local SLED_NUM=$1
461 local ACTION=$2
462
463 case $ACTION in
464 "$ACTION_ON")
465 do_action_on "$SLED_NUM"
466 ;;
467 "$ACTION_OFF")
468 do_action_off "$SLED_NUM"
469 ;;
470 "$ACTION_AC_OFF")
471 do_action_ac_off "$SLED_NUM"
472 ;;
Potin Lai2320b0e2022-06-28 10:38:37 +0800473 "$ACTION_RESET")
474 do_action_reset "$SLED_NUM" "$HOST_ST_ON"
475 ;;
476 "$ACTION_CYCLE")
477 do_action_cycle "$SLED_NUM"
478 ;;
Potin Lai27083c32022-06-02 19:13:24 +0800479 *)
480 echo "Invalid action ($ACTION) for current host state (Sleep)"
481 return 1
482 ;;
483 esac
484}
485
486function host_state_off_action_handler()
487{
488 local SLED_NUM=$1
489 local ACTION=$2
490
491 case $ACTION in
492 "$ACTION_ON")
493 do_action_on "$SLED_NUM"
494 ;;
495 "$ACTION_RECOVERY")
496 do_action_recovery "$SLED_NUM"
497 ;;
498 "$ACTION_AC_OFF")
499 do_action_ac_off "$SLED_NUM"
500 ;;
Potin Lai2320b0e2022-06-28 10:38:37 +0800501 "$ACTION_RESET")
502 do_action_reset "$SLED_NUM" "$HOST_ST_ON"
503 ;;
504 "$ACTION_CYCLE")
505 do_action_cycle "$SLED_NUM"
506 ;;
Potin Lai27083c32022-06-02 19:13:24 +0800507 *)
508 echo "Invalid action ($ACTION) for current host state (Off)"
509 return 1
510 ;;
511 esac
512}
513
514function host_state_ac_off_action_handler()
515{
516 local SLED_NUM=$1
517 local ACTION=$2
518
519 case $ACTION in
520 "$ACTION_AC_ON")
521 do_action_ac_on "$SLED_NUM"
522 ;;
523 "$ACTION_DFU")
524 do_action_dfu "$SLED_NUM"
525 ;;
526 "$ACTION_AC_OFF")
527 echo "sled${SLED_NUM}: already ac off"
528 return 1
529 ;;
Potin Lai2320b0e2022-06-28 10:38:37 +0800530 "$ACTION_CYCLE")
531 do_action_reset "$SLED_NUM"
532 ;;
Potin Lai27083c32022-06-02 19:13:24 +0800533 *)
534 echo "Invalid action ($ACTION) for current host state (AC Off)"
535 return 1
536 ;;
537 esac
538}
539
540function host_state_ac_on_action_handler()
541{
542 local SLED_NUM=$1
543 local ACTION=$2
544
545 case $ACTION in
546 "$ACTION_AC_OFF")
547 do_action_ac_off "$SLED_NUM"
548 ;;
Potin Lai2320b0e2022-06-28 10:38:37 +0800549 "$ACTION_CYCLE")
550 do_action_cycle "$SLED_NUM"
551 ;;
Potin Lai27083c32022-06-02 19:13:24 +0800552 *)
553 echo "sled${SLED_NUM}: already ac on"
554 return 1
555 ;;
556 esac
557}
558
559function host_state_recovery_action_handler()
560{
561 local SLED_NUM=$1
562 local ACTION=$2
563
564 case $ACTION in
565 "$ACTION_OFF")
566 do_action_off "$SLED_NUM"
567 ;;
568 "$ACTION_AC_OFF")
569 do_action_ac_off "$SLED_NUM"
570 ;;
Potin Lai2320b0e2022-06-28 10:38:37 +0800571 "$ACTION_RESET")
572 do_action_reset "$SLED_NUM" "$HOST_ST_ON"
573 ;;
574 "$ACTION_CYCLE")
575 do_action_cycle "$SLED_NUM"
576 ;;
Potin Lai27083c32022-06-02 19:13:24 +0800577 *)
578 echo "Invalid action ($ACTION) for current host state (Recovery)"
579 return 1
580 ;;
581 esac
582}
583
584function host_state_dfu_action_handler()
585{
586 local SLED_NUM=$1
587 local ACTION=$2
588
589 case $ACTION in
590 "$ACTION_AC_OFF")
591 do_action_ac_off "$SLED_NUM"
592 ;;
Potin Lai2320b0e2022-06-28 10:38:37 +0800593 "$ACTION_CYCLE")
594 do_action_cycle "$SLED_NUM"
595 ;;
Potin Lai27083c32022-06-02 19:13:24 +0800596 *)
597 echo "Invalid action ($ACTION) for current host state (DFU)"
598 return 1
599 ;;
600 esac
601}
602
Potin Laidc251662022-04-01 11:16:50 +0800603function create_dev_mem()
604{
605 CHECK_CNT=0
606 while true
607 do
608 CHECK_CNT=$((CHECK_CNT+1))
609 if [ -c /dev/mem ]; then
610 # /dev/mem already exist
611 return 0
612 elif mknod /dev/mem c 1 1; then
613 # mknod success
614 return 0
615 elif [ "$CHECK_CNT" -ge 5 ]; then
616 break
617 fi
618 sleep 1
619 done
620
621 echo "create /dev/mem failed"
622 return 1
623}
624
Allen.Wang4a0948d2021-12-15 13:41:18 +0800625function show_usage(){
Potin Lai27083c32022-06-02 19:13:24 +0800626 echo "Usage: power-ctrl [sled1 | sled2 | sled3 | sled4 | sled5 | sled6] [$VALID_SLED_ACTIONS]"
Allen.Wang4a0948d2021-12-15 13:41:18 +0800627 echo " power-ctrl chassis-cycle"
628}
629
Potin Lai27083c32022-06-02 19:13:24 +0800630
Allen.Wang4a0948d2021-12-15 13:41:18 +0800631if [ $# -eq 1 ]; then
632 if [ "$1" = "chassis-cycle" ];then
633 echo "chassis cycle...."
634 i2cset -y -f 12 0x11 0xd9 c
635 exit 0
636 else
637 echo "Invalid argument: [ $1 ]"
638 show_usage
639 exit 1;
640 fi
641fi
642
643if [ $# -gt 2 ]; then
644 echo "Too many arguments"
645 show_usage
646 exit 1;
647fi
648
Allen.Wang5ff992e2022-01-04 21:00:38 +0800649if [[ "$1" =~ ^(sled[1-6]{1})$ ]]; then
Allen.Wang4a0948d2021-12-15 13:41:18 +0800650 SLED=$1
651 ACTION=$2
652 SLED_NUM=${SLED:4}
653else
654 echo "invalid sled name: ${1}"
655 show_usage
656 exit 1;
657fi
658
659#Check if sled is present
Potin Lai501f4c72022-06-13 13:23:57 +0800660if ! is_sled_present "${SLED_NUM}"; then
Allen.Wang4a0948d2021-12-15 13:41:18 +0800661 echo "${SLED} is not present!"
662 exit 1
Potin Lai27083c32022-06-02 19:13:24 +0800663elif ! is_valid_sled_action "$ACTION"; then
664 echo "Unknown action: $ACTION"
665 show_usage
666 exit 1
Allen.Wang4a0948d2021-12-15 13:41:18 +0800667fi
668
Potin Laia8d258f2022-06-14 18:35:10 +0800669if [ "$ACTION" = "$ACTION_AC_ON" ]; then
670 if [ "$(get_ac_status "$SLED_NUM")" = "$HOST_AC_OFF" ]; then
671 do_action_ac_on "$SLED_NUM"
672 fi
673elif [ "$ACTION" = "$ACTION_AC_OFF" ]; then
674 if [ "$(get_ac_status "$SLED_NUM")" != "$HOST_AC_OFF" ]; then
675 do_action_ac_off "$SLED_NUM"
676 fi
677elif [ "$ACTION" = "$ACTION_STATUS" ];then
678 HOST_CURR_STATUS=$(get_host_status "$SLED_NUM")
Potin Lai27083c32022-06-02 19:13:24 +0800679 echo "$HOST_CURR_STATUS"
Allen.Wang4a0948d2021-12-15 13:41:18 +0800680else
Potin Laia8d258f2022-06-14 18:35:10 +0800681 HOST_CURR_STATUS=$(get_host_status "$SLED_NUM")
Potin Lai27083c32022-06-02 19:13:24 +0800682 case $HOST_CURR_STATUS in
683 "$HOST_AC_OFF")
684 host_state_ac_off_action_handler "$SLED_NUM" "$ACTION"
685 ;;
686 "$HOST_AC_ON")
687 host_state_ac_on_action_handler "$SLED_NUM" "$ACTION"
688 ;;
689 "$HOST_ST_OFF")
690 host_state_off_action_handler "$SLED_NUM" "$ACTION"
691 ;;
692 "$HOST_ST_ON")
693 host_state_on_action_handler "$SLED_NUM" "$ACTION"
694 ;;
695 "$HOST_ST_SLEEP")
696 host_state_sleep_action_handler "$SLED_NUM" "$ACTION"
697 ;;
698 "$HOST_ST_DFU")
699 host_state_dfu_action_handler "$SLED_NUM" "$ACTION"
700 ;;
701 "$HOST_ST_RECOVERY")
702 host_state_recovery_action_handler "$SLED_NUM" "$ACTION"
703 ;;
704 esac
Allen.Wang4a0948d2021-12-15 13:41:18 +0800705fi