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 | { |
Brandon Wyman | aed1f75 | 2019-11-25 18:10:52 -0600 | [diff] [blame] | 14 | int maxPowerSupplies; |
| 15 | }; |
| 16 | |
Brandon Wyman | a0f33ce | 2019-10-17 18:32:29 -0500 | [diff] [blame] | 17 | using namespace phosphor::power::psu; |
| 18 | using namespace phosphor::logging; |
| 19 | |
Brandon Wyman | 63ea78b | 2020-09-24 16:49:09 -0500 | [diff] [blame] | 20 | namespace phosphor::power::manager |
Brandon Wyman | 2bac860 | 2019-09-12 18:12:21 -0500 | [diff] [blame] | 21 | { |
| 22 | |
| 23 | /** |
| 24 | * @class PSUManager |
| 25 | * |
| 26 | * This class will create an object used to manage and monitor a list of power |
| 27 | * supply devices. |
| 28 | */ |
| 29 | class PSUManager |
| 30 | { |
| 31 | public: |
| 32 | PSUManager() = delete; |
| 33 | ~PSUManager() = default; |
| 34 | PSUManager(const PSUManager&) = delete; |
| 35 | PSUManager& operator=(const PSUManager&) = delete; |
| 36 | PSUManager(PSUManager&&) = delete; |
| 37 | PSUManager& operator=(PSUManager&&) = delete; |
| 38 | |
| 39 | /** |
Brandon Wyman | 510acaa | 2020-11-05 18:32:04 -0600 | [diff] [blame] | 40 | * Constructor to read configuration from JSON file. |
Brandon Wyman | 2bac860 | 2019-09-12 18:12:21 -0500 | [diff] [blame] | 41 | * |
| 42 | * @param[in] bus - D-Bus bus object |
| 43 | * @param[in] e - event object |
Brandon Wyman | 2fe5186 | 2019-11-25 16:43:21 -0600 | [diff] [blame] | 44 | * @param[in] configfile - string path to the configuration file |
Brandon Wyman | 2bac860 | 2019-09-12 18:12:21 -0500 | [diff] [blame] | 45 | */ |
| 46 | PSUManager(sdbusplus::bus::bus& bus, const sdeventplus::Event& e, |
Brandon Wyman | aed1f75 | 2019-11-25 18:10:52 -0600 | [diff] [blame] | 47 | const std::string& configfile); |
Brandon Wyman | 2fe5186 | 2019-11-25 16:43:21 -0600 | [diff] [blame] | 48 | |
Adriana Kobylak | 169975c | 2021-03-06 15:19:55 +0000 | [diff] [blame] | 49 | /** |
Brandon Wyman | 510acaa | 2020-11-05 18:32:04 -0600 | [diff] [blame] | 50 | * Constructor to read configuration from D-Bus. |
| 51 | * |
| 52 | * @param[in] bus - D-Bus bus object |
| 53 | * @param[in] e - event object |
| 54 | */ |
| 55 | PSUManager(sdbusplus::bus::bus& bus, const sdeventplus::Event& e); |
| 56 | |
| 57 | /** |
Adriana Kobylak | 9bab9e1 | 2021-02-24 15:32:03 -0600 | [diff] [blame] | 58 | * @brief Initialize the PowerSupply objects from the JSON config file. |
Adriana Kobylak | 169975c | 2021-03-06 15:19:55 +0000 | [diff] [blame] | 59 | * @param[in] path - Path to the JSON config file |
| 60 | */ |
| 61 | void getJSONProperties(const std::string& path); |
Brandon Wyman | 2fe5186 | 2019-11-25 16:43:21 -0600 | [diff] [blame] | 62 | |
Brandon Wyman | 2bac860 | 2019-09-12 18:12:21 -0500 | [diff] [blame] | 63 | /** |
Brandon Wyman | 510acaa | 2020-11-05 18:32:04 -0600 | [diff] [blame] | 64 | * Get PSU properties from D-Bus, use that to build a power supply |
| 65 | * object. |
| 66 | * |
| 67 | * @param[in] properties - A map of property names and values |
| 68 | * |
| 69 | */ |
| 70 | void getPSUProperties(util::DbusPropertyMap& properties); |
| 71 | |
| 72 | /** |
| 73 | * Get PSU configuration from D-Bus |
| 74 | */ |
| 75 | void getPSUConfiguration(); |
| 76 | |
| 77 | /** |
Adriana Kobylak | 9bab9e1 | 2021-02-24 15:32:03 -0600 | [diff] [blame] | 78 | * @brief Initialize the system properties from the Supported Configuration |
| 79 | * D-Bus object provided by Entity Manager. |
| 80 | */ |
| 81 | void getSystemProperties(); |
| 82 | |
| 83 | /** |
Brandon Wyman | 2bac860 | 2019-09-12 18:12:21 -0500 | [diff] [blame] | 84 | * Initializes the manager. |
| 85 | * |
| 86 | * Get current BMC state, ... |
| 87 | */ |
| 88 | void initialize() |
| 89 | { |
Brandon Wyman | a0f33ce | 2019-10-17 18:32:29 -0500 | [diff] [blame] | 90 | // When state = 1, system is powered on |
| 91 | int32_t state = 0; |
| 92 | |
| 93 | try |
| 94 | { |
| 95 | // Use getProperty utility function to get power state. |
| 96 | util::getProperty<int32_t>(POWER_IFACE, "state", POWER_OBJ_PATH, |
| 97 | powerService, bus, state); |
| 98 | |
| 99 | if (state) |
| 100 | { |
| 101 | powerOn = true; |
| 102 | } |
| 103 | else |
| 104 | { |
| 105 | powerOn = false; |
| 106 | } |
| 107 | } |
| 108 | catch (std::exception& e) |
| 109 | { |
| 110 | log<level::INFO>("Failed to get power state. Assuming it is off."); |
| 111 | powerOn = false; |
| 112 | } |
| 113 | |
Brandon Wyman | 59a3579 | 2020-06-04 12:37:40 -0500 | [diff] [blame] | 114 | onOffConfig(phosphor::pmbus::ON_OFF_CONFIG_CONTROL_PIN_ONLY); |
Brandon Wyman | a0f33ce | 2019-10-17 18:32:29 -0500 | [diff] [blame] | 115 | clearFaults(); |
| 116 | updateInventory(); |
Brandon Wyman | 2bac860 | 2019-09-12 18:12:21 -0500 | [diff] [blame] | 117 | } |
| 118 | |
| 119 | /** |
| 120 | * Starts the timer to start monitoring the list of devices. |
| 121 | */ |
| 122 | int run() |
| 123 | { |
Brandon Wyman | 2fe5186 | 2019-11-25 16:43:21 -0600 | [diff] [blame] | 124 | return timer->get_event().loop(); |
Brandon Wyman | 2bac860 | 2019-09-12 18:12:21 -0500 | [diff] [blame] | 125 | } |
| 126 | |
| 127 | /** |
Brandon Wyman | 59a3579 | 2020-06-04 12:37:40 -0500 | [diff] [blame] | 128 | * Write PMBus ON_OFF_CONFIG |
| 129 | * |
| 130 | * This function will be called to cause the PMBus device driver to send the |
| 131 | * ON_OFF_CONFIG command. Takes one byte of data. |
| 132 | */ |
| 133 | void onOffConfig(const uint8_t data) |
| 134 | { |
| 135 | for (auto& psu : psus) |
| 136 | { |
| 137 | psu->onOffConfig(data); |
| 138 | } |
| 139 | } |
| 140 | |
| 141 | /** |
Brandon Wyman | 2bac860 | 2019-09-12 18:12:21 -0500 | [diff] [blame] | 142 | * This function will be called in various situations in order to clear |
| 143 | * any fault status bits that may have been set, in order to start over |
| 144 | * with a clean state. Presence changes and power state changes will want |
| 145 | * to clear any faults logged. |
| 146 | */ |
| 147 | void clearFaults() |
| 148 | { |
Brandon Wyman | a0f33ce | 2019-10-17 18:32:29 -0500 | [diff] [blame] | 149 | for (auto& psu : psus) |
| 150 | { |
Brandon Wyman | aed1f75 | 2019-11-25 18:10:52 -0600 | [diff] [blame] | 151 | psu->clearFaults(); |
Brandon Wyman | a0f33ce | 2019-10-17 18:32:29 -0500 | [diff] [blame] | 152 | } |
Brandon Wyman | 2bac860 | 2019-09-12 18:12:21 -0500 | [diff] [blame] | 153 | } |
| 154 | |
| 155 | private: |
| 156 | /** |
| 157 | * The D-Bus object |
| 158 | */ |
| 159 | sdbusplus::bus::bus& bus; |
| 160 | |
| 161 | /** |
| 162 | * The timer that runs to periodically check the power supplies. |
| 163 | */ |
Brandon Wyman | 2fe5186 | 2019-11-25 16:43:21 -0600 | [diff] [blame] | 164 | std::unique_ptr< |
| 165 | sdeventplus::utility::Timer<sdeventplus::ClockId::Monotonic>> |
| 166 | timer; |
Brandon Wyman | 2bac860 | 2019-09-12 18:12:21 -0500 | [diff] [blame] | 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 |
| 172 | * @param[in] additionalData - The AdditionalData property for the error |
| 173 | */ |
| 174 | void createError(const std::string& faultName, |
| 175 | const std::map<std::string, std::string>& additionalData); |
| 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 | 169975c | 2021-03-06 15:19:55 +0000 | [diff] [blame] | 240 | * @brief The system properties. |
Brandon Wyman | aed1f75 | 2019-11-25 18:10:52 -0600 | [diff] [blame] | 241 | */ |
Adriana Kobylak | 169975c | 2021-03-06 15:19:55 +0000 | [diff] [blame] | 242 | sys_properties sysProperties; |
Brandon Wyman | aed1f75 | 2019-11-25 18:10:52 -0600 | [diff] [blame] | 243 | |
| 244 | /** |
Brandon Wyman | a0f33ce | 2019-10-17 18:32:29 -0500 | [diff] [blame] | 245 | * @brief The vector for power supplies. |
| 246 | */ |
Brandon Wyman | aed1f75 | 2019-11-25 18:10:52 -0600 | [diff] [blame] | 247 | std::vector<std::unique_ptr<PowerSupply>> psus; |
Brandon Wyman | 2bac860 | 2019-09-12 18:12:21 -0500 | [diff] [blame] | 248 | }; |
| 249 | |
Brandon Wyman | 63ea78b | 2020-09-24 16:49:09 -0500 | [diff] [blame] | 250 | } // namespace phosphor::power::manager |