Matthew Barth | c95c527 | 2020-06-15 19:51:13 -0500 | [diff] [blame] | 1 | /** |
| 2 | * Copyright © 2020 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 | */ |
| 16 | #pragma once |
| 17 | |
| 18 | #include "fan.hpp" |
Matt Spinler | ac1efc1 | 2020-10-27 10:20:11 -0500 | [diff] [blame] | 19 | #include "fan_error.hpp" |
Matt Spinler | e892e39 | 2020-10-14 13:21:31 -0500 | [diff] [blame] | 20 | #include "power_off_rule.hpp" |
| 21 | #include "power_state.hpp" |
Matthew Barth | c95c527 | 2020-06-15 19:51:13 -0500 | [diff] [blame] | 22 | #include "tach_sensor.hpp" |
| 23 | #include "trust_manager.hpp" |
| 24 | #include "types.hpp" |
| 25 | |
| 26 | #include <nlohmann/json.hpp> |
| 27 | #include <sdbusplus/bus.hpp> |
| 28 | #include <sdeventplus/event.hpp> |
Matthew Barth | d06905c | 2020-06-12 08:13:06 -0500 | [diff] [blame] | 29 | #include <sdeventplus/source/signal.hpp> |
Matthew Barth | c95c527 | 2020-06-15 19:51:13 -0500 | [diff] [blame] | 30 | |
| 31 | #include <memory> |
| 32 | #include <optional> |
| 33 | #include <vector> |
| 34 | |
| 35 | namespace phosphor::fan::monitor |
| 36 | { |
| 37 | |
| 38 | using json = nlohmann::json; |
| 39 | |
| 40 | class System |
| 41 | { |
| 42 | public: |
| 43 | System() = delete; |
Matt Spinler | ac1efc1 | 2020-10-27 10:20:11 -0500 | [diff] [blame] | 44 | ~System() = default; |
Matthew Barth | c95c527 | 2020-06-15 19:51:13 -0500 | [diff] [blame] | 45 | System(const System&) = delete; |
| 46 | System(System&&) = delete; |
| 47 | System& operator=(const System&) = delete; |
| 48 | System& operator=(System&&) = delete; |
Matthew Barth | c95c527 | 2020-06-15 19:51:13 -0500 | [diff] [blame] | 49 | |
| 50 | /** |
| 51 | * Constructor |
Matthew Barth | c95c527 | 2020-06-15 19:51:13 -0500 | [diff] [blame] | 52 | * |
| 53 | * @param[in] mode - mode of fan monitor |
| 54 | * @param[in] bus - sdbusplus bus object |
| 55 | * @param[in] event - event loop reference |
| 56 | */ |
| 57 | System(Mode mode, sdbusplus::bus::bus& bus, |
| 58 | const sdeventplus::Event& event); |
| 59 | |
Matthew Barth | d06905c | 2020-06-12 08:13:06 -0500 | [diff] [blame] | 60 | /** |
| 61 | * @brief Callback function to handle receiving a HUP signal to reload the |
| 62 | * JSON configuration. |
| 63 | */ |
| 64 | void sighupHandler(sdeventplus::source::Signal&, |
| 65 | const struct signalfd_siginfo*); |
| 66 | |
Matt Spinler | b63aa09 | 2020-10-14 09:45:11 -0500 | [diff] [blame] | 67 | /** |
| 68 | * @brief Called from the fan when it changes either |
| 69 | * present or functional status to update the |
| 70 | * fan health map. |
| 71 | * |
| 72 | * @param[in] fan - The fan that changed |
Matt Spinler | 4283c5d | 2021-03-01 15:56:00 -0600 | [diff] [blame] | 73 | * @param[in] skipRulesCheck - If the rules checks should be done now. |
Matt Spinler | b63aa09 | 2020-10-14 09:45:11 -0500 | [diff] [blame] | 74 | */ |
Matt Spinler | 4283c5d | 2021-03-01 15:56:00 -0600 | [diff] [blame] | 75 | void fanStatusChange(const Fan& fan, bool skipRulesCheck = false); |
Matt Spinler | b63aa09 | 2020-10-14 09:45:11 -0500 | [diff] [blame] | 76 | |
Matt Spinler | f13b42e | 2020-10-26 15:29:49 -0500 | [diff] [blame] | 77 | /** |
| 78 | * @brief Called when a fan sensor's error timer expires, which |
| 79 | * happens when the sensor has been nonfunctional for a |
| 80 | * certain amount of time. An event log will be created. |
| 81 | * |
| 82 | * @param[in] fan - The parent fan of the sensor |
| 83 | * @param[in] sensor - The faulted sensor |
| 84 | */ |
| 85 | void sensorErrorTimerExpired(const Fan& fan, const TachSensor& sensor); |
| 86 | |
Matt Spinler | 27f6b68 | 2020-10-27 08:43:37 -0500 | [diff] [blame] | 87 | /** |
| 88 | * @brief Called when the timer that starts when a fan is missing |
| 89 | * has expired so an event log needs to be created. |
| 90 | * |
| 91 | * @param[in] fan - The missing fan. |
| 92 | */ |
| 93 | void fanMissingErrorTimerExpired(const Fan& fan); |
| 94 | |
Matt Spinler | ac1efc1 | 2020-10-27 10:20:11 -0500 | [diff] [blame] | 95 | /** |
| 96 | * @brief Called by the power off actions to log an error when there is |
| 97 | * a power off due to fan problems. |
| 98 | * |
| 99 | * The error it logs is just the last fan error that occurred. |
| 100 | */ |
| 101 | void logShutdownError(); |
| 102 | |
Matt Spinler | 7d13564 | 2021-02-04 12:44:17 -0600 | [diff] [blame] | 103 | /** |
| 104 | * @brief Returns true if power is on |
| 105 | */ |
| 106 | bool isPowerOn() const |
| 107 | { |
| 108 | return _powerState->isPowerOn(); |
| 109 | } |
| 110 | |
| 111 | /** |
Matthew Barth | 823bc49 | 2021-06-21 14:19:09 -0500 | [diff] [blame] | 112 | * @brief Parses and populates the fan monitor trust groups and list of fans |
Matt Spinler | 7d13564 | 2021-02-04 12:44:17 -0600 | [diff] [blame] | 113 | */ |
Matthew Barth | 823bc49 | 2021-06-21 14:19:09 -0500 | [diff] [blame] | 114 | void start(); |
Matt Spinler | 7d13564 | 2021-02-04 12:44:17 -0600 | [diff] [blame] | 115 | |
Matthew Barth | c95c527 | 2020-06-15 19:51:13 -0500 | [diff] [blame] | 116 | private: |
| 117 | /* The mode of fan monitor */ |
| 118 | Mode _mode; |
| 119 | |
| 120 | /* The sdbusplus bus object */ |
| 121 | sdbusplus::bus::bus& _bus; |
| 122 | |
| 123 | /* The event loop reference */ |
| 124 | const sdeventplus::Event& _event; |
| 125 | |
| 126 | /* Trust manager of trust groups */ |
| 127 | std::unique_ptr<phosphor::fan::trust::Manager> _trust; |
| 128 | |
| 129 | /* List of fan objects to monitor */ |
| 130 | std::vector<std::unique_ptr<Fan>> _fans; |
| 131 | |
| 132 | /** |
Matt Spinler | b63aa09 | 2020-10-14 09:45:11 -0500 | [diff] [blame] | 133 | * @brief The latest health of all the fans |
| 134 | */ |
| 135 | FanHealth _fanHealth; |
| 136 | |
| 137 | /** |
Matt Spinler | e892e39 | 2020-10-14 13:21:31 -0500 | [diff] [blame] | 138 | * @brief The object to watch the power state |
| 139 | */ |
| 140 | std::unique_ptr<PowerState> _powerState; |
| 141 | |
| 142 | /** |
| 143 | * @brief The power off rules, for shutting down the system |
| 144 | * due to fan failures. |
| 145 | */ |
| 146 | std::vector<std::unique_ptr<PowerOffRule>> _powerOffRules; |
| 147 | |
| 148 | /** |
Matt Spinler | f13b42e | 2020-10-26 15:29:49 -0500 | [diff] [blame] | 149 | * @brief The number of concurrently nonfunctional fan sensors |
| 150 | * there must be for an event log created due to a |
| 151 | * nonfunctional fan sensor to have an Error severity as |
| 152 | * opposed to an Informational one. |
| 153 | */ |
| 154 | std::optional<size_t> _numNonfuncSensorsBeforeError; |
| 155 | |
| 156 | /** |
Matt Spinler | ac1efc1 | 2020-10-27 10:20:11 -0500 | [diff] [blame] | 157 | * @brief The most recently committed fan error. |
| 158 | */ |
| 159 | std::unique_ptr<FanError> _lastError; |
| 160 | |
| 161 | /** |
Matt Spinler | c8d3c51 | 2021-01-06 14:22:25 -0600 | [diff] [blame] | 162 | * @brief The thermal alert D-Bus object |
| 163 | */ |
| 164 | ThermalAlertObject _thermalAlert; |
| 165 | |
| 166 | /** |
Matt Spinler | 7d13564 | 2021-02-04 12:44:17 -0600 | [diff] [blame] | 167 | * @brief If start() has been called |
| 168 | */ |
| 169 | bool _started = false; |
| 170 | |
| 171 | /** |
Matt Spinler | f13b42e | 2020-10-26 15:29:49 -0500 | [diff] [blame] | 172 | * @brief Captures tach sensor data as JSON for use in |
| 173 | * fan fault and fan missing event logs. |
| 174 | * |
| 175 | * @return json - The JSON data |
| 176 | */ |
| 177 | json captureSensorData(); |
| 178 | |
| 179 | /** |
Matthew Barth | c95c527 | 2020-06-15 19:51:13 -0500 | [diff] [blame] | 180 | * @brief Retrieve the configured trust groups |
| 181 | * |
| 182 | * @param[in] jsonObj - JSON object to parse from |
| 183 | * |
| 184 | * @return List of functions applied on trust groups |
| 185 | */ |
| 186 | const std::vector<CreateGroupFunction> getTrustGroups(const json& jsonObj); |
| 187 | |
| 188 | /** |
Matthew Barth | d06905c | 2020-06-12 08:13:06 -0500 | [diff] [blame] | 189 | * @brief Set the trust manager's list of trust group functions |
| 190 | * |
| 191 | * @param[in] groupFuncs - list of trust group functions |
| 192 | */ |
| 193 | void setTrustMgr(const std::vector<CreateGroupFunction>& groupFuncs); |
| 194 | |
| 195 | /** |
Matthew Barth | c95c527 | 2020-06-15 19:51:13 -0500 | [diff] [blame] | 196 | * @brief Retrieve the configured fan definitions |
| 197 | * |
| 198 | * @param[in] jsonObj - JSON object to parse from |
| 199 | * |
| 200 | * @return List of fan definition data on the fans configured |
| 201 | */ |
| 202 | const std::vector<FanDefinition> getFanDefinitions(const json& jsonObj); |
Matthew Barth | d06905c | 2020-06-12 08:13:06 -0500 | [diff] [blame] | 203 | |
| 204 | /** |
| 205 | * @brief Set the list of fans to be monitored |
| 206 | * |
| 207 | * @param[in] fanDefs - list of fan definitions to create fans monitored |
| 208 | */ |
| 209 | void setFans(const std::vector<FanDefinition>& fanDefs); |
Matt Spinler | b63aa09 | 2020-10-14 09:45:11 -0500 | [diff] [blame] | 210 | |
| 211 | /** |
| 212 | * @brief Updates the fan health map entry for the fan passed in |
| 213 | * |
| 214 | * @param[in] fan - The fan to update the health map with |
| 215 | */ |
| 216 | void updateFanHealth(const Fan& fan); |
Matt Spinler | e892e39 | 2020-10-14 13:21:31 -0500 | [diff] [blame] | 217 | |
| 218 | /** |
| 219 | * @brief The function that runs when the power state changes |
| 220 | * |
| 221 | * @param[in] powerStateOn - If power is now on or not |
| 222 | */ |
| 223 | void powerStateChanged(bool powerStateOn); |
| 224 | |
| 225 | /** |
| 226 | * @brief Reads the fault configuration from the JSON config |
| 227 | * file, such as the power off rule configuration. |
| 228 | * |
| 229 | * @param[in] jsonObj - JSON object to parse from |
| 230 | */ |
| 231 | void setFaultConfig(const json& jsonObj); |
Matt Spinler | bb449c1 | 2021-06-14 11:45:28 -0600 | [diff] [blame^] | 232 | |
| 233 | /** |
| 234 | * @brief Log an error and shut down due to an offline fan controller |
| 235 | */ |
| 236 | void handleOfflineFanController(); |
Matthew Barth | c95c527 | 2020-06-15 19:51:13 -0500 | [diff] [blame] | 237 | }; |
| 238 | |
| 239 | } // namespace phosphor::fan::monitor |