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