Matt Spinler | abf8da3 | 2017-04-27 14:08:45 -0500 | [diff] [blame] | 1 | #pragma once |
| 2 | |
Matt Spinler | b0412d0 | 2020-10-12 16:53:52 -0500 | [diff] [blame] | 3 | #include "config.h" |
| 4 | |
Matt Spinler | abf8da3 | 2017-04-27 14:08:45 -0500 | [diff] [blame] | 5 | #include "tach_sensor.hpp" |
Matt Spinler | c39e859 | 2017-09-28 13:13:08 -0500 | [diff] [blame] | 6 | #include "trust_manager.hpp" |
Matt Spinler | abf8da3 | 2017-04-27 14:08:45 -0500 | [diff] [blame] | 7 | #include "types.hpp" |
| 8 | |
Matthew Barth | 177fe98 | 2020-05-26 11:05:19 -0500 | [diff] [blame] | 9 | #include <sdbusplus/bus.hpp> |
| 10 | #include <sdeventplus/event.hpp> |
| 11 | |
| 12 | #include <tuple> |
| 13 | #include <vector> |
| 14 | |
Matt Spinler | abf8da3 | 2017-04-27 14:08:45 -0500 | [diff] [blame] | 15 | namespace phosphor |
| 16 | { |
| 17 | namespace fan |
| 18 | { |
| 19 | namespace monitor |
| 20 | { |
| 21 | |
Matt Spinler | b0412d0 | 2020-10-12 16:53:52 -0500 | [diff] [blame] | 22 | class System; |
| 23 | |
Brad Bishop | edaeb31 | 2017-07-30 19:38:20 -0400 | [diff] [blame] | 24 | /** |
| 25 | * @class InvalidSensorError |
| 26 | * |
| 27 | * An exception type for sensors that don't exist or |
| 28 | * are otherwise inaccessible. |
| 29 | */ |
Matthew Barth | 177fe98 | 2020-05-26 11:05:19 -0500 | [diff] [blame] | 30 | class InvalidSensorError : public std::exception |
| 31 | {}; |
Matt Spinler | abf8da3 | 2017-04-27 14:08:45 -0500 | [diff] [blame] | 32 | |
| 33 | /** |
| 34 | * @class Fan |
| 35 | * |
| 36 | * Represents a fan, which can contain 1 or more sensors which |
| 37 | * loosely correspond to rotors. See below. |
| 38 | * |
| 39 | * There is a sensor when hwmon exposes one, which means there is a |
| 40 | * speed value to be read. Sometimes there is a sensor per rotor, |
| 41 | * and other times multiple rotors just use 1 sensor total where |
| 42 | * the sensor reports the slowest speed of all of the rotors. |
| 43 | * |
| 44 | * A rotor's speed is set by writing the Target value of a sensor. |
| 45 | * Sometimes each sensor in a fan supports having a Target, and other |
| 46 | * times not all of them do. A TachSensor object knows if it supports |
| 47 | * the Target property. |
| 48 | * |
| 49 | * The strategy for monitoring fan speeds is as follows: |
| 50 | * |
| 51 | * Every time a Target (new speed written) or Input (actual speed read) |
| 52 | * sensor changes, check if the input value is within some range of the target |
| 53 | * value. If it isn't, start a timer at the end of which the sensor will be |
| 54 | * set to not functional. If enough sensors in the fan are now nonfunctional, |
| 55 | * set the whole fan to nonfunctional in the inventory. |
| 56 | * |
| 57 | * When sensor inputs come back within a specified range of the target, |
| 58 | * stop its timer if running, make the sensor functional again if it wasn't, |
| 59 | * and if enough sensors in the fan are now functional set the whole fan |
| 60 | * back to functional in the inventory. |
| 61 | */ |
| 62 | class Fan |
| 63 | { |
Matt Spinler | b1e1851 | 2017-04-27 14:42:33 -0500 | [diff] [blame] | 64 | using Property = std::string; |
Patrick Williams | c21d0b3 | 2020-05-13 17:55:14 -0500 | [diff] [blame] | 65 | using Value = std::variant<bool>; |
Matt Spinler | b1e1851 | 2017-04-27 14:42:33 -0500 | [diff] [blame] | 66 | using PropertyMap = std::map<Property, Value>; |
| 67 | |
| 68 | using Interface = std::string; |
| 69 | using InterfaceMap = std::map<Interface, PropertyMap>; |
| 70 | |
| 71 | using Object = sdbusplus::message::object_path; |
| 72 | using ObjectMap = std::map<Object, InterfaceMap>; |
Matt Spinler | abf8da3 | 2017-04-27 14:08:45 -0500 | [diff] [blame] | 73 | |
Matthew Barth | 177fe98 | 2020-05-26 11:05:19 -0500 | [diff] [blame] | 74 | public: |
| 75 | Fan() = delete; |
| 76 | Fan(const Fan&) = delete; |
| 77 | Fan(Fan&&) = default; |
| 78 | Fan& operator=(const Fan&) = delete; |
| 79 | Fan& operator=(Fan&&) = default; |
| 80 | ~Fan() = default; |
Matt Spinler | abf8da3 | 2017-04-27 14:08:45 -0500 | [diff] [blame] | 81 | |
Matthew Barth | 177fe98 | 2020-05-26 11:05:19 -0500 | [diff] [blame] | 82 | /** |
| 83 | * @brief Constructor |
| 84 | * |
| 85 | * @param mode - mode of fan monitor |
| 86 | * @param bus - the dbus object |
| 87 | * @param event - event loop reference |
| 88 | * @param trust - the tach trust manager |
| 89 | * @param def - the fan definition structure |
Matt Spinler | b0412d0 | 2020-10-12 16:53:52 -0500 | [diff] [blame] | 90 | * @param system - Reference to the system object |
Matthew Barth | 177fe98 | 2020-05-26 11:05:19 -0500 | [diff] [blame] | 91 | */ |
| 92 | Fan(Mode mode, sdbusplus::bus::bus& bus, const sdeventplus::Event& event, |
Matt Spinler | b0412d0 | 2020-10-12 16:53:52 -0500 | [diff] [blame] | 93 | std::unique_ptr<trust::Manager>& trust, const FanDefinition& def, |
| 94 | System& system); |
Matt Spinler | abf8da3 | 2017-04-27 14:08:45 -0500 | [diff] [blame] | 95 | |
Matthew Barth | 177fe98 | 2020-05-26 11:05:19 -0500 | [diff] [blame] | 96 | /** |
| 97 | * @brief Callback function for when an input sensor changes |
| 98 | * |
| 99 | * Starts a timer, where if it expires then the sensor |
| 100 | * was out of range for too long and can be considered not functional. |
| 101 | */ |
| 102 | void tachChanged(TachSensor& sensor); |
Matt Spinler | abf8da3 | 2017-04-27 14:08:45 -0500 | [diff] [blame] | 103 | |
Matthew Barth | 177fe98 | 2020-05-26 11:05:19 -0500 | [diff] [blame] | 104 | /** |
| 105 | * @brief Calls tachChanged(sensor) on each sensor |
| 106 | */ |
| 107 | void tachChanged(); |
Matt Spinler | ebaae61 | 2017-04-27 14:21:48 -0500 | [diff] [blame] | 108 | |
Matthew Barth | 177fe98 | 2020-05-26 11:05:19 -0500 | [diff] [blame] | 109 | /** |
Jolie Ku | 69f2f48 | 2020-10-21 09:59:43 +0800 | [diff] [blame] | 110 | * @brief The callback function for the method |
Matthew Barth | 177fe98 | 2020-05-26 11:05:19 -0500 | [diff] [blame] | 111 | * |
| 112 | * Sets the sensor to not functional. |
| 113 | * If enough sensors are now not functional, |
| 114 | * updates the functional status of the whole |
| 115 | * fan in the inventory. |
| 116 | * |
Jolie Ku | 69f2f48 | 2020-10-21 09:59:43 +0800 | [diff] [blame] | 117 | * @param[in] sensor - the sensor for state update |
Matthew Barth | 177fe98 | 2020-05-26 11:05:19 -0500 | [diff] [blame] | 118 | */ |
Jolie Ku | 69f2f48 | 2020-10-21 09:59:43 +0800 | [diff] [blame] | 119 | void updateState(TachSensor& sensor); |
Matt Spinler | abf8da3 | 2017-04-27 14:08:45 -0500 | [diff] [blame] | 120 | |
Matthew Barth | 177fe98 | 2020-05-26 11:05:19 -0500 | [diff] [blame] | 121 | /** |
| 122 | * @brief Get the name of the fan |
| 123 | * |
| 124 | * @return - The fan name |
| 125 | */ |
| 126 | inline const std::string& getName() const |
| 127 | { |
| 128 | return _name; |
| 129 | } |
Matt Spinler | a9406a7 | 2017-04-27 14:29:24 -0500 | [diff] [blame] | 130 | |
Matthew Barth | 177fe98 | 2020-05-26 11:05:19 -0500 | [diff] [blame] | 131 | /** |
| 132 | * @brief Finds the target speed of this fan |
| 133 | * |
| 134 | * Finds the target speed from the list of sensors that make up this |
| 135 | * fan. At least one sensor should contain a target speed value. |
| 136 | * |
| 137 | * @return - The target speed found from the list of sensors on the fan |
| 138 | */ |
| 139 | uint64_t findTargetSpeed(); |
Matthew Barth | 4d98285 | 2017-11-17 09:37:13 -0600 | [diff] [blame] | 140 | |
Matt Spinler | b63aa09 | 2020-10-14 09:45:11 -0500 | [diff] [blame] | 141 | /** |
| 142 | * @brief Returns the contained TachSensor objects |
| 143 | * |
| 144 | * @return std::vector<std::shared_ptr<TachSensor>> - The sensors |
| 145 | */ |
| 146 | const std::vector<std::shared_ptr<TachSensor>>& sensors() const |
| 147 | { |
| 148 | return _sensors; |
| 149 | } |
| 150 | |
| 151 | /** |
| 152 | * @brief Returns the presence status of the fan |
| 153 | * |
| 154 | * @return bool - If the fan is present or not |
| 155 | */ |
| 156 | bool present() const |
| 157 | { |
| 158 | return _present; |
| 159 | } |
| 160 | |
Matt Spinler | f13b42e | 2020-10-26 15:29:49 -0500 | [diff] [blame] | 161 | /** |
| 162 | * @brief Called from TachSensor when its error timer expires |
| 163 | * so an event log calling out the fan can be created. |
| 164 | * |
| 165 | * @param[in] sensor - The nonfunctional sensor |
| 166 | */ |
| 167 | void sensorErrorTimerExpired(const TachSensor& sensor); |
| 168 | |
Matthew Barth | 177fe98 | 2020-05-26 11:05:19 -0500 | [diff] [blame] | 169 | private: |
| 170 | /** |
| 171 | * @brief Returns true if the sensor input is not within |
| 172 | * some deviation of the target. |
| 173 | * |
| 174 | * @param[in] sensor - the sensor to check |
| 175 | */ |
| 176 | bool outOfRange(const TachSensor& sensor); |
Matthew Barth | f552ea5 | 2018-01-15 16:22:04 -0600 | [diff] [blame] | 177 | |
Matthew Barth | 177fe98 | 2020-05-26 11:05:19 -0500 | [diff] [blame] | 178 | /** |
| 179 | * @brief Returns true if too many sensors are nonfunctional |
| 180 | * as defined by _numSensorFailsForNonFunc |
| 181 | */ |
| 182 | bool tooManySensorsNonfunctional(); |
Matt Spinler | abf8da3 | 2017-04-27 14:08:45 -0500 | [diff] [blame] | 183 | |
Matthew Barth | 177fe98 | 2020-05-26 11:05:19 -0500 | [diff] [blame] | 184 | /** |
| 185 | * @brief Updates the Functional property in the inventory |
| 186 | * for the fan based on the value passed in. |
| 187 | * |
| 188 | * @param[in] functional - If the Functional property should |
| 189 | * be set to true or false. |
| 190 | */ |
| 191 | void updateInventory(bool functional); |
Matt Spinler | abf8da3 | 2017-04-27 14:08:45 -0500 | [diff] [blame] | 192 | |
Matthew Barth | 177fe98 | 2020-05-26 11:05:19 -0500 | [diff] [blame] | 193 | /** |
Matt Spinler | b0412d0 | 2020-10-12 16:53:52 -0500 | [diff] [blame] | 194 | * @brief Called by _monitorTimer to start fan monitoring some |
| 195 | * amount of time after startup. |
| 196 | */ |
| 197 | void startMonitor(); |
| 198 | |
| 199 | /** |
Matt Spinler | b63aa09 | 2020-10-14 09:45:11 -0500 | [diff] [blame] | 200 | * @brief Called when the fan presence property changes on D-Bus |
| 201 | * |
| 202 | * @param[in] msg - The message from the propertiesChanged signal |
| 203 | */ |
| 204 | void presenceChanged(sdbusplus::message::message& msg); |
| 205 | |
| 206 | /** |
Matthew Barth | 177fe98 | 2020-05-26 11:05:19 -0500 | [diff] [blame] | 207 | * @brief the dbus object |
| 208 | */ |
| 209 | sdbusplus::bus::bus& _bus; |
Matt Spinler | abf8da3 | 2017-04-27 14:08:45 -0500 | [diff] [blame] | 210 | |
Matthew Barth | 177fe98 | 2020-05-26 11:05:19 -0500 | [diff] [blame] | 211 | /** |
| 212 | * @brief The inventory name of the fan |
| 213 | */ |
| 214 | const std::string _name; |
Matt Spinler | b1e1851 | 2017-04-27 14:42:33 -0500 | [diff] [blame] | 215 | |
Matthew Barth | 177fe98 | 2020-05-26 11:05:19 -0500 | [diff] [blame] | 216 | /** |
| 217 | * @brief The percentage that the input speed must be below |
| 218 | * the target speed to be considered an error. |
| 219 | * Between 0 and 100. |
| 220 | */ |
| 221 | const size_t _deviation; |
Matt Spinler | abf8da3 | 2017-04-27 14:08:45 -0500 | [diff] [blame] | 222 | |
Matthew Barth | 177fe98 | 2020-05-26 11:05:19 -0500 | [diff] [blame] | 223 | /** |
| 224 | * The number of sensors that must be nonfunctional at the |
| 225 | * same time in order for the fan to be set to nonfunctional |
| 226 | * in the inventory. |
| 227 | */ |
| 228 | const size_t _numSensorFailsForNonFunc; |
Matt Spinler | abf8da3 | 2017-04-27 14:08:45 -0500 | [diff] [blame] | 229 | |
Matthew Barth | 177fe98 | 2020-05-26 11:05:19 -0500 | [diff] [blame] | 230 | /** |
Jolie Ku | 5d564a9 | 2020-10-23 09:04:28 +0800 | [diff] [blame] | 231 | * The number of failed sensors |
| 232 | */ |
| 233 | size_t _numFailedSensor = 0; |
| 234 | |
| 235 | /** |
Matthew Barth | 177fe98 | 2020-05-26 11:05:19 -0500 | [diff] [blame] | 236 | * @brief The current functional state of the fan |
| 237 | */ |
| 238 | bool _functional = true; |
Matt Spinler | abf8da3 | 2017-04-27 14:08:45 -0500 | [diff] [blame] | 239 | |
Matthew Barth | 177fe98 | 2020-05-26 11:05:19 -0500 | [diff] [blame] | 240 | /** |
| 241 | * The sensor objects for the fan |
| 242 | */ |
| 243 | std::vector<std::shared_ptr<TachSensor>> _sensors; |
Matt Spinler | abf8da3 | 2017-04-27 14:08:45 -0500 | [diff] [blame] | 244 | |
Matthew Barth | 177fe98 | 2020-05-26 11:05:19 -0500 | [diff] [blame] | 245 | /** |
| 246 | * The tach trust manager object |
| 247 | */ |
| 248 | std::unique_ptr<trust::Manager>& _trustManager; |
Matt Spinler | b0412d0 | 2020-10-12 16:53:52 -0500 | [diff] [blame] | 249 | |
| 250 | #ifdef MONITOR_USE_JSON |
| 251 | /** |
| 252 | * @brief The number of seconds to wait after startup until |
| 253 | * fan sensors should checked against their targets. |
| 254 | */ |
| 255 | size_t _monitorDelay; |
| 256 | |
| 257 | /** |
| 258 | * @brief Expires after _monitorDelay to start fan monitoring. |
| 259 | */ |
| 260 | sdeventplus::utility::Timer<sdeventplus::ClockId::Monotonic> _monitorTimer; |
| 261 | #endif |
| 262 | |
| 263 | /** |
| 264 | * @brief Set to true when monitoring can start. |
| 265 | */ |
| 266 | bool _monitorReady = false; |
| 267 | |
| 268 | /** |
| 269 | * @brief Reference to the System object |
| 270 | */ |
| 271 | System& _system; |
Matt Spinler | b63aa09 | 2020-10-14 09:45:11 -0500 | [diff] [blame] | 272 | |
| 273 | /** |
| 274 | * @brief The match object for propertiesChanged signals |
| 275 | * for the inventory item interface to track the |
| 276 | * Present property. |
| 277 | */ |
| 278 | sdbusplus::bus::match::match _presenceMatch; |
| 279 | |
| 280 | /** |
| 281 | * @brief The current presence state |
| 282 | */ |
| 283 | bool _present = false; |
Matt Spinler | 27f6b68 | 2020-10-27 08:43:37 -0500 | [diff] [blame] | 284 | |
| 285 | /** |
| 286 | * @brief The number of seconds to wait after a fan is removed before |
| 287 | * creating an event log for it. If std::nullopt, then no |
| 288 | * event log will be created. |
| 289 | */ |
| 290 | const std::optional<size_t> _fanMissingErrorDelay; |
| 291 | |
| 292 | /** |
| 293 | * @brief The timer that uses the _fanMissingErrorDelay timeout, |
| 294 | * at the end of which an event log will be created. |
| 295 | */ |
| 296 | std::unique_ptr< |
| 297 | sdeventplus::utility::Timer<sdeventplus::ClockId::Monotonic>> |
| 298 | _fanMissingErrorTimer; |
Matt Spinler | abf8da3 | 2017-04-27 14:08:45 -0500 | [diff] [blame] | 299 | }; |
| 300 | |
Matthew Barth | 177fe98 | 2020-05-26 11:05:19 -0500 | [diff] [blame] | 301 | } // namespace monitor |
| 302 | } // namespace fan |
| 303 | } // namespace phosphor |