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