blob: 997be79c3cb81369e3ec00ebfe0dcabbb9e1aab9 [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 Wyman7ee4d7e2021-11-19 20:48:23 +0000217 statusFans12 = pmbusIntf->read(STATUS_FANS_1_2, Type::Debug);
Brandon Wyman96893a42021-11-05 19:56:57 +0000218 statusTemperature =
219 pmbusIntf->read(STATUS_TEMPERATURE, Type::Debug);
Brandon Wyman85c7bf42021-10-19 22:28:48 +0000220 if (statusWord & status_word::CML_FAULT)
221 {
222 if (!cmlFault)
223 {
Brandon Wyman43d32632021-10-26 23:44:11 +0000224 log<level::ERR>(
Brandon Wyman85c7bf42021-10-19 22:28:48 +0000225 fmt::format("CML fault: STATUS_WORD = {:#04x}, "
226 "STATUS_CML = {:#02x}",
227 statusWord, statusCML)
228 .c_str());
229 }
Brandon Wyman9ddc6222021-10-28 17:28:01 +0000230
Brandon Wyman85c7bf42021-10-19 22:28:48 +0000231 cmlFault = true;
232 }
233
Brandon Wyman3f1242f2020-01-28 13:11:25 -0600234 if (statusWord & status_word::INPUT_FAULT_WARN)
235 {
236 if (!inputFault)
237 {
Brandon Wyman43d32632021-10-26 23:44:11 +0000238 log<level::ERR>(
Brandon Wymanf07bc792021-10-12 19:00:35 +0000239 fmt::format("INPUT fault: STATUS_WORD = {:#04x}, "
240 "STATUS_MFR_SPECIFIC = {:#02x}, "
241 "STATUS_INPUT = {:#02x}",
242 statusWord, statusMFR, statusInput)
Brandon Wymanc8996602021-10-12 19:28:56 +0000243 .c_str());
Brandon Wyman3f1242f2020-01-28 13:11:25 -0600244 }
245
Brandon Wyman3f1242f2020-01-28 13:11:25 -0600246 inputFault = true;
247 }
248
Brandon Wyman6710ba22021-10-27 17:39:31 +0000249 if (statusWord & status_word::VOUT_OV_FAULT)
250 {
251 if (!voutOVFault)
252 {
Brandon Wyman43d32632021-10-26 23:44:11 +0000253 log<level::ERR>(
Brandon Wyman2cf46942021-10-28 19:09:16 +0000254 fmt::format(
255 "VOUT_OV_FAULT fault: STATUS_WORD = {:#04x}, "
256 "STATUS_MFR_SPECIFIC = {:#02x}, "
257 "STATUS_VOUT = {:#02x}",
258 statusWord, statusMFR, statusVout)
Brandon Wyman6710ba22021-10-27 17:39:31 +0000259 .c_str());
260 }
Brandon Wyman9ddc6222021-10-28 17:28:01 +0000261
Brandon Wyman6710ba22021-10-27 17:39:31 +0000262 voutOVFault = true;
263 }
264
Brandon Wymanb10b3be2021-11-09 22:12:15 +0000265 if (statusWord & status_word::IOUT_OC_FAULT)
266 {
267 if (!ioutOCFault)
268 {
269 log<level::ERR>(
270 fmt::format("IOUT fault: STATUS_WORD = {:#04x}, "
271 "STATUS_MFR_SPECIFIC = {:#02x}, "
272 "STATUS_IOUT = {:#02x}",
273 statusWord, statusMFR, statusIout)
274 .c_str());
275 }
276
277 ioutOCFault = true;
278 }
279
Brandon Wyman2cf46942021-10-28 19:09:16 +0000280 if ((statusWord & status_word::VOUT_FAULT) &&
281 !(statusWord & status_word::VOUT_OV_FAULT))
282 {
283 if (!voutUVFault)
284 {
285 log<level::ERR>(
286 fmt::format(
287 "VOUT_UV_FAULT fault: STATUS_WORD = {:#04x}, "
288 "STATUS_MFR_SPECIFIC = {:#02x}, "
289 "STATUS_VOUT = {:#02x}",
290 statusWord, statusMFR, statusVout)
291 .c_str());
292 }
293
294 voutUVFault = true;
295 }
296
Brandon Wyman7ee4d7e2021-11-19 20:48:23 +0000297 if (statusWord & status_word::FAN_FAULT)
298 {
299 if (!fanFault)
300 {
301 log<level::ERR>(
302 fmt::format("FANS fault/warning: "
303 "STATUS_WORD = {:#04x}, "
304 "STATUS_MFR_SPECIFIC = {:#02x}, "
305 "STATUS_FANS_1_2 = {:#02x}",
306 statusWord, statusMFR, statusFans12)
307 .c_str());
308 }
309
310 fanFault = true;
311 }
312
Brandon Wyman96893a42021-11-05 19:56:57 +0000313 if (statusWord & status_word::TEMPERATURE_FAULT_WARN)
314 {
315 if (!tempFault)
316 {
317 log<level::ERR>(
318 fmt::format("TEMPERATURE fault/warning: "
319 "STATUS_WORD = {:#04x}, "
320 "STATUS_MFR_SPECIFIC = {:#02x}, "
321 "STATUS_TEMPERATURE = {:#02x}",
322 statusWord, statusMFR,
323 statusTemperature)
324 .c_str());
325 }
326
327 tempFault = true;
328 }
329
Brandon Wyman2916ea52021-11-06 03:31:18 +0000330 if ((statusWord & status_word::POWER_GOOD_NEGATED) ||
331 (statusWord & status_word::UNIT_IS_OFF))
332 {
333 if (!pgoodFault)
334 {
335 log<level::ERR>(
336 fmt::format("PGOOD fault: "
337 "STATUS_WORD = {:#04x}, "
338 "STATUS_MFR_SPECIFIC = {:#02x}",
339 statusWord, statusMFR)
340 .c_str());
341 }
342
343 pgoodFault = true;
344 }
345
Brandon Wyman3f1242f2020-01-28 13:11:25 -0600346 if (statusWord & status_word::MFR_SPECIFIC_FAULT)
347 {
348 if (!mfrFault)
349 {
Brandon Wymanc8996602021-10-12 19:28:56 +0000350 log<level::ERR>(
351 fmt::format("MFR fault: "
352 "STATUS_WORD = {:#04x} "
353 "STATUS_MFR_SPECIFIC = {:#02x}",
354 statusWord, statusMFR)
355 .c_str());
Brandon Wyman3f1242f2020-01-28 13:11:25 -0600356 }
Brandon Wyman9ddc6222021-10-28 17:28:01 +0000357
Brandon Wyman3f1242f2020-01-28 13:11:25 -0600358 mfrFault = true;
359 }
360
361 if (statusWord & status_word::VIN_UV_FAULT)
362 {
363 if (!vinUVFault)
364 {
Brandon Wyman43d32632021-10-26 23:44:11 +0000365 log<level::ERR>(
Brandon Wymanf07bc792021-10-12 19:00:35 +0000366 fmt::format("VIN_UV fault: STATUS_WORD = {:#04x}, "
367 "STATUS_MFR_SPECIFIC = {:#02x}, "
368 "STATUS_INPUT = {:#02x}",
369 statusWord, statusMFR, statusInput)
Brandon Wymanc8996602021-10-12 19:28:56 +0000370 .c_str());
Brandon Wyman3f1242f2020-01-28 13:11:25 -0600371 }
372
Brandon Wyman3f1242f2020-01-28 13:11:25 -0600373 vinUVFault = true;
374 }
375 }
376 else
377 {
Brandon Wyman85c7bf42021-10-19 22:28:48 +0000378 cmlFault = false;
Brandon Wyman3f1242f2020-01-28 13:11:25 -0600379 inputFault = false;
380 mfrFault = false;
381 vinUVFault = false;
Brandon Wyman6710ba22021-10-27 17:39:31 +0000382 voutOVFault = false;
Brandon Wymanb10b3be2021-11-09 22:12:15 +0000383 ioutOCFault = false;
Brandon Wyman2cf46942021-10-28 19:09:16 +0000384 voutUVFault = false;
Brandon Wyman7ee4d7e2021-11-19 20:48:23 +0000385 fanFault = false;
Brandon Wyman96893a42021-11-05 19:56:57 +0000386 tempFault = false;
Brandon Wyman2916ea52021-11-06 03:31:18 +0000387 pgoodFault = false;
Brandon Wyman3f1242f2020-01-28 13:11:25 -0600388 }
389 }
Patrick Williamsc1d4de52021-10-06 12:45:57 -0500390 catch (const ReadFailure& e)
Brandon Wyman3f1242f2020-01-28 13:11:25 -0600391 {
Brandon Wymanf65c4062020-08-19 13:15:53 -0500392 readFail++;
Brandon Wyman3f1242f2020-01-28 13:11:25 -0600393 phosphor::logging::commit<ReadFailure>();
394 }
395 }
396}
397
Brandon Wyman59a35792020-06-04 12:37:40 -0500398void PowerSupply::onOffConfig(uint8_t data)
399{
400 using namespace phosphor::pmbus;
401
402 if (present)
403 {
404 log<level::INFO>("ON_OFF_CONFIG write", entry("DATA=0x%02X", data));
405 try
406 {
407 std::vector<uint8_t> configData{data};
408 pmbusIntf->writeBinary(ON_OFF_CONFIG, configData,
409 Type::HwmonDeviceDebug);
410 }
411 catch (...)
412 {
413 // The underlying code in writeBinary will log a message to the
B. J. Wyman681b2a32021-04-20 22:31:22 +0000414 // journal if the write fails. If the ON_OFF_CONFIG is not setup
415 // as desired, later fault detection and analysis code should
416 // catch any of the fall out. We should not need to terminate
417 // the application if this write fails.
Brandon Wyman59a35792020-06-04 12:37:40 -0500418 }
419 }
420}
421
Brandon Wyman3c208462020-05-13 16:25:58 -0500422void PowerSupply::clearFaults()
423{
Brandon Wyman5474c912021-02-23 14:39:43 -0600424 faultLogged = false;
Brandon Wyman3c208462020-05-13 16:25:58 -0500425 // The PMBus device driver does not allow for writing CLEAR_FAULTS
426 // directly. However, the pmbus hwmon device driver code will send a
427 // CLEAR_FAULTS after reading from any of the hwmon "files" in sysfs, so
428 // reading in1_input should result in clearing the fault bits in
429 // STATUS_BYTE/STATUS_WORD.
430 // I do not care what the return value is.
Brandon Wyman11151532020-11-10 13:45:57 -0600431 if (present)
Brandon Wyman3c208462020-05-13 16:25:58 -0500432 {
Brandon Wyman9564e942020-11-10 14:01:42 -0600433 inputFault = false;
434 mfrFault = false;
Jay Meyer10d94052020-11-30 14:41:21 -0600435 statusMFR = 0;
Brandon Wyman9564e942020-11-10 14:01:42 -0600436 vinUVFault = false;
Brandon Wyman85c7bf42021-10-19 22:28:48 +0000437 cmlFault = false;
Brandon Wyman6710ba22021-10-27 17:39:31 +0000438 voutOVFault = false;
Brandon Wymanb10b3be2021-11-09 22:12:15 +0000439 ioutOCFault = false;
Brandon Wyman2cf46942021-10-28 19:09:16 +0000440 voutUVFault = false;
Brandon Wyman7ee4d7e2021-11-19 20:48:23 +0000441 fanFault = false;
Brandon Wyman96893a42021-11-05 19:56:57 +0000442 tempFault = false;
Brandon Wyman2916ea52021-11-06 03:31:18 +0000443 pgoodFault = false;
Brandon Wyman9564e942020-11-10 14:01:42 -0600444 readFail = 0;
Brandon Wyman9564e942020-11-10 14:01:42 -0600445
Brandon Wyman11151532020-11-10 13:45:57 -0600446 try
447 {
448 static_cast<void>(
449 pmbusIntf->read("in1_input", phosphor::pmbus::Type::Hwmon));
450 }
Patrick Williamsc1d4de52021-10-06 12:45:57 -0500451 catch (const ReadFailure& e)
Brandon Wyman11151532020-11-10 13:45:57 -0600452 {
453 // Since I do not care what the return value is, I really do not
B. J. Wyman681b2a32021-04-20 22:31:22 +0000454 // care much if it gets a ReadFailure either. However, this
455 // should not prevent the application from continuing to run, so
456 // catching the read failure.
Brandon Wyman11151532020-11-10 13:45:57 -0600457 }
Brandon Wyman3c208462020-05-13 16:25:58 -0500458 }
459}
460
Brandon Wymanaed1f752019-11-25 18:10:52 -0600461void PowerSupply::inventoryChanged(sdbusplus::message::message& msg)
462{
463 std::string msgSensor;
Patrick Williamsabe49412020-05-13 17:59:47 -0500464 std::map<std::string, std::variant<uint32_t, bool>> msgData;
Brandon Wymanaed1f752019-11-25 18:10:52 -0600465 msg.read(msgSensor, msgData);
466
467 // Check if it was the Present property that changed.
468 auto valPropMap = msgData.find(PRESENT_PROP);
469 if (valPropMap != msgData.end())
470 {
471 if (std::get<bool>(valPropMap->second))
472 {
473 present = true;
B. J. Wyman681b2a32021-04-20 22:31:22 +0000474 // TODO: Immediately trying to read or write the "files" causes
475 // read or write failures.
Brandon Wyman1d7a7df2020-03-26 10:14:05 -0500476 using namespace std::chrono_literals;
477 std::this_thread::sleep_for(20ms);
Brandon Wyman9564e942020-11-10 14:01:42 -0600478 pmbusIntf->findHwmonDir();
Brandon Wyman59a35792020-06-04 12:37:40 -0500479 onOffConfig(phosphor::pmbus::ON_OFF_CONFIG_CONTROL_PIN_ONLY);
Brandon Wymanaed1f752019-11-25 18:10:52 -0600480 clearFaults();
Brandon Wyman1d7a7df2020-03-26 10:14:05 -0500481 updateInventory();
Brandon Wymanaed1f752019-11-25 18:10:52 -0600482 }
483 else
484 {
485 present = false;
486
487 // Clear out the now outdated inventory properties
488 updateInventory();
489 }
490 }
491}
492
Brandon Wyman9a507db2021-02-25 16:15:22 -0600493void PowerSupply::inventoryAdded(sdbusplus::message::message& msg)
494{
495 sdbusplus::message::object_path path;
496 msg.read(path);
497 // Make sure the signal is for the PSU inventory path
498 if (path == inventoryPath)
499 {
500 std::map<std::string, std::map<std::string, std::variant<bool>>>
501 interfaces;
502 // Get map of interfaces and their properties
503 msg.read(interfaces);
504
505 auto properties = interfaces.find(INVENTORY_IFACE);
506 if (properties != interfaces.end())
507 {
508 auto property = properties->second.find(PRESENT_PROP);
509 if (property != properties->second.end())
510 {
511 present = std::get<bool>(property->second);
512
513 log<level::INFO>(fmt::format("Power Supply {} Present {}",
514 inventoryPath, present)
515 .c_str());
516
517 updateInventory();
518 }
519 }
520 }
521}
522
Brandon Wyman1d7a7df2020-03-26 10:14:05 -0500523void PowerSupply::updateInventory()
524{
525 using namespace phosphor::pmbus;
526
Chanh Nguyenc12c53b2021-04-06 17:24:47 +0700527#if IBM_VPD
Brandon Wyman1d7a7df2020-03-26 10:14:05 -0500528 std::string ccin;
529 std::string pn;
530 std::string fn;
531 std::string header;
532 std::string sn;
Brandon Wyman1d7a7df2020-03-26 10:14:05 -0500533 using PropertyMap =
George Liu070c1bc2020-10-12 11:28:01 +0800534 std::map<std::string,
535 std::variant<std::string, std::vector<uint8_t>, bool>>;
Brandon Wyman1d7a7df2020-03-26 10:14:05 -0500536 PropertyMap assetProps;
George Liu070c1bc2020-10-12 11:28:01 +0800537 PropertyMap operProps;
Brandon Wyman1d7a7df2020-03-26 10:14:05 -0500538 PropertyMap versionProps;
539 PropertyMap ipzvpdDINFProps;
540 PropertyMap ipzvpdVINIProps;
541 using InterfaceMap = std::map<std::string, PropertyMap>;
542 InterfaceMap interfaces;
543 using ObjectMap = std::map<sdbusplus::message::object_path, InterfaceMap>;
544 ObjectMap object;
545#endif
B. J. Wyman681b2a32021-04-20 22:31:22 +0000546 log<level::DEBUG>(
547 fmt::format("updateInventory() inventoryPath: {}", inventoryPath)
548 .c_str());
Brandon Wyman1d7a7df2020-03-26 10:14:05 -0500549
550 if (present)
551 {
552 // TODO: non-IBM inventory updates?
553
Chanh Nguyenc12c53b2021-04-06 17:24:47 +0700554#if IBM_VPD
Brandon Wyman1d7a7df2020-03-26 10:14:05 -0500555 try
556 {
557 ccin = pmbusIntf->readString(CCIN, Type::HwmonDeviceDebug);
558 assetProps.emplace(MODEL_PROP, ccin);
Adriana Kobylak572a9052021-03-30 15:58:07 +0000559 modelName = ccin;
Brandon Wyman1d7a7df2020-03-26 10:14:05 -0500560 }
Patrick Williamsc1d4de52021-10-06 12:45:57 -0500561 catch (const ReadFailure& e)
Brandon Wyman1d7a7df2020-03-26 10:14:05 -0500562 {
B. J. Wyman681b2a32021-04-20 22:31:22 +0000563 // Ignore the read failure, let pmbus code indicate failure,
564 // path...
Brandon Wyman1d7a7df2020-03-26 10:14:05 -0500565 // TODO - ibm918
566 // https://github.com/openbmc/docs/blob/master/designs/vpd-collection.md
567 // The BMC must log errors if any of the VPD cannot be properly
568 // parsed or fails ECC checks.
569 }
570
571 try
572 {
573 pn = pmbusIntf->readString(PART_NUMBER, Type::HwmonDeviceDebug);
574 assetProps.emplace(PN_PROP, pn);
575 }
Patrick Williamsc1d4de52021-10-06 12:45:57 -0500576 catch (const ReadFailure& e)
Brandon Wyman1d7a7df2020-03-26 10:14:05 -0500577 {
B. J. Wyman681b2a32021-04-20 22:31:22 +0000578 // Ignore the read failure, let pmbus code indicate failure,
579 // path...
Brandon Wyman1d7a7df2020-03-26 10:14:05 -0500580 }
581
582 try
583 {
584 fn = pmbusIntf->readString(FRU_NUMBER, Type::HwmonDeviceDebug);
585 }
Patrick Williamsc1d4de52021-10-06 12:45:57 -0500586 catch (const ReadFailure& e)
Brandon Wyman1d7a7df2020-03-26 10:14:05 -0500587 {
B. J. Wyman681b2a32021-04-20 22:31:22 +0000588 // Ignore the read failure, let pmbus code indicate failure,
589 // path...
Brandon Wyman1d7a7df2020-03-26 10:14:05 -0500590 }
591
592 try
593 {
594 header =
595 pmbusIntf->readString(SERIAL_HEADER, Type::HwmonDeviceDebug);
596 sn = pmbusIntf->readString(SERIAL_NUMBER, Type::HwmonDeviceDebug);
597 assetProps.emplace(SN_PROP, sn);
598 }
Patrick Williamsc1d4de52021-10-06 12:45:57 -0500599 catch (const ReadFailure& e)
Brandon Wyman1d7a7df2020-03-26 10:14:05 -0500600 {
B. J. Wyman681b2a32021-04-20 22:31:22 +0000601 // Ignore the read failure, let pmbus code indicate failure,
602 // path...
Brandon Wyman1d7a7df2020-03-26 10:14:05 -0500603 }
604
605 try
606 {
Brandon Wymanc9efe412020-10-09 15:42:50 -0500607 fwVersion =
608 pmbusIntf->readString(FW_VERSION, Type::HwmonDeviceDebug);
609 versionProps.emplace(VERSION_PROP, fwVersion);
Brandon Wyman1d7a7df2020-03-26 10:14:05 -0500610 }
Patrick Williamsc1d4de52021-10-06 12:45:57 -0500611 catch (const ReadFailure& e)
Brandon Wyman1d7a7df2020-03-26 10:14:05 -0500612 {
B. J. Wyman681b2a32021-04-20 22:31:22 +0000613 // Ignore the read failure, let pmbus code indicate failure,
614 // path...
Brandon Wyman1d7a7df2020-03-26 10:14:05 -0500615 }
616
617 ipzvpdVINIProps.emplace("CC",
618 std::vector<uint8_t>(ccin.begin(), ccin.end()));
619 ipzvpdVINIProps.emplace("PN",
620 std::vector<uint8_t>(pn.begin(), pn.end()));
621 ipzvpdVINIProps.emplace("FN",
622 std::vector<uint8_t>(fn.begin(), fn.end()));
623 std::string header_sn = header + sn + '\0';
624 ipzvpdVINIProps.emplace(
625 "SN", std::vector<uint8_t>(header_sn.begin(), header_sn.end()));
626 std::string description = "IBM PS";
627 ipzvpdVINIProps.emplace(
628 "DR", std::vector<uint8_t>(description.begin(), description.end()));
629
630 // Update the Resource Identifier (RI) keyword
631 // 2 byte FRC: 0x0003
632 // 2 byte RID: 0x1000, 0x1001...
633 std::uint8_t num = std::stoul(
634 inventoryPath.substr(inventoryPath.size() - 1, 1), nullptr, 0);
635 std::vector<uint8_t> ri{0x00, 0x03, 0x10, num};
636 ipzvpdDINFProps.emplace("RI", ri);
637
638 // Fill in the FRU Label (FL) keyword.
639 std::string fl = "E";
640 fl.push_back(inventoryPath.back());
641 fl.resize(FL_KW_SIZE, ' ');
642 ipzvpdDINFProps.emplace("FL",
643 std::vector<uint8_t>(fl.begin(), fl.end()));
644
645 interfaces.emplace(ASSET_IFACE, std::move(assetProps));
646 interfaces.emplace(VERSION_IFACE, std::move(versionProps));
647 interfaces.emplace(DINF_IFACE, std::move(ipzvpdDINFProps));
648 interfaces.emplace(VINI_IFACE, std::move(ipzvpdVINIProps));
649
George Liu070c1bc2020-10-12 11:28:01 +0800650 // Update the Functional
651 operProps.emplace(FUNCTIONAL_PROP, present);
652 interfaces.emplace(OPERATIONAL_STATE_IFACE, std::move(operProps));
653
Brandon Wyman1d7a7df2020-03-26 10:14:05 -0500654 auto path = inventoryPath.substr(strlen(INVENTORY_OBJ_PATH));
655 object.emplace(path, std::move(interfaces));
656
657 try
658 {
659 auto service =
660 util::getService(INVENTORY_OBJ_PATH, INVENTORY_MGR_IFACE, bus);
661
662 if (service.empty())
663 {
664 log<level::ERR>("Unable to get inventory manager service");
665 return;
666 }
667
668 auto method =
669 bus.new_method_call(service.c_str(), INVENTORY_OBJ_PATH,
670 INVENTORY_MGR_IFACE, "Notify");
671
672 method.append(std::move(object));
673
674 auto reply = bus.call(method);
675 }
Patrick Williamsc1d4de52021-10-06 12:45:57 -0500676 catch (const std::exception& e)
Brandon Wyman1d7a7df2020-03-26 10:14:05 -0500677 {
Jay Meyer6a3fd2c2020-08-25 16:37:16 -0500678 log<level::ERR>(
679 std::string(e.what() + std::string(" PATH=") + inventoryPath)
680 .c_str());
Brandon Wyman1d7a7df2020-03-26 10:14:05 -0500681 }
682#endif
683 }
684}
685
Adriana Kobylak4175ffb2021-08-02 14:51:05 +0000686void PowerSupply::getInputVoltage(double& actualInputVoltage,
687 int& inputVoltage) const
688{
689 using namespace phosphor::pmbus;
690
691 actualInputVoltage = in_input::VIN_VOLTAGE_0;
692 inputVoltage = in_input::VIN_VOLTAGE_0;
693
694 if (present)
695 {
696 try
697 {
698 // Read input voltage in millivolts
699 auto inputVoltageStr = pmbusIntf->readString(READ_VIN, Type::Hwmon);
700
701 // Convert to volts
702 actualInputVoltage = std::stod(inputVoltageStr) / 1000;
703
704 // Calculate the voltage based on voltage thresholds
705 if (actualInputVoltage < in_input::VIN_VOLTAGE_MIN)
706 {
707 inputVoltage = in_input::VIN_VOLTAGE_0;
708 }
709 else if (actualInputVoltage < in_input::VIN_VOLTAGE_110_THRESHOLD)
710 {
711 inputVoltage = in_input::VIN_VOLTAGE_110;
712 }
713 else
714 {
715 inputVoltage = in_input::VIN_VOLTAGE_220;
716 }
717 }
718 catch (const std::exception& e)
719 {
720 log<level::ERR>(
721 fmt::format("READ_VIN read error: {}", e.what()).c_str());
722 }
723 }
724}
725
Brandon Wyman3f1242f2020-01-28 13:11:25 -0600726} // namespace phosphor::power::psu