Matthew Barth | 29e9e38 | 2020-01-23 13:40:49 -0600 | [diff] [blame] | 1 | /** |
| 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 McCarney | e0c6a2d | 2020-05-01 11:37:08 -0500 | [diff] [blame] | 19 | #include "chassis.hpp" |
| 20 | #include "config_file_parser.hpp" |
| 21 | #include "exception_utils.hpp" |
Shawn McCarney | e0c6a2d | 2020-05-01 11:37:08 -0500 | [diff] [blame] | 22 | #include "rule.hpp" |
Matthew Barth | bbc7c58 | 2020-02-03 15:55:15 -0600 | [diff] [blame] | 23 | #include "utility.hpp" |
| 24 | |
Shawn McCarney | 589c181 | 2021-01-14 12:13:26 -0600 | [diff] [blame] | 25 | #include <algorithm> |
Matthew Barth | f2bcf1f | 2020-01-29 14:42:47 -0600 | [diff] [blame] | 26 | #include <chrono> |
Shawn McCarney | e0c6a2d | 2020-05-01 11:37:08 -0500 | [diff] [blame] | 27 | #include <exception> |
Shawn McCarney | 589c181 | 2021-01-14 12:13:26 -0600 | [diff] [blame] | 28 | #include <functional> |
| 29 | #include <map> |
Shawn McCarney | e0c6a2d | 2020-05-01 11:37:08 -0500 | [diff] [blame] | 30 | #include <tuple> |
| 31 | #include <utility> |
Matthew Barth | 250d0a9 | 2020-02-28 13:04:24 -0600 | [diff] [blame] | 32 | #include <variant> |
Matthew Barth | f2bcf1f | 2020-01-29 14:42:47 -0600 | [diff] [blame] | 33 | |
Shawn McCarney | 84807b9 | 2020-04-30 18:40:03 -0500 | [diff] [blame] | 34 | namespace phosphor::power::regulators |
Matthew Barth | 29e9e38 | 2020-01-23 13:40:49 -0600 | [diff] [blame] | 35 | { |
| 36 | |
Shawn McCarney | e0c6a2d | 2020-05-01 11:37:08 -0500 | [diff] [blame] | 37 | namespace fs = std::filesystem; |
| 38 | |
Shawn McCarney | 589c181 | 2021-01-14 12:13:26 -0600 | [diff] [blame] | 39 | constexpr auto busName = "xyz.openbmc_project.Power.Regulators"; |
| 40 | constexpr auto managerObjPath = "/xyz/openbmc_project/power/regulators/manager"; |
| 41 | constexpr auto compatibleIntf = |
| 42 | "xyz.openbmc_project.Configuration.IBMCompatibleSystem"; |
| 43 | constexpr auto compatibleNamesProp = "Names"; |
| 44 | |
| 45 | /** |
| 46 | * Default configuration file name. This is used when the system does not |
| 47 | * implement the D-Bus compatible interface. |
| 48 | */ |
| 49 | constexpr auto defaultConfigFileName = "config.json"; |
| 50 | |
Shawn McCarney | e0c6a2d | 2020-05-01 11:37:08 -0500 | [diff] [blame] | 51 | /** |
| 52 | * Standard configuration file directory. This directory is part of the |
| 53 | * firmware install image. It contains the standard version of the config file. |
| 54 | */ |
| 55 | const fs::path standardConfigFileDir{"/usr/share/phosphor-regulators"}; |
| 56 | |
| 57 | /** |
| 58 | * Test configuration file directory. This directory can contain a test version |
| 59 | * of the config file. The test version will override the standard version. |
| 60 | */ |
| 61 | const fs::path testConfigFileDir{"/etc/phosphor-regulators"}; |
| 62 | |
Matthew Barth | f2bcf1f | 2020-01-29 14:42:47 -0600 | [diff] [blame] | 63 | Manager::Manager(sdbusplus::bus::bus& bus, const sdeventplus::Event& event) : |
Shawn McCarney | 589c181 | 2021-01-14 12:13:26 -0600 | [diff] [blame] | 64 | ManagerObject{bus, managerObjPath, true}, bus{bus}, eventLoop{event}, |
| 65 | services{bus} |
Matthew Barth | 29e9e38 | 2020-01-23 13:40:49 -0600 | [diff] [blame] | 66 | { |
Shawn McCarney | 589c181 | 2021-01-14 12:13:26 -0600 | [diff] [blame] | 67 | // Subscribe to D-Bus interfacesAdded signal from Entity Manager. This |
| 68 | // notifies us if the compatible interface becomes available later. |
| 69 | std::string matchStr = sdbusplus::bus::match::rules::interfacesAdded() + |
| 70 | sdbusplus::bus::match::rules::sender( |
| 71 | "xyz.openbmc_project.EntityManager"); |
| 72 | std::unique_ptr<sdbusplus::server::match::match> matchPtr = |
| 73 | std::make_unique<sdbusplus::server::match::match>( |
| 74 | bus, matchStr, |
| 75 | std::bind(&Manager::interfacesAddedHandler, this, |
| 76 | std::placeholders::_1)); |
| 77 | signals.emplace_back(std::move(matchPtr)); |
Matthew Barth | 250d0a9 | 2020-02-28 13:04:24 -0600 | [diff] [blame] | 78 | |
Shawn McCarney | 589c181 | 2021-01-14 12:13:26 -0600 | [diff] [blame] | 79 | // Try to find compatible system types using D-Bus compatible interface. |
| 80 | // Note that it might not be supported on this system, or the service that |
| 81 | // provides the interface might not be running yet. |
| 82 | findCompatibleSystemTypes(); |
Shawn McCarney | 84807b9 | 2020-04-30 18:40:03 -0500 | [diff] [blame] | 83 | |
Shawn McCarney | 589c181 | 2021-01-14 12:13:26 -0600 | [diff] [blame] | 84 | // Try to find and load the JSON configuration file |
| 85 | loadConfigFile(); |
Matthew Barth | 29e9e38 | 2020-01-23 13:40:49 -0600 | [diff] [blame] | 86 | |
| 87 | // Obtain dbus service name |
| 88 | bus.request_name(busName); |
| 89 | } |
| 90 | |
| 91 | void Manager::configure() |
| 92 | { |
Shawn McCarney | 9bd94d3 | 2021-01-25 19:40:42 -0600 | [diff] [blame^] | 93 | // Clear any cached data or error history related to hardware devices |
| 94 | clearHardwareData(); |
| 95 | |
Shawn McCarney | 6345c6c | 2020-05-04 10:35:05 -0500 | [diff] [blame] | 96 | // Verify System object exists; this means config file has been loaded |
| 97 | if (system) |
| 98 | { |
| 99 | // Configure the regulator devices in the system |
Bob King | 23243f8 | 2020-07-29 10:38:57 +0800 | [diff] [blame] | 100 | system->configure(services); |
Shawn McCarney | 6345c6c | 2020-05-04 10:35:05 -0500 | [diff] [blame] | 101 | } |
| 102 | else |
| 103 | { |
Shawn McCarney | b464c8b | 2020-07-16 17:51:38 -0500 | [diff] [blame] | 104 | services.getJournal().logError("Unable to configure regulator devices: " |
| 105 | "Configuration file not loaded"); |
Shawn McCarney | 6345c6c | 2020-05-04 10:35:05 -0500 | [diff] [blame] | 106 | // TODO: Log error |
| 107 | } |
| 108 | |
Matthew Barth | 29e9e38 | 2020-01-23 13:40:49 -0600 | [diff] [blame] | 109 | // TODO Configuration errors that should halt poweron, |
| 110 | // throw InternalFailure exception (or similar) to |
| 111 | // fail the call(busctl) to this method |
| 112 | } |
| 113 | |
Shawn McCarney | 589c181 | 2021-01-14 12:13:26 -0600 | [diff] [blame] | 114 | void Manager::interfacesAddedHandler(sdbusplus::message::message& msg) |
| 115 | { |
| 116 | // Verify message is valid |
| 117 | if (!msg) |
| 118 | { |
| 119 | return; |
| 120 | } |
| 121 | |
| 122 | try |
| 123 | { |
| 124 | // Read object path for object that was created or had interface added |
| 125 | sdbusplus::message::object_path objPath; |
| 126 | msg.read(objPath); |
| 127 | |
| 128 | // Read the dictionary whose keys are interface names and whose values |
| 129 | // are dictionaries containing the interface property names and values |
| 130 | std::map<std::string, |
| 131 | std::map<std::string, std::variant<std::vector<std::string>>>> |
| 132 | intfProp; |
| 133 | msg.read(intfProp); |
| 134 | |
| 135 | // Find the compatible interface, if present |
| 136 | auto itIntf = intfProp.find(compatibleIntf); |
| 137 | if (itIntf != intfProp.cend()) |
| 138 | { |
| 139 | // Find the Names property of the compatible interface, if present |
| 140 | auto itProp = itIntf->second.find(compatibleNamesProp); |
| 141 | if (itProp != itIntf->second.cend()) |
| 142 | { |
| 143 | // Get value of Names property |
| 144 | auto propValue = std::get<0>(itProp->second); |
| 145 | if (!propValue.empty()) |
| 146 | { |
| 147 | // Store list of compatible system types |
| 148 | compatibleSystemTypes = propValue; |
| 149 | |
| 150 | // Find and load JSON config file based on system types |
| 151 | loadConfigFile(); |
| 152 | } |
| 153 | } |
| 154 | } |
| 155 | } |
| 156 | catch (const std::exception&) |
| 157 | { |
| 158 | // Error trying to read interfacesAdded message. One possible cause |
| 159 | // could be a property whose value is not a std::vector<std::string>. |
| 160 | } |
| 161 | } |
| 162 | |
Shawn McCarney | 5b19ea5 | 2020-06-02 18:52:56 -0500 | [diff] [blame] | 163 | void Manager::monitor(bool enable) |
Matthew Barth | 29e9e38 | 2020-01-23 13:40:49 -0600 | [diff] [blame] | 164 | { |
| 165 | if (enable) |
| 166 | { |
Shawn McCarney | 5b19ea5 | 2020-06-02 18:52:56 -0500 | [diff] [blame] | 167 | /* Temporarily comment out until monitoring is supported. |
| 168 | Timer timer(eventLoop, std::bind(&Manager::timerExpired, this)); |
| 169 | // Set timer as a repeating 1sec timer |
| 170 | timer.restart(std::chrono::milliseconds(1000)); |
| 171 | timers.emplace_back(std::move(timer)); |
| 172 | */ |
Matthew Barth | 29e9e38 | 2020-01-23 13:40:49 -0600 | [diff] [blame] | 173 | } |
| 174 | else |
| 175 | { |
Shawn McCarney | 5b19ea5 | 2020-06-02 18:52:56 -0500 | [diff] [blame] | 176 | /* Temporarily comment out until monitoring is supported. |
| 177 | // Delete all timers to disable monitoring |
| 178 | timers.clear(); |
| 179 | */ |
| 180 | |
| 181 | // Verify System object exists; this means config file has been loaded |
| 182 | if (system) |
| 183 | { |
| 184 | // Close the regulator devices in the system. Monitoring is |
| 185 | // normally disabled because the system is being powered off. The |
| 186 | // devices should be closed in case hardware is removed or replaced |
Shawn McCarney | 9bd94d3 | 2021-01-25 19:40:42 -0600 | [diff] [blame^] | 187 | // while the system is powered off. |
Bob King | d692d6d | 2020-09-14 13:42:57 +0800 | [diff] [blame] | 188 | system->closeDevices(services); |
Shawn McCarney | 5b19ea5 | 2020-06-02 18:52:56 -0500 | [diff] [blame] | 189 | } |
Matthew Barth | 29e9e38 | 2020-01-23 13:40:49 -0600 | [diff] [blame] | 190 | } |
| 191 | } |
| 192 | |
Shawn McCarney | 589c181 | 2021-01-14 12:13:26 -0600 | [diff] [blame] | 193 | void Manager::sighupHandler(sdeventplus::source::Signal& /*sigSrc*/, |
| 194 | const struct signalfd_siginfo* /*sigInfo*/) |
| 195 | { |
| 196 | // Reload the JSON configuration file |
| 197 | loadConfigFile(); |
| 198 | } |
| 199 | |
Matthew Barth | f2bcf1f | 2020-01-29 14:42:47 -0600 | [diff] [blame] | 200 | void Manager::timerExpired() |
| 201 | { |
| 202 | // TODO Analyze, refresh sensor status, and |
| 203 | // collect/update telemetry for each regulator |
| 204 | } |
| 205 | |
Shawn McCarney | 9bd94d3 | 2021-01-25 19:40:42 -0600 | [diff] [blame^] | 206 | void Manager::clearHardwareData() |
| 207 | { |
| 208 | // Clear any cached hardware presence data |
| 209 | services.getPresenceService().clearCache(); |
| 210 | |
| 211 | // Verify System object exists; this means config file has been loaded |
| 212 | if (system) |
| 213 | { |
| 214 | // Clear any cached hardware data in the System object |
| 215 | system->clearCache(); |
| 216 | } |
| 217 | |
| 218 | // TODO: Clear error history related to hardware devices |
| 219 | } |
| 220 | |
Shawn McCarney | 589c181 | 2021-01-14 12:13:26 -0600 | [diff] [blame] | 221 | void Manager::findCompatibleSystemTypes() |
Matthew Barth | 7cbc553 | 2020-01-29 15:13:13 -0600 | [diff] [blame] | 222 | { |
Matthew Barth | bbc7c58 | 2020-02-03 15:55:15 -0600 | [diff] [blame] | 223 | using namespace phosphor::power::util; |
| 224 | |
| 225 | try |
| 226 | { |
Shawn McCarney | 589c181 | 2021-01-14 12:13:26 -0600 | [diff] [blame] | 227 | // Query object mapper for object paths that implement the compatible |
| 228 | // interface. Returns a map of object paths to a map of services names |
| 229 | // to their interfaces. |
| 230 | DbusSubtree subTree = getSubTree(bus, "/xyz/openbmc_project/inventory", |
| 231 | compatibleIntf, 0); |
| 232 | |
| 233 | // Get the first object path |
| 234 | auto objectIt = subTree.cbegin(); |
| 235 | if (objectIt != subTree.cend()) |
Matthew Barth | bbc7c58 | 2020-02-03 15:55:15 -0600 | [diff] [blame] | 236 | { |
Shawn McCarney | 589c181 | 2021-01-14 12:13:26 -0600 | [diff] [blame] | 237 | std::string objPath = objectIt->first; |
| 238 | |
| 239 | // Get the first service name |
| 240 | auto serviceIt = objectIt->second.cbegin(); |
| 241 | if (serviceIt != objectIt->second.cend()) |
| 242 | { |
| 243 | std::string service = serviceIt->first; |
| 244 | if (!service.empty()) |
| 245 | { |
| 246 | // Get compatible system types property value |
| 247 | getProperty(compatibleIntf, compatibleNamesProp, objPath, |
| 248 | service, bus, compatibleSystemTypes); |
| 249 | } |
| 250 | } |
Matthew Barth | bbc7c58 | 2020-02-03 15:55:15 -0600 | [diff] [blame] | 251 | } |
| 252 | } |
Shawn McCarney | 589c181 | 2021-01-14 12:13:26 -0600 | [diff] [blame] | 253 | catch (const std::exception&) |
Matthew Barth | bbc7c58 | 2020-02-03 15:55:15 -0600 | [diff] [blame] | 254 | { |
Shawn McCarney | 589c181 | 2021-01-14 12:13:26 -0600 | [diff] [blame] | 255 | // Compatible system types information is not available. The current |
| 256 | // system might not support the interface, or the service that |
| 257 | // implements the interface might not be running yet. |
Matthew Barth | bbc7c58 | 2020-02-03 15:55:15 -0600 | [diff] [blame] | 258 | } |
Matthew Barth | bbc7c58 | 2020-02-03 15:55:15 -0600 | [diff] [blame] | 259 | } |
| 260 | |
Shawn McCarney | e0c6a2d | 2020-05-01 11:37:08 -0500 | [diff] [blame] | 261 | fs::path Manager::findConfigFile() |
| 262 | { |
Shawn McCarney | 589c181 | 2021-01-14 12:13:26 -0600 | [diff] [blame] | 263 | // Build list of possible base file names |
| 264 | std::vector<std::string> fileNames{}; |
| 265 | |
| 266 | // Add possible file names based on compatible system types (if any) |
| 267 | for (const std::string& systemType : compatibleSystemTypes) |
Shawn McCarney | e0c6a2d | 2020-05-01 11:37:08 -0500 | [diff] [blame] | 268 | { |
Shawn McCarney | 589c181 | 2021-01-14 12:13:26 -0600 | [diff] [blame] | 269 | // Replace all spaces and commas in system type name with underscores |
| 270 | std::string fileName{systemType}; |
| 271 | std::replace(fileName.begin(), fileName.end(), ' ', '_'); |
| 272 | std::replace(fileName.begin(), fileName.end(), ',', '_'); |
| 273 | |
| 274 | // Append .json suffix and add to list |
| 275 | fileName.append(".json"); |
| 276 | fileNames.emplace_back(fileName); |
| 277 | } |
| 278 | |
| 279 | // Add default file name for systems that don't use compatible interface |
| 280 | fileNames.emplace_back(defaultConfigFileName); |
| 281 | |
| 282 | // Look for a config file with one of the possible base names |
| 283 | for (const std::string& fileName : fileNames) |
| 284 | { |
| 285 | // Check if file exists in test directory |
| 286 | fs::path pathName{testConfigFileDir / fileName}; |
| 287 | if (fs::exists(pathName)) |
Shawn McCarney | e0c6a2d | 2020-05-01 11:37:08 -0500 | [diff] [blame] | 288 | { |
Shawn McCarney | 589c181 | 2021-01-14 12:13:26 -0600 | [diff] [blame] | 289 | return pathName; |
| 290 | } |
| 291 | |
| 292 | // Check if file exists in standard directory |
| 293 | pathName = standardConfigFileDir / fileName; |
| 294 | if (fs::exists(pathName)) |
| 295 | { |
| 296 | return pathName; |
Shawn McCarney | e0c6a2d | 2020-05-01 11:37:08 -0500 | [diff] [blame] | 297 | } |
| 298 | } |
| 299 | |
Shawn McCarney | 589c181 | 2021-01-14 12:13:26 -0600 | [diff] [blame] | 300 | // No config file found; return empty path |
| 301 | return fs::path{}; |
Shawn McCarney | e0c6a2d | 2020-05-01 11:37:08 -0500 | [diff] [blame] | 302 | } |
| 303 | |
| 304 | void Manager::loadConfigFile() |
| 305 | { |
| 306 | try |
| 307 | { |
| 308 | // Find the absolute path to the config file |
| 309 | fs::path pathName = findConfigFile(); |
Shawn McCarney | 589c181 | 2021-01-14 12:13:26 -0600 | [diff] [blame] | 310 | if (!pathName.empty()) |
| 311 | { |
| 312 | // Log info message in journal; config file path is important |
| 313 | services.getJournal().logInfo("Loading configuration file " + |
| 314 | pathName.string()); |
Shawn McCarney | e0c6a2d | 2020-05-01 11:37:08 -0500 | [diff] [blame] | 315 | |
Shawn McCarney | 589c181 | 2021-01-14 12:13:26 -0600 | [diff] [blame] | 316 | // Parse the config file |
| 317 | std::vector<std::unique_ptr<Rule>> rules{}; |
| 318 | std::vector<std::unique_ptr<Chassis>> chassis{}; |
| 319 | std::tie(rules, chassis) = config_file_parser::parse(pathName); |
Shawn McCarney | e0c6a2d | 2020-05-01 11:37:08 -0500 | [diff] [blame] | 320 | |
Shawn McCarney | 589c181 | 2021-01-14 12:13:26 -0600 | [diff] [blame] | 321 | // Store config file information in a new System object. The old |
| 322 | // System object, if any, is automatically deleted. |
| 323 | system = |
| 324 | std::make_unique<System>(std::move(rules), std::move(chassis)); |
| 325 | } |
Shawn McCarney | e0c6a2d | 2020-05-01 11:37:08 -0500 | [diff] [blame] | 326 | } |
| 327 | catch (const std::exception& e) |
| 328 | { |
| 329 | // Log error messages in journal |
Shawn McCarney | b464c8b | 2020-07-16 17:51:38 -0500 | [diff] [blame] | 330 | services.getJournal().logError(exception_utils::getMessages(e)); |
| 331 | services.getJournal().logError("Unable to load configuration file"); |
Shawn McCarney | e0c6a2d | 2020-05-01 11:37:08 -0500 | [diff] [blame] | 332 | |
| 333 | // TODO: Create error log entry |
| 334 | } |
| 335 | } |
| 336 | |
Shawn McCarney | 84807b9 | 2020-04-30 18:40:03 -0500 | [diff] [blame] | 337 | } // namespace phosphor::power::regulators |