meta-bletchley: power-ctrl: add host cycle and reset actions

Add host power cycle and power reset actions

Cycle Action includes following steps:
 - 1. AC Off
 - 2. AC On
 - 3. Power On

Reset Action includes following steps:
 - 1. Power Off
 - 2. Power On

Signed-off-by: Potin Lai <potin.lai@quantatw.com>
Change-Id: I17341ec30776413d428f35498d70534901d250b1
diff --git a/meta-facebook/meta-bletchley/recipes-bletchley/motor-ctrl/files/host-cycle@.service b/meta-facebook/meta-bletchley/recipes-bletchley/motor-ctrl/files/host-cycle@.service
new file mode 100644
index 0000000..2fb7d9d
--- /dev/null
+++ b/meta-facebook/meta-bletchley/recipes-bletchley/motor-ctrl/files/host-cycle@.service
@@ -0,0 +1,9 @@
+[Unit]
+Description=Sled%i Host Power Cycle
+Requires=motor-init-calibration@%i.service
+After=motor-init-calibration@%i.service
+
+[Service]
+Type=oneshot
+ExecStart=/usr/sbin/power-ctrl sled%i cycle
+SyslogIdentifier=power-ctrl
diff --git a/meta-facebook/meta-bletchley/recipes-bletchley/motor-ctrl/files/host-reset@.service b/meta-facebook/meta-bletchley/recipes-bletchley/motor-ctrl/files/host-reset@.service
new file mode 100644
index 0000000..a8b6a08
--- /dev/null
+++ b/meta-facebook/meta-bletchley/recipes-bletchley/motor-ctrl/files/host-reset@.service
@@ -0,0 +1,9 @@
+[Unit]
+Description=Sled%i Host Power Reset
+Requires=motor-init-calibration@%i.service
+After=motor-init-calibration@%i.service
+
+[Service]
+Type=oneshot
+ExecStart=/usr/sbin/power-ctrl sled%i reset
+SyslogIdentifier=power-ctrl
diff --git a/meta-facebook/meta-bletchley/recipes-bletchley/motor-ctrl/files/power-ctrl b/meta-facebook/meta-bletchley/recipes-bletchley/motor-ctrl/files/power-ctrl
index 25cab0b..2c9dfe1 100755
--- a/meta-facebook/meta-bletchley/recipes-bletchley/motor-ctrl/files/power-ctrl
+++ b/meta-facebook/meta-bletchley/recipes-bletchley/motor-ctrl/files/power-ctrl
@@ -34,6 +34,8 @@
 ACTION_OFF="off"
 ACTION_DFU="dfu"
 ACTION_RECOVERY="recovery"
+ACTION_CYCLE="cycle"
+ACTION_RESET="reset"
 ACTION_AC_ON="ac-on"
 ACTION_AC_OFF="ac-off"
 ACTION_STATUS="status"
@@ -46,6 +48,8 @@
     $ACTION_STATUS
     $ACTION_DFU
     $ACTION_RECOVERY
+    $ACTION_CYCLE
+    $ACTION_RESET
 "
 
 function is_valid_sled_action()
@@ -319,6 +323,39 @@
     return $?
 }
 
+function do_action_reset()
+{
+    # 1. Power off
+    # 2. Power on
+
+    local SLED_NUM=$1
+    local CUR_ST=$2
+
+    if [ "$CUR_ST" != "$HOST_ST_OFF" ]; then
+        do_action_off "$SLED_NUM"
+    else
+        echo "sled${SLED_NUM}: already powered off"
+    fi
+
+    sleep 3
+    do_action_on "$SLED_NUM"
+}
+
+function do_action_cycle()
+{
+    # 1. AC off
+    # 2. AC on
+    # 3. Power on
+
+    local SLED_NUM=$1
+
+    do_action_ac_off "$SLED_NUM"
+    sleep 3
+    do_action_ac_on "$SLED_NUM"
+    sleep 3
+    do_action_on "$SLED_NUM"
+}
+
 function do_action_ac_on()
 {
     local SLED_NUM=$1
@@ -404,6 +441,12 @@
         "$ACTION_AC_OFF")
             do_action_ac_off "$SLED_NUM"
             ;;
+        "$ACTION_RESET")
+            do_action_reset "$SLED_NUM" "$HOST_ST_ON"
+            ;;
+        "$ACTION_CYCLE")
+            do_action_cycle "$SLED_NUM"
+            ;;
         *)
             echo "Invalid action ($ACTION) for current host state (On)"
             return 1
@@ -426,6 +469,12 @@
         "$ACTION_AC_OFF")
             do_action_ac_off "$SLED_NUM"
             ;;
+        "$ACTION_RESET")
+            do_action_reset "$SLED_NUM" "$HOST_ST_ON"
+            ;;
+        "$ACTION_CYCLE")
+            do_action_cycle "$SLED_NUM"
+            ;;
         *)
             echo "Invalid action ($ACTION) for current host state (Sleep)"
             return 1
@@ -448,6 +497,12 @@
         "$ACTION_AC_OFF")
             do_action_ac_off "$SLED_NUM"
             ;;
+        "$ACTION_RESET")
+            do_action_reset "$SLED_NUM" "$HOST_ST_ON"
+            ;;
+        "$ACTION_CYCLE")
+            do_action_cycle "$SLED_NUM"
+            ;;
         *)
             echo "Invalid action ($ACTION) for current host state (Off)"
             return 1
@@ -471,6 +526,9 @@
             echo "sled${SLED_NUM}: already ac off"
             return 1
             ;;
+        "$ACTION_CYCLE")
+            do_action_reset "$SLED_NUM"
+            ;;
         *)
             echo "Invalid action ($ACTION) for current host state (AC Off)"
             return 1
@@ -487,6 +545,9 @@
         "$ACTION_AC_OFF")
             do_action_ac_off "$SLED_NUM"
             ;;
+        "$ACTION_CYCLE")
+            do_action_cycle "$SLED_NUM"
+            ;;
         *)
             echo "sled${SLED_NUM}: already ac on"
             return 1
@@ -506,6 +567,12 @@
         "$ACTION_AC_OFF")
             do_action_ac_off "$SLED_NUM"
             ;;
+        "$ACTION_RESET")
+            do_action_reset "$SLED_NUM" "$HOST_ST_ON"
+            ;;
+        "$ACTION_CYCLE")
+            do_action_cycle "$SLED_NUM"
+            ;;
         *)
             echo "Invalid action ($ACTION) for current host state (Recovery)"
             return 1
@@ -522,6 +589,9 @@
         "$ACTION_AC_OFF")
             do_action_ac_off "$SLED_NUM"
             ;;
+        "$ACTION_CYCLE")
+            do_action_cycle "$SLED_NUM"
+            ;;
         *)
             echo "Invalid action ($ACTION) for current host state (DFU)"
             return 1
diff --git a/meta-facebook/meta-bletchley/recipes-bletchley/motor-ctrl/motor-ctrl_0.1.bb b/meta-facebook/meta-bletchley/recipes-bletchley/motor-ctrl/motor-ctrl_0.1.bb
index 9c2c68c..feab224 100644
--- a/meta-facebook/meta-bletchley/recipes-bletchley/motor-ctrl/motor-ctrl_0.1.bb
+++ b/meta-facebook/meta-bletchley/recipes-bletchley/motor-ctrl/motor-ctrl_0.1.bb
@@ -31,6 +31,8 @@
 MOTOR_INIT_INSTFMT="../motor-init-calibration@.service:${TGT}.wants/motor-init-calibration@{0}.service"
 PWR_ON_INSTFMT="../host-poweron@.service:obmc-host-startmin@{0}.target.wants/host-poweron@{0}.service"
 PWR_OFF_INSTFMT="../host-poweroff@.service:obmc-host-shutdown@{0}.target.wants/host-poweroff@{0}.service"
+PWR_RESET_INSTFMT="host-reset@.service:host-reset@{0}.service"
+PWR_CYCLE_INSTFMT="host-cycle@.service:host-cycle@{0}.service"
 AC_ON_INSTFMT="../host-ac-on@.service:${TGT}.wants/host-ac-on@{0}.service"
 AC_OFF_INSTFMT="host-ac-off@.service:host-ac-off@{0}.service"
 
@@ -43,6 +45,12 @@
 SYSTEMD_SERVICE:${PN} += "host-poweroff@.service"
 SYSTEMD_LINK:${PN} += "${@compose_list(d, 'PWR_OFF_INSTFMT', 'OBMC_HOST_INSTANCES')}"
 
+SYSTEMD_SERVICE:${PN} += "host-reset@.service"
+SYSTEMD_LINK:${PN} += "${@compose_list(d, 'PWR_RESET_INSTFMT', 'OBMC_HOST_INSTANCES')}"
+
+SYSTEMD_SERVICE:${PN} += "host-cycle@.service"
+SYSTEMD_LINK:${PN} += "${@compose_list(d, 'PWR_CYCLE_INSTFMT', 'OBMC_HOST_INSTANCES')}"
+
 SYSTEMD_SERVICE:${PN} += "host-ac-on@.service"
 SYSTEMD_LINK:${PN} += "${@compose_list(d, 'AC_ON_INSTFMT', 'OBMC_HOST_INSTANCES')}"