blob: dcf61779296268eb652e6599912a70774bdc4a89 [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 Wyman6710ba22021-10-27 17:39:31 +0000253 fmt::format("INPUT fault: STATUS_WORD = {:#04x}, "
254 "STATUS_MFR_SPECIFIC = {:#02x}, "
255 "STATUS_VOUT = {:#02x}",
256 statusWord, statusMFR, statusVout)
257 .c_str());
258 }
Brandon Wyman9ddc6222021-10-28 17:28:01 +0000259
Brandon Wyman6710ba22021-10-27 17:39:31 +0000260 voutOVFault = true;
261 }
262
Brandon Wymanb10b3be2021-11-09 22:12:15 +0000263 if (statusWord & status_word::IOUT_OC_FAULT)
264 {
265 if (!ioutOCFault)
266 {
267 log<level::ERR>(
268 fmt::format("IOUT fault: STATUS_WORD = {:#04x}, "
269 "STATUS_MFR_SPECIFIC = {:#02x}, "
270 "STATUS_IOUT = {:#02x}",
271 statusWord, statusMFR, statusIout)
272 .c_str());
273 }
274
275 ioutOCFault = true;
276 }
277
Brandon Wyman96893a42021-11-05 19:56:57 +0000278 if (statusWord & status_word::TEMPERATURE_FAULT_WARN)
279 {
280 if (!tempFault)
281 {
282 log<level::ERR>(
283 fmt::format("TEMPERATURE fault/warning: "
284 "STATUS_WORD = {:#04x}, "
285 "STATUS_MFR_SPECIFIC = {:#02x}, "
286 "STATUS_TEMPERATURE = {:#02x}",
287 statusWord, statusMFR,
288 statusTemperature)
289 .c_str());
290 }
291
292 tempFault = true;
293 }
294
Brandon Wyman2916ea52021-11-06 03:31:18 +0000295 if ((statusWord & status_word::POWER_GOOD_NEGATED) ||
296 (statusWord & status_word::UNIT_IS_OFF))
297 {
298 if (!pgoodFault)
299 {
300 log<level::ERR>(
301 fmt::format("PGOOD fault: "
302 "STATUS_WORD = {:#04x}, "
303 "STATUS_MFR_SPECIFIC = {:#02x}",
304 statusWord, statusMFR)
305 .c_str());
306 }
307
308 pgoodFault = true;
309 }
310
Brandon Wyman3f1242f2020-01-28 13:11:25 -0600311 if (statusWord & status_word::MFR_SPECIFIC_FAULT)
312 {
313 if (!mfrFault)
314 {
Brandon Wymanc8996602021-10-12 19:28:56 +0000315 log<level::ERR>(
316 fmt::format("MFR fault: "
317 "STATUS_WORD = {:#04x} "
318 "STATUS_MFR_SPECIFIC = {:#02x}",
319 statusWord, statusMFR)
320 .c_str());
Brandon Wyman3f1242f2020-01-28 13:11:25 -0600321 }
Brandon Wyman9ddc6222021-10-28 17:28:01 +0000322
Brandon Wyman3f1242f2020-01-28 13:11:25 -0600323 mfrFault = true;
324 }
325
326 if (statusWord & status_word::VIN_UV_FAULT)
327 {
328 if (!vinUVFault)
329 {
Brandon Wyman43d32632021-10-26 23:44:11 +0000330 log<level::ERR>(
Brandon Wymanf07bc792021-10-12 19:00:35 +0000331 fmt::format("VIN_UV fault: STATUS_WORD = {:#04x}, "
332 "STATUS_MFR_SPECIFIC = {:#02x}, "
333 "STATUS_INPUT = {:#02x}",
334 statusWord, statusMFR, statusInput)
Brandon Wymanc8996602021-10-12 19:28:56 +0000335 .c_str());
Brandon Wyman3f1242f2020-01-28 13:11:25 -0600336 }
337
Brandon Wyman3f1242f2020-01-28 13:11:25 -0600338 vinUVFault = true;
339 }
340 }
341 else
342 {
Brandon Wyman85c7bf42021-10-19 22:28:48 +0000343 cmlFault = false;
Brandon Wyman3f1242f2020-01-28 13:11:25 -0600344 inputFault = false;
345 mfrFault = false;
346 vinUVFault = false;
Brandon Wyman6710ba22021-10-27 17:39:31 +0000347 voutOVFault = false;
Brandon Wymanb10b3be2021-11-09 22:12:15 +0000348 ioutOCFault = false;
Brandon Wyman96893a42021-11-05 19:56:57 +0000349 tempFault = false;
Brandon Wyman2916ea52021-11-06 03:31:18 +0000350 pgoodFault = false;
Brandon Wyman3f1242f2020-01-28 13:11:25 -0600351 }
352 }
Patrick Williamsc1d4de52021-10-06 12:45:57 -0500353 catch (const ReadFailure& e)
Brandon Wyman3f1242f2020-01-28 13:11:25 -0600354 {
Brandon Wymanf65c4062020-08-19 13:15:53 -0500355 readFail++;
Brandon Wyman3f1242f2020-01-28 13:11:25 -0600356 phosphor::logging::commit<ReadFailure>();
357 }
358 }
359}
360
Brandon Wyman59a35792020-06-04 12:37:40 -0500361void PowerSupply::onOffConfig(uint8_t data)
362{
363 using namespace phosphor::pmbus;
364
365 if (present)
366 {
367 log<level::INFO>("ON_OFF_CONFIG write", entry("DATA=0x%02X", data));
368 try
369 {
370 std::vector<uint8_t> configData{data};
371 pmbusIntf->writeBinary(ON_OFF_CONFIG, configData,
372 Type::HwmonDeviceDebug);
373 }
374 catch (...)
375 {
376 // The underlying code in writeBinary will log a message to the
B. J. Wyman681b2a32021-04-20 22:31:22 +0000377 // journal if the write fails. If the ON_OFF_CONFIG is not setup
378 // as desired, later fault detection and analysis code should
379 // catch any of the fall out. We should not need to terminate
380 // the application if this write fails.
Brandon Wyman59a35792020-06-04 12:37:40 -0500381 }
382 }
383}
384
Brandon Wyman3c208462020-05-13 16:25:58 -0500385void PowerSupply::clearFaults()
386{
Brandon Wyman5474c912021-02-23 14:39:43 -0600387 faultLogged = false;
Brandon Wyman3c208462020-05-13 16:25:58 -0500388 // The PMBus device driver does not allow for writing CLEAR_FAULTS
389 // directly. However, the pmbus hwmon device driver code will send a
390 // CLEAR_FAULTS after reading from any of the hwmon "files" in sysfs, so
391 // reading in1_input should result in clearing the fault bits in
392 // STATUS_BYTE/STATUS_WORD.
393 // I do not care what the return value is.
Brandon Wyman11151532020-11-10 13:45:57 -0600394 if (present)
Brandon Wyman3c208462020-05-13 16:25:58 -0500395 {
Brandon Wyman9564e942020-11-10 14:01:42 -0600396 inputFault = false;
397 mfrFault = false;
Jay Meyer10d94052020-11-30 14:41:21 -0600398 statusMFR = 0;
Brandon Wyman9564e942020-11-10 14:01:42 -0600399 vinUVFault = false;
Brandon Wyman85c7bf42021-10-19 22:28:48 +0000400 cmlFault = false;
Brandon Wyman6710ba22021-10-27 17:39:31 +0000401 voutOVFault = false;
Brandon Wymanb10b3be2021-11-09 22:12:15 +0000402 ioutOCFault = false;
Brandon Wyman96893a42021-11-05 19:56:57 +0000403 tempFault = false;
Brandon Wyman2916ea52021-11-06 03:31:18 +0000404 pgoodFault = false;
Brandon Wyman9564e942020-11-10 14:01:42 -0600405 readFail = 0;
Brandon Wyman9564e942020-11-10 14:01:42 -0600406
Brandon Wyman11151532020-11-10 13:45:57 -0600407 try
408 {
409 static_cast<void>(
410 pmbusIntf->read("in1_input", phosphor::pmbus::Type::Hwmon));
411 }
Patrick Williamsc1d4de52021-10-06 12:45:57 -0500412 catch (const ReadFailure& e)
Brandon Wyman11151532020-11-10 13:45:57 -0600413 {
414 // Since I do not care what the return value is, I really do not
B. J. Wyman681b2a32021-04-20 22:31:22 +0000415 // care much if it gets a ReadFailure either. However, this
416 // should not prevent the application from continuing to run, so
417 // catching the read failure.
Brandon Wyman11151532020-11-10 13:45:57 -0600418 }
Brandon Wyman3c208462020-05-13 16:25:58 -0500419 }
420}
421
Brandon Wymanaed1f752019-11-25 18:10:52 -0600422void PowerSupply::inventoryChanged(sdbusplus::message::message& msg)
423{
424 std::string msgSensor;
Patrick Williamsabe49412020-05-13 17:59:47 -0500425 std::map<std::string, std::variant<uint32_t, bool>> msgData;
Brandon Wymanaed1f752019-11-25 18:10:52 -0600426 msg.read(msgSensor, msgData);
427
428 // Check if it was the Present property that changed.
429 auto valPropMap = msgData.find(PRESENT_PROP);
430 if (valPropMap != msgData.end())
431 {
432 if (std::get<bool>(valPropMap->second))
433 {
434 present = true;
B. J. Wyman681b2a32021-04-20 22:31:22 +0000435 // TODO: Immediately trying to read or write the "files" causes
436 // read or write failures.
Brandon Wyman1d7a7df2020-03-26 10:14:05 -0500437 using namespace std::chrono_literals;
438 std::this_thread::sleep_for(20ms);
Brandon Wyman9564e942020-11-10 14:01:42 -0600439 pmbusIntf->findHwmonDir();
Brandon Wyman59a35792020-06-04 12:37:40 -0500440 onOffConfig(phosphor::pmbus::ON_OFF_CONFIG_CONTROL_PIN_ONLY);
Brandon Wymanaed1f752019-11-25 18:10:52 -0600441 clearFaults();
Brandon Wyman1d7a7df2020-03-26 10:14:05 -0500442 updateInventory();
Brandon Wymanaed1f752019-11-25 18:10:52 -0600443 }
444 else
445 {
446 present = false;
447
448 // Clear out the now outdated inventory properties
449 updateInventory();
450 }
451 }
452}
453
Brandon Wyman9a507db2021-02-25 16:15:22 -0600454void PowerSupply::inventoryAdded(sdbusplus::message::message& msg)
455{
456 sdbusplus::message::object_path path;
457 msg.read(path);
458 // Make sure the signal is for the PSU inventory path
459 if (path == inventoryPath)
460 {
461 std::map<std::string, std::map<std::string, std::variant<bool>>>
462 interfaces;
463 // Get map of interfaces and their properties
464 msg.read(interfaces);
465
466 auto properties = interfaces.find(INVENTORY_IFACE);
467 if (properties != interfaces.end())
468 {
469 auto property = properties->second.find(PRESENT_PROP);
470 if (property != properties->second.end())
471 {
472 present = std::get<bool>(property->second);
473
474 log<level::INFO>(fmt::format("Power Supply {} Present {}",
475 inventoryPath, present)
476 .c_str());
477
478 updateInventory();
479 }
480 }
481 }
482}
483
Brandon Wyman1d7a7df2020-03-26 10:14:05 -0500484void PowerSupply::updateInventory()
485{
486 using namespace phosphor::pmbus;
487
Chanh Nguyenc12c53b2021-04-06 17:24:47 +0700488#if IBM_VPD
Brandon Wyman1d7a7df2020-03-26 10:14:05 -0500489 std::string ccin;
490 std::string pn;
491 std::string fn;
492 std::string header;
493 std::string sn;
Brandon Wyman1d7a7df2020-03-26 10:14:05 -0500494 using PropertyMap =
George Liu070c1bc2020-10-12 11:28:01 +0800495 std::map<std::string,
496 std::variant<std::string, std::vector<uint8_t>, bool>>;
Brandon Wyman1d7a7df2020-03-26 10:14:05 -0500497 PropertyMap assetProps;
George Liu070c1bc2020-10-12 11:28:01 +0800498 PropertyMap operProps;
Brandon Wyman1d7a7df2020-03-26 10:14:05 -0500499 PropertyMap versionProps;
500 PropertyMap ipzvpdDINFProps;
501 PropertyMap ipzvpdVINIProps;
502 using InterfaceMap = std::map<std::string, PropertyMap>;
503 InterfaceMap interfaces;
504 using ObjectMap = std::map<sdbusplus::message::object_path, InterfaceMap>;
505 ObjectMap object;
506#endif
B. J. Wyman681b2a32021-04-20 22:31:22 +0000507 log<level::DEBUG>(
508 fmt::format("updateInventory() inventoryPath: {}", inventoryPath)
509 .c_str());
Brandon Wyman1d7a7df2020-03-26 10:14:05 -0500510
511 if (present)
512 {
513 // TODO: non-IBM inventory updates?
514
Chanh Nguyenc12c53b2021-04-06 17:24:47 +0700515#if IBM_VPD
Brandon Wyman1d7a7df2020-03-26 10:14:05 -0500516 try
517 {
518 ccin = pmbusIntf->readString(CCIN, Type::HwmonDeviceDebug);
519 assetProps.emplace(MODEL_PROP, ccin);
Adriana Kobylak572a9052021-03-30 15:58:07 +0000520 modelName = ccin;
Brandon Wyman1d7a7df2020-03-26 10:14:05 -0500521 }
Patrick Williamsc1d4de52021-10-06 12:45:57 -0500522 catch (const ReadFailure& e)
Brandon Wyman1d7a7df2020-03-26 10:14:05 -0500523 {
B. J. Wyman681b2a32021-04-20 22:31:22 +0000524 // Ignore the read failure, let pmbus code indicate failure,
525 // path...
Brandon Wyman1d7a7df2020-03-26 10:14:05 -0500526 // TODO - ibm918
527 // https://github.com/openbmc/docs/blob/master/designs/vpd-collection.md
528 // The BMC must log errors if any of the VPD cannot be properly
529 // parsed or fails ECC checks.
530 }
531
532 try
533 {
534 pn = pmbusIntf->readString(PART_NUMBER, Type::HwmonDeviceDebug);
535 assetProps.emplace(PN_PROP, pn);
536 }
Patrick Williamsc1d4de52021-10-06 12:45:57 -0500537 catch (const ReadFailure& e)
Brandon Wyman1d7a7df2020-03-26 10:14:05 -0500538 {
B. J. Wyman681b2a32021-04-20 22:31:22 +0000539 // Ignore the read failure, let pmbus code indicate failure,
540 // path...
Brandon Wyman1d7a7df2020-03-26 10:14:05 -0500541 }
542
543 try
544 {
545 fn = pmbusIntf->readString(FRU_NUMBER, Type::HwmonDeviceDebug);
546 }
Patrick Williamsc1d4de52021-10-06 12:45:57 -0500547 catch (const ReadFailure& e)
Brandon Wyman1d7a7df2020-03-26 10:14:05 -0500548 {
B. J. Wyman681b2a32021-04-20 22:31:22 +0000549 // Ignore the read failure, let pmbus code indicate failure,
550 // path...
Brandon Wyman1d7a7df2020-03-26 10:14:05 -0500551 }
552
553 try
554 {
555 header =
556 pmbusIntf->readString(SERIAL_HEADER, Type::HwmonDeviceDebug);
557 sn = pmbusIntf->readString(SERIAL_NUMBER, Type::HwmonDeviceDebug);
558 assetProps.emplace(SN_PROP, sn);
559 }
Patrick Williamsc1d4de52021-10-06 12:45:57 -0500560 catch (const ReadFailure& e)
Brandon Wyman1d7a7df2020-03-26 10:14:05 -0500561 {
B. J. Wyman681b2a32021-04-20 22:31:22 +0000562 // Ignore the read failure, let pmbus code indicate failure,
563 // path...
Brandon Wyman1d7a7df2020-03-26 10:14:05 -0500564 }
565
566 try
567 {
Brandon Wymanc9efe412020-10-09 15:42:50 -0500568 fwVersion =
569 pmbusIntf->readString(FW_VERSION, Type::HwmonDeviceDebug);
570 versionProps.emplace(VERSION_PROP, fwVersion);
Brandon Wyman1d7a7df2020-03-26 10:14:05 -0500571 }
Patrick Williamsc1d4de52021-10-06 12:45:57 -0500572 catch (const ReadFailure& e)
Brandon Wyman1d7a7df2020-03-26 10:14:05 -0500573 {
B. J. Wyman681b2a32021-04-20 22:31:22 +0000574 // Ignore the read failure, let pmbus code indicate failure,
575 // path...
Brandon Wyman1d7a7df2020-03-26 10:14:05 -0500576 }
577
578 ipzvpdVINIProps.emplace("CC",
579 std::vector<uint8_t>(ccin.begin(), ccin.end()));
580 ipzvpdVINIProps.emplace("PN",
581 std::vector<uint8_t>(pn.begin(), pn.end()));
582 ipzvpdVINIProps.emplace("FN",
583 std::vector<uint8_t>(fn.begin(), fn.end()));
584 std::string header_sn = header + sn + '\0';
585 ipzvpdVINIProps.emplace(
586 "SN", std::vector<uint8_t>(header_sn.begin(), header_sn.end()));
587 std::string description = "IBM PS";
588 ipzvpdVINIProps.emplace(
589 "DR", std::vector<uint8_t>(description.begin(), description.end()));
590
591 // Update the Resource Identifier (RI) keyword
592 // 2 byte FRC: 0x0003
593 // 2 byte RID: 0x1000, 0x1001...
594 std::uint8_t num = std::stoul(
595 inventoryPath.substr(inventoryPath.size() - 1, 1), nullptr, 0);
596 std::vector<uint8_t> ri{0x00, 0x03, 0x10, num};
597 ipzvpdDINFProps.emplace("RI", ri);
598
599 // Fill in the FRU Label (FL) keyword.
600 std::string fl = "E";
601 fl.push_back(inventoryPath.back());
602 fl.resize(FL_KW_SIZE, ' ');
603 ipzvpdDINFProps.emplace("FL",
604 std::vector<uint8_t>(fl.begin(), fl.end()));
605
606 interfaces.emplace(ASSET_IFACE, std::move(assetProps));
607 interfaces.emplace(VERSION_IFACE, std::move(versionProps));
608 interfaces.emplace(DINF_IFACE, std::move(ipzvpdDINFProps));
609 interfaces.emplace(VINI_IFACE, std::move(ipzvpdVINIProps));
610
George Liu070c1bc2020-10-12 11:28:01 +0800611 // Update the Functional
612 operProps.emplace(FUNCTIONAL_PROP, present);
613 interfaces.emplace(OPERATIONAL_STATE_IFACE, std::move(operProps));
614
Brandon Wyman1d7a7df2020-03-26 10:14:05 -0500615 auto path = inventoryPath.substr(strlen(INVENTORY_OBJ_PATH));
616 object.emplace(path, std::move(interfaces));
617
618 try
619 {
620 auto service =
621 util::getService(INVENTORY_OBJ_PATH, INVENTORY_MGR_IFACE, bus);
622
623 if (service.empty())
624 {
625 log<level::ERR>("Unable to get inventory manager service");
626 return;
627 }
628
629 auto method =
630 bus.new_method_call(service.c_str(), INVENTORY_OBJ_PATH,
631 INVENTORY_MGR_IFACE, "Notify");
632
633 method.append(std::move(object));
634
635 auto reply = bus.call(method);
636 }
Patrick Williamsc1d4de52021-10-06 12:45:57 -0500637 catch (const std::exception& e)
Brandon Wyman1d7a7df2020-03-26 10:14:05 -0500638 {
Jay Meyer6a3fd2c2020-08-25 16:37:16 -0500639 log<level::ERR>(
640 std::string(e.what() + std::string(" PATH=") + inventoryPath)
641 .c_str());
Brandon Wyman1d7a7df2020-03-26 10:14:05 -0500642 }
643#endif
644 }
645}
646
Adriana Kobylak4175ffb2021-08-02 14:51:05 +0000647void PowerSupply::getInputVoltage(double& actualInputVoltage,
648 int& inputVoltage) const
649{
650 using namespace phosphor::pmbus;
651
652 actualInputVoltage = in_input::VIN_VOLTAGE_0;
653 inputVoltage = in_input::VIN_VOLTAGE_0;
654
655 if (present)
656 {
657 try
658 {
659 // Read input voltage in millivolts
660 auto inputVoltageStr = pmbusIntf->readString(READ_VIN, Type::Hwmon);
661
662 // Convert to volts
663 actualInputVoltage = std::stod(inputVoltageStr) / 1000;
664
665 // Calculate the voltage based on voltage thresholds
666 if (actualInputVoltage < in_input::VIN_VOLTAGE_MIN)
667 {
668 inputVoltage = in_input::VIN_VOLTAGE_0;
669 }
670 else if (actualInputVoltage < in_input::VIN_VOLTAGE_110_THRESHOLD)
671 {
672 inputVoltage = in_input::VIN_VOLTAGE_110;
673 }
674 else
675 {
676 inputVoltage = in_input::VIN_VOLTAGE_220;
677 }
678 }
679 catch (const std::exception& e)
680 {
681 log<level::ERR>(
682 fmt::format("READ_VIN read error: {}", e.what()).c_str());
683 }
684 }
685}
686
Brandon Wyman3f1242f2020-01-28 13:11:25 -0600687} // namespace phosphor::power::psu