blob: 9d884f390db683807ec7cb68d2d1ced81eaec77f [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,
George Liu9464c422023-02-27 14:30:27 +080037 const std::string& gpioLineName,
38 std::function<bool()>&& callback) :
Brandon Wyman510acaa2020-11-05 18:32:04 -060039 bus(bus),
George Liu9464c422023-02-27 14:30:27 +080040 inventoryPath(invpath), bindPath("/sys/bus/i2c/drivers/" + driver),
41 isPowerOn(std::move(callback))
Brandon Wyman510acaa2020-11-05 18:32:04 -060042{
43 if (inventoryPath.empty())
44 {
45 throw std::invalid_argument{"Invalid empty inventoryPath"};
46 }
47
B. J. Wyman681b2a32021-04-20 22:31:22 +000048 if (gpioLineName.empty())
49 {
50 throw std::invalid_argument{"Invalid empty gpioLineName"};
51 }
Brandon Wyman510acaa2020-11-05 18:32:04 -060052
Brandon Wyman321a6152022-03-19 00:11:44 +000053 shortName = findShortName(inventoryPath);
54
55 log<level::DEBUG>(
56 fmt::format("{} gpioLineName: {}", shortName, gpioLineName).c_str());
B. J. Wyman681b2a32021-04-20 22:31:22 +000057 presenceGPIO = createGPIO(gpioLineName);
Brandon Wyman510acaa2020-11-05 18:32:04 -060058
59 std::ostringstream ss;
60 ss << std::hex << std::setw(4) << std::setfill('0') << i2caddr;
61 std::string addrStr = ss.str();
B. J. Wyman681b2a32021-04-20 22:31:22 +000062 std::string busStr = std::to_string(i2cbus);
63 bindDevice = busStr;
64 bindDevice.append("-");
65 bindDevice.append(addrStr);
66
Brandon Wyman510acaa2020-11-05 18:32:04 -060067 pmbusIntf = phosphor::pmbus::createPMBus(i2cbus, addrStr);
68
69 // Get the current state of the Present property.
B. J. Wyman681b2a32021-04-20 22:31:22 +000070 try
71 {
72 updatePresenceGPIO();
73 }
74 catch (...)
75 {
76 // If the above attempt to use the GPIO failed, it likely means that the
77 // GPIOs are in use by the kernel, meaning it is using gpio-keys.
78 // So, I should rely on phosphor-gpio-presence to update D-Bus, and
79 // work that way for power supply presence.
80 presenceGPIO = nullptr;
81 // Setup the functions to call when the D-Bus inventory path for the
82 // Present property changes.
83 presentMatch = std::make_unique<sdbusplus::bus::match_t>(
84 bus,
85 sdbusplus::bus::match::rules::propertiesChanged(inventoryPath,
86 INVENTORY_IFACE),
87 [this](auto& msg) { this->inventoryChanged(msg); });
88
89 presentAddedMatch = std::make_unique<sdbusplus::bus::match_t>(
90 bus,
91 sdbusplus::bus::match::rules::interfacesAdded() +
92 sdbusplus::bus::match::rules::argNpath(0, inventoryPath),
93 [this](auto& msg) { this->inventoryAdded(msg); });
94
95 updatePresence();
96 updateInventory();
Brandon Wymanc3324422022-03-24 20:30:57 +000097 setupInputHistory();
B. J. Wyman681b2a32021-04-20 22:31:22 +000098 }
99}
100
101void PowerSupply::bindOrUnbindDriver(bool present)
102{
103 auto action = (present) ? "bind" : "unbind";
104 auto path = bindPath / action;
105
106 if (present)
107 {
Brandon Wymanb1ee60f2022-03-22 22:37:12 +0000108 std::this_thread::sleep_for(std::chrono::milliseconds(bindDelay));
B. J. Wyman681b2a32021-04-20 22:31:22 +0000109 log<level::INFO>(
110 fmt::format("Binding device driver. path: {} device: {}",
111 path.string(), bindDevice)
112 .c_str());
113 }
114 else
115 {
116 log<level::INFO>(
117 fmt::format("Unbinding device driver. path: {} device: {}",
118 path.string(), bindDevice)
119 .c_str());
120 }
121
122 std::ofstream file;
123
124 file.exceptions(std::ofstream::failbit | std::ofstream::badbit |
125 std::ofstream::eofbit);
126
127 try
128 {
129 file.open(path);
130 file << bindDevice;
131 file.close();
132 }
Patrick Williamsc1d4de52021-10-06 12:45:57 -0500133 catch (const std::exception& e)
B. J. Wyman681b2a32021-04-20 22:31:22 +0000134 {
135 auto err = errno;
136
137 log<level::ERR>(
138 fmt::format("Failed binding or unbinding device. errno={}", err)
139 .c_str());
140 }
Brandon Wyman510acaa2020-11-05 18:32:04 -0600141}
142
Brandon Wymanaed1f752019-11-25 18:10:52 -0600143void PowerSupply::updatePresence()
144{
145 try
146 {
Brandon Wyman3f1242f2020-01-28 13:11:25 -0600147 present = getPresence(bus, inventoryPath);
Brandon Wymanaed1f752019-11-25 18:10:52 -0600148 }
Patrick Williams7354ce62022-07-22 19:26:56 -0500149 catch (const sdbusplus::exception_t& e)
Brandon Wymanaed1f752019-11-25 18:10:52 -0600150 {
151 // Relying on property change or interface added to retry.
152 // Log an informational trace to the journal.
Brandon Wymandf13c3a2020-12-15 14:25:22 -0600153 log<level::INFO>(
154 fmt::format("D-Bus property {} access failure exception",
155 inventoryPath)
156 .c_str());
Brandon Wymanaed1f752019-11-25 18:10:52 -0600157 }
158}
159
B. J. Wyman681b2a32021-04-20 22:31:22 +0000160void PowerSupply::updatePresenceGPIO()
161{
162 bool presentOld = present;
163
164 try
165 {
166 if (presenceGPIO->read() > 0)
167 {
168 present = true;
169 }
170 else
171 {
172 present = false;
173 }
174 }
Patrick Williamsc1d4de52021-10-06 12:45:57 -0500175 catch (const std::exception& e)
B. J. Wyman681b2a32021-04-20 22:31:22 +0000176 {
177 log<level::ERR>(
178 fmt::format("presenceGPIO read fail: {}", e.what()).c_str());
179 throw;
180 }
181
182 if (presentOld != present)
183 {
Brandon Wyman321a6152022-03-19 00:11:44 +0000184 log<level::DEBUG>(fmt::format("{} presentOld: {} present: {}",
185 shortName, presentOld, present)
186 .c_str());
Matt Spinlerca1e9ea2022-02-18 14:03:08 -0600187
188 auto invpath = inventoryPath.substr(strlen(INVENTORY_OBJ_PATH));
Brandon Wyman90d529a2022-03-22 23:02:54 +0000189
190 bindOrUnbindDriver(present);
191 if (present)
192 {
193 // If the power supply was present, then missing, and present again,
194 // the hwmon path may have changed. We will need the correct/updated
195 // path before any reads or writes are attempted.
196 pmbusIntf->findHwmonDir();
197 }
198
Brandon Wyman321a6152022-03-19 00:11:44 +0000199 setPresence(bus, invpath, present, shortName);
Brandon Wymanc3324422022-03-24 20:30:57 +0000200 setupInputHistory();
Matt Spinlerca1e9ea2022-02-18 14:03:08 -0600201 updateInventory();
202
Brandon Wyman90d529a2022-03-22 23:02:54 +0000203 // Need Functional to already be correct before calling this.
Matt Spinlerca1e9ea2022-02-18 14:03:08 -0600204 checkAvailability();
205
B. J. Wyman681b2a32021-04-20 22:31:22 +0000206 if (present)
207 {
B. J. Wyman681b2a32021-04-20 22:31:22 +0000208 onOffConfig(phosphor::pmbus::ON_OFF_CONFIG_CONTROL_PIN_ONLY);
209 clearFaults();
Brandon Wyman18a24d92022-04-19 22:48:34 +0000210 // Indicate that the input history data and timestamps between all
211 // the power supplies that are present in the system need to be
212 // synchronized.
213 syncHistoryRequired = true;
B. J. Wyman681b2a32021-04-20 22:31:22 +0000214 }
B. J. Wyman681b2a32021-04-20 22:31:22 +0000215 }
216}
217
Brandon Wymanc2203432021-12-21 23:09:48 +0000218void PowerSupply::analyzeCMLFault()
219{
220 if (statusWord & phosphor::pmbus::status_word::CML_FAULT)
221 {
Brandon Wymanc2906f42021-12-21 20:14:56 +0000222 if (cmlFault < DEGLITCH_LIMIT)
Brandon Wymanc2203432021-12-21 23:09:48 +0000223 {
Brandon Wyman9e292ee2022-03-10 22:56:23 +0000224 if (statusWord != statusWordOld)
225 {
Brandon Wyman321a6152022-03-19 00:11:44 +0000226 log<level::ERR>(
227 fmt::format("{} CML fault: STATUS_WORD = {:#06x}, "
228 "STATUS_CML = {:#02x}",
229 shortName, statusWord, statusCML)
230 .c_str());
Brandon Wyman9e292ee2022-03-10 22:56:23 +0000231 }
Brandon Wymanc2906f42021-12-21 20:14:56 +0000232 cmlFault++;
233 }
234 }
235 else
236 {
237 cmlFault = 0;
Brandon Wymanc2203432021-12-21 23:09:48 +0000238 }
239}
240
Brandon Wymane3b0bb02021-12-21 23:16:48 +0000241void PowerSupply::analyzeInputFault()
242{
243 if (statusWord & phosphor::pmbus::status_word::INPUT_FAULT_WARN)
244 {
Brandon Wymanc2906f42021-12-21 20:14:56 +0000245 if (inputFault < DEGLITCH_LIMIT)
Brandon Wymane3b0bb02021-12-21 23:16:48 +0000246 {
Brandon Wyman9e292ee2022-03-10 22:56:23 +0000247 if (statusWord != statusWordOld)
248 {
249 log<level::ERR>(
Brandon Wyman321a6152022-03-19 00:11:44 +0000250 fmt::format("{} INPUT fault: STATUS_WORD = {:#06x}, "
Brandon Wyman9e292ee2022-03-10 22:56:23 +0000251 "STATUS_MFR_SPECIFIC = {:#04x}, "
252 "STATUS_INPUT = {:#04x}",
Brandon Wyman321a6152022-03-19 00:11:44 +0000253 shortName, statusWord, statusMFR, statusInput)
Brandon Wyman9e292ee2022-03-10 22:56:23 +0000254 .c_str());
255 }
Brandon Wymanc2906f42021-12-21 20:14:56 +0000256 inputFault++;
257 }
Brandon Wymane3b0bb02021-12-21 23:16:48 +0000258 }
Brandon Wyman82affd92021-11-24 19:12:49 +0000259
260 // If had INPUT/VIN_UV fault, and now off.
261 // Trace that odd behavior.
262 if (inputFault &&
263 !(statusWord & phosphor::pmbus::status_word::INPUT_FAULT_WARN))
264 {
265 log<level::INFO>(
Brandon Wyman321a6152022-03-19 00:11:44 +0000266 fmt::format("{} INPUT fault cleared: STATUS_WORD = {:#06x}, "
Brandon Wyman6f939a32022-03-10 18:42:20 +0000267 "STATUS_MFR_SPECIFIC = {:#04x}, "
268 "STATUS_INPUT = {:#04x}",
Brandon Wyman321a6152022-03-19 00:11:44 +0000269 shortName, statusWord, statusMFR, statusInput)
Brandon Wyman82affd92021-11-24 19:12:49 +0000270 .c_str());
Brandon Wymanc2906f42021-12-21 20:14:56 +0000271 inputFault = 0;
Brandon Wyman82affd92021-11-24 19:12:49 +0000272 }
Brandon Wymane3b0bb02021-12-21 23:16:48 +0000273}
274
Brandon Wymanc2c87132021-12-21 23:22:18 +0000275void PowerSupply::analyzeVoutOVFault()
276{
277 if (statusWord & phosphor::pmbus::status_word::VOUT_OV_FAULT)
278 {
Brandon Wymanc2906f42021-12-21 20:14:56 +0000279 if (voutOVFault < DEGLITCH_LIMIT)
Brandon Wymanc2c87132021-12-21 23:22:18 +0000280 {
Brandon Wyman9e292ee2022-03-10 22:56:23 +0000281 if (statusWord != statusWordOld)
282 {
283 log<level::ERR>(
Brandon Wyman321a6152022-03-19 00:11:44 +0000284 fmt::format(
285 "{} VOUT_OV_FAULT fault: STATUS_WORD = {:#06x}, "
286 "STATUS_MFR_SPECIFIC = {:#04x}, "
287 "STATUS_VOUT = {:#02x}",
288 shortName, statusWord, statusMFR, statusVout)
Brandon Wyman9e292ee2022-03-10 22:56:23 +0000289 .c_str());
290 }
Brandon Wymanc2c87132021-12-21 23:22:18 +0000291
Brandon Wymanc2906f42021-12-21 20:14:56 +0000292 voutOVFault++;
293 }
294 }
295 else
296 {
297 voutOVFault = 0;
Brandon Wymanc2c87132021-12-21 23:22:18 +0000298 }
299}
300
Brandon Wymana00e7302021-12-21 23:28:29 +0000301void PowerSupply::analyzeIoutOCFault()
302{
303 if (statusWord & phosphor::pmbus::status_word::IOUT_OC_FAULT)
304 {
Brandon Wymanc2906f42021-12-21 20:14:56 +0000305 if (ioutOCFault < DEGLITCH_LIMIT)
Brandon Wymana00e7302021-12-21 23:28:29 +0000306 {
Brandon Wyman9e292ee2022-03-10 22:56:23 +0000307 if (statusWord != statusWordOld)
308 {
309 log<level::ERR>(
Brandon Wyman321a6152022-03-19 00:11:44 +0000310 fmt::format("{} IOUT fault: STATUS_WORD = {:#06x}, "
Brandon Wyman9e292ee2022-03-10 22:56:23 +0000311 "STATUS_MFR_SPECIFIC = {:#04x}, "
312 "STATUS_IOUT = {:#04x}",
Brandon Wyman321a6152022-03-19 00:11:44 +0000313 shortName, statusWord, statusMFR, statusIout)
Brandon Wyman9e292ee2022-03-10 22:56:23 +0000314 .c_str());
315 }
Brandon Wymana00e7302021-12-21 23:28:29 +0000316
Brandon Wymanc2906f42021-12-21 20:14:56 +0000317 ioutOCFault++;
318 }
319 }
320 else
321 {
322 ioutOCFault = 0;
Brandon Wymana00e7302021-12-21 23:28:29 +0000323 }
324}
325
Brandon Wyman08378782021-12-21 23:48:15 +0000326void PowerSupply::analyzeVoutUVFault()
327{
328 if ((statusWord & phosphor::pmbus::status_word::VOUT_FAULT) &&
329 !(statusWord & phosphor::pmbus::status_word::VOUT_OV_FAULT))
330 {
Brandon Wymanc2906f42021-12-21 20:14:56 +0000331 if (voutUVFault < DEGLITCH_LIMIT)
Brandon Wyman08378782021-12-21 23:48:15 +0000332 {
Brandon Wyman9e292ee2022-03-10 22:56:23 +0000333 if (statusWord != statusWordOld)
334 {
335 log<level::ERR>(
Brandon Wyman321a6152022-03-19 00:11:44 +0000336 fmt::format(
337 "{} VOUT_UV_FAULT fault: STATUS_WORD = {:#06x}, "
338 "STATUS_MFR_SPECIFIC = {:#04x}, "
339 "STATUS_VOUT = {:#04x}",
340 shortName, statusWord, statusMFR, statusVout)
Brandon Wyman9e292ee2022-03-10 22:56:23 +0000341 .c_str());
342 }
Brandon Wymanc2906f42021-12-21 20:14:56 +0000343 voutUVFault++;
344 }
345 }
346 else
347 {
348 voutUVFault = 0;
Brandon Wyman08378782021-12-21 23:48:15 +0000349 }
350}
351
Brandon Wymand5d9a222021-12-21 23:59:05 +0000352void PowerSupply::analyzeFanFault()
353{
354 if (statusWord & phosphor::pmbus::status_word::FAN_FAULT)
355 {
Brandon Wymanc2906f42021-12-21 20:14:56 +0000356 if (fanFault < DEGLITCH_LIMIT)
Brandon Wymand5d9a222021-12-21 23:59:05 +0000357 {
Brandon Wyman9e292ee2022-03-10 22:56:23 +0000358 if (statusWord != statusWordOld)
359 {
Brandon Wyman321a6152022-03-19 00:11:44 +0000360 log<level::ERR>(fmt::format("{} FANS fault/warning: "
Brandon Wyman9e292ee2022-03-10 22:56:23 +0000361 "STATUS_WORD = {:#06x}, "
362 "STATUS_MFR_SPECIFIC = {:#04x}, "
363 "STATUS_FANS_1_2 = {:#04x}",
Brandon Wyman321a6152022-03-19 00:11:44 +0000364 shortName, statusWord, statusMFR,
365 statusFans12)
Brandon Wyman9e292ee2022-03-10 22:56:23 +0000366 .c_str());
367 }
Brandon Wymanc2906f42021-12-21 20:14:56 +0000368 fanFault++;
369 }
370 }
371 else
372 {
373 fanFault = 0;
Brandon Wymand5d9a222021-12-21 23:59:05 +0000374 }
375}
376
Brandon Wyman52cb3f22021-12-21 23:02:47 +0000377void PowerSupply::analyzeTemperatureFault()
378{
379 if (statusWord & phosphor::pmbus::status_word::TEMPERATURE_FAULT_WARN)
380 {
Brandon Wymanc2906f42021-12-21 20:14:56 +0000381 if (tempFault < DEGLITCH_LIMIT)
Brandon Wyman52cb3f22021-12-21 23:02:47 +0000382 {
Brandon Wyman9e292ee2022-03-10 22:56:23 +0000383 if (statusWord != statusWordOld)
384 {
Brandon Wyman321a6152022-03-19 00:11:44 +0000385 log<level::ERR>(fmt::format("{} TEMPERATURE fault/warning: "
Brandon Wyman9e292ee2022-03-10 22:56:23 +0000386 "STATUS_WORD = {:#06x}, "
387 "STATUS_MFR_SPECIFIC = {:#04x}, "
388 "STATUS_TEMPERATURE = {:#04x}",
Brandon Wyman321a6152022-03-19 00:11:44 +0000389 shortName, statusWord, statusMFR,
Brandon Wyman9e292ee2022-03-10 22:56:23 +0000390 statusTemperature)
391 .c_str());
392 }
Brandon Wymanc2906f42021-12-21 20:14:56 +0000393 tempFault++;
394 }
395 }
396 else
397 {
398 tempFault = 0;
Brandon Wyman52cb3f22021-12-21 23:02:47 +0000399 }
400}
401
Brandon Wyman993b5542021-12-21 22:55:16 +0000402void PowerSupply::analyzePgoodFault()
403{
404 if ((statusWord & phosphor::pmbus::status_word::POWER_GOOD_NEGATED) ||
405 (statusWord & phosphor::pmbus::status_word::UNIT_IS_OFF))
406 {
Brandon Wyman6d469fd2022-06-15 16:58:21 +0000407 if (pgoodFault < PGOOD_DEGLITCH_LIMIT)
Brandon Wyman993b5542021-12-21 22:55:16 +0000408 {
Brandon Wyman9e292ee2022-03-10 22:56:23 +0000409 if (statusWord != statusWordOld)
410 {
Brandon Wyman321a6152022-03-19 00:11:44 +0000411 log<level::ERR>(fmt::format("{} PGOOD fault: "
Brandon Wyman9e292ee2022-03-10 22:56:23 +0000412 "STATUS_WORD = {:#06x}, "
413 "STATUS_MFR_SPECIFIC = {:#04x}",
Brandon Wyman321a6152022-03-19 00:11:44 +0000414 shortName, statusWord, statusMFR)
Brandon Wyman9e292ee2022-03-10 22:56:23 +0000415 .c_str());
416 }
Brandon Wyman993b5542021-12-21 22:55:16 +0000417 pgoodFault++;
418 }
419 }
420 else
421 {
422 pgoodFault = 0;
423 }
424}
425
Brandon Wyman39ea02b2021-11-23 23:22:23 +0000426void PowerSupply::determineMFRFault()
427{
428 if (bindPath.string().find("ibm-cffps") != std::string::npos)
429 {
430 // IBM MFR_SPECIFIC[4] is PS_Kill fault
431 if (statusMFR & 0x10)
432 {
Brandon Wymanc2906f42021-12-21 20:14:56 +0000433 if (psKillFault < DEGLITCH_LIMIT)
434 {
435 psKillFault++;
436 }
437 }
438 else
439 {
440 psKillFault = 0;
Brandon Wyman39ea02b2021-11-23 23:22:23 +0000441 }
442 // IBM MFR_SPECIFIC[6] is 12Vcs fault.
443 if (statusMFR & 0x40)
444 {
Brandon Wymanc2906f42021-12-21 20:14:56 +0000445 if (ps12VcsFault < DEGLITCH_LIMIT)
446 {
447 ps12VcsFault++;
448 }
449 }
450 else
451 {
452 ps12VcsFault = 0;
Brandon Wyman39ea02b2021-11-23 23:22:23 +0000453 }
454 // IBM MFR_SPECIFIC[7] is 12V Current-Share fault.
455 if (statusMFR & 0x80)
456 {
Brandon Wymanc2906f42021-12-21 20:14:56 +0000457 if (psCS12VFault < DEGLITCH_LIMIT)
458 {
459 psCS12VFault++;
460 }
461 }
462 else
463 {
464 psCS12VFault = 0;
Brandon Wyman39ea02b2021-11-23 23:22:23 +0000465 }
466 }
467}
468
Brandon Wyman6c2ac392021-12-21 22:23:06 +0000469void PowerSupply::analyzeMFRFault()
470{
471 if (statusWord & phosphor::pmbus::status_word::MFR_SPECIFIC_FAULT)
472 {
Brandon Wymanc2906f42021-12-21 20:14:56 +0000473 if (mfrFault < DEGLITCH_LIMIT)
Brandon Wyman6c2ac392021-12-21 22:23:06 +0000474 {
Brandon Wyman9e292ee2022-03-10 22:56:23 +0000475 if (statusWord != statusWordOld)
476 {
Brandon Wyman321a6152022-03-19 00:11:44 +0000477 log<level::ERR>(fmt::format("{} MFR fault: "
Brandon Wyman9e292ee2022-03-10 22:56:23 +0000478 "STATUS_WORD = {:#06x} "
479 "STATUS_MFR_SPECIFIC = {:#04x}",
Brandon Wyman321a6152022-03-19 00:11:44 +0000480 shortName, statusWord, statusMFR)
Brandon Wyman9e292ee2022-03-10 22:56:23 +0000481 .c_str());
482 }
Brandon Wymanc2906f42021-12-21 20:14:56 +0000483 mfrFault++;
Brandon Wyman6c2ac392021-12-21 22:23:06 +0000484 }
485
Brandon Wyman6c2ac392021-12-21 22:23:06 +0000486 determineMFRFault();
487 }
Brandon Wymanc2906f42021-12-21 20:14:56 +0000488 else
489 {
490 mfrFault = 0;
491 }
Brandon Wyman6c2ac392021-12-21 22:23:06 +0000492}
493
Brandon Wymanf087f472021-12-22 00:04:27 +0000494void PowerSupply::analyzeVinUVFault()
495{
496 if (statusWord & phosphor::pmbus::status_word::VIN_UV_FAULT)
497 {
Brandon Wymanc2906f42021-12-21 20:14:56 +0000498 if (vinUVFault < DEGLITCH_LIMIT)
Brandon Wymanf087f472021-12-22 00:04:27 +0000499 {
Brandon Wyman9e292ee2022-03-10 22:56:23 +0000500 if (statusWord != statusWordOld)
501 {
502 log<level::ERR>(
Brandon Wyman321a6152022-03-19 00:11:44 +0000503 fmt::format("{} VIN_UV fault: STATUS_WORD = {:#06x}, "
Brandon Wyman9e292ee2022-03-10 22:56:23 +0000504 "STATUS_MFR_SPECIFIC = {:#04x}, "
505 "STATUS_INPUT = {:#04x}",
Brandon Wyman321a6152022-03-19 00:11:44 +0000506 shortName, statusWord, statusMFR, statusInput)
Brandon Wyman9e292ee2022-03-10 22:56:23 +0000507 .c_str());
508 }
Brandon Wymanc2906f42021-12-21 20:14:56 +0000509 vinUVFault++;
Brandon Wymanf087f472021-12-22 00:04:27 +0000510 }
Jim Wright4ab86562022-11-18 14:05:46 -0600511 // Remember that this PSU has seen an AC fault
512 acFault = AC_FAULT_LIMIT;
Brandon Wymanf087f472021-12-22 00:04:27 +0000513 }
Jim Wright7f9288c2022-12-08 11:57:04 -0600514 else
Brandon Wyman82affd92021-11-24 19:12:49 +0000515 {
Jim Wright7f9288c2022-12-08 11:57:04 -0600516 if (vinUVFault != 0)
517 {
518 log<level::INFO>(
519 fmt::format("{} VIN_UV fault cleared: STATUS_WORD = {:#06x}, "
520 "STATUS_MFR_SPECIFIC = {:#04x}, "
521 "STATUS_INPUT = {:#04x}",
522 shortName, statusWord, statusMFR, statusInput)
523 .c_str());
524 vinUVFault = 0;
525 }
Jim Wright4ab86562022-11-18 14:05:46 -0600526 // No AC fail, decrement counter
Jim Wright7f9288c2022-12-08 11:57:04 -0600527 if (acFault != 0)
Jim Wright4ab86562022-11-18 14:05:46 -0600528 {
529 --acFault;
530 }
Brandon Wyman82affd92021-11-24 19:12:49 +0000531 }
Brandon Wymanf087f472021-12-22 00:04:27 +0000532}
533
Brandon Wyman3f1242f2020-01-28 13:11:25 -0600534void PowerSupply::analyze()
535{
536 using namespace phosphor::pmbus;
537
B. J. Wyman681b2a32021-04-20 22:31:22 +0000538 if (presenceGPIO)
539 {
540 updatePresenceGPIO();
541 }
542
Brandon Wyman32453e92021-12-15 19:00:14 +0000543 if (present)
Brandon Wyman3f1242f2020-01-28 13:11:25 -0600544 {
545 try
546 {
Brandon Wyman9e292ee2022-03-10 22:56:23 +0000547 statusWordOld = statusWord;
Brandon Wyman32453e92021-12-15 19:00:14 +0000548 statusWord = pmbusIntf->read(STATUS_WORD, Type::Debug,
549 (readFail < LOG_LIMIT));
Brandon Wymanf65c4062020-08-19 13:15:53 -0500550 // Read worked, reset the fail count.
551 readFail = 0;
Brandon Wyman3f1242f2020-01-28 13:11:25 -0600552
553 if (statusWord)
554 {
Brandon Wymanf07bc792021-10-12 19:00:35 +0000555 statusInput = pmbusIntf->read(STATUS_INPUT, Type::Debug);
Jay Meyer10d94052020-11-30 14:41:21 -0600556 statusMFR = pmbusIntf->read(STATUS_MFR, Type::Debug);
Brandon Wyman85c7bf42021-10-19 22:28:48 +0000557 statusCML = pmbusIntf->read(STATUS_CML, Type::Debug);
Brandon Wyman6710ba22021-10-27 17:39:31 +0000558 auto status0Vout = pmbusIntf->insertPageNum(STATUS_VOUT, 0);
559 statusVout = pmbusIntf->read(status0Vout, Type::Debug);
Brandon Wymanb10b3be2021-11-09 22:12:15 +0000560 statusIout = pmbusIntf->read(STATUS_IOUT, Type::Debug);
Brandon Wyman7ee4d7e2021-11-19 20:48:23 +0000561 statusFans12 = pmbusIntf->read(STATUS_FANS_1_2, Type::Debug);
Brandon Wyman96893a42021-11-05 19:56:57 +0000562 statusTemperature =
563 pmbusIntf->read(STATUS_TEMPERATURE, Type::Debug);
Brandon Wyman9ddc6222021-10-28 17:28:01 +0000564
Brandon Wymanc2203432021-12-21 23:09:48 +0000565 analyzeCMLFault();
Brandon Wyman85c7bf42021-10-19 22:28:48 +0000566
Brandon Wymane3b0bb02021-12-21 23:16:48 +0000567 analyzeInputFault();
Brandon Wyman3f1242f2020-01-28 13:11:25 -0600568
Brandon Wymanc2c87132021-12-21 23:22:18 +0000569 analyzeVoutOVFault();
Brandon Wyman6710ba22021-10-27 17:39:31 +0000570
Brandon Wymana00e7302021-12-21 23:28:29 +0000571 analyzeIoutOCFault();
Brandon Wymanb10b3be2021-11-09 22:12:15 +0000572
Brandon Wyman08378782021-12-21 23:48:15 +0000573 analyzeVoutUVFault();
Brandon Wyman2cf46942021-10-28 19:09:16 +0000574
Brandon Wymand5d9a222021-12-21 23:59:05 +0000575 analyzeFanFault();
Brandon Wyman7ee4d7e2021-11-19 20:48:23 +0000576
Brandon Wyman52cb3f22021-12-21 23:02:47 +0000577 analyzeTemperatureFault();
Brandon Wyman96893a42021-11-05 19:56:57 +0000578
Brandon Wyman993b5542021-12-21 22:55:16 +0000579 analyzePgoodFault();
Brandon Wyman2916ea52021-11-06 03:31:18 +0000580
Brandon Wyman6c2ac392021-12-21 22:23:06 +0000581 analyzeMFRFault();
Brandon Wyman3f1242f2020-01-28 13:11:25 -0600582
Brandon Wymanf087f472021-12-22 00:04:27 +0000583 analyzeVinUVFault();
Brandon Wyman3f1242f2020-01-28 13:11:25 -0600584 }
585 else
586 {
Brandon Wyman9e292ee2022-03-10 22:56:23 +0000587 if (statusWord != statusWordOld)
588 {
Brandon Wyman321a6152022-03-19 00:11:44 +0000589 log<level::INFO>(fmt::format("{} STATUS_WORD = {:#06x}",
590 shortName, statusWord)
Brandon Wyman9e292ee2022-03-10 22:56:23 +0000591 .c_str());
592 }
593
Brandon Wymane3f7ad22021-12-21 20:27:45 +0000594 // if INPUT/VIN_UV fault was on, it cleared, trace it.
595 if (inputFault)
596 {
597 log<level::INFO>(
598 fmt::format(
Brandon Wyman321a6152022-03-19 00:11:44 +0000599 "{} INPUT fault cleared: STATUS_WORD = {:#06x}",
600 shortName, statusWord)
Brandon Wymane3f7ad22021-12-21 20:27:45 +0000601 .c_str());
602 }
603
604 if (vinUVFault)
605 {
606 log<level::INFO>(
Brandon Wyman321a6152022-03-19 00:11:44 +0000607 fmt::format("{} VIN_UV cleared: STATUS_WORD = {:#06x}",
608 shortName, statusWord)
Brandon Wymane3f7ad22021-12-21 20:27:45 +0000609 .c_str());
610 }
611
Brandon Wyman06ca4592021-12-06 22:52:23 +0000612 if (pgoodFault > 0)
Brandon Wyman4aecc292021-11-10 22:40:41 +0000613 {
Brandon Wyman321a6152022-03-19 00:11:44 +0000614 log<level::INFO>(
615 fmt::format("{} pgoodFault cleared", shortName)
616 .c_str());
Brandon Wyman4aecc292021-11-10 22:40:41 +0000617 }
Brandon Wymane3f7ad22021-12-21 20:27:45 +0000618
619 clearFaultFlags();
Jim Wright4ab86562022-11-18 14:05:46 -0600620 // No AC fail, decrement counter
Jim Wright7f9288c2022-12-08 11:57:04 -0600621 if (acFault != 0)
Jim Wright4ab86562022-11-18 14:05:46 -0600622 {
623 --acFault;
624 }
Brandon Wyman3f1242f2020-01-28 13:11:25 -0600625 }
Brandon Wyman82affd92021-11-24 19:12:49 +0000626
627 // Save off old inputVoltage value.
628 // Get latest inputVoltage.
629 // If voltage went from below minimum, and now is not, clear faults.
630 // Note: getInputVoltage() has its own try/catch.
631 int inputVoltageOld = inputVoltage;
Brandon Wyman4fc191f2022-03-10 23:07:13 +0000632 double actualInputVoltageOld = actualInputVoltage;
Brandon Wyman82affd92021-11-24 19:12:49 +0000633 getInputVoltage(actualInputVoltage, inputVoltage);
634 if ((inputVoltageOld == in_input::VIN_VOLTAGE_0) &&
635 (inputVoltage != in_input::VIN_VOLTAGE_0))
636 {
637 log<level::INFO>(
638 fmt::format(
Brandon Wyman4fc191f2022-03-10 23:07:13 +0000639 "{} READ_VIN back in range: actualInputVoltageOld = {} "
640 "actualInputVoltage = {}",
641 shortName, actualInputVoltageOld, actualInputVoltage)
Brandon Wyman82affd92021-11-24 19:12:49 +0000642 .c_str());
Brandon Wyman3225a452022-03-18 18:51:49 +0000643 clearVinUVFault();
Brandon Wyman82affd92021-11-24 19:12:49 +0000644 }
Brandon Wyman4fc191f2022-03-10 23:07:13 +0000645 else if (vinUVFault && (inputVoltage != in_input::VIN_VOLTAGE_0))
646 {
647 log<level::INFO>(
648 fmt::format(
649 "{} CLEAR_FAULTS: vinUVFault {} actualInputVoltage {}",
650 shortName, vinUVFault, actualInputVoltage)
651 .c_str());
652 // Do we have a VIN_UV fault latched that can now be cleared
Jim Wright4ab86562022-11-18 14:05:46 -0600653 // due to voltage back in range? Attempt to clear the
654 // fault(s), re-check faults on next call.
Brandon Wyman3225a452022-03-18 18:51:49 +0000655 clearVinUVFault();
Brandon Wyman4fc191f2022-03-10 23:07:13 +0000656 }
Brandon Wymanae35ac52022-05-23 22:33:40 +0000657 else if (std::abs(actualInputVoltageOld - actualInputVoltage) >
658 10.0)
Brandon Wyman4fc191f2022-03-10 23:07:13 +0000659 {
660 log<level::INFO>(
661 fmt::format(
662 "{} actualInputVoltageOld = {} actualInputVoltage = {}",
663 shortName, actualInputVoltageOld, actualInputVoltage)
664 .c_str());
665 }
Matt Spinler0975eaf2022-02-14 15:38:30 -0600666
667 checkAvailability();
Brandon Wymanc3324422022-03-24 20:30:57 +0000668
669 if (inputHistorySupported)
670 {
671 updateHistory();
672 }
Brandon Wyman3f1242f2020-01-28 13:11:25 -0600673 }
Patrick Williamsc1d4de52021-10-06 12:45:57 -0500674 catch (const ReadFailure& e)
Brandon Wyman3f1242f2020-01-28 13:11:25 -0600675 {
Brandon Wyman32453e92021-12-15 19:00:14 +0000676 if (readFail < SIZE_MAX)
677 {
678 readFail++;
679 }
680 if (readFail == LOG_LIMIT)
681 {
682 phosphor::logging::commit<ReadFailure>();
683 }
Brandon Wyman3f1242f2020-01-28 13:11:25 -0600684 }
685 }
686}
687
Brandon Wyman59a35792020-06-04 12:37:40 -0500688void PowerSupply::onOffConfig(uint8_t data)
689{
690 using namespace phosphor::pmbus;
691
692 if (present)
693 {
694 log<level::INFO>("ON_OFF_CONFIG write", entry("DATA=0x%02X", data));
695 try
696 {
697 std::vector<uint8_t> configData{data};
698 pmbusIntf->writeBinary(ON_OFF_CONFIG, configData,
699 Type::HwmonDeviceDebug);
700 }
701 catch (...)
702 {
703 // The underlying code in writeBinary will log a message to the
B. J. Wyman681b2a32021-04-20 22:31:22 +0000704 // journal if the write fails. If the ON_OFF_CONFIG is not setup
705 // as desired, later fault detection and analysis code should
706 // catch any of the fall out. We should not need to terminate
707 // the application if this write fails.
Brandon Wyman59a35792020-06-04 12:37:40 -0500708 }
709 }
710}
711
Brandon Wyman3225a452022-03-18 18:51:49 +0000712void PowerSupply::clearVinUVFault()
713{
714 // Read in1_lcrit_alarm to clear bits 3 and 4 of STATUS_INPUT.
715 // The fault bits in STAUTS_INPUT roll-up to STATUS_WORD. Clearing those
716 // bits in STATUS_INPUT should result in the corresponding STATUS_WORD bits
717 // also clearing.
718 //
719 // Do not care about return value. Should be 1 if active, 0 if not.
720 static_cast<void>(
721 pmbusIntf->read("in1_lcrit_alarm", phosphor::pmbus::Type::Hwmon));
722 vinUVFault = 0;
723}
724
Brandon Wyman3c208462020-05-13 16:25:58 -0500725void PowerSupply::clearFaults()
726{
Brandon Wyman82affd92021-11-24 19:12:49 +0000727 log<level::DEBUG>(
728 fmt::format("clearFaults() inventoryPath: {}", inventoryPath).c_str());
Brandon Wyman5474c912021-02-23 14:39:43 -0600729 faultLogged = false;
Brandon Wyman3c208462020-05-13 16:25:58 -0500730 // The PMBus device driver does not allow for writing CLEAR_FAULTS
731 // directly. However, the pmbus hwmon device driver code will send a
732 // CLEAR_FAULTS after reading from any of the hwmon "files" in sysfs, so
733 // reading in1_input should result in clearing the fault bits in
734 // STATUS_BYTE/STATUS_WORD.
735 // I do not care what the return value is.
Brandon Wyman11151532020-11-10 13:45:57 -0600736 if (present)
Brandon Wyman3c208462020-05-13 16:25:58 -0500737 {
Brandon Wymane3f7ad22021-12-21 20:27:45 +0000738 clearFaultFlags();
Matt Spinler0975eaf2022-02-14 15:38:30 -0600739 checkAvailability();
Brandon Wyman9564e942020-11-10 14:01:42 -0600740 readFail = 0;
Brandon Wyman9564e942020-11-10 14:01:42 -0600741
Brandon Wyman11151532020-11-10 13:45:57 -0600742 try
743 {
Brandon Wyman3225a452022-03-18 18:51:49 +0000744 clearVinUVFault();
Brandon Wyman11151532020-11-10 13:45:57 -0600745 static_cast<void>(
746 pmbusIntf->read("in1_input", phosphor::pmbus::Type::Hwmon));
747 }
Patrick Williamsc1d4de52021-10-06 12:45:57 -0500748 catch (const ReadFailure& e)
Brandon Wyman11151532020-11-10 13:45:57 -0600749 {
750 // Since I do not care what the return value is, I really do not
B. J. Wyman681b2a32021-04-20 22:31:22 +0000751 // care much if it gets a ReadFailure either. However, this
752 // should not prevent the application from continuing to run, so
753 // catching the read failure.
Brandon Wyman11151532020-11-10 13:45:57 -0600754 }
Brandon Wyman3c208462020-05-13 16:25:58 -0500755 }
756}
757
Patrick Williams7354ce62022-07-22 19:26:56 -0500758void PowerSupply::inventoryChanged(sdbusplus::message_t& msg)
Brandon Wymanaed1f752019-11-25 18:10:52 -0600759{
760 std::string msgSensor;
Patrick Williamsabe49412020-05-13 17:59:47 -0500761 std::map<std::string, std::variant<uint32_t, bool>> msgData;
Brandon Wymanaed1f752019-11-25 18:10:52 -0600762 msg.read(msgSensor, msgData);
763
764 // Check if it was the Present property that changed.
765 auto valPropMap = msgData.find(PRESENT_PROP);
766 if (valPropMap != msgData.end())
767 {
768 if (std::get<bool>(valPropMap->second))
769 {
770 present = true;
B. J. Wyman681b2a32021-04-20 22:31:22 +0000771 // TODO: Immediately trying to read or write the "files" causes
772 // read or write failures.
Brandon Wyman1d7a7df2020-03-26 10:14:05 -0500773 using namespace std::chrono_literals;
774 std::this_thread::sleep_for(20ms);
Brandon Wyman9564e942020-11-10 14:01:42 -0600775 pmbusIntf->findHwmonDir();
Brandon Wyman59a35792020-06-04 12:37:40 -0500776 onOffConfig(phosphor::pmbus::ON_OFF_CONFIG_CONTROL_PIN_ONLY);
Brandon Wymanaed1f752019-11-25 18:10:52 -0600777 clearFaults();
Brandon Wyman1d7a7df2020-03-26 10:14:05 -0500778 updateInventory();
Brandon Wymanaed1f752019-11-25 18:10:52 -0600779 }
780 else
781 {
782 present = false;
783
784 // Clear out the now outdated inventory properties
785 updateInventory();
786 }
Matt Spinler0975eaf2022-02-14 15:38:30 -0600787 checkAvailability();
Brandon Wymanaed1f752019-11-25 18:10:52 -0600788 }
789}
790
Patrick Williams7354ce62022-07-22 19:26:56 -0500791void PowerSupply::inventoryAdded(sdbusplus::message_t& msg)
Brandon Wyman9a507db2021-02-25 16:15:22 -0600792{
793 sdbusplus::message::object_path path;
794 msg.read(path);
795 // Make sure the signal is for the PSU inventory path
796 if (path == inventoryPath)
797 {
798 std::map<std::string, std::map<std::string, std::variant<bool>>>
799 interfaces;
800 // Get map of interfaces and their properties
801 msg.read(interfaces);
802
803 auto properties = interfaces.find(INVENTORY_IFACE);
804 if (properties != interfaces.end())
805 {
806 auto property = properties->second.find(PRESENT_PROP);
807 if (property != properties->second.end())
808 {
809 present = std::get<bool>(property->second);
810
811 log<level::INFO>(fmt::format("Power Supply {} Present {}",
812 inventoryPath, present)
813 .c_str());
814
815 updateInventory();
Matt Spinler0975eaf2022-02-14 15:38:30 -0600816 checkAvailability();
Brandon Wyman9a507db2021-02-25 16:15:22 -0600817 }
818 }
819 }
820}
821
Brandon Wyman8393f462022-06-28 16:06:46 +0000822auto PowerSupply::readVPDValue(const std::string& vpdName,
823 const phosphor::pmbus::Type& type,
824 const std::size_t& vpdSize)
825{
826 std::string vpdValue;
Brandon Wyman056935c2022-06-24 23:05:09 +0000827 const std::regex illegalVPDRegex =
828 std::regex("[^[:alnum:]]", std::regex::basic);
Brandon Wyman8393f462022-06-28 16:06:46 +0000829
830 try
831 {
832 vpdValue = pmbusIntf->readString(vpdName, type);
833 }
834 catch (const ReadFailure& e)
835 {
836 // Ignore the read failure, let pmbus code indicate failure,
837 // path...
838 // TODO - ibm918
839 // https://github.com/openbmc/docs/blob/master/designs/vpd-collection.md
840 // The BMC must log errors if any of the VPD cannot be properly
841 // parsed or fails ECC checks.
842 }
843
844 if (vpdValue.size() != vpdSize)
845 {
846 log<level::INFO>(fmt::format("{} {} resize needed. size: {}", shortName,
847 vpdName, vpdValue.size())
848 .c_str());
849 vpdValue.resize(vpdSize, ' ');
850 }
851
Brandon Wyman056935c2022-06-24 23:05:09 +0000852 // Replace any illegal values with space(s).
853 std::regex_replace(vpdValue.begin(), vpdValue.begin(), vpdValue.end(),
854 illegalVPDRegex, " ");
855
Brandon Wyman8393f462022-06-28 16:06:46 +0000856 return vpdValue;
857}
858
Brandon Wyman1d7a7df2020-03-26 10:14:05 -0500859void PowerSupply::updateInventory()
860{
861 using namespace phosphor::pmbus;
862
Chanh Nguyenc12c53b2021-04-06 17:24:47 +0700863#if IBM_VPD
Brandon Wyman1d7a7df2020-03-26 10:14:05 -0500864 std::string pn;
865 std::string fn;
866 std::string header;
867 std::string sn;
Brandon Wyman8393f462022-06-28 16:06:46 +0000868 // The IBM power supply splits the full serial number into two parts.
869 // Each part is 6 bytes long, which should match up with SN_KW_SIZE.
870 const auto HEADER_SIZE = 6;
871 const auto SERIAL_SIZE = 6;
872 // The IBM PSU firmware version size is a bit complicated. It was originally
873 // 1-byte, per command. It was later expanded to 2-bytes per command, then
874 // up to 8-bytes per command. The device driver only reads up to 2 bytes per
875 // command, but combines all three of the 2-byte reads, or all 4 of the
876 // 1-byte reads into one string. So, the maximum size expected is 6 bytes.
Shawn McCarney983c9892022-10-12 10:49:47 -0500877 // However, it is formatted by the driver as a hex string with two ASCII
878 // characters per byte. So the maximum ASCII string size is 12.
879 const auto VERSION_SIZE = 12;
Brandon Wyman8393f462022-06-28 16:06:46 +0000880
Brandon Wyman1d7a7df2020-03-26 10:14:05 -0500881 using PropertyMap =
George Liu070c1bc2020-10-12 11:28:01 +0800882 std::map<std::string,
883 std::variant<std::string, std::vector<uint8_t>, bool>>;
Brandon Wyman1d7a7df2020-03-26 10:14:05 -0500884 PropertyMap assetProps;
George Liu070c1bc2020-10-12 11:28:01 +0800885 PropertyMap operProps;
Brandon Wyman1d7a7df2020-03-26 10:14:05 -0500886 PropertyMap versionProps;
887 PropertyMap ipzvpdDINFProps;
888 PropertyMap ipzvpdVINIProps;
889 using InterfaceMap = std::map<std::string, PropertyMap>;
890 InterfaceMap interfaces;
891 using ObjectMap = std::map<sdbusplus::message::object_path, InterfaceMap>;
892 ObjectMap object;
893#endif
B. J. Wyman681b2a32021-04-20 22:31:22 +0000894 log<level::DEBUG>(
895 fmt::format("updateInventory() inventoryPath: {}", inventoryPath)
896 .c_str());
Brandon Wyman1d7a7df2020-03-26 10:14:05 -0500897
898 if (present)
899 {
900 // TODO: non-IBM inventory updates?
901
Chanh Nguyenc12c53b2021-04-06 17:24:47 +0700902#if IBM_VPD
Brandon Wyman8393f462022-06-28 16:06:46 +0000903 modelName = readVPDValue(CCIN, Type::HwmonDeviceDebug, CC_KW_SIZE);
904 assetProps.emplace(MODEL_PROP, modelName);
Brandon Wyman1d7a7df2020-03-26 10:14:05 -0500905
Brandon Wyman8393f462022-06-28 16:06:46 +0000906 pn = readVPDValue(PART_NUMBER, Type::HwmonDeviceDebug, PN_KW_SIZE);
907 assetProps.emplace(PN_PROP, pn);
Brandon Wyman1d7a7df2020-03-26 10:14:05 -0500908
Brandon Wyman8393f462022-06-28 16:06:46 +0000909 fn = readVPDValue(FRU_NUMBER, Type::HwmonDeviceDebug, FN_KW_SIZE);
910 assetProps.emplace(SPARE_PN_PROP, fn);
Brandon Wyman1d7a7df2020-03-26 10:14:05 -0500911
Brandon Wyman8393f462022-06-28 16:06:46 +0000912 header =
913 readVPDValue(SERIAL_HEADER, Type::HwmonDeviceDebug, HEADER_SIZE);
914 sn = readVPDValue(SERIAL_NUMBER, Type::HwmonDeviceDebug, SERIAL_SIZE);
915 assetProps.emplace(SN_PROP, header + sn);
Brandon Wyman1d7a7df2020-03-26 10:14:05 -0500916
Brandon Wyman8393f462022-06-28 16:06:46 +0000917 fwVersion =
918 readVPDValue(FW_VERSION, Type::HwmonDeviceDebug, VERSION_SIZE);
919 versionProps.emplace(VERSION_PROP, fwVersion);
Brandon Wyman1d7a7df2020-03-26 10:14:05 -0500920
Brandon Wyman8393f462022-06-28 16:06:46 +0000921 ipzvpdVINIProps.emplace(
922 "CC", std::vector<uint8_t>(modelName.begin(), modelName.end()));
Brandon Wyman1d7a7df2020-03-26 10:14:05 -0500923 ipzvpdVINIProps.emplace("PN",
924 std::vector<uint8_t>(pn.begin(), pn.end()));
925 ipzvpdVINIProps.emplace("FN",
926 std::vector<uint8_t>(fn.begin(), fn.end()));
Brandon Wyman33d492f2022-03-23 20:45:17 +0000927 std::string header_sn = header + sn;
Brandon Wyman1d7a7df2020-03-26 10:14:05 -0500928 ipzvpdVINIProps.emplace(
929 "SN", std::vector<uint8_t>(header_sn.begin(), header_sn.end()));
930 std::string description = "IBM PS";
931 ipzvpdVINIProps.emplace(
932 "DR", std::vector<uint8_t>(description.begin(), description.end()));
933
Ben Tynerf8d8c462022-01-27 16:09:45 -0600934 // Populate the VINI Resource Type (RT) keyword
935 ipzvpdVINIProps.emplace("RT", std::vector<uint8_t>{'V', 'I', 'N', 'I'});
936
Brandon Wyman1d7a7df2020-03-26 10:14:05 -0500937 // Update the Resource Identifier (RI) keyword
938 // 2 byte FRC: 0x0003
939 // 2 byte RID: 0x1000, 0x1001...
940 std::uint8_t num = std::stoul(
941 inventoryPath.substr(inventoryPath.size() - 1, 1), nullptr, 0);
942 std::vector<uint8_t> ri{0x00, 0x03, 0x10, num};
943 ipzvpdDINFProps.emplace("RI", ri);
944
945 // Fill in the FRU Label (FL) keyword.
946 std::string fl = "E";
947 fl.push_back(inventoryPath.back());
948 fl.resize(FL_KW_SIZE, ' ');
949 ipzvpdDINFProps.emplace("FL",
950 std::vector<uint8_t>(fl.begin(), fl.end()));
951
Ben Tynerf8d8c462022-01-27 16:09:45 -0600952 // Populate the DINF Resource Type (RT) keyword
953 ipzvpdDINFProps.emplace("RT", std::vector<uint8_t>{'D', 'I', 'N', 'F'});
954
Brandon Wyman1d7a7df2020-03-26 10:14:05 -0500955 interfaces.emplace(ASSET_IFACE, std::move(assetProps));
956 interfaces.emplace(VERSION_IFACE, std::move(versionProps));
957 interfaces.emplace(DINF_IFACE, std::move(ipzvpdDINFProps));
958 interfaces.emplace(VINI_IFACE, std::move(ipzvpdVINIProps));
959
George Liu070c1bc2020-10-12 11:28:01 +0800960 // Update the Functional
961 operProps.emplace(FUNCTIONAL_PROP, present);
962 interfaces.emplace(OPERATIONAL_STATE_IFACE, std::move(operProps));
963
Brandon Wyman1d7a7df2020-03-26 10:14:05 -0500964 auto path = inventoryPath.substr(strlen(INVENTORY_OBJ_PATH));
965 object.emplace(path, std::move(interfaces));
966
967 try
968 {
969 auto service =
970 util::getService(INVENTORY_OBJ_PATH, INVENTORY_MGR_IFACE, bus);
971
972 if (service.empty())
973 {
974 log<level::ERR>("Unable to get inventory manager service");
975 return;
976 }
977
978 auto method =
979 bus.new_method_call(service.c_str(), INVENTORY_OBJ_PATH,
980 INVENTORY_MGR_IFACE, "Notify");
981
982 method.append(std::move(object));
983
984 auto reply = bus.call(method);
985 }
Patrick Williamsc1d4de52021-10-06 12:45:57 -0500986 catch (const std::exception& e)
Brandon Wyman1d7a7df2020-03-26 10:14:05 -0500987 {
Jay Meyer6a3fd2c2020-08-25 16:37:16 -0500988 log<level::ERR>(
989 std::string(e.what() + std::string(" PATH=") + inventoryPath)
990 .c_str());
Brandon Wyman1d7a7df2020-03-26 10:14:05 -0500991 }
992#endif
993 }
994}
995
Brandon Wymanae35ac52022-05-23 22:33:40 +0000996auto PowerSupply::getMaxPowerOut() const
997{
998 using namespace phosphor::pmbus;
999
1000 auto maxPowerOut = 0;
1001
1002 if (present)
1003 {
1004 try
1005 {
1006 // Read max_power_out, should be direct format
1007 auto maxPowerOutStr =
1008 pmbusIntf->readString(MFR_POUT_MAX, Type::HwmonDeviceDebug);
1009 log<level::INFO>(fmt::format("{} MFR_POUT_MAX read {}", shortName,
1010 maxPowerOutStr)
1011 .c_str());
1012 maxPowerOut = std::stod(maxPowerOutStr);
1013 }
1014 catch (const std::exception& e)
1015 {
1016 log<level::ERR>(fmt::format("{} MFR_POUT_MAX read error: {}",
1017 shortName, e.what())
1018 .c_str());
1019 }
1020 }
1021
1022 return maxPowerOut;
1023}
1024
Brandon Wymanc3324422022-03-24 20:30:57 +00001025void PowerSupply::setupInputHistory()
1026{
1027 if (bindPath.string().find("ibm-cffps") != std::string::npos)
1028 {
Brandon Wymanae35ac52022-05-23 22:33:40 +00001029 auto maxPowerOut = getMaxPowerOut();
1030
1031 if (maxPowerOut != phosphor::pmbus::IBM_CFFPS_1400W)
Brandon Wymanc3324422022-03-24 20:30:57 +00001032 {
Brandon Wymanae35ac52022-05-23 22:33:40 +00001033 // Do not enable input history for power supplies that are missing
1034 if (present)
1035 {
1036 inputHistorySupported = true;
1037 log<level::INFO>(
1038 fmt::format("{} INPUT_HISTORY enabled", shortName).c_str());
1039
1040 std::string name{fmt::format("{}_input_power", shortName)};
1041
1042 historyObjectPath =
1043 std::string{INPUT_HISTORY_SENSOR_ROOT} + '/' + name;
1044
1045 // If the power supply was present, we created the
1046 // recordManager. If it then went missing, the recordManager is
1047 // still there. If it then is reinserted, we should be able to
1048 // use the recordManager that was allocated when it was
1049 // initially present.
1050 if (!recordManager)
1051 {
1052 recordManager = std::make_unique<history::RecordManager>(
1053 INPUT_HISTORY_MAX_RECORDS);
1054 }
1055
1056 if (!average)
1057 {
1058 auto avgPath =
1059 historyObjectPath + '/' + history::Average::name;
1060 average = std::make_unique<history::Average>(bus, avgPath);
1061 log<level::DEBUG>(
1062 fmt::format("{} avgPath: {}", shortName, avgPath)
1063 .c_str());
1064 }
1065
1066 if (!maximum)
1067 {
1068 auto maxPath =
1069 historyObjectPath + '/' + history::Maximum::name;
1070 maximum = std::make_unique<history::Maximum>(bus, maxPath);
1071 log<level::DEBUG>(
1072 fmt::format("{} maxPath: {}", shortName, maxPath)
1073 .c_str());
1074 }
1075
1076 log<level::DEBUG>(fmt::format("{} historyObjectPath: {}",
1077 shortName, historyObjectPath)
1078 .c_str());
1079 }
1080 }
1081 else
1082 {
Brandon Wymanc3324422022-03-24 20:30:57 +00001083 log<level::INFO>(
Brandon Wymanae35ac52022-05-23 22:33:40 +00001084 fmt::format("{} INPUT_HISTORY DISABLED. max_power_out: {}",
1085 shortName, maxPowerOut)
1086 .c_str());
1087 inputHistorySupported = false;
Brandon Wymanc3324422022-03-24 20:30:57 +00001088 }
1089 }
1090 else
1091 {
1092 inputHistorySupported = false;
1093 }
1094}
1095
1096void PowerSupply::updateHistory()
1097{
1098 if (!recordManager)
1099 {
1100 // Not enabled
1101 return;
1102 }
1103
1104 if (!present)
1105 {
1106 // Cannot read when not present
1107 return;
1108 }
1109
1110 // Read just the most recent average/max record
1111 auto data =
1112 pmbusIntf->readBinary(INPUT_HISTORY, pmbus::Type::HwmonDeviceDebug,
1113 history::RecordManager::RAW_RECORD_SIZE);
1114
Brandon Wymanae35ac52022-05-23 22:33:40 +00001115 // Update D-Bus only if something changed (a new record ID, or cleared
1116 // out)
Brandon Wymanc3324422022-03-24 20:30:57 +00001117 auto changed = recordManager->add(data);
1118 if (changed)
1119 {
1120 average->values(std::move(recordManager->getAverageRecords()));
1121 maximum->values(std::move(recordManager->getMaximumRecords()));
1122 }
1123}
1124
Adriana Kobylak4175ffb2021-08-02 14:51:05 +00001125void PowerSupply::getInputVoltage(double& actualInputVoltage,
1126 int& inputVoltage) const
1127{
1128 using namespace phosphor::pmbus;
1129
1130 actualInputVoltage = in_input::VIN_VOLTAGE_0;
1131 inputVoltage = in_input::VIN_VOLTAGE_0;
1132
1133 if (present)
1134 {
1135 try
1136 {
1137 // Read input voltage in millivolts
1138 auto inputVoltageStr = pmbusIntf->readString(READ_VIN, Type::Hwmon);
1139
1140 // Convert to volts
1141 actualInputVoltage = std::stod(inputVoltageStr) / 1000;
1142
1143 // Calculate the voltage based on voltage thresholds
1144 if (actualInputVoltage < in_input::VIN_VOLTAGE_MIN)
1145 {
1146 inputVoltage = in_input::VIN_VOLTAGE_0;
1147 }
1148 else if (actualInputVoltage < in_input::VIN_VOLTAGE_110_THRESHOLD)
1149 {
1150 inputVoltage = in_input::VIN_VOLTAGE_110;
1151 }
1152 else
1153 {
1154 inputVoltage = in_input::VIN_VOLTAGE_220;
1155 }
1156 }
1157 catch (const std::exception& e)
1158 {
1159 log<level::ERR>(
Brandon Wyman321a6152022-03-19 00:11:44 +00001160 fmt::format("{} READ_VIN read error: {}", shortName, e.what())
1161 .c_str());
Adriana Kobylak4175ffb2021-08-02 14:51:05 +00001162 }
1163 }
1164}
1165
Matt Spinler0975eaf2022-02-14 15:38:30 -06001166void PowerSupply::checkAvailability()
1167{
1168 bool origAvailability = available;
George Liu9464c422023-02-27 14:30:27 +08001169 bool faulted = isPowerOn() && (hasPSKillFault() || hasIoutOCFault());
1170 available = present && !hasInputFault() && !hasVINUVFault() && !faulted;
Matt Spinler0975eaf2022-02-14 15:38:30 -06001171
1172 if (origAvailability != available)
1173 {
1174 auto invpath = inventoryPath.substr(strlen(INVENTORY_OBJ_PATH));
1175 phosphor::power::psu::setAvailable(bus, invpath, available);
Matt Spinlerca1e9ea2022-02-18 14:03:08 -06001176
1177 // Check if the health rollup needs to change based on the
1178 // new availability value.
1179 phosphor::power::psu::handleChassisHealthRollup(bus, inventoryPath,
1180 !available);
Matt Spinler0975eaf2022-02-14 15:38:30 -06001181 }
1182}
1183
Brandon Wyman3f1242f2020-01-28 13:11:25 -06001184} // namespace phosphor::power::psu