blob: d4712121a539e7af6c221a216e35332c4510251f [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:
44 PowerSystemInputs(sdbusplus::bus::bus& bus, const std::string& path) :
45 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 */
71 PSUManager(sdbusplus::bus::bus& bus, const sdeventplus::Event& e);
72
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 */
98 void initialize()
99 {
Brandon Wymana0f33ce2019-10-17 18:32:29 -0500100 // When state = 1, system is powered on
101 int32_t state = 0;
102
103 try
104 {
105 // Use getProperty utility function to get power state.
106 util::getProperty<int32_t>(POWER_IFACE, "state", POWER_OBJ_PATH,
107 powerService, bus, state);
108
109 if (state)
110 {
111 powerOn = true;
Adriana Kobylaka4d38fa2021-10-05 19:57:47 +0000112 validationTimer->restartOnce(validationTimeout);
Brandon Wymana0f33ce2019-10-17 18:32:29 -0500113 }
114 else
115 {
116 powerOn = false;
Adriana Kobylak8f16fb52021-03-31 15:50:15 +0000117 runValidateConfig = true;
Brandon Wymana0f33ce2019-10-17 18:32:29 -0500118 }
119 }
Patrick Williamsc1d4de52021-10-06 12:45:57 -0500120 catch (const std::exception& e)
Brandon Wymana0f33ce2019-10-17 18:32:29 -0500121 {
122 log<level::INFO>("Failed to get power state. Assuming it is off.");
123 powerOn = false;
Adriana Kobylak8f16fb52021-03-31 15:50:15 +0000124 runValidateConfig = true;
Brandon Wymana0f33ce2019-10-17 18:32:29 -0500125 }
126
Brandon Wyman59a35792020-06-04 12:37:40 -0500127 onOffConfig(phosphor::pmbus::ON_OFF_CONFIG_CONTROL_PIN_ONLY);
Brandon Wymana0f33ce2019-10-17 18:32:29 -0500128 clearFaults();
Brandon Wyman64e97752022-06-03 23:50:13 +0000129 updateMissingPSUs();
Brandon Wymana0f33ce2019-10-17 18:32:29 -0500130 updateInventory();
Adriana Kobylakc0a07582021-10-13 15:52:25 +0000131 setPowerConfigGPIO();
Brandon Wyman2bac8602019-09-12 18:12:21 -0500132 }
133
134 /**
135 * Starts the timer to start monitoring the list of devices.
136 */
137 int run()
138 {
Brandon Wyman2fe51862019-11-25 16:43:21 -0600139 return timer->get_event().loop();
Brandon Wyman2bac8602019-09-12 18:12:21 -0500140 }
141
142 /**
Brandon Wyman59a35792020-06-04 12:37:40 -0500143 * Write PMBus ON_OFF_CONFIG
144 *
145 * This function will be called to cause the PMBus device driver to send the
146 * ON_OFF_CONFIG command. Takes one byte of data.
147 */
148 void onOffConfig(const uint8_t data)
149 {
150 for (auto& psu : psus)
151 {
152 psu->onOffConfig(data);
153 }
154 }
155
156 /**
Brandon Wyman2bac8602019-09-12 18:12:21 -0500157 * This function will be called in various situations in order to clear
158 * any fault status bits that may have been set, in order to start over
159 * with a clean state. Presence changes and power state changes will want
160 * to clear any faults logged.
161 */
162 void clearFaults()
163 {
Brandon Wyman10fc6e82022-02-08 20:51:22 +0000164 setPowerSupplyError("");
Brandon Wymana0f33ce2019-10-17 18:32:29 -0500165 for (auto& psu : psus)
166 {
Brandon Wymanaed1f752019-11-25 18:10:52 -0600167 psu->clearFaults();
Brandon Wymana0f33ce2019-10-17 18:32:29 -0500168 }
Brandon Wyman2bac8602019-09-12 18:12:21 -0500169 }
170
171 private:
172 /**
173 * The D-Bus object
174 */
175 sdbusplus::bus::bus& bus;
176
177 /**
178 * The timer that runs to periodically check the power supplies.
179 */
Brandon Wyman2fe51862019-11-25 16:43:21 -0600180 std::unique_ptr<
181 sdeventplus::utility::Timer<sdeventplus::ClockId::Monotonic>>
182 timer;
Brandon Wyman2bac8602019-09-12 18:12:21 -0500183
184 /**
Adriana Kobylaka4d38fa2021-10-05 19:57:47 +0000185 * The timer that performs power supply validation as the entity manager
186 * interfaces show up in d-bus.
187 */
188 std::unique_ptr<
189 sdeventplus::utility::Timer<sdeventplus::ClockId::Monotonic>>
190 validationTimer;
191
192 /**
Brandon Wyman10fc6e82022-02-08 20:51:22 +0000193 * Let power control/sequencer application know of PSU error(s).
194 *
195 * @param[in] psuErrorString - string for power supply error
196 */
197 void setPowerSupplyError(const std::string& psuErrorString);
198
199 /**
Brandon Wymanb76ab242020-09-16 18:06:06 -0500200 * Create an error
201 *
202 * @param[in] faultName - 'name' message for the BMC error log entry
Brandon Wyman8b662882021-10-08 17:31:51 +0000203 * @param[in,out] additionalData - The AdditionalData property for the error
Brandon Wymanb76ab242020-09-16 18:06:06 -0500204 */
205 void createError(const std::string& faultName,
Brandon Wyman8b662882021-10-08 17:31:51 +0000206 std::map<std::string, std::string>& additionalData);
Brandon Wymanb76ab242020-09-16 18:06:06 -0500207
208 /**
Brandon Wyman2bac8602019-09-12 18:12:21 -0500209 * Analyze the status of each of the power supplies.
Brandon Wymanb76ab242020-09-16 18:06:06 -0500210 *
211 * Log errors for faults, when and where appropriate.
Brandon Wyman2bac8602019-09-12 18:12:21 -0500212 */
Brandon Wyman63ea78b2020-09-24 16:49:09 -0500213 void analyze();
Brandon Wyman2bac8602019-09-12 18:12:21 -0500214
215 /** @brief True if the power is on. */
216 bool powerOn = false;
217
Adriana Kobylak2549d792022-01-26 20:51:30 +0000218 /** @brief True if an error for a brownout has already been logged. */
219 bool brownoutLogged = false;
220
Brandon Wymana0f33ce2019-10-17 18:32:29 -0500221 /** @brief Used as part of subscribing to power on state changes*/
222 std::string powerService;
223
Brandon Wyman2bac8602019-09-12 18:12:21 -0500224 /** @brief Used to subscribe to D-Bus power on state changes */
225 std::unique_ptr<sdbusplus::bus::match_t> powerOnMatch;
226
Adriana Kobylak9ba38232021-11-16 20:27:45 +0000227 /** @brief Used to subscribe to D-Bus power supply presence changes */
228 std::vector<std::unique_ptr<sdbusplus::bus::match_t>> presenceMatches;
229
Adriana Kobylak9bab9e12021-02-24 15:32:03 -0600230 /** @brief Used to subscribe to Entity Manager interfaces added */
231 std::unique_ptr<sdbusplus::bus::match_t> entityManagerIfacesAddedMatch;
232
Brandon Wyman2bac8602019-09-12 18:12:21 -0500233 /**
Brandon Wyman2bac8602019-09-12 18:12:21 -0500234 * @brief Callback for power state property changes
235 *
236 * Process changes to the powered on state property for the system.
237 *
238 * @param[in] msg - Data associated with the power state signal
239 */
240 void powerStateChanged(sdbusplus::message::message& msg);
241
242 /**
Adriana Kobylak9ba38232021-11-16 20:27:45 +0000243 * @brief Callback for inventory property changes
244 *
245 * Process change of the Present property for power supply.
246 *
247 * @param[in] msg - Data associated with the Present change signal
248 **/
249 void presenceChanged(sdbusplus::message::message& msg);
250
251 /**
Brandon Wyman3e429132021-03-18 18:03:14 -0500252 * @brief Callback for entity-manager interface added
Adriana Kobylak9bab9e12021-02-24 15:32:03 -0600253 *
Brandon Wyman3e429132021-03-18 18:03:14 -0500254 * Process the information from the supported configuration and or IBM CFFPS
255 * Connector interface being added.
Adriana Kobylak9bab9e12021-02-24 15:32:03 -0600256 *
257 * @param[in] msg - Data associated with the interfaces added signal
258 */
Brandon Wyman3e429132021-03-18 18:03:14 -0500259 void entityManagerIfaceAdded(sdbusplus::message::message& msg);
Adriana Kobylak9bab9e12021-02-24 15:32:03 -0600260
261 /**
Brandon Wyman2bac8602019-09-12 18:12:21 -0500262 * @brief Adds properties to the inventory.
263 *
264 * Reads the values from the devices and writes them to the associated
265 * power supply D-Bus inventory objects.
266 *
267 * This needs to be done on startup, and each time the presence state
268 * changes.
269 */
Brandon Wymana0f33ce2019-10-17 18:32:29 -0500270 void updateInventory()
271 {
272 for (auto& psu : psus)
273 {
Brandon Wymanaed1f752019-11-25 18:10:52 -0600274 psu->updateInventory();
Brandon Wymana0f33ce2019-10-17 18:32:29 -0500275 }
276 }
277
278 /**
Adriana Kobylake1074d82021-03-16 20:46:44 +0000279 * @brief Helper function to populate the system properties
280 *
281 * @param[in] properties - A map of property names and values
282 */
283 void populateSysProperties(const util::DbusPropertyMap& properties);
284
285 /**
Brandon Wyman64e97752022-06-03 23:50:13 +0000286 * @brief Update inventory for missing required power supplies
287 */
288 void updateMissingPSUs();
289
290 /**
Adriana Kobylak8f16fb52021-03-31 15:50:15 +0000291 * @brief Perform power supply configuration validation.
292 * @details Validates if the existing power supply properties are a
293 * supported configuration, and acts on its findings such as logging errors.
294 */
295 void validateConfig();
296
297 /**
298 * @brief Flag to indicate if the validateConfig() function should be run.
299 * Set to false once the configuration has been validated to avoid running
300 * multiple times due to interfaces added signal. Set to true during power
301 * off to trigger the validation on power on.
302 */
303 bool runValidateConfig = true;
304
305 /**
Adriana Kobylak4d9aaf92021-06-30 15:27:42 +0000306 * @brief Check that all PSUs have the same model name and that the system
307 * has the required number of PSUs present as specified in the Supported
308 * Configuration interface.
309 *
310 * @param[out] additionalData - Contains debug information on why the check
311 * might have failed. Can be used to fill in error logs.
312 * @return true if all the required PSUs are present, false otherwise.
313 */
314 bool hasRequiredPSUs(std::map<std::string, std::string>& additionalData);
315
316 /**
Adriana Kobylak523704d2021-09-21 15:55:41 +0000317 * @brief Helper function to validate that all PSUs have the same model name
318 *
319 * @param[out] model - The model name. Empty if there is a mismatch.
320 * @param[out] additionalData - If there is a mismatch, it contains debug
321 * information such as the mismatched model name.
322 * @return true if all the PSUs have the same model name, false otherwise.
323 */
324 bool validateModelName(std::string& model,
325 std::map<std::string, std::string>& additionalData);
326
327 /**
Adriana Kobylakc0a07582021-10-13 15:52:25 +0000328 * @brief Set the power-config-full-load GPIO depending on the EM full load
329 * property value.
330 */
331 void setPowerConfigGPIO();
332
333 /**
Adriana Kobylake5b1e082022-03-02 15:37:32 +0000334 * @brief Indicate that the system is in a brownout condition by creating an
335 * error log and setting the PowerSystemInputs status property to Fault.
336 *
337 * @param[in] additionalData - Contains debug information on the number of
338 * PSUs in fault state or not present.
339 */
340 void setBrownout(std::map<std::string, std::string>& additionalData);
341
342 /**
343 * @brief Indicate that the system is no longer in a brownout condition by
344 * setting the PowerSystemInputs status property to Good.
345 */
346 void clearBrownout();
347
348 /**
Adriana Kobylak9ea66a62021-03-24 17:54:14 +0000349 * @brief Map of supported PSU configurations that include the model name
350 * and their properties.
Brandon Wymanaed1f752019-11-25 18:10:52 -0600351 */
Adriana Kobylakd3a70d92021-06-04 16:24:45 +0000352 std::map<std::string, sys_properties> supportedConfigs;
Brandon Wymanaed1f752019-11-25 18:10:52 -0600353
354 /**
Brandon Wymana0f33ce2019-10-17 18:32:29 -0500355 * @brief The vector for power supplies.
356 */
Brandon Wymanaed1f752019-11-25 18:10:52 -0600357 std::vector<std::unique_ptr<PowerSupply>> psus;
Adriana Kobylakc0a07582021-10-13 15:52:25 +0000358
359 /**
360 * @brief The libgpiod object for setting the power supply config
361 */
362 std::unique_ptr<GPIOInterfaceBase> powerConfigGPIO = nullptr;
Adriana Kobylakc9b05732022-03-19 15:15:10 +0000363
364 /**
365 * @brief PowerSystemInputs object
366 */
367 PowerSystemInputs powerSystemInputs;
368
369 /**
Brandon Wymanc3324422022-03-24 20:30:57 +0000370 * @brief Implement the ObjectManager for PowerSystemInputs object.
371 *
372 * Implements the org.freedesktop.DBus.ObjectManager interface used to
373 * communicate updates to the PowerSystemInputs object on the
374 * /xyz/openbmc_project/power/power_supplies root D-Bus path.
Adriana Kobylakc9b05732022-03-19 15:15:10 +0000375 */
376 sdbusplus::server::manager_t objectManager;
Brandon Wymanc3324422022-03-24 20:30:57 +0000377
378 /**
379 * @brief Implement the ObjectManager for power supply input history.
380 *
381 * Implements the org.freedesktop.DBus.ObjectManager interface used to
382 * communicate updates to the Average and Maximum interface properties on
383 * the /org/open_power/sensors root D-Bus path.
384 */
385 sdbusplus::server::manager_t historyManager;
Brandon Wyman18a24d92022-04-19 22:48:34 +0000386
387 /**
388 * @brief GPIO to toggle to 'sync' power supply input history.
389 */
390 std::unique_ptr<GPIOInterfaceBase> syncHistoryGPIO = nullptr;
391
392 /**
393 * @brief Toggles the GPIO to sync power supply input history readings
394 *
395 * This GPIO is connected to all supplies. This will clear the
396 * previous readings out of the supplies and restart them both at the
397 * same time zero and at record ID 0. The supplies will return 0
398 * bytes of data for the input history command right after this until
399 * a new entry shows up.
400 *
401 * This will cause the code to delete all previous history data and
402 * start fresh.
403 */
404 void syncHistory();
Brandon Wyman2bac8602019-09-12 18:12:21 -0500405};
406
Brandon Wyman63ea78b2020-09-24 16:49:09 -0500407} // namespace phosphor::power::manager