blob: 58730daa223edc96bcc9580753fd255e9b34af34 [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 {
Matthew Barth11b5d8f2021-01-28 14:04:09 -0600205 if (sensor.timerRunning())
206 {
207 sensor.stopTimer();
208 }
Jolie Ku69f2f482020-10-21 09:59:43 +0800209 }
210 else
211 {
212 // Start functional timer if not already running
213 sensor.startTimer(TimerMode::func);
214 }
215 break;
216 case MethodMode::count:
217 sensor.setCounter(false);
218 if (!sensor.functional() && sensor.getCounter() == 0)
219 {
220 updateState(sensor);
221 }
222 break;
Matt Spinlera4c8f1f2017-04-27 14:38:38 -0500223 }
224 }
Matt Spinlerebaae612017-04-27 14:21:48 -0500225}
226
Matthew Barthf552ea52018-01-15 16:22:04 -0600227uint64_t Fan::findTargetSpeed()
Matt Spinlerabf8da32017-04-27 14:08:45 -0500228{
229 uint64_t target = 0;
Matthew Barth177fe982020-05-26 11:05:19 -0500230 // The sensor doesn't support a target,
231 // so get it from another sensor.
Matthew Barthf552ea52018-01-15 16:22:04 -0600232 auto s = std::find_if(_sensors.begin(), _sensors.end(),
Matthew Barth177fe982020-05-26 11:05:19 -0500233 [](const auto& s) { return s->hasTarget(); });
Matt Spinlerabf8da32017-04-27 14:08:45 -0500234
Matthew Barthf552ea52018-01-15 16:22:04 -0600235 if (s != _sensors.end())
Matt Spinlerabf8da32017-04-27 14:08:45 -0500236 {
Matthew Barthf552ea52018-01-15 16:22:04 -0600237 target = (*s)->getTarget();
Matt Spinlerabf8da32017-04-27 14:08:45 -0500238 }
239
240 return target;
241}
242
Matthew Barth7c23a042021-01-26 16:21:45 -0600243size_t Fan::countNonFunctionalSensors()
Matt Spinlerabf8da32017-04-27 14:08:45 -0500244{
Matthew Barth7c23a042021-01-26 16:21:45 -0600245 return std::count_if(_sensors.begin(), _sensors.end(),
246 [](const auto& s) { return !s->functional(); });
Matt Spinlerabf8da32017-04-27 14:08:45 -0500247}
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 Barth7c23a042021-01-26 16:21:45 -0600252 auto range = sensor.getRange(_deviation);
Matt Spinlerabf8da32017-04-27 14:08:45 -0500253
Matthew Barth7c23a042021-01-26 16:21:45 -0600254 if ((actual < range.first) || (actual > range.second))
Matt Spinlerabf8da32017-04-27 14:08:45 -0500255 {
256 return true;
257 }
258
259 return false;
260}
261
Jolie Ku69f2f482020-10-21 09:59:43 +0800262void Fan::updateState(TachSensor& sensor)
Matt Spinlera9406a72017-04-27 14:29:24 -0500263{
Matthew Barth7c23a042021-01-26 16:21:45 -0600264 auto range = sensor.getRange(_deviation);
Matthew Barthe11cbc62018-02-20 12:11:07 -0600265 sensor.setFunctional(!sensor.functional());
Matt Spinlerae1f8ef2020-10-14 16:15:51 -0500266 getLogger().log(
267 fmt::format("Setting tach sensor {} functional state to {}. "
Matthew Barth7c23a042021-01-26 16:21:45 -0600268 "[target = {}, input = {}, allowed range = ({} - {})]",
269 sensor.name(), sensor.functional(), sensor.getTarget(),
270 sensor.getInput(), range.first, range.second));
Matt Spinlerae1f8ef2020-10-14 16:15:51 -0500271
272 // A zero value for _numSensorFailsForNonFunc means we aren't dealing
273 // with fan FRU functional status, only sensor functional status.
274 if (_numSensorFailsForNonFunc)
Matthew Barthe11cbc62018-02-20 12:11:07 -0600275 {
Matthew Barth7c23a042021-01-26 16:21:45 -0600276 auto numNonFuncSensors = countNonFunctionalSensors();
Matt Spinlerae1f8ef2020-10-14 16:15:51 -0500277 // If the fan was nonfunctional and enough sensors are now OK,
Matthew Barth7c23a042021-01-26 16:21:45 -0600278 // the fan can be set to functional
279 if (!_functional && !(numNonFuncSensors >= _numSensorFailsForNonFunc))
Matt Spinlerae1f8ef2020-10-14 16:15:51 -0500280 {
Matthew Barth7c23a042021-01-26 16:21:45 -0600281 getLogger().log(fmt::format("Setting fan {} to functional, number "
282 "of nonfunctional sensors = {}",
283 _name, numNonFuncSensors));
Matt Spinlerae1f8ef2020-10-14 16:15:51 -0500284 updateInventory(true);
285 }
Matt Spinlera9406a72017-04-27 14:29:24 -0500286
Matt Spinlerae1f8ef2020-10-14 16:15:51 -0500287 // If the fan is currently functional, but too many
288 // contained sensors are now nonfunctional, update
Matthew Barth7c23a042021-01-26 16:21:45 -0600289 // the fan to nonfunctional.
290 if (_functional && (numNonFuncSensors >= _numSensorFailsForNonFunc))
Matt Spinlerae1f8ef2020-10-14 16:15:51 -0500291 {
Matthew Barth7c23a042021-01-26 16:21:45 -0600292 getLogger().log(fmt::format("Setting fan {} to nonfunctional, "
293 "number of nonfunctional sensors = {}",
294 _name, numNonFuncSensors));
Matt Spinlerae1f8ef2020-10-14 16:15:51 -0500295 updateInventory(false);
296 }
Matt Spinlerb1e18512017-04-27 14:42:33 -0500297 }
Matt Spinlerb63aa092020-10-14 09:45:11 -0500298
299 _system.fanStatusChange(*this);
Matt Spinlera9406a72017-04-27 14:29:24 -0500300}
301
Matt Spinlerb1e18512017-04-27 14:42:33 -0500302void Fan::updateInventory(bool functional)
303{
Matthew Barth177fe982020-05-26 11:05:19 -0500304 auto objectMap =
305 util::getObjMap<bool>(_name, util::OPERATIONAL_STATUS_INTF,
306 util::FUNCTIONAL_PROPERTY, functional);
Matthew Barth51dd1852017-11-16 15:21:13 -0600307 auto response = util::SDBusPlus::lookupAndCallMethod(
Matthew Barth177fe982020-05-26 11:05:19 -0500308 _bus, util::INVENTORY_PATH, util::INVENTORY_INTF, "Notify", objectMap);
Matt Spinlerb1e18512017-04-27 14:42:33 -0500309 if (response.is_method_error())
310 {
311 log<level::ERR>("Error in Notify call to update inventory");
312 return;
313 }
314
Matthew Barth177fe982020-05-26 11:05:19 -0500315 // This will always track the current state of the inventory.
Matt Spinlerb1e18512017-04-27 14:42:33 -0500316 _functional = functional;
317}
318
Matt Spinlerb63aa092020-10-14 09:45:11 -0500319void Fan::presenceChanged(sdbusplus::message::message& msg)
320{
321 std::string interface;
322 std::map<std::string, std::variant<bool>> properties;
323
324 msg.read(interface, properties);
325
326 auto presentProp = properties.find("Present");
327 if (presentProp != properties.end())
328 {
329 _present = std::get<bool>(presentProp->second);
330
Matt Spinler27f6b682020-10-27 08:43:37 -0500331 getLogger().log(
332 fmt::format("Fan {} presence state change to {}", _name, _present),
333 Logger::quiet);
334
Matt Spinlerb63aa092020-10-14 09:45:11 -0500335 _system.fanStatusChange(*this);
Matt Spinler27f6b682020-10-27 08:43:37 -0500336
337 if (_fanMissingErrorDelay)
338 {
339 if (!_present)
340 {
341 _fanMissingErrorTimer->restartOnce(
342 std::chrono::seconds{*_fanMissingErrorDelay});
343 }
344 else if (_fanMissingErrorTimer->isEnabled())
345 {
346 _fanMissingErrorTimer->setEnabled(false);
347 }
348 }
Matt Spinlerb63aa092020-10-14 09:45:11 -0500349 }
350}
Matt Spinlerf13b42e2020-10-26 15:29:49 -0500351
352void Fan::sensorErrorTimerExpired(const TachSensor& sensor)
353{
354 if (_present)
355 {
356 _system.sensorErrorTimerExpired(*this, sensor);
357 }
358}
359
Matthew Barth177fe982020-05-26 11:05:19 -0500360} // namespace monitor
361} // namespace fan
362} // namespace phosphor