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" |
| 11 | POWER_BTN_TIMEOUT_CNT=60 |
| 12 | |
| 13 | #Switch pull low while it be touched |
| 14 | function wait_for_switch() { |
| 15 | TARGET_PIN=$1 |
| 16 | TARGET_SWITCH=1 |
| 17 | TIME_CNT=0 |
| 18 | while [ "$TARGET_SWITCH" -eq 1 ] ;do |
| 19 | TARGET_SWITCH=$(get_gpio "$TARGET_PIN") |
| 20 | sleep 0.1 |
| 21 | TIME_CNT=$(( TIME_CNT +1)) |
| 22 | if [ $TIME_CNT -gt $POWER_BTN_TIMEOUT_CNT ];then |
| 23 | echo "Error: Too long to get target switch, force exit" >&2 |
| 24 | break |
| 25 | fi |
| 26 | done |
| 27 | } |
| 28 | |
| 29 | function trigger_power_button() { |
| 30 | local sled_num=$1 |
Allen.Wang | 6af0dff | 2021-12-28 20:23:05 +0800 | [diff] [blame] | 31 | local delay_time=$2 |
Allen.Wang | 4a0948d | 2021-12-15 13:41:18 +0800 | [diff] [blame] | 32 | |
| 33 | #SLED{N}_MS_DETECT1 (initial position) |
| 34 | GPIO_DETECT_PIN1="SLED${sled_num}_MS_DETECT1" |
| 35 | #SLED{N}_MS_DETECT0 (MAC position) |
| 36 | GPIO_DETECT_PIN0="SLED${sled_num}_MS_DETECT0" |
| 37 | |
| 38 | echo "Motor go forward to press Power key" |
| 39 | motor-ctrl "sled${sled_num}" f >/dev/null |
| 40 | wait_for_switch "${GPIO_DETECT_PIN0}" |
| 41 | motor-ctrl "sled${sled_num}" s >/dev/null |
| 42 | |
| 43 | if [ "$(get_gpio "$GPIO_DETECT_PIN0")" -eq 0 ];then |
| 44 | echo "Power key switch triggered" |
| 45 | echo "Press power key for Sled${1} ${delay_time} seconds..." |
| 46 | sleep "$delay_time" |
| 47 | else |
Allen.Wang | 6af0dff | 2021-12-28 20:23:05 +0800 | [diff] [blame] | 48 | echo "Power key switch not trigger, back motor to initial position" |
Allen.Wang | 4a0948d | 2021-12-15 13:41:18 +0800 | [diff] [blame] | 49 | fi |
| 50 | |
| 51 | motor-ctrl "sled${sled_num}" r >/dev/null |
| 52 | wait_for_switch "${GPIO_DETECT_PIN1}" |
| 53 | motor-ctrl "sled${sled_num}" s >/dev/null |
| 54 | if [ "$(get_gpio "$GPIO_DETECT_PIN1")" -eq 0 ];then |
| 55 | echo "Motor reverse to initial position successful" |
| 56 | else |
Allen.Wang | 6af0dff | 2021-12-28 20:23:05 +0800 | [diff] [blame] | 57 | echo "Initial position switch not trigger, force stop motor" |
Allen.Wang | 4a0948d | 2021-12-15 13:41:18 +0800 | [diff] [blame] | 58 | fi |
| 59 | } |
| 60 | |
Potin Lai | db5648e | 2022-02-16 00:57:55 +0800 | [diff] [blame] | 61 | function release_power_button() { |
| 62 | local sled_num=$1 |
| 63 | GPIO_DETECT_PIN1="SLED${sled_num}_MS_DETECT1" |
| 64 | |
| 65 | if [ "$(get_gpio "$GPIO_DETECT_PIN1")" -eq 0 ]; then |
| 66 | echo "Motor at initial position already" |
| 67 | return 0 |
| 68 | fi |
| 69 | |
| 70 | motor-ctrl "sled${sled_num}" r >/dev/null |
| 71 | wait_for_switch "${GPIO_DETECT_PIN1}" |
| 72 | motor-ctrl "sled${sled_num}" s >/dev/null |
| 73 | if [ "$(get_gpio "$GPIO_DETECT_PIN1")" -eq 0 ];then |
| 74 | echo "Motor reverse to initial position successful" |
| 75 | return 0 |
| 76 | fi |
| 77 | |
| 78 | echo "Error: Initial position switch not trigger" |
| 79 | return 1 |
| 80 | } |
| 81 | |
| 82 | function press_power_button() { |
| 83 | local sled_num=$1 |
| 84 | |
| 85 | GPIO_DETECT_PIN0="SLED${sled_num}_MS_DETECT0" |
| 86 | |
| 87 | echo "Motor go forward to press Power button" |
| 88 | motor-ctrl "sled${sled_num}" f >/dev/null |
| 89 | wait_for_switch "${GPIO_DETECT_PIN0}" |
| 90 | motor-ctrl "sled${sled_num}" s >/dev/null |
| 91 | |
| 92 | if [ "$(get_gpio "$GPIO_DETECT_PIN0")" -eq 0 ];then |
| 93 | echo "Power button switch triggered" |
| 94 | return 0 |
| 95 | fi |
| 96 | |
| 97 | echo "Error: Power button switch not trigger" |
| 98 | return 1 |
| 99 | } |
| 100 | |
Allen.Wang | 4a0948d | 2021-12-15 13:41:18 +0800 | [diff] [blame] | 101 | #Get i2c bus number for sledN |
| 102 | function get_bus_num() { |
| 103 | SLED_NUM=$1 |
| 104 | local bus=0 |
Allen.Wang | 5ff992e | 2022-01-04 21:00:38 +0800 | [diff] [blame] | 105 | #Mapping Sled number 1~6 to i2c bus number 0~5 |
| 106 | if [[ "$SLED_NUM" = [1-6] ]]; then |
| 107 | bus=$(( SLED_NUM - 1 )) |
Allen.Wang | 4a0948d | 2021-12-15 13:41:18 +0800 | [diff] [blame] | 108 | fi |
| 109 | echo "$bus" |
| 110 | } |
| 111 | |
| 112 | function set_gpio() |
| 113 | { |
| 114 | NET_NAME=$1 |
| 115 | OUT_VAL=$2 |
| 116 | mapfile -t -d " " GPIO_INFO < <(gpiofind "$NET_NAME") |
| 117 | if [ "${#GPIO_INFO[@]}" -ne 2 ]; then |
| 118 | echo "set_gpio: can not find gpio, $NET_NAME" |
| 119 | return 1 |
| 120 | fi |
| 121 | echo -n "set_gpio: set $NET_NAME = $OUT_VAL" |
| 122 | if ! gpioset "${GPIO_INFO[0]}" "${GPIO_INFO[1]%$'\n'}"="$OUT_VAL"; then |
| 123 | echo " failed" |
| 124 | return 1 |
| 125 | fi |
| 126 | echo " success" |
| 127 | return 0 |
| 128 | } |
| 129 | |
| 130 | function get_gpio() |
| 131 | { |
| 132 | NET_NAME=$1 |
| 133 | RET_VAL=2 |
| 134 | |
| 135 | mapfile -t -d " " GPIO_INFO < <(gpiofind "$NET_NAME") |
| 136 | if [ "${#GPIO_INFO[@]}" -ne 2 ]; then |
| 137 | echo "get_gpio: can not find gpio, $NET_NAME" >&2 |
| 138 | return 1 |
| 139 | fi |
| 140 | if ! RET_VAL=$(gpioget "${GPIO_INFO[0]}" "${GPIO_INFO[1]%$'\n'}") ; then |
| 141 | echo "get_gpio: get ${NET_NAME} failed" >&2 |
| 142 | return 1 |
| 143 | fi |
| 144 | echo "${RET_VAL}" |
| 145 | return 0 |
| 146 | } |
| 147 | |
| 148 | function get_ac_status(){ |
| 149 | i2c_bus=$(get_bus_num "$1") |
| 150 | p1_output_reg=$(i2cget -f -y "$i2c_bus" 0x76 0x03) |
| 151 | p1_config_reg=$(i2cget -f -y "$i2c_bus" 0x76 0x07) |
| 152 | host_pwr="$(( (p1_output_reg & 0x80)>>7 ))" |
| 153 | is_output="$(( (~p1_config_reg & 0x80)>>7 ))" |
| 154 | |
| 155 | if [ "$(( host_pwr & is_output ))" -eq 1 ];then |
| 156 | echo "AC on" |
| 157 | else |
| 158 | echo "AC off" |
| 159 | fi |
| 160 | } |
| 161 | |
Potin Lai | dc25166 | 2022-04-01 11:16:50 +0800 | [diff] [blame^] | 162 | function get_host_status() |
| 163 | { |
| 164 | SLED_NUM=$1 |
| 165 | MDIO_BUS=0 |
| 166 | HOST_ST_UNKNOW="Unknow" |
| 167 | HOST_ST_ON="On" |
| 168 | HOST_ST_OFF="Off" |
| 169 | declare -a PORT_MAP=(0 3 2 1 7 6 5) |
| 170 | |
| 171 | # check /dev/mem |
| 172 | if ! create_dev_mem; then |
| 173 | return 1 |
| 174 | fi |
| 175 | |
| 176 | CHECK_CNT=0 |
| 177 | MDIO_ERR_CNT=0 |
| 178 | CUR_HOST_ST=$HOST_ST_UNKNOW |
| 179 | |
| 180 | while true |
| 181 | do |
| 182 | if POST_ST_VAL=$(mdio-util c22 r $MDIO_BUS "${PORT_MAP[SLED_NUM]}" 0); then |
| 183 | if [ $((POST_ST_VAL&16#0800)) -eq $((16#0800)) ]; then |
| 184 | TMP_HOST_ST="$HOST_ST_ON" |
| 185 | else |
| 186 | TMP_HOST_ST="$HOST_ST_OFF" |
| 187 | fi |
| 188 | |
| 189 | if [ "$CUR_HOST_ST" == "$TMP_HOST_ST" ]; then |
| 190 | CHECK_CNT=$((CHECK_CNT+1)) |
| 191 | else |
| 192 | CUR_HOST_ST=$TMP_HOST_ST |
| 193 | CHECK_CNT=0 |
| 194 | fi |
| 195 | |
| 196 | if [ "$CHECK_CNT" -ge 5 ]; then |
| 197 | echo "$CUR_HOST_ST" |
| 198 | break |
| 199 | fi |
| 200 | else |
| 201 | MDIO_ERR_CNT=$((MDIO_ERR_CNT+1)) |
| 202 | if [ "$MDIO_ERR_CNT" -ge 5 ]; then |
| 203 | echo "$HOST_ST_UNKNOW" |
| 204 | return 1 |
| 205 | fi |
| 206 | fi |
| 207 | done |
| 208 | |
| 209 | return 0 |
| 210 | } |
| 211 | |
| 212 | function create_dev_mem() |
| 213 | { |
| 214 | CHECK_CNT=0 |
| 215 | while true |
| 216 | do |
| 217 | CHECK_CNT=$((CHECK_CNT+1)) |
| 218 | if [ -c /dev/mem ]; then |
| 219 | # /dev/mem already exist |
| 220 | return 0 |
| 221 | elif mknod /dev/mem c 1 1; then |
| 222 | # mknod success |
| 223 | return 0 |
| 224 | elif [ "$CHECK_CNT" -ge 5 ]; then |
| 225 | break |
| 226 | fi |
| 227 | sleep 1 |
| 228 | done |
| 229 | |
| 230 | echo "create /dev/mem failed" |
| 231 | return 1 |
| 232 | } |
| 233 | |
Allen.Wang | 4a0948d | 2021-12-15 13:41:18 +0800 | [diff] [blame] | 234 | function show_usage(){ |
Allen.Wang | 5ff992e | 2022-01-04 21:00:38 +0800 | [diff] [blame] | 235 | echo "Usage: power-ctrl [sled1 | sled2 | sled3 | sled4 | sled5 | sled6] [on off ac-on ac-off status]" |
Allen.Wang | 4a0948d | 2021-12-15 13:41:18 +0800 | [diff] [blame] | 236 | echo " power-ctrl chassis-cycle" |
| 237 | } |
| 238 | |
| 239 | if [ $# -eq 1 ]; then |
| 240 | if [ "$1" = "chassis-cycle" ];then |
| 241 | echo "chassis cycle...." |
| 242 | i2cset -y -f 12 0x11 0xd9 c |
| 243 | exit 0 |
| 244 | else |
| 245 | echo "Invalid argument: [ $1 ]" |
| 246 | show_usage |
| 247 | exit 1; |
| 248 | fi |
| 249 | fi |
| 250 | |
| 251 | if [ $# -gt 2 ]; then |
| 252 | echo "Too many arguments" |
| 253 | show_usage |
| 254 | exit 1; |
| 255 | fi |
| 256 | |
Allen.Wang | 5ff992e | 2022-01-04 21:00:38 +0800 | [diff] [blame] | 257 | if [[ "$1" =~ ^(sled[1-6]{1})$ ]]; then |
Allen.Wang | 4a0948d | 2021-12-15 13:41:18 +0800 | [diff] [blame] | 258 | SLED=$1 |
| 259 | ACTION=$2 |
| 260 | SLED_NUM=${SLED:4} |
| 261 | else |
| 262 | echo "invalid sled name: ${1}" |
| 263 | show_usage |
| 264 | exit 1; |
| 265 | fi |
| 266 | |
| 267 | #Check if sled is present |
| 268 | SLED_PRESENT=$(get_gpio "presence-sled${SLED_NUM}") |
| 269 | if [ "$SLED_PRESENT" != 0 ];then |
| 270 | echo "${SLED} is not present!" |
| 271 | exit 1 |
| 272 | fi |
| 273 | |
| 274 | if [[ "$ACTION" == "on" ]]; then |
Allen.Wang | 787c9c6 | 2022-03-02 14:55:03 +0800 | [diff] [blame] | 275 | if [ "$(get_ac_status "$SLED_NUM")" == "AC on" ];then |
Potin Lai | dc25166 | 2022-04-01 11:16:50 +0800 | [diff] [blame^] | 276 | host_power_st=$(get_host_status "$SLED_NUM") |
| 277 | if [ "$host_power_st" == "On" ]; then |
| 278 | echo "${SLED} is already On" |
| 279 | elif [ "$host_power_st" == "Off" ]; then |
| 280 | echo "Power on ${SLED}" |
| 281 | trigger_power_button "$SLED_NUM" "$DELAY_POWER_ON" |
| 282 | else |
| 283 | echo "${SLED} power state is $host_power_st" |
| 284 | exit 1 |
| 285 | fi |
Allen.Wang | 787c9c6 | 2022-03-02 14:55:03 +0800 | [diff] [blame] | 286 | else |
| 287 | echo "${SLED} is ac-off, please turn on ac before power on" |
| 288 | exit 1 |
| 289 | fi |
Allen.Wang | 4a0948d | 2021-12-15 13:41:18 +0800 | [diff] [blame] | 290 | elif [[ "$ACTION" == "off" ]];then |
Allen.Wang | 787c9c6 | 2022-03-02 14:55:03 +0800 | [diff] [blame] | 291 | if [ "$(get_ac_status "$SLED_NUM")" == "AC on" ];then |
Potin Lai | dc25166 | 2022-04-01 11:16:50 +0800 | [diff] [blame^] | 292 | host_power_st=$(get_host_status "$SLED_NUM") |
| 293 | if [ "$host_power_st" == "On" ]; then |
| 294 | echo "Power off ${SLED}" |
| 295 | trigger_power_button "$SLED_NUM" "$DELAY_POWER_OFF" |
| 296 | elif [ "$host_power_st" == "Off" ]; then |
| 297 | echo "${SLED} is already Off" |
| 298 | else |
| 299 | echo "${SLED} power state is $host_power_st" |
| 300 | exit 1 |
| 301 | fi |
Allen.Wang | 787c9c6 | 2022-03-02 14:55:03 +0800 | [diff] [blame] | 302 | else |
| 303 | echo "${SLED} is already ac-off" |
| 304 | exit 1 |
| 305 | fi |
Allen.Wang | 4a0948d | 2021-12-15 13:41:18 +0800 | [diff] [blame] | 306 | elif [[ "$ACTION" == "status" ]];then |
Potin Lai | dc25166 | 2022-04-01 11:16:50 +0800 | [diff] [blame^] | 307 | AC_ST=$(get_ac_status "$SLED_NUM") |
| 308 | if [ "$AC_ST" == "AC on" ]; then |
| 309 | # check host power status if AC is on |
| 310 | get_host_status "$SLED_NUM" || exit 1 |
| 311 | else |
| 312 | # AC off |
| 313 | echo "$AC_ST" |
| 314 | fi |
Allen.Wang | 4a0948d | 2021-12-15 13:41:18 +0800 | [diff] [blame] | 315 | elif [[ "$ACTION" == "ac-on" ]];then |
| 316 | set_gpio "power-host${SLED_NUM}" 1 |
| 317 | elif [[ "$ACTION" == "ac-off" ]];then |
| 318 | set_gpio "power-host${SLED_NUM}" 0 |
Potin Lai | db5648e | 2022-02-16 00:57:55 +0800 | [diff] [blame] | 319 | elif [[ "$ACTION" == "dfu" ]]; then |
| 320 | echo "Set host$SLED_NUM DFU mode" |
| 321 | |
Allen.Wang | 787c9c6 | 2022-03-02 14:55:03 +0800 | [diff] [blame] | 322 | # turn ac off, and hold for 25 seconds |
Potin Lai | db5648e | 2022-02-16 00:57:55 +0800 | [diff] [blame] | 323 | echo "SLED$SLED_NUM: turn ac-off" |
| 324 | set_gpio "power-host${SLED_NUM}" 0 |
Allen.Wang | 787c9c6 | 2022-03-02 14:55:03 +0800 | [diff] [blame] | 325 | sleep 25 |
Potin Lai | db5648e | 2022-02-16 00:57:55 +0800 | [diff] [blame] | 326 | |
Allen.Wang | 787c9c6 | 2022-03-02 14:55:03 +0800 | [diff] [blame] | 327 | # press power button |
Potin Lai | db5648e | 2022-02-16 00:57:55 +0800 | [diff] [blame] | 328 | echo "SLED$SLED_NUM: pressing power button" |
| 329 | if ! press_power_button "$SLED_NUM"; then |
| 330 | echo "SLED$SLED_NUM: press power button failed" |
| 331 | echo "SLED$SLED_NUM: releasing power button" |
| 332 | release_power_button "$SLED_NUM" |
| 333 | exit 1 |
| 334 | fi |
Allen.Wang | 787c9c6 | 2022-03-02 14:55:03 +0800 | [diff] [blame] | 335 | sleep 1 |
Potin Lai | db5648e | 2022-02-16 00:57:55 +0800 | [diff] [blame] | 336 | |
| 337 | # turn ac on |
| 338 | echo "SLED$SLED_NUM: turn ac-on" |
| 339 | set_gpio "power-host${SLED_NUM}" 1 |
| 340 | sleep 3 |
| 341 | |
| 342 | # release power button |
| 343 | echo "SLED$SLED_NUM: releasing host power button" |
| 344 | if ! release_power_button "$SLED_NUM"; then |
| 345 | echo "SLED$SLED_NUM: release power button failed" |
| 346 | exit 1 |
| 347 | fi |
Allen.Wang | 4a0948d | 2021-12-15 13:41:18 +0800 | [diff] [blame] | 348 | else |
| 349 | echo "Unknown action: [ ${ACTION} ]" |
| 350 | show_usage |
| 351 | fi |