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 #include "device_callouts.hpp" 17 18 #include "paths.hpp" 19 20 #include <phosphor-logging/lg2.hpp> 21 22 #include <fstream> 23 #include <regex> 24 25 namespace openpower::pels::device_callouts 26 { 27 28 constexpr auto debugFilePath = "/etc/phosphor-logging/"; 29 constexpr auto calloutFileSuffix = "_dev_callouts.json"; 30 31 namespace fs = std::filesystem; 32 33 namespace util 34 { 35 36 fs::path getJSONFilename(const std::vector<std::string>& compatibleList) 37 { 38 std::vector<std::string> names{compatibleList}; 39 auto basePath = getPELReadOnlyDataPath(); 40 fs::path fullPath; 41 42 // Find an entry in the list of compatible system names that 43 // matches a filename we have. 44 45 // Also match on just _dev_callouts.json, which may be present 46 // when it's known that compatibleList won't be correct. 47 names.push_back(""); 48 49 for (const auto& name : names) 50 { 51 fs::path filename = name + calloutFileSuffix; 52 53 // Check the debug path first 54 fs::path path{fs::path{debugFilePath} / filename}; 55 56 if (fs::exists(path)) 57 { 58 lg2::info("Found device callout debug file"); 59 fullPath = path; 60 break; 61 } 62 63 path = basePath / filename; 64 65 if (fs::exists(path)) 66 { 67 fullPath = path; 68 break; 69 } 70 } 71 72 if (fullPath.empty()) 73 { 74 throw std::invalid_argument( 75 "No JSON dev path callout file for this system"); 76 } 77 78 return fullPath; 79 } 80 81 /** 82 * @brief Reads the callout JSON into an object based on the 83 * compatible system names list. 84 * 85 * @param[in] compatibleList - The list of compatible names for this 86 * system. 87 * 88 * @return nlohmann::json - The JSON object 89 */ 90 nlohmann::json loadJSON(const std::vector<std::string>& compatibleList) 91 { 92 auto filename = getJSONFilename(compatibleList); 93 std::ifstream file{filename}; 94 return nlohmann::json::parse(file); 95 } 96 97 std::tuple<size_t, uint8_t> getI2CSearchKeys(const std::string& devPath) 98 { 99 std::smatch match; 100 101 // Look for i2c-A/A-00BB 102 // where A = bus number and BB = address 103 std::regex regex{"i2c-[0-9]+/([0-9]+)-00([0-9a-f]{2})"}; 104 105 regex_search(devPath, match, regex); 106 107 if (match.size() != 3) 108 { 109 std::string msg = "Could not get I2C bus and address from " + devPath; 110 throw std::invalid_argument{msg.c_str()}; 111 } 112 113 size_t bus = std::stoul(match[1].str(), nullptr, 0); 114 115 // An I2C bus on a CFAM has everything greater than the 10s digit 116 // as the CFAM number, so strip it off. Like: 117 // 112 = cfam1 bus 12 118 // 1001 = cfam10 bus 1 119 bus = bus % 100; 120 121 uint8_t address = std::stoul(match[2].str(), nullptr, 16); 122 123 return {bus, address}; 124 } 125 126 std::string getFSISearchKeys(const std::string& devPath) 127 { 128 std::string links; 129 std::smatch match; 130 auto search = devPath; 131 132 // Look for slave@XX: 133 // where XX = link number in hex 134 std::regex regex{"slave@([0-9a-f]{2}):"}; 135 136 // Find all links in the path and separate them with hyphens. 137 while (regex_search(search, match, regex)) 138 { 139 // Convert to an int first to handle a hex number like "0a" 140 // though in reality there won't be more than links 0 - 9. 141 auto linkNum = std::stoul(match[1].str(), nullptr, 16); 142 links += std::to_string(linkNum) + '-'; 143 144 search = match.suffix(); 145 } 146 147 if (links.empty()) 148 { 149 std::string msg = "Could not get FSI links from " + devPath; 150 throw std::invalid_argument{msg.c_str()}; 151 } 152 153 // Remove the trailing '-' 154 links.pop_back(); 155 156 return links; 157 } 158 159 std::tuple<std::string, std::tuple<size_t, uint8_t>> 160 getFSII2CSearchKeys(const std::string& devPath) 161 { 162 // This combines the FSI and i2C search keys 163 164 auto links = getFSISearchKeys(devPath); 165 auto busAndAddr = getI2CSearchKeys(devPath); 166 167 return {std::move(links), std::move(busAndAddr)}; 168 } 169 170 size_t getSPISearchKeys(const std::string& devPath) 171 { 172 std::smatch match; 173 174 // Look for spi_master/spiX/ where X is the SPI bus/port number 175 // Note: This doesn't distinguish between multiple chips on 176 // the same port as no need for it yet. 177 std::regex regex{"spi_master/spi(\\d+)/"}; 178 179 regex_search(devPath, match, regex); 180 181 if (match.size() != 2) 182 { 183 std::string msg = "Could not get SPI bus from " + devPath; 184 throw std::invalid_argument{msg.c_str()}; 185 } 186 187 size_t port = std::stoul(match[1].str()); 188 189 return port; 190 } 191 192 std::tuple<std::string, size_t> getFSISPISearchKeys(const std::string& devPath) 193 { 194 // Combine the FSI and SPI search keys. 195 auto links = getFSISearchKeys(devPath); 196 auto bus = getSPISearchKeys(devPath); 197 198 return {std::move(links), std::move(bus)}; 199 } 200 201 /** 202 * @brief Pull the callouts out of the JSON callout array passed in 203 * 204 * Create a vector of Callout objects based on the JSON. 205 * 206 * This will also fill in the 'debug' member on the first callout 207 * in the list, which could contain things like the I2C address and 208 * bus extracted from the device path. 209 * 210 * The callouts are in the order they should be added to the PEL. 211 * 212 * @param[in] calloutJSON - The Callouts JSON array to extract from 213 * @param[in] debug - The debug message to add to the first callout 214 * 215 * @return std::vector<Callout> - The Callout objects 216 */ 217 std::vector<Callout> extractCallouts(const nlohmann::json& calloutJSON, 218 const std::string& debug) 219 { 220 std::vector<Callout> callouts; 221 bool addDebug = true; 222 223 // The JSON element passed in is the array of callouts 224 if (!calloutJSON.is_array()) 225 { 226 throw std::runtime_error( 227 "Dev path callout JSON entry doesn't contain a 'Callouts' array"); 228 } 229 230 for (auto& callout : calloutJSON) 231 { 232 Callout c; 233 234 // Add any debug data to the first callout 235 if (addDebug && !debug.empty()) 236 { 237 addDebug = false; 238 c.debug = debug; 239 } 240 241 try 242 { 243 c.locationCode = callout.at("LocationCode").get<std::string>(); 244 c.name = callout.at("Name").get<std::string>(); 245 c.priority = callout.at("Priority").get<std::string>(); 246 247 if (callout.contains("MRU")) 248 { 249 c.mru = callout.at("MRU").get<std::string>(); 250 } 251 } 252 catch (const nlohmann::json::out_of_range& e) 253 { 254 std::string msg = 255 "Callout entry missing either LocationCode, Name, or Priority " 256 "properties: " + 257 callout.dump(); 258 throw std::runtime_error(msg.c_str()); 259 } 260 261 callouts.push_back(c); 262 } 263 264 return callouts; 265 } 266 267 /** 268 * @brief Looks up the callouts in the JSON using the I2C keys. 269 * 270 * @param[in] i2cBus - The I2C bus 271 * @param[in] i2cAddress - The I2C address 272 * @param[in] calloutJSON - The JSON containing the callouts 273 * 274 * @return std::vector<Callout> - The callouts 275 */ 276 std::vector<device_callouts::Callout> calloutI2C( 277 size_t i2cBus, uint8_t i2cAddress, const nlohmann::json& calloutJSON) 278 { 279 auto busString = std::to_string(i2cBus); 280 auto addrString = std::to_string(i2cAddress); 281 282 try 283 { 284 const auto& callouts = 285 calloutJSON.at("I2C").at(busString).at(addrString).at("Callouts"); 286 287 auto dest = calloutJSON.at("I2C") 288 .at(busString) 289 .at(addrString) 290 .at("Dest") 291 .get<std::string>(); 292 293 std::string msg = "I2C: bus: " + busString + " address: " + addrString + 294 " dest: " + dest; 295 296 return extractCallouts(callouts, msg); 297 } 298 catch (const nlohmann::json::out_of_range& e) 299 { 300 std::string msg = "Problem looking up I2C callouts on " + busString + 301 " " + addrString + ": " + std::string{e.what()}; 302 throw std::invalid_argument(msg.c_str()); 303 } 304 } 305 306 /** 307 * @brief Looks up the callouts in the JSON for this I2C path. 308 * 309 * @param[in] devPath - The device path 310 * @param[in] calloutJSON - The JSON containing the callouts 311 * 312 * @return std::vector<Callout> - The callouts 313 */ 314 std::vector<device_callouts::Callout> calloutI2CUsingPath( 315 const std::string& devPath, const nlohmann::json& calloutJSON) 316 { 317 auto [bus, address] = getI2CSearchKeys(devPath); 318 319 return calloutI2C(bus, address, calloutJSON); 320 } 321 322 /** 323 * @brief Looks up the callouts in the JSON for this FSI path. 324 * 325 * @param[in] devPath - The device path 326 * @param[in] calloutJSON - The JSON containing the callouts 327 * 328 * @return std::vector<Callout> - The callouts 329 */ 330 std::vector<device_callouts::Callout> 331 calloutFSI(const std::string& devPath, const nlohmann::json& calloutJSON) 332 { 333 auto links = getFSISearchKeys(devPath); 334 335 try 336 { 337 const auto& callouts = calloutJSON.at("FSI").at(links).at("Callouts"); 338 339 auto dest = 340 calloutJSON.at("FSI").at(links).at("Dest").get<std::string>(); 341 342 std::string msg = "FSI: links: " + links + " dest: " + dest; 343 344 return extractCallouts(callouts, msg); 345 } 346 catch (const nlohmann::json::out_of_range& e) 347 { 348 std::string msg = "Problem looking up FSI callouts on " + links + ": " + 349 std::string{e.what()}; 350 throw std::invalid_argument(msg.c_str()); 351 } 352 } 353 354 /** 355 * @brief Looks up the callouts in the JSON for this FSI-I2C path. 356 * 357 * @param[in] devPath - The device path 358 * @param[in] calloutJSON - The JSON containing the callouts 359 * 360 * @return std::vector<Callout> - The callouts 361 */ 362 std::vector<device_callouts::Callout> 363 calloutFSII2C(const std::string& devPath, const nlohmann::json& calloutJSON) 364 { 365 auto linksAndI2C = getFSII2CSearchKeys(devPath); 366 auto links = std::get<std::string>(linksAndI2C); 367 const auto& busAndAddr = std::get<1>(linksAndI2C); 368 369 auto busString = std::to_string(std::get<size_t>(busAndAddr)); 370 auto addrString = std::to_string(std::get<uint8_t>(busAndAddr)); 371 372 try 373 { 374 auto& callouts = calloutJSON.at("FSI-I2C") 375 .at(links) 376 .at(busString) 377 .at(addrString) 378 .at("Callouts"); 379 380 auto dest = calloutJSON.at("FSI-I2C") 381 .at(links) 382 .at(busString) 383 .at(addrString) 384 .at("Dest") 385 .get<std::string>(); 386 387 std::string msg = "FSI-I2C: links: " + links + " bus: " + busString + 388 " addr: " + addrString + " dest: " + dest; 389 390 return extractCallouts(callouts, msg); 391 } 392 catch (const nlohmann::json::out_of_range& e) 393 { 394 std::string msg = "Problem looking up FSI-I2C callouts on " + links + 395 " " + busString + " " + addrString + ": " + e.what(); 396 throw std::invalid_argument(msg.c_str()); 397 } 398 } 399 400 /** 401 * @brief Looks up the callouts in the JSON for this FSI-SPI path. 402 * 403 * @param[in] devPath - The device path 404 * @param[in] calloutJSON - The JSON containing the callouts 405 * 406 * @return std::vector<Callout> - The callouts 407 */ 408 std::vector<device_callouts::Callout> 409 calloutFSISPI(const std::string& devPath, const nlohmann::json& calloutJSON) 410 { 411 auto linksAndSPI = getFSISPISearchKeys(devPath); 412 auto links = std::get<std::string>(linksAndSPI); 413 auto busString = std::to_string(std::get<size_t>(linksAndSPI)); 414 415 try 416 { 417 auto& callouts = 418 calloutJSON.at("FSI-SPI").at(links).at(busString).at("Callouts"); 419 420 auto dest = calloutJSON.at("FSI-SPI") 421 .at(links) 422 .at(busString) 423 .at("Dest") 424 .get<std::string>(); 425 426 std::string msg = "FSI-SPI: links: " + links + " bus: " + busString + 427 " dest: " + dest; 428 429 return extractCallouts(callouts, msg); 430 } 431 catch (const nlohmann::json::out_of_range& e) 432 { 433 std::string msg = "Problem looking up FSI-SPI callouts on " + links + 434 " " + busString + ": " + std::string{e.what()}; 435 throw std::invalid_argument(msg.c_str()); 436 } 437 } 438 439 /** 440 * @brief Returns the callouts from the JSON based on the input 441 * device path. 442 * 443 * @param[in] devPath - The device path 444 * @param[in] json - The callout JSON 445 * 446 * @return std::vector<Callout> - The list of callouts 447 */ 448 std::vector<device_callouts::Callout> 449 findCallouts(const std::string& devPath, const nlohmann::json& json) 450 { 451 std::vector<Callout> callouts; 452 fs::path path; 453 454 // Gives the /sys/devices/platform/ path 455 try 456 { 457 path = fs::canonical(devPath); 458 } 459 catch (const fs::filesystem_error& e) 460 { 461 // Path not there, still try to do the callout 462 path = devPath; 463 } 464 465 switch (util::getCalloutType(path)) 466 { 467 case util::CalloutType::i2c: 468 callouts = calloutI2CUsingPath(path, json); 469 break; 470 case util::CalloutType::fsi: 471 callouts = calloutFSI(path, json); 472 break; 473 case util::CalloutType::fsii2c: 474 callouts = calloutFSII2C(path, json); 475 break; 476 case util::CalloutType::fsispi: 477 callouts = calloutFSISPI(path, json); 478 break; 479 default: 480 std::string msg = 481 "Could not get callout type from device path: " + path.string(); 482 throw std::invalid_argument{msg.c_str()}; 483 break; 484 } 485 486 return callouts; 487 } 488 489 CalloutType getCalloutType(const std::string& devPath) 490 { 491 if ((devPath.find("fsi-master") != std::string::npos) && 492 (devPath.find("i2c-") != std::string::npos)) 493 { 494 return CalloutType::fsii2c; 495 } 496 497 if ((devPath.find("fsi-master") != std::string::npos) && 498 (devPath.find("spi") != std::string::npos)) 499 { 500 return CalloutType::fsispi; 501 } 502 503 // Treat anything else FSI related as plain FSI 504 if (devPath.find("fsi-master") != std::string::npos) 505 { 506 return CalloutType::fsi; 507 } 508 509 if (devPath.find("i2c-") != std::string::npos) 510 { 511 return CalloutType::i2c; 512 } 513 514 return CalloutType::unknown; 515 } 516 517 } // namespace util 518 519 std::vector<Callout> getCallouts(const std::string& devPath, 520 const std::vector<std::string>& compatibleList) 521 { 522 auto json = util::loadJSON(compatibleList); 523 return util::findCallouts(devPath, json); 524 } 525 526 std::vector<Callout> 527 getI2CCallouts(size_t i2cBus, uint8_t i2cAddress, 528 const std::vector<std::string>& compatibleList) 529 { 530 auto json = util::loadJSON(compatibleList); 531 return util::calloutI2C(i2cBus, i2cAddress, json); 532 } 533 534 } // namespace openpower::pels::device_callouts 535