blob: 875e714d2750fa0f830f3147cfb63cc98d8cdd3b [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"
Brandon Wyman3f1242f2020-01-28 13:11:25 -06005#include "utility.hpp"
Brandon Wymanaed1f752019-11-25 18:10:52 -06006
7#include <sdbusplus/bus/match.hpp>
8
Brandon Wyman1d7a7df2020-03-26 10:14:05 -05009#include <stdexcept>
10
Brandon Wymana0f33ce2019-10-17 18:32:29 -050011namespace phosphor::power::psu
12{
Brandon Wyman3f1242f2020-01-28 13:11:25 -060013
Brandon Wyman1d7a7df2020-03-26 10:14:05 -050014#ifdef IBM_VPD
15// PMBus device driver "file name" to read for CCIN value.
16constexpr auto CCIN = "ccin";
17constexpr auto PART_NUMBER = "part_number";
18constexpr auto FRU_NUMBER = "fru";
19constexpr auto SERIAL_HEADER = "header";
20constexpr auto SERIAL_NUMBER = "serial_number";
21constexpr auto FW_VERSION = "fw_version";
22
23// The D-Bus property name to update with the CCIN value.
24constexpr auto MODEL_PROP = "Model";
25constexpr auto PN_PROP = "PartNumber";
26constexpr auto SN_PROP = "SerialNumber";
27constexpr auto VERSION_PROP = "Version";
28
29// ipzVPD Keyword sizes
30static constexpr auto FL_KW_SIZE = 20;
31#endif
32
Brandon Wymanf65c4062020-08-19 13:15:53 -050033constexpr auto LOG_LIMIT = 3;
34
Brandon Wymana0f33ce2019-10-17 18:32:29 -050035/**
36 * @class PowerSupply
37 * Represents a PMBus power supply device.
38 */
39class PowerSupply
40{
41 public:
Brandon Wymanaed1f752019-11-25 18:10:52 -060042 PowerSupply() = delete;
Brandon Wymana0f33ce2019-10-17 18:32:29 -050043 PowerSupply(const PowerSupply&) = delete;
44 PowerSupply(PowerSupply&&) = delete;
45 PowerSupply& operator=(const PowerSupply&) = delete;
46 PowerSupply& operator=(PowerSupply&&) = delete;
47 ~PowerSupply() = default;
48
49 /**
Brandon Wymanc63941c2020-01-27 16:49:33 -060050 * @param[in] invpath - String for inventory path to use
51 * @param[in] i2cbus - The bus number this power supply is on
52 * @param[in] i2caddr - The 16-bit I2C address of the power supply
Brandon Wymanaed1f752019-11-25 18:10:52 -060053 */
Brandon Wymanc63941c2020-01-27 16:49:33 -060054 PowerSupply(sdbusplus::bus::bus& bus, const std::string& invpath,
55 std::uint8_t i2cbus, const std::string& i2caddr) :
56 bus(bus),
Brandon Wyman8d195772020-01-27 15:03:51 -060057 inventoryPath(invpath),
58 pmbusIntf(phosphor::pmbus::createPMBus(i2cbus, i2caddr))
Brandon Wymanaed1f752019-11-25 18:10:52 -060059 {
Brandon Wyman1d7a7df2020-03-26 10:14:05 -050060 if (inventoryPath.empty())
61 {
62 throw std::invalid_argument{"Invalid empty inventoryPath"};
63 }
64
Brandon Wymanaed1f752019-11-25 18:10:52 -060065 // Setup the functions to call when the D-Bus inventory path for the
66 // Present property changes.
67 presentMatch = std::make_unique<sdbusplus::bus::match_t>(
68 bus,
69 sdbusplus::bus::match::rules::propertiesChanged(inventoryPath,
70 INVENTORY_IFACE),
71 [this](auto& msg) { this->inventoryChanged(msg); });
72 presentAddedMatch = std::make_unique<sdbusplus::bus::match_t>(
73 bus,
74 sdbusplus::bus::match::rules::interfacesAdded() +
75 sdbusplus::bus::match::rules::path_namespace(inventoryPath),
76 [this](auto& msg) { this->inventoryChanged(msg); });
77 // Get the current state of the Present property.
78 updatePresence();
79 }
80
Brandon Wyman3f1242f2020-01-28 13:11:25 -060081 phosphor::pmbus::PMBusBase& getPMBus()
82 {
83 return *pmbusIntf;
84 }
85
Brandon Wymanaed1f752019-11-25 18:10:52 -060086 /**
Brandon Wymana0f33ce2019-10-17 18:32:29 -050087 * Power supply specific function to analyze for faults/errors.
88 *
89 * Various PMBus status bits will be checked for fault conditions.
90 * If a certain fault bits are on, the appropriate error will be
91 * committed.
92 */
Brandon Wyman3f1242f2020-01-28 13:11:25 -060093 void analyze();
Brandon Wymana0f33ce2019-10-17 18:32:29 -050094
95 /**
Brandon Wyman59a35792020-06-04 12:37:40 -050096 * Write PMBus ON_OFF_CONFIG
97 *
98 * This function will be called to cause the PMBus device driver to send the
99 * ON_OFF_CONFIG command. Takes one byte of data.
100 *
101 * @param[in] data - The ON_OFF_CONFIG data byte mask.
102 */
103 void onOffConfig(uint8_t data);
104
105 /**
Brandon Wymana0f33ce2019-10-17 18:32:29 -0500106 * Write PMBus CLEAR_FAULTS
107 *
108 * This function will be called in various situations in order to clear
109 * any fault status bits that may have been set, in order to start over
110 * with a clean state. Presence changes and power state changes will
111 * want to clear any faults logged.
112 */
Brandon Wyman3c208462020-05-13 16:25:58 -0500113 void clearFaults();
Brandon Wymana0f33ce2019-10-17 18:32:29 -0500114
115 /**
116 * @brief Adds properties to the inventory.
117 *
118 * Reads the values from the device and writes them to the
119 * associated power supply D-Bus inventory object.
120 *
121 * This needs to be done on startup, and each time the presence
122 * state changes.
123 *
124 * Properties added:
125 * - Serial Number
126 * - Part Number
127 * - CCIN (Customer Card Identification Number) - added as the Model
128 * - Firmware version
129 */
Brandon Wyman1d7a7df2020-03-26 10:14:05 -0500130 void updateInventory();
Brandon Wymana0f33ce2019-10-17 18:32:29 -0500131
Brandon Wymanaed1f752019-11-25 18:10:52 -0600132 /**
133 * @brief Accessor function to indicate present status
134 */
135 bool isPresent() const
136 {
137 return present;
138 }
139
Brandon Wyman3f1242f2020-01-28 13:11:25 -0600140 /**
Brandon Wymanfed0ba22020-09-26 20:02:51 -0500141 * @brief Returns the last read value from STATUS_WORD.
142 */
143 uint64_t getStatusWord() const
144 {
145 return statusWord;
146 }
147
148 /**
Brandon Wyman3f1242f2020-01-28 13:11:25 -0600149 * @brief Returns true if a fault was found.
150 */
151 bool isFaulted() const
152 {
Brandon Wyman4176d6b2020-10-07 17:41:06 -0500153 return (faultFound || hasCommFault());
Brandon Wyman3f1242f2020-01-28 13:11:25 -0600154 }
155
156 /**
Brandon Wymanb76ab242020-09-16 18:06:06 -0500157 * @brief Return whether a fault has been logged for this power supply
158 */
159 bool isFaultLogged() const
160 {
161 return faultLogged;
162 }
163
164 /**
165 * @brief Called when a fault for this power supply has been logged.
166 */
167 void setFaultLogged()
168 {
169 faultLogged = true;
170 }
171
172 /**
Brandon Wyman3f1242f2020-01-28 13:11:25 -0600173 * @brief Returns true if INPUT fault occurred.
174 */
175 bool hasInputFault() const
176 {
177 return inputFault;
178 }
179
180 /**
181 * @brief Returns true if MFRSPECIFIC occurred.
182 */
183 bool hasMFRFault() const
184 {
185 return mfrFault;
186 }
187
188 /**
189 * @brief Returns true if VIN_UV_FAULT occurred.
190 */
191 bool hasVINUVFault() const
192 {
193 return vinUVFault;
194 }
195
Brandon Wyman4176d6b2020-10-07 17:41:06 -0500196 const std::string getDevicePath() const
197 {
198 return pmbusIntf->path();
199 }
200
Brandon Wyman7e495272020-09-26 19:57:46 -0500201 const std::string& getInventoryPath() const
202 {
203 return inventoryPath;
204 }
205
Brandon Wymanf65c4062020-08-19 13:15:53 -0500206 /** @brief Returns true if the number of failed reads exceeds limit
207 * TODO: or CML bit on.
208 */
209 bool hasCommFault() const
210 {
211 return readFail >= LOG_LIMIT;
212 }
213
Brandon Wymana0f33ce2019-10-17 18:32:29 -0500214 private:
Brandon Wymanaed1f752019-11-25 18:10:52 -0600215 /** @brief systemd bus member */
216 sdbusplus::bus::bus& bus;
217
Brandon Wymanfed0ba22020-09-26 20:02:51 -0500218 /** @brief Will be updated to the latest/lastvalue read from STATUS_WORD. */
219 uint64_t statusWord = 0;
220
Brandon Wymana0f33ce2019-10-17 18:32:29 -0500221 /** @brief True if a fault has already been found and not cleared */
222 bool faultFound = false;
Brandon Wymanaed1f752019-11-25 18:10:52 -0600223
Brandon Wymanb76ab242020-09-16 18:06:06 -0500224 /** @brief True if an error for a fault has already been logged. */
225 bool faultLogged = false;
226
Brandon Wyman3f1242f2020-01-28 13:11:25 -0600227 /** @brief True if bit 5 of STATUS_WORD high byte is on. */
228 bool inputFault = false;
229
230 /** @brief True if bit 4 of STATUS_WORD high byte is on. */
231 bool mfrFault = false;
232
233 /** @brief True if bit 3 of STATUS_WORD low byte is on. */
234 bool vinUVFault = false;
235
Brandon Wymanf65c4062020-08-19 13:15:53 -0500236 /** @brief Count of the number of read failures. */
237 size_t readFail = 0;
238
Brandon Wymanaed1f752019-11-25 18:10:52 -0600239 /**
240 * @brief D-Bus path to use for this power supply's inventory status.
241 **/
242 std::string inventoryPath;
243
244 /** @brief True if the power supply is present. */
245 bool present = false;
246
247 /** @brief D-Bus match variable used to subscribe to Present property
248 * changes.
249 **/
250 std::unique_ptr<sdbusplus::bus::match_t> presentMatch;
251
252 /** @brief D-Bus match variable used to subscribe for Present property
253 * interface added.
254 */
255 std::unique_ptr<sdbusplus::bus::match_t> presentAddedMatch;
256
257 /**
Brandon Wyman8d195772020-01-27 15:03:51 -0600258 * @brief Pointer to the PMBus interface
259 *
260 * Used to read or write to/from PMBus power supply devices.
261 */
262 std::unique_ptr<phosphor::pmbus::PMBusBase> pmbusIntf;
263
264 /**
Brandon Wymanaed1f752019-11-25 18:10:52 -0600265 * @brief Updates the presence status by querying D-Bus
266 *
267 * The D-Bus inventory properties for this power supply will be read to
268 * determine if the power supply is present or not and update this
269 * object's present member variable to reflect current status.
270 **/
271 void updatePresence();
272
273 /**
274 * @brief Callback for inventory property changes
275 *
276 * Process change of Present property for power supply.
277 *
278 * @param[in] msg - Data associated with Present change signal
279 **/
280 void inventoryChanged(sdbusplus::message::message& msg);
Brandon Wymana0f33ce2019-10-17 18:32:29 -0500281};
282
283} // namespace phosphor::power::psu