meta-ampere: power control: refactor host power control

Refactor the Mt.Jade Host power control operations to support Altra
CPU following the Altra SoC BMC Interface specification which still
satisfying the OpenBMC power control framework.

Tested: check the below cases and ensure Host state report correctly:
   1. Run "reboot" from Host Linux. Check the Host is graceful
      shutdown, then SYS_RESET is asserted and the Host is booted.
   2. Run "shutdown" from Host Linux. Check the Host is graceful
      shutdown then the chassis power is turned OFF.
   3. Run "ipmitool chassis power soft". Check the Host is graceful
      shutdown
   4. Execute Redfish GracefulShutdown. Check the Host is graceful
      shutdown then the chassis power is turned OFF.
   5. Execute Redfish GracefulRestart. Check the Host is graceful
      shutdown then turned ON.
   6. Check other IPMI chassis power and Redfish Reset actions work.

Signed-off-by: ThuBaNguyen <thu@os.amperecomputing.com>
Change-Id: I3a9716f37d7e80bdc1a1d0422e561c8fc51ad040
diff --git a/meta-ampere/meta-common/recipes-ac01/host/files/ampere_power_util.sh b/meta-ampere/meta-common/recipes-ac01/host/files/ampere_power_util.sh
index 9a8b06c..aa74210 100644
--- a/meta-ampere/meta-common/recipes-ac01/host/files/ampere_power_util.sh
+++ b/meta-ampere/meta-common/recipes-ac01/host/files/ampere_power_util.sh
@@ -4,15 +4,6 @@
   echo "usage: power-util mb [on|status|cycle|reset|graceful_reset|force_reset|soft_off]";
 }
 
-power_off() {
-  echo "power_off"
-}
-
-power_on() {
-  echo "Powering on Server $2"
-  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)
   if [ "$st" == "On\"" ]; then
@@ -22,21 +13,13 @@
   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
-}
-
-timestamp() {
-  date +"%s" # current time
-}
-
 shutdown_ack() {
   if [ -f "/run/openbmc/host@0-softpoweroff" ]; then
     echo "Receive shutdown ACK triggered after softportoff the host."
     touch /run/openbmc/host@0-softpoweroff-shutdown-ack
   else
     echo "Receive shutdown ACK triggered"
+<<<<<<< HEAD
   fi
 }
 
@@ -50,6 +33,10 @@
     sleep 1
     gpioset -l 0 49=0
     sleep 30s
+=======
+    sleep 3
+    systemctl start obmc-chassis-poweroff@0.target
+>>>>>>> 397e033ef... meta-ampere: power control: refactor host power control
   fi
 }
 
@@ -81,6 +68,26 @@
 }
 
 force_reset() {
+  if [ -f "/run/openbmc/host@0-softpoweroff" ]; then
+    # In graceful host reset, after trigger os shutdown,
+    # the phosphor-state-manager will call force-warm-reset
+    # in this case the force_reset should wait for shutdown_ack from host
+    cnt=30
+    while [ $cnt -gt 0 ];
+    do
+      if [ -f "/run/openbmc/host@0-softpoweroff-shutdown-ack" ]; then
+        break
+      fi
+      echo "Waiting for shutdown-ack count down $cnt"
+      sleep 1
+      cnt=$((cnt - 1))
+    done
+    # The host OS is failed to shutdown
+    if [ $cnt == 0 ]; then
+      echo "Shutdown-ack time out after 30s."
+      exit 0
+    fi
+  fi
   echo "Triggering sysreset pin"
   gpioset -l 0 91=1
   sleep 1
@@ -100,30 +107,23 @@
   exit 0;
 fi
 
-if [ $2 = "on" ]; then
-  if [ $(power_status) == "off" ]; then
-    power_on
+# check if power guard enabled
+dir="/run/systemd/system/"
+file="reboot-guard.conf"
+units=("reboot" "poweroff" "halt")
+for unit in "${units[@]}"; do
+  if [ -f ${dir}${unit}.target.d/${file} ]; then
+    echo "PowerGuard enabled, cannot do power control, exit!!!"
+    exit -1
   fi
-elif [ $2 == "cycle" ]; then
-  if [ $(power_status) == "on" ]; then
-    echo "Powering off 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"
-  fi
-elif [ $2 == "graceful_reset" ]; then
+done
+
+if [ ! -d "/run/openbmc/" ]; then
   mkdir -p "/run/openbmc/"
-  touch "/run/openbmc/host@0-graceful-reset"
-  graceful_shutdown
-  sleep 20s
+fi
+
+if [ $2 == "shutdown_ack" ]; then
+  shutdown_ack
 elif [ $2 == "status" ]; then
   power_status
 elif [ $2 == "force_reset" ]; then