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 (port
->throttled
) {
178 port
->iov_offset
+= ret
;
182 port
->iov_offset
= 0;
184 if (port
->throttled
) {
187 virtqueue_push(vq
, &port
->elem
, 0);
188 port
->elem
.out_num
= 0;
190 virtio_notify(vdev
, vq
);
193 static void flush_queued_data(VirtIOSerialPort
*port
)
197 if (!virtio_queue_ready(port
->ovq
)) {
200 do_flush_queued_data(port
, port
->ovq
, &port
->vser
->vdev
);
203 static size_t send_control_msg(VirtIOSerial
*vser
, void *buf
, size_t len
)
205 VirtQueueElement elem
;
209 if (!virtio_queue_ready(vq
)) {
212 if (!virtqueue_pop(vq
, &elem
)) {
216 memcpy(elem
.in_sg
[0].iov_base
, buf
, len
);
218 virtqueue_push(vq
, &elem
, len
);
219 virtio_notify(&vser
->vdev
, vq
);
223 static size_t send_control_event(VirtIOSerial
*vser
, uint32_t port_id
,
224 uint16_t event
, uint16_t value
)
226 struct virtio_console_control cpkt
;
228 stl_p(&cpkt
.id
, port_id
);
229 stw_p(&cpkt
.event
, event
);
230 stw_p(&cpkt
.value
, value
);
232 trace_virtio_serial_send_control_event(port_id
, event
, value
);
233 return send_control_msg(vser
, &cpkt
, sizeof(cpkt
));
236 /* Functions for use inside qemu to open and read from/write to ports */
237 int virtio_serial_open(VirtIOSerialPort
*port
)
239 /* Don't allow opening an already-open port */
240 if (port
->host_connected
) {
243 /* Send port open notification to the guest */
244 port
->host_connected
= true;
245 send_control_event(port
->vser
, port
->id
, VIRTIO_CONSOLE_PORT_OPEN
, 1);
250 int virtio_serial_close(VirtIOSerialPort
*port
)
252 port
->host_connected
= false;
254 * If there's any data the guest sent which the app didn't
255 * consume, reset the throttling flag and discard the data.
257 port
->throttled
= false;
258 discard_vq_data(port
->ovq
, &port
->vser
->vdev
);
260 send_control_event(port
->vser
, port
->id
, VIRTIO_CONSOLE_PORT_OPEN
, 0);
265 /* Individual ports/apps call this function to write to the guest. */
266 ssize_t
virtio_serial_write(VirtIOSerialPort
*port
, const uint8_t *buf
,
269 if (!port
|| !port
->host_connected
|| !port
->guest_connected
) {
272 return write_to_port(port
, buf
, size
);
276 * Readiness of the guest to accept data on a port.
277 * Returns max. data the guest can receive
279 size_t virtio_serial_guest_ready(VirtIOSerialPort
*port
)
281 VirtQueue
*vq
= port
->ivq
;
284 if (!virtio_queue_ready(vq
) ||
285 !(port
->vser
->vdev
.status
& VIRTIO_CONFIG_S_DRIVER_OK
) ||
286 virtio_queue_empty(vq
)) {
289 if (use_multiport(port
->vser
) && !port
->guest_connected
) {
292 virtqueue_get_avail_bytes(vq
, &bytes
, NULL
, 4096, 0);
296 static void flush_queued_data_bh(void *opaque
)
298 VirtIOSerialPort
*port
= opaque
;
300 flush_queued_data(port
);
303 void virtio_serial_throttle_port(VirtIOSerialPort
*port
, bool throttle
)
309 trace_virtio_serial_throttle_port(port
->id
, throttle
);
310 port
->throttled
= throttle
;
314 qemu_bh_schedule(port
->bh
);
317 /* Guest wants to notify us of some event */
318 static void handle_control_message(VirtIOSerial
*vser
, void *buf
, size_t len
)
320 struct VirtIOSerialPort
*port
;
321 VirtIOSerialPortClass
*vsc
;
322 struct virtio_console_control cpkt
, *gcpkt
;
328 if (len
< sizeof(cpkt
)) {
329 /* The guest sent an invalid control packet */
333 cpkt
.event
= lduw_p(&gcpkt
->event
);
334 cpkt
.value
= lduw_p(&gcpkt
->value
);
336 trace_virtio_serial_handle_control_message(cpkt
.event
, cpkt
.value
);
338 if (cpkt
.event
== VIRTIO_CONSOLE_DEVICE_READY
) {
340 error_report("virtio-serial-bus: Guest failure in adding device %s",
341 vser
->bus
.qbus
.name
);
345 * The device is up, we can now tell the device about all the
346 * ports we have here.
348 QTAILQ_FOREACH(port
, &vser
->ports
, next
) {
349 send_control_event(vser
, port
->id
, VIRTIO_CONSOLE_PORT_ADD
, 1);
354 port
= find_port_by_id(vser
, ldl_p(&gcpkt
->id
));
356 error_report("virtio-serial-bus: Unexpected port id %u for device %s",
357 ldl_p(&gcpkt
->id
), vser
->bus
.qbus
.name
);
361 trace_virtio_serial_handle_control_message_port(port
->id
);
363 vsc
= VIRTIO_SERIAL_PORT_GET_CLASS(port
);
366 case VIRTIO_CONSOLE_PORT_READY
:
368 error_report("virtio-serial-bus: Guest failure in adding port %u for device %s",
369 port
->id
, vser
->bus
.qbus
.name
);
373 * Now that we know the guest asked for the port name, we're
374 * sure the guest has initialised whatever state is necessary
375 * for this port. Now's a good time to let the guest know if
376 * this port is a console port so that the guest can hook it
379 if (vsc
->is_console
) {
380 send_control_event(vser
, port
->id
, VIRTIO_CONSOLE_CONSOLE_PORT
, 1);
384 stl_p(&cpkt
.id
, port
->id
);
385 stw_p(&cpkt
.event
, VIRTIO_CONSOLE_PORT_NAME
);
386 stw_p(&cpkt
.value
, 1);
388 buffer_len
= sizeof(cpkt
) + strlen(port
->name
) + 1;
389 buffer
= g_malloc(buffer_len
);
391 memcpy(buffer
, &cpkt
, sizeof(cpkt
));
392 memcpy(buffer
+ sizeof(cpkt
), port
->name
, strlen(port
->name
));
393 buffer
[buffer_len
- 1] = 0;
395 send_control_msg(vser
, buffer
, buffer_len
);
399 if (port
->host_connected
) {
400 send_control_event(vser
, port
->id
, VIRTIO_CONSOLE_PORT_OPEN
, 1);
404 * When the guest has asked us for this information it means
405 * the guest is all setup and has its virtqueues
406 * initialised. If some app is interested in knowing about
407 * this event, let it know.
409 if (vsc
->guest_ready
) {
410 vsc
->guest_ready(port
);
414 case VIRTIO_CONSOLE_PORT_OPEN
:
415 port
->guest_connected
= cpkt
.value
;
416 if (cpkt
.value
&& vsc
->guest_open
) {
417 /* Send the guest opened notification if an app is interested */
418 vsc
->guest_open(port
);
421 if (!cpkt
.value
&& vsc
->guest_close
) {
422 /* Send the guest closed notification if an app is interested */
423 vsc
->guest_close(port
);
429 static void control_in(VirtIODevice
*vdev
, VirtQueue
*vq
)
433 static void control_out(VirtIODevice
*vdev
, VirtQueue
*vq
)
435 VirtQueueElement elem
;
440 vser
= DO_UPCAST(VirtIOSerial
, vdev
, vdev
);
444 while (virtqueue_pop(vq
, &elem
)) {
447 cur_len
= iov_size(elem
.out_sg
, elem
.out_num
);
449 * Allocate a new buf only if we didn't have one previously or
450 * if the size of the buf differs
455 buf
= g_malloc(cur_len
);
458 iov_to_buf(elem
.out_sg
, elem
.out_num
, 0, buf
, cur_len
);
460 handle_control_message(vser
, buf
, cur_len
);
461 virtqueue_push(vq
, &elem
, 0);
464 virtio_notify(vdev
, vq
);
467 /* Guest wrote something to some port. */
468 static void handle_output(VirtIODevice
*vdev
, VirtQueue
*vq
)
471 VirtIOSerialPort
*port
;
473 vser
= DO_UPCAST(VirtIOSerial
, vdev
, vdev
);
474 port
= find_port_by_vq(vser
, vq
);
476 if (!port
|| !port
->host_connected
) {
477 discard_vq_data(vq
, vdev
);
481 if (!port
->throttled
) {
482 do_flush_queued_data(port
, vq
, vdev
);
487 static void handle_input(VirtIODevice
*vdev
, VirtQueue
*vq
)
491 static uint32_t get_features(VirtIODevice
*vdev
, uint32_t features
)
495 vser
= DO_UPCAST(VirtIOSerial
, vdev
, vdev
);
497 if (vser
->bus
.max_nr_ports
> 1) {
498 features
|= (1 << VIRTIO_CONSOLE_F_MULTIPORT
);
503 /* Guest requested config info */
504 static void get_config(VirtIODevice
*vdev
, uint8_t *config_data
)
508 vser
= DO_UPCAST(VirtIOSerial
, vdev
, vdev
);
509 memcpy(config_data
, &vser
->config
, sizeof(struct virtio_console_config
));
512 static void set_config(VirtIODevice
*vdev
, const uint8_t *config_data
)
514 struct virtio_console_config config
;
516 memcpy(&config
, config_data
, sizeof(config
));
519 static void guest_reset(VirtIOSerial
*vser
)
521 VirtIOSerialPort
*port
;
522 VirtIOSerialPortClass
*vsc
;
524 QTAILQ_FOREACH(port
, &vser
->ports
, next
) {
525 vsc
= VIRTIO_SERIAL_PORT_GET_CLASS(port
);
526 if (port
->guest_connected
) {
527 port
->guest_connected
= false;
529 if (vsc
->guest_close
)
530 vsc
->guest_close(port
);
535 static void set_status(VirtIODevice
*vdev
, uint8_t status
)
538 VirtIOSerialPort
*port
;
540 vser
= DO_UPCAST(VirtIOSerial
, vdev
, vdev
);
541 port
= find_port_by_id(vser
, 0);
543 if (port
&& !use_multiport(port
->vser
)
544 && (status
& VIRTIO_CONFIG_S_DRIVER_OK
)) {
546 * Non-multiport guests won't be able to tell us guest
547 * open/close status. Such guests can only have a port at id
548 * 0, so set guest_connected for such ports as soon as guest
551 port
->guest_connected
= true;
553 if (!(status
& VIRTIO_CONFIG_S_DRIVER_OK
)) {
558 static void vser_reset(VirtIODevice
*vdev
)
562 vser
= DO_UPCAST(VirtIOSerial
, vdev
, vdev
);
566 static void virtio_serial_save(QEMUFile
*f
, void *opaque
)
568 VirtIOSerial
*s
= opaque
;
569 VirtIOSerialPort
*port
;
570 uint32_t nr_active_ports
;
571 unsigned int i
, max_nr_ports
;
573 /* The virtio device */
574 virtio_save(&s
->vdev
, f
);
576 /* The config space */
577 qemu_put_be16s(f
, &s
->config
.cols
);
578 qemu_put_be16s(f
, &s
->config
.rows
);
580 qemu_put_be32s(f
, &s
->config
.max_nr_ports
);
583 max_nr_ports
= tswap32(s
->config
.max_nr_ports
);
584 for (i
= 0; i
< (max_nr_ports
+ 31) / 32; i
++) {
585 qemu_put_be32s(f
, &s
->ports_map
[i
]);
591 QTAILQ_FOREACH(port
, &s
->ports
, next
) {
595 qemu_put_be32s(f
, &nr_active_ports
);
598 * Items in struct VirtIOSerialPort.
600 QTAILQ_FOREACH(port
, &s
->ports
, next
) {
601 uint32_t elem_popped
;
603 qemu_put_be32s(f
, &port
->id
);
604 qemu_put_byte(f
, port
->guest_connected
);
605 qemu_put_byte(f
, port
->host_connected
);
608 if (port
->elem
.out_num
) {
611 qemu_put_be32s(f
, &elem_popped
);
613 qemu_put_be32s(f
, &port
->iov_idx
);
614 qemu_put_be64s(f
, &port
->iov_offset
);
616 qemu_put_buffer(f
, (unsigned char *)&port
->elem
,
622 static void virtio_serial_post_load_timer_cb(void *opaque
)
625 VirtIOSerial
*s
= opaque
;
626 VirtIOSerialPort
*port
;
627 uint8_t host_connected
;
632 for (i
= 0 ; i
< s
->post_load
->nr_active_ports
; ++i
) {
633 port
= s
->post_load
->connected
[i
].port
;
634 host_connected
= s
->post_load
->connected
[i
].host_connected
;
635 if (host_connected
!= port
->host_connected
) {
637 * We have to let the guest know of the host connection
640 send_control_event(s
, port
->id
, VIRTIO_CONSOLE_PORT_OPEN
,
641 port
->host_connected
);
644 g_free(s
->post_load
->connected
);
645 qemu_free_timer(s
->post_load
->timer
);
646 g_free(s
->post_load
);
650 static int fetch_active_ports_list(QEMUFile
*f
, int version_id
,
651 VirtIOSerial
*s
, uint32_t nr_active_ports
)
655 s
->post_load
= g_malloc0(sizeof(*s
->post_load
));
656 s
->post_load
->nr_active_ports
= nr_active_ports
;
657 s
->post_load
->connected
=
658 g_malloc0(sizeof(*s
->post_load
->connected
) * nr_active_ports
);
660 s
->post_load
->timer
= qemu_new_timer_ns(vm_clock
,
661 virtio_serial_post_load_timer_cb
,
664 /* Items in struct VirtIOSerialPort */
665 for (i
= 0; i
< nr_active_ports
; i
++) {
666 VirtIOSerialPort
*port
;
669 id
= qemu_get_be32(f
);
670 port
= find_port_by_id(s
, id
);
675 port
->guest_connected
= qemu_get_byte(f
);
676 s
->post_load
->connected
[i
].port
= port
;
677 s
->post_load
->connected
[i
].host_connected
= qemu_get_byte(f
);
679 if (version_id
> 2) {
680 uint32_t elem_popped
;
682 qemu_get_be32s(f
, &elem_popped
);
684 qemu_get_be32s(f
, &port
->iov_idx
);
685 qemu_get_be64s(f
, &port
->iov_offset
);
687 qemu_get_buffer(f
, (unsigned char *)&port
->elem
,
689 virtqueue_map_sg(port
->elem
.in_sg
, port
->elem
.in_addr
,
690 port
->elem
.in_num
, 1);
691 virtqueue_map_sg(port
->elem
.out_sg
, port
->elem
.out_addr
,
692 port
->elem
.out_num
, 1);
695 * Port was throttled on source machine. Let's
696 * unthrottle it here so data starts flowing again.
698 virtio_serial_throttle_port(port
, false);
702 qemu_mod_timer(s
->post_load
->timer
, 1);
706 static int virtio_serial_load(QEMUFile
*f
, void *opaque
, int version_id
)
708 VirtIOSerial
*s
= opaque
;
709 uint32_t max_nr_ports
, nr_active_ports
, ports_map
;
713 if (version_id
> 3) {
717 /* The virtio device */
718 ret
= virtio_load(&s
->vdev
, f
);
723 if (version_id
< 2) {
727 /* The config space */
728 qemu_get_be16s(f
, &s
->config
.cols
);
729 qemu_get_be16s(f
, &s
->config
.rows
);
731 qemu_get_be32s(f
, &max_nr_ports
);
732 tswap32s(&max_nr_ports
);
733 if (max_nr_ports
> tswap32(s
->config
.max_nr_ports
)) {
734 /* Source could have had more ports than us. Fail migration. */
738 for (i
= 0; i
< (max_nr_ports
+ 31) / 32; i
++) {
739 qemu_get_be32s(f
, &ports_map
);
741 if (ports_map
!= s
->ports_map
[i
]) {
743 * Ports active on source and destination don't
744 * match. Fail migration.
750 qemu_get_be32s(f
, &nr_active_ports
);
752 if (nr_active_ports
) {
753 ret
= fetch_active_ports_list(f
, version_id
, s
, nr_active_ports
);
761 static void virtser_bus_dev_print(Monitor
*mon
, DeviceState
*qdev
, int indent
);
763 static Property virtser_props
[] = {
764 DEFINE_PROP_UINT32("nr", VirtIOSerialPort
, id
, VIRTIO_CONSOLE_BAD_ID
),
765 DEFINE_PROP_STRING("name", VirtIOSerialPort
, name
),
766 DEFINE_PROP_END_OF_LIST()
769 #define TYPE_VIRTIO_SERIAL_BUS "virtio-serial-bus"
770 #define VIRTIO_SERIAL_BUS(obj) \
771 OBJECT_CHECK(VirtIOSerialBus, (obj), TYPE_VIRTIO_SERIAL_BUS)
773 static void virtser_bus_class_init(ObjectClass
*klass
, void *data
)
775 BusClass
*k
= BUS_CLASS(klass
);
776 k
->print_dev
= virtser_bus_dev_print
;
779 static const TypeInfo virtser_bus_info
= {
780 .name
= TYPE_VIRTIO_SERIAL_BUS
,
782 .instance_size
= sizeof(VirtIOSerialBus
),
783 .class_init
= virtser_bus_class_init
,
786 static void virtser_bus_dev_print(Monitor
*mon
, DeviceState
*qdev
, int indent
)
788 VirtIOSerialPort
*port
= DO_UPCAST(VirtIOSerialPort
, dev
, qdev
);
790 monitor_printf(mon
, "%*sport %d, guest %s, host %s, throttle %s\n",
791 indent
, "", port
->id
,
792 port
->guest_connected
? "on" : "off",
793 port
->host_connected
? "on" : "off",
794 port
->throttled
? "on" : "off");
797 /* This function is only used if a port id is not provided by the user */
798 static uint32_t find_free_port_id(VirtIOSerial
*vser
)
800 unsigned int i
, max_nr_ports
;
802 max_nr_ports
= tswap32(vser
->config
.max_nr_ports
);
803 for (i
= 0; i
< (max_nr_ports
+ 31) / 32; i
++) {
806 map
= vser
->ports_map
[i
];
809 return (bit
- 1) + i
* 32;
812 return VIRTIO_CONSOLE_BAD_ID
;
815 static void mark_port_added(VirtIOSerial
*vser
, uint32_t port_id
)
820 vser
->ports_map
[i
] |= 1U << (port_id
% 32);
823 static void add_port(VirtIOSerial
*vser
, uint32_t port_id
)
825 mark_port_added(vser
, port_id
);
826 send_control_event(vser
, port_id
, VIRTIO_CONSOLE_PORT_ADD
, 1);
829 static void remove_port(VirtIOSerial
*vser
, uint32_t port_id
)
831 VirtIOSerialPort
*port
;
835 vser
->ports_map
[i
] &= ~(1U << (port_id
% 32));
837 port
= find_port_by_id(vser
, port_id
);
839 * This function is only called from qdev's unplug callback; if we
840 * get a NULL port here, we're in trouble.
844 /* Flush out any unconsumed buffers first */
845 discard_vq_data(port
->ovq
, &port
->vser
->vdev
);
847 send_control_event(vser
, port
->id
, VIRTIO_CONSOLE_PORT_REMOVE
, 1);
850 static int virtser_port_qdev_init(DeviceState
*qdev
)
852 VirtIOSerialPort
*port
= DO_UPCAST(VirtIOSerialPort
, dev
, qdev
);
853 VirtIOSerialPortClass
*vsc
= VIRTIO_SERIAL_PORT_GET_CLASS(port
);
854 VirtIOSerialBus
*bus
= DO_UPCAST(VirtIOSerialBus
, qbus
, qdev
->parent_bus
);
855 int ret
, max_nr_ports
;
858 port
->vser
= bus
->vser
;
859 port
->bh
= qemu_bh_new(flush_queued_data_bh
, port
);
861 assert(vsc
->have_data
);
864 * Is the first console port we're seeing? If so, put it up at
865 * location 0. This is done for backward compatibility (old
868 plugging_port0
= vsc
->is_console
&& !find_port_by_id(port
->vser
, 0);
870 if (find_port_by_id(port
->vser
, port
->id
)) {
871 error_report("virtio-serial-bus: A port already exists at id %u",
876 if (port
->id
== VIRTIO_CONSOLE_BAD_ID
) {
877 if (plugging_port0
) {
880 port
->id
= find_free_port_id(port
->vser
);
881 if (port
->id
== VIRTIO_CONSOLE_BAD_ID
) {
882 error_report("virtio-serial-bus: Maximum port limit for this device reached");
888 max_nr_ports
= tswap32(port
->vser
->config
.max_nr_ports
);
889 if (port
->id
>= max_nr_ports
) {
890 error_report("virtio-serial-bus: Out-of-range port id specified, max. allowed: %u",
895 ret
= vsc
->init(port
);
900 port
->elem
.out_num
= 0;
902 QTAILQ_INSERT_TAIL(&port
->vser
->ports
, port
, next
);
903 port
->ivq
= port
->vser
->ivqs
[port
->id
];
904 port
->ovq
= port
->vser
->ovqs
[port
->id
];
906 add_port(port
->vser
, port
->id
);
908 /* Send an update to the guest about this new port added */
909 virtio_notify_config(&port
->vser
->vdev
);
914 static int virtser_port_qdev_exit(DeviceState
*qdev
)
916 VirtIOSerialPort
*port
= DO_UPCAST(VirtIOSerialPort
, dev
, qdev
);
917 VirtIOSerialPortClass
*vsc
= VIRTIO_SERIAL_PORT_GET_CLASS(port
);
918 VirtIOSerial
*vser
= port
->vser
;
920 qemu_bh_delete(port
->bh
);
921 remove_port(port
->vser
, port
->id
);
923 QTAILQ_REMOVE(&vser
->ports
, port
, next
);
931 VirtIODevice
*virtio_serial_init(DeviceState
*dev
, virtio_serial_conf
*conf
)
935 uint32_t i
, max_supported_ports
;
937 if (!conf
->max_virtserial_ports
)
940 /* Each port takes 2 queues, and one pair is for the control queue */
941 max_supported_ports
= VIRTIO_PCI_QUEUE_MAX
/ 2 - 1;
943 if (conf
->max_virtserial_ports
> max_supported_ports
) {
944 error_report("maximum ports supported: %u", max_supported_ports
);
948 vdev
= virtio_common_init("virtio-serial", VIRTIO_ID_CONSOLE
,
949 sizeof(struct virtio_console_config
),
950 sizeof(VirtIOSerial
));
952 vser
= DO_UPCAST(VirtIOSerial
, vdev
, vdev
);
954 /* Spawn a new virtio-serial bus on which the ports will ride as devices */
955 qbus_create_inplace(&vser
->bus
.qbus
, TYPE_VIRTIO_SERIAL_BUS
, dev
, NULL
);
956 vser
->bus
.qbus
.allow_hotplug
= 1;
957 vser
->bus
.vser
= vser
;
958 QTAILQ_INIT(&vser
->ports
);
960 vser
->bus
.max_nr_ports
= conf
->max_virtserial_ports
;
961 vser
->ivqs
= g_malloc(conf
->max_virtserial_ports
* sizeof(VirtQueue
*));
962 vser
->ovqs
= g_malloc(conf
->max_virtserial_ports
* sizeof(VirtQueue
*));
964 /* Add a queue for host to guest transfers for port 0 (backward compat) */
965 vser
->ivqs
[0] = virtio_add_queue(vdev
, 128, handle_input
);
966 /* Add a queue for guest to host transfers for port 0 (backward compat) */
967 vser
->ovqs
[0] = virtio_add_queue(vdev
, 128, handle_output
);
969 /* TODO: host to guest notifications can get dropped
970 * if the queue fills up. Implement queueing in host,
971 * this might also make it possible to reduce the control
972 * queue size: as guest preposts buffers there,
973 * this will save 4Kbyte of guest memory per entry. */
975 /* control queue: host to guest */
976 vser
->c_ivq
= virtio_add_queue(vdev
, 32, control_in
);
977 /* control queue: guest to host */
978 vser
->c_ovq
= virtio_add_queue(vdev
, 32, control_out
);
980 for (i
= 1; i
< vser
->bus
.max_nr_ports
; i
++) {
981 /* Add a per-port queue for host to guest transfers */
982 vser
->ivqs
[i
] = virtio_add_queue(vdev
, 128, handle_input
);
983 /* Add a per-per queue for guest to host transfers */
984 vser
->ovqs
[i
] = virtio_add_queue(vdev
, 128, handle_output
);
987 vser
->config
.max_nr_ports
= tswap32(conf
->max_virtserial_ports
);
988 vser
->ports_map
= g_malloc0(((conf
->max_virtserial_ports
+ 31) / 32)
989 * sizeof(vser
->ports_map
[0]));
991 * Reserve location 0 for a console port for backward compat
992 * (old kernel, new qemu)
994 mark_port_added(vser
, 0);
996 vser
->vdev
.get_features
= get_features
;
997 vser
->vdev
.get_config
= get_config
;
998 vser
->vdev
.set_config
= set_config
;
999 vser
->vdev
.set_status
= set_status
;
1000 vser
->vdev
.reset
= vser_reset
;
1004 vser
->post_load
= NULL
;
1007 * Register for the savevm section with the virtio-console name
1008 * to preserve backward compat
1010 register_savevm(dev
, "virtio-console", -1, 3, virtio_serial_save
,
1011 virtio_serial_load
, vser
);
1016 void virtio_serial_exit(VirtIODevice
*vdev
)
1018 VirtIOSerial
*vser
= DO_UPCAST(VirtIOSerial
, vdev
, vdev
);
1020 unregister_savevm(vser
->qdev
, "virtio-console", vser
);
1024 g_free(vser
->ports_map
);
1025 if (vser
->post_load
) {
1026 g_free(vser
->post_load
->connected
);
1027 qemu_del_timer(vser
->post_load
->timer
);
1028 qemu_free_timer(vser
->post_load
->timer
);
1029 g_free(vser
->post_load
);
1031 virtio_cleanup(vdev
);
1034 static void virtio_serial_port_class_init(ObjectClass
*klass
, void *data
)
1036 DeviceClass
*k
= DEVICE_CLASS(klass
);
1037 k
->init
= virtser_port_qdev_init
;
1038 k
->bus_type
= TYPE_VIRTIO_SERIAL_BUS
;
1039 k
->exit
= virtser_port_qdev_exit
;
1040 k
->unplug
= qdev_simple_unplug_cb
;
1041 k
->props
= virtser_props
;
1044 static const TypeInfo virtio_serial_port_type_info
= {
1045 .name
= TYPE_VIRTIO_SERIAL_PORT
,
1046 .parent
= TYPE_DEVICE
,
1047 .instance_size
= sizeof(VirtIOSerialPort
),
1049 .class_size
= sizeof(VirtIOSerialPortClass
),
1050 .class_init
= virtio_serial_port_class_init
,
1053 static void virtio_serial_register_types(void)
1055 type_register_static(&virtser_bus_info
);
1056 type_register_static(&virtio_serial_port_type_info
);
1059 type_init(virtio_serial_register_types
)