| Brandon Wyman | 2bac860 | 2019-09-12 18:12:21 -0500 | [diff] [blame] | 1 | #pragma once | 
|  | 2 |  | 
| Brandon Wyman | a0f33ce | 2019-10-17 18:32:29 -0500 | [diff] [blame] | 3 | #include "power_supply.hpp" | 
|  | 4 | #include "types.hpp" | 
|  | 5 | #include "utility.hpp" | 
|  | 6 |  | 
|  | 7 | #include <phosphor-logging/log.hpp> | 
| Brandon Wyman | 2bac860 | 2019-09-12 18:12:21 -0500 | [diff] [blame] | 8 | #include <sdbusplus/bus/match.hpp> | 
|  | 9 | #include <sdeventplus/event.hpp> | 
|  | 10 | #include <sdeventplus/utility/timer.hpp> | 
|  | 11 |  | 
| Brandon Wyman | aed1f75 | 2019-11-25 18:10:52 -0600 | [diff] [blame] | 12 | struct sys_properties | 
|  | 13 | { | 
| Adriana Kobylak | d3a70d9 | 2021-06-04 16:24:45 +0000 | [diff] [blame] | 14 | int powerSupplyCount; | 
|  | 15 | std::vector<uint64_t> inputVoltage; | 
| Adriana Kobylak | 886574c | 2021-11-01 18:22:28 +0000 | [diff] [blame^] | 16 | bool powerConfigFullLoad; | 
| Brandon Wyman | aed1f75 | 2019-11-25 18:10:52 -0600 | [diff] [blame] | 17 | }; | 
|  | 18 |  | 
| Brandon Wyman | a0f33ce | 2019-10-17 18:32:29 -0500 | [diff] [blame] | 19 | using namespace phosphor::power::psu; | 
|  | 20 | using namespace phosphor::logging; | 
|  | 21 |  | 
| Brandon Wyman | 63ea78b | 2020-09-24 16:49:09 -0500 | [diff] [blame] | 22 | namespace phosphor::power::manager | 
| Brandon Wyman | 2bac860 | 2019-09-12 18:12:21 -0500 | [diff] [blame] | 23 | { | 
|  | 24 |  | 
| Adriana Kobylak | 2aba2b2 | 2021-10-11 16:00:05 +0000 | [diff] [blame] | 25 | // Validation timeout. Allow 10s to detect if new EM interfaces show up in D-Bus | 
|  | 26 | // before performing the validation. | 
|  | 27 | constexpr auto validationTimeout = std::chrono::seconds(10); | 
| Adriana Kobylak | a4d38fa | 2021-10-05 19:57:47 +0000 | [diff] [blame] | 28 |  | 
| Brandon Wyman | 2bac860 | 2019-09-12 18:12:21 -0500 | [diff] [blame] | 29 | /** | 
|  | 30 | * @class PSUManager | 
|  | 31 | * | 
|  | 32 | * This class will create an object used to manage and monitor a list of power | 
|  | 33 | * supply devices. | 
|  | 34 | */ | 
|  | 35 | class PSUManager | 
|  | 36 | { | 
|  | 37 | public: | 
|  | 38 | PSUManager() = delete; | 
|  | 39 | ~PSUManager() = default; | 
|  | 40 | PSUManager(const PSUManager&) = delete; | 
|  | 41 | PSUManager& operator=(const PSUManager&) = delete; | 
|  | 42 | PSUManager(PSUManager&&) = delete; | 
|  | 43 | PSUManager& operator=(PSUManager&&) = delete; | 
|  | 44 |  | 
|  | 45 | /** | 
| Brandon Wyman | 510acaa | 2020-11-05 18:32:04 -0600 | [diff] [blame] | 46 | * Constructor to read configuration from D-Bus. | 
|  | 47 | * | 
|  | 48 | * @param[in] bus - D-Bus bus object | 
|  | 49 | * @param[in] e - event object | 
|  | 50 | */ | 
|  | 51 | PSUManager(sdbusplus::bus::bus& bus, const sdeventplus::Event& e); | 
|  | 52 |  | 
|  | 53 | /** | 
| Brandon Wyman | 510acaa | 2020-11-05 18:32:04 -0600 | [diff] [blame] | 54 | * Get PSU properties from D-Bus, use that to build a power supply | 
|  | 55 | * object. | 
|  | 56 | * | 
|  | 57 | * @param[in] properties - A map of property names and values | 
|  | 58 | * | 
|  | 59 | */ | 
|  | 60 | void getPSUProperties(util::DbusPropertyMap& properties); | 
|  | 61 |  | 
|  | 62 | /** | 
|  | 63 | * Get PSU configuration from D-Bus | 
|  | 64 | */ | 
|  | 65 | void getPSUConfiguration(); | 
|  | 66 |  | 
|  | 67 | /** | 
| Adriana Kobylak | 9bab9e1 | 2021-02-24 15:32:03 -0600 | [diff] [blame] | 68 | * @brief Initialize the system properties from the Supported Configuration | 
|  | 69 | *        D-Bus object provided by Entity Manager. | 
|  | 70 | */ | 
|  | 71 | void getSystemProperties(); | 
|  | 72 |  | 
|  | 73 | /** | 
| Brandon Wyman | 2bac860 | 2019-09-12 18:12:21 -0500 | [diff] [blame] | 74 | * Initializes the manager. | 
|  | 75 | * | 
|  | 76 | * Get current BMC state, ... | 
|  | 77 | */ | 
|  | 78 | void initialize() | 
|  | 79 | { | 
| Brandon Wyman | a0f33ce | 2019-10-17 18:32:29 -0500 | [diff] [blame] | 80 | // When state = 1, system is powered on | 
|  | 81 | int32_t state = 0; | 
|  | 82 |  | 
|  | 83 | try | 
|  | 84 | { | 
|  | 85 | // Use getProperty utility function to get power state. | 
|  | 86 | util::getProperty<int32_t>(POWER_IFACE, "state", POWER_OBJ_PATH, | 
|  | 87 | powerService, bus, state); | 
|  | 88 |  | 
|  | 89 | if (state) | 
|  | 90 | { | 
|  | 91 | powerOn = true; | 
| Adriana Kobylak | a4d38fa | 2021-10-05 19:57:47 +0000 | [diff] [blame] | 92 | validationTimer->restartOnce(validationTimeout); | 
| Brandon Wyman | a0f33ce | 2019-10-17 18:32:29 -0500 | [diff] [blame] | 93 | } | 
|  | 94 | else | 
|  | 95 | { | 
|  | 96 | powerOn = false; | 
| Adriana Kobylak | 8f16fb5 | 2021-03-31 15:50:15 +0000 | [diff] [blame] | 97 | runValidateConfig = true; | 
| Brandon Wyman | a0f33ce | 2019-10-17 18:32:29 -0500 | [diff] [blame] | 98 | } | 
|  | 99 | } | 
| Patrick Williams | c1d4de5 | 2021-10-06 12:45:57 -0500 | [diff] [blame] | 100 | catch (const std::exception& e) | 
| Brandon Wyman | a0f33ce | 2019-10-17 18:32:29 -0500 | [diff] [blame] | 101 | { | 
|  | 102 | log<level::INFO>("Failed to get power state. Assuming it is off."); | 
|  | 103 | powerOn = false; | 
| Adriana Kobylak | 8f16fb5 | 2021-03-31 15:50:15 +0000 | [diff] [blame] | 104 | runValidateConfig = true; | 
| Brandon Wyman | a0f33ce | 2019-10-17 18:32:29 -0500 | [diff] [blame] | 105 | } | 
|  | 106 |  | 
| Brandon Wyman | 59a3579 | 2020-06-04 12:37:40 -0500 | [diff] [blame] | 107 | onOffConfig(phosphor::pmbus::ON_OFF_CONFIG_CONTROL_PIN_ONLY); | 
| Brandon Wyman | a0f33ce | 2019-10-17 18:32:29 -0500 | [diff] [blame] | 108 | clearFaults(); | 
|  | 109 | updateInventory(); | 
| Brandon Wyman | 2bac860 | 2019-09-12 18:12:21 -0500 | [diff] [blame] | 110 | } | 
|  | 111 |  | 
|  | 112 | /** | 
|  | 113 | * Starts the timer to start monitoring the list of devices. | 
|  | 114 | */ | 
|  | 115 | int run() | 
|  | 116 | { | 
| Brandon Wyman | 2fe5186 | 2019-11-25 16:43:21 -0600 | [diff] [blame] | 117 | return timer->get_event().loop(); | 
| Brandon Wyman | 2bac860 | 2019-09-12 18:12:21 -0500 | [diff] [blame] | 118 | } | 
|  | 119 |  | 
|  | 120 | /** | 
| Brandon Wyman | 59a3579 | 2020-06-04 12:37:40 -0500 | [diff] [blame] | 121 | * Write PMBus ON_OFF_CONFIG | 
|  | 122 | * | 
|  | 123 | * This function will be called to cause the PMBus device driver to send the | 
|  | 124 | * ON_OFF_CONFIG command. Takes one byte of data. | 
|  | 125 | */ | 
|  | 126 | void onOffConfig(const uint8_t data) | 
|  | 127 | { | 
|  | 128 | for (auto& psu : psus) | 
|  | 129 | { | 
|  | 130 | psu->onOffConfig(data); | 
|  | 131 | } | 
|  | 132 | } | 
|  | 133 |  | 
|  | 134 | /** | 
| Brandon Wyman | 2bac860 | 2019-09-12 18:12:21 -0500 | [diff] [blame] | 135 | * This function will be called in various situations in order to clear | 
|  | 136 | * any fault status bits that may have been set, in order to start over | 
|  | 137 | * with a clean state. Presence changes and power state changes will want | 
|  | 138 | * to clear any faults logged. | 
|  | 139 | */ | 
|  | 140 | void clearFaults() | 
|  | 141 | { | 
| Brandon Wyman | a0f33ce | 2019-10-17 18:32:29 -0500 | [diff] [blame] | 142 | for (auto& psu : psus) | 
|  | 143 | { | 
| Brandon Wyman | aed1f75 | 2019-11-25 18:10:52 -0600 | [diff] [blame] | 144 | psu->clearFaults(); | 
| Brandon Wyman | a0f33ce | 2019-10-17 18:32:29 -0500 | [diff] [blame] | 145 | } | 
| Brandon Wyman | 2bac860 | 2019-09-12 18:12:21 -0500 | [diff] [blame] | 146 | } | 
|  | 147 |  | 
|  | 148 | private: | 
|  | 149 | /** | 
|  | 150 | * The D-Bus object | 
|  | 151 | */ | 
|  | 152 | sdbusplus::bus::bus& bus; | 
|  | 153 |  | 
|  | 154 | /** | 
|  | 155 | * The timer that runs to periodically check the power supplies. | 
|  | 156 | */ | 
| Brandon Wyman | 2fe5186 | 2019-11-25 16:43:21 -0600 | [diff] [blame] | 157 | std::unique_ptr< | 
|  | 158 | sdeventplus::utility::Timer<sdeventplus::ClockId::Monotonic>> | 
|  | 159 | timer; | 
| Brandon Wyman | 2bac860 | 2019-09-12 18:12:21 -0500 | [diff] [blame] | 160 |  | 
|  | 161 | /** | 
| Adriana Kobylak | a4d38fa | 2021-10-05 19:57:47 +0000 | [diff] [blame] | 162 | * The timer that performs power supply validation as the entity manager | 
|  | 163 | * interfaces show up in d-bus. | 
|  | 164 | */ | 
|  | 165 | std::unique_ptr< | 
|  | 166 | sdeventplus::utility::Timer<sdeventplus::ClockId::Monotonic>> | 
|  | 167 | validationTimer; | 
|  | 168 |  | 
|  | 169 | /** | 
| Brandon Wyman | b76ab24 | 2020-09-16 18:06:06 -0500 | [diff] [blame] | 170 | * Create an error | 
|  | 171 | * | 
|  | 172 | * @param[in] faultName - 'name' message for the BMC error log entry | 
| Brandon Wyman | 8b66288 | 2021-10-08 17:31:51 +0000 | [diff] [blame] | 173 | * @param[in,out] additionalData - The AdditionalData property for the error | 
| Brandon Wyman | b76ab24 | 2020-09-16 18:06:06 -0500 | [diff] [blame] | 174 | */ | 
|  | 175 | void createError(const std::string& faultName, | 
| Brandon Wyman | 8b66288 | 2021-10-08 17:31:51 +0000 | [diff] [blame] | 176 | std::map<std::string, std::string>& additionalData); | 
| Brandon Wyman | b76ab24 | 2020-09-16 18:06:06 -0500 | [diff] [blame] | 177 |  | 
|  | 178 | /** | 
| Brandon Wyman | 2bac860 | 2019-09-12 18:12:21 -0500 | [diff] [blame] | 179 | * Analyze the status of each of the power supplies. | 
| Brandon Wyman | b76ab24 | 2020-09-16 18:06:06 -0500 | [diff] [blame] | 180 | * | 
|  | 181 | * Log errors for faults, when and where appropriate. | 
| Brandon Wyman | 2bac860 | 2019-09-12 18:12:21 -0500 | [diff] [blame] | 182 | */ | 
| Brandon Wyman | 63ea78b | 2020-09-24 16:49:09 -0500 | [diff] [blame] | 183 | void analyze(); | 
| Brandon Wyman | 2bac860 | 2019-09-12 18:12:21 -0500 | [diff] [blame] | 184 |  | 
|  | 185 | /** @brief True if the power is on. */ | 
|  | 186 | bool powerOn = false; | 
|  | 187 |  | 
| Brandon Wyman | a0f33ce | 2019-10-17 18:32:29 -0500 | [diff] [blame] | 188 | /** @brief Used as part of subscribing to power on state changes*/ | 
|  | 189 | std::string powerService; | 
|  | 190 |  | 
| Brandon Wyman | 2bac860 | 2019-09-12 18:12:21 -0500 | [diff] [blame] | 191 | /** @brief Used to subscribe to D-Bus power on state changes */ | 
|  | 192 | std::unique_ptr<sdbusplus::bus::match_t> powerOnMatch; | 
|  | 193 |  | 
| Adriana Kobylak | 9bab9e1 | 2021-02-24 15:32:03 -0600 | [diff] [blame] | 194 | /** @brief Used to subscribe to Entity Manager interfaces added */ | 
|  | 195 | std::unique_ptr<sdbusplus::bus::match_t> entityManagerIfacesAddedMatch; | 
|  | 196 |  | 
| Brandon Wyman | 2bac860 | 2019-09-12 18:12:21 -0500 | [diff] [blame] | 197 | /** | 
| Brandon Wyman | 2bac860 | 2019-09-12 18:12:21 -0500 | [diff] [blame] | 198 | * @brief Callback for power state property changes | 
|  | 199 | * | 
|  | 200 | * Process changes to the powered on state property for the system. | 
|  | 201 | * | 
|  | 202 | * @param[in] msg - Data associated with the power state signal | 
|  | 203 | */ | 
|  | 204 | void powerStateChanged(sdbusplus::message::message& msg); | 
|  | 205 |  | 
|  | 206 | /** | 
| Brandon Wyman | 3e42913 | 2021-03-18 18:03:14 -0500 | [diff] [blame] | 207 | * @brief Callback for entity-manager interface added | 
| Adriana Kobylak | 9bab9e1 | 2021-02-24 15:32:03 -0600 | [diff] [blame] | 208 | * | 
| Brandon Wyman | 3e42913 | 2021-03-18 18:03:14 -0500 | [diff] [blame] | 209 | * Process the information from the supported configuration and or IBM CFFPS | 
|  | 210 | * Connector interface being added. | 
| Adriana Kobylak | 9bab9e1 | 2021-02-24 15:32:03 -0600 | [diff] [blame] | 211 | * | 
|  | 212 | * @param[in] msg - Data associated with the interfaces added signal | 
|  | 213 | */ | 
| Brandon Wyman | 3e42913 | 2021-03-18 18:03:14 -0500 | [diff] [blame] | 214 | void entityManagerIfaceAdded(sdbusplus::message::message& msg); | 
| Adriana Kobylak | 9bab9e1 | 2021-02-24 15:32:03 -0600 | [diff] [blame] | 215 |  | 
|  | 216 | /** | 
| Brandon Wyman | 2bac860 | 2019-09-12 18:12:21 -0500 | [diff] [blame] | 217 | * @brief Adds properties to the inventory. | 
|  | 218 | * | 
|  | 219 | * Reads the values from the devices and writes them to the associated | 
|  | 220 | * power supply D-Bus inventory objects. | 
|  | 221 | * | 
|  | 222 | * This needs to be done on startup, and each time the presence state | 
|  | 223 | * changes. | 
|  | 224 | */ | 
| Brandon Wyman | a0f33ce | 2019-10-17 18:32:29 -0500 | [diff] [blame] | 225 | void updateInventory() | 
|  | 226 | { | 
|  | 227 | for (auto& psu : psus) | 
|  | 228 | { | 
| Brandon Wyman | aed1f75 | 2019-11-25 18:10:52 -0600 | [diff] [blame] | 229 | psu->updateInventory(); | 
| Brandon Wyman | a0f33ce | 2019-10-17 18:32:29 -0500 | [diff] [blame] | 230 | } | 
|  | 231 | } | 
|  | 232 |  | 
|  | 233 | /** | 
| Adriana Kobylak | e1074d8 | 2021-03-16 20:46:44 +0000 | [diff] [blame] | 234 | * @brief Helper function to populate the system properties | 
|  | 235 | * | 
|  | 236 | * @param[in] properties - A map of property names and values | 
|  | 237 | */ | 
|  | 238 | void populateSysProperties(const util::DbusPropertyMap& properties); | 
|  | 239 |  | 
|  | 240 | /** | 
| Adriana Kobylak | 8f16fb5 | 2021-03-31 15:50:15 +0000 | [diff] [blame] | 241 | * @brief Perform power supply configuration validation. | 
|  | 242 | * @details Validates if the existing power supply properties are a | 
|  | 243 | * supported configuration, and acts on its findings such as logging errors. | 
|  | 244 | */ | 
|  | 245 | void validateConfig(); | 
|  | 246 |  | 
|  | 247 | /** | 
|  | 248 | * @brief Flag to indicate if the validateConfig() function should be run. | 
|  | 249 | * Set to false once the configuration has been validated to avoid running | 
|  | 250 | * multiple times due to interfaces added signal. Set to true during power | 
|  | 251 | * off to trigger the validation on power on. | 
|  | 252 | */ | 
|  | 253 | bool runValidateConfig = true; | 
|  | 254 |  | 
|  | 255 | /** | 
| Adriana Kobylak | 4d9aaf9 | 2021-06-30 15:27:42 +0000 | [diff] [blame] | 256 | * @brief Check that all PSUs have the same model name and that the system | 
|  | 257 | * has the required number of PSUs present as specified in the Supported | 
|  | 258 | * Configuration interface. | 
|  | 259 | * | 
|  | 260 | * @param[out] additionalData - Contains debug information on why the check | 
|  | 261 | *             might have failed. Can be used to fill in error logs. | 
|  | 262 | * @return true if all the required PSUs are present, false otherwise. | 
|  | 263 | */ | 
|  | 264 | bool hasRequiredPSUs(std::map<std::string, std::string>& additionalData); | 
|  | 265 |  | 
|  | 266 | /** | 
| Adriana Kobylak | 523704d | 2021-09-21 15:55:41 +0000 | [diff] [blame] | 267 | * @brief Helper function to validate that all PSUs have the same model name | 
|  | 268 | * | 
|  | 269 | * @param[out] model - The model name. Empty if there is a mismatch. | 
|  | 270 | * @param[out] additionalData - If there is a mismatch, it contains debug | 
|  | 271 | *             information such as the mismatched model name. | 
|  | 272 | * @return true if all the PSUs have the same model name, false otherwise. | 
|  | 273 | */ | 
|  | 274 | bool validateModelName(std::string& model, | 
|  | 275 | std::map<std::string, std::string>& additionalData); | 
|  | 276 |  | 
|  | 277 | /** | 
| Adriana Kobylak | 9ea66a6 | 2021-03-24 17:54:14 +0000 | [diff] [blame] | 278 | * @brief Map of supported PSU configurations that include the model name | 
|  | 279 | * and their properties. | 
| Brandon Wyman | aed1f75 | 2019-11-25 18:10:52 -0600 | [diff] [blame] | 280 | */ | 
| Adriana Kobylak | d3a70d9 | 2021-06-04 16:24:45 +0000 | [diff] [blame] | 281 | std::map<std::string, sys_properties> supportedConfigs; | 
| Brandon Wyman | aed1f75 | 2019-11-25 18:10:52 -0600 | [diff] [blame] | 282 |  | 
|  | 283 | /** | 
| Brandon Wyman | a0f33ce | 2019-10-17 18:32:29 -0500 | [diff] [blame] | 284 | * @brief The vector for power supplies. | 
|  | 285 | */ | 
| Brandon Wyman | aed1f75 | 2019-11-25 18:10:52 -0600 | [diff] [blame] | 286 | std::vector<std::unique_ptr<PowerSupply>> psus; | 
| Brandon Wyman | 2bac860 | 2019-09-12 18:12:21 -0500 | [diff] [blame] | 287 | }; | 
|  | 288 |  | 
| Brandon Wyman | 63ea78b | 2020-09-24 16:49:09 -0500 | [diff] [blame] | 289 | } // namespace phosphor::power::manager |