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