blob: e6a3225aaac0c302a1a96891acd216524cec9914 [file] [log] [blame]
Brandon Wymana0f33ce2019-10-17 18:32:29 -05001#include "psu_manager.hpp"
2
3#include "utility.hpp"
4
Brandon Wymanb76ab242020-09-16 18:06:06 -05005#include <fmt/format.h>
6#include <sys/types.h>
7#include <unistd.h>
8
Brandon Wymanecbecbc2021-08-31 22:53:21 +00009#include <regex>
10
Brandon Wymanaed1f752019-11-25 18:10:52 -060011using namespace phosphor::logging;
12
Brandon Wyman63ea78b2020-09-24 16:49:09 -050013namespace phosphor::power::manager
Brandon Wymana0f33ce2019-10-17 18:32:29 -050014{
Adriana Kobylakc9b05732022-03-19 15:15:10 +000015constexpr auto managerBusName = "xyz.openbmc_project.Power.PSUMonitor";
16constexpr auto objectManagerObjPath =
17 "/xyz/openbmc_project/power/power_supplies";
18constexpr auto powerSystemsInputsObjPath =
19 "/xyz/openbmc_project/power/power_supplies/chassis0/psus";
Brandon Wymana0f33ce2019-10-17 18:32:29 -050020
Brandon Wyman510acaa2020-11-05 18:32:04 -060021constexpr auto IBMCFFPSInterface =
22 "xyz.openbmc_project.Configuration.IBMCFFPSConnector";
23constexpr auto i2cBusProp = "I2CBus";
24constexpr auto i2cAddressProp = "I2CAddress";
25constexpr auto psuNameProp = "Name";
B. J. Wyman681b2a32021-04-20 22:31:22 +000026constexpr auto presLineName = "NamedPresenceGpio";
Brandon Wyman510acaa2020-11-05 18:32:04 -060027
Adriana Kobylak9bab9e12021-02-24 15:32:03 -060028constexpr auto supportedConfIntf =
29 "xyz.openbmc_project.Configuration.SupportedConfiguration";
Adriana Kobylak9bab9e12021-02-24 15:32:03 -060030
Brandon Wyman510acaa2020-11-05 18:32:04 -060031PSUManager::PSUManager(sdbusplus::bus::bus& bus, const sdeventplus::Event& e) :
Adriana Kobylakc9b05732022-03-19 15:15:10 +000032 bus(bus), powerSystemInputs(bus, powerSystemsInputsObjPath),
33 objectManager(bus, objectManagerObjPath)
Brandon Wyman510acaa2020-11-05 18:32:04 -060034{
Brandon Wyman510acaa2020-11-05 18:32:04 -060035 // Subscribe to InterfacesAdded before doing a property read, otherwise
36 // the interface could be created after the read attempt but before the
37 // match is created.
38 entityManagerIfacesAddedMatch = std::make_unique<sdbusplus::bus::match_t>(
39 bus,
40 sdbusplus::bus::match::rules::interfacesAdded() +
41 sdbusplus::bus::match::rules::sender(
42 "xyz.openbmc_project.EntityManager"),
43 std::bind(&PSUManager::entityManagerIfaceAdded, this,
44 std::placeholders::_1));
45 getPSUConfiguration();
46 getSystemProperties();
47
Adriana Kobylakc9b05732022-03-19 15:15:10 +000048 // Request the bus name before the analyze() function, which is the one that
49 // determines the brownout condition and sets the status d-bus property.
50 bus.request_name(managerBusName);
51
Brandon Wyman510acaa2020-11-05 18:32:04 -060052 using namespace sdeventplus;
53 auto interval = std::chrono::milliseconds(1000);
54 timer = std::make_unique<utility::Timer<ClockId::Monotonic>>(
55 e, std::bind(&PSUManager::analyze, this), interval);
56
Adriana Kobylaka4d38fa2021-10-05 19:57:47 +000057 validationTimer = std::make_unique<utility::Timer<ClockId::Monotonic>>(
58 e, std::bind(&PSUManager::validateConfig, this));
59
Adriana Kobylakc0a07582021-10-13 15:52:25 +000060 try
61 {
62 powerConfigGPIO = createGPIO("power-config-full-load");
63 }
64 catch (const std::exception& e)
65 {
66 // Ignore error, GPIO may not be implemented in this system.
67 powerConfigGPIO = nullptr;
68 }
69
Brandon Wyman510acaa2020-11-05 18:32:04 -060070 // Subscribe to power state changes
71 powerService = util::getService(POWER_OBJ_PATH, POWER_IFACE, bus);
72 powerOnMatch = std::make_unique<sdbusplus::bus::match_t>(
73 bus,
74 sdbusplus::bus::match::rules::propertiesChanged(POWER_OBJ_PATH,
75 POWER_IFACE),
76 [this](auto& msg) { this->powerStateChanged(msg); });
77
78 initialize();
79}
80
Brandon Wyman510acaa2020-11-05 18:32:04 -060081void PSUManager::getPSUConfiguration()
82{
83 using namespace phosphor::power::util;
84 auto depth = 0;
85 auto objects = getSubTree(bus, "/", IBMCFFPSInterface, depth);
86
87 psus.clear();
88
89 // I should get a map of objects back.
90 // Each object will have a path, a service, and an interface.
91 // The interface should match the one passed into this function.
92 for (const auto& [path, services] : objects)
93 {
94 auto service = services.begin()->first;
95
96 if (path.empty() || service.empty())
97 {
98 continue;
99 }
100
101 // For each object in the array of objects, I want to get properties
102 // from the service, path, and interface.
103 auto properties =
104 getAllProperties(bus, path, IBMCFFPSInterface, service);
105
106 getPSUProperties(properties);
107 }
108
109 if (psus.empty())
110 {
111 // Interface or properties not found. Let the Interfaces Added callback
112 // process the information once the interfaces are added to D-Bus.
113 log<level::INFO>(fmt::format("No power supplies to monitor").c_str());
114 }
115}
116
117void PSUManager::getPSUProperties(util::DbusPropertyMap& properties)
118{
119 // From passed in properties, I want to get: I2CBus, I2CAddress,
120 // and Name. Create a power supply object, using Name to build the inventory
121 // path.
122 const auto basePSUInvPath =
123 "/xyz/openbmc_project/inventory/system/chassis/motherboard/powersupply";
124 uint64_t* i2cbus = nullptr;
125 uint64_t* i2caddr = nullptr;
126 std::string* psuname = nullptr;
B. J. Wyman681b2a32021-04-20 22:31:22 +0000127 std::string* preslineptr = nullptr;
Brandon Wyman510acaa2020-11-05 18:32:04 -0600128
129 for (const auto& property : properties)
130 {
131 try
132 {
133 if (property.first == i2cBusProp)
134 {
135 i2cbus = std::get_if<uint64_t>(&properties[i2cBusProp]);
136 }
137 else if (property.first == i2cAddressProp)
138 {
139 i2caddr = std::get_if<uint64_t>(&properties[i2cAddressProp]);
140 }
141 else if (property.first == psuNameProp)
142 {
143 psuname = std::get_if<std::string>(&properties[psuNameProp]);
144 }
B. J. Wyman681b2a32021-04-20 22:31:22 +0000145 else if (property.first == presLineName)
146 {
147 preslineptr =
148 std::get_if<std::string>(&properties[presLineName]);
149 }
Brandon Wyman510acaa2020-11-05 18:32:04 -0600150 }
Patrick Williamsc1d4de52021-10-06 12:45:57 -0500151 catch (const std::exception& e)
Adriana Kobylak0c9a33d2021-09-13 18:05:09 +0000152 {}
Brandon Wyman510acaa2020-11-05 18:32:04 -0600153 }
154
155 if ((i2cbus) && (i2caddr) && (psuname) && (!psuname->empty()))
156 {
157 std::string invpath = basePSUInvPath;
158 invpath.push_back(psuname->back());
B. J. Wyman681b2a32021-04-20 22:31:22 +0000159 std::string presline = "";
Brandon Wyman510acaa2020-11-05 18:32:04 -0600160
161 log<level::DEBUG>(fmt::format("Inventory Path: {}", invpath).c_str());
162
B. J. Wyman681b2a32021-04-20 22:31:22 +0000163 if (nullptr != preslineptr)
164 {
165 presline = *preslineptr;
166 }
167
Brandon Wymanecbecbc2021-08-31 22:53:21 +0000168 auto invMatch =
169 std::find_if(psus.begin(), psus.end(), [&invpath](auto& psu) {
170 return psu->getInventoryPath() == invpath;
171 });
172 if (invMatch != psus.end())
173 {
174 // This power supply has the same inventory path as the one with
175 // information just added to D-Bus.
176 // Changes to GPIO line name unlikely, so skip checking.
177 // Changes to the I2C bus and address unlikely, as that would
178 // require corresponding device tree updates.
179 // Return out to avoid duplicate object creation.
180 return;
181 }
182
B. J. Wyman681b2a32021-04-20 22:31:22 +0000183 log<level::DEBUG>(
184 fmt::format("make PowerSupply bus: {} addr: {} presline: {}",
185 *i2cbus, *i2caddr, presline)
186 .c_str());
187 auto psu = std::make_unique<PowerSupply>(bus, invpath, *i2cbus,
188 *i2caddr, presline);
Brandon Wyman510acaa2020-11-05 18:32:04 -0600189 psus.emplace_back(std::move(psu));
Adriana Kobylak9ba38232021-11-16 20:27:45 +0000190
191 // Subscribe to power supply presence changes
192 auto presenceMatch = std::make_unique<sdbusplus::bus::match_t>(
193 bus,
194 sdbusplus::bus::match::rules::propertiesChanged(invpath,
195 INVENTORY_IFACE),
196 [this](auto& msg) { this->presenceChanged(msg); });
197 presenceMatches.emplace_back(std::move(presenceMatch));
Brandon Wyman510acaa2020-11-05 18:32:04 -0600198 }
199
200 if (psus.empty())
201 {
202 log<level::INFO>(fmt::format("No power supplies to monitor").c_str());
203 }
204}
205
Adriana Kobylake1074d82021-03-16 20:46:44 +0000206void PSUManager::populateSysProperties(const util::DbusPropertyMap& properties)
207{
208 try
209 {
Adriana Kobylak9ea66a62021-03-24 17:54:14 +0000210 auto propIt = properties.find("SupportedType");
211 if (propIt == properties.end())
212 {
213 return;
214 }
215 const std::string* type = std::get_if<std::string>(&(propIt->second));
216 if ((type == nullptr) || (*type != "PowerSupply"))
217 {
218 return;
219 }
220
Adriana Kobylak9ea66a62021-03-24 17:54:14 +0000221 propIt = properties.find("SupportedModel");
222 if (propIt == properties.end())
223 {
224 return;
225 }
Adriana Kobylakd3a70d92021-06-04 16:24:45 +0000226 const std::string* model = std::get_if<std::string>(&(propIt->second));
227 if (model == nullptr)
Adriana Kobylak9ea66a62021-03-24 17:54:14 +0000228 {
229 return;
230 }
Adriana Kobylak9ea66a62021-03-24 17:54:14 +0000231
Adriana Kobylakd3a70d92021-06-04 16:24:45 +0000232 sys_properties sys;
Adriana Kobylak9ea66a62021-03-24 17:54:14 +0000233 propIt = properties.find("RedundantCount");
Adriana Kobylake1074d82021-03-16 20:46:44 +0000234 if (propIt != properties.end())
235 {
236 const uint64_t* count = std::get_if<uint64_t>(&(propIt->second));
237 if (count != nullptr)
238 {
Adriana Kobylakd3a70d92021-06-04 16:24:45 +0000239 sys.powerSupplyCount = *count;
Adriana Kobylake1074d82021-03-16 20:46:44 +0000240 }
241 }
Adriana Kobylak9ea66a62021-03-24 17:54:14 +0000242 propIt = properties.find("InputVoltage");
243 if (propIt != properties.end())
244 {
Adriana Kobylakd3a70d92021-06-04 16:24:45 +0000245 const std::vector<uint64_t>* voltage =
246 std::get_if<std::vector<uint64_t>>(&(propIt->second));
Adriana Kobylak9ea66a62021-03-24 17:54:14 +0000247 if (voltage != nullptr)
248 {
249 sys.inputVoltage = *voltage;
250 }
251 }
252
Adriana Kobylak886574c2021-11-01 18:22:28 +0000253 // The PowerConfigFullLoad is an optional property, default it to false
254 // since that's the default value of the power-config-full-load GPIO.
255 sys.powerConfigFullLoad = false;
256 propIt = properties.find("PowerConfigFullLoad");
257 if (propIt != properties.end())
258 {
259 const bool* fullLoad = std::get_if<bool>(&(propIt->second));
260 if (fullLoad != nullptr)
261 {
262 sys.powerConfigFullLoad = *fullLoad;
263 }
264 }
265
Adriana Kobylakd3a70d92021-06-04 16:24:45 +0000266 supportedConfigs.emplace(*model, sys);
Adriana Kobylake1074d82021-03-16 20:46:44 +0000267 }
Patrick Williamsc1d4de52021-10-06 12:45:57 -0500268 catch (const std::exception& e)
Adriana Kobylak0c9a33d2021-09-13 18:05:09 +0000269 {}
Adriana Kobylake1074d82021-03-16 20:46:44 +0000270}
271
Adriana Kobylak9bab9e12021-02-24 15:32:03 -0600272void PSUManager::getSystemProperties()
273{
Adriana Kobylak9bab9e12021-02-24 15:32:03 -0600274
Adriana Kobylak9bab9e12021-02-24 15:32:03 -0600275 try
276 {
277 util::DbusSubtree subtree =
278 util::getSubTree(bus, INVENTORY_OBJ_PATH, supportedConfIntf, 0);
Adriana Kobylake1074d82021-03-16 20:46:44 +0000279 if (subtree.empty())
Adriana Kobylak9bab9e12021-02-24 15:32:03 -0600280 {
281 throw std::runtime_error("Supported Configuration Not Found");
282 }
Adriana Kobylak9bab9e12021-02-24 15:32:03 -0600283
Adriana Kobylake1074d82021-03-16 20:46:44 +0000284 for (const auto& [objPath, services] : subtree)
285 {
286 std::string service = services.begin()->first;
287 if (objPath.empty() || service.empty())
288 {
289 continue;
Adriana Kobylak9bab9e12021-02-24 15:32:03 -0600290 }
Adriana Kobylake1074d82021-03-16 20:46:44 +0000291 auto properties = util::getAllProperties(
292 bus, objPath, supportedConfIntf, service);
293 populateSysProperties(properties);
Adriana Kobylak9bab9e12021-02-24 15:32:03 -0600294 }
295 }
Patrick Williamsc1d4de52021-10-06 12:45:57 -0500296 catch (const std::exception& e)
Adriana Kobylak9bab9e12021-02-24 15:32:03 -0600297 {
298 // Interface or property not found. Let the Interfaces Added callback
299 // process the information once the interfaces are added to D-Bus.
300 }
301}
302
Brandon Wyman3e429132021-03-18 18:03:14 -0500303void PSUManager::entityManagerIfaceAdded(sdbusplus::message::message& msg)
Adriana Kobylak9bab9e12021-02-24 15:32:03 -0600304{
305 try
306 {
307 sdbusplus::message::object_path objPath;
Adriana Kobylake1074d82021-03-16 20:46:44 +0000308 std::map<std::string, std::map<std::string, util::DbusVariant>>
Adriana Kobylak9bab9e12021-02-24 15:32:03 -0600309 interfaces;
310 msg.read(objPath, interfaces);
311
312 auto itIntf = interfaces.find(supportedConfIntf);
Brandon Wyman510acaa2020-11-05 18:32:04 -0600313 if (itIntf != interfaces.cend())
Adriana Kobylak9bab9e12021-02-24 15:32:03 -0600314 {
Brandon Wyman510acaa2020-11-05 18:32:04 -0600315 populateSysProperties(itIntf->second);
Adriana Kobylak9bab9e12021-02-24 15:32:03 -0600316 }
317
Brandon Wyman510acaa2020-11-05 18:32:04 -0600318 itIntf = interfaces.find(IBMCFFPSInterface);
319 if (itIntf != interfaces.cend())
320 {
321 log<level::INFO>(
322 fmt::format("InterfacesAdded for: {}", IBMCFFPSInterface)
323 .c_str());
324 getPSUProperties(itIntf->second);
325 }
Adriana Kobylak8f16fb52021-03-31 15:50:15 +0000326
327 // Call to validate the psu configuration if the power is on and both
328 // the IBMCFFPSConnector and SupportedConfiguration interfaces have been
329 // processed
330 if (powerOn && !psus.empty() && !supportedConfigs.empty())
331 {
Adriana Kobylaka4d38fa2021-10-05 19:57:47 +0000332 validationTimer->restartOnce(validationTimeout);
Adriana Kobylak8f16fb52021-03-31 15:50:15 +0000333 }
Adriana Kobylak9bab9e12021-02-24 15:32:03 -0600334 }
Patrick Williamsc1d4de52021-10-06 12:45:57 -0500335 catch (const std::exception& e)
Adriana Kobylak9bab9e12021-02-24 15:32:03 -0600336 {
337 // Ignore, the property may be of a different type than expected.
338 }
339}
340
Brandon Wymana0f33ce2019-10-17 18:32:29 -0500341void PSUManager::powerStateChanged(sdbusplus::message::message& msg)
342{
343 int32_t state = 0;
344 std::string msgSensor;
Patrick Williamsabe49412020-05-13 17:59:47 -0500345 std::map<std::string, std::variant<int32_t>> msgData;
Brandon Wymana0f33ce2019-10-17 18:32:29 -0500346 msg.read(msgSensor, msgData);
347
348 // Check if it was the Present property that changed.
349 auto valPropMap = msgData.find("state");
350 if (valPropMap != msgData.end())
351 {
352 state = std::get<int32_t>(valPropMap->second);
353
354 // Power is on when state=1. Clear faults.
355 if (state)
356 {
357 powerOn = true;
Adriana Kobylaka4d38fa2021-10-05 19:57:47 +0000358 validationTimer->restartOnce(validationTimeout);
Brandon Wymana0f33ce2019-10-17 18:32:29 -0500359 clearFaults();
Adriana Kobylakc0a07582021-10-13 15:52:25 +0000360 setPowerConfigGPIO();
Brandon Wymana0f33ce2019-10-17 18:32:29 -0500361 }
362 else
363 {
364 powerOn = false;
Adriana Kobylak8f16fb52021-03-31 15:50:15 +0000365 runValidateConfig = true;
Adriana Kobylak2549d792022-01-26 20:51:30 +0000366 brownoutLogged = false;
Brandon Wymana0f33ce2019-10-17 18:32:29 -0500367 }
368 }
369}
370
Adriana Kobylak9ba38232021-11-16 20:27:45 +0000371void PSUManager::presenceChanged(sdbusplus::message::message& msg)
372{
373 std::string msgSensor;
374 std::map<std::string, std::variant<uint32_t, bool>> msgData;
375 msg.read(msgSensor, msgData);
376
377 // Check if it was the Present property that changed.
378 auto valPropMap = msgData.find(PRESENT_PROP);
379 if (valPropMap != msgData.end())
380 {
381 if (std::get<bool>(valPropMap->second))
382 {
383 // A PSU became present, force the PSU validation to run.
384 runValidateConfig = true;
385 validationTimer->restartOnce(validationTimeout);
386 }
387 }
388}
389
Brandon Wyman10fc6e82022-02-08 20:51:22 +0000390void PSUManager::setPowerSupplyError(const std::string& psuErrorString)
391{
392 using namespace sdbusplus::xyz::openbmc_project;
393 constexpr auto service = "org.openbmc.control.Power";
394 constexpr auto objPath = "/org/openbmc/control/power0";
395 constexpr auto interface = "org.openbmc.control.Power";
396 constexpr auto method = "setPowerSupplyError";
397
398 try
399 {
400 // Call D-Bus method to inform pseq of PSU error
401 auto methodMsg =
402 bus.new_method_call(service, objPath, interface, method);
403 methodMsg.append(psuErrorString);
404 auto callReply = bus.call(methodMsg);
405 }
406 catch (const std::exception& e)
407 {
408 log<level::INFO>(
409 fmt::format("Failed calling setPowerSupplyError due to error {}",
410 e.what())
411 .c_str());
412 }
413}
414
Brandon Wyman8b662882021-10-08 17:31:51 +0000415void PSUManager::createError(const std::string& faultName,
416 std::map<std::string, std::string>& additionalData)
Brandon Wymanb76ab242020-09-16 18:06:06 -0500417{
418 using namespace sdbusplus::xyz::openbmc_project;
419 constexpr auto loggingObjectPath = "/xyz/openbmc_project/logging";
420 constexpr auto loggingCreateInterface =
421 "xyz.openbmc_project.Logging.Create";
422
423 try
424 {
Brandon Wyman8b662882021-10-08 17:31:51 +0000425 additionalData["_PID"] = std::to_string(getpid());
426
Brandon Wymanb76ab242020-09-16 18:06:06 -0500427 auto service =
428 util::getService(loggingObjectPath, loggingCreateInterface, bus);
429
430 if (service.empty())
431 {
432 log<level::ERR>("Unable to get logging manager service");
433 return;
434 }
435
436 auto method = bus.new_method_call(service.c_str(), loggingObjectPath,
437 loggingCreateInterface, "Create");
438
439 auto level = Logging::server::Entry::Level::Error;
440 method.append(faultName, level, additionalData);
441
442 auto reply = bus.call(method);
Brandon Wyman10fc6e82022-02-08 20:51:22 +0000443 setPowerSupplyError(faultName);
Brandon Wymanb76ab242020-09-16 18:06:06 -0500444 }
Patrick Williamsc1d4de52021-10-06 12:45:57 -0500445 catch (const std::exception& e)
Brandon Wymanb76ab242020-09-16 18:06:06 -0500446 {
447 log<level::ERR>(
448 fmt::format(
449 "Failed creating event log for fault {} due to error {}",
450 faultName, e.what())
451 .c_str());
452 }
453}
454
Brandon Wyman63ea78b2020-09-24 16:49:09 -0500455void PSUManager::analyze()
456{
457 for (auto& psu : psus)
458 {
459 psu->analyze();
460 }
461
Brandon Wyman3180f4d2020-12-08 17:53:46 -0600462 if (powerOn)
Brandon Wyman63ea78b2020-09-24 16:49:09 -0500463 {
Adriana Kobylakf2ba1462021-06-24 15:16:17 +0000464 std::map<std::string, std::string> additionalData;
Adriana Kobylake4c3a292022-02-10 16:07:03 +0000465
466 auto notPresentCount = decltype(psus.size())(
467 std::count_if(psus.begin(), psus.end(),
468 [](const auto& psu) { return !psu->isPresent(); }));
469
470 auto hasVINUVFaultCount = decltype(psus.size())(
471 std::count_if(psus.begin(), psus.end(), [](const auto& psu) {
472 return psu->hasVINUVFault();
473 }));
474
475 // The PSU D-Bus objects may not be available yet, so ignore if all
476 // PSUs are not present or the number of PSUs is still 0.
477 if ((psus.size() == (notPresentCount + hasVINUVFaultCount)) &&
478 (psus.size() != notPresentCount) && (psus.size() != 0))
479 {
480 // Brownout: All PSUs report an AC failure: At least one PSU reports
481 // AC loss VIN fault and the rest either report AC loss VIN fault as
482 // well or are not present.
483 if (!brownoutLogged)
484 {
485 createError(
486 "xyz.openbmc_project.State.Shutdown.Power.Error.Blackout",
487 additionalData);
488 brownoutLogged = true;
489 }
490 }
491 else
492 {
493 // Brownout condition is not present or has been cleared
494 brownoutLogged = false;
495 }
Adriana Kobylakf2ba1462021-06-24 15:16:17 +0000496
Brandon Wyman3180f4d2020-12-08 17:53:46 -0600497 for (auto& psu : psus)
Brandon Wyman63ea78b2020-09-24 16:49:09 -0500498 {
Brandon Wymanec0b8dc2021-10-08 21:49:43 +0000499 additionalData.clear();
Brandon Wyman39ea02b2021-11-23 23:22:23 +0000500
Brandon Wyman3180f4d2020-12-08 17:53:46 -0600501 if (!psu->isFaultLogged() && !psu->isPresent())
502 {
Brandon Wymanda369c72021-10-08 18:43:30 +0000503 std::map<std::string, std::string> requiredPSUsData;
504 auto requiredPSUsPresent = hasRequiredPSUs(requiredPSUsData);
Adriana Kobylakf2ba1462021-06-24 15:16:17 +0000505 if (!requiredPSUsPresent)
506 {
Brandon Wymanda369c72021-10-08 18:43:30 +0000507 additionalData.merge(requiredPSUsData);
Adriana Kobylakf2ba1462021-06-24 15:16:17 +0000508 // Create error for power supply missing.
509 additionalData["CALLOUT_INVENTORY_PATH"] =
510 psu->getInventoryPath();
511 additionalData["CALLOUT_PRIORITY"] = "H";
512 createError(
513 "xyz.openbmc_project.Power.PowerSupply.Error.Missing",
514 additionalData);
515 }
Brandon Wyman3180f4d2020-12-08 17:53:46 -0600516 psu->setFaultLogged();
517 }
518 else if (!psu->isFaultLogged() && psu->isFaulted())
519 {
Brandon Wyman786b6f42021-10-12 20:21:41 +0000520 // Add STATUS_WORD and STATUS_MFR last response, in padded
521 // hexadecimal format.
Brandon Wyman3180f4d2020-12-08 17:53:46 -0600522 additionalData["STATUS_WORD"] =
Brandon Wyman786b6f42021-10-12 20:21:41 +0000523 fmt::format("{:#04x}", psu->getStatusWord());
Jay Meyer10d94052020-11-30 14:41:21 -0600524 additionalData["STATUS_MFR"] =
Brandon Wyman786b6f42021-10-12 20:21:41 +0000525 fmt::format("{:#02x}", psu->getMFRFault());
Brandon Wyman3180f4d2020-12-08 17:53:46 -0600526 // If there are faults being reported, they possibly could be
527 // related to a bug in the firmware version running on the power
528 // supply. Capture that data into the error as well.
529 additionalData["FW_VERSION"] = psu->getFWVersion();
530
Brandon Wymanb85b9dd2021-10-19 21:25:17 +0000531 if (psu->hasCommFault())
532 {
Brandon Wyman85c7bf42021-10-19 22:28:48 +0000533 additionalData["STATUS_CML"] =
534 fmt::format("{:#02x}", psu->getStatusCML());
Brandon Wymanb85b9dd2021-10-19 21:25:17 +0000535 /* Attempts to communicate with the power supply have
536 * reached there limit. Create an error. */
537 additionalData["CALLOUT_DEVICE_PATH"] =
538 psu->getDevicePath();
539
540 createError(
541 "xyz.openbmc_project.Power.PowerSupply.Error.CommFault",
542 additionalData);
543
544 psu->setFaultLogged();
545 }
546 else if ((psu->hasInputFault() || psu->hasVINUVFault()))
Brandon Wyman3180f4d2020-12-08 17:53:46 -0600547 {
Brandon Wymanf07bc792021-10-12 19:00:35 +0000548 // Include STATUS_INPUT for input faults.
549 additionalData["STATUS_INPUT"] =
550 fmt::format("{:#02x}", psu->getStatusInput());
551
Brandon Wyman3180f4d2020-12-08 17:53:46 -0600552 /* The power supply location might be needed if the input
553 * fault is due to a problem with the power supply itself.
554 * Include the inventory path with a call out priority of
555 * low.
556 */
557 additionalData["CALLOUT_INVENTORY_PATH"] =
558 psu->getInventoryPath();
559 additionalData["CALLOUT_PRIORITY"] = "L";
560 createError("xyz.openbmc_project.Power.PowerSupply.Error."
561 "InputFault",
562 additionalData);
563 psu->setFaultLogged();
564 }
Brandon Wyman39ea02b2021-11-23 23:22:23 +0000565 else if (psu->hasPSKillFault())
566 {
567 createError(
568 "xyz.openbmc_project.Power.PowerSupply.Error.PSKillFault",
569 additionalData);
570 psu->setFaultLogged();
571 }
Brandon Wyman6710ba22021-10-27 17:39:31 +0000572 else if (psu->hasVoutOVFault())
573 {
574 // Include STATUS_VOUT for Vout faults.
575 additionalData["STATUS_VOUT"] =
576 fmt::format("{:#02x}", psu->getStatusVout());
577
578 additionalData["CALLOUT_INVENTORY_PATH"] =
579 psu->getInventoryPath();
580
581 createError(
582 "xyz.openbmc_project.Power.PowerSupply.Error.Fault",
583 additionalData);
584
585 psu->setFaultLogged();
586 }
Brandon Wymanb10b3be2021-11-09 22:12:15 +0000587 else if (psu->hasIoutOCFault())
588 {
589 // Include STATUS_IOUT for Iout faults.
590 additionalData["STATUS_IOUT"] =
591 fmt::format("{:#02x}", psu->getStatusIout());
592
593 createError(
594 "xyz.openbmc_project.Power.PowerSupply.Error.IoutOCFault",
595 additionalData);
596
597 psu->setFaultLogged();
598 }
Brandon Wyman39ea02b2021-11-23 23:22:23 +0000599 else if (psu->hasVoutUVFault() || psu->hasPS12VcsFault() ||
600 psu->hasPSCS12VFault())
Brandon Wyman2cf46942021-10-28 19:09:16 +0000601 {
602 // Include STATUS_VOUT for Vout faults.
603 additionalData["STATUS_VOUT"] =
604 fmt::format("{:#02x}", psu->getStatusVout());
605
606 additionalData["CALLOUT_INVENTORY_PATH"] =
607 psu->getInventoryPath();
608
609 createError(
610 "xyz.openbmc_project.Power.PowerSupply.Error.Fault",
611 additionalData);
612
613 psu->setFaultLogged();
614 }
Brandon Wyman7ee4d7e2021-11-19 20:48:23 +0000615 // A fan fault should have priority over a temperature fault,
616 // since a failed fan may lead to a temperature problem.
617 else if (psu->hasFanFault())
618 {
619 // Include STATUS_TEMPERATURE and STATUS_FANS_1_2
620 additionalData["STATUS_TEMPERATURE"] =
621 fmt::format("{:#02x}", psu->getStatusTemperature());
622 additionalData["STATUS_FANS_1_2"] =
623 fmt::format("{:#02x}", psu->getStatusFans12());
624
625 additionalData["CALLOUT_INVENTORY_PATH"] =
626 psu->getInventoryPath();
627
628 createError(
629 "xyz.openbmc_project.Power.PowerSupply.Error.FanFault",
630 additionalData);
631
632 psu->setFaultLogged();
633 }
Brandon Wyman96893a42021-11-05 19:56:57 +0000634 else if (psu->hasTempFault())
635 {
636 // Include STATUS_TEMPERATURE for temperature faults.
637 additionalData["STATUS_TEMPERATURE"] =
638 fmt::format("{:#02x}", psu->getStatusTemperature());
639
640 additionalData["CALLOUT_INVENTORY_PATH"] =
641 psu->getInventoryPath();
642
643 createError(
644 "xyz.openbmc_project.Power.PowerSupply.Error.Fault",
645 additionalData);
646
647 psu->setFaultLogged();
648 }
Brandon Wyman3180f4d2020-12-08 17:53:46 -0600649 else if (psu->hasMFRFault())
650 {
651 /* This can represent a variety of faults that result in
652 * calling out the power supply for replacement: Output
653 * OverCurrent, Output Under Voltage, and potentially other
654 * faults.
655 *
656 * Also plan on putting specific fault in AdditionalData,
657 * along with register names and register values
658 * (STATUS_WORD, STATUS_MFR, etc.).*/
659
660 additionalData["CALLOUT_INVENTORY_PATH"] =
661 psu->getInventoryPath();
662
663 createError(
664 "xyz.openbmc_project.Power.PowerSupply.Error.Fault",
Brandon Wyman52e54e82020-10-08 14:44:58 -0500665 additionalData);
Brandon Wyman63ea78b2020-09-24 16:49:09 -0500666
Brandon Wyman3180f4d2020-12-08 17:53:46 -0600667 psu->setFaultLogged();
668 }
Brandon Wyman2916ea52021-11-06 03:31:18 +0000669 else if (psu->hasPgoodFault())
670 {
671 /* POWER_GOOD# is not low, or OFF is on */
672 additionalData["CALLOUT_INVENTORY_PATH"] =
673 psu->getInventoryPath();
674
675 createError(
676 "xyz.openbmc_project.Power.PowerSupply.Error.Fault",
677 additionalData);
678
679 psu->setFaultLogged();
680 }
Brandon Wyman4176d6b2020-10-07 17:41:06 -0500681 }
Brandon Wyman63ea78b2020-09-24 16:49:09 -0500682 }
683 }
684}
685
Adriana Kobylak8f16fb52021-03-31 15:50:15 +0000686void PSUManager::validateConfig()
687{
Adriana Kobylak70e7f932021-06-10 18:53:56 +0000688 if (!runValidateConfig || supportedConfigs.empty())
Adriana Kobylak8f16fb52021-03-31 15:50:15 +0000689 {
690 return;
691 }
692
Adriana Kobylak4d9aaf92021-06-30 15:27:42 +0000693 std::map<std::string, std::string> additionalData;
694 auto supported = hasRequiredPSUs(additionalData);
695 if (supported)
696 {
697 runValidateConfig = false;
698 return;
699 }
700
701 // Validation failed, create an error log.
702 // Return without setting the runValidateConfig flag to false because
703 // it may be that an additional supported configuration interface is
704 // added and we need to validate it to see if it matches this system.
705 createError("xyz.openbmc_project.Power.PowerSupply.Error.NotSupported",
706 additionalData);
707}
708
709bool PSUManager::hasRequiredPSUs(
710 std::map<std::string, std::string>& additionalData)
711{
Adriana Kobylak8f16fb52021-03-31 15:50:15 +0000712 std::string model{};
Adriana Kobylak523704d2021-09-21 15:55:41 +0000713 if (!validateModelName(model, additionalData))
Adriana Kobylak8f16fb52021-03-31 15:50:15 +0000714 {
Adriana Kobylak523704d2021-09-21 15:55:41 +0000715 return false;
Adriana Kobylak8f16fb52021-03-31 15:50:15 +0000716 }
Adriana Kobylak70e7f932021-06-10 18:53:56 +0000717
Adriana Kobylak4d9aaf92021-06-30 15:27:42 +0000718 auto presentCount =
719 std::count_if(psus.begin(), psus.end(),
720 [](const auto& psu) { return psu->isPresent(); });
721
Adriana Kobylak70e7f932021-06-10 18:53:56 +0000722 // Validate the supported configurations. A system may support more than one
Adriana Kobylak4175ffb2021-08-02 14:51:05 +0000723 // power supply model configuration. Since all configurations need to be
724 // checked, the additional data would contain only the information of the
725 // last configuration that did not match.
726 std::map<std::string, std::string> tmpAdditionalData;
Adriana Kobylak70e7f932021-06-10 18:53:56 +0000727 for (const auto& config : supportedConfigs)
728 {
Adriana Kobylak4d9aaf92021-06-30 15:27:42 +0000729 if (config.first != model)
Adriana Kobylak70e7f932021-06-10 18:53:56 +0000730 {
731 continue;
732 }
733 if (presentCount != config.second.powerSupplyCount)
734 {
Adriana Kobylak4175ffb2021-08-02 14:51:05 +0000735 tmpAdditionalData.clear();
736 tmpAdditionalData["EXPECTED_COUNT"] =
Adriana Kobylak70e7f932021-06-10 18:53:56 +0000737 std::to_string(config.second.powerSupplyCount);
Adriana Kobylak4175ffb2021-08-02 14:51:05 +0000738 tmpAdditionalData["ACTUAL_COUNT"] = std::to_string(presentCount);
Adriana Kobylak70e7f932021-06-10 18:53:56 +0000739 continue;
740 }
Adriana Kobylak4175ffb2021-08-02 14:51:05 +0000741
742 bool voltageValidated = true;
743 for (const auto& psu : psus)
744 {
745 if (!psu->isPresent())
746 {
747 // Only present PSUs report a valid input voltage
748 continue;
749 }
750
751 double actualInputVoltage;
752 int inputVoltage;
753 psu->getInputVoltage(actualInputVoltage, inputVoltage);
754
755 if (std::find(config.second.inputVoltage.begin(),
756 config.second.inputVoltage.end(),
757 inputVoltage) == config.second.inputVoltage.end())
758 {
759 tmpAdditionalData.clear();
760 tmpAdditionalData["ACTUAL_VOLTAGE"] =
761 std::to_string(actualInputVoltage);
762 for (const auto& voltage : config.second.inputVoltage)
763 {
764 tmpAdditionalData["EXPECTED_VOLTAGE"] +=
765 std::to_string(voltage) + " ";
766 }
767 tmpAdditionalData["CALLOUT_INVENTORY_PATH"] =
768 psu->getInventoryPath();
769
770 voltageValidated = false;
771 break;
772 }
773 }
774 if (!voltageValidated)
775 {
776 continue;
777 }
778
Adriana Kobylak4d9aaf92021-06-30 15:27:42 +0000779 return true;
Adriana Kobylak70e7f932021-06-10 18:53:56 +0000780 }
Adriana Kobylak70e7f932021-06-10 18:53:56 +0000781
Adriana Kobylak4175ffb2021-08-02 14:51:05 +0000782 additionalData.insert(tmpAdditionalData.begin(), tmpAdditionalData.end());
Adriana Kobylak4d9aaf92021-06-30 15:27:42 +0000783 return false;
Adriana Kobylak8f16fb52021-03-31 15:50:15 +0000784}
785
Adriana Kobylak523704d2021-09-21 15:55:41 +0000786bool PSUManager::validateModelName(
787 std::string& model, std::map<std::string, std::string>& additionalData)
788{
789 // Check that all PSUs have the same model name. Initialize the model
790 // variable with the first PSU name found, then use it as a base to compare
Adriana Kobylakb70eae92022-01-20 22:09:56 +0000791 // against the rest of the PSUs and get its inventory path to use as callout
792 // if needed.
Adriana Kobylak523704d2021-09-21 15:55:41 +0000793 model.clear();
Adriana Kobylakb70eae92022-01-20 22:09:56 +0000794 std::string modelInventoryPath{};
Adriana Kobylak523704d2021-09-21 15:55:41 +0000795 for (const auto& psu : psus)
796 {
797 auto psuModel = psu->getModelName();
798 if (psuModel.empty())
799 {
800 continue;
801 }
802 if (model.empty())
803 {
804 model = psuModel;
Adriana Kobylakb70eae92022-01-20 22:09:56 +0000805 modelInventoryPath = psu->getInventoryPath();
Adriana Kobylak523704d2021-09-21 15:55:41 +0000806 continue;
807 }
808 if (psuModel != model)
809 {
Adriana Kobylakb70eae92022-01-20 22:09:56 +0000810 if (supportedConfigs.find(model) != supportedConfigs.end())
811 {
812 // The base model is supported, callout the mismatched PSU. The
813 // mismatched PSU may or may not be supported.
814 additionalData["EXPECTED_MODEL"] = model;
815 additionalData["ACTUAL_MODEL"] = psuModel;
816 additionalData["CALLOUT_INVENTORY_PATH"] =
817 psu->getInventoryPath();
818 }
819 else if (supportedConfigs.find(psuModel) != supportedConfigs.end())
820 {
821 // The base model is not supported, but the mismatched PSU is,
822 // callout the base PSU.
823 additionalData["EXPECTED_MODEL"] = psuModel;
824 additionalData["ACTUAL_MODEL"] = model;
825 additionalData["CALLOUT_INVENTORY_PATH"] = modelInventoryPath;
826 }
827 else
828 {
829 // The base model and the mismatched PSU are not supported or
830 // could not be found in the supported configuration, callout
831 // the mismatched PSU.
832 additionalData["EXPECTED_MODEL"] = model;
833 additionalData["ACTUAL_MODEL"] = psuModel;
834 additionalData["CALLOUT_INVENTORY_PATH"] =
835 psu->getInventoryPath();
836 }
Adriana Kobylak523704d2021-09-21 15:55:41 +0000837 model.clear();
838 return false;
839 }
840 }
841 return true;
842}
843
Adriana Kobylakc0a07582021-10-13 15:52:25 +0000844void PSUManager::setPowerConfigGPIO()
845{
846 if (!powerConfigGPIO)
847 {
848 return;
849 }
850
851 std::string model{};
852 std::map<std::string, std::string> additionalData;
853 if (!validateModelName(model, additionalData))
854 {
855 return;
856 }
857
858 auto config = supportedConfigs.find(model);
859 if (config != supportedConfigs.end())
860 {
861 // The power-config-full-load is an open drain GPIO. Set it to low (0)
862 // if the supported configuration indicates that this system model
863 // expects the maximum number of power supplies (full load set to true).
864 // Else, set it to high (1), this is the default.
865 auto powerConfigValue =
866 (config->second.powerConfigFullLoad == true ? 0 : 1);
867 auto flags = gpiod::line_request::FLAG_OPEN_DRAIN;
868 powerConfigGPIO->write(powerConfigValue, flags);
869 }
870}
871
Brandon Wyman63ea78b2020-09-24 16:49:09 -0500872} // namespace phosphor::power::manager