blob: 5be801a5500c977168d13161d5402fa13ff9b996 [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 Wyman52cb3f22021-12-21 23:02:47 +0000279void PowerSupply::analyzeTemperatureFault()
280{
281 if (statusWord & phosphor::pmbus::status_word::TEMPERATURE_FAULT_WARN)
282 {
283 if (!tempFault)
284 {
285 log<level::ERR>(fmt::format("TEMPERATURE fault/warning: "
286 "STATUS_WORD = {:#04x}, "
287 "STATUS_MFR_SPECIFIC = {:#02x}, "
288 "STATUS_TEMPERATURE = {:#02x}",
289 statusWord, statusMFR,
290 statusTemperature)
291 .c_str());
292 }
293
294 tempFault = true;
295 }
296}
297
Brandon Wyman993b5542021-12-21 22:55:16 +0000298void PowerSupply::analyzePgoodFault()
299{
300 if ((statusWord & phosphor::pmbus::status_word::POWER_GOOD_NEGATED) ||
301 (statusWord & phosphor::pmbus::status_word::UNIT_IS_OFF))
302 {
303 if (pgoodFault < DEGLITCH_LIMIT)
304 {
305 log<level::ERR>(fmt::format("PGOOD fault: "
306 "STATUS_WORD = {:#04x}, "
307 "STATUS_MFR_SPECIFIC = {:#02x}",
308 statusWord, statusMFR)
309 .c_str());
310
311 pgoodFault++;
312 }
313 }
314 else
315 {
316 pgoodFault = 0;
317 }
318}
319
Brandon Wyman39ea02b2021-11-23 23:22:23 +0000320void PowerSupply::determineMFRFault()
321{
322 if (bindPath.string().find("ibm-cffps") != std::string::npos)
323 {
324 // IBM MFR_SPECIFIC[4] is PS_Kill fault
325 if (statusMFR & 0x10)
326 {
327 psKillFault = true;
328 }
329 // IBM MFR_SPECIFIC[6] is 12Vcs fault.
330 if (statusMFR & 0x40)
331 {
332 ps12VcsFault = true;
333 }
334 // IBM MFR_SPECIFIC[7] is 12V Current-Share fault.
335 if (statusMFR & 0x80)
336 {
337 psCS12VFault = true;
338 }
339 }
340}
341
Brandon Wyman6c2ac392021-12-21 22:23:06 +0000342void PowerSupply::analyzeMFRFault()
343{
344 if (statusWord & phosphor::pmbus::status_word::MFR_SPECIFIC_FAULT)
345 {
346 if (!mfrFault)
347 {
348 log<level::ERR>(fmt::format("MFR fault: "
349 "STATUS_WORD = {:#04x} "
350 "STATUS_MFR_SPECIFIC = {:#02x}",
351 statusWord, statusMFR)
352 .c_str());
353 }
354
355 mfrFault = true;
356 determineMFRFault();
357 }
358}
359
Brandon Wyman3f1242f2020-01-28 13:11:25 -0600360void PowerSupply::analyze()
361{
362 using namespace phosphor::pmbus;
363
B. J. Wyman681b2a32021-04-20 22:31:22 +0000364 if (presenceGPIO)
365 {
366 updatePresenceGPIO();
367 }
368
Brandon Wymanf65c4062020-08-19 13:15:53 -0500369 if ((present) && (readFail < LOG_LIMIT))
Brandon Wyman3f1242f2020-01-28 13:11:25 -0600370 {
371 try
372 {
Brandon Wymanfed0ba22020-09-26 20:02:51 -0500373 statusWord = pmbusIntf->read(STATUS_WORD, Type::Debug);
Brandon Wymanf65c4062020-08-19 13:15:53 -0500374 // Read worked, reset the fail count.
375 readFail = 0;
Brandon Wyman3f1242f2020-01-28 13:11:25 -0600376
377 if (statusWord)
378 {
Brandon Wymanf07bc792021-10-12 19:00:35 +0000379 statusInput = pmbusIntf->read(STATUS_INPUT, Type::Debug);
Jay Meyer10d94052020-11-30 14:41:21 -0600380 statusMFR = pmbusIntf->read(STATUS_MFR, Type::Debug);
Brandon Wyman85c7bf42021-10-19 22:28:48 +0000381 statusCML = pmbusIntf->read(STATUS_CML, Type::Debug);
Brandon Wyman6710ba22021-10-27 17:39:31 +0000382 auto status0Vout = pmbusIntf->insertPageNum(STATUS_VOUT, 0);
383 statusVout = pmbusIntf->read(status0Vout, Type::Debug);
Brandon Wymanb10b3be2021-11-09 22:12:15 +0000384 statusIout = pmbusIntf->read(STATUS_IOUT, Type::Debug);
Brandon Wyman7ee4d7e2021-11-19 20:48:23 +0000385 statusFans12 = pmbusIntf->read(STATUS_FANS_1_2, Type::Debug);
Brandon Wyman96893a42021-11-05 19:56:57 +0000386 statusTemperature =
387 pmbusIntf->read(STATUS_TEMPERATURE, Type::Debug);
Brandon Wyman9ddc6222021-10-28 17:28:01 +0000388
Brandon Wymanc2203432021-12-21 23:09:48 +0000389 analyzeCMLFault();
Brandon Wyman85c7bf42021-10-19 22:28:48 +0000390
Brandon Wymane3b0bb02021-12-21 23:16:48 +0000391 analyzeInputFault();
Brandon Wyman3f1242f2020-01-28 13:11:25 -0600392
Brandon Wymanc2c87132021-12-21 23:22:18 +0000393 analyzeVoutOVFault();
Brandon Wyman6710ba22021-10-27 17:39:31 +0000394
Brandon Wymana00e7302021-12-21 23:28:29 +0000395 analyzeIoutOCFault();
Brandon Wymanb10b3be2021-11-09 22:12:15 +0000396
Brandon Wyman08378782021-12-21 23:48:15 +0000397 analyzeVoutUVFault();
Brandon Wyman2cf46942021-10-28 19:09:16 +0000398
Brandon Wyman7ee4d7e2021-11-19 20:48:23 +0000399 if (statusWord & status_word::FAN_FAULT)
400 {
401 if (!fanFault)
402 {
403 log<level::ERR>(
404 fmt::format("FANS fault/warning: "
405 "STATUS_WORD = {:#04x}, "
406 "STATUS_MFR_SPECIFIC = {:#02x}, "
407 "STATUS_FANS_1_2 = {:#02x}",
408 statusWord, statusMFR, statusFans12)
409 .c_str());
410 }
411
412 fanFault = true;
413 }
414
Brandon Wyman52cb3f22021-12-21 23:02:47 +0000415 analyzeTemperatureFault();
Brandon Wyman96893a42021-11-05 19:56:57 +0000416
Brandon Wyman993b5542021-12-21 22:55:16 +0000417 analyzePgoodFault();
Brandon Wyman2916ea52021-11-06 03:31:18 +0000418
Brandon Wyman6c2ac392021-12-21 22:23:06 +0000419 analyzeMFRFault();
Brandon Wyman3f1242f2020-01-28 13:11:25 -0600420
421 if (statusWord & status_word::VIN_UV_FAULT)
422 {
423 if (!vinUVFault)
424 {
Brandon Wyman43d32632021-10-26 23:44:11 +0000425 log<level::ERR>(
Brandon Wymanf07bc792021-10-12 19:00:35 +0000426 fmt::format("VIN_UV fault: STATUS_WORD = {:#04x}, "
427 "STATUS_MFR_SPECIFIC = {:#02x}, "
428 "STATUS_INPUT = {:#02x}",
429 statusWord, statusMFR, statusInput)
Brandon Wymanc8996602021-10-12 19:28:56 +0000430 .c_str());
Brandon Wyman3f1242f2020-01-28 13:11:25 -0600431 }
432
Brandon Wyman3f1242f2020-01-28 13:11:25 -0600433 vinUVFault = true;
434 }
435 }
436 else
437 {
Brandon Wymane3f7ad22021-12-21 20:27:45 +0000438 // if INPUT/VIN_UV fault was on, it cleared, trace it.
439 if (inputFault)
440 {
441 log<level::INFO>(
442 fmt::format(
443 "INPUT fault cleared: STATUS_WORD = {:#04x}",
444 statusWord)
445 .c_str());
446 }
447
448 if (vinUVFault)
449 {
450 log<level::INFO>(
451 fmt::format("VIN_UV cleared: STATUS_WORD = {:#04x}",
452 statusWord)
453 .c_str());
454 }
455
Brandon Wyman06ca4592021-12-06 22:52:23 +0000456 if (pgoodFault > 0)
Brandon Wyman4aecc292021-11-10 22:40:41 +0000457 {
458 log<level::INFO>(fmt::format("pgoodFault cleared path: {}",
459 inventoryPath)
460 .c_str());
Brandon Wyman4aecc292021-11-10 22:40:41 +0000461 }
Brandon Wymane3f7ad22021-12-21 20:27:45 +0000462
463 clearFaultFlags();
Brandon Wyman3f1242f2020-01-28 13:11:25 -0600464 }
465 }
Patrick Williamsc1d4de52021-10-06 12:45:57 -0500466 catch (const ReadFailure& e)
Brandon Wyman3f1242f2020-01-28 13:11:25 -0600467 {
Brandon Wymanf65c4062020-08-19 13:15:53 -0500468 readFail++;
Brandon Wyman3f1242f2020-01-28 13:11:25 -0600469 phosphor::logging::commit<ReadFailure>();
470 }
471 }
472}
473
Brandon Wyman59a35792020-06-04 12:37:40 -0500474void PowerSupply::onOffConfig(uint8_t data)
475{
476 using namespace phosphor::pmbus;
477
478 if (present)
479 {
480 log<level::INFO>("ON_OFF_CONFIG write", entry("DATA=0x%02X", data));
481 try
482 {
483 std::vector<uint8_t> configData{data};
484 pmbusIntf->writeBinary(ON_OFF_CONFIG, configData,
485 Type::HwmonDeviceDebug);
486 }
487 catch (...)
488 {
489 // The underlying code in writeBinary will log a message to the
B. J. Wyman681b2a32021-04-20 22:31:22 +0000490 // journal if the write fails. If the ON_OFF_CONFIG is not setup
491 // as desired, later fault detection and analysis code should
492 // catch any of the fall out. We should not need to terminate
493 // the application if this write fails.
Brandon Wyman59a35792020-06-04 12:37:40 -0500494 }
495 }
496}
497
Brandon Wyman3c208462020-05-13 16:25:58 -0500498void PowerSupply::clearFaults()
499{
Brandon Wyman5474c912021-02-23 14:39:43 -0600500 faultLogged = false;
Brandon Wyman3c208462020-05-13 16:25:58 -0500501 // The PMBus device driver does not allow for writing CLEAR_FAULTS
502 // directly. However, the pmbus hwmon device driver code will send a
503 // CLEAR_FAULTS after reading from any of the hwmon "files" in sysfs, so
504 // reading in1_input should result in clearing the fault bits in
505 // STATUS_BYTE/STATUS_WORD.
506 // I do not care what the return value is.
Brandon Wyman11151532020-11-10 13:45:57 -0600507 if (present)
Brandon Wyman3c208462020-05-13 16:25:58 -0500508 {
Brandon Wymane3f7ad22021-12-21 20:27:45 +0000509 clearFaultFlags();
Brandon Wyman9564e942020-11-10 14:01:42 -0600510 readFail = 0;
Brandon Wyman9564e942020-11-10 14:01:42 -0600511
Brandon Wyman11151532020-11-10 13:45:57 -0600512 try
513 {
514 static_cast<void>(
515 pmbusIntf->read("in1_input", phosphor::pmbus::Type::Hwmon));
516 }
Patrick Williamsc1d4de52021-10-06 12:45:57 -0500517 catch (const ReadFailure& e)
Brandon Wyman11151532020-11-10 13:45:57 -0600518 {
519 // Since I do not care what the return value is, I really do not
B. J. Wyman681b2a32021-04-20 22:31:22 +0000520 // care much if it gets a ReadFailure either. However, this
521 // should not prevent the application from continuing to run, so
522 // catching the read failure.
Brandon Wyman11151532020-11-10 13:45:57 -0600523 }
Brandon Wyman3c208462020-05-13 16:25:58 -0500524 }
525}
526
Brandon Wymanaed1f752019-11-25 18:10:52 -0600527void PowerSupply::inventoryChanged(sdbusplus::message::message& msg)
528{
529 std::string msgSensor;
Patrick Williamsabe49412020-05-13 17:59:47 -0500530 std::map<std::string, std::variant<uint32_t, bool>> msgData;
Brandon Wymanaed1f752019-11-25 18:10:52 -0600531 msg.read(msgSensor, msgData);
532
533 // Check if it was the Present property that changed.
534 auto valPropMap = msgData.find(PRESENT_PROP);
535 if (valPropMap != msgData.end())
536 {
537 if (std::get<bool>(valPropMap->second))
538 {
539 present = true;
B. J. Wyman681b2a32021-04-20 22:31:22 +0000540 // TODO: Immediately trying to read or write the "files" causes
541 // read or write failures.
Brandon Wyman1d7a7df2020-03-26 10:14:05 -0500542 using namespace std::chrono_literals;
543 std::this_thread::sleep_for(20ms);
Brandon Wyman9564e942020-11-10 14:01:42 -0600544 pmbusIntf->findHwmonDir();
Brandon Wyman59a35792020-06-04 12:37:40 -0500545 onOffConfig(phosphor::pmbus::ON_OFF_CONFIG_CONTROL_PIN_ONLY);
Brandon Wymanaed1f752019-11-25 18:10:52 -0600546 clearFaults();
Brandon Wyman1d7a7df2020-03-26 10:14:05 -0500547 updateInventory();
Brandon Wymanaed1f752019-11-25 18:10:52 -0600548 }
549 else
550 {
551 present = false;
552
553 // Clear out the now outdated inventory properties
554 updateInventory();
555 }
556 }
557}
558
Brandon Wyman9a507db2021-02-25 16:15:22 -0600559void PowerSupply::inventoryAdded(sdbusplus::message::message& msg)
560{
561 sdbusplus::message::object_path path;
562 msg.read(path);
563 // Make sure the signal is for the PSU inventory path
564 if (path == inventoryPath)
565 {
566 std::map<std::string, std::map<std::string, std::variant<bool>>>
567 interfaces;
568 // Get map of interfaces and their properties
569 msg.read(interfaces);
570
571 auto properties = interfaces.find(INVENTORY_IFACE);
572 if (properties != interfaces.end())
573 {
574 auto property = properties->second.find(PRESENT_PROP);
575 if (property != properties->second.end())
576 {
577 present = std::get<bool>(property->second);
578
579 log<level::INFO>(fmt::format("Power Supply {} Present {}",
580 inventoryPath, present)
581 .c_str());
582
583 updateInventory();
584 }
585 }
586 }
587}
588
Brandon Wyman1d7a7df2020-03-26 10:14:05 -0500589void PowerSupply::updateInventory()
590{
591 using namespace phosphor::pmbus;
592
Chanh Nguyenc12c53b2021-04-06 17:24:47 +0700593#if IBM_VPD
Brandon Wyman1d7a7df2020-03-26 10:14:05 -0500594 std::string ccin;
595 std::string pn;
596 std::string fn;
597 std::string header;
598 std::string sn;
Brandon Wyman1d7a7df2020-03-26 10:14:05 -0500599 using PropertyMap =
George Liu070c1bc2020-10-12 11:28:01 +0800600 std::map<std::string,
601 std::variant<std::string, std::vector<uint8_t>, bool>>;
Brandon Wyman1d7a7df2020-03-26 10:14:05 -0500602 PropertyMap assetProps;
George Liu070c1bc2020-10-12 11:28:01 +0800603 PropertyMap operProps;
Brandon Wyman1d7a7df2020-03-26 10:14:05 -0500604 PropertyMap versionProps;
605 PropertyMap ipzvpdDINFProps;
606 PropertyMap ipzvpdVINIProps;
607 using InterfaceMap = std::map<std::string, PropertyMap>;
608 InterfaceMap interfaces;
609 using ObjectMap = std::map<sdbusplus::message::object_path, InterfaceMap>;
610 ObjectMap object;
611#endif
B. J. Wyman681b2a32021-04-20 22:31:22 +0000612 log<level::DEBUG>(
613 fmt::format("updateInventory() inventoryPath: {}", inventoryPath)
614 .c_str());
Brandon Wyman1d7a7df2020-03-26 10:14:05 -0500615
616 if (present)
617 {
618 // TODO: non-IBM inventory updates?
619
Chanh Nguyenc12c53b2021-04-06 17:24:47 +0700620#if IBM_VPD
Brandon Wyman1d7a7df2020-03-26 10:14:05 -0500621 try
622 {
623 ccin = pmbusIntf->readString(CCIN, Type::HwmonDeviceDebug);
624 assetProps.emplace(MODEL_PROP, ccin);
Adriana Kobylak572a9052021-03-30 15:58:07 +0000625 modelName = ccin;
Brandon Wyman1d7a7df2020-03-26 10:14:05 -0500626 }
Patrick Williamsc1d4de52021-10-06 12:45:57 -0500627 catch (const ReadFailure& e)
Brandon Wyman1d7a7df2020-03-26 10:14:05 -0500628 {
B. J. Wyman681b2a32021-04-20 22:31:22 +0000629 // Ignore the read failure, let pmbus code indicate failure,
630 // path...
Brandon Wyman1d7a7df2020-03-26 10:14:05 -0500631 // TODO - ibm918
632 // https://github.com/openbmc/docs/blob/master/designs/vpd-collection.md
633 // The BMC must log errors if any of the VPD cannot be properly
634 // parsed or fails ECC checks.
635 }
636
637 try
638 {
639 pn = pmbusIntf->readString(PART_NUMBER, Type::HwmonDeviceDebug);
640 assetProps.emplace(PN_PROP, pn);
641 }
Patrick Williamsc1d4de52021-10-06 12:45:57 -0500642 catch (const ReadFailure& e)
Brandon Wyman1d7a7df2020-03-26 10:14:05 -0500643 {
B. J. Wyman681b2a32021-04-20 22:31:22 +0000644 // Ignore the read failure, let pmbus code indicate failure,
645 // path...
Brandon Wyman1d7a7df2020-03-26 10:14:05 -0500646 }
647
648 try
649 {
650 fn = pmbusIntf->readString(FRU_NUMBER, Type::HwmonDeviceDebug);
Brandon Wymana169b0f2021-12-07 20:18:06 +0000651 assetProps.emplace(SPARE_PN_PROP, fn);
Brandon Wyman1d7a7df2020-03-26 10:14:05 -0500652 }
Patrick Williamsc1d4de52021-10-06 12:45:57 -0500653 catch (const ReadFailure& e)
Brandon Wyman1d7a7df2020-03-26 10:14:05 -0500654 {
B. J. Wyman681b2a32021-04-20 22:31:22 +0000655 // Ignore the read failure, let pmbus code indicate failure,
656 // path...
Brandon Wyman1d7a7df2020-03-26 10:14:05 -0500657 }
658
659 try
660 {
661 header =
662 pmbusIntf->readString(SERIAL_HEADER, Type::HwmonDeviceDebug);
663 sn = pmbusIntf->readString(SERIAL_NUMBER, Type::HwmonDeviceDebug);
664 assetProps.emplace(SN_PROP, sn);
665 }
Patrick Williamsc1d4de52021-10-06 12:45:57 -0500666 catch (const ReadFailure& e)
Brandon Wyman1d7a7df2020-03-26 10:14:05 -0500667 {
B. J. Wyman681b2a32021-04-20 22:31:22 +0000668 // Ignore the read failure, let pmbus code indicate failure,
669 // path...
Brandon Wyman1d7a7df2020-03-26 10:14:05 -0500670 }
671
672 try
673 {
Brandon Wymanc9efe412020-10-09 15:42:50 -0500674 fwVersion =
675 pmbusIntf->readString(FW_VERSION, Type::HwmonDeviceDebug);
676 versionProps.emplace(VERSION_PROP, fwVersion);
Brandon Wyman1d7a7df2020-03-26 10:14:05 -0500677 }
Patrick Williamsc1d4de52021-10-06 12:45:57 -0500678 catch (const ReadFailure& e)
Brandon Wyman1d7a7df2020-03-26 10:14:05 -0500679 {
B. J. Wyman681b2a32021-04-20 22:31:22 +0000680 // Ignore the read failure, let pmbus code indicate failure,
681 // path...
Brandon Wyman1d7a7df2020-03-26 10:14:05 -0500682 }
683
684 ipzvpdVINIProps.emplace("CC",
685 std::vector<uint8_t>(ccin.begin(), ccin.end()));
686 ipzvpdVINIProps.emplace("PN",
687 std::vector<uint8_t>(pn.begin(), pn.end()));
688 ipzvpdVINIProps.emplace("FN",
689 std::vector<uint8_t>(fn.begin(), fn.end()));
690 std::string header_sn = header + sn + '\0';
691 ipzvpdVINIProps.emplace(
692 "SN", std::vector<uint8_t>(header_sn.begin(), header_sn.end()));
693 std::string description = "IBM PS";
694 ipzvpdVINIProps.emplace(
695 "DR", std::vector<uint8_t>(description.begin(), description.end()));
696
697 // Update the Resource Identifier (RI) keyword
698 // 2 byte FRC: 0x0003
699 // 2 byte RID: 0x1000, 0x1001...
700 std::uint8_t num = std::stoul(
701 inventoryPath.substr(inventoryPath.size() - 1, 1), nullptr, 0);
702 std::vector<uint8_t> ri{0x00, 0x03, 0x10, num};
703 ipzvpdDINFProps.emplace("RI", ri);
704
705 // Fill in the FRU Label (FL) keyword.
706 std::string fl = "E";
707 fl.push_back(inventoryPath.back());
708 fl.resize(FL_KW_SIZE, ' ');
709 ipzvpdDINFProps.emplace("FL",
710 std::vector<uint8_t>(fl.begin(), fl.end()));
711
712 interfaces.emplace(ASSET_IFACE, std::move(assetProps));
713 interfaces.emplace(VERSION_IFACE, std::move(versionProps));
714 interfaces.emplace(DINF_IFACE, std::move(ipzvpdDINFProps));
715 interfaces.emplace(VINI_IFACE, std::move(ipzvpdVINIProps));
716
George Liu070c1bc2020-10-12 11:28:01 +0800717 // Update the Functional
718 operProps.emplace(FUNCTIONAL_PROP, present);
719 interfaces.emplace(OPERATIONAL_STATE_IFACE, std::move(operProps));
720
Brandon Wyman1d7a7df2020-03-26 10:14:05 -0500721 auto path = inventoryPath.substr(strlen(INVENTORY_OBJ_PATH));
722 object.emplace(path, std::move(interfaces));
723
724 try
725 {
726 auto service =
727 util::getService(INVENTORY_OBJ_PATH, INVENTORY_MGR_IFACE, bus);
728
729 if (service.empty())
730 {
731 log<level::ERR>("Unable to get inventory manager service");
732 return;
733 }
734
735 auto method =
736 bus.new_method_call(service.c_str(), INVENTORY_OBJ_PATH,
737 INVENTORY_MGR_IFACE, "Notify");
738
739 method.append(std::move(object));
740
741 auto reply = bus.call(method);
742 }
Patrick Williamsc1d4de52021-10-06 12:45:57 -0500743 catch (const std::exception& e)
Brandon Wyman1d7a7df2020-03-26 10:14:05 -0500744 {
Jay Meyer6a3fd2c2020-08-25 16:37:16 -0500745 log<level::ERR>(
746 std::string(e.what() + std::string(" PATH=") + inventoryPath)
747 .c_str());
Brandon Wyman1d7a7df2020-03-26 10:14:05 -0500748 }
749#endif
750 }
751}
752
Adriana Kobylak4175ffb2021-08-02 14:51:05 +0000753void PowerSupply::getInputVoltage(double& actualInputVoltage,
754 int& inputVoltage) const
755{
756 using namespace phosphor::pmbus;
757
758 actualInputVoltage = in_input::VIN_VOLTAGE_0;
759 inputVoltage = in_input::VIN_VOLTAGE_0;
760
761 if (present)
762 {
763 try
764 {
765 // Read input voltage in millivolts
766 auto inputVoltageStr = pmbusIntf->readString(READ_VIN, Type::Hwmon);
767
768 // Convert to volts
769 actualInputVoltage = std::stod(inputVoltageStr) / 1000;
770
771 // Calculate the voltage based on voltage thresholds
772 if (actualInputVoltage < in_input::VIN_VOLTAGE_MIN)
773 {
774 inputVoltage = in_input::VIN_VOLTAGE_0;
775 }
776 else if (actualInputVoltage < in_input::VIN_VOLTAGE_110_THRESHOLD)
777 {
778 inputVoltage = in_input::VIN_VOLTAGE_110;
779 }
780 else
781 {
782 inputVoltage = in_input::VIN_VOLTAGE_220;
783 }
784 }
785 catch (const std::exception& e)
786 {
787 log<level::ERR>(
788 fmt::format("READ_VIN read error: {}", e.what()).c_str());
789 }
790 }
791}
792
Brandon Wyman3f1242f2020-01-28 13:11:25 -0600793} // namespace phosphor::power::psu