blob: 0ff1828f72214e0a8805ba48e501ad1424cb5255 [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
Matt Spinler2ea9a592022-04-08 14:36:22 -050027#include <filesystem>
Matthew Barth177fe982020-05-26 11:05:19 -050028#include <functional>
Matthew Barth8a8aa442021-11-19 14:13:13 -060029#include <optional>
Matthew Barth7c23a042021-01-26 16:21:45 -060030#include <utility>
Matthew Barth177fe982020-05-26 11:05:19 -050031
Matt Spinlerabf8da32017-04-27 14:08:45 -050032namespace phosphor
33{
34namespace fan
35{
36namespace monitor
37{
38
Matt Spinlerebaae612017-04-27 14:21:48 -050039constexpr auto FAN_TARGET_PROPERTY = "Target";
40constexpr auto FAN_VALUE_PROPERTY = "Value";
Mike Capps7b34ee02022-05-04 14:16:12 -040041constexpr auto MAX_PREV_TACHS = 8;
42constexpr auto MAX_PREV_TARGETS = 8;
Matt Spinlerebaae612017-04-27 14:21:48 -050043
Matt Spinler2ea9a592022-04-08 14:36:22 -050044namespace fs = std::filesystem;
Matthew Barth177fe982020-05-26 11:05:19 -050045using InternalFailure =
46 sdbusplus::xyz::openbmc_project::Common::Error::InternalFailure;
Matt Spinlerebaae612017-04-27 14:21:48 -050047
48/**
49 * @brief Helper function to read a property
50 *
51 * @param[in] interface - the interface the property is on
52 * @param[in] propertName - the name of the property
53 * @param[in] path - the dbus path
Matt Spinlerebaae612017-04-27 14:21:48 -050054 * @param[in] bus - the dbus object
55 * @param[out] value - filled in with the property value
56 */
Matthew Barth177fe982020-05-26 11:05:19 -050057template <typename T>
58static void
59 readProperty(const std::string& interface, const std::string& propertyName,
60 const std::string& path, sdbusplus::bus::bus& bus, T& value)
Matt Spinlerebaae612017-04-27 14:21:48 -050061{
Matt Spinlerebaae612017-04-27 14:21:48 -050062 try
63 {
Matthew Barth177fe982020-05-26 11:05:19 -050064 value =
65 util::SDBusPlus::getProperty<T>(bus, path, interface, propertyName);
Matt Spinlerebaae612017-04-27 14:21:48 -050066 }
Patrick Williamsddb773b2021-10-06 11:24:49 -050067 catch (const std::exception& e)
Matt Spinlerebaae612017-04-27 14:21:48 -050068 {
69 phosphor::logging::log<phosphor::logging::level::ERR>(e.what());
70 }
71}
Matt Spinlerabf8da32017-04-27 14:08:45 -050072
Matthew Barth177fe982020-05-26 11:05:19 -050073TachSensor::TachSensor(Mode mode, sdbusplus::bus::bus& bus, Fan& fan,
74 const std::string& id, bool hasTarget, size_t funcDelay,
75 const std::string& interface, double factor,
Jolie Ku69f2f482020-10-21 09:59:43 +080076 int64_t offset, size_t method, size_t threshold,
Matthew Barth8a8aa442021-11-19 14:13:13 -060077 bool ignoreAboveMax, size_t timeout,
78 const std::optional<size_t>& errorDelay,
Matt Spinlerfdfcc672021-06-01 14:51:06 -060079 size_t countInterval, const sdeventplus::Event& event) :
Matt Spinlerabf8da32017-04-27 14:08:45 -050080 _bus(bus),
Matt Spinler2ea9a592022-04-08 14:36:22 -050081 _fan(fan), _name(FAN_SENSOR_PATH + id),
82 _invName(fs::path(fan.getName()) / id), _hasTarget(hasTarget),
83 _funcDelay(funcDelay), _interface(interface), _factor(factor),
84 _offset(offset), _method(method), _threshold(threshold),
Matthew Barth8a8aa442021-11-19 14:13:13 -060085 _ignoreAboveMax(ignoreAboveMax), _timeout(timeout),
86 _timerMode(TimerMode::func),
Jolie Ku69f2f482020-10-21 09:59:43 +080087 _timer(event, std::bind(&Fan::updateState, &fan, std::ref(*this))),
Matt Spinlerfdfcc672021-06-01 14:51:06 -060088 _errorDelay(errorDelay), _countInterval(countInterval)
Matt Spinlerabf8da32017-04-27 14:08:45 -050089{
Mike Cappsce6820a2021-05-26 10:40:19 -040090 // Query functional state from inventory
91 // TODO - phosphor-fan-presence/issues/25
Mike Cappsce6820a2021-05-26 10:40:19 -040092
Mike Capps7b34ee02022-05-04 14:16:12 -040093 _prevTachs.resize(MAX_PREV_TACHS);
94
95 if (_hasTarget)
96 {
97 _prevTargets.resize(MAX_PREV_TARGETS);
98 }
99
Mike Capps3edb0652021-09-01 18:30:21 -0400100 _functional = true;
101
Mike Cappsce6820a2021-05-26 10:40:19 -0400102 try
103 {
Mike Capps3edb0652021-09-01 18:30:21 -0400104 // see if any object paths in the inventory have the
105 // OperationalStatus interface.
106 auto subtree = util::SDBusPlus::getSubTreeRaw(
107 _bus, util::INVENTORY_PATH, util::OPERATIONAL_STATUS_INTF, 0);
Mike Capps9ff48772021-07-19 14:49:43 -0400108
Mike Capps3edb0652021-09-01 18:30:21 -0400109 // if the tach sensor's entry already exists, we for sure can
110 // read its functional state from the inventory
111 if (subtree.end() != subtree.find(util::INVENTORY_PATH + _invName))
Mike Cappsce6820a2021-05-26 10:40:19 -0400112 {
113 _functional = util::SDBusPlus::getProperty<bool>(
Mike Capps3edb0652021-09-01 18:30:21 -0400114 _bus, util::INVENTORY_PATH + _invName,
Mike Cappsce6820a2021-05-26 10:40:19 -0400115 util::OPERATIONAL_STATUS_INTF, util::FUNCTIONAL_PROPERTY);
116 }
Mike Cappsce6820a2021-05-26 10:40:19 -0400117 }
Patrick Williamsddb773b2021-10-06 11:24:49 -0500118 catch (const util::DBusError& e)
Mike Cappsce6820a2021-05-26 10:40:19 -0400119 {
120 log<level::DEBUG>(e.what());
Mike Cappsce6820a2021-05-26 10:40:19 -0400121 }
122
Mike Capps3edb0652021-09-01 18:30:21 -0400123 updateInventory(_functional);
124
Mike Cappsce6820a2021-05-26 10:40:19 -0400125 if (!_functional && MethodMode::count == _method)
126 {
127 // force continual nonfunctional state
128 _counter = _threshold;
129 }
Brad Bishopedaeb312017-07-30 19:38:20 -0400130
Matthew Barth0a9fe162018-01-26 12:53:15 -0600131 // Load in current Target and Input values when entering monitor mode
Matt Spinlerb0412d02020-10-12 16:53:52 -0500132#ifndef MONITOR_USE_JSON
Matthew Barth0a9fe162018-01-26 12:53:15 -0600133 if (mode != Mode::init)
Brad Bishopedaeb312017-07-30 19:38:20 -0400134 {
Matt Spinlerb0412d02020-10-12 16:53:52 -0500135#endif
Matthew Barth0a9fe162018-01-26 12:53:15 -0600136 try
137 {
Matt Spinler4283c5d2021-03-01 15:56:00 -0600138 updateTachAndTarget();
Matthew Barth0a9fe162018-01-26 12:53:15 -0600139 }
Matt Spinler4283c5d2021-03-01 15:56:00 -0600140 catch (const std::exception& e)
Matthew Barth0a9fe162018-01-26 12:53:15 -0600141 {
Matt Spinler4283c5d2021-03-01 15:56:00 -0600142 // Until the parent Fan's monitor-ready timer expires, the
143 // object can be functional with a missing D-bus sensor.
Matthew Barth0a9fe162018-01-26 12:53:15 -0600144 }
145
Mike Cappsfdcd5db2021-05-20 12:47:10 -0400146 auto match = getMatchString(util::FAN_SENSOR_VALUE_INTF);
Matthew Barth0a9fe162018-01-26 12:53:15 -0600147
Patrick Williams3ea9ec22021-11-19 12:21:08 -0600148 tachSignal = std::make_unique<sdbusplus::bus::match_t>(
Matthew Barth177fe982020-05-26 11:05:19 -0500149 _bus, match.c_str(),
150 [this](auto& msg) { this->handleTachChange(msg); });
Matthew Barth0a9fe162018-01-26 12:53:15 -0600151
152 if (_hasTarget)
153 {
Lei YU80f271b2018-01-31 15:24:46 +0800154 match = getMatchString(_interface);
Matthew Barth0a9fe162018-01-26 12:53:15 -0600155
Patrick Williams3ea9ec22021-11-19 12:21:08 -0600156 targetSignal = std::make_unique<sdbusplus::bus::match_t>(
Matthew Barth177fe982020-05-26 11:05:19 -0500157 _bus, match.c_str(),
158 [this](auto& msg) { this->handleTargetChange(msg); });
Matthew Barth0a9fe162018-01-26 12:53:15 -0600159 }
Matt Spinlerf13b42e2020-10-26 15:29:49 -0500160
161 if (_errorDelay)
162 {
163 _errorTimer = std::make_unique<
164 sdeventplus::utility::Timer<sdeventplus::ClockId::Monotonic>>(
165 event, std::bind(&Fan::sensorErrorTimerExpired, &fan,
166 std::ref(*this)));
167 }
Matt Spinlerfdfcc672021-06-01 14:51:06 -0600168
169 if (_method == MethodMode::count)
170 {
171 _countTimer = std::make_unique<
172 sdeventplus::utility::Timer<sdeventplus::ClockId::Monotonic>>(
173 event,
174 std::bind(&Fan::countTimerExpired, &fan, std::ref(*this)));
175 }
Matt Spinlerb0412d02020-10-12 16:53:52 -0500176#ifndef MONITOR_USE_JSON
Brad Bishopedaeb312017-07-30 19:38:20 -0400177 }
Matt Spinlerb0412d02020-10-12 16:53:52 -0500178#endif
Matt Spinlerebaae612017-04-27 14:21:48 -0500179}
180
Matt Spinler4283c5d2021-03-01 15:56:00 -0600181void TachSensor::updateTachAndTarget()
182{
183 _tachInput = util::SDBusPlus::getProperty<decltype(_tachInput)>(
Mike Cappsfdcd5db2021-05-20 12:47:10 -0400184 _bus, _name, util::FAN_SENSOR_VALUE_INTF, FAN_VALUE_PROPERTY);
Matt Spinler4283c5d2021-03-01 15:56:00 -0600185
186 if (_hasTarget)
187 {
188 readProperty(_interface, FAN_TARGET_PROPERTY, _name, _bus, _tachTarget);
Mike Capps7b34ee02022-05-04 14:16:12 -0400189
190 // record previous target value
191 if (_prevTargets.front() != _tachTarget)
192 {
193 _prevTargets.push_front(_tachTarget);
194
195 _prevTargets.pop_back();
196 }
Matt Spinler4283c5d2021-03-01 15:56:00 -0600197 }
Mike Capps7b34ee02022-05-04 14:16:12 -0400198
199 // record previous tach value
200 _prevTachs.push_front(_tachInput);
201
202 _prevTachs.pop_back();
Matt Spinler4283c5d2021-03-01 15:56:00 -0600203}
204
Matt Spinlerebaae612017-04-27 14:21:48 -0500205std::string TachSensor::getMatchString(const std::string& interface)
206{
Matthew Barth177fe982020-05-26 11:05:19 -0500207 return sdbusplus::bus::match::rules::propertiesChanged(_name, interface);
Matt Spinlerebaae612017-04-27 14:21:48 -0500208}
209
Matthew Barthf552ea52018-01-15 16:22:04 -0600210uint64_t TachSensor::getTarget() const
211{
212 if (!_hasTarget)
213 {
214 return _fan.findTargetSpeed();
215 }
216 return _tachTarget;
217}
218
Matthew Barth8a8aa442021-11-19 14:13:13 -0600219std::pair<uint64_t, std::optional<uint64_t>>
220 TachSensor::getRange(const size_t deviation) const
Matthew Barth7c23a042021-01-26 16:21:45 -0600221{
222 // Determine min/max range applying the deviation
223 uint64_t min = getTarget() * (100 - deviation) / 100;
Matthew Barth8a8aa442021-11-19 14:13:13 -0600224 std::optional<uint64_t> max = getTarget() * (100 + deviation) / 100;
Matthew Barth7c23a042021-01-26 16:21:45 -0600225
226 // Adjust the min/max range by applying the factor & offset
227 min = min * _factor + _offset;
Matthew Barth8a8aa442021-11-19 14:13:13 -0600228 max = max.value() * _factor + _offset;
229
230 if (_ignoreAboveMax)
231 {
232 max = std::nullopt;
233 }
Matthew Barth7c23a042021-01-26 16:21:45 -0600234
235 return std::make_pair(min, max);
236}
237
Matthew Barthfcb0dbc2021-02-10 14:23:39 -0600238void TachSensor::processState()
239{
Matt Spinler623635c2021-03-29 13:13:59 -0500240 // This function runs from inside trust::Manager::checkTrust(), which,
241 // for sensors using the count method, runs right before process()
242 // is called anyway inside Fan::countTimerExpired() so don't call
243 // it now if using that method.
244 if (_method == MethodMode::timebased)
245 {
246 _fan.process(*this);
247 }
Matthew Barthfcb0dbc2021-02-10 14:23:39 -0600248}
249
250void TachSensor::resetMethod()
251{
252 switch (_method)
253 {
254 case MethodMode::timebased:
255 if (timerRunning())
256 {
257 stopTimer();
258 }
259 break;
260 case MethodMode::count:
261 if (_functional)
262 {
263 _counter = 0;
264 }
265 else
266 {
267 _counter = _threshold;
268 }
269 break;
270 }
271}
272
Matthew Barthd199dcd2017-11-20 10:36:41 -0600273void TachSensor::setFunctional(bool functional)
274{
275 _functional = functional;
276 updateInventory(_functional);
Matt Spinlerf13b42e2020-10-26 15:29:49 -0500277
278 if (!_errorTimer)
279 {
280 return;
281 }
282
283 if (!_functional)
284 {
285 if (_fan.present())
286 {
287 _errorTimer->restartOnce(std::chrono::seconds(*_errorDelay));
288 }
289 }
290 else if (_errorTimer->isEnabled())
291 {
292 _errorTimer->setEnabled(false);
293 }
Matthew Barthd199dcd2017-11-20 10:36:41 -0600294}
Matt Spinlerebaae612017-04-27 14:21:48 -0500295
Brad Bishop771659f2017-07-30 19:52:21 -0400296void TachSensor::handleTargetChange(sdbusplus::message::message& msg)
Matt Spinlerebaae612017-04-27 14:21:48 -0500297{
Matthew Barth177fe982020-05-26 11:05:19 -0500298 readPropertyFromMessage(msg, _interface, FAN_TARGET_PROPERTY, _tachTarget);
Matt Spinlerebaae612017-04-27 14:21:48 -0500299
Matthew Barth177fe982020-05-26 11:05:19 -0500300 // Check all tach sensors on the fan against the target
Matt Spinlerebaae612017-04-27 14:21:48 -0500301 _fan.tachChanged();
Mike Capps7b34ee02022-05-04 14:16:12 -0400302
303 // record previous target value
304 if (_prevTargets.front() != _tachTarget)
305 {
306 _prevTargets.push_front(_tachTarget);
307
308 _prevTargets.pop_back();
309 }
Matt Spinlerebaae612017-04-27 14:21:48 -0500310}
311
Brad Bishop771659f2017-07-30 19:52:21 -0400312void TachSensor::handleTachChange(sdbusplus::message::message& msg)
Matt Spinlerebaae612017-04-27 14:21:48 -0500313{
Mike Cappsfdcd5db2021-05-20 12:47:10 -0400314 readPropertyFromMessage(msg, util::FAN_SENSOR_VALUE_INTF,
315 FAN_VALUE_PROPERTY, _tachInput);
Matt Spinlerebaae612017-04-27 14:21:48 -0500316
Matthew Barth177fe982020-05-26 11:05:19 -0500317 // Check just this sensor against the target
318 _fan.tachChanged(*this);
Mike Capps7b34ee02022-05-04 14:16:12 -0400319
320 // record previous tach value
321 _prevTachs.push_front(_tachInput);
322
323 _prevTachs.pop_back();
Matt Spinlerabf8da32017-04-27 14:08:45 -0500324}
325
Matthew Barth3800ae72018-02-19 16:08:04 -0600326void TachSensor::startTimer(TimerMode mode)
327{
Matthew Barth0d0e3552021-01-27 12:31:28 -0600328 using namespace std::chrono;
329
William A. Kennington III8fd879f2018-10-30 19:49:29 -0700330 if (!timerRunning() || mode != _timerMode)
Matthew Barth3800ae72018-02-19 16:08:04 -0600331 {
Matt Spinler1d7379e2021-03-01 16:16:17 -0600332 log<level::DEBUG>(
Matthew Barth0d0e3552021-01-27 12:31:28 -0600333 fmt::format("Start timer({}) on tach sensor {}. [delay = {}s]",
Ed Tanouscf8847e2022-02-01 16:26:38 -0800334 static_cast<int>(mode), _name,
Matthew Barth0d0e3552021-01-27 12:31:28 -0600335 duration_cast<seconds>(getDelay(mode)).count())
336 .c_str());
William A. Kennington III8fd879f2018-10-30 19:49:29 -0700337 _timer.restartOnce(getDelay(mode));
Matthew Barth3800ae72018-02-19 16:08:04 -0600338 _timerMode = mode;
339 }
Matthew Barth3800ae72018-02-19 16:08:04 -0600340}
Matt Spinlerabf8da32017-04-27 14:08:45 -0500341
Matthew Barth3800ae72018-02-19 16:08:04 -0600342std::chrono::microseconds TachSensor::getDelay(TimerMode mode)
Matt Spinlera9406a72017-04-27 14:29:24 -0500343{
344 using namespace std::chrono;
345
Matthew Barth177fe982020-05-26 11:05:19 -0500346 switch (mode)
Matthew Barth3800ae72018-02-19 16:08:04 -0600347 {
Matthew Barth177fe982020-05-26 11:05:19 -0500348 case TimerMode::nonfunc:
349 return duration_cast<microseconds>(seconds(_timeout));
350 case TimerMode::func:
351 return duration_cast<microseconds>(seconds(_funcDelay));
352 default:
353 // Log an internal error for undefined timer mode
354 log<level::ERR>("Undefined timer mode",
355 entry("TIMER_MODE=%u", mode));
356 elog<InternalFailure>();
357 return duration_cast<microseconds>(seconds(0));
Matthew Barth3800ae72018-02-19 16:08:04 -0600358 }
Matt Spinlera9406a72017-04-27 14:29:24 -0500359}
360
Jolie Ku69f2f482020-10-21 09:59:43 +0800361void TachSensor::setCounter(bool count)
362{
363 if (count)
364 {
365 if (_counter < _threshold)
366 {
367 ++_counter;
Matt Spinler623635c2021-03-29 13:13:59 -0500368 log<level::DEBUG>(
369 fmt::format(
370 "Incremented error counter on {} to {} (threshold {})",
371 _name, _counter, _threshold)
372 .c_str());
Jolie Ku69f2f482020-10-21 09:59:43 +0800373 }
374 }
375 else
376 {
377 if (_counter > 0)
378 {
379 --_counter;
Matt Spinler623635c2021-03-29 13:13:59 -0500380 log<level::DEBUG>(
381 fmt::format(
382 "Decremented error counter on {} to {} (threshold {})",
383 _name, _counter, _threshold)
384 .c_str());
Jolie Ku69f2f482020-10-21 09:59:43 +0800385 }
386 }
387}
388
Matt Spinlerfdfcc672021-06-01 14:51:06 -0600389void TachSensor::startCountTimer()
390{
391 if (_countTimer)
392 {
393 log<level::DEBUG>(
394 fmt::format("Starting count timer on sensor {}", _name).c_str());
395 _countTimer->restart(std::chrono::seconds(_countInterval));
396 }
397}
398
399void TachSensor::stopCountTimer()
400{
401 if (_countTimer && _countTimer->isEnabled())
402 {
403 log<level::DEBUG>(
404 fmt::format("Stopping count timer on tach sensor {}.", _name)
405 .c_str());
406 _countTimer->setEnabled(false);
407 }
408}
409
Matthew Barth4d982852017-11-17 09:37:13 -0600410void TachSensor::updateInventory(bool functional)
411{
Matthew Barth177fe982020-05-26 11:05:19 -0500412 auto objectMap =
413 util::getObjMap<bool>(_invName, util::OPERATIONAL_STATUS_INTF,
414 util::FUNCTIONAL_PROPERTY, functional);
Mike Capps8af8a622022-02-04 16:13:33 -0500415
416 auto response = util::SDBusPlus::callMethod(
417 _bus, util::INVENTORY_SVC, util::INVENTORY_PATH, util::INVENTORY_INTF,
418 "Notify", objectMap);
419
Matthew Barth4d982852017-11-17 09:37:13 -0600420 if (response.is_method_error())
421 {
422 log<level::ERR>("Error in notify update of tach sensor inventory");
423 }
424}
Matt Spinlera9406a72017-04-27 14:29:24 -0500425
Matthew Barth177fe982020-05-26 11:05:19 -0500426} // namespace monitor
427} // namespace fan
428} // namespace phosphor