blob: 94f1431694b9079646bf2255384f56bf3c542c33 [file] [log] [blame]
Matthew Barth29e9e382020-01-23 13:40:49 -06001/**
2 * Copyright © 2020 IBM Corporation
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "manager.hpp"
18
Shawn McCarneye0c6a2d2020-05-01 11:37:08 -050019#include "chassis.hpp"
20#include "config_file_parser.hpp"
21#include "exception_utils.hpp"
Shawn McCarneye0c6a2d2020-05-01 11:37:08 -050022#include "rule.hpp"
Matthew Barthbbc7c582020-02-03 15:55:15 -060023#include "utility.hpp"
24
Shawn McCarney415094c2021-02-15 11:08:19 -060025#include <xyz/openbmc_project/Common/error.hpp>
26
Shawn McCarney589c1812021-01-14 12:13:26 -060027#include <algorithm>
Matthew Barthf2bcf1f2020-01-29 14:42:47 -060028#include <chrono>
Shawn McCarneye0c6a2d2020-05-01 11:37:08 -050029#include <exception>
Shawn McCarney589c1812021-01-14 12:13:26 -060030#include <functional>
31#include <map>
Shawn McCarneye0c6a2d2020-05-01 11:37:08 -050032#include <tuple>
33#include <utility>
Matthew Barth250d0a92020-02-28 13:04:24 -060034#include <variant>
Matthew Barthf2bcf1f2020-01-29 14:42:47 -060035
Shawn McCarney84807b92020-04-30 18:40:03 -050036namespace phosphor::power::regulators
Matthew Barth29e9e382020-01-23 13:40:49 -060037{
38
Shawn McCarneye0c6a2d2020-05-01 11:37:08 -050039namespace fs = std::filesystem;
40
Shawn McCarney589c1812021-01-14 12:13:26 -060041constexpr auto busName = "xyz.openbmc_project.Power.Regulators";
42constexpr auto managerObjPath = "/xyz/openbmc_project/power/regulators/manager";
43constexpr auto compatibleIntf =
44 "xyz.openbmc_project.Configuration.IBMCompatibleSystem";
45constexpr auto compatibleNamesProp = "Names";
46
47/**
48 * Default configuration file name. This is used when the system does not
49 * implement the D-Bus compatible interface.
50 */
51constexpr auto defaultConfigFileName = "config.json";
52
Shawn McCarneye0c6a2d2020-05-01 11:37:08 -050053/**
54 * Standard configuration file directory. This directory is part of the
55 * firmware install image. It contains the standard version of the config file.
56 */
57const fs::path standardConfigFileDir{"/usr/share/phosphor-regulators"};
58
59/**
60 * Test configuration file directory. This directory can contain a test version
61 * of the config file. The test version will override the standard version.
62 */
63const fs::path testConfigFileDir{"/etc/phosphor-regulators"};
64
Matthew Barthf2bcf1f2020-01-29 14:42:47 -060065Manager::Manager(sdbusplus::bus::bus& bus, const sdeventplus::Event& event) :
Shawn McCarney589c1812021-01-14 12:13:26 -060066 ManagerObject{bus, managerObjPath, true}, bus{bus}, eventLoop{event},
67 services{bus}
Matthew Barth29e9e382020-01-23 13:40:49 -060068{
Shawn McCarney589c1812021-01-14 12:13:26 -060069 // Subscribe to D-Bus interfacesAdded signal from Entity Manager. This
70 // notifies us if the compatible interface becomes available later.
71 std::string matchStr = sdbusplus::bus::match::rules::interfacesAdded() +
72 sdbusplus::bus::match::rules::sender(
73 "xyz.openbmc_project.EntityManager");
74 std::unique_ptr<sdbusplus::server::match::match> matchPtr =
75 std::make_unique<sdbusplus::server::match::match>(
76 bus, matchStr,
77 std::bind(&Manager::interfacesAddedHandler, this,
78 std::placeholders::_1));
79 signals.emplace_back(std::move(matchPtr));
Matthew Barth250d0a92020-02-28 13:04:24 -060080
Shawn McCarney589c1812021-01-14 12:13:26 -060081 // Try to find compatible system types using D-Bus compatible interface.
82 // Note that it might not be supported on this system, or the service that
83 // provides the interface might not be running yet.
84 findCompatibleSystemTypes();
Shawn McCarney84807b92020-04-30 18:40:03 -050085
Shawn McCarney589c1812021-01-14 12:13:26 -060086 // Try to find and load the JSON configuration file
87 loadConfigFile();
Matthew Barth29e9e382020-01-23 13:40:49 -060088
89 // Obtain dbus service name
90 bus.request_name(busName);
91}
92
93void Manager::configure()
94{
Shawn McCarney9bd94d32021-01-25 19:40:42 -060095 // Clear any cached data or error history related to hardware devices
96 clearHardwareData();
97
Shawn McCarney6345c6c2020-05-04 10:35:05 -050098 // Verify System object exists; this means config file has been loaded
99 if (system)
100 {
101 // Configure the regulator devices in the system
Bob King23243f82020-07-29 10:38:57 +0800102 system->configure(services);
Shawn McCarney6345c6c2020-05-04 10:35:05 -0500103 }
104 else
105 {
Shawn McCarney415094c2021-02-15 11:08:19 -0600106 // Write error message to journal
Shawn McCarneyb464c8b2020-07-16 17:51:38 -0500107 services.getJournal().logError("Unable to configure regulator devices: "
108 "Configuration file not loaded");
Shawn McCarney6345c6c2020-05-04 10:35:05 -0500109
Shawn McCarney415094c2021-02-15 11:08:19 -0600110 // Log critical error since regulators could not be configured. Could
111 // cause hardware damage if default regulator settings are very wrong.
112 services.getErrorLogging().logConfigFileError(Entry::Level::Critical,
113 services.getJournal());
114
115 // Throw InternalFailure to propogate error status to D-Bus client
116 throw sdbusplus::xyz::openbmc_project::Common::Error::InternalFailure{};
117 }
Matthew Barth29e9e382020-01-23 13:40:49 -0600118}
119
Shawn McCarney589c1812021-01-14 12:13:26 -0600120void Manager::interfacesAddedHandler(sdbusplus::message::message& msg)
121{
122 // Verify message is valid
123 if (!msg)
124 {
125 return;
126 }
127
128 try
129 {
130 // Read object path for object that was created or had interface added
131 sdbusplus::message::object_path objPath;
132 msg.read(objPath);
133
134 // Read the dictionary whose keys are interface names and whose values
135 // are dictionaries containing the interface property names and values
136 std::map<std::string,
137 std::map<std::string, std::variant<std::vector<std::string>>>>
138 intfProp;
139 msg.read(intfProp);
140
141 // Find the compatible interface, if present
142 auto itIntf = intfProp.find(compatibleIntf);
143 if (itIntf != intfProp.cend())
144 {
145 // Find the Names property of the compatible interface, if present
146 auto itProp = itIntf->second.find(compatibleNamesProp);
147 if (itProp != itIntf->second.cend())
148 {
149 // Get value of Names property
150 auto propValue = std::get<0>(itProp->second);
151 if (!propValue.empty())
152 {
153 // Store list of compatible system types
154 compatibleSystemTypes = propValue;
155
156 // Find and load JSON config file based on system types
157 loadConfigFile();
158 }
159 }
160 }
161 }
162 catch (const std::exception&)
163 {
164 // Error trying to read interfacesAdded message. One possible cause
165 // could be a property whose value is not a std::vector<std::string>.
166 }
167}
168
Shawn McCarney5b19ea52020-06-02 18:52:56 -0500169void Manager::monitor(bool enable)
Matthew Barth29e9e382020-01-23 13:40:49 -0600170{
171 if (enable)
172 {
Shawn McCarney5b19ea52020-06-02 18:52:56 -0500173 /* Temporarily comment out until monitoring is supported.
174 Timer timer(eventLoop, std::bind(&Manager::timerExpired, this));
175 // Set timer as a repeating 1sec timer
176 timer.restart(std::chrono::milliseconds(1000));
177 timers.emplace_back(std::move(timer));
178 */
Matthew Barth29e9e382020-01-23 13:40:49 -0600179 }
180 else
181 {
Shawn McCarney5b19ea52020-06-02 18:52:56 -0500182 /* Temporarily comment out until monitoring is supported.
183 // Delete all timers to disable monitoring
184 timers.clear();
185 */
186
187 // Verify System object exists; this means config file has been loaded
188 if (system)
189 {
190 // Close the regulator devices in the system. Monitoring is
191 // normally disabled because the system is being powered off. The
192 // devices should be closed in case hardware is removed or replaced
Shawn McCarney9bd94d32021-01-25 19:40:42 -0600193 // while the system is powered off.
Bob Kingd692d6d2020-09-14 13:42:57 +0800194 system->closeDevices(services);
Shawn McCarney5b19ea52020-06-02 18:52:56 -0500195 }
Matthew Barth29e9e382020-01-23 13:40:49 -0600196 }
197}
198
Shawn McCarney589c1812021-01-14 12:13:26 -0600199void Manager::sighupHandler(sdeventplus::source::Signal& /*sigSrc*/,
200 const struct signalfd_siginfo* /*sigInfo*/)
201{
202 // Reload the JSON configuration file
203 loadConfigFile();
204}
205
Matthew Barthf2bcf1f2020-01-29 14:42:47 -0600206void Manager::timerExpired()
207{
208 // TODO Analyze, refresh sensor status, and
209 // collect/update telemetry for each regulator
210}
211
Shawn McCarney9bd94d32021-01-25 19:40:42 -0600212void Manager::clearHardwareData()
213{
Shawn McCarney4e0402c2021-02-05 00:08:33 -0600214 // Clear any cached hardware presence data and VPD values
Shawn McCarney9bd94d32021-01-25 19:40:42 -0600215 services.getPresenceService().clearCache();
Shawn McCarney4e0402c2021-02-05 00:08:33 -0600216 services.getVPD().clearCache();
Shawn McCarney9bd94d32021-01-25 19:40:42 -0600217
218 // Verify System object exists; this means config file has been loaded
219 if (system)
220 {
221 // Clear any cached hardware data in the System object
222 system->clearCache();
223 }
224
225 // TODO: Clear error history related to hardware devices
226}
227
Shawn McCarney589c1812021-01-14 12:13:26 -0600228void Manager::findCompatibleSystemTypes()
Matthew Barth7cbc5532020-01-29 15:13:13 -0600229{
Matthew Barthbbc7c582020-02-03 15:55:15 -0600230 using namespace phosphor::power::util;
231
232 try
233 {
Shawn McCarney589c1812021-01-14 12:13:26 -0600234 // Query object mapper for object paths that implement the compatible
235 // interface. Returns a map of object paths to a map of services names
236 // to their interfaces.
237 DbusSubtree subTree = getSubTree(bus, "/xyz/openbmc_project/inventory",
238 compatibleIntf, 0);
239
240 // Get the first object path
241 auto objectIt = subTree.cbegin();
242 if (objectIt != subTree.cend())
Matthew Barthbbc7c582020-02-03 15:55:15 -0600243 {
Shawn McCarney589c1812021-01-14 12:13:26 -0600244 std::string objPath = objectIt->first;
245
246 // Get the first service name
247 auto serviceIt = objectIt->second.cbegin();
248 if (serviceIt != objectIt->second.cend())
249 {
250 std::string service = serviceIt->first;
251 if (!service.empty())
252 {
253 // Get compatible system types property value
254 getProperty(compatibleIntf, compatibleNamesProp, objPath,
255 service, bus, compatibleSystemTypes);
256 }
257 }
Matthew Barthbbc7c582020-02-03 15:55:15 -0600258 }
259 }
Shawn McCarney589c1812021-01-14 12:13:26 -0600260 catch (const std::exception&)
Matthew Barthbbc7c582020-02-03 15:55:15 -0600261 {
Shawn McCarney589c1812021-01-14 12:13:26 -0600262 // Compatible system types information is not available. The current
263 // system might not support the interface, or the service that
264 // implements the interface might not be running yet.
Matthew Barthbbc7c582020-02-03 15:55:15 -0600265 }
Matthew Barthbbc7c582020-02-03 15:55:15 -0600266}
267
Shawn McCarneye0c6a2d2020-05-01 11:37:08 -0500268fs::path Manager::findConfigFile()
269{
Shawn McCarney589c1812021-01-14 12:13:26 -0600270 // Build list of possible base file names
271 std::vector<std::string> fileNames{};
272
273 // Add possible file names based on compatible system types (if any)
274 for (const std::string& systemType : compatibleSystemTypes)
Shawn McCarneye0c6a2d2020-05-01 11:37:08 -0500275 {
Shawn McCarney589c1812021-01-14 12:13:26 -0600276 // Replace all spaces and commas in system type name with underscores
277 std::string fileName{systemType};
278 std::replace(fileName.begin(), fileName.end(), ' ', '_');
279 std::replace(fileName.begin(), fileName.end(), ',', '_');
280
281 // Append .json suffix and add to list
282 fileName.append(".json");
283 fileNames.emplace_back(fileName);
284 }
285
286 // Add default file name for systems that don't use compatible interface
287 fileNames.emplace_back(defaultConfigFileName);
288
289 // Look for a config file with one of the possible base names
290 for (const std::string& fileName : fileNames)
291 {
292 // Check if file exists in test directory
293 fs::path pathName{testConfigFileDir / fileName};
294 if (fs::exists(pathName))
Shawn McCarneye0c6a2d2020-05-01 11:37:08 -0500295 {
Shawn McCarney589c1812021-01-14 12:13:26 -0600296 return pathName;
297 }
298
299 // Check if file exists in standard directory
300 pathName = standardConfigFileDir / fileName;
301 if (fs::exists(pathName))
302 {
303 return pathName;
Shawn McCarneye0c6a2d2020-05-01 11:37:08 -0500304 }
305 }
306
Shawn McCarney589c1812021-01-14 12:13:26 -0600307 // No config file found; return empty path
308 return fs::path{};
Shawn McCarneye0c6a2d2020-05-01 11:37:08 -0500309}
310
311void Manager::loadConfigFile()
312{
313 try
314 {
315 // Find the absolute path to the config file
316 fs::path pathName = findConfigFile();
Shawn McCarney589c1812021-01-14 12:13:26 -0600317 if (!pathName.empty())
318 {
319 // Log info message in journal; config file path is important
320 services.getJournal().logInfo("Loading configuration file " +
321 pathName.string());
Shawn McCarneye0c6a2d2020-05-01 11:37:08 -0500322
Shawn McCarney589c1812021-01-14 12:13:26 -0600323 // Parse the config file
324 std::vector<std::unique_ptr<Rule>> rules{};
325 std::vector<std::unique_ptr<Chassis>> chassis{};
326 std::tie(rules, chassis) = config_file_parser::parse(pathName);
Shawn McCarneye0c6a2d2020-05-01 11:37:08 -0500327
Shawn McCarney589c1812021-01-14 12:13:26 -0600328 // Store config file information in a new System object. The old
329 // System object, if any, is automatically deleted.
330 system =
331 std::make_unique<System>(std::move(rules), std::move(chassis));
332 }
Shawn McCarneye0c6a2d2020-05-01 11:37:08 -0500333 }
334 catch (const std::exception& e)
335 {
336 // Log error messages in journal
Shawn McCarneyb464c8b2020-07-16 17:51:38 -0500337 services.getJournal().logError(exception_utils::getMessages(e));
338 services.getJournal().logError("Unable to load configuration file");
Shawn McCarneye0c6a2d2020-05-01 11:37:08 -0500339
Shawn McCarney415094c2021-02-15 11:08:19 -0600340 // Log error
341 services.getErrorLogging().logConfigFileError(Entry::Level::Error,
342 services.getJournal());
Shawn McCarneye0c6a2d2020-05-01 11:37:08 -0500343 }
344}
345
Shawn McCarney84807b92020-04-30 18:40:03 -0500346} // namespace phosphor::power::regulators