blob: 36b20b1129321b5ebc4f146e2fce0477c7ed64d8 [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";
41
Matt Spinler2ea9a592022-04-08 14:36:22 -050042namespace fs = std::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 }
Patrick Williamsddb773b2021-10-06 11:24:49 -050065 catch (const std::exception& e)
Matt Spinlerebaae612017-04-27 14:21:48 -050066 {
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,
Matthew Barth8a8aa442021-11-19 14:13:13 -060075 bool ignoreAboveMax, size_t timeout,
76 const std::optional<size_t>& errorDelay,
Matt Spinlerfdfcc672021-06-01 14:51:06 -060077 size_t countInterval, const sdeventplus::Event& event) :
Matt Spinlerabf8da32017-04-27 14:08:45 -050078 _bus(bus),
Matt Spinler2ea9a592022-04-08 14:36:22 -050079 _fan(fan), _name(FAN_SENSOR_PATH + id),
80 _invName(fs::path(fan.getName()) / id), _hasTarget(hasTarget),
81 _funcDelay(funcDelay), _interface(interface), _factor(factor),
82 _offset(offset), _method(method), _threshold(threshold),
Matthew Barth8a8aa442021-11-19 14:13:13 -060083 _ignoreAboveMax(ignoreAboveMax), _timeout(timeout),
84 _timerMode(TimerMode::func),
Jolie Ku69f2f482020-10-21 09:59:43 +080085 _timer(event, std::bind(&Fan::updateState, &fan, std::ref(*this))),
Matt Spinlerfdfcc672021-06-01 14:51:06 -060086 _errorDelay(errorDelay), _countInterval(countInterval)
Matt Spinlerabf8da32017-04-27 14:08:45 -050087{
Mike Cappsce6820a2021-05-26 10:40:19 -040088 // Query functional state from inventory
89 // TODO - phosphor-fan-presence/issues/25
Mike Cappsce6820a2021-05-26 10:40:19 -040090
Mike Capps3edb0652021-09-01 18:30:21 -040091 _functional = true;
92
Mike Cappsce6820a2021-05-26 10:40:19 -040093 try
94 {
Mike Capps3edb0652021-09-01 18:30:21 -040095 // see if any object paths in the inventory have the
96 // OperationalStatus interface.
97 auto subtree = util::SDBusPlus::getSubTreeRaw(
98 _bus, util::INVENTORY_PATH, util::OPERATIONAL_STATUS_INTF, 0);
Mike Capps9ff48772021-07-19 14:49:43 -040099
Mike Capps3edb0652021-09-01 18:30:21 -0400100 // if the tach sensor's entry already exists, we for sure can
101 // read its functional state from the inventory
102 if (subtree.end() != subtree.find(util::INVENTORY_PATH + _invName))
Mike Cappsce6820a2021-05-26 10:40:19 -0400103 {
104 _functional = util::SDBusPlus::getProperty<bool>(
Mike Capps3edb0652021-09-01 18:30:21 -0400105 _bus, util::INVENTORY_PATH + _invName,
Mike Cappsce6820a2021-05-26 10:40:19 -0400106 util::OPERATIONAL_STATUS_INTF, util::FUNCTIONAL_PROPERTY);
107 }
Mike Cappsce6820a2021-05-26 10:40:19 -0400108 }
Patrick Williamsddb773b2021-10-06 11:24:49 -0500109 catch (const util::DBusError& e)
Mike Cappsce6820a2021-05-26 10:40:19 -0400110 {
111 log<level::DEBUG>(e.what());
Mike Cappsce6820a2021-05-26 10:40:19 -0400112 }
113
Mike Capps3edb0652021-09-01 18:30:21 -0400114 updateInventory(_functional);
115
Mike Cappsce6820a2021-05-26 10:40:19 -0400116 if (!_functional && MethodMode::count == _method)
117 {
118 // force continual nonfunctional state
119 _counter = _threshold;
120 }
Brad Bishopedaeb312017-07-30 19:38:20 -0400121
Matthew Barth0a9fe162018-01-26 12:53:15 -0600122 // Load in current Target and Input values when entering monitor mode
Matt Spinlerb0412d02020-10-12 16:53:52 -0500123#ifndef MONITOR_USE_JSON
Matthew Barth0a9fe162018-01-26 12:53:15 -0600124 if (mode != Mode::init)
Brad Bishopedaeb312017-07-30 19:38:20 -0400125 {
Matt Spinlerb0412d02020-10-12 16:53:52 -0500126#endif
Matthew Barth0a9fe162018-01-26 12:53:15 -0600127 try
128 {
Matt Spinler4283c5d2021-03-01 15:56:00 -0600129 updateTachAndTarget();
Matthew Barth0a9fe162018-01-26 12:53:15 -0600130 }
Matt Spinler4283c5d2021-03-01 15:56:00 -0600131 catch (const std::exception& e)
Matthew Barth0a9fe162018-01-26 12:53:15 -0600132 {
Matt Spinler4283c5d2021-03-01 15:56:00 -0600133 // Until the parent Fan's monitor-ready timer expires, the
134 // object can be functional with a missing D-bus sensor.
Matthew Barth0a9fe162018-01-26 12:53:15 -0600135 }
136
Mike Cappsfdcd5db2021-05-20 12:47:10 -0400137 auto match = getMatchString(util::FAN_SENSOR_VALUE_INTF);
Matthew Barth0a9fe162018-01-26 12:53:15 -0600138
Patrick Williams3ea9ec22021-11-19 12:21:08 -0600139 tachSignal = std::make_unique<sdbusplus::bus::match_t>(
Matthew Barth177fe982020-05-26 11:05:19 -0500140 _bus, match.c_str(),
141 [this](auto& msg) { this->handleTachChange(msg); });
Matthew Barth0a9fe162018-01-26 12:53:15 -0600142
143 if (_hasTarget)
144 {
Lei YU80f271b2018-01-31 15:24:46 +0800145 match = getMatchString(_interface);
Matthew Barth0a9fe162018-01-26 12:53:15 -0600146
Patrick Williams3ea9ec22021-11-19 12:21:08 -0600147 targetSignal = std::make_unique<sdbusplus::bus::match_t>(
Matthew Barth177fe982020-05-26 11:05:19 -0500148 _bus, match.c_str(),
149 [this](auto& msg) { this->handleTargetChange(msg); });
Matthew Barth0a9fe162018-01-26 12:53:15 -0600150 }
Matt Spinlerf13b42e2020-10-26 15:29:49 -0500151
152 if (_errorDelay)
153 {
154 _errorTimer = std::make_unique<
155 sdeventplus::utility::Timer<sdeventplus::ClockId::Monotonic>>(
156 event, std::bind(&Fan::sensorErrorTimerExpired, &fan,
157 std::ref(*this)));
158 }
Matt Spinlerfdfcc672021-06-01 14:51:06 -0600159
160 if (_method == MethodMode::count)
161 {
162 _countTimer = std::make_unique<
163 sdeventplus::utility::Timer<sdeventplus::ClockId::Monotonic>>(
164 event,
165 std::bind(&Fan::countTimerExpired, &fan, std::ref(*this)));
166 }
Matt Spinlerb0412d02020-10-12 16:53:52 -0500167#ifndef MONITOR_USE_JSON
Brad Bishopedaeb312017-07-30 19:38:20 -0400168 }
Matt Spinlerb0412d02020-10-12 16:53:52 -0500169#endif
Matt Spinlerebaae612017-04-27 14:21:48 -0500170}
171
Matt Spinler4283c5d2021-03-01 15:56:00 -0600172void TachSensor::updateTachAndTarget()
173{
174 _tachInput = util::SDBusPlus::getProperty<decltype(_tachInput)>(
Mike Cappsfdcd5db2021-05-20 12:47:10 -0400175 _bus, _name, util::FAN_SENSOR_VALUE_INTF, FAN_VALUE_PROPERTY);
Matt Spinler4283c5d2021-03-01 15:56:00 -0600176
177 if (_hasTarget)
178 {
179 readProperty(_interface, FAN_TARGET_PROPERTY, _name, _bus, _tachTarget);
180 }
181}
182
Matt Spinlerebaae612017-04-27 14:21:48 -0500183std::string TachSensor::getMatchString(const std::string& interface)
184{
Matthew Barth177fe982020-05-26 11:05:19 -0500185 return sdbusplus::bus::match::rules::propertiesChanged(_name, interface);
Matt Spinlerebaae612017-04-27 14:21:48 -0500186}
187
Matthew Barthf552ea52018-01-15 16:22:04 -0600188uint64_t TachSensor::getTarget() const
189{
190 if (!_hasTarget)
191 {
192 return _fan.findTargetSpeed();
193 }
194 return _tachTarget;
195}
196
Matthew Barth8a8aa442021-11-19 14:13:13 -0600197std::pair<uint64_t, std::optional<uint64_t>>
198 TachSensor::getRange(const size_t deviation) const
Matthew Barth7c23a042021-01-26 16:21:45 -0600199{
200 // Determine min/max range applying the deviation
201 uint64_t min = getTarget() * (100 - deviation) / 100;
Matthew Barth8a8aa442021-11-19 14:13:13 -0600202 std::optional<uint64_t> max = getTarget() * (100 + deviation) / 100;
Matthew Barth7c23a042021-01-26 16:21:45 -0600203
204 // Adjust the min/max range by applying the factor & offset
205 min = min * _factor + _offset;
Matthew Barth8a8aa442021-11-19 14:13:13 -0600206 max = max.value() * _factor + _offset;
207
208 if (_ignoreAboveMax)
209 {
210 max = std::nullopt;
211 }
Matthew Barth7c23a042021-01-26 16:21:45 -0600212
213 return std::make_pair(min, max);
214}
215
Matthew Barthfcb0dbc2021-02-10 14:23:39 -0600216void TachSensor::processState()
217{
Matt Spinler623635c2021-03-29 13:13:59 -0500218 // This function runs from inside trust::Manager::checkTrust(), which,
219 // for sensors using the count method, runs right before process()
220 // is called anyway inside Fan::countTimerExpired() so don't call
221 // it now if using that method.
222 if (_method == MethodMode::timebased)
223 {
224 _fan.process(*this);
225 }
Matthew Barthfcb0dbc2021-02-10 14:23:39 -0600226}
227
228void TachSensor::resetMethod()
229{
230 switch (_method)
231 {
232 case MethodMode::timebased:
233 if (timerRunning())
234 {
235 stopTimer();
236 }
237 break;
238 case MethodMode::count:
239 if (_functional)
240 {
241 _counter = 0;
242 }
243 else
244 {
245 _counter = _threshold;
246 }
247 break;
248 }
249}
250
Matthew Barthd199dcd2017-11-20 10:36:41 -0600251void TachSensor::setFunctional(bool functional)
252{
253 _functional = functional;
254 updateInventory(_functional);
Matt Spinlerf13b42e2020-10-26 15:29:49 -0500255
256 if (!_errorTimer)
257 {
258 return;
259 }
260
261 if (!_functional)
262 {
263 if (_fan.present())
264 {
265 _errorTimer->restartOnce(std::chrono::seconds(*_errorDelay));
266 }
267 }
268 else if (_errorTimer->isEnabled())
269 {
270 _errorTimer->setEnabled(false);
271 }
Matthew Barthd199dcd2017-11-20 10:36:41 -0600272}
Matt Spinlerebaae612017-04-27 14:21:48 -0500273
Brad Bishop771659f2017-07-30 19:52:21 -0400274void TachSensor::handleTargetChange(sdbusplus::message::message& msg)
Matt Spinlerebaae612017-04-27 14:21:48 -0500275{
Matthew Barth177fe982020-05-26 11:05:19 -0500276 readPropertyFromMessage(msg, _interface, FAN_TARGET_PROPERTY, _tachTarget);
Matt Spinlerebaae612017-04-27 14:21:48 -0500277
Matthew Barth177fe982020-05-26 11:05:19 -0500278 // Check all tach sensors on the fan against the target
Matt Spinlerebaae612017-04-27 14:21:48 -0500279 _fan.tachChanged();
280}
281
Brad Bishop771659f2017-07-30 19:52:21 -0400282void TachSensor::handleTachChange(sdbusplus::message::message& msg)
Matt Spinlerebaae612017-04-27 14:21:48 -0500283{
Mike Cappsfdcd5db2021-05-20 12:47:10 -0400284 readPropertyFromMessage(msg, util::FAN_SENSOR_VALUE_INTF,
285 FAN_VALUE_PROPERTY, _tachInput);
Matt Spinlerebaae612017-04-27 14:21:48 -0500286
Matthew Barth177fe982020-05-26 11:05:19 -0500287 // Check just this sensor against the target
288 _fan.tachChanged(*this);
Matt Spinlerabf8da32017-04-27 14:08:45 -0500289}
290
Matthew Barth3800ae72018-02-19 16:08:04 -0600291void TachSensor::startTimer(TimerMode mode)
292{
Matthew Barth0d0e3552021-01-27 12:31:28 -0600293 using namespace std::chrono;
294
William A. Kennington III8fd879f2018-10-30 19:49:29 -0700295 if (!timerRunning() || mode != _timerMode)
Matthew Barth3800ae72018-02-19 16:08:04 -0600296 {
Matt Spinler1d7379e2021-03-01 16:16:17 -0600297 log<level::DEBUG>(
Matthew Barth0d0e3552021-01-27 12:31:28 -0600298 fmt::format("Start timer({}) on tach sensor {}. [delay = {}s]",
Ed Tanouscf8847e2022-02-01 16:26:38 -0800299 static_cast<int>(mode), _name,
Matthew Barth0d0e3552021-01-27 12:31:28 -0600300 duration_cast<seconds>(getDelay(mode)).count())
301 .c_str());
William A. Kennington III8fd879f2018-10-30 19:49:29 -0700302 _timer.restartOnce(getDelay(mode));
Matthew Barth3800ae72018-02-19 16:08:04 -0600303 _timerMode = mode;
304 }
Matthew Barth3800ae72018-02-19 16:08:04 -0600305}
Matt Spinlerabf8da32017-04-27 14:08:45 -0500306
Matthew Barth3800ae72018-02-19 16:08:04 -0600307std::chrono::microseconds TachSensor::getDelay(TimerMode mode)
Matt Spinlera9406a72017-04-27 14:29:24 -0500308{
309 using namespace std::chrono;
310
Matthew Barth177fe982020-05-26 11:05:19 -0500311 switch (mode)
Matthew Barth3800ae72018-02-19 16:08:04 -0600312 {
Matthew Barth177fe982020-05-26 11:05:19 -0500313 case TimerMode::nonfunc:
314 return duration_cast<microseconds>(seconds(_timeout));
315 case TimerMode::func:
316 return duration_cast<microseconds>(seconds(_funcDelay));
317 default:
318 // Log an internal error for undefined timer mode
319 log<level::ERR>("Undefined timer mode",
320 entry("TIMER_MODE=%u", mode));
321 elog<InternalFailure>();
322 return duration_cast<microseconds>(seconds(0));
Matthew Barth3800ae72018-02-19 16:08:04 -0600323 }
Matt Spinlera9406a72017-04-27 14:29:24 -0500324}
325
Jolie Ku69f2f482020-10-21 09:59:43 +0800326void TachSensor::setCounter(bool count)
327{
328 if (count)
329 {
330 if (_counter < _threshold)
331 {
332 ++_counter;
Matt Spinler623635c2021-03-29 13:13:59 -0500333 log<level::DEBUG>(
334 fmt::format(
335 "Incremented error counter on {} to {} (threshold {})",
336 _name, _counter, _threshold)
337 .c_str());
Jolie Ku69f2f482020-10-21 09:59:43 +0800338 }
339 }
340 else
341 {
342 if (_counter > 0)
343 {
344 --_counter;
Matt Spinler623635c2021-03-29 13:13:59 -0500345 log<level::DEBUG>(
346 fmt::format(
347 "Decremented error counter on {} to {} (threshold {})",
348 _name, _counter, _threshold)
349 .c_str());
Jolie Ku69f2f482020-10-21 09:59:43 +0800350 }
351 }
352}
353
Matt Spinlerfdfcc672021-06-01 14:51:06 -0600354void TachSensor::startCountTimer()
355{
356 if (_countTimer)
357 {
358 log<level::DEBUG>(
359 fmt::format("Starting count timer on sensor {}", _name).c_str());
360 _countTimer->restart(std::chrono::seconds(_countInterval));
361 }
362}
363
364void TachSensor::stopCountTimer()
365{
366 if (_countTimer && _countTimer->isEnabled())
367 {
368 log<level::DEBUG>(
369 fmt::format("Stopping count timer on tach sensor {}.", _name)
370 .c_str());
371 _countTimer->setEnabled(false);
372 }
373}
374
Matthew Barth4d982852017-11-17 09:37:13 -0600375void TachSensor::updateInventory(bool functional)
376{
Matthew Barth177fe982020-05-26 11:05:19 -0500377 auto objectMap =
378 util::getObjMap<bool>(_invName, util::OPERATIONAL_STATUS_INTF,
379 util::FUNCTIONAL_PROPERTY, functional);
Mike Capps8af8a622022-02-04 16:13:33 -0500380
381 auto response = util::SDBusPlus::callMethod(
382 _bus, util::INVENTORY_SVC, util::INVENTORY_PATH, util::INVENTORY_INTF,
383 "Notify", objectMap);
384
Matthew Barth4d982852017-11-17 09:37:13 -0600385 if (response.is_method_error())
386 {
387 log<level::ERR>("Error in notify update of tach sensor inventory");
388 }
389}
Matt Spinlera9406a72017-04-27 14:29:24 -0500390
Matthew Barth177fe982020-05-26 11:05:19 -0500391} // namespace monitor
392} // namespace fan
393} // namespace phosphor