blob: 1bdc9ab596651d63e1d809ef5d38fa9a6d3c17f8 [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 McCarney8acaf542021-03-30 10:38:37 -050032#include <thread>
Shawn McCarneye0c6a2d2020-05-01 11:37:08 -050033#include <tuple>
34#include <utility>
Matthew Barth250d0a92020-02-28 13:04:24 -060035#include <variant>
Matthew Barthf2bcf1f2020-01-29 14:42:47 -060036
Shawn McCarney84807b92020-04-30 18:40:03 -050037namespace phosphor::power::regulators
Matthew Barth29e9e382020-01-23 13:40:49 -060038{
39
Shawn McCarneye0c6a2d2020-05-01 11:37:08 -050040namespace fs = std::filesystem;
41
Shawn McCarney589c1812021-01-14 12:13:26 -060042constexpr auto busName = "xyz.openbmc_project.Power.Regulators";
43constexpr auto managerObjPath = "/xyz/openbmc_project/power/regulators/manager";
44constexpr auto compatibleIntf =
45 "xyz.openbmc_project.Configuration.IBMCompatibleSystem";
46constexpr auto compatibleNamesProp = "Names";
Shawn McCarney8acaf542021-03-30 10:38:37 -050047constexpr std::chrono::minutes maxTimeToWaitForCompatTypes{5};
Shawn McCarney589c1812021-01-14 12:13:26 -060048
49/**
50 * Default configuration file name. This is used when the system does not
51 * implement the D-Bus compatible interface.
52 */
53constexpr auto defaultConfigFileName = "config.json";
54
Shawn McCarneye0c6a2d2020-05-01 11:37:08 -050055/**
56 * Standard configuration file directory. This directory is part of the
57 * firmware install image. It contains the standard version of the config file.
58 */
59const fs::path standardConfigFileDir{"/usr/share/phosphor-regulators"};
60
61/**
62 * Test configuration file directory. This directory can contain a test version
63 * of the config file. The test version will override the standard version.
64 */
65const fs::path testConfigFileDir{"/etc/phosphor-regulators"};
66
Matthew Barthf2bcf1f2020-01-29 14:42:47 -060067Manager::Manager(sdbusplus::bus::bus& bus, const sdeventplus::Event& event) :
Shawn McCarney589c1812021-01-14 12:13:26 -060068 ManagerObject{bus, managerObjPath, true}, bus{bus}, eventLoop{event},
69 services{bus}
Matthew Barth29e9e382020-01-23 13:40:49 -060070{
Shawn McCarney589c1812021-01-14 12:13:26 -060071 // Subscribe to D-Bus interfacesAdded signal from Entity Manager. This
72 // notifies us if the compatible interface becomes available later.
73 std::string matchStr = sdbusplus::bus::match::rules::interfacesAdded() +
74 sdbusplus::bus::match::rules::sender(
75 "xyz.openbmc_project.EntityManager");
76 std::unique_ptr<sdbusplus::server::match::match> matchPtr =
77 std::make_unique<sdbusplus::server::match::match>(
78 bus, matchStr,
79 std::bind(&Manager::interfacesAddedHandler, this,
80 std::placeholders::_1));
81 signals.emplace_back(std::move(matchPtr));
Matthew Barth250d0a92020-02-28 13:04:24 -060082
Shawn McCarney589c1812021-01-14 12:13:26 -060083 // Try to find compatible system types using D-Bus compatible interface.
84 // Note that it might not be supported on this system, or the service that
85 // provides the interface might not be running yet.
86 findCompatibleSystemTypes();
Shawn McCarney84807b92020-04-30 18:40:03 -050087
Shawn McCarney589c1812021-01-14 12:13:26 -060088 // Try to find and load the JSON configuration file
89 loadConfigFile();
Matthew Barth29e9e382020-01-23 13:40:49 -060090
91 // Obtain dbus service name
92 bus.request_name(busName);
93}
94
95void Manager::configure()
96{
Shawn McCarney9bd94d32021-01-25 19:40:42 -060097 // Clear any cached data or error history related to hardware devices
98 clearHardwareData();
99
Shawn McCarney8acaf542021-03-30 10:38:37 -0500100 // Wait until the config file has been loaded or hit max wait time
101 waitUntilConfigFileLoaded();
102
103 // Verify config file has been loaded and System object is valid
104 if (isConfigFileLoaded())
Shawn McCarney6345c6c2020-05-04 10:35:05 -0500105 {
106 // Configure the regulator devices in the system
Bob King23243f82020-07-29 10:38:57 +0800107 system->configure(services);
Shawn McCarney6345c6c2020-05-04 10:35:05 -0500108 }
109 else
110 {
Shawn McCarney415094c2021-02-15 11:08:19 -0600111 // Write error message to journal
Shawn McCarneyb464c8b2020-07-16 17:51:38 -0500112 services.getJournal().logError("Unable to configure regulator devices: "
113 "Configuration file not loaded");
Shawn McCarney6345c6c2020-05-04 10:35:05 -0500114
Shawn McCarney415094c2021-02-15 11:08:19 -0600115 // Log critical error since regulators could not be configured. Could
116 // cause hardware damage if default regulator settings are very wrong.
117 services.getErrorLogging().logConfigFileError(Entry::Level::Critical,
118 services.getJournal());
119
120 // Throw InternalFailure to propogate error status to D-Bus client
121 throw sdbusplus::xyz::openbmc_project::Common::Error::InternalFailure{};
122 }
Matthew Barth29e9e382020-01-23 13:40:49 -0600123}
124
Shawn McCarney589c1812021-01-14 12:13:26 -0600125void Manager::interfacesAddedHandler(sdbusplus::message::message& msg)
126{
127 // Verify message is valid
128 if (!msg)
129 {
130 return;
131 }
132
133 try
134 {
135 // Read object path for object that was created or had interface added
136 sdbusplus::message::object_path objPath;
137 msg.read(objPath);
138
139 // Read the dictionary whose keys are interface names and whose values
140 // are dictionaries containing the interface property names and values
141 std::map<std::string,
142 std::map<std::string, std::variant<std::vector<std::string>>>>
143 intfProp;
144 msg.read(intfProp);
145
146 // Find the compatible interface, if present
147 auto itIntf = intfProp.find(compatibleIntf);
148 if (itIntf != intfProp.cend())
149 {
150 // Find the Names property of the compatible interface, if present
151 auto itProp = itIntf->second.find(compatibleNamesProp);
152 if (itProp != itIntf->second.cend())
153 {
154 // Get value of Names property
155 auto propValue = std::get<0>(itProp->second);
156 if (!propValue.empty())
157 {
158 // Store list of compatible system types
159 compatibleSystemTypes = propValue;
160
161 // Find and load JSON config file based on system types
162 loadConfigFile();
163 }
164 }
165 }
166 }
167 catch (const std::exception&)
168 {
169 // Error trying to read interfacesAdded message. One possible cause
170 // could be a property whose value is not a std::vector<std::string>.
171 }
172}
173
Shawn McCarney5b19ea52020-06-02 18:52:56 -0500174void Manager::monitor(bool enable)
Matthew Barth29e9e382020-01-23 13:40:49 -0600175{
176 if (enable)
177 {
Shawn McCarney5b19ea52020-06-02 18:52:56 -0500178 /* Temporarily comment out until monitoring is supported.
179 Timer timer(eventLoop, std::bind(&Manager::timerExpired, this));
180 // Set timer as a repeating 1sec timer
181 timer.restart(std::chrono::milliseconds(1000));
182 timers.emplace_back(std::move(timer));
183 */
Matthew Barth29e9e382020-01-23 13:40:49 -0600184 }
185 else
186 {
Shawn McCarney5b19ea52020-06-02 18:52:56 -0500187 /* Temporarily comment out until monitoring is supported.
188 // Delete all timers to disable monitoring
189 timers.clear();
190 */
191
Shawn McCarney8acaf542021-03-30 10:38:37 -0500192 // Verify config file has been loaded and System object is valid
193 if (isConfigFileLoaded())
Shawn McCarney5b19ea52020-06-02 18:52:56 -0500194 {
195 // Close the regulator devices in the system. Monitoring is
196 // normally disabled because the system is being powered off. The
197 // devices should be closed in case hardware is removed or replaced
Shawn McCarney9bd94d32021-01-25 19:40:42 -0600198 // while the system is powered off.
Bob Kingd692d6d2020-09-14 13:42:57 +0800199 system->closeDevices(services);
Shawn McCarney5b19ea52020-06-02 18:52:56 -0500200 }
Matthew Barth29e9e382020-01-23 13:40:49 -0600201 }
202}
203
Shawn McCarney589c1812021-01-14 12:13:26 -0600204void Manager::sighupHandler(sdeventplus::source::Signal& /*sigSrc*/,
205 const struct signalfd_siginfo* /*sigInfo*/)
206{
207 // Reload the JSON configuration file
208 loadConfigFile();
209}
210
Matthew Barthf2bcf1f2020-01-29 14:42:47 -0600211void Manager::timerExpired()
212{
213 // TODO Analyze, refresh sensor status, and
214 // collect/update telemetry for each regulator
215}
216
Shawn McCarney9bd94d32021-01-25 19:40:42 -0600217void Manager::clearHardwareData()
218{
Shawn McCarney4e0402c2021-02-05 00:08:33 -0600219 // Clear any cached hardware presence data and VPD values
Shawn McCarney9bd94d32021-01-25 19:40:42 -0600220 services.getPresenceService().clearCache();
Shawn McCarney4e0402c2021-02-05 00:08:33 -0600221 services.getVPD().clearCache();
Shawn McCarney9bd94d32021-01-25 19:40:42 -0600222
Shawn McCarney8acaf542021-03-30 10:38:37 -0500223 // Verify config file has been loaded and System object is valid
224 if (isConfigFileLoaded())
Shawn McCarney9bd94d32021-01-25 19:40:42 -0600225 {
226 // Clear any cached hardware data in the System object
227 system->clearCache();
228 }
229
230 // TODO: Clear error history related to hardware devices
231}
232
Shawn McCarney589c1812021-01-14 12:13:26 -0600233void Manager::findCompatibleSystemTypes()
Matthew Barth7cbc5532020-01-29 15:13:13 -0600234{
Matthew Barthbbc7c582020-02-03 15:55:15 -0600235 using namespace phosphor::power::util;
236
237 try
238 {
Shawn McCarney589c1812021-01-14 12:13:26 -0600239 // Query object mapper for object paths that implement the compatible
240 // interface. Returns a map of object paths to a map of services names
241 // to their interfaces.
242 DbusSubtree subTree = getSubTree(bus, "/xyz/openbmc_project/inventory",
243 compatibleIntf, 0);
244
245 // Get the first object path
246 auto objectIt = subTree.cbegin();
247 if (objectIt != subTree.cend())
Matthew Barthbbc7c582020-02-03 15:55:15 -0600248 {
Shawn McCarney589c1812021-01-14 12:13:26 -0600249 std::string objPath = objectIt->first;
250
251 // Get the first service name
252 auto serviceIt = objectIt->second.cbegin();
253 if (serviceIt != objectIt->second.cend())
254 {
255 std::string service = serviceIt->first;
256 if (!service.empty())
257 {
258 // Get compatible system types property value
259 getProperty(compatibleIntf, compatibleNamesProp, objPath,
260 service, bus, compatibleSystemTypes);
261 }
262 }
Matthew Barthbbc7c582020-02-03 15:55:15 -0600263 }
264 }
Shawn McCarney589c1812021-01-14 12:13:26 -0600265 catch (const std::exception&)
Matthew Barthbbc7c582020-02-03 15:55:15 -0600266 {
Shawn McCarney589c1812021-01-14 12:13:26 -0600267 // Compatible system types information is not available. The current
268 // system might not support the interface, or the service that
269 // implements the interface might not be running yet.
Matthew Barthbbc7c582020-02-03 15:55:15 -0600270 }
Matthew Barthbbc7c582020-02-03 15:55:15 -0600271}
272
Shawn McCarneye0c6a2d2020-05-01 11:37:08 -0500273fs::path Manager::findConfigFile()
274{
Shawn McCarney589c1812021-01-14 12:13:26 -0600275 // Build list of possible base file names
276 std::vector<std::string> fileNames{};
277
278 // Add possible file names based on compatible system types (if any)
279 for (const std::string& systemType : compatibleSystemTypes)
Shawn McCarneye0c6a2d2020-05-01 11:37:08 -0500280 {
Shawn McCarney589c1812021-01-14 12:13:26 -0600281 // Replace all spaces and commas in system type name with underscores
282 std::string fileName{systemType};
283 std::replace(fileName.begin(), fileName.end(), ' ', '_');
284 std::replace(fileName.begin(), fileName.end(), ',', '_');
285
286 // Append .json suffix and add to list
287 fileName.append(".json");
288 fileNames.emplace_back(fileName);
289 }
290
291 // Add default file name for systems that don't use compatible interface
292 fileNames.emplace_back(defaultConfigFileName);
293
294 // Look for a config file with one of the possible base names
295 for (const std::string& fileName : fileNames)
296 {
297 // Check if file exists in test directory
298 fs::path pathName{testConfigFileDir / fileName};
299 if (fs::exists(pathName))
Shawn McCarneye0c6a2d2020-05-01 11:37:08 -0500300 {
Shawn McCarney589c1812021-01-14 12:13:26 -0600301 return pathName;
302 }
303
304 // Check if file exists in standard directory
305 pathName = standardConfigFileDir / fileName;
306 if (fs::exists(pathName))
307 {
308 return pathName;
Shawn McCarneye0c6a2d2020-05-01 11:37:08 -0500309 }
310 }
311
Shawn McCarney589c1812021-01-14 12:13:26 -0600312 // No config file found; return empty path
313 return fs::path{};
Shawn McCarneye0c6a2d2020-05-01 11:37:08 -0500314}
315
316void Manager::loadConfigFile()
317{
318 try
319 {
320 // Find the absolute path to the config file
321 fs::path pathName = findConfigFile();
Shawn McCarney589c1812021-01-14 12:13:26 -0600322 if (!pathName.empty())
323 {
324 // Log info message in journal; config file path is important
325 services.getJournal().logInfo("Loading configuration file " +
326 pathName.string());
Shawn McCarneye0c6a2d2020-05-01 11:37:08 -0500327
Shawn McCarney589c1812021-01-14 12:13:26 -0600328 // Parse the config file
329 std::vector<std::unique_ptr<Rule>> rules{};
330 std::vector<std::unique_ptr<Chassis>> chassis{};
331 std::tie(rules, chassis) = config_file_parser::parse(pathName);
Shawn McCarneye0c6a2d2020-05-01 11:37:08 -0500332
Shawn McCarney589c1812021-01-14 12:13:26 -0600333 // Store config file information in a new System object. The old
334 // System object, if any, is automatically deleted.
335 system =
336 std::make_unique<System>(std::move(rules), std::move(chassis));
337 }
Shawn McCarneye0c6a2d2020-05-01 11:37:08 -0500338 }
339 catch (const std::exception& e)
340 {
341 // Log error messages in journal
Shawn McCarneyb464c8b2020-07-16 17:51:38 -0500342 services.getJournal().logError(exception_utils::getMessages(e));
343 services.getJournal().logError("Unable to load configuration file");
Shawn McCarneye0c6a2d2020-05-01 11:37:08 -0500344
Shawn McCarney415094c2021-02-15 11:08:19 -0600345 // Log error
346 services.getErrorLogging().logConfigFileError(Entry::Level::Error,
347 services.getJournal());
Shawn McCarneye0c6a2d2020-05-01 11:37:08 -0500348 }
349}
350
Shawn McCarney8acaf542021-03-30 10:38:37 -0500351void Manager::waitUntilConfigFileLoaded()
352{
353 // If config file not loaded and list of compatible system types is empty
354 if (!isConfigFileLoaded() && compatibleSystemTypes.empty())
355 {
356 // Loop until compatible system types found or waited max amount of time
357 auto start = std::chrono::system_clock::now();
358 std::chrono::system_clock::duration timeWaited{0};
359 while (compatibleSystemTypes.empty() &&
360 (timeWaited <= maxTimeToWaitForCompatTypes))
361 {
362 // Try to find list of compatible system types
363 findCompatibleSystemTypes();
364 if (!compatibleSystemTypes.empty())
365 {
366 // Compatible system types found; try to load config file
367 loadConfigFile();
368 }
369 else
370 {
371 // Sleep 5 seconds
372 using namespace std::chrono_literals;
373 std::this_thread::sleep_for(5s);
374 }
375 timeWaited = std::chrono::system_clock::now() - start;
376 }
377 }
378}
379
Shawn McCarney84807b92020-04-30 18:40:03 -0500380} // namespace phosphor::power::regulators