blob: bdfc6bb09c210ba89eaece653809dcfb7eef5e4d [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());
Matt Spinlerca1e9ea2022-02-18 14:03:08 -0600171
172 auto invpath = inventoryPath.substr(strlen(INVENTORY_OBJ_PATH));
173 auto const lastSlashPos = invpath.find_last_of('/');
174 std::string prettyName = invpath.substr(lastSlashPos + 1);
175 setPresence(bus, invpath, present, prettyName);
176 updateInventory();
177
178 // Need Functional to already be correct before calling this
179 checkAvailability();
180
B. J. Wyman681b2a32021-04-20 22:31:22 +0000181 if (present)
182 {
183 std::this_thread::sleep_for(std::chrono::milliseconds(bindDelay));
184 bindOrUnbindDriver(present);
185 pmbusIntf->findHwmonDir();
186 onOffConfig(phosphor::pmbus::ON_OFF_CONFIG_CONTROL_PIN_ONLY);
187 clearFaults();
188 }
189 else
190 {
191 bindOrUnbindDriver(present);
192 }
B. J. Wyman681b2a32021-04-20 22:31:22 +0000193 }
194}
195
Brandon Wymanc2203432021-12-21 23:09:48 +0000196void PowerSupply::analyzeCMLFault()
197{
198 if (statusWord & phosphor::pmbus::status_word::CML_FAULT)
199 {
Brandon Wymanc2906f42021-12-21 20:14:56 +0000200 if (cmlFault < DEGLITCH_LIMIT)
Brandon Wymanc2203432021-12-21 23:09:48 +0000201 {
202 log<level::ERR>(fmt::format("CML fault: STATUS_WORD = {:#04x}, "
203 "STATUS_CML = {:#02x}",
204 statusWord, statusCML)
205 .c_str());
Brandon Wymanc2203432021-12-21 23:09:48 +0000206
Brandon Wymanc2906f42021-12-21 20:14:56 +0000207 cmlFault++;
208 }
209 }
210 else
211 {
212 cmlFault = 0;
Brandon Wymanc2203432021-12-21 23:09:48 +0000213 }
214}
215
Brandon Wymane3b0bb02021-12-21 23:16:48 +0000216void PowerSupply::analyzeInputFault()
217{
218 if (statusWord & phosphor::pmbus::status_word::INPUT_FAULT_WARN)
219 {
Brandon Wymanc2906f42021-12-21 20:14:56 +0000220 if (inputFault < DEGLITCH_LIMIT)
Brandon Wymane3b0bb02021-12-21 23:16:48 +0000221 {
222 log<level::ERR>(fmt::format("INPUT fault: STATUS_WORD = {:#04x}, "
223 "STATUS_MFR_SPECIFIC = {:#02x}, "
224 "STATUS_INPUT = {:#02x}",
225 statusWord, statusMFR, statusInput)
226 .c_str());
Brandon Wymane3b0bb02021-12-21 23:16:48 +0000227
Brandon Wymanc2906f42021-12-21 20:14:56 +0000228 inputFault++;
229 }
Brandon Wymane3b0bb02021-12-21 23:16:48 +0000230 }
Brandon Wyman82affd92021-11-24 19:12:49 +0000231
232 // If had INPUT/VIN_UV fault, and now off.
233 // Trace that odd behavior.
234 if (inputFault &&
235 !(statusWord & phosphor::pmbus::status_word::INPUT_FAULT_WARN))
236 {
237 log<level::INFO>(
238 fmt::format("INPUT fault cleared: STATUS_WORD = {:#04x}, "
239 "STATUS_MFR_SPECIFIC = {:#02x}, "
240 "STATUS_INPUT = {:#02x}",
241 statusWord, statusMFR, statusInput)
242 .c_str());
Brandon Wymanc2906f42021-12-21 20:14:56 +0000243 inputFault = 0;
Brandon Wyman82affd92021-11-24 19:12:49 +0000244 }
Brandon Wymane3b0bb02021-12-21 23:16:48 +0000245}
246
Brandon Wymanc2c87132021-12-21 23:22:18 +0000247void PowerSupply::analyzeVoutOVFault()
248{
249 if (statusWord & phosphor::pmbus::status_word::VOUT_OV_FAULT)
250 {
Brandon Wymanc2906f42021-12-21 20:14:56 +0000251 if (voutOVFault < DEGLITCH_LIMIT)
Brandon Wymanc2c87132021-12-21 23:22:18 +0000252 {
253 log<level::ERR>(
254 fmt::format("VOUT_OV_FAULT fault: STATUS_WORD = {:#04x}, "
255 "STATUS_MFR_SPECIFIC = {:#02x}, "
256 "STATUS_VOUT = {:#02x}",
257 statusWord, statusMFR, statusVout)
258 .c_str());
Brandon Wymanc2c87132021-12-21 23:22:18 +0000259
Brandon Wymanc2906f42021-12-21 20:14:56 +0000260 voutOVFault++;
261 }
262 }
263 else
264 {
265 voutOVFault = 0;
Brandon Wymanc2c87132021-12-21 23:22:18 +0000266 }
267}
268
Brandon Wymana00e7302021-12-21 23:28:29 +0000269void PowerSupply::analyzeIoutOCFault()
270{
271 if (statusWord & phosphor::pmbus::status_word::IOUT_OC_FAULT)
272 {
Brandon Wymanc2906f42021-12-21 20:14:56 +0000273 if (ioutOCFault < DEGLITCH_LIMIT)
Brandon Wymana00e7302021-12-21 23:28:29 +0000274 {
275 log<level::ERR>(fmt::format("IOUT fault: STATUS_WORD = {:#04x}, "
276 "STATUS_MFR_SPECIFIC = {:#02x}, "
277 "STATUS_IOUT = {:#02x}",
278 statusWord, statusMFR, statusIout)
279 .c_str());
Brandon Wymana00e7302021-12-21 23:28:29 +0000280
Brandon Wymanc2906f42021-12-21 20:14:56 +0000281 ioutOCFault++;
282 }
283 }
284 else
285 {
286 ioutOCFault = 0;
Brandon Wymana00e7302021-12-21 23:28:29 +0000287 }
288}
289
Brandon Wyman08378782021-12-21 23:48:15 +0000290void PowerSupply::analyzeVoutUVFault()
291{
292 if ((statusWord & phosphor::pmbus::status_word::VOUT_FAULT) &&
293 !(statusWord & phosphor::pmbus::status_word::VOUT_OV_FAULT))
294 {
Brandon Wymanc2906f42021-12-21 20:14:56 +0000295 if (voutUVFault < DEGLITCH_LIMIT)
Brandon Wyman08378782021-12-21 23:48:15 +0000296 {
297 log<level::ERR>(
298 fmt::format("VOUT_UV_FAULT fault: STATUS_WORD = {:#04x}, "
299 "STATUS_MFR_SPECIFIC = {:#02x}, "
300 "STATUS_VOUT = {:#02x}",
301 statusWord, statusMFR, statusVout)
302 .c_str());
Brandon Wyman08378782021-12-21 23:48:15 +0000303
Brandon Wymanc2906f42021-12-21 20:14:56 +0000304 voutUVFault++;
305 }
306 }
307 else
308 {
309 voutUVFault = 0;
Brandon Wyman08378782021-12-21 23:48:15 +0000310 }
311}
312
Brandon Wymand5d9a222021-12-21 23:59:05 +0000313void PowerSupply::analyzeFanFault()
314{
315 if (statusWord & phosphor::pmbus::status_word::FAN_FAULT)
316 {
Brandon Wymanc2906f42021-12-21 20:14:56 +0000317 if (fanFault < DEGLITCH_LIMIT)
Brandon Wymand5d9a222021-12-21 23:59:05 +0000318 {
319 log<level::ERR>(fmt::format("FANS fault/warning: "
320 "STATUS_WORD = {:#04x}, "
321 "STATUS_MFR_SPECIFIC = {:#02x}, "
322 "STATUS_FANS_1_2 = {:#02x}",
323 statusWord, statusMFR, statusFans12)
324 .c_str());
Brandon Wymand5d9a222021-12-21 23:59:05 +0000325
Brandon Wymanc2906f42021-12-21 20:14:56 +0000326 fanFault++;
327 }
328 }
329 else
330 {
331 fanFault = 0;
Brandon Wymand5d9a222021-12-21 23:59:05 +0000332 }
333}
334
Brandon Wyman52cb3f22021-12-21 23:02:47 +0000335void PowerSupply::analyzeTemperatureFault()
336{
337 if (statusWord & phosphor::pmbus::status_word::TEMPERATURE_FAULT_WARN)
338 {
Brandon Wymanc2906f42021-12-21 20:14:56 +0000339 if (tempFault < DEGLITCH_LIMIT)
Brandon Wyman52cb3f22021-12-21 23:02:47 +0000340 {
341 log<level::ERR>(fmt::format("TEMPERATURE fault/warning: "
342 "STATUS_WORD = {:#04x}, "
343 "STATUS_MFR_SPECIFIC = {:#02x}, "
344 "STATUS_TEMPERATURE = {:#02x}",
345 statusWord, statusMFR,
346 statusTemperature)
347 .c_str());
Brandon Wyman52cb3f22021-12-21 23:02:47 +0000348
Brandon Wymanc2906f42021-12-21 20:14:56 +0000349 tempFault++;
350 }
351 }
352 else
353 {
354 tempFault = 0;
Brandon Wyman52cb3f22021-12-21 23:02:47 +0000355 }
356}
357
Brandon Wyman993b5542021-12-21 22:55:16 +0000358void PowerSupply::analyzePgoodFault()
359{
360 if ((statusWord & phosphor::pmbus::status_word::POWER_GOOD_NEGATED) ||
361 (statusWord & phosphor::pmbus::status_word::UNIT_IS_OFF))
362 {
363 if (pgoodFault < DEGLITCH_LIMIT)
364 {
365 log<level::ERR>(fmt::format("PGOOD fault: "
366 "STATUS_WORD = {:#04x}, "
367 "STATUS_MFR_SPECIFIC = {:#02x}",
368 statusWord, statusMFR)
369 .c_str());
370
371 pgoodFault++;
372 }
373 }
374 else
375 {
376 pgoodFault = 0;
377 }
378}
379
Brandon Wyman39ea02b2021-11-23 23:22:23 +0000380void PowerSupply::determineMFRFault()
381{
382 if (bindPath.string().find("ibm-cffps") != std::string::npos)
383 {
384 // IBM MFR_SPECIFIC[4] is PS_Kill fault
385 if (statusMFR & 0x10)
386 {
Brandon Wymanc2906f42021-12-21 20:14:56 +0000387 if (psKillFault < DEGLITCH_LIMIT)
388 {
389 psKillFault++;
390 }
391 }
392 else
393 {
394 psKillFault = 0;
Brandon Wyman39ea02b2021-11-23 23:22:23 +0000395 }
396 // IBM MFR_SPECIFIC[6] is 12Vcs fault.
397 if (statusMFR & 0x40)
398 {
Brandon Wymanc2906f42021-12-21 20:14:56 +0000399 if (ps12VcsFault < DEGLITCH_LIMIT)
400 {
401 ps12VcsFault++;
402 }
403 }
404 else
405 {
406 ps12VcsFault = 0;
Brandon Wyman39ea02b2021-11-23 23:22:23 +0000407 }
408 // IBM MFR_SPECIFIC[7] is 12V Current-Share fault.
409 if (statusMFR & 0x80)
410 {
Brandon Wymanc2906f42021-12-21 20:14:56 +0000411 if (psCS12VFault < DEGLITCH_LIMIT)
412 {
413 psCS12VFault++;
414 }
415 }
416 else
417 {
418 psCS12VFault = 0;
Brandon Wyman39ea02b2021-11-23 23:22:23 +0000419 }
420 }
421}
422
Brandon Wyman6c2ac392021-12-21 22:23:06 +0000423void PowerSupply::analyzeMFRFault()
424{
425 if (statusWord & phosphor::pmbus::status_word::MFR_SPECIFIC_FAULT)
426 {
Brandon Wymanc2906f42021-12-21 20:14:56 +0000427 if (mfrFault < DEGLITCH_LIMIT)
Brandon Wyman6c2ac392021-12-21 22:23:06 +0000428 {
429 log<level::ERR>(fmt::format("MFR fault: "
430 "STATUS_WORD = {:#04x} "
431 "STATUS_MFR_SPECIFIC = {:#02x}",
432 statusWord, statusMFR)
433 .c_str());
Brandon Wymanc2906f42021-12-21 20:14:56 +0000434 mfrFault++;
Brandon Wyman6c2ac392021-12-21 22:23:06 +0000435 }
436
Brandon Wyman6c2ac392021-12-21 22:23:06 +0000437 determineMFRFault();
438 }
Brandon Wymanc2906f42021-12-21 20:14:56 +0000439 else
440 {
441 mfrFault = 0;
442 }
Brandon Wyman6c2ac392021-12-21 22:23:06 +0000443}
444
Brandon Wymanf087f472021-12-22 00:04:27 +0000445void PowerSupply::analyzeVinUVFault()
446{
447 if (statusWord & phosphor::pmbus::status_word::VIN_UV_FAULT)
448 {
Brandon Wymanc2906f42021-12-21 20:14:56 +0000449 if (vinUVFault < DEGLITCH_LIMIT)
Brandon Wymanf087f472021-12-22 00:04:27 +0000450 {
451 log<level::ERR>(fmt::format("VIN_UV fault: STATUS_WORD = {:#04x}, "
452 "STATUS_MFR_SPECIFIC = {:#02x}, "
453 "STATUS_INPUT = {:#02x}",
454 statusWord, statusMFR, statusInput)
455 .c_str());
Brandon Wymanc2906f42021-12-21 20:14:56 +0000456 vinUVFault++;
Brandon Wymanf087f472021-12-22 00:04:27 +0000457 }
Brandon Wymanf087f472021-12-22 00:04:27 +0000458 }
Brandon Wyman82affd92021-11-24 19:12:49 +0000459
460 if (vinUVFault &&
461 !(statusWord & phosphor::pmbus::status_word::VIN_UV_FAULT))
462 {
463 log<level::INFO>(
464 fmt::format("VIN_UV fault cleared: STATUS_WORD = {:#04x}, "
465 "STATUS_MFR_SPECIFIC = {:#02x}, "
466 "STATUS_INPUT = {:#02x}",
467 statusWord, statusMFR, statusInput)
468 .c_str());
Brandon Wymanc2906f42021-12-21 20:14:56 +0000469 vinUVFault = 0;
Brandon Wyman82affd92021-11-24 19:12:49 +0000470 }
Brandon Wymanf087f472021-12-22 00:04:27 +0000471}
472
Brandon Wyman3f1242f2020-01-28 13:11:25 -0600473void PowerSupply::analyze()
474{
475 using namespace phosphor::pmbus;
476
B. J. Wyman681b2a32021-04-20 22:31:22 +0000477 if (presenceGPIO)
478 {
479 updatePresenceGPIO();
480 }
481
Brandon Wyman32453e92021-12-15 19:00:14 +0000482 if (present)
Brandon Wyman3f1242f2020-01-28 13:11:25 -0600483 {
484 try
485 {
Brandon Wyman32453e92021-12-15 19:00:14 +0000486 statusWord = pmbusIntf->read(STATUS_WORD, Type::Debug,
487 (readFail < LOG_LIMIT));
Brandon Wymanf65c4062020-08-19 13:15:53 -0500488 // Read worked, reset the fail count.
489 readFail = 0;
Brandon Wyman3f1242f2020-01-28 13:11:25 -0600490
491 if (statusWord)
492 {
Brandon Wymanf07bc792021-10-12 19:00:35 +0000493 statusInput = pmbusIntf->read(STATUS_INPUT, Type::Debug);
Jay Meyer10d94052020-11-30 14:41:21 -0600494 statusMFR = pmbusIntf->read(STATUS_MFR, Type::Debug);
Brandon Wyman85c7bf42021-10-19 22:28:48 +0000495 statusCML = pmbusIntf->read(STATUS_CML, Type::Debug);
Brandon Wyman6710ba22021-10-27 17:39:31 +0000496 auto status0Vout = pmbusIntf->insertPageNum(STATUS_VOUT, 0);
497 statusVout = pmbusIntf->read(status0Vout, Type::Debug);
Brandon Wymanb10b3be2021-11-09 22:12:15 +0000498 statusIout = pmbusIntf->read(STATUS_IOUT, Type::Debug);
Brandon Wyman7ee4d7e2021-11-19 20:48:23 +0000499 statusFans12 = pmbusIntf->read(STATUS_FANS_1_2, Type::Debug);
Brandon Wyman96893a42021-11-05 19:56:57 +0000500 statusTemperature =
501 pmbusIntf->read(STATUS_TEMPERATURE, Type::Debug);
Brandon Wyman9ddc6222021-10-28 17:28:01 +0000502
Brandon Wymanc2203432021-12-21 23:09:48 +0000503 analyzeCMLFault();
Brandon Wyman85c7bf42021-10-19 22:28:48 +0000504
Brandon Wymane3b0bb02021-12-21 23:16:48 +0000505 analyzeInputFault();
Brandon Wyman3f1242f2020-01-28 13:11:25 -0600506
Brandon Wymanc2c87132021-12-21 23:22:18 +0000507 analyzeVoutOVFault();
Brandon Wyman6710ba22021-10-27 17:39:31 +0000508
Brandon Wymana00e7302021-12-21 23:28:29 +0000509 analyzeIoutOCFault();
Brandon Wymanb10b3be2021-11-09 22:12:15 +0000510
Brandon Wyman08378782021-12-21 23:48:15 +0000511 analyzeVoutUVFault();
Brandon Wyman2cf46942021-10-28 19:09:16 +0000512
Brandon Wymand5d9a222021-12-21 23:59:05 +0000513 analyzeFanFault();
Brandon Wyman7ee4d7e2021-11-19 20:48:23 +0000514
Brandon Wyman52cb3f22021-12-21 23:02:47 +0000515 analyzeTemperatureFault();
Brandon Wyman96893a42021-11-05 19:56:57 +0000516
Brandon Wyman993b5542021-12-21 22:55:16 +0000517 analyzePgoodFault();
Brandon Wyman2916ea52021-11-06 03:31:18 +0000518
Brandon Wyman6c2ac392021-12-21 22:23:06 +0000519 analyzeMFRFault();
Brandon Wyman3f1242f2020-01-28 13:11:25 -0600520
Brandon Wymanf087f472021-12-22 00:04:27 +0000521 analyzeVinUVFault();
Brandon Wyman3f1242f2020-01-28 13:11:25 -0600522 }
523 else
524 {
Brandon Wymane3f7ad22021-12-21 20:27:45 +0000525 // if INPUT/VIN_UV fault was on, it cleared, trace it.
526 if (inputFault)
527 {
528 log<level::INFO>(
529 fmt::format(
530 "INPUT fault cleared: STATUS_WORD = {:#04x}",
531 statusWord)
532 .c_str());
533 }
534
535 if (vinUVFault)
536 {
537 log<level::INFO>(
538 fmt::format("VIN_UV cleared: STATUS_WORD = {:#04x}",
539 statusWord)
540 .c_str());
541 }
542
Brandon Wyman06ca4592021-12-06 22:52:23 +0000543 if (pgoodFault > 0)
Brandon Wyman4aecc292021-11-10 22:40:41 +0000544 {
545 log<level::INFO>(fmt::format("pgoodFault cleared path: {}",
546 inventoryPath)
547 .c_str());
Brandon Wyman4aecc292021-11-10 22:40:41 +0000548 }
Brandon Wymane3f7ad22021-12-21 20:27:45 +0000549
550 clearFaultFlags();
Brandon Wyman3f1242f2020-01-28 13:11:25 -0600551 }
Brandon Wyman82affd92021-11-24 19:12:49 +0000552
553 // Save off old inputVoltage value.
554 // Get latest inputVoltage.
555 // If voltage went from below minimum, and now is not, clear faults.
556 // Note: getInputVoltage() has its own try/catch.
557 int inputVoltageOld = inputVoltage;
558 double actualInputVoltage;
559 getInputVoltage(actualInputVoltage, inputVoltage);
560 if ((inputVoltageOld == in_input::VIN_VOLTAGE_0) &&
561 (inputVoltage != in_input::VIN_VOLTAGE_0))
562 {
563 log<level::INFO>(
564 fmt::format(
565 "READ_VIN back in range: inputVoltageOld = {} inputVoltage = {}",
566 inputVoltageOld, inputVoltage)
567 .c_str());
568 clearFaults();
569 }
Matt Spinler0975eaf2022-02-14 15:38:30 -0600570
571 checkAvailability();
Brandon Wyman3f1242f2020-01-28 13:11:25 -0600572 }
Patrick Williamsc1d4de52021-10-06 12:45:57 -0500573 catch (const ReadFailure& e)
Brandon Wyman3f1242f2020-01-28 13:11:25 -0600574 {
Brandon Wyman32453e92021-12-15 19:00:14 +0000575 if (readFail < SIZE_MAX)
576 {
577 readFail++;
578 }
579 if (readFail == LOG_LIMIT)
580 {
581 phosphor::logging::commit<ReadFailure>();
582 }
Brandon Wyman3f1242f2020-01-28 13:11:25 -0600583 }
584 }
585}
586
Brandon Wyman59a35792020-06-04 12:37:40 -0500587void PowerSupply::onOffConfig(uint8_t data)
588{
589 using namespace phosphor::pmbus;
590
591 if (present)
592 {
593 log<level::INFO>("ON_OFF_CONFIG write", entry("DATA=0x%02X", data));
594 try
595 {
596 std::vector<uint8_t> configData{data};
597 pmbusIntf->writeBinary(ON_OFF_CONFIG, configData,
598 Type::HwmonDeviceDebug);
599 }
600 catch (...)
601 {
602 // The underlying code in writeBinary will log a message to the
B. J. Wyman681b2a32021-04-20 22:31:22 +0000603 // journal if the write fails. If the ON_OFF_CONFIG is not setup
604 // as desired, later fault detection and analysis code should
605 // catch any of the fall out. We should not need to terminate
606 // the application if this write fails.
Brandon Wyman59a35792020-06-04 12:37:40 -0500607 }
608 }
609}
610
Brandon Wyman3c208462020-05-13 16:25:58 -0500611void PowerSupply::clearFaults()
612{
Brandon Wyman82affd92021-11-24 19:12:49 +0000613 log<level::DEBUG>(
614 fmt::format("clearFaults() inventoryPath: {}", inventoryPath).c_str());
Brandon Wyman5474c912021-02-23 14:39:43 -0600615 faultLogged = false;
Brandon Wyman3c208462020-05-13 16:25:58 -0500616 // The PMBus device driver does not allow for writing CLEAR_FAULTS
617 // directly. However, the pmbus hwmon device driver code will send a
618 // CLEAR_FAULTS after reading from any of the hwmon "files" in sysfs, so
619 // reading in1_input should result in clearing the fault bits in
620 // STATUS_BYTE/STATUS_WORD.
621 // I do not care what the return value is.
Brandon Wyman11151532020-11-10 13:45:57 -0600622 if (present)
Brandon Wyman3c208462020-05-13 16:25:58 -0500623 {
Brandon Wymane3f7ad22021-12-21 20:27:45 +0000624 clearFaultFlags();
Matt Spinler0975eaf2022-02-14 15:38:30 -0600625 checkAvailability();
Brandon Wyman9564e942020-11-10 14:01:42 -0600626 readFail = 0;
Brandon Wyman9564e942020-11-10 14:01:42 -0600627
Brandon Wyman11151532020-11-10 13:45:57 -0600628 try
629 {
630 static_cast<void>(
631 pmbusIntf->read("in1_input", phosphor::pmbus::Type::Hwmon));
632 }
Patrick Williamsc1d4de52021-10-06 12:45:57 -0500633 catch (const ReadFailure& e)
Brandon Wyman11151532020-11-10 13:45:57 -0600634 {
635 // Since I do not care what the return value is, I really do not
B. J. Wyman681b2a32021-04-20 22:31:22 +0000636 // care much if it gets a ReadFailure either. However, this
637 // should not prevent the application from continuing to run, so
638 // catching the read failure.
Brandon Wyman11151532020-11-10 13:45:57 -0600639 }
Brandon Wyman3c208462020-05-13 16:25:58 -0500640 }
641}
642
Brandon Wymanaed1f752019-11-25 18:10:52 -0600643void PowerSupply::inventoryChanged(sdbusplus::message::message& msg)
644{
645 std::string msgSensor;
Patrick Williamsabe49412020-05-13 17:59:47 -0500646 std::map<std::string, std::variant<uint32_t, bool>> msgData;
Brandon Wymanaed1f752019-11-25 18:10:52 -0600647 msg.read(msgSensor, msgData);
648
649 // Check if it was the Present property that changed.
650 auto valPropMap = msgData.find(PRESENT_PROP);
651 if (valPropMap != msgData.end())
652 {
653 if (std::get<bool>(valPropMap->second))
654 {
655 present = true;
B. J. Wyman681b2a32021-04-20 22:31:22 +0000656 // TODO: Immediately trying to read or write the "files" causes
657 // read or write failures.
Brandon Wyman1d7a7df2020-03-26 10:14:05 -0500658 using namespace std::chrono_literals;
659 std::this_thread::sleep_for(20ms);
Brandon Wyman9564e942020-11-10 14:01:42 -0600660 pmbusIntf->findHwmonDir();
Brandon Wyman59a35792020-06-04 12:37:40 -0500661 onOffConfig(phosphor::pmbus::ON_OFF_CONFIG_CONTROL_PIN_ONLY);
Brandon Wymanaed1f752019-11-25 18:10:52 -0600662 clearFaults();
Brandon Wyman1d7a7df2020-03-26 10:14:05 -0500663 updateInventory();
Brandon Wymanaed1f752019-11-25 18:10:52 -0600664 }
665 else
666 {
667 present = false;
668
669 // Clear out the now outdated inventory properties
670 updateInventory();
671 }
Matt Spinler0975eaf2022-02-14 15:38:30 -0600672 checkAvailability();
Brandon Wymanaed1f752019-11-25 18:10:52 -0600673 }
674}
675
Brandon Wyman9a507db2021-02-25 16:15:22 -0600676void PowerSupply::inventoryAdded(sdbusplus::message::message& msg)
677{
678 sdbusplus::message::object_path path;
679 msg.read(path);
680 // Make sure the signal is for the PSU inventory path
681 if (path == inventoryPath)
682 {
683 std::map<std::string, std::map<std::string, std::variant<bool>>>
684 interfaces;
685 // Get map of interfaces and their properties
686 msg.read(interfaces);
687
688 auto properties = interfaces.find(INVENTORY_IFACE);
689 if (properties != interfaces.end())
690 {
691 auto property = properties->second.find(PRESENT_PROP);
692 if (property != properties->second.end())
693 {
694 present = std::get<bool>(property->second);
695
696 log<level::INFO>(fmt::format("Power Supply {} Present {}",
697 inventoryPath, present)
698 .c_str());
699
700 updateInventory();
Matt Spinler0975eaf2022-02-14 15:38:30 -0600701 checkAvailability();
Brandon Wyman9a507db2021-02-25 16:15:22 -0600702 }
703 }
704 }
705}
706
Brandon Wyman1d7a7df2020-03-26 10:14:05 -0500707void PowerSupply::updateInventory()
708{
709 using namespace phosphor::pmbus;
710
Chanh Nguyenc12c53b2021-04-06 17:24:47 +0700711#if IBM_VPD
Brandon Wyman1d7a7df2020-03-26 10:14:05 -0500712 std::string ccin;
713 std::string pn;
714 std::string fn;
715 std::string header;
716 std::string sn;
Brandon Wyman1d7a7df2020-03-26 10:14:05 -0500717 using PropertyMap =
George Liu070c1bc2020-10-12 11:28:01 +0800718 std::map<std::string,
719 std::variant<std::string, std::vector<uint8_t>, bool>>;
Brandon Wyman1d7a7df2020-03-26 10:14:05 -0500720 PropertyMap assetProps;
George Liu070c1bc2020-10-12 11:28:01 +0800721 PropertyMap operProps;
Brandon Wyman1d7a7df2020-03-26 10:14:05 -0500722 PropertyMap versionProps;
723 PropertyMap ipzvpdDINFProps;
724 PropertyMap ipzvpdVINIProps;
725 using InterfaceMap = std::map<std::string, PropertyMap>;
726 InterfaceMap interfaces;
727 using ObjectMap = std::map<sdbusplus::message::object_path, InterfaceMap>;
728 ObjectMap object;
729#endif
B. J. Wyman681b2a32021-04-20 22:31:22 +0000730 log<level::DEBUG>(
731 fmt::format("updateInventory() inventoryPath: {}", inventoryPath)
732 .c_str());
Brandon Wyman1d7a7df2020-03-26 10:14:05 -0500733
734 if (present)
735 {
736 // TODO: non-IBM inventory updates?
737
Chanh Nguyenc12c53b2021-04-06 17:24:47 +0700738#if IBM_VPD
Brandon Wyman1d7a7df2020-03-26 10:14:05 -0500739 try
740 {
741 ccin = pmbusIntf->readString(CCIN, Type::HwmonDeviceDebug);
742 assetProps.emplace(MODEL_PROP, ccin);
Adriana Kobylak572a9052021-03-30 15:58:07 +0000743 modelName = ccin;
Brandon Wyman1d7a7df2020-03-26 10:14:05 -0500744 }
Patrick Williamsc1d4de52021-10-06 12:45:57 -0500745 catch (const ReadFailure& e)
Brandon Wyman1d7a7df2020-03-26 10:14:05 -0500746 {
B. J. Wyman681b2a32021-04-20 22:31:22 +0000747 // Ignore the read failure, let pmbus code indicate failure,
748 // path...
Brandon Wyman1d7a7df2020-03-26 10:14:05 -0500749 // TODO - ibm918
750 // https://github.com/openbmc/docs/blob/master/designs/vpd-collection.md
751 // The BMC must log errors if any of the VPD cannot be properly
752 // parsed or fails ECC checks.
753 }
754
755 try
756 {
757 pn = pmbusIntf->readString(PART_NUMBER, Type::HwmonDeviceDebug);
758 assetProps.emplace(PN_PROP, pn);
759 }
Patrick Williamsc1d4de52021-10-06 12:45:57 -0500760 catch (const ReadFailure& e)
Brandon Wyman1d7a7df2020-03-26 10:14:05 -0500761 {
B. J. Wyman681b2a32021-04-20 22:31:22 +0000762 // Ignore the read failure, let pmbus code indicate failure,
763 // path...
Brandon Wyman1d7a7df2020-03-26 10:14:05 -0500764 }
765
766 try
767 {
768 fn = pmbusIntf->readString(FRU_NUMBER, Type::HwmonDeviceDebug);
Brandon Wymana169b0f2021-12-07 20:18:06 +0000769 assetProps.emplace(SPARE_PN_PROP, fn);
Brandon Wyman1d7a7df2020-03-26 10:14:05 -0500770 }
Patrick Williamsc1d4de52021-10-06 12:45:57 -0500771 catch (const ReadFailure& e)
Brandon Wyman1d7a7df2020-03-26 10:14:05 -0500772 {
B. J. Wyman681b2a32021-04-20 22:31:22 +0000773 // Ignore the read failure, let pmbus code indicate failure,
774 // path...
Brandon Wyman1d7a7df2020-03-26 10:14:05 -0500775 }
776
777 try
778 {
779 header =
780 pmbusIntf->readString(SERIAL_HEADER, Type::HwmonDeviceDebug);
781 sn = pmbusIntf->readString(SERIAL_NUMBER, Type::HwmonDeviceDebug);
782 assetProps.emplace(SN_PROP, sn);
783 }
Patrick Williamsc1d4de52021-10-06 12:45:57 -0500784 catch (const ReadFailure& e)
Brandon Wyman1d7a7df2020-03-26 10:14:05 -0500785 {
B. J. Wyman681b2a32021-04-20 22:31:22 +0000786 // Ignore the read failure, let pmbus code indicate failure,
787 // path...
Brandon Wyman1d7a7df2020-03-26 10:14:05 -0500788 }
789
790 try
791 {
Brandon Wymanc9efe412020-10-09 15:42:50 -0500792 fwVersion =
793 pmbusIntf->readString(FW_VERSION, Type::HwmonDeviceDebug);
794 versionProps.emplace(VERSION_PROP, fwVersion);
Brandon Wyman1d7a7df2020-03-26 10:14:05 -0500795 }
Patrick Williamsc1d4de52021-10-06 12:45:57 -0500796 catch (const ReadFailure& e)
Brandon Wyman1d7a7df2020-03-26 10:14:05 -0500797 {
B. J. Wyman681b2a32021-04-20 22:31:22 +0000798 // Ignore the read failure, let pmbus code indicate failure,
799 // path...
Brandon Wyman1d7a7df2020-03-26 10:14:05 -0500800 }
801
802 ipzvpdVINIProps.emplace("CC",
803 std::vector<uint8_t>(ccin.begin(), ccin.end()));
804 ipzvpdVINIProps.emplace("PN",
805 std::vector<uint8_t>(pn.begin(), pn.end()));
806 ipzvpdVINIProps.emplace("FN",
807 std::vector<uint8_t>(fn.begin(), fn.end()));
808 std::string header_sn = header + sn + '\0';
809 ipzvpdVINIProps.emplace(
810 "SN", std::vector<uint8_t>(header_sn.begin(), header_sn.end()));
811 std::string description = "IBM PS";
812 ipzvpdVINIProps.emplace(
813 "DR", std::vector<uint8_t>(description.begin(), description.end()));
814
Ben Tynerf8d8c462022-01-27 16:09:45 -0600815 // Populate the VINI Resource Type (RT) keyword
816 ipzvpdVINIProps.emplace("RT", std::vector<uint8_t>{'V', 'I', 'N', 'I'});
817
Brandon Wyman1d7a7df2020-03-26 10:14:05 -0500818 // Update the Resource Identifier (RI) keyword
819 // 2 byte FRC: 0x0003
820 // 2 byte RID: 0x1000, 0x1001...
821 std::uint8_t num = std::stoul(
822 inventoryPath.substr(inventoryPath.size() - 1, 1), nullptr, 0);
823 std::vector<uint8_t> ri{0x00, 0x03, 0x10, num};
824 ipzvpdDINFProps.emplace("RI", ri);
825
826 // Fill in the FRU Label (FL) keyword.
827 std::string fl = "E";
828 fl.push_back(inventoryPath.back());
829 fl.resize(FL_KW_SIZE, ' ');
830 ipzvpdDINFProps.emplace("FL",
831 std::vector<uint8_t>(fl.begin(), fl.end()));
832
Ben Tynerf8d8c462022-01-27 16:09:45 -0600833 // Populate the DINF Resource Type (RT) keyword
834 ipzvpdDINFProps.emplace("RT", std::vector<uint8_t>{'D', 'I', 'N', 'F'});
835
Brandon Wyman1d7a7df2020-03-26 10:14:05 -0500836 interfaces.emplace(ASSET_IFACE, std::move(assetProps));
837 interfaces.emplace(VERSION_IFACE, std::move(versionProps));
838 interfaces.emplace(DINF_IFACE, std::move(ipzvpdDINFProps));
839 interfaces.emplace(VINI_IFACE, std::move(ipzvpdVINIProps));
840
George Liu070c1bc2020-10-12 11:28:01 +0800841 // Update the Functional
842 operProps.emplace(FUNCTIONAL_PROP, present);
843 interfaces.emplace(OPERATIONAL_STATE_IFACE, std::move(operProps));
844
Brandon Wyman1d7a7df2020-03-26 10:14:05 -0500845 auto path = inventoryPath.substr(strlen(INVENTORY_OBJ_PATH));
846 object.emplace(path, std::move(interfaces));
847
848 try
849 {
850 auto service =
851 util::getService(INVENTORY_OBJ_PATH, INVENTORY_MGR_IFACE, bus);
852
853 if (service.empty())
854 {
855 log<level::ERR>("Unable to get inventory manager service");
856 return;
857 }
858
859 auto method =
860 bus.new_method_call(service.c_str(), INVENTORY_OBJ_PATH,
861 INVENTORY_MGR_IFACE, "Notify");
862
863 method.append(std::move(object));
864
865 auto reply = bus.call(method);
866 }
Patrick Williamsc1d4de52021-10-06 12:45:57 -0500867 catch (const std::exception& e)
Brandon Wyman1d7a7df2020-03-26 10:14:05 -0500868 {
Jay Meyer6a3fd2c2020-08-25 16:37:16 -0500869 log<level::ERR>(
870 std::string(e.what() + std::string(" PATH=") + inventoryPath)
871 .c_str());
Brandon Wyman1d7a7df2020-03-26 10:14:05 -0500872 }
873#endif
874 }
875}
876
Adriana Kobylak4175ffb2021-08-02 14:51:05 +0000877void PowerSupply::getInputVoltage(double& actualInputVoltage,
878 int& inputVoltage) const
879{
880 using namespace phosphor::pmbus;
881
882 actualInputVoltage = in_input::VIN_VOLTAGE_0;
883 inputVoltage = in_input::VIN_VOLTAGE_0;
884
885 if (present)
886 {
887 try
888 {
889 // Read input voltage in millivolts
890 auto inputVoltageStr = pmbusIntf->readString(READ_VIN, Type::Hwmon);
891
892 // Convert to volts
893 actualInputVoltage = std::stod(inputVoltageStr) / 1000;
894
895 // Calculate the voltage based on voltage thresholds
896 if (actualInputVoltage < in_input::VIN_VOLTAGE_MIN)
897 {
898 inputVoltage = in_input::VIN_VOLTAGE_0;
899 }
900 else if (actualInputVoltage < in_input::VIN_VOLTAGE_110_THRESHOLD)
901 {
902 inputVoltage = in_input::VIN_VOLTAGE_110;
903 }
904 else
905 {
906 inputVoltage = in_input::VIN_VOLTAGE_220;
907 }
908 }
909 catch (const std::exception& e)
910 {
911 log<level::ERR>(
912 fmt::format("READ_VIN read error: {}", e.what()).c_str());
913 }
914 }
915}
916
Matt Spinler0975eaf2022-02-14 15:38:30 -0600917void PowerSupply::checkAvailability()
918{
919 bool origAvailability = available;
920 available = present && !hasInputFault() && !hasVINUVFault() &&
921 !hasPSKillFault() && !hasIoutOCFault();
922
923 if (origAvailability != available)
924 {
925 auto invpath = inventoryPath.substr(strlen(INVENTORY_OBJ_PATH));
926 phosphor::power::psu::setAvailable(bus, invpath, available);
Matt Spinlerca1e9ea2022-02-18 14:03:08 -0600927
928 // Check if the health rollup needs to change based on the
929 // new availability value.
930 phosphor::power::psu::handleChassisHealthRollup(bus, inventoryPath,
931 !available);
Matt Spinler0975eaf2022-02-14 15:38:30 -0600932 }
933}
934
Brandon Wyman3f1242f2020-01-28 13:11:25 -0600935} // namespace phosphor::power::psu