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: |
| 65 | auto dir = driverName + "." + std::to_string(instance); |
| 66 | return debugPath / dir; |
Brandon Wyman | ff5f339 | 2017-08-11 17:43:22 -0500 | [diff] [blame] | 67 | break; |
| 68 | } |
| 69 | } |
| 70 | |
Matt Spinler | 57868bc | 2017-08-03 10:07:41 -0500 | [diff] [blame] | 71 | bool PMBus::readBitInPage(const std::string& name, |
| 72 | size_t page, |
| 73 | Type type) |
Matt Spinler | 015e3ad | 2017-08-01 11:20:47 -0500 | [diff] [blame] | 74 | { |
| 75 | auto pagedBit = insertPageNum(name, page); |
Matt Spinler | 57868bc | 2017-08-03 10:07:41 -0500 | [diff] [blame] | 76 | return readBit(pagedBit, type); |
Matt Spinler | 015e3ad | 2017-08-01 11:20:47 -0500 | [diff] [blame] | 77 | } |
| 78 | |
Matt Spinler | 57868bc | 2017-08-03 10:07:41 -0500 | [diff] [blame] | 79 | bool PMBus::readBit(const std::string& name, Type type) |
Matt Spinler | 015e3ad | 2017-08-01 11:20:47 -0500 | [diff] [blame] | 80 | { |
| 81 | unsigned long int value = 0; |
| 82 | std::ifstream file; |
Brandon Wyman | ff5f339 | 2017-08-11 17:43:22 -0500 | [diff] [blame] | 83 | fs::path path = getPath(type); |
Matt Spinler | 57868bc | 2017-08-03 10:07:41 -0500 | [diff] [blame] | 84 | |
Matt Spinler | 015e3ad | 2017-08-01 11:20:47 -0500 | [diff] [blame] | 85 | path /= name; |
| 86 | |
| 87 | file.exceptions(std::ifstream::failbit | |
| 88 | std::ifstream::badbit | |
| 89 | std::ifstream::eofbit); |
| 90 | |
| 91 | try |
| 92 | { |
| 93 | char* err = NULL; |
| 94 | std::string val{1, '\0'}; |
| 95 | |
| 96 | file.open(path); |
| 97 | file.read(&val[0], 1); |
| 98 | |
| 99 | value = strtoul(val.c_str(), &err, 10); |
| 100 | |
| 101 | if (*err) |
| 102 | { |
| 103 | log<level::ERR>("Invalid character in sysfs file", |
| 104 | entry("FILE=%s", path.c_str()), |
| 105 | entry("CONTENTS=%s", val.c_str())); |
| 106 | |
| 107 | //Catch below and handle as a read failure |
| 108 | elog<InternalFailure>(); |
| 109 | } |
| 110 | } |
| 111 | catch (std::exception& e) |
| 112 | { |
| 113 | auto rc = errno; |
| 114 | |
| 115 | log<level::ERR>("Failed to read sysfs file", |
Brandon Wyman | f855e82 | 2017-08-08 18:04:47 -0500 | [diff] [blame] | 116 | entry("FILENAME=%s", path.c_str())); |
Matt Spinler | 015e3ad | 2017-08-01 11:20:47 -0500 | [diff] [blame] | 117 | |
Matt Spinler | ceacf94 | 2017-10-05 13:55:02 -0500 | [diff] [blame] | 118 | using metadata = xyz::openbmc_project::Common::Device::ReadFailure; |
| 119 | |
| 120 | elog<ReadFailure>(metadata::CALLOUT_ERRNO(rc), |
| 121 | metadata::CALLOUT_DEVICE_PATH( |
| 122 | fs::canonical(basePath).c_str())); |
Matt Spinler | 015e3ad | 2017-08-01 11:20:47 -0500 | [diff] [blame] | 123 | } |
| 124 | |
| 125 | return value != 0; |
| 126 | } |
| 127 | |
Brandon Wyman | 3b7b38b | 2017-09-25 16:43:45 -0500 | [diff] [blame] | 128 | bool PMBus::exists(const std::string& name, Type type) |
| 129 | { |
| 130 | auto path = getPath(type); |
| 131 | path /= name; |
| 132 | return fs::exists(path); |
| 133 | } |
| 134 | |
Brandon Wyman | f855e82 | 2017-08-08 18:04:47 -0500 | [diff] [blame] | 135 | uint64_t PMBus::read(const std::string& name, Type type) |
| 136 | { |
| 137 | uint64_t data = 0; |
| 138 | std::ifstream file; |
| 139 | auto path = getPath(type); |
| 140 | path /= name; |
| 141 | |
| 142 | file.exceptions(std::ifstream::failbit | |
| 143 | std::ifstream::badbit | |
| 144 | std::ifstream::eofbit); |
| 145 | |
| 146 | try |
| 147 | { |
| 148 | file.open(path); |
| 149 | file >> std::hex >> data; |
| 150 | } |
| 151 | catch (std::exception& e) |
| 152 | { |
| 153 | auto rc = errno; |
| 154 | log<level::ERR>("Failed to read sysfs file", |
| 155 | entry("FILENAME=%s", path.c_str())); |
| 156 | |
Matt Spinler | ceacf94 | 2017-10-05 13:55:02 -0500 | [diff] [blame] | 157 | using metadata = xyz::openbmc_project::Common::Device::ReadFailure; |
Brandon Wyman | f855e82 | 2017-08-08 18:04:47 -0500 | [diff] [blame] | 158 | |
| 159 | elog<ReadFailure>(metadata::CALLOUT_ERRNO(rc), |
| 160 | metadata::CALLOUT_DEVICE_PATH( |
| 161 | fs::canonical(basePath).c_str())); |
| 162 | } |
| 163 | |
| 164 | return data; |
| 165 | } |
| 166 | |
Matt Spinler | 57868bc | 2017-08-03 10:07:41 -0500 | [diff] [blame] | 167 | void PMBus::write(const std::string& name, int value, Type type) |
Matt Spinler | 015e3ad | 2017-08-01 11:20:47 -0500 | [diff] [blame] | 168 | { |
| 169 | std::ofstream file; |
Brandon Wyman | ff5f339 | 2017-08-11 17:43:22 -0500 | [diff] [blame] | 170 | fs::path path = getPath(type); |
Matt Spinler | 57868bc | 2017-08-03 10:07:41 -0500 | [diff] [blame] | 171 | |
Matt Spinler | 015e3ad | 2017-08-01 11:20:47 -0500 | [diff] [blame] | 172 | path /= name; |
| 173 | |
| 174 | file.exceptions(std::ofstream::failbit | |
| 175 | std::ofstream::badbit | |
| 176 | std::ofstream::eofbit); |
| 177 | |
| 178 | try |
| 179 | { |
| 180 | file.open(path); |
| 181 | file << value; |
| 182 | } |
| 183 | catch (const std::exception& e) |
| 184 | { |
| 185 | auto rc = errno; |
| 186 | |
| 187 | log<level::ERR>("Failed to write sysfs file", |
Brandon Wyman | f855e82 | 2017-08-08 18:04:47 -0500 | [diff] [blame] | 188 | entry("FILENAME=%s", path.c_str())); |
Matt Spinler | 015e3ad | 2017-08-01 11:20:47 -0500 | [diff] [blame] | 189 | |
Matt Spinler | ceacf94 | 2017-10-05 13:55:02 -0500 | [diff] [blame] | 190 | using metadata = xyz::openbmc_project::Common::Device::WriteFailure; |
| 191 | |
| 192 | elog<WriteFailure>(metadata::CALLOUT_ERRNO(rc), |
| 193 | metadata::CALLOUT_DEVICE_PATH( |
| 194 | fs::canonical(basePath).c_str())); |
Matt Spinler | 015e3ad | 2017-08-01 11:20:47 -0500 | [diff] [blame] | 195 | } |
| 196 | } |
| 197 | |
Brandon Wyman | ff5f339 | 2017-08-11 17:43:22 -0500 | [diff] [blame] | 198 | void PMBus::findHwmonDir() |
Matt Spinler | 57868bc | 2017-08-03 10:07:41 -0500 | [diff] [blame] | 199 | { |
| 200 | fs::path path{basePath}; |
| 201 | path /= "hwmon"; |
| 202 | |
Brandon Wyman | aad73e9 | 2017-08-16 16:27:54 -0500 | [diff] [blame] | 203 | // Make sure the directory exists, otherwise for things that can be |
| 204 | // dynamically present or not present an exception will be thrown if the |
| 205 | // hwmon directory is not there, resulting in a program termination. |
| 206 | if (fs::is_directory(path)) |
Matt Spinler | 57868bc | 2017-08-03 10:07:41 -0500 | [diff] [blame] | 207 | { |
Brandon Wyman | aad73e9 | 2017-08-16 16:27:54 -0500 | [diff] [blame] | 208 | //look for <basePath>/hwmon/hwmonN/ |
| 209 | for (auto& f : fs::directory_iterator(path)) |
Matt Spinler | 57868bc | 2017-08-03 10:07:41 -0500 | [diff] [blame] | 210 | { |
Brandon Wyman | aad73e9 | 2017-08-16 16:27:54 -0500 | [diff] [blame] | 211 | if ((f.path().filename().string().find("hwmon") != |
| 212 | std::string::npos) && |
| 213 | (fs::is_directory(f.path()))) |
| 214 | { |
| 215 | hwmonDir = f.path().filename(); |
| 216 | break; |
| 217 | } |
Matt Spinler | 57868bc | 2017-08-03 10:07:41 -0500 | [diff] [blame] | 218 | } |
| 219 | } |
| 220 | |
| 221 | //Don't really want to crash here, just log it |
| 222 | //and let accesses fail later |
Brandon Wyman | ff5f339 | 2017-08-11 17:43:22 -0500 | [diff] [blame] | 223 | if (hwmonDir.empty()) |
Matt Spinler | 57868bc | 2017-08-03 10:07:41 -0500 | [diff] [blame] | 224 | { |
Brandon Wyman | aad73e9 | 2017-08-16 16:27:54 -0500 | [diff] [blame] | 225 | log<level::INFO>("Unable to find hwmon directory " |
| 226 | "in device base path", |
| 227 | entry("DEVICE_PATH=%s", basePath.c_str())); |
Matt Spinler | 57868bc | 2017-08-03 10:07:41 -0500 | [diff] [blame] | 228 | } |
| 229 | |
| 230 | } |
| 231 | |
Matt Spinler | 015e3ad | 2017-08-01 11:20:47 -0500 | [diff] [blame] | 232 | } |
| 233 | } |