blob: 9e58ff2fbb544f0d71023168a57b6e4e5ac960cc [file] [log] [blame]
Brandon Wyman1d7a7df2020-03-26 10:14:05 -05001#include "config.h"
2
Brandon Wymanaed1f752019-11-25 18:10:52 -06003#include "power_supply.hpp"
4
5#include "types.hpp"
Brandon Wyman3f1242f2020-01-28 13:11:25 -06006#include "util.hpp"
Brandon Wymanaed1f752019-11-25 18:10:52 -06007
Brandon Wymandf13c3a2020-12-15 14:25:22 -06008#include <fmt/format.h>
9
Brandon Wyman3f1242f2020-01-28 13:11:25 -060010#include <xyz/openbmc_project/Common/Device/error.hpp>
11
Brandon Wyman1d7a7df2020-03-26 10:14:05 -050012#include <chrono> // sleep_for()
13#include <cstdint> // uint8_t...
B. J. Wyman681b2a32021-04-20 22:31:22 +000014#include <fstream>
15#include <thread> // sleep_for()
Brandon Wyman1d7a7df2020-03-26 10:14:05 -050016
Brandon Wyman3f1242f2020-01-28 13:11:25 -060017namespace phosphor::power::psu
Brandon Wymanaed1f752019-11-25 18:10:52 -060018{
B. J. Wyman681b2a32021-04-20 22:31:22 +000019// Amount of time in milliseconds to delay between power supply going from
20// missing to present before running the bind command(s).
21constexpr auto bindDelay = 1000;
Brandon Wymanaed1f752019-11-25 18:10:52 -060022
23using namespace phosphor::logging;
Brandon Wyman3f1242f2020-01-28 13:11:25 -060024using namespace sdbusplus::xyz::openbmc_project::Common::Device::Error;
Brandon Wymanaed1f752019-11-25 18:10:52 -060025
Brandon Wyman510acaa2020-11-05 18:32:04 -060026PowerSupply::PowerSupply(sdbusplus::bus::bus& bus, const std::string& invpath,
B. J. Wyman681b2a32021-04-20 22:31:22 +000027 std::uint8_t i2cbus, std::uint16_t i2caddr,
28 const std::string& gpioLineName) :
Brandon Wyman510acaa2020-11-05 18:32:04 -060029 bus(bus),
B. J. Wyman681b2a32021-04-20 22:31:22 +000030 inventoryPath(invpath), bindPath("/sys/bus/i2c/drivers/ibm-cffps")
Brandon Wyman510acaa2020-11-05 18:32:04 -060031{
32 if (inventoryPath.empty())
33 {
34 throw std::invalid_argument{"Invalid empty inventoryPath"};
35 }
36
B. J. Wyman681b2a32021-04-20 22:31:22 +000037 if (gpioLineName.empty())
38 {
39 throw std::invalid_argument{"Invalid empty gpioLineName"};
40 }
Brandon Wyman510acaa2020-11-05 18:32:04 -060041
B. J. Wyman681b2a32021-04-20 22:31:22 +000042 log<level::DEBUG>(fmt::format("gpioLineName: {}", gpioLineName).c_str());
43 presenceGPIO = createGPIO(gpioLineName);
Brandon Wyman510acaa2020-11-05 18:32:04 -060044
45 std::ostringstream ss;
46 ss << std::hex << std::setw(4) << std::setfill('0') << i2caddr;
47 std::string addrStr = ss.str();
B. J. Wyman681b2a32021-04-20 22:31:22 +000048 std::string busStr = std::to_string(i2cbus);
49 bindDevice = busStr;
50 bindDevice.append("-");
51 bindDevice.append(addrStr);
52
Brandon Wyman510acaa2020-11-05 18:32:04 -060053 pmbusIntf = phosphor::pmbus::createPMBus(i2cbus, addrStr);
54
55 // Get the current state of the Present property.
B. J. Wyman681b2a32021-04-20 22:31:22 +000056 try
57 {
58 updatePresenceGPIO();
59 }
60 catch (...)
61 {
62 // If the above attempt to use the GPIO failed, it likely means that the
63 // GPIOs are in use by the kernel, meaning it is using gpio-keys.
64 // So, I should rely on phosphor-gpio-presence to update D-Bus, and
65 // work that way for power supply presence.
66 presenceGPIO = nullptr;
67 // Setup the functions to call when the D-Bus inventory path for the
68 // Present property changes.
69 presentMatch = std::make_unique<sdbusplus::bus::match_t>(
70 bus,
71 sdbusplus::bus::match::rules::propertiesChanged(inventoryPath,
72 INVENTORY_IFACE),
73 [this](auto& msg) { this->inventoryChanged(msg); });
74
75 presentAddedMatch = std::make_unique<sdbusplus::bus::match_t>(
76 bus,
77 sdbusplus::bus::match::rules::interfacesAdded() +
78 sdbusplus::bus::match::rules::argNpath(0, inventoryPath),
79 [this](auto& msg) { this->inventoryAdded(msg); });
80
81 updatePresence();
82 updateInventory();
83 }
84}
85
86void PowerSupply::bindOrUnbindDriver(bool present)
87{
88 auto action = (present) ? "bind" : "unbind";
89 auto path = bindPath / action;
90
91 if (present)
92 {
93 log<level::INFO>(
94 fmt::format("Binding device driver. path: {} device: {}",
95 path.string(), bindDevice)
96 .c_str());
97 }
98 else
99 {
100 log<level::INFO>(
101 fmt::format("Unbinding device driver. path: {} device: {}",
102 path.string(), bindDevice)
103 .c_str());
104 }
105
106 std::ofstream file;
107
108 file.exceptions(std::ofstream::failbit | std::ofstream::badbit |
109 std::ofstream::eofbit);
110
111 try
112 {
113 file.open(path);
114 file << bindDevice;
115 file.close();
116 }
Patrick Williamsc1d4de52021-10-06 12:45:57 -0500117 catch (const std::exception& e)
B. J. Wyman681b2a32021-04-20 22:31:22 +0000118 {
119 auto err = errno;
120
121 log<level::ERR>(
122 fmt::format("Failed binding or unbinding device. errno={}", err)
123 .c_str());
124 }
Brandon Wyman510acaa2020-11-05 18:32:04 -0600125}
126
Brandon Wymanaed1f752019-11-25 18:10:52 -0600127void PowerSupply::updatePresence()
128{
129 try
130 {
Brandon Wyman3f1242f2020-01-28 13:11:25 -0600131 present = getPresence(bus, inventoryPath);
Brandon Wymanaed1f752019-11-25 18:10:52 -0600132 }
Patrick Williams69f10ad2021-09-02 09:46:49 -0500133 catch (const sdbusplus::exception::exception& e)
Brandon Wymanaed1f752019-11-25 18:10:52 -0600134 {
135 // Relying on property change or interface added to retry.
136 // Log an informational trace to the journal.
Brandon Wymandf13c3a2020-12-15 14:25:22 -0600137 log<level::INFO>(
138 fmt::format("D-Bus property {} access failure exception",
139 inventoryPath)
140 .c_str());
Brandon Wymanaed1f752019-11-25 18:10:52 -0600141 }
142}
143
B. J. Wyman681b2a32021-04-20 22:31:22 +0000144void PowerSupply::updatePresenceGPIO()
145{
146 bool presentOld = present;
147
148 try
149 {
150 if (presenceGPIO->read() > 0)
151 {
152 present = true;
153 }
154 else
155 {
156 present = false;
157 }
158 }
Patrick Williamsc1d4de52021-10-06 12:45:57 -0500159 catch (const std::exception& e)
B. J. Wyman681b2a32021-04-20 22:31:22 +0000160 {
161 log<level::ERR>(
162 fmt::format("presenceGPIO read fail: {}", e.what()).c_str());
163 throw;
164 }
165
166 if (presentOld != present)
167 {
168 log<level::DEBUG>(
169 fmt::format("presentOld: {} present: {}", presentOld, present)
170 .c_str());
171 if (present)
172 {
173 std::this_thread::sleep_for(std::chrono::milliseconds(bindDelay));
174 bindOrUnbindDriver(present);
175 pmbusIntf->findHwmonDir();
176 onOffConfig(phosphor::pmbus::ON_OFF_CONFIG_CONTROL_PIN_ONLY);
177 clearFaults();
178 }
179 else
180 {
181 bindOrUnbindDriver(present);
182 }
183
184 auto invpath = inventoryPath.substr(strlen(INVENTORY_OBJ_PATH));
185 auto const lastSlashPos = invpath.find_last_of('/');
186 std::string prettyName = invpath.substr(lastSlashPos + 1);
187 setPresence(bus, invpath, present, prettyName);
188 updateInventory();
189 }
190}
191
Brandon Wymanc2203432021-12-21 23:09:48 +0000192void PowerSupply::analyzeCMLFault()
193{
194 if (statusWord & phosphor::pmbus::status_word::CML_FAULT)
195 {
196 if (!cmlFault)
197 {
198 log<level::ERR>(fmt::format("CML fault: STATUS_WORD = {:#04x}, "
199 "STATUS_CML = {:#02x}",
200 statusWord, statusCML)
201 .c_str());
202 }
203
204 cmlFault = true;
205 }
206}
207
Brandon Wymane3b0bb02021-12-21 23:16:48 +0000208void PowerSupply::analyzeInputFault()
209{
210 if (statusWord & phosphor::pmbus::status_word::INPUT_FAULT_WARN)
211 {
212 if (!inputFault)
213 {
214 log<level::ERR>(fmt::format("INPUT fault: STATUS_WORD = {:#04x}, "
215 "STATUS_MFR_SPECIFIC = {:#02x}, "
216 "STATUS_INPUT = {:#02x}",
217 statusWord, statusMFR, statusInput)
218 .c_str());
219 }
220
221 inputFault = true;
222 }
223}
224
Brandon Wymanc2c87132021-12-21 23:22:18 +0000225void PowerSupply::analyzeVoutOVFault()
226{
227 if (statusWord & phosphor::pmbus::status_word::VOUT_OV_FAULT)
228 {
229 if (!voutOVFault)
230 {
231 log<level::ERR>(
232 fmt::format("VOUT_OV_FAULT fault: STATUS_WORD = {:#04x}, "
233 "STATUS_MFR_SPECIFIC = {:#02x}, "
234 "STATUS_VOUT = {:#02x}",
235 statusWord, statusMFR, statusVout)
236 .c_str());
237 }
238
239 voutOVFault = true;
240 }
241}
242
Brandon Wymana00e7302021-12-21 23:28:29 +0000243void PowerSupply::analyzeIoutOCFault()
244{
245 if (statusWord & phosphor::pmbus::status_word::IOUT_OC_FAULT)
246 {
247 if (!ioutOCFault)
248 {
249 log<level::ERR>(fmt::format("IOUT fault: STATUS_WORD = {:#04x}, "
250 "STATUS_MFR_SPECIFIC = {:#02x}, "
251 "STATUS_IOUT = {:#02x}",
252 statusWord, statusMFR, statusIout)
253 .c_str());
254 }
255
256 ioutOCFault = true;
257 }
258}
259
Brandon Wyman52cb3f22021-12-21 23:02:47 +0000260void PowerSupply::analyzeTemperatureFault()
261{
262 if (statusWord & phosphor::pmbus::status_word::TEMPERATURE_FAULT_WARN)
263 {
264 if (!tempFault)
265 {
266 log<level::ERR>(fmt::format("TEMPERATURE fault/warning: "
267 "STATUS_WORD = {:#04x}, "
268 "STATUS_MFR_SPECIFIC = {:#02x}, "
269 "STATUS_TEMPERATURE = {:#02x}",
270 statusWord, statusMFR,
271 statusTemperature)
272 .c_str());
273 }
274
275 tempFault = true;
276 }
277}
278
Brandon Wyman993b5542021-12-21 22:55:16 +0000279void PowerSupply::analyzePgoodFault()
280{
281 if ((statusWord & phosphor::pmbus::status_word::POWER_GOOD_NEGATED) ||
282 (statusWord & phosphor::pmbus::status_word::UNIT_IS_OFF))
283 {
284 if (pgoodFault < DEGLITCH_LIMIT)
285 {
286 log<level::ERR>(fmt::format("PGOOD fault: "
287 "STATUS_WORD = {:#04x}, "
288 "STATUS_MFR_SPECIFIC = {:#02x}",
289 statusWord, statusMFR)
290 .c_str());
291
292 pgoodFault++;
293 }
294 }
295 else
296 {
297 pgoodFault = 0;
298 }
299}
300
Brandon Wyman39ea02b2021-11-23 23:22:23 +0000301void PowerSupply::determineMFRFault()
302{
303 if (bindPath.string().find("ibm-cffps") != std::string::npos)
304 {
305 // IBM MFR_SPECIFIC[4] is PS_Kill fault
306 if (statusMFR & 0x10)
307 {
308 psKillFault = true;
309 }
310 // IBM MFR_SPECIFIC[6] is 12Vcs fault.
311 if (statusMFR & 0x40)
312 {
313 ps12VcsFault = true;
314 }
315 // IBM MFR_SPECIFIC[7] is 12V Current-Share fault.
316 if (statusMFR & 0x80)
317 {
318 psCS12VFault = true;
319 }
320 }
321}
322
Brandon Wyman6c2ac392021-12-21 22:23:06 +0000323void PowerSupply::analyzeMFRFault()
324{
325 if (statusWord & phosphor::pmbus::status_word::MFR_SPECIFIC_FAULT)
326 {
327 if (!mfrFault)
328 {
329 log<level::ERR>(fmt::format("MFR fault: "
330 "STATUS_WORD = {:#04x} "
331 "STATUS_MFR_SPECIFIC = {:#02x}",
332 statusWord, statusMFR)
333 .c_str());
334 }
335
336 mfrFault = true;
337 determineMFRFault();
338 }
339}
340
Brandon Wyman3f1242f2020-01-28 13:11:25 -0600341void PowerSupply::analyze()
342{
343 using namespace phosphor::pmbus;
344
B. J. Wyman681b2a32021-04-20 22:31:22 +0000345 if (presenceGPIO)
346 {
347 updatePresenceGPIO();
348 }
349
Brandon Wymanf65c4062020-08-19 13:15:53 -0500350 if ((present) && (readFail < LOG_LIMIT))
Brandon Wyman3f1242f2020-01-28 13:11:25 -0600351 {
352 try
353 {
Brandon Wymanfed0ba22020-09-26 20:02:51 -0500354 statusWord = pmbusIntf->read(STATUS_WORD, Type::Debug);
Brandon Wymanf65c4062020-08-19 13:15:53 -0500355 // Read worked, reset the fail count.
356 readFail = 0;
Brandon Wyman3f1242f2020-01-28 13:11:25 -0600357
358 if (statusWord)
359 {
Brandon Wymanf07bc792021-10-12 19:00:35 +0000360 statusInput = pmbusIntf->read(STATUS_INPUT, Type::Debug);
Jay Meyer10d94052020-11-30 14:41:21 -0600361 statusMFR = pmbusIntf->read(STATUS_MFR, Type::Debug);
Brandon Wyman85c7bf42021-10-19 22:28:48 +0000362 statusCML = pmbusIntf->read(STATUS_CML, Type::Debug);
Brandon Wyman6710ba22021-10-27 17:39:31 +0000363 auto status0Vout = pmbusIntf->insertPageNum(STATUS_VOUT, 0);
364 statusVout = pmbusIntf->read(status0Vout, Type::Debug);
Brandon Wymanb10b3be2021-11-09 22:12:15 +0000365 statusIout = pmbusIntf->read(STATUS_IOUT, Type::Debug);
Brandon Wyman7ee4d7e2021-11-19 20:48:23 +0000366 statusFans12 = pmbusIntf->read(STATUS_FANS_1_2, Type::Debug);
Brandon Wyman96893a42021-11-05 19:56:57 +0000367 statusTemperature =
368 pmbusIntf->read(STATUS_TEMPERATURE, Type::Debug);
Brandon Wyman9ddc6222021-10-28 17:28:01 +0000369
Brandon Wymanc2203432021-12-21 23:09:48 +0000370 analyzeCMLFault();
Brandon Wyman85c7bf42021-10-19 22:28:48 +0000371
Brandon Wymane3b0bb02021-12-21 23:16:48 +0000372 analyzeInputFault();
Brandon Wyman3f1242f2020-01-28 13:11:25 -0600373
Brandon Wymanc2c87132021-12-21 23:22:18 +0000374 analyzeVoutOVFault();
Brandon Wyman6710ba22021-10-27 17:39:31 +0000375
Brandon Wymana00e7302021-12-21 23:28:29 +0000376 analyzeIoutOCFault();
Brandon Wymanb10b3be2021-11-09 22:12:15 +0000377
Brandon Wyman2cf46942021-10-28 19:09:16 +0000378 if ((statusWord & status_word::VOUT_FAULT) &&
379 !(statusWord & status_word::VOUT_OV_FAULT))
380 {
381 if (!voutUVFault)
382 {
383 log<level::ERR>(
384 fmt::format(
385 "VOUT_UV_FAULT fault: STATUS_WORD = {:#04x}, "
386 "STATUS_MFR_SPECIFIC = {:#02x}, "
387 "STATUS_VOUT = {:#02x}",
388 statusWord, statusMFR, statusVout)
389 .c_str());
390 }
391
392 voutUVFault = true;
393 }
394
Brandon Wyman7ee4d7e2021-11-19 20:48:23 +0000395 if (statusWord & status_word::FAN_FAULT)
396 {
397 if (!fanFault)
398 {
399 log<level::ERR>(
400 fmt::format("FANS fault/warning: "
401 "STATUS_WORD = {:#04x}, "
402 "STATUS_MFR_SPECIFIC = {:#02x}, "
403 "STATUS_FANS_1_2 = {:#02x}",
404 statusWord, statusMFR, statusFans12)
405 .c_str());
406 }
407
408 fanFault = true;
409 }
410
Brandon Wyman52cb3f22021-12-21 23:02:47 +0000411 analyzeTemperatureFault();
Brandon Wyman96893a42021-11-05 19:56:57 +0000412
Brandon Wyman993b5542021-12-21 22:55:16 +0000413 analyzePgoodFault();
Brandon Wyman2916ea52021-11-06 03:31:18 +0000414
Brandon Wyman6c2ac392021-12-21 22:23:06 +0000415 analyzeMFRFault();
Brandon Wyman3f1242f2020-01-28 13:11:25 -0600416
417 if (statusWord & status_word::VIN_UV_FAULT)
418 {
419 if (!vinUVFault)
420 {
Brandon Wyman43d32632021-10-26 23:44:11 +0000421 log<level::ERR>(
Brandon Wymanf07bc792021-10-12 19:00:35 +0000422 fmt::format("VIN_UV fault: STATUS_WORD = {:#04x}, "
423 "STATUS_MFR_SPECIFIC = {:#02x}, "
424 "STATUS_INPUT = {:#02x}",
425 statusWord, statusMFR, statusInput)
Brandon Wymanc8996602021-10-12 19:28:56 +0000426 .c_str());
Brandon Wyman3f1242f2020-01-28 13:11:25 -0600427 }
428
Brandon Wyman3f1242f2020-01-28 13:11:25 -0600429 vinUVFault = true;
430 }
431 }
432 else
433 {
Brandon Wymane3f7ad22021-12-21 20:27:45 +0000434 // if INPUT/VIN_UV fault was on, it cleared, trace it.
435 if (inputFault)
436 {
437 log<level::INFO>(
438 fmt::format(
439 "INPUT fault cleared: STATUS_WORD = {:#04x}",
440 statusWord)
441 .c_str());
442 }
443
444 if (vinUVFault)
445 {
446 log<level::INFO>(
447 fmt::format("VIN_UV cleared: STATUS_WORD = {:#04x}",
448 statusWord)
449 .c_str());
450 }
451
Brandon Wyman06ca4592021-12-06 22:52:23 +0000452 if (pgoodFault > 0)
Brandon Wyman4aecc292021-11-10 22:40:41 +0000453 {
454 log<level::INFO>(fmt::format("pgoodFault cleared path: {}",
455 inventoryPath)
456 .c_str());
Brandon Wyman4aecc292021-11-10 22:40:41 +0000457 }
Brandon Wymane3f7ad22021-12-21 20:27:45 +0000458
459 clearFaultFlags();
Brandon Wyman3f1242f2020-01-28 13:11:25 -0600460 }
461 }
Patrick Williamsc1d4de52021-10-06 12:45:57 -0500462 catch (const ReadFailure& e)
Brandon Wyman3f1242f2020-01-28 13:11:25 -0600463 {
Brandon Wymanf65c4062020-08-19 13:15:53 -0500464 readFail++;
Brandon Wyman3f1242f2020-01-28 13:11:25 -0600465 phosphor::logging::commit<ReadFailure>();
466 }
467 }
468}
469
Brandon Wyman59a35792020-06-04 12:37:40 -0500470void PowerSupply::onOffConfig(uint8_t data)
471{
472 using namespace phosphor::pmbus;
473
474 if (present)
475 {
476 log<level::INFO>("ON_OFF_CONFIG write", entry("DATA=0x%02X", data));
477 try
478 {
479 std::vector<uint8_t> configData{data};
480 pmbusIntf->writeBinary(ON_OFF_CONFIG, configData,
481 Type::HwmonDeviceDebug);
482 }
483 catch (...)
484 {
485 // The underlying code in writeBinary will log a message to the
B. J. Wyman681b2a32021-04-20 22:31:22 +0000486 // journal if the write fails. If the ON_OFF_CONFIG is not setup
487 // as desired, later fault detection and analysis code should
488 // catch any of the fall out. We should not need to terminate
489 // the application if this write fails.
Brandon Wyman59a35792020-06-04 12:37:40 -0500490 }
491 }
492}
493
Brandon Wyman3c208462020-05-13 16:25:58 -0500494void PowerSupply::clearFaults()
495{
Brandon Wyman5474c912021-02-23 14:39:43 -0600496 faultLogged = false;
Brandon Wyman3c208462020-05-13 16:25:58 -0500497 // The PMBus device driver does not allow for writing CLEAR_FAULTS
498 // directly. However, the pmbus hwmon device driver code will send a
499 // CLEAR_FAULTS after reading from any of the hwmon "files" in sysfs, so
500 // reading in1_input should result in clearing the fault bits in
501 // STATUS_BYTE/STATUS_WORD.
502 // I do not care what the return value is.
Brandon Wyman11151532020-11-10 13:45:57 -0600503 if (present)
Brandon Wyman3c208462020-05-13 16:25:58 -0500504 {
Brandon Wymane3f7ad22021-12-21 20:27:45 +0000505 clearFaultFlags();
Brandon Wyman9564e942020-11-10 14:01:42 -0600506 readFail = 0;
Brandon Wyman9564e942020-11-10 14:01:42 -0600507
Brandon Wyman11151532020-11-10 13:45:57 -0600508 try
509 {
510 static_cast<void>(
511 pmbusIntf->read("in1_input", phosphor::pmbus::Type::Hwmon));
512 }
Patrick Williamsc1d4de52021-10-06 12:45:57 -0500513 catch (const ReadFailure& e)
Brandon Wyman11151532020-11-10 13:45:57 -0600514 {
515 // Since I do not care what the return value is, I really do not
B. J. Wyman681b2a32021-04-20 22:31:22 +0000516 // care much if it gets a ReadFailure either. However, this
517 // should not prevent the application from continuing to run, so
518 // catching the read failure.
Brandon Wyman11151532020-11-10 13:45:57 -0600519 }
Brandon Wyman3c208462020-05-13 16:25:58 -0500520 }
521}
522
Brandon Wymanaed1f752019-11-25 18:10:52 -0600523void PowerSupply::inventoryChanged(sdbusplus::message::message& msg)
524{
525 std::string msgSensor;
Patrick Williamsabe49412020-05-13 17:59:47 -0500526 std::map<std::string, std::variant<uint32_t, bool>> msgData;
Brandon Wymanaed1f752019-11-25 18:10:52 -0600527 msg.read(msgSensor, msgData);
528
529 // Check if it was the Present property that changed.
530 auto valPropMap = msgData.find(PRESENT_PROP);
531 if (valPropMap != msgData.end())
532 {
533 if (std::get<bool>(valPropMap->second))
534 {
535 present = true;
B. J. Wyman681b2a32021-04-20 22:31:22 +0000536 // TODO: Immediately trying to read or write the "files" causes
537 // read or write failures.
Brandon Wyman1d7a7df2020-03-26 10:14:05 -0500538 using namespace std::chrono_literals;
539 std::this_thread::sleep_for(20ms);
Brandon Wyman9564e942020-11-10 14:01:42 -0600540 pmbusIntf->findHwmonDir();
Brandon Wyman59a35792020-06-04 12:37:40 -0500541 onOffConfig(phosphor::pmbus::ON_OFF_CONFIG_CONTROL_PIN_ONLY);
Brandon Wymanaed1f752019-11-25 18:10:52 -0600542 clearFaults();
Brandon Wyman1d7a7df2020-03-26 10:14:05 -0500543 updateInventory();
Brandon Wymanaed1f752019-11-25 18:10:52 -0600544 }
545 else
546 {
547 present = false;
548
549 // Clear out the now outdated inventory properties
550 updateInventory();
551 }
552 }
553}
554
Brandon Wyman9a507db2021-02-25 16:15:22 -0600555void PowerSupply::inventoryAdded(sdbusplus::message::message& msg)
556{
557 sdbusplus::message::object_path path;
558 msg.read(path);
559 // Make sure the signal is for the PSU inventory path
560 if (path == inventoryPath)
561 {
562 std::map<std::string, std::map<std::string, std::variant<bool>>>
563 interfaces;
564 // Get map of interfaces and their properties
565 msg.read(interfaces);
566
567 auto properties = interfaces.find(INVENTORY_IFACE);
568 if (properties != interfaces.end())
569 {
570 auto property = properties->second.find(PRESENT_PROP);
571 if (property != properties->second.end())
572 {
573 present = std::get<bool>(property->second);
574
575 log<level::INFO>(fmt::format("Power Supply {} Present {}",
576 inventoryPath, present)
577 .c_str());
578
579 updateInventory();
580 }
581 }
582 }
583}
584
Brandon Wyman1d7a7df2020-03-26 10:14:05 -0500585void PowerSupply::updateInventory()
586{
587 using namespace phosphor::pmbus;
588
Chanh Nguyenc12c53b2021-04-06 17:24:47 +0700589#if IBM_VPD
Brandon Wyman1d7a7df2020-03-26 10:14:05 -0500590 std::string ccin;
591 std::string pn;
592 std::string fn;
593 std::string header;
594 std::string sn;
Brandon Wyman1d7a7df2020-03-26 10:14:05 -0500595 using PropertyMap =
George Liu070c1bc2020-10-12 11:28:01 +0800596 std::map<std::string,
597 std::variant<std::string, std::vector<uint8_t>, bool>>;
Brandon Wyman1d7a7df2020-03-26 10:14:05 -0500598 PropertyMap assetProps;
George Liu070c1bc2020-10-12 11:28:01 +0800599 PropertyMap operProps;
Brandon Wyman1d7a7df2020-03-26 10:14:05 -0500600 PropertyMap versionProps;
601 PropertyMap ipzvpdDINFProps;
602 PropertyMap ipzvpdVINIProps;
603 using InterfaceMap = std::map<std::string, PropertyMap>;
604 InterfaceMap interfaces;
605 using ObjectMap = std::map<sdbusplus::message::object_path, InterfaceMap>;
606 ObjectMap object;
607#endif
B. J. Wyman681b2a32021-04-20 22:31:22 +0000608 log<level::DEBUG>(
609 fmt::format("updateInventory() inventoryPath: {}", inventoryPath)
610 .c_str());
Brandon Wyman1d7a7df2020-03-26 10:14:05 -0500611
612 if (present)
613 {
614 // TODO: non-IBM inventory updates?
615
Chanh Nguyenc12c53b2021-04-06 17:24:47 +0700616#if IBM_VPD
Brandon Wyman1d7a7df2020-03-26 10:14:05 -0500617 try
618 {
619 ccin = pmbusIntf->readString(CCIN, Type::HwmonDeviceDebug);
620 assetProps.emplace(MODEL_PROP, ccin);
Adriana Kobylak572a9052021-03-30 15:58:07 +0000621 modelName = ccin;
Brandon Wyman1d7a7df2020-03-26 10:14:05 -0500622 }
Patrick Williamsc1d4de52021-10-06 12:45:57 -0500623 catch (const ReadFailure& e)
Brandon Wyman1d7a7df2020-03-26 10:14:05 -0500624 {
B. J. Wyman681b2a32021-04-20 22:31:22 +0000625 // Ignore the read failure, let pmbus code indicate failure,
626 // path...
Brandon Wyman1d7a7df2020-03-26 10:14:05 -0500627 // TODO - ibm918
628 // https://github.com/openbmc/docs/blob/master/designs/vpd-collection.md
629 // The BMC must log errors if any of the VPD cannot be properly
630 // parsed or fails ECC checks.
631 }
632
633 try
634 {
635 pn = pmbusIntf->readString(PART_NUMBER, Type::HwmonDeviceDebug);
636 assetProps.emplace(PN_PROP, pn);
637 }
Patrick Williamsc1d4de52021-10-06 12:45:57 -0500638 catch (const ReadFailure& e)
Brandon Wyman1d7a7df2020-03-26 10:14:05 -0500639 {
B. J. Wyman681b2a32021-04-20 22:31:22 +0000640 // Ignore the read failure, let pmbus code indicate failure,
641 // path...
Brandon Wyman1d7a7df2020-03-26 10:14:05 -0500642 }
643
644 try
645 {
646 fn = pmbusIntf->readString(FRU_NUMBER, Type::HwmonDeviceDebug);
Brandon Wymana169b0f2021-12-07 20:18:06 +0000647 assetProps.emplace(SPARE_PN_PROP, fn);
Brandon Wyman1d7a7df2020-03-26 10:14:05 -0500648 }
Patrick Williamsc1d4de52021-10-06 12:45:57 -0500649 catch (const ReadFailure& e)
Brandon Wyman1d7a7df2020-03-26 10:14:05 -0500650 {
B. J. Wyman681b2a32021-04-20 22:31:22 +0000651 // Ignore the read failure, let pmbus code indicate failure,
652 // path...
Brandon Wyman1d7a7df2020-03-26 10:14:05 -0500653 }
654
655 try
656 {
657 header =
658 pmbusIntf->readString(SERIAL_HEADER, Type::HwmonDeviceDebug);
659 sn = pmbusIntf->readString(SERIAL_NUMBER, Type::HwmonDeviceDebug);
660 assetProps.emplace(SN_PROP, sn);
661 }
Patrick Williamsc1d4de52021-10-06 12:45:57 -0500662 catch (const ReadFailure& e)
Brandon Wyman1d7a7df2020-03-26 10:14:05 -0500663 {
B. J. Wyman681b2a32021-04-20 22:31:22 +0000664 // Ignore the read failure, let pmbus code indicate failure,
665 // path...
Brandon Wyman1d7a7df2020-03-26 10:14:05 -0500666 }
667
668 try
669 {
Brandon Wymanc9efe412020-10-09 15:42:50 -0500670 fwVersion =
671 pmbusIntf->readString(FW_VERSION, Type::HwmonDeviceDebug);
672 versionProps.emplace(VERSION_PROP, fwVersion);
Brandon Wyman1d7a7df2020-03-26 10:14:05 -0500673 }
Patrick Williamsc1d4de52021-10-06 12:45:57 -0500674 catch (const ReadFailure& e)
Brandon Wyman1d7a7df2020-03-26 10:14:05 -0500675 {
B. J. Wyman681b2a32021-04-20 22:31:22 +0000676 // Ignore the read failure, let pmbus code indicate failure,
677 // path...
Brandon Wyman1d7a7df2020-03-26 10:14:05 -0500678 }
679
680 ipzvpdVINIProps.emplace("CC",
681 std::vector<uint8_t>(ccin.begin(), ccin.end()));
682 ipzvpdVINIProps.emplace("PN",
683 std::vector<uint8_t>(pn.begin(), pn.end()));
684 ipzvpdVINIProps.emplace("FN",
685 std::vector<uint8_t>(fn.begin(), fn.end()));
686 std::string header_sn = header + sn + '\0';
687 ipzvpdVINIProps.emplace(
688 "SN", std::vector<uint8_t>(header_sn.begin(), header_sn.end()));
689 std::string description = "IBM PS";
690 ipzvpdVINIProps.emplace(
691 "DR", std::vector<uint8_t>(description.begin(), description.end()));
692
693 // Update the Resource Identifier (RI) keyword
694 // 2 byte FRC: 0x0003
695 // 2 byte RID: 0x1000, 0x1001...
696 std::uint8_t num = std::stoul(
697 inventoryPath.substr(inventoryPath.size() - 1, 1), nullptr, 0);
698 std::vector<uint8_t> ri{0x00, 0x03, 0x10, num};
699 ipzvpdDINFProps.emplace("RI", ri);
700
701 // Fill in the FRU Label (FL) keyword.
702 std::string fl = "E";
703 fl.push_back(inventoryPath.back());
704 fl.resize(FL_KW_SIZE, ' ');
705 ipzvpdDINFProps.emplace("FL",
706 std::vector<uint8_t>(fl.begin(), fl.end()));
707
708 interfaces.emplace(ASSET_IFACE, std::move(assetProps));
709 interfaces.emplace(VERSION_IFACE, std::move(versionProps));
710 interfaces.emplace(DINF_IFACE, std::move(ipzvpdDINFProps));
711 interfaces.emplace(VINI_IFACE, std::move(ipzvpdVINIProps));
712
George Liu070c1bc2020-10-12 11:28:01 +0800713 // Update the Functional
714 operProps.emplace(FUNCTIONAL_PROP, present);
715 interfaces.emplace(OPERATIONAL_STATE_IFACE, std::move(operProps));
716
Brandon Wyman1d7a7df2020-03-26 10:14:05 -0500717 auto path = inventoryPath.substr(strlen(INVENTORY_OBJ_PATH));
718 object.emplace(path, std::move(interfaces));
719
720 try
721 {
722 auto service =
723 util::getService(INVENTORY_OBJ_PATH, INVENTORY_MGR_IFACE, bus);
724
725 if (service.empty())
726 {
727 log<level::ERR>("Unable to get inventory manager service");
728 return;
729 }
730
731 auto method =
732 bus.new_method_call(service.c_str(), INVENTORY_OBJ_PATH,
733 INVENTORY_MGR_IFACE, "Notify");
734
735 method.append(std::move(object));
736
737 auto reply = bus.call(method);
738 }
Patrick Williamsc1d4de52021-10-06 12:45:57 -0500739 catch (const std::exception& e)
Brandon Wyman1d7a7df2020-03-26 10:14:05 -0500740 {
Jay Meyer6a3fd2c2020-08-25 16:37:16 -0500741 log<level::ERR>(
742 std::string(e.what() + std::string(" PATH=") + inventoryPath)
743 .c_str());
Brandon Wyman1d7a7df2020-03-26 10:14:05 -0500744 }
745#endif
746 }
747}
748
Adriana Kobylak4175ffb2021-08-02 14:51:05 +0000749void PowerSupply::getInputVoltage(double& actualInputVoltage,
750 int& inputVoltage) const
751{
752 using namespace phosphor::pmbus;
753
754 actualInputVoltage = in_input::VIN_VOLTAGE_0;
755 inputVoltage = in_input::VIN_VOLTAGE_0;
756
757 if (present)
758 {
759 try
760 {
761 // Read input voltage in millivolts
762 auto inputVoltageStr = pmbusIntf->readString(READ_VIN, Type::Hwmon);
763
764 // Convert to volts
765 actualInputVoltage = std::stod(inputVoltageStr) / 1000;
766
767 // Calculate the voltage based on voltage thresholds
768 if (actualInputVoltage < in_input::VIN_VOLTAGE_MIN)
769 {
770 inputVoltage = in_input::VIN_VOLTAGE_0;
771 }
772 else if (actualInputVoltage < in_input::VIN_VOLTAGE_110_THRESHOLD)
773 {
774 inputVoltage = in_input::VIN_VOLTAGE_110;
775 }
776 else
777 {
778 inputVoltage = in_input::VIN_VOLTAGE_220;
779 }
780 }
781 catch (const std::exception& e)
782 {
783 log<level::ERR>(
784 fmt::format("READ_VIN read error: {}", e.what()).c_str());
785 }
786 }
787}
788
Brandon Wyman3f1242f2020-01-28 13:11:25 -0600789} // namespace phosphor::power::psu