Matt Spinler | abf8da3 | 2017-04-27 14:08:45 -0500 | [diff] [blame] | 1 | /** |
| 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 Spinler | b1e1851 | 2017-04-27 14:42:33 -0500 | [diff] [blame] | 20 | #include "utility.hpp" |
Matthew Barth | 51dd185 | 2017-11-16 15:21:13 -0600 | [diff] [blame] | 21 | #include "sdbusplus.hpp" |
Matt Spinler | abf8da3 | 2017-04-27 14:08:45 -0500 | [diff] [blame] | 22 | |
| 23 | namespace phosphor |
| 24 | { |
| 25 | namespace fan |
| 26 | { |
| 27 | namespace monitor |
| 28 | { |
| 29 | |
| 30 | using namespace phosphor::logging; |
| 31 | |
Matthew Barth | 6ad2843 | 2017-08-22 11:18:19 -0500 | [diff] [blame] | 32 | Fan::Fan(Mode mode, |
| 33 | sdbusplus::bus::bus& bus, |
Matt Spinler | e824f98 | 2017-05-11 10:07:55 -0500 | [diff] [blame] | 34 | phosphor::fan::event::EventPtr& events, |
Matt Spinler | c39e859 | 2017-09-28 13:13:08 -0500 | [diff] [blame] | 35 | std::unique_ptr<trust::Manager>& trust, |
Matt Spinler | abf8da3 | 2017-04-27 14:08:45 -0500 | [diff] [blame] | 36 | const FanDefinition& def) : |
| 37 | _bus(bus), |
| 38 | _name(std::get<fanNameField>(def)), |
| 39 | _deviation(std::get<fanDeviationField>(def)), |
Matt Spinler | c39e859 | 2017-09-28 13:13:08 -0500 | [diff] [blame] | 40 | _numSensorFailsForNonFunc(std::get<numSensorFailsForNonfuncField>(def)), |
| 41 | _trustManager(trust) |
Matt Spinler | abf8da3 | 2017-04-27 14:08:45 -0500 | [diff] [blame] | 42 | { |
Matthew Barth | 0a9fe16 | 2018-01-26 12:53:15 -0600 | [diff] [blame^] | 43 | // 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( |
| 50 | std::make_unique<TachSensor>( |
| 51 | mode, |
| 52 | bus, |
| 53 | *this, |
| 54 | std::get<sensorNameField>(s), |
| 55 | std::get<hasTargetField>(s), |
| 56 | std::get<timeoutField>(def), |
| 57 | events)); |
| 58 | |
| 59 | _trustManager->registerSensor(_sensors.back()); |
| 60 | } |
| 61 | catch (InvalidSensorError& e) |
| 62 | { |
| 63 | |
| 64 | } |
| 65 | } |
| 66 | |
Matt Spinler | b1e1851 | 2017-04-27 14:42:33 -0500 | [diff] [blame] | 67 | //Start from a known state of functional |
| 68 | updateInventory(true); |
| 69 | |
Matthew Barth | 0a9fe16 | 2018-01-26 12:53:15 -0600 | [diff] [blame^] | 70 | // Check current tach state when entering monitor mode |
Matthew Barth | 6ad2843 | 2017-08-22 11:18:19 -0500 | [diff] [blame] | 71 | if (mode != Mode::init) |
| 72 | { |
Matthew Barth | 6ad2843 | 2017-08-22 11:18:19 -0500 | [diff] [blame] | 73 | //The TachSensors will now have already read the input |
| 74 | //and target values, so check them. |
| 75 | tachChanged(); |
| 76 | } |
Matt Spinler | abf8da3 | 2017-04-27 14:08:45 -0500 | [diff] [blame] | 77 | } |
| 78 | |
| 79 | |
Matt Spinler | ebaae61 | 2017-04-27 14:21:48 -0500 | [diff] [blame] | 80 | void Fan::tachChanged() |
| 81 | { |
| 82 | for (auto& s : _sensors) |
| 83 | { |
| 84 | tachChanged(*s); |
| 85 | } |
| 86 | } |
| 87 | |
| 88 | |
| 89 | void Fan::tachChanged(TachSensor& sensor) |
| 90 | { |
Matt Spinler | c39e859 | 2017-09-28 13:13:08 -0500 | [diff] [blame] | 91 | if (_trustManager->active()) |
| 92 | { |
| 93 | if (!_trustManager->checkTrust(sensor)) |
| 94 | { |
| 95 | return; |
| 96 | } |
| 97 | } |
| 98 | |
Matt Spinler | 6fa181c | 2017-09-27 16:24:45 -0500 | [diff] [blame] | 99 | auto running = sensor.timerRunning(); |
Matt Spinler | a4c8f1f | 2017-04-27 14:38:38 -0500 | [diff] [blame] | 100 | |
| 101 | //If this sensor is out of range at this moment, start |
| 102 | //its timer, at the end of which the inventory |
| 103 | //for the fan may get updated to not functional. |
| 104 | |
| 105 | //If this sensor is OK, put everything back into a good state. |
| 106 | |
| 107 | if (outOfRange(sensor)) |
| 108 | { |
| 109 | if (sensor.functional() && !running) |
| 110 | { |
Matt Spinler | 6fa181c | 2017-09-27 16:24:45 -0500 | [diff] [blame] | 111 | sensor.startTimer(); |
Matt Spinler | a4c8f1f | 2017-04-27 14:38:38 -0500 | [diff] [blame] | 112 | } |
| 113 | } |
| 114 | else |
| 115 | { |
| 116 | if (!sensor.functional()) |
| 117 | { |
| 118 | sensor.setFunctional(true); |
| 119 | } |
| 120 | |
| 121 | if (running) |
| 122 | { |
Matt Spinler | 6fa181c | 2017-09-27 16:24:45 -0500 | [diff] [blame] | 123 | sensor.stopTimer(); |
Matt Spinler | a4c8f1f | 2017-04-27 14:38:38 -0500 | [diff] [blame] | 124 | } |
| 125 | |
| 126 | //If the fan was nonfunctional and enough sensors are now OK, |
| 127 | //the fan can go back to functional |
| 128 | if (!_functional && !tooManySensorsNonfunctional()) |
| 129 | { |
| 130 | log<level::INFO>("Setting a fan back to functional", |
| 131 | entry("FAN=%s", _name.c_str())); |
| 132 | |
Matt Spinler | b1e1851 | 2017-04-27 14:42:33 -0500 | [diff] [blame] | 133 | updateInventory(true); |
Matt Spinler | a4c8f1f | 2017-04-27 14:38:38 -0500 | [diff] [blame] | 134 | } |
| 135 | } |
Matt Spinler | ebaae61 | 2017-04-27 14:21:48 -0500 | [diff] [blame] | 136 | } |
| 137 | |
| 138 | |
Matthew Barth | f552ea5 | 2018-01-15 16:22:04 -0600 | [diff] [blame] | 139 | uint64_t Fan::findTargetSpeed() |
Matt Spinler | abf8da3 | 2017-04-27 14:08:45 -0500 | [diff] [blame] | 140 | { |
| 141 | uint64_t target = 0; |
Matthew Barth | f552ea5 | 2018-01-15 16:22:04 -0600 | [diff] [blame] | 142 | //The sensor doesn't support a target, |
| 143 | //so get it from another sensor. |
| 144 | auto s = std::find_if(_sensors.begin(), _sensors.end(), |
| 145 | [](const auto& s) |
| 146 | { |
| 147 | return s->hasTarget(); |
| 148 | }); |
Matt Spinler | abf8da3 | 2017-04-27 14:08:45 -0500 | [diff] [blame] | 149 | |
Matthew Barth | f552ea5 | 2018-01-15 16:22:04 -0600 | [diff] [blame] | 150 | if (s != _sensors.end()) |
Matt Spinler | abf8da3 | 2017-04-27 14:08:45 -0500 | [diff] [blame] | 151 | { |
Matthew Barth | f552ea5 | 2018-01-15 16:22:04 -0600 | [diff] [blame] | 152 | target = (*s)->getTarget(); |
Matt Spinler | abf8da3 | 2017-04-27 14:08:45 -0500 | [diff] [blame] | 153 | } |
| 154 | |
| 155 | return target; |
| 156 | } |
| 157 | |
| 158 | |
| 159 | bool Fan::tooManySensorsNonfunctional() |
| 160 | { |
| 161 | size_t numFailed = std::count_if(_sensors.begin(), _sensors.end(), |
| 162 | [](const auto& s) |
| 163 | { |
| 164 | return !s->functional(); |
| 165 | }); |
| 166 | |
| 167 | return (numFailed >= _numSensorFailsForNonFunc); |
| 168 | } |
| 169 | |
| 170 | |
| 171 | bool Fan::outOfRange(const TachSensor& sensor) |
| 172 | { |
| 173 | auto actual = static_cast<uint64_t>(sensor.getInput()); |
Matthew Barth | 5008daf | 2018-01-15 16:38:24 -0600 | [diff] [blame] | 174 | auto target = sensor.getTarget(); |
Matt Spinler | abf8da3 | 2017-04-27 14:08:45 -0500 | [diff] [blame] | 175 | |
| 176 | uint64_t min = target * (100 - _deviation) / 100; |
| 177 | uint64_t max = target * (100 + _deviation) / 100; |
| 178 | |
| 179 | if ((actual < min) || (actual > max)) |
| 180 | { |
| 181 | return true; |
| 182 | } |
| 183 | |
| 184 | return false; |
| 185 | } |
| 186 | |
| 187 | |
Matt Spinler | a9406a7 | 2017-04-27 14:29:24 -0500 | [diff] [blame] | 188 | void Fan::timerExpired(TachSensor& sensor) |
| 189 | { |
| 190 | sensor.setFunctional(false); |
| 191 | |
| 192 | //If the fan is currently functional, but too many |
| 193 | //contained sensors are now nonfunctional, update |
| 194 | //the whole fan nonfunctional. |
Matt Spinler | b1e1851 | 2017-04-27 14:42:33 -0500 | [diff] [blame] | 195 | |
| 196 | if (_functional && tooManySensorsNonfunctional()) |
| 197 | { |
| 198 | log<level::ERR>("Setting a fan to nonfunctional", |
Matt Spinler | ce75b51 | 2017-07-26 15:10:48 -0500 | [diff] [blame] | 199 | entry("FAN=%s", _name.c_str()), |
| 200 | entry("TACH_SENSOR=%s", sensor.name().c_str()), |
| 201 | entry("ACTUAL_SPEED=%lld", sensor.getInput()), |
Matthew Barth | 5008daf | 2018-01-15 16:38:24 -0600 | [diff] [blame] | 202 | entry("TARGET_SPEED=%lld", sensor.getTarget())); |
Matt Spinler | b1e1851 | 2017-04-27 14:42:33 -0500 | [diff] [blame] | 203 | |
| 204 | updateInventory(false); |
| 205 | } |
Matt Spinler | a9406a7 | 2017-04-27 14:29:24 -0500 | [diff] [blame] | 206 | } |
| 207 | |
Matt Spinler | b1e1851 | 2017-04-27 14:42:33 -0500 | [diff] [blame] | 208 | |
| 209 | void Fan::updateInventory(bool functional) |
| 210 | { |
Matthew Barth | 51dd185 | 2017-11-16 15:21:13 -0600 | [diff] [blame] | 211 | auto objectMap = util::getObjMap<bool>( |
| 212 | _name, |
| 213 | util::OPERATIONAL_STATUS_INTF, |
| 214 | util::FUNCTIONAL_PROPERTY, |
| 215 | functional); |
| 216 | auto response = util::SDBusPlus::lookupAndCallMethod( |
| 217 | _bus, |
| 218 | util::INVENTORY_PATH, |
| 219 | util::INVENTORY_INTF, |
| 220 | "Notify", |
| 221 | objectMap); |
Matt Spinler | b1e1851 | 2017-04-27 14:42:33 -0500 | [diff] [blame] | 222 | if (response.is_method_error()) |
| 223 | { |
| 224 | log<level::ERR>("Error in Notify call to update inventory"); |
| 225 | return; |
| 226 | } |
| 227 | |
| 228 | //This will always track the current state of the inventory. |
| 229 | _functional = functional; |
| 230 | } |
| 231 | |
Matt Spinler | abf8da3 | 2017-04-27 14:08:45 -0500 | [diff] [blame] | 232 | } |
| 233 | } |
| 234 | } |