blob: e31734b5941f830ec2cea6fc796e9807e1f6e949 [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 Wyman52cb3f22021-12-21 23:02:47 +0000243void PowerSupply::analyzeTemperatureFault()
244{
245 if (statusWord & phosphor::pmbus::status_word::TEMPERATURE_FAULT_WARN)
246 {
247 if (!tempFault)
248 {
249 log<level::ERR>(fmt::format("TEMPERATURE fault/warning: "
250 "STATUS_WORD = {:#04x}, "
251 "STATUS_MFR_SPECIFIC = {:#02x}, "
252 "STATUS_TEMPERATURE = {:#02x}",
253 statusWord, statusMFR,
254 statusTemperature)
255 .c_str());
256 }
257
258 tempFault = true;
259 }
260}
261
Brandon Wyman993b5542021-12-21 22:55:16 +0000262void PowerSupply::analyzePgoodFault()
263{
264 if ((statusWord & phosphor::pmbus::status_word::POWER_GOOD_NEGATED) ||
265 (statusWord & phosphor::pmbus::status_word::UNIT_IS_OFF))
266 {
267 if (pgoodFault < DEGLITCH_LIMIT)
268 {
269 log<level::ERR>(fmt::format("PGOOD fault: "
270 "STATUS_WORD = {:#04x}, "
271 "STATUS_MFR_SPECIFIC = {:#02x}",
272 statusWord, statusMFR)
273 .c_str());
274
275 pgoodFault++;
276 }
277 }
278 else
279 {
280 pgoodFault = 0;
281 }
282}
283
Brandon Wyman39ea02b2021-11-23 23:22:23 +0000284void PowerSupply::determineMFRFault()
285{
286 if (bindPath.string().find("ibm-cffps") != std::string::npos)
287 {
288 // IBM MFR_SPECIFIC[4] is PS_Kill fault
289 if (statusMFR & 0x10)
290 {
291 psKillFault = true;
292 }
293 // IBM MFR_SPECIFIC[6] is 12Vcs fault.
294 if (statusMFR & 0x40)
295 {
296 ps12VcsFault = true;
297 }
298 // IBM MFR_SPECIFIC[7] is 12V Current-Share fault.
299 if (statusMFR & 0x80)
300 {
301 psCS12VFault = true;
302 }
303 }
304}
305
Brandon Wyman6c2ac392021-12-21 22:23:06 +0000306void PowerSupply::analyzeMFRFault()
307{
308 if (statusWord & phosphor::pmbus::status_word::MFR_SPECIFIC_FAULT)
309 {
310 if (!mfrFault)
311 {
312 log<level::ERR>(fmt::format("MFR fault: "
313 "STATUS_WORD = {:#04x} "
314 "STATUS_MFR_SPECIFIC = {:#02x}",
315 statusWord, statusMFR)
316 .c_str());
317 }
318
319 mfrFault = true;
320 determineMFRFault();
321 }
322}
323
Brandon Wyman3f1242f2020-01-28 13:11:25 -0600324void PowerSupply::analyze()
325{
326 using namespace phosphor::pmbus;
327
B. J. Wyman681b2a32021-04-20 22:31:22 +0000328 if (presenceGPIO)
329 {
330 updatePresenceGPIO();
331 }
332
Brandon Wymanf65c4062020-08-19 13:15:53 -0500333 if ((present) && (readFail < LOG_LIMIT))
Brandon Wyman3f1242f2020-01-28 13:11:25 -0600334 {
335 try
336 {
Brandon Wymanfed0ba22020-09-26 20:02:51 -0500337 statusWord = pmbusIntf->read(STATUS_WORD, Type::Debug);
Brandon Wymanf65c4062020-08-19 13:15:53 -0500338 // Read worked, reset the fail count.
339 readFail = 0;
Brandon Wyman3f1242f2020-01-28 13:11:25 -0600340
341 if (statusWord)
342 {
Brandon Wymanf07bc792021-10-12 19:00:35 +0000343 statusInput = pmbusIntf->read(STATUS_INPUT, Type::Debug);
Jay Meyer10d94052020-11-30 14:41:21 -0600344 statusMFR = pmbusIntf->read(STATUS_MFR, Type::Debug);
Brandon Wyman85c7bf42021-10-19 22:28:48 +0000345 statusCML = pmbusIntf->read(STATUS_CML, Type::Debug);
Brandon Wyman6710ba22021-10-27 17:39:31 +0000346 auto status0Vout = pmbusIntf->insertPageNum(STATUS_VOUT, 0);
347 statusVout = pmbusIntf->read(status0Vout, Type::Debug);
Brandon Wymanb10b3be2021-11-09 22:12:15 +0000348 statusIout = pmbusIntf->read(STATUS_IOUT, Type::Debug);
Brandon Wyman7ee4d7e2021-11-19 20:48:23 +0000349 statusFans12 = pmbusIntf->read(STATUS_FANS_1_2, Type::Debug);
Brandon Wyman96893a42021-11-05 19:56:57 +0000350 statusTemperature =
351 pmbusIntf->read(STATUS_TEMPERATURE, Type::Debug);
Brandon Wyman9ddc6222021-10-28 17:28:01 +0000352
Brandon Wymanc2203432021-12-21 23:09:48 +0000353 analyzeCMLFault();
Brandon Wyman85c7bf42021-10-19 22:28:48 +0000354
Brandon Wymane3b0bb02021-12-21 23:16:48 +0000355 analyzeInputFault();
Brandon Wyman3f1242f2020-01-28 13:11:25 -0600356
Brandon Wymanc2c87132021-12-21 23:22:18 +0000357 analyzeVoutOVFault();
Brandon Wyman6710ba22021-10-27 17:39:31 +0000358
Brandon Wymanb10b3be2021-11-09 22:12:15 +0000359 if (statusWord & status_word::IOUT_OC_FAULT)
360 {
361 if (!ioutOCFault)
362 {
363 log<level::ERR>(
364 fmt::format("IOUT fault: STATUS_WORD = {:#04x}, "
365 "STATUS_MFR_SPECIFIC = {:#02x}, "
366 "STATUS_IOUT = {:#02x}",
367 statusWord, statusMFR, statusIout)
368 .c_str());
369 }
370
371 ioutOCFault = true;
372 }
373
Brandon Wyman2cf46942021-10-28 19:09:16 +0000374 if ((statusWord & status_word::VOUT_FAULT) &&
375 !(statusWord & status_word::VOUT_OV_FAULT))
376 {
377 if (!voutUVFault)
378 {
379 log<level::ERR>(
380 fmt::format(
381 "VOUT_UV_FAULT fault: STATUS_WORD = {:#04x}, "
382 "STATUS_MFR_SPECIFIC = {:#02x}, "
383 "STATUS_VOUT = {:#02x}",
384 statusWord, statusMFR, statusVout)
385 .c_str());
386 }
387
388 voutUVFault = true;
389 }
390
Brandon Wyman7ee4d7e2021-11-19 20:48:23 +0000391 if (statusWord & status_word::FAN_FAULT)
392 {
393 if (!fanFault)
394 {
395 log<level::ERR>(
396 fmt::format("FANS fault/warning: "
397 "STATUS_WORD = {:#04x}, "
398 "STATUS_MFR_SPECIFIC = {:#02x}, "
399 "STATUS_FANS_1_2 = {:#02x}",
400 statusWord, statusMFR, statusFans12)
401 .c_str());
402 }
403
404 fanFault = true;
405 }
406
Brandon Wyman52cb3f22021-12-21 23:02:47 +0000407 analyzeTemperatureFault();
Brandon Wyman96893a42021-11-05 19:56:57 +0000408
Brandon Wyman993b5542021-12-21 22:55:16 +0000409 analyzePgoodFault();
Brandon Wyman2916ea52021-11-06 03:31:18 +0000410
Brandon Wyman6c2ac392021-12-21 22:23:06 +0000411 analyzeMFRFault();
Brandon Wyman3f1242f2020-01-28 13:11:25 -0600412
413 if (statusWord & status_word::VIN_UV_FAULT)
414 {
415 if (!vinUVFault)
416 {
Brandon Wyman43d32632021-10-26 23:44:11 +0000417 log<level::ERR>(
Brandon Wymanf07bc792021-10-12 19:00:35 +0000418 fmt::format("VIN_UV fault: STATUS_WORD = {:#04x}, "
419 "STATUS_MFR_SPECIFIC = {:#02x}, "
420 "STATUS_INPUT = {:#02x}",
421 statusWord, statusMFR, statusInput)
Brandon Wymanc8996602021-10-12 19:28:56 +0000422 .c_str());
Brandon Wyman3f1242f2020-01-28 13:11:25 -0600423 }
424
Brandon Wyman3f1242f2020-01-28 13:11:25 -0600425 vinUVFault = true;
426 }
427 }
428 else
429 {
Brandon Wymane3f7ad22021-12-21 20:27:45 +0000430 // if INPUT/VIN_UV fault was on, it cleared, trace it.
431 if (inputFault)
432 {
433 log<level::INFO>(
434 fmt::format(
435 "INPUT fault cleared: STATUS_WORD = {:#04x}",
436 statusWord)
437 .c_str());
438 }
439
440 if (vinUVFault)
441 {
442 log<level::INFO>(
443 fmt::format("VIN_UV cleared: STATUS_WORD = {:#04x}",
444 statusWord)
445 .c_str());
446 }
447
Brandon Wyman06ca4592021-12-06 22:52:23 +0000448 if (pgoodFault > 0)
Brandon Wyman4aecc292021-11-10 22:40:41 +0000449 {
450 log<level::INFO>(fmt::format("pgoodFault cleared path: {}",
451 inventoryPath)
452 .c_str());
Brandon Wyman4aecc292021-11-10 22:40:41 +0000453 }
Brandon Wymane3f7ad22021-12-21 20:27:45 +0000454
455 clearFaultFlags();
Brandon Wyman3f1242f2020-01-28 13:11:25 -0600456 }
457 }
Patrick Williamsc1d4de52021-10-06 12:45:57 -0500458 catch (const ReadFailure& e)
Brandon Wyman3f1242f2020-01-28 13:11:25 -0600459 {
Brandon Wymanf65c4062020-08-19 13:15:53 -0500460 readFail++;
Brandon Wyman3f1242f2020-01-28 13:11:25 -0600461 phosphor::logging::commit<ReadFailure>();
462 }
463 }
464}
465
Brandon Wyman59a35792020-06-04 12:37:40 -0500466void PowerSupply::onOffConfig(uint8_t data)
467{
468 using namespace phosphor::pmbus;
469
470 if (present)
471 {
472 log<level::INFO>("ON_OFF_CONFIG write", entry("DATA=0x%02X", data));
473 try
474 {
475 std::vector<uint8_t> configData{data};
476 pmbusIntf->writeBinary(ON_OFF_CONFIG, configData,
477 Type::HwmonDeviceDebug);
478 }
479 catch (...)
480 {
481 // The underlying code in writeBinary will log a message to the
B. J. Wyman681b2a32021-04-20 22:31:22 +0000482 // journal if the write fails. If the ON_OFF_CONFIG is not setup
483 // as desired, later fault detection and analysis code should
484 // catch any of the fall out. We should not need to terminate
485 // the application if this write fails.
Brandon Wyman59a35792020-06-04 12:37:40 -0500486 }
487 }
488}
489
Brandon Wyman3c208462020-05-13 16:25:58 -0500490void PowerSupply::clearFaults()
491{
Brandon Wyman5474c912021-02-23 14:39:43 -0600492 faultLogged = false;
Brandon Wyman3c208462020-05-13 16:25:58 -0500493 // The PMBus device driver does not allow for writing CLEAR_FAULTS
494 // directly. However, the pmbus hwmon device driver code will send a
495 // CLEAR_FAULTS after reading from any of the hwmon "files" in sysfs, so
496 // reading in1_input should result in clearing the fault bits in
497 // STATUS_BYTE/STATUS_WORD.
498 // I do not care what the return value is.
Brandon Wyman11151532020-11-10 13:45:57 -0600499 if (present)
Brandon Wyman3c208462020-05-13 16:25:58 -0500500 {
Brandon Wymane3f7ad22021-12-21 20:27:45 +0000501 clearFaultFlags();
Brandon Wyman9564e942020-11-10 14:01:42 -0600502 readFail = 0;
Brandon Wyman9564e942020-11-10 14:01:42 -0600503
Brandon Wyman11151532020-11-10 13:45:57 -0600504 try
505 {
506 static_cast<void>(
507 pmbusIntf->read("in1_input", phosphor::pmbus::Type::Hwmon));
508 }
Patrick Williamsc1d4de52021-10-06 12:45:57 -0500509 catch (const ReadFailure& e)
Brandon Wyman11151532020-11-10 13:45:57 -0600510 {
511 // Since I do not care what the return value is, I really do not
B. J. Wyman681b2a32021-04-20 22:31:22 +0000512 // care much if it gets a ReadFailure either. However, this
513 // should not prevent the application from continuing to run, so
514 // catching the read failure.
Brandon Wyman11151532020-11-10 13:45:57 -0600515 }
Brandon Wyman3c208462020-05-13 16:25:58 -0500516 }
517}
518
Brandon Wymanaed1f752019-11-25 18:10:52 -0600519void PowerSupply::inventoryChanged(sdbusplus::message::message& msg)
520{
521 std::string msgSensor;
Patrick Williamsabe49412020-05-13 17:59:47 -0500522 std::map<std::string, std::variant<uint32_t, bool>> msgData;
Brandon Wymanaed1f752019-11-25 18:10:52 -0600523 msg.read(msgSensor, msgData);
524
525 // Check if it was the Present property that changed.
526 auto valPropMap = msgData.find(PRESENT_PROP);
527 if (valPropMap != msgData.end())
528 {
529 if (std::get<bool>(valPropMap->second))
530 {
531 present = true;
B. J. Wyman681b2a32021-04-20 22:31:22 +0000532 // TODO: Immediately trying to read or write the "files" causes
533 // read or write failures.
Brandon Wyman1d7a7df2020-03-26 10:14:05 -0500534 using namespace std::chrono_literals;
535 std::this_thread::sleep_for(20ms);
Brandon Wyman9564e942020-11-10 14:01:42 -0600536 pmbusIntf->findHwmonDir();
Brandon Wyman59a35792020-06-04 12:37:40 -0500537 onOffConfig(phosphor::pmbus::ON_OFF_CONFIG_CONTROL_PIN_ONLY);
Brandon Wymanaed1f752019-11-25 18:10:52 -0600538 clearFaults();
Brandon Wyman1d7a7df2020-03-26 10:14:05 -0500539 updateInventory();
Brandon Wymanaed1f752019-11-25 18:10:52 -0600540 }
541 else
542 {
543 present = false;
544
545 // Clear out the now outdated inventory properties
546 updateInventory();
547 }
548 }
549}
550
Brandon Wyman9a507db2021-02-25 16:15:22 -0600551void PowerSupply::inventoryAdded(sdbusplus::message::message& msg)
552{
553 sdbusplus::message::object_path path;
554 msg.read(path);
555 // Make sure the signal is for the PSU inventory path
556 if (path == inventoryPath)
557 {
558 std::map<std::string, std::map<std::string, std::variant<bool>>>
559 interfaces;
560 // Get map of interfaces and their properties
561 msg.read(interfaces);
562
563 auto properties = interfaces.find(INVENTORY_IFACE);
564 if (properties != interfaces.end())
565 {
566 auto property = properties->second.find(PRESENT_PROP);
567 if (property != properties->second.end())
568 {
569 present = std::get<bool>(property->second);
570
571 log<level::INFO>(fmt::format("Power Supply {} Present {}",
572 inventoryPath, present)
573 .c_str());
574
575 updateInventory();
576 }
577 }
578 }
579}
580
Brandon Wyman1d7a7df2020-03-26 10:14:05 -0500581void PowerSupply::updateInventory()
582{
583 using namespace phosphor::pmbus;
584
Chanh Nguyenc12c53b2021-04-06 17:24:47 +0700585#if IBM_VPD
Brandon Wyman1d7a7df2020-03-26 10:14:05 -0500586 std::string ccin;
587 std::string pn;
588 std::string fn;
589 std::string header;
590 std::string sn;
Brandon Wyman1d7a7df2020-03-26 10:14:05 -0500591 using PropertyMap =
George Liu070c1bc2020-10-12 11:28:01 +0800592 std::map<std::string,
593 std::variant<std::string, std::vector<uint8_t>, bool>>;
Brandon Wyman1d7a7df2020-03-26 10:14:05 -0500594 PropertyMap assetProps;
George Liu070c1bc2020-10-12 11:28:01 +0800595 PropertyMap operProps;
Brandon Wyman1d7a7df2020-03-26 10:14:05 -0500596 PropertyMap versionProps;
597 PropertyMap ipzvpdDINFProps;
598 PropertyMap ipzvpdVINIProps;
599 using InterfaceMap = std::map<std::string, PropertyMap>;
600 InterfaceMap interfaces;
601 using ObjectMap = std::map<sdbusplus::message::object_path, InterfaceMap>;
602 ObjectMap object;
603#endif
B. J. Wyman681b2a32021-04-20 22:31:22 +0000604 log<level::DEBUG>(
605 fmt::format("updateInventory() inventoryPath: {}", inventoryPath)
606 .c_str());
Brandon Wyman1d7a7df2020-03-26 10:14:05 -0500607
608 if (present)
609 {
610 // TODO: non-IBM inventory updates?
611
Chanh Nguyenc12c53b2021-04-06 17:24:47 +0700612#if IBM_VPD
Brandon Wyman1d7a7df2020-03-26 10:14:05 -0500613 try
614 {
615 ccin = pmbusIntf->readString(CCIN, Type::HwmonDeviceDebug);
616 assetProps.emplace(MODEL_PROP, ccin);
Adriana Kobylak572a9052021-03-30 15:58:07 +0000617 modelName = ccin;
Brandon Wyman1d7a7df2020-03-26 10:14:05 -0500618 }
Patrick Williamsc1d4de52021-10-06 12:45:57 -0500619 catch (const ReadFailure& e)
Brandon Wyman1d7a7df2020-03-26 10:14:05 -0500620 {
B. J. Wyman681b2a32021-04-20 22:31:22 +0000621 // Ignore the read failure, let pmbus code indicate failure,
622 // path...
Brandon Wyman1d7a7df2020-03-26 10:14:05 -0500623 // TODO - ibm918
624 // https://github.com/openbmc/docs/blob/master/designs/vpd-collection.md
625 // The BMC must log errors if any of the VPD cannot be properly
626 // parsed or fails ECC checks.
627 }
628
629 try
630 {
631 pn = pmbusIntf->readString(PART_NUMBER, Type::HwmonDeviceDebug);
632 assetProps.emplace(PN_PROP, pn);
633 }
Patrick Williamsc1d4de52021-10-06 12:45:57 -0500634 catch (const ReadFailure& e)
Brandon Wyman1d7a7df2020-03-26 10:14:05 -0500635 {
B. J. Wyman681b2a32021-04-20 22:31:22 +0000636 // Ignore the read failure, let pmbus code indicate failure,
637 // path...
Brandon Wyman1d7a7df2020-03-26 10:14:05 -0500638 }
639
640 try
641 {
642 fn = pmbusIntf->readString(FRU_NUMBER, Type::HwmonDeviceDebug);
Brandon Wymana169b0f2021-12-07 20:18:06 +0000643 assetProps.emplace(SPARE_PN_PROP, fn);
Brandon Wyman1d7a7df2020-03-26 10:14:05 -0500644 }
Patrick Williamsc1d4de52021-10-06 12:45:57 -0500645 catch (const ReadFailure& e)
Brandon Wyman1d7a7df2020-03-26 10:14:05 -0500646 {
B. J. Wyman681b2a32021-04-20 22:31:22 +0000647 // Ignore the read failure, let pmbus code indicate failure,
648 // path...
Brandon Wyman1d7a7df2020-03-26 10:14:05 -0500649 }
650
651 try
652 {
653 header =
654 pmbusIntf->readString(SERIAL_HEADER, Type::HwmonDeviceDebug);
655 sn = pmbusIntf->readString(SERIAL_NUMBER, Type::HwmonDeviceDebug);
656 assetProps.emplace(SN_PROP, sn);
657 }
Patrick Williamsc1d4de52021-10-06 12:45:57 -0500658 catch (const ReadFailure& e)
Brandon Wyman1d7a7df2020-03-26 10:14:05 -0500659 {
B. J. Wyman681b2a32021-04-20 22:31:22 +0000660 // Ignore the read failure, let pmbus code indicate failure,
661 // path...
Brandon Wyman1d7a7df2020-03-26 10:14:05 -0500662 }
663
664 try
665 {
Brandon Wymanc9efe412020-10-09 15:42:50 -0500666 fwVersion =
667 pmbusIntf->readString(FW_VERSION, Type::HwmonDeviceDebug);
668 versionProps.emplace(VERSION_PROP, fwVersion);
Brandon Wyman1d7a7df2020-03-26 10:14:05 -0500669 }
Patrick Williamsc1d4de52021-10-06 12:45:57 -0500670 catch (const ReadFailure& e)
Brandon Wyman1d7a7df2020-03-26 10:14:05 -0500671 {
B. J. Wyman681b2a32021-04-20 22:31:22 +0000672 // Ignore the read failure, let pmbus code indicate failure,
673 // path...
Brandon Wyman1d7a7df2020-03-26 10:14:05 -0500674 }
675
676 ipzvpdVINIProps.emplace("CC",
677 std::vector<uint8_t>(ccin.begin(), ccin.end()));
678 ipzvpdVINIProps.emplace("PN",
679 std::vector<uint8_t>(pn.begin(), pn.end()));
680 ipzvpdVINIProps.emplace("FN",
681 std::vector<uint8_t>(fn.begin(), fn.end()));
682 std::string header_sn = header + sn + '\0';
683 ipzvpdVINIProps.emplace(
684 "SN", std::vector<uint8_t>(header_sn.begin(), header_sn.end()));
685 std::string description = "IBM PS";
686 ipzvpdVINIProps.emplace(
687 "DR", std::vector<uint8_t>(description.begin(), description.end()));
688
689 // Update the Resource Identifier (RI) keyword
690 // 2 byte FRC: 0x0003
691 // 2 byte RID: 0x1000, 0x1001...
692 std::uint8_t num = std::stoul(
693 inventoryPath.substr(inventoryPath.size() - 1, 1), nullptr, 0);
694 std::vector<uint8_t> ri{0x00, 0x03, 0x10, num};
695 ipzvpdDINFProps.emplace("RI", ri);
696
697 // Fill in the FRU Label (FL) keyword.
698 std::string fl = "E";
699 fl.push_back(inventoryPath.back());
700 fl.resize(FL_KW_SIZE, ' ');
701 ipzvpdDINFProps.emplace("FL",
702 std::vector<uint8_t>(fl.begin(), fl.end()));
703
704 interfaces.emplace(ASSET_IFACE, std::move(assetProps));
705 interfaces.emplace(VERSION_IFACE, std::move(versionProps));
706 interfaces.emplace(DINF_IFACE, std::move(ipzvpdDINFProps));
707 interfaces.emplace(VINI_IFACE, std::move(ipzvpdVINIProps));
708
George Liu070c1bc2020-10-12 11:28:01 +0800709 // Update the Functional
710 operProps.emplace(FUNCTIONAL_PROP, present);
711 interfaces.emplace(OPERATIONAL_STATE_IFACE, std::move(operProps));
712
Brandon Wyman1d7a7df2020-03-26 10:14:05 -0500713 auto path = inventoryPath.substr(strlen(INVENTORY_OBJ_PATH));
714 object.emplace(path, std::move(interfaces));
715
716 try
717 {
718 auto service =
719 util::getService(INVENTORY_OBJ_PATH, INVENTORY_MGR_IFACE, bus);
720
721 if (service.empty())
722 {
723 log<level::ERR>("Unable to get inventory manager service");
724 return;
725 }
726
727 auto method =
728 bus.new_method_call(service.c_str(), INVENTORY_OBJ_PATH,
729 INVENTORY_MGR_IFACE, "Notify");
730
731 method.append(std::move(object));
732
733 auto reply = bus.call(method);
734 }
Patrick Williamsc1d4de52021-10-06 12:45:57 -0500735 catch (const std::exception& e)
Brandon Wyman1d7a7df2020-03-26 10:14:05 -0500736 {
Jay Meyer6a3fd2c2020-08-25 16:37:16 -0500737 log<level::ERR>(
738 std::string(e.what() + std::string(" PATH=") + inventoryPath)
739 .c_str());
Brandon Wyman1d7a7df2020-03-26 10:14:05 -0500740 }
741#endif
742 }
743}
744
Adriana Kobylak4175ffb2021-08-02 14:51:05 +0000745void PowerSupply::getInputVoltage(double& actualInputVoltage,
746 int& inputVoltage) const
747{
748 using namespace phosphor::pmbus;
749
750 actualInputVoltage = in_input::VIN_VOLTAGE_0;
751 inputVoltage = in_input::VIN_VOLTAGE_0;
752
753 if (present)
754 {
755 try
756 {
757 // Read input voltage in millivolts
758 auto inputVoltageStr = pmbusIntf->readString(READ_VIN, Type::Hwmon);
759
760 // Convert to volts
761 actualInputVoltage = std::stod(inputVoltageStr) / 1000;
762
763 // Calculate the voltage based on voltage thresholds
764 if (actualInputVoltage < in_input::VIN_VOLTAGE_MIN)
765 {
766 inputVoltage = in_input::VIN_VOLTAGE_0;
767 }
768 else if (actualInputVoltage < in_input::VIN_VOLTAGE_110_THRESHOLD)
769 {
770 inputVoltage = in_input::VIN_VOLTAGE_110;
771 }
772 else
773 {
774 inputVoltage = in_input::VIN_VOLTAGE_220;
775 }
776 }
777 catch (const std::exception& e)
778 {
779 log<level::ERR>(
780 fmt::format("READ_VIN read error: {}", e.what()).c_str());
781 }
782 }
783}
784
Brandon Wyman3f1242f2020-01-28 13:11:25 -0600785} // namespace phosphor::power::psu