meta-ampere: power control: refactor power soft/off functions

The current ampere-hostctl package removes the power off/on services
of phosphor-state-manager and uses Ampere services. This solution is
not correct. Ampere platform should use power control functions of
phosphor-state-manager. And only appends or overides the default
services by Ampere's services if need.

By default, to handle power soft action, phosphor-state-manager will
trigger obmc-host-shutdown@0.target. This target then call
xyz.openbmc_project.Ipmi.Internal.SoftPowerOff.service to request OS
shutdown. When the host OS shutdown is already done, the target will
trigger obmc-chassis-poweroff@0.target to turn off the chassis. The
default xyz.openbmc_project.Ipmi.Internal.SoftPowerOff.service uses
Ipmi inband to communicate with the host. It is different with Ampere
Mt.Jade platform. We use the GPIO pin SHD_REQ to request shutdown the
host OS. The host will trigger SHD_ACK when shutdown is done. So
*.Ipmi.Internal.SoftPowerOff.service will be overide by Ampere service
named ampere.*.Ipmi.Internal.SoftPowerOff.service. This service will
trigger SHD_REQ pin and wait for SHD_ACK before start
obmc-chassis-poweroff@0.target.

This commit removes ampere-chassis-poweroff, ampere-host-shutdown
serives which handle power off/soft actions and restore
phosphor-state-manager's services. It also supports
ampere.xyz.openbmc_project.Ipmi.Internal.SoftPowerOff.service which
will overide xyz.openbmc_project.Ipmi.Internal.SoftPowerOff.service.

Tested:
    1. Create "power soft" actions use ipmitool, redfish and BMC web.
    2. Make sure the power order will be shutdown the host then power
       of the chassis.
    3. Create "power off" actions use ipmitool, redfish and BMC web.
    4. Make sure the power action will be power off the chassis.

Signed-off-by: ThuBaNguyen <thu@os.amperecomputing.com>
Change-Id: Ibc0dc8c62408e8282520c9b70e41ab75c10137f6
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 8bab9a3..9a8b06c 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
@@ -1,12 +1,11 @@
 #!/bin/bash
 # 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|status|cycle|reset|graceful_reset|force_reset|soft_off]";
 }
 
 power_off() {
-  echo "Shutting down 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.Off
+  echo "power_off"
 }
 
 power_on() {
@@ -28,6 +27,19 @@
   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"
+  fi
+}
+
 graceful_shutdown() {
   if [ -f "/run/openbmc/host@0-request" ]; then
     echo "shutdown host immediately"
@@ -41,6 +53,33 @@
   fi
 }
 
+soft_off() {
+  # Trigger shutdown_req
+  touch /run/openbmc/host@0-softpoweroff
+  gpioset -l 0 49=1
+  sleep 1s
+  gpioset -l 0 49=0
+
+  # Wait for shutdown_ack from the host in 30 seconds
+  cnt=30
+  while [ $cnt -gt 0 ];
+  do
+    # Wait for SHUTDOWN_ACK and create the host@0-softpoweroff-shutdown-ack
+    if [ -f "/run/openbmc/host@0-softpoweroff-shutdown-ack" ]; then
+      break
+    fi
+    sleep 1
+    cnt=$((cnt - 1))
+  done
+  # Softpoweroff is successed
+  sleep 2
+  rm -rf /run/openbmc/host@0-softpoweroff
+  if [ -f "/run/openbmc/host@0-softpoweroff-shutdown-ack" ]; then
+    rm -rf /run/openbmc/host@0-softpoweroff-shutdown-ack
+  fi
+  echo 0
+}
+
 force_reset() {
   echo "Triggering sysreset pin"
   gpioset -l 0 91=1
@@ -65,16 +104,6 @@
   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 "Powering off server"
@@ -90,8 +119,6 @@
   else
     echo "ERROR: Server not powered on"
   fi
-elif [[ $2 == "graceful_shutdown" ]]; then
-  graceful_shutdown
 elif [ $2 == "graceful_reset" ]; then
   mkdir -p "/run/openbmc/"
   touch "/run/openbmc/host@0-graceful-reset"
@@ -101,6 +128,14 @@
   power_status
 elif [ $2 == "force_reset" ]; then
   force_reset
+elif [ $2 == "soft_off" ]; then
+  ret=$(soft_off)
+  if [ $ret == 0 ]; then
+    echo "The host is already softoff"
+  else
+    echo "Failed to softoff the host"
+  fi
+  exit $ret;
 else
   echo "Invalid parameter2=$2"
   usage;