blob: 09637361446ec1dbe3650fd63a8e5ed95e3baa4d [file] [log] [blame]
Patrick Venture863b9242018-03-08 08:29:23 -08001/**
2 * Copyright 2017 Google Inc.
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 */
Jonico Eustaquioaf97d8e2024-01-02 14:35:07 -060016#include "config.h"
17
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070018#include "dbuspassive.hpp"
19
Patrick Ventureaadb30d2020-08-10 09:17:11 -070020#include "dbushelper_interface.hpp"
James Feist98b704e2019-06-03 16:24:53 -070021#include "dbuspassiveredundancy.hpp"
Patrick Ventureaadb30d2020-08-10 09:17:11 -070022#include "dbusutil.hpp"
James Feist0c8223b2019-05-08 15:33:33 -070023#include "util.hpp"
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070024
Patrick Venturea83a3ec2020-08-04 09:52:05 -070025#include <sdbusplus/bus.hpp>
26
Patrick Venture863b9242018-03-08 08:29:23 -080027#include <chrono>
28#include <cmath>
Patrick Venture0ef1faf2018-06-13 12:50:53 -070029#include <memory>
Patrick Venture863b9242018-03-08 08:29:23 -080030#include <mutex>
Patrick Venture0ef1faf2018-06-13 12:50:53 -070031#include <string>
James Feist1f802f52019-02-08 13:51:43 -080032#include <variant>
Patrick Venture863b9242018-03-08 08:29:23 -080033
Patrick Venturea0764872020-08-08 07:48:43 -070034namespace pid_control
35{
36
Patrick Venture563a3562018-10-30 09:31:26 -070037std::unique_ptr<ReadInterface> DbusPassive::createDbusPassive(
Patrick Williamsb228bc32022-07-22 19:26:56 -050038 sdbusplus::bus_t& bus, const std::string& type, const std::string& id,
Patrick Venture8729eb92020-08-10 10:38:44 -070039 std::unique_ptr<DbusHelperInterface> helper, const conf::SensorConfig* info,
James Feist98b704e2019-06-03 16:24:53 -070040 const std::shared_ptr<DbusPassiveRedundancy>& redundancy)
Patrick Venture0ef1faf2018-06-13 12:50:53 -070041{
42 if (helper == nullptr)
43 {
44 return nullptr;
45 }
Patrick Venture7af157b2018-10-30 11:24:40 -070046 if (!validType(type))
Patrick Venture0ef1faf2018-06-13 12:50:53 -070047 {
48 return nullptr;
49 }
50
Patrick Venture863b9242018-03-08 08:29:23 -080051 /* Need to get the scale and initial value */
Patrick Venture863b9242018-03-08 08:29:23 -080052 /* service == busname */
Harvey.Wuf2efcbb2022-02-09 10:24:30 +080053 std::string path;
54 if (info->readPath.empty())
55 {
56 path = getSensorPath(type, id);
57 }
58 else
59 {
60 path = info->readPath;
61 }
Patrick Venture34ddc902018-10-30 11:05:17 -070062
Patrick Venture1df9e872020-10-08 15:35:01 -070063 SensorProperties settings;
Patrick Venturef8cb4642018-10-30 12:02:53 -070064 bool failed;
Patrick Venture863b9242018-03-08 08:29:23 -080065
Patrick Venturef8cb4642018-10-30 12:02:53 -070066 try
67 {
Patrick Venture9b936922020-08-10 11:28:39 -070068 std::string service = helper->getService(sensorintf, path);
Patrick Venturef8cb4642018-10-30 12:02:53 -070069
Patrick Venture9b936922020-08-10 11:28:39 -070070 helper->getProperties(service, path, &settings);
71 failed = helper->thresholdsAsserted(service, path);
Patrick Venturef8cb4642018-10-30 12:02:53 -070072 }
73 catch (const std::exception& e)
74 {
75 return nullptr;
76 }
77
Patrick Venture6b9f5992019-09-10 09:18:28 -070078 /* if these values are zero, they're ignored. */
79 if (info->ignoreDbusMinMax)
80 {
81 settings.min = 0;
82 settings.max = 0;
83 }
84
Alex.Song8f73ad72021-10-07 00:18:27 +080085 settings.unavailableAsFailed = info->unavailableAsFailed;
86
Patrick Venture8729eb92020-08-10 10:38:44 -070087 return std::make_unique<DbusPassive>(bus, type, id, std::move(helper),
88 settings, failed, path, redundancy);
Patrick Venturef8cb4642018-10-30 12:02:53 -070089}
90
James Feist98b704e2019-06-03 16:24:53 -070091DbusPassive::DbusPassive(
Patrick Williamsb228bc32022-07-22 19:26:56 -050092 sdbusplus::bus_t& bus, const std::string& type, const std::string& id,
Patrick Venture8729eb92020-08-10 10:38:44 -070093 std::unique_ptr<DbusHelperInterface> helper,
Patrick Venture1df9e872020-10-08 15:35:01 -070094 const SensorProperties& settings, bool failed, const std::string& path,
James Feist98b704e2019-06-03 16:24:53 -070095 const std::shared_ptr<DbusPassiveRedundancy>& redundancy) :
Patrick Venturef8cb4642018-10-30 12:02:53 -070096 ReadInterface(),
Patrick Williamsd5d83fa2023-07-13 17:40:57 -050097 _signal(bus, getMatch(path), dbusHandleSignal, this), _id(id),
Patrick Ventured0571302020-08-10 12:42:23 -070098 _helper(std::move(helper)), _failed(failed), path(path),
James Feist98b704e2019-06-03 16:24:53 -070099 redundancy(redundancy)
100
Patrick Venturef8cb4642018-10-30 12:02:53 -0700101{
Patrick Venture863b9242018-03-08 08:29:23 -0800102 _scale = settings.scale;
Josh Lehan3e2f7582020-09-20 22:06:03 -0700103 _min = settings.min * std::pow(10.0, _scale);
104 _max = settings.max * std::pow(10.0, _scale);
Alex.Song8f73ad72021-10-07 00:18:27 +0800105 _available = settings.available;
106 _unavailableAsFailed = settings.unavailableAsFailed;
Josh Lehan3e2f7582020-09-20 22:06:03 -0700107
108 // Cache this type knowledge, to avoid repeated string comparison
109 _typeMargin = (type == "margin");
Alex.Song8f73ad72021-10-07 00:18:27 +0800110 _typeFan = (type == "fan");
Josh Lehan3e2f7582020-09-20 22:06:03 -0700111
112 // Force value to be stored, otherwise member would be uninitialized
113 updateValue(settings.value, true);
Patrick Venture863b9242018-03-08 08:29:23 -0800114}
115
116ReadReturn DbusPassive::read(void)
117{
118 std::lock_guard<std::mutex> guard(_lock);
119
Josh Lehanb3005752022-02-22 20:48:07 -0800120 ReadReturn r = {_value, _updated, _unscaled};
Patrick Venture863b9242018-03-08 08:29:23 -0800121
122 return r;
123}
124
Josh Lehanb3005752022-02-22 20:48:07 -0800125void DbusPassive::setValue(double value, double unscaled)
Patrick Venture863b9242018-03-08 08:29:23 -0800126{
127 std::lock_guard<std::mutex> guard(_lock);
128
129 _value = value;
Josh Lehanb3005752022-02-22 20:48:07 -0800130 _unscaled = unscaled;
Patrick Venture863b9242018-03-08 08:29:23 -0800131 _updated = std::chrono::high_resolution_clock::now();
132}
133
Josh Lehanb3005752022-02-22 20:48:07 -0800134void DbusPassive::setValue(double value)
135{
136 // First param is scaled, second param is unscaled, assume same here
137 setValue(value, value);
138}
139
James Feist36b7d8e2018-10-05 15:39:01 -0700140bool DbusPassive::getFailed(void) const
141{
James Feist98b704e2019-06-03 16:24:53 -0700142 if (redundancy)
143 {
144 const std::set<std::string>& failures = redundancy->getFailed();
145 if (failures.find(path) != failures.end())
146 {
147 return true;
148 }
149 }
James Feist4b36f262020-07-07 16:56:41 -0700150
Alex.Song8f73ad72021-10-07 00:18:27 +0800151 /*
152 * Unavailable thermal sensors, who are not present or
153 * power-state-not-matching, should not trigger the failSafe mode. For
154 * example, when a system stays at a powered-off state, its CPU Temp
155 * sensors will be unavailable, these unavailable sensors should not be
156 * treated as failed and trigger failSafe.
157 * This is important for systems whose Fans are always on.
158 */
159 if (!_typeFan && !_available && !_unavailableAsFailed)
160 {
161 return false;
162 }
163
Josh Lehan3e2f7582020-09-20 22:06:03 -0700164 // If a reading has came in,
165 // but its value bad in some way (determined by sensor type),
166 // indicate this sensor has failed,
167 // until another value comes in that is no longer bad.
168 // This is different from the overall _failed flag,
169 // which is set and cleared by other causes.
170 if (_badReading)
171 {
172 return true;
173 }
174
175 // If a reading has came in, and it is not a bad reading,
176 // but it indicates there is no more thermal margin left,
177 // that is bad, something is wrong with the PID loops,
178 // they are not cooling the system, enable failsafe mode also.
179 if (_marginHot)
180 {
181 return true;
182 }
183
Alex.Song8f73ad72021-10-07 00:18:27 +0800184 return _failed || !_available || !_functional;
James Feist36b7d8e2018-10-05 15:39:01 -0700185}
186
187void DbusPassive::setFailed(bool value)
188{
189 _failed = value;
190}
191
James Feist4b36f262020-07-07 16:56:41 -0700192void DbusPassive::setFunctional(bool value)
193{
194 _functional = value;
195}
196
Alex.Song8f73ad72021-10-07 00:18:27 +0800197void DbusPassive::setAvailable(bool value)
198{
199 _available = value;
200}
201
Patrick Venture863b9242018-03-08 08:29:23 -0800202int64_t DbusPassive::getScale(void)
203{
204 return _scale;
205}
206
Patrick Venture563a3562018-10-30 09:31:26 -0700207std::string DbusPassive::getID(void)
Patrick Venture863b9242018-03-08 08:29:23 -0800208{
209 return _id;
210}
211
James Feist75eb7692019-02-25 12:50:02 -0800212double DbusPassive::getMax(void)
213{
214 return _max;
215}
216
217double DbusPassive::getMin(void)
218{
219 return _min;
220}
221
Josh Lehan3e2f7582020-09-20 22:06:03 -0700222void DbusPassive::updateValue(double value, bool force)
223{
224 _badReading = false;
225
226 // Do not let a NAN, or other floating-point oddity, be used to update
227 // the value, as that indicates the sensor has no valid reading.
228 if (!(std::isfinite(value)))
229 {
230 _badReading = true;
231
232 // Do not continue with a bad reading, unless caller forcing
233 if (!force)
234 {
235 return;
236 }
237 }
238
239 value *= std::pow(10.0, _scale);
240
241 auto unscaled = value;
242 scaleSensorReading(_min, _max, value);
243
244 if (_typeMargin)
245 {
246 _marginHot = false;
247
248 // Unlike an absolute temperature sensor,
249 // where 0 degrees C is a good reading,
250 // a value received of 0 (or negative) margin is worrisome,
251 // and should be flagged.
252 // Either it indicates margin not calculated properly,
253 // or somebody forgot to set the margin-zero setpoint,
254 // or the system is really overheating that much.
255 // This is a different condition from _failed
256 // and _badReading, so it merits its own flag.
257 // The sensor has not failed, the reading is good, but the zone
258 // still needs to know that it should go to failsafe mode.
259 if (unscaled <= 0.0)
260 {
261 _marginHot = true;
262 }
263 }
264
Josh Lehanb3005752022-02-22 20:48:07 -0800265 setValue(value, unscaled);
Josh Lehan3e2f7582020-09-20 22:06:03 -0700266}
267
Patrick Williamsb228bc32022-07-22 19:26:56 -0500268int handleSensorValue(sdbusplus::message_t& msg, DbusPassive* owner)
Patrick Venture863b9242018-03-08 08:29:23 -0800269{
Patrick Venture863b9242018-03-08 08:29:23 -0800270 std::string msgSensor;
James Feist1f802f52019-02-08 13:51:43 -0800271 std::map<std::string, std::variant<int64_t, double, bool>> msgData;
Patrick Ventured0c75662018-06-12 19:03:21 -0700272
273 msg.read(msgSensor, msgData);
Patrick Venture863b9242018-03-08 08:29:23 -0800274
275 if (msgSensor == "xyz.openbmc_project.Sensor.Value")
276 {
277 auto valPropMap = msgData.find("Value");
278 if (valPropMap != msgData.end())
279 {
Patrick Williams8c051122023-05-10 07:50:59 -0500280 double value = std::visit(VariantToDoubleVisitor(),
281 valPropMap->second);
Patrick Venture863b9242018-03-08 08:29:23 -0800282
Josh Lehan3e2f7582020-09-20 22:06:03 -0700283 owner->updateValue(value, false);
Patrick Venture863b9242018-03-08 08:29:23 -0800284 }
285 }
James Feist36b7d8e2018-10-05 15:39:01 -0700286 else if (msgSensor == "xyz.openbmc_project.Sensor.Threshold.Critical")
287 {
288 auto criticalAlarmLow = msgData.find("CriticalAlarmLow");
289 auto criticalAlarmHigh = msgData.find("CriticalAlarmHigh");
290 if (criticalAlarmHigh == msgData.end() &&
291 criticalAlarmLow == msgData.end())
292 {
293 return 0;
294 }
295
296 bool asserted = false;
297 if (criticalAlarmLow != msgData.end())
298 {
James Feist1f802f52019-02-08 13:51:43 -0800299 asserted = std::get<bool>(criticalAlarmLow->second);
James Feist36b7d8e2018-10-05 15:39:01 -0700300 }
301
302 // checking both as in theory you could de-assert one threshold and
303 // assert the other at the same moment
304 if (!asserted && criticalAlarmHigh != msgData.end())
305 {
James Feist1f802f52019-02-08 13:51:43 -0800306 asserted = std::get<bool>(criticalAlarmHigh->second);
James Feist36b7d8e2018-10-05 15:39:01 -0700307 }
308 owner->setFailed(asserted);
309 }
Jonico Eustaquioaf97d8e2024-01-02 14:35:07 -0600310#ifdef UNC_FAILSAFE
311 else if (msgSensor == "xyz.openbmc_project.Sensor.Threshold.Warning")
312 {
313 auto warningAlarmHigh = msgData.find("WarningAlarmHigh");
314 if (warningAlarmHigh == msgData.end())
315 {
316 return 0;
317 }
318
319 bool asserted = false;
320 if (warningAlarmHigh != msgData.end())
321 {
322 asserted = std::get<bool>(warningAlarmHigh->second);
323 }
324 owner->setFailed(asserted);
325 }
326#endif
Alex.Song8f73ad72021-10-07 00:18:27 +0800327 else if (msgSensor == "xyz.openbmc_project.State.Decorator.Availability")
328 {
329 auto available = msgData.find("Available");
330 if (available == msgData.end())
331 {
332 return 0;
333 }
334 bool asserted = std::get<bool>(available->second);
335 owner->setAvailable(asserted);
336 if (!asserted)
337 {
338 // A thermal controller will continue its PID calculation and not
339 // trigger a 'failsafe' when some inputs are unavailable.
340 // So, forced to clear the value here to prevent a historical
341 // value to participate in a latter PID calculation.
342 owner->updateValue(std::numeric_limits<double>::quiet_NaN(), true);
343 }
344 }
James Feist4b36f262020-07-07 16:56:41 -0700345 else if (msgSensor ==
346 "xyz.openbmc_project.State.Decorator.OperationalStatus")
347 {
348 auto functional = msgData.find("Functional");
349 if (functional == msgData.end())
350 {
351 return 0;
352 }
353 bool asserted = std::get<bool>(functional->second);
354 owner->setFunctional(asserted);
355 }
Patrick Venture863b9242018-03-08 08:29:23 -0800356
357 return 0;
358}
Patrick Ventured0c75662018-06-12 19:03:21 -0700359
Harvey.Wua1ae4fa2022-10-28 17:38:35 +0800360int dbusHandleSignal(sd_bus_message* msg, void* usrData,
361 [[maybe_unused]] sd_bus_error* err)
Patrick Ventured0c75662018-06-12 19:03:21 -0700362{
Patrick Williamsb228bc32022-07-22 19:26:56 -0500363 auto sdbpMsg = sdbusplus::message_t(msg);
Patrick Ventured0c75662018-06-12 19:03:21 -0700364 DbusPassive* obj = static_cast<DbusPassive*>(usrData);
365
Patrick Venture7af157b2018-10-30 11:24:40 -0700366 return handleSensorValue(sdbpMsg, obj);
Patrick Ventured0c75662018-06-12 19:03:21 -0700367}
Patrick Venturea0764872020-08-08 07:48:43 -0700368
369} // namespace pid_control