blob: f67d2a170b85c1536f6940027559cfba13d643b6 [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
B. J. Wyman681b2a32021-04-20 22:31:22 +000068 GPIOInterface* getPresenceGPIO()
69 {
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 /**
Jay Meyer10d94052020-11-30 14:41:21 -0600148 * @brief Returns the last read value from STATUS_MFR.
149 */
150 uint64_t getMFRFault() const
151 {
152 return statusMFR;
153 }
154
155 /**
Brandon Wyman3f1242f2020-01-28 13:11:25 -0600156 * @brief Returns true if a fault was found.
157 */
158 bool isFaulted() const
159 {
Brandon Wyman4176d6b2020-10-07 17:41:06 -0500160 return (faultFound || hasCommFault());
Brandon Wyman3f1242f2020-01-28 13:11:25 -0600161 }
162
163 /**
Brandon Wymanb76ab242020-09-16 18:06:06 -0500164 * @brief Return whether a fault has been logged for this power supply
165 */
166 bool isFaultLogged() const
167 {
168 return faultLogged;
169 }
170
171 /**
172 * @brief Called when a fault for this power supply has been logged.
173 */
174 void setFaultLogged()
175 {
176 faultLogged = true;
177 }
178
179 /**
Brandon Wyman3f1242f2020-01-28 13:11:25 -0600180 * @brief Returns true if INPUT fault occurred.
181 */
182 bool hasInputFault() const
183 {
184 return inputFault;
185 }
186
187 /**
188 * @brief Returns true if MFRSPECIFIC occurred.
189 */
190 bool hasMFRFault() const
191 {
192 return mfrFault;
193 }
194
195 /**
196 * @brief Returns true if VIN_UV_FAULT occurred.
197 */
198 bool hasVINUVFault() const
199 {
200 return vinUVFault;
201 }
202
Brandon Wymanc9efe412020-10-09 15:42:50 -0500203 /**
204 * @brief Returns the device path
205 *
206 * This can be used for error call outs.
207 * Example: /sys/bus/i2c/devices/3-0068
208 */
Brandon Wyman4176d6b2020-10-07 17:41:06 -0500209 const std::string getDevicePath() const
210 {
211 return pmbusIntf->path();
212 }
213
Brandon Wymanc9efe412020-10-09 15:42:50 -0500214 /**
215 * @brief Returns this power supplies inventory path.
216 *
217 * This can be used for error call outs.
218 * Example:
219 * /xyz/openbmc_project/inventory/system/chassis/motherboard/powersupply1
220 */
Brandon Wyman7e495272020-09-26 19:57:46 -0500221 const std::string& getInventoryPath() const
222 {
223 return inventoryPath;
224 }
225
Brandon Wymanc9efe412020-10-09 15:42:50 -0500226 /**
227 * @brief Returns the firmware revision version read from the power supply
228 */
229 const std::string& getFWVersion() const
230 {
231 return fwVersion;
232 }
233
Adriana Kobylak572a9052021-03-30 15:58:07 +0000234 /**
235 * @brief Returns the model name of the power supply
236 */
237 const std::string& getModelName() const
238 {
239 return modelName;
240 }
241
Brandon Wymanf65c4062020-08-19 13:15:53 -0500242 /** @brief Returns true if the number of failed reads exceeds limit
243 * TODO: or CML bit on.
244 */
245 bool hasCommFault() const
246 {
247 return readFail >= LOG_LIMIT;
248 }
249
Adriana Kobylak4175ffb2021-08-02 14:51:05 +0000250 /**
251 * @brief Reads the pmbus input voltage and returns that actual voltage
252 * reading and the calculated input voltage based on thresholds.
253 * @param[out] actualInputVoltage - The actual voltage reading, in Volts.
254 * @param[out] inputVoltage - A rounded up/down value of the actual input
255 * voltage based on thresholds, in Volts.
256 */
257 void getInputVoltage(double& actualInputVoltage, int& inputVoltage) const;
258
Brandon Wymana0f33ce2019-10-17 18:32:29 -0500259 private:
Brandon Wymanaed1f752019-11-25 18:10:52 -0600260 /** @brief systemd bus member */
261 sdbusplus::bus::bus& bus;
262
Brandon Wyman9564e942020-11-10 14:01:42 -0600263 /** @brief Will be updated to the latest/lastvalue read from STATUS_WORD.*/
Brandon Wymanfed0ba22020-09-26 20:02:51 -0500264 uint64_t statusWord = 0;
265
Jay Meyer10d94052020-11-30 14:41:21 -0600266 /** @brief Will be updated to the latest/lastvalue read from STATUS_MFR.*/
267 uint64_t statusMFR = 0;
268
Brandon Wymana0f33ce2019-10-17 18:32:29 -0500269 /** @brief True if a fault has already been found and not cleared */
270 bool faultFound = false;
Brandon Wymanaed1f752019-11-25 18:10:52 -0600271
Brandon Wymanb76ab242020-09-16 18:06:06 -0500272 /** @brief True if an error for a fault has already been logged. */
273 bool faultLogged = false;
274
Brandon Wyman3f1242f2020-01-28 13:11:25 -0600275 /** @brief True if bit 5 of STATUS_WORD high byte is on. */
276 bool inputFault = false;
277
278 /** @brief True if bit 4 of STATUS_WORD high byte is on. */
279 bool mfrFault = false;
280
281 /** @brief True if bit 3 of STATUS_WORD low byte is on. */
282 bool vinUVFault = false;
283
Brandon Wymanf65c4062020-08-19 13:15:53 -0500284 /** @brief Count of the number of read failures. */
285 size_t readFail = 0;
286
Brandon Wymanaed1f752019-11-25 18:10:52 -0600287 /**
288 * @brief D-Bus path to use for this power supply's inventory status.
289 **/
290 std::string inventoryPath;
291
B. J. Wyman681b2a32021-04-20 22:31:22 +0000292 /**
293 * @brief The libgpiod object for monitoring PSU presence
294 */
295 std::unique_ptr<GPIOInterface> presenceGPIO = nullptr;
296
Brandon Wymanaed1f752019-11-25 18:10:52 -0600297 /** @brief True if the power supply is present. */
298 bool present = false;
299
Adriana Kobylak572a9052021-03-30 15:58:07 +0000300 /** @brief Power supply model name. */
301 std::string modelName;
302
Brandon Wymanaed1f752019-11-25 18:10:52 -0600303 /** @brief D-Bus match variable used to subscribe to Present property
304 * changes.
305 **/
306 std::unique_ptr<sdbusplus::bus::match_t> presentMatch;
307
308 /** @brief D-Bus match variable used to subscribe for Present property
309 * interface added.
310 */
311 std::unique_ptr<sdbusplus::bus::match_t> presentAddedMatch;
312
313 /**
Brandon Wyman8d195772020-01-27 15:03:51 -0600314 * @brief Pointer to the PMBus interface
315 *
316 * Used to read or write to/from PMBus power supply devices.
317 */
Brandon Wyman9564e942020-11-10 14:01:42 -0600318 std::unique_ptr<phosphor::pmbus::PMBusBase> pmbusIntf = nullptr;
Brandon Wyman8d195772020-01-27 15:03:51 -0600319
Brandon Wymanc9efe412020-10-09 15:42:50 -0500320 /** @brief Stored copy of the firmware version/revision string */
321 std::string fwVersion;
322
Brandon Wyman8d195772020-01-27 15:03:51 -0600323 /**
B. J. Wyman681b2a32021-04-20 22:31:22 +0000324 * @brief The file system path used for binding the device driver.
325 */
326 const std::filesystem::path bindPath;
327
328 /* @brief The string to pass in for binding the device driver. */
329 std::string bindDevice;
330
331 /**
332 * @brief Binds or unbinds the power supply device driver
333 *
334 * Called when a presence change is detected to either bind the device
335 * driver for the power supply when it is installed, or unbind the device
336 * driver when the power supply is removed.
337 *
338 * Writes <device> to <path>/bind (or unbind)
339 *
340 * @param present - when true, will bind the device driver
341 * when false, will unbind the device driver
342 */
343 void bindOrUnbindDriver(bool present);
344
345 /**
Brandon Wymanaed1f752019-11-25 18:10:52 -0600346 * @brief Updates the presence status by querying D-Bus
347 *
348 * The D-Bus inventory properties for this power supply will be read to
349 * determine if the power supply is present or not and update this
350 * object's present member variable to reflect current status.
351 **/
352 void updatePresence();
353
354 /**
B. J. Wyman681b2a32021-04-20 22:31:22 +0000355 * @brief Updates the power supply presence by reading the GPIO line.
356 */
357 void updatePresenceGPIO();
358
359 /**
Brandon Wymanaed1f752019-11-25 18:10:52 -0600360 * @brief Callback for inventory property changes
361 *
362 * Process change of Present property for power supply.
363 *
B. J. Wyman681b2a32021-04-20 22:31:22 +0000364 * This is used if we are watching the D-Bus properties instead of reading
365 * the GPIO presence line ourselves.
366 *
Brandon Wymanaed1f752019-11-25 18:10:52 -0600367 * @param[in] msg - Data associated with Present change signal
368 **/
369 void inventoryChanged(sdbusplus::message::message& msg);
Brandon Wyman9a507db2021-02-25 16:15:22 -0600370
371 /**
372 * @brief Callback for inventory property added.
373 *
374 * Process add of the interface with the Present property for power supply.
375 *
B. J. Wyman681b2a32021-04-20 22:31:22 +0000376 * This is used if we are watching the D-Bus properties instead of reading
377 * the GPIO presence line ourselves.
378 *
Brandon Wyman9a507db2021-02-25 16:15:22 -0600379 * @param[in] msg - Data associated with Present add signal
380 **/
381 void inventoryAdded(sdbusplus::message::message& msg);
Brandon Wymana0f33ce2019-10-17 18:32:29 -0500382};
383
384} // namespace phosphor::power::psu