blob: 6314438c3125c5afcd682bf84d4af57c460c61ab [file] [log] [blame]
Brandon Wyman2bac8602019-09-12 18:12:21 -05001#pragma once
2
Brandon Wymana0f33ce2019-10-17 18:32:29 -05003#include "power_supply.hpp"
4#include "types.hpp"
5#include "utility.hpp"
6
7#include <phosphor-logging/log.hpp>
Brandon Wyman2bac8602019-09-12 18:12:21 -05008#include <sdbusplus/bus/match.hpp>
9#include <sdeventplus/event.hpp>
10#include <sdeventplus/utility/timer.hpp>
11
Brandon Wymanaed1f752019-11-25 18:10:52 -060012struct sys_properties
13{
Adriana Kobylakd3a70d92021-06-04 16:24:45 +000014 int powerSupplyCount;
15 std::vector<uint64_t> inputVoltage;
Adriana Kobylak886574c2021-11-01 18:22:28 +000016 bool powerConfigFullLoad;
Brandon Wymanaed1f752019-11-25 18:10:52 -060017};
18
Brandon Wymana0f33ce2019-10-17 18:32:29 -050019using namespace phosphor::power::psu;
20using namespace phosphor::logging;
21
Brandon Wyman63ea78b2020-09-24 16:49:09 -050022namespace phosphor::power::manager
Brandon Wyman2bac8602019-09-12 18:12:21 -050023{
24
Adriana Kobylak2aba2b22021-10-11 16:00:05 +000025// Validation timeout. Allow 10s to detect if new EM interfaces show up in D-Bus
26// before performing the validation.
27constexpr auto validationTimeout = std::chrono::seconds(10);
Adriana Kobylaka4d38fa2021-10-05 19:57:47 +000028
Brandon Wyman2bac8602019-09-12 18:12:21 -050029/**
30 * @class PSUManager
31 *
32 * This class will create an object used to manage and monitor a list of power
33 * supply devices.
34 */
35class 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 Wyman510acaa2020-11-05 18:32:04 -060046 * 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 Wyman510acaa2020-11-05 18:32:04 -060054 * 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 Kobylak9bab9e12021-02-24 15:32:03 -060068 * @brief Initialize the system properties from the Supported Configuration
69 * D-Bus object provided by Entity Manager.
70 */
71 void getSystemProperties();
72
73 /**
Brandon Wyman2bac8602019-09-12 18:12:21 -050074 * Initializes the manager.
75 *
76 * Get current BMC state, ...
77 */
78 void initialize()
79 {
Brandon Wymana0f33ce2019-10-17 18:32:29 -050080 // 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 Kobylaka4d38fa2021-10-05 19:57:47 +000092 validationTimer->restartOnce(validationTimeout);
Brandon Wymana0f33ce2019-10-17 18:32:29 -050093 }
94 else
95 {
96 powerOn = false;
Adriana Kobylak8f16fb52021-03-31 15:50:15 +000097 runValidateConfig = true;
Brandon Wymana0f33ce2019-10-17 18:32:29 -050098 }
99 }
Patrick Williamsc1d4de52021-10-06 12:45:57 -0500100 catch (const std::exception& e)
Brandon Wymana0f33ce2019-10-17 18:32:29 -0500101 {
102 log<level::INFO>("Failed to get power state. Assuming it is off.");
103 powerOn = false;
Adriana Kobylak8f16fb52021-03-31 15:50:15 +0000104 runValidateConfig = true;
Brandon Wymana0f33ce2019-10-17 18:32:29 -0500105 }
106
Brandon Wyman59a35792020-06-04 12:37:40 -0500107 onOffConfig(phosphor::pmbus::ON_OFF_CONFIG_CONTROL_PIN_ONLY);
Brandon Wymana0f33ce2019-10-17 18:32:29 -0500108 clearFaults();
109 updateInventory();
Adriana Kobylakc0a07582021-10-13 15:52:25 +0000110 setPowerConfigGPIO();
Brandon Wyman2bac8602019-09-12 18:12:21 -0500111 }
112
113 /**
114 * Starts the timer to start monitoring the list of devices.
115 */
116 int run()
117 {
Brandon Wyman2fe51862019-11-25 16:43:21 -0600118 return timer->get_event().loop();
Brandon Wyman2bac8602019-09-12 18:12:21 -0500119 }
120
121 /**
Brandon Wyman59a35792020-06-04 12:37:40 -0500122 * Write PMBus ON_OFF_CONFIG
123 *
124 * This function will be called to cause the PMBus device driver to send the
125 * ON_OFF_CONFIG command. Takes one byte of data.
126 */
127 void onOffConfig(const uint8_t data)
128 {
129 for (auto& psu : psus)
130 {
131 psu->onOffConfig(data);
132 }
133 }
134
135 /**
Brandon Wyman2bac8602019-09-12 18:12:21 -0500136 * This function will be called in various situations in order to clear
137 * any fault status bits that may have been set, in order to start over
138 * with a clean state. Presence changes and power state changes will want
139 * to clear any faults logged.
140 */
141 void clearFaults()
142 {
Brandon Wymana0f33ce2019-10-17 18:32:29 -0500143 for (auto& psu : psus)
144 {
Brandon Wymanaed1f752019-11-25 18:10:52 -0600145 psu->clearFaults();
Brandon Wymana0f33ce2019-10-17 18:32:29 -0500146 }
Brandon Wyman2bac8602019-09-12 18:12:21 -0500147 }
148
149 private:
150 /**
151 * The D-Bus object
152 */
153 sdbusplus::bus::bus& bus;
154
155 /**
156 * The timer that runs to periodically check the power supplies.
157 */
Brandon Wyman2fe51862019-11-25 16:43:21 -0600158 std::unique_ptr<
159 sdeventplus::utility::Timer<sdeventplus::ClockId::Monotonic>>
160 timer;
Brandon Wyman2bac8602019-09-12 18:12:21 -0500161
162 /**
Adriana Kobylaka4d38fa2021-10-05 19:57:47 +0000163 * The timer that performs power supply validation as the entity manager
164 * interfaces show up in d-bus.
165 */
166 std::unique_ptr<
167 sdeventplus::utility::Timer<sdeventplus::ClockId::Monotonic>>
168 validationTimer;
169
170 /**
Brandon Wymanb76ab242020-09-16 18:06:06 -0500171 * Create an error
172 *
173 * @param[in] faultName - 'name' message for the BMC error log entry
Brandon Wyman8b662882021-10-08 17:31:51 +0000174 * @param[in,out] additionalData - The AdditionalData property for the error
Brandon Wymanb76ab242020-09-16 18:06:06 -0500175 */
176 void createError(const std::string& faultName,
Brandon Wyman8b662882021-10-08 17:31:51 +0000177 std::map<std::string, std::string>& additionalData);
Brandon Wymanb76ab242020-09-16 18:06:06 -0500178
179 /**
Brandon Wyman2bac8602019-09-12 18:12:21 -0500180 * Analyze the status of each of the power supplies.
Brandon Wymanb76ab242020-09-16 18:06:06 -0500181 *
182 * Log errors for faults, when and where appropriate.
Brandon Wyman2bac8602019-09-12 18:12:21 -0500183 */
Brandon Wyman63ea78b2020-09-24 16:49:09 -0500184 void analyze();
Brandon Wyman2bac8602019-09-12 18:12:21 -0500185
186 /** @brief True if the power is on. */
187 bool powerOn = false;
188
Adriana Kobylak2549d792022-01-26 20:51:30 +0000189 /** @brief True if an error for a brownout has already been logged. */
190 bool brownoutLogged = false;
191
Brandon Wymana0f33ce2019-10-17 18:32:29 -0500192 /** @brief Used as part of subscribing to power on state changes*/
193 std::string powerService;
194
Brandon Wyman2bac8602019-09-12 18:12:21 -0500195 /** @brief Used to subscribe to D-Bus power on state changes */
196 std::unique_ptr<sdbusplus::bus::match_t> powerOnMatch;
197
Adriana Kobylak9ba38232021-11-16 20:27:45 +0000198 /** @brief Used to subscribe to D-Bus power supply presence changes */
199 std::vector<std::unique_ptr<sdbusplus::bus::match_t>> presenceMatches;
200
Adriana Kobylak9bab9e12021-02-24 15:32:03 -0600201 /** @brief Used to subscribe to Entity Manager interfaces added */
202 std::unique_ptr<sdbusplus::bus::match_t> entityManagerIfacesAddedMatch;
203
Brandon Wyman2bac8602019-09-12 18:12:21 -0500204 /**
Brandon Wyman2bac8602019-09-12 18:12:21 -0500205 * @brief Callback for power state property changes
206 *
207 * Process changes to the powered on state property for the system.
208 *
209 * @param[in] msg - Data associated with the power state signal
210 */
211 void powerStateChanged(sdbusplus::message::message& msg);
212
213 /**
Adriana Kobylak9ba38232021-11-16 20:27:45 +0000214 * @brief Callback for inventory property changes
215 *
216 * Process change of the Present property for power supply.
217 *
218 * @param[in] msg - Data associated with the Present change signal
219 **/
220 void presenceChanged(sdbusplus::message::message& msg);
221
222 /**
Brandon Wyman3e429132021-03-18 18:03:14 -0500223 * @brief Callback for entity-manager interface added
Adriana Kobylak9bab9e12021-02-24 15:32:03 -0600224 *
Brandon Wyman3e429132021-03-18 18:03:14 -0500225 * Process the information from the supported configuration and or IBM CFFPS
226 * Connector interface being added.
Adriana Kobylak9bab9e12021-02-24 15:32:03 -0600227 *
228 * @param[in] msg - Data associated with the interfaces added signal
229 */
Brandon Wyman3e429132021-03-18 18:03:14 -0500230 void entityManagerIfaceAdded(sdbusplus::message::message& msg);
Adriana Kobylak9bab9e12021-02-24 15:32:03 -0600231
232 /**
Brandon Wyman2bac8602019-09-12 18:12:21 -0500233 * @brief Adds properties to the inventory.
234 *
235 * Reads the values from the devices and writes them to the associated
236 * power supply D-Bus inventory objects.
237 *
238 * This needs to be done on startup, and each time the presence state
239 * changes.
240 */
Brandon Wymana0f33ce2019-10-17 18:32:29 -0500241 void updateInventory()
242 {
243 for (auto& psu : psus)
244 {
Brandon Wymanaed1f752019-11-25 18:10:52 -0600245 psu->updateInventory();
Brandon Wymana0f33ce2019-10-17 18:32:29 -0500246 }
247 }
248
249 /**
Adriana Kobylake1074d82021-03-16 20:46:44 +0000250 * @brief Helper function to populate the system properties
251 *
252 * @param[in] properties - A map of property names and values
253 */
254 void populateSysProperties(const util::DbusPropertyMap& properties);
255
256 /**
Adriana Kobylak8f16fb52021-03-31 15:50:15 +0000257 * @brief Perform power supply configuration validation.
258 * @details Validates if the existing power supply properties are a
259 * supported configuration, and acts on its findings such as logging errors.
260 */
261 void validateConfig();
262
263 /**
264 * @brief Flag to indicate if the validateConfig() function should be run.
265 * Set to false once the configuration has been validated to avoid running
266 * multiple times due to interfaces added signal. Set to true during power
267 * off to trigger the validation on power on.
268 */
269 bool runValidateConfig = true;
270
271 /**
Adriana Kobylak4d9aaf92021-06-30 15:27:42 +0000272 * @brief Check that all PSUs have the same model name and that the system
273 * has the required number of PSUs present as specified in the Supported
274 * Configuration interface.
275 *
276 * @param[out] additionalData - Contains debug information on why the check
277 * might have failed. Can be used to fill in error logs.
278 * @return true if all the required PSUs are present, false otherwise.
279 */
280 bool hasRequiredPSUs(std::map<std::string, std::string>& additionalData);
281
282 /**
Adriana Kobylak523704d2021-09-21 15:55:41 +0000283 * @brief Helper function to validate that all PSUs have the same model name
284 *
285 * @param[out] model - The model name. Empty if there is a mismatch.
286 * @param[out] additionalData - If there is a mismatch, it contains debug
287 * information such as the mismatched model name.
288 * @return true if all the PSUs have the same model name, false otherwise.
289 */
290 bool validateModelName(std::string& model,
291 std::map<std::string, std::string>& additionalData);
292
293 /**
Adriana Kobylakc0a07582021-10-13 15:52:25 +0000294 * @brief Set the power-config-full-load GPIO depending on the EM full load
295 * property value.
296 */
297 void setPowerConfigGPIO();
298
299 /**
Adriana Kobylak9ea66a62021-03-24 17:54:14 +0000300 * @brief Map of supported PSU configurations that include the model name
301 * and their properties.
Brandon Wymanaed1f752019-11-25 18:10:52 -0600302 */
Adriana Kobylakd3a70d92021-06-04 16:24:45 +0000303 std::map<std::string, sys_properties> supportedConfigs;
Brandon Wymanaed1f752019-11-25 18:10:52 -0600304
305 /**
Brandon Wymana0f33ce2019-10-17 18:32:29 -0500306 * @brief The vector for power supplies.
307 */
Brandon Wymanaed1f752019-11-25 18:10:52 -0600308 std::vector<std::unique_ptr<PowerSupply>> psus;
Adriana Kobylakc0a07582021-10-13 15:52:25 +0000309
310 /**
311 * @brief The libgpiod object for setting the power supply config
312 */
313 std::unique_ptr<GPIOInterfaceBase> powerConfigGPIO = nullptr;
Brandon Wyman2bac8602019-09-12 18:12:21 -0500314};
315
Brandon Wyman63ea78b2020-09-24 16:49:09 -0500316} // namespace phosphor::power::manager