blob: 6e28ca865a4602e053bce49985b53aad10271da9 [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
Brandon Wymanc3324422022-03-24 20:30:57 +000025// The number of INPUT_HISTORY records to keep on D-Bus.
26// Each record covers a 30-second span. That means two records are needed to
27// cover a minute of time. If we want one (1) hour of data, that would be 120
28// records.
29constexpr auto INPUT_HISTORY_MAX_RECORDS = 120;
30
Brandon Wymanaed1f752019-11-25 18:10:52 -060031using namespace phosphor::logging;
Brandon Wyman3f1242f2020-01-28 13:11:25 -060032using namespace sdbusplus::xyz::openbmc_project::Common::Device::Error;
Brandon Wymanaed1f752019-11-25 18:10:52 -060033
Patrick Williams7354ce62022-07-22 19:26:56 -050034PowerSupply::PowerSupply(sdbusplus::bus_t& bus, const std::string& invpath,
B. J. Wyman681b2a32021-04-20 22:31:22 +000035 std::uint8_t i2cbus, std::uint16_t i2caddr,
Brandon Wymanc3324422022-03-24 20:30:57 +000036 const std::string& driver,
George Liu9464c422023-02-27 14:30:27 +080037 const std::string& gpioLineName,
38 std::function<bool()>&& callback) :
Brandon Wyman510acaa2020-11-05 18:32:04 -060039 bus(bus),
George Liu9464c422023-02-27 14:30:27 +080040 inventoryPath(invpath), bindPath("/sys/bus/i2c/drivers/" + driver),
Faisal Awadab66ae502023-04-01 18:30:32 -050041 isPowerOn(std::move(callback)), driverName(driver)
Brandon Wyman510acaa2020-11-05 18:32:04 -060042{
43 if (inventoryPath.empty())
44 {
45 throw std::invalid_argument{"Invalid empty inventoryPath"};
46 }
47
B. J. Wyman681b2a32021-04-20 22:31:22 +000048 if (gpioLineName.empty())
49 {
50 throw std::invalid_argument{"Invalid empty gpioLineName"};
51 }
Brandon Wyman510acaa2020-11-05 18:32:04 -060052
Brandon Wyman321a6152022-03-19 00:11:44 +000053 shortName = findShortName(inventoryPath);
54
55 log<level::DEBUG>(
56 fmt::format("{} gpioLineName: {}", shortName, gpioLineName).c_str());
B. J. Wyman681b2a32021-04-20 22:31:22 +000057 presenceGPIO = createGPIO(gpioLineName);
Brandon Wyman510acaa2020-11-05 18:32:04 -060058
59 std::ostringstream ss;
60 ss << std::hex << std::setw(4) << std::setfill('0') << i2caddr;
61 std::string addrStr = ss.str();
B. J. Wyman681b2a32021-04-20 22:31:22 +000062 std::string busStr = std::to_string(i2cbus);
63 bindDevice = busStr;
64 bindDevice.append("-");
65 bindDevice.append(addrStr);
66
Brandon Wyman510acaa2020-11-05 18:32:04 -060067 pmbusIntf = phosphor::pmbus::createPMBus(i2cbus, addrStr);
68
69 // Get the current state of the Present property.
B. J. Wyman681b2a32021-04-20 22:31:22 +000070 try
71 {
72 updatePresenceGPIO();
73 }
74 catch (...)
75 {
76 // If the above attempt to use the GPIO failed, it likely means that the
77 // GPIOs are in use by the kernel, meaning it is using gpio-keys.
78 // So, I should rely on phosphor-gpio-presence to update D-Bus, and
79 // work that way for power supply presence.
80 presenceGPIO = nullptr;
81 // Setup the functions to call when the D-Bus inventory path for the
82 // Present property changes.
83 presentMatch = std::make_unique<sdbusplus::bus::match_t>(
84 bus,
85 sdbusplus::bus::match::rules::propertiesChanged(inventoryPath,
86 INVENTORY_IFACE),
87 [this](auto& msg) { this->inventoryChanged(msg); });
88
89 presentAddedMatch = std::make_unique<sdbusplus::bus::match_t>(
90 bus,
91 sdbusplus::bus::match::rules::interfacesAdded() +
92 sdbusplus::bus::match::rules::argNpath(0, inventoryPath),
93 [this](auto& msg) { this->inventoryAdded(msg); });
94
95 updatePresence();
96 updateInventory();
Brandon Wymanc3324422022-03-24 20:30:57 +000097 setupInputHistory();
Matt Spinler592bd272023-08-30 11:00:01 -050098 setupSensors();
B. J. Wyman681b2a32021-04-20 22:31:22 +000099 }
Matt Spinlera068f422023-03-10 13:06:49 -0600100
101 setInputVoltageRating();
B. J. Wyman681b2a32021-04-20 22:31:22 +0000102}
103
104void PowerSupply::bindOrUnbindDriver(bool present)
105{
106 auto action = (present) ? "bind" : "unbind";
107 auto path = bindPath / action;
108
109 if (present)
110 {
Brandon Wymanb1ee60f2022-03-22 22:37:12 +0000111 std::this_thread::sleep_for(std::chrono::milliseconds(bindDelay));
B. J. Wyman681b2a32021-04-20 22:31:22 +0000112 log<level::INFO>(
113 fmt::format("Binding device driver. path: {} device: {}",
114 path.string(), bindDevice)
115 .c_str());
116 }
117 else
118 {
119 log<level::INFO>(
120 fmt::format("Unbinding device driver. path: {} device: {}",
121 path.string(), bindDevice)
122 .c_str());
123 }
124
125 std::ofstream file;
126
127 file.exceptions(std::ofstream::failbit | std::ofstream::badbit |
128 std::ofstream::eofbit);
129
130 try
131 {
132 file.open(path);
133 file << bindDevice;
134 file.close();
135 }
Patrick Williamsc1d4de52021-10-06 12:45:57 -0500136 catch (const std::exception& e)
B. J. Wyman681b2a32021-04-20 22:31:22 +0000137 {
138 auto err = errno;
139
140 log<level::ERR>(
141 fmt::format("Failed binding or unbinding device. errno={}", err)
142 .c_str());
143 }
Brandon Wyman510acaa2020-11-05 18:32:04 -0600144}
145
Brandon Wymanaed1f752019-11-25 18:10:52 -0600146void PowerSupply::updatePresence()
147{
148 try
149 {
Brandon Wyman3f1242f2020-01-28 13:11:25 -0600150 present = getPresence(bus, inventoryPath);
Brandon Wymanaed1f752019-11-25 18:10:52 -0600151 }
Patrick Williams7354ce62022-07-22 19:26:56 -0500152 catch (const sdbusplus::exception_t& e)
Brandon Wymanaed1f752019-11-25 18:10:52 -0600153 {
154 // Relying on property change or interface added to retry.
155 // Log an informational trace to the journal.
Brandon Wymandf13c3a2020-12-15 14:25:22 -0600156 log<level::INFO>(
157 fmt::format("D-Bus property {} access failure exception",
158 inventoryPath)
159 .c_str());
Brandon Wymanaed1f752019-11-25 18:10:52 -0600160 }
161}
162
B. J. Wyman681b2a32021-04-20 22:31:22 +0000163void PowerSupply::updatePresenceGPIO()
164{
165 bool presentOld = present;
166
167 try
168 {
169 if (presenceGPIO->read() > 0)
170 {
171 present = true;
172 }
173 else
174 {
175 present = false;
176 }
177 }
Patrick Williamsc1d4de52021-10-06 12:45:57 -0500178 catch (const std::exception& e)
B. J. Wyman681b2a32021-04-20 22:31:22 +0000179 {
180 log<level::ERR>(
181 fmt::format("presenceGPIO read fail: {}", e.what()).c_str());
182 throw;
183 }
184
185 if (presentOld != present)
186 {
Brandon Wyman321a6152022-03-19 00:11:44 +0000187 log<level::DEBUG>(fmt::format("{} presentOld: {} present: {}",
188 shortName, presentOld, present)
189 .c_str());
Matt Spinlerca1e9ea2022-02-18 14:03:08 -0600190
191 auto invpath = inventoryPath.substr(strlen(INVENTORY_OBJ_PATH));
Brandon Wyman90d529a2022-03-22 23:02:54 +0000192
193 bindOrUnbindDriver(present);
194 if (present)
195 {
196 // If the power supply was present, then missing, and present again,
197 // the hwmon path may have changed. We will need the correct/updated
198 // path before any reads or writes are attempted.
199 pmbusIntf->findHwmonDir();
200 }
201
Brandon Wyman321a6152022-03-19 00:11:44 +0000202 setPresence(bus, invpath, present, shortName);
Brandon Wymanc3324422022-03-24 20:30:57 +0000203 setupInputHistory();
Matt Spinler592bd272023-08-30 11:00:01 -0500204 setupSensors();
Matt Spinlerca1e9ea2022-02-18 14:03:08 -0600205 updateInventory();
206
Brandon Wyman90d529a2022-03-22 23:02:54 +0000207 // Need Functional to already be correct before calling this.
Matt Spinlerca1e9ea2022-02-18 14:03:08 -0600208 checkAvailability();
209
B. J. Wyman681b2a32021-04-20 22:31:22 +0000210 if (present)
211 {
B. J. Wyman681b2a32021-04-20 22:31:22 +0000212 onOffConfig(phosphor::pmbus::ON_OFF_CONFIG_CONTROL_PIN_ONLY);
213 clearFaults();
Brandon Wyman18a24d92022-04-19 22:48:34 +0000214 // Indicate that the input history data and timestamps between all
215 // the power supplies that are present in the system need to be
216 // synchronized.
217 syncHistoryRequired = true;
B. J. Wyman681b2a32021-04-20 22:31:22 +0000218 }
Matt Spinler592bd272023-08-30 11:00:01 -0500219 else
220 {
221 setSensorsNotAvailable();
222 }
B. J. Wyman681b2a32021-04-20 22:31:22 +0000223 }
224}
225
Brandon Wymanc2203432021-12-21 23:09:48 +0000226void PowerSupply::analyzeCMLFault()
227{
228 if (statusWord & phosphor::pmbus::status_word::CML_FAULT)
229 {
Brandon Wymanc2906f42021-12-21 20:14:56 +0000230 if (cmlFault < DEGLITCH_LIMIT)
Brandon Wymanc2203432021-12-21 23:09:48 +0000231 {
Brandon Wyman9e292ee2022-03-10 22:56:23 +0000232 if (statusWord != statusWordOld)
233 {
Brandon Wyman321a6152022-03-19 00:11:44 +0000234 log<level::ERR>(
235 fmt::format("{} CML fault: STATUS_WORD = {:#06x}, "
236 "STATUS_CML = {:#02x}",
237 shortName, statusWord, statusCML)
238 .c_str());
Brandon Wyman9e292ee2022-03-10 22:56:23 +0000239 }
Brandon Wymanc2906f42021-12-21 20:14:56 +0000240 cmlFault++;
241 }
242 }
243 else
244 {
245 cmlFault = 0;
Brandon Wymanc2203432021-12-21 23:09:48 +0000246 }
247}
248
Brandon Wymane3b0bb02021-12-21 23:16:48 +0000249void PowerSupply::analyzeInputFault()
250{
251 if (statusWord & phosphor::pmbus::status_word::INPUT_FAULT_WARN)
252 {
Brandon Wymanc2906f42021-12-21 20:14:56 +0000253 if (inputFault < DEGLITCH_LIMIT)
Brandon Wymane3b0bb02021-12-21 23:16:48 +0000254 {
Brandon Wyman9e292ee2022-03-10 22:56:23 +0000255 if (statusWord != statusWordOld)
256 {
257 log<level::ERR>(
Brandon Wyman321a6152022-03-19 00:11:44 +0000258 fmt::format("{} INPUT fault: STATUS_WORD = {:#06x}, "
Brandon Wyman9e292ee2022-03-10 22:56:23 +0000259 "STATUS_MFR_SPECIFIC = {:#04x}, "
260 "STATUS_INPUT = {:#04x}",
Brandon Wyman321a6152022-03-19 00:11:44 +0000261 shortName, statusWord, statusMFR, statusInput)
Brandon Wyman9e292ee2022-03-10 22:56:23 +0000262 .c_str());
263 }
Brandon Wymanc2906f42021-12-21 20:14:56 +0000264 inputFault++;
265 }
Brandon Wymane3b0bb02021-12-21 23:16:48 +0000266 }
Brandon Wyman82affd92021-11-24 19:12:49 +0000267
268 // If had INPUT/VIN_UV fault, and now off.
269 // Trace that odd behavior.
270 if (inputFault &&
271 !(statusWord & phosphor::pmbus::status_word::INPUT_FAULT_WARN))
272 {
273 log<level::INFO>(
Brandon Wyman321a6152022-03-19 00:11:44 +0000274 fmt::format("{} INPUT fault cleared: STATUS_WORD = {:#06x}, "
Brandon Wyman6f939a32022-03-10 18:42:20 +0000275 "STATUS_MFR_SPECIFIC = {:#04x}, "
276 "STATUS_INPUT = {:#04x}",
Brandon Wyman321a6152022-03-19 00:11:44 +0000277 shortName, statusWord, statusMFR, statusInput)
Brandon Wyman82affd92021-11-24 19:12:49 +0000278 .c_str());
Brandon Wymanc2906f42021-12-21 20:14:56 +0000279 inputFault = 0;
Brandon Wyman82affd92021-11-24 19:12:49 +0000280 }
Brandon Wymane3b0bb02021-12-21 23:16:48 +0000281}
282
Brandon Wymanc2c87132021-12-21 23:22:18 +0000283void PowerSupply::analyzeVoutOVFault()
284{
285 if (statusWord & phosphor::pmbus::status_word::VOUT_OV_FAULT)
286 {
Brandon Wymanc2906f42021-12-21 20:14:56 +0000287 if (voutOVFault < DEGLITCH_LIMIT)
Brandon Wymanc2c87132021-12-21 23:22:18 +0000288 {
Brandon Wyman9e292ee2022-03-10 22:56:23 +0000289 if (statusWord != statusWordOld)
290 {
291 log<level::ERR>(
Brandon Wyman321a6152022-03-19 00:11:44 +0000292 fmt::format(
293 "{} VOUT_OV_FAULT fault: STATUS_WORD = {:#06x}, "
294 "STATUS_MFR_SPECIFIC = {:#04x}, "
295 "STATUS_VOUT = {:#02x}",
296 shortName, statusWord, statusMFR, statusVout)
Brandon Wyman9e292ee2022-03-10 22:56:23 +0000297 .c_str());
298 }
Brandon Wymanc2c87132021-12-21 23:22:18 +0000299
Brandon Wymanc2906f42021-12-21 20:14:56 +0000300 voutOVFault++;
301 }
302 }
303 else
304 {
305 voutOVFault = 0;
Brandon Wymanc2c87132021-12-21 23:22:18 +0000306 }
307}
308
Brandon Wymana00e7302021-12-21 23:28:29 +0000309void PowerSupply::analyzeIoutOCFault()
310{
311 if (statusWord & phosphor::pmbus::status_word::IOUT_OC_FAULT)
312 {
Brandon Wymanc2906f42021-12-21 20:14:56 +0000313 if (ioutOCFault < DEGLITCH_LIMIT)
Brandon Wymana00e7302021-12-21 23:28:29 +0000314 {
Brandon Wyman9e292ee2022-03-10 22:56:23 +0000315 if (statusWord != statusWordOld)
316 {
317 log<level::ERR>(
Brandon Wyman321a6152022-03-19 00:11:44 +0000318 fmt::format("{} IOUT fault: STATUS_WORD = {:#06x}, "
Brandon Wyman9e292ee2022-03-10 22:56:23 +0000319 "STATUS_MFR_SPECIFIC = {:#04x}, "
320 "STATUS_IOUT = {:#04x}",
Brandon Wyman321a6152022-03-19 00:11:44 +0000321 shortName, statusWord, statusMFR, statusIout)
Brandon Wyman9e292ee2022-03-10 22:56:23 +0000322 .c_str());
323 }
Brandon Wymana00e7302021-12-21 23:28:29 +0000324
Brandon Wymanc2906f42021-12-21 20:14:56 +0000325 ioutOCFault++;
326 }
327 }
328 else
329 {
330 ioutOCFault = 0;
Brandon Wymana00e7302021-12-21 23:28:29 +0000331 }
332}
333
Brandon Wyman08378782021-12-21 23:48:15 +0000334void PowerSupply::analyzeVoutUVFault()
335{
336 if ((statusWord & phosphor::pmbus::status_word::VOUT_FAULT) &&
337 !(statusWord & phosphor::pmbus::status_word::VOUT_OV_FAULT))
338 {
Brandon Wymanc2906f42021-12-21 20:14:56 +0000339 if (voutUVFault < DEGLITCH_LIMIT)
Brandon Wyman08378782021-12-21 23:48:15 +0000340 {
Brandon Wyman9e292ee2022-03-10 22:56:23 +0000341 if (statusWord != statusWordOld)
342 {
343 log<level::ERR>(
Brandon Wyman321a6152022-03-19 00:11:44 +0000344 fmt::format(
345 "{} VOUT_UV_FAULT fault: STATUS_WORD = {:#06x}, "
346 "STATUS_MFR_SPECIFIC = {:#04x}, "
347 "STATUS_VOUT = {:#04x}",
348 shortName, statusWord, statusMFR, statusVout)
Brandon Wyman9e292ee2022-03-10 22:56:23 +0000349 .c_str());
350 }
Brandon Wymanc2906f42021-12-21 20:14:56 +0000351 voutUVFault++;
352 }
353 }
354 else
355 {
356 voutUVFault = 0;
Brandon Wyman08378782021-12-21 23:48:15 +0000357 }
358}
359
Brandon Wymand5d9a222021-12-21 23:59:05 +0000360void PowerSupply::analyzeFanFault()
361{
362 if (statusWord & phosphor::pmbus::status_word::FAN_FAULT)
363 {
Brandon Wymanc2906f42021-12-21 20:14:56 +0000364 if (fanFault < DEGLITCH_LIMIT)
Brandon Wymand5d9a222021-12-21 23:59:05 +0000365 {
Brandon Wyman9e292ee2022-03-10 22:56:23 +0000366 if (statusWord != statusWordOld)
367 {
Brandon Wyman321a6152022-03-19 00:11:44 +0000368 log<level::ERR>(fmt::format("{} FANS fault/warning: "
Brandon Wyman9e292ee2022-03-10 22:56:23 +0000369 "STATUS_WORD = {:#06x}, "
370 "STATUS_MFR_SPECIFIC = {:#04x}, "
371 "STATUS_FANS_1_2 = {:#04x}",
Brandon Wyman321a6152022-03-19 00:11:44 +0000372 shortName, statusWord, statusMFR,
373 statusFans12)
Brandon Wyman9e292ee2022-03-10 22:56:23 +0000374 .c_str());
375 }
Brandon Wymanc2906f42021-12-21 20:14:56 +0000376 fanFault++;
377 }
378 }
379 else
380 {
381 fanFault = 0;
Brandon Wymand5d9a222021-12-21 23:59:05 +0000382 }
383}
384
Brandon Wyman52cb3f22021-12-21 23:02:47 +0000385void PowerSupply::analyzeTemperatureFault()
386{
387 if (statusWord & phosphor::pmbus::status_word::TEMPERATURE_FAULT_WARN)
388 {
Brandon Wymanc2906f42021-12-21 20:14:56 +0000389 if (tempFault < DEGLITCH_LIMIT)
Brandon Wyman52cb3f22021-12-21 23:02:47 +0000390 {
Brandon Wyman9e292ee2022-03-10 22:56:23 +0000391 if (statusWord != statusWordOld)
392 {
Brandon Wyman321a6152022-03-19 00:11:44 +0000393 log<level::ERR>(fmt::format("{} TEMPERATURE fault/warning: "
Brandon Wyman9e292ee2022-03-10 22:56:23 +0000394 "STATUS_WORD = {:#06x}, "
395 "STATUS_MFR_SPECIFIC = {:#04x}, "
396 "STATUS_TEMPERATURE = {:#04x}",
Brandon Wyman321a6152022-03-19 00:11:44 +0000397 shortName, statusWord, statusMFR,
Brandon Wyman9e292ee2022-03-10 22:56:23 +0000398 statusTemperature)
399 .c_str());
400 }
Brandon Wymanc2906f42021-12-21 20:14:56 +0000401 tempFault++;
402 }
403 }
404 else
405 {
406 tempFault = 0;
Brandon Wyman52cb3f22021-12-21 23:02:47 +0000407 }
408}
409
Brandon Wyman993b5542021-12-21 22:55:16 +0000410void PowerSupply::analyzePgoodFault()
411{
412 if ((statusWord & phosphor::pmbus::status_word::POWER_GOOD_NEGATED) ||
413 (statusWord & phosphor::pmbus::status_word::UNIT_IS_OFF))
414 {
Brandon Wyman6d469fd2022-06-15 16:58:21 +0000415 if (pgoodFault < PGOOD_DEGLITCH_LIMIT)
Brandon Wyman993b5542021-12-21 22:55:16 +0000416 {
Brandon Wyman9e292ee2022-03-10 22:56:23 +0000417 if (statusWord != statusWordOld)
418 {
Brandon Wyman321a6152022-03-19 00:11:44 +0000419 log<level::ERR>(fmt::format("{} PGOOD fault: "
Brandon Wyman9e292ee2022-03-10 22:56:23 +0000420 "STATUS_WORD = {:#06x}, "
421 "STATUS_MFR_SPECIFIC = {:#04x}",
Brandon Wyman321a6152022-03-19 00:11:44 +0000422 shortName, statusWord, statusMFR)
Brandon Wyman9e292ee2022-03-10 22:56:23 +0000423 .c_str());
424 }
Brandon Wyman993b5542021-12-21 22:55:16 +0000425 pgoodFault++;
426 }
427 }
428 else
429 {
430 pgoodFault = 0;
431 }
432}
433
Brandon Wyman39ea02b2021-11-23 23:22:23 +0000434void PowerSupply::determineMFRFault()
435{
Faisal Awadab66ae502023-04-01 18:30:32 -0500436 if (bindPath.string().find(IBMCFFPS_DD_NAME) != std::string::npos)
Brandon Wyman39ea02b2021-11-23 23:22:23 +0000437 {
438 // IBM MFR_SPECIFIC[4] is PS_Kill fault
439 if (statusMFR & 0x10)
440 {
Brandon Wymanc2906f42021-12-21 20:14:56 +0000441 if (psKillFault < DEGLITCH_LIMIT)
442 {
443 psKillFault++;
444 }
445 }
446 else
447 {
448 psKillFault = 0;
Brandon Wyman39ea02b2021-11-23 23:22:23 +0000449 }
450 // IBM MFR_SPECIFIC[6] is 12Vcs fault.
451 if (statusMFR & 0x40)
452 {
Brandon Wymanc2906f42021-12-21 20:14:56 +0000453 if (ps12VcsFault < DEGLITCH_LIMIT)
454 {
455 ps12VcsFault++;
456 }
457 }
458 else
459 {
460 ps12VcsFault = 0;
Brandon Wyman39ea02b2021-11-23 23:22:23 +0000461 }
462 // IBM MFR_SPECIFIC[7] is 12V Current-Share fault.
463 if (statusMFR & 0x80)
464 {
Brandon Wymanc2906f42021-12-21 20:14:56 +0000465 if (psCS12VFault < DEGLITCH_LIMIT)
466 {
467 psCS12VFault++;
468 }
469 }
470 else
471 {
472 psCS12VFault = 0;
Brandon Wyman39ea02b2021-11-23 23:22:23 +0000473 }
474 }
475}
476
Brandon Wyman6c2ac392021-12-21 22:23:06 +0000477void PowerSupply::analyzeMFRFault()
478{
479 if (statusWord & phosphor::pmbus::status_word::MFR_SPECIFIC_FAULT)
480 {
Brandon Wymanc2906f42021-12-21 20:14:56 +0000481 if (mfrFault < DEGLITCH_LIMIT)
Brandon Wyman6c2ac392021-12-21 22:23:06 +0000482 {
Brandon Wyman9e292ee2022-03-10 22:56:23 +0000483 if (statusWord != statusWordOld)
484 {
Brandon Wyman321a6152022-03-19 00:11:44 +0000485 log<level::ERR>(fmt::format("{} MFR fault: "
Brandon Wyman9e292ee2022-03-10 22:56:23 +0000486 "STATUS_WORD = {:#06x} "
487 "STATUS_MFR_SPECIFIC = {:#04x}",
Brandon Wyman321a6152022-03-19 00:11:44 +0000488 shortName, statusWord, statusMFR)
Brandon Wyman9e292ee2022-03-10 22:56:23 +0000489 .c_str());
490 }
Brandon Wymanc2906f42021-12-21 20:14:56 +0000491 mfrFault++;
Brandon Wyman6c2ac392021-12-21 22:23:06 +0000492 }
493
Brandon Wyman6c2ac392021-12-21 22:23:06 +0000494 determineMFRFault();
495 }
Brandon Wymanc2906f42021-12-21 20:14:56 +0000496 else
497 {
498 mfrFault = 0;
499 }
Brandon Wyman6c2ac392021-12-21 22:23:06 +0000500}
501
Brandon Wymanf087f472021-12-22 00:04:27 +0000502void PowerSupply::analyzeVinUVFault()
503{
504 if (statusWord & phosphor::pmbus::status_word::VIN_UV_FAULT)
505 {
Brandon Wymanc2906f42021-12-21 20:14:56 +0000506 if (vinUVFault < DEGLITCH_LIMIT)
Brandon Wymanf087f472021-12-22 00:04:27 +0000507 {
Brandon Wyman9e292ee2022-03-10 22:56:23 +0000508 if (statusWord != statusWordOld)
509 {
510 log<level::ERR>(
Brandon Wyman321a6152022-03-19 00:11:44 +0000511 fmt::format("{} VIN_UV fault: STATUS_WORD = {:#06x}, "
Brandon Wyman9e292ee2022-03-10 22:56:23 +0000512 "STATUS_MFR_SPECIFIC = {:#04x}, "
513 "STATUS_INPUT = {:#04x}",
Brandon Wyman321a6152022-03-19 00:11:44 +0000514 shortName, statusWord, statusMFR, statusInput)
Brandon Wyman9e292ee2022-03-10 22:56:23 +0000515 .c_str());
516 }
Brandon Wymanc2906f42021-12-21 20:14:56 +0000517 vinUVFault++;
Brandon Wymanf087f472021-12-22 00:04:27 +0000518 }
Jim Wright4ab86562022-11-18 14:05:46 -0600519 // Remember that this PSU has seen an AC fault
520 acFault = AC_FAULT_LIMIT;
Brandon Wymanf087f472021-12-22 00:04:27 +0000521 }
Jim Wright7f9288c2022-12-08 11:57:04 -0600522 else
Brandon Wyman82affd92021-11-24 19:12:49 +0000523 {
Jim Wright7f9288c2022-12-08 11:57:04 -0600524 if (vinUVFault != 0)
525 {
526 log<level::INFO>(
527 fmt::format("{} VIN_UV fault cleared: STATUS_WORD = {:#06x}, "
528 "STATUS_MFR_SPECIFIC = {:#04x}, "
529 "STATUS_INPUT = {:#04x}",
530 shortName, statusWord, statusMFR, statusInput)
531 .c_str());
532 vinUVFault = 0;
533 }
Jim Wright4ab86562022-11-18 14:05:46 -0600534 // No AC fail, decrement counter
Jim Wright7f9288c2022-12-08 11:57:04 -0600535 if (acFault != 0)
Jim Wright4ab86562022-11-18 14:05:46 -0600536 {
537 --acFault;
538 }
Brandon Wyman82affd92021-11-24 19:12:49 +0000539 }
Brandon Wymanf087f472021-12-22 00:04:27 +0000540}
541
Brandon Wyman3f1242f2020-01-28 13:11:25 -0600542void PowerSupply::analyze()
543{
544 using namespace phosphor::pmbus;
545
B. J. Wyman681b2a32021-04-20 22:31:22 +0000546 if (presenceGPIO)
547 {
548 updatePresenceGPIO();
549 }
550
Brandon Wyman32453e92021-12-15 19:00:14 +0000551 if (present)
Brandon Wyman3f1242f2020-01-28 13:11:25 -0600552 {
553 try
554 {
Brandon Wyman9e292ee2022-03-10 22:56:23 +0000555 statusWordOld = statusWord;
Brandon Wyman32453e92021-12-15 19:00:14 +0000556 statusWord = pmbusIntf->read(STATUS_WORD, Type::Debug,
557 (readFail < LOG_LIMIT));
Brandon Wymanf65c4062020-08-19 13:15:53 -0500558 // Read worked, reset the fail count.
559 readFail = 0;
Brandon Wyman3f1242f2020-01-28 13:11:25 -0600560
561 if (statusWord)
562 {
Brandon Wymanf07bc792021-10-12 19:00:35 +0000563 statusInput = pmbusIntf->read(STATUS_INPUT, Type::Debug);
Faisal Awadab66ae502023-04-01 18:30:32 -0500564 if (bindPath.string().find(IBMCFFPS_DD_NAME) !=
565 std::string::npos)
566 {
567 statusMFR = pmbusIntf->read(STATUS_MFR, Type::Debug);
568 }
Brandon Wyman85c7bf42021-10-19 22:28:48 +0000569 statusCML = pmbusIntf->read(STATUS_CML, Type::Debug);
Brandon Wyman6710ba22021-10-27 17:39:31 +0000570 auto status0Vout = pmbusIntf->insertPageNum(STATUS_VOUT, 0);
571 statusVout = pmbusIntf->read(status0Vout, Type::Debug);
Brandon Wymanb10b3be2021-11-09 22:12:15 +0000572 statusIout = pmbusIntf->read(STATUS_IOUT, Type::Debug);
Brandon Wyman7ee4d7e2021-11-19 20:48:23 +0000573 statusFans12 = pmbusIntf->read(STATUS_FANS_1_2, Type::Debug);
Patrick Williams48781ae2023-05-10 07:50:50 -0500574 statusTemperature = pmbusIntf->read(STATUS_TEMPERATURE,
575 Type::Debug);
Brandon Wyman9ddc6222021-10-28 17:28:01 +0000576
Brandon Wymanc2203432021-12-21 23:09:48 +0000577 analyzeCMLFault();
Brandon Wyman85c7bf42021-10-19 22:28:48 +0000578
Brandon Wymane3b0bb02021-12-21 23:16:48 +0000579 analyzeInputFault();
Brandon Wyman3f1242f2020-01-28 13:11:25 -0600580
Brandon Wymanc2c87132021-12-21 23:22:18 +0000581 analyzeVoutOVFault();
Brandon Wyman6710ba22021-10-27 17:39:31 +0000582
Brandon Wymana00e7302021-12-21 23:28:29 +0000583 analyzeIoutOCFault();
Brandon Wymanb10b3be2021-11-09 22:12:15 +0000584
Brandon Wyman08378782021-12-21 23:48:15 +0000585 analyzeVoutUVFault();
Brandon Wyman2cf46942021-10-28 19:09:16 +0000586
Brandon Wymand5d9a222021-12-21 23:59:05 +0000587 analyzeFanFault();
Brandon Wyman7ee4d7e2021-11-19 20:48:23 +0000588
Brandon Wyman52cb3f22021-12-21 23:02:47 +0000589 analyzeTemperatureFault();
Brandon Wyman96893a42021-11-05 19:56:57 +0000590
Brandon Wyman993b5542021-12-21 22:55:16 +0000591 analyzePgoodFault();
Brandon Wyman2916ea52021-11-06 03:31:18 +0000592
Brandon Wyman6c2ac392021-12-21 22:23:06 +0000593 analyzeMFRFault();
Brandon Wyman3f1242f2020-01-28 13:11:25 -0600594
Brandon Wymanf087f472021-12-22 00:04:27 +0000595 analyzeVinUVFault();
Brandon Wyman3f1242f2020-01-28 13:11:25 -0600596 }
597 else
598 {
Brandon Wyman9e292ee2022-03-10 22:56:23 +0000599 if (statusWord != statusWordOld)
600 {
Brandon Wyman321a6152022-03-19 00:11:44 +0000601 log<level::INFO>(fmt::format("{} STATUS_WORD = {:#06x}",
602 shortName, statusWord)
Brandon Wyman9e292ee2022-03-10 22:56:23 +0000603 .c_str());
604 }
605
Brandon Wymane3f7ad22021-12-21 20:27:45 +0000606 // if INPUT/VIN_UV fault was on, it cleared, trace it.
607 if (inputFault)
608 {
609 log<level::INFO>(
610 fmt::format(
Brandon Wyman321a6152022-03-19 00:11:44 +0000611 "{} INPUT fault cleared: STATUS_WORD = {:#06x}",
612 shortName, statusWord)
Brandon Wymane3f7ad22021-12-21 20:27:45 +0000613 .c_str());
614 }
615
616 if (vinUVFault)
617 {
618 log<level::INFO>(
Brandon Wyman321a6152022-03-19 00:11:44 +0000619 fmt::format("{} VIN_UV cleared: STATUS_WORD = {:#06x}",
620 shortName, statusWord)
Brandon Wymane3f7ad22021-12-21 20:27:45 +0000621 .c_str());
622 }
623
Brandon Wyman06ca4592021-12-06 22:52:23 +0000624 if (pgoodFault > 0)
Brandon Wyman4aecc292021-11-10 22:40:41 +0000625 {
Brandon Wyman321a6152022-03-19 00:11:44 +0000626 log<level::INFO>(
627 fmt::format("{} pgoodFault cleared", shortName)
628 .c_str());
Brandon Wyman4aecc292021-11-10 22:40:41 +0000629 }
Brandon Wymane3f7ad22021-12-21 20:27:45 +0000630
631 clearFaultFlags();
Jim Wright4ab86562022-11-18 14:05:46 -0600632 // No AC fail, decrement counter
Jim Wright7f9288c2022-12-08 11:57:04 -0600633 if (acFault != 0)
Jim Wright4ab86562022-11-18 14:05:46 -0600634 {
635 --acFault;
636 }
Brandon Wyman3f1242f2020-01-28 13:11:25 -0600637 }
Brandon Wyman82affd92021-11-24 19:12:49 +0000638
639 // Save off old inputVoltage value.
640 // Get latest inputVoltage.
641 // If voltage went from below minimum, and now is not, clear faults.
642 // Note: getInputVoltage() has its own try/catch.
643 int inputVoltageOld = inputVoltage;
Brandon Wyman4fc191f2022-03-10 23:07:13 +0000644 double actualInputVoltageOld = actualInputVoltage;
Brandon Wyman82affd92021-11-24 19:12:49 +0000645 getInputVoltage(actualInputVoltage, inputVoltage);
646 if ((inputVoltageOld == in_input::VIN_VOLTAGE_0) &&
647 (inputVoltage != in_input::VIN_VOLTAGE_0))
648 {
649 log<level::INFO>(
650 fmt::format(
Brandon Wyman4fc191f2022-03-10 23:07:13 +0000651 "{} READ_VIN back in range: actualInputVoltageOld = {} "
652 "actualInputVoltage = {}",
653 shortName, actualInputVoltageOld, actualInputVoltage)
Brandon Wyman82affd92021-11-24 19:12:49 +0000654 .c_str());
Brandon Wyman3225a452022-03-18 18:51:49 +0000655 clearVinUVFault();
Brandon Wyman82affd92021-11-24 19:12:49 +0000656 }
Brandon Wyman4fc191f2022-03-10 23:07:13 +0000657 else if (vinUVFault && (inputVoltage != in_input::VIN_VOLTAGE_0))
658 {
659 log<level::INFO>(
660 fmt::format(
661 "{} CLEAR_FAULTS: vinUVFault {} actualInputVoltage {}",
662 shortName, vinUVFault, actualInputVoltage)
663 .c_str());
664 // Do we have a VIN_UV fault latched that can now be cleared
Jim Wright4ab86562022-11-18 14:05:46 -0600665 // due to voltage back in range? Attempt to clear the
666 // fault(s), re-check faults on next call.
Brandon Wyman3225a452022-03-18 18:51:49 +0000667 clearVinUVFault();
Brandon Wyman4fc191f2022-03-10 23:07:13 +0000668 }
Brandon Wymanae35ac52022-05-23 22:33:40 +0000669 else if (std::abs(actualInputVoltageOld - actualInputVoltage) >
670 10.0)
Brandon Wyman4fc191f2022-03-10 23:07:13 +0000671 {
672 log<level::INFO>(
673 fmt::format(
674 "{} actualInputVoltageOld = {} actualInputVoltage = {}",
675 shortName, actualInputVoltageOld, actualInputVoltage)
676 .c_str());
677 }
Matt Spinler0975eaf2022-02-14 15:38:30 -0600678
Matt Spinler592bd272023-08-30 11:00:01 -0500679 monitorSensors();
680
Matt Spinler0975eaf2022-02-14 15:38:30 -0600681 checkAvailability();
Brandon Wymanc3324422022-03-24 20:30:57 +0000682
683 if (inputHistorySupported)
684 {
685 updateHistory();
686 }
Brandon Wyman3f1242f2020-01-28 13:11:25 -0600687 }
Patrick Williamsc1d4de52021-10-06 12:45:57 -0500688 catch (const ReadFailure& e)
Brandon Wyman3f1242f2020-01-28 13:11:25 -0600689 {
Brandon Wyman32453e92021-12-15 19:00:14 +0000690 if (readFail < SIZE_MAX)
691 {
692 readFail++;
693 }
694 if (readFail == LOG_LIMIT)
695 {
696 phosphor::logging::commit<ReadFailure>();
697 }
Brandon Wyman3f1242f2020-01-28 13:11:25 -0600698 }
699 }
700}
701
Brandon Wyman59a35792020-06-04 12:37:40 -0500702void PowerSupply::onOffConfig(uint8_t data)
703{
704 using namespace phosphor::pmbus;
705
Faisal Awada9582d9c2023-07-11 09:31:22 -0500706 if (present && driverName != ACBEL_FSG032_DD_NAME)
Brandon Wyman59a35792020-06-04 12:37:40 -0500707 {
708 log<level::INFO>("ON_OFF_CONFIG write", entry("DATA=0x%02X", data));
709 try
710 {
711 std::vector<uint8_t> configData{data};
712 pmbusIntf->writeBinary(ON_OFF_CONFIG, configData,
713 Type::HwmonDeviceDebug);
714 }
715 catch (...)
716 {
717 // The underlying code in writeBinary will log a message to the
B. J. Wyman681b2a32021-04-20 22:31:22 +0000718 // journal if the write fails. If the ON_OFF_CONFIG is not setup
719 // as desired, later fault detection and analysis code should
720 // catch any of the fall out. We should not need to terminate
721 // the application if this write fails.
Brandon Wyman59a35792020-06-04 12:37:40 -0500722 }
723 }
724}
725
Brandon Wyman3225a452022-03-18 18:51:49 +0000726void PowerSupply::clearVinUVFault()
727{
728 // Read in1_lcrit_alarm to clear bits 3 and 4 of STATUS_INPUT.
729 // The fault bits in STAUTS_INPUT roll-up to STATUS_WORD. Clearing those
730 // bits in STATUS_INPUT should result in the corresponding STATUS_WORD bits
731 // also clearing.
732 //
733 // Do not care about return value. Should be 1 if active, 0 if not.
Faisal Awada9582d9c2023-07-11 09:31:22 -0500734 if (driverName != ACBEL_FSG032_DD_NAME)
735 {
736 static_cast<void>(
737 pmbusIntf->read("in1_lcrit_alarm", phosphor::pmbus::Type::Hwmon));
738 }
739 else
740 {
741 static_cast<void>(
742 pmbusIntf->read("curr1_crit_alarm", phosphor::pmbus::Type::Hwmon));
743 }
Brandon Wyman3225a452022-03-18 18:51:49 +0000744 vinUVFault = 0;
745}
746
Brandon Wyman3c208462020-05-13 16:25:58 -0500747void PowerSupply::clearFaults()
748{
Brandon Wyman82affd92021-11-24 19:12:49 +0000749 log<level::DEBUG>(
750 fmt::format("clearFaults() inventoryPath: {}", inventoryPath).c_str());
Brandon Wyman5474c912021-02-23 14:39:43 -0600751 faultLogged = false;
Brandon Wyman3c208462020-05-13 16:25:58 -0500752 // The PMBus device driver does not allow for writing CLEAR_FAULTS
753 // directly. However, the pmbus hwmon device driver code will send a
754 // CLEAR_FAULTS after reading from any of the hwmon "files" in sysfs, so
755 // reading in1_input should result in clearing the fault bits in
756 // STATUS_BYTE/STATUS_WORD.
757 // I do not care what the return value is.
Brandon Wyman11151532020-11-10 13:45:57 -0600758 if (present)
Brandon Wyman3c208462020-05-13 16:25:58 -0500759 {
Brandon Wymane3f7ad22021-12-21 20:27:45 +0000760 clearFaultFlags();
Matt Spinler0975eaf2022-02-14 15:38:30 -0600761 checkAvailability();
Brandon Wyman9564e942020-11-10 14:01:42 -0600762 readFail = 0;
Brandon Wyman9564e942020-11-10 14:01:42 -0600763
Brandon Wyman11151532020-11-10 13:45:57 -0600764 try
765 {
Brandon Wyman3225a452022-03-18 18:51:49 +0000766 clearVinUVFault();
Brandon Wyman11151532020-11-10 13:45:57 -0600767 static_cast<void>(
768 pmbusIntf->read("in1_input", phosphor::pmbus::Type::Hwmon));
769 }
Patrick Williamsc1d4de52021-10-06 12:45:57 -0500770 catch (const ReadFailure& e)
Brandon Wyman11151532020-11-10 13:45:57 -0600771 {
772 // Since I do not care what the return value is, I really do not
B. J. Wyman681b2a32021-04-20 22:31:22 +0000773 // care much if it gets a ReadFailure either. However, this
774 // should not prevent the application from continuing to run, so
775 // catching the read failure.
Brandon Wyman11151532020-11-10 13:45:57 -0600776 }
Brandon Wyman3c208462020-05-13 16:25:58 -0500777 }
778}
779
Patrick Williams7354ce62022-07-22 19:26:56 -0500780void PowerSupply::inventoryChanged(sdbusplus::message_t& msg)
Brandon Wymanaed1f752019-11-25 18:10:52 -0600781{
782 std::string msgSensor;
Patrick Williamsabe49412020-05-13 17:59:47 -0500783 std::map<std::string, std::variant<uint32_t, bool>> msgData;
Brandon Wymanaed1f752019-11-25 18:10:52 -0600784 msg.read(msgSensor, msgData);
785
786 // Check if it was the Present property that changed.
787 auto valPropMap = msgData.find(PRESENT_PROP);
788 if (valPropMap != msgData.end())
789 {
790 if (std::get<bool>(valPropMap->second))
791 {
792 present = true;
B. J. Wyman681b2a32021-04-20 22:31:22 +0000793 // TODO: Immediately trying to read or write the "files" causes
794 // read or write failures.
Brandon Wyman1d7a7df2020-03-26 10:14:05 -0500795 using namespace std::chrono_literals;
796 std::this_thread::sleep_for(20ms);
Brandon Wyman9564e942020-11-10 14:01:42 -0600797 pmbusIntf->findHwmonDir();
Brandon Wyman59a35792020-06-04 12:37:40 -0500798 onOffConfig(phosphor::pmbus::ON_OFF_CONFIG_CONTROL_PIN_ONLY);
Brandon Wymanaed1f752019-11-25 18:10:52 -0600799 clearFaults();
Brandon Wyman1d7a7df2020-03-26 10:14:05 -0500800 updateInventory();
Brandon Wymanaed1f752019-11-25 18:10:52 -0600801 }
802 else
803 {
804 present = false;
805
806 // Clear out the now outdated inventory properties
807 updateInventory();
808 }
Matt Spinler0975eaf2022-02-14 15:38:30 -0600809 checkAvailability();
Brandon Wymanaed1f752019-11-25 18:10:52 -0600810 }
811}
812
Patrick Williams7354ce62022-07-22 19:26:56 -0500813void PowerSupply::inventoryAdded(sdbusplus::message_t& msg)
Brandon Wyman9a507db2021-02-25 16:15:22 -0600814{
815 sdbusplus::message::object_path path;
816 msg.read(path);
817 // Make sure the signal is for the PSU inventory path
818 if (path == inventoryPath)
819 {
820 std::map<std::string, std::map<std::string, std::variant<bool>>>
821 interfaces;
822 // Get map of interfaces and their properties
823 msg.read(interfaces);
824
825 auto properties = interfaces.find(INVENTORY_IFACE);
826 if (properties != interfaces.end())
827 {
828 auto property = properties->second.find(PRESENT_PROP);
829 if (property != properties->second.end())
830 {
831 present = std::get<bool>(property->second);
832
833 log<level::INFO>(fmt::format("Power Supply {} Present {}",
834 inventoryPath, present)
835 .c_str());
836
837 updateInventory();
Matt Spinler0975eaf2022-02-14 15:38:30 -0600838 checkAvailability();
Brandon Wyman9a507db2021-02-25 16:15:22 -0600839 }
840 }
841 }
842}
843
Brandon Wyman8393f462022-06-28 16:06:46 +0000844auto PowerSupply::readVPDValue(const std::string& vpdName,
845 const phosphor::pmbus::Type& type,
846 const std::size_t& vpdSize)
847{
848 std::string vpdValue;
Patrick Williams48781ae2023-05-10 07:50:50 -0500849 const std::regex illegalVPDRegex = std::regex("[^[:alnum:]]",
850 std::regex::basic);
Brandon Wyman8393f462022-06-28 16:06:46 +0000851
852 try
853 {
854 vpdValue = pmbusIntf->readString(vpdName, type);
855 }
856 catch (const ReadFailure& e)
857 {
858 // Ignore the read failure, let pmbus code indicate failure,
859 // path...
860 // TODO - ibm918
861 // https://github.com/openbmc/docs/blob/master/designs/vpd-collection.md
862 // The BMC must log errors if any of the VPD cannot be properly
863 // parsed or fails ECC checks.
864 }
865
866 if (vpdValue.size() != vpdSize)
867 {
868 log<level::INFO>(fmt::format("{} {} resize needed. size: {}", shortName,
869 vpdName, vpdValue.size())
870 .c_str());
871 vpdValue.resize(vpdSize, ' ');
872 }
873
Brandon Wyman056935c2022-06-24 23:05:09 +0000874 // Replace any illegal values with space(s).
875 std::regex_replace(vpdValue.begin(), vpdValue.begin(), vpdValue.end(),
876 illegalVPDRegex, " ");
877
Brandon Wyman8393f462022-06-28 16:06:46 +0000878 return vpdValue;
879}
880
Brandon Wyman1d7a7df2020-03-26 10:14:05 -0500881void PowerSupply::updateInventory()
882{
883 using namespace phosphor::pmbus;
884
Chanh Nguyenc12c53b2021-04-06 17:24:47 +0700885#if IBM_VPD
Brandon Wyman1d7a7df2020-03-26 10:14:05 -0500886 std::string pn;
887 std::string fn;
888 std::string header;
889 std::string sn;
Brandon Wyman8393f462022-06-28 16:06:46 +0000890 // The IBM power supply splits the full serial number into two parts.
891 // Each part is 6 bytes long, which should match up with SN_KW_SIZE.
892 const auto HEADER_SIZE = 6;
893 const auto SERIAL_SIZE = 6;
894 // The IBM PSU firmware version size is a bit complicated. It was originally
895 // 1-byte, per command. It was later expanded to 2-bytes per command, then
896 // up to 8-bytes per command. The device driver only reads up to 2 bytes per
897 // command, but combines all three of the 2-byte reads, or all 4 of the
898 // 1-byte reads into one string. So, the maximum size expected is 6 bytes.
Shawn McCarney983c9892022-10-12 10:49:47 -0500899 // However, it is formatted by the driver as a hex string with two ASCII
900 // characters per byte. So the maximum ASCII string size is 12.
Faisal Awada9582d9c2023-07-11 09:31:22 -0500901 const auto IBMCFFPS_FW_VERSION_SIZE = 12;
902 const auto ACBEL_FSG032_FW_VERSION_SIZE = 6;
Brandon Wyman8393f462022-06-28 16:06:46 +0000903
Brandon Wyman1d7a7df2020-03-26 10:14:05 -0500904 using PropertyMap =
George Liu070c1bc2020-10-12 11:28:01 +0800905 std::map<std::string,
906 std::variant<std::string, std::vector<uint8_t>, bool>>;
Brandon Wyman1d7a7df2020-03-26 10:14:05 -0500907 PropertyMap assetProps;
George Liu070c1bc2020-10-12 11:28:01 +0800908 PropertyMap operProps;
Brandon Wyman1d7a7df2020-03-26 10:14:05 -0500909 PropertyMap versionProps;
910 PropertyMap ipzvpdDINFProps;
911 PropertyMap ipzvpdVINIProps;
912 using InterfaceMap = std::map<std::string, PropertyMap>;
913 InterfaceMap interfaces;
914 using ObjectMap = std::map<sdbusplus::message::object_path, InterfaceMap>;
915 ObjectMap object;
916#endif
B. J. Wyman681b2a32021-04-20 22:31:22 +0000917 log<level::DEBUG>(
918 fmt::format("updateInventory() inventoryPath: {}", inventoryPath)
919 .c_str());
Brandon Wyman1d7a7df2020-03-26 10:14:05 -0500920
921 if (present)
922 {
923 // TODO: non-IBM inventory updates?
924
Chanh Nguyenc12c53b2021-04-06 17:24:47 +0700925#if IBM_VPD
Faisal Awada9582d9c2023-07-11 09:31:22 -0500926 if (driverName == ACBEL_FSG032_DD_NAME)
faisaladed7a02023-05-10 14:59:01 -0500927 {
928 getPsuVpdFromDbus("CC", modelName);
929 getPsuVpdFromDbus("PN", pn);
930 getPsuVpdFromDbus("FN", fn);
931 getPsuVpdFromDbus("SN", sn);
932 assetProps.emplace(SN_PROP, sn);
Faisal Awada9582d9c2023-07-11 09:31:22 -0500933 fwVersion = readVPDValue(FW_VERSION, Type::Debug,
934 ACBEL_FSG032_FW_VERSION_SIZE);
935 versionProps.emplace(VERSION_PROP, fwVersion);
faisaladed7a02023-05-10 14:59:01 -0500936 }
937 else
938 {
939 modelName = readVPDValue(CCIN, Type::HwmonDeviceDebug, CC_KW_SIZE);
940 pn = readVPDValue(PART_NUMBER, Type::Debug, PN_KW_SIZE);
941 fn = readVPDValue(FRU_NUMBER, Type::Debug, FN_KW_SIZE);
942
943 header = readVPDValue(SERIAL_HEADER, Type::Debug, HEADER_SIZE);
944 sn = readVPDValue(SERIAL_NUMBER, Type::Debug, SERIAL_SIZE);
945 assetProps.emplace(SN_PROP, header + sn);
Faisal Awada9582d9c2023-07-11 09:31:22 -0500946 fwVersion = readVPDValue(FW_VERSION, Type::HwmonDeviceDebug,
947 IBMCFFPS_FW_VERSION_SIZE);
948 versionProps.emplace(VERSION_PROP, fwVersion);
faisaladed7a02023-05-10 14:59:01 -0500949 }
950
Brandon Wyman8393f462022-06-28 16:06:46 +0000951 assetProps.emplace(MODEL_PROP, modelName);
Brandon Wyman8393f462022-06-28 16:06:46 +0000952 assetProps.emplace(PN_PROP, pn);
Brandon Wyman8393f462022-06-28 16:06:46 +0000953 assetProps.emplace(SPARE_PN_PROP, fn);
Brandon Wyman1d7a7df2020-03-26 10:14:05 -0500954
Brandon Wyman8393f462022-06-28 16:06:46 +0000955 ipzvpdVINIProps.emplace(
956 "CC", std::vector<uint8_t>(modelName.begin(), modelName.end()));
Brandon Wyman1d7a7df2020-03-26 10:14:05 -0500957 ipzvpdVINIProps.emplace("PN",
958 std::vector<uint8_t>(pn.begin(), pn.end()));
959 ipzvpdVINIProps.emplace("FN",
960 std::vector<uint8_t>(fn.begin(), fn.end()));
Brandon Wyman33d492f2022-03-23 20:45:17 +0000961 std::string header_sn = header + sn;
Brandon Wyman1d7a7df2020-03-26 10:14:05 -0500962 ipzvpdVINIProps.emplace(
963 "SN", std::vector<uint8_t>(header_sn.begin(), header_sn.end()));
964 std::string description = "IBM PS";
965 ipzvpdVINIProps.emplace(
966 "DR", std::vector<uint8_t>(description.begin(), description.end()));
967
Ben Tynerf8d8c462022-01-27 16:09:45 -0600968 // Populate the VINI Resource Type (RT) keyword
969 ipzvpdVINIProps.emplace("RT", std::vector<uint8_t>{'V', 'I', 'N', 'I'});
970
Brandon Wyman1d7a7df2020-03-26 10:14:05 -0500971 // Update the Resource Identifier (RI) keyword
972 // 2 byte FRC: 0x0003
973 // 2 byte RID: 0x1000, 0x1001...
974 std::uint8_t num = std::stoul(
975 inventoryPath.substr(inventoryPath.size() - 1, 1), nullptr, 0);
976 std::vector<uint8_t> ri{0x00, 0x03, 0x10, num};
977 ipzvpdDINFProps.emplace("RI", ri);
978
979 // Fill in the FRU Label (FL) keyword.
980 std::string fl = "E";
981 fl.push_back(inventoryPath.back());
982 fl.resize(FL_KW_SIZE, ' ');
983 ipzvpdDINFProps.emplace("FL",
984 std::vector<uint8_t>(fl.begin(), fl.end()));
985
Ben Tynerf8d8c462022-01-27 16:09:45 -0600986 // Populate the DINF Resource Type (RT) keyword
987 ipzvpdDINFProps.emplace("RT", std::vector<uint8_t>{'D', 'I', 'N', 'F'});
988
Brandon Wyman1d7a7df2020-03-26 10:14:05 -0500989 interfaces.emplace(ASSET_IFACE, std::move(assetProps));
990 interfaces.emplace(VERSION_IFACE, std::move(versionProps));
991 interfaces.emplace(DINF_IFACE, std::move(ipzvpdDINFProps));
992 interfaces.emplace(VINI_IFACE, std::move(ipzvpdVINIProps));
993
George Liu070c1bc2020-10-12 11:28:01 +0800994 // Update the Functional
995 operProps.emplace(FUNCTIONAL_PROP, present);
996 interfaces.emplace(OPERATIONAL_STATE_IFACE, std::move(operProps));
997
Brandon Wyman1d7a7df2020-03-26 10:14:05 -0500998 auto path = inventoryPath.substr(strlen(INVENTORY_OBJ_PATH));
999 object.emplace(path, std::move(interfaces));
1000
1001 try
1002 {
Patrick Williams48781ae2023-05-10 07:50:50 -05001003 auto service = util::getService(INVENTORY_OBJ_PATH,
1004 INVENTORY_MGR_IFACE, bus);
Brandon Wyman1d7a7df2020-03-26 10:14:05 -05001005
1006 if (service.empty())
1007 {
1008 log<level::ERR>("Unable to get inventory manager service");
1009 return;
1010 }
1011
Patrick Williams48781ae2023-05-10 07:50:50 -05001012 auto method = bus.new_method_call(service.c_str(),
1013 INVENTORY_OBJ_PATH,
1014 INVENTORY_MGR_IFACE, "Notify");
Brandon Wyman1d7a7df2020-03-26 10:14:05 -05001015
1016 method.append(std::move(object));
1017
1018 auto reply = bus.call(method);
1019 }
Patrick Williamsc1d4de52021-10-06 12:45:57 -05001020 catch (const std::exception& e)
Brandon Wyman1d7a7df2020-03-26 10:14:05 -05001021 {
Jay Meyer6a3fd2c2020-08-25 16:37:16 -05001022 log<level::ERR>(
1023 std::string(e.what() + std::string(" PATH=") + inventoryPath)
1024 .c_str());
Brandon Wyman1d7a7df2020-03-26 10:14:05 -05001025 }
1026#endif
1027 }
1028}
1029
Brandon Wymanae35ac52022-05-23 22:33:40 +00001030auto PowerSupply::getMaxPowerOut() const
1031{
1032 using namespace phosphor::pmbus;
1033
1034 auto maxPowerOut = 0;
1035
1036 if (present)
1037 {
1038 try
1039 {
1040 // Read max_power_out, should be direct format
Patrick Williams48781ae2023-05-10 07:50:50 -05001041 auto maxPowerOutStr = pmbusIntf->readString(MFR_POUT_MAX,
1042 Type::HwmonDeviceDebug);
Brandon Wymanae35ac52022-05-23 22:33:40 +00001043 log<level::INFO>(fmt::format("{} MFR_POUT_MAX read {}", shortName,
1044 maxPowerOutStr)
1045 .c_str());
1046 maxPowerOut = std::stod(maxPowerOutStr);
1047 }
1048 catch (const std::exception& e)
1049 {
1050 log<level::ERR>(fmt::format("{} MFR_POUT_MAX read error: {}",
1051 shortName, e.what())
1052 .c_str());
1053 }
1054 }
1055
1056 return maxPowerOut;
1057}
1058
Brandon Wymanc3324422022-03-24 20:30:57 +00001059void PowerSupply::setupInputHistory()
1060{
Faisal Awadab66ae502023-04-01 18:30:32 -05001061 if (bindPath.string().find(IBMCFFPS_DD_NAME) != std::string::npos)
Brandon Wymanc3324422022-03-24 20:30:57 +00001062 {
Brandon Wymanae35ac52022-05-23 22:33:40 +00001063 auto maxPowerOut = getMaxPowerOut();
1064
1065 if (maxPowerOut != phosphor::pmbus::IBM_CFFPS_1400W)
Brandon Wymanc3324422022-03-24 20:30:57 +00001066 {
Brandon Wymanae35ac52022-05-23 22:33:40 +00001067 // Do not enable input history for power supplies that are missing
1068 if (present)
1069 {
1070 inputHistorySupported = true;
1071 log<level::INFO>(
1072 fmt::format("{} INPUT_HISTORY enabled", shortName).c_str());
1073
1074 std::string name{fmt::format("{}_input_power", shortName)};
1075
Patrick Williams48781ae2023-05-10 07:50:50 -05001076 historyObjectPath = std::string{INPUT_HISTORY_SENSOR_ROOT} +
1077 '/' + name;
Brandon Wymanae35ac52022-05-23 22:33:40 +00001078
1079 // If the power supply was present, we created the
1080 // recordManager. If it then went missing, the recordManager is
1081 // still there. If it then is reinserted, we should be able to
1082 // use the recordManager that was allocated when it was
1083 // initially present.
1084 if (!recordManager)
1085 {
1086 recordManager = std::make_unique<history::RecordManager>(
1087 INPUT_HISTORY_MAX_RECORDS);
1088 }
1089
1090 if (!average)
1091 {
Patrick Williams48781ae2023-05-10 07:50:50 -05001092 auto avgPath = historyObjectPath + '/' +
1093 history::Average::name;
Brandon Wymanae35ac52022-05-23 22:33:40 +00001094 average = std::make_unique<history::Average>(bus, avgPath);
1095 log<level::DEBUG>(
1096 fmt::format("{} avgPath: {}", shortName, avgPath)
1097 .c_str());
1098 }
1099
1100 if (!maximum)
1101 {
Patrick Williams48781ae2023-05-10 07:50:50 -05001102 auto maxPath = historyObjectPath + '/' +
1103 history::Maximum::name;
Brandon Wymanae35ac52022-05-23 22:33:40 +00001104 maximum = std::make_unique<history::Maximum>(bus, maxPath);
1105 log<level::DEBUG>(
1106 fmt::format("{} maxPath: {}", shortName, maxPath)
1107 .c_str());
1108 }
1109
1110 log<level::DEBUG>(fmt::format("{} historyObjectPath: {}",
1111 shortName, historyObjectPath)
1112 .c_str());
1113 }
1114 }
1115 else
1116 {
Brandon Wymanc3324422022-03-24 20:30:57 +00001117 log<level::INFO>(
Brandon Wymanae35ac52022-05-23 22:33:40 +00001118 fmt::format("{} INPUT_HISTORY DISABLED. max_power_out: {}",
1119 shortName, maxPowerOut)
1120 .c_str());
1121 inputHistorySupported = false;
Brandon Wymanc3324422022-03-24 20:30:57 +00001122 }
1123 }
1124 else
1125 {
1126 inputHistorySupported = false;
1127 }
1128}
1129
Matt Spinler592bd272023-08-30 11:00:01 -05001130void PowerSupply::setupSensors()
1131{
1132 setupInputPowerPeakSensor();
1133}
1134
1135void PowerSupply::setupInputPowerPeakSensor()
1136{
1137 if (peakInputPowerSensor || !present ||
1138 (bindPath.string().find(IBMCFFPS_DD_NAME) == std::string::npos))
1139 {
1140 return;
1141 }
1142
1143 // This PSU has problems with the input_history command
1144 if (getMaxPowerOut() == phosphor::pmbus::IBM_CFFPS_1400W)
1145 {
1146 return;
1147 }
1148
1149 auto sensorPath =
1150 fmt::format("/xyz/openbmc_project/sensors/power/ps{}_input_power_peak",
1151 shortName.back());
1152
1153 peakInputPowerSensor = std::make_unique<PowerSensorObject>(
1154 bus, sensorPath.c_str(), PowerSensorObject::action::defer_emit);
1155
1156 // The others can remain at the defaults.
1157 peakInputPowerSensor->functional(true, true);
1158 peakInputPowerSensor->available(true, true);
1159 peakInputPowerSensor->value(0, true);
1160 peakInputPowerSensor->unit(
1161 sdbusplus::xyz::openbmc_project::Sensor::server::Value::Unit::Watts,
1162 true);
1163
1164 auto associations = getSensorAssociations();
1165 peakInputPowerSensor->associations(associations, true);
1166
1167 peakInputPowerSensor->emit_object_added();
1168}
1169
1170void PowerSupply::setSensorsNotAvailable()
1171{
1172 if (peakInputPowerSensor)
1173 {
1174 peakInputPowerSensor->value(std::numeric_limits<double>::quiet_NaN());
1175 peakInputPowerSensor->available(false);
1176 }
1177}
1178
Brandon Wymanc3324422022-03-24 20:30:57 +00001179void PowerSupply::updateHistory()
1180{
1181 if (!recordManager)
1182 {
1183 // Not enabled
1184 return;
1185 }
1186
1187 if (!present)
1188 {
1189 // Cannot read when not present
1190 return;
1191 }
1192
1193 // Read just the most recent average/max record
Patrick Williams48781ae2023-05-10 07:50:50 -05001194 auto data = pmbusIntf->readBinary(INPUT_HISTORY,
1195 pmbus::Type::HwmonDeviceDebug,
1196 history::RecordManager::RAW_RECORD_SIZE);
Brandon Wymanc3324422022-03-24 20:30:57 +00001197
Brandon Wymanae35ac52022-05-23 22:33:40 +00001198 // Update D-Bus only if something changed (a new record ID, or cleared
1199 // out)
Brandon Wymanc3324422022-03-24 20:30:57 +00001200 auto changed = recordManager->add(data);
1201 if (changed)
1202 {
Patrick Williams41a6b9f2023-05-31 19:54:59 -05001203 average->values(recordManager->getAverageRecords());
1204 maximum->values(recordManager->getMaximumRecords());
Brandon Wymanc3324422022-03-24 20:30:57 +00001205 }
1206}
1207
Matt Spinler592bd272023-08-30 11:00:01 -05001208void PowerSupply::monitorSensors()
1209{
1210 monitorPeakInputPowerSensor();
1211}
1212
1213void PowerSupply::monitorPeakInputPowerSensor()
1214{
1215 if (!peakInputPowerSensor)
1216 {
1217 return;
1218 }
1219
1220 constexpr size_t recordSize = 5;
1221 std::vector<uint8_t> data;
1222
1223 // Get the peak input power with input history command.
1224 // New data only shows up every 30s, but just try to read it every 1s
1225 // anyway so we always have the most up to date value.
1226 try
1227 {
1228 data = pmbusIntf->readBinary(INPUT_HISTORY,
1229 pmbus::Type::HwmonDeviceDebug, recordSize);
1230 }
1231 catch (const ReadFailure& e)
1232 {
1233 peakInputPowerSensor->value(std::numeric_limits<double>::quiet_NaN());
1234 peakInputPowerSensor->functional(false);
1235 throw;
1236 }
1237
1238 if (data.size() != recordSize)
1239 {
1240 log<level::DEBUG>(
1241 fmt::format("Input history command returned {} bytes instead of 5",
1242 data.size())
1243 .c_str());
1244 peakInputPowerSensor->value(std::numeric_limits<double>::quiet_NaN());
1245 peakInputPowerSensor->functional(false);
1246 return;
1247 }
1248
1249 // The format is SSAAAAPPPP:
1250 // SS = packet sequence number
1251 // AAAA = average power (linear format, little endian)
1252 // PPPP = peak power (linear format, little endian)
1253 auto peak = static_cast<uint16_t>(data[4]) << 8 | data[3];
1254 auto peakPower = linearToInteger(peak);
1255
1256 peakInputPowerSensor->value(peakPower);
1257 peakInputPowerSensor->functional(true);
1258 peakInputPowerSensor->available(true);
1259}
1260
Adriana Kobylak4175ffb2021-08-02 14:51:05 +00001261void PowerSupply::getInputVoltage(double& actualInputVoltage,
1262 int& inputVoltage) const
1263{
1264 using namespace phosphor::pmbus;
1265
1266 actualInputVoltage = in_input::VIN_VOLTAGE_0;
1267 inputVoltage = in_input::VIN_VOLTAGE_0;
1268
1269 if (present)
1270 {
1271 try
1272 {
1273 // Read input voltage in millivolts
1274 auto inputVoltageStr = pmbusIntf->readString(READ_VIN, Type::Hwmon);
1275
1276 // Convert to volts
1277 actualInputVoltage = std::stod(inputVoltageStr) / 1000;
1278
1279 // Calculate the voltage based on voltage thresholds
1280 if (actualInputVoltage < in_input::VIN_VOLTAGE_MIN)
1281 {
1282 inputVoltage = in_input::VIN_VOLTAGE_0;
1283 }
1284 else if (actualInputVoltage < in_input::VIN_VOLTAGE_110_THRESHOLD)
1285 {
1286 inputVoltage = in_input::VIN_VOLTAGE_110;
1287 }
1288 else
1289 {
1290 inputVoltage = in_input::VIN_VOLTAGE_220;
1291 }
1292 }
1293 catch (const std::exception& e)
1294 {
1295 log<level::ERR>(
Brandon Wyman321a6152022-03-19 00:11:44 +00001296 fmt::format("{} READ_VIN read error: {}", shortName, e.what())
1297 .c_str());
Adriana Kobylak4175ffb2021-08-02 14:51:05 +00001298 }
1299 }
1300}
1301
Matt Spinler0975eaf2022-02-14 15:38:30 -06001302void PowerSupply::checkAvailability()
1303{
1304 bool origAvailability = available;
George Liu9464c422023-02-27 14:30:27 +08001305 bool faulted = isPowerOn() && (hasPSKillFault() || hasIoutOCFault());
1306 available = present && !hasInputFault() && !hasVINUVFault() && !faulted;
Matt Spinler0975eaf2022-02-14 15:38:30 -06001307
1308 if (origAvailability != available)
1309 {
1310 auto invpath = inventoryPath.substr(strlen(INVENTORY_OBJ_PATH));
1311 phosphor::power::psu::setAvailable(bus, invpath, available);
Matt Spinlerca1e9ea2022-02-18 14:03:08 -06001312
1313 // Check if the health rollup needs to change based on the
1314 // new availability value.
1315 phosphor::power::psu::handleChassisHealthRollup(bus, inventoryPath,
1316 !available);
Matt Spinler0975eaf2022-02-14 15:38:30 -06001317 }
1318}
1319
Matt Spinlera068f422023-03-10 13:06:49 -06001320void PowerSupply::setInputVoltageRating()
1321{
1322 if (!present)
1323 {
Matt Spinler1aaf9f82023-03-22 09:52:22 -05001324 if (inputVoltageRatingIface)
1325 {
1326 inputVoltageRatingIface->value(0);
1327 inputVoltageRatingIface.reset();
1328 }
Matt Spinlera068f422023-03-10 13:06:49 -06001329 return;
1330 }
1331
1332 double inputVoltageValue{};
1333 int inputVoltageRating{};
1334 getInputVoltage(inputVoltageValue, inputVoltageRating);
1335
1336 if (!inputVoltageRatingIface)
1337 {
1338 auto path = fmt::format(
1339 "/xyz/openbmc_project/sensors/voltage/ps{}_input_voltage_rating",
1340 shortName.back());
1341
1342 inputVoltageRatingIface = std::make_unique<SensorObject>(
1343 bus, path.c_str(), SensorObject::action::defer_emit);
1344
1345 // Leave other properties at their defaults
1346 inputVoltageRatingIface->unit(SensorInterface::Unit::Volts, true);
1347 inputVoltageRatingIface->value(static_cast<double>(inputVoltageRating),
1348 true);
1349
1350 inputVoltageRatingIface->emit_object_added();
1351 }
1352 else
1353 {
1354 inputVoltageRatingIface->value(static_cast<double>(inputVoltageRating));
1355 }
1356}
1357
faisaladed7a02023-05-10 14:59:01 -05001358void PowerSupply::getPsuVpdFromDbus(const std::string& keyword,
1359 std::string& vpdStr)
1360{
1361 try
1362 {
1363 std::vector<uint8_t> value;
1364 vpdStr.clear();
1365 util::getProperty(VINI_IFACE, keyword, inventoryPath,
1366 INVENTORY_MGR_IFACE, bus, value);
1367 for (char c : value)
1368 {
1369 vpdStr += c;
1370 }
1371 }
1372 catch (const sdbusplus::exception_t& e)
1373 {
1374 log<level::ERR>(
1375 fmt::format("Failed getProperty error: {}", e.what()).c_str());
1376 }
1377}
Matt Spinler592bd272023-08-30 11:00:01 -05001378
1379double PowerSupply::linearToInteger(uint16_t data)
1380{
1381 // The exponent is the first 5 bits, followed by 11 bits of mantissa.
1382 int8_t exponent = (data & 0xF800) >> 11;
1383 int16_t mantissa = (data & 0x07FF);
1384
1385 // If exponent's MSB on, then it's negative.
1386 // Convert from two's complement.
1387 if (exponent & 0x10)
1388 {
1389 exponent = (~exponent) & 0x1F;
1390 exponent = (exponent + 1) * -1;
1391 }
1392
1393 // If mantissa's MSB on, then it's negative.
1394 // Convert from two's complement.
1395 if (mantissa & 0x400)
1396 {
1397 mantissa = (~mantissa) & 0x07FF;
1398 mantissa = (mantissa + 1) * -1;
1399 }
1400
1401 auto value = static_cast<double>(mantissa) * pow(2, exponent);
1402 return value;
1403}
1404
1405std::vector<AssociationTuple> PowerSupply::getSensorAssociations()
1406{
1407 std::vector<AssociationTuple> associations;
1408
1409 associations.emplace_back("inventory", "sensors", inventoryPath);
1410
1411 auto chassis = getChassis(bus, inventoryPath);
1412 associations.emplace_back("chassis", "all_sensors", std::move(chassis));
1413
1414 return associations;
1415}
1416
Brandon Wyman3f1242f2020-01-28 13:11:25 -06001417} // namespace phosphor::power::psu