blob: 43ed82fd469f74f07728460f1f536263b245b945 [file] [log] [blame]
Patrick Venture46470a32018-09-07 19:26:25 -07001#pragma once
Chris Austenac4604a2015-10-13 12:43:27 -05002
Patrick Venture0b02be92018-08-31 11:55:55 -07003#include <stdint.h>
4
Brandon Kim9cf85622019-06-19 12:05:08 -07005#include <exception>
Vernon Mauerye08fbff2019-04-03 09:19:34 -07006#include <ipmid/api.hpp>
Vernon Mauery33250242019-03-12 16:49:26 -07007#include <ipmid/types.hpp>
8
Chris Austenac4604a2015-10-13 12:43:27 -05009// IPMI commands for net functions.
10enum ipmi_netfn_sen_cmds
11{
Jia, Chunhui3342a8e2018-12-29 13:32:26 +080012 IPMI_CMD_PLATFORM_EVENT = 0x2,
Tom Joseph5ca50952018-02-22 00:33:38 +053013 IPMI_CMD_GET_DEVICE_SDR_INFO = 0x20,
Patrick Venture0b02be92018-08-31 11:55:55 -070014 IPMI_CMD_GET_DEVICE_SDR = 0x21,
15 IPMI_CMD_RESERVE_DEVICE_SDR_REPO = 0x22,
Chris Austen10ccc0f2015-12-10 18:27:04 -060016 IPMI_CMD_GET_SENSOR_READING = 0x2D,
Patrick Venture0b02be92018-08-31 11:55:55 -070017 IPMI_CMD_GET_SENSOR_TYPE = 0x2F,
18 IPMI_CMD_SET_SENSOR = 0x30,
Dhruvaraj Subhashchandran5c0beec2018-01-23 04:47:06 -060019 IPMI_CMD_GET_SENSOR_THRESHOLDS = 0x27,
Chris Austenac4604a2015-10-13 12:43:27 -050020};
21
Ratan Guptae0cc8552018-01-22 14:23:04 +053022/**
23 * @enum device_type
24 * IPMI FRU device types
25 */
26enum device_type
27{
28 IPMI_PHYSICAL_FRU = 0x00,
29 IPMI_LOGICAL_FRU = 0x80,
30};
31
Emily Shaffer1fabf222017-04-05 08:53:21 -070032// Discrete sensor types.
33enum ipmi_sensor_types
34{
Patrick Venture0b02be92018-08-31 11:55:55 -070035 IPMI_SENSOR_TEMP = 0x01,
Emily Shaffer1fabf222017-04-05 08:53:21 -070036 IPMI_SENSOR_VOLTAGE = 0x02,
37 IPMI_SENSOR_CURRENT = 0x03,
Patrick Venture0b02be92018-08-31 11:55:55 -070038 IPMI_SENSOR_FAN = 0x04,
39 IPMI_SENSOR_TPM = 0xCC,
Emily Shaffer1fabf222017-04-05 08:53:21 -070040};
41
Brandon Kim9cf85622019-06-19 12:05:08 -070042/** @brief Custom exception for reading sensors that are not funcitonal.
43 */
44struct SensorFunctionalError : public std::exception
45{
46 const char* what() const noexcept
47 {
48 return "Sensor not functional";
49 }
50};
51
Chris Austen0012e9b2015-10-22 01:37:46 -050052#define MAX_DBUS_PATH 128
Patrick Venture0b02be92018-08-31 11:55:55 -070053struct dbus_interface_t
54{
55 uint8_t sensornumber;
56 uint8_t sensortype;
Chris Austen0012e9b2015-10-22 01:37:46 -050057
Patrick Venture0b02be92018-08-31 11:55:55 -070058 char bus[MAX_DBUS_PATH];
59 char path[MAX_DBUS_PATH];
60 char interface[MAX_DBUS_PATH];
Chris Austen0012e9b2015-10-22 01:37:46 -050061};
Tomd700e762016-09-20 18:24:13 +053062
Jia, Chunhui3342a8e2018-12-29 13:32:26 +080063struct PlatformEventRequest
64{
65 uint8_t eventMessageRevision;
66 uint8_t sensorType;
67 uint8_t sensorNumber;
68 uint8_t eventDirectionType;
69 uint8_t data[3];
70};
71
72static constexpr char const* ipmiSELPath = "/xyz/openbmc_project/Logging/IPMI";
73static constexpr char const* ipmiSELAddInterface =
74 "xyz.openbmc_project.Logging.IPMI";
75static const std::string ipmiSELAddMessage = "SEL Entry";
76
77static constexpr int selSystemEventSizeWith3Bytes = 8;
78static constexpr int selSystemEventSizeWith2Bytes = 7;
79static constexpr int selSystemEventSizeWith1Bytes = 6;
80static constexpr int selIPMBEventSize = 7;
81static constexpr uint8_t directionMask = 0x80;
82static constexpr uint8_t byte3EnableMask = 0x30;
83static constexpr uint8_t byte2EnableMask = 0xC0;
84
Patrick Venture0b02be92018-08-31 11:55:55 -070085int set_sensor_dbus_state_s(uint8_t, const char*, const char*);
86int set_sensor_dbus_state_y(uint8_t, const char*, const uint8_t);
87int find_openbmc_path(uint8_t, dbus_interface_t*);
Tom05732372016-09-06 17:21:23 +053088
Tom Joseph5ca50952018-02-22 00:33:38 +053089ipmi_ret_t ipmi_sen_get_sdr(ipmi_netfn_t netfn, ipmi_cmd_t cmd,
90 ipmi_request_t request, ipmi_response_t response,
91 ipmi_data_len_t data_len, ipmi_context_t context);
92
jayaprakash Mutyalad9578232019-05-13 20:22:50 +000093ipmi::RspType<uint16_t> ipmiSensorReserveSdr();
Tom Joseph5ca50952018-02-22 00:33:38 +053094
Ratan Guptae0cc8552018-01-22 14:23:04 +053095static const uint16_t FRU_RECORD_ID_START = 256;
Jaghathiswari Rankappagounder Natarajan9c118942019-02-12 13:22:55 -080096static const uint16_t ENTITY_RECORD_ID_START = 512;
Ratan Guptae0cc8552018-01-22 14:23:04 +053097static const uint8_t SDR_VERSION = 0x51;
98static const uint16_t END_OF_RECORD = 0xFFFF;
99static const uint8_t LENGTH_MASK = 0x1F;
100
Tom Josephbe703f72017-03-09 12:34:35 +0530101/**
Emily Shafferd06e0e72017-04-05 09:08:57 -0700102 * Get SDR Info
103 */
104
105namespace get_sdr_info
106{
107namespace request
108{
109// Note: for some reason the ipmi_request_t appears to be the
110// raw value for this call.
111inline bool get_count(void* req)
112{
Patrick Venture0b02be92018-08-31 11:55:55 -0700113 return (bool)((uint64_t)(req)&1);
Emily Shafferd06e0e72017-04-05 09:08:57 -0700114}
115} // namespace request
Emily Shafferd06e0e72017-04-05 09:08:57 -0700116} // namespace get_sdr_info
Emily Shafferbbef71c2017-05-08 16:36:17 -0700117
118/**
119 * Get SDR
120 */
121namespace get_sdr
122{
123
124struct GetSdrReq
125{
126 uint8_t reservation_id_lsb;
127 uint8_t reservation_id_msb;
128 uint8_t record_id_lsb;
129 uint8_t record_id_msb;
130 uint8_t offset;
131 uint8_t bytes_to_read;
132} __attribute__((packed));
133
134namespace request
135{
136
Emily Shaffere5c9d2f2019-06-25 14:26:45 -0700137inline uint16_t get_reservation_id(GetSdrReq* req)
Emily Shafferbbef71c2017-05-08 16:36:17 -0700138{
139 return (req->reservation_id_lsb + (req->reservation_id_msb << 8));
140};
141
Ratan Guptae0cc8552018-01-22 14:23:04 +0530142inline uint16_t get_record_id(GetSdrReq* req)
Emily Shafferbbef71c2017-05-08 16:36:17 -0700143{
144 return (req->record_id_lsb + (req->record_id_msb << 8));
145};
146
147} // namespace request
148
149// Response
150struct GetSdrResp
151{
152 uint8_t next_record_id_lsb;
153 uint8_t next_record_id_msb;
154 uint8_t record_data[64];
155} __attribute__((packed));
156
157namespace response
158{
159
Ratan Guptae0cc8552018-01-22 14:23:04 +0530160inline void set_next_record_id(uint16_t next, GetSdrResp* resp)
Emily Shafferbbef71c2017-05-08 16:36:17 -0700161{
162 resp->next_record_id_lsb = next & 0xff;
163 resp->next_record_id_msb = (next >> 8) & 0xff;
164};
165
166} // namespace response
167
168// Record header
169struct SensorDataRecordHeader
170{
171 uint8_t record_id_lsb;
172 uint8_t record_id_msb;
173 uint8_t sdr_version;
174 uint8_t record_type;
175 uint8_t record_length; // Length not counting the header
176} __attribute__((packed));
177
178namespace header
179{
180
181inline void set_record_id(int id, SensorDataRecordHeader* hdr)
182{
183 hdr->record_id_lsb = (id & 0xFF);
184 hdr->record_id_msb = (id >> 8) & 0xFF;
185};
186
187} // namespace header
188
189enum SensorDataRecordType
190{
Ratan Guptae0cc8552018-01-22 14:23:04 +0530191 SENSOR_DATA_FULL_RECORD = 0x1,
selvaganapathi7b2e5502023-02-14 07:10:47 +0530192 SENSOR_DATA_COMPACT_RECORD = 0x2,
Hao Jiangda06d152021-01-19 15:27:43 -0800193 SENSOR_DATA_EVENT_RECORD = 0x3,
Ratan Guptae0cc8552018-01-22 14:23:04 +0530194 SENSOR_DATA_FRU_RECORD = 0x11,
Jaghathiswari Rankappagounder Natarajan9c118942019-02-12 13:22:55 -0800195 SENSOR_DATA_ENTITY_RECORD = 0x8,
Emily Shafferbbef71c2017-05-08 16:36:17 -0700196};
197
198// Record key
199struct SensorDataRecordKey
200{
201 uint8_t owner_id;
202 uint8_t owner_lun;
203 uint8_t sensor_number;
204} __attribute__((packed));
205
Ratan Guptae0cc8552018-01-22 14:23:04 +0530206/** @struct SensorDataFruRecordKey
207 *
208 * FRU Device Locator Record(key) - SDR Type 11
209 */
210struct SensorDataFruRecordKey
211{
212 uint8_t deviceAddress;
213 uint8_t fruID;
214 uint8_t accessLun;
215 uint8_t channelNumber;
216} __attribute__((packed));
217
Jaghathiswari Rankappagounder Natarajan9c118942019-02-12 13:22:55 -0800218/** @struct SensorDataEntityRecordKey
219 *
220 * Entity Association Record(key) - SDR Type 8
221 */
222struct SensorDataEntityRecordKey
223{
224 uint8_t containerEntityId;
225 uint8_t containerEntityInstance;
226 uint8_t flags;
227 uint8_t entityId1;
228 uint8_t entityInstance1;
229} __attribute__((packed));
230
Emily Shafferbbef71c2017-05-08 16:36:17 -0700231namespace key
232{
233
Jaghathiswari Rankappagounder Natarajan9c118942019-02-12 13:22:55 -0800234static constexpr uint8_t listOrRangeBit = 7;
235static constexpr uint8_t linkedBit = 6;
236
Emily Shafferbbef71c2017-05-08 16:36:17 -0700237inline void set_owner_id_ipmb(SensorDataRecordKey* key)
238{
239 key->owner_id &= ~0x01;
240};
241
242inline void set_owner_id_system_sw(SensorDataRecordKey* key)
243{
244 key->owner_id |= 0x01;
245};
246
Tom Joseph96423912018-01-25 00:14:34 +0530247inline void set_owner_id_bmc(SensorDataRecordKey* key)
248{
249 key->owner_id |= 0x20;
250};
251
Emily Shafferbbef71c2017-05-08 16:36:17 -0700252inline void set_owner_id_address(uint8_t addr, SensorDataRecordKey* key)
253{
254 key->owner_id &= 0x01;
Patrick Venture0b02be92018-08-31 11:55:55 -0700255 key->owner_id |= addr << 1;
Emily Shafferbbef71c2017-05-08 16:36:17 -0700256};
257
258inline void set_owner_lun(uint8_t lun, SensorDataRecordKey* key)
259{
260 key->owner_lun &= ~0x03;
Patrick Venture0b02be92018-08-31 11:55:55 -0700261 key->owner_lun |= (lun & 0x03);
Emily Shafferbbef71c2017-05-08 16:36:17 -0700262};
263
264inline void set_owner_lun_channel(uint8_t channel, SensorDataRecordKey* key)
265{
266 key->owner_lun &= 0x0f;
Patrick Venture0b02be92018-08-31 11:55:55 -0700267 key->owner_lun |= ((channel & 0xf) << 4);
Emily Shafferbbef71c2017-05-08 16:36:17 -0700268};
269
Jaghathiswari Rankappagounder Natarajan9c118942019-02-12 13:22:55 -0800270inline void set_flags(bool isList, bool isLinked,
271 SensorDataEntityRecordKey* key)
272{
273 key->flags = 0x00;
274 if (!isList)
275 key->flags |= 1 << listOrRangeBit;
276
277 if (isLinked)
278 key->flags |= 1 << linkedBit;
279};
280
Emily Shafferbbef71c2017-05-08 16:36:17 -0700281} // namespace key
282
Dhruvaraj Subhashchandran5c0beec2018-01-23 04:47:06 -0600283/** @struct GetSensorThresholdsResponse
284 *
285 * Response structure for Get Sensor Thresholds command
286 */
287struct GetSensorThresholdsResponse
288{
Patrick Venture0b02be92018-08-31 11:55:55 -0700289 uint8_t validMask; //!< valid mask
290 uint8_t lowerNonCritical; //!< lower non-critical threshold
291 uint8_t lowerCritical; //!< lower critical threshold
292 uint8_t lowerNonRecoverable; //!< lower non-recoverable threshold
293 uint8_t upperNonCritical; //!< upper non-critical threshold
294 uint8_t upperCritical; //!< upper critical threshold
295 uint8_t upperNonRecoverable; //!< upper non-recoverable threshold
Dhruvaraj Subhashchandran5c0beec2018-01-23 04:47:06 -0600296} __attribute__((packed));
297
Emily Shafferbbef71c2017-05-08 16:36:17 -0700298// Body - full record
299#define FULL_RECORD_ID_STR_MAX_LENGTH 16
Ratan Guptae0cc8552018-01-22 14:23:04 +0530300
301static const int FRU_RECORD_DEVICE_ID_MAX_LENGTH = 16;
302
Emily Shafferbbef71c2017-05-08 16:36:17 -0700303struct SensorDataFullRecordBody
304{
305 uint8_t entity_id;
306 uint8_t entity_instance;
307 uint8_t sensor_initialization;
308 uint8_t sensor_capabilities; // no macro support
309 uint8_t sensor_type;
310 uint8_t event_reading_type;
Patrick Venture0b02be92018-08-31 11:55:55 -0700311 uint8_t supported_assertions[2]; // no macro support
312 uint8_t supported_deassertions[2]; // no macro support
Emily Shafferbbef71c2017-05-08 16:36:17 -0700313 uint8_t discrete_reading_setting_mask[2]; // no macro support
314 uint8_t sensor_units_1;
315 uint8_t sensor_units_2_base;
316 uint8_t sensor_units_3_modifier;
317 uint8_t linearization;
318 uint8_t m_lsb;
319 uint8_t m_msb_and_tolerance;
320 uint8_t b_lsb;
321 uint8_t b_msb_and_accuracy_lsb;
322 uint8_t accuracy_and_sensor_direction;
323 uint8_t r_b_exponents;
Patrick Venture0b02be92018-08-31 11:55:55 -0700324 uint8_t analog_characteristic_flags; // no macro support
Emily Shafferbbef71c2017-05-08 16:36:17 -0700325 uint8_t nominal_reading;
326 uint8_t normal_max;
327 uint8_t normal_min;
328 uint8_t sensor_max;
329 uint8_t sensor_min;
330 uint8_t upper_nonrecoverable_threshold;
331 uint8_t upper_critical_threshold;
332 uint8_t upper_noncritical_threshold;
333 uint8_t lower_nonrecoverable_threshold;
334 uint8_t lower_critical_threshold;
335 uint8_t lower_noncritical_threshold;
336 uint8_t positive_threshold_hysteresis;
337 uint8_t negative_threshold_hysteresis;
338 uint16_t reserved;
339 uint8_t oem_reserved;
340 uint8_t id_string_info;
Emily Shaffer10f49592017-05-10 12:01:10 -0700341 char id_string[FULL_RECORD_ID_STR_MAX_LENGTH];
Emily Shafferbbef71c2017-05-08 16:36:17 -0700342} __attribute__((packed));
343
selvaganapathi7b2e5502023-02-14 07:10:47 +0530344/** @struct SensorDataCompactRecord
345 *
346 * Compact Sensor Record(body) - SDR Type 2
347 */
348struct SensorDataCompactRecordBody
349{
350 uint8_t entity_id;
351 uint8_t entity_instance;
352 uint8_t sensor_initialization;
353 uint8_t sensor_capabilities; // no macro support
354 uint8_t sensor_type;
355 uint8_t event_reading_type;
356 uint8_t supported_assertions[2]; // no macro support
357 uint8_t supported_deassertions[2]; // no macro support
358 uint8_t discrete_reading_setting_mask[2]; // no macro support
359 uint8_t sensor_units_1;
360 uint8_t sensor_units_2_base;
361 uint8_t sensor_units_3_modifier;
362 uint8_t record_sharing[2];
363 uint8_t positive_threshold_hysteresis;
364 uint8_t negative_threshold_hysteresis;
365 uint8_t reserved[3];
366 uint8_t oem_reserved;
367 uint8_t id_string_info;
368 char id_string[FULL_RECORD_ID_STR_MAX_LENGTH];
369} __attribute__((packed));
370
Hao Jiangda06d152021-01-19 15:27:43 -0800371/** @struct SensorDataEventRecord
372 *
373 * Event Only Sensor Record(body) - SDR Type 3
374 */
375struct SensorDataEventRecordBody
376{
377 uint8_t entity_id;
378 uint8_t entity_instance;
379 uint8_t sensor_type;
380 uint8_t event_reading_type;
381 uint8_t sensor_record_sharing_1;
382 uint8_t sensor_record_sharing_2;
383 uint8_t reserved;
384 uint8_t oem_reserved;
385 uint8_t id_string_info;
386 char id_string[FULL_RECORD_ID_STR_MAX_LENGTH];
387} __attribute__((packed));
388
Ratan Guptae0cc8552018-01-22 14:23:04 +0530389/** @struct SensorDataFruRecordBody
390 *
391 * FRU Device Locator Record(body) - SDR Type 11
392 */
393struct SensorDataFruRecordBody
394{
395 uint8_t reserved;
396 uint8_t deviceType;
397 uint8_t deviceTypeModifier;
398 uint8_t entityID;
399 uint8_t entityInstance;
400 uint8_t oem;
401 uint8_t deviceIDLen;
402 char deviceID[FRU_RECORD_DEVICE_ID_MAX_LENGTH];
403} __attribute__((packed));
404
Jaghathiswari Rankappagounder Natarajan9c118942019-02-12 13:22:55 -0800405/** @struct SensorDataEntityRecordBody
406 *
407 * Entity Association Record(body) - SDR Type 8
408 */
409struct SensorDataEntityRecordBody
410{
411 uint8_t entityId2;
412 uint8_t entityInstance2;
413 uint8_t entityId3;
414 uint8_t entityInstance3;
415 uint8_t entityId4;
416 uint8_t entityInstance4;
417} __attribute__((packed));
418
Emily Shafferbbef71c2017-05-08 16:36:17 -0700419namespace body
420{
421
422inline void set_entity_instance_number(uint8_t n,
423 SensorDataFullRecordBody* body)
424{
Patrick Venture0b02be92018-08-31 11:55:55 -0700425 body->entity_instance &= 1 << 7;
426 body->entity_instance |= (n & ~(1 << 7));
Emily Shafferbbef71c2017-05-08 16:36:17 -0700427};
428inline void set_entity_physical_entity(SensorDataFullRecordBody* body)
429{
Patrick Venture0b02be92018-08-31 11:55:55 -0700430 body->entity_instance &= ~(1 << 7);
Emily Shafferbbef71c2017-05-08 16:36:17 -0700431};
432inline void set_entity_logical_container(SensorDataFullRecordBody* body)
433{
Patrick Venture0b02be92018-08-31 11:55:55 -0700434 body->entity_instance |= 1 << 7;
Emily Shafferbbef71c2017-05-08 16:36:17 -0700435};
436
Patrick Venture0b02be92018-08-31 11:55:55 -0700437inline void sensor_scanning_state(bool enabled, SensorDataFullRecordBody* body)
Emily Shafferbbef71c2017-05-08 16:36:17 -0700438{
439 if (enabled)
440 {
Patrick Venture0b02be92018-08-31 11:55:55 -0700441 body->sensor_initialization |= 1 << 0;
Emily Shafferbbef71c2017-05-08 16:36:17 -0700442 }
443 else
444 {
Patrick Venture0b02be92018-08-31 11:55:55 -0700445 body->sensor_initialization &= ~(1 << 0);
Emily Shafferbbef71c2017-05-08 16:36:17 -0700446 };
447};
Patrick Venture0b02be92018-08-31 11:55:55 -0700448inline void event_generation_state(bool enabled, SensorDataFullRecordBody* body)
Emily Shafferbbef71c2017-05-08 16:36:17 -0700449{
450 if (enabled)
451 {
Patrick Venture0b02be92018-08-31 11:55:55 -0700452 body->sensor_initialization |= 1 << 1;
Emily Shafferbbef71c2017-05-08 16:36:17 -0700453 }
454 else
455 {
Patrick Venture0b02be92018-08-31 11:55:55 -0700456 body->sensor_initialization &= ~(1 << 1);
Emily Shafferbbef71c2017-05-08 16:36:17 -0700457 }
458};
Patrick Venture0b02be92018-08-31 11:55:55 -0700459inline void init_types_state(bool enabled, SensorDataFullRecordBody* body)
Emily Shafferbbef71c2017-05-08 16:36:17 -0700460{
461 if (enabled)
462 {
Patrick Venture0b02be92018-08-31 11:55:55 -0700463 body->sensor_initialization |= 1 << 2;
Emily Shafferbbef71c2017-05-08 16:36:17 -0700464 }
465 else
466 {
Patrick Venture0b02be92018-08-31 11:55:55 -0700467 body->sensor_initialization &= ~(1 << 2);
Emily Shafferbbef71c2017-05-08 16:36:17 -0700468 }
469};
Patrick Venture0b02be92018-08-31 11:55:55 -0700470inline void init_hyst_state(bool enabled, SensorDataFullRecordBody* body)
Emily Shafferbbef71c2017-05-08 16:36:17 -0700471{
472 if (enabled)
473 {
Patrick Venture0b02be92018-08-31 11:55:55 -0700474 body->sensor_initialization |= 1 << 3;
Emily Shafferbbef71c2017-05-08 16:36:17 -0700475 }
476 else
477 {
Patrick Venture0b02be92018-08-31 11:55:55 -0700478 body->sensor_initialization &= ~(1 << 3);
Emily Shafferbbef71c2017-05-08 16:36:17 -0700479 }
480};
Patrick Venture0b02be92018-08-31 11:55:55 -0700481inline void init_thresh_state(bool enabled, SensorDataFullRecordBody* body)
Emily Shafferbbef71c2017-05-08 16:36:17 -0700482{
483 if (enabled)
484 {
Patrick Venture0b02be92018-08-31 11:55:55 -0700485 body->sensor_initialization |= 1 << 4;
Emily Shafferbbef71c2017-05-08 16:36:17 -0700486 }
487 else
488 {
Patrick Venture0b02be92018-08-31 11:55:55 -0700489 body->sensor_initialization &= ~(1 << 4);
Emily Shafferbbef71c2017-05-08 16:36:17 -0700490 }
491};
Patrick Venture0b02be92018-08-31 11:55:55 -0700492inline void init_events_state(bool enabled, SensorDataFullRecordBody* body)
Emily Shafferbbef71c2017-05-08 16:36:17 -0700493{
494 if (enabled)
495 {
Patrick Venture0b02be92018-08-31 11:55:55 -0700496 body->sensor_initialization |= 1 << 5;
Emily Shafferbbef71c2017-05-08 16:36:17 -0700497 }
498 else
499 {
Patrick Venture0b02be92018-08-31 11:55:55 -0700500 body->sensor_initialization &= ~(1 << 5);
Emily Shafferbbef71c2017-05-08 16:36:17 -0700501 }
502};
Patrick Venture0b02be92018-08-31 11:55:55 -0700503inline void init_scanning_state(bool enabled, SensorDataFullRecordBody* body)
Emily Shafferbbef71c2017-05-08 16:36:17 -0700504{
505 if (enabled)
506 {
Patrick Venture0b02be92018-08-31 11:55:55 -0700507 body->sensor_initialization |= 1 << 6;
Emily Shafferbbef71c2017-05-08 16:36:17 -0700508 }
509 else
510 {
Patrick Venture0b02be92018-08-31 11:55:55 -0700511 body->sensor_initialization &= ~(1 << 6);
Emily Shafferbbef71c2017-05-08 16:36:17 -0700512 }
513};
Patrick Venture0b02be92018-08-31 11:55:55 -0700514inline void init_settable_state(bool enabled, SensorDataFullRecordBody* body)
Emily Shafferbbef71c2017-05-08 16:36:17 -0700515{
516 if (enabled)
517 {
Patrick Venture0b02be92018-08-31 11:55:55 -0700518 body->sensor_initialization |= 1 << 7;
Emily Shafferbbef71c2017-05-08 16:36:17 -0700519 }
520 else
521 {
Patrick Venture0b02be92018-08-31 11:55:55 -0700522 body->sensor_initialization &= ~(1 << 7);
Emily Shafferbbef71c2017-05-08 16:36:17 -0700523 }
524};
525
526inline void set_percentage(SensorDataFullRecordBody* body)
527{
Patrick Venture0b02be92018-08-31 11:55:55 -0700528 body->sensor_units_1 |= 1 << 0;
Emily Shafferbbef71c2017-05-08 16:36:17 -0700529};
530inline void unset_percentage(SensorDataFullRecordBody* body)
531{
Patrick Venture0b02be92018-08-31 11:55:55 -0700532 body->sensor_units_1 &= ~(1 << 0);
Emily Shafferbbef71c2017-05-08 16:36:17 -0700533};
534inline void set_modifier_operation(uint8_t op, SensorDataFullRecordBody* body)
535{
Patrick Venture0b02be92018-08-31 11:55:55 -0700536 body->sensor_units_1 &= ~(3 << 1);
537 body->sensor_units_1 |= (op & 0x3) << 1;
Emily Shafferbbef71c2017-05-08 16:36:17 -0700538};
539inline void set_rate_unit(uint8_t unit, SensorDataFullRecordBody* body)
540{
Patrick Venture0b02be92018-08-31 11:55:55 -0700541 body->sensor_units_1 &= ~(7 << 3);
542 body->sensor_units_1 |= (unit & 0x7) << 3;
Emily Shafferbbef71c2017-05-08 16:36:17 -0700543};
544inline void set_analog_data_format(uint8_t format,
545 SensorDataFullRecordBody* body)
546{
Patrick Venture0b02be92018-08-31 11:55:55 -0700547 body->sensor_units_1 &= ~(3 << 6);
548 body->sensor_units_1 |= (format & 0x3) << 6;
Emily Shafferbbef71c2017-05-08 16:36:17 -0700549};
550
Emily Shaffer7cad3fc2017-08-30 17:50:37 -0700551inline void set_m(uint16_t m, SensorDataFullRecordBody* body)
Emily Shafferbbef71c2017-05-08 16:36:17 -0700552{
553 body->m_lsb = m & 0xff;
Patrick Venture0b02be92018-08-31 11:55:55 -0700554 body->m_msb_and_tolerance &= ~(3 << 6);
555 body->m_msb_and_tolerance |= ((m & (3 << 8)) >> 2);
Emily Shafferbbef71c2017-05-08 16:36:17 -0700556};
557inline void set_tolerance(uint8_t tol, SensorDataFullRecordBody* body)
558{
559 body->m_msb_and_tolerance &= ~0x3f;
560 body->m_msb_and_tolerance |= tol & 0x3f;
561};
562
Emily Shaffer7cad3fc2017-08-30 17:50:37 -0700563inline void set_b(uint16_t b, SensorDataFullRecordBody* body)
Emily Shafferbbef71c2017-05-08 16:36:17 -0700564{
565 body->b_lsb = b & 0xff;
Patrick Venture0b02be92018-08-31 11:55:55 -0700566 body->b_msb_and_accuracy_lsb &= ~(3 << 6);
567 body->b_msb_and_accuracy_lsb |= ((b & (3 << 8)) >> 2);
Emily Shafferbbef71c2017-05-08 16:36:17 -0700568};
Emily Shaffer7cad3fc2017-08-30 17:50:37 -0700569inline void set_accuracy(uint16_t acc, SensorDataFullRecordBody* body)
Emily Shafferbbef71c2017-05-08 16:36:17 -0700570{
Emily Shaffer7cad3fc2017-08-30 17:50:37 -0700571 // bottom 6 bits
Emily Shafferbbef71c2017-05-08 16:36:17 -0700572 body->b_msb_and_accuracy_lsb &= ~0x3f;
573 body->b_msb_and_accuracy_lsb |= acc & 0x3f;
Emily Shaffer7cad3fc2017-08-30 17:50:37 -0700574 // top 4 bits
Emily Shafferbbef71c2017-05-08 16:36:17 -0700575 body->accuracy_and_sensor_direction &= 0x0f;
Emily Shaffer7cad3fc2017-08-30 17:50:37 -0700576 body->accuracy_and_sensor_direction |= ((acc >> 6) & 0xf) << 4;
Emily Shafferbbef71c2017-05-08 16:36:17 -0700577};
578inline void set_accuracy_exp(uint8_t exp, SensorDataFullRecordBody* body)
579{
Patrick Venture0b02be92018-08-31 11:55:55 -0700580 body->accuracy_and_sensor_direction &= ~(3 << 2);
581 body->accuracy_and_sensor_direction |= (exp & 3) << 2;
Emily Shafferbbef71c2017-05-08 16:36:17 -0700582};
583inline void set_sensor_dir(uint8_t dir, SensorDataFullRecordBody* body)
584{
Patrick Venture0b02be92018-08-31 11:55:55 -0700585 body->accuracy_and_sensor_direction &= ~(3 << 0);
Emily Shafferbbef71c2017-05-08 16:36:17 -0700586 body->accuracy_and_sensor_direction |= (dir & 3);
587};
588
589inline void set_b_exp(uint8_t exp, SensorDataFullRecordBody* body)
590{
591 body->r_b_exponents &= 0xf0;
592 body->r_b_exponents |= exp & 0x0f;
593};
594inline void set_r_exp(uint8_t exp, SensorDataFullRecordBody* body)
595{
596 body->r_b_exponents &= 0x0f;
Patrick Venture0b02be92018-08-31 11:55:55 -0700597 body->r_b_exponents |= (exp & 0x0f) << 4;
Emily Shafferbbef71c2017-05-08 16:36:17 -0700598};
599
600inline void set_id_strlen(uint8_t len, SensorDataFullRecordBody* body)
601{
602 body->id_string_info &= ~(0x1f);
603 body->id_string_info |= len & 0x1f;
604};
Willy Tueae88592022-12-13 14:28:02 -0800605inline void set_id_strlen(uint8_t len, SensorDataEventRecordBody* body)
606{
607 body->id_string_info &= ~(0x1f);
608 body->id_string_info |= len & 0x1f;
609};
Patrick Venture0b02be92018-08-31 11:55:55 -0700610inline uint8_t get_id_strlen(SensorDataFullRecordBody* body)
Emily Shafferbbef71c2017-05-08 16:36:17 -0700611{
612 return body->id_string_info & 0x1f;
613};
614inline void set_id_type(uint8_t type, SensorDataFullRecordBody* body)
615{
Patrick Venture0b02be92018-08-31 11:55:55 -0700616 body->id_string_info &= ~(3 << 6);
617 body->id_string_info |= (type & 0x3) << 6;
Emily Shafferbbef71c2017-05-08 16:36:17 -0700618};
Willy Tueae88592022-12-13 14:28:02 -0800619inline void set_id_type(uint8_t type, SensorDataEventRecordBody* body)
620{
621 body->id_string_info &= ~(3 << 6);
622 body->id_string_info |= (type & 0x3) << 6;
623};
Emily Shafferbbef71c2017-05-08 16:36:17 -0700624
Ratan Guptae0cc8552018-01-22 14:23:04 +0530625inline void set_device_id_strlen(uint8_t len, SensorDataFruRecordBody* body)
626{
627 body->deviceIDLen &= ~(LENGTH_MASK);
628 body->deviceIDLen |= len & LENGTH_MASK;
629};
630
631inline uint8_t get_device_id_strlen(SensorDataFruRecordBody* body)
632{
633 return body->deviceIDLen & LENGTH_MASK;
634};
635
Tom Josephdc212b22018-02-16 09:59:57 +0530636inline void set_readable_mask(uint8_t mask, SensorDataFullRecordBody* body)
637{
638 body->discrete_reading_setting_mask[1] = mask & 0x3F;
639}
640
Emily Shafferbbef71c2017-05-08 16:36:17 -0700641} // namespace body
642
643// More types contained in section 43.17 Sensor Unit Type Codes,
644// IPMI spec v2 rev 1.1
645enum SensorUnitTypeCodes
646{
647 SENSOR_UNIT_UNSPECIFIED = 0,
648 SENSOR_UNIT_DEGREES_C = 1,
649 SENSOR_UNIT_VOLTS = 4,
650 SENSOR_UNIT_AMPERES = 5,
Tom Josephdc212b22018-02-16 09:59:57 +0530651 SENSOR_UNIT_WATTS = 6,
Emily Shafferbbef71c2017-05-08 16:36:17 -0700652 SENSOR_UNIT_JOULES = 7,
Kirill Pakhomov812e44c2018-10-22 16:25:35 +0300653 SENSOR_UNIT_RPM = 18,
Emily Shafferbbef71c2017-05-08 16:36:17 -0700654 SENSOR_UNIT_METERS = 34,
655 SENSOR_UNIT_REVOLUTIONS = 41,
656};
657
658struct SensorDataFullRecord
659{
660 SensorDataRecordHeader header;
661 SensorDataRecordKey key;
662 SensorDataFullRecordBody body;
663} __attribute__((packed));
664
selvaganapathi7b2e5502023-02-14 07:10:47 +0530665/** @struct SensorDataComapactRecord
666 *
667 * Compact Sensor Record - SDR Type 2
668 */
669struct SensorDataCompactRecord
670{
671 SensorDataRecordHeader header;
672 SensorDataRecordKey key;
673 SensorDataCompactRecordBody body;
674} __attribute__((packed));
675
Hao Jiangda06d152021-01-19 15:27:43 -0800676/** @struct SensorDataEventRecord
677 *
678 * Event Only Sensor Record - SDR Type 3
679 */
680struct SensorDataEventRecord
681{
682 SensorDataRecordHeader header;
683 SensorDataRecordKey key;
684 SensorDataEventRecordBody body;
685} __attribute__((packed));
686
Ratan Guptae0cc8552018-01-22 14:23:04 +0530687/** @struct SensorDataFruRecord
688 *
689 * FRU Device Locator Record - SDR Type 11
690 */
691struct SensorDataFruRecord
692{
693 SensorDataRecordHeader header;
694 SensorDataFruRecordKey key;
695 SensorDataFruRecordBody body;
696} __attribute__((packed));
697
Jaghathiswari Rankappagounder Natarajan9c118942019-02-12 13:22:55 -0800698/** @struct SensorDataEntityRecord
699 *
700 * Entity Association Record - SDR Type 8
701 */
702struct SensorDataEntityRecord
703{
704 SensorDataRecordHeader header;
705 SensorDataEntityRecordKey key;
706 SensorDataEntityRecordBody body;
707} __attribute__((packed));
708
Patrick Venture0b02be92018-08-31 11:55:55 -0700709} // namespace get_sdr
Tom Joseph816e92b2017-09-06 19:23:00 +0530710
711namespace ipmi
712{
713
714namespace sensor
715{
716
717/**
718 * @brief Map offset to the corresponding bit in the assertion byte.
719 *
Gunnar Mills8991dd62017-10-25 17:11:29 -0500720 * The discrete sensors support up to 14 states. 0-7 offsets are stored in one
Tom Joseph816e92b2017-09-06 19:23:00 +0530721 * byte and offsets 8-14 in the second byte.
722 *
723 * @param[in] offset - offset number.
724 * @param[in/out] resp - get sensor reading response.
725 */
Sui Chen4cc42552019-09-11 10:28:35 -0700726inline void setOffset(uint8_t offset, ipmi::sensor::GetSensorResponse* resp)
Tom Joseph816e92b2017-09-06 19:23:00 +0530727{
728 if (offset > 7)
729 {
Sui Chen4cc42552019-09-11 10:28:35 -0700730 resp->discreteReadingSensorStates |= 1 << (offset - 8);
Tom Joseph816e92b2017-09-06 19:23:00 +0530731 }
732 else
733 {
Sui Chen4cc42552019-09-11 10:28:35 -0700734 resp->thresholdLevelsStates |= 1 << offset;
Tom Joseph816e92b2017-09-06 19:23:00 +0530735 }
736}
737
Tom Josephe4014fc2017-09-06 23:57:36 +0530738/**
739 * @brief Set the reading field in the response.
740 *
741 * @param[in] offset - offset number.
742 * @param[in/out] resp - get sensor reading response.
743 */
Sui Chen4cc42552019-09-11 10:28:35 -0700744inline void setReading(uint8_t value, ipmi::sensor::GetSensorResponse* resp)
Tom Josephe4014fc2017-09-06 23:57:36 +0530745{
746 resp->reading = value;
747}
748
Tom Joseph295f17e2017-09-07 00:09:46 +0530749/**
750 * @brief Map the value to the assertion bytes. The assertion states are stored
751 * in 2 bytes.
752 *
753 * @param[in] value - value to mapped to the assertion byte.
754 * @param[in/out] resp - get sensor reading response.
755 */
756inline void setAssertionBytes(uint16_t value,
Sui Chen4cc42552019-09-11 10:28:35 -0700757 ipmi::sensor::GetSensorResponse* resp)
Tom Joseph295f17e2017-09-07 00:09:46 +0530758{
Sui Chen4cc42552019-09-11 10:28:35 -0700759 resp->thresholdLevelsStates = static_cast<uint8_t>(value & 0x00FF);
760 resp->discreteReadingSensorStates = static_cast<uint8_t>(value >> 8);
Tom Joseph295f17e2017-09-07 00:09:46 +0530761}
762
Tom Josephe05b2922017-09-07 00:43:16 +0530763/**
764 * @brief Set the scanning enabled bit in the response.
765 *
766 * @param[in/out] resp - get sensor reading response.
767 */
Sui Chen4cc42552019-09-11 10:28:35 -0700768inline void enableScanning(ipmi::sensor::GetSensorResponse* resp)
Tom Josephe05b2922017-09-07 00:43:16 +0530769{
Sui Chen4cc42552019-09-11 10:28:35 -0700770 resp->readingOrStateUnavailable = false;
771 resp->scanningEnabled = true;
772 resp->allEventMessagesEnabled = false;
Tom Josephe05b2922017-09-07 00:43:16 +0530773}
774
Tom Joseph816e92b2017-09-06 19:23:00 +0530775} // namespace sensor
776
777} // namespace ipmi