Allen.Wang | 4a0948d | 2021-12-15 13:41:18 +0800 | [diff] [blame] | 1 | #!/bin/bash |
| 2 | # |
| 3 | # Power Control tool |
| 4 | # Enable/disable AC relay |
| 5 | # On/off System by step moter to press power key |
| 6 | |
| 7 | export PATH=$PATH:/usr/sbin:/usr/libexec |
| 8 | |
| 9 | DELAY_POWER_ON="0.5" |
| 10 | DELAY_POWER_OFF="5" |
Potin Lai | 27083c3 | 2022-06-02 19:13:24 +0800 | [diff] [blame^] | 11 | DELAY_POWER_RECOVERY_MODE="10" |
Allen.Wang | 4a0948d | 2021-12-15 13:41:18 +0800 | [diff] [blame] | 12 | POWER_BTN_TIMEOUT_CNT=60 |
| 13 | |
Potin Lai | bb91c1b | 2022-04-28 15:03:05 +0800 | [diff] [blame] | 14 | REV_EVT="EVT" |
| 15 | REV_DVT="DVT" |
| 16 | REV_UNKNOW="UNKNOW" |
| 17 | |
| 18 | DBUS_HOST_ST_ON="xyz.openbmc_project.State.Host.HostState.Running" |
| 19 | DBUS_HOST_ST_OFF="xyz.openbmc_project.State.Host.HostState.Off" |
| 20 | |
| 21 | HOST_ST_UNKNOW="Unknow" |
| 22 | HOST_ST_ON="On" |
| 23 | HOST_ST_OFF="Off" |
Potin Lai | 27083c3 | 2022-06-02 19:13:24 +0800 | [diff] [blame^] | 24 | HOST_ST_SLEEP="Sleep" |
| 25 | HOST_ST_DFU="DFU" |
| 26 | HOST_ST_RECOVERY="Recovery" |
Potin Lai | bb91c1b | 2022-04-28 15:03:05 +0800 | [diff] [blame] | 27 | HOST_AC_ON="AC On" |
| 28 | HOST_AC_OFF="AC Off" |
| 29 | |
Potin Lai | 27083c3 | 2022-06-02 19:13:24 +0800 | [diff] [blame^] | 30 | ACTION_ON="on" |
| 31 | ACTION_OFF="off" |
| 32 | ACTION_DFU="dfu" |
| 33 | ACTION_RECOVERY="recovery" |
| 34 | ACTION_AC_ON="ac-on" |
| 35 | ACTION_AC_OFF="ac-off" |
| 36 | ACTION_STATUS="status" |
| 37 | |
| 38 | VALID_SLED_ACTIONS=" |
| 39 | $ACTION_ON |
| 40 | $ACTION_OFF |
| 41 | $ACTION_AC_ON |
| 42 | $ACTION_AC_OFF |
| 43 | $ACTION_STATUS |
| 44 | $ACTION_DFU |
| 45 | $ACTION_RECOVERY |
| 46 | " |
| 47 | |
| 48 | function is_valid_sled_action() |
| 49 | { |
| 50 | local ACTION=$1 |
| 51 | for i in $VALID_SLED_ACTIONS |
| 52 | do |
| 53 | if [ "$i" = "$ACTION" ]; then |
| 54 | return 0 |
| 55 | fi |
| 56 | done |
| 57 | return 1 |
| 58 | } |
| 59 | |
| 60 | function get_board_rev() |
| 61 | { |
Potin Lai | bb91c1b | 2022-04-28 15:03:05 +0800 | [diff] [blame] | 62 | local rev_id0 |
| 63 | local rev_id1 |
| 64 | local rev_id2 |
| 65 | local rev_val |
| 66 | |
| 67 | rev_id0=$(get_gpio "REV_ID0") |
| 68 | rev_id1=$(get_gpio "REV_ID1") |
| 69 | rev_id2=$(get_gpio "REV_ID2") |
| 70 | rev_val=$((rev_id0+(rev_id1<<1)+(rev_id2<<2))) |
| 71 | |
| 72 | case $rev_val in |
| 73 | 0) |
| 74 | echo "$REV_EVT" |
| 75 | ;; |
| 76 | 1) |
| 77 | echo "$REV_DVT" |
| 78 | ;; |
| 79 | *) |
| 80 | echo "$REV_UNKNOW" |
| 81 | return 1 |
| 82 | ;; |
| 83 | esac |
| 84 | |
| 85 | return 0 |
| 86 | } |
| 87 | |
Allen.Wang | 4a0948d | 2021-12-15 13:41:18 +0800 | [diff] [blame] | 88 | #Switch pull low while it be touched |
Potin Lai | 27083c3 | 2022-06-02 19:13:24 +0800 | [diff] [blame^] | 89 | function wait_for_switch() |
| 90 | { |
Allen.Wang | 4a0948d | 2021-12-15 13:41:18 +0800 | [diff] [blame] | 91 | TARGET_PIN=$1 |
| 92 | TARGET_SWITCH=1 |
| 93 | TIME_CNT=0 |
| 94 | while [ "$TARGET_SWITCH" -eq 1 ] ;do |
| 95 | TARGET_SWITCH=$(get_gpio "$TARGET_PIN") |
| 96 | sleep 0.1 |
| 97 | TIME_CNT=$(( TIME_CNT +1)) |
| 98 | if [ $TIME_CNT -gt $POWER_BTN_TIMEOUT_CNT ];then |
| 99 | echo "Error: Too long to get target switch, force exit" >&2 |
| 100 | break |
| 101 | fi |
| 102 | done |
| 103 | } |
| 104 | |
Potin Lai | 27083c3 | 2022-06-02 19:13:24 +0800 | [diff] [blame^] | 105 | function trigger_power_button() |
| 106 | { |
Allen.Wang | 4a0948d | 2021-12-15 13:41:18 +0800 | [diff] [blame] | 107 | local sled_num=$1 |
Allen.Wang | 6af0dff | 2021-12-28 20:23:05 +0800 | [diff] [blame] | 108 | local delay_time=$2 |
Allen.Wang | 4a0948d | 2021-12-15 13:41:18 +0800 | [diff] [blame] | 109 | |
| 110 | #SLED{N}_MS_DETECT1 (initial position) |
| 111 | GPIO_DETECT_PIN1="SLED${sled_num}_MS_DETECT1" |
| 112 | #SLED{N}_MS_DETECT0 (MAC position) |
| 113 | GPIO_DETECT_PIN0="SLED${sled_num}_MS_DETECT0" |
| 114 | |
| 115 | echo "Motor go forward to press Power key" |
| 116 | motor-ctrl "sled${sled_num}" f >/dev/null |
| 117 | wait_for_switch "${GPIO_DETECT_PIN0}" |
| 118 | motor-ctrl "sled${sled_num}" s >/dev/null |
| 119 | |
| 120 | if [ "$(get_gpio "$GPIO_DETECT_PIN0")" -eq 0 ];then |
| 121 | echo "Power key switch triggered" |
| 122 | echo "Press power key for Sled${1} ${delay_time} seconds..." |
| 123 | sleep "$delay_time" |
| 124 | else |
Allen.Wang | 6af0dff | 2021-12-28 20:23:05 +0800 | [diff] [blame] | 125 | echo "Power key switch not trigger, back motor to initial position" |
Allen.Wang | 4a0948d | 2021-12-15 13:41:18 +0800 | [diff] [blame] | 126 | fi |
| 127 | |
| 128 | motor-ctrl "sled${sled_num}" r >/dev/null |
| 129 | wait_for_switch "${GPIO_DETECT_PIN1}" |
| 130 | motor-ctrl "sled${sled_num}" s >/dev/null |
| 131 | if [ "$(get_gpio "$GPIO_DETECT_PIN1")" -eq 0 ];then |
| 132 | echo "Motor reverse to initial position successful" |
| 133 | else |
Allen.Wang | 6af0dff | 2021-12-28 20:23:05 +0800 | [diff] [blame] | 134 | echo "Initial position switch not trigger, force stop motor" |
Allen.Wang | 4a0948d | 2021-12-15 13:41:18 +0800 | [diff] [blame] | 135 | fi |
| 136 | } |
| 137 | |
Potin Lai | 27083c3 | 2022-06-02 19:13:24 +0800 | [diff] [blame^] | 138 | function release_power_button() |
| 139 | { |
Potin Lai | db5648e | 2022-02-16 00:57:55 +0800 | [diff] [blame] | 140 | local sled_num=$1 |
| 141 | GPIO_DETECT_PIN1="SLED${sled_num}_MS_DETECT1" |
| 142 | |
| 143 | if [ "$(get_gpio "$GPIO_DETECT_PIN1")" -eq 0 ]; then |
| 144 | echo "Motor at initial position already" |
| 145 | return 0 |
| 146 | fi |
| 147 | |
| 148 | motor-ctrl "sled${sled_num}" r >/dev/null |
| 149 | wait_for_switch "${GPIO_DETECT_PIN1}" |
| 150 | motor-ctrl "sled${sled_num}" s >/dev/null |
| 151 | if [ "$(get_gpio "$GPIO_DETECT_PIN1")" -eq 0 ];then |
| 152 | echo "Motor reverse to initial position successful" |
| 153 | return 0 |
| 154 | fi |
| 155 | |
| 156 | echo "Error: Initial position switch not trigger" |
| 157 | return 1 |
| 158 | } |
| 159 | |
Potin Lai | 27083c3 | 2022-06-02 19:13:24 +0800 | [diff] [blame^] | 160 | function press_power_button() |
| 161 | { |
Potin Lai | db5648e | 2022-02-16 00:57:55 +0800 | [diff] [blame] | 162 | local sled_num=$1 |
| 163 | |
| 164 | GPIO_DETECT_PIN0="SLED${sled_num}_MS_DETECT0" |
| 165 | |
| 166 | echo "Motor go forward to press Power button" |
| 167 | motor-ctrl "sled${sled_num}" f >/dev/null |
| 168 | wait_for_switch "${GPIO_DETECT_PIN0}" |
| 169 | motor-ctrl "sled${sled_num}" s >/dev/null |
| 170 | |
| 171 | if [ "$(get_gpio "$GPIO_DETECT_PIN0")" -eq 0 ];then |
| 172 | echo "Power button switch triggered" |
| 173 | return 0 |
| 174 | fi |
| 175 | |
| 176 | echo "Error: Power button switch not trigger" |
| 177 | return 1 |
| 178 | } |
| 179 | |
Allen.Wang | 4a0948d | 2021-12-15 13:41:18 +0800 | [diff] [blame] | 180 | #Get i2c bus number for sledN |
Potin Lai | 27083c3 | 2022-06-02 19:13:24 +0800 | [diff] [blame^] | 181 | function get_bus_num() |
| 182 | { |
Allen.Wang | 4a0948d | 2021-12-15 13:41:18 +0800 | [diff] [blame] | 183 | SLED_NUM=$1 |
| 184 | local bus=0 |
Allen.Wang | 5ff992e | 2022-01-04 21:00:38 +0800 | [diff] [blame] | 185 | #Mapping Sled number 1~6 to i2c bus number 0~5 |
| 186 | if [[ "$SLED_NUM" = [1-6] ]]; then |
| 187 | bus=$(( SLED_NUM - 1 )) |
Allen.Wang | 4a0948d | 2021-12-15 13:41:18 +0800 | [diff] [blame] | 188 | fi |
| 189 | echo "$bus" |
| 190 | } |
| 191 | |
| 192 | function set_gpio() |
| 193 | { |
| 194 | NET_NAME=$1 |
| 195 | OUT_VAL=$2 |
| 196 | mapfile -t -d " " GPIO_INFO < <(gpiofind "$NET_NAME") |
| 197 | if [ "${#GPIO_INFO[@]}" -ne 2 ]; then |
| 198 | echo "set_gpio: can not find gpio, $NET_NAME" |
| 199 | return 1 |
| 200 | fi |
| 201 | echo -n "set_gpio: set $NET_NAME = $OUT_VAL" |
| 202 | if ! gpioset "${GPIO_INFO[0]}" "${GPIO_INFO[1]%$'\n'}"="$OUT_VAL"; then |
| 203 | echo " failed" |
| 204 | return 1 |
| 205 | fi |
| 206 | echo " success" |
| 207 | return 0 |
| 208 | } |
| 209 | |
| 210 | function get_gpio() |
| 211 | { |
| 212 | NET_NAME=$1 |
| 213 | RET_VAL=2 |
| 214 | |
| 215 | mapfile -t -d " " GPIO_INFO < <(gpiofind "$NET_NAME") |
| 216 | if [ "${#GPIO_INFO[@]}" -ne 2 ]; then |
| 217 | echo "get_gpio: can not find gpio, $NET_NAME" >&2 |
| 218 | return 1 |
| 219 | fi |
| 220 | if ! RET_VAL=$(gpioget "${GPIO_INFO[0]}" "${GPIO_INFO[1]%$'\n'}") ; then |
| 221 | echo "get_gpio: get ${NET_NAME} failed" >&2 |
| 222 | return 1 |
| 223 | fi |
| 224 | echo "${RET_VAL}" |
| 225 | return 0 |
| 226 | } |
| 227 | |
Potin Lai | 27083c3 | 2022-06-02 19:13:24 +0800 | [diff] [blame^] | 228 | function get_ac_status() |
| 229 | { |
Allen.Wang | 4a0948d | 2021-12-15 13:41:18 +0800 | [diff] [blame] | 230 | i2c_bus=$(get_bus_num "$1") |
| 231 | p1_output_reg=$(i2cget -f -y "$i2c_bus" 0x76 0x03) |
| 232 | p1_config_reg=$(i2cget -f -y "$i2c_bus" 0x76 0x07) |
| 233 | host_pwr="$(( (p1_output_reg & 0x80)>>7 ))" |
| 234 | is_output="$(( (~p1_config_reg & 0x80)>>7 ))" |
| 235 | |
| 236 | if [ "$(( host_pwr & is_output ))" -eq 1 ];then |
Potin Lai | bb91c1b | 2022-04-28 15:03:05 +0800 | [diff] [blame] | 237 | echo "$HOST_AC_ON" |
Allen.Wang | 4a0948d | 2021-12-15 13:41:18 +0800 | [diff] [blame] | 238 | else |
Potin Lai | bb91c1b | 2022-04-28 15:03:05 +0800 | [diff] [blame] | 239 | echo "$HOST_AC_OFF" |
Allen.Wang | 4a0948d | 2021-12-15 13:41:18 +0800 | [diff] [blame] | 240 | fi |
| 241 | } |
| 242 | |
Potin Lai | bb91c1b | 2022-04-28 15:03:05 +0800 | [diff] [blame] | 243 | function get_host_status_dbus() |
| 244 | { |
| 245 | local sled_num=$1 |
| 246 | local object="/xyz/openbmc_project/state/host${sled_num}" |
| 247 | local service="xyz.openbmc_project.State.Host${sled_num}" |
| 248 | local interface="xyz.openbmc_project.State.Host" |
| 249 | local property="CurrentHostState" |
| 250 | local host_state |
| 251 | |
| 252 | host_state=$(busctl get-property "$service" "$object" "$interface" "$property" | cut -d '"' -f2) |
| 253 | |
| 254 | if [ "$host_state" = "$DBUS_HOST_ST_ON" ]; then |
| 255 | echo "$HOST_ST_ON" |
| 256 | elif [ "$host_state" = "$DBUS_HOST_ST_OFF" ]; then |
| 257 | echo "$HOST_ST_OFF" |
| 258 | else |
| 259 | echo "$HOST_ST_UNKNOW" |
| 260 | return 1 |
| 261 | fi |
| 262 | |
| 263 | return 0 |
| 264 | } |
| 265 | |
| 266 | function get_host_status_mdio() |
Potin Lai | dc25166 | 2022-04-01 11:16:50 +0800 | [diff] [blame] | 267 | { |
Potin Lai | 27083c3 | 2022-06-02 19:13:24 +0800 | [diff] [blame^] | 268 | local SLED_NUM=$1 |
| 269 | local MDIO_BUS=0 |
Potin Lai | bb91c1b | 2022-04-28 15:03:05 +0800 | [diff] [blame] | 270 | |
Potin Lai | dc25166 | 2022-04-01 11:16:50 +0800 | [diff] [blame] | 271 | declare -a PORT_MAP=(0 3 2 1 7 6 5) |
| 272 | |
| 273 | # check /dev/mem |
| 274 | if ! create_dev_mem; then |
| 275 | return 1 |
| 276 | fi |
| 277 | |
Potin Lai | 27083c3 | 2022-06-02 19:13:24 +0800 | [diff] [blame^] | 278 | local CHECK_CNT=0 |
| 279 | local MDIO_ERR_CNT=0 |
| 280 | local CUR_HOST_ST=$HOST_ST_UNKNOW |
| 281 | local SLED_LAST_ACTION |
| 282 | |
| 283 | if [ -f /tmp/sled"${SLED_NUM}"-last-action ]; then |
| 284 | SLED_LAST_ACTION=$(cat /tmp/sled"${SLED_NUM}"-last-action) |
| 285 | fi |
Potin Lai | dc25166 | 2022-04-01 11:16:50 +0800 | [diff] [blame] | 286 | |
| 287 | while true |
| 288 | do |
| 289 | if POST_ST_VAL=$(mdio-util c22 r $MDIO_BUS "${PORT_MAP[SLED_NUM]}" 0); then |
Potin Lai | 27083c3 | 2022-06-02 19:13:24 +0800 | [diff] [blame^] | 290 | if [ $((POST_ST_VAL&16#0800)) -eq $((16#0000)) ]; then |
| 291 | case $SLED_LAST_ACTION in |
| 292 | "$ACTION_DFU") |
| 293 | TMP_HOST_ST="$HOST_ST_DFU" |
| 294 | ;; |
| 295 | *) |
| 296 | TMP_HOST_ST="$HOST_ST_OFF" |
| 297 | ;; |
| 298 | esac |
| 299 | elif [ $((POST_ST_VAL&16#0A00)) -eq $((16#0A00)) ]; then |
Potin Lai | dc25166 | 2022-04-01 11:16:50 +0800 | [diff] [blame] | 300 | TMP_HOST_ST="$HOST_ST_ON" |
Potin Lai | 27083c3 | 2022-06-02 19:13:24 +0800 | [diff] [blame^] | 301 | case $SLED_LAST_ACTION in |
| 302 | "$ACTION_RECOVERY") |
| 303 | TMP_HOST_ST="$HOST_ST_RECOVERY" |
| 304 | ;; |
| 305 | *) |
| 306 | TMP_HOST_ST="$HOST_ST_ON" |
| 307 | ;; |
| 308 | esac |
| 309 | elif [ $((POST_ST_VAL&16#0900)) -eq $((16#0900)) ]; then |
| 310 | TMP_HOST_ST="$HOST_ST_SLEEP" |
Potin Lai | dc25166 | 2022-04-01 11:16:50 +0800 | [diff] [blame] | 311 | else |
Potin Lai | 27083c3 | 2022-06-02 19:13:24 +0800 | [diff] [blame^] | 312 | TMP_HOST_ST="$HOST_ST_UNKNOW" |
Potin Lai | dc25166 | 2022-04-01 11:16:50 +0800 | [diff] [blame] | 313 | fi |
| 314 | |
| 315 | if [ "$CUR_HOST_ST" == "$TMP_HOST_ST" ]; then |
| 316 | CHECK_CNT=$((CHECK_CNT+1)) |
| 317 | else |
| 318 | CUR_HOST_ST=$TMP_HOST_ST |
| 319 | CHECK_CNT=0 |
| 320 | fi |
| 321 | |
| 322 | if [ "$CHECK_CNT" -ge 5 ]; then |
| 323 | echo "$CUR_HOST_ST" |
| 324 | break |
| 325 | fi |
| 326 | else |
| 327 | MDIO_ERR_CNT=$((MDIO_ERR_CNT+1)) |
| 328 | if [ "$MDIO_ERR_CNT" -ge 5 ]; then |
| 329 | echo "$HOST_ST_UNKNOW" |
| 330 | return 1 |
| 331 | fi |
| 332 | fi |
| 333 | done |
| 334 | |
| 335 | return 0 |
| 336 | } |
| 337 | |
Potin Lai | bb91c1b | 2022-04-28 15:03:05 +0800 | [diff] [blame] | 338 | function get_host_status() |
| 339 | { |
| 340 | local sled_num=$1 |
Potin Lai | bb91c1b | 2022-04-28 15:03:05 +0800 | [diff] [blame] | 341 | |
Potin Lai | 27083c3 | 2022-06-02 19:13:24 +0800 | [diff] [blame^] | 342 | if [ "$(get_ac_status "$SLED_NUM")" == "$HOST_AC_OFF" ];then |
| 343 | echo "$HOST_AC_OFF" |
| 344 | return 0 |
| 345 | fi |
Potin Lai | bb91c1b | 2022-04-28 15:03:05 +0800 | [diff] [blame] | 346 | |
Potin Lai | 27083c3 | 2022-06-02 19:13:24 +0800 | [diff] [blame^] | 347 | if [ "$(get_board_rev)" = "$REV_EVT" ]; then |
Potin Lai | bb91c1b | 2022-04-28 15:03:05 +0800 | [diff] [blame] | 348 | get_host_status_dbus "$sled_num" |
| 349 | else |
| 350 | get_host_status_mdio "$sled_num" |
| 351 | fi |
Potin Lai | bb91c1b | 2022-04-28 15:03:05 +0800 | [diff] [blame] | 352 | return $? |
| 353 | } |
| 354 | |
Potin Lai | 27083c3 | 2022-06-02 19:13:24 +0800 | [diff] [blame^] | 355 | function do_action_ac_on() |
| 356 | { |
| 357 | local SLED_NUM=$1 |
| 358 | echo "sled${SLED_NUM}: turn on AC" |
| 359 | set_gpio "power-host${SLED_NUM}" 1 |
| 360 | echo "$ACTION_AC_ON" > "/tmp/sled${SLED_NUM}-last-action" |
| 361 | } |
| 362 | |
| 363 | function do_action_ac_off() |
| 364 | { |
| 365 | local SLED_NUM=$1 |
| 366 | echo "sled${SLED_NUM}: turn off AC" |
| 367 | set_gpio "power-host${SLED_NUM}" 0 |
| 368 | echo "$ACTION_AC_OFF" > "/tmp/sled${SLED_NUM}-last-action" |
| 369 | } |
| 370 | |
| 371 | function do_action_on() |
| 372 | { |
| 373 | local SLED_NUM=$1 |
| 374 | echo "sled${SLED_NUM}: power on host" |
| 375 | trigger_power_button "$SLED_NUM" "$DELAY_POWER_ON" |
| 376 | echo "$ACTION_ON" > "/tmp/sled${SLED_NUM}-last-action" |
| 377 | } |
| 378 | |
| 379 | function do_action_off() |
| 380 | { |
| 381 | local SLED_NUM=$1 |
| 382 | echo "sled${SLED_NUM}: power off host" |
| 383 | trigger_power_button "$SLED_NUM" "$DELAY_POWER_OFF" |
| 384 | echo "$ACTION_OFF" > "/tmp/sled${SLED_NUM}-last-action" |
| 385 | } |
| 386 | |
| 387 | function do_action_recovery() |
| 388 | { |
| 389 | local SLED_NUM=$1 |
| 390 | echo "sled${SLED_NUM}: trigger host recovery mode" |
| 391 | trigger_power_button "$SLED_NUM" "$DELAY_POWER_RECOVERY_MODE" |
| 392 | echo "$ACTION_RECOVERY" > "/tmp/sled${SLED_NUM}-last-action" |
| 393 | } |
| 394 | |
| 395 | function do_action_dfu() |
| 396 | { |
| 397 | local SLED_NUM=$1 |
| 398 | echo "sled${SLED_NUM}: trigger host dfu mode" |
| 399 | |
| 400 | # turn ac off, and hold for 25 seconds |
| 401 | do_action_ac_off "$SLED_NUM" |
| 402 | sleep 25 |
| 403 | |
| 404 | # press power button |
| 405 | echo "SLED$SLED_NUM: pressing power button" |
| 406 | if ! press_power_button "$SLED_NUM"; then |
| 407 | echo "SLED$SLED_NUM: press power button failed" |
| 408 | echo "SLED$SLED_NUM: releasing power button" |
| 409 | release_power_button "$SLED_NUM" |
| 410 | exit 1 |
| 411 | fi |
| 412 | sleep 1 |
| 413 | |
| 414 | # turn ac on |
| 415 | echo "SLED$SLED_NUM: turn ac-on" |
| 416 | do_action_ac_on "$SLED_NUM" |
| 417 | sleep 3 |
| 418 | |
| 419 | # release power button |
| 420 | echo "SLED$SLED_NUM: releasing host power button" |
| 421 | if ! release_power_button "$SLED_NUM"; then |
| 422 | echo "SLED$SLED_NUM: release power button failed" |
| 423 | exit 1 |
| 424 | fi |
| 425 | echo "$ACTION_DFU" > "/tmp/sled${SLED_NUM}-last-action" |
| 426 | } |
| 427 | |
| 428 | function host_state_on_action_handler() |
| 429 | { |
| 430 | local SLED_NUM=$1 |
| 431 | local ACTION=$2 |
| 432 | |
| 433 | case $ACTION in |
| 434 | "$ACTION_OFF") |
| 435 | do_action_off "$SLED_NUM" |
| 436 | ;; |
| 437 | "$ACTION_AC_OFF") |
| 438 | do_action_ac_off "$SLED_NUM" |
| 439 | ;; |
| 440 | *) |
| 441 | echo "Invalid action ($ACTION) for current host state (On)" |
| 442 | return 1 |
| 443 | ;; |
| 444 | esac |
| 445 | } |
| 446 | |
| 447 | function host_state_sleep_action_handler() |
| 448 | { |
| 449 | local SLED_NUM=$1 |
| 450 | local ACTION=$2 |
| 451 | |
| 452 | case $ACTION in |
| 453 | "$ACTION_ON") |
| 454 | do_action_on "$SLED_NUM" |
| 455 | ;; |
| 456 | "$ACTION_OFF") |
| 457 | do_action_off "$SLED_NUM" |
| 458 | ;; |
| 459 | "$ACTION_AC_OFF") |
| 460 | do_action_ac_off "$SLED_NUM" |
| 461 | ;; |
| 462 | *) |
| 463 | echo "Invalid action ($ACTION) for current host state (Sleep)" |
| 464 | return 1 |
| 465 | ;; |
| 466 | esac |
| 467 | } |
| 468 | |
| 469 | function host_state_off_action_handler() |
| 470 | { |
| 471 | local SLED_NUM=$1 |
| 472 | local ACTION=$2 |
| 473 | |
| 474 | case $ACTION in |
| 475 | "$ACTION_ON") |
| 476 | do_action_on "$SLED_NUM" |
| 477 | ;; |
| 478 | "$ACTION_RECOVERY") |
| 479 | do_action_recovery "$SLED_NUM" |
| 480 | ;; |
| 481 | "$ACTION_AC_OFF") |
| 482 | do_action_ac_off "$SLED_NUM" |
| 483 | ;; |
| 484 | *) |
| 485 | echo "Invalid action ($ACTION) for current host state (Off)" |
| 486 | return 1 |
| 487 | ;; |
| 488 | esac |
| 489 | } |
| 490 | |
| 491 | function host_state_ac_off_action_handler() |
| 492 | { |
| 493 | local SLED_NUM=$1 |
| 494 | local ACTION=$2 |
| 495 | |
| 496 | case $ACTION in |
| 497 | "$ACTION_AC_ON") |
| 498 | do_action_ac_on "$SLED_NUM" |
| 499 | ;; |
| 500 | "$ACTION_DFU") |
| 501 | do_action_dfu "$SLED_NUM" |
| 502 | ;; |
| 503 | "$ACTION_AC_OFF") |
| 504 | echo "sled${SLED_NUM}: already ac off" |
| 505 | return 1 |
| 506 | ;; |
| 507 | *) |
| 508 | echo "Invalid action ($ACTION) for current host state (AC Off)" |
| 509 | return 1 |
| 510 | ;; |
| 511 | esac |
| 512 | } |
| 513 | |
| 514 | function host_state_ac_on_action_handler() |
| 515 | { |
| 516 | local SLED_NUM=$1 |
| 517 | local ACTION=$2 |
| 518 | |
| 519 | case $ACTION in |
| 520 | "$ACTION_AC_OFF") |
| 521 | do_action_ac_off "$SLED_NUM" |
| 522 | ;; |
| 523 | *) |
| 524 | echo "sled${SLED_NUM}: already ac on" |
| 525 | return 1 |
| 526 | ;; |
| 527 | esac |
| 528 | } |
| 529 | |
| 530 | function host_state_recovery_action_handler() |
| 531 | { |
| 532 | local SLED_NUM=$1 |
| 533 | local ACTION=$2 |
| 534 | |
| 535 | case $ACTION in |
| 536 | "$ACTION_OFF") |
| 537 | do_action_off "$SLED_NUM" |
| 538 | ;; |
| 539 | "$ACTION_AC_OFF") |
| 540 | do_action_ac_off "$SLED_NUM" |
| 541 | ;; |
| 542 | *) |
| 543 | echo "Invalid action ($ACTION) for current host state (Recovery)" |
| 544 | return 1 |
| 545 | ;; |
| 546 | esac |
| 547 | } |
| 548 | |
| 549 | function host_state_dfu_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 | ;; |
| 558 | *) |
| 559 | echo "Invalid action ($ACTION) for current host state (DFU)" |
| 560 | return 1 |
| 561 | ;; |
| 562 | esac |
| 563 | } |
| 564 | |
Potin Lai | dc25166 | 2022-04-01 11:16:50 +0800 | [diff] [blame] | 565 | function create_dev_mem() |
| 566 | { |
| 567 | CHECK_CNT=0 |
| 568 | while true |
| 569 | do |
| 570 | CHECK_CNT=$((CHECK_CNT+1)) |
| 571 | if [ -c /dev/mem ]; then |
| 572 | # /dev/mem already exist |
| 573 | return 0 |
| 574 | elif mknod /dev/mem c 1 1; then |
| 575 | # mknod success |
| 576 | return 0 |
| 577 | elif [ "$CHECK_CNT" -ge 5 ]; then |
| 578 | break |
| 579 | fi |
| 580 | sleep 1 |
| 581 | done |
| 582 | |
| 583 | echo "create /dev/mem failed" |
| 584 | return 1 |
| 585 | } |
| 586 | |
Allen.Wang | 4a0948d | 2021-12-15 13:41:18 +0800 | [diff] [blame] | 587 | function show_usage(){ |
Potin Lai | 27083c3 | 2022-06-02 19:13:24 +0800 | [diff] [blame^] | 588 | echo "Usage: power-ctrl [sled1 | sled2 | sled3 | sled4 | sled5 | sled6] [$VALID_SLED_ACTIONS]" |
Allen.Wang | 4a0948d | 2021-12-15 13:41:18 +0800 | [diff] [blame] | 589 | echo " power-ctrl chassis-cycle" |
| 590 | } |
| 591 | |
Potin Lai | 27083c3 | 2022-06-02 19:13:24 +0800 | [diff] [blame^] | 592 | |
Allen.Wang | 4a0948d | 2021-12-15 13:41:18 +0800 | [diff] [blame] | 593 | if [ $# -eq 1 ]; then |
| 594 | if [ "$1" = "chassis-cycle" ];then |
| 595 | echo "chassis cycle...." |
| 596 | i2cset -y -f 12 0x11 0xd9 c |
| 597 | exit 0 |
| 598 | else |
| 599 | echo "Invalid argument: [ $1 ]" |
| 600 | show_usage |
| 601 | exit 1; |
| 602 | fi |
| 603 | fi |
| 604 | |
| 605 | if [ $# -gt 2 ]; then |
| 606 | echo "Too many arguments" |
| 607 | show_usage |
| 608 | exit 1; |
| 609 | fi |
| 610 | |
Allen.Wang | 5ff992e | 2022-01-04 21:00:38 +0800 | [diff] [blame] | 611 | if [[ "$1" =~ ^(sled[1-6]{1})$ ]]; then |
Allen.Wang | 4a0948d | 2021-12-15 13:41:18 +0800 | [diff] [blame] | 612 | SLED=$1 |
| 613 | ACTION=$2 |
| 614 | SLED_NUM=${SLED:4} |
| 615 | else |
| 616 | echo "invalid sled name: ${1}" |
| 617 | show_usage |
| 618 | exit 1; |
| 619 | fi |
| 620 | |
| 621 | #Check if sled is present |
| 622 | SLED_PRESENT=$(get_gpio "presence-sled${SLED_NUM}") |
| 623 | if [ "$SLED_PRESENT" != 0 ];then |
| 624 | echo "${SLED} is not present!" |
| 625 | exit 1 |
Potin Lai | 27083c3 | 2022-06-02 19:13:24 +0800 | [diff] [blame^] | 626 | elif ! is_valid_sled_action "$ACTION"; then |
| 627 | echo "Unknown action: $ACTION" |
| 628 | show_usage |
| 629 | exit 1 |
Allen.Wang | 4a0948d | 2021-12-15 13:41:18 +0800 | [diff] [blame] | 630 | fi |
| 631 | |
Potin Lai | 27083c3 | 2022-06-02 19:13:24 +0800 | [diff] [blame^] | 632 | HOST_CURR_STATUS=$(get_host_status "$SLED_NUM") |
| 633 | if [ "$ACTION" = "$ACTION_STATUS" ];then |
| 634 | echo "$HOST_CURR_STATUS" |
Allen.Wang | 4a0948d | 2021-12-15 13:41:18 +0800 | [diff] [blame] | 635 | else |
Potin Lai | 27083c3 | 2022-06-02 19:13:24 +0800 | [diff] [blame^] | 636 | case $HOST_CURR_STATUS in |
| 637 | "$HOST_AC_OFF") |
| 638 | host_state_ac_off_action_handler "$SLED_NUM" "$ACTION" |
| 639 | ;; |
| 640 | "$HOST_AC_ON") |
| 641 | host_state_ac_on_action_handler "$SLED_NUM" "$ACTION" |
| 642 | ;; |
| 643 | "$HOST_ST_OFF") |
| 644 | host_state_off_action_handler "$SLED_NUM" "$ACTION" |
| 645 | ;; |
| 646 | "$HOST_ST_ON") |
| 647 | host_state_on_action_handler "$SLED_NUM" "$ACTION" |
| 648 | ;; |
| 649 | "$HOST_ST_SLEEP") |
| 650 | host_state_sleep_action_handler "$SLED_NUM" "$ACTION" |
| 651 | ;; |
| 652 | "$HOST_ST_DFU") |
| 653 | host_state_dfu_action_handler "$SLED_NUM" "$ACTION" |
| 654 | ;; |
| 655 | "$HOST_ST_RECOVERY") |
| 656 | host_state_recovery_action_handler "$SLED_NUM" "$ACTION" |
| 657 | ;; |
| 658 | esac |
Allen.Wang | 4a0948d | 2021-12-15 13:41:18 +0800 | [diff] [blame] | 659 | fi |