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