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