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.
17 * Contributions after 2012-01-13 are licensed under the terms of the
18 * GNU GPL, version 2 or (at your option) any later version.
22 #include "monitor/monitor.h"
23 #include "qemu/queue.h"
26 #include "virtio-serial.h"
28 /* The virtio-serial bus on top of which the ports will ride as devices */
29 struct VirtIOSerialBus
{
32 /* This is the parent device that provides the bus for ports. */
35 /* The maximum number of ports that can ride on top of this bus */
36 uint32_t max_nr_ports
;
39 typedef struct VirtIOSerialPostLoad
{
41 uint32_t nr_active_ports
;
43 VirtIOSerialPort
*port
;
44 uint8_t host_connected
;
46 } VirtIOSerialPostLoad
;
51 VirtQueue
*c_ivq
, *c_ovq
;
52 /* Arrays of ivqs and ovqs: one per port */
53 VirtQueue
**ivqs
, **ovqs
;
59 QTAILQ_HEAD(, VirtIOSerialPort
) ports
;
61 /* bitmap for identifying active ports */
64 struct virtio_console_config config
;
66 struct VirtIOSerialPostLoad
*post_load
;
69 static VirtIOSerialPort
*find_port_by_id(VirtIOSerial
*vser
, uint32_t id
)
71 VirtIOSerialPort
*port
;
73 if (id
== VIRTIO_CONSOLE_BAD_ID
) {
77 QTAILQ_FOREACH(port
, &vser
->ports
, next
) {
84 static VirtIOSerialPort
*find_port_by_vq(VirtIOSerial
*vser
, VirtQueue
*vq
)
86 VirtIOSerialPort
*port
;
88 QTAILQ_FOREACH(port
, &vser
->ports
, next
) {
89 if (port
->ivq
== vq
|| port
->ovq
== vq
)
95 static bool use_multiport(VirtIOSerial
*vser
)
97 return vser
->vdev
.guest_features
& (1 << VIRTIO_CONSOLE_F_MULTIPORT
);
100 static size_t write_to_port(VirtIOSerialPort
*port
,
101 const uint8_t *buf
, size_t size
)
103 VirtQueueElement elem
;
108 if (!virtio_queue_ready(vq
)) {
113 while (offset
< size
) {
116 if (!virtqueue_pop(vq
, &elem
)) {
120 len
= iov_from_buf(elem
.in_sg
, elem
.in_num
, 0,
121 buf
+ offset
, size
- offset
);
124 virtqueue_push(vq
, &elem
, len
);
127 virtio_notify(&port
->vser
->vdev
, vq
);
131 static void discard_vq_data(VirtQueue
*vq
, VirtIODevice
*vdev
)
133 VirtQueueElement elem
;
135 if (!virtio_queue_ready(vq
)) {
138 while (virtqueue_pop(vq
, &elem
)) {
139 virtqueue_push(vq
, &elem
, 0);
141 virtio_notify(vdev
, vq
);
144 static void do_flush_queued_data(VirtIOSerialPort
*port
, VirtQueue
*vq
,
147 VirtIOSerialPortClass
*vsc
;
150 assert(virtio_queue_ready(vq
));
152 vsc
= VIRTIO_SERIAL_PORT_GET_CLASS(port
);
154 while (!port
->throttled
) {
157 /* Pop an elem only if we haven't left off a previous one mid-way */
158 if (!port
->elem
.out_num
) {
159 if (!virtqueue_pop(vq
, &port
->elem
)) {
163 port
->iov_offset
= 0;
166 for (i
= port
->iov_idx
; i
< port
->elem
.out_num
; i
++) {
170 buf_size
= port
->elem
.out_sg
[i
].iov_len
- port
->iov_offset
;
171 ret
= vsc
->have_data(port
,
172 port
->elem
.out_sg
[i
].iov_base
175 if (ret
< 0 && ret
!= -EAGAIN
) {
176 /* We don't handle any other type of errors here */
179 if (ret
== -EAGAIN
|| (ret
>= 0 && ret
< buf_size
)) {
181 * this is a temporary check until chardevs can signal to
182 * frontends that they are writable again. This prevents
183 * the console from going into throttled mode (forever)
184 * if virtio-console is connected to a pty without a
185 * listener. Otherwise the guest spins forever.
186 * We can revert this if
187 * 1: chardevs can notify frondends
188 * 2: the guest driver does not spin in these cases
190 if (!vsc
->is_console
) {
191 virtio_serial_throttle_port(port
, true);
195 port
->iov_offset
+= ret
;
199 port
->iov_offset
= 0;
201 if (port
->throttled
) {
204 virtqueue_push(vq
, &port
->elem
, 0);
205 port
->elem
.out_num
= 0;
207 virtio_notify(vdev
, vq
);
210 static void flush_queued_data(VirtIOSerialPort
*port
)
214 if (!virtio_queue_ready(port
->ovq
)) {
217 do_flush_queued_data(port
, port
->ovq
, &port
->vser
->vdev
);
220 static size_t send_control_msg(VirtIOSerial
*vser
, void *buf
, size_t len
)
222 VirtQueueElement elem
;
226 if (!virtio_queue_ready(vq
)) {
229 if (!virtqueue_pop(vq
, &elem
)) {
233 memcpy(elem
.in_sg
[0].iov_base
, buf
, len
);
235 virtqueue_push(vq
, &elem
, len
);
236 virtio_notify(&vser
->vdev
, vq
);
240 static size_t send_control_event(VirtIOSerial
*vser
, uint32_t port_id
,
241 uint16_t event
, uint16_t value
)
243 struct virtio_console_control cpkt
;
245 stl_p(&cpkt
.id
, port_id
);
246 stw_p(&cpkt
.event
, event
);
247 stw_p(&cpkt
.value
, value
);
249 trace_virtio_serial_send_control_event(port_id
, event
, value
);
250 return send_control_msg(vser
, &cpkt
, sizeof(cpkt
));
253 /* Functions for use inside qemu to open and read from/write to ports */
254 int virtio_serial_open(VirtIOSerialPort
*port
)
256 /* Don't allow opening an already-open port */
257 if (port
->host_connected
) {
260 /* Send port open notification to the guest */
261 port
->host_connected
= true;
262 send_control_event(port
->vser
, port
->id
, VIRTIO_CONSOLE_PORT_OPEN
, 1);
267 int virtio_serial_close(VirtIOSerialPort
*port
)
269 port
->host_connected
= false;
271 * If there's any data the guest sent which the app didn't
272 * consume, reset the throttling flag and discard the data.
274 port
->throttled
= false;
275 discard_vq_data(port
->ovq
, &port
->vser
->vdev
);
277 send_control_event(port
->vser
, port
->id
, VIRTIO_CONSOLE_PORT_OPEN
, 0);
282 /* Individual ports/apps call this function to write to the guest. */
283 ssize_t
virtio_serial_write(VirtIOSerialPort
*port
, const uint8_t *buf
,
286 if (!port
|| !port
->host_connected
|| !port
->guest_connected
) {
289 return write_to_port(port
, buf
, size
);
293 * Readiness of the guest to accept data on a port.
294 * Returns max. data the guest can receive
296 size_t virtio_serial_guest_ready(VirtIOSerialPort
*port
)
298 VirtQueue
*vq
= port
->ivq
;
301 if (!virtio_queue_ready(vq
) ||
302 !(port
->vser
->vdev
.status
& VIRTIO_CONFIG_S_DRIVER_OK
) ||
303 virtio_queue_empty(vq
)) {
306 if (use_multiport(port
->vser
) && !port
->guest_connected
) {
309 virtqueue_get_avail_bytes(vq
, &bytes
, NULL
, 4096, 0);
313 static void flush_queued_data_bh(void *opaque
)
315 VirtIOSerialPort
*port
= opaque
;
317 flush_queued_data(port
);
320 void virtio_serial_throttle_port(VirtIOSerialPort
*port
, bool throttle
)
326 trace_virtio_serial_throttle_port(port
->id
, throttle
);
327 port
->throttled
= throttle
;
331 qemu_bh_schedule(port
->bh
);
334 /* Guest wants to notify us of some event */
335 static void handle_control_message(VirtIOSerial
*vser
, void *buf
, size_t len
)
337 struct VirtIOSerialPort
*port
;
338 VirtIOSerialPortClass
*vsc
;
339 struct virtio_console_control cpkt
, *gcpkt
;
345 if (len
< sizeof(cpkt
)) {
346 /* The guest sent an invalid control packet */
350 cpkt
.event
= lduw_p(&gcpkt
->event
);
351 cpkt
.value
= lduw_p(&gcpkt
->value
);
353 trace_virtio_serial_handle_control_message(cpkt
.event
, cpkt
.value
);
355 if (cpkt
.event
== VIRTIO_CONSOLE_DEVICE_READY
) {
357 error_report("virtio-serial-bus: Guest failure in adding device %s",
358 vser
->bus
.qbus
.name
);
362 * The device is up, we can now tell the device about all the
363 * ports we have here.
365 QTAILQ_FOREACH(port
, &vser
->ports
, next
) {
366 send_control_event(vser
, port
->id
, VIRTIO_CONSOLE_PORT_ADD
, 1);
371 port
= find_port_by_id(vser
, ldl_p(&gcpkt
->id
));
373 error_report("virtio-serial-bus: Unexpected port id %u for device %s",
374 ldl_p(&gcpkt
->id
), vser
->bus
.qbus
.name
);
378 trace_virtio_serial_handle_control_message_port(port
->id
);
380 vsc
= VIRTIO_SERIAL_PORT_GET_CLASS(port
);
383 case VIRTIO_CONSOLE_PORT_READY
:
385 error_report("virtio-serial-bus: Guest failure in adding port %u for device %s",
386 port
->id
, vser
->bus
.qbus
.name
);
390 * Now that we know the guest asked for the port name, we're
391 * sure the guest has initialised whatever state is necessary
392 * for this port. Now's a good time to let the guest know if
393 * this port is a console port so that the guest can hook it
396 if (vsc
->is_console
) {
397 send_control_event(vser
, port
->id
, VIRTIO_CONSOLE_CONSOLE_PORT
, 1);
401 stl_p(&cpkt
.id
, port
->id
);
402 stw_p(&cpkt
.event
, VIRTIO_CONSOLE_PORT_NAME
);
403 stw_p(&cpkt
.value
, 1);
405 buffer_len
= sizeof(cpkt
) + strlen(port
->name
) + 1;
406 buffer
= g_malloc(buffer_len
);
408 memcpy(buffer
, &cpkt
, sizeof(cpkt
));
409 memcpy(buffer
+ sizeof(cpkt
), port
->name
, strlen(port
->name
));
410 buffer
[buffer_len
- 1] = 0;
412 send_control_msg(vser
, buffer
, buffer_len
);
416 if (port
->host_connected
) {
417 send_control_event(vser
, port
->id
, VIRTIO_CONSOLE_PORT_OPEN
, 1);
421 * When the guest has asked us for this information it means
422 * the guest is all setup and has its virtqueues
423 * initialised. If some app is interested in knowing about
424 * this event, let it know.
426 if (vsc
->guest_ready
) {
427 vsc
->guest_ready(port
);
431 case VIRTIO_CONSOLE_PORT_OPEN
:
432 port
->guest_connected
= cpkt
.value
;
433 if (cpkt
.value
&& vsc
->guest_open
) {
434 /* Send the guest opened notification if an app is interested */
435 vsc
->guest_open(port
);
438 if (!cpkt
.value
&& vsc
->guest_close
) {
439 /* Send the guest closed notification if an app is interested */
440 vsc
->guest_close(port
);
446 static void control_in(VirtIODevice
*vdev
, VirtQueue
*vq
)
450 static void control_out(VirtIODevice
*vdev
, VirtQueue
*vq
)
452 VirtQueueElement elem
;
457 vser
= DO_UPCAST(VirtIOSerial
, vdev
, vdev
);
461 while (virtqueue_pop(vq
, &elem
)) {
464 cur_len
= iov_size(elem
.out_sg
, elem
.out_num
);
466 * Allocate a new buf only if we didn't have one previously or
467 * if the size of the buf differs
472 buf
= g_malloc(cur_len
);
475 iov_to_buf(elem
.out_sg
, elem
.out_num
, 0, buf
, cur_len
);
477 handle_control_message(vser
, buf
, cur_len
);
478 virtqueue_push(vq
, &elem
, 0);
481 virtio_notify(vdev
, vq
);
484 /* Guest wrote something to some port. */
485 static void handle_output(VirtIODevice
*vdev
, VirtQueue
*vq
)
488 VirtIOSerialPort
*port
;
490 vser
= DO_UPCAST(VirtIOSerial
, vdev
, vdev
);
491 port
= find_port_by_vq(vser
, vq
);
493 if (!port
|| !port
->host_connected
) {
494 discard_vq_data(vq
, vdev
);
498 if (!port
->throttled
) {
499 do_flush_queued_data(port
, vq
, vdev
);
504 static void handle_input(VirtIODevice
*vdev
, VirtQueue
*vq
)
508 static uint32_t get_features(VirtIODevice
*vdev
, uint32_t features
)
512 vser
= DO_UPCAST(VirtIOSerial
, vdev
, vdev
);
514 if (vser
->bus
.max_nr_ports
> 1) {
515 features
|= (1 << VIRTIO_CONSOLE_F_MULTIPORT
);
520 /* Guest requested config info */
521 static void get_config(VirtIODevice
*vdev
, uint8_t *config_data
)
525 vser
= DO_UPCAST(VirtIOSerial
, vdev
, vdev
);
526 memcpy(config_data
, &vser
->config
, sizeof(struct virtio_console_config
));
529 static void set_config(VirtIODevice
*vdev
, const uint8_t *config_data
)
531 struct virtio_console_config config
;
533 memcpy(&config
, config_data
, sizeof(config
));
536 static void guest_reset(VirtIOSerial
*vser
)
538 VirtIOSerialPort
*port
;
539 VirtIOSerialPortClass
*vsc
;
541 QTAILQ_FOREACH(port
, &vser
->ports
, next
) {
542 vsc
= VIRTIO_SERIAL_PORT_GET_CLASS(port
);
543 if (port
->guest_connected
) {
544 port
->guest_connected
= false;
546 if (vsc
->guest_close
)
547 vsc
->guest_close(port
);
552 static void set_status(VirtIODevice
*vdev
, uint8_t status
)
555 VirtIOSerialPort
*port
;
557 vser
= DO_UPCAST(VirtIOSerial
, vdev
, vdev
);
558 port
= find_port_by_id(vser
, 0);
560 if (port
&& !use_multiport(port
->vser
)
561 && (status
& VIRTIO_CONFIG_S_DRIVER_OK
)) {
563 * Non-multiport guests won't be able to tell us guest
564 * open/close status. Such guests can only have a port at id
565 * 0, so set guest_connected for such ports as soon as guest
568 port
->guest_connected
= true;
570 if (!(status
& VIRTIO_CONFIG_S_DRIVER_OK
)) {
575 static void vser_reset(VirtIODevice
*vdev
)
579 vser
= DO_UPCAST(VirtIOSerial
, vdev
, vdev
);
583 static void virtio_serial_save(QEMUFile
*f
, void *opaque
)
585 VirtIOSerial
*s
= opaque
;
586 VirtIOSerialPort
*port
;
587 uint32_t nr_active_ports
;
588 unsigned int i
, max_nr_ports
;
590 /* The virtio device */
591 virtio_save(&s
->vdev
, f
);
593 /* The config space */
594 qemu_put_be16s(f
, &s
->config
.cols
);
595 qemu_put_be16s(f
, &s
->config
.rows
);
597 qemu_put_be32s(f
, &s
->config
.max_nr_ports
);
600 max_nr_ports
= tswap32(s
->config
.max_nr_ports
);
601 for (i
= 0; i
< (max_nr_ports
+ 31) / 32; i
++) {
602 qemu_put_be32s(f
, &s
->ports_map
[i
]);
608 QTAILQ_FOREACH(port
, &s
->ports
, next
) {
612 qemu_put_be32s(f
, &nr_active_ports
);
615 * Items in struct VirtIOSerialPort.
617 QTAILQ_FOREACH(port
, &s
->ports
, next
) {
618 uint32_t elem_popped
;
620 qemu_put_be32s(f
, &port
->id
);
621 qemu_put_byte(f
, port
->guest_connected
);
622 qemu_put_byte(f
, port
->host_connected
);
625 if (port
->elem
.out_num
) {
628 qemu_put_be32s(f
, &elem_popped
);
630 qemu_put_be32s(f
, &port
->iov_idx
);
631 qemu_put_be64s(f
, &port
->iov_offset
);
633 qemu_put_buffer(f
, (unsigned char *)&port
->elem
,
639 static void virtio_serial_post_load_timer_cb(void *opaque
)
642 VirtIOSerial
*s
= opaque
;
643 VirtIOSerialPort
*port
;
644 uint8_t host_connected
;
649 for (i
= 0 ; i
< s
->post_load
->nr_active_ports
; ++i
) {
650 port
= s
->post_load
->connected
[i
].port
;
651 host_connected
= s
->post_load
->connected
[i
].host_connected
;
652 if (host_connected
!= port
->host_connected
) {
654 * We have to let the guest know of the host connection
657 send_control_event(s
, port
->id
, VIRTIO_CONSOLE_PORT_OPEN
,
658 port
->host_connected
);
661 g_free(s
->post_load
->connected
);
662 qemu_free_timer(s
->post_load
->timer
);
663 g_free(s
->post_load
);
667 static int fetch_active_ports_list(QEMUFile
*f
, int version_id
,
668 VirtIOSerial
*s
, uint32_t nr_active_ports
)
672 s
->post_load
= g_malloc0(sizeof(*s
->post_load
));
673 s
->post_load
->nr_active_ports
= nr_active_ports
;
674 s
->post_load
->connected
=
675 g_malloc0(sizeof(*s
->post_load
->connected
) * nr_active_ports
);
677 s
->post_load
->timer
= qemu_new_timer_ns(vm_clock
,
678 virtio_serial_post_load_timer_cb
,
681 /* Items in struct VirtIOSerialPort */
682 for (i
= 0; i
< nr_active_ports
; i
++) {
683 VirtIOSerialPort
*port
;
686 id
= qemu_get_be32(f
);
687 port
= find_port_by_id(s
, id
);
692 port
->guest_connected
= qemu_get_byte(f
);
693 s
->post_load
->connected
[i
].port
= port
;
694 s
->post_load
->connected
[i
].host_connected
= qemu_get_byte(f
);
696 if (version_id
> 2) {
697 uint32_t elem_popped
;
699 qemu_get_be32s(f
, &elem_popped
);
701 qemu_get_be32s(f
, &port
->iov_idx
);
702 qemu_get_be64s(f
, &port
->iov_offset
);
704 qemu_get_buffer(f
, (unsigned char *)&port
->elem
,
706 virtqueue_map_sg(port
->elem
.in_sg
, port
->elem
.in_addr
,
707 port
->elem
.in_num
, 1);
708 virtqueue_map_sg(port
->elem
.out_sg
, port
->elem
.out_addr
,
709 port
->elem
.out_num
, 1);
712 * Port was throttled on source machine. Let's
713 * unthrottle it here so data starts flowing again.
715 virtio_serial_throttle_port(port
, false);
719 qemu_mod_timer(s
->post_load
->timer
, 1);
723 static int virtio_serial_load(QEMUFile
*f
, void *opaque
, int version_id
)
725 VirtIOSerial
*s
= opaque
;
726 uint32_t max_nr_ports
, nr_active_ports
, ports_map
;
730 if (version_id
> 3) {
734 /* The virtio device */
735 ret
= virtio_load(&s
->vdev
, f
);
740 if (version_id
< 2) {
744 /* The config space */
745 qemu_get_be16s(f
, &s
->config
.cols
);
746 qemu_get_be16s(f
, &s
->config
.rows
);
748 qemu_get_be32s(f
, &max_nr_ports
);
749 tswap32s(&max_nr_ports
);
750 if (max_nr_ports
> tswap32(s
->config
.max_nr_ports
)) {
751 /* Source could have had more ports than us. Fail migration. */
755 for (i
= 0; i
< (max_nr_ports
+ 31) / 32; i
++) {
756 qemu_get_be32s(f
, &ports_map
);
758 if (ports_map
!= s
->ports_map
[i
]) {
760 * Ports active on source and destination don't
761 * match. Fail migration.
767 qemu_get_be32s(f
, &nr_active_ports
);
769 if (nr_active_ports
) {
770 ret
= fetch_active_ports_list(f
, version_id
, s
, nr_active_ports
);
778 static void virtser_bus_dev_print(Monitor
*mon
, DeviceState
*qdev
, int indent
);
780 static Property virtser_props
[] = {
781 DEFINE_PROP_UINT32("nr", VirtIOSerialPort
, id
, VIRTIO_CONSOLE_BAD_ID
),
782 DEFINE_PROP_STRING("name", VirtIOSerialPort
, name
),
783 DEFINE_PROP_END_OF_LIST()
786 #define TYPE_VIRTIO_SERIAL_BUS "virtio-serial-bus"
787 #define VIRTIO_SERIAL_BUS(obj) \
788 OBJECT_CHECK(VirtIOSerialBus, (obj), TYPE_VIRTIO_SERIAL_BUS)
790 static void virtser_bus_class_init(ObjectClass
*klass
, void *data
)
792 BusClass
*k
= BUS_CLASS(klass
);
793 k
->print_dev
= virtser_bus_dev_print
;
796 static const TypeInfo virtser_bus_info
= {
797 .name
= TYPE_VIRTIO_SERIAL_BUS
,
799 .instance_size
= sizeof(VirtIOSerialBus
),
800 .class_init
= virtser_bus_class_init
,
803 static void virtser_bus_dev_print(Monitor
*mon
, DeviceState
*qdev
, int indent
)
805 VirtIOSerialPort
*port
= DO_UPCAST(VirtIOSerialPort
, dev
, qdev
);
807 monitor_printf(mon
, "%*sport %d, guest %s, host %s, throttle %s\n",
808 indent
, "", port
->id
,
809 port
->guest_connected
? "on" : "off",
810 port
->host_connected
? "on" : "off",
811 port
->throttled
? "on" : "off");
814 /* This function is only used if a port id is not provided by the user */
815 static uint32_t find_free_port_id(VirtIOSerial
*vser
)
817 unsigned int i
, max_nr_ports
;
819 max_nr_ports
= tswap32(vser
->config
.max_nr_ports
);
820 for (i
= 0; i
< (max_nr_ports
+ 31) / 32; i
++) {
823 map
= vser
->ports_map
[i
];
826 return (bit
- 1) + i
* 32;
829 return VIRTIO_CONSOLE_BAD_ID
;
832 static void mark_port_added(VirtIOSerial
*vser
, uint32_t port_id
)
837 vser
->ports_map
[i
] |= 1U << (port_id
% 32);
840 static void add_port(VirtIOSerial
*vser
, uint32_t port_id
)
842 mark_port_added(vser
, port_id
);
843 send_control_event(vser
, port_id
, VIRTIO_CONSOLE_PORT_ADD
, 1);
846 static void remove_port(VirtIOSerial
*vser
, uint32_t port_id
)
848 VirtIOSerialPort
*port
;
852 vser
->ports_map
[i
] &= ~(1U << (port_id
% 32));
854 port
= find_port_by_id(vser
, port_id
);
856 * This function is only called from qdev's unplug callback; if we
857 * get a NULL port here, we're in trouble.
861 /* Flush out any unconsumed buffers first */
862 discard_vq_data(port
->ovq
, &port
->vser
->vdev
);
864 send_control_event(vser
, port
->id
, VIRTIO_CONSOLE_PORT_REMOVE
, 1);
867 static int virtser_port_qdev_init(DeviceState
*qdev
)
869 VirtIOSerialPort
*port
= DO_UPCAST(VirtIOSerialPort
, dev
, qdev
);
870 VirtIOSerialPortClass
*vsc
= VIRTIO_SERIAL_PORT_GET_CLASS(port
);
871 VirtIOSerialBus
*bus
= DO_UPCAST(VirtIOSerialBus
, qbus
, qdev
->parent_bus
);
872 int ret
, max_nr_ports
;
875 port
->vser
= bus
->vser
;
876 port
->bh
= qemu_bh_new(flush_queued_data_bh
, port
);
878 assert(vsc
->have_data
);
881 * Is the first console port we're seeing? If so, put it up at
882 * location 0. This is done for backward compatibility (old
885 plugging_port0
= vsc
->is_console
&& !find_port_by_id(port
->vser
, 0);
887 if (find_port_by_id(port
->vser
, port
->id
)) {
888 error_report("virtio-serial-bus: A port already exists at id %u",
893 if (port
->id
== VIRTIO_CONSOLE_BAD_ID
) {
894 if (plugging_port0
) {
897 port
->id
= find_free_port_id(port
->vser
);
898 if (port
->id
== VIRTIO_CONSOLE_BAD_ID
) {
899 error_report("virtio-serial-bus: Maximum port limit for this device reached");
905 max_nr_ports
= tswap32(port
->vser
->config
.max_nr_ports
);
906 if (port
->id
>= max_nr_ports
) {
907 error_report("virtio-serial-bus: Out-of-range port id specified, max. allowed: %u",
912 ret
= vsc
->init(port
);
917 port
->elem
.out_num
= 0;
919 QTAILQ_INSERT_TAIL(&port
->vser
->ports
, port
, next
);
920 port
->ivq
= port
->vser
->ivqs
[port
->id
];
921 port
->ovq
= port
->vser
->ovqs
[port
->id
];
923 add_port(port
->vser
, port
->id
);
925 /* Send an update to the guest about this new port added */
926 virtio_notify_config(&port
->vser
->vdev
);
931 static int virtser_port_qdev_exit(DeviceState
*qdev
)
933 VirtIOSerialPort
*port
= DO_UPCAST(VirtIOSerialPort
, dev
, qdev
);
934 VirtIOSerialPortClass
*vsc
= VIRTIO_SERIAL_PORT_GET_CLASS(port
);
935 VirtIOSerial
*vser
= port
->vser
;
937 qemu_bh_delete(port
->bh
);
938 remove_port(port
->vser
, port
->id
);
940 QTAILQ_REMOVE(&vser
->ports
, port
, next
);
948 VirtIODevice
*virtio_serial_init(DeviceState
*dev
, virtio_serial_conf
*conf
)
952 uint32_t i
, max_supported_ports
;
954 if (!conf
->max_virtserial_ports
)
957 /* Each port takes 2 queues, and one pair is for the control queue */
958 max_supported_ports
= VIRTIO_PCI_QUEUE_MAX
/ 2 - 1;
960 if (conf
->max_virtserial_ports
> max_supported_ports
) {
961 error_report("maximum ports supported: %u", max_supported_ports
);
965 vdev
= virtio_common_init("virtio-serial", VIRTIO_ID_CONSOLE
,
966 sizeof(struct virtio_console_config
),
967 sizeof(VirtIOSerial
));
969 vser
= DO_UPCAST(VirtIOSerial
, vdev
, vdev
);
971 /* Spawn a new virtio-serial bus on which the ports will ride as devices */
972 qbus_create_inplace(&vser
->bus
.qbus
, TYPE_VIRTIO_SERIAL_BUS
, dev
, NULL
);
973 vser
->bus
.qbus
.allow_hotplug
= 1;
974 vser
->bus
.vser
= vser
;
975 QTAILQ_INIT(&vser
->ports
);
977 vser
->bus
.max_nr_ports
= conf
->max_virtserial_ports
;
978 vser
->ivqs
= g_malloc(conf
->max_virtserial_ports
* sizeof(VirtQueue
*));
979 vser
->ovqs
= g_malloc(conf
->max_virtserial_ports
* sizeof(VirtQueue
*));
981 /* Add a queue for host to guest transfers for port 0 (backward compat) */
982 vser
->ivqs
[0] = virtio_add_queue(vdev
, 128, handle_input
);
983 /* Add a queue for guest to host transfers for port 0 (backward compat) */
984 vser
->ovqs
[0] = virtio_add_queue(vdev
, 128, handle_output
);
986 /* TODO: host to guest notifications can get dropped
987 * if the queue fills up. Implement queueing in host,
988 * this might also make it possible to reduce the control
989 * queue size: as guest preposts buffers there,
990 * this will save 4Kbyte of guest memory per entry. */
992 /* control queue: host to guest */
993 vser
->c_ivq
= virtio_add_queue(vdev
, 32, control_in
);
994 /* control queue: guest to host */
995 vser
->c_ovq
= virtio_add_queue(vdev
, 32, control_out
);
997 for (i
= 1; i
< vser
->bus
.max_nr_ports
; i
++) {
998 /* Add a per-port queue for host to guest transfers */
999 vser
->ivqs
[i
] = virtio_add_queue(vdev
, 128, handle_input
);
1000 /* Add a per-per queue for guest to host transfers */
1001 vser
->ovqs
[i
] = virtio_add_queue(vdev
, 128, handle_output
);
1004 vser
->config
.max_nr_ports
= tswap32(conf
->max_virtserial_ports
);
1005 vser
->ports_map
= g_malloc0(((conf
->max_virtserial_ports
+ 31) / 32)
1006 * sizeof(vser
->ports_map
[0]));
1008 * Reserve location 0 for a console port for backward compat
1009 * (old kernel, new qemu)
1011 mark_port_added(vser
, 0);
1013 vser
->vdev
.get_features
= get_features
;
1014 vser
->vdev
.get_config
= get_config
;
1015 vser
->vdev
.set_config
= set_config
;
1016 vser
->vdev
.set_status
= set_status
;
1017 vser
->vdev
.reset
= vser_reset
;
1021 vser
->post_load
= NULL
;
1024 * Register for the savevm section with the virtio-console name
1025 * to preserve backward compat
1027 register_savevm(dev
, "virtio-console", -1, 3, virtio_serial_save
,
1028 virtio_serial_load
, vser
);
1033 void virtio_serial_exit(VirtIODevice
*vdev
)
1035 VirtIOSerial
*vser
= DO_UPCAST(VirtIOSerial
, vdev
, vdev
);
1037 unregister_savevm(vser
->qdev
, "virtio-console", vser
);
1041 g_free(vser
->ports_map
);
1042 if (vser
->post_load
) {
1043 g_free(vser
->post_load
->connected
);
1044 qemu_del_timer(vser
->post_load
->timer
);
1045 qemu_free_timer(vser
->post_load
->timer
);
1046 g_free(vser
->post_load
);
1048 virtio_cleanup(vdev
);
1051 static void virtio_serial_port_class_init(ObjectClass
*klass
, void *data
)
1053 DeviceClass
*k
= DEVICE_CLASS(klass
);
1054 k
->init
= virtser_port_qdev_init
;
1055 k
->bus_type
= TYPE_VIRTIO_SERIAL_BUS
;
1056 k
->exit
= virtser_port_qdev_exit
;
1057 k
->unplug
= qdev_simple_unplug_cb
;
1058 k
->props
= virtser_props
;
1061 static TypeInfo virtio_serial_port_type_info
= {
1062 .name
= TYPE_VIRTIO_SERIAL_PORT
,
1063 .parent
= TYPE_DEVICE
,
1064 .instance_size
= sizeof(VirtIOSerialPort
),
1066 .class_size
= sizeof(VirtIOSerialPortClass
),
1067 .class_init
= virtio_serial_port_class_init
,
1070 static void virtio_serial_register_types(void)
1072 type_register_static(&virtser_bus_info
);
1073 type_register_static(&virtio_serial_port_type_info
);
1076 type_init(virtio_serial_register_types
)