2 * A bus for connecting virtio serial and console ports
4 * Copyright (C) 2009, 2010 Red Hat, Inc.
7 * Amit Shah <amit.shah@redhat.com>
9 * Some earlier parts are:
10 * Copyright IBM, Corp. 2008
12 * Christian Ehrhardt <ehrhardt@linux.vnet.ibm.com>
14 * This work is licensed under the terms of the GNU GPL, version 2. See
15 * the COPYING file in the top-level directory.
20 #include "qemu-queue.h"
23 #include "virtio-serial.h"
25 /* The virtio-serial bus on top of which the ports will ride as devices */
26 struct VirtIOSerialBus
{
29 /* This is the parent device that provides the bus for ports. */
32 /* The maximum number of ports that can ride on top of this bus */
33 uint32_t max_nr_ports
;
39 VirtQueue
*c_ivq
, *c_ovq
;
40 /* Arrays of ivqs and ovqs: one per port */
41 VirtQueue
**ivqs
, **ovqs
;
47 QTAILQ_HEAD(, VirtIOSerialPort
) ports
;
49 /* bitmap for identifying active ports */
52 struct virtio_console_config config
;
55 static VirtIOSerialPort
*find_port_by_id(VirtIOSerial
*vser
, uint32_t id
)
57 VirtIOSerialPort
*port
;
59 if (id
== VIRTIO_CONSOLE_BAD_ID
) {
63 QTAILQ_FOREACH(port
, &vser
->ports
, next
) {
70 static VirtIOSerialPort
*find_port_by_vq(VirtIOSerial
*vser
, VirtQueue
*vq
)
72 VirtIOSerialPort
*port
;
74 QTAILQ_FOREACH(port
, &vser
->ports
, next
) {
75 if (port
->ivq
== vq
|| port
->ovq
== vq
)
81 static bool use_multiport(VirtIOSerial
*vser
)
83 return vser
->vdev
.guest_features
& (1 << VIRTIO_CONSOLE_F_MULTIPORT
);
86 static size_t write_to_port(VirtIOSerialPort
*port
,
87 const uint8_t *buf
, size_t size
)
89 VirtQueueElement elem
;
94 if (!virtio_queue_ready(vq
)) {
99 while (offset
< size
) {
102 if (!virtqueue_pop(vq
, &elem
)) {
106 len
= iov_from_buf(elem
.in_sg
, elem
.in_num
,
107 buf
+ offset
, 0, size
- offset
);
110 virtqueue_push(vq
, &elem
, len
);
113 virtio_notify(&port
->vser
->vdev
, vq
);
117 static void discard_vq_data(VirtQueue
*vq
, VirtIODevice
*vdev
)
119 VirtQueueElement elem
;
121 if (!virtio_queue_ready(vq
)) {
124 while (virtqueue_pop(vq
, &elem
)) {
125 virtqueue_push(vq
, &elem
, 0);
127 virtio_notify(vdev
, vq
);
130 static void do_flush_queued_data(VirtIOSerialPort
*port
, VirtQueue
*vq
,
133 VirtIOSerialPortInfo
*info
;
136 assert(virtio_queue_ready(vq
));
138 info
= DO_UPCAST(VirtIOSerialPortInfo
, qdev
, port
->dev
.info
);
140 while (!port
->throttled
) {
143 /* Pop an elem only if we haven't left off a previous one mid-way */
144 if (!port
->elem
.out_num
) {
145 if (!virtqueue_pop(vq
, &port
->elem
)) {
149 port
->iov_offset
= 0;
152 for (i
= port
->iov_idx
; i
< port
->elem
.out_num
; i
++) {
156 buf_size
= port
->elem
.out_sg
[i
].iov_len
- port
->iov_offset
;
157 ret
= info
->have_data(port
,
158 port
->elem
.out_sg
[i
].iov_base
161 if (ret
< 0 && ret
!= -EAGAIN
) {
162 /* We don't handle any other type of errors here */
165 if (ret
== -EAGAIN
|| (ret
>= 0 && ret
< buf_size
)) {
167 * this is a temporary check until chardevs can signal to
168 * frontends that they are writable again. This prevents
169 * the console from going into throttled mode (forever)
170 * if virtio-console is connected to a pty without a
171 * listener. Otherwise the guest spins forever.
172 * We can revert this if
173 * 1: chardevs can notify frondends
174 * 2: the guest driver does not spin in these cases
176 if (!info
->is_console
) {
177 virtio_serial_throttle_port(port
, true);
181 port
->iov_offset
+= ret
;
185 port
->iov_offset
= 0;
187 if (port
->throttled
) {
190 virtqueue_push(vq
, &port
->elem
, 0);
191 port
->elem
.out_num
= 0;
193 virtio_notify(vdev
, vq
);
196 static void flush_queued_data(VirtIOSerialPort
*port
)
200 if (!virtio_queue_ready(port
->ovq
)) {
203 do_flush_queued_data(port
, port
->ovq
, &port
->vser
->vdev
);
206 static size_t send_control_msg(VirtIOSerialPort
*port
, void *buf
, size_t len
)
208 VirtQueueElement elem
;
210 struct virtio_console_control
*cpkt
;
212 vq
= port
->vser
->c_ivq
;
213 if (!virtio_queue_ready(vq
)) {
216 if (!virtqueue_pop(vq
, &elem
)) {
220 cpkt
= (struct virtio_console_control
*)buf
;
221 stl_p(&cpkt
->id
, port
->id
);
222 memcpy(elem
.in_sg
[0].iov_base
, buf
, len
);
224 virtqueue_push(vq
, &elem
, len
);
225 virtio_notify(&port
->vser
->vdev
, vq
);
229 static size_t send_control_event(VirtIOSerialPort
*port
, uint16_t event
,
232 struct virtio_console_control cpkt
;
234 stw_p(&cpkt
.event
, event
);
235 stw_p(&cpkt
.value
, value
);
237 trace_virtio_serial_send_control_event(port
->id
, event
, value
);
238 return send_control_msg(port
, &cpkt
, sizeof(cpkt
));
241 /* Functions for use inside qemu to open and read from/write to ports */
242 int virtio_serial_open(VirtIOSerialPort
*port
)
244 /* Don't allow opening an already-open port */
245 if (port
->host_connected
) {
248 /* Send port open notification to the guest */
249 port
->host_connected
= true;
250 send_control_event(port
, VIRTIO_CONSOLE_PORT_OPEN
, 1);
255 int virtio_serial_close(VirtIOSerialPort
*port
)
257 port
->host_connected
= false;
259 * If there's any data the guest sent which the app didn't
260 * consume, reset the throttling flag and discard the data.
262 port
->throttled
= false;
263 discard_vq_data(port
->ovq
, &port
->vser
->vdev
);
265 send_control_event(port
, VIRTIO_CONSOLE_PORT_OPEN
, 0);
270 /* Individual ports/apps call this function to write to the guest. */
271 ssize_t
virtio_serial_write(VirtIOSerialPort
*port
, const uint8_t *buf
,
274 if (!port
|| !port
->host_connected
|| !port
->guest_connected
) {
277 return write_to_port(port
, buf
, size
);
281 * Readiness of the guest to accept data on a port.
282 * Returns max. data the guest can receive
284 size_t virtio_serial_guest_ready(VirtIOSerialPort
*port
)
286 VirtQueue
*vq
= port
->ivq
;
288 if (!virtio_queue_ready(vq
) ||
289 !(port
->vser
->vdev
.status
& VIRTIO_CONFIG_S_DRIVER_OK
) ||
290 virtio_queue_empty(vq
)) {
293 if (use_multiport(port
->vser
) && !port
->guest_connected
) {
297 if (virtqueue_avail_bytes(vq
, 4096, 0)) {
300 if (virtqueue_avail_bytes(vq
, 1, 0)) {
306 static void flush_queued_data_bh(void *opaque
)
308 VirtIOSerialPort
*port
= opaque
;
310 flush_queued_data(port
);
313 void virtio_serial_throttle_port(VirtIOSerialPort
*port
, bool throttle
)
319 trace_virtio_serial_throttle_port(port
->id
, throttle
);
320 port
->throttled
= throttle
;
324 qemu_bh_schedule(port
->bh
);
327 /* Guest wants to notify us of some event */
328 static void handle_control_message(VirtIOSerial
*vser
, void *buf
, size_t len
)
330 struct VirtIOSerialPort
*port
;
331 struct VirtIOSerialPortInfo
*info
;
332 struct virtio_console_control cpkt
, *gcpkt
;
338 if (len
< sizeof(cpkt
)) {
339 /* The guest sent an invalid control packet */
343 cpkt
.event
= lduw_p(&gcpkt
->event
);
344 cpkt
.value
= lduw_p(&gcpkt
->value
);
346 trace_virtio_serial_handle_control_message(cpkt
.event
, cpkt
.value
);
348 if (cpkt
.event
== VIRTIO_CONSOLE_DEVICE_READY
) {
350 error_report("virtio-serial-bus: Guest failure in adding device %s",
351 vser
->bus
.qbus
.name
);
355 * The device is up, we can now tell the device about all the
356 * ports we have here.
358 QTAILQ_FOREACH(port
, &vser
->ports
, next
) {
359 send_control_event(port
, VIRTIO_CONSOLE_PORT_ADD
, 1);
364 port
= find_port_by_id(vser
, ldl_p(&gcpkt
->id
));
366 error_report("virtio-serial-bus: Unexpected port id %u for device %s",
367 ldl_p(&gcpkt
->id
), vser
->bus
.qbus
.name
);
371 trace_virtio_serial_handle_control_message_port(port
->id
);
373 info
= DO_UPCAST(VirtIOSerialPortInfo
, qdev
, port
->dev
.info
);
376 case VIRTIO_CONSOLE_PORT_READY
:
378 error_report("virtio-serial-bus: Guest failure in adding port %u for device %s",
379 port
->id
, vser
->bus
.qbus
.name
);
383 * Now that we know the guest asked for the port name, we're
384 * sure the guest has initialised whatever state is necessary
385 * for this port. Now's a good time to let the guest know if
386 * this port is a console port so that the guest can hook it
389 if (info
->is_console
) {
390 send_control_event(port
, VIRTIO_CONSOLE_CONSOLE_PORT
, 1);
394 stw_p(&cpkt
.event
, VIRTIO_CONSOLE_PORT_NAME
);
395 stw_p(&cpkt
.value
, 1);
397 buffer_len
= sizeof(cpkt
) + strlen(port
->name
) + 1;
398 buffer
= g_malloc(buffer_len
);
400 memcpy(buffer
, &cpkt
, sizeof(cpkt
));
401 memcpy(buffer
+ sizeof(cpkt
), port
->name
, strlen(port
->name
));
402 buffer
[buffer_len
- 1] = 0;
404 send_control_msg(port
, buffer
, buffer_len
);
408 if (port
->host_connected
) {
409 send_control_event(port
, VIRTIO_CONSOLE_PORT_OPEN
, 1);
413 * When the guest has asked us for this information it means
414 * the guest is all setup and has its virtqueues
415 * initialised. If some app is interested in knowing about
416 * this event, let it know.
418 if (info
->guest_ready
) {
419 info
->guest_ready(port
);
423 case VIRTIO_CONSOLE_PORT_OPEN
:
424 port
->guest_connected
= cpkt
.value
;
425 if (cpkt
.value
&& info
->guest_open
) {
426 /* Send the guest opened notification if an app is interested */
427 info
->guest_open(port
);
430 if (!cpkt
.value
&& info
->guest_close
) {
431 /* Send the guest closed notification if an app is interested */
432 info
->guest_close(port
);
438 static void control_in(VirtIODevice
*vdev
, VirtQueue
*vq
)
442 static void control_out(VirtIODevice
*vdev
, VirtQueue
*vq
)
444 VirtQueueElement elem
;
449 vser
= DO_UPCAST(VirtIOSerial
, vdev
, vdev
);
453 while (virtqueue_pop(vq
, &elem
)) {
454 size_t cur_len
, copied
;
456 cur_len
= iov_size(elem
.out_sg
, elem
.out_num
);
458 * Allocate a new buf only if we didn't have one previously or
459 * if the size of the buf differs
464 buf
= g_malloc(cur_len
);
467 copied
= iov_to_buf(elem
.out_sg
, elem
.out_num
, buf
, 0, len
);
469 handle_control_message(vser
, buf
, copied
);
470 virtqueue_push(vq
, &elem
, 0);
473 virtio_notify(vdev
, vq
);
476 /* Guest wrote something to some port. */
477 static void handle_output(VirtIODevice
*vdev
, VirtQueue
*vq
)
480 VirtIOSerialPort
*port
;
482 vser
= DO_UPCAST(VirtIOSerial
, vdev
, vdev
);
483 port
= find_port_by_vq(vser
, vq
);
485 if (!port
|| !port
->host_connected
) {
486 discard_vq_data(vq
, vdev
);
490 if (!port
->throttled
) {
491 do_flush_queued_data(port
, vq
, vdev
);
496 static void handle_input(VirtIODevice
*vdev
, VirtQueue
*vq
)
500 static uint32_t get_features(VirtIODevice
*vdev
, uint32_t features
)
504 vser
= DO_UPCAST(VirtIOSerial
, vdev
, vdev
);
506 if (vser
->bus
.max_nr_ports
> 1) {
507 features
|= (1 << VIRTIO_CONSOLE_F_MULTIPORT
);
512 /* Guest requested config info */
513 static void get_config(VirtIODevice
*vdev
, uint8_t *config_data
)
517 vser
= DO_UPCAST(VirtIOSerial
, vdev
, vdev
);
518 memcpy(config_data
, &vser
->config
, sizeof(struct virtio_console_config
));
521 static void set_config(VirtIODevice
*vdev
, const uint8_t *config_data
)
523 struct virtio_console_config config
;
525 memcpy(&config
, config_data
, sizeof(config
));
528 static void virtio_serial_save(QEMUFile
*f
, void *opaque
)
530 VirtIOSerial
*s
= opaque
;
531 VirtIOSerialPort
*port
;
532 uint32_t nr_active_ports
;
533 unsigned int i
, max_nr_ports
;
535 /* The virtio device */
536 virtio_save(&s
->vdev
, f
);
538 /* The config space */
539 qemu_put_be16s(f
, &s
->config
.cols
);
540 qemu_put_be16s(f
, &s
->config
.rows
);
542 qemu_put_be32s(f
, &s
->config
.max_nr_ports
);
545 max_nr_ports
= tswap32(s
->config
.max_nr_ports
);
546 for (i
= 0; i
< (max_nr_ports
+ 31) / 32; i
++) {
547 qemu_put_be32s(f
, &s
->ports_map
[i
]);
553 QTAILQ_FOREACH(port
, &s
->ports
, next
) {
557 qemu_put_be32s(f
, &nr_active_ports
);
560 * Items in struct VirtIOSerialPort.
562 QTAILQ_FOREACH(port
, &s
->ports
, next
) {
563 uint32_t elem_popped
;
565 qemu_put_be32s(f
, &port
->id
);
566 qemu_put_byte(f
, port
->guest_connected
);
567 qemu_put_byte(f
, port
->host_connected
);
570 if (port
->elem
.out_num
) {
573 qemu_put_be32s(f
, &elem_popped
);
575 qemu_put_be32s(f
, &port
->iov_idx
);
576 qemu_put_be64s(f
, &port
->iov_offset
);
578 qemu_put_buffer(f
, (unsigned char *)&port
->elem
,
584 static int virtio_serial_load(QEMUFile
*f
, void *opaque
, int version_id
)
586 VirtIOSerial
*s
= opaque
;
587 VirtIOSerialPort
*port
;
588 uint32_t max_nr_ports
, nr_active_ports
, ports_map
;
591 if (version_id
> 3) {
595 /* The virtio device */
596 virtio_load(&s
->vdev
, f
);
598 if (version_id
< 2) {
602 /* The config space */
603 qemu_get_be16s(f
, &s
->config
.cols
);
604 qemu_get_be16s(f
, &s
->config
.rows
);
606 qemu_get_be32s(f
, &max_nr_ports
);
607 tswap32s(&max_nr_ports
);
608 if (max_nr_ports
> tswap32(s
->config
.max_nr_ports
)) {
609 /* Source could have had more ports than us. Fail migration. */
613 for (i
= 0; i
< (max_nr_ports
+ 31) / 32; i
++) {
614 qemu_get_be32s(f
, &ports_map
);
616 if (ports_map
!= s
->ports_map
[i
]) {
618 * Ports active on source and destination don't
619 * match. Fail migration.
625 qemu_get_be32s(f
, &nr_active_ports
);
627 /* Items in struct VirtIOSerialPort */
628 for (i
= 0; i
< nr_active_ports
; i
++) {
632 id
= qemu_get_be32(f
);
633 port
= find_port_by_id(s
, id
);
638 port
->guest_connected
= qemu_get_byte(f
);
639 host_connected
= qemu_get_byte(f
);
640 if (host_connected
!= port
->host_connected
) {
642 * We have to let the guest know of the host connection
645 send_control_event(port
, VIRTIO_CONSOLE_PORT_OPEN
,
646 port
->host_connected
);
649 if (version_id
> 2) {
650 uint32_t elem_popped
;
652 qemu_get_be32s(f
, &elem_popped
);
654 qemu_get_be32s(f
, &port
->iov_idx
);
655 qemu_get_be64s(f
, &port
->iov_offset
);
657 qemu_get_buffer(f
, (unsigned char *)&port
->elem
,
659 virtqueue_map_sg(port
->elem
.in_sg
, port
->elem
.in_addr
,
660 port
->elem
.in_num
, 1);
661 virtqueue_map_sg(port
->elem
.out_sg
, port
->elem
.out_addr
,
662 port
->elem
.out_num
, 1);
665 * Port was throttled on source machine. Let's
666 * unthrottle it here so data starts flowing again.
668 virtio_serial_throttle_port(port
, false);
675 static void virtser_bus_dev_print(Monitor
*mon
, DeviceState
*qdev
, int indent
);
677 static struct BusInfo virtser_bus_info
= {
678 .name
= "virtio-serial-bus",
679 .size
= sizeof(VirtIOSerialBus
),
680 .print_dev
= virtser_bus_dev_print
,
681 .props
= (Property
[]) {
682 DEFINE_PROP_UINT32("nr", VirtIOSerialPort
, id
, VIRTIO_CONSOLE_BAD_ID
),
683 DEFINE_PROP_STRING("name", VirtIOSerialPort
, name
),
684 DEFINE_PROP_END_OF_LIST()
688 static void virtser_bus_dev_print(Monitor
*mon
, DeviceState
*qdev
, int indent
)
690 VirtIOSerialPort
*port
= DO_UPCAST(VirtIOSerialPort
, dev
, qdev
);
692 monitor_printf(mon
, "%*sport %d, guest %s, host %s, throttle %s\n",
693 indent
, "", port
->id
,
694 port
->guest_connected
? "on" : "off",
695 port
->host_connected
? "on" : "off",
696 port
->throttled
? "on" : "off");
699 /* This function is only used if a port id is not provided by the user */
700 static uint32_t find_free_port_id(VirtIOSerial
*vser
)
702 unsigned int i
, max_nr_ports
;
704 max_nr_ports
= tswap32(vser
->config
.max_nr_ports
);
705 for (i
= 0; i
< (max_nr_ports
+ 31) / 32; i
++) {
708 map
= vser
->ports_map
[i
];
711 return (bit
- 1) + i
* 32;
714 return VIRTIO_CONSOLE_BAD_ID
;
717 static void mark_port_added(VirtIOSerial
*vser
, uint32_t port_id
)
722 vser
->ports_map
[i
] |= 1U << (port_id
% 32);
725 static void add_port(VirtIOSerial
*vser
, uint32_t port_id
)
727 mark_port_added(vser
, port_id
);
729 send_control_event(find_port_by_id(vser
, port_id
),
730 VIRTIO_CONSOLE_PORT_ADD
, 1);
733 static void remove_port(VirtIOSerial
*vser
, uint32_t port_id
)
735 VirtIOSerialPort
*port
;
739 vser
->ports_map
[i
] &= ~(1U << (port_id
% 32));
741 port
= find_port_by_id(vser
, port_id
);
742 /* Flush out any unconsumed buffers first */
743 discard_vq_data(port
->ovq
, &port
->vser
->vdev
);
745 send_control_event(port
, VIRTIO_CONSOLE_PORT_REMOVE
, 1);
748 static int virtser_port_qdev_init(DeviceState
*qdev
, DeviceInfo
*base
)
750 VirtIOSerialPort
*port
= DO_UPCAST(VirtIOSerialPort
, dev
, qdev
);
751 VirtIOSerialPortInfo
*info
= DO_UPCAST(VirtIOSerialPortInfo
, qdev
, base
);
752 VirtIOSerialBus
*bus
= DO_UPCAST(VirtIOSerialBus
, qbus
, qdev
->parent_bus
);
753 int ret
, max_nr_ports
;
756 port
->vser
= bus
->vser
;
757 port
->bh
= qemu_bh_new(flush_queued_data_bh
, port
);
759 assert(info
->have_data
);
762 * Is the first console port we're seeing? If so, put it up at
763 * location 0. This is done for backward compatibility (old
766 plugging_port0
= info
->is_console
&& !find_port_by_id(port
->vser
, 0);
768 if (find_port_by_id(port
->vser
, port
->id
)) {
769 error_report("virtio-serial-bus: A port already exists at id %u",
774 if (port
->id
== VIRTIO_CONSOLE_BAD_ID
) {
775 if (plugging_port0
) {
778 port
->id
= find_free_port_id(port
->vser
);
779 if (port
->id
== VIRTIO_CONSOLE_BAD_ID
) {
780 error_report("virtio-serial-bus: Maximum port limit for this device reached");
786 max_nr_ports
= tswap32(port
->vser
->config
.max_nr_ports
);
787 if (port
->id
>= max_nr_ports
) {
788 error_report("virtio-serial-bus: Out-of-range port id specified, max. allowed: %u",
793 ret
= info
->init(port
);
798 if (!use_multiport(port
->vser
)) {
800 * Allow writes to guest in this case; we have no way of
801 * knowing if a guest port is connected.
803 port
->guest_connected
= true;
806 port
->elem
.out_num
= 0;
808 QTAILQ_INSERT_TAIL(&port
->vser
->ports
, port
, next
);
809 port
->ivq
= port
->vser
->ivqs
[port
->id
];
810 port
->ovq
= port
->vser
->ovqs
[port
->id
];
812 add_port(port
->vser
, port
->id
);
814 /* Send an update to the guest about this new port added */
815 virtio_notify_config(&port
->vser
->vdev
);
820 static int virtser_port_qdev_exit(DeviceState
*qdev
)
822 VirtIOSerialPort
*port
= DO_UPCAST(VirtIOSerialPort
, dev
, qdev
);
823 VirtIOSerialPortInfo
*info
= DO_UPCAST(VirtIOSerialPortInfo
, qdev
,
825 VirtIOSerial
*vser
= port
->vser
;
827 qemu_bh_delete(port
->bh
);
828 remove_port(port
->vser
, port
->id
);
830 QTAILQ_REMOVE(&vser
->ports
, port
, next
);
838 void virtio_serial_port_qdev_register(VirtIOSerialPortInfo
*info
)
840 info
->qdev
.init
= virtser_port_qdev_init
;
841 info
->qdev
.bus_info
= &virtser_bus_info
;
842 info
->qdev
.exit
= virtser_port_qdev_exit
;
843 info
->qdev
.unplug
= qdev_simple_unplug_cb
;
844 qdev_register(&info
->qdev
);
847 VirtIODevice
*virtio_serial_init(DeviceState
*dev
, virtio_serial_conf
*conf
)
851 uint32_t i
, max_supported_ports
;
853 if (!conf
->max_virtserial_ports
)
856 /* Each port takes 2 queues, and one pair is for the control queue */
857 max_supported_ports
= VIRTIO_PCI_QUEUE_MAX
/ 2 - 1;
859 if (conf
->max_virtserial_ports
> max_supported_ports
) {
860 error_report("maximum ports supported: %u", max_supported_ports
);
864 vdev
= virtio_common_init("virtio-serial", VIRTIO_ID_CONSOLE
,
865 sizeof(struct virtio_console_config
),
866 sizeof(VirtIOSerial
));
868 vser
= DO_UPCAST(VirtIOSerial
, vdev
, vdev
);
870 /* Spawn a new virtio-serial bus on which the ports will ride as devices */
871 qbus_create_inplace(&vser
->bus
.qbus
, &virtser_bus_info
, dev
, NULL
);
872 vser
->bus
.qbus
.allow_hotplug
= 1;
873 vser
->bus
.vser
= vser
;
874 QTAILQ_INIT(&vser
->ports
);
876 vser
->bus
.max_nr_ports
= conf
->max_virtserial_ports
;
877 vser
->ivqs
= g_malloc(conf
->max_virtserial_ports
* sizeof(VirtQueue
*));
878 vser
->ovqs
= g_malloc(conf
->max_virtserial_ports
* sizeof(VirtQueue
*));
880 /* Add a queue for host to guest transfers for port 0 (backward compat) */
881 vser
->ivqs
[0] = virtio_add_queue(vdev
, 128, handle_input
);
882 /* Add a queue for guest to host transfers for port 0 (backward compat) */
883 vser
->ovqs
[0] = virtio_add_queue(vdev
, 128, handle_output
);
885 /* TODO: host to guest notifications can get dropped
886 * if the queue fills up. Implement queueing in host,
887 * this might also make it possible to reduce the control
888 * queue size: as guest preposts buffers there,
889 * this will save 4Kbyte of guest memory per entry. */
891 /* control queue: host to guest */
892 vser
->c_ivq
= virtio_add_queue(vdev
, 32, control_in
);
893 /* control queue: guest to host */
894 vser
->c_ovq
= virtio_add_queue(vdev
, 32, control_out
);
896 for (i
= 1; i
< vser
->bus
.max_nr_ports
; i
++) {
897 /* Add a per-port queue for host to guest transfers */
898 vser
->ivqs
[i
] = virtio_add_queue(vdev
, 128, handle_input
);
899 /* Add a per-per queue for guest to host transfers */
900 vser
->ovqs
[i
] = virtio_add_queue(vdev
, 128, handle_output
);
903 vser
->config
.max_nr_ports
= tswap32(conf
->max_virtserial_ports
);
904 vser
->ports_map
= g_malloc0(((conf
->max_virtserial_ports
+ 31) / 32)
905 * sizeof(vser
->ports_map
[0]));
907 * Reserve location 0 for a console port for backward compat
908 * (old kernel, new qemu)
910 mark_port_added(vser
, 0);
912 vser
->vdev
.get_features
= get_features
;
913 vser
->vdev
.get_config
= get_config
;
914 vser
->vdev
.set_config
= set_config
;
919 * Register for the savevm section with the virtio-console name
920 * to preserve backward compat
922 register_savevm(dev
, "virtio-console", -1, 3, virtio_serial_save
,
923 virtio_serial_load
, vser
);
928 void virtio_serial_exit(VirtIODevice
*vdev
)
930 VirtIOSerial
*vser
= DO_UPCAST(VirtIOSerial
, vdev
, vdev
);
932 unregister_savevm(vser
->qdev
, "virtio-console", vser
);
936 g_free(vser
->ports_map
);
938 virtio_cleanup(vdev
);