blob: abc97436ef481b1c4114ef3c8e0d4933d65232c2 [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 */
Matt Spinlerabf8da32017-04-27 14:08:45 -050016#include "fan.hpp"
Matthew Barth177fe982020-05-26 11:05:19 -050017
Matt Spinlerae1f8ef2020-10-14 16:15:51 -050018#include "logging.hpp"
Matthew Barth177fe982020-05-26 11:05:19 -050019#include "sdbusplus.hpp"
Matt Spinlerb0412d02020-10-12 16:53:52 -050020#include "system.hpp"
Matt Spinlerabf8da32017-04-27 14:08:45 -050021#include "types.hpp"
Matt Spinlerb1e18512017-04-27 14:42:33 -050022#include "utility.hpp"
Matthew Barth177fe982020-05-26 11:05:19 -050023
Jay Meyera7aed012020-10-06 14:32:22 -050024#include <fmt/format.h>
25
Matthew Barth177fe982020-05-26 11:05:19 -050026#include <phosphor-logging/log.hpp>
27
28#include <algorithm>
Matt Spinlerabf8da32017-04-27 14:08:45 -050029
30namespace phosphor
31{
32namespace fan
33{
34namespace monitor
35{
36
37using namespace phosphor::logging;
Matt Spinlerb0412d02020-10-12 16:53:52 -050038using namespace sdbusplus::bus::match;
Matt Spinlerabf8da32017-04-27 14:08:45 -050039
Matthew Barth177fe982020-05-26 11:05:19 -050040Fan::Fan(Mode mode, sdbusplus::bus::bus& bus, const sdeventplus::Event& event,
Matt Spinlerb0412d02020-10-12 16:53:52 -050041 std::unique_ptr<trust::Manager>& trust, const FanDefinition& def,
42 System& system) :
Matt Spinlerabf8da32017-04-27 14:08:45 -050043 _bus(bus),
44 _name(std::get<fanNameField>(def)),
45 _deviation(std::get<fanDeviationField>(def)),
Matt Spinlerc39e8592017-09-28 13:13:08 -050046 _numSensorFailsForNonFunc(std::get<numSensorFailsForNonfuncField>(def)),
Matt Spinlerb0412d02020-10-12 16:53:52 -050047 _trustManager(trust),
48#ifdef MONITOR_USE_JSON
49 _monitorDelay(std::get<monitorStartDelayField>(def)),
50 _monitorTimer(event, std::bind(std::mem_fn(&Fan::startMonitor), this)),
51#endif
Matt Spinlerb63aa092020-10-14 09:45:11 -050052 _system(system),
53 _presenceMatch(bus,
54 rules::propertiesChanged(util::INVENTORY_PATH + _name,
55 util::INV_ITEM_IFACE),
56 std::bind(std::mem_fn(&Fan::presenceChanged), this,
Matt Spinler27f6b682020-10-27 08:43:37 -050057 std::placeholders::_1)),
58 _fanMissingErrorDelay(std::get<fanMissingErrDelayField>(def))
Matt Spinlerabf8da32017-04-27 14:08:45 -050059{
Matt Spinlerae1f8ef2020-10-14 16:15:51 -050060 // Start from a known state of functional (even if
61 // _numSensorFailsForNonFunc is 0)
Jolie Ku4c3c24f2020-09-09 11:01:46 +080062 updateInventory(true);
63
Matthew Barth0a9fe162018-01-26 12:53:15 -060064 // Setup tach sensors for monitoring
65 auto& sensors = std::get<sensorListField>(def);
66 for (auto& s : sensors)
67 {
68 try
69 {
Matthew Barth177fe982020-05-26 11:05:19 -050070 _sensors.emplace_back(std::make_shared<TachSensor>(
71 mode, bus, *this, std::get<sensorNameField>(s),
72 std::get<hasTargetField>(s), std::get<funcDelay>(def),
73 std::get<targetInterfaceField>(s), std::get<factorField>(s),
Jolie Ku69f2f482020-10-21 09:59:43 +080074 std::get<offsetField>(s), std::get<methodField>(def),
75 std::get<thresholdField>(s), std::get<timeoutField>(def),
Matt Spinlerf13b42e2020-10-26 15:29:49 -050076 std::get<nonfuncRotorErrDelayField>(def), event));
Matthew Barth0a9fe162018-01-26 12:53:15 -060077
78 _trustManager->registerSensor(_sensors.back());
79 }
80 catch (InvalidSensorError& e)
Jolie Ku4c3c24f2020-09-09 11:01:46 +080081 {
Matt Spinlerae1f8ef2020-10-14 16:15:51 -050082 // Count the number of failed tach sensors, though if
83 // _numSensorFailsForNonFunc is zero that means the fan should not
84 // be set to nonfunctional.
85 if (_numSensorFailsForNonFunc &&
86 (++_numFailedSensor >= _numSensorFailsForNonFunc))
Jolie Ku5d564a92020-10-23 09:04:28 +080087 {
88 // Mark associated fan as nonfunctional
89 updateInventory(false);
90 }
Jolie Ku4c3c24f2020-09-09 11:01:46 +080091 }
Matthew Barth0a9fe162018-01-26 12:53:15 -060092 }
93
Matt Spinlerb0412d02020-10-12 16:53:52 -050094#ifndef MONITOR_USE_JSON
Matthew Barth0a9fe162018-01-26 12:53:15 -060095 // Check current tach state when entering monitor mode
Matthew Barth6ad28432017-08-22 11:18:19 -050096 if (mode != Mode::init)
97 {
Matt Spinlerb0412d02020-10-12 16:53:52 -050098 _monitorReady = true;
99
Matthew Barth177fe982020-05-26 11:05:19 -0500100 // The TachSensors will now have already read the input
101 // and target values, so check them.
Matthew Barth6ad28432017-08-22 11:18:19 -0500102 tachChanged();
103 }
Matt Spinlerb0412d02020-10-12 16:53:52 -0500104#else
105 // If it used the JSON config, then it also will do all the work
106 // out of fan-monitor-init, after _monitorDelay.
107 _monitorTimer.restartOnce(std::chrono::seconds(_monitorDelay));
Matt Spinlerb0412d02020-10-12 16:53:52 -0500108#endif
Matt Spinlerb63aa092020-10-14 09:45:11 -0500109
110 // Get the initial presence state
111 _present = util::SDBusPlus::getProperty<bool>(
112 util::INVENTORY_PATH + _name, util::INV_ITEM_IFACE, "Present");
Matt Spinler27f6b682020-10-27 08:43:37 -0500113
114 if (_fanMissingErrorDelay)
115 {
116 _fanMissingErrorTimer = std::make_unique<
117 sdeventplus::utility::Timer<sdeventplus::ClockId::Monotonic>>(
118 event, std::bind(&System::fanMissingErrorTimerExpired, &system,
119 std::ref(*this)));
120
121 if (!_present)
122 {
123 // The fan presence application handles the journal for missing
124 // fans, so only internally log missing fan info here.
125 getLogger().log(fmt::format("On startup, fan {} is missing", _name),
126 Logger::quiet);
127 _fanMissingErrorTimer->restartOnce(
128 std::chrono::seconds{*_fanMissingErrorDelay});
129 }
130 }
Matt Spinlerb0412d02020-10-12 16:53:52 -0500131}
132
133void Fan::startMonitor()
134{
135 _monitorReady = true;
136
137 tachChanged();
Matt Spinlerabf8da32017-04-27 14:08:45 -0500138}
139
Matt Spinlerebaae612017-04-27 14:21:48 -0500140void Fan::tachChanged()
141{
Matt Spinlerb0412d02020-10-12 16:53:52 -0500142 if (_monitorReady)
Matt Spinlerebaae612017-04-27 14:21:48 -0500143 {
Matt Spinlerb0412d02020-10-12 16:53:52 -0500144 for (auto& s : _sensors)
145 {
146 tachChanged(*s);
147 }
Matt Spinlerebaae612017-04-27 14:21:48 -0500148 }
149}
150
Matt Spinlerebaae612017-04-27 14:21:48 -0500151void Fan::tachChanged(TachSensor& sensor)
152{
Matt Spinlerc39e8592017-09-28 13:13:08 -0500153 if (_trustManager->active())
154 {
155 if (!_trustManager->checkTrust(sensor))
156 {
157 return;
158 }
159 }
160
Matthew Barth177fe982020-05-26 11:05:19 -0500161 // If this sensor is out of range at this moment, start
162 // its timer, at the end of which the inventory
163 // for the fan may get updated to not functional.
Matt Spinlera4c8f1f2017-04-27 14:38:38 -0500164
Matthew Barth177fe982020-05-26 11:05:19 -0500165 // If this sensor is OK, put everything back into a good state.
Matt Spinlera4c8f1f2017-04-27 14:38:38 -0500166
167 if (outOfRange(sensor))
168 {
Matthew Barthe11cbc62018-02-20 12:11:07 -0600169 if (sensor.functional())
Matt Spinlera4c8f1f2017-04-27 14:38:38 -0500170 {
Jolie Ku69f2f482020-10-21 09:59:43 +0800171 switch (sensor.getMethod())
172 {
173 case MethodMode::timebased:
174 // Start nonfunctional timer if not already running
175 sensor.startTimer(TimerMode::nonfunc);
176 break;
177 case MethodMode::count:
178 sensor.setCounter(true);
179 if (sensor.getCounter() >= sensor.getThreshold())
180 {
181 updateState(sensor);
182 }
183 break;
184 }
Matt Spinlera4c8f1f2017-04-27 14:38:38 -0500185 }
186 }
187 else
188 {
Jolie Ku69f2f482020-10-21 09:59:43 +0800189 switch (sensor.getMethod())
Matt Spinlera4c8f1f2017-04-27 14:38:38 -0500190 {
Jolie Ku69f2f482020-10-21 09:59:43 +0800191 case MethodMode::timebased:
192 if (sensor.functional())
193 {
194 sensor.stopTimer();
195 }
196 else
197 {
198 // Start functional timer if not already running
199 sensor.startTimer(TimerMode::func);
200 }
201 break;
202 case MethodMode::count:
203 sensor.setCounter(false);
204 if (!sensor.functional() && sensor.getCounter() == 0)
205 {
206 updateState(sensor);
207 }
208 break;
Matt Spinlera4c8f1f2017-04-27 14:38:38 -0500209 }
210 }
Matt Spinlerebaae612017-04-27 14:21:48 -0500211}
212
Matthew Barthf552ea52018-01-15 16:22:04 -0600213uint64_t Fan::findTargetSpeed()
Matt Spinlerabf8da32017-04-27 14:08:45 -0500214{
215 uint64_t target = 0;
Matthew Barth177fe982020-05-26 11:05:19 -0500216 // The sensor doesn't support a target,
217 // so get it from another sensor.
Matthew Barthf552ea52018-01-15 16:22:04 -0600218 auto s = std::find_if(_sensors.begin(), _sensors.end(),
Matthew Barth177fe982020-05-26 11:05:19 -0500219 [](const auto& s) { return s->hasTarget(); });
Matt Spinlerabf8da32017-04-27 14:08:45 -0500220
Matthew Barthf552ea52018-01-15 16:22:04 -0600221 if (s != _sensors.end())
Matt Spinlerabf8da32017-04-27 14:08:45 -0500222 {
Matthew Barthf552ea52018-01-15 16:22:04 -0600223 target = (*s)->getTarget();
Matt Spinlerabf8da32017-04-27 14:08:45 -0500224 }
225
226 return target;
227}
228
Matt Spinlerabf8da32017-04-27 14:08:45 -0500229bool Fan::tooManySensorsNonfunctional()
230{
Matthew Barth177fe982020-05-26 11:05:19 -0500231 size_t numFailed =
232 std::count_if(_sensors.begin(), _sensors.end(),
233 [](const auto& s) { return !s->functional(); });
Matt Spinlerabf8da32017-04-27 14:08:45 -0500234
235 return (numFailed >= _numSensorFailsForNonFunc);
236}
237
Matt Spinlerabf8da32017-04-27 14:08:45 -0500238bool Fan::outOfRange(const TachSensor& sensor)
239{
240 auto actual = static_cast<uint64_t>(sensor.getInput());
Matthew Barth5008daf2018-01-15 16:38:24 -0600241 auto target = sensor.getTarget();
Lei YU8e5d1972018-01-26 17:14:00 +0800242 auto factor = sensor.getFactor();
243 auto offset = sensor.getOffset();
Matt Spinlerabf8da32017-04-27 14:08:45 -0500244
245 uint64_t min = target * (100 - _deviation) / 100;
246 uint64_t max = target * (100 + _deviation) / 100;
247
Lei YU8e5d1972018-01-26 17:14:00 +0800248 // TODO: openbmc/openbmc#2937 enhance this function
249 // either by making it virtual, or by predefining different
250 // outOfRange ops and selecting by yaml config
251 min = min * factor + offset;
252 max = max * factor + offset;
Matt Spinlerabf8da32017-04-27 14:08:45 -0500253 if ((actual < min) || (actual > max))
254 {
255 return true;
256 }
257
258 return false;
259}
260
Jolie Ku69f2f482020-10-21 09:59:43 +0800261void Fan::updateState(TachSensor& sensor)
Matt Spinlera9406a72017-04-27 14:29:24 -0500262{
Matthew Barthe11cbc62018-02-20 12:11:07 -0600263 sensor.setFunctional(!sensor.functional());
264
Matt Spinlerae1f8ef2020-10-14 16:15:51 -0500265 getLogger().log(
266 fmt::format("Setting tach sensor {} functional state to {}. "
267 "Actual speed: {} Target speed: {}",
268 sensor.name(), sensor.functional(), sensor.getInput(),
269 sensor.getTarget()));
270
271 // A zero value for _numSensorFailsForNonFunc means we aren't dealing
272 // with fan FRU functional status, only sensor functional status.
273 if (_numSensorFailsForNonFunc)
Matthew Barthe11cbc62018-02-20 12:11:07 -0600274 {
Matt Spinlerae1f8ef2020-10-14 16:15:51 -0500275 // If the fan was nonfunctional and enough sensors are now OK,
276 // the fan can go back to functional
277 if (!_functional && !tooManySensorsNonfunctional())
278 {
279 getLogger().log(
280 fmt::format("Setting fan {} back to functional", _name));
Matthew Barthe11cbc62018-02-20 12:11:07 -0600281
Matt Spinlerae1f8ef2020-10-14 16:15:51 -0500282 updateInventory(true);
283 }
Matt Spinlera9406a72017-04-27 14:29:24 -0500284
Matt Spinlerae1f8ef2020-10-14 16:15:51 -0500285 // If the fan is currently functional, but too many
286 // contained sensors are now nonfunctional, update
287 // the whole fan nonfunctional.
288 if (_functional && tooManySensorsNonfunctional())
289 {
290 getLogger().log(fmt::format("Setting fan {} to nonfunctional "
291 "Sensor: {} "
292 "Actual speed: {} "
293 "Target speed: {}",
294 _name, sensor.name(), sensor.getInput(),
295 sensor.getTarget()));
296 updateInventory(false);
297 }
Matt Spinlerb1e18512017-04-27 14:42:33 -0500298 }
Matt Spinlerb63aa092020-10-14 09:45:11 -0500299
300 _system.fanStatusChange(*this);
Matt Spinlera9406a72017-04-27 14:29:24 -0500301}
302
Matt Spinlerb1e18512017-04-27 14:42:33 -0500303void Fan::updateInventory(bool functional)
304{
Matthew Barth177fe982020-05-26 11:05:19 -0500305 auto objectMap =
306 util::getObjMap<bool>(_name, util::OPERATIONAL_STATUS_INTF,
307 util::FUNCTIONAL_PROPERTY, functional);
Matthew Barth51dd1852017-11-16 15:21:13 -0600308 auto response = util::SDBusPlus::lookupAndCallMethod(
Matthew Barth177fe982020-05-26 11:05:19 -0500309 _bus, util::INVENTORY_PATH, util::INVENTORY_INTF, "Notify", objectMap);
Matt Spinlerb1e18512017-04-27 14:42:33 -0500310 if (response.is_method_error())
311 {
312 log<level::ERR>("Error in Notify call to update inventory");
313 return;
314 }
315
Matthew Barth177fe982020-05-26 11:05:19 -0500316 // This will always track the current state of the inventory.
Matt Spinlerb1e18512017-04-27 14:42:33 -0500317 _functional = functional;
318}
319
Matt Spinlerb63aa092020-10-14 09:45:11 -0500320void Fan::presenceChanged(sdbusplus::message::message& msg)
321{
322 std::string interface;
323 std::map<std::string, std::variant<bool>> properties;
324
325 msg.read(interface, properties);
326
327 auto presentProp = properties.find("Present");
328 if (presentProp != properties.end())
329 {
330 _present = std::get<bool>(presentProp->second);
331
Matt Spinler27f6b682020-10-27 08:43:37 -0500332 getLogger().log(
333 fmt::format("Fan {} presence state change to {}", _name, _present),
334 Logger::quiet);
335
Matt Spinlerb63aa092020-10-14 09:45:11 -0500336 _system.fanStatusChange(*this);
Matt Spinler27f6b682020-10-27 08:43:37 -0500337
338 if (_fanMissingErrorDelay)
339 {
340 if (!_present)
341 {
342 _fanMissingErrorTimer->restartOnce(
343 std::chrono::seconds{*_fanMissingErrorDelay});
344 }
345 else if (_fanMissingErrorTimer->isEnabled())
346 {
347 _fanMissingErrorTimer->setEnabled(false);
348 }
349 }
Matt Spinlerb63aa092020-10-14 09:45:11 -0500350 }
351}
Matt Spinlerf13b42e2020-10-26 15:29:49 -0500352
353void Fan::sensorErrorTimerExpired(const TachSensor& sensor)
354{
355 if (_present)
356 {
357 _system.sensorErrorTimerExpired(*this, sensor);
358 }
359}
360
Matthew Barth177fe982020-05-26 11:05:19 -0500361} // namespace monitor
362} // namespace fan
363} // namespace phosphor