blob: b3434247a55c70ee20a9ba245c2cd0c3cfbc3dd6 [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 Spinler18c42b02020-06-02 15:59:50 -0500197std::vector<device_callouts::Callout>
198 calloutI2C(size_t i2cBus, uint8_t i2cAddress,
199 const nlohmann::json& calloutJSON)
200{
201 // TODO
202 return {};
203}
204
205std::vector<device_callouts::Callout> findCallouts(const std::string& devPath,
206 const nlohmann::json& json)
207{
208 std::vector<Callout> callouts;
Matt Spinlera3070892020-06-03 10:52:32 -0500209 fs::path path;
Matt Spinler18c42b02020-06-02 15:59:50 -0500210
Matt Spinlera3070892020-06-03 10:52:32 -0500211 // Gives the /sys/devices/platform/ path
212 try
213 {
214 path = fs::canonical(devPath);
215 }
216 catch (const fs::filesystem_error& e)
217 {
218 // Path not there, still try to do the callout
219 path = devPath;
220 }
221
222 switch (util::getCalloutType(path))
223 {
224 case util::CalloutType::i2c:
225 // callouts = calloutI2CUsingPath(errnoValue, path, json);
226 break;
227 case util::CalloutType::fsi:
228 // callouts = calloutFSI(errnoValue, path, json);
229 break;
230 case util::CalloutType::fsii2c:
231 // callouts = calloutFSII2C(errnoValue, path, json);
232 break;
233 case util::CalloutType::fsispi:
234 // callouts = calloutFSISPI(errnoValue, path, json);
235 break;
236 default:
237 std::string msg =
238 "Could not get callout type from device path: " + path.string();
239 throw std::invalid_argument{msg.c_str()};
240 break;
241 }
Matt Spinler18c42b02020-06-02 15:59:50 -0500242
243 return callouts;
244}
245
Matt Spinlera3070892020-06-03 10:52:32 -0500246CalloutType getCalloutType(const std::string& devPath)
247{
248 if ((devPath.find("fsi-master") != std::string::npos) &&
249 (devPath.find("i2c-") != std::string::npos))
250 {
251 return CalloutType::fsii2c;
252 }
253
254 if ((devPath.find("fsi-master") != std::string::npos) &&
255 (devPath.find("spi") != std::string::npos))
256 {
257 return CalloutType::fsispi;
258 }
259
260 // Treat anything else FSI related as plain FSI
261 if (devPath.find("fsi-master") != std::string::npos)
262 {
263 return CalloutType::fsi;
264 }
265
266 if (devPath.find("i2c-bus/i2c-") != std::string::npos)
267 {
268 return CalloutType::i2c;
269 }
270
271 return CalloutType::unknown;
272}
273
Matt Spinler18c42b02020-06-02 15:59:50 -0500274} // namespace util
275
276std::vector<Callout> getCallouts(const std::string& devPath,
277 const std::vector<std::string>& compatibleList)
278{
279 auto json = util::loadJSON(compatibleList);
280 return util::findCallouts(devPath, json);
281}
282
283std::vector<Callout>
284 getI2CCallouts(size_t i2cBus, uint8_t i2cAddress,
285 const std::vector<std::string>& compatibleList)
286{
287 auto json = util::loadJSON(compatibleList);
288 return util::calloutI2C(i2cBus, i2cAddress, json);
289}
290
291} // namespace openpower::pels::device_callouts