blob: 53a414f3177972038355ce13fa0bc8f3f25366c2 [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;
Michael Tritz3ed10082017-03-20 23:28:09 -050037
Michael Tritz3ed10082017-03-20 23:28:09 -050038} // namespace manager
39} // namespace state
Andrew Geisslera965cf02018-08-31 08:37:05 -070040} // namespace phosphor
Michael Tritz3ed10082017-03-20 23:28:09 -050041
Michael Tritzd50f6572017-09-05 16:38:11 -050042int main(int argc, char** argv)
Michael Tritz3ed10082017-03-20 23:28:09 -050043{
Deepak Kodihalli55f132b2017-07-25 07:36:06 -050044 using namespace phosphor::logging;
45
Potin Laic328a4c2022-03-18 23:49:37 +080046 size_t hostId = 0;
Michael Tritzd50f6572017-09-05 16:38:11 -050047 int arg;
48 int optIndex = 0;
49
Pavithra Barithayaf15b9542024-06-21 08:18:48 -050050 static struct option longOpts[] = {
51 {"host", required_argument, nullptr, 'h'}, {nullptr, 0, nullptr, 0}};
Michael Tritzd50f6572017-09-05 16:38:11 -050052
Andrew Geissler58a18012018-01-19 19:36:05 -080053 while ((arg = getopt_long(argc, argv, "h:", longOpts, &optIndex)) != -1)
Michael Tritzd50f6572017-09-05 16:38:11 -050054 {
55 switch (arg)
56 {
57 case 'h':
Potin Laic328a4c2022-03-18 23:49:37 +080058 hostId = std::stoul(optarg);
Michael Tritzd50f6572017-09-05 16:38:11 -050059 break;
60 default:
61 break;
62 }
63 }
64
Patrick Williams9a286db2024-01-17 06:29:47 -060065 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 Tritz3ed10082017-03-20 23:28:09 -050070 auto bus = sdbusplus::bus::new_default();
71
Deepak Kodihalli55f132b2017-07-25 07:36:06 -050072 using namespace settings;
Potin Laic328a4c2022-03-18 23:49:37 +080073 HostObjects settings(bus, hostId);
Deepak Kodihalli55f132b2017-07-25 07:36:06 -050074
Michael Tritz3ed10082017-03-20 23:28:09 -050075 using namespace phosphor::state::manager;
Patrick Williams7e969cb2023-08-23 16:24:23 -050076 namespace server = sdbusplus::server::xyz::openbmc_project::state;
Michael Tritz3ed10082017-03-20 23:28:09 -050077
Andrew Geissler033fc3b2017-08-30 15:11:44 -050078 // This application is only run if chassis power is off
Michael Tritz3ed10082017-03-20 23:28:09 -050079
Andrew Geisslerb2b3d9c2022-01-26 14:45:10 -060080 // If the BMC was rebooted due to a user initiated pinhole reset, do not
81 // implement any power restore policies
Patrick Williams9a286db2024-01-17 06:29:47 -060082 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 Barithaya695888d2024-06-21 12:01:30 -050089 bus, bmcPath.str, BMC_BUSNAME, "LastRebootCause"));
Patrick Williams9a286db2024-01-17 06:29:47 -060090
91 if (bmcRebootCause == BMC::RebootCause::PinholeReset)
Andrew Geisslerb2b3d9c2022-01-26 14:45:10 -060092 {
93 info(
94 "BMC was reset due to pinhole reset, no power restore policy will be run");
95 return 0;
96 }
Patrick Williams9a286db2024-01-17 06:29:47 -060097 else if (bmcRebootCause == BMC::RebootCause::Watchdog)
Thang Q. Nguyenb669ea32022-02-22 07:15:15 +000098 {
99 info(
100 "BMC was reset due to cold reset, no power restore policy will be run");
101 return 0;
102 }
Andrew Geisslerb2b3d9c2022-01-26 14:45:10 -0600103
Andrew Geissler35ca2e32021-02-09 13:54:26 -0600104 /* 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 Geissler58a18012018-01-19 19:36:05 -0800116 settings.service(settings.powerRestorePolicy, powerRestoreIntf).c_str(),
117 settings.powerRestorePolicy.c_str(), "org.freedesktop.DBus.Properties",
118 "Get");
Andrew Geissler35ca2e32021-02-09 13:54:26 -0600119 methodUserSetting.append(powerRestoreIntf, "PowerRestorePolicy");
Anthony Wilson32c532e2018-10-25 21:56:07 -0500120
Patrick Williams2975e262020-05-13 18:01:09 -0500121 std::variant<std::string> result;
Anthony Wilson32c532e2018-10-25 21:56:07 -0500122 try
Michael Tritz3ed10082017-03-20 23:28:09 -0500123 {
Andrew Geissler35ca2e32021-02-09 13:54:26 -0600124 auto reply = bus.call(methodOneTime);
Anthony Wilson32c532e2018-10-25 21:56:07 -0500125 reply.read(result);
Andrew Geissler35ca2e32021-02-09 13:54:26 -0600126 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 Geissler8ffdb262021-09-20 15:25:19 -0500132 info("One time not set, check user setting of power policy");
NodeMan97bcbee4a2022-05-27 15:28:45 -0500133
Andrew Geissler35ca2e32021-02-09 13:54:26 -0600134 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 Geissler8ffdb262021-09-20 15:25:19 -0500142 info("One time set, use it and reset to default");
Andrew Geissler49e67132022-01-26 14:27:52 -0600143 phosphor::state::manager::utils::setProperty(
Pavithra Barithaya695888d2024-06-21 12:01:30 -0500144 bus, settings.powerRestorePolicyOneTime, powerRestoreIntf,
145 "PowerRestorePolicy",
Andrew Geissler49e67132022-01-26 14:27:52 -0600146 convertForMessage(RestorePolicy::Policy::None));
Andrew Geissler35ca2e32021-02-09 13:54:26 -0600147 }
148
NodeMan97358de952022-07-20 08:31:36 -0500149 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 Geissler8ffdb262021-09-20 15:25:19 -0500167 info("Host power is off, processing power policy {POWER_POLICY}",
168 "POWER_POLICY", powerPolicy);
Andrew Geissler35ca2e32021-02-09 13:54:26 -0600169
170 if (RestorePolicy::Policy::AlwaysOn ==
171 RestorePolicy::convertPolicyFromString(powerPolicy))
172 {
NodeMan97358de952022-07-20 08:31:36 -0500173 info(
174 "power_policy=ALWAYS_POWER_ON, powering host on ({DELAY}s delay)",
175 "DELAY", powerRestoreDelaySec.count());
Potin Lai08865452023-11-07 23:28:11 +0800176 utils::waitBmcReady(bus, powerRestoreDelaySec);
Andrew Geissler49e67132022-01-26 14:27:52 -0600177 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 Geissler35ca2e32021-02-09 13:54:26 -0600184 }
Andrew Geissler70b48f32022-08-18 11:48:24 -0400185 // 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 Laie9601842023-03-03 17:23:28 +0800188#if ONLY_RUN_APR_ON_POWER_LOSS
Andrew Geissler70b48f32022-08-18 11:48:24 -0400189 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 Tran1fc48452022-02-08 16:04:11 +0700196 else if (RestorePolicy::Policy::AlwaysOff ==
197 RestorePolicy::convertPolicyFromString(powerPolicy))
198 {
NodeMan97358de952022-07-20 08:31:36 -0500199 info(
200 "power_policy=ALWAYS_POWER_OFF, set requested state to off ({DELAY}s delay)",
201 "DELAY", powerRestoreDelaySec.count());
Potin Lai08865452023-11-07 23:28:11 +0800202 utils::waitBmcReady(bus, powerRestoreDelaySec);
Thang Tran6810ad52022-02-24 09:36:57 +0700203 // 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");
NodeMan97744fe622022-07-21 17:14:23 -0500206 if (hostReqState !=
207 convertForMessage(server::Host::Transition::Off))
Thang Tran6810ad52022-02-24 09:36:57 +0700208 {
209 phosphor::state::manager::utils::setProperty(
210 bus, hostPath, HOST_BUSNAME, "RequestedHostTransition",
211 convertForMessage(server::Host::Transition::Off));
212 }
Thang Tran1fc48452022-02-08 16:04:11 +0700213 }
Andrew Geissler35ca2e32021-02-09 13:54:26 -0600214 else if (RestorePolicy::Policy::Restore ==
215 RestorePolicy::convertPolicyFromString(powerPolicy))
216 {
NodeMan97358de952022-07-20 08:31:36 -0500217 info("power_policy=RESTORE, restoring last state ({DELAY}s delay)",
218 "DELAY", powerRestoreDelaySec.count());
Potin Lai08865452023-11-07 23:28:11 +0800219 utils::waitBmcReady(bus, powerRestoreDelaySec);
Andrew Geissler35ca2e32021-02-09 13:54:26 -0600220 // Read last requested state and re-request it to execute it
Andrew Geissler49e67132022-01-26 14:27:52 -0600221 auto hostReqState = phosphor::state::manager::utils::getProperty(
222 bus, hostPath, HOST_BUSNAME, "RequestedHostTransition");
NodeMan97744fe622022-07-21 17:14:23 -0500223
224 // As long as the host transition is not 'Off' power on host state.
225 if (hostReqState !=
226 convertForMessage(server::Host::Transition::Off))
Thang Tran6810ad52022-02-24 09:36:57 +0700227 {
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",
NodeMan97744fe622022-07-21 17:14:23 -0500234 convertForMessage(server::Host::Transition::On));
Thang Tran6810ad52022-02-24 09:36:57 +0700235 }
Andrew Geissler35ca2e32021-02-09 13:54:26 -0600236 }
Anthony Wilson32c532e2018-10-25 21:56:07 -0500237 }
Patrick Williamsf053e6f2022-07-22 19:26:54 -0500238 catch (const sdbusplus::exception_t& e)
Anthony Wilson32c532e2018-10-25 21:56:07 -0500239 {
Andrew Geissler8ffdb262021-09-20 15:25:19 -0500240 error("Error in PowerRestorePolicy Get: {ERROR}", "ERROR", e);
Andrew Geissler033fc3b2017-08-30 15:11:44 -0500241 elog<InternalFailure>();
242 }
Deepak Kodihalli55f132b2017-07-25 07:36:06 -0500243
Michael Tritz3ed10082017-03-20 23:28:09 -0500244 return 0;
245}