Brandon Wyman | a0f33ce | 2019-10-17 18:32:29 -0500 | [diff] [blame] | 1 | #pragma once |
| 2 | |
Brandon Wyman | 8d19577 | 2020-01-27 15:03:51 -0600 | [diff] [blame] | 3 | #include "pmbus.hpp" |
Brandon Wyman | aed1f75 | 2019-11-25 18:10:52 -0600 | [diff] [blame] | 4 | #include "types.hpp" |
Brandon Wyman | 3f1242f | 2020-01-28 13:11:25 -0600 | [diff] [blame] | 5 | #include "utility.hpp" |
Brandon Wyman | aed1f75 | 2019-11-25 18:10:52 -0600 | [diff] [blame] | 6 | |
| 7 | #include <sdbusplus/bus/match.hpp> |
| 8 | |
Brandon Wyman | 1d7a7df | 2020-03-26 10:14:05 -0500 | [diff] [blame] | 9 | #include <stdexcept> |
| 10 | |
Brandon Wyman | a0f33ce | 2019-10-17 18:32:29 -0500 | [diff] [blame] | 11 | namespace phosphor::power::psu |
| 12 | { |
Brandon Wyman | 3f1242f | 2020-01-28 13:11:25 -0600 | [diff] [blame] | 13 | |
Brandon Wyman | 1d7a7df | 2020-03-26 10:14:05 -0500 | [diff] [blame] | 14 | #ifdef IBM_VPD |
| 15 | // PMBus device driver "file name" to read for CCIN value. |
| 16 | constexpr auto CCIN = "ccin"; |
| 17 | constexpr auto PART_NUMBER = "part_number"; |
| 18 | constexpr auto FRU_NUMBER = "fru"; |
| 19 | constexpr auto SERIAL_HEADER = "header"; |
| 20 | constexpr auto SERIAL_NUMBER = "serial_number"; |
| 21 | constexpr auto FW_VERSION = "fw_version"; |
| 22 | |
| 23 | // The D-Bus property name to update with the CCIN value. |
| 24 | constexpr auto MODEL_PROP = "Model"; |
| 25 | constexpr auto PN_PROP = "PartNumber"; |
| 26 | constexpr auto SN_PROP = "SerialNumber"; |
| 27 | constexpr auto VERSION_PROP = "Version"; |
| 28 | |
| 29 | // ipzVPD Keyword sizes |
| 30 | static constexpr auto FL_KW_SIZE = 20; |
| 31 | #endif |
| 32 | |
Brandon Wyman | a0f33ce | 2019-10-17 18:32:29 -0500 | [diff] [blame] | 33 | /** |
| 34 | * @class PowerSupply |
| 35 | * Represents a PMBus power supply device. |
| 36 | */ |
| 37 | class PowerSupply |
| 38 | { |
| 39 | public: |
Brandon Wyman | aed1f75 | 2019-11-25 18:10:52 -0600 | [diff] [blame] | 40 | PowerSupply() = delete; |
Brandon Wyman | a0f33ce | 2019-10-17 18:32:29 -0500 | [diff] [blame] | 41 | PowerSupply(const PowerSupply&) = delete; |
| 42 | PowerSupply(PowerSupply&&) = delete; |
| 43 | PowerSupply& operator=(const PowerSupply&) = delete; |
| 44 | PowerSupply& operator=(PowerSupply&&) = delete; |
| 45 | ~PowerSupply() = default; |
| 46 | |
| 47 | /** |
Brandon Wyman | c63941c | 2020-01-27 16:49:33 -0600 | [diff] [blame] | 48 | * @param[in] invpath - String for inventory path to use |
| 49 | * @param[in] i2cbus - The bus number this power supply is on |
| 50 | * @param[in] i2caddr - The 16-bit I2C address of the power supply |
Brandon Wyman | aed1f75 | 2019-11-25 18:10:52 -0600 | [diff] [blame] | 51 | */ |
Brandon Wyman | c63941c | 2020-01-27 16:49:33 -0600 | [diff] [blame] | 52 | PowerSupply(sdbusplus::bus::bus& bus, const std::string& invpath, |
| 53 | std::uint8_t i2cbus, const std::string& i2caddr) : |
| 54 | bus(bus), |
Brandon Wyman | 8d19577 | 2020-01-27 15:03:51 -0600 | [diff] [blame] | 55 | inventoryPath(invpath), |
| 56 | pmbusIntf(phosphor::pmbus::createPMBus(i2cbus, i2caddr)) |
Brandon Wyman | aed1f75 | 2019-11-25 18:10:52 -0600 | [diff] [blame] | 57 | { |
Brandon Wyman | 1d7a7df | 2020-03-26 10:14:05 -0500 | [diff] [blame] | 58 | if (inventoryPath.empty()) |
| 59 | { |
| 60 | throw std::invalid_argument{"Invalid empty inventoryPath"}; |
| 61 | } |
| 62 | |
Brandon Wyman | aed1f75 | 2019-11-25 18:10:52 -0600 | [diff] [blame] | 63 | // Setup the functions to call when the D-Bus inventory path for the |
| 64 | // Present property changes. |
| 65 | presentMatch = std::make_unique<sdbusplus::bus::match_t>( |
| 66 | bus, |
| 67 | sdbusplus::bus::match::rules::propertiesChanged(inventoryPath, |
| 68 | INVENTORY_IFACE), |
| 69 | [this](auto& msg) { this->inventoryChanged(msg); }); |
| 70 | presentAddedMatch = std::make_unique<sdbusplus::bus::match_t>( |
| 71 | bus, |
| 72 | sdbusplus::bus::match::rules::interfacesAdded() + |
| 73 | sdbusplus::bus::match::rules::path_namespace(inventoryPath), |
| 74 | [this](auto& msg) { this->inventoryChanged(msg); }); |
| 75 | // Get the current state of the Present property. |
| 76 | updatePresence(); |
| 77 | } |
| 78 | |
Brandon Wyman | 3f1242f | 2020-01-28 13:11:25 -0600 | [diff] [blame] | 79 | phosphor::pmbus::PMBusBase& getPMBus() |
| 80 | { |
| 81 | return *pmbusIntf; |
| 82 | } |
| 83 | |
Brandon Wyman | aed1f75 | 2019-11-25 18:10:52 -0600 | [diff] [blame] | 84 | /** |
Brandon Wyman | a0f33ce | 2019-10-17 18:32:29 -0500 | [diff] [blame] | 85 | * Power supply specific function to analyze for faults/errors. |
| 86 | * |
| 87 | * Various PMBus status bits will be checked for fault conditions. |
| 88 | * If a certain fault bits are on, the appropriate error will be |
| 89 | * committed. |
| 90 | */ |
Brandon Wyman | 3f1242f | 2020-01-28 13:11:25 -0600 | [diff] [blame] | 91 | void analyze(); |
Brandon Wyman | a0f33ce | 2019-10-17 18:32:29 -0500 | [diff] [blame] | 92 | |
| 93 | /** |
Brandon Wyman | 59a3579 | 2020-06-04 12:37:40 -0500 | [diff] [blame] | 94 | * Write PMBus ON_OFF_CONFIG |
| 95 | * |
| 96 | * This function will be called to cause the PMBus device driver to send the |
| 97 | * ON_OFF_CONFIG command. Takes one byte of data. |
| 98 | * |
| 99 | * @param[in] data - The ON_OFF_CONFIG data byte mask. |
| 100 | */ |
| 101 | void onOffConfig(uint8_t data); |
| 102 | |
| 103 | /** |
Brandon Wyman | a0f33ce | 2019-10-17 18:32:29 -0500 | [diff] [blame] | 104 | * Write PMBus CLEAR_FAULTS |
| 105 | * |
| 106 | * This function will be called in various situations in order to clear |
| 107 | * any fault status bits that may have been set, in order to start over |
| 108 | * with a clean state. Presence changes and power state changes will |
| 109 | * want to clear any faults logged. |
| 110 | */ |
Brandon Wyman | 3c20846 | 2020-05-13 16:25:58 -0500 | [diff] [blame] | 111 | void clearFaults(); |
Brandon Wyman | a0f33ce | 2019-10-17 18:32:29 -0500 | [diff] [blame] | 112 | |
| 113 | /** |
| 114 | * @brief Adds properties to the inventory. |
| 115 | * |
| 116 | * Reads the values from the device and writes them to the |
| 117 | * associated power supply D-Bus inventory object. |
| 118 | * |
| 119 | * This needs to be done on startup, and each time the presence |
| 120 | * state changes. |
| 121 | * |
| 122 | * Properties added: |
| 123 | * - Serial Number |
| 124 | * - Part Number |
| 125 | * - CCIN (Customer Card Identification Number) - added as the Model |
| 126 | * - Firmware version |
| 127 | */ |
Brandon Wyman | 1d7a7df | 2020-03-26 10:14:05 -0500 | [diff] [blame] | 128 | void updateInventory(); |
Brandon Wyman | a0f33ce | 2019-10-17 18:32:29 -0500 | [diff] [blame] | 129 | |
Brandon Wyman | aed1f75 | 2019-11-25 18:10:52 -0600 | [diff] [blame] | 130 | /** |
| 131 | * @brief Accessor function to indicate present status |
| 132 | */ |
| 133 | bool isPresent() const |
| 134 | { |
| 135 | return present; |
| 136 | } |
| 137 | |
Brandon Wyman | 3f1242f | 2020-01-28 13:11:25 -0600 | [diff] [blame] | 138 | /** |
Brandon Wyman | fed0ba2 | 2020-09-26 20:02:51 -0500 | [diff] [blame] | 139 | * @brief Returns the last read value from STATUS_WORD. |
| 140 | */ |
| 141 | uint64_t getStatusWord() const |
| 142 | { |
| 143 | return statusWord; |
| 144 | } |
| 145 | |
| 146 | /** |
Brandon Wyman | 3f1242f | 2020-01-28 13:11:25 -0600 | [diff] [blame] | 147 | * @brief Returns true if a fault was found. |
| 148 | */ |
| 149 | bool isFaulted() const |
| 150 | { |
| 151 | return faultFound; |
| 152 | } |
| 153 | |
| 154 | /** |
Brandon Wyman | b76ab24 | 2020-09-16 18:06:06 -0500 | [diff] [blame] | 155 | * @brief Return whether a fault has been logged for this power supply |
| 156 | */ |
| 157 | bool isFaultLogged() const |
| 158 | { |
| 159 | return faultLogged; |
| 160 | } |
| 161 | |
| 162 | /** |
| 163 | * @brief Called when a fault for this power supply has been logged. |
| 164 | */ |
| 165 | void setFaultLogged() |
| 166 | { |
| 167 | faultLogged = true; |
| 168 | } |
| 169 | |
| 170 | /** |
Brandon Wyman | 3f1242f | 2020-01-28 13:11:25 -0600 | [diff] [blame] | 171 | * @brief Returns true if INPUT fault occurred. |
| 172 | */ |
| 173 | bool hasInputFault() const |
| 174 | { |
| 175 | return inputFault; |
| 176 | } |
| 177 | |
| 178 | /** |
| 179 | * @brief Returns true if MFRSPECIFIC occurred. |
| 180 | */ |
| 181 | bool hasMFRFault() const |
| 182 | { |
| 183 | return mfrFault; |
| 184 | } |
| 185 | |
| 186 | /** |
| 187 | * @brief Returns true if VIN_UV_FAULT occurred. |
| 188 | */ |
| 189 | bool hasVINUVFault() const |
| 190 | { |
| 191 | return vinUVFault; |
| 192 | } |
| 193 | |
Brandon Wyman | 7e49527 | 2020-09-26 19:57:46 -0500 | [diff] [blame] | 194 | const std::string& getInventoryPath() const |
| 195 | { |
| 196 | return inventoryPath; |
| 197 | } |
| 198 | |
Brandon Wyman | a0f33ce | 2019-10-17 18:32:29 -0500 | [diff] [blame] | 199 | private: |
Brandon Wyman | aed1f75 | 2019-11-25 18:10:52 -0600 | [diff] [blame] | 200 | /** @brief systemd bus member */ |
| 201 | sdbusplus::bus::bus& bus; |
| 202 | |
Brandon Wyman | fed0ba2 | 2020-09-26 20:02:51 -0500 | [diff] [blame] | 203 | /** @brief Will be updated to the latest/lastvalue read from STATUS_WORD. */ |
| 204 | uint64_t statusWord = 0; |
| 205 | |
Brandon Wyman | a0f33ce | 2019-10-17 18:32:29 -0500 | [diff] [blame] | 206 | /** @brief True if a fault has already been found and not cleared */ |
| 207 | bool faultFound = false; |
Brandon Wyman | aed1f75 | 2019-11-25 18:10:52 -0600 | [diff] [blame] | 208 | |
Brandon Wyman | b76ab24 | 2020-09-16 18:06:06 -0500 | [diff] [blame] | 209 | /** @brief True if an error for a fault has already been logged. */ |
| 210 | bool faultLogged = false; |
| 211 | |
Brandon Wyman | 3f1242f | 2020-01-28 13:11:25 -0600 | [diff] [blame] | 212 | /** @brief True if bit 5 of STATUS_WORD high byte is on. */ |
| 213 | bool inputFault = false; |
| 214 | |
| 215 | /** @brief True if bit 4 of STATUS_WORD high byte is on. */ |
| 216 | bool mfrFault = false; |
| 217 | |
| 218 | /** @brief True if bit 3 of STATUS_WORD low byte is on. */ |
| 219 | bool vinUVFault = false; |
| 220 | |
Brandon Wyman | aed1f75 | 2019-11-25 18:10:52 -0600 | [diff] [blame] | 221 | /** |
| 222 | * @brief D-Bus path to use for this power supply's inventory status. |
| 223 | **/ |
| 224 | std::string inventoryPath; |
| 225 | |
| 226 | /** @brief True if the power supply is present. */ |
| 227 | bool present = false; |
| 228 | |
| 229 | /** @brief D-Bus match variable used to subscribe to Present property |
| 230 | * changes. |
| 231 | **/ |
| 232 | std::unique_ptr<sdbusplus::bus::match_t> presentMatch; |
| 233 | |
| 234 | /** @brief D-Bus match variable used to subscribe for Present property |
| 235 | * interface added. |
| 236 | */ |
| 237 | std::unique_ptr<sdbusplus::bus::match_t> presentAddedMatch; |
| 238 | |
| 239 | /** |
Brandon Wyman | 8d19577 | 2020-01-27 15:03:51 -0600 | [diff] [blame] | 240 | * @brief Pointer to the PMBus interface |
| 241 | * |
| 242 | * Used to read or write to/from PMBus power supply devices. |
| 243 | */ |
| 244 | std::unique_ptr<phosphor::pmbus::PMBusBase> pmbusIntf; |
| 245 | |
| 246 | /** |
Brandon Wyman | aed1f75 | 2019-11-25 18:10:52 -0600 | [diff] [blame] | 247 | * @brief Updates the presence status by querying D-Bus |
| 248 | * |
| 249 | * The D-Bus inventory properties for this power supply will be read to |
| 250 | * determine if the power supply is present or not and update this |
| 251 | * object's present member variable to reflect current status. |
| 252 | **/ |
| 253 | void updatePresence(); |
| 254 | |
| 255 | /** |
| 256 | * @brief Callback for inventory property changes |
| 257 | * |
| 258 | * Process change of Present property for power supply. |
| 259 | * |
| 260 | * @param[in] msg - Data associated with Present change signal |
| 261 | **/ |
| 262 | void inventoryChanged(sdbusplus::message::message& msg); |
Brandon Wyman | a0f33ce | 2019-10-17 18:32:29 -0500 | [diff] [blame] | 263 | }; |
| 264 | |
| 265 | } // namespace phosphor::power::psu |