Matt Spinler | abf8da3 | 2017-04-27 14:08:45 -0500 | [diff] [blame] | 1 | /** |
| 2 | * Copyright © 2017 IBM Corporation |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
Matt Spinler | abf8da3 | 2017-04-27 14:08:45 -0500 | [diff] [blame] | 16 | #include "fan.hpp" |
Matthew Barth | 177fe98 | 2020-05-26 11:05:19 -0500 | [diff] [blame] | 17 | |
Matt Spinler | ae1f8ef | 2020-10-14 16:15:51 -0500 | [diff] [blame] | 18 | #include "logging.hpp" |
Matthew Barth | 177fe98 | 2020-05-26 11:05:19 -0500 | [diff] [blame] | 19 | #include "sdbusplus.hpp" |
Matt Spinler | b0412d0 | 2020-10-12 16:53:52 -0500 | [diff] [blame] | 20 | #include "system.hpp" |
Matt Spinler | abf8da3 | 2017-04-27 14:08:45 -0500 | [diff] [blame] | 21 | #include "types.hpp" |
Matt Spinler | b1e1851 | 2017-04-27 14:42:33 -0500 | [diff] [blame] | 22 | #include "utility.hpp" |
Matthew Barth | 177fe98 | 2020-05-26 11:05:19 -0500 | [diff] [blame] | 23 | |
Jay Meyer | a7aed01 | 2020-10-06 14:32:22 -0500 | [diff] [blame] | 24 | #include <fmt/format.h> |
| 25 | |
Matthew Barth | 177fe98 | 2020-05-26 11:05:19 -0500 | [diff] [blame] | 26 | #include <phosphor-logging/log.hpp> |
| 27 | |
| 28 | #include <algorithm> |
Matt Spinler | abf8da3 | 2017-04-27 14:08:45 -0500 | [diff] [blame] | 29 | |
| 30 | namespace phosphor |
| 31 | { |
| 32 | namespace fan |
| 33 | { |
| 34 | namespace monitor |
| 35 | { |
| 36 | |
| 37 | using namespace phosphor::logging; |
Matt Spinler | b0412d0 | 2020-10-12 16:53:52 -0500 | [diff] [blame] | 38 | using namespace sdbusplus::bus::match; |
Matt Spinler | abf8da3 | 2017-04-27 14:08:45 -0500 | [diff] [blame] | 39 | |
Matthew Barth | 177fe98 | 2020-05-26 11:05:19 -0500 | [diff] [blame] | 40 | Fan::Fan(Mode mode, sdbusplus::bus::bus& bus, const sdeventplus::Event& event, |
Matt Spinler | b0412d0 | 2020-10-12 16:53:52 -0500 | [diff] [blame] | 41 | std::unique_ptr<trust::Manager>& trust, const FanDefinition& def, |
| 42 | System& system) : |
Matt Spinler | abf8da3 | 2017-04-27 14:08:45 -0500 | [diff] [blame] | 43 | _bus(bus), |
| 44 | _name(std::get<fanNameField>(def)), |
| 45 | _deviation(std::get<fanDeviationField>(def)), |
Matt Spinler | c39e859 | 2017-09-28 13:13:08 -0500 | [diff] [blame] | 46 | _numSensorFailsForNonFunc(std::get<numSensorFailsForNonfuncField>(def)), |
Matt Spinler | b0412d0 | 2020-10-12 16:53:52 -0500 | [diff] [blame] | 47 | _trustManager(trust), |
| 48 | #ifdef MONITOR_USE_JSON |
| 49 | _monitorDelay(std::get<monitorStartDelayField>(def)), |
| 50 | _monitorTimer(event, std::bind(std::mem_fn(&Fan::startMonitor), this)), |
| 51 | #endif |
Matt Spinler | b63aa09 | 2020-10-14 09:45:11 -0500 | [diff] [blame] | 52 | _system(system), |
| 53 | _presenceMatch(bus, |
| 54 | rules::propertiesChanged(util::INVENTORY_PATH + _name, |
| 55 | util::INV_ITEM_IFACE), |
| 56 | std::bind(std::mem_fn(&Fan::presenceChanged), this, |
Matt Spinler | 27f6b68 | 2020-10-27 08:43:37 -0500 | [diff] [blame] | 57 | std::placeholders::_1)), |
Matt Spinler | 7d13564 | 2021-02-04 12:44:17 -0600 | [diff] [blame] | 58 | _presenceIfaceAddedMatch( |
| 59 | bus, |
| 60 | rules::interfacesAdded() + |
| 61 | rules::argNpath(0, util::INVENTORY_PATH + _name), |
| 62 | std::bind(std::mem_fn(&Fan::presenceIfaceAdded), this, |
| 63 | std::placeholders::_1)), |
Matt Spinler | 27f6b68 | 2020-10-27 08:43:37 -0500 | [diff] [blame] | 64 | _fanMissingErrorDelay(std::get<fanMissingErrDelayField>(def)) |
Matt Spinler | abf8da3 | 2017-04-27 14:08:45 -0500 | [diff] [blame] | 65 | { |
Matt Spinler | ae1f8ef | 2020-10-14 16:15:51 -0500 | [diff] [blame] | 66 | // Start from a known state of functional (even if |
| 67 | // _numSensorFailsForNonFunc is 0) |
Jolie Ku | 4c3c24f | 2020-09-09 11:01:46 +0800 | [diff] [blame] | 68 | updateInventory(true); |
| 69 | |
Matthew Barth | 0a9fe16 | 2018-01-26 12:53:15 -0600 | [diff] [blame] | 70 | // Setup tach sensors for monitoring |
| 71 | auto& sensors = std::get<sensorListField>(def); |
| 72 | for (auto& s : sensors) |
| 73 | { |
| 74 | try |
| 75 | { |
Matthew Barth | 177fe98 | 2020-05-26 11:05:19 -0500 | [diff] [blame] | 76 | _sensors.emplace_back(std::make_shared<TachSensor>( |
| 77 | mode, bus, *this, std::get<sensorNameField>(s), |
| 78 | std::get<hasTargetField>(s), std::get<funcDelay>(def), |
| 79 | std::get<targetInterfaceField>(s), std::get<factorField>(s), |
Jolie Ku | 69f2f48 | 2020-10-21 09:59:43 +0800 | [diff] [blame] | 80 | std::get<offsetField>(s), std::get<methodField>(def), |
| 81 | std::get<thresholdField>(s), std::get<timeoutField>(def), |
Matt Spinler | f13b42e | 2020-10-26 15:29:49 -0500 | [diff] [blame] | 82 | std::get<nonfuncRotorErrDelayField>(def), event)); |
Matthew Barth | 0a9fe16 | 2018-01-26 12:53:15 -0600 | [diff] [blame] | 83 | |
| 84 | _trustManager->registerSensor(_sensors.back()); |
| 85 | } |
| 86 | catch (InvalidSensorError& e) |
Jolie Ku | 4c3c24f | 2020-09-09 11:01:46 +0800 | [diff] [blame] | 87 | { |
Matt Spinler | ae1f8ef | 2020-10-14 16:15:51 -0500 | [diff] [blame] | 88 | // Count the number of failed tach sensors, though if |
| 89 | // _numSensorFailsForNonFunc is zero that means the fan should not |
| 90 | // be set to nonfunctional. |
| 91 | if (_numSensorFailsForNonFunc && |
| 92 | (++_numFailedSensor >= _numSensorFailsForNonFunc)) |
Jolie Ku | 5d564a9 | 2020-10-23 09:04:28 +0800 | [diff] [blame] | 93 | { |
| 94 | // Mark associated fan as nonfunctional |
| 95 | updateInventory(false); |
| 96 | } |
Jolie Ku | 4c3c24f | 2020-09-09 11:01:46 +0800 | [diff] [blame] | 97 | } |
Matthew Barth | 0a9fe16 | 2018-01-26 12:53:15 -0600 | [diff] [blame] | 98 | } |
| 99 | |
Matt Spinler | b0412d0 | 2020-10-12 16:53:52 -0500 | [diff] [blame] | 100 | #ifndef MONITOR_USE_JSON |
Matthew Barth | 0a9fe16 | 2018-01-26 12:53:15 -0600 | [diff] [blame] | 101 | // Check current tach state when entering monitor mode |
Matthew Barth | 6ad2843 | 2017-08-22 11:18:19 -0500 | [diff] [blame] | 102 | if (mode != Mode::init) |
| 103 | { |
Matt Spinler | b0412d0 | 2020-10-12 16:53:52 -0500 | [diff] [blame] | 104 | _monitorReady = true; |
| 105 | |
Matthew Barth | 177fe98 | 2020-05-26 11:05:19 -0500 | [diff] [blame] | 106 | // The TachSensors will now have already read the input |
| 107 | // and target values, so check them. |
Matthew Barth | 6ad2843 | 2017-08-22 11:18:19 -0500 | [diff] [blame] | 108 | tachChanged(); |
| 109 | } |
Matt Spinler | b0412d0 | 2020-10-12 16:53:52 -0500 | [diff] [blame] | 110 | #else |
Matt Spinler | 7d13564 | 2021-02-04 12:44:17 -0600 | [diff] [blame] | 111 | if (_system.isPowerOn()) |
| 112 | { |
| 113 | _monitorTimer.restartOnce(std::chrono::seconds(_monitorDelay)); |
| 114 | } |
Matt Spinler | b0412d0 | 2020-10-12 16:53:52 -0500 | [diff] [blame] | 115 | #endif |
Matt Spinler | b63aa09 | 2020-10-14 09:45:11 -0500 | [diff] [blame] | 116 | |
Matt Spinler | 27f6b68 | 2020-10-27 08:43:37 -0500 | [diff] [blame] | 117 | if (_fanMissingErrorDelay) |
| 118 | { |
| 119 | _fanMissingErrorTimer = std::make_unique< |
| 120 | sdeventplus::utility::Timer<sdeventplus::ClockId::Monotonic>>( |
| 121 | event, std::bind(&System::fanMissingErrorTimerExpired, &system, |
| 122 | std::ref(*this))); |
Matt Spinler | 7d13564 | 2021-02-04 12:44:17 -0600 | [diff] [blame] | 123 | } |
Matt Spinler | 27f6b68 | 2020-10-27 08:43:37 -0500 | [diff] [blame] | 124 | |
Matt Spinler | 7d13564 | 2021-02-04 12:44:17 -0600 | [diff] [blame] | 125 | try |
| 126 | { |
| 127 | _present = util::SDBusPlus::getProperty<bool>( |
| 128 | util::INVENTORY_PATH + _name, util::INV_ITEM_IFACE, "Present"); |
| 129 | |
| 130 | if (!_present) |
Matt Spinler | 27f6b68 | 2020-10-27 08:43:37 -0500 | [diff] [blame] | 131 | { |
Matt Spinler | ac37297 | 2021-01-25 15:11:22 -0600 | [diff] [blame] | 132 | getLogger().log( |
| 133 | fmt::format("On startup, fan {} is missing", _name)); |
Matt Spinler | 7d13564 | 2021-02-04 12:44:17 -0600 | [diff] [blame] | 134 | if (_system.isPowerOn() && _fanMissingErrorTimer) |
| 135 | { |
| 136 | _fanMissingErrorTimer->restartOnce( |
| 137 | std::chrono::seconds{*_fanMissingErrorDelay}); |
| 138 | } |
| 139 | } |
| 140 | } |
| 141 | catch (const util::DBusServiceError& e) |
| 142 | { |
| 143 | // This could happen on the first BMC boot if the presence |
| 144 | // detect app hasn't started yet and there isn't an inventory |
| 145 | // cache yet. |
| 146 | } |
| 147 | } |
| 148 | |
| 149 | void Fan::presenceIfaceAdded(sdbusplus::message::message& msg) |
| 150 | { |
| 151 | sdbusplus::message::object_path path; |
| 152 | std::map<std::string, std::map<std::string, std::variant<bool>>> interfaces; |
| 153 | |
| 154 | msg.read(path, interfaces); |
| 155 | |
| 156 | auto properties = interfaces.find(util::INV_ITEM_IFACE); |
| 157 | if (properties == interfaces.end()) |
| 158 | { |
| 159 | return; |
| 160 | } |
| 161 | |
| 162 | auto property = properties->second.find("Present"); |
| 163 | if (property == properties->second.end()) |
| 164 | { |
| 165 | return; |
| 166 | } |
| 167 | |
| 168 | _present = std::get<bool>(property->second); |
| 169 | |
| 170 | if (!_present) |
| 171 | { |
| 172 | getLogger().log(fmt::format( |
| 173 | "New fan {} interface added and fan is not present", _name)); |
| 174 | if (_system.isPowerOn() && _fanMissingErrorTimer) |
| 175 | { |
Matt Spinler | 27f6b68 | 2020-10-27 08:43:37 -0500 | [diff] [blame] | 176 | _fanMissingErrorTimer->restartOnce( |
| 177 | std::chrono::seconds{*_fanMissingErrorDelay}); |
| 178 | } |
| 179 | } |
Matt Spinler | 7d13564 | 2021-02-04 12:44:17 -0600 | [diff] [blame] | 180 | |
| 181 | _system.fanStatusChange(*this); |
Matt Spinler | b0412d0 | 2020-10-12 16:53:52 -0500 | [diff] [blame] | 182 | } |
| 183 | |
| 184 | void Fan::startMonitor() |
| 185 | { |
| 186 | _monitorReady = true; |
| 187 | |
| 188 | tachChanged(); |
Matt Spinler | abf8da3 | 2017-04-27 14:08:45 -0500 | [diff] [blame] | 189 | } |
| 190 | |
Matt Spinler | ebaae61 | 2017-04-27 14:21:48 -0500 | [diff] [blame] | 191 | void Fan::tachChanged() |
| 192 | { |
Matt Spinler | b0412d0 | 2020-10-12 16:53:52 -0500 | [diff] [blame] | 193 | if (_monitorReady) |
Matt Spinler | ebaae61 | 2017-04-27 14:21:48 -0500 | [diff] [blame] | 194 | { |
Matt Spinler | b0412d0 | 2020-10-12 16:53:52 -0500 | [diff] [blame] | 195 | for (auto& s : _sensors) |
| 196 | { |
| 197 | tachChanged(*s); |
| 198 | } |
Matt Spinler | ebaae61 | 2017-04-27 14:21:48 -0500 | [diff] [blame] | 199 | } |
| 200 | } |
| 201 | |
Matt Spinler | ebaae61 | 2017-04-27 14:21:48 -0500 | [diff] [blame] | 202 | void Fan::tachChanged(TachSensor& sensor) |
| 203 | { |
Matt Spinler | 7d13564 | 2021-02-04 12:44:17 -0600 | [diff] [blame] | 204 | if (!_system.isPowerOn() || !_monitorReady) |
| 205 | { |
| 206 | return; |
| 207 | } |
| 208 | |
Matt Spinler | c39e859 | 2017-09-28 13:13:08 -0500 | [diff] [blame] | 209 | if (_trustManager->active()) |
| 210 | { |
| 211 | if (!_trustManager->checkTrust(sensor)) |
| 212 | { |
| 213 | return; |
| 214 | } |
| 215 | } |
| 216 | |
Matthew Barth | fcb0dbc | 2021-02-10 14:23:39 -0600 | [diff] [blame] | 217 | process(sensor); |
| 218 | } |
| 219 | |
| 220 | void Fan::process(TachSensor& sensor) |
| 221 | { |
Matthew Barth | 177fe98 | 2020-05-26 11:05:19 -0500 | [diff] [blame] | 222 | // If this sensor is out of range at this moment, start |
| 223 | // its timer, at the end of which the inventory |
| 224 | // for the fan may get updated to not functional. |
Matt Spinler | a4c8f1f | 2017-04-27 14:38:38 -0500 | [diff] [blame] | 225 | |
Matthew Barth | 177fe98 | 2020-05-26 11:05:19 -0500 | [diff] [blame] | 226 | // If this sensor is OK, put everything back into a good state. |
Matt Spinler | a4c8f1f | 2017-04-27 14:38:38 -0500 | [diff] [blame] | 227 | |
| 228 | if (outOfRange(sensor)) |
| 229 | { |
Matthew Barth | e11cbc6 | 2018-02-20 12:11:07 -0600 | [diff] [blame] | 230 | if (sensor.functional()) |
Matt Spinler | a4c8f1f | 2017-04-27 14:38:38 -0500 | [diff] [blame] | 231 | { |
Jolie Ku | 69f2f48 | 2020-10-21 09:59:43 +0800 | [diff] [blame] | 232 | switch (sensor.getMethod()) |
| 233 | { |
| 234 | case MethodMode::timebased: |
| 235 | // Start nonfunctional timer if not already running |
| 236 | sensor.startTimer(TimerMode::nonfunc); |
| 237 | break; |
| 238 | case MethodMode::count: |
| 239 | sensor.setCounter(true); |
| 240 | if (sensor.getCounter() >= sensor.getThreshold()) |
| 241 | { |
| 242 | updateState(sensor); |
| 243 | } |
| 244 | break; |
| 245 | } |
Matt Spinler | a4c8f1f | 2017-04-27 14:38:38 -0500 | [diff] [blame] | 246 | } |
| 247 | } |
| 248 | else |
| 249 | { |
Jolie Ku | 69f2f48 | 2020-10-21 09:59:43 +0800 | [diff] [blame] | 250 | switch (sensor.getMethod()) |
Matt Spinler | a4c8f1f | 2017-04-27 14:38:38 -0500 | [diff] [blame] | 251 | { |
Jolie Ku | 69f2f48 | 2020-10-21 09:59:43 +0800 | [diff] [blame] | 252 | case MethodMode::timebased: |
| 253 | if (sensor.functional()) |
| 254 | { |
Matthew Barth | 11b5d8f | 2021-01-28 14:04:09 -0600 | [diff] [blame] | 255 | if (sensor.timerRunning()) |
| 256 | { |
| 257 | sensor.stopTimer(); |
| 258 | } |
Jolie Ku | 69f2f48 | 2020-10-21 09:59:43 +0800 | [diff] [blame] | 259 | } |
| 260 | else |
| 261 | { |
| 262 | // Start functional timer if not already running |
| 263 | sensor.startTimer(TimerMode::func); |
| 264 | } |
| 265 | break; |
| 266 | case MethodMode::count: |
| 267 | sensor.setCounter(false); |
| 268 | if (!sensor.functional() && sensor.getCounter() == 0) |
| 269 | { |
| 270 | updateState(sensor); |
| 271 | } |
| 272 | break; |
Matt Spinler | a4c8f1f | 2017-04-27 14:38:38 -0500 | [diff] [blame] | 273 | } |
| 274 | } |
Matt Spinler | ebaae61 | 2017-04-27 14:21:48 -0500 | [diff] [blame] | 275 | } |
| 276 | |
Matthew Barth | f552ea5 | 2018-01-15 16:22:04 -0600 | [diff] [blame] | 277 | uint64_t Fan::findTargetSpeed() |
Matt Spinler | abf8da3 | 2017-04-27 14:08:45 -0500 | [diff] [blame] | 278 | { |
| 279 | uint64_t target = 0; |
Matthew Barth | 177fe98 | 2020-05-26 11:05:19 -0500 | [diff] [blame] | 280 | // The sensor doesn't support a target, |
| 281 | // so get it from another sensor. |
Matthew Barth | f552ea5 | 2018-01-15 16:22:04 -0600 | [diff] [blame] | 282 | auto s = std::find_if(_sensors.begin(), _sensors.end(), |
Matthew Barth | 177fe98 | 2020-05-26 11:05:19 -0500 | [diff] [blame] | 283 | [](const auto& s) { return s->hasTarget(); }); |
Matt Spinler | abf8da3 | 2017-04-27 14:08:45 -0500 | [diff] [blame] | 284 | |
Matthew Barth | f552ea5 | 2018-01-15 16:22:04 -0600 | [diff] [blame] | 285 | if (s != _sensors.end()) |
Matt Spinler | abf8da3 | 2017-04-27 14:08:45 -0500 | [diff] [blame] | 286 | { |
Matthew Barth | f552ea5 | 2018-01-15 16:22:04 -0600 | [diff] [blame] | 287 | target = (*s)->getTarget(); |
Matt Spinler | abf8da3 | 2017-04-27 14:08:45 -0500 | [diff] [blame] | 288 | } |
| 289 | |
| 290 | return target; |
| 291 | } |
| 292 | |
Matthew Barth | 7c23a04 | 2021-01-26 16:21:45 -0600 | [diff] [blame] | 293 | size_t Fan::countNonFunctionalSensors() |
Matt Spinler | abf8da3 | 2017-04-27 14:08:45 -0500 | [diff] [blame] | 294 | { |
Matthew Barth | 7c23a04 | 2021-01-26 16:21:45 -0600 | [diff] [blame] | 295 | return std::count_if(_sensors.begin(), _sensors.end(), |
| 296 | [](const auto& s) { return !s->functional(); }); |
Matt Spinler | abf8da3 | 2017-04-27 14:08:45 -0500 | [diff] [blame] | 297 | } |
| 298 | |
Matt Spinler | abf8da3 | 2017-04-27 14:08:45 -0500 | [diff] [blame] | 299 | bool Fan::outOfRange(const TachSensor& sensor) |
| 300 | { |
| 301 | auto actual = static_cast<uint64_t>(sensor.getInput()); |
Matthew Barth | 7c23a04 | 2021-01-26 16:21:45 -0600 | [diff] [blame] | 302 | auto range = sensor.getRange(_deviation); |
Matt Spinler | abf8da3 | 2017-04-27 14:08:45 -0500 | [diff] [blame] | 303 | |
Matthew Barth | 7c23a04 | 2021-01-26 16:21:45 -0600 | [diff] [blame] | 304 | if ((actual < range.first) || (actual > range.second)) |
Matt Spinler | abf8da3 | 2017-04-27 14:08:45 -0500 | [diff] [blame] | 305 | { |
| 306 | return true; |
| 307 | } |
| 308 | |
| 309 | return false; |
| 310 | } |
| 311 | |
Jolie Ku | 69f2f48 | 2020-10-21 09:59:43 +0800 | [diff] [blame] | 312 | void Fan::updateState(TachSensor& sensor) |
Matt Spinler | a9406a7 | 2017-04-27 14:29:24 -0500 | [diff] [blame] | 313 | { |
Matthew Barth | 7c23a04 | 2021-01-26 16:21:45 -0600 | [diff] [blame] | 314 | auto range = sensor.getRange(_deviation); |
Matt Spinler | 7d13564 | 2021-02-04 12:44:17 -0600 | [diff] [blame] | 315 | |
| 316 | if (!_system.isPowerOn()) |
| 317 | { |
| 318 | return; |
| 319 | } |
| 320 | |
Matthew Barth | e11cbc6 | 2018-02-20 12:11:07 -0600 | [diff] [blame] | 321 | sensor.setFunctional(!sensor.functional()); |
Matt Spinler | ae1f8ef | 2020-10-14 16:15:51 -0500 | [diff] [blame] | 322 | getLogger().log( |
| 323 | fmt::format("Setting tach sensor {} functional state to {}. " |
Matthew Barth | 7c23a04 | 2021-01-26 16:21:45 -0600 | [diff] [blame] | 324 | "[target = {}, input = {}, allowed range = ({} - {})]", |
| 325 | sensor.name(), sensor.functional(), sensor.getTarget(), |
| 326 | sensor.getInput(), range.first, range.second)); |
Matt Spinler | ae1f8ef | 2020-10-14 16:15:51 -0500 | [diff] [blame] | 327 | |
| 328 | // A zero value for _numSensorFailsForNonFunc means we aren't dealing |
| 329 | // with fan FRU functional status, only sensor functional status. |
| 330 | if (_numSensorFailsForNonFunc) |
Matthew Barth | e11cbc6 | 2018-02-20 12:11:07 -0600 | [diff] [blame] | 331 | { |
Matthew Barth | 7c23a04 | 2021-01-26 16:21:45 -0600 | [diff] [blame] | 332 | auto numNonFuncSensors = countNonFunctionalSensors(); |
Matt Spinler | ae1f8ef | 2020-10-14 16:15:51 -0500 | [diff] [blame] | 333 | // If the fan was nonfunctional and enough sensors are now OK, |
Matthew Barth | 7c23a04 | 2021-01-26 16:21:45 -0600 | [diff] [blame] | 334 | // the fan can be set to functional |
| 335 | if (!_functional && !(numNonFuncSensors >= _numSensorFailsForNonFunc)) |
Matt Spinler | ae1f8ef | 2020-10-14 16:15:51 -0500 | [diff] [blame] | 336 | { |
Matthew Barth | 7c23a04 | 2021-01-26 16:21:45 -0600 | [diff] [blame] | 337 | getLogger().log(fmt::format("Setting fan {} to functional, number " |
| 338 | "of nonfunctional sensors = {}", |
| 339 | _name, numNonFuncSensors)); |
Matt Spinler | ae1f8ef | 2020-10-14 16:15:51 -0500 | [diff] [blame] | 340 | updateInventory(true); |
| 341 | } |
Matt Spinler | a9406a7 | 2017-04-27 14:29:24 -0500 | [diff] [blame] | 342 | |
Matt Spinler | ae1f8ef | 2020-10-14 16:15:51 -0500 | [diff] [blame] | 343 | // If the fan is currently functional, but too many |
| 344 | // contained sensors are now nonfunctional, update |
Matthew Barth | 7c23a04 | 2021-01-26 16:21:45 -0600 | [diff] [blame] | 345 | // the fan to nonfunctional. |
| 346 | if (_functional && (numNonFuncSensors >= _numSensorFailsForNonFunc)) |
Matt Spinler | ae1f8ef | 2020-10-14 16:15:51 -0500 | [diff] [blame] | 347 | { |
Matthew Barth | 7c23a04 | 2021-01-26 16:21:45 -0600 | [diff] [blame] | 348 | getLogger().log(fmt::format("Setting fan {} to nonfunctional, " |
| 349 | "number of nonfunctional sensors = {}", |
| 350 | _name, numNonFuncSensors)); |
Matt Spinler | ae1f8ef | 2020-10-14 16:15:51 -0500 | [diff] [blame] | 351 | updateInventory(false); |
| 352 | } |
Matt Spinler | b1e1851 | 2017-04-27 14:42:33 -0500 | [diff] [blame] | 353 | } |
Matt Spinler | b63aa09 | 2020-10-14 09:45:11 -0500 | [diff] [blame] | 354 | |
| 355 | _system.fanStatusChange(*this); |
Matt Spinler | a9406a7 | 2017-04-27 14:29:24 -0500 | [diff] [blame] | 356 | } |
| 357 | |
Matt Spinler | b1e1851 | 2017-04-27 14:42:33 -0500 | [diff] [blame] | 358 | void Fan::updateInventory(bool functional) |
| 359 | { |
Matthew Barth | 177fe98 | 2020-05-26 11:05:19 -0500 | [diff] [blame] | 360 | auto objectMap = |
| 361 | util::getObjMap<bool>(_name, util::OPERATIONAL_STATUS_INTF, |
| 362 | util::FUNCTIONAL_PROPERTY, functional); |
Matthew Barth | 51dd185 | 2017-11-16 15:21:13 -0600 | [diff] [blame] | 363 | auto response = util::SDBusPlus::lookupAndCallMethod( |
Matthew Barth | 177fe98 | 2020-05-26 11:05:19 -0500 | [diff] [blame] | 364 | _bus, util::INVENTORY_PATH, util::INVENTORY_INTF, "Notify", objectMap); |
Matt Spinler | b1e1851 | 2017-04-27 14:42:33 -0500 | [diff] [blame] | 365 | if (response.is_method_error()) |
| 366 | { |
| 367 | log<level::ERR>("Error in Notify call to update inventory"); |
| 368 | return; |
| 369 | } |
| 370 | |
Matthew Barth | 177fe98 | 2020-05-26 11:05:19 -0500 | [diff] [blame] | 371 | // This will always track the current state of the inventory. |
Matt Spinler | b1e1851 | 2017-04-27 14:42:33 -0500 | [diff] [blame] | 372 | _functional = functional; |
| 373 | } |
| 374 | |
Matt Spinler | b63aa09 | 2020-10-14 09:45:11 -0500 | [diff] [blame] | 375 | void Fan::presenceChanged(sdbusplus::message::message& msg) |
| 376 | { |
| 377 | std::string interface; |
| 378 | std::map<std::string, std::variant<bool>> properties; |
| 379 | |
| 380 | msg.read(interface, properties); |
| 381 | |
| 382 | auto presentProp = properties.find("Present"); |
| 383 | if (presentProp != properties.end()) |
| 384 | { |
| 385 | _present = std::get<bool>(presentProp->second); |
| 386 | |
Matt Spinler | 27f6b68 | 2020-10-27 08:43:37 -0500 | [diff] [blame] | 387 | getLogger().log( |
Matt Spinler | ac37297 | 2021-01-25 15:11:22 -0600 | [diff] [blame] | 388 | fmt::format("Fan {} presence state change to {}", _name, _present)); |
Matt Spinler | 27f6b68 | 2020-10-27 08:43:37 -0500 | [diff] [blame] | 389 | |
Matt Spinler | b63aa09 | 2020-10-14 09:45:11 -0500 | [diff] [blame] | 390 | _system.fanStatusChange(*this); |
Matt Spinler | 27f6b68 | 2020-10-27 08:43:37 -0500 | [diff] [blame] | 391 | |
| 392 | if (_fanMissingErrorDelay) |
| 393 | { |
Matt Spinler | 7d13564 | 2021-02-04 12:44:17 -0600 | [diff] [blame] | 394 | if (!_present && _system.isPowerOn()) |
Matt Spinler | 27f6b68 | 2020-10-27 08:43:37 -0500 | [diff] [blame] | 395 | { |
| 396 | _fanMissingErrorTimer->restartOnce( |
| 397 | std::chrono::seconds{*_fanMissingErrorDelay}); |
| 398 | } |
Matt Spinler | 7d13564 | 2021-02-04 12:44:17 -0600 | [diff] [blame] | 399 | else if (_present && _fanMissingErrorTimer->isEnabled()) |
Matt Spinler | 27f6b68 | 2020-10-27 08:43:37 -0500 | [diff] [blame] | 400 | { |
| 401 | _fanMissingErrorTimer->setEnabled(false); |
| 402 | } |
| 403 | } |
Matt Spinler | b63aa09 | 2020-10-14 09:45:11 -0500 | [diff] [blame] | 404 | } |
| 405 | } |
Matt Spinler | f13b42e | 2020-10-26 15:29:49 -0500 | [diff] [blame] | 406 | |
| 407 | void Fan::sensorErrorTimerExpired(const TachSensor& sensor) |
| 408 | { |
Matt Spinler | 7d13564 | 2021-02-04 12:44:17 -0600 | [diff] [blame] | 409 | if (_present && _system.isPowerOn()) |
Matt Spinler | f13b42e | 2020-10-26 15:29:49 -0500 | [diff] [blame] | 410 | { |
| 411 | _system.sensorErrorTimerExpired(*this, sensor); |
| 412 | } |
| 413 | } |
| 414 | |
Matt Spinler | 7d13564 | 2021-02-04 12:44:17 -0600 | [diff] [blame] | 415 | void Fan::powerStateChanged(bool powerStateOn) |
| 416 | { |
| 417 | #ifdef MONITOR_USE_JSON |
| 418 | if (powerStateOn) |
| 419 | { |
| 420 | // set all fans back to functional to start with |
| 421 | std::for_each(_sensors.begin(), _sensors.end(), |
| 422 | [](auto& sensor) { sensor->setFunctional(true); }); |
| 423 | |
| 424 | _monitorTimer.restartOnce(std::chrono::seconds(_monitorDelay)); |
| 425 | |
| 426 | if (!_present) |
| 427 | { |
| 428 | getLogger().log( |
| 429 | fmt::format("At power on, fan {} is missing", _name)); |
| 430 | |
| 431 | if (_fanMissingErrorTimer) |
| 432 | { |
| 433 | _fanMissingErrorTimer->restartOnce( |
| 434 | std::chrono::seconds{*_fanMissingErrorDelay}); |
| 435 | } |
| 436 | } |
| 437 | } |
| 438 | else |
| 439 | { |
| 440 | _monitorReady = false; |
| 441 | |
| 442 | if (_monitorTimer.isEnabled()) |
| 443 | { |
| 444 | _monitorTimer.setEnabled(false); |
| 445 | } |
| 446 | |
| 447 | if (_fanMissingErrorTimer && _fanMissingErrorTimer->isEnabled()) |
| 448 | { |
| 449 | _fanMissingErrorTimer->setEnabled(false); |
| 450 | } |
| 451 | |
| 452 | std::for_each(_sensors.begin(), _sensors.end(), [](auto& sensor) { |
| 453 | if (sensor->timerRunning()) |
| 454 | { |
| 455 | sensor->stopTimer(); |
| 456 | } |
| 457 | }); |
| 458 | } |
| 459 | #endif |
| 460 | } |
| 461 | |
Matthew Barth | 177fe98 | 2020-05-26 11:05:19 -0500 | [diff] [blame] | 462 | } // namespace monitor |
| 463 | } // namespace fan |
| 464 | } // namespace phosphor |