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 #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 #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 /* Specified in the SMBIOS spec. */ 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 * Used for transferring information to interfaces that add 72 * entries to firmware tables. 73 */ 74 typedef 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 * Called by each instantiated IPMI interface device to get it's uuid. 101 */ 102 uint32_t ipmi_next_uuid(void); 103 104 /* IPMI Interface types (KCS, SMIC, BT) are prefixed with this */ 105 #define TYPE_IPMI_INTERFACE_PREFIX "ipmi-interface-" 106 107 /* 108 * An IPMI Interface, the interface for talking between the target 109 * and the BMC. 110 */ 111 #define TYPE_IPMI_INTERFACE "ipmi-interface" 112 #define IPMI_INTERFACE(obj) \ 113 INTERFACE_CHECK(IPMIInterface, (obj), TYPE_IPMI_INTERFACE) 114 typedef struct IPMIInterfaceClass IPMIInterfaceClass; 115 #define IPMI_INTERFACE_CLASS(class) \ 116 OBJECT_CLASS_CHECK(IPMIInterfaceClass, (class), TYPE_IPMI_INTERFACE) 117 #define IPMI_INTERFACE_GET_CLASS(class) \ 118 OBJECT_GET_CLASS(IPMIInterfaceClass, (class), TYPE_IPMI_INTERFACE) 119 120 typedef struct IPMIInterface IPMIInterface; 121 122 struct IPMIInterfaceClass { 123 InterfaceClass parent; 124 125 /* 126 * min_size is the requested I/O size and must be a power of 2. 127 * This is so PCI (or other busses) can request a bigger range. 128 * Use 0 for the default. 129 */ 130 void (*init)(struct IPMIInterface *s, unsigned int min_size, Error **errp); 131 132 /* 133 * Perform various operations on the hardware. If checkonly is 134 * true, it will return if the operation can be performed, but it 135 * will not do the operation. 136 */ 137 int (*do_hw_op)(struct IPMIInterface *s, enum ipmi_op op, int checkonly); 138 139 /* 140 * Enable/disable irqs on the interface when the BMC requests this. 141 */ 142 void (*set_irq_enable)(struct IPMIInterface *s, int val); 143 144 /* 145 * Handle an event that occurred on the interface, generally the. 146 * target writing to a register. 147 */ 148 void (*handle_if_event)(struct IPMIInterface *s); 149 150 /* 151 * The interfaces use this to perform certain ops 152 */ 153 void (*set_atn)(struct IPMIInterface *s, int val, int irq); 154 155 /* 156 * Got an IPMI warm/cold reset. 157 */ 158 void (*reset)(struct IPMIInterface *s, bool is_cold); 159 160 /* 161 * Handle a response from the bmc. 162 */ 163 void (*handle_rsp)(struct IPMIInterface *s, uint8_t msg_id, 164 unsigned char *rsp, unsigned int rsp_len); 165 166 /* 167 * Set by the owner to hold the backend data for the interface. 168 */ 169 void *(*get_backend_data)(struct IPMIInterface *s); 170 171 /* 172 * Return the firmware info for a device. 173 */ 174 void (*get_fwinfo)(struct IPMIInterface *s, IPMIFwInfo *info); 175 }; 176 177 /* 178 * Define a BMC simulator (or perhaps a connection to a real BMC) 179 */ 180 #define TYPE_IPMI_BMC "ipmi-bmc" 181 typedef struct IPMIBmc IPMIBmc; 182 typedef struct IPMIBmcClass IPMIBmcClass; 183 #define IPMI_BMC(obj) \ 184 OBJECT_CHECK(IPMIBmc, (obj), TYPE_IPMI_BMC) 185 #define IPMI_BMC_CLASS(obj_class) \ 186 OBJECT_CLASS_CHECK(IPMIBmcClass, (obj_class), TYPE_IPMI_BMC) 187 #define IPMI_BMC_GET_CLASS(obj) \ 188 OBJECT_GET_CLASS(IPMIBmcClass, (obj), TYPE_IPMI_BMC) 189 190 struct IPMIBmc { 191 DeviceState parent; 192 193 uint8_t slave_addr; 194 195 IPMIInterface *intf; 196 }; 197 198 struct IPMIBmcClass { 199 DeviceClass parent; 200 201 /* Called when the system resets to report to the bmc. */ 202 void (*handle_reset)(struct IPMIBmc *s); 203 204 /* 205 * Handle a command to the bmc. 206 */ 207 void (*handle_command)(struct IPMIBmc *s, 208 uint8_t *cmd, unsigned int cmd_len, 209 unsigned int max_cmd_len, 210 uint8_t msg_id); 211 }; 212 213 /* 214 * Add a link property to obj that points to a BMC. 215 */ 216 void ipmi_bmc_find_and_link(Object *obj, Object **bmc); 217 218 #ifdef IPMI_DEBUG 219 #define ipmi_debug(fs, ...) \ 220 fprintf(stderr, "IPMI (%s): " fs, __func__, ##__VA_ARGS__) 221 #else 222 #define ipmi_debug(fs, ...) 223 #endif 224 225 struct ipmi_sdr_header { 226 uint8_t rec_id[2]; 227 uint8_t sdr_version; /* 0x51 */ 228 uint8_t rec_type; 229 uint8_t rec_length; 230 }; 231 #define IPMI_SDR_HEADER_SIZE sizeof(struct ipmi_sdr_header) 232 233 #define ipmi_sdr_recid(sdr) ((sdr)->rec_id[0] | ((sdr)->rec_id[1] << 8)) 234 #define ipmi_sdr_length(sdr) ((sdr)->rec_length + IPMI_SDR_HEADER_SIZE) 235 236 /* 237 * 43.2 SDR Type 02h. Compact Sensor Record 238 */ 239 #define IPMI_SDR_COMPACT_TYPE 2 240 241 struct ipmi_sdr_compact { 242 struct ipmi_sdr_header header; 243 244 uint8_t sensor_owner_id; 245 uint8_t sensor_owner_lun; 246 uint8_t sensor_owner_number; /* byte 8 */ 247 uint8_t entity_id; 248 uint8_t entity_instance; 249 uint8_t sensor_init; 250 uint8_t sensor_caps; 251 uint8_t sensor_type; 252 uint8_t reading_type; 253 uint8_t assert_mask[2]; /* byte 16 */ 254 uint8_t deassert_mask[2]; 255 uint8_t discrete_mask[2]; 256 uint8_t sensor_unit1; 257 uint8_t sensor_unit2; 258 uint8_t sensor_unit3; 259 uint8_t sensor_direction[2]; /* byte 24 */ 260 uint8_t positive_threshold; 261 uint8_t negative_threshold; 262 uint8_t reserved[3]; 263 uint8_t oem; 264 uint8_t id_str_len; /* byte 32 */ 265 uint8_t id_string[16]; 266 }; 267 268 typedef uint8_t ipmi_sdr_compact_buffer[sizeof(struct ipmi_sdr_compact)]; 269 270 int ipmi_bmc_sdr_find(IPMIBmc *b, uint16_t recid, 271 const struct ipmi_sdr_compact **sdr, uint16_t *nextrec); 272 void ipmi_bmc_gen_event(IPMIBmc *b, uint8_t *evt, bool log); 273 274 #define TYPE_IPMI_BMC_SIMULATOR "ipmi-bmc-sim" 275 typedef struct IPMIBmcSim IPMIBmcSim; 276 #define IPMI_BMC_SIMULATOR(obj) OBJECT_CHECK(IPMIBmcSim, (obj), \ 277 TYPE_IPMI_BMC_SIMULATOR) 278 279 280 typedef struct RspBuffer { 281 uint8_t buffer[MAX_IPMI_MSG_SIZE]; 282 unsigned int len; 283 } RspBuffer; 284 285 static inline void rsp_buffer_set_error(RspBuffer *rsp, uint8_t byte) 286 { 287 rsp->buffer[2] = byte; 288 } 289 290 /* Add a byte to the response. */ 291 static inline void rsp_buffer_push(RspBuffer *rsp, uint8_t byte) 292 { 293 if (rsp->len >= sizeof(rsp->buffer)) { 294 rsp_buffer_set_error(rsp, IPMI_CC_REQUEST_DATA_TRUNCATED); 295 return; 296 } 297 rsp->buffer[rsp->len++] = byte; 298 } 299 300 typedef struct IPMICmdHandler { 301 void (*cmd_handler)(IPMIBmcSim *s, 302 uint8_t *cmd, unsigned int cmd_len, 303 RspBuffer *rsp); 304 unsigned int cmd_len_min; 305 } IPMICmdHandler; 306 307 typedef struct IPMINetfn { 308 unsigned int cmd_nums; 309 const IPMICmdHandler *cmd_handlers; 310 } IPMINetfn; 311 312 int ipmi_sim_register_netfn(IPMIBmcSim *s, unsigned int netfn, 313 const IPMINetfn *netfnd); 314 315 #endif 316