blob: 37c0d3add05025b6e649c3e21345d36afe66bb1b [file] [log] [blame]
Matthew Barthfd05d642019-11-14 15:01:57 -06001/**
2 * Copyright © 2019 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 */
Jolie Ku1a568652020-08-24 16:32:15 +080016#include "json_parser.hpp"
Matthew Barth2d2caa32020-05-26 11:07:24 -050017
18#include "anyof.hpp"
19#include "fallback.hpp"
20#include "gpio.hpp"
Jolie Ku1a568652020-08-24 16:32:15 +080021#include "json_config.hpp"
Matthew Barth2d2caa32020-05-26 11:07:24 -050022#include "sdbusplus.hpp"
23#include "tach.hpp"
24
Matthew Barth0961fae2019-11-15 09:00:27 -060025#include <nlohmann/json.hpp>
Anwaar Hadiebead9a2025-03-19 20:35:57 +000026#include <phosphor-logging/lg2.hpp>
Matthew Barth5060b102019-12-16 10:46:35 -060027#include <sdbusplus/bus.hpp>
Mike Cappsa35a8902021-06-10 10:20:14 -040028#include <xyz/openbmc_project/Logging/Create/server.hpp>
29#include <xyz/openbmc_project/Logging/Entry/server.hpp>
Matthew Barthfd05d642019-11-14 15:01:57 -060030
Matthew Barth2d2caa32020-05-26 11:07:24 -050031#include <filesystem>
32#include <fstream>
33#include <string>
Matthew Barthfd05d642019-11-14 15:01:57 -060034
35namespace phosphor
36{
37namespace fan
38{
39namespace presence
40{
41
Matthew Barth0961fae2019-11-15 09:00:27 -060042using json = nlohmann::json;
43namespace fs = std::filesystem;
44using namespace phosphor::logging;
45
Matthew Barthfd05d642019-11-14 15:01:57 -060046policies JsonConfig::_policies;
Matthew Barth2d2caa32020-05-26 11:07:24 -050047const std::map<std::string, methodHandler> JsonConfig::_methods = {
48 {"tach", method::getTach}, {"gpio", method::getGpio}};
49const std::map<std::string, rpolicyHandler> JsonConfig::_rpolicies = {
50 {"anyof", rpolicy::getAnyof}, {"fallback", rpolicy::getFallback}};
Matthew Barthfd05d642019-11-14 15:01:57 -060051
Mike Cappsa35a8902021-06-10 10:20:14 -040052const auto loggingPath = "/xyz/openbmc_project/logging";
53const auto loggingCreateIface = "xyz.openbmc_project.Logging.Create";
54
Patrick Williams61b73292023-05-10 07:50:12 -050055JsonConfig::JsonConfig(sdbusplus::bus_t& bus) : _bus(bus) {}
Jolie Ku1a568652020-08-24 16:32:15 +080056
Matthew Barth5b839912021-06-21 14:46:34 -050057void JsonConfig::start()
Matt Spinler0daedd12021-01-25 14:46:03 -060058{
Matthew Barth5b839912021-06-21 14:46:34 -050059 using config = fan::JsonConfig;
60
Matt Spinlerdfc8c4d2022-06-22 16:52:27 -050061 if (!_loaded)
Matt Spinler0daedd12021-01-25 14:46:03 -060062 {
Mike Capps808d7fe2022-06-13 10:12:16 -040063 process(config::load(config::getConfFile(confAppName, confFileName)));
Matt Spinlerdfc8c4d2022-06-22 16:52:27 -050064
65 _loaded = true;
66
67 for (auto& p : _policies)
68 {
69 p->monitor();
70 }
Matt Spinler0daedd12021-01-25 14:46:03 -060071 }
Matthew Barthfd05d642019-11-14 15:01:57 -060072}
73
74const policies& JsonConfig::get()
75{
76 return _policies;
77}
78
Mike Capps808d7fe2022-06-13 10:12:16 -040079void JsonConfig::sighupHandler(sdeventplus::source::Signal& /*sigSrc*/,
80 const struct signalfd_siginfo* /*sigInfo*/)
Matthew Barthf3e70472019-12-03 13:33:20 -060081{
82 try
83 {
Jolie Ku1a568652020-08-24 16:32:15 +080084 using config = fan::JsonConfig;
85
Matt Spinler9e9f5992020-09-30 08:29:24 -050086 _reporter.reset();
87
Matthew Barthf3e70472019-12-03 13:33:20 -060088 // Load and process the json configuration
Mike Capps808d7fe2022-06-13 10:12:16 -040089 process(config::load(config::getConfFile(confAppName, confFileName)));
Jolie Ku1a568652020-08-24 16:32:15 +080090
Matthew Barth2d2caa32020-05-26 11:07:24 -050091 for (auto& p : _policies)
Matthew Barthf3e70472019-12-03 13:33:20 -060092 {
93 p->monitor();
94 }
Anwaar Hadiebead9a2025-03-19 20:35:57 +000095 lg2::info("Configuration loaded successfully");
Matthew Barthf3e70472019-12-03 13:33:20 -060096 }
Patrick Williamsddb773b2021-10-06 11:24:49 -050097 catch (const std::runtime_error& re)
Matthew Barthf3e70472019-12-03 13:33:20 -060098 {
Anwaar Hadiebead9a2025-03-19 20:35:57 +000099 lg2::error("Error loading config, no config changes made: {ERROR}",
100 "ERROR", re);
Matthew Barthf3e70472019-12-03 13:33:20 -0600101 }
102}
103
Matthew Barth4a94dec2019-11-15 10:40:47 -0600104void JsonConfig::process(const json& jsonConf)
105{
Matthew Barthf3e70472019-12-03 13:33:20 -0600106 policies policies;
107 std::vector<fanPolicy> fans;
Matthew Barthaa8d81d2019-11-21 14:07:31 -0600108 // Set the expected number of fan entries
109 // to be size of the list of fan json config entries
110 // (Must be done to eliminate vector reallocation of fan references)
Matt Spinler9e9f5992020-09-30 08:29:24 -0500111 fans.reserve(jsonConf.size());
112 for (auto& member : jsonConf)
Matthew Barth4a94dec2019-11-15 10:40:47 -0600113 {
Matthew Barthe7566632019-11-18 16:13:04 -0600114 if (!member.contains("name") || !member.contains("path") ||
Matthew Barthaa8d81d2019-11-21 14:07:31 -0600115 !member.contains("methods") || !member.contains("rpolicy"))
Matthew Barth4a94dec2019-11-15 10:40:47 -0600116 {
Anwaar Hadiebead9a2025-03-19 20:35:57 +0000117 lg2::error(
118 "Missing one of the required fan presence properties, which are: 'name, path, methods, rpolicy'");
Matthew Barth4a94dec2019-11-15 10:40:47 -0600119 throw std::runtime_error(
120 "Missing required fan presence properties");
121 }
Matthew Barthe7566632019-11-18 16:13:04 -0600122
123 // Loop thru the configured methods of presence detection
Matthew Barthaa8d81d2019-11-21 14:07:31 -0600124 std::vector<std::unique_ptr<PresenceSensor>> sensors;
Matthew Barthe7566632019-11-18 16:13:04 -0600125 for (auto& method : member["methods"].items())
126 {
127 if (!method.value().contains("type"))
128 {
Anwaar Hadiebead9a2025-03-19 20:35:57 +0000129 lg2::error(
130 "Missing required fan presence method type for fan {FAN_NAME}",
131 "FAN_NAME", member["name"].get<std::string>());
Matthew Barthe7566632019-11-18 16:13:04 -0600132 throw std::runtime_error(
133 "Missing required fan presence method type");
134 }
135 // The method type of fan presence detection
136 // (Must have a supported function within the method namespace)
137 auto type = method.value()["type"].get<std::string>();
138 std::transform(type.begin(), type.end(), type.begin(), tolower);
139 auto func = _methods.find(type);
140 if (func != _methods.end())
141 {
142 // Call function for method type
Matthew Barthf3e70472019-12-03 13:33:20 -0600143 auto sensor = func->second(fans.size(), method.value());
Matthew Barthe7566632019-11-18 16:13:04 -0600144 if (sensor)
145 {
Matthew Barthaa8d81d2019-11-21 14:07:31 -0600146 sensors.emplace_back(std::move(sensor));
Matthew Barthe7566632019-11-18 16:13:04 -0600147 }
148 }
149 else
150 {
Anwaar Hadiebead9a2025-03-19 20:35:57 +0000151 lg2::error(
152 "Invalid fan presence method type {METHOD_TYPE} for fan {FAN_NAME}",
153 "FAN_NAME", member["name"].get<std::string>(),
154 "METHOD_TYPE", type);
Matthew Barthe7566632019-11-18 16:13:04 -0600155 throw std::runtime_error("Invalid fan presence method type");
156 }
157 }
Matt Spinler9e9f5992020-09-30 08:29:24 -0500158
159 // Get the amount of time a fan must be not present before
160 // creating an error.
161 std::optional<size_t> timeUntilError;
162 if (member.contains("fan_missing_error_time"))
163 {
164 timeUntilError = member["fan_missing_error_time"].get<size_t>();
165 }
166
Matt Spinlerbc4179e2022-10-04 15:15:06 -0500167 std::unique_ptr<EEPROMDevice> eepromDevice;
168 if (member.contains("eeprom"))
169 {
170 const auto& eeprom = member.at("eeprom");
171 if (!eeprom.contains("bus_address") ||
172 !eeprom.contains("driver_name") ||
173 !eeprom.contains("bind_delay_ms"))
174 {
Anwaar Hadiebead9a2025-03-19 20:35:57 +0000175 lg2::error(
176 "Missing address, driver_name, or bind_delay_ms in eeprom section for fan {FAN_NAME}",
177 "FAN_NAME", member["name"].get<std::string>());
Matt Spinlerbc4179e2022-10-04 15:15:06 -0500178
179 throw std::runtime_error("Missing address, driver_name, or "
180 "bind_delay_ms in eeprom section");
181 }
182 eepromDevice = std::make_unique<EEPROMDevice>(
183 eeprom["bus_address"].get<std::string>(),
184 eeprom["driver_name"].get<std::string>(),
185 eeprom["bind_delay_ms"].get<size_t>());
186 }
187
Patrick Williamsdfddd642024-08-16 15:21:51 -0400188 auto fan =
189 std::make_tuple(member["name"], member["path"], timeUntilError);
Matthew Barthaa8d81d2019-11-21 14:07:31 -0600190 // Create a fan object
Matthew Barthf3e70472019-12-03 13:33:20 -0600191 fans.emplace_back(std::make_tuple(fan, std::move(sensors)));
Matthew Barthaa8d81d2019-11-21 14:07:31 -0600192
193 // Add fan presence policy
Patrick Williamsdfddd642024-08-16 15:21:51 -0400194 auto policy =
195 getPolicy(member["rpolicy"], fans.back(), std::move(eepromDevice));
Matthew Barthf3e70472019-12-03 13:33:20 -0600196 if (policy)
197 {
198 policies.emplace_back(std::move(policy));
199 }
Matthew Barthaa8d81d2019-11-21 14:07:31 -0600200 }
Matthew Barthf3e70472019-12-03 13:33:20 -0600201
202 // Success, refresh fans and policies lists
203 _fans.clear();
204 _fans.swap(fans);
205
206 _policies.clear();
207 _policies.swap(policies);
Matt Spinlere8122392020-09-24 13:22:18 -0500208
209 // Create the error reporter class if necessary
Matt Spinler9e9f5992020-09-30 08:29:24 -0500210 if (std::any_of(_fans.begin(), _fans.end(), [](const auto& fan) {
Patrick Williamsdfddd642024-08-16 15:21:51 -0400211 return std::get<std::optional<size_t>>(std::get<Fan>(fan)) !=
212 std::nullopt;
213 }))
Matt Spinlere8122392020-09-24 13:22:18 -0500214 {
Matt Spinler9e9f5992020-09-30 08:29:24 -0500215 _reporter = std::make_unique<ErrorReporter>(_bus, _fans);
Matt Spinlere8122392020-09-24 13:22:18 -0500216 }
Matthew Barthaa8d81d2019-11-21 14:07:31 -0600217}
218
Patrick Williams4fa67aa2025-02-03 14:28:47 -0500219std::unique_ptr<RedundancyPolicy> JsonConfig::getPolicy(
220 const json& rpolicy, const fanPolicy& fpolicy,
221 std::unique_ptr<EEPROMDevice> eepromDevice)
Matthew Barthaa8d81d2019-11-21 14:07:31 -0600222{
223 if (!rpolicy.contains("type"))
224 {
Anwaar Hadiebead9a2025-03-19 20:35:57 +0000225 lg2::error(
226 "Missing required fan presence policy type for fan {FAN_NAME}",
227 "FAN_NAME", std::get<fanPolicyFanPos>(std::get<Fan>(fpolicy)));
Matthew Barthaa8d81d2019-11-21 14:07:31 -0600228 throw std::runtime_error("Missing required fan presence policy type");
229 }
230
231 // The redundancy policy type for fan presence detection
232 // (Must have a supported function within the rpolicy namespace)
233 auto type = rpolicy["type"].get<std::string>();
234 std::transform(type.begin(), type.end(), type.begin(), tolower);
235 auto func = _rpolicies.find(type);
236 if (func != _rpolicies.end())
237 {
Matthew Barthf3e70472019-12-03 13:33:20 -0600238 // Call function for redundancy policy type and return the policy
Matt Spinlerbc4179e2022-10-04 15:15:06 -0500239 return func->second(fpolicy, std::move(eepromDevice));
Matthew Barthaa8d81d2019-11-21 14:07:31 -0600240 }
241 else
242 {
Anwaar Hadiebead9a2025-03-19 20:35:57 +0000243 lg2::error(
244 "Invalid fan presence policy type {RPOLICY_TYPE} for fan {FAN_NAME}",
245 "FAN_NAME", std::get<fanPolicyFanPos>(std::get<Fan>(fpolicy)),
246 "RPOLICY_TYPE", type);
Matthew Barthaa8d81d2019-11-21 14:07:31 -0600247 throw std::runtime_error("Invalid fan presence methods policy type");
Matthew Barth4a94dec2019-11-15 10:40:47 -0600248 }
249}
250
Matthew Barthe7566632019-11-18 16:13:04 -0600251/**
252 * Methods of fan presence detection function definitions
253 */
254namespace method
255{
Matthew Barth2d2caa32020-05-26 11:07:24 -0500256// Get a constructed presence sensor for fan presence detection by tach
257std::unique_ptr<PresenceSensor> getTach(size_t fanIndex, const json& method)
258{
259 if (!method.contains("sensors") || method["sensors"].size() == 0)
Matthew Barthe7566632019-11-18 16:13:04 -0600260 {
Anwaar Hadiebead9a2025-03-19 20:35:57 +0000261 lg2::error(
262 "Missing required tach method property 'sensors' for fan index {FAN_ENTRY}",
263 "FAN_ENTRY", fanIndex);
Matthew Barth2d2caa32020-05-26 11:07:24 -0500264 throw std::runtime_error("Missing required tach method properties");
Matthew Barthe7566632019-11-18 16:13:04 -0600265 }
266
Matthew Barth2d2caa32020-05-26 11:07:24 -0500267 std::vector<std::string> sensors;
268 for (auto& sensor : method["sensors"])
Matthew Barthe7566632019-11-18 16:13:04 -0600269 {
Matthew Barth2d2caa32020-05-26 11:07:24 -0500270 sensors.emplace_back(sensor.get<std::string>());
Matthew Barthe7566632019-11-18 16:13:04 -0600271 }
272
Patrick Williamsdfddd642024-08-16 15:21:51 -0400273 return std::make_unique<PolicyAccess<Tach, JsonConfig>>(
274 fanIndex, std::move(sensors));
Matthew Barth2d2caa32020-05-26 11:07:24 -0500275}
276
277// Get a constructed presence sensor for fan presence detection by gpio
278std::unique_ptr<PresenceSensor> getGpio(size_t fanIndex, const json& method)
279{
280 if (!method.contains("physpath") || !method.contains("devpath") ||
281 !method.contains("key"))
282 {
Anwaar Hadiebead9a2025-03-19 20:35:57 +0000283 lg2::error(
284 "Missing one of the required gpio method properties for fan index {FAN_ENTRY}, which are: 'physpath, devpath, key'",
285 "FAN_ENTRY", fanIndex);
Matthew Barth2d2caa32020-05-26 11:07:24 -0500286 throw std::runtime_error("Missing required gpio method properties");
287 }
288
289 auto physpath = method["physpath"].get<std::string>();
290 auto devpath = method["devpath"].get<std::string>();
291 auto key = method["key"].get<unsigned int>();
292
Mike Cappsa35a8902021-06-10 10:20:14 -0400293 try
294 {
295 return std::make_unique<PolicyAccess<Gpio, JsonConfig>>(
296 fanIndex, physpath, devpath, key);
297 }
298 catch (const sdbusplus::exception_t& e)
299 {
300 namespace sdlogging = sdbusplus::xyz::openbmc_project::Logging::server;
301
Anwaar Hadiebead9a2025-03-19 20:35:57 +0000302 lg2::error(
303 "Error creating Gpio device bridge, hardware not detected: {ERROR}",
304 "ERROR", e);
Mike Cappsa35a8902021-06-10 10:20:14 -0400305
306 auto severity =
307 sdlogging::convertForMessage(sdlogging::Entry::Level::Error);
308
309 std::map<std::string, std::string> additionalData{
310 {"PHYSPATH", physpath},
311 {"DEVPATH", devpath},
312 {"FANINDEX", std::to_string(fanIndex)}};
313
314 try
315 {
Mike Cappsa35a8902021-06-10 10:20:14 -0400316 util::SDBusPlus::lookupAndCallMethod(
317 loggingPath, loggingCreateIface, "Create",
318 "xyz.openbmc_project.Fan.Presence.Error.GPIODeviceUnavailable",
319 severity, additionalData);
320 }
321 catch (const util::DBusError& e)
322 {
Anwaar Hadiebead9a2025-03-19 20:35:57 +0000323 lg2::error(
324 "Call to create an error log for presence-sensor failure failed: {ERROR}",
325 "ERROR", e);
Mike Cappsa35a8902021-06-10 10:20:14 -0400326 }
327
328 return std::make_unique<PolicyAccess<NullGpio, JsonConfig>>();
329 }
Matthew Barth2d2caa32020-05-26 11:07:24 -0500330}
331
Matthew Barthe7566632019-11-18 16:13:04 -0600332} // namespace method
333
Matthew Barthaa8d81d2019-11-21 14:07:31 -0600334/**
335 * Redundancy policies for fan presence detection function definitions
336 */
337namespace rpolicy
338{
Matthew Barth2d2caa32020-05-26 11:07:24 -0500339// Get an `Anyof` redundancy policy for the fan
Patrick Williams4fa67aa2025-02-03 14:28:47 -0500340std::unique_ptr<RedundancyPolicy> getAnyof(
341 const fanPolicy& fan, std::unique_ptr<EEPROMDevice> eepromDevice)
Matthew Barth2d2caa32020-05-26 11:07:24 -0500342{
343 std::vector<std::reference_wrapper<PresenceSensor>> pSensors;
344 for (auto& fanSensor : std::get<fanPolicySensorListPos>(fan))
Matthew Barthaa8d81d2019-11-21 14:07:31 -0600345 {
Matthew Barth2d2caa32020-05-26 11:07:24 -0500346 pSensors.emplace_back(*fanSensor);
Matthew Barthaa8d81d2019-11-21 14:07:31 -0600347 }
348
Matt Spinlerbc4179e2022-10-04 15:15:06 -0500349 return std::make_unique<AnyOf>(std::get<fanPolicyFanPos>(fan), pSensors,
350 std::move(eepromDevice));
Matthew Barth2d2caa32020-05-26 11:07:24 -0500351}
Matthew Barth26ad44a2019-11-22 15:37:14 -0600352
Matthew Barth2d2caa32020-05-26 11:07:24 -0500353// Get a `Fallback` redundancy policy for the fan
Patrick Williamsdfddd642024-08-16 15:21:51 -0400354std::unique_ptr<RedundancyPolicy> getFallback(
355 const fanPolicy& fan, std::unique_ptr<EEPROMDevice> eepromDevice)
Matthew Barth2d2caa32020-05-26 11:07:24 -0500356{
357 std::vector<std::reference_wrapper<PresenceSensor>> pSensors;
358 for (auto& fanSensor : std::get<fanPolicySensorListPos>(fan))
359 {
360 // Place in the order given to fallback correctly
361 pSensors.emplace_back(*fanSensor);
Matthew Barthaa8d81d2019-11-21 14:07:31 -0600362 }
363
Matt Spinlerbc4179e2022-10-04 15:15:06 -0500364 return std::make_unique<Fallback>(std::get<fanPolicyFanPos>(fan), pSensors,
365 std::move(eepromDevice));
Matthew Barth2d2caa32020-05-26 11:07:24 -0500366}
367
368} // namespace rpolicy
Matthew Barthaa8d81d2019-11-21 14:07:31 -0600369
Matthew Barthfd05d642019-11-14 15:01:57 -0600370} // namespace presence
371} // namespace fan
372} // namespace phosphor