meta-facebook: greatlakes: Set 12V and host power state

Description:
- Set 12V and host power state.

Design:
- Use i2c bus 12 address 0xf register 0x25 to
  detect 12V state
- Register gpio interrupt to set host power state when 12V off
- Set correct state on CurrentHostState property for host status
  after doing host power control
- Set correcr state on CurrentPowerState property for 12V status
  after doing 12V power control

Test Case:
1. 12V off - pass
2. 12V on - pass
3. 12V cycle - pass
4. sled cycle - pass

Signed-off-by: Delphine CC Chiu <Delphine_CC_Chiu@wiwynn.com>
Change-Id: I4ccdec0a98037fd6ff405b2f534bb897d0e2e010
diff --git a/meta-facebook/meta-greatlakes/recipes-phosphor/gpio/greatlakes-gpio-monitor-register.bb b/meta-facebook/meta-greatlakes/recipes-phosphor/gpio/greatlakes-gpio-monitor-register.bb
new file mode 100644
index 0000000..6a98f27
--- /dev/null
+++ b/meta-facebook/meta-greatlakes/recipes-phosphor/gpio/greatlakes-gpio-monitor-register.bb
@@ -0,0 +1,16 @@
+SUMMARY = "greatlakes gpio monitor register"
+DESCRIPTION = "greatlakes gpio monitor register"
+SECTION = "base"
+PR = "r1"
+LICENSE = "Apache-2.0"
+LIC_FILES_CHKSUM = "file://${COREBASE}/meta/files/common-licenses/Apache-2.0;md5=89aea4e17d99a7cacdbeed46a0096b10"
+S = "${WORKDIR}"
+
+inherit obmc-phosphor-systemd
+
+SRC_URI = "file://set-current-host-state@.service \
+          "
+
+DEPENDS += "systemd"
+
+SYSTEMD_SERVICE:${PN} += "set-current-host-state@.service"
diff --git a/meta-facebook/meta-greatlakes/recipes-phosphor/gpio/greatlakes-gpio-monitor-register/set-current-host-state@.service b/meta-facebook/meta-greatlakes/recipes-phosphor/gpio/greatlakes-gpio-monitor-register/set-current-host-state@.service
new file mode 100644
index 0000000..27973b9
--- /dev/null
+++ b/meta-facebook/meta-greatlakes/recipes-phosphor/gpio/greatlakes-gpio-monitor-register/set-current-host-state@.service
@@ -0,0 +1,8 @@
+[Unit]
+Description=Set currrent host state property slot%i
+
+[Service]
+Type=oneshot
+ExecStart=/usr/bin/busctl set-property xyz.openbmc_project.State.Host%i /xyz/openbmc_project/state/host%i xyz.openbmc_project.State.Host CurrentHostState s "xyz.openbmc_project.State.Host.HostState.Off"
+SyslogIdentifier=set-current-host-state-slot%i
+
diff --git a/meta-facebook/meta-greatlakes/recipes-phosphor/images/fb-greatlakes--phosphor-image.inc b/meta-facebook/meta-greatlakes/recipes-phosphor/images/fb-greatlakes--phosphor-image.inc
deleted file mode 100644
index eebd451..0000000
--- a/meta-facebook/meta-greatlakes/recipes-phosphor/images/fb-greatlakes--phosphor-image.inc
+++ /dev/null
@@ -1,3 +0,0 @@
-OBMC_IMAGE_EXTRA_INSTALL:append = " \
-    greatlakes-sysinit \
-"
diff --git a/meta-facebook/meta-greatlakes/recipes-phosphor/images/fb-greatlakes-phosphor-image.inc b/meta-facebook/meta-greatlakes/recipes-phosphor/images/fb-greatlakes-phosphor-image.inc
new file mode 100644
index 0000000..afff8ec
--- /dev/null
+++ b/meta-facebook/meta-greatlakes/recipes-phosphor/images/fb-greatlakes-phosphor-image.inc
@@ -0,0 +1,13 @@
+OBMC_IMAGE_EXTRA_INSTALL:append = " \
+    greatlakes-sysinit \
+    greatlakes-gpio-monitor-register \
+"
+
+# Currently, the led related services couldn't complete
+# causing that multi-user.target never complete.
+# If multi-user.target doesn't complete,
+# the obmc-host-startmin@.target couldn't start.
+# In that case, BMC couldn't set current host state to "Running".
+IMAGE_FEATURES:remove = " \
+    obmc-leds \
+"
diff --git a/meta-facebook/recipes-phosphor/state/phosphor-state-manager/greatlakes/chassis-powercycle b/meta-facebook/recipes-phosphor/state/phosphor-state-manager/greatlakes/chassis-powercycle
index b765e1e..ab9c457 100644
--- a/meta-facebook/recipes-phosphor/state/phosphor-state-manager/greatlakes/chassis-powercycle
+++ b/meta-facebook/recipes-phosphor/state/phosphor-state-manager/greatlakes/chassis-powercycle
@@ -4,36 +4,47 @@
 # shellcheck source=meta-facebook/recipes-phosphor/state/phosphor-state-manager/greatlakes/power-cmd
 source /usr/libexec/phosphor-state-manager/power-cmd
 
-POWER_OK_SLOT=$(( $1+9 ))
-slot=$(( $1-1 ))
-OFFSET=$(( 0x9 + slot ))
+SLOT=$(( $1-1 ))
+OFFSET=$(( 0x9 + SLOT ))
 
-chassis-power-gpio-status()
+chassis-power-cycle()
 {
-    POWER_OK_SLOT=$(( $1+9 ))
-    if [ "$1" -le 4  ]
+    CHASSIS_ID=$1
+
+    if [ "$CHASSIS_ID" -le 4  ]
     then
-        status=$(gpioget 0 $POWER_OK_SLOT)
-        if [ "$status" == 1 ]
+        chassis_status=$(chassis-power-status $SLOT)
+        if [ "$chassis_status" == "$STATE_ON" ]
         then
-            i2cset -y $BUS_NUM $CPLD_PWR_CTRL_ADDR $OFFSET $POW_OFF_SLOT
+            i2cset -y "$CPLD_BUS_NUM" "$CPLD_PWR_CTRL_ADDR" "$OFFSET" "$POW_OFF_SLOT"
             sleep 10
-	fi
-        i2cset -y $BUS_NUM $CPLD_PWR_CTRL_ADDR $OFFSET $POW_ON_SLOT
-        exit 0;
+        fi
+        i2cset -y "$CPLD_BUS_NUM" "$CPLD_PWR_CTRL_ADDR" "$OFFSET" "$POW_ON_SLOT"
+        sleep 2
+
+        # Check chassis stattus after doing 12V cycle
+        chassis_status=$(chassis-power-status $SLOT)
+        if [ "$chassis_status" == "$STATE_ON" ]
+        then
+            busctl set-property "$CHASSIS_BUS_NAME""$CHASSIS_ID" "$CHASSIS_OBJ_PATH""$CHASSIS_ID" "$CHASSIS_INTF_NAME" "$CHASSIS_PROPERTY_NAME" s "$CHASSIS_ON"
+            exit 0;
+        else
+            busctl set-property "$CHASSIS_BUS_NAME""$CHASSIS_ID" "$CHASSIS_OBJ_PATH""$CHASSIS_ID" "$CHASSIS_INTF_NAME" "$CHASSIS_PROPERTY_NAME" s "$CHASSIS_OFF"
+            exit 0;
+        fi
     fi
-    echo "invalid slot id"
+
+    echo "Invalid slot id"
     exit 1;
 }
 
-chassisid=$1
-if [ "$chassisid" == 0 ]
+if [ "$1" == 0 ]
 then
     echo "Enter SLED-cycle"
-    result=$(i2cset -y $BUS_NUM $CPLD_PWR_CTRL_ADDR 0x2b 0x1)
+    result=$(i2cset -y "$CPLD_BUS_NUM" "$CPLD_PWR_CTRL_ADDR" 0x2b 0x1)
     echo "$result"
 else
     echo "Enter slot$1 cycle"
-    chassis-power-gpio-status "$chassisid"
+    chassis-power-cycle "$1"
 fi
 
diff --git a/meta-facebook/recipes-phosphor/state/phosphor-state-manager/greatlakes/chassis-poweroff b/meta-facebook/recipes-phosphor/state/phosphor-state-manager/greatlakes/chassis-poweroff
index f7d6448..9417352 100644
--- a/meta-facebook/recipes-phosphor/state/phosphor-state-manager/greatlakes/chassis-poweroff
+++ b/meta-facebook/recipes-phosphor/state/phosphor-state-manager/greatlakes/chassis-poweroff
@@ -4,40 +4,40 @@
 # shellcheck source=meta-facebook/recipes-phosphor/state/phosphor-state-manager/greatlakes/power-cmd
 source /usr/libexec/phosphor-state-manager/power-cmd
 
-POWER_OK_SLOT=$(( $1+9 ))
-slot=$(( $1-1 ))
-OFFSET=$(( 0x9 + slot ))
+CHASSIS_ID=$1
+SLOT=$(( $1-1 ))
+OFFSET=$(( 0x9 + SLOT ))
 
 # Server 12v power off
 chassis-power-off()
 {
-        result=$(i2cset -y $BUS_NUM $CPLD_PWR_CTRL_ADDR $OFFSET $POW_OFF_SLOT)
+        result=$(i2cset -y "$CPLD_BUS_NUM" "$CPLD_PWR_CTRL_ADDR" "$OFFSET $POW_OFF_SLOT")
         echo "$result"
-	sleep 1
-        chassis_status=$(gpioget 0 $POWER_OK_SLOT)
-        if [ "$chassis_status" == 0 ]
+        sleep 1
+
+        # Check chassis status after doing 12V off
+        chassis_status=$(chassis-power-status $SLOT)
+        if [ "$chassis_status" == "$STATE_OFF" ]
         then
+                busctl set-property "$CHASSIS_BUS_NAME""$CHASSIS_ID" "$CHASSIS_OBJ_PATH""$CHASSIS_ID" "$CHASSIS_INTF_NAME" "$CHASSIS_PROPERTY_NAME" s "$CHASSIS_OFF"
                 echo "Chassis is power off"
         else
-                echo "Chassis is already off"
-                exit 1;
+                busctl set-property "$CHASSIS_BUS_NAME""$CHASSIS_ID" "$CHASSIS_OBJ_PATH""$CHASSIS_ID" "$CHASSIS_INTF_NAME" "$CHASSIS_PROPERTY_NAME" s "$CHASSIS_ON"
+                echo "Chassis is power on"
+                exit 0;
         fi
 }
-chassis-power-gpio-status()
-{
-        if [ "$slot" -le 4 ]
-        then
-                status=$(gpioget 0 $POWER_OK_SLOT)
-                if [ "$status" == 1 ]
-                then
-                        chassis-power-off
-                        exit 0;
-                else
-                        echo "Chassis is already off"
-                        exit 1;
-                fi
-        fi
+
+chassis_status=$(chassis-power-status $SLOT)
+if [ "$chassis_status" == "$STATE_UNKNOWN" ]
+then
         echo "Invalid slot id"
         exit 1;
-}
-chassis-power-gpio-status
+elif [ "$chassis_status" == "$STATE_ON" ]
+then
+        chassis-power-off
+        exit 0;
+else
+        echo "Chassis is already off"
+        exit 0;
+fi
diff --git a/meta-facebook/recipes-phosphor/state/phosphor-state-manager/greatlakes/chassis-poweron b/meta-facebook/recipes-phosphor/state/phosphor-state-manager/greatlakes/chassis-poweron
index 9fe8315..08b1e30 100644
--- a/meta-facebook/recipes-phosphor/state/phosphor-state-manager/greatlakes/chassis-poweron
+++ b/meta-facebook/recipes-phosphor/state/phosphor-state-manager/greatlakes/chassis-poweron
@@ -4,40 +4,40 @@
 # shellcheck source=meta-facebook/recipes-phosphor/state/phosphor-state-manager/greatlakes/power-cmd
 source /usr/libexec/phosphor-state-manager/power-cmd
 
-POWER_OK_SLOT=$(( $1+9 ))
-slot=$(( $1-1 ))
-OFFSET=$(( 0x9 + slot ))
+CHASSIS_ID=$1
+SLOT=$(( $1-1 ))
+OFFSET=$(( 0x9 + SLOT ))
 
 # Server 12v power on
 chassis-power-on()
 {
-       result=$(i2cset -y $BUS_NUM $CPLD_PWR_CTRL_ADDR $OFFSET $POW_ON_SLOT)
+       result=$(i2cset -y "$CPLD_BUS_NUM" "$CPLD_PWR_CTRL_ADDR" "$OFFSET" "$POW_ON_SLOT")
        echo "$result"
        sleep 1
-       chassis_status=$(gpioget 0 $POWER_OK_SLOT)
-       if [ "$chassis_status" == 1 ]
+
+       # Check chassis stattus after doing 12V on
+       chassis_status=$(chassis-power-status $SLOT)
+       if [ "$chassis_status" == "$STATE_ON" ]
        then
+               busctl set-property "$CHASSIS_BUS_NAME""$CHASSIS_ID" "$CHASSIS_OBJ_PATH""$CHASSIS_ID" "$CHASSIS_INTF_NAME" "$CHASSIS_PROPERTY_NAME" s "$CHASSIS_ON"
                echo "Chassis is power on"
        else
-               echo "Chassis is already on"
-               exit 1;
+               busctl set-property "$CHASSIS_BUS_NAME""$CHASSIS_ID" "$CHASSIS_OBJ_PATH""$CHASSIS_ID" "$CHASSIS_INTF_NAME" "$CHASSIS_PROPERTY_NAME" s "$CHASSIS_OFF"
+               echo "Chassis is power off"
+               exit 0;
        fi
 }
-chassis-power-gpio-status()
-{
-        if [ "$slot" -le 4  ]
-        then
-                status=$(gpioget 0 $POWER_OK_SLOT)
-                if [ "$status" == 0 ]
-                then
-                        chassis-power-on
-                        exit 0;
-                else
-                        echo "Chassis is already on"
-                        exit 1;
-                fi
-        fi
+
+chassis_status=$(chassis-power-status $SLOT)
+if [ "$chassis_status" == "$STATE_UNKNOWN" ]
+then
         echo "Invalid slot id"
         exit 1;
-}
-chassis-power-gpio-status
+elif [ "$chassis_status" == "$STATE_OFF" ]
+then
+        chassis-power-on
+        exit 0;
+else
+        echo "Chassis is already on"
+        exit 0;
+fi
diff --git a/meta-facebook/recipes-phosphor/state/phosphor-state-manager/greatlakes/host-powercycle b/meta-facebook/recipes-phosphor/state/phosphor-state-manager/greatlakes/host-powercycle
index 81cb429..22a093f 100644
--- a/meta-facebook/recipes-phosphor/state/phosphor-state-manager/greatlakes/host-powercycle
+++ b/meta-facebook/recipes-phosphor/state/phosphor-state-manager/greatlakes/host-powercycle
@@ -4,6 +4,7 @@
 # shellcheck source=meta-facebook/recipes-phosphor/state/phosphor-state-manager/greatlakes/host-poweron
 source /usr/libexec/phosphor-state-manager/power-cmd
 
+HOST_ID=$1
 SLOT_ID=$(( $1-1  << 2 ))
 
 # Host Reboot
@@ -12,9 +13,9 @@
         power_delay=$1
         for pwr_cmd in "${power_seq[@]}"
         do
-             result="$(busctl call $SERVICE $OBJECT_PATH $INTERFACE sendRequest yyyyay $SLOT_ID $NETFN $LUN $CMD $DATA_LEN 0x01 0x42 0x01 0x00 "$pwr_cmd")"
+             result="$(busctl call "$SERVICE" "$OBJECT_PATH" "$INTERFACE" sendRequest yyyyay "$SLOT_ID" "$NETFN" "$LUN" "$CMD" "$DATA_LEN" 0x01 0x42 0x01 0x00 "$pwr_cmd")"
              echo "$result"
-             if [ "$pwr_cmd" == $POWER_BTN_LOW ]
+             if [ "$pwr_cmd" == "$POWER_BTN_LOW" ]
              then
                      sleep "$power_delay"
              fi
@@ -22,22 +23,25 @@
 }
 
 # Check Host Response and Status
-check-response()
-{
-   response="$(busctl call $SERVICE $OBJECT_PATH $INTERFACE sendRequest yyyyay $SLOT_ID 0x38 $LUN 0x3 0x03 0x15 0xa0 0x00)"
+host_status=$(host-power-status $SLOT_ID)
+if [ "$host_status" == "$STATE_ON" ]
+then
+  host-reboot 6
+  sleep 15
+  host-reboot 2
+  
+  host_status=$(host-power-status $SLOT_ID)
+  if [ "$host_status" == "$STATE_ON" ]
+  then
+      busctl set-property "$HOST_BUS_NAME""$HOST_ID" "$HOST_OBJ_PATH""$HOST_ID" "$HOST_INTF_NAME" "$HOST_PROPERTY_NAME" s "$HOST_ON"
+      exit 0;
+  else
+      busctl set-property "$HOST_BUS_NAME""$HOST_ID" "$HOST_OBJ_PATH""$HOST_ID" "$HOST_INTF_NAME" "$HOST_PROPERTY_NAME" s "$HOST_OFF"
+      exit 0;
+  fi
+else
+  echo "Host is Off"
+  exit 1;
+fi
 
-   result=$(echo "$response" | cut -d" " -f $PWRGD_SYS_PWROK_INDEX)
-   res="$(( "$result" & 0x80  ))"
-   pgood="$(( "$res" >> 7 ))"
-   if [ "$pgood" == $STATE_ON ]
-   then
-     host-reboot 6
-     sleep 15
-     host-reboot 2
-     exit 0;
-   else
-       echo "Host is Off"
-       exit 1;
-   fi
-}
-check-response
+
diff --git a/meta-facebook/recipes-phosphor/state/phosphor-state-manager/greatlakes/host-poweroff b/meta-facebook/recipes-phosphor/state/phosphor-state-manager/greatlakes/host-poweroff
index 70e8474..624d6d5 100644
--- a/meta-facebook/recipes-phosphor/state/phosphor-state-manager/greatlakes/host-poweroff
+++ b/meta-facebook/recipes-phosphor/state/phosphor-state-manager/greatlakes/host-poweroff
@@ -4,6 +4,7 @@
 # shellcheck source=meta-facebook/recipes-phosphor/state/phosphor-state-manager/greatlakes/power-cmd
 source /usr/libexec/phosphor-state-manager/power-cmd
 
+HOST_ID=$1
 SLOT_ID=$(( $1-1  << 2 ))
 
 # Host Power Off
@@ -11,30 +12,34 @@
 {
         for pwr_cmd in "${power_seq[@]}"
         do
-             result="$(busctl call $SERVICE $OBJECT_PATH $INTERFACE sendRequest yyyyay $SLOT_ID $NETFN $LUN $CMD $DATA_LEN 0x01 0x42 0x01 0x00 "$pwr_cmd")"
+             result="$(busctl call "$SERVICE" "$OBJECT_PATH" "$INTERFACE" sendRequest yyyyay "$SLOT_ID" "$NETFN" "$LUN" "$CMD" "$DATA_LEN" 0x01 0x42 0x01 0x00 "$pwr_cmd")"
              echo "$result"
-             if [ "$pwr_cmd" == $POWER_BTN_LOW ]
+             if [ "$pwr_cmd" == "$POWER_BTN_LOW" ]
              then
                      sleep 6
              fi
         done
-        echo "Host power off"
-        exit 0;
+
+        # Check host stattus after doing host power off
+        host_status=$(host-power-status $SLOT_ID)
+        if [ "$host_status" == "$STATE_OFF" ]
+        then
+             busctl set-property "$HOST_BUS_NAME""$HOST_ID" "$HOST_OBJ_PATH""$HOST_ID" "$HOST_INTF_NAME" "$HOST_PROPERTY_NAME" s "$HOST_OFF"
+             echo "Host power off"
+             exit 0;
+        else
+             busctl set-property "$HOST_BUS_NAME""$HOST_ID" "$HOST_OBJ_PATH""$HOST_ID" "$HOST_INTF_NAME" "$HOST_PROPERTY_NAME" s "$HOST_ON"
+             echo "Host power on"
+             exit 0;
+        fi
 }
 
 # Check Host Response and Status
-host-power-status()
-{
-   response="$(busctl call $SERVICE $OBJECT_PATH $INTERFACE sendRequest yyyyay $SLOT_ID 0x38 $LUN 0x3 0x03 0x15 0xa0 0x00)"
-
-   result=$(echo "$response" | cut -d" " -f $PWRGD_SYS_PWROK_INDEX)
-   res="$(( "$result" & 0x80  ))"
-   pgood="$(( "$res" >> 7 ))"
-   if [ "$pgood" == $STATE_ON ]
-   then
-         host-power-off
-   fi
-   echo "Power is already off"
-   exit 1;
-}
-host-power-status
+host_status=$(host-power-status $SLOT_ID)
+if [ "$host_status" == "$STATE_ON" ]
+then
+       host-power-off
+else
+       echo "Power is already off"
+       exit 1;
+fi
diff --git a/meta-facebook/recipes-phosphor/state/phosphor-state-manager/greatlakes/host-poweron b/meta-facebook/recipes-phosphor/state/phosphor-state-manager/greatlakes/host-poweron
index 008ff45..bd72979 100644
--- a/meta-facebook/recipes-phosphor/state/phosphor-state-manager/greatlakes/host-poweron
+++ b/meta-facebook/recipes-phosphor/state/phosphor-state-manager/greatlakes/host-poweron
@@ -4,36 +4,41 @@
 # shellcheck source=meta-facebook/recipes-phosphor/state/phosphor-state-manager/greatlakes/power-cmd
 source /usr/libexec/phosphor-state-manager/power-cmd
 
+HOST_ID=$1
 SLOT_ID=$(( $1-1  << 2 ))
 # Host Power On
 host-power-on()
 {
         for pwr_cmd in "${power_seq[@]}"
         do
-             result="$(busctl call $SERVICE $OBJECT_PATH $INTERFACE sendRequest yyyyay $SLOT_ID $NETFN $LUN $CMD $DATA_LEN 0x01 0x42 0x01 0x00 "$pwr_cmd")"
+             result="$(busctl call "$SERVICE" "$OBJECT_PATH" "$INTERFACE" sendRequest yyyyay "$SLOT_ID" "$NETFN" "$LUN" "$CMD" "$DATA_LEN" 0x01 0x42 0x01 0x00 "$pwr_cmd")"
              echo "$result"
-             if [ "$pwr_cmd" == $POWER_BTN_LOW ]
+             if [ "$pwr_cmd" == "$POWER_BTN_LOW" ]
              then
                      sleep 2
              fi
         done
-        echo "Host power on"
-        exit 0;
+
+        # Check host status after doing host power on
+        host_status=$(host-power-status $SLOT_ID)
+        if [ "$host_status" == "$STATE_ON" ]
+        then
+             busctl set-property "$HOST_BUS_NAME""$HOST_ID" "$HOST_OBJ_PATH""$HOST_ID" "$HOST_INTF_NAME" "$HOST_PROPERTY_NAME" s "$HOST_ON"
+             echo "Host power on"
+             exit 0;
+        else
+             busctl set-property "$HOST_BUS_NAME""$HOST_ID" "$HOST_OBJ_PATH""$HOST_ID" "$HOST_INTF_NAME" "$HOST_PROPERTY_NAME" s "$HOST_OFF"
+             echo "Host power off"
+             exit 0;
+        fi
 }
 
 # Check Host Response and Status
-host-power-status()
-{
-   response="$(busctl call $SERVICE $OBJECT_PATH $INTERFACE sendRequest yyyyay $SLOT_ID 0x38 $LUN 0x03 0x03 0x15 0xa0 0x00)"
-
-   result=$(echo "$response" | cut -d" " -f $PWRGD_SYS_PWROK_INDEX)
-   res="$(( "$result" & 0x80  ))"
-   pgood="$(( "$res" >> 7 ))"
-   if [ "$pgood" == $STATE_OFF ]
-   then
-       host-power-on
-   fi
+host_status=$(host-power-status $SLOT_ID)
+if [ "$host_status" == "$STATE_OFF" ]
+then
+   host-power-on
+else
    echo "Power is already on"
    exit 1;
-}
-host-power-status
+fi
diff --git a/meta-facebook/recipes-phosphor/state/phosphor-state-manager/greatlakes/host-powerreset b/meta-facebook/recipes-phosphor/state/phosphor-state-manager/greatlakes/host-powerreset
index e4bf1ff..88c27fb 100644
--- a/meta-facebook/recipes-phosphor/state/phosphor-state-manager/greatlakes/host-powerreset
+++ b/meta-facebook/recipes-phosphor/state/phosphor-state-manager/greatlakes/host-powerreset
@@ -4,6 +4,7 @@
 # shellcheck source=meta-facebook/recipes-phosphor/state/phosphor-state-manager/greatlakes/power-cmd
 source /usr/libexec/phosphor-state-manager/power-cmd
 
+HOST_ID=$1
 SLOT_ID=$(( $1-1  << 2 ))
 
 # Host Reboot
@@ -11,33 +12,35 @@
 {
     for reset_cmd in "${reset_seq[@]}"
     do
-        result="$(busctl call $SERVICE $OBJECT_PATH $INTERFACE sendRequest yyyyay $SLOT_ID $NETFN $LUN $CMD $DATA_LEN 0x01 0x42 0x01 0 "$reset_cmd")"
+        result="$(busctl call "$SERVICE" "$OBJECT_PATH" "$INTERFACE" sendRequest yyyyay "$SLOT_ID" "$NETFN" "$LUN" "$CMD" "$DATA_LEN" 0x01 0x42 0x01 0 "$reset_cmd")"
         echo "$result"
-        if [ "$reset_cmd" == $POWER_BTN_LOW ]
+        if [ "$reset_cmd" == "$POWER_BTN_LOW" ]
         then
             sleep "$1"
         fi
     done
-    echo "Host Power Reset"
-    exit 0
-}
 
-# Check Host Response and Status
-check-response()
-{
-    response="$(busctl call $SERVICE $OBJECT_PATH $INTERFACE sendRequest yyyyay $SLOT_ID 0x38 $LUN 0x3 0x03 0x15 0xa0 0x00)"
-
-    result=$(echo "$response" | cut -d" " -f $PWRGD_SYS_PWROK_INDEX)
-    res="$(( "$result" & 0x80  ))"
-    pgood="$(( "$res" >> 7 ))"
-    if [ "$pgood" == $STATE_ON ]
+    # Check host stattus after doing host power reset
+    host_status=$(host-power-status $SLOT_ID)
+    if [ "$host_status" == "$STATE_ON" ]
     then
-        host-reboot 1
+        busctl set-property "$HOST_BUS_NAME""$HOST_ID" "$HOST_OBJ_PATH""$HOST_ID" "$HOST_INTF_NAME" "$HOST_PROPERTY_NAME" s "$HOST_ON"
+        echo "Host power Reset"
         exit 0;
     else
-        echo "Host is Off"
-        exit 1;
+        busctl set-property "$HOST_BUS_NAME""$HOST_ID" "$HOST_OBJ_PATH""$HOST_ID" "$HOST_INTF_NAME" "$HOST_PROPERTY_NAME" s "$HOST_OFF"
+        echo "Host power off"
+        exit 0;
     fi
 }
 
-check-response
+# Check Host Response and Status
+host_status=$(host-power-status $SLOT_ID)
+if [ "$host_status" == "$STATE_ON" ]
+then
+    host-reboot 1
+    exit 0;
+else
+    echo "Host is Off"
+    exit 1;
+fi
diff --git a/meta-facebook/recipes-phosphor/state/phosphor-state-manager/greatlakes/power-cmd b/meta-facebook/recipes-phosphor/state/phosphor-state-manager/greatlakes/power-cmd
index 892cde5..ed75851 100644
--- a/meta-facebook/recipes-phosphor/state/phosphor-state-manager/greatlakes/power-cmd
+++ b/meta-facebook/recipes-phosphor/state/phosphor-state-manager/greatlakes/power-cmd
@@ -15,10 +15,11 @@
 export CMD=0x52
 export STATE_OFF=0
 export STATE_ON=1
-export BUS_NUM=12
+export STATE_UNKNOWN=-1
+export CPLD_BUS_NUM=12
+export CPLD_PWR_CTRL_ADDR=0xf
 export POW_ON_SLOT=0x01
 export POW_OFF_SLOT=0x00
-export CPLD_PWR_CTRL_ADDR=0xf
 export PWRGD_SYS_PWROK_INDEX=12
 export IANA="0x15 0xA0 0x0"
 export IANA_LEN=3
@@ -30,4 +31,36 @@
 export HOST_OBJ_PATH="/xyz/openbmc_project/state/host"
 export HOST_INTF_NAME="xyz.openbmc_project.State.Host"
 export HOST_PROPERTY_NAME="CurrentHostState"
+export CHASSIS_ON="xyz.openbmc_project.State.Chassis.PowerState.On"
+export CHASSIS_OFF="xyz.openbmc_project.State.Chassis.PowerState.Off"
+export HOST_ON="xyz.openbmc_project.State.Host.HostState.Running"
+export HOST_OFF="xyz.openbmc_project.State.Host.HostState.Off"
 
+host-power-status()
+{
+    SLOT_ID=$1
+
+    response="$(busctl call "$SERVICE" "$OBJECT_PATH" "$INTERFACE" sendRequest yyyyay "$SLOT_ID" 0x38 "$LUN" 0x03 0x03 0x15 0xa0 0x00)"
+    result=$(echo "$response" | cut -d" " -f "$PWRGD_SYS_PWROK_INDEX")
+    res="$(( "$result" & 0x80  ))"
+    status="$(( "$res" >> 7 ))"
+
+    echo "$status"
+    return 0
+}
+
+chassis-power-status()
+{
+    slot=$1
+    status=-1
+
+    if [ "$slot" -le 4  ]
+    then
+            response="$(i2cget -y "$CPLD_BUS_NUM" "$CPLD_PWR_CTRL_ADDR" 0x25)"
+            res="$(( "$response" >> slot ))"
+            status="$(( "$res" & 0x01  ))"
+    fi
+
+    echo "$status"
+    return 0
+}