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