blob: 1d922314c2569680652600547187b588ca3d94c3 [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
Matthew Barth177fe982020-05-26 11:05:19 -050022#include <phosphor-logging/elog.hpp>
23#include <phosphor-logging/log.hpp>
24
Matt Spinler2ea9a592022-04-08 14:36:22 -050025#include <filesystem>
Patrick Williamsfbf47032023-07-17 12:27:34 -050026#include <format>
Matthew Barth177fe982020-05-26 11:05:19 -050027#include <functional>
Matthew Barth8a8aa442021-11-19 14:13:13 -060028#include <optional>
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_TARGET_PROPERTY = "Target";
39constexpr auto FAN_VALUE_PROPERTY = "Value";
Mike Capps7b34ee02022-05-04 14:16:12 -040040constexpr auto MAX_PREV_TACHS = 8;
41constexpr auto MAX_PREV_TARGETS = 8;
Matt Spinlerebaae612017-04-27 14:21:48 -050042
Matt Spinler2ea9a592022-04-08 14:36:22 -050043namespace fs = std::filesystem;
Matthew Barth177fe982020-05-26 11:05:19 -050044using InternalFailure =
45 sdbusplus::xyz::openbmc_project::Common::Error::InternalFailure;
Matt Spinlerebaae612017-04-27 14:21:48 -050046
47/**
48 * @brief Helper function to read a property
49 *
50 * @param[in] interface - the interface the property is on
51 * @param[in] propertName - the name of the property
52 * @param[in] path - the dbus path
Matt Spinlerebaae612017-04-27 14:21:48 -050053 * @param[in] bus - the dbus object
54 * @param[out] value - filled in with the property value
55 */
Matthew Barth177fe982020-05-26 11:05:19 -050056template <typename T>
57static void
58 readProperty(const std::string& interface, const std::string& propertyName,
Patrick Williamscb356d42022-07-22 19:26:53 -050059 const std::string& path, sdbusplus::bus_t& bus, T& value)
Matt Spinlerebaae612017-04-27 14:21:48 -050060{
Matt Spinlerebaae612017-04-27 14:21:48 -050061 try
62 {
Patrick Williams61b73292023-05-10 07:50:12 -050063 value = util::SDBusPlus::getProperty<T>(bus, path, interface,
64 propertyName);
Matt Spinlerebaae612017-04-27 14:21:48 -050065 }
Patrick Williamsddb773b2021-10-06 11:24:49 -050066 catch (const std::exception& e)
Matt Spinlerebaae612017-04-27 14:21:48 -050067 {
68 phosphor::logging::log<phosphor::logging::level::ERR>(e.what());
69 }
70}
Matt Spinlerabf8da32017-04-27 14:08:45 -050071
Patrick Williamscb356d42022-07-22 19:26:53 -050072TachSensor::TachSensor([[maybe_unused]] Mode mode, sdbusplus::bus_t& bus,
Mike Capps808d7fe2022-06-13 10:12:16 -040073 Fan& fan, const std::string& id, bool hasTarget,
74 size_t funcDelay, const std::string& interface,
Chau Ly27cc39f2022-09-20 08:16:56 +000075 const std::string& path, double factor, int64_t offset,
76 size_t method, size_t threshold, bool ignoreAboveMax,
77 size_t timeout, const std::optional<size_t>& errorDelay,
Matt Spinlerfdfcc672021-06-01 14:51:06 -060078 size_t countInterval, const sdeventplus::Event& event) :
Matt Spinlerabf8da32017-04-27 14:08:45 -050079 _bus(bus),
Matt Spinler2ea9a592022-04-08 14:36:22 -050080 _fan(fan), _name(FAN_SENSOR_PATH + id),
81 _invName(fs::path(fan.getName()) / id), _hasTarget(hasTarget),
Chau Ly27cc39f2022-09-20 08:16:56 +000082 _funcDelay(funcDelay), _interface(interface), _path(path), _factor(factor),
Matt Spinler2ea9a592022-04-08 14:36:22 -050083 _offset(offset), _method(method), _threshold(threshold),
Matthew Barth8a8aa442021-11-19 14:13:13 -060084 _ignoreAboveMax(ignoreAboveMax), _timeout(timeout),
85 _timerMode(TimerMode::func),
Jolie Ku69f2f482020-10-21 09:59:43 +080086 _timer(event, std::bind(&Fan::updateState, &fan, std::ref(*this))),
Matt Spinlerfdfcc672021-06-01 14:51:06 -060087 _errorDelay(errorDelay), _countInterval(countInterval)
Matt Spinlerabf8da32017-04-27 14:08:45 -050088{
Mike Capps7b34ee02022-05-04 14:16:12 -040089 _prevTachs.resize(MAX_PREV_TACHS);
90
91 if (_hasTarget)
92 {
93 _prevTargets.resize(MAX_PREV_TARGETS);
94 }
95
Mike Capps3edb0652021-09-01 18:30:21 -040096 updateInventory(_functional);
97
Matthew Barth0a9fe162018-01-26 12:53:15 -060098 // Load in current Target and Input values when entering monitor mode
Matt Spinlerb0412d02020-10-12 16:53:52 -050099#ifndef MONITOR_USE_JSON
Matthew Barth0a9fe162018-01-26 12:53:15 -0600100 if (mode != Mode::init)
Brad Bishopedaeb312017-07-30 19:38:20 -0400101 {
Matt Spinlerb0412d02020-10-12 16:53:52 -0500102#endif
Matthew Barth0a9fe162018-01-26 12:53:15 -0600103 try
104 {
Matt Spinler4283c5d2021-03-01 15:56:00 -0600105 updateTachAndTarget();
Matthew Barth0a9fe162018-01-26 12:53:15 -0600106 }
Matt Spinler4283c5d2021-03-01 15:56:00 -0600107 catch (const std::exception& e)
Matthew Barth0a9fe162018-01-26 12:53:15 -0600108 {
Matt Spinler4283c5d2021-03-01 15:56:00 -0600109 // Until the parent Fan's monitor-ready timer expires, the
110 // object can be functional with a missing D-bus sensor.
Matthew Barth0a9fe162018-01-26 12:53:15 -0600111 }
112
Chau Ly27cc39f2022-09-20 08:16:56 +0000113 auto match = getMatchString(std::nullopt, util::FAN_SENSOR_VALUE_INTF);
Matthew Barth0a9fe162018-01-26 12:53:15 -0600114
Patrick Williams3ea9ec22021-11-19 12:21:08 -0600115 tachSignal = std::make_unique<sdbusplus::bus::match_t>(
Matthew Barth177fe982020-05-26 11:05:19 -0500116 _bus, match.c_str(),
117 [this](auto& msg) { this->handleTachChange(msg); });
Matthew Barth0a9fe162018-01-26 12:53:15 -0600118
119 if (_hasTarget)
120 {
Chau Ly27cc39f2022-09-20 08:16:56 +0000121 if (_path.empty())
122 {
123 match = getMatchString(std::nullopt, _interface);
124 }
125 else
126 {
127 match = getMatchString(_path, _interface);
128 }
Patrick Williams3ea9ec22021-11-19 12:21:08 -0600129 targetSignal = std::make_unique<sdbusplus::bus::match_t>(
Matthew Barth177fe982020-05-26 11:05:19 -0500130 _bus, match.c_str(),
131 [this](auto& msg) { this->handleTargetChange(msg); });
Matthew Barth0a9fe162018-01-26 12:53:15 -0600132 }
Matt Spinlerf13b42e2020-10-26 15:29:49 -0500133
134 if (_errorDelay)
135 {
136 _errorTimer = std::make_unique<
137 sdeventplus::utility::Timer<sdeventplus::ClockId::Monotonic>>(
138 event, std::bind(&Fan::sensorErrorTimerExpired, &fan,
139 std::ref(*this)));
140 }
Matt Spinlerfdfcc672021-06-01 14:51:06 -0600141
142 if (_method == MethodMode::count)
143 {
144 _countTimer = std::make_unique<
145 sdeventplus::utility::Timer<sdeventplus::ClockId::Monotonic>>(
146 event,
147 std::bind(&Fan::countTimerExpired, &fan, std::ref(*this)));
148 }
Matt Spinlerb0412d02020-10-12 16:53:52 -0500149#ifndef MONITOR_USE_JSON
Brad Bishopedaeb312017-07-30 19:38:20 -0400150 }
Matt Spinlerb0412d02020-10-12 16:53:52 -0500151#endif
Matt Spinlerebaae612017-04-27 14:21:48 -0500152}
153
Matt Spinler4283c5d2021-03-01 15:56:00 -0600154void TachSensor::updateTachAndTarget()
155{
156 _tachInput = util::SDBusPlus::getProperty<decltype(_tachInput)>(
Mike Cappsfdcd5db2021-05-20 12:47:10 -0400157 _bus, _name, util::FAN_SENSOR_VALUE_INTF, FAN_VALUE_PROPERTY);
Matt Spinler4283c5d2021-03-01 15:56:00 -0600158
159 if (_hasTarget)
160 {
Chau Ly27cc39f2022-09-20 08:16:56 +0000161 if (_path.empty())
162 {
163 // Target path is optional
164 readProperty(_interface, FAN_TARGET_PROPERTY, _name, _bus,
165 _tachTarget);
166 }
167 else
168 {
169 readProperty(_interface, FAN_TARGET_PROPERTY, _path, _bus,
170 _tachTarget);
171 }
Mike Capps7b34ee02022-05-04 14:16:12 -0400172
173 // record previous target value
174 if (_prevTargets.front() != _tachTarget)
175 {
176 _prevTargets.push_front(_tachTarget);
177
178 _prevTargets.pop_back();
179 }
Matt Spinler4283c5d2021-03-01 15:56:00 -0600180 }
Mike Capps7b34ee02022-05-04 14:16:12 -0400181
182 // record previous tach value
183 _prevTachs.push_front(_tachInput);
184
185 _prevTachs.pop_back();
Matt Spinler4283c5d2021-03-01 15:56:00 -0600186}
187
Chau Ly27cc39f2022-09-20 08:16:56 +0000188std::string TachSensor::getMatchString(const std::optional<std::string> path,
189 const std::string& interface)
Matt Spinlerebaae612017-04-27 14:21:48 -0500190{
Chau Ly27cc39f2022-09-20 08:16:56 +0000191 if (path)
192 {
193 return sdbusplus::bus::match::rules::propertiesChanged(path.value(),
194 interface);
195 }
Matthew Barth177fe982020-05-26 11:05:19 -0500196 return sdbusplus::bus::match::rules::propertiesChanged(_name, interface);
Matt Spinlerebaae612017-04-27 14:21:48 -0500197}
198
Matthew Barthf552ea52018-01-15 16:22:04 -0600199uint64_t TachSensor::getTarget() const
200{
201 if (!_hasTarget)
202 {
203 return _fan.findTargetSpeed();
204 }
205 return _tachTarget;
206}
207
Matthew Barth8a8aa442021-11-19 14:13:13 -0600208std::pair<uint64_t, std::optional<uint64_t>>
Matt Spinlerf724c162023-05-10 11:14:37 -0500209 TachSensor::getRange(const size_t lowerDeviation,
210 const size_t upperDeviation) const
Matthew Barth7c23a042021-01-26 16:21:45 -0600211{
212 // Determine min/max range applying the deviation
Matt Spinlerf724c162023-05-10 11:14:37 -0500213 uint64_t min = getTarget() * (100 - lowerDeviation) / 100;
214 std::optional<uint64_t> max = getTarget() * (100 + upperDeviation) / 100;
Matthew Barth7c23a042021-01-26 16:21:45 -0600215
216 // Adjust the min/max range by applying the factor & offset
217 min = min * _factor + _offset;
Matthew Barth8a8aa442021-11-19 14:13:13 -0600218 max = max.value() * _factor + _offset;
219
220 if (_ignoreAboveMax)
221 {
222 max = std::nullopt;
223 }
Matthew Barth7c23a042021-01-26 16:21:45 -0600224
225 return std::make_pair(min, max);
226}
227
Matthew Barthfcb0dbc2021-02-10 14:23:39 -0600228void TachSensor::processState()
229{
Matt Spinler623635c2021-03-29 13:13:59 -0500230 // This function runs from inside trust::Manager::checkTrust(), which,
231 // for sensors using the count method, runs right before process()
232 // is called anyway inside Fan::countTimerExpired() so don't call
233 // it now if using that method.
234 if (_method == MethodMode::timebased)
235 {
236 _fan.process(*this);
237 }
Matthew Barthfcb0dbc2021-02-10 14:23:39 -0600238}
239
240void TachSensor::resetMethod()
241{
242 switch (_method)
243 {
244 case MethodMode::timebased:
245 if (timerRunning())
246 {
247 stopTimer();
248 }
249 break;
250 case MethodMode::count:
251 if (_functional)
252 {
253 _counter = 0;
254 }
255 else
256 {
257 _counter = _threshold;
258 }
259 break;
260 }
261}
262
Matt Spinlerae01b5f2022-07-06 16:49:04 -0500263void TachSensor::setFunctional(bool functional, bool skipErrorTimer)
Matthew Barthd199dcd2017-11-20 10:36:41 -0600264{
265 _functional = functional;
266 updateInventory(_functional);
Matt Spinlerf13b42e2020-10-26 15:29:49 -0500267
268 if (!_errorTimer)
269 {
270 return;
271 }
272
273 if (!_functional)
274 {
Matt Spinlerae01b5f2022-07-06 16:49:04 -0500275 if (_fan.present() && !skipErrorTimer)
Matt Spinlerf13b42e2020-10-26 15:29:49 -0500276 {
277 _errorTimer->restartOnce(std::chrono::seconds(*_errorDelay));
278 }
279 }
280 else if (_errorTimer->isEnabled())
281 {
282 _errorTimer->setEnabled(false);
283 }
Matthew Barthd199dcd2017-11-20 10:36:41 -0600284}
Matt Spinlerebaae612017-04-27 14:21:48 -0500285
Patrick Williamscb356d42022-07-22 19:26:53 -0500286void TachSensor::handleTargetChange(sdbusplus::message_t& msg)
Matt Spinlerebaae612017-04-27 14:21:48 -0500287{
Matthew Barth177fe982020-05-26 11:05:19 -0500288 readPropertyFromMessage(msg, _interface, FAN_TARGET_PROPERTY, _tachTarget);
Matt Spinlerebaae612017-04-27 14:21:48 -0500289
Matthew Barth177fe982020-05-26 11:05:19 -0500290 // Check all tach sensors on the fan against the target
Matt Spinlerebaae612017-04-27 14:21:48 -0500291 _fan.tachChanged();
Mike Capps7b34ee02022-05-04 14:16:12 -0400292
293 // record previous target value
294 if (_prevTargets.front() != _tachTarget)
295 {
296 _prevTargets.push_front(_tachTarget);
297
298 _prevTargets.pop_back();
299 }
Matt Spinlerebaae612017-04-27 14:21:48 -0500300}
301
Patrick Williamscb356d42022-07-22 19:26:53 -0500302void TachSensor::handleTachChange(sdbusplus::message_t& msg)
Matt Spinlerebaae612017-04-27 14:21:48 -0500303{
Mike Cappsfdcd5db2021-05-20 12:47:10 -0400304 readPropertyFromMessage(msg, util::FAN_SENSOR_VALUE_INTF,
305 FAN_VALUE_PROPERTY, _tachInput);
Matt Spinlerebaae612017-04-27 14:21:48 -0500306
Matthew Barth177fe982020-05-26 11:05:19 -0500307 // Check just this sensor against the target
308 _fan.tachChanged(*this);
Mike Capps7b34ee02022-05-04 14:16:12 -0400309
310 // record previous tach value
311 _prevTachs.push_front(_tachInput);
312
313 _prevTachs.pop_back();
Matt Spinlerabf8da32017-04-27 14:08:45 -0500314}
315
Matthew Barth3800ae72018-02-19 16:08:04 -0600316void TachSensor::startTimer(TimerMode mode)
317{
Matthew Barth0d0e3552021-01-27 12:31:28 -0600318 using namespace std::chrono;
319
William A. Kennington III8fd879f2018-10-30 19:49:29 -0700320 if (!timerRunning() || mode != _timerMode)
Matthew Barth3800ae72018-02-19 16:08:04 -0600321 {
Matt Spinler1d7379e2021-03-01 16:16:17 -0600322 log<level::DEBUG>(
Patrick Williamsfbf47032023-07-17 12:27:34 -0500323 std::format("Start timer({}) on tach sensor {}. [delay = {}s]",
Ed Tanouscf8847e2022-02-01 16:26:38 -0800324 static_cast<int>(mode), _name,
Matthew Barth0d0e3552021-01-27 12:31:28 -0600325 duration_cast<seconds>(getDelay(mode)).count())
326 .c_str());
William A. Kennington III8fd879f2018-10-30 19:49:29 -0700327 _timer.restartOnce(getDelay(mode));
Matthew Barth3800ae72018-02-19 16:08:04 -0600328 _timerMode = mode;
329 }
Matthew Barth3800ae72018-02-19 16:08:04 -0600330}
Matt Spinlerabf8da32017-04-27 14:08:45 -0500331
Matthew Barth3800ae72018-02-19 16:08:04 -0600332std::chrono::microseconds TachSensor::getDelay(TimerMode mode)
Matt Spinlera9406a72017-04-27 14:29:24 -0500333{
334 using namespace std::chrono;
335
Matthew Barth177fe982020-05-26 11:05:19 -0500336 switch (mode)
Matthew Barth3800ae72018-02-19 16:08:04 -0600337 {
Matthew Barth177fe982020-05-26 11:05:19 -0500338 case TimerMode::nonfunc:
339 return duration_cast<microseconds>(seconds(_timeout));
340 case TimerMode::func:
341 return duration_cast<microseconds>(seconds(_funcDelay));
342 default:
343 // Log an internal error for undefined timer mode
344 log<level::ERR>("Undefined timer mode",
345 entry("TIMER_MODE=%u", mode));
346 elog<InternalFailure>();
347 return duration_cast<microseconds>(seconds(0));
Matthew Barth3800ae72018-02-19 16:08:04 -0600348 }
Matt Spinlera9406a72017-04-27 14:29:24 -0500349}
350
Jolie Ku69f2f482020-10-21 09:59:43 +0800351void TachSensor::setCounter(bool count)
352{
353 if (count)
354 {
355 if (_counter < _threshold)
356 {
357 ++_counter;
Matt Spinler623635c2021-03-29 13:13:59 -0500358 log<level::DEBUG>(
Patrick Williamsfbf47032023-07-17 12:27:34 -0500359 std::format(
Matt Spinler623635c2021-03-29 13:13:59 -0500360 "Incremented error counter on {} to {} (threshold {})",
361 _name, _counter, _threshold)
362 .c_str());
Jolie Ku69f2f482020-10-21 09:59:43 +0800363 }
364 }
365 else
366 {
367 if (_counter > 0)
368 {
369 --_counter;
Matt Spinler623635c2021-03-29 13:13:59 -0500370 log<level::DEBUG>(
Patrick Williamsfbf47032023-07-17 12:27:34 -0500371 std::format(
Matt Spinler623635c2021-03-29 13:13:59 -0500372 "Decremented error counter on {} to {} (threshold {})",
373 _name, _counter, _threshold)
374 .c_str());
Jolie Ku69f2f482020-10-21 09:59:43 +0800375 }
376 }
377}
378
Matt Spinlerfdfcc672021-06-01 14:51:06 -0600379void TachSensor::startCountTimer()
380{
381 if (_countTimer)
382 {
383 log<level::DEBUG>(
Patrick Williamsfbf47032023-07-17 12:27:34 -0500384 std::format("Starting count timer on sensor {}", _name).c_str());
Matt Spinlerfdfcc672021-06-01 14:51:06 -0600385 _countTimer->restart(std::chrono::seconds(_countInterval));
386 }
387}
388
389void TachSensor::stopCountTimer()
390{
391 if (_countTimer && _countTimer->isEnabled())
392 {
393 log<level::DEBUG>(
Patrick Williamsfbf47032023-07-17 12:27:34 -0500394 std::format("Stopping count timer on tach sensor {}.", _name)
Matt Spinlerfdfcc672021-06-01 14:51:06 -0600395 .c_str());
396 _countTimer->setEnabled(false);
397 }
398}
399
Matthew Barth4d982852017-11-17 09:37:13 -0600400void TachSensor::updateInventory(bool functional)
401{
Matthew Barth177fe982020-05-26 11:05:19 -0500402 auto objectMap =
403 util::getObjMap<bool>(_invName, util::OPERATIONAL_STATUS_INTF,
404 util::FUNCTIONAL_PROPERTY, functional);
Mike Capps8af8a622022-02-04 16:13:33 -0500405
406 auto response = util::SDBusPlus::callMethod(
407 _bus, util::INVENTORY_SVC, util::INVENTORY_PATH, util::INVENTORY_INTF,
408 "Notify", objectMap);
409
Matthew Barth4d982852017-11-17 09:37:13 -0600410 if (response.is_method_error())
411 {
412 log<level::ERR>("Error in notify update of tach sensor inventory");
413 }
414}
Matt Spinlera9406a72017-04-27 14:29:24 -0500415
Matthew Barth177fe982020-05-26 11:05:19 -0500416} // namespace monitor
417} // namespace fan
418} // namespace phosphor