blob: 2cef36874c6b8070c0f18f821130407558f2a001 [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 Wyman3f1242f2020-01-28 13:11:25 -0600279 if (statusWord & status_word::MFR_SPECIFIC_FAULT)
280 {
281 if (!mfrFault)
282 {
Brandon Wymanc8996602021-10-12 19:28:56 +0000283 log<level::ERR>(
284 fmt::format("MFR fault: "
285 "STATUS_WORD = {:#04x} "
286 "STATUS_MFR_SPECIFIC = {:#02x}",
287 statusWord, statusMFR)
288 .c_str());
Brandon Wyman3f1242f2020-01-28 13:11:25 -0600289 }
Brandon Wyman9ddc6222021-10-28 17:28:01 +0000290
Brandon Wyman3f1242f2020-01-28 13:11:25 -0600291 mfrFault = true;
292 }
293
294 if (statusWord & status_word::VIN_UV_FAULT)
295 {
296 if (!vinUVFault)
297 {
Brandon Wyman43d32632021-10-26 23:44:11 +0000298 log<level::ERR>(
Brandon Wymanf07bc792021-10-12 19:00:35 +0000299 fmt::format("VIN_UV fault: STATUS_WORD = {:#04x}, "
300 "STATUS_MFR_SPECIFIC = {:#02x}, "
301 "STATUS_INPUT = {:#02x}",
302 statusWord, statusMFR, statusInput)
Brandon Wymanc8996602021-10-12 19:28:56 +0000303 .c_str());
Brandon Wyman3f1242f2020-01-28 13:11:25 -0600304 }
305
Brandon Wyman3f1242f2020-01-28 13:11:25 -0600306 vinUVFault = true;
307 }
308 }
309 else
310 {
Brandon Wyman85c7bf42021-10-19 22:28:48 +0000311 cmlFault = false;
Brandon Wyman3f1242f2020-01-28 13:11:25 -0600312 inputFault = false;
313 mfrFault = false;
314 vinUVFault = false;
Brandon Wyman6710ba22021-10-27 17:39:31 +0000315 voutOVFault = false;
Brandon Wyman96893a42021-11-05 19:56:57 +0000316 tempFault = false;
Brandon Wyman3f1242f2020-01-28 13:11:25 -0600317 }
318 }
Patrick Williamsc1d4de52021-10-06 12:45:57 -0500319 catch (const ReadFailure& e)
Brandon Wyman3f1242f2020-01-28 13:11:25 -0600320 {
Brandon Wymanf65c4062020-08-19 13:15:53 -0500321 readFail++;
Brandon Wyman3f1242f2020-01-28 13:11:25 -0600322 phosphor::logging::commit<ReadFailure>();
323 }
324 }
325}
326
Brandon Wyman59a35792020-06-04 12:37:40 -0500327void PowerSupply::onOffConfig(uint8_t data)
328{
329 using namespace phosphor::pmbus;
330
331 if (present)
332 {
333 log<level::INFO>("ON_OFF_CONFIG write", entry("DATA=0x%02X", data));
334 try
335 {
336 std::vector<uint8_t> configData{data};
337 pmbusIntf->writeBinary(ON_OFF_CONFIG, configData,
338 Type::HwmonDeviceDebug);
339 }
340 catch (...)
341 {
342 // The underlying code in writeBinary will log a message to the
B. J. Wyman681b2a32021-04-20 22:31:22 +0000343 // journal if the write fails. If the ON_OFF_CONFIG is not setup
344 // as desired, later fault detection and analysis code should
345 // catch any of the fall out. We should not need to terminate
346 // the application if this write fails.
Brandon Wyman59a35792020-06-04 12:37:40 -0500347 }
348 }
349}
350
Brandon Wyman3c208462020-05-13 16:25:58 -0500351void PowerSupply::clearFaults()
352{
Brandon Wyman5474c912021-02-23 14:39:43 -0600353 faultLogged = false;
Brandon Wyman3c208462020-05-13 16:25:58 -0500354 // The PMBus device driver does not allow for writing CLEAR_FAULTS
355 // directly. However, the pmbus hwmon device driver code will send a
356 // CLEAR_FAULTS after reading from any of the hwmon "files" in sysfs, so
357 // reading in1_input should result in clearing the fault bits in
358 // STATUS_BYTE/STATUS_WORD.
359 // I do not care what the return value is.
Brandon Wyman11151532020-11-10 13:45:57 -0600360 if (present)
Brandon Wyman3c208462020-05-13 16:25:58 -0500361 {
Brandon Wyman9564e942020-11-10 14:01:42 -0600362 inputFault = false;
363 mfrFault = false;
Jay Meyer10d94052020-11-30 14:41:21 -0600364 statusMFR = 0;
Brandon Wyman9564e942020-11-10 14:01:42 -0600365 vinUVFault = false;
Brandon Wyman85c7bf42021-10-19 22:28:48 +0000366 cmlFault = false;
Brandon Wyman6710ba22021-10-27 17:39:31 +0000367 voutOVFault = false;
Brandon Wyman96893a42021-11-05 19:56:57 +0000368 tempFault = false;
Brandon Wyman9564e942020-11-10 14:01:42 -0600369 readFail = 0;
Brandon Wyman9564e942020-11-10 14:01:42 -0600370
Brandon Wyman11151532020-11-10 13:45:57 -0600371 try
372 {
373 static_cast<void>(
374 pmbusIntf->read("in1_input", phosphor::pmbus::Type::Hwmon));
375 }
Patrick Williamsc1d4de52021-10-06 12:45:57 -0500376 catch (const ReadFailure& e)
Brandon Wyman11151532020-11-10 13:45:57 -0600377 {
378 // Since I do not care what the return value is, I really do not
B. J. Wyman681b2a32021-04-20 22:31:22 +0000379 // care much if it gets a ReadFailure either. However, this
380 // should not prevent the application from continuing to run, so
381 // catching the read failure.
Brandon Wyman11151532020-11-10 13:45:57 -0600382 }
Brandon Wyman3c208462020-05-13 16:25:58 -0500383 }
384}
385
Brandon Wymanaed1f752019-11-25 18:10:52 -0600386void PowerSupply::inventoryChanged(sdbusplus::message::message& msg)
387{
388 std::string msgSensor;
Patrick Williamsabe49412020-05-13 17:59:47 -0500389 std::map<std::string, std::variant<uint32_t, bool>> msgData;
Brandon Wymanaed1f752019-11-25 18:10:52 -0600390 msg.read(msgSensor, msgData);
391
392 // Check if it was the Present property that changed.
393 auto valPropMap = msgData.find(PRESENT_PROP);
394 if (valPropMap != msgData.end())
395 {
396 if (std::get<bool>(valPropMap->second))
397 {
398 present = true;
B. J. Wyman681b2a32021-04-20 22:31:22 +0000399 // TODO: Immediately trying to read or write the "files" causes
400 // read or write failures.
Brandon Wyman1d7a7df2020-03-26 10:14:05 -0500401 using namespace std::chrono_literals;
402 std::this_thread::sleep_for(20ms);
Brandon Wyman9564e942020-11-10 14:01:42 -0600403 pmbusIntf->findHwmonDir();
Brandon Wyman59a35792020-06-04 12:37:40 -0500404 onOffConfig(phosphor::pmbus::ON_OFF_CONFIG_CONTROL_PIN_ONLY);
Brandon Wymanaed1f752019-11-25 18:10:52 -0600405 clearFaults();
Brandon Wyman1d7a7df2020-03-26 10:14:05 -0500406 updateInventory();
Brandon Wymanaed1f752019-11-25 18:10:52 -0600407 }
408 else
409 {
410 present = false;
411
412 // Clear out the now outdated inventory properties
413 updateInventory();
414 }
415 }
416}
417
Brandon Wyman9a507db2021-02-25 16:15:22 -0600418void PowerSupply::inventoryAdded(sdbusplus::message::message& msg)
419{
420 sdbusplus::message::object_path path;
421 msg.read(path);
422 // Make sure the signal is for the PSU inventory path
423 if (path == inventoryPath)
424 {
425 std::map<std::string, std::map<std::string, std::variant<bool>>>
426 interfaces;
427 // Get map of interfaces and their properties
428 msg.read(interfaces);
429
430 auto properties = interfaces.find(INVENTORY_IFACE);
431 if (properties != interfaces.end())
432 {
433 auto property = properties->second.find(PRESENT_PROP);
434 if (property != properties->second.end())
435 {
436 present = std::get<bool>(property->second);
437
438 log<level::INFO>(fmt::format("Power Supply {} Present {}",
439 inventoryPath, present)
440 .c_str());
441
442 updateInventory();
443 }
444 }
445 }
446}
447
Brandon Wyman1d7a7df2020-03-26 10:14:05 -0500448void PowerSupply::updateInventory()
449{
450 using namespace phosphor::pmbus;
451
Chanh Nguyenc12c53b2021-04-06 17:24:47 +0700452#if IBM_VPD
Brandon Wyman1d7a7df2020-03-26 10:14:05 -0500453 std::string ccin;
454 std::string pn;
455 std::string fn;
456 std::string header;
457 std::string sn;
Brandon Wyman1d7a7df2020-03-26 10:14:05 -0500458 using PropertyMap =
George Liu070c1bc2020-10-12 11:28:01 +0800459 std::map<std::string,
460 std::variant<std::string, std::vector<uint8_t>, bool>>;
Brandon Wyman1d7a7df2020-03-26 10:14:05 -0500461 PropertyMap assetProps;
George Liu070c1bc2020-10-12 11:28:01 +0800462 PropertyMap operProps;
Brandon Wyman1d7a7df2020-03-26 10:14:05 -0500463 PropertyMap versionProps;
464 PropertyMap ipzvpdDINFProps;
465 PropertyMap ipzvpdVINIProps;
466 using InterfaceMap = std::map<std::string, PropertyMap>;
467 InterfaceMap interfaces;
468 using ObjectMap = std::map<sdbusplus::message::object_path, InterfaceMap>;
469 ObjectMap object;
470#endif
B. J. Wyman681b2a32021-04-20 22:31:22 +0000471 log<level::DEBUG>(
472 fmt::format("updateInventory() inventoryPath: {}", inventoryPath)
473 .c_str());
Brandon Wyman1d7a7df2020-03-26 10:14:05 -0500474
475 if (present)
476 {
477 // TODO: non-IBM inventory updates?
478
Chanh Nguyenc12c53b2021-04-06 17:24:47 +0700479#if IBM_VPD
Brandon Wyman1d7a7df2020-03-26 10:14:05 -0500480 try
481 {
482 ccin = pmbusIntf->readString(CCIN, Type::HwmonDeviceDebug);
483 assetProps.emplace(MODEL_PROP, ccin);
Adriana Kobylak572a9052021-03-30 15:58:07 +0000484 modelName = ccin;
Brandon Wyman1d7a7df2020-03-26 10:14:05 -0500485 }
Patrick Williamsc1d4de52021-10-06 12:45:57 -0500486 catch (const ReadFailure& e)
Brandon Wyman1d7a7df2020-03-26 10:14:05 -0500487 {
B. J. Wyman681b2a32021-04-20 22:31:22 +0000488 // Ignore the read failure, let pmbus code indicate failure,
489 // path...
Brandon Wyman1d7a7df2020-03-26 10:14:05 -0500490 // TODO - ibm918
491 // https://github.com/openbmc/docs/blob/master/designs/vpd-collection.md
492 // The BMC must log errors if any of the VPD cannot be properly
493 // parsed or fails ECC checks.
494 }
495
496 try
497 {
498 pn = pmbusIntf->readString(PART_NUMBER, Type::HwmonDeviceDebug);
499 assetProps.emplace(PN_PROP, pn);
500 }
Patrick Williamsc1d4de52021-10-06 12:45:57 -0500501 catch (const ReadFailure& e)
Brandon Wyman1d7a7df2020-03-26 10:14:05 -0500502 {
B. J. Wyman681b2a32021-04-20 22:31:22 +0000503 // Ignore the read failure, let pmbus code indicate failure,
504 // path...
Brandon Wyman1d7a7df2020-03-26 10:14:05 -0500505 }
506
507 try
508 {
509 fn = pmbusIntf->readString(FRU_NUMBER, Type::HwmonDeviceDebug);
510 }
Patrick Williamsc1d4de52021-10-06 12:45:57 -0500511 catch (const ReadFailure& e)
Brandon Wyman1d7a7df2020-03-26 10:14:05 -0500512 {
B. J. Wyman681b2a32021-04-20 22:31:22 +0000513 // Ignore the read failure, let pmbus code indicate failure,
514 // path...
Brandon Wyman1d7a7df2020-03-26 10:14:05 -0500515 }
516
517 try
518 {
519 header =
520 pmbusIntf->readString(SERIAL_HEADER, Type::HwmonDeviceDebug);
521 sn = pmbusIntf->readString(SERIAL_NUMBER, Type::HwmonDeviceDebug);
522 assetProps.emplace(SN_PROP, sn);
523 }
Patrick Williamsc1d4de52021-10-06 12:45:57 -0500524 catch (const ReadFailure& e)
Brandon Wyman1d7a7df2020-03-26 10:14:05 -0500525 {
B. J. Wyman681b2a32021-04-20 22:31:22 +0000526 // Ignore the read failure, let pmbus code indicate failure,
527 // path...
Brandon Wyman1d7a7df2020-03-26 10:14:05 -0500528 }
529
530 try
531 {
Brandon Wymanc9efe412020-10-09 15:42:50 -0500532 fwVersion =
533 pmbusIntf->readString(FW_VERSION, Type::HwmonDeviceDebug);
534 versionProps.emplace(VERSION_PROP, fwVersion);
Brandon Wyman1d7a7df2020-03-26 10:14:05 -0500535 }
Patrick Williamsc1d4de52021-10-06 12:45:57 -0500536 catch (const ReadFailure& e)
Brandon Wyman1d7a7df2020-03-26 10:14:05 -0500537 {
B. J. Wyman681b2a32021-04-20 22:31:22 +0000538 // Ignore the read failure, let pmbus code indicate failure,
539 // path...
Brandon Wyman1d7a7df2020-03-26 10:14:05 -0500540 }
541
542 ipzvpdVINIProps.emplace("CC",
543 std::vector<uint8_t>(ccin.begin(), ccin.end()));
544 ipzvpdVINIProps.emplace("PN",
545 std::vector<uint8_t>(pn.begin(), pn.end()));
546 ipzvpdVINIProps.emplace("FN",
547 std::vector<uint8_t>(fn.begin(), fn.end()));
548 std::string header_sn = header + sn + '\0';
549 ipzvpdVINIProps.emplace(
550 "SN", std::vector<uint8_t>(header_sn.begin(), header_sn.end()));
551 std::string description = "IBM PS";
552 ipzvpdVINIProps.emplace(
553 "DR", std::vector<uint8_t>(description.begin(), description.end()));
554
555 // Update the Resource Identifier (RI) keyword
556 // 2 byte FRC: 0x0003
557 // 2 byte RID: 0x1000, 0x1001...
558 std::uint8_t num = std::stoul(
559 inventoryPath.substr(inventoryPath.size() - 1, 1), nullptr, 0);
560 std::vector<uint8_t> ri{0x00, 0x03, 0x10, num};
561 ipzvpdDINFProps.emplace("RI", ri);
562
563 // Fill in the FRU Label (FL) keyword.
564 std::string fl = "E";
565 fl.push_back(inventoryPath.back());
566 fl.resize(FL_KW_SIZE, ' ');
567 ipzvpdDINFProps.emplace("FL",
568 std::vector<uint8_t>(fl.begin(), fl.end()));
569
570 interfaces.emplace(ASSET_IFACE, std::move(assetProps));
571 interfaces.emplace(VERSION_IFACE, std::move(versionProps));
572 interfaces.emplace(DINF_IFACE, std::move(ipzvpdDINFProps));
573 interfaces.emplace(VINI_IFACE, std::move(ipzvpdVINIProps));
574
George Liu070c1bc2020-10-12 11:28:01 +0800575 // Update the Functional
576 operProps.emplace(FUNCTIONAL_PROP, present);
577 interfaces.emplace(OPERATIONAL_STATE_IFACE, std::move(operProps));
578
Brandon Wyman1d7a7df2020-03-26 10:14:05 -0500579 auto path = inventoryPath.substr(strlen(INVENTORY_OBJ_PATH));
580 object.emplace(path, std::move(interfaces));
581
582 try
583 {
584 auto service =
585 util::getService(INVENTORY_OBJ_PATH, INVENTORY_MGR_IFACE, bus);
586
587 if (service.empty())
588 {
589 log<level::ERR>("Unable to get inventory manager service");
590 return;
591 }
592
593 auto method =
594 bus.new_method_call(service.c_str(), INVENTORY_OBJ_PATH,
595 INVENTORY_MGR_IFACE, "Notify");
596
597 method.append(std::move(object));
598
599 auto reply = bus.call(method);
600 }
Patrick Williamsc1d4de52021-10-06 12:45:57 -0500601 catch (const std::exception& e)
Brandon Wyman1d7a7df2020-03-26 10:14:05 -0500602 {
Jay Meyer6a3fd2c2020-08-25 16:37:16 -0500603 log<level::ERR>(
604 std::string(e.what() + std::string(" PATH=") + inventoryPath)
605 .c_str());
Brandon Wyman1d7a7df2020-03-26 10:14:05 -0500606 }
607#endif
608 }
609}
610
Adriana Kobylak4175ffb2021-08-02 14:51:05 +0000611void PowerSupply::getInputVoltage(double& actualInputVoltage,
612 int& inputVoltage) const
613{
614 using namespace phosphor::pmbus;
615
616 actualInputVoltage = in_input::VIN_VOLTAGE_0;
617 inputVoltage = in_input::VIN_VOLTAGE_0;
618
619 if (present)
620 {
621 try
622 {
623 // Read input voltage in millivolts
624 auto inputVoltageStr = pmbusIntf->readString(READ_VIN, Type::Hwmon);
625
626 // Convert to volts
627 actualInputVoltage = std::stod(inputVoltageStr) / 1000;
628
629 // Calculate the voltage based on voltage thresholds
630 if (actualInputVoltage < in_input::VIN_VOLTAGE_MIN)
631 {
632 inputVoltage = in_input::VIN_VOLTAGE_0;
633 }
634 else if (actualInputVoltage < in_input::VIN_VOLTAGE_110_THRESHOLD)
635 {
636 inputVoltage = in_input::VIN_VOLTAGE_110;
637 }
638 else
639 {
640 inputVoltage = in_input::VIN_VOLTAGE_220;
641 }
642 }
643 catch (const std::exception& e)
644 {
645 log<level::ERR>(
646 fmt::format("READ_VIN read error: {}", e.what()).c_str());
647 }
648 }
649}
650
Brandon Wyman3f1242f2020-01-28 13:11:25 -0600651} // namespace phosphor::power::psu