blob: fa4b8e35a2791a32e544bb79c9d8835f85e2d960 [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
Patrick Williams48781ae2023-05-10 07:50:50 -050012#include <chrono> // sleep_for()
Brandon Wyman4fc191f2022-03-10 23:07:13 +000013#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
25using namespace phosphor::logging;
Brandon Wyman3f1242f2020-01-28 13:11:25 -060026using namespace sdbusplus::xyz::openbmc_project::Common::Device::Error;
Brandon Wymanaed1f752019-11-25 18:10:52 -060027
Patrick Williams7354ce62022-07-22 19:26:56 -050028PowerSupply::PowerSupply(sdbusplus::bus_t& bus, const std::string& invpath,
B. J. Wyman681b2a32021-04-20 22:31:22 +000029 std::uint8_t i2cbus, std::uint16_t i2caddr,
Brandon Wymanc3324422022-03-24 20:30:57 +000030 const std::string& driver,
George Liu9464c422023-02-27 14:30:27 +080031 const std::string& gpioLineName,
32 std::function<bool()>&& callback) :
Brandon Wyman510acaa2020-11-05 18:32:04 -060033 bus(bus),
George Liu9464c422023-02-27 14:30:27 +080034 inventoryPath(invpath), bindPath("/sys/bus/i2c/drivers/" + driver),
Faisal Awadab66ae502023-04-01 18:30:32 -050035 isPowerOn(std::move(callback)), driverName(driver)
Brandon Wyman510acaa2020-11-05 18:32:04 -060036{
37 if (inventoryPath.empty())
38 {
39 throw std::invalid_argument{"Invalid empty inventoryPath"};
40 }
41
B. J. Wyman681b2a32021-04-20 22:31:22 +000042 if (gpioLineName.empty())
43 {
44 throw std::invalid_argument{"Invalid empty gpioLineName"};
45 }
Brandon Wyman510acaa2020-11-05 18:32:04 -060046
Brandon Wyman321a6152022-03-19 00:11:44 +000047 shortName = findShortName(inventoryPath);
48
49 log<level::DEBUG>(
50 fmt::format("{} gpioLineName: {}", shortName, gpioLineName).c_str());
B. J. Wyman681b2a32021-04-20 22:31:22 +000051 presenceGPIO = createGPIO(gpioLineName);
Brandon Wyman510acaa2020-11-05 18:32:04 -060052
53 std::ostringstream ss;
54 ss << std::hex << std::setw(4) << std::setfill('0') << i2caddr;
55 std::string addrStr = ss.str();
B. J. Wyman681b2a32021-04-20 22:31:22 +000056 std::string busStr = std::to_string(i2cbus);
57 bindDevice = busStr;
58 bindDevice.append("-");
59 bindDevice.append(addrStr);
60
Brandon Wyman510acaa2020-11-05 18:32:04 -060061 pmbusIntf = phosphor::pmbus::createPMBus(i2cbus, addrStr);
62
63 // Get the current state of the Present property.
B. J. Wyman681b2a32021-04-20 22:31:22 +000064 try
65 {
66 updatePresenceGPIO();
67 }
68 catch (...)
69 {
70 // If the above attempt to use the GPIO failed, it likely means that the
71 // GPIOs are in use by the kernel, meaning it is using gpio-keys.
72 // So, I should rely on phosphor-gpio-presence to update D-Bus, and
73 // work that way for power supply presence.
74 presenceGPIO = nullptr;
75 // Setup the functions to call when the D-Bus inventory path for the
76 // Present property changes.
77 presentMatch = std::make_unique<sdbusplus::bus::match_t>(
78 bus,
79 sdbusplus::bus::match::rules::propertiesChanged(inventoryPath,
80 INVENTORY_IFACE),
81 [this](auto& msg) { this->inventoryChanged(msg); });
82
83 presentAddedMatch = std::make_unique<sdbusplus::bus::match_t>(
84 bus,
85 sdbusplus::bus::match::rules::interfacesAdded() +
86 sdbusplus::bus::match::rules::argNpath(0, inventoryPath),
87 [this](auto& msg) { this->inventoryAdded(msg); });
88
89 updatePresence();
90 updateInventory();
Matt Spinler592bd272023-08-30 11:00:01 -050091 setupSensors();
B. J. Wyman681b2a32021-04-20 22:31:22 +000092 }
Matt Spinlera068f422023-03-10 13:06:49 -060093
94 setInputVoltageRating();
B. J. Wyman681b2a32021-04-20 22:31:22 +000095}
96
97void PowerSupply::bindOrUnbindDriver(bool present)
98{
99 auto action = (present) ? "bind" : "unbind";
100 auto path = bindPath / action;
101
102 if (present)
103 {
Brandon Wymanb1ee60f2022-03-22 22:37:12 +0000104 std::this_thread::sleep_for(std::chrono::milliseconds(bindDelay));
B. J. Wyman681b2a32021-04-20 22:31:22 +0000105 log<level::INFO>(
106 fmt::format("Binding device driver. path: {} device: {}",
107 path.string(), bindDevice)
108 .c_str());
109 }
110 else
111 {
112 log<level::INFO>(
113 fmt::format("Unbinding device driver. path: {} device: {}",
114 path.string(), bindDevice)
115 .c_str());
116 }
117
118 std::ofstream file;
119
120 file.exceptions(std::ofstream::failbit | std::ofstream::badbit |
121 std::ofstream::eofbit);
122
123 try
124 {
125 file.open(path);
126 file << bindDevice;
127 file.close();
128 }
Patrick Williamsc1d4de52021-10-06 12:45:57 -0500129 catch (const std::exception& e)
B. J. Wyman681b2a32021-04-20 22:31:22 +0000130 {
131 auto err = errno;
132
133 log<level::ERR>(
134 fmt::format("Failed binding or unbinding device. errno={}", err)
135 .c_str());
136 }
Brandon Wyman510acaa2020-11-05 18:32:04 -0600137}
138
Brandon Wymanaed1f752019-11-25 18:10:52 -0600139void PowerSupply::updatePresence()
140{
141 try
142 {
Brandon Wyman3f1242f2020-01-28 13:11:25 -0600143 present = getPresence(bus, inventoryPath);
Brandon Wymanaed1f752019-11-25 18:10:52 -0600144 }
Patrick Williams7354ce62022-07-22 19:26:56 -0500145 catch (const sdbusplus::exception_t& e)
Brandon Wymanaed1f752019-11-25 18:10:52 -0600146 {
147 // Relying on property change or interface added to retry.
148 // Log an informational trace to the journal.
Brandon Wymandf13c3a2020-12-15 14:25:22 -0600149 log<level::INFO>(
150 fmt::format("D-Bus property {} access failure exception",
151 inventoryPath)
152 .c_str());
Brandon Wymanaed1f752019-11-25 18:10:52 -0600153 }
154}
155
B. J. Wyman681b2a32021-04-20 22:31:22 +0000156void PowerSupply::updatePresenceGPIO()
157{
158 bool presentOld = present;
159
160 try
161 {
162 if (presenceGPIO->read() > 0)
163 {
164 present = true;
165 }
166 else
167 {
168 present = false;
169 }
170 }
Patrick Williamsc1d4de52021-10-06 12:45:57 -0500171 catch (const std::exception& e)
B. J. Wyman681b2a32021-04-20 22:31:22 +0000172 {
173 log<level::ERR>(
174 fmt::format("presenceGPIO read fail: {}", e.what()).c_str());
175 throw;
176 }
177
178 if (presentOld != present)
179 {
Brandon Wyman321a6152022-03-19 00:11:44 +0000180 log<level::DEBUG>(fmt::format("{} presentOld: {} present: {}",
181 shortName, presentOld, present)
182 .c_str());
Matt Spinlerca1e9ea2022-02-18 14:03:08 -0600183
184 auto invpath = inventoryPath.substr(strlen(INVENTORY_OBJ_PATH));
Brandon Wyman90d529a2022-03-22 23:02:54 +0000185
186 bindOrUnbindDriver(present);
187 if (present)
188 {
189 // If the power supply was present, then missing, and present again,
190 // the hwmon path may have changed. We will need the correct/updated
191 // path before any reads or writes are attempted.
192 pmbusIntf->findHwmonDir();
193 }
194
Brandon Wyman321a6152022-03-19 00:11:44 +0000195 setPresence(bus, invpath, present, shortName);
Matt Spinler592bd272023-08-30 11:00:01 -0500196 setupSensors();
Matt Spinlerca1e9ea2022-02-18 14:03:08 -0600197 updateInventory();
198
Brandon Wyman90d529a2022-03-22 23:02:54 +0000199 // Need Functional to already be correct before calling this.
Matt Spinlerca1e9ea2022-02-18 14:03:08 -0600200 checkAvailability();
201
B. J. Wyman681b2a32021-04-20 22:31:22 +0000202 if (present)
203 {
B. J. Wyman681b2a32021-04-20 22:31:22 +0000204 onOffConfig(phosphor::pmbus::ON_OFF_CONFIG_CONTROL_PIN_ONLY);
205 clearFaults();
Brandon Wyman18a24d92022-04-19 22:48:34 +0000206 // Indicate that the input history data and timestamps between all
207 // the power supplies that are present in the system need to be
208 // synchronized.
209 syncHistoryRequired = true;
B. J. Wyman681b2a32021-04-20 22:31:22 +0000210 }
Matt Spinler592bd272023-08-30 11:00:01 -0500211 else
212 {
213 setSensorsNotAvailable();
214 }
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{
Faisal Awadab66ae502023-04-01 18:30:32 -0500428 if (bindPath.string().find(IBMCFFPS_DD_NAME) != std::string::npos)
Brandon Wyman39ea02b2021-11-23 23:22:23 +0000429 {
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);
Faisal Awadab66ae502023-04-01 18:30:32 -0500556 if (bindPath.string().find(IBMCFFPS_DD_NAME) !=
557 std::string::npos)
558 {
559 statusMFR = pmbusIntf->read(STATUS_MFR, Type::Debug);
560 }
Brandon Wyman85c7bf42021-10-19 22:28:48 +0000561 statusCML = pmbusIntf->read(STATUS_CML, Type::Debug);
Brandon Wyman6710ba22021-10-27 17:39:31 +0000562 auto status0Vout = pmbusIntf->insertPageNum(STATUS_VOUT, 0);
563 statusVout = pmbusIntf->read(status0Vout, Type::Debug);
Brandon Wymanb10b3be2021-11-09 22:12:15 +0000564 statusIout = pmbusIntf->read(STATUS_IOUT, Type::Debug);
Brandon Wyman7ee4d7e2021-11-19 20:48:23 +0000565 statusFans12 = pmbusIntf->read(STATUS_FANS_1_2, Type::Debug);
Patrick Williams48781ae2023-05-10 07:50:50 -0500566 statusTemperature = pmbusIntf->read(STATUS_TEMPERATURE,
567 Type::Debug);
Brandon Wyman9ddc6222021-10-28 17:28:01 +0000568
Brandon Wymanc2203432021-12-21 23:09:48 +0000569 analyzeCMLFault();
Brandon Wyman85c7bf42021-10-19 22:28:48 +0000570
Brandon Wymane3b0bb02021-12-21 23:16:48 +0000571 analyzeInputFault();
Brandon Wyman3f1242f2020-01-28 13:11:25 -0600572
Brandon Wymanc2c87132021-12-21 23:22:18 +0000573 analyzeVoutOVFault();
Brandon Wyman6710ba22021-10-27 17:39:31 +0000574
Brandon Wymana00e7302021-12-21 23:28:29 +0000575 analyzeIoutOCFault();
Brandon Wymanb10b3be2021-11-09 22:12:15 +0000576
Brandon Wyman08378782021-12-21 23:48:15 +0000577 analyzeVoutUVFault();
Brandon Wyman2cf46942021-10-28 19:09:16 +0000578
Brandon Wymand5d9a222021-12-21 23:59:05 +0000579 analyzeFanFault();
Brandon Wyman7ee4d7e2021-11-19 20:48:23 +0000580
Brandon Wyman52cb3f22021-12-21 23:02:47 +0000581 analyzeTemperatureFault();
Brandon Wyman96893a42021-11-05 19:56:57 +0000582
Brandon Wyman993b5542021-12-21 22:55:16 +0000583 analyzePgoodFault();
Brandon Wyman2916ea52021-11-06 03:31:18 +0000584
Brandon Wyman6c2ac392021-12-21 22:23:06 +0000585 analyzeMFRFault();
Brandon Wyman3f1242f2020-01-28 13:11:25 -0600586
Brandon Wymanf087f472021-12-22 00:04:27 +0000587 analyzeVinUVFault();
Brandon Wyman3f1242f2020-01-28 13:11:25 -0600588 }
589 else
590 {
Brandon Wyman9e292ee2022-03-10 22:56:23 +0000591 if (statusWord != statusWordOld)
592 {
Brandon Wyman321a6152022-03-19 00:11:44 +0000593 log<level::INFO>(fmt::format("{} STATUS_WORD = {:#06x}",
594 shortName, statusWord)
Brandon Wyman9e292ee2022-03-10 22:56:23 +0000595 .c_str());
596 }
597
Brandon Wymane3f7ad22021-12-21 20:27:45 +0000598 // if INPUT/VIN_UV fault was on, it cleared, trace it.
599 if (inputFault)
600 {
601 log<level::INFO>(
602 fmt::format(
Brandon Wyman321a6152022-03-19 00:11:44 +0000603 "{} INPUT fault cleared: STATUS_WORD = {:#06x}",
604 shortName, statusWord)
Brandon Wymane3f7ad22021-12-21 20:27:45 +0000605 .c_str());
606 }
607
608 if (vinUVFault)
609 {
610 log<level::INFO>(
Brandon Wyman321a6152022-03-19 00:11:44 +0000611 fmt::format("{} VIN_UV cleared: STATUS_WORD = {:#06x}",
612 shortName, statusWord)
Brandon Wymane3f7ad22021-12-21 20:27:45 +0000613 .c_str());
614 }
615
Brandon Wyman06ca4592021-12-06 22:52:23 +0000616 if (pgoodFault > 0)
Brandon Wyman4aecc292021-11-10 22:40:41 +0000617 {
Brandon Wyman321a6152022-03-19 00:11:44 +0000618 log<level::INFO>(
619 fmt::format("{} pgoodFault cleared", shortName)
620 .c_str());
Brandon Wyman4aecc292021-11-10 22:40:41 +0000621 }
Brandon Wymane3f7ad22021-12-21 20:27:45 +0000622
623 clearFaultFlags();
Jim Wright4ab86562022-11-18 14:05:46 -0600624 // No AC fail, decrement counter
Jim Wright7f9288c2022-12-08 11:57:04 -0600625 if (acFault != 0)
Jim Wright4ab86562022-11-18 14:05:46 -0600626 {
627 --acFault;
628 }
Brandon Wyman3f1242f2020-01-28 13:11:25 -0600629 }
Brandon Wyman82affd92021-11-24 19:12:49 +0000630
631 // Save off old inputVoltage value.
632 // Get latest inputVoltage.
633 // If voltage went from below minimum, and now is not, clear faults.
634 // Note: getInputVoltage() has its own try/catch.
635 int inputVoltageOld = inputVoltage;
Brandon Wyman4fc191f2022-03-10 23:07:13 +0000636 double actualInputVoltageOld = actualInputVoltage;
Brandon Wyman82affd92021-11-24 19:12:49 +0000637 getInputVoltage(actualInputVoltage, inputVoltage);
638 if ((inputVoltageOld == in_input::VIN_VOLTAGE_0) &&
639 (inputVoltage != in_input::VIN_VOLTAGE_0))
640 {
641 log<level::INFO>(
642 fmt::format(
Brandon Wyman4fc191f2022-03-10 23:07:13 +0000643 "{} READ_VIN back in range: actualInputVoltageOld = {} "
644 "actualInputVoltage = {}",
645 shortName, actualInputVoltageOld, actualInputVoltage)
Brandon Wyman82affd92021-11-24 19:12:49 +0000646 .c_str());
Brandon Wyman3225a452022-03-18 18:51:49 +0000647 clearVinUVFault();
Brandon Wyman82affd92021-11-24 19:12:49 +0000648 }
Brandon Wyman4fc191f2022-03-10 23:07:13 +0000649 else if (vinUVFault && (inputVoltage != in_input::VIN_VOLTAGE_0))
650 {
651 log<level::INFO>(
652 fmt::format(
653 "{} CLEAR_FAULTS: vinUVFault {} actualInputVoltage {}",
654 shortName, vinUVFault, actualInputVoltage)
655 .c_str());
656 // Do we have a VIN_UV fault latched that can now be cleared
Jim Wright4ab86562022-11-18 14:05:46 -0600657 // due to voltage back in range? Attempt to clear the
658 // fault(s), re-check faults on next call.
Brandon Wyman3225a452022-03-18 18:51:49 +0000659 clearVinUVFault();
Brandon Wyman4fc191f2022-03-10 23:07:13 +0000660 }
Brandon Wymanae35ac52022-05-23 22:33:40 +0000661 else if (std::abs(actualInputVoltageOld - actualInputVoltage) >
662 10.0)
Brandon Wyman4fc191f2022-03-10 23:07:13 +0000663 {
664 log<level::INFO>(
665 fmt::format(
666 "{} actualInputVoltageOld = {} actualInputVoltage = {}",
667 shortName, actualInputVoltageOld, actualInputVoltage)
668 .c_str());
669 }
Matt Spinler0975eaf2022-02-14 15:38:30 -0600670
Matt Spinler592bd272023-08-30 11:00:01 -0500671 monitorSensors();
672
Matt Spinler0975eaf2022-02-14 15:38:30 -0600673 checkAvailability();
Brandon Wyman3f1242f2020-01-28 13:11:25 -0600674 }
Patrick Williamsc1d4de52021-10-06 12:45:57 -0500675 catch (const ReadFailure& e)
Brandon Wyman3f1242f2020-01-28 13:11:25 -0600676 {
Brandon Wyman32453e92021-12-15 19:00:14 +0000677 if (readFail < SIZE_MAX)
678 {
679 readFail++;
680 }
681 if (readFail == LOG_LIMIT)
682 {
683 phosphor::logging::commit<ReadFailure>();
684 }
Brandon Wyman3f1242f2020-01-28 13:11:25 -0600685 }
686 }
687}
688
Brandon Wyman59a35792020-06-04 12:37:40 -0500689void PowerSupply::onOffConfig(uint8_t data)
690{
691 using namespace phosphor::pmbus;
692
Faisal Awada9582d9c2023-07-11 09:31:22 -0500693 if (present && driverName != ACBEL_FSG032_DD_NAME)
Brandon Wyman59a35792020-06-04 12:37:40 -0500694 {
695 log<level::INFO>("ON_OFF_CONFIG write", entry("DATA=0x%02X", data));
696 try
697 {
698 std::vector<uint8_t> configData{data};
699 pmbusIntf->writeBinary(ON_OFF_CONFIG, configData,
700 Type::HwmonDeviceDebug);
701 }
702 catch (...)
703 {
704 // The underlying code in writeBinary will log a message to the
B. J. Wyman681b2a32021-04-20 22:31:22 +0000705 // journal if the write fails. If the ON_OFF_CONFIG is not setup
706 // as desired, later fault detection and analysis code should
707 // catch any of the fall out. We should not need to terminate
708 // the application if this write fails.
Brandon Wyman59a35792020-06-04 12:37:40 -0500709 }
710 }
711}
712
Brandon Wyman3225a452022-03-18 18:51:49 +0000713void PowerSupply::clearVinUVFault()
714{
715 // Read in1_lcrit_alarm to clear bits 3 and 4 of STATUS_INPUT.
716 // The fault bits in STAUTS_INPUT roll-up to STATUS_WORD. Clearing those
717 // bits in STATUS_INPUT should result in the corresponding STATUS_WORD bits
718 // also clearing.
719 //
720 // Do not care about return value. Should be 1 if active, 0 if not.
Faisal Awada9582d9c2023-07-11 09:31:22 -0500721 if (driverName != ACBEL_FSG032_DD_NAME)
722 {
723 static_cast<void>(
724 pmbusIntf->read("in1_lcrit_alarm", phosphor::pmbus::Type::Hwmon));
725 }
726 else
727 {
728 static_cast<void>(
729 pmbusIntf->read("curr1_crit_alarm", phosphor::pmbus::Type::Hwmon));
730 }
Brandon Wyman3225a452022-03-18 18:51:49 +0000731 vinUVFault = 0;
732}
733
Brandon Wyman3c208462020-05-13 16:25:58 -0500734void PowerSupply::clearFaults()
735{
Brandon Wyman82affd92021-11-24 19:12:49 +0000736 log<level::DEBUG>(
737 fmt::format("clearFaults() inventoryPath: {}", inventoryPath).c_str());
Brandon Wyman5474c912021-02-23 14:39:43 -0600738 faultLogged = false;
Brandon Wyman3c208462020-05-13 16:25:58 -0500739 // The PMBus device driver does not allow for writing CLEAR_FAULTS
740 // directly. However, the pmbus hwmon device driver code will send a
741 // CLEAR_FAULTS after reading from any of the hwmon "files" in sysfs, so
742 // reading in1_input should result in clearing the fault bits in
743 // STATUS_BYTE/STATUS_WORD.
744 // I do not care what the return value is.
Brandon Wyman11151532020-11-10 13:45:57 -0600745 if (present)
Brandon Wyman3c208462020-05-13 16:25:58 -0500746 {
Brandon Wymane3f7ad22021-12-21 20:27:45 +0000747 clearFaultFlags();
Matt Spinler0975eaf2022-02-14 15:38:30 -0600748 checkAvailability();
Brandon Wyman9564e942020-11-10 14:01:42 -0600749 readFail = 0;
Brandon Wyman9564e942020-11-10 14:01:42 -0600750
Brandon Wyman11151532020-11-10 13:45:57 -0600751 try
752 {
Brandon Wyman3225a452022-03-18 18:51:49 +0000753 clearVinUVFault();
Brandon Wyman11151532020-11-10 13:45:57 -0600754 static_cast<void>(
755 pmbusIntf->read("in1_input", phosphor::pmbus::Type::Hwmon));
756 }
Patrick Williamsc1d4de52021-10-06 12:45:57 -0500757 catch (const ReadFailure& e)
Brandon Wyman11151532020-11-10 13:45:57 -0600758 {
759 // Since I do not care what the return value is, I really do not
B. J. Wyman681b2a32021-04-20 22:31:22 +0000760 // care much if it gets a ReadFailure either. However, this
761 // should not prevent the application from continuing to run, so
762 // catching the read failure.
Brandon Wyman11151532020-11-10 13:45:57 -0600763 }
Brandon Wyman3c208462020-05-13 16:25:58 -0500764 }
765}
766
Patrick Williams7354ce62022-07-22 19:26:56 -0500767void PowerSupply::inventoryChanged(sdbusplus::message_t& msg)
Brandon Wymanaed1f752019-11-25 18:10:52 -0600768{
769 std::string msgSensor;
Patrick Williamsabe49412020-05-13 17:59:47 -0500770 std::map<std::string, std::variant<uint32_t, bool>> msgData;
Brandon Wymanaed1f752019-11-25 18:10:52 -0600771 msg.read(msgSensor, msgData);
772
773 // Check if it was the Present property that changed.
774 auto valPropMap = msgData.find(PRESENT_PROP);
775 if (valPropMap != msgData.end())
776 {
777 if (std::get<bool>(valPropMap->second))
778 {
779 present = true;
B. J. Wyman681b2a32021-04-20 22:31:22 +0000780 // TODO: Immediately trying to read or write the "files" causes
781 // read or write failures.
Brandon Wyman1d7a7df2020-03-26 10:14:05 -0500782 using namespace std::chrono_literals;
783 std::this_thread::sleep_for(20ms);
Brandon Wyman9564e942020-11-10 14:01:42 -0600784 pmbusIntf->findHwmonDir();
Brandon Wyman59a35792020-06-04 12:37:40 -0500785 onOffConfig(phosphor::pmbus::ON_OFF_CONFIG_CONTROL_PIN_ONLY);
Brandon Wymanaed1f752019-11-25 18:10:52 -0600786 clearFaults();
Brandon Wyman1d7a7df2020-03-26 10:14:05 -0500787 updateInventory();
Brandon Wymanaed1f752019-11-25 18:10:52 -0600788 }
789 else
790 {
791 present = false;
792
793 // Clear out the now outdated inventory properties
794 updateInventory();
795 }
Matt Spinler0975eaf2022-02-14 15:38:30 -0600796 checkAvailability();
Brandon Wymanaed1f752019-11-25 18:10:52 -0600797 }
798}
799
Patrick Williams7354ce62022-07-22 19:26:56 -0500800void PowerSupply::inventoryAdded(sdbusplus::message_t& msg)
Brandon Wyman9a507db2021-02-25 16:15:22 -0600801{
802 sdbusplus::message::object_path path;
803 msg.read(path);
804 // Make sure the signal is for the PSU inventory path
805 if (path == inventoryPath)
806 {
807 std::map<std::string, std::map<std::string, std::variant<bool>>>
808 interfaces;
809 // Get map of interfaces and their properties
810 msg.read(interfaces);
811
812 auto properties = interfaces.find(INVENTORY_IFACE);
813 if (properties != interfaces.end())
814 {
815 auto property = properties->second.find(PRESENT_PROP);
816 if (property != properties->second.end())
817 {
818 present = std::get<bool>(property->second);
819
820 log<level::INFO>(fmt::format("Power Supply {} Present {}",
821 inventoryPath, present)
822 .c_str());
823
824 updateInventory();
Matt Spinler0975eaf2022-02-14 15:38:30 -0600825 checkAvailability();
Brandon Wyman9a507db2021-02-25 16:15:22 -0600826 }
827 }
828 }
829}
830
Brandon Wyman8393f462022-06-28 16:06:46 +0000831auto PowerSupply::readVPDValue(const std::string& vpdName,
832 const phosphor::pmbus::Type& type,
833 const std::size_t& vpdSize)
834{
835 std::string vpdValue;
Patrick Williams48781ae2023-05-10 07:50:50 -0500836 const std::regex illegalVPDRegex = std::regex("[^[:alnum:]]",
837 std::regex::basic);
Brandon Wyman8393f462022-06-28 16:06:46 +0000838
839 try
840 {
841 vpdValue = pmbusIntf->readString(vpdName, type);
842 }
843 catch (const ReadFailure& e)
844 {
845 // Ignore the read failure, let pmbus code indicate failure,
846 // path...
847 // TODO - ibm918
848 // https://github.com/openbmc/docs/blob/master/designs/vpd-collection.md
849 // The BMC must log errors if any of the VPD cannot be properly
850 // parsed or fails ECC checks.
851 }
852
853 if (vpdValue.size() != vpdSize)
854 {
855 log<level::INFO>(fmt::format("{} {} resize needed. size: {}", shortName,
856 vpdName, vpdValue.size())
857 .c_str());
858 vpdValue.resize(vpdSize, ' ');
859 }
860
Brandon Wyman056935c2022-06-24 23:05:09 +0000861 // Replace any illegal values with space(s).
862 std::regex_replace(vpdValue.begin(), vpdValue.begin(), vpdValue.end(),
863 illegalVPDRegex, " ");
864
Brandon Wyman8393f462022-06-28 16:06:46 +0000865 return vpdValue;
866}
867
Brandon Wyman1d7a7df2020-03-26 10:14:05 -0500868void PowerSupply::updateInventory()
869{
870 using namespace phosphor::pmbus;
871
Chanh Nguyenc12c53b2021-04-06 17:24:47 +0700872#if IBM_VPD
Brandon Wyman1d7a7df2020-03-26 10:14:05 -0500873 std::string pn;
874 std::string fn;
875 std::string header;
876 std::string sn;
Brandon Wyman8393f462022-06-28 16:06:46 +0000877 // The IBM power supply splits the full serial number into two parts.
878 // Each part is 6 bytes long, which should match up with SN_KW_SIZE.
879 const auto HEADER_SIZE = 6;
880 const auto SERIAL_SIZE = 6;
881 // The IBM PSU firmware version size is a bit complicated. It was originally
882 // 1-byte, per command. It was later expanded to 2-bytes per command, then
883 // up to 8-bytes per command. The device driver only reads up to 2 bytes per
884 // command, but combines all three of the 2-byte reads, or all 4 of the
885 // 1-byte reads into one string. So, the maximum size expected is 6 bytes.
Shawn McCarney983c9892022-10-12 10:49:47 -0500886 // However, it is formatted by the driver as a hex string with two ASCII
887 // characters per byte. So the maximum ASCII string size is 12.
Faisal Awada9582d9c2023-07-11 09:31:22 -0500888 const auto IBMCFFPS_FW_VERSION_SIZE = 12;
889 const auto ACBEL_FSG032_FW_VERSION_SIZE = 6;
Brandon Wyman8393f462022-06-28 16:06:46 +0000890
Brandon Wyman1d7a7df2020-03-26 10:14:05 -0500891 using PropertyMap =
George Liu070c1bc2020-10-12 11:28:01 +0800892 std::map<std::string,
893 std::variant<std::string, std::vector<uint8_t>, bool>>;
Brandon Wyman1d7a7df2020-03-26 10:14:05 -0500894 PropertyMap assetProps;
George Liu070c1bc2020-10-12 11:28:01 +0800895 PropertyMap operProps;
Brandon Wyman1d7a7df2020-03-26 10:14:05 -0500896 PropertyMap versionProps;
897 PropertyMap ipzvpdDINFProps;
898 PropertyMap ipzvpdVINIProps;
899 using InterfaceMap = std::map<std::string, PropertyMap>;
900 InterfaceMap interfaces;
901 using ObjectMap = std::map<sdbusplus::message::object_path, InterfaceMap>;
902 ObjectMap object;
903#endif
B. J. Wyman681b2a32021-04-20 22:31:22 +0000904 log<level::DEBUG>(
905 fmt::format("updateInventory() inventoryPath: {}", inventoryPath)
906 .c_str());
Brandon Wyman1d7a7df2020-03-26 10:14:05 -0500907
908 if (present)
909 {
910 // TODO: non-IBM inventory updates?
911
Chanh Nguyenc12c53b2021-04-06 17:24:47 +0700912#if IBM_VPD
Faisal Awada9582d9c2023-07-11 09:31:22 -0500913 if (driverName == ACBEL_FSG032_DD_NAME)
faisaladed7a02023-05-10 14:59:01 -0500914 {
915 getPsuVpdFromDbus("CC", modelName);
916 getPsuVpdFromDbus("PN", pn);
917 getPsuVpdFromDbus("FN", fn);
918 getPsuVpdFromDbus("SN", sn);
919 assetProps.emplace(SN_PROP, sn);
Faisal Awada9582d9c2023-07-11 09:31:22 -0500920 fwVersion = readVPDValue(FW_VERSION, Type::Debug,
921 ACBEL_FSG032_FW_VERSION_SIZE);
922 versionProps.emplace(VERSION_PROP, fwVersion);
faisaladed7a02023-05-10 14:59:01 -0500923 }
924 else
925 {
926 modelName = readVPDValue(CCIN, Type::HwmonDeviceDebug, CC_KW_SIZE);
927 pn = readVPDValue(PART_NUMBER, Type::Debug, PN_KW_SIZE);
928 fn = readVPDValue(FRU_NUMBER, Type::Debug, FN_KW_SIZE);
929
930 header = readVPDValue(SERIAL_HEADER, Type::Debug, HEADER_SIZE);
931 sn = readVPDValue(SERIAL_NUMBER, Type::Debug, SERIAL_SIZE);
932 assetProps.emplace(SN_PROP, header + sn);
Faisal Awada9582d9c2023-07-11 09:31:22 -0500933 fwVersion = readVPDValue(FW_VERSION, Type::HwmonDeviceDebug,
934 IBMCFFPS_FW_VERSION_SIZE);
935 versionProps.emplace(VERSION_PROP, fwVersion);
faisaladed7a02023-05-10 14:59:01 -0500936 }
937
Brandon Wyman8393f462022-06-28 16:06:46 +0000938 assetProps.emplace(MODEL_PROP, modelName);
Brandon Wyman8393f462022-06-28 16:06:46 +0000939 assetProps.emplace(PN_PROP, pn);
Brandon Wyman8393f462022-06-28 16:06:46 +0000940 assetProps.emplace(SPARE_PN_PROP, fn);
Brandon Wyman1d7a7df2020-03-26 10:14:05 -0500941
Brandon Wyman8393f462022-06-28 16:06:46 +0000942 ipzvpdVINIProps.emplace(
943 "CC", std::vector<uint8_t>(modelName.begin(), modelName.end()));
Brandon Wyman1d7a7df2020-03-26 10:14:05 -0500944 ipzvpdVINIProps.emplace("PN",
945 std::vector<uint8_t>(pn.begin(), pn.end()));
946 ipzvpdVINIProps.emplace("FN",
947 std::vector<uint8_t>(fn.begin(), fn.end()));
Brandon Wyman33d492f2022-03-23 20:45:17 +0000948 std::string header_sn = header + sn;
Brandon Wyman1d7a7df2020-03-26 10:14:05 -0500949 ipzvpdVINIProps.emplace(
950 "SN", std::vector<uint8_t>(header_sn.begin(), header_sn.end()));
951 std::string description = "IBM PS";
952 ipzvpdVINIProps.emplace(
953 "DR", std::vector<uint8_t>(description.begin(), description.end()));
954
Ben Tynerf8d8c462022-01-27 16:09:45 -0600955 // Populate the VINI Resource Type (RT) keyword
956 ipzvpdVINIProps.emplace("RT", std::vector<uint8_t>{'V', 'I', 'N', 'I'});
957
Brandon Wyman1d7a7df2020-03-26 10:14:05 -0500958 // Update the Resource Identifier (RI) keyword
959 // 2 byte FRC: 0x0003
960 // 2 byte RID: 0x1000, 0x1001...
961 std::uint8_t num = std::stoul(
962 inventoryPath.substr(inventoryPath.size() - 1, 1), nullptr, 0);
963 std::vector<uint8_t> ri{0x00, 0x03, 0x10, num};
964 ipzvpdDINFProps.emplace("RI", ri);
965
966 // Fill in the FRU Label (FL) keyword.
967 std::string fl = "E";
968 fl.push_back(inventoryPath.back());
969 fl.resize(FL_KW_SIZE, ' ');
970 ipzvpdDINFProps.emplace("FL",
971 std::vector<uint8_t>(fl.begin(), fl.end()));
972
Ben Tynerf8d8c462022-01-27 16:09:45 -0600973 // Populate the DINF Resource Type (RT) keyword
974 ipzvpdDINFProps.emplace("RT", std::vector<uint8_t>{'D', 'I', 'N', 'F'});
975
Brandon Wyman1d7a7df2020-03-26 10:14:05 -0500976 interfaces.emplace(ASSET_IFACE, std::move(assetProps));
977 interfaces.emplace(VERSION_IFACE, std::move(versionProps));
978 interfaces.emplace(DINF_IFACE, std::move(ipzvpdDINFProps));
979 interfaces.emplace(VINI_IFACE, std::move(ipzvpdVINIProps));
980
George Liu070c1bc2020-10-12 11:28:01 +0800981 // Update the Functional
982 operProps.emplace(FUNCTIONAL_PROP, present);
983 interfaces.emplace(OPERATIONAL_STATE_IFACE, std::move(operProps));
984
Brandon Wyman1d7a7df2020-03-26 10:14:05 -0500985 auto path = inventoryPath.substr(strlen(INVENTORY_OBJ_PATH));
986 object.emplace(path, std::move(interfaces));
987
988 try
989 {
Patrick Williams48781ae2023-05-10 07:50:50 -0500990 auto service = util::getService(INVENTORY_OBJ_PATH,
991 INVENTORY_MGR_IFACE, bus);
Brandon Wyman1d7a7df2020-03-26 10:14:05 -0500992
993 if (service.empty())
994 {
995 log<level::ERR>("Unable to get inventory manager service");
996 return;
997 }
998
Patrick Williams48781ae2023-05-10 07:50:50 -0500999 auto method = bus.new_method_call(service.c_str(),
1000 INVENTORY_OBJ_PATH,
1001 INVENTORY_MGR_IFACE, "Notify");
Brandon Wyman1d7a7df2020-03-26 10:14:05 -05001002
1003 method.append(std::move(object));
1004
1005 auto reply = bus.call(method);
1006 }
Patrick Williamsc1d4de52021-10-06 12:45:57 -05001007 catch (const std::exception& e)
Brandon Wyman1d7a7df2020-03-26 10:14:05 -05001008 {
Jay Meyer6a3fd2c2020-08-25 16:37:16 -05001009 log<level::ERR>(
1010 std::string(e.what() + std::string(" PATH=") + inventoryPath)
1011 .c_str());
Brandon Wyman1d7a7df2020-03-26 10:14:05 -05001012 }
1013#endif
1014 }
1015}
1016
Brandon Wymanae35ac52022-05-23 22:33:40 +00001017auto PowerSupply::getMaxPowerOut() const
1018{
1019 using namespace phosphor::pmbus;
1020
1021 auto maxPowerOut = 0;
1022
1023 if (present)
1024 {
1025 try
1026 {
1027 // Read max_power_out, should be direct format
Patrick Williams48781ae2023-05-10 07:50:50 -05001028 auto maxPowerOutStr = pmbusIntf->readString(MFR_POUT_MAX,
1029 Type::HwmonDeviceDebug);
Brandon Wymanae35ac52022-05-23 22:33:40 +00001030 log<level::INFO>(fmt::format("{} MFR_POUT_MAX read {}", shortName,
1031 maxPowerOutStr)
1032 .c_str());
1033 maxPowerOut = std::stod(maxPowerOutStr);
1034 }
1035 catch (const std::exception& e)
1036 {
1037 log<level::ERR>(fmt::format("{} MFR_POUT_MAX read error: {}",
1038 shortName, e.what())
1039 .c_str());
1040 }
1041 }
1042
1043 return maxPowerOut;
1044}
1045
Matt Spinler592bd272023-08-30 11:00:01 -05001046void PowerSupply::setupSensors()
1047{
1048 setupInputPowerPeakSensor();
1049}
1050
1051void PowerSupply::setupInputPowerPeakSensor()
1052{
1053 if (peakInputPowerSensor || !present ||
1054 (bindPath.string().find(IBMCFFPS_DD_NAME) == std::string::npos))
1055 {
1056 return;
1057 }
1058
1059 // This PSU has problems with the input_history command
1060 if (getMaxPowerOut() == phosphor::pmbus::IBM_CFFPS_1400W)
1061 {
1062 return;
1063 }
1064
1065 auto sensorPath =
1066 fmt::format("/xyz/openbmc_project/sensors/power/ps{}_input_power_peak",
1067 shortName.back());
1068
1069 peakInputPowerSensor = std::make_unique<PowerSensorObject>(
1070 bus, sensorPath.c_str(), PowerSensorObject::action::defer_emit);
1071
1072 // The others can remain at the defaults.
1073 peakInputPowerSensor->functional(true, true);
1074 peakInputPowerSensor->available(true, true);
1075 peakInputPowerSensor->value(0, true);
1076 peakInputPowerSensor->unit(
1077 sdbusplus::xyz::openbmc_project::Sensor::server::Value::Unit::Watts,
1078 true);
1079
1080 auto associations = getSensorAssociations();
1081 peakInputPowerSensor->associations(associations, true);
1082
1083 peakInputPowerSensor->emit_object_added();
1084}
1085
1086void PowerSupply::setSensorsNotAvailable()
1087{
1088 if (peakInputPowerSensor)
1089 {
1090 peakInputPowerSensor->value(std::numeric_limits<double>::quiet_NaN());
1091 peakInputPowerSensor->available(false);
1092 }
1093}
1094
Matt Spinler592bd272023-08-30 11:00:01 -05001095void PowerSupply::monitorSensors()
1096{
1097 monitorPeakInputPowerSensor();
1098}
1099
1100void PowerSupply::monitorPeakInputPowerSensor()
1101{
1102 if (!peakInputPowerSensor)
1103 {
1104 return;
1105 }
1106
1107 constexpr size_t recordSize = 5;
1108 std::vector<uint8_t> data;
1109
1110 // Get the peak input power with input history command.
1111 // New data only shows up every 30s, but just try to read it every 1s
1112 // anyway so we always have the most up to date value.
1113 try
1114 {
1115 data = pmbusIntf->readBinary(INPUT_HISTORY,
1116 pmbus::Type::HwmonDeviceDebug, recordSize);
1117 }
1118 catch (const ReadFailure& e)
1119 {
1120 peakInputPowerSensor->value(std::numeric_limits<double>::quiet_NaN());
1121 peakInputPowerSensor->functional(false);
1122 throw;
1123 }
1124
1125 if (data.size() != recordSize)
1126 {
1127 log<level::DEBUG>(
1128 fmt::format("Input history command returned {} bytes instead of 5",
1129 data.size())
1130 .c_str());
1131 peakInputPowerSensor->value(std::numeric_limits<double>::quiet_NaN());
1132 peakInputPowerSensor->functional(false);
1133 return;
1134 }
1135
1136 // The format is SSAAAAPPPP:
1137 // SS = packet sequence number
1138 // AAAA = average power (linear format, little endian)
1139 // PPPP = peak power (linear format, little endian)
1140 auto peak = static_cast<uint16_t>(data[4]) << 8 | data[3];
1141 auto peakPower = linearToInteger(peak);
1142
1143 peakInputPowerSensor->value(peakPower);
1144 peakInputPowerSensor->functional(true);
1145 peakInputPowerSensor->available(true);
1146}
1147
Adriana Kobylak4175ffb2021-08-02 14:51:05 +00001148void PowerSupply::getInputVoltage(double& actualInputVoltage,
1149 int& inputVoltage) const
1150{
1151 using namespace phosphor::pmbus;
1152
1153 actualInputVoltage = in_input::VIN_VOLTAGE_0;
1154 inputVoltage = in_input::VIN_VOLTAGE_0;
1155
1156 if (present)
1157 {
1158 try
1159 {
1160 // Read input voltage in millivolts
1161 auto inputVoltageStr = pmbusIntf->readString(READ_VIN, Type::Hwmon);
1162
1163 // Convert to volts
1164 actualInputVoltage = std::stod(inputVoltageStr) / 1000;
1165
1166 // Calculate the voltage based on voltage thresholds
1167 if (actualInputVoltage < in_input::VIN_VOLTAGE_MIN)
1168 {
1169 inputVoltage = in_input::VIN_VOLTAGE_0;
1170 }
1171 else if (actualInputVoltage < in_input::VIN_VOLTAGE_110_THRESHOLD)
1172 {
1173 inputVoltage = in_input::VIN_VOLTAGE_110;
1174 }
1175 else
1176 {
1177 inputVoltage = in_input::VIN_VOLTAGE_220;
1178 }
1179 }
1180 catch (const std::exception& e)
1181 {
1182 log<level::ERR>(
Brandon Wyman321a6152022-03-19 00:11:44 +00001183 fmt::format("{} READ_VIN read error: {}", shortName, e.what())
1184 .c_str());
Adriana Kobylak4175ffb2021-08-02 14:51:05 +00001185 }
1186 }
1187}
1188
Matt Spinler0975eaf2022-02-14 15:38:30 -06001189void PowerSupply::checkAvailability()
1190{
1191 bool origAvailability = available;
George Liu9464c422023-02-27 14:30:27 +08001192 bool faulted = isPowerOn() && (hasPSKillFault() || hasIoutOCFault());
1193 available = present && !hasInputFault() && !hasVINUVFault() && !faulted;
Matt Spinler0975eaf2022-02-14 15:38:30 -06001194
1195 if (origAvailability != available)
1196 {
1197 auto invpath = inventoryPath.substr(strlen(INVENTORY_OBJ_PATH));
1198 phosphor::power::psu::setAvailable(bus, invpath, available);
Matt Spinlerca1e9ea2022-02-18 14:03:08 -06001199
1200 // Check if the health rollup needs to change based on the
1201 // new availability value.
1202 phosphor::power::psu::handleChassisHealthRollup(bus, inventoryPath,
1203 !available);
Matt Spinler0975eaf2022-02-14 15:38:30 -06001204 }
1205}
1206
Matt Spinlera068f422023-03-10 13:06:49 -06001207void PowerSupply::setInputVoltageRating()
1208{
1209 if (!present)
1210 {
Matt Spinler1aaf9f82023-03-22 09:52:22 -05001211 if (inputVoltageRatingIface)
1212 {
1213 inputVoltageRatingIface->value(0);
1214 inputVoltageRatingIface.reset();
1215 }
Matt Spinlera068f422023-03-10 13:06:49 -06001216 return;
1217 }
1218
1219 double inputVoltageValue{};
1220 int inputVoltageRating{};
1221 getInputVoltage(inputVoltageValue, inputVoltageRating);
1222
1223 if (!inputVoltageRatingIface)
1224 {
1225 auto path = fmt::format(
1226 "/xyz/openbmc_project/sensors/voltage/ps{}_input_voltage_rating",
1227 shortName.back());
1228
1229 inputVoltageRatingIface = std::make_unique<SensorObject>(
1230 bus, path.c_str(), SensorObject::action::defer_emit);
1231
1232 // Leave other properties at their defaults
1233 inputVoltageRatingIface->unit(SensorInterface::Unit::Volts, true);
1234 inputVoltageRatingIface->value(static_cast<double>(inputVoltageRating),
1235 true);
1236
1237 inputVoltageRatingIface->emit_object_added();
1238 }
1239 else
1240 {
1241 inputVoltageRatingIface->value(static_cast<double>(inputVoltageRating));
1242 }
1243}
1244
faisaladed7a02023-05-10 14:59:01 -05001245void PowerSupply::getPsuVpdFromDbus(const std::string& keyword,
1246 std::string& vpdStr)
1247{
1248 try
1249 {
1250 std::vector<uint8_t> value;
1251 vpdStr.clear();
1252 util::getProperty(VINI_IFACE, keyword, inventoryPath,
1253 INVENTORY_MGR_IFACE, bus, value);
1254 for (char c : value)
1255 {
1256 vpdStr += c;
1257 }
1258 }
1259 catch (const sdbusplus::exception_t& e)
1260 {
1261 log<level::ERR>(
1262 fmt::format("Failed getProperty error: {}", e.what()).c_str());
1263 }
1264}
Matt Spinler592bd272023-08-30 11:00:01 -05001265
1266double PowerSupply::linearToInteger(uint16_t data)
1267{
1268 // The exponent is the first 5 bits, followed by 11 bits of mantissa.
1269 int8_t exponent = (data & 0xF800) >> 11;
1270 int16_t mantissa = (data & 0x07FF);
1271
1272 // If exponent's MSB on, then it's negative.
1273 // Convert from two's complement.
1274 if (exponent & 0x10)
1275 {
1276 exponent = (~exponent) & 0x1F;
1277 exponent = (exponent + 1) * -1;
1278 }
1279
1280 // If mantissa's MSB on, then it's negative.
1281 // Convert from two's complement.
1282 if (mantissa & 0x400)
1283 {
1284 mantissa = (~mantissa) & 0x07FF;
1285 mantissa = (mantissa + 1) * -1;
1286 }
1287
1288 auto value = static_cast<double>(mantissa) * pow(2, exponent);
1289 return value;
1290}
1291
1292std::vector<AssociationTuple> PowerSupply::getSensorAssociations()
1293{
1294 std::vector<AssociationTuple> associations;
1295
1296 associations.emplace_back("inventory", "sensors", inventoryPath);
1297
1298 auto chassis = getChassis(bus, inventoryPath);
1299 associations.emplace_back("chassis", "all_sensors", std::move(chassis));
1300
1301 return associations;
1302}
1303
Brandon Wyman3f1242f2020-01-28 13:11:25 -06001304} // namespace phosphor::power::psu