blob: b5eeb2ff223477c439af91f2212d333e8121e1cd [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 Wyman52cb3f22021-12-21 23:02:47 +0000192void PowerSupply::analyzeTemperatureFault()
193{
194 if (statusWord & phosphor::pmbus::status_word::TEMPERATURE_FAULT_WARN)
195 {
196 if (!tempFault)
197 {
198 log<level::ERR>(fmt::format("TEMPERATURE fault/warning: "
199 "STATUS_WORD = {:#04x}, "
200 "STATUS_MFR_SPECIFIC = {:#02x}, "
201 "STATUS_TEMPERATURE = {:#02x}",
202 statusWord, statusMFR,
203 statusTemperature)
204 .c_str());
205 }
206
207 tempFault = true;
208 }
209}
210
Brandon Wyman993b5542021-12-21 22:55:16 +0000211void PowerSupply::analyzePgoodFault()
212{
213 if ((statusWord & phosphor::pmbus::status_word::POWER_GOOD_NEGATED) ||
214 (statusWord & phosphor::pmbus::status_word::UNIT_IS_OFF))
215 {
216 if (pgoodFault < DEGLITCH_LIMIT)
217 {
218 log<level::ERR>(fmt::format("PGOOD fault: "
219 "STATUS_WORD = {:#04x}, "
220 "STATUS_MFR_SPECIFIC = {:#02x}",
221 statusWord, statusMFR)
222 .c_str());
223
224 pgoodFault++;
225 }
226 }
227 else
228 {
229 pgoodFault = 0;
230 }
231}
232
Brandon Wyman39ea02b2021-11-23 23:22:23 +0000233void PowerSupply::determineMFRFault()
234{
235 if (bindPath.string().find("ibm-cffps") != std::string::npos)
236 {
237 // IBM MFR_SPECIFIC[4] is PS_Kill fault
238 if (statusMFR & 0x10)
239 {
240 psKillFault = true;
241 }
242 // IBM MFR_SPECIFIC[6] is 12Vcs fault.
243 if (statusMFR & 0x40)
244 {
245 ps12VcsFault = true;
246 }
247 // IBM MFR_SPECIFIC[7] is 12V Current-Share fault.
248 if (statusMFR & 0x80)
249 {
250 psCS12VFault = true;
251 }
252 }
253}
254
Brandon Wyman6c2ac392021-12-21 22:23:06 +0000255void PowerSupply::analyzeMFRFault()
256{
257 if (statusWord & phosphor::pmbus::status_word::MFR_SPECIFIC_FAULT)
258 {
259 if (!mfrFault)
260 {
261 log<level::ERR>(fmt::format("MFR fault: "
262 "STATUS_WORD = {:#04x} "
263 "STATUS_MFR_SPECIFIC = {:#02x}",
264 statusWord, statusMFR)
265 .c_str());
266 }
267
268 mfrFault = true;
269 determineMFRFault();
270 }
271}
272
Brandon Wyman3f1242f2020-01-28 13:11:25 -0600273void PowerSupply::analyze()
274{
275 using namespace phosphor::pmbus;
276
B. J. Wyman681b2a32021-04-20 22:31:22 +0000277 if (presenceGPIO)
278 {
279 updatePresenceGPIO();
280 }
281
Brandon Wymanf65c4062020-08-19 13:15:53 -0500282 if ((present) && (readFail < LOG_LIMIT))
Brandon Wyman3f1242f2020-01-28 13:11:25 -0600283 {
284 try
285 {
Brandon Wymanfed0ba22020-09-26 20:02:51 -0500286 statusWord = pmbusIntf->read(STATUS_WORD, Type::Debug);
Brandon Wymanf65c4062020-08-19 13:15:53 -0500287 // Read worked, reset the fail count.
288 readFail = 0;
Brandon Wyman3f1242f2020-01-28 13:11:25 -0600289
290 if (statusWord)
291 {
Brandon Wymanf07bc792021-10-12 19:00:35 +0000292 statusInput = pmbusIntf->read(STATUS_INPUT, Type::Debug);
Jay Meyer10d94052020-11-30 14:41:21 -0600293 statusMFR = pmbusIntf->read(STATUS_MFR, Type::Debug);
Brandon Wyman85c7bf42021-10-19 22:28:48 +0000294 statusCML = pmbusIntf->read(STATUS_CML, Type::Debug);
Brandon Wyman6710ba22021-10-27 17:39:31 +0000295 auto status0Vout = pmbusIntf->insertPageNum(STATUS_VOUT, 0);
296 statusVout = pmbusIntf->read(status0Vout, Type::Debug);
Brandon Wymanb10b3be2021-11-09 22:12:15 +0000297 statusIout = pmbusIntf->read(STATUS_IOUT, Type::Debug);
Brandon Wyman7ee4d7e2021-11-19 20:48:23 +0000298 statusFans12 = pmbusIntf->read(STATUS_FANS_1_2, Type::Debug);
Brandon Wyman96893a42021-11-05 19:56:57 +0000299 statusTemperature =
300 pmbusIntf->read(STATUS_TEMPERATURE, Type::Debug);
Brandon Wyman85c7bf42021-10-19 22:28:48 +0000301 if (statusWord & status_word::CML_FAULT)
302 {
303 if (!cmlFault)
304 {
Brandon Wyman43d32632021-10-26 23:44:11 +0000305 log<level::ERR>(
Brandon Wyman85c7bf42021-10-19 22:28:48 +0000306 fmt::format("CML fault: STATUS_WORD = {:#04x}, "
307 "STATUS_CML = {:#02x}",
308 statusWord, statusCML)
309 .c_str());
310 }
Brandon Wyman9ddc6222021-10-28 17:28:01 +0000311
Brandon Wyman85c7bf42021-10-19 22:28:48 +0000312 cmlFault = true;
313 }
314
Brandon Wyman3f1242f2020-01-28 13:11:25 -0600315 if (statusWord & status_word::INPUT_FAULT_WARN)
316 {
317 if (!inputFault)
318 {
Brandon Wyman43d32632021-10-26 23:44:11 +0000319 log<level::ERR>(
Brandon Wymanf07bc792021-10-12 19:00:35 +0000320 fmt::format("INPUT fault: STATUS_WORD = {:#04x}, "
321 "STATUS_MFR_SPECIFIC = {:#02x}, "
322 "STATUS_INPUT = {:#02x}",
323 statusWord, statusMFR, statusInput)
Brandon Wymanc8996602021-10-12 19:28:56 +0000324 .c_str());
Brandon Wyman3f1242f2020-01-28 13:11:25 -0600325 }
326
Brandon Wyman3f1242f2020-01-28 13:11:25 -0600327 inputFault = true;
328 }
329
Brandon Wyman6710ba22021-10-27 17:39:31 +0000330 if (statusWord & status_word::VOUT_OV_FAULT)
331 {
332 if (!voutOVFault)
333 {
Brandon Wyman43d32632021-10-26 23:44:11 +0000334 log<level::ERR>(
Brandon Wyman2cf46942021-10-28 19:09:16 +0000335 fmt::format(
336 "VOUT_OV_FAULT fault: STATUS_WORD = {:#04x}, "
337 "STATUS_MFR_SPECIFIC = {:#02x}, "
338 "STATUS_VOUT = {:#02x}",
339 statusWord, statusMFR, statusVout)
Brandon Wyman6710ba22021-10-27 17:39:31 +0000340 .c_str());
341 }
Brandon Wyman9ddc6222021-10-28 17:28:01 +0000342
Brandon Wyman6710ba22021-10-27 17:39:31 +0000343 voutOVFault = true;
344 }
345
Brandon Wymanb10b3be2021-11-09 22:12:15 +0000346 if (statusWord & status_word::IOUT_OC_FAULT)
347 {
348 if (!ioutOCFault)
349 {
350 log<level::ERR>(
351 fmt::format("IOUT fault: STATUS_WORD = {:#04x}, "
352 "STATUS_MFR_SPECIFIC = {:#02x}, "
353 "STATUS_IOUT = {:#02x}",
354 statusWord, statusMFR, statusIout)
355 .c_str());
356 }
357
358 ioutOCFault = true;
359 }
360
Brandon Wyman2cf46942021-10-28 19:09:16 +0000361 if ((statusWord & status_word::VOUT_FAULT) &&
362 !(statusWord & status_word::VOUT_OV_FAULT))
363 {
364 if (!voutUVFault)
365 {
366 log<level::ERR>(
367 fmt::format(
368 "VOUT_UV_FAULT fault: STATUS_WORD = {:#04x}, "
369 "STATUS_MFR_SPECIFIC = {:#02x}, "
370 "STATUS_VOUT = {:#02x}",
371 statusWord, statusMFR, statusVout)
372 .c_str());
373 }
374
375 voutUVFault = true;
376 }
377
Brandon Wyman7ee4d7e2021-11-19 20:48:23 +0000378 if (statusWord & status_word::FAN_FAULT)
379 {
380 if (!fanFault)
381 {
382 log<level::ERR>(
383 fmt::format("FANS fault/warning: "
384 "STATUS_WORD = {:#04x}, "
385 "STATUS_MFR_SPECIFIC = {:#02x}, "
386 "STATUS_FANS_1_2 = {:#02x}",
387 statusWord, statusMFR, statusFans12)
388 .c_str());
389 }
390
391 fanFault = true;
392 }
393
Brandon Wyman52cb3f22021-12-21 23:02:47 +0000394 analyzeTemperatureFault();
Brandon Wyman96893a42021-11-05 19:56:57 +0000395
Brandon Wyman993b5542021-12-21 22:55:16 +0000396 analyzePgoodFault();
Brandon Wyman2916ea52021-11-06 03:31:18 +0000397
Brandon Wyman6c2ac392021-12-21 22:23:06 +0000398 analyzeMFRFault();
Brandon Wyman3f1242f2020-01-28 13:11:25 -0600399
400 if (statusWord & status_word::VIN_UV_FAULT)
401 {
402 if (!vinUVFault)
403 {
Brandon Wyman43d32632021-10-26 23:44:11 +0000404 log<level::ERR>(
Brandon Wymanf07bc792021-10-12 19:00:35 +0000405 fmt::format("VIN_UV fault: STATUS_WORD = {:#04x}, "
406 "STATUS_MFR_SPECIFIC = {:#02x}, "
407 "STATUS_INPUT = {:#02x}",
408 statusWord, statusMFR, statusInput)
Brandon Wymanc8996602021-10-12 19:28:56 +0000409 .c_str());
Brandon Wyman3f1242f2020-01-28 13:11:25 -0600410 }
411
Brandon Wyman3f1242f2020-01-28 13:11:25 -0600412 vinUVFault = true;
413 }
414 }
415 else
416 {
Brandon Wymane3f7ad22021-12-21 20:27:45 +0000417 // if INPUT/VIN_UV fault was on, it cleared, trace it.
418 if (inputFault)
419 {
420 log<level::INFO>(
421 fmt::format(
422 "INPUT fault cleared: STATUS_WORD = {:#04x}",
423 statusWord)
424 .c_str());
425 }
426
427 if (vinUVFault)
428 {
429 log<level::INFO>(
430 fmt::format("VIN_UV cleared: STATUS_WORD = {:#04x}",
431 statusWord)
432 .c_str());
433 }
434
Brandon Wyman06ca4592021-12-06 22:52:23 +0000435 if (pgoodFault > 0)
Brandon Wyman4aecc292021-11-10 22:40:41 +0000436 {
437 log<level::INFO>(fmt::format("pgoodFault cleared path: {}",
438 inventoryPath)
439 .c_str());
Brandon Wyman4aecc292021-11-10 22:40:41 +0000440 }
Brandon Wymane3f7ad22021-12-21 20:27:45 +0000441
442 clearFaultFlags();
Brandon Wyman3f1242f2020-01-28 13:11:25 -0600443 }
444 }
Patrick Williamsc1d4de52021-10-06 12:45:57 -0500445 catch (const ReadFailure& e)
Brandon Wyman3f1242f2020-01-28 13:11:25 -0600446 {
Brandon Wymanf65c4062020-08-19 13:15:53 -0500447 readFail++;
Brandon Wyman3f1242f2020-01-28 13:11:25 -0600448 phosphor::logging::commit<ReadFailure>();
449 }
450 }
451}
452
Brandon Wyman59a35792020-06-04 12:37:40 -0500453void PowerSupply::onOffConfig(uint8_t data)
454{
455 using namespace phosphor::pmbus;
456
457 if (present)
458 {
459 log<level::INFO>("ON_OFF_CONFIG write", entry("DATA=0x%02X", data));
460 try
461 {
462 std::vector<uint8_t> configData{data};
463 pmbusIntf->writeBinary(ON_OFF_CONFIG, configData,
464 Type::HwmonDeviceDebug);
465 }
466 catch (...)
467 {
468 // The underlying code in writeBinary will log a message to the
B. J. Wyman681b2a32021-04-20 22:31:22 +0000469 // journal if the write fails. If the ON_OFF_CONFIG is not setup
470 // as desired, later fault detection and analysis code should
471 // catch any of the fall out. We should not need to terminate
472 // the application if this write fails.
Brandon Wyman59a35792020-06-04 12:37:40 -0500473 }
474 }
475}
476
Brandon Wyman3c208462020-05-13 16:25:58 -0500477void PowerSupply::clearFaults()
478{
Brandon Wyman5474c912021-02-23 14:39:43 -0600479 faultLogged = false;
Brandon Wyman3c208462020-05-13 16:25:58 -0500480 // The PMBus device driver does not allow for writing CLEAR_FAULTS
481 // directly. However, the pmbus hwmon device driver code will send a
482 // CLEAR_FAULTS after reading from any of the hwmon "files" in sysfs, so
483 // reading in1_input should result in clearing the fault bits in
484 // STATUS_BYTE/STATUS_WORD.
485 // I do not care what the return value is.
Brandon Wyman11151532020-11-10 13:45:57 -0600486 if (present)
Brandon Wyman3c208462020-05-13 16:25:58 -0500487 {
Brandon Wymane3f7ad22021-12-21 20:27:45 +0000488 clearFaultFlags();
Brandon Wyman9564e942020-11-10 14:01:42 -0600489 readFail = 0;
Brandon Wyman9564e942020-11-10 14:01:42 -0600490
Brandon Wyman11151532020-11-10 13:45:57 -0600491 try
492 {
493 static_cast<void>(
494 pmbusIntf->read("in1_input", phosphor::pmbus::Type::Hwmon));
495 }
Patrick Williamsc1d4de52021-10-06 12:45:57 -0500496 catch (const ReadFailure& e)
Brandon Wyman11151532020-11-10 13:45:57 -0600497 {
498 // Since I do not care what the return value is, I really do not
B. J. Wyman681b2a32021-04-20 22:31:22 +0000499 // care much if it gets a ReadFailure either. However, this
500 // should not prevent the application from continuing to run, so
501 // catching the read failure.
Brandon Wyman11151532020-11-10 13:45:57 -0600502 }
Brandon Wyman3c208462020-05-13 16:25:58 -0500503 }
504}
505
Brandon Wymanaed1f752019-11-25 18:10:52 -0600506void PowerSupply::inventoryChanged(sdbusplus::message::message& msg)
507{
508 std::string msgSensor;
Patrick Williamsabe49412020-05-13 17:59:47 -0500509 std::map<std::string, std::variant<uint32_t, bool>> msgData;
Brandon Wymanaed1f752019-11-25 18:10:52 -0600510 msg.read(msgSensor, msgData);
511
512 // Check if it was the Present property that changed.
513 auto valPropMap = msgData.find(PRESENT_PROP);
514 if (valPropMap != msgData.end())
515 {
516 if (std::get<bool>(valPropMap->second))
517 {
518 present = true;
B. J. Wyman681b2a32021-04-20 22:31:22 +0000519 // TODO: Immediately trying to read or write the "files" causes
520 // read or write failures.
Brandon Wyman1d7a7df2020-03-26 10:14:05 -0500521 using namespace std::chrono_literals;
522 std::this_thread::sleep_for(20ms);
Brandon Wyman9564e942020-11-10 14:01:42 -0600523 pmbusIntf->findHwmonDir();
Brandon Wyman59a35792020-06-04 12:37:40 -0500524 onOffConfig(phosphor::pmbus::ON_OFF_CONFIG_CONTROL_PIN_ONLY);
Brandon Wymanaed1f752019-11-25 18:10:52 -0600525 clearFaults();
Brandon Wyman1d7a7df2020-03-26 10:14:05 -0500526 updateInventory();
Brandon Wymanaed1f752019-11-25 18:10:52 -0600527 }
528 else
529 {
530 present = false;
531
532 // Clear out the now outdated inventory properties
533 updateInventory();
534 }
535 }
536}
537
Brandon Wyman9a507db2021-02-25 16:15:22 -0600538void PowerSupply::inventoryAdded(sdbusplus::message::message& msg)
539{
540 sdbusplus::message::object_path path;
541 msg.read(path);
542 // Make sure the signal is for the PSU inventory path
543 if (path == inventoryPath)
544 {
545 std::map<std::string, std::map<std::string, std::variant<bool>>>
546 interfaces;
547 // Get map of interfaces and their properties
548 msg.read(interfaces);
549
550 auto properties = interfaces.find(INVENTORY_IFACE);
551 if (properties != interfaces.end())
552 {
553 auto property = properties->second.find(PRESENT_PROP);
554 if (property != properties->second.end())
555 {
556 present = std::get<bool>(property->second);
557
558 log<level::INFO>(fmt::format("Power Supply {} Present {}",
559 inventoryPath, present)
560 .c_str());
561
562 updateInventory();
563 }
564 }
565 }
566}
567
Brandon Wyman1d7a7df2020-03-26 10:14:05 -0500568void PowerSupply::updateInventory()
569{
570 using namespace phosphor::pmbus;
571
Chanh Nguyenc12c53b2021-04-06 17:24:47 +0700572#if IBM_VPD
Brandon Wyman1d7a7df2020-03-26 10:14:05 -0500573 std::string ccin;
574 std::string pn;
575 std::string fn;
576 std::string header;
577 std::string sn;
Brandon Wyman1d7a7df2020-03-26 10:14:05 -0500578 using PropertyMap =
George Liu070c1bc2020-10-12 11:28:01 +0800579 std::map<std::string,
580 std::variant<std::string, std::vector<uint8_t>, bool>>;
Brandon Wyman1d7a7df2020-03-26 10:14:05 -0500581 PropertyMap assetProps;
George Liu070c1bc2020-10-12 11:28:01 +0800582 PropertyMap operProps;
Brandon Wyman1d7a7df2020-03-26 10:14:05 -0500583 PropertyMap versionProps;
584 PropertyMap ipzvpdDINFProps;
585 PropertyMap ipzvpdVINIProps;
586 using InterfaceMap = std::map<std::string, PropertyMap>;
587 InterfaceMap interfaces;
588 using ObjectMap = std::map<sdbusplus::message::object_path, InterfaceMap>;
589 ObjectMap object;
590#endif
B. J. Wyman681b2a32021-04-20 22:31:22 +0000591 log<level::DEBUG>(
592 fmt::format("updateInventory() inventoryPath: {}", inventoryPath)
593 .c_str());
Brandon Wyman1d7a7df2020-03-26 10:14:05 -0500594
595 if (present)
596 {
597 // TODO: non-IBM inventory updates?
598
Chanh Nguyenc12c53b2021-04-06 17:24:47 +0700599#if IBM_VPD
Brandon Wyman1d7a7df2020-03-26 10:14:05 -0500600 try
601 {
602 ccin = pmbusIntf->readString(CCIN, Type::HwmonDeviceDebug);
603 assetProps.emplace(MODEL_PROP, ccin);
Adriana Kobylak572a9052021-03-30 15:58:07 +0000604 modelName = ccin;
Brandon Wyman1d7a7df2020-03-26 10:14:05 -0500605 }
Patrick Williamsc1d4de52021-10-06 12:45:57 -0500606 catch (const ReadFailure& e)
Brandon Wyman1d7a7df2020-03-26 10:14:05 -0500607 {
B. J. Wyman681b2a32021-04-20 22:31:22 +0000608 // Ignore the read failure, let pmbus code indicate failure,
609 // path...
Brandon Wyman1d7a7df2020-03-26 10:14:05 -0500610 // TODO - ibm918
611 // https://github.com/openbmc/docs/blob/master/designs/vpd-collection.md
612 // The BMC must log errors if any of the VPD cannot be properly
613 // parsed or fails ECC checks.
614 }
615
616 try
617 {
618 pn = pmbusIntf->readString(PART_NUMBER, Type::HwmonDeviceDebug);
619 assetProps.emplace(PN_PROP, pn);
620 }
Patrick Williamsc1d4de52021-10-06 12:45:57 -0500621 catch (const ReadFailure& e)
Brandon Wyman1d7a7df2020-03-26 10:14:05 -0500622 {
B. J. Wyman681b2a32021-04-20 22:31:22 +0000623 // Ignore the read failure, let pmbus code indicate failure,
624 // path...
Brandon Wyman1d7a7df2020-03-26 10:14:05 -0500625 }
626
627 try
628 {
629 fn = pmbusIntf->readString(FRU_NUMBER, Type::HwmonDeviceDebug);
Brandon Wymana169b0f2021-12-07 20:18:06 +0000630 assetProps.emplace(SPARE_PN_PROP, fn);
Brandon Wyman1d7a7df2020-03-26 10:14:05 -0500631 }
Patrick Williamsc1d4de52021-10-06 12:45:57 -0500632 catch (const ReadFailure& e)
Brandon Wyman1d7a7df2020-03-26 10:14:05 -0500633 {
B. J. Wyman681b2a32021-04-20 22:31:22 +0000634 // Ignore the read failure, let pmbus code indicate failure,
635 // path...
Brandon Wyman1d7a7df2020-03-26 10:14:05 -0500636 }
637
638 try
639 {
640 header =
641 pmbusIntf->readString(SERIAL_HEADER, Type::HwmonDeviceDebug);
642 sn = pmbusIntf->readString(SERIAL_NUMBER, Type::HwmonDeviceDebug);
643 assetProps.emplace(SN_PROP, sn);
644 }
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 {
Brandon Wymanc9efe412020-10-09 15:42:50 -0500653 fwVersion =
654 pmbusIntf->readString(FW_VERSION, Type::HwmonDeviceDebug);
655 versionProps.emplace(VERSION_PROP, fwVersion);
Brandon Wyman1d7a7df2020-03-26 10:14:05 -0500656 }
Patrick Williamsc1d4de52021-10-06 12:45:57 -0500657 catch (const ReadFailure& e)
Brandon Wyman1d7a7df2020-03-26 10:14:05 -0500658 {
B. J. Wyman681b2a32021-04-20 22:31:22 +0000659 // Ignore the read failure, let pmbus code indicate failure,
660 // path...
Brandon Wyman1d7a7df2020-03-26 10:14:05 -0500661 }
662
663 ipzvpdVINIProps.emplace("CC",
664 std::vector<uint8_t>(ccin.begin(), ccin.end()));
665 ipzvpdVINIProps.emplace("PN",
666 std::vector<uint8_t>(pn.begin(), pn.end()));
667 ipzvpdVINIProps.emplace("FN",
668 std::vector<uint8_t>(fn.begin(), fn.end()));
669 std::string header_sn = header + sn + '\0';
670 ipzvpdVINIProps.emplace(
671 "SN", std::vector<uint8_t>(header_sn.begin(), header_sn.end()));
672 std::string description = "IBM PS";
673 ipzvpdVINIProps.emplace(
674 "DR", std::vector<uint8_t>(description.begin(), description.end()));
675
676 // Update the Resource Identifier (RI) keyword
677 // 2 byte FRC: 0x0003
678 // 2 byte RID: 0x1000, 0x1001...
679 std::uint8_t num = std::stoul(
680 inventoryPath.substr(inventoryPath.size() - 1, 1), nullptr, 0);
681 std::vector<uint8_t> ri{0x00, 0x03, 0x10, num};
682 ipzvpdDINFProps.emplace("RI", ri);
683
684 // Fill in the FRU Label (FL) keyword.
685 std::string fl = "E";
686 fl.push_back(inventoryPath.back());
687 fl.resize(FL_KW_SIZE, ' ');
688 ipzvpdDINFProps.emplace("FL",
689 std::vector<uint8_t>(fl.begin(), fl.end()));
690
691 interfaces.emplace(ASSET_IFACE, std::move(assetProps));
692 interfaces.emplace(VERSION_IFACE, std::move(versionProps));
693 interfaces.emplace(DINF_IFACE, std::move(ipzvpdDINFProps));
694 interfaces.emplace(VINI_IFACE, std::move(ipzvpdVINIProps));
695
George Liu070c1bc2020-10-12 11:28:01 +0800696 // Update the Functional
697 operProps.emplace(FUNCTIONAL_PROP, present);
698 interfaces.emplace(OPERATIONAL_STATE_IFACE, std::move(operProps));
699
Brandon Wyman1d7a7df2020-03-26 10:14:05 -0500700 auto path = inventoryPath.substr(strlen(INVENTORY_OBJ_PATH));
701 object.emplace(path, std::move(interfaces));
702
703 try
704 {
705 auto service =
706 util::getService(INVENTORY_OBJ_PATH, INVENTORY_MGR_IFACE, bus);
707
708 if (service.empty())
709 {
710 log<level::ERR>("Unable to get inventory manager service");
711 return;
712 }
713
714 auto method =
715 bus.new_method_call(service.c_str(), INVENTORY_OBJ_PATH,
716 INVENTORY_MGR_IFACE, "Notify");
717
718 method.append(std::move(object));
719
720 auto reply = bus.call(method);
721 }
Patrick Williamsc1d4de52021-10-06 12:45:57 -0500722 catch (const std::exception& e)
Brandon Wyman1d7a7df2020-03-26 10:14:05 -0500723 {
Jay Meyer6a3fd2c2020-08-25 16:37:16 -0500724 log<level::ERR>(
725 std::string(e.what() + std::string(" PATH=") + inventoryPath)
726 .c_str());
Brandon Wyman1d7a7df2020-03-26 10:14:05 -0500727 }
728#endif
729 }
730}
731
Adriana Kobylak4175ffb2021-08-02 14:51:05 +0000732void PowerSupply::getInputVoltage(double& actualInputVoltage,
733 int& inputVoltage) const
734{
735 using namespace phosphor::pmbus;
736
737 actualInputVoltage = in_input::VIN_VOLTAGE_0;
738 inputVoltage = in_input::VIN_VOLTAGE_0;
739
740 if (present)
741 {
742 try
743 {
744 // Read input voltage in millivolts
745 auto inputVoltageStr = pmbusIntf->readString(READ_VIN, Type::Hwmon);
746
747 // Convert to volts
748 actualInputVoltage = std::stod(inputVoltageStr) / 1000;
749
750 // Calculate the voltage based on voltage thresholds
751 if (actualInputVoltage < in_input::VIN_VOLTAGE_MIN)
752 {
753 inputVoltage = in_input::VIN_VOLTAGE_0;
754 }
755 else if (actualInputVoltage < in_input::VIN_VOLTAGE_110_THRESHOLD)
756 {
757 inputVoltage = in_input::VIN_VOLTAGE_110;
758 }
759 else
760 {
761 inputVoltage = in_input::VIN_VOLTAGE_220;
762 }
763 }
764 catch (const std::exception& e)
765 {
766 log<level::ERR>(
767 fmt::format("READ_VIN read error: {}", e.what()).c_str());
768 }
769 }
770}
771
Brandon Wyman3f1242f2020-01-28 13:11:25 -0600772} // namespace phosphor::power::psu