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 | |
Potin Lai | 501f4c7 | 2022-06-13 13:23:57 +0800 | [diff] [blame] | 9 | # shellcheck source=meta-facebook/meta-bletchley/recipes-bletchley/plat-tools/files/bletchley-common-functions |
| 10 | source /usr/libexec/bletchley-common-functions |
| 11 | |
Allen.Wang | 4a0948d | 2021-12-15 13:41:18 +0800 | [diff] [blame] | 12 | DELAY_POWER_ON="0.5" |
Potin Lai | 8f2d3f2 | 2023-06-06 19:41:09 +0800 | [diff] [blame] | 13 | DELAY_POWER_OFF="10" |
Potin Lai | 27083c3 | 2022-06-02 19:13:24 +0800 | [diff] [blame] | 14 | DELAY_POWER_RECOVERY_MODE="10" |
Potin Lai | d2e7f20 | 2023-02-23 19:12:32 +0800 | [diff] [blame] | 15 | POWER_BTN_TIMEOUT_SEC=10 |
Allen.Wang | 4a0948d | 2021-12-15 13:41:18 +0800 | [diff] [blame] | 16 | |
Potin Lai | bb91c1b | 2022-04-28 15:03:05 +0800 | [diff] [blame] | 17 | REV_EVT="EVT" |
| 18 | REV_DVT="DVT" |
| 19 | REV_UNKNOW="UNKNOW" |
| 20 | |
| 21 | DBUS_HOST_ST_ON="xyz.openbmc_project.State.Host.HostState.Running" |
| 22 | DBUS_HOST_ST_OFF="xyz.openbmc_project.State.Host.HostState.Off" |
| 23 | |
| 24 | HOST_ST_UNKNOW="Unknow" |
| 25 | HOST_ST_ON="On" |
| 26 | HOST_ST_OFF="Off" |
Potin Lai | 27083c3 | 2022-06-02 19:13:24 +0800 | [diff] [blame] | 27 | HOST_ST_SLEEP="Sleep" |
| 28 | HOST_ST_DFU="DFU" |
| 29 | HOST_ST_RECOVERY="Recovery" |
Potin Lai | bb91c1b | 2022-04-28 15:03:05 +0800 | [diff] [blame] | 30 | HOST_AC_ON="AC On" |
| 31 | HOST_AC_OFF="AC Off" |
| 32 | |
Potin Lai | 27083c3 | 2022-06-02 19:13:24 +0800 | [diff] [blame] | 33 | ACTION_ON="on" |
| 34 | ACTION_OFF="off" |
| 35 | ACTION_DFU="dfu" |
| 36 | ACTION_RECOVERY="recovery" |
Potin Lai | 2320b0e | 2022-06-28 10:38:37 +0800 | [diff] [blame] | 37 | ACTION_CYCLE="cycle" |
| 38 | ACTION_RESET="reset" |
Potin Lai | 27083c3 | 2022-06-02 19:13:24 +0800 | [diff] [blame] | 39 | ACTION_AC_ON="ac-on" |
| 40 | ACTION_AC_OFF="ac-off" |
| 41 | ACTION_STATUS="status" |
Potin Lai | 6a0b3d3 | 2022-11-14 19:12:08 +0800 | [diff] [blame] | 42 | ACTION_BOOT_MODE="boot-from-bootmode" |
Potin Lai | 27083c3 | 2022-06-02 19:13:24 +0800 | [diff] [blame] | 43 | |
| 44 | VALID_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 Lai | 2320b0e | 2022-06-28 10:38:37 +0800 | [diff] [blame] | 52 | $ACTION_CYCLE |
| 53 | $ACTION_RESET |
Potin Lai | 6a0b3d3 | 2022-11-14 19:12:08 +0800 | [diff] [blame] | 54 | $ACTION_BOOT_MODE |
Potin Lai | 27083c3 | 2022-06-02 19:13:24 +0800 | [diff] [blame] | 55 | " |
| 56 | |
Potin Lai | 6a0b3d3 | 2022-11-14 19:12:08 +0800 | [diff] [blame] | 57 | BOOT_MODE_REGULAR="\"xyz.openbmc_project.Control.Boot.Mode.Modes.Regular\"" |
| 58 | BOOT_MODE_SAFE="\"xyz.openbmc_project.Control.Boot.Mode.Modes.Safe\"" |
| 59 | BOOT_MODE_SETUP="\"xyz.openbmc_project.Control.Boot.Mode.Modes.Setup\"" |
| 60 | |
Potin Lai | 27083c3 | 2022-06-02 19:13:24 +0800 | [diff] [blame] | 61 | function 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 | |
| 73 | function get_board_rev() |
| 74 | { |
Potin Lai | bb91c1b | 2022-04-28 15:03:05 +0800 | [diff] [blame] | 75 | 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 Lai | 27083c3 | 2022-06-02 19:13:24 +0800 | [diff] [blame] | 101 | function trigger_power_button() |
| 102 | { |
Allen.Wang | 4a0948d | 2021-12-15 13:41:18 +0800 | [diff] [blame] | 103 | local sled_num=$1 |
Allen.Wang | 6af0dff | 2021-12-28 20:23:05 +0800 | [diff] [blame] | 104 | local delay_time=$2 |
Allen.Wang | 4a0948d | 2021-12-15 13:41:18 +0800 | [diff] [blame] | 105 | |
| 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 Lai | d2e7f20 | 2023-02-23 19:12:32 +0800 | [diff] [blame] | 113 | wait_gpio_falling "${GPIO_DETECT_PIN0}" "$POWER_BTN_TIMEOUT_SEC" |
Allen.Wang | 4a0948d | 2021-12-15 13:41:18 +0800 | [diff] [blame] | 114 | 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.Wang | 6af0dff | 2021-12-28 20:23:05 +0800 | [diff] [blame] | 121 | echo "Power key switch not trigger, back motor to initial position" |
Allen.Wang | 4a0948d | 2021-12-15 13:41:18 +0800 | [diff] [blame] | 122 | fi |
| 123 | |
| 124 | motor-ctrl "sled${sled_num}" r >/dev/null |
Potin Lai | d2e7f20 | 2023-02-23 19:12:32 +0800 | [diff] [blame] | 125 | wait_gpio_falling "${GPIO_DETECT_PIN1}" "$POWER_BTN_TIMEOUT_SEC" |
Allen.Wang | 4a0948d | 2021-12-15 13:41:18 +0800 | [diff] [blame] | 126 | 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.Wang | 6af0dff | 2021-12-28 20:23:05 +0800 | [diff] [blame] | 130 | echo "Initial position switch not trigger, force stop motor" |
Allen.Wang | 4a0948d | 2021-12-15 13:41:18 +0800 | [diff] [blame] | 131 | fi |
| 132 | } |
| 133 | |
Potin Lai | 27083c3 | 2022-06-02 19:13:24 +0800 | [diff] [blame] | 134 | function release_power_button() |
| 135 | { |
Potin Lai | db5648e | 2022-02-16 00:57:55 +0800 | [diff] [blame] | 136 | 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 Lai | d2e7f20 | 2023-02-23 19:12:32 +0800 | [diff] [blame] | 145 | wait_gpio_falling "${GPIO_DETECT_PIN1}" "$POWER_BTN_TIMEOUT_SEC" |
Potin Lai | db5648e | 2022-02-16 00:57:55 +0800 | [diff] [blame] | 146 | 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 Lai | 27083c3 | 2022-06-02 19:13:24 +0800 | [diff] [blame] | 156 | function press_power_button() |
| 157 | { |
Potin Lai | db5648e | 2022-02-16 00:57:55 +0800 | [diff] [blame] | 158 | 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 Lai | d2e7f20 | 2023-02-23 19:12:32 +0800 | [diff] [blame] | 164 | wait_gpio_falling "${GPIO_DETECT_PIN0}" "$POWER_BTN_TIMEOUT_SEC" |
Potin Lai | db5648e | 2022-02-16 00:57:55 +0800 | [diff] [blame] | 165 | 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.Wang | 4a0948d | 2021-12-15 13:41:18 +0800 | [diff] [blame] | 176 | #Get i2c bus number for sledN |
Potin Lai | 27083c3 | 2022-06-02 19:13:24 +0800 | [diff] [blame] | 177 | function get_bus_num() |
| 178 | { |
Allen.Wang | 4a0948d | 2021-12-15 13:41:18 +0800 | [diff] [blame] | 179 | SLED_NUM=$1 |
| 180 | local bus=0 |
Allen.Wang | 5ff992e | 2022-01-04 21:00:38 +0800 | [diff] [blame] | 181 | #Mapping Sled number 1~6 to i2c bus number 0~5 |
| 182 | if [[ "$SLED_NUM" = [1-6] ]]; then |
| 183 | bus=$(( SLED_NUM - 1 )) |
Allen.Wang | 4a0948d | 2021-12-15 13:41:18 +0800 | [diff] [blame] | 184 | fi |
| 185 | echo "$bus" |
| 186 | } |
| 187 | |
Potin Lai | 27083c3 | 2022-06-02 19:13:24 +0800 | [diff] [blame] | 188 | function get_ac_status() |
| 189 | { |
Allen.Wang | 4a0948d | 2021-12-15 13:41:18 +0800 | [diff] [blame] | 190 | 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 Lai | bb91c1b | 2022-04-28 15:03:05 +0800 | [diff] [blame] | 197 | echo "$HOST_AC_ON" |
Allen.Wang | 4a0948d | 2021-12-15 13:41:18 +0800 | [diff] [blame] | 198 | else |
Potin Lai | bb91c1b | 2022-04-28 15:03:05 +0800 | [diff] [blame] | 199 | echo "$HOST_AC_OFF" |
Allen.Wang | 4a0948d | 2021-12-15 13:41:18 +0800 | [diff] [blame] | 200 | fi |
| 201 | } |
| 202 | |
Potin Lai | bb91c1b | 2022-04-28 15:03:05 +0800 | [diff] [blame] | 203 | function 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 Lai | b8f52ae | 2022-09-19 14:20:58 +0800 | [diff] [blame] | 226 | function 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 Lai | bb91c1b | 2022-04-28 15:03:05 +0800 | [diff] [blame] | 233 | function get_host_status_mdio() |
Potin Lai | dc25166 | 2022-04-01 11:16:50 +0800 | [diff] [blame] | 234 | { |
Potin Lai | 27083c3 | 2022-06-02 19:13:24 +0800 | [diff] [blame] | 235 | local SLED_NUM=$1 |
Potin Lai | b8f52ae | 2022-09-19 14:20:58 +0800 | [diff] [blame] | 236 | local MDIO_BUS |
| 237 | |
| 238 | MDIO_BUS="$(get_addr_from_dts_aliases mdio0).mdio-1" |
Potin Lai | bb91c1b | 2022-04-28 15:03:05 +0800 | [diff] [blame] | 239 | |
Potin Lai | dc25166 | 2022-04-01 11:16:50 +0800 | [diff] [blame] | 240 | 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 Lai | 27083c3 | 2022-06-02 19:13:24 +0800 | [diff] [blame] | 247 | 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 Lai | dc25166 | 2022-04-01 11:16:50 +0800 | [diff] [blame] | 255 | |
| 256 | while true |
| 257 | do |
Potin Lai | b8f52ae | 2022-09-19 14:20:58 +0800 | [diff] [blame] | 258 | if POST_ST_VAL=$(mdio "$MDIO_BUS" phy "${PORT_MAP[SLED_NUM]}" 0); then |
Potin Lai | 27083c3 | 2022-06-02 19:13:24 +0800 | [diff] [blame] | 259 | 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 Lai | dc25166 | 2022-04-01 11:16:50 +0800 | [diff] [blame] | 269 | TMP_HOST_ST="$HOST_ST_ON" |
Potin Lai | 27083c3 | 2022-06-02 19:13:24 +0800 | [diff] [blame] | 270 | 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 Lai | dc25166 | 2022-04-01 11:16:50 +0800 | [diff] [blame] | 280 | else |
Potin Lai | 27083c3 | 2022-06-02 19:13:24 +0800 | [diff] [blame] | 281 | TMP_HOST_ST="$HOST_ST_UNKNOW" |
Potin Lai | dc25166 | 2022-04-01 11:16:50 +0800 | [diff] [blame] | 282 | 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 Lai | bb91c1b | 2022-04-28 15:03:05 +0800 | [diff] [blame] | 307 | function get_host_status() |
| 308 | { |
| 309 | local sled_num=$1 |
Potin Lai | bb91c1b | 2022-04-28 15:03:05 +0800 | [diff] [blame] | 310 | |
Potin Lai | 27083c3 | 2022-06-02 19:13:24 +0800 | [diff] [blame] | 311 | if [ "$(get_ac_status "$SLED_NUM")" == "$HOST_AC_OFF" ];then |
| 312 | echo "$HOST_AC_OFF" |
| 313 | return 0 |
| 314 | fi |
Potin Lai | bb91c1b | 2022-04-28 15:03:05 +0800 | [diff] [blame] | 315 | |
Potin Lai | 27083c3 | 2022-06-02 19:13:24 +0800 | [diff] [blame] | 316 | if [ "$(get_board_rev)" = "$REV_EVT" ]; then |
Potin Lai | bb91c1b | 2022-04-28 15:03:05 +0800 | [diff] [blame] | 317 | get_host_status_dbus "$sled_num" |
| 318 | else |
| 319 | get_host_status_mdio "$sled_num" |
| 320 | fi |
Potin Lai | bb91c1b | 2022-04-28 15:03:05 +0800 | [diff] [blame] | 321 | return $? |
| 322 | } |
| 323 | |
Potin Lai | 6a0b3d3 | 2022-11-14 19:12:08 +0800 | [diff] [blame] | 324 | function 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 Lai | 2320b0e | 2022-06-28 10:38:37 +0800 | [diff] [blame] | 332 | function 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 | |
| 350 | function 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 Lai | 27083c3 | 2022-06-02 19:13:24 +0800 | [diff] [blame] | 365 | function 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 | |
| 373 | function 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 | |
| 381 | function 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 Lai | aafe187 | 2022-08-19 06:56:49 +0000 | [diff] [blame] | 386 | sleep 10 # Mac mini need about 10 second to stable link status |
Potin Lai | 27083c3 | 2022-06-02 19:13:24 +0800 | [diff] [blame] | 387 | echo "$ACTION_ON" > "/tmp/sled${SLED_NUM}-last-action" |
| 388 | } |
| 389 | |
| 390 | function 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 | |
| 398 | function 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 | |
| 406 | function 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 Lai | 6a0b3d3 | 2022-11-14 19:12:08 +0800 | [diff] [blame] | 421 | return 1 |
Potin Lai | 27083c3 | 2022-06-02 19:13:24 +0800 | [diff] [blame] | 422 | 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 Lai | 6a0b3d3 | 2022-11-14 19:12:08 +0800 | [diff] [blame] | 434 | return 1 |
Potin Lai | 27083c3 | 2022-06-02 19:13:24 +0800 | [diff] [blame] | 435 | fi |
| 436 | echo "$ACTION_DFU" > "/tmp/sled${SLED_NUM}-last-action" |
| 437 | } |
| 438 | |
| 439 | function 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 Lai | 2320b0e | 2022-06-28 10:38:37 +0800 | [diff] [blame] | 451 | "$ACTION_RESET") |
| 452 | do_action_reset "$SLED_NUM" "$HOST_ST_ON" |
| 453 | ;; |
| 454 | "$ACTION_CYCLE") |
| 455 | do_action_cycle "$SLED_NUM" |
| 456 | ;; |
Potin Lai | 27083c3 | 2022-06-02 19:13:24 +0800 | [diff] [blame] | 457 | *) |
| 458 | echo "Invalid action ($ACTION) for current host state (On)" |
| 459 | return 1 |
| 460 | ;; |
| 461 | esac |
| 462 | } |
| 463 | |
| 464 | function 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 Lai | 2320b0e | 2022-06-28 10:38:37 +0800 | [diff] [blame] | 479 | "$ACTION_RESET") |
| 480 | do_action_reset "$SLED_NUM" "$HOST_ST_ON" |
| 481 | ;; |
| 482 | "$ACTION_CYCLE") |
| 483 | do_action_cycle "$SLED_NUM" |
| 484 | ;; |
Potin Lai | 27083c3 | 2022-06-02 19:13:24 +0800 | [diff] [blame] | 485 | *) |
| 486 | echo "Invalid action ($ACTION) for current host state (Sleep)" |
| 487 | return 1 |
| 488 | ;; |
| 489 | esac |
| 490 | } |
| 491 | |
| 492 | function 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 Lai | 6a0b3d3 | 2022-11-14 19:12:08 +0800 | [diff] [blame] | 504 | "$ACTION_DFU") |
| 505 | do_action_dfu "$SLED_NUM" |
| 506 | ;; |
Potin Lai | 27083c3 | 2022-06-02 19:13:24 +0800 | [diff] [blame] | 507 | "$ACTION_AC_OFF") |
| 508 | do_action_ac_off "$SLED_NUM" |
| 509 | ;; |
Potin Lai | 2320b0e | 2022-06-28 10:38:37 +0800 | [diff] [blame] | 510 | "$ACTION_RESET") |
| 511 | do_action_reset "$SLED_NUM" "$HOST_ST_ON" |
| 512 | ;; |
| 513 | "$ACTION_CYCLE") |
| 514 | do_action_cycle "$SLED_NUM" |
| 515 | ;; |
Potin Lai | 27083c3 | 2022-06-02 19:13:24 +0800 | [diff] [blame] | 516 | *) |
| 517 | echo "Invalid action ($ACTION) for current host state (Off)" |
| 518 | return 1 |
| 519 | ;; |
| 520 | esac |
| 521 | } |
| 522 | |
| 523 | function 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 Lai | 2320b0e | 2022-06-28 10:38:37 +0800 | [diff] [blame] | 539 | "$ACTION_CYCLE") |
| 540 | do_action_reset "$SLED_NUM" |
| 541 | ;; |
Potin Lai | 27083c3 | 2022-06-02 19:13:24 +0800 | [diff] [blame] | 542 | *) |
| 543 | echo "Invalid action ($ACTION) for current host state (AC Off)" |
| 544 | return 1 |
| 545 | ;; |
| 546 | esac |
| 547 | } |
| 548 | |
| 549 | function 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 Lai | 6a0b3d3 | 2022-11-14 19:12:08 +0800 | [diff] [blame] | 558 | "$ACTION_DFU") |
| 559 | do_action_dfu "$SLED_NUM" |
| 560 | ;; |
Potin Lai | 2320b0e | 2022-06-28 10:38:37 +0800 | [diff] [blame] | 561 | "$ACTION_CYCLE") |
| 562 | do_action_cycle "$SLED_NUM" |
| 563 | ;; |
Potin Lai | 27083c3 | 2022-06-02 19:13:24 +0800 | [diff] [blame] | 564 | *) |
| 565 | echo "sled${SLED_NUM}: already ac on" |
| 566 | return 1 |
| 567 | ;; |
| 568 | esac |
| 569 | } |
| 570 | |
| 571 | function 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 Lai | 2320b0e | 2022-06-28 10:38:37 +0800 | [diff] [blame] | 583 | "$ACTION_RESET") |
| 584 | do_action_reset "$SLED_NUM" "$HOST_ST_ON" |
| 585 | ;; |
| 586 | "$ACTION_CYCLE") |
| 587 | do_action_cycle "$SLED_NUM" |
| 588 | ;; |
Potin Lai | 27083c3 | 2022-06-02 19:13:24 +0800 | [diff] [blame] | 589 | *) |
| 590 | echo "Invalid action ($ACTION) for current host state (Recovery)" |
| 591 | return 1 |
| 592 | ;; |
| 593 | esac |
| 594 | } |
| 595 | |
| 596 | function 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 Lai | 2320b0e | 2022-06-28 10:38:37 +0800 | [diff] [blame] | 605 | "$ACTION_CYCLE") |
| 606 | do_action_cycle "$SLED_NUM" |
| 607 | ;; |
Potin Lai | 27083c3 | 2022-06-02 19:13:24 +0800 | [diff] [blame] | 608 | *) |
| 609 | echo "Invalid action ($ACTION) for current host state (DFU)" |
| 610 | return 1 |
| 611 | ;; |
| 612 | esac |
| 613 | } |
| 614 | |
Potin Lai | dc25166 | 2022-04-01 11:16:50 +0800 | [diff] [blame] | 615 | function 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.Wang | 4a0948d | 2021-12-15 13:41:18 +0800 | [diff] [blame] | 637 | function show_usage(){ |
Potin Lai | 27083c3 | 2022-06-02 19:13:24 +0800 | [diff] [blame] | 638 | 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] | 639 | echo " power-ctrl chassis-cycle" |
| 640 | } |
| 641 | |
Potin Lai | 27083c3 | 2022-06-02 19:13:24 +0800 | [diff] [blame] | 642 | |
Allen.Wang | 4a0948d | 2021-12-15 13:41:18 +0800 | [diff] [blame] | 643 | if [ $# -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 |
| 653 | fi |
| 654 | |
| 655 | if [ $# -gt 2 ]; then |
| 656 | echo "Too many arguments" |
| 657 | show_usage |
| 658 | exit 1; |
| 659 | fi |
| 660 | |
Allen.Wang | 5ff992e | 2022-01-04 21:00:38 +0800 | [diff] [blame] | 661 | if [[ "$1" =~ ^(sled[1-6]{1})$ ]]; then |
Allen.Wang | 4a0948d | 2021-12-15 13:41:18 +0800 | [diff] [blame] | 662 | SLED=$1 |
| 663 | ACTION=$2 |
| 664 | SLED_NUM=${SLED:4} |
| 665 | else |
| 666 | echo "invalid sled name: ${1}" |
| 667 | show_usage |
| 668 | exit 1; |
| 669 | fi |
| 670 | |
| 671 | #Check if sled is present |
Potin Lai | 501f4c7 | 2022-06-13 13:23:57 +0800 | [diff] [blame] | 672 | if ! is_sled_present "${SLED_NUM}"; then |
Allen.Wang | 4a0948d | 2021-12-15 13:41:18 +0800 | [diff] [blame] | 673 | echo "${SLED} is not present!" |
| 674 | exit 1 |
Potin Lai | 27083c3 | 2022-06-02 19:13:24 +0800 | [diff] [blame] | 675 | elif ! is_valid_sled_action "$ACTION"; then |
| 676 | echo "Unknown action: $ACTION" |
| 677 | show_usage |
| 678 | exit 1 |
Allen.Wang | 4a0948d | 2021-12-15 13:41:18 +0800 | [diff] [blame] | 679 | fi |
| 680 | |
Potin Lai | a8d258f | 2022-06-14 18:35:10 +0800 | [diff] [blame] | 681 | if [ "$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 |
| 685 | elif [ "$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 |
| 689 | elif [ "$ACTION" = "$ACTION_STATUS" ];then |
| 690 | HOST_CURR_STATUS=$(get_host_status "$SLED_NUM") |
Potin Lai | 27083c3 | 2022-06-02 19:13:24 +0800 | [diff] [blame] | 691 | echo "$HOST_CURR_STATUS" |
Allen.Wang | 4a0948d | 2021-12-15 13:41:18 +0800 | [diff] [blame] | 692 | else |
Potin Lai | a8d258f | 2022-06-14 18:35:10 +0800 | [diff] [blame] | 693 | HOST_CURR_STATUS=$(get_host_status "$SLED_NUM") |
Potin Lai | 6a0b3d3 | 2022-11-14 19:12:08 +0800 | [diff] [blame] | 694 | |
| 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 Lai | 27083c3 | 2022-06-02 19:13:24 +0800 | [diff] [blame] | 716 | 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.Wang | 4a0948d | 2021-12-15 13:41:18 +0800 | [diff] [blame] | 739 | fi |