1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25#ifndef HW_IPMI_H
26#define HW_IPMI_H
27
28#include "exec/memory.h"
29#include "hw/qdev-core.h"
30#include "qom/object.h"
31
32#define MAX_IPMI_MSG_SIZE 300
33
34enum ipmi_op {
35 IPMI_RESET_CHASSIS,
36 IPMI_POWEROFF_CHASSIS,
37 IPMI_POWERON_CHASSIS,
38 IPMI_POWERCYCLE_CHASSIS,
39 IPMI_PULSE_DIAG_IRQ,
40 IPMI_SHUTDOWN_VIA_ACPI_OVERTEMP,
41 IPMI_SEND_NMI
42};
43
44#define IPMI_CC_INVALID_CMD 0xc1
45#define IPMI_CC_COMMAND_INVALID_FOR_LUN 0xc2
46#define IPMI_CC_TIMEOUT 0xc3
47#define IPMI_CC_OUT_OF_SPACE 0xc4
48#define IPMI_CC_INVALID_RESERVATION 0xc5
49#define IPMI_CC_REQUEST_DATA_TRUNCATED 0xc6
50#define IPMI_CC_REQUEST_DATA_LENGTH_INVALID 0xc7
51#define IPMI_CC_PARM_OUT_OF_RANGE 0xc9
52#define IPMI_CC_CANNOT_RETURN_REQ_NUM_BYTES 0xca
53#define IPMI_CC_REQ_ENTRY_NOT_PRESENT 0xcb
54#define IPMI_CC_INVALID_DATA_FIELD 0xcc
55#define IPMI_CC_BMC_INIT_IN_PROGRESS 0xd2
56#define IPMI_CC_COMMAND_NOT_SUPPORTED 0xd5
57#define IPMI_CC_UNSPECIFIED 0xff
58
59#define IPMI_NETFN_APP 0x06
60#define IPMI_NETFN_OEM 0x3a
61
62#define IPMI_DEBUG 1
63
64
65#define IPMI_SMBIOS_KCS 0x01
66#define IPMI_SMBIOS_SMIC 0x02
67#define IPMI_SMBIOS_BT 0x03
68#define IPMI_SMBIOS_SSIF 0x04
69
70
71
72
73
74typedef struct IPMIFwInfo {
75 const char *interface_name;
76 int interface_type;
77 uint8_t ipmi_spec_major_revision;
78 uint8_t ipmi_spec_minor_revision;
79 uint8_t i2c_slave_address;
80 uint32_t uuid;
81
82 uint64_t base_address;
83 uint64_t register_length;
84 uint8_t register_spacing;
85 enum {
86 IPMI_MEMSPACE_IO,
87 IPMI_MEMSPACE_MEM32,
88 IPMI_MEMSPACE_MEM64,
89 IPMI_MEMSPACE_SMBUS
90 } memspace;
91
92 int interrupt_number;
93 enum {
94 IPMI_LEVEL_IRQ,
95 IPMI_EDGE_IRQ
96 } irq_type;
97} IPMIFwInfo;
98
99
100
101
102uint32_t ipmi_next_uuid(void);
103
104
105#define TYPE_IPMI_INTERFACE_PREFIX "ipmi-interface-"
106
107
108
109
110
111#define TYPE_IPMI_INTERFACE "ipmi-interface"
112#define IPMI_INTERFACE(obj) \
113 INTERFACE_CHECK(IPMIInterface, (obj), TYPE_IPMI_INTERFACE)
114typedef struct IPMIInterfaceClass IPMIInterfaceClass;
115DECLARE_CLASS_CHECKERS(IPMIInterfaceClass, IPMI_INTERFACE,
116 TYPE_IPMI_INTERFACE)
117
118typedef struct IPMIInterface IPMIInterface;
119
120struct IPMIInterfaceClass {
121 InterfaceClass parent;
122
123
124
125
126
127
128 void (*init)(struct IPMIInterface *s, unsigned int min_size, Error **errp);
129
130
131
132
133
134
135 int (*do_hw_op)(struct IPMIInterface *s, enum ipmi_op op, int checkonly);
136
137
138
139
140 void (*set_irq_enable)(struct IPMIInterface *s, int val);
141
142
143
144
145
146 void (*handle_if_event)(struct IPMIInterface *s);
147
148
149
150
151 void (*set_atn)(struct IPMIInterface *s, int val, int irq);
152
153
154
155
156 void (*reset)(struct IPMIInterface *s, bool is_cold);
157
158
159
160
161 void (*handle_rsp)(struct IPMIInterface *s, uint8_t msg_id,
162 unsigned char *rsp, unsigned int rsp_len);
163
164
165
166
167 void *(*get_backend_data)(struct IPMIInterface *s);
168
169
170
171
172 void (*get_fwinfo)(struct IPMIInterface *s, IPMIFwInfo *info);
173};
174
175
176
177
178#define TYPE_IPMI_BMC "ipmi-bmc"
179OBJECT_DECLARE_TYPE(IPMIBmc, IPMIBmcClass,
180 IPMI_BMC)
181
182struct IPMIBmc {
183 DeviceState parent;
184
185 uint8_t slave_addr;
186
187 IPMIInterface *intf;
188};
189
190struct IPMIBmcClass {
191 DeviceClass parent;
192
193
194 void (*handle_reset)(struct IPMIBmc *s);
195
196
197
198
199 void (*handle_command)(struct IPMIBmc *s,
200 uint8_t *cmd, unsigned int cmd_len,
201 unsigned int max_cmd_len,
202 uint8_t msg_id);
203};
204
205
206
207
208void ipmi_bmc_find_and_link(Object *obj, Object **bmc);
209
210#ifdef IPMI_DEBUG
211#define ipmi_debug(fs, ...) \
212 fprintf(stderr, "IPMI (%s): " fs, __func__, ##__VA_ARGS__)
213#else
214#define ipmi_debug(fs, ...)
215#endif
216
217struct ipmi_sdr_header {
218 uint8_t rec_id[2];
219 uint8_t sdr_version;
220 uint8_t rec_type;
221 uint8_t rec_length;
222};
223#define IPMI_SDR_HEADER_SIZE sizeof(struct ipmi_sdr_header)
224
225#define ipmi_sdr_recid(sdr) ((sdr)->rec_id[0] | ((sdr)->rec_id[1] << 8))
226#define ipmi_sdr_length(sdr) ((sdr)->rec_length + IPMI_SDR_HEADER_SIZE)
227
228
229
230
231#define IPMI_SDR_COMPACT_TYPE 2
232
233struct ipmi_sdr_compact {
234 struct ipmi_sdr_header header;
235
236 uint8_t sensor_owner_id;
237 uint8_t sensor_owner_lun;
238 uint8_t sensor_owner_number;
239 uint8_t entity_id;
240 uint8_t entity_instance;
241 uint8_t sensor_init;
242 uint8_t sensor_caps;
243 uint8_t sensor_type;
244 uint8_t reading_type;
245 uint8_t assert_mask[2];
246 uint8_t deassert_mask[2];
247 uint8_t discrete_mask[2];
248 uint8_t sensor_unit1;
249 uint8_t sensor_unit2;
250 uint8_t sensor_unit3;
251 uint8_t sensor_direction[2];
252 uint8_t positive_threshold;
253 uint8_t negative_threshold;
254 uint8_t reserved[3];
255 uint8_t oem;
256 uint8_t id_str_len;
257 uint8_t id_string[16];
258};
259
260typedef uint8_t ipmi_sdr_compact_buffer[sizeof(struct ipmi_sdr_compact)];
261
262int ipmi_bmc_sdr_find(IPMIBmc *b, uint16_t recid,
263 const struct ipmi_sdr_compact **sdr, uint16_t *nextrec);
264void ipmi_bmc_gen_event(IPMIBmc *b, uint8_t *evt, bool log);
265
266#define TYPE_IPMI_BMC_SIMULATOR "ipmi-bmc-sim"
267OBJECT_DECLARE_SIMPLE_TYPE(IPMIBmcSim, IPMI_BMC_SIMULATOR)
268
269
270typedef struct RspBuffer {
271 uint8_t buffer[MAX_IPMI_MSG_SIZE];
272 unsigned int len;
273} RspBuffer;
274
275static inline void rsp_buffer_set_error(RspBuffer *rsp, uint8_t byte)
276{
277 rsp->buffer[2] = byte;
278}
279
280
281static inline void rsp_buffer_push(RspBuffer *rsp, uint8_t byte)
282{
283 if (rsp->len >= sizeof(rsp->buffer)) {
284 rsp_buffer_set_error(rsp, IPMI_CC_REQUEST_DATA_TRUNCATED);
285 return;
286 }
287 rsp->buffer[rsp->len++] = byte;
288}
289
290typedef struct IPMICmdHandler {
291 void (*cmd_handler)(IPMIBmcSim *s,
292 uint8_t *cmd, unsigned int cmd_len,
293 RspBuffer *rsp);
294 unsigned int cmd_len_min;
295} IPMICmdHandler;
296
297typedef struct IPMINetfn {
298 unsigned int cmd_nums;
299 const IPMICmdHandler *cmd_handlers;
300} IPMINetfn;
301
302int ipmi_sim_register_netfn(IPMIBmcSim *s, unsigned int netfn,
303 const IPMINetfn *netfnd);
304
305#endif
306