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