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"
22 #include "qapi/error.h"
24 #include "monitor/monitor.h"
25 #include "qemu/error-report.h"
26 #include "qemu/queue.h"
27 #include "hw/sysbus.h"
29 #include "hw/virtio/virtio-serial.h"
30 #include "hw/virtio/virtio-access.h"
32 static struct VirtIOSerialDevices
{
33 QLIST_HEAD(, VirtIOSerial
) devices
;
36 static VirtIOSerialPort
*find_port_by_id(VirtIOSerial
*vser
, uint32_t id
)
38 VirtIOSerialPort
*port
;
40 if (id
== VIRTIO_CONSOLE_BAD_ID
) {
44 QTAILQ_FOREACH(port
, &vser
->ports
, next
) {
51 static VirtIOSerialPort
*find_port_by_vq(VirtIOSerial
*vser
, VirtQueue
*vq
)
53 VirtIOSerialPort
*port
;
55 QTAILQ_FOREACH(port
, &vser
->ports
, next
) {
56 if (port
->ivq
== vq
|| port
->ovq
== vq
)
62 static VirtIOSerialPort
*find_port_by_name(char *name
)
66 QLIST_FOREACH(vser
, &vserdevices
.devices
, next
) {
67 VirtIOSerialPort
*port
;
69 QTAILQ_FOREACH(port
, &vser
->ports
, next
) {
70 if (port
->name
&& !strcmp(port
->name
, name
)) {
78 static bool use_multiport(VirtIOSerial
*vser
)
80 VirtIODevice
*vdev
= VIRTIO_DEVICE(vser
);
81 return virtio_vdev_has_feature(vdev
, VIRTIO_CONSOLE_F_MULTIPORT
);
84 static size_t write_to_port(VirtIOSerialPort
*port
,
85 const uint8_t *buf
, size_t size
)
87 VirtQueueElement
*elem
;
92 if (!virtio_queue_ready(vq
)) {
97 while (offset
< size
) {
100 elem
= virtqueue_pop(vq
, sizeof(VirtQueueElement
));
105 len
= iov_from_buf(elem
->in_sg
, elem
->in_num
, 0,
106 buf
+ offset
, size
- offset
);
109 virtqueue_push(vq
, elem
, len
);
113 virtio_notify(VIRTIO_DEVICE(port
->vser
), vq
);
117 static void discard_vq_data(VirtQueue
*vq
, VirtIODevice
*vdev
)
119 VirtQueueElement
*elem
;
121 if (!virtio_queue_ready(vq
)) {
125 elem
= virtqueue_pop(vq
, sizeof(VirtQueueElement
));
129 virtqueue_push(vq
, elem
, 0);
132 virtio_notify(vdev
, vq
);
135 static void do_flush_queued_data(VirtIOSerialPort
*port
, VirtQueue
*vq
,
138 VirtIOSerialPortClass
*vsc
;
141 assert(virtio_queue_ready(vq
));
143 vsc
= VIRTIO_SERIAL_PORT_GET_CLASS(port
);
145 while (!port
->throttled
) {
148 /* Pop an elem only if we haven't left off a previous one mid-way */
150 port
->elem
= virtqueue_pop(vq
, sizeof(VirtQueueElement
));
155 port
->iov_offset
= 0;
158 for (i
= port
->iov_idx
; i
< port
->elem
->out_num
; i
++) {
162 buf_size
= port
->elem
->out_sg
[i
].iov_len
- port
->iov_offset
;
163 ret
= vsc
->have_data(port
,
164 port
->elem
->out_sg
[i
].iov_base
167 if (port
->throttled
) {
170 port
->iov_offset
+= ret
;
174 port
->iov_offset
= 0;
176 if (port
->throttled
) {
179 virtqueue_push(vq
, port
->elem
, 0);
183 virtio_notify(vdev
, vq
);
186 static void flush_queued_data(VirtIOSerialPort
*port
)
190 if (!virtio_queue_ready(port
->ovq
)) {
193 do_flush_queued_data(port
, port
->ovq
, VIRTIO_DEVICE(port
->vser
));
196 static size_t send_control_msg(VirtIOSerial
*vser
, void *buf
, size_t len
)
198 VirtQueueElement
*elem
;
202 if (!virtio_queue_ready(vq
)) {
206 elem
= virtqueue_pop(vq
, sizeof(VirtQueueElement
));
211 /* TODO: detect a buffer that's too short, set NEEDS_RESET */
212 iov_from_buf(elem
->in_sg
, elem
->in_num
, 0, buf
, len
);
214 virtqueue_push(vq
, elem
, len
);
215 virtio_notify(VIRTIO_DEVICE(vser
), vq
);
221 static size_t send_control_event(VirtIOSerial
*vser
, uint32_t port_id
,
222 uint16_t event
, uint16_t value
)
224 VirtIODevice
*vdev
= VIRTIO_DEVICE(vser
);
225 struct virtio_console_control cpkt
;
227 virtio_stl_p(vdev
, &cpkt
.id
, port_id
);
228 virtio_stw_p(vdev
, &cpkt
.event
, event
);
229 virtio_stw_p(vdev
, &cpkt
.value
, value
);
231 trace_virtio_serial_send_control_event(port_id
, event
, value
);
232 return send_control_msg(vser
, &cpkt
, sizeof(cpkt
));
235 /* Functions for use inside qemu to open and read from/write to ports */
236 int virtio_serial_open(VirtIOSerialPort
*port
)
238 /* Don't allow opening an already-open port */
239 if (port
->host_connected
) {
242 /* Send port open notification to the guest */
243 port
->host_connected
= true;
244 send_control_event(port
->vser
, port
->id
, VIRTIO_CONSOLE_PORT_OPEN
, 1);
249 int virtio_serial_close(VirtIOSerialPort
*port
)
251 port
->host_connected
= false;
253 * If there's any data the guest sent which the app didn't
254 * consume, reset the throttling flag and discard the data.
256 port
->throttled
= false;
257 discard_vq_data(port
->ovq
, VIRTIO_DEVICE(port
->vser
));
259 send_control_event(port
->vser
, port
->id
, VIRTIO_CONSOLE_PORT_OPEN
, 0);
264 /* Individual ports/apps call this function to write to the guest. */
265 ssize_t
virtio_serial_write(VirtIOSerialPort
*port
, const uint8_t *buf
,
268 if (!port
|| !port
->host_connected
|| !port
->guest_connected
) {
271 return write_to_port(port
, buf
, size
);
275 * Readiness of the guest to accept data on a port.
276 * Returns max. data the guest can receive
278 size_t virtio_serial_guest_ready(VirtIOSerialPort
*port
)
280 VirtIODevice
*vdev
= VIRTIO_DEVICE(port
->vser
);
281 VirtQueue
*vq
= port
->ivq
;
284 if (!virtio_queue_ready(vq
) ||
285 !(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 VirtIODevice
*vdev
= VIRTIO_DEVICE(vser
);
321 struct VirtIOSerialPort
*port
;
322 VirtIOSerialPortClass
*vsc
;
323 struct virtio_console_control cpkt
, *gcpkt
;
329 if (len
< sizeof(cpkt
)) {
330 /* The guest sent an invalid control packet */
334 cpkt
.event
= virtio_lduw_p(vdev
, &gcpkt
->event
);
335 cpkt
.value
= virtio_lduw_p(vdev
, &gcpkt
->value
);
337 trace_virtio_serial_handle_control_message(cpkt
.event
, cpkt
.value
);
339 if (cpkt
.event
== VIRTIO_CONSOLE_DEVICE_READY
) {
341 error_report("virtio-serial-bus: Guest failure in adding device %s",
342 vser
->bus
.qbus
.name
);
346 * The device is up, we can now tell the device about all the
347 * ports we have here.
349 QTAILQ_FOREACH(port
, &vser
->ports
, next
) {
350 send_control_event(vser
, port
->id
, VIRTIO_CONSOLE_PORT_ADD
, 1);
355 port
= find_port_by_id(vser
, virtio_ldl_p(vdev
, &gcpkt
->id
));
357 error_report("virtio-serial-bus: Unexpected port id %u for device %s",
358 virtio_ldl_p(vdev
, &gcpkt
->id
), vser
->bus
.qbus
.name
);
362 trace_virtio_serial_handle_control_message_port(port
->id
);
364 vsc
= VIRTIO_SERIAL_PORT_GET_CLASS(port
);
367 case VIRTIO_CONSOLE_PORT_READY
:
369 error_report("virtio-serial-bus: Guest failure in adding port %u for device %s",
370 port
->id
, vser
->bus
.qbus
.name
);
374 * Now that we know the guest asked for the port name, we're
375 * sure the guest has initialised whatever state is necessary
376 * for this port. Now's a good time to let the guest know if
377 * this port is a console port so that the guest can hook it
380 if (vsc
->is_console
) {
381 send_control_event(vser
, port
->id
, VIRTIO_CONSOLE_CONSOLE_PORT
, 1);
385 virtio_stl_p(vdev
, &cpkt
.id
, port
->id
);
386 virtio_stw_p(vdev
, &cpkt
.event
, VIRTIO_CONSOLE_PORT_NAME
);
387 virtio_stw_p(vdev
, &cpkt
.value
, 1);
389 buffer_len
= sizeof(cpkt
) + strlen(port
->name
) + 1;
390 buffer
= g_malloc(buffer_len
);
392 memcpy(buffer
, &cpkt
, sizeof(cpkt
));
393 memcpy(buffer
+ sizeof(cpkt
), port
->name
, strlen(port
->name
));
394 buffer
[buffer_len
- 1] = 0;
396 send_control_msg(vser
, buffer
, buffer_len
);
400 if (port
->host_connected
) {
401 send_control_event(vser
, port
->id
, VIRTIO_CONSOLE_PORT_OPEN
, 1);
405 * When the guest has asked us for this information it means
406 * the guest is all setup and has its virtqueues
407 * initialised. If some app is interested in knowing about
408 * this event, let it know.
410 if (vsc
->guest_ready
) {
411 vsc
->guest_ready(port
);
415 case VIRTIO_CONSOLE_PORT_OPEN
:
416 port
->guest_connected
= cpkt
.value
;
417 if (vsc
->set_guest_connected
) {
418 /* Send the guest opened notification if an app is interested */
419 vsc
->set_guest_connected(port
, cpkt
.value
);
425 static void control_in(VirtIODevice
*vdev
, VirtQueue
*vq
)
429 static void control_out(VirtIODevice
*vdev
, VirtQueue
*vq
)
431 VirtQueueElement
*elem
;
436 vser
= VIRTIO_SERIAL(vdev
);
443 elem
= virtqueue_pop(vq
, sizeof(VirtQueueElement
));
448 cur_len
= iov_size(elem
->out_sg
, elem
->out_num
);
450 * Allocate a new buf only if we didn't have one previously or
451 * if the size of the buf differs
456 buf
= g_malloc(cur_len
);
459 iov_to_buf(elem
->out_sg
, elem
->out_num
, 0, buf
, cur_len
);
461 handle_control_message(vser
, buf
, cur_len
);
462 virtqueue_push(vq
, elem
, 0);
466 virtio_notify(vdev
, vq
);
469 /* Guest wrote something to some port. */
470 static void handle_output(VirtIODevice
*vdev
, VirtQueue
*vq
)
473 VirtIOSerialPort
*port
;
475 vser
= VIRTIO_SERIAL(vdev
);
476 port
= find_port_by_vq(vser
, vq
);
478 if (!port
|| !port
->host_connected
) {
479 discard_vq_data(vq
, vdev
);
483 if (!port
->throttled
) {
484 do_flush_queued_data(port
, vq
, vdev
);
489 static void handle_input(VirtIODevice
*vdev
, VirtQueue
*vq
)
492 * Users of virtio-serial would like to know when guest becomes
493 * writable again -- i.e. if a vq had stuff queued up and the
494 * guest wasn't reading at all, the host would not be able to
495 * write to the vq anymore. Once the guest reads off something,
496 * we can start queueing things up again. However, this call is
497 * made for each buffer addition by the guest -- even though free
498 * buffers existed prior to the current buffer addition. This is
499 * done so as not to maintain previous state, which will need
500 * additional live-migration-related changes.
503 VirtIOSerialPort
*port
;
504 VirtIOSerialPortClass
*vsc
;
506 vser
= VIRTIO_SERIAL(vdev
);
507 port
= find_port_by_vq(vser
, vq
);
512 vsc
= VIRTIO_SERIAL_PORT_GET_CLASS(port
);
515 * If guest_connected is false, this call is being made by the
516 * early-boot queueing up of descriptors, which is just noise for
517 * the host apps -- don't disturb them in that case.
519 if (port
->guest_connected
&& port
->host_connected
&& vsc
->guest_writable
) {
520 vsc
->guest_writable(port
);
524 static uint64_t get_features(VirtIODevice
*vdev
, uint64_t features
,
529 vser
= VIRTIO_SERIAL(vdev
);
531 if (vser
->bus
.max_nr_ports
> 1) {
532 virtio_add_feature(&features
, VIRTIO_CONSOLE_F_MULTIPORT
);
537 /* Guest requested config info */
538 static void get_config(VirtIODevice
*vdev
, uint8_t *config_data
)
540 VirtIOSerial
*vser
= VIRTIO_SERIAL(vdev
);
541 struct virtio_console_config
*config
=
542 (struct virtio_console_config
*)config_data
;
546 config
->max_nr_ports
= virtio_tswap32(vdev
,
547 vser
->serial
.max_virtserial_ports
);
550 static void guest_reset(VirtIOSerial
*vser
)
552 VirtIOSerialPort
*port
;
553 VirtIOSerialPortClass
*vsc
;
555 QTAILQ_FOREACH(port
, &vser
->ports
, next
) {
556 vsc
= VIRTIO_SERIAL_PORT_GET_CLASS(port
);
557 if (port
->guest_connected
) {
558 port
->guest_connected
= false;
559 if (vsc
->set_guest_connected
) {
560 vsc
->set_guest_connected(port
, false);
566 static void set_status(VirtIODevice
*vdev
, uint8_t status
)
569 VirtIOSerialPort
*port
;
571 vser
= VIRTIO_SERIAL(vdev
);
572 port
= find_port_by_id(vser
, 0);
574 if (port
&& !use_multiport(port
->vser
)
575 && (status
& VIRTIO_CONFIG_S_DRIVER_OK
)) {
577 * Non-multiport guests won't be able to tell us guest
578 * open/close status. Such guests can only have a port at id
579 * 0, so set guest_connected for such ports as soon as guest
582 port
->guest_connected
= true;
584 if (!(status
& VIRTIO_CONFIG_S_DRIVER_OK
)) {
589 static void vser_reset(VirtIODevice
*vdev
)
593 vser
= VIRTIO_SERIAL(vdev
);
597 static void virtio_serial_save_device(VirtIODevice
*vdev
, QEMUFile
*f
)
599 VirtIOSerial
*s
= VIRTIO_SERIAL(vdev
);
600 VirtIOSerialPort
*port
;
601 uint32_t nr_active_ports
;
602 unsigned int i
, max_nr_ports
;
603 struct virtio_console_config config
;
605 /* The config space (ignored on the far end in current versions) */
606 get_config(vdev
, (uint8_t *)&config
);
607 qemu_put_be16s(f
, &config
.cols
);
608 qemu_put_be16s(f
, &config
.rows
);
609 qemu_put_be32s(f
, &config
.max_nr_ports
);
612 max_nr_ports
= s
->serial
.max_virtserial_ports
;
613 for (i
= 0; i
< (max_nr_ports
+ 31) / 32; i
++) {
614 qemu_put_be32s(f
, &s
->ports_map
[i
]);
620 QTAILQ_FOREACH(port
, &s
->ports
, next
) {
624 qemu_put_be32s(f
, &nr_active_ports
);
627 * Items in struct VirtIOSerialPort.
629 QTAILQ_FOREACH(port
, &s
->ports
, next
) {
630 uint32_t elem_popped
;
632 qemu_put_be32s(f
, &port
->id
);
633 qemu_put_byte(f
, port
->guest_connected
);
634 qemu_put_byte(f
, port
->host_connected
);
640 qemu_put_be32s(f
, &elem_popped
);
642 qemu_put_be32s(f
, &port
->iov_idx
);
643 qemu_put_be64s(f
, &port
->iov_offset
);
644 qemu_put_virtqueue_element(f
, port
->elem
);
649 static void virtio_serial_post_load_timer_cb(void *opaque
)
652 VirtIOSerial
*s
= VIRTIO_SERIAL(opaque
);
653 VirtIOSerialPort
*port
;
654 uint8_t host_connected
;
655 VirtIOSerialPortClass
*vsc
;
660 for (i
= 0 ; i
< s
->post_load
->nr_active_ports
; ++i
) {
661 port
= s
->post_load
->connected
[i
].port
;
662 host_connected
= s
->post_load
->connected
[i
].host_connected
;
663 if (host_connected
!= port
->host_connected
) {
665 * We have to let the guest know of the host connection
668 send_control_event(s
, port
->id
, VIRTIO_CONSOLE_PORT_OPEN
,
669 port
->host_connected
);
671 vsc
= VIRTIO_SERIAL_PORT_GET_CLASS(port
);
672 if (vsc
->set_guest_connected
) {
673 vsc
->set_guest_connected(port
, port
->guest_connected
);
676 g_free(s
->post_load
->connected
);
677 timer_free(s
->post_load
->timer
);
678 g_free(s
->post_load
);
682 static int fetch_active_ports_list(QEMUFile
*f
,
683 VirtIOSerial
*s
, uint32_t nr_active_ports
)
687 s
->post_load
= g_malloc0(sizeof(*s
->post_load
));
688 s
->post_load
->nr_active_ports
= nr_active_ports
;
689 s
->post_load
->connected
=
690 g_malloc0(sizeof(*s
->post_load
->connected
) * nr_active_ports
);
692 s
->post_load
->timer
= timer_new_ns(QEMU_CLOCK_VIRTUAL
,
693 virtio_serial_post_load_timer_cb
,
696 /* Items in struct VirtIOSerialPort */
697 for (i
= 0; i
< nr_active_ports
; i
++) {
698 VirtIOSerialPort
*port
;
699 uint32_t elem_popped
;
702 id
= qemu_get_be32(f
);
703 port
= find_port_by_id(s
, id
);
708 port
->guest_connected
= qemu_get_byte(f
);
709 s
->post_load
->connected
[i
].port
= port
;
710 s
->post_load
->connected
[i
].host_connected
= qemu_get_byte(f
);
712 qemu_get_be32s(f
, &elem_popped
);
714 qemu_get_be32s(f
, &port
->iov_idx
);
715 qemu_get_be64s(f
, &port
->iov_offset
);
718 qemu_get_virtqueue_element(f
, sizeof(VirtQueueElement
));
721 * Port was throttled on source machine. Let's
722 * unthrottle it here so data starts flowing again.
724 virtio_serial_throttle_port(port
, false);
727 timer_mod(s
->post_load
->timer
, 1);
731 static int virtio_serial_load(QEMUFile
*f
, void *opaque
, size_t size
)
733 /* The virtio device */
734 return virtio_load(VIRTIO_DEVICE(opaque
), f
, 3);
737 static int virtio_serial_load_device(VirtIODevice
*vdev
, QEMUFile
*f
,
740 VirtIOSerial
*s
= VIRTIO_SERIAL(vdev
);
741 uint32_t max_nr_ports
, nr_active_ports
, ports_map
;
747 qemu_get_be16s(f
, (uint16_t *) &tmp
);
748 qemu_get_be16s(f
, (uint16_t *) &tmp
);
749 qemu_get_be32s(f
, &tmp
);
751 max_nr_ports
= s
->serial
.max_virtserial_ports
;
752 for (i
= 0; i
< (max_nr_ports
+ 31) / 32; i
++) {
753 qemu_get_be32s(f
, &ports_map
);
755 if (ports_map
!= s
->ports_map
[i
]) {
757 * Ports active on source and destination don't
758 * match. Fail migration.
764 qemu_get_be32s(f
, &nr_active_ports
);
766 if (nr_active_ports
) {
767 ret
= fetch_active_ports_list(f
, s
, nr_active_ports
);
775 static void virtser_bus_dev_print(Monitor
*mon
, DeviceState
*qdev
, int indent
);
777 static Property virtser_props
[] = {
778 DEFINE_PROP_UINT32("nr", VirtIOSerialPort
, id
, VIRTIO_CONSOLE_BAD_ID
),
779 DEFINE_PROP_STRING("name", VirtIOSerialPort
, name
),
780 DEFINE_PROP_END_OF_LIST()
783 #define TYPE_VIRTIO_SERIAL_BUS "virtio-serial-bus"
784 #define VIRTIO_SERIAL_BUS(obj) \
785 OBJECT_CHECK(VirtIOSerialBus, (obj), TYPE_VIRTIO_SERIAL_BUS)
787 static void virtser_bus_class_init(ObjectClass
*klass
, void *data
)
789 BusClass
*k
= BUS_CLASS(klass
);
790 k
->print_dev
= virtser_bus_dev_print
;
793 static const TypeInfo virtser_bus_info
= {
794 .name
= TYPE_VIRTIO_SERIAL_BUS
,
796 .instance_size
= sizeof(VirtIOSerialBus
),
797 .class_init
= virtser_bus_class_init
,
800 static void virtser_bus_dev_print(Monitor
*mon
, DeviceState
*qdev
, int indent
)
802 VirtIOSerialPort
*port
= VIRTIO_SERIAL_PORT(qdev
);
804 monitor_printf(mon
, "%*sport %d, guest %s, host %s, throttle %s\n",
805 indent
, "", port
->id
,
806 port
->guest_connected
? "on" : "off",
807 port
->host_connected
? "on" : "off",
808 port
->throttled
? "on" : "off");
811 /* This function is only used if a port id is not provided by the user */
812 static uint32_t find_free_port_id(VirtIOSerial
*vser
)
814 unsigned int i
, max_nr_ports
;
816 max_nr_ports
= vser
->serial
.max_virtserial_ports
;
817 for (i
= 0; i
< (max_nr_ports
+ 31) / 32; i
++) {
818 uint32_t map
, zeroes
;
820 map
= vser
->ports_map
[i
];
821 zeroes
= ctz32(~map
);
823 return zeroes
+ i
* 32;
826 return VIRTIO_CONSOLE_BAD_ID
;
829 static void mark_port_added(VirtIOSerial
*vser
, uint32_t port_id
)
834 vser
->ports_map
[i
] |= 1U << (port_id
% 32);
837 static void add_port(VirtIOSerial
*vser
, uint32_t port_id
)
839 mark_port_added(vser
, port_id
);
840 send_control_event(vser
, port_id
, VIRTIO_CONSOLE_PORT_ADD
, 1);
843 static void remove_port(VirtIOSerial
*vser
, uint32_t port_id
)
845 VirtIOSerialPort
*port
;
848 * Don't mark port 0 removed -- we explicitly reserve it for
849 * backward compat with older guests, ensure a virtconsole device
850 * unplug retains the reservation.
856 vser
->ports_map
[i
] &= ~(1U << (port_id
% 32));
859 port
= find_port_by_id(vser
, port_id
);
861 * This function is only called from qdev's unplug callback; if we
862 * get a NULL port here, we're in trouble.
866 /* Flush out any unconsumed buffers first */
867 discard_vq_data(port
->ovq
, VIRTIO_DEVICE(port
->vser
));
869 send_control_event(vser
, port
->id
, VIRTIO_CONSOLE_PORT_REMOVE
, 1);
872 static void virtser_port_device_realize(DeviceState
*dev
, Error
**errp
)
874 VirtIOSerialPort
*port
= VIRTIO_SERIAL_PORT(dev
);
875 VirtIOSerialPortClass
*vsc
= VIRTIO_SERIAL_PORT_GET_CLASS(port
);
876 VirtIOSerialBus
*bus
= VIRTIO_SERIAL_BUS(qdev_get_parent_bus(dev
));
881 port
->vser
= bus
->vser
;
882 port
->bh
= qemu_bh_new(flush_queued_data_bh
, port
);
884 assert(vsc
->have_data
);
887 * Is the first console port we're seeing? If so, put it up at
888 * location 0. This is done for backward compatibility (old
891 plugging_port0
= vsc
->is_console
&& !find_port_by_id(port
->vser
, 0);
893 if (find_port_by_id(port
->vser
, port
->id
)) {
894 error_setg(errp
, "virtio-serial-bus: A port already exists at id %u",
899 if (port
->name
!= NULL
&& find_port_by_name(port
->name
)) {
900 error_setg(errp
, "virtio-serial-bus: A port already exists by name %s",
905 if (port
->id
== VIRTIO_CONSOLE_BAD_ID
) {
906 if (plugging_port0
) {
909 port
->id
= find_free_port_id(port
->vser
);
910 if (port
->id
== VIRTIO_CONSOLE_BAD_ID
) {
911 error_setg(errp
, "virtio-serial-bus: Maximum port limit for "
912 "this device reached");
918 max_nr_ports
= port
->vser
->serial
.max_virtserial_ports
;
919 if (port
->id
>= max_nr_ports
) {
920 error_setg(errp
, "virtio-serial-bus: Out-of-range port id specified, "
921 "max. allowed: %u", max_nr_ports
- 1);
925 vsc
->realize(dev
, &err
);
927 error_propagate(errp
, err
);
934 static void virtser_port_device_plug(HotplugHandler
*hotplug_dev
,
935 DeviceState
*dev
, Error
**errp
)
937 VirtIOSerialPort
*port
= VIRTIO_SERIAL_PORT(dev
);
939 QTAILQ_INSERT_TAIL(&port
->vser
->ports
, port
, next
);
940 port
->ivq
= port
->vser
->ivqs
[port
->id
];
941 port
->ovq
= port
->vser
->ovqs
[port
->id
];
943 add_port(port
->vser
, port
->id
);
945 /* Send an update to the guest about this new port added */
946 virtio_notify_config(VIRTIO_DEVICE(hotplug_dev
));
949 static void virtser_port_device_unrealize(DeviceState
*dev
, Error
**errp
)
951 VirtIOSerialPort
*port
= VIRTIO_SERIAL_PORT(dev
);
952 VirtIOSerialPortClass
*vsc
= VIRTIO_SERIAL_PORT_GET_CLASS(dev
);
953 VirtIOSerial
*vser
= port
->vser
;
955 qemu_bh_delete(port
->bh
);
956 remove_port(port
->vser
, port
->id
);
958 QTAILQ_REMOVE(&vser
->ports
, port
, next
);
960 if (vsc
->unrealize
) {
961 vsc
->unrealize(dev
, errp
);
965 static void virtio_serial_device_realize(DeviceState
*dev
, Error
**errp
)
967 VirtIODevice
*vdev
= VIRTIO_DEVICE(dev
);
968 VirtIOSerial
*vser
= VIRTIO_SERIAL(dev
);
969 uint32_t i
, max_supported_ports
;
971 if (!vser
->serial
.max_virtserial_ports
) {
972 error_setg(errp
, "Maximum number of serial ports not specified");
976 /* Each port takes 2 queues, and one pair is for the control queue */
977 max_supported_ports
= VIRTIO_QUEUE_MAX
/ 2 - 1;
979 if (vser
->serial
.max_virtserial_ports
> max_supported_ports
) {
980 error_setg(errp
, "maximum ports supported: %u", max_supported_ports
);
984 /* We don't support emergency write, skip it for now. */
985 /* TODO: cleaner fix, depending on host features. */
986 virtio_init(vdev
, "virtio-serial", VIRTIO_ID_CONSOLE
,
987 offsetof(struct virtio_console_config
, emerg_wr
));
989 /* Spawn a new virtio-serial bus on which the ports will ride as devices */
990 qbus_create_inplace(&vser
->bus
, sizeof(vser
->bus
), TYPE_VIRTIO_SERIAL_BUS
,
991 dev
, vdev
->bus_name
);
992 qbus_set_hotplug_handler(BUS(&vser
->bus
), DEVICE(vser
), errp
);
993 vser
->bus
.vser
= vser
;
994 QTAILQ_INIT(&vser
->ports
);
996 vser
->bus
.max_nr_ports
= vser
->serial
.max_virtserial_ports
;
997 vser
->ivqs
= g_malloc(vser
->serial
.max_virtserial_ports
998 * sizeof(VirtQueue
*));
999 vser
->ovqs
= g_malloc(vser
->serial
.max_virtserial_ports
1000 * sizeof(VirtQueue
*));
1002 /* Add a queue for host to guest transfers for port 0 (backward compat) */
1003 vser
->ivqs
[0] = virtio_add_queue(vdev
, 128, handle_input
);
1004 /* Add a queue for guest to host transfers for port 0 (backward compat) */
1005 vser
->ovqs
[0] = virtio_add_queue(vdev
, 128, handle_output
);
1007 /* TODO: host to guest notifications can get dropped
1008 * if the queue fills up. Implement queueing in host,
1009 * this might also make it possible to reduce the control
1010 * queue size: as guest preposts buffers there,
1011 * this will save 4Kbyte of guest memory per entry. */
1013 /* control queue: host to guest */
1014 vser
->c_ivq
= virtio_add_queue(vdev
, 32, control_in
);
1015 /* control queue: guest to host */
1016 vser
->c_ovq
= virtio_add_queue(vdev
, 32, control_out
);
1018 for (i
= 1; i
< vser
->bus
.max_nr_ports
; i
++) {
1019 /* Add a per-port queue for host to guest transfers */
1020 vser
->ivqs
[i
] = virtio_add_queue(vdev
, 128, handle_input
);
1021 /* Add a per-per queue for guest to host transfers */
1022 vser
->ovqs
[i
] = virtio_add_queue(vdev
, 128, handle_output
);
1025 vser
->ports_map
= g_malloc0(((vser
->serial
.max_virtserial_ports
+ 31) / 32)
1026 * sizeof(vser
->ports_map
[0]));
1028 * Reserve location 0 for a console port for backward compat
1029 * (old kernel, new qemu)
1031 mark_port_added(vser
, 0);
1033 vser
->post_load
= NULL
;
1035 QLIST_INSERT_HEAD(&vserdevices
.devices
, vser
, next
);
1038 static void virtio_serial_port_class_init(ObjectClass
*klass
, void *data
)
1040 DeviceClass
*k
= DEVICE_CLASS(klass
);
1042 set_bit(DEVICE_CATEGORY_INPUT
, k
->categories
);
1043 k
->bus_type
= TYPE_VIRTIO_SERIAL_BUS
;
1044 k
->realize
= virtser_port_device_realize
;
1045 k
->unrealize
= virtser_port_device_unrealize
;
1046 k
->props
= virtser_props
;
1049 static const TypeInfo virtio_serial_port_type_info
= {
1050 .name
= TYPE_VIRTIO_SERIAL_PORT
,
1051 .parent
= TYPE_DEVICE
,
1052 .instance_size
= sizeof(VirtIOSerialPort
),
1054 .class_size
= sizeof(VirtIOSerialPortClass
),
1055 .class_init
= virtio_serial_port_class_init
,
1058 static void virtio_serial_device_unrealize(DeviceState
*dev
, Error
**errp
)
1060 VirtIODevice
*vdev
= VIRTIO_DEVICE(dev
);
1061 VirtIOSerial
*vser
= VIRTIO_SERIAL(dev
);
1063 QLIST_REMOVE(vser
, next
);
1067 g_free(vser
->ports_map
);
1068 if (vser
->post_load
) {
1069 g_free(vser
->post_load
->connected
);
1070 timer_del(vser
->post_load
->timer
);
1071 timer_free(vser
->post_load
->timer
);
1072 g_free(vser
->post_load
);
1074 virtio_cleanup(vdev
);
1077 /* Note: 'console' is used for backwards compatibility */
1078 VMSTATE_VIRTIO_DEVICE(console
, 3, virtio_serial_load
, virtio_vmstate_save
);
1080 static Property virtio_serial_properties
[] = {
1081 DEFINE_PROP_UINT32("max_ports", VirtIOSerial
, serial
.max_virtserial_ports
,
1083 DEFINE_PROP_END_OF_LIST(),
1086 static void virtio_serial_class_init(ObjectClass
*klass
, void *data
)
1088 DeviceClass
*dc
= DEVICE_CLASS(klass
);
1089 VirtioDeviceClass
*vdc
= VIRTIO_DEVICE_CLASS(klass
);
1090 HotplugHandlerClass
*hc
= HOTPLUG_HANDLER_CLASS(klass
);
1092 QLIST_INIT(&vserdevices
.devices
);
1094 dc
->props
= virtio_serial_properties
;
1095 dc
->vmsd
= &vmstate_virtio_console
;
1096 set_bit(DEVICE_CATEGORY_INPUT
, dc
->categories
);
1097 vdc
->realize
= virtio_serial_device_realize
;
1098 vdc
->unrealize
= virtio_serial_device_unrealize
;
1099 vdc
->get_features
= get_features
;
1100 vdc
->get_config
= get_config
;
1101 vdc
->set_status
= set_status
;
1102 vdc
->reset
= vser_reset
;
1103 vdc
->save
= virtio_serial_save_device
;
1104 vdc
->load
= virtio_serial_load_device
;
1105 hc
->plug
= virtser_port_device_plug
;
1106 hc
->unplug
= qdev_simple_device_unplug_cb
;
1109 static const TypeInfo virtio_device_info
= {
1110 .name
= TYPE_VIRTIO_SERIAL
,
1111 .parent
= TYPE_VIRTIO_DEVICE
,
1112 .instance_size
= sizeof(VirtIOSerial
),
1113 .class_init
= virtio_serial_class_init
,
1114 .interfaces
= (InterfaceInfo
[]) {
1115 { TYPE_HOTPLUG_HANDLER
},
1120 static void virtio_serial_register_types(void)
1122 type_register_static(&virtser_bus_info
);
1123 type_register_static(&virtio_serial_port_type_info
);
1124 type_register_static(&virtio_device_info
);
1127 type_init(virtio_serial_register_types
)