blob: 9fe63c12ef6c52555c304c4afd0e4ea5514d3568 [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
Matt Spinler06480142021-01-20 13:45:31 -0600111 bool available = true;
112
113 try
114 {
115 _present = util::SDBusPlus::getProperty<bool>(
116 util::INVENTORY_PATH + _name, util::INV_ITEM_IFACE, "Present");
117 }
118 catch (const util::DBusServiceError& e)
119 {
120 // This could be the initial boot and phosphor-fan-presence hasn't
121 // written to the inventory yet.
122 available = false;
123 }
Matt Spinler27f6b682020-10-27 08:43:37 -0500124
125 if (_fanMissingErrorDelay)
126 {
127 _fanMissingErrorTimer = std::make_unique<
128 sdeventplus::utility::Timer<sdeventplus::ClockId::Monotonic>>(
129 event, std::bind(&System::fanMissingErrorTimerExpired, &system,
130 std::ref(*this)));
131
Matt Spinler06480142021-01-20 13:45:31 -0600132 if (!_present && available)
Matt Spinler27f6b682020-10-27 08:43:37 -0500133 {
134 // The fan presence application handles the journal for missing
135 // fans, so only internally log missing fan info here.
136 getLogger().log(fmt::format("On startup, fan {} is missing", _name),
137 Logger::quiet);
138 _fanMissingErrorTimer->restartOnce(
139 std::chrono::seconds{*_fanMissingErrorDelay});
140 }
141 }
Matt Spinlerb0412d02020-10-12 16:53:52 -0500142}
143
144void Fan::startMonitor()
145{
146 _monitorReady = true;
147
148 tachChanged();
Matt Spinlerabf8da32017-04-27 14:08:45 -0500149}
150
Matt Spinlerebaae612017-04-27 14:21:48 -0500151void Fan::tachChanged()
152{
Matt Spinlerb0412d02020-10-12 16:53:52 -0500153 if (_monitorReady)
Matt Spinlerebaae612017-04-27 14:21:48 -0500154 {
Matt Spinlerb0412d02020-10-12 16:53:52 -0500155 for (auto& s : _sensors)
156 {
157 tachChanged(*s);
158 }
Matt Spinlerebaae612017-04-27 14:21:48 -0500159 }
160}
161
Matt Spinlerebaae612017-04-27 14:21:48 -0500162void Fan::tachChanged(TachSensor& sensor)
163{
Matt Spinlerc39e8592017-09-28 13:13:08 -0500164 if (_trustManager->active())
165 {
166 if (!_trustManager->checkTrust(sensor))
167 {
168 return;
169 }
170 }
171
Matthew Barth177fe982020-05-26 11:05:19 -0500172 // If this sensor is out of range at this moment, start
173 // its timer, at the end of which the inventory
174 // for the fan may get updated to not functional.
Matt Spinlera4c8f1f2017-04-27 14:38:38 -0500175
Matthew Barth177fe982020-05-26 11:05:19 -0500176 // If this sensor is OK, put everything back into a good state.
Matt Spinlera4c8f1f2017-04-27 14:38:38 -0500177
178 if (outOfRange(sensor))
179 {
Matthew Barthe11cbc62018-02-20 12:11:07 -0600180 if (sensor.functional())
Matt Spinlera4c8f1f2017-04-27 14:38:38 -0500181 {
Jolie Ku69f2f482020-10-21 09:59:43 +0800182 switch (sensor.getMethod())
183 {
184 case MethodMode::timebased:
185 // Start nonfunctional timer if not already running
186 sensor.startTimer(TimerMode::nonfunc);
187 break;
188 case MethodMode::count:
189 sensor.setCounter(true);
190 if (sensor.getCounter() >= sensor.getThreshold())
191 {
192 updateState(sensor);
193 }
194 break;
195 }
Matt Spinlera4c8f1f2017-04-27 14:38:38 -0500196 }
197 }
198 else
199 {
Jolie Ku69f2f482020-10-21 09:59:43 +0800200 switch (sensor.getMethod())
Matt Spinlera4c8f1f2017-04-27 14:38:38 -0500201 {
Jolie Ku69f2f482020-10-21 09:59:43 +0800202 case MethodMode::timebased:
203 if (sensor.functional())
204 {
205 sensor.stopTimer();
206 }
207 else
208 {
209 // Start functional timer if not already running
210 sensor.startTimer(TimerMode::func);
211 }
212 break;
213 case MethodMode::count:
214 sensor.setCounter(false);
215 if (!sensor.functional() && sensor.getCounter() == 0)
216 {
217 updateState(sensor);
218 }
219 break;
Matt Spinlera4c8f1f2017-04-27 14:38:38 -0500220 }
221 }
Matt Spinlerebaae612017-04-27 14:21:48 -0500222}
223
Matthew Barthf552ea52018-01-15 16:22:04 -0600224uint64_t Fan::findTargetSpeed()
Matt Spinlerabf8da32017-04-27 14:08:45 -0500225{
226 uint64_t target = 0;
Matthew Barth177fe982020-05-26 11:05:19 -0500227 // The sensor doesn't support a target,
228 // so get it from another sensor.
Matthew Barthf552ea52018-01-15 16:22:04 -0600229 auto s = std::find_if(_sensors.begin(), _sensors.end(),
Matthew Barth177fe982020-05-26 11:05:19 -0500230 [](const auto& s) { return s->hasTarget(); });
Matt Spinlerabf8da32017-04-27 14:08:45 -0500231
Matthew Barthf552ea52018-01-15 16:22:04 -0600232 if (s != _sensors.end())
Matt Spinlerabf8da32017-04-27 14:08:45 -0500233 {
Matthew Barthf552ea52018-01-15 16:22:04 -0600234 target = (*s)->getTarget();
Matt Spinlerabf8da32017-04-27 14:08:45 -0500235 }
236
237 return target;
238}
239
Matt Spinlerabf8da32017-04-27 14:08:45 -0500240bool Fan::tooManySensorsNonfunctional()
241{
Matthew Barth177fe982020-05-26 11:05:19 -0500242 size_t numFailed =
243 std::count_if(_sensors.begin(), _sensors.end(),
244 [](const auto& s) { return !s->functional(); });
Matt Spinlerabf8da32017-04-27 14:08:45 -0500245
246 return (numFailed >= _numSensorFailsForNonFunc);
247}
248
Matt Spinlerabf8da32017-04-27 14:08:45 -0500249bool Fan::outOfRange(const TachSensor& sensor)
250{
251 auto actual = static_cast<uint64_t>(sensor.getInput());
Matthew Barth5008daf2018-01-15 16:38:24 -0600252 auto target = sensor.getTarget();
Lei YU8e5d1972018-01-26 17:14:00 +0800253 auto factor = sensor.getFactor();
254 auto offset = sensor.getOffset();
Matt Spinlerabf8da32017-04-27 14:08:45 -0500255
256 uint64_t min = target * (100 - _deviation) / 100;
257 uint64_t max = target * (100 + _deviation) / 100;
258
Lei YU8e5d1972018-01-26 17:14:00 +0800259 // TODO: openbmc/openbmc#2937 enhance this function
260 // either by making it virtual, or by predefining different
261 // outOfRange ops and selecting by yaml config
262 min = min * factor + offset;
263 max = max * factor + offset;
Matt Spinlerabf8da32017-04-27 14:08:45 -0500264 if ((actual < min) || (actual > max))
265 {
266 return true;
267 }
268
269 return false;
270}
271
Jolie Ku69f2f482020-10-21 09:59:43 +0800272void Fan::updateState(TachSensor& sensor)
Matt Spinlera9406a72017-04-27 14:29:24 -0500273{
Matthew Barthe11cbc62018-02-20 12:11:07 -0600274 sensor.setFunctional(!sensor.functional());
275
Matt Spinlerae1f8ef2020-10-14 16:15:51 -0500276 getLogger().log(
277 fmt::format("Setting tach sensor {} functional state to {}. "
278 "Actual speed: {} Target speed: {}",
279 sensor.name(), sensor.functional(), sensor.getInput(),
280 sensor.getTarget()));
281
282 // A zero value for _numSensorFailsForNonFunc means we aren't dealing
283 // with fan FRU functional status, only sensor functional status.
284 if (_numSensorFailsForNonFunc)
Matthew Barthe11cbc62018-02-20 12:11:07 -0600285 {
Matt Spinlerae1f8ef2020-10-14 16:15:51 -0500286 // If the fan was nonfunctional and enough sensors are now OK,
287 // the fan can go back to functional
288 if (!_functional && !tooManySensorsNonfunctional())
289 {
290 getLogger().log(
291 fmt::format("Setting fan {} back to functional", _name));
Matthew Barthe11cbc62018-02-20 12:11:07 -0600292
Matt Spinlerae1f8ef2020-10-14 16:15:51 -0500293 updateInventory(true);
294 }
Matt Spinlera9406a72017-04-27 14:29:24 -0500295
Matt Spinlerae1f8ef2020-10-14 16:15:51 -0500296 // If the fan is currently functional, but too many
297 // contained sensors are now nonfunctional, update
298 // the whole fan nonfunctional.
299 if (_functional && tooManySensorsNonfunctional())
300 {
301 getLogger().log(fmt::format("Setting fan {} to nonfunctional "
302 "Sensor: {} "
303 "Actual speed: {} "
304 "Target speed: {}",
305 _name, sensor.name(), sensor.getInput(),
306 sensor.getTarget()));
307 updateInventory(false);
308 }
Matt Spinlerb1e18512017-04-27 14:42:33 -0500309 }
Matt Spinlerb63aa092020-10-14 09:45:11 -0500310
311 _system.fanStatusChange(*this);
Matt Spinlera9406a72017-04-27 14:29:24 -0500312}
313
Matt Spinlerb1e18512017-04-27 14:42:33 -0500314void Fan::updateInventory(bool functional)
315{
Matthew Barth177fe982020-05-26 11:05:19 -0500316 auto objectMap =
317 util::getObjMap<bool>(_name, util::OPERATIONAL_STATUS_INTF,
318 util::FUNCTIONAL_PROPERTY, functional);
Matthew Barth51dd1852017-11-16 15:21:13 -0600319 auto response = util::SDBusPlus::lookupAndCallMethod(
Matthew Barth177fe982020-05-26 11:05:19 -0500320 _bus, util::INVENTORY_PATH, util::INVENTORY_INTF, "Notify", objectMap);
Matt Spinlerb1e18512017-04-27 14:42:33 -0500321 if (response.is_method_error())
322 {
323 log<level::ERR>("Error in Notify call to update inventory");
324 return;
325 }
326
Matthew Barth177fe982020-05-26 11:05:19 -0500327 // This will always track the current state of the inventory.
Matt Spinlerb1e18512017-04-27 14:42:33 -0500328 _functional = functional;
329}
330
Matt Spinlerb63aa092020-10-14 09:45:11 -0500331void Fan::presenceChanged(sdbusplus::message::message& msg)
332{
333 std::string interface;
334 std::map<std::string, std::variant<bool>> properties;
335
336 msg.read(interface, properties);
337
338 auto presentProp = properties.find("Present");
339 if (presentProp != properties.end())
340 {
341 _present = std::get<bool>(presentProp->second);
342
Matt Spinler27f6b682020-10-27 08:43:37 -0500343 getLogger().log(
344 fmt::format("Fan {} presence state change to {}", _name, _present),
345 Logger::quiet);
346
Matt Spinlerb63aa092020-10-14 09:45:11 -0500347 _system.fanStatusChange(*this);
Matt Spinler27f6b682020-10-27 08:43:37 -0500348
349 if (_fanMissingErrorDelay)
350 {
351 if (!_present)
352 {
353 _fanMissingErrorTimer->restartOnce(
354 std::chrono::seconds{*_fanMissingErrorDelay});
355 }
356 else if (_fanMissingErrorTimer->isEnabled())
357 {
358 _fanMissingErrorTimer->setEnabled(false);
359 }
360 }
Matt Spinlerb63aa092020-10-14 09:45:11 -0500361 }
362}
Matt Spinlerf13b42e2020-10-26 15:29:49 -0500363
364void Fan::sensorErrorTimerExpired(const TachSensor& sensor)
365{
366 if (_present)
367 {
368 _system.sensorErrorTimerExpired(*this, sensor);
369 }
370}
371
Matthew Barth177fe982020-05-26 11:05:19 -0500372} // namespace monitor
373} // namespace fan
374} // namespace phosphor