blob: 8183e37c2ac8c07e11cec0684e29442f9c165422 [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 Wyman96893a42021-11-05 19:56:57 +0000216 statusTemperature =
217 pmbusIntf->read(STATUS_TEMPERATURE, Type::Debug);
Brandon Wyman85c7bf42021-10-19 22:28:48 +0000218 if (statusWord & status_word::CML_FAULT)
219 {
220 if (!cmlFault)
221 {
Brandon Wyman43d32632021-10-26 23:44:11 +0000222 log<level::ERR>(
Brandon Wyman85c7bf42021-10-19 22:28:48 +0000223 fmt::format("CML fault: STATUS_WORD = {:#04x}, "
224 "STATUS_CML = {:#02x}",
225 statusWord, statusCML)
226 .c_str());
227 }
Brandon Wyman9ddc6222021-10-28 17:28:01 +0000228
Brandon Wyman85c7bf42021-10-19 22:28:48 +0000229 cmlFault = true;
230 }
231
Brandon Wyman3f1242f2020-01-28 13:11:25 -0600232 if (statusWord & status_word::INPUT_FAULT_WARN)
233 {
234 if (!inputFault)
235 {
Brandon Wyman43d32632021-10-26 23:44:11 +0000236 log<level::ERR>(
Brandon Wymanf07bc792021-10-12 19:00:35 +0000237 fmt::format("INPUT fault: STATUS_WORD = {:#04x}, "
238 "STATUS_MFR_SPECIFIC = {:#02x}, "
239 "STATUS_INPUT = {:#02x}",
240 statusWord, statusMFR, statusInput)
Brandon Wymanc8996602021-10-12 19:28:56 +0000241 .c_str());
Brandon Wyman3f1242f2020-01-28 13:11:25 -0600242 }
243
Brandon Wyman3f1242f2020-01-28 13:11:25 -0600244 inputFault = true;
245 }
246
Brandon Wyman6710ba22021-10-27 17:39:31 +0000247 if (statusWord & status_word::VOUT_OV_FAULT)
248 {
249 if (!voutOVFault)
250 {
Brandon Wyman43d32632021-10-26 23:44:11 +0000251 log<level::ERR>(
Brandon Wyman6710ba22021-10-27 17:39:31 +0000252 fmt::format("INPUT fault: STATUS_WORD = {:#04x}, "
253 "STATUS_MFR_SPECIFIC = {:#02x}, "
254 "STATUS_VOUT = {:#02x}",
255 statusWord, statusMFR, statusVout)
256 .c_str());
257 }
Brandon Wyman9ddc6222021-10-28 17:28:01 +0000258
Brandon Wyman6710ba22021-10-27 17:39:31 +0000259 voutOVFault = true;
260 }
261
Brandon Wyman96893a42021-11-05 19:56:57 +0000262 if (statusWord & status_word::TEMPERATURE_FAULT_WARN)
263 {
264 if (!tempFault)
265 {
266 log<level::ERR>(
267 fmt::format("TEMPERATURE fault/warning: "
268 "STATUS_WORD = {:#04x}, "
269 "STATUS_MFR_SPECIFIC = {:#02x}, "
270 "STATUS_TEMPERATURE = {:#02x}",
271 statusWord, statusMFR,
272 statusTemperature)
273 .c_str());
274 }
275
276 tempFault = true;
277 }
278
Brandon Wyman2916ea52021-11-06 03:31:18 +0000279 if ((statusWord & status_word::POWER_GOOD_NEGATED) ||
280 (statusWord & status_word::UNIT_IS_OFF))
281 {
282 if (!pgoodFault)
283 {
284 log<level::ERR>(
285 fmt::format("PGOOD fault: "
286 "STATUS_WORD = {:#04x}, "
287 "STATUS_MFR_SPECIFIC = {:#02x}",
288 statusWord, statusMFR)
289 .c_str());
290 }
291
292 pgoodFault = true;
293 }
294
Brandon Wyman3f1242f2020-01-28 13:11:25 -0600295 if (statusWord & status_word::MFR_SPECIFIC_FAULT)
296 {
297 if (!mfrFault)
298 {
Brandon Wymanc8996602021-10-12 19:28:56 +0000299 log<level::ERR>(
300 fmt::format("MFR fault: "
301 "STATUS_WORD = {:#04x} "
302 "STATUS_MFR_SPECIFIC = {:#02x}",
303 statusWord, statusMFR)
304 .c_str());
Brandon Wyman3f1242f2020-01-28 13:11:25 -0600305 }
Brandon Wyman9ddc6222021-10-28 17:28:01 +0000306
Brandon Wyman3f1242f2020-01-28 13:11:25 -0600307 mfrFault = true;
308 }
309
310 if (statusWord & status_word::VIN_UV_FAULT)
311 {
312 if (!vinUVFault)
313 {
Brandon Wyman43d32632021-10-26 23:44:11 +0000314 log<level::ERR>(
Brandon Wymanf07bc792021-10-12 19:00:35 +0000315 fmt::format("VIN_UV fault: STATUS_WORD = {:#04x}, "
316 "STATUS_MFR_SPECIFIC = {:#02x}, "
317 "STATUS_INPUT = {:#02x}",
318 statusWord, statusMFR, statusInput)
Brandon Wymanc8996602021-10-12 19:28:56 +0000319 .c_str());
Brandon Wyman3f1242f2020-01-28 13:11:25 -0600320 }
321
Brandon Wyman3f1242f2020-01-28 13:11:25 -0600322 vinUVFault = true;
323 }
324 }
325 else
326 {
Brandon Wyman85c7bf42021-10-19 22:28:48 +0000327 cmlFault = false;
Brandon Wyman3f1242f2020-01-28 13:11:25 -0600328 inputFault = false;
329 mfrFault = false;
330 vinUVFault = false;
Brandon Wyman6710ba22021-10-27 17:39:31 +0000331 voutOVFault = false;
Brandon Wyman96893a42021-11-05 19:56:57 +0000332 tempFault = false;
Brandon Wyman2916ea52021-11-06 03:31:18 +0000333 pgoodFault = false;
Brandon Wyman3f1242f2020-01-28 13:11:25 -0600334 }
335 }
Patrick Williamsc1d4de52021-10-06 12:45:57 -0500336 catch (const ReadFailure& e)
Brandon Wyman3f1242f2020-01-28 13:11:25 -0600337 {
Brandon Wymanf65c4062020-08-19 13:15:53 -0500338 readFail++;
Brandon Wyman3f1242f2020-01-28 13:11:25 -0600339 phosphor::logging::commit<ReadFailure>();
340 }
341 }
342}
343
Brandon Wyman59a35792020-06-04 12:37:40 -0500344void PowerSupply::onOffConfig(uint8_t data)
345{
346 using namespace phosphor::pmbus;
347
348 if (present)
349 {
350 log<level::INFO>("ON_OFF_CONFIG write", entry("DATA=0x%02X", data));
351 try
352 {
353 std::vector<uint8_t> configData{data};
354 pmbusIntf->writeBinary(ON_OFF_CONFIG, configData,
355 Type::HwmonDeviceDebug);
356 }
357 catch (...)
358 {
359 // The underlying code in writeBinary will log a message to the
B. J. Wyman681b2a32021-04-20 22:31:22 +0000360 // journal if the write fails. If the ON_OFF_CONFIG is not setup
361 // as desired, later fault detection and analysis code should
362 // catch any of the fall out. We should not need to terminate
363 // the application if this write fails.
Brandon Wyman59a35792020-06-04 12:37:40 -0500364 }
365 }
366}
367
Brandon Wyman3c208462020-05-13 16:25:58 -0500368void PowerSupply::clearFaults()
369{
Brandon Wyman5474c912021-02-23 14:39:43 -0600370 faultLogged = false;
Brandon Wyman3c208462020-05-13 16:25:58 -0500371 // The PMBus device driver does not allow for writing CLEAR_FAULTS
372 // directly. However, the pmbus hwmon device driver code will send a
373 // CLEAR_FAULTS after reading from any of the hwmon "files" in sysfs, so
374 // reading in1_input should result in clearing the fault bits in
375 // STATUS_BYTE/STATUS_WORD.
376 // I do not care what the return value is.
Brandon Wyman11151532020-11-10 13:45:57 -0600377 if (present)
Brandon Wyman3c208462020-05-13 16:25:58 -0500378 {
Brandon Wyman9564e942020-11-10 14:01:42 -0600379 inputFault = false;
380 mfrFault = false;
Jay Meyer10d94052020-11-30 14:41:21 -0600381 statusMFR = 0;
Brandon Wyman9564e942020-11-10 14:01:42 -0600382 vinUVFault = false;
Brandon Wyman85c7bf42021-10-19 22:28:48 +0000383 cmlFault = false;
Brandon Wyman6710ba22021-10-27 17:39:31 +0000384 voutOVFault = false;
Brandon Wyman96893a42021-11-05 19:56:57 +0000385 tempFault = false;
Brandon Wyman2916ea52021-11-06 03:31:18 +0000386 pgoodFault = false;
Brandon Wyman9564e942020-11-10 14:01:42 -0600387 readFail = 0;
Brandon Wyman9564e942020-11-10 14:01:42 -0600388
Brandon Wyman11151532020-11-10 13:45:57 -0600389 try
390 {
391 static_cast<void>(
392 pmbusIntf->read("in1_input", phosphor::pmbus::Type::Hwmon));
393 }
Patrick Williamsc1d4de52021-10-06 12:45:57 -0500394 catch (const ReadFailure& e)
Brandon Wyman11151532020-11-10 13:45:57 -0600395 {
396 // Since I do not care what the return value is, I really do not
B. J. Wyman681b2a32021-04-20 22:31:22 +0000397 // care much if it gets a ReadFailure either. However, this
398 // should not prevent the application from continuing to run, so
399 // catching the read failure.
Brandon Wyman11151532020-11-10 13:45:57 -0600400 }
Brandon Wyman3c208462020-05-13 16:25:58 -0500401 }
402}
403
Brandon Wymanaed1f752019-11-25 18:10:52 -0600404void PowerSupply::inventoryChanged(sdbusplus::message::message& msg)
405{
406 std::string msgSensor;
Patrick Williamsabe49412020-05-13 17:59:47 -0500407 std::map<std::string, std::variant<uint32_t, bool>> msgData;
Brandon Wymanaed1f752019-11-25 18:10:52 -0600408 msg.read(msgSensor, msgData);
409
410 // Check if it was the Present property that changed.
411 auto valPropMap = msgData.find(PRESENT_PROP);
412 if (valPropMap != msgData.end())
413 {
414 if (std::get<bool>(valPropMap->second))
415 {
416 present = true;
B. J. Wyman681b2a32021-04-20 22:31:22 +0000417 // TODO: Immediately trying to read or write the "files" causes
418 // read or write failures.
Brandon Wyman1d7a7df2020-03-26 10:14:05 -0500419 using namespace std::chrono_literals;
420 std::this_thread::sleep_for(20ms);
Brandon Wyman9564e942020-11-10 14:01:42 -0600421 pmbusIntf->findHwmonDir();
Brandon Wyman59a35792020-06-04 12:37:40 -0500422 onOffConfig(phosphor::pmbus::ON_OFF_CONFIG_CONTROL_PIN_ONLY);
Brandon Wymanaed1f752019-11-25 18:10:52 -0600423 clearFaults();
Brandon Wyman1d7a7df2020-03-26 10:14:05 -0500424 updateInventory();
Brandon Wymanaed1f752019-11-25 18:10:52 -0600425 }
426 else
427 {
428 present = false;
429
430 // Clear out the now outdated inventory properties
431 updateInventory();
432 }
433 }
434}
435
Brandon Wyman9a507db2021-02-25 16:15:22 -0600436void PowerSupply::inventoryAdded(sdbusplus::message::message& msg)
437{
438 sdbusplus::message::object_path path;
439 msg.read(path);
440 // Make sure the signal is for the PSU inventory path
441 if (path == inventoryPath)
442 {
443 std::map<std::string, std::map<std::string, std::variant<bool>>>
444 interfaces;
445 // Get map of interfaces and their properties
446 msg.read(interfaces);
447
448 auto properties = interfaces.find(INVENTORY_IFACE);
449 if (properties != interfaces.end())
450 {
451 auto property = properties->second.find(PRESENT_PROP);
452 if (property != properties->second.end())
453 {
454 present = std::get<bool>(property->second);
455
456 log<level::INFO>(fmt::format("Power Supply {} Present {}",
457 inventoryPath, present)
458 .c_str());
459
460 updateInventory();
461 }
462 }
463 }
464}
465
Brandon Wyman1d7a7df2020-03-26 10:14:05 -0500466void PowerSupply::updateInventory()
467{
468 using namespace phosphor::pmbus;
469
Chanh Nguyenc12c53b2021-04-06 17:24:47 +0700470#if IBM_VPD
Brandon Wyman1d7a7df2020-03-26 10:14:05 -0500471 std::string ccin;
472 std::string pn;
473 std::string fn;
474 std::string header;
475 std::string sn;
Brandon Wyman1d7a7df2020-03-26 10:14:05 -0500476 using PropertyMap =
George Liu070c1bc2020-10-12 11:28:01 +0800477 std::map<std::string,
478 std::variant<std::string, std::vector<uint8_t>, bool>>;
Brandon Wyman1d7a7df2020-03-26 10:14:05 -0500479 PropertyMap assetProps;
George Liu070c1bc2020-10-12 11:28:01 +0800480 PropertyMap operProps;
Brandon Wyman1d7a7df2020-03-26 10:14:05 -0500481 PropertyMap versionProps;
482 PropertyMap ipzvpdDINFProps;
483 PropertyMap ipzvpdVINIProps;
484 using InterfaceMap = std::map<std::string, PropertyMap>;
485 InterfaceMap interfaces;
486 using ObjectMap = std::map<sdbusplus::message::object_path, InterfaceMap>;
487 ObjectMap object;
488#endif
B. J. Wyman681b2a32021-04-20 22:31:22 +0000489 log<level::DEBUG>(
490 fmt::format("updateInventory() inventoryPath: {}", inventoryPath)
491 .c_str());
Brandon Wyman1d7a7df2020-03-26 10:14:05 -0500492
493 if (present)
494 {
495 // TODO: non-IBM inventory updates?
496
Chanh Nguyenc12c53b2021-04-06 17:24:47 +0700497#if IBM_VPD
Brandon Wyman1d7a7df2020-03-26 10:14:05 -0500498 try
499 {
500 ccin = pmbusIntf->readString(CCIN, Type::HwmonDeviceDebug);
501 assetProps.emplace(MODEL_PROP, ccin);
Adriana Kobylak572a9052021-03-30 15:58:07 +0000502 modelName = ccin;
Brandon Wyman1d7a7df2020-03-26 10:14:05 -0500503 }
Patrick Williamsc1d4de52021-10-06 12:45:57 -0500504 catch (const ReadFailure& e)
Brandon Wyman1d7a7df2020-03-26 10:14:05 -0500505 {
B. J. Wyman681b2a32021-04-20 22:31:22 +0000506 // Ignore the read failure, let pmbus code indicate failure,
507 // path...
Brandon Wyman1d7a7df2020-03-26 10:14:05 -0500508 // TODO - ibm918
509 // https://github.com/openbmc/docs/blob/master/designs/vpd-collection.md
510 // The BMC must log errors if any of the VPD cannot be properly
511 // parsed or fails ECC checks.
512 }
513
514 try
515 {
516 pn = pmbusIntf->readString(PART_NUMBER, Type::HwmonDeviceDebug);
517 assetProps.emplace(PN_PROP, pn);
518 }
Patrick Williamsc1d4de52021-10-06 12:45:57 -0500519 catch (const ReadFailure& e)
Brandon Wyman1d7a7df2020-03-26 10:14:05 -0500520 {
B. J. Wyman681b2a32021-04-20 22:31:22 +0000521 // Ignore the read failure, let pmbus code indicate failure,
522 // path...
Brandon Wyman1d7a7df2020-03-26 10:14:05 -0500523 }
524
525 try
526 {
527 fn = pmbusIntf->readString(FRU_NUMBER, Type::HwmonDeviceDebug);
528 }
Patrick Williamsc1d4de52021-10-06 12:45:57 -0500529 catch (const ReadFailure& e)
Brandon Wyman1d7a7df2020-03-26 10:14:05 -0500530 {
B. J. Wyman681b2a32021-04-20 22:31:22 +0000531 // Ignore the read failure, let pmbus code indicate failure,
532 // path...
Brandon Wyman1d7a7df2020-03-26 10:14:05 -0500533 }
534
535 try
536 {
537 header =
538 pmbusIntf->readString(SERIAL_HEADER, Type::HwmonDeviceDebug);
539 sn = pmbusIntf->readString(SERIAL_NUMBER, Type::HwmonDeviceDebug);
540 assetProps.emplace(SN_PROP, sn);
541 }
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 }
547
548 try
549 {
Brandon Wymanc9efe412020-10-09 15:42:50 -0500550 fwVersion =
551 pmbusIntf->readString(FW_VERSION, Type::HwmonDeviceDebug);
552 versionProps.emplace(VERSION_PROP, fwVersion);
Brandon Wyman1d7a7df2020-03-26 10:14:05 -0500553 }
Patrick Williamsc1d4de52021-10-06 12:45:57 -0500554 catch (const ReadFailure& e)
Brandon Wyman1d7a7df2020-03-26 10:14:05 -0500555 {
B. J. Wyman681b2a32021-04-20 22:31:22 +0000556 // Ignore the read failure, let pmbus code indicate failure,
557 // path...
Brandon Wyman1d7a7df2020-03-26 10:14:05 -0500558 }
559
560 ipzvpdVINIProps.emplace("CC",
561 std::vector<uint8_t>(ccin.begin(), ccin.end()));
562 ipzvpdVINIProps.emplace("PN",
563 std::vector<uint8_t>(pn.begin(), pn.end()));
564 ipzvpdVINIProps.emplace("FN",
565 std::vector<uint8_t>(fn.begin(), fn.end()));
566 std::string header_sn = header + sn + '\0';
567 ipzvpdVINIProps.emplace(
568 "SN", std::vector<uint8_t>(header_sn.begin(), header_sn.end()));
569 std::string description = "IBM PS";
570 ipzvpdVINIProps.emplace(
571 "DR", std::vector<uint8_t>(description.begin(), description.end()));
572
573 // Update the Resource Identifier (RI) keyword
574 // 2 byte FRC: 0x0003
575 // 2 byte RID: 0x1000, 0x1001...
576 std::uint8_t num = std::stoul(
577 inventoryPath.substr(inventoryPath.size() - 1, 1), nullptr, 0);
578 std::vector<uint8_t> ri{0x00, 0x03, 0x10, num};
579 ipzvpdDINFProps.emplace("RI", ri);
580
581 // Fill in the FRU Label (FL) keyword.
582 std::string fl = "E";
583 fl.push_back(inventoryPath.back());
584 fl.resize(FL_KW_SIZE, ' ');
585 ipzvpdDINFProps.emplace("FL",
586 std::vector<uint8_t>(fl.begin(), fl.end()));
587
588 interfaces.emplace(ASSET_IFACE, std::move(assetProps));
589 interfaces.emplace(VERSION_IFACE, std::move(versionProps));
590 interfaces.emplace(DINF_IFACE, std::move(ipzvpdDINFProps));
591 interfaces.emplace(VINI_IFACE, std::move(ipzvpdVINIProps));
592
George Liu070c1bc2020-10-12 11:28:01 +0800593 // Update the Functional
594 operProps.emplace(FUNCTIONAL_PROP, present);
595 interfaces.emplace(OPERATIONAL_STATE_IFACE, std::move(operProps));
596
Brandon Wyman1d7a7df2020-03-26 10:14:05 -0500597 auto path = inventoryPath.substr(strlen(INVENTORY_OBJ_PATH));
598 object.emplace(path, std::move(interfaces));
599
600 try
601 {
602 auto service =
603 util::getService(INVENTORY_OBJ_PATH, INVENTORY_MGR_IFACE, bus);
604
605 if (service.empty())
606 {
607 log<level::ERR>("Unable to get inventory manager service");
608 return;
609 }
610
611 auto method =
612 bus.new_method_call(service.c_str(), INVENTORY_OBJ_PATH,
613 INVENTORY_MGR_IFACE, "Notify");
614
615 method.append(std::move(object));
616
617 auto reply = bus.call(method);
618 }
Patrick Williamsc1d4de52021-10-06 12:45:57 -0500619 catch (const std::exception& e)
Brandon Wyman1d7a7df2020-03-26 10:14:05 -0500620 {
Jay Meyer6a3fd2c2020-08-25 16:37:16 -0500621 log<level::ERR>(
622 std::string(e.what() + std::string(" PATH=") + inventoryPath)
623 .c_str());
Brandon Wyman1d7a7df2020-03-26 10:14:05 -0500624 }
625#endif
626 }
627}
628
Adriana Kobylak4175ffb2021-08-02 14:51:05 +0000629void PowerSupply::getInputVoltage(double& actualInputVoltage,
630 int& inputVoltage) const
631{
632 using namespace phosphor::pmbus;
633
634 actualInputVoltage = in_input::VIN_VOLTAGE_0;
635 inputVoltage = in_input::VIN_VOLTAGE_0;
636
637 if (present)
638 {
639 try
640 {
641 // Read input voltage in millivolts
642 auto inputVoltageStr = pmbusIntf->readString(READ_VIN, Type::Hwmon);
643
644 // Convert to volts
645 actualInputVoltage = std::stod(inputVoltageStr) / 1000;
646
647 // Calculate the voltage based on voltage thresholds
648 if (actualInputVoltage < in_input::VIN_VOLTAGE_MIN)
649 {
650 inputVoltage = in_input::VIN_VOLTAGE_0;
651 }
652 else if (actualInputVoltage < in_input::VIN_VOLTAGE_110_THRESHOLD)
653 {
654 inputVoltage = in_input::VIN_VOLTAGE_110;
655 }
656 else
657 {
658 inputVoltage = in_input::VIN_VOLTAGE_220;
659 }
660 }
661 catch (const std::exception& e)
662 {
663 log<level::ERR>(
664 fmt::format("READ_VIN read error: {}", e.what()).c_str());
665 }
666 }
667}
668
Brandon Wyman3f1242f2020-01-28 13:11:25 -0600669} // namespace phosphor::power::psu