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