blob: c94faaf08990e2990e36f305bebe0d6fdbee2b80 [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
NodeMan97bcbee4a2022-05-27 15:28:45 -05009#include <fmt/format.h>
10#include <fmt/printf.h>
Andrew Geisslere426b582020-05-28 12:40:55 -050011#include <getopt.h>
12#include <systemd/sd-bus.h>
13
14#include <phosphor-logging/elog-errors.hpp>
Andrew Geissler8ffdb262021-09-20 15:25:19 -050015#include <phosphor-logging/lg2.hpp>
Andrew Geisslere426b582020-05-28 12:40:55 -050016#include <sdbusplus/exception.hpp>
17#include <sdbusplus/server.hpp>
18
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;
36using namespace sdbusplus::xyz::openbmc_project::Control::Power::server;
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 std::string hostPath = "/xyz/openbmc_project/state/host0";
48 int arg;
49 int optIndex = 0;
50
Andrew Geissler58a18012018-01-19 19:36:05 -080051 static struct option longOpts[] = {{"host", required_argument, 0, 'h'},
52 {0, 0, 0, 0}};
Michael Tritzd50f6572017-09-05 16:38:11 -050053
Andrew Geissler58a18012018-01-19 19:36:05 -080054 while ((arg = getopt_long(argc, argv, "h:", longOpts, &optIndex)) != -1)
Michael Tritzd50f6572017-09-05 16:38:11 -050055 {
56 switch (arg)
57 {
58 case 'h':
Potin Laic328a4c2022-03-18 23:49:37 +080059 hostId = std::stoul(optarg);
Andrew Geissler58a18012018-01-19 19:36:05 -080060 hostPath =
61 std::string("/xyz/openbmc_project/state/host") + optarg;
Michael Tritzd50f6572017-09-05 16:38:11 -050062 break;
63 default:
64 break;
65 }
66 }
67
Michael Tritz3ed10082017-03-20 23:28:09 -050068 auto bus = sdbusplus::bus::new_default();
69
Deepak Kodihalli55f132b2017-07-25 07:36:06 -050070 using namespace settings;
Potin Laic328a4c2022-03-18 23:49:37 +080071 HostObjects settings(bus, hostId);
Deepak Kodihalli55f132b2017-07-25 07:36:06 -050072
Michael Tritz3ed10082017-03-20 23:28:09 -050073 using namespace phosphor::state::manager;
74 namespace server = sdbusplus::xyz::openbmc_project::State::server;
75
Andrew Geissler033fc3b2017-08-30 15:11:44 -050076 // This application is only run if chassis power is off
Michael Tritz3ed10082017-03-20 23:28:09 -050077
Andrew Geisslerb2b3d9c2022-01-26 14:45:10 -060078 // 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. Nguyenb669ea32022-02-22 07:15:15 +000089 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 Geisslerb2b3d9c2022-01-26 14:45:10 -060096
Andrew Geissler35ca2e32021-02-09 13:54:26 -060097 /* 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 Geissler58a18012018-01-19 19:36:05 -0800109 settings.service(settings.powerRestorePolicy, powerRestoreIntf).c_str(),
110 settings.powerRestorePolicy.c_str(), "org.freedesktop.DBus.Properties",
111 "Get");
Andrew Geissler35ca2e32021-02-09 13:54:26 -0600112 methodUserSetting.append(powerRestoreIntf, "PowerRestorePolicy");
Anthony Wilson32c532e2018-10-25 21:56:07 -0500113
Patrick Williams2975e262020-05-13 18:01:09 -0500114 std::variant<std::string> result;
Anthony Wilson32c532e2018-10-25 21:56:07 -0500115 try
Michael Tritz3ed10082017-03-20 23:28:09 -0500116 {
Andrew Geissler35ca2e32021-02-09 13:54:26 -0600117 auto reply = bus.call(methodOneTime);
Anthony Wilson32c532e2018-10-25 21:56:07 -0500118 reply.read(result);
Andrew Geissler35ca2e32021-02-09 13:54:26 -0600119 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 Geissler8ffdb262021-09-20 15:25:19 -0500125 info("One time not set, check user setting of power policy");
NodeMan97bcbee4a2022-05-27 15:28:45 -0500126
127#ifdef ONLY_RUN_APR_ON_POWER_LOSS
NodeMan9727d1e142022-07-27 15:10:07 -0500128 if (!phosphor::state::manager::utils::checkACLoss(hostId))
NodeMan97bcbee4a2022-05-27 15:28:45 -0500129 {
130 info(
131 "Chassis power was not on prior to BMC reboot so do not run any power policy");
132 return 0;
133 }
134#endif
Andrew Geissler35ca2e32021-02-09 13:54:26 -0600135 auto reply = bus.call(methodUserSetting);
136 reply.read(result);
137 powerPolicy = std::get<std::string>(result);
138 }
139 else
140 {
141 // one_time setting was set so we're going to use it. Reset it
142 // to default for next time.
Andrew Geissler8ffdb262021-09-20 15:25:19 -0500143 info("One time set, use it and reset to default");
Andrew Geissler49e67132022-01-26 14:27:52 -0600144 phosphor::state::manager::utils::setProperty(
145 bus, settings.powerRestorePolicyOneTime.c_str(),
146 powerRestoreIntf, "PowerRestorePolicy",
147 convertForMessage(RestorePolicy::Policy::None));
Andrew Geissler35ca2e32021-02-09 13:54:26 -0600148 }
149
NodeMan97358de952022-07-20 08:31:36 -0500150 auto methodUserSettingDelay = bus.new_method_call(
151 settings.service(settings.powerRestorePolicy, powerRestoreIntf)
152 .c_str(),
153 settings.powerRestorePolicy.c_str(),
154 "org.freedesktop.DBus.Properties", "Get");
155
156 methodUserSettingDelay.append(powerRestoreIntf, "PowerRestoreDelay");
157
158 std::variant<uint64_t> restoreDelay;
159
160 auto delayResult = bus.call(methodUserSettingDelay);
161 delayResult.read(restoreDelay);
162 auto powerRestoreDelayUsec =
163 std::chrono::microseconds(std::get<uint64_t>(restoreDelay));
164 auto powerRestoreDelaySec =
165 std::chrono::duration_cast<std::chrono::seconds>(
166 powerRestoreDelayUsec);
167
Andrew Geissler8ffdb262021-09-20 15:25:19 -0500168 info("Host power is off, processing power policy {POWER_POLICY}",
169 "POWER_POLICY", powerPolicy);
Andrew Geissler35ca2e32021-02-09 13:54:26 -0600170
171 if (RestorePolicy::Policy::AlwaysOn ==
172 RestorePolicy::convertPolicyFromString(powerPolicy))
173 {
NodeMan97358de952022-07-20 08:31:36 -0500174 info(
175 "power_policy=ALWAYS_POWER_ON, powering host on ({DELAY}s delay)",
176 "DELAY", powerRestoreDelaySec.count());
177 std::this_thread::sleep_for(powerRestoreDelayUsec);
Andrew Geissler49e67132022-01-26 14:27:52 -0600178 phosphor::state::manager::utils::setProperty(
179 bus, hostPath, HOST_BUSNAME, "RestartCause",
180 convertForMessage(
181 server::Host::RestartCause::PowerPolicyAlwaysOn));
182 phosphor::state::manager::utils::setProperty(
183 bus, hostPath, HOST_BUSNAME, "RequestedHostTransition",
184 convertForMessage(server::Host::Transition::On));
Andrew Geissler35ca2e32021-02-09 13:54:26 -0600185 }
Thang Tran1fc48452022-02-08 16:04:11 +0700186 else if (RestorePolicy::Policy::AlwaysOff ==
187 RestorePolicy::convertPolicyFromString(powerPolicy))
188 {
NodeMan97358de952022-07-20 08:31:36 -0500189 info(
190 "power_policy=ALWAYS_POWER_OFF, set requested state to off ({DELAY}s delay)",
191 "DELAY", powerRestoreDelaySec.count());
192 std::this_thread::sleep_for(powerRestoreDelayUsec);
Thang Tran6810ad52022-02-24 09:36:57 +0700193 // Read last requested state and re-request it to execute it
194 auto hostReqState = phosphor::state::manager::utils::getProperty(
195 bus, hostPath, HOST_BUSNAME, "RequestedHostTransition");
NodeMan97744fe622022-07-21 17:14:23 -0500196 if (hostReqState !=
197 convertForMessage(server::Host::Transition::Off))
Thang Tran6810ad52022-02-24 09:36:57 +0700198 {
199 phosphor::state::manager::utils::setProperty(
200 bus, hostPath, HOST_BUSNAME, "RequestedHostTransition",
201 convertForMessage(server::Host::Transition::Off));
202 }
Thang Tran1fc48452022-02-08 16:04:11 +0700203 }
Andrew Geissler35ca2e32021-02-09 13:54:26 -0600204 else if (RestorePolicy::Policy::Restore ==
205 RestorePolicy::convertPolicyFromString(powerPolicy))
206 {
NodeMan97358de952022-07-20 08:31:36 -0500207 info("power_policy=RESTORE, restoring last state ({DELAY}s delay)",
208 "DELAY", powerRestoreDelaySec.count());
209 std::this_thread::sleep_for(powerRestoreDelayUsec);
Andrew Geissler35ca2e32021-02-09 13:54:26 -0600210 // Read last requested state and re-request it to execute it
Andrew Geissler49e67132022-01-26 14:27:52 -0600211 auto hostReqState = phosphor::state::manager::utils::getProperty(
212 bus, hostPath, HOST_BUSNAME, "RequestedHostTransition");
NodeMan97744fe622022-07-21 17:14:23 -0500213
214 // As long as the host transition is not 'Off' power on host state.
215 if (hostReqState !=
216 convertForMessage(server::Host::Transition::Off))
Thang Tran6810ad52022-02-24 09:36:57 +0700217 {
218 phosphor::state::manager::utils::setProperty(
219 bus, hostPath, HOST_BUSNAME, "RestartCause",
220 convertForMessage(
221 server::Host::RestartCause::PowerPolicyPreviousState));
222 phosphor::state::manager::utils::setProperty(
223 bus, hostPath, HOST_BUSNAME, "RequestedHostTransition",
NodeMan97744fe622022-07-21 17:14:23 -0500224 convertForMessage(server::Host::Transition::On));
Thang Tran6810ad52022-02-24 09:36:57 +0700225 }
Andrew Geissler35ca2e32021-02-09 13:54:26 -0600226 }
Anthony Wilson32c532e2018-10-25 21:56:07 -0500227 }
Patrick Williamsf053e6f2022-07-22 19:26:54 -0500228 catch (const sdbusplus::exception_t& e)
Anthony Wilson32c532e2018-10-25 21:56:07 -0500229 {
Andrew Geissler8ffdb262021-09-20 15:25:19 -0500230 error("Error in PowerRestorePolicy Get: {ERROR}", "ERROR", e);
Andrew Geissler033fc3b2017-08-30 15:11:44 -0500231 elog<InternalFailure>();
232 }
Deepak Kodihalli55f132b2017-07-25 07:36:06 -0500233
Michael Tritz3ed10082017-03-20 23:28:09 -0500234 return 0;
235}