Jim Wright | 1553cd9 | 2021-03-31 16:11:59 -0500 | [diff] [blame] | 1 | /** |
| 2 | * Copyright © 2021 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 | |
Jim Wright | 7945dd2 | 2021-04-06 16:55:15 -0500 | [diff] [blame] | 17 | #include "ucd90320_monitor.hpp" |
| 18 | |
Jim Wright | 56ae78e | 2021-12-01 14:46:15 -0600 | [diff] [blame] | 19 | #include "utility.hpp" |
Jim Wright | 7945dd2 | 2021-04-06 16:55:15 -0500 | [diff] [blame] | 20 | |
Jim Wright | 56ae78e | 2021-12-01 14:46:15 -0600 | [diff] [blame] | 21 | #include <fmt/format.h> |
| 22 | #include <fmt/ranges.h> |
| 23 | |
Jim Wright | d8a8617 | 2021-12-08 11:38:26 -0600 | [diff] [blame] | 24 | #include <nlohmann/json.hpp> |
Jim Wright | 56ae78e | 2021-12-01 14:46:15 -0600 | [diff] [blame] | 25 | #include <phosphor-logging/log.hpp> |
| 26 | #include <sdbusplus/bus.hpp> |
Jim Wright | 71a1413 | 2022-01-28 09:46:46 -0600 | [diff] [blame] | 27 | #include <xyz/openbmc_project/Common/Device/error.hpp> |
Jim Wright | 56ae78e | 2021-12-01 14:46:15 -0600 | [diff] [blame] | 28 | |
Jim Wright | d8a8617 | 2021-12-08 11:38:26 -0600 | [diff] [blame] | 29 | #include <fstream> |
Jim Wright | 56ae78e | 2021-12-01 14:46:15 -0600 | [diff] [blame] | 30 | #include <map> |
Jim Wright | 7945dd2 | 2021-04-06 16:55:15 -0500 | [diff] [blame] | 31 | #include <string> |
| 32 | |
| 33 | namespace phosphor::power::sequencer |
Jim Wright | 1553cd9 | 2021-03-31 16:11:59 -0500 | [diff] [blame] | 34 | { |
Jim Wright | 7945dd2 | 2021-04-06 16:55:15 -0500 | [diff] [blame] | 35 | |
Jim Wright | d8a8617 | 2021-12-08 11:38:26 -0600 | [diff] [blame] | 36 | using json = nlohmann::json; |
Jim Wright | 71a1413 | 2022-01-28 09:46:46 -0600 | [diff] [blame] | 37 | using namespace pmbus; |
Jim Wright | 56ae78e | 2021-12-01 14:46:15 -0600 | [diff] [blame] | 38 | using namespace phosphor::logging; |
| 39 | using namespace phosphor::power; |
| 40 | |
| 41 | const std::string compatibleInterface = |
| 42 | "xyz.openbmc_project.Configuration.IBMCompatibleSystem"; |
| 43 | const std::string compatibleNamesProperty = "Names"; |
| 44 | |
Jim Wright | 71a1413 | 2022-01-28 09:46:46 -0600 | [diff] [blame] | 45 | namespace device_error = sdbusplus::xyz::openbmc_project::Common::Device::Error; |
| 46 | |
Jim Wright | 7945dd2 | 2021-04-06 16:55:15 -0500 | [diff] [blame] | 47 | UCD90320Monitor::UCD90320Monitor(sdbusplus::bus::bus& bus, std::uint8_t i2cBus, |
| 48 | std::uint16_t i2cAddress) : |
Jim Wright | 930458c | 2022-01-24 14:37:27 -0600 | [diff] [blame] | 49 | PowerSequencerMonitor(bus), |
| 50 | match{bus, |
| 51 | sdbusplus::bus::match::rules::interfacesAdded() + |
| 52 | sdbusplus::bus::match::rules::sender( |
| 53 | "xyz.openbmc_project.EntityManager"), |
| 54 | std::bind(&UCD90320Monitor::interfacesAddedHandler, this, |
| 55 | std::placeholders::_1)}, |
Jim Wright | 56ae78e | 2021-12-01 14:46:15 -0600 | [diff] [blame] | 56 | pmbusInterface{ |
| 57 | fmt::format("/sys/bus/i2c/devices/{}-{:04x}", i2cBus, i2cAddress) |
| 58 | .c_str(), |
| 59 | "ucd9000", 0} |
| 60 | |
| 61 | { |
| 62 | // Use the compatible system types information, if already available, to |
| 63 | // load the configuration file |
| 64 | findCompatibleSystemTypes(); |
Jim Wright | 1553cd9 | 2021-03-31 16:11:59 -0500 | [diff] [blame] | 65 | } |
Jim Wright | 56ae78e | 2021-12-01 14:46:15 -0600 | [diff] [blame] | 66 | |
Jim Wright | 71a1413 | 2022-01-28 09:46:46 -0600 | [diff] [blame] | 67 | bool UCD90320Monitor::checkPGOODFaults( |
| 68 | std::map<std::string, std::string>& additionalData) |
| 69 | { |
| 70 | // Check only the GPIs configured on this system. |
| 71 | std::vector<int> values = lines.get_values(); |
| 72 | |
| 73 | bool errorCreated = false; |
| 74 | for (size_t pin = 0; pin < pins.size(); ++pin) |
| 75 | { |
| 76 | if (pin < values.size() && !values[pin]) |
| 77 | { |
| 78 | try |
| 79 | { |
| 80 | additionalData.emplace( |
| 81 | "STATUS_WORD", fmt::format("{:#04x}", readStatusWord())); |
| 82 | additionalData.emplace("MFR_STATUS", |
| 83 | fmt::format("{:#04x}", readMFRStatus())); |
| 84 | } |
| 85 | catch (device_error::ReadFailure& e) |
| 86 | { |
| 87 | log<level::ERR>("ReadFailure when collecting metadata"); |
| 88 | } |
| 89 | additionalData.emplace("INPUT_NUM", |
| 90 | fmt::format("{}", pins[pin].line)); |
| 91 | additionalData.emplace("INPUT_NAME", pins[pin].name); |
| 92 | additionalData.emplace("INPUT_STATUS", |
| 93 | fmt::format("{}", values[pin])); |
| 94 | |
| 95 | logError("xyz.openbmc_project.Power.Error.PowerSequencerPGOODFault", |
| 96 | additionalData); |
| 97 | |
| 98 | errorCreated = true; |
| 99 | break; |
| 100 | } |
| 101 | } |
| 102 | return errorCreated; |
| 103 | } |
| 104 | |
| 105 | bool UCD90320Monitor::checkVOUTFaults( |
| 106 | std::map<std::string, std::string>& additionalData) |
| 107 | { |
| 108 | // The status_word register has a summary bit to tell us |
| 109 | // if each page even needs to be checked |
| 110 | auto statusWord = readStatusWord(); |
| 111 | if (!(statusWord & status_word::VOUT_FAULT)) |
| 112 | { |
| 113 | return false; |
| 114 | } |
| 115 | |
| 116 | constexpr size_t numberPages = 24; |
| 117 | bool errorCreated = false; |
| 118 | for (size_t page = 0; page < numberPages; page++) |
| 119 | { |
| 120 | auto statusVout = pmbusInterface.insertPageNum(STATUS_VOUT, page); |
| 121 | uint8_t vout = pmbusInterface.read(statusVout, Type::Debug); |
| 122 | |
| 123 | // If any bits are on log them, though some are just |
| 124 | // warnings so they won't cause errors |
| 125 | if (vout) |
| 126 | { |
| 127 | log<level::INFO>("A voltage rail has bits on in STATUS_VOUT", |
| 128 | entry("STATUS_VOUT=0x%X", vout), |
| 129 | entry("PAGE=%d", page)); |
| 130 | } |
| 131 | |
| 132 | // Log errors if any non-warning bits on |
| 133 | if (vout & ~status_vout::WARNING_MASK) |
| 134 | { |
| 135 | auto railName = rails[page]; |
| 136 | |
| 137 | additionalData.emplace("STATUS_WORD", |
| 138 | fmt::format("{:#04x}", statusWord)); |
| 139 | additionalData.emplace("STATUS_VOUT", fmt::format("{:#02x}", vout)); |
| 140 | try |
| 141 | { |
| 142 | additionalData.emplace("MFR_STATUS", |
| 143 | fmt::format("{:#04x}", readMFRStatus())); |
| 144 | } |
| 145 | catch (device_error::ReadFailure& e) |
| 146 | { |
| 147 | log<level::ERR>("ReadFailure when collecting MFR_STATUS"); |
| 148 | } |
| 149 | additionalData.emplace("RAIL", fmt::format("{}", page)); |
| 150 | additionalData.emplace("RAIL_NAME", railName); |
| 151 | |
| 152 | logError( |
| 153 | "xyz.openbmc_project.Power.Error.PowerSequencerVoltageFault", |
| 154 | additionalData); |
| 155 | |
| 156 | errorCreated = true; |
| 157 | break; |
| 158 | } |
| 159 | } |
| 160 | |
| 161 | return errorCreated; |
| 162 | } |
| 163 | |
Jim Wright | 56ae78e | 2021-12-01 14:46:15 -0600 | [diff] [blame] | 164 | void UCD90320Monitor::findCompatibleSystemTypes() |
| 165 | { |
| 166 | try |
| 167 | { |
| 168 | auto subTree = util::getSubTree(bus, "/xyz/openbmc_project/inventory", |
| 169 | compatibleInterface, 0); |
| 170 | |
| 171 | auto objectIt = subTree.cbegin(); |
| 172 | if (objectIt != subTree.cend()) |
| 173 | { |
| 174 | const auto& objPath = objectIt->first; |
| 175 | |
| 176 | // Get the first service name |
| 177 | auto serviceIt = objectIt->second.cbegin(); |
| 178 | if (serviceIt != objectIt->second.cend()) |
| 179 | { |
| 180 | std::string service = serviceIt->first; |
| 181 | if (!service.empty()) |
| 182 | { |
| 183 | std::vector<std::string> compatibleSystemTypes; |
| 184 | |
| 185 | // Get compatible system types property value |
| 186 | util::getProperty(compatibleInterface, |
| 187 | compatibleNamesProperty, objPath, service, |
| 188 | bus, compatibleSystemTypes); |
| 189 | |
| 190 | log<level::DEBUG>( |
| 191 | fmt::format("Found compatible systems: {}", |
| 192 | compatibleSystemTypes) |
| 193 | .c_str()); |
| 194 | // Use compatible systems information to find config file |
Jim Wright | d8a8617 | 2021-12-08 11:38:26 -0600 | [diff] [blame] | 195 | findConfigFile(compatibleSystemTypes); |
Jim Wright | 56ae78e | 2021-12-01 14:46:15 -0600 | [diff] [blame] | 196 | } |
| 197 | } |
| 198 | } |
| 199 | } |
| 200 | catch (const std::exception&) |
| 201 | { |
| 202 | // Compatible system types information is not available. |
| 203 | } |
| 204 | } |
| 205 | |
Jim Wright | d8a8617 | 2021-12-08 11:38:26 -0600 | [diff] [blame] | 206 | void UCD90320Monitor::findConfigFile( |
| 207 | const std::vector<std::string>& compatibleSystemTypes) |
| 208 | { |
| 209 | // Expected config file path name: |
| 210 | // /usr/share/phosphor-power-sequencer/UCD90320Monitor_<systemType>.json |
| 211 | |
| 212 | // Add possible file names based on compatible system types (if any) |
| 213 | for (const std::string& systemType : compatibleSystemTypes) |
| 214 | { |
| 215 | // Check if file exists |
| 216 | std::filesystem::path pathName{ |
| 217 | "/usr/share/phosphor-power-sequencer/UCD90320Monitor_" + |
| 218 | systemType + ".json"}; |
| 219 | if (std::filesystem::exists(pathName)) |
| 220 | { |
| 221 | log<level::INFO>( |
| 222 | fmt::format("Config file path: {}", pathName.string()).c_str()); |
| 223 | parseConfigFile(pathName); |
| 224 | break; |
| 225 | } |
| 226 | } |
| 227 | } |
| 228 | |
Jim Wright | 56ae78e | 2021-12-01 14:46:15 -0600 | [diff] [blame] | 229 | void UCD90320Monitor::interfacesAddedHandler(sdbusplus::message::message& msg) |
| 230 | { |
Jim Wright | d8a8617 | 2021-12-08 11:38:26 -0600 | [diff] [blame] | 231 | // Only continue if message is valid and rails / pins have not already been |
| 232 | // found |
| 233 | if (!msg || !rails.empty()) |
Jim Wright | 56ae78e | 2021-12-01 14:46:15 -0600 | [diff] [blame] | 234 | { |
| 235 | return; |
| 236 | } |
| 237 | |
| 238 | try |
| 239 | { |
| 240 | // Read the dbus message |
| 241 | sdbusplus::message::object_path objPath; |
| 242 | std::map<std::string, |
| 243 | std::map<std::string, std::variant<std::vector<std::string>>>> |
| 244 | interfaces; |
| 245 | msg.read(objPath, interfaces); |
| 246 | |
| 247 | // Find the compatible interface, if present |
| 248 | auto itIntf = interfaces.find(compatibleInterface); |
| 249 | if (itIntf != interfaces.cend()) |
| 250 | { |
| 251 | // Find the Names property of the compatible interface, if present |
| 252 | auto itProp = itIntf->second.find(compatibleNamesProperty); |
| 253 | if (itProp != itIntf->second.cend()) |
| 254 | { |
| 255 | // Get value of Names property |
| 256 | const auto& propValue = std::get<0>(itProp->second); |
| 257 | if (!propValue.empty()) |
| 258 | { |
| 259 | log<level::INFO>( |
| 260 | fmt::format( |
| 261 | "InterfacesAdded for compatible systems: {}", |
| 262 | propValue) |
| 263 | .c_str()); |
| 264 | |
| 265 | // Use compatible systems information to find config file |
Jim Wright | d8a8617 | 2021-12-08 11:38:26 -0600 | [diff] [blame] | 266 | findConfigFile(propValue); |
Jim Wright | 56ae78e | 2021-12-01 14:46:15 -0600 | [diff] [blame] | 267 | } |
| 268 | } |
| 269 | } |
| 270 | } |
| 271 | catch (const std::exception&) |
| 272 | { |
| 273 | // Error trying to read interfacesAdded message. |
| 274 | } |
| 275 | } |
Jim Wright | 7945dd2 | 2021-04-06 16:55:15 -0500 | [diff] [blame] | 276 | |
Jim Wright | d8a8617 | 2021-12-08 11:38:26 -0600 | [diff] [blame] | 277 | void UCD90320Monitor::parseConfigFile(const std::filesystem::path& pathName) |
| 278 | { |
| 279 | try |
| 280 | { |
| 281 | std::ifstream file{pathName}; |
| 282 | json rootElement = json::parse(file); |
| 283 | |
| 284 | // Parse rail information from config file |
| 285 | auto railsIterator = rootElement.find("rails"); |
| 286 | if (railsIterator != rootElement.end()) |
| 287 | { |
| 288 | for (const auto& railElement : *railsIterator) |
| 289 | { |
| 290 | std::string rail = railElement.get<std::string>(); |
| 291 | rails.emplace_back(std::move(rail)); |
| 292 | } |
| 293 | } |
| 294 | else |
| 295 | { |
| 296 | log<level::ERR>( |
| 297 | fmt::format("No rails found in configuration file: {}", |
| 298 | pathName.string()) |
| 299 | .c_str()); |
| 300 | } |
| 301 | log<level::DEBUG>(fmt::format("Found rails: {}", rails).c_str()); |
| 302 | |
Jim Wright | d8fc068 | 2022-01-11 15:36:00 -0600 | [diff] [blame] | 303 | // List of line offsets for libgpiod |
| 304 | std::vector<unsigned int> offsets; |
| 305 | |
Jim Wright | d8a8617 | 2021-12-08 11:38:26 -0600 | [diff] [blame] | 306 | // Parse pin information from config file |
| 307 | auto pinsIterator = rootElement.find("pins"); |
| 308 | if (pinsIterator != rootElement.end()) |
| 309 | { |
| 310 | for (const auto& pinElement : *pinsIterator) |
| 311 | { |
| 312 | auto nameIterator = pinElement.find("name"); |
| 313 | auto lineIterator = pinElement.find("line"); |
| 314 | |
| 315 | if (nameIterator != pinElement.end() && |
| 316 | lineIterator != pinElement.end()) |
| 317 | { |
| 318 | std::string name = (*nameIterator).get<std::string>(); |
Jim Wright | d8fc068 | 2022-01-11 15:36:00 -0600 | [diff] [blame] | 319 | unsigned int line = (*lineIterator).get<unsigned int>(); |
Jim Wright | d8a8617 | 2021-12-08 11:38:26 -0600 | [diff] [blame] | 320 | |
| 321 | Pin pin; |
| 322 | pin.name = name; |
| 323 | pin.line = line; |
| 324 | pins.emplace_back(std::move(pin)); |
Jim Wright | d8fc068 | 2022-01-11 15:36:00 -0600 | [diff] [blame] | 325 | offsets.emplace_back(line); |
Jim Wright | d8a8617 | 2021-12-08 11:38:26 -0600 | [diff] [blame] | 326 | } |
| 327 | else |
| 328 | { |
| 329 | log<level::ERR>( |
| 330 | fmt::format( |
| 331 | "No name or line found within pin in configuration file: {}", |
| 332 | pathName.string()) |
| 333 | .c_str()); |
| 334 | } |
| 335 | } |
| 336 | } |
| 337 | else |
| 338 | { |
| 339 | log<level::ERR>( |
| 340 | fmt::format("No pins found in configuration file: {}", |
| 341 | pathName.string()) |
| 342 | .c_str()); |
| 343 | } |
| 344 | log<level::DEBUG>( |
| 345 | fmt::format("Found number of pins: {}", rails.size()).c_str()); |
Jim Wright | d8fc068 | 2022-01-11 15:36:00 -0600 | [diff] [blame] | 346 | setUpGpio(offsets); |
Jim Wright | d8a8617 | 2021-12-08 11:38:26 -0600 | [diff] [blame] | 347 | } |
| 348 | catch (const std::exception& e) |
| 349 | { |
| 350 | // Log error message in journal |
| 351 | log<level::ERR>(std::string("Exception parsing configuration file: " + |
| 352 | std::string(e.what())) |
| 353 | .c_str()); |
| 354 | } |
| 355 | } |
| 356 | |
Jim Wright | 71a1413 | 2022-01-28 09:46:46 -0600 | [diff] [blame] | 357 | void UCD90320Monitor::onFailure(bool timeout, |
| 358 | const std::string& powerSupplyError) |
| 359 | { |
| 360 | std::map<std::string, std::string> additionalData{}; |
| 361 | if (!powerSupplyError.empty()) |
| 362 | { |
| 363 | logError(powerSupplyError, additionalData); |
| 364 | return; |
| 365 | } |
| 366 | |
| 367 | try |
| 368 | { |
| 369 | bool voutError = checkVOUTFaults(additionalData); |
| 370 | bool pgoodError = checkPGOODFaults(additionalData); |
| 371 | |
| 372 | // Not a voltage or PGOOD fault, but we know something |
| 373 | // failed so still create an error log. |
| 374 | if (!voutError && !pgoodError) |
| 375 | { |
| 376 | // Default to generic pgood error |
| 377 | logError("xyz.openbmc_project.Power.Error.Shutdown", |
| 378 | additionalData); |
| 379 | } |
| 380 | } |
| 381 | catch (device_error::ReadFailure& e) |
| 382 | { |
| 383 | log<level::ERR>("ReadFailure when collecting metadata"); |
| 384 | |
| 385 | if (timeout) |
| 386 | { |
| 387 | // Default to timeout error |
| 388 | logError("xyz.openbmc_project.Power.Error.PowerOnTimeout", |
| 389 | additionalData); |
| 390 | } |
| 391 | else |
| 392 | { |
| 393 | // Default to generic pgood error |
| 394 | logError("xyz.openbmc_project.Power.Error.Shutdown", |
| 395 | additionalData); |
| 396 | } |
| 397 | } |
| 398 | } |
| 399 | |
| 400 | uint16_t UCD90320Monitor::readStatusWord() |
| 401 | { |
| 402 | return pmbusInterface.read(STATUS_WORD, Type::Debug); |
| 403 | } |
| 404 | |
| 405 | uint32_t UCD90320Monitor::readMFRStatus() |
| 406 | { |
| 407 | const std::string mfrStatus = "mfr_status"; |
| 408 | return pmbusInterface.read(mfrStatus, Type::HwmonDeviceDebug); |
| 409 | } |
| 410 | |
Jim Wright | d8fc068 | 2022-01-11 15:36:00 -0600 | [diff] [blame] | 411 | void UCD90320Monitor::setUpGpio(const std::vector<unsigned int>& offsets) |
| 412 | { |
| 413 | gpiod::chip chip{"ucd90320", gpiod::chip::OPEN_BY_LABEL}; |
| 414 | lines = chip.get_lines(offsets); |
| 415 | lines.request( |
| 416 | {"phosphor-power-control", gpiod::line_request::DIRECTION_INPUT, 0}); |
| 417 | } |
| 418 | |
Jim Wright | 7945dd2 | 2021-04-06 16:55:15 -0500 | [diff] [blame] | 419 | } // namespace phosphor::power::sequencer |