Andrew Geissler | 769a62f | 2019-12-06 13:36:08 -0600 | [diff] [blame] | 1 | #include "config.h" |
Andrew Geissler | e426b58 | 2020-05-28 12:40:55 -0500 | [diff] [blame] | 2 | |
Michael Tritz | 3ed1008 | 2017-03-20 23:28:09 -0500 | [diff] [blame] | 3 | #include "host_state_manager.hpp" |
Deepak Kodihalli | 55f132b | 2017-07-25 07:36:06 -0500 | [diff] [blame] | 4 | #include "settings.hpp" |
Andrew Geissler | 49e6713 | 2022-01-26 14:27:52 -0600 | [diff] [blame] | 5 | #include "utils.hpp" |
Deepak Kodihalli | 55f132b | 2017-07-25 07:36:06 -0500 | [diff] [blame] | 6 | #include "xyz/openbmc_project/Common/error.hpp" |
| 7 | #include "xyz/openbmc_project/Control/Power/RestorePolicy/server.hpp" |
Michael Tritz | 3ed1008 | 2017-03-20 23:28:09 -0500 | [diff] [blame] | 8 | |
Andrew Geissler | e426b58 | 2020-05-28 12:40:55 -0500 | [diff] [blame] | 9 | #include <getopt.h> |
| 10 | #include <systemd/sd-bus.h> |
| 11 | |
| 12 | #include <phosphor-logging/elog-errors.hpp> |
Andrew Geissler | 8ffdb26 | 2021-09-20 15:25:19 -0500 | [diff] [blame] | 13 | #include <phosphor-logging/lg2.hpp> |
Andrew Geissler | e426b58 | 2020-05-28 12:40:55 -0500 | [diff] [blame] | 14 | #include <sdbusplus/exception.hpp> |
| 15 | #include <sdbusplus/server.hpp> |
Patrick Williams | 9a286db | 2024-01-17 06:29:47 -0600 | [diff] [blame] | 16 | #include <xyz/openbmc_project/State/BMC/client.hpp> |
| 17 | #include <xyz/openbmc_project/State/Host/client.hpp> |
Andrew Geissler | e426b58 | 2020-05-28 12:40:55 -0500 | [diff] [blame] | 18 | |
NodeMan97 | bcbee4a | 2022-05-27 15:28:45 -0500 | [diff] [blame] | 19 | #include <filesystem> |
Andrew Geissler | e426b58 | 2020-05-28 12:40:55 -0500 | [diff] [blame] | 20 | #include <iostream> |
| 21 | #include <map> |
| 22 | #include <string> |
NodeMan97 | 358de95 | 2022-07-20 08:31:36 -0500 | [diff] [blame] | 23 | #include <thread> |
Andrew Geissler | e426b58 | 2020-05-28 12:40:55 -0500 | [diff] [blame] | 24 | |
Michael Tritz | 3ed1008 | 2017-03-20 23:28:09 -0500 | [diff] [blame] | 25 | namespace phosphor |
| 26 | { |
| 27 | namespace state |
| 28 | { |
| 29 | namespace manager |
| 30 | { |
| 31 | |
Andrew Geissler | 8ffdb26 | 2021-09-20 15:25:19 -0500 | [diff] [blame] | 32 | PHOSPHOR_LOG2_USING; |
| 33 | |
Michael Tritz | 3ed1008 | 2017-03-20 23:28:09 -0500 | [diff] [blame] | 34 | using namespace phosphor::logging; |
Deepak Kodihalli | 55f132b | 2017-07-25 07:36:06 -0500 | [diff] [blame] | 35 | using namespace sdbusplus::xyz::openbmc_project::Common::Error; |
Patrick Williams | 7e969cb | 2023-08-23 16:24:23 -0500 | [diff] [blame] | 36 | using namespace sdbusplus::server::xyz::openbmc_project::control::power; |
Amithash Prasasd | f566c96 | 2024-07-22 20:28:42 -0700 | [diff] [blame] | 37 | using HostState = sdbusplus::client::xyz::openbmc_project::state::Host<>; |
| 38 | using BMCState = sdbusplus::client::xyz::openbmc_project::state::BMC<>; |
Michael Tritz | 3ed1008 | 2017-03-20 23:28:09 -0500 | [diff] [blame] | 39 | |
Michael Tritz | 3ed1008 | 2017-03-20 23:28:09 -0500 | [diff] [blame] | 40 | } // namespace manager |
| 41 | } // namespace state |
Andrew Geissler | a965cf0 | 2018-08-31 08:37:05 -0700 | [diff] [blame] | 42 | } // namespace phosphor |
Michael Tritz | 3ed1008 | 2017-03-20 23:28:09 -0500 | [diff] [blame] | 43 | |
Michael Tritz | d50f657 | 2017-09-05 16:38:11 -0500 | [diff] [blame] | 44 | int main(int argc, char** argv) |
Michael Tritz | 3ed1008 | 2017-03-20 23:28:09 -0500 | [diff] [blame] | 45 | { |
Deepak Kodihalli | 55f132b | 2017-07-25 07:36:06 -0500 | [diff] [blame] | 46 | using namespace phosphor::logging; |
| 47 | |
Potin Lai | c328a4c | 2022-03-18 23:49:37 +0800 | [diff] [blame] | 48 | size_t hostId = 0; |
Michael Tritz | d50f657 | 2017-09-05 16:38:11 -0500 | [diff] [blame] | 49 | int arg; |
| 50 | int optIndex = 0; |
| 51 | |
Pavithra Barithaya | f15b954 | 2024-06-21 08:18:48 -0500 | [diff] [blame] | 52 | static struct option longOpts[] = { |
| 53 | {"host", required_argument, nullptr, 'h'}, {nullptr, 0, nullptr, 0}}; |
Michael Tritz | d50f657 | 2017-09-05 16:38:11 -0500 | [diff] [blame] | 54 | |
Andrew Geissler | 58a1801 | 2018-01-19 19:36:05 -0800 | [diff] [blame] | 55 | while ((arg = getopt_long(argc, argv, "h:", longOpts, &optIndex)) != -1) |
Michael Tritz | d50f657 | 2017-09-05 16:38:11 -0500 | [diff] [blame] | 56 | { |
| 57 | switch (arg) |
| 58 | { |
| 59 | case 'h': |
Potin Lai | c328a4c | 2022-03-18 23:49:37 +0800 | [diff] [blame] | 60 | hostId = std::stoul(optarg); |
Michael Tritz | d50f657 | 2017-09-05 16:38:11 -0500 | [diff] [blame] | 61 | break; |
| 62 | default: |
| 63 | break; |
| 64 | } |
| 65 | } |
| 66 | |
Patrick Williams | 9a286db | 2024-01-17 06:29:47 -0600 | [diff] [blame] | 67 | using Host = sdbusplus::client::xyz::openbmc_project::state::Host<>; |
Patrick Williams | 1b2c3c0 | 2024-08-16 15:20:29 -0400 | [diff] [blame] | 68 | std::string hostPath = |
| 69 | std::string(Host::namespace_path::value) + "/" + |
| 70 | std::string(Host::namespace_path::host) + std::to_string(hostId); |
Patrick Williams | 9a286db | 2024-01-17 06:29:47 -0600 | [diff] [blame] | 71 | |
Michael Tritz | 3ed1008 | 2017-03-20 23:28:09 -0500 | [diff] [blame] | 72 | auto bus = sdbusplus::bus::new_default(); |
| 73 | |
Deepak Kodihalli | 55f132b | 2017-07-25 07:36:06 -0500 | [diff] [blame] | 74 | using namespace settings; |
Potin Lai | c328a4c | 2022-03-18 23:49:37 +0800 | [diff] [blame] | 75 | HostObjects settings(bus, hostId); |
Deepak Kodihalli | 55f132b | 2017-07-25 07:36:06 -0500 | [diff] [blame] | 76 | |
Michael Tritz | 3ed1008 | 2017-03-20 23:28:09 -0500 | [diff] [blame] | 77 | using namespace phosphor::state::manager; |
Patrick Williams | 7e969cb | 2023-08-23 16:24:23 -0500 | [diff] [blame] | 78 | namespace server = sdbusplus::server::xyz::openbmc_project::state; |
Michael Tritz | 3ed1008 | 2017-03-20 23:28:09 -0500 | [diff] [blame] | 79 | |
Andrew Geissler | 033fc3b | 2017-08-30 15:11:44 -0500 | [diff] [blame] | 80 | // This application is only run if chassis power is off |
Michael Tritz | 3ed1008 | 2017-03-20 23:28:09 -0500 | [diff] [blame] | 81 | |
Andrew Geissler | b2b3d9c | 2022-01-26 14:45:10 -0600 | [diff] [blame] | 82 | // If the BMC was rebooted due to a user initiated pinhole reset, do not |
| 83 | // implement any power restore policies |
Patrick Williams | 9a286db | 2024-01-17 06:29:47 -0600 | [diff] [blame] | 84 | using BMC = sdbusplus::client::xyz::openbmc_project::state::BMC<>; |
| 85 | auto bmcPath = sdbusplus::message::object_path(BMC::namespace_path::value) / |
| 86 | BMC::namespace_path::bmc; |
| 87 | |
| 88 | auto bmcRebootCause = |
| 89 | sdbusplus::message::convert_from_string<BMC::RebootCause>( |
| 90 | phosphor::state::manager::utils::getProperty( |
Amithash Prasasd | f566c96 | 2024-07-22 20:28:42 -0700 | [diff] [blame] | 91 | bus, bmcPath.str, BMCState::interface, "LastRebootCause")); |
Patrick Williams | 9a286db | 2024-01-17 06:29:47 -0600 | [diff] [blame] | 92 | |
| 93 | if (bmcRebootCause == BMC::RebootCause::PinholeReset) |
Andrew Geissler | b2b3d9c | 2022-01-26 14:45:10 -0600 | [diff] [blame] | 94 | { |
| 95 | info( |
| 96 | "BMC was reset due to pinhole reset, no power restore policy will be run"); |
| 97 | return 0; |
| 98 | } |
Patrick Williams | 9a286db | 2024-01-17 06:29:47 -0600 | [diff] [blame] | 99 | else if (bmcRebootCause == BMC::RebootCause::Watchdog) |
Thang Q. Nguyen | b669ea3 | 2022-02-22 07:15:15 +0000 | [diff] [blame] | 100 | { |
| 101 | info( |
| 102 | "BMC was reset due to cold reset, no power restore policy will be run"); |
| 103 | return 0; |
| 104 | } |
Andrew Geissler | b2b3d9c | 2022-01-26 14:45:10 -0600 | [diff] [blame] | 105 | |
Andrew Geissler | 35ca2e3 | 2021-02-09 13:54:26 -0600 | [diff] [blame] | 106 | /* The logic here is to first check the one-time PowerRestorePolicy setting. |
| 107 | * If this property is not the default then look at the persistent |
| 108 | * user setting in the non one-time object, otherwise honor the one-time |
| 109 | * setting. |
| 110 | */ |
| 111 | auto methodOneTime = bus.new_method_call( |
| 112 | settings.service(settings.powerRestorePolicy, powerRestoreIntf).c_str(), |
| 113 | settings.powerRestorePolicyOneTime.c_str(), |
| 114 | "org.freedesktop.DBus.Properties", "Get"); |
| 115 | methodOneTime.append(powerRestoreIntf, "PowerRestorePolicy"); |
| 116 | |
| 117 | auto methodUserSetting = bus.new_method_call( |
Andrew Geissler | 58a1801 | 2018-01-19 19:36:05 -0800 | [diff] [blame] | 118 | settings.service(settings.powerRestorePolicy, powerRestoreIntf).c_str(), |
| 119 | settings.powerRestorePolicy.c_str(), "org.freedesktop.DBus.Properties", |
| 120 | "Get"); |
Andrew Geissler | 35ca2e3 | 2021-02-09 13:54:26 -0600 | [diff] [blame] | 121 | methodUserSetting.append(powerRestoreIntf, "PowerRestorePolicy"); |
Anthony Wilson | 32c532e | 2018-10-25 21:56:07 -0500 | [diff] [blame] | 122 | |
Patrick Williams | 2975e26 | 2020-05-13 18:01:09 -0500 | [diff] [blame] | 123 | std::variant<std::string> result; |
Anthony Wilson | 32c532e | 2018-10-25 21:56:07 -0500 | [diff] [blame] | 124 | try |
Michael Tritz | 3ed1008 | 2017-03-20 23:28:09 -0500 | [diff] [blame] | 125 | { |
Andrew Geissler | 35ca2e3 | 2021-02-09 13:54:26 -0600 | [diff] [blame] | 126 | auto reply = bus.call(methodOneTime); |
Anthony Wilson | 32c532e | 2018-10-25 21:56:07 -0500 | [diff] [blame] | 127 | reply.read(result); |
Andrew Geissler | 35ca2e3 | 2021-02-09 13:54:26 -0600 | [diff] [blame] | 128 | auto powerPolicy = std::get<std::string>(result); |
| 129 | |
| 130 | if (RestorePolicy::Policy::None == |
| 131 | RestorePolicy::convertPolicyFromString(powerPolicy)) |
| 132 | { |
| 133 | // one_time is set to None so use the customer setting |
Andrew Geissler | 8ffdb26 | 2021-09-20 15:25:19 -0500 | [diff] [blame] | 134 | info("One time not set, check user setting of power policy"); |
NodeMan97 | bcbee4a | 2022-05-27 15:28:45 -0500 | [diff] [blame] | 135 | |
Andrew Geissler | 35ca2e3 | 2021-02-09 13:54:26 -0600 | [diff] [blame] | 136 | auto reply = bus.call(methodUserSetting); |
| 137 | reply.read(result); |
| 138 | powerPolicy = std::get<std::string>(result); |
| 139 | } |
| 140 | else |
| 141 | { |
| 142 | // one_time setting was set so we're going to use it. Reset it |
| 143 | // to default for next time. |
Andrew Geissler | 8ffdb26 | 2021-09-20 15:25:19 -0500 | [diff] [blame] | 144 | info("One time set, use it and reset to default"); |
Andrew Geissler | 49e6713 | 2022-01-26 14:27:52 -0600 | [diff] [blame] | 145 | phosphor::state::manager::utils::setProperty( |
Pavithra Barithaya | 695888d | 2024-06-21 12:01:30 -0500 | [diff] [blame] | 146 | bus, settings.powerRestorePolicyOneTime, powerRestoreIntf, |
| 147 | "PowerRestorePolicy", |
Andrew Geissler | 49e6713 | 2022-01-26 14:27:52 -0600 | [diff] [blame] | 148 | convertForMessage(RestorePolicy::Policy::None)); |
Andrew Geissler | 35ca2e3 | 2021-02-09 13:54:26 -0600 | [diff] [blame] | 149 | } |
| 150 | |
NodeMan97 | 358de95 | 2022-07-20 08:31:36 -0500 | [diff] [blame] | 151 | auto methodUserSettingDelay = bus.new_method_call( |
| 152 | settings.service(settings.powerRestorePolicy, powerRestoreIntf) |
| 153 | .c_str(), |
| 154 | settings.powerRestorePolicy.c_str(), |
| 155 | "org.freedesktop.DBus.Properties", "Get"); |
| 156 | |
| 157 | methodUserSettingDelay.append(powerRestoreIntf, "PowerRestoreDelay"); |
| 158 | |
| 159 | std::variant<uint64_t> restoreDelay; |
| 160 | |
| 161 | auto delayResult = bus.call(methodUserSettingDelay); |
| 162 | delayResult.read(restoreDelay); |
| 163 | auto powerRestoreDelayUsec = |
| 164 | std::chrono::microseconds(std::get<uint64_t>(restoreDelay)); |
| 165 | auto powerRestoreDelaySec = |
| 166 | std::chrono::duration_cast<std::chrono::seconds>( |
| 167 | powerRestoreDelayUsec); |
| 168 | |
Andrew Geissler | 8ffdb26 | 2021-09-20 15:25:19 -0500 | [diff] [blame] | 169 | info("Host power is off, processing power policy {POWER_POLICY}", |
| 170 | "POWER_POLICY", powerPolicy); |
Andrew Geissler | 35ca2e3 | 2021-02-09 13:54:26 -0600 | [diff] [blame] | 171 | |
| 172 | if (RestorePolicy::Policy::AlwaysOn == |
| 173 | RestorePolicy::convertPolicyFromString(powerPolicy)) |
| 174 | { |
NodeMan97 | 358de95 | 2022-07-20 08:31:36 -0500 | [diff] [blame] | 175 | info( |
| 176 | "power_policy=ALWAYS_POWER_ON, powering host on ({DELAY}s delay)", |
| 177 | "DELAY", powerRestoreDelaySec.count()); |
Potin Lai | 0886545 | 2023-11-07 23:28:11 +0800 | [diff] [blame] | 178 | utils::waitBmcReady(bus, powerRestoreDelaySec); |
Andrew Geissler | 49e6713 | 2022-01-26 14:27:52 -0600 | [diff] [blame] | 179 | phosphor::state::manager::utils::setProperty( |
Amithash Prasasd | f566c96 | 2024-07-22 20:28:42 -0700 | [diff] [blame] | 180 | bus, hostPath, HostState::interface, "RestartCause", |
Andrew Geissler | 49e6713 | 2022-01-26 14:27:52 -0600 | [diff] [blame] | 181 | convertForMessage( |
| 182 | server::Host::RestartCause::PowerPolicyAlwaysOn)); |
| 183 | phosphor::state::manager::utils::setProperty( |
Amithash Prasasd | f566c96 | 2024-07-22 20:28:42 -0700 | [diff] [blame] | 184 | bus, hostPath, HostState::interface, "RequestedHostTransition", |
Andrew Geissler | 49e6713 | 2022-01-26 14:27:52 -0600 | [diff] [blame] | 185 | convertForMessage(server::Host::Transition::On)); |
Andrew Geissler | 35ca2e3 | 2021-02-09 13:54:26 -0600 | [diff] [blame] | 186 | } |
Andrew Geissler | 70b48f3 | 2022-08-18 11:48:24 -0400 | [diff] [blame] | 187 | // Always execute power on if AlwaysOn is set, otherwise check config |
| 188 | // option (and AC loss status) on whether to execute other policy |
| 189 | // settings |
Potin Lai | e960184 | 2023-03-03 17:23:28 +0800 | [diff] [blame] | 190 | #if ONLY_RUN_APR_ON_POWER_LOSS |
Andrew Geissler | 70b48f3 | 2022-08-18 11:48:24 -0400 | [diff] [blame] | 191 | else if (!phosphor::state::manager::utils::checkACLoss(hostId)) |
| 192 | { |
| 193 | info( |
| 194 | "Chassis power was not on prior to BMC reboot so do not run any further power policy"); |
| 195 | return 0; |
| 196 | } |
| 197 | #endif |
Thang Tran | 1fc4845 | 2022-02-08 16:04:11 +0700 | [diff] [blame] | 198 | else if (RestorePolicy::Policy::AlwaysOff == |
| 199 | RestorePolicy::convertPolicyFromString(powerPolicy)) |
| 200 | { |
NodeMan97 | 358de95 | 2022-07-20 08:31:36 -0500 | [diff] [blame] | 201 | info( |
| 202 | "power_policy=ALWAYS_POWER_OFF, set requested state to off ({DELAY}s delay)", |
| 203 | "DELAY", powerRestoreDelaySec.count()); |
Potin Lai | 0886545 | 2023-11-07 23:28:11 +0800 | [diff] [blame] | 204 | utils::waitBmcReady(bus, powerRestoreDelaySec); |
Thang Tran | 6810ad5 | 2022-02-24 09:36:57 +0700 | [diff] [blame] | 205 | // Read last requested state and re-request it to execute it |
| 206 | auto hostReqState = phosphor::state::manager::utils::getProperty( |
Amithash Prasasd | f566c96 | 2024-07-22 20:28:42 -0700 | [diff] [blame] | 207 | bus, hostPath, HostState::interface, "RequestedHostTransition"); |
NodeMan97 | 744fe62 | 2022-07-21 17:14:23 -0500 | [diff] [blame] | 208 | if (hostReqState != |
| 209 | convertForMessage(server::Host::Transition::Off)) |
Thang Tran | 6810ad5 | 2022-02-24 09:36:57 +0700 | [diff] [blame] | 210 | { |
| 211 | phosphor::state::manager::utils::setProperty( |
Amithash Prasasd | f566c96 | 2024-07-22 20:28:42 -0700 | [diff] [blame] | 212 | bus, hostPath, HostState::interface, |
| 213 | "RequestedHostTransition", |
Thang Tran | 6810ad5 | 2022-02-24 09:36:57 +0700 | [diff] [blame] | 214 | convertForMessage(server::Host::Transition::Off)); |
| 215 | } |
Thang Tran | 1fc4845 | 2022-02-08 16:04:11 +0700 | [diff] [blame] | 216 | } |
Andrew Geissler | 35ca2e3 | 2021-02-09 13:54:26 -0600 | [diff] [blame] | 217 | else if (RestorePolicy::Policy::Restore == |
| 218 | RestorePolicy::convertPolicyFromString(powerPolicy)) |
| 219 | { |
NodeMan97 | 358de95 | 2022-07-20 08:31:36 -0500 | [diff] [blame] | 220 | info("power_policy=RESTORE, restoring last state ({DELAY}s delay)", |
| 221 | "DELAY", powerRestoreDelaySec.count()); |
Potin Lai | 0886545 | 2023-11-07 23:28:11 +0800 | [diff] [blame] | 222 | utils::waitBmcReady(bus, powerRestoreDelaySec); |
Andrew Geissler | 35ca2e3 | 2021-02-09 13:54:26 -0600 | [diff] [blame] | 223 | // Read last requested state and re-request it to execute it |
Andrew Geissler | 49e6713 | 2022-01-26 14:27:52 -0600 | [diff] [blame] | 224 | auto hostReqState = phosphor::state::manager::utils::getProperty( |
Amithash Prasasd | f566c96 | 2024-07-22 20:28:42 -0700 | [diff] [blame] | 225 | bus, hostPath, HostState::interface, "RequestedHostTransition"); |
NodeMan97 | 744fe62 | 2022-07-21 17:14:23 -0500 | [diff] [blame] | 226 | |
| 227 | // As long as the host transition is not 'Off' power on host state. |
| 228 | if (hostReqState != |
| 229 | convertForMessage(server::Host::Transition::Off)) |
Thang Tran | 6810ad5 | 2022-02-24 09:36:57 +0700 | [diff] [blame] | 230 | { |
| 231 | phosphor::state::manager::utils::setProperty( |
Amithash Prasasd | f566c96 | 2024-07-22 20:28:42 -0700 | [diff] [blame] | 232 | bus, hostPath, HostState::interface, "RestartCause", |
Thang Tran | 6810ad5 | 2022-02-24 09:36:57 +0700 | [diff] [blame] | 233 | convertForMessage( |
| 234 | server::Host::RestartCause::PowerPolicyPreviousState)); |
| 235 | phosphor::state::manager::utils::setProperty( |
Amithash Prasasd | f566c96 | 2024-07-22 20:28:42 -0700 | [diff] [blame] | 236 | bus, hostPath, HostState::interface, |
| 237 | "RequestedHostTransition", |
NodeMan97 | 744fe62 | 2022-07-21 17:14:23 -0500 | [diff] [blame] | 238 | convertForMessage(server::Host::Transition::On)); |
Thang Tran | 6810ad5 | 2022-02-24 09:36:57 +0700 | [diff] [blame] | 239 | } |
Andrew Geissler | 35ca2e3 | 2021-02-09 13:54:26 -0600 | [diff] [blame] | 240 | } |
Anthony Wilson | 32c532e | 2018-10-25 21:56:07 -0500 | [diff] [blame] | 241 | } |
Patrick Williams | f053e6f | 2022-07-22 19:26:54 -0500 | [diff] [blame] | 242 | catch (const sdbusplus::exception_t& e) |
Anthony Wilson | 32c532e | 2018-10-25 21:56:07 -0500 | [diff] [blame] | 243 | { |
Andrew Geissler | 8ffdb26 | 2021-09-20 15:25:19 -0500 | [diff] [blame] | 244 | error("Error in PowerRestorePolicy Get: {ERROR}", "ERROR", e); |
Andrew Geissler | 033fc3b | 2017-08-30 15:11:44 -0500 | [diff] [blame] | 245 | elog<InternalFailure>(); |
| 246 | } |
Deepak Kodihalli | 55f132b | 2017-07-25 07:36:06 -0500 | [diff] [blame] | 247 | |
Michael Tritz | 3ed1008 | 2017-03-20 23:28:09 -0500 | [diff] [blame] | 248 | return 0; |
| 249 | } |