blob: 7905b7871af6547f319080fa32e080d2cdc81ab3 [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 {
Matt Spinlerac372972021-01-25 15:11:22 -0600134 getLogger().log(
135 fmt::format("On startup, fan {} is missing", _name));
Matt Spinler27f6b682020-10-27 08:43:37 -0500136 _fanMissingErrorTimer->restartOnce(
137 std::chrono::seconds{*_fanMissingErrorDelay});
138 }
139 }
Matt Spinlerb0412d02020-10-12 16:53:52 -0500140}
141
142void Fan::startMonitor()
143{
144 _monitorReady = true;
145
146 tachChanged();
Matt Spinlerabf8da32017-04-27 14:08:45 -0500147}
148
Matt Spinlerebaae612017-04-27 14:21:48 -0500149void Fan::tachChanged()
150{
Matt Spinlerb0412d02020-10-12 16:53:52 -0500151 if (_monitorReady)
Matt Spinlerebaae612017-04-27 14:21:48 -0500152 {
Matt Spinlerb0412d02020-10-12 16:53:52 -0500153 for (auto& s : _sensors)
154 {
155 tachChanged(*s);
156 }
Matt Spinlerebaae612017-04-27 14:21:48 -0500157 }
158}
159
Matt Spinlerebaae612017-04-27 14:21:48 -0500160void Fan::tachChanged(TachSensor& sensor)
161{
Matt Spinlerc39e8592017-09-28 13:13:08 -0500162 if (_trustManager->active())
163 {
164 if (!_trustManager->checkTrust(sensor))
165 {
166 return;
167 }
168 }
169
Matthew Barth177fe982020-05-26 11:05:19 -0500170 // If this sensor is out of range at this moment, start
171 // its timer, at the end of which the inventory
172 // for the fan may get updated to not functional.
Matt Spinlera4c8f1f2017-04-27 14:38:38 -0500173
Matthew Barth177fe982020-05-26 11:05:19 -0500174 // If this sensor is OK, put everything back into a good state.
Matt Spinlera4c8f1f2017-04-27 14:38:38 -0500175
176 if (outOfRange(sensor))
177 {
Matthew Barthe11cbc62018-02-20 12:11:07 -0600178 if (sensor.functional())
Matt Spinlera4c8f1f2017-04-27 14:38:38 -0500179 {
Jolie Ku69f2f482020-10-21 09:59:43 +0800180 switch (sensor.getMethod())
181 {
182 case MethodMode::timebased:
183 // Start nonfunctional timer if not already running
184 sensor.startTimer(TimerMode::nonfunc);
185 break;
186 case MethodMode::count:
187 sensor.setCounter(true);
188 if (sensor.getCounter() >= sensor.getThreshold())
189 {
190 updateState(sensor);
191 }
192 break;
193 }
Matt Spinlera4c8f1f2017-04-27 14:38:38 -0500194 }
195 }
196 else
197 {
Jolie Ku69f2f482020-10-21 09:59:43 +0800198 switch (sensor.getMethod())
Matt Spinlera4c8f1f2017-04-27 14:38:38 -0500199 {
Jolie Ku69f2f482020-10-21 09:59:43 +0800200 case MethodMode::timebased:
201 if (sensor.functional())
202 {
Matthew Barth11b5d8f2021-01-28 14:04:09 -0600203 if (sensor.timerRunning())
204 {
205 sensor.stopTimer();
206 }
Jolie Ku69f2f482020-10-21 09:59:43 +0800207 }
208 else
209 {
210 // Start functional timer if not already running
211 sensor.startTimer(TimerMode::func);
212 }
213 break;
214 case MethodMode::count:
215 sensor.setCounter(false);
216 if (!sensor.functional() && sensor.getCounter() == 0)
217 {
218 updateState(sensor);
219 }
220 break;
Matt Spinlera4c8f1f2017-04-27 14:38:38 -0500221 }
222 }
Matt Spinlerebaae612017-04-27 14:21:48 -0500223}
224
Matthew Barthf552ea52018-01-15 16:22:04 -0600225uint64_t Fan::findTargetSpeed()
Matt Spinlerabf8da32017-04-27 14:08:45 -0500226{
227 uint64_t target = 0;
Matthew Barth177fe982020-05-26 11:05:19 -0500228 // The sensor doesn't support a target,
229 // so get it from another sensor.
Matthew Barthf552ea52018-01-15 16:22:04 -0600230 auto s = std::find_if(_sensors.begin(), _sensors.end(),
Matthew Barth177fe982020-05-26 11:05:19 -0500231 [](const auto& s) { return s->hasTarget(); });
Matt Spinlerabf8da32017-04-27 14:08:45 -0500232
Matthew Barthf552ea52018-01-15 16:22:04 -0600233 if (s != _sensors.end())
Matt Spinlerabf8da32017-04-27 14:08:45 -0500234 {
Matthew Barthf552ea52018-01-15 16:22:04 -0600235 target = (*s)->getTarget();
Matt Spinlerabf8da32017-04-27 14:08:45 -0500236 }
237
238 return target;
239}
240
Matthew Barth7c23a042021-01-26 16:21:45 -0600241size_t Fan::countNonFunctionalSensors()
Matt Spinlerabf8da32017-04-27 14:08:45 -0500242{
Matthew Barth7c23a042021-01-26 16:21:45 -0600243 return std::count_if(_sensors.begin(), _sensors.end(),
244 [](const auto& s) { return !s->functional(); });
Matt Spinlerabf8da32017-04-27 14:08:45 -0500245}
246
Matt Spinlerabf8da32017-04-27 14:08:45 -0500247bool Fan::outOfRange(const TachSensor& sensor)
248{
249 auto actual = static_cast<uint64_t>(sensor.getInput());
Matthew Barth7c23a042021-01-26 16:21:45 -0600250 auto range = sensor.getRange(_deviation);
Matt Spinlerabf8da32017-04-27 14:08:45 -0500251
Matthew Barth7c23a042021-01-26 16:21:45 -0600252 if ((actual < range.first) || (actual > range.second))
Matt Spinlerabf8da32017-04-27 14:08:45 -0500253 {
254 return true;
255 }
256
257 return false;
258}
259
Jolie Ku69f2f482020-10-21 09:59:43 +0800260void Fan::updateState(TachSensor& sensor)
Matt Spinlera9406a72017-04-27 14:29:24 -0500261{
Matthew Barth7c23a042021-01-26 16:21:45 -0600262 auto range = sensor.getRange(_deviation);
Matthew Barthe11cbc62018-02-20 12:11:07 -0600263 sensor.setFunctional(!sensor.functional());
Matt Spinlerae1f8ef2020-10-14 16:15:51 -0500264 getLogger().log(
265 fmt::format("Setting tach sensor {} functional state to {}. "
Matthew Barth7c23a042021-01-26 16:21:45 -0600266 "[target = {}, input = {}, allowed range = ({} - {})]",
267 sensor.name(), sensor.functional(), sensor.getTarget(),
268 sensor.getInput(), range.first, range.second));
Matt Spinlerae1f8ef2020-10-14 16:15:51 -0500269
270 // A zero value for _numSensorFailsForNonFunc means we aren't dealing
271 // with fan FRU functional status, only sensor functional status.
272 if (_numSensorFailsForNonFunc)
Matthew Barthe11cbc62018-02-20 12:11:07 -0600273 {
Matthew Barth7c23a042021-01-26 16:21:45 -0600274 auto numNonFuncSensors = countNonFunctionalSensors();
Matt Spinlerae1f8ef2020-10-14 16:15:51 -0500275 // If the fan was nonfunctional and enough sensors are now OK,
Matthew Barth7c23a042021-01-26 16:21:45 -0600276 // the fan can be set to functional
277 if (!_functional && !(numNonFuncSensors >= _numSensorFailsForNonFunc))
Matt Spinlerae1f8ef2020-10-14 16:15:51 -0500278 {
Matthew Barth7c23a042021-01-26 16:21:45 -0600279 getLogger().log(fmt::format("Setting fan {} to functional, number "
280 "of nonfunctional sensors = {}",
281 _name, numNonFuncSensors));
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
Matthew Barth7c23a042021-01-26 16:21:45 -0600287 // the fan to nonfunctional.
288 if (_functional && (numNonFuncSensors >= _numSensorFailsForNonFunc))
Matt Spinlerae1f8ef2020-10-14 16:15:51 -0500289 {
Matthew Barth7c23a042021-01-26 16:21:45 -0600290 getLogger().log(fmt::format("Setting fan {} to nonfunctional, "
291 "number of nonfunctional sensors = {}",
292 _name, numNonFuncSensors));
Matt Spinlerae1f8ef2020-10-14 16:15:51 -0500293 updateInventory(false);
294 }
Matt Spinlerb1e18512017-04-27 14:42:33 -0500295 }
Matt Spinlerb63aa092020-10-14 09:45:11 -0500296
297 _system.fanStatusChange(*this);
Matt Spinlera9406a72017-04-27 14:29:24 -0500298}
299
Matt Spinlerb1e18512017-04-27 14:42:33 -0500300void Fan::updateInventory(bool functional)
301{
Matthew Barth177fe982020-05-26 11:05:19 -0500302 auto objectMap =
303 util::getObjMap<bool>(_name, util::OPERATIONAL_STATUS_INTF,
304 util::FUNCTIONAL_PROPERTY, functional);
Matthew Barth51dd1852017-11-16 15:21:13 -0600305 auto response = util::SDBusPlus::lookupAndCallMethod(
Matthew Barth177fe982020-05-26 11:05:19 -0500306 _bus, util::INVENTORY_PATH, util::INVENTORY_INTF, "Notify", objectMap);
Matt Spinlerb1e18512017-04-27 14:42:33 -0500307 if (response.is_method_error())
308 {
309 log<level::ERR>("Error in Notify call to update inventory");
310 return;
311 }
312
Matthew Barth177fe982020-05-26 11:05:19 -0500313 // This will always track the current state of the inventory.
Matt Spinlerb1e18512017-04-27 14:42:33 -0500314 _functional = functional;
315}
316
Matt Spinlerb63aa092020-10-14 09:45:11 -0500317void Fan::presenceChanged(sdbusplus::message::message& msg)
318{
319 std::string interface;
320 std::map<std::string, std::variant<bool>> properties;
321
322 msg.read(interface, properties);
323
324 auto presentProp = properties.find("Present");
325 if (presentProp != properties.end())
326 {
327 _present = std::get<bool>(presentProp->second);
328
Matt Spinler27f6b682020-10-27 08:43:37 -0500329 getLogger().log(
Matt Spinlerac372972021-01-25 15:11:22 -0600330 fmt::format("Fan {} presence state change to {}", _name, _present));
Matt Spinler27f6b682020-10-27 08:43:37 -0500331
Matt Spinlerb63aa092020-10-14 09:45:11 -0500332 _system.fanStatusChange(*this);
Matt Spinler27f6b682020-10-27 08:43:37 -0500333
334 if (_fanMissingErrorDelay)
335 {
336 if (!_present)
337 {
338 _fanMissingErrorTimer->restartOnce(
339 std::chrono::seconds{*_fanMissingErrorDelay});
340 }
341 else if (_fanMissingErrorTimer->isEnabled())
342 {
343 _fanMissingErrorTimer->setEnabled(false);
344 }
345 }
Matt Spinlerb63aa092020-10-14 09:45:11 -0500346 }
347}
Matt Spinlerf13b42e2020-10-26 15:29:49 -0500348
349void Fan::sensorErrorTimerExpired(const TachSensor& sensor)
350{
351 if (_present)
352 {
353 _system.sensorErrorTimerExpired(*this, sensor);
354 }
355}
356
Matthew Barth177fe982020-05-26 11:05:19 -0500357} // namespace monitor
358} // namespace fan
359} // namespace phosphor