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), |
| 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( |
| 124 | fmt::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 | { |
| 163 | getLogger().log(fmt::format( |
| 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) { |
| 180 | if (_present) |
| 181 | { |
| 182 | try |
| 183 | { |
| 184 | // Force a getProperty call to check if the tach sensor is |
| 185 | // on D-Bus. If it isn't, now set it to nonfunctional. |
| 186 | // This isn't done earlier so that code watching for |
| 187 | // nonfunctional tach sensors doesn't take actions before |
| 188 | // those sensors show up on D-Bus. |
| 189 | sensor->updateTachAndTarget(); |
| 190 | tachChanged(*sensor); |
| 191 | } |
| 192 | catch (const util::DBusServiceError& e) |
| 193 | { |
Matt Spinler | ae01b5f | 2022-07-06 16:49:04 -0500 | [diff] [blame] | 194 | // The tach property still isn't on D-Bus. Ensure |
| 195 | // sensor is nonfunctional, but skip creating an |
| 196 | // error for it since it isn't a fan problem. |
Matt Spinler | 4283c5d | 2021-03-01 15:56:00 -0600 | [diff] [blame] | 197 | getLogger().log(fmt::format( |
| 198 | "Monitoring starting but {} sensor value not on D-Bus", |
| 199 | sensor->name())); |
| 200 | |
Matt Spinler | ae01b5f | 2022-07-06 16:49:04 -0500 | [diff] [blame] | 201 | sensor->setFunctional(false, true); |
Matt Spinler | 4283c5d | 2021-03-01 15:56:00 -0600 | [diff] [blame] | 202 | |
| 203 | if (_numSensorFailsForNonFunc) |
| 204 | { |
| 205 | if (_functional && (countNonFunctionalSensors() >= |
| 206 | _numSensorFailsForNonFunc)) |
| 207 | { |
| 208 | updateInventory(false); |
| 209 | } |
| 210 | } |
| 211 | |
Matt Spinler | ae01b5f | 2022-07-06 16:49:04 -0500 | [diff] [blame] | 212 | // At this point, don't start any power off actions due |
| 213 | // to missing sensors. Let something else handle that |
| 214 | // policy. |
| 215 | _system.fanStatusChange(*this, true); |
Matt Spinler | 4283c5d | 2021-03-01 15:56:00 -0600 | [diff] [blame] | 216 | } |
| 217 | } |
| 218 | }); |
Matt Spinler | abf8da3 | 2017-04-27 14:08:45 -0500 | [diff] [blame] | 219 | } |
| 220 | |
Matt Spinler | ebaae61 | 2017-04-27 14:21:48 -0500 | [diff] [blame] | 221 | void Fan::tachChanged() |
| 222 | { |
Matt Spinler | b0412d0 | 2020-10-12 16:53:52 -0500 | [diff] [blame] | 223 | if (_monitorReady) |
Matt Spinler | ebaae61 | 2017-04-27 14:21:48 -0500 | [diff] [blame] | 224 | { |
Matt Spinler | b0412d0 | 2020-10-12 16:53:52 -0500 | [diff] [blame] | 225 | for (auto& s : _sensors) |
| 226 | { |
| 227 | tachChanged(*s); |
| 228 | } |
Matt Spinler | ebaae61 | 2017-04-27 14:21:48 -0500 | [diff] [blame] | 229 | } |
| 230 | } |
| 231 | |
Matt Spinler | ebaae61 | 2017-04-27 14:21:48 -0500 | [diff] [blame] | 232 | void Fan::tachChanged(TachSensor& sensor) |
| 233 | { |
Matt Spinler | 7d13564 | 2021-02-04 12:44:17 -0600 | [diff] [blame] | 234 | if (!_system.isPowerOn() || !_monitorReady) |
| 235 | { |
| 236 | return; |
| 237 | } |
| 238 | |
Matt Spinler | c39e859 | 2017-09-28 13:13:08 -0500 | [diff] [blame] | 239 | if (_trustManager->active()) |
| 240 | { |
| 241 | if (!_trustManager->checkTrust(sensor)) |
| 242 | { |
| 243 | return; |
| 244 | } |
| 245 | } |
| 246 | |
Matt Spinler | fdfcc67 | 2021-06-01 14:51:06 -0600 | [diff] [blame] | 247 | // If the error checking method is 'count', if a tach change leads |
| 248 | // to an out of range sensor the count timer will take over in calling |
| 249 | // process() until the sensor is healthy again. |
| 250 | if (!sensor.countTimerRunning()) |
Matt Spinler | 623635c | 2021-03-29 13:13:59 -0500 | [diff] [blame] | 251 | { |
| 252 | process(sensor); |
| 253 | } |
| 254 | } |
| 255 | |
Matt Spinler | fdfcc67 | 2021-06-01 14:51:06 -0600 | [diff] [blame] | 256 | void Fan::countTimerExpired(TachSensor& sensor) |
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 | if (_trustManager->active() && !_trustManager->checkTrust(sensor)) |
Matt Spinler | 623635c | 2021-03-29 13:13:59 -0500 | [diff] [blame] | 259 | { |
Matt Spinler | fdfcc67 | 2021-06-01 14:51:06 -0600 | [diff] [blame] | 260 | return; |
Matt Spinler | 623635c | 2021-03-29 13:13:59 -0500 | [diff] [blame] | 261 | } |
Matt Spinler | fdfcc67 | 2021-06-01 14:51:06 -0600 | [diff] [blame] | 262 | process(sensor); |
Matthew Barth | fcb0dbc | 2021-02-10 14:23:39 -0600 | [diff] [blame] | 263 | } |
| 264 | |
| 265 | void Fan::process(TachSensor& sensor) |
| 266 | { |
Matthew Barth | 177fe98 | 2020-05-26 11:05:19 -0500 | [diff] [blame] | 267 | // If this sensor is out of range at this moment, start |
| 268 | // its timer, at the end of which the inventory |
| 269 | // for the fan may get updated to not functional. |
Matt Spinler | a4c8f1f | 2017-04-27 14:38:38 -0500 | [diff] [blame] | 270 | |
Matthew Barth | 177fe98 | 2020-05-26 11:05:19 -0500 | [diff] [blame] | 271 | // If this sensor is OK, put everything back into a good state. |
Matt Spinler | a4c8f1f | 2017-04-27 14:38:38 -0500 | [diff] [blame] | 272 | |
| 273 | if (outOfRange(sensor)) |
| 274 | { |
Matthew Barth | e11cbc6 | 2018-02-20 12:11:07 -0600 | [diff] [blame] | 275 | if (sensor.functional()) |
Matt Spinler | a4c8f1f | 2017-04-27 14:38:38 -0500 | [diff] [blame] | 276 | { |
Jolie Ku | 69f2f48 | 2020-10-21 09:59:43 +0800 | [diff] [blame] | 277 | switch (sensor.getMethod()) |
| 278 | { |
| 279 | case MethodMode::timebased: |
| 280 | // Start nonfunctional timer if not already running |
| 281 | sensor.startTimer(TimerMode::nonfunc); |
| 282 | break; |
| 283 | case MethodMode::count: |
Matt Spinler | fdfcc67 | 2021-06-01 14:51:06 -0600 | [diff] [blame] | 284 | |
| 285 | if (!sensor.countTimerRunning()) |
| 286 | { |
| 287 | sensor.startCountTimer(); |
| 288 | } |
Jolie Ku | 69f2f48 | 2020-10-21 09:59:43 +0800 | [diff] [blame] | 289 | sensor.setCounter(true); |
| 290 | if (sensor.getCounter() >= sensor.getThreshold()) |
| 291 | { |
| 292 | updateState(sensor); |
| 293 | } |
| 294 | break; |
| 295 | } |
Matt Spinler | a4c8f1f | 2017-04-27 14:38:38 -0500 | [diff] [blame] | 296 | } |
| 297 | } |
| 298 | else |
| 299 | { |
Jolie Ku | 69f2f48 | 2020-10-21 09:59:43 +0800 | [diff] [blame] | 300 | switch (sensor.getMethod()) |
Matt Spinler | a4c8f1f | 2017-04-27 14:38:38 -0500 | [diff] [blame] | 301 | { |
Jolie Ku | 69f2f48 | 2020-10-21 09:59:43 +0800 | [diff] [blame] | 302 | case MethodMode::timebased: |
| 303 | if (sensor.functional()) |
| 304 | { |
Matthew Barth | 11b5d8f | 2021-01-28 14:04:09 -0600 | [diff] [blame] | 305 | if (sensor.timerRunning()) |
| 306 | { |
| 307 | sensor.stopTimer(); |
| 308 | } |
Jolie Ku | 69f2f48 | 2020-10-21 09:59:43 +0800 | [diff] [blame] | 309 | } |
| 310 | else |
| 311 | { |
| 312 | // Start functional timer if not already running |
| 313 | sensor.startTimer(TimerMode::func); |
| 314 | } |
| 315 | break; |
| 316 | case MethodMode::count: |
| 317 | sensor.setCounter(false); |
Matt Spinler | fdfcc67 | 2021-06-01 14:51:06 -0600 | [diff] [blame] | 318 | if (sensor.getCounter() == 0) |
Jolie Ku | 69f2f48 | 2020-10-21 09:59:43 +0800 | [diff] [blame] | 319 | { |
Matt Spinler | fdfcc67 | 2021-06-01 14:51:06 -0600 | [diff] [blame] | 320 | if (!sensor.functional()) |
| 321 | { |
| 322 | updateState(sensor); |
| 323 | } |
| 324 | |
| 325 | sensor.stopCountTimer(); |
Jolie Ku | 69f2f48 | 2020-10-21 09:59:43 +0800 | [diff] [blame] | 326 | } |
| 327 | break; |
Matt Spinler | a4c8f1f | 2017-04-27 14:38:38 -0500 | [diff] [blame] | 328 | } |
| 329 | } |
Matt Spinler | ebaae61 | 2017-04-27 14:21:48 -0500 | [diff] [blame] | 330 | } |
| 331 | |
Matthew Barth | f552ea5 | 2018-01-15 16:22:04 -0600 | [diff] [blame] | 332 | uint64_t Fan::findTargetSpeed() |
Matt Spinler | abf8da3 | 2017-04-27 14:08:45 -0500 | [diff] [blame] | 333 | { |
| 334 | uint64_t target = 0; |
Matthew Barth | 177fe98 | 2020-05-26 11:05:19 -0500 | [diff] [blame] | 335 | // The sensor doesn't support a target, |
| 336 | // so get it from another sensor. |
Matthew Barth | f552ea5 | 2018-01-15 16:22:04 -0600 | [diff] [blame] | 337 | auto s = std::find_if(_sensors.begin(), _sensors.end(), |
Matthew Barth | 177fe98 | 2020-05-26 11:05:19 -0500 | [diff] [blame] | 338 | [](const auto& s) { return s->hasTarget(); }); |
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 | if (s != _sensors.end()) |
Matt Spinler | abf8da3 | 2017-04-27 14:08:45 -0500 | [diff] [blame] | 341 | { |
Matthew Barth | f552ea5 | 2018-01-15 16:22:04 -0600 | [diff] [blame] | 342 | target = (*s)->getTarget(); |
Matt Spinler | abf8da3 | 2017-04-27 14:08:45 -0500 | [diff] [blame] | 343 | } |
| 344 | |
| 345 | return target; |
| 346 | } |
| 347 | |
Mike Capps | ce6820a | 2021-05-26 10:40:19 -0400 | [diff] [blame] | 348 | size_t Fan::countNonFunctionalSensors() const |
Matt Spinler | abf8da3 | 2017-04-27 14:08:45 -0500 | [diff] [blame] | 349 | { |
Matthew Barth | 7c23a04 | 2021-01-26 16:21:45 -0600 | [diff] [blame] | 350 | return std::count_if(_sensors.begin(), _sensors.end(), |
| 351 | [](const auto& s) { return !s->functional(); }); |
Matt Spinler | abf8da3 | 2017-04-27 14:08:45 -0500 | [diff] [blame] | 352 | } |
| 353 | |
Matt Spinler | abf8da3 | 2017-04-27 14:08:45 -0500 | [diff] [blame] | 354 | bool Fan::outOfRange(const TachSensor& sensor) |
| 355 | { |
Mike Capps | fdcd5db | 2021-05-20 12:47:10 -0400 | [diff] [blame] | 356 | if (!sensor.hasOwner()) |
Matt Spinler | abf8da3 | 2017-04-27 14:08:45 -0500 | [diff] [blame] | 357 | { |
| 358 | return true; |
| 359 | } |
| 360 | |
Mike Capps | fdcd5db | 2021-05-20 12:47:10 -0400 | [diff] [blame] | 361 | auto actual = static_cast<uint64_t>(sensor.getInput()); |
| 362 | auto range = sensor.getRange(_deviation); |
| 363 | |
Matthew Barth | 8a8aa44 | 2021-11-19 14:13:13 -0600 | [diff] [blame] | 364 | return ((actual < range.first) || |
| 365 | (range.second && actual > range.second.value())); |
Matt Spinler | abf8da3 | 2017-04-27 14:08:45 -0500 | [diff] [blame] | 366 | } |
| 367 | |
Jolie Ku | 69f2f48 | 2020-10-21 09:59:43 +0800 | [diff] [blame] | 368 | void Fan::updateState(TachSensor& sensor) |
Matt Spinler | a9406a7 | 2017-04-27 14:29:24 -0500 | [diff] [blame] | 369 | { |
Matt Spinler | 7d13564 | 2021-02-04 12:44:17 -0600 | [diff] [blame] | 370 | if (!_system.isPowerOn()) |
| 371 | { |
| 372 | return; |
| 373 | } |
| 374 | |
Matthew Barth | 8a8aa44 | 2021-11-19 14:13:13 -0600 | [diff] [blame] | 375 | auto range = sensor.getRange(_deviation); |
| 376 | std::string rangeMax = "NoMax"; |
| 377 | if (range.second) |
| 378 | { |
| 379 | rangeMax = std::to_string(range.second.value()); |
| 380 | } |
| 381 | |
Matt Spinler | ae01b5f | 2022-07-06 16:49:04 -0500 | [diff] [blame] | 382 | // Skip starting the error timer if the sensor |
| 383 | // isn't on D-Bus as this isn't a fan hardware problem. |
| 384 | sensor.setFunctional(!sensor.functional(), !sensor.hasOwner()); |
| 385 | |
| 386 | getLogger().log(fmt::format( |
| 387 | "Setting tach sensor {} functional state to {}. " |
Matt Spinler | 466bd22 | 2023-01-25 16:15:52 -0600 | [diff] [blame] | 388 | "[target = {}, actual = {}, allowed range = ({} - {}) " |
Matt Spinler | ae01b5f | 2022-07-06 16:49:04 -0500 | [diff] [blame] | 389 | "owned = {}]", |
| 390 | sensor.name(), sensor.functional(), sensor.getTarget(), |
| 391 | sensor.getInput(), range.first, rangeMax, sensor.hasOwner())); |
Matt Spinler | ae1f8ef | 2020-10-14 16:15:51 -0500 | [diff] [blame] | 392 | |
| 393 | // A zero value for _numSensorFailsForNonFunc means we aren't dealing |
| 394 | // with fan FRU functional status, only sensor functional status. |
| 395 | if (_numSensorFailsForNonFunc) |
Matthew Barth | e11cbc6 | 2018-02-20 12:11:07 -0600 | [diff] [blame] | 396 | { |
Matthew Barth | 7c23a04 | 2021-01-26 16:21:45 -0600 | [diff] [blame] | 397 | auto numNonFuncSensors = countNonFunctionalSensors(); |
Matt Spinler | ae1f8ef | 2020-10-14 16:15:51 -0500 | [diff] [blame] | 398 | // If the fan was nonfunctional and enough sensors are now OK, |
Matthew Barth | cceffdd | 2021-05-20 12:17:21 -0500 | [diff] [blame] | 399 | // the fan can be set to functional as long as `set_func_on_present` was |
| 400 | // not set |
| 401 | if (!_setFuncOnPresent && !_functional && |
| 402 | !(numNonFuncSensors >= _numSensorFailsForNonFunc)) |
Matt Spinler | ae1f8ef | 2020-10-14 16:15:51 -0500 | [diff] [blame] | 403 | { |
Matthew Barth | 7c23a04 | 2021-01-26 16:21:45 -0600 | [diff] [blame] | 404 | getLogger().log(fmt::format("Setting fan {} to functional, number " |
| 405 | "of nonfunctional sensors = {}", |
| 406 | _name, numNonFuncSensors)); |
Matt Spinler | ae1f8ef | 2020-10-14 16:15:51 -0500 | [diff] [blame] | 407 | updateInventory(true); |
| 408 | } |
Matt Spinler | a9406a7 | 2017-04-27 14:29:24 -0500 | [diff] [blame] | 409 | |
Matt Spinler | ae1f8ef | 2020-10-14 16:15:51 -0500 | [diff] [blame] | 410 | // If the fan is currently functional, but too many |
| 411 | // contained sensors are now nonfunctional, update |
Matthew Barth | 7c23a04 | 2021-01-26 16:21:45 -0600 | [diff] [blame] | 412 | // the fan to nonfunctional. |
| 413 | if (_functional && (numNonFuncSensors >= _numSensorFailsForNonFunc)) |
Matt Spinler | ae1f8ef | 2020-10-14 16:15:51 -0500 | [diff] [blame] | 414 | { |
Matthew Barth | 7c23a04 | 2021-01-26 16:21:45 -0600 | [diff] [blame] | 415 | getLogger().log(fmt::format("Setting fan {} to nonfunctional, " |
| 416 | "number of nonfunctional sensors = {}", |
| 417 | _name, numNonFuncSensors)); |
Matt Spinler | ae1f8ef | 2020-10-14 16:15:51 -0500 | [diff] [blame] | 418 | updateInventory(false); |
| 419 | } |
Matt Spinler | b1e1851 | 2017-04-27 14:42:33 -0500 | [diff] [blame] | 420 | } |
Matt Spinler | b63aa09 | 2020-10-14 09:45:11 -0500 | [diff] [blame] | 421 | |
Matt Spinler | ae01b5f | 2022-07-06 16:49:04 -0500 | [diff] [blame] | 422 | // Skip the power off rule checks if the sensor isn't |
| 423 | // on D-Bus so a running system isn't shutdown. |
| 424 | _system.fanStatusChange(*this, !sensor.hasOwner()); |
Matt Spinler | a9406a7 | 2017-04-27 14:29:24 -0500 | [diff] [blame] | 425 | } |
| 426 | |
Mike Capps | 9ff4877 | 2021-07-19 14:49:43 -0400 | [diff] [blame] | 427 | bool Fan::updateInventory(bool functional) |
Matt Spinler | b1e1851 | 2017-04-27 14:42:33 -0500 | [diff] [blame] | 428 | { |
Mike Capps | 9ff4877 | 2021-07-19 14:49:43 -0400 | [diff] [blame] | 429 | bool dbusError = false; |
| 430 | |
| 431 | try |
Matt Spinler | b1e1851 | 2017-04-27 14:42:33 -0500 | [diff] [blame] | 432 | { |
Mike Capps | 9ff4877 | 2021-07-19 14:49:43 -0400 | [diff] [blame] | 433 | auto objectMap = |
| 434 | util::getObjMap<bool>(_name, util::OPERATIONAL_STATUS_INTF, |
| 435 | util::FUNCTIONAL_PROPERTY, functional); |
| 436 | |
Mike Capps | 8af8a62 | 2022-02-04 16:13:33 -0500 | [diff] [blame] | 437 | auto response = util::SDBusPlus::callMethod( |
| 438 | _bus, util::INVENTORY_SVC, util::INVENTORY_PATH, |
| 439 | util::INVENTORY_INTF, "Notify", objectMap); |
Mike Capps | 9ff4877 | 2021-07-19 14:49:43 -0400 | [diff] [blame] | 440 | |
| 441 | if (response.is_method_error()) |
| 442 | { |
| 443 | log<level::ERR>("Error in Notify call to update inventory"); |
| 444 | |
| 445 | dbusError = true; |
| 446 | } |
| 447 | } |
| 448 | catch (const util::DBusError& e) |
| 449 | { |
| 450 | dbusError = true; |
| 451 | |
| 452 | getLogger().log( |
| 453 | fmt::format("D-Bus Exception reading/updating inventory : {}", |
| 454 | e.what()), |
| 455 | Logger::error); |
Matt Spinler | b1e1851 | 2017-04-27 14:42:33 -0500 | [diff] [blame] | 456 | } |
| 457 | |
Matthew Barth | 177fe98 | 2020-05-26 11:05:19 -0500 | [diff] [blame] | 458 | // This will always track the current state of the inventory. |
Matt Spinler | b1e1851 | 2017-04-27 14:42:33 -0500 | [diff] [blame] | 459 | _functional = functional; |
Mike Capps | 9ff4877 | 2021-07-19 14:49:43 -0400 | [diff] [blame] | 460 | |
| 461 | return dbusError; |
Matt Spinler | b1e1851 | 2017-04-27 14:42:33 -0500 | [diff] [blame] | 462 | } |
| 463 | |
Patrick Williams | cb356d4 | 2022-07-22 19:26:53 -0500 | [diff] [blame] | 464 | void Fan::presenceChanged(sdbusplus::message_t& msg) |
Matt Spinler | b63aa09 | 2020-10-14 09:45:11 -0500 | [diff] [blame] | 465 | { |
| 466 | std::string interface; |
| 467 | std::map<std::string, std::variant<bool>> properties; |
| 468 | |
| 469 | msg.read(interface, properties); |
| 470 | |
| 471 | auto presentProp = properties.find("Present"); |
| 472 | if (presentProp != properties.end()) |
| 473 | { |
| 474 | _present = std::get<bool>(presentProp->second); |
| 475 | |
Matt Spinler | 27f6b68 | 2020-10-27 08:43:37 -0500 | [diff] [blame] | 476 | getLogger().log( |
Matt Spinler | ac37297 | 2021-01-25 15:11:22 -0600 | [diff] [blame] | 477 | fmt::format("Fan {} presence state change to {}", _name, _present)); |
Matt Spinler | 27f6b68 | 2020-10-27 08:43:37 -0500 | [diff] [blame] | 478 | |
Matt Spinler | a3584bd | 2021-03-29 15:48:30 -0500 | [diff] [blame] | 479 | if (_present && _setFuncOnPresent) |
| 480 | { |
| 481 | updateInventory(true); |
| 482 | std::for_each(_sensors.begin(), _sensors.end(), [](auto& sensor) { |
| 483 | sensor->setFunctional(true); |
| 484 | sensor->resetMethod(); |
| 485 | }); |
| 486 | } |
| 487 | |
Matthew Barth | 43b4cde | 2022-02-15 16:30:11 -0600 | [diff] [blame] | 488 | _system.fanStatusChange(*this); |
| 489 | |
Matt Spinler | 27f6b68 | 2020-10-27 08:43:37 -0500 | [diff] [blame] | 490 | if (_fanMissingErrorDelay) |
| 491 | { |
Matt Spinler | 7d13564 | 2021-02-04 12:44:17 -0600 | [diff] [blame] | 492 | if (!_present && _system.isPowerOn()) |
Matt Spinler | 27f6b68 | 2020-10-27 08:43:37 -0500 | [diff] [blame] | 493 | { |
| 494 | _fanMissingErrorTimer->restartOnce( |
| 495 | std::chrono::seconds{*_fanMissingErrorDelay}); |
| 496 | } |
Matt Spinler | 7d13564 | 2021-02-04 12:44:17 -0600 | [diff] [blame] | 497 | else if (_present && _fanMissingErrorTimer->isEnabled()) |
Matt Spinler | 27f6b68 | 2020-10-27 08:43:37 -0500 | [diff] [blame] | 498 | { |
| 499 | _fanMissingErrorTimer->setEnabled(false); |
| 500 | } |
| 501 | } |
Matt Spinler | b63aa09 | 2020-10-14 09:45:11 -0500 | [diff] [blame] | 502 | } |
| 503 | } |
Matt Spinler | f13b42e | 2020-10-26 15:29:49 -0500 | [diff] [blame] | 504 | |
| 505 | void Fan::sensorErrorTimerExpired(const TachSensor& sensor) |
| 506 | { |
Matt Spinler | 7d13564 | 2021-02-04 12:44:17 -0600 | [diff] [blame] | 507 | if (_present && _system.isPowerOn()) |
Matt Spinler | f13b42e | 2020-10-26 15:29:49 -0500 | [diff] [blame] | 508 | { |
| 509 | _system.sensorErrorTimerExpired(*this, sensor); |
| 510 | } |
| 511 | } |
| 512 | |
Mike Capps | 808d7fe | 2022-06-13 10:12:16 -0400 | [diff] [blame] | 513 | void Fan::powerStateChanged([[maybe_unused]] bool powerStateOn) |
Matt Spinler | 7d13564 | 2021-02-04 12:44:17 -0600 | [diff] [blame] | 514 | { |
| 515 | #ifdef MONITOR_USE_JSON |
| 516 | if (powerStateOn) |
| 517 | { |
Matt Spinler | 7d13564 | 2021-02-04 12:44:17 -0600 | [diff] [blame] | 518 | _monitorTimer.restartOnce(std::chrono::seconds(_monitorDelay)); |
| 519 | |
Matt Spinler | bb449c1 | 2021-06-14 11:45:28 -0600 | [diff] [blame] | 520 | _numSensorsOnDBusAtPowerOn = 0; |
| 521 | |
| 522 | std::for_each(_sensors.begin(), _sensors.end(), [this](auto& sensor) { |
| 523 | try |
| 524 | { |
| 525 | // Force a getProperty call. If sensor is on D-Bus, |
| 526 | // then make sure it's functional. |
| 527 | sensor->updateTachAndTarget(); |
| 528 | |
| 529 | _numSensorsOnDBusAtPowerOn++; |
| 530 | |
| 531 | if (_present) |
| 532 | { |
| 533 | // If not functional, set it back to functional. |
| 534 | if (!sensor->functional()) |
| 535 | { |
| 536 | sensor->setFunctional(true); |
| 537 | _system.fanStatusChange(*this, true); |
| 538 | } |
| 539 | |
| 540 | // Set the counters back to zero |
| 541 | if (sensor->getMethod() == MethodMode::count) |
| 542 | { |
| 543 | sensor->resetMethod(); |
| 544 | } |
| 545 | } |
| 546 | } |
| 547 | catch (const util::DBusError& e) |
| 548 | { |
| 549 | // Properties still aren't on D-Bus. Let startMonitor() |
| 550 | // deal with it, or maybe System::powerStateChanged() if |
| 551 | // there aren't any sensors at all on D-Bus. |
| 552 | getLogger().log(fmt::format( |
| 553 | "At power on, tach sensor {} value not on D-Bus", |
| 554 | sensor->name())); |
| 555 | } |
| 556 | }); |
| 557 | |
Matt Spinler | 4283c5d | 2021-03-01 15:56:00 -0600 | [diff] [blame] | 558 | if (_present) |
| 559 | { |
Matt Spinler | 4283c5d | 2021-03-01 15:56:00 -0600 | [diff] [blame] | 560 | // If configured to change functional state on the fan itself, |
| 561 | // Set it back to true now if necessary. |
| 562 | if (_numSensorFailsForNonFunc) |
| 563 | { |
| 564 | if (!_functional && |
| 565 | (countNonFunctionalSensors() < _numSensorFailsForNonFunc)) |
| 566 | { |
| 567 | updateInventory(true); |
| 568 | } |
| 569 | } |
| 570 | } |
| 571 | else |
Matt Spinler | 7d13564 | 2021-02-04 12:44:17 -0600 | [diff] [blame] | 572 | { |
| 573 | getLogger().log( |
| 574 | fmt::format("At power on, fan {} is missing", _name)); |
| 575 | |
| 576 | if (_fanMissingErrorTimer) |
| 577 | { |
| 578 | _fanMissingErrorTimer->restartOnce( |
| 579 | std::chrono::seconds{*_fanMissingErrorDelay}); |
| 580 | } |
| 581 | } |
| 582 | } |
| 583 | else |
| 584 | { |
| 585 | _monitorReady = false; |
| 586 | |
| 587 | if (_monitorTimer.isEnabled()) |
| 588 | { |
| 589 | _monitorTimer.setEnabled(false); |
| 590 | } |
| 591 | |
| 592 | if (_fanMissingErrorTimer && _fanMissingErrorTimer->isEnabled()) |
| 593 | { |
| 594 | _fanMissingErrorTimer->setEnabled(false); |
| 595 | } |
| 596 | |
| 597 | std::for_each(_sensors.begin(), _sensors.end(), [](auto& sensor) { |
| 598 | if (sensor->timerRunning()) |
| 599 | { |
| 600 | sensor->stopTimer(); |
| 601 | } |
Matt Spinler | 623635c | 2021-03-29 13:13:59 -0500 | [diff] [blame] | 602 | |
Matt Spinler | fdfcc67 | 2021-06-01 14:51:06 -0600 | [diff] [blame] | 603 | sensor->stopCountTimer(); |
| 604 | }); |
Matt Spinler | 7d13564 | 2021-02-04 12:44:17 -0600 | [diff] [blame] | 605 | } |
| 606 | #endif |
| 607 | } |
| 608 | |
Matthew Barth | 177fe98 | 2020-05-26 11:05:19 -0500 | [diff] [blame] | 609 | } // namespace monitor |
| 610 | } // namespace fan |
| 611 | } // namespace phosphor |