meta-fii/meta-kudo: Fix power control states and timing

Summary of changes:
 1. Adjust start time of xyz.openbmc_project.State.Chassis.service
    to fix initial chassis state
 2. Add shutdown ack service
 3. Remove reset ack service
 4. Remove heartbeat mode
 5. Remove sysreset pin control from power_control.exe
 6. Remove all phosphor-fan services that caused power off during
    a system reset
 7. Adjustments to ampere_power_util.sh (See below)

ampere_power_util.sh:
 1. Use GPIO instead of dbus for host state
 2. Wait 3 seconds before checking power status for power on
 3. Wait 6 seconds before checking power status for power off
 4. Have force_off act GPIO while power_off sets Chassis state
 5. Graceful shutdown acts GPIO
 6. Check host status before issuing system reset
 7. Set Watchdog Expire Action to None before power off and reset

Signed-off-by: Charles Boyer <Charles.Boyer@fii-usa.com>
Change-Id: Ie2720ee60d489c4160165c6827a78a400ae4d30b
diff --git a/meta-fii/meta-kudo/recipes-kudo/host/ampere-hostctrl.bb b/meta-fii/meta-kudo/recipes-kudo/host/ampere-hostctrl.bb
index f7daa94..77ad9d5 100644
--- a/meta-fii/meta-kudo/recipes-kudo/host/ampere-hostctrl.bb
+++ b/meta-fii/meta-kudo/recipes-kudo/host/ampere-hostctrl.bb
@@ -14,7 +14,7 @@
           file://ampere_power_util.sh \
           file://ampere-chassis-poweroff.service \
           file://ampere-chassis-poweron.service \
-          file://ampere-host-reset-ack.service \
+          file://ampere-host-shutdown-ack.service \
           file://ampere-host-force-reset.service \
           file://ampere-host-power-cycle.service \
           "
@@ -28,7 +28,7 @@
         ampere-host-reset.service \
         ampere-chassis-poweroff.service \
         ampere-chassis-poweron.service \
-        ampere-host-reset-ack.service \
+        ampere-host-shutdown-ack.service \
         ampere-host-force-reset.service \
         ampere-host-power-cycle.service \
         "
diff --git a/meta-fii/meta-kudo/recipes-kudo/host/ampere-hostctrl/ampere-host-reset-ack.service b/meta-fii/meta-kudo/recipes-kudo/host/ampere-hostctrl/ampere-host-reset-ack.service
deleted file mode 100644
index a533879..0000000
--- a/meta-fii/meta-kudo/recipes-kudo/host/ampere-hostctrl/ampere-host-reset-ack.service
+++ /dev/null
@@ -1,11 +0,0 @@
-[Unit]
-Description=Ampere Computing LLC triggering reset ACK
-Conflicts=obmc-host-start@0.target
-OnFailure=obmc-chassis-powerreset@0.target
-
-[Service]
-RemainAfterExit=no
-Type=oneshot
-ExecStart=/usr/bin/env ampere_power_util.sh mb force_reset
-SyslogIdentifier=ampere_power_util.sh
-
diff --git a/meta-fii/meta-kudo/recipes-kudo/host/ampere-hostctrl/ampere-host-shutdown-ack.service b/meta-fii/meta-kudo/recipes-kudo/host/ampere-hostctrl/ampere-host-shutdown-ack.service
new file mode 100644
index 0000000..fe05915
--- /dev/null
+++ b/meta-fii/meta-kudo/recipes-kudo/host/ampere-hostctrl/ampere-host-shutdown-ack.service
@@ -0,0 +1,12 @@
+[Unit]
+Description=Ampere Computing LLC host shutdown ACK
+Requires=op-wait-power-off@%i.service
+Before=op-wait-power-off@%i.service
+Conflicts=obmc-chassis-poweron@0.target
+
+[Service]
+RemainAfterExit=no
+Type=oneshot
+ExecStart=/usr/bin/env ampere_power_util.sh mb shutdown_ack
+SyslogIdentifier=ampere_power_util.sh
+
diff --git a/meta-fii/meta-kudo/recipes-kudo/host/files/ampere_power_util.sh b/meta-fii/meta-kudo/recipes-kudo/host/files/ampere_power_util.sh
index 420abfb..8d58a08 100644
--- a/meta-fii/meta-kudo/recipes-kudo/host/files/ampere_power_util.sh
+++ b/meta-fii/meta-kudo/recipes-kudo/host/files/ampere_power_util.sh
@@ -1,24 +1,36 @@
 #!/bin/bash
+
+source /usr/sbin/kudo-lib.sh
+
 # Usage of this utility
 function usage() {
-  echo "usage: power-util mb [on|off|status|cycle|reset|graceful_shutdown|graceful_reset|force_reset]";
+  echo "usage: power-util mb [on|off|graceful_shutdown|force_reset|shutdown_ack]";
+}
+
+force_off() {
+  echo "Powering down Server"
+
+  set_gpio_ctrl 203 out 1
+  sleep 6
+  set_gpio_ctrl 203 out 0
 }
 
 power_off() {
-  echo "Shutting down Server $2"
+  busctl set-property xyz.openbmc_project.Watchdog /xyz/openbmc_project/watchdog/host0 xyz.openbmc_project.State.Watchdog ExpireAction s xyz.openbmc_project.State.Watchdog.Action.None
   busctl set-property xyz.openbmc_project.State.Chassis /xyz/openbmc_project/state/chassis0 xyz.openbmc_project.State.Chassis RequestedPowerTransition s xyz.openbmc_project.State.Chassis.Transition.Off
 }
 
 power_on() {
-  echo "Powering on Server $2"
-  gpioset 6 11=1
+  echo "Powering on Server"
+
+  set_gpio_ctrl 203 out 1
   sleep 1
-  gpioset 6 11=0
+  set_gpio_ctrl 203 out 0
   busctl set-property xyz.openbmc_project.State.Chassis /xyz/openbmc_project/state/chassis0 xyz.openbmc_project.State.Chassis RequestedPowerTransition s xyz.openbmc_project.State.Chassis.Transition.On
 }
 
 power_status() {
-  st=$(busctl get-property xyz.openbmc_project.State.Chassis /xyz/openbmc_project/state/chassis0 xyz.openbmc_project.State.Chassis CurrentPowerState | cut -d"." -f6)
+  st=$(busctl get-property xyz.openbmc_project.State.Chassis /xyz/openbmc_project/state/chassis0 xyz.openbmc_project.State.Chassis CurrentPowerState | cut -d "." -f6)
   if [ "$st" == "On\"" ]; then
   echo "on"
   else
@@ -26,33 +38,53 @@
   fi
 }
 
-power_reset() {
-  echo "Reset on server $2"
-  busctl set-property xyz.openbmc_project.State.Host /xyz/openbmc_project/state/host0 xyz.openbmc_project.State.Host RequestedHostTransition s xyz.openbmc_project.State.Host.Transition.Reboot
+host_status() {
+  BOOT_OK=$(get_gpio_ctrl 194)
+  S5_N=$(get_gpio_ctrl 204)
+  if [ $S5_N == 1 ] || [ $BOOT_OK == 1 ]; then
+    echo "on"
+  else
+    echo "off"
+  fi
+}
+
+timestamp() {
+  date +"%s" # current time
 }
 
 graceful_shutdown() {
   if [ -f "/run/openbmc/host@0-request" ]; then
-    echo "shutdown host immediately"
-    gpioset 6 11=1
-    sleep 6
-    gpioset 6 11=0
+    echo "Shutdown host immediately"
     power_off
   else
     echo "Triggering graceful shutdown"
-    gpioset -l 2 6=0
+    mkdir /run/openbmc
+    echo "$(timestamp)" > "/run/openbmc/host@0-shutdown-req-time"
+    set_gpio_ctrl 70 out 0
     sleep 3
-    gpioset -l 2 6=1
-    sleep 30s
-    power_off
+    set_gpio_ctrl 70 out 1
   fi
 }
 
 force_reset() {
-  echo "Triggering sysreset pin"
-  gpioset -l 2 1=0
-  sleep 1
-  gpioset -l 2 1=1
+  if [ $(host_status) == "on" ]; then
+    echo "Triggering sysreset pin"
+    busctl set-property xyz.openbmc_project.Watchdog /xyz/openbmc_project/watchdog/host0 xyz.openbmc_project.State.Watchdog ExpireAction s xyz.openbmc_project.State.Watchdog.Action.None
+    set_gpio_ctrl 65 out 0
+    sleep 1
+    set_gpio_ctrl 65 out 1
+  else
+    echo "Host is off, cannot reset."
+  fi
+}
+
+shutdown_ack() {
+  echo "Receive shutdown ACK triggered"
+  power_off
+
+  if [ -f "/run/openbmc/host@0-shutdown-req-time" ]; then
+    rm -rf "/run/openbmc/host@0-shutdown-req-time"
+  fi
 }
 
 if [ $# -lt 2 ]; then
@@ -69,49 +101,27 @@
 fi
 
 if [ $2 = "on" ]; then
+  sleep 3
   if [ $(power_status) == "off" ]; then
     power_on
   fi
 elif [ $2 = "off" ]; then
   if [ $(power_status) == "on" ]; then
     power_off
-  fi
-  # If any request of graceful reset, need to power on
-  if [ -f "/run/openbmc/host@0-graceful-reset" ]; then
-    sleep 20s
-    power_on
-    rm -f "/run/openbmc/host@0-graceful-reset"
-  fi
-elif [ $2 == "cycle" ]; then
-  if [ $(power_status) == "on" ]; then
-    echo "Power cycling server"
-    power_off
-    sleep 20s
-    power_on
-  else
-    echo "Host is already off, do nothing"
-  fi
-elif [ $2 == "reset" ]; then
-  if [ $(power_status) == "on" ]; then
-    power_reset
-  else
-    echo "ERROR: Server not powered on"
+    sleep 6
+    if [ $(host_status) == "on" ]; then
+      force_off
+    fi
   fi
 elif [[ $2 == "graceful_shutdown" ]]; then
   graceful_shutdown
-elif [ $2 == "graceful_reset" ]; then
-  mkdir -p "/run/openbmc/"
-  touch "/run/openbmc/host@0-graceful-reset"
-  graceful_shutdown
-  sleep 20s
-elif [ $2 == "status" ]; then
-  power_status
 elif [ $2 == "force_reset" ]; then
   force_reset
+elif [ $2 == "shutdown_ack" ]; then
+  shutdown_ack
 else
   echo "Invalid parameter2=$2"
   usage;
 fi
 
 exit 0;
-