xref: /openbmc/phosphor-host-ipmid/sensorhandler.hpp (revision 812e44c7b6ff1e25b8cf79e8595db86bae7a98a5)
146470a38SPatrick Venture #pragma once
246470a38SPatrick Venture 
346470a38SPatrick Venture #include "types.hpp"
446470a38SPatrick Venture 
546470a38SPatrick Venture #include <host-ipmid/ipmid-api.h>
646470a38SPatrick Venture #include <stdint.h>
746470a38SPatrick Venture 
846470a38SPatrick Venture // IPMI commands for net functions.
946470a38SPatrick Venture enum ipmi_netfn_sen_cmds
1046470a38SPatrick Venture {
1146470a38SPatrick Venture     IPMI_CMD_GET_DEVICE_SDR_INFO = 0x20,
1246470a38SPatrick Venture     IPMI_CMD_GET_DEVICE_SDR = 0x21,
1346470a38SPatrick Venture     IPMI_CMD_RESERVE_DEVICE_SDR_REPO = 0x22,
1446470a38SPatrick Venture     IPMI_CMD_GET_SENSOR_READING = 0x2D,
1546470a38SPatrick Venture     IPMI_CMD_GET_SENSOR_TYPE = 0x2F,
1646470a38SPatrick Venture     IPMI_CMD_SET_SENSOR = 0x30,
1746470a38SPatrick Venture     IPMI_CMD_GET_SENSOR_THRESHOLDS = 0x27,
1846470a38SPatrick Venture };
1946470a38SPatrick Venture 
2046470a38SPatrick Venture /**
2146470a38SPatrick Venture  * @enum device_type
2246470a38SPatrick Venture  * IPMI FRU device types
2346470a38SPatrick Venture  */
2446470a38SPatrick Venture enum device_type
2546470a38SPatrick Venture {
2646470a38SPatrick Venture     IPMI_PHYSICAL_FRU = 0x00,
2746470a38SPatrick Venture     IPMI_LOGICAL_FRU = 0x80,
2846470a38SPatrick Venture };
2946470a38SPatrick Venture 
3046470a38SPatrick Venture // Discrete sensor types.
3146470a38SPatrick Venture enum ipmi_sensor_types
3246470a38SPatrick Venture {
3346470a38SPatrick Venture     IPMI_SENSOR_TEMP = 0x01,
3446470a38SPatrick Venture     IPMI_SENSOR_VOLTAGE = 0x02,
3546470a38SPatrick Venture     IPMI_SENSOR_CURRENT = 0x03,
3646470a38SPatrick Venture     IPMI_SENSOR_FAN = 0x04,
3746470a38SPatrick Venture     IPMI_SENSOR_TPM = 0xCC,
3846470a38SPatrick Venture };
3946470a38SPatrick Venture 
4046470a38SPatrick Venture #define MAX_DBUS_PATH 128
4146470a38SPatrick Venture struct dbus_interface_t
4246470a38SPatrick Venture {
4346470a38SPatrick Venture     uint8_t sensornumber;
4446470a38SPatrick Venture     uint8_t sensortype;
4546470a38SPatrick Venture 
4646470a38SPatrick Venture     char bus[MAX_DBUS_PATH];
4746470a38SPatrick Venture     char path[MAX_DBUS_PATH];
4846470a38SPatrick Venture     char interface[MAX_DBUS_PATH];
4946470a38SPatrick Venture };
5046470a38SPatrick Venture 
5146470a38SPatrick Venture int set_sensor_dbus_state_s(uint8_t, const char*, const char*);
5246470a38SPatrick Venture int set_sensor_dbus_state_y(uint8_t, const char*, const uint8_t);
5346470a38SPatrick Venture int find_openbmc_path(uint8_t, dbus_interface_t*);
5446470a38SPatrick Venture 
5546470a38SPatrick Venture ipmi_ret_t ipmi_sen_get_sdr(ipmi_netfn_t netfn, ipmi_cmd_t cmd,
5646470a38SPatrick Venture                             ipmi_request_t request, ipmi_response_t response,
5746470a38SPatrick Venture                             ipmi_data_len_t data_len, ipmi_context_t context);
5846470a38SPatrick Venture 
5946470a38SPatrick Venture ipmi_ret_t ipmi_sen_reserve_sdr(ipmi_netfn_t netfn, ipmi_cmd_t cmd,
6046470a38SPatrick Venture                                 ipmi_request_t request,
6146470a38SPatrick Venture                                 ipmi_response_t response,
6246470a38SPatrick Venture                                 ipmi_data_len_t data_len,
6346470a38SPatrick Venture                                 ipmi_context_t context);
6446470a38SPatrick Venture 
6546470a38SPatrick Venture static const uint16_t FRU_RECORD_ID_START = 256;
6646470a38SPatrick Venture static const uint8_t SDR_VERSION = 0x51;
6746470a38SPatrick Venture static const uint16_t END_OF_RECORD = 0xFFFF;
6846470a38SPatrick Venture static const uint8_t LENGTH_MASK = 0x1F;
6946470a38SPatrick Venture 
7046470a38SPatrick Venture /**
7146470a38SPatrick Venture  * Get SDR Info
7246470a38SPatrick Venture  */
7346470a38SPatrick Venture 
7446470a38SPatrick Venture namespace get_sdr_info
7546470a38SPatrick Venture {
7646470a38SPatrick Venture namespace request
7746470a38SPatrick Venture {
7846470a38SPatrick Venture // Note: for some reason the ipmi_request_t appears to be the
7946470a38SPatrick Venture // raw value for this call.
8046470a38SPatrick Venture inline bool get_count(void* req)
8146470a38SPatrick Venture {
8246470a38SPatrick Venture     return (bool)((uint64_t)(req)&1);
8346470a38SPatrick Venture }
8446470a38SPatrick Venture } // namespace request
8546470a38SPatrick Venture 
8646470a38SPatrick Venture namespace response
8746470a38SPatrick Venture {
8846470a38SPatrick Venture #define SDR_INFO_RESP_SIZE 2
8946470a38SPatrick Venture inline void set_lun_present(int lun, uint8_t* resp)
9046470a38SPatrick Venture {
9146470a38SPatrick Venture     *resp |= 1 << lun;
9246470a38SPatrick Venture }
9346470a38SPatrick Venture inline void set_lun_not_present(int lun, uint8_t* resp)
9446470a38SPatrick Venture {
9546470a38SPatrick Venture     *resp &= ~(1 << lun);
9646470a38SPatrick Venture }
9746470a38SPatrick Venture inline void set_dynamic_population(uint8_t* resp)
9846470a38SPatrick Venture {
9946470a38SPatrick Venture     *resp |= 1 << 7;
10046470a38SPatrick Venture }
10146470a38SPatrick Venture inline void set_static_population(uint8_t* resp)
10246470a38SPatrick Venture {
10346470a38SPatrick Venture     *resp &= ~(1 << 7);
10446470a38SPatrick Venture }
10546470a38SPatrick Venture } // namespace response
10646470a38SPatrick Venture 
10746470a38SPatrick Venture struct GetSdrInfoResp
10846470a38SPatrick Venture {
10946470a38SPatrick Venture     uint8_t count;
11046470a38SPatrick Venture     uint8_t luns_and_dynamic_population;
11146470a38SPatrick Venture };
11246470a38SPatrick Venture 
11346470a38SPatrick Venture } // namespace get_sdr_info
11446470a38SPatrick Venture 
11546470a38SPatrick Venture /**
11646470a38SPatrick Venture  * Get SDR
11746470a38SPatrick Venture  */
11846470a38SPatrick Venture namespace get_sdr
11946470a38SPatrick Venture {
12046470a38SPatrick Venture 
12146470a38SPatrick Venture struct GetSdrReq
12246470a38SPatrick Venture {
12346470a38SPatrick Venture     uint8_t reservation_id_lsb;
12446470a38SPatrick Venture     uint8_t reservation_id_msb;
12546470a38SPatrick Venture     uint8_t record_id_lsb;
12646470a38SPatrick Venture     uint8_t record_id_msb;
12746470a38SPatrick Venture     uint8_t offset;
12846470a38SPatrick Venture     uint8_t bytes_to_read;
12946470a38SPatrick Venture } __attribute__((packed));
13046470a38SPatrick Venture 
13146470a38SPatrick Venture namespace request
13246470a38SPatrick Venture {
13346470a38SPatrick Venture 
13446470a38SPatrick Venture inline uint8_t get_reservation_id(GetSdrReq* req)
13546470a38SPatrick Venture {
13646470a38SPatrick Venture     return (req->reservation_id_lsb + (req->reservation_id_msb << 8));
13746470a38SPatrick Venture };
13846470a38SPatrick Venture 
13946470a38SPatrick Venture inline uint16_t get_record_id(GetSdrReq* req)
14046470a38SPatrick Venture {
14146470a38SPatrick Venture     return (req->record_id_lsb + (req->record_id_msb << 8));
14246470a38SPatrick Venture };
14346470a38SPatrick Venture 
14446470a38SPatrick Venture } // namespace request
14546470a38SPatrick Venture 
14646470a38SPatrick Venture // Response
14746470a38SPatrick Venture struct GetSdrResp
14846470a38SPatrick Venture {
14946470a38SPatrick Venture     uint8_t next_record_id_lsb;
15046470a38SPatrick Venture     uint8_t next_record_id_msb;
15146470a38SPatrick Venture     uint8_t record_data[64];
15246470a38SPatrick Venture } __attribute__((packed));
15346470a38SPatrick Venture 
15446470a38SPatrick Venture namespace response
15546470a38SPatrick Venture {
15646470a38SPatrick Venture 
15746470a38SPatrick Venture inline void set_next_record_id(uint16_t next, GetSdrResp* resp)
15846470a38SPatrick Venture {
15946470a38SPatrick Venture     resp->next_record_id_lsb = next & 0xff;
16046470a38SPatrick Venture     resp->next_record_id_msb = (next >> 8) & 0xff;
16146470a38SPatrick Venture };
16246470a38SPatrick Venture 
16346470a38SPatrick Venture } // namespace response
16446470a38SPatrick Venture 
16546470a38SPatrick Venture // Record header
16646470a38SPatrick Venture struct SensorDataRecordHeader
16746470a38SPatrick Venture {
16846470a38SPatrick Venture     uint8_t record_id_lsb;
16946470a38SPatrick Venture     uint8_t record_id_msb;
17046470a38SPatrick Venture     uint8_t sdr_version;
17146470a38SPatrick Venture     uint8_t record_type;
17246470a38SPatrick Venture     uint8_t record_length; // Length not counting the header
17346470a38SPatrick Venture } __attribute__((packed));
17446470a38SPatrick Venture 
17546470a38SPatrick Venture namespace header
17646470a38SPatrick Venture {
17746470a38SPatrick Venture 
17846470a38SPatrick Venture inline void set_record_id(int id, SensorDataRecordHeader* hdr)
17946470a38SPatrick Venture {
18046470a38SPatrick Venture     hdr->record_id_lsb = (id & 0xFF);
18146470a38SPatrick Venture     hdr->record_id_msb = (id >> 8) & 0xFF;
18246470a38SPatrick Venture };
18346470a38SPatrick Venture 
18446470a38SPatrick Venture } // namespace header
18546470a38SPatrick Venture 
18646470a38SPatrick Venture enum SensorDataRecordType
18746470a38SPatrick Venture {
18846470a38SPatrick Venture     SENSOR_DATA_FULL_RECORD = 0x1,
18946470a38SPatrick Venture     SENSOR_DATA_FRU_RECORD = 0x11,
19046470a38SPatrick Venture };
19146470a38SPatrick Venture 
19246470a38SPatrick Venture // Record key
19346470a38SPatrick Venture struct SensorDataRecordKey
19446470a38SPatrick Venture {
19546470a38SPatrick Venture     uint8_t owner_id;
19646470a38SPatrick Venture     uint8_t owner_lun;
19746470a38SPatrick Venture     uint8_t sensor_number;
19846470a38SPatrick Venture } __attribute__((packed));
19946470a38SPatrick Venture 
20046470a38SPatrick Venture /** @struct SensorDataFruRecordKey
20146470a38SPatrick Venture  *
20246470a38SPatrick Venture  *  FRU Device Locator Record(key) - SDR Type 11
20346470a38SPatrick Venture  */
20446470a38SPatrick Venture struct SensorDataFruRecordKey
20546470a38SPatrick Venture {
20646470a38SPatrick Venture     uint8_t deviceAddress;
20746470a38SPatrick Venture     uint8_t fruID;
20846470a38SPatrick Venture     uint8_t accessLun;
20946470a38SPatrick Venture     uint8_t channelNumber;
21046470a38SPatrick Venture } __attribute__((packed));
21146470a38SPatrick Venture 
21246470a38SPatrick Venture namespace key
21346470a38SPatrick Venture {
21446470a38SPatrick Venture 
21546470a38SPatrick Venture inline void set_owner_id_ipmb(SensorDataRecordKey* key)
21646470a38SPatrick Venture {
21746470a38SPatrick Venture     key->owner_id &= ~0x01;
21846470a38SPatrick Venture };
21946470a38SPatrick Venture 
22046470a38SPatrick Venture inline void set_owner_id_system_sw(SensorDataRecordKey* key)
22146470a38SPatrick Venture {
22246470a38SPatrick Venture     key->owner_id |= 0x01;
22346470a38SPatrick Venture };
22446470a38SPatrick Venture 
22546470a38SPatrick Venture inline void set_owner_id_bmc(SensorDataRecordKey* key)
22646470a38SPatrick Venture {
22746470a38SPatrick Venture     key->owner_id |= 0x20;
22846470a38SPatrick Venture };
22946470a38SPatrick Venture 
23046470a38SPatrick Venture inline void set_owner_id_address(uint8_t addr, SensorDataRecordKey* key)
23146470a38SPatrick Venture {
23246470a38SPatrick Venture     key->owner_id &= 0x01;
23346470a38SPatrick Venture     key->owner_id |= addr << 1;
23446470a38SPatrick Venture };
23546470a38SPatrick Venture 
23646470a38SPatrick Venture inline void set_owner_lun(uint8_t lun, SensorDataRecordKey* key)
23746470a38SPatrick Venture {
23846470a38SPatrick Venture     key->owner_lun &= ~0x03;
23946470a38SPatrick Venture     key->owner_lun |= (lun & 0x03);
24046470a38SPatrick Venture };
24146470a38SPatrick Venture 
24246470a38SPatrick Venture inline void set_owner_lun_channel(uint8_t channel, SensorDataRecordKey* key)
24346470a38SPatrick Venture {
24446470a38SPatrick Venture     key->owner_lun &= 0x0f;
24546470a38SPatrick Venture     key->owner_lun |= ((channel & 0xf) << 4);
24646470a38SPatrick Venture };
24746470a38SPatrick Venture 
24846470a38SPatrick Venture } // namespace key
24946470a38SPatrick Venture 
25046470a38SPatrick Venture /** @struct GetSensorThresholdsResponse
25146470a38SPatrick Venture  *
25246470a38SPatrick Venture  *  Response structure for Get Sensor Thresholds command
25346470a38SPatrick Venture  */
25446470a38SPatrick Venture struct GetSensorThresholdsResponse
25546470a38SPatrick Venture {
25646470a38SPatrick Venture     uint8_t validMask;           //!< valid mask
25746470a38SPatrick Venture     uint8_t lowerNonCritical;    //!< lower non-critical threshold
25846470a38SPatrick Venture     uint8_t lowerCritical;       //!< lower critical threshold
25946470a38SPatrick Venture     uint8_t lowerNonRecoverable; //!< lower non-recoverable threshold
26046470a38SPatrick Venture     uint8_t upperNonCritical;    //!< upper non-critical threshold
26146470a38SPatrick Venture     uint8_t upperCritical;       //!< upper critical threshold
26246470a38SPatrick Venture     uint8_t upperNonRecoverable; //!< upper non-recoverable threshold
26346470a38SPatrick Venture } __attribute__((packed));
26446470a38SPatrick Venture 
26546470a38SPatrick Venture // Body - full record
26646470a38SPatrick Venture #define FULL_RECORD_ID_STR_MAX_LENGTH 16
26746470a38SPatrick Venture 
26846470a38SPatrick Venture static const int FRU_RECORD_DEVICE_ID_MAX_LENGTH = 16;
26946470a38SPatrick Venture 
27046470a38SPatrick Venture struct SensorDataFullRecordBody
27146470a38SPatrick Venture {
27246470a38SPatrick Venture     uint8_t entity_id;
27346470a38SPatrick Venture     uint8_t entity_instance;
27446470a38SPatrick Venture     uint8_t sensor_initialization;
27546470a38SPatrick Venture     uint8_t sensor_capabilities; // no macro support
27646470a38SPatrick Venture     uint8_t sensor_type;
27746470a38SPatrick Venture     uint8_t event_reading_type;
27846470a38SPatrick Venture     uint8_t supported_assertions[2];          // no macro support
27946470a38SPatrick Venture     uint8_t supported_deassertions[2];        // no macro support
28046470a38SPatrick Venture     uint8_t discrete_reading_setting_mask[2]; // no macro support
28146470a38SPatrick Venture     uint8_t sensor_units_1;
28246470a38SPatrick Venture     uint8_t sensor_units_2_base;
28346470a38SPatrick Venture     uint8_t sensor_units_3_modifier;
28446470a38SPatrick Venture     uint8_t linearization;
28546470a38SPatrick Venture     uint8_t m_lsb;
28646470a38SPatrick Venture     uint8_t m_msb_and_tolerance;
28746470a38SPatrick Venture     uint8_t b_lsb;
28846470a38SPatrick Venture     uint8_t b_msb_and_accuracy_lsb;
28946470a38SPatrick Venture     uint8_t accuracy_and_sensor_direction;
29046470a38SPatrick Venture     uint8_t r_b_exponents;
29146470a38SPatrick Venture     uint8_t analog_characteristic_flags; // no macro support
29246470a38SPatrick Venture     uint8_t nominal_reading;
29346470a38SPatrick Venture     uint8_t normal_max;
29446470a38SPatrick Venture     uint8_t normal_min;
29546470a38SPatrick Venture     uint8_t sensor_max;
29646470a38SPatrick Venture     uint8_t sensor_min;
29746470a38SPatrick Venture     uint8_t upper_nonrecoverable_threshold;
29846470a38SPatrick Venture     uint8_t upper_critical_threshold;
29946470a38SPatrick Venture     uint8_t upper_noncritical_threshold;
30046470a38SPatrick Venture     uint8_t lower_nonrecoverable_threshold;
30146470a38SPatrick Venture     uint8_t lower_critical_threshold;
30246470a38SPatrick Venture     uint8_t lower_noncritical_threshold;
30346470a38SPatrick Venture     uint8_t positive_threshold_hysteresis;
30446470a38SPatrick Venture     uint8_t negative_threshold_hysteresis;
30546470a38SPatrick Venture     uint16_t reserved;
30646470a38SPatrick Venture     uint8_t oem_reserved;
30746470a38SPatrick Venture     uint8_t id_string_info;
30846470a38SPatrick Venture     char id_string[FULL_RECORD_ID_STR_MAX_LENGTH];
30946470a38SPatrick Venture } __attribute__((packed));
31046470a38SPatrick Venture 
31146470a38SPatrick Venture /** @struct SensorDataFruRecordBody
31246470a38SPatrick Venture  *
31346470a38SPatrick Venture  *  FRU Device Locator Record(body) - SDR Type 11
31446470a38SPatrick Venture  */
31546470a38SPatrick Venture struct SensorDataFruRecordBody
31646470a38SPatrick Venture {
31746470a38SPatrick Venture     uint8_t reserved;
31846470a38SPatrick Venture     uint8_t deviceType;
31946470a38SPatrick Venture     uint8_t deviceTypeModifier;
32046470a38SPatrick Venture     uint8_t entityID;
32146470a38SPatrick Venture     uint8_t entityInstance;
32246470a38SPatrick Venture     uint8_t oem;
32346470a38SPatrick Venture     uint8_t deviceIDLen;
32446470a38SPatrick Venture     char deviceID[FRU_RECORD_DEVICE_ID_MAX_LENGTH];
32546470a38SPatrick Venture } __attribute__((packed));
32646470a38SPatrick Venture 
32746470a38SPatrick Venture namespace body
32846470a38SPatrick Venture {
32946470a38SPatrick Venture 
33046470a38SPatrick Venture inline void set_entity_instance_number(uint8_t n,
33146470a38SPatrick Venture                                        SensorDataFullRecordBody* body)
33246470a38SPatrick Venture {
33346470a38SPatrick Venture     body->entity_instance &= 1 << 7;
33446470a38SPatrick Venture     body->entity_instance |= (n & ~(1 << 7));
33546470a38SPatrick Venture };
33646470a38SPatrick Venture inline void set_entity_physical_entity(SensorDataFullRecordBody* body)
33746470a38SPatrick Venture {
33846470a38SPatrick Venture     body->entity_instance &= ~(1 << 7);
33946470a38SPatrick Venture };
34046470a38SPatrick Venture inline void set_entity_logical_container(SensorDataFullRecordBody* body)
34146470a38SPatrick Venture {
34246470a38SPatrick Venture     body->entity_instance |= 1 << 7;
34346470a38SPatrick Venture };
34446470a38SPatrick Venture 
34546470a38SPatrick Venture inline void sensor_scanning_state(bool enabled, SensorDataFullRecordBody* body)
34646470a38SPatrick Venture {
34746470a38SPatrick Venture     if (enabled)
34846470a38SPatrick Venture     {
34946470a38SPatrick Venture         body->sensor_initialization |= 1 << 0;
35046470a38SPatrick Venture     }
35146470a38SPatrick Venture     else
35246470a38SPatrick Venture     {
35346470a38SPatrick Venture         body->sensor_initialization &= ~(1 << 0);
35446470a38SPatrick Venture     };
35546470a38SPatrick Venture };
35646470a38SPatrick Venture inline void event_generation_state(bool enabled, SensorDataFullRecordBody* body)
35746470a38SPatrick Venture {
35846470a38SPatrick Venture     if (enabled)
35946470a38SPatrick Venture     {
36046470a38SPatrick Venture         body->sensor_initialization |= 1 << 1;
36146470a38SPatrick Venture     }
36246470a38SPatrick Venture     else
36346470a38SPatrick Venture     {
36446470a38SPatrick Venture         body->sensor_initialization &= ~(1 << 1);
36546470a38SPatrick Venture     }
36646470a38SPatrick Venture };
36746470a38SPatrick Venture inline void init_types_state(bool enabled, SensorDataFullRecordBody* body)
36846470a38SPatrick Venture {
36946470a38SPatrick Venture     if (enabled)
37046470a38SPatrick Venture     {
37146470a38SPatrick Venture         body->sensor_initialization |= 1 << 2;
37246470a38SPatrick Venture     }
37346470a38SPatrick Venture     else
37446470a38SPatrick Venture     {
37546470a38SPatrick Venture         body->sensor_initialization &= ~(1 << 2);
37646470a38SPatrick Venture     }
37746470a38SPatrick Venture };
37846470a38SPatrick Venture inline void init_hyst_state(bool enabled, SensorDataFullRecordBody* body)
37946470a38SPatrick Venture {
38046470a38SPatrick Venture     if (enabled)
38146470a38SPatrick Venture     {
38246470a38SPatrick Venture         body->sensor_initialization |= 1 << 3;
38346470a38SPatrick Venture     }
38446470a38SPatrick Venture     else
38546470a38SPatrick Venture     {
38646470a38SPatrick Venture         body->sensor_initialization &= ~(1 << 3);
38746470a38SPatrick Venture     }
38846470a38SPatrick Venture };
38946470a38SPatrick Venture inline void init_thresh_state(bool enabled, SensorDataFullRecordBody* body)
39046470a38SPatrick Venture {
39146470a38SPatrick Venture     if (enabled)
39246470a38SPatrick Venture     {
39346470a38SPatrick Venture         body->sensor_initialization |= 1 << 4;
39446470a38SPatrick Venture     }
39546470a38SPatrick Venture     else
39646470a38SPatrick Venture     {
39746470a38SPatrick Venture         body->sensor_initialization &= ~(1 << 4);
39846470a38SPatrick Venture     }
39946470a38SPatrick Venture };
40046470a38SPatrick Venture inline void init_events_state(bool enabled, SensorDataFullRecordBody* body)
40146470a38SPatrick Venture {
40246470a38SPatrick Venture     if (enabled)
40346470a38SPatrick Venture     {
40446470a38SPatrick Venture         body->sensor_initialization |= 1 << 5;
40546470a38SPatrick Venture     }
40646470a38SPatrick Venture     else
40746470a38SPatrick Venture     {
40846470a38SPatrick Venture         body->sensor_initialization &= ~(1 << 5);
40946470a38SPatrick Venture     }
41046470a38SPatrick Venture };
41146470a38SPatrick Venture inline void init_scanning_state(bool enabled, SensorDataFullRecordBody* body)
41246470a38SPatrick Venture {
41346470a38SPatrick Venture     if (enabled)
41446470a38SPatrick Venture     {
41546470a38SPatrick Venture         body->sensor_initialization |= 1 << 6;
41646470a38SPatrick Venture     }
41746470a38SPatrick Venture     else
41846470a38SPatrick Venture     {
41946470a38SPatrick Venture         body->sensor_initialization &= ~(1 << 6);
42046470a38SPatrick Venture     }
42146470a38SPatrick Venture };
42246470a38SPatrick Venture inline void init_settable_state(bool enabled, SensorDataFullRecordBody* body)
42346470a38SPatrick Venture {
42446470a38SPatrick Venture     if (enabled)
42546470a38SPatrick Venture     {
42646470a38SPatrick Venture         body->sensor_initialization |= 1 << 7;
42746470a38SPatrick Venture     }
42846470a38SPatrick Venture     else
42946470a38SPatrick Venture     {
43046470a38SPatrick Venture         body->sensor_initialization &= ~(1 << 7);
43146470a38SPatrick Venture     }
43246470a38SPatrick Venture };
43346470a38SPatrick Venture 
43446470a38SPatrick Venture inline void set_percentage(SensorDataFullRecordBody* body)
43546470a38SPatrick Venture {
43646470a38SPatrick Venture     body->sensor_units_1 |= 1 << 0;
43746470a38SPatrick Venture };
43846470a38SPatrick Venture inline void unset_percentage(SensorDataFullRecordBody* body)
43946470a38SPatrick Venture {
44046470a38SPatrick Venture     body->sensor_units_1 &= ~(1 << 0);
44146470a38SPatrick Venture };
44246470a38SPatrick Venture inline void set_modifier_operation(uint8_t op, SensorDataFullRecordBody* body)
44346470a38SPatrick Venture {
44446470a38SPatrick Venture     body->sensor_units_1 &= ~(3 << 1);
44546470a38SPatrick Venture     body->sensor_units_1 |= (op & 0x3) << 1;
44646470a38SPatrick Venture };
44746470a38SPatrick Venture inline void set_rate_unit(uint8_t unit, SensorDataFullRecordBody* body)
44846470a38SPatrick Venture {
44946470a38SPatrick Venture     body->sensor_units_1 &= ~(7 << 3);
45046470a38SPatrick Venture     body->sensor_units_1 |= (unit & 0x7) << 3;
45146470a38SPatrick Venture };
45246470a38SPatrick Venture inline void set_analog_data_format(uint8_t format,
45346470a38SPatrick Venture                                    SensorDataFullRecordBody* body)
45446470a38SPatrick Venture {
45546470a38SPatrick Venture     body->sensor_units_1 &= ~(3 << 6);
45646470a38SPatrick Venture     body->sensor_units_1 |= (format & 0x3) << 6;
45746470a38SPatrick Venture };
45846470a38SPatrick Venture 
45946470a38SPatrick Venture inline void set_m(uint16_t m, SensorDataFullRecordBody* body)
46046470a38SPatrick Venture {
46146470a38SPatrick Venture     body->m_lsb = m & 0xff;
46246470a38SPatrick Venture     body->m_msb_and_tolerance &= ~(3 << 6);
46346470a38SPatrick Venture     body->m_msb_and_tolerance |= ((m & (3 << 8)) >> 2);
46446470a38SPatrick Venture };
46546470a38SPatrick Venture inline void set_tolerance(uint8_t tol, SensorDataFullRecordBody* body)
46646470a38SPatrick Venture {
46746470a38SPatrick Venture     body->m_msb_and_tolerance &= ~0x3f;
46846470a38SPatrick Venture     body->m_msb_and_tolerance |= tol & 0x3f;
46946470a38SPatrick Venture };
47046470a38SPatrick Venture 
47146470a38SPatrick Venture inline void set_b(uint16_t b, SensorDataFullRecordBody* body)
47246470a38SPatrick Venture {
47346470a38SPatrick Venture     body->b_lsb = b & 0xff;
47446470a38SPatrick Venture     body->b_msb_and_accuracy_lsb &= ~(3 << 6);
47546470a38SPatrick Venture     body->b_msb_and_accuracy_lsb |= ((b & (3 << 8)) >> 2);
47646470a38SPatrick Venture };
47746470a38SPatrick Venture inline void set_accuracy(uint16_t acc, SensorDataFullRecordBody* body)
47846470a38SPatrick Venture {
47946470a38SPatrick Venture     // bottom 6 bits
48046470a38SPatrick Venture     body->b_msb_and_accuracy_lsb &= ~0x3f;
48146470a38SPatrick Venture     body->b_msb_and_accuracy_lsb |= acc & 0x3f;
48246470a38SPatrick Venture     // top 4 bits
48346470a38SPatrick Venture     body->accuracy_and_sensor_direction &= 0x0f;
48446470a38SPatrick Venture     body->accuracy_and_sensor_direction |= ((acc >> 6) & 0xf) << 4;
48546470a38SPatrick Venture };
48646470a38SPatrick Venture inline void set_accuracy_exp(uint8_t exp, SensorDataFullRecordBody* body)
48746470a38SPatrick Venture {
48846470a38SPatrick Venture     body->accuracy_and_sensor_direction &= ~(3 << 2);
48946470a38SPatrick Venture     body->accuracy_and_sensor_direction |= (exp & 3) << 2;
49046470a38SPatrick Venture };
49146470a38SPatrick Venture inline void set_sensor_dir(uint8_t dir, SensorDataFullRecordBody* body)
49246470a38SPatrick Venture {
49346470a38SPatrick Venture     body->accuracy_and_sensor_direction &= ~(3 << 0);
49446470a38SPatrick Venture     body->accuracy_and_sensor_direction |= (dir & 3);
49546470a38SPatrick Venture };
49646470a38SPatrick Venture 
49746470a38SPatrick Venture inline void set_b_exp(uint8_t exp, SensorDataFullRecordBody* body)
49846470a38SPatrick Venture {
49946470a38SPatrick Venture     body->r_b_exponents &= 0xf0;
50046470a38SPatrick Venture     body->r_b_exponents |= exp & 0x0f;
50146470a38SPatrick Venture };
50246470a38SPatrick Venture inline void set_r_exp(uint8_t exp, SensorDataFullRecordBody* body)
50346470a38SPatrick Venture {
50446470a38SPatrick Venture     body->r_b_exponents &= 0x0f;
50546470a38SPatrick Venture     body->r_b_exponents |= (exp & 0x0f) << 4;
50646470a38SPatrick Venture };
50746470a38SPatrick Venture 
50846470a38SPatrick Venture inline void set_id_strlen(uint8_t len, SensorDataFullRecordBody* body)
50946470a38SPatrick Venture {
51046470a38SPatrick Venture     body->id_string_info &= ~(0x1f);
51146470a38SPatrick Venture     body->id_string_info |= len & 0x1f;
51246470a38SPatrick Venture };
51346470a38SPatrick Venture inline uint8_t get_id_strlen(SensorDataFullRecordBody* body)
51446470a38SPatrick Venture {
51546470a38SPatrick Venture     return body->id_string_info & 0x1f;
51646470a38SPatrick Venture };
51746470a38SPatrick Venture inline void set_id_type(uint8_t type, SensorDataFullRecordBody* body)
51846470a38SPatrick Venture {
51946470a38SPatrick Venture     body->id_string_info &= ~(3 << 6);
52046470a38SPatrick Venture     body->id_string_info |= (type & 0x3) << 6;
52146470a38SPatrick Venture };
52246470a38SPatrick Venture 
52346470a38SPatrick Venture inline void set_device_id_strlen(uint8_t len, SensorDataFruRecordBody* body)
52446470a38SPatrick Venture {
52546470a38SPatrick Venture     body->deviceIDLen &= ~(LENGTH_MASK);
52646470a38SPatrick Venture     body->deviceIDLen |= len & LENGTH_MASK;
52746470a38SPatrick Venture };
52846470a38SPatrick Venture 
52946470a38SPatrick Venture inline uint8_t get_device_id_strlen(SensorDataFruRecordBody* body)
53046470a38SPatrick Venture {
53146470a38SPatrick Venture     return body->deviceIDLen & LENGTH_MASK;
53246470a38SPatrick Venture };
53346470a38SPatrick Venture 
53446470a38SPatrick Venture inline void set_readable_mask(uint8_t mask, SensorDataFullRecordBody* body)
53546470a38SPatrick Venture {
53646470a38SPatrick Venture     body->discrete_reading_setting_mask[1] = mask & 0x3F;
53746470a38SPatrick Venture }
53846470a38SPatrick Venture 
53946470a38SPatrick Venture } // namespace body
54046470a38SPatrick Venture 
54146470a38SPatrick Venture // More types contained in section 43.17 Sensor Unit Type Codes,
54246470a38SPatrick Venture // IPMI spec v2 rev 1.1
54346470a38SPatrick Venture enum SensorUnitTypeCodes
54446470a38SPatrick Venture {
54546470a38SPatrick Venture     SENSOR_UNIT_UNSPECIFIED = 0,
54646470a38SPatrick Venture     SENSOR_UNIT_DEGREES_C = 1,
54746470a38SPatrick Venture     SENSOR_UNIT_VOLTS = 4,
54846470a38SPatrick Venture     SENSOR_UNIT_AMPERES = 5,
54946470a38SPatrick Venture     SENSOR_UNIT_WATTS = 6,
55046470a38SPatrick Venture     SENSOR_UNIT_JOULES = 7,
551*812e44c7SKirill Pakhomov     SENSOR_UNIT_RPM = 18,
55246470a38SPatrick Venture     SENSOR_UNIT_METERS = 34,
55346470a38SPatrick Venture     SENSOR_UNIT_REVOLUTIONS = 41,
55446470a38SPatrick Venture };
55546470a38SPatrick Venture 
55646470a38SPatrick Venture struct SensorDataFullRecord
55746470a38SPatrick Venture {
55846470a38SPatrick Venture     SensorDataRecordHeader header;
55946470a38SPatrick Venture     SensorDataRecordKey key;
56046470a38SPatrick Venture     SensorDataFullRecordBody body;
56146470a38SPatrick Venture } __attribute__((packed));
56246470a38SPatrick Venture 
56346470a38SPatrick Venture /** @struct SensorDataFruRecord
56446470a38SPatrick Venture  *
56546470a38SPatrick Venture  *  FRU Device Locator Record - SDR Type 11
56646470a38SPatrick Venture  */
56746470a38SPatrick Venture struct SensorDataFruRecord
56846470a38SPatrick Venture {
56946470a38SPatrick Venture     SensorDataRecordHeader header;
57046470a38SPatrick Venture     SensorDataFruRecordKey key;
57146470a38SPatrick Venture     SensorDataFruRecordBody body;
57246470a38SPatrick Venture } __attribute__((packed));
57346470a38SPatrick Venture 
57446470a38SPatrick Venture } // namespace get_sdr
57546470a38SPatrick Venture 
57646470a38SPatrick Venture namespace ipmi
57746470a38SPatrick Venture {
57846470a38SPatrick Venture 
57946470a38SPatrick Venture namespace sensor
58046470a38SPatrick Venture {
58146470a38SPatrick Venture 
58246470a38SPatrick Venture /**
58346470a38SPatrick Venture  * @brief Map offset to the corresponding bit in the assertion byte.
58446470a38SPatrick Venture  *
58546470a38SPatrick Venture  * The discrete sensors support up to 14 states. 0-7 offsets are stored in one
58646470a38SPatrick Venture  * byte and offsets 8-14 in the second byte.
58746470a38SPatrick Venture  *
58846470a38SPatrick Venture  * @param[in] offset - offset number.
58946470a38SPatrick Venture  * @param[in/out] resp - get sensor reading response.
59046470a38SPatrick Venture  */
59146470a38SPatrick Venture inline void setOffset(uint8_t offset, ipmi::sensor::GetReadingResponse* resp)
59246470a38SPatrick Venture {
59346470a38SPatrick Venture     if (offset > 7)
59446470a38SPatrick Venture     {
59546470a38SPatrick Venture         resp->assertOffset8_14 |= 1 << (offset - 8);
59646470a38SPatrick Venture     }
59746470a38SPatrick Venture     else
59846470a38SPatrick Venture     {
59946470a38SPatrick Venture         resp->assertOffset0_7 |= 1 << offset;
60046470a38SPatrick Venture     }
60146470a38SPatrick Venture }
60246470a38SPatrick Venture 
60346470a38SPatrick Venture /**
60446470a38SPatrick Venture  * @brief Set the reading field in the response.
60546470a38SPatrick Venture  *
60646470a38SPatrick Venture  * @param[in] offset - offset number.
60746470a38SPatrick Venture  * @param[in/out] resp - get sensor reading response.
60846470a38SPatrick Venture  */
60946470a38SPatrick Venture inline void setReading(uint8_t value, ipmi::sensor::GetReadingResponse* resp)
61046470a38SPatrick Venture {
61146470a38SPatrick Venture     resp->reading = value;
61246470a38SPatrick Venture }
61346470a38SPatrick Venture 
61446470a38SPatrick Venture /**
61546470a38SPatrick Venture  * @brief Map the value to the assertion bytes. The assertion states are stored
61646470a38SPatrick Venture  *        in 2 bytes.
61746470a38SPatrick Venture  *
61846470a38SPatrick Venture  * @param[in] value - value to mapped to the assertion byte.
61946470a38SPatrick Venture  * @param[in/out] resp - get sensor reading response.
62046470a38SPatrick Venture  */
62146470a38SPatrick Venture inline void setAssertionBytes(uint16_t value,
62246470a38SPatrick Venture                               ipmi::sensor::GetReadingResponse* resp)
62346470a38SPatrick Venture {
62446470a38SPatrick Venture     resp->assertOffset0_7 = static_cast<uint8_t>(value & 0x00FF);
62546470a38SPatrick Venture     resp->assertOffset8_14 = static_cast<uint8_t>(value >> 8);
62646470a38SPatrick Venture }
62746470a38SPatrick Venture 
62846470a38SPatrick Venture /**
62946470a38SPatrick Venture  * @brief Set the scanning enabled bit in the response.
63046470a38SPatrick Venture  *
63146470a38SPatrick Venture  * @param[in/out] resp - get sensor reading response.
63246470a38SPatrick Venture  */
63346470a38SPatrick Venture inline void enableScanning(ipmi::sensor::GetReadingResponse* resp)
63446470a38SPatrick Venture {
63546470a38SPatrick Venture     resp->operation = 1 << 6;
63646470a38SPatrick Venture }
63746470a38SPatrick Venture 
63846470a38SPatrick Venture } // namespace sensor
63946470a38SPatrick Venture 
64046470a38SPatrick Venture } // namespace ipmi
641