blob: 71b33697fcf46e70e815e489f448527e269aca82 [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
Matthew Barthf2bcf1f2020-01-29 14:42:47 -060074Manager::Manager(sdbusplus::bus::bus& bus, const sdeventplus::Event& event) :
Shawn McCarney589c1812021-01-14 12:13:26 -060075 ManagerObject{bus, managerObjPath, true}, bus{bus}, eventLoop{event},
Shawn McCarneyc8cbeac2021-09-10 15:42:56 -050076 services{bus}, phaseFaultTimer{event,
77 std::bind(&Manager::phaseFaultTimerExpired,
78 this)},
79 sensorTimer{event, std::bind(&Manager::sensorTimerExpired, this)}
Matthew Barth29e9e382020-01-23 13:40:49 -060080{
Shawn McCarney589c1812021-01-14 12:13:26 -060081 // Subscribe to D-Bus interfacesAdded signal from Entity Manager. This
82 // notifies us if the compatible interface becomes available later.
83 std::string matchStr = sdbusplus::bus::match::rules::interfacesAdded() +
84 sdbusplus::bus::match::rules::sender(
85 "xyz.openbmc_project.EntityManager");
Patrick Williamsa61c1aa2021-11-19 12:25:21 -060086 std::unique_ptr<sdbusplus::bus::match_t> matchPtr =
87 std::make_unique<sdbusplus::bus::match_t>(
Shawn McCarney589c1812021-01-14 12:13:26 -060088 bus, matchStr,
89 std::bind(&Manager::interfacesAddedHandler, this,
90 std::placeholders::_1));
91 signals.emplace_back(std::move(matchPtr));
Matthew Barth250d0a92020-02-28 13:04:24 -060092
Shawn McCarney589c1812021-01-14 12:13:26 -060093 // Try to find compatible system types using D-Bus compatible interface.
94 // Note that it might not be supported on this system, or the service that
95 // provides the interface might not be running yet.
96 findCompatibleSystemTypes();
Shawn McCarney84807b92020-04-30 18:40:03 -050097
Shawn McCarney589c1812021-01-14 12:13:26 -060098 // Try to find and load the JSON configuration file
99 loadConfigFile();
Matthew Barth29e9e382020-01-23 13:40:49 -0600100
Shawn McCarneyd9c8be52021-05-18 10:07:53 -0500101 // Obtain D-Bus service name
Matthew Barth29e9e382020-01-23 13:40:49 -0600102 bus.request_name(busName);
Shawn McCarneyd9c8be52021-05-18 10:07:53 -0500103
104 // If system is already powered on, enable monitoring
105 if (isSystemPoweredOn())
106 {
107 monitor(true);
108 }
Matthew Barth29e9e382020-01-23 13:40:49 -0600109}
110
111void Manager::configure()
112{
Shawn McCarney9bd94d32021-01-25 19:40:42 -0600113 // Clear any cached data or error history related to hardware devices
114 clearHardwareData();
115
Shawn McCarney8acaf542021-03-30 10:38:37 -0500116 // Wait until the config file has been loaded or hit max wait time
117 waitUntilConfigFileLoaded();
118
119 // Verify config file has been loaded and System object is valid
120 if (isConfigFileLoaded())
Shawn McCarney6345c6c2020-05-04 10:35:05 -0500121 {
122 // Configure the regulator devices in the system
Bob King23243f82020-07-29 10:38:57 +0800123 system->configure(services);
Shawn McCarney6345c6c2020-05-04 10:35:05 -0500124 }
125 else
126 {
Shawn McCarney415094c2021-02-15 11:08:19 -0600127 // Write error message to journal
Shawn McCarneyb464c8b2020-07-16 17:51:38 -0500128 services.getJournal().logError("Unable to configure regulator devices: "
129 "Configuration file not loaded");
Shawn McCarney6345c6c2020-05-04 10:35:05 -0500130
Shawn McCarney415094c2021-02-15 11:08:19 -0600131 // Log critical error since regulators could not be configured. Could
132 // cause hardware damage if default regulator settings are very wrong.
133 services.getErrorLogging().logConfigFileError(Entry::Level::Critical,
134 services.getJournal());
135
136 // Throw InternalFailure to propogate error status to D-Bus client
137 throw sdbusplus::xyz::openbmc_project::Common::Error::InternalFailure{};
138 }
Matthew Barth29e9e382020-01-23 13:40:49 -0600139}
140
Shawn McCarney589c1812021-01-14 12:13:26 -0600141void Manager::interfacesAddedHandler(sdbusplus::message::message& msg)
142{
143 // Verify message is valid
144 if (!msg)
145 {
146 return;
147 }
148
149 try
150 {
151 // Read object path for object that was created or had interface added
152 sdbusplus::message::object_path objPath;
153 msg.read(objPath);
154
155 // Read the dictionary whose keys are interface names and whose values
156 // are dictionaries containing the interface property names and values
157 std::map<std::string,
158 std::map<std::string, std::variant<std::vector<std::string>>>>
159 intfProp;
160 msg.read(intfProp);
161
162 // Find the compatible interface, if present
163 auto itIntf = intfProp.find(compatibleIntf);
164 if (itIntf != intfProp.cend())
165 {
166 // Find the Names property of the compatible interface, if present
167 auto itProp = itIntf->second.find(compatibleNamesProp);
168 if (itProp != itIntf->second.cend())
169 {
170 // Get value of Names property
171 auto propValue = std::get<0>(itProp->second);
172 if (!propValue.empty())
173 {
174 // Store list of compatible system types
175 compatibleSystemTypes = propValue;
176
177 // Find and load JSON config file based on system types
178 loadConfigFile();
179 }
180 }
181 }
182 }
183 catch (const std::exception&)
184 {
185 // Error trying to read interfacesAdded message. One possible cause
186 // could be a property whose value is not a std::vector<std::string>.
187 }
188}
189
Shawn McCarney5b19ea52020-06-02 18:52:56 -0500190void Manager::monitor(bool enable)
Matthew Barth29e9e382020-01-23 13:40:49 -0600191{
Shawn McCarneyd9c8be52021-05-18 10:07:53 -0500192 // Check whether already in the requested monitoring state
193 if (enable == isMonitoringEnabled)
Matthew Barth29e9e382020-01-23 13:40:49 -0600194 {
Shawn McCarneyd9c8be52021-05-18 10:07:53 -0500195 return;
196 }
197
198 isMonitoringEnabled = enable;
199 if (isMonitoringEnabled)
200 {
201 services.getJournal().logDebug("Monitoring enabled");
202
Shawn McCarneyc8cbeac2021-09-10 15:42:56 -0500203 // Restart phase fault detection timer with repeating 15 second interval
204 phaseFaultTimer.restart(std::chrono::seconds(15));
205
206 // Restart sensor monitoring timer with repeating 1 second interval
207 sensorTimer.restart(std::chrono::seconds(1));
Shawn McCarneyd9c8be52021-05-18 10:07:53 -0500208
209 // Enable sensors service; put all sensors in an active state
210 services.getSensors().enable();
Matthew Barth29e9e382020-01-23 13:40:49 -0600211 }
212 else
213 {
Shawn McCarneyd9c8be52021-05-18 10:07:53 -0500214 services.getJournal().logDebug("Monitoring disabled");
215
Shawn McCarneyc8cbeac2021-09-10 15:42:56 -0500216 // Disable timers
217 phaseFaultTimer.setEnabled(false);
218 sensorTimer.setEnabled(false);
Shawn McCarneyd9c8be52021-05-18 10:07:53 -0500219
220 // Disable sensors service; put all sensors in an inactive state
221 services.getSensors().disable();
Shawn McCarney5b19ea52020-06-02 18:52:56 -0500222
Shawn McCarney8acaf542021-03-30 10:38:37 -0500223 // Verify config file has been loaded and System object is valid
224 if (isConfigFileLoaded())
Shawn McCarney5b19ea52020-06-02 18:52:56 -0500225 {
226 // Close the regulator devices in the system. Monitoring is
227 // normally disabled because the system is being powered off. The
228 // devices should be closed in case hardware is removed or replaced
Shawn McCarney9bd94d32021-01-25 19:40:42 -0600229 // while the system is powered off.
Bob Kingd692d6d2020-09-14 13:42:57 +0800230 system->closeDevices(services);
Shawn McCarney5b19ea52020-06-02 18:52:56 -0500231 }
Matthew Barth29e9e382020-01-23 13:40:49 -0600232 }
233}
234
Shawn McCarneyc8cbeac2021-09-10 15:42:56 -0500235void Manager::phaseFaultTimerExpired()
Shawn McCarney589c1812021-01-14 12:13:26 -0600236{
Shawn McCarneyc8cbeac2021-09-10 15:42:56 -0500237 // Verify config file has been loaded and System object is valid
238 if (isConfigFileLoaded())
239 {
240 // Detect redundant phase faults in regulator devices in the system
241 system->detectPhaseFaults(services);
242 }
Shawn McCarney589c1812021-01-14 12:13:26 -0600243}
244
Shawn McCarneyc8cbeac2021-09-10 15:42:56 -0500245void Manager::sensorTimerExpired()
Matthew Barthf2bcf1f2020-01-29 14:42:47 -0600246{
Shawn McCarneyd9c8be52021-05-18 10:07:53 -0500247 // Notify sensors service that a sensor monitoring cycle is starting
248 services.getSensors().startCycle();
249
250 // Verify config file has been loaded and System object is valid
251 if (isConfigFileLoaded())
252 {
253 // Monitor sensors for the voltage rails in the system
254 system->monitorSensors(services);
255 }
256
257 // Notify sensors service that current sensor monitoring cycle has ended
258 services.getSensors().endCycle();
Matthew Barthf2bcf1f2020-01-29 14:42:47 -0600259}
260
Shawn McCarneyc8cbeac2021-09-10 15:42:56 -0500261void Manager::sighupHandler(sdeventplus::source::Signal& /*sigSrc*/,
262 const struct signalfd_siginfo* /*sigInfo*/)
263{
264 // Reload the JSON configuration file
265 loadConfigFile();
266}
267
Shawn McCarney9bd94d32021-01-25 19:40:42 -0600268void Manager::clearHardwareData()
269{
Shawn McCarney4e0402c2021-02-05 00:08:33 -0600270 // Clear any cached hardware presence data and VPD values
Shawn McCarney9bd94d32021-01-25 19:40:42 -0600271 services.getPresenceService().clearCache();
Shawn McCarney4e0402c2021-02-05 00:08:33 -0600272 services.getVPD().clearCache();
Shawn McCarney9bd94d32021-01-25 19:40:42 -0600273
Shawn McCarney8acaf542021-03-30 10:38:37 -0500274 // Verify config file has been loaded and System object is valid
275 if (isConfigFileLoaded())
Shawn McCarney9bd94d32021-01-25 19:40:42 -0600276 {
277 // Clear any cached hardware data in the System object
278 system->clearCache();
Shawn McCarney9bd94d32021-01-25 19:40:42 -0600279
Shawn McCarneyce540f32021-05-14 17:08:41 -0500280 // Clear error history related to hardware devices in the System object
281 system->clearErrorHistory();
282 }
Shawn McCarney9bd94d32021-01-25 19:40:42 -0600283}
284
Shawn McCarney589c1812021-01-14 12:13:26 -0600285void Manager::findCompatibleSystemTypes()
Matthew Barth7cbc5532020-01-29 15:13:13 -0600286{
Matthew Barthbbc7c582020-02-03 15:55:15 -0600287 using namespace phosphor::power::util;
288
289 try
290 {
Shawn McCarney589c1812021-01-14 12:13:26 -0600291 // Query object mapper for object paths that implement the compatible
292 // interface. Returns a map of object paths to a map of services names
293 // to their interfaces.
294 DbusSubtree subTree = getSubTree(bus, "/xyz/openbmc_project/inventory",
295 compatibleIntf, 0);
296
297 // Get the first object path
298 auto objectIt = subTree.cbegin();
299 if (objectIt != subTree.cend())
Matthew Barthbbc7c582020-02-03 15:55:15 -0600300 {
Shawn McCarney589c1812021-01-14 12:13:26 -0600301 std::string objPath = objectIt->first;
302
303 // Get the first service name
304 auto serviceIt = objectIt->second.cbegin();
305 if (serviceIt != objectIt->second.cend())
306 {
307 std::string service = serviceIt->first;
308 if (!service.empty())
309 {
310 // Get compatible system types property value
311 getProperty(compatibleIntf, compatibleNamesProp, objPath,
312 service, bus, compatibleSystemTypes);
313 }
314 }
Matthew Barthbbc7c582020-02-03 15:55:15 -0600315 }
316 }
Shawn McCarney589c1812021-01-14 12:13:26 -0600317 catch (const std::exception&)
Matthew Barthbbc7c582020-02-03 15:55:15 -0600318 {
Shawn McCarney589c1812021-01-14 12:13:26 -0600319 // Compatible system types information is not available. The current
320 // system might not support the interface, or the service that
321 // implements the interface might not be running yet.
Matthew Barthbbc7c582020-02-03 15:55:15 -0600322 }
Matthew Barthbbc7c582020-02-03 15:55:15 -0600323}
324
Shawn McCarneye0c6a2d2020-05-01 11:37:08 -0500325fs::path Manager::findConfigFile()
326{
Shawn McCarney589c1812021-01-14 12:13:26 -0600327 // Build list of possible base file names
328 std::vector<std::string> fileNames{};
329
330 // Add possible file names based on compatible system types (if any)
331 for (const std::string& systemType : compatibleSystemTypes)
Shawn McCarneye0c6a2d2020-05-01 11:37:08 -0500332 {
Shawn McCarney589c1812021-01-14 12:13:26 -0600333 // Replace all spaces and commas in system type name with underscores
334 std::string fileName{systemType};
335 std::replace(fileName.begin(), fileName.end(), ' ', '_');
336 std::replace(fileName.begin(), fileName.end(), ',', '_');
337
338 // Append .json suffix and add to list
339 fileName.append(".json");
340 fileNames.emplace_back(fileName);
341 }
342
343 // Add default file name for systems that don't use compatible interface
344 fileNames.emplace_back(defaultConfigFileName);
345
346 // Look for a config file with one of the possible base names
347 for (const std::string& fileName : fileNames)
348 {
349 // Check if file exists in test directory
350 fs::path pathName{testConfigFileDir / fileName};
351 if (fs::exists(pathName))
Shawn McCarneye0c6a2d2020-05-01 11:37:08 -0500352 {
Shawn McCarney589c1812021-01-14 12:13:26 -0600353 return pathName;
354 }
355
356 // Check if file exists in standard directory
357 pathName = standardConfigFileDir / fileName;
358 if (fs::exists(pathName))
359 {
360 return pathName;
Shawn McCarneye0c6a2d2020-05-01 11:37:08 -0500361 }
362 }
363
Shawn McCarney589c1812021-01-14 12:13:26 -0600364 // No config file found; return empty path
365 return fs::path{};
Shawn McCarneye0c6a2d2020-05-01 11:37:08 -0500366}
367
Shawn McCarneyd9c8be52021-05-18 10:07:53 -0500368bool Manager::isSystemPoweredOn()
369{
370 bool isOn{false};
371
372 try
373 {
374 // Get D-Bus property that contains the current power state for
375 // chassis0, which represents the entire system (all chassis)
376 using namespace phosphor::power::util;
377 auto service = getService(chassisStatePath, chassisStateIntf, bus);
378 if (!service.empty())
379 {
380 PowerState currentPowerState;
381 getProperty(chassisStateIntf, chassisStateProp, chassisStatePath,
382 service, bus, currentPowerState);
383 if (currentPowerState == PowerState::On)
384 {
385 isOn = true;
386 }
387 }
388 }
389 catch (const std::exception& e)
390 {
391 // Current power state might not be available yet. The regulators
392 // application can start before the power state is published on D-Bus.
393 }
394
395 return isOn;
396}
397
Shawn McCarneye0c6a2d2020-05-01 11:37:08 -0500398void Manager::loadConfigFile()
399{
400 try
401 {
402 // Find the absolute path to the config file
403 fs::path pathName = findConfigFile();
Shawn McCarney589c1812021-01-14 12:13:26 -0600404 if (!pathName.empty())
405 {
406 // Log info message in journal; config file path is important
407 services.getJournal().logInfo("Loading configuration file " +
408 pathName.string());
Shawn McCarneye0c6a2d2020-05-01 11:37:08 -0500409
Shawn McCarney589c1812021-01-14 12:13:26 -0600410 // Parse the config file
411 std::vector<std::unique_ptr<Rule>> rules{};
412 std::vector<std::unique_ptr<Chassis>> chassis{};
413 std::tie(rules, chassis) = config_file_parser::parse(pathName);
Shawn McCarneye0c6a2d2020-05-01 11:37:08 -0500414
Shawn McCarney589c1812021-01-14 12:13:26 -0600415 // Store config file information in a new System object. The old
416 // System object, if any, is automatically deleted.
417 system =
418 std::make_unique<System>(std::move(rules), std::move(chassis));
419 }
Shawn McCarneye0c6a2d2020-05-01 11:37:08 -0500420 }
421 catch (const std::exception& e)
422 {
423 // Log error messages in journal
Shawn McCarneyb464c8b2020-07-16 17:51:38 -0500424 services.getJournal().logError(exception_utils::getMessages(e));
425 services.getJournal().logError("Unable to load configuration file");
Shawn McCarneye0c6a2d2020-05-01 11:37:08 -0500426
Shawn McCarney415094c2021-02-15 11:08:19 -0600427 // Log error
428 services.getErrorLogging().logConfigFileError(Entry::Level::Error,
429 services.getJournal());
Shawn McCarneye0c6a2d2020-05-01 11:37:08 -0500430 }
431}
432
Shawn McCarney8acaf542021-03-30 10:38:37 -0500433void Manager::waitUntilConfigFileLoaded()
434{
435 // If config file not loaded and list of compatible system types is empty
436 if (!isConfigFileLoaded() && compatibleSystemTypes.empty())
437 {
438 // Loop until compatible system types found or waited max amount of time
439 auto start = std::chrono::system_clock::now();
440 std::chrono::system_clock::duration timeWaited{0};
441 while (compatibleSystemTypes.empty() &&
442 (timeWaited <= maxTimeToWaitForCompatTypes))
443 {
444 // Try to find list of compatible system types
445 findCompatibleSystemTypes();
446 if (!compatibleSystemTypes.empty())
447 {
448 // Compatible system types found; try to load config file
449 loadConfigFile();
450 }
451 else
452 {
453 // Sleep 5 seconds
454 using namespace std::chrono_literals;
455 std::this_thread::sleep_for(5s);
456 }
457 timeWaited = std::chrono::system_clock::now() - start;
458 }
459 }
460}
461
Shawn McCarney84807b92020-04-30 18:40:03 -0500462} // namespace phosphor::power::regulators