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> |
| 16 | |
| 17 | #include <iostream> |
| 18 | #include <map> |
| 19 | #include <string> |
| 20 | |
Michael Tritz | 3ed1008 | 2017-03-20 23:28:09 -0500 | [diff] [blame] | 21 | namespace phosphor |
| 22 | { |
| 23 | namespace state |
| 24 | { |
| 25 | namespace manager |
| 26 | { |
| 27 | |
Andrew Geissler | 8ffdb26 | 2021-09-20 15:25:19 -0500 | [diff] [blame] | 28 | PHOSPHOR_LOG2_USING; |
| 29 | |
Michael Tritz | 3ed1008 | 2017-03-20 23:28:09 -0500 | [diff] [blame] | 30 | using namespace phosphor::logging; |
Deepak Kodihalli | 55f132b | 2017-07-25 07:36:06 -0500 | [diff] [blame] | 31 | using namespace sdbusplus::xyz::openbmc_project::Common::Error; |
| 32 | using namespace sdbusplus::xyz::openbmc_project::Control::Power::server; |
Michael Tritz | 3ed1008 | 2017-03-20 23:28:09 -0500 | [diff] [blame] | 33 | |
Michael Tritz | 3ed1008 | 2017-03-20 23:28:09 -0500 | [diff] [blame] | 34 | } // namespace manager |
| 35 | } // namespace state |
Andrew Geissler | a965cf0 | 2018-08-31 08:37:05 -0700 | [diff] [blame] | 36 | } // namespace phosphor |
Michael Tritz | 3ed1008 | 2017-03-20 23:28:09 -0500 | [diff] [blame] | 37 | |
Michael Tritz | d50f657 | 2017-09-05 16:38:11 -0500 | [diff] [blame] | 38 | int main(int argc, char** argv) |
Michael Tritz | 3ed1008 | 2017-03-20 23:28:09 -0500 | [diff] [blame] | 39 | { |
Deepak Kodihalli | 55f132b | 2017-07-25 07:36:06 -0500 | [diff] [blame] | 40 | using namespace phosphor::logging; |
| 41 | |
Potin Lai | c328a4c | 2022-03-18 23:49:37 +0800 | [diff] [blame] | 42 | size_t hostId = 0; |
Michael Tritz | d50f657 | 2017-09-05 16:38:11 -0500 | [diff] [blame] | 43 | std::string hostPath = "/xyz/openbmc_project/state/host0"; |
| 44 | int arg; |
| 45 | int optIndex = 0; |
| 46 | |
Andrew Geissler | 58a1801 | 2018-01-19 19:36:05 -0800 | [diff] [blame] | 47 | static struct option longOpts[] = {{"host", required_argument, 0, 'h'}, |
| 48 | {0, 0, 0, 0}}; |
Michael Tritz | d50f657 | 2017-09-05 16:38:11 -0500 | [diff] [blame] | 49 | |
Andrew Geissler | 58a1801 | 2018-01-19 19:36:05 -0800 | [diff] [blame] | 50 | while ((arg = getopt_long(argc, argv, "h:", longOpts, &optIndex)) != -1) |
Michael Tritz | d50f657 | 2017-09-05 16:38:11 -0500 | [diff] [blame] | 51 | { |
| 52 | switch (arg) |
| 53 | { |
| 54 | case 'h': |
Potin Lai | c328a4c | 2022-03-18 23:49:37 +0800 | [diff] [blame] | 55 | hostId = std::stoul(optarg); |
Andrew Geissler | 58a1801 | 2018-01-19 19:36:05 -0800 | [diff] [blame] | 56 | hostPath = |
| 57 | std::string("/xyz/openbmc_project/state/host") + optarg; |
Michael Tritz | d50f657 | 2017-09-05 16:38:11 -0500 | [diff] [blame] | 58 | break; |
| 59 | default: |
| 60 | break; |
| 61 | } |
| 62 | } |
| 63 | |
Michael Tritz | 3ed1008 | 2017-03-20 23:28:09 -0500 | [diff] [blame] | 64 | auto bus = sdbusplus::bus::new_default(); |
| 65 | |
Deepak Kodihalli | 55f132b | 2017-07-25 07:36:06 -0500 | [diff] [blame] | 66 | using namespace settings; |
Potin Lai | c328a4c | 2022-03-18 23:49:37 +0800 | [diff] [blame] | 67 | HostObjects settings(bus, hostId); |
Deepak Kodihalli | 55f132b | 2017-07-25 07:36:06 -0500 | [diff] [blame] | 68 | |
Michael Tritz | 3ed1008 | 2017-03-20 23:28:09 -0500 | [diff] [blame] | 69 | using namespace phosphor::state::manager; |
| 70 | namespace server = sdbusplus::xyz::openbmc_project::State::server; |
| 71 | |
Andrew Geissler | 033fc3b | 2017-08-30 15:11:44 -0500 | [diff] [blame] | 72 | // This application is only run if chassis power is off |
Michael Tritz | 3ed1008 | 2017-03-20 23:28:09 -0500 | [diff] [blame] | 73 | |
Andrew Geissler | b2b3d9c | 2022-01-26 14:45:10 -0600 | [diff] [blame] | 74 | // If the BMC was rebooted due to a user initiated pinhole reset, do not |
| 75 | // implement any power restore policies |
| 76 | auto bmcRebootCause = phosphor::state::manager::utils::getProperty( |
| 77 | bus, "/xyz/openbmc_project/state/bmc0", BMC_BUSNAME, "LastRebootCause"); |
| 78 | if (bmcRebootCause == |
| 79 | "xyz.openbmc_project.State.BMC.RebootCause.PinholeReset") |
| 80 | { |
| 81 | info( |
| 82 | "BMC was reset due to pinhole reset, no power restore policy will be run"); |
| 83 | return 0; |
| 84 | } |
Thang Q. Nguyen | b669ea3 | 2022-02-22 07:15:15 +0000 | [diff] [blame] | 85 | else if (bmcRebootCause == |
| 86 | "xyz.openbmc_project.State.BMC.RebootCause.Watchdog") |
| 87 | { |
| 88 | info( |
| 89 | "BMC was reset due to cold reset, no power restore policy will be run"); |
| 90 | return 0; |
| 91 | } |
Andrew Geissler | b2b3d9c | 2022-01-26 14:45:10 -0600 | [diff] [blame] | 92 | |
Andrew Geissler | 35ca2e3 | 2021-02-09 13:54:26 -0600 | [diff] [blame] | 93 | /* The logic here is to first check the one-time PowerRestorePolicy setting. |
| 94 | * If this property is not the default then look at the persistent |
| 95 | * user setting in the non one-time object, otherwise honor the one-time |
| 96 | * setting. |
| 97 | */ |
| 98 | auto methodOneTime = bus.new_method_call( |
| 99 | settings.service(settings.powerRestorePolicy, powerRestoreIntf).c_str(), |
| 100 | settings.powerRestorePolicyOneTime.c_str(), |
| 101 | "org.freedesktop.DBus.Properties", "Get"); |
| 102 | methodOneTime.append(powerRestoreIntf, "PowerRestorePolicy"); |
| 103 | |
| 104 | auto methodUserSetting = bus.new_method_call( |
Andrew Geissler | 58a1801 | 2018-01-19 19:36:05 -0800 | [diff] [blame] | 105 | settings.service(settings.powerRestorePolicy, powerRestoreIntf).c_str(), |
| 106 | settings.powerRestorePolicy.c_str(), "org.freedesktop.DBus.Properties", |
| 107 | "Get"); |
Andrew Geissler | 35ca2e3 | 2021-02-09 13:54:26 -0600 | [diff] [blame] | 108 | methodUserSetting.append(powerRestoreIntf, "PowerRestorePolicy"); |
Anthony Wilson | 32c532e | 2018-10-25 21:56:07 -0500 | [diff] [blame] | 109 | |
Patrick Williams | 2975e26 | 2020-05-13 18:01:09 -0500 | [diff] [blame] | 110 | std::variant<std::string> result; |
Anthony Wilson | 32c532e | 2018-10-25 21:56:07 -0500 | [diff] [blame] | 111 | try |
Michael Tritz | 3ed1008 | 2017-03-20 23:28:09 -0500 | [diff] [blame] | 112 | { |
Andrew Geissler | 35ca2e3 | 2021-02-09 13:54:26 -0600 | [diff] [blame] | 113 | auto reply = bus.call(methodOneTime); |
Anthony Wilson | 32c532e | 2018-10-25 21:56:07 -0500 | [diff] [blame] | 114 | reply.read(result); |
Andrew Geissler | 35ca2e3 | 2021-02-09 13:54:26 -0600 | [diff] [blame] | 115 | auto powerPolicy = std::get<std::string>(result); |
| 116 | |
| 117 | if (RestorePolicy::Policy::None == |
| 118 | RestorePolicy::convertPolicyFromString(powerPolicy)) |
| 119 | { |
| 120 | // one_time is set to None so use the customer setting |
Andrew Geissler | 8ffdb26 | 2021-09-20 15:25:19 -0500 | [diff] [blame] | 121 | info("One time not set, check user setting of power policy"); |
Andrew Geissler | 35ca2e3 | 2021-02-09 13:54:26 -0600 | [diff] [blame] | 122 | auto reply = bus.call(methodUserSetting); |
| 123 | reply.read(result); |
| 124 | powerPolicy = std::get<std::string>(result); |
| 125 | } |
| 126 | else |
| 127 | { |
| 128 | // one_time setting was set so we're going to use it. Reset it |
| 129 | // to default for next time. |
Andrew Geissler | 8ffdb26 | 2021-09-20 15:25:19 -0500 | [diff] [blame] | 130 | info("One time set, use it and reset to default"); |
Andrew Geissler | 49e6713 | 2022-01-26 14:27:52 -0600 | [diff] [blame] | 131 | phosphor::state::manager::utils::setProperty( |
| 132 | bus, settings.powerRestorePolicyOneTime.c_str(), |
| 133 | powerRestoreIntf, "PowerRestorePolicy", |
| 134 | convertForMessage(RestorePolicy::Policy::None)); |
Andrew Geissler | 35ca2e3 | 2021-02-09 13:54:26 -0600 | [diff] [blame] | 135 | } |
| 136 | |
Andrew Geissler | 8ffdb26 | 2021-09-20 15:25:19 -0500 | [diff] [blame] | 137 | info("Host power is off, processing power policy {POWER_POLICY}", |
| 138 | "POWER_POLICY", powerPolicy); |
Andrew Geissler | 35ca2e3 | 2021-02-09 13:54:26 -0600 | [diff] [blame] | 139 | |
| 140 | if (RestorePolicy::Policy::AlwaysOn == |
| 141 | RestorePolicy::convertPolicyFromString(powerPolicy)) |
| 142 | { |
Andrew Geissler | 8ffdb26 | 2021-09-20 15:25:19 -0500 | [diff] [blame] | 143 | info("power_policy=ALWAYS_POWER_ON, powering host on"); |
Andrew Geissler | 49e6713 | 2022-01-26 14:27:52 -0600 | [diff] [blame] | 144 | phosphor::state::manager::utils::setProperty( |
| 145 | bus, hostPath, HOST_BUSNAME, "RestartCause", |
| 146 | convertForMessage( |
| 147 | server::Host::RestartCause::PowerPolicyAlwaysOn)); |
| 148 | phosphor::state::manager::utils::setProperty( |
| 149 | bus, hostPath, HOST_BUSNAME, "RequestedHostTransition", |
| 150 | convertForMessage(server::Host::Transition::On)); |
Andrew Geissler | 35ca2e3 | 2021-02-09 13:54:26 -0600 | [diff] [blame] | 151 | } |
Thang Tran | 1fc4845 | 2022-02-08 16:04:11 +0700 | [diff] [blame] | 152 | else if (RestorePolicy::Policy::AlwaysOff == |
| 153 | RestorePolicy::convertPolicyFromString(powerPolicy)) |
| 154 | { |
| 155 | info("power_policy=ALWAYS_POWER_OFF, set requested state to off"); |
Thang Tran | 6810ad5 | 2022-02-24 09:36:57 +0700 | [diff] [blame] | 156 | // Read last requested state and re-request it to execute it |
| 157 | auto hostReqState = phosphor::state::manager::utils::getProperty( |
| 158 | bus, hostPath, HOST_BUSNAME, "RequestedHostTransition"); |
| 159 | if (hostReqState == convertForMessage(server::Host::Transition::On)) |
| 160 | { |
| 161 | phosphor::state::manager::utils::setProperty( |
| 162 | bus, hostPath, HOST_BUSNAME, "RequestedHostTransition", |
| 163 | convertForMessage(server::Host::Transition::Off)); |
| 164 | } |
Thang Tran | 1fc4845 | 2022-02-08 16:04:11 +0700 | [diff] [blame] | 165 | } |
Andrew Geissler | 35ca2e3 | 2021-02-09 13:54:26 -0600 | [diff] [blame] | 166 | else if (RestorePolicy::Policy::Restore == |
| 167 | RestorePolicy::convertPolicyFromString(powerPolicy)) |
| 168 | { |
Andrew Geissler | 8ffdb26 | 2021-09-20 15:25:19 -0500 | [diff] [blame] | 169 | info("power_policy=RESTORE, restoring last state"); |
Andrew Geissler | 35ca2e3 | 2021-02-09 13:54:26 -0600 | [diff] [blame] | 170 | // Read last requested state and re-request it to execute it |
Andrew Geissler | 49e6713 | 2022-01-26 14:27:52 -0600 | [diff] [blame] | 171 | auto hostReqState = phosphor::state::manager::utils::getProperty( |
| 172 | bus, hostPath, HOST_BUSNAME, "RequestedHostTransition"); |
Thang Tran | 6810ad5 | 2022-02-24 09:36:57 +0700 | [diff] [blame] | 173 | if (hostReqState == convertForMessage(server::Host::Transition::On)) |
| 174 | { |
| 175 | phosphor::state::manager::utils::setProperty( |
| 176 | bus, hostPath, HOST_BUSNAME, "RestartCause", |
| 177 | convertForMessage( |
| 178 | server::Host::RestartCause::PowerPolicyPreviousState)); |
| 179 | phosphor::state::manager::utils::setProperty( |
| 180 | bus, hostPath, HOST_BUSNAME, "RequestedHostTransition", |
| 181 | hostReqState); |
| 182 | } |
Andrew Geissler | 35ca2e3 | 2021-02-09 13:54:26 -0600 | [diff] [blame] | 183 | } |
Anthony Wilson | 32c532e | 2018-10-25 21:56:07 -0500 | [diff] [blame] | 184 | } |
Patrick Williams | 0a67521 | 2021-09-02 09:49:43 -0500 | [diff] [blame] | 185 | catch (const sdbusplus::exception::exception& e) |
Anthony Wilson | 32c532e | 2018-10-25 21:56:07 -0500 | [diff] [blame] | 186 | { |
Andrew Geissler | 8ffdb26 | 2021-09-20 15:25:19 -0500 | [diff] [blame] | 187 | error("Error in PowerRestorePolicy Get: {ERROR}", "ERROR", e); |
Andrew Geissler | 033fc3b | 2017-08-30 15:11:44 -0500 | [diff] [blame] | 188 | elog<InternalFailure>(); |
| 189 | } |
Deepak Kodihalli | 55f132b | 2017-07-25 07:36:06 -0500 | [diff] [blame] | 190 | |
Michael Tritz | 3ed1008 | 2017-03-20 23:28:09 -0500 | [diff] [blame] | 191 | return 0; |
| 192 | } |