xref: /openbmc/qemu/include/hw/ipmi/ipmi.h (revision b15e402f)
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 "exec/memory.h"
29 #include "hw/qdev-core.h"
30 
31 #define MAX_IPMI_MSG_SIZE 300
32 
33 enum ipmi_op {
34     IPMI_RESET_CHASSIS,
35     IPMI_POWEROFF_CHASSIS,
36     IPMI_POWERON_CHASSIS,
37     IPMI_POWERCYCLE_CHASSIS,
38     IPMI_PULSE_DIAG_IRQ,
39     IPMI_SHUTDOWN_VIA_ACPI_OVERTEMP,
40     IPMI_SEND_NMI
41 };
42 
43 #define IPMI_CC_INVALID_CMD                              0xc1
44 #define IPMI_CC_COMMAND_INVALID_FOR_LUN                  0xc2
45 #define IPMI_CC_TIMEOUT                                  0xc3
46 #define IPMI_CC_OUT_OF_SPACE                             0xc4
47 #define IPMI_CC_INVALID_RESERVATION                      0xc5
48 #define IPMI_CC_REQUEST_DATA_TRUNCATED                   0xc6
49 #define IPMI_CC_REQUEST_DATA_LENGTH_INVALID              0xc7
50 #define IPMI_CC_PARM_OUT_OF_RANGE                        0xc9
51 #define IPMI_CC_CANNOT_RETURN_REQ_NUM_BYTES              0xca
52 #define IPMI_CC_REQ_ENTRY_NOT_PRESENT                    0xcb
53 #define IPMI_CC_INVALID_DATA_FIELD                       0xcc
54 #define IPMI_CC_BMC_INIT_IN_PROGRESS                     0xd2
55 #define IPMI_CC_COMMAND_NOT_SUPPORTED                    0xd5
56 #define IPMI_CC_UNSPECIFIED                              0xff
57 
58 #define IPMI_NETFN_APP                0x06
59 #define IPMI_NETFN_OEM                0x3a
60 
61 #define IPMI_DEBUG 1
62 
63 /* Specified in the SMBIOS spec. */
64 #define IPMI_SMBIOS_KCS         0x01
65 #define IPMI_SMBIOS_SMIC        0x02
66 #define IPMI_SMBIOS_BT          0x03
67 #define IPMI_SMBIOS_SSIF        0x04
68 
69 /*
70  * Used for transferring information to interfaces that add
71  * entries to firmware tables.
72  */
73 typedef struct IPMIFwInfo {
74     const char *interface_name;
75     int interface_type;
76     uint8_t ipmi_spec_major_revision;
77     uint8_t ipmi_spec_minor_revision;
78     uint8_t i2c_slave_address;
79     uint32_t uuid;
80 
81     uint64_t base_address;
82     uint64_t register_length;
83     uint8_t register_spacing;
84     enum {
85         IPMI_MEMSPACE_IO,
86         IPMI_MEMSPACE_MEM32,
87         IPMI_MEMSPACE_MEM64,
88         IPMI_MEMSPACE_SMBUS
89     } memspace;
90 
91     int interrupt_number;
92     enum {
93         IPMI_LEVEL_IRQ,
94         IPMI_EDGE_IRQ
95     } irq_type;
96 } IPMIFwInfo;
97 
98 /*
99  * Called by each instantiated IPMI interface device to get it's uuid.
100  */
101 uint32_t ipmi_next_uuid(void);
102 
103 /* IPMI Interface types (KCS, SMIC, BT) are prefixed with this */
104 #define TYPE_IPMI_INTERFACE_PREFIX "ipmi-interface-"
105 
106 /*
107  * An IPMI Interface, the interface for talking between the target
108  * and the BMC.
109  */
110 #define TYPE_IPMI_INTERFACE "ipmi-interface"
111 #define IPMI_INTERFACE(obj) \
112      INTERFACE_CHECK(IPMIInterface, (obj), TYPE_IPMI_INTERFACE)
113 #define IPMI_INTERFACE_CLASS(class) \
114      OBJECT_CLASS_CHECK(IPMIInterfaceClass, (class), TYPE_IPMI_INTERFACE)
115 #define IPMI_INTERFACE_GET_CLASS(class) \
116      OBJECT_GET_CLASS(IPMIInterfaceClass, (class), TYPE_IPMI_INTERFACE)
117 
118 typedef struct IPMIInterface IPMIInterface;
119 
120 typedef struct IPMIInterfaceClass {
121     InterfaceClass parent;
122 
123     /*
124      * min_size is the requested I/O size and must be a power of 2.
125      * This is so PCI (or other busses) can request a bigger range.
126      * Use 0 for the default.
127      */
128     void (*init)(struct IPMIInterface *s, unsigned int min_size, Error **errp);
129 
130     /*
131      * Perform various operations on the hardware.  If checkonly is
132      * true, it will return if the operation can be performed, but it
133      * will not do the operation.
134      */
135     int (*do_hw_op)(struct IPMIInterface *s, enum ipmi_op op, int checkonly);
136 
137     /*
138      * Enable/disable irqs on the interface when the BMC requests this.
139      */
140     void (*set_irq_enable)(struct IPMIInterface *s, int val);
141 
142     /*
143      * Handle an event that occurred on the interface, generally the.
144      * target writing to a register.
145      */
146     void (*handle_if_event)(struct IPMIInterface *s);
147 
148     /*
149      * The interfaces use this to perform certain ops
150      */
151     void (*set_atn)(struct IPMIInterface *s, int val, int irq);
152 
153     /*
154      * Got an IPMI warm/cold reset.
155      */
156     void (*reset)(struct IPMIInterface *s, bool is_cold);
157 
158     /*
159      * Handle a response from the bmc.
160      */
161     void (*handle_rsp)(struct IPMIInterface *s, uint8_t msg_id,
162                        unsigned char *rsp, unsigned int rsp_len);
163 
164     /*
165      * Set by the owner to hold the backend data for the interface.
166      */
167     void *(*get_backend_data)(struct IPMIInterface *s);
168 
169     /*
170      * Return the firmware info for a device.
171      */
172     void (*get_fwinfo)(struct IPMIInterface *s, IPMIFwInfo *info);
173 } IPMIInterfaceClass;
174 
175 /*
176  * Define a BMC simulator (or perhaps a connection to a real BMC)
177  */
178 #define TYPE_IPMI_BMC "ipmi-bmc"
179 #define IPMI_BMC(obj) \
180      OBJECT_CHECK(IPMIBmc, (obj), TYPE_IPMI_BMC)
181 #define IPMI_BMC_CLASS(obj_class) \
182      OBJECT_CLASS_CHECK(IPMIBmcClass, (obj_class), TYPE_IPMI_BMC)
183 #define IPMI_BMC_GET_CLASS(obj) \
184      OBJECT_GET_CLASS(IPMIBmcClass, (obj), TYPE_IPMI_BMC)
185 
186 typedef struct IPMIBmc {
187     DeviceState parent;
188 
189     uint8_t slave_addr;
190 
191     IPMIInterface *intf;
192 } IPMIBmc;
193 
194 typedef struct IPMIBmcClass {
195     DeviceClass parent;
196 
197     /* Called when the system resets to report to the bmc. */
198     void (*handle_reset)(struct IPMIBmc *s);
199 
200     /*
201      * Handle a command to the bmc.
202      */
203     void (*handle_command)(struct IPMIBmc *s,
204                            uint8_t *cmd, unsigned int cmd_len,
205                            unsigned int max_cmd_len,
206                            uint8_t msg_id);
207 } IPMIBmcClass;
208 
209 /*
210  * Add a link property to obj that points to a BMC.
211  */
212 void ipmi_bmc_find_and_link(Object *obj, Object **bmc);
213 
214 #ifdef IPMI_DEBUG
215 #define ipmi_debug(fs, ...) \
216     fprintf(stderr, "IPMI (%s): " fs, __func__, ##__VA_ARGS__)
217 #else
218 #define ipmi_debug(fs, ...)
219 #endif
220 
221 struct ipmi_sdr_header {
222     uint8_t  rec_id[2];
223     uint8_t  sdr_version;               /* 0x51 */
224     uint8_t  rec_type;
225     uint8_t  rec_length;
226 };
227 #define IPMI_SDR_HEADER_SIZE     sizeof(struct ipmi_sdr_header)
228 
229 #define ipmi_sdr_recid(sdr) ((sdr)->rec_id[0] | ((sdr)->rec_id[1] << 8))
230 #define ipmi_sdr_length(sdr) ((sdr)->rec_length + IPMI_SDR_HEADER_SIZE)
231 
232 /*
233  * 43.2 SDR Type 02h. Compact Sensor Record
234  */
235 #define IPMI_SDR_COMPACT_TYPE    2
236 
237 struct ipmi_sdr_compact {
238     struct ipmi_sdr_header header;
239 
240     uint8_t  sensor_owner_id;
241     uint8_t  sensor_owner_lun;
242     uint8_t  sensor_owner_number;       /* byte 8 */
243     uint8_t  entity_id;
244     uint8_t  entity_instance;
245     uint8_t  sensor_init;
246     uint8_t  sensor_caps;
247     uint8_t  sensor_type;
248     uint8_t  reading_type;
249     uint8_t  assert_mask[2];            /* byte 16 */
250     uint8_t  deassert_mask[2];
251     uint8_t  discrete_mask[2];
252     uint8_t  sensor_unit1;
253     uint8_t  sensor_unit2;
254     uint8_t  sensor_unit3;
255     uint8_t  sensor_direction[2];       /* byte 24 */
256     uint8_t  positive_threshold;
257     uint8_t  negative_threshold;
258     uint8_t  reserved[3];
259     uint8_t  oem;
260     uint8_t  id_str_len;                /* byte 32 */
261     uint8_t  id_string[16];
262 };
263 
264 typedef uint8_t ipmi_sdr_compact_buffer[sizeof(struct ipmi_sdr_compact)];
265 
266 int ipmi_bmc_sdr_find(IPMIBmc *b, uint16_t recid,
267                       const struct ipmi_sdr_compact **sdr, uint16_t *nextrec);
268 void ipmi_bmc_gen_event(IPMIBmc *b, uint8_t *evt, bool log);
269 
270 #define TYPE_IPMI_BMC_SIMULATOR "ipmi-bmc-sim"
271 #define IPMI_BMC_SIMULATOR(obj) OBJECT_CHECK(IPMIBmcSim, (obj), \
272                                         TYPE_IPMI_BMC_SIMULATOR)
273 
274 typedef struct IPMIBmcSim IPMIBmcSim;
275 
276 typedef struct RspBuffer {
277     uint8_t buffer[MAX_IPMI_MSG_SIZE];
278     unsigned int len;
279 } RspBuffer;
280 
281 static inline void rsp_buffer_set_error(RspBuffer *rsp, uint8_t byte)
282 {
283     rsp->buffer[2] = byte;
284 }
285 
286 /* Add a byte to the response. */
287 static inline void rsp_buffer_push(RspBuffer *rsp, uint8_t byte)
288 {
289     if (rsp->len >= sizeof(rsp->buffer)) {
290         rsp_buffer_set_error(rsp, IPMI_CC_REQUEST_DATA_TRUNCATED);
291         return;
292     }
293     rsp->buffer[rsp->len++] = byte;
294 }
295 
296 typedef struct IPMICmdHandler {
297     void (*cmd_handler)(IPMIBmcSim *s,
298                         uint8_t *cmd, unsigned int cmd_len,
299                         RspBuffer *rsp);
300     unsigned int cmd_len_min;
301 } IPMICmdHandler;
302 
303 typedef struct IPMINetfn {
304     unsigned int cmd_nums;
305     const IPMICmdHandler *cmd_handlers;
306 } IPMINetfn;
307 
308 int ipmi_sim_register_netfn(IPMIBmcSim *s, unsigned int netfn,
309                             const IPMINetfn *netfnd);
310 
311 #endif
312