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