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