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 2823076bb3SCorey Minyard #include "exec/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 4423076bb3SCorey Minyard #define IPMI_CC_INVALID_CMD 0xc1 4523076bb3SCorey Minyard #define IPMI_CC_COMMAND_INVALID_FOR_LUN 0xc2 4623076bb3SCorey Minyard #define IPMI_CC_TIMEOUT 0xc3 4723076bb3SCorey Minyard #define IPMI_CC_OUT_OF_SPACE 0xc4 4823076bb3SCorey Minyard #define IPMI_CC_INVALID_RESERVATION 0xc5 4923076bb3SCorey Minyard #define IPMI_CC_REQUEST_DATA_TRUNCATED 0xc6 5023076bb3SCorey Minyard #define IPMI_CC_REQUEST_DATA_LENGTH_INVALID 0xc7 5123076bb3SCorey Minyard #define IPMI_CC_PARM_OUT_OF_RANGE 0xc9 5223076bb3SCorey Minyard #define IPMI_CC_CANNOT_RETURN_REQ_NUM_BYTES 0xca 5323076bb3SCorey Minyard #define IPMI_CC_REQ_ENTRY_NOT_PRESENT 0xcb 5423076bb3SCorey Minyard #define IPMI_CC_INVALID_DATA_FIELD 0xcc 5523076bb3SCorey Minyard #define IPMI_CC_BMC_INIT_IN_PROGRESS 0xd2 5623076bb3SCorey Minyard #define IPMI_CC_COMMAND_NOT_SUPPORTED 0xd5 57c02f9892SCédric Le Goater #define IPMI_CC_UNSPECIFIED 0xff 5823076bb3SCorey Minyard 5923076bb3SCorey Minyard #define IPMI_NETFN_APP 0x06 60ed8da05cSCédric Le Goater #define IPMI_NETFN_OEM 0x3a 6123076bb3SCorey Minyard 6223076bb3SCorey Minyard #define IPMI_DEBUG 1 6323076bb3SCorey Minyard 6423076bb3SCorey Minyard /* Specified in the SMBIOS spec. */ 6523076bb3SCorey Minyard #define IPMI_SMBIOS_KCS 0x01 6623076bb3SCorey Minyard #define IPMI_SMBIOS_SMIC 0x02 6723076bb3SCorey Minyard #define IPMI_SMBIOS_BT 0x03 6823076bb3SCorey Minyard #define IPMI_SMBIOS_SSIF 0x04 6923076bb3SCorey Minyard 7015139b8eSCorey Minyard /* 7115139b8eSCorey Minyard * Used for transferring information to interfaces that add 7215139b8eSCorey Minyard * entries to firmware tables. 7315139b8eSCorey Minyard */ 7415139b8eSCorey Minyard typedef struct IPMIFwInfo { 7515139b8eSCorey Minyard const char *interface_name; 7615139b8eSCorey Minyard int interface_type; 7715139b8eSCorey Minyard uint8_t ipmi_spec_major_revision; 7815139b8eSCorey Minyard uint8_t ipmi_spec_minor_revision; 7915139b8eSCorey Minyard uint8_t i2c_slave_address; 8015139b8eSCorey Minyard uint32_t uuid; 8115139b8eSCorey Minyard 8215139b8eSCorey Minyard uint64_t base_address; 8315139b8eSCorey Minyard uint64_t register_length; 8415139b8eSCorey Minyard uint8_t register_spacing; 8515139b8eSCorey Minyard enum { 8615139b8eSCorey Minyard IPMI_MEMSPACE_IO, 8715139b8eSCorey Minyard IPMI_MEMSPACE_MEM32, 8815139b8eSCorey Minyard IPMI_MEMSPACE_MEM64, 8915139b8eSCorey Minyard IPMI_MEMSPACE_SMBUS 9015139b8eSCorey Minyard } memspace; 9115139b8eSCorey Minyard 9215139b8eSCorey Minyard int interrupt_number; 9315139b8eSCorey Minyard enum { 9415139b8eSCorey Minyard IPMI_LEVEL_IRQ, 9515139b8eSCorey Minyard IPMI_EDGE_IRQ 9615139b8eSCorey Minyard } irq_type; 9715139b8eSCorey Minyard } IPMIFwInfo; 9815139b8eSCorey Minyard 9915139b8eSCorey Minyard /* 10015139b8eSCorey Minyard * Called by each instantiated IPMI interface device to get it's uuid. 10115139b8eSCorey Minyard */ 10215139b8eSCorey Minyard uint32_t ipmi_next_uuid(void); 10315139b8eSCorey Minyard 10423076bb3SCorey Minyard /* IPMI Interface types (KCS, SMIC, BT) are prefixed with this */ 10523076bb3SCorey Minyard #define TYPE_IPMI_INTERFACE_PREFIX "ipmi-interface-" 10623076bb3SCorey Minyard 10723076bb3SCorey Minyard /* 10823076bb3SCorey Minyard * An IPMI Interface, the interface for talking between the target 10923076bb3SCorey Minyard * and the BMC. 11023076bb3SCorey Minyard */ 11123076bb3SCorey Minyard #define TYPE_IPMI_INTERFACE "ipmi-interface" 11223076bb3SCorey Minyard #define IPMI_INTERFACE(obj) \ 11323076bb3SCorey Minyard INTERFACE_CHECK(IPMIInterface, (obj), TYPE_IPMI_INTERFACE) 114db1015e9SEduardo Habkost typedef struct IPMIInterfaceClass IPMIInterfaceClass; 1158110fa1dSEduardo Habkost DECLARE_CLASS_CHECKERS(IPMIInterfaceClass, IPMI_INTERFACE, 1168110fa1dSEduardo Habkost TYPE_IPMI_INTERFACE) 11723076bb3SCorey Minyard 118aa1b35b9SMarc-André Lureau typedef struct IPMIInterface IPMIInterface; 11923076bb3SCorey Minyard 120db1015e9SEduardo Habkost struct IPMIInterfaceClass { 12123076bb3SCorey Minyard InterfaceClass parent; 12223076bb3SCorey Minyard 12379d29a9dSCorey Minyard /* 12479d29a9dSCorey Minyard * min_size is the requested I/O size and must be a power of 2. 12579d29a9dSCorey Minyard * This is so PCI (or other busses) can request a bigger range. 12679d29a9dSCorey Minyard * Use 0 for the default. 12779d29a9dSCorey Minyard */ 12879d29a9dSCorey Minyard void (*init)(struct IPMIInterface *s, unsigned int min_size, Error **errp); 12923076bb3SCorey Minyard 13023076bb3SCorey Minyard /* 13123076bb3SCorey Minyard * Perform various operations on the hardware. If checkonly is 13223076bb3SCorey Minyard * true, it will return if the operation can be performed, but it 13323076bb3SCorey Minyard * will not do the operation. 13423076bb3SCorey Minyard */ 13523076bb3SCorey Minyard int (*do_hw_op)(struct IPMIInterface *s, enum ipmi_op op, int checkonly); 13623076bb3SCorey Minyard 13723076bb3SCorey Minyard /* 13823076bb3SCorey Minyard * Enable/disable irqs on the interface when the BMC requests this. 13923076bb3SCorey Minyard */ 14023076bb3SCorey Minyard void (*set_irq_enable)(struct IPMIInterface *s, int val); 14123076bb3SCorey Minyard 14223076bb3SCorey Minyard /* 14323076bb3SCorey Minyard * Handle an event that occurred on the interface, generally the. 14423076bb3SCorey Minyard * target writing to a register. 14523076bb3SCorey Minyard */ 14623076bb3SCorey Minyard void (*handle_if_event)(struct IPMIInterface *s); 14723076bb3SCorey Minyard 14823076bb3SCorey Minyard /* 14923076bb3SCorey Minyard * The interfaces use this to perform certain ops 15023076bb3SCorey Minyard */ 15123076bb3SCorey Minyard void (*set_atn)(struct IPMIInterface *s, int val, int irq); 15223076bb3SCorey Minyard 15323076bb3SCorey Minyard /* 15423076bb3SCorey Minyard * Got an IPMI warm/cold reset. 15523076bb3SCorey Minyard */ 15623076bb3SCorey Minyard void (*reset)(struct IPMIInterface *s, bool is_cold); 15723076bb3SCorey Minyard 15823076bb3SCorey Minyard /* 15923076bb3SCorey Minyard * Handle a response from the bmc. 16023076bb3SCorey Minyard */ 16123076bb3SCorey Minyard void (*handle_rsp)(struct IPMIInterface *s, uint8_t msg_id, 16223076bb3SCorey Minyard unsigned char *rsp, unsigned int rsp_len); 16323076bb3SCorey Minyard 16423076bb3SCorey Minyard /* 16523076bb3SCorey Minyard * Set by the owner to hold the backend data for the interface. 16623076bb3SCorey Minyard */ 16723076bb3SCorey Minyard void *(*get_backend_data)(struct IPMIInterface *s); 16815139b8eSCorey Minyard 16915139b8eSCorey Minyard /* 17015139b8eSCorey Minyard * Return the firmware info for a device. 17115139b8eSCorey Minyard */ 17215139b8eSCorey Minyard void (*get_fwinfo)(struct IPMIInterface *s, IPMIFwInfo *info); 173db1015e9SEduardo Habkost }; 17423076bb3SCorey Minyard 17523076bb3SCorey Minyard /* 17623076bb3SCorey Minyard * Define a BMC simulator (or perhaps a connection to a real BMC) 17723076bb3SCorey Minyard */ 17823076bb3SCorey Minyard #define TYPE_IPMI_BMC "ipmi-bmc" 179c821774aSEduardo Habkost OBJECT_DECLARE_TYPE(IPMIBmc, IPMIBmcClass, 180*30b5707cSEduardo Habkost IPMI_BMC) 18123076bb3SCorey Minyard 182db1015e9SEduardo Habkost struct IPMIBmc { 18323076bb3SCorey Minyard DeviceState parent; 18423076bb3SCorey Minyard 18523076bb3SCorey Minyard uint8_t slave_addr; 18623076bb3SCorey Minyard 18723076bb3SCorey Minyard IPMIInterface *intf; 188db1015e9SEduardo Habkost }; 18923076bb3SCorey Minyard 190db1015e9SEduardo Habkost struct IPMIBmcClass { 19123076bb3SCorey Minyard DeviceClass parent; 19223076bb3SCorey Minyard 19323076bb3SCorey Minyard /* Called when the system resets to report to the bmc. */ 19423076bb3SCorey Minyard void (*handle_reset)(struct IPMIBmc *s); 19523076bb3SCorey Minyard 19623076bb3SCorey Minyard /* 19723076bb3SCorey Minyard * Handle a command to the bmc. 19823076bb3SCorey Minyard */ 19923076bb3SCorey Minyard void (*handle_command)(struct IPMIBmc *s, 20023076bb3SCorey Minyard uint8_t *cmd, unsigned int cmd_len, 20123076bb3SCorey Minyard unsigned int max_cmd_len, 20223076bb3SCorey Minyard uint8_t msg_id); 203db1015e9SEduardo Habkost }; 20423076bb3SCorey Minyard 20523076bb3SCorey Minyard /* 20623076bb3SCorey Minyard * Add a link property to obj that points to a BMC. 20723076bb3SCorey Minyard */ 20823076bb3SCorey Minyard void ipmi_bmc_find_and_link(Object *obj, Object **bmc); 20923076bb3SCorey Minyard 21023076bb3SCorey Minyard #ifdef IPMI_DEBUG 21123076bb3SCorey Minyard #define ipmi_debug(fs, ...) \ 21223076bb3SCorey Minyard fprintf(stderr, "IPMI (%s): " fs, __func__, ##__VA_ARGS__) 21323076bb3SCorey Minyard #else 21423076bb3SCorey Minyard #define ipmi_debug(fs, ...) 21523076bb3SCorey Minyard #endif 21623076bb3SCorey Minyard 217a2295f0aSCédric Le Goater struct ipmi_sdr_header { 218a2295f0aSCédric Le Goater uint8_t rec_id[2]; 219a2295f0aSCédric Le Goater uint8_t sdr_version; /* 0x51 */ 220a2295f0aSCédric Le Goater uint8_t rec_type; 221a2295f0aSCédric Le Goater uint8_t rec_length; 222a2295f0aSCédric Le Goater }; 223a2295f0aSCédric Le Goater #define IPMI_SDR_HEADER_SIZE sizeof(struct ipmi_sdr_header) 224a2295f0aSCédric Le Goater 225a2295f0aSCédric Le Goater #define ipmi_sdr_recid(sdr) ((sdr)->rec_id[0] | ((sdr)->rec_id[1] << 8)) 226a2295f0aSCédric Le Goater #define ipmi_sdr_length(sdr) ((sdr)->rec_length + IPMI_SDR_HEADER_SIZE) 227a2295f0aSCédric Le Goater 228a2295f0aSCédric Le Goater /* 229a2295f0aSCédric Le Goater * 43.2 SDR Type 02h. Compact Sensor Record 230a2295f0aSCédric Le Goater */ 231a2295f0aSCédric Le Goater #define IPMI_SDR_COMPACT_TYPE 2 232a2295f0aSCédric Le Goater 233a2295f0aSCédric Le Goater struct ipmi_sdr_compact { 234a2295f0aSCédric Le Goater struct ipmi_sdr_header header; 235a2295f0aSCédric Le Goater 236a2295f0aSCédric Le Goater uint8_t sensor_owner_id; 237a2295f0aSCédric Le Goater uint8_t sensor_owner_lun; 238a2295f0aSCédric Le Goater uint8_t sensor_owner_number; /* byte 8 */ 239a2295f0aSCédric Le Goater uint8_t entity_id; 240a2295f0aSCédric Le Goater uint8_t entity_instance; 241a2295f0aSCédric Le Goater uint8_t sensor_init; 242a2295f0aSCédric Le Goater uint8_t sensor_caps; 243a2295f0aSCédric Le Goater uint8_t sensor_type; 244a2295f0aSCédric Le Goater uint8_t reading_type; 245a2295f0aSCédric Le Goater uint8_t assert_mask[2]; /* byte 16 */ 246a2295f0aSCédric Le Goater uint8_t deassert_mask[2]; 247a2295f0aSCédric Le Goater uint8_t discrete_mask[2]; 248a2295f0aSCédric Le Goater uint8_t sensor_unit1; 249a2295f0aSCédric Le Goater uint8_t sensor_unit2; 250a2295f0aSCédric Le Goater uint8_t sensor_unit3; 251a2295f0aSCédric Le Goater uint8_t sensor_direction[2]; /* byte 24 */ 252a2295f0aSCédric Le Goater uint8_t positive_threshold; 253a2295f0aSCédric Le Goater uint8_t negative_threshold; 254a2295f0aSCédric Le Goater uint8_t reserved[3]; 255a2295f0aSCédric Le Goater uint8_t oem; 256a2295f0aSCédric Le Goater uint8_t id_str_len; /* byte 32 */ 257a2295f0aSCédric Le Goater uint8_t id_string[16]; 258a2295f0aSCédric Le Goater }; 259a2295f0aSCédric Le Goater 260a2295f0aSCédric Le Goater typedef uint8_t ipmi_sdr_compact_buffer[sizeof(struct ipmi_sdr_compact)]; 261a2295f0aSCédric Le Goater 2627fabcdb9SCédric Le Goater int ipmi_bmc_sdr_find(IPMIBmc *b, uint16_t recid, 2637fabcdb9SCédric Le Goater const struct ipmi_sdr_compact **sdr, uint16_t *nextrec); 264cd60d85eSCédric Le Goater void ipmi_bmc_gen_event(IPMIBmc *b, uint8_t *evt, bool log); 265cd60d85eSCédric Le Goater 266ed8da05cSCédric Le Goater #define TYPE_IPMI_BMC_SIMULATOR "ipmi-bmc-sim" 267db1015e9SEduardo Habkost typedef struct IPMIBmcSim IPMIBmcSim; 2688110fa1dSEduardo Habkost DECLARE_INSTANCE_CHECKER(IPMIBmcSim, IPMI_BMC_SIMULATOR, 269ed8da05cSCédric Le Goater TYPE_IPMI_BMC_SIMULATOR) 270ed8da05cSCédric Le Goater 271ed8da05cSCédric Le Goater 272ed8da05cSCédric Le Goater typedef struct RspBuffer { 273ed8da05cSCédric Le Goater uint8_t buffer[MAX_IPMI_MSG_SIZE]; 274ed8da05cSCédric Le Goater unsigned int len; 275ed8da05cSCédric Le Goater } RspBuffer; 276ed8da05cSCédric Le Goater 277ed8da05cSCédric Le Goater static inline void rsp_buffer_set_error(RspBuffer *rsp, uint8_t byte) 278ed8da05cSCédric Le Goater { 279ed8da05cSCédric Le Goater rsp->buffer[2] = byte; 280ed8da05cSCédric Le Goater } 281ed8da05cSCédric Le Goater 282ed8da05cSCédric Le Goater /* Add a byte to the response. */ 283ed8da05cSCédric Le Goater static inline void rsp_buffer_push(RspBuffer *rsp, uint8_t byte) 284ed8da05cSCédric Le Goater { 285ed8da05cSCédric Le Goater if (rsp->len >= sizeof(rsp->buffer)) { 286ed8da05cSCédric Le Goater rsp_buffer_set_error(rsp, IPMI_CC_REQUEST_DATA_TRUNCATED); 287ed8da05cSCédric Le Goater return; 288ed8da05cSCédric Le Goater } 289ed8da05cSCédric Le Goater rsp->buffer[rsp->len++] = byte; 290ed8da05cSCédric Le Goater } 291ed8da05cSCédric Le Goater 292ed8da05cSCédric Le Goater typedef struct IPMICmdHandler { 293ed8da05cSCédric Le Goater void (*cmd_handler)(IPMIBmcSim *s, 294ed8da05cSCédric Le Goater uint8_t *cmd, unsigned int cmd_len, 295ed8da05cSCédric Le Goater RspBuffer *rsp); 296ed8da05cSCédric Le Goater unsigned int cmd_len_min; 297ed8da05cSCédric Le Goater } IPMICmdHandler; 298ed8da05cSCédric Le Goater 299ed8da05cSCédric Le Goater typedef struct IPMINetfn { 300ed8da05cSCédric Le Goater unsigned int cmd_nums; 301ed8da05cSCédric Le Goater const IPMICmdHandler *cmd_handlers; 302ed8da05cSCédric Le Goater } IPMINetfn; 303ed8da05cSCédric Le Goater 304ed8da05cSCédric Le Goater int ipmi_sim_register_netfn(IPMIBmcSim *s, unsigned int netfn, 305ed8da05cSCédric Le Goater const IPMINetfn *netfnd); 306ed8da05cSCédric Le Goater 30723076bb3SCorey Minyard #endif 308