xref: /qemu/include/hw/ipmi/ipmi.h (revision fc524567087c2537b5103cdfc1d41e4f442892b6)
123076bb3SCorey Minyard /*
223076bb3SCorey Minyard  * IPMI base class
323076bb3SCorey Minyard  *
423076bb3SCorey Minyard  * Copyright (c) 2015 Corey Minyard, MontaVista Software, LLC
523076bb3SCorey Minyard  *
623076bb3SCorey Minyard  * Permission is hereby granted, free of charge, to any person obtaining a copy
723076bb3SCorey Minyard  * of this software and associated documentation files (the "Software"), to deal
823076bb3SCorey Minyard  * in the Software without restriction, including without limitation the rights
923076bb3SCorey Minyard  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
1023076bb3SCorey Minyard  * copies of the Software, and to permit persons to whom the Software is
1123076bb3SCorey Minyard  * furnished to do so, subject to the following conditions:
1223076bb3SCorey Minyard  *
1323076bb3SCorey Minyard  * The above copyright notice and this permission notice shall be included in
1423076bb3SCorey Minyard  * all copies or substantial portions of the Software.
1523076bb3SCorey Minyard  *
1623076bb3SCorey Minyard  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
1723076bb3SCorey Minyard  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
1823076bb3SCorey Minyard  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
1923076bb3SCorey Minyard  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
2023076bb3SCorey Minyard  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
2123076bb3SCorey Minyard  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
2223076bb3SCorey Minyard  * THE SOFTWARE.
2323076bb3SCorey Minyard  */
2423076bb3SCorey Minyard 
2523076bb3SCorey Minyard #ifndef HW_IPMI_H
2623076bb3SCorey Minyard #define HW_IPMI_H
2723076bb3SCorey Minyard 
28*8be545baSRichard Henderson #include "system/memory.h"
29a27bd6c7SMarkus Armbruster #include "hw/qdev-core.h"
30db1015e9SEduardo Habkost #include "qom/object.h"
3123076bb3SCorey Minyard 
3223076bb3SCorey Minyard #define MAX_IPMI_MSG_SIZE 300
3323076bb3SCorey Minyard 
3423076bb3SCorey Minyard enum ipmi_op {
3523076bb3SCorey Minyard     IPMI_RESET_CHASSIS,
3623076bb3SCorey Minyard     IPMI_POWEROFF_CHASSIS,
3723076bb3SCorey Minyard     IPMI_POWERON_CHASSIS,
3823076bb3SCorey Minyard     IPMI_POWERCYCLE_CHASSIS,
3923076bb3SCorey Minyard     IPMI_PULSE_DIAG_IRQ,
4023076bb3SCorey Minyard     IPMI_SHUTDOWN_VIA_ACPI_OVERTEMP,
4123076bb3SCorey Minyard     IPMI_SEND_NMI
4223076bb3SCorey Minyard };
4323076bb3SCorey Minyard 
447f9e7af4SNicholas Piggin /* Channel properties */
457f9e7af4SNicholas Piggin #define IPMI_CHANNEL_IPMB                0x00
467f9e7af4SNicholas Piggin #define IPMI_CHANNEL_SYSTEM              0x0f
477f9e7af4SNicholas Piggin #define IPMI_CHANNEL_MEDIUM_IPMB         0x01
487f9e7af4SNicholas Piggin #define IPMI_CHANNEL_MEDIUM_SYSTEM       0x0c
497f9e7af4SNicholas Piggin #define IPMI_CHANNEL_PROTOCOL_IPMB       0x01
507f9e7af4SNicholas Piggin #define IPMI_CHANNEL_PROTOCOL_KCS        0x05
517f9e7af4SNicholas Piggin #define IPMI_CHANNEL_PROTOCOL_BT_15      0x08
527f9e7af4SNicholas Piggin 
5323076bb3SCorey Minyard #define IPMI_CC_INVALID_CMD                              0xc1
5423076bb3SCorey Minyard #define IPMI_CC_COMMAND_INVALID_FOR_LUN                  0xc2
5523076bb3SCorey Minyard #define IPMI_CC_TIMEOUT                                  0xc3
5623076bb3SCorey Minyard #define IPMI_CC_OUT_OF_SPACE                             0xc4
5723076bb3SCorey Minyard #define IPMI_CC_INVALID_RESERVATION                      0xc5
5823076bb3SCorey Minyard #define IPMI_CC_REQUEST_DATA_TRUNCATED                   0xc6
5923076bb3SCorey Minyard #define IPMI_CC_REQUEST_DATA_LENGTH_INVALID              0xc7
6023076bb3SCorey Minyard #define IPMI_CC_PARM_OUT_OF_RANGE                        0xc9
6123076bb3SCorey Minyard #define IPMI_CC_CANNOT_RETURN_REQ_NUM_BYTES              0xca
6223076bb3SCorey Minyard #define IPMI_CC_REQ_ENTRY_NOT_PRESENT                    0xcb
6323076bb3SCorey Minyard #define IPMI_CC_INVALID_DATA_FIELD                       0xcc
6423076bb3SCorey Minyard #define IPMI_CC_BMC_INIT_IN_PROGRESS                     0xd2
6523076bb3SCorey Minyard #define IPMI_CC_COMMAND_NOT_SUPPORTED                    0xd5
66c02f9892SCédric Le Goater #define IPMI_CC_UNSPECIFIED                              0xff
6723076bb3SCorey Minyard 
6823076bb3SCorey Minyard #define IPMI_NETFN_APP                0x06
69ed8da05cSCédric Le Goater #define IPMI_NETFN_OEM                0x3a
7023076bb3SCorey Minyard 
7123076bb3SCorey Minyard #define IPMI_DEBUG 1
7223076bb3SCorey Minyard 
7323076bb3SCorey Minyard /* Specified in the SMBIOS spec. */
7423076bb3SCorey Minyard #define IPMI_SMBIOS_KCS         0x01
7523076bb3SCorey Minyard #define IPMI_SMBIOS_SMIC        0x02
7623076bb3SCorey Minyard #define IPMI_SMBIOS_BT          0x03
7723076bb3SCorey Minyard #define IPMI_SMBIOS_SSIF        0x04
7823076bb3SCorey Minyard 
7915139b8eSCorey Minyard /*
8015139b8eSCorey Minyard  * Used for transferring information to interfaces that add
8115139b8eSCorey Minyard  * entries to firmware tables.
8215139b8eSCorey Minyard  */
8315139b8eSCorey Minyard typedef struct IPMIFwInfo {
8415139b8eSCorey Minyard     const char *interface_name;
8515139b8eSCorey Minyard     int interface_type;
8615139b8eSCorey Minyard     uint8_t ipmi_spec_major_revision;
8715139b8eSCorey Minyard     uint8_t ipmi_spec_minor_revision;
887f9e7af4SNicholas Piggin     uint8_t ipmi_channel_protocol;
8915139b8eSCorey Minyard     uint8_t i2c_slave_address;
9015139b8eSCorey Minyard     uint32_t uuid;
9115139b8eSCorey Minyard 
9215139b8eSCorey Minyard     uint64_t base_address;
9315139b8eSCorey Minyard     uint64_t register_length;
9415139b8eSCorey Minyard     uint8_t register_spacing;
9515139b8eSCorey Minyard     enum {
9615139b8eSCorey Minyard         IPMI_MEMSPACE_IO,
9715139b8eSCorey Minyard         IPMI_MEMSPACE_MEM32,
9815139b8eSCorey Minyard         IPMI_MEMSPACE_MEM64,
9915139b8eSCorey Minyard         IPMI_MEMSPACE_SMBUS
10015139b8eSCorey Minyard     } memspace;
10115139b8eSCorey Minyard 
10215139b8eSCorey Minyard     int interrupt_number;
10315139b8eSCorey Minyard     enum {
1047376d10bSNicholas Piggin         IPMI_NO_IRQ = 0,
1057376d10bSNicholas Piggin         IPMI_ISA_IRQ,
1067376d10bSNicholas Piggin         IPMI_PCI_IRQ,
1077376d10bSNicholas Piggin     } irq_source;
1087376d10bSNicholas Piggin     enum {
10915139b8eSCorey Minyard         IPMI_LEVEL_IRQ,
11015139b8eSCorey Minyard         IPMI_EDGE_IRQ
11115139b8eSCorey Minyard     } irq_type;
11215139b8eSCorey Minyard } IPMIFwInfo;
11315139b8eSCorey Minyard 
11415139b8eSCorey Minyard /*
11515139b8eSCorey Minyard  * Called by each instantiated IPMI interface device to get it's uuid.
11615139b8eSCorey Minyard  */
11715139b8eSCorey Minyard uint32_t ipmi_next_uuid(void);
11815139b8eSCorey Minyard 
11923076bb3SCorey Minyard /* IPMI Interface types (KCS, SMIC, BT) are prefixed with this */
12023076bb3SCorey Minyard #define TYPE_IPMI_INTERFACE_PREFIX "ipmi-interface-"
12123076bb3SCorey Minyard 
12223076bb3SCorey Minyard /*
12323076bb3SCorey Minyard  * An IPMI Interface, the interface for talking between the target
12423076bb3SCorey Minyard  * and the BMC.
12523076bb3SCorey Minyard  */
12623076bb3SCorey Minyard #define TYPE_IPMI_INTERFACE "ipmi-interface"
12723076bb3SCorey Minyard #define IPMI_INTERFACE(obj) \
12823076bb3SCorey Minyard      INTERFACE_CHECK(IPMIInterface, (obj), TYPE_IPMI_INTERFACE)
129db1015e9SEduardo Habkost typedef struct IPMIInterfaceClass IPMIInterfaceClass;
1308110fa1dSEduardo Habkost DECLARE_CLASS_CHECKERS(IPMIInterfaceClass, IPMI_INTERFACE,
1318110fa1dSEduardo Habkost                        TYPE_IPMI_INTERFACE)
13223076bb3SCorey Minyard 
133aa1b35b9SMarc-André Lureau typedef struct IPMIInterface IPMIInterface;
13423076bb3SCorey Minyard 
135db1015e9SEduardo Habkost struct IPMIInterfaceClass {
13623076bb3SCorey Minyard     InterfaceClass parent;
13723076bb3SCorey Minyard 
13879d29a9dSCorey Minyard     /*
13979d29a9dSCorey Minyard      * min_size is the requested I/O size and must be a power of 2.
14079d29a9dSCorey Minyard      * This is so PCI (or other busses) can request a bigger range.
14179d29a9dSCorey Minyard      * Use 0 for the default.
14279d29a9dSCorey Minyard      */
14379d29a9dSCorey Minyard     void (*init)(struct IPMIInterface *s, unsigned int min_size, Error **errp);
14423076bb3SCorey Minyard 
14523076bb3SCorey Minyard     /*
14623076bb3SCorey Minyard      * Perform various operations on the hardware.  If checkonly is
14723076bb3SCorey Minyard      * true, it will return if the operation can be performed, but it
14823076bb3SCorey Minyard      * will not do the operation.
14923076bb3SCorey Minyard      */
15023076bb3SCorey Minyard     int (*do_hw_op)(struct IPMIInterface *s, enum ipmi_op op, int checkonly);
15123076bb3SCorey Minyard 
15223076bb3SCorey Minyard     /*
15323076bb3SCorey Minyard      * Enable/disable irqs on the interface when the BMC requests this.
15423076bb3SCorey Minyard      */
15523076bb3SCorey Minyard     void (*set_irq_enable)(struct IPMIInterface *s, int val);
15623076bb3SCorey Minyard 
15723076bb3SCorey Minyard     /*
15823076bb3SCorey Minyard      * Handle an event that occurred on the interface, generally the.
15923076bb3SCorey Minyard      * target writing to a register.
16023076bb3SCorey Minyard      */
16123076bb3SCorey Minyard     void (*handle_if_event)(struct IPMIInterface *s);
16223076bb3SCorey Minyard 
16323076bb3SCorey Minyard     /*
16423076bb3SCorey Minyard      * The interfaces use this to perform certain ops
16523076bb3SCorey Minyard      */
16623076bb3SCorey Minyard     void (*set_atn)(struct IPMIInterface *s, int val, int irq);
16723076bb3SCorey Minyard 
16823076bb3SCorey Minyard     /*
16923076bb3SCorey Minyard      * Got an IPMI warm/cold reset.
17023076bb3SCorey Minyard      */
17123076bb3SCorey Minyard     void (*reset)(struct IPMIInterface *s, bool is_cold);
17223076bb3SCorey Minyard 
17323076bb3SCorey Minyard     /*
17423076bb3SCorey Minyard      * Handle a response from the bmc.
17523076bb3SCorey Minyard      */
17623076bb3SCorey Minyard     void (*handle_rsp)(struct IPMIInterface *s, uint8_t msg_id,
17723076bb3SCorey Minyard                        unsigned char *rsp, unsigned int rsp_len);
17823076bb3SCorey Minyard 
17923076bb3SCorey Minyard     /*
18023076bb3SCorey Minyard      * Set by the owner to hold the backend data for the interface.
18123076bb3SCorey Minyard      */
18223076bb3SCorey Minyard     void *(*get_backend_data)(struct IPMIInterface *s);
18315139b8eSCorey Minyard 
18415139b8eSCorey Minyard     /*
18515139b8eSCorey Minyard      * Return the firmware info for a device.
18615139b8eSCorey Minyard      */
18715139b8eSCorey Minyard     void (*get_fwinfo)(struct IPMIInterface *s, IPMIFwInfo *info);
188db1015e9SEduardo Habkost };
18923076bb3SCorey Minyard 
19023076bb3SCorey Minyard /*
19123076bb3SCorey Minyard  * Define a BMC simulator (or perhaps a connection to a real BMC)
19223076bb3SCorey Minyard  */
19323076bb3SCorey Minyard #define TYPE_IPMI_BMC "ipmi-bmc"
194c821774aSEduardo Habkost OBJECT_DECLARE_TYPE(IPMIBmc, IPMIBmcClass,
19530b5707cSEduardo Habkost                     IPMI_BMC)
19623076bb3SCorey Minyard 
197db1015e9SEduardo Habkost struct IPMIBmc {
19823076bb3SCorey Minyard     DeviceState parent;
19923076bb3SCorey Minyard 
20023076bb3SCorey Minyard     uint8_t slave_addr;
20123076bb3SCorey Minyard 
20223076bb3SCorey Minyard     IPMIInterface *intf;
203db1015e9SEduardo Habkost };
20423076bb3SCorey Minyard 
205db1015e9SEduardo Habkost struct IPMIBmcClass {
20623076bb3SCorey Minyard     DeviceClass parent;
20723076bb3SCorey Minyard 
20823076bb3SCorey Minyard     /* Called when the system resets to report to the bmc. */
20923076bb3SCorey Minyard     void (*handle_reset)(struct IPMIBmc *s);
21023076bb3SCorey Minyard 
21123076bb3SCorey Minyard     /*
21223076bb3SCorey Minyard      * Handle a command to the bmc.
21323076bb3SCorey Minyard      */
21423076bb3SCorey Minyard     void (*handle_command)(struct IPMIBmc *s,
21523076bb3SCorey Minyard                            uint8_t *cmd, unsigned int cmd_len,
21623076bb3SCorey Minyard                            unsigned int max_cmd_len,
21723076bb3SCorey Minyard                            uint8_t msg_id);
218db1015e9SEduardo Habkost };
21923076bb3SCorey Minyard 
22023076bb3SCorey Minyard /*
22123076bb3SCorey Minyard  * Add a link property to obj that points to a BMC.
22223076bb3SCorey Minyard  */
22323076bb3SCorey Minyard void ipmi_bmc_find_and_link(Object *obj, Object **bmc);
22423076bb3SCorey Minyard 
22523076bb3SCorey Minyard #ifdef IPMI_DEBUG
22623076bb3SCorey Minyard #define ipmi_debug(fs, ...) \
22723076bb3SCorey Minyard     fprintf(stderr, "IPMI (%s): " fs, __func__, ##__VA_ARGS__)
22823076bb3SCorey Minyard #else
22923076bb3SCorey Minyard #define ipmi_debug(fs, ...)
23023076bb3SCorey Minyard #endif
23123076bb3SCorey Minyard 
232a2295f0aSCédric Le Goater struct ipmi_sdr_header {
233a2295f0aSCédric Le Goater     uint8_t  rec_id[2];
234a2295f0aSCédric Le Goater     uint8_t  sdr_version;               /* 0x51 */
235a2295f0aSCédric Le Goater     uint8_t  rec_type;
236a2295f0aSCédric Le Goater     uint8_t  rec_length;
237a2295f0aSCédric Le Goater };
238a2295f0aSCédric Le Goater #define IPMI_SDR_HEADER_SIZE     sizeof(struct ipmi_sdr_header)
239a2295f0aSCédric Le Goater 
240a2295f0aSCédric Le Goater #define ipmi_sdr_recid(sdr) ((sdr)->rec_id[0] | ((sdr)->rec_id[1] << 8))
241a2295f0aSCédric Le Goater #define ipmi_sdr_length(sdr) ((sdr)->rec_length + IPMI_SDR_HEADER_SIZE)
242a2295f0aSCédric Le Goater 
243a2295f0aSCédric Le Goater /*
244a2295f0aSCédric Le Goater  * 43.2 SDR Type 02h. Compact Sensor Record
245a2295f0aSCédric Le Goater  */
246a2295f0aSCédric Le Goater #define IPMI_SDR_COMPACT_TYPE    2
247a2295f0aSCédric Le Goater 
248a2295f0aSCédric Le Goater struct ipmi_sdr_compact {
249a2295f0aSCédric Le Goater     struct ipmi_sdr_header header;
250a2295f0aSCédric Le Goater 
251a2295f0aSCédric Le Goater     uint8_t  sensor_owner_id;
252a2295f0aSCédric Le Goater     uint8_t  sensor_owner_lun;
253a2295f0aSCédric Le Goater     uint8_t  sensor_owner_number;       /* byte 8 */
254a2295f0aSCédric Le Goater     uint8_t  entity_id;
255a2295f0aSCédric Le Goater     uint8_t  entity_instance;
256a2295f0aSCédric Le Goater     uint8_t  sensor_init;
257a2295f0aSCédric Le Goater     uint8_t  sensor_caps;
258a2295f0aSCédric Le Goater     uint8_t  sensor_type;
259a2295f0aSCédric Le Goater     uint8_t  reading_type;
260a2295f0aSCédric Le Goater     uint8_t  assert_mask[2];            /* byte 16 */
261a2295f0aSCédric Le Goater     uint8_t  deassert_mask[2];
262a2295f0aSCédric Le Goater     uint8_t  discrete_mask[2];
263a2295f0aSCédric Le Goater     uint8_t  sensor_unit1;
264a2295f0aSCédric Le Goater     uint8_t  sensor_unit2;
265a2295f0aSCédric Le Goater     uint8_t  sensor_unit3;
266a2295f0aSCédric Le Goater     uint8_t  sensor_direction[2];       /* byte 24 */
267a2295f0aSCédric Le Goater     uint8_t  positive_threshold;
268a2295f0aSCédric Le Goater     uint8_t  negative_threshold;
269a2295f0aSCédric Le Goater     uint8_t  reserved[3];
270a2295f0aSCédric Le Goater     uint8_t  oem;
271a2295f0aSCédric Le Goater     uint8_t  id_str_len;                /* byte 32 */
272a2295f0aSCédric Le Goater     uint8_t  id_string[16];
273a2295f0aSCédric Le Goater };
274a2295f0aSCédric Le Goater 
275a2295f0aSCédric Le Goater typedef uint8_t ipmi_sdr_compact_buffer[sizeof(struct ipmi_sdr_compact)];
276a2295f0aSCédric Le Goater 
2777fabcdb9SCédric Le Goater int ipmi_bmc_sdr_find(IPMIBmc *b, uint16_t recid,
2787fabcdb9SCédric Le Goater                       const struct ipmi_sdr_compact **sdr, uint16_t *nextrec);
279cd60d85eSCédric Le Goater void ipmi_bmc_gen_event(IPMIBmc *b, uint8_t *evt, bool log);
280cd60d85eSCédric Le Goater 
281ed8da05cSCédric Le Goater #define TYPE_IPMI_BMC_SIMULATOR "ipmi-bmc-sim"
2828063396bSEduardo Habkost OBJECT_DECLARE_SIMPLE_TYPE(IPMIBmcSim, IPMI_BMC_SIMULATOR)
283ed8da05cSCédric Le Goater 
284ed8da05cSCédric Le Goater 
285ed8da05cSCédric Le Goater typedef struct RspBuffer {
286ed8da05cSCédric Le Goater     uint8_t buffer[MAX_IPMI_MSG_SIZE];
287ed8da05cSCédric Le Goater     unsigned int len;
288ed8da05cSCédric Le Goater } RspBuffer;
289ed8da05cSCédric Le Goater 
rsp_buffer_set_error(RspBuffer * rsp,uint8_t byte)290ed8da05cSCédric Le Goater static inline void rsp_buffer_set_error(RspBuffer *rsp, uint8_t byte)
291ed8da05cSCédric Le Goater {
292ed8da05cSCédric Le Goater     rsp->buffer[2] = byte;
293ed8da05cSCédric Le Goater }
294ed8da05cSCédric Le Goater 
295ed8da05cSCédric Le Goater /* Add a byte to the response. */
rsp_buffer_push(RspBuffer * rsp,uint8_t byte)296ed8da05cSCédric Le Goater static inline void rsp_buffer_push(RspBuffer *rsp, uint8_t byte)
297ed8da05cSCédric Le Goater {
298ed8da05cSCédric Le Goater     if (rsp->len >= sizeof(rsp->buffer)) {
299ed8da05cSCédric Le Goater         rsp_buffer_set_error(rsp, IPMI_CC_REQUEST_DATA_TRUNCATED);
300ed8da05cSCédric Le Goater         return;
301ed8da05cSCédric Le Goater     }
302ed8da05cSCédric Le Goater     rsp->buffer[rsp->len++] = byte;
303ed8da05cSCédric Le Goater }
304ed8da05cSCédric Le Goater 
305ed8da05cSCédric Le Goater typedef struct IPMICmdHandler {
306ed8da05cSCédric Le Goater     void (*cmd_handler)(IPMIBmcSim *s,
307ed8da05cSCédric Le Goater                         uint8_t *cmd, unsigned int cmd_len,
308ed8da05cSCédric Le Goater                         RspBuffer *rsp);
309ed8da05cSCédric Le Goater     unsigned int cmd_len_min;
310ed8da05cSCédric Le Goater } IPMICmdHandler;
311ed8da05cSCédric Le Goater 
312ed8da05cSCédric Le Goater typedef struct IPMINetfn {
313ed8da05cSCédric Le Goater     unsigned int cmd_nums;
314ed8da05cSCédric Le Goater     const IPMICmdHandler *cmd_handlers;
315ed8da05cSCédric Le Goater } IPMINetfn;
316ed8da05cSCédric Le Goater 
317ed8da05cSCédric Le Goater int ipmi_sim_register_netfn(IPMIBmcSim *s, unsigned int netfn,
318ed8da05cSCédric Le Goater                             const IPMINetfn *netfnd);
319ed8da05cSCédric Le Goater 
32023076bb3SCorey Minyard #endif
321