blob: d416c943451e5afe984e645cb348085b6aff0f7b [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 Wyman08378782021-12-21 23:48:15 +0000260void PowerSupply::analyzeVoutUVFault()
261{
262 if ((statusWord & phosphor::pmbus::status_word::VOUT_FAULT) &&
263 !(statusWord & phosphor::pmbus::status_word::VOUT_OV_FAULT))
264 {
265 if (!voutUVFault)
266 {
267 log<level::ERR>(
268 fmt::format("VOUT_UV_FAULT fault: STATUS_WORD = {:#04x}, "
269 "STATUS_MFR_SPECIFIC = {:#02x}, "
270 "STATUS_VOUT = {:#02x}",
271 statusWord, statusMFR, statusVout)
272 .c_str());
273 }
274
275 voutUVFault = true;
276 }
277}
278
Brandon Wymand5d9a222021-12-21 23:59:05 +0000279void PowerSupply::analyzeFanFault()
280{
281 if (statusWord & phosphor::pmbus::status_word::FAN_FAULT)
282 {
283 if (!fanFault)
284 {
285 log<level::ERR>(fmt::format("FANS fault/warning: "
286 "STATUS_WORD = {:#04x}, "
287 "STATUS_MFR_SPECIFIC = {:#02x}, "
288 "STATUS_FANS_1_2 = {:#02x}",
289 statusWord, statusMFR, statusFans12)
290 .c_str());
291 }
292
293 fanFault = true;
294 }
295}
296
Brandon Wyman52cb3f22021-12-21 23:02:47 +0000297void PowerSupply::analyzeTemperatureFault()
298{
299 if (statusWord & phosphor::pmbus::status_word::TEMPERATURE_FAULT_WARN)
300 {
301 if (!tempFault)
302 {
303 log<level::ERR>(fmt::format("TEMPERATURE fault/warning: "
304 "STATUS_WORD = {:#04x}, "
305 "STATUS_MFR_SPECIFIC = {:#02x}, "
306 "STATUS_TEMPERATURE = {:#02x}",
307 statusWord, statusMFR,
308 statusTemperature)
309 .c_str());
310 }
311
312 tempFault = true;
313 }
314}
315
Brandon Wyman993b5542021-12-21 22:55:16 +0000316void PowerSupply::analyzePgoodFault()
317{
318 if ((statusWord & phosphor::pmbus::status_word::POWER_GOOD_NEGATED) ||
319 (statusWord & phosphor::pmbus::status_word::UNIT_IS_OFF))
320 {
321 if (pgoodFault < DEGLITCH_LIMIT)
322 {
323 log<level::ERR>(fmt::format("PGOOD fault: "
324 "STATUS_WORD = {:#04x}, "
325 "STATUS_MFR_SPECIFIC = {:#02x}",
326 statusWord, statusMFR)
327 .c_str());
328
329 pgoodFault++;
330 }
331 }
332 else
333 {
334 pgoodFault = 0;
335 }
336}
337
Brandon Wyman39ea02b2021-11-23 23:22:23 +0000338void PowerSupply::determineMFRFault()
339{
340 if (bindPath.string().find("ibm-cffps") != std::string::npos)
341 {
342 // IBM MFR_SPECIFIC[4] is PS_Kill fault
343 if (statusMFR & 0x10)
344 {
345 psKillFault = true;
346 }
347 // IBM MFR_SPECIFIC[6] is 12Vcs fault.
348 if (statusMFR & 0x40)
349 {
350 ps12VcsFault = true;
351 }
352 // IBM MFR_SPECIFIC[7] is 12V Current-Share fault.
353 if (statusMFR & 0x80)
354 {
355 psCS12VFault = true;
356 }
357 }
358}
359
Brandon Wyman6c2ac392021-12-21 22:23:06 +0000360void PowerSupply::analyzeMFRFault()
361{
362 if (statusWord & phosphor::pmbus::status_word::MFR_SPECIFIC_FAULT)
363 {
364 if (!mfrFault)
365 {
366 log<level::ERR>(fmt::format("MFR fault: "
367 "STATUS_WORD = {:#04x} "
368 "STATUS_MFR_SPECIFIC = {:#02x}",
369 statusWord, statusMFR)
370 .c_str());
371 }
372
373 mfrFault = true;
374 determineMFRFault();
375 }
376}
377
Brandon Wymanf087f472021-12-22 00:04:27 +0000378void PowerSupply::analyzeVinUVFault()
379{
380 if (statusWord & phosphor::pmbus::status_word::VIN_UV_FAULT)
381 {
382 if (!vinUVFault)
383 {
384 log<level::ERR>(fmt::format("VIN_UV fault: STATUS_WORD = {:#04x}, "
385 "STATUS_MFR_SPECIFIC = {:#02x}, "
386 "STATUS_INPUT = {:#02x}",
387 statusWord, statusMFR, statusInput)
388 .c_str());
389 }
390
391 vinUVFault = true;
392 }
393}
394
Brandon Wyman3f1242f2020-01-28 13:11:25 -0600395void PowerSupply::analyze()
396{
397 using namespace phosphor::pmbus;
398
B. J. Wyman681b2a32021-04-20 22:31:22 +0000399 if (presenceGPIO)
400 {
401 updatePresenceGPIO();
402 }
403
Brandon Wymanf65c4062020-08-19 13:15:53 -0500404 if ((present) && (readFail < LOG_LIMIT))
Brandon Wyman3f1242f2020-01-28 13:11:25 -0600405 {
406 try
407 {
Brandon Wymanfed0ba22020-09-26 20:02:51 -0500408 statusWord = pmbusIntf->read(STATUS_WORD, Type::Debug);
Brandon Wymanf65c4062020-08-19 13:15:53 -0500409 // Read worked, reset the fail count.
410 readFail = 0;
Brandon Wyman3f1242f2020-01-28 13:11:25 -0600411
412 if (statusWord)
413 {
Brandon Wymanf07bc792021-10-12 19:00:35 +0000414 statusInput = pmbusIntf->read(STATUS_INPUT, Type::Debug);
Jay Meyer10d94052020-11-30 14:41:21 -0600415 statusMFR = pmbusIntf->read(STATUS_MFR, Type::Debug);
Brandon Wyman85c7bf42021-10-19 22:28:48 +0000416 statusCML = pmbusIntf->read(STATUS_CML, Type::Debug);
Brandon Wyman6710ba22021-10-27 17:39:31 +0000417 auto status0Vout = pmbusIntf->insertPageNum(STATUS_VOUT, 0);
418 statusVout = pmbusIntf->read(status0Vout, Type::Debug);
Brandon Wymanb10b3be2021-11-09 22:12:15 +0000419 statusIout = pmbusIntf->read(STATUS_IOUT, Type::Debug);
Brandon Wyman7ee4d7e2021-11-19 20:48:23 +0000420 statusFans12 = pmbusIntf->read(STATUS_FANS_1_2, Type::Debug);
Brandon Wyman96893a42021-11-05 19:56:57 +0000421 statusTemperature =
422 pmbusIntf->read(STATUS_TEMPERATURE, Type::Debug);
Brandon Wyman9ddc6222021-10-28 17:28:01 +0000423
Brandon Wymanc2203432021-12-21 23:09:48 +0000424 analyzeCMLFault();
Brandon Wyman85c7bf42021-10-19 22:28:48 +0000425
Brandon Wymane3b0bb02021-12-21 23:16:48 +0000426 analyzeInputFault();
Brandon Wyman3f1242f2020-01-28 13:11:25 -0600427
Brandon Wymanc2c87132021-12-21 23:22:18 +0000428 analyzeVoutOVFault();
Brandon Wyman6710ba22021-10-27 17:39:31 +0000429
Brandon Wymana00e7302021-12-21 23:28:29 +0000430 analyzeIoutOCFault();
Brandon Wymanb10b3be2021-11-09 22:12:15 +0000431
Brandon Wyman08378782021-12-21 23:48:15 +0000432 analyzeVoutUVFault();
Brandon Wyman2cf46942021-10-28 19:09:16 +0000433
Brandon Wymand5d9a222021-12-21 23:59:05 +0000434 analyzeFanFault();
Brandon Wyman7ee4d7e2021-11-19 20:48:23 +0000435
Brandon Wyman52cb3f22021-12-21 23:02:47 +0000436 analyzeTemperatureFault();
Brandon Wyman96893a42021-11-05 19:56:57 +0000437
Brandon Wyman993b5542021-12-21 22:55:16 +0000438 analyzePgoodFault();
Brandon Wyman2916ea52021-11-06 03:31:18 +0000439
Brandon Wyman6c2ac392021-12-21 22:23:06 +0000440 analyzeMFRFault();
Brandon Wyman3f1242f2020-01-28 13:11:25 -0600441
Brandon Wymanf087f472021-12-22 00:04:27 +0000442 analyzeVinUVFault();
Brandon Wyman3f1242f2020-01-28 13:11:25 -0600443 }
444 else
445 {
Brandon Wymane3f7ad22021-12-21 20:27:45 +0000446 // if INPUT/VIN_UV fault was on, it cleared, trace it.
447 if (inputFault)
448 {
449 log<level::INFO>(
450 fmt::format(
451 "INPUT fault cleared: STATUS_WORD = {:#04x}",
452 statusWord)
453 .c_str());
454 }
455
456 if (vinUVFault)
457 {
458 log<level::INFO>(
459 fmt::format("VIN_UV cleared: STATUS_WORD = {:#04x}",
460 statusWord)
461 .c_str());
462 }
463
Brandon Wyman06ca4592021-12-06 22:52:23 +0000464 if (pgoodFault > 0)
Brandon Wyman4aecc292021-11-10 22:40:41 +0000465 {
466 log<level::INFO>(fmt::format("pgoodFault cleared path: {}",
467 inventoryPath)
468 .c_str());
Brandon Wyman4aecc292021-11-10 22:40:41 +0000469 }
Brandon Wymane3f7ad22021-12-21 20:27:45 +0000470
471 clearFaultFlags();
Brandon Wyman3f1242f2020-01-28 13:11:25 -0600472 }
473 }
Patrick Williamsc1d4de52021-10-06 12:45:57 -0500474 catch (const ReadFailure& e)
Brandon Wyman3f1242f2020-01-28 13:11:25 -0600475 {
Brandon Wymanf65c4062020-08-19 13:15:53 -0500476 readFail++;
Brandon Wyman3f1242f2020-01-28 13:11:25 -0600477 phosphor::logging::commit<ReadFailure>();
478 }
479 }
480}
481
Brandon Wyman59a35792020-06-04 12:37:40 -0500482void PowerSupply::onOffConfig(uint8_t data)
483{
484 using namespace phosphor::pmbus;
485
486 if (present)
487 {
488 log<level::INFO>("ON_OFF_CONFIG write", entry("DATA=0x%02X", data));
489 try
490 {
491 std::vector<uint8_t> configData{data};
492 pmbusIntf->writeBinary(ON_OFF_CONFIG, configData,
493 Type::HwmonDeviceDebug);
494 }
495 catch (...)
496 {
497 // The underlying code in writeBinary will log a message to the
B. J. Wyman681b2a32021-04-20 22:31:22 +0000498 // journal if the write fails. If the ON_OFF_CONFIG is not setup
499 // as desired, later fault detection and analysis code should
500 // catch any of the fall out. We should not need to terminate
501 // the application if this write fails.
Brandon Wyman59a35792020-06-04 12:37:40 -0500502 }
503 }
504}
505
Brandon Wyman3c208462020-05-13 16:25:58 -0500506void PowerSupply::clearFaults()
507{
Brandon Wyman5474c912021-02-23 14:39:43 -0600508 faultLogged = false;
Brandon Wyman3c208462020-05-13 16:25:58 -0500509 // The PMBus device driver does not allow for writing CLEAR_FAULTS
510 // directly. However, the pmbus hwmon device driver code will send a
511 // CLEAR_FAULTS after reading from any of the hwmon "files" in sysfs, so
512 // reading in1_input should result in clearing the fault bits in
513 // STATUS_BYTE/STATUS_WORD.
514 // I do not care what the return value is.
Brandon Wyman11151532020-11-10 13:45:57 -0600515 if (present)
Brandon Wyman3c208462020-05-13 16:25:58 -0500516 {
Brandon Wymane3f7ad22021-12-21 20:27:45 +0000517 clearFaultFlags();
Brandon Wyman9564e942020-11-10 14:01:42 -0600518 readFail = 0;
Brandon Wyman9564e942020-11-10 14:01:42 -0600519
Brandon Wyman11151532020-11-10 13:45:57 -0600520 try
521 {
522 static_cast<void>(
523 pmbusIntf->read("in1_input", phosphor::pmbus::Type::Hwmon));
524 }
Patrick Williamsc1d4de52021-10-06 12:45:57 -0500525 catch (const ReadFailure& e)
Brandon Wyman11151532020-11-10 13:45:57 -0600526 {
527 // Since I do not care what the return value is, I really do not
B. J. Wyman681b2a32021-04-20 22:31:22 +0000528 // care much if it gets a ReadFailure either. However, this
529 // should not prevent the application from continuing to run, so
530 // catching the read failure.
Brandon Wyman11151532020-11-10 13:45:57 -0600531 }
Brandon Wyman3c208462020-05-13 16:25:58 -0500532 }
533}
534
Brandon Wymanaed1f752019-11-25 18:10:52 -0600535void PowerSupply::inventoryChanged(sdbusplus::message::message& msg)
536{
537 std::string msgSensor;
Patrick Williamsabe49412020-05-13 17:59:47 -0500538 std::map<std::string, std::variant<uint32_t, bool>> msgData;
Brandon Wymanaed1f752019-11-25 18:10:52 -0600539 msg.read(msgSensor, msgData);
540
541 // Check if it was the Present property that changed.
542 auto valPropMap = msgData.find(PRESENT_PROP);
543 if (valPropMap != msgData.end())
544 {
545 if (std::get<bool>(valPropMap->second))
546 {
547 present = true;
B. J. Wyman681b2a32021-04-20 22:31:22 +0000548 // TODO: Immediately trying to read or write the "files" causes
549 // read or write failures.
Brandon Wyman1d7a7df2020-03-26 10:14:05 -0500550 using namespace std::chrono_literals;
551 std::this_thread::sleep_for(20ms);
Brandon Wyman9564e942020-11-10 14:01:42 -0600552 pmbusIntf->findHwmonDir();
Brandon Wyman59a35792020-06-04 12:37:40 -0500553 onOffConfig(phosphor::pmbus::ON_OFF_CONFIG_CONTROL_PIN_ONLY);
Brandon Wymanaed1f752019-11-25 18:10:52 -0600554 clearFaults();
Brandon Wyman1d7a7df2020-03-26 10:14:05 -0500555 updateInventory();
Brandon Wymanaed1f752019-11-25 18:10:52 -0600556 }
557 else
558 {
559 present = false;
560
561 // Clear out the now outdated inventory properties
562 updateInventory();
563 }
564 }
565}
566
Brandon Wyman9a507db2021-02-25 16:15:22 -0600567void PowerSupply::inventoryAdded(sdbusplus::message::message& msg)
568{
569 sdbusplus::message::object_path path;
570 msg.read(path);
571 // Make sure the signal is for the PSU inventory path
572 if (path == inventoryPath)
573 {
574 std::map<std::string, std::map<std::string, std::variant<bool>>>
575 interfaces;
576 // Get map of interfaces and their properties
577 msg.read(interfaces);
578
579 auto properties = interfaces.find(INVENTORY_IFACE);
580 if (properties != interfaces.end())
581 {
582 auto property = properties->second.find(PRESENT_PROP);
583 if (property != properties->second.end())
584 {
585 present = std::get<bool>(property->second);
586
587 log<level::INFO>(fmt::format("Power Supply {} Present {}",
588 inventoryPath, present)
589 .c_str());
590
591 updateInventory();
592 }
593 }
594 }
595}
596
Brandon Wyman1d7a7df2020-03-26 10:14:05 -0500597void PowerSupply::updateInventory()
598{
599 using namespace phosphor::pmbus;
600
Chanh Nguyenc12c53b2021-04-06 17:24:47 +0700601#if IBM_VPD
Brandon Wyman1d7a7df2020-03-26 10:14:05 -0500602 std::string ccin;
603 std::string pn;
604 std::string fn;
605 std::string header;
606 std::string sn;
Brandon Wyman1d7a7df2020-03-26 10:14:05 -0500607 using PropertyMap =
George Liu070c1bc2020-10-12 11:28:01 +0800608 std::map<std::string,
609 std::variant<std::string, std::vector<uint8_t>, bool>>;
Brandon Wyman1d7a7df2020-03-26 10:14:05 -0500610 PropertyMap assetProps;
George Liu070c1bc2020-10-12 11:28:01 +0800611 PropertyMap operProps;
Brandon Wyman1d7a7df2020-03-26 10:14:05 -0500612 PropertyMap versionProps;
613 PropertyMap ipzvpdDINFProps;
614 PropertyMap ipzvpdVINIProps;
615 using InterfaceMap = std::map<std::string, PropertyMap>;
616 InterfaceMap interfaces;
617 using ObjectMap = std::map<sdbusplus::message::object_path, InterfaceMap>;
618 ObjectMap object;
619#endif
B. J. Wyman681b2a32021-04-20 22:31:22 +0000620 log<level::DEBUG>(
621 fmt::format("updateInventory() inventoryPath: {}", inventoryPath)
622 .c_str());
Brandon Wyman1d7a7df2020-03-26 10:14:05 -0500623
624 if (present)
625 {
626 // TODO: non-IBM inventory updates?
627
Chanh Nguyenc12c53b2021-04-06 17:24:47 +0700628#if IBM_VPD
Brandon Wyman1d7a7df2020-03-26 10:14:05 -0500629 try
630 {
631 ccin = pmbusIntf->readString(CCIN, Type::HwmonDeviceDebug);
632 assetProps.emplace(MODEL_PROP, ccin);
Adriana Kobylak572a9052021-03-30 15:58:07 +0000633 modelName = ccin;
Brandon Wyman1d7a7df2020-03-26 10:14:05 -0500634 }
Patrick Williamsc1d4de52021-10-06 12:45:57 -0500635 catch (const ReadFailure& e)
Brandon Wyman1d7a7df2020-03-26 10:14:05 -0500636 {
B. J. Wyman681b2a32021-04-20 22:31:22 +0000637 // Ignore the read failure, let pmbus code indicate failure,
638 // path...
Brandon Wyman1d7a7df2020-03-26 10:14:05 -0500639 // TODO - ibm918
640 // https://github.com/openbmc/docs/blob/master/designs/vpd-collection.md
641 // The BMC must log errors if any of the VPD cannot be properly
642 // parsed or fails ECC checks.
643 }
644
645 try
646 {
647 pn = pmbusIntf->readString(PART_NUMBER, Type::HwmonDeviceDebug);
648 assetProps.emplace(PN_PROP, pn);
649 }
Patrick Williamsc1d4de52021-10-06 12:45:57 -0500650 catch (const ReadFailure& e)
Brandon Wyman1d7a7df2020-03-26 10:14:05 -0500651 {
B. J. Wyman681b2a32021-04-20 22:31:22 +0000652 // Ignore the read failure, let pmbus code indicate failure,
653 // path...
Brandon Wyman1d7a7df2020-03-26 10:14:05 -0500654 }
655
656 try
657 {
658 fn = pmbusIntf->readString(FRU_NUMBER, Type::HwmonDeviceDebug);
Brandon Wymana169b0f2021-12-07 20:18:06 +0000659 assetProps.emplace(SPARE_PN_PROP, fn);
Brandon Wyman1d7a7df2020-03-26 10:14:05 -0500660 }
Patrick Williamsc1d4de52021-10-06 12:45:57 -0500661 catch (const ReadFailure& e)
Brandon Wyman1d7a7df2020-03-26 10:14:05 -0500662 {
B. J. Wyman681b2a32021-04-20 22:31:22 +0000663 // Ignore the read failure, let pmbus code indicate failure,
664 // path...
Brandon Wyman1d7a7df2020-03-26 10:14:05 -0500665 }
666
667 try
668 {
669 header =
670 pmbusIntf->readString(SERIAL_HEADER, Type::HwmonDeviceDebug);
671 sn = pmbusIntf->readString(SERIAL_NUMBER, Type::HwmonDeviceDebug);
672 assetProps.emplace(SN_PROP, sn);
673 }
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 try
681 {
Brandon Wymanc9efe412020-10-09 15:42:50 -0500682 fwVersion =
683 pmbusIntf->readString(FW_VERSION, Type::HwmonDeviceDebug);
684 versionProps.emplace(VERSION_PROP, fwVersion);
Brandon Wyman1d7a7df2020-03-26 10:14:05 -0500685 }
Patrick Williamsc1d4de52021-10-06 12:45:57 -0500686 catch (const ReadFailure& e)
Brandon Wyman1d7a7df2020-03-26 10:14:05 -0500687 {
B. J. Wyman681b2a32021-04-20 22:31:22 +0000688 // Ignore the read failure, let pmbus code indicate failure,
689 // path...
Brandon Wyman1d7a7df2020-03-26 10:14:05 -0500690 }
691
692 ipzvpdVINIProps.emplace("CC",
693 std::vector<uint8_t>(ccin.begin(), ccin.end()));
694 ipzvpdVINIProps.emplace("PN",
695 std::vector<uint8_t>(pn.begin(), pn.end()));
696 ipzvpdVINIProps.emplace("FN",
697 std::vector<uint8_t>(fn.begin(), fn.end()));
698 std::string header_sn = header + sn + '\0';
699 ipzvpdVINIProps.emplace(
700 "SN", std::vector<uint8_t>(header_sn.begin(), header_sn.end()));
701 std::string description = "IBM PS";
702 ipzvpdVINIProps.emplace(
703 "DR", std::vector<uint8_t>(description.begin(), description.end()));
704
705 // Update the Resource Identifier (RI) keyword
706 // 2 byte FRC: 0x0003
707 // 2 byte RID: 0x1000, 0x1001...
708 std::uint8_t num = std::stoul(
709 inventoryPath.substr(inventoryPath.size() - 1, 1), nullptr, 0);
710 std::vector<uint8_t> ri{0x00, 0x03, 0x10, num};
711 ipzvpdDINFProps.emplace("RI", ri);
712
713 // Fill in the FRU Label (FL) keyword.
714 std::string fl = "E";
715 fl.push_back(inventoryPath.back());
716 fl.resize(FL_KW_SIZE, ' ');
717 ipzvpdDINFProps.emplace("FL",
718 std::vector<uint8_t>(fl.begin(), fl.end()));
719
720 interfaces.emplace(ASSET_IFACE, std::move(assetProps));
721 interfaces.emplace(VERSION_IFACE, std::move(versionProps));
722 interfaces.emplace(DINF_IFACE, std::move(ipzvpdDINFProps));
723 interfaces.emplace(VINI_IFACE, std::move(ipzvpdVINIProps));
724
George Liu070c1bc2020-10-12 11:28:01 +0800725 // Update the Functional
726 operProps.emplace(FUNCTIONAL_PROP, present);
727 interfaces.emplace(OPERATIONAL_STATE_IFACE, std::move(operProps));
728
Brandon Wyman1d7a7df2020-03-26 10:14:05 -0500729 auto path = inventoryPath.substr(strlen(INVENTORY_OBJ_PATH));
730 object.emplace(path, std::move(interfaces));
731
732 try
733 {
734 auto service =
735 util::getService(INVENTORY_OBJ_PATH, INVENTORY_MGR_IFACE, bus);
736
737 if (service.empty())
738 {
739 log<level::ERR>("Unable to get inventory manager service");
740 return;
741 }
742
743 auto method =
744 bus.new_method_call(service.c_str(), INVENTORY_OBJ_PATH,
745 INVENTORY_MGR_IFACE, "Notify");
746
747 method.append(std::move(object));
748
749 auto reply = bus.call(method);
750 }
Patrick Williamsc1d4de52021-10-06 12:45:57 -0500751 catch (const std::exception& e)
Brandon Wyman1d7a7df2020-03-26 10:14:05 -0500752 {
Jay Meyer6a3fd2c2020-08-25 16:37:16 -0500753 log<level::ERR>(
754 std::string(e.what() + std::string(" PATH=") + inventoryPath)
755 .c_str());
Brandon Wyman1d7a7df2020-03-26 10:14:05 -0500756 }
757#endif
758 }
759}
760
Adriana Kobylak4175ffb2021-08-02 14:51:05 +0000761void PowerSupply::getInputVoltage(double& actualInputVoltage,
762 int& inputVoltage) const
763{
764 using namespace phosphor::pmbus;
765
766 actualInputVoltage = in_input::VIN_VOLTAGE_0;
767 inputVoltage = in_input::VIN_VOLTAGE_0;
768
769 if (present)
770 {
771 try
772 {
773 // Read input voltage in millivolts
774 auto inputVoltageStr = pmbusIntf->readString(READ_VIN, Type::Hwmon);
775
776 // Convert to volts
777 actualInputVoltage = std::stod(inputVoltageStr) / 1000;
778
779 // Calculate the voltage based on voltage thresholds
780 if (actualInputVoltage < in_input::VIN_VOLTAGE_MIN)
781 {
782 inputVoltage = in_input::VIN_VOLTAGE_0;
783 }
784 else if (actualInputVoltage < in_input::VIN_VOLTAGE_110_THRESHOLD)
785 {
786 inputVoltage = in_input::VIN_VOLTAGE_110;
787 }
788 else
789 {
790 inputVoltage = in_input::VIN_VOLTAGE_220;
791 }
792 }
793 catch (const std::exception& e)
794 {
795 log<level::ERR>(
796 fmt::format("READ_VIN read error: {}", e.what()).c_str());
797 }
798 }
799}
800
Brandon Wyman3f1242f2020-01-28 13:11:25 -0600801} // namespace phosphor::power::psu