blob: 73998f399c0a61bec5d37949908b200dd1a09d8d [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 Wyman85c7bf42021-10-19 22:28:48 +0000216 if (statusWord & status_word::CML_FAULT)
217 {
218 if (!cmlFault)
219 {
Brandon Wyman43d32632021-10-26 23:44:11 +0000220 log<level::ERR>(
Brandon Wyman85c7bf42021-10-19 22:28:48 +0000221 fmt::format("CML fault: STATUS_WORD = {:#04x}, "
222 "STATUS_CML = {:#02x}",
223 statusWord, statusCML)
224 .c_str());
225 }
Brandon Wyman9ddc6222021-10-28 17:28:01 +0000226
Brandon Wyman85c7bf42021-10-19 22:28:48 +0000227 cmlFault = true;
228 }
229
Brandon Wyman3f1242f2020-01-28 13:11:25 -0600230 if (statusWord & status_word::INPUT_FAULT_WARN)
231 {
232 if (!inputFault)
233 {
Brandon Wyman43d32632021-10-26 23:44:11 +0000234 log<level::ERR>(
Brandon Wymanf07bc792021-10-12 19:00:35 +0000235 fmt::format("INPUT fault: STATUS_WORD = {:#04x}, "
236 "STATUS_MFR_SPECIFIC = {:#02x}, "
237 "STATUS_INPUT = {:#02x}",
238 statusWord, statusMFR, statusInput)
Brandon Wymanc8996602021-10-12 19:28:56 +0000239 .c_str());
Brandon Wyman3f1242f2020-01-28 13:11:25 -0600240 }
241
Brandon Wyman3f1242f2020-01-28 13:11:25 -0600242 inputFault = true;
243 }
244
Brandon Wyman6710ba22021-10-27 17:39:31 +0000245 if (statusWord & status_word::VOUT_OV_FAULT)
246 {
247 if (!voutOVFault)
248 {
Brandon Wyman43d32632021-10-26 23:44:11 +0000249 log<level::ERR>(
Brandon Wyman6710ba22021-10-27 17:39:31 +0000250 fmt::format("INPUT fault: STATUS_WORD = {:#04x}, "
251 "STATUS_MFR_SPECIFIC = {:#02x}, "
252 "STATUS_VOUT = {:#02x}",
253 statusWord, statusMFR, statusVout)
254 .c_str());
255 }
Brandon Wyman9ddc6222021-10-28 17:28:01 +0000256
Brandon Wyman6710ba22021-10-27 17:39:31 +0000257 voutOVFault = true;
258 }
259
Brandon Wyman3f1242f2020-01-28 13:11:25 -0600260 if (statusWord & status_word::MFR_SPECIFIC_FAULT)
261 {
262 if (!mfrFault)
263 {
Brandon Wymanc8996602021-10-12 19:28:56 +0000264 log<level::ERR>(
265 fmt::format("MFR fault: "
266 "STATUS_WORD = {:#04x} "
267 "STATUS_MFR_SPECIFIC = {:#02x}",
268 statusWord, statusMFR)
269 .c_str());
Brandon Wyman3f1242f2020-01-28 13:11:25 -0600270 }
Brandon Wyman9ddc6222021-10-28 17:28:01 +0000271
Brandon Wyman3f1242f2020-01-28 13:11:25 -0600272 mfrFault = true;
273 }
274
275 if (statusWord & status_word::VIN_UV_FAULT)
276 {
277 if (!vinUVFault)
278 {
Brandon Wyman43d32632021-10-26 23:44:11 +0000279 log<level::ERR>(
Brandon Wymanf07bc792021-10-12 19:00:35 +0000280 fmt::format("VIN_UV fault: STATUS_WORD = {:#04x}, "
281 "STATUS_MFR_SPECIFIC = {:#02x}, "
282 "STATUS_INPUT = {:#02x}",
283 statusWord, statusMFR, statusInput)
Brandon Wymanc8996602021-10-12 19:28:56 +0000284 .c_str());
Brandon Wyman3f1242f2020-01-28 13:11:25 -0600285 }
286
Brandon Wyman3f1242f2020-01-28 13:11:25 -0600287 vinUVFault = true;
288 }
289 }
290 else
291 {
Brandon Wyman85c7bf42021-10-19 22:28:48 +0000292 cmlFault = false;
Brandon Wyman3f1242f2020-01-28 13:11:25 -0600293 inputFault = false;
294 mfrFault = false;
295 vinUVFault = false;
Brandon Wyman6710ba22021-10-27 17:39:31 +0000296 voutOVFault = false;
Brandon Wyman3f1242f2020-01-28 13:11:25 -0600297 }
298 }
Patrick Williamsc1d4de52021-10-06 12:45:57 -0500299 catch (const ReadFailure& e)
Brandon Wyman3f1242f2020-01-28 13:11:25 -0600300 {
Brandon Wymanf65c4062020-08-19 13:15:53 -0500301 readFail++;
Brandon Wyman3f1242f2020-01-28 13:11:25 -0600302 phosphor::logging::commit<ReadFailure>();
303 }
304 }
305}
306
Brandon Wyman59a35792020-06-04 12:37:40 -0500307void PowerSupply::onOffConfig(uint8_t data)
308{
309 using namespace phosphor::pmbus;
310
311 if (present)
312 {
313 log<level::INFO>("ON_OFF_CONFIG write", entry("DATA=0x%02X", data));
314 try
315 {
316 std::vector<uint8_t> configData{data};
317 pmbusIntf->writeBinary(ON_OFF_CONFIG, configData,
318 Type::HwmonDeviceDebug);
319 }
320 catch (...)
321 {
322 // The underlying code in writeBinary will log a message to the
B. J. Wyman681b2a32021-04-20 22:31:22 +0000323 // journal if the write fails. If the ON_OFF_CONFIG is not setup
324 // as desired, later fault detection and analysis code should
325 // catch any of the fall out. We should not need to terminate
326 // the application if this write fails.
Brandon Wyman59a35792020-06-04 12:37:40 -0500327 }
328 }
329}
330
Brandon Wyman3c208462020-05-13 16:25:58 -0500331void PowerSupply::clearFaults()
332{
Brandon Wyman5474c912021-02-23 14:39:43 -0600333 faultLogged = false;
Brandon Wyman3c208462020-05-13 16:25:58 -0500334 // The PMBus device driver does not allow for writing CLEAR_FAULTS
335 // directly. However, the pmbus hwmon device driver code will send a
336 // CLEAR_FAULTS after reading from any of the hwmon "files" in sysfs, so
337 // reading in1_input should result in clearing the fault bits in
338 // STATUS_BYTE/STATUS_WORD.
339 // I do not care what the return value is.
Brandon Wyman11151532020-11-10 13:45:57 -0600340 if (present)
Brandon Wyman3c208462020-05-13 16:25:58 -0500341 {
Brandon Wyman9564e942020-11-10 14:01:42 -0600342 inputFault = false;
343 mfrFault = false;
Jay Meyer10d94052020-11-30 14:41:21 -0600344 statusMFR = 0;
Brandon Wyman9564e942020-11-10 14:01:42 -0600345 vinUVFault = false;
Brandon Wyman85c7bf42021-10-19 22:28:48 +0000346 cmlFault = false;
Brandon Wyman6710ba22021-10-27 17:39:31 +0000347 voutOVFault = false;
Brandon Wyman9564e942020-11-10 14:01:42 -0600348 readFail = 0;
Brandon Wyman9564e942020-11-10 14:01:42 -0600349
Brandon Wyman11151532020-11-10 13:45:57 -0600350 try
351 {
352 static_cast<void>(
353 pmbusIntf->read("in1_input", phosphor::pmbus::Type::Hwmon));
354 }
Patrick Williamsc1d4de52021-10-06 12:45:57 -0500355 catch (const ReadFailure& e)
Brandon Wyman11151532020-11-10 13:45:57 -0600356 {
357 // Since I do not care what the return value is, I really do not
B. J. Wyman681b2a32021-04-20 22:31:22 +0000358 // care much if it gets a ReadFailure either. However, this
359 // should not prevent the application from continuing to run, so
360 // catching the read failure.
Brandon Wyman11151532020-11-10 13:45:57 -0600361 }
Brandon Wyman3c208462020-05-13 16:25:58 -0500362 }
363}
364
Brandon Wymanaed1f752019-11-25 18:10:52 -0600365void PowerSupply::inventoryChanged(sdbusplus::message::message& msg)
366{
367 std::string msgSensor;
Patrick Williamsabe49412020-05-13 17:59:47 -0500368 std::map<std::string, std::variant<uint32_t, bool>> msgData;
Brandon Wymanaed1f752019-11-25 18:10:52 -0600369 msg.read(msgSensor, msgData);
370
371 // Check if it was the Present property that changed.
372 auto valPropMap = msgData.find(PRESENT_PROP);
373 if (valPropMap != msgData.end())
374 {
375 if (std::get<bool>(valPropMap->second))
376 {
377 present = true;
B. J. Wyman681b2a32021-04-20 22:31:22 +0000378 // TODO: Immediately trying to read or write the "files" causes
379 // read or write failures.
Brandon Wyman1d7a7df2020-03-26 10:14:05 -0500380 using namespace std::chrono_literals;
381 std::this_thread::sleep_for(20ms);
Brandon Wyman9564e942020-11-10 14:01:42 -0600382 pmbusIntf->findHwmonDir();
Brandon Wyman59a35792020-06-04 12:37:40 -0500383 onOffConfig(phosphor::pmbus::ON_OFF_CONFIG_CONTROL_PIN_ONLY);
Brandon Wymanaed1f752019-11-25 18:10:52 -0600384 clearFaults();
Brandon Wyman1d7a7df2020-03-26 10:14:05 -0500385 updateInventory();
Brandon Wymanaed1f752019-11-25 18:10:52 -0600386 }
387 else
388 {
389 present = false;
390
391 // Clear out the now outdated inventory properties
392 updateInventory();
393 }
394 }
395}
396
Brandon Wyman9a507db2021-02-25 16:15:22 -0600397void PowerSupply::inventoryAdded(sdbusplus::message::message& msg)
398{
399 sdbusplus::message::object_path path;
400 msg.read(path);
401 // Make sure the signal is for the PSU inventory path
402 if (path == inventoryPath)
403 {
404 std::map<std::string, std::map<std::string, std::variant<bool>>>
405 interfaces;
406 // Get map of interfaces and their properties
407 msg.read(interfaces);
408
409 auto properties = interfaces.find(INVENTORY_IFACE);
410 if (properties != interfaces.end())
411 {
412 auto property = properties->second.find(PRESENT_PROP);
413 if (property != properties->second.end())
414 {
415 present = std::get<bool>(property->second);
416
417 log<level::INFO>(fmt::format("Power Supply {} Present {}",
418 inventoryPath, present)
419 .c_str());
420
421 updateInventory();
422 }
423 }
424 }
425}
426
Brandon Wyman1d7a7df2020-03-26 10:14:05 -0500427void PowerSupply::updateInventory()
428{
429 using namespace phosphor::pmbus;
430
Chanh Nguyenc12c53b2021-04-06 17:24:47 +0700431#if IBM_VPD
Brandon Wyman1d7a7df2020-03-26 10:14:05 -0500432 std::string ccin;
433 std::string pn;
434 std::string fn;
435 std::string header;
436 std::string sn;
Brandon Wyman1d7a7df2020-03-26 10:14:05 -0500437 using PropertyMap =
George Liu070c1bc2020-10-12 11:28:01 +0800438 std::map<std::string,
439 std::variant<std::string, std::vector<uint8_t>, bool>>;
Brandon Wyman1d7a7df2020-03-26 10:14:05 -0500440 PropertyMap assetProps;
George Liu070c1bc2020-10-12 11:28:01 +0800441 PropertyMap operProps;
Brandon Wyman1d7a7df2020-03-26 10:14:05 -0500442 PropertyMap versionProps;
443 PropertyMap ipzvpdDINFProps;
444 PropertyMap ipzvpdVINIProps;
445 using InterfaceMap = std::map<std::string, PropertyMap>;
446 InterfaceMap interfaces;
447 using ObjectMap = std::map<sdbusplus::message::object_path, InterfaceMap>;
448 ObjectMap object;
449#endif
B. J. Wyman681b2a32021-04-20 22:31:22 +0000450 log<level::DEBUG>(
451 fmt::format("updateInventory() inventoryPath: {}", inventoryPath)
452 .c_str());
Brandon Wyman1d7a7df2020-03-26 10:14:05 -0500453
454 if (present)
455 {
456 // TODO: non-IBM inventory updates?
457
Chanh Nguyenc12c53b2021-04-06 17:24:47 +0700458#if IBM_VPD
Brandon Wyman1d7a7df2020-03-26 10:14:05 -0500459 try
460 {
461 ccin = pmbusIntf->readString(CCIN, Type::HwmonDeviceDebug);
462 assetProps.emplace(MODEL_PROP, ccin);
Adriana Kobylak572a9052021-03-30 15:58:07 +0000463 modelName = ccin;
Brandon Wyman1d7a7df2020-03-26 10:14:05 -0500464 }
Patrick Williamsc1d4de52021-10-06 12:45:57 -0500465 catch (const ReadFailure& e)
Brandon Wyman1d7a7df2020-03-26 10:14:05 -0500466 {
B. J. Wyman681b2a32021-04-20 22:31:22 +0000467 // Ignore the read failure, let pmbus code indicate failure,
468 // path...
Brandon Wyman1d7a7df2020-03-26 10:14:05 -0500469 // TODO - ibm918
470 // https://github.com/openbmc/docs/blob/master/designs/vpd-collection.md
471 // The BMC must log errors if any of the VPD cannot be properly
472 // parsed or fails ECC checks.
473 }
474
475 try
476 {
477 pn = pmbusIntf->readString(PART_NUMBER, Type::HwmonDeviceDebug);
478 assetProps.emplace(PN_PROP, pn);
479 }
Patrick Williamsc1d4de52021-10-06 12:45:57 -0500480 catch (const ReadFailure& e)
Brandon Wyman1d7a7df2020-03-26 10:14:05 -0500481 {
B. J. Wyman681b2a32021-04-20 22:31:22 +0000482 // Ignore the read failure, let pmbus code indicate failure,
483 // path...
Brandon Wyman1d7a7df2020-03-26 10:14:05 -0500484 }
485
486 try
487 {
488 fn = pmbusIntf->readString(FRU_NUMBER, Type::HwmonDeviceDebug);
489 }
Patrick Williamsc1d4de52021-10-06 12:45:57 -0500490 catch (const ReadFailure& e)
Brandon Wyman1d7a7df2020-03-26 10:14:05 -0500491 {
B. J. Wyman681b2a32021-04-20 22:31:22 +0000492 // Ignore the read failure, let pmbus code indicate failure,
493 // path...
Brandon Wyman1d7a7df2020-03-26 10:14:05 -0500494 }
495
496 try
497 {
498 header =
499 pmbusIntf->readString(SERIAL_HEADER, Type::HwmonDeviceDebug);
500 sn = pmbusIntf->readString(SERIAL_NUMBER, Type::HwmonDeviceDebug);
501 assetProps.emplace(SN_PROP, sn);
502 }
Patrick Williamsc1d4de52021-10-06 12:45:57 -0500503 catch (const ReadFailure& e)
Brandon Wyman1d7a7df2020-03-26 10:14:05 -0500504 {
B. J. Wyman681b2a32021-04-20 22:31:22 +0000505 // Ignore the read failure, let pmbus code indicate failure,
506 // path...
Brandon Wyman1d7a7df2020-03-26 10:14:05 -0500507 }
508
509 try
510 {
Brandon Wymanc9efe412020-10-09 15:42:50 -0500511 fwVersion =
512 pmbusIntf->readString(FW_VERSION, Type::HwmonDeviceDebug);
513 versionProps.emplace(VERSION_PROP, fwVersion);
Brandon Wyman1d7a7df2020-03-26 10:14:05 -0500514 }
Patrick Williamsc1d4de52021-10-06 12:45:57 -0500515 catch (const ReadFailure& e)
Brandon Wyman1d7a7df2020-03-26 10:14:05 -0500516 {
B. J. Wyman681b2a32021-04-20 22:31:22 +0000517 // Ignore the read failure, let pmbus code indicate failure,
518 // path...
Brandon Wyman1d7a7df2020-03-26 10:14:05 -0500519 }
520
521 ipzvpdVINIProps.emplace("CC",
522 std::vector<uint8_t>(ccin.begin(), ccin.end()));
523 ipzvpdVINIProps.emplace("PN",
524 std::vector<uint8_t>(pn.begin(), pn.end()));
525 ipzvpdVINIProps.emplace("FN",
526 std::vector<uint8_t>(fn.begin(), fn.end()));
527 std::string header_sn = header + sn + '\0';
528 ipzvpdVINIProps.emplace(
529 "SN", std::vector<uint8_t>(header_sn.begin(), header_sn.end()));
530 std::string description = "IBM PS";
531 ipzvpdVINIProps.emplace(
532 "DR", std::vector<uint8_t>(description.begin(), description.end()));
533
534 // Update the Resource Identifier (RI) keyword
535 // 2 byte FRC: 0x0003
536 // 2 byte RID: 0x1000, 0x1001...
537 std::uint8_t num = std::stoul(
538 inventoryPath.substr(inventoryPath.size() - 1, 1), nullptr, 0);
539 std::vector<uint8_t> ri{0x00, 0x03, 0x10, num};
540 ipzvpdDINFProps.emplace("RI", ri);
541
542 // Fill in the FRU Label (FL) keyword.
543 std::string fl = "E";
544 fl.push_back(inventoryPath.back());
545 fl.resize(FL_KW_SIZE, ' ');
546 ipzvpdDINFProps.emplace("FL",
547 std::vector<uint8_t>(fl.begin(), fl.end()));
548
549 interfaces.emplace(ASSET_IFACE, std::move(assetProps));
550 interfaces.emplace(VERSION_IFACE, std::move(versionProps));
551 interfaces.emplace(DINF_IFACE, std::move(ipzvpdDINFProps));
552 interfaces.emplace(VINI_IFACE, std::move(ipzvpdVINIProps));
553
George Liu070c1bc2020-10-12 11:28:01 +0800554 // Update the Functional
555 operProps.emplace(FUNCTIONAL_PROP, present);
556 interfaces.emplace(OPERATIONAL_STATE_IFACE, std::move(operProps));
557
Brandon Wyman1d7a7df2020-03-26 10:14:05 -0500558 auto path = inventoryPath.substr(strlen(INVENTORY_OBJ_PATH));
559 object.emplace(path, std::move(interfaces));
560
561 try
562 {
563 auto service =
564 util::getService(INVENTORY_OBJ_PATH, INVENTORY_MGR_IFACE, bus);
565
566 if (service.empty())
567 {
568 log<level::ERR>("Unable to get inventory manager service");
569 return;
570 }
571
572 auto method =
573 bus.new_method_call(service.c_str(), INVENTORY_OBJ_PATH,
574 INVENTORY_MGR_IFACE, "Notify");
575
576 method.append(std::move(object));
577
578 auto reply = bus.call(method);
579 }
Patrick Williamsc1d4de52021-10-06 12:45:57 -0500580 catch (const std::exception& e)
Brandon Wyman1d7a7df2020-03-26 10:14:05 -0500581 {
Jay Meyer6a3fd2c2020-08-25 16:37:16 -0500582 log<level::ERR>(
583 std::string(e.what() + std::string(" PATH=") + inventoryPath)
584 .c_str());
Brandon Wyman1d7a7df2020-03-26 10:14:05 -0500585 }
586#endif
587 }
588}
589
Adriana Kobylak4175ffb2021-08-02 14:51:05 +0000590void PowerSupply::getInputVoltage(double& actualInputVoltage,
591 int& inputVoltage) const
592{
593 using namespace phosphor::pmbus;
594
595 actualInputVoltage = in_input::VIN_VOLTAGE_0;
596 inputVoltage = in_input::VIN_VOLTAGE_0;
597
598 if (present)
599 {
600 try
601 {
602 // Read input voltage in millivolts
603 auto inputVoltageStr = pmbusIntf->readString(READ_VIN, Type::Hwmon);
604
605 // Convert to volts
606 actualInputVoltage = std::stod(inputVoltageStr) / 1000;
607
608 // Calculate the voltage based on voltage thresholds
609 if (actualInputVoltage < in_input::VIN_VOLTAGE_MIN)
610 {
611 inputVoltage = in_input::VIN_VOLTAGE_0;
612 }
613 else if (actualInputVoltage < in_input::VIN_VOLTAGE_110_THRESHOLD)
614 {
615 inputVoltage = in_input::VIN_VOLTAGE_110;
616 }
617 else
618 {
619 inputVoltage = in_input::VIN_VOLTAGE_220;
620 }
621 }
622 catch (const std::exception& e)
623 {
624 log<level::ERR>(
625 fmt::format("READ_VIN read error: {}", e.what()).c_str());
626 }
627 }
628}
629
Brandon Wyman3f1242f2020-01-28 13:11:25 -0600630} // namespace phosphor::power::psu