meta-bletchley: motor-ctrl: fix host-ac-on failed during bmc boot

1. add service dependency to avoid host-ac-on service failed due to
inventory (presence_sledX) property not avaliable.

host-ac-on@X.service
  |- motor-init-calibration@X.service (Requires, After)
     |- phosphor-gpio-presence@presence-sledX.service (Requires, After)

2. simplify check method for ac-on & ac-off actions, just check host
ac status before set ac on/off.

3. fix host-ac-on service shows failed when ac is aready turned on
(ex: BMC reset), return 0 for both ac-on & ac-off actions no
matter GPIO (power-hostX) set required or not.

Signed-off-by: Potin Lai <potin.lai@quantatw.com>
Change-Id: I2c878dea05ea2511e6564c0980ba34dc28a46e2c
diff --git a/meta-facebook/meta-bletchley/recipes-bletchley/motor-ctrl/files/host-ac-off@.service b/meta-facebook/meta-bletchley/recipes-bletchley/motor-ctrl/files/host-ac-off@.service
index 3962613..e65911f 100644
--- a/meta-facebook/meta-bletchley/recipes-bletchley/motor-ctrl/files/host-ac-off@.service
+++ b/meta-facebook/meta-bletchley/recipes-bletchley/motor-ctrl/files/host-ac-off@.service
@@ -1,5 +1,6 @@
 [Unit]
 Description=Turn off AC at Sled%i
+Conflicts=host-ac-on@%i.service
 
 [Service]
 Type=oneshot
diff --git a/meta-facebook/meta-bletchley/recipes-bletchley/motor-ctrl/files/host-ac-on@.service b/meta-facebook/meta-bletchley/recipes-bletchley/motor-ctrl/files/host-ac-on@.service
index 69d6042..32bdf49 100644
--- a/meta-facebook/meta-bletchley/recipes-bletchley/motor-ctrl/files/host-ac-on@.service
+++ b/meta-facebook/meta-bletchley/recipes-bletchley/motor-ctrl/files/host-ac-on@.service
@@ -1,6 +1,8 @@
 [Unit]
 Description=Turn on AC at Sled%i
+Requires=motor-init-calibration@%i.service
 After=motor-init-calibration@%i.service
+Conflicts=host-ac-off@%i.service
 
 [Service]
 Type=oneshot
diff --git a/meta-facebook/meta-bletchley/recipes-bletchley/motor-ctrl/files/host-poweroff@.service b/meta-facebook/meta-bletchley/recipes-bletchley/motor-ctrl/files/host-poweroff@.service
index 50cd532..c81f6d6a 100644
--- a/meta-facebook/meta-bletchley/recipes-bletchley/motor-ctrl/files/host-poweroff@.service
+++ b/meta-facebook/meta-bletchley/recipes-bletchley/motor-ctrl/files/host-poweroff@.service
@@ -2,6 +2,7 @@
 Description=Power off System in Sled%i by Step Motor
 Requires=motor-init-calibration@%i.service
 After=motor-init-calibration@%i.service
+Conflicts=host-poweron@%i.service
 
 [Service]
 Type=oneshot
diff --git a/meta-facebook/meta-bletchley/recipes-bletchley/motor-ctrl/files/host-poweron@.service b/meta-facebook/meta-bletchley/recipes-bletchley/motor-ctrl/files/host-poweron@.service
index 6ff1120..c9f15a6 100644
--- a/meta-facebook/meta-bletchley/recipes-bletchley/motor-ctrl/files/host-poweron@.service
+++ b/meta-facebook/meta-bletchley/recipes-bletchley/motor-ctrl/files/host-poweron@.service
@@ -2,6 +2,9 @@
 Description=Power on System in Sled%i by Step Motor
 Requires=motor-init-calibration@%i.service
 After=motor-init-calibration@%i.service
+Requires=host-ac-on@%i.service
+After=host-ac-on@%i.service
+Conflicts=host-poweroff@%i.service
 
 [Service]
 Type=oneshot
diff --git a/meta-facebook/meta-bletchley/recipes-bletchley/motor-ctrl/files/motor-init-calibration@.service b/meta-facebook/meta-bletchley/recipes-bletchley/motor-ctrl/files/motor-init-calibration@.service
index f28ab5e..e77c782 100644
--- a/meta-facebook/meta-bletchley/recipes-bletchley/motor-ctrl/files/motor-init-calibration@.service
+++ b/meta-facebook/meta-bletchley/recipes-bletchley/motor-ctrl/files/motor-init-calibration@.service
@@ -2,8 +2,10 @@
 Description=Motor Initialize for sled%i
 StartLimitIntervalSec=90
 StartLimitBurst=5
-After=mapper-wait@-xyz-openbmc_project-inventory-system-chassis-presence-presence_sled%i
-Wants=mapper-wait@-xyz-openbmc_project-inventory-system-chassis-presence-presence_sled%i
+Requires=phosphor-gpio-presence@presence-sled%i.service
+After=phosphor-gpio-presence@presence-sled%i.service
+Wants=mapper-wait@-xyz-openbmc_project-inventory-system-chassis-presence-presence_sled%i.service
+After=mapper-wait@-xyz-openbmc_project-inventory-system-chassis-presence-presence_sled%i.service
 
 [Service]
 ExecStart=/usr/libexec/motor-init sled%i
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 e430f8f..25cab0b 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
@@ -595,10 +595,19 @@
     exit 1
 fi
 
-HOST_CURR_STATUS=$(get_host_status "$SLED_NUM")
-if [ "$ACTION" = "$ACTION_STATUS" ];then
+if [ "$ACTION" = "$ACTION_AC_ON" ]; then
+    if [ "$(get_ac_status "$SLED_NUM")" = "$HOST_AC_OFF" ]; then
+        do_action_ac_on "$SLED_NUM"
+    fi
+elif [ "$ACTION" = "$ACTION_AC_OFF" ]; then
+    if [ "$(get_ac_status "$SLED_NUM")" != "$HOST_AC_OFF" ]; then
+        do_action_ac_off "$SLED_NUM"
+    fi
+elif [ "$ACTION" = "$ACTION_STATUS" ];then
+    HOST_CURR_STATUS=$(get_host_status "$SLED_NUM")
     echo "$HOST_CURR_STATUS"
 else
+    HOST_CURR_STATUS=$(get_host_status "$SLED_NUM")
     case $HOST_CURR_STATUS in
         "$HOST_AC_OFF")
             host_state_ac_off_action_handler "$SLED_NUM" "$ACTION"