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 | |
Jim Wright | d8fc068 | 2022-01-11 15:36:00 -0600 | [diff] [blame] | 206 | // List of line offsets for libgpiod |
| 207 | std::vector<unsigned int> offsets; |
| 208 | |
Jim Wright | d8a8617 | 2021-12-08 11:38:26 -0600 | [diff] [blame] | 209 | // Parse pin information from config file |
| 210 | auto pinsIterator = rootElement.find("pins"); |
| 211 | if (pinsIterator != rootElement.end()) |
| 212 | { |
| 213 | for (const auto& pinElement : *pinsIterator) |
| 214 | { |
| 215 | auto nameIterator = pinElement.find("name"); |
| 216 | auto lineIterator = pinElement.find("line"); |
| 217 | |
| 218 | if (nameIterator != pinElement.end() && |
| 219 | lineIterator != pinElement.end()) |
| 220 | { |
| 221 | std::string name = (*nameIterator).get<std::string>(); |
Jim Wright | d8fc068 | 2022-01-11 15:36:00 -0600 | [diff] [blame] | 222 | unsigned int line = (*lineIterator).get<unsigned int>(); |
Jim Wright | d8a8617 | 2021-12-08 11:38:26 -0600 | [diff] [blame] | 223 | |
| 224 | Pin pin; |
| 225 | pin.name = name; |
| 226 | pin.line = line; |
| 227 | pins.emplace_back(std::move(pin)); |
Jim Wright | d8fc068 | 2022-01-11 15:36:00 -0600 | [diff] [blame] | 228 | offsets.emplace_back(line); |
Jim Wright | d8a8617 | 2021-12-08 11:38:26 -0600 | [diff] [blame] | 229 | } |
| 230 | else |
| 231 | { |
| 232 | log<level::ERR>( |
| 233 | fmt::format( |
| 234 | "No name or line found within pin in configuration file: {}", |
| 235 | pathName.string()) |
| 236 | .c_str()); |
| 237 | } |
| 238 | } |
| 239 | } |
| 240 | else |
| 241 | { |
| 242 | log<level::ERR>( |
| 243 | fmt::format("No pins found in configuration file: {}", |
| 244 | pathName.string()) |
| 245 | .c_str()); |
| 246 | } |
| 247 | log<level::DEBUG>( |
| 248 | fmt::format("Found number of pins: {}", rails.size()).c_str()); |
Jim Wright | d8fc068 | 2022-01-11 15:36:00 -0600 | [diff] [blame] | 249 | setUpGpio(offsets); |
Jim Wright | d8a8617 | 2021-12-08 11:38:26 -0600 | [diff] [blame] | 250 | } |
| 251 | catch (const std::exception& e) |
| 252 | { |
| 253 | // Log error message in journal |
| 254 | log<level::ERR>(std::string("Exception parsing configuration file: " + |
| 255 | std::string(e.what())) |
| 256 | .c_str()); |
| 257 | } |
| 258 | } |
| 259 | |
Jim Wright | 71a1413 | 2022-01-28 09:46:46 -0600 | [diff] [blame] | 260 | void UCD90320Monitor::onFailure(bool timeout, |
| 261 | const std::string& powerSupplyError) |
| 262 | { |
Jim Wright | f6f0da9 | 2022-02-28 21:08:33 -0600 | [diff] [blame^] | 263 | std::string message; |
Jim Wright | 71a1413 | 2022-01-28 09:46:46 -0600 | [diff] [blame] | 264 | std::map<std::string, std::string> additionalData{}; |
Jim Wright | 71a1413 | 2022-01-28 09:46:46 -0600 | [diff] [blame] | 265 | |
| 266 | try |
| 267 | { |
Jim Wright | f6f0da9 | 2022-02-28 21:08:33 -0600 | [diff] [blame^] | 268 | auto statusWord = readStatusWord(); |
| 269 | additionalData.emplace("STATUS_WORD", |
| 270 | fmt::format("{:#04x}", statusWord)); |
| 271 | try |
Jim Wright | 71a1413 | 2022-01-28 09:46:46 -0600 | [diff] [blame] | 272 | { |
Jim Wright | f6f0da9 | 2022-02-28 21:08:33 -0600 | [diff] [blame^] | 273 | additionalData.emplace("MFR_STATUS", |
| 274 | fmt::format("{:#04x}", readMFRStatus())); |
| 275 | } |
| 276 | catch (device_error::ReadFailure& e) |
| 277 | { |
| 278 | log<level::ERR>( |
| 279 | fmt::format("ReadFailure when collecting MFR_STATUS, error {}", |
| 280 | e.what()) |
| 281 | .c_str()); |
| 282 | } |
| 283 | |
| 284 | // The status_word register has a summary bit to tell us if each page |
| 285 | // even needs to be checked |
| 286 | if (statusWord & status_word::VOUT_FAULT) |
| 287 | { |
| 288 | constexpr size_t numberPages = 24; |
| 289 | for (size_t page = 0; page < numberPages; page++) |
| 290 | { |
| 291 | auto statusVout = |
| 292 | pmbusInterface.insertPageNum(STATUS_VOUT, page); |
| 293 | uint8_t vout = pmbusInterface.read(statusVout, Type::Debug); |
| 294 | |
| 295 | // If any bits are on log them, though some are just warnings so |
| 296 | // they won't cause errors |
| 297 | if (vout) |
| 298 | { |
| 299 | log<level::INFO>( |
| 300 | fmt::format("STATUS_VOUT, page: {}, value: {:#02x}", |
| 301 | page, vout) |
| 302 | .c_str()); |
| 303 | } |
| 304 | |
| 305 | // Log errors if any non-warning bits on |
| 306 | if (vout & ~status_vout::WARNING_MASK) |
| 307 | { |
| 308 | additionalData.emplace("STATUS_VOUT", |
| 309 | fmt::format("{:#02x}", vout)); |
| 310 | additionalData.emplace("PAGE", fmt::format("{}", page)); |
| 311 | additionalData.emplace("RAIL_NAME", rails[page]); |
| 312 | |
| 313 | // Use power supply error if set and 12v rail has failed, |
| 314 | // else use voltage error |
| 315 | message = |
| 316 | ((page == 0) && !powerSupplyError.empty()) |
| 317 | ? powerSupplyError |
| 318 | : "xyz.openbmc_project.Power.Error.PowerSequencerVoltageFault"; |
| 319 | break; |
| 320 | } |
| 321 | } |
| 322 | } |
| 323 | |
| 324 | // Check only the GPIs configured on this system. |
| 325 | std::vector<int> values = lines.get_values(); |
| 326 | additionalData.emplace("GPIO_VALUES", fmt::format("{}", values)); |
| 327 | |
| 328 | if (message.empty()) |
| 329 | { |
| 330 | // Didn't find a rail fail, examine pins |
| 331 | for (size_t pin = 0; pin < pins.size(); ++pin) |
| 332 | { |
| 333 | if (pin < values.size() && !values[pin]) |
| 334 | { |
| 335 | additionalData.emplace("INPUT_NUM", |
| 336 | fmt::format("{}", pins[pin].line)); |
| 337 | additionalData.emplace("INPUT_NAME", pins[pin].name); |
| 338 | additionalData.emplace("INPUT_STATUS", |
| 339 | fmt::format("{}", values[pin])); |
| 340 | message = |
| 341 | "xyz.openbmc_project.Power.Error.PowerSequencerPGOODFault"; |
| 342 | break; |
| 343 | } |
| 344 | } |
Jim Wright | 71a1413 | 2022-01-28 09:46:46 -0600 | [diff] [blame] | 345 | } |
| 346 | } |
| 347 | catch (device_error::ReadFailure& e) |
| 348 | { |
Jim Wright | f6f0da9 | 2022-02-28 21:08:33 -0600 | [diff] [blame^] | 349 | log<level::ERR>( |
| 350 | fmt::format("ReadFailure when collecting metadata, error {}", |
| 351 | e.what()) |
| 352 | .c_str()); |
Jim Wright | 71a1413 | 2022-01-28 09:46:46 -0600 | [diff] [blame] | 353 | } |
Jim Wright | f6f0da9 | 2022-02-28 21:08:33 -0600 | [diff] [blame^] | 354 | |
| 355 | if (message.empty()) |
| 356 | { |
| 357 | // Could not isolate, but we know something failed, so issue a timeout |
| 358 | // or generic power good error |
| 359 | message = timeout ? "xyz.openbmc_project.Power.Error.PowerOnTimeout" |
| 360 | : "xyz.openbmc_project.Power.Error.Shutdown"; |
| 361 | } |
| 362 | logError(message, additionalData); |
Jim Wright | 71a1413 | 2022-01-28 09:46:46 -0600 | [diff] [blame] | 363 | } |
| 364 | |
| 365 | uint16_t UCD90320Monitor::readStatusWord() |
| 366 | { |
| 367 | return pmbusInterface.read(STATUS_WORD, Type::Debug); |
| 368 | } |
| 369 | |
| 370 | uint32_t UCD90320Monitor::readMFRStatus() |
| 371 | { |
| 372 | const std::string mfrStatus = "mfr_status"; |
| 373 | return pmbusInterface.read(mfrStatus, Type::HwmonDeviceDebug); |
| 374 | } |
| 375 | |
Jim Wright | d8fc068 | 2022-01-11 15:36:00 -0600 | [diff] [blame] | 376 | void UCD90320Monitor::setUpGpio(const std::vector<unsigned int>& offsets) |
| 377 | { |
| 378 | gpiod::chip chip{"ucd90320", gpiod::chip::OPEN_BY_LABEL}; |
| 379 | lines = chip.get_lines(offsets); |
| 380 | lines.request( |
| 381 | {"phosphor-power-control", gpiod::line_request::DIRECTION_INPUT, 0}); |
| 382 | } |
| 383 | |
Jim Wright | 7945dd2 | 2021-04-06 16:55:15 -0500 | [diff] [blame] | 384 | } // namespace phosphor::power::sequencer |