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 "standard-headers/linux/virtio_ids.h"
23 #include "monitor/monitor.h"
24 #include "qemu/queue.h"
25 #include "hw/sysbus.h"
27 #include "hw/virtio/virtio-serial.h"
28 #include "hw/virtio/virtio-access.h"
30 static struct VirtIOSerialDevices
{
31 QLIST_HEAD(, VirtIOSerial
) devices
;
34 static VirtIOSerialPort
*find_port_by_id(VirtIOSerial
*vser
, uint32_t id
)
36 VirtIOSerialPort
*port
;
38 if (id
== VIRTIO_CONSOLE_BAD_ID
) {
42 QTAILQ_FOREACH(port
, &vser
->ports
, next
) {
49 static VirtIOSerialPort
*find_port_by_vq(VirtIOSerial
*vser
, VirtQueue
*vq
)
51 VirtIOSerialPort
*port
;
53 QTAILQ_FOREACH(port
, &vser
->ports
, next
) {
54 if (port
->ivq
== vq
|| port
->ovq
== vq
)
60 static VirtIOSerialPort
*find_port_by_name(char *name
)
64 QLIST_FOREACH(vser
, &vserdevices
.devices
, next
) {
65 VirtIOSerialPort
*port
;
67 QTAILQ_FOREACH(port
, &vser
->ports
, next
) {
68 if (!strcmp(port
->name
, name
)) {
76 static bool use_multiport(VirtIOSerial
*vser
)
78 VirtIODevice
*vdev
= VIRTIO_DEVICE(vser
);
79 return virtio_has_feature(vdev
, VIRTIO_CONSOLE_F_MULTIPORT
);
82 static size_t write_to_port(VirtIOSerialPort
*port
,
83 const uint8_t *buf
, size_t size
)
85 VirtQueueElement elem
;
90 if (!virtio_queue_ready(vq
)) {
95 while (offset
< size
) {
98 if (!virtqueue_pop(vq
, &elem
)) {
102 len
= iov_from_buf(elem
.in_sg
, elem
.in_num
, 0,
103 buf
+ offset
, size
- offset
);
106 virtqueue_push(vq
, &elem
, len
);
109 virtio_notify(VIRTIO_DEVICE(port
->vser
), vq
);
113 static void discard_vq_data(VirtQueue
*vq
, VirtIODevice
*vdev
)
115 VirtQueueElement elem
;
117 if (!virtio_queue_ready(vq
)) {
120 while (virtqueue_pop(vq
, &elem
)) {
121 virtqueue_push(vq
, &elem
, 0);
123 virtio_notify(vdev
, vq
);
126 static void do_flush_queued_data(VirtIOSerialPort
*port
, VirtQueue
*vq
,
129 VirtIOSerialPortClass
*vsc
;
132 assert(virtio_queue_ready(vq
));
134 vsc
= VIRTIO_SERIAL_PORT_GET_CLASS(port
);
136 while (!port
->throttled
) {
139 /* Pop an elem only if we haven't left off a previous one mid-way */
140 if (!port
->elem
.out_num
) {
141 if (!virtqueue_pop(vq
, &port
->elem
)) {
145 port
->iov_offset
= 0;
148 for (i
= port
->iov_idx
; i
< port
->elem
.out_num
; i
++) {
152 buf_size
= port
->elem
.out_sg
[i
].iov_len
- port
->iov_offset
;
153 ret
= vsc
->have_data(port
,
154 port
->elem
.out_sg
[i
].iov_base
157 if (port
->throttled
) {
160 port
->iov_offset
+= ret
;
164 port
->iov_offset
= 0;
166 if (port
->throttled
) {
169 virtqueue_push(vq
, &port
->elem
, 0);
170 port
->elem
.out_num
= 0;
172 virtio_notify(vdev
, vq
);
175 static void flush_queued_data(VirtIOSerialPort
*port
)
179 if (!virtio_queue_ready(port
->ovq
)) {
182 do_flush_queued_data(port
, port
->ovq
, VIRTIO_DEVICE(port
->vser
));
185 static size_t send_control_msg(VirtIOSerial
*vser
, void *buf
, size_t len
)
187 VirtQueueElement elem
;
191 if (!virtio_queue_ready(vq
)) {
194 if (!virtqueue_pop(vq
, &elem
)) {
198 memcpy(elem
.in_sg
[0].iov_base
, buf
, len
);
200 virtqueue_push(vq
, &elem
, len
);
201 virtio_notify(VIRTIO_DEVICE(vser
), vq
);
205 static size_t send_control_event(VirtIOSerial
*vser
, uint32_t port_id
,
206 uint16_t event
, uint16_t value
)
208 VirtIODevice
*vdev
= VIRTIO_DEVICE(vser
);
209 struct virtio_console_control cpkt
;
211 virtio_stl_p(vdev
, &cpkt
.id
, port_id
);
212 virtio_stw_p(vdev
, &cpkt
.event
, event
);
213 virtio_stw_p(vdev
, &cpkt
.value
, value
);
215 trace_virtio_serial_send_control_event(port_id
, event
, value
);
216 return send_control_msg(vser
, &cpkt
, sizeof(cpkt
));
219 /* Functions for use inside qemu to open and read from/write to ports */
220 int virtio_serial_open(VirtIOSerialPort
*port
)
222 /* Don't allow opening an already-open port */
223 if (port
->host_connected
) {
226 /* Send port open notification to the guest */
227 port
->host_connected
= true;
228 send_control_event(port
->vser
, port
->id
, VIRTIO_CONSOLE_PORT_OPEN
, 1);
233 int virtio_serial_close(VirtIOSerialPort
*port
)
235 port
->host_connected
= false;
237 * If there's any data the guest sent which the app didn't
238 * consume, reset the throttling flag and discard the data.
240 port
->throttled
= false;
241 discard_vq_data(port
->ovq
, VIRTIO_DEVICE(port
->vser
));
243 send_control_event(port
->vser
, port
->id
, VIRTIO_CONSOLE_PORT_OPEN
, 0);
248 /* Individual ports/apps call this function to write to the guest. */
249 ssize_t
virtio_serial_write(VirtIOSerialPort
*port
, const uint8_t *buf
,
252 if (!port
|| !port
->host_connected
|| !port
->guest_connected
) {
255 return write_to_port(port
, buf
, size
);
259 * Readiness of the guest to accept data on a port.
260 * Returns max. data the guest can receive
262 size_t virtio_serial_guest_ready(VirtIOSerialPort
*port
)
264 VirtIODevice
*vdev
= VIRTIO_DEVICE(port
->vser
);
265 VirtQueue
*vq
= port
->ivq
;
268 if (!virtio_queue_ready(vq
) ||
269 !(vdev
->status
& VIRTIO_CONFIG_S_DRIVER_OK
) ||
270 virtio_queue_empty(vq
)) {
273 if (use_multiport(port
->vser
) && !port
->guest_connected
) {
276 virtqueue_get_avail_bytes(vq
, &bytes
, NULL
, 4096, 0);
280 static void flush_queued_data_bh(void *opaque
)
282 VirtIOSerialPort
*port
= opaque
;
284 flush_queued_data(port
);
287 void virtio_serial_throttle_port(VirtIOSerialPort
*port
, bool throttle
)
293 trace_virtio_serial_throttle_port(port
->id
, throttle
);
294 port
->throttled
= throttle
;
298 qemu_bh_schedule(port
->bh
);
301 /* Guest wants to notify us of some event */
302 static void handle_control_message(VirtIOSerial
*vser
, void *buf
, size_t len
)
304 VirtIODevice
*vdev
= VIRTIO_DEVICE(vser
);
305 struct VirtIOSerialPort
*port
;
306 VirtIOSerialPortClass
*vsc
;
307 struct virtio_console_control cpkt
, *gcpkt
;
313 if (len
< sizeof(cpkt
)) {
314 /* The guest sent an invalid control packet */
318 cpkt
.event
= virtio_lduw_p(vdev
, &gcpkt
->event
);
319 cpkt
.value
= virtio_lduw_p(vdev
, &gcpkt
->value
);
321 trace_virtio_serial_handle_control_message(cpkt
.event
, cpkt
.value
);
323 if (cpkt
.event
== VIRTIO_CONSOLE_DEVICE_READY
) {
325 error_report("virtio-serial-bus: Guest failure in adding device %s",
326 vser
->bus
.qbus
.name
);
330 * The device is up, we can now tell the device about all the
331 * ports we have here.
333 QTAILQ_FOREACH(port
, &vser
->ports
, next
) {
334 send_control_event(vser
, port
->id
, VIRTIO_CONSOLE_PORT_ADD
, 1);
339 port
= find_port_by_id(vser
, virtio_ldl_p(vdev
, &gcpkt
->id
));
341 error_report("virtio-serial-bus: Unexpected port id %u for device %s",
342 virtio_ldl_p(vdev
, &gcpkt
->id
), vser
->bus
.qbus
.name
);
346 trace_virtio_serial_handle_control_message_port(port
->id
);
348 vsc
= VIRTIO_SERIAL_PORT_GET_CLASS(port
);
351 case VIRTIO_CONSOLE_PORT_READY
:
353 error_report("virtio-serial-bus: Guest failure in adding port %u for device %s",
354 port
->id
, vser
->bus
.qbus
.name
);
358 * Now that we know the guest asked for the port name, we're
359 * sure the guest has initialised whatever state is necessary
360 * for this port. Now's a good time to let the guest know if
361 * this port is a console port so that the guest can hook it
364 if (vsc
->is_console
) {
365 send_control_event(vser
, port
->id
, VIRTIO_CONSOLE_CONSOLE_PORT
, 1);
369 virtio_stl_p(vdev
, &cpkt
.id
, port
->id
);
370 virtio_stw_p(vdev
, &cpkt
.event
, VIRTIO_CONSOLE_PORT_NAME
);
371 virtio_stw_p(vdev
, &cpkt
.value
, 1);
373 buffer_len
= sizeof(cpkt
) + strlen(port
->name
) + 1;
374 buffer
= g_malloc(buffer_len
);
376 memcpy(buffer
, &cpkt
, sizeof(cpkt
));
377 memcpy(buffer
+ sizeof(cpkt
), port
->name
, strlen(port
->name
));
378 buffer
[buffer_len
- 1] = 0;
380 send_control_msg(vser
, buffer
, buffer_len
);
384 if (port
->host_connected
) {
385 send_control_event(vser
, port
->id
, VIRTIO_CONSOLE_PORT_OPEN
, 1);
389 * When the guest has asked us for this information it means
390 * the guest is all setup and has its virtqueues
391 * initialised. If some app is interested in knowing about
392 * this event, let it know.
394 if (vsc
->guest_ready
) {
395 vsc
->guest_ready(port
);
399 case VIRTIO_CONSOLE_PORT_OPEN
:
400 port
->guest_connected
= cpkt
.value
;
401 if (vsc
->set_guest_connected
) {
402 /* Send the guest opened notification if an app is interested */
403 vsc
->set_guest_connected(port
, cpkt
.value
);
409 static void control_in(VirtIODevice
*vdev
, VirtQueue
*vq
)
413 static void control_out(VirtIODevice
*vdev
, VirtQueue
*vq
)
415 VirtQueueElement elem
;
420 vser
= VIRTIO_SERIAL(vdev
);
424 while (virtqueue_pop(vq
, &elem
)) {
427 cur_len
= iov_size(elem
.out_sg
, elem
.out_num
);
429 * Allocate a new buf only if we didn't have one previously or
430 * if the size of the buf differs
435 buf
= g_malloc(cur_len
);
438 iov_to_buf(elem
.out_sg
, elem
.out_num
, 0, buf
, cur_len
);
440 handle_control_message(vser
, buf
, cur_len
);
441 virtqueue_push(vq
, &elem
, 0);
444 virtio_notify(vdev
, vq
);
447 /* Guest wrote something to some port. */
448 static void handle_output(VirtIODevice
*vdev
, VirtQueue
*vq
)
451 VirtIOSerialPort
*port
;
453 vser
= VIRTIO_SERIAL(vdev
);
454 port
= find_port_by_vq(vser
, vq
);
456 if (!port
|| !port
->host_connected
) {
457 discard_vq_data(vq
, vdev
);
461 if (!port
->throttled
) {
462 do_flush_queued_data(port
, vq
, vdev
);
467 static void handle_input(VirtIODevice
*vdev
, VirtQueue
*vq
)
471 static uint32_t get_features(VirtIODevice
*vdev
, uint32_t features
)
475 vser
= VIRTIO_SERIAL(vdev
);
477 if (vser
->bus
.max_nr_ports
> 1) {
478 virtio_add_feature(&features
, VIRTIO_CONSOLE_F_MULTIPORT
);
483 /* Guest requested config info */
484 static void get_config(VirtIODevice
*vdev
, uint8_t *config_data
)
486 VirtIOSerial
*vser
= VIRTIO_SERIAL(vdev
);
487 struct virtio_console_config
*config
=
488 (struct virtio_console_config
*)config_data
;
492 config
->max_nr_ports
= virtio_tswap32(vdev
,
493 vser
->serial
.max_virtserial_ports
);
496 static void guest_reset(VirtIOSerial
*vser
)
498 VirtIOSerialPort
*port
;
499 VirtIOSerialPortClass
*vsc
;
501 QTAILQ_FOREACH(port
, &vser
->ports
, next
) {
502 vsc
= VIRTIO_SERIAL_PORT_GET_CLASS(port
);
503 if (port
->guest_connected
) {
504 port
->guest_connected
= false;
505 if (vsc
->set_guest_connected
) {
506 vsc
->set_guest_connected(port
, false);
512 static void set_status(VirtIODevice
*vdev
, uint8_t status
)
515 VirtIOSerialPort
*port
;
517 vser
= VIRTIO_SERIAL(vdev
);
518 port
= find_port_by_id(vser
, 0);
520 if (port
&& !use_multiport(port
->vser
)
521 && (status
& VIRTIO_CONFIG_S_DRIVER_OK
)) {
523 * Non-multiport guests won't be able to tell us guest
524 * open/close status. Such guests can only have a port at id
525 * 0, so set guest_connected for such ports as soon as guest
528 port
->guest_connected
= true;
530 if (!(status
& VIRTIO_CONFIG_S_DRIVER_OK
)) {
535 static void vser_reset(VirtIODevice
*vdev
)
539 vser
= VIRTIO_SERIAL(vdev
);
543 static void virtio_serial_save(QEMUFile
*f
, void *opaque
)
545 /* The virtio device */
546 virtio_save(VIRTIO_DEVICE(opaque
), f
);
549 static void virtio_serial_save_device(VirtIODevice
*vdev
, QEMUFile
*f
)
551 VirtIOSerial
*s
= VIRTIO_SERIAL(vdev
);
552 VirtIOSerialPort
*port
;
553 uint32_t nr_active_ports
;
554 unsigned int i
, max_nr_ports
;
555 struct virtio_console_config config
;
557 /* The config space (ignored on the far end in current versions) */
558 get_config(vdev
, (uint8_t *)&config
);
559 qemu_put_be16s(f
, &config
.cols
);
560 qemu_put_be16s(f
, &config
.rows
);
561 qemu_put_be32s(f
, &config
.max_nr_ports
);
564 max_nr_ports
= s
->serial
.max_virtserial_ports
;
565 for (i
= 0; i
< (max_nr_ports
+ 31) / 32; i
++) {
566 qemu_put_be32s(f
, &s
->ports_map
[i
]);
572 QTAILQ_FOREACH(port
, &s
->ports
, next
) {
576 qemu_put_be32s(f
, &nr_active_ports
);
579 * Items in struct VirtIOSerialPort.
581 QTAILQ_FOREACH(port
, &s
->ports
, next
) {
582 uint32_t elem_popped
;
584 qemu_put_be32s(f
, &port
->id
);
585 qemu_put_byte(f
, port
->guest_connected
);
586 qemu_put_byte(f
, port
->host_connected
);
589 if (port
->elem
.out_num
) {
592 qemu_put_be32s(f
, &elem_popped
);
594 qemu_put_be32s(f
, &port
->iov_idx
);
595 qemu_put_be64s(f
, &port
->iov_offset
);
597 qemu_put_buffer(f
, (unsigned char *)&port
->elem
,
603 static void virtio_serial_post_load_timer_cb(void *opaque
)
606 VirtIOSerial
*s
= VIRTIO_SERIAL(opaque
);
607 VirtIOSerialPort
*port
;
608 uint8_t host_connected
;
609 VirtIOSerialPortClass
*vsc
;
614 for (i
= 0 ; i
< s
->post_load
->nr_active_ports
; ++i
) {
615 port
= s
->post_load
->connected
[i
].port
;
616 host_connected
= s
->post_load
->connected
[i
].host_connected
;
617 if (host_connected
!= port
->host_connected
) {
619 * We have to let the guest know of the host connection
622 send_control_event(s
, port
->id
, VIRTIO_CONSOLE_PORT_OPEN
,
623 port
->host_connected
);
625 vsc
= VIRTIO_SERIAL_PORT_GET_CLASS(port
);
626 if (vsc
->set_guest_connected
) {
627 vsc
->set_guest_connected(port
, port
->guest_connected
);
630 g_free(s
->post_load
->connected
);
631 timer_free(s
->post_load
->timer
);
632 g_free(s
->post_load
);
636 static int fetch_active_ports_list(QEMUFile
*f
, int version_id
,
637 VirtIOSerial
*s
, uint32_t nr_active_ports
)
641 s
->post_load
= g_malloc0(sizeof(*s
->post_load
));
642 s
->post_load
->nr_active_ports
= nr_active_ports
;
643 s
->post_load
->connected
=
644 g_malloc0(sizeof(*s
->post_load
->connected
) * nr_active_ports
);
646 s
->post_load
->timer
= timer_new_ns(QEMU_CLOCK_VIRTUAL
,
647 virtio_serial_post_load_timer_cb
,
650 /* Items in struct VirtIOSerialPort */
651 for (i
= 0; i
< nr_active_ports
; i
++) {
652 VirtIOSerialPort
*port
;
655 id
= qemu_get_be32(f
);
656 port
= find_port_by_id(s
, id
);
661 port
->guest_connected
= qemu_get_byte(f
);
662 s
->post_load
->connected
[i
].port
= port
;
663 s
->post_load
->connected
[i
].host_connected
= qemu_get_byte(f
);
665 if (version_id
> 2) {
666 uint32_t elem_popped
;
668 qemu_get_be32s(f
, &elem_popped
);
670 qemu_get_be32s(f
, &port
->iov_idx
);
671 qemu_get_be64s(f
, &port
->iov_offset
);
673 qemu_get_buffer(f
, (unsigned char *)&port
->elem
,
675 virtqueue_map_sg(port
->elem
.in_sg
, port
->elem
.in_addr
,
676 port
->elem
.in_num
, 1);
677 virtqueue_map_sg(port
->elem
.out_sg
, port
->elem
.out_addr
,
678 port
->elem
.out_num
, 1);
681 * Port was throttled on source machine. Let's
682 * unthrottle it here so data starts flowing again.
684 virtio_serial_throttle_port(port
, false);
688 timer_mod(s
->post_load
->timer
, 1);
692 static int virtio_serial_load(QEMUFile
*f
, void *opaque
, int version_id
)
694 if (version_id
> 3) {
698 /* The virtio device */
699 return virtio_load(VIRTIO_DEVICE(opaque
), f
, version_id
);
702 static int virtio_serial_load_device(VirtIODevice
*vdev
, QEMUFile
*f
,
705 VirtIOSerial
*s
= VIRTIO_SERIAL(vdev
);
706 uint32_t max_nr_ports
, nr_active_ports
, ports_map
;
711 if (version_id
< 2) {
716 qemu_get_be16s(f
, (uint16_t *) &tmp
);
717 qemu_get_be16s(f
, (uint16_t *) &tmp
);
718 qemu_get_be32s(f
, &tmp
);
720 max_nr_ports
= s
->serial
.max_virtserial_ports
;
721 for (i
= 0; i
< (max_nr_ports
+ 31) / 32; i
++) {
722 qemu_get_be32s(f
, &ports_map
);
724 if (ports_map
!= s
->ports_map
[i
]) {
726 * Ports active on source and destination don't
727 * match. Fail migration.
733 qemu_get_be32s(f
, &nr_active_ports
);
735 if (nr_active_ports
) {
736 ret
= fetch_active_ports_list(f
, version_id
, s
, nr_active_ports
);
744 static void virtser_bus_dev_print(Monitor
*mon
, DeviceState
*qdev
, int indent
);
746 static Property virtser_props
[] = {
747 DEFINE_PROP_UINT32("nr", VirtIOSerialPort
, id
, VIRTIO_CONSOLE_BAD_ID
),
748 DEFINE_PROP_STRING("name", VirtIOSerialPort
, name
),
749 DEFINE_PROP_END_OF_LIST()
752 #define TYPE_VIRTIO_SERIAL_BUS "virtio-serial-bus"
753 #define VIRTIO_SERIAL_BUS(obj) \
754 OBJECT_CHECK(VirtIOSerialBus, (obj), TYPE_VIRTIO_SERIAL_BUS)
756 static void virtser_bus_class_init(ObjectClass
*klass
, void *data
)
758 BusClass
*k
= BUS_CLASS(klass
);
759 k
->print_dev
= virtser_bus_dev_print
;
762 static const TypeInfo virtser_bus_info
= {
763 .name
= TYPE_VIRTIO_SERIAL_BUS
,
765 .instance_size
= sizeof(VirtIOSerialBus
),
766 .class_init
= virtser_bus_class_init
,
769 static void virtser_bus_dev_print(Monitor
*mon
, DeviceState
*qdev
, int indent
)
771 VirtIOSerialPort
*port
= DO_UPCAST(VirtIOSerialPort
, dev
, qdev
);
773 monitor_printf(mon
, "%*sport %d, guest %s, host %s, throttle %s\n",
774 indent
, "", port
->id
,
775 port
->guest_connected
? "on" : "off",
776 port
->host_connected
? "on" : "off",
777 port
->throttled
? "on" : "off");
780 /* This function is only used if a port id is not provided by the user */
781 static uint32_t find_free_port_id(VirtIOSerial
*vser
)
783 unsigned int i
, max_nr_ports
;
785 max_nr_ports
= vser
->serial
.max_virtserial_ports
;
786 for (i
= 0; i
< (max_nr_ports
+ 31) / 32; i
++) {
789 map
= vser
->ports_map
[i
];
792 return (bit
- 1) + i
* 32;
795 return VIRTIO_CONSOLE_BAD_ID
;
798 static void mark_port_added(VirtIOSerial
*vser
, uint32_t port_id
)
803 vser
->ports_map
[i
] |= 1U << (port_id
% 32);
806 static void add_port(VirtIOSerial
*vser
, uint32_t port_id
)
808 mark_port_added(vser
, port_id
);
809 send_control_event(vser
, port_id
, VIRTIO_CONSOLE_PORT_ADD
, 1);
812 static void remove_port(VirtIOSerial
*vser
, uint32_t port_id
)
814 VirtIOSerialPort
*port
;
817 * Don't mark port 0 removed -- we explicitly reserve it for
818 * backward compat with older guests, ensure a virtconsole device
819 * unplug retains the reservation.
825 vser
->ports_map
[i
] &= ~(1U << (port_id
% 32));
828 port
= find_port_by_id(vser
, port_id
);
830 * This function is only called from qdev's unplug callback; if we
831 * get a NULL port here, we're in trouble.
835 /* Flush out any unconsumed buffers first */
836 discard_vq_data(port
->ovq
, VIRTIO_DEVICE(port
->vser
));
838 send_control_event(vser
, port
->id
, VIRTIO_CONSOLE_PORT_REMOVE
, 1);
841 static void virtser_port_device_realize(DeviceState
*dev
, Error
**errp
)
843 VirtIOSerialPort
*port
= VIRTIO_SERIAL_PORT(dev
);
844 VirtIOSerialPortClass
*vsc
= VIRTIO_SERIAL_PORT_GET_CLASS(port
);
845 VirtIOSerialBus
*bus
= VIRTIO_SERIAL_BUS(qdev_get_parent_bus(dev
));
850 port
->vser
= bus
->vser
;
851 port
->bh
= qemu_bh_new(flush_queued_data_bh
, port
);
853 assert(vsc
->have_data
);
856 * Is the first console port we're seeing? If so, put it up at
857 * location 0. This is done for backward compatibility (old
860 plugging_port0
= vsc
->is_console
&& !find_port_by_id(port
->vser
, 0);
862 if (find_port_by_id(port
->vser
, port
->id
)) {
863 error_setg(errp
, "virtio-serial-bus: A port already exists at id %u",
868 if (port
->name
!= NULL
&& find_port_by_name(port
->name
)) {
869 error_setg(errp
, "virtio-serial-bus: A port already exists by name %s",
874 if (port
->id
== VIRTIO_CONSOLE_BAD_ID
) {
875 if (plugging_port0
) {
878 port
->id
= find_free_port_id(port
->vser
);
879 if (port
->id
== VIRTIO_CONSOLE_BAD_ID
) {
880 error_setg(errp
, "virtio-serial-bus: Maximum port limit for "
881 "this device reached");
887 max_nr_ports
= port
->vser
->serial
.max_virtserial_ports
;
888 if (port
->id
>= max_nr_ports
) {
889 error_setg(errp
, "virtio-serial-bus: Out-of-range port id specified, "
890 "max. allowed: %u", max_nr_ports
- 1);
894 vsc
->realize(dev
, &err
);
896 error_propagate(errp
, err
);
900 port
->elem
.out_num
= 0;
903 static void virtser_port_device_plug(HotplugHandler
*hotplug_dev
,
904 DeviceState
*dev
, Error
**errp
)
906 VirtIOSerialPort
*port
= VIRTIO_SERIAL_PORT(dev
);
908 QTAILQ_INSERT_TAIL(&port
->vser
->ports
, port
, next
);
909 port
->ivq
= port
->vser
->ivqs
[port
->id
];
910 port
->ovq
= port
->vser
->ovqs
[port
->id
];
912 add_port(port
->vser
, port
->id
);
914 /* Send an update to the guest about this new port added */
915 virtio_notify_config(VIRTIO_DEVICE(hotplug_dev
));
918 static void virtser_port_device_unrealize(DeviceState
*dev
, Error
**errp
)
920 VirtIOSerialPort
*port
= VIRTIO_SERIAL_PORT(dev
);
921 VirtIOSerialPortClass
*vsc
= VIRTIO_SERIAL_PORT_GET_CLASS(dev
);
922 VirtIOSerial
*vser
= port
->vser
;
924 qemu_bh_delete(port
->bh
);
925 remove_port(port
->vser
, port
->id
);
927 QTAILQ_REMOVE(&vser
->ports
, port
, next
);
929 if (vsc
->unrealize
) {
930 vsc
->unrealize(dev
, errp
);
934 static void virtio_serial_device_realize(DeviceState
*dev
, Error
**errp
)
936 VirtIODevice
*vdev
= VIRTIO_DEVICE(dev
);
937 VirtIOSerial
*vser
= VIRTIO_SERIAL(dev
);
938 uint32_t i
, max_supported_ports
;
940 if (!vser
->serial
.max_virtserial_ports
) {
941 error_setg(errp
, "Maximum number of serial ports not specified");
945 /* Each port takes 2 queues, and one pair is for the control queue */
946 max_supported_ports
= VIRTIO_PCI_QUEUE_MAX
/ 2 - 1;
948 if (vser
->serial
.max_virtserial_ports
> max_supported_ports
) {
949 error_setg(errp
, "maximum ports supported: %u", max_supported_ports
);
953 virtio_init(vdev
, "virtio-serial", VIRTIO_ID_CONSOLE
,
954 sizeof(struct virtio_console_config
));
956 /* Spawn a new virtio-serial bus on which the ports will ride as devices */
957 qbus_create_inplace(&vser
->bus
, sizeof(vser
->bus
), TYPE_VIRTIO_SERIAL_BUS
,
958 dev
, vdev
->bus_name
);
959 qbus_set_hotplug_handler(BUS(&vser
->bus
), DEVICE(vser
), errp
);
960 vser
->bus
.vser
= vser
;
961 QTAILQ_INIT(&vser
->ports
);
963 vser
->bus
.max_nr_ports
= vser
->serial
.max_virtserial_ports
;
964 vser
->ivqs
= g_malloc(vser
->serial
.max_virtserial_ports
965 * sizeof(VirtQueue
*));
966 vser
->ovqs
= g_malloc(vser
->serial
.max_virtserial_ports
967 * sizeof(VirtQueue
*));
969 /* Add a queue for host to guest transfers for port 0 (backward compat) */
970 vser
->ivqs
[0] = virtio_add_queue(vdev
, 128, handle_input
);
971 /* Add a queue for guest to host transfers for port 0 (backward compat) */
972 vser
->ovqs
[0] = virtio_add_queue(vdev
, 128, handle_output
);
974 /* TODO: host to guest notifications can get dropped
975 * if the queue fills up. Implement queueing in host,
976 * this might also make it possible to reduce the control
977 * queue size: as guest preposts buffers there,
978 * this will save 4Kbyte of guest memory per entry. */
980 /* control queue: host to guest */
981 vser
->c_ivq
= virtio_add_queue(vdev
, 32, control_in
);
982 /* control queue: guest to host */
983 vser
->c_ovq
= virtio_add_queue(vdev
, 32, control_out
);
985 for (i
= 1; i
< vser
->bus
.max_nr_ports
; i
++) {
986 /* Add a per-port queue for host to guest transfers */
987 vser
->ivqs
[i
] = virtio_add_queue(vdev
, 128, handle_input
);
988 /* Add a per-per queue for guest to host transfers */
989 vser
->ovqs
[i
] = virtio_add_queue(vdev
, 128, handle_output
);
992 vser
->ports_map
= g_malloc0(((vser
->serial
.max_virtserial_ports
+ 31) / 32)
993 * sizeof(vser
->ports_map
[0]));
995 * Reserve location 0 for a console port for backward compat
996 * (old kernel, new qemu)
998 mark_port_added(vser
, 0);
1000 vser
->post_load
= NULL
;
1003 * Register for the savevm section with the virtio-console name
1004 * to preserve backward compat
1006 register_savevm(dev
, "virtio-console", -1, 3, virtio_serial_save
,
1007 virtio_serial_load
, vser
);
1009 QLIST_INSERT_HEAD(&vserdevices
.devices
, vser
, next
);
1012 static void virtio_serial_port_class_init(ObjectClass
*klass
, void *data
)
1014 DeviceClass
*k
= DEVICE_CLASS(klass
);
1016 set_bit(DEVICE_CATEGORY_INPUT
, k
->categories
);
1017 k
->bus_type
= TYPE_VIRTIO_SERIAL_BUS
;
1018 k
->realize
= virtser_port_device_realize
;
1019 k
->unrealize
= virtser_port_device_unrealize
;
1020 k
->props
= virtser_props
;
1023 static const TypeInfo virtio_serial_port_type_info
= {
1024 .name
= TYPE_VIRTIO_SERIAL_PORT
,
1025 .parent
= TYPE_DEVICE
,
1026 .instance_size
= sizeof(VirtIOSerialPort
),
1028 .class_size
= sizeof(VirtIOSerialPortClass
),
1029 .class_init
= virtio_serial_port_class_init
,
1032 static void virtio_serial_device_unrealize(DeviceState
*dev
, Error
**errp
)
1034 VirtIODevice
*vdev
= VIRTIO_DEVICE(dev
);
1035 VirtIOSerial
*vser
= VIRTIO_SERIAL(dev
);
1037 QLIST_REMOVE(vser
, next
);
1039 unregister_savevm(dev
, "virtio-console", vser
);
1043 g_free(vser
->ports_map
);
1044 if (vser
->post_load
) {
1045 g_free(vser
->post_load
->connected
);
1046 timer_del(vser
->post_load
->timer
);
1047 timer_free(vser
->post_load
->timer
);
1048 g_free(vser
->post_load
);
1050 virtio_cleanup(vdev
);
1053 static Property virtio_serial_properties
[] = {
1054 DEFINE_VIRTIO_SERIAL_PROPERTIES(VirtIOSerial
, serial
),
1055 DEFINE_PROP_END_OF_LIST(),
1058 static void virtio_serial_class_init(ObjectClass
*klass
, void *data
)
1060 DeviceClass
*dc
= DEVICE_CLASS(klass
);
1061 VirtioDeviceClass
*vdc
= VIRTIO_DEVICE_CLASS(klass
);
1062 HotplugHandlerClass
*hc
= HOTPLUG_HANDLER_CLASS(klass
);
1064 QLIST_INIT(&vserdevices
.devices
);
1066 dc
->props
= virtio_serial_properties
;
1067 set_bit(DEVICE_CATEGORY_INPUT
, dc
->categories
);
1068 vdc
->realize
= virtio_serial_device_realize
;
1069 vdc
->unrealize
= virtio_serial_device_unrealize
;
1070 vdc
->get_features
= get_features
;
1071 vdc
->get_config
= get_config
;
1072 vdc
->set_status
= set_status
;
1073 vdc
->reset
= vser_reset
;
1074 vdc
->save
= virtio_serial_save_device
;
1075 vdc
->load
= virtio_serial_load_device
;
1076 hc
->plug
= virtser_port_device_plug
;
1077 hc
->unplug
= qdev_simple_device_unplug_cb
;
1080 static const TypeInfo virtio_device_info
= {
1081 .name
= TYPE_VIRTIO_SERIAL
,
1082 .parent
= TYPE_VIRTIO_DEVICE
,
1083 .instance_size
= sizeof(VirtIOSerial
),
1084 .class_init
= virtio_serial_class_init
,
1085 .interfaces
= (InterfaceInfo
[]) {
1086 { TYPE_HOTPLUG_HANDLER
},
1091 static void virtio_serial_register_types(void)
1093 type_register_static(&virtser_bus_info
);
1094 type_register_static(&virtio_serial_port_type_info
);
1095 type_register_static(&virtio_device_info
);
1098 type_init(virtio_serial_register_types
)