blob: 1f65fb26aa4a12c5982af96f46b4cd363a81edf5 [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 Wyman3f1242f2020-01-28 13:11:25 -06008#include <xyz/openbmc_project/Common/Device/error.hpp>
9
Patrick Williams48781ae2023-05-10 07:50:50 -050010#include <chrono> // sleep_for()
Brandon Wyman4fc191f2022-03-10 23:07:13 +000011#include <cmath>
Brandon Wyman1d7a7df2020-03-26 10:14:05 -050012#include <cstdint> // uint8_t...
Shawn McCarney768d2262024-07-09 15:02:59 -050013#include <format>
B. J. Wyman681b2a32021-04-20 22:31:22 +000014#include <fstream>
Brandon Wyman056935c2022-06-24 23:05:09 +000015#include <regex>
B. J. Wyman681b2a32021-04-20 22:31:22 +000016#include <thread> // sleep_for()
Brandon Wyman1d7a7df2020-03-26 10:14:05 -050017
Brandon Wyman3f1242f2020-01-28 13:11:25 -060018namespace phosphor::power::psu
Brandon Wymanaed1f752019-11-25 18:10:52 -060019{
B. J. Wyman681b2a32021-04-20 22:31:22 +000020// Amount of time in milliseconds to delay between power supply going from
21// missing to present before running the bind command(s).
22constexpr auto bindDelay = 1000;
Brandon Wymanaed1f752019-11-25 18:10:52 -060023
24using namespace phosphor::logging;
Brandon Wyman3f1242f2020-01-28 13:11:25 -060025using namespace sdbusplus::xyz::openbmc_project::Common::Device::Error;
Brandon Wymanaed1f752019-11-25 18:10:52 -060026
Patrick Williams7354ce62022-07-22 19:26:56 -050027PowerSupply::PowerSupply(sdbusplus::bus_t& bus, const std::string& invpath,
B. J. Wyman681b2a32021-04-20 22:31:22 +000028 std::uint8_t i2cbus, std::uint16_t i2caddr,
Brandon Wymanc3324422022-03-24 20:30:57 +000029 const std::string& driver,
George Liu9464c422023-02-27 14:30:27 +080030 const std::string& gpioLineName,
31 std::function<bool()>&& callback) :
Brandon Wyman510acaa2020-11-05 18:32:04 -060032 bus(bus),
George Liu9464c422023-02-27 14:30:27 +080033 inventoryPath(invpath), bindPath("/sys/bus/i2c/drivers/" + driver),
Faisal Awadab66ae502023-04-01 18:30:32 -050034 isPowerOn(std::move(callback)), driverName(driver)
Brandon Wyman510acaa2020-11-05 18:32:04 -060035{
36 if (inventoryPath.empty())
37 {
38 throw std::invalid_argument{"Invalid empty inventoryPath"};
39 }
40
B. J. Wyman681b2a32021-04-20 22:31:22 +000041 if (gpioLineName.empty())
42 {
43 throw std::invalid_argument{"Invalid empty gpioLineName"};
44 }
Brandon Wyman510acaa2020-11-05 18:32:04 -060045
Brandon Wyman321a6152022-03-19 00:11:44 +000046 shortName = findShortName(inventoryPath);
47
48 log<level::DEBUG>(
Shawn McCarney768d2262024-07-09 15:02:59 -050049 std::format("{} gpioLineName: {}", shortName, gpioLineName).c_str());
B. J. Wyman681b2a32021-04-20 22:31:22 +000050 presenceGPIO = createGPIO(gpioLineName);
Brandon Wyman510acaa2020-11-05 18:32:04 -060051
52 std::ostringstream ss;
53 ss << std::hex << std::setw(4) << std::setfill('0') << i2caddr;
54 std::string addrStr = ss.str();
B. J. Wyman681b2a32021-04-20 22:31:22 +000055 std::string busStr = std::to_string(i2cbus);
56 bindDevice = busStr;
57 bindDevice.append("-");
58 bindDevice.append(addrStr);
59
Brandon Wyman510acaa2020-11-05 18:32:04 -060060 pmbusIntf = phosphor::pmbus::createPMBus(i2cbus, addrStr);
61
62 // Get the current state of the Present property.
B. J. Wyman681b2a32021-04-20 22:31:22 +000063 try
64 {
65 updatePresenceGPIO();
66 }
67 catch (...)
68 {
69 // If the above attempt to use the GPIO failed, it likely means that the
70 // GPIOs are in use by the kernel, meaning it is using gpio-keys.
71 // So, I should rely on phosphor-gpio-presence to update D-Bus, and
72 // work that way for power supply presence.
73 presenceGPIO = nullptr;
74 // Setup the functions to call when the D-Bus inventory path for the
75 // Present property changes.
76 presentMatch = std::make_unique<sdbusplus::bus::match_t>(
77 bus,
78 sdbusplus::bus::match::rules::propertiesChanged(inventoryPath,
79 INVENTORY_IFACE),
80 [this](auto& msg) { this->inventoryChanged(msg); });
81
82 presentAddedMatch = std::make_unique<sdbusplus::bus::match_t>(
83 bus,
84 sdbusplus::bus::match::rules::interfacesAdded() +
85 sdbusplus::bus::match::rules::argNpath(0, inventoryPath),
86 [this](auto& msg) { this->inventoryAdded(msg); });
87
88 updatePresence();
89 updateInventory();
Matt Spinler592bd272023-08-30 11:00:01 -050090 setupSensors();
B. J. Wyman681b2a32021-04-20 22:31:22 +000091 }
Matt Spinlera068f422023-03-10 13:06:49 -060092
93 setInputVoltageRating();
B. J. Wyman681b2a32021-04-20 22:31:22 +000094}
95
96void PowerSupply::bindOrUnbindDriver(bool present)
97{
Faisal Awadab7131a12023-10-26 19:38:45 -050098 // Symbolic link to the device will exist if the driver is bound.
99 // So exit no action required if both the link and PSU are present
100 // or neither is present.
101 namespace fs = std::filesystem;
102 fs::path path;
B. J. Wyman681b2a32021-04-20 22:31:22 +0000103 auto action = (present) ? "bind" : "unbind";
B. J. Wyman681b2a32021-04-20 22:31:22 +0000104
Faisal Awadab7131a12023-10-26 19:38:45 -0500105 // This case should not happen, if no device driver name return.
106 if (driverName.empty())
107 {
108 log<level::INFO>("No device driver name found");
109 return;
110 }
111 if (bindPath.string().find(driverName) != std::string::npos)
112 {
113 // bindPath has driver name
114 path = bindPath / action;
115 }
116 else
117 {
118 // Add driver name to bindPath
119 path = bindPath / driverName / action;
120 bindPath = bindPath / driverName;
121 }
122
123 if ((std::filesystem::exists(bindPath / bindDevice) && present) ||
124 (!std::filesystem::exists(bindPath / bindDevice) && !present))
125 {
126 return;
127 }
B. J. Wyman681b2a32021-04-20 22:31:22 +0000128 if (present)
129 {
Brandon Wymanb1ee60f2022-03-22 22:37:12 +0000130 std::this_thread::sleep_for(std::chrono::milliseconds(bindDelay));
B. J. Wyman681b2a32021-04-20 22:31:22 +0000131 log<level::INFO>(
Shawn McCarney768d2262024-07-09 15:02:59 -0500132 std::format("Binding device driver. path: {} device: {}",
B. J. Wyman681b2a32021-04-20 22:31:22 +0000133 path.string(), bindDevice)
134 .c_str());
135 }
136 else
137 {
138 log<level::INFO>(
Shawn McCarney768d2262024-07-09 15:02:59 -0500139 std::format("Unbinding device driver. path: {} device: {}",
B. J. Wyman681b2a32021-04-20 22:31:22 +0000140 path.string(), bindDevice)
141 .c_str());
142 }
143
144 std::ofstream file;
145
146 file.exceptions(std::ofstream::failbit | std::ofstream::badbit |
147 std::ofstream::eofbit);
148
149 try
150 {
151 file.open(path);
152 file << bindDevice;
153 file.close();
154 }
Patrick Williamsc1d4de52021-10-06 12:45:57 -0500155 catch (const std::exception& e)
B. J. Wyman681b2a32021-04-20 22:31:22 +0000156 {
157 auto err = errno;
158
159 log<level::ERR>(
Shawn McCarney768d2262024-07-09 15:02:59 -0500160 std::format("Failed binding or unbinding device. errno={}", err)
B. J. Wyman681b2a32021-04-20 22:31:22 +0000161 .c_str());
162 }
Brandon Wyman510acaa2020-11-05 18:32:04 -0600163}
164
Brandon Wymanaed1f752019-11-25 18:10:52 -0600165void PowerSupply::updatePresence()
166{
167 try
168 {
Brandon Wyman3f1242f2020-01-28 13:11:25 -0600169 present = getPresence(bus, inventoryPath);
Brandon Wymanaed1f752019-11-25 18:10:52 -0600170 }
Patrick Williams7354ce62022-07-22 19:26:56 -0500171 catch (const sdbusplus::exception_t& e)
Brandon Wymanaed1f752019-11-25 18:10:52 -0600172 {
173 // Relying on property change or interface added to retry.
174 // Log an informational trace to the journal.
Brandon Wymandf13c3a2020-12-15 14:25:22 -0600175 log<level::INFO>(
Shawn McCarney768d2262024-07-09 15:02:59 -0500176 std::format("D-Bus property {} access failure exception",
Brandon Wymandf13c3a2020-12-15 14:25:22 -0600177 inventoryPath)
178 .c_str());
Brandon Wymanaed1f752019-11-25 18:10:52 -0600179 }
180}
181
B. J. Wyman681b2a32021-04-20 22:31:22 +0000182void PowerSupply::updatePresenceGPIO()
183{
184 bool presentOld = present;
185
186 try
187 {
188 if (presenceGPIO->read() > 0)
189 {
190 present = true;
191 }
192 else
193 {
194 present = false;
195 }
196 }
Patrick Williamsc1d4de52021-10-06 12:45:57 -0500197 catch (const std::exception& e)
B. J. Wyman681b2a32021-04-20 22:31:22 +0000198 {
199 log<level::ERR>(
Shawn McCarney768d2262024-07-09 15:02:59 -0500200 std::format("presenceGPIO read fail: {}", e.what()).c_str());
B. J. Wyman681b2a32021-04-20 22:31:22 +0000201 throw;
202 }
203
204 if (presentOld != present)
205 {
Shawn McCarney768d2262024-07-09 15:02:59 -0500206 log<level::DEBUG>(std::format("{} presentOld: {} present: {}",
Brandon Wyman321a6152022-03-19 00:11:44 +0000207 shortName, presentOld, present)
208 .c_str());
Matt Spinlerca1e9ea2022-02-18 14:03:08 -0600209
210 auto invpath = inventoryPath.substr(strlen(INVENTORY_OBJ_PATH));
Brandon Wyman90d529a2022-03-22 23:02:54 +0000211
212 bindOrUnbindDriver(present);
213 if (present)
214 {
215 // If the power supply was present, then missing, and present again,
216 // the hwmon path may have changed. We will need the correct/updated
217 // path before any reads or writes are attempted.
218 pmbusIntf->findHwmonDir();
219 }
220
Brandon Wyman321a6152022-03-19 00:11:44 +0000221 setPresence(bus, invpath, present, shortName);
Matt Spinler592bd272023-08-30 11:00:01 -0500222 setupSensors();
Matt Spinlerca1e9ea2022-02-18 14:03:08 -0600223 updateInventory();
224
Brandon Wyman90d529a2022-03-22 23:02:54 +0000225 // Need Functional to already be correct before calling this.
Matt Spinlerca1e9ea2022-02-18 14:03:08 -0600226 checkAvailability();
227
B. J. Wyman681b2a32021-04-20 22:31:22 +0000228 if (present)
229 {
B. J. Wyman681b2a32021-04-20 22:31:22 +0000230 onOffConfig(phosphor::pmbus::ON_OFF_CONFIG_CONTROL_PIN_ONLY);
231 clearFaults();
Brandon Wyman18a24d92022-04-19 22:48:34 +0000232 // Indicate that the input history data and timestamps between all
233 // the power supplies that are present in the system need to be
234 // synchronized.
235 syncHistoryRequired = true;
B. J. Wyman681b2a32021-04-20 22:31:22 +0000236 }
Matt Spinler592bd272023-08-30 11:00:01 -0500237 else
238 {
239 setSensorsNotAvailable();
240 }
B. J. Wyman681b2a32021-04-20 22:31:22 +0000241 }
242}
243
Brandon Wymanc2203432021-12-21 23:09:48 +0000244void PowerSupply::analyzeCMLFault()
245{
246 if (statusWord & phosphor::pmbus::status_word::CML_FAULT)
247 {
Brandon Wymanc2906f42021-12-21 20:14:56 +0000248 if (cmlFault < DEGLITCH_LIMIT)
Brandon Wymanc2203432021-12-21 23:09:48 +0000249 {
Brandon Wyman9e292ee2022-03-10 22:56:23 +0000250 if (statusWord != statusWordOld)
251 {
Brandon Wyman321a6152022-03-19 00:11:44 +0000252 log<level::ERR>(
Shawn McCarney768d2262024-07-09 15:02:59 -0500253 std::format("{} CML fault: STATUS_WORD = {:#06x}, "
Brandon Wyman321a6152022-03-19 00:11:44 +0000254 "STATUS_CML = {:#02x}",
255 shortName, statusWord, statusCML)
256 .c_str());
Brandon Wyman9e292ee2022-03-10 22:56:23 +0000257 }
Brandon Wymanc2906f42021-12-21 20:14:56 +0000258 cmlFault++;
259 }
260 }
261 else
262 {
263 cmlFault = 0;
Brandon Wymanc2203432021-12-21 23:09:48 +0000264 }
265}
266
Brandon Wymane3b0bb02021-12-21 23:16:48 +0000267void PowerSupply::analyzeInputFault()
268{
269 if (statusWord & phosphor::pmbus::status_word::INPUT_FAULT_WARN)
270 {
Brandon Wymanc2906f42021-12-21 20:14:56 +0000271 if (inputFault < DEGLITCH_LIMIT)
Brandon Wymane3b0bb02021-12-21 23:16:48 +0000272 {
Brandon Wyman9e292ee2022-03-10 22:56:23 +0000273 if (statusWord != statusWordOld)
274 {
275 log<level::ERR>(
Shawn McCarney768d2262024-07-09 15:02:59 -0500276 std::format("{} INPUT fault: STATUS_WORD = {:#06x}, "
Brandon Wyman9e292ee2022-03-10 22:56:23 +0000277 "STATUS_MFR_SPECIFIC = {:#04x}, "
278 "STATUS_INPUT = {:#04x}",
Brandon Wyman321a6152022-03-19 00:11:44 +0000279 shortName, statusWord, statusMFR, statusInput)
Brandon Wyman9e292ee2022-03-10 22:56:23 +0000280 .c_str());
281 }
Brandon Wymanc2906f42021-12-21 20:14:56 +0000282 inputFault++;
283 }
Brandon Wymane3b0bb02021-12-21 23:16:48 +0000284 }
Brandon Wyman82affd92021-11-24 19:12:49 +0000285
286 // If had INPUT/VIN_UV fault, and now off.
287 // Trace that odd behavior.
288 if (inputFault &&
289 !(statusWord & phosphor::pmbus::status_word::INPUT_FAULT_WARN))
290 {
291 log<level::INFO>(
Shawn McCarney768d2262024-07-09 15:02:59 -0500292 std::format("{} INPUT fault cleared: STATUS_WORD = {:#06x}, "
Brandon Wyman6f939a32022-03-10 18:42:20 +0000293 "STATUS_MFR_SPECIFIC = {:#04x}, "
294 "STATUS_INPUT = {:#04x}",
Brandon Wyman321a6152022-03-19 00:11:44 +0000295 shortName, statusWord, statusMFR, statusInput)
Brandon Wyman82affd92021-11-24 19:12:49 +0000296 .c_str());
Brandon Wymanc2906f42021-12-21 20:14:56 +0000297 inputFault = 0;
Brandon Wyman82affd92021-11-24 19:12:49 +0000298 }
Brandon Wymane3b0bb02021-12-21 23:16:48 +0000299}
300
Brandon Wymanc2c87132021-12-21 23:22:18 +0000301void PowerSupply::analyzeVoutOVFault()
302{
303 if (statusWord & phosphor::pmbus::status_word::VOUT_OV_FAULT)
304 {
Brandon Wymanc2906f42021-12-21 20:14:56 +0000305 if (voutOVFault < DEGLITCH_LIMIT)
Brandon Wymanc2c87132021-12-21 23:22:18 +0000306 {
Brandon Wyman9e292ee2022-03-10 22:56:23 +0000307 if (statusWord != statusWordOld)
308 {
309 log<level::ERR>(
Shawn McCarney768d2262024-07-09 15:02:59 -0500310 std::format(
Brandon Wyman321a6152022-03-19 00:11:44 +0000311 "{} VOUT_OV_FAULT fault: STATUS_WORD = {:#06x}, "
312 "STATUS_MFR_SPECIFIC = {:#04x}, "
313 "STATUS_VOUT = {:#02x}",
314 shortName, statusWord, statusMFR, statusVout)
Brandon Wyman9e292ee2022-03-10 22:56:23 +0000315 .c_str());
316 }
Brandon Wymanc2c87132021-12-21 23:22:18 +0000317
Brandon Wymanc2906f42021-12-21 20:14:56 +0000318 voutOVFault++;
319 }
320 }
321 else
322 {
323 voutOVFault = 0;
Brandon Wymanc2c87132021-12-21 23:22:18 +0000324 }
325}
326
Brandon Wymana00e7302021-12-21 23:28:29 +0000327void PowerSupply::analyzeIoutOCFault()
328{
329 if (statusWord & phosphor::pmbus::status_word::IOUT_OC_FAULT)
330 {
Brandon Wymanc2906f42021-12-21 20:14:56 +0000331 if (ioutOCFault < DEGLITCH_LIMIT)
Brandon Wymana00e7302021-12-21 23:28:29 +0000332 {
Brandon Wyman9e292ee2022-03-10 22:56:23 +0000333 if (statusWord != statusWordOld)
334 {
335 log<level::ERR>(
Shawn McCarney768d2262024-07-09 15:02:59 -0500336 std::format("{} IOUT fault: STATUS_WORD = {:#06x}, "
Brandon Wyman9e292ee2022-03-10 22:56:23 +0000337 "STATUS_MFR_SPECIFIC = {:#04x}, "
338 "STATUS_IOUT = {:#04x}",
Brandon Wyman321a6152022-03-19 00:11:44 +0000339 shortName, statusWord, statusMFR, statusIout)
Brandon Wyman9e292ee2022-03-10 22:56:23 +0000340 .c_str());
341 }
Brandon Wymana00e7302021-12-21 23:28:29 +0000342
Brandon Wymanc2906f42021-12-21 20:14:56 +0000343 ioutOCFault++;
344 }
345 }
346 else
347 {
348 ioutOCFault = 0;
Brandon Wymana00e7302021-12-21 23:28:29 +0000349 }
350}
351
Brandon Wyman08378782021-12-21 23:48:15 +0000352void PowerSupply::analyzeVoutUVFault()
353{
354 if ((statusWord & phosphor::pmbus::status_word::VOUT_FAULT) &&
355 !(statusWord & phosphor::pmbus::status_word::VOUT_OV_FAULT))
356 {
Brandon Wymanc2906f42021-12-21 20:14:56 +0000357 if (voutUVFault < DEGLITCH_LIMIT)
Brandon Wyman08378782021-12-21 23:48:15 +0000358 {
Brandon Wyman9e292ee2022-03-10 22:56:23 +0000359 if (statusWord != statusWordOld)
360 {
361 log<level::ERR>(
Shawn McCarney768d2262024-07-09 15:02:59 -0500362 std::format(
Brandon Wyman321a6152022-03-19 00:11:44 +0000363 "{} VOUT_UV_FAULT fault: STATUS_WORD = {:#06x}, "
364 "STATUS_MFR_SPECIFIC = {:#04x}, "
365 "STATUS_VOUT = {:#04x}",
366 shortName, statusWord, statusMFR, statusVout)
Brandon Wyman9e292ee2022-03-10 22:56:23 +0000367 .c_str());
368 }
Brandon Wymanc2906f42021-12-21 20:14:56 +0000369 voutUVFault++;
370 }
371 }
372 else
373 {
374 voutUVFault = 0;
Brandon Wyman08378782021-12-21 23:48:15 +0000375 }
376}
377
Brandon Wymand5d9a222021-12-21 23:59:05 +0000378void PowerSupply::analyzeFanFault()
379{
380 if (statusWord & phosphor::pmbus::status_word::FAN_FAULT)
381 {
Brandon Wymanc2906f42021-12-21 20:14:56 +0000382 if (fanFault < DEGLITCH_LIMIT)
Brandon Wymand5d9a222021-12-21 23:59:05 +0000383 {
Brandon Wyman9e292ee2022-03-10 22:56:23 +0000384 if (statusWord != statusWordOld)
385 {
Shawn McCarney768d2262024-07-09 15:02:59 -0500386 log<level::ERR>(std::format("{} FANS fault/warning: "
Brandon Wyman9e292ee2022-03-10 22:56:23 +0000387 "STATUS_WORD = {:#06x}, "
388 "STATUS_MFR_SPECIFIC = {:#04x}, "
389 "STATUS_FANS_1_2 = {:#04x}",
Brandon Wyman321a6152022-03-19 00:11:44 +0000390 shortName, statusWord, statusMFR,
391 statusFans12)
Brandon Wyman9e292ee2022-03-10 22:56:23 +0000392 .c_str());
393 }
Brandon Wymanc2906f42021-12-21 20:14:56 +0000394 fanFault++;
395 }
396 }
397 else
398 {
399 fanFault = 0;
Brandon Wymand5d9a222021-12-21 23:59:05 +0000400 }
401}
402
Brandon Wyman52cb3f22021-12-21 23:02:47 +0000403void PowerSupply::analyzeTemperatureFault()
404{
405 if (statusWord & phosphor::pmbus::status_word::TEMPERATURE_FAULT_WARN)
406 {
Brandon Wymanc2906f42021-12-21 20:14:56 +0000407 if (tempFault < DEGLITCH_LIMIT)
Brandon Wyman52cb3f22021-12-21 23:02:47 +0000408 {
Brandon Wyman9e292ee2022-03-10 22:56:23 +0000409 if (statusWord != statusWordOld)
410 {
Shawn McCarney768d2262024-07-09 15:02:59 -0500411 log<level::ERR>(std::format("{} TEMPERATURE fault/warning: "
Brandon Wyman9e292ee2022-03-10 22:56:23 +0000412 "STATUS_WORD = {:#06x}, "
413 "STATUS_MFR_SPECIFIC = {:#04x}, "
414 "STATUS_TEMPERATURE = {:#04x}",
Brandon Wyman321a6152022-03-19 00:11:44 +0000415 shortName, statusWord, statusMFR,
Brandon Wyman9e292ee2022-03-10 22:56:23 +0000416 statusTemperature)
417 .c_str());
418 }
Brandon Wymanc2906f42021-12-21 20:14:56 +0000419 tempFault++;
420 }
421 }
422 else
423 {
424 tempFault = 0;
Brandon Wyman52cb3f22021-12-21 23:02:47 +0000425 }
426}
427
Brandon Wyman993b5542021-12-21 22:55:16 +0000428void PowerSupply::analyzePgoodFault()
429{
430 if ((statusWord & phosphor::pmbus::status_word::POWER_GOOD_NEGATED) ||
431 (statusWord & phosphor::pmbus::status_word::UNIT_IS_OFF))
432 {
Brandon Wyman6d469fd2022-06-15 16:58:21 +0000433 if (pgoodFault < PGOOD_DEGLITCH_LIMIT)
Brandon Wyman993b5542021-12-21 22:55:16 +0000434 {
Brandon Wyman9e292ee2022-03-10 22:56:23 +0000435 if (statusWord != statusWordOld)
436 {
Shawn McCarney768d2262024-07-09 15:02:59 -0500437 log<level::ERR>(std::format("{} PGOOD fault: "
Brandon Wyman9e292ee2022-03-10 22:56:23 +0000438 "STATUS_WORD = {:#06x}, "
439 "STATUS_MFR_SPECIFIC = {:#04x}",
Brandon Wyman321a6152022-03-19 00:11:44 +0000440 shortName, statusWord, statusMFR)
Brandon Wyman9e292ee2022-03-10 22:56:23 +0000441 .c_str());
442 }
Brandon Wyman993b5542021-12-21 22:55:16 +0000443 pgoodFault++;
444 }
445 }
446 else
447 {
448 pgoodFault = 0;
449 }
450}
451
Brandon Wyman39ea02b2021-11-23 23:22:23 +0000452void PowerSupply::determineMFRFault()
453{
Faisal Awadab66ae502023-04-01 18:30:32 -0500454 if (bindPath.string().find(IBMCFFPS_DD_NAME) != std::string::npos)
Brandon Wyman39ea02b2021-11-23 23:22:23 +0000455 {
456 // IBM MFR_SPECIFIC[4] is PS_Kill fault
457 if (statusMFR & 0x10)
458 {
Brandon Wymanc2906f42021-12-21 20:14:56 +0000459 if (psKillFault < DEGLITCH_LIMIT)
460 {
461 psKillFault++;
462 }
463 }
464 else
465 {
466 psKillFault = 0;
Brandon Wyman39ea02b2021-11-23 23:22:23 +0000467 }
468 // IBM MFR_SPECIFIC[6] is 12Vcs fault.
469 if (statusMFR & 0x40)
470 {
Brandon Wymanc2906f42021-12-21 20:14:56 +0000471 if (ps12VcsFault < DEGLITCH_LIMIT)
472 {
473 ps12VcsFault++;
474 }
475 }
476 else
477 {
478 ps12VcsFault = 0;
Brandon Wyman39ea02b2021-11-23 23:22:23 +0000479 }
480 // IBM MFR_SPECIFIC[7] is 12V Current-Share fault.
481 if (statusMFR & 0x80)
482 {
Brandon Wymanc2906f42021-12-21 20:14:56 +0000483 if (psCS12VFault < DEGLITCH_LIMIT)
484 {
485 psCS12VFault++;
486 }
487 }
488 else
489 {
490 psCS12VFault = 0;
Brandon Wyman39ea02b2021-11-23 23:22:23 +0000491 }
492 }
493}
494
Brandon Wyman6c2ac392021-12-21 22:23:06 +0000495void PowerSupply::analyzeMFRFault()
496{
497 if (statusWord & phosphor::pmbus::status_word::MFR_SPECIFIC_FAULT)
498 {
Brandon Wymanc2906f42021-12-21 20:14:56 +0000499 if (mfrFault < DEGLITCH_LIMIT)
Brandon Wyman6c2ac392021-12-21 22:23:06 +0000500 {
Brandon Wyman9e292ee2022-03-10 22:56:23 +0000501 if (statusWord != statusWordOld)
502 {
Shawn McCarney768d2262024-07-09 15:02:59 -0500503 log<level::ERR>(std::format("{} MFR fault: "
Brandon Wyman9e292ee2022-03-10 22:56:23 +0000504 "STATUS_WORD = {:#06x} "
505 "STATUS_MFR_SPECIFIC = {:#04x}",
Brandon Wyman321a6152022-03-19 00:11:44 +0000506 shortName, statusWord, statusMFR)
Brandon Wyman9e292ee2022-03-10 22:56:23 +0000507 .c_str());
508 }
Brandon Wymanc2906f42021-12-21 20:14:56 +0000509 mfrFault++;
Brandon Wyman6c2ac392021-12-21 22:23:06 +0000510 }
511
Brandon Wyman6c2ac392021-12-21 22:23:06 +0000512 determineMFRFault();
513 }
Brandon Wymanc2906f42021-12-21 20:14:56 +0000514 else
515 {
516 mfrFault = 0;
517 }
Brandon Wyman6c2ac392021-12-21 22:23:06 +0000518}
519
Brandon Wymanf087f472021-12-22 00:04:27 +0000520void PowerSupply::analyzeVinUVFault()
521{
522 if (statusWord & phosphor::pmbus::status_word::VIN_UV_FAULT)
523 {
Brandon Wymanc2906f42021-12-21 20:14:56 +0000524 if (vinUVFault < DEGLITCH_LIMIT)
Brandon Wymanf087f472021-12-22 00:04:27 +0000525 {
Brandon Wyman9e292ee2022-03-10 22:56:23 +0000526 if (statusWord != statusWordOld)
527 {
528 log<level::ERR>(
Shawn McCarney768d2262024-07-09 15:02:59 -0500529 std::format("{} VIN_UV fault: STATUS_WORD = {:#06x}, "
Brandon Wyman9e292ee2022-03-10 22:56:23 +0000530 "STATUS_MFR_SPECIFIC = {:#04x}, "
531 "STATUS_INPUT = {:#04x}",
Brandon Wyman321a6152022-03-19 00:11:44 +0000532 shortName, statusWord, statusMFR, statusInput)
Brandon Wyman9e292ee2022-03-10 22:56:23 +0000533 .c_str());
534 }
Brandon Wymanc2906f42021-12-21 20:14:56 +0000535 vinUVFault++;
Brandon Wymanf087f472021-12-22 00:04:27 +0000536 }
Jim Wright4ab86562022-11-18 14:05:46 -0600537 // Remember that this PSU has seen an AC fault
538 acFault = AC_FAULT_LIMIT;
Brandon Wymanf087f472021-12-22 00:04:27 +0000539 }
Jim Wright7f9288c2022-12-08 11:57:04 -0600540 else
Brandon Wyman82affd92021-11-24 19:12:49 +0000541 {
Jim Wright7f9288c2022-12-08 11:57:04 -0600542 if (vinUVFault != 0)
543 {
544 log<level::INFO>(
Shawn McCarney768d2262024-07-09 15:02:59 -0500545 std::format("{} VIN_UV fault cleared: STATUS_WORD = {:#06x}, "
Jim Wright7f9288c2022-12-08 11:57:04 -0600546 "STATUS_MFR_SPECIFIC = {:#04x}, "
547 "STATUS_INPUT = {:#04x}",
548 shortName, statusWord, statusMFR, statusInput)
549 .c_str());
550 vinUVFault = 0;
551 }
Jim Wright4ab86562022-11-18 14:05:46 -0600552 // No AC fail, decrement counter
Jim Wright7f9288c2022-12-08 11:57:04 -0600553 if (acFault != 0)
Jim Wright4ab86562022-11-18 14:05:46 -0600554 {
555 --acFault;
556 }
Brandon Wyman82affd92021-11-24 19:12:49 +0000557 }
Brandon Wymanf087f472021-12-22 00:04:27 +0000558}
559
Brandon Wyman3f1242f2020-01-28 13:11:25 -0600560void PowerSupply::analyze()
561{
562 using namespace phosphor::pmbus;
563
B. J. Wyman681b2a32021-04-20 22:31:22 +0000564 if (presenceGPIO)
565 {
566 updatePresenceGPIO();
567 }
568
Brandon Wyman32453e92021-12-15 19:00:14 +0000569 if (present)
Brandon Wyman3f1242f2020-01-28 13:11:25 -0600570 {
571 try
572 {
Brandon Wyman9e292ee2022-03-10 22:56:23 +0000573 statusWordOld = statusWord;
Brandon Wyman32453e92021-12-15 19:00:14 +0000574 statusWord = pmbusIntf->read(STATUS_WORD, Type::Debug,
575 (readFail < LOG_LIMIT));
Brandon Wymanf65c4062020-08-19 13:15:53 -0500576 // Read worked, reset the fail count.
577 readFail = 0;
Brandon Wyman3f1242f2020-01-28 13:11:25 -0600578
579 if (statusWord)
580 {
Brandon Wymanf07bc792021-10-12 19:00:35 +0000581 statusInput = pmbusIntf->read(STATUS_INPUT, Type::Debug);
Faisal Awadab66ae502023-04-01 18:30:32 -0500582 if (bindPath.string().find(IBMCFFPS_DD_NAME) !=
583 std::string::npos)
584 {
585 statusMFR = pmbusIntf->read(STATUS_MFR, Type::Debug);
586 }
Brandon Wyman85c7bf42021-10-19 22:28:48 +0000587 statusCML = pmbusIntf->read(STATUS_CML, Type::Debug);
Brandon Wyman6710ba22021-10-27 17:39:31 +0000588 auto status0Vout = pmbusIntf->insertPageNum(STATUS_VOUT, 0);
589 statusVout = pmbusIntf->read(status0Vout, Type::Debug);
Brandon Wymanb10b3be2021-11-09 22:12:15 +0000590 statusIout = pmbusIntf->read(STATUS_IOUT, Type::Debug);
Brandon Wyman7ee4d7e2021-11-19 20:48:23 +0000591 statusFans12 = pmbusIntf->read(STATUS_FANS_1_2, Type::Debug);
Patrick Williams48781ae2023-05-10 07:50:50 -0500592 statusTemperature = pmbusIntf->read(STATUS_TEMPERATURE,
593 Type::Debug);
Brandon Wyman9ddc6222021-10-28 17:28:01 +0000594
Brandon Wymanc2203432021-12-21 23:09:48 +0000595 analyzeCMLFault();
Brandon Wyman85c7bf42021-10-19 22:28:48 +0000596
Brandon Wymane3b0bb02021-12-21 23:16:48 +0000597 analyzeInputFault();
Brandon Wyman3f1242f2020-01-28 13:11:25 -0600598
Brandon Wymanc2c87132021-12-21 23:22:18 +0000599 analyzeVoutOVFault();
Brandon Wyman6710ba22021-10-27 17:39:31 +0000600
Brandon Wymana00e7302021-12-21 23:28:29 +0000601 analyzeIoutOCFault();
Brandon Wymanb10b3be2021-11-09 22:12:15 +0000602
Brandon Wyman08378782021-12-21 23:48:15 +0000603 analyzeVoutUVFault();
Brandon Wyman2cf46942021-10-28 19:09:16 +0000604
Brandon Wymand5d9a222021-12-21 23:59:05 +0000605 analyzeFanFault();
Brandon Wyman7ee4d7e2021-11-19 20:48:23 +0000606
Brandon Wyman52cb3f22021-12-21 23:02:47 +0000607 analyzeTemperatureFault();
Brandon Wyman96893a42021-11-05 19:56:57 +0000608
Brandon Wyman993b5542021-12-21 22:55:16 +0000609 analyzePgoodFault();
Brandon Wyman2916ea52021-11-06 03:31:18 +0000610
Brandon Wyman6c2ac392021-12-21 22:23:06 +0000611 analyzeMFRFault();
Brandon Wyman3f1242f2020-01-28 13:11:25 -0600612
Brandon Wymanf087f472021-12-22 00:04:27 +0000613 analyzeVinUVFault();
Brandon Wyman3f1242f2020-01-28 13:11:25 -0600614 }
615 else
616 {
Brandon Wyman9e292ee2022-03-10 22:56:23 +0000617 if (statusWord != statusWordOld)
618 {
Shawn McCarney768d2262024-07-09 15:02:59 -0500619 log<level::INFO>(std::format("{} STATUS_WORD = {:#06x}",
Brandon Wyman321a6152022-03-19 00:11:44 +0000620 shortName, statusWord)
Brandon Wyman9e292ee2022-03-10 22:56:23 +0000621 .c_str());
622 }
623
Brandon Wymane3f7ad22021-12-21 20:27:45 +0000624 // if INPUT/VIN_UV fault was on, it cleared, trace it.
625 if (inputFault)
626 {
627 log<level::INFO>(
Shawn McCarney768d2262024-07-09 15:02:59 -0500628 std::format(
Brandon Wyman321a6152022-03-19 00:11:44 +0000629 "{} INPUT fault cleared: STATUS_WORD = {:#06x}",
630 shortName, statusWord)
Brandon Wymane3f7ad22021-12-21 20:27:45 +0000631 .c_str());
632 }
633
634 if (vinUVFault)
635 {
636 log<level::INFO>(
Shawn McCarney768d2262024-07-09 15:02:59 -0500637 std::format("{} VIN_UV cleared: STATUS_WORD = {:#06x}",
Brandon Wyman321a6152022-03-19 00:11:44 +0000638 shortName, statusWord)
Brandon Wymane3f7ad22021-12-21 20:27:45 +0000639 .c_str());
640 }
641
Brandon Wyman06ca4592021-12-06 22:52:23 +0000642 if (pgoodFault > 0)
Brandon Wyman4aecc292021-11-10 22:40:41 +0000643 {
Brandon Wyman321a6152022-03-19 00:11:44 +0000644 log<level::INFO>(
Shawn McCarney768d2262024-07-09 15:02:59 -0500645 std::format("{} pgoodFault cleared", shortName)
Brandon Wyman321a6152022-03-19 00:11:44 +0000646 .c_str());
Brandon Wyman4aecc292021-11-10 22:40:41 +0000647 }
Brandon Wymane3f7ad22021-12-21 20:27:45 +0000648
649 clearFaultFlags();
Jim Wright4ab86562022-11-18 14:05:46 -0600650 // No AC fail, decrement counter
Jim Wright7f9288c2022-12-08 11:57:04 -0600651 if (acFault != 0)
Jim Wright4ab86562022-11-18 14:05:46 -0600652 {
653 --acFault;
654 }
Brandon Wyman3f1242f2020-01-28 13:11:25 -0600655 }
Brandon Wyman82affd92021-11-24 19:12:49 +0000656
657 // Save off old inputVoltage value.
658 // Get latest inputVoltage.
659 // If voltage went from below minimum, and now is not, clear faults.
660 // Note: getInputVoltage() has its own try/catch.
661 int inputVoltageOld = inputVoltage;
Brandon Wyman4fc191f2022-03-10 23:07:13 +0000662 double actualInputVoltageOld = actualInputVoltage;
Brandon Wyman82affd92021-11-24 19:12:49 +0000663 getInputVoltage(actualInputVoltage, inputVoltage);
664 if ((inputVoltageOld == in_input::VIN_VOLTAGE_0) &&
665 (inputVoltage != in_input::VIN_VOLTAGE_0))
666 {
667 log<level::INFO>(
Shawn McCarney768d2262024-07-09 15:02:59 -0500668 std::format(
Brandon Wyman4fc191f2022-03-10 23:07:13 +0000669 "{} READ_VIN back in range: actualInputVoltageOld = {} "
670 "actualInputVoltage = {}",
671 shortName, actualInputVoltageOld, actualInputVoltage)
Brandon Wyman82affd92021-11-24 19:12:49 +0000672 .c_str());
Brandon Wyman3225a452022-03-18 18:51:49 +0000673 clearVinUVFault();
Brandon Wyman82affd92021-11-24 19:12:49 +0000674 }
Brandon Wyman4fc191f2022-03-10 23:07:13 +0000675 else if (vinUVFault && (inputVoltage != in_input::VIN_VOLTAGE_0))
676 {
677 log<level::INFO>(
Shawn McCarney768d2262024-07-09 15:02:59 -0500678 std::format(
Brandon Wyman4fc191f2022-03-10 23:07:13 +0000679 "{} CLEAR_FAULTS: vinUVFault {} actualInputVoltage {}",
680 shortName, vinUVFault, actualInputVoltage)
681 .c_str());
682 // Do we have a VIN_UV fault latched that can now be cleared
Jim Wright4ab86562022-11-18 14:05:46 -0600683 // due to voltage back in range? Attempt to clear the
684 // fault(s), re-check faults on next call.
Brandon Wyman3225a452022-03-18 18:51:49 +0000685 clearVinUVFault();
Brandon Wyman4fc191f2022-03-10 23:07:13 +0000686 }
Brandon Wymanae35ac52022-05-23 22:33:40 +0000687 else if (std::abs(actualInputVoltageOld - actualInputVoltage) >
688 10.0)
Brandon Wyman4fc191f2022-03-10 23:07:13 +0000689 {
690 log<level::INFO>(
Shawn McCarney768d2262024-07-09 15:02:59 -0500691 std::format(
Brandon Wyman4fc191f2022-03-10 23:07:13 +0000692 "{} actualInputVoltageOld = {} actualInputVoltage = {}",
693 shortName, actualInputVoltageOld, actualInputVoltage)
694 .c_str());
695 }
Matt Spinler0975eaf2022-02-14 15:38:30 -0600696
Matt Spinler592bd272023-08-30 11:00:01 -0500697 monitorSensors();
698
Matt Spinler0975eaf2022-02-14 15:38:30 -0600699 checkAvailability();
Brandon Wyman3f1242f2020-01-28 13:11:25 -0600700 }
Patrick Williamsc1d4de52021-10-06 12:45:57 -0500701 catch (const ReadFailure& e)
Brandon Wyman3f1242f2020-01-28 13:11:25 -0600702 {
Brandon Wyman32453e92021-12-15 19:00:14 +0000703 if (readFail < SIZE_MAX)
704 {
705 readFail++;
706 }
707 if (readFail == LOG_LIMIT)
708 {
709 phosphor::logging::commit<ReadFailure>();
710 }
Brandon Wyman3f1242f2020-01-28 13:11:25 -0600711 }
712 }
713}
714
Brandon Wyman59a35792020-06-04 12:37:40 -0500715void PowerSupply::onOffConfig(uint8_t data)
716{
717 using namespace phosphor::pmbus;
718
Faisal Awada9582d9c2023-07-11 09:31:22 -0500719 if (present && driverName != ACBEL_FSG032_DD_NAME)
Brandon Wyman59a35792020-06-04 12:37:40 -0500720 {
721 log<level::INFO>("ON_OFF_CONFIG write", entry("DATA=0x%02X", data));
722 try
723 {
724 std::vector<uint8_t> configData{data};
725 pmbusIntf->writeBinary(ON_OFF_CONFIG, configData,
726 Type::HwmonDeviceDebug);
727 }
728 catch (...)
729 {
730 // The underlying code in writeBinary will log a message to the
B. J. Wyman681b2a32021-04-20 22:31:22 +0000731 // journal if the write fails. If the ON_OFF_CONFIG is not setup
732 // as desired, later fault detection and analysis code should
733 // catch any of the fall out. We should not need to terminate
734 // the application if this write fails.
Brandon Wyman59a35792020-06-04 12:37:40 -0500735 }
736 }
737}
738
Brandon Wyman3225a452022-03-18 18:51:49 +0000739void PowerSupply::clearVinUVFault()
740{
741 // Read in1_lcrit_alarm to clear bits 3 and 4 of STATUS_INPUT.
742 // The fault bits in STAUTS_INPUT roll-up to STATUS_WORD. Clearing those
743 // bits in STATUS_INPUT should result in the corresponding STATUS_WORD bits
744 // also clearing.
745 //
746 // Do not care about return value. Should be 1 if active, 0 if not.
Faisal Awada9582d9c2023-07-11 09:31:22 -0500747 if (driverName != ACBEL_FSG032_DD_NAME)
748 {
749 static_cast<void>(
750 pmbusIntf->read("in1_lcrit_alarm", phosphor::pmbus::Type::Hwmon));
751 }
752 else
753 {
754 static_cast<void>(
755 pmbusIntf->read("curr1_crit_alarm", phosphor::pmbus::Type::Hwmon));
756 }
Brandon Wyman3225a452022-03-18 18:51:49 +0000757 vinUVFault = 0;
758}
759
Brandon Wyman3c208462020-05-13 16:25:58 -0500760void PowerSupply::clearFaults()
761{
Brandon Wyman82affd92021-11-24 19:12:49 +0000762 log<level::DEBUG>(
Shawn McCarney768d2262024-07-09 15:02:59 -0500763 std::format("clearFaults() inventoryPath: {}", inventoryPath).c_str());
Brandon Wyman5474c912021-02-23 14:39:43 -0600764 faultLogged = false;
Brandon Wyman3c208462020-05-13 16:25:58 -0500765 // The PMBus device driver does not allow for writing CLEAR_FAULTS
766 // directly. However, the pmbus hwmon device driver code will send a
767 // CLEAR_FAULTS after reading from any of the hwmon "files" in sysfs, so
768 // reading in1_input should result in clearing the fault bits in
769 // STATUS_BYTE/STATUS_WORD.
770 // I do not care what the return value is.
Brandon Wyman11151532020-11-10 13:45:57 -0600771 if (present)
Brandon Wyman3c208462020-05-13 16:25:58 -0500772 {
Brandon Wymane3f7ad22021-12-21 20:27:45 +0000773 clearFaultFlags();
Matt Spinler0975eaf2022-02-14 15:38:30 -0600774 checkAvailability();
Brandon Wyman9564e942020-11-10 14:01:42 -0600775 readFail = 0;
Brandon Wyman9564e942020-11-10 14:01:42 -0600776
Brandon Wyman11151532020-11-10 13:45:57 -0600777 try
778 {
Brandon Wyman3225a452022-03-18 18:51:49 +0000779 clearVinUVFault();
Brandon Wyman11151532020-11-10 13:45:57 -0600780 static_cast<void>(
781 pmbusIntf->read("in1_input", phosphor::pmbus::Type::Hwmon));
782 }
Patrick Williamsc1d4de52021-10-06 12:45:57 -0500783 catch (const ReadFailure& e)
Brandon Wyman11151532020-11-10 13:45:57 -0600784 {
785 // Since I do not care what the return value is, I really do not
B. J. Wyman681b2a32021-04-20 22:31:22 +0000786 // care much if it gets a ReadFailure either. However, this
787 // should not prevent the application from continuing to run, so
788 // catching the read failure.
Brandon Wyman11151532020-11-10 13:45:57 -0600789 }
Brandon Wyman3c208462020-05-13 16:25:58 -0500790 }
791}
792
Patrick Williams7354ce62022-07-22 19:26:56 -0500793void PowerSupply::inventoryChanged(sdbusplus::message_t& msg)
Brandon Wymanaed1f752019-11-25 18:10:52 -0600794{
795 std::string msgSensor;
Patrick Williamsabe49412020-05-13 17:59:47 -0500796 std::map<std::string, std::variant<uint32_t, bool>> msgData;
Brandon Wymanaed1f752019-11-25 18:10:52 -0600797 msg.read(msgSensor, msgData);
798
799 // Check if it was the Present property that changed.
800 auto valPropMap = msgData.find(PRESENT_PROP);
801 if (valPropMap != msgData.end())
802 {
803 if (std::get<bool>(valPropMap->second))
804 {
805 present = true;
B. J. Wyman681b2a32021-04-20 22:31:22 +0000806 // TODO: Immediately trying to read or write the "files" causes
807 // read or write failures.
Brandon Wyman1d7a7df2020-03-26 10:14:05 -0500808 using namespace std::chrono_literals;
809 std::this_thread::sleep_for(20ms);
Brandon Wyman9564e942020-11-10 14:01:42 -0600810 pmbusIntf->findHwmonDir();
Brandon Wyman59a35792020-06-04 12:37:40 -0500811 onOffConfig(phosphor::pmbus::ON_OFF_CONFIG_CONTROL_PIN_ONLY);
Brandon Wymanaed1f752019-11-25 18:10:52 -0600812 clearFaults();
Brandon Wyman1d7a7df2020-03-26 10:14:05 -0500813 updateInventory();
Brandon Wymanaed1f752019-11-25 18:10:52 -0600814 }
815 else
816 {
817 present = false;
818
819 // Clear out the now outdated inventory properties
820 updateInventory();
821 }
Matt Spinler0975eaf2022-02-14 15:38:30 -0600822 checkAvailability();
Brandon Wymanaed1f752019-11-25 18:10:52 -0600823 }
824}
825
Patrick Williams7354ce62022-07-22 19:26:56 -0500826void PowerSupply::inventoryAdded(sdbusplus::message_t& msg)
Brandon Wyman9a507db2021-02-25 16:15:22 -0600827{
828 sdbusplus::message::object_path path;
829 msg.read(path);
830 // Make sure the signal is for the PSU inventory path
831 if (path == inventoryPath)
832 {
833 std::map<std::string, std::map<std::string, std::variant<bool>>>
834 interfaces;
835 // Get map of interfaces and their properties
836 msg.read(interfaces);
837
838 auto properties = interfaces.find(INVENTORY_IFACE);
839 if (properties != interfaces.end())
840 {
841 auto property = properties->second.find(PRESENT_PROP);
842 if (property != properties->second.end())
843 {
844 present = std::get<bool>(property->second);
845
Shawn McCarney768d2262024-07-09 15:02:59 -0500846 log<level::INFO>(std::format("Power Supply {} Present {}",
Brandon Wyman9a507db2021-02-25 16:15:22 -0600847 inventoryPath, present)
848 .c_str());
849
850 updateInventory();
Matt Spinler0975eaf2022-02-14 15:38:30 -0600851 checkAvailability();
Brandon Wyman9a507db2021-02-25 16:15:22 -0600852 }
853 }
854 }
855}
856
Brandon Wyman8393f462022-06-28 16:06:46 +0000857auto PowerSupply::readVPDValue(const std::string& vpdName,
858 const phosphor::pmbus::Type& type,
859 const std::size_t& vpdSize)
860{
861 std::string vpdValue;
Patrick Williams48781ae2023-05-10 07:50:50 -0500862 const std::regex illegalVPDRegex = std::regex("[^[:alnum:]]",
863 std::regex::basic);
Brandon Wyman8393f462022-06-28 16:06:46 +0000864
865 try
866 {
867 vpdValue = pmbusIntf->readString(vpdName, type);
868 }
869 catch (const ReadFailure& e)
870 {
871 // Ignore the read failure, let pmbus code indicate failure,
872 // path...
873 // TODO - ibm918
874 // https://github.com/openbmc/docs/blob/master/designs/vpd-collection.md
875 // The BMC must log errors if any of the VPD cannot be properly
876 // parsed or fails ECC checks.
877 }
878
879 if (vpdValue.size() != vpdSize)
880 {
Shawn McCarney768d2262024-07-09 15:02:59 -0500881 log<level::INFO>(std::format("{} {} resize needed. size: {}", shortName,
Brandon Wyman8393f462022-06-28 16:06:46 +0000882 vpdName, vpdValue.size())
883 .c_str());
884 vpdValue.resize(vpdSize, ' ');
885 }
886
Brandon Wyman056935c2022-06-24 23:05:09 +0000887 // Replace any illegal values with space(s).
888 std::regex_replace(vpdValue.begin(), vpdValue.begin(), vpdValue.end(),
889 illegalVPDRegex, " ");
890
Brandon Wyman8393f462022-06-28 16:06:46 +0000891 return vpdValue;
892}
893
Brandon Wyman1d7a7df2020-03-26 10:14:05 -0500894void PowerSupply::updateInventory()
895{
896 using namespace phosphor::pmbus;
897
Chanh Nguyenc12c53b2021-04-06 17:24:47 +0700898#if IBM_VPD
Brandon Wyman1d7a7df2020-03-26 10:14:05 -0500899 std::string pn;
900 std::string fn;
901 std::string header;
902 std::string sn;
Brandon Wyman8393f462022-06-28 16:06:46 +0000903 // The IBM power supply splits the full serial number into two parts.
904 // Each part is 6 bytes long, which should match up with SN_KW_SIZE.
905 const auto HEADER_SIZE = 6;
906 const auto SERIAL_SIZE = 6;
907 // The IBM PSU firmware version size is a bit complicated. It was originally
908 // 1-byte, per command. It was later expanded to 2-bytes per command, then
909 // up to 8-bytes per command. The device driver only reads up to 2 bytes per
910 // command, but combines all three of the 2-byte reads, or all 4 of the
911 // 1-byte reads into one string. So, the maximum size expected is 6 bytes.
Shawn McCarney983c9892022-10-12 10:49:47 -0500912 // However, it is formatted by the driver as a hex string with two ASCII
913 // characters per byte. So the maximum ASCII string size is 12.
Faisal Awada9582d9c2023-07-11 09:31:22 -0500914 const auto IBMCFFPS_FW_VERSION_SIZE = 12;
915 const auto ACBEL_FSG032_FW_VERSION_SIZE = 6;
Brandon Wyman8393f462022-06-28 16:06:46 +0000916
Brandon Wyman1d7a7df2020-03-26 10:14:05 -0500917 using PropertyMap =
George Liu070c1bc2020-10-12 11:28:01 +0800918 std::map<std::string,
919 std::variant<std::string, std::vector<uint8_t>, bool>>;
Brandon Wyman1d7a7df2020-03-26 10:14:05 -0500920 PropertyMap assetProps;
George Liu070c1bc2020-10-12 11:28:01 +0800921 PropertyMap operProps;
Brandon Wyman1d7a7df2020-03-26 10:14:05 -0500922 PropertyMap versionProps;
923 PropertyMap ipzvpdDINFProps;
924 PropertyMap ipzvpdVINIProps;
925 using InterfaceMap = std::map<std::string, PropertyMap>;
926 InterfaceMap interfaces;
927 using ObjectMap = std::map<sdbusplus::message::object_path, InterfaceMap>;
928 ObjectMap object;
929#endif
B. J. Wyman681b2a32021-04-20 22:31:22 +0000930 log<level::DEBUG>(
Shawn McCarney768d2262024-07-09 15:02:59 -0500931 std::format("updateInventory() inventoryPath: {}", inventoryPath)
B. J. Wyman681b2a32021-04-20 22:31:22 +0000932 .c_str());
Brandon Wyman1d7a7df2020-03-26 10:14:05 -0500933
934 if (present)
935 {
936 // TODO: non-IBM inventory updates?
937
Chanh Nguyenc12c53b2021-04-06 17:24:47 +0700938#if IBM_VPD
Faisal Awada9582d9c2023-07-11 09:31:22 -0500939 if (driverName == ACBEL_FSG032_DD_NAME)
faisaladed7a02023-05-10 14:59:01 -0500940 {
941 getPsuVpdFromDbus("CC", modelName);
942 getPsuVpdFromDbus("PN", pn);
943 getPsuVpdFromDbus("FN", fn);
944 getPsuVpdFromDbus("SN", sn);
945 assetProps.emplace(SN_PROP, sn);
Faisal Awada9582d9c2023-07-11 09:31:22 -0500946 fwVersion = readVPDValue(FW_VERSION, Type::Debug,
947 ACBEL_FSG032_FW_VERSION_SIZE);
948 versionProps.emplace(VERSION_PROP, fwVersion);
faisaladed7a02023-05-10 14:59:01 -0500949 }
950 else
951 {
952 modelName = readVPDValue(CCIN, Type::HwmonDeviceDebug, CC_KW_SIZE);
953 pn = readVPDValue(PART_NUMBER, Type::Debug, PN_KW_SIZE);
954 fn = readVPDValue(FRU_NUMBER, Type::Debug, FN_KW_SIZE);
955
956 header = readVPDValue(SERIAL_HEADER, Type::Debug, HEADER_SIZE);
957 sn = readVPDValue(SERIAL_NUMBER, Type::Debug, SERIAL_SIZE);
958 assetProps.emplace(SN_PROP, header + sn);
Faisal Awada9582d9c2023-07-11 09:31:22 -0500959 fwVersion = readVPDValue(FW_VERSION, Type::HwmonDeviceDebug,
960 IBMCFFPS_FW_VERSION_SIZE);
961 versionProps.emplace(VERSION_PROP, fwVersion);
faisaladed7a02023-05-10 14:59:01 -0500962 }
963
Brandon Wyman8393f462022-06-28 16:06:46 +0000964 assetProps.emplace(MODEL_PROP, modelName);
Brandon Wyman8393f462022-06-28 16:06:46 +0000965 assetProps.emplace(PN_PROP, pn);
Brandon Wyman8393f462022-06-28 16:06:46 +0000966 assetProps.emplace(SPARE_PN_PROP, fn);
Brandon Wyman1d7a7df2020-03-26 10:14:05 -0500967
Brandon Wyman8393f462022-06-28 16:06:46 +0000968 ipzvpdVINIProps.emplace(
969 "CC", std::vector<uint8_t>(modelName.begin(), modelName.end()));
Brandon Wyman1d7a7df2020-03-26 10:14:05 -0500970 ipzvpdVINIProps.emplace("PN",
971 std::vector<uint8_t>(pn.begin(), pn.end()));
972 ipzvpdVINIProps.emplace("FN",
973 std::vector<uint8_t>(fn.begin(), fn.end()));
Brandon Wyman33d492f2022-03-23 20:45:17 +0000974 std::string header_sn = header + sn;
Brandon Wyman1d7a7df2020-03-26 10:14:05 -0500975 ipzvpdVINIProps.emplace(
976 "SN", std::vector<uint8_t>(header_sn.begin(), header_sn.end()));
977 std::string description = "IBM PS";
978 ipzvpdVINIProps.emplace(
979 "DR", std::vector<uint8_t>(description.begin(), description.end()));
980
Ben Tynerf8d8c462022-01-27 16:09:45 -0600981 // Populate the VINI Resource Type (RT) keyword
982 ipzvpdVINIProps.emplace("RT", std::vector<uint8_t>{'V', 'I', 'N', 'I'});
983
Brandon Wyman1d7a7df2020-03-26 10:14:05 -0500984 // Update the Resource Identifier (RI) keyword
985 // 2 byte FRC: 0x0003
986 // 2 byte RID: 0x1000, 0x1001...
987 std::uint8_t num = std::stoul(
988 inventoryPath.substr(inventoryPath.size() - 1, 1), nullptr, 0);
989 std::vector<uint8_t> ri{0x00, 0x03, 0x10, num};
990 ipzvpdDINFProps.emplace("RI", ri);
991
992 // Fill in the FRU Label (FL) keyword.
993 std::string fl = "E";
994 fl.push_back(inventoryPath.back());
995 fl.resize(FL_KW_SIZE, ' ');
996 ipzvpdDINFProps.emplace("FL",
997 std::vector<uint8_t>(fl.begin(), fl.end()));
998
Ben Tynerf8d8c462022-01-27 16:09:45 -0600999 // Populate the DINF Resource Type (RT) keyword
1000 ipzvpdDINFProps.emplace("RT", std::vector<uint8_t>{'D', 'I', 'N', 'F'});
1001
Brandon Wyman1d7a7df2020-03-26 10:14:05 -05001002 interfaces.emplace(ASSET_IFACE, std::move(assetProps));
1003 interfaces.emplace(VERSION_IFACE, std::move(versionProps));
1004 interfaces.emplace(DINF_IFACE, std::move(ipzvpdDINFProps));
1005 interfaces.emplace(VINI_IFACE, std::move(ipzvpdVINIProps));
1006
George Liu070c1bc2020-10-12 11:28:01 +08001007 // Update the Functional
1008 operProps.emplace(FUNCTIONAL_PROP, present);
1009 interfaces.emplace(OPERATIONAL_STATE_IFACE, std::move(operProps));
1010
Brandon Wyman1d7a7df2020-03-26 10:14:05 -05001011 auto path = inventoryPath.substr(strlen(INVENTORY_OBJ_PATH));
1012 object.emplace(path, std::move(interfaces));
1013
1014 try
1015 {
Patrick Williams48781ae2023-05-10 07:50:50 -05001016 auto service = util::getService(INVENTORY_OBJ_PATH,
1017 INVENTORY_MGR_IFACE, bus);
Brandon Wyman1d7a7df2020-03-26 10:14:05 -05001018
1019 if (service.empty())
1020 {
1021 log<level::ERR>("Unable to get inventory manager service");
1022 return;
1023 }
1024
Patrick Williams48781ae2023-05-10 07:50:50 -05001025 auto method = bus.new_method_call(service.c_str(),
1026 INVENTORY_OBJ_PATH,
1027 INVENTORY_MGR_IFACE, "Notify");
Brandon Wyman1d7a7df2020-03-26 10:14:05 -05001028
1029 method.append(std::move(object));
1030
1031 auto reply = bus.call(method);
1032 }
Patrick Williamsc1d4de52021-10-06 12:45:57 -05001033 catch (const std::exception& e)
Brandon Wyman1d7a7df2020-03-26 10:14:05 -05001034 {
Jay Meyer6a3fd2c2020-08-25 16:37:16 -05001035 log<level::ERR>(
1036 std::string(e.what() + std::string(" PATH=") + inventoryPath)
1037 .c_str());
Brandon Wyman1d7a7df2020-03-26 10:14:05 -05001038 }
1039#endif
1040 }
1041}
1042
Brandon Wymanae35ac52022-05-23 22:33:40 +00001043auto PowerSupply::getMaxPowerOut() const
1044{
1045 using namespace phosphor::pmbus;
1046
1047 auto maxPowerOut = 0;
1048
1049 if (present)
1050 {
1051 try
1052 {
1053 // Read max_power_out, should be direct format
Patrick Williams48781ae2023-05-10 07:50:50 -05001054 auto maxPowerOutStr = pmbusIntf->readString(MFR_POUT_MAX,
1055 Type::HwmonDeviceDebug);
Shawn McCarney768d2262024-07-09 15:02:59 -05001056 log<level::INFO>(std::format("{} MFR_POUT_MAX read {}", shortName,
Brandon Wymanae35ac52022-05-23 22:33:40 +00001057 maxPowerOutStr)
1058 .c_str());
1059 maxPowerOut = std::stod(maxPowerOutStr);
1060 }
1061 catch (const std::exception& e)
1062 {
Shawn McCarney768d2262024-07-09 15:02:59 -05001063 log<level::ERR>(std::format("{} MFR_POUT_MAX read error: {}",
Brandon Wymanae35ac52022-05-23 22:33:40 +00001064 shortName, e.what())
1065 .c_str());
1066 }
1067 }
1068
1069 return maxPowerOut;
1070}
1071
Matt Spinler592bd272023-08-30 11:00:01 -05001072void PowerSupply::setupSensors()
1073{
1074 setupInputPowerPeakSensor();
1075}
1076
1077void PowerSupply::setupInputPowerPeakSensor()
1078{
1079 if (peakInputPowerSensor || !present ||
1080 (bindPath.string().find(IBMCFFPS_DD_NAME) == std::string::npos))
1081 {
1082 return;
1083 }
1084
1085 // This PSU has problems with the input_history command
1086 if (getMaxPowerOut() == phosphor::pmbus::IBM_CFFPS_1400W)
1087 {
1088 return;
1089 }
1090
1091 auto sensorPath =
Shawn McCarney768d2262024-07-09 15:02:59 -05001092 std::format("/xyz/openbmc_project/sensors/power/ps{}_input_power_peak",
Matt Spinler592bd272023-08-30 11:00:01 -05001093 shortName.back());
1094
1095 peakInputPowerSensor = std::make_unique<PowerSensorObject>(
1096 bus, sensorPath.c_str(), PowerSensorObject::action::defer_emit);
1097
1098 // The others can remain at the defaults.
1099 peakInputPowerSensor->functional(true, true);
1100 peakInputPowerSensor->available(true, true);
1101 peakInputPowerSensor->value(0, true);
1102 peakInputPowerSensor->unit(
1103 sdbusplus::xyz::openbmc_project::Sensor::server::Value::Unit::Watts,
1104 true);
1105
1106 auto associations = getSensorAssociations();
1107 peakInputPowerSensor->associations(associations, true);
1108
1109 peakInputPowerSensor->emit_object_added();
1110}
1111
1112void PowerSupply::setSensorsNotAvailable()
1113{
1114 if (peakInputPowerSensor)
1115 {
1116 peakInputPowerSensor->value(std::numeric_limits<double>::quiet_NaN());
1117 peakInputPowerSensor->available(false);
1118 }
1119}
1120
Matt Spinler592bd272023-08-30 11:00:01 -05001121void PowerSupply::monitorSensors()
1122{
1123 monitorPeakInputPowerSensor();
1124}
1125
1126void PowerSupply::monitorPeakInputPowerSensor()
1127{
1128 if (!peakInputPowerSensor)
1129 {
1130 return;
1131 }
1132
1133 constexpr size_t recordSize = 5;
1134 std::vector<uint8_t> data;
1135
1136 // Get the peak input power with input history command.
1137 // New data only shows up every 30s, but just try to read it every 1s
1138 // anyway so we always have the most up to date value.
1139 try
1140 {
1141 data = pmbusIntf->readBinary(INPUT_HISTORY,
1142 pmbus::Type::HwmonDeviceDebug, recordSize);
1143 }
1144 catch (const ReadFailure& e)
1145 {
1146 peakInputPowerSensor->value(std::numeric_limits<double>::quiet_NaN());
1147 peakInputPowerSensor->functional(false);
1148 throw;
1149 }
1150
1151 if (data.size() != recordSize)
1152 {
1153 log<level::DEBUG>(
Shawn McCarney768d2262024-07-09 15:02:59 -05001154 std::format("Input history command returned {} bytes instead of 5",
Matt Spinler592bd272023-08-30 11:00:01 -05001155 data.size())
1156 .c_str());
1157 peakInputPowerSensor->value(std::numeric_limits<double>::quiet_NaN());
1158 peakInputPowerSensor->functional(false);
1159 return;
1160 }
1161
1162 // The format is SSAAAAPPPP:
1163 // SS = packet sequence number
1164 // AAAA = average power (linear format, little endian)
1165 // PPPP = peak power (linear format, little endian)
1166 auto peak = static_cast<uint16_t>(data[4]) << 8 | data[3];
1167 auto peakPower = linearToInteger(peak);
1168
1169 peakInputPowerSensor->value(peakPower);
1170 peakInputPowerSensor->functional(true);
1171 peakInputPowerSensor->available(true);
1172}
1173
Adriana Kobylak4175ffb2021-08-02 14:51:05 +00001174void PowerSupply::getInputVoltage(double& actualInputVoltage,
1175 int& inputVoltage) const
1176{
1177 using namespace phosphor::pmbus;
1178
1179 actualInputVoltage = in_input::VIN_VOLTAGE_0;
1180 inputVoltage = in_input::VIN_VOLTAGE_0;
1181
1182 if (present)
1183 {
1184 try
1185 {
1186 // Read input voltage in millivolts
1187 auto inputVoltageStr = pmbusIntf->readString(READ_VIN, Type::Hwmon);
1188
1189 // Convert to volts
1190 actualInputVoltage = std::stod(inputVoltageStr) / 1000;
1191
1192 // Calculate the voltage based on voltage thresholds
1193 if (actualInputVoltage < in_input::VIN_VOLTAGE_MIN)
1194 {
1195 inputVoltage = in_input::VIN_VOLTAGE_0;
1196 }
1197 else if (actualInputVoltage < in_input::VIN_VOLTAGE_110_THRESHOLD)
1198 {
1199 inputVoltage = in_input::VIN_VOLTAGE_110;
1200 }
1201 else
1202 {
1203 inputVoltage = in_input::VIN_VOLTAGE_220;
1204 }
1205 }
1206 catch (const std::exception& e)
1207 {
1208 log<level::ERR>(
Shawn McCarney768d2262024-07-09 15:02:59 -05001209 std::format("{} READ_VIN read error: {}", shortName, e.what())
Brandon Wyman321a6152022-03-19 00:11:44 +00001210 .c_str());
Adriana Kobylak4175ffb2021-08-02 14:51:05 +00001211 }
1212 }
1213}
1214
Matt Spinler0975eaf2022-02-14 15:38:30 -06001215void PowerSupply::checkAvailability()
1216{
1217 bool origAvailability = available;
George Liu9464c422023-02-27 14:30:27 +08001218 bool faulted = isPowerOn() && (hasPSKillFault() || hasIoutOCFault());
1219 available = present && !hasInputFault() && !hasVINUVFault() && !faulted;
Matt Spinler0975eaf2022-02-14 15:38:30 -06001220
1221 if (origAvailability != available)
1222 {
1223 auto invpath = inventoryPath.substr(strlen(INVENTORY_OBJ_PATH));
1224 phosphor::power::psu::setAvailable(bus, invpath, available);
Matt Spinlerca1e9ea2022-02-18 14:03:08 -06001225
1226 // Check if the health rollup needs to change based on the
1227 // new availability value.
1228 phosphor::power::psu::handleChassisHealthRollup(bus, inventoryPath,
1229 !available);
Matt Spinler0975eaf2022-02-14 15:38:30 -06001230 }
1231}
1232
Matt Spinlera068f422023-03-10 13:06:49 -06001233void PowerSupply::setInputVoltageRating()
1234{
1235 if (!present)
1236 {
Matt Spinler1aaf9f82023-03-22 09:52:22 -05001237 if (inputVoltageRatingIface)
1238 {
1239 inputVoltageRatingIface->value(0);
1240 inputVoltageRatingIface.reset();
1241 }
Matt Spinlera068f422023-03-10 13:06:49 -06001242 return;
1243 }
1244
1245 double inputVoltageValue{};
1246 int inputVoltageRating{};
1247 getInputVoltage(inputVoltageValue, inputVoltageRating);
1248
1249 if (!inputVoltageRatingIface)
1250 {
Shawn McCarney768d2262024-07-09 15:02:59 -05001251 auto path = std::format(
Matt Spinlera068f422023-03-10 13:06:49 -06001252 "/xyz/openbmc_project/sensors/voltage/ps{}_input_voltage_rating",
1253 shortName.back());
1254
1255 inputVoltageRatingIface = std::make_unique<SensorObject>(
1256 bus, path.c_str(), SensorObject::action::defer_emit);
1257
1258 // Leave other properties at their defaults
1259 inputVoltageRatingIface->unit(SensorInterface::Unit::Volts, true);
1260 inputVoltageRatingIface->value(static_cast<double>(inputVoltageRating),
1261 true);
1262
1263 inputVoltageRatingIface->emit_object_added();
1264 }
1265 else
1266 {
1267 inputVoltageRatingIface->value(static_cast<double>(inputVoltageRating));
1268 }
1269}
1270
faisaladed7a02023-05-10 14:59:01 -05001271void PowerSupply::getPsuVpdFromDbus(const std::string& keyword,
1272 std::string& vpdStr)
1273{
1274 try
1275 {
1276 std::vector<uint8_t> value;
1277 vpdStr.clear();
1278 util::getProperty(VINI_IFACE, keyword, inventoryPath,
1279 INVENTORY_MGR_IFACE, bus, value);
1280 for (char c : value)
1281 {
1282 vpdStr += c;
1283 }
1284 }
1285 catch (const sdbusplus::exception_t& e)
1286 {
1287 log<level::ERR>(
Shawn McCarney768d2262024-07-09 15:02:59 -05001288 std::format("Failed getProperty error: {}", e.what()).c_str());
faisaladed7a02023-05-10 14:59:01 -05001289 }
1290}
Matt Spinler592bd272023-08-30 11:00:01 -05001291
1292double PowerSupply::linearToInteger(uint16_t data)
1293{
1294 // The exponent is the first 5 bits, followed by 11 bits of mantissa.
1295 int8_t exponent = (data & 0xF800) >> 11;
1296 int16_t mantissa = (data & 0x07FF);
1297
1298 // If exponent's MSB on, then it's negative.
1299 // Convert from two's complement.
1300 if (exponent & 0x10)
1301 {
1302 exponent = (~exponent) & 0x1F;
1303 exponent = (exponent + 1) * -1;
1304 }
1305
1306 // If mantissa's MSB on, then it's negative.
1307 // Convert from two's complement.
1308 if (mantissa & 0x400)
1309 {
1310 mantissa = (~mantissa) & 0x07FF;
1311 mantissa = (mantissa + 1) * -1;
1312 }
1313
1314 auto value = static_cast<double>(mantissa) * pow(2, exponent);
1315 return value;
1316}
1317
1318std::vector<AssociationTuple> PowerSupply::getSensorAssociations()
1319{
1320 std::vector<AssociationTuple> associations;
1321
1322 associations.emplace_back("inventory", "sensors", inventoryPath);
1323
1324 auto chassis = getChassis(bus, inventoryPath);
1325 associations.emplace_back("chassis", "all_sensors", std::move(chassis));
1326
1327 return associations;
1328}
1329
Brandon Wyman3f1242f2020-01-28 13:11:25 -06001330} // namespace phosphor::power::psu