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=("" "" "" "" "" "" "")
142f800cabSPotin Laideclare -a HOST_STATE_CHANGE_CHECK=(0 0 0 0 0 0 0)
152f800cabSPotin LaiHOST_STATE_CHANGE_CHECH_CNT=5
162f800cabSPotin Lai
172f800cabSPotin Laideclare -A HOST_ACPI_ST_MAP
182f800cabSPotin LaiHOST_ACPI_ST_MAP["UNKNOW"]="Unknow"
192f800cabSPotin LaiHOST_ACPI_ST_MAP["NOT_PRESENT"]="G3"
202f800cabSPotin LaiHOST_ACPI_ST_MAP["AC_OFF"]="G3"
212f800cabSPotin LaiHOST_ACPI_ST_MAP["OFF"]="G3"
222f800cabSPotin LaiHOST_ACPI_ST_MAP["SLEEP"]="SLEEP"
232f800cabSPotin LaiHOST_ACPI_ST_MAP["ON"]="S0_G0_D0"
242f800cabSPotin Lai
252f800cabSPotin Laideclare -A HOST_STATE_MAP
262f800cabSPotin LaiHOST_STATE_MAP["UNKNOW"]="Quiesced"
272f800cabSPotin LaiHOST_STATE_MAP["NOT_PRESENT"]="Off"
282f800cabSPotin LaiHOST_STATE_MAP["AC_OFF"]="Off"
292f800cabSPotin LaiHOST_STATE_MAP["OFF"]="Off"
302f800cabSPotin LaiHOST_STATE_MAP["SLEEP"]="Standby"
312f800cabSPotin LaiHOST_STATE_MAP["ON"]="Running"
322f800cabSPotin Lai
332f800cabSPotin Laideclare -A CHASSIS_PWR_STATE_MAP
342f800cabSPotin LaiCHASSIS_PWR_STATE_MAP["UNKNOW"]="On"
352f800cabSPotin LaiCHASSIS_PWR_STATE_MAP["NOT_PRESENT"]="Off"
362f800cabSPotin LaiCHASSIS_PWR_STATE_MAP["AC_OFF"]="Off"
372f800cabSPotin LaiCHASSIS_PWR_STATE_MAP["OFF"]="On"
382f800cabSPotin LaiCHASSIS_PWR_STATE_MAP["SLEEP"]="On"
392f800cabSPotin LaiCHASSIS_PWR_STATE_MAP["ON"]="On"
402f800cabSPotin Lai
412f800cabSPotin Laiis_host_ac_on()
422f800cabSPotin Lai{
432f800cabSPotin Lai    local HOST_ID=$1
442f800cabSPotin Lai    local I2C_BUS
452f800cabSPotin Lai    local P1_OUTPUT_REG
462f800cabSPotin Lai    local P1_CONFIG_REG
472f800cabSPotin Lai    local HOST_PWR
482f800cabSPotin Lai    local IS_OUTPUT
492f800cabSPotin Lai
502f800cabSPotin Lai    I2C_BUS=$((HOST_ID-1))
512f800cabSPotin Lai    P1_OUTPUT_REG=$(i2cget -f -y "$I2C_BUS" 0x76 0x03)
522f800cabSPotin Lai    P1_CONFIG_REG=$(i2cget -f -y "$I2C_BUS" 0x76 0x07)
532f800cabSPotin Lai    HOST_PWR="$(( (P1_OUTPUT_REG & 0x80)>>7 ))"
542f800cabSPotin Lai    IS_OUTPUT="$(( (~P1_CONFIG_REG & 0x80)>>7 ))"
552f800cabSPotin Lai
562f800cabSPotin Lai    if [ "$((HOST_PWR & IS_OUTPUT))" -eq 1 ];then
572f800cabSPotin Lai        return 0
582f800cabSPotin Lai    fi
592f800cabSPotin Lai
602f800cabSPotin Lai    return 1
612f800cabSPotin Lai}
622f800cabSPotin Lai
632f800cabSPotin Laiupdate_host_acpi_power_state()
642f800cabSPotin Lai{
652f800cabSPotin Lai    local BUS_NAME="xyz.openbmc_project.Settings"
662f800cabSPotin Lai    local OBJ_PATH="/xyz/openbmc_project/control/host$1/acpi_power_state"
672f800cabSPotin Lai    local DBUS_PROPERTIES_INTF_NAME="org.freedesktop.DBus.Properties"
682f800cabSPotin Lai    local INTF_NAME="xyz.openbmc_project.Control.Power.ACPIPowerState"
692f800cabSPotin Lai    local PROPERTY_NAME="SysACPIStatus"
702f800cabSPotin Lai    local PROPERTY_VAL="xyz.openbmc_project.Control.Power.ACPIPowerState.ACPI.$2"
712f800cabSPotin Lai    busctl call "$BUS_NAME" "$OBJ_PATH" "$DBUS_PROPERTIES_INTF_NAME" Set ssv "$INTF_NAME" "$PROPERTY_NAME" s "$PROPERTY_VAL"
722f800cabSPotin Lai}
732f800cabSPotin Lai
742f800cabSPotin Laiupdate_host_state()
752f800cabSPotin Lai{
762f800cabSPotin Lai    local BUS_NAME="xyz.openbmc_project.State.Host$1"
772f800cabSPotin Lai    local OBJ_PATH="/xyz/openbmc_project/state/host$1"
782f800cabSPotin Lai    local DBUS_PROPERTIES_INTF_NAME="org.freedesktop.DBus.Properties"
792f800cabSPotin Lai    local INTF_NAME="xyz.openbmc_project.State.Host"
802f800cabSPotin Lai    local PROPERTY_NAME="CurrentHostState"
812f800cabSPotin Lai    local PROPERTY_VAL="xyz.openbmc_project.State.Host.HostState.$2"
822f800cabSPotin Lai    busctl call "$BUS_NAME" "$OBJ_PATH" "$DBUS_PROPERTIES_INTF_NAME" Set ssv "$INTF_NAME" "$PROPERTY_NAME" s "$PROPERTY_VAL"
832f800cabSPotin Lai}
842f800cabSPotin Lai
852f800cabSPotin Laiupdate_chassis_power_state()
862f800cabSPotin Lai{
872f800cabSPotin Lai    local BUS_NAME="xyz.openbmc_project.State.Chassis$1"
882f800cabSPotin Lai    local OBJ_PATH="/xyz/openbmc_project/state/chassis$1"
892f800cabSPotin Lai    local DBUS_PROPERTIES_INTF_NAME="org.freedesktop.DBus.Properties"
902f800cabSPotin Lai    local INTF_NAME="xyz.openbmc_project.State.Chassis"
912f800cabSPotin Lai    local PROPERTY_NAME="CurrentPowerState"
922f800cabSPotin Lai    local PROPERTY_VAL="xyz.openbmc_project.State.Chassis.PowerState.$2"
932f800cabSPotin Lai    busctl call "$BUS_NAME" "$OBJ_PATH" "$DBUS_PROPERTIES_INTF_NAME" Set ssv "$INTF_NAME" "$PROPERTY_NAME" s "$PROPERTY_VAL"
942f800cabSPotin Lai}
952f800cabSPotin Lai
962f800cabSPotin Laiupdate_sled_led_state()
972f800cabSPotin Lai{
982f800cabSPotin Lai    local HOST_ID=$1
992f800cabSPotin Lai    local HOST_STATE=$2
1002f800cabSPotin Lai    case "$HOST_STATE" in
1012f800cabSPotin Lai        ON|SLEEP)
1022f800cabSPotin Lai            systemctl start obmc-led-group-start@sled"$i"_good.service
1032f800cabSPotin Lai            ;;
1042f800cabSPotin Lai        AC_OFF|OFF)
1052f800cabSPotin Lai            systemctl start obmc-led-group-stop@sled"$i"_good.service
1062f800cabSPotin Lai            ;;
1072f800cabSPotin Lai        *)
1082f800cabSPotin Lai            ;;
1092f800cabSPotin Lai    esac
1102f800cabSPotin Lai}
1112f800cabSPotin Lai
1122f800cabSPotin Laicheck_host_state()
1132f800cabSPotin Lai{
1142f800cabSPotin Lai    if ! PORT_ST_VAL=$("$MDIO_TOOL" "$SWITCH_MDIO_BUS" phy "${PORT_NUM_MAP[$1]}" 0x00); then
1152f800cabSPotin Lai        # failed to get port status via mdio
1162f800cabSPotin Lai        echo "UNKNOW"
1172f800cabSPotin Lai        return 1
1182f800cabSPotin Lai    fi
1192f800cabSPotin Lai
1202f800cabSPotin Lai    if [ $((PORT_ST_VAL&16#0800)) -eq $((16#0000)) ]; then
1212f800cabSPotin Lai        echo "OFF"
1222f800cabSPotin Lai    elif [ $((PORT_ST_VAL&16#0A00)) -eq $((16#0A00)) ]; then
1232f800cabSPotin Lai        echo "ON"
1242f800cabSPotin Lai    elif [ $((PORT_ST_VAL&16#0900)) -eq $((16#0900)) ]; then
1252f800cabSPotin Lai        echo "SLEEP"
1262f800cabSPotin Lai    else
1272f800cabSPotin Lai        echo "UNKNOW"
1282f800cabSPotin Lai    fi
1292f800cabSPotin Lai    return 0
1302f800cabSPotin Lai}
1312f800cabSPotin Lai
1322f800cabSPotin Laiwhile true
1332f800cabSPotin Laido
1342f800cabSPotin Lai    for i in {1..6}
1352f800cabSPotin Lai    do
1362f800cabSPotin Lai        HOST_STATE=""
1372f800cabSPotin Lai        if ! is_sled_present "$i"; then
1382f800cabSPotin Lai            HOST_STATE="NOT_PRESENT"
1392f800cabSPotin Lai        elif ! is_host_ac_on "$i"; then
1402f800cabSPotin Lai            HOST_STATE="AC_OFF"
1412f800cabSPotin Lai        else
1422f800cabSPotin Lai            HOST_STATE=$(check_host_state "$i")
1432f800cabSPotin Lai        fi
1442f800cabSPotin Lai
1452f800cabSPotin Lai        if [ "$HOST_STATE" = "${HOST_PREVIOUS_STATE[$i]}" ]; then
146*d96085e5SAndrew Geissler            HOST_STATE_CHANGE_CHECK[i]="$HOST_STATE_CHANGE_CHECH_CNT"
1472f800cabSPotin Lai        elif [ "${HOST_STATE_CHANGE_CHECK[$i]}" -gt "0" ]; then
1482f800cabSPotin Lai            echo "SLED$i: detected state changed (previous:${HOST_PREVIOUS_STATE[$i]}, current:$HOST_STATE), check count: ${HOST_STATE_CHANGE_CHECK[$i]}"
149*d96085e5SAndrew Geissler            HOST_STATE_CHANGE_CHECK[i]=$((HOST_STATE_CHANGE_CHECK[i]-1))
1502f800cabSPotin Lai        else
1512f800cabSPotin Lai            echo "SLED$i: detected state changed, update host state to $HOST_STATE"
1522f800cabSPotin Lai            update_host_acpi_power_state "$i" "${HOST_ACPI_ST_MAP[$HOST_STATE]}"
1532f800cabSPotin Lai            update_host_state "$i" "${HOST_STATE_MAP[$HOST_STATE]}"
1542f800cabSPotin Lai            update_chassis_power_state "$i" "${CHASSIS_PWR_STATE_MAP[$HOST_STATE]}"
1552f800cabSPotin Lai            update_sled_led_state "$i" "$HOST_STATE"
156*d96085e5SAndrew Geissler            HOST_STATE_CHANGE_CHECK[i]="$HOST_STATE_CHANGE_CHECH_CNT"
157*d96085e5SAndrew Geissler            HOST_PREVIOUS_STATE[i]="$HOST_STATE"
1582f800cabSPotin Lai        fi
1592f800cabSPotin Lai    done
1602f800cabSPotin Lai    sleep 1
1612f800cabSPotin Laidone