Patrick Venture | 043d323 | 2018-08-31 10:10:53 -0700 | [diff] [blame] | 1 | #include "config.h" |
| 2 | |
| 3 | #include "sensor.hpp" |
| 4 | |
| 5 | #include "env.hpp" |
Patrick Venture | b28f432 | 2018-09-14 10:19:14 -0700 | [diff] [blame] | 6 | #include "gpio_handle.hpp" |
Patrick Venture | 043d323 | 2018-08-31 10:10:53 -0700 | [diff] [blame] | 7 | #include "hwmon.hpp" |
| 8 | #include "sensorset.hpp" |
| 9 | #include "sysfs.hpp" |
| 10 | |
Matt Spinler | 5e034af | 2020-06-24 15:21:53 -0500 | [diff] [blame^] | 11 | #include <fmt/format.h> |
| 12 | |
Brandon Kim | 86dcac8 | 2019-06-18 17:48:51 -0700 | [diff] [blame] | 13 | #include <cassert> |
William A. Kennington III | 2227bd5 | 2019-06-19 11:32:22 -0700 | [diff] [blame] | 14 | #include <chrono> |
James Feist | ee73f5b | 2018-08-01 16:31:42 -0700 | [diff] [blame] | 15 | #include <cmath> |
Matthew Barth | cb3daaf | 2018-05-07 15:03:16 -0500 | [diff] [blame] | 16 | #include <cstring> |
Patrick Venture | 9e997b4 | 2019-03-08 13:42:10 -0800 | [diff] [blame] | 17 | #include <filesystem> |
Matthew Barth | 3581938 | 2018-04-18 14:53:01 -0500 | [diff] [blame] | 18 | #include <phosphor-logging/elog-errors.hpp> |
Patrick Venture | b28f432 | 2018-09-14 10:19:14 -0700 | [diff] [blame] | 19 | #include <thread> |
| 20 | #include <xyz/openbmc_project/Common/error.hpp> |
Matthew Barth | 3581938 | 2018-04-18 14:53:01 -0500 | [diff] [blame] | 21 | #include <xyz/openbmc_project/Sensor/Device/error.hpp> |
| 22 | |
Matthew Barth | 3581938 | 2018-04-18 14:53:01 -0500 | [diff] [blame] | 23 | namespace sensor |
| 24 | { |
| 25 | |
Matthew Barth | cb3daaf | 2018-05-07 15:03:16 -0500 | [diff] [blame] | 26 | using namespace phosphor::logging; |
Patrick Venture | b28f432 | 2018-09-14 10:19:14 -0700 | [diff] [blame] | 27 | using namespace sdbusplus::xyz::openbmc_project::Common::Error; |
Matthew Barth | cb3daaf | 2018-05-07 15:03:16 -0500 | [diff] [blame] | 28 | |
James Feist | ee73f5b | 2018-08-01 16:31:42 -0700 | [diff] [blame] | 29 | // todo: this can be deleted once we move to double |
| 30 | // helper class to set the scale on the value iface only when it's available |
| 31 | template <typename T> |
| 32 | void setScale(T& iface, int64_t value, double) |
| 33 | { |
| 34 | } |
| 35 | template <typename T> |
| 36 | void setScale(T& iface, int64_t value, int64_t) |
| 37 | { |
| 38 | iface->scale(value); |
| 39 | } |
| 40 | |
| 41 | // todo: this can be simplified once we move to the double interface |
Matthew Barth | 2e41b13 | 2018-05-07 14:15:45 -0500 | [diff] [blame] | 42 | Sensor::Sensor(const SensorSet::key_type& sensor, |
Patrick Venture | 2864b06 | 2018-12-19 08:13:41 -0800 | [diff] [blame] | 43 | const hwmonio::HwmonIOInterface* ioAccess, |
| 44 | const std::string& devPath) : |
Patrick Venture | 12659aa | 2018-12-19 13:58:43 -0800 | [diff] [blame] | 45 | _sensor(sensor), |
Brandon Kim | 86dcac8 | 2019-06-18 17:48:51 -0700 | [diff] [blame] | 46 | _ioAccess(ioAccess), _devPath(devPath), _scale(0), _hasFaultFile(false) |
Matthew Barth | 9c43106 | 2018-05-07 13:55:29 -0500 | [diff] [blame] | 47 | { |
Patrick Venture | b28f432 | 2018-09-14 10:19:14 -0700 | [diff] [blame] | 48 | auto chip = env::getEnv("GPIOCHIP", sensor); |
| 49 | auto access = env::getEnv("GPIO", sensor); |
| 50 | if (!access.empty() && !chip.empty()) |
| 51 | { |
Patrick Venture | 12659aa | 2018-12-19 13:58:43 -0800 | [diff] [blame] | 52 | _handle = gpio::BuildGpioHandle(chip, access); |
Patrick Venture | b28f432 | 2018-09-14 10:19:14 -0700 | [diff] [blame] | 53 | |
Patrick Venture | 12659aa | 2018-12-19 13:58:43 -0800 | [diff] [blame] | 54 | if (!_handle) |
Patrick Venture | b28f432 | 2018-09-14 10:19:14 -0700 | [diff] [blame] | 55 | { |
| 56 | log<level::ERR>("Unable to set up gpio locking"); |
| 57 | elog<InternalFailure>(); |
| 58 | } |
| 59 | } |
| 60 | |
Matthew Barth | ac47309 | 2018-05-07 14:41:46 -0500 | [diff] [blame] | 61 | auto gain = env::getEnv("GAIN", sensor); |
| 62 | if (!gain.empty()) |
| 63 | { |
Patrick Venture | 12659aa | 2018-12-19 13:58:43 -0800 | [diff] [blame] | 64 | _sensorAdjusts.gain = std::stod(gain); |
Matthew Barth | ac47309 | 2018-05-07 14:41:46 -0500 | [diff] [blame] | 65 | } |
| 66 | |
| 67 | auto offset = env::getEnv("OFFSET", sensor); |
| 68 | if (!offset.empty()) |
| 69 | { |
Patrick Venture | 12659aa | 2018-12-19 13:58:43 -0800 | [diff] [blame] | 70 | _sensorAdjusts.offset = std::stoi(offset); |
Matthew Barth | ac47309 | 2018-05-07 14:41:46 -0500 | [diff] [blame] | 71 | } |
| 72 | auto senRmRCs = env::getEnv("REMOVERCS", sensor); |
| 73 | // Add sensor removal return codes defined per sensor |
| 74 | addRemoveRCs(senRmRCs); |
Matthew Barth | 9c43106 | 2018-05-07 13:55:29 -0500 | [diff] [blame] | 75 | } |
| 76 | |
Matthew Barth | cb3daaf | 2018-05-07 15:03:16 -0500 | [diff] [blame] | 77 | void Sensor::addRemoveRCs(const std::string& rcList) |
| 78 | { |
| 79 | if (rcList.empty()) |
| 80 | { |
| 81 | return; |
| 82 | } |
| 83 | |
| 84 | // Convert to a char* for strtok |
Patrick Venture | 043d323 | 2018-08-31 10:10:53 -0700 | [diff] [blame] | 85 | std::vector<char> rmRCs(rcList.c_str(), rcList.c_str() + rcList.size() + 1); |
Matthew Barth | cb3daaf | 2018-05-07 15:03:16 -0500 | [diff] [blame] | 86 | auto rmRC = std::strtok(&rmRCs[0], ", "); |
| 87 | while (rmRC != nullptr) |
| 88 | { |
| 89 | try |
| 90 | { |
Patrick Venture | 12659aa | 2018-12-19 13:58:43 -0800 | [diff] [blame] | 91 | _sensorAdjusts.rmRCs.insert(std::stoi(rmRC)); |
Matthew Barth | cb3daaf | 2018-05-07 15:03:16 -0500 | [diff] [blame] | 92 | } |
| 93 | catch (const std::logic_error& le) |
| 94 | { |
| 95 | // Unable to convert to int, continue to next token |
Patrick Venture | 12659aa | 2018-12-19 13:58:43 -0800 | [diff] [blame] | 96 | std::string name = _sensor.first + "_" + _sensor.second; |
Matthew Barth | cb3daaf | 2018-05-07 15:03:16 -0500 | [diff] [blame] | 97 | log<level::INFO>("Unable to convert sensor removal return code", |
| 98 | entry("SENSOR=%s", name.c_str()), |
| 99 | entry("RC=%s", rmRC), |
| 100 | entry("EXCEPTION=%s", le.what())); |
| 101 | } |
| 102 | rmRC = std::strtok(nullptr, ", "); |
| 103 | } |
| 104 | } |
| 105 | |
James Feist | ee73f5b | 2018-08-01 16:31:42 -0700 | [diff] [blame] | 106 | SensorValueType Sensor::adjustValue(SensorValueType value) |
Matthew Barth | cb3daaf | 2018-05-07 15:03:16 -0500 | [diff] [blame] | 107 | { |
| 108 | // Because read doesn't have an out pointer to store errors. |
| 109 | // let's assume negative values are errors if they have this |
| 110 | // set. |
| 111 | #ifdef NEGATIVE_ERRNO_ON_FAIL |
| 112 | if (value < 0) |
| 113 | { |
| 114 | return value; |
| 115 | } |
| 116 | #endif |
| 117 | |
| 118 | // Adjust based on gain and offset |
Patrick Venture | 12659aa | 2018-12-19 13:58:43 -0800 | [diff] [blame] | 119 | value = static_cast<decltype(value)>(static_cast<double>(value) * |
| 120 | _sensorAdjusts.gain + |
| 121 | _sensorAdjusts.offset); |
Matthew Barth | cb3daaf | 2018-05-07 15:03:16 -0500 | [diff] [blame] | 122 | |
James Feist | ee73f5b | 2018-08-01 16:31:42 -0700 | [diff] [blame] | 123 | if constexpr (std::is_same<SensorValueType, double>::value) |
| 124 | { |
Patrick Venture | 12659aa | 2018-12-19 13:58:43 -0800 | [diff] [blame] | 125 | value *= std::pow(10, _scale); |
James Feist | ee73f5b | 2018-08-01 16:31:42 -0700 | [diff] [blame] | 126 | } |
| 127 | |
Matthew Barth | cb3daaf | 2018-05-07 15:03:16 -0500 | [diff] [blame] | 128 | return value; |
| 129 | } |
| 130 | |
Patrick Venture | 043d323 | 2018-08-31 10:10:53 -0700 | [diff] [blame] | 131 | std::shared_ptr<ValueObject> Sensor::addValue(const RetryIO& retryIO, |
| 132 | ObjectInfo& info) |
Matthew Barth | cb3daaf | 2018-05-07 15:03:16 -0500 | [diff] [blame] | 133 | { |
| 134 | static constexpr bool deferSignals = true; |
| 135 | |
| 136 | // Get the initial value for the value interface. |
| 137 | auto& bus = *std::get<sdbusplus::bus::bus*>(info); |
Patrick Venture | 6206723 | 2019-06-19 17:39:33 -0700 | [diff] [blame] | 138 | auto& obj = std::get<InterfaceMap>(info); |
Matthew Barth | cb3daaf | 2018-05-07 15:03:16 -0500 | [diff] [blame] | 139 | auto& objPath = std::get<std::string>(info); |
| 140 | |
James Feist | ee73f5b | 2018-08-01 16:31:42 -0700 | [diff] [blame] | 141 | SensorValueType val = 0; |
Matthew Barth | cb3daaf | 2018-05-07 15:03:16 -0500 | [diff] [blame] | 142 | |
Brandon Kim | 86dcac8 | 2019-06-18 17:48:51 -0700 | [diff] [blame] | 143 | auto& statusIface = std::any_cast<std::shared_ptr<StatusObject>&>( |
| 144 | obj[InterfaceType::STATUS]); |
| 145 | // As long as addStatus is called before addValue, statusIface |
| 146 | // should never be nullptr |
| 147 | assert(statusIface); |
| 148 | |
| 149 | // Only read the input value if the status is functional |
| 150 | if (statusIface->functional()) |
Matthew Barth | cb3daaf | 2018-05-07 15:03:16 -0500 | [diff] [blame] | 151 | { |
Brandon Kim | 79205b2 | 2019-06-20 12:18:24 -0700 | [diff] [blame] | 152 | #ifdef UPDATE_FUNCTIONAL_ON_FAIL |
| 153 | try |
| 154 | #endif |
| 155 | { |
| 156 | // RAII object for GPIO unlock / lock |
William A. Kennington III | 2227bd5 | 2019-06-19 11:32:22 -0700 | [diff] [blame] | 157 | auto locker = gpioUnlock(getGpio()); |
Patrick Venture | b28f432 | 2018-09-14 10:19:14 -0700 | [diff] [blame] | 158 | |
Brandon Kim | 79205b2 | 2019-06-20 12:18:24 -0700 | [diff] [blame] | 159 | // Retry for up to a second if device is busy |
| 160 | // or has a transient error. |
| 161 | val = |
| 162 | _ioAccess->read(_sensor.first, _sensor.second, |
| 163 | hwmon::entry::cinput, std::get<size_t>(retryIO), |
| 164 | std::get<std::chrono::milliseconds>(retryIO)); |
Patrick Venture | b28f432 | 2018-09-14 10:19:14 -0700 | [diff] [blame] | 165 | |
Brandon Kim | 79205b2 | 2019-06-20 12:18:24 -0700 | [diff] [blame] | 166 | val = adjustValue(val); |
| 167 | } |
| 168 | #ifdef UPDATE_FUNCTIONAL_ON_FAIL |
| 169 | catch (const std::system_error& e) |
| 170 | { |
| 171 | // Catch the exception here and update the functional property. |
| 172 | // By catching the exception, it will not propagate it up the stack |
| 173 | // and thus the code will skip the "Remove RCs" check in |
| 174 | // MainLoop::getObject and will not exit on failure. |
| 175 | statusIface->functional(false); |
| 176 | } |
| 177 | #endif |
Matthew Barth | cb3daaf | 2018-05-07 15:03:16 -0500 | [diff] [blame] | 178 | } |
| 179 | |
Patrick Venture | 043d323 | 2018-08-31 10:10:53 -0700 | [diff] [blame] | 180 | auto iface = |
| 181 | std::make_shared<ValueObject>(bus, objPath.c_str(), deferSignals); |
Matthew Barth | cb3daaf | 2018-05-07 15:03:16 -0500 | [diff] [blame] | 182 | iface->value(val); |
| 183 | |
| 184 | hwmon::Attributes attrs; |
Patrick Venture | 12659aa | 2018-12-19 13:58:43 -0800 | [diff] [blame] | 185 | if (hwmon::getAttributes(_sensor.first, attrs)) |
Matthew Barth | cb3daaf | 2018-05-07 15:03:16 -0500 | [diff] [blame] | 186 | { |
| 187 | iface->unit(hwmon::getUnit(attrs)); |
James Feist | ee73f5b | 2018-08-01 16:31:42 -0700 | [diff] [blame] | 188 | |
| 189 | setScale(iface, hwmon::getScale(attrs), val); |
| 190 | |
Patrick Venture | 12659aa | 2018-12-19 13:58:43 -0800 | [diff] [blame] | 191 | _scale = hwmon::getScale(attrs); |
Matthew Barth | cb3daaf | 2018-05-07 15:03:16 -0500 | [diff] [blame] | 192 | } |
| 193 | |
Patrick Venture | 12659aa | 2018-12-19 13:58:43 -0800 | [diff] [blame] | 194 | auto maxValue = env::getEnv("MAXVALUE", _sensor); |
Patrick Venture | 043d323 | 2018-08-31 10:10:53 -0700 | [diff] [blame] | 195 | if (!maxValue.empty()) |
Matthew Barth | cb3daaf | 2018-05-07 15:03:16 -0500 | [diff] [blame] | 196 | { |
| 197 | iface->maxValue(std::stoll(maxValue)); |
| 198 | } |
Patrick Venture | 12659aa | 2018-12-19 13:58:43 -0800 | [diff] [blame] | 199 | auto minValue = env::getEnv("MINVALUE", _sensor); |
Patrick Venture | 043d323 | 2018-08-31 10:10:53 -0700 | [diff] [blame] | 200 | if (!minValue.empty()) |
Matthew Barth | cb3daaf | 2018-05-07 15:03:16 -0500 | [diff] [blame] | 201 | { |
| 202 | iface->minValue(std::stoll(minValue)); |
| 203 | } |
| 204 | |
| 205 | obj[InterfaceType::VALUE] = iface; |
| 206 | return iface; |
| 207 | } |
| 208 | |
Matthew Barth | 2e41b13 | 2018-05-07 14:15:45 -0500 | [diff] [blame] | 209 | std::shared_ptr<StatusObject> Sensor::addStatus(ObjectInfo& info) |
Matthew Barth | 3581938 | 2018-04-18 14:53:01 -0500 | [diff] [blame] | 210 | { |
Patrick Venture | 9e997b4 | 2019-03-08 13:42:10 -0800 | [diff] [blame] | 211 | namespace fs = std::filesystem; |
Matthew Barth | 3581938 | 2018-04-18 14:53:01 -0500 | [diff] [blame] | 212 | |
| 213 | std::shared_ptr<StatusObject> iface = nullptr; |
Matthew Barth | 3581938 | 2018-04-18 14:53:01 -0500 | [diff] [blame] | 214 | auto& objPath = std::get<std::string>(info); |
Patrick Venture | 6206723 | 2019-06-19 17:39:33 -0700 | [diff] [blame] | 215 | auto& obj = std::get<InterfaceMap>(info); |
Matthew Barth | 3581938 | 2018-04-18 14:53:01 -0500 | [diff] [blame] | 216 | |
| 217 | // Check if fault sysfs file exists |
Patrick Venture | 12659aa | 2018-12-19 13:58:43 -0800 | [diff] [blame] | 218 | std::string faultName = _sensor.first; |
| 219 | std::string faultID = _sensor.second; |
Matthew Barth | 3581938 | 2018-04-18 14:53:01 -0500 | [diff] [blame] | 220 | std::string entry = hwmon::entry::fault; |
| 221 | |
Brandon Kim | 86dcac8 | 2019-06-18 17:48:51 -0700 | [diff] [blame] | 222 | bool functional = true; |
Patrick Venture | 043d323 | 2018-08-31 10:10:53 -0700 | [diff] [blame] | 223 | auto sysfsFullPath = |
Patrick Venture | 12659aa | 2018-12-19 13:58:43 -0800 | [diff] [blame] | 224 | sysfs::make_sysfs_path(_ioAccess->path(), faultName, faultID, entry); |
Matthew Barth | 3581938 | 2018-04-18 14:53:01 -0500 | [diff] [blame] | 225 | if (fs::exists(sysfsFullPath)) |
| 226 | { |
Brandon Kim | 86dcac8 | 2019-06-18 17:48:51 -0700 | [diff] [blame] | 227 | _hasFaultFile = true; |
Matthew Barth | 3581938 | 2018-04-18 14:53:01 -0500 | [diff] [blame] | 228 | try |
| 229 | { |
Patrick Venture | 12659aa | 2018-12-19 13:58:43 -0800 | [diff] [blame] | 230 | uint32_t fault = _ioAccess->read(faultName, faultID, entry, |
| 231 | hwmonio::retries, hwmonio::delay); |
Matthew Barth | 3581938 | 2018-04-18 14:53:01 -0500 | [diff] [blame] | 232 | if (fault != 0) |
| 233 | { |
| 234 | functional = false; |
| 235 | } |
| 236 | } |
| 237 | catch (const std::system_error& e) |
| 238 | { |
Patrick Venture | 043d323 | 2018-08-31 10:10:53 -0700 | [diff] [blame] | 239 | using namespace sdbusplus::xyz::openbmc_project::Sensor::Device:: |
| 240 | Error; |
| 241 | using metadata = xyz::openbmc_project::Sensor::Device::ReadFailure; |
Matthew Barth | 3581938 | 2018-04-18 14:53:01 -0500 | [diff] [blame] | 242 | |
Patrick Venture | 12659aa | 2018-12-19 13:58:43 -0800 | [diff] [blame] | 243 | report<ReadFailure>( |
| 244 | metadata::CALLOUT_ERRNO(e.code().value()), |
| 245 | metadata::CALLOUT_DEVICE_PATH(_devPath.c_str())); |
Matthew Barth | 3581938 | 2018-04-18 14:53:01 -0500 | [diff] [blame] | 246 | |
Patrick Venture | 043d323 | 2018-08-31 10:10:53 -0700 | [diff] [blame] | 247 | log<level::INFO>( |
Matt Spinler | 5e034af | 2020-06-24 15:21:53 -0500 | [diff] [blame^] | 248 | fmt::format("Failing sysfs file: {}", sysfsFullPath).c_str()); |
Matthew Barth | 3581938 | 2018-04-18 14:53:01 -0500 | [diff] [blame] | 249 | } |
Matthew Barth | 3581938 | 2018-04-18 14:53:01 -0500 | [diff] [blame] | 250 | } |
| 251 | |
Brandon Kim | 86dcac8 | 2019-06-18 17:48:51 -0700 | [diff] [blame] | 252 | static constexpr bool deferSignals = true; |
| 253 | auto& bus = *std::get<sdbusplus::bus::bus*>(info); |
| 254 | |
| 255 | iface = std::make_shared<StatusObject>(bus, objPath.c_str(), deferSignals); |
| 256 | // Set functional property |
| 257 | iface->functional(functional); |
| 258 | |
| 259 | obj[InterfaceType::STATUS] = iface; |
| 260 | |
Matthew Barth | 3581938 | 2018-04-18 14:53:01 -0500 | [diff] [blame] | 261 | return iface; |
| 262 | } |
| 263 | |
William A. Kennington III | 2227bd5 | 2019-06-19 11:32:22 -0700 | [diff] [blame] | 264 | void gpioLock(const gpioplus::HandleInterface*&& handle) |
Brandon Kim | db76d49 | 2019-06-17 11:53:04 -0700 | [diff] [blame] | 265 | { |
William A. Kennington III | 2227bd5 | 2019-06-19 11:32:22 -0700 | [diff] [blame] | 266 | handle->setValues({0}); |
Brandon Kim | db76d49 | 2019-06-17 11:53:04 -0700 | [diff] [blame] | 267 | } |
| 268 | |
William A. Kennington III | 2227bd5 | 2019-06-19 11:32:22 -0700 | [diff] [blame] | 269 | std::optional<GpioLocker> gpioUnlock(const gpioplus::HandleInterface* handle) |
Brandon Kim | db76d49 | 2019-06-17 11:53:04 -0700 | [diff] [blame] | 270 | { |
William A. Kennington III | 2227bd5 | 2019-06-19 11:32:22 -0700 | [diff] [blame] | 271 | if (handle == nullptr) |
Patrick Venture | b28f432 | 2018-09-14 10:19:14 -0700 | [diff] [blame] | 272 | { |
William A. Kennington III | 2227bd5 | 2019-06-19 11:32:22 -0700 | [diff] [blame] | 273 | return std::nullopt; |
Patrick Venture | b28f432 | 2018-09-14 10:19:14 -0700 | [diff] [blame] | 274 | } |
Patrick Venture | b28f432 | 2018-09-14 10:19:14 -0700 | [diff] [blame] | 275 | |
William A. Kennington III | 2227bd5 | 2019-06-19 11:32:22 -0700 | [diff] [blame] | 276 | handle->setValues({1}); |
| 277 | // Default pause needed to guarantee sensors are ready |
| 278 | std::this_thread::sleep_for(std::chrono::milliseconds(500)); |
| 279 | return GpioLocker(std::move(handle)); |
Patrick Venture | b28f432 | 2018-09-14 10:19:14 -0700 | [diff] [blame] | 280 | } |
| 281 | |
Matthew Barth | 3581938 | 2018-04-18 14:53:01 -0500 | [diff] [blame] | 282 | } // namespace sensor |