blob: 2ee61a69ae88d6f48a93fb3ee0dfd5315e28c1f6 [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>
Adriana Kobylakc9b05732022-03-19 15:15:10 +00009#include <sdbusplus/server/manager.hpp>
10#include <sdbusplus/server/object.hpp>
Brandon Wyman2bac8602019-09-12 18:12:21 -050011#include <sdeventplus/event.hpp>
12#include <sdeventplus/utility/timer.hpp>
Adriana Kobylakc9b05732022-03-19 15:15:10 +000013#include <xyz/openbmc_project/State/Decorator/PowerSystemInputs/server.hpp>
Brandon Wyman2bac8602019-09-12 18:12:21 -050014
Brandon Wymanaed1f752019-11-25 18:10:52 -060015struct sys_properties
16{
Adriana Kobylakd3a70d92021-06-04 16:24:45 +000017 int powerSupplyCount;
18 std::vector<uint64_t> inputVoltage;
Adriana Kobylak886574c2021-11-01 18:22:28 +000019 bool powerConfigFullLoad;
Brandon Wymanaed1f752019-11-25 18:10:52 -060020};
21
Brandon Wymana0f33ce2019-10-17 18:32:29 -050022using namespace phosphor::power::psu;
23using namespace phosphor::logging;
24
Brandon Wyman63ea78b2020-09-24 16:49:09 -050025namespace phosphor::power::manager
Brandon Wyman2bac8602019-09-12 18:12:21 -050026{
27
Adriana Kobylakc9b05732022-03-19 15:15:10 +000028using PowerSystemInputsInterface = sdbusplus::xyz::openbmc_project::State::
29 Decorator::server::PowerSystemInputs;
30using PowerSystemInputsObject =
31 sdbusplus::server::object_t<PowerSystemInputsInterface>;
32
Adriana Kobylak2aba2b22021-10-11 16:00:05 +000033// Validation timeout. Allow 10s to detect if new EM interfaces show up in D-Bus
34// before performing the validation.
35constexpr auto validationTimeout = std::chrono::seconds(10);
Adriana Kobylaka4d38fa2021-10-05 19:57:47 +000036
Brandon Wyman2bac8602019-09-12 18:12:21 -050037/**
Adriana Kobylakc9b05732022-03-19 15:15:10 +000038 * @class PowerSystemInputs
39 * @brief A concrete implementation for the PowerSystemInputs interface.
40 */
41class PowerSystemInputs : public PowerSystemInputsObject
42{
43 public:
Patrick Williams7354ce62022-07-22 19:26:56 -050044 PowerSystemInputs(sdbusplus::bus_t& bus, const std::string& path) :
Adriana Kobylakc9b05732022-03-19 15:15:10 +000045 PowerSystemInputsObject(bus, path.c_str())
46 {}
47};
48
49/**
Brandon Wyman2bac8602019-09-12 18:12:21 -050050 * @class PSUManager
51 *
52 * This class will create an object used to manage and monitor a list of power
53 * supply devices.
54 */
55class PSUManager
56{
57 public:
58 PSUManager() = delete;
59 ~PSUManager() = default;
60 PSUManager(const PSUManager&) = delete;
61 PSUManager& operator=(const PSUManager&) = delete;
62 PSUManager(PSUManager&&) = delete;
63 PSUManager& operator=(PSUManager&&) = delete;
64
65 /**
Brandon Wyman510acaa2020-11-05 18:32:04 -060066 * Constructor to read configuration from D-Bus.
67 *
68 * @param[in] bus - D-Bus bus object
69 * @param[in] e - event object
70 */
Patrick Williams7354ce62022-07-22 19:26:56 -050071 PSUManager(sdbusplus::bus_t& bus, const sdeventplus::Event& e);
Brandon Wyman510acaa2020-11-05 18:32:04 -060072
73 /**
Brandon Wyman510acaa2020-11-05 18:32:04 -060074 * Get PSU properties from D-Bus, use that to build a power supply
75 * object.
76 *
77 * @param[in] properties - A map of property names and values
78 *
79 */
80 void getPSUProperties(util::DbusPropertyMap& properties);
81
82 /**
83 * Get PSU configuration from D-Bus
84 */
85 void getPSUConfiguration();
86
87 /**
Adriana Kobylak9bab9e12021-02-24 15:32:03 -060088 * @brief Initialize the system properties from the Supported Configuration
89 * D-Bus object provided by Entity Manager.
90 */
91 void getSystemProperties();
92
93 /**
Brandon Wyman2bac8602019-09-12 18:12:21 -050094 * Initializes the manager.
95 *
96 * Get current BMC state, ...
97 */
Jim Wrightaca86d02022-06-10 12:01:39 -050098 void initialize();
Brandon Wyman2bac8602019-09-12 18:12:21 -050099
100 /**
101 * Starts the timer to start monitoring the list of devices.
102 */
103 int run()
104 {
Brandon Wyman2fe51862019-11-25 16:43:21 -0600105 return timer->get_event().loop();
Brandon Wyman2bac8602019-09-12 18:12:21 -0500106 }
107
108 /**
Brandon Wyman59a35792020-06-04 12:37:40 -0500109 * Write PMBus ON_OFF_CONFIG
110 *
111 * This function will be called to cause the PMBus device driver to send the
112 * ON_OFF_CONFIG command. Takes one byte of data.
113 */
114 void onOffConfig(const uint8_t data)
115 {
116 for (auto& psu : psus)
117 {
118 psu->onOffConfig(data);
119 }
120 }
121
122 /**
Brandon Wyman2bac8602019-09-12 18:12:21 -0500123 * This function will be called in various situations in order to clear
124 * any fault status bits that may have been set, in order to start over
125 * with a clean state. Presence changes and power state changes will want
126 * to clear any faults logged.
127 */
128 void clearFaults()
129 {
Brandon Wyman10fc6e82022-02-08 20:51:22 +0000130 setPowerSupplyError("");
Brandon Wymana0f33ce2019-10-17 18:32:29 -0500131 for (auto& psu : psus)
132 {
Brandon Wymanaed1f752019-11-25 18:10:52 -0600133 psu->clearFaults();
Brandon Wymana0f33ce2019-10-17 18:32:29 -0500134 }
Brandon Wyman2bac8602019-09-12 18:12:21 -0500135 }
136
137 private:
138 /**
139 * The D-Bus object
140 */
Patrick Williams7354ce62022-07-22 19:26:56 -0500141 sdbusplus::bus_t& bus;
Brandon Wyman2bac8602019-09-12 18:12:21 -0500142
143 /**
144 * The timer that runs to periodically check the power supplies.
145 */
Brandon Wyman2fe51862019-11-25 16:43:21 -0600146 std::unique_ptr<
147 sdeventplus::utility::Timer<sdeventplus::ClockId::Monotonic>>
148 timer;
Brandon Wyman2bac8602019-09-12 18:12:21 -0500149
150 /**
Adriana Kobylaka4d38fa2021-10-05 19:57:47 +0000151 * The timer that performs power supply validation as the entity manager
152 * interfaces show up in d-bus.
153 */
154 std::unique_ptr<
155 sdeventplus::utility::Timer<sdeventplus::ClockId::Monotonic>>
156 validationTimer;
157
158 /**
Brandon Wyman10fc6e82022-02-08 20:51:22 +0000159 * Let power control/sequencer application know of PSU error(s).
160 *
161 * @param[in] psuErrorString - string for power supply error
162 */
163 void setPowerSupplyError(const std::string& psuErrorString);
164
165 /**
Brandon Wymanb76ab242020-09-16 18:06:06 -0500166 * Create an error
167 *
168 * @param[in] faultName - 'name' message for the BMC error log entry
Brandon Wyman8b662882021-10-08 17:31:51 +0000169 * @param[in,out] additionalData - The AdditionalData property for the error
Brandon Wymanb76ab242020-09-16 18:06:06 -0500170 */
171 void createError(const std::string& faultName,
Brandon Wyman8b662882021-10-08 17:31:51 +0000172 std::map<std::string, std::string>& additionalData);
Brandon Wymanb76ab242020-09-16 18:06:06 -0500173
174 /**
Brandon Wyman2bac8602019-09-12 18:12:21 -0500175 * Analyze the status of each of the power supplies.
Brandon Wymanb76ab242020-09-16 18:06:06 -0500176 *
177 * Log errors for faults, when and where appropriate.
Brandon Wyman2bac8602019-09-12 18:12:21 -0500178 */
Brandon Wyman63ea78b2020-09-24 16:49:09 -0500179 void analyze();
Brandon Wyman2bac8602019-09-12 18:12:21 -0500180
Jim Wright7f9288c2022-12-08 11:57:04 -0600181 /**
182 * @brief Analyze the set of the power supplies for a brownout failure. Log
183 * error when necessary, clear brownout condition when window has passed.
184 */
185 void analyzeBrownout();
186
Brandon Wyman2bac8602019-09-12 18:12:21 -0500187 /** @brief True if the power is on. */
188 bool powerOn = false;
189
Jim Wrightaca86d02022-06-10 12:01:39 -0500190 /** @brief True if power control is in the window between chassis pgood loss
191 * and power off. */
192 bool powerFaultOccurring = false;
193
Adriana Kobylak2549d792022-01-26 20:51:30 +0000194 /** @brief True if an error for a brownout has already been logged. */
195 bool brownoutLogged = false;
196
Brandon Wymana0f33ce2019-10-17 18:32:29 -0500197 /** @brief Used as part of subscribing to power on state changes*/
198 std::string powerService;
199
Brandon Wyman2bac8602019-09-12 18:12:21 -0500200 /** @brief Used to subscribe to D-Bus power on state changes */
201 std::unique_ptr<sdbusplus::bus::match_t> powerOnMatch;
202
Adriana Kobylak9ba38232021-11-16 20:27:45 +0000203 /** @brief Used to subscribe to D-Bus power supply presence changes */
204 std::vector<std::unique_ptr<sdbusplus::bus::match_t>> presenceMatches;
205
Adriana Kobylak9bab9e12021-02-24 15:32:03 -0600206 /** @brief Used to subscribe to Entity Manager interfaces added */
207 std::unique_ptr<sdbusplus::bus::match_t> entityManagerIfacesAddedMatch;
208
Brandon Wyman2bac8602019-09-12 18:12:21 -0500209 /**
Brandon Wyman2bac8602019-09-12 18:12:21 -0500210 * @brief Callback for power state property changes
211 *
212 * Process changes to the powered on state property for the system.
213 *
214 * @param[in] msg - Data associated with the power state signal
215 */
Patrick Williams7354ce62022-07-22 19:26:56 -0500216 void powerStateChanged(sdbusplus::message_t& msg);
Brandon Wyman2bac8602019-09-12 18:12:21 -0500217
218 /**
Adriana Kobylak9ba38232021-11-16 20:27:45 +0000219 * @brief Callback for inventory property changes
220 *
221 * Process change of the Present property for power supply.
222 *
223 * @param[in] msg - Data associated with the Present change signal
224 **/
Patrick Williams7354ce62022-07-22 19:26:56 -0500225 void presenceChanged(sdbusplus::message_t& msg);
Adriana Kobylak9ba38232021-11-16 20:27:45 +0000226
227 /**
Brandon Wyman3e429132021-03-18 18:03:14 -0500228 * @brief Callback for entity-manager interface added
Adriana Kobylak9bab9e12021-02-24 15:32:03 -0600229 *
Brandon Wyman3e429132021-03-18 18:03:14 -0500230 * Process the information from the supported configuration and or IBM CFFPS
231 * Connector interface being added.
Adriana Kobylak9bab9e12021-02-24 15:32:03 -0600232 *
233 * @param[in] msg - Data associated with the interfaces added signal
234 */
Patrick Williams7354ce62022-07-22 19:26:56 -0500235 void entityManagerIfaceAdded(sdbusplus::message_t& msg);
Adriana Kobylak9bab9e12021-02-24 15:32:03 -0600236
237 /**
Brandon Wyman2bac8602019-09-12 18:12:21 -0500238 * @brief Adds properties to the inventory.
239 *
240 * Reads the values from the devices and writes them to the associated
241 * power supply D-Bus inventory objects.
242 *
243 * This needs to be done on startup, and each time the presence state
244 * changes.
245 */
Brandon Wymana0f33ce2019-10-17 18:32:29 -0500246 void updateInventory()
247 {
248 for (auto& psu : psus)
249 {
Brandon Wymanaed1f752019-11-25 18:10:52 -0600250 psu->updateInventory();
Brandon Wymana0f33ce2019-10-17 18:32:29 -0500251 }
252 }
253
254 /**
Adriana Kobylake1074d82021-03-16 20:46:44 +0000255 * @brief Helper function to populate the system properties
256 *
257 * @param[in] properties - A map of property names and values
258 */
259 void populateSysProperties(const util::DbusPropertyMap& properties);
260
261 /**
Brandon Wyman64e97752022-06-03 23:50:13 +0000262 * @brief Update inventory for missing required power supplies
263 */
264 void updateMissingPSUs();
265
266 /**
Adriana Kobylak8f16fb52021-03-31 15:50:15 +0000267 * @brief Perform power supply configuration validation.
268 * @details Validates if the existing power supply properties are a
269 * supported configuration, and acts on its findings such as logging errors.
270 */
271 void validateConfig();
272
273 /**
274 * @brief Flag to indicate if the validateConfig() function should be run.
275 * Set to false once the configuration has been validated to avoid running
276 * multiple times due to interfaces added signal. Set to true during power
277 * off to trigger the validation on power on.
278 */
279 bool runValidateConfig = true;
280
281 /**
Adriana Kobylak4d9aaf92021-06-30 15:27:42 +0000282 * @brief Check that all PSUs have the same model name and that the system
283 * has the required number of PSUs present as specified in the Supported
284 * Configuration interface.
285 *
286 * @param[out] additionalData - Contains debug information on why the check
287 * might have failed. Can be used to fill in error logs.
288 * @return true if all the required PSUs are present, false otherwise.
289 */
290 bool hasRequiredPSUs(std::map<std::string, std::string>& additionalData);
291
292 /**
Shawn McCarney9252b7e2022-06-10 12:47:38 -0500293 * @brief Returns the number of PSUs that are required to be present.
294 *
295 * @return required number of PSUs, or 0 if the number could not be
296 * determined.
297 */
298 unsigned int getRequiredPSUCount();
299
300 /**
301 * @brief Returns whether the specified PSU is required to be present.
302 *
303 * @param[in] psu - Power supply to check
304 * @return true if PSU is required, false otherwise.
305 */
306 bool isRequiredPSU(const PowerSupply& psu);
307
308 /**
Adriana Kobylak523704d2021-09-21 15:55:41 +0000309 * @brief Helper function to validate that all PSUs have the same model name
310 *
311 * @param[out] model - The model name. Empty if there is a mismatch.
312 * @param[out] additionalData - If there is a mismatch, it contains debug
313 * information such as the mismatched model name.
314 * @return true if all the PSUs have the same model name, false otherwise.
315 */
316 bool validateModelName(std::string& model,
317 std::map<std::string, std::string>& additionalData);
318
319 /**
Adriana Kobylakc0a07582021-10-13 15:52:25 +0000320 * @brief Set the power-config-full-load GPIO depending on the EM full load
321 * property value.
322 */
323 void setPowerConfigGPIO();
324
325 /**
Adriana Kobylak9ea66a62021-03-24 17:54:14 +0000326 * @brief Map of supported PSU configurations that include the model name
327 * and their properties.
Brandon Wymanaed1f752019-11-25 18:10:52 -0600328 */
Adriana Kobylakd3a70d92021-06-04 16:24:45 +0000329 std::map<std::string, sys_properties> supportedConfigs;
Brandon Wymanaed1f752019-11-25 18:10:52 -0600330
331 /**
Brandon Wymana0f33ce2019-10-17 18:32:29 -0500332 * @brief The vector for power supplies.
333 */
Brandon Wymanaed1f752019-11-25 18:10:52 -0600334 std::vector<std::unique_ptr<PowerSupply>> psus;
Adriana Kobylakc0a07582021-10-13 15:52:25 +0000335
336 /**
337 * @brief The libgpiod object for setting the power supply config
338 */
339 std::unique_ptr<GPIOInterfaceBase> powerConfigGPIO = nullptr;
Adriana Kobylakc9b05732022-03-19 15:15:10 +0000340
341 /**
342 * @brief PowerSystemInputs object
343 */
344 PowerSystemInputs powerSystemInputs;
345
346 /**
Brandon Wymanc3324422022-03-24 20:30:57 +0000347 * @brief Implement the ObjectManager for PowerSystemInputs object.
348 *
349 * Implements the org.freedesktop.DBus.ObjectManager interface used to
350 * communicate updates to the PowerSystemInputs object on the
351 * /xyz/openbmc_project/power/power_supplies root D-Bus path.
Adriana Kobylakc9b05732022-03-19 15:15:10 +0000352 */
353 sdbusplus::server::manager_t objectManager;
Brandon Wymanc3324422022-03-24 20:30:57 +0000354
355 /**
356 * @brief Implement the ObjectManager for power supply input history.
357 *
358 * Implements the org.freedesktop.DBus.ObjectManager interface used to
359 * communicate updates to the Average and Maximum interface properties on
360 * the /org/open_power/sensors root D-Bus path.
361 */
362 sdbusplus::server::manager_t historyManager;
Brandon Wyman18a24d92022-04-19 22:48:34 +0000363
364 /**
365 * @brief GPIO to toggle to 'sync' power supply input history.
366 */
367 std::unique_ptr<GPIOInterfaceBase> syncHistoryGPIO = nullptr;
368
369 /**
370 * @brief Toggles the GPIO to sync power supply input history readings
371 *
372 * This GPIO is connected to all supplies. This will clear the
373 * previous readings out of the supplies and restart them both at the
374 * same time zero and at record ID 0. The supplies will return 0
375 * bytes of data for the input history command right after this until
376 * a new entry shows up.
377 *
378 * This will cause the code to delete all previous history data and
379 * start fresh.
380 */
381 void syncHistory();
Brandon Wyman2bac8602019-09-12 18:12:21 -0500382};
383
Brandon Wyman63ea78b2020-09-24 16:49:09 -0500384} // namespace phosphor::power::manager