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 | */ |
Matthew Barth | 177fe98 | 2020-05-26 11:05:19 -0500 | [diff] [blame] | 16 | #include "tach_sensor.hpp" |
| 17 | |
Matt Spinler | abf8da3 | 2017-04-27 14:08:45 -0500 | [diff] [blame] | 18 | #include "fan.hpp" |
Brad Bishop | 2a58e2c | 2017-07-30 13:49:09 -0400 | [diff] [blame] | 19 | #include "sdbusplus.hpp" |
Matthew Barth | 4d98285 | 2017-11-17 09:37:13 -0600 | [diff] [blame] | 20 | #include "utility.hpp" |
Matt Spinler | abf8da3 | 2017-04-27 14:08:45 -0500 | [diff] [blame] | 21 | |
Jolie Ku | 4c3c24f | 2020-09-09 11:01:46 +0800 | [diff] [blame] | 22 | #include <fmt/format.h> |
| 23 | |
Matthew Barth | 177fe98 | 2020-05-26 11:05:19 -0500 | [diff] [blame] | 24 | #include <phosphor-logging/elog.hpp> |
| 25 | #include <phosphor-logging/log.hpp> |
| 26 | |
| 27 | #include <experimental/filesystem> |
| 28 | #include <functional> |
Matthew Barth | 7c23a04 | 2021-01-26 16:21:45 -0600 | [diff] [blame] | 29 | #include <utility> |
Matthew Barth | 177fe98 | 2020-05-26 11:05:19 -0500 | [diff] [blame] | 30 | |
Matt Spinler | abf8da3 | 2017-04-27 14:08:45 -0500 | [diff] [blame] | 31 | namespace phosphor |
| 32 | { |
| 33 | namespace fan |
| 34 | { |
| 35 | namespace monitor |
| 36 | { |
| 37 | |
Matt Spinler | ebaae61 | 2017-04-27 14:21:48 -0500 | [diff] [blame] | 38 | constexpr auto FAN_SENSOR_VALUE_INTF = "xyz.openbmc_project.Sensor.Value"; |
| 39 | constexpr auto FAN_TARGET_PROPERTY = "Target"; |
| 40 | constexpr auto FAN_VALUE_PROPERTY = "Value"; |
| 41 | |
Matthew Barth | 4d98285 | 2017-11-17 09:37:13 -0600 | [diff] [blame] | 42 | using namespace std::experimental::filesystem; |
Matthew Barth | 177fe98 | 2020-05-26 11:05:19 -0500 | [diff] [blame] | 43 | using InternalFailure = |
| 44 | sdbusplus::xyz::openbmc_project::Common::Error::InternalFailure; |
Matt Spinler | ebaae61 | 2017-04-27 14:21:48 -0500 | [diff] [blame] | 45 | |
| 46 | /** |
| 47 | * @brief Helper function to read a property |
| 48 | * |
| 49 | * @param[in] interface - the interface the property is on |
| 50 | * @param[in] propertName - the name of the property |
| 51 | * @param[in] path - the dbus path |
Matt Spinler | ebaae61 | 2017-04-27 14:21:48 -0500 | [diff] [blame] | 52 | * @param[in] bus - the dbus object |
| 53 | * @param[out] value - filled in with the property value |
| 54 | */ |
Matthew Barth | 177fe98 | 2020-05-26 11:05:19 -0500 | [diff] [blame] | 55 | template <typename T> |
| 56 | static void |
| 57 | readProperty(const std::string& interface, const std::string& propertyName, |
| 58 | const std::string& path, sdbusplus::bus::bus& bus, T& value) |
Matt Spinler | ebaae61 | 2017-04-27 14:21:48 -0500 | [diff] [blame] | 59 | { |
Matt Spinler | ebaae61 | 2017-04-27 14:21:48 -0500 | [diff] [blame] | 60 | try |
| 61 | { |
Matthew Barth | 177fe98 | 2020-05-26 11:05:19 -0500 | [diff] [blame] | 62 | value = |
| 63 | util::SDBusPlus::getProperty<T>(bus, path, interface, propertyName); |
Matt Spinler | ebaae61 | 2017-04-27 14:21:48 -0500 | [diff] [blame] | 64 | } |
| 65 | catch (std::exception& e) |
| 66 | { |
| 67 | phosphor::logging::log<phosphor::logging::level::ERR>(e.what()); |
| 68 | } |
| 69 | } |
Matt Spinler | abf8da3 | 2017-04-27 14:08:45 -0500 | [diff] [blame] | 70 | |
Matthew Barth | 177fe98 | 2020-05-26 11:05:19 -0500 | [diff] [blame] | 71 | TachSensor::TachSensor(Mode mode, sdbusplus::bus::bus& bus, Fan& fan, |
| 72 | const std::string& id, bool hasTarget, size_t funcDelay, |
| 73 | const std::string& interface, double factor, |
Jolie Ku | 69f2f48 | 2020-10-21 09:59:43 +0800 | [diff] [blame] | 74 | int64_t offset, size_t method, size_t threshold, |
| 75 | size_t timeout, const std::optional<size_t>& errorDelay, |
William A. Kennington III | 1cfc2f1 | 2018-10-19 17:29:46 -0700 | [diff] [blame] | 76 | const sdeventplus::Event& event) : |
Matt Spinler | abf8da3 | 2017-04-27 14:08:45 -0500 | [diff] [blame] | 77 | _bus(bus), |
Matthew Barth | 177fe98 | 2020-05-26 11:05:19 -0500 | [diff] [blame] | 78 | _fan(fan), _name(FAN_SENSOR_PATH + id), _invName(path(fan.getName()) / id), |
| 79 | _hasTarget(hasTarget), _funcDelay(funcDelay), _interface(interface), |
Jolie Ku | 69f2f48 | 2020-10-21 09:59:43 +0800 | [diff] [blame] | 80 | _factor(factor), _offset(offset), _method(method), _threshold(threshold), |
| 81 | _timeout(timeout), _timerMode(TimerMode::func), |
| 82 | _timer(event, std::bind(&Fan::updateState, &fan, std::ref(*this))), |
Matt Spinler | f13b42e | 2020-10-26 15:29:49 -0500 | [diff] [blame] | 83 | _errorDelay(errorDelay) |
Matt Spinler | abf8da3 | 2017-04-27 14:08:45 -0500 | [diff] [blame] | 84 | { |
Matthew Barth | d199dcd | 2017-11-20 10:36:41 -0600 | [diff] [blame] | 85 | // Start from a known state of functional |
Matthew Barth | 7f7df31 | 2018-01-15 16:27:50 -0600 | [diff] [blame] | 86 | setFunctional(true); |
Brad Bishop | edaeb31 | 2017-07-30 19:38:20 -0400 | [diff] [blame] | 87 | |
Matthew Barth | 0a9fe16 | 2018-01-26 12:53:15 -0600 | [diff] [blame] | 88 | // Load in current Target and Input values when entering monitor mode |
Matt Spinler | b0412d0 | 2020-10-12 16:53:52 -0500 | [diff] [blame] | 89 | #ifndef MONITOR_USE_JSON |
Matthew Barth | 0a9fe16 | 2018-01-26 12:53:15 -0600 | [diff] [blame] | 90 | if (mode != Mode::init) |
Brad Bishop | edaeb31 | 2017-07-30 19:38:20 -0400 | [diff] [blame] | 91 | { |
Matt Spinler | b0412d0 | 2020-10-12 16:53:52 -0500 | [diff] [blame] | 92 | #endif |
Matthew Barth | 0a9fe16 | 2018-01-26 12:53:15 -0600 | [diff] [blame] | 93 | try |
| 94 | { |
Matt Spinler | 4283c5d | 2021-03-01 15:56:00 -0600 | [diff] [blame^] | 95 | updateTachAndTarget(); |
Matthew Barth | 0a9fe16 | 2018-01-26 12:53:15 -0600 | [diff] [blame] | 96 | } |
Matt Spinler | 4283c5d | 2021-03-01 15:56:00 -0600 | [diff] [blame^] | 97 | catch (const std::exception& e) |
Matthew Barth | 0a9fe16 | 2018-01-26 12:53:15 -0600 | [diff] [blame] | 98 | { |
Matt Spinler | 4283c5d | 2021-03-01 15:56:00 -0600 | [diff] [blame^] | 99 | // Until the parent Fan's monitor-ready timer expires, the |
| 100 | // object can be functional with a missing D-bus sensor. |
Matthew Barth | 0a9fe16 | 2018-01-26 12:53:15 -0600 | [diff] [blame] | 101 | } |
| 102 | |
| 103 | auto match = getMatchString(FAN_SENSOR_VALUE_INTF); |
| 104 | |
| 105 | tachSignal = std::make_unique<sdbusplus::server::match::match>( |
Matthew Barth | 177fe98 | 2020-05-26 11:05:19 -0500 | [diff] [blame] | 106 | _bus, match.c_str(), |
| 107 | [this](auto& msg) { this->handleTachChange(msg); }); |
Matthew Barth | 0a9fe16 | 2018-01-26 12:53:15 -0600 | [diff] [blame] | 108 | |
| 109 | if (_hasTarget) |
| 110 | { |
Lei YU | 80f271b | 2018-01-31 15:24:46 +0800 | [diff] [blame] | 111 | match = getMatchString(_interface); |
Matthew Barth | 0a9fe16 | 2018-01-26 12:53:15 -0600 | [diff] [blame] | 112 | |
| 113 | targetSignal = std::make_unique<sdbusplus::server::match::match>( |
Matthew Barth | 177fe98 | 2020-05-26 11:05:19 -0500 | [diff] [blame] | 114 | _bus, match.c_str(), |
| 115 | [this](auto& msg) { this->handleTargetChange(msg); }); |
Matthew Barth | 0a9fe16 | 2018-01-26 12:53:15 -0600 | [diff] [blame] | 116 | } |
Matt Spinler | f13b42e | 2020-10-26 15:29:49 -0500 | [diff] [blame] | 117 | |
| 118 | if (_errorDelay) |
| 119 | { |
| 120 | _errorTimer = std::make_unique< |
| 121 | sdeventplus::utility::Timer<sdeventplus::ClockId::Monotonic>>( |
| 122 | event, std::bind(&Fan::sensorErrorTimerExpired, &fan, |
| 123 | std::ref(*this))); |
| 124 | } |
Matt Spinler | b0412d0 | 2020-10-12 16:53:52 -0500 | [diff] [blame] | 125 | #ifndef MONITOR_USE_JSON |
Brad Bishop | edaeb31 | 2017-07-30 19:38:20 -0400 | [diff] [blame] | 126 | } |
Matt Spinler | b0412d0 | 2020-10-12 16:53:52 -0500 | [diff] [blame] | 127 | #endif |
Matt Spinler | ebaae61 | 2017-04-27 14:21:48 -0500 | [diff] [blame] | 128 | } |
| 129 | |
Matt Spinler | 4283c5d | 2021-03-01 15:56:00 -0600 | [diff] [blame^] | 130 | void TachSensor::updateTachAndTarget() |
| 131 | { |
| 132 | _tachInput = util::SDBusPlus::getProperty<decltype(_tachInput)>( |
| 133 | _bus, _name, FAN_SENSOR_VALUE_INTF, FAN_VALUE_PROPERTY); |
| 134 | |
| 135 | if (_hasTarget) |
| 136 | { |
| 137 | readProperty(_interface, FAN_TARGET_PROPERTY, _name, _bus, _tachTarget); |
| 138 | } |
| 139 | } |
| 140 | |
Matt Spinler | ebaae61 | 2017-04-27 14:21:48 -0500 | [diff] [blame] | 141 | std::string TachSensor::getMatchString(const std::string& interface) |
| 142 | { |
Matthew Barth | 177fe98 | 2020-05-26 11:05:19 -0500 | [diff] [blame] | 143 | return sdbusplus::bus::match::rules::propertiesChanged(_name, interface); |
Matt Spinler | ebaae61 | 2017-04-27 14:21:48 -0500 | [diff] [blame] | 144 | } |
| 145 | |
Matthew Barth | f552ea5 | 2018-01-15 16:22:04 -0600 | [diff] [blame] | 146 | uint64_t TachSensor::getTarget() const |
| 147 | { |
| 148 | if (!_hasTarget) |
| 149 | { |
| 150 | return _fan.findTargetSpeed(); |
| 151 | } |
| 152 | return _tachTarget; |
| 153 | } |
| 154 | |
Matthew Barth | 7c23a04 | 2021-01-26 16:21:45 -0600 | [diff] [blame] | 155 | std::pair<uint64_t, uint64_t> TachSensor::getRange(const size_t deviation) const |
| 156 | { |
| 157 | // Determine min/max range applying the deviation |
| 158 | uint64_t min = getTarget() * (100 - deviation) / 100; |
| 159 | uint64_t max = getTarget() * (100 + deviation) / 100; |
| 160 | |
| 161 | // Adjust the min/max range by applying the factor & offset |
| 162 | min = min * _factor + _offset; |
| 163 | max = max * _factor + _offset; |
| 164 | |
| 165 | return std::make_pair(min, max); |
| 166 | } |
| 167 | |
Matthew Barth | fcb0dbc | 2021-02-10 14:23:39 -0600 | [diff] [blame] | 168 | void TachSensor::processState() |
| 169 | { |
| 170 | _fan.process(*this); |
| 171 | } |
| 172 | |
| 173 | void TachSensor::resetMethod() |
| 174 | { |
| 175 | switch (_method) |
| 176 | { |
| 177 | case MethodMode::timebased: |
| 178 | if (timerRunning()) |
| 179 | { |
| 180 | stopTimer(); |
| 181 | } |
| 182 | break; |
| 183 | case MethodMode::count: |
| 184 | if (_functional) |
| 185 | { |
| 186 | _counter = 0; |
| 187 | } |
| 188 | else |
| 189 | { |
| 190 | _counter = _threshold; |
| 191 | } |
| 192 | break; |
| 193 | } |
| 194 | } |
| 195 | |
Matthew Barth | d199dcd | 2017-11-20 10:36:41 -0600 | [diff] [blame] | 196 | void TachSensor::setFunctional(bool functional) |
| 197 | { |
| 198 | _functional = functional; |
| 199 | updateInventory(_functional); |
Matt Spinler | f13b42e | 2020-10-26 15:29:49 -0500 | [diff] [blame] | 200 | |
| 201 | if (!_errorTimer) |
| 202 | { |
| 203 | return; |
| 204 | } |
| 205 | |
| 206 | if (!_functional) |
| 207 | { |
| 208 | if (_fan.present()) |
| 209 | { |
| 210 | _errorTimer->restartOnce(std::chrono::seconds(*_errorDelay)); |
| 211 | } |
| 212 | } |
| 213 | else if (_errorTimer->isEnabled()) |
| 214 | { |
| 215 | _errorTimer->setEnabled(false); |
| 216 | } |
Matthew Barth | d199dcd | 2017-11-20 10:36:41 -0600 | [diff] [blame] | 217 | } |
Matt Spinler | ebaae61 | 2017-04-27 14:21:48 -0500 | [diff] [blame] | 218 | |
Brad Bishop | 771659f | 2017-07-30 19:52:21 -0400 | [diff] [blame] | 219 | void TachSensor::handleTargetChange(sdbusplus::message::message& msg) |
Matt Spinler | ebaae61 | 2017-04-27 14:21:48 -0500 | [diff] [blame] | 220 | { |
Matthew Barth | 177fe98 | 2020-05-26 11:05:19 -0500 | [diff] [blame] | 221 | readPropertyFromMessage(msg, _interface, FAN_TARGET_PROPERTY, _tachTarget); |
Matt Spinler | ebaae61 | 2017-04-27 14:21:48 -0500 | [diff] [blame] | 222 | |
Matthew Barth | 177fe98 | 2020-05-26 11:05:19 -0500 | [diff] [blame] | 223 | // Check all tach sensors on the fan against the target |
Matt Spinler | ebaae61 | 2017-04-27 14:21:48 -0500 | [diff] [blame] | 224 | _fan.tachChanged(); |
| 225 | } |
| 226 | |
Brad Bishop | 771659f | 2017-07-30 19:52:21 -0400 | [diff] [blame] | 227 | void TachSensor::handleTachChange(sdbusplus::message::message& msg) |
Matt Spinler | ebaae61 | 2017-04-27 14:21:48 -0500 | [diff] [blame] | 228 | { |
Matthew Barth | 177fe98 | 2020-05-26 11:05:19 -0500 | [diff] [blame] | 229 | readPropertyFromMessage(msg, FAN_SENSOR_VALUE_INTF, FAN_VALUE_PROPERTY, |
| 230 | _tachInput); |
Matt Spinler | ebaae61 | 2017-04-27 14:21:48 -0500 | [diff] [blame] | 231 | |
Matthew Barth | 177fe98 | 2020-05-26 11:05:19 -0500 | [diff] [blame] | 232 | // Check just this sensor against the target |
| 233 | _fan.tachChanged(*this); |
Matt Spinler | abf8da3 | 2017-04-27 14:08:45 -0500 | [diff] [blame] | 234 | } |
| 235 | |
Matthew Barth | 3800ae7 | 2018-02-19 16:08:04 -0600 | [diff] [blame] | 236 | void TachSensor::startTimer(TimerMode mode) |
| 237 | { |
Matthew Barth | 0d0e355 | 2021-01-27 12:31:28 -0600 | [diff] [blame] | 238 | using namespace std::chrono; |
| 239 | |
William A. Kennington III | 8fd879f | 2018-10-30 19:49:29 -0700 | [diff] [blame] | 240 | if (!timerRunning() || mode != _timerMode) |
Matthew Barth | 3800ae7 | 2018-02-19 16:08:04 -0600 | [diff] [blame] | 241 | { |
Matthew Barth | 0d0e355 | 2021-01-27 12:31:28 -0600 | [diff] [blame] | 242 | log<level::INFO>( |
| 243 | fmt::format("Start timer({}) on tach sensor {}. [delay = {}s]", |
| 244 | mode, _name, |
| 245 | duration_cast<seconds>(getDelay(mode)).count()) |
| 246 | .c_str()); |
William A. Kennington III | 8fd879f | 2018-10-30 19:49:29 -0700 | [diff] [blame] | 247 | _timer.restartOnce(getDelay(mode)); |
Matthew Barth | 3800ae7 | 2018-02-19 16:08:04 -0600 | [diff] [blame] | 248 | _timerMode = mode; |
| 249 | } |
Matthew Barth | 3800ae7 | 2018-02-19 16:08:04 -0600 | [diff] [blame] | 250 | } |
Matt Spinler | abf8da3 | 2017-04-27 14:08:45 -0500 | [diff] [blame] | 251 | |
Matthew Barth | 3800ae7 | 2018-02-19 16:08:04 -0600 | [diff] [blame] | 252 | std::chrono::microseconds TachSensor::getDelay(TimerMode mode) |
Matt Spinler | a9406a7 | 2017-04-27 14:29:24 -0500 | [diff] [blame] | 253 | { |
| 254 | using namespace std::chrono; |
| 255 | |
Matthew Barth | 177fe98 | 2020-05-26 11:05:19 -0500 | [diff] [blame] | 256 | switch (mode) |
Matthew Barth | 3800ae7 | 2018-02-19 16:08:04 -0600 | [diff] [blame] | 257 | { |
Matthew Barth | 177fe98 | 2020-05-26 11:05:19 -0500 | [diff] [blame] | 258 | case TimerMode::nonfunc: |
| 259 | return duration_cast<microseconds>(seconds(_timeout)); |
| 260 | case TimerMode::func: |
| 261 | return duration_cast<microseconds>(seconds(_funcDelay)); |
| 262 | default: |
| 263 | // Log an internal error for undefined timer mode |
| 264 | log<level::ERR>("Undefined timer mode", |
| 265 | entry("TIMER_MODE=%u", mode)); |
| 266 | elog<InternalFailure>(); |
| 267 | return duration_cast<microseconds>(seconds(0)); |
Matthew Barth | 3800ae7 | 2018-02-19 16:08:04 -0600 | [diff] [blame] | 268 | } |
Matt Spinler | a9406a7 | 2017-04-27 14:29:24 -0500 | [diff] [blame] | 269 | } |
| 270 | |
Jolie Ku | 69f2f48 | 2020-10-21 09:59:43 +0800 | [diff] [blame] | 271 | void TachSensor::setCounter(bool count) |
| 272 | { |
| 273 | if (count) |
| 274 | { |
| 275 | if (_counter < _threshold) |
| 276 | { |
| 277 | ++_counter; |
| 278 | } |
| 279 | } |
| 280 | else |
| 281 | { |
| 282 | if (_counter > 0) |
| 283 | { |
| 284 | --_counter; |
| 285 | } |
| 286 | } |
| 287 | } |
| 288 | |
Matthew Barth | 4d98285 | 2017-11-17 09:37:13 -0600 | [diff] [blame] | 289 | void TachSensor::updateInventory(bool functional) |
| 290 | { |
Matthew Barth | 177fe98 | 2020-05-26 11:05:19 -0500 | [diff] [blame] | 291 | auto objectMap = |
| 292 | util::getObjMap<bool>(_invName, util::OPERATIONAL_STATUS_INTF, |
| 293 | util::FUNCTIONAL_PROPERTY, functional); |
Matthew Barth | 4d98285 | 2017-11-17 09:37:13 -0600 | [diff] [blame] | 294 | auto response = util::SDBusPlus::lookupAndCallMethod( |
Matthew Barth | 177fe98 | 2020-05-26 11:05:19 -0500 | [diff] [blame] | 295 | _bus, util::INVENTORY_PATH, util::INVENTORY_INTF, "Notify", objectMap); |
Matthew Barth | 4d98285 | 2017-11-17 09:37:13 -0600 | [diff] [blame] | 296 | if (response.is_method_error()) |
| 297 | { |
| 298 | log<level::ERR>("Error in notify update of tach sensor inventory"); |
| 299 | } |
| 300 | } |
Matt Spinler | a9406a7 | 2017-04-27 14:29:24 -0500 | [diff] [blame] | 301 | |
Matthew Barth | 177fe98 | 2020-05-26 11:05:19 -0500 | [diff] [blame] | 302 | } // namespace monitor |
| 303 | } // namespace fan |
| 304 | } // namespace phosphor |