blob: 8de9e5afa78e1decdc0018b072044a1b4a6741dc [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"
Potin Lai6a0b3d32022-11-14 19:12:08 +080042ACTION_BOOT_MODE="boot-from-bootmode"
Potin Lai27083c32022-06-02 19:13:24 +080043
44VALID_SLED_ACTIONS="
45 $ACTION_ON
46 $ACTION_OFF
47 $ACTION_AC_ON
48 $ACTION_AC_OFF
49 $ACTION_STATUS
50 $ACTION_DFU
51 $ACTION_RECOVERY
Potin Lai2320b0e2022-06-28 10:38:37 +080052 $ACTION_CYCLE
53 $ACTION_RESET
Potin Lai6a0b3d32022-11-14 19:12:08 +080054 $ACTION_BOOT_MODE
Potin Lai27083c32022-06-02 19:13:24 +080055"
56
Potin Lai6a0b3d32022-11-14 19:12:08 +080057BOOT_MODE_REGULAR="\"xyz.openbmc_project.Control.Boot.Mode.Modes.Regular\""
58BOOT_MODE_SAFE="\"xyz.openbmc_project.Control.Boot.Mode.Modes.Safe\""
59BOOT_MODE_SETUP="\"xyz.openbmc_project.Control.Boot.Mode.Modes.Setup\""
60
Potin Lai27083c32022-06-02 19:13:24 +080061function is_valid_sled_action()
62{
63 local ACTION=$1
64 for i in $VALID_SLED_ACTIONS
65 do
66 if [ "$i" = "$ACTION" ]; then
67 return 0
68 fi
69 done
70 return 1
71}
72
73function get_board_rev()
74{
Potin Laibb91c1b2022-04-28 15:03:05 +080075 local rev_id0
76 local rev_id1
77 local rev_id2
78 local rev_val
79
80 rev_id0=$(get_gpio "REV_ID0")
81 rev_id1=$(get_gpio "REV_ID1")
82 rev_id2=$(get_gpio "REV_ID2")
83 rev_val=$((rev_id0+(rev_id1<<1)+(rev_id2<<2)))
84
85 case $rev_val in
86 0)
87 echo "$REV_EVT"
88 ;;
89 1)
90 echo "$REV_DVT"
91 ;;
92 *)
93 echo "$REV_UNKNOW"
94 return 1
95 ;;
96 esac
97
98 return 0
99}
100
Allen.Wang4a0948d2021-12-15 13:41:18 +0800101#Switch pull low while it be touched
Potin Lai27083c32022-06-02 19:13:24 +0800102function wait_for_switch()
103{
Allen.Wang4a0948d2021-12-15 13:41:18 +0800104 TARGET_PIN=$1
105 TARGET_SWITCH=1
106 TIME_CNT=0
107 while [ "$TARGET_SWITCH" -eq 1 ] ;do
108 TARGET_SWITCH=$(get_gpio "$TARGET_PIN")
109 sleep 0.1
110 TIME_CNT=$(( TIME_CNT +1))
111 if [ $TIME_CNT -gt $POWER_BTN_TIMEOUT_CNT ];then
112 echo "Error: Too long to get target switch, force exit" >&2
113 break
114 fi
115 done
116}
117
Potin Lai27083c32022-06-02 19:13:24 +0800118function trigger_power_button()
119{
Allen.Wang4a0948d2021-12-15 13:41:18 +0800120 local sled_num=$1
Allen.Wang6af0dff2021-12-28 20:23:05 +0800121 local delay_time=$2
Allen.Wang4a0948d2021-12-15 13:41:18 +0800122
123 #SLED{N}_MS_DETECT1 (initial position)
124 GPIO_DETECT_PIN1="SLED${sled_num}_MS_DETECT1"
125 #SLED{N}_MS_DETECT0 (MAC position)
126 GPIO_DETECT_PIN0="SLED${sled_num}_MS_DETECT0"
127
128 echo "Motor go forward to press Power key"
129 motor-ctrl "sled${sled_num}" f >/dev/null
130 wait_for_switch "${GPIO_DETECT_PIN0}"
131 motor-ctrl "sled${sled_num}" s >/dev/null
132
133 if [ "$(get_gpio "$GPIO_DETECT_PIN0")" -eq 0 ];then
134 echo "Power key switch triggered"
135 echo "Press power key for Sled${1} ${delay_time} seconds..."
136 sleep "$delay_time"
137 else
Allen.Wang6af0dff2021-12-28 20:23:05 +0800138 echo "Power key switch not trigger, back motor to initial position"
Allen.Wang4a0948d2021-12-15 13:41:18 +0800139 fi
140
141 motor-ctrl "sled${sled_num}" r >/dev/null
142 wait_for_switch "${GPIO_DETECT_PIN1}"
143 motor-ctrl "sled${sled_num}" s >/dev/null
144 if [ "$(get_gpio "$GPIO_DETECT_PIN1")" -eq 0 ];then
145 echo "Motor reverse to initial position successful"
146 else
Allen.Wang6af0dff2021-12-28 20:23:05 +0800147 echo "Initial position switch not trigger, force stop motor"
Allen.Wang4a0948d2021-12-15 13:41:18 +0800148 fi
149}
150
Potin Lai27083c32022-06-02 19:13:24 +0800151function release_power_button()
152{
Potin Laidb5648e2022-02-16 00:57:55 +0800153 local sled_num=$1
154 GPIO_DETECT_PIN1="SLED${sled_num}_MS_DETECT1"
155
156 if [ "$(get_gpio "$GPIO_DETECT_PIN1")" -eq 0 ]; then
157 echo "Motor at initial position already"
158 return 0
159 fi
160
161 motor-ctrl "sled${sled_num}" r >/dev/null
162 wait_for_switch "${GPIO_DETECT_PIN1}"
163 motor-ctrl "sled${sled_num}" s >/dev/null
164 if [ "$(get_gpio "$GPIO_DETECT_PIN1")" -eq 0 ];then
165 echo "Motor reverse to initial position successful"
166 return 0
167 fi
168
169 echo "Error: Initial position switch not trigger"
170 return 1
171}
172
Potin Lai27083c32022-06-02 19:13:24 +0800173function press_power_button()
174{
Potin Laidb5648e2022-02-16 00:57:55 +0800175 local sled_num=$1
176
177 GPIO_DETECT_PIN0="SLED${sled_num}_MS_DETECT0"
178
179 echo "Motor go forward to press Power button"
180 motor-ctrl "sled${sled_num}" f >/dev/null
181 wait_for_switch "${GPIO_DETECT_PIN0}"
182 motor-ctrl "sled${sled_num}" s >/dev/null
183
184 if [ "$(get_gpio "$GPIO_DETECT_PIN0")" -eq 0 ];then
185 echo "Power button switch triggered"
186 return 0
187 fi
188
189 echo "Error: Power button switch not trigger"
190 return 1
191}
192
Allen.Wang4a0948d2021-12-15 13:41:18 +0800193#Get i2c bus number for sledN
Potin Lai27083c32022-06-02 19:13:24 +0800194function get_bus_num()
195{
Allen.Wang4a0948d2021-12-15 13:41:18 +0800196 SLED_NUM=$1
197 local bus=0
Allen.Wang5ff992e2022-01-04 21:00:38 +0800198 #Mapping Sled number 1~6 to i2c bus number 0~5
199 if [[ "$SLED_NUM" = [1-6] ]]; then
200 bus=$(( SLED_NUM - 1 ))
Allen.Wang4a0948d2021-12-15 13:41:18 +0800201 fi
202 echo "$bus"
203}
204
Potin Lai27083c32022-06-02 19:13:24 +0800205function get_ac_status()
206{
Allen.Wang4a0948d2021-12-15 13:41:18 +0800207 i2c_bus=$(get_bus_num "$1")
208 p1_output_reg=$(i2cget -f -y "$i2c_bus" 0x76 0x03)
209 p1_config_reg=$(i2cget -f -y "$i2c_bus" 0x76 0x07)
210 host_pwr="$(( (p1_output_reg & 0x80)>>7 ))"
211 is_output="$(( (~p1_config_reg & 0x80)>>7 ))"
212
213 if [ "$(( host_pwr & is_output ))" -eq 1 ];then
Potin Laibb91c1b2022-04-28 15:03:05 +0800214 echo "$HOST_AC_ON"
Allen.Wang4a0948d2021-12-15 13:41:18 +0800215 else
Potin Laibb91c1b2022-04-28 15:03:05 +0800216 echo "$HOST_AC_OFF"
Allen.Wang4a0948d2021-12-15 13:41:18 +0800217 fi
218}
219
Potin Laibb91c1b2022-04-28 15:03:05 +0800220function get_host_status_dbus()
221{
222 local sled_num=$1
223 local object="/xyz/openbmc_project/state/host${sled_num}"
224 local service="xyz.openbmc_project.State.Host${sled_num}"
225 local interface="xyz.openbmc_project.State.Host"
226 local property="CurrentHostState"
227 local host_state
228
229 host_state=$(busctl get-property "$service" "$object" "$interface" "$property" | cut -d '"' -f2)
230
231 if [ "$host_state" = "$DBUS_HOST_ST_ON" ]; then
232 echo "$HOST_ST_ON"
233 elif [ "$host_state" = "$DBUS_HOST_ST_OFF" ]; then
234 echo "$HOST_ST_OFF"
235 else
236 echo "$HOST_ST_UNKNOW"
237 return 1
238 fi
239
240 return 0
241}
242
Potin Laib8f52ae2022-09-19 14:20:58 +0800243function get_addr_from_dts_aliases()
244{
245 local node_address
246 node_address=$(awk -F '@' '{printf $2}' /sys/firmware/devicetree/base/aliases/"$1")
247 echo "$node_address"
248}
249
Potin Laibb91c1b2022-04-28 15:03:05 +0800250function get_host_status_mdio()
Potin Laidc251662022-04-01 11:16:50 +0800251{
Potin Lai27083c32022-06-02 19:13:24 +0800252 local SLED_NUM=$1
Potin Laib8f52ae2022-09-19 14:20:58 +0800253 local MDIO_BUS
254
255 MDIO_BUS="$(get_addr_from_dts_aliases mdio0).mdio-1"
Potin Laibb91c1b2022-04-28 15:03:05 +0800256
Potin Laidc251662022-04-01 11:16:50 +0800257 declare -a PORT_MAP=(0 3 2 1 7 6 5)
258
259 # check /dev/mem
260 if ! create_dev_mem; then
261 return 1
262 fi
263
Potin Lai27083c32022-06-02 19:13:24 +0800264 local CHECK_CNT=0
265 local MDIO_ERR_CNT=0
266 local CUR_HOST_ST=$HOST_ST_UNKNOW
267 local SLED_LAST_ACTION
268
269 if [ -f /tmp/sled"${SLED_NUM}"-last-action ]; then
270 SLED_LAST_ACTION=$(cat /tmp/sled"${SLED_NUM}"-last-action)
271 fi
Potin Laidc251662022-04-01 11:16:50 +0800272
273 while true
274 do
Potin Laib8f52ae2022-09-19 14:20:58 +0800275 if POST_ST_VAL=$(mdio "$MDIO_BUS" phy "${PORT_MAP[SLED_NUM]}" 0); then
Potin Lai27083c32022-06-02 19:13:24 +0800276 if [ $((POST_ST_VAL&16#0800)) -eq $((16#0000)) ]; then
277 case $SLED_LAST_ACTION in
278 "$ACTION_DFU")
279 TMP_HOST_ST="$HOST_ST_DFU"
280 ;;
281 *)
282 TMP_HOST_ST="$HOST_ST_OFF"
283 ;;
284 esac
285 elif [ $((POST_ST_VAL&16#0A00)) -eq $((16#0A00)) ]; then
Potin Laidc251662022-04-01 11:16:50 +0800286 TMP_HOST_ST="$HOST_ST_ON"
Potin Lai27083c32022-06-02 19:13:24 +0800287 case $SLED_LAST_ACTION in
288 "$ACTION_RECOVERY")
289 TMP_HOST_ST="$HOST_ST_RECOVERY"
290 ;;
291 *)
292 TMP_HOST_ST="$HOST_ST_ON"
293 ;;
294 esac
295 elif [ $((POST_ST_VAL&16#0900)) -eq $((16#0900)) ]; then
296 TMP_HOST_ST="$HOST_ST_SLEEP"
Potin Laidc251662022-04-01 11:16:50 +0800297 else
Potin Lai27083c32022-06-02 19:13:24 +0800298 TMP_HOST_ST="$HOST_ST_UNKNOW"
Potin Laidc251662022-04-01 11:16:50 +0800299 fi
300
301 if [ "$CUR_HOST_ST" == "$TMP_HOST_ST" ]; then
302 CHECK_CNT=$((CHECK_CNT+1))
303 else
304 CUR_HOST_ST=$TMP_HOST_ST
305 CHECK_CNT=0
306 fi
307
308 if [ "$CHECK_CNT" -ge 5 ]; then
309 echo "$CUR_HOST_ST"
310 break
311 fi
312 else
313 MDIO_ERR_CNT=$((MDIO_ERR_CNT+1))
314 if [ "$MDIO_ERR_CNT" -ge 5 ]; then
315 echo "$HOST_ST_UNKNOW"
316 return 1
317 fi
318 fi
319 done
320
321 return 0
322}
323
Potin Laibb91c1b2022-04-28 15:03:05 +0800324function get_host_status()
325{
326 local sled_num=$1
Potin Laibb91c1b2022-04-28 15:03:05 +0800327
Potin Lai27083c32022-06-02 19:13:24 +0800328 if [ "$(get_ac_status "$SLED_NUM")" == "$HOST_AC_OFF" ];then
329 echo "$HOST_AC_OFF"
330 return 0
331 fi
Potin Laibb91c1b2022-04-28 15:03:05 +0800332
Potin Lai27083c32022-06-02 19:13:24 +0800333 if [ "$(get_board_rev)" = "$REV_EVT" ]; then
Potin Laibb91c1b2022-04-28 15:03:05 +0800334 get_host_status_dbus "$sled_num"
335 else
336 get_host_status_mdio "$sled_num"
337 fi
Potin Laibb91c1b2022-04-28 15:03:05 +0800338 return $?
339}
340
Potin Lai6a0b3d32022-11-14 19:12:08 +0800341function get_host_bootmode()
342{
343 local BUS_NAME="xyz.openbmc_project.Settings"
344 local OBJ_PATH="/xyz/openbmc_project/control/host${1}/boot"
345 local INTF_NAME="xyz.openbmc_project.Control.Boot.Mode"
346 busctl get-property "${BUS_NAME}" "${OBJ_PATH}" "${INTF_NAME}" BootMode | awk '{print $2}'
347}
348
Potin Lai2320b0e2022-06-28 10:38:37 +0800349function do_action_reset()
350{
351 # 1. Power off
352 # 2. Power on
353
354 local SLED_NUM=$1
355 local CUR_ST=$2
356
357 if [ "$CUR_ST" != "$HOST_ST_OFF" ]; then
358 do_action_off "$SLED_NUM"
359 else
360 echo "sled${SLED_NUM}: already powered off"
361 fi
362
363 sleep 3
364 do_action_on "$SLED_NUM"
365}
366
367function do_action_cycle()
368{
369 # 1. AC off
370 # 2. AC on
371 # 3. Power on
372
373 local SLED_NUM=$1
374
375 do_action_ac_off "$SLED_NUM"
376 sleep 3
377 do_action_ac_on "$SLED_NUM"
378 sleep 3
379 do_action_on "$SLED_NUM"
380}
381
Potin Lai27083c32022-06-02 19:13:24 +0800382function do_action_ac_on()
383{
384 local SLED_NUM=$1
385 echo "sled${SLED_NUM}: turn on AC"
386 set_gpio "power-host${SLED_NUM}" 1
387 echo "$ACTION_AC_ON" > "/tmp/sled${SLED_NUM}-last-action"
388}
389
390function do_action_ac_off()
391{
392 local SLED_NUM=$1
393 echo "sled${SLED_NUM}: turn off AC"
394 set_gpio "power-host${SLED_NUM}" 0
395 echo "$ACTION_AC_OFF" > "/tmp/sled${SLED_NUM}-last-action"
396}
397
398function do_action_on()
399{
400 local SLED_NUM=$1
401 echo "sled${SLED_NUM}: power on host"
402 trigger_power_button "$SLED_NUM" "$DELAY_POWER_ON"
Potin Laiaafe1872022-08-19 06:56:49 +0000403 sleep 10 # Mac mini need about 10 second to stable link status
Potin Lai27083c32022-06-02 19:13:24 +0800404 echo "$ACTION_ON" > "/tmp/sled${SLED_NUM}-last-action"
405}
406
407function do_action_off()
408{
409 local SLED_NUM=$1
410 echo "sled${SLED_NUM}: power off host"
411 trigger_power_button "$SLED_NUM" "$DELAY_POWER_OFF"
412 echo "$ACTION_OFF" > "/tmp/sled${SLED_NUM}-last-action"
413}
414
415function do_action_recovery()
416{
417 local SLED_NUM=$1
418 echo "sled${SLED_NUM}: trigger host recovery mode"
419 trigger_power_button "$SLED_NUM" "$DELAY_POWER_RECOVERY_MODE"
420 echo "$ACTION_RECOVERY" > "/tmp/sled${SLED_NUM}-last-action"
421}
422
423function do_action_dfu()
424{
425 local SLED_NUM=$1
426 echo "sled${SLED_NUM}: trigger host dfu mode"
427
428 # turn ac off, and hold for 25 seconds
429 do_action_ac_off "$SLED_NUM"
430 sleep 25
431
432 # press power button
433 echo "SLED$SLED_NUM: pressing power button"
434 if ! press_power_button "$SLED_NUM"; then
435 echo "SLED$SLED_NUM: press power button failed"
436 echo "SLED$SLED_NUM: releasing power button"
437 release_power_button "$SLED_NUM"
Potin Lai6a0b3d32022-11-14 19:12:08 +0800438 return 1
Potin Lai27083c32022-06-02 19:13:24 +0800439 fi
440 sleep 1
441
442 # turn ac on
443 echo "SLED$SLED_NUM: turn ac-on"
444 do_action_ac_on "$SLED_NUM"
445 sleep 3
446
447 # release power button
448 echo "SLED$SLED_NUM: releasing host power button"
449 if ! release_power_button "$SLED_NUM"; then
450 echo "SLED$SLED_NUM: release power button failed"
Potin Lai6a0b3d32022-11-14 19:12:08 +0800451 return 1
Potin Lai27083c32022-06-02 19:13:24 +0800452 fi
453 echo "$ACTION_DFU" > "/tmp/sled${SLED_NUM}-last-action"
454}
455
456function host_state_on_action_handler()
457{
458 local SLED_NUM=$1
459 local ACTION=$2
460
461 case $ACTION in
462 "$ACTION_OFF")
463 do_action_off "$SLED_NUM"
464 ;;
465 "$ACTION_AC_OFF")
466 do_action_ac_off "$SLED_NUM"
467 ;;
Potin Lai2320b0e2022-06-28 10:38:37 +0800468 "$ACTION_RESET")
469 do_action_reset "$SLED_NUM" "$HOST_ST_ON"
470 ;;
471 "$ACTION_CYCLE")
472 do_action_cycle "$SLED_NUM"
473 ;;
Potin Lai27083c32022-06-02 19:13:24 +0800474 *)
475 echo "Invalid action ($ACTION) for current host state (On)"
476 return 1
477 ;;
478 esac
479}
480
481function host_state_sleep_action_handler()
482{
483 local SLED_NUM=$1
484 local ACTION=$2
485
486 case $ACTION in
487 "$ACTION_ON")
488 do_action_on "$SLED_NUM"
489 ;;
490 "$ACTION_OFF")
491 do_action_off "$SLED_NUM"
492 ;;
493 "$ACTION_AC_OFF")
494 do_action_ac_off "$SLED_NUM"
495 ;;
Potin Lai2320b0e2022-06-28 10:38:37 +0800496 "$ACTION_RESET")
497 do_action_reset "$SLED_NUM" "$HOST_ST_ON"
498 ;;
499 "$ACTION_CYCLE")
500 do_action_cycle "$SLED_NUM"
501 ;;
Potin Lai27083c32022-06-02 19:13:24 +0800502 *)
503 echo "Invalid action ($ACTION) for current host state (Sleep)"
504 return 1
505 ;;
506 esac
507}
508
509function host_state_off_action_handler()
510{
511 local SLED_NUM=$1
512 local ACTION=$2
513
514 case $ACTION in
515 "$ACTION_ON")
516 do_action_on "$SLED_NUM"
517 ;;
518 "$ACTION_RECOVERY")
519 do_action_recovery "$SLED_NUM"
520 ;;
Potin Lai6a0b3d32022-11-14 19:12:08 +0800521 "$ACTION_DFU")
522 do_action_dfu "$SLED_NUM"
523 ;;
Potin Lai27083c32022-06-02 19:13:24 +0800524 "$ACTION_AC_OFF")
525 do_action_ac_off "$SLED_NUM"
526 ;;
Potin Lai2320b0e2022-06-28 10:38:37 +0800527 "$ACTION_RESET")
528 do_action_reset "$SLED_NUM" "$HOST_ST_ON"
529 ;;
530 "$ACTION_CYCLE")
531 do_action_cycle "$SLED_NUM"
532 ;;
Potin Lai27083c32022-06-02 19:13:24 +0800533 *)
534 echo "Invalid action ($ACTION) for current host state (Off)"
535 return 1
536 ;;
537 esac
538}
539
540function host_state_ac_off_action_handler()
541{
542 local SLED_NUM=$1
543 local ACTION=$2
544
545 case $ACTION in
546 "$ACTION_AC_ON")
547 do_action_ac_on "$SLED_NUM"
548 ;;
549 "$ACTION_DFU")
550 do_action_dfu "$SLED_NUM"
551 ;;
552 "$ACTION_AC_OFF")
553 echo "sled${SLED_NUM}: already ac off"
554 return 1
555 ;;
Potin Lai2320b0e2022-06-28 10:38:37 +0800556 "$ACTION_CYCLE")
557 do_action_reset "$SLED_NUM"
558 ;;
Potin Lai27083c32022-06-02 19:13:24 +0800559 *)
560 echo "Invalid action ($ACTION) for current host state (AC Off)"
561 return 1
562 ;;
563 esac
564}
565
566function host_state_ac_on_action_handler()
567{
568 local SLED_NUM=$1
569 local ACTION=$2
570
571 case $ACTION in
572 "$ACTION_AC_OFF")
573 do_action_ac_off "$SLED_NUM"
574 ;;
Potin Lai6a0b3d32022-11-14 19:12:08 +0800575 "$ACTION_DFU")
576 do_action_dfu "$SLED_NUM"
577 ;;
Potin Lai2320b0e2022-06-28 10:38:37 +0800578 "$ACTION_CYCLE")
579 do_action_cycle "$SLED_NUM"
580 ;;
Potin Lai27083c32022-06-02 19:13:24 +0800581 *)
582 echo "sled${SLED_NUM}: already ac on"
583 return 1
584 ;;
585 esac
586}
587
588function host_state_recovery_action_handler()
589{
590 local SLED_NUM=$1
591 local ACTION=$2
592
593 case $ACTION in
594 "$ACTION_OFF")
595 do_action_off "$SLED_NUM"
596 ;;
597 "$ACTION_AC_OFF")
598 do_action_ac_off "$SLED_NUM"
599 ;;
Potin Lai2320b0e2022-06-28 10:38:37 +0800600 "$ACTION_RESET")
601 do_action_reset "$SLED_NUM" "$HOST_ST_ON"
602 ;;
603 "$ACTION_CYCLE")
604 do_action_cycle "$SLED_NUM"
605 ;;
Potin Lai27083c32022-06-02 19:13:24 +0800606 *)
607 echo "Invalid action ($ACTION) for current host state (Recovery)"
608 return 1
609 ;;
610 esac
611}
612
613function host_state_dfu_action_handler()
614{
615 local SLED_NUM=$1
616 local ACTION=$2
617
618 case $ACTION in
619 "$ACTION_AC_OFF")
620 do_action_ac_off "$SLED_NUM"
621 ;;
Potin Lai2320b0e2022-06-28 10:38:37 +0800622 "$ACTION_CYCLE")
623 do_action_cycle "$SLED_NUM"
624 ;;
Potin Lai27083c32022-06-02 19:13:24 +0800625 *)
626 echo "Invalid action ($ACTION) for current host state (DFU)"
627 return 1
628 ;;
629 esac
630}
631
Potin Laidc251662022-04-01 11:16:50 +0800632function create_dev_mem()
633{
634 CHECK_CNT=0
635 while true
636 do
637 CHECK_CNT=$((CHECK_CNT+1))
638 if [ -c /dev/mem ]; then
639 # /dev/mem already exist
640 return 0
641 elif mknod /dev/mem c 1 1; then
642 # mknod success
643 return 0
644 elif [ "$CHECK_CNT" -ge 5 ]; then
645 break
646 fi
647 sleep 1
648 done
649
650 echo "create /dev/mem failed"
651 return 1
652}
653
Allen.Wang4a0948d2021-12-15 13:41:18 +0800654function show_usage(){
Potin Lai27083c32022-06-02 19:13:24 +0800655 echo "Usage: power-ctrl [sled1 | sled2 | sled3 | sled4 | sled5 | sled6] [$VALID_SLED_ACTIONS]"
Allen.Wang4a0948d2021-12-15 13:41:18 +0800656 echo " power-ctrl chassis-cycle"
657}
658
Potin Lai27083c32022-06-02 19:13:24 +0800659
Allen.Wang4a0948d2021-12-15 13:41:18 +0800660if [ $# -eq 1 ]; then
661 if [ "$1" = "chassis-cycle" ];then
662 echo "chassis cycle...."
663 i2cset -y -f 12 0x11 0xd9 c
664 exit 0
665 else
666 echo "Invalid argument: [ $1 ]"
667 show_usage
668 exit 1;
669 fi
670fi
671
672if [ $# -gt 2 ]; then
673 echo "Too many arguments"
674 show_usage
675 exit 1;
676fi
677
Allen.Wang5ff992e2022-01-04 21:00:38 +0800678if [[ "$1" =~ ^(sled[1-6]{1})$ ]]; then
Allen.Wang4a0948d2021-12-15 13:41:18 +0800679 SLED=$1
680 ACTION=$2
681 SLED_NUM=${SLED:4}
682else
683 echo "invalid sled name: ${1}"
684 show_usage
685 exit 1;
686fi
687
688#Check if sled is present
Potin Lai501f4c72022-06-13 13:23:57 +0800689if ! is_sled_present "${SLED_NUM}"; then
Allen.Wang4a0948d2021-12-15 13:41:18 +0800690 echo "${SLED} is not present!"
691 exit 1
Potin Lai27083c32022-06-02 19:13:24 +0800692elif ! is_valid_sled_action "$ACTION"; then
693 echo "Unknown action: $ACTION"
694 show_usage
695 exit 1
Allen.Wang4a0948d2021-12-15 13:41:18 +0800696fi
697
Potin Laia8d258f2022-06-14 18:35:10 +0800698if [ "$ACTION" = "$ACTION_AC_ON" ]; then
699 if [ "$(get_ac_status "$SLED_NUM")" = "$HOST_AC_OFF" ]; then
700 do_action_ac_on "$SLED_NUM"
701 fi
702elif [ "$ACTION" = "$ACTION_AC_OFF" ]; then
703 if [ "$(get_ac_status "$SLED_NUM")" != "$HOST_AC_OFF" ]; then
704 do_action_ac_off "$SLED_NUM"
705 fi
706elif [ "$ACTION" = "$ACTION_STATUS" ];then
707 HOST_CURR_STATUS=$(get_host_status "$SLED_NUM")
Potin Lai27083c32022-06-02 19:13:24 +0800708 echo "$HOST_CURR_STATUS"
Allen.Wang4a0948d2021-12-15 13:41:18 +0800709else
Potin Laia8d258f2022-06-14 18:35:10 +0800710 HOST_CURR_STATUS=$(get_host_status "$SLED_NUM")
Potin Lai6a0b3d32022-11-14 19:12:08 +0800711
712 if [ "$ACTION" = "$ACTION_BOOT_MODE" ]; then
713 BOOT_MODE=$(get_host_bootmode "$SLED_NUM")
714 case "$BOOT_MODE" in
715 "$BOOT_MODE_REGULAR")
716 echo "Boot mode: on (regular)"
717 ACTION="$ACTION_ON"
718 ;;
719 "$BOOT_MODE_SAFE")
720 echo "Boot mode: recovery (safe)"
721 ACTION="$ACTION_RECOVERY"
722 ;;
723 "$BOOT_MODE_SETUP")
724 echo "Boot mode: dfu (setup)"
725 ACTION="$ACTION_DFU"
726 ;;
727 *)
728 echo "Boot mode: unknow"
729 ;;
730 esac
731 fi
732
Potin Lai27083c32022-06-02 19:13:24 +0800733 case $HOST_CURR_STATUS in
734 "$HOST_AC_OFF")
735 host_state_ac_off_action_handler "$SLED_NUM" "$ACTION"
736 ;;
737 "$HOST_AC_ON")
738 host_state_ac_on_action_handler "$SLED_NUM" "$ACTION"
739 ;;
740 "$HOST_ST_OFF")
741 host_state_off_action_handler "$SLED_NUM" "$ACTION"
742 ;;
743 "$HOST_ST_ON")
744 host_state_on_action_handler "$SLED_NUM" "$ACTION"
745 ;;
746 "$HOST_ST_SLEEP")
747 host_state_sleep_action_handler "$SLED_NUM" "$ACTION"
748 ;;
749 "$HOST_ST_DFU")
750 host_state_dfu_action_handler "$SLED_NUM" "$ACTION"
751 ;;
752 "$HOST_ST_RECOVERY")
753 host_state_recovery_action_handler "$SLED_NUM" "$ACTION"
754 ;;
755 esac
Allen.Wang4a0948d2021-12-15 13:41:18 +0800756fi