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