blob: 4d06540e9b1c24a6dce70a000c7c31f9c3223db4 [file] [log] [blame]
Matthew Barth9ea8bee2020-06-04 14:27:19 -05001/**
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#include "json_parser.hpp"
17
Matthew Barth22ab93b2020-06-08 10:47:56 -050018#include "conditions.hpp"
Matthew Barth9ea8bee2020-06-04 14:27:19 -050019#include "json_config.hpp"
20#include "nonzero_speed_trust.hpp"
Matt Spinlerf06ab072020-10-14 12:58:22 -050021#include "power_interface.hpp"
22#include "power_off_rule.hpp"
Jolie Ku69f2f482020-10-21 09:59:43 +080023#include "tach_sensor.hpp"
Matthew Barth9ea8bee2020-06-04 14:27:19 -050024#include "types.hpp"
25
Matt Spinlerf06ab072020-10-14 12:58:22 -050026#include <fmt/format.h>
27
Matthew Barth9ea8bee2020-06-04 14:27:19 -050028#include <nlohmann/json.hpp>
29#include <phosphor-logging/log.hpp>
30
31#include <algorithm>
32#include <map>
33#include <memory>
Matthew Barth22ab93b2020-06-08 10:47:56 -050034#include <optional>
Matthew Barth9ea8bee2020-06-04 14:27:19 -050035#include <vector>
36
37namespace phosphor::fan::monitor
38{
39
40using json = nlohmann::json;
41using namespace phosphor::logging;
42
43namespace tClass
44{
45
46// Get a constructed trust group class for a non-zero speed group
47CreateGroupFunction
48 getNonZeroSpeed(const std::vector<trust::GroupDefinition>& group)
49{
50 return [group]() {
51 return std::make_unique<trust::NonzeroSpeed>(std::move(group));
52 };
53}
54
55} // namespace tClass
56
57const std::map<std::string, trustHandler> trusts = {
58 {"nonzerospeed", tClass::getNonZeroSpeed}};
Matthew Barth3ad14342020-06-08 16:17:42 -050059const std::map<std::string, condHandler> conditions = {
60 {"propertiesmatch", condition::getPropertiesMatch}};
Jolie Ku69f2f482020-10-21 09:59:43 +080061const std::map<std::string, size_t> methods = {
62 {"timebased", MethodMode::timebased}, {"count", MethodMode::count}};
Matthew Barth9ea8bee2020-06-04 14:27:19 -050063
64const std::vector<CreateGroupFunction> getTrustGrps(const json& obj)
65{
66 std::vector<CreateGroupFunction> grpFuncs;
67
68 if (obj.contains("sensor_trust_groups"))
69 {
70 for (auto& stg : obj["sensor_trust_groups"])
71 {
72 if (!stg.contains("class") || !stg.contains("group"))
73 {
74 // Log error on missing required parameters
75 log<level::ERR>(
76 "Missing required fan monitor trust group parameters",
77 entry("REQUIRED_PARAMETERS=%s", "{class, group}"));
78 throw std::runtime_error(
79 "Missing required fan trust group parameters");
80 }
81 auto tgClass = stg["class"].get<std::string>();
82 std::vector<trust::GroupDefinition> group;
83 for (auto& member : stg["group"])
84 {
85 // Construct list of group members
86 if (!member.contains("name"))
87 {
88 // Log error on missing required parameter
89 log<level::ERR>(
90 "Missing required fan monitor trust group member name",
91 entry("CLASS=%s", tgClass.c_str()));
92 throw std::runtime_error(
93 "Missing required fan monitor trust group member name");
94 }
95 auto in_trust = true;
96 if (member.contains("in_trust"))
97 {
98 in_trust = member["in_trust"].get<bool>();
99 }
100 group.emplace_back(trust::GroupDefinition{
101 member["name"].get<std::string>(), in_trust});
102 }
103 // The class for fan sensor trust groups
104 // (Must have a supported function within the tClass namespace)
105 std::transform(tgClass.begin(), tgClass.end(), tgClass.begin(),
106 tolower);
107 auto handler = trusts.find(tgClass);
108 if (handler != trusts.end())
109 {
110 // Call function for trust group class
111 grpFuncs.emplace_back(handler->second(group));
112 }
113 else
114 {
115 // Log error on unsupported trust group class
116 log<level::ERR>("Invalid fan monitor trust group class",
117 entry("CLASS=%s", tgClass.c_str()));
118 throw std::runtime_error(
119 "Invalid fan monitor trust group class");
120 }
121 }
122 }
123
124 return grpFuncs;
125}
126
Matthew Barth22ab93b2020-06-08 10:47:56 -0500127const std::vector<SensorDefinition> getSensorDefs(const json& sensors)
128{
129 std::vector<SensorDefinition> sensorDefs;
130
131 for (const auto& sensor : sensors)
132 {
133 if (!sensor.contains("name") || !sensor.contains("has_target"))
134 {
135 // Log error on missing required parameters
136 log<level::ERR>(
137 "Missing required fan sensor definition parameters",
138 entry("REQUIRED_PARAMETERS=%s", "{name, has_target}"));
139 throw std::runtime_error(
140 "Missing required fan sensor definition parameters");
141 }
142 // Target interface is optional and defaults to
143 // 'xyz.openbmc_project.Control.FanSpeed'
144 std::string targetIntf = "xyz.openbmc_project.Control.FanSpeed";
145 if (sensor.contains("target_interface"))
146 {
147 targetIntf = sensor["target_interface"].get<std::string>();
148 }
Chau Ly27cc39f2022-09-20 08:16:56 +0000149 // Target path is optional
150 std::string targetPath;
151 if (sensor.contains("target_path"))
152 {
153 targetPath = sensor["target_path"].get<std::string>();
154 }
Matthew Barth22ab93b2020-06-08 10:47:56 -0500155 // Factor is optional and defaults to 1
Matt Spinler18fb12b2023-05-09 11:17:42 -0500156 double factor = 1.0;
Matthew Barth22ab93b2020-06-08 10:47:56 -0500157 if (sensor.contains("factor"))
158 {
159 factor = sensor["factor"].get<double>();
160 }
161 // Offset is optional and defaults to 0
Matt Spinler18fb12b2023-05-09 11:17:42 -0500162 int64_t offset = 0;
Matthew Barth22ab93b2020-06-08 10:47:56 -0500163 if (sensor.contains("offset"))
164 {
165 offset = sensor["offset"].get<int64_t>();
166 }
Jolie Ku69f2f482020-10-21 09:59:43 +0800167 // Threshold is optional and defaults to 1
Matt Spinler18fb12b2023-05-09 11:17:42 -0500168 size_t threshold = 1;
Jolie Ku69f2f482020-10-21 09:59:43 +0800169 if (sensor.contains("threshold"))
170 {
171 threshold = sensor["threshold"].get<size_t>();
172 }
Matthew Barth8a8aa442021-11-19 14:13:13 -0600173 // Ignore being above the allowed max is optional, defaults to not
174 bool ignoreAboveMax = false;
175 if (sensor.contains("ignore_above_max"))
176 {
177 ignoreAboveMax = sensor["ignore_above_max"].get<bool>();
178 }
Matthew Barth22ab93b2020-06-08 10:47:56 -0500179
Matt Spinler18fb12b2023-05-09 11:17:42 -0500180 SensorDefinition def{.name = sensor["name"].get<std::string>(),
181 .hasTarget = sensor["has_target"].get<bool>(),
182 .targetInterface = targetIntf,
183 .targetPath = targetPath,
184 .factor = factor,
185 .offset = offset,
186 .threshold = threshold,
187 .ignoreAboveMax = ignoreAboveMax};
188
189 sensorDefs.push_back(std::move(def));
Matthew Barth22ab93b2020-06-08 10:47:56 -0500190 }
191
192 return sensorDefs;
193}
194
195const std::vector<FanDefinition> getFanDefs(const json& obj)
196{
197 std::vector<FanDefinition> fanDefs;
198
199 for (const auto& fan : obj["fans"])
200 {
Jolie Ku69f2f482020-10-21 09:59:43 +0800201 if (!fan.contains("inventory") || !fan.contains("deviation") ||
202 !fan.contains("sensors"))
Matthew Barth22ab93b2020-06-08 10:47:56 -0500203 {
204 // Log error on missing required parameters
205 log<level::ERR>(
206 "Missing required fan monitor definition parameters",
207 entry("REQUIRED_PARAMETERS=%s",
Jolie Ku69f2f482020-10-21 09:59:43 +0800208 "{inventory, deviation, sensors}"));
Matthew Barth22ab93b2020-06-08 10:47:56 -0500209 throw std::runtime_error(
210 "Missing required fan monitor definition parameters");
211 }
Matthew Barth4c4de262021-02-17 13:03:02 -0600212 // Valid deviation range is 0 - 100%
213 auto deviation = fan["deviation"].get<size_t>();
Mike Capps808d7fe2022-06-13 10:12:16 -0400214 if (100 < deviation)
Matthew Barth4c4de262021-02-17 13:03:02 -0600215 {
Mike Capps78182482022-06-23 11:28:26 -0400216 auto msg = fmt::format(
217 "Invalid deviation of {} found, must be between 0 and 100",
218 deviation);
219
220 log<level::ERR>(msg.c_str());
221 throw std::runtime_error(msg.c_str());
Matthew Barth4c4de262021-02-17 13:03:02 -0600222 }
223
Matt Spinlerf724c162023-05-10 11:14:37 -0500224 // Upper deviation defaults to the deviation value and
225 // can also be separately specified.
226 size_t upperDeviation = deviation;
227 if (fan.contains("upper_deviation"))
228 {
229 upperDeviation = fan["upper_deviation"].get<size_t>();
230 if (100 < upperDeviation)
231 {
232 auto msg =
233 fmt::format("Invalid upper_deviation of {} found, must "
234 "be between 0 and 100",
235 upperDeviation);
236
237 log<level::ERR>(msg.c_str());
238 throw std::runtime_error(msg.c_str());
239 }
240 }
241
Matthew Barth22ab93b2020-06-08 10:47:56 -0500242 // Construct the sensor definitions for this fan
243 auto sensorDefs = getSensorDefs(fan["sensors"]);
244
245 // Functional delay is optional and defaults to 0
246 size_t funcDelay = 0;
247 if (fan.contains("functional_delay"))
248 {
249 funcDelay = fan["functional_delay"].get<size_t>();
250 }
251
Jolie Ku69f2f482020-10-21 09:59:43 +0800252 // Method is optional and defaults to time based functional
253 // determination
254 size_t method = MethodMode::timebased;
Matt Spinler623635c2021-03-29 13:13:59 -0500255 size_t countInterval = 1;
Jolie Ku69f2f482020-10-21 09:59:43 +0800256 if (fan.contains("method"))
257 {
258 auto methodConf = fan["method"].get<std::string>();
259 auto methodFunc = methods.find(methodConf);
260 if (methodFunc != methods.end())
261 {
262 method = methodFunc->second;
263 }
264 else
265 {
266 // Log error on unsupported method parameter
267 log<level::ERR>("Invalid fan method");
268 throw std::runtime_error("Invalid fan method");
269 }
Matt Spinler623635c2021-03-29 13:13:59 -0500270
271 // Read the count interval value used with the count method.
272 if (method == MethodMode::count)
273 {
274 if (fan.contains("count_interval"))
275 {
276 countInterval = fan["count_interval"].get<size_t>();
277 }
278 }
Jolie Ku69f2f482020-10-21 09:59:43 +0800279 }
280
281 // Timeout defaults to 0
282 size_t timeout = 0;
283 if (method == MethodMode::timebased)
284 {
285 if (!fan.contains("allowed_out_of_range_time"))
286 {
287 // Log error on missing required parameter
288 log<level::ERR>(
289 "Missing required fan monitor definition parameters",
290 entry("REQUIRED_PARAMETER=%s",
291 "{allowed_out_of_range_time}"));
292 throw std::runtime_error(
293 "Missing required fan monitor definition parameters");
294 }
295 else
296 {
297 timeout = fan["allowed_out_of_range_time"].get<size_t>();
298 }
299 }
300
Matt Spinlerb0412d02020-10-12 16:53:52 -0500301 // Monitor start delay is optional and defaults to 0
302 size_t monitorDelay = 0;
303 if (fan.contains("monitor_start_delay"))
304 {
305 monitorDelay = fan["monitor_start_delay"].get<size_t>();
306 }
307
Matt Spinlerae1f8ef2020-10-14 16:15:51 -0500308 // num_sensors_nonfunc_for_fan_nonfunc is optional and defaults
309 // to zero if not present, meaning the code will not set the
310 // parent fan to nonfunctional based on sensors.
311 size_t nonfuncSensorsCount = 0;
312 if (fan.contains("num_sensors_nonfunc_for_fan_nonfunc"))
313 {
314 nonfuncSensorsCount =
315 fan["num_sensors_nonfunc_for_fan_nonfunc"].get<size_t>();
316 }
317
Matt Spinlerf13b42e2020-10-26 15:29:49 -0500318 // nonfunc_rotor_error_delay is optional, though it will
319 // default to zero if 'fault_handling' is present.
320 std::optional<size_t> nonfuncRotorErrorDelay;
321 if (fan.contains("nonfunc_rotor_error_delay"))
322 {
323 nonfuncRotorErrorDelay =
324 fan["nonfunc_rotor_error_delay"].get<size_t>();
325 }
326 else if (obj.contains("fault_handling"))
327 {
328 nonfuncRotorErrorDelay = 0;
329 }
330
Matt Spinler27f6b682020-10-27 08:43:37 -0500331 // fan_missing_error_delay is optional.
332 std::optional<size_t> fanMissingErrorDelay;
333 if (fan.contains("fan_missing_error_delay"))
334 {
335 fanMissingErrorDelay =
336 fan.at("fan_missing_error_delay").get<size_t>();
337 }
338
Matthew Barth3ad14342020-06-08 16:17:42 -0500339 // Handle optional conditions
Matthew Barth8a0c2322020-06-17 09:53:10 -0500340 auto cond = std::optional<Condition>();
Matthew Barth3ad14342020-06-08 16:17:42 -0500341 if (fan.contains("condition"))
342 {
343 if (!fan["condition"].contains("name"))
344 {
345 // Log error on missing required parameter
346 log<level::ERR>(
347 "Missing required fan monitor condition parameter",
348 entry("REQUIRED_PARAMETER=%s", "{name}"));
349 throw std::runtime_error(
350 "Missing required fan monitor condition parameter");
351 }
352 auto name = fan["condition"]["name"].get<std::string>();
353 // The function for fan monitoring condition
354 // (Must have a supported function within the condition namespace)
355 std::transform(name.begin(), name.end(), name.begin(), tolower);
356 auto handler = conditions.find(name);
357 if (handler != conditions.end())
358 {
359 cond = handler->second(fan["condition"]);
360 }
361 else
362 {
363 log<level::INFO>(
364 "No handler found for configured condition",
365 entry("CONDITION_NAME=%s", name.c_str()),
366 entry("JSON_DUMP=%s", fan["condition"].dump().c_str()));
367 }
368 }
Jolie Ku69f2f482020-10-21 09:59:43 +0800369
Matt Spinlera3584bd2021-03-29 15:48:30 -0500370 // if the fan should be set to functional when plugged in
371 bool setFuncOnPresent = false;
372 if (fan.contains("set_func_on_present"))
373 {
374 setFuncOnPresent = fan["set_func_on_present"].get<bool>();
375 }
376
Matt Spinler18fb12b2023-05-09 11:17:42 -0500377 FanDefinition def{.name = fan["inventory"].get<std::string>(),
378 .method = method,
379 .funcDelay = funcDelay,
380 .timeout = timeout,
381 .deviation = deviation,
Matt Spinlerf724c162023-05-10 11:14:37 -0500382 .upperDeviation = upperDeviation,
Matt Spinler18fb12b2023-05-09 11:17:42 -0500383 .numSensorFailsForNonfunc = nonfuncSensorsCount,
384 .monitorStartDelay = monitorDelay,
385 .countInterval = countInterval,
386 .nonfuncRotorErrDelay = nonfuncRotorErrorDelay,
387 .fanMissingErrDelay = fanMissingErrorDelay,
388 .sensorList = std::move(sensorDefs),
389 .condition = cond,
390 .funcOnPresent = setFuncOnPresent};
391
392 fanDefs.push_back(std::move(def));
Matthew Barth22ab93b2020-06-08 10:47:56 -0500393 }
394
395 return fanDefs;
396}
397
Matt Spinlerf06ab072020-10-14 12:58:22 -0500398PowerRuleState getPowerOffPowerRuleState(const json& powerOffConfig)
399{
400 // The state is optional and defaults to runtime
401 PowerRuleState ruleState{PowerRuleState::runtime};
402
403 if (powerOffConfig.contains("state"))
404 {
405 auto state = powerOffConfig.at("state").get<std::string>();
406 if (state == "at_pgood")
407 {
408 ruleState = PowerRuleState::atPgood;
409 }
410 else if (state != "runtime")
411 {
412 auto msg = fmt::format("Invalid power off state entry {}", state);
413 log<level::ERR>(msg.c_str());
414 throw std::runtime_error(msg.c_str());
415 }
416 }
417
418 return ruleState;
419}
420
421std::unique_ptr<PowerOffCause> getPowerOffCause(const json& powerOffConfig)
422{
423 std::unique_ptr<PowerOffCause> cause;
424
425 if (!powerOffConfig.contains("count") || !powerOffConfig.contains("cause"))
426 {
427 const auto msg =
428 "Missing 'count' or 'cause' entries in power off config";
429 log<level::ERR>(msg);
430 throw std::runtime_error(msg);
431 }
432
433 auto count = powerOffConfig.at("count").get<size_t>();
434 auto powerOffCause = powerOffConfig.at("cause").get<std::string>();
435
436 const std::map<std::string, std::function<std::unique_ptr<PowerOffCause>()>>
437 causes{
438 {"missing_fan_frus",
439 [count]() { return std::make_unique<MissingFanFRUCause>(count); }},
440 {"nonfunc_fan_rotors", [count]() {
441 return std::make_unique<NonfuncFanRotorCause>(count);
442 }}};
443
444 auto it = causes.find(powerOffCause);
445 if (it != causes.end())
446 {
447 cause = it->second();
448 }
449 else
450 {
451 auto msg =
452 fmt::format("Invalid power off cause {} in power off config JSON",
453 powerOffCause);
454 log<level::ERR>(msg.c_str());
455 throw std::runtime_error(msg.c_str());
456 }
457
458 return cause;
459}
460
461std::unique_ptr<PowerOffAction>
462 getPowerOffAction(const json& powerOffConfig,
Matt Spinlerac1efc12020-10-27 10:20:11 -0500463 std::shared_ptr<PowerInterfaceBase>& powerInterface,
464 PowerOffAction::PrePowerOffFunc& func)
Matt Spinlerf06ab072020-10-14 12:58:22 -0500465{
466 std::unique_ptr<PowerOffAction> action;
467 if (!powerOffConfig.contains("type"))
468 {
469 const auto msg = "Missing 'type' entry in power off config";
470 log<level::ERR>(msg);
471 throw std::runtime_error(msg);
472 }
473
474 auto type = powerOffConfig.at("type").get<std::string>();
475
476 if (((type == "hard") || (type == "soft")) &&
477 !powerOffConfig.contains("delay"))
478 {
479 const auto msg = "Missing 'delay' entry in power off config";
480 log<level::ERR>(msg);
481 throw std::runtime_error(msg);
482 }
483 else if ((type == "epow") &&
484 (!powerOffConfig.contains("service_mode_delay") ||
485 !powerOffConfig.contains("meltdown_delay")))
486 {
487 const auto msg = "Missing 'service_mode_delay' or 'meltdown_delay' "
488 "entry in power off config";
489 log<level::ERR>(msg);
490 throw std::runtime_error(msg);
491 }
492
493 if (type == "hard")
494 {
495 action = std::make_unique<HardPowerOff>(
Matt Spinlerac1efc12020-10-27 10:20:11 -0500496 powerOffConfig.at("delay").get<uint32_t>(), powerInterface, func);
Matt Spinlerf06ab072020-10-14 12:58:22 -0500497 }
498 else if (type == "soft")
499 {
500 action = std::make_unique<SoftPowerOff>(
Matt Spinlerac1efc12020-10-27 10:20:11 -0500501 powerOffConfig.at("delay").get<uint32_t>(), powerInterface, func);
Matt Spinlerf06ab072020-10-14 12:58:22 -0500502 }
503 else if (type == "epow")
504 {
505 action = std::make_unique<EpowPowerOff>(
506 powerOffConfig.at("service_mode_delay").get<uint32_t>(),
Matt Spinlerac1efc12020-10-27 10:20:11 -0500507 powerOffConfig.at("meltdown_delay").get<uint32_t>(), powerInterface,
508 func);
Matt Spinlerf06ab072020-10-14 12:58:22 -0500509 }
510 else
511 {
Patrick Williams61b73292023-05-10 07:50:12 -0500512 auto msg = fmt::format("Invalid 'type' entry {} in power off config",
513 type);
Matt Spinlerf06ab072020-10-14 12:58:22 -0500514 log<level::ERR>(msg.c_str());
515 throw std::runtime_error(msg.c_str());
516 }
517
518 return action;
519}
520
521std::vector<std::unique_ptr<PowerOffRule>>
522 getPowerOffRules(const json& obj,
Matt Spinlerac1efc12020-10-27 10:20:11 -0500523 std::shared_ptr<PowerInterfaceBase>& powerInterface,
524 PowerOffAction::PrePowerOffFunc& func)
Matt Spinlerf06ab072020-10-14 12:58:22 -0500525{
526 std::vector<std::unique_ptr<PowerOffRule>> rules;
527
528 if (!(obj.contains("fault_handling") &&
529 obj.at("fault_handling").contains("power_off_config")))
530 {
531 return rules;
532 }
533
534 for (const auto& config : obj.at("fault_handling").at("power_off_config"))
535 {
536 auto state = getPowerOffPowerRuleState(config);
537 auto cause = getPowerOffCause(config);
Matt Spinlerac1efc12020-10-27 10:20:11 -0500538 auto action = getPowerOffAction(config, powerInterface, func);
Matt Spinlerf06ab072020-10-14 12:58:22 -0500539
540 auto rule = std::make_unique<PowerOffRule>(
541 std::move(state), std::move(cause), std::move(action));
542 rules.push_back(std::move(rule));
543 }
544
545 return rules;
546}
547
Matt Spinlerf13b42e2020-10-26 15:29:49 -0500548std::optional<size_t> getNumNonfuncRotorsBeforeError(const json& obj)
549{
550 std::optional<size_t> num;
551
552 if (obj.contains("fault_handling"))
553 {
554 // Defaults to 1 if not present inside of 'fault_handling'.
555 num = obj.at("fault_handling")
556 .value("num_nonfunc_rotors_before_error", 1);
557 }
558
559 return num;
560}
561
Matthew Barth9ea8bee2020-06-04 14:27:19 -0500562} // namespace phosphor::fan::monitor