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