blob: 5fe14dbe03208a17053ac8bfeb22d38d88b2497a [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"
Potin Lai8f2d3f22023-06-06 19:41:09 +080013DELAY_POWER_OFF="10"
Potin Lai27083c32022-06-02 19:13:24 +080014DELAY_POWER_RECOVERY_MODE="10"
Potin Laid2e7f202023-02-23 19:12:32 +080015POWER_BTN_TIMEOUT_SEC=10
Allen.Wang4a0948d2021-12-15 13:41:18 +080016
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
Potin Lai27083c32022-06-02 19:13:24 +0800101function trigger_power_button()
102{
Allen.Wang4a0948d2021-12-15 13:41:18 +0800103 local sled_num=$1
Allen.Wang6af0dff2021-12-28 20:23:05 +0800104 local delay_time=$2
Allen.Wang4a0948d2021-12-15 13:41:18 +0800105
106 #SLED{N}_MS_DETECT1 (initial position)
107 GPIO_DETECT_PIN1="SLED${sled_num}_MS_DETECT1"
108 #SLED{N}_MS_DETECT0 (MAC position)
109 GPIO_DETECT_PIN0="SLED${sled_num}_MS_DETECT0"
110
111 echo "Motor go forward to press Power key"
112 motor-ctrl "sled${sled_num}" f >/dev/null
Potin Laid2e7f202023-02-23 19:12:32 +0800113 wait_gpio_falling "${GPIO_DETECT_PIN0}" "$POWER_BTN_TIMEOUT_SEC"
Allen.Wang4a0948d2021-12-15 13:41:18 +0800114 motor-ctrl "sled${sled_num}" s >/dev/null
115
116 if [ "$(get_gpio "$GPIO_DETECT_PIN0")" -eq 0 ];then
117 echo "Power key switch triggered"
118 echo "Press power key for Sled${1} ${delay_time} seconds..."
119 sleep "$delay_time"
120 else
Allen.Wang6af0dff2021-12-28 20:23:05 +0800121 echo "Power key switch not trigger, back motor to initial position"
Allen.Wang4a0948d2021-12-15 13:41:18 +0800122 fi
123
124 motor-ctrl "sled${sled_num}" r >/dev/null
Potin Laid2e7f202023-02-23 19:12:32 +0800125 wait_gpio_falling "${GPIO_DETECT_PIN1}" "$POWER_BTN_TIMEOUT_SEC"
Allen.Wang4a0948d2021-12-15 13:41:18 +0800126 motor-ctrl "sled${sled_num}" s >/dev/null
127 if [ "$(get_gpio "$GPIO_DETECT_PIN1")" -eq 0 ];then
128 echo "Motor reverse to initial position successful"
129 else
Allen.Wang6af0dff2021-12-28 20:23:05 +0800130 echo "Initial position switch not trigger, force stop motor"
Allen.Wang4a0948d2021-12-15 13:41:18 +0800131 fi
132}
133
Potin Lai27083c32022-06-02 19:13:24 +0800134function release_power_button()
135{
Potin Laidb5648e2022-02-16 00:57:55 +0800136 local sled_num=$1
137 GPIO_DETECT_PIN1="SLED${sled_num}_MS_DETECT1"
138
139 if [ "$(get_gpio "$GPIO_DETECT_PIN1")" -eq 0 ]; then
140 echo "Motor at initial position already"
141 return 0
142 fi
143
144 motor-ctrl "sled${sled_num}" r >/dev/null
Potin Laid2e7f202023-02-23 19:12:32 +0800145 wait_gpio_falling "${GPIO_DETECT_PIN1}" "$POWER_BTN_TIMEOUT_SEC"
Potin Laidb5648e2022-02-16 00:57:55 +0800146 motor-ctrl "sled${sled_num}" s >/dev/null
147 if [ "$(get_gpio "$GPIO_DETECT_PIN1")" -eq 0 ];then
148 echo "Motor reverse to initial position successful"
149 return 0
150 fi
151
152 echo "Error: Initial position switch not trigger"
153 return 1
154}
155
Potin Lai27083c32022-06-02 19:13:24 +0800156function press_power_button()
157{
Potin Laidb5648e2022-02-16 00:57:55 +0800158 local sled_num=$1
159
160 GPIO_DETECT_PIN0="SLED${sled_num}_MS_DETECT0"
161
162 echo "Motor go forward to press Power button"
163 motor-ctrl "sled${sled_num}" f >/dev/null
Potin Laid2e7f202023-02-23 19:12:32 +0800164 wait_gpio_falling "${GPIO_DETECT_PIN0}" "$POWER_BTN_TIMEOUT_SEC"
Potin Laidb5648e2022-02-16 00:57:55 +0800165 motor-ctrl "sled${sled_num}" s >/dev/null
166
167 if [ "$(get_gpio "$GPIO_DETECT_PIN0")" -eq 0 ];then
168 echo "Power button switch triggered"
169 return 0
170 fi
171
172 echo "Error: Power button switch not trigger"
173 return 1
174}
175
Allen.Wang4a0948d2021-12-15 13:41:18 +0800176#Get i2c bus number for sledN
Potin Lai27083c32022-06-02 19:13:24 +0800177function get_bus_num()
178{
Allen.Wang4a0948d2021-12-15 13:41:18 +0800179 SLED_NUM=$1
180 local bus=0
Allen.Wang5ff992e2022-01-04 21:00:38 +0800181 #Mapping Sled number 1~6 to i2c bus number 0~5
182 if [[ "$SLED_NUM" = [1-6] ]]; then
183 bus=$(( SLED_NUM - 1 ))
Allen.Wang4a0948d2021-12-15 13:41:18 +0800184 fi
185 echo "$bus"
186}
187
Potin Lai27083c32022-06-02 19:13:24 +0800188function get_ac_status()
189{
Allen.Wang4a0948d2021-12-15 13:41:18 +0800190 i2c_bus=$(get_bus_num "$1")
191 p1_output_reg=$(i2cget -f -y "$i2c_bus" 0x76 0x03)
192 p1_config_reg=$(i2cget -f -y "$i2c_bus" 0x76 0x07)
193 host_pwr="$(( (p1_output_reg & 0x80)>>7 ))"
194 is_output="$(( (~p1_config_reg & 0x80)>>7 ))"
195
196 if [ "$(( host_pwr & is_output ))" -eq 1 ];then
Potin Laibb91c1b2022-04-28 15:03:05 +0800197 echo "$HOST_AC_ON"
Allen.Wang4a0948d2021-12-15 13:41:18 +0800198 else
Potin Laibb91c1b2022-04-28 15:03:05 +0800199 echo "$HOST_AC_OFF"
Allen.Wang4a0948d2021-12-15 13:41:18 +0800200 fi
201}
202
Potin Laibb91c1b2022-04-28 15:03:05 +0800203function get_host_status_dbus()
204{
205 local sled_num=$1
206 local object="/xyz/openbmc_project/state/host${sled_num}"
207 local service="xyz.openbmc_project.State.Host${sled_num}"
208 local interface="xyz.openbmc_project.State.Host"
209 local property="CurrentHostState"
210 local host_state
211
212 host_state=$(busctl get-property "$service" "$object" "$interface" "$property" | cut -d '"' -f2)
213
214 if [ "$host_state" = "$DBUS_HOST_ST_ON" ]; then
215 echo "$HOST_ST_ON"
216 elif [ "$host_state" = "$DBUS_HOST_ST_OFF" ]; then
217 echo "$HOST_ST_OFF"
218 else
219 echo "$HOST_ST_UNKNOW"
220 return 1
221 fi
222
223 return 0
224}
225
Potin Laib8f52ae2022-09-19 14:20:58 +0800226function get_addr_from_dts_aliases()
227{
228 local node_address
229 node_address=$(awk -F '@' '{printf $2}' /sys/firmware/devicetree/base/aliases/"$1")
230 echo "$node_address"
231}
232
Potin Laibb91c1b2022-04-28 15:03:05 +0800233function get_host_status_mdio()
Potin Laidc251662022-04-01 11:16:50 +0800234{
Potin Lai27083c32022-06-02 19:13:24 +0800235 local SLED_NUM=$1
Potin Laib8f52ae2022-09-19 14:20:58 +0800236 local MDIO_BUS
237
238 MDIO_BUS="$(get_addr_from_dts_aliases mdio0).mdio-1"
Potin Laibb91c1b2022-04-28 15:03:05 +0800239
Potin Laidc251662022-04-01 11:16:50 +0800240 declare -a PORT_MAP=(0 3 2 1 7 6 5)
241
242 # check /dev/mem
243 if ! create_dev_mem; then
244 return 1
245 fi
246
Potin Lai27083c32022-06-02 19:13:24 +0800247 local CHECK_CNT=0
248 local MDIO_ERR_CNT=0
249 local CUR_HOST_ST=$HOST_ST_UNKNOW
250 local SLED_LAST_ACTION
251
252 if [ -f /tmp/sled"${SLED_NUM}"-last-action ]; then
253 SLED_LAST_ACTION=$(cat /tmp/sled"${SLED_NUM}"-last-action)
254 fi
Potin Laidc251662022-04-01 11:16:50 +0800255
256 while true
257 do
Potin Laib8f52ae2022-09-19 14:20:58 +0800258 if POST_ST_VAL=$(mdio "$MDIO_BUS" phy "${PORT_MAP[SLED_NUM]}" 0); then
Potin Lai27083c32022-06-02 19:13:24 +0800259 if [ $((POST_ST_VAL&16#0800)) -eq $((16#0000)) ]; then
260 case $SLED_LAST_ACTION in
261 "$ACTION_DFU")
262 TMP_HOST_ST="$HOST_ST_DFU"
263 ;;
264 *)
265 TMP_HOST_ST="$HOST_ST_OFF"
266 ;;
267 esac
268 elif [ $((POST_ST_VAL&16#0A00)) -eq $((16#0A00)) ]; then
Potin Laidc251662022-04-01 11:16:50 +0800269 TMP_HOST_ST="$HOST_ST_ON"
Potin Lai27083c32022-06-02 19:13:24 +0800270 case $SLED_LAST_ACTION in
271 "$ACTION_RECOVERY")
272 TMP_HOST_ST="$HOST_ST_RECOVERY"
273 ;;
274 *)
275 TMP_HOST_ST="$HOST_ST_ON"
276 ;;
277 esac
278 elif [ $((POST_ST_VAL&16#0900)) -eq $((16#0900)) ]; then
279 TMP_HOST_ST="$HOST_ST_SLEEP"
Potin Laidc251662022-04-01 11:16:50 +0800280 else
Potin Lai27083c32022-06-02 19:13:24 +0800281 TMP_HOST_ST="$HOST_ST_UNKNOW"
Potin Laidc251662022-04-01 11:16:50 +0800282 fi
283
284 if [ "$CUR_HOST_ST" == "$TMP_HOST_ST" ]; then
285 CHECK_CNT=$((CHECK_CNT+1))
286 else
287 CUR_HOST_ST=$TMP_HOST_ST
288 CHECK_CNT=0
289 fi
290
291 if [ "$CHECK_CNT" -ge 5 ]; then
292 echo "$CUR_HOST_ST"
293 break
294 fi
295 else
296 MDIO_ERR_CNT=$((MDIO_ERR_CNT+1))
297 if [ "$MDIO_ERR_CNT" -ge 5 ]; then
298 echo "$HOST_ST_UNKNOW"
299 return 1
300 fi
301 fi
302 done
303
304 return 0
305}
306
Potin Laibb91c1b2022-04-28 15:03:05 +0800307function get_host_status()
308{
309 local sled_num=$1
Potin Laibb91c1b2022-04-28 15:03:05 +0800310
Potin Lai27083c32022-06-02 19:13:24 +0800311 if [ "$(get_ac_status "$SLED_NUM")" == "$HOST_AC_OFF" ];then
312 echo "$HOST_AC_OFF"
313 return 0
314 fi
Potin Laibb91c1b2022-04-28 15:03:05 +0800315
Potin Lai27083c32022-06-02 19:13:24 +0800316 if [ "$(get_board_rev)" = "$REV_EVT" ]; then
Potin Laibb91c1b2022-04-28 15:03:05 +0800317 get_host_status_dbus "$sled_num"
318 else
319 get_host_status_mdio "$sled_num"
320 fi
Potin Laibb91c1b2022-04-28 15:03:05 +0800321 return $?
322}
323
Potin Lai6a0b3d32022-11-14 19:12:08 +0800324function get_host_bootmode()
325{
326 local BUS_NAME="xyz.openbmc_project.Settings"
327 local OBJ_PATH="/xyz/openbmc_project/control/host${1}/boot"
328 local INTF_NAME="xyz.openbmc_project.Control.Boot.Mode"
329 busctl get-property "${BUS_NAME}" "${OBJ_PATH}" "${INTF_NAME}" BootMode | awk '{print $2}'
330}
331
Potin Lai2320b0e2022-06-28 10:38:37 +0800332function do_action_reset()
333{
334 # 1. Power off
335 # 2. Power on
336
337 local SLED_NUM=$1
338 local CUR_ST=$2
339
340 if [ "$CUR_ST" != "$HOST_ST_OFF" ]; then
341 do_action_off "$SLED_NUM"
342 else
343 echo "sled${SLED_NUM}: already powered off"
344 fi
345
346 sleep 3
347 do_action_on "$SLED_NUM"
348}
349
350function do_action_cycle()
351{
352 # 1. AC off
353 # 2. AC on
354 # 3. Power on
355
356 local SLED_NUM=$1
357
358 do_action_ac_off "$SLED_NUM"
359 sleep 3
360 do_action_ac_on "$SLED_NUM"
361 sleep 3
362 do_action_on "$SLED_NUM"
363}
364
Potin Lai27083c32022-06-02 19:13:24 +0800365function do_action_ac_on()
366{
367 local SLED_NUM=$1
368 echo "sled${SLED_NUM}: turn on AC"
369 set_gpio "power-host${SLED_NUM}" 1
370 echo "$ACTION_AC_ON" > "/tmp/sled${SLED_NUM}-last-action"
371}
372
373function do_action_ac_off()
374{
375 local SLED_NUM=$1
376 echo "sled${SLED_NUM}: turn off AC"
377 set_gpio "power-host${SLED_NUM}" 0
378 echo "$ACTION_AC_OFF" > "/tmp/sled${SLED_NUM}-last-action"
379}
380
381function do_action_on()
382{
383 local SLED_NUM=$1
384 echo "sled${SLED_NUM}: power on host"
385 trigger_power_button "$SLED_NUM" "$DELAY_POWER_ON"
Potin Laiaafe1872022-08-19 06:56:49 +0000386 sleep 10 # Mac mini need about 10 second to stable link status
Potin Lai27083c32022-06-02 19:13:24 +0800387 echo "$ACTION_ON" > "/tmp/sled${SLED_NUM}-last-action"
388}
389
390function do_action_off()
391{
392 local SLED_NUM=$1
393 echo "sled${SLED_NUM}: power off host"
394 trigger_power_button "$SLED_NUM" "$DELAY_POWER_OFF"
395 echo "$ACTION_OFF" > "/tmp/sled${SLED_NUM}-last-action"
396}
397
398function do_action_recovery()
399{
400 local SLED_NUM=$1
401 echo "sled${SLED_NUM}: trigger host recovery mode"
402 trigger_power_button "$SLED_NUM" "$DELAY_POWER_RECOVERY_MODE"
403 echo "$ACTION_RECOVERY" > "/tmp/sled${SLED_NUM}-last-action"
404}
405
406function do_action_dfu()
407{
408 local SLED_NUM=$1
409 echo "sled${SLED_NUM}: trigger host dfu mode"
410
411 # turn ac off, and hold for 25 seconds
412 do_action_ac_off "$SLED_NUM"
413 sleep 25
414
415 # press power button
416 echo "SLED$SLED_NUM: pressing power button"
417 if ! press_power_button "$SLED_NUM"; then
418 echo "SLED$SLED_NUM: press power button failed"
419 echo "SLED$SLED_NUM: releasing power button"
420 release_power_button "$SLED_NUM"
Potin Lai6a0b3d32022-11-14 19:12:08 +0800421 return 1
Potin Lai27083c32022-06-02 19:13:24 +0800422 fi
423 sleep 1
424
425 # turn ac on
426 echo "SLED$SLED_NUM: turn ac-on"
427 do_action_ac_on "$SLED_NUM"
428 sleep 3
429
430 # release power button
431 echo "SLED$SLED_NUM: releasing host power button"
432 if ! release_power_button "$SLED_NUM"; then
433 echo "SLED$SLED_NUM: release power button failed"
Potin Lai6a0b3d32022-11-14 19:12:08 +0800434 return 1
Potin Lai27083c32022-06-02 19:13:24 +0800435 fi
436 echo "$ACTION_DFU" > "/tmp/sled${SLED_NUM}-last-action"
437}
438
439function host_state_on_action_handler()
440{
441 local SLED_NUM=$1
442 local ACTION=$2
443
444 case $ACTION in
445 "$ACTION_OFF")
446 do_action_off "$SLED_NUM"
447 ;;
448 "$ACTION_AC_OFF")
449 do_action_ac_off "$SLED_NUM"
450 ;;
Potin Lai2320b0e2022-06-28 10:38:37 +0800451 "$ACTION_RESET")
452 do_action_reset "$SLED_NUM" "$HOST_ST_ON"
453 ;;
454 "$ACTION_CYCLE")
455 do_action_cycle "$SLED_NUM"
456 ;;
Potin Lai27083c32022-06-02 19:13:24 +0800457 *)
458 echo "Invalid action ($ACTION) for current host state (On)"
459 return 1
460 ;;
461 esac
462}
463
464function host_state_sleep_action_handler()
465{
466 local SLED_NUM=$1
467 local ACTION=$2
468
469 case $ACTION in
470 "$ACTION_ON")
471 do_action_on "$SLED_NUM"
472 ;;
473 "$ACTION_OFF")
474 do_action_off "$SLED_NUM"
475 ;;
476 "$ACTION_AC_OFF")
477 do_action_ac_off "$SLED_NUM"
478 ;;
Potin Lai2320b0e2022-06-28 10:38:37 +0800479 "$ACTION_RESET")
480 do_action_reset "$SLED_NUM" "$HOST_ST_ON"
481 ;;
482 "$ACTION_CYCLE")
483 do_action_cycle "$SLED_NUM"
484 ;;
Potin Lai27083c32022-06-02 19:13:24 +0800485 *)
486 echo "Invalid action ($ACTION) for current host state (Sleep)"
487 return 1
488 ;;
489 esac
490}
491
492function host_state_off_action_handler()
493{
494 local SLED_NUM=$1
495 local ACTION=$2
496
497 case $ACTION in
498 "$ACTION_ON")
499 do_action_on "$SLED_NUM"
500 ;;
501 "$ACTION_RECOVERY")
502 do_action_recovery "$SLED_NUM"
503 ;;
Potin Lai6a0b3d32022-11-14 19:12:08 +0800504 "$ACTION_DFU")
505 do_action_dfu "$SLED_NUM"
506 ;;
Potin Lai27083c32022-06-02 19:13:24 +0800507 "$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 Lai6a0b3d32022-11-14 19:12:08 +0800558 "$ACTION_DFU")
559 do_action_dfu "$SLED_NUM"
560 ;;
Potin Lai2320b0e2022-06-28 10:38:37 +0800561 "$ACTION_CYCLE")
562 do_action_cycle "$SLED_NUM"
563 ;;
Potin Lai27083c32022-06-02 19:13:24 +0800564 *)
565 echo "sled${SLED_NUM}: already ac on"
566 return 1
567 ;;
568 esac
569}
570
571function host_state_recovery_action_handler()
572{
573 local SLED_NUM=$1
574 local ACTION=$2
575
576 case $ACTION in
577 "$ACTION_OFF")
578 do_action_off "$SLED_NUM"
579 ;;
580 "$ACTION_AC_OFF")
581 do_action_ac_off "$SLED_NUM"
582 ;;
Potin Lai2320b0e2022-06-28 10:38:37 +0800583 "$ACTION_RESET")
584 do_action_reset "$SLED_NUM" "$HOST_ST_ON"
585 ;;
586 "$ACTION_CYCLE")
587 do_action_cycle "$SLED_NUM"
588 ;;
Potin Lai27083c32022-06-02 19:13:24 +0800589 *)
590 echo "Invalid action ($ACTION) for current host state (Recovery)"
591 return 1
592 ;;
593 esac
594}
595
596function host_state_dfu_action_handler()
597{
598 local SLED_NUM=$1
599 local ACTION=$2
600
601 case $ACTION in
602 "$ACTION_AC_OFF")
603 do_action_ac_off "$SLED_NUM"
604 ;;
Potin Lai2320b0e2022-06-28 10:38:37 +0800605 "$ACTION_CYCLE")
606 do_action_cycle "$SLED_NUM"
607 ;;
Potin Lai27083c32022-06-02 19:13:24 +0800608 *)
609 echo "Invalid action ($ACTION) for current host state (DFU)"
610 return 1
611 ;;
612 esac
613}
614
Potin Laidc251662022-04-01 11:16:50 +0800615function create_dev_mem()
616{
617 CHECK_CNT=0
618 while true
619 do
620 CHECK_CNT=$((CHECK_CNT+1))
621 if [ -c /dev/mem ]; then
622 # /dev/mem already exist
623 return 0
624 elif mknod /dev/mem c 1 1; then
625 # mknod success
626 return 0
627 elif [ "$CHECK_CNT" -ge 5 ]; then
628 break
629 fi
630 sleep 1
631 done
632
633 echo "create /dev/mem failed"
634 return 1
635}
636
Allen.Wang4a0948d2021-12-15 13:41:18 +0800637function show_usage(){
Potin Lai27083c32022-06-02 19:13:24 +0800638 echo "Usage: power-ctrl [sled1 | sled2 | sled3 | sled4 | sled5 | sled6] [$VALID_SLED_ACTIONS]"
Allen.Wang4a0948d2021-12-15 13:41:18 +0800639 echo " power-ctrl chassis-cycle"
640}
641
Potin Lai27083c32022-06-02 19:13:24 +0800642
Allen.Wang4a0948d2021-12-15 13:41:18 +0800643if [ $# -eq 1 ]; then
644 if [ "$1" = "chassis-cycle" ];then
645 echo "chassis cycle...."
646 i2cset -y -f 12 0x11 0xd9 c
647 exit 0
648 else
649 echo "Invalid argument: [ $1 ]"
650 show_usage
651 exit 1;
652 fi
653fi
654
655if [ $# -gt 2 ]; then
656 echo "Too many arguments"
657 show_usage
658 exit 1;
659fi
660
Allen.Wang5ff992e2022-01-04 21:00:38 +0800661if [[ "$1" =~ ^(sled[1-6]{1})$ ]]; then
Allen.Wang4a0948d2021-12-15 13:41:18 +0800662 SLED=$1
663 ACTION=$2
664 SLED_NUM=${SLED:4}
665else
666 echo "invalid sled name: ${1}"
667 show_usage
668 exit 1;
669fi
670
671#Check if sled is present
Potin Lai501f4c72022-06-13 13:23:57 +0800672if ! is_sled_present "${SLED_NUM}"; then
Allen.Wang4a0948d2021-12-15 13:41:18 +0800673 echo "${SLED} is not present!"
674 exit 1
Potin Lai27083c32022-06-02 19:13:24 +0800675elif ! is_valid_sled_action "$ACTION"; then
676 echo "Unknown action: $ACTION"
677 show_usage
678 exit 1
Allen.Wang4a0948d2021-12-15 13:41:18 +0800679fi
680
Potin Laia8d258f2022-06-14 18:35:10 +0800681if [ "$ACTION" = "$ACTION_AC_ON" ]; then
682 if [ "$(get_ac_status "$SLED_NUM")" = "$HOST_AC_OFF" ]; then
683 do_action_ac_on "$SLED_NUM"
684 fi
685elif [ "$ACTION" = "$ACTION_AC_OFF" ]; then
686 if [ "$(get_ac_status "$SLED_NUM")" != "$HOST_AC_OFF" ]; then
687 do_action_ac_off "$SLED_NUM"
688 fi
689elif [ "$ACTION" = "$ACTION_STATUS" ];then
690 HOST_CURR_STATUS=$(get_host_status "$SLED_NUM")
Potin Lai27083c32022-06-02 19:13:24 +0800691 echo "$HOST_CURR_STATUS"
Allen.Wang4a0948d2021-12-15 13:41:18 +0800692else
Potin Laia8d258f2022-06-14 18:35:10 +0800693 HOST_CURR_STATUS=$(get_host_status "$SLED_NUM")
Potin Lai6a0b3d32022-11-14 19:12:08 +0800694
695 if [ "$ACTION" = "$ACTION_BOOT_MODE" ]; then
696 BOOT_MODE=$(get_host_bootmode "$SLED_NUM")
697 case "$BOOT_MODE" in
698 "$BOOT_MODE_REGULAR")
699 echo "Boot mode: on (regular)"
700 ACTION="$ACTION_ON"
701 ;;
702 "$BOOT_MODE_SAFE")
703 echo "Boot mode: recovery (safe)"
704 ACTION="$ACTION_RECOVERY"
705 ;;
706 "$BOOT_MODE_SETUP")
707 echo "Boot mode: dfu (setup)"
708 ACTION="$ACTION_DFU"
709 ;;
710 *)
711 echo "Boot mode: unknow"
712 ;;
713 esac
714 fi
715
Potin Lai27083c32022-06-02 19:13:24 +0800716 case $HOST_CURR_STATUS in
717 "$HOST_AC_OFF")
718 host_state_ac_off_action_handler "$SLED_NUM" "$ACTION"
719 ;;
720 "$HOST_AC_ON")
721 host_state_ac_on_action_handler "$SLED_NUM" "$ACTION"
722 ;;
723 "$HOST_ST_OFF")
724 host_state_off_action_handler "$SLED_NUM" "$ACTION"
725 ;;
726 "$HOST_ST_ON")
727 host_state_on_action_handler "$SLED_NUM" "$ACTION"
728 ;;
729 "$HOST_ST_SLEEP")
730 host_state_sleep_action_handler "$SLED_NUM" "$ACTION"
731 ;;
732 "$HOST_ST_DFU")
733 host_state_dfu_action_handler "$SLED_NUM" "$ACTION"
734 ;;
735 "$HOST_ST_RECOVERY")
736 host_state_recovery_action_handler "$SLED_NUM" "$ACTION"
737 ;;
738 esac
Allen.Wang4a0948d2021-12-15 13:41:18 +0800739fi