blob: f24312de55115cea5c3cba9c9628a998ed057400 [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{
Matt Spinlerb1e18512017-04-27 14:42:33 -050043 //Start from a known state of functional
44 updateInventory(true);
45
Matthew Barth6ad28432017-08-22 11:18:19 -050046 // Setup tach sensors for monitoring when in monitor mode
47 if (mode != Mode::init)
48 {
49 auto& sensors = std::get<sensorListField>(def);
50 for (auto& s : sensors)
51 {
52 try
53 {
54 _sensors.emplace_back(
55 std::make_unique<TachSensor>(
56 bus,
57 *this,
58 std::get<sensorNameField>(s),
59 std::get<hasTargetField>(s),
60 std::get<timeoutField>(def),
61 events));
Matt Spinlerc39e8592017-09-28 13:13:08 -050062
63 _trustManager->registerSensor(_sensors.back());
Matthew Barth6ad28432017-08-22 11:18:19 -050064 }
65 catch (InvalidSensorError& e)
66 {
67
68 }
69 }
70
71 //The TachSensors will now have already read the input
72 //and target values, so check them.
73 tachChanged();
74 }
Matt Spinlerabf8da32017-04-27 14:08:45 -050075}
76
77
Matt Spinlerebaae612017-04-27 14:21:48 -050078void Fan::tachChanged()
79{
80 for (auto& s : _sensors)
81 {
82 tachChanged(*s);
83 }
84}
85
86
87void Fan::tachChanged(TachSensor& sensor)
88{
Matt Spinlerc39e8592017-09-28 13:13:08 -050089 if (_trustManager->active())
90 {
91 if (!_trustManager->checkTrust(sensor))
92 {
93 return;
94 }
95 }
96
Matt Spinler6fa181c2017-09-27 16:24:45 -050097 auto running = sensor.timerRunning();
Matt Spinlera4c8f1f2017-04-27 14:38:38 -050098
99 //If this sensor is out of range at this moment, start
100 //its timer, at the end of which the inventory
101 //for the fan may get updated to not functional.
102
103 //If this sensor is OK, put everything back into a good state.
104
105 if (outOfRange(sensor))
106 {
107 if (sensor.functional() && !running)
108 {
Matt Spinler6fa181c2017-09-27 16:24:45 -0500109 sensor.startTimer();
Matt Spinlera4c8f1f2017-04-27 14:38:38 -0500110 }
111 }
112 else
113 {
114 if (!sensor.functional())
115 {
116 sensor.setFunctional(true);
117 }
118
119 if (running)
120 {
Matt Spinler6fa181c2017-09-27 16:24:45 -0500121 sensor.stopTimer();
Matt Spinlera4c8f1f2017-04-27 14:38:38 -0500122 }
123
124 //If the fan was nonfunctional and enough sensors are now OK,
125 //the fan can go back to functional
126 if (!_functional && !tooManySensorsNonfunctional())
127 {
128 log<level::INFO>("Setting a fan back to functional",
129 entry("FAN=%s", _name.c_str()));
130
Matt Spinlerb1e18512017-04-27 14:42:33 -0500131 updateInventory(true);
Matt Spinlera4c8f1f2017-04-27 14:38:38 -0500132 }
133 }
Matt Spinlerebaae612017-04-27 14:21:48 -0500134}
135
136
Matthew Barthf552ea52018-01-15 16:22:04 -0600137uint64_t Fan::findTargetSpeed()
Matt Spinlerabf8da32017-04-27 14:08:45 -0500138{
139 uint64_t target = 0;
Matthew Barthf552ea52018-01-15 16:22:04 -0600140 //The sensor doesn't support a target,
141 //so get it from another sensor.
142 auto s = std::find_if(_sensors.begin(), _sensors.end(),
143 [](const auto& s)
144 {
145 return s->hasTarget();
146 });
Matt Spinlerabf8da32017-04-27 14:08:45 -0500147
Matthew Barthf552ea52018-01-15 16:22:04 -0600148 if (s != _sensors.end())
Matt Spinlerabf8da32017-04-27 14:08:45 -0500149 {
Matthew Barthf552ea52018-01-15 16:22:04 -0600150 target = (*s)->getTarget();
Matt Spinlerabf8da32017-04-27 14:08:45 -0500151 }
152
153 return target;
154}
155
156
Matthew Barthf552ea52018-01-15 16:22:04 -0600157uint64_t Fan::getTargetSpeed(const TachSensor& sensor)
158{
159 return sensor.getTarget();
160}
161
162
Matt Spinlerabf8da32017-04-27 14:08:45 -0500163bool 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());
178 auto target = getTargetSpeed(sensor);
179
180 uint64_t min = target * (100 - _deviation) / 100;
181 uint64_t max = target * (100 + _deviation) / 100;
182
183 if ((actual < min) || (actual > max))
184 {
185 return true;
186 }
187
188 return false;
189}
190
191
Matt Spinlera9406a72017-04-27 14:29:24 -0500192void Fan::timerExpired(TachSensor& sensor)
193{
194 sensor.setFunctional(false);
195
196 //If the fan is currently functional, but too many
197 //contained sensors are now nonfunctional, update
198 //the whole fan nonfunctional.
Matt Spinlerb1e18512017-04-27 14:42:33 -0500199
200 if (_functional && tooManySensorsNonfunctional())
201 {
202 log<level::ERR>("Setting a fan to nonfunctional",
Matt Spinlerce75b512017-07-26 15:10:48 -0500203 entry("FAN=%s", _name.c_str()),
204 entry("TACH_SENSOR=%s", sensor.name().c_str()),
205 entry("ACTUAL_SPEED=%lld", sensor.getInput()),
206 entry("TARGET_SPEED=%lld", getTargetSpeed(sensor)));
Matt Spinlerb1e18512017-04-27 14:42:33 -0500207
208 updateInventory(false);
209 }
Matt Spinlera9406a72017-04-27 14:29:24 -0500210}
211
Matt Spinlerb1e18512017-04-27 14:42:33 -0500212
213void Fan::updateInventory(bool functional)
214{
Matthew Barth51dd1852017-11-16 15:21:13 -0600215 auto objectMap = util::getObjMap<bool>(
216 _name,
217 util::OPERATIONAL_STATUS_INTF,
218 util::FUNCTIONAL_PROPERTY,
219 functional);
220 auto response = util::SDBusPlus::lookupAndCallMethod(
221 _bus,
222 util::INVENTORY_PATH,
223 util::INVENTORY_INTF,
224 "Notify",
225 objectMap);
Matt Spinlerb1e18512017-04-27 14:42:33 -0500226 if (response.is_method_error())
227 {
228 log<level::ERR>("Error in Notify call to update inventory");
229 return;
230 }
231
232 //This will always track the current state of the inventory.
233 _functional = functional;
234}
235
Matt Spinlerabf8da32017-04-27 14:08:45 -0500236}
237}
238}