1#!/bin/bash 2 3export PATH=$PATH:/usr/sbin:/usr/libexec 4 5# shellcheck source=meta-facebook/meta-bletchley/recipes-bletchley/plat-tools/files/bletchley-common-functions 6source /usr/libexec/bletchley-common-functions 7 8MDIO_TOOL="/usr/sbin/mdio" 9SWITCH_MDIO_BUS="1e650000.mdio-1" 10 11declare -a PORT_NUM_MAP=(10 3 2 1 7 6 5) 12 13declare -a HOST_PREVIOUS_STATE=("" "" "" "" "" "" "") 14declare -a HOST_STATE_CHANGE_CHECK=(0 0 0 0 0 0 0) 15HOST_STATE_CHANGE_CHECH_CNT=5 16 17declare -A HOST_ACPI_ST_MAP 18HOST_ACPI_ST_MAP["UNKNOW"]="Unknow" 19HOST_ACPI_ST_MAP["NOT_PRESENT"]="G3" 20HOST_ACPI_ST_MAP["AC_OFF"]="G3" 21HOST_ACPI_ST_MAP["OFF"]="G3" 22HOST_ACPI_ST_MAP["SLEEP"]="SLEEP" 23HOST_ACPI_ST_MAP["ON"]="S0_G0_D0" 24 25declare -A HOST_STATE_MAP 26HOST_STATE_MAP["UNKNOW"]="Quiesced" 27HOST_STATE_MAP["NOT_PRESENT"]="Off" 28HOST_STATE_MAP["AC_OFF"]="Off" 29HOST_STATE_MAP["OFF"]="Off" 30HOST_STATE_MAP["SLEEP"]="Standby" 31HOST_STATE_MAP["ON"]="Running" 32 33declare -A CHASSIS_PWR_STATE_MAP 34CHASSIS_PWR_STATE_MAP["UNKNOW"]="On" 35CHASSIS_PWR_STATE_MAP["NOT_PRESENT"]="Off" 36CHASSIS_PWR_STATE_MAP["AC_OFF"]="Off" 37CHASSIS_PWR_STATE_MAP["OFF"]="On" 38CHASSIS_PWR_STATE_MAP["SLEEP"]="On" 39CHASSIS_PWR_STATE_MAP["ON"]="On" 40 41is_host_ac_on() 42{ 43 local HOST_ID=$1 44 local I2C_BUS 45 local P1_OUTPUT_REG 46 local P1_CONFIG_REG 47 local HOST_PWR 48 local IS_OUTPUT 49 50 I2C_BUS=$((HOST_ID-1)) 51 P1_OUTPUT_REG=$(i2cget -f -y "$I2C_BUS" 0x76 0x03) 52 P1_CONFIG_REG=$(i2cget -f -y "$I2C_BUS" 0x76 0x07) 53 HOST_PWR="$(( (P1_OUTPUT_REG & 0x80)>>7 ))" 54 IS_OUTPUT="$(( (~P1_CONFIG_REG & 0x80)>>7 ))" 55 56 if [ "$((HOST_PWR & IS_OUTPUT))" -eq 1 ];then 57 return 0 58 fi 59 60 return 1 61} 62 63update_host_acpi_power_state() 64{ 65 local BUS_NAME="xyz.openbmc_project.Settings" 66 local OBJ_PATH="/xyz/openbmc_project/control/host$1/acpi_power_state" 67 local DBUS_PROPERTIES_INTF_NAME="org.freedesktop.DBus.Properties" 68 local INTF_NAME="xyz.openbmc_project.Control.Power.ACPIPowerState" 69 local PROPERTY_NAME="SysACPIStatus" 70 local PROPERTY_VAL="xyz.openbmc_project.Control.Power.ACPIPowerState.ACPI.$2" 71 busctl call "$BUS_NAME" "$OBJ_PATH" "$DBUS_PROPERTIES_INTF_NAME" Set ssv "$INTF_NAME" "$PROPERTY_NAME" s "$PROPERTY_VAL" 72} 73 74update_host_state() 75{ 76 local BUS_NAME="xyz.openbmc_project.State.Host$1" 77 local OBJ_PATH="/xyz/openbmc_project/state/host$1" 78 local DBUS_PROPERTIES_INTF_NAME="org.freedesktop.DBus.Properties" 79 local INTF_NAME="xyz.openbmc_project.State.Host" 80 local PROPERTY_NAME="CurrentHostState" 81 local PROPERTY_VAL="xyz.openbmc_project.State.Host.HostState.$2" 82 busctl call "$BUS_NAME" "$OBJ_PATH" "$DBUS_PROPERTIES_INTF_NAME" Set ssv "$INTF_NAME" "$PROPERTY_NAME" s "$PROPERTY_VAL" 83} 84 85update_chassis_power_state() 86{ 87 local BUS_NAME="xyz.openbmc_project.State.Chassis$1" 88 local OBJ_PATH="/xyz/openbmc_project/state/chassis$1" 89 local DBUS_PROPERTIES_INTF_NAME="org.freedesktop.DBus.Properties" 90 local INTF_NAME="xyz.openbmc_project.State.Chassis" 91 local PROPERTY_NAME="CurrentPowerState" 92 local PROPERTY_VAL="xyz.openbmc_project.State.Chassis.PowerState.$2" 93 busctl call "$BUS_NAME" "$OBJ_PATH" "$DBUS_PROPERTIES_INTF_NAME" Set ssv "$INTF_NAME" "$PROPERTY_NAME" s "$PROPERTY_VAL" 94} 95 96update_sled_led_state() 97{ 98 local HOST_ID=$1 99 local HOST_STATE=$2 100 case "$HOST_STATE" in 101 ON|SLEEP) 102 systemctl start obmc-led-group-start@sled"$i"_good.service 103 ;; 104 AC_OFF|OFF) 105 systemctl start obmc-led-group-stop@sled"$i"_good.service 106 ;; 107 *) 108 ;; 109 esac 110} 111 112check_host_state() 113{ 114 if ! PORT_ST_VAL=$("$MDIO_TOOL" "$SWITCH_MDIO_BUS" phy "${PORT_NUM_MAP[$1]}" 0x00); then 115 # failed to get port status via mdio 116 echo "UNKNOW" 117 return 1 118 fi 119 120 if [ $((PORT_ST_VAL&16#0800)) -eq $((16#0000)) ]; then 121 echo "OFF" 122 elif [ $((PORT_ST_VAL&16#0A00)) -eq $((16#0A00)) ]; then 123 echo "ON" 124 elif [ $((PORT_ST_VAL&16#0900)) -eq $((16#0900)) ]; then 125 echo "SLEEP" 126 else 127 echo "UNKNOW" 128 fi 129 return 0 130} 131 132while true 133do 134 for i in {1..6} 135 do 136 HOST_STATE="" 137 if ! is_sled_present "$i"; then 138 HOST_STATE="NOT_PRESENT" 139 elif ! is_host_ac_on "$i"; then 140 HOST_STATE="AC_OFF" 141 else 142 HOST_STATE=$(check_host_state "$i") 143 fi 144 145 if [ "$HOST_STATE" = "${HOST_PREVIOUS_STATE[$i]}" ]; then 146 HOST_STATE_CHANGE_CHECK[i]="$HOST_STATE_CHANGE_CHECH_CNT" 147 elif [ "${HOST_STATE_CHANGE_CHECK[$i]}" -gt "0" ]; then 148 echo "SLED$i: detected state changed (previous:${HOST_PREVIOUS_STATE[$i]}, current:$HOST_STATE), check count: ${HOST_STATE_CHANGE_CHECK[$i]}" 149 HOST_STATE_CHANGE_CHECK[i]=$((HOST_STATE_CHANGE_CHECK[i]-1)) 150 else 151 echo "SLED$i: detected state changed, update host state to $HOST_STATE" 152 update_host_acpi_power_state "$i" "${HOST_ACPI_ST_MAP[$HOST_STATE]}" 153 update_host_state "$i" "${HOST_STATE_MAP[$HOST_STATE]}" 154 update_chassis_power_state "$i" "${CHASSIS_PWR_STATE_MAP[$HOST_STATE]}" 155 update_sled_led_state "$i" "$HOST_STATE" 156 HOST_STATE_CHANGE_CHECK[i]="$HOST_STATE_CHANGE_CHECH_CNT" 157 HOST_PREVIOUS_STATE[i]="$HOST_STATE" 158 fi 159 done 160 sleep 1 161done