blob: 6ec8115df1a12e38b835c03d3d9a56cea90344f4 [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{
Faisal Awadab7131a12023-10-26 19:38:45 -050099 // Symbolic link to the device will exist if the driver is bound.
100 // So exit no action required if both the link and PSU are present
101 // or neither is present.
102 namespace fs = std::filesystem;
103 fs::path path;
B. J. Wyman681b2a32021-04-20 22:31:22 +0000104 auto action = (present) ? "bind" : "unbind";
B. J. Wyman681b2a32021-04-20 22:31:22 +0000105
Faisal Awadab7131a12023-10-26 19:38:45 -0500106 // This case should not happen, if no device driver name return.
107 if (driverName.empty())
108 {
109 log<level::INFO>("No device driver name found");
110 return;
111 }
112 if (bindPath.string().find(driverName) != std::string::npos)
113 {
114 // bindPath has driver name
115 path = bindPath / action;
116 }
117 else
118 {
119 // Add driver name to bindPath
120 path = bindPath / driverName / action;
121 bindPath = bindPath / driverName;
122 }
123
124 if ((std::filesystem::exists(bindPath / bindDevice) && present) ||
125 (!std::filesystem::exists(bindPath / bindDevice) && !present))
126 {
127 return;
128 }
B. J. Wyman681b2a32021-04-20 22:31:22 +0000129 if (present)
130 {
Brandon Wymanb1ee60f2022-03-22 22:37:12 +0000131 std::this_thread::sleep_for(std::chrono::milliseconds(bindDelay));
B. J. Wyman681b2a32021-04-20 22:31:22 +0000132 log<level::INFO>(
133 fmt::format("Binding device driver. path: {} device: {}",
134 path.string(), bindDevice)
135 .c_str());
136 }
137 else
138 {
139 log<level::INFO>(
140 fmt::format("Unbinding device driver. path: {} device: {}",
141 path.string(), bindDevice)
142 .c_str());
143 }
144
145 std::ofstream file;
146
147 file.exceptions(std::ofstream::failbit | std::ofstream::badbit |
148 std::ofstream::eofbit);
149
150 try
151 {
152 file.open(path);
153 file << bindDevice;
154 file.close();
155 }
Patrick Williamsc1d4de52021-10-06 12:45:57 -0500156 catch (const std::exception& e)
B. J. Wyman681b2a32021-04-20 22:31:22 +0000157 {
158 auto err = errno;
159
160 log<level::ERR>(
161 fmt::format("Failed binding or unbinding device. errno={}", err)
162 .c_str());
163 }
Brandon Wyman510acaa2020-11-05 18:32:04 -0600164}
165
Brandon Wymanaed1f752019-11-25 18:10:52 -0600166void PowerSupply::updatePresence()
167{
168 try
169 {
Brandon Wyman3f1242f2020-01-28 13:11:25 -0600170 present = getPresence(bus, inventoryPath);
Brandon Wymanaed1f752019-11-25 18:10:52 -0600171 }
Patrick Williams7354ce62022-07-22 19:26:56 -0500172 catch (const sdbusplus::exception_t& e)
Brandon Wymanaed1f752019-11-25 18:10:52 -0600173 {
174 // Relying on property change or interface added to retry.
175 // Log an informational trace to the journal.
Brandon Wymandf13c3a2020-12-15 14:25:22 -0600176 log<level::INFO>(
177 fmt::format("D-Bus property {} access failure exception",
178 inventoryPath)
179 .c_str());
Brandon Wymanaed1f752019-11-25 18:10:52 -0600180 }
181}
182
B. J. Wyman681b2a32021-04-20 22:31:22 +0000183void PowerSupply::updatePresenceGPIO()
184{
185 bool presentOld = present;
186
187 try
188 {
189 if (presenceGPIO->read() > 0)
190 {
191 present = true;
192 }
193 else
194 {
195 present = false;
196 }
197 }
Patrick Williamsc1d4de52021-10-06 12:45:57 -0500198 catch (const std::exception& e)
B. J. Wyman681b2a32021-04-20 22:31:22 +0000199 {
200 log<level::ERR>(
201 fmt::format("presenceGPIO read fail: {}", e.what()).c_str());
202 throw;
203 }
204
205 if (presentOld != present)
206 {
Brandon Wyman321a6152022-03-19 00:11:44 +0000207 log<level::DEBUG>(fmt::format("{} presentOld: {} present: {}",
208 shortName, presentOld, present)
209 .c_str());
Matt Spinlerca1e9ea2022-02-18 14:03:08 -0600210
211 auto invpath = inventoryPath.substr(strlen(INVENTORY_OBJ_PATH));
Brandon Wyman90d529a2022-03-22 23:02:54 +0000212
213 bindOrUnbindDriver(present);
214 if (present)
215 {
216 // If the power supply was present, then missing, and present again,
217 // the hwmon path may have changed. We will need the correct/updated
218 // path before any reads or writes are attempted.
219 pmbusIntf->findHwmonDir();
220 }
221
Brandon Wyman321a6152022-03-19 00:11:44 +0000222 setPresence(bus, invpath, present, shortName);
Matt Spinler592bd272023-08-30 11:00:01 -0500223 setupSensors();
Matt Spinlerca1e9ea2022-02-18 14:03:08 -0600224 updateInventory();
225
Brandon Wyman90d529a2022-03-22 23:02:54 +0000226 // Need Functional to already be correct before calling this.
Matt Spinlerca1e9ea2022-02-18 14:03:08 -0600227 checkAvailability();
228
B. J. Wyman681b2a32021-04-20 22:31:22 +0000229 if (present)
230 {
B. J. Wyman681b2a32021-04-20 22:31:22 +0000231 onOffConfig(phosphor::pmbus::ON_OFF_CONFIG_CONTROL_PIN_ONLY);
232 clearFaults();
Brandon Wyman18a24d92022-04-19 22:48:34 +0000233 // Indicate that the input history data and timestamps between all
234 // the power supplies that are present in the system need to be
235 // synchronized.
236 syncHistoryRequired = true;
B. J. Wyman681b2a32021-04-20 22:31:22 +0000237 }
Matt Spinler592bd272023-08-30 11:00:01 -0500238 else
239 {
240 setSensorsNotAvailable();
241 }
B. J. Wyman681b2a32021-04-20 22:31:22 +0000242 }
243}
244
Brandon Wymanc2203432021-12-21 23:09:48 +0000245void PowerSupply::analyzeCMLFault()
246{
247 if (statusWord & phosphor::pmbus::status_word::CML_FAULT)
248 {
Brandon Wymanc2906f42021-12-21 20:14:56 +0000249 if (cmlFault < DEGLITCH_LIMIT)
Brandon Wymanc2203432021-12-21 23:09:48 +0000250 {
Brandon Wyman9e292ee2022-03-10 22:56:23 +0000251 if (statusWord != statusWordOld)
252 {
Brandon Wyman321a6152022-03-19 00:11:44 +0000253 log<level::ERR>(
254 fmt::format("{} CML fault: STATUS_WORD = {:#06x}, "
255 "STATUS_CML = {:#02x}",
256 shortName, statusWord, statusCML)
257 .c_str());
Brandon Wyman9e292ee2022-03-10 22:56:23 +0000258 }
Brandon Wymanc2906f42021-12-21 20:14:56 +0000259 cmlFault++;
260 }
261 }
262 else
263 {
264 cmlFault = 0;
Brandon Wymanc2203432021-12-21 23:09:48 +0000265 }
266}
267
Brandon Wymane3b0bb02021-12-21 23:16:48 +0000268void PowerSupply::analyzeInputFault()
269{
270 if (statusWord & phosphor::pmbus::status_word::INPUT_FAULT_WARN)
271 {
Brandon Wymanc2906f42021-12-21 20:14:56 +0000272 if (inputFault < DEGLITCH_LIMIT)
Brandon Wymane3b0bb02021-12-21 23:16:48 +0000273 {
Brandon Wyman9e292ee2022-03-10 22:56:23 +0000274 if (statusWord != statusWordOld)
275 {
276 log<level::ERR>(
Brandon Wyman321a6152022-03-19 00:11:44 +0000277 fmt::format("{} INPUT fault: STATUS_WORD = {:#06x}, "
Brandon Wyman9e292ee2022-03-10 22:56:23 +0000278 "STATUS_MFR_SPECIFIC = {:#04x}, "
279 "STATUS_INPUT = {:#04x}",
Brandon Wyman321a6152022-03-19 00:11:44 +0000280 shortName, statusWord, statusMFR, statusInput)
Brandon Wyman9e292ee2022-03-10 22:56:23 +0000281 .c_str());
282 }
Brandon Wymanc2906f42021-12-21 20:14:56 +0000283 inputFault++;
284 }
Brandon Wymane3b0bb02021-12-21 23:16:48 +0000285 }
Brandon Wyman82affd92021-11-24 19:12:49 +0000286
287 // If had INPUT/VIN_UV fault, and now off.
288 // Trace that odd behavior.
289 if (inputFault &&
290 !(statusWord & phosphor::pmbus::status_word::INPUT_FAULT_WARN))
291 {
292 log<level::INFO>(
Brandon Wyman321a6152022-03-19 00:11:44 +0000293 fmt::format("{} INPUT fault cleared: STATUS_WORD = {:#06x}, "
Brandon Wyman6f939a32022-03-10 18:42:20 +0000294 "STATUS_MFR_SPECIFIC = {:#04x}, "
295 "STATUS_INPUT = {:#04x}",
Brandon Wyman321a6152022-03-19 00:11:44 +0000296 shortName, statusWord, statusMFR, statusInput)
Brandon Wyman82affd92021-11-24 19:12:49 +0000297 .c_str());
Brandon Wymanc2906f42021-12-21 20:14:56 +0000298 inputFault = 0;
Brandon Wyman82affd92021-11-24 19:12:49 +0000299 }
Brandon Wymane3b0bb02021-12-21 23:16:48 +0000300}
301
Brandon Wymanc2c87132021-12-21 23:22:18 +0000302void PowerSupply::analyzeVoutOVFault()
303{
304 if (statusWord & phosphor::pmbus::status_word::VOUT_OV_FAULT)
305 {
Brandon Wymanc2906f42021-12-21 20:14:56 +0000306 if (voutOVFault < DEGLITCH_LIMIT)
Brandon Wymanc2c87132021-12-21 23:22:18 +0000307 {
Brandon Wyman9e292ee2022-03-10 22:56:23 +0000308 if (statusWord != statusWordOld)
309 {
310 log<level::ERR>(
Brandon Wyman321a6152022-03-19 00:11:44 +0000311 fmt::format(
312 "{} VOUT_OV_FAULT fault: STATUS_WORD = {:#06x}, "
313 "STATUS_MFR_SPECIFIC = {:#04x}, "
314 "STATUS_VOUT = {:#02x}",
315 shortName, statusWord, statusMFR, statusVout)
Brandon Wyman9e292ee2022-03-10 22:56:23 +0000316 .c_str());
317 }
Brandon Wymanc2c87132021-12-21 23:22:18 +0000318
Brandon Wymanc2906f42021-12-21 20:14:56 +0000319 voutOVFault++;
320 }
321 }
322 else
323 {
324 voutOVFault = 0;
Brandon Wymanc2c87132021-12-21 23:22:18 +0000325 }
326}
327
Brandon Wymana00e7302021-12-21 23:28:29 +0000328void PowerSupply::analyzeIoutOCFault()
329{
330 if (statusWord & phosphor::pmbus::status_word::IOUT_OC_FAULT)
331 {
Brandon Wymanc2906f42021-12-21 20:14:56 +0000332 if (ioutOCFault < DEGLITCH_LIMIT)
Brandon Wymana00e7302021-12-21 23:28:29 +0000333 {
Brandon Wyman9e292ee2022-03-10 22:56:23 +0000334 if (statusWord != statusWordOld)
335 {
336 log<level::ERR>(
Brandon Wyman321a6152022-03-19 00:11:44 +0000337 fmt::format("{} IOUT fault: STATUS_WORD = {:#06x}, "
Brandon Wyman9e292ee2022-03-10 22:56:23 +0000338 "STATUS_MFR_SPECIFIC = {:#04x}, "
339 "STATUS_IOUT = {:#04x}",
Brandon Wyman321a6152022-03-19 00:11:44 +0000340 shortName, statusWord, statusMFR, statusIout)
Brandon Wyman9e292ee2022-03-10 22:56:23 +0000341 .c_str());
342 }
Brandon Wymana00e7302021-12-21 23:28:29 +0000343
Brandon Wymanc2906f42021-12-21 20:14:56 +0000344 ioutOCFault++;
345 }
346 }
347 else
348 {
349 ioutOCFault = 0;
Brandon Wymana00e7302021-12-21 23:28:29 +0000350 }
351}
352
Brandon Wyman08378782021-12-21 23:48:15 +0000353void PowerSupply::analyzeVoutUVFault()
354{
355 if ((statusWord & phosphor::pmbus::status_word::VOUT_FAULT) &&
356 !(statusWord & phosphor::pmbus::status_word::VOUT_OV_FAULT))
357 {
Brandon Wymanc2906f42021-12-21 20:14:56 +0000358 if (voutUVFault < DEGLITCH_LIMIT)
Brandon Wyman08378782021-12-21 23:48:15 +0000359 {
Brandon Wyman9e292ee2022-03-10 22:56:23 +0000360 if (statusWord != statusWordOld)
361 {
362 log<level::ERR>(
Brandon Wyman321a6152022-03-19 00:11:44 +0000363 fmt::format(
364 "{} VOUT_UV_FAULT fault: STATUS_WORD = {:#06x}, "
365 "STATUS_MFR_SPECIFIC = {:#04x}, "
366 "STATUS_VOUT = {:#04x}",
367 shortName, statusWord, statusMFR, statusVout)
Brandon Wyman9e292ee2022-03-10 22:56:23 +0000368 .c_str());
369 }
Brandon Wymanc2906f42021-12-21 20:14:56 +0000370 voutUVFault++;
371 }
372 }
373 else
374 {
375 voutUVFault = 0;
Brandon Wyman08378782021-12-21 23:48:15 +0000376 }
377}
378
Brandon Wymand5d9a222021-12-21 23:59:05 +0000379void PowerSupply::analyzeFanFault()
380{
381 if (statusWord & phosphor::pmbus::status_word::FAN_FAULT)
382 {
Brandon Wymanc2906f42021-12-21 20:14:56 +0000383 if (fanFault < DEGLITCH_LIMIT)
Brandon Wymand5d9a222021-12-21 23:59:05 +0000384 {
Brandon Wyman9e292ee2022-03-10 22:56:23 +0000385 if (statusWord != statusWordOld)
386 {
Brandon Wyman321a6152022-03-19 00:11:44 +0000387 log<level::ERR>(fmt::format("{} FANS fault/warning: "
Brandon Wyman9e292ee2022-03-10 22:56:23 +0000388 "STATUS_WORD = {:#06x}, "
389 "STATUS_MFR_SPECIFIC = {:#04x}, "
390 "STATUS_FANS_1_2 = {:#04x}",
Brandon Wyman321a6152022-03-19 00:11:44 +0000391 shortName, statusWord, statusMFR,
392 statusFans12)
Brandon Wyman9e292ee2022-03-10 22:56:23 +0000393 .c_str());
394 }
Brandon Wymanc2906f42021-12-21 20:14:56 +0000395 fanFault++;
396 }
397 }
398 else
399 {
400 fanFault = 0;
Brandon Wymand5d9a222021-12-21 23:59:05 +0000401 }
402}
403
Brandon Wyman52cb3f22021-12-21 23:02:47 +0000404void PowerSupply::analyzeTemperatureFault()
405{
406 if (statusWord & phosphor::pmbus::status_word::TEMPERATURE_FAULT_WARN)
407 {
Brandon Wymanc2906f42021-12-21 20:14:56 +0000408 if (tempFault < DEGLITCH_LIMIT)
Brandon Wyman52cb3f22021-12-21 23:02:47 +0000409 {
Brandon Wyman9e292ee2022-03-10 22:56:23 +0000410 if (statusWord != statusWordOld)
411 {
Brandon Wyman321a6152022-03-19 00:11:44 +0000412 log<level::ERR>(fmt::format("{} TEMPERATURE fault/warning: "
Brandon Wyman9e292ee2022-03-10 22:56:23 +0000413 "STATUS_WORD = {:#06x}, "
414 "STATUS_MFR_SPECIFIC = {:#04x}, "
415 "STATUS_TEMPERATURE = {:#04x}",
Brandon Wyman321a6152022-03-19 00:11:44 +0000416 shortName, statusWord, statusMFR,
Brandon Wyman9e292ee2022-03-10 22:56:23 +0000417 statusTemperature)
418 .c_str());
419 }
Brandon Wymanc2906f42021-12-21 20:14:56 +0000420 tempFault++;
421 }
422 }
423 else
424 {
425 tempFault = 0;
Brandon Wyman52cb3f22021-12-21 23:02:47 +0000426 }
427}
428
Brandon Wyman993b5542021-12-21 22:55:16 +0000429void PowerSupply::analyzePgoodFault()
430{
431 if ((statusWord & phosphor::pmbus::status_word::POWER_GOOD_NEGATED) ||
432 (statusWord & phosphor::pmbus::status_word::UNIT_IS_OFF))
433 {
Brandon Wyman6d469fd2022-06-15 16:58:21 +0000434 if (pgoodFault < PGOOD_DEGLITCH_LIMIT)
Brandon Wyman993b5542021-12-21 22:55:16 +0000435 {
Brandon Wyman9e292ee2022-03-10 22:56:23 +0000436 if (statusWord != statusWordOld)
437 {
Brandon Wyman321a6152022-03-19 00:11:44 +0000438 log<level::ERR>(fmt::format("{} PGOOD fault: "
Brandon Wyman9e292ee2022-03-10 22:56:23 +0000439 "STATUS_WORD = {:#06x}, "
440 "STATUS_MFR_SPECIFIC = {:#04x}",
Brandon Wyman321a6152022-03-19 00:11:44 +0000441 shortName, statusWord, statusMFR)
Brandon Wyman9e292ee2022-03-10 22:56:23 +0000442 .c_str());
443 }
Brandon Wyman993b5542021-12-21 22:55:16 +0000444 pgoodFault++;
445 }
446 }
447 else
448 {
449 pgoodFault = 0;
450 }
451}
452
Brandon Wyman39ea02b2021-11-23 23:22:23 +0000453void PowerSupply::determineMFRFault()
454{
Faisal Awadab66ae502023-04-01 18:30:32 -0500455 if (bindPath.string().find(IBMCFFPS_DD_NAME) != std::string::npos)
Brandon Wyman39ea02b2021-11-23 23:22:23 +0000456 {
457 // IBM MFR_SPECIFIC[4] is PS_Kill fault
458 if (statusMFR & 0x10)
459 {
Brandon Wymanc2906f42021-12-21 20:14:56 +0000460 if (psKillFault < DEGLITCH_LIMIT)
461 {
462 psKillFault++;
463 }
464 }
465 else
466 {
467 psKillFault = 0;
Brandon Wyman39ea02b2021-11-23 23:22:23 +0000468 }
469 // IBM MFR_SPECIFIC[6] is 12Vcs fault.
470 if (statusMFR & 0x40)
471 {
Brandon Wymanc2906f42021-12-21 20:14:56 +0000472 if (ps12VcsFault < DEGLITCH_LIMIT)
473 {
474 ps12VcsFault++;
475 }
476 }
477 else
478 {
479 ps12VcsFault = 0;
Brandon Wyman39ea02b2021-11-23 23:22:23 +0000480 }
481 // IBM MFR_SPECIFIC[7] is 12V Current-Share fault.
482 if (statusMFR & 0x80)
483 {
Brandon Wymanc2906f42021-12-21 20:14:56 +0000484 if (psCS12VFault < DEGLITCH_LIMIT)
485 {
486 psCS12VFault++;
487 }
488 }
489 else
490 {
491 psCS12VFault = 0;
Brandon Wyman39ea02b2021-11-23 23:22:23 +0000492 }
493 }
494}
495
Brandon Wyman6c2ac392021-12-21 22:23:06 +0000496void PowerSupply::analyzeMFRFault()
497{
498 if (statusWord & phosphor::pmbus::status_word::MFR_SPECIFIC_FAULT)
499 {
Brandon Wymanc2906f42021-12-21 20:14:56 +0000500 if (mfrFault < DEGLITCH_LIMIT)
Brandon Wyman6c2ac392021-12-21 22:23:06 +0000501 {
Brandon Wyman9e292ee2022-03-10 22:56:23 +0000502 if (statusWord != statusWordOld)
503 {
Brandon Wyman321a6152022-03-19 00:11:44 +0000504 log<level::ERR>(fmt::format("{} MFR fault: "
Brandon Wyman9e292ee2022-03-10 22:56:23 +0000505 "STATUS_WORD = {:#06x} "
506 "STATUS_MFR_SPECIFIC = {:#04x}",
Brandon Wyman321a6152022-03-19 00:11:44 +0000507 shortName, statusWord, statusMFR)
Brandon Wyman9e292ee2022-03-10 22:56:23 +0000508 .c_str());
509 }
Brandon Wymanc2906f42021-12-21 20:14:56 +0000510 mfrFault++;
Brandon Wyman6c2ac392021-12-21 22:23:06 +0000511 }
512
Brandon Wyman6c2ac392021-12-21 22:23:06 +0000513 determineMFRFault();
514 }
Brandon Wymanc2906f42021-12-21 20:14:56 +0000515 else
516 {
517 mfrFault = 0;
518 }
Brandon Wyman6c2ac392021-12-21 22:23:06 +0000519}
520
Brandon Wymanf087f472021-12-22 00:04:27 +0000521void PowerSupply::analyzeVinUVFault()
522{
523 if (statusWord & phosphor::pmbus::status_word::VIN_UV_FAULT)
524 {
Brandon Wymanc2906f42021-12-21 20:14:56 +0000525 if (vinUVFault < DEGLITCH_LIMIT)
Brandon Wymanf087f472021-12-22 00:04:27 +0000526 {
Brandon Wyman9e292ee2022-03-10 22:56:23 +0000527 if (statusWord != statusWordOld)
528 {
529 log<level::ERR>(
Brandon Wyman321a6152022-03-19 00:11:44 +0000530 fmt::format("{} VIN_UV fault: STATUS_WORD = {:#06x}, "
Brandon Wyman9e292ee2022-03-10 22:56:23 +0000531 "STATUS_MFR_SPECIFIC = {:#04x}, "
532 "STATUS_INPUT = {:#04x}",
Brandon Wyman321a6152022-03-19 00:11:44 +0000533 shortName, statusWord, statusMFR, statusInput)
Brandon Wyman9e292ee2022-03-10 22:56:23 +0000534 .c_str());
535 }
Brandon Wymanc2906f42021-12-21 20:14:56 +0000536 vinUVFault++;
Brandon Wymanf087f472021-12-22 00:04:27 +0000537 }
Jim Wright4ab86562022-11-18 14:05:46 -0600538 // Remember that this PSU has seen an AC fault
539 acFault = AC_FAULT_LIMIT;
Brandon Wymanf087f472021-12-22 00:04:27 +0000540 }
Jim Wright7f9288c2022-12-08 11:57:04 -0600541 else
Brandon Wyman82affd92021-11-24 19:12:49 +0000542 {
Jim Wright7f9288c2022-12-08 11:57:04 -0600543 if (vinUVFault != 0)
544 {
545 log<level::INFO>(
546 fmt::format("{} VIN_UV fault cleared: STATUS_WORD = {:#06x}, "
547 "STATUS_MFR_SPECIFIC = {:#04x}, "
548 "STATUS_INPUT = {:#04x}",
549 shortName, statusWord, statusMFR, statusInput)
550 .c_str());
551 vinUVFault = 0;
552 }
Jim Wright4ab86562022-11-18 14:05:46 -0600553 // No AC fail, decrement counter
Jim Wright7f9288c2022-12-08 11:57:04 -0600554 if (acFault != 0)
Jim Wright4ab86562022-11-18 14:05:46 -0600555 {
556 --acFault;
557 }
Brandon Wyman82affd92021-11-24 19:12:49 +0000558 }
Brandon Wymanf087f472021-12-22 00:04:27 +0000559}
560
Brandon Wyman3f1242f2020-01-28 13:11:25 -0600561void PowerSupply::analyze()
562{
563 using namespace phosphor::pmbus;
564
B. J. Wyman681b2a32021-04-20 22:31:22 +0000565 if (presenceGPIO)
566 {
567 updatePresenceGPIO();
568 }
569
Brandon Wyman32453e92021-12-15 19:00:14 +0000570 if (present)
Brandon Wyman3f1242f2020-01-28 13:11:25 -0600571 {
572 try
573 {
Brandon Wyman9e292ee2022-03-10 22:56:23 +0000574 statusWordOld = statusWord;
Brandon Wyman32453e92021-12-15 19:00:14 +0000575 statusWord = pmbusIntf->read(STATUS_WORD, Type::Debug,
576 (readFail < LOG_LIMIT));
Brandon Wymanf65c4062020-08-19 13:15:53 -0500577 // Read worked, reset the fail count.
578 readFail = 0;
Brandon Wyman3f1242f2020-01-28 13:11:25 -0600579
580 if (statusWord)
581 {
Brandon Wymanf07bc792021-10-12 19:00:35 +0000582 statusInput = pmbusIntf->read(STATUS_INPUT, Type::Debug);
Faisal Awadab66ae502023-04-01 18:30:32 -0500583 if (bindPath.string().find(IBMCFFPS_DD_NAME) !=
584 std::string::npos)
585 {
586 statusMFR = pmbusIntf->read(STATUS_MFR, Type::Debug);
587 }
Brandon Wyman85c7bf42021-10-19 22:28:48 +0000588 statusCML = pmbusIntf->read(STATUS_CML, Type::Debug);
Brandon Wyman6710ba22021-10-27 17:39:31 +0000589 auto status0Vout = pmbusIntf->insertPageNum(STATUS_VOUT, 0);
590 statusVout = pmbusIntf->read(status0Vout, Type::Debug);
Brandon Wymanb10b3be2021-11-09 22:12:15 +0000591 statusIout = pmbusIntf->read(STATUS_IOUT, Type::Debug);
Brandon Wyman7ee4d7e2021-11-19 20:48:23 +0000592 statusFans12 = pmbusIntf->read(STATUS_FANS_1_2, Type::Debug);
Patrick Williams48781ae2023-05-10 07:50:50 -0500593 statusTemperature = pmbusIntf->read(STATUS_TEMPERATURE,
594 Type::Debug);
Brandon Wyman9ddc6222021-10-28 17:28:01 +0000595
Brandon Wymanc2203432021-12-21 23:09:48 +0000596 analyzeCMLFault();
Brandon Wyman85c7bf42021-10-19 22:28:48 +0000597
Brandon Wymane3b0bb02021-12-21 23:16:48 +0000598 analyzeInputFault();
Brandon Wyman3f1242f2020-01-28 13:11:25 -0600599
Brandon Wymanc2c87132021-12-21 23:22:18 +0000600 analyzeVoutOVFault();
Brandon Wyman6710ba22021-10-27 17:39:31 +0000601
Brandon Wymana00e7302021-12-21 23:28:29 +0000602 analyzeIoutOCFault();
Brandon Wymanb10b3be2021-11-09 22:12:15 +0000603
Brandon Wyman08378782021-12-21 23:48:15 +0000604 analyzeVoutUVFault();
Brandon Wyman2cf46942021-10-28 19:09:16 +0000605
Brandon Wymand5d9a222021-12-21 23:59:05 +0000606 analyzeFanFault();
Brandon Wyman7ee4d7e2021-11-19 20:48:23 +0000607
Brandon Wyman52cb3f22021-12-21 23:02:47 +0000608 analyzeTemperatureFault();
Brandon Wyman96893a42021-11-05 19:56:57 +0000609
Brandon Wyman993b5542021-12-21 22:55:16 +0000610 analyzePgoodFault();
Brandon Wyman2916ea52021-11-06 03:31:18 +0000611
Brandon Wyman6c2ac392021-12-21 22:23:06 +0000612 analyzeMFRFault();
Brandon Wyman3f1242f2020-01-28 13:11:25 -0600613
Brandon Wymanf087f472021-12-22 00:04:27 +0000614 analyzeVinUVFault();
Brandon Wyman3f1242f2020-01-28 13:11:25 -0600615 }
616 else
617 {
Brandon Wyman9e292ee2022-03-10 22:56:23 +0000618 if (statusWord != statusWordOld)
619 {
Brandon Wyman321a6152022-03-19 00:11:44 +0000620 log<level::INFO>(fmt::format("{} STATUS_WORD = {:#06x}",
621 shortName, statusWord)
Brandon Wyman9e292ee2022-03-10 22:56:23 +0000622 .c_str());
623 }
624
Brandon Wymane3f7ad22021-12-21 20:27:45 +0000625 // if INPUT/VIN_UV fault was on, it cleared, trace it.
626 if (inputFault)
627 {
628 log<level::INFO>(
629 fmt::format(
Brandon Wyman321a6152022-03-19 00:11:44 +0000630 "{} INPUT fault cleared: STATUS_WORD = {:#06x}",
631 shortName, statusWord)
Brandon Wymane3f7ad22021-12-21 20:27:45 +0000632 .c_str());
633 }
634
635 if (vinUVFault)
636 {
637 log<level::INFO>(
Brandon Wyman321a6152022-03-19 00:11:44 +0000638 fmt::format("{} VIN_UV cleared: STATUS_WORD = {:#06x}",
639 shortName, statusWord)
Brandon Wymane3f7ad22021-12-21 20:27:45 +0000640 .c_str());
641 }
642
Brandon Wyman06ca4592021-12-06 22:52:23 +0000643 if (pgoodFault > 0)
Brandon Wyman4aecc292021-11-10 22:40:41 +0000644 {
Brandon Wyman321a6152022-03-19 00:11:44 +0000645 log<level::INFO>(
646 fmt::format("{} pgoodFault cleared", shortName)
647 .c_str());
Brandon Wyman4aecc292021-11-10 22:40:41 +0000648 }
Brandon Wymane3f7ad22021-12-21 20:27:45 +0000649
650 clearFaultFlags();
Jim Wright4ab86562022-11-18 14:05:46 -0600651 // No AC fail, decrement counter
Jim Wright7f9288c2022-12-08 11:57:04 -0600652 if (acFault != 0)
Jim Wright4ab86562022-11-18 14:05:46 -0600653 {
654 --acFault;
655 }
Brandon Wyman3f1242f2020-01-28 13:11:25 -0600656 }
Brandon Wyman82affd92021-11-24 19:12:49 +0000657
658 // Save off old inputVoltage value.
659 // Get latest inputVoltage.
660 // If voltage went from below minimum, and now is not, clear faults.
661 // Note: getInputVoltage() has its own try/catch.
662 int inputVoltageOld = inputVoltage;
Brandon Wyman4fc191f2022-03-10 23:07:13 +0000663 double actualInputVoltageOld = actualInputVoltage;
Brandon Wyman82affd92021-11-24 19:12:49 +0000664 getInputVoltage(actualInputVoltage, inputVoltage);
665 if ((inputVoltageOld == in_input::VIN_VOLTAGE_0) &&
666 (inputVoltage != in_input::VIN_VOLTAGE_0))
667 {
668 log<level::INFO>(
669 fmt::format(
Brandon Wyman4fc191f2022-03-10 23:07:13 +0000670 "{} READ_VIN back in range: actualInputVoltageOld = {} "
671 "actualInputVoltage = {}",
672 shortName, actualInputVoltageOld, actualInputVoltage)
Brandon Wyman82affd92021-11-24 19:12:49 +0000673 .c_str());
Brandon Wyman3225a452022-03-18 18:51:49 +0000674 clearVinUVFault();
Brandon Wyman82affd92021-11-24 19:12:49 +0000675 }
Brandon Wyman4fc191f2022-03-10 23:07:13 +0000676 else if (vinUVFault && (inputVoltage != in_input::VIN_VOLTAGE_0))
677 {
678 log<level::INFO>(
679 fmt::format(
680 "{} CLEAR_FAULTS: vinUVFault {} actualInputVoltage {}",
681 shortName, vinUVFault, actualInputVoltage)
682 .c_str());
683 // Do we have a VIN_UV fault latched that can now be cleared
Jim Wright4ab86562022-11-18 14:05:46 -0600684 // due to voltage back in range? Attempt to clear the
685 // fault(s), re-check faults on next call.
Brandon Wyman3225a452022-03-18 18:51:49 +0000686 clearVinUVFault();
Brandon Wyman4fc191f2022-03-10 23:07:13 +0000687 }
Brandon Wymanae35ac52022-05-23 22:33:40 +0000688 else if (std::abs(actualInputVoltageOld - actualInputVoltage) >
689 10.0)
Brandon Wyman4fc191f2022-03-10 23:07:13 +0000690 {
691 log<level::INFO>(
692 fmt::format(
693 "{} actualInputVoltageOld = {} actualInputVoltage = {}",
694 shortName, actualInputVoltageOld, actualInputVoltage)
695 .c_str());
696 }
Matt Spinler0975eaf2022-02-14 15:38:30 -0600697
Matt Spinler592bd272023-08-30 11:00:01 -0500698 monitorSensors();
699
Matt Spinler0975eaf2022-02-14 15:38:30 -0600700 checkAvailability();
Brandon Wyman3f1242f2020-01-28 13:11:25 -0600701 }
Patrick Williamsc1d4de52021-10-06 12:45:57 -0500702 catch (const ReadFailure& e)
Brandon Wyman3f1242f2020-01-28 13:11:25 -0600703 {
Brandon Wyman32453e92021-12-15 19:00:14 +0000704 if (readFail < SIZE_MAX)
705 {
706 readFail++;
707 }
708 if (readFail == LOG_LIMIT)
709 {
710 phosphor::logging::commit<ReadFailure>();
711 }
Brandon Wyman3f1242f2020-01-28 13:11:25 -0600712 }
713 }
714}
715
Brandon Wyman59a35792020-06-04 12:37:40 -0500716void PowerSupply::onOffConfig(uint8_t data)
717{
718 using namespace phosphor::pmbus;
719
Faisal Awada9582d9c2023-07-11 09:31:22 -0500720 if (present && driverName != ACBEL_FSG032_DD_NAME)
Brandon Wyman59a35792020-06-04 12:37:40 -0500721 {
722 log<level::INFO>("ON_OFF_CONFIG write", entry("DATA=0x%02X", data));
723 try
724 {
725 std::vector<uint8_t> configData{data};
726 pmbusIntf->writeBinary(ON_OFF_CONFIG, configData,
727 Type::HwmonDeviceDebug);
728 }
729 catch (...)
730 {
731 // The underlying code in writeBinary will log a message to the
B. J. Wyman681b2a32021-04-20 22:31:22 +0000732 // journal if the write fails. If the ON_OFF_CONFIG is not setup
733 // as desired, later fault detection and analysis code should
734 // catch any of the fall out. We should not need to terminate
735 // the application if this write fails.
Brandon Wyman59a35792020-06-04 12:37:40 -0500736 }
737 }
738}
739
Brandon Wyman3225a452022-03-18 18:51:49 +0000740void PowerSupply::clearVinUVFault()
741{
742 // Read in1_lcrit_alarm to clear bits 3 and 4 of STATUS_INPUT.
743 // The fault bits in STAUTS_INPUT roll-up to STATUS_WORD. Clearing those
744 // bits in STATUS_INPUT should result in the corresponding STATUS_WORD bits
745 // also clearing.
746 //
747 // Do not care about return value. Should be 1 if active, 0 if not.
Faisal Awada9582d9c2023-07-11 09:31:22 -0500748 if (driverName != ACBEL_FSG032_DD_NAME)
749 {
750 static_cast<void>(
751 pmbusIntf->read("in1_lcrit_alarm", phosphor::pmbus::Type::Hwmon));
752 }
753 else
754 {
755 static_cast<void>(
756 pmbusIntf->read("curr1_crit_alarm", phosphor::pmbus::Type::Hwmon));
757 }
Brandon Wyman3225a452022-03-18 18:51:49 +0000758 vinUVFault = 0;
759}
760
Brandon Wyman3c208462020-05-13 16:25:58 -0500761void PowerSupply::clearFaults()
762{
Brandon Wyman82affd92021-11-24 19:12:49 +0000763 log<level::DEBUG>(
764 fmt::format("clearFaults() inventoryPath: {}", inventoryPath).c_str());
Brandon Wyman5474c912021-02-23 14:39:43 -0600765 faultLogged = false;
Brandon Wyman3c208462020-05-13 16:25:58 -0500766 // The PMBus device driver does not allow for writing CLEAR_FAULTS
767 // directly. However, the pmbus hwmon device driver code will send a
768 // CLEAR_FAULTS after reading from any of the hwmon "files" in sysfs, so
769 // reading in1_input should result in clearing the fault bits in
770 // STATUS_BYTE/STATUS_WORD.
771 // I do not care what the return value is.
Brandon Wyman11151532020-11-10 13:45:57 -0600772 if (present)
Brandon Wyman3c208462020-05-13 16:25:58 -0500773 {
Brandon Wymane3f7ad22021-12-21 20:27:45 +0000774 clearFaultFlags();
Matt Spinler0975eaf2022-02-14 15:38:30 -0600775 checkAvailability();
Brandon Wyman9564e942020-11-10 14:01:42 -0600776 readFail = 0;
Brandon Wyman9564e942020-11-10 14:01:42 -0600777
Brandon Wyman11151532020-11-10 13:45:57 -0600778 try
779 {
Brandon Wyman3225a452022-03-18 18:51:49 +0000780 clearVinUVFault();
Brandon Wyman11151532020-11-10 13:45:57 -0600781 static_cast<void>(
782 pmbusIntf->read("in1_input", phosphor::pmbus::Type::Hwmon));
783 }
Patrick Williamsc1d4de52021-10-06 12:45:57 -0500784 catch (const ReadFailure& e)
Brandon Wyman11151532020-11-10 13:45:57 -0600785 {
786 // Since I do not care what the return value is, I really do not
B. J. Wyman681b2a32021-04-20 22:31:22 +0000787 // care much if it gets a ReadFailure either. However, this
788 // should not prevent the application from continuing to run, so
789 // catching the read failure.
Brandon Wyman11151532020-11-10 13:45:57 -0600790 }
Brandon Wyman3c208462020-05-13 16:25:58 -0500791 }
792}
793
Patrick Williams7354ce62022-07-22 19:26:56 -0500794void PowerSupply::inventoryChanged(sdbusplus::message_t& msg)
Brandon Wymanaed1f752019-11-25 18:10:52 -0600795{
796 std::string msgSensor;
Patrick Williamsabe49412020-05-13 17:59:47 -0500797 std::map<std::string, std::variant<uint32_t, bool>> msgData;
Brandon Wymanaed1f752019-11-25 18:10:52 -0600798 msg.read(msgSensor, msgData);
799
800 // Check if it was the Present property that changed.
801 auto valPropMap = msgData.find(PRESENT_PROP);
802 if (valPropMap != msgData.end())
803 {
804 if (std::get<bool>(valPropMap->second))
805 {
806 present = true;
B. J. Wyman681b2a32021-04-20 22:31:22 +0000807 // TODO: Immediately trying to read or write the "files" causes
808 // read or write failures.
Brandon Wyman1d7a7df2020-03-26 10:14:05 -0500809 using namespace std::chrono_literals;
810 std::this_thread::sleep_for(20ms);
Brandon Wyman9564e942020-11-10 14:01:42 -0600811 pmbusIntf->findHwmonDir();
Brandon Wyman59a35792020-06-04 12:37:40 -0500812 onOffConfig(phosphor::pmbus::ON_OFF_CONFIG_CONTROL_PIN_ONLY);
Brandon Wymanaed1f752019-11-25 18:10:52 -0600813 clearFaults();
Brandon Wyman1d7a7df2020-03-26 10:14:05 -0500814 updateInventory();
Brandon Wymanaed1f752019-11-25 18:10:52 -0600815 }
816 else
817 {
818 present = false;
819
820 // Clear out the now outdated inventory properties
821 updateInventory();
822 }
Matt Spinler0975eaf2022-02-14 15:38:30 -0600823 checkAvailability();
Brandon Wymanaed1f752019-11-25 18:10:52 -0600824 }
825}
826
Patrick Williams7354ce62022-07-22 19:26:56 -0500827void PowerSupply::inventoryAdded(sdbusplus::message_t& msg)
Brandon Wyman9a507db2021-02-25 16:15:22 -0600828{
829 sdbusplus::message::object_path path;
830 msg.read(path);
831 // Make sure the signal is for the PSU inventory path
832 if (path == inventoryPath)
833 {
834 std::map<std::string, std::map<std::string, std::variant<bool>>>
835 interfaces;
836 // Get map of interfaces and their properties
837 msg.read(interfaces);
838
839 auto properties = interfaces.find(INVENTORY_IFACE);
840 if (properties != interfaces.end())
841 {
842 auto property = properties->second.find(PRESENT_PROP);
843 if (property != properties->second.end())
844 {
845 present = std::get<bool>(property->second);
846
847 log<level::INFO>(fmt::format("Power Supply {} Present {}",
848 inventoryPath, present)
849 .c_str());
850
851 updateInventory();
Matt Spinler0975eaf2022-02-14 15:38:30 -0600852 checkAvailability();
Brandon Wyman9a507db2021-02-25 16:15:22 -0600853 }
854 }
855 }
856}
857
Brandon Wyman8393f462022-06-28 16:06:46 +0000858auto PowerSupply::readVPDValue(const std::string& vpdName,
859 const phosphor::pmbus::Type& type,
860 const std::size_t& vpdSize)
861{
862 std::string vpdValue;
Patrick Williams48781ae2023-05-10 07:50:50 -0500863 const std::regex illegalVPDRegex = std::regex("[^[:alnum:]]",
864 std::regex::basic);
Brandon Wyman8393f462022-06-28 16:06:46 +0000865
866 try
867 {
868 vpdValue = pmbusIntf->readString(vpdName, type);
869 }
870 catch (const ReadFailure& e)
871 {
872 // Ignore the read failure, let pmbus code indicate failure,
873 // path...
874 // TODO - ibm918
875 // https://github.com/openbmc/docs/blob/master/designs/vpd-collection.md
876 // The BMC must log errors if any of the VPD cannot be properly
877 // parsed or fails ECC checks.
878 }
879
880 if (vpdValue.size() != vpdSize)
881 {
882 log<level::INFO>(fmt::format("{} {} resize needed. size: {}", shortName,
883 vpdName, vpdValue.size())
884 .c_str());
885 vpdValue.resize(vpdSize, ' ');
886 }
887
Brandon Wyman056935c2022-06-24 23:05:09 +0000888 // Replace any illegal values with space(s).
889 std::regex_replace(vpdValue.begin(), vpdValue.begin(), vpdValue.end(),
890 illegalVPDRegex, " ");
891
Brandon Wyman8393f462022-06-28 16:06:46 +0000892 return vpdValue;
893}
894
Brandon Wyman1d7a7df2020-03-26 10:14:05 -0500895void PowerSupply::updateInventory()
896{
897 using namespace phosphor::pmbus;
898
Chanh Nguyenc12c53b2021-04-06 17:24:47 +0700899#if IBM_VPD
Brandon Wyman1d7a7df2020-03-26 10:14:05 -0500900 std::string pn;
901 std::string fn;
902 std::string header;
903 std::string sn;
Brandon Wyman8393f462022-06-28 16:06:46 +0000904 // The IBM power supply splits the full serial number into two parts.
905 // Each part is 6 bytes long, which should match up with SN_KW_SIZE.
906 const auto HEADER_SIZE = 6;
907 const auto SERIAL_SIZE = 6;
908 // The IBM PSU firmware version size is a bit complicated. It was originally
909 // 1-byte, per command. It was later expanded to 2-bytes per command, then
910 // up to 8-bytes per command. The device driver only reads up to 2 bytes per
911 // command, but combines all three of the 2-byte reads, or all 4 of the
912 // 1-byte reads into one string. So, the maximum size expected is 6 bytes.
Shawn McCarney983c9892022-10-12 10:49:47 -0500913 // However, it is formatted by the driver as a hex string with two ASCII
914 // characters per byte. So the maximum ASCII string size is 12.
Faisal Awada9582d9c2023-07-11 09:31:22 -0500915 const auto IBMCFFPS_FW_VERSION_SIZE = 12;
916 const auto ACBEL_FSG032_FW_VERSION_SIZE = 6;
Brandon Wyman8393f462022-06-28 16:06:46 +0000917
Brandon Wyman1d7a7df2020-03-26 10:14:05 -0500918 using PropertyMap =
George Liu070c1bc2020-10-12 11:28:01 +0800919 std::map<std::string,
920 std::variant<std::string, std::vector<uint8_t>, bool>>;
Brandon Wyman1d7a7df2020-03-26 10:14:05 -0500921 PropertyMap assetProps;
George Liu070c1bc2020-10-12 11:28:01 +0800922 PropertyMap operProps;
Brandon Wyman1d7a7df2020-03-26 10:14:05 -0500923 PropertyMap versionProps;
924 PropertyMap ipzvpdDINFProps;
925 PropertyMap ipzvpdVINIProps;
926 using InterfaceMap = std::map<std::string, PropertyMap>;
927 InterfaceMap interfaces;
928 using ObjectMap = std::map<sdbusplus::message::object_path, InterfaceMap>;
929 ObjectMap object;
930#endif
B. J. Wyman681b2a32021-04-20 22:31:22 +0000931 log<level::DEBUG>(
932 fmt::format("updateInventory() inventoryPath: {}", inventoryPath)
933 .c_str());
Brandon Wyman1d7a7df2020-03-26 10:14:05 -0500934
935 if (present)
936 {
937 // TODO: non-IBM inventory updates?
938
Chanh Nguyenc12c53b2021-04-06 17:24:47 +0700939#if IBM_VPD
Faisal Awada9582d9c2023-07-11 09:31:22 -0500940 if (driverName == ACBEL_FSG032_DD_NAME)
faisaladed7a02023-05-10 14:59:01 -0500941 {
942 getPsuVpdFromDbus("CC", modelName);
943 getPsuVpdFromDbus("PN", pn);
944 getPsuVpdFromDbus("FN", fn);
945 getPsuVpdFromDbus("SN", sn);
946 assetProps.emplace(SN_PROP, sn);
Faisal Awada9582d9c2023-07-11 09:31:22 -0500947 fwVersion = readVPDValue(FW_VERSION, Type::Debug,
948 ACBEL_FSG032_FW_VERSION_SIZE);
949 versionProps.emplace(VERSION_PROP, fwVersion);
faisaladed7a02023-05-10 14:59:01 -0500950 }
951 else
952 {
953 modelName = readVPDValue(CCIN, Type::HwmonDeviceDebug, CC_KW_SIZE);
954 pn = readVPDValue(PART_NUMBER, Type::Debug, PN_KW_SIZE);
955 fn = readVPDValue(FRU_NUMBER, Type::Debug, FN_KW_SIZE);
956
957 header = readVPDValue(SERIAL_HEADER, Type::Debug, HEADER_SIZE);
958 sn = readVPDValue(SERIAL_NUMBER, Type::Debug, SERIAL_SIZE);
959 assetProps.emplace(SN_PROP, header + sn);
Faisal Awada9582d9c2023-07-11 09:31:22 -0500960 fwVersion = readVPDValue(FW_VERSION, Type::HwmonDeviceDebug,
961 IBMCFFPS_FW_VERSION_SIZE);
962 versionProps.emplace(VERSION_PROP, fwVersion);
faisaladed7a02023-05-10 14:59:01 -0500963 }
964
Brandon Wyman8393f462022-06-28 16:06:46 +0000965 assetProps.emplace(MODEL_PROP, modelName);
Brandon Wyman8393f462022-06-28 16:06:46 +0000966 assetProps.emplace(PN_PROP, pn);
Brandon Wyman8393f462022-06-28 16:06:46 +0000967 assetProps.emplace(SPARE_PN_PROP, fn);
Brandon Wyman1d7a7df2020-03-26 10:14:05 -0500968
Brandon Wyman8393f462022-06-28 16:06:46 +0000969 ipzvpdVINIProps.emplace(
970 "CC", std::vector<uint8_t>(modelName.begin(), modelName.end()));
Brandon Wyman1d7a7df2020-03-26 10:14:05 -0500971 ipzvpdVINIProps.emplace("PN",
972 std::vector<uint8_t>(pn.begin(), pn.end()));
973 ipzvpdVINIProps.emplace("FN",
974 std::vector<uint8_t>(fn.begin(), fn.end()));
Brandon Wyman33d492f2022-03-23 20:45:17 +0000975 std::string header_sn = header + sn;
Brandon Wyman1d7a7df2020-03-26 10:14:05 -0500976 ipzvpdVINIProps.emplace(
977 "SN", std::vector<uint8_t>(header_sn.begin(), header_sn.end()));
978 std::string description = "IBM PS";
979 ipzvpdVINIProps.emplace(
980 "DR", std::vector<uint8_t>(description.begin(), description.end()));
981
Ben Tynerf8d8c462022-01-27 16:09:45 -0600982 // Populate the VINI Resource Type (RT) keyword
983 ipzvpdVINIProps.emplace("RT", std::vector<uint8_t>{'V', 'I', 'N', 'I'});
984
Brandon Wyman1d7a7df2020-03-26 10:14:05 -0500985 // Update the Resource Identifier (RI) keyword
986 // 2 byte FRC: 0x0003
987 // 2 byte RID: 0x1000, 0x1001...
988 std::uint8_t num = std::stoul(
989 inventoryPath.substr(inventoryPath.size() - 1, 1), nullptr, 0);
990 std::vector<uint8_t> ri{0x00, 0x03, 0x10, num};
991 ipzvpdDINFProps.emplace("RI", ri);
992
993 // Fill in the FRU Label (FL) keyword.
994 std::string fl = "E";
995 fl.push_back(inventoryPath.back());
996 fl.resize(FL_KW_SIZE, ' ');
997 ipzvpdDINFProps.emplace("FL",
998 std::vector<uint8_t>(fl.begin(), fl.end()));
999
Ben Tynerf8d8c462022-01-27 16:09:45 -06001000 // Populate the DINF Resource Type (RT) keyword
1001 ipzvpdDINFProps.emplace("RT", std::vector<uint8_t>{'D', 'I', 'N', 'F'});
1002
Brandon Wyman1d7a7df2020-03-26 10:14:05 -05001003 interfaces.emplace(ASSET_IFACE, std::move(assetProps));
1004 interfaces.emplace(VERSION_IFACE, std::move(versionProps));
1005 interfaces.emplace(DINF_IFACE, std::move(ipzvpdDINFProps));
1006 interfaces.emplace(VINI_IFACE, std::move(ipzvpdVINIProps));
1007
George Liu070c1bc2020-10-12 11:28:01 +08001008 // Update the Functional
1009 operProps.emplace(FUNCTIONAL_PROP, present);
1010 interfaces.emplace(OPERATIONAL_STATE_IFACE, std::move(operProps));
1011
Brandon Wyman1d7a7df2020-03-26 10:14:05 -05001012 auto path = inventoryPath.substr(strlen(INVENTORY_OBJ_PATH));
1013 object.emplace(path, std::move(interfaces));
1014
1015 try
1016 {
Patrick Williams48781ae2023-05-10 07:50:50 -05001017 auto service = util::getService(INVENTORY_OBJ_PATH,
1018 INVENTORY_MGR_IFACE, bus);
Brandon Wyman1d7a7df2020-03-26 10:14:05 -05001019
1020 if (service.empty())
1021 {
1022 log<level::ERR>("Unable to get inventory manager service");
1023 return;
1024 }
1025
Patrick Williams48781ae2023-05-10 07:50:50 -05001026 auto method = bus.new_method_call(service.c_str(),
1027 INVENTORY_OBJ_PATH,
1028 INVENTORY_MGR_IFACE, "Notify");
Brandon Wyman1d7a7df2020-03-26 10:14:05 -05001029
1030 method.append(std::move(object));
1031
1032 auto reply = bus.call(method);
1033 }
Patrick Williamsc1d4de52021-10-06 12:45:57 -05001034 catch (const std::exception& e)
Brandon Wyman1d7a7df2020-03-26 10:14:05 -05001035 {
Jay Meyer6a3fd2c2020-08-25 16:37:16 -05001036 log<level::ERR>(
1037 std::string(e.what() + std::string(" PATH=") + inventoryPath)
1038 .c_str());
Brandon Wyman1d7a7df2020-03-26 10:14:05 -05001039 }
1040#endif
1041 }
1042}
1043
Brandon Wymanae35ac52022-05-23 22:33:40 +00001044auto PowerSupply::getMaxPowerOut() const
1045{
1046 using namespace phosphor::pmbus;
1047
1048 auto maxPowerOut = 0;
1049
1050 if (present)
1051 {
1052 try
1053 {
1054 // Read max_power_out, should be direct format
Patrick Williams48781ae2023-05-10 07:50:50 -05001055 auto maxPowerOutStr = pmbusIntf->readString(MFR_POUT_MAX,
1056 Type::HwmonDeviceDebug);
Brandon Wymanae35ac52022-05-23 22:33:40 +00001057 log<level::INFO>(fmt::format("{} MFR_POUT_MAX read {}", shortName,
1058 maxPowerOutStr)
1059 .c_str());
1060 maxPowerOut = std::stod(maxPowerOutStr);
1061 }
1062 catch (const std::exception& e)
1063 {
1064 log<level::ERR>(fmt::format("{} MFR_POUT_MAX read error: {}",
1065 shortName, e.what())
1066 .c_str());
1067 }
1068 }
1069
1070 return maxPowerOut;
1071}
1072
Matt Spinler592bd272023-08-30 11:00:01 -05001073void PowerSupply::setupSensors()
1074{
1075 setupInputPowerPeakSensor();
1076}
1077
1078void PowerSupply::setupInputPowerPeakSensor()
1079{
1080 if (peakInputPowerSensor || !present ||
1081 (bindPath.string().find(IBMCFFPS_DD_NAME) == std::string::npos))
1082 {
1083 return;
1084 }
1085
1086 // This PSU has problems with the input_history command
1087 if (getMaxPowerOut() == phosphor::pmbus::IBM_CFFPS_1400W)
1088 {
1089 return;
1090 }
1091
1092 auto sensorPath =
1093 fmt::format("/xyz/openbmc_project/sensors/power/ps{}_input_power_peak",
1094 shortName.back());
1095
1096 peakInputPowerSensor = std::make_unique<PowerSensorObject>(
1097 bus, sensorPath.c_str(), PowerSensorObject::action::defer_emit);
1098
1099 // The others can remain at the defaults.
1100 peakInputPowerSensor->functional(true, true);
1101 peakInputPowerSensor->available(true, true);
1102 peakInputPowerSensor->value(0, true);
1103 peakInputPowerSensor->unit(
1104 sdbusplus::xyz::openbmc_project::Sensor::server::Value::Unit::Watts,
1105 true);
1106
1107 auto associations = getSensorAssociations();
1108 peakInputPowerSensor->associations(associations, true);
1109
1110 peakInputPowerSensor->emit_object_added();
1111}
1112
1113void PowerSupply::setSensorsNotAvailable()
1114{
1115 if (peakInputPowerSensor)
1116 {
1117 peakInputPowerSensor->value(std::numeric_limits<double>::quiet_NaN());
1118 peakInputPowerSensor->available(false);
1119 }
1120}
1121
Matt Spinler592bd272023-08-30 11:00:01 -05001122void PowerSupply::monitorSensors()
1123{
1124 monitorPeakInputPowerSensor();
1125}
1126
1127void PowerSupply::monitorPeakInputPowerSensor()
1128{
1129 if (!peakInputPowerSensor)
1130 {
1131 return;
1132 }
1133
1134 constexpr size_t recordSize = 5;
1135 std::vector<uint8_t> data;
1136
1137 // Get the peak input power with input history command.
1138 // New data only shows up every 30s, but just try to read it every 1s
1139 // anyway so we always have the most up to date value.
1140 try
1141 {
1142 data = pmbusIntf->readBinary(INPUT_HISTORY,
1143 pmbus::Type::HwmonDeviceDebug, recordSize);
1144 }
1145 catch (const ReadFailure& e)
1146 {
1147 peakInputPowerSensor->value(std::numeric_limits<double>::quiet_NaN());
1148 peakInputPowerSensor->functional(false);
1149 throw;
1150 }
1151
1152 if (data.size() != recordSize)
1153 {
1154 log<level::DEBUG>(
1155 fmt::format("Input history command returned {} bytes instead of 5",
1156 data.size())
1157 .c_str());
1158 peakInputPowerSensor->value(std::numeric_limits<double>::quiet_NaN());
1159 peakInputPowerSensor->functional(false);
1160 return;
1161 }
1162
1163 // The format is SSAAAAPPPP:
1164 // SS = packet sequence number
1165 // AAAA = average power (linear format, little endian)
1166 // PPPP = peak power (linear format, little endian)
1167 auto peak = static_cast<uint16_t>(data[4]) << 8 | data[3];
1168 auto peakPower = linearToInteger(peak);
1169
1170 peakInputPowerSensor->value(peakPower);
1171 peakInputPowerSensor->functional(true);
1172 peakInputPowerSensor->available(true);
1173}
1174
Adriana Kobylak4175ffb2021-08-02 14:51:05 +00001175void PowerSupply::getInputVoltage(double& actualInputVoltage,
1176 int& inputVoltage) const
1177{
1178 using namespace phosphor::pmbus;
1179
1180 actualInputVoltage = in_input::VIN_VOLTAGE_0;
1181 inputVoltage = in_input::VIN_VOLTAGE_0;
1182
1183 if (present)
1184 {
1185 try
1186 {
1187 // Read input voltage in millivolts
1188 auto inputVoltageStr = pmbusIntf->readString(READ_VIN, Type::Hwmon);
1189
1190 // Convert to volts
1191 actualInputVoltage = std::stod(inputVoltageStr) / 1000;
1192
1193 // Calculate the voltage based on voltage thresholds
1194 if (actualInputVoltage < in_input::VIN_VOLTAGE_MIN)
1195 {
1196 inputVoltage = in_input::VIN_VOLTAGE_0;
1197 }
1198 else if (actualInputVoltage < in_input::VIN_VOLTAGE_110_THRESHOLD)
1199 {
1200 inputVoltage = in_input::VIN_VOLTAGE_110;
1201 }
1202 else
1203 {
1204 inputVoltage = in_input::VIN_VOLTAGE_220;
1205 }
1206 }
1207 catch (const std::exception& e)
1208 {
1209 log<level::ERR>(
Brandon Wyman321a6152022-03-19 00:11:44 +00001210 fmt::format("{} READ_VIN read error: {}", shortName, e.what())
1211 .c_str());
Adriana Kobylak4175ffb2021-08-02 14:51:05 +00001212 }
1213 }
1214}
1215
Matt Spinler0975eaf2022-02-14 15:38:30 -06001216void PowerSupply::checkAvailability()
1217{
1218 bool origAvailability = available;
George Liu9464c422023-02-27 14:30:27 +08001219 bool faulted = isPowerOn() && (hasPSKillFault() || hasIoutOCFault());
1220 available = present && !hasInputFault() && !hasVINUVFault() && !faulted;
Matt Spinler0975eaf2022-02-14 15:38:30 -06001221
1222 if (origAvailability != available)
1223 {
1224 auto invpath = inventoryPath.substr(strlen(INVENTORY_OBJ_PATH));
1225 phosphor::power::psu::setAvailable(bus, invpath, available);
Matt Spinlerca1e9ea2022-02-18 14:03:08 -06001226
1227 // Check if the health rollup needs to change based on the
1228 // new availability value.
1229 phosphor::power::psu::handleChassisHealthRollup(bus, inventoryPath,
1230 !available);
Matt Spinler0975eaf2022-02-14 15:38:30 -06001231 }
1232}
1233
Matt Spinlera068f422023-03-10 13:06:49 -06001234void PowerSupply::setInputVoltageRating()
1235{
1236 if (!present)
1237 {
Matt Spinler1aaf9f82023-03-22 09:52:22 -05001238 if (inputVoltageRatingIface)
1239 {
1240 inputVoltageRatingIface->value(0);
1241 inputVoltageRatingIface.reset();
1242 }
Matt Spinlera068f422023-03-10 13:06:49 -06001243 return;
1244 }
1245
1246 double inputVoltageValue{};
1247 int inputVoltageRating{};
1248 getInputVoltage(inputVoltageValue, inputVoltageRating);
1249
1250 if (!inputVoltageRatingIface)
1251 {
1252 auto path = fmt::format(
1253 "/xyz/openbmc_project/sensors/voltage/ps{}_input_voltage_rating",
1254 shortName.back());
1255
1256 inputVoltageRatingIface = std::make_unique<SensorObject>(
1257 bus, path.c_str(), SensorObject::action::defer_emit);
1258
1259 // Leave other properties at their defaults
1260 inputVoltageRatingIface->unit(SensorInterface::Unit::Volts, true);
1261 inputVoltageRatingIface->value(static_cast<double>(inputVoltageRating),
1262 true);
1263
1264 inputVoltageRatingIface->emit_object_added();
1265 }
1266 else
1267 {
1268 inputVoltageRatingIface->value(static_cast<double>(inputVoltageRating));
1269 }
1270}
1271
faisaladed7a02023-05-10 14:59:01 -05001272void PowerSupply::getPsuVpdFromDbus(const std::string& keyword,
1273 std::string& vpdStr)
1274{
1275 try
1276 {
1277 std::vector<uint8_t> value;
1278 vpdStr.clear();
1279 util::getProperty(VINI_IFACE, keyword, inventoryPath,
1280 INVENTORY_MGR_IFACE, bus, value);
1281 for (char c : value)
1282 {
1283 vpdStr += c;
1284 }
1285 }
1286 catch (const sdbusplus::exception_t& e)
1287 {
1288 log<level::ERR>(
1289 fmt::format("Failed getProperty error: {}", e.what()).c_str());
1290 }
1291}
Matt Spinler592bd272023-08-30 11:00:01 -05001292
1293double PowerSupply::linearToInteger(uint16_t data)
1294{
1295 // The exponent is the first 5 bits, followed by 11 bits of mantissa.
1296 int8_t exponent = (data & 0xF800) >> 11;
1297 int16_t mantissa = (data & 0x07FF);
1298
1299 // If exponent's MSB on, then it's negative.
1300 // Convert from two's complement.
1301 if (exponent & 0x10)
1302 {
1303 exponent = (~exponent) & 0x1F;
1304 exponent = (exponent + 1) * -1;
1305 }
1306
1307 // If mantissa's MSB on, then it's negative.
1308 // Convert from two's complement.
1309 if (mantissa & 0x400)
1310 {
1311 mantissa = (~mantissa) & 0x07FF;
1312 mantissa = (mantissa + 1) * -1;
1313 }
1314
1315 auto value = static_cast<double>(mantissa) * pow(2, exponent);
1316 return value;
1317}
1318
1319std::vector<AssociationTuple> PowerSupply::getSensorAssociations()
1320{
1321 std::vector<AssociationTuple> associations;
1322
1323 associations.emplace_back("inventory", "sensors", inventoryPath);
1324
1325 auto chassis = getChassis(bus, inventoryPath);
1326 associations.emplace_back("chassis", "all_sensors", std::move(chassis));
1327
1328 return associations;
1329}
1330
Brandon Wyman3f1242f2020-01-28 13:11:25 -06001331} // namespace phosphor::power::psu