blob: 27bc5027cf397be3b27b4a2daed21410cc727d67 [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 Wymanc2203432021-12-21 23:09:48 +0000192void PowerSupply::analyzeCMLFault()
193{
194 if (statusWord & phosphor::pmbus::status_word::CML_FAULT)
195 {
Brandon Wymanc2906f42021-12-21 20:14:56 +0000196 if (cmlFault < DEGLITCH_LIMIT)
Brandon Wymanc2203432021-12-21 23:09:48 +0000197 {
198 log<level::ERR>(fmt::format("CML fault: STATUS_WORD = {:#04x}, "
199 "STATUS_CML = {:#02x}",
200 statusWord, statusCML)
201 .c_str());
Brandon Wymanc2203432021-12-21 23:09:48 +0000202
Brandon Wymanc2906f42021-12-21 20:14:56 +0000203 cmlFault++;
204 }
205 }
206 else
207 {
208 cmlFault = 0;
Brandon Wymanc2203432021-12-21 23:09:48 +0000209 }
210}
211
Brandon Wymane3b0bb02021-12-21 23:16:48 +0000212void PowerSupply::analyzeInputFault()
213{
214 if (statusWord & phosphor::pmbus::status_word::INPUT_FAULT_WARN)
215 {
Brandon Wymanc2906f42021-12-21 20:14:56 +0000216 if (inputFault < DEGLITCH_LIMIT)
Brandon Wymane3b0bb02021-12-21 23:16:48 +0000217 {
218 log<level::ERR>(fmt::format("INPUT fault: STATUS_WORD = {:#04x}, "
219 "STATUS_MFR_SPECIFIC = {:#02x}, "
220 "STATUS_INPUT = {:#02x}",
221 statusWord, statusMFR, statusInput)
222 .c_str());
Brandon Wymane3b0bb02021-12-21 23:16:48 +0000223
Brandon Wymanc2906f42021-12-21 20:14:56 +0000224 inputFault++;
225 }
Brandon Wymane3b0bb02021-12-21 23:16:48 +0000226 }
Brandon Wyman82affd92021-11-24 19:12:49 +0000227
228 // If had INPUT/VIN_UV fault, and now off.
229 // Trace that odd behavior.
230 if (inputFault &&
231 !(statusWord & phosphor::pmbus::status_word::INPUT_FAULT_WARN))
232 {
233 log<level::INFO>(
234 fmt::format("INPUT fault cleared: STATUS_WORD = {:#04x}, "
235 "STATUS_MFR_SPECIFIC = {:#02x}, "
236 "STATUS_INPUT = {:#02x}",
237 statusWord, statusMFR, statusInput)
238 .c_str());
Brandon Wymanc2906f42021-12-21 20:14:56 +0000239 inputFault = 0;
Brandon Wyman82affd92021-11-24 19:12:49 +0000240 }
Brandon Wymane3b0bb02021-12-21 23:16:48 +0000241}
242
Brandon Wymanc2c87132021-12-21 23:22:18 +0000243void PowerSupply::analyzeVoutOVFault()
244{
245 if (statusWord & phosphor::pmbus::status_word::VOUT_OV_FAULT)
246 {
Brandon Wymanc2906f42021-12-21 20:14:56 +0000247 if (voutOVFault < DEGLITCH_LIMIT)
Brandon Wymanc2c87132021-12-21 23:22:18 +0000248 {
249 log<level::ERR>(
250 fmt::format("VOUT_OV_FAULT fault: STATUS_WORD = {:#04x}, "
251 "STATUS_MFR_SPECIFIC = {:#02x}, "
252 "STATUS_VOUT = {:#02x}",
253 statusWord, statusMFR, statusVout)
254 .c_str());
Brandon Wymanc2c87132021-12-21 23:22:18 +0000255
Brandon Wymanc2906f42021-12-21 20:14:56 +0000256 voutOVFault++;
257 }
258 }
259 else
260 {
261 voutOVFault = 0;
Brandon Wymanc2c87132021-12-21 23:22:18 +0000262 }
263}
264
Brandon Wymana00e7302021-12-21 23:28:29 +0000265void PowerSupply::analyzeIoutOCFault()
266{
267 if (statusWord & phosphor::pmbus::status_word::IOUT_OC_FAULT)
268 {
Brandon Wymanc2906f42021-12-21 20:14:56 +0000269 if (ioutOCFault < DEGLITCH_LIMIT)
Brandon Wymana00e7302021-12-21 23:28:29 +0000270 {
271 log<level::ERR>(fmt::format("IOUT fault: STATUS_WORD = {:#04x}, "
272 "STATUS_MFR_SPECIFIC = {:#02x}, "
273 "STATUS_IOUT = {:#02x}",
274 statusWord, statusMFR, statusIout)
275 .c_str());
Brandon Wymana00e7302021-12-21 23:28:29 +0000276
Brandon Wymanc2906f42021-12-21 20:14:56 +0000277 ioutOCFault++;
278 }
279 }
280 else
281 {
282 ioutOCFault = 0;
Brandon Wymana00e7302021-12-21 23:28:29 +0000283 }
284}
285
Brandon Wyman08378782021-12-21 23:48:15 +0000286void PowerSupply::analyzeVoutUVFault()
287{
288 if ((statusWord & phosphor::pmbus::status_word::VOUT_FAULT) &&
289 !(statusWord & phosphor::pmbus::status_word::VOUT_OV_FAULT))
290 {
Brandon Wymanc2906f42021-12-21 20:14:56 +0000291 if (voutUVFault < DEGLITCH_LIMIT)
Brandon Wyman08378782021-12-21 23:48:15 +0000292 {
293 log<level::ERR>(
294 fmt::format("VOUT_UV_FAULT fault: STATUS_WORD = {:#04x}, "
295 "STATUS_MFR_SPECIFIC = {:#02x}, "
296 "STATUS_VOUT = {:#02x}",
297 statusWord, statusMFR, statusVout)
298 .c_str());
Brandon Wyman08378782021-12-21 23:48:15 +0000299
Brandon Wymanc2906f42021-12-21 20:14:56 +0000300 voutUVFault++;
301 }
302 }
303 else
304 {
305 voutUVFault = 0;
Brandon Wyman08378782021-12-21 23:48:15 +0000306 }
307}
308
Brandon Wymand5d9a222021-12-21 23:59:05 +0000309void PowerSupply::analyzeFanFault()
310{
311 if (statusWord & phosphor::pmbus::status_word::FAN_FAULT)
312 {
Brandon Wymanc2906f42021-12-21 20:14:56 +0000313 if (fanFault < DEGLITCH_LIMIT)
Brandon Wymand5d9a222021-12-21 23:59:05 +0000314 {
315 log<level::ERR>(fmt::format("FANS fault/warning: "
316 "STATUS_WORD = {:#04x}, "
317 "STATUS_MFR_SPECIFIC = {:#02x}, "
318 "STATUS_FANS_1_2 = {:#02x}",
319 statusWord, statusMFR, statusFans12)
320 .c_str());
Brandon Wymand5d9a222021-12-21 23:59:05 +0000321
Brandon Wymanc2906f42021-12-21 20:14:56 +0000322 fanFault++;
323 }
324 }
325 else
326 {
327 fanFault = 0;
Brandon Wymand5d9a222021-12-21 23:59:05 +0000328 }
329}
330
Brandon Wyman52cb3f22021-12-21 23:02:47 +0000331void PowerSupply::analyzeTemperatureFault()
332{
333 if (statusWord & phosphor::pmbus::status_word::TEMPERATURE_FAULT_WARN)
334 {
Brandon Wymanc2906f42021-12-21 20:14:56 +0000335 if (tempFault < DEGLITCH_LIMIT)
Brandon Wyman52cb3f22021-12-21 23:02:47 +0000336 {
337 log<level::ERR>(fmt::format("TEMPERATURE fault/warning: "
338 "STATUS_WORD = {:#04x}, "
339 "STATUS_MFR_SPECIFIC = {:#02x}, "
340 "STATUS_TEMPERATURE = {:#02x}",
341 statusWord, statusMFR,
342 statusTemperature)
343 .c_str());
Brandon Wyman52cb3f22021-12-21 23:02:47 +0000344
Brandon Wymanc2906f42021-12-21 20:14:56 +0000345 tempFault++;
346 }
347 }
348 else
349 {
350 tempFault = 0;
Brandon Wyman52cb3f22021-12-21 23:02:47 +0000351 }
352}
353
Brandon Wyman993b5542021-12-21 22:55:16 +0000354void PowerSupply::analyzePgoodFault()
355{
356 if ((statusWord & phosphor::pmbus::status_word::POWER_GOOD_NEGATED) ||
357 (statusWord & phosphor::pmbus::status_word::UNIT_IS_OFF))
358 {
359 if (pgoodFault < DEGLITCH_LIMIT)
360 {
361 log<level::ERR>(fmt::format("PGOOD fault: "
362 "STATUS_WORD = {:#04x}, "
363 "STATUS_MFR_SPECIFIC = {:#02x}",
364 statusWord, statusMFR)
365 .c_str());
366
367 pgoodFault++;
368 }
369 }
370 else
371 {
372 pgoodFault = 0;
373 }
374}
375
Brandon Wyman39ea02b2021-11-23 23:22:23 +0000376void PowerSupply::determineMFRFault()
377{
378 if (bindPath.string().find("ibm-cffps") != std::string::npos)
379 {
380 // IBM MFR_SPECIFIC[4] is PS_Kill fault
381 if (statusMFR & 0x10)
382 {
Brandon Wymanc2906f42021-12-21 20:14:56 +0000383 if (psKillFault < DEGLITCH_LIMIT)
384 {
385 psKillFault++;
386 }
387 }
388 else
389 {
390 psKillFault = 0;
Brandon Wyman39ea02b2021-11-23 23:22:23 +0000391 }
392 // IBM MFR_SPECIFIC[6] is 12Vcs fault.
393 if (statusMFR & 0x40)
394 {
Brandon Wymanc2906f42021-12-21 20:14:56 +0000395 if (ps12VcsFault < DEGLITCH_LIMIT)
396 {
397 ps12VcsFault++;
398 }
399 }
400 else
401 {
402 ps12VcsFault = 0;
Brandon Wyman39ea02b2021-11-23 23:22:23 +0000403 }
404 // IBM MFR_SPECIFIC[7] is 12V Current-Share fault.
405 if (statusMFR & 0x80)
406 {
Brandon Wymanc2906f42021-12-21 20:14:56 +0000407 if (psCS12VFault < DEGLITCH_LIMIT)
408 {
409 psCS12VFault++;
410 }
411 }
412 else
413 {
414 psCS12VFault = 0;
Brandon Wyman39ea02b2021-11-23 23:22:23 +0000415 }
416 }
417}
418
Brandon Wyman6c2ac392021-12-21 22:23:06 +0000419void PowerSupply::analyzeMFRFault()
420{
421 if (statusWord & phosphor::pmbus::status_word::MFR_SPECIFIC_FAULT)
422 {
Brandon Wymanc2906f42021-12-21 20:14:56 +0000423 if (mfrFault < DEGLITCH_LIMIT)
Brandon Wyman6c2ac392021-12-21 22:23:06 +0000424 {
425 log<level::ERR>(fmt::format("MFR fault: "
426 "STATUS_WORD = {:#04x} "
427 "STATUS_MFR_SPECIFIC = {:#02x}",
428 statusWord, statusMFR)
429 .c_str());
Brandon Wymanc2906f42021-12-21 20:14:56 +0000430 mfrFault++;
Brandon Wyman6c2ac392021-12-21 22:23:06 +0000431 }
432
Brandon Wyman6c2ac392021-12-21 22:23:06 +0000433 determineMFRFault();
434 }
Brandon Wymanc2906f42021-12-21 20:14:56 +0000435 else
436 {
437 mfrFault = 0;
438 }
Brandon Wyman6c2ac392021-12-21 22:23:06 +0000439}
440
Brandon Wymanf087f472021-12-22 00:04:27 +0000441void PowerSupply::analyzeVinUVFault()
442{
443 if (statusWord & phosphor::pmbus::status_word::VIN_UV_FAULT)
444 {
Brandon Wymanc2906f42021-12-21 20:14:56 +0000445 if (vinUVFault < DEGLITCH_LIMIT)
Brandon Wymanf087f472021-12-22 00:04:27 +0000446 {
447 log<level::ERR>(fmt::format("VIN_UV fault: STATUS_WORD = {:#04x}, "
448 "STATUS_MFR_SPECIFIC = {:#02x}, "
449 "STATUS_INPUT = {:#02x}",
450 statusWord, statusMFR, statusInput)
451 .c_str());
Brandon Wymanc2906f42021-12-21 20:14:56 +0000452 vinUVFault++;
Brandon Wymanf087f472021-12-22 00:04:27 +0000453 }
Brandon Wymanf087f472021-12-22 00:04:27 +0000454 }
Brandon Wyman82affd92021-11-24 19:12:49 +0000455
456 if (vinUVFault &&
457 !(statusWord & phosphor::pmbus::status_word::VIN_UV_FAULT))
458 {
459 log<level::INFO>(
460 fmt::format("VIN_UV fault cleared: STATUS_WORD = {:#04x}, "
461 "STATUS_MFR_SPECIFIC = {:#02x}, "
462 "STATUS_INPUT = {:#02x}",
463 statusWord, statusMFR, statusInput)
464 .c_str());
Brandon Wymanc2906f42021-12-21 20:14:56 +0000465 vinUVFault = 0;
Brandon Wyman82affd92021-11-24 19:12:49 +0000466 }
Brandon Wymanf087f472021-12-22 00:04:27 +0000467}
468
Brandon Wyman3f1242f2020-01-28 13:11:25 -0600469void PowerSupply::analyze()
470{
471 using namespace phosphor::pmbus;
472
B. J. Wyman681b2a32021-04-20 22:31:22 +0000473 if (presenceGPIO)
474 {
475 updatePresenceGPIO();
476 }
477
Brandon Wyman32453e92021-12-15 19:00:14 +0000478 if (present)
Brandon Wyman3f1242f2020-01-28 13:11:25 -0600479 {
480 try
481 {
Brandon Wyman32453e92021-12-15 19:00:14 +0000482 statusWord = pmbusIntf->read(STATUS_WORD, Type::Debug,
483 (readFail < LOG_LIMIT));
Brandon Wymanf65c4062020-08-19 13:15:53 -0500484 // Read worked, reset the fail count.
485 readFail = 0;
Brandon Wyman3f1242f2020-01-28 13:11:25 -0600486
487 if (statusWord)
488 {
Brandon Wymanf07bc792021-10-12 19:00:35 +0000489 statusInput = pmbusIntf->read(STATUS_INPUT, Type::Debug);
Jay Meyer10d94052020-11-30 14:41:21 -0600490 statusMFR = pmbusIntf->read(STATUS_MFR, Type::Debug);
Brandon Wyman85c7bf42021-10-19 22:28:48 +0000491 statusCML = pmbusIntf->read(STATUS_CML, Type::Debug);
Brandon Wyman6710ba22021-10-27 17:39:31 +0000492 auto status0Vout = pmbusIntf->insertPageNum(STATUS_VOUT, 0);
493 statusVout = pmbusIntf->read(status0Vout, Type::Debug);
Brandon Wymanb10b3be2021-11-09 22:12:15 +0000494 statusIout = pmbusIntf->read(STATUS_IOUT, Type::Debug);
Brandon Wyman7ee4d7e2021-11-19 20:48:23 +0000495 statusFans12 = pmbusIntf->read(STATUS_FANS_1_2, Type::Debug);
Brandon Wyman96893a42021-11-05 19:56:57 +0000496 statusTemperature =
497 pmbusIntf->read(STATUS_TEMPERATURE, Type::Debug);
Brandon Wyman9ddc6222021-10-28 17:28:01 +0000498
Brandon Wymanc2203432021-12-21 23:09:48 +0000499 analyzeCMLFault();
Brandon Wyman85c7bf42021-10-19 22:28:48 +0000500
Brandon Wymane3b0bb02021-12-21 23:16:48 +0000501 analyzeInputFault();
Brandon Wyman3f1242f2020-01-28 13:11:25 -0600502
Brandon Wymanc2c87132021-12-21 23:22:18 +0000503 analyzeVoutOVFault();
Brandon Wyman6710ba22021-10-27 17:39:31 +0000504
Brandon Wymana00e7302021-12-21 23:28:29 +0000505 analyzeIoutOCFault();
Brandon Wymanb10b3be2021-11-09 22:12:15 +0000506
Brandon Wyman08378782021-12-21 23:48:15 +0000507 analyzeVoutUVFault();
Brandon Wyman2cf46942021-10-28 19:09:16 +0000508
Brandon Wymand5d9a222021-12-21 23:59:05 +0000509 analyzeFanFault();
Brandon Wyman7ee4d7e2021-11-19 20:48:23 +0000510
Brandon Wyman52cb3f22021-12-21 23:02:47 +0000511 analyzeTemperatureFault();
Brandon Wyman96893a42021-11-05 19:56:57 +0000512
Brandon Wyman993b5542021-12-21 22:55:16 +0000513 analyzePgoodFault();
Brandon Wyman2916ea52021-11-06 03:31:18 +0000514
Brandon Wyman6c2ac392021-12-21 22:23:06 +0000515 analyzeMFRFault();
Brandon Wyman3f1242f2020-01-28 13:11:25 -0600516
Brandon Wymanf087f472021-12-22 00:04:27 +0000517 analyzeVinUVFault();
Brandon Wyman3f1242f2020-01-28 13:11:25 -0600518 }
519 else
520 {
Brandon Wymane3f7ad22021-12-21 20:27:45 +0000521 // if INPUT/VIN_UV fault was on, it cleared, trace it.
522 if (inputFault)
523 {
524 log<level::INFO>(
525 fmt::format(
526 "INPUT fault cleared: STATUS_WORD = {:#04x}",
527 statusWord)
528 .c_str());
529 }
530
531 if (vinUVFault)
532 {
533 log<level::INFO>(
534 fmt::format("VIN_UV cleared: STATUS_WORD = {:#04x}",
535 statusWord)
536 .c_str());
537 }
538
Brandon Wyman06ca4592021-12-06 22:52:23 +0000539 if (pgoodFault > 0)
Brandon Wyman4aecc292021-11-10 22:40:41 +0000540 {
541 log<level::INFO>(fmt::format("pgoodFault cleared path: {}",
542 inventoryPath)
543 .c_str());
Brandon Wyman4aecc292021-11-10 22:40:41 +0000544 }
Brandon Wymane3f7ad22021-12-21 20:27:45 +0000545
546 clearFaultFlags();
Brandon Wyman3f1242f2020-01-28 13:11:25 -0600547 }
Brandon Wyman82affd92021-11-24 19:12:49 +0000548
549 // Save off old inputVoltage value.
550 // Get latest inputVoltage.
551 // If voltage went from below minimum, and now is not, clear faults.
552 // Note: getInputVoltage() has its own try/catch.
553 int inputVoltageOld = inputVoltage;
554 double actualInputVoltage;
555 getInputVoltage(actualInputVoltage, inputVoltage);
556 if ((inputVoltageOld == in_input::VIN_VOLTAGE_0) &&
557 (inputVoltage != in_input::VIN_VOLTAGE_0))
558 {
559 log<level::INFO>(
560 fmt::format(
561 "READ_VIN back in range: inputVoltageOld = {} inputVoltage = {}",
562 inputVoltageOld, inputVoltage)
563 .c_str());
564 clearFaults();
565 }
Brandon Wyman3f1242f2020-01-28 13:11:25 -0600566 }
Patrick Williamsc1d4de52021-10-06 12:45:57 -0500567 catch (const ReadFailure& e)
Brandon Wyman3f1242f2020-01-28 13:11:25 -0600568 {
Brandon Wyman32453e92021-12-15 19:00:14 +0000569 if (readFail < SIZE_MAX)
570 {
571 readFail++;
572 }
573 if (readFail == LOG_LIMIT)
574 {
575 phosphor::logging::commit<ReadFailure>();
576 }
Brandon Wyman3f1242f2020-01-28 13:11:25 -0600577 }
578 }
579}
580
Brandon Wyman59a35792020-06-04 12:37:40 -0500581void PowerSupply::onOffConfig(uint8_t data)
582{
583 using namespace phosphor::pmbus;
584
585 if (present)
586 {
587 log<level::INFO>("ON_OFF_CONFIG write", entry("DATA=0x%02X", data));
588 try
589 {
590 std::vector<uint8_t> configData{data};
591 pmbusIntf->writeBinary(ON_OFF_CONFIG, configData,
592 Type::HwmonDeviceDebug);
593 }
594 catch (...)
595 {
596 // The underlying code in writeBinary will log a message to the
B. J. Wyman681b2a32021-04-20 22:31:22 +0000597 // journal if the write fails. If the ON_OFF_CONFIG is not setup
598 // as desired, later fault detection and analysis code should
599 // catch any of the fall out. We should not need to terminate
600 // the application if this write fails.
Brandon Wyman59a35792020-06-04 12:37:40 -0500601 }
602 }
603}
604
Brandon Wyman3c208462020-05-13 16:25:58 -0500605void PowerSupply::clearFaults()
606{
Brandon Wyman82affd92021-11-24 19:12:49 +0000607 log<level::DEBUG>(
608 fmt::format("clearFaults() inventoryPath: {}", inventoryPath).c_str());
Brandon Wyman5474c912021-02-23 14:39:43 -0600609 faultLogged = false;
Brandon Wyman3c208462020-05-13 16:25:58 -0500610 // The PMBus device driver does not allow for writing CLEAR_FAULTS
611 // directly. However, the pmbus hwmon device driver code will send a
612 // CLEAR_FAULTS after reading from any of the hwmon "files" in sysfs, so
613 // reading in1_input should result in clearing the fault bits in
614 // STATUS_BYTE/STATUS_WORD.
615 // I do not care what the return value is.
Brandon Wyman11151532020-11-10 13:45:57 -0600616 if (present)
Brandon Wyman3c208462020-05-13 16:25:58 -0500617 {
Brandon Wymane3f7ad22021-12-21 20:27:45 +0000618 clearFaultFlags();
Brandon Wyman9564e942020-11-10 14:01:42 -0600619 readFail = 0;
Brandon Wyman9564e942020-11-10 14:01:42 -0600620
Brandon Wyman11151532020-11-10 13:45:57 -0600621 try
622 {
623 static_cast<void>(
624 pmbusIntf->read("in1_input", phosphor::pmbus::Type::Hwmon));
625 }
Patrick Williamsc1d4de52021-10-06 12:45:57 -0500626 catch (const ReadFailure& e)
Brandon Wyman11151532020-11-10 13:45:57 -0600627 {
628 // Since I do not care what the return value is, I really do not
B. J. Wyman681b2a32021-04-20 22:31:22 +0000629 // care much if it gets a ReadFailure either. However, this
630 // should not prevent the application from continuing to run, so
631 // catching the read failure.
Brandon Wyman11151532020-11-10 13:45:57 -0600632 }
Brandon Wyman3c208462020-05-13 16:25:58 -0500633 }
634}
635
Brandon Wymanaed1f752019-11-25 18:10:52 -0600636void PowerSupply::inventoryChanged(sdbusplus::message::message& msg)
637{
638 std::string msgSensor;
Patrick Williamsabe49412020-05-13 17:59:47 -0500639 std::map<std::string, std::variant<uint32_t, bool>> msgData;
Brandon Wymanaed1f752019-11-25 18:10:52 -0600640 msg.read(msgSensor, msgData);
641
642 // Check if it was the Present property that changed.
643 auto valPropMap = msgData.find(PRESENT_PROP);
644 if (valPropMap != msgData.end())
645 {
646 if (std::get<bool>(valPropMap->second))
647 {
648 present = true;
B. J. Wyman681b2a32021-04-20 22:31:22 +0000649 // TODO: Immediately trying to read or write the "files" causes
650 // read or write failures.
Brandon Wyman1d7a7df2020-03-26 10:14:05 -0500651 using namespace std::chrono_literals;
652 std::this_thread::sleep_for(20ms);
Brandon Wyman9564e942020-11-10 14:01:42 -0600653 pmbusIntf->findHwmonDir();
Brandon Wyman59a35792020-06-04 12:37:40 -0500654 onOffConfig(phosphor::pmbus::ON_OFF_CONFIG_CONTROL_PIN_ONLY);
Brandon Wymanaed1f752019-11-25 18:10:52 -0600655 clearFaults();
Brandon Wyman1d7a7df2020-03-26 10:14:05 -0500656 updateInventory();
Brandon Wymanaed1f752019-11-25 18:10:52 -0600657 }
658 else
659 {
660 present = false;
661
662 // Clear out the now outdated inventory properties
663 updateInventory();
664 }
665 }
666}
667
Brandon Wyman9a507db2021-02-25 16:15:22 -0600668void PowerSupply::inventoryAdded(sdbusplus::message::message& msg)
669{
670 sdbusplus::message::object_path path;
671 msg.read(path);
672 // Make sure the signal is for the PSU inventory path
673 if (path == inventoryPath)
674 {
675 std::map<std::string, std::map<std::string, std::variant<bool>>>
676 interfaces;
677 // Get map of interfaces and their properties
678 msg.read(interfaces);
679
680 auto properties = interfaces.find(INVENTORY_IFACE);
681 if (properties != interfaces.end())
682 {
683 auto property = properties->second.find(PRESENT_PROP);
684 if (property != properties->second.end())
685 {
686 present = std::get<bool>(property->second);
687
688 log<level::INFO>(fmt::format("Power Supply {} Present {}",
689 inventoryPath, present)
690 .c_str());
691
692 updateInventory();
693 }
694 }
695 }
696}
697
Brandon Wyman1d7a7df2020-03-26 10:14:05 -0500698void PowerSupply::updateInventory()
699{
700 using namespace phosphor::pmbus;
701
Chanh Nguyenc12c53b2021-04-06 17:24:47 +0700702#if IBM_VPD
Brandon Wyman1d7a7df2020-03-26 10:14:05 -0500703 std::string ccin;
704 std::string pn;
705 std::string fn;
706 std::string header;
707 std::string sn;
Brandon Wyman1d7a7df2020-03-26 10:14:05 -0500708 using PropertyMap =
George Liu070c1bc2020-10-12 11:28:01 +0800709 std::map<std::string,
710 std::variant<std::string, std::vector<uint8_t>, bool>>;
Brandon Wyman1d7a7df2020-03-26 10:14:05 -0500711 PropertyMap assetProps;
George Liu070c1bc2020-10-12 11:28:01 +0800712 PropertyMap operProps;
Brandon Wyman1d7a7df2020-03-26 10:14:05 -0500713 PropertyMap versionProps;
714 PropertyMap ipzvpdDINFProps;
715 PropertyMap ipzvpdVINIProps;
716 using InterfaceMap = std::map<std::string, PropertyMap>;
717 InterfaceMap interfaces;
718 using ObjectMap = std::map<sdbusplus::message::object_path, InterfaceMap>;
719 ObjectMap object;
720#endif
B. J. Wyman681b2a32021-04-20 22:31:22 +0000721 log<level::DEBUG>(
722 fmt::format("updateInventory() inventoryPath: {}", inventoryPath)
723 .c_str());
Brandon Wyman1d7a7df2020-03-26 10:14:05 -0500724
725 if (present)
726 {
727 // TODO: non-IBM inventory updates?
728
Chanh Nguyenc12c53b2021-04-06 17:24:47 +0700729#if IBM_VPD
Brandon Wyman1d7a7df2020-03-26 10:14:05 -0500730 try
731 {
732 ccin = pmbusIntf->readString(CCIN, Type::HwmonDeviceDebug);
733 assetProps.emplace(MODEL_PROP, ccin);
Adriana Kobylak572a9052021-03-30 15:58:07 +0000734 modelName = ccin;
Brandon Wyman1d7a7df2020-03-26 10:14:05 -0500735 }
Patrick Williamsc1d4de52021-10-06 12:45:57 -0500736 catch (const ReadFailure& e)
Brandon Wyman1d7a7df2020-03-26 10:14:05 -0500737 {
B. J. Wyman681b2a32021-04-20 22:31:22 +0000738 // Ignore the read failure, let pmbus code indicate failure,
739 // path...
Brandon Wyman1d7a7df2020-03-26 10:14:05 -0500740 // TODO - ibm918
741 // https://github.com/openbmc/docs/blob/master/designs/vpd-collection.md
742 // The BMC must log errors if any of the VPD cannot be properly
743 // parsed or fails ECC checks.
744 }
745
746 try
747 {
748 pn = pmbusIntf->readString(PART_NUMBER, Type::HwmonDeviceDebug);
749 assetProps.emplace(PN_PROP, pn);
750 }
Patrick Williamsc1d4de52021-10-06 12:45:57 -0500751 catch (const ReadFailure& e)
Brandon Wyman1d7a7df2020-03-26 10:14:05 -0500752 {
B. J. Wyman681b2a32021-04-20 22:31:22 +0000753 // Ignore the read failure, let pmbus code indicate failure,
754 // path...
Brandon Wyman1d7a7df2020-03-26 10:14:05 -0500755 }
756
757 try
758 {
759 fn = pmbusIntf->readString(FRU_NUMBER, Type::HwmonDeviceDebug);
Brandon Wymana169b0f2021-12-07 20:18:06 +0000760 assetProps.emplace(SPARE_PN_PROP, fn);
Brandon Wyman1d7a7df2020-03-26 10:14:05 -0500761 }
Patrick Williamsc1d4de52021-10-06 12:45:57 -0500762 catch (const ReadFailure& e)
Brandon Wyman1d7a7df2020-03-26 10:14:05 -0500763 {
B. J. Wyman681b2a32021-04-20 22:31:22 +0000764 // Ignore the read failure, let pmbus code indicate failure,
765 // path...
Brandon Wyman1d7a7df2020-03-26 10:14:05 -0500766 }
767
768 try
769 {
770 header =
771 pmbusIntf->readString(SERIAL_HEADER, Type::HwmonDeviceDebug);
772 sn = pmbusIntf->readString(SERIAL_NUMBER, Type::HwmonDeviceDebug);
773 assetProps.emplace(SN_PROP, sn);
774 }
Patrick Williamsc1d4de52021-10-06 12:45:57 -0500775 catch (const ReadFailure& e)
Brandon Wyman1d7a7df2020-03-26 10:14:05 -0500776 {
B. J. Wyman681b2a32021-04-20 22:31:22 +0000777 // Ignore the read failure, let pmbus code indicate failure,
778 // path...
Brandon Wyman1d7a7df2020-03-26 10:14:05 -0500779 }
780
781 try
782 {
Brandon Wymanc9efe412020-10-09 15:42:50 -0500783 fwVersion =
784 pmbusIntf->readString(FW_VERSION, Type::HwmonDeviceDebug);
785 versionProps.emplace(VERSION_PROP, fwVersion);
Brandon Wyman1d7a7df2020-03-26 10:14:05 -0500786 }
Patrick Williamsc1d4de52021-10-06 12:45:57 -0500787 catch (const ReadFailure& e)
Brandon Wyman1d7a7df2020-03-26 10:14:05 -0500788 {
B. J. Wyman681b2a32021-04-20 22:31:22 +0000789 // Ignore the read failure, let pmbus code indicate failure,
790 // path...
Brandon Wyman1d7a7df2020-03-26 10:14:05 -0500791 }
792
793 ipzvpdVINIProps.emplace("CC",
794 std::vector<uint8_t>(ccin.begin(), ccin.end()));
795 ipzvpdVINIProps.emplace("PN",
796 std::vector<uint8_t>(pn.begin(), pn.end()));
797 ipzvpdVINIProps.emplace("FN",
798 std::vector<uint8_t>(fn.begin(), fn.end()));
799 std::string header_sn = header + sn + '\0';
800 ipzvpdVINIProps.emplace(
801 "SN", std::vector<uint8_t>(header_sn.begin(), header_sn.end()));
802 std::string description = "IBM PS";
803 ipzvpdVINIProps.emplace(
804 "DR", std::vector<uint8_t>(description.begin(), description.end()));
805
Ben Tynerf8d8c462022-01-27 16:09:45 -0600806 // Populate the VINI Resource Type (RT) keyword
807 ipzvpdVINIProps.emplace("RT", std::vector<uint8_t>{'V', 'I', 'N', 'I'});
808
Brandon Wyman1d7a7df2020-03-26 10:14:05 -0500809 // Update the Resource Identifier (RI) keyword
810 // 2 byte FRC: 0x0003
811 // 2 byte RID: 0x1000, 0x1001...
812 std::uint8_t num = std::stoul(
813 inventoryPath.substr(inventoryPath.size() - 1, 1), nullptr, 0);
814 std::vector<uint8_t> ri{0x00, 0x03, 0x10, num};
815 ipzvpdDINFProps.emplace("RI", ri);
816
817 // Fill in the FRU Label (FL) keyword.
818 std::string fl = "E";
819 fl.push_back(inventoryPath.back());
820 fl.resize(FL_KW_SIZE, ' ');
821 ipzvpdDINFProps.emplace("FL",
822 std::vector<uint8_t>(fl.begin(), fl.end()));
823
Ben Tynerf8d8c462022-01-27 16:09:45 -0600824 // Populate the DINF Resource Type (RT) keyword
825 ipzvpdDINFProps.emplace("RT", std::vector<uint8_t>{'D', 'I', 'N', 'F'});
826
Brandon Wyman1d7a7df2020-03-26 10:14:05 -0500827 interfaces.emplace(ASSET_IFACE, std::move(assetProps));
828 interfaces.emplace(VERSION_IFACE, std::move(versionProps));
829 interfaces.emplace(DINF_IFACE, std::move(ipzvpdDINFProps));
830 interfaces.emplace(VINI_IFACE, std::move(ipzvpdVINIProps));
831
George Liu070c1bc2020-10-12 11:28:01 +0800832 // Update the Functional
833 operProps.emplace(FUNCTIONAL_PROP, present);
834 interfaces.emplace(OPERATIONAL_STATE_IFACE, std::move(operProps));
835
Brandon Wyman1d7a7df2020-03-26 10:14:05 -0500836 auto path = inventoryPath.substr(strlen(INVENTORY_OBJ_PATH));
837 object.emplace(path, std::move(interfaces));
838
839 try
840 {
841 auto service =
842 util::getService(INVENTORY_OBJ_PATH, INVENTORY_MGR_IFACE, bus);
843
844 if (service.empty())
845 {
846 log<level::ERR>("Unable to get inventory manager service");
847 return;
848 }
849
850 auto method =
851 bus.new_method_call(service.c_str(), INVENTORY_OBJ_PATH,
852 INVENTORY_MGR_IFACE, "Notify");
853
854 method.append(std::move(object));
855
856 auto reply = bus.call(method);
857 }
Patrick Williamsc1d4de52021-10-06 12:45:57 -0500858 catch (const std::exception& e)
Brandon Wyman1d7a7df2020-03-26 10:14:05 -0500859 {
Jay Meyer6a3fd2c2020-08-25 16:37:16 -0500860 log<level::ERR>(
861 std::string(e.what() + std::string(" PATH=") + inventoryPath)
862 .c_str());
Brandon Wyman1d7a7df2020-03-26 10:14:05 -0500863 }
864#endif
865 }
866}
867
Adriana Kobylak4175ffb2021-08-02 14:51:05 +0000868void PowerSupply::getInputVoltage(double& actualInputVoltage,
869 int& inputVoltage) const
870{
871 using namespace phosphor::pmbus;
872
873 actualInputVoltage = in_input::VIN_VOLTAGE_0;
874 inputVoltage = in_input::VIN_VOLTAGE_0;
875
876 if (present)
877 {
878 try
879 {
880 // Read input voltage in millivolts
881 auto inputVoltageStr = pmbusIntf->readString(READ_VIN, Type::Hwmon);
882
883 // Convert to volts
884 actualInputVoltage = std::stod(inputVoltageStr) / 1000;
885
886 // Calculate the voltage based on voltage thresholds
887 if (actualInputVoltage < in_input::VIN_VOLTAGE_MIN)
888 {
889 inputVoltage = in_input::VIN_VOLTAGE_0;
890 }
891 else if (actualInputVoltage < in_input::VIN_VOLTAGE_110_THRESHOLD)
892 {
893 inputVoltage = in_input::VIN_VOLTAGE_110;
894 }
895 else
896 {
897 inputVoltage = in_input::VIN_VOLTAGE_220;
898 }
899 }
900 catch (const std::exception& e)
901 {
902 log<level::ERR>(
903 fmt::format("READ_VIN read error: {}", e.what()).c_str());
904 }
905 }
906}
907
Brandon Wyman3f1242f2020-01-28 13:11:25 -0600908} // namespace phosphor::power::psu