Matthew Barth | c95c527 | 2020-06-15 19:51:13 -0500 | [diff] [blame] | 1 | /** |
Mike Capps | 7b34ee0 | 2022-05-04 14:16:12 -0400 | [diff] [blame] | 2 | * Copyright © 2022 IBM Corporation |
Matthew Barth | c95c527 | 2020-06-15 19:51:13 -0500 | [diff] [blame] | 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 | c95c527 | 2020-06-15 19:51:13 -0500 | [diff] [blame] | 16 | #include "system.hpp" |
| 17 | |
Mike Capps | bf8e56f | 2022-06-29 14:23:07 -0400 | [diff] [blame] | 18 | #include "dbus_paths.hpp" |
Matthew Barth | c95c527 | 2020-06-15 19:51:13 -0500 | [diff] [blame] | 19 | #include "fan.hpp" |
| 20 | #include "fan_defs.hpp" |
| 21 | #include "tach_sensor.hpp" |
| 22 | #include "trust_manager.hpp" |
| 23 | #include "types.hpp" |
Mike Capps | fdcd5db | 2021-05-20 12:47:10 -0400 | [diff] [blame] | 24 | #include "utility.hpp" |
Matthew Barth | c95c527 | 2020-06-15 19:51:13 -0500 | [diff] [blame] | 25 | #ifdef MONITOR_USE_JSON |
Mike Capps | b4379a1 | 2021-10-11 14:18:06 -0400 | [diff] [blame] | 26 | #include "json_config.hpp" |
Matthew Barth | c95c527 | 2020-06-15 19:51:13 -0500 | [diff] [blame] | 27 | #include "json_parser.hpp" |
| 28 | #endif |
| 29 | |
Matt Spinler | c8d3c51 | 2021-01-06 14:22:25 -0600 | [diff] [blame] | 30 | #include "config.h" |
| 31 | |
Matt Spinler | bb449c1 | 2021-06-14 11:45:28 -0600 | [diff] [blame] | 32 | #include "hwmon_ffdc.hpp" |
| 33 | |
Matthew Barth | c95c527 | 2020-06-15 19:51:13 -0500 | [diff] [blame] | 34 | #include <nlohmann/json.hpp> |
Matthew Barth | d06905c | 2020-06-12 08:13:06 -0500 | [diff] [blame] | 35 | #include <phosphor-logging/log.hpp> |
Matthew Barth | c95c527 | 2020-06-15 19:51:13 -0500 | [diff] [blame] | 36 | #include <sdbusplus/bus.hpp> |
Patrick Williams | cb356d4 | 2022-07-22 19:26:53 -0500 | [diff] [blame] | 37 | #include <sdbusplus/bus/match.hpp> |
Matthew Barth | c95c527 | 2020-06-15 19:51:13 -0500 | [diff] [blame] | 38 | #include <sdeventplus/event.hpp> |
Matthew Barth | d06905c | 2020-06-12 08:13:06 -0500 | [diff] [blame] | 39 | #include <sdeventplus/source/signal.hpp> |
Matthew Barth | c95c527 | 2020-06-15 19:51:13 -0500 | [diff] [blame] | 40 | |
| 41 | namespace phosphor::fan::monitor |
| 42 | { |
| 43 | |
| 44 | using json = nlohmann::json; |
Matt Spinler | f13b42e | 2020-10-26 15:29:49 -0500 | [diff] [blame] | 45 | using Severity = sdbusplus::xyz::openbmc_project::Logging::server::Entry::Level; |
| 46 | |
Matthew Barth | d06905c | 2020-06-12 08:13:06 -0500 | [diff] [blame] | 47 | using namespace phosphor::logging; |
Matthew Barth | c95c527 | 2020-06-15 19:51:13 -0500 | [diff] [blame] | 48 | |
Matt Spinler | 4f472a8 | 2022-08-26 13:55:34 -0500 | [diff] [blame] | 49 | const std::string System::dumpFile = "/tmp/fan_monitor_dump.json"; |
| 50 | |
Patrick Williams | cb356d4 | 2022-07-22 19:26:53 -0500 | [diff] [blame] | 51 | System::System(Mode mode, sdbusplus::bus_t& bus, |
Matthew Barth | c95c527 | 2020-06-15 19:51:13 -0500 | [diff] [blame] | 52 | const sdeventplus::Event& event) : |
| 53 | _mode(mode), |
Matt Spinler | c8d3c51 | 2021-01-06 14:22:25 -0600 | [diff] [blame] | 54 | _bus(bus), _event(event), |
Chau Ly | fce1490 | 2023-01-13 08:52:33 +0000 | [diff] [blame] | 55 | #ifdef MONITOR_USE_HOST_STATE |
| 56 | _powerState(std::make_unique<HostPowerState>( |
| 57 | #else |
Matt Spinler | c8d3c51 | 2021-01-06 14:22:25 -0600 | [diff] [blame] | 58 | _powerState(std::make_unique<PGoodState>( |
Chau Ly | fce1490 | 2023-01-13 08:52:33 +0000 | [diff] [blame] | 59 | #endif |
Matt Spinler | e892e39 | 2020-10-14 13:21:31 -0500 | [diff] [blame] | 60 | bus, std::bind(std::mem_fn(&System::powerStateChanged), this, |
Matt Spinler | c8d3c51 | 2021-01-06 14:22:25 -0600 | [diff] [blame] | 61 | std::placeholders::_1))), |
| 62 | _thermalAlert(bus, THERMAL_ALERT_OBJPATH) |
Matt Spinler | 7d13564 | 2021-02-04 12:44:17 -0600 | [diff] [blame] | 63 | {} |
Matt Spinler | e892e39 | 2020-10-14 13:21:31 -0500 | [diff] [blame] | 64 | |
Matthew Barth | 823bc49 | 2021-06-21 14:19:09 -0500 | [diff] [blame] | 65 | void System::start() |
Matt Spinler | 7d13564 | 2021-02-04 12:44:17 -0600 | [diff] [blame] | 66 | { |
Mike Capps | b4379a1 | 2021-10-11 14:18:06 -0400 | [diff] [blame] | 67 | namespace match = sdbusplus::bus::match; |
| 68 | |
| 69 | // must be done before service detection |
Patrick Williams | cb356d4 | 2022-07-22 19:26:53 -0500 | [diff] [blame] | 70 | _inventoryMatch = std::make_unique<sdbusplus::bus::match_t>( |
Mike Capps | b4379a1 | 2021-10-11 14:18:06 -0400 | [diff] [blame] | 71 | _bus, match::rules::nameOwnerChanged(util::INVENTORY_SVC), |
| 72 | std::bind(&System::inventoryOnlineCb, this, std::placeholders::_1)); |
| 73 | |
| 74 | bool invServiceRunning = util::SDBusPlus::callMethodAndRead<bool>( |
| 75 | _bus, "org.freedesktop.DBus", "/org/freedesktop/DBus", |
| 76 | "org.freedesktop.DBus", "NameHasOwner", util::INVENTORY_SVC); |
| 77 | |
| 78 | if (invServiceRunning) |
| 79 | { |
| 80 | _inventoryMatch.reset(); |
| 81 | |
| 82 | if (!_loaded) |
| 83 | { |
| 84 | load(); |
| 85 | } |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | void System::load() |
| 90 | { |
Matthew Barth | c95c527 | 2020-06-15 19:51:13 -0500 | [diff] [blame] | 91 | json jsonObj = json::object(); |
| 92 | #ifdef MONITOR_USE_JSON |
Mike Capps | b4379a1 | 2021-10-11 14:18:06 -0400 | [diff] [blame] | 93 | try |
| 94 | { |
Mike Capps | 808d7fe | 2022-06-13 10:12:16 -0400 | [diff] [blame] | 95 | jsonObj = getJsonObj(); |
Matthew Barth | c95c527 | 2020-06-15 19:51:13 -0500 | [diff] [blame] | 96 | #endif |
Mike Capps | b4379a1 | 2021-10-11 14:18:06 -0400 | [diff] [blame] | 97 | auto trustGrps = getTrustGroups(jsonObj); |
| 98 | auto fanDefs = getFanDefinitions(jsonObj); |
| 99 | // Retrieve and set trust groups within the trust manager |
| 100 | setTrustMgr(getTrustGroups(jsonObj)); |
| 101 | // Clear/set configured fan definitions |
| 102 | _fans.clear(); |
| 103 | _fanHealth.clear(); |
| 104 | // Retrieve fan definitions and create fan objects to be monitored |
| 105 | setFans(fanDefs); |
| 106 | setFaultConfig(jsonObj); |
| 107 | log<level::INFO>("Configuration loaded"); |
| 108 | |
| 109 | _loaded = true; |
| 110 | #ifdef MONITOR_USE_JSON |
| 111 | } |
| 112 | catch (const phosphor::fan::NoConfigFound&) |
| 113 | {} |
| 114 | #endif |
Matt Spinler | e892e39 | 2020-10-14 13:21:31 -0500 | [diff] [blame] | 115 | |
Matt Spinler | e892e39 | 2020-10-14 13:21:31 -0500 | [diff] [blame] | 116 | if (_powerState->isPowerOn()) |
| 117 | { |
Matt Spinler | 752f24e | 2022-07-06 15:57:54 -0500 | [diff] [blame] | 118 | // Fans could be missing on startup, so check the power off rules. |
| 119 | // Tach sensors default to functional, so they wouldn't cause a power |
| 120 | // off here. |
Matt Spinler | e892e39 | 2020-10-14 13:21:31 -0500 | [diff] [blame] | 121 | std::for_each(_powerOffRules.begin(), _powerOffRules.end(), |
| 122 | [this](auto& rule) { |
Patrick Williams | 61b7329 | 2023-05-10 07:50:12 -0500 | [diff] [blame] | 123 | rule->check(PowerRuleState::runtime, _fanHealth); |
| 124 | }); |
Matt Spinler | e892e39 | 2020-10-14 13:21:31 -0500 | [diff] [blame] | 125 | } |
Mike Capps | fdcd5db | 2021-05-20 12:47:10 -0400 | [diff] [blame] | 126 | |
Mike Capps | b4379a1 | 2021-10-11 14:18:06 -0400 | [diff] [blame] | 127 | subscribeSensorsToServices(); |
Mike Capps | fdcd5db | 2021-05-20 12:47:10 -0400 | [diff] [blame] | 128 | } |
| 129 | |
Mike Capps | 25f0327 | 2021-09-13 13:38:44 -0400 | [diff] [blame] | 130 | void System::subscribeSensorsToServices() |
Mike Capps | fdcd5db | 2021-05-20 12:47:10 -0400 | [diff] [blame] | 131 | { |
Mike Capps | 25f0327 | 2021-09-13 13:38:44 -0400 | [diff] [blame] | 132 | namespace match = sdbusplus::bus::match; |
| 133 | |
Mike Capps | b4379a1 | 2021-10-11 14:18:06 -0400 | [diff] [blame] | 134 | _sensorMatch.clear(); |
| 135 | |
Mike Capps | fdcd5db | 2021-05-20 12:47:10 -0400 | [diff] [blame] | 136 | SensorMapType sensorMap; |
| 137 | |
| 138 | // build a list of all interfaces, always including the value interface |
| 139 | // using set automatically guards against duplicates |
| 140 | std::set<std::string> unique_interfaces{util::FAN_SENSOR_VALUE_INTF}; |
| 141 | |
| 142 | for (const auto& fan : _fans) |
| 143 | { |
| 144 | for (const auto& sensor : fan->sensors()) |
| 145 | { |
| 146 | unique_interfaces.insert(sensor->getInterface()); |
| 147 | } |
| 148 | } |
| 149 | // convert them to vector to pass into getSubTreeRaw |
| 150 | std::vector<std::string> interfaces(unique_interfaces.begin(), |
| 151 | unique_interfaces.end()); |
| 152 | |
Mike Capps | 25f0327 | 2021-09-13 13:38:44 -0400 | [diff] [blame] | 153 | try |
Mike Capps | fdcd5db | 2021-05-20 12:47:10 -0400 | [diff] [blame] | 154 | { |
Mike Capps | 25f0327 | 2021-09-13 13:38:44 -0400 | [diff] [blame] | 155 | // get service information for all service names that are |
| 156 | // hosting these interfaces |
| 157 | auto serviceObjects = util::SDBusPlus::getSubTreeRaw( |
| 158 | _bus, FAN_SENSOR_PATH, interfaces, 0); |
| 159 | |
| 160 | for (const auto& fan : _fans) |
Mike Capps | fdcd5db | 2021-05-20 12:47:10 -0400 | [diff] [blame] | 161 | { |
Mike Capps | 25f0327 | 2021-09-13 13:38:44 -0400 | [diff] [blame] | 162 | // For every sensor in each fan |
| 163 | for (const auto& sensor : fan->sensors()) |
Mike Capps | fdcd5db | 2021-05-20 12:47:10 -0400 | [diff] [blame] | 164 | { |
Mike Capps | 25f0327 | 2021-09-13 13:38:44 -0400 | [diff] [blame] | 165 | const auto itServ = serviceObjects.find(sensor->name()); |
Mike Capps | fdcd5db | 2021-05-20 12:47:10 -0400 | [diff] [blame] | 166 | |
Mike Capps | 25f0327 | 2021-09-13 13:38:44 -0400 | [diff] [blame] | 167 | if (serviceObjects.end() == itServ || itServ->second.empty()) |
| 168 | { |
| 169 | getLogger().log( |
Patrick Williams | fbf4703 | 2023-07-17 12:27:34 -0500 | [diff] [blame] | 170 | std::format("Fan sensor entry {} not found in D-Bus", |
Mike Capps | 25f0327 | 2021-09-13 13:38:44 -0400 | [diff] [blame] | 171 | sensor->name()), |
| 172 | Logger::error); |
| 173 | continue; |
| 174 | } |
| 175 | |
| 176 | for (const auto& [serviceName, unused] : itServ->second) |
| 177 | { |
| 178 | // associate service name with sensor |
| 179 | sensorMap[serviceName].insert(sensor); |
| 180 | } |
Mike Capps | fdcd5db | 2021-05-20 12:47:10 -0400 | [diff] [blame] | 181 | } |
| 182 | } |
Mike Capps | fdcd5db | 2021-05-20 12:47:10 -0400 | [diff] [blame] | 183 | |
Mike Capps | 25f0327 | 2021-09-13 13:38:44 -0400 | [diff] [blame] | 184 | // only create 1 match per service |
| 185 | for (const auto& [serviceName, unused] : sensorMap) |
| 186 | { |
| 187 | // map its service name to the sensor |
Patrick Williams | cb356d4 | 2022-07-22 19:26:53 -0500 | [diff] [blame] | 188 | _sensorMatch.emplace_back(std::make_unique<sdbusplus::bus::match_t>( |
Mike Capps | 25f0327 | 2021-09-13 13:38:44 -0400 | [diff] [blame] | 189 | _bus, match::rules::nameOwnerChanged(serviceName), |
| 190 | std::bind(&System::tachSignalOffline, this, |
| 191 | std::placeholders::_1, sensorMap))); |
| 192 | } |
| 193 | } |
| 194 | catch (const util::DBusError&) |
| 195 | { |
| 196 | // catch exception from getSubTreeRaw() when fan sensor paths don't |
| 197 | // exist yet |
| 198 | } |
Matthew Barth | d06905c | 2020-06-12 08:13:06 -0500 | [diff] [blame] | 199 | } |
| 200 | |
Patrick Williams | cb356d4 | 2022-07-22 19:26:53 -0500 | [diff] [blame] | 201 | void System::inventoryOnlineCb(sdbusplus::message_t& msg) |
Mike Capps | b4379a1 | 2021-10-11 14:18:06 -0400 | [diff] [blame] | 202 | { |
| 203 | namespace match = sdbusplus::bus::match; |
| 204 | |
| 205 | std::string iface; |
| 206 | msg.read(iface); |
| 207 | |
| 208 | if (util::INVENTORY_INTF != iface) |
| 209 | { |
| 210 | return; |
| 211 | } |
| 212 | |
| 213 | std::string oldName; |
| 214 | msg.read(oldName); |
| 215 | |
| 216 | std::string newName; |
| 217 | msg.read(newName); |
| 218 | |
| 219 | // newName should never be empty since match was reset on the first |
| 220 | // nameOwnerChanged signal received from the service. |
| 221 | if (!_loaded && !newName.empty()) |
| 222 | { |
| 223 | load(); |
| 224 | } |
| 225 | |
| 226 | // cancel any further notifications about the service state |
| 227 | _inventoryMatch.reset(); |
| 228 | } |
| 229 | |
Matthew Barth | d06905c | 2020-06-12 08:13:06 -0500 | [diff] [blame] | 230 | void System::sighupHandler(sdeventplus::source::Signal&, |
| 231 | const struct signalfd_siginfo*) |
| 232 | { |
| 233 | try |
Matthew Barth | c95c527 | 2020-06-15 19:51:13 -0500 | [diff] [blame] | 234 | { |
Mike Capps | b4379a1 | 2021-10-11 14:18:06 -0400 | [diff] [blame] | 235 | load(); |
Matthew Barth | d06905c | 2020-06-12 08:13:06 -0500 | [diff] [blame] | 236 | } |
Mike Capps | b4379a1 | 2021-10-11 14:18:06 -0400 | [diff] [blame] | 237 | catch (std::runtime_error& re) |
Matthew Barth | d06905c | 2020-06-12 08:13:06 -0500 | [diff] [blame] | 238 | { |
| 239 | log<level::ERR>("Error reloading config, no config changes made", |
| 240 | entry("LOAD_ERROR=%s", re.what())); |
Matthew Barth | c95c527 | 2020-06-15 19:51:13 -0500 | [diff] [blame] | 241 | } |
| 242 | } |
| 243 | |
| 244 | const std::vector<CreateGroupFunction> |
Mike Capps | 808d7fe | 2022-06-13 10:12:16 -0400 | [diff] [blame] | 245 | System::getTrustGroups([[maybe_unused]] const json& jsonObj) |
Matthew Barth | c95c527 | 2020-06-15 19:51:13 -0500 | [diff] [blame] | 246 | { |
| 247 | #ifdef MONITOR_USE_JSON |
| 248 | return getTrustGrps(jsonObj); |
| 249 | #else |
| 250 | return trustGroups; |
| 251 | #endif |
| 252 | } |
| 253 | |
Matthew Barth | d06905c | 2020-06-12 08:13:06 -0500 | [diff] [blame] | 254 | void System::setTrustMgr(const std::vector<CreateGroupFunction>& groupFuncs) |
| 255 | { |
| 256 | _trust = std::make_unique<trust::Manager>(groupFuncs); |
| 257 | } |
| 258 | |
Mike Capps | 808d7fe | 2022-06-13 10:12:16 -0400 | [diff] [blame] | 259 | const std::vector<FanDefinition> |
| 260 | System::getFanDefinitions([[maybe_unused]] const json& jsonObj) |
Matthew Barth | c95c527 | 2020-06-15 19:51:13 -0500 | [diff] [blame] | 261 | { |
| 262 | #ifdef MONITOR_USE_JSON |
| 263 | return getFanDefs(jsonObj); |
| 264 | #else |
| 265 | return fanDefinitions; |
| 266 | #endif |
| 267 | } |
| 268 | |
Matthew Barth | d06905c | 2020-06-12 08:13:06 -0500 | [diff] [blame] | 269 | void System::setFans(const std::vector<FanDefinition>& fanDefs) |
| 270 | { |
| 271 | for (const auto& fanDef : fanDefs) |
| 272 | { |
| 273 | // Check if a condition exists on the fan |
Matt Spinler | 18fb12b | 2023-05-09 11:17:42 -0500 | [diff] [blame] | 274 | auto condition = fanDef.condition; |
Matthew Barth | d06905c | 2020-06-12 08:13:06 -0500 | [diff] [blame] | 275 | if (condition) |
| 276 | { |
| 277 | // Condition exists, skip adding fan if it fails |
| 278 | if (!(*condition)(_bus)) |
| 279 | { |
| 280 | continue; |
| 281 | } |
| 282 | } |
| 283 | _fans.emplace_back( |
Matt Spinler | b0412d0 | 2020-10-12 16:53:52 -0500 | [diff] [blame] | 284 | std::make_unique<Fan>(_mode, _bus, _event, _trust, fanDef, *this)); |
Matt Spinler | b63aa09 | 2020-10-14 09:45:11 -0500 | [diff] [blame] | 285 | |
| 286 | updateFanHealth(*(_fans.back())); |
Matthew Barth | d06905c | 2020-06-12 08:13:06 -0500 | [diff] [blame] | 287 | } |
| 288 | } |
| 289 | |
Mike Capps | fdcd5db | 2021-05-20 12:47:10 -0400 | [diff] [blame] | 290 | // callback indicating a service went [on|off]line. |
| 291 | // Determine on/offline status, set all sensors for that service |
| 292 | // to new state |
| 293 | // |
Patrick Williams | cb356d4 | 2022-07-22 19:26:53 -0500 | [diff] [blame] | 294 | void System::tachSignalOffline(sdbusplus::message_t& msg, |
Patrick Williams | 61b7329 | 2023-05-10 07:50:12 -0500 | [diff] [blame] | 295 | const SensorMapType& sensorMap) |
Mike Capps | fdcd5db | 2021-05-20 12:47:10 -0400 | [diff] [blame] | 296 | { |
| 297 | std::string serviceName, oldOwner, newOwner; |
| 298 | |
| 299 | msg.read(serviceName); |
| 300 | msg.read(oldOwner); |
| 301 | msg.read(newOwner); |
| 302 | |
| 303 | // true if sensor server came back online, false -> went offline |
| 304 | bool hasOwner = !newOwner.empty() && oldOwner.empty(); |
| 305 | |
| 306 | std::string stateStr(hasOwner ? "online" : "offline"); |
Patrick Williams | fbf4703 | 2023-07-17 12:27:34 -0500 | [diff] [blame] | 307 | getLogger().log(std::format("Changing sensors for service {} to {}", |
Mike Capps | fdcd5db | 2021-05-20 12:47:10 -0400 | [diff] [blame] | 308 | serviceName, stateStr), |
| 309 | Logger::info); |
| 310 | |
| 311 | auto sensorItr(sensorMap.find(serviceName)); |
| 312 | |
| 313 | if (sensorItr != sensorMap.end()) |
| 314 | { |
| 315 | // set all sensors' owner state to not-owned |
| 316 | for (auto& sensor : sensorItr->second) |
| 317 | { |
| 318 | sensor->setOwner(hasOwner); |
| 319 | sensor->getFan().process(*sensor); |
| 320 | } |
| 321 | } |
| 322 | } |
| 323 | |
Matt Spinler | b63aa09 | 2020-10-14 09:45:11 -0500 | [diff] [blame] | 324 | void System::updateFanHealth(const Fan& fan) |
| 325 | { |
| 326 | std::vector<bool> sensorStatus; |
| 327 | for (const auto& sensor : fan.sensors()) |
| 328 | { |
| 329 | sensorStatus.push_back(sensor->functional()); |
| 330 | } |
| 331 | |
Patrick Williams | 61b7329 | 2023-05-10 07:50:12 -0500 | [diff] [blame] | 332 | _fanHealth[fan.getName()] = std::make_tuple(fan.present(), |
| 333 | std::move(sensorStatus)); |
Matt Spinler | b63aa09 | 2020-10-14 09:45:11 -0500 | [diff] [blame] | 334 | } |
| 335 | |
Matt Spinler | 4283c5d | 2021-03-01 15:56:00 -0600 | [diff] [blame] | 336 | void System::fanStatusChange(const Fan& fan, bool skipRulesCheck) |
Matt Spinler | b63aa09 | 2020-10-14 09:45:11 -0500 | [diff] [blame] | 337 | { |
| 338 | updateFanHealth(fan); |
Matt Spinler | e892e39 | 2020-10-14 13:21:31 -0500 | [diff] [blame] | 339 | |
Matt Spinler | 4283c5d | 2021-03-01 15:56:00 -0600 | [diff] [blame] | 340 | if (_powerState->isPowerOn() && !skipRulesCheck) |
Matt Spinler | e892e39 | 2020-10-14 13:21:31 -0500 | [diff] [blame] | 341 | { |
| 342 | std::for_each(_powerOffRules.begin(), _powerOffRules.end(), |
| 343 | [this](auto& rule) { |
Patrick Williams | 61b7329 | 2023-05-10 07:50:12 -0500 | [diff] [blame] | 344 | rule->check(PowerRuleState::runtime, _fanHealth); |
| 345 | }); |
Matt Spinler | e892e39 | 2020-10-14 13:21:31 -0500 | [diff] [blame] | 346 | } |
| 347 | } |
| 348 | |
Mike Capps | 808d7fe | 2022-06-13 10:12:16 -0400 | [diff] [blame] | 349 | void System::setFaultConfig([[maybe_unused]] const json& jsonObj) |
Matt Spinler | e892e39 | 2020-10-14 13:21:31 -0500 | [diff] [blame] | 350 | { |
| 351 | #ifdef MONITOR_USE_JSON |
| 352 | std::shared_ptr<PowerInterfaceBase> powerInterface = |
Matt Spinler | ba3ee9a | 2021-01-06 14:45:50 -0600 | [diff] [blame] | 353 | std::make_shared<PowerInterface>(_thermalAlert); |
Matt Spinler | e892e39 | 2020-10-14 13:21:31 -0500 | [diff] [blame] | 354 | |
Matt Spinler | ac1efc1 | 2020-10-27 10:20:11 -0500 | [diff] [blame] | 355 | PowerOffAction::PrePowerOffFunc func = |
| 356 | std::bind(std::mem_fn(&System::logShutdownError), this); |
| 357 | |
| 358 | _powerOffRules = getPowerOffRules(jsonObj, powerInterface, func); |
Matt Spinler | f13b42e | 2020-10-26 15:29:49 -0500 | [diff] [blame] | 359 | |
| 360 | _numNonfuncSensorsBeforeError = getNumNonfuncRotorsBeforeError(jsonObj); |
Matt Spinler | e892e39 | 2020-10-14 13:21:31 -0500 | [diff] [blame] | 361 | #endif |
| 362 | } |
| 363 | |
| 364 | void System::powerStateChanged(bool powerStateOn) |
| 365 | { |
Matt Spinler | 7d13564 | 2021-02-04 12:44:17 -0600 | [diff] [blame] | 366 | std::for_each(_fans.begin(), _fans.end(), [powerStateOn](auto& fan) { |
| 367 | fan->powerStateChanged(powerStateOn); |
| 368 | }); |
| 369 | |
Matt Spinler | e892e39 | 2020-10-14 13:21:31 -0500 | [diff] [blame] | 370 | if (powerStateOn) |
| 371 | { |
Mike Capps | b4379a1 | 2021-10-11 14:18:06 -0400 | [diff] [blame] | 372 | if (!_loaded) |
Matt Spinler | 7d13564 | 2021-02-04 12:44:17 -0600 | [diff] [blame] | 373 | { |
| 374 | log<level::ERR>("No conf file found at power on"); |
Matthew Barth | ba53d3e | 2021-02-24 07:48:37 -0600 | [diff] [blame] | 375 | throw std::runtime_error("No conf file found at power on"); |
Matt Spinler | 7d13564 | 2021-02-04 12:44:17 -0600 | [diff] [blame] | 376 | } |
| 377 | |
Matt Spinler | bb449c1 | 2021-06-14 11:45:28 -0600 | [diff] [blame] | 378 | // If no fan has its sensors on D-Bus, then there is a problem |
| 379 | // with the fan controller. Log an error and shut down. |
| 380 | if (std::all_of(_fans.begin(), _fans.end(), [](const auto& fan) { |
Patrick Williams | 5e15c3b | 2023-10-20 11:18:11 -0500 | [diff] [blame] | 381 | return fan->numSensorsOnDBusAtPowerOn() == 0; |
| 382 | })) |
Matt Spinler | bb449c1 | 2021-06-14 11:45:28 -0600 | [diff] [blame] | 383 | { |
Chau Ly | 751c8be | 2023-01-13 08:21:03 +0000 | [diff] [blame] | 384 | #if DELAY_HOST_CONTROL > 0 |
| 385 | sleep(DELAY_HOST_CONTROL); |
| 386 | std::for_each(_fans.begin(), _fans.end(), |
| 387 | [powerStateOn](auto& fan) { |
Patrick Williams | 61b7329 | 2023-05-10 07:50:12 -0500 | [diff] [blame] | 388 | fan->powerStateChanged(powerStateOn); |
| 389 | }); |
Chau Ly | 751c8be | 2023-01-13 08:21:03 +0000 | [diff] [blame] | 390 | if (std::all_of(_fans.begin(), _fans.end(), [](const auto& fan) { |
Patrick Williams | 5e15c3b | 2023-10-20 11:18:11 -0500 | [diff] [blame] | 391 | return fan->numSensorsOnDBusAtPowerOn() == 0; |
| 392 | })) |
Chau Ly | 751c8be | 2023-01-13 08:21:03 +0000 | [diff] [blame] | 393 | { |
| 394 | handleOfflineFanController(); |
| 395 | return; |
| 396 | } |
| 397 | #else |
Matt Spinler | bb449c1 | 2021-06-14 11:45:28 -0600 | [diff] [blame] | 398 | handleOfflineFanController(); |
| 399 | return; |
Chau Ly | 751c8be | 2023-01-13 08:21:03 +0000 | [diff] [blame] | 400 | #endif |
Matt Spinler | bb449c1 | 2021-06-14 11:45:28 -0600 | [diff] [blame] | 401 | } |
| 402 | |
Mike Capps | 25f0327 | 2021-09-13 13:38:44 -0400 | [diff] [blame] | 403 | if (_sensorMatch.empty()) |
| 404 | { |
| 405 | subscribeSensorsToServices(); |
| 406 | } |
| 407 | |
Matt Spinler | e892e39 | 2020-10-14 13:21:31 -0500 | [diff] [blame] | 408 | std::for_each(_powerOffRules.begin(), _powerOffRules.end(), |
| 409 | [this](auto& rule) { |
Patrick Williams | 61b7329 | 2023-05-10 07:50:12 -0500 | [diff] [blame] | 410 | rule->check(PowerRuleState::atPgood, _fanHealth); |
| 411 | }); |
Matt Spinler | e892e39 | 2020-10-14 13:21:31 -0500 | [diff] [blame] | 412 | std::for_each(_powerOffRules.begin(), _powerOffRules.end(), |
| 413 | [this](auto& rule) { |
Patrick Williams | 61b7329 | 2023-05-10 07:50:12 -0500 | [diff] [blame] | 414 | rule->check(PowerRuleState::runtime, _fanHealth); |
| 415 | }); |
Matt Spinler | e892e39 | 2020-10-14 13:21:31 -0500 | [diff] [blame] | 416 | } |
| 417 | else |
| 418 | { |
Matt Spinler | c8d3c51 | 2021-01-06 14:22:25 -0600 | [diff] [blame] | 419 | _thermalAlert.enabled(false); |
| 420 | |
Matt Spinler | e892e39 | 2020-10-14 13:21:31 -0500 | [diff] [blame] | 421 | // Cancel any in-progress power off actions |
| 422 | std::for_each(_powerOffRules.begin(), _powerOffRules.end(), |
| 423 | [this](auto& rule) { rule->cancel(); }); |
| 424 | } |
Matt Spinler | b63aa09 | 2020-10-14 09:45:11 -0500 | [diff] [blame] | 425 | } |
| 426 | |
Matt Spinler | f13b42e | 2020-10-26 15:29:49 -0500 | [diff] [blame] | 427 | void System::sensorErrorTimerExpired(const Fan& fan, const TachSensor& sensor) |
| 428 | { |
| 429 | std::string fanPath{util::INVENTORY_PATH + fan.getName()}; |
| 430 | |
| 431 | getLogger().log( |
Patrick Williams | fbf4703 | 2023-07-17 12:27:34 -0500 | [diff] [blame] | 432 | std::format("Creating event log for faulted fan {} sensor {}", fanPath, |
Matt Spinler | f13b42e | 2020-10-26 15:29:49 -0500 | [diff] [blame] | 433 | sensor.name()), |
| 434 | Logger::error); |
| 435 | |
| 436 | // In order to know if the event log should have a severity of error or |
| 437 | // informational, count the number of existing nonfunctional sensors and |
| 438 | // compare it to _numNonfuncSensorsBeforeError. |
| 439 | size_t nonfuncSensors = 0; |
| 440 | for (const auto& fan : _fans) |
| 441 | { |
| 442 | for (const auto& s : fan->sensors()) |
| 443 | { |
| 444 | // Don't count nonfunctional sensors that still have their |
| 445 | // error timer running as nonfunctional since they haven't |
| 446 | // had event logs created for those errors yet. |
| 447 | if (!s->functional() && !s->errorTimerRunning()) |
| 448 | { |
| 449 | nonfuncSensors++; |
| 450 | } |
| 451 | } |
| 452 | } |
| 453 | |
| 454 | Severity severity = Severity::Error; |
| 455 | if (nonfuncSensors < _numNonfuncSensorsBeforeError) |
| 456 | { |
| 457 | severity = Severity::Informational; |
| 458 | } |
| 459 | |
| 460 | auto error = |
| 461 | std::make_unique<FanError>("xyz.openbmc_project.Fan.Error.Fault", |
| 462 | fanPath, sensor.name(), severity); |
| 463 | |
| 464 | auto sensorData = captureSensorData(); |
| 465 | error->commit(sensorData); |
| 466 | |
Matt Spinler | ac1efc1 | 2020-10-27 10:20:11 -0500 | [diff] [blame] | 467 | // Save the error so it can be committed again on a power off. |
| 468 | _lastError = std::move(error); |
Matt Spinler | f13b42e | 2020-10-26 15:29:49 -0500 | [diff] [blame] | 469 | } |
| 470 | |
Matt Spinler | 27f6b68 | 2020-10-27 08:43:37 -0500 | [diff] [blame] | 471 | void System::fanMissingErrorTimerExpired(const Fan& fan) |
| 472 | { |
| 473 | std::string fanPath{util::INVENTORY_PATH + fan.getName()}; |
| 474 | |
| 475 | getLogger().log( |
Patrick Williams | fbf4703 | 2023-07-17 12:27:34 -0500 | [diff] [blame] | 476 | std::format("Creating event log for missing fan {}", fanPath), |
Matt Spinler | 27f6b68 | 2020-10-27 08:43:37 -0500 | [diff] [blame] | 477 | Logger::error); |
| 478 | |
| 479 | auto error = std::make_unique<FanError>( |
| 480 | "xyz.openbmc_project.Fan.Error.Missing", fanPath, "", Severity::Error); |
| 481 | |
| 482 | auto sensorData = captureSensorData(); |
| 483 | error->commit(sensorData); |
| 484 | |
Matt Spinler | ac1efc1 | 2020-10-27 10:20:11 -0500 | [diff] [blame] | 485 | // Save the error so it can be committed again on a power off. |
| 486 | _lastError = std::move(error); |
| 487 | } |
| 488 | |
| 489 | void System::logShutdownError() |
| 490 | { |
| 491 | if (_lastError) |
| 492 | { |
| 493 | getLogger().log("Re-committing previous fan error before power off"); |
| 494 | |
| 495 | // Still use the latest sensor data |
| 496 | auto sensorData = captureSensorData(); |
Matt Spinler | f435eb1 | 2021-05-11 14:44:25 -0500 | [diff] [blame] | 497 | _lastError->commit(sensorData, true); |
Matt Spinler | ac1efc1 | 2020-10-27 10:20:11 -0500 | [diff] [blame] | 498 | } |
Matt Spinler | 27f6b68 | 2020-10-27 08:43:37 -0500 | [diff] [blame] | 499 | } |
| 500 | |
Matt Spinler | f13b42e | 2020-10-26 15:29:49 -0500 | [diff] [blame] | 501 | json System::captureSensorData() |
| 502 | { |
| 503 | json data; |
| 504 | |
| 505 | for (const auto& fan : _fans) |
| 506 | { |
| 507 | for (const auto& sensor : fan->sensors()) |
| 508 | { |
| 509 | json values; |
| 510 | values["present"] = fan->present(); |
| 511 | values["functional"] = sensor->functional(); |
Matt Spinler | d16d464 | 2022-08-26 13:32:07 -0500 | [diff] [blame] | 512 | values["in_range"] = !fan->outOfRange(*sensor); |
Matt Spinler | f13b42e | 2020-10-26 15:29:49 -0500 | [diff] [blame] | 513 | values["tach"] = sensor->getInput(); |
Mike Capps | 7b34ee0 | 2022-05-04 14:16:12 -0400 | [diff] [blame] | 514 | |
Matt Spinler | f13b42e | 2020-10-26 15:29:49 -0500 | [diff] [blame] | 515 | if (sensor->hasTarget()) |
| 516 | { |
| 517 | values["target"] = sensor->getTarget(); |
| 518 | } |
| 519 | |
Mike Capps | 7b34ee0 | 2022-05-04 14:16:12 -0400 | [diff] [blame] | 520 | // convert between string/json to remove newlines |
| 521 | values["prev_tachs"] = json(sensor->getPrevTach()).dump(); |
| 522 | |
| 523 | if (sensor->hasTarget()) |
| 524 | { |
| 525 | values["prev_targets"] = json(sensor->getPrevTarget()).dump(); |
| 526 | } |
| 527 | |
Matt Spinler | 87f9adc | 2022-08-11 13:17:09 -0500 | [diff] [blame] | 528 | if (sensor->getMethod() == MethodMode::count) |
| 529 | { |
| 530 | values["ticks"] = sensor->getCounter(); |
| 531 | } |
Matt Spinler | f13b42e | 2020-10-26 15:29:49 -0500 | [diff] [blame] | 532 | data["sensors"][sensor->name()] = values; |
| 533 | } |
| 534 | } |
| 535 | |
| 536 | return data; |
| 537 | } |
| 538 | |
Matt Spinler | bb449c1 | 2021-06-14 11:45:28 -0600 | [diff] [blame] | 539 | void System::handleOfflineFanController() |
| 540 | { |
| 541 | getLogger().log("The fan controller appears to be offline. Shutting down.", |
| 542 | Logger::error); |
| 543 | |
| 544 | auto ffdc = collectHwmonFFDC(); |
| 545 | |
| 546 | FanError error{"xyz.openbmc_project.Fan.Error.FanControllerOffline", |
| 547 | Severity::Critical}; |
| 548 | error.commit(ffdc, true); |
| 549 | |
| 550 | PowerInterface::executeHardPowerOff(); |
Mike Capps | 683a96c | 2022-04-27 16:46:06 -0400 | [diff] [blame] | 551 | |
| 552 | createBmcDump(); |
| 553 | } |
| 554 | |
| 555 | /** |
| 556 | * @brief Create a BMC Dump |
| 557 | */ |
| 558 | void System::createBmcDump() const |
| 559 | { |
| 560 | try |
| 561 | { |
| 562 | util::SDBusPlus::callMethod( |
| 563 | "xyz.openbmc_project.Dump.Manager", "/xyz/openbmc_project/dump/bmc", |
| 564 | "xyz.openbmc_project.Dump.Create", "CreateDump", |
| 565 | std::vector< |
| 566 | std::pair<std::string, std::variant<std::string, uint64_t>>>()); |
| 567 | } |
Mike Capps | 477b13b | 2022-07-11 10:45:46 -0400 | [diff] [blame] | 568 | catch (const std::exception& e) |
| 569 | { |
| 570 | getLogger().log( |
Patrick Williams | fbf4703 | 2023-07-17 12:27:34 -0500 | [diff] [blame] | 571 | std::format("Caught exception while creating BMC dump: {}", |
Mike Capps | 477b13b | 2022-07-11 10:45:46 -0400 | [diff] [blame] | 572 | e.what()), |
| 573 | Logger::error); |
| 574 | } |
Matt Spinler | bb449c1 | 2021-06-14 11:45:28 -0600 | [diff] [blame] | 575 | } |
| 576 | |
Matt Spinler | 4f472a8 | 2022-08-26 13:55:34 -0500 | [diff] [blame] | 577 | void System::dumpDebugData(sdeventplus::source::Signal&, |
| 578 | const struct signalfd_siginfo*) |
| 579 | { |
| 580 | json output; |
| 581 | |
| 582 | if (_loaded) |
| 583 | { |
| 584 | output["logs"] = getLogger().getLogs(); |
| 585 | output["sensors"] = captureSensorData(); |
| 586 | } |
| 587 | else |
| 588 | { |
| 589 | output["error"] = "Fan monitor not loaded yet. Try again later."; |
| 590 | } |
| 591 | |
| 592 | std::ofstream file{System::dumpFile}; |
| 593 | if (!file) |
| 594 | { |
| 595 | log<level::ERR>("Could not open file for fan monitor dump"); |
| 596 | } |
| 597 | else |
| 598 | { |
| 599 | file << std::setw(4) << output; |
| 600 | } |
| 601 | } |
| 602 | |
Matthew Barth | c95c527 | 2020-06-15 19:51:13 -0500 | [diff] [blame] | 603 | } // namespace phosphor::fan::monitor |