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