blob: e166c8b1017713dc3444c6f9ca96f8eb67e926b9 [file] [log] [blame]
Brandon Wyman18a24d92022-04-19 22:48:34 +00001#include "config.h"
2
Brandon Wymana0f33ce2019-10-17 18:32:29 -05003#include "psu_manager.hpp"
4
5#include "utility.hpp"
6
Brandon Wymanb76ab242020-09-16 18:06:06 -05007#include <fmt/format.h>
8#include <sys/types.h>
9#include <unistd.h>
10
Shawn McCarney9252b7e2022-06-10 12:47:38 -050011#include <algorithm>
Brandon Wymanecbecbc2021-08-31 22:53:21 +000012#include <regex>
Shawn McCarney9252b7e2022-06-10 12:47:38 -050013#include <set>
Brandon Wymanecbecbc2021-08-31 22:53:21 +000014
Brandon Wymanaed1f752019-11-25 18:10:52 -060015using namespace phosphor::logging;
16
Brandon Wyman63ea78b2020-09-24 16:49:09 -050017namespace phosphor::power::manager
Brandon Wymana0f33ce2019-10-17 18:32:29 -050018{
Adriana Kobylakc9b05732022-03-19 15:15:10 +000019constexpr auto managerBusName = "xyz.openbmc_project.Power.PSUMonitor";
20constexpr auto objectManagerObjPath =
21 "/xyz/openbmc_project/power/power_supplies";
22constexpr auto powerSystemsInputsObjPath =
23 "/xyz/openbmc_project/power/power_supplies/chassis0/psus";
Brandon Wymana0f33ce2019-10-17 18:32:29 -050024
Brandon Wyman510acaa2020-11-05 18:32:04 -060025constexpr auto IBMCFFPSInterface =
26 "xyz.openbmc_project.Configuration.IBMCFFPSConnector";
27constexpr auto i2cBusProp = "I2CBus";
28constexpr auto i2cAddressProp = "I2CAddress";
29constexpr auto psuNameProp = "Name";
B. J. Wyman681b2a32021-04-20 22:31:22 +000030constexpr auto presLineName = "NamedPresenceGpio";
Brandon Wyman510acaa2020-11-05 18:32:04 -060031
Adriana Kobylak9bab9e12021-02-24 15:32:03 -060032constexpr auto supportedConfIntf =
33 "xyz.openbmc_project.Configuration.SupportedConfiguration";
Adriana Kobylak9bab9e12021-02-24 15:32:03 -060034
Brandon Wymanc9e840e2022-05-10 20:48:41 +000035constexpr auto INPUT_HISTORY_SYNC_DELAY = 5;
Brandon Wyman18a24d92022-04-19 22:48:34 +000036
Brandon Wyman510acaa2020-11-05 18:32:04 -060037PSUManager::PSUManager(sdbusplus::bus::bus& bus, const sdeventplus::Event& e) :
Adriana Kobylakc9b05732022-03-19 15:15:10 +000038 bus(bus), powerSystemInputs(bus, powerSystemsInputsObjPath),
Brandon Wymanc3324422022-03-24 20:30:57 +000039 objectManager(bus, objectManagerObjPath),
40 historyManager(bus, "/org/open_power/sensors")
Brandon Wyman510acaa2020-11-05 18:32:04 -060041{
Brandon Wyman510acaa2020-11-05 18:32:04 -060042 // Subscribe to InterfacesAdded before doing a property read, otherwise
43 // the interface could be created after the read attempt but before the
44 // match is created.
45 entityManagerIfacesAddedMatch = std::make_unique<sdbusplus::bus::match_t>(
46 bus,
47 sdbusplus::bus::match::rules::interfacesAdded() +
48 sdbusplus::bus::match::rules::sender(
49 "xyz.openbmc_project.EntityManager"),
50 std::bind(&PSUManager::entityManagerIfaceAdded, this,
51 std::placeholders::_1));
52 getPSUConfiguration();
53 getSystemProperties();
54
Adriana Kobylakc9b05732022-03-19 15:15:10 +000055 // Request the bus name before the analyze() function, which is the one that
56 // determines the brownout condition and sets the status d-bus property.
57 bus.request_name(managerBusName);
58
Brandon Wyman510acaa2020-11-05 18:32:04 -060059 using namespace sdeventplus;
60 auto interval = std::chrono::milliseconds(1000);
61 timer = std::make_unique<utility::Timer<ClockId::Monotonic>>(
62 e, std::bind(&PSUManager::analyze, this), interval);
63
Adriana Kobylaka4d38fa2021-10-05 19:57:47 +000064 validationTimer = std::make_unique<utility::Timer<ClockId::Monotonic>>(
65 e, std::bind(&PSUManager::validateConfig, this));
66
Adriana Kobylakc0a07582021-10-13 15:52:25 +000067 try
68 {
69 powerConfigGPIO = createGPIO("power-config-full-load");
70 }
71 catch (const std::exception& e)
72 {
73 // Ignore error, GPIO may not be implemented in this system.
74 powerConfigGPIO = nullptr;
75 }
76
Brandon Wyman510acaa2020-11-05 18:32:04 -060077 // Subscribe to power state changes
78 powerService = util::getService(POWER_OBJ_PATH, POWER_IFACE, bus);
79 powerOnMatch = std::make_unique<sdbusplus::bus::match_t>(
80 bus,
81 sdbusplus::bus::match::rules::propertiesChanged(POWER_OBJ_PATH,
82 POWER_IFACE),
83 [this](auto& msg) { this->powerStateChanged(msg); });
84
85 initialize();
86}
87
Jim Wrightaca86d02022-06-10 12:01:39 -050088void PSUManager::initialize()
89{
90 try
91 {
92 // pgood is the latest read of the chassis pgood
93 int pgood = 0;
94 util::getProperty<int>(POWER_IFACE, "pgood", POWER_OBJ_PATH,
95 powerService, bus, pgood);
96
97 // state is the latest requested power on / off transition
98 auto method = bus.new_method_call(POWER_IFACE, POWER_OBJ_PATH,
99 POWER_IFACE, "getPowerState");
100 auto reply = bus.call(method);
101 int state = 0;
102 reply.read(state);
103
104 if (state)
105 {
106 // Monitor PSUs anytime state is on
107 powerOn = true;
108 // In the power fault window if pgood is off
109 powerFaultOccurring = !pgood;
110 validationTimer->restartOnce(validationTimeout);
111 }
112 else
113 {
114 // Power is off
115 powerOn = false;
116 powerFaultOccurring = false;
117 runValidateConfig = true;
118 }
119 }
120 catch (const std::exception& e)
121 {
122 log<level::INFO>(
123 fmt::format(
124 "Failed to get power state, assuming it is off, error {}",
125 e.what())
126 .c_str());
127 powerOn = false;
128 powerFaultOccurring = false;
129 runValidateConfig = true;
130 }
131
132 onOffConfig(phosphor::pmbus::ON_OFF_CONFIG_CONTROL_PIN_ONLY);
133 clearFaults();
134 updateMissingPSUs();
135 updateInventory();
136 setPowerConfigGPIO();
137
138 log<level::INFO>(
139 fmt::format("initialize: power on: {}, power fault occurring: {}",
140 powerOn, powerFaultOccurring)
141 .c_str());
142}
143
Brandon Wyman510acaa2020-11-05 18:32:04 -0600144void PSUManager::getPSUConfiguration()
145{
146 using namespace phosphor::power::util;
147 auto depth = 0;
148 auto objects = getSubTree(bus, "/", IBMCFFPSInterface, depth);
149
150 psus.clear();
151
152 // I should get a map of objects back.
153 // Each object will have a path, a service, and an interface.
154 // The interface should match the one passed into this function.
155 for (const auto& [path, services] : objects)
156 {
157 auto service = services.begin()->first;
158
159 if (path.empty() || service.empty())
160 {
161 continue;
162 }
163
164 // For each object in the array of objects, I want to get properties
165 // from the service, path, and interface.
166 auto properties =
167 getAllProperties(bus, path, IBMCFFPSInterface, service);
168
169 getPSUProperties(properties);
170 }
171
172 if (psus.empty())
173 {
174 // Interface or properties not found. Let the Interfaces Added callback
175 // process the information once the interfaces are added to D-Bus.
176 log<level::INFO>(fmt::format("No power supplies to monitor").c_str());
177 }
178}
179
180void PSUManager::getPSUProperties(util::DbusPropertyMap& properties)
181{
182 // From passed in properties, I want to get: I2CBus, I2CAddress,
183 // and Name. Create a power supply object, using Name to build the inventory
184 // path.
185 const auto basePSUInvPath =
186 "/xyz/openbmc_project/inventory/system/chassis/motherboard/powersupply";
187 uint64_t* i2cbus = nullptr;
188 uint64_t* i2caddr = nullptr;
189 std::string* psuname = nullptr;
B. J. Wyman681b2a32021-04-20 22:31:22 +0000190 std::string* preslineptr = nullptr;
Brandon Wyman510acaa2020-11-05 18:32:04 -0600191
192 for (const auto& property : properties)
193 {
194 try
195 {
196 if (property.first == i2cBusProp)
197 {
198 i2cbus = std::get_if<uint64_t>(&properties[i2cBusProp]);
199 }
200 else if (property.first == i2cAddressProp)
201 {
202 i2caddr = std::get_if<uint64_t>(&properties[i2cAddressProp]);
203 }
204 else if (property.first == psuNameProp)
205 {
206 psuname = std::get_if<std::string>(&properties[psuNameProp]);
207 }
B. J. Wyman681b2a32021-04-20 22:31:22 +0000208 else if (property.first == presLineName)
209 {
210 preslineptr =
211 std::get_if<std::string>(&properties[presLineName]);
212 }
Brandon Wyman510acaa2020-11-05 18:32:04 -0600213 }
Patrick Williamsc1d4de52021-10-06 12:45:57 -0500214 catch (const std::exception& e)
Adriana Kobylak0c9a33d2021-09-13 18:05:09 +0000215 {}
Brandon Wyman510acaa2020-11-05 18:32:04 -0600216 }
217
218 if ((i2cbus) && (i2caddr) && (psuname) && (!psuname->empty()))
219 {
220 std::string invpath = basePSUInvPath;
221 invpath.push_back(psuname->back());
B. J. Wyman681b2a32021-04-20 22:31:22 +0000222 std::string presline = "";
Brandon Wyman510acaa2020-11-05 18:32:04 -0600223
224 log<level::DEBUG>(fmt::format("Inventory Path: {}", invpath).c_str());
225
B. J. Wyman681b2a32021-04-20 22:31:22 +0000226 if (nullptr != preslineptr)
227 {
228 presline = *preslineptr;
229 }
230
Brandon Wymanecbecbc2021-08-31 22:53:21 +0000231 auto invMatch =
232 std::find_if(psus.begin(), psus.end(), [&invpath](auto& psu) {
233 return psu->getInventoryPath() == invpath;
234 });
235 if (invMatch != psus.end())
236 {
237 // This power supply has the same inventory path as the one with
238 // information just added to D-Bus.
239 // Changes to GPIO line name unlikely, so skip checking.
240 // Changes to the I2C bus and address unlikely, as that would
241 // require corresponding device tree updates.
242 // Return out to avoid duplicate object creation.
243 return;
244 }
245
Brandon Wymanc3324422022-03-24 20:30:57 +0000246 constexpr auto driver = "ibm-cffps";
B. J. Wyman681b2a32021-04-20 22:31:22 +0000247 log<level::DEBUG>(
Brandon Wymanc3324422022-03-24 20:30:57 +0000248 fmt::format(
249 "make PowerSupply bus: {} addr: {} driver: {} presline: {}",
250 *i2cbus, *i2caddr, driver, presline)
B. J. Wyman681b2a32021-04-20 22:31:22 +0000251 .c_str());
252 auto psu = std::make_unique<PowerSupply>(bus, invpath, *i2cbus,
Brandon Wymanc3324422022-03-24 20:30:57 +0000253 *i2caddr, driver, presline);
Brandon Wyman510acaa2020-11-05 18:32:04 -0600254 psus.emplace_back(std::move(psu));
Adriana Kobylak9ba38232021-11-16 20:27:45 +0000255
256 // Subscribe to power supply presence changes
257 auto presenceMatch = std::make_unique<sdbusplus::bus::match_t>(
258 bus,
259 sdbusplus::bus::match::rules::propertiesChanged(invpath,
260 INVENTORY_IFACE),
261 [this](auto& msg) { this->presenceChanged(msg); });
262 presenceMatches.emplace_back(std::move(presenceMatch));
Brandon Wyman510acaa2020-11-05 18:32:04 -0600263 }
264
265 if (psus.empty())
266 {
267 log<level::INFO>(fmt::format("No power supplies to monitor").c_str());
268 }
269}
270
Adriana Kobylake1074d82021-03-16 20:46:44 +0000271void PSUManager::populateSysProperties(const util::DbusPropertyMap& properties)
272{
273 try
274 {
Adriana Kobylak9ea66a62021-03-24 17:54:14 +0000275 auto propIt = properties.find("SupportedType");
276 if (propIt == properties.end())
277 {
278 return;
279 }
280 const std::string* type = std::get_if<std::string>(&(propIt->second));
281 if ((type == nullptr) || (*type != "PowerSupply"))
282 {
283 return;
284 }
285
Adriana Kobylak9ea66a62021-03-24 17:54:14 +0000286 propIt = properties.find("SupportedModel");
287 if (propIt == properties.end())
288 {
289 return;
290 }
Adriana Kobylakd3a70d92021-06-04 16:24:45 +0000291 const std::string* model = std::get_if<std::string>(&(propIt->second));
292 if (model == nullptr)
Adriana Kobylak9ea66a62021-03-24 17:54:14 +0000293 {
294 return;
295 }
Adriana Kobylak9ea66a62021-03-24 17:54:14 +0000296
Adriana Kobylakd3a70d92021-06-04 16:24:45 +0000297 sys_properties sys;
Adriana Kobylak9ea66a62021-03-24 17:54:14 +0000298 propIt = properties.find("RedundantCount");
Adriana Kobylake1074d82021-03-16 20:46:44 +0000299 if (propIt != properties.end())
300 {
301 const uint64_t* count = std::get_if<uint64_t>(&(propIt->second));
302 if (count != nullptr)
303 {
Adriana Kobylakd3a70d92021-06-04 16:24:45 +0000304 sys.powerSupplyCount = *count;
Adriana Kobylake1074d82021-03-16 20:46:44 +0000305 }
306 }
Adriana Kobylak9ea66a62021-03-24 17:54:14 +0000307 propIt = properties.find("InputVoltage");
308 if (propIt != properties.end())
309 {
Adriana Kobylakd3a70d92021-06-04 16:24:45 +0000310 const std::vector<uint64_t>* voltage =
311 std::get_if<std::vector<uint64_t>>(&(propIt->second));
Adriana Kobylak9ea66a62021-03-24 17:54:14 +0000312 if (voltage != nullptr)
313 {
314 sys.inputVoltage = *voltage;
315 }
316 }
317
Adriana Kobylak886574c2021-11-01 18:22:28 +0000318 // The PowerConfigFullLoad is an optional property, default it to false
319 // since that's the default value of the power-config-full-load GPIO.
320 sys.powerConfigFullLoad = false;
321 propIt = properties.find("PowerConfigFullLoad");
322 if (propIt != properties.end())
323 {
324 const bool* fullLoad = std::get_if<bool>(&(propIt->second));
325 if (fullLoad != nullptr)
326 {
327 sys.powerConfigFullLoad = *fullLoad;
328 }
329 }
330
Adriana Kobylakd3a70d92021-06-04 16:24:45 +0000331 supportedConfigs.emplace(*model, sys);
Adriana Kobylake1074d82021-03-16 20:46:44 +0000332 }
Patrick Williamsc1d4de52021-10-06 12:45:57 -0500333 catch (const std::exception& e)
Adriana Kobylak0c9a33d2021-09-13 18:05:09 +0000334 {}
Adriana Kobylake1074d82021-03-16 20:46:44 +0000335}
336
Adriana Kobylak9bab9e12021-02-24 15:32:03 -0600337void PSUManager::getSystemProperties()
338{
Adriana Kobylak9bab9e12021-02-24 15:32:03 -0600339
Adriana Kobylak9bab9e12021-02-24 15:32:03 -0600340 try
341 {
342 util::DbusSubtree subtree =
343 util::getSubTree(bus, INVENTORY_OBJ_PATH, supportedConfIntf, 0);
Adriana Kobylake1074d82021-03-16 20:46:44 +0000344 if (subtree.empty())
Adriana Kobylak9bab9e12021-02-24 15:32:03 -0600345 {
346 throw std::runtime_error("Supported Configuration Not Found");
347 }
Adriana Kobylak9bab9e12021-02-24 15:32:03 -0600348
Adriana Kobylake1074d82021-03-16 20:46:44 +0000349 for (const auto& [objPath, services] : subtree)
350 {
351 std::string service = services.begin()->first;
352 if (objPath.empty() || service.empty())
353 {
354 continue;
Adriana Kobylak9bab9e12021-02-24 15:32:03 -0600355 }
Adriana Kobylake1074d82021-03-16 20:46:44 +0000356 auto properties = util::getAllProperties(
357 bus, objPath, supportedConfIntf, service);
358 populateSysProperties(properties);
Adriana Kobylak9bab9e12021-02-24 15:32:03 -0600359 }
360 }
Patrick Williamsc1d4de52021-10-06 12:45:57 -0500361 catch (const std::exception& e)
Adriana Kobylak9bab9e12021-02-24 15:32:03 -0600362 {
363 // Interface or property not found. Let the Interfaces Added callback
364 // process the information once the interfaces are added to D-Bus.
365 }
366}
367
Brandon Wyman3e429132021-03-18 18:03:14 -0500368void PSUManager::entityManagerIfaceAdded(sdbusplus::message::message& msg)
Adriana Kobylak9bab9e12021-02-24 15:32:03 -0600369{
370 try
371 {
372 sdbusplus::message::object_path objPath;
Adriana Kobylake1074d82021-03-16 20:46:44 +0000373 std::map<std::string, std::map<std::string, util::DbusVariant>>
Adriana Kobylak9bab9e12021-02-24 15:32:03 -0600374 interfaces;
375 msg.read(objPath, interfaces);
376
377 auto itIntf = interfaces.find(supportedConfIntf);
Brandon Wyman510acaa2020-11-05 18:32:04 -0600378 if (itIntf != interfaces.cend())
Adriana Kobylak9bab9e12021-02-24 15:32:03 -0600379 {
Brandon Wyman510acaa2020-11-05 18:32:04 -0600380 populateSysProperties(itIntf->second);
Adriana Kobylak9bab9e12021-02-24 15:32:03 -0600381 }
382
Brandon Wyman510acaa2020-11-05 18:32:04 -0600383 itIntf = interfaces.find(IBMCFFPSInterface);
384 if (itIntf != interfaces.cend())
385 {
386 log<level::INFO>(
387 fmt::format("InterfacesAdded for: {}", IBMCFFPSInterface)
388 .c_str());
389 getPSUProperties(itIntf->second);
390 }
Adriana Kobylak8f16fb52021-03-31 15:50:15 +0000391
Brandon Wyman64e97752022-06-03 23:50:13 +0000392 updateMissingPSUs();
393
Adriana Kobylak8f16fb52021-03-31 15:50:15 +0000394 // Call to validate the psu configuration if the power is on and both
395 // the IBMCFFPSConnector and SupportedConfiguration interfaces have been
396 // processed
397 if (powerOn && !psus.empty() && !supportedConfigs.empty())
398 {
Adriana Kobylaka4d38fa2021-10-05 19:57:47 +0000399 validationTimer->restartOnce(validationTimeout);
Adriana Kobylak8f16fb52021-03-31 15:50:15 +0000400 }
Adriana Kobylak9bab9e12021-02-24 15:32:03 -0600401 }
Patrick Williamsc1d4de52021-10-06 12:45:57 -0500402 catch (const std::exception& e)
Adriana Kobylak9bab9e12021-02-24 15:32:03 -0600403 {
404 // Ignore, the property may be of a different type than expected.
405 }
406}
407
Brandon Wymana0f33ce2019-10-17 18:32:29 -0500408void PSUManager::powerStateChanged(sdbusplus::message::message& msg)
409{
Brandon Wymana0f33ce2019-10-17 18:32:29 -0500410 std::string msgSensor;
Jim Wrightaca86d02022-06-10 12:01:39 -0500411 std::map<std::string, std::variant<int>> msgData;
Brandon Wymana0f33ce2019-10-17 18:32:29 -0500412 msg.read(msgSensor, msgData);
413
Jim Wrightaca86d02022-06-10 12:01:39 -0500414 // Check if it was the state property that changed.
Brandon Wymana0f33ce2019-10-17 18:32:29 -0500415 auto valPropMap = msgData.find("state");
416 if (valPropMap != msgData.end())
417 {
Jim Wrightaca86d02022-06-10 12:01:39 -0500418 int state = std::get<int>(valPropMap->second);
Brandon Wymana0f33ce2019-10-17 18:32:29 -0500419 if (state)
420 {
Jim Wrightaca86d02022-06-10 12:01:39 -0500421 // Power on requested
Brandon Wymana0f33ce2019-10-17 18:32:29 -0500422 powerOn = true;
Jim Wrightaca86d02022-06-10 12:01:39 -0500423 powerFaultOccurring = false;
Adriana Kobylaka4d38fa2021-10-05 19:57:47 +0000424 validationTimer->restartOnce(validationTimeout);
Brandon Wymana0f33ce2019-10-17 18:32:29 -0500425 clearFaults();
Brandon Wyman49b8ec42022-04-20 21:18:33 +0000426 syncHistory();
Adriana Kobylakc0a07582021-10-13 15:52:25 +0000427 setPowerConfigGPIO();
Brandon Wymana0f33ce2019-10-17 18:32:29 -0500428 }
429 else
430 {
Jim Wrightaca86d02022-06-10 12:01:39 -0500431 // Power off requested
Brandon Wymana0f33ce2019-10-17 18:32:29 -0500432 powerOn = false;
Jim Wrightaca86d02022-06-10 12:01:39 -0500433 powerFaultOccurring = false;
Adriana Kobylak8f16fb52021-03-31 15:50:15 +0000434 runValidateConfig = true;
Brandon Wymana0f33ce2019-10-17 18:32:29 -0500435 }
436 }
Jim Wrightaca86d02022-06-10 12:01:39 -0500437
438 // Check if it was the pgood property that changed.
439 valPropMap = msgData.find("pgood");
440 if (valPropMap != msgData.end())
441 {
442 int pgood = std::get<int>(valPropMap->second);
443 if (!pgood)
444 {
445 // Chassis power good has turned off
446 if (powerOn)
447 {
448 // pgood is off but state is on, in power fault window
449 powerFaultOccurring = true;
450 }
451 }
452 }
453 log<level::INFO>(
454 fmt::format(
455 "powerStateChanged: power on: {}, power fault occurring: {}",
456 powerOn, powerFaultOccurring)
457 .c_str());
Brandon Wymana0f33ce2019-10-17 18:32:29 -0500458}
459
Adriana Kobylak9ba38232021-11-16 20:27:45 +0000460void PSUManager::presenceChanged(sdbusplus::message::message& msg)
461{
462 std::string msgSensor;
463 std::map<std::string, std::variant<uint32_t, bool>> msgData;
464 msg.read(msgSensor, msgData);
465
466 // Check if it was the Present property that changed.
467 auto valPropMap = msgData.find(PRESENT_PROP);
468 if (valPropMap != msgData.end())
469 {
470 if (std::get<bool>(valPropMap->second))
471 {
472 // A PSU became present, force the PSU validation to run.
473 runValidateConfig = true;
474 validationTimer->restartOnce(validationTimeout);
475 }
476 }
477}
478
Brandon Wyman10fc6e82022-02-08 20:51:22 +0000479void PSUManager::setPowerSupplyError(const std::string& psuErrorString)
480{
481 using namespace sdbusplus::xyz::openbmc_project;
482 constexpr auto service = "org.openbmc.control.Power";
483 constexpr auto objPath = "/org/openbmc/control/power0";
484 constexpr auto interface = "org.openbmc.control.Power";
485 constexpr auto method = "setPowerSupplyError";
486
487 try
488 {
489 // Call D-Bus method to inform pseq of PSU error
490 auto methodMsg =
491 bus.new_method_call(service, objPath, interface, method);
492 methodMsg.append(psuErrorString);
493 auto callReply = bus.call(methodMsg);
494 }
495 catch (const std::exception& e)
496 {
497 log<level::INFO>(
498 fmt::format("Failed calling setPowerSupplyError due to error {}",
499 e.what())
500 .c_str());
501 }
502}
503
Brandon Wyman8b662882021-10-08 17:31:51 +0000504void PSUManager::createError(const std::string& faultName,
505 std::map<std::string, std::string>& additionalData)
Brandon Wymanb76ab242020-09-16 18:06:06 -0500506{
507 using namespace sdbusplus::xyz::openbmc_project;
508 constexpr auto loggingObjectPath = "/xyz/openbmc_project/logging";
509 constexpr auto loggingCreateInterface =
510 "xyz.openbmc_project.Logging.Create";
511
512 try
513 {
Brandon Wyman8b662882021-10-08 17:31:51 +0000514 additionalData["_PID"] = std::to_string(getpid());
515
Brandon Wymanb76ab242020-09-16 18:06:06 -0500516 auto service =
517 util::getService(loggingObjectPath, loggingCreateInterface, bus);
518
519 if (service.empty())
520 {
521 log<level::ERR>("Unable to get logging manager service");
522 return;
523 }
524
525 auto method = bus.new_method_call(service.c_str(), loggingObjectPath,
526 loggingCreateInterface, "Create");
527
528 auto level = Logging::server::Entry::Level::Error;
529 method.append(faultName, level, additionalData);
530
531 auto reply = bus.call(method);
Brandon Wyman10fc6e82022-02-08 20:51:22 +0000532 setPowerSupplyError(faultName);
Brandon Wymanb76ab242020-09-16 18:06:06 -0500533 }
Patrick Williamsc1d4de52021-10-06 12:45:57 -0500534 catch (const std::exception& e)
Brandon Wymanb76ab242020-09-16 18:06:06 -0500535 {
536 log<level::ERR>(
537 fmt::format(
538 "Failed creating event log for fault {} due to error {}",
539 faultName, e.what())
540 .c_str());
541 }
542}
543
Brandon Wyman18a24d92022-04-19 22:48:34 +0000544void PSUManager::syncHistory()
545{
546 log<level::INFO>("Synchronize INPUT_HISTORY");
547
548 if (!syncHistoryGPIO)
549 {
550 syncHistoryGPIO = createGPIO(INPUT_HISTORY_SYNC_GPIO);
551 }
552 if (syncHistoryGPIO)
553 {
554 const std::chrono::milliseconds delay{INPUT_HISTORY_SYNC_DELAY};
555 syncHistoryGPIO->toggleLowHigh(delay);
556 for (auto& psu : psus)
557 {
558 psu->clearSyncHistoryRequired();
559 }
560 }
561
562 log<level::INFO>("Synchronize INPUT_HISTORY completed");
563}
564
Brandon Wyman63ea78b2020-09-24 16:49:09 -0500565void PSUManager::analyze()
566{
Brandon Wyman18a24d92022-04-19 22:48:34 +0000567 auto syncHistoryRequired =
568 std::any_of(psus.begin(), psus.end(), [](const auto& psu) {
569 return psu->isSyncHistoryRequired();
570 });
571 if (syncHistoryRequired)
572 {
573 syncHistory();
574 }
575
Brandon Wyman63ea78b2020-09-24 16:49:09 -0500576 for (auto& psu : psus)
577 {
578 psu->analyze();
579 }
580
Adriana Kobylake5b1e082022-03-02 15:37:32 +0000581 std::map<std::string, std::string> additionalData;
582
583 auto notPresentCount = decltype(psus.size())(
584 std::count_if(psus.begin(), psus.end(),
585 [](const auto& psu) { return !psu->isPresent(); }));
586
587 auto hasVINUVFaultCount = decltype(psus.size())(
588 std::count_if(psus.begin(), psus.end(),
589 [](const auto& psu) { return psu->hasVINUVFault(); }));
590
591 // The PSU D-Bus objects may not be available yet, so ignore if all
592 // PSUs are not present or the number of PSUs is still 0.
593 if ((psus.size() == (notPresentCount + hasVINUVFaultCount)) &&
594 (psus.size() != notPresentCount) && (psus.size() != 0))
595 {
596 // Brownout: All PSUs report an AC failure: At least one PSU reports
597 // AC loss VIN fault and the rest either report AC loss VIN fault as
598 // well or are not present.
599 additionalData["NOT_PRESENT_COUNT"] = std::to_string(notPresentCount);
600 additionalData["VIN_FAULT_COUNT"] = std::to_string(hasVINUVFaultCount);
601 setBrownout(additionalData);
602 }
603 else
604 {
605 // Brownout condition is not present or has been cleared
606 clearBrownout();
607 }
608
Brandon Wyman3180f4d2020-12-08 17:53:46 -0600609 if (powerOn)
Brandon Wyman63ea78b2020-09-24 16:49:09 -0500610 {
Brandon Wyman3180f4d2020-12-08 17:53:46 -0600611 for (auto& psu : psus)
Brandon Wyman63ea78b2020-09-24 16:49:09 -0500612 {
Brandon Wymanec0b8dc2021-10-08 21:49:43 +0000613 additionalData.clear();
Brandon Wyman39ea02b2021-11-23 23:22:23 +0000614
Brandon Wyman3180f4d2020-12-08 17:53:46 -0600615 if (!psu->isFaultLogged() && !psu->isPresent())
616 {
Brandon Wymanda369c72021-10-08 18:43:30 +0000617 std::map<std::string, std::string> requiredPSUsData;
618 auto requiredPSUsPresent = hasRequiredPSUs(requiredPSUsData);
Shawn McCarney9252b7e2022-06-10 12:47:38 -0500619 if (!requiredPSUsPresent && isRequiredPSU(*psu))
Adriana Kobylakf2ba1462021-06-24 15:16:17 +0000620 {
Brandon Wymanda369c72021-10-08 18:43:30 +0000621 additionalData.merge(requiredPSUsData);
Adriana Kobylakf2ba1462021-06-24 15:16:17 +0000622 // Create error for power supply missing.
623 additionalData["CALLOUT_INVENTORY_PATH"] =
624 psu->getInventoryPath();
625 additionalData["CALLOUT_PRIORITY"] = "H";
626 createError(
627 "xyz.openbmc_project.Power.PowerSupply.Error.Missing",
628 additionalData);
629 }
Brandon Wyman3180f4d2020-12-08 17:53:46 -0600630 psu->setFaultLogged();
631 }
632 else if (!psu->isFaultLogged() && psu->isFaulted())
633 {
Brandon Wyman786b6f42021-10-12 20:21:41 +0000634 // Add STATUS_WORD and STATUS_MFR last response, in padded
635 // hexadecimal format.
Brandon Wyman3180f4d2020-12-08 17:53:46 -0600636 additionalData["STATUS_WORD"] =
Brandon Wyman786b6f42021-10-12 20:21:41 +0000637 fmt::format("{:#04x}", psu->getStatusWord());
Jay Meyer10d94052020-11-30 14:41:21 -0600638 additionalData["STATUS_MFR"] =
Brandon Wyman786b6f42021-10-12 20:21:41 +0000639 fmt::format("{:#02x}", psu->getMFRFault());
Brandon Wyman3180f4d2020-12-08 17:53:46 -0600640 // If there are faults being reported, they possibly could be
641 // related to a bug in the firmware version running on the power
642 // supply. Capture that data into the error as well.
643 additionalData["FW_VERSION"] = psu->getFWVersion();
644
Brandon Wymanb85b9dd2021-10-19 21:25:17 +0000645 if (psu->hasCommFault())
646 {
Brandon Wyman85c7bf42021-10-19 22:28:48 +0000647 additionalData["STATUS_CML"] =
648 fmt::format("{:#02x}", psu->getStatusCML());
Brandon Wymanb85b9dd2021-10-19 21:25:17 +0000649 /* Attempts to communicate with the power supply have
650 * reached there limit. Create an error. */
651 additionalData["CALLOUT_DEVICE_PATH"] =
652 psu->getDevicePath();
653
654 createError(
655 "xyz.openbmc_project.Power.PowerSupply.Error.CommFault",
656 additionalData);
657
658 psu->setFaultLogged();
659 }
660 else if ((psu->hasInputFault() || psu->hasVINUVFault()))
Brandon Wyman3180f4d2020-12-08 17:53:46 -0600661 {
Brandon Wymanf07bc792021-10-12 19:00:35 +0000662 // Include STATUS_INPUT for input faults.
663 additionalData["STATUS_INPUT"] =
664 fmt::format("{:#02x}", psu->getStatusInput());
665
Brandon Wyman3180f4d2020-12-08 17:53:46 -0600666 /* The power supply location might be needed if the input
667 * fault is due to a problem with the power supply itself.
668 * Include the inventory path with a call out priority of
669 * low.
670 */
671 additionalData["CALLOUT_INVENTORY_PATH"] =
672 psu->getInventoryPath();
673 additionalData["CALLOUT_PRIORITY"] = "L";
674 createError("xyz.openbmc_project.Power.PowerSupply.Error."
675 "InputFault",
676 additionalData);
677 psu->setFaultLogged();
678 }
Brandon Wyman39ea02b2021-11-23 23:22:23 +0000679 else if (psu->hasPSKillFault())
680 {
681 createError(
682 "xyz.openbmc_project.Power.PowerSupply.Error.PSKillFault",
683 additionalData);
684 psu->setFaultLogged();
685 }
Brandon Wyman6710ba22021-10-27 17:39:31 +0000686 else if (psu->hasVoutOVFault())
687 {
688 // Include STATUS_VOUT for Vout faults.
689 additionalData["STATUS_VOUT"] =
690 fmt::format("{:#02x}", psu->getStatusVout());
691
692 additionalData["CALLOUT_INVENTORY_PATH"] =
693 psu->getInventoryPath();
694
695 createError(
696 "xyz.openbmc_project.Power.PowerSupply.Error.Fault",
697 additionalData);
698
699 psu->setFaultLogged();
700 }
Brandon Wymanb10b3be2021-11-09 22:12:15 +0000701 else if (psu->hasIoutOCFault())
702 {
703 // Include STATUS_IOUT for Iout faults.
704 additionalData["STATUS_IOUT"] =
705 fmt::format("{:#02x}", psu->getStatusIout());
706
707 createError(
708 "xyz.openbmc_project.Power.PowerSupply.Error.IoutOCFault",
709 additionalData);
710
711 psu->setFaultLogged();
712 }
Brandon Wyman39ea02b2021-11-23 23:22:23 +0000713 else if (psu->hasVoutUVFault() || psu->hasPS12VcsFault() ||
714 psu->hasPSCS12VFault())
Brandon Wyman2cf46942021-10-28 19:09:16 +0000715 {
716 // Include STATUS_VOUT for Vout faults.
717 additionalData["STATUS_VOUT"] =
718 fmt::format("{:#02x}", psu->getStatusVout());
719
720 additionalData["CALLOUT_INVENTORY_PATH"] =
721 psu->getInventoryPath();
722
723 createError(
724 "xyz.openbmc_project.Power.PowerSupply.Error.Fault",
725 additionalData);
726
727 psu->setFaultLogged();
728 }
Brandon Wyman7ee4d7e2021-11-19 20:48:23 +0000729 // A fan fault should have priority over a temperature fault,
730 // since a failed fan may lead to a temperature problem.
Jim Wrightaca86d02022-06-10 12:01:39 -0500731 // Only process if not in power fault window.
732 else if (psu->hasFanFault() && !powerFaultOccurring)
Brandon Wyman7ee4d7e2021-11-19 20:48:23 +0000733 {
734 // Include STATUS_TEMPERATURE and STATUS_FANS_1_2
735 additionalData["STATUS_TEMPERATURE"] =
736 fmt::format("{:#02x}", psu->getStatusTemperature());
737 additionalData["STATUS_FANS_1_2"] =
738 fmt::format("{:#02x}", psu->getStatusFans12());
739
740 additionalData["CALLOUT_INVENTORY_PATH"] =
741 psu->getInventoryPath();
742
743 createError(
744 "xyz.openbmc_project.Power.PowerSupply.Error.FanFault",
745 additionalData);
746
747 psu->setFaultLogged();
748 }
Brandon Wyman96893a42021-11-05 19:56:57 +0000749 else if (psu->hasTempFault())
750 {
751 // Include STATUS_TEMPERATURE for temperature faults.
752 additionalData["STATUS_TEMPERATURE"] =
753 fmt::format("{:#02x}", psu->getStatusTemperature());
754
755 additionalData["CALLOUT_INVENTORY_PATH"] =
756 psu->getInventoryPath();
757
758 createError(
759 "xyz.openbmc_project.Power.PowerSupply.Error.Fault",
760 additionalData);
761
762 psu->setFaultLogged();
763 }
Brandon Wyman3180f4d2020-12-08 17:53:46 -0600764 else if (psu->hasMFRFault())
765 {
766 /* This can represent a variety of faults that result in
767 * calling out the power supply for replacement: Output
768 * OverCurrent, Output Under Voltage, and potentially other
769 * faults.
770 *
771 * Also plan on putting specific fault in AdditionalData,
772 * along with register names and register values
773 * (STATUS_WORD, STATUS_MFR, etc.).*/
774
775 additionalData["CALLOUT_INVENTORY_PATH"] =
776 psu->getInventoryPath();
777
778 createError(
779 "xyz.openbmc_project.Power.PowerSupply.Error.Fault",
Brandon Wyman52e54e82020-10-08 14:44:58 -0500780 additionalData);
Brandon Wyman63ea78b2020-09-24 16:49:09 -0500781
Brandon Wyman3180f4d2020-12-08 17:53:46 -0600782 psu->setFaultLogged();
783 }
Jim Wrightaca86d02022-06-10 12:01:39 -0500784 // Only process if not in power fault window.
785 else if (psu->hasPgoodFault() && !powerFaultOccurring)
Brandon Wyman2916ea52021-11-06 03:31:18 +0000786 {
787 /* POWER_GOOD# is not low, or OFF is on */
788 additionalData["CALLOUT_INVENTORY_PATH"] =
789 psu->getInventoryPath();
790
791 createError(
792 "xyz.openbmc_project.Power.PowerSupply.Error.Fault",
793 additionalData);
794
795 psu->setFaultLogged();
796 }
Brandon Wyman4176d6b2020-10-07 17:41:06 -0500797 }
Brandon Wyman63ea78b2020-09-24 16:49:09 -0500798 }
799 }
800}
801
Brandon Wyman64e97752022-06-03 23:50:13 +0000802void PSUManager::updateMissingPSUs()
803{
804 if (supportedConfigs.empty() || psus.empty())
805 {
806 return;
807 }
808
809 // Power supplies default to missing. If the power supply is present,
810 // the PowerSupply object will update the inventory Present property to
811 // true. If we have less than the required number of power supplies, and
812 // this power supply is missing, update the inventory Present property
813 // to false to indicate required power supply is missing. Avoid
814 // indicating power supply missing if not required.
815
816 auto presentCount =
817 std::count_if(psus.begin(), psus.end(),
818 [](const auto& psu) { return psu->isPresent(); });
819
820 for (const auto& config : supportedConfigs)
821 {
822 for (const auto& psu : psus)
823 {
824 auto psuModel = psu->getModelName();
825 auto psuShortName = psu->getShortName();
826 auto psuInventoryPath = psu->getInventoryPath();
827 auto relativeInvPath =
828 psuInventoryPath.substr(strlen(INVENTORY_OBJ_PATH));
829 auto psuPresent = psu->isPresent();
830 auto presProperty = false;
831 auto propReadFail = false;
832
833 try
834 {
835 presProperty = getPresence(bus, psuInventoryPath);
836 propReadFail = false;
837 }
838 catch (const sdbusplus::exception::exception& e)
839 {
840 propReadFail = true;
841 // Relying on property change or interface added to retry.
842 // Log an informational trace to the journal.
843 log<level::INFO>(
844 fmt::format("D-Bus property {} access failure exception",
845 psuInventoryPath)
846 .c_str());
847 }
848
849 if (psuModel.empty())
850 {
851 if (!propReadFail && (presProperty != psuPresent))
852 {
853 // We already have this property, and it is not false
854 // set Present to false
855 setPresence(bus, relativeInvPath, psuPresent, psuShortName);
856 }
857 continue;
858 }
859
860 if (config.first != psuModel)
861 {
862 continue;
863 }
864
865 if ((presentCount < config.second.powerSupplyCount) && !psuPresent)
866 {
867 setPresence(bus, relativeInvPath, psuPresent, psuShortName);
868 }
869 }
870 }
871}
872
Adriana Kobylak8f16fb52021-03-31 15:50:15 +0000873void PSUManager::validateConfig()
874{
Adriana Kobylakb23e4432022-04-01 14:22:47 +0000875 if (!runValidateConfig || supportedConfigs.empty() || psus.empty())
Adriana Kobylak8f16fb52021-03-31 15:50:15 +0000876 {
877 return;
878 }
879
Brandon Wyman9666ddf2022-04-27 21:53:14 +0000880 for (const auto& psu : psus)
881 {
882 if ((psu->hasInputFault() || psu->hasVINUVFault()))
883 {
884 // Do not try to validate if input voltage fault present.
885 validationTimer->restartOnce(validationTimeout);
886 return;
887 }
888 }
889
Adriana Kobylak4d9aaf92021-06-30 15:27:42 +0000890 std::map<std::string, std::string> additionalData;
891 auto supported = hasRequiredPSUs(additionalData);
892 if (supported)
893 {
894 runValidateConfig = false;
895 return;
896 }
897
898 // Validation failed, create an error log.
899 // Return without setting the runValidateConfig flag to false because
900 // it may be that an additional supported configuration interface is
901 // added and we need to validate it to see if it matches this system.
902 createError("xyz.openbmc_project.Power.PowerSupply.Error.NotSupported",
903 additionalData);
904}
905
906bool PSUManager::hasRequiredPSUs(
907 std::map<std::string, std::string>& additionalData)
908{
Adriana Kobylak8f16fb52021-03-31 15:50:15 +0000909 std::string model{};
Adriana Kobylak523704d2021-09-21 15:55:41 +0000910 if (!validateModelName(model, additionalData))
Adriana Kobylak8f16fb52021-03-31 15:50:15 +0000911 {
Adriana Kobylak523704d2021-09-21 15:55:41 +0000912 return false;
Adriana Kobylak8f16fb52021-03-31 15:50:15 +0000913 }
Adriana Kobylak70e7f932021-06-10 18:53:56 +0000914
Adriana Kobylak4d9aaf92021-06-30 15:27:42 +0000915 auto presentCount =
916 std::count_if(psus.begin(), psus.end(),
917 [](const auto& psu) { return psu->isPresent(); });
918
Adriana Kobylak70e7f932021-06-10 18:53:56 +0000919 // Validate the supported configurations. A system may support more than one
Adriana Kobylak4175ffb2021-08-02 14:51:05 +0000920 // power supply model configuration. Since all configurations need to be
921 // checked, the additional data would contain only the information of the
922 // last configuration that did not match.
923 std::map<std::string, std::string> tmpAdditionalData;
Adriana Kobylak70e7f932021-06-10 18:53:56 +0000924 for (const auto& config : supportedConfigs)
925 {
Adriana Kobylak4d9aaf92021-06-30 15:27:42 +0000926 if (config.first != model)
Adriana Kobylak70e7f932021-06-10 18:53:56 +0000927 {
928 continue;
929 }
Brandon Wyman64e97752022-06-03 23:50:13 +0000930
Adriana Kobylak70e7f932021-06-10 18:53:56 +0000931 if (presentCount != config.second.powerSupplyCount)
932 {
Adriana Kobylak4175ffb2021-08-02 14:51:05 +0000933 tmpAdditionalData.clear();
934 tmpAdditionalData["EXPECTED_COUNT"] =
Adriana Kobylak70e7f932021-06-10 18:53:56 +0000935 std::to_string(config.second.powerSupplyCount);
Adriana Kobylak4175ffb2021-08-02 14:51:05 +0000936 tmpAdditionalData["ACTUAL_COUNT"] = std::to_string(presentCount);
Adriana Kobylak70e7f932021-06-10 18:53:56 +0000937 continue;
938 }
Adriana Kobylak4175ffb2021-08-02 14:51:05 +0000939
940 bool voltageValidated = true;
941 for (const auto& psu : psus)
942 {
943 if (!psu->isPresent())
944 {
945 // Only present PSUs report a valid input voltage
946 continue;
947 }
948
949 double actualInputVoltage;
950 int inputVoltage;
951 psu->getInputVoltage(actualInputVoltage, inputVoltage);
952
953 if (std::find(config.second.inputVoltage.begin(),
954 config.second.inputVoltage.end(),
955 inputVoltage) == config.second.inputVoltage.end())
956 {
957 tmpAdditionalData.clear();
958 tmpAdditionalData["ACTUAL_VOLTAGE"] =
959 std::to_string(actualInputVoltage);
960 for (const auto& voltage : config.second.inputVoltage)
961 {
962 tmpAdditionalData["EXPECTED_VOLTAGE"] +=
963 std::to_string(voltage) + " ";
964 }
965 tmpAdditionalData["CALLOUT_INVENTORY_PATH"] =
966 psu->getInventoryPath();
967
968 voltageValidated = false;
969 break;
970 }
971 }
972 if (!voltageValidated)
973 {
974 continue;
975 }
976
Adriana Kobylak4d9aaf92021-06-30 15:27:42 +0000977 return true;
Adriana Kobylak70e7f932021-06-10 18:53:56 +0000978 }
Adriana Kobylak70e7f932021-06-10 18:53:56 +0000979
Adriana Kobylak4175ffb2021-08-02 14:51:05 +0000980 additionalData.insert(tmpAdditionalData.begin(), tmpAdditionalData.end());
Adriana Kobylak4d9aaf92021-06-30 15:27:42 +0000981 return false;
Adriana Kobylak8f16fb52021-03-31 15:50:15 +0000982}
983
Shawn McCarney9252b7e2022-06-10 12:47:38 -0500984unsigned int PSUManager::getRequiredPSUCount()
985{
986 unsigned int requiredCount{0};
987
988 // Verify we have the supported configuration and PSU information
989 if (!supportedConfigs.empty() && !psus.empty())
990 {
991 // Find PSU models. They should all be the same.
992 std::set<std::string> models{};
993 std::for_each(psus.begin(), psus.end(), [&models](const auto& psu) {
994 if (!psu->getModelName().empty())
995 {
996 models.insert(psu->getModelName());
997 }
998 });
999
1000 // If exactly one model was found, find corresponding configuration
1001 if (models.size() == 1)
1002 {
1003 const std::string& model = *(models.begin());
1004 auto it = supportedConfigs.find(model);
1005 if (it != supportedConfigs.end())
1006 {
1007 requiredCount = it->second.powerSupplyCount;
1008 }
1009 }
1010 }
1011
1012 return requiredCount;
1013}
1014
1015bool PSUManager::isRequiredPSU(const PowerSupply& psu)
1016{
1017 // Get required number of PSUs; if not found, we don't know if PSU required
1018 unsigned int requiredCount = getRequiredPSUCount();
1019 if (requiredCount == 0)
1020 {
1021 return false;
1022 }
1023
1024 // If total PSU count <= the required count, all PSUs are required
1025 if (psus.size() <= requiredCount)
1026 {
1027 return true;
1028 }
1029
1030 // We don't currently get information from EntityManager about which PSUs
1031 // are required, so we have to do some guesswork. First check if this PSU
1032 // is present. If so, assume it is required.
1033 if (psu.isPresent())
1034 {
1035 return true;
1036 }
1037
1038 // This PSU is not present. Count the number of other PSUs that are
1039 // present. If enough other PSUs are present, assume the specified PSU is
1040 // not required.
1041 unsigned int psuCount =
1042 std::count_if(psus.begin(), psus.end(),
1043 [](const auto& psu) { return psu->isPresent(); });
1044 if (psuCount >= requiredCount)
1045 {
1046 return false;
1047 }
1048
1049 // Check if this PSU was previously present. If so, assume it is required.
1050 // We know it was previously present if it has a non-empty model name.
1051 if (!psu.getModelName().empty())
1052 {
1053 return true;
1054 }
1055
1056 // This PSU was never present. Count the number of other PSUs that were
1057 // previously present. If including those PSUs is enough, assume the
1058 // specified PSU is not required.
1059 psuCount += std::count_if(psus.begin(), psus.end(), [](const auto& psu) {
1060 return (!psu->isPresent() && !psu->getModelName().empty());
1061 });
1062 if (psuCount >= requiredCount)
1063 {
1064 return false;
1065 }
1066
1067 // We still haven't found enough PSUs. Sort the inventory paths of PSUs
1068 // that were never present. PSU inventory paths typically end with the PSU
1069 // number (0, 1, 2, ...). Assume that lower-numbered PSUs are required.
1070 std::vector<std::string> sortedPaths;
1071 std::for_each(psus.begin(), psus.end(), [&sortedPaths](const auto& psu) {
1072 if (!psu->isPresent() && psu->getModelName().empty())
1073 {
1074 sortedPaths.push_back(psu->getInventoryPath());
1075 }
1076 });
1077 std::sort(sortedPaths.begin(), sortedPaths.end());
1078
1079 // Check if specified PSU is close enough to start of list to be required
1080 for (const auto& path : sortedPaths)
1081 {
1082 if (path == psu.getInventoryPath())
1083 {
1084 return true;
1085 }
1086 if (++psuCount >= requiredCount)
1087 {
1088 break;
1089 }
1090 }
1091
1092 // PSU was not close to start of sorted list; assume not required
1093 return false;
1094}
1095
Adriana Kobylak523704d2021-09-21 15:55:41 +00001096bool PSUManager::validateModelName(
1097 std::string& model, std::map<std::string, std::string>& additionalData)
1098{
1099 // Check that all PSUs have the same model name. Initialize the model
1100 // variable with the first PSU name found, then use it as a base to compare
Adriana Kobylakb70eae92022-01-20 22:09:56 +00001101 // against the rest of the PSUs and get its inventory path to use as callout
1102 // if needed.
Adriana Kobylak523704d2021-09-21 15:55:41 +00001103 model.clear();
Adriana Kobylakb70eae92022-01-20 22:09:56 +00001104 std::string modelInventoryPath{};
Adriana Kobylak523704d2021-09-21 15:55:41 +00001105 for (const auto& psu : psus)
1106 {
1107 auto psuModel = psu->getModelName();
1108 if (psuModel.empty())
1109 {
1110 continue;
1111 }
1112 if (model.empty())
1113 {
1114 model = psuModel;
Adriana Kobylakb70eae92022-01-20 22:09:56 +00001115 modelInventoryPath = psu->getInventoryPath();
Adriana Kobylak523704d2021-09-21 15:55:41 +00001116 continue;
1117 }
1118 if (psuModel != model)
1119 {
Adriana Kobylakb70eae92022-01-20 22:09:56 +00001120 if (supportedConfigs.find(model) != supportedConfigs.end())
1121 {
1122 // The base model is supported, callout the mismatched PSU. The
1123 // mismatched PSU may or may not be supported.
1124 additionalData["EXPECTED_MODEL"] = model;
1125 additionalData["ACTUAL_MODEL"] = psuModel;
1126 additionalData["CALLOUT_INVENTORY_PATH"] =
1127 psu->getInventoryPath();
1128 }
1129 else if (supportedConfigs.find(psuModel) != supportedConfigs.end())
1130 {
1131 // The base model is not supported, but the mismatched PSU is,
1132 // callout the base PSU.
1133 additionalData["EXPECTED_MODEL"] = psuModel;
1134 additionalData["ACTUAL_MODEL"] = model;
1135 additionalData["CALLOUT_INVENTORY_PATH"] = modelInventoryPath;
1136 }
1137 else
1138 {
1139 // The base model and the mismatched PSU are not supported or
1140 // could not be found in the supported configuration, callout
1141 // the mismatched PSU.
1142 additionalData["EXPECTED_MODEL"] = model;
1143 additionalData["ACTUAL_MODEL"] = psuModel;
1144 additionalData["CALLOUT_INVENTORY_PATH"] =
1145 psu->getInventoryPath();
1146 }
Adriana Kobylak523704d2021-09-21 15:55:41 +00001147 model.clear();
1148 return false;
1149 }
1150 }
1151 return true;
1152}
1153
Adriana Kobylakc0a07582021-10-13 15:52:25 +00001154void PSUManager::setPowerConfigGPIO()
1155{
1156 if (!powerConfigGPIO)
1157 {
1158 return;
1159 }
1160
1161 std::string model{};
1162 std::map<std::string, std::string> additionalData;
1163 if (!validateModelName(model, additionalData))
1164 {
1165 return;
1166 }
1167
1168 auto config = supportedConfigs.find(model);
1169 if (config != supportedConfigs.end())
1170 {
1171 // The power-config-full-load is an open drain GPIO. Set it to low (0)
1172 // if the supported configuration indicates that this system model
1173 // expects the maximum number of power supplies (full load set to true).
1174 // Else, set it to high (1), this is the default.
1175 auto powerConfigValue =
1176 (config->second.powerConfigFullLoad == true ? 0 : 1);
1177 auto flags = gpiod::line_request::FLAG_OPEN_DRAIN;
1178 powerConfigGPIO->write(powerConfigValue, flags);
1179 }
1180}
1181
Adriana Kobylake5b1e082022-03-02 15:37:32 +00001182void PSUManager::setBrownout(std::map<std::string, std::string>& additionalData)
1183{
1184 powerSystemInputs.status(sdbusplus::xyz::openbmc_project::State::Decorator::
1185 server::PowerSystemInputs::Status::Fault);
1186 if (!brownoutLogged)
1187 {
1188 if (powerOn)
1189 {
1190 createError(
1191 "xyz.openbmc_project.State.Shutdown.Power.Error.Blackout",
1192 additionalData);
1193 brownoutLogged = true;
1194 }
1195 }
1196}
1197
1198void PSUManager::clearBrownout()
1199{
1200 powerSystemInputs.status(sdbusplus::xyz::openbmc_project::State::Decorator::
1201 server::PowerSystemInputs::Status::Good);
1202 brownoutLogged = false;
1203}
1204
Brandon Wyman63ea78b2020-09-24 16:49:09 -05001205} // namespace phosphor::power::manager