blob: 8bd2a3034fd077c7c29728c6d271ee844d0c5a52 [file] [log] [blame]
Brandon Wymana0f33ce2019-10-17 18:32:29 -05001#pragma once
2
Brandon Wyman8d195772020-01-27 15:03:51 -06003#include "pmbus.hpp"
Brandon Wymanaed1f752019-11-25 18:10:52 -06004#include "types.hpp"
B. J. Wyman681b2a32021-04-20 22:31:22 +00005#include "util.hpp"
Brandon Wyman3f1242f2020-01-28 13:11:25 -06006#include "utility.hpp"
Brandon Wymanaed1f752019-11-25 18:10:52 -06007
B. J. Wyman681b2a32021-04-20 22:31:22 +00008#include <gpiod.hpp>
Brandon Wymanaed1f752019-11-25 18:10:52 -06009#include <sdbusplus/bus/match.hpp>
10
B. J. Wyman681b2a32021-04-20 22:31:22 +000011#include <filesystem>
Brandon Wyman1d7a7df2020-03-26 10:14:05 -050012#include <stdexcept>
13
Brandon Wymana0f33ce2019-10-17 18:32:29 -050014namespace phosphor::power::psu
15{
Brandon Wyman3f1242f2020-01-28 13:11:25 -060016
Chanh Nguyenc12c53b2021-04-06 17:24:47 +070017#if IBM_VPD
Brandon Wyman1d7a7df2020-03-26 10:14:05 -050018// PMBus device driver "file name" to read for CCIN value.
19constexpr auto CCIN = "ccin";
20constexpr auto PART_NUMBER = "part_number";
21constexpr auto FRU_NUMBER = "fru";
22constexpr auto SERIAL_HEADER = "header";
23constexpr auto SERIAL_NUMBER = "serial_number";
24constexpr auto FW_VERSION = "fw_version";
25
26// The D-Bus property name to update with the CCIN value.
27constexpr auto MODEL_PROP = "Model";
28constexpr auto PN_PROP = "PartNumber";
29constexpr auto SN_PROP = "SerialNumber";
30constexpr auto VERSION_PROP = "Version";
31
32// ipzVPD Keyword sizes
33static constexpr auto FL_KW_SIZE = 20;
34#endif
35
Brandon Wymanf65c4062020-08-19 13:15:53 -050036constexpr auto LOG_LIMIT = 3;
37
Brandon Wymana0f33ce2019-10-17 18:32:29 -050038/**
39 * @class PowerSupply
40 * Represents a PMBus power supply device.
41 */
42class PowerSupply
43{
44 public:
Brandon Wymanaed1f752019-11-25 18:10:52 -060045 PowerSupply() = delete;
Brandon Wymana0f33ce2019-10-17 18:32:29 -050046 PowerSupply(const PowerSupply&) = delete;
47 PowerSupply(PowerSupply&&) = delete;
48 PowerSupply& operator=(const PowerSupply&) = delete;
49 PowerSupply& operator=(PowerSupply&&) = delete;
50 ~PowerSupply() = default;
51
52 /**
Brandon Wymanc63941c2020-01-27 16:49:33 -060053 * @param[in] invpath - String for inventory path to use
54 * @param[in] i2cbus - The bus number this power supply is on
55 * @param[in] i2caddr - The 16-bit I2C address of the power supply
B. J. Wyman681b2a32021-04-20 22:31:22 +000056 * @param[in] gpioLineName - The gpio-line-name to read for presence. See
57 * https://github.com/openbmc/docs/blob/master/designs/device-tree-gpio-naming.md
Brandon Wymanaed1f752019-11-25 18:10:52 -060058 */
Brandon Wymanc63941c2020-01-27 16:49:33 -060059 PowerSupply(sdbusplus::bus::bus& bus, const std::string& invpath,
B. J. Wyman681b2a32021-04-20 22:31:22 +000060 std::uint8_t i2cbus, const std::uint16_t i2caddr,
61 const std::string& gpioLineName);
Brandon Wymanaed1f752019-11-25 18:10:52 -060062
Brandon Wyman3f1242f2020-01-28 13:11:25 -060063 phosphor::pmbus::PMBusBase& getPMBus()
64 {
65 return *pmbusIntf;
66 }
67
Adriana Kobylak3ca062a2021-10-20 15:27:23 +000068 GPIOInterfaceBase* getPresenceGPIO()
B. J. Wyman681b2a32021-04-20 22:31:22 +000069 {
70 return presenceGPIO.get();
71 }
72
B. J. Wymand8b8cb12021-07-15 22:03:34 +000073 std::string getPresenceGPIOName() const
74 {
75 if (presenceGPIO != nullptr)
76 {
77 return presenceGPIO->getName();
78 }
79 else
80 {
81 return std::string();
82 }
83 }
84
Brandon Wymanaed1f752019-11-25 18:10:52 -060085 /**
Brandon Wymana0f33ce2019-10-17 18:32:29 -050086 * Power supply specific function to analyze for faults/errors.
87 *
88 * Various PMBus status bits will be checked for fault conditions.
89 * If a certain fault bits are on, the appropriate error will be
90 * committed.
91 */
Brandon Wyman3f1242f2020-01-28 13:11:25 -060092 void analyze();
Brandon Wymana0f33ce2019-10-17 18:32:29 -050093
94 /**
Brandon Wyman59a35792020-06-04 12:37:40 -050095 * Write PMBus ON_OFF_CONFIG
96 *
97 * This function will be called to cause the PMBus device driver to send the
98 * ON_OFF_CONFIG command. Takes one byte of data.
99 *
100 * @param[in] data - The ON_OFF_CONFIG data byte mask.
101 */
102 void onOffConfig(uint8_t data);
103
104 /**
Brandon Wymana0f33ce2019-10-17 18:32:29 -0500105 * Write PMBus CLEAR_FAULTS
106 *
107 * This function will be called in various situations in order to clear
108 * any fault status bits that may have been set, in order to start over
109 * with a clean state. Presence changes and power state changes will
110 * want to clear any faults logged.
111 */
Brandon Wyman3c208462020-05-13 16:25:58 -0500112 void clearFaults();
Brandon Wymana0f33ce2019-10-17 18:32:29 -0500113
114 /**
115 * @brief Adds properties to the inventory.
116 *
117 * Reads the values from the device and writes them to the
118 * associated power supply D-Bus inventory object.
119 *
120 * This needs to be done on startup, and each time the presence
121 * state changes.
122 *
123 * Properties added:
124 * - Serial Number
125 * - Part Number
126 * - CCIN (Customer Card Identification Number) - added as the Model
127 * - Firmware version
128 */
Brandon Wyman1d7a7df2020-03-26 10:14:05 -0500129 void updateInventory();
Brandon Wymana0f33ce2019-10-17 18:32:29 -0500130
Brandon Wymanaed1f752019-11-25 18:10:52 -0600131 /**
132 * @brief Accessor function to indicate present status
133 */
134 bool isPresent() const
135 {
136 return present;
137 }
138
Brandon Wyman3f1242f2020-01-28 13:11:25 -0600139 /**
Brandon Wymanfed0ba22020-09-26 20:02:51 -0500140 * @brief Returns the last read value from STATUS_WORD.
141 */
142 uint64_t getStatusWord() const
143 {
144 return statusWord;
145 }
146
147 /**
Brandon Wymanf07bc792021-10-12 19:00:35 +0000148 * @brief Returns the last read value from STATUS_INPUT.
149 */
150 uint64_t getStatusInput() const
151 {
152 return statusInput;
153 }
154
155 /**
Jay Meyer10d94052020-11-30 14:41:21 -0600156 * @brief Returns the last read value from STATUS_MFR.
157 */
158 uint64_t getMFRFault() const
159 {
160 return statusMFR;
161 }
162
163 /**
Brandon Wyman85c7bf42021-10-19 22:28:48 +0000164 * @brief Returns the last read value from STATUS_CML.
165 */
166 uint64_t getStatusCML() const
167 {
168 return statusCML;
169 }
170
171 /**
Brandon Wyman6710ba22021-10-27 17:39:31 +0000172 * @brief Returns the last read value from STATUS_VOUT.
173 */
174 uint64_t getStatusVout() const
175 {
176 return statusVout;
177 }
178
179 /**
Brandon Wymanb10b3be2021-11-09 22:12:15 +0000180 * @brief Returns the last value read from STATUS_IOUT.
181 */
182 uint64_t getStatusIout() const
183 {
184 return statusIout;
185 }
186
187 /**
Brandon Wyman96893a42021-11-05 19:56:57 +0000188 * @brief Returns the last value read from STATUS_TEMPERATURE.
189 */
190 uint64_t getStatusTemperature() const
191 {
192 return statusTemperature;
193 }
194
195 /**
Brandon Wyman3f1242f2020-01-28 13:11:25 -0600196 * @brief Returns true if a fault was found.
197 */
198 bool isFaulted() const
199 {
Brandon Wyman9ddc6222021-10-28 17:28:01 +0000200 return (hasCommFault() || vinUVFault || inputFault || voutOVFault ||
Brandon Wymanb10b3be2021-11-09 22:12:15 +0000201 ioutOCFault || tempFault || pgoodFault || mfrFault);
Brandon Wyman3f1242f2020-01-28 13:11:25 -0600202 }
203
204 /**
Brandon Wymanb76ab242020-09-16 18:06:06 -0500205 * @brief Return whether a fault has been logged for this power supply
206 */
207 bool isFaultLogged() const
208 {
209 return faultLogged;
210 }
211
212 /**
213 * @brief Called when a fault for this power supply has been logged.
214 */
215 void setFaultLogged()
216 {
217 faultLogged = true;
218 }
219
220 /**
Brandon Wyman3f1242f2020-01-28 13:11:25 -0600221 * @brief Returns true if INPUT fault occurred.
222 */
223 bool hasInputFault() const
224 {
225 return inputFault;
226 }
227
228 /**
229 * @brief Returns true if MFRSPECIFIC occurred.
230 */
231 bool hasMFRFault() const
232 {
233 return mfrFault;
234 }
235
236 /**
237 * @brief Returns true if VIN_UV_FAULT occurred.
238 */
239 bool hasVINUVFault() const
240 {
241 return vinUVFault;
242 }
243
Brandon Wymanc9efe412020-10-09 15:42:50 -0500244 /**
Brandon Wyman6710ba22021-10-27 17:39:31 +0000245 * @brief Returns true if VOUT_OV_FAULT occurred.
246 */
247 bool hasVoutOVFault() const
248 {
249 return voutOVFault;
250 }
251
252 /**
Brandon Wymanb10b3be2021-11-09 22:12:15 +0000253 * @brief Returns true if IOUT_OC fault occurred (bit 4 STATUS_BYTE).
254 */
255 bool hasIoutOCFault() const
256 {
257 return ioutOCFault;
258 }
259
260 /**
Brandon Wyman96893a42021-11-05 19:56:57 +0000261 * @brief Returns true if TEMPERATURE fault occurred.
262 */
263 bool hasTempFault() const
264 {
265 return tempFault;
266 }
267
268 /**
Brandon Wyman2916ea52021-11-06 03:31:18 +0000269 * @brief Returns true if there is a PGood fault (PGOOD# inactive, or OFF
270 * bit on).
271 */
272 bool hasPgoodFault() const
273 {
274 return pgoodFault;
275 }
276
277 /**
Brandon Wymanc9efe412020-10-09 15:42:50 -0500278 * @brief Returns the device path
279 *
280 * This can be used for error call outs.
281 * Example: /sys/bus/i2c/devices/3-0068
282 */
Brandon Wyman4176d6b2020-10-07 17:41:06 -0500283 const std::string getDevicePath() const
284 {
285 return pmbusIntf->path();
286 }
287
Brandon Wymanc9efe412020-10-09 15:42:50 -0500288 /**
289 * @brief Returns this power supplies inventory path.
290 *
291 * This can be used for error call outs.
292 * Example:
293 * /xyz/openbmc_project/inventory/system/chassis/motherboard/powersupply1
294 */
Brandon Wyman7e495272020-09-26 19:57:46 -0500295 const std::string& getInventoryPath() const
296 {
297 return inventoryPath;
298 }
299
Brandon Wymanc9efe412020-10-09 15:42:50 -0500300 /**
301 * @brief Returns the firmware revision version read from the power supply
302 */
303 const std::string& getFWVersion() const
304 {
305 return fwVersion;
306 }
307
Adriana Kobylak572a9052021-03-30 15:58:07 +0000308 /**
309 * @brief Returns the model name of the power supply
310 */
311 const std::string& getModelName() const
312 {
313 return modelName;
314 }
315
Brandon Wymanf65c4062020-08-19 13:15:53 -0500316 /** @brief Returns true if the number of failed reads exceeds limit
317 * TODO: or CML bit on.
318 */
319 bool hasCommFault() const
320 {
Brandon Wyman85c7bf42021-10-19 22:28:48 +0000321 return ((readFail >= LOG_LIMIT) || (cmlFault));
Brandon Wymanf65c4062020-08-19 13:15:53 -0500322 }
323
Adriana Kobylak4175ffb2021-08-02 14:51:05 +0000324 /**
325 * @brief Reads the pmbus input voltage and returns that actual voltage
326 * reading and the calculated input voltage based on thresholds.
327 * @param[out] actualInputVoltage - The actual voltage reading, in Volts.
328 * @param[out] inputVoltage - A rounded up/down value of the actual input
329 * voltage based on thresholds, in Volts.
330 */
331 void getInputVoltage(double& actualInputVoltage, int& inputVoltage) const;
332
Brandon Wymana0f33ce2019-10-17 18:32:29 -0500333 private:
Brandon Wymanaed1f752019-11-25 18:10:52 -0600334 /** @brief systemd bus member */
335 sdbusplus::bus::bus& bus;
336
Brandon Wyman9564e942020-11-10 14:01:42 -0600337 /** @brief Will be updated to the latest/lastvalue read from STATUS_WORD.*/
Brandon Wymanfed0ba22020-09-26 20:02:51 -0500338 uint64_t statusWord = 0;
339
Brandon Wymanf07bc792021-10-12 19:00:35 +0000340 /** @brief Will be updated to the latest/lastvalue read from STATUS_INPUT.*/
341 uint64_t statusInput = 0;
342
Jay Meyer10d94052020-11-30 14:41:21 -0600343 /** @brief Will be updated to the latest/lastvalue read from STATUS_MFR.*/
344 uint64_t statusMFR = 0;
345
Brandon Wyman85c7bf42021-10-19 22:28:48 +0000346 /** @brief Will be updated to the latest/last value read from STATUS_CML.*/
347 uint64_t statusCML = 0;
348
Brandon Wyman6710ba22021-10-27 17:39:31 +0000349 /** @brief Will be updated to the latest/last value read from STATUS_VOUT.*/
350 uint64_t statusVout = 0;
351
Brandon Wymanb10b3be2021-11-09 22:12:15 +0000352 /** @brief Will be updated to the latest/last value read from STATUS_IOUT.*/
353 uint64_t statusIout = 0;
354
Brandon Wyman96893a42021-11-05 19:56:57 +0000355 /** @brief Will be updated to the latest/last value read from
356 * STATUS_TEMPERATURE.*/
357 uint64_t statusTemperature = 0;
358
Brandon Wymanb76ab242020-09-16 18:06:06 -0500359 /** @brief True if an error for a fault has already been logged. */
360 bool faultLogged = false;
361
Brandon Wyman96893a42021-11-05 19:56:57 +0000362 /** @brief True if bit 1 of STATUS_WORD low byte is on. */
Brandon Wyman85c7bf42021-10-19 22:28:48 +0000363 bool cmlFault = false;
364
Brandon Wyman3f1242f2020-01-28 13:11:25 -0600365 /** @brief True if bit 5 of STATUS_WORD high byte is on. */
366 bool inputFault = false;
367
368 /** @brief True if bit 4 of STATUS_WORD high byte is on. */
369 bool mfrFault = false;
370
371 /** @brief True if bit 3 of STATUS_WORD low byte is on. */
372 bool vinUVFault = false;
373
Brandon Wyman6710ba22021-10-27 17:39:31 +0000374 /** @brief True if bit 5 of STATUS_WORD low byte is on. */
375 bool voutOVFault = false;
376
Brandon Wymanb10b3be2021-11-09 22:12:15 +0000377 /** @brief True if bit 4 of STATUS_WORD low byte is on. */
378 bool ioutOCFault = false;
379
Brandon Wyman96893a42021-11-05 19:56:57 +0000380 /** @brief True if bit 2 of STATUS_WORD low byte is on. */
381 bool tempFault = false;
382
Brandon Wyman2916ea52021-11-06 03:31:18 +0000383 /** @brief True if bit 11 or 6 of STATUS_WORD is on. PGOOD# is inactive, or
384 * the unit is off.
385 */
386 bool pgoodFault = false;
387
Brandon Wymanf65c4062020-08-19 13:15:53 -0500388 /** @brief Count of the number of read failures. */
389 size_t readFail = 0;
390
Brandon Wymanaed1f752019-11-25 18:10:52 -0600391 /**
392 * @brief D-Bus path to use for this power supply's inventory status.
393 **/
394 std::string inventoryPath;
395
B. J. Wyman681b2a32021-04-20 22:31:22 +0000396 /**
397 * @brief The libgpiod object for monitoring PSU presence
398 */
Adriana Kobylak3ca062a2021-10-20 15:27:23 +0000399 std::unique_ptr<GPIOInterfaceBase> presenceGPIO = nullptr;
B. J. Wyman681b2a32021-04-20 22:31:22 +0000400
Brandon Wymanaed1f752019-11-25 18:10:52 -0600401 /** @brief True if the power supply is present. */
402 bool present = false;
403
Adriana Kobylak572a9052021-03-30 15:58:07 +0000404 /** @brief Power supply model name. */
405 std::string modelName;
406
Brandon Wymanaed1f752019-11-25 18:10:52 -0600407 /** @brief D-Bus match variable used to subscribe to Present property
408 * changes.
409 **/
410 std::unique_ptr<sdbusplus::bus::match_t> presentMatch;
411
412 /** @brief D-Bus match variable used to subscribe for Present property
413 * interface added.
414 */
415 std::unique_ptr<sdbusplus::bus::match_t> presentAddedMatch;
416
417 /**
Brandon Wyman8d195772020-01-27 15:03:51 -0600418 * @brief Pointer to the PMBus interface
419 *
420 * Used to read or write to/from PMBus power supply devices.
421 */
Brandon Wyman9564e942020-11-10 14:01:42 -0600422 std::unique_ptr<phosphor::pmbus::PMBusBase> pmbusIntf = nullptr;
Brandon Wyman8d195772020-01-27 15:03:51 -0600423
Brandon Wymanc9efe412020-10-09 15:42:50 -0500424 /** @brief Stored copy of the firmware version/revision string */
425 std::string fwVersion;
426
Brandon Wyman8d195772020-01-27 15:03:51 -0600427 /**
B. J. Wyman681b2a32021-04-20 22:31:22 +0000428 * @brief The file system path used for binding the device driver.
429 */
430 const std::filesystem::path bindPath;
431
432 /* @brief The string to pass in for binding the device driver. */
433 std::string bindDevice;
434
435 /**
436 * @brief Binds or unbinds the power supply device driver
437 *
438 * Called when a presence change is detected to either bind the device
439 * driver for the power supply when it is installed, or unbind the device
440 * driver when the power supply is removed.
441 *
442 * Writes <device> to <path>/bind (or unbind)
443 *
444 * @param present - when true, will bind the device driver
445 * when false, will unbind the device driver
446 */
447 void bindOrUnbindDriver(bool present);
448
449 /**
Brandon Wymanaed1f752019-11-25 18:10:52 -0600450 * @brief Updates the presence status by querying D-Bus
451 *
452 * The D-Bus inventory properties for this power supply will be read to
453 * determine if the power supply is present or not and update this
454 * object's present member variable to reflect current status.
455 **/
456 void updatePresence();
457
458 /**
B. J. Wyman681b2a32021-04-20 22:31:22 +0000459 * @brief Updates the power supply presence by reading the GPIO line.
460 */
461 void updatePresenceGPIO();
462
463 /**
Brandon Wymanaed1f752019-11-25 18:10:52 -0600464 * @brief Callback for inventory property changes
465 *
466 * Process change of Present property for power supply.
467 *
B. J. Wyman681b2a32021-04-20 22:31:22 +0000468 * This is used if we are watching the D-Bus properties instead of reading
469 * the GPIO presence line ourselves.
470 *
Brandon Wymanaed1f752019-11-25 18:10:52 -0600471 * @param[in] msg - Data associated with Present change signal
472 **/
473 void inventoryChanged(sdbusplus::message::message& msg);
Brandon Wyman9a507db2021-02-25 16:15:22 -0600474
475 /**
476 * @brief Callback for inventory property added.
477 *
478 * Process add of the interface with the Present property for power supply.
479 *
B. J. Wyman681b2a32021-04-20 22:31:22 +0000480 * This is used if we are watching the D-Bus properties instead of reading
481 * the GPIO presence line ourselves.
482 *
Brandon Wyman9a507db2021-02-25 16:15:22 -0600483 * @param[in] msg - Data associated with Present add signal
484 **/
485 void inventoryAdded(sdbusplus::message::message& msg);
Brandon Wymana0f33ce2019-10-17 18:32:29 -0500486};
487
488} // namespace phosphor::power::psu