2 * QEMU IPMI KCS emulation
4 * Copyright (c) 2015,2017 Corey Minyard, MontaVista Software, LLC
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:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
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
24 #include "qemu/osdep.h"
25 #include "migration/vmstate.h"
27 #include "qapi/error.h"
28 #include "hw/ipmi/ipmi_kcs.h"
30 #define IPMI_KCS_OBF_BIT 0
31 #define IPMI_KCS_IBF_BIT 1
32 #define IPMI_KCS_SMS_ATN_BIT 2
33 #define IPMI_KCS_CD_BIT 3
35 #define IPMI_KCS_OBF_MASK (1 << IPMI_KCS_OBF_BIT)
36 #define IPMI_KCS_GET_OBF(d) (((d) >> IPMI_KCS_OBF_BIT) & 0x1)
37 #define IPMI_KCS_SET_OBF(d, v) (d) = (((d) & ~IPMI_KCS_OBF_MASK) | \
38 (((v) & 1) << IPMI_KCS_OBF_BIT))
39 #define IPMI_KCS_IBF_MASK (1 << IPMI_KCS_IBF_BIT)
40 #define IPMI_KCS_GET_IBF(d) (((d) >> IPMI_KCS_IBF_BIT) & 0x1)
41 #define IPMI_KCS_SET_IBF(d, v) (d) = (((d) & ~IPMI_KCS_IBF_MASK) | \
42 (((v) & 1) << IPMI_KCS_IBF_BIT))
43 #define IPMI_KCS_SMS_ATN_MASK (1 << IPMI_KCS_SMS_ATN_BIT)
44 #define IPMI_KCS_GET_SMS_ATN(d) (((d) >> IPMI_KCS_SMS_ATN_BIT) & 0x1)
45 #define IPMI_KCS_SET_SMS_ATN(d, v) (d) = (((d) & ~IPMI_KCS_SMS_ATN_MASK) | \
46 (((v) & 1) << IPMI_KCS_SMS_ATN_BIT))
47 #define IPMI_KCS_CD_MASK (1 << IPMI_KCS_CD_BIT)
48 #define IPMI_KCS_GET_CD(d) (((d) >> IPMI_KCS_CD_BIT) & 0x1)
49 #define IPMI_KCS_SET_CD(d, v) (d) = (((d) & ~IPMI_KCS_CD_MASK) | \
50 (((v) & 1) << IPMI_KCS_CD_BIT))
52 #define IPMI_KCS_IDLE_STATE 0
53 #define IPMI_KCS_READ_STATE 1
54 #define IPMI_KCS_WRITE_STATE 2
55 #define IPMI_KCS_ERROR_STATE 3
57 #define IPMI_KCS_GET_STATE(d) (((d) >> 6) & 0x3)
58 #define IPMI_KCS_SET_STATE(d, v) ((d) = ((d) & ~0xc0) | (((v) & 0x3) << 6))
60 #define IPMI_KCS_ABORT_STATUS_CMD 0x60
61 #define IPMI_KCS_WRITE_START_CMD 0x61
62 #define IPMI_KCS_WRITE_END_CMD 0x62
63 #define IPMI_KCS_READ_CMD 0x68
65 #define IPMI_KCS_STATUS_NO_ERR 0x00
66 #define IPMI_KCS_STATUS_ABORTED_ERR 0x01
67 #define IPMI_KCS_STATUS_BAD_CC_ERR 0x02
68 #define IPMI_KCS_STATUS_LENGTH_ERR 0x06
70 static void ipmi_kcs_raise_irq(IPMIKCS
*ik
)
72 if (ik
->use_irq
&& ik
->irqs_enabled
&& ik
->raise_irq
) {
77 static void ipmi_kcs_lower_irq(IPMIKCS
*ik
)
86 IPMI_KCS_SET_OBF(ik->status_reg, 1); \
87 if (!ik->obf_irq_set) { \
88 ik->obf_irq_set = 1; \
89 if (!ik->atn_irq_set) { \
90 ipmi_kcs_raise_irq(ik); \
95 static void ipmi_kcs_signal(IPMIKCS
*ik
, IPMIInterface
*ii
)
97 IPMIInterfaceClass
*iic
= IPMI_INTERFACE_GET_CLASS(ii
);
100 while (ik
->do_wake
) {
102 iic
->handle_if_event(ii
);
106 static void ipmi_kcs_handle_event(IPMIInterface
*ii
)
108 IPMIInterfaceClass
*iic
= IPMI_INTERFACE_GET_CLASS(ii
);
109 IPMIKCS
*ik
= iic
->get_backend_data(ii
);
111 if (ik
->cmd_reg
== IPMI_KCS_ABORT_STATUS_CMD
) {
112 if (IPMI_KCS_GET_STATE(ik
->status_reg
) != IPMI_KCS_ERROR_STATE
) {
113 ik
->waiting_rsp
++; /* Invalidate the message */
114 ik
->outmsg
[0] = IPMI_KCS_STATUS_ABORTED_ERR
;
117 IPMI_KCS_SET_STATE(ik
->status_reg
, IPMI_KCS_ERROR_STATE
);
123 switch (IPMI_KCS_GET_STATE(ik
->status_reg
)) {
124 case IPMI_KCS_IDLE_STATE
:
125 if (ik
->cmd_reg
== IPMI_KCS_WRITE_START_CMD
) {
126 IPMI_KCS_SET_STATE(ik
->status_reg
, IPMI_KCS_WRITE_STATE
);
134 case IPMI_KCS_READ_STATE
:
136 if (ik
->outpos
>= ik
->outlen
) {
137 IPMI_KCS_SET_STATE(ik
->status_reg
, IPMI_KCS_IDLE_STATE
);
139 } else if (ik
->data_in_reg
== IPMI_KCS_READ_CMD
) {
140 ik
->data_out_reg
= ik
->outmsg
[ik
->outpos
];
144 ik
->outmsg
[0] = IPMI_KCS_STATUS_BAD_CC_ERR
;
147 IPMI_KCS_SET_STATE(ik
->status_reg
, IPMI_KCS_ERROR_STATE
);
153 case IPMI_KCS_WRITE_STATE
:
154 if (ik
->data_in_reg
!= -1) {
156 * Don't worry about input overrun here, that will be
157 * handled in the BMC.
159 if (ik
->inlen
< sizeof(ik
->inmsg
)) {
160 ik
->inmsg
[ik
->inlen
] = ik
->data_in_reg
;
165 IPMIBmcClass
*bk
= IPMI_BMC_GET_CLASS(ik
->bmc
);
169 bk
->handle_command(ik
->bmc
, ik
->inmsg
, ik
->inlen
, sizeof(ik
->inmsg
),
172 } else if (ik
->cmd_reg
== IPMI_KCS_WRITE_END_CMD
) {
179 case IPMI_KCS_ERROR_STATE
:
180 if (ik
->data_in_reg
!= -1) {
181 IPMI_KCS_SET_STATE(ik
->status_reg
, IPMI_KCS_READ_STATE
);
182 ik
->data_in_reg
= IPMI_KCS_READ_CMD
;
188 if (ik
->cmd_reg
!= -1) {
189 /* Got an invalid command */
190 ik
->outmsg
[0] = IPMI_KCS_STATUS_BAD_CC_ERR
;
193 IPMI_KCS_SET_STATE(ik
->status_reg
, IPMI_KCS_ERROR_STATE
);
198 ik
->data_in_reg
= -1;
199 IPMI_KCS_SET_IBF(ik
->status_reg
, 0);
204 static void ipmi_kcs_handle_rsp(IPMIInterface
*ii
, uint8_t msg_id
,
205 unsigned char *rsp
, unsigned int rsp_len
)
207 IPMIInterfaceClass
*iic
= IPMI_INTERFACE_GET_CLASS(ii
);
208 IPMIKCS
*ik
= iic
->get_backend_data(ii
);
210 if (ik
->waiting_rsp
== msg_id
) {
212 if (rsp_len
> sizeof(ik
->outmsg
)) {
213 ik
->outmsg
[0] = rsp
[0];
214 ik
->outmsg
[1] = rsp
[1];
215 ik
->outmsg
[2] = IPMI_CC_CANNOT_RETURN_REQ_NUM_BYTES
;
218 memcpy(ik
->outmsg
, rsp
, rsp_len
);
219 ik
->outlen
= rsp_len
;
221 IPMI_KCS_SET_STATE(ik
->status_reg
, IPMI_KCS_READ_STATE
);
222 ik
->data_in_reg
= IPMI_KCS_READ_CMD
;
223 ipmi_kcs_signal(ik
, ii
);
228 static uint64_t ipmi_kcs_ioport_read(void *opaque
, hwaddr addr
, unsigned size
)
230 IPMIInterface
*ii
= opaque
;
231 IPMIInterfaceClass
*iic
= IPMI_INTERFACE_GET_CLASS(ii
);
232 IPMIKCS
*ik
= iic
->get_backend_data(ii
);
235 switch (addr
& ik
->size_mask
) {
237 ret
= ik
->data_out_reg
;
238 IPMI_KCS_SET_OBF(ik
->status_reg
, 0);
239 if (ik
->obf_irq_set
) {
241 if (!ik
->atn_irq_set
) {
242 ipmi_kcs_lower_irq(ik
);
248 ret
= ik
->status_reg
;
249 if (ik
->atn_irq_set
) {
251 if (!ik
->obf_irq_set
) {
252 ipmi_kcs_lower_irq(ik
);
263 static void ipmi_kcs_ioport_write(void *opaque
, hwaddr addr
, uint64_t val
,
266 IPMIInterface
*ii
= opaque
;
267 IPMIInterfaceClass
*iic
= IPMI_INTERFACE_GET_CLASS(ii
);
268 IPMIKCS
*ik
= iic
->get_backend_data(ii
);
270 if (IPMI_KCS_GET_IBF(ik
->status_reg
)) {
274 switch (addr
& ik
->size_mask
) {
276 ik
->data_in_reg
= val
;
287 IPMI_KCS_SET_IBF(ik
->status_reg
, 1);
288 ipmi_kcs_signal(ik
, ii
);
291 const MemoryRegionOps ipmi_kcs_io_ops
= {
292 .read
= ipmi_kcs_ioport_read
,
293 .write
= ipmi_kcs_ioport_write
,
295 .min_access_size
= 1,
296 .max_access_size
= 1,
298 .endianness
= DEVICE_LITTLE_ENDIAN
,
301 static void ipmi_kcs_set_atn(IPMIInterface
*ii
, int val
, int irq
)
303 IPMIInterfaceClass
*iic
= IPMI_INTERFACE_GET_CLASS(ii
);
304 IPMIKCS
*ik
= iic
->get_backend_data(ii
);
306 IPMI_KCS_SET_SMS_ATN(ik
->status_reg
, val
);
308 if (irq
&& !ik
->atn_irq_set
) {
310 if (!ik
->obf_irq_set
) {
311 ipmi_kcs_raise_irq(ik
);
315 if (ik
->atn_irq_set
) {
317 if (!ik
->obf_irq_set
) {
318 ipmi_kcs_lower_irq(ik
);
324 static void ipmi_kcs_set_irq_enable(IPMIInterface
*ii
, int val
)
326 IPMIInterfaceClass
*iic
= IPMI_INTERFACE_GET_CLASS(ii
);
327 IPMIKCS
*ik
= iic
->get_backend_data(ii
);
329 ik
->irqs_enabled
= val
;
332 /* min_size must be a power of 2. */
333 static void ipmi_kcs_init(IPMIInterface
*ii
, unsigned int min_size
,
336 IPMIInterfaceClass
*iic
= IPMI_INTERFACE_GET_CLASS(ii
);
337 IPMIKCS
*ik
= iic
->get_backend_data(ii
);
342 ik
->size_mask
= min_size
- 1;
344 memory_region_init_io(&ik
->io
, NULL
, &ipmi_kcs_io_ops
, ii
, "ipmi-kcs",
348 int ipmi_kcs_vmstate_post_load(void *opaque
, int version
)
350 IPMIKCS
*ik
= opaque
;
352 /* Make sure all the values are sane. */
353 if (ik
->outpos
>= MAX_IPMI_MSG_SIZE
|| ik
->outlen
>= MAX_IPMI_MSG_SIZE
||
354 ik
->outpos
>= ik
->outlen
) {
355 qemu_log_mask(LOG_GUEST_ERROR
,
356 "ipmi:kcs: vmstate transfer received bad out values: %d %d\n",
357 ik
->outpos
, ik
->outlen
);
362 if (ik
->inlen
>= MAX_IPMI_MSG_SIZE
) {
363 qemu_log_mask(LOG_GUEST_ERROR
,
364 "ipmi:kcs: vmstate transfer received bad in value: %d\n",
372 static bool vmstate_kcs_before_version2(void *opaque
, int version
)
377 const VMStateDescription vmstate_IPMIKCS
= {
378 .name
= TYPE_IPMI_INTERFACE_PREFIX
"kcs",
380 .minimum_version_id
= 1,
381 .post_load
= ipmi_kcs_vmstate_post_load
,
382 .fields
= (VMStateField
[]) {
383 VMSTATE_BOOL(obf_irq_set
, IPMIKCS
),
384 VMSTATE_BOOL(atn_irq_set
, IPMIKCS
),
385 VMSTATE_UNUSED_TEST(vmstate_kcs_before_version2
, 1), /* Was use_irq */
386 VMSTATE_BOOL(irqs_enabled
, IPMIKCS
),
387 VMSTATE_UINT32(outpos
, IPMIKCS
),
388 VMSTATE_UINT32_V(outlen
, IPMIKCS
, 2),
389 VMSTATE_UINT8_ARRAY(outmsg
, IPMIKCS
, MAX_IPMI_MSG_SIZE
),
390 VMSTATE_UINT32_V(inlen
, IPMIKCS
, 2),
391 VMSTATE_UINT8_ARRAY(inmsg
, IPMIKCS
, MAX_IPMI_MSG_SIZE
),
392 VMSTATE_BOOL(write_end
, IPMIKCS
),
393 VMSTATE_UINT8(status_reg
, IPMIKCS
),
394 VMSTATE_UINT8(data_out_reg
, IPMIKCS
),
395 VMSTATE_INT16(data_in_reg
, IPMIKCS
),
396 VMSTATE_INT16(cmd_reg
, IPMIKCS
),
397 VMSTATE_UINT8(waiting_rsp
, IPMIKCS
),
398 VMSTATE_END_OF_LIST()
402 void ipmi_kcs_get_fwinfo(IPMIKCS
*ik
, IPMIFwInfo
*info
)
404 info
->interface_name
= "kcs";
405 info
->interface_type
= IPMI_SMBIOS_KCS
;
406 info
->ipmi_spec_major_revision
= 2;
407 info
->ipmi_spec_minor_revision
= 0;
408 info
->base_address
= ik
->io_base
;
409 info
->i2c_slave_address
= ik
->bmc
->slave_addr
;
410 info
->register_length
= ik
->io_length
;
411 info
->register_spacing
= 1;
412 info
->memspace
= IPMI_MEMSPACE_IO
;
413 info
->irq_type
= IPMI_LEVEL_IRQ
;
416 void ipmi_kcs_class_init(IPMIInterfaceClass
*iic
)
418 iic
->init
= ipmi_kcs_init
;
419 iic
->set_atn
= ipmi_kcs_set_atn
;
420 iic
->handle_rsp
= ipmi_kcs_handle_rsp
;
421 iic
->handle_if_event
= ipmi_kcs_handle_event
;
422 iic
->set_irq_enable
= ipmi_kcs_set_irq_enable
;