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, |
Matt Spinler | 27f6b68 | 2020-10-27 08:43:37 -0500 | [diff] [blame] | 57 | std::placeholders::_1)), |
Matt Spinler | 7d13564 | 2021-02-04 12:44:17 -0600 | [diff] [blame] | 58 | _presenceIfaceAddedMatch( |
| 59 | bus, |
| 60 | rules::interfacesAdded() + |
| 61 | rules::argNpath(0, util::INVENTORY_PATH + _name), |
| 62 | std::bind(std::mem_fn(&Fan::presenceIfaceAdded), this, |
| 63 | std::placeholders::_1)), |
Matt Spinler | 623635c | 2021-03-29 13:13:59 -0500 | [diff] [blame] | 64 | _fanMissingErrorDelay(std::get<fanMissingErrDelayField>(def)), |
Matt Spinler | a3584bd | 2021-03-29 15:48:30 -0500 | [diff] [blame] | 65 | _setFuncOnPresent(std::get<funcOnPresentField>(def)) |
Matt Spinler | abf8da3 | 2017-04-27 14:08:45 -0500 | [diff] [blame] | 66 | { |
Matthew Barth | 0a9fe16 | 2018-01-26 12:53:15 -0600 | [diff] [blame] | 67 | // Setup tach sensors for monitoring |
| 68 | auto& sensors = std::get<sensorListField>(def); |
| 69 | for (auto& s : sensors) |
| 70 | { |
Matt Spinler | 4283c5d | 2021-03-01 15:56:00 -0600 | [diff] [blame] | 71 | _sensors.emplace_back(std::make_shared<TachSensor>( |
| 72 | mode, bus, *this, std::get<sensorNameField>(s), |
| 73 | std::get<hasTargetField>(s), std::get<funcDelay>(def), |
| 74 | std::get<targetInterfaceField>(s), std::get<factorField>(s), |
| 75 | std::get<offsetField>(s), std::get<methodField>(def), |
Matthew Barth | 8a8aa44 | 2021-11-19 14:13:13 -0600 | [diff] [blame^] | 76 | std::get<thresholdField>(s), std::get<ignoreAboveMaxField>(s), |
| 77 | std::get<timeoutField>(def), |
Matt Spinler | fdfcc67 | 2021-06-01 14:51:06 -0600 | [diff] [blame] | 78 | std::get<nonfuncRotorErrDelayField>(def), |
| 79 | std::get<countIntervalField>(def), event)); |
Matthew Barth | 0a9fe16 | 2018-01-26 12:53:15 -0600 | [diff] [blame] | 80 | |
Matt Spinler | 4283c5d | 2021-03-01 15:56:00 -0600 | [diff] [blame] | 81 | _trustManager->registerSensor(_sensors.back()); |
Matthew Barth | 0a9fe16 | 2018-01-26 12:53:15 -0600 | [diff] [blame] | 82 | } |
| 83 | |
Mike Capps | ce6820a | 2021-05-26 10:40:19 -0400 | [diff] [blame] | 84 | bool functionalState = |
| 85 | (_numSensorFailsForNonFunc == 0) || |
| 86 | (countNonFunctionalSensors() < _numSensorFailsForNonFunc); |
| 87 | |
Mike Capps | 9ff4877 | 2021-07-19 14:49:43 -0400 | [diff] [blame] | 88 | if (updateInventory(functionalState) && !functionalState) |
| 89 | { |
| 90 | // the inventory update threw an exception, possibly because D-Bus |
| 91 | // wasn't ready. Try to update sensors back to functional to avoid a |
| 92 | // false-alarm. They will be updated again from subscribing to the |
| 93 | // properties-changed event |
| 94 | |
| 95 | for (auto& sensor : _sensors) |
| 96 | sensor->setFunctional(true); |
| 97 | } |
Mike Capps | ce6820a | 2021-05-26 10:40:19 -0400 | [diff] [blame] | 98 | |
Matt Spinler | b0412d0 | 2020-10-12 16:53:52 -0500 | [diff] [blame] | 99 | #ifndef MONITOR_USE_JSON |
Matthew Barth | 0a9fe16 | 2018-01-26 12:53:15 -0600 | [diff] [blame] | 100 | // Check current tach state when entering monitor mode |
Matthew Barth | 6ad2843 | 2017-08-22 11:18:19 -0500 | [diff] [blame] | 101 | if (mode != Mode::init) |
| 102 | { |
Matt Spinler | b0412d0 | 2020-10-12 16:53:52 -0500 | [diff] [blame] | 103 | _monitorReady = true; |
| 104 | |
Matthew Barth | 177fe98 | 2020-05-26 11:05:19 -0500 | [diff] [blame] | 105 | // The TachSensors will now have already read the input |
| 106 | // and target values, so check them. |
Matthew Barth | 6ad2843 | 2017-08-22 11:18:19 -0500 | [diff] [blame] | 107 | tachChanged(); |
| 108 | } |
Matt Spinler | b0412d0 | 2020-10-12 16:53:52 -0500 | [diff] [blame] | 109 | #else |
Matt Spinler | 7d13564 | 2021-02-04 12:44:17 -0600 | [diff] [blame] | 110 | if (_system.isPowerOn()) |
| 111 | { |
| 112 | _monitorTimer.restartOnce(std::chrono::seconds(_monitorDelay)); |
| 113 | } |
Matt Spinler | b0412d0 | 2020-10-12 16:53:52 -0500 | [diff] [blame] | 114 | #endif |
Matt Spinler | b63aa09 | 2020-10-14 09:45:11 -0500 | [diff] [blame] | 115 | |
Matt Spinler | 27f6b68 | 2020-10-27 08:43:37 -0500 | [diff] [blame] | 116 | if (_fanMissingErrorDelay) |
| 117 | { |
| 118 | _fanMissingErrorTimer = std::make_unique< |
| 119 | sdeventplus::utility::Timer<sdeventplus::ClockId::Monotonic>>( |
| 120 | event, std::bind(&System::fanMissingErrorTimerExpired, &system, |
| 121 | std::ref(*this))); |
Matt Spinler | 7d13564 | 2021-02-04 12:44:17 -0600 | [diff] [blame] | 122 | } |
Matt Spinler | 27f6b68 | 2020-10-27 08:43:37 -0500 | [diff] [blame] | 123 | |
Matt Spinler | 7d13564 | 2021-02-04 12:44:17 -0600 | [diff] [blame] | 124 | try |
| 125 | { |
| 126 | _present = util::SDBusPlus::getProperty<bool>( |
| 127 | util::INVENTORY_PATH + _name, util::INV_ITEM_IFACE, "Present"); |
| 128 | |
| 129 | if (!_present) |
Matt Spinler | 27f6b68 | 2020-10-27 08:43:37 -0500 | [diff] [blame] | 130 | { |
Matt Spinler | ac37297 | 2021-01-25 15:11:22 -0600 | [diff] [blame] | 131 | getLogger().log( |
| 132 | fmt::format("On startup, fan {} is missing", _name)); |
Matt Spinler | 7d13564 | 2021-02-04 12:44:17 -0600 | [diff] [blame] | 133 | if (_system.isPowerOn() && _fanMissingErrorTimer) |
| 134 | { |
| 135 | _fanMissingErrorTimer->restartOnce( |
| 136 | std::chrono::seconds{*_fanMissingErrorDelay}); |
| 137 | } |
| 138 | } |
| 139 | } |
| 140 | catch (const util::DBusServiceError& e) |
| 141 | { |
| 142 | // This could happen on the first BMC boot if the presence |
| 143 | // detect app hasn't started yet and there isn't an inventory |
| 144 | // cache yet. |
| 145 | } |
| 146 | } |
| 147 | |
| 148 | void Fan::presenceIfaceAdded(sdbusplus::message::message& msg) |
| 149 | { |
| 150 | sdbusplus::message::object_path path; |
| 151 | std::map<std::string, std::map<std::string, std::variant<bool>>> interfaces; |
| 152 | |
| 153 | msg.read(path, interfaces); |
| 154 | |
| 155 | auto properties = interfaces.find(util::INV_ITEM_IFACE); |
| 156 | if (properties == interfaces.end()) |
| 157 | { |
| 158 | return; |
| 159 | } |
| 160 | |
| 161 | auto property = properties->second.find("Present"); |
| 162 | if (property == properties->second.end()) |
| 163 | { |
| 164 | return; |
| 165 | } |
| 166 | |
| 167 | _present = std::get<bool>(property->second); |
| 168 | |
| 169 | if (!_present) |
| 170 | { |
| 171 | getLogger().log(fmt::format( |
| 172 | "New fan {} interface added and fan is not present", _name)); |
| 173 | if (_system.isPowerOn() && _fanMissingErrorTimer) |
| 174 | { |
Matt Spinler | 27f6b68 | 2020-10-27 08:43:37 -0500 | [diff] [blame] | 175 | _fanMissingErrorTimer->restartOnce( |
| 176 | std::chrono::seconds{*_fanMissingErrorDelay}); |
| 177 | } |
| 178 | } |
Matt Spinler | 7d13564 | 2021-02-04 12:44:17 -0600 | [diff] [blame] | 179 | |
| 180 | _system.fanStatusChange(*this); |
Matt Spinler | b0412d0 | 2020-10-12 16:53:52 -0500 | [diff] [blame] | 181 | } |
| 182 | |
| 183 | void Fan::startMonitor() |
| 184 | { |
| 185 | _monitorReady = true; |
| 186 | |
Matt Spinler | 4283c5d | 2021-03-01 15:56:00 -0600 | [diff] [blame] | 187 | std::for_each(_sensors.begin(), _sensors.end(), [this](auto& sensor) { |
| 188 | if (_present) |
| 189 | { |
| 190 | try |
| 191 | { |
| 192 | // Force a getProperty call to check if the tach sensor is |
| 193 | // on D-Bus. If it isn't, now set it to nonfunctional. |
| 194 | // This isn't done earlier so that code watching for |
| 195 | // nonfunctional tach sensors doesn't take actions before |
| 196 | // those sensors show up on D-Bus. |
| 197 | sensor->updateTachAndTarget(); |
| 198 | tachChanged(*sensor); |
| 199 | } |
| 200 | catch (const util::DBusServiceError& e) |
| 201 | { |
| 202 | // The tach property still isn't on D-Bus, ensure |
| 203 | // sensor is nonfunctional. |
| 204 | getLogger().log(fmt::format( |
| 205 | "Monitoring starting but {} sensor value not on D-Bus", |
| 206 | sensor->name())); |
| 207 | |
| 208 | sensor->setFunctional(false); |
| 209 | |
| 210 | if (_numSensorFailsForNonFunc) |
| 211 | { |
| 212 | if (_functional && (countNonFunctionalSensors() >= |
| 213 | _numSensorFailsForNonFunc)) |
| 214 | { |
| 215 | updateInventory(false); |
| 216 | } |
| 217 | } |
| 218 | |
| 219 | _system.fanStatusChange(*this); |
| 220 | } |
| 221 | } |
| 222 | }); |
Matt Spinler | abf8da3 | 2017-04-27 14:08:45 -0500 | [diff] [blame] | 223 | } |
| 224 | |
Matt Spinler | ebaae61 | 2017-04-27 14:21:48 -0500 | [diff] [blame] | 225 | void Fan::tachChanged() |
| 226 | { |
Matt Spinler | b0412d0 | 2020-10-12 16:53:52 -0500 | [diff] [blame] | 227 | if (_monitorReady) |
Matt Spinler | ebaae61 | 2017-04-27 14:21:48 -0500 | [diff] [blame] | 228 | { |
Matt Spinler | b0412d0 | 2020-10-12 16:53:52 -0500 | [diff] [blame] | 229 | for (auto& s : _sensors) |
| 230 | { |
| 231 | tachChanged(*s); |
| 232 | } |
Matt Spinler | ebaae61 | 2017-04-27 14:21:48 -0500 | [diff] [blame] | 233 | } |
| 234 | } |
| 235 | |
Matt Spinler | ebaae61 | 2017-04-27 14:21:48 -0500 | [diff] [blame] | 236 | void Fan::tachChanged(TachSensor& sensor) |
| 237 | { |
Matt Spinler | 7d13564 | 2021-02-04 12:44:17 -0600 | [diff] [blame] | 238 | if (!_system.isPowerOn() || !_monitorReady) |
| 239 | { |
| 240 | return; |
| 241 | } |
| 242 | |
Matt Spinler | c39e859 | 2017-09-28 13:13:08 -0500 | [diff] [blame] | 243 | if (_trustManager->active()) |
| 244 | { |
| 245 | if (!_trustManager->checkTrust(sensor)) |
| 246 | { |
| 247 | return; |
| 248 | } |
| 249 | } |
| 250 | |
Matt Spinler | fdfcc67 | 2021-06-01 14:51:06 -0600 | [diff] [blame] | 251 | // If the error checking method is 'count', if a tach change leads |
| 252 | // to an out of range sensor the count timer will take over in calling |
| 253 | // process() until the sensor is healthy again. |
| 254 | if (!sensor.countTimerRunning()) |
Matt Spinler | 623635c | 2021-03-29 13:13:59 -0500 | [diff] [blame] | 255 | { |
| 256 | process(sensor); |
| 257 | } |
| 258 | } |
| 259 | |
Matt Spinler | fdfcc67 | 2021-06-01 14:51:06 -0600 | [diff] [blame] | 260 | void Fan::countTimerExpired(TachSensor& sensor) |
Matt Spinler | 623635c | 2021-03-29 13:13:59 -0500 | [diff] [blame] | 261 | { |
Matt Spinler | fdfcc67 | 2021-06-01 14:51:06 -0600 | [diff] [blame] | 262 | if (_trustManager->active() && !_trustManager->checkTrust(sensor)) |
Matt Spinler | 623635c | 2021-03-29 13:13:59 -0500 | [diff] [blame] | 263 | { |
Matt Spinler | fdfcc67 | 2021-06-01 14:51:06 -0600 | [diff] [blame] | 264 | return; |
Matt Spinler | 623635c | 2021-03-29 13:13:59 -0500 | [diff] [blame] | 265 | } |
Matt Spinler | fdfcc67 | 2021-06-01 14:51:06 -0600 | [diff] [blame] | 266 | process(sensor); |
Matthew Barth | fcb0dbc | 2021-02-10 14:23:39 -0600 | [diff] [blame] | 267 | } |
| 268 | |
| 269 | void Fan::process(TachSensor& sensor) |
| 270 | { |
Matthew Barth | 177fe98 | 2020-05-26 11:05:19 -0500 | [diff] [blame] | 271 | // If this sensor is out of range at this moment, start |
| 272 | // its timer, at the end of which the inventory |
| 273 | // for the fan may get updated to not functional. |
Matt Spinler | a4c8f1f | 2017-04-27 14:38:38 -0500 | [diff] [blame] | 274 | |
Matthew Barth | 177fe98 | 2020-05-26 11:05:19 -0500 | [diff] [blame] | 275 | // If this sensor is OK, put everything back into a good state. |
Matt Spinler | a4c8f1f | 2017-04-27 14:38:38 -0500 | [diff] [blame] | 276 | |
| 277 | if (outOfRange(sensor)) |
| 278 | { |
Matthew Barth | e11cbc6 | 2018-02-20 12:11:07 -0600 | [diff] [blame] | 279 | if (sensor.functional()) |
Matt Spinler | a4c8f1f | 2017-04-27 14:38:38 -0500 | [diff] [blame] | 280 | { |
Jolie Ku | 69f2f48 | 2020-10-21 09:59:43 +0800 | [diff] [blame] | 281 | switch (sensor.getMethod()) |
| 282 | { |
| 283 | case MethodMode::timebased: |
| 284 | // Start nonfunctional timer if not already running |
| 285 | sensor.startTimer(TimerMode::nonfunc); |
| 286 | break; |
| 287 | case MethodMode::count: |
Matt Spinler | fdfcc67 | 2021-06-01 14:51:06 -0600 | [diff] [blame] | 288 | |
| 289 | if (!sensor.countTimerRunning()) |
| 290 | { |
| 291 | sensor.startCountTimer(); |
| 292 | } |
Jolie Ku | 69f2f48 | 2020-10-21 09:59:43 +0800 | [diff] [blame] | 293 | sensor.setCounter(true); |
| 294 | if (sensor.getCounter() >= sensor.getThreshold()) |
| 295 | { |
| 296 | updateState(sensor); |
| 297 | } |
| 298 | break; |
| 299 | } |
Matt Spinler | a4c8f1f | 2017-04-27 14:38:38 -0500 | [diff] [blame] | 300 | } |
| 301 | } |
| 302 | else |
| 303 | { |
Jolie Ku | 69f2f48 | 2020-10-21 09:59:43 +0800 | [diff] [blame] | 304 | switch (sensor.getMethod()) |
Matt Spinler | a4c8f1f | 2017-04-27 14:38:38 -0500 | [diff] [blame] | 305 | { |
Jolie Ku | 69f2f48 | 2020-10-21 09:59:43 +0800 | [diff] [blame] | 306 | case MethodMode::timebased: |
| 307 | if (sensor.functional()) |
| 308 | { |
Matthew Barth | 11b5d8f | 2021-01-28 14:04:09 -0600 | [diff] [blame] | 309 | if (sensor.timerRunning()) |
| 310 | { |
| 311 | sensor.stopTimer(); |
| 312 | } |
Jolie Ku | 69f2f48 | 2020-10-21 09:59:43 +0800 | [diff] [blame] | 313 | } |
| 314 | else |
| 315 | { |
| 316 | // Start functional timer if not already running |
| 317 | sensor.startTimer(TimerMode::func); |
| 318 | } |
| 319 | break; |
| 320 | case MethodMode::count: |
| 321 | sensor.setCounter(false); |
Matt Spinler | fdfcc67 | 2021-06-01 14:51:06 -0600 | [diff] [blame] | 322 | if (sensor.getCounter() == 0) |
Jolie Ku | 69f2f48 | 2020-10-21 09:59:43 +0800 | [diff] [blame] | 323 | { |
Matt Spinler | fdfcc67 | 2021-06-01 14:51:06 -0600 | [diff] [blame] | 324 | if (!sensor.functional()) |
| 325 | { |
| 326 | updateState(sensor); |
| 327 | } |
| 328 | |
| 329 | sensor.stopCountTimer(); |
Jolie Ku | 69f2f48 | 2020-10-21 09:59:43 +0800 | [diff] [blame] | 330 | } |
| 331 | break; |
Matt Spinler | a4c8f1f | 2017-04-27 14:38:38 -0500 | [diff] [blame] | 332 | } |
| 333 | } |
Matt Spinler | ebaae61 | 2017-04-27 14:21:48 -0500 | [diff] [blame] | 334 | } |
| 335 | |
Matthew Barth | f552ea5 | 2018-01-15 16:22:04 -0600 | [diff] [blame] | 336 | uint64_t Fan::findTargetSpeed() |
Matt Spinler | abf8da3 | 2017-04-27 14:08:45 -0500 | [diff] [blame] | 337 | { |
| 338 | uint64_t target = 0; |
Matthew Barth | 177fe98 | 2020-05-26 11:05:19 -0500 | [diff] [blame] | 339 | // The sensor doesn't support a target, |
| 340 | // so get it from another sensor. |
Matthew Barth | f552ea5 | 2018-01-15 16:22:04 -0600 | [diff] [blame] | 341 | auto s = std::find_if(_sensors.begin(), _sensors.end(), |
Matthew Barth | 177fe98 | 2020-05-26 11:05:19 -0500 | [diff] [blame] | 342 | [](const auto& s) { return s->hasTarget(); }); |
Matt Spinler | abf8da3 | 2017-04-27 14:08:45 -0500 | [diff] [blame] | 343 | |
Matthew Barth | f552ea5 | 2018-01-15 16:22:04 -0600 | [diff] [blame] | 344 | if (s != _sensors.end()) |
Matt Spinler | abf8da3 | 2017-04-27 14:08:45 -0500 | [diff] [blame] | 345 | { |
Matthew Barth | f552ea5 | 2018-01-15 16:22:04 -0600 | [diff] [blame] | 346 | target = (*s)->getTarget(); |
Matt Spinler | abf8da3 | 2017-04-27 14:08:45 -0500 | [diff] [blame] | 347 | } |
| 348 | |
| 349 | return target; |
| 350 | } |
| 351 | |
Mike Capps | ce6820a | 2021-05-26 10:40:19 -0400 | [diff] [blame] | 352 | size_t Fan::countNonFunctionalSensors() const |
Matt Spinler | abf8da3 | 2017-04-27 14:08:45 -0500 | [diff] [blame] | 353 | { |
Matthew Barth | 7c23a04 | 2021-01-26 16:21:45 -0600 | [diff] [blame] | 354 | return std::count_if(_sensors.begin(), _sensors.end(), |
| 355 | [](const auto& s) { return !s->functional(); }); |
Matt Spinler | abf8da3 | 2017-04-27 14:08:45 -0500 | [diff] [blame] | 356 | } |
| 357 | |
Matt Spinler | abf8da3 | 2017-04-27 14:08:45 -0500 | [diff] [blame] | 358 | bool Fan::outOfRange(const TachSensor& sensor) |
| 359 | { |
Mike Capps | fdcd5db | 2021-05-20 12:47:10 -0400 | [diff] [blame] | 360 | if (!sensor.hasOwner()) |
Matt Spinler | abf8da3 | 2017-04-27 14:08:45 -0500 | [diff] [blame] | 361 | { |
| 362 | return true; |
| 363 | } |
| 364 | |
Mike Capps | fdcd5db | 2021-05-20 12:47:10 -0400 | [diff] [blame] | 365 | auto actual = static_cast<uint64_t>(sensor.getInput()); |
| 366 | auto range = sensor.getRange(_deviation); |
| 367 | |
Matthew Barth | 8a8aa44 | 2021-11-19 14:13:13 -0600 | [diff] [blame^] | 368 | return ((actual < range.first) || |
| 369 | (range.second && actual > range.second.value())); |
Matt Spinler | abf8da3 | 2017-04-27 14:08:45 -0500 | [diff] [blame] | 370 | } |
| 371 | |
Jolie Ku | 69f2f48 | 2020-10-21 09:59:43 +0800 | [diff] [blame] | 372 | void Fan::updateState(TachSensor& sensor) |
Matt Spinler | a9406a7 | 2017-04-27 14:29:24 -0500 | [diff] [blame] | 373 | { |
Matt Spinler | 7d13564 | 2021-02-04 12:44:17 -0600 | [diff] [blame] | 374 | if (!_system.isPowerOn()) |
| 375 | { |
| 376 | return; |
| 377 | } |
| 378 | |
Matthew Barth | 8a8aa44 | 2021-11-19 14:13:13 -0600 | [diff] [blame^] | 379 | auto range = sensor.getRange(_deviation); |
| 380 | std::string rangeMax = "NoMax"; |
| 381 | if (range.second) |
| 382 | { |
| 383 | rangeMax = std::to_string(range.second.value()); |
| 384 | } |
| 385 | |
Matthew Barth | e11cbc6 | 2018-02-20 12:11:07 -0600 | [diff] [blame] | 386 | sensor.setFunctional(!sensor.functional()); |
Matt Spinler | ae1f8ef | 2020-10-14 16:15:51 -0500 | [diff] [blame] | 387 | getLogger().log( |
| 388 | fmt::format("Setting tach sensor {} functional state to {}. " |
Matthew Barth | 7c23a04 | 2021-01-26 16:21:45 -0600 | [diff] [blame] | 389 | "[target = {}, input = {}, allowed range = ({} - {})]", |
| 390 | sensor.name(), sensor.functional(), sensor.getTarget(), |
Matthew Barth | 8a8aa44 | 2021-11-19 14:13:13 -0600 | [diff] [blame^] | 391 | sensor.getInput(), range.first, rangeMax)); |
Matt Spinler | ae1f8ef | 2020-10-14 16:15:51 -0500 | [diff] [blame] | 392 | |
| 393 | // A zero value for _numSensorFailsForNonFunc means we aren't dealing |
| 394 | // with fan FRU functional status, only sensor functional status. |
| 395 | if (_numSensorFailsForNonFunc) |
Matthew Barth | e11cbc6 | 2018-02-20 12:11:07 -0600 | [diff] [blame] | 396 | { |
Matthew Barth | 7c23a04 | 2021-01-26 16:21:45 -0600 | [diff] [blame] | 397 | auto numNonFuncSensors = countNonFunctionalSensors(); |
Matt Spinler | ae1f8ef | 2020-10-14 16:15:51 -0500 | [diff] [blame] | 398 | // If the fan was nonfunctional and enough sensors are now OK, |
Matthew Barth | cceffdd | 2021-05-20 12:17:21 -0500 | [diff] [blame] | 399 | // the fan can be set to functional as long as `set_func_on_present` was |
| 400 | // not set |
| 401 | if (!_setFuncOnPresent && !_functional && |
| 402 | !(numNonFuncSensors >= _numSensorFailsForNonFunc)) |
Matt Spinler | ae1f8ef | 2020-10-14 16:15:51 -0500 | [diff] [blame] | 403 | { |
Matthew Barth | 7c23a04 | 2021-01-26 16:21:45 -0600 | [diff] [blame] | 404 | getLogger().log(fmt::format("Setting fan {} to functional, number " |
| 405 | "of nonfunctional sensors = {}", |
| 406 | _name, numNonFuncSensors)); |
Matt Spinler | ae1f8ef | 2020-10-14 16:15:51 -0500 | [diff] [blame] | 407 | updateInventory(true); |
| 408 | } |
Matt Spinler | a9406a7 | 2017-04-27 14:29:24 -0500 | [diff] [blame] | 409 | |
Matt Spinler | ae1f8ef | 2020-10-14 16:15:51 -0500 | [diff] [blame] | 410 | // If the fan is currently functional, but too many |
| 411 | // contained sensors are now nonfunctional, update |
Matthew Barth | 7c23a04 | 2021-01-26 16:21:45 -0600 | [diff] [blame] | 412 | // the fan to nonfunctional. |
| 413 | if (_functional && (numNonFuncSensors >= _numSensorFailsForNonFunc)) |
Matt Spinler | ae1f8ef | 2020-10-14 16:15:51 -0500 | [diff] [blame] | 414 | { |
Matthew Barth | 7c23a04 | 2021-01-26 16:21:45 -0600 | [diff] [blame] | 415 | getLogger().log(fmt::format("Setting fan {} to nonfunctional, " |
| 416 | "number of nonfunctional sensors = {}", |
| 417 | _name, numNonFuncSensors)); |
Matt Spinler | ae1f8ef | 2020-10-14 16:15:51 -0500 | [diff] [blame] | 418 | updateInventory(false); |
| 419 | } |
Matt Spinler | b1e1851 | 2017-04-27 14:42:33 -0500 | [diff] [blame] | 420 | } |
Matt Spinler | b63aa09 | 2020-10-14 09:45:11 -0500 | [diff] [blame] | 421 | |
| 422 | _system.fanStatusChange(*this); |
Matt Spinler | a9406a7 | 2017-04-27 14:29:24 -0500 | [diff] [blame] | 423 | } |
| 424 | |
Mike Capps | 9ff4877 | 2021-07-19 14:49:43 -0400 | [diff] [blame] | 425 | bool Fan::updateInventory(bool functional) |
Matt Spinler | b1e1851 | 2017-04-27 14:42:33 -0500 | [diff] [blame] | 426 | { |
Mike Capps | 9ff4877 | 2021-07-19 14:49:43 -0400 | [diff] [blame] | 427 | bool dbusError = false; |
| 428 | |
| 429 | try |
Matt Spinler | b1e1851 | 2017-04-27 14:42:33 -0500 | [diff] [blame] | 430 | { |
Mike Capps | 9ff4877 | 2021-07-19 14:49:43 -0400 | [diff] [blame] | 431 | auto objectMap = |
| 432 | util::getObjMap<bool>(_name, util::OPERATIONAL_STATUS_INTF, |
| 433 | util::FUNCTIONAL_PROPERTY, functional); |
| 434 | |
| 435 | auto response = util::SDBusPlus::lookupAndCallMethod( |
| 436 | _bus, util::INVENTORY_PATH, util::INVENTORY_INTF, "Notify", |
| 437 | objectMap); |
| 438 | |
| 439 | if (response.is_method_error()) |
| 440 | { |
| 441 | log<level::ERR>("Error in Notify call to update inventory"); |
| 442 | |
| 443 | dbusError = true; |
| 444 | } |
| 445 | } |
| 446 | catch (const util::DBusError& e) |
| 447 | { |
| 448 | dbusError = true; |
| 449 | |
| 450 | getLogger().log( |
| 451 | fmt::format("D-Bus Exception reading/updating inventory : {}", |
| 452 | e.what()), |
| 453 | Logger::error); |
Matt Spinler | b1e1851 | 2017-04-27 14:42:33 -0500 | [diff] [blame] | 454 | } |
| 455 | |
Matthew Barth | 177fe98 | 2020-05-26 11:05:19 -0500 | [diff] [blame] | 456 | // This will always track the current state of the inventory. |
Matt Spinler | b1e1851 | 2017-04-27 14:42:33 -0500 | [diff] [blame] | 457 | _functional = functional; |
Mike Capps | 9ff4877 | 2021-07-19 14:49:43 -0400 | [diff] [blame] | 458 | |
| 459 | return dbusError; |
Matt Spinler | b1e1851 | 2017-04-27 14:42:33 -0500 | [diff] [blame] | 460 | } |
| 461 | |
Matt Spinler | b63aa09 | 2020-10-14 09:45:11 -0500 | [diff] [blame] | 462 | void Fan::presenceChanged(sdbusplus::message::message& msg) |
| 463 | { |
| 464 | std::string interface; |
| 465 | std::map<std::string, std::variant<bool>> properties; |
| 466 | |
| 467 | msg.read(interface, properties); |
| 468 | |
| 469 | auto presentProp = properties.find("Present"); |
| 470 | if (presentProp != properties.end()) |
| 471 | { |
| 472 | _present = std::get<bool>(presentProp->second); |
| 473 | |
Matt Spinler | 27f6b68 | 2020-10-27 08:43:37 -0500 | [diff] [blame] | 474 | getLogger().log( |
Matt Spinler | ac37297 | 2021-01-25 15:11:22 -0600 | [diff] [blame] | 475 | fmt::format("Fan {} presence state change to {}", _name, _present)); |
Matt Spinler | 27f6b68 | 2020-10-27 08:43:37 -0500 | [diff] [blame] | 476 | |
Matt Spinler | b63aa09 | 2020-10-14 09:45:11 -0500 | [diff] [blame] | 477 | _system.fanStatusChange(*this); |
Matt Spinler | 27f6b68 | 2020-10-27 08:43:37 -0500 | [diff] [blame] | 478 | |
Matt Spinler | a3584bd | 2021-03-29 15:48:30 -0500 | [diff] [blame] | 479 | if (_present && _setFuncOnPresent) |
| 480 | { |
| 481 | updateInventory(true); |
| 482 | std::for_each(_sensors.begin(), _sensors.end(), [](auto& sensor) { |
| 483 | sensor->setFunctional(true); |
| 484 | sensor->resetMethod(); |
| 485 | }); |
| 486 | } |
| 487 | |
Matt Spinler | 27f6b68 | 2020-10-27 08:43:37 -0500 | [diff] [blame] | 488 | if (_fanMissingErrorDelay) |
| 489 | { |
Matt Spinler | 7d13564 | 2021-02-04 12:44:17 -0600 | [diff] [blame] | 490 | if (!_present && _system.isPowerOn()) |
Matt Spinler | 27f6b68 | 2020-10-27 08:43:37 -0500 | [diff] [blame] | 491 | { |
| 492 | _fanMissingErrorTimer->restartOnce( |
| 493 | std::chrono::seconds{*_fanMissingErrorDelay}); |
| 494 | } |
Matt Spinler | 7d13564 | 2021-02-04 12:44:17 -0600 | [diff] [blame] | 495 | else if (_present && _fanMissingErrorTimer->isEnabled()) |
Matt Spinler | 27f6b68 | 2020-10-27 08:43:37 -0500 | [diff] [blame] | 496 | { |
| 497 | _fanMissingErrorTimer->setEnabled(false); |
| 498 | } |
| 499 | } |
Matt Spinler | b63aa09 | 2020-10-14 09:45:11 -0500 | [diff] [blame] | 500 | } |
| 501 | } |
Matt Spinler | f13b42e | 2020-10-26 15:29:49 -0500 | [diff] [blame] | 502 | |
| 503 | void Fan::sensorErrorTimerExpired(const TachSensor& sensor) |
| 504 | { |
Matt Spinler | 7d13564 | 2021-02-04 12:44:17 -0600 | [diff] [blame] | 505 | if (_present && _system.isPowerOn()) |
Matt Spinler | f13b42e | 2020-10-26 15:29:49 -0500 | [diff] [blame] | 506 | { |
| 507 | _system.sensorErrorTimerExpired(*this, sensor); |
| 508 | } |
| 509 | } |
| 510 | |
Matt Spinler | 7d13564 | 2021-02-04 12:44:17 -0600 | [diff] [blame] | 511 | void Fan::powerStateChanged(bool powerStateOn) |
| 512 | { |
| 513 | #ifdef MONITOR_USE_JSON |
| 514 | if (powerStateOn) |
| 515 | { |
Matt Spinler | 7d13564 | 2021-02-04 12:44:17 -0600 | [diff] [blame] | 516 | _monitorTimer.restartOnce(std::chrono::seconds(_monitorDelay)); |
| 517 | |
Matt Spinler | bb449c1 | 2021-06-14 11:45:28 -0600 | [diff] [blame] | 518 | _numSensorsOnDBusAtPowerOn = 0; |
| 519 | |
| 520 | std::for_each(_sensors.begin(), _sensors.end(), [this](auto& sensor) { |
| 521 | try |
| 522 | { |
| 523 | // Force a getProperty call. If sensor is on D-Bus, |
| 524 | // then make sure it's functional. |
| 525 | sensor->updateTachAndTarget(); |
| 526 | |
| 527 | _numSensorsOnDBusAtPowerOn++; |
| 528 | |
| 529 | if (_present) |
| 530 | { |
| 531 | // If not functional, set it back to functional. |
| 532 | if (!sensor->functional()) |
| 533 | { |
| 534 | sensor->setFunctional(true); |
| 535 | _system.fanStatusChange(*this, true); |
| 536 | } |
| 537 | |
| 538 | // Set the counters back to zero |
| 539 | if (sensor->getMethod() == MethodMode::count) |
| 540 | { |
| 541 | sensor->resetMethod(); |
| 542 | } |
| 543 | } |
| 544 | } |
| 545 | catch (const util::DBusError& e) |
| 546 | { |
| 547 | // Properties still aren't on D-Bus. Let startMonitor() |
| 548 | // deal with it, or maybe System::powerStateChanged() if |
| 549 | // there aren't any sensors at all on D-Bus. |
| 550 | getLogger().log(fmt::format( |
| 551 | "At power on, tach sensor {} value not on D-Bus", |
| 552 | sensor->name())); |
| 553 | } |
| 554 | }); |
| 555 | |
Matt Spinler | 4283c5d | 2021-03-01 15:56:00 -0600 | [diff] [blame] | 556 | if (_present) |
| 557 | { |
Matt Spinler | 4283c5d | 2021-03-01 15:56:00 -0600 | [diff] [blame] | 558 | // If configured to change functional state on the fan itself, |
| 559 | // Set it back to true now if necessary. |
| 560 | if (_numSensorFailsForNonFunc) |
| 561 | { |
| 562 | if (!_functional && |
| 563 | (countNonFunctionalSensors() < _numSensorFailsForNonFunc)) |
| 564 | { |
| 565 | updateInventory(true); |
| 566 | } |
| 567 | } |
| 568 | } |
| 569 | else |
Matt Spinler | 7d13564 | 2021-02-04 12:44:17 -0600 | [diff] [blame] | 570 | { |
| 571 | getLogger().log( |
| 572 | fmt::format("At power on, fan {} is missing", _name)); |
| 573 | |
| 574 | if (_fanMissingErrorTimer) |
| 575 | { |
| 576 | _fanMissingErrorTimer->restartOnce( |
| 577 | std::chrono::seconds{*_fanMissingErrorDelay}); |
| 578 | } |
| 579 | } |
| 580 | } |
| 581 | else |
| 582 | { |
| 583 | _monitorReady = false; |
| 584 | |
| 585 | if (_monitorTimer.isEnabled()) |
| 586 | { |
| 587 | _monitorTimer.setEnabled(false); |
| 588 | } |
| 589 | |
| 590 | if (_fanMissingErrorTimer && _fanMissingErrorTimer->isEnabled()) |
| 591 | { |
| 592 | _fanMissingErrorTimer->setEnabled(false); |
| 593 | } |
| 594 | |
| 595 | std::for_each(_sensors.begin(), _sensors.end(), [](auto& sensor) { |
| 596 | if (sensor->timerRunning()) |
| 597 | { |
| 598 | sensor->stopTimer(); |
| 599 | } |
Matt Spinler | 623635c | 2021-03-29 13:13:59 -0500 | [diff] [blame] | 600 | |
Matt Spinler | fdfcc67 | 2021-06-01 14:51:06 -0600 | [diff] [blame] | 601 | sensor->stopCountTimer(); |
| 602 | }); |
Matt Spinler | 7d13564 | 2021-02-04 12:44:17 -0600 | [diff] [blame] | 603 | } |
| 604 | #endif |
| 605 | } |
| 606 | |
Matthew Barth | 177fe98 | 2020-05-26 11:05:19 -0500 | [diff] [blame] | 607 | } // namespace monitor |
| 608 | } // namespace fan |
| 609 | } // namespace phosphor |