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 | /** |
| 40 | * Constructor |
| 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 | /** |
Adriana Kobylak | 9bab9e1 | 2021-02-24 15:32:03 -0600 | [diff] [blame] | 50 | * @brief Initialize the PowerSupply objects from the JSON config file. |
Adriana Kobylak | 169975c | 2021-03-06 15:19:55 +0000 | [diff] [blame] | 51 | * @param[in] path - Path to the JSON config file |
| 52 | */ |
| 53 | void getJSONProperties(const std::string& path); |
Brandon Wyman | 2fe5186 | 2019-11-25 16:43:21 -0600 | [diff] [blame] | 54 | |
Brandon Wyman | 2bac860 | 2019-09-12 18:12:21 -0500 | [diff] [blame] | 55 | /** |
Adriana Kobylak | 9bab9e1 | 2021-02-24 15:32:03 -0600 | [diff] [blame] | 56 | * @brief Initialize the system properties from the Supported Configuration |
| 57 | * D-Bus object provided by Entity Manager. |
| 58 | */ |
| 59 | void getSystemProperties(); |
| 60 | |
| 61 | /** |
Brandon Wyman | 2bac860 | 2019-09-12 18:12:21 -0500 | [diff] [blame] | 62 | * Initializes the manager. |
| 63 | * |
| 64 | * Get current BMC state, ... |
| 65 | */ |
| 66 | void initialize() |
| 67 | { |
Brandon Wyman | a0f33ce | 2019-10-17 18:32:29 -0500 | [diff] [blame] | 68 | // When state = 1, system is powered on |
| 69 | int32_t state = 0; |
| 70 | |
| 71 | try |
| 72 | { |
| 73 | // Use getProperty utility function to get power state. |
| 74 | util::getProperty<int32_t>(POWER_IFACE, "state", POWER_OBJ_PATH, |
| 75 | powerService, bus, state); |
| 76 | |
| 77 | if (state) |
| 78 | { |
| 79 | powerOn = true; |
| 80 | } |
| 81 | else |
| 82 | { |
| 83 | powerOn = false; |
| 84 | } |
| 85 | } |
| 86 | catch (std::exception& e) |
| 87 | { |
| 88 | log<level::INFO>("Failed to get power state. Assuming it is off."); |
| 89 | powerOn = false; |
| 90 | } |
| 91 | |
Brandon Wyman | 59a3579 | 2020-06-04 12:37:40 -0500 | [diff] [blame] | 92 | onOffConfig(phosphor::pmbus::ON_OFF_CONFIG_CONTROL_PIN_ONLY); |
Brandon Wyman | a0f33ce | 2019-10-17 18:32:29 -0500 | [diff] [blame] | 93 | clearFaults(); |
| 94 | updateInventory(); |
Brandon Wyman | 2bac860 | 2019-09-12 18:12:21 -0500 | [diff] [blame] | 95 | } |
| 96 | |
| 97 | /** |
| 98 | * Starts the timer to start monitoring the list of devices. |
| 99 | */ |
| 100 | int run() |
| 101 | { |
Brandon Wyman | 2fe5186 | 2019-11-25 16:43:21 -0600 | [diff] [blame] | 102 | return timer->get_event().loop(); |
Brandon Wyman | 2bac860 | 2019-09-12 18:12:21 -0500 | [diff] [blame] | 103 | } |
| 104 | |
| 105 | /** |
Brandon Wyman | 59a3579 | 2020-06-04 12:37:40 -0500 | [diff] [blame] | 106 | * Write PMBus ON_OFF_CONFIG |
| 107 | * |
| 108 | * This function will be called to cause the PMBus device driver to send the |
| 109 | * ON_OFF_CONFIG command. Takes one byte of data. |
| 110 | */ |
| 111 | void onOffConfig(const uint8_t data) |
| 112 | { |
| 113 | for (auto& psu : psus) |
| 114 | { |
| 115 | psu->onOffConfig(data); |
| 116 | } |
| 117 | } |
| 118 | |
| 119 | /** |
Brandon Wyman | 2bac860 | 2019-09-12 18:12:21 -0500 | [diff] [blame] | 120 | * This function will be called in various situations in order to clear |
| 121 | * any fault status bits that may have been set, in order to start over |
| 122 | * with a clean state. Presence changes and power state changes will want |
| 123 | * to clear any faults logged. |
| 124 | */ |
| 125 | void clearFaults() |
| 126 | { |
Brandon Wyman | a0f33ce | 2019-10-17 18:32:29 -0500 | [diff] [blame] | 127 | for (auto& psu : psus) |
| 128 | { |
Brandon Wyman | aed1f75 | 2019-11-25 18:10:52 -0600 | [diff] [blame] | 129 | psu->clearFaults(); |
Brandon Wyman | a0f33ce | 2019-10-17 18:32:29 -0500 | [diff] [blame] | 130 | } |
Brandon Wyman | 2bac860 | 2019-09-12 18:12:21 -0500 | [diff] [blame] | 131 | } |
| 132 | |
| 133 | private: |
| 134 | /** |
| 135 | * The D-Bus object |
| 136 | */ |
| 137 | sdbusplus::bus::bus& bus; |
| 138 | |
| 139 | /** |
| 140 | * The timer that runs to periodically check the power supplies. |
| 141 | */ |
Brandon Wyman | 2fe5186 | 2019-11-25 16:43:21 -0600 | [diff] [blame] | 142 | std::unique_ptr< |
| 143 | sdeventplus::utility::Timer<sdeventplus::ClockId::Monotonic>> |
| 144 | timer; |
Brandon Wyman | 2bac860 | 2019-09-12 18:12:21 -0500 | [diff] [blame] | 145 | |
| 146 | /** |
Brandon Wyman | b76ab24 | 2020-09-16 18:06:06 -0500 | [diff] [blame] | 147 | * Create an error |
| 148 | * |
| 149 | * @param[in] faultName - 'name' message for the BMC error log entry |
| 150 | * @param[in] additionalData - The AdditionalData property for the error |
| 151 | */ |
| 152 | void createError(const std::string& faultName, |
| 153 | const std::map<std::string, std::string>& additionalData); |
| 154 | |
| 155 | /** |
Brandon Wyman | 2bac860 | 2019-09-12 18:12:21 -0500 | [diff] [blame] | 156 | * Analyze the status of each of the power supplies. |
Brandon Wyman | b76ab24 | 2020-09-16 18:06:06 -0500 | [diff] [blame] | 157 | * |
| 158 | * Log errors for faults, when and where appropriate. |
Brandon Wyman | 2bac860 | 2019-09-12 18:12:21 -0500 | [diff] [blame] | 159 | */ |
Brandon Wyman | 63ea78b | 2020-09-24 16:49:09 -0500 | [diff] [blame] | 160 | void analyze(); |
Brandon Wyman | 2bac860 | 2019-09-12 18:12:21 -0500 | [diff] [blame] | 161 | |
| 162 | /** @brief True if the power is on. */ |
| 163 | bool powerOn = false; |
| 164 | |
Brandon Wyman | a0f33ce | 2019-10-17 18:32:29 -0500 | [diff] [blame] | 165 | /** @brief Used as part of subscribing to power on state changes*/ |
| 166 | std::string powerService; |
| 167 | |
Brandon Wyman | 2bac860 | 2019-09-12 18:12:21 -0500 | [diff] [blame] | 168 | /** @brief Used to subscribe to D-Bus power on state changes */ |
| 169 | std::unique_ptr<sdbusplus::bus::match_t> powerOnMatch; |
| 170 | |
Adriana Kobylak | 9bab9e1 | 2021-02-24 15:32:03 -0600 | [diff] [blame] | 171 | /** @brief Used to subscribe to Entity Manager interfaces added */ |
| 172 | std::unique_ptr<sdbusplus::bus::match_t> entityManagerIfacesAddedMatch; |
| 173 | |
Brandon Wyman | 2bac860 | 2019-09-12 18:12:21 -0500 | [diff] [blame] | 174 | /** |
Brandon Wyman | 2bac860 | 2019-09-12 18:12:21 -0500 | [diff] [blame] | 175 | * @brief Callback for power state property changes |
| 176 | * |
| 177 | * Process changes to the powered on state property for the system. |
| 178 | * |
| 179 | * @param[in] msg - Data associated with the power state signal |
| 180 | */ |
| 181 | void powerStateChanged(sdbusplus::message::message& msg); |
| 182 | |
| 183 | /** |
Brandon Wyman | 3e42913 | 2021-03-18 18:03:14 -0500 | [diff] [blame] | 184 | * @brief Callback for entity-manager interface added |
Adriana Kobylak | 9bab9e1 | 2021-02-24 15:32:03 -0600 | [diff] [blame] | 185 | * |
Brandon Wyman | 3e42913 | 2021-03-18 18:03:14 -0500 | [diff] [blame] | 186 | * Process the information from the supported configuration and or IBM CFFPS |
| 187 | * Connector interface being added. |
Adriana Kobylak | 9bab9e1 | 2021-02-24 15:32:03 -0600 | [diff] [blame] | 188 | * |
| 189 | * @param[in] msg - Data associated with the interfaces added signal |
| 190 | */ |
Brandon Wyman | 3e42913 | 2021-03-18 18:03:14 -0500 | [diff] [blame] | 191 | void entityManagerIfaceAdded(sdbusplus::message::message& msg); |
Adriana Kobylak | 9bab9e1 | 2021-02-24 15:32:03 -0600 | [diff] [blame] | 192 | |
| 193 | /** |
Brandon Wyman | 2bac860 | 2019-09-12 18:12:21 -0500 | [diff] [blame] | 194 | * @brief Adds properties to the inventory. |
| 195 | * |
| 196 | * Reads the values from the devices and writes them to the associated |
| 197 | * power supply D-Bus inventory objects. |
| 198 | * |
| 199 | * This needs to be done on startup, and each time the presence state |
| 200 | * changes. |
| 201 | */ |
Brandon Wyman | a0f33ce | 2019-10-17 18:32:29 -0500 | [diff] [blame] | 202 | void updateInventory() |
| 203 | { |
| 204 | for (auto& psu : psus) |
| 205 | { |
Brandon Wyman | aed1f75 | 2019-11-25 18:10:52 -0600 | [diff] [blame] | 206 | psu->updateInventory(); |
Brandon Wyman | a0f33ce | 2019-10-17 18:32:29 -0500 | [diff] [blame] | 207 | } |
| 208 | } |
| 209 | |
| 210 | /** |
Adriana Kobylak | e1074d8 | 2021-03-16 20:46:44 +0000 | [diff] [blame^] | 211 | * @brief Helper function to populate the system properties |
| 212 | * |
| 213 | * @param[in] properties - A map of property names and values |
| 214 | */ |
| 215 | void populateSysProperties(const util::DbusPropertyMap& properties); |
| 216 | |
| 217 | /** |
Adriana Kobylak | 169975c | 2021-03-06 15:19:55 +0000 | [diff] [blame] | 218 | * @brief The system properties. |
Brandon Wyman | aed1f75 | 2019-11-25 18:10:52 -0600 | [diff] [blame] | 219 | */ |
Adriana Kobylak | 169975c | 2021-03-06 15:19:55 +0000 | [diff] [blame] | 220 | sys_properties sysProperties; |
Brandon Wyman | aed1f75 | 2019-11-25 18:10:52 -0600 | [diff] [blame] | 221 | |
| 222 | /** |
Brandon Wyman | a0f33ce | 2019-10-17 18:32:29 -0500 | [diff] [blame] | 223 | * @brief The vector for power supplies. |
| 224 | */ |
Brandon Wyman | aed1f75 | 2019-11-25 18:10:52 -0600 | [diff] [blame] | 225 | std::vector<std::unique_ptr<PowerSupply>> psus; |
Brandon Wyman | 2bac860 | 2019-09-12 18:12:21 -0500 | [diff] [blame] | 226 | }; |
| 227 | |
Brandon Wyman | 63ea78b | 2020-09-24 16:49:09 -0500 | [diff] [blame] | 228 | } // namespace phosphor::power::manager |