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/ampere-hostctrl.bb b/meta-ampere/meta-common/recipes-ac01/host/ampere-hostctrl.bb
index f8e2954..4a44d1b 100644
--- a/meta-ampere/meta-common/recipes-ac01/host/ampere-hostctrl.bb
+++ b/meta-ampere/meta-common/recipes-ac01/host/ampere-hostctrl.bb
@@ -14,12 +14,9 @@
 S = "${WORKDIR}"
 
 SRC_URI = " \
-          file://ampere-host-reset.service \
           file://ampere_power_util.sh \
-          file://ampere-chassis-poweron.service \
-          file://ampere-host-reset-ack.service \
-          file://ampere-host-force-reset.service \
-          file://ampere-host-power-cycle.service \
+          file://ampere-host-force-reset@.service \
+          file://ampere-host-shutdown-ack@.service \
           "
 
 DEPENDS = "systemd virtual/obmc-gpio-monitor"
@@ -27,37 +24,16 @@
 
 SYSTEMD_PACKAGES = "${PN}"
 SYSTEMD_SERVICE_${PN} = " \
-        ampere-host-reset.service \
-        ampere-chassis-poweron.service \
-        ampere-host-reset-ack.service \
-        ampere-host-force-reset.service \
-        ampere-host-power-cycle.service \
+        ampere-host-force-reset@.service \
+        ampere-host-shutdown-ack@.service \
         "
-# host power control
-# Force the power cycle target to run the ampere power cycle
-HOST_REBOOT_SVC = "ampere-host-power-cycle.service"
-HOST_REBOOT_SVC_TGTFMT = "obmc-host-reboot@{0}.target"
-HOST_REBOOT_SVC_FMT = "../${HOST_REBOOT_SVC}:${HOST_REBOOT_SVC_TGTFMT}.requires/${HOST_REBOOT_SVC}"
-SYSTEMD_LINK_${PN} += "${@compose_list_zip(d, 'HOST_REBOOT_SVC_FMT', 'OBMC_HOST_INSTANCES')}"
-
-# overwrite the host reset to graceful reset
-HOST_WARM_REBOOT_SOFT_SVC = "ampere-host-reset.service"
-HOST_WARM_REBOOT_TGTFMT = "obmc-host-warm-reboot@{0}.target"
-HOST_WARM_REBOOT_SOFT_SVC_FMT = "../${HOST_WARM_REBOOT_SOFT_SVC}:${HOST_WARM_REBOOT_TGTFMT}.requires/${HOST_WARM_REBOOT_SOFT_SVC}"
-SYSTEMD_LINK_${PN} += "${@compose_list_zip(d, 'HOST_WARM_REBOOT_SOFT_SVC_FMT', 'OBMC_HOST_INSTANCES')}"
 
 # overwrite force reboot
-HOST_WARM_REBOOT_FORCE_TGT = "ampere-host-force-reset.service"
+HOST_WARM_REBOOT_FORCE_TGT = "ampere-host-force-reset@.service"
 HOST_WARM_REBOOT_FORCE_TGTFMT = "obmc-host-force-warm-reboot@{0}.target"
 HOST_WARM_REBOOT_FORCE_TARGET_FMT = "../${HOST_WARM_REBOOT_FORCE_TGT}:${HOST_WARM_REBOOT_FORCE_TGTFMT}.requires/${HOST_WARM_REBOOT_FORCE_TGT}"
 SYSTEMD_LINK_${PN} += "${@compose_list_zip(d, 'HOST_WARM_REBOOT_FORCE_TARGET_FMT', 'OBMC_HOST_INSTANCES')}"
 
-# chassis power control
-CHASSIS_POWERON_SVC = "ampere-chassis-poweron.service"
-CHASSIS_POWERON_TGTFMT = "obmc-chassis-poweron@{0}.target"
-CHASSIS_POWERON_FMT = "../${CHASSIS_POWERON_SVC}:${CHASSIS_POWERON_TGTFMT}.requires/${CHASSIS_POWERON_SVC}"
-SYSTEMD_LINK_${PN} += "${@compose_list_zip(d, 'CHASSIS_POWERON_FMT', 'OBMC_CHASSIS_INSTANCES')}"
-
 TMPL = "phosphor-gpio-monitor@.service"
 INSTFMT = "phosphor-gpio-monitor@{0}.service"
 TGT = "multi-user.target"
diff --git a/meta-ampere/meta-common/recipes-ac01/host/ampere-hostctrl/ampere-chassis-poweron.service b/meta-ampere/meta-common/recipes-ac01/host/ampere-hostctrl/ampere-chassis-poweron.service
deleted file mode 100644
index 76ceb88..0000000
--- a/meta-ampere/meta-common/recipes-ac01/host/ampere-hostctrl/ampere-chassis-poweron.service
+++ /dev/null
@@ -1,9 +0,0 @@
-[Unit]
-Description=Ampere Computing LLC Power ON Chassis
-Conflicts=obmc-chassis-poweroff@0.target
-
-[Service]
-RemainAfterExit=yes
-Type=oneshot
-ExecStart=/usr/bin/env ampere_power_util.sh mb on
-SyslogIdentifier=ampere_power_util.sh
diff --git a/meta-ampere/meta-common/recipes-ac01/host/ampere-hostctrl/ampere-host-force-reset.service b/meta-ampere/meta-common/recipes-ac01/host/ampere-hostctrl/ampere-host-force-reset.service
deleted file mode 100644
index 8a40baf..0000000
--- a/meta-ampere/meta-common/recipes-ac01/host/ampere-hostctrl/ampere-host-force-reset.service
+++ /dev/null
@@ -1,11 +0,0 @@
-[Unit]
-Description=Ampere Computing LLC force reset host
-Conflicts=obmc-chassis-poweroff@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-ampere/meta-common/recipes-ac01/host/ampere-hostctrl/ampere-host-force-reset@.service b/meta-ampere/meta-common/recipes-ac01/host/ampere-hostctrl/ampere-host-force-reset@.service
new file mode 100644
index 0000000..6e03c8b
--- /dev/null
+++ b/meta-ampere/meta-common/recipes-ac01/host/ampere-hostctrl/ampere-host-force-reset@.service
@@ -0,0 +1,14 @@
+[Unit]
+Description=Ampere Computing LLC force reset host%i
+Wants=phosphor-reboot-host@%i.service
+Conflicts=obmc-chassis-poweroff@%i.target
+OnFailure=obmc-chassis-powerreset@%i.target
+
+[Service]
+RemainAfterExit=no
+Type=oneshot
+ExecStart=/bin/sh -c "sleep 3 && /usr/bin/env ampere_power_util.sh mb force_reset"
+SyslogIdentifier=ampere_power_util.sh
+
+[Install]
+WantedBy=obmc-host-force-warm-reboot@%i.target
\ No newline at end of file
diff --git a/meta-ampere/meta-common/recipes-ac01/host/ampere-hostctrl/ampere-host-power-cycle.service b/meta-ampere/meta-common/recipes-ac01/host/ampere-hostctrl/ampere-host-power-cycle.service
deleted file mode 100644
index 2fb03ef..0000000
--- a/meta-ampere/meta-common/recipes-ac01/host/ampere-hostctrl/ampere-host-power-cycle.service
+++ /dev/null
@@ -1,10 +0,0 @@
-[Unit]
-Description=Ampere Computing LLC power cycle service
-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 cycle
-SyslogIdentifier=ampere_power_util.sh
diff --git a/meta-ampere/meta-common/recipes-ac01/host/ampere-hostctrl/ampere-host-reset-ack.service b/meta-ampere/meta-common/recipes-ac01/host/ampere-hostctrl/ampere-host-reset-ack.service
deleted file mode 100644
index 504186e..0000000
--- a/meta-ampere/meta-common/recipes-ac01/host/ampere-hostctrl/ampere-host-reset-ack.service
+++ /dev/null
@@ -1,10 +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-ampere/meta-common/recipes-ac01/host/ampere-hostctrl/ampere-host-reset.service b/meta-ampere/meta-common/recipes-ac01/host/ampere-hostctrl/ampere-host-reset.service
deleted file mode 100644
index ac7618a..0000000
--- a/meta-ampere/meta-common/recipes-ac01/host/ampere-hostctrl/ampere-host-reset.service
+++ /dev/null
@@ -1,9 +0,0 @@
-[Unit]
-Description=Ampere Computing LLC warm reset host
-Conflicts=obmc-chassis-poweroff@0.target
-
-[Service]
-RemainAfterExit=no
-Type=oneshot
-ExecStart=/usr/bin/env ampere_power_util.sh mb graceful_reset
-SyslogIdentifier=ampere_power_util.sh
diff --git a/meta-ampere/meta-common/recipes-ac01/host/ampere-hostctrl/ampere-host-shutdown-ack@.service b/meta-ampere/meta-common/recipes-ac01/host/ampere-hostctrl/ampere-host-shutdown-ack@.service
new file mode 100644
index 0000000..6588191
--- /dev/null
+++ b/meta-ampere/meta-common/recipes-ac01/host/ampere-hostctrl/ampere-host-shutdown-ack@.service
@@ -0,0 +1,8 @@
+[Unit]
+Description=Ampere Computing LLC host shutdown ACK
+Conflicts=obmc-chassis-poweron@0.target
+
+[Service]
+Type=oneshot
+ExecStart=/usr/bin/env ampere_power_util.sh mb shutdown_ack
+SyslogIdentifier=ampere_host_shutdown_ack
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
diff --git a/meta-ampere/meta-common/recipes-ac01/host/files/obmc/gpio/reboot_ack b/meta-ampere/meta-common/recipes-ac01/host/files/obmc/gpio/reboot_ack
index 0436d32..a8ad98e 100644
--- a/meta-ampere/meta-common/recipes-ac01/host/files/obmc/gpio/reboot_ack
+++ b/meta-ampere/meta-common/recipes-ac01/host/files/obmc/gpio/reboot_ack
@@ -1,4 +1,4 @@
 DEVPATH=/dev/input/event0
 KEY=75
 POLARITY=1
-TARGET=ampere-host-reset-ack.service
+TARGET=obmc-host-force-warm-reboot@0.target
diff --git a/meta-ampere/meta-common/recipes-ac01/host/files/obmc/gpio/shutdown_ack b/meta-ampere/meta-common/recipes-ac01/host/files/obmc/gpio/shutdown_ack
index a0e857f..18cc872 100644
--- a/meta-ampere/meta-common/recipes-ac01/host/files/obmc/gpio/shutdown_ack
+++ b/meta-ampere/meta-common/recipes-ac01/host/files/obmc/gpio/shutdown_ack
@@ -1,4 +1,4 @@
 DEVPATH=/dev/input/event0
 KEY=50
 POLARITY=1
-TARGET=ampere-chassis-poweroff.service
+TARGET=ampere-host-shutdown-ack@0.service
diff --git a/meta-ampere/meta-common/recipes-phosphor/state/phosphor-state-manager_%.bbappend b/meta-ampere/meta-common/recipes-phosphor/state/phosphor-state-manager_%.bbappend
index 78683f8..dcf8cac 100644
--- a/meta-ampere/meta-common/recipes-phosphor/state/phosphor-state-manager_%.bbappend
+++ b/meta-ampere/meta-common/recipes-phosphor/state/phosphor-state-manager_%.bbappend
@@ -3,14 +3,3 @@
 DEPS_CFG = "resetreason.conf"
 DEPS_TGT = "phosphor-discover-system-state@.service"
 SYSTEMD_OVERRIDE_${PN}-discover_append = "${DEPS_CFG}:${DEPS_TGT}.d/${DEPS_CFG}"
-
-pkg_postinst_${PN}-obmc-targets_append() {
-    rm "$D$systemd_system_unitdir/obmc-host-warm-reboot@0.target.requires/xyz.openbmc_project.Ipmi.Internal.SoftPowerOff.service"
-    rm "$D$systemd_system_unitdir/obmc-host-warm-reboot@0.target.requires/obmc-host-force-warm-reboot@0.target"
-
-    rm "$D$systemd_system_unitdir/obmc-host-reboot@0.target.requires/phosphor-reboot-host@0.service"
-    rm "$D$systemd_system_unitdir/obmc-host-reboot@0.target.requires/obmc-host-shutdown@0.target"
-
-    rm "$D$systemd_system_unitdir/obmc-host-force-warm-reboot@0.target.requires/obmc-host-stop@0.target"
-    rm "$D$systemd_system_unitdir/obmc-host-force-warm-reboot@0.target.requires/phosphor-reboot-host@0.service"
-}