blob: 6fd412a18ec1cc089522af9c580dc213f716323e [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 Wyman993b5542021-12-21 22:55:16 +0000192void PowerSupply::analyzePgoodFault()
193{
194 if ((statusWord & phosphor::pmbus::status_word::POWER_GOOD_NEGATED) ||
195 (statusWord & phosphor::pmbus::status_word::UNIT_IS_OFF))
196 {
197 if (pgoodFault < DEGLITCH_LIMIT)
198 {
199 log<level::ERR>(fmt::format("PGOOD fault: "
200 "STATUS_WORD = {:#04x}, "
201 "STATUS_MFR_SPECIFIC = {:#02x}",
202 statusWord, statusMFR)
203 .c_str());
204
205 pgoodFault++;
206 }
207 }
208 else
209 {
210 pgoodFault = 0;
211 }
212}
213
Brandon Wyman39ea02b2021-11-23 23:22:23 +0000214void PowerSupply::determineMFRFault()
215{
216 if (bindPath.string().find("ibm-cffps") != std::string::npos)
217 {
218 // IBM MFR_SPECIFIC[4] is PS_Kill fault
219 if (statusMFR & 0x10)
220 {
221 psKillFault = true;
222 }
223 // IBM MFR_SPECIFIC[6] is 12Vcs fault.
224 if (statusMFR & 0x40)
225 {
226 ps12VcsFault = true;
227 }
228 // IBM MFR_SPECIFIC[7] is 12V Current-Share fault.
229 if (statusMFR & 0x80)
230 {
231 psCS12VFault = true;
232 }
233 }
234}
235
Brandon Wyman6c2ac392021-12-21 22:23:06 +0000236void PowerSupply::analyzeMFRFault()
237{
238 if (statusWord & phosphor::pmbus::status_word::MFR_SPECIFIC_FAULT)
239 {
240 if (!mfrFault)
241 {
242 log<level::ERR>(fmt::format("MFR fault: "
243 "STATUS_WORD = {:#04x} "
244 "STATUS_MFR_SPECIFIC = {:#02x}",
245 statusWord, statusMFR)
246 .c_str());
247 }
248
249 mfrFault = true;
250 determineMFRFault();
251 }
252}
253
Brandon Wyman3f1242f2020-01-28 13:11:25 -0600254void PowerSupply::analyze()
255{
256 using namespace phosphor::pmbus;
257
B. J. Wyman681b2a32021-04-20 22:31:22 +0000258 if (presenceGPIO)
259 {
260 updatePresenceGPIO();
261 }
262
Brandon Wymanf65c4062020-08-19 13:15:53 -0500263 if ((present) && (readFail < LOG_LIMIT))
Brandon Wyman3f1242f2020-01-28 13:11:25 -0600264 {
265 try
266 {
Brandon Wymanfed0ba22020-09-26 20:02:51 -0500267 statusWord = pmbusIntf->read(STATUS_WORD, Type::Debug);
Brandon Wymanf65c4062020-08-19 13:15:53 -0500268 // Read worked, reset the fail count.
269 readFail = 0;
Brandon Wyman3f1242f2020-01-28 13:11:25 -0600270
271 if (statusWord)
272 {
Brandon Wymanf07bc792021-10-12 19:00:35 +0000273 statusInput = pmbusIntf->read(STATUS_INPUT, Type::Debug);
Jay Meyer10d94052020-11-30 14:41:21 -0600274 statusMFR = pmbusIntf->read(STATUS_MFR, Type::Debug);
Brandon Wyman85c7bf42021-10-19 22:28:48 +0000275 statusCML = pmbusIntf->read(STATUS_CML, Type::Debug);
Brandon Wyman6710ba22021-10-27 17:39:31 +0000276 auto status0Vout = pmbusIntf->insertPageNum(STATUS_VOUT, 0);
277 statusVout = pmbusIntf->read(status0Vout, Type::Debug);
Brandon Wymanb10b3be2021-11-09 22:12:15 +0000278 statusIout = pmbusIntf->read(STATUS_IOUT, Type::Debug);
Brandon Wyman7ee4d7e2021-11-19 20:48:23 +0000279 statusFans12 = pmbusIntf->read(STATUS_FANS_1_2, Type::Debug);
Brandon Wyman96893a42021-11-05 19:56:57 +0000280 statusTemperature =
281 pmbusIntf->read(STATUS_TEMPERATURE, Type::Debug);
Brandon Wyman85c7bf42021-10-19 22:28:48 +0000282 if (statusWord & status_word::CML_FAULT)
283 {
284 if (!cmlFault)
285 {
Brandon Wyman43d32632021-10-26 23:44:11 +0000286 log<level::ERR>(
Brandon Wyman85c7bf42021-10-19 22:28:48 +0000287 fmt::format("CML fault: STATUS_WORD = {:#04x}, "
288 "STATUS_CML = {:#02x}",
289 statusWord, statusCML)
290 .c_str());
291 }
Brandon Wyman9ddc6222021-10-28 17:28:01 +0000292
Brandon Wyman85c7bf42021-10-19 22:28:48 +0000293 cmlFault = true;
294 }
295
Brandon Wyman3f1242f2020-01-28 13:11:25 -0600296 if (statusWord & status_word::INPUT_FAULT_WARN)
297 {
298 if (!inputFault)
299 {
Brandon Wyman43d32632021-10-26 23:44:11 +0000300 log<level::ERR>(
Brandon Wymanf07bc792021-10-12 19:00:35 +0000301 fmt::format("INPUT fault: STATUS_WORD = {:#04x}, "
302 "STATUS_MFR_SPECIFIC = {:#02x}, "
303 "STATUS_INPUT = {:#02x}",
304 statusWord, statusMFR, statusInput)
Brandon Wymanc8996602021-10-12 19:28:56 +0000305 .c_str());
Brandon Wyman3f1242f2020-01-28 13:11:25 -0600306 }
307
Brandon Wyman3f1242f2020-01-28 13:11:25 -0600308 inputFault = true;
309 }
310
Brandon Wyman6710ba22021-10-27 17:39:31 +0000311 if (statusWord & status_word::VOUT_OV_FAULT)
312 {
313 if (!voutOVFault)
314 {
Brandon Wyman43d32632021-10-26 23:44:11 +0000315 log<level::ERR>(
Brandon Wyman2cf46942021-10-28 19:09:16 +0000316 fmt::format(
317 "VOUT_OV_FAULT fault: STATUS_WORD = {:#04x}, "
318 "STATUS_MFR_SPECIFIC = {:#02x}, "
319 "STATUS_VOUT = {:#02x}",
320 statusWord, statusMFR, statusVout)
Brandon Wyman6710ba22021-10-27 17:39:31 +0000321 .c_str());
322 }
Brandon Wyman9ddc6222021-10-28 17:28:01 +0000323
Brandon Wyman6710ba22021-10-27 17:39:31 +0000324 voutOVFault = true;
325 }
326
Brandon Wymanb10b3be2021-11-09 22:12:15 +0000327 if (statusWord & status_word::IOUT_OC_FAULT)
328 {
329 if (!ioutOCFault)
330 {
331 log<level::ERR>(
332 fmt::format("IOUT fault: STATUS_WORD = {:#04x}, "
333 "STATUS_MFR_SPECIFIC = {:#02x}, "
334 "STATUS_IOUT = {:#02x}",
335 statusWord, statusMFR, statusIout)
336 .c_str());
337 }
338
339 ioutOCFault = true;
340 }
341
Brandon Wyman2cf46942021-10-28 19:09:16 +0000342 if ((statusWord & status_word::VOUT_FAULT) &&
343 !(statusWord & status_word::VOUT_OV_FAULT))
344 {
345 if (!voutUVFault)
346 {
347 log<level::ERR>(
348 fmt::format(
349 "VOUT_UV_FAULT fault: STATUS_WORD = {:#04x}, "
350 "STATUS_MFR_SPECIFIC = {:#02x}, "
351 "STATUS_VOUT = {:#02x}",
352 statusWord, statusMFR, statusVout)
353 .c_str());
354 }
355
356 voutUVFault = true;
357 }
358
Brandon Wyman7ee4d7e2021-11-19 20:48:23 +0000359 if (statusWord & status_word::FAN_FAULT)
360 {
361 if (!fanFault)
362 {
363 log<level::ERR>(
364 fmt::format("FANS fault/warning: "
365 "STATUS_WORD = {:#04x}, "
366 "STATUS_MFR_SPECIFIC = {:#02x}, "
367 "STATUS_FANS_1_2 = {:#02x}",
368 statusWord, statusMFR, statusFans12)
369 .c_str());
370 }
371
372 fanFault = true;
373 }
374
Brandon Wyman96893a42021-11-05 19:56:57 +0000375 if (statusWord & status_word::TEMPERATURE_FAULT_WARN)
376 {
377 if (!tempFault)
378 {
379 log<level::ERR>(
380 fmt::format("TEMPERATURE fault/warning: "
381 "STATUS_WORD = {:#04x}, "
382 "STATUS_MFR_SPECIFIC = {:#02x}, "
383 "STATUS_TEMPERATURE = {:#02x}",
384 statusWord, statusMFR,
385 statusTemperature)
386 .c_str());
387 }
388
389 tempFault = true;
390 }
391
Brandon Wyman993b5542021-12-21 22:55:16 +0000392 analyzePgoodFault();
Brandon Wyman2916ea52021-11-06 03:31:18 +0000393
Brandon Wyman6c2ac392021-12-21 22:23:06 +0000394 analyzeMFRFault();
Brandon Wyman3f1242f2020-01-28 13:11:25 -0600395
396 if (statusWord & status_word::VIN_UV_FAULT)
397 {
398 if (!vinUVFault)
399 {
Brandon Wyman43d32632021-10-26 23:44:11 +0000400 log<level::ERR>(
Brandon Wymanf07bc792021-10-12 19:00:35 +0000401 fmt::format("VIN_UV fault: STATUS_WORD = {:#04x}, "
402 "STATUS_MFR_SPECIFIC = {:#02x}, "
403 "STATUS_INPUT = {:#02x}",
404 statusWord, statusMFR, statusInput)
Brandon Wymanc8996602021-10-12 19:28:56 +0000405 .c_str());
Brandon Wyman3f1242f2020-01-28 13:11:25 -0600406 }
407
Brandon Wyman3f1242f2020-01-28 13:11:25 -0600408 vinUVFault = true;
409 }
410 }
411 else
412 {
Brandon Wymane3f7ad22021-12-21 20:27:45 +0000413 // if INPUT/VIN_UV fault was on, it cleared, trace it.
414 if (inputFault)
415 {
416 log<level::INFO>(
417 fmt::format(
418 "INPUT fault cleared: STATUS_WORD = {:#04x}",
419 statusWord)
420 .c_str());
421 }
422
423 if (vinUVFault)
424 {
425 log<level::INFO>(
426 fmt::format("VIN_UV cleared: STATUS_WORD = {:#04x}",
427 statusWord)
428 .c_str());
429 }
430
Brandon Wyman06ca4592021-12-06 22:52:23 +0000431 if (pgoodFault > 0)
Brandon Wyman4aecc292021-11-10 22:40:41 +0000432 {
433 log<level::INFO>(fmt::format("pgoodFault cleared path: {}",
434 inventoryPath)
435 .c_str());
Brandon Wyman4aecc292021-11-10 22:40:41 +0000436 }
Brandon Wymane3f7ad22021-12-21 20:27:45 +0000437
438 clearFaultFlags();
Brandon Wyman3f1242f2020-01-28 13:11:25 -0600439 }
440 }
Patrick Williamsc1d4de52021-10-06 12:45:57 -0500441 catch (const ReadFailure& e)
Brandon Wyman3f1242f2020-01-28 13:11:25 -0600442 {
Brandon Wymanf65c4062020-08-19 13:15:53 -0500443 readFail++;
Brandon Wyman3f1242f2020-01-28 13:11:25 -0600444 phosphor::logging::commit<ReadFailure>();
445 }
446 }
447}
448
Brandon Wyman59a35792020-06-04 12:37:40 -0500449void PowerSupply::onOffConfig(uint8_t data)
450{
451 using namespace phosphor::pmbus;
452
453 if (present)
454 {
455 log<level::INFO>("ON_OFF_CONFIG write", entry("DATA=0x%02X", data));
456 try
457 {
458 std::vector<uint8_t> configData{data};
459 pmbusIntf->writeBinary(ON_OFF_CONFIG, configData,
460 Type::HwmonDeviceDebug);
461 }
462 catch (...)
463 {
464 // The underlying code in writeBinary will log a message to the
B. J. Wyman681b2a32021-04-20 22:31:22 +0000465 // journal if the write fails. If the ON_OFF_CONFIG is not setup
466 // as desired, later fault detection and analysis code should
467 // catch any of the fall out. We should not need to terminate
468 // the application if this write fails.
Brandon Wyman59a35792020-06-04 12:37:40 -0500469 }
470 }
471}
472
Brandon Wyman3c208462020-05-13 16:25:58 -0500473void PowerSupply::clearFaults()
474{
Brandon Wyman5474c912021-02-23 14:39:43 -0600475 faultLogged = false;
Brandon Wyman3c208462020-05-13 16:25:58 -0500476 // The PMBus device driver does not allow for writing CLEAR_FAULTS
477 // directly. However, the pmbus hwmon device driver code will send a
478 // CLEAR_FAULTS after reading from any of the hwmon "files" in sysfs, so
479 // reading in1_input should result in clearing the fault bits in
480 // STATUS_BYTE/STATUS_WORD.
481 // I do not care what the return value is.
Brandon Wyman11151532020-11-10 13:45:57 -0600482 if (present)
Brandon Wyman3c208462020-05-13 16:25:58 -0500483 {
Brandon Wymane3f7ad22021-12-21 20:27:45 +0000484 clearFaultFlags();
Brandon Wyman9564e942020-11-10 14:01:42 -0600485 readFail = 0;
Brandon Wyman9564e942020-11-10 14:01:42 -0600486
Brandon Wyman11151532020-11-10 13:45:57 -0600487 try
488 {
489 static_cast<void>(
490 pmbusIntf->read("in1_input", phosphor::pmbus::Type::Hwmon));
491 }
Patrick Williamsc1d4de52021-10-06 12:45:57 -0500492 catch (const ReadFailure& e)
Brandon Wyman11151532020-11-10 13:45:57 -0600493 {
494 // Since I do not care what the return value is, I really do not
B. J. Wyman681b2a32021-04-20 22:31:22 +0000495 // care much if it gets a ReadFailure either. However, this
496 // should not prevent the application from continuing to run, so
497 // catching the read failure.
Brandon Wyman11151532020-11-10 13:45:57 -0600498 }
Brandon Wyman3c208462020-05-13 16:25:58 -0500499 }
500}
501
Brandon Wymanaed1f752019-11-25 18:10:52 -0600502void PowerSupply::inventoryChanged(sdbusplus::message::message& msg)
503{
504 std::string msgSensor;
Patrick Williamsabe49412020-05-13 17:59:47 -0500505 std::map<std::string, std::variant<uint32_t, bool>> msgData;
Brandon Wymanaed1f752019-11-25 18:10:52 -0600506 msg.read(msgSensor, msgData);
507
508 // Check if it was the Present property that changed.
509 auto valPropMap = msgData.find(PRESENT_PROP);
510 if (valPropMap != msgData.end())
511 {
512 if (std::get<bool>(valPropMap->second))
513 {
514 present = true;
B. J. Wyman681b2a32021-04-20 22:31:22 +0000515 // TODO: Immediately trying to read or write the "files" causes
516 // read or write failures.
Brandon Wyman1d7a7df2020-03-26 10:14:05 -0500517 using namespace std::chrono_literals;
518 std::this_thread::sleep_for(20ms);
Brandon Wyman9564e942020-11-10 14:01:42 -0600519 pmbusIntf->findHwmonDir();
Brandon Wyman59a35792020-06-04 12:37:40 -0500520 onOffConfig(phosphor::pmbus::ON_OFF_CONFIG_CONTROL_PIN_ONLY);
Brandon Wymanaed1f752019-11-25 18:10:52 -0600521 clearFaults();
Brandon Wyman1d7a7df2020-03-26 10:14:05 -0500522 updateInventory();
Brandon Wymanaed1f752019-11-25 18:10:52 -0600523 }
524 else
525 {
526 present = false;
527
528 // Clear out the now outdated inventory properties
529 updateInventory();
530 }
531 }
532}
533
Brandon Wyman9a507db2021-02-25 16:15:22 -0600534void PowerSupply::inventoryAdded(sdbusplus::message::message& msg)
535{
536 sdbusplus::message::object_path path;
537 msg.read(path);
538 // Make sure the signal is for the PSU inventory path
539 if (path == inventoryPath)
540 {
541 std::map<std::string, std::map<std::string, std::variant<bool>>>
542 interfaces;
543 // Get map of interfaces and their properties
544 msg.read(interfaces);
545
546 auto properties = interfaces.find(INVENTORY_IFACE);
547 if (properties != interfaces.end())
548 {
549 auto property = properties->second.find(PRESENT_PROP);
550 if (property != properties->second.end())
551 {
552 present = std::get<bool>(property->second);
553
554 log<level::INFO>(fmt::format("Power Supply {} Present {}",
555 inventoryPath, present)
556 .c_str());
557
558 updateInventory();
559 }
560 }
561 }
562}
563
Brandon Wyman1d7a7df2020-03-26 10:14:05 -0500564void PowerSupply::updateInventory()
565{
566 using namespace phosphor::pmbus;
567
Chanh Nguyenc12c53b2021-04-06 17:24:47 +0700568#if IBM_VPD
Brandon Wyman1d7a7df2020-03-26 10:14:05 -0500569 std::string ccin;
570 std::string pn;
571 std::string fn;
572 std::string header;
573 std::string sn;
Brandon Wyman1d7a7df2020-03-26 10:14:05 -0500574 using PropertyMap =
George Liu070c1bc2020-10-12 11:28:01 +0800575 std::map<std::string,
576 std::variant<std::string, std::vector<uint8_t>, bool>>;
Brandon Wyman1d7a7df2020-03-26 10:14:05 -0500577 PropertyMap assetProps;
George Liu070c1bc2020-10-12 11:28:01 +0800578 PropertyMap operProps;
Brandon Wyman1d7a7df2020-03-26 10:14:05 -0500579 PropertyMap versionProps;
580 PropertyMap ipzvpdDINFProps;
581 PropertyMap ipzvpdVINIProps;
582 using InterfaceMap = std::map<std::string, PropertyMap>;
583 InterfaceMap interfaces;
584 using ObjectMap = std::map<sdbusplus::message::object_path, InterfaceMap>;
585 ObjectMap object;
586#endif
B. J. Wyman681b2a32021-04-20 22:31:22 +0000587 log<level::DEBUG>(
588 fmt::format("updateInventory() inventoryPath: {}", inventoryPath)
589 .c_str());
Brandon Wyman1d7a7df2020-03-26 10:14:05 -0500590
591 if (present)
592 {
593 // TODO: non-IBM inventory updates?
594
Chanh Nguyenc12c53b2021-04-06 17:24:47 +0700595#if IBM_VPD
Brandon Wyman1d7a7df2020-03-26 10:14:05 -0500596 try
597 {
598 ccin = pmbusIntf->readString(CCIN, Type::HwmonDeviceDebug);
599 assetProps.emplace(MODEL_PROP, ccin);
Adriana Kobylak572a9052021-03-30 15:58:07 +0000600 modelName = ccin;
Brandon Wyman1d7a7df2020-03-26 10:14:05 -0500601 }
Patrick Williamsc1d4de52021-10-06 12:45:57 -0500602 catch (const ReadFailure& e)
Brandon Wyman1d7a7df2020-03-26 10:14:05 -0500603 {
B. J. Wyman681b2a32021-04-20 22:31:22 +0000604 // Ignore the read failure, let pmbus code indicate failure,
605 // path...
Brandon Wyman1d7a7df2020-03-26 10:14:05 -0500606 // TODO - ibm918
607 // https://github.com/openbmc/docs/blob/master/designs/vpd-collection.md
608 // The BMC must log errors if any of the VPD cannot be properly
609 // parsed or fails ECC checks.
610 }
611
612 try
613 {
614 pn = pmbusIntf->readString(PART_NUMBER, Type::HwmonDeviceDebug);
615 assetProps.emplace(PN_PROP, pn);
616 }
Patrick Williamsc1d4de52021-10-06 12:45:57 -0500617 catch (const ReadFailure& e)
Brandon Wyman1d7a7df2020-03-26 10:14:05 -0500618 {
B. J. Wyman681b2a32021-04-20 22:31:22 +0000619 // Ignore the read failure, let pmbus code indicate failure,
620 // path...
Brandon Wyman1d7a7df2020-03-26 10:14:05 -0500621 }
622
623 try
624 {
625 fn = pmbusIntf->readString(FRU_NUMBER, Type::HwmonDeviceDebug);
Brandon Wymana169b0f2021-12-07 20:18:06 +0000626 assetProps.emplace(SPARE_PN_PROP, fn);
Brandon Wyman1d7a7df2020-03-26 10:14:05 -0500627 }
Patrick Williamsc1d4de52021-10-06 12:45:57 -0500628 catch (const ReadFailure& e)
Brandon Wyman1d7a7df2020-03-26 10:14:05 -0500629 {
B. J. Wyman681b2a32021-04-20 22:31:22 +0000630 // Ignore the read failure, let pmbus code indicate failure,
631 // path...
Brandon Wyman1d7a7df2020-03-26 10:14:05 -0500632 }
633
634 try
635 {
636 header =
637 pmbusIntf->readString(SERIAL_HEADER, Type::HwmonDeviceDebug);
638 sn = pmbusIntf->readString(SERIAL_NUMBER, Type::HwmonDeviceDebug);
639 assetProps.emplace(SN_PROP, sn);
640 }
Patrick Williamsc1d4de52021-10-06 12:45:57 -0500641 catch (const ReadFailure& e)
Brandon Wyman1d7a7df2020-03-26 10:14:05 -0500642 {
B. J. Wyman681b2a32021-04-20 22:31:22 +0000643 // Ignore the read failure, let pmbus code indicate failure,
644 // path...
Brandon Wyman1d7a7df2020-03-26 10:14:05 -0500645 }
646
647 try
648 {
Brandon Wymanc9efe412020-10-09 15:42:50 -0500649 fwVersion =
650 pmbusIntf->readString(FW_VERSION, Type::HwmonDeviceDebug);
651 versionProps.emplace(VERSION_PROP, fwVersion);
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 ipzvpdVINIProps.emplace("CC",
660 std::vector<uint8_t>(ccin.begin(), ccin.end()));
661 ipzvpdVINIProps.emplace("PN",
662 std::vector<uint8_t>(pn.begin(), pn.end()));
663 ipzvpdVINIProps.emplace("FN",
664 std::vector<uint8_t>(fn.begin(), fn.end()));
665 std::string header_sn = header + sn + '\0';
666 ipzvpdVINIProps.emplace(
667 "SN", std::vector<uint8_t>(header_sn.begin(), header_sn.end()));
668 std::string description = "IBM PS";
669 ipzvpdVINIProps.emplace(
670 "DR", std::vector<uint8_t>(description.begin(), description.end()));
671
672 // Update the Resource Identifier (RI) keyword
673 // 2 byte FRC: 0x0003
674 // 2 byte RID: 0x1000, 0x1001...
675 std::uint8_t num = std::stoul(
676 inventoryPath.substr(inventoryPath.size() - 1, 1), nullptr, 0);
677 std::vector<uint8_t> ri{0x00, 0x03, 0x10, num};
678 ipzvpdDINFProps.emplace("RI", ri);
679
680 // Fill in the FRU Label (FL) keyword.
681 std::string fl = "E";
682 fl.push_back(inventoryPath.back());
683 fl.resize(FL_KW_SIZE, ' ');
684 ipzvpdDINFProps.emplace("FL",
685 std::vector<uint8_t>(fl.begin(), fl.end()));
686
687 interfaces.emplace(ASSET_IFACE, std::move(assetProps));
688 interfaces.emplace(VERSION_IFACE, std::move(versionProps));
689 interfaces.emplace(DINF_IFACE, std::move(ipzvpdDINFProps));
690 interfaces.emplace(VINI_IFACE, std::move(ipzvpdVINIProps));
691
George Liu070c1bc2020-10-12 11:28:01 +0800692 // Update the Functional
693 operProps.emplace(FUNCTIONAL_PROP, present);
694 interfaces.emplace(OPERATIONAL_STATE_IFACE, std::move(operProps));
695
Brandon Wyman1d7a7df2020-03-26 10:14:05 -0500696 auto path = inventoryPath.substr(strlen(INVENTORY_OBJ_PATH));
697 object.emplace(path, std::move(interfaces));
698
699 try
700 {
701 auto service =
702 util::getService(INVENTORY_OBJ_PATH, INVENTORY_MGR_IFACE, bus);
703
704 if (service.empty())
705 {
706 log<level::ERR>("Unable to get inventory manager service");
707 return;
708 }
709
710 auto method =
711 bus.new_method_call(service.c_str(), INVENTORY_OBJ_PATH,
712 INVENTORY_MGR_IFACE, "Notify");
713
714 method.append(std::move(object));
715
716 auto reply = bus.call(method);
717 }
Patrick Williamsc1d4de52021-10-06 12:45:57 -0500718 catch (const std::exception& e)
Brandon Wyman1d7a7df2020-03-26 10:14:05 -0500719 {
Jay Meyer6a3fd2c2020-08-25 16:37:16 -0500720 log<level::ERR>(
721 std::string(e.what() + std::string(" PATH=") + inventoryPath)
722 .c_str());
Brandon Wyman1d7a7df2020-03-26 10:14:05 -0500723 }
724#endif
725 }
726}
727
Adriana Kobylak4175ffb2021-08-02 14:51:05 +0000728void PowerSupply::getInputVoltage(double& actualInputVoltage,
729 int& inputVoltage) const
730{
731 using namespace phosphor::pmbus;
732
733 actualInputVoltage = in_input::VIN_VOLTAGE_0;
734 inputVoltage = in_input::VIN_VOLTAGE_0;
735
736 if (present)
737 {
738 try
739 {
740 // Read input voltage in millivolts
741 auto inputVoltageStr = pmbusIntf->readString(READ_VIN, Type::Hwmon);
742
743 // Convert to volts
744 actualInputVoltage = std::stod(inputVoltageStr) / 1000;
745
746 // Calculate the voltage based on voltage thresholds
747 if (actualInputVoltage < in_input::VIN_VOLTAGE_MIN)
748 {
749 inputVoltage = in_input::VIN_VOLTAGE_0;
750 }
751 else if (actualInputVoltage < in_input::VIN_VOLTAGE_110_THRESHOLD)
752 {
753 inputVoltage = in_input::VIN_VOLTAGE_110;
754 }
755 else
756 {
757 inputVoltage = in_input::VIN_VOLTAGE_220;
758 }
759 }
760 catch (const std::exception& e)
761 {
762 log<level::ERR>(
763 fmt::format("READ_VIN read error: {}", e.what()).c_str());
764 }
765 }
766}
767
Brandon Wyman3f1242f2020-01-28 13:11:25 -0600768} // namespace phosphor::power::psu