blob: 9c16ea042b2bbb33ca5d94754481afc2e2f86d19 [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";
Andrew Geissler77a91832022-05-11 12:11:03 -040048constexpr auto AUTO_POWER_RESTORE_SVC_FMT =
49 "phosphor-discover-system-state@{}.service";
Josh D. King697474c2017-03-02 11:15:55 -060050constexpr auto ACTIVE_STATE = "active";
51constexpr auto ACTIVATING_STATE = "activating";
52
Andrew Geissler2cf2a262022-02-02 14:38:41 -060053// Details at https://upower.freedesktop.org/docs/Device.html
54constexpr uint TYPE_UPS = 3;
55constexpr uint STATE_FULLY_CHARGED = 4;
56constexpr uint BATTERY_LVL_FULL = 8;
57
Andrew Geissler58a18012018-01-19 19:36:05 -080058constexpr auto SYSTEMD_SERVICE = "org.freedesktop.systemd1";
59constexpr auto SYSTEMD_OBJ_PATH = "/org/freedesktop/systemd1";
Andrew Geisslerce80f242017-01-24 13:25:33 -060060constexpr auto SYSTEMD_INTERFACE = "org.freedesktop.systemd1.Manager";
61
Josh D. King697474c2017-03-02 11:15:55 -060062constexpr auto SYSTEMD_PROPERTY_IFACE = "org.freedesktop.DBus.Properties";
63constexpr auto SYSTEMD_INTERFACE_UNIT = "org.freedesktop.systemd1.Unit";
64
Andrew Geissler8b1f8622022-01-28 16:37:07 -060065constexpr auto MAPPER_BUSNAME = "xyz.openbmc_project.ObjectMapper";
66constexpr auto MAPPER_PATH = "/xyz/openbmc_project/object_mapper";
67constexpr auto MAPPER_INTERFACE = "xyz.openbmc_project.ObjectMapper";
68constexpr auto UPOWER_INTERFACE = "org.freedesktop.UPower.Device";
Adriana Kobylak396ed8a2022-03-22 15:45:41 +000069constexpr auto POWERSYSINPUTS_INTERFACE =
70 "xyz.openbmc_project.State.Decorator.PowerSystemInputs";
Andrew Geissler8b1f8622022-01-28 16:37:07 -060071constexpr auto PROPERTY_INTERFACE = "org.freedesktop.DBus.Properties";
72
Andrew Geissler0029a5d2017-01-24 14:48:35 -060073void Chassis::subscribeToSystemdSignals()
Andrew Geissler2ec3a7e2016-12-13 22:01:28 -060074{
Andrew Geissler3a30b052019-05-14 15:54:37 -050075 try
76 {
77 auto method = this->bus.new_method_call(
78 SYSTEMD_SERVICE, SYSTEMD_OBJ_PATH, SYSTEMD_INTERFACE, "Subscribe");
Andrew Geissler5b950272019-05-24 12:27:51 -050079 this->bus.call(method);
Andrew Geissler3a30b052019-05-14 15:54:37 -050080 }
Patrick Williamsf053e6f2022-07-22 19:26:54 -050081 catch (const sdbusplus::exception_t& e)
Andrew Geissler3a30b052019-05-14 15:54:37 -050082 {
Andrew Geissler8ffdb262021-09-20 15:25:19 -050083 error("Failed to subscribe to systemd signals: {ERROR}", "ERROR", e);
Andrew Geissler3a30b052019-05-14 15:54:37 -050084 elog<InternalFailure>();
85 }
Andrew Geissler2ec3a7e2016-12-13 22:01:28 -060086
Andrew Geissler0029a5d2017-01-24 14:48:35 -060087 return;
Andrew Geissler2ec3a7e2016-12-13 22:01:28 -060088}
89
Potin Lai70f36d82022-03-15 10:25:39 +080090void Chassis::createSystemdTargetTable()
91{
92 systemdTargetTable = {
93 // Use the hard off target to ensure we shutdown immediately
94 {Transition::Off, fmt::format(CHASSIS_STATE_HARD_POWEROFF_TGT_FMT, id)},
95 {Transition::On, fmt::format(CHASSIS_STATE_POWERON_TGT_FMT, id)}};
96}
97
Andrew Geisslerdff50ed2016-12-13 20:39:04 -060098// TODO - Will be rewritten once sdbusplus client bindings are in place
99// and persistent storage design is in place and sdbusplus
100// has read property function
101void Chassis::determineInitialState()
102{
Andrew Geissler2cf2a262022-02-02 14:38:41 -0600103 // Monitor for any properties changed signals on UPower device path
104 uPowerPropChangeSignal = std::make_unique<sdbusplus::bus::match_t>(
105 bus,
106 sdbusplus::bus::match::rules::propertiesChangedNamespace(
107 "/org/freedesktop/UPower", UPOWER_INTERFACE),
108 [this](auto& msg) { this->uPowerChangeEvent(msg); });
109
Adriana Kobylak396ed8a2022-03-22 15:45:41 +0000110 // Monitor for any properties changed signals on PowerSystemInputs
111 powerSysInputsPropChangeSignal = std::make_unique<sdbusplus::bus::match_t>(
112 bus,
113 sdbusplus::bus::match::rules::propertiesChangedNamespace(
114 fmt::format(
115 "/xyz/openbmc_project/power/power_supplies/chassis{}/psus", id),
116 POWERSYSINPUTS_INTERFACE),
117 [this](auto& msg) { this->powerSysInputsChangeEvent(msg); });
118
Andrew Geissler8b1f8622022-01-28 16:37:07 -0600119 determineStatusOfPower();
120
Patrick Williams2975e262020-05-13 18:01:09 -0500121 std::variant<int> pgood = -1;
Andrew Geissler58a18012018-01-19 19:36:05 -0800122 auto method = this->bus.new_method_call(
123 "org.openbmc.control.Power", "/org/openbmc/control/power0",
124 "org.freedesktop.DBus.Properties", "Get");
Andrew Geisslerdff50ed2016-12-13 20:39:04 -0600125
126 method.append("org.openbmc.control.Power", "pgood");
William A. Kennington III09568ff2018-05-11 00:03:12 -0700127 try
128 {
William A. Kennington III9a2f37c2018-06-28 16:18:37 -0700129 auto reply = this->bus.call(method);
Anthony Wilson32c532e2018-10-25 21:56:07 -0500130 reply.read(pgood);
William A. Kennington III9a2f37c2018-06-28 16:18:37 -0700131
Patrick Williams37413dc2020-05-13 11:29:54 -0500132 if (std::get<int>(pgood) == 1)
William A. Kennington III9a2f37c2018-06-28 16:18:37 -0700133 {
Andrew Geissler8ffdb262021-09-20 15:25:19 -0500134 info("Initial Chassis State will be On");
William A. Kennington III9a2f37c2018-06-28 16:18:37 -0700135 server::Chassis::currentPowerState(PowerState::On);
136 server::Chassis::requestedPowerTransition(Transition::On);
137 return;
138 }
Matt Spinler9eab9862018-07-11 14:13:52 -0500139 else
140 {
141 // The system is off. If we think it should be on then
142 // we probably lost AC while up, so set a new state
143 // change time.
144 uint64_t lastTime;
145 PowerState lastState;
146
147 if (deserializeStateChangeTime(lastTime, lastState))
148 {
Andrew Geissler7ed36232022-01-26 13:49:28 -0600149 // If power was on before the BMC reboot and the reboot reason
150 // was not a pinhole reset, log an error
Matt Spinler9eab9862018-07-11 14:13:52 -0500151 if (lastState == PowerState::On)
152 {
Andrew Geissler7ed36232022-01-26 13:49:28 -0600153 info(
154 "Chassis power was on before the BMC reboot and it is off now");
Matthew Barthbe6efab2022-03-01 13:21:45 -0600155
156 // Reset host sensors since system is off now
Potin Lai70f36d82022-03-15 10:25:39 +0800157 startUnit(fmt::format(RESET_HOST_SENSORS_SVC_FMT, id));
Matthew Barthbe6efab2022-03-01 13:21:45 -0600158
Matt Spinler9eab9862018-07-11 14:13:52 -0500159 setStateChangeTime();
Andrew Geissler7ed36232022-01-26 13:49:28 -0600160
Brandon Wyman4fe860f2022-03-16 00:56:53 +0000161 // 0 indicates pinhole reset. 1 is NOT pinhole reset
Andrew Geissler7ed36232022-01-26 13:49:28 -0600162 if (phosphor::state::manager::utils::getGpioValue(
Brandon Wyman4fe860f2022-03-16 00:56:53 +0000163 "reset-cause-pinhole") != 0)
Andrew Geissler7ed36232022-01-26 13:49:28 -0600164 {
165 if (standbyVoltageRegulatorFault())
166 {
167 report<Regulator>();
168 }
169 else
170 {
Mike Capps073fa2b2022-03-15 16:06:45 -0400171 report<Blackout>(Entry::Level::Critical);
Andrew Geissler7ed36232022-01-26 13:49:28 -0600172 }
173 }
Brandon Wyman4fe860f2022-03-16 00:56:53 +0000174 else
175 {
176 info("Pinhole reset");
177 }
Matt Spinler9eab9862018-07-11 14:13:52 -0500178 }
179 }
180 }
William A. Kennington III09568ff2018-05-11 00:03:12 -0700181 }
Patrick Williamsf053e6f2022-07-22 19:26:54 -0500182 catch (const sdbusplus::exception_t& e)
William A. Kennington III09568ff2018-05-11 00:03:12 -0700183 {
William A. Kennington III9a2f37c2018-06-28 16:18:37 -0700184 // It's acceptable for the pgood state service to not be available
185 // since it will notify us of the pgood state when it comes up.
186 if (e.name() != nullptr &&
187 strcmp("org.freedesktop.DBus.Error.ServiceUnknown", e.name()) == 0)
188 {
189 goto fail;
190 }
Andrew Geisslerdff50ed2016-12-13 20:39:04 -0600191
William A. Kennington III9a2f37c2018-06-28 16:18:37 -0700192 // Only log for unexpected error types.
Andrew Geissler8ffdb262021-09-20 15:25:19 -0500193 error("Error performing call to get pgood: {ERROR}", "ERROR", e);
William A. Kennington III9a2f37c2018-06-28 16:18:37 -0700194 goto fail;
Andrew Geisslerdff50ed2016-12-13 20:39:04 -0600195 }
William A. Kennington III09568ff2018-05-11 00:03:12 -0700196
197fail:
Andrew Geissler8ffdb262021-09-20 15:25:19 -0500198 info("Initial Chassis State will be Off");
William A. Kennington III09568ff2018-05-11 00:03:12 -0700199 server::Chassis::currentPowerState(PowerState::Off);
200 server::Chassis::requestedPowerTransition(Transition::Off);
Andrew Geisslerdff50ed2016-12-13 20:39:04 -0600201
202 return;
203}
204
Andrew Geissler8b1f8622022-01-28 16:37:07 -0600205void Chassis::determineStatusOfPower()
206{
Andrew Geissler77a91832022-05-11 12:11:03 -0400207 auto initialPowerStatus = server::Chassis::currentPowerStatus();
208
Andrew Geissler66aacdc2022-05-11 08:31:47 -0400209 bool powerGood = determineStatusOfUPSPower();
210 if (!powerGood)
Adriana Kobylak396ed8a2022-03-22 15:45:41 +0000211 {
212 return;
213 }
214
Andrew Geissler66aacdc2022-05-11 08:31:47 -0400215 powerGood = determineStatusOfPSUPower();
216 if (powerGood)
217 {
218 // All checks passed, set power status to good
219 server::Chassis::currentPowerStatus(PowerStatus::Good);
Andrew Geissler77a91832022-05-11 12:11:03 -0400220
221 // If power status transitioned from bad to good and chassis power is
222 // off then call Auto Power Restart to see if the system should auto
223 // power on now that power status is good
224 if ((initialPowerStatus != PowerStatus::Good) &&
225 (server::Chassis::currentPowerState() == PowerState::Off))
226 {
227 info("power status transitioned from {START_PWR_STATE} to Good and "
228 "chassis power is off, calling APR",
229 "START_PWR_STATE", initialPowerStatus);
230 restartUnit(fmt::format(AUTO_POWER_RESTORE_SVC_FMT, this->id));
231 }
Andrew Geissler66aacdc2022-05-11 08:31:47 -0400232 }
Adriana Kobylak396ed8a2022-03-22 15:45:41 +0000233}
234
Andrew Geissler66aacdc2022-05-11 08:31:47 -0400235bool Chassis::determineStatusOfUPSPower()
Adriana Kobylak396ed8a2022-03-22 15:45:41 +0000236{
Andrew Geissler8b1f8622022-01-28 16:37:07 -0600237 // Find all implementations of the UPower interface
238 auto mapper = bus.new_method_call(MAPPER_BUSNAME, MAPPER_PATH,
239 MAPPER_INTERFACE, "GetSubTree");
240
241 mapper.append("/", 0, std::vector<std::string>({UPOWER_INTERFACE}));
242
243 std::map<std::string, std::map<std::string, std::vector<std::string>>>
244 mapperResponse;
245
246 try
247 {
248 auto mapperResponseMsg = bus.call(mapper);
249 mapperResponseMsg.read(mapperResponse);
250 }
Patrick Williamsf053e6f2022-07-22 19:26:54 -0500251 catch (const sdbusplus::exception_t& e)
Andrew Geissler8b1f8622022-01-28 16:37:07 -0600252 {
253 error("Error in mapper GetSubTree call for UPS: {ERROR}", "ERROR", e);
254 throw;
255 }
256
257 if (mapperResponse.empty())
258 {
259 debug("No UPower devices found in system");
Andrew Geissler8b1f8622022-01-28 16:37:07 -0600260 }
261
262 // Iterate through all returned Upower interfaces and look for UPS's
263 for (const auto& [path, services] : mapperResponse)
264 {
265 for (const auto& serviceIter : services)
266 {
267 const std::string& service = serviceIter.first;
268
269 try
270 {
271 auto method = bus.new_method_call(service.c_str(), path.c_str(),
272 PROPERTY_INTERFACE, "GetAll");
273 method.append(UPOWER_INTERFACE);
274
275 auto response = bus.call(method);
276 using Property = std::string;
277 using Value = std::variant<bool, uint>;
278 using PropertyMap = std::map<Property, Value>;
279 PropertyMap properties;
280 response.read(properties);
281
Andrew Geissler8b1f8622022-01-28 16:37:07 -0600282 if (std::get<uint>(properties["Type"]) != TYPE_UPS)
283 {
284 info("UPower device {OBJ_PATH} is not a UPS device",
285 "OBJ_PATH", path);
286 continue;
287 }
288
Andrew Geissler2cf2a262022-02-02 14:38:41 -0600289 if (std::get<bool>(properties["IsPresent"]) != true)
290 {
291 // There is a UPS detected but it is not officially
292 // "present" yet. Monitor it for state change.
293 info("UPower device {OBJ_PATH} is not present", "OBJ_PATH",
294 path);
295 continue;
296 }
297
Andrew Geissler8b1f8622022-01-28 16:37:07 -0600298 if (std::get<uint>(properties["State"]) == STATE_FULLY_CHARGED)
299 {
300 info("UPS is fully charged");
301 }
302 else
303 {
304 info("UPS is not fully charged: {UPS_STATE}", "UPS_STATE",
305 std::get<uint>(properties["State"]));
306 server::Chassis::currentPowerStatus(
307 PowerStatus::UninterruptiblePowerSupply);
Andrew Geissler66aacdc2022-05-11 08:31:47 -0400308 return false;
Andrew Geissler8b1f8622022-01-28 16:37:07 -0600309 }
310
311 if (std::get<uint>(properties["BatteryLevel"]) ==
312 BATTERY_LVL_FULL)
313 {
314 info("UPS Battery Level is Full");
Andrew Geissler2cf2a262022-02-02 14:38:41 -0600315 // Only one UPS per system, we've found it and it's all
316 // good so exit function
Andrew Geissler66aacdc2022-05-11 08:31:47 -0400317 return true;
Andrew Geissler8b1f8622022-01-28 16:37:07 -0600318 }
319 else
320 {
321 info("UPS Battery Level is Low: {UPS_BAT_LEVEL}",
322 "UPS_BAT_LEVEL",
323 std::get<uint>(properties["BatteryLevel"]));
324 server::Chassis::currentPowerStatus(
325 PowerStatus::UninterruptiblePowerSupply);
Andrew Geissler66aacdc2022-05-11 08:31:47 -0400326 return false;
Andrew Geissler8b1f8622022-01-28 16:37:07 -0600327 }
328 }
Patrick Williamsf053e6f2022-07-22 19:26:54 -0500329 catch (const sdbusplus::exception_t& e)
Andrew Geissler8b1f8622022-01-28 16:37:07 -0600330 {
331 error("Error reading UPS property, error: {ERROR}, "
332 "service: {SERVICE} path: {PATH}",
333 "ERROR", e, "SERVICE", service, "PATH", path);
334 throw;
335 }
336 }
337 }
Andrew Geissler66aacdc2022-05-11 08:31:47 -0400338 return true;
Andrew Geissler8b1f8622022-01-28 16:37:07 -0600339}
340
Andrew Geissler66aacdc2022-05-11 08:31:47 -0400341bool Chassis::determineStatusOfPSUPower()
Adriana Kobylak396ed8a2022-03-22 15:45:41 +0000342{
343 // Find all implementations of the PowerSystemInputs interface
344 auto mapper = bus.new_method_call(MAPPER_BUSNAME, MAPPER_PATH,
345 MAPPER_INTERFACE, "GetSubTree");
346
347 mapper.append("/", 0, std::vector<std::string>({POWERSYSINPUTS_INTERFACE}));
348
349 std::map<std::string, std::map<std::string, std::vector<std::string>>>
350 mapperResponse;
351
352 try
353 {
354 auto mapperResponseMsg = bus.call(mapper);
355 mapperResponseMsg.read(mapperResponse);
356 }
Patrick Williamsf053e6f2022-07-22 19:26:54 -0500357 catch (const sdbusplus::exception_t& e)
Adriana Kobylak396ed8a2022-03-22 15:45:41 +0000358 {
359 error("Error in mapper GetSubTree call for PowerSystemInputs: {ERROR}",
360 "ERROR", e);
361 throw;
362 }
363
364 for (const auto& [path, services] : mapperResponse)
365 {
366 for (const auto& serviceIter : services)
367 {
368 const std::string& service = serviceIter.first;
369
370 try
371 {
372 auto method = bus.new_method_call(service.c_str(), path.c_str(),
373 PROPERTY_INTERFACE, "GetAll");
374 method.append(POWERSYSINPUTS_INTERFACE);
375
376 auto response = bus.call(method);
377 using Property = std::string;
378 using Value = std::variant<std::string>;
379 using PropertyMap = std::map<Property, Value>;
380 PropertyMap properties;
381 response.read(properties);
382
383 auto statusStr = std::get<std::string>(properties["Status"]);
384 auto status =
385 decoratorServer::PowerSystemInputs::convertStatusFromString(
386 statusStr);
387
388 if (status == decoratorServer::PowerSystemInputs::Status::Fault)
389 {
390 info("Power System Inputs status is in Fault state");
391 server::Chassis::currentPowerStatus(PowerStatus::BrownOut);
Andrew Geissler66aacdc2022-05-11 08:31:47 -0400392 return false;
Adriana Kobylak396ed8a2022-03-22 15:45:41 +0000393 }
394 }
Patrick Williamsf053e6f2022-07-22 19:26:54 -0500395 catch (const sdbusplus::exception_t& e)
Adriana Kobylak396ed8a2022-03-22 15:45:41 +0000396 {
397 error(
398 "Error reading Power System Inputs property, error: {ERROR}, "
399 "service: {SERVICE} path: {PATH}",
400 "ERROR", e, "SERVICE", service, "PATH", path);
401 throw;
402 }
403 }
404 }
Andrew Geissler66aacdc2022-05-11 08:31:47 -0400405 return true;
Adriana Kobylak396ed8a2022-03-22 15:45:41 +0000406}
407
Patrick Williamsf053e6f2022-07-22 19:26:54 -0500408void Chassis::uPowerChangeEvent(sdbusplus::message_t& msg)
Andrew Geissler2cf2a262022-02-02 14:38:41 -0600409{
410 debug("UPS Property Change Event Triggered");
411 std::string statusInterface;
412 std::map<std::string, std::variant<uint, bool>> msgData;
413 msg.read(statusInterface, msgData);
414
Adriana Kobylak396ed8a2022-03-22 15:45:41 +0000415 // If the change is to any of the properties we are interested in, then call
416 // determineStatusOfPower(), which looks at all the power-related
417 // interfaces, to see if a power status change is needed
Andrew Geissler2cf2a262022-02-02 14:38:41 -0600418 auto propertyMap = msgData.find("IsPresent");
419 if (propertyMap != msgData.end())
420 {
421 info("UPS presence changed to {UPS_PRES_INFO}", "UPS_PRES_INFO",
422 std::get<bool>(propertyMap->second));
423 determineStatusOfPower();
424 return;
425 }
426
427 propertyMap = msgData.find("State");
428 if (propertyMap != msgData.end())
429 {
430 info("UPS State changed to {UPS_STATE}", "UPS_STATE",
431 std::get<uint>(propertyMap->second));
432 determineStatusOfPower();
433 return;
434 }
435
436 propertyMap = msgData.find("BatteryLevel");
437 if (propertyMap != msgData.end())
438 {
439 info("UPS BatteryLevel changed to {UPS_BAT_LEVEL}", "UPS_BAT_LEVEL",
440 std::get<uint>(propertyMap->second));
441 determineStatusOfPower();
442 return;
443 }
444 return;
445}
446
Patrick Williamsf053e6f2022-07-22 19:26:54 -0500447void Chassis::powerSysInputsChangeEvent(sdbusplus::message_t& msg)
Adriana Kobylak396ed8a2022-03-22 15:45:41 +0000448{
449 debug("Power System Inputs Property Change Event Triggered");
450 std::string statusInterface;
451 std::map<std::string, std::variant<std::string>> msgData;
452 msg.read(statusInterface, msgData);
453
454 // If the change is to any of the properties we are interested in, then call
455 // determineStatusOfPower(), which looks at all the power-related
456 // interfaces, to see if a power status change is needed
457 auto propertyMap = msgData.find("Status");
458 if (propertyMap != msgData.end())
459 {
460 info("Power System Inputs status changed to {POWER_SYS_INPUT_STATUS}",
461 "POWER_SYS_INPUT_STATUS",
462 std::get<std::string>(propertyMap->second));
463 determineStatusOfPower();
464 return;
465 }
466 return;
467}
468
Matthew Barthbe6efab2022-03-01 13:21:45 -0600469void Chassis::startUnit(const std::string& sysdUnit)
Andrew Geisslerce80f242017-01-24 13:25:33 -0600470{
Andrew Geissler58a18012018-01-19 19:36:05 -0800471 auto method = this->bus.new_method_call(SYSTEMD_SERVICE, SYSTEMD_OBJ_PATH,
472 SYSTEMD_INTERFACE, "StartUnit");
Andrew Geisslerce80f242017-01-24 13:25:33 -0600473
Matthew Barthbe6efab2022-03-01 13:21:45 -0600474 method.append(sysdUnit);
Andrew Geisslerce80f242017-01-24 13:25:33 -0600475 method.append("replace");
476
477 this->bus.call_noreply(method);
478
479 return;
480}
481
Andrew Geissler77a91832022-05-11 12:11:03 -0400482void Chassis::restartUnit(const std::string& sysdUnit)
483{
484 auto method = this->bus.new_method_call(SYSTEMD_SERVICE, SYSTEMD_OBJ_PATH,
485 SYSTEMD_INTERFACE, "RestartUnit");
486
487 method.append(sysdUnit);
488 method.append("replace");
489
490 this->bus.call_noreply(method);
491
492 return;
493}
494
Josh D. King697474c2017-03-02 11:15:55 -0600495bool Chassis::stateActive(const std::string& target)
496{
Patrick Williams2975e262020-05-13 18:01:09 -0500497 std::variant<std::string> currentState;
Josh D. King697474c2017-03-02 11:15:55 -0600498 sdbusplus::message::object_path unitTargetPath;
499
Andrew Geissler58a18012018-01-19 19:36:05 -0800500 auto method = this->bus.new_method_call(SYSTEMD_SERVICE, SYSTEMD_OBJ_PATH,
501 SYSTEMD_INTERFACE, "GetUnit");
Josh D. King697474c2017-03-02 11:15:55 -0600502
503 method.append(target);
Josh D. King697474c2017-03-02 11:15:55 -0600504
William A. Kennington III09568ff2018-05-11 00:03:12 -0700505 try
506 {
Anthony Wilson32c532e2018-10-25 21:56:07 -0500507 auto result = this->bus.call(method);
William A. Kennington III09568ff2018-05-11 00:03:12 -0700508 result.read(unitTargetPath);
509 }
Patrick Williamsf053e6f2022-07-22 19:26:54 -0500510 catch (const sdbusplus::exception_t& e)
William A. Kennington III09568ff2018-05-11 00:03:12 -0700511 {
Andrew Geissler8ffdb262021-09-20 15:25:19 -0500512 error("Error in GetUnit call: {ERROR}", "ERROR", e);
William A. Kennington III09568ff2018-05-11 00:03:12 -0700513 return false;
514 }
Josh D. King697474c2017-03-02 11:15:55 -0600515
Andrew Geissler58a18012018-01-19 19:36:05 -0800516 method = this->bus.new_method_call(
517 SYSTEMD_SERVICE,
518 static_cast<const std::string&>(unitTargetPath).c_str(),
519 SYSTEMD_PROPERTY_IFACE, "Get");
Josh D. King697474c2017-03-02 11:15:55 -0600520
521 method.append(SYSTEMD_INTERFACE_UNIT, "ActiveState");
Josh D. King697474c2017-03-02 11:15:55 -0600522
Anthony Wilson32c532e2018-10-25 21:56:07 -0500523 try
Josh D. King697474c2017-03-02 11:15:55 -0600524 {
Anthony Wilson32c532e2018-10-25 21:56:07 -0500525 auto result = this->bus.call(method);
526 result.read(currentState);
527 }
Patrick Williamsf053e6f2022-07-22 19:26:54 -0500528 catch (const sdbusplus::exception_t& e)
Anthony Wilson32c532e2018-10-25 21:56:07 -0500529 {
Andrew Geissler8ffdb262021-09-20 15:25:19 -0500530 error("Error in ActiveState Get: {ERROR}", "ERROR", e);
Josh D. King697474c2017-03-02 11:15:55 -0600531 return false;
532 }
533
Patrick Williams37413dc2020-05-13 11:29:54 -0500534 const auto& currentStateStr = std::get<std::string>(currentState);
William A. Kennington III7a0689a2018-11-12 17:19:33 -0800535 return currentStateStr == ACTIVE_STATE ||
536 currentStateStr == ACTIVATING_STATE;
Josh D. King697474c2017-03-02 11:15:55 -0600537}
538
Patrick Williamsf053e6f2022-07-22 19:26:54 -0500539int Chassis::sysStateChange(sdbusplus::message_t& msg)
Andrew Geissler0029a5d2017-01-24 14:48:35 -0600540{
Andrew Geissler0029a5d2017-01-24 14:48:35 -0600541 sdbusplus::message::object_path newStateObjPath;
542 std::string newStateUnit{};
543 std::string newStateResult{};
544
Andrew Geissler58a18012018-01-19 19:36:05 -0800545 // Read the msg and populate each variable
William A. Kennington III09568ff2018-05-11 00:03:12 -0700546 try
547 {
Andrew Geissler9b8af4f2019-09-12 14:19:14 -0500548 // newStateID is a throwaway that is needed in order to read the
549 // parameters that are useful out of the dbus message
550 uint32_t newStateID{};
William A. Kennington III09568ff2018-05-11 00:03:12 -0700551 msg.read(newStateID, newStateObjPath, newStateUnit, newStateResult);
552 }
Patrick Williamsf053e6f2022-07-22 19:26:54 -0500553 catch (const sdbusplus::exception_t& e)
William A. Kennington III09568ff2018-05-11 00:03:12 -0700554 {
Andrew Geissler8ffdb262021-09-20 15:25:19 -0500555 error("Error in state change - bad encoding: {ERROR} {REPLY_SIG}",
556 "ERROR", e, "REPLY_SIG", msg.get_signature());
William A. Kennington III09568ff2018-05-11 00:03:12 -0700557 return 0;
558 }
Andrew Geissler0029a5d2017-01-24 14:48:35 -0600559
Allen.Wangc0895622022-03-23 15:46:47 +0800560 if ((newStateUnit == fmt::format(CHASSIS_STATE_POWEROFF_TGT_FMT, id)) &&
Potin Lai70f36d82022-03-15 10:25:39 +0800561 (newStateResult == "done") &&
562 (!stateActive(systemdTargetTable[Transition::On])))
Andrew Geissler0029a5d2017-01-24 14:48:35 -0600563 {
Andrew Geissler8ffdb262021-09-20 15:25:19 -0500564 info("Received signal that power OFF is complete");
Andrew Geissler0029a5d2017-01-24 14:48:35 -0600565 this->currentPowerState(server::Chassis::PowerState::Off);
Matt Spinler9eab9862018-07-11 14:13:52 -0500566 this->setStateChangeTime();
Andrew Geissler0029a5d2017-01-24 14:48:35 -0600567 }
Potin Lai70f36d82022-03-15 10:25:39 +0800568 else if ((newStateUnit == systemdTargetTable[Transition::On]) &&
Andrew Geissler58a18012018-01-19 19:36:05 -0800569 (newStateResult == "done") &&
Potin Lai70f36d82022-03-15 10:25:39 +0800570 (stateActive(systemdTargetTable[Transition::On])))
Andrew Geissler58a18012018-01-19 19:36:05 -0800571 {
Andrew Geissler8ffdb262021-09-20 15:25:19 -0500572 info("Received signal that power ON is complete");
Andrew Geissler58a18012018-01-19 19:36:05 -0800573 this->currentPowerState(server::Chassis::PowerState::On);
Matt Spinler9eab9862018-07-11 14:13:52 -0500574 this->setStateChangeTime();
Andrew Geisslerf2b22e82021-03-12 14:47:03 -0600575
576 // Remove temporary file which is utilized for scenarios where the
577 // BMC is rebooted while the chassis power is still on.
578 // This file is used to indicate to chassis related systemd services
579 // that the chassis is already on and they should skip running.
580 // Once the chassis state is back to on we can clear this file.
581 auto size = std::snprintf(nullptr, 0, CHASSIS_ON_FILE, 0);
582 size++; // null
583 std::unique_ptr<char[]> chassisFile(new char[size]);
584 std::snprintf(chassisFile.get(), size, CHASSIS_ON_FILE, 0);
585 if (std::filesystem::exists(chassisFile.get()))
586 {
587 std::filesystem::remove(chassisFile.get());
588 }
Andrew Geissler58a18012018-01-19 19:36:05 -0800589 }
Andrew Geissler0029a5d2017-01-24 14:48:35 -0600590
591 return 0;
592}
593
Andrew Geisslera90a31a2016-12-13 16:16:28 -0600594Chassis::Transition Chassis::requestedPowerTransition(Transition value)
595{
Andrew Geissler8ffdb262021-09-20 15:25:19 -0500596 info("Change to Chassis Requested Power State: {REQ_POWER_TRAN}",
597 "REQ_POWER_TRAN", value);
Potin Lai70f36d82022-03-15 10:25:39 +0800598 startUnit(systemdTargetTable.find(value)->second);
Andrew Geisslera90a31a2016-12-13 16:16:28 -0600599 return server::Chassis::requestedPowerTransition(value);
600}
601
602Chassis::PowerState Chassis::currentPowerState(PowerState value)
603{
Nagaraju Goruganticb781fe2018-04-06 13:41:01 -0500604 PowerState chassisPowerState;
Andrew Geissler8ffdb262021-09-20 15:25:19 -0500605 info("Change to Chassis Power State: {CUR_POWER_STATE}", "CUR_POWER_STATE",
606 value);
Nagaraju Goruganticb781fe2018-04-06 13:41:01 -0500607
608 chassisPowerState = server::Chassis::currentPowerState(value);
shamim ali6d582a82022-03-15 18:19:32 +0530609 if (chassisPowerState == PowerState::On)
610 {
611 pohTimer.resetRemaining();
612 }
Nagaraju Goruganticb781fe2018-04-06 13:41:01 -0500613 return chassisPowerState;
614}
615
Patrick Williams45a1ed72021-04-30 21:02:43 -0500616uint32_t Chassis::pohCounter(uint32_t value)
Nagaraju Goruganticb781fe2018-04-06 13:41:01 -0500617{
Patrick Williams45a1ed72021-04-30 21:02:43 -0500618 if (value != pohCounter())
Nagaraju Goruganticb781fe2018-04-06 13:41:01 -0500619 {
Patrick Williams45a1ed72021-04-30 21:02:43 -0500620 ChassisInherit::pohCounter(value);
Matt Spinler81957842018-07-11 10:37:12 -0500621 serializePOH();
Nagaraju Goruganticb781fe2018-04-06 13:41:01 -0500622 }
Patrick Williams45a1ed72021-04-30 21:02:43 -0500623 return pohCounter();
Nagaraju Goruganticb781fe2018-04-06 13:41:01 -0500624}
625
Patrick Williams45a1ed72021-04-30 21:02:43 -0500626void Chassis::pohCallback()
William A. Kennington IIId998f822018-10-17 23:17:57 -0700627{
628 if (ChassisInherit::currentPowerState() == PowerState::On)
629 {
Patrick Williams45a1ed72021-04-30 21:02:43 -0500630 pohCounter(pohCounter() + 1);
William A. Kennington IIId998f822018-10-17 23:17:57 -0700631 }
632}
633
Nagaraju Goruganticb781fe2018-04-06 13:41:01 -0500634void Chassis::restorePOHCounter()
635{
636 uint32_t counter;
Allen.Wangba182f02022-03-23 19:01:53 +0800637 if (!deserializePOH(counter))
Nagaraju Goruganticb781fe2018-04-06 13:41:01 -0500638 {
639 // set to default value
Patrick Williams45a1ed72021-04-30 21:02:43 -0500640 pohCounter(0);
Nagaraju Goruganticb781fe2018-04-06 13:41:01 -0500641 }
642 else
643 {
Patrick Williams45a1ed72021-04-30 21:02:43 -0500644 pohCounter(counter);
Nagaraju Goruganticb781fe2018-04-06 13:41:01 -0500645 }
646}
647
Allen.Wangba182f02022-03-23 19:01:53 +0800648fs::path Chassis::serializePOH()
Nagaraju Goruganticb781fe2018-04-06 13:41:01 -0500649{
Allen.Wangba182f02022-03-23 19:01:53 +0800650 fs::path path{fmt::format(POH_COUNTER_PERSIST_PATH, id)};
Nagaraju Goruganticb781fe2018-04-06 13:41:01 -0500651 std::ofstream os(path.c_str(), std::ios::binary);
652 cereal::JSONOutputArchive oarchive(os);
Patrick Williams45a1ed72021-04-30 21:02:43 -0500653 oarchive(pohCounter());
Nagaraju Goruganticb781fe2018-04-06 13:41:01 -0500654 return path;
655}
656
Allen.Wangba182f02022-03-23 19:01:53 +0800657bool Chassis::deserializePOH(uint32_t& pohCounter)
Nagaraju Goruganticb781fe2018-04-06 13:41:01 -0500658{
Allen.Wangba182f02022-03-23 19:01:53 +0800659 fs::path path{fmt::format(POH_COUNTER_PERSIST_PATH, id)};
Nagaraju Goruganticb781fe2018-04-06 13:41:01 -0500660 try
661 {
662 if (fs::exists(path))
663 {
664 std::ifstream is(path.c_str(), std::ios::in | std::ios::binary);
665 cereal::JSONInputArchive iarchive(is);
Patrick Williams45a1ed72021-04-30 21:02:43 -0500666 iarchive(pohCounter);
Nagaraju Goruganticb781fe2018-04-06 13:41:01 -0500667 return true;
668 }
669 return false;
670 }
Patrick Williams8583b3b2021-10-06 12:19:20 -0500671 catch (const cereal::Exception& e)
Nagaraju Goruganticb781fe2018-04-06 13:41:01 -0500672 {
Andrew Geissler8ffdb262021-09-20 15:25:19 -0500673 error("deserialize exception: {ERROR}", "ERROR", e);
Nagaraju Goruganticb781fe2018-04-06 13:41:01 -0500674 fs::remove(path);
675 return false;
676 }
677 catch (const fs::filesystem_error& e)
678 {
679 return false;
680 }
681
682 return false;
683}
684
685void Chassis::startPOHCounter()
686{
Nagaraju Goruganticb781fe2018-04-06 13:41:01 -0500687 auto dir = fs::path(POH_COUNTER_PERSIST_PATH).parent_path();
688 fs::create_directories(dir);
689
Nagaraju Goruganticb781fe2018-04-06 13:41:01 -0500690 try
691 {
William A. Kennington IIId998f822018-10-17 23:17:57 -0700692 auto event = sdeventplus::Event::get_default();
693 bus.attach_event(event.get(), SD_EVENT_PRIORITY_NORMAL);
694 event.loop();
Nagaraju Goruganticb781fe2018-04-06 13:41:01 -0500695 }
William A. Kennington IIId998f822018-10-17 23:17:57 -0700696 catch (const sdeventplus::SdEventError& e)
Nagaraju Goruganticb781fe2018-04-06 13:41:01 -0500697 {
Andrew Geissler8ffdb262021-09-20 15:25:19 -0500698 error("Error occurred during the sdeventplus loop: {ERROR}", "ERROR",
699 e);
Nagaraju Goruganticb781fe2018-04-06 13:41:01 -0500700 phosphor::logging::commit<InternalFailure>();
701 }
Andrew Geisslera90a31a2016-12-13 16:16:28 -0600702}
703
Matt Spinler9eab9862018-07-11 14:13:52 -0500704void Chassis::serializeStateChangeTime()
705{
Allen.Wangba182f02022-03-23 19:01:53 +0800706 fs::path path{fmt::format(CHASSIS_STATE_CHANGE_PERSIST_PATH, id)};
Matt Spinler9eab9862018-07-11 14:13:52 -0500707 std::ofstream os(path.c_str(), std::ios::binary);
708 cereal::JSONOutputArchive oarchive(os);
709
710 oarchive(ChassisInherit::lastStateChangeTime(),
711 ChassisInherit::currentPowerState());
712}
713
714bool Chassis::deserializeStateChangeTime(uint64_t& time, PowerState& state)
715{
Allen.Wangba182f02022-03-23 19:01:53 +0800716 fs::path path{fmt::format(CHASSIS_STATE_CHANGE_PERSIST_PATH, id)};
Matt Spinler9eab9862018-07-11 14:13:52 -0500717
718 try
719 {
720 if (fs::exists(path))
721 {
722 std::ifstream is(path.c_str(), std::ios::in | std::ios::binary);
723 cereal::JSONInputArchive iarchive(is);
724 iarchive(time, state);
725 return true;
726 }
727 }
Patrick Williams8583b3b2021-10-06 12:19:20 -0500728 catch (const std::exception& e)
Matt Spinler9eab9862018-07-11 14:13:52 -0500729 {
Andrew Geissler8ffdb262021-09-20 15:25:19 -0500730 error("deserialize exception: {ERROR}", "ERROR", e);
Matt Spinler9eab9862018-07-11 14:13:52 -0500731 fs::remove(path);
732 }
733
734 return false;
735}
736
737void Chassis::restoreChassisStateChangeTime()
738{
739 uint64_t time;
740 PowerState state;
741
742 if (!deserializeStateChangeTime(time, state))
743 {
744 ChassisInherit::lastStateChangeTime(0);
745 }
746 else
747 {
748 ChassisInherit::lastStateChangeTime(time);
749 }
750}
751
752void Chassis::setStateChangeTime()
753{
754 using namespace std::chrono;
755 uint64_t lastTime;
756 PowerState lastState;
757
758 auto now =
759 duration_cast<milliseconds>(system_clock::now().time_since_epoch())
760 .count();
761
762 // If power is on when the BMC is rebooted, this function will get called
763 // because sysStateChange() runs. Since the power state didn't change
764 // in this case, neither should the state change time, so check that
765 // the power state actually did change here.
766 if (deserializeStateChangeTime(lastTime, lastState))
767 {
768 if (lastState == ChassisInherit::currentPowerState())
769 {
770 return;
771 }
772 }
773
774 ChassisInherit::lastStateChangeTime(now);
775 serializeStateChangeTime();
776}
777
Ben Tyner2c36e5a2021-07-12 14:56:49 -0500778bool Chassis::standbyVoltageRegulatorFault()
779{
780 bool regulatorFault = false;
781
Andrew Geisslerf8ae6a02022-01-21 17:00:20 -0600782 // find standby voltage regulator fault via gpiog
Ben Tyner2c36e5a2021-07-12 14:56:49 -0500783
Andrew Geisslerf8ae6a02022-01-21 17:00:20 -0600784 auto gpioval = utils::getGpioValue("regulator-standby-faulted");
785
786 if (-1 == gpioval)
Ben Tyner2c36e5a2021-07-12 14:56:49 -0500787 {
Andrew Geisslerf8ae6a02022-01-21 17:00:20 -0600788 error("Failed reading regulator-standby-faulted GPIO");
Ben Tyner2c36e5a2021-07-12 14:56:49 -0500789 }
Andrew Geisslerf8ae6a02022-01-21 17:00:20 -0600790
791 if (1 == gpioval)
792 {
793 info("Detected standby voltage regulator fault");
794 regulatorFault = true;
795 }
796
Ben Tyner2c36e5a2021-07-12 14:56:49 -0500797 return regulatorFault;
798}
799
Andrew Geisslera90a31a2016-12-13 16:16:28 -0600800} // namespace manager
801} // namespace state
Andrew Geisslera965cf02018-08-31 08:37:05 -0700802} // namespace phosphor