1 /*
2 * IPMI base class
3 *
4 * Copyright (c) 2015 Corey Minyard, MontaVista Software, LLC
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 * THE SOFTWARE.
23 */
24
25 #ifndef HW_IPMI_H
26 #define HW_IPMI_H
27
28 #include "system/memory.h"
29 #include "hw/qdev-core.h"
30 #include "qom/object.h"
31
32 #define MAX_IPMI_MSG_SIZE 300
33
34 enum 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 /* Channel properties */
45 #define IPMI_CHANNEL_IPMB 0x00
46 #define IPMI_CHANNEL_SYSTEM 0x0f
47 #define IPMI_CHANNEL_MEDIUM_IPMB 0x01
48 #define IPMI_CHANNEL_MEDIUM_SYSTEM 0x0c
49 #define IPMI_CHANNEL_PROTOCOL_IPMB 0x01
50 #define IPMI_CHANNEL_PROTOCOL_KCS 0x05
51 #define IPMI_CHANNEL_PROTOCOL_BT_15 0x08
52
53 #define IPMI_CC_INVALID_CMD 0xc1
54 #define IPMI_CC_COMMAND_INVALID_FOR_LUN 0xc2
55 #define IPMI_CC_TIMEOUT 0xc3
56 #define IPMI_CC_OUT_OF_SPACE 0xc4
57 #define IPMI_CC_INVALID_RESERVATION 0xc5
58 #define IPMI_CC_REQUEST_DATA_TRUNCATED 0xc6
59 #define IPMI_CC_REQUEST_DATA_LENGTH_INVALID 0xc7
60 #define IPMI_CC_PARM_OUT_OF_RANGE 0xc9
61 #define IPMI_CC_CANNOT_RETURN_REQ_NUM_BYTES 0xca
62 #define IPMI_CC_REQ_ENTRY_NOT_PRESENT 0xcb
63 #define IPMI_CC_INVALID_DATA_FIELD 0xcc
64 #define IPMI_CC_BMC_INIT_IN_PROGRESS 0xd2
65 #define IPMI_CC_COMMAND_NOT_SUPPORTED 0xd5
66 #define IPMI_CC_UNSPECIFIED 0xff
67
68 #define IPMI_NETFN_APP 0x06
69 #define IPMI_NETFN_OEM 0x3a
70
71 #define IPMI_DEBUG 1
72
73 /* Specified in the SMBIOS spec. */
74 #define IPMI_SMBIOS_KCS 0x01
75 #define IPMI_SMBIOS_SMIC 0x02
76 #define IPMI_SMBIOS_BT 0x03
77 #define IPMI_SMBIOS_SSIF 0x04
78
79 /*
80 * Used for transferring information to interfaces that add
81 * entries to firmware tables.
82 */
83 typedef struct IPMIFwInfo {
84 const char *interface_name;
85 int interface_type;
86 uint8_t ipmi_spec_major_revision;
87 uint8_t ipmi_spec_minor_revision;
88 uint8_t ipmi_channel_protocol;
89 uint8_t i2c_slave_address;
90 uint32_t uuid;
91
92 uint64_t base_address;
93 uint64_t register_length;
94 uint8_t register_spacing;
95 enum {
96 IPMI_MEMSPACE_IO,
97 IPMI_MEMSPACE_MEM32,
98 IPMI_MEMSPACE_MEM64,
99 IPMI_MEMSPACE_SMBUS
100 } memspace;
101
102 int interrupt_number;
103 enum {
104 IPMI_NO_IRQ = 0,
105 IPMI_ISA_IRQ,
106 IPMI_PCI_IRQ,
107 } irq_source;
108 enum {
109 IPMI_LEVEL_IRQ,
110 IPMI_EDGE_IRQ
111 } irq_type;
112 } IPMIFwInfo;
113
114 /*
115 * Called by each instantiated IPMI interface device to get it's uuid.
116 */
117 uint32_t ipmi_next_uuid(void);
118
119 /* IPMI Interface types (KCS, SMIC, BT) are prefixed with this */
120 #define TYPE_IPMI_INTERFACE_PREFIX "ipmi-interface-"
121
122 /*
123 * An IPMI Interface, the interface for talking between the target
124 * and the BMC.
125 */
126 #define TYPE_IPMI_INTERFACE "ipmi-interface"
127 #define IPMI_INTERFACE(obj) \
128 INTERFACE_CHECK(IPMIInterface, (obj), TYPE_IPMI_INTERFACE)
129 typedef struct IPMIInterfaceClass IPMIInterfaceClass;
130 DECLARE_CLASS_CHECKERS(IPMIInterfaceClass, IPMI_INTERFACE,
131 TYPE_IPMI_INTERFACE)
132
133 typedef struct IPMIInterface IPMIInterface;
134
135 struct IPMIInterfaceClass {
136 InterfaceClass parent;
137
138 /*
139 * min_size is the requested I/O size and must be a power of 2.
140 * This is so PCI (or other busses) can request a bigger range.
141 * Use 0 for the default.
142 */
143 void (*init)(struct IPMIInterface *s, unsigned int min_size, Error **errp);
144
145 /*
146 * Perform various operations on the hardware. If checkonly is
147 * true, it will return if the operation can be performed, but it
148 * will not do the operation.
149 */
150 int (*do_hw_op)(struct IPMIInterface *s, enum ipmi_op op, int checkonly);
151
152 /*
153 * Enable/disable irqs on the interface when the BMC requests this.
154 */
155 void (*set_irq_enable)(struct IPMIInterface *s, int val);
156
157 /*
158 * Handle an event that occurred on the interface, generally the.
159 * target writing to a register.
160 */
161 void (*handle_if_event)(struct IPMIInterface *s);
162
163 /*
164 * The interfaces use this to perform certain ops
165 */
166 void (*set_atn)(struct IPMIInterface *s, int val, int irq);
167
168 /*
169 * Got an IPMI warm/cold reset.
170 */
171 void (*reset)(struct IPMIInterface *s, bool is_cold);
172
173 /*
174 * Handle a response from the bmc.
175 */
176 void (*handle_rsp)(struct IPMIInterface *s, uint8_t msg_id,
177 unsigned char *rsp, unsigned int rsp_len);
178
179 /*
180 * Set by the owner to hold the backend data for the interface.
181 */
182 void *(*get_backend_data)(struct IPMIInterface *s);
183
184 /*
185 * Return the firmware info for a device.
186 */
187 void (*get_fwinfo)(struct IPMIInterface *s, IPMIFwInfo *info);
188 };
189
190 /*
191 * Define a BMC simulator (or perhaps a connection to a real BMC)
192 */
193 #define TYPE_IPMI_BMC "ipmi-bmc"
194 OBJECT_DECLARE_TYPE(IPMIBmc, IPMIBmcClass,
195 IPMI_BMC)
196
197 struct IPMIBmc {
198 DeviceState parent;
199
200 uint8_t slave_addr;
201
202 IPMIInterface *intf;
203 };
204
205 struct IPMIBmcClass {
206 DeviceClass parent;
207
208 /* Called when the system resets to report to the bmc. */
209 void (*handle_reset)(struct IPMIBmc *s);
210
211 /*
212 * Handle a command to the bmc.
213 */
214 void (*handle_command)(struct IPMIBmc *s,
215 uint8_t *cmd, unsigned int cmd_len,
216 unsigned int max_cmd_len,
217 uint8_t msg_id);
218 };
219
220 /*
221 * Add a link property to obj that points to a BMC.
222 */
223 void ipmi_bmc_find_and_link(Object *obj, Object **bmc);
224
225 #ifdef IPMI_DEBUG
226 #define ipmi_debug(fs, ...) \
227 fprintf(stderr, "IPMI (%s): " fs, __func__, ##__VA_ARGS__)
228 #else
229 #define ipmi_debug(fs, ...)
230 #endif
231
232 struct ipmi_sdr_header {
233 uint8_t rec_id[2];
234 uint8_t sdr_version; /* 0x51 */
235 uint8_t rec_type;
236 uint8_t rec_length;
237 };
238 #define IPMI_SDR_HEADER_SIZE sizeof(struct ipmi_sdr_header)
239
240 #define ipmi_sdr_recid(sdr) ((sdr)->rec_id[0] | ((sdr)->rec_id[1] << 8))
241 #define ipmi_sdr_length(sdr) ((sdr)->rec_length + IPMI_SDR_HEADER_SIZE)
242
243 /*
244 * 43.2 SDR Type 02h. Compact Sensor Record
245 */
246 #define IPMI_SDR_COMPACT_TYPE 2
247
248 struct ipmi_sdr_compact {
249 struct ipmi_sdr_header header;
250
251 uint8_t sensor_owner_id;
252 uint8_t sensor_owner_lun;
253 uint8_t sensor_owner_number; /* byte 8 */
254 uint8_t entity_id;
255 uint8_t entity_instance;
256 uint8_t sensor_init;
257 uint8_t sensor_caps;
258 uint8_t sensor_type;
259 uint8_t reading_type;
260 uint8_t assert_mask[2]; /* byte 16 */
261 uint8_t deassert_mask[2];
262 uint8_t discrete_mask[2];
263 uint8_t sensor_unit1;
264 uint8_t sensor_unit2;
265 uint8_t sensor_unit3;
266 uint8_t sensor_direction[2]; /* byte 24 */
267 uint8_t positive_threshold;
268 uint8_t negative_threshold;
269 uint8_t reserved[3];
270 uint8_t oem;
271 uint8_t id_str_len; /* byte 32 */
272 uint8_t id_string[16];
273 };
274
275 typedef uint8_t ipmi_sdr_compact_buffer[sizeof(struct ipmi_sdr_compact)];
276
277 int ipmi_bmc_sdr_find(IPMIBmc *b, uint16_t recid,
278 const struct ipmi_sdr_compact **sdr, uint16_t *nextrec);
279 void ipmi_bmc_gen_event(IPMIBmc *b, uint8_t *evt, bool log);
280
281 #define TYPE_IPMI_BMC_SIMULATOR "ipmi-bmc-sim"
282 OBJECT_DECLARE_SIMPLE_TYPE(IPMIBmcSim, IPMI_BMC_SIMULATOR)
283
284
285 typedef struct RspBuffer {
286 uint8_t buffer[MAX_IPMI_MSG_SIZE];
287 unsigned int len;
288 } RspBuffer;
289
rsp_buffer_set_error(RspBuffer * rsp,uint8_t byte)290 static inline void rsp_buffer_set_error(RspBuffer *rsp, uint8_t byte)
291 {
292 rsp->buffer[2] = byte;
293 }
294
295 /* Add a byte to the response. */
rsp_buffer_push(RspBuffer * rsp,uint8_t byte)296 static inline void rsp_buffer_push(RspBuffer *rsp, uint8_t byte)
297 {
298 if (rsp->len >= sizeof(rsp->buffer)) {
299 rsp_buffer_set_error(rsp, IPMI_CC_REQUEST_DATA_TRUNCATED);
300 return;
301 }
302 rsp->buffer[rsp->len++] = byte;
303 }
304
305 typedef struct IPMICmdHandler {
306 void (*cmd_handler)(IPMIBmcSim *s,
307 uint8_t *cmd, unsigned int cmd_len,
308 RspBuffer *rsp);
309 unsigned int cmd_len_min;
310 } IPMICmdHandler;
311
312 typedef struct IPMINetfn {
313 unsigned int cmd_nums;
314 const IPMICmdHandler *cmd_handlers;
315 } IPMINetfn;
316
317 int ipmi_sim_register_netfn(IPMIBmcSim *s, unsigned int netfn,
318 const IPMINetfn *netfnd);
319
320 #endif
321