blob: c68e9bef4ed086b548c209ab9c67541e1a0967c3 [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 Wyman4fc191f2022-03-10 23:07:13 +000012#include <chrono> // sleep_for()
13#include <cmath>
Brandon Wyman1d7a7df2020-03-26 10:14:05 -050014#include <cstdint> // uint8_t...
B. J. Wyman681b2a32021-04-20 22:31:22 +000015#include <fstream>
Brandon Wyman056935c2022-06-24 23:05:09 +000016#include <regex>
B. J. Wyman681b2a32021-04-20 22:31:22 +000017#include <thread> // sleep_for()
Brandon Wyman1d7a7df2020-03-26 10:14:05 -050018
Brandon Wyman3f1242f2020-01-28 13:11:25 -060019namespace phosphor::power::psu
Brandon Wymanaed1f752019-11-25 18:10:52 -060020{
B. J. Wyman681b2a32021-04-20 22:31:22 +000021// Amount of time in milliseconds to delay between power supply going from
22// missing to present before running the bind command(s).
23constexpr auto bindDelay = 1000;
Brandon Wymanaed1f752019-11-25 18:10:52 -060024
Brandon Wymanc3324422022-03-24 20:30:57 +000025// The number of INPUT_HISTORY records to keep on D-Bus.
26// Each record covers a 30-second span. That means two records are needed to
27// cover a minute of time. If we want one (1) hour of data, that would be 120
28// records.
29constexpr auto INPUT_HISTORY_MAX_RECORDS = 120;
30
Brandon Wymanaed1f752019-11-25 18:10:52 -060031using namespace phosphor::logging;
Brandon Wyman3f1242f2020-01-28 13:11:25 -060032using namespace sdbusplus::xyz::openbmc_project::Common::Device::Error;
Brandon Wymanaed1f752019-11-25 18:10:52 -060033
Patrick Williams7354ce62022-07-22 19:26:56 -050034PowerSupply::PowerSupply(sdbusplus::bus_t& bus, const std::string& invpath,
B. J. Wyman681b2a32021-04-20 22:31:22 +000035 std::uint8_t i2cbus, std::uint16_t i2caddr,
Brandon Wymanc3324422022-03-24 20:30:57 +000036 const std::string& driver,
B. J. Wyman681b2a32021-04-20 22:31:22 +000037 const std::string& gpioLineName) :
Brandon Wyman510acaa2020-11-05 18:32:04 -060038 bus(bus),
Brandon Wymanc3324422022-03-24 20:30:57 +000039 inventoryPath(invpath), bindPath("/sys/bus/i2c/drivers/" + driver)
Brandon Wyman510acaa2020-11-05 18:32:04 -060040{
41 if (inventoryPath.empty())
42 {
43 throw std::invalid_argument{"Invalid empty inventoryPath"};
44 }
45
B. J. Wyman681b2a32021-04-20 22:31:22 +000046 if (gpioLineName.empty())
47 {
48 throw std::invalid_argument{"Invalid empty gpioLineName"};
49 }
Brandon Wyman510acaa2020-11-05 18:32:04 -060050
Brandon Wyman321a6152022-03-19 00:11:44 +000051 shortName = findShortName(inventoryPath);
52
53 log<level::DEBUG>(
54 fmt::format("{} gpioLineName: {}", shortName, gpioLineName).c_str());
B. J. Wyman681b2a32021-04-20 22:31:22 +000055 presenceGPIO = createGPIO(gpioLineName);
Brandon Wyman510acaa2020-11-05 18:32:04 -060056
57 std::ostringstream ss;
58 ss << std::hex << std::setw(4) << std::setfill('0') << i2caddr;
59 std::string addrStr = ss.str();
B. J. Wyman681b2a32021-04-20 22:31:22 +000060 std::string busStr = std::to_string(i2cbus);
61 bindDevice = busStr;
62 bindDevice.append("-");
63 bindDevice.append(addrStr);
64
Brandon Wyman510acaa2020-11-05 18:32:04 -060065 pmbusIntf = phosphor::pmbus::createPMBus(i2cbus, addrStr);
66
67 // Get the current state of the Present property.
B. J. Wyman681b2a32021-04-20 22:31:22 +000068 try
69 {
70 updatePresenceGPIO();
71 }
72 catch (...)
73 {
74 // If the above attempt to use the GPIO failed, it likely means that the
75 // GPIOs are in use by the kernel, meaning it is using gpio-keys.
76 // So, I should rely on phosphor-gpio-presence to update D-Bus, and
77 // work that way for power supply presence.
78 presenceGPIO = nullptr;
79 // Setup the functions to call when the D-Bus inventory path for the
80 // Present property changes.
81 presentMatch = std::make_unique<sdbusplus::bus::match_t>(
82 bus,
83 sdbusplus::bus::match::rules::propertiesChanged(inventoryPath,
84 INVENTORY_IFACE),
85 [this](auto& msg) { this->inventoryChanged(msg); });
86
87 presentAddedMatch = std::make_unique<sdbusplus::bus::match_t>(
88 bus,
89 sdbusplus::bus::match::rules::interfacesAdded() +
90 sdbusplus::bus::match::rules::argNpath(0, inventoryPath),
91 [this](auto& msg) { this->inventoryAdded(msg); });
92
93 updatePresence();
94 updateInventory();
Brandon Wymanc3324422022-03-24 20:30:57 +000095 setupInputHistory();
B. J. Wyman681b2a32021-04-20 22:31:22 +000096 }
97}
98
99void PowerSupply::bindOrUnbindDriver(bool present)
100{
101 auto action = (present) ? "bind" : "unbind";
102 auto path = bindPath / action;
103
104 if (present)
105 {
Brandon Wymanb1ee60f2022-03-22 22:37:12 +0000106 std::this_thread::sleep_for(std::chrono::milliseconds(bindDelay));
B. J. Wyman681b2a32021-04-20 22:31:22 +0000107 log<level::INFO>(
108 fmt::format("Binding device driver. path: {} device: {}",
109 path.string(), bindDevice)
110 .c_str());
111 }
112 else
113 {
114 log<level::INFO>(
115 fmt::format("Unbinding device driver. path: {} device: {}",
116 path.string(), bindDevice)
117 .c_str());
118 }
119
120 std::ofstream file;
121
122 file.exceptions(std::ofstream::failbit | std::ofstream::badbit |
123 std::ofstream::eofbit);
124
125 try
126 {
127 file.open(path);
128 file << bindDevice;
129 file.close();
130 }
Patrick Williamsc1d4de52021-10-06 12:45:57 -0500131 catch (const std::exception& e)
B. J. Wyman681b2a32021-04-20 22:31:22 +0000132 {
133 auto err = errno;
134
135 log<level::ERR>(
136 fmt::format("Failed binding or unbinding device. errno={}", err)
137 .c_str());
138 }
Brandon Wyman510acaa2020-11-05 18:32:04 -0600139}
140
Brandon Wymanaed1f752019-11-25 18:10:52 -0600141void PowerSupply::updatePresence()
142{
143 try
144 {
Brandon Wyman3f1242f2020-01-28 13:11:25 -0600145 present = getPresence(bus, inventoryPath);
Brandon Wymanaed1f752019-11-25 18:10:52 -0600146 }
Patrick Williams7354ce62022-07-22 19:26:56 -0500147 catch (const sdbusplus::exception_t& e)
Brandon Wymanaed1f752019-11-25 18:10:52 -0600148 {
149 // Relying on property change or interface added to retry.
150 // Log an informational trace to the journal.
Brandon Wymandf13c3a2020-12-15 14:25:22 -0600151 log<level::INFO>(
152 fmt::format("D-Bus property {} access failure exception",
153 inventoryPath)
154 .c_str());
Brandon Wymanaed1f752019-11-25 18:10:52 -0600155 }
156}
157
B. J. Wyman681b2a32021-04-20 22:31:22 +0000158void PowerSupply::updatePresenceGPIO()
159{
160 bool presentOld = present;
161
162 try
163 {
164 if (presenceGPIO->read() > 0)
165 {
166 present = true;
167 }
168 else
169 {
170 present = false;
171 }
172 }
Patrick Williamsc1d4de52021-10-06 12:45:57 -0500173 catch (const std::exception& e)
B. J. Wyman681b2a32021-04-20 22:31:22 +0000174 {
175 log<level::ERR>(
176 fmt::format("presenceGPIO read fail: {}", e.what()).c_str());
177 throw;
178 }
179
180 if (presentOld != present)
181 {
Brandon Wyman321a6152022-03-19 00:11:44 +0000182 log<level::DEBUG>(fmt::format("{} presentOld: {} present: {}",
183 shortName, presentOld, present)
184 .c_str());
Matt Spinlerca1e9ea2022-02-18 14:03:08 -0600185
186 auto invpath = inventoryPath.substr(strlen(INVENTORY_OBJ_PATH));
Brandon Wyman90d529a2022-03-22 23:02:54 +0000187
188 bindOrUnbindDriver(present);
189 if (present)
190 {
191 // If the power supply was present, then missing, and present again,
192 // the hwmon path may have changed. We will need the correct/updated
193 // path before any reads or writes are attempted.
194 pmbusIntf->findHwmonDir();
195 }
196
Brandon Wyman321a6152022-03-19 00:11:44 +0000197 setPresence(bus, invpath, present, shortName);
Brandon Wymanc3324422022-03-24 20:30:57 +0000198 setupInputHistory();
Matt Spinlerca1e9ea2022-02-18 14:03:08 -0600199 updateInventory();
200
Brandon Wyman90d529a2022-03-22 23:02:54 +0000201 // Need Functional to already be correct before calling this.
Matt Spinlerca1e9ea2022-02-18 14:03:08 -0600202 checkAvailability();
203
B. J. Wyman681b2a32021-04-20 22:31:22 +0000204 if (present)
205 {
B. J. Wyman681b2a32021-04-20 22:31:22 +0000206 onOffConfig(phosphor::pmbus::ON_OFF_CONFIG_CONTROL_PIN_ONLY);
207 clearFaults();
Brandon Wyman18a24d92022-04-19 22:48:34 +0000208 // Indicate that the input history data and timestamps between all
209 // the power supplies that are present in the system need to be
210 // synchronized.
211 syncHistoryRequired = true;
B. J. Wyman681b2a32021-04-20 22:31:22 +0000212 }
B. J. Wyman681b2a32021-04-20 22:31:22 +0000213 }
214}
215
Brandon Wymanc2203432021-12-21 23:09:48 +0000216void PowerSupply::analyzeCMLFault()
217{
218 if (statusWord & phosphor::pmbus::status_word::CML_FAULT)
219 {
Brandon Wymanc2906f42021-12-21 20:14:56 +0000220 if (cmlFault < DEGLITCH_LIMIT)
Brandon Wymanc2203432021-12-21 23:09:48 +0000221 {
Brandon Wyman9e292ee2022-03-10 22:56:23 +0000222 if (statusWord != statusWordOld)
223 {
Brandon Wyman321a6152022-03-19 00:11:44 +0000224 log<level::ERR>(
225 fmt::format("{} CML fault: STATUS_WORD = {:#06x}, "
226 "STATUS_CML = {:#02x}",
227 shortName, statusWord, statusCML)
228 .c_str());
Brandon Wyman9e292ee2022-03-10 22:56:23 +0000229 }
Brandon Wymanc2906f42021-12-21 20:14:56 +0000230 cmlFault++;
231 }
232 }
233 else
234 {
235 cmlFault = 0;
Brandon Wymanc2203432021-12-21 23:09:48 +0000236 }
237}
238
Brandon Wymane3b0bb02021-12-21 23:16:48 +0000239void PowerSupply::analyzeInputFault()
240{
241 if (statusWord & phosphor::pmbus::status_word::INPUT_FAULT_WARN)
242 {
Brandon Wymanc2906f42021-12-21 20:14:56 +0000243 if (inputFault < DEGLITCH_LIMIT)
Brandon Wymane3b0bb02021-12-21 23:16:48 +0000244 {
Brandon Wyman9e292ee2022-03-10 22:56:23 +0000245 if (statusWord != statusWordOld)
246 {
247 log<level::ERR>(
Brandon Wyman321a6152022-03-19 00:11:44 +0000248 fmt::format("{} INPUT fault: STATUS_WORD = {:#06x}, "
Brandon Wyman9e292ee2022-03-10 22:56:23 +0000249 "STATUS_MFR_SPECIFIC = {:#04x}, "
250 "STATUS_INPUT = {:#04x}",
Brandon Wyman321a6152022-03-19 00:11:44 +0000251 shortName, statusWord, statusMFR, statusInput)
Brandon Wyman9e292ee2022-03-10 22:56:23 +0000252 .c_str());
253 }
Brandon Wymanc2906f42021-12-21 20:14:56 +0000254 inputFault++;
255 }
Brandon Wymane3b0bb02021-12-21 23:16:48 +0000256 }
Brandon Wyman82affd92021-11-24 19:12:49 +0000257
258 // If had INPUT/VIN_UV fault, and now off.
259 // Trace that odd behavior.
260 if (inputFault &&
261 !(statusWord & phosphor::pmbus::status_word::INPUT_FAULT_WARN))
262 {
263 log<level::INFO>(
Brandon Wyman321a6152022-03-19 00:11:44 +0000264 fmt::format("{} INPUT fault cleared: STATUS_WORD = {:#06x}, "
Brandon Wyman6f939a32022-03-10 18:42:20 +0000265 "STATUS_MFR_SPECIFIC = {:#04x}, "
266 "STATUS_INPUT = {:#04x}",
Brandon Wyman321a6152022-03-19 00:11:44 +0000267 shortName, statusWord, statusMFR, statusInput)
Brandon Wyman82affd92021-11-24 19:12:49 +0000268 .c_str());
Brandon Wymanc2906f42021-12-21 20:14:56 +0000269 inputFault = 0;
Brandon Wyman82affd92021-11-24 19:12:49 +0000270 }
Brandon Wymane3b0bb02021-12-21 23:16:48 +0000271}
272
Brandon Wymanc2c87132021-12-21 23:22:18 +0000273void PowerSupply::analyzeVoutOVFault()
274{
275 if (statusWord & phosphor::pmbus::status_word::VOUT_OV_FAULT)
276 {
Brandon Wymanc2906f42021-12-21 20:14:56 +0000277 if (voutOVFault < DEGLITCH_LIMIT)
Brandon Wymanc2c87132021-12-21 23:22:18 +0000278 {
Brandon Wyman9e292ee2022-03-10 22:56:23 +0000279 if (statusWord != statusWordOld)
280 {
281 log<level::ERR>(
Brandon Wyman321a6152022-03-19 00:11:44 +0000282 fmt::format(
283 "{} VOUT_OV_FAULT fault: STATUS_WORD = {:#06x}, "
284 "STATUS_MFR_SPECIFIC = {:#04x}, "
285 "STATUS_VOUT = {:#02x}",
286 shortName, statusWord, statusMFR, statusVout)
Brandon Wyman9e292ee2022-03-10 22:56:23 +0000287 .c_str());
288 }
Brandon Wymanc2c87132021-12-21 23:22:18 +0000289
Brandon Wymanc2906f42021-12-21 20:14:56 +0000290 voutOVFault++;
291 }
292 }
293 else
294 {
295 voutOVFault = 0;
Brandon Wymanc2c87132021-12-21 23:22:18 +0000296 }
297}
298
Brandon Wymana00e7302021-12-21 23:28:29 +0000299void PowerSupply::analyzeIoutOCFault()
300{
301 if (statusWord & phosphor::pmbus::status_word::IOUT_OC_FAULT)
302 {
Brandon Wymanc2906f42021-12-21 20:14:56 +0000303 if (ioutOCFault < DEGLITCH_LIMIT)
Brandon Wymana00e7302021-12-21 23:28:29 +0000304 {
Brandon Wyman9e292ee2022-03-10 22:56:23 +0000305 if (statusWord != statusWordOld)
306 {
307 log<level::ERR>(
Brandon Wyman321a6152022-03-19 00:11:44 +0000308 fmt::format("{} IOUT fault: STATUS_WORD = {:#06x}, "
Brandon Wyman9e292ee2022-03-10 22:56:23 +0000309 "STATUS_MFR_SPECIFIC = {:#04x}, "
310 "STATUS_IOUT = {:#04x}",
Brandon Wyman321a6152022-03-19 00:11:44 +0000311 shortName, statusWord, statusMFR, statusIout)
Brandon Wyman9e292ee2022-03-10 22:56:23 +0000312 .c_str());
313 }
Brandon Wymana00e7302021-12-21 23:28:29 +0000314
Brandon Wymanc2906f42021-12-21 20:14:56 +0000315 ioutOCFault++;
316 }
317 }
318 else
319 {
320 ioutOCFault = 0;
Brandon Wymana00e7302021-12-21 23:28:29 +0000321 }
322}
323
Brandon Wyman08378782021-12-21 23:48:15 +0000324void PowerSupply::analyzeVoutUVFault()
325{
326 if ((statusWord & phosphor::pmbus::status_word::VOUT_FAULT) &&
327 !(statusWord & phosphor::pmbus::status_word::VOUT_OV_FAULT))
328 {
Brandon Wymanc2906f42021-12-21 20:14:56 +0000329 if (voutUVFault < DEGLITCH_LIMIT)
Brandon Wyman08378782021-12-21 23:48:15 +0000330 {
Brandon Wyman9e292ee2022-03-10 22:56:23 +0000331 if (statusWord != statusWordOld)
332 {
333 log<level::ERR>(
Brandon Wyman321a6152022-03-19 00:11:44 +0000334 fmt::format(
335 "{} VOUT_UV_FAULT fault: STATUS_WORD = {:#06x}, "
336 "STATUS_MFR_SPECIFIC = {:#04x}, "
337 "STATUS_VOUT = {:#04x}",
338 shortName, statusWord, statusMFR, statusVout)
Brandon Wyman9e292ee2022-03-10 22:56:23 +0000339 .c_str());
340 }
Brandon Wymanc2906f42021-12-21 20:14:56 +0000341 voutUVFault++;
342 }
343 }
344 else
345 {
346 voutUVFault = 0;
Brandon Wyman08378782021-12-21 23:48:15 +0000347 }
348}
349
Brandon Wymand5d9a222021-12-21 23:59:05 +0000350void PowerSupply::analyzeFanFault()
351{
352 if (statusWord & phosphor::pmbus::status_word::FAN_FAULT)
353 {
Brandon Wymanc2906f42021-12-21 20:14:56 +0000354 if (fanFault < DEGLITCH_LIMIT)
Brandon Wymand5d9a222021-12-21 23:59:05 +0000355 {
Brandon Wyman9e292ee2022-03-10 22:56:23 +0000356 if (statusWord != statusWordOld)
357 {
Brandon Wyman321a6152022-03-19 00:11:44 +0000358 log<level::ERR>(fmt::format("{} FANS fault/warning: "
Brandon Wyman9e292ee2022-03-10 22:56:23 +0000359 "STATUS_WORD = {:#06x}, "
360 "STATUS_MFR_SPECIFIC = {:#04x}, "
361 "STATUS_FANS_1_2 = {:#04x}",
Brandon Wyman321a6152022-03-19 00:11:44 +0000362 shortName, statusWord, statusMFR,
363 statusFans12)
Brandon Wyman9e292ee2022-03-10 22:56:23 +0000364 .c_str());
365 }
Brandon Wymanc2906f42021-12-21 20:14:56 +0000366 fanFault++;
367 }
368 }
369 else
370 {
371 fanFault = 0;
Brandon Wymand5d9a222021-12-21 23:59:05 +0000372 }
373}
374
Brandon Wyman52cb3f22021-12-21 23:02:47 +0000375void PowerSupply::analyzeTemperatureFault()
376{
377 if (statusWord & phosphor::pmbus::status_word::TEMPERATURE_FAULT_WARN)
378 {
Brandon Wymanc2906f42021-12-21 20:14:56 +0000379 if (tempFault < DEGLITCH_LIMIT)
Brandon Wyman52cb3f22021-12-21 23:02:47 +0000380 {
Brandon Wyman9e292ee2022-03-10 22:56:23 +0000381 if (statusWord != statusWordOld)
382 {
Brandon Wyman321a6152022-03-19 00:11:44 +0000383 log<level::ERR>(fmt::format("{} TEMPERATURE fault/warning: "
Brandon Wyman9e292ee2022-03-10 22:56:23 +0000384 "STATUS_WORD = {:#06x}, "
385 "STATUS_MFR_SPECIFIC = {:#04x}, "
386 "STATUS_TEMPERATURE = {:#04x}",
Brandon Wyman321a6152022-03-19 00:11:44 +0000387 shortName, statusWord, statusMFR,
Brandon Wyman9e292ee2022-03-10 22:56:23 +0000388 statusTemperature)
389 .c_str());
390 }
Brandon Wymanc2906f42021-12-21 20:14:56 +0000391 tempFault++;
392 }
393 }
394 else
395 {
396 tempFault = 0;
Brandon Wyman52cb3f22021-12-21 23:02:47 +0000397 }
398}
399
Brandon Wyman993b5542021-12-21 22:55:16 +0000400void PowerSupply::analyzePgoodFault()
401{
402 if ((statusWord & phosphor::pmbus::status_word::POWER_GOOD_NEGATED) ||
403 (statusWord & phosphor::pmbus::status_word::UNIT_IS_OFF))
404 {
Brandon Wyman6d469fd2022-06-15 16:58:21 +0000405 if (pgoodFault < PGOOD_DEGLITCH_LIMIT)
Brandon Wyman993b5542021-12-21 22:55:16 +0000406 {
Brandon Wyman9e292ee2022-03-10 22:56:23 +0000407 if (statusWord != statusWordOld)
408 {
Brandon Wyman321a6152022-03-19 00:11:44 +0000409 log<level::ERR>(fmt::format("{} PGOOD fault: "
Brandon Wyman9e292ee2022-03-10 22:56:23 +0000410 "STATUS_WORD = {:#06x}, "
411 "STATUS_MFR_SPECIFIC = {:#04x}",
Brandon Wyman321a6152022-03-19 00:11:44 +0000412 shortName, statusWord, statusMFR)
Brandon Wyman9e292ee2022-03-10 22:56:23 +0000413 .c_str());
414 }
Brandon Wyman993b5542021-12-21 22:55:16 +0000415 pgoodFault++;
416 }
417 }
418 else
419 {
420 pgoodFault = 0;
421 }
422}
423
Brandon Wyman39ea02b2021-11-23 23:22:23 +0000424void PowerSupply::determineMFRFault()
425{
426 if (bindPath.string().find("ibm-cffps") != std::string::npos)
427 {
428 // IBM MFR_SPECIFIC[4] is PS_Kill fault
429 if (statusMFR & 0x10)
430 {
Brandon Wymanc2906f42021-12-21 20:14:56 +0000431 if (psKillFault < DEGLITCH_LIMIT)
432 {
433 psKillFault++;
434 }
435 }
436 else
437 {
438 psKillFault = 0;
Brandon Wyman39ea02b2021-11-23 23:22:23 +0000439 }
440 // IBM MFR_SPECIFIC[6] is 12Vcs fault.
441 if (statusMFR & 0x40)
442 {
Brandon Wymanc2906f42021-12-21 20:14:56 +0000443 if (ps12VcsFault < DEGLITCH_LIMIT)
444 {
445 ps12VcsFault++;
446 }
447 }
448 else
449 {
450 ps12VcsFault = 0;
Brandon Wyman39ea02b2021-11-23 23:22:23 +0000451 }
452 // IBM MFR_SPECIFIC[7] is 12V Current-Share fault.
453 if (statusMFR & 0x80)
454 {
Brandon Wymanc2906f42021-12-21 20:14:56 +0000455 if (psCS12VFault < DEGLITCH_LIMIT)
456 {
457 psCS12VFault++;
458 }
459 }
460 else
461 {
462 psCS12VFault = 0;
Brandon Wyman39ea02b2021-11-23 23:22:23 +0000463 }
464 }
465}
466
Brandon Wyman6c2ac392021-12-21 22:23:06 +0000467void PowerSupply::analyzeMFRFault()
468{
469 if (statusWord & phosphor::pmbus::status_word::MFR_SPECIFIC_FAULT)
470 {
Brandon Wymanc2906f42021-12-21 20:14:56 +0000471 if (mfrFault < DEGLITCH_LIMIT)
Brandon Wyman6c2ac392021-12-21 22:23:06 +0000472 {
Brandon Wyman9e292ee2022-03-10 22:56:23 +0000473 if (statusWord != statusWordOld)
474 {
Brandon Wyman321a6152022-03-19 00:11:44 +0000475 log<level::ERR>(fmt::format("{} MFR fault: "
Brandon Wyman9e292ee2022-03-10 22:56:23 +0000476 "STATUS_WORD = {:#06x} "
477 "STATUS_MFR_SPECIFIC = {:#04x}",
Brandon Wyman321a6152022-03-19 00:11:44 +0000478 shortName, statusWord, statusMFR)
Brandon Wyman9e292ee2022-03-10 22:56:23 +0000479 .c_str());
480 }
Brandon Wymanc2906f42021-12-21 20:14:56 +0000481 mfrFault++;
Brandon Wyman6c2ac392021-12-21 22:23:06 +0000482 }
483
Brandon Wyman6c2ac392021-12-21 22:23:06 +0000484 determineMFRFault();
485 }
Brandon Wymanc2906f42021-12-21 20:14:56 +0000486 else
487 {
488 mfrFault = 0;
489 }
Brandon Wyman6c2ac392021-12-21 22:23:06 +0000490}
491
Brandon Wymanf087f472021-12-22 00:04:27 +0000492void PowerSupply::analyzeVinUVFault()
493{
494 if (statusWord & phosphor::pmbus::status_word::VIN_UV_FAULT)
495 {
Brandon Wymanc2906f42021-12-21 20:14:56 +0000496 if (vinUVFault < DEGLITCH_LIMIT)
Brandon Wymanf087f472021-12-22 00:04:27 +0000497 {
Brandon Wyman9e292ee2022-03-10 22:56:23 +0000498 if (statusWord != statusWordOld)
499 {
500 log<level::ERR>(
Brandon Wyman321a6152022-03-19 00:11:44 +0000501 fmt::format("{} VIN_UV fault: STATUS_WORD = {:#06x}, "
Brandon Wyman9e292ee2022-03-10 22:56:23 +0000502 "STATUS_MFR_SPECIFIC = {:#04x}, "
503 "STATUS_INPUT = {:#04x}",
Brandon Wyman321a6152022-03-19 00:11:44 +0000504 shortName, statusWord, statusMFR, statusInput)
Brandon Wyman9e292ee2022-03-10 22:56:23 +0000505 .c_str());
506 }
Brandon Wymanc2906f42021-12-21 20:14:56 +0000507 vinUVFault++;
Brandon Wymanf087f472021-12-22 00:04:27 +0000508 }
Jim Wright4ab86562022-11-18 14:05:46 -0600509 // Remember that this PSU has seen an AC fault
510 acFault = AC_FAULT_LIMIT;
Brandon Wymanf087f472021-12-22 00:04:27 +0000511 }
Brandon Wyman82affd92021-11-24 19:12:49 +0000512
513 if (vinUVFault &&
514 !(statusWord & phosphor::pmbus::status_word::VIN_UV_FAULT))
515 {
516 log<level::INFO>(
Brandon Wyman321a6152022-03-19 00:11:44 +0000517 fmt::format("{} VIN_UV fault cleared: STATUS_WORD = {:#06x}, "
Brandon Wyman6f939a32022-03-10 18:42:20 +0000518 "STATUS_MFR_SPECIFIC = {:#04x}, "
519 "STATUS_INPUT = {:#04x}",
Brandon Wyman321a6152022-03-19 00:11:44 +0000520 shortName, statusWord, statusMFR, statusInput)
Brandon Wyman82affd92021-11-24 19:12:49 +0000521 .c_str());
Brandon Wymanc2906f42021-12-21 20:14:56 +0000522 vinUVFault = 0;
Jim Wright4ab86562022-11-18 14:05:46 -0600523 // No AC fail, decrement counter
524 if (acFault)
525 {
526 --acFault;
527 }
Brandon Wyman82affd92021-11-24 19:12:49 +0000528 }
Brandon Wymanf087f472021-12-22 00:04:27 +0000529}
530
Brandon Wyman3f1242f2020-01-28 13:11:25 -0600531void PowerSupply::analyze()
532{
533 using namespace phosphor::pmbus;
534
B. J. Wyman681b2a32021-04-20 22:31:22 +0000535 if (presenceGPIO)
536 {
537 updatePresenceGPIO();
538 }
539
Brandon Wyman32453e92021-12-15 19:00:14 +0000540 if (present)
Brandon Wyman3f1242f2020-01-28 13:11:25 -0600541 {
542 try
543 {
Brandon Wyman9e292ee2022-03-10 22:56:23 +0000544 statusWordOld = statusWord;
Brandon Wyman32453e92021-12-15 19:00:14 +0000545 statusWord = pmbusIntf->read(STATUS_WORD, Type::Debug,
546 (readFail < LOG_LIMIT));
Brandon Wymanf65c4062020-08-19 13:15:53 -0500547 // Read worked, reset the fail count.
548 readFail = 0;
Brandon Wyman3f1242f2020-01-28 13:11:25 -0600549
550 if (statusWord)
551 {
Brandon Wymanf07bc792021-10-12 19:00:35 +0000552 statusInput = pmbusIntf->read(STATUS_INPUT, Type::Debug);
Jay Meyer10d94052020-11-30 14:41:21 -0600553 statusMFR = pmbusIntf->read(STATUS_MFR, Type::Debug);
Brandon Wyman85c7bf42021-10-19 22:28:48 +0000554 statusCML = pmbusIntf->read(STATUS_CML, Type::Debug);
Brandon Wyman6710ba22021-10-27 17:39:31 +0000555 auto status0Vout = pmbusIntf->insertPageNum(STATUS_VOUT, 0);
556 statusVout = pmbusIntf->read(status0Vout, Type::Debug);
Brandon Wymanb10b3be2021-11-09 22:12:15 +0000557 statusIout = pmbusIntf->read(STATUS_IOUT, Type::Debug);
Brandon Wyman7ee4d7e2021-11-19 20:48:23 +0000558 statusFans12 = pmbusIntf->read(STATUS_FANS_1_2, Type::Debug);
Brandon Wyman96893a42021-11-05 19:56:57 +0000559 statusTemperature =
560 pmbusIntf->read(STATUS_TEMPERATURE, Type::Debug);
Brandon Wyman9ddc6222021-10-28 17:28:01 +0000561
Brandon Wymanc2203432021-12-21 23:09:48 +0000562 analyzeCMLFault();
Brandon Wyman85c7bf42021-10-19 22:28:48 +0000563
Brandon Wymane3b0bb02021-12-21 23:16:48 +0000564 analyzeInputFault();
Brandon Wyman3f1242f2020-01-28 13:11:25 -0600565
Brandon Wymanc2c87132021-12-21 23:22:18 +0000566 analyzeVoutOVFault();
Brandon Wyman6710ba22021-10-27 17:39:31 +0000567
Brandon Wymana00e7302021-12-21 23:28:29 +0000568 analyzeIoutOCFault();
Brandon Wymanb10b3be2021-11-09 22:12:15 +0000569
Brandon Wyman08378782021-12-21 23:48:15 +0000570 analyzeVoutUVFault();
Brandon Wyman2cf46942021-10-28 19:09:16 +0000571
Brandon Wymand5d9a222021-12-21 23:59:05 +0000572 analyzeFanFault();
Brandon Wyman7ee4d7e2021-11-19 20:48:23 +0000573
Brandon Wyman52cb3f22021-12-21 23:02:47 +0000574 analyzeTemperatureFault();
Brandon Wyman96893a42021-11-05 19:56:57 +0000575
Brandon Wyman993b5542021-12-21 22:55:16 +0000576 analyzePgoodFault();
Brandon Wyman2916ea52021-11-06 03:31:18 +0000577
Brandon Wyman6c2ac392021-12-21 22:23:06 +0000578 analyzeMFRFault();
Brandon Wyman3f1242f2020-01-28 13:11:25 -0600579
Brandon Wymanf087f472021-12-22 00:04:27 +0000580 analyzeVinUVFault();
Brandon Wyman3f1242f2020-01-28 13:11:25 -0600581 }
582 else
583 {
Brandon Wyman9e292ee2022-03-10 22:56:23 +0000584 if (statusWord != statusWordOld)
585 {
Brandon Wyman321a6152022-03-19 00:11:44 +0000586 log<level::INFO>(fmt::format("{} STATUS_WORD = {:#06x}",
587 shortName, statusWord)
Brandon Wyman9e292ee2022-03-10 22:56:23 +0000588 .c_str());
589 }
590
Brandon Wymane3f7ad22021-12-21 20:27:45 +0000591 // if INPUT/VIN_UV fault was on, it cleared, trace it.
592 if (inputFault)
593 {
594 log<level::INFO>(
595 fmt::format(
Brandon Wyman321a6152022-03-19 00:11:44 +0000596 "{} INPUT fault cleared: STATUS_WORD = {:#06x}",
597 shortName, statusWord)
Brandon Wymane3f7ad22021-12-21 20:27:45 +0000598 .c_str());
599 }
600
601 if (vinUVFault)
602 {
603 log<level::INFO>(
Brandon Wyman321a6152022-03-19 00:11:44 +0000604 fmt::format("{} VIN_UV cleared: STATUS_WORD = {:#06x}",
605 shortName, statusWord)
Brandon Wymane3f7ad22021-12-21 20:27:45 +0000606 .c_str());
607 }
608
Brandon Wyman06ca4592021-12-06 22:52:23 +0000609 if (pgoodFault > 0)
Brandon Wyman4aecc292021-11-10 22:40:41 +0000610 {
Brandon Wyman321a6152022-03-19 00:11:44 +0000611 log<level::INFO>(
612 fmt::format("{} pgoodFault cleared", shortName)
613 .c_str());
Brandon Wyman4aecc292021-11-10 22:40:41 +0000614 }
Brandon Wymane3f7ad22021-12-21 20:27:45 +0000615
616 clearFaultFlags();
Jim Wright4ab86562022-11-18 14:05:46 -0600617 // No AC fail, decrement counter
618 if (acFault)
619 {
620 --acFault;
621 }
Brandon Wyman3f1242f2020-01-28 13:11:25 -0600622 }
Brandon Wyman82affd92021-11-24 19:12:49 +0000623
624 // Save off old inputVoltage value.
625 // Get latest inputVoltage.
626 // If voltage went from below minimum, and now is not, clear faults.
627 // Note: getInputVoltage() has its own try/catch.
628 int inputVoltageOld = inputVoltage;
Brandon Wyman4fc191f2022-03-10 23:07:13 +0000629 double actualInputVoltageOld = actualInputVoltage;
Brandon Wyman82affd92021-11-24 19:12:49 +0000630 getInputVoltage(actualInputVoltage, inputVoltage);
631 if ((inputVoltageOld == in_input::VIN_VOLTAGE_0) &&
632 (inputVoltage != in_input::VIN_VOLTAGE_0))
633 {
634 log<level::INFO>(
635 fmt::format(
Brandon Wyman4fc191f2022-03-10 23:07:13 +0000636 "{} READ_VIN back in range: actualInputVoltageOld = {} "
637 "actualInputVoltage = {}",
638 shortName, actualInputVoltageOld, actualInputVoltage)
Brandon Wyman82affd92021-11-24 19:12:49 +0000639 .c_str());
Brandon Wyman3225a452022-03-18 18:51:49 +0000640 clearVinUVFault();
Brandon Wyman82affd92021-11-24 19:12:49 +0000641 }
Brandon Wyman4fc191f2022-03-10 23:07:13 +0000642 else if (vinUVFault && (inputVoltage != in_input::VIN_VOLTAGE_0))
643 {
644 log<level::INFO>(
645 fmt::format(
646 "{} CLEAR_FAULTS: vinUVFault {} actualInputVoltage {}",
647 shortName, vinUVFault, actualInputVoltage)
648 .c_str());
649 // Do we have a VIN_UV fault latched that can now be cleared
Jim Wright4ab86562022-11-18 14:05:46 -0600650 // due to voltage back in range? Attempt to clear the
651 // fault(s), re-check faults on next call.
Brandon Wyman3225a452022-03-18 18:51:49 +0000652 clearVinUVFault();
Brandon Wyman4fc191f2022-03-10 23:07:13 +0000653 }
Brandon Wymanae35ac52022-05-23 22:33:40 +0000654 else if (std::abs(actualInputVoltageOld - actualInputVoltage) >
655 10.0)
Brandon Wyman4fc191f2022-03-10 23:07:13 +0000656 {
657 log<level::INFO>(
658 fmt::format(
659 "{} actualInputVoltageOld = {} actualInputVoltage = {}",
660 shortName, actualInputVoltageOld, actualInputVoltage)
661 .c_str());
662 }
Matt Spinler0975eaf2022-02-14 15:38:30 -0600663
664 checkAvailability();
Brandon Wymanc3324422022-03-24 20:30:57 +0000665
666 if (inputHistorySupported)
667 {
668 updateHistory();
669 }
Brandon Wyman3f1242f2020-01-28 13:11:25 -0600670 }
Patrick Williamsc1d4de52021-10-06 12:45:57 -0500671 catch (const ReadFailure& e)
Brandon Wyman3f1242f2020-01-28 13:11:25 -0600672 {
Brandon Wyman32453e92021-12-15 19:00:14 +0000673 if (readFail < SIZE_MAX)
674 {
675 readFail++;
676 }
677 if (readFail == LOG_LIMIT)
678 {
679 phosphor::logging::commit<ReadFailure>();
680 }
Brandon Wyman3f1242f2020-01-28 13:11:25 -0600681 }
682 }
683}
684
Brandon Wyman59a35792020-06-04 12:37:40 -0500685void PowerSupply::onOffConfig(uint8_t data)
686{
687 using namespace phosphor::pmbus;
688
689 if (present)
690 {
691 log<level::INFO>("ON_OFF_CONFIG write", entry("DATA=0x%02X", data));
692 try
693 {
694 std::vector<uint8_t> configData{data};
695 pmbusIntf->writeBinary(ON_OFF_CONFIG, configData,
696 Type::HwmonDeviceDebug);
697 }
698 catch (...)
699 {
700 // The underlying code in writeBinary will log a message to the
B. J. Wyman681b2a32021-04-20 22:31:22 +0000701 // journal if the write fails. If the ON_OFF_CONFIG is not setup
702 // as desired, later fault detection and analysis code should
703 // catch any of the fall out. We should not need to terminate
704 // the application if this write fails.
Brandon Wyman59a35792020-06-04 12:37:40 -0500705 }
706 }
707}
708
Brandon Wyman3225a452022-03-18 18:51:49 +0000709void PowerSupply::clearVinUVFault()
710{
711 // Read in1_lcrit_alarm to clear bits 3 and 4 of STATUS_INPUT.
712 // The fault bits in STAUTS_INPUT roll-up to STATUS_WORD. Clearing those
713 // bits in STATUS_INPUT should result in the corresponding STATUS_WORD bits
714 // also clearing.
715 //
716 // Do not care about return value. Should be 1 if active, 0 if not.
717 static_cast<void>(
718 pmbusIntf->read("in1_lcrit_alarm", phosphor::pmbus::Type::Hwmon));
719 vinUVFault = 0;
720}
721
Brandon Wyman3c208462020-05-13 16:25:58 -0500722void PowerSupply::clearFaults()
723{
Brandon Wyman82affd92021-11-24 19:12:49 +0000724 log<level::DEBUG>(
725 fmt::format("clearFaults() inventoryPath: {}", inventoryPath).c_str());
Brandon Wyman5474c912021-02-23 14:39:43 -0600726 faultLogged = false;
Brandon Wyman3c208462020-05-13 16:25:58 -0500727 // The PMBus device driver does not allow for writing CLEAR_FAULTS
728 // directly. However, the pmbus hwmon device driver code will send a
729 // CLEAR_FAULTS after reading from any of the hwmon "files" in sysfs, so
730 // reading in1_input should result in clearing the fault bits in
731 // STATUS_BYTE/STATUS_WORD.
732 // I do not care what the return value is.
Brandon Wyman11151532020-11-10 13:45:57 -0600733 if (present)
Brandon Wyman3c208462020-05-13 16:25:58 -0500734 {
Brandon Wymane3f7ad22021-12-21 20:27:45 +0000735 clearFaultFlags();
Matt Spinler0975eaf2022-02-14 15:38:30 -0600736 checkAvailability();
Brandon Wyman9564e942020-11-10 14:01:42 -0600737 readFail = 0;
Brandon Wyman9564e942020-11-10 14:01:42 -0600738
Brandon Wyman11151532020-11-10 13:45:57 -0600739 try
740 {
Brandon Wyman3225a452022-03-18 18:51:49 +0000741 clearVinUVFault();
Brandon Wyman11151532020-11-10 13:45:57 -0600742 static_cast<void>(
743 pmbusIntf->read("in1_input", phosphor::pmbus::Type::Hwmon));
744 }
Patrick Williamsc1d4de52021-10-06 12:45:57 -0500745 catch (const ReadFailure& e)
Brandon Wyman11151532020-11-10 13:45:57 -0600746 {
747 // Since I do not care what the return value is, I really do not
B. J. Wyman681b2a32021-04-20 22:31:22 +0000748 // care much if it gets a ReadFailure either. However, this
749 // should not prevent the application from continuing to run, so
750 // catching the read failure.
Brandon Wyman11151532020-11-10 13:45:57 -0600751 }
Brandon Wyman3c208462020-05-13 16:25:58 -0500752 }
753}
754
Patrick Williams7354ce62022-07-22 19:26:56 -0500755void PowerSupply::inventoryChanged(sdbusplus::message_t& msg)
Brandon Wymanaed1f752019-11-25 18:10:52 -0600756{
757 std::string msgSensor;
Patrick Williamsabe49412020-05-13 17:59:47 -0500758 std::map<std::string, std::variant<uint32_t, bool>> msgData;
Brandon Wymanaed1f752019-11-25 18:10:52 -0600759 msg.read(msgSensor, msgData);
760
761 // Check if it was the Present property that changed.
762 auto valPropMap = msgData.find(PRESENT_PROP);
763 if (valPropMap != msgData.end())
764 {
765 if (std::get<bool>(valPropMap->second))
766 {
767 present = true;
B. J. Wyman681b2a32021-04-20 22:31:22 +0000768 // TODO: Immediately trying to read or write the "files" causes
769 // read or write failures.
Brandon Wyman1d7a7df2020-03-26 10:14:05 -0500770 using namespace std::chrono_literals;
771 std::this_thread::sleep_for(20ms);
Brandon Wyman9564e942020-11-10 14:01:42 -0600772 pmbusIntf->findHwmonDir();
Brandon Wyman59a35792020-06-04 12:37:40 -0500773 onOffConfig(phosphor::pmbus::ON_OFF_CONFIG_CONTROL_PIN_ONLY);
Brandon Wymanaed1f752019-11-25 18:10:52 -0600774 clearFaults();
Brandon Wyman1d7a7df2020-03-26 10:14:05 -0500775 updateInventory();
Brandon Wymanaed1f752019-11-25 18:10:52 -0600776 }
777 else
778 {
779 present = false;
780
781 // Clear out the now outdated inventory properties
782 updateInventory();
783 }
Matt Spinler0975eaf2022-02-14 15:38:30 -0600784 checkAvailability();
Brandon Wymanaed1f752019-11-25 18:10:52 -0600785 }
786}
787
Patrick Williams7354ce62022-07-22 19:26:56 -0500788void PowerSupply::inventoryAdded(sdbusplus::message_t& msg)
Brandon Wyman9a507db2021-02-25 16:15:22 -0600789{
790 sdbusplus::message::object_path path;
791 msg.read(path);
792 // Make sure the signal is for the PSU inventory path
793 if (path == inventoryPath)
794 {
795 std::map<std::string, std::map<std::string, std::variant<bool>>>
796 interfaces;
797 // Get map of interfaces and their properties
798 msg.read(interfaces);
799
800 auto properties = interfaces.find(INVENTORY_IFACE);
801 if (properties != interfaces.end())
802 {
803 auto property = properties->second.find(PRESENT_PROP);
804 if (property != properties->second.end())
805 {
806 present = std::get<bool>(property->second);
807
808 log<level::INFO>(fmt::format("Power Supply {} Present {}",
809 inventoryPath, present)
810 .c_str());
811
812 updateInventory();
Matt Spinler0975eaf2022-02-14 15:38:30 -0600813 checkAvailability();
Brandon Wyman9a507db2021-02-25 16:15:22 -0600814 }
815 }
816 }
817}
818
Brandon Wyman8393f462022-06-28 16:06:46 +0000819auto PowerSupply::readVPDValue(const std::string& vpdName,
820 const phosphor::pmbus::Type& type,
821 const std::size_t& vpdSize)
822{
823 std::string vpdValue;
Brandon Wyman056935c2022-06-24 23:05:09 +0000824 const std::regex illegalVPDRegex =
825 std::regex("[^[:alnum:]]", std::regex::basic);
Brandon Wyman8393f462022-06-28 16:06:46 +0000826
827 try
828 {
829 vpdValue = pmbusIntf->readString(vpdName, type);
830 }
831 catch (const ReadFailure& e)
832 {
833 // Ignore the read failure, let pmbus code indicate failure,
834 // path...
835 // TODO - ibm918
836 // https://github.com/openbmc/docs/blob/master/designs/vpd-collection.md
837 // The BMC must log errors if any of the VPD cannot be properly
838 // parsed or fails ECC checks.
839 }
840
841 if (vpdValue.size() != vpdSize)
842 {
843 log<level::INFO>(fmt::format("{} {} resize needed. size: {}", shortName,
844 vpdName, vpdValue.size())
845 .c_str());
846 vpdValue.resize(vpdSize, ' ');
847 }
848
Brandon Wyman056935c2022-06-24 23:05:09 +0000849 // Replace any illegal values with space(s).
850 std::regex_replace(vpdValue.begin(), vpdValue.begin(), vpdValue.end(),
851 illegalVPDRegex, " ");
852
Brandon Wyman8393f462022-06-28 16:06:46 +0000853 return vpdValue;
854}
855
Brandon Wyman1d7a7df2020-03-26 10:14:05 -0500856void PowerSupply::updateInventory()
857{
858 using namespace phosphor::pmbus;
859
Chanh Nguyenc12c53b2021-04-06 17:24:47 +0700860#if IBM_VPD
Brandon Wyman1d7a7df2020-03-26 10:14:05 -0500861 std::string pn;
862 std::string fn;
863 std::string header;
864 std::string sn;
Brandon Wyman8393f462022-06-28 16:06:46 +0000865 // The IBM power supply splits the full serial number into two parts.
866 // Each part is 6 bytes long, which should match up with SN_KW_SIZE.
867 const auto HEADER_SIZE = 6;
868 const auto SERIAL_SIZE = 6;
869 // The IBM PSU firmware version size is a bit complicated. It was originally
870 // 1-byte, per command. It was later expanded to 2-bytes per command, then
871 // up to 8-bytes per command. The device driver only reads up to 2 bytes per
872 // command, but combines all three of the 2-byte reads, or all 4 of the
873 // 1-byte reads into one string. So, the maximum size expected is 6 bytes.
Shawn McCarney983c9892022-10-12 10:49:47 -0500874 // However, it is formatted by the driver as a hex string with two ASCII
875 // characters per byte. So the maximum ASCII string size is 12.
876 const auto VERSION_SIZE = 12;
Brandon Wyman8393f462022-06-28 16:06:46 +0000877
Brandon Wyman1d7a7df2020-03-26 10:14:05 -0500878 using PropertyMap =
George Liu070c1bc2020-10-12 11:28:01 +0800879 std::map<std::string,
880 std::variant<std::string, std::vector<uint8_t>, bool>>;
Brandon Wyman1d7a7df2020-03-26 10:14:05 -0500881 PropertyMap assetProps;
George Liu070c1bc2020-10-12 11:28:01 +0800882 PropertyMap operProps;
Brandon Wyman1d7a7df2020-03-26 10:14:05 -0500883 PropertyMap versionProps;
884 PropertyMap ipzvpdDINFProps;
885 PropertyMap ipzvpdVINIProps;
886 using InterfaceMap = std::map<std::string, PropertyMap>;
887 InterfaceMap interfaces;
888 using ObjectMap = std::map<sdbusplus::message::object_path, InterfaceMap>;
889 ObjectMap object;
890#endif
B. J. Wyman681b2a32021-04-20 22:31:22 +0000891 log<level::DEBUG>(
892 fmt::format("updateInventory() inventoryPath: {}", inventoryPath)
893 .c_str());
Brandon Wyman1d7a7df2020-03-26 10:14:05 -0500894
895 if (present)
896 {
897 // TODO: non-IBM inventory updates?
898
Chanh Nguyenc12c53b2021-04-06 17:24:47 +0700899#if IBM_VPD
Brandon Wyman8393f462022-06-28 16:06:46 +0000900 modelName = readVPDValue(CCIN, Type::HwmonDeviceDebug, CC_KW_SIZE);
901 assetProps.emplace(MODEL_PROP, modelName);
Brandon Wyman1d7a7df2020-03-26 10:14:05 -0500902
Brandon Wyman8393f462022-06-28 16:06:46 +0000903 pn = readVPDValue(PART_NUMBER, Type::HwmonDeviceDebug, PN_KW_SIZE);
904 assetProps.emplace(PN_PROP, pn);
Brandon Wyman1d7a7df2020-03-26 10:14:05 -0500905
Brandon Wyman8393f462022-06-28 16:06:46 +0000906 fn = readVPDValue(FRU_NUMBER, Type::HwmonDeviceDebug, FN_KW_SIZE);
907 assetProps.emplace(SPARE_PN_PROP, fn);
Brandon Wyman1d7a7df2020-03-26 10:14:05 -0500908
Brandon Wyman8393f462022-06-28 16:06:46 +0000909 header =
910 readVPDValue(SERIAL_HEADER, Type::HwmonDeviceDebug, HEADER_SIZE);
911 sn = readVPDValue(SERIAL_NUMBER, Type::HwmonDeviceDebug, SERIAL_SIZE);
912 assetProps.emplace(SN_PROP, header + sn);
Brandon Wyman1d7a7df2020-03-26 10:14:05 -0500913
Brandon Wyman8393f462022-06-28 16:06:46 +0000914 fwVersion =
915 readVPDValue(FW_VERSION, Type::HwmonDeviceDebug, VERSION_SIZE);
916 versionProps.emplace(VERSION_PROP, fwVersion);
Brandon Wyman1d7a7df2020-03-26 10:14:05 -0500917
Brandon Wyman8393f462022-06-28 16:06:46 +0000918 ipzvpdVINIProps.emplace(
919 "CC", std::vector<uint8_t>(modelName.begin(), modelName.end()));
Brandon Wyman1d7a7df2020-03-26 10:14:05 -0500920 ipzvpdVINIProps.emplace("PN",
921 std::vector<uint8_t>(pn.begin(), pn.end()));
922 ipzvpdVINIProps.emplace("FN",
923 std::vector<uint8_t>(fn.begin(), fn.end()));
Brandon Wyman33d492f2022-03-23 20:45:17 +0000924 std::string header_sn = header + sn;
Brandon Wyman1d7a7df2020-03-26 10:14:05 -0500925 ipzvpdVINIProps.emplace(
926 "SN", std::vector<uint8_t>(header_sn.begin(), header_sn.end()));
927 std::string description = "IBM PS";
928 ipzvpdVINIProps.emplace(
929 "DR", std::vector<uint8_t>(description.begin(), description.end()));
930
Ben Tynerf8d8c462022-01-27 16:09:45 -0600931 // Populate the VINI Resource Type (RT) keyword
932 ipzvpdVINIProps.emplace("RT", std::vector<uint8_t>{'V', 'I', 'N', 'I'});
933
Brandon Wyman1d7a7df2020-03-26 10:14:05 -0500934 // Update the Resource Identifier (RI) keyword
935 // 2 byte FRC: 0x0003
936 // 2 byte RID: 0x1000, 0x1001...
937 std::uint8_t num = std::stoul(
938 inventoryPath.substr(inventoryPath.size() - 1, 1), nullptr, 0);
939 std::vector<uint8_t> ri{0x00, 0x03, 0x10, num};
940 ipzvpdDINFProps.emplace("RI", ri);
941
942 // Fill in the FRU Label (FL) keyword.
943 std::string fl = "E";
944 fl.push_back(inventoryPath.back());
945 fl.resize(FL_KW_SIZE, ' ');
946 ipzvpdDINFProps.emplace("FL",
947 std::vector<uint8_t>(fl.begin(), fl.end()));
948
Ben Tynerf8d8c462022-01-27 16:09:45 -0600949 // Populate the DINF Resource Type (RT) keyword
950 ipzvpdDINFProps.emplace("RT", std::vector<uint8_t>{'D', 'I', 'N', 'F'});
951
Brandon Wyman1d7a7df2020-03-26 10:14:05 -0500952 interfaces.emplace(ASSET_IFACE, std::move(assetProps));
953 interfaces.emplace(VERSION_IFACE, std::move(versionProps));
954 interfaces.emplace(DINF_IFACE, std::move(ipzvpdDINFProps));
955 interfaces.emplace(VINI_IFACE, std::move(ipzvpdVINIProps));
956
George Liu070c1bc2020-10-12 11:28:01 +0800957 // Update the Functional
958 operProps.emplace(FUNCTIONAL_PROP, present);
959 interfaces.emplace(OPERATIONAL_STATE_IFACE, std::move(operProps));
960
Brandon Wyman1d7a7df2020-03-26 10:14:05 -0500961 auto path = inventoryPath.substr(strlen(INVENTORY_OBJ_PATH));
962 object.emplace(path, std::move(interfaces));
963
964 try
965 {
966 auto service =
967 util::getService(INVENTORY_OBJ_PATH, INVENTORY_MGR_IFACE, bus);
968
969 if (service.empty())
970 {
971 log<level::ERR>("Unable to get inventory manager service");
972 return;
973 }
974
975 auto method =
976 bus.new_method_call(service.c_str(), INVENTORY_OBJ_PATH,
977 INVENTORY_MGR_IFACE, "Notify");
978
979 method.append(std::move(object));
980
981 auto reply = bus.call(method);
982 }
Patrick Williamsc1d4de52021-10-06 12:45:57 -0500983 catch (const std::exception& e)
Brandon Wyman1d7a7df2020-03-26 10:14:05 -0500984 {
Jay Meyer6a3fd2c2020-08-25 16:37:16 -0500985 log<level::ERR>(
986 std::string(e.what() + std::string(" PATH=") + inventoryPath)
987 .c_str());
Brandon Wyman1d7a7df2020-03-26 10:14:05 -0500988 }
989#endif
990 }
991}
992
Brandon Wymanae35ac52022-05-23 22:33:40 +0000993auto PowerSupply::getMaxPowerOut() const
994{
995 using namespace phosphor::pmbus;
996
997 auto maxPowerOut = 0;
998
999 if (present)
1000 {
1001 try
1002 {
1003 // Read max_power_out, should be direct format
1004 auto maxPowerOutStr =
1005 pmbusIntf->readString(MFR_POUT_MAX, Type::HwmonDeviceDebug);
1006 log<level::INFO>(fmt::format("{} MFR_POUT_MAX read {}", shortName,
1007 maxPowerOutStr)
1008 .c_str());
1009 maxPowerOut = std::stod(maxPowerOutStr);
1010 }
1011 catch (const std::exception& e)
1012 {
1013 log<level::ERR>(fmt::format("{} MFR_POUT_MAX read error: {}",
1014 shortName, e.what())
1015 .c_str());
1016 }
1017 }
1018
1019 return maxPowerOut;
1020}
1021
Brandon Wymanc3324422022-03-24 20:30:57 +00001022void PowerSupply::setupInputHistory()
1023{
1024 if (bindPath.string().find("ibm-cffps") != std::string::npos)
1025 {
Brandon Wymanae35ac52022-05-23 22:33:40 +00001026 auto maxPowerOut = getMaxPowerOut();
1027
1028 if (maxPowerOut != phosphor::pmbus::IBM_CFFPS_1400W)
Brandon Wymanc3324422022-03-24 20:30:57 +00001029 {
Brandon Wymanae35ac52022-05-23 22:33:40 +00001030 // Do not enable input history for power supplies that are missing
1031 if (present)
1032 {
1033 inputHistorySupported = true;
1034 log<level::INFO>(
1035 fmt::format("{} INPUT_HISTORY enabled", shortName).c_str());
1036
1037 std::string name{fmt::format("{}_input_power", shortName)};
1038
1039 historyObjectPath =
1040 std::string{INPUT_HISTORY_SENSOR_ROOT} + '/' + name;
1041
1042 // If the power supply was present, we created the
1043 // recordManager. If it then went missing, the recordManager is
1044 // still there. If it then is reinserted, we should be able to
1045 // use the recordManager that was allocated when it was
1046 // initially present.
1047 if (!recordManager)
1048 {
1049 recordManager = std::make_unique<history::RecordManager>(
1050 INPUT_HISTORY_MAX_RECORDS);
1051 }
1052
1053 if (!average)
1054 {
1055 auto avgPath =
1056 historyObjectPath + '/' + history::Average::name;
1057 average = std::make_unique<history::Average>(bus, avgPath);
1058 log<level::DEBUG>(
1059 fmt::format("{} avgPath: {}", shortName, avgPath)
1060 .c_str());
1061 }
1062
1063 if (!maximum)
1064 {
1065 auto maxPath =
1066 historyObjectPath + '/' + history::Maximum::name;
1067 maximum = std::make_unique<history::Maximum>(bus, maxPath);
1068 log<level::DEBUG>(
1069 fmt::format("{} maxPath: {}", shortName, maxPath)
1070 .c_str());
1071 }
1072
1073 log<level::DEBUG>(fmt::format("{} historyObjectPath: {}",
1074 shortName, historyObjectPath)
1075 .c_str());
1076 }
1077 }
1078 else
1079 {
Brandon Wymanc3324422022-03-24 20:30:57 +00001080 log<level::INFO>(
Brandon Wymanae35ac52022-05-23 22:33:40 +00001081 fmt::format("{} INPUT_HISTORY DISABLED. max_power_out: {}",
1082 shortName, maxPowerOut)
1083 .c_str());
1084 inputHistorySupported = false;
Brandon Wymanc3324422022-03-24 20:30:57 +00001085 }
1086 }
1087 else
1088 {
1089 inputHistorySupported = false;
1090 }
1091}
1092
1093void PowerSupply::updateHistory()
1094{
1095 if (!recordManager)
1096 {
1097 // Not enabled
1098 return;
1099 }
1100
1101 if (!present)
1102 {
1103 // Cannot read when not present
1104 return;
1105 }
1106
1107 // Read just the most recent average/max record
1108 auto data =
1109 pmbusIntf->readBinary(INPUT_HISTORY, pmbus::Type::HwmonDeviceDebug,
1110 history::RecordManager::RAW_RECORD_SIZE);
1111
Brandon Wymanae35ac52022-05-23 22:33:40 +00001112 // Update D-Bus only if something changed (a new record ID, or cleared
1113 // out)
Brandon Wymanc3324422022-03-24 20:30:57 +00001114 auto changed = recordManager->add(data);
1115 if (changed)
1116 {
1117 average->values(std::move(recordManager->getAverageRecords()));
1118 maximum->values(std::move(recordManager->getMaximumRecords()));
1119 }
1120}
1121
Adriana Kobylak4175ffb2021-08-02 14:51:05 +00001122void PowerSupply::getInputVoltage(double& actualInputVoltage,
1123 int& inputVoltage) const
1124{
1125 using namespace phosphor::pmbus;
1126
1127 actualInputVoltage = in_input::VIN_VOLTAGE_0;
1128 inputVoltage = in_input::VIN_VOLTAGE_0;
1129
1130 if (present)
1131 {
1132 try
1133 {
1134 // Read input voltage in millivolts
1135 auto inputVoltageStr = pmbusIntf->readString(READ_VIN, Type::Hwmon);
1136
1137 // Convert to volts
1138 actualInputVoltage = std::stod(inputVoltageStr) / 1000;
1139
1140 // Calculate the voltage based on voltage thresholds
1141 if (actualInputVoltage < in_input::VIN_VOLTAGE_MIN)
1142 {
1143 inputVoltage = in_input::VIN_VOLTAGE_0;
1144 }
1145 else if (actualInputVoltage < in_input::VIN_VOLTAGE_110_THRESHOLD)
1146 {
1147 inputVoltage = in_input::VIN_VOLTAGE_110;
1148 }
1149 else
1150 {
1151 inputVoltage = in_input::VIN_VOLTAGE_220;
1152 }
1153 }
1154 catch (const std::exception& e)
1155 {
1156 log<level::ERR>(
Brandon Wyman321a6152022-03-19 00:11:44 +00001157 fmt::format("{} READ_VIN read error: {}", shortName, e.what())
1158 .c_str());
Adriana Kobylak4175ffb2021-08-02 14:51:05 +00001159 }
1160 }
1161}
1162
Matt Spinler0975eaf2022-02-14 15:38:30 -06001163void PowerSupply::checkAvailability()
1164{
1165 bool origAvailability = available;
1166 available = present && !hasInputFault() && !hasVINUVFault() &&
1167 !hasPSKillFault() && !hasIoutOCFault();
1168
1169 if (origAvailability != available)
1170 {
1171 auto invpath = inventoryPath.substr(strlen(INVENTORY_OBJ_PATH));
1172 phosphor::power::psu::setAvailable(bus, invpath, available);
Matt Spinlerca1e9ea2022-02-18 14:03:08 -06001173
1174 // Check if the health rollup needs to change based on the
1175 // new availability value.
1176 phosphor::power::psu::handleChassisHealthRollup(bus, inventoryPath,
1177 !available);
Matt Spinler0975eaf2022-02-14 15:38:30 -06001178 }
1179}
1180
Brandon Wyman3f1242f2020-01-28 13:11:25 -06001181} // namespace phosphor::power::psu