blob: d526358279308e3981c9215fb16596468d02eee8 [file] [log] [blame]
Andrew Geisslere426b582020-05-28 12:40:55 -05001#include "config.h"
2
3#include "chassis_state_manager.hpp"
4
Andrew Geisslerf8ae6a02022-01-21 17:00:20 -06005#include "utils.hpp"
Andrew Geisslere426b582020-05-28 12:40:55 -05006#include "xyz/openbmc_project/Common/error.hpp"
7#include "xyz/openbmc_project/State/Shutdown/Power/error.hpp"
8
Potin Lai70f36d82022-03-15 10:25:39 +08009#include <fmt/format.h>
10
Andrew Geisslere426b582020-05-28 12:40:55 -050011#include <cereal/archives/json.hpp>
12#include <phosphor-logging/elog-errors.hpp>
Andrew Geissler8ffdb262021-09-20 15:25:19 -050013#include <phosphor-logging/lg2.hpp>
Andrew Geissler0029a5d2017-01-24 14:48:35 -060014#include <sdbusplus/bus.hpp>
William A. Kennington III09568ff2018-05-11 00:03:12 -070015#include <sdbusplus/exception.hpp>
William A. Kennington IIId998f822018-10-17 23:17:57 -070016#include <sdeventplus/event.hpp>
17#include <sdeventplus/exception.hpp>
Adriana Kobylak396ed8a2022-03-22 15:45:41 +000018#include <xyz/openbmc_project/State/Decorator/PowerSystemInputs/server.hpp>
Andrew Geisslere426b582020-05-28 12:40:55 -050019
Andrew Geisslerf2b22e82021-03-12 14:47:03 -060020#include <filesystem>
Andrew Geisslere426b582020-05-28 12:40:55 -050021#include <fstream>
Nagaraju Goruganticb781fe2018-04-06 13:41:01 -050022
Andrew Geisslera90a31a2016-12-13 16:16:28 -060023namespace phosphor
24{
25namespace state
26{
27namespace manager
28{
29
Andrew Geissler8ffdb262021-09-20 15:25:19 -050030PHOSPHOR_LOG2_USING;
31
Andrew Geisslera90a31a2016-12-13 16:16:28 -060032// When you see server:: you know we're referencing our base class
33namespace server = sdbusplus::xyz::openbmc_project::State::server;
Adriana Kobylak396ed8a2022-03-22 15:45:41 +000034namespace decoratorServer =
35 sdbusplus::xyz::openbmc_project::State::Decorator::server;
Andrew Geisslera90a31a2016-12-13 16:16:28 -060036
37using namespace phosphor::logging;
William A. Kennington IIId998f822018-10-17 23:17:57 -070038using sdbusplus::xyz::openbmc_project::Common::Error::InternalFailure;
Aatir Manzur27115ae2019-07-23 16:25:38 -050039using sdbusplus::xyz::openbmc_project::State::Shutdown::Power::Error::Blackout;
Ben Tyner2c36e5a2021-07-12 14:56:49 -050040using sdbusplus::xyz::openbmc_project::State::Shutdown::Power::Error::Regulator;
Potin Lai70f36d82022-03-15 10:25:39 +080041constexpr auto CHASSIS_STATE_POWEROFF_TGT_FMT =
42 "obmc-chassis-poweroff@{}.target";
43constexpr auto CHASSIS_STATE_HARD_POWEROFF_TGT_FMT =
44 "obmc-chassis-hard-poweroff@{}.target";
45constexpr auto CHASSIS_STATE_POWERON_TGT_FMT = "obmc-chassis-poweron@{}.target";
46constexpr auto RESET_HOST_SENSORS_SVC_FMT =
47 "phosphor-reset-sensor-states@{}.service";
Josh D. King697474c2017-03-02 11:15:55 -060048constexpr auto ACTIVE_STATE = "active";
49constexpr auto ACTIVATING_STATE = "activating";
50
Andrew Geissler2cf2a262022-02-02 14:38:41 -060051// Details at https://upower.freedesktop.org/docs/Device.html
52constexpr uint TYPE_UPS = 3;
53constexpr uint STATE_FULLY_CHARGED = 4;
54constexpr uint BATTERY_LVL_FULL = 8;
55
Andrew Geissler58a18012018-01-19 19:36:05 -080056constexpr auto SYSTEMD_SERVICE = "org.freedesktop.systemd1";
57constexpr auto SYSTEMD_OBJ_PATH = "/org/freedesktop/systemd1";
Andrew Geisslerce80f242017-01-24 13:25:33 -060058constexpr auto SYSTEMD_INTERFACE = "org.freedesktop.systemd1.Manager";
59
Josh D. King697474c2017-03-02 11:15:55 -060060constexpr auto SYSTEMD_PROPERTY_IFACE = "org.freedesktop.DBus.Properties";
61constexpr auto SYSTEMD_INTERFACE_UNIT = "org.freedesktop.systemd1.Unit";
62
Andrew Geissler8b1f8622022-01-28 16:37:07 -060063constexpr auto MAPPER_BUSNAME = "xyz.openbmc_project.ObjectMapper";
64constexpr auto MAPPER_PATH = "/xyz/openbmc_project/object_mapper";
65constexpr auto MAPPER_INTERFACE = "xyz.openbmc_project.ObjectMapper";
66constexpr auto UPOWER_INTERFACE = "org.freedesktop.UPower.Device";
Adriana Kobylak396ed8a2022-03-22 15:45:41 +000067constexpr auto POWERSYSINPUTS_INTERFACE =
68 "xyz.openbmc_project.State.Decorator.PowerSystemInputs";
Andrew Geissler8b1f8622022-01-28 16:37:07 -060069constexpr auto PROPERTY_INTERFACE = "org.freedesktop.DBus.Properties";
70
Andrew Geissler0029a5d2017-01-24 14:48:35 -060071void Chassis::subscribeToSystemdSignals()
Andrew Geissler2ec3a7e2016-12-13 22:01:28 -060072{
Andrew Geissler3a30b052019-05-14 15:54:37 -050073 try
74 {
75 auto method = this->bus.new_method_call(
76 SYSTEMD_SERVICE, SYSTEMD_OBJ_PATH, SYSTEMD_INTERFACE, "Subscribe");
Andrew Geissler5b950272019-05-24 12:27:51 -050077 this->bus.call(method);
Andrew Geissler3a30b052019-05-14 15:54:37 -050078 }
Andrew Geissler8ffdb262021-09-20 15:25:19 -050079 catch (const sdbusplus::exception::exception& e)
Andrew Geissler3a30b052019-05-14 15:54:37 -050080 {
Andrew Geissler8ffdb262021-09-20 15:25:19 -050081 error("Failed to subscribe to systemd signals: {ERROR}", "ERROR", e);
Andrew Geissler3a30b052019-05-14 15:54:37 -050082 elog<InternalFailure>();
83 }
Andrew Geissler2ec3a7e2016-12-13 22:01:28 -060084
Andrew Geissler0029a5d2017-01-24 14:48:35 -060085 return;
Andrew Geissler2ec3a7e2016-12-13 22:01:28 -060086}
87
Potin Lai70f36d82022-03-15 10:25:39 +080088void Chassis::createSystemdTargetTable()
89{
90 systemdTargetTable = {
91 // Use the hard off target to ensure we shutdown immediately
92 {Transition::Off, fmt::format(CHASSIS_STATE_HARD_POWEROFF_TGT_FMT, id)},
93 {Transition::On, fmt::format(CHASSIS_STATE_POWERON_TGT_FMT, id)}};
94}
95
Andrew Geisslerdff50ed2016-12-13 20:39:04 -060096// TODO - Will be rewritten once sdbusplus client bindings are in place
97// and persistent storage design is in place and sdbusplus
98// has read property function
99void Chassis::determineInitialState()
100{
Andrew Geissler2cf2a262022-02-02 14:38:41 -0600101
102 // Monitor for any properties changed signals on UPower device path
103 uPowerPropChangeSignal = std::make_unique<sdbusplus::bus::match_t>(
104 bus,
105 sdbusplus::bus::match::rules::propertiesChangedNamespace(
106 "/org/freedesktop/UPower", UPOWER_INTERFACE),
107 [this](auto& msg) { this->uPowerChangeEvent(msg); });
108
Adriana Kobylak396ed8a2022-03-22 15:45:41 +0000109 // Monitor for any properties changed signals on PowerSystemInputs
110 powerSysInputsPropChangeSignal = std::make_unique<sdbusplus::bus::match_t>(
111 bus,
112 sdbusplus::bus::match::rules::propertiesChangedNamespace(
113 fmt::format(
114 "/xyz/openbmc_project/power/power_supplies/chassis{}/psus", id),
115 POWERSYSINPUTS_INTERFACE),
116 [this](auto& msg) { this->powerSysInputsChangeEvent(msg); });
117
Andrew Geissler8b1f8622022-01-28 16:37:07 -0600118 determineStatusOfPower();
119
Patrick Williams2975e262020-05-13 18:01:09 -0500120 std::variant<int> pgood = -1;
Andrew Geissler58a18012018-01-19 19:36:05 -0800121 auto method = this->bus.new_method_call(
122 "org.openbmc.control.Power", "/org/openbmc/control/power0",
123 "org.freedesktop.DBus.Properties", "Get");
Andrew Geisslerdff50ed2016-12-13 20:39:04 -0600124
125 method.append("org.openbmc.control.Power", "pgood");
William A. Kennington III09568ff2018-05-11 00:03:12 -0700126 try
127 {
William A. Kennington III9a2f37c2018-06-28 16:18:37 -0700128 auto reply = this->bus.call(method);
Anthony Wilson32c532e2018-10-25 21:56:07 -0500129 reply.read(pgood);
William A. Kennington III9a2f37c2018-06-28 16:18:37 -0700130
Patrick Williams37413dc2020-05-13 11:29:54 -0500131 if (std::get<int>(pgood) == 1)
William A. Kennington III9a2f37c2018-06-28 16:18:37 -0700132 {
Andrew Geissler8ffdb262021-09-20 15:25:19 -0500133 info("Initial Chassis State will be On");
William A. Kennington III9a2f37c2018-06-28 16:18:37 -0700134 server::Chassis::currentPowerState(PowerState::On);
135 server::Chassis::requestedPowerTransition(Transition::On);
136 return;
137 }
Matt Spinler9eab9862018-07-11 14:13:52 -0500138 else
139 {
140 // The system is off. If we think it should be on then
141 // we probably lost AC while up, so set a new state
142 // change time.
143 uint64_t lastTime;
144 PowerState lastState;
145
146 if (deserializeStateChangeTime(lastTime, lastState))
147 {
Andrew Geissler7ed36232022-01-26 13:49:28 -0600148 // If power was on before the BMC reboot and the reboot reason
149 // was not a pinhole reset, log an error
Matt Spinler9eab9862018-07-11 14:13:52 -0500150 if (lastState == PowerState::On)
151 {
Andrew Geissler7ed36232022-01-26 13:49:28 -0600152 info(
153 "Chassis power was on before the BMC reboot and it is off now");
Matthew Barthbe6efab2022-03-01 13:21:45 -0600154
155 // Reset host sensors since system is off now
Potin Lai70f36d82022-03-15 10:25:39 +0800156 startUnit(fmt::format(RESET_HOST_SENSORS_SVC_FMT, id));
Matthew Barthbe6efab2022-03-01 13:21:45 -0600157
Matt Spinler9eab9862018-07-11 14:13:52 -0500158 setStateChangeTime();
Andrew Geissler7ed36232022-01-26 13:49:28 -0600159
Brandon Wyman4fe860f2022-03-16 00:56:53 +0000160 // 0 indicates pinhole reset. 1 is NOT pinhole reset
Andrew Geissler7ed36232022-01-26 13:49:28 -0600161 if (phosphor::state::manager::utils::getGpioValue(
Brandon Wyman4fe860f2022-03-16 00:56:53 +0000162 "reset-cause-pinhole") != 0)
Andrew Geissler7ed36232022-01-26 13:49:28 -0600163 {
164 if (standbyVoltageRegulatorFault())
165 {
166 report<Regulator>();
167 }
168 else
169 {
Mike Capps073fa2b2022-03-15 16:06:45 -0400170 report<Blackout>(Entry::Level::Critical);
Andrew Geissler7ed36232022-01-26 13:49:28 -0600171 }
172 }
Brandon Wyman4fe860f2022-03-16 00:56:53 +0000173 else
174 {
175 info("Pinhole reset");
176 }
Matt Spinler9eab9862018-07-11 14:13:52 -0500177 }
178 }
179 }
William A. Kennington III09568ff2018-05-11 00:03:12 -0700180 }
Patrick Williams0a675212021-09-02 09:49:43 -0500181 catch (const sdbusplus::exception::exception& e)
William A. Kennington III09568ff2018-05-11 00:03:12 -0700182 {
William A. Kennington III9a2f37c2018-06-28 16:18:37 -0700183 // It's acceptable for the pgood state service to not be available
184 // since it will notify us of the pgood state when it comes up.
185 if (e.name() != nullptr &&
186 strcmp("org.freedesktop.DBus.Error.ServiceUnknown", e.name()) == 0)
187 {
188 goto fail;
189 }
Andrew Geisslerdff50ed2016-12-13 20:39:04 -0600190
William A. Kennington III9a2f37c2018-06-28 16:18:37 -0700191 // Only log for unexpected error types.
Andrew Geissler8ffdb262021-09-20 15:25:19 -0500192 error("Error performing call to get pgood: {ERROR}", "ERROR", e);
William A. Kennington III9a2f37c2018-06-28 16:18:37 -0700193 goto fail;
Andrew Geisslerdff50ed2016-12-13 20:39:04 -0600194 }
William A. Kennington III09568ff2018-05-11 00:03:12 -0700195
196fail:
Andrew Geissler8ffdb262021-09-20 15:25:19 -0500197 info("Initial Chassis State will be Off");
William A. Kennington III09568ff2018-05-11 00:03:12 -0700198 server::Chassis::currentPowerState(PowerState::Off);
199 server::Chassis::requestedPowerTransition(Transition::Off);
Andrew Geisslerdff50ed2016-12-13 20:39:04 -0600200
201 return;
202}
203
Andrew Geissler8b1f8622022-01-28 16:37:07 -0600204void Chassis::determineStatusOfPower()
205{
Andrew Geissler8b1f8622022-01-28 16:37:07 -0600206 // Default PowerStatus to good
207 server::Chassis::currentPowerStatus(PowerStatus::Good);
208
Adriana Kobylak396ed8a2022-03-22 15:45:41 +0000209 determineStatusOfUPSPower();
210 if (server::Chassis::currentPowerStatus() != PowerStatus::Good)
211 {
212 return;
213 }
214
215 determineStatusOfPSUPower();
216}
217
218void Chassis::determineStatusOfUPSPower()
219{
Andrew Geissler8b1f8622022-01-28 16:37:07 -0600220 // Find all implementations of the UPower interface
221 auto mapper = bus.new_method_call(MAPPER_BUSNAME, MAPPER_PATH,
222 MAPPER_INTERFACE, "GetSubTree");
223
224 mapper.append("/", 0, std::vector<std::string>({UPOWER_INTERFACE}));
225
226 std::map<std::string, std::map<std::string, std::vector<std::string>>>
227 mapperResponse;
228
229 try
230 {
231 auto mapperResponseMsg = bus.call(mapper);
232 mapperResponseMsg.read(mapperResponse);
233 }
234 catch (const sdbusplus::exception::exception& e)
235 {
236 error("Error in mapper GetSubTree call for UPS: {ERROR}", "ERROR", e);
237 throw;
238 }
239
240 if (mapperResponse.empty())
241 {
242 debug("No UPower devices found in system");
Andrew Geissler8b1f8622022-01-28 16:37:07 -0600243 }
244
245 // Iterate through all returned Upower interfaces and look for UPS's
246 for (const auto& [path, services] : mapperResponse)
247 {
248 for (const auto& serviceIter : services)
249 {
250 const std::string& service = serviceIter.first;
251
252 try
253 {
254 auto method = bus.new_method_call(service.c_str(), path.c_str(),
255 PROPERTY_INTERFACE, "GetAll");
256 method.append(UPOWER_INTERFACE);
257
258 auto response = bus.call(method);
259 using Property = std::string;
260 using Value = std::variant<bool, uint>;
261 using PropertyMap = std::map<Property, Value>;
262 PropertyMap properties;
263 response.read(properties);
264
Andrew Geissler8b1f8622022-01-28 16:37:07 -0600265 if (std::get<uint>(properties["Type"]) != TYPE_UPS)
266 {
267 info("UPower device {OBJ_PATH} is not a UPS device",
268 "OBJ_PATH", path);
269 continue;
270 }
271
Andrew Geissler2cf2a262022-02-02 14:38:41 -0600272 if (std::get<bool>(properties["IsPresent"]) != true)
273 {
274 // There is a UPS detected but it is not officially
275 // "present" yet. Monitor it for state change.
276 info("UPower device {OBJ_PATH} is not present", "OBJ_PATH",
277 path);
278 continue;
279 }
280
Andrew Geissler8b1f8622022-01-28 16:37:07 -0600281 if (std::get<uint>(properties["State"]) == STATE_FULLY_CHARGED)
282 {
283 info("UPS is fully charged");
284 }
285 else
286 {
287 info("UPS is not fully charged: {UPS_STATE}", "UPS_STATE",
288 std::get<uint>(properties["State"]));
289 server::Chassis::currentPowerStatus(
290 PowerStatus::UninterruptiblePowerSupply);
291 return;
292 }
293
294 if (std::get<uint>(properties["BatteryLevel"]) ==
295 BATTERY_LVL_FULL)
296 {
297 info("UPS Battery Level is Full");
Andrew Geissler2cf2a262022-02-02 14:38:41 -0600298 // Only one UPS per system, we've found it and it's all
299 // good so exit function
300 return;
Andrew Geissler8b1f8622022-01-28 16:37:07 -0600301 }
302 else
303 {
304 info("UPS Battery Level is Low: {UPS_BAT_LEVEL}",
305 "UPS_BAT_LEVEL",
306 std::get<uint>(properties["BatteryLevel"]));
307 server::Chassis::currentPowerStatus(
308 PowerStatus::UninterruptiblePowerSupply);
309 return;
310 }
311 }
312 catch (const sdbusplus::exception::exception& e)
313 {
314 error("Error reading UPS property, error: {ERROR}, "
315 "service: {SERVICE} path: {PATH}",
316 "ERROR", e, "SERVICE", service, "PATH", path);
317 throw;
318 }
319 }
320 }
321 return;
322}
323
Adriana Kobylak396ed8a2022-03-22 15:45:41 +0000324void Chassis::determineStatusOfPSUPower()
325{
326 // Find all implementations of the PowerSystemInputs interface
327 auto mapper = bus.new_method_call(MAPPER_BUSNAME, MAPPER_PATH,
328 MAPPER_INTERFACE, "GetSubTree");
329
330 mapper.append("/", 0, std::vector<std::string>({POWERSYSINPUTS_INTERFACE}));
331
332 std::map<std::string, std::map<std::string, std::vector<std::string>>>
333 mapperResponse;
334
335 try
336 {
337 auto mapperResponseMsg = bus.call(mapper);
338 mapperResponseMsg.read(mapperResponse);
339 }
340 catch (const sdbusplus::exception::exception& e)
341 {
342 error("Error in mapper GetSubTree call for PowerSystemInputs: {ERROR}",
343 "ERROR", e);
344 throw;
345 }
346
347 for (const auto& [path, services] : mapperResponse)
348 {
349 for (const auto& serviceIter : services)
350 {
351 const std::string& service = serviceIter.first;
352
353 try
354 {
355 auto method = bus.new_method_call(service.c_str(), path.c_str(),
356 PROPERTY_INTERFACE, "GetAll");
357 method.append(POWERSYSINPUTS_INTERFACE);
358
359 auto response = bus.call(method);
360 using Property = std::string;
361 using Value = std::variant<std::string>;
362 using PropertyMap = std::map<Property, Value>;
363 PropertyMap properties;
364 response.read(properties);
365
366 auto statusStr = std::get<std::string>(properties["Status"]);
367 auto status =
368 decoratorServer::PowerSystemInputs::convertStatusFromString(
369 statusStr);
370
371 if (status == decoratorServer::PowerSystemInputs::Status::Fault)
372 {
373 info("Power System Inputs status is in Fault state");
374 server::Chassis::currentPowerStatus(PowerStatus::BrownOut);
375 return;
376 }
377 }
378 catch (const sdbusplus::exception::exception& e)
379 {
380 error(
381 "Error reading Power System Inputs property, error: {ERROR}, "
382 "service: {SERVICE} path: {PATH}",
383 "ERROR", e, "SERVICE", service, "PATH", path);
384 throw;
385 }
386 }
387 }
388 return;
389}
390
Andrew Geissler2cf2a262022-02-02 14:38:41 -0600391void Chassis::uPowerChangeEvent(sdbusplus::message::message& msg)
392{
393 debug("UPS Property Change Event Triggered");
394 std::string statusInterface;
395 std::map<std::string, std::variant<uint, bool>> msgData;
396 msg.read(statusInterface, msgData);
397
Adriana Kobylak396ed8a2022-03-22 15:45:41 +0000398 // If the change is to any of the properties we are interested in, then call
399 // determineStatusOfPower(), which looks at all the power-related
400 // interfaces, to see if a power status change is needed
Andrew Geissler2cf2a262022-02-02 14:38:41 -0600401 auto propertyMap = msgData.find("IsPresent");
402 if (propertyMap != msgData.end())
403 {
404 info("UPS presence changed to {UPS_PRES_INFO}", "UPS_PRES_INFO",
405 std::get<bool>(propertyMap->second));
406 determineStatusOfPower();
407 return;
408 }
409
410 propertyMap = msgData.find("State");
411 if (propertyMap != msgData.end())
412 {
413 info("UPS State changed to {UPS_STATE}", "UPS_STATE",
414 std::get<uint>(propertyMap->second));
415 determineStatusOfPower();
416 return;
417 }
418
419 propertyMap = msgData.find("BatteryLevel");
420 if (propertyMap != msgData.end())
421 {
422 info("UPS BatteryLevel changed to {UPS_BAT_LEVEL}", "UPS_BAT_LEVEL",
423 std::get<uint>(propertyMap->second));
424 determineStatusOfPower();
425 return;
426 }
427 return;
428}
429
Adriana Kobylak396ed8a2022-03-22 15:45:41 +0000430void Chassis::powerSysInputsChangeEvent(sdbusplus::message::message& msg)
431{
432 debug("Power System Inputs Property Change Event Triggered");
433 std::string statusInterface;
434 std::map<std::string, std::variant<std::string>> msgData;
435 msg.read(statusInterface, msgData);
436
437 // If the change is to any of the properties we are interested in, then call
438 // determineStatusOfPower(), which looks at all the power-related
439 // interfaces, to see if a power status change is needed
440 auto propertyMap = msgData.find("Status");
441 if (propertyMap != msgData.end())
442 {
443 info("Power System Inputs status changed to {POWER_SYS_INPUT_STATUS}",
444 "POWER_SYS_INPUT_STATUS",
445 std::get<std::string>(propertyMap->second));
446 determineStatusOfPower();
447 return;
448 }
449 return;
450}
451
Matthew Barthbe6efab2022-03-01 13:21:45 -0600452void Chassis::startUnit(const std::string& sysdUnit)
Andrew Geisslerce80f242017-01-24 13:25:33 -0600453{
Andrew Geissler58a18012018-01-19 19:36:05 -0800454 auto method = this->bus.new_method_call(SYSTEMD_SERVICE, SYSTEMD_OBJ_PATH,
455 SYSTEMD_INTERFACE, "StartUnit");
Andrew Geisslerce80f242017-01-24 13:25:33 -0600456
Matthew Barthbe6efab2022-03-01 13:21:45 -0600457 method.append(sysdUnit);
Andrew Geisslerce80f242017-01-24 13:25:33 -0600458 method.append("replace");
459
460 this->bus.call_noreply(method);
461
462 return;
463}
464
Josh D. King697474c2017-03-02 11:15:55 -0600465bool Chassis::stateActive(const std::string& target)
466{
Patrick Williams2975e262020-05-13 18:01:09 -0500467 std::variant<std::string> currentState;
Josh D. King697474c2017-03-02 11:15:55 -0600468 sdbusplus::message::object_path unitTargetPath;
469
Andrew Geissler58a18012018-01-19 19:36:05 -0800470 auto method = this->bus.new_method_call(SYSTEMD_SERVICE, SYSTEMD_OBJ_PATH,
471 SYSTEMD_INTERFACE, "GetUnit");
Josh D. King697474c2017-03-02 11:15:55 -0600472
473 method.append(target);
Josh D. King697474c2017-03-02 11:15:55 -0600474
William A. Kennington III09568ff2018-05-11 00:03:12 -0700475 try
476 {
Anthony Wilson32c532e2018-10-25 21:56:07 -0500477 auto result = this->bus.call(method);
William A. Kennington III09568ff2018-05-11 00:03:12 -0700478 result.read(unitTargetPath);
479 }
Patrick Williams0a675212021-09-02 09:49:43 -0500480 catch (const sdbusplus::exception::exception& e)
William A. Kennington III09568ff2018-05-11 00:03:12 -0700481 {
Andrew Geissler8ffdb262021-09-20 15:25:19 -0500482 error("Error in GetUnit call: {ERROR}", "ERROR", e);
William A. Kennington III09568ff2018-05-11 00:03:12 -0700483 return false;
484 }
Josh D. King697474c2017-03-02 11:15:55 -0600485
Andrew Geissler58a18012018-01-19 19:36:05 -0800486 method = this->bus.new_method_call(
487 SYSTEMD_SERVICE,
488 static_cast<const std::string&>(unitTargetPath).c_str(),
489 SYSTEMD_PROPERTY_IFACE, "Get");
Josh D. King697474c2017-03-02 11:15:55 -0600490
491 method.append(SYSTEMD_INTERFACE_UNIT, "ActiveState");
Josh D. King697474c2017-03-02 11:15:55 -0600492
Anthony Wilson32c532e2018-10-25 21:56:07 -0500493 try
Josh D. King697474c2017-03-02 11:15:55 -0600494 {
Anthony Wilson32c532e2018-10-25 21:56:07 -0500495 auto result = this->bus.call(method);
496 result.read(currentState);
497 }
Patrick Williams0a675212021-09-02 09:49:43 -0500498 catch (const sdbusplus::exception::exception& e)
Anthony Wilson32c532e2018-10-25 21:56:07 -0500499 {
Andrew Geissler8ffdb262021-09-20 15:25:19 -0500500 error("Error in ActiveState Get: {ERROR}", "ERROR", e);
Josh D. King697474c2017-03-02 11:15:55 -0600501 return false;
502 }
503
Patrick Williams37413dc2020-05-13 11:29:54 -0500504 const auto& currentStateStr = std::get<std::string>(currentState);
William A. Kennington III7a0689a2018-11-12 17:19:33 -0800505 return currentStateStr == ACTIVE_STATE ||
506 currentStateStr == ACTIVATING_STATE;
Josh D. King697474c2017-03-02 11:15:55 -0600507}
508
Patrick Williams8f8ba392017-05-05 15:47:39 -0500509int Chassis::sysStateChange(sdbusplus::message::message& msg)
Andrew Geissler0029a5d2017-01-24 14:48:35 -0600510{
Andrew Geissler0029a5d2017-01-24 14:48:35 -0600511 sdbusplus::message::object_path newStateObjPath;
512 std::string newStateUnit{};
513 std::string newStateResult{};
514
Andrew Geissler58a18012018-01-19 19:36:05 -0800515 // Read the msg and populate each variable
William A. Kennington III09568ff2018-05-11 00:03:12 -0700516 try
517 {
Andrew Geissler9b8af4f2019-09-12 14:19:14 -0500518 // newStateID is a throwaway that is needed in order to read the
519 // parameters that are useful out of the dbus message
520 uint32_t newStateID{};
William A. Kennington III09568ff2018-05-11 00:03:12 -0700521 msg.read(newStateID, newStateObjPath, newStateUnit, newStateResult);
522 }
Patrick Williams0a675212021-09-02 09:49:43 -0500523 catch (const sdbusplus::exception::exception& e)
William A. Kennington III09568ff2018-05-11 00:03:12 -0700524 {
Andrew Geissler8ffdb262021-09-20 15:25:19 -0500525 error("Error in state change - bad encoding: {ERROR} {REPLY_SIG}",
526 "ERROR", e, "REPLY_SIG", msg.get_signature());
William A. Kennington III09568ff2018-05-11 00:03:12 -0700527 return 0;
528 }
Andrew Geissler0029a5d2017-01-24 14:48:35 -0600529
Allen.Wangc0895622022-03-23 15:46:47 +0800530 if ((newStateUnit == fmt::format(CHASSIS_STATE_POWEROFF_TGT_FMT, id)) &&
Potin Lai70f36d82022-03-15 10:25:39 +0800531 (newStateResult == "done") &&
532 (!stateActive(systemdTargetTable[Transition::On])))
Andrew Geissler0029a5d2017-01-24 14:48:35 -0600533 {
Andrew Geissler8ffdb262021-09-20 15:25:19 -0500534 info("Received signal that power OFF is complete");
Andrew Geissler0029a5d2017-01-24 14:48:35 -0600535 this->currentPowerState(server::Chassis::PowerState::Off);
Matt Spinler9eab9862018-07-11 14:13:52 -0500536 this->setStateChangeTime();
Andrew Geissler0029a5d2017-01-24 14:48:35 -0600537 }
Potin Lai70f36d82022-03-15 10:25:39 +0800538 else if ((newStateUnit == systemdTargetTable[Transition::On]) &&
Andrew Geissler58a18012018-01-19 19:36:05 -0800539 (newStateResult == "done") &&
Potin Lai70f36d82022-03-15 10:25:39 +0800540 (stateActive(systemdTargetTable[Transition::On])))
Andrew Geissler58a18012018-01-19 19:36:05 -0800541 {
Andrew Geissler8ffdb262021-09-20 15:25:19 -0500542 info("Received signal that power ON is complete");
Andrew Geissler58a18012018-01-19 19:36:05 -0800543 this->currentPowerState(server::Chassis::PowerState::On);
Matt Spinler9eab9862018-07-11 14:13:52 -0500544 this->setStateChangeTime();
Andrew Geisslerf2b22e82021-03-12 14:47:03 -0600545
546 // Remove temporary file which is utilized for scenarios where the
547 // BMC is rebooted while the chassis power is still on.
548 // This file is used to indicate to chassis related systemd services
549 // that the chassis is already on and they should skip running.
550 // Once the chassis state is back to on we can clear this file.
551 auto size = std::snprintf(nullptr, 0, CHASSIS_ON_FILE, 0);
552 size++; // null
553 std::unique_ptr<char[]> chassisFile(new char[size]);
554 std::snprintf(chassisFile.get(), size, CHASSIS_ON_FILE, 0);
555 if (std::filesystem::exists(chassisFile.get()))
556 {
557 std::filesystem::remove(chassisFile.get());
558 }
Andrew Geissler58a18012018-01-19 19:36:05 -0800559 }
Andrew Geissler0029a5d2017-01-24 14:48:35 -0600560
561 return 0;
562}
563
Andrew Geisslera90a31a2016-12-13 16:16:28 -0600564Chassis::Transition Chassis::requestedPowerTransition(Transition value)
565{
566
Andrew Geissler8ffdb262021-09-20 15:25:19 -0500567 info("Change to Chassis Requested Power State: {REQ_POWER_TRAN}",
568 "REQ_POWER_TRAN", value);
Potin Lai70f36d82022-03-15 10:25:39 +0800569 startUnit(systemdTargetTable.find(value)->second);
Andrew Geisslera90a31a2016-12-13 16:16:28 -0600570 return server::Chassis::requestedPowerTransition(value);
571}
572
573Chassis::PowerState Chassis::currentPowerState(PowerState value)
574{
Nagaraju Goruganticb781fe2018-04-06 13:41:01 -0500575 PowerState chassisPowerState;
Andrew Geissler8ffdb262021-09-20 15:25:19 -0500576 info("Change to Chassis Power State: {CUR_POWER_STATE}", "CUR_POWER_STATE",
577 value);
Nagaraju Goruganticb781fe2018-04-06 13:41:01 -0500578
579 chassisPowerState = server::Chassis::currentPowerState(value);
shamim ali6d582a82022-03-15 18:19:32 +0530580 if (chassisPowerState == PowerState::On)
581 {
582 pohTimer.resetRemaining();
583 }
Nagaraju Goruganticb781fe2018-04-06 13:41:01 -0500584 return chassisPowerState;
585}
586
Patrick Williams45a1ed72021-04-30 21:02:43 -0500587uint32_t Chassis::pohCounter(uint32_t value)
Nagaraju Goruganticb781fe2018-04-06 13:41:01 -0500588{
Patrick Williams45a1ed72021-04-30 21:02:43 -0500589 if (value != pohCounter())
Nagaraju Goruganticb781fe2018-04-06 13:41:01 -0500590 {
Patrick Williams45a1ed72021-04-30 21:02:43 -0500591 ChassisInherit::pohCounter(value);
Matt Spinler81957842018-07-11 10:37:12 -0500592 serializePOH();
Nagaraju Goruganticb781fe2018-04-06 13:41:01 -0500593 }
Patrick Williams45a1ed72021-04-30 21:02:43 -0500594 return pohCounter();
Nagaraju Goruganticb781fe2018-04-06 13:41:01 -0500595}
596
Patrick Williams45a1ed72021-04-30 21:02:43 -0500597void Chassis::pohCallback()
William A. Kennington IIId998f822018-10-17 23:17:57 -0700598{
599 if (ChassisInherit::currentPowerState() == PowerState::On)
600 {
Patrick Williams45a1ed72021-04-30 21:02:43 -0500601 pohCounter(pohCounter() + 1);
William A. Kennington IIId998f822018-10-17 23:17:57 -0700602 }
603}
604
Nagaraju Goruganticb781fe2018-04-06 13:41:01 -0500605void Chassis::restorePOHCounter()
606{
607 uint32_t counter;
Allen.Wangba182f02022-03-23 19:01:53 +0800608 if (!deserializePOH(counter))
Nagaraju Goruganticb781fe2018-04-06 13:41:01 -0500609 {
610 // set to default value
Patrick Williams45a1ed72021-04-30 21:02:43 -0500611 pohCounter(0);
Nagaraju Goruganticb781fe2018-04-06 13:41:01 -0500612 }
613 else
614 {
Patrick Williams45a1ed72021-04-30 21:02:43 -0500615 pohCounter(counter);
Nagaraju Goruganticb781fe2018-04-06 13:41:01 -0500616 }
617}
618
Allen.Wangba182f02022-03-23 19:01:53 +0800619fs::path Chassis::serializePOH()
Nagaraju Goruganticb781fe2018-04-06 13:41:01 -0500620{
Allen.Wangba182f02022-03-23 19:01:53 +0800621 fs::path path{fmt::format(POH_COUNTER_PERSIST_PATH, id)};
Nagaraju Goruganticb781fe2018-04-06 13:41:01 -0500622 std::ofstream os(path.c_str(), std::ios::binary);
623 cereal::JSONOutputArchive oarchive(os);
Patrick Williams45a1ed72021-04-30 21:02:43 -0500624 oarchive(pohCounter());
Nagaraju Goruganticb781fe2018-04-06 13:41:01 -0500625 return path;
626}
627
Allen.Wangba182f02022-03-23 19:01:53 +0800628bool Chassis::deserializePOH(uint32_t& pohCounter)
Nagaraju Goruganticb781fe2018-04-06 13:41:01 -0500629{
Allen.Wangba182f02022-03-23 19:01:53 +0800630 fs::path path{fmt::format(POH_COUNTER_PERSIST_PATH, id)};
Nagaraju Goruganticb781fe2018-04-06 13:41:01 -0500631 try
632 {
633 if (fs::exists(path))
634 {
635 std::ifstream is(path.c_str(), std::ios::in | std::ios::binary);
636 cereal::JSONInputArchive iarchive(is);
Patrick Williams45a1ed72021-04-30 21:02:43 -0500637 iarchive(pohCounter);
Nagaraju Goruganticb781fe2018-04-06 13:41:01 -0500638 return true;
639 }
640 return false;
641 }
Patrick Williams8583b3b2021-10-06 12:19:20 -0500642 catch (const cereal::Exception& e)
Nagaraju Goruganticb781fe2018-04-06 13:41:01 -0500643 {
Andrew Geissler8ffdb262021-09-20 15:25:19 -0500644 error("deserialize exception: {ERROR}", "ERROR", e);
Nagaraju Goruganticb781fe2018-04-06 13:41:01 -0500645 fs::remove(path);
646 return false;
647 }
648 catch (const fs::filesystem_error& e)
649 {
650 return false;
651 }
652
653 return false;
654}
655
656void Chassis::startPOHCounter()
657{
Nagaraju Goruganticb781fe2018-04-06 13:41:01 -0500658 auto dir = fs::path(POH_COUNTER_PERSIST_PATH).parent_path();
659 fs::create_directories(dir);
660
Nagaraju Goruganticb781fe2018-04-06 13:41:01 -0500661 try
662 {
William A. Kennington IIId998f822018-10-17 23:17:57 -0700663 auto event = sdeventplus::Event::get_default();
664 bus.attach_event(event.get(), SD_EVENT_PRIORITY_NORMAL);
665 event.loop();
Nagaraju Goruganticb781fe2018-04-06 13:41:01 -0500666 }
William A. Kennington IIId998f822018-10-17 23:17:57 -0700667 catch (const sdeventplus::SdEventError& e)
Nagaraju Goruganticb781fe2018-04-06 13:41:01 -0500668 {
Andrew Geissler8ffdb262021-09-20 15:25:19 -0500669 error("Error occurred during the sdeventplus loop: {ERROR}", "ERROR",
670 e);
Nagaraju Goruganticb781fe2018-04-06 13:41:01 -0500671 phosphor::logging::commit<InternalFailure>();
672 }
Andrew Geisslera90a31a2016-12-13 16:16:28 -0600673}
674
Matt Spinler9eab9862018-07-11 14:13:52 -0500675void Chassis::serializeStateChangeTime()
676{
Allen.Wangba182f02022-03-23 19:01:53 +0800677 fs::path path{fmt::format(CHASSIS_STATE_CHANGE_PERSIST_PATH, id)};
Matt Spinler9eab9862018-07-11 14:13:52 -0500678 std::ofstream os(path.c_str(), std::ios::binary);
679 cereal::JSONOutputArchive oarchive(os);
680
681 oarchive(ChassisInherit::lastStateChangeTime(),
682 ChassisInherit::currentPowerState());
683}
684
685bool Chassis::deserializeStateChangeTime(uint64_t& time, PowerState& state)
686{
Allen.Wangba182f02022-03-23 19:01:53 +0800687 fs::path path{fmt::format(CHASSIS_STATE_CHANGE_PERSIST_PATH, id)};
Matt Spinler9eab9862018-07-11 14:13:52 -0500688
689 try
690 {
691 if (fs::exists(path))
692 {
693 std::ifstream is(path.c_str(), std::ios::in | std::ios::binary);
694 cereal::JSONInputArchive iarchive(is);
695 iarchive(time, state);
696 return true;
697 }
698 }
Patrick Williams8583b3b2021-10-06 12:19:20 -0500699 catch (const std::exception& e)
Matt Spinler9eab9862018-07-11 14:13:52 -0500700 {
Andrew Geissler8ffdb262021-09-20 15:25:19 -0500701 error("deserialize exception: {ERROR}", "ERROR", e);
Matt Spinler9eab9862018-07-11 14:13:52 -0500702 fs::remove(path);
703 }
704
705 return false;
706}
707
708void Chassis::restoreChassisStateChangeTime()
709{
710 uint64_t time;
711 PowerState state;
712
713 if (!deserializeStateChangeTime(time, state))
714 {
715 ChassisInherit::lastStateChangeTime(0);
716 }
717 else
718 {
719 ChassisInherit::lastStateChangeTime(time);
720 }
721}
722
723void Chassis::setStateChangeTime()
724{
725 using namespace std::chrono;
726 uint64_t lastTime;
727 PowerState lastState;
728
729 auto now =
730 duration_cast<milliseconds>(system_clock::now().time_since_epoch())
731 .count();
732
733 // If power is on when the BMC is rebooted, this function will get called
734 // because sysStateChange() runs. Since the power state didn't change
735 // in this case, neither should the state change time, so check that
736 // the power state actually did change here.
737 if (deserializeStateChangeTime(lastTime, lastState))
738 {
739 if (lastState == ChassisInherit::currentPowerState())
740 {
741 return;
742 }
743 }
744
745 ChassisInherit::lastStateChangeTime(now);
746 serializeStateChangeTime();
747}
748
Ben Tyner2c36e5a2021-07-12 14:56:49 -0500749bool Chassis::standbyVoltageRegulatorFault()
750{
751 bool regulatorFault = false;
752
Andrew Geisslerf8ae6a02022-01-21 17:00:20 -0600753 // find standby voltage regulator fault via gpiog
Ben Tyner2c36e5a2021-07-12 14:56:49 -0500754
Andrew Geisslerf8ae6a02022-01-21 17:00:20 -0600755 auto gpioval = utils::getGpioValue("regulator-standby-faulted");
756
757 if (-1 == gpioval)
Ben Tyner2c36e5a2021-07-12 14:56:49 -0500758 {
Andrew Geisslerf8ae6a02022-01-21 17:00:20 -0600759 error("Failed reading regulator-standby-faulted GPIO");
Ben Tyner2c36e5a2021-07-12 14:56:49 -0500760 }
Andrew Geisslerf8ae6a02022-01-21 17:00:20 -0600761
762 if (1 == gpioval)
763 {
764 info("Detected standby voltage regulator fault");
765 regulatorFault = true;
766 }
767
Ben Tyner2c36e5a2021-07-12 14:56:49 -0500768 return regulatorFault;
769}
770
Andrew Geisslera90a31a2016-12-13 16:16:28 -0600771} // namespace manager
772} // namespace state
Andrew Geisslera965cf02018-08-31 08:37:05 -0700773} // namespace phosphor