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