blob: daeb1f0e5707854368e2148d93a2803ae25cbd36 [file] [log] [blame]
Matt Spinlerabf8da32017-04-27 14:08:45 -05001/**
2 * Copyright © 2017 IBM Corporation
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
Matthew Barth177fe982020-05-26 11:05:19 -050016#include "tach_sensor.hpp"
17
Matt Spinlerabf8da32017-04-27 14:08:45 -050018#include "fan.hpp"
Brad Bishop2a58e2c2017-07-30 13:49:09 -040019#include "sdbusplus.hpp"
Matthew Barth4d982852017-11-17 09:37:13 -060020#include "utility.hpp"
Matt Spinlerabf8da32017-04-27 14:08:45 -050021
Jolie Ku4c3c24f2020-09-09 11:01:46 +080022#include <fmt/format.h>
23
Matthew Barth177fe982020-05-26 11:05:19 -050024#include <phosphor-logging/elog.hpp>
25#include <phosphor-logging/log.hpp>
26
27#include <experimental/filesystem>
28#include <functional>
Matthew Barth7c23a042021-01-26 16:21:45 -060029#include <utility>
Matthew Barth177fe982020-05-26 11:05:19 -050030
Matt Spinlerabf8da32017-04-27 14:08:45 -050031namespace phosphor
32{
33namespace fan
34{
35namespace monitor
36{
37
Matt Spinlerebaae612017-04-27 14:21:48 -050038constexpr auto FAN_SENSOR_VALUE_INTF = "xyz.openbmc_project.Sensor.Value";
39constexpr auto FAN_TARGET_PROPERTY = "Target";
40constexpr auto FAN_VALUE_PROPERTY = "Value";
41
Matthew Barth4d982852017-11-17 09:37:13 -060042using namespace std::experimental::filesystem;
Matthew Barth177fe982020-05-26 11:05:19 -050043using InternalFailure =
44 sdbusplus::xyz::openbmc_project::Common::Error::InternalFailure;
Matt Spinlerebaae612017-04-27 14:21:48 -050045
46/**
47 * @brief Helper function to read a property
48 *
49 * @param[in] interface - the interface the property is on
50 * @param[in] propertName - the name of the property
51 * @param[in] path - the dbus path
Matt Spinlerebaae612017-04-27 14:21:48 -050052 * @param[in] bus - the dbus object
53 * @param[out] value - filled in with the property value
54 */
Matthew Barth177fe982020-05-26 11:05:19 -050055template <typename T>
56static void
57 readProperty(const std::string& interface, const std::string& propertyName,
58 const std::string& path, sdbusplus::bus::bus& bus, T& value)
Matt Spinlerebaae612017-04-27 14:21:48 -050059{
Matt Spinlerebaae612017-04-27 14:21:48 -050060 try
61 {
Matthew Barth177fe982020-05-26 11:05:19 -050062 value =
63 util::SDBusPlus::getProperty<T>(bus, path, interface, propertyName);
Matt Spinlerebaae612017-04-27 14:21:48 -050064 }
65 catch (std::exception& e)
66 {
67 phosphor::logging::log<phosphor::logging::level::ERR>(e.what());
68 }
69}
Matt Spinlerabf8da32017-04-27 14:08:45 -050070
Matthew Barth177fe982020-05-26 11:05:19 -050071TachSensor::TachSensor(Mode mode, sdbusplus::bus::bus& bus, Fan& fan,
72 const std::string& id, bool hasTarget, size_t funcDelay,
73 const std::string& interface, double factor,
Jolie Ku69f2f482020-10-21 09:59:43 +080074 int64_t offset, size_t method, size_t threshold,
75 size_t timeout, const std::optional<size_t>& errorDelay,
Matt Spinlerfdfcc672021-06-01 14:51:06 -060076 size_t countInterval, const sdeventplus::Event& event) :
Matt Spinlerabf8da32017-04-27 14:08:45 -050077 _bus(bus),
Matthew Barth177fe982020-05-26 11:05:19 -050078 _fan(fan), _name(FAN_SENSOR_PATH + id), _invName(path(fan.getName()) / id),
79 _hasTarget(hasTarget), _funcDelay(funcDelay), _interface(interface),
Jolie Ku69f2f482020-10-21 09:59:43 +080080 _factor(factor), _offset(offset), _method(method), _threshold(threshold),
81 _timeout(timeout), _timerMode(TimerMode::func),
82 _timer(event, std::bind(&Fan::updateState, &fan, std::ref(*this))),
Matt Spinlerfdfcc672021-06-01 14:51:06 -060083 _errorDelay(errorDelay), _countInterval(countInterval)
Matt Spinlerabf8da32017-04-27 14:08:45 -050084{
Mike Cappsce6820a2021-05-26 10:40:19 -040085 // Query functional state from inventory
86 // TODO - phosphor-fan-presence/issues/25
Mike Cappsce6820a2021-05-26 10:40:19 -040087
88 try
89 {
Mike Capps9ff48772021-07-19 14:49:43 -040090 auto service =
91 util::SDBusPlus::getService(_bus, util::INVENTORY_PATH + _invName,
92 util::OPERATIONAL_STATUS_INTF);
93
Mike Cappsce6820a2021-05-26 10:40:19 -040094 if (!service.empty())
95 {
96 _functional = util::SDBusPlus::getProperty<bool>(
97 service, util::INVENTORY_PATH + _invName,
98 util::OPERATIONAL_STATUS_INTF, util::FUNCTIONAL_PROPERTY);
99 }
100 else
101 {
102 // default to functional when service not up. Error handling done
103 // later
104 _functional = true;
Mike Cappsce6820a2021-05-26 10:40:19 -0400105 }
106 }
107 catch (util::DBusError& e)
108 {
109 log<level::DEBUG>(e.what());
110 _functional = true;
Mike Cappsce6820a2021-05-26 10:40:19 -0400111 }
112
113 if (!_functional && MethodMode::count == _method)
114 {
115 // force continual nonfunctional state
116 _counter = _threshold;
117 }
Brad Bishopedaeb312017-07-30 19:38:20 -0400118
Matthew Barth0a9fe162018-01-26 12:53:15 -0600119 // Load in current Target and Input values when entering monitor mode
Matt Spinlerb0412d02020-10-12 16:53:52 -0500120#ifndef MONITOR_USE_JSON
Matthew Barth0a9fe162018-01-26 12:53:15 -0600121 if (mode != Mode::init)
Brad Bishopedaeb312017-07-30 19:38:20 -0400122 {
Matt Spinlerb0412d02020-10-12 16:53:52 -0500123#endif
Matthew Barth0a9fe162018-01-26 12:53:15 -0600124 try
125 {
Matt Spinler4283c5d2021-03-01 15:56:00 -0600126 updateTachAndTarget();
Matthew Barth0a9fe162018-01-26 12:53:15 -0600127 }
Matt Spinler4283c5d2021-03-01 15:56:00 -0600128 catch (const std::exception& e)
Matthew Barth0a9fe162018-01-26 12:53:15 -0600129 {
Matt Spinler4283c5d2021-03-01 15:56:00 -0600130 // Until the parent Fan's monitor-ready timer expires, the
131 // object can be functional with a missing D-bus sensor.
Matthew Barth0a9fe162018-01-26 12:53:15 -0600132 }
133
134 auto match = getMatchString(FAN_SENSOR_VALUE_INTF);
135
136 tachSignal = std::make_unique<sdbusplus::server::match::match>(
Matthew Barth177fe982020-05-26 11:05:19 -0500137 _bus, match.c_str(),
138 [this](auto& msg) { this->handleTachChange(msg); });
Matthew Barth0a9fe162018-01-26 12:53:15 -0600139
140 if (_hasTarget)
141 {
Lei YU80f271b2018-01-31 15:24:46 +0800142 match = getMatchString(_interface);
Matthew Barth0a9fe162018-01-26 12:53:15 -0600143
144 targetSignal = std::make_unique<sdbusplus::server::match::match>(
Matthew Barth177fe982020-05-26 11:05:19 -0500145 _bus, match.c_str(),
146 [this](auto& msg) { this->handleTargetChange(msg); });
Matthew Barth0a9fe162018-01-26 12:53:15 -0600147 }
Matt Spinlerf13b42e2020-10-26 15:29:49 -0500148
149 if (_errorDelay)
150 {
151 _errorTimer = std::make_unique<
152 sdeventplus::utility::Timer<sdeventplus::ClockId::Monotonic>>(
153 event, std::bind(&Fan::sensorErrorTimerExpired, &fan,
154 std::ref(*this)));
155 }
Matt Spinlerfdfcc672021-06-01 14:51:06 -0600156
157 if (_method == MethodMode::count)
158 {
159 _countTimer = std::make_unique<
160 sdeventplus::utility::Timer<sdeventplus::ClockId::Monotonic>>(
161 event,
162 std::bind(&Fan::countTimerExpired, &fan, std::ref(*this)));
163 }
Matt Spinlerb0412d02020-10-12 16:53:52 -0500164#ifndef MONITOR_USE_JSON
Brad Bishopedaeb312017-07-30 19:38:20 -0400165 }
Matt Spinlerb0412d02020-10-12 16:53:52 -0500166#endif
Matt Spinlerebaae612017-04-27 14:21:48 -0500167}
168
Matt Spinler4283c5d2021-03-01 15:56:00 -0600169void TachSensor::updateTachAndTarget()
170{
171 _tachInput = util::SDBusPlus::getProperty<decltype(_tachInput)>(
172 _bus, _name, FAN_SENSOR_VALUE_INTF, FAN_VALUE_PROPERTY);
173
174 if (_hasTarget)
175 {
176 readProperty(_interface, FAN_TARGET_PROPERTY, _name, _bus, _tachTarget);
177 }
178}
179
Matt Spinlerebaae612017-04-27 14:21:48 -0500180std::string TachSensor::getMatchString(const std::string& interface)
181{
Matthew Barth177fe982020-05-26 11:05:19 -0500182 return sdbusplus::bus::match::rules::propertiesChanged(_name, interface);
Matt Spinlerebaae612017-04-27 14:21:48 -0500183}
184
Matthew Barthf552ea52018-01-15 16:22:04 -0600185uint64_t TachSensor::getTarget() const
186{
187 if (!_hasTarget)
188 {
189 return _fan.findTargetSpeed();
190 }
191 return _tachTarget;
192}
193
Matthew Barth7c23a042021-01-26 16:21:45 -0600194std::pair<uint64_t, uint64_t> TachSensor::getRange(const size_t deviation) const
195{
196 // Determine min/max range applying the deviation
197 uint64_t min = getTarget() * (100 - deviation) / 100;
198 uint64_t max = getTarget() * (100 + deviation) / 100;
199
200 // Adjust the min/max range by applying the factor & offset
201 min = min * _factor + _offset;
202 max = max * _factor + _offset;
203
204 return std::make_pair(min, max);
205}
206
Matthew Barthfcb0dbc2021-02-10 14:23:39 -0600207void TachSensor::processState()
208{
Matt Spinler623635c2021-03-29 13:13:59 -0500209 // This function runs from inside trust::Manager::checkTrust(), which,
210 // for sensors using the count method, runs right before process()
211 // is called anyway inside Fan::countTimerExpired() so don't call
212 // it now if using that method.
213 if (_method == MethodMode::timebased)
214 {
215 _fan.process(*this);
216 }
Matthew Barthfcb0dbc2021-02-10 14:23:39 -0600217}
218
219void TachSensor::resetMethod()
220{
221 switch (_method)
222 {
223 case MethodMode::timebased:
224 if (timerRunning())
225 {
226 stopTimer();
227 }
228 break;
229 case MethodMode::count:
230 if (_functional)
231 {
232 _counter = 0;
233 }
234 else
235 {
236 _counter = _threshold;
237 }
238 break;
239 }
240}
241
Matthew Barthd199dcd2017-11-20 10:36:41 -0600242void TachSensor::setFunctional(bool functional)
243{
244 _functional = functional;
245 updateInventory(_functional);
Matt Spinlerf13b42e2020-10-26 15:29:49 -0500246
247 if (!_errorTimer)
248 {
249 return;
250 }
251
252 if (!_functional)
253 {
254 if (_fan.present())
255 {
256 _errorTimer->restartOnce(std::chrono::seconds(*_errorDelay));
257 }
258 }
259 else if (_errorTimer->isEnabled())
260 {
261 _errorTimer->setEnabled(false);
262 }
Matthew Barthd199dcd2017-11-20 10:36:41 -0600263}
Matt Spinlerebaae612017-04-27 14:21:48 -0500264
Brad Bishop771659f2017-07-30 19:52:21 -0400265void TachSensor::handleTargetChange(sdbusplus::message::message& msg)
Matt Spinlerebaae612017-04-27 14:21:48 -0500266{
Matthew Barth177fe982020-05-26 11:05:19 -0500267 readPropertyFromMessage(msg, _interface, FAN_TARGET_PROPERTY, _tachTarget);
Matt Spinlerebaae612017-04-27 14:21:48 -0500268
Matthew Barth177fe982020-05-26 11:05:19 -0500269 // Check all tach sensors on the fan against the target
Matt Spinlerebaae612017-04-27 14:21:48 -0500270 _fan.tachChanged();
271}
272
Brad Bishop771659f2017-07-30 19:52:21 -0400273void TachSensor::handleTachChange(sdbusplus::message::message& msg)
Matt Spinlerebaae612017-04-27 14:21:48 -0500274{
Matthew Barth177fe982020-05-26 11:05:19 -0500275 readPropertyFromMessage(msg, FAN_SENSOR_VALUE_INTF, FAN_VALUE_PROPERTY,
276 _tachInput);
Matt Spinlerebaae612017-04-27 14:21:48 -0500277
Matthew Barth177fe982020-05-26 11:05:19 -0500278 // Check just this sensor against the target
279 _fan.tachChanged(*this);
Matt Spinlerabf8da32017-04-27 14:08:45 -0500280}
281
Matthew Barth3800ae72018-02-19 16:08:04 -0600282void TachSensor::startTimer(TimerMode mode)
283{
Matthew Barth0d0e3552021-01-27 12:31:28 -0600284 using namespace std::chrono;
285
William A. Kennington III8fd879f2018-10-30 19:49:29 -0700286 if (!timerRunning() || mode != _timerMode)
Matthew Barth3800ae72018-02-19 16:08:04 -0600287 {
Matt Spinler1d7379e2021-03-01 16:16:17 -0600288 log<level::DEBUG>(
Matthew Barth0d0e3552021-01-27 12:31:28 -0600289 fmt::format("Start timer({}) on tach sensor {}. [delay = {}s]",
290 mode, _name,
291 duration_cast<seconds>(getDelay(mode)).count())
292 .c_str());
William A. Kennington III8fd879f2018-10-30 19:49:29 -0700293 _timer.restartOnce(getDelay(mode));
Matthew Barth3800ae72018-02-19 16:08:04 -0600294 _timerMode = mode;
295 }
Matthew Barth3800ae72018-02-19 16:08:04 -0600296}
Matt Spinlerabf8da32017-04-27 14:08:45 -0500297
Matthew Barth3800ae72018-02-19 16:08:04 -0600298std::chrono::microseconds TachSensor::getDelay(TimerMode mode)
Matt Spinlera9406a72017-04-27 14:29:24 -0500299{
300 using namespace std::chrono;
301
Matthew Barth177fe982020-05-26 11:05:19 -0500302 switch (mode)
Matthew Barth3800ae72018-02-19 16:08:04 -0600303 {
Matthew Barth177fe982020-05-26 11:05:19 -0500304 case TimerMode::nonfunc:
305 return duration_cast<microseconds>(seconds(_timeout));
306 case TimerMode::func:
307 return duration_cast<microseconds>(seconds(_funcDelay));
308 default:
309 // Log an internal error for undefined timer mode
310 log<level::ERR>("Undefined timer mode",
311 entry("TIMER_MODE=%u", mode));
312 elog<InternalFailure>();
313 return duration_cast<microseconds>(seconds(0));
Matthew Barth3800ae72018-02-19 16:08:04 -0600314 }
Matt Spinlera9406a72017-04-27 14:29:24 -0500315}
316
Jolie Ku69f2f482020-10-21 09:59:43 +0800317void TachSensor::setCounter(bool count)
318{
319 if (count)
320 {
321 if (_counter < _threshold)
322 {
323 ++_counter;
Matt Spinler623635c2021-03-29 13:13:59 -0500324 log<level::DEBUG>(
325 fmt::format(
326 "Incremented error counter on {} to {} (threshold {})",
327 _name, _counter, _threshold)
328 .c_str());
Jolie Ku69f2f482020-10-21 09:59:43 +0800329 }
330 }
331 else
332 {
333 if (_counter > 0)
334 {
335 --_counter;
Matt Spinler623635c2021-03-29 13:13:59 -0500336 log<level::DEBUG>(
337 fmt::format(
338 "Decremented error counter on {} to {} (threshold {})",
339 _name, _counter, _threshold)
340 .c_str());
Jolie Ku69f2f482020-10-21 09:59:43 +0800341 }
342 }
343}
344
Matt Spinlerfdfcc672021-06-01 14:51:06 -0600345void TachSensor::startCountTimer()
346{
347 if (_countTimer)
348 {
349 log<level::DEBUG>(
350 fmt::format("Starting count timer on sensor {}", _name).c_str());
351 _countTimer->restart(std::chrono::seconds(_countInterval));
352 }
353}
354
355void TachSensor::stopCountTimer()
356{
357 if (_countTimer && _countTimer->isEnabled())
358 {
359 log<level::DEBUG>(
360 fmt::format("Stopping count timer on tach sensor {}.", _name)
361 .c_str());
362 _countTimer->setEnabled(false);
363 }
364}
365
Matthew Barth4d982852017-11-17 09:37:13 -0600366void TachSensor::updateInventory(bool functional)
367{
Matthew Barth177fe982020-05-26 11:05:19 -0500368 auto objectMap =
369 util::getObjMap<bool>(_invName, util::OPERATIONAL_STATUS_INTF,
370 util::FUNCTIONAL_PROPERTY, functional);
Matthew Barth4d982852017-11-17 09:37:13 -0600371 auto response = util::SDBusPlus::lookupAndCallMethod(
Matthew Barth177fe982020-05-26 11:05:19 -0500372 _bus, util::INVENTORY_PATH, util::INVENTORY_INTF, "Notify", objectMap);
Matthew Barth4d982852017-11-17 09:37:13 -0600373 if (response.is_method_error())
374 {
375 log<level::ERR>("Error in notify update of tach sensor inventory");
376 }
377}
Matt Spinlera9406a72017-04-27 14:29:24 -0500378
Matthew Barth177fe982020-05-26 11:05:19 -0500379} // namespace monitor
380} // namespace fan
381} // namespace phosphor