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