blob: cadaedd248dfc62e677933d221cd31f8f8e6a7fb [file] [log] [blame]
Andrew Geissler769a62f2019-12-06 13:36:08 -06001#include "config.h"
Andrew Geisslere426b582020-05-28 12:40:55 -05002
Michael Tritz3ed10082017-03-20 23:28:09 -05003#include "host_state_manager.hpp"
Deepak Kodihalli55f132b2017-07-25 07:36:06 -05004#include "settings.hpp"
Andrew Geissler49e67132022-01-26 14:27:52 -06005#include "utils.hpp"
Deepak Kodihalli55f132b2017-07-25 07:36:06 -05006#include "xyz/openbmc_project/Common/error.hpp"
7#include "xyz/openbmc_project/Control/Power/RestorePolicy/server.hpp"
Michael Tritz3ed10082017-03-20 23:28:09 -05008
Andrew Geisslere426b582020-05-28 12:40:55 -05009#include <getopt.h>
10#include <systemd/sd-bus.h>
11
12#include <phosphor-logging/elog-errors.hpp>
Andrew Geissler8ffdb262021-09-20 15:25:19 -050013#include <phosphor-logging/lg2.hpp>
Andrew Geisslere426b582020-05-28 12:40:55 -050014#include <sdbusplus/exception.hpp>
15#include <sdbusplus/server.hpp>
Patrick Williams9a286db2024-01-17 06:29:47 -060016#include <xyz/openbmc_project/State/BMC/client.hpp>
17#include <xyz/openbmc_project/State/Host/client.hpp>
Andrew Geisslere426b582020-05-28 12:40:55 -050018
NodeMan97bcbee4a2022-05-27 15:28:45 -050019#include <filesystem>
Andrew Geisslere426b582020-05-28 12:40:55 -050020#include <iostream>
21#include <map>
22#include <string>
NodeMan97358de952022-07-20 08:31:36 -050023#include <thread>
Andrew Geisslere426b582020-05-28 12:40:55 -050024
Michael Tritz3ed10082017-03-20 23:28:09 -050025namespace phosphor
26{
27namespace state
28{
29namespace manager
30{
31
Andrew Geissler8ffdb262021-09-20 15:25:19 -050032PHOSPHOR_LOG2_USING;
33
Michael Tritz3ed10082017-03-20 23:28:09 -050034using namespace phosphor::logging;
Deepak Kodihalli55f132b2017-07-25 07:36:06 -050035using namespace sdbusplus::xyz::openbmc_project::Common::Error;
Patrick Williams7e969cb2023-08-23 16:24:23 -050036using namespace sdbusplus::server::xyz::openbmc_project::control::power;
Amithash Prasasdf566c962024-07-22 20:28:42 -070037using HostState = sdbusplus::client::xyz::openbmc_project::state::Host<>;
38using BMCState = sdbusplus::client::xyz::openbmc_project::state::BMC<>;
Michael Tritz3ed10082017-03-20 23:28:09 -050039
Michael Tritz3ed10082017-03-20 23:28:09 -050040} // namespace manager
41} // namespace state
Andrew Geisslera965cf02018-08-31 08:37:05 -070042} // namespace phosphor
Michael Tritz3ed10082017-03-20 23:28:09 -050043
Michael Tritzd50f6572017-09-05 16:38:11 -050044int main(int argc, char** argv)
Michael Tritz3ed10082017-03-20 23:28:09 -050045{
Deepak Kodihalli55f132b2017-07-25 07:36:06 -050046 using namespace phosphor::logging;
47
Potin Laic328a4c2022-03-18 23:49:37 +080048 size_t hostId = 0;
Michael Tritzd50f6572017-09-05 16:38:11 -050049 int arg;
50 int optIndex = 0;
51
Pavithra Barithayaf15b9542024-06-21 08:18:48 -050052 static struct option longOpts[] = {
53 {"host", required_argument, nullptr, 'h'}, {nullptr, 0, nullptr, 0}};
Michael Tritzd50f6572017-09-05 16:38:11 -050054
Andrew Geissler58a18012018-01-19 19:36:05 -080055 while ((arg = getopt_long(argc, argv, "h:", longOpts, &optIndex)) != -1)
Michael Tritzd50f6572017-09-05 16:38:11 -050056 {
57 switch (arg)
58 {
59 case 'h':
Potin Laic328a4c2022-03-18 23:49:37 +080060 hostId = std::stoul(optarg);
Michael Tritzd50f6572017-09-05 16:38:11 -050061 break;
62 default:
63 break;
64 }
65 }
66
Patrick Williams9a286db2024-01-17 06:29:47 -060067 using Host = sdbusplus::client::xyz::openbmc_project::state::Host<>;
Patrick Williams1b2c3c02024-08-16 15:20:29 -040068 std::string hostPath =
69 std::string(Host::namespace_path::value) + "/" +
70 std::string(Host::namespace_path::host) + std::to_string(hostId);
Patrick Williams9a286db2024-01-17 06:29:47 -060071
Michael Tritz3ed10082017-03-20 23:28:09 -050072 auto bus = sdbusplus::bus::new_default();
73
Deepak Kodihalli55f132b2017-07-25 07:36:06 -050074 using namespace settings;
Potin Laic328a4c2022-03-18 23:49:37 +080075 HostObjects settings(bus, hostId);
Deepak Kodihalli55f132b2017-07-25 07:36:06 -050076
Michael Tritz3ed10082017-03-20 23:28:09 -050077 using namespace phosphor::state::manager;
Patrick Williams7e969cb2023-08-23 16:24:23 -050078 namespace server = sdbusplus::server::xyz::openbmc_project::state;
Michael Tritz3ed10082017-03-20 23:28:09 -050079
Andrew Geissler033fc3b2017-08-30 15:11:44 -050080 // This application is only run if chassis power is off
Michael Tritz3ed10082017-03-20 23:28:09 -050081
Andrew Geisslerb2b3d9c2022-01-26 14:45:10 -060082 // If the BMC was rebooted due to a user initiated pinhole reset, do not
83 // implement any power restore policies
Patrick Williams9a286db2024-01-17 06:29:47 -060084 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 Prasasdf566c962024-07-22 20:28:42 -070091 bus, bmcPath.str, BMCState::interface, "LastRebootCause"));
Patrick Williams9a286db2024-01-17 06:29:47 -060092
93 if (bmcRebootCause == BMC::RebootCause::PinholeReset)
Andrew Geisslerb2b3d9c2022-01-26 14:45:10 -060094 {
95 info(
96 "BMC was reset due to pinhole reset, no power restore policy will be run");
97 return 0;
98 }
Patrick Williams9a286db2024-01-17 06:29:47 -060099 else if (bmcRebootCause == BMC::RebootCause::Watchdog)
Thang Q. Nguyenb669ea32022-02-22 07:15:15 +0000100 {
101 info(
102 "BMC was reset due to cold reset, no power restore policy will be run");
103 return 0;
104 }
Andrew Geisslerb2b3d9c2022-01-26 14:45:10 -0600105
Andrew Geissler35ca2e32021-02-09 13:54:26 -0600106 /* 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 Geissler58a18012018-01-19 19:36:05 -0800118 settings.service(settings.powerRestorePolicy, powerRestoreIntf).c_str(),
119 settings.powerRestorePolicy.c_str(), "org.freedesktop.DBus.Properties",
120 "Get");
Andrew Geissler35ca2e32021-02-09 13:54:26 -0600121 methodUserSetting.append(powerRestoreIntf, "PowerRestorePolicy");
Anthony Wilson32c532e2018-10-25 21:56:07 -0500122
Patrick Williams2975e262020-05-13 18:01:09 -0500123 std::variant<std::string> result;
Anthony Wilson32c532e2018-10-25 21:56:07 -0500124 try
Michael Tritz3ed10082017-03-20 23:28:09 -0500125 {
Andrew Geissler35ca2e32021-02-09 13:54:26 -0600126 auto reply = bus.call(methodOneTime);
Anthony Wilson32c532e2018-10-25 21:56:07 -0500127 reply.read(result);
Andrew Geissler35ca2e32021-02-09 13:54:26 -0600128 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 Geissler8ffdb262021-09-20 15:25:19 -0500134 info("One time not set, check user setting of power policy");
NodeMan97bcbee4a2022-05-27 15:28:45 -0500135
Andrew Geissler35ca2e32021-02-09 13:54:26 -0600136 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 Geissler8ffdb262021-09-20 15:25:19 -0500144 info("One time set, use it and reset to default");
Andrew Geissler49e67132022-01-26 14:27:52 -0600145 phosphor::state::manager::utils::setProperty(
Pavithra Barithaya695888d2024-06-21 12:01:30 -0500146 bus, settings.powerRestorePolicyOneTime, powerRestoreIntf,
147 "PowerRestorePolicy",
Andrew Geissler49e67132022-01-26 14:27:52 -0600148 convertForMessage(RestorePolicy::Policy::None));
Andrew Geissler35ca2e32021-02-09 13:54:26 -0600149 }
150
NodeMan97358de952022-07-20 08:31:36 -0500151 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 Geissler8ffdb262021-09-20 15:25:19 -0500169 info("Host power is off, processing power policy {POWER_POLICY}",
170 "POWER_POLICY", powerPolicy);
Andrew Geissler35ca2e32021-02-09 13:54:26 -0600171
172 if (RestorePolicy::Policy::AlwaysOn ==
173 RestorePolicy::convertPolicyFromString(powerPolicy))
174 {
NodeMan97358de952022-07-20 08:31:36 -0500175 info(
176 "power_policy=ALWAYS_POWER_ON, powering host on ({DELAY}s delay)",
177 "DELAY", powerRestoreDelaySec.count());
Potin Lai08865452023-11-07 23:28:11 +0800178 utils::waitBmcReady(bus, powerRestoreDelaySec);
Andrew Geissler49e67132022-01-26 14:27:52 -0600179 phosphor::state::manager::utils::setProperty(
Amithash Prasasdf566c962024-07-22 20:28:42 -0700180 bus, hostPath, HostState::interface, "RestartCause",
Andrew Geissler49e67132022-01-26 14:27:52 -0600181 convertForMessage(
182 server::Host::RestartCause::PowerPolicyAlwaysOn));
183 phosphor::state::manager::utils::setProperty(
Amithash Prasasdf566c962024-07-22 20:28:42 -0700184 bus, hostPath, HostState::interface, "RequestedHostTransition",
Andrew Geissler49e67132022-01-26 14:27:52 -0600185 convertForMessage(server::Host::Transition::On));
Andrew Geissler35ca2e32021-02-09 13:54:26 -0600186 }
Andrew Geissler70b48f32022-08-18 11:48:24 -0400187 // 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 Laie9601842023-03-03 17:23:28 +0800190#if ONLY_RUN_APR_ON_POWER_LOSS
Andrew Geissler70b48f32022-08-18 11:48:24 -0400191 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 Tran1fc48452022-02-08 16:04:11 +0700198 else if (RestorePolicy::Policy::AlwaysOff ==
199 RestorePolicy::convertPolicyFromString(powerPolicy))
200 {
NodeMan97358de952022-07-20 08:31:36 -0500201 info(
202 "power_policy=ALWAYS_POWER_OFF, set requested state to off ({DELAY}s delay)",
203 "DELAY", powerRestoreDelaySec.count());
Potin Lai08865452023-11-07 23:28:11 +0800204 utils::waitBmcReady(bus, powerRestoreDelaySec);
Thang Tran6810ad52022-02-24 09:36:57 +0700205 // Read last requested state and re-request it to execute it
206 auto hostReqState = phosphor::state::manager::utils::getProperty(
Amithash Prasasdf566c962024-07-22 20:28:42 -0700207 bus, hostPath, HostState::interface, "RequestedHostTransition");
NodeMan97744fe622022-07-21 17:14:23 -0500208 if (hostReqState !=
209 convertForMessage(server::Host::Transition::Off))
Thang Tran6810ad52022-02-24 09:36:57 +0700210 {
211 phosphor::state::manager::utils::setProperty(
Amithash Prasasdf566c962024-07-22 20:28:42 -0700212 bus, hostPath, HostState::interface,
213 "RequestedHostTransition",
Thang Tran6810ad52022-02-24 09:36:57 +0700214 convertForMessage(server::Host::Transition::Off));
215 }
Thang Tran1fc48452022-02-08 16:04:11 +0700216 }
Andrew Geissler35ca2e32021-02-09 13:54:26 -0600217 else if (RestorePolicy::Policy::Restore ==
218 RestorePolicy::convertPolicyFromString(powerPolicy))
219 {
NodeMan97358de952022-07-20 08:31:36 -0500220 info("power_policy=RESTORE, restoring last state ({DELAY}s delay)",
221 "DELAY", powerRestoreDelaySec.count());
Potin Lai08865452023-11-07 23:28:11 +0800222 utils::waitBmcReady(bus, powerRestoreDelaySec);
Andrew Geissler35ca2e32021-02-09 13:54:26 -0600223 // Read last requested state and re-request it to execute it
Andrew Geissler49e67132022-01-26 14:27:52 -0600224 auto hostReqState = phosphor::state::manager::utils::getProperty(
Amithash Prasasdf566c962024-07-22 20:28:42 -0700225 bus, hostPath, HostState::interface, "RequestedHostTransition");
NodeMan97744fe622022-07-21 17:14:23 -0500226
227 // As long as the host transition is not 'Off' power on host state.
228 if (hostReqState !=
229 convertForMessage(server::Host::Transition::Off))
Thang Tran6810ad52022-02-24 09:36:57 +0700230 {
231 phosphor::state::manager::utils::setProperty(
Amithash Prasasdf566c962024-07-22 20:28:42 -0700232 bus, hostPath, HostState::interface, "RestartCause",
Thang Tran6810ad52022-02-24 09:36:57 +0700233 convertForMessage(
234 server::Host::RestartCause::PowerPolicyPreviousState));
235 phosphor::state::manager::utils::setProperty(
Amithash Prasasdf566c962024-07-22 20:28:42 -0700236 bus, hostPath, HostState::interface,
237 "RequestedHostTransition",
NodeMan97744fe622022-07-21 17:14:23 -0500238 convertForMessage(server::Host::Transition::On));
Thang Tran6810ad52022-02-24 09:36:57 +0700239 }
Andrew Geissler35ca2e32021-02-09 13:54:26 -0600240 }
Anthony Wilson32c532e2018-10-25 21:56:07 -0500241 }
Patrick Williamsf053e6f2022-07-22 19:26:54 -0500242 catch (const sdbusplus::exception_t& e)
Anthony Wilson32c532e2018-10-25 21:56:07 -0500243 {
Andrew Geissler8ffdb262021-09-20 15:25:19 -0500244 error("Error in PowerRestorePolicy Get: {ERROR}", "ERROR", e);
Andrew Geissler033fc3b2017-08-30 15:11:44 -0500245 elog<InternalFailure>();
246 }
Deepak Kodihalli55f132b2017-07-25 07:36:06 -0500247
Michael Tritz3ed10082017-03-20 23:28:09 -0500248 return 0;
249}