12f800cabSPotin Lai#!/bin/bash
22f800cabSPotin Lai
32f800cabSPotin Laiexport PATH=$PATH:/usr/sbin:/usr/libexec
42f800cabSPotin Lai
52f800cabSPotin Lai# shellcheck source=meta-facebook/meta-bletchley/recipes-bletchley/plat-tools/files/bletchley-common-functions
62f800cabSPotin Laisource /usr/libexec/bletchley-common-functions
72f800cabSPotin Lai
82f800cabSPotin LaiMDIO_TOOL="/usr/sbin/mdio"
92f800cabSPotin LaiSWITCH_MDIO_BUS="1e650000.mdio-1"
102f800cabSPotin Lai
112f800cabSPotin Laideclare -a PORT_NUM_MAP=(10 3 2 1 7 6 5)
122f800cabSPotin Lai
132f800cabSPotin Laideclare -a HOST_PREVIOUS_STATE=("" "" "" "" "" "" "")
14*985632efSPotin Laideclare -a HOST_NEW_STATE_TEMP=("" "" "" "" "" "" "")
152f800cabSPotin Laideclare -a HOST_STATE_CHANGE_CHECK=(0 0 0 0 0 0 0)
16*985632efSPotin LaiHOST_STATE_CHANGE_CHECH_CNT=8
172f800cabSPotin Lai
182f800cabSPotin Laideclare -A HOST_ACPI_ST_MAP
192f800cabSPotin LaiHOST_ACPI_ST_MAP["UNKNOW"]="Unknow"
202f800cabSPotin LaiHOST_ACPI_ST_MAP["NOT_PRESENT"]="G3"
212f800cabSPotin LaiHOST_ACPI_ST_MAP["AC_OFF"]="G3"
222f800cabSPotin LaiHOST_ACPI_ST_MAP["OFF"]="G3"
232f800cabSPotin LaiHOST_ACPI_ST_MAP["SLEEP"]="SLEEP"
242f800cabSPotin LaiHOST_ACPI_ST_MAP["ON"]="S0_G0_D0"
252f800cabSPotin Lai
262f800cabSPotin Laideclare -A HOST_STATE_MAP
272f800cabSPotin LaiHOST_STATE_MAP["UNKNOW"]="Quiesced"
282f800cabSPotin LaiHOST_STATE_MAP["NOT_PRESENT"]="Off"
292f800cabSPotin LaiHOST_STATE_MAP["AC_OFF"]="Off"
302f800cabSPotin LaiHOST_STATE_MAP["OFF"]="Off"
312f800cabSPotin LaiHOST_STATE_MAP["SLEEP"]="Standby"
322f800cabSPotin LaiHOST_STATE_MAP["ON"]="Running"
332f800cabSPotin Lai
342f800cabSPotin Laideclare -A CHASSIS_PWR_STATE_MAP
352f800cabSPotin LaiCHASSIS_PWR_STATE_MAP["UNKNOW"]="On"
362f800cabSPotin LaiCHASSIS_PWR_STATE_MAP["NOT_PRESENT"]="Off"
372f800cabSPotin LaiCHASSIS_PWR_STATE_MAP["AC_OFF"]="Off"
382f800cabSPotin LaiCHASSIS_PWR_STATE_MAP["OFF"]="On"
392f800cabSPotin LaiCHASSIS_PWR_STATE_MAP["SLEEP"]="On"
402f800cabSPotin LaiCHASSIS_PWR_STATE_MAP["ON"]="On"
412f800cabSPotin Lai
422f800cabSPotin Laiis_host_ac_on()
432f800cabSPotin Lai{
442f800cabSPotin Lai    local HOST_ID=$1
452f800cabSPotin Lai    local I2C_BUS
462f800cabSPotin Lai    local P1_OUTPUT_REG
472f800cabSPotin Lai    local P1_CONFIG_REG
482f800cabSPotin Lai    local HOST_PWR
492f800cabSPotin Lai    local IS_OUTPUT
502f800cabSPotin Lai
512f800cabSPotin Lai    I2C_BUS=$((HOST_ID-1))
522f800cabSPotin Lai    P1_OUTPUT_REG=$(i2cget -f -y "$I2C_BUS" 0x76 0x03)
532f800cabSPotin Lai    P1_CONFIG_REG=$(i2cget -f -y "$I2C_BUS" 0x76 0x07)
542f800cabSPotin Lai    HOST_PWR="$(( (P1_OUTPUT_REG & 0x80)>>7 ))"
552f800cabSPotin Lai    IS_OUTPUT="$(( (~P1_CONFIG_REG & 0x80)>>7 ))"
562f800cabSPotin Lai
572f800cabSPotin Lai    if [ "$((HOST_PWR & IS_OUTPUT))" -eq 1 ];then
582f800cabSPotin Lai        return 0
592f800cabSPotin Lai    fi
602f800cabSPotin Lai
612f800cabSPotin Lai    return 1
622f800cabSPotin Lai}
632f800cabSPotin Lai
642f800cabSPotin Laiupdate_host_acpi_power_state()
652f800cabSPotin Lai{
662f800cabSPotin Lai    local BUS_NAME="xyz.openbmc_project.Settings"
672f800cabSPotin Lai    local OBJ_PATH="/xyz/openbmc_project/control/host$1/acpi_power_state"
682f800cabSPotin Lai    local DBUS_PROPERTIES_INTF_NAME="org.freedesktop.DBus.Properties"
692f800cabSPotin Lai    local INTF_NAME="xyz.openbmc_project.Control.Power.ACPIPowerState"
702f800cabSPotin Lai    local PROPERTY_NAME="SysACPIStatus"
712f800cabSPotin Lai    local PROPERTY_VAL="xyz.openbmc_project.Control.Power.ACPIPowerState.ACPI.$2"
722f800cabSPotin Lai    busctl call "$BUS_NAME" "$OBJ_PATH" "$DBUS_PROPERTIES_INTF_NAME" Set ssv "$INTF_NAME" "$PROPERTY_NAME" s "$PROPERTY_VAL"
732f800cabSPotin Lai}
742f800cabSPotin Lai
752f800cabSPotin Laiupdate_host_state()
762f800cabSPotin Lai{
772f800cabSPotin Lai    local BUS_NAME="xyz.openbmc_project.State.Host$1"
782f800cabSPotin Lai    local OBJ_PATH="/xyz/openbmc_project/state/host$1"
792f800cabSPotin Lai    local DBUS_PROPERTIES_INTF_NAME="org.freedesktop.DBus.Properties"
802f800cabSPotin Lai    local INTF_NAME="xyz.openbmc_project.State.Host"
812f800cabSPotin Lai    local PROPERTY_NAME="CurrentHostState"
822f800cabSPotin Lai    local PROPERTY_VAL="xyz.openbmc_project.State.Host.HostState.$2"
832f800cabSPotin Lai    busctl call "$BUS_NAME" "$OBJ_PATH" "$DBUS_PROPERTIES_INTF_NAME" Set ssv "$INTF_NAME" "$PROPERTY_NAME" s "$PROPERTY_VAL"
842f800cabSPotin Lai}
852f800cabSPotin Lai
862f800cabSPotin Laiupdate_chassis_power_state()
872f800cabSPotin Lai{
882f800cabSPotin Lai    local BUS_NAME="xyz.openbmc_project.State.Chassis$1"
892f800cabSPotin Lai    local OBJ_PATH="/xyz/openbmc_project/state/chassis$1"
902f800cabSPotin Lai    local DBUS_PROPERTIES_INTF_NAME="org.freedesktop.DBus.Properties"
912f800cabSPotin Lai    local INTF_NAME="xyz.openbmc_project.State.Chassis"
922f800cabSPotin Lai    local PROPERTY_NAME="CurrentPowerState"
932f800cabSPotin Lai    local PROPERTY_VAL="xyz.openbmc_project.State.Chassis.PowerState.$2"
942f800cabSPotin Lai    busctl call "$BUS_NAME" "$OBJ_PATH" "$DBUS_PROPERTIES_INTF_NAME" Set ssv "$INTF_NAME" "$PROPERTY_NAME" s "$PROPERTY_VAL"
952f800cabSPotin Lai}
962f800cabSPotin Lai
972f800cabSPotin Laiupdate_sled_led_state()
982f800cabSPotin Lai{
992f800cabSPotin Lai    local HOST_ID=$1
1002f800cabSPotin Lai    local HOST_STATE=$2
1012f800cabSPotin Lai    case "$HOST_STATE" in
1022f800cabSPotin Lai        ON|SLEEP)
103*985632efSPotin Lai            systemctl start obmc-led-group-start@sled"$HOST_ID"_good.service
1042f800cabSPotin Lai            ;;
1052f800cabSPotin Lai        AC_OFF|OFF)
106*985632efSPotin Lai            systemctl start obmc-led-group-stop@sled"$HOST_ID"_good.service
1072f800cabSPotin Lai            ;;
1082f800cabSPotin Lai        *)
1092f800cabSPotin Lai            ;;
1102f800cabSPotin Lai    esac
1112f800cabSPotin Lai}
1122f800cabSPotin Lai
1132f800cabSPotin Laicheck_host_state()
1142f800cabSPotin Lai{
1152f800cabSPotin Lai    if ! PORT_ST_VAL=$("$MDIO_TOOL" "$SWITCH_MDIO_BUS" phy "${PORT_NUM_MAP[$1]}" 0x00); then
1162f800cabSPotin Lai        # failed to get port status via mdio
1172f800cabSPotin Lai        echo "UNKNOW"
1182f800cabSPotin Lai        return 1
1192f800cabSPotin Lai    fi
1202f800cabSPotin Lai
1212f800cabSPotin Lai    if [ $((PORT_ST_VAL&16#0800)) -eq $((16#0000)) ]; then
1222f800cabSPotin Lai        echo "OFF"
1232f800cabSPotin Lai    elif [ $((PORT_ST_VAL&16#0A00)) -eq $((16#0A00)) ]; then
1242f800cabSPotin Lai        echo "ON"
1252f800cabSPotin Lai    elif [ $((PORT_ST_VAL&16#0900)) -eq $((16#0900)) ]; then
1262f800cabSPotin Lai        echo "SLEEP"
1272f800cabSPotin Lai    else
1282f800cabSPotin Lai        echo "UNKNOW"
1292f800cabSPotin Lai    fi
1302f800cabSPotin Lai    return 0
1312f800cabSPotin Lai}
1322f800cabSPotin Lai
1332f800cabSPotin Laiwhile true
1342f800cabSPotin Laido
1352f800cabSPotin Lai    for i in {1..6}
1362f800cabSPotin Lai    do
1372f800cabSPotin Lai        HOST_STATE=""
1382f800cabSPotin Lai        if ! is_sled_present "$i"; then
1392f800cabSPotin Lai            HOST_STATE="NOT_PRESENT"
1402f800cabSPotin Lai        elif ! is_host_ac_on "$i"; then
1412f800cabSPotin Lai            HOST_STATE="AC_OFF"
1422f800cabSPotin Lai        else
1432f800cabSPotin Lai            HOST_STATE=$(check_host_state "$i")
1442f800cabSPotin Lai        fi
1452f800cabSPotin Lai
1462f800cabSPotin Lai        if [ "$HOST_STATE" = "${HOST_PREVIOUS_STATE[$i]}" ]; then
147*985632efSPotin Lai            if [ "${HOST_STATE_CHANGE_CHECK[$i]}" -lt "$HOST_STATE_CHANGE_CHECH_CNT" ]; then
148*985632efSPotin Lai                echo "SLED$i: detected state recover (previous:${HOST_PREVIOUS_STATE[$i]}, current:$HOST_STATE)"
149*985632efSPotin Lai            fi
150d96085e5SAndrew Geissler            HOST_STATE_CHANGE_CHECK[i]="$HOST_STATE_CHANGE_CHECH_CNT"
151*985632efSPotin Lai            HOST_NEW_STATE_TEMP[i]="$HOST_STATE"
152*985632efSPotin Lai        else
153*985632efSPotin Lai            if [ "$HOST_STATE" != "${HOST_NEW_STATE_TEMP[$i]}" ]; then
154*985632efSPotin Lai                HOST_STATE_CHANGE_CHECK[i]="$HOST_STATE_CHANGE_CHECH_CNT"
155*985632efSPotin Lai            fi
156*985632efSPotin Lai
157*985632efSPotin Lai            if [ "${HOST_STATE_CHANGE_CHECK[$i]}" -gt "0" ]; then
1582f800cabSPotin Lai                echo "SLED$i: detected state changed (previous:${HOST_PREVIOUS_STATE[$i]}, current:$HOST_STATE), check count: ${HOST_STATE_CHANGE_CHECK[$i]}"
159d96085e5SAndrew Geissler                HOST_STATE_CHANGE_CHECK[i]=$((HOST_STATE_CHANGE_CHECK[i]-1))
1602f800cabSPotin Lai            else
1612f800cabSPotin Lai                echo "SLED$i: detected state changed, update host state to $HOST_STATE"
1622f800cabSPotin Lai                update_host_acpi_power_state "$i" "${HOST_ACPI_ST_MAP[$HOST_STATE]}"
1632f800cabSPotin Lai                update_host_state "$i" "${HOST_STATE_MAP[$HOST_STATE]}"
1642f800cabSPotin Lai                update_chassis_power_state "$i" "${CHASSIS_PWR_STATE_MAP[$HOST_STATE]}"
1652f800cabSPotin Lai                update_sled_led_state "$i" "$HOST_STATE"
166d96085e5SAndrew Geissler                HOST_STATE_CHANGE_CHECK[i]="$HOST_STATE_CHANGE_CHECH_CNT"
167d96085e5SAndrew Geissler                HOST_PREVIOUS_STATE[i]="$HOST_STATE"
1682f800cabSPotin Lai            fi
169*985632efSPotin Lai            HOST_NEW_STATE_TEMP[i]="$HOST_STATE"
170*985632efSPotin Lai        fi
1712f800cabSPotin Lai    done
1722f800cabSPotin Lai    sleep 1
1732f800cabSPotin Laidone