blob: 34406674d2ea3beadc083e26ac1a337275099c26 [file] [log] [blame]
Matt Spinler015e3ad2017-08-01 11:20:47 -05001/**
2 * Copyright © 2017 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 */
Matt Spinlerf0f02b92018-10-25 16:12:43 -050016#include "pmbus.hpp"
17
Matt Spinler015e3ad2017-08-01 11:20:47 -050018#include <phosphor-logging/elog-errors.hpp>
Matt Spinlerf0f02b92018-10-25 16:12:43 -050019#include <phosphor-logging/elog.hpp>
Matt Spinlerceacf942017-10-05 13:55:02 -050020#include <xyz/openbmc_project/Common/Device/error.hpp>
Matt Spinlerf0f02b92018-10-25 16:12:43 -050021#include <xyz/openbmc_project/Common/error.hpp>
Matt Spinler015e3ad2017-08-01 11:20:47 -050022
Brandon Wymand1bc4ce2019-12-13 14:20:34 -060023#include <filesystem>
24#include <fstream>
25
Lei YUab093322019-10-09 16:43:22 +080026namespace phosphor
Matt Spinler015e3ad2017-08-01 11:20:47 -050027{
28namespace pmbus
29{
30
31using namespace phosphor::logging;
32using namespace sdbusplus::xyz::openbmc_project::Common::Error;
Matt Spinlerceacf942017-10-05 13:55:02 -050033using namespace sdbusplus::xyz::openbmc_project::Common::Device::Error;
Brandon Wyman9c7897c2019-03-28 17:42:34 -050034namespace fs = std::filesystem;
Matt Spinler015e3ad2017-08-01 11:20:47 -050035
Matt Spinlerfa23e332018-01-18 11:24:58 -060036/**
37 * @brief Helper to close a file handle
38 */
39struct FileCloser
40{
41 void operator()(FILE* fp) const
42 {
43 fclose(fp);
44 }
45};
46
Matt Spinlerf0f02b92018-10-25 16:12:43 -050047std::string PMBus::insertPageNum(const std::string& templateName, size_t page)
Matt Spinler015e3ad2017-08-01 11:20:47 -050048{
49 auto name = templateName;
50
Matt Spinlerf0f02b92018-10-25 16:12:43 -050051 // insert the page where the P was
Matt Spinler015e3ad2017-08-01 11:20:47 -050052 auto pos = name.find('P');
53 if (pos != std::string::npos)
54 {
55 name.replace(pos, 1, std::to_string(page));
56 }
57
58 return name;
59}
60
Brandon Wymanff5f3392017-08-11 17:43:22 -050061fs::path PMBus::getPath(Type type)
62{
63 switch (type)
64 {
65 default:
Brandon Wymanf855e822017-08-08 18:04:47 -050066 /* fall through */
Brandon Wymanff5f3392017-08-11 17:43:22 -050067 case Type::Base:
68 return basePath;
69 break;
70 case Type::Hwmon:
71 return basePath / "hwmon" / hwmonDir;
72 break;
73 case Type::Debug:
Matt Spinler8f0d9532017-08-21 11:22:37 -050074 return debugPath / "pmbus" / hwmonDir;
75 break;
76 case Type::DeviceDebug:
Matt Spinler4dc46782018-01-04 14:29:16 -060077 {
Matt Spinler8f0d9532017-08-21 11:22:37 -050078 auto dir = driverName + "." + std::to_string(instance);
79 return debugPath / dir;
Brandon Wymanff5f3392017-08-11 17:43:22 -050080 break;
Matt Spinler4dc46782018-01-04 14:29:16 -060081 }
82 case Type::HwmonDeviceDebug:
83 return debugPath / "pmbus" / hwmonDir / getDeviceName();
84 break;
Brandon Wymanff5f3392017-08-11 17:43:22 -050085 }
86}
87
Matt Spinlerba053482018-01-04 14:26:05 -060088std::string PMBus::getDeviceName()
89{
90 std::string name;
91 std::ifstream file;
92 auto path = basePath / "name";
93
Matt Spinlerf0f02b92018-10-25 16:12:43 -050094 file.exceptions(std::ifstream::failbit | std::ifstream::badbit |
Matt Spinlerba053482018-01-04 14:26:05 -060095 std::ifstream::eofbit);
96 try
97 {
98 file.open(path);
99 file >> name;
100 }
101 catch (std::exception& e)
102 {
Jay Meyer6a3fd2c2020-08-25 16:37:16 -0500103 log<level::ERR>((std::string("Unable to read PMBus device name "
104 "PATH=") +
105 path.string())
106 .c_str());
Matt Spinlerba053482018-01-04 14:26:05 -0600107 }
108
109 return name;
110}
111
Matt Spinlerf0f02b92018-10-25 16:12:43 -0500112bool PMBus::readBitInPage(const std::string& name, size_t page, Type type)
Matt Spinler015e3ad2017-08-01 11:20:47 -0500113{
114 auto pagedBit = insertPageNum(name, page);
Matt Spinler57868bc2017-08-03 10:07:41 -0500115 return readBit(pagedBit, type);
Matt Spinler015e3ad2017-08-01 11:20:47 -0500116}
117
Matt Spinler57868bc2017-08-03 10:07:41 -0500118bool PMBus::readBit(const std::string& name, Type type)
Matt Spinler015e3ad2017-08-01 11:20:47 -0500119{
120 unsigned long int value = 0;
121 std::ifstream file;
Brandon Wymanff5f3392017-08-11 17:43:22 -0500122 fs::path path = getPath(type);
Matt Spinler57868bc2017-08-03 10:07:41 -0500123
Matt Spinler015e3ad2017-08-01 11:20:47 -0500124 path /= name;
125
Matt Spinlerf0f02b92018-10-25 16:12:43 -0500126 file.exceptions(std::ifstream::failbit | std::ifstream::badbit |
Matt Spinler015e3ad2017-08-01 11:20:47 -0500127 std::ifstream::eofbit);
128
129 try
130 {
131 char* err = NULL;
132 std::string val{1, '\0'};
133
134 file.open(path);
135 file.read(&val[0], 1);
136
137 value = strtoul(val.c_str(), &err, 10);
138
139 if (*err)
140 {
Jay Meyer6a3fd2c2020-08-25 16:37:16 -0500141 log<level::ERR>((std::string("Invalid character in sysfs file"
142 " FILE=") +
143 path.string() + std::string(" CONTENTS=") + val)
144 .c_str());
Matt Spinler015e3ad2017-08-01 11:20:47 -0500145
Matt Spinlerf0f02b92018-10-25 16:12:43 -0500146 // Catch below and handle as a read failure
Matt Spinler015e3ad2017-08-01 11:20:47 -0500147 elog<InternalFailure>();
148 }
149 }
150 catch (std::exception& e)
151 {
152 auto rc = errno;
153
Jay Meyer6a3fd2c2020-08-25 16:37:16 -0500154 log<level::ERR>((std::string("Failed to read sysfs file "
155 "errno=") +
156 std::to_string(rc) + std::string(" FILENAME=") +
157 path.string())
158 .c_str());
Matt Spinler015e3ad2017-08-01 11:20:47 -0500159
Matt Spinlerceacf942017-10-05 13:55:02 -0500160 using metadata = xyz::openbmc_project::Common::Device::ReadFailure;
161
Matt Spinlerf0f02b92018-10-25 16:12:43 -0500162 elog<ReadFailure>(
163 metadata::CALLOUT_ERRNO(rc),
164 metadata::CALLOUT_DEVICE_PATH(fs::canonical(basePath).c_str()));
Matt Spinler015e3ad2017-08-01 11:20:47 -0500165 }
166
167 return value != 0;
168}
169
Brandon Wyman3b7b38b2017-09-25 16:43:45 -0500170bool PMBus::exists(const std::string& name, Type type)
171{
172 auto path = getPath(type);
173 path /= name;
174 return fs::exists(path);
175}
176
Brandon Wymanf855e822017-08-08 18:04:47 -0500177uint64_t PMBus::read(const std::string& name, Type type)
178{
179 uint64_t data = 0;
180 std::ifstream file;
181 auto path = getPath(type);
182 path /= name;
183
Matt Spinlerf0f02b92018-10-25 16:12:43 -0500184 file.exceptions(std::ifstream::failbit | std::ifstream::badbit |
Brandon Wymanf855e822017-08-08 18:04:47 -0500185 std::ifstream::eofbit);
186
187 try
188 {
189 file.open(path);
190 file >> std::hex >> data;
191 }
192 catch (std::exception& e)
193 {
194 auto rc = errno;
Jay Meyer6a3fd2c2020-08-25 16:37:16 -0500195 log<level::ERR>((std::string("Failed to read sysfs file "
196 "errno=") +
197 std::to_string(rc) + " FILENAME=" + path.string())
198 .c_str());
Brandon Wymanf855e822017-08-08 18:04:47 -0500199
Matt Spinlerceacf942017-10-05 13:55:02 -0500200 using metadata = xyz::openbmc_project::Common::Device::ReadFailure;
Brandon Wymanf855e822017-08-08 18:04:47 -0500201
Matt Spinlerf0f02b92018-10-25 16:12:43 -0500202 elog<ReadFailure>(
203 metadata::CALLOUT_ERRNO(rc),
204 metadata::CALLOUT_DEVICE_PATH(fs::canonical(basePath).c_str()));
Brandon Wymanf855e822017-08-08 18:04:47 -0500205 }
206
207 return data;
208}
209
Matt Spinlerfbae7b62018-01-04 14:33:13 -0600210std::string PMBus::readString(const std::string& name, Type type)
211{
212 std::string data;
213 std::ifstream file;
214 auto path = getPath(type);
215 path /= name;
216
Matt Spinlerf0f02b92018-10-25 16:12:43 -0500217 file.exceptions(std::ifstream::failbit | std::ifstream::badbit |
Matt Spinlerfbae7b62018-01-04 14:33:13 -0600218 std::ifstream::eofbit);
219
220 try
221 {
222 file.open(path);
223 file >> data;
224 }
225 catch (std::exception& e)
226 {
227 auto rc = errno;
Jay Meyer6a3fd2c2020-08-25 16:37:16 -0500228 log<level::ERR>((std::string("Failed to read sysfs file "
229 "errno=") +
230 std::to_string(rc) + " FILENAME=" + path.string())
231 .c_str());
Matt Spinlerfbae7b62018-01-04 14:33:13 -0600232
233 using metadata = xyz::openbmc_project::Common::Device::ReadFailure;
234
Matt Spinlerf0f02b92018-10-25 16:12:43 -0500235 elog<ReadFailure>(
236 metadata::CALLOUT_ERRNO(rc),
237 metadata::CALLOUT_DEVICE_PATH(fs::canonical(basePath).c_str()));
Matt Spinlerfbae7b62018-01-04 14:33:13 -0600238 }
239
240 return data;
241}
242
Matt Spinlerf0f02b92018-10-25 16:12:43 -0500243std::vector<uint8_t> PMBus::readBinary(const std::string& name, Type type,
Matt Spinlerfa23e332018-01-18 11:24:58 -0600244 size_t length)
245{
246 auto path = getPath(type) / name;
247
Matt Spinlerf0f02b92018-10-25 16:12:43 -0500248 // Use C style IO because it's easier to handle telling the difference
249 // between hitting EOF or getting an actual error.
Matt Spinlerfa23e332018-01-18 11:24:58 -0600250 std::unique_ptr<FILE, FileCloser> file{fopen(path.c_str(), "rb")};
251
252 if (file)
253 {
254 std::vector<uint8_t> data(length, 0);
255
Matt Spinlerf0f02b92018-10-25 16:12:43 -0500256 auto bytes =
257 fread(data.data(), sizeof(decltype(data[0])), length, file.get());
Matt Spinlerfa23e332018-01-18 11:24:58 -0600258
259 if (bytes != length)
260 {
Matt Spinlerf0f02b92018-10-25 16:12:43 -0500261 // If hit EOF, just return the amount of data that was read.
Matt Spinlerfa23e332018-01-18 11:24:58 -0600262 if (feof(file.get()))
263 {
Matt Spinlerf0f02b92018-10-25 16:12:43 -0500264 data.erase(data.begin() + bytes, data.end());
Matt Spinlerfa23e332018-01-18 11:24:58 -0600265 }
266 else if (ferror(file.get()))
267 {
268 auto rc = errno;
Jay Meyer6a3fd2c2020-08-25 16:37:16 -0500269 log<level::ERR>((std::string("Failed to read sysfs file "
270 "errno=") +
271 std::to_string(rc) +
272 " FILENAME=" + path.string())
273 .c_str());
Matt Spinlerf0f02b92018-10-25 16:12:43 -0500274 using metadata =
275 xyz::openbmc_project::Common::Device::ReadFailure;
Matt Spinlerfa23e332018-01-18 11:24:58 -0600276
277 elog<ReadFailure>(metadata::CALLOUT_ERRNO(rc),
278 metadata::CALLOUT_DEVICE_PATH(
Matt Spinlerf0f02b92018-10-25 16:12:43 -0500279 fs::canonical(basePath).c_str()));
Matt Spinlerfa23e332018-01-18 11:24:58 -0600280 }
281 }
282 return data;
283 }
284
285 return std::vector<uint8_t>{};
286}
287
Matt Spinler57868bc2017-08-03 10:07:41 -0500288void PMBus::write(const std::string& name, int value, Type type)
Matt Spinler015e3ad2017-08-01 11:20:47 -0500289{
290 std::ofstream file;
Brandon Wymanff5f3392017-08-11 17:43:22 -0500291 fs::path path = getPath(type);
Matt Spinler57868bc2017-08-03 10:07:41 -0500292
Matt Spinler015e3ad2017-08-01 11:20:47 -0500293 path /= name;
294
Matt Spinlerf0f02b92018-10-25 16:12:43 -0500295 file.exceptions(std::ofstream::failbit | std::ofstream::badbit |
Matt Spinler015e3ad2017-08-01 11:20:47 -0500296 std::ofstream::eofbit);
297
298 try
299 {
300 file.open(path);
301 file << value;
302 }
303 catch (const std::exception& e)
304 {
305 auto rc = errno;
Jay Meyer6a3fd2c2020-08-25 16:37:16 -0500306 log<level::ERR>((std::string("Failed to write sysfs file "
307 "errno=") +
308 std::to_string(rc) + " FILENAME=" + path.string())
309 .c_str());
Matt Spinler015e3ad2017-08-01 11:20:47 -0500310
Matt Spinlerceacf942017-10-05 13:55:02 -0500311 using metadata = xyz::openbmc_project::Common::Device::WriteFailure;
312
Matt Spinlerf0f02b92018-10-25 16:12:43 -0500313 elog<WriteFailure>(
314 metadata::CALLOUT_ERRNO(rc),
315 metadata::CALLOUT_DEVICE_PATH(fs::canonical(basePath).c_str()));
Matt Spinler015e3ad2017-08-01 11:20:47 -0500316 }
317}
318
Brandon Wyman59a35792020-06-04 12:37:40 -0500319void PMBus::writeBinary(const std::string& name, std::vector<uint8_t> data,
320 Type type)
321{
322 std::ofstream file;
323 fs::path path = getPath(type);
324
325 path /= name;
326
327 file.exceptions(std::ofstream::failbit | std::ofstream::badbit |
328 std::ofstream::eofbit);
329
330 try
331 {
332 // I need to specify binary mode when I construct the ofstream
333 file.open(path, std::ios::out | std::ios_base::binary);
Jay Meyer6a3fd2c2020-08-25 16:37:16 -0500334 log<level::DEBUG>(std::string("Write data to sysfs file "
335 "FILENAME=" +
336 path.string())
337 .c_str());
Brandon Wyman59a35792020-06-04 12:37:40 -0500338 file.write(reinterpret_cast<const char*>(&data[0]), data.size());
339 }
340 catch (const std::exception& e)
341 {
342 auto rc = errno;
Jay Meyer6a3fd2c2020-08-25 16:37:16 -0500343 log<level::ERR>(
344 (std::string("Failed to write binary data to sysfs file "
345 "errno=") +
346 std::to_string(rc) + " FILENAME=" + path.string())
347 .c_str());
Brandon Wyman59a35792020-06-04 12:37:40 -0500348
349 using metadata = xyz::openbmc_project::Common::Device::WriteFailure;
350
351 elog<WriteFailure>(
352 metadata::CALLOUT_ERRNO(rc),
353 metadata::CALLOUT_DEVICE_PATH(fs::canonical(basePath).c_str()));
354 }
355}
356
Brandon Wymanff5f3392017-08-11 17:43:22 -0500357void PMBus::findHwmonDir()
Matt Spinler57868bc2017-08-03 10:07:41 -0500358{
359 fs::path path{basePath};
360 path /= "hwmon";
361
Brandon Wymanaad73e92017-08-16 16:27:54 -0500362 // Make sure the directory exists, otherwise for things that can be
363 // dynamically present or not present an exception will be thrown if the
364 // hwmon directory is not there, resulting in a program termination.
365 if (fs::is_directory(path))
Matt Spinler57868bc2017-08-03 10:07:41 -0500366 {
Matt Spinlerf0f02b92018-10-25 16:12:43 -0500367 // look for <basePath>/hwmon/hwmonN/
Brandon Wymanaad73e92017-08-16 16:27:54 -0500368 for (auto& f : fs::directory_iterator(path))
Matt Spinler57868bc2017-08-03 10:07:41 -0500369 {
Brandon Wymanaad73e92017-08-16 16:27:54 -0500370 if ((f.path().filename().string().find("hwmon") !=
371 std::string::npos) &&
372 (fs::is_directory(f.path())))
373 {
374 hwmonDir = f.path().filename();
375 break;
376 }
Matt Spinler57868bc2017-08-03 10:07:41 -0500377 }
378 }
379
Matt Spinlerf0f02b92018-10-25 16:12:43 -0500380 // Don't really want to crash here, just log it
381 // and let accesses fail later
Brandon Wymanff5f3392017-08-11 17:43:22 -0500382 if (hwmonDir.empty())
Matt Spinler57868bc2017-08-03 10:07:41 -0500383 {
Jay Meyer6a3fd2c2020-08-25 16:37:16 -0500384 log<level::INFO>(std::string("Unable to find hwmon directory "
385 "in device base path"
386 " DEVICE_PATH=" +
387 basePath.string())
388 .c_str());
Matt Spinler57868bc2017-08-03 10:07:41 -0500389 }
Matt Spinler57868bc2017-08-03 10:07:41 -0500390}
391
Brandon Wyman8d195772020-01-27 15:03:51 -0600392std::unique_ptr<PMBusBase> PMBus::createPMBus(std::uint8_t bus,
393 const std::string& address)
394{
395 const std::string physpath = {"/sys/bus/i2c/devices/" +
396 std::to_string(bus) + "-" + address};
397 auto interface = std::make_unique<PMBus>(physpath);
398
399 return interface;
400}
401
402std::unique_ptr<PMBusBase> createPMBus(std::uint8_t bus,
403 const std::string& address)
404{
405 return PMBus::createPMBus(bus, address);
406}
407
Matt Spinlerf0f02b92018-10-25 16:12:43 -0500408} // namespace pmbus
Lei YUab093322019-10-09 16:43:22 +0800409} // namespace phosphor