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