blob: a59b2fc7c81d17d8c090d9766fbdee7c6fba8903 [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;
Brandon Wymanaed1f752019-11-25 18:10:52 -060016};
17
Brandon Wymana0f33ce2019-10-17 18:32:29 -050018using namespace phosphor::power::psu;
19using namespace phosphor::logging;
20
Brandon Wyman63ea78b2020-09-24 16:49:09 -050021namespace phosphor::power::manager
Brandon Wyman2bac8602019-09-12 18:12:21 -050022{
23
24/**
25 * @class PSUManager
26 *
27 * This class will create an object used to manage and monitor a list of power
28 * supply devices.
29 */
30class PSUManager
31{
32 public:
33 PSUManager() = delete;
34 ~PSUManager() = default;
35 PSUManager(const PSUManager&) = delete;
36 PSUManager& operator=(const PSUManager&) = delete;
37 PSUManager(PSUManager&&) = delete;
38 PSUManager& operator=(PSUManager&&) = delete;
39
40 /**
Brandon Wyman510acaa2020-11-05 18:32:04 -060041 * Constructor to read configuration from D-Bus.
42 *
43 * @param[in] bus - D-Bus bus object
44 * @param[in] e - event object
45 */
46 PSUManager(sdbusplus::bus::bus& bus, const sdeventplus::Event& e);
47
48 /**
Brandon Wyman510acaa2020-11-05 18:32:04 -060049 * Get PSU properties from D-Bus, use that to build a power supply
50 * object.
51 *
52 * @param[in] properties - A map of property names and values
53 *
54 */
55 void getPSUProperties(util::DbusPropertyMap& properties);
56
57 /**
58 * Get PSU configuration from D-Bus
59 */
60 void getPSUConfiguration();
61
62 /**
Adriana Kobylak9bab9e12021-02-24 15:32:03 -060063 * @brief Initialize the system properties from the Supported Configuration
64 * D-Bus object provided by Entity Manager.
65 */
66 void getSystemProperties();
67
68 /**
Brandon Wyman2bac8602019-09-12 18:12:21 -050069 * Initializes the manager.
70 *
71 * Get current BMC state, ...
72 */
73 void initialize()
74 {
Brandon Wymana0f33ce2019-10-17 18:32:29 -050075 // When state = 1, system is powered on
76 int32_t state = 0;
77
78 try
79 {
80 // Use getProperty utility function to get power state.
81 util::getProperty<int32_t>(POWER_IFACE, "state", POWER_OBJ_PATH,
82 powerService, bus, state);
83
84 if (state)
85 {
86 powerOn = true;
87 }
88 else
89 {
90 powerOn = false;
91 }
92 }
93 catch (std::exception& e)
94 {
95 log<level::INFO>("Failed to get power state. Assuming it is off.");
96 powerOn = false;
97 }
98
Brandon Wyman59a35792020-06-04 12:37:40 -050099 onOffConfig(phosphor::pmbus::ON_OFF_CONFIG_CONTROL_PIN_ONLY);
Brandon Wymana0f33ce2019-10-17 18:32:29 -0500100 clearFaults();
101 updateInventory();
Brandon Wyman2bac8602019-09-12 18:12:21 -0500102 }
103
104 /**
105 * Starts the timer to start monitoring the list of devices.
106 */
107 int run()
108 {
Brandon Wyman2fe51862019-11-25 16:43:21 -0600109 return timer->get_event().loop();
Brandon Wyman2bac8602019-09-12 18:12:21 -0500110 }
111
112 /**
Brandon Wyman59a35792020-06-04 12:37:40 -0500113 * Write PMBus ON_OFF_CONFIG
114 *
115 * This function will be called to cause the PMBus device driver to send the
116 * ON_OFF_CONFIG command. Takes one byte of data.
117 */
118 void onOffConfig(const uint8_t data)
119 {
120 for (auto& psu : psus)
121 {
122 psu->onOffConfig(data);
123 }
124 }
125
126 /**
Brandon Wyman2bac8602019-09-12 18:12:21 -0500127 * This function will be called in various situations in order to clear
128 * any fault status bits that may have been set, in order to start over
129 * with a clean state. Presence changes and power state changes will want
130 * to clear any faults logged.
131 */
132 void clearFaults()
133 {
Brandon Wymana0f33ce2019-10-17 18:32:29 -0500134 for (auto& psu : psus)
135 {
Brandon Wymanaed1f752019-11-25 18:10:52 -0600136 psu->clearFaults();
Brandon Wymana0f33ce2019-10-17 18:32:29 -0500137 }
Brandon Wyman2bac8602019-09-12 18:12:21 -0500138 }
139
140 private:
141 /**
142 * The D-Bus object
143 */
144 sdbusplus::bus::bus& bus;
145
146 /**
147 * The timer that runs to periodically check the power supplies.
148 */
Brandon Wyman2fe51862019-11-25 16:43:21 -0600149 std::unique_ptr<
150 sdeventplus::utility::Timer<sdeventplus::ClockId::Monotonic>>
151 timer;
Brandon Wyman2bac8602019-09-12 18:12:21 -0500152
153 /**
Brandon Wymanb76ab242020-09-16 18:06:06 -0500154 * Create an error
155 *
156 * @param[in] faultName - 'name' message for the BMC error log entry
157 * @param[in] additionalData - The AdditionalData property for the error
158 */
159 void createError(const std::string& faultName,
160 const std::map<std::string, std::string>& additionalData);
161
162 /**
Brandon Wyman2bac8602019-09-12 18:12:21 -0500163 * Analyze the status of each of the power supplies.
Brandon Wymanb76ab242020-09-16 18:06:06 -0500164 *
165 * Log errors for faults, when and where appropriate.
Brandon Wyman2bac8602019-09-12 18:12:21 -0500166 */
Brandon Wyman63ea78b2020-09-24 16:49:09 -0500167 void analyze();
Brandon Wyman2bac8602019-09-12 18:12:21 -0500168
169 /** @brief True if the power is on. */
170 bool powerOn = false;
171
Brandon Wymana0f33ce2019-10-17 18:32:29 -0500172 /** @brief Used as part of subscribing to power on state changes*/
173 std::string powerService;
174
Brandon Wyman2bac8602019-09-12 18:12:21 -0500175 /** @brief Used to subscribe to D-Bus power on state changes */
176 std::unique_ptr<sdbusplus::bus::match_t> powerOnMatch;
177
Adriana Kobylak9bab9e12021-02-24 15:32:03 -0600178 /** @brief Used to subscribe to Entity Manager interfaces added */
179 std::unique_ptr<sdbusplus::bus::match_t> entityManagerIfacesAddedMatch;
180
Brandon Wyman2bac8602019-09-12 18:12:21 -0500181 /**
Brandon Wyman2bac8602019-09-12 18:12:21 -0500182 * @brief Callback for power state property changes
183 *
184 * Process changes to the powered on state property for the system.
185 *
186 * @param[in] msg - Data associated with the power state signal
187 */
188 void powerStateChanged(sdbusplus::message::message& msg);
189
190 /**
Brandon Wyman3e429132021-03-18 18:03:14 -0500191 * @brief Callback for entity-manager interface added
Adriana Kobylak9bab9e12021-02-24 15:32:03 -0600192 *
Brandon Wyman3e429132021-03-18 18:03:14 -0500193 * Process the information from the supported configuration and or IBM CFFPS
194 * Connector interface being added.
Adriana Kobylak9bab9e12021-02-24 15:32:03 -0600195 *
196 * @param[in] msg - Data associated with the interfaces added signal
197 */
Brandon Wyman3e429132021-03-18 18:03:14 -0500198 void entityManagerIfaceAdded(sdbusplus::message::message& msg);
Adriana Kobylak9bab9e12021-02-24 15:32:03 -0600199
200 /**
Brandon Wyman2bac8602019-09-12 18:12:21 -0500201 * @brief Adds properties to the inventory.
202 *
203 * Reads the values from the devices and writes them to the associated
204 * power supply D-Bus inventory objects.
205 *
206 * This needs to be done on startup, and each time the presence state
207 * changes.
208 */
Brandon Wymana0f33ce2019-10-17 18:32:29 -0500209 void updateInventory()
210 {
211 for (auto& psu : psus)
212 {
Brandon Wymanaed1f752019-11-25 18:10:52 -0600213 psu->updateInventory();
Brandon Wymana0f33ce2019-10-17 18:32:29 -0500214 }
215 }
216
217 /**
Adriana Kobylake1074d82021-03-16 20:46:44 +0000218 * @brief Helper function to populate the system properties
219 *
220 * @param[in] properties - A map of property names and values
221 */
222 void populateSysProperties(const util::DbusPropertyMap& properties);
223
224 /**
Adriana Kobylak9ea66a62021-03-24 17:54:14 +0000225 * @brief Map of supported PSU configurations that include the model name
226 * and their properties.
Brandon Wymanaed1f752019-11-25 18:10:52 -0600227 */
Adriana Kobylakd3a70d92021-06-04 16:24:45 +0000228 std::map<std::string, sys_properties> supportedConfigs;
Brandon Wymanaed1f752019-11-25 18:10:52 -0600229
230 /**
Brandon Wymana0f33ce2019-10-17 18:32:29 -0500231 * @brief The vector for power supplies.
232 */
Brandon Wymanaed1f752019-11-25 18:10:52 -0600233 std::vector<std::unique_ptr<PowerSupply>> psus;
Brandon Wyman2bac8602019-09-12 18:12:21 -0500234};
235
Brandon Wyman63ea78b2020-09-24 16:49:09 -0500236} // namespace phosphor::power::manager