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 | |
Patrick Williams | 2c4fbc4 | 2020-06-26 15:33:11 -0500 | [diff] [blame] | 23 | #include <filesystem> |
| 24 | #include <fstream> |
| 25 | |
Matt Spinler | 015e3ad | 2017-08-01 11:20:47 -0500 | [diff] [blame] | 26 | namespace witherspoon |
| 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 | } |
| 101 | catch (std::exception& e) |
| 102 | { |
| 103 | log<level::ERR>("Unable to read PMBus device name", |
| 104 | entry("PATH=%s", path.c_str())); |
| 105 | } |
| 106 | |
| 107 | return name; |
| 108 | } |
| 109 | |
Matt Spinler | f0f02b9 | 2018-10-25 16:12:43 -0500 | [diff] [blame] | 110 | bool PMBus::readBitInPage(const std::string& name, size_t page, Type type) |
Matt Spinler | 015e3ad | 2017-08-01 11:20:47 -0500 | [diff] [blame] | 111 | { |
| 112 | auto pagedBit = insertPageNum(name, page); |
Matt Spinler | 57868bc | 2017-08-03 10:07:41 -0500 | [diff] [blame] | 113 | return readBit(pagedBit, type); |
Matt Spinler | 015e3ad | 2017-08-01 11:20:47 -0500 | [diff] [blame] | 114 | } |
| 115 | |
Matt Spinler | 57868bc | 2017-08-03 10:07:41 -0500 | [diff] [blame] | 116 | bool PMBus::readBit(const std::string& name, Type type) |
Matt Spinler | 015e3ad | 2017-08-01 11:20:47 -0500 | [diff] [blame] | 117 | { |
| 118 | unsigned long int value = 0; |
| 119 | std::ifstream file; |
Brandon Wyman | ff5f339 | 2017-08-11 17:43:22 -0500 | [diff] [blame] | 120 | fs::path path = getPath(type); |
Matt Spinler | 57868bc | 2017-08-03 10:07:41 -0500 | [diff] [blame] | 121 | |
Matt Spinler | 015e3ad | 2017-08-01 11:20:47 -0500 | [diff] [blame] | 122 | path /= name; |
| 123 | |
Matt Spinler | f0f02b9 | 2018-10-25 16:12:43 -0500 | [diff] [blame] | 124 | file.exceptions(std::ifstream::failbit | std::ifstream::badbit | |
Matt Spinler | 015e3ad | 2017-08-01 11:20:47 -0500 | [diff] [blame] | 125 | std::ifstream::eofbit); |
| 126 | |
| 127 | try |
| 128 | { |
| 129 | char* err = NULL; |
| 130 | std::string val{1, '\0'}; |
| 131 | |
| 132 | file.open(path); |
| 133 | file.read(&val[0], 1); |
| 134 | |
| 135 | value = strtoul(val.c_str(), &err, 10); |
| 136 | |
| 137 | if (*err) |
| 138 | { |
| 139 | log<level::ERR>("Invalid character in sysfs file", |
| 140 | entry("FILE=%s", path.c_str()), |
| 141 | entry("CONTENTS=%s", val.c_str())); |
| 142 | |
Matt Spinler | f0f02b9 | 2018-10-25 16:12:43 -0500 | [diff] [blame] | 143 | // Catch below and handle as a read failure |
Matt Spinler | 015e3ad | 2017-08-01 11:20:47 -0500 | [diff] [blame] | 144 | elog<InternalFailure>(); |
| 145 | } |
| 146 | } |
| 147 | catch (std::exception& e) |
| 148 | { |
| 149 | auto rc = errno; |
| 150 | |
| 151 | log<level::ERR>("Failed to read sysfs file", |
Brandon Wyman | f855e82 | 2017-08-08 18:04:47 -0500 | [diff] [blame] | 152 | entry("FILENAME=%s", path.c_str())); |
Matt Spinler | 015e3ad | 2017-08-01 11:20:47 -0500 | [diff] [blame] | 153 | |
Matt Spinler | ceacf94 | 2017-10-05 13:55:02 -0500 | [diff] [blame] | 154 | using metadata = xyz::openbmc_project::Common::Device::ReadFailure; |
| 155 | |
Matt Spinler | f0f02b9 | 2018-10-25 16:12:43 -0500 | [diff] [blame] | 156 | elog<ReadFailure>( |
| 157 | metadata::CALLOUT_ERRNO(rc), |
| 158 | metadata::CALLOUT_DEVICE_PATH(fs::canonical(basePath).c_str())); |
Matt Spinler | 015e3ad | 2017-08-01 11:20:47 -0500 | [diff] [blame] | 159 | } |
| 160 | |
| 161 | return value != 0; |
| 162 | } |
| 163 | |
Brandon Wyman | 3b7b38b | 2017-09-25 16:43:45 -0500 | [diff] [blame] | 164 | bool PMBus::exists(const std::string& name, Type type) |
| 165 | { |
| 166 | auto path = getPath(type); |
| 167 | path /= name; |
| 168 | return fs::exists(path); |
| 169 | } |
| 170 | |
Brandon Wyman | f855e82 | 2017-08-08 18:04:47 -0500 | [diff] [blame] | 171 | uint64_t PMBus::read(const std::string& name, Type type) |
| 172 | { |
| 173 | uint64_t data = 0; |
| 174 | std::ifstream file; |
| 175 | auto path = getPath(type); |
| 176 | path /= name; |
| 177 | |
Matt Spinler | f0f02b9 | 2018-10-25 16:12:43 -0500 | [diff] [blame] | 178 | file.exceptions(std::ifstream::failbit | std::ifstream::badbit | |
Brandon Wyman | f855e82 | 2017-08-08 18:04:47 -0500 | [diff] [blame] | 179 | std::ifstream::eofbit); |
| 180 | |
| 181 | try |
| 182 | { |
| 183 | file.open(path); |
| 184 | file >> std::hex >> data; |
| 185 | } |
| 186 | catch (std::exception& e) |
| 187 | { |
| 188 | auto rc = errno; |
| 189 | log<level::ERR>("Failed to read sysfs file", |
| 190 | entry("FILENAME=%s", path.c_str())); |
| 191 | |
Matt Spinler | ceacf94 | 2017-10-05 13:55:02 -0500 | [diff] [blame] | 192 | using metadata = xyz::openbmc_project::Common::Device::ReadFailure; |
Brandon Wyman | f855e82 | 2017-08-08 18:04:47 -0500 | [diff] [blame] | 193 | |
Matt Spinler | f0f02b9 | 2018-10-25 16:12:43 -0500 | [diff] [blame] | 194 | elog<ReadFailure>( |
| 195 | metadata::CALLOUT_ERRNO(rc), |
| 196 | metadata::CALLOUT_DEVICE_PATH(fs::canonical(basePath).c_str())); |
Brandon Wyman | f855e82 | 2017-08-08 18:04:47 -0500 | [diff] [blame] | 197 | } |
| 198 | |
| 199 | return data; |
| 200 | } |
| 201 | |
Matt Spinler | fbae7b6 | 2018-01-04 14:33:13 -0600 | [diff] [blame] | 202 | std::string PMBus::readString(const std::string& name, Type type) |
| 203 | { |
| 204 | std::string data; |
| 205 | std::ifstream file; |
| 206 | auto path = getPath(type); |
| 207 | path /= name; |
| 208 | |
Matt Spinler | f0f02b9 | 2018-10-25 16:12:43 -0500 | [diff] [blame] | 209 | file.exceptions(std::ifstream::failbit | std::ifstream::badbit | |
Matt Spinler | fbae7b6 | 2018-01-04 14:33:13 -0600 | [diff] [blame] | 210 | std::ifstream::eofbit); |
| 211 | |
| 212 | try |
| 213 | { |
| 214 | file.open(path); |
| 215 | file >> data; |
| 216 | } |
| 217 | catch (std::exception& e) |
| 218 | { |
| 219 | auto rc = errno; |
| 220 | log<level::ERR>("Failed to read sysfs file", |
| 221 | entry("FILENAME=%s", path.c_str())); |
| 222 | |
| 223 | using metadata = xyz::openbmc_project::Common::Device::ReadFailure; |
| 224 | |
Matt Spinler | f0f02b9 | 2018-10-25 16:12:43 -0500 | [diff] [blame] | 225 | elog<ReadFailure>( |
| 226 | metadata::CALLOUT_ERRNO(rc), |
| 227 | metadata::CALLOUT_DEVICE_PATH(fs::canonical(basePath).c_str())); |
Matt Spinler | fbae7b6 | 2018-01-04 14:33:13 -0600 | [diff] [blame] | 228 | } |
| 229 | |
| 230 | return data; |
| 231 | } |
| 232 | |
Matt Spinler | f0f02b9 | 2018-10-25 16:12:43 -0500 | [diff] [blame] | 233 | std::vector<uint8_t> PMBus::readBinary(const std::string& name, Type type, |
Matt Spinler | fa23e33 | 2018-01-18 11:24:58 -0600 | [diff] [blame] | 234 | size_t length) |
| 235 | { |
| 236 | auto path = getPath(type) / name; |
| 237 | |
Matt Spinler | f0f02b9 | 2018-10-25 16:12:43 -0500 | [diff] [blame] | 238 | // Use C style IO because it's easier to handle telling the difference |
| 239 | // between hitting EOF or getting an actual error. |
Matt Spinler | fa23e33 | 2018-01-18 11:24:58 -0600 | [diff] [blame] | 240 | std::unique_ptr<FILE, FileCloser> file{fopen(path.c_str(), "rb")}; |
| 241 | |
| 242 | if (file) |
| 243 | { |
| 244 | std::vector<uint8_t> data(length, 0); |
| 245 | |
Matt Spinler | f0f02b9 | 2018-10-25 16:12:43 -0500 | [diff] [blame] | 246 | auto bytes = |
| 247 | fread(data.data(), sizeof(decltype(data[0])), length, file.get()); |
Matt Spinler | fa23e33 | 2018-01-18 11:24:58 -0600 | [diff] [blame] | 248 | |
| 249 | if (bytes != length) |
| 250 | { |
Matt Spinler | f0f02b9 | 2018-10-25 16:12:43 -0500 | [diff] [blame] | 251 | // If hit EOF, just return the amount of data that was read. |
Matt Spinler | fa23e33 | 2018-01-18 11:24:58 -0600 | [diff] [blame] | 252 | if (feof(file.get())) |
| 253 | { |
Matt Spinler | f0f02b9 | 2018-10-25 16:12:43 -0500 | [diff] [blame] | 254 | data.erase(data.begin() + bytes, data.end()); |
Matt Spinler | fa23e33 | 2018-01-18 11:24:58 -0600 | [diff] [blame] | 255 | } |
| 256 | else if (ferror(file.get())) |
| 257 | { |
| 258 | auto rc = errno; |
| 259 | log<level::ERR>("Failed to read sysfs file", |
| 260 | entry("FILENAME=%s", path.c_str())); |
| 261 | |
Matt Spinler | f0f02b9 | 2018-10-25 16:12:43 -0500 | [diff] [blame] | 262 | using metadata = |
| 263 | xyz::openbmc_project::Common::Device::ReadFailure; |
Matt Spinler | fa23e33 | 2018-01-18 11:24:58 -0600 | [diff] [blame] | 264 | |
| 265 | elog<ReadFailure>(metadata::CALLOUT_ERRNO(rc), |
| 266 | metadata::CALLOUT_DEVICE_PATH( |
Matt Spinler | f0f02b9 | 2018-10-25 16:12:43 -0500 | [diff] [blame] | 267 | fs::canonical(basePath).c_str())); |
Matt Spinler | fa23e33 | 2018-01-18 11:24:58 -0600 | [diff] [blame] | 268 | } |
| 269 | } |
| 270 | return data; |
| 271 | } |
| 272 | |
| 273 | return std::vector<uint8_t>{}; |
| 274 | } |
| 275 | |
Matt Spinler | 57868bc | 2017-08-03 10:07:41 -0500 | [diff] [blame] | 276 | void PMBus::write(const std::string& name, int value, Type type) |
Matt Spinler | 015e3ad | 2017-08-01 11:20:47 -0500 | [diff] [blame] | 277 | { |
| 278 | std::ofstream file; |
Brandon Wyman | ff5f339 | 2017-08-11 17:43:22 -0500 | [diff] [blame] | 279 | fs::path path = getPath(type); |
Matt Spinler | 57868bc | 2017-08-03 10:07:41 -0500 | [diff] [blame] | 280 | |
Matt Spinler | 015e3ad | 2017-08-01 11:20:47 -0500 | [diff] [blame] | 281 | path /= name; |
| 282 | |
Matt Spinler | f0f02b9 | 2018-10-25 16:12:43 -0500 | [diff] [blame] | 283 | file.exceptions(std::ofstream::failbit | std::ofstream::badbit | |
Matt Spinler | 015e3ad | 2017-08-01 11:20:47 -0500 | [diff] [blame] | 284 | std::ofstream::eofbit); |
| 285 | |
| 286 | try |
| 287 | { |
| 288 | file.open(path); |
| 289 | file << value; |
| 290 | } |
| 291 | catch (const std::exception& e) |
| 292 | { |
| 293 | auto rc = errno; |
| 294 | |
| 295 | log<level::ERR>("Failed to write sysfs file", |
Brandon Wyman | f855e82 | 2017-08-08 18:04:47 -0500 | [diff] [blame] | 296 | entry("FILENAME=%s", path.c_str())); |
Matt Spinler | 015e3ad | 2017-08-01 11:20:47 -0500 | [diff] [blame] | 297 | |
Matt Spinler | ceacf94 | 2017-10-05 13:55:02 -0500 | [diff] [blame] | 298 | using metadata = xyz::openbmc_project::Common::Device::WriteFailure; |
| 299 | |
Matt Spinler | f0f02b9 | 2018-10-25 16:12:43 -0500 | [diff] [blame] | 300 | elog<WriteFailure>( |
| 301 | metadata::CALLOUT_ERRNO(rc), |
| 302 | metadata::CALLOUT_DEVICE_PATH(fs::canonical(basePath).c_str())); |
Matt Spinler | 015e3ad | 2017-08-01 11:20:47 -0500 | [diff] [blame] | 303 | } |
| 304 | } |
| 305 | |
Brandon Wyman | ff5f339 | 2017-08-11 17:43:22 -0500 | [diff] [blame] | 306 | void PMBus::findHwmonDir() |
Matt Spinler | 57868bc | 2017-08-03 10:07:41 -0500 | [diff] [blame] | 307 | { |
| 308 | fs::path path{basePath}; |
| 309 | path /= "hwmon"; |
| 310 | |
Brandon Wyman | aad73e9 | 2017-08-16 16:27:54 -0500 | [diff] [blame] | 311 | // Make sure the directory exists, otherwise for things that can be |
| 312 | // dynamically present or not present an exception will be thrown if the |
| 313 | // hwmon directory is not there, resulting in a program termination. |
| 314 | if (fs::is_directory(path)) |
Matt Spinler | 57868bc | 2017-08-03 10:07:41 -0500 | [diff] [blame] | 315 | { |
Matt Spinler | f0f02b9 | 2018-10-25 16:12:43 -0500 | [diff] [blame] | 316 | // look for <basePath>/hwmon/hwmonN/ |
Brandon Wyman | aad73e9 | 2017-08-16 16:27:54 -0500 | [diff] [blame] | 317 | for (auto& f : fs::directory_iterator(path)) |
Matt Spinler | 57868bc | 2017-08-03 10:07:41 -0500 | [diff] [blame] | 318 | { |
Brandon Wyman | aad73e9 | 2017-08-16 16:27:54 -0500 | [diff] [blame] | 319 | if ((f.path().filename().string().find("hwmon") != |
| 320 | std::string::npos) && |
| 321 | (fs::is_directory(f.path()))) |
| 322 | { |
| 323 | hwmonDir = f.path().filename(); |
| 324 | break; |
| 325 | } |
Matt Spinler | 57868bc | 2017-08-03 10:07:41 -0500 | [diff] [blame] | 326 | } |
| 327 | } |
| 328 | |
Matt Spinler | f0f02b9 | 2018-10-25 16:12:43 -0500 | [diff] [blame] | 329 | // Don't really want to crash here, just log it |
| 330 | // and let accesses fail later |
Brandon Wyman | ff5f339 | 2017-08-11 17:43:22 -0500 | [diff] [blame] | 331 | if (hwmonDir.empty()) |
Matt Spinler | 57868bc | 2017-08-03 10:07:41 -0500 | [diff] [blame] | 332 | { |
Brandon Wyman | aad73e9 | 2017-08-16 16:27:54 -0500 | [diff] [blame] | 333 | log<level::INFO>("Unable to find hwmon directory " |
| 334 | "in device base path", |
| 335 | entry("DEVICE_PATH=%s", basePath.c_str())); |
Matt Spinler | 57868bc | 2017-08-03 10:07:41 -0500 | [diff] [blame] | 336 | } |
Matt Spinler | 57868bc | 2017-08-03 10:07:41 -0500 | [diff] [blame] | 337 | } |
| 338 | |
Matt Spinler | f0f02b9 | 2018-10-25 16:12:43 -0500 | [diff] [blame] | 339 | } // namespace pmbus |
| 340 | } // namespace witherspoon |