blob: 91b4091118749abf1281554862794f6a2d529f8c [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_TARGET_PROPERTY = "Target";
39constexpr auto FAN_VALUE_PROPERTY = "Value";
40
Matthew Barth4d982852017-11-17 09:37:13 -060041using namespace std::experimental::filesystem;
Matthew Barth177fe982020-05-26 11:05:19 -050042using InternalFailure =
43 sdbusplus::xyz::openbmc_project::Common::Error::InternalFailure;
Matt Spinlerebaae612017-04-27 14:21:48 -050044
45/**
46 * @brief Helper function to read a property
47 *
48 * @param[in] interface - the interface the property is on
49 * @param[in] propertName - the name of the property
50 * @param[in] path - the dbus path
Matt Spinlerebaae612017-04-27 14:21:48 -050051 * @param[in] bus - the dbus object
52 * @param[out] value - filled in with the property value
53 */
Matthew Barth177fe982020-05-26 11:05:19 -050054template <typename T>
55static void
56 readProperty(const std::string& interface, const std::string& propertyName,
57 const std::string& path, sdbusplus::bus::bus& bus, T& value)
Matt Spinlerebaae612017-04-27 14:21:48 -050058{
Matt Spinlerebaae612017-04-27 14:21:48 -050059 try
60 {
Matthew Barth177fe982020-05-26 11:05:19 -050061 value =
62 util::SDBusPlus::getProperty<T>(bus, path, interface, propertyName);
Matt Spinlerebaae612017-04-27 14:21:48 -050063 }
Patrick Williamsddb773b2021-10-06 11:24:49 -050064 catch (const std::exception& e)
Matt Spinlerebaae612017-04-27 14:21:48 -050065 {
66 phosphor::logging::log<phosphor::logging::level::ERR>(e.what());
67 }
68}
Matt Spinlerabf8da32017-04-27 14:08:45 -050069
Matthew Barth177fe982020-05-26 11:05:19 -050070TachSensor::TachSensor(Mode mode, sdbusplus::bus::bus& bus, Fan& fan,
71 const std::string& id, bool hasTarget, size_t funcDelay,
72 const std::string& interface, double factor,
Jolie Ku69f2f482020-10-21 09:59:43 +080073 int64_t offset, size_t method, size_t threshold,
74 size_t timeout, const std::optional<size_t>& errorDelay,
Matt Spinlerfdfcc672021-06-01 14:51:06 -060075 size_t countInterval, const sdeventplus::Event& event) :
Matt Spinlerabf8da32017-04-27 14:08:45 -050076 _bus(bus),
Matthew Barth177fe982020-05-26 11:05:19 -050077 _fan(fan), _name(FAN_SENSOR_PATH + id), _invName(path(fan.getName()) / id),
78 _hasTarget(hasTarget), _funcDelay(funcDelay), _interface(interface),
Jolie Ku69f2f482020-10-21 09:59:43 +080079 _factor(factor), _offset(offset), _method(method), _threshold(threshold),
80 _timeout(timeout), _timerMode(TimerMode::func),
81 _timer(event, std::bind(&Fan::updateState, &fan, std::ref(*this))),
Matt Spinlerfdfcc672021-06-01 14:51:06 -060082 _errorDelay(errorDelay), _countInterval(countInterval)
Matt Spinlerabf8da32017-04-27 14:08:45 -050083{
Mike Cappsce6820a2021-05-26 10:40:19 -040084 // Query functional state from inventory
85 // TODO - phosphor-fan-presence/issues/25
Mike Cappsce6820a2021-05-26 10:40:19 -040086
Mike Capps3edb0652021-09-01 18:30:21 -040087 _functional = true;
88
Mike Cappsce6820a2021-05-26 10:40:19 -040089 try
90 {
Mike Capps3edb0652021-09-01 18:30:21 -040091 // see if any object paths in the inventory have the
92 // OperationalStatus interface.
93 auto subtree = util::SDBusPlus::getSubTreeRaw(
94 _bus, util::INVENTORY_PATH, util::OPERATIONAL_STATUS_INTF, 0);
Mike Capps9ff48772021-07-19 14:49:43 -040095
Mike Capps3edb0652021-09-01 18:30:21 -040096 // if the tach sensor's entry already exists, we for sure can
97 // read its functional state from the inventory
98 if (subtree.end() != subtree.find(util::INVENTORY_PATH + _invName))
Mike Cappsce6820a2021-05-26 10:40:19 -040099 {
100 _functional = util::SDBusPlus::getProperty<bool>(
Mike Capps3edb0652021-09-01 18:30:21 -0400101 _bus, util::INVENTORY_PATH + _invName,
Mike Cappsce6820a2021-05-26 10:40:19 -0400102 util::OPERATIONAL_STATUS_INTF, util::FUNCTIONAL_PROPERTY);
103 }
Mike Cappsce6820a2021-05-26 10:40:19 -0400104 }
Patrick Williamsddb773b2021-10-06 11:24:49 -0500105 catch (const util::DBusError& e)
Mike Cappsce6820a2021-05-26 10:40:19 -0400106 {
107 log<level::DEBUG>(e.what());
Mike Cappsce6820a2021-05-26 10:40:19 -0400108 }
109
Mike Capps3edb0652021-09-01 18:30:21 -0400110 updateInventory(_functional);
111
Mike Cappsce6820a2021-05-26 10:40:19 -0400112 if (!_functional && MethodMode::count == _method)
113 {
114 // force continual nonfunctional state
115 _counter = _threshold;
116 }
Brad Bishopedaeb312017-07-30 19:38:20 -0400117
Matthew Barth0a9fe162018-01-26 12:53:15 -0600118 // Load in current Target and Input values when entering monitor mode
Matt Spinlerb0412d02020-10-12 16:53:52 -0500119#ifndef MONITOR_USE_JSON
Matthew Barth0a9fe162018-01-26 12:53:15 -0600120 if (mode != Mode::init)
Brad Bishopedaeb312017-07-30 19:38:20 -0400121 {
Matt Spinlerb0412d02020-10-12 16:53:52 -0500122#endif
Matthew Barth0a9fe162018-01-26 12:53:15 -0600123 try
124 {
Matt Spinler4283c5d2021-03-01 15:56:00 -0600125 updateTachAndTarget();
Matthew Barth0a9fe162018-01-26 12:53:15 -0600126 }
Matt Spinler4283c5d2021-03-01 15:56:00 -0600127 catch (const std::exception& e)
Matthew Barth0a9fe162018-01-26 12:53:15 -0600128 {
Matt Spinler4283c5d2021-03-01 15:56:00 -0600129 // Until the parent Fan's monitor-ready timer expires, the
130 // object can be functional with a missing D-bus sensor.
Matthew Barth0a9fe162018-01-26 12:53:15 -0600131 }
132
Mike Cappsfdcd5db2021-05-20 12:47:10 -0400133 auto match = getMatchString(util::FAN_SENSOR_VALUE_INTF);
Matthew Barth0a9fe162018-01-26 12:53:15 -0600134
Patrick Williams3ea9ec22021-11-19 12:21:08 -0600135 tachSignal = std::make_unique<sdbusplus::bus::match_t>(
Matthew Barth177fe982020-05-26 11:05:19 -0500136 _bus, match.c_str(),
137 [this](auto& msg) { this->handleTachChange(msg); });
Matthew Barth0a9fe162018-01-26 12:53:15 -0600138
139 if (_hasTarget)
140 {
Lei YU80f271b2018-01-31 15:24:46 +0800141 match = getMatchString(_interface);
Matthew Barth0a9fe162018-01-26 12:53:15 -0600142
Patrick Williams3ea9ec22021-11-19 12:21:08 -0600143 targetSignal = std::make_unique<sdbusplus::bus::match_t>(
Matthew Barth177fe982020-05-26 11:05:19 -0500144 _bus, match.c_str(),
145 [this](auto& msg) { this->handleTargetChange(msg); });
Matthew Barth0a9fe162018-01-26 12:53:15 -0600146 }
Matt Spinlerf13b42e2020-10-26 15:29:49 -0500147
148 if (_errorDelay)
149 {
150 _errorTimer = std::make_unique<
151 sdeventplus::utility::Timer<sdeventplus::ClockId::Monotonic>>(
152 event, std::bind(&Fan::sensorErrorTimerExpired, &fan,
153 std::ref(*this)));
154 }
Matt Spinlerfdfcc672021-06-01 14:51:06 -0600155
156 if (_method == MethodMode::count)
157 {
158 _countTimer = std::make_unique<
159 sdeventplus::utility::Timer<sdeventplus::ClockId::Monotonic>>(
160 event,
161 std::bind(&Fan::countTimerExpired, &fan, std::ref(*this)));
162 }
Matt Spinlerb0412d02020-10-12 16:53:52 -0500163#ifndef MONITOR_USE_JSON
Brad Bishopedaeb312017-07-30 19:38:20 -0400164 }
Matt Spinlerb0412d02020-10-12 16:53:52 -0500165#endif
Matt Spinlerebaae612017-04-27 14:21:48 -0500166}
167
Matt Spinler4283c5d2021-03-01 15:56:00 -0600168void TachSensor::updateTachAndTarget()
169{
170 _tachInput = util::SDBusPlus::getProperty<decltype(_tachInput)>(
Mike Cappsfdcd5db2021-05-20 12:47:10 -0400171 _bus, _name, util::FAN_SENSOR_VALUE_INTF, FAN_VALUE_PROPERTY);
Matt Spinler4283c5d2021-03-01 15:56:00 -0600172
173 if (_hasTarget)
174 {
175 readProperty(_interface, FAN_TARGET_PROPERTY, _name, _bus, _tachTarget);
176 }
177}
178
Matt Spinlerebaae612017-04-27 14:21:48 -0500179std::string TachSensor::getMatchString(const std::string& interface)
180{
Matthew Barth177fe982020-05-26 11:05:19 -0500181 return sdbusplus::bus::match::rules::propertiesChanged(_name, interface);
Matt Spinlerebaae612017-04-27 14:21:48 -0500182}
183
Matthew Barthf552ea52018-01-15 16:22:04 -0600184uint64_t TachSensor::getTarget() const
185{
186 if (!_hasTarget)
187 {
188 return _fan.findTargetSpeed();
189 }
190 return _tachTarget;
191}
192
Matthew Barth7c23a042021-01-26 16:21:45 -0600193std::pair<uint64_t, uint64_t> TachSensor::getRange(const size_t deviation) const
194{
195 // Determine min/max range applying the deviation
196 uint64_t min = getTarget() * (100 - deviation) / 100;
197 uint64_t max = getTarget() * (100 + deviation) / 100;
198
199 // Adjust the min/max range by applying the factor & offset
200 min = min * _factor + _offset;
201 max = max * _factor + _offset;
202
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
Matthew Barthd199dcd2017-11-20 10:36:41 -0600241void TachSensor::setFunctional(bool functional)
242{
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 {
253 if (_fan.present())
254 {
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
Brad Bishop771659f2017-07-30 19:52:21 -0400264void TachSensor::handleTargetChange(sdbusplus::message::message& 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();
270}
271
Brad Bishop771659f2017-07-30 19:52:21 -0400272void TachSensor::handleTachChange(sdbusplus::message::message& msg)
Matt Spinlerebaae612017-04-27 14:21:48 -0500273{
Mike Cappsfdcd5db2021-05-20 12:47:10 -0400274 readPropertyFromMessage(msg, util::FAN_SENSOR_VALUE_INTF,
275 FAN_VALUE_PROPERTY, _tachInput);
Matt Spinlerebaae612017-04-27 14:21:48 -0500276
Matthew Barth177fe982020-05-26 11:05:19 -0500277 // Check just this sensor against the target
278 _fan.tachChanged(*this);
Matt Spinlerabf8da32017-04-27 14:08:45 -0500279}
280
Matthew Barth3800ae72018-02-19 16:08:04 -0600281void TachSensor::startTimer(TimerMode mode)
282{
Matthew Barth0d0e3552021-01-27 12:31:28 -0600283 using namespace std::chrono;
284
William A. Kennington III8fd879f2018-10-30 19:49:29 -0700285 if (!timerRunning() || mode != _timerMode)
Matthew Barth3800ae72018-02-19 16:08:04 -0600286 {
Matt Spinler1d7379e2021-03-01 16:16:17 -0600287 log<level::DEBUG>(
Matthew Barth0d0e3552021-01-27 12:31:28 -0600288 fmt::format("Start timer({}) on tach sensor {}. [delay = {}s]",
289 mode, _name,
290 duration_cast<seconds>(getDelay(mode)).count())
291 .c_str());
William A. Kennington III8fd879f2018-10-30 19:49:29 -0700292 _timer.restartOnce(getDelay(mode));
Matthew Barth3800ae72018-02-19 16:08:04 -0600293 _timerMode = mode;
294 }
Matthew Barth3800ae72018-02-19 16:08:04 -0600295}
Matt Spinlerabf8da32017-04-27 14:08:45 -0500296
Matthew Barth3800ae72018-02-19 16:08:04 -0600297std::chrono::microseconds TachSensor::getDelay(TimerMode mode)
Matt Spinlera9406a72017-04-27 14:29:24 -0500298{
299 using namespace std::chrono;
300
Matthew Barth177fe982020-05-26 11:05:19 -0500301 switch (mode)
Matthew Barth3800ae72018-02-19 16:08:04 -0600302 {
Matthew Barth177fe982020-05-26 11:05:19 -0500303 case TimerMode::nonfunc:
304 return duration_cast<microseconds>(seconds(_timeout));
305 case TimerMode::func:
306 return duration_cast<microseconds>(seconds(_funcDelay));
307 default:
308 // Log an internal error for undefined timer mode
309 log<level::ERR>("Undefined timer mode",
310 entry("TIMER_MODE=%u", mode));
311 elog<InternalFailure>();
312 return duration_cast<microseconds>(seconds(0));
Matthew Barth3800ae72018-02-19 16:08:04 -0600313 }
Matt Spinlera9406a72017-04-27 14:29:24 -0500314}
315
Jolie Ku69f2f482020-10-21 09:59:43 +0800316void TachSensor::setCounter(bool count)
317{
318 if (count)
319 {
320 if (_counter < _threshold)
321 {
322 ++_counter;
Matt Spinler623635c2021-03-29 13:13:59 -0500323 log<level::DEBUG>(
324 fmt::format(
325 "Incremented error counter on {} to {} (threshold {})",
326 _name, _counter, _threshold)
327 .c_str());
Jolie Ku69f2f482020-10-21 09:59:43 +0800328 }
329 }
330 else
331 {
332 if (_counter > 0)
333 {
334 --_counter;
Matt Spinler623635c2021-03-29 13:13:59 -0500335 log<level::DEBUG>(
336 fmt::format(
337 "Decremented error counter on {} to {} (threshold {})",
338 _name, _counter, _threshold)
339 .c_str());
Jolie Ku69f2f482020-10-21 09:59:43 +0800340 }
341 }
342}
343
Matt Spinlerfdfcc672021-06-01 14:51:06 -0600344void TachSensor::startCountTimer()
345{
346 if (_countTimer)
347 {
348 log<level::DEBUG>(
349 fmt::format("Starting count timer on sensor {}", _name).c_str());
350 _countTimer->restart(std::chrono::seconds(_countInterval));
351 }
352}
353
354void TachSensor::stopCountTimer()
355{
356 if (_countTimer && _countTimer->isEnabled())
357 {
358 log<level::DEBUG>(
359 fmt::format("Stopping count timer on tach sensor {}.", _name)
360 .c_str());
361 _countTimer->setEnabled(false);
362 }
363}
364
Matthew Barth4d982852017-11-17 09:37:13 -0600365void TachSensor::updateInventory(bool functional)
366{
Matthew Barth177fe982020-05-26 11:05:19 -0500367 auto objectMap =
368 util::getObjMap<bool>(_invName, util::OPERATIONAL_STATUS_INTF,
369 util::FUNCTIONAL_PROPERTY, functional);
Matthew Barth4d982852017-11-17 09:37:13 -0600370 auto response = util::SDBusPlus::lookupAndCallMethod(
Matthew Barth177fe982020-05-26 11:05:19 -0500371 _bus, util::INVENTORY_PATH, util::INVENTORY_INTF, "Notify", objectMap);
Matthew Barth4d982852017-11-17 09:37:13 -0600372 if (response.is_method_error())
373 {
374 log<level::ERR>("Error in notify update of tach sensor inventory");
375 }
376}
Matt Spinlera9406a72017-04-27 14:29:24 -0500377
Matthew Barth177fe982020-05-26 11:05:19 -0500378} // namespace monitor
379} // namespace fan
380} // namespace phosphor