blob: c57a1a2988ebba15199b26e6ebb72c249c9e0420 [file] [log] [blame]
Willy Tude54f482021-01-26 15:59:09 -08001/*
2// Copyright (c) 2017 2018 Intel 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
17#include "dbus-sdr/sensorcommands.hpp"
18
19#include "dbus-sdr/sdrutils.hpp"
20#include "dbus-sdr/sensorutils.hpp"
21#include "dbus-sdr/storagecommands.hpp"
Harvey Wu05d17c02021-09-15 08:46:59 +080022#include "entity_map_json.hpp"
Willy Tude54f482021-01-26 15:59:09 -080023
24#include <algorithm>
25#include <array>
26#include <boost/algorithm/string.hpp>
27#include <boost/container/flat_map.hpp>
28#include <chrono>
29#include <cmath>
30#include <cstring>
31#include <iostream>
32#include <ipmid/api.hpp>
33#include <ipmid/types.hpp>
34#include <ipmid/utils.hpp>
35#include <map>
36#include <memory>
37#include <optional>
38#include <phosphor-logging/log.hpp>
39#include <sdbusplus/bus.hpp>
40#include <stdexcept>
41#include <string>
Chalapathi Venkataramashetty6f43f4a2021-06-20 19:50:33 +000042#include <user_channel/channel_layer.hpp>
Willy Tude54f482021-01-26 15:59:09 -080043#include <utility>
44#include <variant>
45
Scron Chang2703b022021-07-06 15:47:45 +080046#ifdef FEATURE_HYBRID_SENSORS
47
48#include "sensordatahandler.hpp"
49namespace ipmi
50{
51namespace sensor
52{
53extern const IdInfoMap sensors;
54} // namespace sensor
55} // namespace ipmi
56#endif
adarshgrami042e9db2022-09-15 10:34:34 +053057namespace ipmi
58{
59namespace dcmi
60{
61// Refer Table 6-14, DCMI Entity ID Extension, DCMI v1.5 spec
62static const std::map<uint8_t, uint8_t> validEntityId{
63 {0x40, 0x37}, {0x37, 0x40}, {0x41, 0x03},
64 {0x03, 0x41}, {0x42, 0x07}, {0x07, 0x42}};
65constexpr uint8_t temperatureSensorType = 0x01;
66constexpr uint8_t maxRecords = 8;
67} // namespace dcmi
68} // namespace ipmi
JeffLind950f412021-10-20 18:49:34 +080069constexpr std::array<const char*, 7> suffixes = {
70 "_Output_Voltage", "_Input_Voltage", "_Output_Current", "_Input_Current",
71 "_Output_Power", "_Input_Power", "_Temperature"};
Willy Tude54f482021-01-26 15:59:09 -080072namespace ipmi
73{
Hao Jiangd48c9212021-02-03 15:45:06 -080074
75using phosphor::logging::entry;
76using phosphor::logging::level;
77using phosphor::logging::log;
78
Willy Tude54f482021-01-26 15:59:09 -080079static constexpr int sensorMapUpdatePeriod = 10;
Alex Qiu9ab2f942020-07-15 17:56:21 -070080static constexpr int sensorMapSdrUpdatePeriod = 60;
Willy Tude54f482021-01-26 15:59:09 -080081
Willy Tu38e7a2b2021-03-29 15:09:56 -070082// BMC I2C address is generally at 0x20
83static constexpr uint8_t bmcI2CAddr = 0x20;
84
Willy Tude54f482021-01-26 15:59:09 -080085constexpr size_t maxSDRTotalSize =
86 76; // Largest SDR Record Size (type 01) + SDR Overheader Size
87constexpr static const uint32_t noTimestamp = 0xFFFFFFFF;
88
89static uint16_t sdrReservationID;
90static uint32_t sdrLastAdd = noTimestamp;
91static uint32_t sdrLastRemove = noTimestamp;
92static constexpr size_t lastRecordIndex = 0xFFFF;
Johnathan Mantey6619ae42021-08-06 11:21:10 -070093
94// The IPMI spec defines four Logical Units (LUN), each capable of supporting
95// 255 sensors. The 256 values assigned to LUN 2 are special and are not used
96// for general purpose sensors. Each LUN reserves location 0xFF. The maximum
97// number of IPMI sensors are LUN 0 + LUN 1 + LUN 2, less the reserved
98// location.
99static constexpr size_t maxIPMISensors = ((3 * 256) - (3 * 1));
100
101static constexpr size_t lun0MaxSensorNum = 0xfe;
102static constexpr size_t lun1MaxSensorNum = 0x1fe;
103static constexpr size_t lun3MaxSensorNum = 0x3fe;
Willy Tude54f482021-01-26 15:59:09 -0800104static constexpr int GENERAL_ERROR = -1;
105
Willy Tude54f482021-01-26 15:59:09 -0800106static boost::container::flat_map<std::string, ObjectValueTree> SensorCache;
107
108// Specify the comparison required to sort and find char* map objects
109struct CmpStr
110{
111 bool operator()(const char* a, const char* b) const
112 {
113 return std::strcmp(a, b) < 0;
114 }
115};
116const static boost::container::flat_map<const char*, SensorUnits, CmpStr>
117 sensorUnits{{{"temperature", SensorUnits::degreesC},
118 {"voltage", SensorUnits::volts},
119 {"current", SensorUnits::amps},
120 {"fan_tach", SensorUnits::rpm},
121 {"power", SensorUnits::watts}}};
122
123void registerSensorFunctions() __attribute__((constructor));
124
Patrick Williams5d82f472022-07-22 19:26:53 -0500125static sdbusplus::bus::match_t sensorAdded(
Willy Tude54f482021-01-26 15:59:09 -0800126 *getSdBus(),
127 "type='signal',member='InterfacesAdded',arg0path='/xyz/openbmc_project/"
128 "sensors/'",
Patrick Williams5d82f472022-07-22 19:26:53 -0500129 [](sdbusplus::message_t&) {
Willy Tude54f482021-01-26 15:59:09 -0800130 getSensorTree().clear();
Willy Tu4eca2512022-06-20 21:14:51 -0700131 getIpmiDecoratorPaths(/*ctx=*/std::nullopt).reset();
Willy Tude54f482021-01-26 15:59:09 -0800132 sdrLastAdd = std::chrono::duration_cast<std::chrono::seconds>(
133 std::chrono::system_clock::now().time_since_epoch())
134 .count();
135 });
136
Patrick Williams5d82f472022-07-22 19:26:53 -0500137static sdbusplus::bus::match_t sensorRemoved(
Willy Tude54f482021-01-26 15:59:09 -0800138 *getSdBus(),
139 "type='signal',member='InterfacesRemoved',arg0path='/xyz/openbmc_project/"
140 "sensors/'",
Patrick Williams5d82f472022-07-22 19:26:53 -0500141 [](sdbusplus::message_t&) {
Willy Tude54f482021-01-26 15:59:09 -0800142 getSensorTree().clear();
Willy Tu4eca2512022-06-20 21:14:51 -0700143 getIpmiDecoratorPaths(/*ctx=*/std::nullopt).reset();
Willy Tude54f482021-01-26 15:59:09 -0800144 sdrLastRemove = std::chrono::duration_cast<std::chrono::seconds>(
145 std::chrono::system_clock::now().time_since_epoch())
146 .count();
147 });
148
149// this keeps track of deassertions for sensor event status command. A
150// deasertion can only happen if an assertion was seen first.
151static boost::container::flat_map<
152 std::string, boost::container::flat_map<std::string, std::optional<bool>>>
153 thresholdDeassertMap;
154
Patrick Williams5d82f472022-07-22 19:26:53 -0500155static sdbusplus::bus::match_t thresholdChanged(
Willy Tude54f482021-01-26 15:59:09 -0800156 *getSdBus(),
157 "type='signal',member='PropertiesChanged',interface='org.freedesktop.DBus."
158 "Properties',arg0namespace='xyz.openbmc_project.Sensor.Threshold'",
Patrick Williams5d82f472022-07-22 19:26:53 -0500159 [](sdbusplus::message_t& m) {
Willy Tude54f482021-01-26 15:59:09 -0800160 boost::container::flat_map<std::string, std::variant<bool, double>>
161 values;
162 m.read(std::string(), values);
163
164 auto findAssert =
165 std::find_if(values.begin(), values.end(), [](const auto& pair) {
166 return pair.first.find("Alarm") != std::string::npos;
167 });
168 if (findAssert != values.end())
169 {
170 auto ptr = std::get_if<bool>(&(findAssert->second));
171 if (ptr == nullptr)
172 {
173 phosphor::logging::log<phosphor::logging::level::ERR>(
174 "thresholdChanged: Assert non bool");
175 return;
176 }
177 if (*ptr)
178 {
179 phosphor::logging::log<phosphor::logging::level::INFO>(
180 "thresholdChanged: Assert",
181 phosphor::logging::entry("SENSOR=%s", m.get_path()));
182 thresholdDeassertMap[m.get_path()][findAssert->first] = *ptr;
183 }
184 else
185 {
186 auto& value =
187 thresholdDeassertMap[m.get_path()][findAssert->first];
188 if (value)
189 {
190 phosphor::logging::log<phosphor::logging::level::INFO>(
191 "thresholdChanged: deassert",
192 phosphor::logging::entry("SENSOR=%s", m.get_path()));
193 value = *ptr;
194 }
195 }
196 }
197 });
198
Hao Jiangd2afd052020-12-10 15:09:32 -0800199namespace sensor
200{
201static constexpr const char* vrInterface =
202 "xyz.openbmc_project.Control.VoltageRegulatorMode";
203static constexpr const char* sensorInterface =
204 "xyz.openbmc_project.Sensor.Value";
205} // namespace sensor
206
Willy Tude54f482021-01-26 15:59:09 -0800207static void getSensorMaxMin(const DbusInterfaceMap& sensorMap, double& max,
208 double& min)
209{
210 max = 127;
211 min = -128;
212
Hao Jiangd2afd052020-12-10 15:09:32 -0800213 auto sensorObject = sensorMap.find(sensor::sensorInterface);
Willy Tude54f482021-01-26 15:59:09 -0800214 auto critical =
215 sensorMap.find("xyz.openbmc_project.Sensor.Threshold.Critical");
216 auto warning =
217 sensorMap.find("xyz.openbmc_project.Sensor.Threshold.Warning");
218
219 if (sensorObject != sensorMap.end())
220 {
221 auto maxMap = sensorObject->second.find("MaxValue");
222 auto minMap = sensorObject->second.find("MinValue");
223
224 if (maxMap != sensorObject->second.end())
225 {
226 max = std::visit(VariantToDoubleVisitor(), maxMap->second);
227 }
228 if (minMap != sensorObject->second.end())
229 {
230 min = std::visit(VariantToDoubleVisitor(), minMap->second);
231 }
232 }
233 if (critical != sensorMap.end())
234 {
235 auto lower = critical->second.find("CriticalLow");
236 auto upper = critical->second.find("CriticalHigh");
237 if (lower != critical->second.end())
238 {
239 double value = std::visit(VariantToDoubleVisitor(), lower->second);
Konstantin Aladyshev8265af22021-12-16 18:18:10 +0300240 if (std::isfinite(value))
241 {
242 min = std::min(value, min);
243 }
Willy Tude54f482021-01-26 15:59:09 -0800244 }
245 if (upper != critical->second.end())
246 {
247 double value = std::visit(VariantToDoubleVisitor(), upper->second);
Konstantin Aladyshev8265af22021-12-16 18:18:10 +0300248 if (std::isfinite(value))
249 {
250 max = std::max(value, max);
251 }
Willy Tude54f482021-01-26 15:59:09 -0800252 }
253 }
254 if (warning != sensorMap.end())
255 {
256
257 auto lower = warning->second.find("WarningLow");
258 auto upper = warning->second.find("WarningHigh");
259 if (lower != warning->second.end())
260 {
261 double value = std::visit(VariantToDoubleVisitor(), lower->second);
Konstantin Aladyshev8265af22021-12-16 18:18:10 +0300262 if (std::isfinite(value))
263 {
264 min = std::min(value, min);
265 }
Willy Tude54f482021-01-26 15:59:09 -0800266 }
267 if (upper != warning->second.end())
268 {
269 double value = std::visit(VariantToDoubleVisitor(), upper->second);
Konstantin Aladyshev8265af22021-12-16 18:18:10 +0300270 if (std::isfinite(value))
271 {
272 max = std::max(value, max);
273 }
Willy Tude54f482021-01-26 15:59:09 -0800274 }
275 }
276}
277
278static bool getSensorMap(ipmi::Context::ptr ctx, std::string sensorConnection,
Alex Qiu9ab2f942020-07-15 17:56:21 -0700279 std::string sensorPath, DbusInterfaceMap& sensorMap,
280 int updatePeriod = sensorMapUpdatePeriod)
Willy Tude54f482021-01-26 15:59:09 -0800281{
Scron Chang2703b022021-07-06 15:47:45 +0800282#ifdef FEATURE_HYBRID_SENSORS
283 if (auto sensor = findStaticSensor(sensorPath);
284 sensor != ipmi::sensor::sensors.end() &&
285 getSensorEventTypeFromPath(sensorPath) !=
286 static_cast<uint8_t>(SensorEventTypeCodes::threshold))
287 {
288 // If the incoming sensor is a discrete sensor, it might fail in
289 // getManagedObjects(), return true, and use its own getFunc to get
290 // value.
291 return true;
292 }
293#endif
294
Willy Tude54f482021-01-26 15:59:09 -0800295 static boost::container::flat_map<
296 std::string, std::chrono::time_point<std::chrono::steady_clock>>
297 updateTimeMap;
298
299 auto updateFind = updateTimeMap.find(sensorConnection);
300 auto lastUpdate = std::chrono::time_point<std::chrono::steady_clock>();
301 if (updateFind != updateTimeMap.end())
302 {
303 lastUpdate = updateFind->second;
304 }
305
306 auto now = std::chrono::steady_clock::now();
307
308 if (std::chrono::duration_cast<std::chrono::seconds>(now - lastUpdate)
Alex Qiu9ab2f942020-07-15 17:56:21 -0700309 .count() > updatePeriod)
Willy Tude54f482021-01-26 15:59:09 -0800310 {
Sui Chenc2cb1bc2023-01-10 02:52:06 -0800311 bool found = false;
Willy Tude54f482021-01-26 15:59:09 -0800312
Sui Chenc2cb1bc2023-01-10 02:52:06 -0800313 // Object managers for different kinds of OpenBMC DBus interfaces.
314 // Documented in the phosphor-dbus-interfaces repository.
315 const char* paths[] = {
316 "/xyz/openbmc_project/sensors",
317 "/xyz/openbmc_project/vr",
318 };
319 constexpr size_t num_paths = sizeof(paths) / sizeof(paths[0]);
320 ObjectValueTree allManagedObjects;
321
322 for (size_t i = 0; i < num_paths; i++)
323 {
324 ObjectValueTree managedObjects;
325 boost::system::error_code ec = getManagedObjects(
326 ctx, sensorConnection.c_str(), paths[i], managedObjects);
327 if (ec)
328 {
329 phosphor::logging::log<phosphor::logging::level::ERR>(
330 "GetMangagedObjects for getSensorMap failed",
331 phosphor::logging::entry("ERROR=%s", ec.message().c_str()));
332
333 continue;
334 }
335 allManagedObjects.merge(managedObjects);
336 found = true;
337 }
338
339 if (!found)
340 {
Willy Tude54f482021-01-26 15:59:09 -0800341 return false;
342 }
343
Sui Chenc2cb1bc2023-01-10 02:52:06 -0800344 SensorCache[sensorConnection] = allManagedObjects;
Alex Qiu9ab2f942020-07-15 17:56:21 -0700345 // Update time after finish building the map which allow the
346 // data to be cached for updatePeriod plus the build time.
347 updateTimeMap[sensorConnection] = std::chrono::steady_clock::now();
Willy Tude54f482021-01-26 15:59:09 -0800348 }
349 auto connection = SensorCache.find(sensorConnection);
350 if (connection == SensorCache.end())
351 {
352 return false;
353 }
354 auto path = connection->second.find(sensorPath);
355 if (path == connection->second.end())
356 {
357 return false;
358 }
359 sensorMap = path->second;
360
361 return true;
362}
363
Hao Jiangd2afd052020-12-10 15:09:32 -0800364namespace sensor
365{
Hao Jiangd48c9212021-02-03 15:45:06 -0800366// Read VR profiles from sensor(daemon) interface
367static std::optional<std::vector<std::string>>
368 getSupportedVrProfiles(const ipmi::DbusInterfaceMap::mapped_type& object)
Hao Jiangd2afd052020-12-10 15:09:32 -0800369{
370 // get VR mode profiles from Supported Interface
Hao Jiangd48c9212021-02-03 15:45:06 -0800371 auto supportedProperty = object.find("Supported");
372 if (supportedProperty == object.end() ||
373 object.find("Selected") == object.end())
Hao Jiangd2afd052020-12-10 15:09:32 -0800374 {
375 phosphor::logging::log<phosphor::logging::level::ERR>(
376 "Missing the required Supported and Selected properties");
377 return std::nullopt;
378 }
379
380 const auto profilesPtr =
381 std::get_if<std::vector<std::string>>(&supportedProperty->second);
382
383 if (profilesPtr == nullptr)
384 {
385 phosphor::logging::log<phosphor::logging::level::ERR>(
386 "property is not array of string");
387 return std::nullopt;
388 }
Hao Jiangd48c9212021-02-03 15:45:06 -0800389 return *profilesPtr;
390}
391
392// Calculate VR Mode from input IPMI discrete event bytes
393static std::optional<std::string>
394 calculateVRMode(uint15_t assertOffset,
395 const ipmi::DbusInterfaceMap::mapped_type& VRObject)
396{
397 // get VR mode profiles from Supported Interface
398 auto profiles = getSupportedVrProfiles(VRObject);
399 if (!profiles)
400 {
401 return std::nullopt;
402 }
Hao Jiangd2afd052020-12-10 15:09:32 -0800403
404 // interpret IPMI cmd bits into profiles' index
405 long unsigned int index = 0;
406 // only one bit should be set and the highest bit should not be used.
407 if (assertOffset == 0 || assertOffset == (1u << 15) ||
408 (assertOffset & (assertOffset - 1)))
409 {
410 phosphor::logging::log<phosphor::logging::level::ERR>(
411 "IPMI cmd format incorrect",
412
413 phosphor::logging::entry("BYTES=%#02x",
414 static_cast<uint16_t>(assertOffset)));
415 return std::nullopt;
416 }
417
418 while (assertOffset != 1)
419 {
420 assertOffset >>= 1;
421 index++;
422 }
423
Hao Jiangd48c9212021-02-03 15:45:06 -0800424 if (index >= profiles->size())
Hao Jiangd2afd052020-12-10 15:09:32 -0800425 {
426 phosphor::logging::log<phosphor::logging::level::ERR>(
427 "profile index out of boundary");
428 return std::nullopt;
429 }
430
Hao Jiangd48c9212021-02-03 15:45:06 -0800431 return profiles->at(index);
Hao Jiangd2afd052020-12-10 15:09:32 -0800432}
433
434// Calculate sensor value from IPMI reading byte
435static std::optional<double>
436 calculateValue(uint8_t reading, const ipmi::DbusInterfaceMap& sensorMap,
437 const ipmi::DbusInterfaceMap::mapped_type& valueObject)
438{
439 if (valueObject.find("Value") == valueObject.end())
440 {
441 phosphor::logging::log<phosphor::logging::level::ERR>(
442 "Missing the required Value property");
443 return std::nullopt;
444 }
445
446 double max = 0;
447 double min = 0;
448 getSensorMaxMin(sensorMap, max, min);
449
450 int16_t mValue = 0;
451 int16_t bValue = 0;
452 int8_t rExp = 0;
453 int8_t bExp = 0;
454 bool bSigned = false;
455
456 if (!getSensorAttributes(max, min, mValue, rExp, bValue, bExp, bSigned))
457 {
458 return std::nullopt;
459 }
460
461 double value = bSigned ? ((int8_t)reading) : reading;
462
463 value *= ((double)mValue);
464 value += ((double)bValue) * std::pow(10.0, bExp);
465 value *= std::pow(10.0, rExp);
466
467 return value;
468}
469
Willy Tu38e7a2b2021-03-29 15:09:56 -0700470// Extract file name from sensor path as the sensors SDR ID. Simplify the name
471// if it is too long.
472std::string parseSdrIdFromPath(const std::string& path)
473{
474 std::string name;
475 size_t nameStart = path.rfind("/");
476 if (nameStart != std::string::npos)
477 {
478 name = path.substr(nameStart + 1, std::string::npos - nameStart);
479 }
480
Willy Tu38e7a2b2021-03-29 15:09:56 -0700481 if (name.size() > FULL_RECORD_ID_STR_MAX_LENGTH)
482 {
483 // try to not truncate by replacing common words
JeffLind950f412021-10-20 18:49:34 +0800484 for (const auto& suffix : suffixes)
Willy Tu38e7a2b2021-03-29 15:09:56 -0700485 {
JeffLind950f412021-10-20 18:49:34 +0800486 if (boost::ends_with(name, suffix))
487 {
488 boost::replace_all(name, suffix, "");
489 break;
490 }
Willy Tu38e7a2b2021-03-29 15:09:56 -0700491 }
Duke Du97014f52021-12-16 17:21:01 +0800492 if (name.size() > FULL_RECORD_ID_STR_MAX_LENGTH)
493 {
494 name.resize(FULL_RECORD_ID_STR_MAX_LENGTH);
495 }
Willy Tu38e7a2b2021-03-29 15:09:56 -0700496 }
497 return name;
498}
499
Hao Jiangd48c9212021-02-03 15:45:06 -0800500bool getVrEventStatus(ipmi::Context::ptr ctx, const std::string& connection,
501 const std::string& path,
502 const ipmi::DbusInterfaceMap::mapped_type& object,
503 std::bitset<16>& assertions)
504{
505 auto profiles = sensor::getSupportedVrProfiles(object);
506 if (!profiles)
507 {
508 return false;
509 }
Willy Tu8366f0b2022-04-29 05:00:17 -0700510 std::string mode;
Hao Jiangd48c9212021-02-03 15:45:06 -0800511
512 auto ec = getDbusProperty(ctx, connection, path, sensor::vrInterface,
Willy Tu8366f0b2022-04-29 05:00:17 -0700513 "Selected", mode);
Hao Jiangd48c9212021-02-03 15:45:06 -0800514 if (ec)
515 {
516 log<level::ERR>("Failed to get property",
517 entry("PROPERTY=%s", "Selected"),
518 entry("PATH=%s", path.c_str()),
519 entry("INTERFACE=%s", sensor::sensorInterface),
520 entry("WHAT=%s", ec.message().c_str()));
521 return false;
522 }
523
Willy Tu8366f0b2022-04-29 05:00:17 -0700524 auto itr = std::find(profiles->begin(), profiles->end(), mode);
Hao Jiangd48c9212021-02-03 15:45:06 -0800525 if (itr == profiles->end())
526 {
527 using namespace phosphor::logging;
528 log<level::ERR>("VR mode doesn't match any of its profiles",
529 entry("PATH=%s", path.c_str()));
530 return false;
531 }
532 std::size_t index =
533 static_cast<std::size_t>(std::distance(profiles->begin(), itr));
534
Willy Tubef102a2022-06-09 15:36:09 -0700535 // map index to response event assertion bit.
536 if (index < 16)
Hao Jiangd48c9212021-02-03 15:45:06 -0800537 {
Willy Tubef102a2022-06-09 15:36:09 -0700538 assertions.set(index);
Hao Jiangd48c9212021-02-03 15:45:06 -0800539 }
540 else
541 {
542 log<level::ERR>("VR profile index reaches max assertion bit",
543 entry("PATH=%s", path.c_str()),
544 entry("INDEX=%uz", index));
545 return false;
546 }
547 if constexpr (debug)
548 {
549 std::cerr << "VR sensor " << sensor::parseSdrIdFromPath(path)
Willy Tu8366f0b2022-04-29 05:00:17 -0700550 << " mode is: [" << index << "] " << mode << std::endl;
Hao Jiangd48c9212021-02-03 15:45:06 -0800551 }
552 return true;
553}
Hao Jiangd2afd052020-12-10 15:09:32 -0800554} // namespace sensor
555
Chalapathi Venkataramashetty6f43f4a2021-06-20 19:50:33 +0000556ipmi::RspType<> ipmiSenPlatformEvent(ipmi::Context::ptr ctx,
557 ipmi::message::Payload& p)
Willy Tude54f482021-01-26 15:59:09 -0800558{
Chalapathi Venkataramashetty6f43f4a2021-06-20 19:50:33 +0000559 constexpr const uint8_t validEnvmRev = 0x04;
560 constexpr const uint8_t lastSensorType = 0x2C;
561 constexpr const uint8_t oemReserved = 0xC0;
562
Sujoy Ray3ab2c2b2021-11-01 13:17:27 -0700563 uint8_t sysgeneratorID = 0;
Chalapathi Venkataramashetty6f43f4a2021-06-20 19:50:33 +0000564 uint8_t evmRev = 0;
565 uint8_t sensorType = 0;
566 uint8_t sensorNum = 0;
567 uint8_t eventType = 0;
568 uint8_t eventData1 = 0;
569 std::optional<uint8_t> eventData2 = 0;
570 std::optional<uint8_t> eventData3 = 0;
Sujoy Ray3ab2c2b2021-11-01 13:17:27 -0700571 [[maybe_unused]] uint16_t generatorID = 0;
Chalapathi Venkataramashetty6f43f4a2021-06-20 19:50:33 +0000572 ipmi::ChannelInfo chInfo;
573
574 if (ipmi::getChannelInfo(ctx->channel, chInfo) != ipmi::ccSuccess)
575 {
576 phosphor::logging::log<phosphor::logging::level::ERR>(
577 "Failed to get Channel Info",
578 phosphor::logging::entry("CHANNEL=%d", ctx->channel));
579 return ipmi::responseUnspecifiedError();
580 }
581
582 if (static_cast<ipmi::EChannelMediumType>(chInfo.mediumType) ==
583 ipmi::EChannelMediumType::systemInterface)
584 {
585
Sujoy Ray3ab2c2b2021-11-01 13:17:27 -0700586 p.unpack(sysgeneratorID, evmRev, sensorType, sensorNum, eventType,
Chalapathi Venkataramashetty6f43f4a2021-06-20 19:50:33 +0000587 eventData1, eventData2, eventData3);
Sujoy Ray3ab2c2b2021-11-01 13:17:27 -0700588 // Refer to IPMI Spec Table 32: SEL Event Records
589 generatorID = (ctx->channel << 12) // Channel
590 | (0x0 << 10) // Reserved
591 | (0x0 << 8) // 0x0 for sys-soft ID
592 | ((sysgeneratorID << 1) | 0x1);
Chalapathi Venkataramashetty6f43f4a2021-06-20 19:50:33 +0000593 }
594 else
595 {
596
597 p.unpack(evmRev, sensorType, sensorNum, eventType, eventData1,
598 eventData2, eventData3);
Sujoy Ray3ab2c2b2021-11-01 13:17:27 -0700599 // Refer to IPMI Spec Table 32: SEL Event Records
600 generatorID = (ctx->channel << 12) // Channel
601 | (0x0 << 10) // Reserved
602 | ((ctx->lun & 0x3) << 8) // Lun
603 | (ctx->rqSA << 1);
Chalapathi Venkataramashetty6f43f4a2021-06-20 19:50:33 +0000604 }
605
606 if (!p.fullyUnpacked())
607 {
608 return ipmi::responseReqDataLenInvalid();
609 }
610
611 // Check for valid evmRev and Sensor Type(per Table 42 of spec)
612 if (evmRev != validEnvmRev)
613 {
614 return ipmi::responseInvalidFieldRequest();
615 }
616 if ((sensorType > lastSensorType) && (sensorType < oemReserved))
617 {
618 return ipmi::responseInvalidFieldRequest();
619 }
620
Willy Tude54f482021-01-26 15:59:09 -0800621 return ipmi::responseSuccess();
622}
623
Willy Tudbafbce2021-03-29 00:37:05 -0700624ipmi::RspType<> ipmiSetSensorReading(ipmi::Context::ptr ctx,
Willy Tu11d68892022-01-20 10:37:34 -0800625 uint8_t sensorNumber, uint8_t,
Willy Tudbafbce2021-03-29 00:37:05 -0700626 uint8_t reading, uint15_t assertOffset,
Willy Tu11d68892022-01-20 10:37:34 -0800627 bool, uint15_t, bool, uint8_t, uint8_t,
628 uint8_t)
Willy Tudbafbce2021-03-29 00:37:05 -0700629{
630 std::string connection;
631 std::string path;
Hao Jiange39d4d82021-04-16 17:02:40 -0700632 std::vector<std::string> interfaces;
633
634 ipmi::Cc status =
635 getSensorConnection(ctx, sensorNumber, connection, path, &interfaces);
Willy Tudbafbce2021-03-29 00:37:05 -0700636 if (status)
637 {
638 return ipmi::response(status);
639 }
640
Hao Jiangd2afd052020-12-10 15:09:32 -0800641 // we can tell the sensor type by its interface type
Hao Jiange39d4d82021-04-16 17:02:40 -0700642 if (std::find(interfaces.begin(), interfaces.end(),
643 sensor::sensorInterface) != interfaces.end())
Willy Tudbafbce2021-03-29 00:37:05 -0700644 {
Hao Jiange39d4d82021-04-16 17:02:40 -0700645 DbusInterfaceMap sensorMap;
646 if (!getSensorMap(ctx, connection, path, sensorMap))
647 {
648 return ipmi::responseResponseError();
649 }
650 auto sensorObject = sensorMap.find(sensor::sensorInterface);
Harvey Wuf61c0862021-09-15 08:48:40 +0800651 if (sensorObject == sensorMap.end())
Hao Jiange39d4d82021-04-16 17:02:40 -0700652 {
653 return ipmi::responseResponseError();
654 }
655
Jie Yangf0a89942021-07-29 15:30:25 -0700656 // Only allow external SetSensor if write permission granted
Harvey.Wu0e7a8af2022-06-10 16:46:46 +0800657 if (!details::sdrWriteTable.getWritePermission((ctx->lun << 8) |
658 sensorNumber))
Jie Yangf0a89942021-07-29 15:30:25 -0700659 {
660 return ipmi::responseResponseError();
661 }
662
Hao Jiangd2afd052020-12-10 15:09:32 -0800663 auto value =
664 sensor::calculateValue(reading, sensorMap, sensorObject->second);
665 if (!value)
666 {
667 return ipmi::responseResponseError();
668 }
669
670 if constexpr (debug)
671 {
672 phosphor::logging::log<phosphor::logging::level::INFO>(
673 "IPMI SET_SENSOR",
674 phosphor::logging::entry("SENSOR_NUM=%d", sensorNumber),
675 phosphor::logging::entry("BYTE=%u", (unsigned int)reading),
676 phosphor::logging::entry("VALUE=%f", *value));
677 }
678
679 boost::system::error_code ec =
680 setDbusProperty(ctx, connection, path, sensor::sensorInterface,
681 "Value", ipmi::Value(*value));
682
683 // setDbusProperty intended to resolve dbus exception/rc within the
Patrick Williamsef1259b2021-09-02 09:12:33 -0500684 // function but failed to achieve that. Catch exception in the ipmi
Hao Jiangd2afd052020-12-10 15:09:32 -0800685 // callback functions for now (e.g. ipmiSetSensorReading).
686 if (ec)
687 {
688 using namespace phosphor::logging;
689 log<level::ERR>("Failed to set property",
690 entry("PROPERTY=%s", "Value"),
691 entry("PATH=%s", path.c_str()),
692 entry("INTERFACE=%s", sensor::sensorInterface),
693 entry("WHAT=%s", ec.message().c_str()));
694 return ipmi::responseResponseError();
695 }
696 return ipmi::responseSuccess();
Willy Tudbafbce2021-03-29 00:37:05 -0700697 }
698
Hao Jiange39d4d82021-04-16 17:02:40 -0700699 if (std::find(interfaces.begin(), interfaces.end(), sensor::vrInterface) !=
700 interfaces.end())
Willy Tudbafbce2021-03-29 00:37:05 -0700701 {
Hao Jiange39d4d82021-04-16 17:02:40 -0700702 DbusInterfaceMap sensorMap;
703 if (!getSensorMap(ctx, connection, path, sensorMap))
704 {
705 return ipmi::responseResponseError();
706 }
707 auto sensorObject = sensorMap.find(sensor::vrInterface);
Harvey Wuf61c0862021-09-15 08:48:40 +0800708 if (sensorObject == sensorMap.end())
Hao Jiange39d4d82021-04-16 17:02:40 -0700709 {
710 return ipmi::responseResponseError();
711 }
712
Hao Jiangd2afd052020-12-10 15:09:32 -0800713 // VR sensors are treated as a special case and we will not check the
714 // write permission for VR sensors, since they always deemed writable
715 // and permission table are not applied to VR sensors.
716 auto vrMode =
717 sensor::calculateVRMode(assertOffset, sensorObject->second);
718 if (!vrMode)
719 {
720 return ipmi::responseResponseError();
721 }
722 boost::system::error_code ec = setDbusProperty(
723 ctx, connection, path, sensor::vrInterface, "Selected", *vrMode);
724 // setDbusProperty intended to resolve dbus exception/rc within the
Patrick Williamsef1259b2021-09-02 09:12:33 -0500725 // function but failed to achieve that. Catch exception in the ipmi
Hao Jiangd2afd052020-12-10 15:09:32 -0800726 // callback functions for now (e.g. ipmiSetSensorReading).
727 if (ec)
728 {
729 using namespace phosphor::logging;
730 log<level::ERR>("Failed to set property",
731 entry("PROPERTY=%s", "Selected"),
732 entry("PATH=%s", path.c_str()),
733 entry("INTERFACE=%s", sensor::sensorInterface),
734 entry("WHAT=%s", ec.message().c_str()));
735 return ipmi::responseResponseError();
736 }
737 return ipmi::responseSuccess();
Willy Tudbafbce2021-03-29 00:37:05 -0700738 }
739
Hao Jiangd2afd052020-12-10 15:09:32 -0800740 phosphor::logging::log<phosphor::logging::level::ERR>(
741 "unknown sensor type",
742 phosphor::logging::entry("PATH=%s", path.c_str()));
743 return ipmi::responseResponseError();
Willy Tudbafbce2021-03-29 00:37:05 -0700744}
745
Willy Tude54f482021-01-26 15:59:09 -0800746ipmi::RspType<uint8_t, uint8_t, uint8_t, std::optional<uint8_t>>
747 ipmiSenGetSensorReading(ipmi::Context::ptr ctx, uint8_t sensnum)
748{
749 std::string connection;
750 std::string path;
751
Chalapathi Venkataramashetty8c2f3c42021-06-13 19:51:17 +0000752 if (sensnum == reservedSensorNumber)
753 {
754 return ipmi::responseInvalidFieldRequest();
755 }
756
Willy Tude54f482021-01-26 15:59:09 -0800757 auto status = getSensorConnection(ctx, sensnum, connection, path);
758 if (status)
759 {
760 return ipmi::response(status);
761 }
762
Scron Chang2703b022021-07-06 15:47:45 +0800763#ifdef FEATURE_HYBRID_SENSORS
764 if (auto sensor = findStaticSensor(path);
765 sensor != ipmi::sensor::sensors.end() &&
766 getSensorEventTypeFromPath(path) !=
767 static_cast<uint8_t>(SensorEventTypeCodes::threshold))
768 {
769 if (ipmi::sensor::Mutability::Read !=
770 (sensor->second.mutability & ipmi::sensor::Mutability::Read))
771 {
772 return ipmi::responseIllegalCommand();
773 }
774
775 uint8_t operation;
776 try
777 {
778 ipmi::sensor::GetSensorResponse getResponse =
779 sensor->second.getFunc(sensor->second);
780
781 if (getResponse.readingOrStateUnavailable)
782 {
783 operation |= static_cast<uint8_t>(
784 IPMISensorReadingByte2::readingStateUnavailable);
785 }
786 if (getResponse.scanningEnabled)
787 {
788 operation |= static_cast<uint8_t>(
789 IPMISensorReadingByte2::sensorScanningEnable);
790 }
791 if (getResponse.allEventMessagesEnabled)
792 {
793 operation |= static_cast<uint8_t>(
794 IPMISensorReadingByte2::eventMessagesEnable);
795 }
796 return ipmi::responseSuccess(
797 getResponse.reading, operation,
798 getResponse.thresholdLevelsStates,
799 getResponse.discreteReadingSensorStates);
800 }
801 catch (const std::exception& e)
802 {
803 operation |= static_cast<uint8_t>(
804 IPMISensorReadingByte2::readingStateUnavailable);
805 return ipmi::responseSuccess(0, operation, 0, std::nullopt);
806 }
807 }
808#endif
809
Willy Tude54f482021-01-26 15:59:09 -0800810 DbusInterfaceMap sensorMap;
811 if (!getSensorMap(ctx, connection, path, sensorMap))
812 {
813 return ipmi::responseResponseError();
814 }
Hao Jiangd2afd052020-12-10 15:09:32 -0800815 auto sensorObject = sensorMap.find(sensor::sensorInterface);
Willy Tude54f482021-01-26 15:59:09 -0800816
817 if (sensorObject == sensorMap.end() ||
818 sensorObject->second.find("Value") == sensorObject->second.end())
819 {
820 return ipmi::responseResponseError();
821 }
822 auto& valueVariant = sensorObject->second["Value"];
823 double reading = std::visit(VariantToDoubleVisitor(), valueVariant);
824
825 double max = 0;
826 double min = 0;
827 getSensorMaxMin(sensorMap, max, min);
828
829 int16_t mValue = 0;
830 int16_t bValue = 0;
831 int8_t rExp = 0;
832 int8_t bExp = 0;
833 bool bSigned = false;
834
835 if (!getSensorAttributes(max, min, mValue, rExp, bValue, bExp, bSigned))
836 {
837 return ipmi::responseResponseError();
838 }
839
840 uint8_t value =
841 scaleIPMIValueFromDouble(reading, mValue, rExp, bValue, bExp, bSigned);
842 uint8_t operation =
843 static_cast<uint8_t>(IPMISensorReadingByte2::sensorScanningEnable);
844 operation |=
845 static_cast<uint8_t>(IPMISensorReadingByte2::eventMessagesEnable);
846 bool notReading = std::isnan(reading);
847
848 if (!notReading)
849 {
850 auto availableObject =
851 sensorMap.find("xyz.openbmc_project.State.Decorator.Availability");
852 if (availableObject != sensorMap.end())
853 {
854 auto findAvailable = availableObject->second.find("Available");
855 if (findAvailable != availableObject->second.end())
856 {
857 bool* available = std::get_if<bool>(&(findAvailable->second));
858 if (available && !(*available))
859 {
860 notReading = true;
861 }
862 }
863 }
864 }
865
866 if (notReading)
867 {
868 operation |= static_cast<uint8_t>(
869 IPMISensorReadingByte2::readingStateUnavailable);
870 }
871
Josh Lehana55c9532020-10-28 21:59:06 -0700872 if constexpr (details::enableInstrumentation)
873 {
874 int byteValue;
875 if (bSigned)
876 {
877 byteValue = static_cast<int>(static_cast<int8_t>(value));
878 }
879 else
880 {
881 byteValue = static_cast<int>(static_cast<uint8_t>(value));
882 }
883
884 // Keep stats on the reading just obtained, even if it is "NaN"
Harvey.Wu0e7a8af2022-06-10 16:46:46 +0800885 if (details::sdrStatsTable.updateReading((ctx->lun << 8) | sensnum,
886 reading, byteValue))
Josh Lehana55c9532020-10-28 21:59:06 -0700887 {
888 // This is the first reading, show the coefficients
889 double step = (max - min) / 255.0;
890 std::cerr << "IPMI sensor "
Harvey.Wu0e7a8af2022-06-10 16:46:46 +0800891 << details::sdrStatsTable.getName((ctx->lun << 8) |
892 sensnum)
Josh Lehana55c9532020-10-28 21:59:06 -0700893 << ": Range min=" << min << " max=" << max
894 << ", step=" << step
895 << ", Coefficients mValue=" << static_cast<int>(mValue)
896 << " rExp=" << static_cast<int>(rExp)
897 << " bValue=" << static_cast<int>(bValue)
898 << " bExp=" << static_cast<int>(bExp)
899 << " bSigned=" << static_cast<int>(bSigned) << "\n";
900 }
901 }
902
Willy Tude54f482021-01-26 15:59:09 -0800903 uint8_t thresholds = 0;
904
905 auto warningObject =
906 sensorMap.find("xyz.openbmc_project.Sensor.Threshold.Warning");
907 if (warningObject != sensorMap.end())
908 {
909 auto alarmHigh = warningObject->second.find("WarningAlarmHigh");
910 auto alarmLow = warningObject->second.find("WarningAlarmLow");
911 if (alarmHigh != warningObject->second.end())
912 {
913 if (std::get<bool>(alarmHigh->second))
914 {
915 thresholds |= static_cast<uint8_t>(
916 IPMISensorReadingByte3::upperNonCritical);
917 }
918 }
919 if (alarmLow != warningObject->second.end())
920 {
921 if (std::get<bool>(alarmLow->second))
922 {
923 thresholds |= static_cast<uint8_t>(
924 IPMISensorReadingByte3::lowerNonCritical);
925 }
926 }
927 }
928
929 auto criticalObject =
930 sensorMap.find("xyz.openbmc_project.Sensor.Threshold.Critical");
931 if (criticalObject != sensorMap.end())
932 {
933 auto alarmHigh = criticalObject->second.find("CriticalAlarmHigh");
934 auto alarmLow = criticalObject->second.find("CriticalAlarmLow");
935 if (alarmHigh != criticalObject->second.end())
936 {
937 if (std::get<bool>(alarmHigh->second))
938 {
939 thresholds |=
940 static_cast<uint8_t>(IPMISensorReadingByte3::upperCritical);
941 }
942 }
943 if (alarmLow != criticalObject->second.end())
944 {
945 if (std::get<bool>(alarmLow->second))
946 {
947 thresholds |=
948 static_cast<uint8_t>(IPMISensorReadingByte3::lowerCritical);
949 }
950 }
951 }
952
953 // no discrete as of today so optional byte is never returned
954 return ipmi::responseSuccess(value, operation, thresholds, std::nullopt);
955}
956
957/** @brief implements the Set Sensor threshold command
958 * @param sensorNumber - sensor number
959 * @param lowerNonCriticalThreshMask
960 * @param lowerCriticalThreshMask
961 * @param lowerNonRecovThreshMask
962 * @param upperNonCriticalThreshMask
963 * @param upperCriticalThreshMask
964 * @param upperNonRecovThreshMask
965 * @param reserved
966 * @param lowerNonCritical - lower non-critical threshold
967 * @param lowerCritical - Lower critical threshold
968 * @param lowerNonRecoverable - Lower non recovarable threshold
969 * @param upperNonCritical - Upper non-critical threshold
970 * @param upperCritical - Upper critical
971 * @param upperNonRecoverable - Upper Non-recoverable
972 *
973 * @returns IPMI completion code
974 */
975ipmi::RspType<> ipmiSenSetSensorThresholds(
976 ipmi::Context::ptr ctx, uint8_t sensorNum, bool lowerNonCriticalThreshMask,
977 bool lowerCriticalThreshMask, bool lowerNonRecovThreshMask,
978 bool upperNonCriticalThreshMask, bool upperCriticalThreshMask,
979 bool upperNonRecovThreshMask, uint2_t reserved, uint8_t lowerNonCritical,
Willy Tu11d68892022-01-20 10:37:34 -0800980 uint8_t lowerCritical, [[maybe_unused]] uint8_t lowerNonRecoverable,
Willy Tude54f482021-01-26 15:59:09 -0800981 uint8_t upperNonCritical, uint8_t upperCritical,
Willy Tu11d68892022-01-20 10:37:34 -0800982 [[maybe_unused]] uint8_t upperNonRecoverable)
Willy Tude54f482021-01-26 15:59:09 -0800983{
Chalapathi Venkataramashetty8c2f3c42021-06-13 19:51:17 +0000984 if (sensorNum == reservedSensorNumber || reserved)
Willy Tude54f482021-01-26 15:59:09 -0800985 {
986 return ipmi::responseInvalidFieldRequest();
987 }
988
989 // lower nc and upper nc not suppported on any sensor
990 if (lowerNonRecovThreshMask || upperNonRecovThreshMask)
991 {
992 return ipmi::responseInvalidFieldRequest();
993 }
994
995 // if none of the threshold mask are set, nothing to do
996 if (!(lowerNonCriticalThreshMask | lowerCriticalThreshMask |
997 lowerNonRecovThreshMask | upperNonCriticalThreshMask |
998 upperCriticalThreshMask | upperNonRecovThreshMask))
999 {
1000 return ipmi::responseSuccess();
1001 }
1002
1003 std::string connection;
1004 std::string path;
1005
1006 ipmi::Cc status = getSensorConnection(ctx, sensorNum, connection, path);
1007 if (status)
1008 {
1009 return ipmi::response(status);
1010 }
1011 DbusInterfaceMap sensorMap;
1012 if (!getSensorMap(ctx, connection, path, sensorMap))
1013 {
1014 return ipmi::responseResponseError();
1015 }
1016
1017 double max = 0;
1018 double min = 0;
1019 getSensorMaxMin(sensorMap, max, min);
1020
1021 int16_t mValue = 0;
1022 int16_t bValue = 0;
1023 int8_t rExp = 0;
1024 int8_t bExp = 0;
1025 bool bSigned = false;
1026
1027 if (!getSensorAttributes(max, min, mValue, rExp, bValue, bExp, bSigned))
1028 {
1029 return ipmi::responseResponseError();
1030 }
1031
1032 // store a vector of property name, value to set, and interface
1033 std::vector<std::tuple<std::string, uint8_t, std::string>> thresholdsToSet;
1034
1035 // define the indexes of the tuple
1036 constexpr uint8_t propertyName = 0;
1037 constexpr uint8_t thresholdValue = 1;
1038 constexpr uint8_t interface = 2;
1039 // verifiy all needed fields are present
1040 if (lowerCriticalThreshMask || upperCriticalThreshMask)
1041 {
1042 auto findThreshold =
1043 sensorMap.find("xyz.openbmc_project.Sensor.Threshold.Critical");
1044 if (findThreshold == sensorMap.end())
1045 {
1046 return ipmi::responseInvalidFieldRequest();
1047 }
1048 if (lowerCriticalThreshMask)
1049 {
1050 auto findLower = findThreshold->second.find("CriticalLow");
1051 if (findLower == findThreshold->second.end())
1052 {
1053 return ipmi::responseInvalidFieldRequest();
1054 }
1055 thresholdsToSet.emplace_back("CriticalLow", lowerCritical,
1056 findThreshold->first);
1057 }
1058 if (upperCriticalThreshMask)
1059 {
1060 auto findUpper = findThreshold->second.find("CriticalHigh");
1061 if (findUpper == findThreshold->second.end())
1062 {
1063 return ipmi::responseInvalidFieldRequest();
1064 }
1065 thresholdsToSet.emplace_back("CriticalHigh", upperCritical,
1066 findThreshold->first);
1067 }
1068 }
1069 if (lowerNonCriticalThreshMask || upperNonCriticalThreshMask)
1070 {
1071 auto findThreshold =
1072 sensorMap.find("xyz.openbmc_project.Sensor.Threshold.Warning");
1073 if (findThreshold == sensorMap.end())
1074 {
1075 return ipmi::responseInvalidFieldRequest();
1076 }
1077 if (lowerNonCriticalThreshMask)
1078 {
1079 auto findLower = findThreshold->second.find("WarningLow");
1080 if (findLower == findThreshold->second.end())
1081 {
1082 return ipmi::responseInvalidFieldRequest();
1083 }
1084 thresholdsToSet.emplace_back("WarningLow", lowerNonCritical,
1085 findThreshold->first);
1086 }
1087 if (upperNonCriticalThreshMask)
1088 {
1089 auto findUpper = findThreshold->second.find("WarningHigh");
1090 if (findUpper == findThreshold->second.end())
1091 {
1092 return ipmi::responseInvalidFieldRequest();
1093 }
1094 thresholdsToSet.emplace_back("WarningHigh", upperNonCritical,
1095 findThreshold->first);
1096 }
1097 }
1098 for (const auto& property : thresholdsToSet)
1099 {
1100 // from section 36.3 in the IPMI Spec, assume all linear
1101 double valueToSet = ((mValue * std::get<thresholdValue>(property)) +
1102 (bValue * std::pow(10.0, bExp))) *
1103 std::pow(10.0, rExp);
1104 setDbusProperty(
1105 *getSdBus(), connection, path, std::get<interface>(property),
1106 std::get<propertyName>(property), ipmi::Value(valueToSet));
1107 }
1108 return ipmi::responseSuccess();
1109}
1110
1111IPMIThresholds getIPMIThresholds(const DbusInterfaceMap& sensorMap)
1112{
1113 IPMIThresholds resp;
1114 auto warningInterface =
1115 sensorMap.find("xyz.openbmc_project.Sensor.Threshold.Warning");
1116 auto criticalInterface =
1117 sensorMap.find("xyz.openbmc_project.Sensor.Threshold.Critical");
1118
1119 if ((warningInterface != sensorMap.end()) ||
1120 (criticalInterface != sensorMap.end()))
1121 {
Hao Jiangd2afd052020-12-10 15:09:32 -08001122 auto sensorPair = sensorMap.find(sensor::sensorInterface);
Willy Tude54f482021-01-26 15:59:09 -08001123
1124 if (sensorPair == sensorMap.end())
1125 {
1126 // should not have been able to find a sensor not implementing
1127 // the sensor object
1128 throw std::runtime_error("Invalid sensor map");
1129 }
1130
1131 double max = 0;
1132 double min = 0;
1133 getSensorMaxMin(sensorMap, max, min);
1134
1135 int16_t mValue = 0;
1136 int16_t bValue = 0;
1137 int8_t rExp = 0;
1138 int8_t bExp = 0;
1139 bool bSigned = false;
1140
1141 if (!getSensorAttributes(max, min, mValue, rExp, bValue, bExp, bSigned))
1142 {
1143 throw std::runtime_error("Invalid sensor atrributes");
1144 }
1145 if (warningInterface != sensorMap.end())
1146 {
1147 auto& warningMap = warningInterface->second;
1148
1149 auto warningHigh = warningMap.find("WarningHigh");
1150 auto warningLow = warningMap.find("WarningLow");
1151
1152 if (warningHigh != warningMap.end())
1153 {
1154
1155 double value =
1156 std::visit(VariantToDoubleVisitor(), warningHigh->second);
Konstantin Aladyshev8265af22021-12-16 18:18:10 +03001157 if (std::isfinite(value))
1158 {
1159 resp.warningHigh = scaleIPMIValueFromDouble(
1160 value, mValue, rExp, bValue, bExp, bSigned);
1161 }
Willy Tude54f482021-01-26 15:59:09 -08001162 }
1163 if (warningLow != warningMap.end())
1164 {
1165 double value =
1166 std::visit(VariantToDoubleVisitor(), warningLow->second);
Konstantin Aladyshev8265af22021-12-16 18:18:10 +03001167 if (std::isfinite(value))
1168 {
1169 resp.warningLow = scaleIPMIValueFromDouble(
1170 value, mValue, rExp, bValue, bExp, bSigned);
1171 }
Willy Tude54f482021-01-26 15:59:09 -08001172 }
1173 }
1174 if (criticalInterface != sensorMap.end())
1175 {
1176 auto& criticalMap = criticalInterface->second;
1177
1178 auto criticalHigh = criticalMap.find("CriticalHigh");
1179 auto criticalLow = criticalMap.find("CriticalLow");
1180
1181 if (criticalHigh != criticalMap.end())
1182 {
1183 double value =
1184 std::visit(VariantToDoubleVisitor(), criticalHigh->second);
Konstantin Aladyshev8265af22021-12-16 18:18:10 +03001185 if (std::isfinite(value))
1186 {
1187 resp.criticalHigh = scaleIPMIValueFromDouble(
1188 value, mValue, rExp, bValue, bExp, bSigned);
1189 }
Willy Tude54f482021-01-26 15:59:09 -08001190 }
1191 if (criticalLow != criticalMap.end())
1192 {
1193 double value =
1194 std::visit(VariantToDoubleVisitor(), criticalLow->second);
Konstantin Aladyshev8265af22021-12-16 18:18:10 +03001195 if (std::isfinite(value))
1196 {
1197 resp.criticalLow = scaleIPMIValueFromDouble(
1198 value, mValue, rExp, bValue, bExp, bSigned);
1199 }
Willy Tude54f482021-01-26 15:59:09 -08001200 }
1201 }
1202 }
1203 return resp;
1204}
1205
1206ipmi::RspType<uint8_t, // readable
1207 uint8_t, // lowerNCrit
1208 uint8_t, // lowerCrit
1209 uint8_t, // lowerNrecoverable
1210 uint8_t, // upperNC
1211 uint8_t, // upperCrit
1212 uint8_t> // upperNRecoverable
1213 ipmiSenGetSensorThresholds(ipmi::Context::ptr ctx, uint8_t sensorNumber)
1214{
1215 std::string connection;
1216 std::string path;
1217
Chalapathi Venkataramashetty8c2f3c42021-06-13 19:51:17 +00001218 if (sensorNumber == reservedSensorNumber)
1219 {
1220 return ipmi::responseInvalidFieldRequest();
1221 }
1222
Willy Tude54f482021-01-26 15:59:09 -08001223 auto status = getSensorConnection(ctx, sensorNumber, connection, path);
1224 if (status)
1225 {
1226 return ipmi::response(status);
1227 }
1228
1229 DbusInterfaceMap sensorMap;
1230 if (!getSensorMap(ctx, connection, path, sensorMap))
1231 {
1232 return ipmi::responseResponseError();
1233 }
1234
1235 IPMIThresholds thresholdData;
1236 try
1237 {
1238 thresholdData = getIPMIThresholds(sensorMap);
1239 }
Patrick Williamsa2ad2da2021-10-06 12:21:46 -05001240 catch (const std::exception&)
Willy Tude54f482021-01-26 15:59:09 -08001241 {
1242 return ipmi::responseResponseError();
1243 }
1244
1245 uint8_t readable = 0;
1246 uint8_t lowerNC = 0;
1247 uint8_t lowerCritical = 0;
1248 uint8_t lowerNonRecoverable = 0;
1249 uint8_t upperNC = 0;
1250 uint8_t upperCritical = 0;
1251 uint8_t upperNonRecoverable = 0;
1252
1253 if (thresholdData.warningHigh)
1254 {
1255 readable |=
1256 1 << static_cast<uint8_t>(IPMIThresholdRespBits::upperNonCritical);
1257 upperNC = *thresholdData.warningHigh;
1258 }
1259 if (thresholdData.warningLow)
1260 {
1261 readable |=
1262 1 << static_cast<uint8_t>(IPMIThresholdRespBits::lowerNonCritical);
1263 lowerNC = *thresholdData.warningLow;
1264 }
1265
1266 if (thresholdData.criticalHigh)
1267 {
1268 readable |=
1269 1 << static_cast<uint8_t>(IPMIThresholdRespBits::upperCritical);
1270 upperCritical = *thresholdData.criticalHigh;
1271 }
1272 if (thresholdData.criticalLow)
1273 {
1274 readable |=
1275 1 << static_cast<uint8_t>(IPMIThresholdRespBits::lowerCritical);
1276 lowerCritical = *thresholdData.criticalLow;
1277 }
1278
1279 return ipmi::responseSuccess(readable, lowerNC, lowerCritical,
1280 lowerNonRecoverable, upperNC, upperCritical,
1281 upperNonRecoverable);
1282}
1283
1284/** @brief implements the get Sensor event enable command
1285 * @param sensorNumber - sensor number
1286 *
1287 * @returns IPMI completion code plus response data
1288 * - enabled - Sensor Event messages
1289 * - assertionEnabledLsb - Assertion event messages
1290 * - assertionEnabledMsb - Assertion event messages
1291 * - deassertionEnabledLsb - Deassertion event messages
1292 * - deassertionEnabledMsb - Deassertion event messages
1293 */
1294
1295ipmi::RspType<uint8_t, // enabled
1296 uint8_t, // assertionEnabledLsb
1297 uint8_t, // assertionEnabledMsb
1298 uint8_t, // deassertionEnabledLsb
1299 uint8_t> // deassertionEnabledMsb
1300 ipmiSenGetSensorEventEnable(ipmi::Context::ptr ctx, uint8_t sensorNum)
1301{
1302 std::string connection;
1303 std::string path;
1304
1305 uint8_t enabled = 0;
1306 uint8_t assertionEnabledLsb = 0;
1307 uint8_t assertionEnabledMsb = 0;
1308 uint8_t deassertionEnabledLsb = 0;
1309 uint8_t deassertionEnabledMsb = 0;
1310
Chalapathi Venkataramashetty8c2f3c42021-06-13 19:51:17 +00001311 if (sensorNum == reservedSensorNumber)
1312 {
1313 return ipmi::responseInvalidFieldRequest();
1314 }
1315
Willy Tude54f482021-01-26 15:59:09 -08001316 auto status = getSensorConnection(ctx, sensorNum, connection, path);
1317 if (status)
1318 {
1319 return ipmi::response(status);
1320 }
1321
Scron Chang2703b022021-07-06 15:47:45 +08001322#ifdef FEATURE_HYBRID_SENSORS
1323 if (auto sensor = findStaticSensor(path);
1324 sensor != ipmi::sensor::sensors.end() &&
1325 getSensorEventTypeFromPath(path) !=
1326 static_cast<uint8_t>(SensorEventTypeCodes::threshold))
1327 {
1328 enabled = static_cast<uint8_t>(
1329 IPMISensorEventEnableByte2::sensorScanningEnable);
1330 uint16_t assertionEnabled = 0;
1331 for (auto& offsetValMap : sensor->second.propertyInterfaces.begin()
1332 ->second.begin()
1333 ->second.second)
1334 {
1335 assertionEnabled |= (1 << offsetValMap.first);
1336 }
1337 assertionEnabledLsb = static_cast<uint8_t>((assertionEnabled & 0xFF));
1338 assertionEnabledMsb =
1339 static_cast<uint8_t>(((assertionEnabled >> 8) & 0xFF));
1340
1341 return ipmi::responseSuccess(enabled, assertionEnabledLsb,
1342 assertionEnabledMsb, deassertionEnabledLsb,
1343 deassertionEnabledMsb);
1344 }
1345#endif
1346
Willy Tude54f482021-01-26 15:59:09 -08001347 DbusInterfaceMap sensorMap;
1348 if (!getSensorMap(ctx, connection, path, sensorMap))
1349 {
1350 return ipmi::responseResponseError();
1351 }
1352
1353 auto warningInterface =
1354 sensorMap.find("xyz.openbmc_project.Sensor.Threshold.Warning");
1355 auto criticalInterface =
1356 sensorMap.find("xyz.openbmc_project.Sensor.Threshold.Critical");
1357 if ((warningInterface != sensorMap.end()) ||
1358 (criticalInterface != sensorMap.end()))
1359 {
1360 enabled = static_cast<uint8_t>(
1361 IPMISensorEventEnableByte2::sensorScanningEnable);
1362 if (warningInterface != sensorMap.end())
1363 {
1364 auto& warningMap = warningInterface->second;
1365
1366 auto warningHigh = warningMap.find("WarningHigh");
1367 auto warningLow = warningMap.find("WarningLow");
1368 if (warningHigh != warningMap.end())
1369 {
1370 assertionEnabledLsb |= static_cast<uint8_t>(
1371 IPMISensorEventEnableThresholds::upperNonCriticalGoingHigh);
1372 deassertionEnabledLsb |= static_cast<uint8_t>(
1373 IPMISensorEventEnableThresholds::upperNonCriticalGoingLow);
1374 }
1375 if (warningLow != warningMap.end())
1376 {
1377 assertionEnabledLsb |= static_cast<uint8_t>(
1378 IPMISensorEventEnableThresholds::lowerNonCriticalGoingLow);
1379 deassertionEnabledLsb |= static_cast<uint8_t>(
1380 IPMISensorEventEnableThresholds::lowerNonCriticalGoingHigh);
1381 }
1382 }
1383 if (criticalInterface != sensorMap.end())
1384 {
1385 auto& criticalMap = criticalInterface->second;
1386
1387 auto criticalHigh = criticalMap.find("CriticalHigh");
1388 auto criticalLow = criticalMap.find("CriticalLow");
1389
1390 if (criticalHigh != criticalMap.end())
1391 {
1392 assertionEnabledMsb |= static_cast<uint8_t>(
1393 IPMISensorEventEnableThresholds::upperCriticalGoingHigh);
1394 deassertionEnabledMsb |= static_cast<uint8_t>(
1395 IPMISensorEventEnableThresholds::upperCriticalGoingLow);
1396 }
1397 if (criticalLow != criticalMap.end())
1398 {
1399 assertionEnabledLsb |= static_cast<uint8_t>(
1400 IPMISensorEventEnableThresholds::lowerCriticalGoingLow);
1401 deassertionEnabledLsb |= static_cast<uint8_t>(
1402 IPMISensorEventEnableThresholds::lowerCriticalGoingHigh);
1403 }
1404 }
1405 }
1406
1407 return ipmi::responseSuccess(enabled, assertionEnabledLsb,
1408 assertionEnabledMsb, deassertionEnabledLsb,
1409 deassertionEnabledMsb);
1410}
1411
1412/** @brief implements the get Sensor event status command
1413 * @param sensorNumber - sensor number, FFh = reserved
1414 *
1415 * @returns IPMI completion code plus response data
1416 * - sensorEventStatus - Sensor Event messages state
1417 * - assertions - Assertion event messages
1418 * - deassertions - Deassertion event messages
1419 */
1420ipmi::RspType<uint8_t, // sensorEventStatus
1421 std::bitset<16>, // assertions
1422 std::bitset<16> // deassertion
1423 >
1424 ipmiSenGetSensorEventStatus(ipmi::Context::ptr ctx, uint8_t sensorNum)
1425{
1426 if (sensorNum == reservedSensorNumber)
1427 {
1428 return ipmi::responseInvalidFieldRequest();
1429 }
1430
1431 std::string connection;
1432 std::string path;
1433 auto status = getSensorConnection(ctx, sensorNum, connection, path);
1434 if (status)
1435 {
1436 phosphor::logging::log<phosphor::logging::level::ERR>(
1437 "ipmiSenGetSensorEventStatus: Sensor connection Error",
1438 phosphor::logging::entry("SENSOR=%d", sensorNum));
1439 return ipmi::response(status);
1440 }
1441
Scron Chang2703b022021-07-06 15:47:45 +08001442#ifdef FEATURE_HYBRID_SENSORS
1443 if (auto sensor = findStaticSensor(path);
1444 sensor != ipmi::sensor::sensors.end() &&
1445 getSensorEventTypeFromPath(path) !=
1446 static_cast<uint8_t>(SensorEventTypeCodes::threshold))
1447 {
1448 auto response = ipmi::sensor::get::mapDbusToAssertion(
1449 sensor->second, path, sensor->second.sensorInterface);
1450 std::bitset<16> assertions;
1451 // deassertions are not used.
1452 std::bitset<16> deassertions = 0;
1453 uint8_t sensorEventStatus;
1454 if (response.readingOrStateUnavailable)
1455 {
1456 sensorEventStatus |= static_cast<uint8_t>(
1457 IPMISensorReadingByte2::readingStateUnavailable);
1458 }
1459 if (response.scanningEnabled)
1460 {
1461 sensorEventStatus |= static_cast<uint8_t>(
1462 IPMISensorReadingByte2::sensorScanningEnable);
1463 }
1464 if (response.allEventMessagesEnabled)
1465 {
1466 sensorEventStatus |= static_cast<uint8_t>(
1467 IPMISensorReadingByte2::eventMessagesEnable);
1468 }
1469 assertions |= response.discreteReadingSensorStates << 8;
1470 assertions |= response.thresholdLevelsStates;
1471 return ipmi::responseSuccess(sensorEventStatus, assertions,
1472 deassertions);
1473 }
1474#endif
1475
Willy Tude54f482021-01-26 15:59:09 -08001476 DbusInterfaceMap sensorMap;
1477 if (!getSensorMap(ctx, connection, path, sensorMap))
1478 {
1479 phosphor::logging::log<phosphor::logging::level::ERR>(
1480 "ipmiSenGetSensorEventStatus: Sensor Mapping Error",
1481 phosphor::logging::entry("SENSOR=%s", path.c_str()));
1482 return ipmi::responseResponseError();
1483 }
Hao Jiangd48c9212021-02-03 15:45:06 -08001484
1485 uint8_t sensorEventStatus =
1486 static_cast<uint8_t>(IPMISensorEventEnableByte2::sensorScanningEnable);
1487 std::bitset<16> assertions = 0;
1488 std::bitset<16> deassertions = 0;
1489
1490 // handle VR typed sensor
1491 auto vrInterface = sensorMap.find(sensor::vrInterface);
1492 if (vrInterface != sensorMap.end())
1493 {
1494 if (!sensor::getVrEventStatus(ctx, connection, path,
1495 vrInterface->second, assertions))
1496 {
1497 return ipmi::responseResponseError();
1498 }
1499
1500 // both Event Message and Sensor Scanning are disable for VR.
1501 sensorEventStatus = 0;
1502 return ipmi::responseSuccess(sensorEventStatus, assertions,
1503 deassertions);
1504 }
1505
Willy Tude54f482021-01-26 15:59:09 -08001506 auto warningInterface =
1507 sensorMap.find("xyz.openbmc_project.Sensor.Threshold.Warning");
1508 auto criticalInterface =
1509 sensorMap.find("xyz.openbmc_project.Sensor.Threshold.Critical");
1510
Willy Tude54f482021-01-26 15:59:09 -08001511 std::optional<bool> criticalDeassertHigh =
1512 thresholdDeassertMap[path]["CriticalAlarmHigh"];
1513 std::optional<bool> criticalDeassertLow =
1514 thresholdDeassertMap[path]["CriticalAlarmLow"];
1515 std::optional<bool> warningDeassertHigh =
1516 thresholdDeassertMap[path]["WarningAlarmHigh"];
1517 std::optional<bool> warningDeassertLow =
1518 thresholdDeassertMap[path]["WarningAlarmLow"];
1519
Willy Tude54f482021-01-26 15:59:09 -08001520 if (criticalDeassertHigh && !*criticalDeassertHigh)
1521 {
1522 deassertions.set(static_cast<size_t>(
1523 IPMIGetSensorEventEnableThresholds::upperCriticalGoingHigh));
1524 }
1525 if (criticalDeassertLow && !*criticalDeassertLow)
1526 {
1527 deassertions.set(static_cast<size_t>(
1528 IPMIGetSensorEventEnableThresholds::upperCriticalGoingLow));
1529 }
1530 if (warningDeassertHigh && !*warningDeassertHigh)
1531 {
1532 deassertions.set(static_cast<size_t>(
1533 IPMIGetSensorEventEnableThresholds::upperNonCriticalGoingHigh));
1534 }
1535 if (warningDeassertLow && !*warningDeassertLow)
1536 {
1537 deassertions.set(static_cast<size_t>(
1538 IPMIGetSensorEventEnableThresholds::lowerNonCriticalGoingHigh));
1539 }
1540 if ((warningInterface != sensorMap.end()) ||
1541 (criticalInterface != sensorMap.end()))
1542 {
1543 sensorEventStatus = static_cast<size_t>(
1544 IPMISensorEventEnableByte2::eventMessagesEnable);
1545 if (warningInterface != sensorMap.end())
1546 {
1547 auto& warningMap = warningInterface->second;
1548
1549 auto warningHigh = warningMap.find("WarningAlarmHigh");
1550 auto warningLow = warningMap.find("WarningAlarmLow");
1551 auto warningHighAlarm = false;
1552 auto warningLowAlarm = false;
1553
1554 if (warningHigh != warningMap.end())
1555 {
1556 warningHighAlarm = std::get<bool>(warningHigh->second);
1557 }
1558 if (warningLow != warningMap.end())
1559 {
1560 warningLowAlarm = std::get<bool>(warningLow->second);
1561 }
1562 if (warningHighAlarm)
1563 {
1564 assertions.set(
1565 static_cast<size_t>(IPMIGetSensorEventEnableThresholds::
1566 upperNonCriticalGoingHigh));
1567 }
1568 if (warningLowAlarm)
1569 {
1570 assertions.set(
1571 static_cast<size_t>(IPMIGetSensorEventEnableThresholds::
1572 lowerNonCriticalGoingLow));
1573 }
1574 }
1575 if (criticalInterface != sensorMap.end())
1576 {
1577 auto& criticalMap = criticalInterface->second;
1578
1579 auto criticalHigh = criticalMap.find("CriticalAlarmHigh");
1580 auto criticalLow = criticalMap.find("CriticalAlarmLow");
1581 auto criticalHighAlarm = false;
1582 auto criticalLowAlarm = false;
1583
1584 if (criticalHigh != criticalMap.end())
1585 {
1586 criticalHighAlarm = std::get<bool>(criticalHigh->second);
1587 }
1588 if (criticalLow != criticalMap.end())
1589 {
1590 criticalLowAlarm = std::get<bool>(criticalLow->second);
1591 }
1592 if (criticalHighAlarm)
1593 {
1594 assertions.set(
1595 static_cast<size_t>(IPMIGetSensorEventEnableThresholds::
1596 upperCriticalGoingHigh));
1597 }
1598 if (criticalLowAlarm)
1599 {
1600 assertions.set(static_cast<size_t>(
1601 IPMIGetSensorEventEnableThresholds::lowerCriticalGoingLow));
1602 }
1603 }
1604 }
1605
1606 return ipmi::responseSuccess(sensorEventStatus, assertions, deassertions);
1607}
1608
Willy Tu38e7a2b2021-03-29 15:09:56 -07001609// Construct a type 1 SDR for threshold sensor.
Hao Jiange39d4d82021-04-16 17:02:40 -07001610void constructSensorSdrHeaderKey(uint16_t sensorNum, uint16_t recordID,
1611 get_sdr::SensorDataFullRecord& record)
Willy Tude54f482021-01-26 15:59:09 -08001612{
Kuiying Wanga8b5b262021-02-06 23:38:22 +08001613 get_sdr::header::set_record_id(
1614 recordID, reinterpret_cast<get_sdr::SensorDataRecordHeader*>(&record));
1615
Willy Tu38e7a2b2021-03-29 15:09:56 -07001616 uint8_t sensornumber = static_cast<uint8_t>(sensorNum);
1617 uint8_t lun = static_cast<uint8_t>(sensorNum >> 8);
1618
Kuiying Wanga8b5b262021-02-06 23:38:22 +08001619 record.header.sdr_version = ipmiSdrVersion;
1620 record.header.record_type = get_sdr::SENSOR_DATA_FULL_RECORD;
1621 record.header.record_length = sizeof(get_sdr::SensorDataFullRecord) -
1622 sizeof(get_sdr::SensorDataRecordHeader);
Willy Tu38e7a2b2021-03-29 15:09:56 -07001623 record.key.owner_id = bmcI2CAddr;
Kuiying Wanga8b5b262021-02-06 23:38:22 +08001624 record.key.owner_lun = lun;
1625 record.key.sensor_number = sensornumber;
Hao Jiange39d4d82021-04-16 17:02:40 -07001626}
Willy Tu4eca2512022-06-20 21:14:51 -07001627bool constructSensorSdr(
1628 ipmi::Context::ptr ctx,
1629 const std::unordered_set<std::string>& ipmiDecoratorPaths,
1630 uint16_t sensorNum, uint16_t recordID, const std::string& service,
1631 const std::string& path, get_sdr::SensorDataFullRecord& record)
Hao Jiange39d4d82021-04-16 17:02:40 -07001632{
Hao Jiange39d4d82021-04-16 17:02:40 -07001633 constructSensorSdrHeaderKey(sensorNum, recordID, record);
1634
1635 DbusInterfaceMap sensorMap;
1636 if (!getSensorMap(ctx, service, path, sensorMap, sensorMapSdrUpdatePeriod))
1637 {
1638 phosphor::logging::log<phosphor::logging::level::ERR>(
1639 "Failed to update sensor map for threshold sensor",
1640 phosphor::logging::entry("SERVICE=%s", service.c_str()),
1641 phosphor::logging::entry("PATH=%s", path.c_str()));
1642 return false;
1643 }
Kuiying Wanga8b5b262021-02-06 23:38:22 +08001644
1645 record.body.sensor_capabilities = 0x68; // auto rearm - todo hysteresis
1646 record.body.sensor_type = getSensorTypeFromPath(path);
1647 std::string type = getSensorTypeStringFromPath(path);
1648 auto typeCstr = type.c_str();
1649 auto findUnits = sensorUnits.find(typeCstr);
1650 if (findUnits != sensorUnits.end())
1651 {
1652 record.body.sensor_units_2_base =
1653 static_cast<uint8_t>(findUnits->second);
1654 } // else default 0x0 unspecified
1655
1656 record.body.event_reading_type = getSensorEventTypeFromPath(path);
1657
Hao Jiangd2afd052020-12-10 15:09:32 -08001658 auto sensorObject = sensorMap.find(sensor::sensorInterface);
Kuiying Wanga8b5b262021-02-06 23:38:22 +08001659 if (sensorObject == sensorMap.end())
1660 {
1661 phosphor::logging::log<phosphor::logging::level::ERR>(
1662 "getSensorDataRecord: sensorObject error");
Willy Tu38e7a2b2021-03-29 15:09:56 -07001663 return false;
Kuiying Wanga8b5b262021-02-06 23:38:22 +08001664 }
1665
1666 uint8_t entityId = 0;
1667 uint8_t entityInstance = 0x01;
1668
1669 // follow the association chain to get the parent board's entityid and
1670 // entityInstance
Willy Tu4eca2512022-06-20 21:14:51 -07001671 updateIpmiFromAssociation(path, ipmiDecoratorPaths, sensorMap, entityId,
1672 entityInstance);
Kuiying Wanga8b5b262021-02-06 23:38:22 +08001673
1674 record.body.entity_id = entityId;
1675 record.body.entity_instance = entityInstance;
1676
Shakeeb Pasha93889722021-10-14 10:20:13 +05301677 double max = 0;
1678 double min = 0;
1679 getSensorMaxMin(sensorMap, max, min);
Kuiying Wanga8b5b262021-02-06 23:38:22 +08001680
1681 int16_t mValue = 0;
1682 int8_t rExp = 0;
1683 int16_t bValue = 0;
1684 int8_t bExp = 0;
1685 bool bSigned = false;
1686
1687 if (!getSensorAttributes(max, min, mValue, rExp, bValue, bExp, bSigned))
1688 {
1689 phosphor::logging::log<phosphor::logging::level::ERR>(
1690 "getSensorDataRecord: getSensorAttributes error");
Willy Tu38e7a2b2021-03-29 15:09:56 -07001691 return false;
Kuiying Wanga8b5b262021-02-06 23:38:22 +08001692 }
1693
1694 // The record.body is a struct SensorDataFullRecordBody
1695 // from sensorhandler.hpp in phosphor-ipmi-host.
1696 // The meaning of these bits appears to come from
1697 // table 43.1 of the IPMI spec.
1698 // The above 5 sensor attributes are stuffed in as follows:
1699 // Byte 21 = AA000000 = analog interpretation, 10 signed, 00 unsigned
1700 // Byte 22-24 are for other purposes
1701 // Byte 25 = MMMMMMMM = LSB of M
1702 // Byte 26 = MMTTTTTT = MSB of M (signed), and Tolerance
1703 // Byte 27 = BBBBBBBB = LSB of B
1704 // Byte 28 = BBAAAAAA = MSB of B (signed), and LSB of Accuracy
1705 // Byte 29 = AAAAEE00 = MSB of Accuracy, exponent of Accuracy
1706 // Byte 30 = RRRRBBBB = rExp (signed), bExp (signed)
1707
1708 // apply M, B, and exponents, M and B are 10 bit values, exponents are 4
1709 record.body.m_lsb = mValue & 0xFF;
1710
1711 uint8_t mBitSign = (mValue < 0) ? 1 : 0;
1712 uint8_t mBitNine = (mValue & 0x0100) >> 8;
1713
1714 // move the smallest bit of the MSB into place (bit 9)
1715 // the MSbs are bits 7:8 in m_msb_and_tolerance
1716 record.body.m_msb_and_tolerance = (mBitSign << 7) | (mBitNine << 6);
1717
1718 record.body.b_lsb = bValue & 0xFF;
1719
1720 uint8_t bBitSign = (bValue < 0) ? 1 : 0;
1721 uint8_t bBitNine = (bValue & 0x0100) >> 8;
1722
1723 // move the smallest bit of the MSB into place (bit 9)
1724 // the MSbs are bits 7:8 in b_msb_and_accuracy_lsb
1725 record.body.b_msb_and_accuracy_lsb = (bBitSign << 7) | (bBitNine << 6);
1726
1727 uint8_t rExpSign = (rExp < 0) ? 1 : 0;
1728 uint8_t rExpBits = rExp & 0x07;
1729
1730 uint8_t bExpSign = (bExp < 0) ? 1 : 0;
1731 uint8_t bExpBits = bExp & 0x07;
1732
1733 // move rExp and bExp into place
1734 record.body.r_b_exponents =
1735 (rExpSign << 7) | (rExpBits << 4) | (bExpSign << 3) | bExpBits;
1736
1737 // Set the analog reading byte interpretation accordingly
1738 record.body.sensor_units_1 = (bSigned ? 1 : 0) << 7;
1739
1740 // TODO(): Perhaps care about Tolerance, Accuracy, and so on
1741 // These seem redundant, but derivable from the above 5 attributes
1742 // Original comment said "todo fill out rest of units"
1743
1744 // populate sensor name from path
Willy Tu38e7a2b2021-03-29 15:09:56 -07001745 auto name = sensor::parseSdrIdFromPath(path);
Paul Fertser51136982022-08-18 12:36:41 +00001746 get_sdr::body::set_id_strlen(name.size(), &record.body);
1747 get_sdr::body::set_id_type(3, &record.body); // "8-bit ASCII + Latin 1"
Kuiying Wanga8b5b262021-02-06 23:38:22 +08001748 std::strncpy(record.body.id_string, name.c_str(),
1749 sizeof(record.body.id_string));
1750
Josh Lehana55c9532020-10-28 21:59:06 -07001751 // Remember the sensor name, as determined for this sensor number
Harvey.Wu0e7a8af2022-06-10 16:46:46 +08001752 details::sdrStatsTable.updateName(sensorNum, name);
Josh Lehana55c9532020-10-28 21:59:06 -07001753
Jie Yangf0a89942021-07-29 15:30:25 -07001754 bool sensorSettable = false;
1755 auto mutability =
1756 sensorMap.find("xyz.openbmc_project.Sensor.ValueMutability");
1757 if (mutability != sensorMap.end())
1758 {
1759 sensorSettable =
1760 mappedVariant<bool>(mutability->second, "Mutable", false);
1761 }
1762 get_sdr::body::init_settable_state(sensorSettable, &record.body);
1763
1764 // Grant write permission to sensors deemed externally settable
Harvey.Wu0e7a8af2022-06-10 16:46:46 +08001765 details::sdrWriteTable.setWritePermission(sensorNum, sensorSettable);
Willy Tu530e2772021-07-02 14:42:06 -07001766
Kuiying Wanga8b5b262021-02-06 23:38:22 +08001767 IPMIThresholds thresholdData;
1768 try
1769 {
1770 thresholdData = getIPMIThresholds(sensorMap);
1771 }
Patrick Williamsa2ad2da2021-10-06 12:21:46 -05001772 catch (const std::exception&)
Kuiying Wanga8b5b262021-02-06 23:38:22 +08001773 {
1774 phosphor::logging::log<phosphor::logging::level::ERR>(
1775 "getSensorDataRecord: getIPMIThresholds error");
Willy Tu38e7a2b2021-03-29 15:09:56 -07001776 return false;
Kuiying Wanga8b5b262021-02-06 23:38:22 +08001777 }
1778
1779 if (thresholdData.criticalHigh)
1780 {
1781 record.body.upper_critical_threshold = *thresholdData.criticalHigh;
1782 record.body.supported_deassertions[1] |= static_cast<uint8_t>(
1783 IPMISensorEventEnableThresholds::criticalThreshold);
1784 record.body.supported_deassertions[1] |= static_cast<uint8_t>(
1785 IPMISensorEventEnableThresholds::upperCriticalGoingHigh);
1786 record.body.supported_assertions[1] |= static_cast<uint8_t>(
1787 IPMISensorEventEnableThresholds::upperCriticalGoingHigh);
1788 record.body.discrete_reading_setting_mask[0] |=
1789 static_cast<uint8_t>(IPMISensorReadingByte3::upperCritical);
1790 }
1791 if (thresholdData.warningHigh)
1792 {
1793 record.body.upper_noncritical_threshold = *thresholdData.warningHigh;
1794 record.body.supported_deassertions[1] |= static_cast<uint8_t>(
1795 IPMISensorEventEnableThresholds::nonCriticalThreshold);
1796 record.body.supported_deassertions[0] |= static_cast<uint8_t>(
1797 IPMISensorEventEnableThresholds::upperNonCriticalGoingHigh);
1798 record.body.supported_assertions[0] |= static_cast<uint8_t>(
1799 IPMISensorEventEnableThresholds::upperNonCriticalGoingHigh);
1800 record.body.discrete_reading_setting_mask[0] |=
1801 static_cast<uint8_t>(IPMISensorReadingByte3::upperNonCritical);
1802 }
1803 if (thresholdData.criticalLow)
1804 {
1805 record.body.lower_critical_threshold = *thresholdData.criticalLow;
1806 record.body.supported_assertions[1] |= static_cast<uint8_t>(
1807 IPMISensorEventEnableThresholds::criticalThreshold);
1808 record.body.supported_deassertions[0] |= static_cast<uint8_t>(
1809 IPMISensorEventEnableThresholds::lowerCriticalGoingLow);
1810 record.body.supported_assertions[0] |= static_cast<uint8_t>(
1811 IPMISensorEventEnableThresholds::lowerCriticalGoingLow);
1812 record.body.discrete_reading_setting_mask[0] |=
1813 static_cast<uint8_t>(IPMISensorReadingByte3::lowerCritical);
1814 }
1815 if (thresholdData.warningLow)
1816 {
1817 record.body.lower_noncritical_threshold = *thresholdData.warningLow;
1818 record.body.supported_assertions[1] |= static_cast<uint8_t>(
1819 IPMISensorEventEnableThresholds::nonCriticalThreshold);
1820 record.body.supported_deassertions[0] |= static_cast<uint8_t>(
1821 IPMISensorEventEnableThresholds::lowerNonCriticalGoingLow);
1822 record.body.supported_assertions[0] |= static_cast<uint8_t>(
1823 IPMISensorEventEnableThresholds::lowerNonCriticalGoingLow);
1824 record.body.discrete_reading_setting_mask[0] |=
1825 static_cast<uint8_t>(IPMISensorReadingByte3::lowerNonCritical);
1826 }
1827
1828 // everything that is readable is setable
1829 record.body.discrete_reading_setting_mask[1] =
1830 record.body.discrete_reading_setting_mask[0];
Willy Tu38e7a2b2021-03-29 15:09:56 -07001831 return true;
1832}
1833
Scron Chang2703b022021-07-06 15:47:45 +08001834#ifdef FEATURE_HYBRID_SENSORS
1835// Construct a type 1 SDR for discrete Sensor typed sensor.
Willy Tu11d68892022-01-20 10:37:34 -08001836void constructStaticSensorSdr(ipmi::Context::ptr, uint16_t sensorNum,
Scron Chang2703b022021-07-06 15:47:45 +08001837 uint16_t recordID,
1838 ipmi::sensor::IdInfoMap::const_iterator sensor,
1839 get_sdr::SensorDataFullRecord& record)
1840{
1841 constructSensorSdrHeaderKey(sensorNum, recordID, record);
1842
1843 record.body.entity_id = sensor->second.entityType;
1844 record.body.sensor_type = sensor->second.sensorType;
1845 record.body.event_reading_type = sensor->second.sensorReadingType;
1846 record.body.entity_instance = sensor->second.instance;
1847 if (ipmi::sensor::Mutability::Write ==
1848 (sensor->second.mutability & ipmi::sensor::Mutability::Write))
1849 {
1850 get_sdr::body::init_settable_state(true, &(record.body));
1851 }
1852
1853 auto id_string = sensor->second.sensorName;
1854
1855 if (id_string.empty())
1856 {
1857 id_string = sensor->second.sensorNameFunc(sensor->second);
1858 }
1859
1860 if (id_string.length() > FULL_RECORD_ID_STR_MAX_LENGTH)
1861 {
1862 get_sdr::body::set_id_strlen(FULL_RECORD_ID_STR_MAX_LENGTH,
1863 &(record.body));
1864 }
1865 else
1866 {
1867 get_sdr::body::set_id_strlen(id_string.length(), &(record.body));
1868 }
Paul Fertser51136982022-08-18 12:36:41 +00001869 get_sdr::body::set_id_type(3, &record.body); // "8-bit ASCII + Latin 1"
Scron Chang2703b022021-07-06 15:47:45 +08001870 std::strncpy(record.body.id_string, id_string.c_str(),
1871 get_sdr::body::get_id_strlen(&(record.body)));
1872}
1873#endif
1874
Hao Jiange39d4d82021-04-16 17:02:40 -07001875// Construct type 3 SDR header and key (for VR and other discrete sensors)
1876void constructEventSdrHeaderKey(uint16_t sensorNum, uint16_t recordID,
1877 get_sdr::SensorDataEventRecord& record)
Willy Tu61992ad2021-03-29 15:33:20 -07001878{
1879 uint8_t sensornumber = static_cast<uint8_t>(sensorNum);
1880 uint8_t lun = static_cast<uint8_t>(sensorNum >> 8);
1881
1882 get_sdr::header::set_record_id(
1883 recordID, reinterpret_cast<get_sdr::SensorDataRecordHeader*>(&record));
1884
1885 record.header.sdr_version = ipmiSdrVersion;
1886 record.header.record_type = get_sdr::SENSOR_DATA_EVENT_RECORD;
1887 record.header.record_length = sizeof(get_sdr::SensorDataEventRecord) -
1888 sizeof(get_sdr::SensorDataRecordHeader);
1889 record.key.owner_id = bmcI2CAddr;
1890 record.key.owner_lun = lun;
1891 record.key.sensor_number = sensornumber;
1892
1893 record.body.entity_id = 0x00;
1894 record.body.entity_instance = 0x01;
Hao Jiange39d4d82021-04-16 17:02:40 -07001895}
Willy Tu61992ad2021-03-29 15:33:20 -07001896
Hao Jiange39d4d82021-04-16 17:02:40 -07001897// Construct a type 3 SDR for VR typed sensor(daemon).
Willy Tu4eca2512022-06-20 21:14:51 -07001898bool constructVrSdr(ipmi::Context::ptr ctx,
1899 const std::unordered_set<std::string>& ipmiDecoratorPaths,
1900 uint16_t sensorNum, uint16_t recordID,
1901 const std::string& service, const std::string& path,
Hao Jiange39d4d82021-04-16 17:02:40 -07001902 get_sdr::SensorDataEventRecord& record)
1903{
Hao Jiange39d4d82021-04-16 17:02:40 -07001904 constructEventSdrHeaderKey(sensorNum, recordID, record);
1905
1906 DbusInterfaceMap sensorMap;
1907 if (!getSensorMap(ctx, service, path, sensorMap, sensorMapSdrUpdatePeriod))
1908 {
1909 phosphor::logging::log<phosphor::logging::level::ERR>(
1910 "Failed to update sensor map for VR sensor",
1911 phosphor::logging::entry("SERVICE=%s", service.c_str()),
1912 phosphor::logging::entry("PATH=%s", path.c_str()));
1913 return false;
1914 }
Willy Tu61992ad2021-03-29 15:33:20 -07001915 // follow the association chain to get the parent board's entityid and
1916 // entityInstance
Willy Tu4eca2512022-06-20 21:14:51 -07001917 updateIpmiFromAssociation(path, ipmiDecoratorPaths, sensorMap,
1918 record.body.entity_id,
Willy Tu61992ad2021-03-29 15:33:20 -07001919 record.body.entity_instance);
1920
1921 // Sensor type is hardcoded as a module/board type instead of parsing from
1922 // sensor path. This is because VR control is allocated in an independent
1923 // path(/xyz/openbmc_project/vr/profile/...) which is not categorized by
1924 // types.
1925 static constexpr const uint8_t module_board_type = 0x15;
1926 record.body.sensor_type = module_board_type;
1927 record.body.event_reading_type = 0x00;
1928
1929 record.body.sensor_record_sharing_1 = 0x00;
1930 record.body.sensor_record_sharing_2 = 0x00;
1931
1932 // populate sensor name from path
1933 auto name = sensor::parseSdrIdFromPath(path);
1934 int nameSize = std::min(name.size(), sizeof(record.body.id_string));
Paul Fertser51136982022-08-18 12:36:41 +00001935 get_sdr::body::set_id_strlen(nameSize, &record.body);
1936 get_sdr::body::set_id_type(3, &record.body); // "8-bit ASCII + Latin 1"
Willy Tu61992ad2021-03-29 15:33:20 -07001937 std::memset(record.body.id_string, 0x00, sizeof(record.body.id_string));
1938 std::memcpy(record.body.id_string, name.c_str(), nameSize);
1939
1940 // Remember the sensor name, as determined for this sensor number
Harvey.Wu0e7a8af2022-06-10 16:46:46 +08001941 details::sdrStatsTable.updateName(sensorNum, name);
Hao Jiange39d4d82021-04-16 17:02:40 -07001942
1943 return true;
Willy Tu61992ad2021-03-29 15:33:20 -07001944}
1945
Johnathan Mantey6619ae42021-08-06 11:21:10 -07001946static inline uint16_t getNumberOfSensors()
1947{
1948 return std::min(getSensorTree().size(), maxIPMISensors);
1949}
1950
Willy Tu4eca2512022-06-20 21:14:51 -07001951static int getSensorDataRecord(
1952 ipmi::Context::ptr ctx,
1953 const std::unordered_set<std::string>& ipmiDecoratorPaths,
1954 std::vector<uint8_t>& recordData, uint16_t recordID,
1955 uint8_t readBytes = std::numeric_limits<uint8_t>::max())
Willy Tu38e7a2b2021-03-29 15:09:56 -07001956{
1957 size_t fruCount = 0;
1958 ipmi::Cc ret = ipmi::storage::getFruSdrCount(ctx, fruCount);
1959 if (ret != ipmi::ccSuccess)
1960 {
1961 phosphor::logging::log<phosphor::logging::level::ERR>(
1962 "getSensorDataRecord: getFruSdrCount error");
1963 return GENERAL_ERROR;
1964 }
1965
Harvey Wu05d17c02021-09-15 08:46:59 +08001966 const auto& entityRecords =
1967 ipmi::sensor::EntityInfoMapContainer::getContainer()
1968 ->getIpmiEntityRecords();
1969 size_t entityCount = entityRecords.size();
1970
selvaganapathi7b2e5502023-02-14 07:10:47 +05301971 recordData.clear();
Harvey Wu05d17c02021-09-15 08:46:59 +08001972 size_t lastRecord = getNumberOfSensors() + fruCount +
1973 ipmi::storage::type12Count + entityCount - 1;
Willy Tu38e7a2b2021-03-29 15:09:56 -07001974 if (recordID == lastRecordIndex)
1975 {
1976 recordID = lastRecord;
1977 }
1978 if (recordID > lastRecord)
1979 {
1980 phosphor::logging::log<phosphor::logging::level::ERR>(
1981 "getSensorDataRecord: recordID > lastRecord error");
1982 return GENERAL_ERROR;
1983 }
1984
Johnathan Mantey6619ae42021-08-06 11:21:10 -07001985 if (recordID >= getNumberOfSensors())
Willy Tu38e7a2b2021-03-29 15:09:56 -07001986 {
Harvey Wu05d17c02021-09-15 08:46:59 +08001987 size_t sdrIndex = recordID - getNumberOfSensors();
Willy Tu38e7a2b2021-03-29 15:09:56 -07001988
Harvey Wu05d17c02021-09-15 08:46:59 +08001989 if (sdrIndex >= fruCount + ipmi::storage::type12Count)
1990 {
1991 // handle type 8 entity map records
1992 ipmi::sensor::EntityInfoMap::const_iterator entity =
1993 entityRecords.find(static_cast<uint8_t>(
1994 sdrIndex - fruCount - ipmi::storage::type12Count));
1995 if (entity == entityRecords.end())
1996 {
1997 return IPMI_CC_SENSOR_INVALID;
1998 }
1999 recordData = ipmi::storage::getType8SDRs(entity, recordID);
2000 }
2001 else if (sdrIndex >= fruCount)
Willy Tu38e7a2b2021-03-29 15:09:56 -07002002 {
2003 // handle type 12 hardcoded records
Harvey Wu05d17c02021-09-15 08:46:59 +08002004 size_t type12Index = sdrIndex - fruCount;
Willy Tu38e7a2b2021-03-29 15:09:56 -07002005 if (type12Index >= ipmi::storage::type12Count)
2006 {
2007 phosphor::logging::log<phosphor::logging::level::ERR>(
2008 "getSensorDataRecord: type12Index error");
2009 return GENERAL_ERROR;
2010 }
2011 recordData = ipmi::storage::getType12SDRs(type12Index, recordID);
2012 }
2013 else
2014 {
2015 // handle fru records
2016 get_sdr::SensorDataFruRecord data;
Harvey Wu05d17c02021-09-15 08:46:59 +08002017 ret = ipmi::storage::getFruSdrs(ctx, sdrIndex, data);
Willy Tu38e7a2b2021-03-29 15:09:56 -07002018 if (ret != IPMI_CC_OK)
2019 {
2020 return GENERAL_ERROR;
2021 }
2022 data.header.record_id_msb = recordID >> 8;
2023 data.header.record_id_lsb = recordID & 0xFF;
selvaganapathi7b2e5502023-02-14 07:10:47 +05302024 recordData.insert(recordData.end(),
2025 reinterpret_cast<uint8_t*>(&data),
2026 reinterpret_cast<uint8_t*>(&data) + sizeof(data));
Willy Tu38e7a2b2021-03-29 15:09:56 -07002027 }
2028
2029 return 0;
2030 }
2031
Johnathan Mantey6619ae42021-08-06 11:21:10 -07002032 // Perform a incremental scan of the SDR Record ID's and translate the
2033 // first 765 SDR records (i.e. maxIPMISensors) into IPMI Sensor
2034 // Numbers. The IPMI sensor numbers are not linear, and have a reserved
2035 // gap at 0xff. This code creates 254 sensors per LUN, excepting LUN 2
2036 // which has special meaning.
Willy Tu38e7a2b2021-03-29 15:09:56 -07002037 std::string connection;
2038 std::string path;
Hao Jiange39d4d82021-04-16 17:02:40 -07002039 std::vector<std::string> interfaces;
Johnathan Manteyce982772021-07-28 15:08:30 -07002040 uint16_t sensNumFromRecID{recordID};
Johnathan Mantey6619ae42021-08-06 11:21:10 -07002041 if ((recordID > lun0MaxSensorNum) && (recordID < lun1MaxSensorNum))
Johnathan Manteyce982772021-07-28 15:08:30 -07002042 {
Johnathan Mantey6619ae42021-08-06 11:21:10 -07002043 // LUN 0 has one reserved sensor number. Compensate here by adding one
2044 // to the record ID
2045 sensNumFromRecID = recordID + 1;
Johnathan Manteyce982772021-07-28 15:08:30 -07002046 ctx->lun = 1;
2047 }
Johnathan Mantey6619ae42021-08-06 11:21:10 -07002048 else if ((recordID >= lun1MaxSensorNum) && (recordID < maxIPMISensors))
Johnathan Manteyce982772021-07-28 15:08:30 -07002049 {
Johnathan Mantey6619ae42021-08-06 11:21:10 -07002050 // LUN 0, 1 have a reserved sensor number. Compensate here by adding 2
2051 // to the record ID. Skip all 256 sensors in LUN 2, as it has special
2052 // rules governing its use.
2053 sensNumFromRecID = recordID + (maxSensorsPerLUN + 1) + 2;
Johnathan Manteyce982772021-07-28 15:08:30 -07002054 ctx->lun = 3;
2055 }
Hao Jiange39d4d82021-04-16 17:02:40 -07002056
Johnathan Mantey6619ae42021-08-06 11:21:10 -07002057 auto status =
2058 getSensorConnection(ctx, static_cast<uint8_t>(sensNumFromRecID),
2059 connection, path, &interfaces);
Willy Tu38e7a2b2021-03-29 15:09:56 -07002060 if (status)
2061 {
2062 phosphor::logging::log<phosphor::logging::level::ERR>(
2063 "getSensorDataRecord: getSensorConnection error");
2064 return GENERAL_ERROR;
2065 }
Willy Tu38e7a2b2021-03-29 15:09:56 -07002066 uint16_t sensorNum = getSensorNumberFromPath(path);
Johnathan Mantey6619ae42021-08-06 11:21:10 -07002067 // Return an error on LUN 2 assingments, and any sensor number beyond the
2068 // range of LUN 3
2069 if (((sensorNum > lun1MaxSensorNum) && (sensorNum <= maxIPMISensors)) ||
2070 (sensorNum > lun3MaxSensorNum))
Willy Tu38e7a2b2021-03-29 15:09:56 -07002071 {
2072 phosphor::logging::log<phosphor::logging::level::ERR>(
2073 "getSensorDataRecord: invalidSensorNumber");
2074 return GENERAL_ERROR;
2075 }
Johnathan Manteyce982772021-07-28 15:08:30 -07002076 uint8_t sensornumber = static_cast<uint8_t>(sensorNum);
2077 uint8_t lun = static_cast<uint8_t>(sensorNum >> 8);
2078
Johnathan Mantey6619ae42021-08-06 11:21:10 -07002079 if ((sensornumber != static_cast<uint8_t>(sensNumFromRecID)) &&
2080 (lun != ctx->lun))
Johnathan Manteyce982772021-07-28 15:08:30 -07002081 {
2082 phosphor::logging::log<phosphor::logging::level::ERR>(
2083 "getSensorDataRecord: sensor record mismatch");
2084 return GENERAL_ERROR;
2085 }
Willy Tu38e7a2b2021-03-29 15:09:56 -07002086
Willy Tu38e7a2b2021-03-29 15:09:56 -07002087 // Construct full record (SDR type 1) for the threshold sensors
Hao Jiange39d4d82021-04-16 17:02:40 -07002088 if (std::find(interfaces.begin(), interfaces.end(),
2089 sensor::sensorInterface) != interfaces.end())
Willy Tu38e7a2b2021-03-29 15:09:56 -07002090 {
Willy Tu11d68892022-01-20 10:37:34 -08002091 get_sdr::SensorDataFullRecord record = {};
Willy Tu38e7a2b2021-03-29 15:09:56 -07002092
Hao Jiange39d4d82021-04-16 17:02:40 -07002093 // If the request doesn't read SDR body, construct only header and key
2094 // part to avoid additional DBus transaction.
2095 if (readBytes <= sizeof(record.header) + sizeof(record.key))
2096 {
2097 constructSensorSdrHeaderKey(sensorNum, recordID, record);
2098 }
Willy Tu4eca2512022-06-20 21:14:51 -07002099 else if (!constructSensorSdr(ctx, ipmiDecoratorPaths, sensorNum,
2100 recordID, connection, path, record))
Willy Tu38e7a2b2021-03-29 15:09:56 -07002101 {
2102 return GENERAL_ERROR;
2103 }
Hao Jiange39d4d82021-04-16 17:02:40 -07002104
selvaganapathi7b2e5502023-02-14 07:10:47 +05302105 recordData.insert(recordData.end(), reinterpret_cast<uint8_t*>(&record),
2106 reinterpret_cast<uint8_t*>(&record) + sizeof(record));
Willy Tu61992ad2021-03-29 15:33:20 -07002107
2108 return 0;
Willy Tu38e7a2b2021-03-29 15:09:56 -07002109 }
Willy Tu61992ad2021-03-29 15:33:20 -07002110
Scron Chang2703b022021-07-06 15:47:45 +08002111#ifdef FEATURE_HYBRID_SENSORS
2112 if (auto sensor = findStaticSensor(path);
2113 sensor != ipmi::sensor::sensors.end() &&
2114 getSensorEventTypeFromPath(path) !=
2115 static_cast<uint8_t>(SensorEventTypeCodes::threshold))
2116 {
Willy Tu11d68892022-01-20 10:37:34 -08002117 get_sdr::SensorDataFullRecord record = {};
Scron Chang2703b022021-07-06 15:47:45 +08002118
2119 // If the request doesn't read SDR body, construct only header and key
2120 // part to avoid additional DBus transaction.
2121 if (readBytes <= sizeof(record.header) + sizeof(record.key))
2122 {
2123 constructSensorSdrHeaderKey(sensorNum, recordID, record);
2124 }
2125 else
2126 {
2127 constructStaticSensorSdr(ctx, sensorNum, recordID, sensor, record);
2128 }
2129
selvaganapathi7b2e5502023-02-14 07:10:47 +05302130 recordData.insert(recordData.end(), reinterpret_cast<uint8_t*>(&record),
2131 reinterpret_cast<uint8_t*>(&record) + sizeof(record));
Scron Chang2703b022021-07-06 15:47:45 +08002132
2133 return 0;
2134 }
2135#endif
2136
Willy Tu61992ad2021-03-29 15:33:20 -07002137 // Contruct SDR type 3 record for VR sensor (daemon)
Hao Jiange39d4d82021-04-16 17:02:40 -07002138 if (std::find(interfaces.begin(), interfaces.end(), sensor::vrInterface) !=
2139 interfaces.end())
Willy Tu61992ad2021-03-29 15:33:20 -07002140 {
Willy Tu11d68892022-01-20 10:37:34 -08002141 get_sdr::SensorDataEventRecord record = {};
Willy Tu61992ad2021-03-29 15:33:20 -07002142
Hao Jiange39d4d82021-04-16 17:02:40 -07002143 // If the request doesn't read SDR body, construct only header and key
2144 // part to avoid additional DBus transaction.
2145 if (readBytes <= sizeof(record.header) + sizeof(record.key))
2146 {
2147 constructEventSdrHeaderKey(sensorNum, recordID, record);
2148 }
Willy Tu4eca2512022-06-20 21:14:51 -07002149 else if (!constructVrSdr(ctx, ipmiDecoratorPaths, sensorNum, recordID,
2150 connection, path, record))
Hao Jiange39d4d82021-04-16 17:02:40 -07002151 {
2152 return GENERAL_ERROR;
2153 }
selvaganapathi7b2e5502023-02-14 07:10:47 +05302154 recordData.insert(recordData.end(), reinterpret_cast<uint8_t*>(&record),
2155 reinterpret_cast<uint8_t*>(&record) + sizeof(record));
Willy Tu61992ad2021-03-29 15:33:20 -07002156 }
2157
Willy Tude54f482021-01-26 15:59:09 -08002158 return 0;
2159}
2160
2161/** @brief implements the get SDR Info command
2162 * @param count - Operation
2163 *
2164 * @returns IPMI completion code plus response data
2165 * - sdrCount - sensor/SDR count
2166 * - lunsAndDynamicPopulation - static/Dynamic sensor population flag
2167 */
2168static ipmi::RspType<uint8_t, // respcount
2169 uint8_t, // dynamic population flags
2170 uint32_t // last time a sensor was added
2171 >
2172 ipmiSensorGetDeviceSdrInfo(ipmi::Context::ptr ctx,
2173 std::optional<uint8_t> count)
2174{
2175 auto& sensorTree = getSensorTree();
2176 uint8_t sdrCount = 0;
Kuiying Wanga8b5b262021-02-06 23:38:22 +08002177 uint16_t recordID = 0;
2178 std::vector<uint8_t> record;
Willy Tude54f482021-01-26 15:59:09 -08002179 // Sensors are dynamically allocated, and there is at least one LUN
2180 uint8_t lunsAndDynamicPopulation = 0x80;
2181 constexpr uint8_t getSdrCount = 0x01;
2182 constexpr uint8_t getSensorCount = 0x00;
2183
2184 if (!getSensorSubtree(sensorTree) || sensorTree.empty())
2185 {
2186 return ipmi::responseResponseError();
2187 }
Johnathan Mantey6619ae42021-08-06 11:21:10 -07002188 uint16_t numSensors = getNumberOfSensors();
Willy Tude54f482021-01-26 15:59:09 -08002189 if (count.value_or(0) == getSdrCount)
2190 {
Willy Tu4eca2512022-06-20 21:14:51 -07002191 auto& ipmiDecoratorPaths = getIpmiDecoratorPaths(ctx);
2192
Willy Tude54f482021-01-26 15:59:09 -08002193 // Count the number of Type 1 SDR entries assigned to the LUN
Willy Tu4eca2512022-06-20 21:14:51 -07002194 while (!getSensorDataRecord(
2195 ctx, ipmiDecoratorPaths.value_or(std::unordered_set<std::string>()),
2196 record, recordID++))
Willy Tude54f482021-01-26 15:59:09 -08002197 {
2198 get_sdr::SensorDataRecordHeader* hdr =
2199 reinterpret_cast<get_sdr::SensorDataRecordHeader*>(
Kuiying Wanga8b5b262021-02-06 23:38:22 +08002200 record.data());
selvaganapathi7b2e5502023-02-14 07:10:47 +05302201 if (!hdr)
2202 {
2203 continue;
2204 }
2205
2206 if (hdr->record_type == get_sdr::SENSOR_DATA_FULL_RECORD)
Willy Tude54f482021-01-26 15:59:09 -08002207 {
Kuiying Wanga8b5b262021-02-06 23:38:22 +08002208 get_sdr::SensorDataFullRecord* recordData =
Willy Tude54f482021-01-26 15:59:09 -08002209 reinterpret_cast<get_sdr::SensorDataFullRecord*>(
Kuiying Wanga8b5b262021-02-06 23:38:22 +08002210 record.data());
2211 if (ctx->lun == recordData->key.owner_lun)
Willy Tude54f482021-01-26 15:59:09 -08002212 {
2213 sdrCount++;
2214 }
2215 }
selvaganapathi7b2e5502023-02-14 07:10:47 +05302216 else if (hdr->record_type == get_sdr::SENSOR_DATA_COMPACT_RECORD)
2217 {
2218 get_sdr::SensorDataCompactRecord* recordData =
2219 reinterpret_cast<get_sdr::SensorDataCompactRecord*>(
2220 record.data());
2221 if (ctx->lun == recordData->key.owner_lun)
2222 {
2223 sdrCount++;
2224 }
2225 }
2226 else if (hdr->record_type == get_sdr::SENSOR_DATA_FRU_RECORD)
2227 {
2228 sdrCount++;
2229 }
Willy Tude54f482021-01-26 15:59:09 -08002230 }
selvaganapathi7b2e5502023-02-14 07:10:47 +05302231 sdrCount += ipmi::storage::type12Count;
Willy Tude54f482021-01-26 15:59:09 -08002232 }
2233 else if (count.value_or(0) == getSensorCount)
2234 {
2235 // Return the number of sensors attached to the LUN
2236 if ((ctx->lun == 0) && (numSensors > 0))
2237 {
2238 sdrCount =
2239 (numSensors > maxSensorsPerLUN) ? maxSensorsPerLUN : numSensors;
2240 }
2241 else if ((ctx->lun == 1) && (numSensors > maxSensorsPerLUN))
2242 {
2243 sdrCount = (numSensors > (2 * maxSensorsPerLUN))
2244 ? maxSensorsPerLUN
2245 : (numSensors - maxSensorsPerLUN) & maxSensorsPerLUN;
2246 }
2247 else if (ctx->lun == 3)
2248 {
2249 if (numSensors <= maxIPMISensors)
2250 {
2251 sdrCount =
2252 (numSensors - (2 * maxSensorsPerLUN)) & maxSensorsPerLUN;
2253 }
2254 else
2255 {
2256 // error
2257 throw std::out_of_range(
2258 "Maximum number of IPMI sensors exceeded.");
2259 }
2260 }
2261 }
2262 else
2263 {
2264 return ipmi::responseInvalidFieldRequest();
2265 }
2266
2267 // Get Sensor count. This returns the number of sensors
2268 if (numSensors > 0)
2269 {
2270 lunsAndDynamicPopulation |= 1;
2271 }
2272 if (numSensors > maxSensorsPerLUN)
2273 {
2274 lunsAndDynamicPopulation |= 2;
2275 }
2276 if (numSensors >= (maxSensorsPerLUN * 2))
2277 {
2278 lunsAndDynamicPopulation |= 8;
2279 }
2280 if (numSensors > maxIPMISensors)
2281 {
2282 // error
2283 throw std::out_of_range("Maximum number of IPMI sensors exceeded.");
2284 }
2285
2286 return ipmi::responseSuccess(sdrCount, lunsAndDynamicPopulation,
2287 sdrLastAdd);
2288}
2289
2290/* end sensor commands */
2291
2292/* storage commands */
2293
2294ipmi::RspType<uint8_t, // sdr version
2295 uint16_t, // record count
2296 uint16_t, // free space
2297 uint32_t, // most recent addition
2298 uint32_t, // most recent erase
2299 uint8_t // operationSupport
2300 >
2301 ipmiStorageGetSDRRepositoryInfo(ipmi::Context::ptr ctx)
2302{
2303 auto& sensorTree = getSensorTree();
2304 constexpr const uint16_t unspecifiedFreeSpace = 0xFFFF;
Kuiying Wanga8b5b262021-02-06 23:38:22 +08002305 if (!getSensorSubtree(sensorTree) && sensorTree.empty())
Willy Tude54f482021-01-26 15:59:09 -08002306 {
2307 return ipmi::responseResponseError();
2308 }
2309
2310 size_t fruCount = 0;
2311 ipmi::Cc ret = ipmi::storage::getFruSdrCount(ctx, fruCount);
2312 if (ret != ipmi::ccSuccess)
2313 {
2314 return ipmi::response(ret);
2315 }
2316
2317 uint16_t recordCount =
Johnathan Mantey6619ae42021-08-06 11:21:10 -07002318 getNumberOfSensors() + fruCount + ipmi::storage::type12Count;
Willy Tude54f482021-01-26 15:59:09 -08002319
2320 uint8_t operationSupport = static_cast<uint8_t>(
2321 SdrRepositoryInfoOps::overflow); // write not supported
2322
2323 operationSupport |=
2324 static_cast<uint8_t>(SdrRepositoryInfoOps::allocCommandSupported);
2325 operationSupport |= static_cast<uint8_t>(
2326 SdrRepositoryInfoOps::reserveSDRRepositoryCommandSupported);
2327 return ipmi::responseSuccess(ipmiSdrVersion, recordCount,
2328 unspecifiedFreeSpace, sdrLastAdd,
2329 sdrLastRemove, operationSupport);
2330}
2331
2332/** @brief implements the get SDR allocation info command
2333 *
2334 * @returns IPMI completion code plus response data
2335 * - allocUnits - Number of possible allocation units
2336 * - allocUnitSize - Allocation unit size in bytes.
2337 * - allocUnitFree - Number of free allocation units
2338 * - allocUnitLargestFree - Largest free block in allocation units
2339 * - maxRecordSize - Maximum record size in allocation units.
2340 */
2341ipmi::RspType<uint16_t, // allocUnits
2342 uint16_t, // allocUnitSize
2343 uint16_t, // allocUnitFree
2344 uint16_t, // allocUnitLargestFree
2345 uint8_t // maxRecordSize
2346 >
2347 ipmiStorageGetSDRAllocationInfo()
2348{
2349 // 0000h unspecified number of alloc units
2350 constexpr uint16_t allocUnits = 0;
2351
2352 constexpr uint16_t allocUnitFree = 0;
2353 constexpr uint16_t allocUnitLargestFree = 0;
2354 // only allow one block at a time
2355 constexpr uint8_t maxRecordSize = 1;
2356
2357 return ipmi::responseSuccess(allocUnits, maxSDRTotalSize, allocUnitFree,
2358 allocUnitLargestFree, maxRecordSize);
2359}
2360
2361/** @brief implements the reserve SDR command
2362 * @returns IPMI completion code plus response data
2363 * - sdrReservationID
2364 */
2365ipmi::RspType<uint16_t> ipmiStorageReserveSDR()
2366{
2367 sdrReservationID++;
2368 if (sdrReservationID == 0)
2369 {
2370 sdrReservationID++;
2371 }
2372
2373 return ipmi::responseSuccess(sdrReservationID);
2374}
2375
2376ipmi::RspType<uint16_t, // next record ID
2377 std::vector<uint8_t> // payload
2378 >
2379 ipmiStorageGetSDR(ipmi::Context::ptr ctx, uint16_t reservationID,
2380 uint16_t recordID, uint8_t offset, uint8_t bytesToRead)
2381{
Kuiying Wanga8b5b262021-02-06 23:38:22 +08002382 size_t fruCount = 0;
Willy Tude54f482021-01-26 15:59:09 -08002383 // reservation required for partial reads with non zero offset into
2384 // record
2385 if ((sdrReservationID == 0 || reservationID != sdrReservationID) && offset)
2386 {
Kuiying Wanga8b5b262021-02-06 23:38:22 +08002387 phosphor::logging::log<phosphor::logging::level::ERR>(
2388 "ipmiStorageGetSDR: responseInvalidReservationId");
Willy Tude54f482021-01-26 15:59:09 -08002389 return ipmi::responseInvalidReservationId();
2390 }
Willy Tude54f482021-01-26 15:59:09 -08002391 ipmi::Cc ret = ipmi::storage::getFruSdrCount(ctx, fruCount);
2392 if (ret != ipmi::ccSuccess)
2393 {
Kuiying Wanga8b5b262021-02-06 23:38:22 +08002394 phosphor::logging::log<phosphor::logging::level::ERR>(
2395 "ipmiStorageGetSDR: getFruSdrCount error");
Willy Tude54f482021-01-26 15:59:09 -08002396 return ipmi::response(ret);
2397 }
2398
Harvey Wu05d17c02021-09-15 08:46:59 +08002399 const auto& entityRecords =
2400 ipmi::sensor::EntityInfoMapContainer::getContainer()
2401 ->getIpmiEntityRecords();
2402 int entityCount = entityRecords.size();
2403
Kuiying Wanga8b5b262021-02-06 23:38:22 +08002404 auto& sensorTree = getSensorTree();
Harvey Wu05d17c02021-09-15 08:46:59 +08002405 size_t lastRecord = getNumberOfSensors() + fruCount +
2406 ipmi::storage::type12Count + entityCount - 1;
Kuiying Wanga8b5b262021-02-06 23:38:22 +08002407 uint16_t nextRecordId = lastRecord > recordID ? recordID + 1 : 0XFFFF;
2408
2409 if (!getSensorSubtree(sensorTree) && sensorTree.empty())
Willy Tude54f482021-01-26 15:59:09 -08002410 {
Kuiying Wanga8b5b262021-02-06 23:38:22 +08002411 phosphor::logging::log<phosphor::logging::level::ERR>(
2412 "ipmiStorageGetSDR: getSensorSubtree error");
2413 return ipmi::responseResponseError();
Willy Tude54f482021-01-26 15:59:09 -08002414 }
Kuiying Wanga8b5b262021-02-06 23:38:22 +08002415
Willy Tu4eca2512022-06-20 21:14:51 -07002416 auto& ipmiDecoratorPaths = getIpmiDecoratorPaths(ctx);
2417
Kuiying Wanga8b5b262021-02-06 23:38:22 +08002418 std::vector<uint8_t> record;
Willy Tu4eca2512022-06-20 21:14:51 -07002419 if (getSensorDataRecord(
2420 ctx, ipmiDecoratorPaths.value_or(std::unordered_set<std::string>()),
2421 record, recordID, offset + bytesToRead))
Willy Tude54f482021-01-26 15:59:09 -08002422 {
Kuiying Wanga8b5b262021-02-06 23:38:22 +08002423 phosphor::logging::log<phosphor::logging::level::ERR>(
2424 "ipmiStorageGetSDR: fail to get SDR");
Willy Tude54f482021-01-26 15:59:09 -08002425 return ipmi::responseInvalidFieldRequest();
2426 }
Willy Tude54f482021-01-26 15:59:09 -08002427 get_sdr::SensorDataRecordHeader* hdr =
Kuiying Wanga8b5b262021-02-06 23:38:22 +08002428 reinterpret_cast<get_sdr::SensorDataRecordHeader*>(record.data());
Willy Tude54f482021-01-26 15:59:09 -08002429 if (!hdr)
2430 {
2431 phosphor::logging::log<phosphor::logging::level::ERR>(
Kuiying Wanga8b5b262021-02-06 23:38:22 +08002432 "ipmiStorageGetSDR: record header is null");
2433 return ipmi::responseSuccess(nextRecordId, record);
Willy Tude54f482021-01-26 15:59:09 -08002434 }
2435
2436 size_t sdrLength =
2437 sizeof(get_sdr::SensorDataRecordHeader) + hdr->record_length;
2438 if (sdrLength < (offset + bytesToRead))
2439 {
2440 bytesToRead = sdrLength - offset;
2441 }
2442
2443 uint8_t* respStart = reinterpret_cast<uint8_t*>(hdr) + offset;
2444 if (!respStart)
2445 {
2446 phosphor::logging::log<phosphor::logging::level::ERR>(
Kuiying Wanga8b5b262021-02-06 23:38:22 +08002447 "ipmiStorageGetSDR: record is null");
2448 return ipmi::responseSuccess(nextRecordId, record);
Willy Tude54f482021-01-26 15:59:09 -08002449 }
2450
2451 std::vector<uint8_t> recordData(respStart, respStart + bytesToRead);
Kuiying Wanga8b5b262021-02-06 23:38:22 +08002452
Willy Tude54f482021-01-26 15:59:09 -08002453 return ipmi::responseSuccess(nextRecordId, recordData);
2454}
adarshgrami042e9db2022-09-15 10:34:34 +05302455namespace dcmi
2456{
2457
2458ipmi::RspType<uint8_t, // No of instances for requested id
2459 uint8_t, // No of record ids in the response
2460 std::vector<uint16_t> // SDR Record ID corresponding to the Entity
2461 // IDs
2462 >
2463 getSensorInfo(ipmi::Context::ptr ctx, uint8_t sensorType, uint8_t entityId,
Willy Tu0679e4b2022-11-11 14:34:33 -08002464 uint8_t entityInstance,
2465 [[maybe_unused]] uint8_t instanceStart)
adarshgrami042e9db2022-09-15 10:34:34 +05302466{
2467 auto match = ipmi::dcmi::validEntityId.find(entityId);
2468 if (match == ipmi::dcmi::validEntityId.end())
2469 {
2470 log<level::ERR>("Unknown Entity ID", entry("ENTITY_ID=%d", entityId));
2471
2472 return ipmi::responseInvalidFieldRequest();
2473 }
2474
2475 if (sensorType != ipmi::dcmi::temperatureSensorType)
2476 {
2477 log<level::ERR>("Invalid sensor type",
2478 entry("SENSOR_TYPE=%d", sensorType));
2479
2480 return ipmi::responseInvalidFieldRequest();
2481 }
2482 auto& sensorTree = getSensorTree();
2483 if (!getSensorSubtree(sensorTree) && sensorTree.empty())
2484 {
2485 return ipmi::responseUnspecifiedError();
2486 }
2487
2488 std::vector<uint16_t> sensorRec{};
2489 uint8_t numInstances = 0;
2490
Willy Tu0679e4b2022-11-11 14:34:33 -08002491 auto& ipmiDecoratorPaths = getIpmiDecoratorPaths(ctx);
adarshgrami042e9db2022-09-15 10:34:34 +05302492 for (const auto& sensor : sensorTree)
2493 {
2494 auto sensorTypeValue = getSensorTypeFromPath(sensor.first);
2495 if (sensorTypeValue != ipmi::dcmi::temperatureSensorType)
2496 {
2497 continue;
2498 }
2499 const auto& connection = sensor.second.begin()->first;
2500
2501 DbusInterfaceMap sensorMap;
2502 if (!getSensorMap(ctx, connection, sensor.first, sensorMap,
2503 sensorMapSdrUpdatePeriod))
2504 {
2505 phosphor::logging::log<phosphor::logging::level::ERR>(
2506 "Failed to update sensor map for threshold sensor",
2507 phosphor::logging::entry("SERVICE=%s", connection.c_str()),
2508 phosphor::logging::entry("PATH=%s", sensor.first.c_str()));
2509 continue;
2510 }
2511 uint8_t entityIdValue = 0;
2512 uint8_t entityInstanceValue = 0;
Willy Tu0679e4b2022-11-11 14:34:33 -08002513 updateIpmiFromAssociation(
2514 sensor.first,
2515 ipmiDecoratorPaths.value_or(std::unordered_set<std::string>()),
2516 sensorMap, entityIdValue, entityInstanceValue);
adarshgrami042e9db2022-09-15 10:34:34 +05302517 if (!entityInstance)
2518 {
2519 if (entityIdValue == match->first || entityIdValue == match->second)
2520 {
2521 auto recordId = getSensorNumberFromPath(sensor.first);
2522 if (recordId != invalidSensorNumber)
2523 {
2524 numInstances++;
2525 if (numInstances <= ipmi::dcmi::maxRecords)
2526 {
2527 sensorRec.push_back(recordId);
2528 }
2529 }
2530 }
2531 }
2532 else
2533 {
2534 if (entityIdValue == match->first || entityIdValue == match->second)
2535 {
2536 if (entityInstance == entityInstanceValue)
2537 {
2538 auto recordId = getSensorNumberFromPath(sensor.first);
2539 if ((recordId != invalidSensorNumber) && sensorRec.empty())
2540 {
2541 sensorRec.push_back(recordId);
2542 }
2543 }
2544 numInstances++;
2545 }
2546 }
2547 }
2548 if (sensorRec.empty())
2549 {
2550 return ipmi::responseSensorInvalid();
2551 }
2552 uint8_t numRecords = sensorRec.size();
2553 return ipmi::responseSuccess(numInstances, numRecords, sensorRec);
2554}
2555} // namespace dcmi
2556
Willy Tude54f482021-01-26 15:59:09 -08002557/* end storage commands */
2558
2559void registerSensorFunctions()
2560{
2561 // <Platform Event>
2562 ipmi::registerHandler(ipmi::prioOpenBmcBase, ipmi::netFnSensor,
2563 ipmi::sensor_event::cmdPlatformEvent,
2564 ipmi::Privilege::Operator, ipmiSenPlatformEvent);
2565
Willy Tudbafbce2021-03-29 00:37:05 -07002566 // <Set Sensor Reading and Event Status>
2567 ipmi::registerHandler(ipmi::prioOpenBmcBase, ipmi::netFnSensor,
2568 ipmi::sensor_event::cmdSetSensorReadingAndEvtSts,
2569 ipmi::Privilege::Operator, ipmiSetSensorReading);
Willy Tudbafbce2021-03-29 00:37:05 -07002570
Willy Tude54f482021-01-26 15:59:09 -08002571 // <Get Sensor Reading>
2572 ipmi::registerHandler(ipmi::prioOpenBmcBase, ipmi::netFnSensor,
2573 ipmi::sensor_event::cmdGetSensorReading,
2574 ipmi::Privilege::User, ipmiSenGetSensorReading);
2575
2576 // <Get Sensor Threshold>
2577 ipmi::registerHandler(ipmi::prioOpenBmcBase, ipmi::netFnSensor,
2578 ipmi::sensor_event::cmdGetSensorThreshold,
2579 ipmi::Privilege::User, ipmiSenGetSensorThresholds);
2580
2581 // <Set Sensor Threshold>
2582 ipmi::registerHandler(ipmi::prioOpenBmcBase, ipmi::netFnSensor,
2583 ipmi::sensor_event::cmdSetSensorThreshold,
2584 ipmi::Privilege::Operator,
2585 ipmiSenSetSensorThresholds);
2586
2587 // <Get Sensor Event Enable>
2588 ipmi::registerHandler(ipmi::prioOpenBmcBase, ipmi::netFnSensor,
2589 ipmi::sensor_event::cmdGetSensorEventEnable,
2590 ipmi::Privilege::User, ipmiSenGetSensorEventEnable);
2591
2592 // <Get Sensor Event Status>
2593 ipmi::registerHandler(ipmi::prioOpenBmcBase, ipmi::netFnSensor,
2594 ipmi::sensor_event::cmdGetSensorEventStatus,
2595 ipmi::Privilege::User, ipmiSenGetSensorEventStatus);
2596
2597 // register all storage commands for both Sensor and Storage command
2598 // versions
2599
2600 // <Get SDR Repository Info>
2601 ipmi::registerHandler(ipmi::prioOpenBmcBase, ipmi::netFnStorage,
2602 ipmi::storage::cmdGetSdrRepositoryInfo,
2603 ipmi::Privilege::User,
2604 ipmiStorageGetSDRRepositoryInfo);
2605
2606 // <Get Device SDR Info>
2607 ipmi::registerHandler(ipmi::prioOpenBmcBase, ipmi::netFnSensor,
2608 ipmi::sensor_event::cmdGetDeviceSdrInfo,
2609 ipmi::Privilege::User, ipmiSensorGetDeviceSdrInfo);
2610
2611 // <Get SDR Allocation Info>
2612 ipmi::registerHandler(ipmi::prioOpenBmcBase, ipmi::netFnStorage,
2613 ipmi::storage::cmdGetSdrRepositoryAllocInfo,
2614 ipmi::Privilege::User,
2615 ipmiStorageGetSDRAllocationInfo);
2616
2617 // <Reserve SDR Repo>
2618 ipmi::registerHandler(ipmi::prioOpenBmcBase, ipmi::netFnSensor,
2619 ipmi::sensor_event::cmdReserveDeviceSdrRepository,
2620 ipmi::Privilege::User, ipmiStorageReserveSDR);
2621
2622 ipmi::registerHandler(ipmi::prioOpenBmcBase, ipmi::netFnStorage,
2623 ipmi::storage::cmdReserveSdrRepository,
2624 ipmi::Privilege::User, ipmiStorageReserveSDR);
2625
2626 // <Get Sdr>
2627 ipmi::registerHandler(ipmi::prioOpenBmcBase, ipmi::netFnSensor,
2628 ipmi::sensor_event::cmdGetDeviceSdr,
2629 ipmi::Privilege::User, ipmiStorageGetSDR);
2630
2631 ipmi::registerHandler(ipmi::prioOpenBmcBase, ipmi::netFnStorage,
2632 ipmi::storage::cmdGetSdr, ipmi::Privilege::User,
2633 ipmiStorageGetSDR);
adarshgrami042e9db2022-09-15 10:34:34 +05302634 // <Get DCMI Sensor Info>
2635 ipmi::registerGroupHandler(ipmi::prioOpenBmcBase, ipmi::groupDCMI,
2636 ipmi::dcmi::cmdGetDcmiSensorInfo,
2637 ipmi::Privilege::User,
2638 ipmi::dcmi::getSensorInfo);
Willy Tude54f482021-01-26 15:59:09 -08002639}
2640} // namespace ipmi