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