blob: cfbb81172e0098eb8eac6d802e1348e9e8661929 [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>
Shawn McCarneyd9c8be52021-05-18 10:07:53 -050026#include <xyz/openbmc_project/State/Chassis/server.hpp>
Shawn McCarney415094c2021-02-15 11:08:19 -060027
Shawn McCarney589c1812021-01-14 12:13:26 -060028#include <algorithm>
Matthew Barthf2bcf1f2020-01-29 14:42:47 -060029#include <chrono>
Shawn McCarneye0c6a2d2020-05-01 11:37:08 -050030#include <exception>
Shawn McCarney589c1812021-01-14 12:13:26 -060031#include <functional>
32#include <map>
Shawn McCarney8acaf542021-03-30 10:38:37 -050033#include <thread>
Shawn McCarneye0c6a2d2020-05-01 11:37:08 -050034#include <tuple>
35#include <utility>
Matthew Barth250d0a92020-02-28 13:04:24 -060036#include <variant>
Matthew Barthf2bcf1f2020-01-29 14:42:47 -060037
Shawn McCarney84807b92020-04-30 18:40:03 -050038namespace phosphor::power::regulators
Matthew Barth29e9e382020-01-23 13:40:49 -060039{
40
Shawn McCarneye0c6a2d2020-05-01 11:37:08 -050041namespace fs = std::filesystem;
42
Shawn McCarney589c1812021-01-14 12:13:26 -060043constexpr auto busName = "xyz.openbmc_project.Power.Regulators";
44constexpr auto managerObjPath = "/xyz/openbmc_project/power/regulators/manager";
45constexpr auto compatibleIntf =
46 "xyz.openbmc_project.Configuration.IBMCompatibleSystem";
47constexpr auto compatibleNamesProp = "Names";
Shawn McCarneyd9c8be52021-05-18 10:07:53 -050048constexpr auto chassisStatePath = "/xyz/openbmc_project/state/chassis0";
49constexpr auto chassisStateIntf = "xyz.openbmc_project.State.Chassis";
50constexpr auto chassisStateProp = "CurrentPowerState";
Shawn McCarney8acaf542021-03-30 10:38:37 -050051constexpr std::chrono::minutes maxTimeToWaitForCompatTypes{5};
Shawn McCarney589c1812021-01-14 12:13:26 -060052
Shawn McCarneyd9c8be52021-05-18 10:07:53 -050053using PowerState =
54 sdbusplus::xyz::openbmc_project::State::server::Chassis::PowerState;
55
Shawn McCarney589c1812021-01-14 12:13:26 -060056/**
57 * Default configuration file name. This is used when the system does not
58 * implement the D-Bus compatible interface.
59 */
60constexpr auto defaultConfigFileName = "config.json";
61
Shawn McCarneye0c6a2d2020-05-01 11:37:08 -050062/**
63 * Standard configuration file directory. This directory is part of the
64 * firmware install image. It contains the standard version of the config file.
65 */
66const fs::path standardConfigFileDir{"/usr/share/phosphor-regulators"};
67
68/**
69 * Test configuration file directory. This directory can contain a test version
70 * of the config file. The test version will override the standard version.
71 */
72const fs::path testConfigFileDir{"/etc/phosphor-regulators"};
73
Patrick Williams7354ce62022-07-22 19:26:56 -050074Manager::Manager(sdbusplus::bus_t& bus, const sdeventplus::Event& event) :
Zev Weissd1307292022-04-19 17:13:28 -070075 ManagerObject{bus, managerObjPath}, bus{bus}, eventLoop{event},
Patrick Williams48781ae2023-05-10 07:50:50 -050076 services{bus},
77 phaseFaultTimer{event, std::bind(&Manager::phaseFaultTimerExpired, this)},
Shawn McCarneyc8cbeac2021-09-10 15:42:56 -050078 sensorTimer{event, std::bind(&Manager::sensorTimerExpired, this)}
Matthew Barth29e9e382020-01-23 13:40:49 -060079{
Shawn McCarney589c1812021-01-14 12:13:26 -060080 // Subscribe to D-Bus interfacesAdded signal from Entity Manager. This
81 // notifies us if the compatible interface becomes available later.
82 std::string matchStr = sdbusplus::bus::match::rules::interfacesAdded() +
83 sdbusplus::bus::match::rules::sender(
84 "xyz.openbmc_project.EntityManager");
Patrick Williamsa61c1aa2021-11-19 12:25:21 -060085 std::unique_ptr<sdbusplus::bus::match_t> matchPtr =
86 std::make_unique<sdbusplus::bus::match_t>(
Shawn McCarney589c1812021-01-14 12:13:26 -060087 bus, matchStr,
88 std::bind(&Manager::interfacesAddedHandler, this,
89 std::placeholders::_1));
90 signals.emplace_back(std::move(matchPtr));
Matthew Barth250d0a92020-02-28 13:04:24 -060091
Shawn McCarney589c1812021-01-14 12:13:26 -060092 // Try to find compatible system types using D-Bus compatible interface.
93 // Note that it might not be supported on this system, or the service that
94 // provides the interface might not be running yet.
95 findCompatibleSystemTypes();
Shawn McCarney84807b92020-04-30 18:40:03 -050096
Shawn McCarney589c1812021-01-14 12:13:26 -060097 // Try to find and load the JSON configuration file
98 loadConfigFile();
Matthew Barth29e9e382020-01-23 13:40:49 -060099
Shawn McCarneyd9c8be52021-05-18 10:07:53 -0500100 // Obtain D-Bus service name
Matthew Barth29e9e382020-01-23 13:40:49 -0600101 bus.request_name(busName);
Shawn McCarneyd9c8be52021-05-18 10:07:53 -0500102
103 // If system is already powered on, enable monitoring
104 if (isSystemPoweredOn())
105 {
106 monitor(true);
107 }
Matthew Barth29e9e382020-01-23 13:40:49 -0600108}
109
110void Manager::configure()
111{
Shawn McCarney9bd94d32021-01-25 19:40:42 -0600112 // Clear any cached data or error history related to hardware devices
113 clearHardwareData();
114
Shawn McCarney8acaf542021-03-30 10:38:37 -0500115 // Wait until the config file has been loaded or hit max wait time
116 waitUntilConfigFileLoaded();
117
118 // Verify config file has been loaded and System object is valid
119 if (isConfigFileLoaded())
Shawn McCarney6345c6c2020-05-04 10:35:05 -0500120 {
121 // Configure the regulator devices in the system
Bob King23243f82020-07-29 10:38:57 +0800122 system->configure(services);
Shawn McCarney6345c6c2020-05-04 10:35:05 -0500123 }
124 else
125 {
Shawn McCarney415094c2021-02-15 11:08:19 -0600126 // Write error message to journal
Shawn McCarneyb464c8b2020-07-16 17:51:38 -0500127 services.getJournal().logError("Unable to configure regulator devices: "
128 "Configuration file not loaded");
Shawn McCarney6345c6c2020-05-04 10:35:05 -0500129
Shawn McCarney415094c2021-02-15 11:08:19 -0600130 // Log critical error since regulators could not be configured. Could
131 // cause hardware damage if default regulator settings are very wrong.
132 services.getErrorLogging().logConfigFileError(Entry::Level::Critical,
133 services.getJournal());
134
135 // Throw InternalFailure to propogate error status to D-Bus client
136 throw sdbusplus::xyz::openbmc_project::Common::Error::InternalFailure{};
137 }
Matthew Barth29e9e382020-01-23 13:40:49 -0600138}
139
Patrick Williams7354ce62022-07-22 19:26:56 -0500140void Manager::interfacesAddedHandler(sdbusplus::message_t& msg)
Shawn McCarney589c1812021-01-14 12:13:26 -0600141{
142 // Verify message is valid
143 if (!msg)
144 {
145 return;
146 }
147
148 try
149 {
150 // Read object path for object that was created or had interface added
151 sdbusplus::message::object_path objPath;
152 msg.read(objPath);
153
154 // Read the dictionary whose keys are interface names and whose values
155 // are dictionaries containing the interface property names and values
156 std::map<std::string,
157 std::map<std::string, std::variant<std::vector<std::string>>>>
158 intfProp;
159 msg.read(intfProp);
160
161 // Find the compatible interface, if present
162 auto itIntf = intfProp.find(compatibleIntf);
163 if (itIntf != intfProp.cend())
164 {
165 // Find the Names property of the compatible interface, if present
166 auto itProp = itIntf->second.find(compatibleNamesProp);
167 if (itProp != itIntf->second.cend())
168 {
169 // Get value of Names property
170 auto propValue = std::get<0>(itProp->second);
171 if (!propValue.empty())
172 {
173 // Store list of compatible system types
174 compatibleSystemTypes = propValue;
175
176 // Find and load JSON config file based on system types
177 loadConfigFile();
178 }
179 }
180 }
181 }
182 catch (const std::exception&)
183 {
184 // Error trying to read interfacesAdded message. One possible cause
185 // could be a property whose value is not a std::vector<std::string>.
186 }
187}
188
Shawn McCarney5b19ea52020-06-02 18:52:56 -0500189void Manager::monitor(bool enable)
Matthew Barth29e9e382020-01-23 13:40:49 -0600190{
Shawn McCarneyd9c8be52021-05-18 10:07:53 -0500191 // Check whether already in the requested monitoring state
192 if (enable == isMonitoringEnabled)
Matthew Barth29e9e382020-01-23 13:40:49 -0600193 {
Shawn McCarneyd9c8be52021-05-18 10:07:53 -0500194 return;
195 }
196
197 isMonitoringEnabled = enable;
198 if (isMonitoringEnabled)
199 {
200 services.getJournal().logDebug("Monitoring enabled");
201
Shawn McCarneyc8cbeac2021-09-10 15:42:56 -0500202 // Restart phase fault detection timer with repeating 15 second interval
203 phaseFaultTimer.restart(std::chrono::seconds(15));
204
205 // Restart sensor monitoring timer with repeating 1 second interval
206 sensorTimer.restart(std::chrono::seconds(1));
Shawn McCarneyd9c8be52021-05-18 10:07:53 -0500207
208 // Enable sensors service; put all sensors in an active state
209 services.getSensors().enable();
Matthew Barth29e9e382020-01-23 13:40:49 -0600210 }
211 else
212 {
Shawn McCarneyd9c8be52021-05-18 10:07:53 -0500213 services.getJournal().logDebug("Monitoring disabled");
214
Shawn McCarneyc8cbeac2021-09-10 15:42:56 -0500215 // Disable timers
216 phaseFaultTimer.setEnabled(false);
217 sensorTimer.setEnabled(false);
Shawn McCarneyd9c8be52021-05-18 10:07:53 -0500218
219 // Disable sensors service; put all sensors in an inactive state
220 services.getSensors().disable();
Shawn McCarney5b19ea52020-06-02 18:52:56 -0500221
Shawn McCarney8acaf542021-03-30 10:38:37 -0500222 // Verify config file has been loaded and System object is valid
223 if (isConfigFileLoaded())
Shawn McCarney5b19ea52020-06-02 18:52:56 -0500224 {
225 // Close the regulator devices in the system. Monitoring is
226 // normally disabled because the system is being powered off. The
227 // devices should be closed in case hardware is removed or replaced
Shawn McCarney9bd94d32021-01-25 19:40:42 -0600228 // while the system is powered off.
Bob Kingd692d6d2020-09-14 13:42:57 +0800229 system->closeDevices(services);
Shawn McCarney5b19ea52020-06-02 18:52:56 -0500230 }
Matthew Barth29e9e382020-01-23 13:40:49 -0600231 }
232}
233
Shawn McCarneyc8cbeac2021-09-10 15:42:56 -0500234void Manager::phaseFaultTimerExpired()
Shawn McCarney589c1812021-01-14 12:13:26 -0600235{
Shawn McCarneyc8cbeac2021-09-10 15:42:56 -0500236 // Verify config file has been loaded and System object is valid
237 if (isConfigFileLoaded())
238 {
239 // Detect redundant phase faults in regulator devices in the system
240 system->detectPhaseFaults(services);
241 }
Shawn McCarney589c1812021-01-14 12:13:26 -0600242}
243
Shawn McCarneyc8cbeac2021-09-10 15:42:56 -0500244void Manager::sensorTimerExpired()
Matthew Barthf2bcf1f2020-01-29 14:42:47 -0600245{
Shawn McCarneyd9c8be52021-05-18 10:07:53 -0500246 // Notify sensors service that a sensor monitoring cycle is starting
247 services.getSensors().startCycle();
248
249 // Verify config file has been loaded and System object is valid
250 if (isConfigFileLoaded())
251 {
252 // Monitor sensors for the voltage rails in the system
253 system->monitorSensors(services);
254 }
255
256 // Notify sensors service that current sensor monitoring cycle has ended
257 services.getSensors().endCycle();
Matthew Barthf2bcf1f2020-01-29 14:42:47 -0600258}
259
Shawn McCarneyc8cbeac2021-09-10 15:42:56 -0500260void Manager::sighupHandler(sdeventplus::source::Signal& /*sigSrc*/,
261 const struct signalfd_siginfo* /*sigInfo*/)
262{
263 // Reload the JSON configuration file
264 loadConfigFile();
265}
266
Shawn McCarney9bd94d32021-01-25 19:40:42 -0600267void Manager::clearHardwareData()
268{
Shawn McCarney4e0402c2021-02-05 00:08:33 -0600269 // Clear any cached hardware presence data and VPD values
Shawn McCarney9bd94d32021-01-25 19:40:42 -0600270 services.getPresenceService().clearCache();
Shawn McCarney4e0402c2021-02-05 00:08:33 -0600271 services.getVPD().clearCache();
Shawn McCarney9bd94d32021-01-25 19:40:42 -0600272
Shawn McCarney8acaf542021-03-30 10:38:37 -0500273 // Verify config file has been loaded and System object is valid
274 if (isConfigFileLoaded())
Shawn McCarney9bd94d32021-01-25 19:40:42 -0600275 {
276 // Clear any cached hardware data in the System object
277 system->clearCache();
Shawn McCarney9bd94d32021-01-25 19:40:42 -0600278
Shawn McCarneyce540f32021-05-14 17:08:41 -0500279 // Clear error history related to hardware devices in the System object
280 system->clearErrorHistory();
281 }
Shawn McCarney9bd94d32021-01-25 19:40:42 -0600282}
283
Shawn McCarney589c1812021-01-14 12:13:26 -0600284void Manager::findCompatibleSystemTypes()
Matthew Barth7cbc5532020-01-29 15:13:13 -0600285{
Matthew Barthbbc7c582020-02-03 15:55:15 -0600286 using namespace phosphor::power::util;
287
288 try
289 {
Shawn McCarney589c1812021-01-14 12:13:26 -0600290 // Query object mapper for object paths that implement the compatible
291 // interface. Returns a map of object paths to a map of services names
292 // to their interfaces.
293 DbusSubtree subTree = getSubTree(bus, "/xyz/openbmc_project/inventory",
294 compatibleIntf, 0);
295
296 // Get the first object path
297 auto objectIt = subTree.cbegin();
298 if (objectIt != subTree.cend())
Matthew Barthbbc7c582020-02-03 15:55:15 -0600299 {
Shawn McCarney589c1812021-01-14 12:13:26 -0600300 std::string objPath = objectIt->first;
301
302 // Get the first service name
303 auto serviceIt = objectIt->second.cbegin();
304 if (serviceIt != objectIt->second.cend())
305 {
306 std::string service = serviceIt->first;
307 if (!service.empty())
308 {
309 // Get compatible system types property value
310 getProperty(compatibleIntf, compatibleNamesProp, objPath,
311 service, bus, compatibleSystemTypes);
312 }
313 }
Matthew Barthbbc7c582020-02-03 15:55:15 -0600314 }
315 }
Shawn McCarney589c1812021-01-14 12:13:26 -0600316 catch (const std::exception&)
Matthew Barthbbc7c582020-02-03 15:55:15 -0600317 {
Shawn McCarney589c1812021-01-14 12:13:26 -0600318 // Compatible system types information is not available. The current
319 // system might not support the interface, or the service that
320 // implements the interface might not be running yet.
Matthew Barthbbc7c582020-02-03 15:55:15 -0600321 }
Matthew Barthbbc7c582020-02-03 15:55:15 -0600322}
323
Shawn McCarneye0c6a2d2020-05-01 11:37:08 -0500324fs::path Manager::findConfigFile()
325{
Shawn McCarney589c1812021-01-14 12:13:26 -0600326 // Build list of possible base file names
327 std::vector<std::string> fileNames{};
328
329 // Add possible file names based on compatible system types (if any)
330 for (const std::string& systemType : compatibleSystemTypes)
Shawn McCarneye0c6a2d2020-05-01 11:37:08 -0500331 {
Shawn McCarney589c1812021-01-14 12:13:26 -0600332 // Replace all spaces and commas in system type name with underscores
333 std::string fileName{systemType};
334 std::replace(fileName.begin(), fileName.end(), ' ', '_');
335 std::replace(fileName.begin(), fileName.end(), ',', '_');
336
337 // Append .json suffix and add to list
338 fileName.append(".json");
339 fileNames.emplace_back(fileName);
340 }
341
342 // Add default file name for systems that don't use compatible interface
343 fileNames.emplace_back(defaultConfigFileName);
344
345 // Look for a config file with one of the possible base names
346 for (const std::string& fileName : fileNames)
347 {
348 // Check if file exists in test directory
349 fs::path pathName{testConfigFileDir / fileName};
350 if (fs::exists(pathName))
Shawn McCarneye0c6a2d2020-05-01 11:37:08 -0500351 {
Shawn McCarney589c1812021-01-14 12:13:26 -0600352 return pathName;
353 }
354
355 // Check if file exists in standard directory
356 pathName = standardConfigFileDir / fileName;
357 if (fs::exists(pathName))
358 {
359 return pathName;
Shawn McCarneye0c6a2d2020-05-01 11:37:08 -0500360 }
361 }
362
Shawn McCarney589c1812021-01-14 12:13:26 -0600363 // No config file found; return empty path
364 return fs::path{};
Shawn McCarneye0c6a2d2020-05-01 11:37:08 -0500365}
366
Shawn McCarneyd9c8be52021-05-18 10:07:53 -0500367bool Manager::isSystemPoweredOn()
368{
369 bool isOn{false};
370
371 try
372 {
373 // Get D-Bus property that contains the current power state for
374 // chassis0, which represents the entire system (all chassis)
375 using namespace phosphor::power::util;
376 auto service = getService(chassisStatePath, chassisStateIntf, bus);
377 if (!service.empty())
378 {
379 PowerState currentPowerState;
380 getProperty(chassisStateIntf, chassisStateProp, chassisStatePath,
381 service, bus, currentPowerState);
382 if (currentPowerState == PowerState::On)
383 {
384 isOn = true;
385 }
386 }
387 }
388 catch (const std::exception& e)
389 {
390 // Current power state might not be available yet. The regulators
391 // application can start before the power state is published on D-Bus.
392 }
393
394 return isOn;
395}
396
Shawn McCarneye0c6a2d2020-05-01 11:37:08 -0500397void Manager::loadConfigFile()
398{
399 try
400 {
401 // Find the absolute path to the config file
402 fs::path pathName = findConfigFile();
Shawn McCarney589c1812021-01-14 12:13:26 -0600403 if (!pathName.empty())
404 {
405 // Log info message in journal; config file path is important
406 services.getJournal().logInfo("Loading configuration file " +
407 pathName.string());
Shawn McCarneye0c6a2d2020-05-01 11:37:08 -0500408
Shawn McCarney589c1812021-01-14 12:13:26 -0600409 // Parse the config file
410 std::vector<std::unique_ptr<Rule>> rules{};
411 std::vector<std::unique_ptr<Chassis>> chassis{};
412 std::tie(rules, chassis) = config_file_parser::parse(pathName);
Shawn McCarneye0c6a2d2020-05-01 11:37:08 -0500413
Shawn McCarney589c1812021-01-14 12:13:26 -0600414 // Store config file information in a new System object. The old
415 // System object, if any, is automatically deleted.
Patrick Williams48781ae2023-05-10 07:50:50 -0500416 system = std::make_unique<System>(std::move(rules),
417 std::move(chassis));
Shawn McCarney589c1812021-01-14 12:13:26 -0600418 }
Shawn McCarneye0c6a2d2020-05-01 11:37:08 -0500419 }
420 catch (const std::exception& e)
421 {
422 // Log error messages in journal
Shawn McCarneyb464c8b2020-07-16 17:51:38 -0500423 services.getJournal().logError(exception_utils::getMessages(e));
424 services.getJournal().logError("Unable to load configuration file");
Shawn McCarneye0c6a2d2020-05-01 11:37:08 -0500425
Shawn McCarney415094c2021-02-15 11:08:19 -0600426 // Log error
427 services.getErrorLogging().logConfigFileError(Entry::Level::Error,
428 services.getJournal());
Shawn McCarneye0c6a2d2020-05-01 11:37:08 -0500429 }
430}
431
Shawn McCarney8acaf542021-03-30 10:38:37 -0500432void Manager::waitUntilConfigFileLoaded()
433{
434 // If config file not loaded and list of compatible system types is empty
435 if (!isConfigFileLoaded() && compatibleSystemTypes.empty())
436 {
437 // Loop until compatible system types found or waited max amount of time
438 auto start = std::chrono::system_clock::now();
439 std::chrono::system_clock::duration timeWaited{0};
440 while (compatibleSystemTypes.empty() &&
441 (timeWaited <= maxTimeToWaitForCompatTypes))
442 {
443 // Try to find list of compatible system types
444 findCompatibleSystemTypes();
445 if (!compatibleSystemTypes.empty())
446 {
447 // Compatible system types found; try to load config file
448 loadConfigFile();
449 }
450 else
451 {
452 // Sleep 5 seconds
453 using namespace std::chrono_literals;
454 std::this_thread::sleep_for(5s);
455 }
456 timeWaited = std::chrono::system_clock::now() - start;
457 }
458 }
459}
460
Shawn McCarney84807b92020-04-30 18:40:03 -0500461} // namespace phosphor::power::regulators