blob: b362a57cdb40e07ba1b12f2a541283eeb22402ef [file] [log] [blame]
Brandon Wyman1d7a7df2020-03-26 10:14:05 -05001#include "config.h"
2
Brandon Wymanaed1f752019-11-25 18:10:52 -06003#include "power_supply.hpp"
4
5#include "types.hpp"
Brandon Wyman3f1242f2020-01-28 13:11:25 -06006#include "util.hpp"
Brandon Wymanaed1f752019-11-25 18:10:52 -06007
Brandon Wymandf13c3a2020-12-15 14:25:22 -06008#include <fmt/format.h>
9
Brandon Wyman3f1242f2020-01-28 13:11:25 -060010#include <xyz/openbmc_project/Common/Device/error.hpp>
11
Brandon Wyman4fc191f2022-03-10 23:07:13 +000012#include <chrono> // sleep_for()
13#include <cmath>
Brandon Wyman1d7a7df2020-03-26 10:14:05 -050014#include <cstdint> // uint8_t...
B. J. Wyman681b2a32021-04-20 22:31:22 +000015#include <fstream>
Brandon Wyman056935c2022-06-24 23:05:09 +000016#include <regex>
B. J. Wyman681b2a32021-04-20 22:31:22 +000017#include <thread> // sleep_for()
Brandon Wyman1d7a7df2020-03-26 10:14:05 -050018
Brandon Wyman3f1242f2020-01-28 13:11:25 -060019namespace phosphor::power::psu
Brandon Wymanaed1f752019-11-25 18:10:52 -060020{
B. J. Wyman681b2a32021-04-20 22:31:22 +000021// Amount of time in milliseconds to delay between power supply going from
22// missing to present before running the bind command(s).
23constexpr auto bindDelay = 1000;
Brandon Wymanaed1f752019-11-25 18:10:52 -060024
Brandon Wymanc3324422022-03-24 20:30:57 +000025// The number of INPUT_HISTORY records to keep on D-Bus.
26// Each record covers a 30-second span. That means two records are needed to
27// cover a minute of time. If we want one (1) hour of data, that would be 120
28// records.
29constexpr auto INPUT_HISTORY_MAX_RECORDS = 120;
30
Brandon Wymanaed1f752019-11-25 18:10:52 -060031using namespace phosphor::logging;
Brandon Wyman3f1242f2020-01-28 13:11:25 -060032using namespace sdbusplus::xyz::openbmc_project::Common::Device::Error;
Brandon Wymanaed1f752019-11-25 18:10:52 -060033
Patrick Williams7354ce62022-07-22 19:26:56 -050034PowerSupply::PowerSupply(sdbusplus::bus_t& bus, const std::string& invpath,
B. J. Wyman681b2a32021-04-20 22:31:22 +000035 std::uint8_t i2cbus, std::uint16_t i2caddr,
Brandon Wymanc3324422022-03-24 20:30:57 +000036 const std::string& driver,
George Liu9464c422023-02-27 14:30:27 +080037 const std::string& gpioLineName,
38 std::function<bool()>&& callback) :
Brandon Wyman510acaa2020-11-05 18:32:04 -060039 bus(bus),
George Liu9464c422023-02-27 14:30:27 +080040 inventoryPath(invpath), bindPath("/sys/bus/i2c/drivers/" + driver),
41 isPowerOn(std::move(callback))
Brandon Wyman510acaa2020-11-05 18:32:04 -060042{
43 if (inventoryPath.empty())
44 {
45 throw std::invalid_argument{"Invalid empty inventoryPath"};
46 }
47
B. J. Wyman681b2a32021-04-20 22:31:22 +000048 if (gpioLineName.empty())
49 {
50 throw std::invalid_argument{"Invalid empty gpioLineName"};
51 }
Brandon Wyman510acaa2020-11-05 18:32:04 -060052
Brandon Wyman321a6152022-03-19 00:11:44 +000053 shortName = findShortName(inventoryPath);
54
55 log<level::DEBUG>(
56 fmt::format("{} gpioLineName: {}", shortName, gpioLineName).c_str());
B. J. Wyman681b2a32021-04-20 22:31:22 +000057 presenceGPIO = createGPIO(gpioLineName);
Brandon Wyman510acaa2020-11-05 18:32:04 -060058
59 std::ostringstream ss;
60 ss << std::hex << std::setw(4) << std::setfill('0') << i2caddr;
61 std::string addrStr = ss.str();
B. J. Wyman681b2a32021-04-20 22:31:22 +000062 std::string busStr = std::to_string(i2cbus);
63 bindDevice = busStr;
64 bindDevice.append("-");
65 bindDevice.append(addrStr);
66
Brandon Wyman510acaa2020-11-05 18:32:04 -060067 pmbusIntf = phosphor::pmbus::createPMBus(i2cbus, addrStr);
68
69 // Get the current state of the Present property.
B. J. Wyman681b2a32021-04-20 22:31:22 +000070 try
71 {
72 updatePresenceGPIO();
73 }
74 catch (...)
75 {
76 // If the above attempt to use the GPIO failed, it likely means that the
77 // GPIOs are in use by the kernel, meaning it is using gpio-keys.
78 // So, I should rely on phosphor-gpio-presence to update D-Bus, and
79 // work that way for power supply presence.
80 presenceGPIO = nullptr;
81 // Setup the functions to call when the D-Bus inventory path for the
82 // Present property changes.
83 presentMatch = std::make_unique<sdbusplus::bus::match_t>(
84 bus,
85 sdbusplus::bus::match::rules::propertiesChanged(inventoryPath,
86 INVENTORY_IFACE),
87 [this](auto& msg) { this->inventoryChanged(msg); });
88
89 presentAddedMatch = std::make_unique<sdbusplus::bus::match_t>(
90 bus,
91 sdbusplus::bus::match::rules::interfacesAdded() +
92 sdbusplus::bus::match::rules::argNpath(0, inventoryPath),
93 [this](auto& msg) { this->inventoryAdded(msg); });
94
95 updatePresence();
96 updateInventory();
Brandon Wymanc3324422022-03-24 20:30:57 +000097 setupInputHistory();
B. J. Wyman681b2a32021-04-20 22:31:22 +000098 }
Matt Spinlera068f422023-03-10 13:06:49 -060099
100 setInputVoltageRating();
B. J. Wyman681b2a32021-04-20 22:31:22 +0000101}
102
103void PowerSupply::bindOrUnbindDriver(bool present)
104{
105 auto action = (present) ? "bind" : "unbind";
106 auto path = bindPath / action;
107
108 if (present)
109 {
Brandon Wymanb1ee60f2022-03-22 22:37:12 +0000110 std::this_thread::sleep_for(std::chrono::milliseconds(bindDelay));
B. J. Wyman681b2a32021-04-20 22:31:22 +0000111 log<level::INFO>(
112 fmt::format("Binding device driver. path: {} device: {}",
113 path.string(), bindDevice)
114 .c_str());
115 }
116 else
117 {
118 log<level::INFO>(
119 fmt::format("Unbinding device driver. path: {} device: {}",
120 path.string(), bindDevice)
121 .c_str());
122 }
123
124 std::ofstream file;
125
126 file.exceptions(std::ofstream::failbit | std::ofstream::badbit |
127 std::ofstream::eofbit);
128
129 try
130 {
131 file.open(path);
132 file << bindDevice;
133 file.close();
134 }
Patrick Williamsc1d4de52021-10-06 12:45:57 -0500135 catch (const std::exception& e)
B. J. Wyman681b2a32021-04-20 22:31:22 +0000136 {
137 auto err = errno;
138
139 log<level::ERR>(
140 fmt::format("Failed binding or unbinding device. errno={}", err)
141 .c_str());
142 }
Brandon Wyman510acaa2020-11-05 18:32:04 -0600143}
144
Brandon Wymanaed1f752019-11-25 18:10:52 -0600145void PowerSupply::updatePresence()
146{
147 try
148 {
Brandon Wyman3f1242f2020-01-28 13:11:25 -0600149 present = getPresence(bus, inventoryPath);
Brandon Wymanaed1f752019-11-25 18:10:52 -0600150 }
Patrick Williams7354ce62022-07-22 19:26:56 -0500151 catch (const sdbusplus::exception_t& e)
Brandon Wymanaed1f752019-11-25 18:10:52 -0600152 {
153 // Relying on property change or interface added to retry.
154 // Log an informational trace to the journal.
Brandon Wymandf13c3a2020-12-15 14:25:22 -0600155 log<level::INFO>(
156 fmt::format("D-Bus property {} access failure exception",
157 inventoryPath)
158 .c_str());
Brandon Wymanaed1f752019-11-25 18:10:52 -0600159 }
160}
161
B. J. Wyman681b2a32021-04-20 22:31:22 +0000162void PowerSupply::updatePresenceGPIO()
163{
164 bool presentOld = present;
165
166 try
167 {
168 if (presenceGPIO->read() > 0)
169 {
170 present = true;
171 }
172 else
173 {
174 present = false;
175 }
176 }
Patrick Williamsc1d4de52021-10-06 12:45:57 -0500177 catch (const std::exception& e)
B. J. Wyman681b2a32021-04-20 22:31:22 +0000178 {
179 log<level::ERR>(
180 fmt::format("presenceGPIO read fail: {}", e.what()).c_str());
181 throw;
182 }
183
184 if (presentOld != present)
185 {
Brandon Wyman321a6152022-03-19 00:11:44 +0000186 log<level::DEBUG>(fmt::format("{} presentOld: {} present: {}",
187 shortName, presentOld, present)
188 .c_str());
Matt Spinlerca1e9ea2022-02-18 14:03:08 -0600189
190 auto invpath = inventoryPath.substr(strlen(INVENTORY_OBJ_PATH));
Brandon Wyman90d529a2022-03-22 23:02:54 +0000191
192 bindOrUnbindDriver(present);
193 if (present)
194 {
195 // If the power supply was present, then missing, and present again,
196 // the hwmon path may have changed. We will need the correct/updated
197 // path before any reads or writes are attempted.
198 pmbusIntf->findHwmonDir();
199 }
200
Brandon Wyman321a6152022-03-19 00:11:44 +0000201 setPresence(bus, invpath, present, shortName);
Brandon Wymanc3324422022-03-24 20:30:57 +0000202 setupInputHistory();
Matt Spinlerca1e9ea2022-02-18 14:03:08 -0600203 updateInventory();
204
Brandon Wyman90d529a2022-03-22 23:02:54 +0000205 // Need Functional to already be correct before calling this.
Matt Spinlerca1e9ea2022-02-18 14:03:08 -0600206 checkAvailability();
207
B. J. Wyman681b2a32021-04-20 22:31:22 +0000208 if (present)
209 {
B. J. Wyman681b2a32021-04-20 22:31:22 +0000210 onOffConfig(phosphor::pmbus::ON_OFF_CONFIG_CONTROL_PIN_ONLY);
211 clearFaults();
Brandon Wyman18a24d92022-04-19 22:48:34 +0000212 // Indicate that the input history data and timestamps between all
213 // the power supplies that are present in the system need to be
214 // synchronized.
215 syncHistoryRequired = true;
B. J. Wyman681b2a32021-04-20 22:31:22 +0000216 }
B. J. Wyman681b2a32021-04-20 22:31:22 +0000217 }
218}
219
Brandon Wymanc2203432021-12-21 23:09:48 +0000220void PowerSupply::analyzeCMLFault()
221{
222 if (statusWord & phosphor::pmbus::status_word::CML_FAULT)
223 {
Brandon Wymanc2906f42021-12-21 20:14:56 +0000224 if (cmlFault < DEGLITCH_LIMIT)
Brandon Wymanc2203432021-12-21 23:09:48 +0000225 {
Brandon Wyman9e292ee2022-03-10 22:56:23 +0000226 if (statusWord != statusWordOld)
227 {
Brandon Wyman321a6152022-03-19 00:11:44 +0000228 log<level::ERR>(
229 fmt::format("{} CML fault: STATUS_WORD = {:#06x}, "
230 "STATUS_CML = {:#02x}",
231 shortName, statusWord, statusCML)
232 .c_str());
Brandon Wyman9e292ee2022-03-10 22:56:23 +0000233 }
Brandon Wymanc2906f42021-12-21 20:14:56 +0000234 cmlFault++;
235 }
236 }
237 else
238 {
239 cmlFault = 0;
Brandon Wymanc2203432021-12-21 23:09:48 +0000240 }
241}
242
Brandon Wymane3b0bb02021-12-21 23:16:48 +0000243void PowerSupply::analyzeInputFault()
244{
245 if (statusWord & phosphor::pmbus::status_word::INPUT_FAULT_WARN)
246 {
Brandon Wymanc2906f42021-12-21 20:14:56 +0000247 if (inputFault < DEGLITCH_LIMIT)
Brandon Wymane3b0bb02021-12-21 23:16:48 +0000248 {
Brandon Wyman9e292ee2022-03-10 22:56:23 +0000249 if (statusWord != statusWordOld)
250 {
251 log<level::ERR>(
Brandon Wyman321a6152022-03-19 00:11:44 +0000252 fmt::format("{} INPUT fault: STATUS_WORD = {:#06x}, "
Brandon Wyman9e292ee2022-03-10 22:56:23 +0000253 "STATUS_MFR_SPECIFIC = {:#04x}, "
254 "STATUS_INPUT = {:#04x}",
Brandon Wyman321a6152022-03-19 00:11:44 +0000255 shortName, statusWord, statusMFR, statusInput)
Brandon Wyman9e292ee2022-03-10 22:56:23 +0000256 .c_str());
257 }
Brandon Wymanc2906f42021-12-21 20:14:56 +0000258 inputFault++;
259 }
Brandon Wymane3b0bb02021-12-21 23:16:48 +0000260 }
Brandon Wyman82affd92021-11-24 19:12:49 +0000261
262 // If had INPUT/VIN_UV fault, and now off.
263 // Trace that odd behavior.
264 if (inputFault &&
265 !(statusWord & phosphor::pmbus::status_word::INPUT_FAULT_WARN))
266 {
267 log<level::INFO>(
Brandon Wyman321a6152022-03-19 00:11:44 +0000268 fmt::format("{} INPUT fault cleared: STATUS_WORD = {:#06x}, "
Brandon Wyman6f939a32022-03-10 18:42:20 +0000269 "STATUS_MFR_SPECIFIC = {:#04x}, "
270 "STATUS_INPUT = {:#04x}",
Brandon Wyman321a6152022-03-19 00:11:44 +0000271 shortName, statusWord, statusMFR, statusInput)
Brandon Wyman82affd92021-11-24 19:12:49 +0000272 .c_str());
Brandon Wymanc2906f42021-12-21 20:14:56 +0000273 inputFault = 0;
Brandon Wyman82affd92021-11-24 19:12:49 +0000274 }
Brandon Wymane3b0bb02021-12-21 23:16:48 +0000275}
276
Brandon Wymanc2c87132021-12-21 23:22:18 +0000277void PowerSupply::analyzeVoutOVFault()
278{
279 if (statusWord & phosphor::pmbus::status_word::VOUT_OV_FAULT)
280 {
Brandon Wymanc2906f42021-12-21 20:14:56 +0000281 if (voutOVFault < DEGLITCH_LIMIT)
Brandon Wymanc2c87132021-12-21 23:22:18 +0000282 {
Brandon Wyman9e292ee2022-03-10 22:56:23 +0000283 if (statusWord != statusWordOld)
284 {
285 log<level::ERR>(
Brandon Wyman321a6152022-03-19 00:11:44 +0000286 fmt::format(
287 "{} VOUT_OV_FAULT fault: STATUS_WORD = {:#06x}, "
288 "STATUS_MFR_SPECIFIC = {:#04x}, "
289 "STATUS_VOUT = {:#02x}",
290 shortName, statusWord, statusMFR, statusVout)
Brandon Wyman9e292ee2022-03-10 22:56:23 +0000291 .c_str());
292 }
Brandon Wymanc2c87132021-12-21 23:22:18 +0000293
Brandon Wymanc2906f42021-12-21 20:14:56 +0000294 voutOVFault++;
295 }
296 }
297 else
298 {
299 voutOVFault = 0;
Brandon Wymanc2c87132021-12-21 23:22:18 +0000300 }
301}
302
Brandon Wymana00e7302021-12-21 23:28:29 +0000303void PowerSupply::analyzeIoutOCFault()
304{
305 if (statusWord & phosphor::pmbus::status_word::IOUT_OC_FAULT)
306 {
Brandon Wymanc2906f42021-12-21 20:14:56 +0000307 if (ioutOCFault < DEGLITCH_LIMIT)
Brandon Wymana00e7302021-12-21 23:28:29 +0000308 {
Brandon Wyman9e292ee2022-03-10 22:56:23 +0000309 if (statusWord != statusWordOld)
310 {
311 log<level::ERR>(
Brandon Wyman321a6152022-03-19 00:11:44 +0000312 fmt::format("{} IOUT fault: STATUS_WORD = {:#06x}, "
Brandon Wyman9e292ee2022-03-10 22:56:23 +0000313 "STATUS_MFR_SPECIFIC = {:#04x}, "
314 "STATUS_IOUT = {:#04x}",
Brandon Wyman321a6152022-03-19 00:11:44 +0000315 shortName, statusWord, statusMFR, statusIout)
Brandon Wyman9e292ee2022-03-10 22:56:23 +0000316 .c_str());
317 }
Brandon Wymana00e7302021-12-21 23:28:29 +0000318
Brandon Wymanc2906f42021-12-21 20:14:56 +0000319 ioutOCFault++;
320 }
321 }
322 else
323 {
324 ioutOCFault = 0;
Brandon Wymana00e7302021-12-21 23:28:29 +0000325 }
326}
327
Brandon Wyman08378782021-12-21 23:48:15 +0000328void PowerSupply::analyzeVoutUVFault()
329{
330 if ((statusWord & phosphor::pmbus::status_word::VOUT_FAULT) &&
331 !(statusWord & phosphor::pmbus::status_word::VOUT_OV_FAULT))
332 {
Brandon Wymanc2906f42021-12-21 20:14:56 +0000333 if (voutUVFault < DEGLITCH_LIMIT)
Brandon Wyman08378782021-12-21 23:48:15 +0000334 {
Brandon Wyman9e292ee2022-03-10 22:56:23 +0000335 if (statusWord != statusWordOld)
336 {
337 log<level::ERR>(
Brandon Wyman321a6152022-03-19 00:11:44 +0000338 fmt::format(
339 "{} VOUT_UV_FAULT fault: STATUS_WORD = {:#06x}, "
340 "STATUS_MFR_SPECIFIC = {:#04x}, "
341 "STATUS_VOUT = {:#04x}",
342 shortName, statusWord, statusMFR, statusVout)
Brandon Wyman9e292ee2022-03-10 22:56:23 +0000343 .c_str());
344 }
Brandon Wymanc2906f42021-12-21 20:14:56 +0000345 voutUVFault++;
346 }
347 }
348 else
349 {
350 voutUVFault = 0;
Brandon Wyman08378782021-12-21 23:48:15 +0000351 }
352}
353
Brandon Wymand5d9a222021-12-21 23:59:05 +0000354void PowerSupply::analyzeFanFault()
355{
356 if (statusWord & phosphor::pmbus::status_word::FAN_FAULT)
357 {
Brandon Wymanc2906f42021-12-21 20:14:56 +0000358 if (fanFault < DEGLITCH_LIMIT)
Brandon Wymand5d9a222021-12-21 23:59:05 +0000359 {
Brandon Wyman9e292ee2022-03-10 22:56:23 +0000360 if (statusWord != statusWordOld)
361 {
Brandon Wyman321a6152022-03-19 00:11:44 +0000362 log<level::ERR>(fmt::format("{} FANS fault/warning: "
Brandon Wyman9e292ee2022-03-10 22:56:23 +0000363 "STATUS_WORD = {:#06x}, "
364 "STATUS_MFR_SPECIFIC = {:#04x}, "
365 "STATUS_FANS_1_2 = {:#04x}",
Brandon Wyman321a6152022-03-19 00:11:44 +0000366 shortName, statusWord, statusMFR,
367 statusFans12)
Brandon Wyman9e292ee2022-03-10 22:56:23 +0000368 .c_str());
369 }
Brandon Wymanc2906f42021-12-21 20:14:56 +0000370 fanFault++;
371 }
372 }
373 else
374 {
375 fanFault = 0;
Brandon Wymand5d9a222021-12-21 23:59:05 +0000376 }
377}
378
Brandon Wyman52cb3f22021-12-21 23:02:47 +0000379void PowerSupply::analyzeTemperatureFault()
380{
381 if (statusWord & phosphor::pmbus::status_word::TEMPERATURE_FAULT_WARN)
382 {
Brandon Wymanc2906f42021-12-21 20:14:56 +0000383 if (tempFault < DEGLITCH_LIMIT)
Brandon Wyman52cb3f22021-12-21 23:02:47 +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("{} TEMPERATURE fault/warning: "
Brandon Wyman9e292ee2022-03-10 22:56:23 +0000388 "STATUS_WORD = {:#06x}, "
389 "STATUS_MFR_SPECIFIC = {:#04x}, "
390 "STATUS_TEMPERATURE = {:#04x}",
Brandon Wyman321a6152022-03-19 00:11:44 +0000391 shortName, statusWord, statusMFR,
Brandon Wyman9e292ee2022-03-10 22:56:23 +0000392 statusTemperature)
393 .c_str());
394 }
Brandon Wymanc2906f42021-12-21 20:14:56 +0000395 tempFault++;
396 }
397 }
398 else
399 {
400 tempFault = 0;
Brandon Wyman52cb3f22021-12-21 23:02:47 +0000401 }
402}
403
Brandon Wyman993b5542021-12-21 22:55:16 +0000404void PowerSupply::analyzePgoodFault()
405{
406 if ((statusWord & phosphor::pmbus::status_word::POWER_GOOD_NEGATED) ||
407 (statusWord & phosphor::pmbus::status_word::UNIT_IS_OFF))
408 {
Brandon Wyman6d469fd2022-06-15 16:58:21 +0000409 if (pgoodFault < PGOOD_DEGLITCH_LIMIT)
Brandon Wyman993b5542021-12-21 22:55:16 +0000410 {
Brandon Wyman9e292ee2022-03-10 22:56:23 +0000411 if (statusWord != statusWordOld)
412 {
Brandon Wyman321a6152022-03-19 00:11:44 +0000413 log<level::ERR>(fmt::format("{} PGOOD fault: "
Brandon Wyman9e292ee2022-03-10 22:56:23 +0000414 "STATUS_WORD = {:#06x}, "
415 "STATUS_MFR_SPECIFIC = {:#04x}",
Brandon Wyman321a6152022-03-19 00:11:44 +0000416 shortName, statusWord, statusMFR)
Brandon Wyman9e292ee2022-03-10 22:56:23 +0000417 .c_str());
418 }
Brandon Wyman993b5542021-12-21 22:55:16 +0000419 pgoodFault++;
420 }
421 }
422 else
423 {
424 pgoodFault = 0;
425 }
426}
427
Brandon Wyman39ea02b2021-11-23 23:22:23 +0000428void PowerSupply::determineMFRFault()
429{
430 if (bindPath.string().find("ibm-cffps") != std::string::npos)
431 {
432 // IBM MFR_SPECIFIC[4] is PS_Kill fault
433 if (statusMFR & 0x10)
434 {
Brandon Wymanc2906f42021-12-21 20:14:56 +0000435 if (psKillFault < DEGLITCH_LIMIT)
436 {
437 psKillFault++;
438 }
439 }
440 else
441 {
442 psKillFault = 0;
Brandon Wyman39ea02b2021-11-23 23:22:23 +0000443 }
444 // IBM MFR_SPECIFIC[6] is 12Vcs fault.
445 if (statusMFR & 0x40)
446 {
Brandon Wymanc2906f42021-12-21 20:14:56 +0000447 if (ps12VcsFault < DEGLITCH_LIMIT)
448 {
449 ps12VcsFault++;
450 }
451 }
452 else
453 {
454 ps12VcsFault = 0;
Brandon Wyman39ea02b2021-11-23 23:22:23 +0000455 }
456 // IBM MFR_SPECIFIC[7] is 12V Current-Share fault.
457 if (statusMFR & 0x80)
458 {
Brandon Wymanc2906f42021-12-21 20:14:56 +0000459 if (psCS12VFault < DEGLITCH_LIMIT)
460 {
461 psCS12VFault++;
462 }
463 }
464 else
465 {
466 psCS12VFault = 0;
Brandon Wyman39ea02b2021-11-23 23:22:23 +0000467 }
468 }
469}
470
Brandon Wyman6c2ac392021-12-21 22:23:06 +0000471void PowerSupply::analyzeMFRFault()
472{
473 if (statusWord & phosphor::pmbus::status_word::MFR_SPECIFIC_FAULT)
474 {
Brandon Wymanc2906f42021-12-21 20:14:56 +0000475 if (mfrFault < DEGLITCH_LIMIT)
Brandon Wyman6c2ac392021-12-21 22:23:06 +0000476 {
Brandon Wyman9e292ee2022-03-10 22:56:23 +0000477 if (statusWord != statusWordOld)
478 {
Brandon Wyman321a6152022-03-19 00:11:44 +0000479 log<level::ERR>(fmt::format("{} MFR fault: "
Brandon Wyman9e292ee2022-03-10 22:56:23 +0000480 "STATUS_WORD = {:#06x} "
481 "STATUS_MFR_SPECIFIC = {:#04x}",
Brandon Wyman321a6152022-03-19 00:11:44 +0000482 shortName, statusWord, statusMFR)
Brandon Wyman9e292ee2022-03-10 22:56:23 +0000483 .c_str());
484 }
Brandon Wymanc2906f42021-12-21 20:14:56 +0000485 mfrFault++;
Brandon Wyman6c2ac392021-12-21 22:23:06 +0000486 }
487
Brandon Wyman6c2ac392021-12-21 22:23:06 +0000488 determineMFRFault();
489 }
Brandon Wymanc2906f42021-12-21 20:14:56 +0000490 else
491 {
492 mfrFault = 0;
493 }
Brandon Wyman6c2ac392021-12-21 22:23:06 +0000494}
495
Brandon Wymanf087f472021-12-22 00:04:27 +0000496void PowerSupply::analyzeVinUVFault()
497{
498 if (statusWord & phosphor::pmbus::status_word::VIN_UV_FAULT)
499 {
Brandon Wymanc2906f42021-12-21 20:14:56 +0000500 if (vinUVFault < DEGLITCH_LIMIT)
Brandon Wymanf087f472021-12-22 00:04:27 +0000501 {
Brandon Wyman9e292ee2022-03-10 22:56:23 +0000502 if (statusWord != statusWordOld)
503 {
504 log<level::ERR>(
Brandon Wyman321a6152022-03-19 00:11:44 +0000505 fmt::format("{} VIN_UV fault: STATUS_WORD = {:#06x}, "
Brandon Wyman9e292ee2022-03-10 22:56:23 +0000506 "STATUS_MFR_SPECIFIC = {:#04x}, "
507 "STATUS_INPUT = {:#04x}",
Brandon Wyman321a6152022-03-19 00:11:44 +0000508 shortName, statusWord, statusMFR, statusInput)
Brandon Wyman9e292ee2022-03-10 22:56:23 +0000509 .c_str());
510 }
Brandon Wymanc2906f42021-12-21 20:14:56 +0000511 vinUVFault++;
Brandon Wymanf087f472021-12-22 00:04:27 +0000512 }
Jim Wright4ab86562022-11-18 14:05:46 -0600513 // Remember that this PSU has seen an AC fault
514 acFault = AC_FAULT_LIMIT;
Brandon Wymanf087f472021-12-22 00:04:27 +0000515 }
Jim Wright7f9288c2022-12-08 11:57:04 -0600516 else
Brandon Wyman82affd92021-11-24 19:12:49 +0000517 {
Jim Wright7f9288c2022-12-08 11:57:04 -0600518 if (vinUVFault != 0)
519 {
520 log<level::INFO>(
521 fmt::format("{} VIN_UV fault cleared: STATUS_WORD = {:#06x}, "
522 "STATUS_MFR_SPECIFIC = {:#04x}, "
523 "STATUS_INPUT = {:#04x}",
524 shortName, statusWord, statusMFR, statusInput)
525 .c_str());
526 vinUVFault = 0;
527 }
Jim Wright4ab86562022-11-18 14:05:46 -0600528 // No AC fail, decrement counter
Jim Wright7f9288c2022-12-08 11:57:04 -0600529 if (acFault != 0)
Jim Wright4ab86562022-11-18 14:05:46 -0600530 {
531 --acFault;
532 }
Brandon Wyman82affd92021-11-24 19:12:49 +0000533 }
Brandon Wymanf087f472021-12-22 00:04:27 +0000534}
535
Brandon Wyman3f1242f2020-01-28 13:11:25 -0600536void PowerSupply::analyze()
537{
538 using namespace phosphor::pmbus;
539
B. J. Wyman681b2a32021-04-20 22:31:22 +0000540 if (presenceGPIO)
541 {
542 updatePresenceGPIO();
543 }
544
Brandon Wyman32453e92021-12-15 19:00:14 +0000545 if (present)
Brandon Wyman3f1242f2020-01-28 13:11:25 -0600546 {
547 try
548 {
Brandon Wyman9e292ee2022-03-10 22:56:23 +0000549 statusWordOld = statusWord;
Brandon Wyman32453e92021-12-15 19:00:14 +0000550 statusWord = pmbusIntf->read(STATUS_WORD, Type::Debug,
551 (readFail < LOG_LIMIT));
Brandon Wymanf65c4062020-08-19 13:15:53 -0500552 // Read worked, reset the fail count.
553 readFail = 0;
Brandon Wyman3f1242f2020-01-28 13:11:25 -0600554
555 if (statusWord)
556 {
Brandon Wymanf07bc792021-10-12 19:00:35 +0000557 statusInput = pmbusIntf->read(STATUS_INPUT, Type::Debug);
Jay Meyer10d94052020-11-30 14:41:21 -0600558 statusMFR = pmbusIntf->read(STATUS_MFR, Type::Debug);
Brandon Wyman85c7bf42021-10-19 22:28:48 +0000559 statusCML = pmbusIntf->read(STATUS_CML, Type::Debug);
Brandon Wyman6710ba22021-10-27 17:39:31 +0000560 auto status0Vout = pmbusIntf->insertPageNum(STATUS_VOUT, 0);
561 statusVout = pmbusIntf->read(status0Vout, Type::Debug);
Brandon Wymanb10b3be2021-11-09 22:12:15 +0000562 statusIout = pmbusIntf->read(STATUS_IOUT, Type::Debug);
Brandon Wyman7ee4d7e2021-11-19 20:48:23 +0000563 statusFans12 = pmbusIntf->read(STATUS_FANS_1_2, Type::Debug);
Brandon Wyman96893a42021-11-05 19:56:57 +0000564 statusTemperature =
565 pmbusIntf->read(STATUS_TEMPERATURE, Type::Debug);
Brandon Wyman9ddc6222021-10-28 17:28:01 +0000566
Brandon Wymanc2203432021-12-21 23:09:48 +0000567 analyzeCMLFault();
Brandon Wyman85c7bf42021-10-19 22:28:48 +0000568
Brandon Wymane3b0bb02021-12-21 23:16:48 +0000569 analyzeInputFault();
Brandon Wyman3f1242f2020-01-28 13:11:25 -0600570
Brandon Wymanc2c87132021-12-21 23:22:18 +0000571 analyzeVoutOVFault();
Brandon Wyman6710ba22021-10-27 17:39:31 +0000572
Brandon Wymana00e7302021-12-21 23:28:29 +0000573 analyzeIoutOCFault();
Brandon Wymanb10b3be2021-11-09 22:12:15 +0000574
Brandon Wyman08378782021-12-21 23:48:15 +0000575 analyzeVoutUVFault();
Brandon Wyman2cf46942021-10-28 19:09:16 +0000576
Brandon Wymand5d9a222021-12-21 23:59:05 +0000577 analyzeFanFault();
Brandon Wyman7ee4d7e2021-11-19 20:48:23 +0000578
Brandon Wyman52cb3f22021-12-21 23:02:47 +0000579 analyzeTemperatureFault();
Brandon Wyman96893a42021-11-05 19:56:57 +0000580
Brandon Wyman993b5542021-12-21 22:55:16 +0000581 analyzePgoodFault();
Brandon Wyman2916ea52021-11-06 03:31:18 +0000582
Brandon Wyman6c2ac392021-12-21 22:23:06 +0000583 analyzeMFRFault();
Brandon Wyman3f1242f2020-01-28 13:11:25 -0600584
Brandon Wymanf087f472021-12-22 00:04:27 +0000585 analyzeVinUVFault();
Brandon Wyman3f1242f2020-01-28 13:11:25 -0600586 }
587 else
588 {
Brandon Wyman9e292ee2022-03-10 22:56:23 +0000589 if (statusWord != statusWordOld)
590 {
Brandon Wyman321a6152022-03-19 00:11:44 +0000591 log<level::INFO>(fmt::format("{} STATUS_WORD = {:#06x}",
592 shortName, statusWord)
Brandon Wyman9e292ee2022-03-10 22:56:23 +0000593 .c_str());
594 }
595
Brandon Wymane3f7ad22021-12-21 20:27:45 +0000596 // if INPUT/VIN_UV fault was on, it cleared, trace it.
597 if (inputFault)
598 {
599 log<level::INFO>(
600 fmt::format(
Brandon Wyman321a6152022-03-19 00:11:44 +0000601 "{} INPUT fault cleared: STATUS_WORD = {:#06x}",
602 shortName, statusWord)
Brandon Wymane3f7ad22021-12-21 20:27:45 +0000603 .c_str());
604 }
605
606 if (vinUVFault)
607 {
608 log<level::INFO>(
Brandon Wyman321a6152022-03-19 00:11:44 +0000609 fmt::format("{} VIN_UV cleared: STATUS_WORD = {:#06x}",
610 shortName, statusWord)
Brandon Wymane3f7ad22021-12-21 20:27:45 +0000611 .c_str());
612 }
613
Brandon Wyman06ca4592021-12-06 22:52:23 +0000614 if (pgoodFault > 0)
Brandon Wyman4aecc292021-11-10 22:40:41 +0000615 {
Brandon Wyman321a6152022-03-19 00:11:44 +0000616 log<level::INFO>(
617 fmt::format("{} pgoodFault cleared", shortName)
618 .c_str());
Brandon Wyman4aecc292021-11-10 22:40:41 +0000619 }
Brandon Wymane3f7ad22021-12-21 20:27:45 +0000620
621 clearFaultFlags();
Jim Wright4ab86562022-11-18 14:05:46 -0600622 // No AC fail, decrement counter
Jim Wright7f9288c2022-12-08 11:57:04 -0600623 if (acFault != 0)
Jim Wright4ab86562022-11-18 14:05:46 -0600624 {
625 --acFault;
626 }
Brandon Wyman3f1242f2020-01-28 13:11:25 -0600627 }
Brandon Wyman82affd92021-11-24 19:12:49 +0000628
629 // Save off old inputVoltage value.
630 // Get latest inputVoltage.
631 // If voltage went from below minimum, and now is not, clear faults.
632 // Note: getInputVoltage() has its own try/catch.
633 int inputVoltageOld = inputVoltage;
Brandon Wyman4fc191f2022-03-10 23:07:13 +0000634 double actualInputVoltageOld = actualInputVoltage;
Brandon Wyman82affd92021-11-24 19:12:49 +0000635 getInputVoltage(actualInputVoltage, inputVoltage);
636 if ((inputVoltageOld == in_input::VIN_VOLTAGE_0) &&
637 (inputVoltage != in_input::VIN_VOLTAGE_0))
638 {
639 log<level::INFO>(
640 fmt::format(
Brandon Wyman4fc191f2022-03-10 23:07:13 +0000641 "{} READ_VIN back in range: actualInputVoltageOld = {} "
642 "actualInputVoltage = {}",
643 shortName, actualInputVoltageOld, actualInputVoltage)
Brandon Wyman82affd92021-11-24 19:12:49 +0000644 .c_str());
Brandon Wyman3225a452022-03-18 18:51:49 +0000645 clearVinUVFault();
Brandon Wyman82affd92021-11-24 19:12:49 +0000646 }
Brandon Wyman4fc191f2022-03-10 23:07:13 +0000647 else if (vinUVFault && (inputVoltage != in_input::VIN_VOLTAGE_0))
648 {
649 log<level::INFO>(
650 fmt::format(
651 "{} CLEAR_FAULTS: vinUVFault {} actualInputVoltage {}",
652 shortName, vinUVFault, actualInputVoltage)
653 .c_str());
654 // Do we have a VIN_UV fault latched that can now be cleared
Jim Wright4ab86562022-11-18 14:05:46 -0600655 // due to voltage back in range? Attempt to clear the
656 // fault(s), re-check faults on next call.
Brandon Wyman3225a452022-03-18 18:51:49 +0000657 clearVinUVFault();
Brandon Wyman4fc191f2022-03-10 23:07:13 +0000658 }
Brandon Wymanae35ac52022-05-23 22:33:40 +0000659 else if (std::abs(actualInputVoltageOld - actualInputVoltage) >
660 10.0)
Brandon Wyman4fc191f2022-03-10 23:07:13 +0000661 {
662 log<level::INFO>(
663 fmt::format(
664 "{} actualInputVoltageOld = {} actualInputVoltage = {}",
665 shortName, actualInputVoltageOld, actualInputVoltage)
666 .c_str());
667 }
Matt Spinler0975eaf2022-02-14 15:38:30 -0600668
669 checkAvailability();
Brandon Wymanc3324422022-03-24 20:30:57 +0000670
671 if (inputHistorySupported)
672 {
673 updateHistory();
674 }
Brandon Wyman3f1242f2020-01-28 13:11:25 -0600675 }
Patrick Williamsc1d4de52021-10-06 12:45:57 -0500676 catch (const ReadFailure& e)
Brandon Wyman3f1242f2020-01-28 13:11:25 -0600677 {
Brandon Wyman32453e92021-12-15 19:00:14 +0000678 if (readFail < SIZE_MAX)
679 {
680 readFail++;
681 }
682 if (readFail == LOG_LIMIT)
683 {
684 phosphor::logging::commit<ReadFailure>();
685 }
Brandon Wyman3f1242f2020-01-28 13:11:25 -0600686 }
687 }
688}
689
Brandon Wyman59a35792020-06-04 12:37:40 -0500690void PowerSupply::onOffConfig(uint8_t data)
691{
692 using namespace phosphor::pmbus;
693
694 if (present)
695 {
696 log<level::INFO>("ON_OFF_CONFIG write", entry("DATA=0x%02X", data));
697 try
698 {
699 std::vector<uint8_t> configData{data};
700 pmbusIntf->writeBinary(ON_OFF_CONFIG, configData,
701 Type::HwmonDeviceDebug);
702 }
703 catch (...)
704 {
705 // The underlying code in writeBinary will log a message to the
B. J. Wyman681b2a32021-04-20 22:31:22 +0000706 // journal if the write fails. If the ON_OFF_CONFIG is not setup
707 // as desired, later fault detection and analysis code should
708 // catch any of the fall out. We should not need to terminate
709 // the application if this write fails.
Brandon Wyman59a35792020-06-04 12:37:40 -0500710 }
711 }
712}
713
Brandon Wyman3225a452022-03-18 18:51:49 +0000714void PowerSupply::clearVinUVFault()
715{
716 // Read in1_lcrit_alarm to clear bits 3 and 4 of STATUS_INPUT.
717 // The fault bits in STAUTS_INPUT roll-up to STATUS_WORD. Clearing those
718 // bits in STATUS_INPUT should result in the corresponding STATUS_WORD bits
719 // also clearing.
720 //
721 // Do not care about return value. Should be 1 if active, 0 if not.
722 static_cast<void>(
723 pmbusIntf->read("in1_lcrit_alarm", phosphor::pmbus::Type::Hwmon));
724 vinUVFault = 0;
725}
726
Brandon Wyman3c208462020-05-13 16:25:58 -0500727void PowerSupply::clearFaults()
728{
Brandon Wyman82affd92021-11-24 19:12:49 +0000729 log<level::DEBUG>(
730 fmt::format("clearFaults() inventoryPath: {}", inventoryPath).c_str());
Brandon Wyman5474c912021-02-23 14:39:43 -0600731 faultLogged = false;
Brandon Wyman3c208462020-05-13 16:25:58 -0500732 // The PMBus device driver does not allow for writing CLEAR_FAULTS
733 // directly. However, the pmbus hwmon device driver code will send a
734 // CLEAR_FAULTS after reading from any of the hwmon "files" in sysfs, so
735 // reading in1_input should result in clearing the fault bits in
736 // STATUS_BYTE/STATUS_WORD.
737 // I do not care what the return value is.
Brandon Wyman11151532020-11-10 13:45:57 -0600738 if (present)
Brandon Wyman3c208462020-05-13 16:25:58 -0500739 {
Brandon Wymane3f7ad22021-12-21 20:27:45 +0000740 clearFaultFlags();
Matt Spinler0975eaf2022-02-14 15:38:30 -0600741 checkAvailability();
Brandon Wyman9564e942020-11-10 14:01:42 -0600742 readFail = 0;
Brandon Wyman9564e942020-11-10 14:01:42 -0600743
Brandon Wyman11151532020-11-10 13:45:57 -0600744 try
745 {
Brandon Wyman3225a452022-03-18 18:51:49 +0000746 clearVinUVFault();
Brandon Wyman11151532020-11-10 13:45:57 -0600747 static_cast<void>(
748 pmbusIntf->read("in1_input", phosphor::pmbus::Type::Hwmon));
749 }
Patrick Williamsc1d4de52021-10-06 12:45:57 -0500750 catch (const ReadFailure& e)
Brandon Wyman11151532020-11-10 13:45:57 -0600751 {
752 // Since I do not care what the return value is, I really do not
B. J. Wyman681b2a32021-04-20 22:31:22 +0000753 // care much if it gets a ReadFailure either. However, this
754 // should not prevent the application from continuing to run, so
755 // catching the read failure.
Brandon Wyman11151532020-11-10 13:45:57 -0600756 }
Brandon Wyman3c208462020-05-13 16:25:58 -0500757 }
758}
759
Patrick Williams7354ce62022-07-22 19:26:56 -0500760void PowerSupply::inventoryChanged(sdbusplus::message_t& msg)
Brandon Wymanaed1f752019-11-25 18:10:52 -0600761{
762 std::string msgSensor;
Patrick Williamsabe49412020-05-13 17:59:47 -0500763 std::map<std::string, std::variant<uint32_t, bool>> msgData;
Brandon Wymanaed1f752019-11-25 18:10:52 -0600764 msg.read(msgSensor, msgData);
765
766 // Check if it was the Present property that changed.
767 auto valPropMap = msgData.find(PRESENT_PROP);
768 if (valPropMap != msgData.end())
769 {
770 if (std::get<bool>(valPropMap->second))
771 {
772 present = true;
B. J. Wyman681b2a32021-04-20 22:31:22 +0000773 // TODO: Immediately trying to read or write the "files" causes
774 // read or write failures.
Brandon Wyman1d7a7df2020-03-26 10:14:05 -0500775 using namespace std::chrono_literals;
776 std::this_thread::sleep_for(20ms);
Brandon Wyman9564e942020-11-10 14:01:42 -0600777 pmbusIntf->findHwmonDir();
Brandon Wyman59a35792020-06-04 12:37:40 -0500778 onOffConfig(phosphor::pmbus::ON_OFF_CONFIG_CONTROL_PIN_ONLY);
Brandon Wymanaed1f752019-11-25 18:10:52 -0600779 clearFaults();
Brandon Wyman1d7a7df2020-03-26 10:14:05 -0500780 updateInventory();
Brandon Wymanaed1f752019-11-25 18:10:52 -0600781 }
782 else
783 {
784 present = false;
785
786 // Clear out the now outdated inventory properties
787 updateInventory();
788 }
Matt Spinler0975eaf2022-02-14 15:38:30 -0600789 checkAvailability();
Brandon Wymanaed1f752019-11-25 18:10:52 -0600790 }
791}
792
Patrick Williams7354ce62022-07-22 19:26:56 -0500793void PowerSupply::inventoryAdded(sdbusplus::message_t& msg)
Brandon Wyman9a507db2021-02-25 16:15:22 -0600794{
795 sdbusplus::message::object_path path;
796 msg.read(path);
797 // Make sure the signal is for the PSU inventory path
798 if (path == inventoryPath)
799 {
800 std::map<std::string, std::map<std::string, std::variant<bool>>>
801 interfaces;
802 // Get map of interfaces and their properties
803 msg.read(interfaces);
804
805 auto properties = interfaces.find(INVENTORY_IFACE);
806 if (properties != interfaces.end())
807 {
808 auto property = properties->second.find(PRESENT_PROP);
809 if (property != properties->second.end())
810 {
811 present = std::get<bool>(property->second);
812
813 log<level::INFO>(fmt::format("Power Supply {} Present {}",
814 inventoryPath, present)
815 .c_str());
816
817 updateInventory();
Matt Spinler0975eaf2022-02-14 15:38:30 -0600818 checkAvailability();
Brandon Wyman9a507db2021-02-25 16:15:22 -0600819 }
820 }
821 }
822}
823
Brandon Wyman8393f462022-06-28 16:06:46 +0000824auto PowerSupply::readVPDValue(const std::string& vpdName,
825 const phosphor::pmbus::Type& type,
826 const std::size_t& vpdSize)
827{
828 std::string vpdValue;
Brandon Wyman056935c2022-06-24 23:05:09 +0000829 const std::regex illegalVPDRegex =
830 std::regex("[^[:alnum:]]", std::regex::basic);
Brandon Wyman8393f462022-06-28 16:06:46 +0000831
832 try
833 {
834 vpdValue = pmbusIntf->readString(vpdName, type);
835 }
836 catch (const ReadFailure& e)
837 {
838 // Ignore the read failure, let pmbus code indicate failure,
839 // path...
840 // TODO - ibm918
841 // https://github.com/openbmc/docs/blob/master/designs/vpd-collection.md
842 // The BMC must log errors if any of the VPD cannot be properly
843 // parsed or fails ECC checks.
844 }
845
846 if (vpdValue.size() != vpdSize)
847 {
848 log<level::INFO>(fmt::format("{} {} resize needed. size: {}", shortName,
849 vpdName, vpdValue.size())
850 .c_str());
851 vpdValue.resize(vpdSize, ' ');
852 }
853
Brandon Wyman056935c2022-06-24 23:05:09 +0000854 // Replace any illegal values with space(s).
855 std::regex_replace(vpdValue.begin(), vpdValue.begin(), vpdValue.end(),
856 illegalVPDRegex, " ");
857
Brandon Wyman8393f462022-06-28 16:06:46 +0000858 return vpdValue;
859}
860
Brandon Wyman1d7a7df2020-03-26 10:14:05 -0500861void PowerSupply::updateInventory()
862{
863 using namespace phosphor::pmbus;
864
Chanh Nguyenc12c53b2021-04-06 17:24:47 +0700865#if IBM_VPD
Brandon Wyman1d7a7df2020-03-26 10:14:05 -0500866 std::string pn;
867 std::string fn;
868 std::string header;
869 std::string sn;
Brandon Wyman8393f462022-06-28 16:06:46 +0000870 // The IBM power supply splits the full serial number into two parts.
871 // Each part is 6 bytes long, which should match up with SN_KW_SIZE.
872 const auto HEADER_SIZE = 6;
873 const auto SERIAL_SIZE = 6;
874 // The IBM PSU firmware version size is a bit complicated. It was originally
875 // 1-byte, per command. It was later expanded to 2-bytes per command, then
876 // up to 8-bytes per command. The device driver only reads up to 2 bytes per
877 // command, but combines all three of the 2-byte reads, or all 4 of the
878 // 1-byte reads into one string. So, the maximum size expected is 6 bytes.
Shawn McCarney983c9892022-10-12 10:49:47 -0500879 // However, it is formatted by the driver as a hex string with two ASCII
880 // characters per byte. So the maximum ASCII string size is 12.
881 const auto VERSION_SIZE = 12;
Brandon Wyman8393f462022-06-28 16:06:46 +0000882
Brandon Wyman1d7a7df2020-03-26 10:14:05 -0500883 using PropertyMap =
George Liu070c1bc2020-10-12 11:28:01 +0800884 std::map<std::string,
885 std::variant<std::string, std::vector<uint8_t>, bool>>;
Brandon Wyman1d7a7df2020-03-26 10:14:05 -0500886 PropertyMap assetProps;
George Liu070c1bc2020-10-12 11:28:01 +0800887 PropertyMap operProps;
Brandon Wyman1d7a7df2020-03-26 10:14:05 -0500888 PropertyMap versionProps;
889 PropertyMap ipzvpdDINFProps;
890 PropertyMap ipzvpdVINIProps;
891 using InterfaceMap = std::map<std::string, PropertyMap>;
892 InterfaceMap interfaces;
893 using ObjectMap = std::map<sdbusplus::message::object_path, InterfaceMap>;
894 ObjectMap object;
895#endif
B. J. Wyman681b2a32021-04-20 22:31:22 +0000896 log<level::DEBUG>(
897 fmt::format("updateInventory() inventoryPath: {}", inventoryPath)
898 .c_str());
Brandon Wyman1d7a7df2020-03-26 10:14:05 -0500899
900 if (present)
901 {
902 // TODO: non-IBM inventory updates?
903
Chanh Nguyenc12c53b2021-04-06 17:24:47 +0700904#if IBM_VPD
Brandon Wyman8393f462022-06-28 16:06:46 +0000905 modelName = readVPDValue(CCIN, Type::HwmonDeviceDebug, CC_KW_SIZE);
906 assetProps.emplace(MODEL_PROP, modelName);
Brandon Wyman1d7a7df2020-03-26 10:14:05 -0500907
Matt Spinlerb40f04c2023-03-20 11:07:44 -0500908 pn = readVPDValue(PART_NUMBER, Type::Debug, PN_KW_SIZE);
Brandon Wyman8393f462022-06-28 16:06:46 +0000909 assetProps.emplace(PN_PROP, pn);
Brandon Wyman1d7a7df2020-03-26 10:14:05 -0500910
Matt Spinlerb40f04c2023-03-20 11:07:44 -0500911 fn = readVPDValue(FRU_NUMBER, Type::Debug, FN_KW_SIZE);
Brandon Wyman8393f462022-06-28 16:06:46 +0000912 assetProps.emplace(SPARE_PN_PROP, fn);
Brandon Wyman1d7a7df2020-03-26 10:14:05 -0500913
Matt Spinlerb40f04c2023-03-20 11:07:44 -0500914 header = readVPDValue(SERIAL_HEADER, Type::Debug, HEADER_SIZE);
915 sn = readVPDValue(SERIAL_NUMBER, Type::Debug, SERIAL_SIZE);
Brandon Wyman8393f462022-06-28 16:06:46 +0000916 assetProps.emplace(SN_PROP, header + sn);
Brandon Wyman1d7a7df2020-03-26 10:14:05 -0500917
Brandon Wyman8393f462022-06-28 16:06:46 +0000918 fwVersion =
919 readVPDValue(FW_VERSION, Type::HwmonDeviceDebug, VERSION_SIZE);
920 versionProps.emplace(VERSION_PROP, fwVersion);
Brandon Wyman1d7a7df2020-03-26 10:14:05 -0500921
Brandon Wyman8393f462022-06-28 16:06:46 +0000922 ipzvpdVINIProps.emplace(
923 "CC", std::vector<uint8_t>(modelName.begin(), modelName.end()));
Brandon Wyman1d7a7df2020-03-26 10:14:05 -0500924 ipzvpdVINIProps.emplace("PN",
925 std::vector<uint8_t>(pn.begin(), pn.end()));
926 ipzvpdVINIProps.emplace("FN",
927 std::vector<uint8_t>(fn.begin(), fn.end()));
Brandon Wyman33d492f2022-03-23 20:45:17 +0000928 std::string header_sn = header + sn;
Brandon Wyman1d7a7df2020-03-26 10:14:05 -0500929 ipzvpdVINIProps.emplace(
930 "SN", std::vector<uint8_t>(header_sn.begin(), header_sn.end()));
931 std::string description = "IBM PS";
932 ipzvpdVINIProps.emplace(
933 "DR", std::vector<uint8_t>(description.begin(), description.end()));
934
Ben Tynerf8d8c462022-01-27 16:09:45 -0600935 // Populate the VINI Resource Type (RT) keyword
936 ipzvpdVINIProps.emplace("RT", std::vector<uint8_t>{'V', 'I', 'N', 'I'});
937
Brandon Wyman1d7a7df2020-03-26 10:14:05 -0500938 // Update the Resource Identifier (RI) keyword
939 // 2 byte FRC: 0x0003
940 // 2 byte RID: 0x1000, 0x1001...
941 std::uint8_t num = std::stoul(
942 inventoryPath.substr(inventoryPath.size() - 1, 1), nullptr, 0);
943 std::vector<uint8_t> ri{0x00, 0x03, 0x10, num};
944 ipzvpdDINFProps.emplace("RI", ri);
945
946 // Fill in the FRU Label (FL) keyword.
947 std::string fl = "E";
948 fl.push_back(inventoryPath.back());
949 fl.resize(FL_KW_SIZE, ' ');
950 ipzvpdDINFProps.emplace("FL",
951 std::vector<uint8_t>(fl.begin(), fl.end()));
952
Ben Tynerf8d8c462022-01-27 16:09:45 -0600953 // Populate the DINF Resource Type (RT) keyword
954 ipzvpdDINFProps.emplace("RT", std::vector<uint8_t>{'D', 'I', 'N', 'F'});
955
Brandon Wyman1d7a7df2020-03-26 10:14:05 -0500956 interfaces.emplace(ASSET_IFACE, std::move(assetProps));
957 interfaces.emplace(VERSION_IFACE, std::move(versionProps));
958 interfaces.emplace(DINF_IFACE, std::move(ipzvpdDINFProps));
959 interfaces.emplace(VINI_IFACE, std::move(ipzvpdVINIProps));
960
George Liu070c1bc2020-10-12 11:28:01 +0800961 // Update the Functional
962 operProps.emplace(FUNCTIONAL_PROP, present);
963 interfaces.emplace(OPERATIONAL_STATE_IFACE, std::move(operProps));
964
Brandon Wyman1d7a7df2020-03-26 10:14:05 -0500965 auto path = inventoryPath.substr(strlen(INVENTORY_OBJ_PATH));
966 object.emplace(path, std::move(interfaces));
967
968 try
969 {
970 auto service =
971 util::getService(INVENTORY_OBJ_PATH, INVENTORY_MGR_IFACE, bus);
972
973 if (service.empty())
974 {
975 log<level::ERR>("Unable to get inventory manager service");
976 return;
977 }
978
979 auto method =
980 bus.new_method_call(service.c_str(), INVENTORY_OBJ_PATH,
981 INVENTORY_MGR_IFACE, "Notify");
982
983 method.append(std::move(object));
984
985 auto reply = bus.call(method);
986 }
Patrick Williamsc1d4de52021-10-06 12:45:57 -0500987 catch (const std::exception& e)
Brandon Wyman1d7a7df2020-03-26 10:14:05 -0500988 {
Jay Meyer6a3fd2c2020-08-25 16:37:16 -0500989 log<level::ERR>(
990 std::string(e.what() + std::string(" PATH=") + inventoryPath)
991 .c_str());
Brandon Wyman1d7a7df2020-03-26 10:14:05 -0500992 }
993#endif
994 }
995}
996
Brandon Wymanae35ac52022-05-23 22:33:40 +0000997auto PowerSupply::getMaxPowerOut() const
998{
999 using namespace phosphor::pmbus;
1000
1001 auto maxPowerOut = 0;
1002
1003 if (present)
1004 {
1005 try
1006 {
1007 // Read max_power_out, should be direct format
1008 auto maxPowerOutStr =
1009 pmbusIntf->readString(MFR_POUT_MAX, Type::HwmonDeviceDebug);
1010 log<level::INFO>(fmt::format("{} MFR_POUT_MAX read {}", shortName,
1011 maxPowerOutStr)
1012 .c_str());
1013 maxPowerOut = std::stod(maxPowerOutStr);
1014 }
1015 catch (const std::exception& e)
1016 {
1017 log<level::ERR>(fmt::format("{} MFR_POUT_MAX read error: {}",
1018 shortName, e.what())
1019 .c_str());
1020 }
1021 }
1022
1023 return maxPowerOut;
1024}
1025
Brandon Wymanc3324422022-03-24 20:30:57 +00001026void PowerSupply::setupInputHistory()
1027{
1028 if (bindPath.string().find("ibm-cffps") != std::string::npos)
1029 {
Brandon Wymanae35ac52022-05-23 22:33:40 +00001030 auto maxPowerOut = getMaxPowerOut();
1031
1032 if (maxPowerOut != phosphor::pmbus::IBM_CFFPS_1400W)
Brandon Wymanc3324422022-03-24 20:30:57 +00001033 {
Brandon Wymanae35ac52022-05-23 22:33:40 +00001034 // Do not enable input history for power supplies that are missing
1035 if (present)
1036 {
1037 inputHistorySupported = true;
1038 log<level::INFO>(
1039 fmt::format("{} INPUT_HISTORY enabled", shortName).c_str());
1040
1041 std::string name{fmt::format("{}_input_power", shortName)};
1042
1043 historyObjectPath =
1044 std::string{INPUT_HISTORY_SENSOR_ROOT} + '/' + name;
1045
1046 // If the power supply was present, we created the
1047 // recordManager. If it then went missing, the recordManager is
1048 // still there. If it then is reinserted, we should be able to
1049 // use the recordManager that was allocated when it was
1050 // initially present.
1051 if (!recordManager)
1052 {
1053 recordManager = std::make_unique<history::RecordManager>(
1054 INPUT_HISTORY_MAX_RECORDS);
1055 }
1056
1057 if (!average)
1058 {
1059 auto avgPath =
1060 historyObjectPath + '/' + history::Average::name;
1061 average = std::make_unique<history::Average>(bus, avgPath);
1062 log<level::DEBUG>(
1063 fmt::format("{} avgPath: {}", shortName, avgPath)
1064 .c_str());
1065 }
1066
1067 if (!maximum)
1068 {
1069 auto maxPath =
1070 historyObjectPath + '/' + history::Maximum::name;
1071 maximum = std::make_unique<history::Maximum>(bus, maxPath);
1072 log<level::DEBUG>(
1073 fmt::format("{} maxPath: {}", shortName, maxPath)
1074 .c_str());
1075 }
1076
1077 log<level::DEBUG>(fmt::format("{} historyObjectPath: {}",
1078 shortName, historyObjectPath)
1079 .c_str());
1080 }
1081 }
1082 else
1083 {
Brandon Wymanc3324422022-03-24 20:30:57 +00001084 log<level::INFO>(
Brandon Wymanae35ac52022-05-23 22:33:40 +00001085 fmt::format("{} INPUT_HISTORY DISABLED. max_power_out: {}",
1086 shortName, maxPowerOut)
1087 .c_str());
1088 inputHistorySupported = false;
Brandon Wymanc3324422022-03-24 20:30:57 +00001089 }
1090 }
1091 else
1092 {
1093 inputHistorySupported = false;
1094 }
1095}
1096
1097void PowerSupply::updateHistory()
1098{
1099 if (!recordManager)
1100 {
1101 // Not enabled
1102 return;
1103 }
1104
1105 if (!present)
1106 {
1107 // Cannot read when not present
1108 return;
1109 }
1110
1111 // Read just the most recent average/max record
1112 auto data =
1113 pmbusIntf->readBinary(INPUT_HISTORY, pmbus::Type::HwmonDeviceDebug,
1114 history::RecordManager::RAW_RECORD_SIZE);
1115
Brandon Wymanae35ac52022-05-23 22:33:40 +00001116 // Update D-Bus only if something changed (a new record ID, or cleared
1117 // out)
Brandon Wymanc3324422022-03-24 20:30:57 +00001118 auto changed = recordManager->add(data);
1119 if (changed)
1120 {
1121 average->values(std::move(recordManager->getAverageRecords()));
1122 maximum->values(std::move(recordManager->getMaximumRecords()));
1123 }
1124}
1125
Adriana Kobylak4175ffb2021-08-02 14:51:05 +00001126void PowerSupply::getInputVoltage(double& actualInputVoltage,
1127 int& inputVoltage) const
1128{
1129 using namespace phosphor::pmbus;
1130
1131 actualInputVoltage = in_input::VIN_VOLTAGE_0;
1132 inputVoltage = in_input::VIN_VOLTAGE_0;
1133
1134 if (present)
1135 {
1136 try
1137 {
1138 // Read input voltage in millivolts
1139 auto inputVoltageStr = pmbusIntf->readString(READ_VIN, Type::Hwmon);
1140
1141 // Convert to volts
1142 actualInputVoltage = std::stod(inputVoltageStr) / 1000;
1143
1144 // Calculate the voltage based on voltage thresholds
1145 if (actualInputVoltage < in_input::VIN_VOLTAGE_MIN)
1146 {
1147 inputVoltage = in_input::VIN_VOLTAGE_0;
1148 }
1149 else if (actualInputVoltage < in_input::VIN_VOLTAGE_110_THRESHOLD)
1150 {
1151 inputVoltage = in_input::VIN_VOLTAGE_110;
1152 }
1153 else
1154 {
1155 inputVoltage = in_input::VIN_VOLTAGE_220;
1156 }
1157 }
1158 catch (const std::exception& e)
1159 {
1160 log<level::ERR>(
Brandon Wyman321a6152022-03-19 00:11:44 +00001161 fmt::format("{} READ_VIN read error: {}", shortName, e.what())
1162 .c_str());
Adriana Kobylak4175ffb2021-08-02 14:51:05 +00001163 }
1164 }
1165}
1166
Matt Spinler0975eaf2022-02-14 15:38:30 -06001167void PowerSupply::checkAvailability()
1168{
1169 bool origAvailability = available;
George Liu9464c422023-02-27 14:30:27 +08001170 bool faulted = isPowerOn() && (hasPSKillFault() || hasIoutOCFault());
1171 available = present && !hasInputFault() && !hasVINUVFault() && !faulted;
Matt Spinler0975eaf2022-02-14 15:38:30 -06001172
1173 if (origAvailability != available)
1174 {
1175 auto invpath = inventoryPath.substr(strlen(INVENTORY_OBJ_PATH));
1176 phosphor::power::psu::setAvailable(bus, invpath, available);
Matt Spinlerca1e9ea2022-02-18 14:03:08 -06001177
1178 // Check if the health rollup needs to change based on the
1179 // new availability value.
1180 phosphor::power::psu::handleChassisHealthRollup(bus, inventoryPath,
1181 !available);
Matt Spinler0975eaf2022-02-14 15:38:30 -06001182 }
1183}
1184
Matt Spinlera068f422023-03-10 13:06:49 -06001185void PowerSupply::setInputVoltageRating()
1186{
1187 if (!present)
1188 {
1189 return;
1190 }
1191
1192 double inputVoltageValue{};
1193 int inputVoltageRating{};
1194 getInputVoltage(inputVoltageValue, inputVoltageRating);
1195
1196 if (!inputVoltageRatingIface)
1197 {
1198 auto path = fmt::format(
1199 "/xyz/openbmc_project/sensors/voltage/ps{}_input_voltage_rating",
1200 shortName.back());
1201
1202 inputVoltageRatingIface = std::make_unique<SensorObject>(
1203 bus, path.c_str(), SensorObject::action::defer_emit);
1204
1205 // Leave other properties at their defaults
1206 inputVoltageRatingIface->unit(SensorInterface::Unit::Volts, true);
1207 inputVoltageRatingIface->value(static_cast<double>(inputVoltageRating),
1208 true);
1209
1210 inputVoltageRatingIface->emit_object_added();
1211 }
1212 else
1213 {
1214 inputVoltageRatingIface->value(static_cast<double>(inputVoltageRating));
1215 }
1216}
1217
Brandon Wyman3f1242f2020-01-28 13:11:25 -06001218} // namespace phosphor::power::psu