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