Matt Spinler | 015e3ad | 2017-08-01 11:20:47 -0500 | [diff] [blame] | 1 | /** |
| 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 Spinler | f0f02b9 | 2018-10-25 16:12:43 -0500 | [diff] [blame] | 16 | #include "pmbus.hpp" |
| 17 | |
Matt Spinler | 015e3ad | 2017-08-01 11:20:47 -0500 | [diff] [blame] | 18 | #include <phosphor-logging/elog-errors.hpp> |
Matt Spinler | f0f02b9 | 2018-10-25 16:12:43 -0500 | [diff] [blame] | 19 | #include <phosphor-logging/elog.hpp> |
Matt Spinler | ceacf94 | 2017-10-05 13:55:02 -0500 | [diff] [blame] | 20 | #include <xyz/openbmc_project/Common/Device/error.hpp> |
Matt Spinler | f0f02b9 | 2018-10-25 16:12:43 -0500 | [diff] [blame] | 21 | #include <xyz/openbmc_project/Common/error.hpp> |
Matt Spinler | 015e3ad | 2017-08-01 11:20:47 -0500 | [diff] [blame] | 22 | |
Brandon Wyman | d1bc4ce | 2019-12-13 14:20:34 -0600 | [diff] [blame] | 23 | #include <filesystem> |
| 24 | #include <fstream> |
| 25 | |
Lei YU | ab09332 | 2019-10-09 16:43:22 +0800 | [diff] [blame] | 26 | namespace phosphor |
Matt Spinler | 015e3ad | 2017-08-01 11:20:47 -0500 | [diff] [blame] | 27 | { |
| 28 | namespace pmbus |
| 29 | { |
| 30 | |
| 31 | using namespace phosphor::logging; |
| 32 | using namespace sdbusplus::xyz::openbmc_project::Common::Error; |
Matt Spinler | ceacf94 | 2017-10-05 13:55:02 -0500 | [diff] [blame] | 33 | using namespace sdbusplus::xyz::openbmc_project::Common::Device::Error; |
Brandon Wyman | 9c7897c | 2019-03-28 17:42:34 -0500 | [diff] [blame] | 34 | namespace fs = std::filesystem; |
Matt Spinler | 015e3ad | 2017-08-01 11:20:47 -0500 | [diff] [blame] | 35 | |
Matt Spinler | fa23e33 | 2018-01-18 11:24:58 -0600 | [diff] [blame] | 36 | /** |
| 37 | * @brief Helper to close a file handle |
| 38 | */ |
| 39 | struct FileCloser |
| 40 | { |
| 41 | void operator()(FILE* fp) const |
| 42 | { |
| 43 | fclose(fp); |
| 44 | } |
| 45 | }; |
| 46 | |
Matt Spinler | f0f02b9 | 2018-10-25 16:12:43 -0500 | [diff] [blame] | 47 | std::string PMBus::insertPageNum(const std::string& templateName, size_t page) |
Matt Spinler | 015e3ad | 2017-08-01 11:20:47 -0500 | [diff] [blame] | 48 | { |
| 49 | auto name = templateName; |
| 50 | |
Matt Spinler | f0f02b9 | 2018-10-25 16:12:43 -0500 | [diff] [blame] | 51 | // insert the page where the P was |
Matt Spinler | 015e3ad | 2017-08-01 11:20:47 -0500 | [diff] [blame] | 52 | 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 Wyman | ff5f339 | 2017-08-11 17:43:22 -0500 | [diff] [blame] | 61 | fs::path PMBus::getPath(Type type) |
| 62 | { |
| 63 | switch (type) |
| 64 | { |
| 65 | default: |
Brandon Wyman | f855e82 | 2017-08-08 18:04:47 -0500 | [diff] [blame] | 66 | /* fall through */ |
Brandon Wyman | ff5f339 | 2017-08-11 17:43:22 -0500 | [diff] [blame] | 67 | case Type::Base: |
| 68 | return basePath; |
| 69 | break; |
| 70 | case Type::Hwmon: |
| 71 | return basePath / "hwmon" / hwmonDir; |
| 72 | break; |
| 73 | case Type::Debug: |
Matt Spinler | 8f0d953 | 2017-08-21 11:22:37 -0500 | [diff] [blame] | 74 | return debugPath / "pmbus" / hwmonDir; |
| 75 | break; |
| 76 | case Type::DeviceDebug: |
Matt Spinler | 4dc4678 | 2018-01-04 14:29:16 -0600 | [diff] [blame] | 77 | { |
Matt Spinler | 8f0d953 | 2017-08-21 11:22:37 -0500 | [diff] [blame] | 78 | auto dir = driverName + "." + std::to_string(instance); |
| 79 | return debugPath / dir; |
Brandon Wyman | ff5f339 | 2017-08-11 17:43:22 -0500 | [diff] [blame] | 80 | break; |
Matt Spinler | 4dc4678 | 2018-01-04 14:29:16 -0600 | [diff] [blame] | 81 | } |
| 82 | case Type::HwmonDeviceDebug: |
| 83 | return debugPath / "pmbus" / hwmonDir / getDeviceName(); |
| 84 | break; |
Brandon Wyman | ff5f339 | 2017-08-11 17:43:22 -0500 | [diff] [blame] | 85 | } |
| 86 | } |
| 87 | |
Matt Spinler | ba05348 | 2018-01-04 14:26:05 -0600 | [diff] [blame] | 88 | std::string PMBus::getDeviceName() |
| 89 | { |
| 90 | std::string name; |
| 91 | std::ifstream file; |
| 92 | auto path = basePath / "name"; |
| 93 | |
Matt Spinler | f0f02b9 | 2018-10-25 16:12:43 -0500 | [diff] [blame] | 94 | file.exceptions(std::ifstream::failbit | std::ifstream::badbit | |
Matt Spinler | ba05348 | 2018-01-04 14:26:05 -0600 | [diff] [blame] | 95 | std::ifstream::eofbit); |
| 96 | try |
| 97 | { |
| 98 | file.open(path); |
| 99 | file >> name; |
| 100 | } |
Patrick Williams | c1d4de5 | 2021-10-06 12:45:57 -0500 | [diff] [blame] | 101 | catch (const std::exception& e) |
Matt Spinler | ba05348 | 2018-01-04 14:26:05 -0600 | [diff] [blame] | 102 | { |
Jay Meyer | 6a3fd2c | 2020-08-25 16:37:16 -0500 | [diff] [blame] | 103 | log<level::ERR>((std::string("Unable to read PMBus device name " |
| 104 | "PATH=") + |
| 105 | path.string()) |
| 106 | .c_str()); |
Matt Spinler | ba05348 | 2018-01-04 14:26:05 -0600 | [diff] [blame] | 107 | } |
| 108 | |
| 109 | return name; |
| 110 | } |
| 111 | |
Matt Spinler | f0f02b9 | 2018-10-25 16:12:43 -0500 | [diff] [blame] | 112 | bool PMBus::readBitInPage(const std::string& name, size_t page, Type type) |
Matt Spinler | 015e3ad | 2017-08-01 11:20:47 -0500 | [diff] [blame] | 113 | { |
| 114 | auto pagedBit = insertPageNum(name, page); |
Matt Spinler | 57868bc | 2017-08-03 10:07:41 -0500 | [diff] [blame] | 115 | return readBit(pagedBit, type); |
Matt Spinler | 015e3ad | 2017-08-01 11:20:47 -0500 | [diff] [blame] | 116 | } |
| 117 | |
Matt Spinler | 57868bc | 2017-08-03 10:07:41 -0500 | [diff] [blame] | 118 | bool PMBus::readBit(const std::string& name, Type type) |
Matt Spinler | 015e3ad | 2017-08-01 11:20:47 -0500 | [diff] [blame] | 119 | { |
| 120 | unsigned long int value = 0; |
| 121 | std::ifstream file; |
Brandon Wyman | ff5f339 | 2017-08-11 17:43:22 -0500 | [diff] [blame] | 122 | fs::path path = getPath(type); |
Matt Spinler | 57868bc | 2017-08-03 10:07:41 -0500 | [diff] [blame] | 123 | |
Matt Spinler | 015e3ad | 2017-08-01 11:20:47 -0500 | [diff] [blame] | 124 | path /= name; |
| 125 | |
Matt Spinler | f0f02b9 | 2018-10-25 16:12:43 -0500 | [diff] [blame] | 126 | file.exceptions(std::ifstream::failbit | std::ifstream::badbit | |
Matt Spinler | 015e3ad | 2017-08-01 11:20:47 -0500 | [diff] [blame] | 127 | 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 Meyer | 6a3fd2c | 2020-08-25 16:37:16 -0500 | [diff] [blame] | 141 | log<level::ERR>((std::string("Invalid character in sysfs file" |
| 142 | " FILE=") + |
| 143 | path.string() + std::string(" CONTENTS=") + val) |
| 144 | .c_str()); |
Matt Spinler | 015e3ad | 2017-08-01 11:20:47 -0500 | [diff] [blame] | 145 | |
Matt Spinler | f0f02b9 | 2018-10-25 16:12:43 -0500 | [diff] [blame] | 146 | // Catch below and handle as a read failure |
Matt Spinler | 015e3ad | 2017-08-01 11:20:47 -0500 | [diff] [blame] | 147 | elog<InternalFailure>(); |
| 148 | } |
| 149 | } |
Patrick Williams | c1d4de5 | 2021-10-06 12:45:57 -0500 | [diff] [blame] | 150 | catch (const std::exception& e) |
Matt Spinler | 015e3ad | 2017-08-01 11:20:47 -0500 | [diff] [blame] | 151 | { |
| 152 | auto rc = errno; |
| 153 | |
Jay Meyer | 6a3fd2c | 2020-08-25 16:37:16 -0500 | [diff] [blame] | 154 | 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 Spinler | 015e3ad | 2017-08-01 11:20:47 -0500 | [diff] [blame] | 159 | |
Matt Spinler | ceacf94 | 2017-10-05 13:55:02 -0500 | [diff] [blame] | 160 | using metadata = xyz::openbmc_project::Common::Device::ReadFailure; |
| 161 | |
Matt Spinler | f0f02b9 | 2018-10-25 16:12:43 -0500 | [diff] [blame] | 162 | elog<ReadFailure>( |
| 163 | metadata::CALLOUT_ERRNO(rc), |
| 164 | metadata::CALLOUT_DEVICE_PATH(fs::canonical(basePath).c_str())); |
Matt Spinler | 015e3ad | 2017-08-01 11:20:47 -0500 | [diff] [blame] | 165 | } |
| 166 | |
| 167 | return value != 0; |
| 168 | } |
| 169 | |
Brandon Wyman | 3b7b38b | 2017-09-25 16:43:45 -0500 | [diff] [blame] | 170 | bool 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 Wyman | 32453e9 | 2021-12-15 19:00:14 +0000 | [diff] [blame] | 177 | uint64_t PMBus::read(const std::string& name, Type type, bool errTrace) |
Brandon Wyman | f855e82 | 2017-08-08 18:04:47 -0500 | [diff] [blame] | 178 | { |
| 179 | uint64_t data = 0; |
| 180 | std::ifstream file; |
| 181 | auto path = getPath(type); |
| 182 | path /= name; |
| 183 | |
Matt Spinler | f0f02b9 | 2018-10-25 16:12:43 -0500 | [diff] [blame] | 184 | file.exceptions(std::ifstream::failbit | std::ifstream::badbit | |
Brandon Wyman | f855e82 | 2017-08-08 18:04:47 -0500 | [diff] [blame] | 185 | std::ifstream::eofbit); |
| 186 | |
| 187 | try |
| 188 | { |
| 189 | file.open(path); |
| 190 | file >> std::hex >> data; |
| 191 | } |
Patrick Williams | c1d4de5 | 2021-10-06 12:45:57 -0500 | [diff] [blame] | 192 | catch (const std::exception& e) |
Brandon Wyman | f855e82 | 2017-08-08 18:04:47 -0500 | [diff] [blame] | 193 | { |
| 194 | auto rc = errno; |
Brandon Wyman | f855e82 | 2017-08-08 18:04:47 -0500 | [diff] [blame] | 195 | |
Brandon Wyman | 32453e9 | 2021-12-15 19:00:14 +0000 | [diff] [blame] | 196 | if (errTrace) |
| 197 | { |
| 198 | log<level::ERR>((std::string("Failed to read sysfs file " |
| 199 | "errno=") + |
| 200 | std::to_string(rc) + " FILENAME=" + path.string()) |
| 201 | .c_str()); |
Brandon Wyman | f855e82 | 2017-08-08 18:04:47 -0500 | [diff] [blame] | 202 | |
Brandon Wyman | 32453e9 | 2021-12-15 19:00:14 +0000 | [diff] [blame] | 203 | using metadata = xyz::openbmc_project::Common::Device::ReadFailure; |
| 204 | |
| 205 | elog<ReadFailure>( |
| 206 | metadata::CALLOUT_ERRNO(rc), |
| 207 | metadata::CALLOUT_DEVICE_PATH(fs::canonical(basePath).c_str())); |
| 208 | } |
| 209 | else |
| 210 | { |
| 211 | throw ReadFailure(); |
| 212 | } |
Brandon Wyman | f855e82 | 2017-08-08 18:04:47 -0500 | [diff] [blame] | 213 | } |
| 214 | |
| 215 | return data; |
| 216 | } |
| 217 | |
Matt Spinler | fbae7b6 | 2018-01-04 14:33:13 -0600 | [diff] [blame] | 218 | std::string PMBus::readString(const std::string& name, Type type) |
| 219 | { |
| 220 | std::string data; |
| 221 | std::ifstream file; |
| 222 | auto path = getPath(type); |
| 223 | path /= name; |
| 224 | |
Matt Spinler | f0f02b9 | 2018-10-25 16:12:43 -0500 | [diff] [blame] | 225 | file.exceptions(std::ifstream::failbit | std::ifstream::badbit | |
Matt Spinler | fbae7b6 | 2018-01-04 14:33:13 -0600 | [diff] [blame] | 226 | std::ifstream::eofbit); |
| 227 | |
| 228 | try |
| 229 | { |
| 230 | file.open(path); |
| 231 | file >> data; |
| 232 | } |
Patrick Williams | c1d4de5 | 2021-10-06 12:45:57 -0500 | [diff] [blame] | 233 | catch (const std::exception& e) |
Matt Spinler | fbae7b6 | 2018-01-04 14:33:13 -0600 | [diff] [blame] | 234 | { |
| 235 | auto rc = errno; |
Jay Meyer | 6a3fd2c | 2020-08-25 16:37:16 -0500 | [diff] [blame] | 236 | log<level::ERR>((std::string("Failed to read sysfs file " |
| 237 | "errno=") + |
| 238 | std::to_string(rc) + " FILENAME=" + path.string()) |
| 239 | .c_str()); |
Matt Spinler | fbae7b6 | 2018-01-04 14:33:13 -0600 | [diff] [blame] | 240 | |
| 241 | using metadata = xyz::openbmc_project::Common::Device::ReadFailure; |
| 242 | |
Matt Spinler | f0f02b9 | 2018-10-25 16:12:43 -0500 | [diff] [blame] | 243 | elog<ReadFailure>( |
| 244 | metadata::CALLOUT_ERRNO(rc), |
| 245 | metadata::CALLOUT_DEVICE_PATH(fs::canonical(basePath).c_str())); |
Matt Spinler | fbae7b6 | 2018-01-04 14:33:13 -0600 | [diff] [blame] | 246 | } |
| 247 | |
| 248 | return data; |
| 249 | } |
| 250 | |
Matt Spinler | f0f02b9 | 2018-10-25 16:12:43 -0500 | [diff] [blame] | 251 | std::vector<uint8_t> PMBus::readBinary(const std::string& name, Type type, |
Matt Spinler | fa23e33 | 2018-01-18 11:24:58 -0600 | [diff] [blame] | 252 | size_t length) |
| 253 | { |
| 254 | auto path = getPath(type) / name; |
| 255 | |
Matt Spinler | f0f02b9 | 2018-10-25 16:12:43 -0500 | [diff] [blame] | 256 | // Use C style IO because it's easier to handle telling the difference |
| 257 | // between hitting EOF or getting an actual error. |
Matt Spinler | fa23e33 | 2018-01-18 11:24:58 -0600 | [diff] [blame] | 258 | std::unique_ptr<FILE, FileCloser> file{fopen(path.c_str(), "rb")}; |
| 259 | |
| 260 | if (file) |
| 261 | { |
| 262 | std::vector<uint8_t> data(length, 0); |
| 263 | |
Patrick Williams | 48781ae | 2023-05-10 07:50:50 -0500 | [diff] [blame] | 264 | auto bytes = fread(data.data(), sizeof(decltype(data[0])), length, |
| 265 | file.get()); |
Matt Spinler | fa23e33 | 2018-01-18 11:24:58 -0600 | [diff] [blame] | 266 | |
| 267 | if (bytes != length) |
| 268 | { |
Matt Spinler | f0f02b9 | 2018-10-25 16:12:43 -0500 | [diff] [blame] | 269 | // If hit EOF, just return the amount of data that was read. |
Matt Spinler | fa23e33 | 2018-01-18 11:24:58 -0600 | [diff] [blame] | 270 | if (feof(file.get())) |
| 271 | { |
Matt Spinler | f0f02b9 | 2018-10-25 16:12:43 -0500 | [diff] [blame] | 272 | data.erase(data.begin() + bytes, data.end()); |
Matt Spinler | fa23e33 | 2018-01-18 11:24:58 -0600 | [diff] [blame] | 273 | } |
| 274 | else if (ferror(file.get())) |
| 275 | { |
| 276 | auto rc = errno; |
Jay Meyer | 6a3fd2c | 2020-08-25 16:37:16 -0500 | [diff] [blame] | 277 | log<level::ERR>((std::string("Failed to read sysfs file " |
| 278 | "errno=") + |
| 279 | std::to_string(rc) + |
| 280 | " FILENAME=" + path.string()) |
| 281 | .c_str()); |
Matt Spinler | f0f02b9 | 2018-10-25 16:12:43 -0500 | [diff] [blame] | 282 | using metadata = |
| 283 | xyz::openbmc_project::Common::Device::ReadFailure; |
Matt Spinler | fa23e33 | 2018-01-18 11:24:58 -0600 | [diff] [blame] | 284 | |
| 285 | elog<ReadFailure>(metadata::CALLOUT_ERRNO(rc), |
| 286 | metadata::CALLOUT_DEVICE_PATH( |
Matt Spinler | f0f02b9 | 2018-10-25 16:12:43 -0500 | [diff] [blame] | 287 | fs::canonical(basePath).c_str())); |
Matt Spinler | fa23e33 | 2018-01-18 11:24:58 -0600 | [diff] [blame] | 288 | } |
| 289 | } |
| 290 | return data; |
| 291 | } |
| 292 | |
| 293 | return std::vector<uint8_t>{}; |
| 294 | } |
| 295 | |
Matt Spinler | 57868bc | 2017-08-03 10:07:41 -0500 | [diff] [blame] | 296 | void PMBus::write(const std::string& name, int value, Type type) |
Matt Spinler | 015e3ad | 2017-08-01 11:20:47 -0500 | [diff] [blame] | 297 | { |
| 298 | std::ofstream file; |
Brandon Wyman | ff5f339 | 2017-08-11 17:43:22 -0500 | [diff] [blame] | 299 | fs::path path = getPath(type); |
Matt Spinler | 57868bc | 2017-08-03 10:07:41 -0500 | [diff] [blame] | 300 | |
Matt Spinler | 015e3ad | 2017-08-01 11:20:47 -0500 | [diff] [blame] | 301 | path /= name; |
| 302 | |
Matt Spinler | f0f02b9 | 2018-10-25 16:12:43 -0500 | [diff] [blame] | 303 | file.exceptions(std::ofstream::failbit | std::ofstream::badbit | |
Matt Spinler | 015e3ad | 2017-08-01 11:20:47 -0500 | [diff] [blame] | 304 | std::ofstream::eofbit); |
| 305 | |
| 306 | try |
| 307 | { |
| 308 | file.open(path); |
| 309 | file << value; |
| 310 | } |
| 311 | catch (const std::exception& e) |
| 312 | { |
| 313 | auto rc = errno; |
Jay Meyer | 6a3fd2c | 2020-08-25 16:37:16 -0500 | [diff] [blame] | 314 | log<level::ERR>((std::string("Failed to write sysfs file " |
| 315 | "errno=") + |
| 316 | std::to_string(rc) + " FILENAME=" + path.string()) |
| 317 | .c_str()); |
Matt Spinler | 015e3ad | 2017-08-01 11:20:47 -0500 | [diff] [blame] | 318 | |
Matt Spinler | ceacf94 | 2017-10-05 13:55:02 -0500 | [diff] [blame] | 319 | using metadata = xyz::openbmc_project::Common::Device::WriteFailure; |
| 320 | |
Matt Spinler | f0f02b9 | 2018-10-25 16:12:43 -0500 | [diff] [blame] | 321 | elog<WriteFailure>( |
| 322 | metadata::CALLOUT_ERRNO(rc), |
| 323 | metadata::CALLOUT_DEVICE_PATH(fs::canonical(basePath).c_str())); |
Matt Spinler | 015e3ad | 2017-08-01 11:20:47 -0500 | [diff] [blame] | 324 | } |
| 325 | } |
| 326 | |
Brandon Wyman | 59a3579 | 2020-06-04 12:37:40 -0500 | [diff] [blame] | 327 | void PMBus::writeBinary(const std::string& name, std::vector<uint8_t> data, |
| 328 | Type type) |
| 329 | { |
| 330 | std::ofstream file; |
| 331 | fs::path path = getPath(type); |
| 332 | |
| 333 | path /= name; |
| 334 | |
| 335 | file.exceptions(std::ofstream::failbit | std::ofstream::badbit | |
| 336 | std::ofstream::eofbit); |
| 337 | |
| 338 | try |
| 339 | { |
| 340 | // I need to specify binary mode when I construct the ofstream |
| 341 | file.open(path, std::ios::out | std::ios_base::binary); |
Jay Meyer | 6a3fd2c | 2020-08-25 16:37:16 -0500 | [diff] [blame] | 342 | log<level::DEBUG>(std::string("Write data to sysfs file " |
| 343 | "FILENAME=" + |
| 344 | path.string()) |
| 345 | .c_str()); |
Brandon Wyman | 59a3579 | 2020-06-04 12:37:40 -0500 | [diff] [blame] | 346 | file.write(reinterpret_cast<const char*>(&data[0]), data.size()); |
| 347 | } |
| 348 | catch (const std::exception& e) |
| 349 | { |
| 350 | auto rc = errno; |
Jay Meyer | 6a3fd2c | 2020-08-25 16:37:16 -0500 | [diff] [blame] | 351 | log<level::ERR>( |
| 352 | (std::string("Failed to write binary data to sysfs file " |
| 353 | "errno=") + |
| 354 | std::to_string(rc) + " FILENAME=" + path.string()) |
| 355 | .c_str()); |
Brandon Wyman | 59a3579 | 2020-06-04 12:37:40 -0500 | [diff] [blame] | 356 | |
| 357 | using metadata = xyz::openbmc_project::Common::Device::WriteFailure; |
| 358 | |
| 359 | elog<WriteFailure>( |
| 360 | metadata::CALLOUT_ERRNO(rc), |
| 361 | metadata::CALLOUT_DEVICE_PATH(fs::canonical(basePath).c_str())); |
| 362 | } |
| 363 | } |
| 364 | |
Brandon Wyman | ff5f339 | 2017-08-11 17:43:22 -0500 | [diff] [blame] | 365 | void PMBus::findHwmonDir() |
Matt Spinler | 57868bc | 2017-08-03 10:07:41 -0500 | [diff] [blame] | 366 | { |
| 367 | fs::path path{basePath}; |
| 368 | path /= "hwmon"; |
| 369 | |
Brandon Wyman | aad73e9 | 2017-08-16 16:27:54 -0500 | [diff] [blame] | 370 | // Make sure the directory exists, otherwise for things that can be |
| 371 | // dynamically present or not present an exception will be thrown if the |
| 372 | // hwmon directory is not there, resulting in a program termination. |
| 373 | if (fs::is_directory(path)) |
Matt Spinler | 57868bc | 2017-08-03 10:07:41 -0500 | [diff] [blame] | 374 | { |
Matt Spinler | f0f02b9 | 2018-10-25 16:12:43 -0500 | [diff] [blame] | 375 | // look for <basePath>/hwmon/hwmonN/ |
Brandon Wyman | aad73e9 | 2017-08-16 16:27:54 -0500 | [diff] [blame] | 376 | for (auto& f : fs::directory_iterator(path)) |
Matt Spinler | 57868bc | 2017-08-03 10:07:41 -0500 | [diff] [blame] | 377 | { |
Brandon Wyman | aad73e9 | 2017-08-16 16:27:54 -0500 | [diff] [blame] | 378 | if ((f.path().filename().string().find("hwmon") != |
| 379 | std::string::npos) && |
| 380 | (fs::is_directory(f.path()))) |
| 381 | { |
| 382 | hwmonDir = f.path().filename(); |
| 383 | break; |
| 384 | } |
Matt Spinler | 57868bc | 2017-08-03 10:07:41 -0500 | [diff] [blame] | 385 | } |
| 386 | } |
| 387 | |
Matt Spinler | f0f02b9 | 2018-10-25 16:12:43 -0500 | [diff] [blame] | 388 | // Don't really want to crash here, just log it |
| 389 | // and let accesses fail later |
Brandon Wyman | ff5f339 | 2017-08-11 17:43:22 -0500 | [diff] [blame] | 390 | if (hwmonDir.empty()) |
Matt Spinler | 57868bc | 2017-08-03 10:07:41 -0500 | [diff] [blame] | 391 | { |
Jay Meyer | 6a3fd2c | 2020-08-25 16:37:16 -0500 | [diff] [blame] | 392 | log<level::INFO>(std::string("Unable to find hwmon directory " |
| 393 | "in device base path" |
| 394 | " DEVICE_PATH=" + |
| 395 | basePath.string()) |
| 396 | .c_str()); |
Matt Spinler | 57868bc | 2017-08-03 10:07:41 -0500 | [diff] [blame] | 397 | } |
Matt Spinler | 57868bc | 2017-08-03 10:07:41 -0500 | [diff] [blame] | 398 | } |
| 399 | |
Brandon Wyman | 8d19577 | 2020-01-27 15:03:51 -0600 | [diff] [blame] | 400 | std::unique_ptr<PMBusBase> PMBus::createPMBus(std::uint8_t bus, |
| 401 | const std::string& address) |
| 402 | { |
| 403 | const std::string physpath = {"/sys/bus/i2c/devices/" + |
| 404 | std::to_string(bus) + "-" + address}; |
| 405 | auto interface = std::make_unique<PMBus>(physpath); |
| 406 | |
| 407 | return interface; |
| 408 | } |
| 409 | |
| 410 | std::unique_ptr<PMBusBase> createPMBus(std::uint8_t bus, |
| 411 | const std::string& address) |
| 412 | { |
| 413 | return PMBus::createPMBus(bus, address); |
| 414 | } |
| 415 | |
Matt Spinler | f0f02b9 | 2018-10-25 16:12:43 -0500 | [diff] [blame] | 416 | } // namespace pmbus |
Lei YU | ab09332 | 2019-10-09 16:43:22 +0800 | [diff] [blame] | 417 | } // namespace phosphor |