Targets and services to check for pgood on bmc reset

Once enabled these targets and services will be run after
a BMC reboot to check if pgood is on and if so, to create
a file which will be used by other power on services
to know whether they need to run or not.

Change-Id: Id2e75f9bce20f362d2f57a79e91ece1eaf7d7c45
Signed-off-by: Andrew Geissler <andrewg@us.ibm.com>
diff --git a/common/recipes-core/systemd/obmc-targets.bb b/common/recipes-core/systemd/obmc-targets.bb
index 5f34c17..6f83a9e 100644
--- a/common/recipes-core/systemd/obmc-targets.bb
+++ b/common/recipes-core/systemd/obmc-targets.bb
@@ -8,25 +8,50 @@
 inherit allarch
 
 # Overall chassis and host control
+# - start: Services to run to start the host
+# - stop:  Services to run to stop the host
 CHASSIS_TARGETS = "start stop"
+
 # Synchronization targets
-SYNCH_POWER_TARGETS = "start-pre start on stop-pre stop off"
+# - start-pre:         Services to run before we start power on process
+# - start:             Services to run to do the chassis power on
+# - on:                Services to run once power is on
+# - stop-pre,stop,off: Same as above but applied to powering off
+# - reset-on:          Services to check if chassis power is on after bmc reset
+SYNCH_POWER_TARGETS = "start-pre start on stop-pre stop off reset-on"
+
 # Control chassis power
+# - on:  Services to run to power on the chassis
+# - off: Services to run to power off the chassis
 CHASSIS_POWER_TARGETS = "on off"
-# Track all host synchronization point target
+
+#TODO - After target renames #1205, combine this with CHASSIS_POWER_TARGETS
+# - reset: Services to check chassis power state and update chassis "on" target
+CHASSIS_POWER_TARGETS_2 = "reset"
+
+# Track all host synchronization point targets
+# - start-pre:             Services to run before we start host boot
+# - start:                 Services to run to do the host boot
+# - started:               Services to run once the host is booted
+# - stop-pre,stop,stopped: Same as above but applied to shutting down the host
 HOST_SYNCH_TARGETS = "start-pre start started stop-pre stop stopped"
+
 # Track all host action targets
+# - stop:    Services to run to shutdown the host
+# - quiesce: Target to enter on host boot failure
 HOST_ACTION_TARGETS = "stop quiesce"
 
 CHASSIS_FMT = "obmc-chassis-{0}@.target"
 SYNCH_POWER_FMT = "obmc-power-{0}@.target"
 CHASSIS_POWER_FMT = "obmc-power-chassis-{0}@.target"
+CHASSIS_POWER_FMT_2 = "obmc-chassis-{0}@.target"
 HOST_SYNCH_FMT = "obmc-host-{0}@.target"
 HOST_ACTION_FMT = "obmc-{0}-host@.target"
 
 CHASSIS_LINK_FMT = "${CHASSIS_FMT}:obmc-chassis-{0}@{1}.target"
 SYNCH_POWER_LINK_FMT = "${SYNCH_POWER_FMT}:obmc-power-{0}@{1}.target"
 CHASSIS_POWER_LINK_FMT = "${CHASSIS_POWER_FMT}:obmc-power-chassis-{0}@{1}.target"
+CHASSIS_POWER_LINK_FMT_2 = "${CHASSIS_POWER_FMT_2}:obmc-chassis-{0}@{1}.target"
 HOST_LINK_SYNCH_FMT = "${HOST_SYNCH_FMT}:obmc-host-{0}@{1}.target"
 HOST_LINK_ACTION_FMT = "${HOST_ACTION_FMT}:obmc-{0}-host@{1}.target"
 
@@ -41,11 +66,13 @@
 SYSTEMD_SERVICE_${PN} += "${@compose_list(d, 'CHASSIS_FMT', 'CHASSIS_TARGETS')}"
 SYSTEMD_SERVICE_${PN} += "${@compose_list(d, 'SYNCH_POWER_FMT', 'SYNCH_POWER_TARGETS')}"
 SYSTEMD_SERVICE_${PN} += "${@compose_list(d, 'CHASSIS_POWER_FMT', 'CHASSIS_POWER_TARGETS')}"
+SYSTEMD_SERVICE_${PN} += "${@compose_list(d, 'CHASSIS_POWER_FMT_2', 'CHASSIS_POWER_TARGETS_2')}"
 SYSTEMD_SERVICE_${PN} += "${@compose_list(d, 'HOST_SYNCH_FMT', 'HOST_SYNCH_TARGETS')}"
 SYSTEMD_SERVICE_${PN} += "${@compose_list(d, 'HOST_ACTION_FMT', 'HOST_ACTION_TARGETS')}"
 
 SYSTEMD_LINK_${PN} += "${@compose_list(d, 'CHASSIS_LINK_FMT', 'CHASSIS_TARGETS', 'OBMC_CHASSIS_INSTANCES')}"
 SYSTEMD_LINK_${PN} += "${@compose_list(d, 'SYNCH_POWER_LINK_FMT', 'SYNCH_POWER_TARGETS', 'OBMC_POWER_INSTANCES')}"
 SYSTEMD_LINK_${PN} += "${@compose_list(d, 'CHASSIS_POWER_LINK_FMT', 'CHASSIS_POWER_TARGETS', 'OBMC_CHASSIS_INSTANCES')}"
+SYSTEMD_LINK_${PN} += "${@compose_list(d, 'CHASSIS_POWER_LINK_FMT_2', 'CHASSIS_POWER_TARGETS_2', 'OBMC_CHASSIS_INSTANCES')}"
 SYSTEMD_LINK_${PN} += "${@compose_list(d, 'HOST_LINK_SYNCH_FMT', 'HOST_SYNCH_TARGETS', 'OBMC_HOST_INSTANCES')}"
 SYSTEMD_LINK_${PN} += "${@compose_list(d, 'HOST_LINK_ACTION_FMT', 'HOST_ACTION_TARGETS', 'OBMC_HOST_INSTANCES')}"
diff --git a/common/recipes-core/systemd/obmc-targets/obmc-chassis-reset@.target b/common/recipes-core/systemd/obmc-targets/obmc-chassis-reset@.target
new file mode 100644
index 0000000..1741d41
--- /dev/null
+++ b/common/recipes-core/systemd/obmc-targets/obmc-chassis-reset@.target
@@ -0,0 +1,4 @@
+[Unit]
+Description=Chassis%i (Reset Check)
+Conflicts=obmc-power-chassis-off%i.target
+RefuseManualStop=yes
diff --git a/common/recipes-core/systemd/obmc-targets/obmc-power-chassis-off@.target b/common/recipes-core/systemd/obmc-targets/obmc-power-chassis-off@.target
index b3c8256..ceba1e3 100644
--- a/common/recipes-core/systemd/obmc-targets/obmc-power-chassis-off@.target
+++ b/common/recipes-core/systemd/obmc-targets/obmc-power-chassis-off@.target
@@ -5,4 +5,5 @@
 Wants=mapper-wait@-xyz-openbmc_project-state-chassis%i.service
 After=mapper-wait@-xyz-openbmc_project-state-chassis%i.service
 Conflicts=obmc-power-chassis-on@%i.target
+Conflicts=obmc-chassis-reset@%i.target
 RefuseManualStop=yes
diff --git a/common/recipes-core/systemd/obmc-targets/obmc-power-reset-on@.target b/common/recipes-core/systemd/obmc-targets/obmc-power-reset-on@.target
new file mode 100644
index 0000000..5c8d3e6
--- /dev/null
+++ b/common/recipes-core/systemd/obmc-targets/obmc-power-reset-on@.target
@@ -0,0 +1,5 @@
+[Unit]
+Description=Chassis%i power on after reset
+Conflicts=obmc-power-chassis-off@%i.target
+RefuseManualStart=yes
+RefuseManualStop=yes
diff --git a/common/recipes-phosphor/chassis/obmc-op-control-power.bb b/common/recipes-phosphor/chassis/obmc-op-control-power.bb
index eb9513b..7c4fb2b 100644
--- a/common/recipes-phosphor/chassis/obmc-op-control-power.bb
+++ b/common/recipes-phosphor/chassis/obmc-op-control-power.bb
@@ -17,6 +17,8 @@
         op-wait-power-on@.service \
         op-power-stop@.service \
         op-wait-power-off@.service \
+        op-reset-pgood-check@.service \
+        op-reset-set-power-on@.service \
         "
 
 SYSTEMD_ENVIRONMENT_FILE_${PN} += "obmc/power_control"
@@ -39,11 +41,22 @@
 OFF_INSTFMT = "op-wait-power-off@{0}.service"
 OFF_FMT = "../${OFF_TMPL}:${STOP_TGTFMT}.requires/${OFF_INSTFMT}"
 
+RESET_TMPL = "op-reset-pgood-check@.service"
+RESET_TGTFMT = "obmc-chassis-reset@{1}.target"
+RESET_INSTFMT = "op-reset-pgood-check@{0}.service"
+RESET_FMT = "../${RESET_TMPL}:${RESET_TGTFMT}.requires/${RESET_INSTFMT}"
+
+RESET_ON_TMPL = "op-reset-set-power-on@.service"
+RESET_ON_INSTFMT = "op-reset-set-power-on@{0}.service"
+RESET_ON_FMT = "../${RESET_ON_TMPL}:${RESET_TGTFMT}.requires/${RESET_ON_INSTFMT}"
+
 # Build up requires relationship for START_TGTFMT and STOP_TGTFMT
 SYSTEMD_LINK_${PN} += "${@compose_list_zip(d, 'START_FMT', 'OBMC_POWER_INSTANCES', 'OBMC_CHASSIS_INSTANCES')}"
 SYSTEMD_LINK_${PN} += "${@compose_list_zip(d, 'STOP_FMT', 'OBMC_POWER_INSTANCES', 'OBMC_CHASSIS_INSTANCES')}"
 SYSTEMD_LINK_${PN} += "${@compose_list_zip(d, 'ON_FMT', 'OBMC_POWER_INSTANCES', 'OBMC_CHASSIS_INSTANCES')}"
 SYSTEMD_LINK_${PN} += "${@compose_list_zip(d, 'OFF_FMT', 'OBMC_POWER_INSTANCES', 'OBMC_CHASSIS_INSTANCES')}"
+SYSTEMD_LINK_${PN} += "${@compose_list_zip(d, 'RESET_FMT', 'OBMC_POWER_INSTANCES', 'OBMC_CHASSIS_INSTANCES')}"
+SYSTEMD_LINK_${PN} += "${@compose_list_zip(d, 'RESET_ON_FMT', 'OBMC_POWER_INSTANCES', 'OBMC_CHASSIS_INSTANCES')}"
 
 # Now show that the main control target requires these power targets
 START_TMPL_CTRL = "obmc-power-chassis-on@.target"
diff --git a/common/recipes-phosphor/chassis/obmc-op-control-power/op-reset-pgood-check@.service b/common/recipes-phosphor/chassis/obmc-op-control-power/op-reset-pgood-check@.service
new file mode 100644
index 0000000..b009537
--- /dev/null
+++ b/common/recipes-phosphor/chassis/obmc-op-control-power/op-reset-pgood-check@.service
@@ -0,0 +1,16 @@
+[Unit]
+Description=Check Chassis%i pgood on reset
+Wants=mapper-wait@-org-openbmc-control-power%i.service
+After=mapper-wait@-org-openbmc-control-power%i.service
+Wants=obmc-power-reset-on@%i.target
+Before=obmc-power-reset-on@%i.target
+Conflicts=obmc-power-chassis-off@%i.target
+
+[Service]
+RemainAfterExit=no
+Type=oneshot
+ExecStart=/bin/sh -c "busctl get-property `mapper get-service /org/openbmc/control/power%i` /org/openbmc/control/power%i org.openbmc.control.Power pgood | sed 's/i\s*[1]/on/' | grep on"
+
+
+[Install]
+WantedBy=obmc-chassis-reset@%i.target
diff --git a/common/recipes-phosphor/chassis/obmc-op-control-power/op-reset-set-power-on@.service b/common/recipes-phosphor/chassis/obmc-op-control-power/op-reset-set-power-on@.service
new file mode 100644
index 0000000..4d58d61
--- /dev/null
+++ b/common/recipes-phosphor/chassis/obmc-op-control-power/op-reset-set-power-on@.service
@@ -0,0 +1,16 @@
+[Unit]
+Description=Create file to indicate chassis%i pgood is on
+After=obmc-power-reset-on@%i.target
+Requires=obmc-power-reset-on@%i.target
+After=op-reset-pgood-check@%i.service
+Requires=op-reset-pgood-check@%i.service
+Conflicts=obmc-power-chassis-off@%i.target
+
+[Service]
+RemainAfterExit=no
+Type=oneshot
+ExecStart=/bin/sh -c "mkdir -p /run/openbmc/ && touch /run/openbmc/chassis@%i-on"
+
+
+[Install]
+WantedBy=obmc-chassis-reset@%i.target