blob: 1940eea133ee601932564f2b999336b69521b419 [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 Wymanaed1f752019-11-25 18:10:52 -06009using namespace phosphor::logging;
10
Brandon Wyman63ea78b2020-09-24 16:49:09 -050011namespace phosphor::power::manager
Brandon Wymana0f33ce2019-10-17 18:32:29 -050012{
13
Brandon Wyman510acaa2020-11-05 18:32:04 -060014constexpr auto IBMCFFPSInterface =
15 "xyz.openbmc_project.Configuration.IBMCFFPSConnector";
16constexpr auto i2cBusProp = "I2CBus";
17constexpr auto i2cAddressProp = "I2CAddress";
18constexpr auto psuNameProp = "Name";
B. J. Wyman681b2a32021-04-20 22:31:22 +000019constexpr auto presLineName = "NamedPresenceGpio";
Brandon Wyman510acaa2020-11-05 18:32:04 -060020
Adriana Kobylak9bab9e12021-02-24 15:32:03 -060021constexpr auto supportedConfIntf =
22 "xyz.openbmc_project.Configuration.SupportedConfiguration";
Adriana Kobylak9bab9e12021-02-24 15:32:03 -060023
Brandon Wyman510acaa2020-11-05 18:32:04 -060024PSUManager::PSUManager(sdbusplus::bus::bus& bus, const sdeventplus::Event& e) :
25 bus(bus)
26{
Brandon Wyman510acaa2020-11-05 18:32:04 -060027 // Subscribe to InterfacesAdded before doing a property read, otherwise
28 // the interface could be created after the read attempt but before the
29 // match is created.
30 entityManagerIfacesAddedMatch = std::make_unique<sdbusplus::bus::match_t>(
31 bus,
32 sdbusplus::bus::match::rules::interfacesAdded() +
33 sdbusplus::bus::match::rules::sender(
34 "xyz.openbmc_project.EntityManager"),
35 std::bind(&PSUManager::entityManagerIfaceAdded, this,
36 std::placeholders::_1));
37 getPSUConfiguration();
38 getSystemProperties();
39
40 using namespace sdeventplus;
41 auto interval = std::chrono::milliseconds(1000);
42 timer = std::make_unique<utility::Timer<ClockId::Monotonic>>(
43 e, std::bind(&PSUManager::analyze, this), interval);
44
45 // Subscribe to power state changes
46 powerService = util::getService(POWER_OBJ_PATH, POWER_IFACE, bus);
47 powerOnMatch = std::make_unique<sdbusplus::bus::match_t>(
48 bus,
49 sdbusplus::bus::match::rules::propertiesChanged(POWER_OBJ_PATH,
50 POWER_IFACE),
51 [this](auto& msg) { this->powerStateChanged(msg); });
52
53 initialize();
54}
55
Brandon Wyman510acaa2020-11-05 18:32:04 -060056void PSUManager::getPSUConfiguration()
57{
58 using namespace phosphor::power::util;
59 auto depth = 0;
60 auto objects = getSubTree(bus, "/", IBMCFFPSInterface, depth);
61
62 psus.clear();
63
64 // I should get a map of objects back.
65 // Each object will have a path, a service, and an interface.
66 // The interface should match the one passed into this function.
67 for (const auto& [path, services] : objects)
68 {
69 auto service = services.begin()->first;
70
71 if (path.empty() || service.empty())
72 {
73 continue;
74 }
75
76 // For each object in the array of objects, I want to get properties
77 // from the service, path, and interface.
78 auto properties =
79 getAllProperties(bus, path, IBMCFFPSInterface, service);
80
81 getPSUProperties(properties);
82 }
83
84 if (psus.empty())
85 {
86 // Interface or properties not found. Let the Interfaces Added callback
87 // process the information once the interfaces are added to D-Bus.
88 log<level::INFO>(fmt::format("No power supplies to monitor").c_str());
89 }
90}
91
92void PSUManager::getPSUProperties(util::DbusPropertyMap& properties)
93{
94 // From passed in properties, I want to get: I2CBus, I2CAddress,
95 // and Name. Create a power supply object, using Name to build the inventory
96 // path.
97 const auto basePSUInvPath =
98 "/xyz/openbmc_project/inventory/system/chassis/motherboard/powersupply";
99 uint64_t* i2cbus = nullptr;
100 uint64_t* i2caddr = nullptr;
101 std::string* psuname = nullptr;
B. J. Wyman681b2a32021-04-20 22:31:22 +0000102 std::string* preslineptr = nullptr;
Brandon Wyman510acaa2020-11-05 18:32:04 -0600103
104 for (const auto& property : properties)
105 {
106 try
107 {
108 if (property.first == i2cBusProp)
109 {
110 i2cbus = std::get_if<uint64_t>(&properties[i2cBusProp]);
111 }
112 else if (property.first == i2cAddressProp)
113 {
114 i2caddr = std::get_if<uint64_t>(&properties[i2cAddressProp]);
115 }
116 else if (property.first == psuNameProp)
117 {
118 psuname = std::get_if<std::string>(&properties[psuNameProp]);
119 }
B. J. Wyman681b2a32021-04-20 22:31:22 +0000120 else if (property.first == presLineName)
121 {
122 preslineptr =
123 std::get_if<std::string>(&properties[presLineName]);
124 }
Brandon Wyman510acaa2020-11-05 18:32:04 -0600125 }
126 catch (std::exception& e)
127 {
128 }
129 }
130
131 if ((i2cbus) && (i2caddr) && (psuname) && (!psuname->empty()))
132 {
133 std::string invpath = basePSUInvPath;
134 invpath.push_back(psuname->back());
B. J. Wyman681b2a32021-04-20 22:31:22 +0000135 std::string presline = "";
Brandon Wyman510acaa2020-11-05 18:32:04 -0600136
137 log<level::DEBUG>(fmt::format("Inventory Path: {}", invpath).c_str());
138
B. J. Wyman681b2a32021-04-20 22:31:22 +0000139 if (nullptr != preslineptr)
140 {
141 presline = *preslineptr;
142 }
143
144 log<level::DEBUG>(
145 fmt::format("make PowerSupply bus: {} addr: {} presline: {}",
146 *i2cbus, *i2caddr, presline)
147 .c_str());
148 auto psu = std::make_unique<PowerSupply>(bus, invpath, *i2cbus,
149 *i2caddr, presline);
Brandon Wyman510acaa2020-11-05 18:32:04 -0600150 psus.emplace_back(std::move(psu));
151 }
152
153 if (psus.empty())
154 {
155 log<level::INFO>(fmt::format("No power supplies to monitor").c_str());
156 }
157}
158
Adriana Kobylake1074d82021-03-16 20:46:44 +0000159void PSUManager::populateSysProperties(const util::DbusPropertyMap& properties)
160{
161 try
162 {
Adriana Kobylak9ea66a62021-03-24 17:54:14 +0000163 auto propIt = properties.find("SupportedType");
164 if (propIt == properties.end())
165 {
166 return;
167 }
168 const std::string* type = std::get_if<std::string>(&(propIt->second));
169 if ((type == nullptr) || (*type != "PowerSupply"))
170 {
171 return;
172 }
173
Adriana Kobylak9ea66a62021-03-24 17:54:14 +0000174 propIt = properties.find("SupportedModel");
175 if (propIt == properties.end())
176 {
177 return;
178 }
Adriana Kobylakd3a70d92021-06-04 16:24:45 +0000179 const std::string* model = std::get_if<std::string>(&(propIt->second));
180 if (model == nullptr)
Adriana Kobylak9ea66a62021-03-24 17:54:14 +0000181 {
182 return;
183 }
Adriana Kobylak9ea66a62021-03-24 17:54:14 +0000184
Adriana Kobylakd3a70d92021-06-04 16:24:45 +0000185 sys_properties sys;
Adriana Kobylak9ea66a62021-03-24 17:54:14 +0000186 propIt = properties.find("RedundantCount");
Adriana Kobylake1074d82021-03-16 20:46:44 +0000187 if (propIt != properties.end())
188 {
189 const uint64_t* count = std::get_if<uint64_t>(&(propIt->second));
190 if (count != nullptr)
191 {
Adriana Kobylakd3a70d92021-06-04 16:24:45 +0000192 sys.powerSupplyCount = *count;
Adriana Kobylake1074d82021-03-16 20:46:44 +0000193 }
194 }
Adriana Kobylak9ea66a62021-03-24 17:54:14 +0000195 propIt = properties.find("InputVoltage");
196 if (propIt != properties.end())
197 {
Adriana Kobylakd3a70d92021-06-04 16:24:45 +0000198 const std::vector<uint64_t>* voltage =
199 std::get_if<std::vector<uint64_t>>(&(propIt->second));
Adriana Kobylak9ea66a62021-03-24 17:54:14 +0000200 if (voltage != nullptr)
201 {
202 sys.inputVoltage = *voltage;
203 }
204 }
205
Adriana Kobylakd3a70d92021-06-04 16:24:45 +0000206 supportedConfigs.emplace(*model, sys);
Adriana Kobylake1074d82021-03-16 20:46:44 +0000207 }
208 catch (std::exception& e)
209 {
210 }
211}
212
Adriana Kobylak9bab9e12021-02-24 15:32:03 -0600213void PSUManager::getSystemProperties()
214{
Adriana Kobylak9bab9e12021-02-24 15:32:03 -0600215
Adriana Kobylak9bab9e12021-02-24 15:32:03 -0600216 try
217 {
218 util::DbusSubtree subtree =
219 util::getSubTree(bus, INVENTORY_OBJ_PATH, supportedConfIntf, 0);
Adriana Kobylake1074d82021-03-16 20:46:44 +0000220 if (subtree.empty())
Adriana Kobylak9bab9e12021-02-24 15:32:03 -0600221 {
222 throw std::runtime_error("Supported Configuration Not Found");
223 }
Adriana Kobylak9bab9e12021-02-24 15:32:03 -0600224
Adriana Kobylake1074d82021-03-16 20:46:44 +0000225 for (const auto& [objPath, services] : subtree)
226 {
227 std::string service = services.begin()->first;
228 if (objPath.empty() || service.empty())
229 {
230 continue;
Adriana Kobylak9bab9e12021-02-24 15:32:03 -0600231 }
Adriana Kobylake1074d82021-03-16 20:46:44 +0000232 auto properties = util::getAllProperties(
233 bus, objPath, supportedConfIntf, service);
234 populateSysProperties(properties);
Adriana Kobylak9bab9e12021-02-24 15:32:03 -0600235 }
236 }
237 catch (std::exception& e)
238 {
239 // Interface or property not found. Let the Interfaces Added callback
240 // process the information once the interfaces are added to D-Bus.
241 }
242}
243
Brandon Wyman3e429132021-03-18 18:03:14 -0500244void PSUManager::entityManagerIfaceAdded(sdbusplus::message::message& msg)
Adriana Kobylak9bab9e12021-02-24 15:32:03 -0600245{
246 try
247 {
248 sdbusplus::message::object_path objPath;
Adriana Kobylake1074d82021-03-16 20:46:44 +0000249 std::map<std::string, std::map<std::string, util::DbusVariant>>
Adriana Kobylak9bab9e12021-02-24 15:32:03 -0600250 interfaces;
251 msg.read(objPath, interfaces);
252
253 auto itIntf = interfaces.find(supportedConfIntf);
Brandon Wyman510acaa2020-11-05 18:32:04 -0600254 if (itIntf != interfaces.cend())
Adriana Kobylak9bab9e12021-02-24 15:32:03 -0600255 {
Brandon Wyman510acaa2020-11-05 18:32:04 -0600256 populateSysProperties(itIntf->second);
Adriana Kobylak9bab9e12021-02-24 15:32:03 -0600257 }
258
Brandon Wyman510acaa2020-11-05 18:32:04 -0600259 itIntf = interfaces.find(IBMCFFPSInterface);
260 if (itIntf != interfaces.cend())
261 {
262 log<level::INFO>(
263 fmt::format("InterfacesAdded for: {}", IBMCFFPSInterface)
264 .c_str());
265 getPSUProperties(itIntf->second);
266 }
Adriana Kobylak8f16fb52021-03-31 15:50:15 +0000267
268 // Call to validate the psu configuration if the power is on and both
269 // the IBMCFFPSConnector and SupportedConfiguration interfaces have been
270 // processed
271 if (powerOn && !psus.empty() && !supportedConfigs.empty())
272 {
273 validateConfig();
274 }
Adriana Kobylak9bab9e12021-02-24 15:32:03 -0600275 }
276 catch (std::exception& e)
277 {
278 // Ignore, the property may be of a different type than expected.
279 }
280}
281
Brandon Wymana0f33ce2019-10-17 18:32:29 -0500282void PSUManager::powerStateChanged(sdbusplus::message::message& msg)
283{
284 int32_t state = 0;
285 std::string msgSensor;
Patrick Williamsabe49412020-05-13 17:59:47 -0500286 std::map<std::string, std::variant<int32_t>> msgData;
Brandon Wymana0f33ce2019-10-17 18:32:29 -0500287 msg.read(msgSensor, msgData);
288
289 // Check if it was the Present property that changed.
290 auto valPropMap = msgData.find("state");
291 if (valPropMap != msgData.end())
292 {
293 state = std::get<int32_t>(valPropMap->second);
294
295 // Power is on when state=1. Clear faults.
296 if (state)
297 {
298 powerOn = true;
Adriana Kobylak8f16fb52021-03-31 15:50:15 +0000299 validateConfig();
Brandon Wymana0f33ce2019-10-17 18:32:29 -0500300 clearFaults();
301 }
302 else
303 {
304 powerOn = false;
Adriana Kobylak8f16fb52021-03-31 15:50:15 +0000305 runValidateConfig = true;
Brandon Wymana0f33ce2019-10-17 18:32:29 -0500306 }
307 }
308}
309
Brandon Wymanb76ab242020-09-16 18:06:06 -0500310void PSUManager::createError(
311 const std::string& faultName,
312 const std::map<std::string, std::string>& additionalData)
313{
314 using namespace sdbusplus::xyz::openbmc_project;
315 constexpr auto loggingObjectPath = "/xyz/openbmc_project/logging";
316 constexpr auto loggingCreateInterface =
317 "xyz.openbmc_project.Logging.Create";
318
319 try
320 {
321 auto service =
322 util::getService(loggingObjectPath, loggingCreateInterface, bus);
323
324 if (service.empty())
325 {
326 log<level::ERR>("Unable to get logging manager service");
327 return;
328 }
329
330 auto method = bus.new_method_call(service.c_str(), loggingObjectPath,
331 loggingCreateInterface, "Create");
332
333 auto level = Logging::server::Entry::Level::Error;
334 method.append(faultName, level, additionalData);
335
336 auto reply = bus.call(method);
337 }
338 catch (std::exception& e)
339 {
340 log<level::ERR>(
341 fmt::format(
342 "Failed creating event log for fault {} due to error {}",
343 faultName, e.what())
344 .c_str());
345 }
346}
347
Brandon Wyman63ea78b2020-09-24 16:49:09 -0500348void PSUManager::analyze()
349{
350 for (auto& psu : psus)
351 {
352 psu->analyze();
353 }
354
Brandon Wyman3180f4d2020-12-08 17:53:46 -0600355 if (powerOn)
Brandon Wyman63ea78b2020-09-24 16:49:09 -0500356 {
Brandon Wyman3180f4d2020-12-08 17:53:46 -0600357 for (auto& psu : psus)
Brandon Wyman63ea78b2020-09-24 16:49:09 -0500358 {
Brandon Wyman3180f4d2020-12-08 17:53:46 -0600359 std::map<std::string, std::string> additionalData;
360 additionalData["_PID"] = std::to_string(getpid());
361 // TODO: Fault priorities #918
362 if (!psu->isFaultLogged() && !psu->isPresent())
363 {
364 // Create error for power supply missing.
365 additionalData["CALLOUT_INVENTORY_PATH"] =
366 psu->getInventoryPath();
367 additionalData["CALLOUT_PRIORITY"] = "H";
368 createError(
369 "xyz.openbmc_project.Power.PowerSupply.Error.Missing",
370 additionalData);
371 psu->setFaultLogged();
372 }
373 else if (!psu->isFaultLogged() && psu->isFaulted())
374 {
375 additionalData["STATUS_WORD"] =
376 std::to_string(psu->getStatusWord());
Jay Meyer10d94052020-11-30 14:41:21 -0600377 additionalData["STATUS_MFR"] =
378 std::to_string(psu->getMFRFault());
Brandon Wyman3180f4d2020-12-08 17:53:46 -0600379 // If there are faults being reported, they possibly could be
380 // related to a bug in the firmware version running on the power
381 // supply. Capture that data into the error as well.
382 additionalData["FW_VERSION"] = psu->getFWVersion();
383
384 if ((psu->hasInputFault() || psu->hasVINUVFault()))
385 {
386 /* The power supply location might be needed if the input
387 * fault is due to a problem with the power supply itself.
388 * Include the inventory path with a call out priority of
389 * low.
390 */
391 additionalData["CALLOUT_INVENTORY_PATH"] =
392 psu->getInventoryPath();
393 additionalData["CALLOUT_PRIORITY"] = "L";
394 createError("xyz.openbmc_project.Power.PowerSupply.Error."
395 "InputFault",
396 additionalData);
397 psu->setFaultLogged();
398 }
399 else if (psu->hasMFRFault())
400 {
401 /* This can represent a variety of faults that result in
402 * calling out the power supply for replacement: Output
403 * OverCurrent, Output Under Voltage, and potentially other
404 * faults.
405 *
406 * Also plan on putting specific fault in AdditionalData,
407 * along with register names and register values
408 * (STATUS_WORD, STATUS_MFR, etc.).*/
409
410 additionalData["CALLOUT_INVENTORY_PATH"] =
411 psu->getInventoryPath();
412
413 createError(
414 "xyz.openbmc_project.Power.PowerSupply.Error.Fault",
Brandon Wyman52e54e82020-10-08 14:44:58 -0500415 additionalData);
Brandon Wyman63ea78b2020-09-24 16:49:09 -0500416
Brandon Wyman3180f4d2020-12-08 17:53:46 -0600417 psu->setFaultLogged();
418 }
419 else if (psu->hasCommFault())
420 {
421 /* Attempts to communicate with the power supply have
422 * reached there limit. Create an error. */
423 additionalData["CALLOUT_DEVICE_PATH"] =
424 psu->getDevicePath();
Brandon Wymanb76ab242020-09-16 18:06:06 -0500425
Brandon Wyman3180f4d2020-12-08 17:53:46 -0600426 createError(
427 "xyz.openbmc_project.Power.PowerSupply.Error.CommFault",
428 additionalData);
Brandon Wymanb76ab242020-09-16 18:06:06 -0500429
Brandon Wyman3180f4d2020-12-08 17:53:46 -0600430 psu->setFaultLogged();
431 }
Brandon Wyman4176d6b2020-10-07 17:41:06 -0500432 }
Brandon Wyman63ea78b2020-09-24 16:49:09 -0500433 }
434 }
435}
436
Adriana Kobylak8f16fb52021-03-31 15:50:15 +0000437void PSUManager::validateConfig()
438{
Adriana Kobylak70e7f932021-06-10 18:53:56 +0000439 if (!runValidateConfig || supportedConfigs.empty())
Adriana Kobylak8f16fb52021-03-31 15:50:15 +0000440 {
441 return;
442 }
443
Adriana Kobylak4d9aaf92021-06-30 15:27:42 +0000444 std::map<std::string, std::string> additionalData;
445 auto supported = hasRequiredPSUs(additionalData);
446 if (supported)
447 {
448 runValidateConfig = false;
449 return;
450 }
451
452 // Validation failed, create an error log.
453 // Return without setting the runValidateConfig flag to false because
454 // it may be that an additional supported configuration interface is
455 // added and we need to validate it to see if it matches this system.
456 createError("xyz.openbmc_project.Power.PowerSupply.Error.NotSupported",
457 additionalData);
458}
459
460bool PSUManager::hasRequiredPSUs(
461 std::map<std::string, std::string>& additionalData)
462{
Adriana Kobylak8f16fb52021-03-31 15:50:15 +0000463 // Check that all PSUs have the same model name. Initialize the model
464 // variable with the first PSU name found, then use it as a base to compare
465 // against the rest of the PSUs.
466 std::string model{};
Adriana Kobylak4d9aaf92021-06-30 15:27:42 +0000467 for (const auto& psu : psus)
Adriana Kobylak8f16fb52021-03-31 15:50:15 +0000468 {
Adriana Kobylak4d9aaf92021-06-30 15:27:42 +0000469 auto psuModel = psu->getModelName();
Adriana Kobylak8f16fb52021-03-31 15:50:15 +0000470 if (psuModel.empty())
471 {
472 continue;
473 }
474 if (model.empty())
475 {
476 model = psuModel;
477 continue;
478 }
Adriana Kobylak4d9aaf92021-06-30 15:27:42 +0000479 if (psuModel != model)
Adriana Kobylak8f16fb52021-03-31 15:50:15 +0000480 {
Adriana Kobylak8f16fb52021-03-31 15:50:15 +0000481 additionalData["EXPECTED_MODEL"] = model;
482 additionalData["ACTUAL_MODEL"] = psuModel;
Adriana Kobylak4d9aaf92021-06-30 15:27:42 +0000483 additionalData["CALLOUT_INVENTORY_PATH"] = psu->getInventoryPath();
484 return false;
Adriana Kobylak8f16fb52021-03-31 15:50:15 +0000485 }
486 }
Adriana Kobylak70e7f932021-06-10 18:53:56 +0000487
Adriana Kobylak4d9aaf92021-06-30 15:27:42 +0000488 auto presentCount =
489 std::count_if(psus.begin(), psus.end(),
490 [](const auto& psu) { return psu->isPresent(); });
491
Adriana Kobylak70e7f932021-06-10 18:53:56 +0000492 // Validate the supported configurations. A system may support more than one
493 // power supply model configuration.
Adriana Kobylak70e7f932021-06-10 18:53:56 +0000494 for (const auto& config : supportedConfigs)
495 {
Adriana Kobylak4d9aaf92021-06-30 15:27:42 +0000496 if (config.first != model)
Adriana Kobylak70e7f932021-06-10 18:53:56 +0000497 {
498 continue;
499 }
500 if (presentCount != config.second.powerSupplyCount)
501 {
502 additionalData["EXPECTED_COUNT"] =
503 std::to_string(config.second.powerSupplyCount);
Adriana Kobylak4d9aaf92021-06-30 15:27:42 +0000504 additionalData["ACTUAL_COUNT"] = std::to_string(presentCount);
Adriana Kobylak70e7f932021-06-10 18:53:56 +0000505 continue;
506 }
Adriana Kobylak4d9aaf92021-06-30 15:27:42 +0000507 return true;
Adriana Kobylak70e7f932021-06-10 18:53:56 +0000508 }
Adriana Kobylak70e7f932021-06-10 18:53:56 +0000509
Adriana Kobylak4d9aaf92021-06-30 15:27:42 +0000510 return false;
Adriana Kobylak8f16fb52021-03-31 15:50:15 +0000511}
512
Brandon Wyman63ea78b2020-09-24 16:49:09 -0500513} // namespace phosphor::power::manager