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 | |
| 18 | #include "sdbusplus.hpp" |
Matt Spinler | abf8da3 | 2017-04-27 14:08:45 -0500 | [diff] [blame] | 19 | #include "types.hpp" |
Matt Spinler | b1e1851 | 2017-04-27 14:42:33 -0500 | [diff] [blame] | 20 | #include "utility.hpp" |
Matthew Barth | 177fe98 | 2020-05-26 11:05:19 -0500 | [diff] [blame] | 21 | |
| 22 | #include <phosphor-logging/log.hpp> |
| 23 | |
| 24 | #include <algorithm> |
Matt Spinler | abf8da3 | 2017-04-27 14:08:45 -0500 | [diff] [blame] | 25 | |
| 26 | namespace phosphor |
| 27 | { |
| 28 | namespace fan |
| 29 | { |
| 30 | namespace monitor |
| 31 | { |
| 32 | |
| 33 | using namespace phosphor::logging; |
| 34 | |
Matthew Barth | 177fe98 | 2020-05-26 11:05:19 -0500 | [diff] [blame] | 35 | Fan::Fan(Mode mode, sdbusplus::bus::bus& bus, const sdeventplus::Event& event, |
| 36 | std::unique_ptr<trust::Manager>& trust, const FanDefinition& def) : |
Matt Spinler | abf8da3 | 2017-04-27 14:08:45 -0500 | [diff] [blame] | 37 | _bus(bus), |
| 38 | _name(std::get<fanNameField>(def)), |
| 39 | _deviation(std::get<fanDeviationField>(def)), |
Matt Spinler | c39e859 | 2017-09-28 13:13:08 -0500 | [diff] [blame] | 40 | _numSensorFailsForNonFunc(std::get<numSensorFailsForNonfuncField>(def)), |
| 41 | _trustManager(trust) |
Matt Spinler | abf8da3 | 2017-04-27 14:08:45 -0500 | [diff] [blame] | 42 | { |
Jolie Ku | 4c3c24f | 2020-09-09 11:01:46 +0800 | [diff] [blame^] | 43 | // Start from a known state of functional |
| 44 | updateInventory(true); |
| 45 | |
Matthew Barth | 0a9fe16 | 2018-01-26 12:53:15 -0600 | [diff] [blame] | 46 | // Setup tach sensors for monitoring |
| 47 | auto& sensors = std::get<sensorListField>(def); |
| 48 | for (auto& s : sensors) |
| 49 | { |
| 50 | try |
| 51 | { |
Matthew Barth | 177fe98 | 2020-05-26 11:05:19 -0500 | [diff] [blame] | 52 | _sensors.emplace_back(std::make_shared<TachSensor>( |
| 53 | mode, bus, *this, std::get<sensorNameField>(s), |
| 54 | std::get<hasTargetField>(s), std::get<funcDelay>(def), |
| 55 | std::get<targetInterfaceField>(s), std::get<factorField>(s), |
| 56 | std::get<offsetField>(s), std::get<timeoutField>(def), event)); |
Matthew Barth | 0a9fe16 | 2018-01-26 12:53:15 -0600 | [diff] [blame] | 57 | |
| 58 | _trustManager->registerSensor(_sensors.back()); |
| 59 | } |
| 60 | catch (InvalidSensorError& e) |
Jolie Ku | 4c3c24f | 2020-09-09 11:01:46 +0800 | [diff] [blame^] | 61 | { |
| 62 | // mark associated fan as nonfunctional |
| 63 | updateInventory(false); |
| 64 | } |
Matthew Barth | 0a9fe16 | 2018-01-26 12:53:15 -0600 | [diff] [blame] | 65 | } |
| 66 | |
Matthew Barth | 0a9fe16 | 2018-01-26 12:53:15 -0600 | [diff] [blame] | 67 | // Check current tach state when entering monitor mode |
Matthew Barth | 6ad2843 | 2017-08-22 11:18:19 -0500 | [diff] [blame] | 68 | if (mode != Mode::init) |
| 69 | { |
Matthew Barth | 177fe98 | 2020-05-26 11:05:19 -0500 | [diff] [blame] | 70 | // The TachSensors will now have already read the input |
| 71 | // and target values, so check them. |
Matthew Barth | 6ad2843 | 2017-08-22 11:18:19 -0500 | [diff] [blame] | 72 | tachChanged(); |
| 73 | } |
Matt Spinler | abf8da3 | 2017-04-27 14:08:45 -0500 | [diff] [blame] | 74 | } |
| 75 | |
Matt Spinler | ebaae61 | 2017-04-27 14:21:48 -0500 | [diff] [blame] | 76 | void Fan::tachChanged() |
| 77 | { |
| 78 | for (auto& s : _sensors) |
| 79 | { |
| 80 | tachChanged(*s); |
| 81 | } |
| 82 | } |
| 83 | |
Matt Spinler | ebaae61 | 2017-04-27 14:21:48 -0500 | [diff] [blame] | 84 | void Fan::tachChanged(TachSensor& sensor) |
| 85 | { |
Matt Spinler | c39e859 | 2017-09-28 13:13:08 -0500 | [diff] [blame] | 86 | if (_trustManager->active()) |
| 87 | { |
| 88 | if (!_trustManager->checkTrust(sensor)) |
| 89 | { |
| 90 | return; |
| 91 | } |
| 92 | } |
| 93 | |
Matthew Barth | 177fe98 | 2020-05-26 11:05:19 -0500 | [diff] [blame] | 94 | // If this sensor is out of range at this moment, start |
| 95 | // its timer, at the end of which the inventory |
| 96 | // for the fan may get updated to not functional. |
Matt Spinler | a4c8f1f | 2017-04-27 14:38:38 -0500 | [diff] [blame] | 97 | |
Matthew Barth | 177fe98 | 2020-05-26 11:05:19 -0500 | [diff] [blame] | 98 | // If this sensor is OK, put everything back into a good state. |
Matt Spinler | a4c8f1f | 2017-04-27 14:38:38 -0500 | [diff] [blame] | 99 | |
| 100 | if (outOfRange(sensor)) |
| 101 | { |
Matthew Barth | e11cbc6 | 2018-02-20 12:11:07 -0600 | [diff] [blame] | 102 | if (sensor.functional()) |
Matt Spinler | a4c8f1f | 2017-04-27 14:38:38 -0500 | [diff] [blame] | 103 | { |
Matthew Barth | e11cbc6 | 2018-02-20 12:11:07 -0600 | [diff] [blame] | 104 | // Start nonfunctional timer if not already running |
Matthew Barth | 3800ae7 | 2018-02-19 16:08:04 -0600 | [diff] [blame] | 105 | sensor.startTimer(TimerMode::nonfunc); |
Matt Spinler | a4c8f1f | 2017-04-27 14:38:38 -0500 | [diff] [blame] | 106 | } |
| 107 | } |
| 108 | else |
| 109 | { |
Matthew Barth | e11cbc6 | 2018-02-20 12:11:07 -0600 | [diff] [blame] | 110 | if (sensor.functional()) |
Matt Spinler | a4c8f1f | 2017-04-27 14:38:38 -0500 | [diff] [blame] | 111 | { |
Matt Spinler | 6fa181c | 2017-09-27 16:24:45 -0500 | [diff] [blame] | 112 | sensor.stopTimer(); |
Matt Spinler | a4c8f1f | 2017-04-27 14:38:38 -0500 | [diff] [blame] | 113 | } |
Matthew Barth | e11cbc6 | 2018-02-20 12:11:07 -0600 | [diff] [blame] | 114 | else |
Matt Spinler | a4c8f1f | 2017-04-27 14:38:38 -0500 | [diff] [blame] | 115 | { |
Matthew Barth | e11cbc6 | 2018-02-20 12:11:07 -0600 | [diff] [blame] | 116 | // Start functional timer if not already running |
| 117 | sensor.startTimer(TimerMode::func); |
Matt Spinler | a4c8f1f | 2017-04-27 14:38:38 -0500 | [diff] [blame] | 118 | } |
| 119 | } |
Matt Spinler | ebaae61 | 2017-04-27 14:21:48 -0500 | [diff] [blame] | 120 | } |
| 121 | |
Matthew Barth | f552ea5 | 2018-01-15 16:22:04 -0600 | [diff] [blame] | 122 | uint64_t Fan::findTargetSpeed() |
Matt Spinler | abf8da3 | 2017-04-27 14:08:45 -0500 | [diff] [blame] | 123 | { |
| 124 | uint64_t target = 0; |
Matthew Barth | 177fe98 | 2020-05-26 11:05:19 -0500 | [diff] [blame] | 125 | // The sensor doesn't support a target, |
| 126 | // so get it from another sensor. |
Matthew Barth | f552ea5 | 2018-01-15 16:22:04 -0600 | [diff] [blame] | 127 | auto s = std::find_if(_sensors.begin(), _sensors.end(), |
Matthew Barth | 177fe98 | 2020-05-26 11:05:19 -0500 | [diff] [blame] | 128 | [](const auto& s) { return s->hasTarget(); }); |
Matt Spinler | abf8da3 | 2017-04-27 14:08:45 -0500 | [diff] [blame] | 129 | |
Matthew Barth | f552ea5 | 2018-01-15 16:22:04 -0600 | [diff] [blame] | 130 | if (s != _sensors.end()) |
Matt Spinler | abf8da3 | 2017-04-27 14:08:45 -0500 | [diff] [blame] | 131 | { |
Matthew Barth | f552ea5 | 2018-01-15 16:22:04 -0600 | [diff] [blame] | 132 | target = (*s)->getTarget(); |
Matt Spinler | abf8da3 | 2017-04-27 14:08:45 -0500 | [diff] [blame] | 133 | } |
| 134 | |
| 135 | return target; |
| 136 | } |
| 137 | |
Matt Spinler | abf8da3 | 2017-04-27 14:08:45 -0500 | [diff] [blame] | 138 | bool Fan::tooManySensorsNonfunctional() |
| 139 | { |
Matthew Barth | 177fe98 | 2020-05-26 11:05:19 -0500 | [diff] [blame] | 140 | size_t numFailed = |
| 141 | std::count_if(_sensors.begin(), _sensors.end(), |
| 142 | [](const auto& s) { return !s->functional(); }); |
Matt Spinler | abf8da3 | 2017-04-27 14:08:45 -0500 | [diff] [blame] | 143 | |
| 144 | return (numFailed >= _numSensorFailsForNonFunc); |
| 145 | } |
| 146 | |
Matt Spinler | abf8da3 | 2017-04-27 14:08:45 -0500 | [diff] [blame] | 147 | bool Fan::outOfRange(const TachSensor& sensor) |
| 148 | { |
| 149 | auto actual = static_cast<uint64_t>(sensor.getInput()); |
Matthew Barth | 5008daf | 2018-01-15 16:38:24 -0600 | [diff] [blame] | 150 | auto target = sensor.getTarget(); |
Lei YU | 8e5d197 | 2018-01-26 17:14:00 +0800 | [diff] [blame] | 151 | auto factor = sensor.getFactor(); |
| 152 | auto offset = sensor.getOffset(); |
Matt Spinler | abf8da3 | 2017-04-27 14:08:45 -0500 | [diff] [blame] | 153 | |
| 154 | uint64_t min = target * (100 - _deviation) / 100; |
| 155 | uint64_t max = target * (100 + _deviation) / 100; |
| 156 | |
Lei YU | 8e5d197 | 2018-01-26 17:14:00 +0800 | [diff] [blame] | 157 | // TODO: openbmc/openbmc#2937 enhance this function |
| 158 | // either by making it virtual, or by predefining different |
| 159 | // outOfRange ops and selecting by yaml config |
| 160 | min = min * factor + offset; |
| 161 | max = max * factor + offset; |
Matt Spinler | abf8da3 | 2017-04-27 14:08:45 -0500 | [diff] [blame] | 162 | if ((actual < min) || (actual > max)) |
| 163 | { |
| 164 | return true; |
| 165 | } |
| 166 | |
| 167 | return false; |
| 168 | } |
| 169 | |
Matt Spinler | a9406a7 | 2017-04-27 14:29:24 -0500 | [diff] [blame] | 170 | void Fan::timerExpired(TachSensor& sensor) |
| 171 | { |
Matthew Barth | e11cbc6 | 2018-02-20 12:11:07 -0600 | [diff] [blame] | 172 | sensor.setFunctional(!sensor.functional()); |
| 173 | |
Matthew Barth | 177fe98 | 2020-05-26 11:05:19 -0500 | [diff] [blame] | 174 | // If the fan was nonfunctional and enough sensors are now OK, |
| 175 | // the fan can go back to functional |
Matthew Barth | e11cbc6 | 2018-02-20 12:11:07 -0600 | [diff] [blame] | 176 | if (!_functional && !tooManySensorsNonfunctional()) |
| 177 | { |
| 178 | log<level::INFO>("Setting a fan back to functional", |
| 179 | entry("FAN=%s", _name.c_str())); |
| 180 | |
| 181 | updateInventory(true); |
| 182 | } |
Matt Spinler | a9406a7 | 2017-04-27 14:29:24 -0500 | [diff] [blame] | 183 | |
Matthew Barth | 177fe98 | 2020-05-26 11:05:19 -0500 | [diff] [blame] | 184 | // If the fan is currently functional, but too many |
| 185 | // contained sensors are now nonfunctional, update |
| 186 | // the whole fan nonfunctional. |
Matt Spinler | b1e1851 | 2017-04-27 14:42:33 -0500 | [diff] [blame] | 187 | if (_functional && tooManySensorsNonfunctional()) |
| 188 | { |
| 189 | log<level::ERR>("Setting a fan to nonfunctional", |
Matthew Barth | 177fe98 | 2020-05-26 11:05:19 -0500 | [diff] [blame] | 190 | entry("FAN=%s", _name.c_str()), |
| 191 | entry("TACH_SENSOR=%s", sensor.name().c_str()), |
| 192 | entry("ACTUAL_SPEED=%lld", sensor.getInput()), |
| 193 | entry("TARGET_SPEED=%lld", sensor.getTarget())); |
Matt Spinler | b1e1851 | 2017-04-27 14:42:33 -0500 | [diff] [blame] | 194 | |
| 195 | updateInventory(false); |
| 196 | } |
Matt Spinler | a9406a7 | 2017-04-27 14:29:24 -0500 | [diff] [blame] | 197 | } |
| 198 | |
Matt Spinler | b1e1851 | 2017-04-27 14:42:33 -0500 | [diff] [blame] | 199 | void Fan::updateInventory(bool functional) |
| 200 | { |
Matthew Barth | 177fe98 | 2020-05-26 11:05:19 -0500 | [diff] [blame] | 201 | auto objectMap = |
| 202 | util::getObjMap<bool>(_name, util::OPERATIONAL_STATUS_INTF, |
| 203 | util::FUNCTIONAL_PROPERTY, functional); |
Matthew Barth | 51dd185 | 2017-11-16 15:21:13 -0600 | [diff] [blame] | 204 | auto response = util::SDBusPlus::lookupAndCallMethod( |
Matthew Barth | 177fe98 | 2020-05-26 11:05:19 -0500 | [diff] [blame] | 205 | _bus, util::INVENTORY_PATH, util::INVENTORY_INTF, "Notify", objectMap); |
Matt Spinler | b1e1851 | 2017-04-27 14:42:33 -0500 | [diff] [blame] | 206 | if (response.is_method_error()) |
| 207 | { |
| 208 | log<level::ERR>("Error in Notify call to update inventory"); |
| 209 | return; |
| 210 | } |
| 211 | |
Matthew Barth | 177fe98 | 2020-05-26 11:05:19 -0500 | [diff] [blame] | 212 | // This will always track the current state of the inventory. |
Matt Spinler | b1e1851 | 2017-04-27 14:42:33 -0500 | [diff] [blame] | 213 | _functional = functional; |
| 214 | } |
| 215 | |
Matthew Barth | 177fe98 | 2020-05-26 11:05:19 -0500 | [diff] [blame] | 216 | } // namespace monitor |
| 217 | } // namespace fan |
| 218 | } // namespace phosphor |