blob: 8b488d271f1080dbfa5c30809d88a7faefe02e95 [file] [log] [blame]
Alexander Hansen40fb5492025-10-28 17:56:12 +01001// SPDX-License-Identifier: Apache-2.0
2// SPDX-FileCopyrightText: Copyright 2020 IBM Corporation
3
Matt Spinler18c42b02020-06-02 15:59:50 -05004#include "device_callouts.hpp"
5
6#include "paths.hpp"
7
Arya K Padman5bc26532024-04-10 06:19:25 -05008#include <phosphor-logging/lg2.hpp>
Patrick Williams2544b412022-10-04 08:41:06 -05009
10#include <fstream>
Matt Spinler18c42b02020-06-02 15:59:50 -050011#include <regex>
12
13namespace openpower::pels::device_callouts
14{
15
16constexpr auto debugFilePath = "/etc/phosphor-logging/";
17constexpr auto calloutFileSuffix = "_dev_callouts.json";
18
19namespace fs = std::filesystem;
Matt Spinler18c42b02020-06-02 15:59:50 -050020
21namespace util
22{
23
24fs::path getJSONFilename(const std::vector<std::string>& compatibleList)
25{
Matt Spinler0f8a7382020-07-22 12:58:45 -050026 std::vector<std::string> names{compatibleList};
Matt Spinler18c42b02020-06-02 15:59:50 -050027 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
Matt Spinler0f8a7382020-07-22 12:58:45 -050033 // 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)
Matt Spinler18c42b02020-06-02 15:59:50 -050038 {
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 {
Arya K Padman5bc26532024-04-10 06:19:25 -050046 lg2::info("Found device callout debug file");
Matt Spinler18c42b02020-06-02 15:59:50 -050047 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 */
78nlohmann::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
Matt Spinler44c0a642020-06-03 11:28:25 -050085std::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
114std::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
Patrick Williams25291152025-02-01 08:21:42 -0500147std::tuple<std::string, std::tuple<size_t, uint8_t>> getFSII2CSearchKeys(
148 const std::string& devPath)
Matt Spinler44c0a642020-06-03 11:28:25 -0500149{
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
158size_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
180std::tuple<std::string, size_t> getFSISPISearchKeys(const std::string& devPath)
181{
Matt Spinler44c0a642020-06-03 11:28:25 -0500182 // 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
Matt Spinler6bc74ae2020-06-03 15:13:59 -0500189/**
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 */
205std::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 */
Patrick Williams075c7922024-08-16 15:19:49 -0400264std::vector<device_callouts::Callout> calloutI2C(
265 size_t i2cBus, uint8_t i2cAddress, const nlohmann::json& calloutJSON)
Matt Spinler18c42b02020-06-02 15:59:50 -0500266{
Matt Spinler6bc74ae2020-06-03 15:13:59 -0500267 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 }
Matt Spinler18c42b02020-06-02 15:59:50 -0500292}
293
Matt Spinler6bc74ae2020-06-03 15:13:59 -0500294/**
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 */
Patrick Williams075c7922024-08-16 15:19:49 -0400302std::vector<device_callouts::Callout> calloutI2CUsingPath(
303 const std::string& devPath, const nlohmann::json& calloutJSON)
Matt Spinler6bc74ae2020-06-03 15:13:59 -0500304{
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 */
Patrick Williams25291152025-02-01 08:21:42 -0500318std::vector<device_callouts::Callout> calloutFSI(
319 const std::string& devPath, const nlohmann::json& calloutJSON)
Matt Spinler6bc74ae2020-06-03 15:13:59 -0500320{
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 */
Patrick Williams25291152025-02-01 08:21:42 -0500350std::vector<device_callouts::Callout> calloutFSII2C(
351 const std::string& devPath, const nlohmann::json& calloutJSON)
Matt Spinler6bc74ae2020-06-03 15:13:59 -0500352{
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 */
Patrick Williams25291152025-02-01 08:21:42 -0500396std::vector<device_callouts::Callout> calloutFSISPI(
397 const std::string& devPath, const nlohmann::json& calloutJSON)
Matt Spinler6bc74ae2020-06-03 15:13:59 -0500398{
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 */
Patrick Williams25291152025-02-01 08:21:42 -0500436std::vector<device_callouts::Callout> findCallouts(const std::string& devPath,
437 const nlohmann::json& json)
Matt Spinler18c42b02020-06-02 15:59:50 -0500438{
439 std::vector<Callout> callouts;
Matt Spinlera3070892020-06-03 10:52:32 -0500440 fs::path path;
Matt Spinler18c42b02020-06-02 15:59:50 -0500441
Matt Spinlera3070892020-06-03 10:52:32 -0500442 // 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:
Matt Spinler6bc74ae2020-06-03 15:13:59 -0500456 callouts = calloutI2CUsingPath(path, json);
Matt Spinlera3070892020-06-03 10:52:32 -0500457 break;
458 case util::CalloutType::fsi:
Matt Spinler6bc74ae2020-06-03 15:13:59 -0500459 callouts = calloutFSI(path, json);
Matt Spinlera3070892020-06-03 10:52:32 -0500460 break;
461 case util::CalloutType::fsii2c:
Matt Spinler6bc74ae2020-06-03 15:13:59 -0500462 callouts = calloutFSII2C(path, json);
Matt Spinlera3070892020-06-03 10:52:32 -0500463 break;
464 case util::CalloutType::fsispi:
Matt Spinler6bc74ae2020-06-03 15:13:59 -0500465 callouts = calloutFSISPI(path, json);
Matt Spinlera3070892020-06-03 10:52:32 -0500466 break;
467 default:
Patrick Williams075c7922024-08-16 15:19:49 -0400468 std::string msg =
469 "Could not get callout type from device path: " + path.string();
Matt Spinlera3070892020-06-03 10:52:32 -0500470 throw std::invalid_argument{msg.c_str()};
471 break;
472 }
Matt Spinler18c42b02020-06-02 15:59:50 -0500473
474 return callouts;
475}
476
Matt Spinlera3070892020-06-03 10:52:32 -0500477CalloutType 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
Matt Spinler70e8a112024-09-24 14:27:42 -0500497 if (devPath.find("i2c-") != std::string::npos)
Matt Spinlera3070892020-06-03 10:52:32 -0500498 {
499 return CalloutType::i2c;
500 }
501
502 return CalloutType::unknown;
503}
504
Matt Spinler18c42b02020-06-02 15:59:50 -0500505} // namespace util
506
507std::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
Patrick Williams25291152025-02-01 08:21:42 -0500514std::vector<Callout> getI2CCallouts(
515 size_t i2cBus, uint8_t i2cAddress,
516 const std::vector<std::string>& compatibleList)
Matt Spinler18c42b02020-06-02 15:59:50 -0500517{
518 auto json = util::loadJSON(compatibleList);
519 return util::calloutI2C(i2cBus, i2cAddress, json);
520}
521
522} // namespace openpower::pels::device_callouts