2 * IPMI BMC external connection
4 * Copyright (c) 2015 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
26 * This is designed to connect with OpenIPMI's lanserv serial interface
27 * using the "VM" connection type. See that for details.
30 #include "qemu/osdep.h"
31 #include "qapi/error.h"
32 #include "qemu/timer.h"
33 #include "sysemu/char.h"
34 #include "sysemu/sysemu.h"
35 #include "hw/ipmi/ipmi.h"
37 #define VM_MSG_CHAR 0xA0 /* Marks end of message */
38 #define VM_CMD_CHAR 0xA1 /* Marks end of a command */
39 #define VM_ESCAPE_CHAR 0xAA /* Set bit 4 from the next byte to 0 */
41 #define VM_PROTOCOL_VERSION 1
42 #define VM_CMD_VERSION 0xff /* A version number byte follows */
43 #define VM_CMD_NOATTN 0x00
44 #define VM_CMD_ATTN 0x01
45 #define VM_CMD_ATTN_IRQ 0x02
46 #define VM_CMD_POWEROFF 0x03
47 #define VM_CMD_RESET 0x04
48 #define VM_CMD_ENABLE_IRQ 0x05 /* Enable/disable the messaging irq */
49 #define VM_CMD_DISABLE_IRQ 0x06
50 #define VM_CMD_SEND_NMI 0x07
51 #define VM_CMD_CAPABILITIES 0x08
52 #define VM_CAPABILITIES_POWER 0x01
53 #define VM_CAPABILITIES_RESET 0x02
54 #define VM_CAPABILITIES_IRQ 0x04
55 #define VM_CAPABILITIES_NMI 0x08
56 #define VM_CAPABILITIES_ATTN 0x10
57 #define VM_CMD_FORCEOFF 0x09
59 #define TYPE_IPMI_BMC_EXTERN "ipmi-bmc-extern"
60 #define IPMI_BMC_EXTERN(obj) OBJECT_CHECK(IPMIBmcExtern, (obj), \
62 typedef struct IPMIBmcExtern
{
69 unsigned char inbuf
[MAX_IPMI_MSG_SIZE
+ 2];
76 unsigned char outbuf
[(MAX_IPMI_MSG_SIZE
+ 2) * 2 + 1];
80 struct QEMUTimer
*extern_timer
;
82 /* A reset event is pending to be sent upstream. */
86 static int can_receive(void *opaque
);
87 static void receive(void *opaque
, const uint8_t *buf
, int size
);
88 static void chr_event(void *opaque
, int event
);
91 ipmb_checksum(const unsigned char *data
, int size
, unsigned char start
)
93 unsigned char csum
= start
;
95 for (; size
> 0; size
--, data
++) {
101 static void continue_send(IPMIBmcExtern
*ibe
)
104 if (ibe
->outlen
== 0) {
108 ret
= qemu_chr_fe_write(&ibe
->chr
, ibe
->outbuf
+ ibe
->outpos
,
109 ibe
->outlen
- ibe
->outpos
);
113 if (ibe
->outpos
< ibe
->outlen
) {
114 /* Not fully transmitted, try again in a 10ms */
115 timer_mod_ns(ibe
->extern_timer
,
116 qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
) + 10000000);
121 if (!ibe
->sending_cmd
) {
122 ibe
->waiting_rsp
= true;
124 ibe
->sending_cmd
= false;
127 if (ibe
->connected
&& ibe
->send_reset
) {
129 ibe
->outbuf
[0] = VM_CMD_RESET
;
130 ibe
->outbuf
[1] = VM_CMD_CHAR
;
133 ibe
->send_reset
= false;
134 ibe
->sending_cmd
= true;
138 if (ibe
->waiting_rsp
) {
139 /* Make sure we get a response within 4 seconds. */
140 timer_mod_ns(ibe
->extern_timer
,
141 qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
) + 4000000000ULL);
147 static void extern_timeout(void *opaque
)
149 IPMIBmcExtern
*ibe
= opaque
;
150 IPMIInterface
*s
= ibe
->parent
.intf
;
152 if (ibe
->connected
) {
153 if (ibe
->waiting_rsp
&& (ibe
->outlen
== 0)) {
154 IPMIInterfaceClass
*k
= IPMI_INTERFACE_GET_CLASS(s
);
155 /* The message response timed out, return an error. */
156 ibe
->waiting_rsp
= false;
157 ibe
->inbuf
[1] = ibe
->outbuf
[1] | 0x04;
158 ibe
->inbuf
[2] = ibe
->outbuf
[2];
159 ibe
->inbuf
[3] = IPMI_CC_TIMEOUT
;
160 k
->handle_rsp(s
, ibe
->outbuf
[0], ibe
->inbuf
+ 1, 3);
167 static void addchar(IPMIBmcExtern
*ibe
, unsigned char ch
)
173 ibe
->outbuf
[ibe
->outlen
] = VM_ESCAPE_CHAR
;
179 ibe
->outbuf
[ibe
->outlen
] = ch
;
184 static void ipmi_bmc_extern_handle_command(IPMIBmc
*b
,
185 uint8_t *cmd
, unsigned int cmd_len
,
186 unsigned int max_cmd_len
,
189 IPMIBmcExtern
*ibe
= IPMI_BMC_EXTERN(b
);
190 IPMIInterface
*s
= ibe
->parent
.intf
;
191 uint8_t err
= 0, csum
;
195 /* We already have a command queued. Shouldn't ever happen. */
196 fprintf(stderr
, "IPMI KCS: Got command when not finished with the"
197 " previous command\n");
201 /* If it's too short or it was truncated, return an error. */
203 err
= IPMI_CC_REQUEST_DATA_LENGTH_INVALID
;
204 } else if ((cmd_len
> max_cmd_len
) || (cmd_len
> MAX_IPMI_MSG_SIZE
)) {
205 err
= IPMI_CC_REQUEST_DATA_TRUNCATED
;
206 } else if (!ibe
->connected
) {
207 err
= IPMI_CC_BMC_INIT_IN_PROGRESS
;
210 IPMIInterfaceClass
*k
= IPMI_INTERFACE_GET_CLASS(s
);
211 unsigned char rsp
[3];
212 rsp
[0] = cmd
[0] | 0x04;
215 ibe
->waiting_rsp
= false;
216 k
->handle_rsp(s
, msg_id
, rsp
, 3);
220 addchar(ibe
, msg_id
);
221 for (i
= 0; i
< cmd_len
; i
++) {
222 addchar(ibe
, cmd
[i
]);
224 csum
= ipmb_checksum(&msg_id
, 1, 0);
225 addchar(ibe
, -ipmb_checksum(cmd
, cmd_len
, csum
));
227 ibe
->outbuf
[ibe
->outlen
] = VM_MSG_CHAR
;
230 /* Start the transmit */
237 static void handle_hw_op(IPMIBmcExtern
*ibe
, unsigned char hw_op
)
239 IPMIInterface
*s
= ibe
->parent
.intf
;
240 IPMIInterfaceClass
*k
= IPMI_INTERFACE_GET_CLASS(s
);
244 /* We only support one version at this time. */
255 case VM_CMD_ATTN_IRQ
:
259 case VM_CMD_POWEROFF
:
260 k
->do_hw_op(s
, IPMI_POWEROFF_CHASSIS
, 0);
264 k
->do_hw_op(s
, IPMI_RESET_CHASSIS
, 0);
267 case VM_CMD_ENABLE_IRQ
:
268 k
->set_irq_enable(s
, 1);
271 case VM_CMD_DISABLE_IRQ
:
272 k
->set_irq_enable(s
, 0);
275 case VM_CMD_SEND_NMI
:
276 k
->do_hw_op(s
, IPMI_SEND_NMI
, 0);
279 case VM_CMD_FORCEOFF
:
280 qemu_system_shutdown_request();
285 static void handle_msg(IPMIBmcExtern
*ibe
)
287 IPMIInterfaceClass
*k
= IPMI_INTERFACE_GET_CLASS(ibe
->parent
.intf
);
289 if (ibe
->in_escape
) {
290 ipmi_debug("msg escape not ended\n");
293 if (ibe
->inpos
< 5) {
294 ipmi_debug("msg too short\n");
297 if (ibe
->in_too_many
) {
298 ibe
->inbuf
[3] = IPMI_CC_REQUEST_DATA_TRUNCATED
;
300 } else if (ipmb_checksum(ibe
->inbuf
, ibe
->inpos
, 0) != 0) {
301 ipmi_debug("msg checksum failure\n");
304 ibe
->inpos
--; /* Remove checkum */
307 timer_del(ibe
->extern_timer
);
308 ibe
->waiting_rsp
= false;
309 k
->handle_rsp(ibe
->parent
.intf
, ibe
->inbuf
[0], ibe
->inbuf
+ 1, ibe
->inpos
- 1);
312 static int can_receive(void *opaque
)
317 static void receive(void *opaque
, const uint8_t *buf
, int size
)
319 IPMIBmcExtern
*ibe
= opaque
;
323 for (i
= 0; i
< size
; i
++) {
324 unsigned char ch
= buf
[i
];
329 ibe
->in_too_many
= false;
334 if (ibe
->in_too_many
) {
335 ipmi_debug("cmd in too many\n");
336 ibe
->in_too_many
= false;
340 if (ibe
->in_escape
) {
341 ipmi_debug("cmd in escape\n");
342 ibe
->in_too_many
= false;
344 ibe
->in_escape
= false;
347 ibe
->in_too_many
= false;
348 if (ibe
->inpos
< 1) {
351 hw_op
= ibe
->inbuf
[0];
357 ibe
->in_escape
= true;
361 if (ibe
->in_escape
) {
363 ibe
->in_escape
= false;
365 if (ibe
->in_too_many
) {
368 if (ibe
->inpos
>= sizeof(ibe
->inbuf
)) {
369 ibe
->in_too_many
= true;
372 ibe
->inbuf
[ibe
->inpos
] = ch
;
380 handle_hw_op(ibe
, hw_op
);
383 static void chr_event(void *opaque
, int event
)
385 IPMIBmcExtern
*ibe
= opaque
;
386 IPMIInterface
*s
= ibe
->parent
.intf
;
387 IPMIInterfaceClass
*k
= IPMI_INTERFACE_GET_CLASS(s
);
391 case CHR_EVENT_OPENED
:
392 ibe
->connected
= true;
395 addchar(ibe
, VM_CMD_VERSION
);
396 addchar(ibe
, VM_PROTOCOL_VERSION
);
397 ibe
->outbuf
[ibe
->outlen
] = VM_CMD_CHAR
;
399 addchar(ibe
, VM_CMD_CAPABILITIES
);
400 v
= VM_CAPABILITIES_IRQ
| VM_CAPABILITIES_ATTN
;
401 if (k
->do_hw_op(ibe
->parent
.intf
, IPMI_POWEROFF_CHASSIS
, 1) == 0) {
402 v
|= VM_CAPABILITIES_POWER
;
404 if (k
->do_hw_op(ibe
->parent
.intf
, IPMI_RESET_CHASSIS
, 1) == 0) {
405 v
|= VM_CAPABILITIES_RESET
;
407 if (k
->do_hw_op(ibe
->parent
.intf
, IPMI_SEND_NMI
, 1) == 0) {
408 v
|= VM_CAPABILITIES_NMI
;
411 ibe
->outbuf
[ibe
->outlen
] = VM_CMD_CHAR
;
413 ibe
->sending_cmd
= false;
417 case CHR_EVENT_CLOSED
:
418 if (!ibe
->connected
) {
421 ibe
->connected
= false;
422 if (ibe
->waiting_rsp
) {
423 ibe
->waiting_rsp
= false;
424 ibe
->inbuf
[1] = ibe
->outbuf
[1] | 0x04;
425 ibe
->inbuf
[2] = ibe
->outbuf
[2];
426 ibe
->inbuf
[3] = IPMI_CC_BMC_INIT_IN_PROGRESS
;
427 k
->handle_rsp(s
, ibe
->outbuf
[0], ibe
->inbuf
+ 1, 3);
433 static void ipmi_bmc_extern_handle_reset(IPMIBmc
*b
)
435 IPMIBmcExtern
*ibe
= IPMI_BMC_EXTERN(b
);
437 ibe
->send_reset
= true;
441 static void ipmi_bmc_extern_realize(DeviceState
*dev
, Error
**errp
)
443 IPMIBmcExtern
*ibe
= IPMI_BMC_EXTERN(dev
);
445 if (!qemu_chr_fe_get_driver(&ibe
->chr
)) {
446 error_setg(errp
, "IPMI external bmc requires chardev attribute");
450 qemu_chr_fe_set_handlers(&ibe
->chr
, can_receive
, receive
,
451 chr_event
, ibe
, NULL
, true);
454 static int ipmi_bmc_extern_post_migrate(void *opaque
, int version_id
)
456 IPMIBmcExtern
*ibe
= opaque
;
459 * We don't directly restore waiting_rsp, Instead, we return an
460 * error on the interface if a response was being waited for.
462 if (ibe
->waiting_rsp
) {
463 IPMIInterface
*ii
= ibe
->parent
.intf
;
464 IPMIInterfaceClass
*iic
= IPMI_INTERFACE_GET_CLASS(ii
);
466 ibe
->waiting_rsp
= false;
467 ibe
->inbuf
[1] = ibe
->outbuf
[1] | 0x04;
468 ibe
->inbuf
[2] = ibe
->outbuf
[2];
469 ibe
->inbuf
[3] = IPMI_CC_BMC_INIT_IN_PROGRESS
;
470 iic
->handle_rsp(ii
, ibe
->outbuf
[0], ibe
->inbuf
+ 1, 3);
475 static const VMStateDescription vmstate_ipmi_bmc_extern
= {
476 .name
= TYPE_IPMI_BMC_EXTERN
,
478 .minimum_version_id
= 1,
479 .post_load
= ipmi_bmc_extern_post_migrate
,
480 .fields
= (VMStateField
[]) {
481 VMSTATE_BOOL(send_reset
, IPMIBmcExtern
),
482 VMSTATE_BOOL(waiting_rsp
, IPMIBmcExtern
),
483 VMSTATE_END_OF_LIST()
487 static void ipmi_bmc_extern_init(Object
*obj
)
489 IPMIBmcExtern
*ibe
= IPMI_BMC_EXTERN(obj
);
491 ibe
->extern_timer
= timer_new_ns(QEMU_CLOCK_VIRTUAL
, extern_timeout
, ibe
);
492 vmstate_register(NULL
, 0, &vmstate_ipmi_bmc_extern
, ibe
);
495 static void ipmi_bmc_extern_finalize(Object
*obj
)
497 IPMIBmcExtern
*ibe
= IPMI_BMC_EXTERN(obj
);
499 timer_del(ibe
->extern_timer
);
500 timer_free(ibe
->extern_timer
);
503 static Property ipmi_bmc_extern_properties
[] = {
504 DEFINE_PROP_CHR("chardev", IPMIBmcExtern
, chr
),
505 DEFINE_PROP_END_OF_LIST(),
508 static void ipmi_bmc_extern_class_init(ObjectClass
*oc
, void *data
)
510 DeviceClass
*dc
= DEVICE_CLASS(oc
);
511 IPMIBmcClass
*bk
= IPMI_BMC_CLASS(oc
);
513 bk
->handle_command
= ipmi_bmc_extern_handle_command
;
514 bk
->handle_reset
= ipmi_bmc_extern_handle_reset
;
515 dc
->realize
= ipmi_bmc_extern_realize
;
516 dc
->props
= ipmi_bmc_extern_properties
;
519 static const TypeInfo ipmi_bmc_extern_type
= {
520 .name
= TYPE_IPMI_BMC_EXTERN
,
521 .parent
= TYPE_IPMI_BMC
,
522 .instance_size
= sizeof(IPMIBmcExtern
),
523 .instance_init
= ipmi_bmc_extern_init
,
524 .instance_finalize
= ipmi_bmc_extern_finalize
,
525 .class_init
= ipmi_bmc_extern_class_init
,
528 static void ipmi_bmc_extern_register_types(void)
530 type_register_static(&ipmi_bmc_extern_type
);
533 type_init(ipmi_bmc_extern_register_types
)