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