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 | |
Anwaar Hadi | a00f683 | 2025-03-27 15:03:11 +0000 | [diff] [blame] | 24 | #include <phosphor-logging/lg2.hpp> |
Matthew Barth | 177fe98 | 2020-05-26 11:05:19 -0500 | [diff] [blame] | 25 | |
Patrick Williams | fbf4703 | 2023-07-17 12:27:34 -0500 | [diff] [blame] | 26 | #include <format> |
| 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 | |
Matt Spinler | b0412d0 | 2020-10-12 16:53:52 -0500 | [diff] [blame] | 35 | using namespace sdbusplus::bus::match; |
Matt Spinler | abf8da3 | 2017-04-27 14:08:45 -0500 | [diff] [blame] | 36 | |
Patrick Williams | cb356d4 | 2022-07-22 19:26:53 -0500 | [diff] [blame] | 37 | Fan::Fan(Mode mode, sdbusplus::bus_t& bus, const sdeventplus::Event& event, |
Matt Spinler | b0412d0 | 2020-10-12 16:53:52 -0500 | [diff] [blame] | 38 | std::unique_ptr<trust::Manager>& trust, const FanDefinition& def, |
| 39 | System& system) : |
Patrick Williams | dfddd64 | 2024-08-16 15:21:51 -0400 | [diff] [blame] | 40 | _bus(bus), _name(def.name), _deviation(def.deviation), |
Matt Spinler | f724c16 | 2023-05-10 11:14:37 -0500 | [diff] [blame] | 41 | _upperDeviation(def.upperDeviation), |
Matt Spinler | 18fb12b | 2023-05-09 11:17:42 -0500 | [diff] [blame] | 42 | _numSensorFailsForNonFunc(def.numSensorFailsForNonfunc), |
Matt Spinler | b0412d0 | 2020-10-12 16:53:52 -0500 | [diff] [blame] | 43 | _trustManager(trust), |
| 44 | #ifdef MONITOR_USE_JSON |
Matt Spinler | 18fb12b | 2023-05-09 11:17:42 -0500 | [diff] [blame] | 45 | _monitorDelay(def.monitorStartDelay), |
Matt Spinler | b0412d0 | 2020-10-12 16:53:52 -0500 | [diff] [blame] | 46 | _monitorTimer(event, std::bind(std::mem_fn(&Fan::startMonitor), this)), |
| 47 | #endif |
Matt Spinler | b63aa09 | 2020-10-14 09:45:11 -0500 | [diff] [blame] | 48 | _system(system), |
| 49 | _presenceMatch(bus, |
| 50 | rules::propertiesChanged(util::INVENTORY_PATH + _name, |
| 51 | util::INV_ITEM_IFACE), |
| 52 | std::bind(std::mem_fn(&Fan::presenceChanged), this, |
Matt Spinler | 27f6b68 | 2020-10-27 08:43:37 -0500 | [diff] [blame] | 53 | std::placeholders::_1)), |
Matt Spinler | 7d13564 | 2021-02-04 12:44:17 -0600 | [diff] [blame] | 54 | _presenceIfaceAddedMatch( |
| 55 | bus, |
| 56 | rules::interfacesAdded() + |
| 57 | rules::argNpath(0, util::INVENTORY_PATH + _name), |
| 58 | std::bind(std::mem_fn(&Fan::presenceIfaceAdded), this, |
| 59 | std::placeholders::_1)), |
Matt Spinler | 18fb12b | 2023-05-09 11:17:42 -0500 | [diff] [blame] | 60 | _fanMissingErrorDelay(def.fanMissingErrDelay), |
| 61 | _setFuncOnPresent(def.funcOnPresent) |
Matt Spinler | abf8da3 | 2017-04-27 14:08:45 -0500 | [diff] [blame] | 62 | { |
Matthew Barth | 0a9fe16 | 2018-01-26 12:53:15 -0600 | [diff] [blame] | 63 | // Setup tach sensors for monitoring |
Matt Spinler | 18fb12b | 2023-05-09 11:17:42 -0500 | [diff] [blame] | 64 | for (const auto& s : def.sensorList) |
Matthew Barth | 0a9fe16 | 2018-01-26 12:53:15 -0600 | [diff] [blame] | 65 | { |
Matt Spinler | 4283c5d | 2021-03-01 15:56:00 -0600 | [diff] [blame] | 66 | _sensors.emplace_back(std::make_shared<TachSensor>( |
Matt Spinler | 18fb12b | 2023-05-09 11:17:42 -0500 | [diff] [blame] | 67 | mode, bus, *this, s.name, s.hasTarget, def.funcDelay, |
| 68 | s.targetInterface, s.targetPath, s.factor, s.offset, def.method, |
| 69 | s.threshold, s.ignoreAboveMax, def.timeout, |
| 70 | def.nonfuncRotorErrDelay, def.countInterval, event)); |
Matthew Barth | 0a9fe16 | 2018-01-26 12:53:15 -0600 | [diff] [blame] | 71 | |
Matt Spinler | 4283c5d | 2021-03-01 15:56:00 -0600 | [diff] [blame] | 72 | _trustManager->registerSensor(_sensors.back()); |
Matthew Barth | 0a9fe16 | 2018-01-26 12:53:15 -0600 | [diff] [blame] | 73 | } |
| 74 | |
Mike Capps | ce6820a | 2021-05-26 10:40:19 -0400 | [diff] [blame] | 75 | bool functionalState = |
| 76 | (_numSensorFailsForNonFunc == 0) || |
| 77 | (countNonFunctionalSensors() < _numSensorFailsForNonFunc); |
| 78 | |
Mike Capps | 9ff4877 | 2021-07-19 14:49:43 -0400 | [diff] [blame] | 79 | if (updateInventory(functionalState) && !functionalState) |
| 80 | { |
| 81 | // the inventory update threw an exception, possibly because D-Bus |
| 82 | // wasn't ready. Try to update sensors back to functional to avoid a |
| 83 | // false-alarm. They will be updated again from subscribing to the |
| 84 | // properties-changed event |
| 85 | |
| 86 | for (auto& sensor : _sensors) |
| 87 | sensor->setFunctional(true); |
| 88 | } |
Mike Capps | ce6820a | 2021-05-26 10:40:19 -0400 | [diff] [blame] | 89 | |
Matt Spinler | b0412d0 | 2020-10-12 16:53:52 -0500 | [diff] [blame] | 90 | #ifndef MONITOR_USE_JSON |
Matthew Barth | 0a9fe16 | 2018-01-26 12:53:15 -0600 | [diff] [blame] | 91 | // Check current tach state when entering monitor mode |
Matthew Barth | 6ad2843 | 2017-08-22 11:18:19 -0500 | [diff] [blame] | 92 | if (mode != Mode::init) |
| 93 | { |
Matt Spinler | b0412d0 | 2020-10-12 16:53:52 -0500 | [diff] [blame] | 94 | _monitorReady = true; |
| 95 | |
Matthew Barth | 177fe98 | 2020-05-26 11:05:19 -0500 | [diff] [blame] | 96 | // The TachSensors will now have already read the input |
| 97 | // and target values, so check them. |
Matthew Barth | 6ad2843 | 2017-08-22 11:18:19 -0500 | [diff] [blame] | 98 | tachChanged(); |
| 99 | } |
Matt Spinler | b0412d0 | 2020-10-12 16:53:52 -0500 | [diff] [blame] | 100 | #else |
Matt Spinler | 7d13564 | 2021-02-04 12:44:17 -0600 | [diff] [blame] | 101 | if (_system.isPowerOn()) |
| 102 | { |
| 103 | _monitorTimer.restartOnce(std::chrono::seconds(_monitorDelay)); |
| 104 | } |
Matt Spinler | b0412d0 | 2020-10-12 16:53:52 -0500 | [diff] [blame] | 105 | #endif |
Matt Spinler | b63aa09 | 2020-10-14 09:45:11 -0500 | [diff] [blame] | 106 | |
Matt Spinler | 27f6b68 | 2020-10-27 08:43:37 -0500 | [diff] [blame] | 107 | if (_fanMissingErrorDelay) |
| 108 | { |
| 109 | _fanMissingErrorTimer = std::make_unique< |
| 110 | sdeventplus::utility::Timer<sdeventplus::ClockId::Monotonic>>( |
| 111 | event, std::bind(&System::fanMissingErrorTimerExpired, &system, |
| 112 | std::ref(*this))); |
Matt Spinler | 7d13564 | 2021-02-04 12:44:17 -0600 | [diff] [blame] | 113 | } |
Matt Spinler | 27f6b68 | 2020-10-27 08:43:37 -0500 | [diff] [blame] | 114 | |
Matt Spinler | 7d13564 | 2021-02-04 12:44:17 -0600 | [diff] [blame] | 115 | try |
| 116 | { |
| 117 | _present = util::SDBusPlus::getProperty<bool>( |
| 118 | util::INVENTORY_PATH + _name, util::INV_ITEM_IFACE, "Present"); |
| 119 | |
| 120 | if (!_present) |
Matt Spinler | 27f6b68 | 2020-10-27 08:43:37 -0500 | [diff] [blame] | 121 | { |
Matt Spinler | ac37297 | 2021-01-25 15:11:22 -0600 | [diff] [blame] | 122 | getLogger().log( |
Patrick Williams | fbf4703 | 2023-07-17 12:27:34 -0500 | [diff] [blame] | 123 | std::format("On startup, fan {} is missing", _name)); |
Matt Spinler | 7d13564 | 2021-02-04 12:44:17 -0600 | [diff] [blame] | 124 | if (_system.isPowerOn() && _fanMissingErrorTimer) |
| 125 | { |
| 126 | _fanMissingErrorTimer->restartOnce( |
| 127 | std::chrono::seconds{*_fanMissingErrorDelay}); |
| 128 | } |
| 129 | } |
| 130 | } |
| 131 | catch (const util::DBusServiceError& e) |
| 132 | { |
| 133 | // This could happen on the first BMC boot if the presence |
| 134 | // detect app hasn't started yet and there isn't an inventory |
| 135 | // cache yet. |
| 136 | } |
| 137 | } |
| 138 | |
Patrick Williams | cb356d4 | 2022-07-22 19:26:53 -0500 | [diff] [blame] | 139 | void Fan::presenceIfaceAdded(sdbusplus::message_t& msg) |
Matt Spinler | 7d13564 | 2021-02-04 12:44:17 -0600 | [diff] [blame] | 140 | { |
| 141 | sdbusplus::message::object_path path; |
| 142 | std::map<std::string, std::map<std::string, std::variant<bool>>> interfaces; |
| 143 | |
| 144 | msg.read(path, interfaces); |
| 145 | |
| 146 | auto properties = interfaces.find(util::INV_ITEM_IFACE); |
| 147 | if (properties == interfaces.end()) |
| 148 | { |
| 149 | return; |
| 150 | } |
| 151 | |
| 152 | auto property = properties->second.find("Present"); |
| 153 | if (property == properties->second.end()) |
| 154 | { |
| 155 | return; |
| 156 | } |
| 157 | |
| 158 | _present = std::get<bool>(property->second); |
| 159 | |
| 160 | if (!_present) |
| 161 | { |
Patrick Williams | fbf4703 | 2023-07-17 12:27:34 -0500 | [diff] [blame] | 162 | getLogger().log(std::format( |
Matt Spinler | 7d13564 | 2021-02-04 12:44:17 -0600 | [diff] [blame] | 163 | "New fan {} interface added and fan is not present", _name)); |
| 164 | if (_system.isPowerOn() && _fanMissingErrorTimer) |
| 165 | { |
Matt Spinler | 27f6b68 | 2020-10-27 08:43:37 -0500 | [diff] [blame] | 166 | _fanMissingErrorTimer->restartOnce( |
| 167 | std::chrono::seconds{*_fanMissingErrorDelay}); |
| 168 | } |
| 169 | } |
Matt Spinler | 7d13564 | 2021-02-04 12:44:17 -0600 | [diff] [blame] | 170 | |
| 171 | _system.fanStatusChange(*this); |
Matt Spinler | b0412d0 | 2020-10-12 16:53:52 -0500 | [diff] [blame] | 172 | } |
| 173 | |
| 174 | void Fan::startMonitor() |
| 175 | { |
| 176 | _monitorReady = true; |
| 177 | |
Matt Spinler | 4283c5d | 2021-03-01 15:56:00 -0600 | [diff] [blame] | 178 | std::for_each(_sensors.begin(), _sensors.end(), [this](auto& sensor) { |
Matt Spinler | 3494a57 | 2023-11-28 12:45:32 -0600 | [diff] [blame] | 179 | try |
Matt Spinler | 4283c5d | 2021-03-01 15:56:00 -0600 | [diff] [blame] | 180 | { |
Matt Spinler | 3494a57 | 2023-11-28 12:45:32 -0600 | [diff] [blame] | 181 | // Force a getProperty call to check if the tach sensor is |
| 182 | // on D-Bus. If it isn't, now set it to nonfunctional. |
| 183 | // This isn't done earlier so that code watching for |
| 184 | // nonfunctional tach sensors doesn't take actions before |
| 185 | // those sensors show up on D-Bus. |
| 186 | sensor->updateTachAndTarget(); |
| 187 | tachChanged(*sensor); |
| 188 | } |
| 189 | catch (const util::DBusServiceError& e) |
| 190 | { |
| 191 | // The tach property still isn't on D-Bus. Ensure |
| 192 | // sensor is nonfunctional, but skip creating an |
| 193 | // error for it since it isn't a fan problem. |
| 194 | getLogger().log(std::format( |
| 195 | "Monitoring starting but {} sensor value not on D-Bus", |
| 196 | sensor->name())); |
Matt Spinler | 4283c5d | 2021-03-01 15:56:00 -0600 | [diff] [blame] | 197 | |
Matt Spinler | 3494a57 | 2023-11-28 12:45:32 -0600 | [diff] [blame] | 198 | sensor->setFunctional(false, true); |
Matt Spinler | 4283c5d | 2021-03-01 15:56:00 -0600 | [diff] [blame] | 199 | |
Matt Spinler | 3494a57 | 2023-11-28 12:45:32 -0600 | [diff] [blame] | 200 | if (_numSensorFailsForNonFunc) |
| 201 | { |
| 202 | if (_functional && |
| 203 | (countNonFunctionalSensors() >= _numSensorFailsForNonFunc)) |
Matt Spinler | 4283c5d | 2021-03-01 15:56:00 -0600 | [diff] [blame] | 204 | { |
Matt Spinler | 3494a57 | 2023-11-28 12:45:32 -0600 | [diff] [blame] | 205 | updateInventory(false); |
Matt Spinler | 4283c5d | 2021-03-01 15:56:00 -0600 | [diff] [blame] | 206 | } |
Matt Spinler | 4283c5d | 2021-03-01 15:56:00 -0600 | [diff] [blame] | 207 | } |
Matt Spinler | 3494a57 | 2023-11-28 12:45:32 -0600 | [diff] [blame] | 208 | |
| 209 | // At this point, don't start any power off actions due |
| 210 | // to missing sensors. Let something else handle that |
| 211 | // policy. |
| 212 | _system.fanStatusChange(*this, true); |
Matt Spinler | 4283c5d | 2021-03-01 15:56:00 -0600 | [diff] [blame] | 213 | } |
| 214 | }); |
Matt Spinler | abf8da3 | 2017-04-27 14:08:45 -0500 | [diff] [blame] | 215 | } |
| 216 | |
Matt Spinler | ebaae61 | 2017-04-27 14:21:48 -0500 | [diff] [blame] | 217 | void Fan::tachChanged() |
| 218 | { |
Matt Spinler | b0412d0 | 2020-10-12 16:53:52 -0500 | [diff] [blame] | 219 | if (_monitorReady) |
Matt Spinler | ebaae61 | 2017-04-27 14:21:48 -0500 | [diff] [blame] | 220 | { |
Matt Spinler | b0412d0 | 2020-10-12 16:53:52 -0500 | [diff] [blame] | 221 | for (auto& s : _sensors) |
| 222 | { |
| 223 | tachChanged(*s); |
| 224 | } |
Matt Spinler | ebaae61 | 2017-04-27 14:21:48 -0500 | [diff] [blame] | 225 | } |
| 226 | } |
| 227 | |
Matt Spinler | ebaae61 | 2017-04-27 14:21:48 -0500 | [diff] [blame] | 228 | void Fan::tachChanged(TachSensor& sensor) |
| 229 | { |
Matt Spinler | 7d13564 | 2021-02-04 12:44:17 -0600 | [diff] [blame] | 230 | if (!_system.isPowerOn() || !_monitorReady) |
| 231 | { |
| 232 | return; |
| 233 | } |
| 234 | |
Matt Spinler | c39e859 | 2017-09-28 13:13:08 -0500 | [diff] [blame] | 235 | if (_trustManager->active()) |
| 236 | { |
| 237 | if (!_trustManager->checkTrust(sensor)) |
| 238 | { |
| 239 | return; |
| 240 | } |
| 241 | } |
| 242 | |
Matt Spinler | fdfcc67 | 2021-06-01 14:51:06 -0600 | [diff] [blame] | 243 | // If the error checking method is 'count', if a tach change leads |
| 244 | // to an out of range sensor the count timer will take over in calling |
| 245 | // process() until the sensor is healthy again. |
| 246 | if (!sensor.countTimerRunning()) |
Matt Spinler | 623635c | 2021-03-29 13:13:59 -0500 | [diff] [blame] | 247 | { |
| 248 | process(sensor); |
| 249 | } |
| 250 | } |
| 251 | |
Matt Spinler | fdfcc67 | 2021-06-01 14:51:06 -0600 | [diff] [blame] | 252 | void Fan::countTimerExpired(TachSensor& sensor) |
Matt Spinler | 623635c | 2021-03-29 13:13:59 -0500 | [diff] [blame] | 253 | { |
Matt Spinler | fdfcc67 | 2021-06-01 14:51:06 -0600 | [diff] [blame] | 254 | if (_trustManager->active() && !_trustManager->checkTrust(sensor)) |
Matt Spinler | 623635c | 2021-03-29 13:13:59 -0500 | [diff] [blame] | 255 | { |
Matt Spinler | fdfcc67 | 2021-06-01 14:51:06 -0600 | [diff] [blame] | 256 | return; |
Matt Spinler | 623635c | 2021-03-29 13:13:59 -0500 | [diff] [blame] | 257 | } |
Matt Spinler | fdfcc67 | 2021-06-01 14:51:06 -0600 | [diff] [blame] | 258 | process(sensor); |
Matthew Barth | fcb0dbc | 2021-02-10 14:23:39 -0600 | [diff] [blame] | 259 | } |
| 260 | |
| 261 | void Fan::process(TachSensor& sensor) |
| 262 | { |
Matthew Barth | 177fe98 | 2020-05-26 11:05:19 -0500 | [diff] [blame] | 263 | // If this sensor is out of range at this moment, start |
| 264 | // its timer, at the end of which the inventory |
| 265 | // for the fan may get updated to not functional. |
Matt Spinler | a4c8f1f | 2017-04-27 14:38:38 -0500 | [diff] [blame] | 266 | |
Matthew Barth | 177fe98 | 2020-05-26 11:05:19 -0500 | [diff] [blame] | 267 | // If this sensor is OK, put everything back into a good state. |
Matt Spinler | a4c8f1f | 2017-04-27 14:38:38 -0500 | [diff] [blame] | 268 | |
| 269 | if (outOfRange(sensor)) |
| 270 | { |
Matthew Barth | e11cbc6 | 2018-02-20 12:11:07 -0600 | [diff] [blame] | 271 | if (sensor.functional()) |
Matt Spinler | a4c8f1f | 2017-04-27 14:38:38 -0500 | [diff] [blame] | 272 | { |
Jolie Ku | 69f2f48 | 2020-10-21 09:59:43 +0800 | [diff] [blame] | 273 | switch (sensor.getMethod()) |
| 274 | { |
| 275 | case MethodMode::timebased: |
| 276 | // Start nonfunctional timer if not already running |
| 277 | sensor.startTimer(TimerMode::nonfunc); |
| 278 | break; |
| 279 | case MethodMode::count: |
Matt Spinler | fdfcc67 | 2021-06-01 14:51:06 -0600 | [diff] [blame] | 280 | |
| 281 | if (!sensor.countTimerRunning()) |
| 282 | { |
| 283 | sensor.startCountTimer(); |
| 284 | } |
Jolie Ku | 69f2f48 | 2020-10-21 09:59:43 +0800 | [diff] [blame] | 285 | sensor.setCounter(true); |
| 286 | if (sensor.getCounter() >= sensor.getThreshold()) |
| 287 | { |
| 288 | updateState(sensor); |
| 289 | } |
| 290 | break; |
| 291 | } |
Matt Spinler | a4c8f1f | 2017-04-27 14:38:38 -0500 | [diff] [blame] | 292 | } |
| 293 | } |
| 294 | else |
| 295 | { |
Jolie Ku | 69f2f48 | 2020-10-21 09:59:43 +0800 | [diff] [blame] | 296 | switch (sensor.getMethod()) |
Matt Spinler | a4c8f1f | 2017-04-27 14:38:38 -0500 | [diff] [blame] | 297 | { |
Jolie Ku | 69f2f48 | 2020-10-21 09:59:43 +0800 | [diff] [blame] | 298 | case MethodMode::timebased: |
| 299 | if (sensor.functional()) |
| 300 | { |
Matthew Barth | 11b5d8f | 2021-01-28 14:04:09 -0600 | [diff] [blame] | 301 | if (sensor.timerRunning()) |
| 302 | { |
| 303 | sensor.stopTimer(); |
| 304 | } |
Jolie Ku | 69f2f48 | 2020-10-21 09:59:43 +0800 | [diff] [blame] | 305 | } |
| 306 | else |
| 307 | { |
| 308 | // Start functional timer if not already running |
| 309 | sensor.startTimer(TimerMode::func); |
| 310 | } |
| 311 | break; |
| 312 | case MethodMode::count: |
| 313 | sensor.setCounter(false); |
Matt Spinler | fdfcc67 | 2021-06-01 14:51:06 -0600 | [diff] [blame] | 314 | if (sensor.getCounter() == 0) |
Jolie Ku | 69f2f48 | 2020-10-21 09:59:43 +0800 | [diff] [blame] | 315 | { |
Matt Spinler | fdfcc67 | 2021-06-01 14:51:06 -0600 | [diff] [blame] | 316 | if (!sensor.functional()) |
| 317 | { |
| 318 | updateState(sensor); |
| 319 | } |
| 320 | |
| 321 | sensor.stopCountTimer(); |
Jolie Ku | 69f2f48 | 2020-10-21 09:59:43 +0800 | [diff] [blame] | 322 | } |
| 323 | break; |
Matt Spinler | a4c8f1f | 2017-04-27 14:38:38 -0500 | [diff] [blame] | 324 | } |
| 325 | } |
Matt Spinler | ebaae61 | 2017-04-27 14:21:48 -0500 | [diff] [blame] | 326 | } |
| 327 | |
Matthew Barth | f552ea5 | 2018-01-15 16:22:04 -0600 | [diff] [blame] | 328 | uint64_t Fan::findTargetSpeed() |
Matt Spinler | abf8da3 | 2017-04-27 14:08:45 -0500 | [diff] [blame] | 329 | { |
| 330 | uint64_t target = 0; |
Matthew Barth | 177fe98 | 2020-05-26 11:05:19 -0500 | [diff] [blame] | 331 | // The sensor doesn't support a target, |
| 332 | // so get it from another sensor. |
Patrick Williams | dfddd64 | 2024-08-16 15:21:51 -0400 | [diff] [blame] | 333 | auto s = std::find_if(_sensors.begin(), _sensors.end(), [](const auto& s) { |
| 334 | return s->hasTarget(); |
| 335 | }); |
Matt Spinler | abf8da3 | 2017-04-27 14:08:45 -0500 | [diff] [blame] | 336 | |
Matthew Barth | f552ea5 | 2018-01-15 16:22:04 -0600 | [diff] [blame] | 337 | if (s != _sensors.end()) |
Matt Spinler | abf8da3 | 2017-04-27 14:08:45 -0500 | [diff] [blame] | 338 | { |
Matthew Barth | f552ea5 | 2018-01-15 16:22:04 -0600 | [diff] [blame] | 339 | target = (*s)->getTarget(); |
Matt Spinler | abf8da3 | 2017-04-27 14:08:45 -0500 | [diff] [blame] | 340 | } |
| 341 | |
| 342 | return target; |
| 343 | } |
| 344 | |
Mike Capps | ce6820a | 2021-05-26 10:40:19 -0400 | [diff] [blame] | 345 | size_t Fan::countNonFunctionalSensors() const |
Matt Spinler | abf8da3 | 2017-04-27 14:08:45 -0500 | [diff] [blame] | 346 | { |
Patrick Williams | dfddd64 | 2024-08-16 15:21:51 -0400 | [diff] [blame] | 347 | return std::count_if(_sensors.begin(), _sensors.end(), [](const auto& s) { |
| 348 | return !s->functional(); |
| 349 | }); |
Matt Spinler | abf8da3 | 2017-04-27 14:08:45 -0500 | [diff] [blame] | 350 | } |
| 351 | |
Matt Spinler | abf8da3 | 2017-04-27 14:08:45 -0500 | [diff] [blame] | 352 | bool Fan::outOfRange(const TachSensor& sensor) |
| 353 | { |
Mike Capps | fdcd5db | 2021-05-20 12:47:10 -0400 | [diff] [blame] | 354 | if (!sensor.hasOwner()) |
Matt Spinler | abf8da3 | 2017-04-27 14:08:45 -0500 | [diff] [blame] | 355 | { |
| 356 | return true; |
| 357 | } |
| 358 | |
Mike Capps | fdcd5db | 2021-05-20 12:47:10 -0400 | [diff] [blame] | 359 | auto actual = static_cast<uint64_t>(sensor.getInput()); |
Matt Spinler | f724c16 | 2023-05-10 11:14:37 -0500 | [diff] [blame] | 360 | auto range = sensor.getRange(_deviation, _upperDeviation); |
Mike Capps | fdcd5db | 2021-05-20 12:47:10 -0400 | [diff] [blame] | 361 | |
Matthew Barth | 8a8aa44 | 2021-11-19 14:13:13 -0600 | [diff] [blame] | 362 | return ((actual < range.first) || |
| 363 | (range.second && actual > range.second.value())); |
Matt Spinler | abf8da3 | 2017-04-27 14:08:45 -0500 | [diff] [blame] | 364 | } |
| 365 | |
Jolie Ku | 69f2f48 | 2020-10-21 09:59:43 +0800 | [diff] [blame] | 366 | void Fan::updateState(TachSensor& sensor) |
Matt Spinler | a9406a7 | 2017-04-27 14:29:24 -0500 | [diff] [blame] | 367 | { |
Matt Spinler | 7d13564 | 2021-02-04 12:44:17 -0600 | [diff] [blame] | 368 | if (!_system.isPowerOn()) |
| 369 | { |
| 370 | return; |
| 371 | } |
| 372 | |
Matt Spinler | f724c16 | 2023-05-10 11:14:37 -0500 | [diff] [blame] | 373 | auto range = sensor.getRange(_deviation, _upperDeviation); |
Matthew Barth | 8a8aa44 | 2021-11-19 14:13:13 -0600 | [diff] [blame] | 374 | std::string rangeMax = "NoMax"; |
| 375 | if (range.second) |
| 376 | { |
| 377 | rangeMax = std::to_string(range.second.value()); |
| 378 | } |
| 379 | |
Matt Spinler | ae01b5f | 2022-07-06 16:49:04 -0500 | [diff] [blame] | 380 | // Skip starting the error timer if the sensor |
| 381 | // isn't on D-Bus as this isn't a fan hardware problem. |
| 382 | sensor.setFunctional(!sensor.functional(), !sensor.hasOwner()); |
| 383 | |
Patrick Williams | fbf4703 | 2023-07-17 12:27:34 -0500 | [diff] [blame] | 384 | getLogger().log(std::format( |
Matt Spinler | ae01b5f | 2022-07-06 16:49:04 -0500 | [diff] [blame] | 385 | "Setting tach sensor {} functional state to {}. " |
Matt Spinler | 466bd22 | 2023-01-25 16:15:52 -0600 | [diff] [blame] | 386 | "[target = {}, actual = {}, allowed range = ({} - {}) " |
Matt Spinler | ae01b5f | 2022-07-06 16:49:04 -0500 | [diff] [blame] | 387 | "owned = {}]", |
| 388 | sensor.name(), sensor.functional(), sensor.getTarget(), |
| 389 | sensor.getInput(), range.first, rangeMax, sensor.hasOwner())); |
Matt Spinler | ae1f8ef | 2020-10-14 16:15:51 -0500 | [diff] [blame] | 390 | |
| 391 | // A zero value for _numSensorFailsForNonFunc means we aren't dealing |
| 392 | // with fan FRU functional status, only sensor functional status. |
| 393 | if (_numSensorFailsForNonFunc) |
Matthew Barth | e11cbc6 | 2018-02-20 12:11:07 -0600 | [diff] [blame] | 394 | { |
Matthew Barth | 7c23a04 | 2021-01-26 16:21:45 -0600 | [diff] [blame] | 395 | auto numNonFuncSensors = countNonFunctionalSensors(); |
Matt Spinler | ae1f8ef | 2020-10-14 16:15:51 -0500 | [diff] [blame] | 396 | // If the fan was nonfunctional and enough sensors are now OK, |
Matthew Barth | cceffdd | 2021-05-20 12:17:21 -0500 | [diff] [blame] | 397 | // the fan can be set to functional as long as `set_func_on_present` was |
| 398 | // not set |
| 399 | if (!_setFuncOnPresent && !_functional && |
| 400 | !(numNonFuncSensors >= _numSensorFailsForNonFunc)) |
Matt Spinler | ae1f8ef | 2020-10-14 16:15:51 -0500 | [diff] [blame] | 401 | { |
Patrick Williams | fbf4703 | 2023-07-17 12:27:34 -0500 | [diff] [blame] | 402 | getLogger().log(std::format("Setting fan {} to functional, number " |
Matthew Barth | 7c23a04 | 2021-01-26 16:21:45 -0600 | [diff] [blame] | 403 | "of nonfunctional sensors = {}", |
| 404 | _name, numNonFuncSensors)); |
Matt Spinler | ae1f8ef | 2020-10-14 16:15:51 -0500 | [diff] [blame] | 405 | updateInventory(true); |
| 406 | } |
Matt Spinler | a9406a7 | 2017-04-27 14:29:24 -0500 | [diff] [blame] | 407 | |
Matt Spinler | ae1f8ef | 2020-10-14 16:15:51 -0500 | [diff] [blame] | 408 | // If the fan is currently functional, but too many |
| 409 | // contained sensors are now nonfunctional, update |
Matthew Barth | 7c23a04 | 2021-01-26 16:21:45 -0600 | [diff] [blame] | 410 | // the fan to nonfunctional. |
| 411 | if (_functional && (numNonFuncSensors >= _numSensorFailsForNonFunc)) |
Matt Spinler | ae1f8ef | 2020-10-14 16:15:51 -0500 | [diff] [blame] | 412 | { |
Patrick Williams | fbf4703 | 2023-07-17 12:27:34 -0500 | [diff] [blame] | 413 | getLogger().log(std::format("Setting fan {} to nonfunctional, " |
Matthew Barth | 7c23a04 | 2021-01-26 16:21:45 -0600 | [diff] [blame] | 414 | "number of nonfunctional sensors = {}", |
| 415 | _name, numNonFuncSensors)); |
Matt Spinler | ae1f8ef | 2020-10-14 16:15:51 -0500 | [diff] [blame] | 416 | updateInventory(false); |
| 417 | } |
Matt Spinler | b1e1851 | 2017-04-27 14:42:33 -0500 | [diff] [blame] | 418 | } |
Matt Spinler | b63aa09 | 2020-10-14 09:45:11 -0500 | [diff] [blame] | 419 | |
Matt Spinler | ae01b5f | 2022-07-06 16:49:04 -0500 | [diff] [blame] | 420 | // Skip the power off rule checks if the sensor isn't |
| 421 | // on D-Bus so a running system isn't shutdown. |
| 422 | _system.fanStatusChange(*this, !sensor.hasOwner()); |
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 | |
Mike Capps | 8af8a62 | 2022-02-04 16:13:33 -0500 | [diff] [blame] | 435 | auto response = util::SDBusPlus::callMethod( |
| 436 | _bus, util::INVENTORY_SVC, util::INVENTORY_PATH, |
| 437 | util::INVENTORY_INTF, "Notify", objectMap); |
Mike Capps | 9ff4877 | 2021-07-19 14:49:43 -0400 | [diff] [blame] | 438 | |
| 439 | if (response.is_method_error()) |
| 440 | { |
Anwaar Hadi | a00f683 | 2025-03-27 15:03:11 +0000 | [diff] [blame] | 441 | lg2::error("Error in Notify call to update inventory"); |
Mike Capps | 9ff4877 | 2021-07-19 14:49:43 -0400 | [diff] [blame] | 442 | |
| 443 | dbusError = true; |
| 444 | } |
| 445 | } |
| 446 | catch (const util::DBusError& e) |
| 447 | { |
| 448 | dbusError = true; |
| 449 | |
| 450 | getLogger().log( |
Patrick Williams | fbf4703 | 2023-07-17 12:27:34 -0500 | [diff] [blame] | 451 | std::format("D-Bus Exception reading/updating inventory : {}", |
Mike Capps | 9ff4877 | 2021-07-19 14:49:43 -0400 | [diff] [blame] | 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 | |
Patrick Williams | cb356d4 | 2022-07-22 19:26:53 -0500 | [diff] [blame] | 462 | void Fan::presenceChanged(sdbusplus::message_t& msg) |
Matt Spinler | b63aa09 | 2020-10-14 09:45:11 -0500 | [diff] [blame] | 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( |
Patrick Williams | fbf4703 | 2023-07-17 12:27:34 -0500 | [diff] [blame] | 475 | std::format("Fan {} presence state change to {}", _name, _present)); |
Matt Spinler | 27f6b68 | 2020-10-27 08:43:37 -0500 | [diff] [blame] | 476 | |
Matt Spinler | a3584bd | 2021-03-29 15:48:30 -0500 | [diff] [blame] | 477 | if (_present && _setFuncOnPresent) |
| 478 | { |
| 479 | updateInventory(true); |
| 480 | std::for_each(_sensors.begin(), _sensors.end(), [](auto& sensor) { |
| 481 | sensor->setFunctional(true); |
| 482 | sensor->resetMethod(); |
| 483 | }); |
| 484 | } |
| 485 | |
Matthew Barth | 43b4cde | 2022-02-15 16:30:11 -0600 | [diff] [blame] | 486 | _system.fanStatusChange(*this); |
| 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 | |
Mike Capps | 808d7fe | 2022-06-13 10:12:16 -0400 | [diff] [blame] | 511 | void Fan::powerStateChanged([[maybe_unused]] bool powerStateOn) |
Matt Spinler | 7d13564 | 2021-02-04 12:44:17 -0600 | [diff] [blame] | 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. |
Patrick Williams | fbf4703 | 2023-07-17 12:27:34 -0500 | [diff] [blame] | 550 | getLogger().log(std::format( |
Matt Spinler | bb449c1 | 2021-06-14 11:45:28 -0600 | [diff] [blame] | 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( |
Patrick Williams | fbf4703 | 2023-07-17 12:27:34 -0500 | [diff] [blame] | 572 | std::format("At power on, fan {} is missing", _name)); |
Matt Spinler | 7d13564 | 2021-02-04 12:44:17 -0600 | [diff] [blame] | 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 |