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