blob: 7c884ff83bd301dbaee6bd53b198f4337423a69a [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 Wyman3f1242f2020-01-28 13:11:25 -0600192void PowerSupply::analyze()
193{
194 using namespace phosphor::pmbus;
195
B. J. Wyman681b2a32021-04-20 22:31:22 +0000196 if (presenceGPIO)
197 {
198 updatePresenceGPIO();
199 }
200
Brandon Wymanf65c4062020-08-19 13:15:53 -0500201 if ((present) && (readFail < LOG_LIMIT))
Brandon Wyman3f1242f2020-01-28 13:11:25 -0600202 {
203 try
204 {
Brandon Wymanfed0ba22020-09-26 20:02:51 -0500205 statusWord = pmbusIntf->read(STATUS_WORD, Type::Debug);
Brandon Wymanf65c4062020-08-19 13:15:53 -0500206 // Read worked, reset the fail count.
207 readFail = 0;
Brandon Wyman3f1242f2020-01-28 13:11:25 -0600208
209 if (statusWord)
210 {
Brandon Wymanf07bc792021-10-12 19:00:35 +0000211 statusInput = pmbusIntf->read(STATUS_INPUT, Type::Debug);
Jay Meyer10d94052020-11-30 14:41:21 -0600212 statusMFR = pmbusIntf->read(STATUS_MFR, Type::Debug);
Brandon Wyman85c7bf42021-10-19 22:28:48 +0000213 statusCML = pmbusIntf->read(STATUS_CML, Type::Debug);
Brandon Wyman6710ba22021-10-27 17:39:31 +0000214 auto status0Vout = pmbusIntf->insertPageNum(STATUS_VOUT, 0);
215 statusVout = pmbusIntf->read(status0Vout, Type::Debug);
Brandon Wymanb10b3be2021-11-09 22:12:15 +0000216 statusIout = pmbusIntf->read(STATUS_IOUT, Type::Debug);
Brandon Wyman96893a42021-11-05 19:56:57 +0000217 statusTemperature =
218 pmbusIntf->read(STATUS_TEMPERATURE, Type::Debug);
Brandon Wyman85c7bf42021-10-19 22:28:48 +0000219 if (statusWord & status_word::CML_FAULT)
220 {
221 if (!cmlFault)
222 {
Brandon Wyman43d32632021-10-26 23:44:11 +0000223 log<level::ERR>(
Brandon Wyman85c7bf42021-10-19 22:28:48 +0000224 fmt::format("CML fault: STATUS_WORD = {:#04x}, "
225 "STATUS_CML = {:#02x}",
226 statusWord, statusCML)
227 .c_str());
228 }
Brandon Wyman9ddc6222021-10-28 17:28:01 +0000229
Brandon Wyman85c7bf42021-10-19 22:28:48 +0000230 cmlFault = true;
231 }
232
Brandon Wyman3f1242f2020-01-28 13:11:25 -0600233 if (statusWord & status_word::INPUT_FAULT_WARN)
234 {
235 if (!inputFault)
236 {
Brandon Wyman43d32632021-10-26 23:44:11 +0000237 log<level::ERR>(
Brandon Wymanf07bc792021-10-12 19:00:35 +0000238 fmt::format("INPUT fault: STATUS_WORD = {:#04x}, "
239 "STATUS_MFR_SPECIFIC = {:#02x}, "
240 "STATUS_INPUT = {:#02x}",
241 statusWord, statusMFR, statusInput)
Brandon Wymanc8996602021-10-12 19:28:56 +0000242 .c_str());
Brandon Wyman3f1242f2020-01-28 13:11:25 -0600243 }
244
Brandon Wyman3f1242f2020-01-28 13:11:25 -0600245 inputFault = true;
246 }
247
Brandon Wyman6710ba22021-10-27 17:39:31 +0000248 if (statusWord & status_word::VOUT_OV_FAULT)
249 {
250 if (!voutOVFault)
251 {
Brandon Wyman43d32632021-10-26 23:44:11 +0000252 log<level::ERR>(
Brandon Wyman2cf46942021-10-28 19:09:16 +0000253 fmt::format(
254 "VOUT_OV_FAULT fault: STATUS_WORD = {:#04x}, "
255 "STATUS_MFR_SPECIFIC = {:#02x}, "
256 "STATUS_VOUT = {:#02x}",
257 statusWord, statusMFR, statusVout)
Brandon Wyman6710ba22021-10-27 17:39:31 +0000258 .c_str());
259 }
Brandon Wyman9ddc6222021-10-28 17:28:01 +0000260
Brandon Wyman6710ba22021-10-27 17:39:31 +0000261 voutOVFault = true;
262 }
263
Brandon Wymanb10b3be2021-11-09 22:12:15 +0000264 if (statusWord & status_word::IOUT_OC_FAULT)
265 {
266 if (!ioutOCFault)
267 {
268 log<level::ERR>(
269 fmt::format("IOUT fault: STATUS_WORD = {:#04x}, "
270 "STATUS_MFR_SPECIFIC = {:#02x}, "
271 "STATUS_IOUT = {:#02x}",
272 statusWord, statusMFR, statusIout)
273 .c_str());
274 }
275
276 ioutOCFault = true;
277 }
278
Brandon Wyman2cf46942021-10-28 19:09:16 +0000279 if ((statusWord & status_word::VOUT_FAULT) &&
280 !(statusWord & status_word::VOUT_OV_FAULT))
281 {
282 if (!voutUVFault)
283 {
284 log<level::ERR>(
285 fmt::format(
286 "VOUT_UV_FAULT fault: STATUS_WORD = {:#04x}, "
287 "STATUS_MFR_SPECIFIC = {:#02x}, "
288 "STATUS_VOUT = {:#02x}",
289 statusWord, statusMFR, statusVout)
290 .c_str());
291 }
292
293 voutUVFault = true;
294 }
295
Brandon Wyman96893a42021-11-05 19:56:57 +0000296 if (statusWord & status_word::TEMPERATURE_FAULT_WARN)
297 {
298 if (!tempFault)
299 {
300 log<level::ERR>(
301 fmt::format("TEMPERATURE fault/warning: "
302 "STATUS_WORD = {:#04x}, "
303 "STATUS_MFR_SPECIFIC = {:#02x}, "
304 "STATUS_TEMPERATURE = {:#02x}",
305 statusWord, statusMFR,
306 statusTemperature)
307 .c_str());
308 }
309
310 tempFault = true;
311 }
312
Brandon Wyman2916ea52021-11-06 03:31:18 +0000313 if ((statusWord & status_word::POWER_GOOD_NEGATED) ||
314 (statusWord & status_word::UNIT_IS_OFF))
315 {
316 if (!pgoodFault)
317 {
318 log<level::ERR>(
319 fmt::format("PGOOD fault: "
320 "STATUS_WORD = {:#04x}, "
321 "STATUS_MFR_SPECIFIC = {:#02x}",
322 statusWord, statusMFR)
323 .c_str());
324 }
325
326 pgoodFault = true;
327 }
328
Brandon Wyman3f1242f2020-01-28 13:11:25 -0600329 if (statusWord & status_word::MFR_SPECIFIC_FAULT)
330 {
331 if (!mfrFault)
332 {
Brandon Wymanc8996602021-10-12 19:28:56 +0000333 log<level::ERR>(
334 fmt::format("MFR fault: "
335 "STATUS_WORD = {:#04x} "
336 "STATUS_MFR_SPECIFIC = {:#02x}",
337 statusWord, statusMFR)
338 .c_str());
Brandon Wyman3f1242f2020-01-28 13:11:25 -0600339 }
Brandon Wyman9ddc6222021-10-28 17:28:01 +0000340
Brandon Wyman3f1242f2020-01-28 13:11:25 -0600341 mfrFault = true;
342 }
343
344 if (statusWord & status_word::VIN_UV_FAULT)
345 {
346 if (!vinUVFault)
347 {
Brandon Wyman43d32632021-10-26 23:44:11 +0000348 log<level::ERR>(
Brandon Wymanf07bc792021-10-12 19:00:35 +0000349 fmt::format("VIN_UV fault: STATUS_WORD = {:#04x}, "
350 "STATUS_MFR_SPECIFIC = {:#02x}, "
351 "STATUS_INPUT = {:#02x}",
352 statusWord, statusMFR, statusInput)
Brandon Wymanc8996602021-10-12 19:28:56 +0000353 .c_str());
Brandon Wyman3f1242f2020-01-28 13:11:25 -0600354 }
355
Brandon Wyman3f1242f2020-01-28 13:11:25 -0600356 vinUVFault = true;
357 }
358 }
359 else
360 {
Brandon Wyman85c7bf42021-10-19 22:28:48 +0000361 cmlFault = false;
Brandon Wyman3f1242f2020-01-28 13:11:25 -0600362 inputFault = false;
363 mfrFault = false;
364 vinUVFault = false;
Brandon Wyman6710ba22021-10-27 17:39:31 +0000365 voutOVFault = false;
Brandon Wymanb10b3be2021-11-09 22:12:15 +0000366 ioutOCFault = false;
Brandon Wyman2cf46942021-10-28 19:09:16 +0000367 voutUVFault = false;
Brandon Wyman96893a42021-11-05 19:56:57 +0000368 tempFault = false;
Brandon Wyman2916ea52021-11-06 03:31:18 +0000369 pgoodFault = false;
Brandon Wyman3f1242f2020-01-28 13:11:25 -0600370 }
371 }
Patrick Williamsc1d4de52021-10-06 12:45:57 -0500372 catch (const ReadFailure& e)
Brandon Wyman3f1242f2020-01-28 13:11:25 -0600373 {
Brandon Wymanf65c4062020-08-19 13:15:53 -0500374 readFail++;
Brandon Wyman3f1242f2020-01-28 13:11:25 -0600375 phosphor::logging::commit<ReadFailure>();
376 }
377 }
378}
379
Brandon Wyman59a35792020-06-04 12:37:40 -0500380void PowerSupply::onOffConfig(uint8_t data)
381{
382 using namespace phosphor::pmbus;
383
384 if (present)
385 {
386 log<level::INFO>("ON_OFF_CONFIG write", entry("DATA=0x%02X", data));
387 try
388 {
389 std::vector<uint8_t> configData{data};
390 pmbusIntf->writeBinary(ON_OFF_CONFIG, configData,
391 Type::HwmonDeviceDebug);
392 }
393 catch (...)
394 {
395 // The underlying code in writeBinary will log a message to the
B. J. Wyman681b2a32021-04-20 22:31:22 +0000396 // journal if the write fails. If the ON_OFF_CONFIG is not setup
397 // as desired, later fault detection and analysis code should
398 // catch any of the fall out. We should not need to terminate
399 // the application if this write fails.
Brandon Wyman59a35792020-06-04 12:37:40 -0500400 }
401 }
402}
403
Brandon Wyman3c208462020-05-13 16:25:58 -0500404void PowerSupply::clearFaults()
405{
Brandon Wyman5474c912021-02-23 14:39:43 -0600406 faultLogged = false;
Brandon Wyman3c208462020-05-13 16:25:58 -0500407 // The PMBus device driver does not allow for writing CLEAR_FAULTS
408 // directly. However, the pmbus hwmon device driver code will send a
409 // CLEAR_FAULTS after reading from any of the hwmon "files" in sysfs, so
410 // reading in1_input should result in clearing the fault bits in
411 // STATUS_BYTE/STATUS_WORD.
412 // I do not care what the return value is.
Brandon Wyman11151532020-11-10 13:45:57 -0600413 if (present)
Brandon Wyman3c208462020-05-13 16:25:58 -0500414 {
Brandon Wyman9564e942020-11-10 14:01:42 -0600415 inputFault = false;
416 mfrFault = false;
Jay Meyer10d94052020-11-30 14:41:21 -0600417 statusMFR = 0;
Brandon Wyman9564e942020-11-10 14:01:42 -0600418 vinUVFault = false;
Brandon Wyman85c7bf42021-10-19 22:28:48 +0000419 cmlFault = false;
Brandon Wyman6710ba22021-10-27 17:39:31 +0000420 voutOVFault = false;
Brandon Wymanb10b3be2021-11-09 22:12:15 +0000421 ioutOCFault = false;
Brandon Wyman2cf46942021-10-28 19:09:16 +0000422 voutUVFault = false;
Brandon Wyman96893a42021-11-05 19:56:57 +0000423 tempFault = false;
Brandon Wyman2916ea52021-11-06 03:31:18 +0000424 pgoodFault = false;
Brandon Wyman9564e942020-11-10 14:01:42 -0600425 readFail = 0;
Brandon Wyman9564e942020-11-10 14:01:42 -0600426
Brandon Wyman11151532020-11-10 13:45:57 -0600427 try
428 {
429 static_cast<void>(
430 pmbusIntf->read("in1_input", phosphor::pmbus::Type::Hwmon));
431 }
Patrick Williamsc1d4de52021-10-06 12:45:57 -0500432 catch (const ReadFailure& e)
Brandon Wyman11151532020-11-10 13:45:57 -0600433 {
434 // Since I do not care what the return value is, I really do not
B. J. Wyman681b2a32021-04-20 22:31:22 +0000435 // care much if it gets a ReadFailure either. However, this
436 // should not prevent the application from continuing to run, so
437 // catching the read failure.
Brandon Wyman11151532020-11-10 13:45:57 -0600438 }
Brandon Wyman3c208462020-05-13 16:25:58 -0500439 }
440}
441
Brandon Wymanaed1f752019-11-25 18:10:52 -0600442void PowerSupply::inventoryChanged(sdbusplus::message::message& msg)
443{
444 std::string msgSensor;
Patrick Williamsabe49412020-05-13 17:59:47 -0500445 std::map<std::string, std::variant<uint32_t, bool>> msgData;
Brandon Wymanaed1f752019-11-25 18:10:52 -0600446 msg.read(msgSensor, msgData);
447
448 // Check if it was the Present property that changed.
449 auto valPropMap = msgData.find(PRESENT_PROP);
450 if (valPropMap != msgData.end())
451 {
452 if (std::get<bool>(valPropMap->second))
453 {
454 present = true;
B. J. Wyman681b2a32021-04-20 22:31:22 +0000455 // TODO: Immediately trying to read or write the "files" causes
456 // read or write failures.
Brandon Wyman1d7a7df2020-03-26 10:14:05 -0500457 using namespace std::chrono_literals;
458 std::this_thread::sleep_for(20ms);
Brandon Wyman9564e942020-11-10 14:01:42 -0600459 pmbusIntf->findHwmonDir();
Brandon Wyman59a35792020-06-04 12:37:40 -0500460 onOffConfig(phosphor::pmbus::ON_OFF_CONFIG_CONTROL_PIN_ONLY);
Brandon Wymanaed1f752019-11-25 18:10:52 -0600461 clearFaults();
Brandon Wyman1d7a7df2020-03-26 10:14:05 -0500462 updateInventory();
Brandon Wymanaed1f752019-11-25 18:10:52 -0600463 }
464 else
465 {
466 present = false;
467
468 // Clear out the now outdated inventory properties
469 updateInventory();
470 }
471 }
472}
473
Brandon Wyman9a507db2021-02-25 16:15:22 -0600474void PowerSupply::inventoryAdded(sdbusplus::message::message& msg)
475{
476 sdbusplus::message::object_path path;
477 msg.read(path);
478 // Make sure the signal is for the PSU inventory path
479 if (path == inventoryPath)
480 {
481 std::map<std::string, std::map<std::string, std::variant<bool>>>
482 interfaces;
483 // Get map of interfaces and their properties
484 msg.read(interfaces);
485
486 auto properties = interfaces.find(INVENTORY_IFACE);
487 if (properties != interfaces.end())
488 {
489 auto property = properties->second.find(PRESENT_PROP);
490 if (property != properties->second.end())
491 {
492 present = std::get<bool>(property->second);
493
494 log<level::INFO>(fmt::format("Power Supply {} Present {}",
495 inventoryPath, present)
496 .c_str());
497
498 updateInventory();
499 }
500 }
501 }
502}
503
Brandon Wyman1d7a7df2020-03-26 10:14:05 -0500504void PowerSupply::updateInventory()
505{
506 using namespace phosphor::pmbus;
507
Chanh Nguyenc12c53b2021-04-06 17:24:47 +0700508#if IBM_VPD
Brandon Wyman1d7a7df2020-03-26 10:14:05 -0500509 std::string ccin;
510 std::string pn;
511 std::string fn;
512 std::string header;
513 std::string sn;
Brandon Wyman1d7a7df2020-03-26 10:14:05 -0500514 using PropertyMap =
George Liu070c1bc2020-10-12 11:28:01 +0800515 std::map<std::string,
516 std::variant<std::string, std::vector<uint8_t>, bool>>;
Brandon Wyman1d7a7df2020-03-26 10:14:05 -0500517 PropertyMap assetProps;
George Liu070c1bc2020-10-12 11:28:01 +0800518 PropertyMap operProps;
Brandon Wyman1d7a7df2020-03-26 10:14:05 -0500519 PropertyMap versionProps;
520 PropertyMap ipzvpdDINFProps;
521 PropertyMap ipzvpdVINIProps;
522 using InterfaceMap = std::map<std::string, PropertyMap>;
523 InterfaceMap interfaces;
524 using ObjectMap = std::map<sdbusplus::message::object_path, InterfaceMap>;
525 ObjectMap object;
526#endif
B. J. Wyman681b2a32021-04-20 22:31:22 +0000527 log<level::DEBUG>(
528 fmt::format("updateInventory() inventoryPath: {}", inventoryPath)
529 .c_str());
Brandon Wyman1d7a7df2020-03-26 10:14:05 -0500530
531 if (present)
532 {
533 // TODO: non-IBM inventory updates?
534
Chanh Nguyenc12c53b2021-04-06 17:24:47 +0700535#if IBM_VPD
Brandon Wyman1d7a7df2020-03-26 10:14:05 -0500536 try
537 {
538 ccin = pmbusIntf->readString(CCIN, Type::HwmonDeviceDebug);
539 assetProps.emplace(MODEL_PROP, ccin);
Adriana Kobylak572a9052021-03-30 15:58:07 +0000540 modelName = ccin;
Brandon Wyman1d7a7df2020-03-26 10:14:05 -0500541 }
Patrick Williamsc1d4de52021-10-06 12:45:57 -0500542 catch (const ReadFailure& e)
Brandon Wyman1d7a7df2020-03-26 10:14:05 -0500543 {
B. J. Wyman681b2a32021-04-20 22:31:22 +0000544 // Ignore the read failure, let pmbus code indicate failure,
545 // path...
Brandon Wyman1d7a7df2020-03-26 10:14:05 -0500546 // TODO - ibm918
547 // https://github.com/openbmc/docs/blob/master/designs/vpd-collection.md
548 // The BMC must log errors if any of the VPD cannot be properly
549 // parsed or fails ECC checks.
550 }
551
552 try
553 {
554 pn = pmbusIntf->readString(PART_NUMBER, Type::HwmonDeviceDebug);
555 assetProps.emplace(PN_PROP, pn);
556 }
Patrick Williamsc1d4de52021-10-06 12:45:57 -0500557 catch (const ReadFailure& e)
Brandon Wyman1d7a7df2020-03-26 10:14:05 -0500558 {
B. J. Wyman681b2a32021-04-20 22:31:22 +0000559 // Ignore the read failure, let pmbus code indicate failure,
560 // path...
Brandon Wyman1d7a7df2020-03-26 10:14:05 -0500561 }
562
563 try
564 {
565 fn = pmbusIntf->readString(FRU_NUMBER, Type::HwmonDeviceDebug);
566 }
Patrick Williamsc1d4de52021-10-06 12:45:57 -0500567 catch (const ReadFailure& e)
Brandon Wyman1d7a7df2020-03-26 10:14:05 -0500568 {
B. J. Wyman681b2a32021-04-20 22:31:22 +0000569 // Ignore the read failure, let pmbus code indicate failure,
570 // path...
Brandon Wyman1d7a7df2020-03-26 10:14:05 -0500571 }
572
573 try
574 {
575 header =
576 pmbusIntf->readString(SERIAL_HEADER, Type::HwmonDeviceDebug);
577 sn = pmbusIntf->readString(SERIAL_NUMBER, Type::HwmonDeviceDebug);
578 assetProps.emplace(SN_PROP, sn);
579 }
Patrick Williamsc1d4de52021-10-06 12:45:57 -0500580 catch (const ReadFailure& e)
Brandon Wyman1d7a7df2020-03-26 10:14:05 -0500581 {
B. J. Wyman681b2a32021-04-20 22:31:22 +0000582 // Ignore the read failure, let pmbus code indicate failure,
583 // path...
Brandon Wyman1d7a7df2020-03-26 10:14:05 -0500584 }
585
586 try
587 {
Brandon Wymanc9efe412020-10-09 15:42:50 -0500588 fwVersion =
589 pmbusIntf->readString(FW_VERSION, Type::HwmonDeviceDebug);
590 versionProps.emplace(VERSION_PROP, fwVersion);
Brandon Wyman1d7a7df2020-03-26 10:14:05 -0500591 }
Patrick Williamsc1d4de52021-10-06 12:45:57 -0500592 catch (const ReadFailure& e)
Brandon Wyman1d7a7df2020-03-26 10:14:05 -0500593 {
B. J. Wyman681b2a32021-04-20 22:31:22 +0000594 // Ignore the read failure, let pmbus code indicate failure,
595 // path...
Brandon Wyman1d7a7df2020-03-26 10:14:05 -0500596 }
597
598 ipzvpdVINIProps.emplace("CC",
599 std::vector<uint8_t>(ccin.begin(), ccin.end()));
600 ipzvpdVINIProps.emplace("PN",
601 std::vector<uint8_t>(pn.begin(), pn.end()));
602 ipzvpdVINIProps.emplace("FN",
603 std::vector<uint8_t>(fn.begin(), fn.end()));
604 std::string header_sn = header + sn + '\0';
605 ipzvpdVINIProps.emplace(
606 "SN", std::vector<uint8_t>(header_sn.begin(), header_sn.end()));
607 std::string description = "IBM PS";
608 ipzvpdVINIProps.emplace(
609 "DR", std::vector<uint8_t>(description.begin(), description.end()));
610
611 // Update the Resource Identifier (RI) keyword
612 // 2 byte FRC: 0x0003
613 // 2 byte RID: 0x1000, 0x1001...
614 std::uint8_t num = std::stoul(
615 inventoryPath.substr(inventoryPath.size() - 1, 1), nullptr, 0);
616 std::vector<uint8_t> ri{0x00, 0x03, 0x10, num};
617 ipzvpdDINFProps.emplace("RI", ri);
618
619 // Fill in the FRU Label (FL) keyword.
620 std::string fl = "E";
621 fl.push_back(inventoryPath.back());
622 fl.resize(FL_KW_SIZE, ' ');
623 ipzvpdDINFProps.emplace("FL",
624 std::vector<uint8_t>(fl.begin(), fl.end()));
625
626 interfaces.emplace(ASSET_IFACE, std::move(assetProps));
627 interfaces.emplace(VERSION_IFACE, std::move(versionProps));
628 interfaces.emplace(DINF_IFACE, std::move(ipzvpdDINFProps));
629 interfaces.emplace(VINI_IFACE, std::move(ipzvpdVINIProps));
630
George Liu070c1bc2020-10-12 11:28:01 +0800631 // Update the Functional
632 operProps.emplace(FUNCTIONAL_PROP, present);
633 interfaces.emplace(OPERATIONAL_STATE_IFACE, std::move(operProps));
634
Brandon Wyman1d7a7df2020-03-26 10:14:05 -0500635 auto path = inventoryPath.substr(strlen(INVENTORY_OBJ_PATH));
636 object.emplace(path, std::move(interfaces));
637
638 try
639 {
640 auto service =
641 util::getService(INVENTORY_OBJ_PATH, INVENTORY_MGR_IFACE, bus);
642
643 if (service.empty())
644 {
645 log<level::ERR>("Unable to get inventory manager service");
646 return;
647 }
648
649 auto method =
650 bus.new_method_call(service.c_str(), INVENTORY_OBJ_PATH,
651 INVENTORY_MGR_IFACE, "Notify");
652
653 method.append(std::move(object));
654
655 auto reply = bus.call(method);
656 }
Patrick Williamsc1d4de52021-10-06 12:45:57 -0500657 catch (const std::exception& e)
Brandon Wyman1d7a7df2020-03-26 10:14:05 -0500658 {
Jay Meyer6a3fd2c2020-08-25 16:37:16 -0500659 log<level::ERR>(
660 std::string(e.what() + std::string(" PATH=") + inventoryPath)
661 .c_str());
Brandon Wyman1d7a7df2020-03-26 10:14:05 -0500662 }
663#endif
664 }
665}
666
Adriana Kobylak4175ffb2021-08-02 14:51:05 +0000667void PowerSupply::getInputVoltage(double& actualInputVoltage,
668 int& inputVoltage) const
669{
670 using namespace phosphor::pmbus;
671
672 actualInputVoltage = in_input::VIN_VOLTAGE_0;
673 inputVoltage = in_input::VIN_VOLTAGE_0;
674
675 if (present)
676 {
677 try
678 {
679 // Read input voltage in millivolts
680 auto inputVoltageStr = pmbusIntf->readString(READ_VIN, Type::Hwmon);
681
682 // Convert to volts
683 actualInputVoltage = std::stod(inputVoltageStr) / 1000;
684
685 // Calculate the voltage based on voltage thresholds
686 if (actualInputVoltage < in_input::VIN_VOLTAGE_MIN)
687 {
688 inputVoltage = in_input::VIN_VOLTAGE_0;
689 }
690 else if (actualInputVoltage < in_input::VIN_VOLTAGE_110_THRESHOLD)
691 {
692 inputVoltage = in_input::VIN_VOLTAGE_110;
693 }
694 else
695 {
696 inputVoltage = in_input::VIN_VOLTAGE_220;
697 }
698 }
699 catch (const std::exception& e)
700 {
701 log<level::ERR>(
702 fmt::format("READ_VIN read error: {}", e.what()).c_str());
703 }
704 }
705}
706
Brandon Wyman3f1242f2020-01-28 13:11:25 -0600707} // namespace phosphor::power::psu