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