diff options
| author | Peter Yin <peter.yin@quantatw.com> | 2025-08-22 05:35:13 +0300 |
|---|---|---|
| committer | Patrick Williams <patrick@stwcx.xyz> | 2025-08-26 16:22:15 +0300 |
| commit | 176a0992cdc8b0a11a376ceb479e2b7fb03f2dc5 (patch) | |
| tree | 72d0e184a04e1af44a91057c23117bc02f204e89 | |
| parent | 92505498cda0bd2f231cd6aed356fc2daf98ecc0 (diff) | |
| download | openbmc-176a0992cdc8b0a11a376ceb479e2b7fb03f2dc5.tar.xz | |
meta-facebook: harma: Refactor power scripts and add init service
- Use loop for blocking service check, remove code duplication
- Replace "-z" with `systemctl is-active` to cover unknown states
- Move power sync into power-cmd functions for consistency
- Add initial-poweron-device.service for i2c mux rebind
- Add Conflicts in host-poweron@.service to avoid race conditions
Change-Id: Idfc778000bf3dc880b6c589482856992b476ee0a
Signed-off-by: Peter Yin <peter.yin@quantatw.com>
11 files changed, 199 insertions, 79 deletions
diff --git a/meta-facebook/meta-harma/recipes-phosphor/gpio/phosphor-gpio-monitor/assert-power-good b/meta-facebook/meta-harma/recipes-phosphor/gpio/phosphor-gpio-monitor/assert-power-good index 84d745817b..72c8127858 100644 --- a/meta-facebook/meta-harma/recipes-phosphor/gpio/phosphor-gpio-monitor/assert-power-good +++ b/meta-facebook/meta-harma/recipes-phosphor/gpio/phosphor-gpio-monitor/assert-power-good @@ -3,47 +3,42 @@ # shellcheck source=meta-facebook/meta-harma/recipes-phosphor/state/phosphor-state-manager/power-cmd source /usr/libexec/phosphor-state-manager/power-cmd -# Sync Led status to off +# Check SGPIO validity +if ! check_valid_sgpio; then + exit 0 +fi + +# Sync Led status to on systemctl start obmc-led-group-stop@power_on.service -currentstate=$(busctl get-property \ +# Get current host state +current_state=$(busctl get-property \ xyz.openbmc_project.State.Host0 \ /xyz/openbmc_project/state/host0 \ xyz.openbmc_project.State.Host \ CurrentHostState | awk '{print $2}' | tr -d '"') -if [ "$currentstate" == "xyz.openbmc_project.State.Host.HostState.TransitioningToOff" ]; then +# Exit if host is already transitioning off +if [ "$current_state" = "xyz.openbmc_project.State.Host.HostState.TransitioningToOff" ]; then exit 0 fi -active=$(systemctl is-active host-graceful-poweroff@0.service) -if [ -z "$active" ] || [ "$active" != "inactive" ]; then - exit 0 -fi +# List of services that block power_off_sync +block_services=( + host-graceful-poweroff@0.service + host-force-poweroff@0.service + host-powerreset@0.service +) -active=$(systemctl is-active host-force-poweroff@0.service) -if [ -z "$active" ] || [ "$active" != "inactive" ]; then - exit 0 -fi +# Exit if any blocking service is not inactive +for svc in "${block_services[@]}"; do + if [ "$(systemctl is-active "$svc")" != "inactive" ]; then + exit 0 + fi +done -active=$(systemctl is-active host-powerreset@0.service) -if [ -z "$active" ] || [ "$active" != "inactive" ]; then - exit 0 -fi +# Safe to power off +# Wait 8 seconds, checking every second +power_off_sync 8 1 -sleep 3 -# Sync power state to "off" for abnormal power lose. -transition=$(busctl get-property \ - xyz.openbmc_project.State.Host0 \ - /xyz/openbmc_project/state/host0 \ - xyz.openbmc_project.State.Host \ - RequestedHostTransition | awk '{print $2}' | tr -d '"') - -if [ "$transition" != "xyz.openbmc_project.State.Host.Transition.Off" ] && [ "$(power_status)" == "off" ]; then - busctl set-property xyz.openbmc_project.State.Host0 \ - /xyz/openbmc_project/state/host0 \ - xyz.openbmc_project.State.Host \ - RequestedHostTransition s \ - xyz.openbmc_project.State.Host.Transition.Off -fi exit 0 diff --git a/meta-facebook/meta-harma/recipes-phosphor/gpio/phosphor-gpio-monitor/deassert-power-good b/meta-facebook/meta-harma/recipes-phosphor/gpio/phosphor-gpio-monitor/deassert-power-good index 8fb9aca337..1dfee3cd48 100644 --- a/meta-facebook/meta-harma/recipes-phosphor/gpio/phosphor-gpio-monitor/deassert-power-good +++ b/meta-facebook/meta-harma/recipes-phosphor/gpio/phosphor-gpio-monitor/deassert-power-good @@ -1,63 +1,45 @@ #!/bin/bash -# shellcheck source=meta-facebook/recipes-fb/obmc_functions/files/fb-common-functions -source /usr/libexec/fb-common-functions # shellcheck source=meta-facebook/meta-harma/recipes-phosphor/state/phosphor-state-manager/power-cmd source /usr/libexec/phosphor-state-manager/power-cmd -# shellcheck source=meta-facebook/meta-harma/recipes-phosphor/gpio/phosphor-gpio-monitor/device-util -source /usr/libexec/phosphor-gpio-monitor/device-util - -# The hardware disables the multiplexer in standby -# to prevent sending a command by mistake to the retimer when it boots up. -# Therefore, we need to rebind it when powered on. -if [ ! -e "/sys/bus/i2c/drivers/pca954x/3-0070" ]; then - sleep 6 - rebind_i2c_dev 3 70 "pca954x" -fi -check_valid_sgpio -valid_sgpio=$? +# Check SGPIO validity +if ! check_valid_sgpio; then + exit 0 +fi -if [ "$valid_sgpio" -eq 0 ]; then - # Sync Led status to on - systemctl start obmc-led-group-start@power_on.service +# Sync Led status to on +systemctl start obmc-led-group-start@power_on.service - currentstate=$(busctl get-property \ - xyz.openbmc_project.State.Host0 \ - /xyz/openbmc_project/state/host0 \ - xyz.openbmc_project.State.Host \ - CurrentHostState | awk '{print $2}' | tr -d '"') - if [ "$currentstate" == "xyz.openbmc_project.State.Host.HostState.TransitioningToRunning" ]; then - exit 0 - fi -fi +# Checking and Syncing power policy. +# Get current host state +current_state=$(busctl get-property \ + xyz.openbmc_project.State.Host0 \ + /xyz/openbmc_project/state/host0 \ + xyz.openbmc_project.State.Host \ + CurrentHostState | awk '{print $2}' | tr -d '"') -active=$(systemctl is-active host-poweron@0.service) -if [ -z "$active" ] || [ "$active" != "inactive" ]; then +# Exit if host is already transitioning to running +if [ "$current_state" = "xyz.openbmc_project.State.Host.HostState.TransitioningToRunning" ]; then exit 0 fi -active=$(systemctl is-active host-powerreset@0.service) -if [ -z "$active" ] || [ "$active" != "inactive" ]; then - exit 0 -fi +# List of services that block power_on_sync +block_services=( + host-poweron@0.service + host-powerreset@0.service +) -# Sync power status to "On" for abnormal power-on scenarios. -if [ "$valid_sgpio" -eq 0 ]; then - transition=$(busctl get-property \ - xyz.openbmc_project.State.Host0 \ - /xyz/openbmc_project/state/host0 \ - xyz.openbmc_project.State.Host \ - RequestedHostTransition | awk '{print $2}' | tr -d '"') - - if [ "$transition" != "xyz.openbmc_project.State.Host.Transition.On" ] && [ "$(power_status)" == "on" ]; then - busctl set-property xyz.openbmc_project.State.Host0 \ - /xyz/openbmc_project/state/host0 \ - xyz.openbmc_project.State.Host \ - RequestedHostTransition s \ - xyz.openbmc_project.State.Host.Transition.On +# Exit if any blocking service is not inactive +for svc in "${block_services[@]}"; do + if [ "$(systemctl is-active "$svc")" != "inactive" ]; then + exit 0 fi -fi +done + +# Safe to power on +# Wait 3 seconds, checking every second +power_on_sync 3 1 exit 0 diff --git a/meta-facebook/meta-harma/recipes-phosphor/gpio/phosphor-gpio-monitor/initial-poweron-device b/meta-facebook/meta-harma/recipes-phosphor/gpio/phosphor-gpio-monitor/initial-poweron-device new file mode 100644 index 0000000000..0f93066c56 --- /dev/null +++ b/meta-facebook/meta-harma/recipes-phosphor/gpio/phosphor-gpio-monitor/initial-poweron-device @@ -0,0 +1,14 @@ +#!/bin/bash + +# shellcheck source=meta-facebook/meta-harma/recipes-phosphor/gpio/phosphor-gpio-monitor/device-util +source /usr/libexec/phosphor-gpio-monitor/device-util + +# The hardware disables the multiplexer in standby +# to prevent sending a command by mistake to the retimer when it boots up. +# Therefore, we need to rebind it when powered on. +if [ ! -e "/sys/bus/i2c/drivers/pca954x/3-0070" ]; then + sleep 6 + rebind_i2c_dev 3 70 "pca954x" +fi + +exit 0 diff --git a/meta-facebook/meta-harma/recipes-phosphor/gpio/phosphor-gpio-monitor/initial-poweron-device.service b/meta-facebook/meta-harma/recipes-phosphor/gpio/phosphor-gpio-monitor/initial-poweron-device.service new file mode 100644 index 0000000000..1ad1f43103 --- /dev/null +++ b/meta-facebook/meta-harma/recipes-phosphor/gpio/phosphor-gpio-monitor/initial-poweron-device.service @@ -0,0 +1,8 @@ +[Unit] +Description=Initial Power On Device + +[Service] +Type=oneshot +ExecStart=/usr/libexec/phosphor-gpio-monitor/initial-poweron-device + +SyslogIdentifier=initial-poweron-device diff --git a/meta-facebook/meta-harma/recipes-phosphor/gpio/phosphor-gpio-monitor/plat-phosphor-multi-gpio-monitor.json b/meta-facebook/meta-harma/recipes-phosphor/gpio/phosphor-gpio-monitor/plat-phosphor-multi-gpio-monitor.json index 3ae18801e9..ee49ab09e4 100644 --- a/meta-facebook/meta-harma/recipes-phosphor/gpio/phosphor-gpio-monitor/plat-phosphor-multi-gpio-monitor.json +++ b/meta-facebook/meta-harma/recipes-phosphor/gpio/phosphor-gpio-monitor/plat-phosphor-multi-gpio-monitor.json @@ -98,7 +98,8 @@ "Targets": { "RISING": [ "deassert-power-good.service", - "power-rail-deassert-log@2_power-host-good.service" + "initial-poweron-device.service", + "power-rail-deassert-log@2_power-host-good.service" ], "FALLING": [ "assert-power-good.service", diff --git a/meta-facebook/meta-harma/recipes-phosphor/gpio/phosphor-gpio-monitor_%.bbappend b/meta-facebook/meta-harma/recipes-phosphor/gpio/phosphor-gpio-monitor_%.bbappend index b2d5d490bb..ae82d20f37 100644 --- a/meta-facebook/meta-harma/recipes-phosphor/gpio/phosphor-gpio-monitor_%.bbappend +++ b/meta-facebook/meta-harma/recipes-phosphor/gpio/phosphor-gpio-monitor_%.bbappend @@ -29,6 +29,8 @@ SRC_URI += "file://assert-post-end \ file://gpios-assert-log@.service \ file://gpios-deassert-log@.service \ file://gpios-event-logger \ + file://initial-poweron-device \ + file://initial-poweron-device.service \ file://logging-util \ file://mmc-recovery.service \ file://multi-gpios-sys-init \ @@ -66,6 +68,7 @@ SYSTEMD_SERVICE:${PN} += " \ deassert-uart-switch-button.service \ device-reinitial@.service \ fan-reload.service \ + initial-poweron-device.service \ mmc-recovery.service \ multi-gpios-sys-init.service \ prochot-assert-log.service \ @@ -120,6 +123,7 @@ do_install:append() { install -m 0755 ${UNPACKDIR}/auto-poweron ${D}${libexecdir}/${PN}/ install -m 0755 ${UNPACKDIR}/fan-reload ${D}${libexecdir}/${PN}/ + install -m 0755 ${UNPACKDIR}/initial-poweron-device ${D}${libexecdir}/${PN}/ } SYSTEMD_OVERRIDE:${PN}-monitor += "phosphor-multi-gpio-monitor.conf:phosphor-multi-gpio-monitor.service.d/phosphor-multi-gpio-monitor.conf" diff --git a/meta-facebook/meta-harma/recipes-phosphor/state/phosphor-state-manager/host-force-poweroff@.service b/meta-facebook/meta-harma/recipes-phosphor/state/phosphor-state-manager/host-force-poweroff@.service index 062d4f2098..fcb1da5d52 100644 --- a/meta-facebook/meta-harma/recipes-phosphor/state/phosphor-state-manager/host-force-poweroff@.service +++ b/meta-facebook/meta-harma/recipes-phosphor/state/phosphor-state-manager/host-force-poweroff@.service @@ -9,6 +9,7 @@ Restart=no Type=oneshot RemainAfterExit=yes ExecStart=/usr/libexec/phosphor-state-manager/host-force-poweroff %i +ExecStop=/usr/libexec/fb-common-functions set_gpio power-host-control 1 [Install] RequiredBy=obmc-host-stop@%i.target diff --git a/meta-facebook/meta-harma/recipes-phosphor/state/phosphor-state-manager/host-graceful-poweroff@.service b/meta-facebook/meta-harma/recipes-phosphor/state/phosphor-state-manager/host-graceful-poweroff@.service index c8af4ee700..ec5f5db76a 100644 --- a/meta-facebook/meta-harma/recipes-phosphor/state/phosphor-state-manager/host-graceful-poweroff@.service +++ b/meta-facebook/meta-harma/recipes-phosphor/state/phosphor-state-manager/host-graceful-poweroff@.service @@ -10,6 +10,7 @@ Restart=no Type=oneshot RemainAfterExit=yes ExecStart=/usr/libexec/phosphor-state-manager/host-graceful-poweroff %i +ExecStop=/usr/libexec/fb-common-functions set_gpio power-host-control 1 [Install] WantedBy=obmc-host-shutdown@%i.target diff --git a/meta-facebook/meta-harma/recipes-phosphor/state/phosphor-state-manager/host-poweron@.service b/meta-facebook/meta-harma/recipes-phosphor/state/phosphor-state-manager/host-poweron@.service index 050c5c9b4c..fb814c5d79 100644 --- a/meta-facebook/meta-harma/recipes-phosphor/state/phosphor-state-manager/host-poweron@.service +++ b/meta-facebook/meta-harma/recipes-phosphor/state/phosphor-state-manager/host-poweron@.service @@ -7,6 +7,7 @@ Before=obmc-host-starting@%i.target Before=obmc-host-started@%i.target Wants=obmc-power-on@%i.target After=obmc-power-on@%i.target +Conflicts=obmc-host-shutdown@%i.target Conflicts=obmc-host-stop@%i.target Wants=phosphor-set-host-transition-to-running@service Before=phosphor-set-host-transition-to-running@service @@ -17,7 +18,6 @@ Restart=no Type=oneshot RemainAfterExit=yes ExecStart=/usr/libexec/phosphor-state-manager/host-poweron %i -ExecStartPost=/bin/systemctl stop host-graceful-poweroff@%i.service [Install] RequiredBy=obmc-host-startmin@%i.target diff --git a/meta-facebook/meta-harma/recipes-phosphor/state/phosphor-state-manager/power-cmd b/meta-facebook/meta-harma/recipes-phosphor/state/phosphor-state-manager/power-cmd index 849b0419ca..87c3f67408 100644 --- a/meta-facebook/meta-harma/recipes-phosphor/state/phosphor-state-manager/power-cmd +++ b/meta-facebook/meta-harma/recipes-phosphor/state/phosphor-state-manager/power-cmd @@ -84,6 +84,8 @@ graceful_power_off() { # DC on power_on() { + set_gpio power-host-control 1 + sleep 1 if [ "$(power_status)" == "off" ]; then set_gpio power-host-control 0 sleep 1 @@ -113,3 +115,95 @@ power_reset() { sleep 1 return 0 } + +# Sync power state to "off" for abnormal power loss. +power_off_sync() { + local timeout=${1:-8} # default 8 seconds if not provided + local delay=${2:-1} # default 1 second interval if not provided + + for ((i=0; i<timeout; i++)); do + local status + status=$(power_status) + if [ "$status" != "off" ]; then + return + fi + sleep "$delay" + done + + local transition + transition=$(busctl get-property \ + xyz.openbmc_project.State.Host0 \ + /xyz/openbmc_project/state/host0 \ + xyz.openbmc_project.State.Host \ + RequestedHostTransition | awk '{print $2}' | tr -d '"') + + if [ "$transition" != "xyz.openbmc_project.State.Host.Transition.Off" ] && [ "$(power_status)" == "off" ]; then + busctl set-property xyz.openbmc_project.State.Host0 \ + /xyz/openbmc_project/state/host0 \ + xyz.openbmc_project.State.Host \ + RequestedHostTransition s \ + xyz.openbmc_project.State.Host.Transition.Off + fi +} + +# Sync power status to "On" for abnormal power-on scenarios. +power_on_sync() { + local timeout=${1:-3} # default 3 seconds if not provided + local delay=${2:-1} # default 1 second interval if not provided + + for ((i=0; i<timeout; i++)); do + local status + status=$(power_status) + if [ "$status" != "on" ]; then + return + fi + sleep "$delay" + done + + local transition + transition=$(busctl get-property \ + xyz.openbmc_project.State.Host0 \ + /xyz/openbmc_project/state/host0 \ + xyz.openbmc_project.State.Host \ + RequestedHostTransition | awk '{print $2}' | tr -d '"') + + if [ "$transition" != "xyz.openbmc_project.State.Host.Transition.On" ] && [ "$(power_status)" == "on" ]; then + busctl set-property xyz.openbmc_project.State.Host0 \ + /xyz/openbmc_project/state/host0 \ + xyz.openbmc_project.State.Host \ + RequestedHostTransition s \ + xyz.openbmc_project.State.Host.Transition.On + fi +} + +# Main execution +if [[ "${BASH_SOURCE[0]}" == "$0" ]]; then + case "$1" in + force_off) + force_power_off + ;; + graceful_off) + graceful_power_off + ;; + power_on) + power_on + ;; + power_reset) + power_reset + ;; + off_sync) + power_off_sync "$2" "$3" + ;; + on_sync) + power_on_sync "$2" "$3" + ;; + status) + power_status + ;; + *) + echo "Usage: $0 {force_off|graceful_off|power_on|power_reset|off_sync|on_sync|status}" + exit 0 + ;; + esac +fi + diff --git a/meta-facebook/recipes-fb/obmc_functions/files/fb-common-functions b/meta-facebook/recipes-fb/obmc_functions/files/fb-common-functions index 7dd92ac7b1..2da67ef522 100644 --- a/meta-facebook/recipes-fb/obmc_functions/files/fb-common-functions +++ b/meta-facebook/recipes-fb/obmc_functions/files/fb-common-functions @@ -76,3 +76,23 @@ bind_i2c_device() return 1 } + +if [[ "${BASH_SOURCE[0]}" == "${0}" ]]; then + CMD=$1 + shift + case "$CMD" in + get_gpio) + get_gpio "$@" + ;; + set_gpio) + set_gpio "$@" + ;; + bind_i2c_device) + bind_i2c_device "$@" + ;; + *) + echo "Usage: $0 {get_gpio|set_gpio|bind_i2c_device} args..." + exit 1 + ;; + esac +fi |
