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 
getJSONFilename(const std::vector<std::string> & compatibleList)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  */
loadJSON(const std::vector<std::string> & compatibleList)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 
getI2CSearchKeys(const std::string & devPath)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 
getFSISearchKeys(const std::string & devPath)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>>
getFSII2CSearchKeys(const std::string & devPath)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 
getSPISearchKeys(const std::string & devPath)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 
getFSISPISearchKeys(const std::string & devPath)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  */
extractCallouts(const nlohmann::json & calloutJSON,const std::string & debug)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(size_t i2cBus,uint8_t i2cAddress,const nlohmann::json & calloutJSON)277     calloutI2C(size_t i2cBus, uint8_t i2cAddress,
278                const nlohmann::json& calloutJSON)
279 {
280     auto busString = std::to_string(i2cBus);
281     auto addrString = std::to_string(i2cAddress);
282 
283     try
284     {
285         const auto& callouts =
286             calloutJSON.at("I2C").at(busString).at(addrString).at("Callouts");
287 
288         auto dest = calloutJSON.at("I2C")
289                         .at(busString)
290                         .at(addrString)
291                         .at("Dest")
292                         .get<std::string>();
293 
294         std::string msg = "I2C: bus: " + busString + " address: " + addrString +
295                           " dest: " + dest;
296 
297         return extractCallouts(callouts, msg);
298     }
299     catch (const nlohmann::json::out_of_range& e)
300     {
301         std::string msg = "Problem looking up I2C callouts on " + busString +
302                           " " + addrString + ": " + std::string{e.what()};
303         throw std::invalid_argument(msg.c_str());
304     }
305 }
306 
307 /**
308  * @brief Looks up the callouts in the JSON for this I2C path.
309  *
310  * @param[in] devPath - The device path
311  * @param[in] calloutJSON - The JSON containing the callouts
312  *
313  * @return std::vector<Callout> - The callouts
314  */
315 std::vector<device_callouts::Callout>
calloutI2CUsingPath(const std::string & devPath,const nlohmann::json & calloutJSON)316     calloutI2CUsingPath(const std::string& devPath,
317                         const nlohmann::json& calloutJSON)
318 {
319     auto [bus, address] = getI2CSearchKeys(devPath);
320 
321     return calloutI2C(bus, address, calloutJSON);
322 }
323 
324 /**
325  * @brief Looks up the callouts in the JSON for this FSI path.
326  *
327  * @param[in] devPath - The device path
328  * @param[in] calloutJSON - The JSON containing the callouts
329  *
330  * @return std::vector<Callout> - The callouts
331  */
332 std::vector<device_callouts::Callout>
calloutFSI(const std::string & devPath,const nlohmann::json & calloutJSON)333     calloutFSI(const std::string& devPath, const nlohmann::json& calloutJSON)
334 {
335     auto links = getFSISearchKeys(devPath);
336 
337     try
338     {
339         const auto& callouts = calloutJSON.at("FSI").at(links).at("Callouts");
340 
341         auto dest =
342             calloutJSON.at("FSI").at(links).at("Dest").get<std::string>();
343 
344         std::string msg = "FSI: links: " + links + " dest: " + dest;
345 
346         return extractCallouts(callouts, msg);
347     }
348     catch (const nlohmann::json::out_of_range& e)
349     {
350         std::string msg = "Problem looking up FSI callouts on " + links + ": " +
351                           std::string{e.what()};
352         throw std::invalid_argument(msg.c_str());
353     }
354 }
355 
356 /**
357  * @brief Looks up the callouts in the JSON for this FSI-I2C path.
358  *
359  * @param[in] devPath - The device path
360  * @param[in] calloutJSON - The JSON containing the callouts
361  *
362  * @return std::vector<Callout> - The callouts
363  */
364 std::vector<device_callouts::Callout>
calloutFSII2C(const std::string & devPath,const nlohmann::json & calloutJSON)365     calloutFSII2C(const std::string& devPath, const nlohmann::json& calloutJSON)
366 {
367     auto linksAndI2C = getFSII2CSearchKeys(devPath);
368     auto links = std::get<std::string>(linksAndI2C);
369     const auto& busAndAddr = std::get<1>(linksAndI2C);
370 
371     auto busString = std::to_string(std::get<size_t>(busAndAddr));
372     auto addrString = std::to_string(std::get<uint8_t>(busAndAddr));
373 
374     try
375     {
376         auto& callouts = calloutJSON.at("FSI-I2C")
377                              .at(links)
378                              .at(busString)
379                              .at(addrString)
380                              .at("Callouts");
381 
382         auto dest = calloutJSON.at("FSI-I2C")
383                         .at(links)
384                         .at(busString)
385                         .at(addrString)
386                         .at("Dest")
387                         .get<std::string>();
388 
389         std::string msg = "FSI-I2C: links: " + links + " bus: " + busString +
390                           " addr: " + addrString + " dest: " + dest;
391 
392         return extractCallouts(callouts, msg);
393     }
394     catch (const nlohmann::json::out_of_range& e)
395     {
396         std::string msg = "Problem looking up FSI-I2C callouts on " + links +
397                           " " + busString + " " + addrString + ": " + e.what();
398         throw std::invalid_argument(msg.c_str());
399     }
400 }
401 
402 /**
403  * @brief Looks up the callouts in the JSON for this FSI-SPI path.
404  *
405  * @param[in] devPath - The device path
406  * @param[in] calloutJSON - The JSON containing the callouts
407  *
408  * @return std::vector<Callout> - The callouts
409  */
410 std::vector<device_callouts::Callout>
calloutFSISPI(const std::string & devPath,const nlohmann::json & calloutJSON)411     calloutFSISPI(const std::string& devPath, const nlohmann::json& calloutJSON)
412 {
413     auto linksAndSPI = getFSISPISearchKeys(devPath);
414     auto links = std::get<std::string>(linksAndSPI);
415     auto busString = std::to_string(std::get<size_t>(linksAndSPI));
416 
417     try
418     {
419         auto& callouts =
420             calloutJSON.at("FSI-SPI").at(links).at(busString).at("Callouts");
421 
422         auto dest = calloutJSON.at("FSI-SPI")
423                         .at(links)
424                         .at(busString)
425                         .at("Dest")
426                         .get<std::string>();
427 
428         std::string msg = "FSI-SPI: links: " + links + " bus: " + busString +
429                           " dest: " + dest;
430 
431         return extractCallouts(callouts, msg);
432     }
433     catch (const nlohmann::json::out_of_range& e)
434     {
435         std::string msg = "Problem looking up FSI-SPI callouts on " + links +
436                           " " + busString + ": " + std::string{e.what()};
437         throw std::invalid_argument(msg.c_str());
438     }
439 }
440 
441 /**
442  * @brief Returns the callouts from the JSON based on the input
443  *        device path.
444  *
445  * @param[in] devPath - The device path
446  * @param[in] json - The callout JSON
447  *
448  * @return std::vector<Callout> - The list of callouts
449  */
findCallouts(const std::string & devPath,const nlohmann::json & json)450 std::vector<device_callouts::Callout> findCallouts(const std::string& devPath,
451                                                    const nlohmann::json& json)
452 {
453     std::vector<Callout> callouts;
454     fs::path path;
455 
456     // Gives the /sys/devices/platform/ path
457     try
458     {
459         path = fs::canonical(devPath);
460     }
461     catch (const fs::filesystem_error& e)
462     {
463         // Path not there, still try to do the callout
464         path = devPath;
465     }
466 
467     switch (util::getCalloutType(path))
468     {
469         case util::CalloutType::i2c:
470             callouts = calloutI2CUsingPath(path, json);
471             break;
472         case util::CalloutType::fsi:
473             callouts = calloutFSI(path, json);
474             break;
475         case util::CalloutType::fsii2c:
476             callouts = calloutFSII2C(path, json);
477             break;
478         case util::CalloutType::fsispi:
479             callouts = calloutFSISPI(path, json);
480             break;
481         default:
482             std::string msg = "Could not get callout type from device path: " +
483                               path.string();
484             throw std::invalid_argument{msg.c_str()};
485             break;
486     }
487 
488     return callouts;
489 }
490 
getCalloutType(const std::string & devPath)491 CalloutType getCalloutType(const std::string& devPath)
492 {
493     if ((devPath.find("fsi-master") != std::string::npos) &&
494         (devPath.find("i2c-") != std::string::npos))
495     {
496         return CalloutType::fsii2c;
497     }
498 
499     if ((devPath.find("fsi-master") != std::string::npos) &&
500         (devPath.find("spi") != std::string::npos))
501     {
502         return CalloutType::fsispi;
503     }
504 
505     // Treat anything else FSI related as plain FSI
506     if (devPath.find("fsi-master") != std::string::npos)
507     {
508         return CalloutType::fsi;
509     }
510 
511     if (devPath.find("i2c-bus/i2c-") != std::string::npos)
512     {
513         return CalloutType::i2c;
514     }
515 
516     return CalloutType::unknown;
517 }
518 
519 } // namespace util
520 
getCallouts(const std::string & devPath,const std::vector<std::string> & compatibleList)521 std::vector<Callout> getCallouts(const std::string& devPath,
522                                  const std::vector<std::string>& compatibleList)
523 {
524     auto json = util::loadJSON(compatibleList);
525     return util::findCallouts(devPath, json);
526 }
527 
528 std::vector<Callout>
getI2CCallouts(size_t i2cBus,uint8_t i2cAddress,const std::vector<std::string> & compatibleList)529     getI2CCallouts(size_t i2cBus, uint8_t i2cAddress,
530                    const std::vector<std::string>& compatibleList)
531 {
532     auto json = util::loadJSON(compatibleList);
533     return util::calloutI2C(i2cBus, i2cAddress, json);
534 }
535 
536 } // namespace openpower::pels::device_callouts
537