blob: 3f36dc7d17fa664b576a89ca5f3ccb8283d0552f [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),
Matt Spinlerf13b42e2020-10-26 15:29:49 -050074 std::get<offsetField>(s), std::get<timeoutField>(def),
75 std::get<nonfuncRotorErrDelayField>(def), event));
Matthew Barth0a9fe162018-01-26 12:53:15 -060076
77 _trustManager->registerSensor(_sensors.back());
78 }
79 catch (InvalidSensorError& e)
Jolie Ku4c3c24f2020-09-09 11:01:46 +080080 {
Matt Spinlerae1f8ef2020-10-14 16:15:51 -050081 // Count the number of failed tach sensors, though if
82 // _numSensorFailsForNonFunc is zero that means the fan should not
83 // be set to nonfunctional.
84 if (_numSensorFailsForNonFunc &&
85 (++_numFailedSensor >= _numSensorFailsForNonFunc))
Jolie Ku5d564a92020-10-23 09:04:28 +080086 {
87 // Mark associated fan as nonfunctional
88 updateInventory(false);
89 }
Jolie Ku4c3c24f2020-09-09 11:01:46 +080090 }
Matthew Barth0a9fe162018-01-26 12:53:15 -060091 }
92
Matt Spinlerb0412d02020-10-12 16:53:52 -050093#ifndef MONITOR_USE_JSON
Matthew Barth0a9fe162018-01-26 12:53:15 -060094 // Check current tach state when entering monitor mode
Matthew Barth6ad28432017-08-22 11:18:19 -050095 if (mode != Mode::init)
96 {
Matt Spinlerb0412d02020-10-12 16:53:52 -050097 _monitorReady = true;
98
Matthew Barth177fe982020-05-26 11:05:19 -050099 // The TachSensors will now have already read the input
100 // and target values, so check them.
Matthew Barth6ad28432017-08-22 11:18:19 -0500101 tachChanged();
102 }
Matt Spinlerb0412d02020-10-12 16:53:52 -0500103#else
104 // If it used the JSON config, then it also will do all the work
105 // out of fan-monitor-init, after _monitorDelay.
106 _monitorTimer.restartOnce(std::chrono::seconds(_monitorDelay));
Matt Spinlerb0412d02020-10-12 16:53:52 -0500107#endif
Matt Spinlerb63aa092020-10-14 09:45:11 -0500108
109 // Get the initial presence state
110 _present = util::SDBusPlus::getProperty<bool>(
111 util::INVENTORY_PATH + _name, util::INV_ITEM_IFACE, "Present");
Matt Spinler27f6b682020-10-27 08:43:37 -0500112
113 if (_fanMissingErrorDelay)
114 {
115 _fanMissingErrorTimer = std::make_unique<
116 sdeventplus::utility::Timer<sdeventplus::ClockId::Monotonic>>(
117 event, std::bind(&System::fanMissingErrorTimerExpired, &system,
118 std::ref(*this)));
119
120 if (!_present)
121 {
122 // The fan presence application handles the journal for missing
123 // fans, so only internally log missing fan info here.
124 getLogger().log(fmt::format("On startup, fan {} is missing", _name),
125 Logger::quiet);
126 _fanMissingErrorTimer->restartOnce(
127 std::chrono::seconds{*_fanMissingErrorDelay});
128 }
129 }
Matt Spinlerb0412d02020-10-12 16:53:52 -0500130}
131
132void Fan::startMonitor()
133{
134 _monitorReady = true;
135
136 tachChanged();
Matt Spinlerabf8da32017-04-27 14:08:45 -0500137}
138
Matt Spinlerebaae612017-04-27 14:21:48 -0500139void Fan::tachChanged()
140{
Matt Spinlerb0412d02020-10-12 16:53:52 -0500141 if (_monitorReady)
Matt Spinlerebaae612017-04-27 14:21:48 -0500142 {
Matt Spinlerb0412d02020-10-12 16:53:52 -0500143 for (auto& s : _sensors)
144 {
145 tachChanged(*s);
146 }
Matt Spinlerebaae612017-04-27 14:21:48 -0500147 }
148}
149
Matt Spinlerebaae612017-04-27 14:21:48 -0500150void Fan::tachChanged(TachSensor& sensor)
151{
Matt Spinlerc39e8592017-09-28 13:13:08 -0500152 if (_trustManager->active())
153 {
154 if (!_trustManager->checkTrust(sensor))
155 {
156 return;
157 }
158 }
159
Matthew Barth177fe982020-05-26 11:05:19 -0500160 // If this sensor is out of range at this moment, start
161 // its timer, at the end of which the inventory
162 // for the fan may get updated to not functional.
Matt Spinlera4c8f1f2017-04-27 14:38:38 -0500163
Matthew Barth177fe982020-05-26 11:05:19 -0500164 // If this sensor is OK, put everything back into a good state.
Matt Spinlera4c8f1f2017-04-27 14:38:38 -0500165
166 if (outOfRange(sensor))
167 {
Matthew Barthe11cbc62018-02-20 12:11:07 -0600168 if (sensor.functional())
Matt Spinlera4c8f1f2017-04-27 14:38:38 -0500169 {
Matthew Barthe11cbc62018-02-20 12:11:07 -0600170 // Start nonfunctional timer if not already running
Matthew Barth3800ae72018-02-19 16:08:04 -0600171 sensor.startTimer(TimerMode::nonfunc);
Matt Spinlera4c8f1f2017-04-27 14:38:38 -0500172 }
173 }
174 else
175 {
Matthew Barthe11cbc62018-02-20 12:11:07 -0600176 if (sensor.functional())
Matt Spinlera4c8f1f2017-04-27 14:38:38 -0500177 {
Matt Spinler6fa181c2017-09-27 16:24:45 -0500178 sensor.stopTimer();
Matt Spinlera4c8f1f2017-04-27 14:38:38 -0500179 }
Matthew Barthe11cbc62018-02-20 12:11:07 -0600180 else
Matt Spinlera4c8f1f2017-04-27 14:38:38 -0500181 {
Matthew Barthe11cbc62018-02-20 12:11:07 -0600182 // Start functional timer if not already running
183 sensor.startTimer(TimerMode::func);
Matt Spinlera4c8f1f2017-04-27 14:38:38 -0500184 }
185 }
Matt Spinlerebaae612017-04-27 14:21:48 -0500186}
187
Matthew Barthf552ea52018-01-15 16:22:04 -0600188uint64_t Fan::findTargetSpeed()
Matt Spinlerabf8da32017-04-27 14:08:45 -0500189{
190 uint64_t target = 0;
Matthew Barth177fe982020-05-26 11:05:19 -0500191 // The sensor doesn't support a target,
192 // so get it from another sensor.
Matthew Barthf552ea52018-01-15 16:22:04 -0600193 auto s = std::find_if(_sensors.begin(), _sensors.end(),
Matthew Barth177fe982020-05-26 11:05:19 -0500194 [](const auto& s) { return s->hasTarget(); });
Matt Spinlerabf8da32017-04-27 14:08:45 -0500195
Matthew Barthf552ea52018-01-15 16:22:04 -0600196 if (s != _sensors.end())
Matt Spinlerabf8da32017-04-27 14:08:45 -0500197 {
Matthew Barthf552ea52018-01-15 16:22:04 -0600198 target = (*s)->getTarget();
Matt Spinlerabf8da32017-04-27 14:08:45 -0500199 }
200
201 return target;
202}
203
Matt Spinlerabf8da32017-04-27 14:08:45 -0500204bool Fan::tooManySensorsNonfunctional()
205{
Matthew Barth177fe982020-05-26 11:05:19 -0500206 size_t numFailed =
207 std::count_if(_sensors.begin(), _sensors.end(),
208 [](const auto& s) { return !s->functional(); });
Matt Spinlerabf8da32017-04-27 14:08:45 -0500209
210 return (numFailed >= _numSensorFailsForNonFunc);
211}
212
Matt Spinlerabf8da32017-04-27 14:08:45 -0500213bool Fan::outOfRange(const TachSensor& sensor)
214{
215 auto actual = static_cast<uint64_t>(sensor.getInput());
Matthew Barth5008daf2018-01-15 16:38:24 -0600216 auto target = sensor.getTarget();
Lei YU8e5d1972018-01-26 17:14:00 +0800217 auto factor = sensor.getFactor();
218 auto offset = sensor.getOffset();
Matt Spinlerabf8da32017-04-27 14:08:45 -0500219
220 uint64_t min = target * (100 - _deviation) / 100;
221 uint64_t max = target * (100 + _deviation) / 100;
222
Lei YU8e5d1972018-01-26 17:14:00 +0800223 // TODO: openbmc/openbmc#2937 enhance this function
224 // either by making it virtual, or by predefining different
225 // outOfRange ops and selecting by yaml config
226 min = min * factor + offset;
227 max = max * factor + offset;
Matt Spinlerabf8da32017-04-27 14:08:45 -0500228 if ((actual < min) || (actual > max))
229 {
230 return true;
231 }
232
233 return false;
234}
235
Matt Spinlera9406a72017-04-27 14:29:24 -0500236void Fan::timerExpired(TachSensor& sensor)
237{
Matthew Barthe11cbc62018-02-20 12:11:07 -0600238 sensor.setFunctional(!sensor.functional());
239
Matt Spinlerae1f8ef2020-10-14 16:15:51 -0500240 getLogger().log(
241 fmt::format("Setting tach sensor {} functional state to {}. "
242 "Actual speed: {} Target speed: {}",
243 sensor.name(), sensor.functional(), sensor.getInput(),
244 sensor.getTarget()));
245
246 // A zero value for _numSensorFailsForNonFunc means we aren't dealing
247 // with fan FRU functional status, only sensor functional status.
248 if (_numSensorFailsForNonFunc)
Matthew Barthe11cbc62018-02-20 12:11:07 -0600249 {
Matt Spinlerae1f8ef2020-10-14 16:15:51 -0500250 // If the fan was nonfunctional and enough sensors are now OK,
251 // the fan can go back to functional
252 if (!_functional && !tooManySensorsNonfunctional())
253 {
254 getLogger().log(
255 fmt::format("Setting fan {} back to functional", _name));
Matthew Barthe11cbc62018-02-20 12:11:07 -0600256
Matt Spinlerae1f8ef2020-10-14 16:15:51 -0500257 updateInventory(true);
258 }
Matt Spinlera9406a72017-04-27 14:29:24 -0500259
Matt Spinlerae1f8ef2020-10-14 16:15:51 -0500260 // If the fan is currently functional, but too many
261 // contained sensors are now nonfunctional, update
262 // the whole fan nonfunctional.
263 if (_functional && tooManySensorsNonfunctional())
264 {
265 getLogger().log(fmt::format("Setting fan {} to nonfunctional "
266 "Sensor: {} "
267 "Actual speed: {} "
268 "Target speed: {}",
269 _name, sensor.name(), sensor.getInput(),
270 sensor.getTarget()));
271 updateInventory(false);
272 }
Matt Spinlerb1e18512017-04-27 14:42:33 -0500273 }
Matt Spinlerb63aa092020-10-14 09:45:11 -0500274
275 _system.fanStatusChange(*this);
Matt Spinlera9406a72017-04-27 14:29:24 -0500276}
277
Matt Spinlerb1e18512017-04-27 14:42:33 -0500278void Fan::updateInventory(bool functional)
279{
Matthew Barth177fe982020-05-26 11:05:19 -0500280 auto objectMap =
281 util::getObjMap<bool>(_name, util::OPERATIONAL_STATUS_INTF,
282 util::FUNCTIONAL_PROPERTY, functional);
Matthew Barth51dd1852017-11-16 15:21:13 -0600283 auto response = util::SDBusPlus::lookupAndCallMethod(
Matthew Barth177fe982020-05-26 11:05:19 -0500284 _bus, util::INVENTORY_PATH, util::INVENTORY_INTF, "Notify", objectMap);
Matt Spinlerb1e18512017-04-27 14:42:33 -0500285 if (response.is_method_error())
286 {
287 log<level::ERR>("Error in Notify call to update inventory");
288 return;
289 }
290
Matthew Barth177fe982020-05-26 11:05:19 -0500291 // This will always track the current state of the inventory.
Matt Spinlerb1e18512017-04-27 14:42:33 -0500292 _functional = functional;
293}
294
Matt Spinlerb63aa092020-10-14 09:45:11 -0500295void Fan::presenceChanged(sdbusplus::message::message& msg)
296{
297 std::string interface;
298 std::map<std::string, std::variant<bool>> properties;
299
300 msg.read(interface, properties);
301
302 auto presentProp = properties.find("Present");
303 if (presentProp != properties.end())
304 {
305 _present = std::get<bool>(presentProp->second);
306
Matt Spinler27f6b682020-10-27 08:43:37 -0500307 getLogger().log(
308 fmt::format("Fan {} presence state change to {}", _name, _present),
309 Logger::quiet);
310
Matt Spinlerb63aa092020-10-14 09:45:11 -0500311 _system.fanStatusChange(*this);
Matt Spinler27f6b682020-10-27 08:43:37 -0500312
313 if (_fanMissingErrorDelay)
314 {
315 if (!_present)
316 {
317 _fanMissingErrorTimer->restartOnce(
318 std::chrono::seconds{*_fanMissingErrorDelay});
319 }
320 else if (_fanMissingErrorTimer->isEnabled())
321 {
322 _fanMissingErrorTimer->setEnabled(false);
323 }
324 }
Matt Spinlerb63aa092020-10-14 09:45:11 -0500325 }
326}
Matt Spinlerf13b42e2020-10-26 15:29:49 -0500327
328void Fan::sensorErrorTimerExpired(const TachSensor& sensor)
329{
330 if (_present)
331 {
332 _system.sensorErrorTimerExpired(*this, sensor);
333 }
334}
335
Matthew Barth177fe982020-05-26 11:05:19 -0500336} // namespace monitor
337} // namespace fan
338} // namespace phosphor