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.
21 #include "qemu/osdep.h"
23 #include "monitor/monitor.h"
24 #include "qemu/error-report.h"
25 #include "qemu/queue.h"
26 #include "hw/sysbus.h"
28 #include "hw/virtio/virtio-serial.h"
29 #include "hw/virtio/virtio-access.h"
31 static struct VirtIOSerialDevices
{
32 QLIST_HEAD(, VirtIOSerial
) devices
;
35 static VirtIOSerialPort
*find_port_by_id(VirtIOSerial
*vser
, uint32_t id
)
37 VirtIOSerialPort
*port
;
39 if (id
== VIRTIO_CONSOLE_BAD_ID
) {
43 QTAILQ_FOREACH(port
, &vser
->ports
, next
) {
50 static VirtIOSerialPort
*find_port_by_vq(VirtIOSerial
*vser
, VirtQueue
*vq
)
52 VirtIOSerialPort
*port
;
54 QTAILQ_FOREACH(port
, &vser
->ports
, next
) {
55 if (port
->ivq
== vq
|| port
->ovq
== vq
)
61 static VirtIOSerialPort
*find_port_by_name(char *name
)
65 QLIST_FOREACH(vser
, &vserdevices
.devices
, next
) {
66 VirtIOSerialPort
*port
;
68 QTAILQ_FOREACH(port
, &vser
->ports
, next
) {
69 if (port
->name
&& !strcmp(port
->name
, name
)) {
77 static bool use_multiport(VirtIOSerial
*vser
)
79 VirtIODevice
*vdev
= VIRTIO_DEVICE(vser
);
80 return virtio_vdev_has_feature(vdev
, VIRTIO_CONSOLE_F_MULTIPORT
);
83 static size_t write_to_port(VirtIOSerialPort
*port
,
84 const uint8_t *buf
, size_t size
)
86 VirtQueueElement
*elem
;
91 if (!virtio_queue_ready(vq
)) {
96 while (offset
< size
) {
99 elem
= virtqueue_pop(vq
, sizeof(VirtQueueElement
));
104 len
= iov_from_buf(elem
->in_sg
, elem
->in_num
, 0,
105 buf
+ offset
, size
- offset
);
108 virtqueue_push(vq
, elem
, len
);
112 virtio_notify(VIRTIO_DEVICE(port
->vser
), vq
);
116 static void discard_vq_data(VirtQueue
*vq
, VirtIODevice
*vdev
)
118 VirtQueueElement
*elem
;
120 if (!virtio_queue_ready(vq
)) {
124 elem
= virtqueue_pop(vq
, sizeof(VirtQueueElement
));
128 virtqueue_push(vq
, elem
, 0);
131 virtio_notify(vdev
, vq
);
134 static void do_flush_queued_data(VirtIOSerialPort
*port
, VirtQueue
*vq
,
137 VirtIOSerialPortClass
*vsc
;
140 assert(virtio_queue_ready(vq
));
142 vsc
= VIRTIO_SERIAL_PORT_GET_CLASS(port
);
144 while (!port
->throttled
) {
147 /* Pop an elem only if we haven't left off a previous one mid-way */
149 port
->elem
= virtqueue_pop(vq
, sizeof(VirtQueueElement
));
154 port
->iov_offset
= 0;
157 for (i
= port
->iov_idx
; i
< port
->elem
->out_num
; i
++) {
161 buf_size
= port
->elem
->out_sg
[i
].iov_len
- port
->iov_offset
;
162 ret
= vsc
->have_data(port
,
163 port
->elem
->out_sg
[i
].iov_base
166 if (port
->throttled
) {
169 port
->iov_offset
+= ret
;
173 port
->iov_offset
= 0;
175 if (port
->throttled
) {
178 virtqueue_push(vq
, port
->elem
, 0);
182 virtio_notify(vdev
, vq
);
185 static void flush_queued_data(VirtIOSerialPort
*port
)
189 if (!virtio_queue_ready(port
->ovq
)) {
192 do_flush_queued_data(port
, port
->ovq
, VIRTIO_DEVICE(port
->vser
));
195 static size_t send_control_msg(VirtIOSerial
*vser
, void *buf
, size_t len
)
197 VirtQueueElement
*elem
;
201 if (!virtio_queue_ready(vq
)) {
205 elem
= virtqueue_pop(vq
, sizeof(VirtQueueElement
));
210 /* TODO: detect a buffer that's too short, set NEEDS_RESET */
211 iov_from_buf(elem
->in_sg
, elem
->in_num
, 0, buf
, len
);
213 virtqueue_push(vq
, elem
, len
);
214 virtio_notify(VIRTIO_DEVICE(vser
), vq
);
220 static size_t send_control_event(VirtIOSerial
*vser
, uint32_t port_id
,
221 uint16_t event
, uint16_t value
)
223 VirtIODevice
*vdev
= VIRTIO_DEVICE(vser
);
224 struct virtio_console_control cpkt
;
226 virtio_stl_p(vdev
, &cpkt
.id
, port_id
);
227 virtio_stw_p(vdev
, &cpkt
.event
, event
);
228 virtio_stw_p(vdev
, &cpkt
.value
, value
);
230 trace_virtio_serial_send_control_event(port_id
, event
, value
);
231 return send_control_msg(vser
, &cpkt
, sizeof(cpkt
));
234 /* Functions for use inside qemu to open and read from/write to ports */
235 int virtio_serial_open(VirtIOSerialPort
*port
)
237 /* Don't allow opening an already-open port */
238 if (port
->host_connected
) {
241 /* Send port open notification to the guest */
242 port
->host_connected
= true;
243 send_control_event(port
->vser
, port
->id
, VIRTIO_CONSOLE_PORT_OPEN
, 1);
248 int virtio_serial_close(VirtIOSerialPort
*port
)
250 port
->host_connected
= false;
252 * If there's any data the guest sent which the app didn't
253 * consume, reset the throttling flag and discard the data.
255 port
->throttled
= false;
256 discard_vq_data(port
->ovq
, VIRTIO_DEVICE(port
->vser
));
258 send_control_event(port
->vser
, port
->id
, VIRTIO_CONSOLE_PORT_OPEN
, 0);
263 /* Individual ports/apps call this function to write to the guest. */
264 ssize_t
virtio_serial_write(VirtIOSerialPort
*port
, const uint8_t *buf
,
267 if (!port
|| !port
->host_connected
|| !port
->guest_connected
) {
270 return write_to_port(port
, buf
, size
);
274 * Readiness of the guest to accept data on a port.
275 * Returns max. data the guest can receive
277 size_t virtio_serial_guest_ready(VirtIOSerialPort
*port
)
279 VirtIODevice
*vdev
= VIRTIO_DEVICE(port
->vser
);
280 VirtQueue
*vq
= port
->ivq
;
283 if (!virtio_queue_ready(vq
) ||
284 !(vdev
->status
& VIRTIO_CONFIG_S_DRIVER_OK
) ||
285 virtio_queue_empty(vq
)) {
288 if (use_multiport(port
->vser
) && !port
->guest_connected
) {
291 virtqueue_get_avail_bytes(vq
, &bytes
, NULL
, 4096, 0);
295 static void flush_queued_data_bh(void *opaque
)
297 VirtIOSerialPort
*port
= opaque
;
299 flush_queued_data(port
);
302 void virtio_serial_throttle_port(VirtIOSerialPort
*port
, bool throttle
)
308 trace_virtio_serial_throttle_port(port
->id
, throttle
);
309 port
->throttled
= throttle
;
313 qemu_bh_schedule(port
->bh
);
316 /* Guest wants to notify us of some event */
317 static void handle_control_message(VirtIOSerial
*vser
, void *buf
, size_t len
)
319 VirtIODevice
*vdev
= VIRTIO_DEVICE(vser
);
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
= virtio_lduw_p(vdev
, &gcpkt
->event
);
334 cpkt
.value
= virtio_lduw_p(vdev
, &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
, virtio_ldl_p(vdev
, &gcpkt
->id
));
356 error_report("virtio-serial-bus: Unexpected port id %u for device %s",
357 virtio_ldl_p(vdev
, &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 virtio_stl_p(vdev
, &cpkt
.id
, port
->id
);
385 virtio_stw_p(vdev
, &cpkt
.event
, VIRTIO_CONSOLE_PORT_NAME
);
386 virtio_stw_p(vdev
, &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 (vsc
->set_guest_connected
) {
417 /* Send the guest opened notification if an app is interested */
418 vsc
->set_guest_connected(port
, cpkt
.value
);
424 static void control_in(VirtIODevice
*vdev
, VirtQueue
*vq
)
428 static void control_out(VirtIODevice
*vdev
, VirtQueue
*vq
)
430 VirtQueueElement
*elem
;
435 vser
= VIRTIO_SERIAL(vdev
);
442 elem
= virtqueue_pop(vq
, sizeof(VirtQueueElement
));
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);
465 virtio_notify(vdev
, vq
);
468 /* Guest wrote something to some port. */
469 static void handle_output(VirtIODevice
*vdev
, VirtQueue
*vq
)
472 VirtIOSerialPort
*port
;
474 vser
= VIRTIO_SERIAL(vdev
);
475 port
= find_port_by_vq(vser
, vq
);
477 if (!port
|| !port
->host_connected
) {
478 discard_vq_data(vq
, vdev
);
482 if (!port
->throttled
) {
483 do_flush_queued_data(port
, vq
, vdev
);
488 static void handle_input(VirtIODevice
*vdev
, VirtQueue
*vq
)
491 * Users of virtio-serial would like to know when guest becomes
492 * writable again -- i.e. if a vq had stuff queued up and the
493 * guest wasn't reading at all, the host would not be able to
494 * write to the vq anymore. Once the guest reads off something,
495 * we can start queueing things up again. However, this call is
496 * made for each buffer addition by the guest -- even though free
497 * buffers existed prior to the current buffer addition. This is
498 * done so as not to maintain previous state, which will need
499 * additional live-migration-related changes.
502 VirtIOSerialPort
*port
;
503 VirtIOSerialPortClass
*vsc
;
505 vser
= VIRTIO_SERIAL(vdev
);
506 port
= find_port_by_vq(vser
, vq
);
511 vsc
= VIRTIO_SERIAL_PORT_GET_CLASS(port
);
514 * If guest_connected is false, this call is being made by the
515 * early-boot queueing up of descriptors, which is just noise for
516 * the host apps -- don't disturb them in that case.
518 if (port
->guest_connected
&& port
->host_connected
&& vsc
->guest_writable
) {
519 vsc
->guest_writable(port
);
523 static uint64_t get_features(VirtIODevice
*vdev
, uint64_t features
,
528 vser
= VIRTIO_SERIAL(vdev
);
530 if (vser
->bus
.max_nr_ports
> 1) {
531 virtio_add_feature(&features
, VIRTIO_CONSOLE_F_MULTIPORT
);
536 /* Guest requested config info */
537 static void get_config(VirtIODevice
*vdev
, uint8_t *config_data
)
539 VirtIOSerial
*vser
= VIRTIO_SERIAL(vdev
);
540 struct virtio_console_config
*config
=
541 (struct virtio_console_config
*)config_data
;
545 config
->max_nr_ports
= virtio_tswap32(vdev
,
546 vser
->serial
.max_virtserial_ports
);
549 static void guest_reset(VirtIOSerial
*vser
)
551 VirtIOSerialPort
*port
;
552 VirtIOSerialPortClass
*vsc
;
554 QTAILQ_FOREACH(port
, &vser
->ports
, next
) {
555 vsc
= VIRTIO_SERIAL_PORT_GET_CLASS(port
);
556 if (port
->guest_connected
) {
557 port
->guest_connected
= false;
558 if (vsc
->set_guest_connected
) {
559 vsc
->set_guest_connected(port
, false);
565 static void set_status(VirtIODevice
*vdev
, uint8_t status
)
568 VirtIOSerialPort
*port
;
570 vser
= VIRTIO_SERIAL(vdev
);
571 port
= find_port_by_id(vser
, 0);
573 if (port
&& !use_multiport(port
->vser
)
574 && (status
& VIRTIO_CONFIG_S_DRIVER_OK
)) {
576 * Non-multiport guests won't be able to tell us guest
577 * open/close status. Such guests can only have a port at id
578 * 0, so set guest_connected for such ports as soon as guest
581 port
->guest_connected
= true;
583 if (!(status
& VIRTIO_CONFIG_S_DRIVER_OK
)) {
588 static void vser_reset(VirtIODevice
*vdev
)
592 vser
= VIRTIO_SERIAL(vdev
);
596 static void virtio_serial_save(QEMUFile
*f
, void *opaque
)
598 /* The virtio device */
599 virtio_save(VIRTIO_DEVICE(opaque
), f
);
602 static void virtio_serial_save_device(VirtIODevice
*vdev
, QEMUFile
*f
)
604 VirtIOSerial
*s
= VIRTIO_SERIAL(vdev
);
605 VirtIOSerialPort
*port
;
606 uint32_t nr_active_ports
;
607 unsigned int i
, max_nr_ports
;
608 struct virtio_console_config config
;
610 /* The config space (ignored on the far end in current versions) */
611 get_config(vdev
, (uint8_t *)&config
);
612 qemu_put_be16s(f
, &config
.cols
);
613 qemu_put_be16s(f
, &config
.rows
);
614 qemu_put_be32s(f
, &config
.max_nr_ports
);
617 max_nr_ports
= s
->serial
.max_virtserial_ports
;
618 for (i
= 0; i
< (max_nr_ports
+ 31) / 32; i
++) {
619 qemu_put_be32s(f
, &s
->ports_map
[i
]);
625 QTAILQ_FOREACH(port
, &s
->ports
, next
) {
629 qemu_put_be32s(f
, &nr_active_ports
);
632 * Items in struct VirtIOSerialPort.
634 QTAILQ_FOREACH(port
, &s
->ports
, next
) {
635 uint32_t elem_popped
;
637 qemu_put_be32s(f
, &port
->id
);
638 qemu_put_byte(f
, port
->guest_connected
);
639 qemu_put_byte(f
, port
->host_connected
);
645 qemu_put_be32s(f
, &elem_popped
);
647 qemu_put_be32s(f
, &port
->iov_idx
);
648 qemu_put_be64s(f
, &port
->iov_offset
);
649 qemu_put_virtqueue_element(f
, port
->elem
);
654 static void virtio_serial_post_load_timer_cb(void *opaque
)
657 VirtIOSerial
*s
= VIRTIO_SERIAL(opaque
);
658 VirtIOSerialPort
*port
;
659 uint8_t host_connected
;
660 VirtIOSerialPortClass
*vsc
;
665 for (i
= 0 ; i
< s
->post_load
->nr_active_ports
; ++i
) {
666 port
= s
->post_load
->connected
[i
].port
;
667 host_connected
= s
->post_load
->connected
[i
].host_connected
;
668 if (host_connected
!= port
->host_connected
) {
670 * We have to let the guest know of the host connection
673 send_control_event(s
, port
->id
, VIRTIO_CONSOLE_PORT_OPEN
,
674 port
->host_connected
);
676 vsc
= VIRTIO_SERIAL_PORT_GET_CLASS(port
);
677 if (vsc
->set_guest_connected
) {
678 vsc
->set_guest_connected(port
, port
->guest_connected
);
681 g_free(s
->post_load
->connected
);
682 timer_free(s
->post_load
->timer
);
683 g_free(s
->post_load
);
687 static int fetch_active_ports_list(QEMUFile
*f
, int version_id
,
688 VirtIOSerial
*s
, uint32_t nr_active_ports
)
692 s
->post_load
= g_malloc0(sizeof(*s
->post_load
));
693 s
->post_load
->nr_active_ports
= nr_active_ports
;
694 s
->post_load
->connected
=
695 g_malloc0(sizeof(*s
->post_load
->connected
) * nr_active_ports
);
697 s
->post_load
->timer
= timer_new_ns(QEMU_CLOCK_VIRTUAL
,
698 virtio_serial_post_load_timer_cb
,
701 /* Items in struct VirtIOSerialPort */
702 for (i
= 0; i
< nr_active_ports
; i
++) {
703 VirtIOSerialPort
*port
;
706 id
= qemu_get_be32(f
);
707 port
= find_port_by_id(s
, id
);
712 port
->guest_connected
= qemu_get_byte(f
);
713 s
->post_load
->connected
[i
].port
= port
;
714 s
->post_load
->connected
[i
].host_connected
= qemu_get_byte(f
);
716 if (version_id
> 2) {
717 uint32_t elem_popped
;
719 qemu_get_be32s(f
, &elem_popped
);
721 qemu_get_be32s(f
, &port
->iov_idx
);
722 qemu_get_be64s(f
, &port
->iov_offset
);
725 qemu_get_virtqueue_element(f
, sizeof(VirtQueueElement
));
728 * Port was throttled on source machine. Let's
729 * unthrottle it here so data starts flowing again.
731 virtio_serial_throttle_port(port
, false);
735 timer_mod(s
->post_load
->timer
, 1);
739 static int virtio_serial_load(QEMUFile
*f
, void *opaque
, int version_id
)
741 if (version_id
> 3) {
745 /* The virtio device */
746 return virtio_load(VIRTIO_DEVICE(opaque
), f
, version_id
);
749 static int virtio_serial_load_device(VirtIODevice
*vdev
, QEMUFile
*f
,
752 VirtIOSerial
*s
= VIRTIO_SERIAL(vdev
);
753 uint32_t max_nr_ports
, nr_active_ports
, ports_map
;
758 if (version_id
< 2) {
763 qemu_get_be16s(f
, (uint16_t *) &tmp
);
764 qemu_get_be16s(f
, (uint16_t *) &tmp
);
765 qemu_get_be32s(f
, &tmp
);
767 max_nr_ports
= s
->serial
.max_virtserial_ports
;
768 for (i
= 0; i
< (max_nr_ports
+ 31) / 32; i
++) {
769 qemu_get_be32s(f
, &ports_map
);
771 if (ports_map
!= s
->ports_map
[i
]) {
773 * Ports active on source and destination don't
774 * match. Fail migration.
780 qemu_get_be32s(f
, &nr_active_ports
);
782 if (nr_active_ports
) {
783 ret
= fetch_active_ports_list(f
, version_id
, s
, nr_active_ports
);
791 static void virtser_bus_dev_print(Monitor
*mon
, DeviceState
*qdev
, int indent
);
793 static Property virtser_props
[] = {
794 DEFINE_PROP_UINT32("nr", VirtIOSerialPort
, id
, VIRTIO_CONSOLE_BAD_ID
),
795 DEFINE_PROP_STRING("name", VirtIOSerialPort
, name
),
796 DEFINE_PROP_END_OF_LIST()
799 #define TYPE_VIRTIO_SERIAL_BUS "virtio-serial-bus"
800 #define VIRTIO_SERIAL_BUS(obj) \
801 OBJECT_CHECK(VirtIOSerialBus, (obj), TYPE_VIRTIO_SERIAL_BUS)
803 static void virtser_bus_class_init(ObjectClass
*klass
, void *data
)
805 BusClass
*k
= BUS_CLASS(klass
);
806 k
->print_dev
= virtser_bus_dev_print
;
809 static const TypeInfo virtser_bus_info
= {
810 .name
= TYPE_VIRTIO_SERIAL_BUS
,
812 .instance_size
= sizeof(VirtIOSerialBus
),
813 .class_init
= virtser_bus_class_init
,
816 static void virtser_bus_dev_print(Monitor
*mon
, DeviceState
*qdev
, int indent
)
818 VirtIOSerialPort
*port
= VIRTIO_SERIAL_PORT(qdev
);
820 monitor_printf(mon
, "%*sport %d, guest %s, host %s, throttle %s\n",
821 indent
, "", port
->id
,
822 port
->guest_connected
? "on" : "off",
823 port
->host_connected
? "on" : "off",
824 port
->throttled
? "on" : "off");
827 /* This function is only used if a port id is not provided by the user */
828 static uint32_t find_free_port_id(VirtIOSerial
*vser
)
830 unsigned int i
, max_nr_ports
;
832 max_nr_ports
= vser
->serial
.max_virtserial_ports
;
833 for (i
= 0; i
< (max_nr_ports
+ 31) / 32; i
++) {
834 uint32_t map
, zeroes
;
836 map
= vser
->ports_map
[i
];
837 zeroes
= ctz32(~map
);
839 return zeroes
+ i
* 32;
842 return VIRTIO_CONSOLE_BAD_ID
;
845 static void mark_port_added(VirtIOSerial
*vser
, uint32_t port_id
)
850 vser
->ports_map
[i
] |= 1U << (port_id
% 32);
853 static void add_port(VirtIOSerial
*vser
, uint32_t port_id
)
855 mark_port_added(vser
, port_id
);
856 send_control_event(vser
, port_id
, VIRTIO_CONSOLE_PORT_ADD
, 1);
859 static void remove_port(VirtIOSerial
*vser
, uint32_t port_id
)
861 VirtIOSerialPort
*port
;
864 * Don't mark port 0 removed -- we explicitly reserve it for
865 * backward compat with older guests, ensure a virtconsole device
866 * unplug retains the reservation.
872 vser
->ports_map
[i
] &= ~(1U << (port_id
% 32));
875 port
= find_port_by_id(vser
, port_id
);
877 * This function is only called from qdev's unplug callback; if we
878 * get a NULL port here, we're in trouble.
882 /* Flush out any unconsumed buffers first */
883 discard_vq_data(port
->ovq
, VIRTIO_DEVICE(port
->vser
));
885 send_control_event(vser
, port
->id
, VIRTIO_CONSOLE_PORT_REMOVE
, 1);
888 static void virtser_port_device_realize(DeviceState
*dev
, Error
**errp
)
890 VirtIOSerialPort
*port
= VIRTIO_SERIAL_PORT(dev
);
891 VirtIOSerialPortClass
*vsc
= VIRTIO_SERIAL_PORT_GET_CLASS(port
);
892 VirtIOSerialBus
*bus
= VIRTIO_SERIAL_BUS(qdev_get_parent_bus(dev
));
897 port
->vser
= bus
->vser
;
898 port
->bh
= qemu_bh_new(flush_queued_data_bh
, port
);
900 assert(vsc
->have_data
);
903 * Is the first console port we're seeing? If so, put it up at
904 * location 0. This is done for backward compatibility (old
907 plugging_port0
= vsc
->is_console
&& !find_port_by_id(port
->vser
, 0);
909 if (find_port_by_id(port
->vser
, port
->id
)) {
910 error_setg(errp
, "virtio-serial-bus: A port already exists at id %u",
915 if (port
->name
!= NULL
&& find_port_by_name(port
->name
)) {
916 error_setg(errp
, "virtio-serial-bus: A port already exists by name %s",
921 if (port
->id
== VIRTIO_CONSOLE_BAD_ID
) {
922 if (plugging_port0
) {
925 port
->id
= find_free_port_id(port
->vser
);
926 if (port
->id
== VIRTIO_CONSOLE_BAD_ID
) {
927 error_setg(errp
, "virtio-serial-bus: Maximum port limit for "
928 "this device reached");
934 max_nr_ports
= port
->vser
->serial
.max_virtserial_ports
;
935 if (port
->id
>= max_nr_ports
) {
936 error_setg(errp
, "virtio-serial-bus: Out-of-range port id specified, "
937 "max. allowed: %u", max_nr_ports
- 1);
941 vsc
->realize(dev
, &err
);
943 error_propagate(errp
, err
);
950 static void virtser_port_device_plug(HotplugHandler
*hotplug_dev
,
951 DeviceState
*dev
, Error
**errp
)
953 VirtIOSerialPort
*port
= VIRTIO_SERIAL_PORT(dev
);
955 QTAILQ_INSERT_TAIL(&port
->vser
->ports
, port
, next
);
956 port
->ivq
= port
->vser
->ivqs
[port
->id
];
957 port
->ovq
= port
->vser
->ovqs
[port
->id
];
959 add_port(port
->vser
, port
->id
);
961 /* Send an update to the guest about this new port added */
962 virtio_notify_config(VIRTIO_DEVICE(hotplug_dev
));
965 static void virtser_port_device_unrealize(DeviceState
*dev
, Error
**errp
)
967 VirtIOSerialPort
*port
= VIRTIO_SERIAL_PORT(dev
);
968 VirtIOSerialPortClass
*vsc
= VIRTIO_SERIAL_PORT_GET_CLASS(dev
);
969 VirtIOSerial
*vser
= port
->vser
;
971 qemu_bh_delete(port
->bh
);
972 remove_port(port
->vser
, port
->id
);
974 QTAILQ_REMOVE(&vser
->ports
, port
, next
);
976 if (vsc
->unrealize
) {
977 vsc
->unrealize(dev
, errp
);
981 static void virtio_serial_device_realize(DeviceState
*dev
, Error
**errp
)
983 VirtIODevice
*vdev
= VIRTIO_DEVICE(dev
);
984 VirtIOSerial
*vser
= VIRTIO_SERIAL(dev
);
985 uint32_t i
, max_supported_ports
;
987 if (!vser
->serial
.max_virtserial_ports
) {
988 error_setg(errp
, "Maximum number of serial ports not specified");
992 /* Each port takes 2 queues, and one pair is for the control queue */
993 max_supported_ports
= VIRTIO_QUEUE_MAX
/ 2 - 1;
995 if (vser
->serial
.max_virtserial_ports
> max_supported_ports
) {
996 error_setg(errp
, "maximum ports supported: %u", max_supported_ports
);
1000 /* We don't support emergency write, skip it for now. */
1001 /* TODO: cleaner fix, depending on host features. */
1002 virtio_init(vdev
, "virtio-serial", VIRTIO_ID_CONSOLE
,
1003 offsetof(struct virtio_console_config
, emerg_wr
));
1005 /* Spawn a new virtio-serial bus on which the ports will ride as devices */
1006 qbus_create_inplace(&vser
->bus
, sizeof(vser
->bus
), TYPE_VIRTIO_SERIAL_BUS
,
1007 dev
, vdev
->bus_name
);
1008 qbus_set_hotplug_handler(BUS(&vser
->bus
), DEVICE(vser
), errp
);
1009 vser
->bus
.vser
= vser
;
1010 QTAILQ_INIT(&vser
->ports
);
1012 vser
->bus
.max_nr_ports
= vser
->serial
.max_virtserial_ports
;
1013 vser
->ivqs
= g_malloc(vser
->serial
.max_virtserial_ports
1014 * sizeof(VirtQueue
*));
1015 vser
->ovqs
= g_malloc(vser
->serial
.max_virtserial_ports
1016 * sizeof(VirtQueue
*));
1018 /* Add a queue for host to guest transfers for port 0 (backward compat) */
1019 vser
->ivqs
[0] = virtio_add_queue(vdev
, 128, handle_input
);
1020 /* Add a queue for guest to host transfers for port 0 (backward compat) */
1021 vser
->ovqs
[0] = virtio_add_queue(vdev
, 128, handle_output
);
1023 /* TODO: host to guest notifications can get dropped
1024 * if the queue fills up. Implement queueing in host,
1025 * this might also make it possible to reduce the control
1026 * queue size: as guest preposts buffers there,
1027 * this will save 4Kbyte of guest memory per entry. */
1029 /* control queue: host to guest */
1030 vser
->c_ivq
= virtio_add_queue(vdev
, 32, control_in
);
1031 /* control queue: guest to host */
1032 vser
->c_ovq
= virtio_add_queue(vdev
, 32, control_out
);
1034 for (i
= 1; i
< vser
->bus
.max_nr_ports
; i
++) {
1035 /* Add a per-port queue for host to guest transfers */
1036 vser
->ivqs
[i
] = virtio_add_queue(vdev
, 128, handle_input
);
1037 /* Add a per-per queue for guest to host transfers */
1038 vser
->ovqs
[i
] = virtio_add_queue(vdev
, 128, handle_output
);
1041 vser
->ports_map
= g_malloc0(((vser
->serial
.max_virtserial_ports
+ 31) / 32)
1042 * sizeof(vser
->ports_map
[0]));
1044 * Reserve location 0 for a console port for backward compat
1045 * (old kernel, new qemu)
1047 mark_port_added(vser
, 0);
1049 vser
->post_load
= NULL
;
1052 * Register for the savevm section with the virtio-console name
1053 * to preserve backward compat
1055 register_savevm(dev
, "virtio-console", -1, 3, virtio_serial_save
,
1056 virtio_serial_load
, vser
);
1058 QLIST_INSERT_HEAD(&vserdevices
.devices
, vser
, next
);
1061 static void virtio_serial_port_class_init(ObjectClass
*klass
, void *data
)
1063 DeviceClass
*k
= DEVICE_CLASS(klass
);
1065 set_bit(DEVICE_CATEGORY_INPUT
, k
->categories
);
1066 k
->bus_type
= TYPE_VIRTIO_SERIAL_BUS
;
1067 k
->realize
= virtser_port_device_realize
;
1068 k
->unrealize
= virtser_port_device_unrealize
;
1069 k
->props
= virtser_props
;
1072 static const TypeInfo virtio_serial_port_type_info
= {
1073 .name
= TYPE_VIRTIO_SERIAL_PORT
,
1074 .parent
= TYPE_DEVICE
,
1075 .instance_size
= sizeof(VirtIOSerialPort
),
1077 .class_size
= sizeof(VirtIOSerialPortClass
),
1078 .class_init
= virtio_serial_port_class_init
,
1081 static void virtio_serial_device_unrealize(DeviceState
*dev
, Error
**errp
)
1083 VirtIODevice
*vdev
= VIRTIO_DEVICE(dev
);
1084 VirtIOSerial
*vser
= VIRTIO_SERIAL(dev
);
1086 QLIST_REMOVE(vser
, next
);
1088 unregister_savevm(dev
, "virtio-console", vser
);
1092 g_free(vser
->ports_map
);
1093 if (vser
->post_load
) {
1094 g_free(vser
->post_load
->connected
);
1095 timer_del(vser
->post_load
->timer
);
1096 timer_free(vser
->post_load
->timer
);
1097 g_free(vser
->post_load
);
1099 virtio_cleanup(vdev
);
1102 static Property virtio_serial_properties
[] = {
1103 DEFINE_PROP_UINT32("max_ports", VirtIOSerial
, serial
.max_virtserial_ports
,
1105 DEFINE_PROP_END_OF_LIST(),
1108 static void virtio_serial_class_init(ObjectClass
*klass
, void *data
)
1110 DeviceClass
*dc
= DEVICE_CLASS(klass
);
1111 VirtioDeviceClass
*vdc
= VIRTIO_DEVICE_CLASS(klass
);
1112 HotplugHandlerClass
*hc
= HOTPLUG_HANDLER_CLASS(klass
);
1114 QLIST_INIT(&vserdevices
.devices
);
1116 dc
->props
= virtio_serial_properties
;
1117 set_bit(DEVICE_CATEGORY_INPUT
, dc
->categories
);
1118 vdc
->realize
= virtio_serial_device_realize
;
1119 vdc
->unrealize
= virtio_serial_device_unrealize
;
1120 vdc
->get_features
= get_features
;
1121 vdc
->get_config
= get_config
;
1122 vdc
->set_status
= set_status
;
1123 vdc
->reset
= vser_reset
;
1124 vdc
->save
= virtio_serial_save_device
;
1125 vdc
->load
= virtio_serial_load_device
;
1126 hc
->plug
= virtser_port_device_plug
;
1127 hc
->unplug
= qdev_simple_device_unplug_cb
;
1130 static const TypeInfo virtio_device_info
= {
1131 .name
= TYPE_VIRTIO_SERIAL
,
1132 .parent
= TYPE_VIRTIO_DEVICE
,
1133 .instance_size
= sizeof(VirtIOSerial
),
1134 .class_init
= virtio_serial_class_init
,
1135 .interfaces
= (InterfaceInfo
[]) {
1136 { TYPE_HOTPLUG_HANDLER
},
1141 static void virtio_serial_register_types(void)
1143 type_register_static(&virtser_bus_info
);
1144 type_register_static(&virtio_serial_port_type_info
);
1145 type_register_static(&virtio_device_info
);
1148 type_init(virtio_serial_register_types
)