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/error-report.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 (port
->name
&& !strcmp(port
->name
, name
)) {
76 static bool use_multiport(VirtIOSerial
*vser
)
78 VirtIODevice
*vdev
= VIRTIO_DEVICE(vser
);
79 return virtio_vdev_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 /* TODO: detect a buffer that's too short, set NEEDS_RESET */
199 iov_from_buf(elem
.in_sg
, elem
.in_num
, 0, buf
, len
);
201 virtqueue_push(vq
, &elem
, len
);
202 virtio_notify(VIRTIO_DEVICE(vser
), vq
);
206 static size_t send_control_event(VirtIOSerial
*vser
, uint32_t port_id
,
207 uint16_t event
, uint16_t value
)
209 VirtIODevice
*vdev
= VIRTIO_DEVICE(vser
);
210 struct virtio_console_control cpkt
;
212 virtio_stl_p(vdev
, &cpkt
.id
, port_id
);
213 virtio_stw_p(vdev
, &cpkt
.event
, event
);
214 virtio_stw_p(vdev
, &cpkt
.value
, value
);
216 trace_virtio_serial_send_control_event(port_id
, event
, value
);
217 return send_control_msg(vser
, &cpkt
, sizeof(cpkt
));
220 /* Functions for use inside qemu to open and read from/write to ports */
221 int virtio_serial_open(VirtIOSerialPort
*port
)
223 /* Don't allow opening an already-open port */
224 if (port
->host_connected
) {
227 /* Send port open notification to the guest */
228 port
->host_connected
= true;
229 send_control_event(port
->vser
, port
->id
, VIRTIO_CONSOLE_PORT_OPEN
, 1);
234 int virtio_serial_close(VirtIOSerialPort
*port
)
236 port
->host_connected
= false;
238 * If there's any data the guest sent which the app didn't
239 * consume, reset the throttling flag and discard the data.
241 port
->throttled
= false;
242 discard_vq_data(port
->ovq
, VIRTIO_DEVICE(port
->vser
));
244 send_control_event(port
->vser
, port
->id
, VIRTIO_CONSOLE_PORT_OPEN
, 0);
249 /* Individual ports/apps call this function to write to the guest. */
250 ssize_t
virtio_serial_write(VirtIOSerialPort
*port
, const uint8_t *buf
,
253 if (!port
|| !port
->host_connected
|| !port
->guest_connected
) {
256 return write_to_port(port
, buf
, size
);
260 * Readiness of the guest to accept data on a port.
261 * Returns max. data the guest can receive
263 size_t virtio_serial_guest_ready(VirtIOSerialPort
*port
)
265 VirtIODevice
*vdev
= VIRTIO_DEVICE(port
->vser
);
266 VirtQueue
*vq
= port
->ivq
;
269 if (!virtio_queue_ready(vq
) ||
270 !(vdev
->status
& VIRTIO_CONFIG_S_DRIVER_OK
) ||
271 virtio_queue_empty(vq
)) {
274 if (use_multiport(port
->vser
) && !port
->guest_connected
) {
277 virtqueue_get_avail_bytes(vq
, &bytes
, NULL
, 4096, 0);
281 static void flush_queued_data_bh(void *opaque
)
283 VirtIOSerialPort
*port
= opaque
;
285 flush_queued_data(port
);
288 void virtio_serial_throttle_port(VirtIOSerialPort
*port
, bool throttle
)
294 trace_virtio_serial_throttle_port(port
->id
, throttle
);
295 port
->throttled
= throttle
;
299 qemu_bh_schedule(port
->bh
);
302 /* Guest wants to notify us of some event */
303 static void handle_control_message(VirtIOSerial
*vser
, void *buf
, size_t len
)
305 VirtIODevice
*vdev
= VIRTIO_DEVICE(vser
);
306 struct VirtIOSerialPort
*port
;
307 VirtIOSerialPortClass
*vsc
;
308 struct virtio_console_control cpkt
, *gcpkt
;
314 if (len
< sizeof(cpkt
)) {
315 /* The guest sent an invalid control packet */
319 cpkt
.event
= virtio_lduw_p(vdev
, &gcpkt
->event
);
320 cpkt
.value
= virtio_lduw_p(vdev
, &gcpkt
->value
);
322 trace_virtio_serial_handle_control_message(cpkt
.event
, cpkt
.value
);
324 if (cpkt
.event
== VIRTIO_CONSOLE_DEVICE_READY
) {
326 error_report("virtio-serial-bus: Guest failure in adding device %s",
327 vser
->bus
.qbus
.name
);
331 * The device is up, we can now tell the device about all the
332 * ports we have here.
334 QTAILQ_FOREACH(port
, &vser
->ports
, next
) {
335 send_control_event(vser
, port
->id
, VIRTIO_CONSOLE_PORT_ADD
, 1);
340 port
= find_port_by_id(vser
, virtio_ldl_p(vdev
, &gcpkt
->id
));
342 error_report("virtio-serial-bus: Unexpected port id %u for device %s",
343 virtio_ldl_p(vdev
, &gcpkt
->id
), vser
->bus
.qbus
.name
);
347 trace_virtio_serial_handle_control_message_port(port
->id
);
349 vsc
= VIRTIO_SERIAL_PORT_GET_CLASS(port
);
352 case VIRTIO_CONSOLE_PORT_READY
:
354 error_report("virtio-serial-bus: Guest failure in adding port %u for device %s",
355 port
->id
, vser
->bus
.qbus
.name
);
359 * Now that we know the guest asked for the port name, we're
360 * sure the guest has initialised whatever state is necessary
361 * for this port. Now's a good time to let the guest know if
362 * this port is a console port so that the guest can hook it
365 if (vsc
->is_console
) {
366 send_control_event(vser
, port
->id
, VIRTIO_CONSOLE_CONSOLE_PORT
, 1);
370 virtio_stl_p(vdev
, &cpkt
.id
, port
->id
);
371 virtio_stw_p(vdev
, &cpkt
.event
, VIRTIO_CONSOLE_PORT_NAME
);
372 virtio_stw_p(vdev
, &cpkt
.value
, 1);
374 buffer_len
= sizeof(cpkt
) + strlen(port
->name
) + 1;
375 buffer
= g_malloc(buffer_len
);
377 memcpy(buffer
, &cpkt
, sizeof(cpkt
));
378 memcpy(buffer
+ sizeof(cpkt
), port
->name
, strlen(port
->name
));
379 buffer
[buffer_len
- 1] = 0;
381 send_control_msg(vser
, buffer
, buffer_len
);
385 if (port
->host_connected
) {
386 send_control_event(vser
, port
->id
, VIRTIO_CONSOLE_PORT_OPEN
, 1);
390 * When the guest has asked us for this information it means
391 * the guest is all setup and has its virtqueues
392 * initialised. If some app is interested in knowing about
393 * this event, let it know.
395 if (vsc
->guest_ready
) {
396 vsc
->guest_ready(port
);
400 case VIRTIO_CONSOLE_PORT_OPEN
:
401 port
->guest_connected
= cpkt
.value
;
402 if (vsc
->set_guest_connected
) {
403 /* Send the guest opened notification if an app is interested */
404 vsc
->set_guest_connected(port
, cpkt
.value
);
410 static void control_in(VirtIODevice
*vdev
, VirtQueue
*vq
)
414 static void control_out(VirtIODevice
*vdev
, VirtQueue
*vq
)
416 VirtQueueElement elem
;
421 vser
= VIRTIO_SERIAL(vdev
);
425 while (virtqueue_pop(vq
, &elem
)) {
428 cur_len
= iov_size(elem
.out_sg
, elem
.out_num
);
430 * Allocate a new buf only if we didn't have one previously or
431 * if the size of the buf differs
436 buf
= g_malloc(cur_len
);
439 iov_to_buf(elem
.out_sg
, elem
.out_num
, 0, buf
, cur_len
);
441 handle_control_message(vser
, buf
, cur_len
);
442 virtqueue_push(vq
, &elem
, 0);
445 virtio_notify(vdev
, vq
);
448 /* Guest wrote something to some port. */
449 static void handle_output(VirtIODevice
*vdev
, VirtQueue
*vq
)
452 VirtIOSerialPort
*port
;
454 vser
= VIRTIO_SERIAL(vdev
);
455 port
= find_port_by_vq(vser
, vq
);
457 if (!port
|| !port
->host_connected
) {
458 discard_vq_data(vq
, vdev
);
462 if (!port
->throttled
) {
463 do_flush_queued_data(port
, vq
, vdev
);
468 static void handle_input(VirtIODevice
*vdev
, VirtQueue
*vq
)
471 * Users of virtio-serial would like to know when guest becomes
472 * writable again -- i.e. if a vq had stuff queued up and the
473 * guest wasn't reading at all, the host would not be able to
474 * write to the vq anymore. Once the guest reads off something,
475 * we can start queueing things up again. However, this call is
476 * made for each buffer addition by the guest -- even though free
477 * buffers existed prior to the current buffer addition. This is
478 * done so as not to maintain previous state, which will need
479 * additional live-migration-related changes.
482 VirtIOSerialPort
*port
;
483 VirtIOSerialPortClass
*vsc
;
485 vser
= VIRTIO_SERIAL(vdev
);
486 port
= find_port_by_vq(vser
, vq
);
491 vsc
= VIRTIO_SERIAL_PORT_GET_CLASS(port
);
494 * If guest_connected is false, this call is being made by the
495 * early-boot queueing up of descriptors, which is just noise for
496 * the host apps -- don't disturb them in that case.
498 if (port
->guest_connected
&& port
->host_connected
&& vsc
->guest_writable
) {
499 vsc
->guest_writable(port
);
503 static uint64_t get_features(VirtIODevice
*vdev
, uint64_t features
,
508 vser
= VIRTIO_SERIAL(vdev
);
510 if (vser
->bus
.max_nr_ports
> 1) {
511 virtio_add_feature(&features
, VIRTIO_CONSOLE_F_MULTIPORT
);
516 /* Guest requested config info */
517 static void get_config(VirtIODevice
*vdev
, uint8_t *config_data
)
519 VirtIOSerial
*vser
= VIRTIO_SERIAL(vdev
);
520 struct virtio_console_config
*config
=
521 (struct virtio_console_config
*)config_data
;
525 config
->max_nr_ports
= virtio_tswap32(vdev
,
526 vser
->serial
.max_virtserial_ports
);
529 static void guest_reset(VirtIOSerial
*vser
)
531 VirtIOSerialPort
*port
;
532 VirtIOSerialPortClass
*vsc
;
534 QTAILQ_FOREACH(port
, &vser
->ports
, next
) {
535 vsc
= VIRTIO_SERIAL_PORT_GET_CLASS(port
);
536 if (port
->guest_connected
) {
537 port
->guest_connected
= false;
538 if (vsc
->set_guest_connected
) {
539 vsc
->set_guest_connected(port
, false);
545 static void set_status(VirtIODevice
*vdev
, uint8_t status
)
548 VirtIOSerialPort
*port
;
550 vser
= VIRTIO_SERIAL(vdev
);
551 port
= find_port_by_id(vser
, 0);
553 if (port
&& !use_multiport(port
->vser
)
554 && (status
& VIRTIO_CONFIG_S_DRIVER_OK
)) {
556 * Non-multiport guests won't be able to tell us guest
557 * open/close status. Such guests can only have a port at id
558 * 0, so set guest_connected for such ports as soon as guest
561 port
->guest_connected
= true;
563 if (!(status
& VIRTIO_CONFIG_S_DRIVER_OK
)) {
568 static void vser_reset(VirtIODevice
*vdev
)
572 vser
= VIRTIO_SERIAL(vdev
);
576 static void virtio_serial_save(QEMUFile
*f
, void *opaque
)
578 /* The virtio device */
579 virtio_save(VIRTIO_DEVICE(opaque
), f
);
582 static void virtio_serial_save_device(VirtIODevice
*vdev
, QEMUFile
*f
)
584 VirtIOSerial
*s
= VIRTIO_SERIAL(vdev
);
585 VirtIOSerialPort
*port
;
586 uint32_t nr_active_ports
;
587 unsigned int i
, max_nr_ports
;
588 struct virtio_console_config config
;
590 /* The config space (ignored on the far end in current versions) */
591 get_config(vdev
, (uint8_t *)&config
);
592 qemu_put_be16s(f
, &config
.cols
);
593 qemu_put_be16s(f
, &config
.rows
);
594 qemu_put_be32s(f
, &config
.max_nr_ports
);
597 max_nr_ports
= s
->serial
.max_virtserial_ports
;
598 for (i
= 0; i
< (max_nr_ports
+ 31) / 32; i
++) {
599 qemu_put_be32s(f
, &s
->ports_map
[i
]);
605 QTAILQ_FOREACH(port
, &s
->ports
, next
) {
609 qemu_put_be32s(f
, &nr_active_ports
);
612 * Items in struct VirtIOSerialPort.
614 QTAILQ_FOREACH(port
, &s
->ports
, next
) {
615 uint32_t elem_popped
;
617 qemu_put_be32s(f
, &port
->id
);
618 qemu_put_byte(f
, port
->guest_connected
);
619 qemu_put_byte(f
, port
->host_connected
);
622 if (port
->elem
.out_num
) {
625 qemu_put_be32s(f
, &elem_popped
);
627 qemu_put_be32s(f
, &port
->iov_idx
);
628 qemu_put_be64s(f
, &port
->iov_offset
);
630 qemu_put_buffer(f
, (unsigned char *)&port
->elem
,
636 static void virtio_serial_post_load_timer_cb(void *opaque
)
639 VirtIOSerial
*s
= VIRTIO_SERIAL(opaque
);
640 VirtIOSerialPort
*port
;
641 uint8_t host_connected
;
642 VirtIOSerialPortClass
*vsc
;
647 for (i
= 0 ; i
< s
->post_load
->nr_active_ports
; ++i
) {
648 port
= s
->post_load
->connected
[i
].port
;
649 host_connected
= s
->post_load
->connected
[i
].host_connected
;
650 if (host_connected
!= port
->host_connected
) {
652 * We have to let the guest know of the host connection
655 send_control_event(s
, port
->id
, VIRTIO_CONSOLE_PORT_OPEN
,
656 port
->host_connected
);
658 vsc
= VIRTIO_SERIAL_PORT_GET_CLASS(port
);
659 if (vsc
->set_guest_connected
) {
660 vsc
->set_guest_connected(port
, port
->guest_connected
);
663 g_free(s
->post_load
->connected
);
664 timer_free(s
->post_load
->timer
);
665 g_free(s
->post_load
);
669 static int fetch_active_ports_list(QEMUFile
*f
, int version_id
,
670 VirtIOSerial
*s
, uint32_t nr_active_ports
)
674 s
->post_load
= g_malloc0(sizeof(*s
->post_load
));
675 s
->post_load
->nr_active_ports
= nr_active_ports
;
676 s
->post_load
->connected
=
677 g_malloc0(sizeof(*s
->post_load
->connected
) * nr_active_ports
);
679 s
->post_load
->timer
= timer_new_ns(QEMU_CLOCK_VIRTUAL
,
680 virtio_serial_post_load_timer_cb
,
683 /* Items in struct VirtIOSerialPort */
684 for (i
= 0; i
< nr_active_ports
; i
++) {
685 VirtIOSerialPort
*port
;
688 id
= qemu_get_be32(f
);
689 port
= find_port_by_id(s
, id
);
694 port
->guest_connected
= qemu_get_byte(f
);
695 s
->post_load
->connected
[i
].port
= port
;
696 s
->post_load
->connected
[i
].host_connected
= qemu_get_byte(f
);
698 if (version_id
> 2) {
699 uint32_t elem_popped
;
701 qemu_get_be32s(f
, &elem_popped
);
703 qemu_get_be32s(f
, &port
->iov_idx
);
704 qemu_get_be64s(f
, &port
->iov_offset
);
706 qemu_get_buffer(f
, (unsigned char *)&port
->elem
,
708 virtqueue_map_sg(port
->elem
.in_sg
, port
->elem
.in_addr
,
709 port
->elem
.in_num
, 1);
710 virtqueue_map_sg(port
->elem
.out_sg
, port
->elem
.out_addr
,
711 port
->elem
.out_num
, 1);
714 * Port was throttled on source machine. Let's
715 * unthrottle it here so data starts flowing again.
717 virtio_serial_throttle_port(port
, false);
721 timer_mod(s
->post_load
->timer
, 1);
725 static int virtio_serial_load(QEMUFile
*f
, void *opaque
, int version_id
)
727 if (version_id
> 3) {
731 /* The virtio device */
732 return virtio_load(VIRTIO_DEVICE(opaque
), f
, version_id
);
735 static int virtio_serial_load_device(VirtIODevice
*vdev
, QEMUFile
*f
,
738 VirtIOSerial
*s
= VIRTIO_SERIAL(vdev
);
739 uint32_t max_nr_ports
, nr_active_ports
, ports_map
;
744 if (version_id
< 2) {
749 qemu_get_be16s(f
, (uint16_t *) &tmp
);
750 qemu_get_be16s(f
, (uint16_t *) &tmp
);
751 qemu_get_be32s(f
, &tmp
);
753 max_nr_ports
= s
->serial
.max_virtserial_ports
;
754 for (i
= 0; i
< (max_nr_ports
+ 31) / 32; i
++) {
755 qemu_get_be32s(f
, &ports_map
);
757 if (ports_map
!= s
->ports_map
[i
]) {
759 * Ports active on source and destination don't
760 * match. Fail migration.
766 qemu_get_be32s(f
, &nr_active_ports
);
768 if (nr_active_ports
) {
769 ret
= fetch_active_ports_list(f
, version_id
, s
, nr_active_ports
);
777 static void virtser_bus_dev_print(Monitor
*mon
, DeviceState
*qdev
, int indent
);
779 static Property virtser_props
[] = {
780 DEFINE_PROP_UINT32("nr", VirtIOSerialPort
, id
, VIRTIO_CONSOLE_BAD_ID
),
781 DEFINE_PROP_STRING("name", VirtIOSerialPort
, name
),
782 DEFINE_PROP_END_OF_LIST()
785 #define TYPE_VIRTIO_SERIAL_BUS "virtio-serial-bus"
786 #define VIRTIO_SERIAL_BUS(obj) \
787 OBJECT_CHECK(VirtIOSerialBus, (obj), TYPE_VIRTIO_SERIAL_BUS)
789 static void virtser_bus_class_init(ObjectClass
*klass
, void *data
)
791 BusClass
*k
= BUS_CLASS(klass
);
792 k
->print_dev
= virtser_bus_dev_print
;
795 static const TypeInfo virtser_bus_info
= {
796 .name
= TYPE_VIRTIO_SERIAL_BUS
,
798 .instance_size
= sizeof(VirtIOSerialBus
),
799 .class_init
= virtser_bus_class_init
,
802 static void virtser_bus_dev_print(Monitor
*mon
, DeviceState
*qdev
, int indent
)
804 VirtIOSerialPort
*port
= DO_UPCAST(VirtIOSerialPort
, dev
, qdev
);
806 monitor_printf(mon
, "%*sport %d, guest %s, host %s, throttle %s\n",
807 indent
, "", port
->id
,
808 port
->guest_connected
? "on" : "off",
809 port
->host_connected
? "on" : "off",
810 port
->throttled
? "on" : "off");
813 /* This function is only used if a port id is not provided by the user */
814 static uint32_t find_free_port_id(VirtIOSerial
*vser
)
816 unsigned int i
, max_nr_ports
;
818 max_nr_ports
= vser
->serial
.max_virtserial_ports
;
819 for (i
= 0; i
< (max_nr_ports
+ 31) / 32; i
++) {
820 uint32_t map
, zeroes
;
822 map
= vser
->ports_map
[i
];
823 zeroes
= ctz32(~map
);
825 return zeroes
+ i
* 32;
828 return VIRTIO_CONSOLE_BAD_ID
;
831 static void mark_port_added(VirtIOSerial
*vser
, uint32_t port_id
)
836 vser
->ports_map
[i
] |= 1U << (port_id
% 32);
839 static void add_port(VirtIOSerial
*vser
, uint32_t port_id
)
841 mark_port_added(vser
, port_id
);
842 send_control_event(vser
, port_id
, VIRTIO_CONSOLE_PORT_ADD
, 1);
845 static void remove_port(VirtIOSerial
*vser
, uint32_t port_id
)
847 VirtIOSerialPort
*port
;
850 * Don't mark port 0 removed -- we explicitly reserve it for
851 * backward compat with older guests, ensure a virtconsole device
852 * unplug retains the reservation.
858 vser
->ports_map
[i
] &= ~(1U << (port_id
% 32));
861 port
= find_port_by_id(vser
, port_id
);
863 * This function is only called from qdev's unplug callback; if we
864 * get a NULL port here, we're in trouble.
868 /* Flush out any unconsumed buffers first */
869 discard_vq_data(port
->ovq
, VIRTIO_DEVICE(port
->vser
));
871 send_control_event(vser
, port
->id
, VIRTIO_CONSOLE_PORT_REMOVE
, 1);
874 static void virtser_port_device_realize(DeviceState
*dev
, Error
**errp
)
876 VirtIOSerialPort
*port
= VIRTIO_SERIAL_PORT(dev
);
877 VirtIOSerialPortClass
*vsc
= VIRTIO_SERIAL_PORT_GET_CLASS(port
);
878 VirtIOSerialBus
*bus
= VIRTIO_SERIAL_BUS(qdev_get_parent_bus(dev
));
883 port
->vser
= bus
->vser
;
884 port
->bh
= qemu_bh_new(flush_queued_data_bh
, port
);
886 assert(vsc
->have_data
);
889 * Is the first console port we're seeing? If so, put it up at
890 * location 0. This is done for backward compatibility (old
893 plugging_port0
= vsc
->is_console
&& !find_port_by_id(port
->vser
, 0);
895 if (find_port_by_id(port
->vser
, port
->id
)) {
896 error_setg(errp
, "virtio-serial-bus: A port already exists at id %u",
901 if (port
->name
!= NULL
&& find_port_by_name(port
->name
)) {
902 error_setg(errp
, "virtio-serial-bus: A port already exists by name %s",
907 if (port
->id
== VIRTIO_CONSOLE_BAD_ID
) {
908 if (plugging_port0
) {
911 port
->id
= find_free_port_id(port
->vser
);
912 if (port
->id
== VIRTIO_CONSOLE_BAD_ID
) {
913 error_setg(errp
, "virtio-serial-bus: Maximum port limit for "
914 "this device reached");
920 max_nr_ports
= port
->vser
->serial
.max_virtserial_ports
;
921 if (port
->id
>= max_nr_ports
) {
922 error_setg(errp
, "virtio-serial-bus: Out-of-range port id specified, "
923 "max. allowed: %u", max_nr_ports
- 1);
927 vsc
->realize(dev
, &err
);
929 error_propagate(errp
, err
);
933 port
->elem
.out_num
= 0;
936 static void virtser_port_device_plug(HotplugHandler
*hotplug_dev
,
937 DeviceState
*dev
, Error
**errp
)
939 VirtIOSerialPort
*port
= VIRTIO_SERIAL_PORT(dev
);
941 QTAILQ_INSERT_TAIL(&port
->vser
->ports
, port
, next
);
942 port
->ivq
= port
->vser
->ivqs
[port
->id
];
943 port
->ovq
= port
->vser
->ovqs
[port
->id
];
945 add_port(port
->vser
, port
->id
);
947 /* Send an update to the guest about this new port added */
948 virtio_notify_config(VIRTIO_DEVICE(hotplug_dev
));
951 static void virtser_port_device_unrealize(DeviceState
*dev
, Error
**errp
)
953 VirtIOSerialPort
*port
= VIRTIO_SERIAL_PORT(dev
);
954 VirtIOSerialPortClass
*vsc
= VIRTIO_SERIAL_PORT_GET_CLASS(dev
);
955 VirtIOSerial
*vser
= port
->vser
;
957 qemu_bh_delete(port
->bh
);
958 remove_port(port
->vser
, port
->id
);
960 QTAILQ_REMOVE(&vser
->ports
, port
, next
);
962 if (vsc
->unrealize
) {
963 vsc
->unrealize(dev
, errp
);
967 static void virtio_serial_device_realize(DeviceState
*dev
, Error
**errp
)
969 VirtIODevice
*vdev
= VIRTIO_DEVICE(dev
);
970 VirtIOSerial
*vser
= VIRTIO_SERIAL(dev
);
971 uint32_t i
, max_supported_ports
;
973 if (!vser
->serial
.max_virtserial_ports
) {
974 error_setg(errp
, "Maximum number of serial ports not specified");
978 /* Each port takes 2 queues, and one pair is for the control queue */
979 max_supported_ports
= VIRTIO_QUEUE_MAX
/ 2 - 1;
981 if (vser
->serial
.max_virtserial_ports
> max_supported_ports
) {
982 error_setg(errp
, "maximum ports supported: %u", max_supported_ports
);
986 /* We don't support emergency write, skip it for now. */
987 /* TODO: cleaner fix, depending on host features. */
988 virtio_init(vdev
, "virtio-serial", VIRTIO_ID_CONSOLE
,
989 offsetof(struct virtio_console_config
, emerg_wr
));
991 /* Spawn a new virtio-serial bus on which the ports will ride as devices */
992 qbus_create_inplace(&vser
->bus
, sizeof(vser
->bus
), TYPE_VIRTIO_SERIAL_BUS
,
993 dev
, vdev
->bus_name
);
994 qbus_set_hotplug_handler(BUS(&vser
->bus
), DEVICE(vser
), errp
);
995 vser
->bus
.vser
= vser
;
996 QTAILQ_INIT(&vser
->ports
);
998 vser
->bus
.max_nr_ports
= vser
->serial
.max_virtserial_ports
;
999 vser
->ivqs
= g_malloc(vser
->serial
.max_virtserial_ports
1000 * sizeof(VirtQueue
*));
1001 vser
->ovqs
= g_malloc(vser
->serial
.max_virtserial_ports
1002 * sizeof(VirtQueue
*));
1004 /* Add a queue for host to guest transfers for port 0 (backward compat) */
1005 vser
->ivqs
[0] = virtio_add_queue(vdev
, 128, handle_input
);
1006 /* Add a queue for guest to host transfers for port 0 (backward compat) */
1007 vser
->ovqs
[0] = virtio_add_queue(vdev
, 128, handle_output
);
1009 /* TODO: host to guest notifications can get dropped
1010 * if the queue fills up. Implement queueing in host,
1011 * this might also make it possible to reduce the control
1012 * queue size: as guest preposts buffers there,
1013 * this will save 4Kbyte of guest memory per entry. */
1015 /* control queue: host to guest */
1016 vser
->c_ivq
= virtio_add_queue(vdev
, 32, control_in
);
1017 /* control queue: guest to host */
1018 vser
->c_ovq
= virtio_add_queue(vdev
, 32, control_out
);
1020 for (i
= 1; i
< vser
->bus
.max_nr_ports
; i
++) {
1021 /* Add a per-port queue for host to guest transfers */
1022 vser
->ivqs
[i
] = virtio_add_queue(vdev
, 128, handle_input
);
1023 /* Add a per-per queue for guest to host transfers */
1024 vser
->ovqs
[i
] = virtio_add_queue(vdev
, 128, handle_output
);
1027 vser
->ports_map
= g_malloc0(((vser
->serial
.max_virtserial_ports
+ 31) / 32)
1028 * sizeof(vser
->ports_map
[0]));
1030 * Reserve location 0 for a console port for backward compat
1031 * (old kernel, new qemu)
1033 mark_port_added(vser
, 0);
1035 vser
->post_load
= NULL
;
1038 * Register for the savevm section with the virtio-console name
1039 * to preserve backward compat
1041 register_savevm(dev
, "virtio-console", -1, 3, virtio_serial_save
,
1042 virtio_serial_load
, vser
);
1044 QLIST_INSERT_HEAD(&vserdevices
.devices
, vser
, next
);
1047 static void virtio_serial_port_class_init(ObjectClass
*klass
, void *data
)
1049 DeviceClass
*k
= DEVICE_CLASS(klass
);
1051 set_bit(DEVICE_CATEGORY_INPUT
, k
->categories
);
1052 k
->bus_type
= TYPE_VIRTIO_SERIAL_BUS
;
1053 k
->realize
= virtser_port_device_realize
;
1054 k
->unrealize
= virtser_port_device_unrealize
;
1055 k
->props
= virtser_props
;
1058 static const TypeInfo virtio_serial_port_type_info
= {
1059 .name
= TYPE_VIRTIO_SERIAL_PORT
,
1060 .parent
= TYPE_DEVICE
,
1061 .instance_size
= sizeof(VirtIOSerialPort
),
1063 .class_size
= sizeof(VirtIOSerialPortClass
),
1064 .class_init
= virtio_serial_port_class_init
,
1067 static void virtio_serial_device_unrealize(DeviceState
*dev
, Error
**errp
)
1069 VirtIODevice
*vdev
= VIRTIO_DEVICE(dev
);
1070 VirtIOSerial
*vser
= VIRTIO_SERIAL(dev
);
1072 QLIST_REMOVE(vser
, next
);
1074 unregister_savevm(dev
, "virtio-console", vser
);
1078 g_free(vser
->ports_map
);
1079 if (vser
->post_load
) {
1080 g_free(vser
->post_load
->connected
);
1081 timer_del(vser
->post_load
->timer
);
1082 timer_free(vser
->post_load
->timer
);
1083 g_free(vser
->post_load
);
1085 virtio_cleanup(vdev
);
1088 static Property virtio_serial_properties
[] = {
1089 DEFINE_PROP_UINT32("max_ports", VirtIOSerial
, serial
.max_virtserial_ports
,
1091 DEFINE_PROP_END_OF_LIST(),
1094 static void virtio_serial_class_init(ObjectClass
*klass
, void *data
)
1096 DeviceClass
*dc
= DEVICE_CLASS(klass
);
1097 VirtioDeviceClass
*vdc
= VIRTIO_DEVICE_CLASS(klass
);
1098 HotplugHandlerClass
*hc
= HOTPLUG_HANDLER_CLASS(klass
);
1100 QLIST_INIT(&vserdevices
.devices
);
1102 dc
->props
= virtio_serial_properties
;
1103 set_bit(DEVICE_CATEGORY_INPUT
, dc
->categories
);
1104 vdc
->realize
= virtio_serial_device_realize
;
1105 vdc
->unrealize
= virtio_serial_device_unrealize
;
1106 vdc
->get_features
= get_features
;
1107 vdc
->get_config
= get_config
;
1108 vdc
->set_status
= set_status
;
1109 vdc
->reset
= vser_reset
;
1110 vdc
->save
= virtio_serial_save_device
;
1111 vdc
->load
= virtio_serial_load_device
;
1112 hc
->plug
= virtser_port_device_plug
;
1113 hc
->unplug
= qdev_simple_device_unplug_cb
;
1116 static const TypeInfo virtio_device_info
= {
1117 .name
= TYPE_VIRTIO_SERIAL
,
1118 .parent
= TYPE_VIRTIO_DEVICE
,
1119 .instance_size
= sizeof(VirtIOSerial
),
1120 .class_init
= virtio_serial_class_init
,
1121 .interfaces
= (InterfaceInfo
[]) {
1122 { TYPE_HOTPLUG_HANDLER
},
1127 static void virtio_serial_register_types(void)
1129 type_register_static(&virtser_bus_info
);
1130 type_register_static(&virtio_serial_port_type_info
);
1131 type_register_static(&virtio_device_info
);
1134 type_init(virtio_serial_register_types
)