blob: ddca883e7c40d137ffc6a290400229ca83929b78 [file] [log] [blame]
Patrick Venture043d3232018-08-31 10:10:53 -07001#include "config.h"
2
3#include "sensor.hpp"
4
5#include "env.hpp"
Patrick Ventureb28f4322018-09-14 10:19:14 -07006#include "gpio_handle.hpp"
Patrick Venture043d3232018-08-31 10:10:53 -07007#include "hwmon.hpp"
8#include "sensorset.hpp"
9#include "sysfs.hpp"
10
Matt Spinler5e034af2020-06-24 15:21:53 -050011#include <fmt/format.h>
12
Brandon Kim86dcac82019-06-18 17:48:51 -070013#include <cassert>
William A. Kennington III2227bd52019-06-19 11:32:22 -070014#include <chrono>
James Feistee73f5b2018-08-01 16:31:42 -070015#include <cmath>
Matthew Barthcb3daaf2018-05-07 15:03:16 -050016#include <cstring>
Patrick Venture9e997b42019-03-08 13:42:10 -080017#include <filesystem>
Brandon Kim6d50c3e2019-08-09 15:38:53 -070018#include <future>
Matthew Barth35819382018-04-18 14:53:01 -050019#include <phosphor-logging/elog-errors.hpp>
Patrick Ventureb28f4322018-09-14 10:19:14 -070020#include <thread>
21#include <xyz/openbmc_project/Common/error.hpp>
Matthew Barth35819382018-04-18 14:53:01 -050022#include <xyz/openbmc_project/Sensor/Device/error.hpp>
23
Matthew Barth35819382018-04-18 14:53:01 -050024namespace sensor
25{
26
Matthew Barthcb3daaf2018-05-07 15:03:16 -050027using namespace phosphor::logging;
Patrick Ventureb28f4322018-09-14 10:19:14 -070028using namespace sdbusplus::xyz::openbmc_project::Common::Error;
Matthew Barthcb3daaf2018-05-07 15:03:16 -050029
James Feistee73f5b2018-08-01 16:31:42 -070030// todo: this can be simplified once we move to the double interface
Matthew Barth2e41b132018-05-07 14:15:45 -050031Sensor::Sensor(const SensorSet::key_type& sensor,
Patrick Venture2864b062018-12-19 08:13:41 -080032 const hwmonio::HwmonIOInterface* ioAccess,
33 const std::string& devPath) :
Patrick Venture12659aa2018-12-19 13:58:43 -080034 _sensor(sensor),
Brandon Kim86dcac82019-06-18 17:48:51 -070035 _ioAccess(ioAccess), _devPath(devPath), _scale(0), _hasFaultFile(false)
Matthew Barth9c431062018-05-07 13:55:29 -050036{
Patrick Ventureb28f4322018-09-14 10:19:14 -070037 auto chip = env::getEnv("GPIOCHIP", sensor);
38 auto access = env::getEnv("GPIO", sensor);
39 if (!access.empty() && !chip.empty())
40 {
Patrick Venture12659aa2018-12-19 13:58:43 -080041 _handle = gpio::BuildGpioHandle(chip, access);
Patrick Ventureb28f4322018-09-14 10:19:14 -070042
Patrick Venture12659aa2018-12-19 13:58:43 -080043 if (!_handle)
Patrick Ventureb28f4322018-09-14 10:19:14 -070044 {
45 log<level::ERR>("Unable to set up gpio locking");
46 elog<InternalFailure>();
47 }
48 }
49
Matthew Barthac473092018-05-07 14:41:46 -050050 auto gain = env::getEnv("GAIN", sensor);
51 if (!gain.empty())
52 {
Patrick Venture12659aa2018-12-19 13:58:43 -080053 _sensorAdjusts.gain = std::stod(gain);
Matthew Barthac473092018-05-07 14:41:46 -050054 }
55
56 auto offset = env::getEnv("OFFSET", sensor);
57 if (!offset.empty())
58 {
Patrick Venture12659aa2018-12-19 13:58:43 -080059 _sensorAdjusts.offset = std::stoi(offset);
Matthew Barthac473092018-05-07 14:41:46 -050060 }
61 auto senRmRCs = env::getEnv("REMOVERCS", sensor);
62 // Add sensor removal return codes defined per sensor
63 addRemoveRCs(senRmRCs);
Matthew Barth9c431062018-05-07 13:55:29 -050064}
65
Matthew Barthcb3daaf2018-05-07 15:03:16 -050066void Sensor::addRemoveRCs(const std::string& rcList)
67{
68 if (rcList.empty())
69 {
70 return;
71 }
72
73 // Convert to a char* for strtok
Patrick Venture043d3232018-08-31 10:10:53 -070074 std::vector<char> rmRCs(rcList.c_str(), rcList.c_str() + rcList.size() + 1);
Matthew Barthcb3daaf2018-05-07 15:03:16 -050075 auto rmRC = std::strtok(&rmRCs[0], ", ");
76 while (rmRC != nullptr)
77 {
78 try
79 {
Patrick Venture12659aa2018-12-19 13:58:43 -080080 _sensorAdjusts.rmRCs.insert(std::stoi(rmRC));
Matthew Barthcb3daaf2018-05-07 15:03:16 -050081 }
82 catch (const std::logic_error& le)
83 {
84 // Unable to convert to int, continue to next token
Patrick Venture12659aa2018-12-19 13:58:43 -080085 std::string name = _sensor.first + "_" + _sensor.second;
Matthew Barthcb3daaf2018-05-07 15:03:16 -050086 log<level::INFO>("Unable to convert sensor removal return code",
87 entry("SENSOR=%s", name.c_str()),
88 entry("RC=%s", rmRC),
89 entry("EXCEPTION=%s", le.what()));
90 }
91 rmRC = std::strtok(nullptr, ", ");
92 }
93}
94
James Feistee73f5b2018-08-01 16:31:42 -070095SensorValueType Sensor::adjustValue(SensorValueType value)
Matthew Barthcb3daaf2018-05-07 15:03:16 -050096{
97// Because read doesn't have an out pointer to store errors.
98// let's assume negative values are errors if they have this
99// set.
Matt Spinlerd8cacfd2021-04-26 09:56:21 -0500100#if NEGATIVE_ERRNO_ON_FAIL
Matthew Barthcb3daaf2018-05-07 15:03:16 -0500101 if (value < 0)
102 {
103 return value;
104 }
105#endif
106
107 // Adjust based on gain and offset
Patrick Venture12659aa2018-12-19 13:58:43 -0800108 value = static_cast<decltype(value)>(static_cast<double>(value) *
109 _sensorAdjusts.gain +
110 _sensorAdjusts.offset);
Matthew Barthcb3daaf2018-05-07 15:03:16 -0500111
James Feistee73f5b2018-08-01 16:31:42 -0700112 if constexpr (std::is_same<SensorValueType, double>::value)
113 {
Patrick Venture12659aa2018-12-19 13:58:43 -0800114 value *= std::pow(10, _scale);
James Feistee73f5b2018-08-01 16:31:42 -0700115 }
116
Matthew Barthcb3daaf2018-05-07 15:03:16 -0500117 return value;
118}
119
Patrick Venture043d3232018-08-31 10:10:53 -0700120std::shared_ptr<ValueObject> Sensor::addValue(const RetryIO& retryIO,
Brandon Kim6d50c3e2019-08-09 15:38:53 -0700121 ObjectInfo& info,
122 TimedoutMap& timedoutMap)
Matthew Barthcb3daaf2018-05-07 15:03:16 -0500123{
124 static constexpr bool deferSignals = true;
125
126 // Get the initial value for the value interface.
127 auto& bus = *std::get<sdbusplus::bus::bus*>(info);
Patrick Venture62067232019-06-19 17:39:33 -0700128 auto& obj = std::get<InterfaceMap>(info);
Matthew Barthcb3daaf2018-05-07 15:03:16 -0500129 auto& objPath = std::get<std::string>(info);
130
James Feistee73f5b2018-08-01 16:31:42 -0700131 SensorValueType val = 0;
Matthew Barthcb3daaf2018-05-07 15:03:16 -0500132
Brandon Kim86dcac82019-06-18 17:48:51 -0700133 auto& statusIface = std::any_cast<std::shared_ptr<StatusObject>&>(
134 obj[InterfaceType::STATUS]);
135 // As long as addStatus is called before addValue, statusIface
136 // should never be nullptr
137 assert(statusIface);
138
139 // Only read the input value if the status is functional
140 if (statusIface->functional())
Matthew Barthcb3daaf2018-05-07 15:03:16 -0500141 {
Matt Spinlerd8cacfd2021-04-26 09:56:21 -0500142#if UPDATE_FUNCTIONAL_ON_FAIL
Brandon Kim79205b22019-06-20 12:18:24 -0700143 try
144#endif
145 {
146 // RAII object for GPIO unlock / lock
William A. Kennington III2227bd52019-06-19 11:32:22 -0700147 auto locker = gpioUnlock(getGpio());
Patrick Ventureb28f4322018-09-14 10:19:14 -0700148
Brandon Kim6d50c3e2019-08-09 15:38:53 -0700149 // For sensors with attribute ASYNC_READ_TIMEOUT,
150 // spawn a thread with timeout
151 auto asyncReadTimeout = env::getEnv("ASYNC_READ_TIMEOUT", _sensor);
152 if (!asyncReadTimeout.empty())
153 {
154 std::chrono::milliseconds asyncTimeout{
155 std::stoi(asyncReadTimeout)};
156 val = asyncRead(_sensor, _ioAccess, asyncTimeout, timedoutMap,
157 _sensor.first, _sensor.second,
Brandon Kim79205b22019-06-20 12:18:24 -0700158 hwmon::entry::cinput, std::get<size_t>(retryIO),
159 std::get<std::chrono::milliseconds>(retryIO));
Brandon Kim6d50c3e2019-08-09 15:38:53 -0700160 }
161 else
162 {
163 // Retry for up to a second if device is busy
164 // or has a transient error.
165 val = _ioAccess->read(
166 _sensor.first, _sensor.second, hwmon::entry::cinput,
167 std::get<size_t>(retryIO),
168 std::get<std::chrono::milliseconds>(retryIO));
169 }
Brandon Kim79205b22019-06-20 12:18:24 -0700170 }
Matt Spinlerd8cacfd2021-04-26 09:56:21 -0500171#if UPDATE_FUNCTIONAL_ON_FAIL
Brandon Kim79205b22019-06-20 12:18:24 -0700172 catch (const std::system_error& e)
173 {
174 // Catch the exception here and update the functional property.
175 // By catching the exception, it will not propagate it up the stack
176 // and thus the code will skip the "Remove RCs" check in
177 // MainLoop::getObject and will not exit on failure.
178 statusIface->functional(false);
179 }
180#endif
Matthew Barthcb3daaf2018-05-07 15:03:16 -0500181 }
182
Patrick Venture043d3232018-08-31 10:10:53 -0700183 auto iface =
184 std::make_shared<ValueObject>(bus, objPath.c_str(), deferSignals);
Matthew Barthcb3daaf2018-05-07 15:03:16 -0500185
186 hwmon::Attributes attrs;
Patrick Venture12659aa2018-12-19 13:58:43 -0800187 if (hwmon::getAttributes(_sensor.first, attrs))
Matthew Barthcb3daaf2018-05-07 15:03:16 -0500188 {
189 iface->unit(hwmon::getUnit(attrs));
James Feistee73f5b2018-08-01 16:31:42 -0700190
Patrick Venture12659aa2018-12-19 13:58:43 -0800191 _scale = hwmon::getScale(attrs);
Matthew Barthcb3daaf2018-05-07 15:03:16 -0500192 }
193
Matt Spinler7ab1b252020-07-22 15:11:02 -0500194 val = adjustValue(val);
195 iface->value(val);
196
Patrick Venture12659aa2018-12-19 13:58:43 -0800197 auto maxValue = env::getEnv("MAXVALUE", _sensor);
Patrick Venture043d3232018-08-31 10:10:53 -0700198 if (!maxValue.empty())
Matthew Barthcb3daaf2018-05-07 15:03:16 -0500199 {
200 iface->maxValue(std::stoll(maxValue));
201 }
Patrick Venture12659aa2018-12-19 13:58:43 -0800202 auto minValue = env::getEnv("MINVALUE", _sensor);
Patrick Venture043d3232018-08-31 10:10:53 -0700203 if (!minValue.empty())
Matthew Barthcb3daaf2018-05-07 15:03:16 -0500204 {
205 iface->minValue(std::stoll(minValue));
206 }
207
208 obj[InterfaceType::VALUE] = iface;
209 return iface;
210}
211
Matthew Barth2e41b132018-05-07 14:15:45 -0500212std::shared_ptr<StatusObject> Sensor::addStatus(ObjectInfo& info)
Matthew Barth35819382018-04-18 14:53:01 -0500213{
Patrick Venture9e997b42019-03-08 13:42:10 -0800214 namespace fs = std::filesystem;
Matthew Barth35819382018-04-18 14:53:01 -0500215
216 std::shared_ptr<StatusObject> iface = nullptr;
Matthew Barth35819382018-04-18 14:53:01 -0500217 auto& objPath = std::get<std::string>(info);
Patrick Venture62067232019-06-19 17:39:33 -0700218 auto& obj = std::get<InterfaceMap>(info);
Matthew Barth35819382018-04-18 14:53:01 -0500219
220 // Check if fault sysfs file exists
Patrick Venture12659aa2018-12-19 13:58:43 -0800221 std::string faultName = _sensor.first;
222 std::string faultID = _sensor.second;
Matthew Barth35819382018-04-18 14:53:01 -0500223 std::string entry = hwmon::entry::fault;
224
Brandon Kim86dcac82019-06-18 17:48:51 -0700225 bool functional = true;
Patrick Venture043d3232018-08-31 10:10:53 -0700226 auto sysfsFullPath =
Patrick Venture12659aa2018-12-19 13:58:43 -0800227 sysfs::make_sysfs_path(_ioAccess->path(), faultName, faultID, entry);
Matthew Barth35819382018-04-18 14:53:01 -0500228 if (fs::exists(sysfsFullPath))
229 {
Brandon Kim86dcac82019-06-18 17:48:51 -0700230 _hasFaultFile = true;
Matthew Barth35819382018-04-18 14:53:01 -0500231 try
232 {
Patrick Venture12659aa2018-12-19 13:58:43 -0800233 uint32_t fault = _ioAccess->read(faultName, faultID, entry,
234 hwmonio::retries, hwmonio::delay);
Matthew Barth35819382018-04-18 14:53:01 -0500235 if (fault != 0)
236 {
237 functional = false;
238 }
239 }
240 catch (const std::system_error& e)
241 {
Patrick Venture043d3232018-08-31 10:10:53 -0700242 using namespace sdbusplus::xyz::openbmc_project::Sensor::Device::
243 Error;
244 using metadata = xyz::openbmc_project::Sensor::Device::ReadFailure;
Matthew Barth35819382018-04-18 14:53:01 -0500245
Patrick Venture12659aa2018-12-19 13:58:43 -0800246 report<ReadFailure>(
247 metadata::CALLOUT_ERRNO(e.code().value()),
248 metadata::CALLOUT_DEVICE_PATH(_devPath.c_str()));
Matthew Barth35819382018-04-18 14:53:01 -0500249
Matt Spinler6a391de2020-07-08 13:03:10 -0500250 log<level::INFO>(fmt::format("Failing sysfs file: {} errno {}",
251 sysfsFullPath, e.code().value())
252 .c_str());
Matthew Barth35819382018-04-18 14:53:01 -0500253 }
Matthew Barth35819382018-04-18 14:53:01 -0500254 }
255
Brandon Kim86dcac82019-06-18 17:48:51 -0700256 static constexpr bool deferSignals = true;
257 auto& bus = *std::get<sdbusplus::bus::bus*>(info);
258
259 iface = std::make_shared<StatusObject>(bus, objPath.c_str(), deferSignals);
260 // Set functional property
261 iface->functional(functional);
262
263 obj[InterfaceType::STATUS] = iface;
264
Matthew Barth35819382018-04-18 14:53:01 -0500265 return iface;
266}
267
William A. Kennington III2227bd52019-06-19 11:32:22 -0700268void gpioLock(const gpioplus::HandleInterface*&& handle)
Brandon Kimdb76d492019-06-17 11:53:04 -0700269{
William A. Kennington III2227bd52019-06-19 11:32:22 -0700270 handle->setValues({0});
Brandon Kimdb76d492019-06-17 11:53:04 -0700271}
272
William A. Kennington III2227bd52019-06-19 11:32:22 -0700273std::optional<GpioLocker> gpioUnlock(const gpioplus::HandleInterface* handle)
Brandon Kimdb76d492019-06-17 11:53:04 -0700274{
William A. Kennington III2227bd52019-06-19 11:32:22 -0700275 if (handle == nullptr)
Patrick Ventureb28f4322018-09-14 10:19:14 -0700276 {
William A. Kennington III2227bd52019-06-19 11:32:22 -0700277 return std::nullopt;
Patrick Ventureb28f4322018-09-14 10:19:14 -0700278 }
Patrick Ventureb28f4322018-09-14 10:19:14 -0700279
William A. Kennington III2227bd52019-06-19 11:32:22 -0700280 handle->setValues({1});
281 // Default pause needed to guarantee sensors are ready
282 std::this_thread::sleep_for(std::chrono::milliseconds(500));
283 return GpioLocker(std::move(handle));
Patrick Ventureb28f4322018-09-14 10:19:14 -0700284}
285
Brandon Kim6d50c3e2019-08-09 15:38:53 -0700286SensorValueType asyncRead(const SensorSet::key_type& sensorSetKey,
287 const hwmonio::HwmonIOInterface* ioAccess,
288 std::chrono::milliseconds asyncTimeout,
289 TimedoutMap& timedoutMap, const std::string& type,
290 const std::string& id, const std::string& sensor,
291 const size_t retries,
292 const std::chrono::milliseconds delay)
293{
294 // Default async read timeout
295 bool valueIsValid = false;
296 std::future<int64_t> asyncThread;
297
298 auto asyncIter = timedoutMap.find(sensorSetKey);
299 if (asyncIter == timedoutMap.end())
300 {
301 // If sensor not found in timedoutMap, spawn an async thread
302 asyncThread =
303 std::async(std::launch::async, &hwmonio::HwmonIOInterface::read,
304 ioAccess, type, id, sensor, retries, delay);
305 valueIsValid = true;
306 }
307 else
308 {
309 // If we already have the async thread in the timedoutMap, it means this
310 // sensor has already timed out in the previous reads. No need to wait
311 // on subsequent reads - proceed to check the future_status to see when
312 // the async thread finishes
313 asyncTimeout = std::chrono::seconds(0);
314 asyncThread = std::move(asyncIter->second);
315 }
316
317 // TODO: This is still not a true asynchronous read as it still blocks the
318 // main thread for asyncTimeout amount of time. To make this completely
319 // asynchronous, schedule a read and register a callback to update the
320 // sensor value
321 std::future_status status = asyncThread.wait_for(asyncTimeout);
322 switch (status)
323 {
324 case std::future_status::ready:
325 // Read has finished
326 if (valueIsValid)
327 {
328 return asyncThread.get();
329 // Good sensor reads should skip the code below
330 }
331 // Async read thread has completed but had previously timed out (was
332 // found in the timedoutMap). Erase from timedoutMap and throw to
333 // allow retry in the next read cycle. Not returning the read value
334 // as the sensor reading may be bad / corrupted if it took so long.
335 timedoutMap.erase(sensorSetKey);
336 throw AsyncSensorReadTimeOut();
337 default:
338 // Read timed out so add the thread to the timedoutMap (if the entry
339 // already exists, operator[] updates it).
340 //
341 // Keeping the timed out futures in a map is required to prevent
342 // their destructor from being called when returning from this
343 // stack. The destructor will otherwise block until the read
344 // completes due to the limitation of std::async.
345 timedoutMap[sensorSetKey] = std::move(asyncThread);
346 throw AsyncSensorReadTimeOut();
347 }
348}
349
Matthew Barth35819382018-04-18 14:53:01 -0500350} // namespace sensor