blob: 4b84fffbff6026fdf04d54c2f6282708f4b238a8 [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
18#include "sdbusplus.hpp"
Matt Spinlerabf8da32017-04-27 14:08:45 -050019#include "types.hpp"
Matt Spinlerb1e18512017-04-27 14:42:33 -050020#include "utility.hpp"
Matthew Barth177fe982020-05-26 11:05:19 -050021
Jay Meyera7aed012020-10-06 14:32:22 -050022#include <fmt/format.h>
23
Matthew Barth177fe982020-05-26 11:05:19 -050024#include <phosphor-logging/log.hpp>
25
26#include <algorithm>
Matt Spinlerabf8da32017-04-27 14:08:45 -050027
28namespace phosphor
29{
30namespace fan
31{
32namespace monitor
33{
34
35using namespace phosphor::logging;
36
Matthew Barth177fe982020-05-26 11:05:19 -050037Fan::Fan(Mode mode, sdbusplus::bus::bus& bus, const sdeventplus::Event& event,
38 std::unique_ptr<trust::Manager>& trust, const FanDefinition& def) :
Matt Spinlerabf8da32017-04-27 14:08:45 -050039 _bus(bus),
40 _name(std::get<fanNameField>(def)),
41 _deviation(std::get<fanDeviationField>(def)),
Matt Spinlerc39e8592017-09-28 13:13:08 -050042 _numSensorFailsForNonFunc(std::get<numSensorFailsForNonfuncField>(def)),
43 _trustManager(trust)
Matt Spinlerabf8da32017-04-27 14:08:45 -050044{
Jolie Ku4c3c24f2020-09-09 11:01:46 +080045 // Start from a known state of functional
46 updateInventory(true);
47
Matthew Barth0a9fe162018-01-26 12:53:15 -060048 // Setup tach sensors for monitoring
49 auto& sensors = std::get<sensorListField>(def);
50 for (auto& s : sensors)
51 {
52 try
53 {
Matthew Barth177fe982020-05-26 11:05:19 -050054 _sensors.emplace_back(std::make_shared<TachSensor>(
55 mode, bus, *this, std::get<sensorNameField>(s),
56 std::get<hasTargetField>(s), std::get<funcDelay>(def),
57 std::get<targetInterfaceField>(s), std::get<factorField>(s),
58 std::get<offsetField>(s), std::get<timeoutField>(def), event));
Matthew Barth0a9fe162018-01-26 12:53:15 -060059
60 _trustManager->registerSensor(_sensors.back());
61 }
62 catch (InvalidSensorError& e)
Jolie Ku4c3c24f2020-09-09 11:01:46 +080063 {
64 // mark associated fan as nonfunctional
65 updateInventory(false);
66 }
Matthew Barth0a9fe162018-01-26 12:53:15 -060067 }
68
Matthew Barth0a9fe162018-01-26 12:53:15 -060069 // Check current tach state when entering monitor mode
Matthew Barth6ad28432017-08-22 11:18:19 -050070 if (mode != Mode::init)
71 {
Matthew Barth177fe982020-05-26 11:05:19 -050072 // The TachSensors will now have already read the input
73 // and target values, so check them.
Matthew Barth6ad28432017-08-22 11:18:19 -050074 tachChanged();
75 }
Matt Spinlerabf8da32017-04-27 14:08:45 -050076}
77
Matt Spinlerebaae612017-04-27 14:21:48 -050078void Fan::tachChanged()
79{
80 for (auto& s : _sensors)
81 {
82 tachChanged(*s);
83 }
84}
85
Matt Spinlerebaae612017-04-27 14:21:48 -050086void Fan::tachChanged(TachSensor& sensor)
87{
Matt Spinlerc39e8592017-09-28 13:13:08 -050088 if (_trustManager->active())
89 {
90 if (!_trustManager->checkTrust(sensor))
91 {
92 return;
93 }
94 }
95
Matthew Barth177fe982020-05-26 11:05:19 -050096 // If this sensor is out of range at this moment, start
97 // its timer, at the end of which the inventory
98 // for the fan may get updated to not functional.
Matt Spinlera4c8f1f2017-04-27 14:38:38 -050099
Matthew Barth177fe982020-05-26 11:05:19 -0500100 // If this sensor is OK, put everything back into a good state.
Matt Spinlera4c8f1f2017-04-27 14:38:38 -0500101
102 if (outOfRange(sensor))
103 {
Matthew Barthe11cbc62018-02-20 12:11:07 -0600104 if (sensor.functional())
Matt Spinlera4c8f1f2017-04-27 14:38:38 -0500105 {
Matthew Barthe11cbc62018-02-20 12:11:07 -0600106 // Start nonfunctional timer if not already running
Matthew Barth3800ae72018-02-19 16:08:04 -0600107 sensor.startTimer(TimerMode::nonfunc);
Matt Spinlera4c8f1f2017-04-27 14:38:38 -0500108 }
109 }
110 else
111 {
Matthew Barthe11cbc62018-02-20 12:11:07 -0600112 if (sensor.functional())
Matt Spinlera4c8f1f2017-04-27 14:38:38 -0500113 {
Matt Spinler6fa181c2017-09-27 16:24:45 -0500114 sensor.stopTimer();
Matt Spinlera4c8f1f2017-04-27 14:38:38 -0500115 }
Matthew Barthe11cbc62018-02-20 12:11:07 -0600116 else
Matt Spinlera4c8f1f2017-04-27 14:38:38 -0500117 {
Matthew Barthe11cbc62018-02-20 12:11:07 -0600118 // Start functional timer if not already running
119 sensor.startTimer(TimerMode::func);
Matt Spinlera4c8f1f2017-04-27 14:38:38 -0500120 }
121 }
Matt Spinlerebaae612017-04-27 14:21:48 -0500122}
123
Matthew Barthf552ea52018-01-15 16:22:04 -0600124uint64_t Fan::findTargetSpeed()
Matt Spinlerabf8da32017-04-27 14:08:45 -0500125{
126 uint64_t target = 0;
Matthew Barth177fe982020-05-26 11:05:19 -0500127 // The sensor doesn't support a target,
128 // so get it from another sensor.
Matthew Barthf552ea52018-01-15 16:22:04 -0600129 auto s = std::find_if(_sensors.begin(), _sensors.end(),
Matthew Barth177fe982020-05-26 11:05:19 -0500130 [](const auto& s) { return s->hasTarget(); });
Matt Spinlerabf8da32017-04-27 14:08:45 -0500131
Matthew Barthf552ea52018-01-15 16:22:04 -0600132 if (s != _sensors.end())
Matt Spinlerabf8da32017-04-27 14:08:45 -0500133 {
Matthew Barthf552ea52018-01-15 16:22:04 -0600134 target = (*s)->getTarget();
Matt Spinlerabf8da32017-04-27 14:08:45 -0500135 }
136
137 return target;
138}
139
Matt Spinlerabf8da32017-04-27 14:08:45 -0500140bool Fan::tooManySensorsNonfunctional()
141{
Matthew Barth177fe982020-05-26 11:05:19 -0500142 size_t numFailed =
143 std::count_if(_sensors.begin(), _sensors.end(),
144 [](const auto& s) { return !s->functional(); });
Matt Spinlerabf8da32017-04-27 14:08:45 -0500145
146 return (numFailed >= _numSensorFailsForNonFunc);
147}
148
Matt Spinlerabf8da32017-04-27 14:08:45 -0500149bool Fan::outOfRange(const TachSensor& sensor)
150{
151 auto actual = static_cast<uint64_t>(sensor.getInput());
Matthew Barth5008daf2018-01-15 16:38:24 -0600152 auto target = sensor.getTarget();
Lei YU8e5d1972018-01-26 17:14:00 +0800153 auto factor = sensor.getFactor();
154 auto offset = sensor.getOffset();
Matt Spinlerabf8da32017-04-27 14:08:45 -0500155
156 uint64_t min = target * (100 - _deviation) / 100;
157 uint64_t max = target * (100 + _deviation) / 100;
158
Lei YU8e5d1972018-01-26 17:14:00 +0800159 // TODO: openbmc/openbmc#2937 enhance this function
160 // either by making it virtual, or by predefining different
161 // outOfRange ops and selecting by yaml config
162 min = min * factor + offset;
163 max = max * factor + offset;
Matt Spinlerabf8da32017-04-27 14:08:45 -0500164 if ((actual < min) || (actual > max))
165 {
166 return true;
167 }
168
169 return false;
170}
171
Matt Spinlera9406a72017-04-27 14:29:24 -0500172void Fan::timerExpired(TachSensor& sensor)
173{
Matthew Barthe11cbc62018-02-20 12:11:07 -0600174 sensor.setFunctional(!sensor.functional());
175
Matthew Barth177fe982020-05-26 11:05:19 -0500176 // If the fan was nonfunctional and enough sensors are now OK,
177 // the fan can go back to functional
Matthew Barthe11cbc62018-02-20 12:11:07 -0600178 if (!_functional && !tooManySensorsNonfunctional())
179 {
Jay Meyera7aed012020-10-06 14:32:22 -0500180 log<level::INFO>(
181 fmt::format("Setting fan {} back to functional", _name).c_str());
Matthew Barthe11cbc62018-02-20 12:11:07 -0600182
183 updateInventory(true);
184 }
Matt Spinlera9406a72017-04-27 14:29:24 -0500185
Matthew Barth177fe982020-05-26 11:05:19 -0500186 // If the fan is currently functional, but too many
187 // contained sensors are now nonfunctional, update
188 // the whole fan nonfunctional.
Matt Spinlerb1e18512017-04-27 14:42:33 -0500189 if (_functional && tooManySensorsNonfunctional())
190 {
Jay Meyera7aed012020-10-06 14:32:22 -0500191 log<level::ERR>(fmt::format("Setting fan {} to nonfunctional "
192 "Sensor: {} "
193 "Actual speed: {} "
194 "Target speed: {}",
195 _name, sensor.name(), sensor.getInput(),
196 sensor.getTarget())
197 .c_str());
Matt Spinlerb1e18512017-04-27 14:42:33 -0500198
199 updateInventory(false);
200 }
Matt Spinlera9406a72017-04-27 14:29:24 -0500201}
202
Matt Spinlerb1e18512017-04-27 14:42:33 -0500203void Fan::updateInventory(bool functional)
204{
Matthew Barth177fe982020-05-26 11:05:19 -0500205 auto objectMap =
206 util::getObjMap<bool>(_name, util::OPERATIONAL_STATUS_INTF,
207 util::FUNCTIONAL_PROPERTY, functional);
Matthew Barth51dd1852017-11-16 15:21:13 -0600208 auto response = util::SDBusPlus::lookupAndCallMethod(
Matthew Barth177fe982020-05-26 11:05:19 -0500209 _bus, util::INVENTORY_PATH, util::INVENTORY_INTF, "Notify", objectMap);
Matt Spinlerb1e18512017-04-27 14:42:33 -0500210 if (response.is_method_error())
211 {
212 log<level::ERR>("Error in Notify call to update inventory");
213 return;
214 }
215
Matthew Barth177fe982020-05-26 11:05:19 -0500216 // This will always track the current state of the inventory.
Matt Spinlerb1e18512017-04-27 14:42:33 -0500217 _functional = functional;
218}
219
Matthew Barth177fe982020-05-26 11:05:19 -0500220} // namespace monitor
221} // namespace fan
222} // namespace phosphor