blob: 825faacdd0f3ae30e5afe59762c209d1cb7b64ba [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,
Patrick Williamscb356d42022-07-22 19:26:53 -050060 const std::string& path, sdbusplus::bus_t& 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
Patrick Williamscb356d42022-07-22 19:26:53 -050073TachSensor::TachSensor([[maybe_unused]] Mode mode, sdbusplus::bus_t& bus,
Mike Capps808d7fe2022-06-13 10:12:16 -040074 Fan& fan, const std::string& id, bool hasTarget,
75 size_t funcDelay, const std::string& interface,
76 double factor, int64_t offset, size_t method,
77 size_t threshold, bool ignoreAboveMax, size_t timeout,
Matthew Barth8a8aa442021-11-19 14:13:13 -060078 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 Capps7b34ee02022-05-04 14:16:12 -040090 _prevTachs.resize(MAX_PREV_TACHS);
91
92 if (_hasTarget)
93 {
94 _prevTargets.resize(MAX_PREV_TARGETS);
95 }
96
Mike Capps3edb0652021-09-01 18:30:21 -040097 updateInventory(_functional);
98
Matthew Barth0a9fe162018-01-26 12:53:15 -060099 // Load in current Target and Input values when entering monitor mode
Matt Spinlerb0412d02020-10-12 16:53:52 -0500100#ifndef MONITOR_USE_JSON
Matthew Barth0a9fe162018-01-26 12:53:15 -0600101 if (mode != Mode::init)
Brad Bishopedaeb312017-07-30 19:38:20 -0400102 {
Matt Spinlerb0412d02020-10-12 16:53:52 -0500103#endif
Matthew Barth0a9fe162018-01-26 12:53:15 -0600104 try
105 {
Matt Spinler4283c5d2021-03-01 15:56:00 -0600106 updateTachAndTarget();
Matthew Barth0a9fe162018-01-26 12:53:15 -0600107 }
Matt Spinler4283c5d2021-03-01 15:56:00 -0600108 catch (const std::exception& e)
Matthew Barth0a9fe162018-01-26 12:53:15 -0600109 {
Matt Spinler4283c5d2021-03-01 15:56:00 -0600110 // Until the parent Fan's monitor-ready timer expires, the
111 // object can be functional with a missing D-bus sensor.
Matthew Barth0a9fe162018-01-26 12:53:15 -0600112 }
113
Mike Cappsfdcd5db2021-05-20 12:47:10 -0400114 auto match = getMatchString(util::FAN_SENSOR_VALUE_INTF);
Matthew Barth0a9fe162018-01-26 12:53:15 -0600115
Patrick Williams3ea9ec22021-11-19 12:21:08 -0600116 tachSignal = std::make_unique<sdbusplus::bus::match_t>(
Matthew Barth177fe982020-05-26 11:05:19 -0500117 _bus, match.c_str(),
118 [this](auto& msg) { this->handleTachChange(msg); });
Matthew Barth0a9fe162018-01-26 12:53:15 -0600119
120 if (_hasTarget)
121 {
Lei YU80f271b2018-01-31 15:24:46 +0800122 match = getMatchString(_interface);
Matthew Barth0a9fe162018-01-26 12:53:15 -0600123
Patrick Williams3ea9ec22021-11-19 12:21:08 -0600124 targetSignal = std::make_unique<sdbusplus::bus::match_t>(
Matthew Barth177fe982020-05-26 11:05:19 -0500125 _bus, match.c_str(),
126 [this](auto& msg) { this->handleTargetChange(msg); });
Matthew Barth0a9fe162018-01-26 12:53:15 -0600127 }
Matt Spinlerf13b42e2020-10-26 15:29:49 -0500128
129 if (_errorDelay)
130 {
131 _errorTimer = std::make_unique<
132 sdeventplus::utility::Timer<sdeventplus::ClockId::Monotonic>>(
133 event, std::bind(&Fan::sensorErrorTimerExpired, &fan,
134 std::ref(*this)));
135 }
Matt Spinlerfdfcc672021-06-01 14:51:06 -0600136
137 if (_method == MethodMode::count)
138 {
139 _countTimer = std::make_unique<
140 sdeventplus::utility::Timer<sdeventplus::ClockId::Monotonic>>(
141 event,
142 std::bind(&Fan::countTimerExpired, &fan, std::ref(*this)));
143 }
Matt Spinlerb0412d02020-10-12 16:53:52 -0500144#ifndef MONITOR_USE_JSON
Brad Bishopedaeb312017-07-30 19:38:20 -0400145 }
Matt Spinlerb0412d02020-10-12 16:53:52 -0500146#endif
Matt Spinlerebaae612017-04-27 14:21:48 -0500147}
148
Matt Spinler4283c5d2021-03-01 15:56:00 -0600149void TachSensor::updateTachAndTarget()
150{
151 _tachInput = util::SDBusPlus::getProperty<decltype(_tachInput)>(
Mike Cappsfdcd5db2021-05-20 12:47:10 -0400152 _bus, _name, util::FAN_SENSOR_VALUE_INTF, FAN_VALUE_PROPERTY);
Matt Spinler4283c5d2021-03-01 15:56:00 -0600153
154 if (_hasTarget)
155 {
156 readProperty(_interface, FAN_TARGET_PROPERTY, _name, _bus, _tachTarget);
Mike Capps7b34ee02022-05-04 14:16:12 -0400157
158 // record previous target value
159 if (_prevTargets.front() != _tachTarget)
160 {
161 _prevTargets.push_front(_tachTarget);
162
163 _prevTargets.pop_back();
164 }
Matt Spinler4283c5d2021-03-01 15:56:00 -0600165 }
Mike Capps7b34ee02022-05-04 14:16:12 -0400166
167 // record previous tach value
168 _prevTachs.push_front(_tachInput);
169
170 _prevTachs.pop_back();
Matt Spinler4283c5d2021-03-01 15:56:00 -0600171}
172
Matt Spinlerebaae612017-04-27 14:21:48 -0500173std::string TachSensor::getMatchString(const std::string& interface)
174{
Matthew Barth177fe982020-05-26 11:05:19 -0500175 return sdbusplus::bus::match::rules::propertiesChanged(_name, interface);
Matt Spinlerebaae612017-04-27 14:21:48 -0500176}
177
Matthew Barthf552ea52018-01-15 16:22:04 -0600178uint64_t TachSensor::getTarget() const
179{
180 if (!_hasTarget)
181 {
182 return _fan.findTargetSpeed();
183 }
184 return _tachTarget;
185}
186
Matthew Barth8a8aa442021-11-19 14:13:13 -0600187std::pair<uint64_t, std::optional<uint64_t>>
188 TachSensor::getRange(const size_t deviation) const
Matthew Barth7c23a042021-01-26 16:21:45 -0600189{
190 // Determine min/max range applying the deviation
191 uint64_t min = getTarget() * (100 - deviation) / 100;
Matthew Barth8a8aa442021-11-19 14:13:13 -0600192 std::optional<uint64_t> max = getTarget() * (100 + deviation) / 100;
Matthew Barth7c23a042021-01-26 16:21:45 -0600193
194 // Adjust the min/max range by applying the factor & offset
195 min = min * _factor + _offset;
Matthew Barth8a8aa442021-11-19 14:13:13 -0600196 max = max.value() * _factor + _offset;
197
198 if (_ignoreAboveMax)
199 {
200 max = std::nullopt;
201 }
Matthew Barth7c23a042021-01-26 16:21:45 -0600202
203 return std::make_pair(min, max);
204}
205
Matthew Barthfcb0dbc2021-02-10 14:23:39 -0600206void TachSensor::processState()
207{
Matt Spinler623635c2021-03-29 13:13:59 -0500208 // This function runs from inside trust::Manager::checkTrust(), which,
209 // for sensors using the count method, runs right before process()
210 // is called anyway inside Fan::countTimerExpired() so don't call
211 // it now if using that method.
212 if (_method == MethodMode::timebased)
213 {
214 _fan.process(*this);
215 }
Matthew Barthfcb0dbc2021-02-10 14:23:39 -0600216}
217
218void TachSensor::resetMethod()
219{
220 switch (_method)
221 {
222 case MethodMode::timebased:
223 if (timerRunning())
224 {
225 stopTimer();
226 }
227 break;
228 case MethodMode::count:
229 if (_functional)
230 {
231 _counter = 0;
232 }
233 else
234 {
235 _counter = _threshold;
236 }
237 break;
238 }
239}
240
Matt Spinlerae01b5f2022-07-06 16:49:04 -0500241void TachSensor::setFunctional(bool functional, bool skipErrorTimer)
Matthew Barthd199dcd2017-11-20 10:36:41 -0600242{
243 _functional = functional;
244 updateInventory(_functional);
Matt Spinlerf13b42e2020-10-26 15:29:49 -0500245
246 if (!_errorTimer)
247 {
248 return;
249 }
250
251 if (!_functional)
252 {
Matt Spinlerae01b5f2022-07-06 16:49:04 -0500253 if (_fan.present() && !skipErrorTimer)
Matt Spinlerf13b42e2020-10-26 15:29:49 -0500254 {
255 _errorTimer->restartOnce(std::chrono::seconds(*_errorDelay));
256 }
257 }
258 else if (_errorTimer->isEnabled())
259 {
260 _errorTimer->setEnabled(false);
261 }
Matthew Barthd199dcd2017-11-20 10:36:41 -0600262}
Matt Spinlerebaae612017-04-27 14:21:48 -0500263
Patrick Williamscb356d42022-07-22 19:26:53 -0500264void TachSensor::handleTargetChange(sdbusplus::message_t& msg)
Matt Spinlerebaae612017-04-27 14:21:48 -0500265{
Matthew Barth177fe982020-05-26 11:05:19 -0500266 readPropertyFromMessage(msg, _interface, FAN_TARGET_PROPERTY, _tachTarget);
Matt Spinlerebaae612017-04-27 14:21:48 -0500267
Matthew Barth177fe982020-05-26 11:05:19 -0500268 // Check all tach sensors on the fan against the target
Matt Spinlerebaae612017-04-27 14:21:48 -0500269 _fan.tachChanged();
Mike Capps7b34ee02022-05-04 14:16:12 -0400270
271 // record previous target value
272 if (_prevTargets.front() != _tachTarget)
273 {
274 _prevTargets.push_front(_tachTarget);
275
276 _prevTargets.pop_back();
277 }
Matt Spinlerebaae612017-04-27 14:21:48 -0500278}
279
Patrick Williamscb356d42022-07-22 19:26:53 -0500280void TachSensor::handleTachChange(sdbusplus::message_t& msg)
Matt Spinlerebaae612017-04-27 14:21:48 -0500281{
Mike Cappsfdcd5db2021-05-20 12:47:10 -0400282 readPropertyFromMessage(msg, util::FAN_SENSOR_VALUE_INTF,
283 FAN_VALUE_PROPERTY, _tachInput);
Matt Spinlerebaae612017-04-27 14:21:48 -0500284
Matthew Barth177fe982020-05-26 11:05:19 -0500285 // Check just this sensor against the target
286 _fan.tachChanged(*this);
Mike Capps7b34ee02022-05-04 14:16:12 -0400287
288 // record previous tach value
289 _prevTachs.push_front(_tachInput);
290
291 _prevTachs.pop_back();
Matt Spinlerabf8da32017-04-27 14:08:45 -0500292}
293
Matthew Barth3800ae72018-02-19 16:08:04 -0600294void TachSensor::startTimer(TimerMode mode)
295{
Matthew Barth0d0e3552021-01-27 12:31:28 -0600296 using namespace std::chrono;
297
William A. Kennington III8fd879f2018-10-30 19:49:29 -0700298 if (!timerRunning() || mode != _timerMode)
Matthew Barth3800ae72018-02-19 16:08:04 -0600299 {
Matt Spinler1d7379e2021-03-01 16:16:17 -0600300 log<level::DEBUG>(
Matthew Barth0d0e3552021-01-27 12:31:28 -0600301 fmt::format("Start timer({}) on tach sensor {}. [delay = {}s]",
Ed Tanouscf8847e2022-02-01 16:26:38 -0800302 static_cast<int>(mode), _name,
Matthew Barth0d0e3552021-01-27 12:31:28 -0600303 duration_cast<seconds>(getDelay(mode)).count())
304 .c_str());
William A. Kennington III8fd879f2018-10-30 19:49:29 -0700305 _timer.restartOnce(getDelay(mode));
Matthew Barth3800ae72018-02-19 16:08:04 -0600306 _timerMode = mode;
307 }
Matthew Barth3800ae72018-02-19 16:08:04 -0600308}
Matt Spinlerabf8da32017-04-27 14:08:45 -0500309
Matthew Barth3800ae72018-02-19 16:08:04 -0600310std::chrono::microseconds TachSensor::getDelay(TimerMode mode)
Matt Spinlera9406a72017-04-27 14:29:24 -0500311{
312 using namespace std::chrono;
313
Matthew Barth177fe982020-05-26 11:05:19 -0500314 switch (mode)
Matthew Barth3800ae72018-02-19 16:08:04 -0600315 {
Matthew Barth177fe982020-05-26 11:05:19 -0500316 case TimerMode::nonfunc:
317 return duration_cast<microseconds>(seconds(_timeout));
318 case TimerMode::func:
319 return duration_cast<microseconds>(seconds(_funcDelay));
320 default:
321 // Log an internal error for undefined timer mode
322 log<level::ERR>("Undefined timer mode",
323 entry("TIMER_MODE=%u", mode));
324 elog<InternalFailure>();
325 return duration_cast<microseconds>(seconds(0));
Matthew Barth3800ae72018-02-19 16:08:04 -0600326 }
Matt Spinlera9406a72017-04-27 14:29:24 -0500327}
328
Jolie Ku69f2f482020-10-21 09:59:43 +0800329void TachSensor::setCounter(bool count)
330{
331 if (count)
332 {
333 if (_counter < _threshold)
334 {
335 ++_counter;
Matt Spinler623635c2021-03-29 13:13:59 -0500336 log<level::DEBUG>(
337 fmt::format(
338 "Incremented error counter on {} to {} (threshold {})",
339 _name, _counter, _threshold)
340 .c_str());
Jolie Ku69f2f482020-10-21 09:59:43 +0800341 }
342 }
343 else
344 {
345 if (_counter > 0)
346 {
347 --_counter;
Matt Spinler623635c2021-03-29 13:13:59 -0500348 log<level::DEBUG>(
349 fmt::format(
350 "Decremented error counter on {} to {} (threshold {})",
351 _name, _counter, _threshold)
352 .c_str());
Jolie Ku69f2f482020-10-21 09:59:43 +0800353 }
354 }
355}
356
Matt Spinlerfdfcc672021-06-01 14:51:06 -0600357void TachSensor::startCountTimer()
358{
359 if (_countTimer)
360 {
361 log<level::DEBUG>(
362 fmt::format("Starting count timer on sensor {}", _name).c_str());
363 _countTimer->restart(std::chrono::seconds(_countInterval));
364 }
365}
366
367void TachSensor::stopCountTimer()
368{
369 if (_countTimer && _countTimer->isEnabled())
370 {
371 log<level::DEBUG>(
372 fmt::format("Stopping count timer on tach sensor {}.", _name)
373 .c_str());
374 _countTimer->setEnabled(false);
375 }
376}
377
Matthew Barth4d982852017-11-17 09:37:13 -0600378void TachSensor::updateInventory(bool functional)
379{
Matthew Barth177fe982020-05-26 11:05:19 -0500380 auto objectMap =
381 util::getObjMap<bool>(_invName, util::OPERATIONAL_STATUS_INTF,
382 util::FUNCTIONAL_PROPERTY, functional);
Mike Capps8af8a622022-02-04 16:13:33 -0500383
384 auto response = util::SDBusPlus::callMethod(
385 _bus, util::INVENTORY_SVC, util::INVENTORY_PATH, util::INVENTORY_INTF,
386 "Notify", objectMap);
387
Matthew Barth4d982852017-11-17 09:37:13 -0600388 if (response.is_method_error())
389 {
390 log<level::ERR>("Error in notify update of tach sensor inventory");
391 }
392}
Matt Spinlera9406a72017-04-27 14:29:24 -0500393
Matthew Barth177fe982020-05-26 11:05:19 -0500394} // namespace monitor
395} // namespace fan
396} // namespace phosphor