blob: 6fa824ae56530c935ad669749cdd777962866e7e [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{
15
Brandon Wyman510acaa2020-11-05 18:32:04 -060016constexpr auto IBMCFFPSInterface =
17 "xyz.openbmc_project.Configuration.IBMCFFPSConnector";
18constexpr auto i2cBusProp = "I2CBus";
19constexpr auto i2cAddressProp = "I2CAddress";
20constexpr auto psuNameProp = "Name";
B. J. Wyman681b2a32021-04-20 22:31:22 +000021constexpr auto presLineName = "NamedPresenceGpio";
Brandon Wyman510acaa2020-11-05 18:32:04 -060022
Adriana Kobylak9bab9e12021-02-24 15:32:03 -060023constexpr auto supportedConfIntf =
24 "xyz.openbmc_project.Configuration.SupportedConfiguration";
Adriana Kobylak9bab9e12021-02-24 15:32:03 -060025
Brandon Wyman510acaa2020-11-05 18:32:04 -060026PSUManager::PSUManager(sdbusplus::bus::bus& bus, const sdeventplus::Event& e) :
27 bus(bus)
28{
Brandon Wyman510acaa2020-11-05 18:32:04 -060029 // Subscribe to InterfacesAdded before doing a property read, otherwise
30 // the interface could be created after the read attempt but before the
31 // match is created.
32 entityManagerIfacesAddedMatch = std::make_unique<sdbusplus::bus::match_t>(
33 bus,
34 sdbusplus::bus::match::rules::interfacesAdded() +
35 sdbusplus::bus::match::rules::sender(
36 "xyz.openbmc_project.EntityManager"),
37 std::bind(&PSUManager::entityManagerIfaceAdded, this,
38 std::placeholders::_1));
39 getPSUConfiguration();
40 getSystemProperties();
41
42 using namespace sdeventplus;
43 auto interval = std::chrono::milliseconds(1000);
44 timer = std::make_unique<utility::Timer<ClockId::Monotonic>>(
45 e, std::bind(&PSUManager::analyze, this), interval);
46
Adriana Kobylaka4d38fa2021-10-05 19:57:47 +000047 validationTimer = std::make_unique<utility::Timer<ClockId::Monotonic>>(
48 e, std::bind(&PSUManager::validateConfig, this));
49
Adriana Kobylakc0a07582021-10-13 15:52:25 +000050 try
51 {
52 powerConfigGPIO = createGPIO("power-config-full-load");
53 }
54 catch (const std::exception& e)
55 {
56 // Ignore error, GPIO may not be implemented in this system.
57 powerConfigGPIO = nullptr;
58 }
59
Brandon Wyman510acaa2020-11-05 18:32:04 -060060 // Subscribe to power state changes
61 powerService = util::getService(POWER_OBJ_PATH, POWER_IFACE, bus);
62 powerOnMatch = std::make_unique<sdbusplus::bus::match_t>(
63 bus,
64 sdbusplus::bus::match::rules::propertiesChanged(POWER_OBJ_PATH,
65 POWER_IFACE),
66 [this](auto& msg) { this->powerStateChanged(msg); });
67
68 initialize();
69}
70
Brandon Wyman510acaa2020-11-05 18:32:04 -060071void PSUManager::getPSUConfiguration()
72{
73 using namespace phosphor::power::util;
74 auto depth = 0;
75 auto objects = getSubTree(bus, "/", IBMCFFPSInterface, depth);
76
77 psus.clear();
78
79 // I should get a map of objects back.
80 // Each object will have a path, a service, and an interface.
81 // The interface should match the one passed into this function.
82 for (const auto& [path, services] : objects)
83 {
84 auto service = services.begin()->first;
85
86 if (path.empty() || service.empty())
87 {
88 continue;
89 }
90
91 // For each object in the array of objects, I want to get properties
92 // from the service, path, and interface.
93 auto properties =
94 getAllProperties(bus, path, IBMCFFPSInterface, service);
95
96 getPSUProperties(properties);
97 }
98
99 if (psus.empty())
100 {
101 // Interface or properties not found. Let the Interfaces Added callback
102 // process the information once the interfaces are added to D-Bus.
103 log<level::INFO>(fmt::format("No power supplies to monitor").c_str());
104 }
105}
106
107void PSUManager::getPSUProperties(util::DbusPropertyMap& properties)
108{
109 // From passed in properties, I want to get: I2CBus, I2CAddress,
110 // and Name. Create a power supply object, using Name to build the inventory
111 // path.
112 const auto basePSUInvPath =
113 "/xyz/openbmc_project/inventory/system/chassis/motherboard/powersupply";
114 uint64_t* i2cbus = nullptr;
115 uint64_t* i2caddr = nullptr;
116 std::string* psuname = nullptr;
B. J. Wyman681b2a32021-04-20 22:31:22 +0000117 std::string* preslineptr = nullptr;
Brandon Wyman510acaa2020-11-05 18:32:04 -0600118
119 for (const auto& property : properties)
120 {
121 try
122 {
123 if (property.first == i2cBusProp)
124 {
125 i2cbus = std::get_if<uint64_t>(&properties[i2cBusProp]);
126 }
127 else if (property.first == i2cAddressProp)
128 {
129 i2caddr = std::get_if<uint64_t>(&properties[i2cAddressProp]);
130 }
131 else if (property.first == psuNameProp)
132 {
133 psuname = std::get_if<std::string>(&properties[psuNameProp]);
134 }
B. J. Wyman681b2a32021-04-20 22:31:22 +0000135 else if (property.first == presLineName)
136 {
137 preslineptr =
138 std::get_if<std::string>(&properties[presLineName]);
139 }
Brandon Wyman510acaa2020-11-05 18:32:04 -0600140 }
Patrick Williamsc1d4de52021-10-06 12:45:57 -0500141 catch (const std::exception& e)
Adriana Kobylak0c9a33d2021-09-13 18:05:09 +0000142 {}
Brandon Wyman510acaa2020-11-05 18:32:04 -0600143 }
144
145 if ((i2cbus) && (i2caddr) && (psuname) && (!psuname->empty()))
146 {
147 std::string invpath = basePSUInvPath;
148 invpath.push_back(psuname->back());
B. J. Wyman681b2a32021-04-20 22:31:22 +0000149 std::string presline = "";
Brandon Wyman510acaa2020-11-05 18:32:04 -0600150
151 log<level::DEBUG>(fmt::format("Inventory Path: {}", invpath).c_str());
152
B. J. Wyman681b2a32021-04-20 22:31:22 +0000153 if (nullptr != preslineptr)
154 {
155 presline = *preslineptr;
156 }
157
Brandon Wymanecbecbc2021-08-31 22:53:21 +0000158 auto invMatch =
159 std::find_if(psus.begin(), psus.end(), [&invpath](auto& psu) {
160 return psu->getInventoryPath() == invpath;
161 });
162 if (invMatch != psus.end())
163 {
164 // This power supply has the same inventory path as the one with
165 // information just added to D-Bus.
166 // Changes to GPIO line name unlikely, so skip checking.
167 // Changes to the I2C bus and address unlikely, as that would
168 // require corresponding device tree updates.
169 // Return out to avoid duplicate object creation.
170 return;
171 }
172
B. J. Wyman681b2a32021-04-20 22:31:22 +0000173 log<level::DEBUG>(
174 fmt::format("make PowerSupply bus: {} addr: {} presline: {}",
175 *i2cbus, *i2caddr, presline)
176 .c_str());
177 auto psu = std::make_unique<PowerSupply>(bus, invpath, *i2cbus,
178 *i2caddr, presline);
Brandon Wyman510acaa2020-11-05 18:32:04 -0600179 psus.emplace_back(std::move(psu));
Adriana Kobylak9ba38232021-11-16 20:27:45 +0000180
181 // Subscribe to power supply presence changes
182 auto presenceMatch = std::make_unique<sdbusplus::bus::match_t>(
183 bus,
184 sdbusplus::bus::match::rules::propertiesChanged(invpath,
185 INVENTORY_IFACE),
186 [this](auto& msg) { this->presenceChanged(msg); });
187 presenceMatches.emplace_back(std::move(presenceMatch));
Brandon Wyman510acaa2020-11-05 18:32:04 -0600188 }
189
190 if (psus.empty())
191 {
192 log<level::INFO>(fmt::format("No power supplies to monitor").c_str());
193 }
194}
195
Adriana Kobylake1074d82021-03-16 20:46:44 +0000196void PSUManager::populateSysProperties(const util::DbusPropertyMap& properties)
197{
198 try
199 {
Adriana Kobylak9ea66a62021-03-24 17:54:14 +0000200 auto propIt = properties.find("SupportedType");
201 if (propIt == properties.end())
202 {
203 return;
204 }
205 const std::string* type = std::get_if<std::string>(&(propIt->second));
206 if ((type == nullptr) || (*type != "PowerSupply"))
207 {
208 return;
209 }
210
Adriana Kobylak9ea66a62021-03-24 17:54:14 +0000211 propIt = properties.find("SupportedModel");
212 if (propIt == properties.end())
213 {
214 return;
215 }
Adriana Kobylakd3a70d92021-06-04 16:24:45 +0000216 const std::string* model = std::get_if<std::string>(&(propIt->second));
217 if (model == nullptr)
Adriana Kobylak9ea66a62021-03-24 17:54:14 +0000218 {
219 return;
220 }
Adriana Kobylak9ea66a62021-03-24 17:54:14 +0000221
Adriana Kobylakd3a70d92021-06-04 16:24:45 +0000222 sys_properties sys;
Adriana Kobylak9ea66a62021-03-24 17:54:14 +0000223 propIt = properties.find("RedundantCount");
Adriana Kobylake1074d82021-03-16 20:46:44 +0000224 if (propIt != properties.end())
225 {
226 const uint64_t* count = std::get_if<uint64_t>(&(propIt->second));
227 if (count != nullptr)
228 {
Adriana Kobylakd3a70d92021-06-04 16:24:45 +0000229 sys.powerSupplyCount = *count;
Adriana Kobylake1074d82021-03-16 20:46:44 +0000230 }
231 }
Adriana Kobylak9ea66a62021-03-24 17:54:14 +0000232 propIt = properties.find("InputVoltage");
233 if (propIt != properties.end())
234 {
Adriana Kobylakd3a70d92021-06-04 16:24:45 +0000235 const std::vector<uint64_t>* voltage =
236 std::get_if<std::vector<uint64_t>>(&(propIt->second));
Adriana Kobylak9ea66a62021-03-24 17:54:14 +0000237 if (voltage != nullptr)
238 {
239 sys.inputVoltage = *voltage;
240 }
241 }
242
Adriana Kobylak886574c2021-11-01 18:22:28 +0000243 // The PowerConfigFullLoad is an optional property, default it to false
244 // since that's the default value of the power-config-full-load GPIO.
245 sys.powerConfigFullLoad = false;
246 propIt = properties.find("PowerConfigFullLoad");
247 if (propIt != properties.end())
248 {
249 const bool* fullLoad = std::get_if<bool>(&(propIt->second));
250 if (fullLoad != nullptr)
251 {
252 sys.powerConfigFullLoad = *fullLoad;
253 }
254 }
255
Adriana Kobylakd3a70d92021-06-04 16:24:45 +0000256 supportedConfigs.emplace(*model, sys);
Adriana Kobylake1074d82021-03-16 20:46:44 +0000257 }
Patrick Williamsc1d4de52021-10-06 12:45:57 -0500258 catch (const std::exception& e)
Adriana Kobylak0c9a33d2021-09-13 18:05:09 +0000259 {}
Adriana Kobylake1074d82021-03-16 20:46:44 +0000260}
261
Adriana Kobylak9bab9e12021-02-24 15:32:03 -0600262void PSUManager::getSystemProperties()
263{
Adriana Kobylak9bab9e12021-02-24 15:32:03 -0600264
Adriana Kobylak9bab9e12021-02-24 15:32:03 -0600265 try
266 {
267 util::DbusSubtree subtree =
268 util::getSubTree(bus, INVENTORY_OBJ_PATH, supportedConfIntf, 0);
Adriana Kobylake1074d82021-03-16 20:46:44 +0000269 if (subtree.empty())
Adriana Kobylak9bab9e12021-02-24 15:32:03 -0600270 {
271 throw std::runtime_error("Supported Configuration Not Found");
272 }
Adriana Kobylak9bab9e12021-02-24 15:32:03 -0600273
Adriana Kobylake1074d82021-03-16 20:46:44 +0000274 for (const auto& [objPath, services] : subtree)
275 {
276 std::string service = services.begin()->first;
277 if (objPath.empty() || service.empty())
278 {
279 continue;
Adriana Kobylak9bab9e12021-02-24 15:32:03 -0600280 }
Adriana Kobylake1074d82021-03-16 20:46:44 +0000281 auto properties = util::getAllProperties(
282 bus, objPath, supportedConfIntf, service);
283 populateSysProperties(properties);
Adriana Kobylak9bab9e12021-02-24 15:32:03 -0600284 }
285 }
Patrick Williamsc1d4de52021-10-06 12:45:57 -0500286 catch (const std::exception& e)
Adriana Kobylak9bab9e12021-02-24 15:32:03 -0600287 {
288 // Interface or property not found. Let the Interfaces Added callback
289 // process the information once the interfaces are added to D-Bus.
290 }
291}
292
Brandon Wyman3e429132021-03-18 18:03:14 -0500293void PSUManager::entityManagerIfaceAdded(sdbusplus::message::message& msg)
Adriana Kobylak9bab9e12021-02-24 15:32:03 -0600294{
295 try
296 {
297 sdbusplus::message::object_path objPath;
Adriana Kobylake1074d82021-03-16 20:46:44 +0000298 std::map<std::string, std::map<std::string, util::DbusVariant>>
Adriana Kobylak9bab9e12021-02-24 15:32:03 -0600299 interfaces;
300 msg.read(objPath, interfaces);
301
302 auto itIntf = interfaces.find(supportedConfIntf);
Brandon Wyman510acaa2020-11-05 18:32:04 -0600303 if (itIntf != interfaces.cend())
Adriana Kobylak9bab9e12021-02-24 15:32:03 -0600304 {
Brandon Wyman510acaa2020-11-05 18:32:04 -0600305 populateSysProperties(itIntf->second);
Adriana Kobylak9bab9e12021-02-24 15:32:03 -0600306 }
307
Brandon Wyman510acaa2020-11-05 18:32:04 -0600308 itIntf = interfaces.find(IBMCFFPSInterface);
309 if (itIntf != interfaces.cend())
310 {
311 log<level::INFO>(
312 fmt::format("InterfacesAdded for: {}", IBMCFFPSInterface)
313 .c_str());
314 getPSUProperties(itIntf->second);
315 }
Adriana Kobylak8f16fb52021-03-31 15:50:15 +0000316
317 // Call to validate the psu configuration if the power is on and both
318 // the IBMCFFPSConnector and SupportedConfiguration interfaces have been
319 // processed
320 if (powerOn && !psus.empty() && !supportedConfigs.empty())
321 {
Adriana Kobylaka4d38fa2021-10-05 19:57:47 +0000322 validationTimer->restartOnce(validationTimeout);
Adriana Kobylak8f16fb52021-03-31 15:50:15 +0000323 }
Adriana Kobylak9bab9e12021-02-24 15:32:03 -0600324 }
Patrick Williamsc1d4de52021-10-06 12:45:57 -0500325 catch (const std::exception& e)
Adriana Kobylak9bab9e12021-02-24 15:32:03 -0600326 {
327 // Ignore, the property may be of a different type than expected.
328 }
329}
330
Brandon Wymana0f33ce2019-10-17 18:32:29 -0500331void PSUManager::powerStateChanged(sdbusplus::message::message& msg)
332{
333 int32_t state = 0;
334 std::string msgSensor;
Patrick Williamsabe49412020-05-13 17:59:47 -0500335 std::map<std::string, std::variant<int32_t>> msgData;
Brandon Wymana0f33ce2019-10-17 18:32:29 -0500336 msg.read(msgSensor, msgData);
337
338 // Check if it was the Present property that changed.
339 auto valPropMap = msgData.find("state");
340 if (valPropMap != msgData.end())
341 {
342 state = std::get<int32_t>(valPropMap->second);
343
344 // Power is on when state=1. Clear faults.
345 if (state)
346 {
347 powerOn = true;
Adriana Kobylaka4d38fa2021-10-05 19:57:47 +0000348 validationTimer->restartOnce(validationTimeout);
Brandon Wymana0f33ce2019-10-17 18:32:29 -0500349 clearFaults();
Adriana Kobylakc0a07582021-10-13 15:52:25 +0000350 setPowerConfigGPIO();
Brandon Wymana0f33ce2019-10-17 18:32:29 -0500351 }
352 else
353 {
354 powerOn = false;
Adriana Kobylak8f16fb52021-03-31 15:50:15 +0000355 runValidateConfig = true;
Brandon Wymana0f33ce2019-10-17 18:32:29 -0500356 }
357 }
358}
359
Adriana Kobylak9ba38232021-11-16 20:27:45 +0000360void PSUManager::presenceChanged(sdbusplus::message::message& msg)
361{
362 std::string msgSensor;
363 std::map<std::string, std::variant<uint32_t, bool>> msgData;
364 msg.read(msgSensor, msgData);
365
366 // Check if it was the Present property that changed.
367 auto valPropMap = msgData.find(PRESENT_PROP);
368 if (valPropMap != msgData.end())
369 {
370 if (std::get<bool>(valPropMap->second))
371 {
372 // A PSU became present, force the PSU validation to run.
373 runValidateConfig = true;
374 validationTimer->restartOnce(validationTimeout);
375 }
376 }
377}
378
Brandon Wyman8b662882021-10-08 17:31:51 +0000379void PSUManager::createError(const std::string& faultName,
380 std::map<std::string, std::string>& additionalData)
Brandon Wymanb76ab242020-09-16 18:06:06 -0500381{
382 using namespace sdbusplus::xyz::openbmc_project;
383 constexpr auto loggingObjectPath = "/xyz/openbmc_project/logging";
384 constexpr auto loggingCreateInterface =
385 "xyz.openbmc_project.Logging.Create";
386
387 try
388 {
Brandon Wyman8b662882021-10-08 17:31:51 +0000389 additionalData["_PID"] = std::to_string(getpid());
390
Brandon Wymanb76ab242020-09-16 18:06:06 -0500391 auto service =
392 util::getService(loggingObjectPath, loggingCreateInterface, bus);
393
394 if (service.empty())
395 {
396 log<level::ERR>("Unable to get logging manager service");
397 return;
398 }
399
400 auto method = bus.new_method_call(service.c_str(), loggingObjectPath,
401 loggingCreateInterface, "Create");
402
403 auto level = Logging::server::Entry::Level::Error;
404 method.append(faultName, level, additionalData);
405
406 auto reply = bus.call(method);
407 }
Patrick Williamsc1d4de52021-10-06 12:45:57 -0500408 catch (const std::exception& e)
Brandon Wymanb76ab242020-09-16 18:06:06 -0500409 {
410 log<level::ERR>(
411 fmt::format(
412 "Failed creating event log for fault {} due to error {}",
413 faultName, e.what())
414 .c_str());
415 }
416}
417
Brandon Wyman63ea78b2020-09-24 16:49:09 -0500418void PSUManager::analyze()
419{
420 for (auto& psu : psus)
421 {
422 psu->analyze();
423 }
424
Brandon Wyman3180f4d2020-12-08 17:53:46 -0600425 if (powerOn)
Brandon Wyman63ea78b2020-09-24 16:49:09 -0500426 {
Adriana Kobylakf2ba1462021-06-24 15:16:17 +0000427 std::map<std::string, std::string> additionalData;
Adriana Kobylakf2ba1462021-06-24 15:16:17 +0000428
Brandon Wyman3180f4d2020-12-08 17:53:46 -0600429 for (auto& psu : psus)
Brandon Wyman63ea78b2020-09-24 16:49:09 -0500430 {
Brandon Wymanec0b8dc2021-10-08 21:49:43 +0000431 additionalData.clear();
Brandon Wyman3180f4d2020-12-08 17:53:46 -0600432 // TODO: Fault priorities #918
433 if (!psu->isFaultLogged() && !psu->isPresent())
434 {
Brandon Wymanda369c72021-10-08 18:43:30 +0000435 std::map<std::string, std::string> requiredPSUsData;
436 auto requiredPSUsPresent = hasRequiredPSUs(requiredPSUsData);
Adriana Kobylakf2ba1462021-06-24 15:16:17 +0000437 if (!requiredPSUsPresent)
438 {
Brandon Wymanda369c72021-10-08 18:43:30 +0000439 additionalData.merge(requiredPSUsData);
Adriana Kobylakf2ba1462021-06-24 15:16:17 +0000440 // Create error for power supply missing.
441 additionalData["CALLOUT_INVENTORY_PATH"] =
442 psu->getInventoryPath();
443 additionalData["CALLOUT_PRIORITY"] = "H";
444 createError(
445 "xyz.openbmc_project.Power.PowerSupply.Error.Missing",
446 additionalData);
447 }
Brandon Wyman3180f4d2020-12-08 17:53:46 -0600448 psu->setFaultLogged();
449 }
450 else if (!psu->isFaultLogged() && psu->isFaulted())
451 {
Brandon Wyman786b6f42021-10-12 20:21:41 +0000452 // Add STATUS_WORD and STATUS_MFR last response, in padded
453 // hexadecimal format.
Brandon Wyman3180f4d2020-12-08 17:53:46 -0600454 additionalData["STATUS_WORD"] =
Brandon Wyman786b6f42021-10-12 20:21:41 +0000455 fmt::format("{:#04x}", psu->getStatusWord());
Jay Meyer10d94052020-11-30 14:41:21 -0600456 additionalData["STATUS_MFR"] =
Brandon Wyman786b6f42021-10-12 20:21:41 +0000457 fmt::format("{:#02x}", psu->getMFRFault());
Brandon Wyman3180f4d2020-12-08 17:53:46 -0600458 // If there are faults being reported, they possibly could be
459 // related to a bug in the firmware version running on the power
460 // supply. Capture that data into the error as well.
461 additionalData["FW_VERSION"] = psu->getFWVersion();
462
Brandon Wymanb85b9dd2021-10-19 21:25:17 +0000463 if (psu->hasCommFault())
464 {
Brandon Wyman85c7bf42021-10-19 22:28:48 +0000465 additionalData["STATUS_CML"] =
466 fmt::format("{:#02x}", psu->getStatusCML());
Brandon Wymanb85b9dd2021-10-19 21:25:17 +0000467 /* Attempts to communicate with the power supply have
468 * reached there limit. Create an error. */
469 additionalData["CALLOUT_DEVICE_PATH"] =
470 psu->getDevicePath();
471
472 createError(
473 "xyz.openbmc_project.Power.PowerSupply.Error.CommFault",
474 additionalData);
475
476 psu->setFaultLogged();
477 }
478 else if ((psu->hasInputFault() || psu->hasVINUVFault()))
Brandon Wyman3180f4d2020-12-08 17:53:46 -0600479 {
Brandon Wymanf07bc792021-10-12 19:00:35 +0000480 // Include STATUS_INPUT for input faults.
481 additionalData["STATUS_INPUT"] =
482 fmt::format("{:#02x}", psu->getStatusInput());
483
Brandon Wyman3180f4d2020-12-08 17:53:46 -0600484 /* The power supply location might be needed if the input
485 * fault is due to a problem with the power supply itself.
486 * Include the inventory path with a call out priority of
487 * low.
488 */
489 additionalData["CALLOUT_INVENTORY_PATH"] =
490 psu->getInventoryPath();
491 additionalData["CALLOUT_PRIORITY"] = "L";
492 createError("xyz.openbmc_project.Power.PowerSupply.Error."
493 "InputFault",
494 additionalData);
495 psu->setFaultLogged();
496 }
Brandon Wyman6710ba22021-10-27 17:39:31 +0000497 else if (psu->hasVoutOVFault())
498 {
499 // Include STATUS_VOUT for Vout faults.
500 additionalData["STATUS_VOUT"] =
501 fmt::format("{:#02x}", psu->getStatusVout());
502
503 additionalData["CALLOUT_INVENTORY_PATH"] =
504 psu->getInventoryPath();
505
506 createError(
507 "xyz.openbmc_project.Power.PowerSupply.Error.Fault",
508 additionalData);
509
510 psu->setFaultLogged();
511 }
Brandon Wymanb10b3be2021-11-09 22:12:15 +0000512 else if (psu->hasIoutOCFault())
513 {
514 // Include STATUS_IOUT for Iout faults.
515 additionalData["STATUS_IOUT"] =
516 fmt::format("{:#02x}", psu->getStatusIout());
517
518 createError(
519 "xyz.openbmc_project.Power.PowerSupply.Error.IoutOCFault",
520 additionalData);
521
522 psu->setFaultLogged();
523 }
Brandon Wyman2cf46942021-10-28 19:09:16 +0000524 else if (psu->hasVoutUVFault())
525 {
526 // Include STATUS_VOUT for Vout faults.
527 additionalData["STATUS_VOUT"] =
528 fmt::format("{:#02x}", psu->getStatusVout());
529
530 additionalData["CALLOUT_INVENTORY_PATH"] =
531 psu->getInventoryPath();
532
533 createError(
534 "xyz.openbmc_project.Power.PowerSupply.Error.Fault",
535 additionalData);
536
537 psu->setFaultLogged();
538 }
Brandon Wyman7ee4d7e2021-11-19 20:48:23 +0000539 // A fan fault should have priority over a temperature fault,
540 // since a failed fan may lead to a temperature problem.
541 else if (psu->hasFanFault())
542 {
543 // Include STATUS_TEMPERATURE and STATUS_FANS_1_2
544 additionalData["STATUS_TEMPERATURE"] =
545 fmt::format("{:#02x}", psu->getStatusTemperature());
546 additionalData["STATUS_FANS_1_2"] =
547 fmt::format("{:#02x}", psu->getStatusFans12());
548
549 additionalData["CALLOUT_INVENTORY_PATH"] =
550 psu->getInventoryPath();
551
552 createError(
553 "xyz.openbmc_project.Power.PowerSupply.Error.FanFault",
554 additionalData);
555
556 psu->setFaultLogged();
557 }
Brandon Wyman96893a42021-11-05 19:56:57 +0000558 else if (psu->hasTempFault())
559 {
560 // Include STATUS_TEMPERATURE for temperature faults.
561 additionalData["STATUS_TEMPERATURE"] =
562 fmt::format("{:#02x}", psu->getStatusTemperature());
563
564 additionalData["CALLOUT_INVENTORY_PATH"] =
565 psu->getInventoryPath();
566
567 createError(
568 "xyz.openbmc_project.Power.PowerSupply.Error.Fault",
569 additionalData);
570
571 psu->setFaultLogged();
572 }
Brandon Wyman3180f4d2020-12-08 17:53:46 -0600573 else if (psu->hasMFRFault())
574 {
575 /* This can represent a variety of faults that result in
576 * calling out the power supply for replacement: Output
577 * OverCurrent, Output Under Voltage, and potentially other
578 * faults.
579 *
580 * Also plan on putting specific fault in AdditionalData,
581 * along with register names and register values
582 * (STATUS_WORD, STATUS_MFR, etc.).*/
583
584 additionalData["CALLOUT_INVENTORY_PATH"] =
585 psu->getInventoryPath();
586
587 createError(
588 "xyz.openbmc_project.Power.PowerSupply.Error.Fault",
Brandon Wyman52e54e82020-10-08 14:44:58 -0500589 additionalData);
Brandon Wyman63ea78b2020-09-24 16:49:09 -0500590
Brandon Wyman3180f4d2020-12-08 17:53:46 -0600591 psu->setFaultLogged();
592 }
Brandon Wyman2916ea52021-11-06 03:31:18 +0000593 else if (psu->hasPgoodFault())
594 {
595 /* POWER_GOOD# is not low, or OFF is on */
596 additionalData["CALLOUT_INVENTORY_PATH"] =
597 psu->getInventoryPath();
598
599 createError(
600 "xyz.openbmc_project.Power.PowerSupply.Error.Fault",
601 additionalData);
602
603 psu->setFaultLogged();
604 }
Brandon Wyman4176d6b2020-10-07 17:41:06 -0500605 }
Brandon Wyman63ea78b2020-09-24 16:49:09 -0500606 }
607 }
608}
609
Adriana Kobylak8f16fb52021-03-31 15:50:15 +0000610void PSUManager::validateConfig()
611{
Adriana Kobylak70e7f932021-06-10 18:53:56 +0000612 if (!runValidateConfig || supportedConfigs.empty())
Adriana Kobylak8f16fb52021-03-31 15:50:15 +0000613 {
614 return;
615 }
616
Adriana Kobylak4d9aaf92021-06-30 15:27:42 +0000617 std::map<std::string, std::string> additionalData;
618 auto supported = hasRequiredPSUs(additionalData);
619 if (supported)
620 {
621 runValidateConfig = false;
622 return;
623 }
624
625 // Validation failed, create an error log.
626 // Return without setting the runValidateConfig flag to false because
627 // it may be that an additional supported configuration interface is
628 // added and we need to validate it to see if it matches this system.
629 createError("xyz.openbmc_project.Power.PowerSupply.Error.NotSupported",
630 additionalData);
631}
632
633bool PSUManager::hasRequiredPSUs(
634 std::map<std::string, std::string>& additionalData)
635{
Adriana Kobylak8f16fb52021-03-31 15:50:15 +0000636 std::string model{};
Adriana Kobylak523704d2021-09-21 15:55:41 +0000637 if (!validateModelName(model, additionalData))
Adriana Kobylak8f16fb52021-03-31 15:50:15 +0000638 {
Adriana Kobylak523704d2021-09-21 15:55:41 +0000639 return false;
Adriana Kobylak8f16fb52021-03-31 15:50:15 +0000640 }
Adriana Kobylak70e7f932021-06-10 18:53:56 +0000641
Adriana Kobylak4d9aaf92021-06-30 15:27:42 +0000642 auto presentCount =
643 std::count_if(psus.begin(), psus.end(),
644 [](const auto& psu) { return psu->isPresent(); });
645
Adriana Kobylak70e7f932021-06-10 18:53:56 +0000646 // Validate the supported configurations. A system may support more than one
Adriana Kobylak4175ffb2021-08-02 14:51:05 +0000647 // power supply model configuration. Since all configurations need to be
648 // checked, the additional data would contain only the information of the
649 // last configuration that did not match.
650 std::map<std::string, std::string> tmpAdditionalData;
Adriana Kobylak70e7f932021-06-10 18:53:56 +0000651 for (const auto& config : supportedConfigs)
652 {
Adriana Kobylak4d9aaf92021-06-30 15:27:42 +0000653 if (config.first != model)
Adriana Kobylak70e7f932021-06-10 18:53:56 +0000654 {
655 continue;
656 }
657 if (presentCount != config.second.powerSupplyCount)
658 {
Adriana Kobylak4175ffb2021-08-02 14:51:05 +0000659 tmpAdditionalData.clear();
660 tmpAdditionalData["EXPECTED_COUNT"] =
Adriana Kobylak70e7f932021-06-10 18:53:56 +0000661 std::to_string(config.second.powerSupplyCount);
Adriana Kobylak4175ffb2021-08-02 14:51:05 +0000662 tmpAdditionalData["ACTUAL_COUNT"] = std::to_string(presentCount);
Adriana Kobylak70e7f932021-06-10 18:53:56 +0000663 continue;
664 }
Adriana Kobylak4175ffb2021-08-02 14:51:05 +0000665
666 bool voltageValidated = true;
667 for (const auto& psu : psus)
668 {
669 if (!psu->isPresent())
670 {
671 // Only present PSUs report a valid input voltage
672 continue;
673 }
674
675 double actualInputVoltage;
676 int inputVoltage;
677 psu->getInputVoltage(actualInputVoltage, inputVoltage);
678
679 if (std::find(config.second.inputVoltage.begin(),
680 config.second.inputVoltage.end(),
681 inputVoltage) == config.second.inputVoltage.end())
682 {
683 tmpAdditionalData.clear();
684 tmpAdditionalData["ACTUAL_VOLTAGE"] =
685 std::to_string(actualInputVoltage);
686 for (const auto& voltage : config.second.inputVoltage)
687 {
688 tmpAdditionalData["EXPECTED_VOLTAGE"] +=
689 std::to_string(voltage) + " ";
690 }
691 tmpAdditionalData["CALLOUT_INVENTORY_PATH"] =
692 psu->getInventoryPath();
693
694 voltageValidated = false;
695 break;
696 }
697 }
698 if (!voltageValidated)
699 {
700 continue;
701 }
702
Adriana Kobylak4d9aaf92021-06-30 15:27:42 +0000703 return true;
Adriana Kobylak70e7f932021-06-10 18:53:56 +0000704 }
Adriana Kobylak70e7f932021-06-10 18:53:56 +0000705
Adriana Kobylak4175ffb2021-08-02 14:51:05 +0000706 additionalData.insert(tmpAdditionalData.begin(), tmpAdditionalData.end());
Adriana Kobylak4d9aaf92021-06-30 15:27:42 +0000707 return false;
Adriana Kobylak8f16fb52021-03-31 15:50:15 +0000708}
709
Adriana Kobylak523704d2021-09-21 15:55:41 +0000710bool PSUManager::validateModelName(
711 std::string& model, std::map<std::string, std::string>& additionalData)
712{
713 // Check that all PSUs have the same model name. Initialize the model
714 // variable with the first PSU name found, then use it as a base to compare
715 // against the rest of the PSUs.
716 model.clear();
717 for (const auto& psu : psus)
718 {
719 auto psuModel = psu->getModelName();
720 if (psuModel.empty())
721 {
722 continue;
723 }
724 if (model.empty())
725 {
726 model = psuModel;
727 continue;
728 }
729 if (psuModel != model)
730 {
731 additionalData["EXPECTED_MODEL"] = model;
732 additionalData["ACTUAL_MODEL"] = psuModel;
733 additionalData["CALLOUT_INVENTORY_PATH"] = psu->getInventoryPath();
734 model.clear();
735 return false;
736 }
737 }
738 return true;
739}
740
Adriana Kobylakc0a07582021-10-13 15:52:25 +0000741void PSUManager::setPowerConfigGPIO()
742{
743 if (!powerConfigGPIO)
744 {
745 return;
746 }
747
748 std::string model{};
749 std::map<std::string, std::string> additionalData;
750 if (!validateModelName(model, additionalData))
751 {
752 return;
753 }
754
755 auto config = supportedConfigs.find(model);
756 if (config != supportedConfigs.end())
757 {
758 // The power-config-full-load is an open drain GPIO. Set it to low (0)
759 // if the supported configuration indicates that this system model
760 // expects the maximum number of power supplies (full load set to true).
761 // Else, set it to high (1), this is the default.
762 auto powerConfigValue =
763 (config->second.powerConfigFullLoad == true ? 0 : 1);
764 auto flags = gpiod::line_request::FLAG_OPEN_DRAIN;
765 powerConfigGPIO->write(powerConfigValue, flags);
766 }
767}
768
Brandon Wyman63ea78b2020-09-24 16:49:09 -0500769} // namespace phosphor::power::manager