blob: 228ecd32cf79d57815d623f4f31d61e9e08d8256 [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 */
16#include <algorithm>
17#include <phosphor-logging/log.hpp>
18#include "fan.hpp"
19#include "types.hpp"
Matt Spinlerb1e18512017-04-27 14:42:33 -050020#include "utility.hpp"
Matthew Barth51dd1852017-11-16 15:21:13 -060021#include "sdbusplus.hpp"
Matt Spinlerabf8da32017-04-27 14:08:45 -050022
23namespace phosphor
24{
25namespace fan
26{
27namespace monitor
28{
29
30using namespace phosphor::logging;
31
Matthew Barth6ad28432017-08-22 11:18:19 -050032Fan::Fan(Mode mode,
33 sdbusplus::bus::bus& bus,
Matt Spinlere824f982017-05-11 10:07:55 -050034 phosphor::fan::event::EventPtr& events,
Matt Spinlerc39e8592017-09-28 13:13:08 -050035 std::unique_ptr<trust::Manager>& trust,
Matt Spinlerabf8da32017-04-27 14:08:45 -050036 const FanDefinition& def) :
37 _bus(bus),
38 _name(std::get<fanNameField>(def)),
39 _deviation(std::get<fanDeviationField>(def)),
Matt Spinlerc39e8592017-09-28 13:13:08 -050040 _numSensorFailsForNonFunc(std::get<numSensorFailsForNonfuncField>(def)),
41 _trustManager(trust)
Matt Spinlerabf8da32017-04-27 14:08:45 -050042{
Matthew Barth0a9fe162018-01-26 12:53:15 -060043 // Setup tach sensors for monitoring
44 auto& sensors = std::get<sensorListField>(def);
45 for (auto& s : sensors)
46 {
47 try
48 {
49 _sensors.emplace_back(
Matthew Barth32affb92018-02-16 16:11:13 -060050 std::make_shared<TachSensor>(
Matthew Barth0a9fe162018-01-26 12:53:15 -060051 mode,
52 bus,
53 *this,
54 std::get<sensorNameField>(s),
55 std::get<hasTargetField>(s),
Matthew Barth9396bcc2018-02-19 14:13:20 -060056 std::get<funcDelay>(def),
Lei YU80f271b2018-01-31 15:24:46 +080057 std::get<targetInterfaceField>(s),
Lei YU8e5d1972018-01-26 17:14:00 +080058 std::get<factorField>(s),
59 std::get<offsetField>(s),
Matthew Barth0a9fe162018-01-26 12:53:15 -060060 std::get<timeoutField>(def),
61 events));
62
63 _trustManager->registerSensor(_sensors.back());
64 }
65 catch (InvalidSensorError& e)
66 {
67
68 }
69 }
70
Matt Spinlerb1e18512017-04-27 14:42:33 -050071 //Start from a known state of functional
72 updateInventory(true);
73
Matthew Barth0a9fe162018-01-26 12:53:15 -060074 // Check current tach state when entering monitor mode
Matthew Barth6ad28432017-08-22 11:18:19 -050075 if (mode != Mode::init)
76 {
Matthew Barth6ad28432017-08-22 11:18:19 -050077 //The TachSensors will now have already read the input
78 //and target values, so check them.
79 tachChanged();
80 }
Matt Spinlerabf8da32017-04-27 14:08:45 -050081}
82
83
Matt Spinlerebaae612017-04-27 14:21:48 -050084void Fan::tachChanged()
85{
86 for (auto& s : _sensors)
87 {
88 tachChanged(*s);
89 }
90}
91
92
93void Fan::tachChanged(TachSensor& sensor)
94{
Matt Spinlerc39e8592017-09-28 13:13:08 -050095 if (_trustManager->active())
96 {
97 if (!_trustManager->checkTrust(sensor))
98 {
99 return;
100 }
101 }
102
Matt Spinler6fa181c2017-09-27 16:24:45 -0500103 auto running = sensor.timerRunning();
Matt Spinlera4c8f1f2017-04-27 14:38:38 -0500104
105 //If this sensor is out of range at this moment, start
106 //its timer, at the end of which the inventory
107 //for the fan may get updated to not functional.
108
109 //If this sensor is OK, put everything back into a good state.
110
111 if (outOfRange(sensor))
112 {
113 if (sensor.functional() && !running)
114 {
Matt Spinler6fa181c2017-09-27 16:24:45 -0500115 sensor.startTimer();
Matt Spinlera4c8f1f2017-04-27 14:38:38 -0500116 }
117 }
118 else
119 {
120 if (!sensor.functional())
121 {
122 sensor.setFunctional(true);
123 }
124
125 if (running)
126 {
Matt Spinler6fa181c2017-09-27 16:24:45 -0500127 sensor.stopTimer();
Matt Spinlera4c8f1f2017-04-27 14:38:38 -0500128 }
129
130 //If the fan was nonfunctional and enough sensors are now OK,
131 //the fan can go back to functional
132 if (!_functional && !tooManySensorsNonfunctional())
133 {
134 log<level::INFO>("Setting a fan back to functional",
135 entry("FAN=%s", _name.c_str()));
136
Matt Spinlerb1e18512017-04-27 14:42:33 -0500137 updateInventory(true);
Matt Spinlera4c8f1f2017-04-27 14:38:38 -0500138 }
139 }
Matt Spinlerebaae612017-04-27 14:21:48 -0500140}
141
142
Matthew Barthf552ea52018-01-15 16:22:04 -0600143uint64_t Fan::findTargetSpeed()
Matt Spinlerabf8da32017-04-27 14:08:45 -0500144{
145 uint64_t target = 0;
Matthew Barthf552ea52018-01-15 16:22:04 -0600146 //The sensor doesn't support a target,
147 //so get it from another sensor.
148 auto s = std::find_if(_sensors.begin(), _sensors.end(),
149 [](const auto& s)
150 {
151 return s->hasTarget();
152 });
Matt Spinlerabf8da32017-04-27 14:08:45 -0500153
Matthew Barthf552ea52018-01-15 16:22:04 -0600154 if (s != _sensors.end())
Matt Spinlerabf8da32017-04-27 14:08:45 -0500155 {
Matthew Barthf552ea52018-01-15 16:22:04 -0600156 target = (*s)->getTarget();
Matt Spinlerabf8da32017-04-27 14:08:45 -0500157 }
158
159 return target;
160}
161
162
163bool Fan::tooManySensorsNonfunctional()
164{
165 size_t numFailed = std::count_if(_sensors.begin(), _sensors.end(),
166 [](const auto& s)
167 {
168 return !s->functional();
169 });
170
171 return (numFailed >= _numSensorFailsForNonFunc);
172}
173
174
175bool Fan::outOfRange(const TachSensor& sensor)
176{
177 auto actual = static_cast<uint64_t>(sensor.getInput());
Matthew Barth5008daf2018-01-15 16:38:24 -0600178 auto target = sensor.getTarget();
Lei YU8e5d1972018-01-26 17:14:00 +0800179 auto factor = sensor.getFactor();
180 auto offset = sensor.getOffset();
Matt Spinlerabf8da32017-04-27 14:08:45 -0500181
182 uint64_t min = target * (100 - _deviation) / 100;
183 uint64_t max = target * (100 + _deviation) / 100;
184
Lei YU8e5d1972018-01-26 17:14:00 +0800185 // TODO: openbmc/openbmc#2937 enhance this function
186 // either by making it virtual, or by predefining different
187 // outOfRange ops and selecting by yaml config
188 min = min * factor + offset;
189 max = max * factor + offset;
Matt Spinlerabf8da32017-04-27 14:08:45 -0500190 if ((actual < min) || (actual > max))
191 {
192 return true;
193 }
194
195 return false;
196}
197
198
Matt Spinlera9406a72017-04-27 14:29:24 -0500199void Fan::timerExpired(TachSensor& sensor)
200{
201 sensor.setFunctional(false);
202
203 //If the fan is currently functional, but too many
204 //contained sensors are now nonfunctional, update
205 //the whole fan nonfunctional.
Matt Spinlerb1e18512017-04-27 14:42:33 -0500206
207 if (_functional && tooManySensorsNonfunctional())
208 {
209 log<level::ERR>("Setting a fan to nonfunctional",
Matt Spinlerce75b512017-07-26 15:10:48 -0500210 entry("FAN=%s", _name.c_str()),
211 entry("TACH_SENSOR=%s", sensor.name().c_str()),
212 entry("ACTUAL_SPEED=%lld", sensor.getInput()),
Matthew Barth5008daf2018-01-15 16:38:24 -0600213 entry("TARGET_SPEED=%lld", sensor.getTarget()));
Matt Spinlerb1e18512017-04-27 14:42:33 -0500214
215 updateInventory(false);
216 }
Matt Spinlera9406a72017-04-27 14:29:24 -0500217}
218
Matt Spinlerb1e18512017-04-27 14:42:33 -0500219
220void Fan::updateInventory(bool functional)
221{
Matthew Barth51dd1852017-11-16 15:21:13 -0600222 auto objectMap = util::getObjMap<bool>(
223 _name,
224 util::OPERATIONAL_STATUS_INTF,
225 util::FUNCTIONAL_PROPERTY,
226 functional);
227 auto response = util::SDBusPlus::lookupAndCallMethod(
228 _bus,
229 util::INVENTORY_PATH,
230 util::INVENTORY_INTF,
231 "Notify",
232 objectMap);
Matt Spinlerb1e18512017-04-27 14:42:33 -0500233 if (response.is_method_error())
234 {
235 log<level::ERR>("Error in Notify call to update inventory");
236 return;
237 }
238
239 //This will always track the current state of the inventory.
240 _functional = functional;
241}
242
Matt Spinlerabf8da32017-04-27 14:08:45 -0500243}
244}
245}