2 * A bus for connecting virtio serial and console ports
4 * Copyright (C) 2009, 2010 Red Hat, Inc.
7 * Amit Shah <amit.shah@redhat.com>
9 * Some earlier parts are:
10 * Copyright IBM, Corp. 2008
12 * Christian Ehrhardt <ehrhardt@linux.vnet.ibm.com>
14 * This work is licensed under the terms of the GNU GPL, version 2. See
15 * the COPYING file in the top-level directory.
17 * Contributions after 2012-01-13 are licensed under the terms of the
18 * GNU GPL, version 2 or (at your option) any later version.
22 #include "monitor/monitor.h"
23 #include "qemu/queue.h"
24 #include "hw/sysbus.h"
26 #include "hw/virtio/virtio-serial.h"
28 static VirtIOSerialPort
*find_port_by_id(VirtIOSerial
*vser
, uint32_t id
)
30 VirtIOSerialPort
*port
;
32 if (id
== VIRTIO_CONSOLE_BAD_ID
) {
36 QTAILQ_FOREACH(port
, &vser
->ports
, next
) {
43 static VirtIOSerialPort
*find_port_by_vq(VirtIOSerial
*vser
, VirtQueue
*vq
)
45 VirtIOSerialPort
*port
;
47 QTAILQ_FOREACH(port
, &vser
->ports
, next
) {
48 if (port
->ivq
== vq
|| port
->ovq
== vq
)
54 static bool use_multiport(VirtIOSerial
*vser
)
56 VirtIODevice
*vdev
= VIRTIO_DEVICE(vser
);
57 return vdev
->guest_features
& (1 << VIRTIO_CONSOLE_F_MULTIPORT
);
60 static size_t write_to_port(VirtIOSerialPort
*port
,
61 const uint8_t *buf
, size_t size
)
63 VirtQueueElement elem
;
68 if (!virtio_queue_ready(vq
)) {
73 while (offset
< size
) {
76 if (!virtqueue_pop(vq
, &elem
)) {
80 len
= iov_from_buf(elem
.in_sg
, elem
.in_num
, 0,
81 buf
+ offset
, size
- offset
);
84 virtqueue_push(vq
, &elem
, len
);
87 virtio_notify(VIRTIO_DEVICE(port
->vser
), vq
);
91 static void discard_vq_data(VirtQueue
*vq
, VirtIODevice
*vdev
)
93 VirtQueueElement elem
;
95 if (!virtio_queue_ready(vq
)) {
98 while (virtqueue_pop(vq
, &elem
)) {
99 virtqueue_push(vq
, &elem
, 0);
101 virtio_notify(vdev
, vq
);
104 static void do_flush_queued_data(VirtIOSerialPort
*port
, VirtQueue
*vq
,
107 VirtIOSerialPortClass
*vsc
;
110 assert(virtio_queue_ready(vq
));
112 vsc
= VIRTIO_SERIAL_PORT_GET_CLASS(port
);
114 while (!port
->throttled
) {
117 /* Pop an elem only if we haven't left off a previous one mid-way */
118 if (!port
->elem
.out_num
) {
119 if (!virtqueue_pop(vq
, &port
->elem
)) {
123 port
->iov_offset
= 0;
126 for (i
= port
->iov_idx
; i
< port
->elem
.out_num
; i
++) {
130 buf_size
= port
->elem
.out_sg
[i
].iov_len
- port
->iov_offset
;
131 ret
= vsc
->have_data(port
,
132 port
->elem
.out_sg
[i
].iov_base
135 if (port
->throttled
) {
138 port
->iov_offset
+= ret
;
142 port
->iov_offset
= 0;
144 if (port
->throttled
) {
147 virtqueue_push(vq
, &port
->elem
, 0);
148 port
->elem
.out_num
= 0;
150 virtio_notify(vdev
, vq
);
153 static void flush_queued_data(VirtIOSerialPort
*port
)
157 if (!virtio_queue_ready(port
->ovq
)) {
160 do_flush_queued_data(port
, port
->ovq
, VIRTIO_DEVICE(port
->vser
));
163 static size_t send_control_msg(VirtIOSerial
*vser
, void *buf
, size_t len
)
165 VirtQueueElement elem
;
169 if (!virtio_queue_ready(vq
)) {
172 if (!virtqueue_pop(vq
, &elem
)) {
176 memcpy(elem
.in_sg
[0].iov_base
, buf
, len
);
178 virtqueue_push(vq
, &elem
, len
);
179 virtio_notify(VIRTIO_DEVICE(vser
), vq
);
183 static size_t send_control_event(VirtIOSerial
*vser
, uint32_t port_id
,
184 uint16_t event
, uint16_t value
)
186 struct virtio_console_control cpkt
;
188 stl_p(&cpkt
.id
, port_id
);
189 stw_p(&cpkt
.event
, event
);
190 stw_p(&cpkt
.value
, value
);
192 trace_virtio_serial_send_control_event(port_id
, event
, value
);
193 return send_control_msg(vser
, &cpkt
, sizeof(cpkt
));
196 /* Functions for use inside qemu to open and read from/write to ports */
197 int virtio_serial_open(VirtIOSerialPort
*port
)
199 /* Don't allow opening an already-open port */
200 if (port
->host_connected
) {
203 /* Send port open notification to the guest */
204 port
->host_connected
= true;
205 send_control_event(port
->vser
, port
->id
, VIRTIO_CONSOLE_PORT_OPEN
, 1);
210 int virtio_serial_close(VirtIOSerialPort
*port
)
212 port
->host_connected
= false;
214 * If there's any data the guest sent which the app didn't
215 * consume, reset the throttling flag and discard the data.
217 port
->throttled
= false;
218 discard_vq_data(port
->ovq
, VIRTIO_DEVICE(port
->vser
));
220 send_control_event(port
->vser
, port
->id
, VIRTIO_CONSOLE_PORT_OPEN
, 0);
225 /* Individual ports/apps call this function to write to the guest. */
226 ssize_t
virtio_serial_write(VirtIOSerialPort
*port
, const uint8_t *buf
,
229 if (!port
|| !port
->host_connected
|| !port
->guest_connected
) {
232 return write_to_port(port
, buf
, size
);
236 * Readiness of the guest to accept data on a port.
237 * Returns max. data the guest can receive
239 size_t virtio_serial_guest_ready(VirtIOSerialPort
*port
)
241 VirtIODevice
*vdev
= VIRTIO_DEVICE(port
->vser
);
242 VirtQueue
*vq
= port
->ivq
;
245 if (!virtio_queue_ready(vq
) ||
246 !(vdev
->status
& VIRTIO_CONFIG_S_DRIVER_OK
) ||
247 virtio_queue_empty(vq
)) {
250 if (use_multiport(port
->vser
) && !port
->guest_connected
) {
253 virtqueue_get_avail_bytes(vq
, &bytes
, NULL
, 4096, 0);
257 static void flush_queued_data_bh(void *opaque
)
259 VirtIOSerialPort
*port
= opaque
;
261 flush_queued_data(port
);
264 void virtio_serial_throttle_port(VirtIOSerialPort
*port
, bool throttle
)
270 trace_virtio_serial_throttle_port(port
->id
, throttle
);
271 port
->throttled
= throttle
;
275 qemu_bh_schedule(port
->bh
);
278 /* Guest wants to notify us of some event */
279 static void handle_control_message(VirtIOSerial
*vser
, void *buf
, size_t len
)
281 struct VirtIOSerialPort
*port
;
282 VirtIOSerialPortClass
*vsc
;
283 struct virtio_console_control cpkt
, *gcpkt
;
289 if (len
< sizeof(cpkt
)) {
290 /* The guest sent an invalid control packet */
294 cpkt
.event
= lduw_p(&gcpkt
->event
);
295 cpkt
.value
= lduw_p(&gcpkt
->value
);
297 trace_virtio_serial_handle_control_message(cpkt
.event
, cpkt
.value
);
299 if (cpkt
.event
== VIRTIO_CONSOLE_DEVICE_READY
) {
301 error_report("virtio-serial-bus: Guest failure in adding device %s",
302 vser
->bus
.qbus
.name
);
306 * The device is up, we can now tell the device about all the
307 * ports we have here.
309 QTAILQ_FOREACH(port
, &vser
->ports
, next
) {
310 send_control_event(vser
, port
->id
, VIRTIO_CONSOLE_PORT_ADD
, 1);
315 port
= find_port_by_id(vser
, ldl_p(&gcpkt
->id
));
317 error_report("virtio-serial-bus: Unexpected port id %u for device %s",
318 ldl_p(&gcpkt
->id
), vser
->bus
.qbus
.name
);
322 trace_virtio_serial_handle_control_message_port(port
->id
);
324 vsc
= VIRTIO_SERIAL_PORT_GET_CLASS(port
);
327 case VIRTIO_CONSOLE_PORT_READY
:
329 error_report("virtio-serial-bus: Guest failure in adding port %u for device %s",
330 port
->id
, vser
->bus
.qbus
.name
);
334 * Now that we know the guest asked for the port name, we're
335 * sure the guest has initialised whatever state is necessary
336 * for this port. Now's a good time to let the guest know if
337 * this port is a console port so that the guest can hook it
340 if (vsc
->is_console
) {
341 send_control_event(vser
, port
->id
, VIRTIO_CONSOLE_CONSOLE_PORT
, 1);
345 stl_p(&cpkt
.id
, port
->id
);
346 stw_p(&cpkt
.event
, VIRTIO_CONSOLE_PORT_NAME
);
347 stw_p(&cpkt
.value
, 1);
349 buffer_len
= sizeof(cpkt
) + strlen(port
->name
) + 1;
350 buffer
= g_malloc(buffer_len
);
352 memcpy(buffer
, &cpkt
, sizeof(cpkt
));
353 memcpy(buffer
+ sizeof(cpkt
), port
->name
, strlen(port
->name
));
354 buffer
[buffer_len
- 1] = 0;
356 send_control_msg(vser
, buffer
, buffer_len
);
360 if (port
->host_connected
) {
361 send_control_event(vser
, port
->id
, VIRTIO_CONSOLE_PORT_OPEN
, 1);
365 * When the guest has asked us for this information it means
366 * the guest is all setup and has its virtqueues
367 * initialised. If some app is interested in knowing about
368 * this event, let it know.
370 if (vsc
->guest_ready
) {
371 vsc
->guest_ready(port
);
375 case VIRTIO_CONSOLE_PORT_OPEN
:
376 port
->guest_connected
= cpkt
.value
;
377 if (vsc
->set_guest_connected
) {
378 /* Send the guest opened notification if an app is interested */
379 vsc
->set_guest_connected(port
, cpkt
.value
);
385 static void control_in(VirtIODevice
*vdev
, VirtQueue
*vq
)
389 static void control_out(VirtIODevice
*vdev
, VirtQueue
*vq
)
391 VirtQueueElement elem
;
396 vser
= VIRTIO_SERIAL(vdev
);
400 while (virtqueue_pop(vq
, &elem
)) {
403 cur_len
= iov_size(elem
.out_sg
, elem
.out_num
);
405 * Allocate a new buf only if we didn't have one previously or
406 * if the size of the buf differs
411 buf
= g_malloc(cur_len
);
414 iov_to_buf(elem
.out_sg
, elem
.out_num
, 0, buf
, cur_len
);
416 handle_control_message(vser
, buf
, cur_len
);
417 virtqueue_push(vq
, &elem
, 0);
420 virtio_notify(vdev
, vq
);
423 /* Guest wrote something to some port. */
424 static void handle_output(VirtIODevice
*vdev
, VirtQueue
*vq
)
427 VirtIOSerialPort
*port
;
429 vser
= VIRTIO_SERIAL(vdev
);
430 port
= find_port_by_vq(vser
, vq
);
432 if (!port
|| !port
->host_connected
) {
433 discard_vq_data(vq
, vdev
);
437 if (!port
->throttled
) {
438 do_flush_queued_data(port
, vq
, vdev
);
443 static void handle_input(VirtIODevice
*vdev
, VirtQueue
*vq
)
447 static uint32_t get_features(VirtIODevice
*vdev
, uint32_t features
)
451 vser
= VIRTIO_SERIAL(vdev
);
453 if (vser
->bus
.max_nr_ports
> 1) {
454 features
|= (1 << VIRTIO_CONSOLE_F_MULTIPORT
);
459 /* Guest requested config info */
460 static void get_config(VirtIODevice
*vdev
, uint8_t *config_data
)
464 vser
= VIRTIO_SERIAL(vdev
);
465 memcpy(config_data
, &vser
->config
, sizeof(struct virtio_console_config
));
468 static void set_config(VirtIODevice
*vdev
, const uint8_t *config_data
)
470 struct virtio_console_config config
;
472 memcpy(&config
, config_data
, sizeof(config
));
475 static void guest_reset(VirtIOSerial
*vser
)
477 VirtIOSerialPort
*port
;
478 VirtIOSerialPortClass
*vsc
;
480 QTAILQ_FOREACH(port
, &vser
->ports
, next
) {
481 vsc
= VIRTIO_SERIAL_PORT_GET_CLASS(port
);
482 if (port
->guest_connected
) {
483 port
->guest_connected
= false;
484 if (vsc
->set_guest_connected
) {
485 vsc
->set_guest_connected(port
, false);
491 static void set_status(VirtIODevice
*vdev
, uint8_t status
)
494 VirtIOSerialPort
*port
;
496 vser
= VIRTIO_SERIAL(vdev
);
497 port
= find_port_by_id(vser
, 0);
499 if (port
&& !use_multiport(port
->vser
)
500 && (status
& VIRTIO_CONFIG_S_DRIVER_OK
)) {
502 * Non-multiport guests won't be able to tell us guest
503 * open/close status. Such guests can only have a port at id
504 * 0, so set guest_connected for such ports as soon as guest
507 port
->guest_connected
= true;
509 if (!(status
& VIRTIO_CONFIG_S_DRIVER_OK
)) {
514 static void vser_reset(VirtIODevice
*vdev
)
518 vser
= VIRTIO_SERIAL(vdev
);
522 static void virtio_serial_save(QEMUFile
*f
, void *opaque
)
524 VirtIOSerial
*s
= VIRTIO_SERIAL(opaque
);
525 VirtIOSerialPort
*port
;
526 uint32_t nr_active_ports
;
527 unsigned int i
, max_nr_ports
;
529 /* The virtio device */
530 virtio_save(VIRTIO_DEVICE(s
), f
);
532 /* The config space */
533 qemu_put_be16s(f
, &s
->config
.cols
);
534 qemu_put_be16s(f
, &s
->config
.rows
);
536 qemu_put_be32s(f
, &s
->config
.max_nr_ports
);
539 max_nr_ports
= tswap32(s
->config
.max_nr_ports
);
540 for (i
= 0; i
< (max_nr_ports
+ 31) / 32; i
++) {
541 qemu_put_be32s(f
, &s
->ports_map
[i
]);
547 QTAILQ_FOREACH(port
, &s
->ports
, next
) {
551 qemu_put_be32s(f
, &nr_active_ports
);
554 * Items in struct VirtIOSerialPort.
556 QTAILQ_FOREACH(port
, &s
->ports
, next
) {
557 uint32_t elem_popped
;
559 qemu_put_be32s(f
, &port
->id
);
560 qemu_put_byte(f
, port
->guest_connected
);
561 qemu_put_byte(f
, port
->host_connected
);
564 if (port
->elem
.out_num
) {
567 qemu_put_be32s(f
, &elem_popped
);
569 qemu_put_be32s(f
, &port
->iov_idx
);
570 qemu_put_be64s(f
, &port
->iov_offset
);
572 qemu_put_buffer(f
, (unsigned char *)&port
->elem
,
578 static void virtio_serial_post_load_timer_cb(void *opaque
)
581 VirtIOSerial
*s
= VIRTIO_SERIAL(opaque
);
582 VirtIOSerialPort
*port
;
583 uint8_t host_connected
;
584 VirtIOSerialPortClass
*vsc
;
589 for (i
= 0 ; i
< s
->post_load
->nr_active_ports
; ++i
) {
590 port
= s
->post_load
->connected
[i
].port
;
591 host_connected
= s
->post_load
->connected
[i
].host_connected
;
592 if (host_connected
!= port
->host_connected
) {
594 * We have to let the guest know of the host connection
597 send_control_event(s
, port
->id
, VIRTIO_CONSOLE_PORT_OPEN
,
598 port
->host_connected
);
600 vsc
= VIRTIO_SERIAL_PORT_GET_CLASS(port
);
601 if (vsc
->set_guest_connected
) {
602 vsc
->set_guest_connected(port
, port
->guest_connected
);
605 g_free(s
->post_load
->connected
);
606 timer_free(s
->post_load
->timer
);
607 g_free(s
->post_load
);
611 static int fetch_active_ports_list(QEMUFile
*f
, int version_id
,
612 VirtIOSerial
*s
, uint32_t nr_active_ports
)
616 s
->post_load
= g_malloc0(sizeof(*s
->post_load
));
617 s
->post_load
->nr_active_ports
= nr_active_ports
;
618 s
->post_load
->connected
=
619 g_malloc0(sizeof(*s
->post_load
->connected
) * nr_active_ports
);
621 s
->post_load
->timer
= timer_new_ns(QEMU_CLOCK_VIRTUAL
,
622 virtio_serial_post_load_timer_cb
,
625 /* Items in struct VirtIOSerialPort */
626 for (i
= 0; i
< nr_active_ports
; i
++) {
627 VirtIOSerialPort
*port
;
630 id
= qemu_get_be32(f
);
631 port
= find_port_by_id(s
, id
);
636 port
->guest_connected
= qemu_get_byte(f
);
637 s
->post_load
->connected
[i
].port
= port
;
638 s
->post_load
->connected
[i
].host_connected
= qemu_get_byte(f
);
640 if (version_id
> 2) {
641 uint32_t elem_popped
;
643 qemu_get_be32s(f
, &elem_popped
);
645 qemu_get_be32s(f
, &port
->iov_idx
);
646 qemu_get_be64s(f
, &port
->iov_offset
);
648 qemu_get_buffer(f
, (unsigned char *)&port
->elem
,
650 virtqueue_map_sg(port
->elem
.in_sg
, port
->elem
.in_addr
,
651 port
->elem
.in_num
, 1);
652 virtqueue_map_sg(port
->elem
.out_sg
, port
->elem
.out_addr
,
653 port
->elem
.out_num
, 1);
656 * Port was throttled on source machine. Let's
657 * unthrottle it here so data starts flowing again.
659 virtio_serial_throttle_port(port
, false);
663 timer_mod(s
->post_load
->timer
, 1);
667 static int virtio_serial_load(QEMUFile
*f
, void *opaque
, int version_id
)
669 VirtIOSerial
*s
= VIRTIO_SERIAL(opaque
);
670 uint32_t max_nr_ports
, nr_active_ports
, ports_map
;
674 if (version_id
> 3) {
678 /* The virtio device */
679 ret
= virtio_load(VIRTIO_DEVICE(s
), f
);
684 if (version_id
< 2) {
688 /* The config space */
689 qemu_get_be16s(f
, &s
->config
.cols
);
690 qemu_get_be16s(f
, &s
->config
.rows
);
692 qemu_get_be32s(f
, &max_nr_ports
);
693 tswap32s(&max_nr_ports
);
694 if (max_nr_ports
> tswap32(s
->config
.max_nr_ports
)) {
695 /* Source could have had more ports than us. Fail migration. */
699 for (i
= 0; i
< (max_nr_ports
+ 31) / 32; i
++) {
700 qemu_get_be32s(f
, &ports_map
);
702 if (ports_map
!= s
->ports_map
[i
]) {
704 * Ports active on source and destination don't
705 * match. Fail migration.
711 qemu_get_be32s(f
, &nr_active_ports
);
713 if (nr_active_ports
) {
714 ret
= fetch_active_ports_list(f
, version_id
, s
, nr_active_ports
);
722 static void virtser_bus_dev_print(Monitor
*mon
, DeviceState
*qdev
, int indent
);
724 static Property virtser_props
[] = {
725 DEFINE_PROP_UINT32("nr", VirtIOSerialPort
, id
, VIRTIO_CONSOLE_BAD_ID
),
726 DEFINE_PROP_STRING("name", VirtIOSerialPort
, name
),
727 DEFINE_PROP_END_OF_LIST()
730 #define TYPE_VIRTIO_SERIAL_BUS "virtio-serial-bus"
731 #define VIRTIO_SERIAL_BUS(obj) \
732 OBJECT_CHECK(VirtIOSerialBus, (obj), TYPE_VIRTIO_SERIAL_BUS)
734 static void virtser_bus_class_init(ObjectClass
*klass
, void *data
)
736 BusClass
*k
= BUS_CLASS(klass
);
737 k
->print_dev
= virtser_bus_dev_print
;
740 static const TypeInfo virtser_bus_info
= {
741 .name
= TYPE_VIRTIO_SERIAL_BUS
,
743 .instance_size
= sizeof(VirtIOSerialBus
),
744 .class_init
= virtser_bus_class_init
,
747 static void virtser_bus_dev_print(Monitor
*mon
, DeviceState
*qdev
, int indent
)
749 VirtIOSerialPort
*port
= DO_UPCAST(VirtIOSerialPort
, dev
, qdev
);
751 monitor_printf(mon
, "%*sport %d, guest %s, host %s, throttle %s\n",
752 indent
, "", port
->id
,
753 port
->guest_connected
? "on" : "off",
754 port
->host_connected
? "on" : "off",
755 port
->throttled
? "on" : "off");
758 /* This function is only used if a port id is not provided by the user */
759 static uint32_t find_free_port_id(VirtIOSerial
*vser
)
761 unsigned int i
, max_nr_ports
;
763 max_nr_ports
= tswap32(vser
->config
.max_nr_ports
);
764 for (i
= 0; i
< (max_nr_ports
+ 31) / 32; i
++) {
767 map
= vser
->ports_map
[i
];
770 return (bit
- 1) + i
* 32;
773 return VIRTIO_CONSOLE_BAD_ID
;
776 static void mark_port_added(VirtIOSerial
*vser
, uint32_t port_id
)
781 vser
->ports_map
[i
] |= 1U << (port_id
% 32);
784 static void add_port(VirtIOSerial
*vser
, uint32_t port_id
)
786 mark_port_added(vser
, port_id
);
787 send_control_event(vser
, port_id
, VIRTIO_CONSOLE_PORT_ADD
, 1);
790 static void remove_port(VirtIOSerial
*vser
, uint32_t port_id
)
792 VirtIOSerialPort
*port
;
796 vser
->ports_map
[i
] &= ~(1U << (port_id
% 32));
798 port
= find_port_by_id(vser
, port_id
);
800 * This function is only called from qdev's unplug callback; if we
801 * get a NULL port here, we're in trouble.
805 /* Flush out any unconsumed buffers first */
806 discard_vq_data(port
->ovq
, VIRTIO_DEVICE(port
->vser
));
808 send_control_event(vser
, port
->id
, VIRTIO_CONSOLE_PORT_REMOVE
, 1);
811 static void virtser_port_device_realize(DeviceState
*dev
, Error
**errp
)
813 VirtIOSerialPort
*port
= VIRTIO_SERIAL_PORT(dev
);
814 VirtIOSerialPortClass
*vsc
= VIRTIO_SERIAL_PORT_GET_CLASS(port
);
815 VirtIOSerialBus
*bus
= VIRTIO_SERIAL_BUS(qdev_get_parent_bus(dev
));
820 port
->vser
= bus
->vser
;
821 port
->bh
= qemu_bh_new(flush_queued_data_bh
, port
);
823 assert(vsc
->have_data
);
826 * Is the first console port we're seeing? If so, put it up at
827 * location 0. This is done for backward compatibility (old
830 plugging_port0
= vsc
->is_console
&& !find_port_by_id(port
->vser
, 0);
832 if (find_port_by_id(port
->vser
, port
->id
)) {
833 error_setg(errp
, "virtio-serial-bus: A port already exists at id %u",
838 if (port
->id
== VIRTIO_CONSOLE_BAD_ID
) {
839 if (plugging_port0
) {
842 port
->id
= find_free_port_id(port
->vser
);
843 if (port
->id
== VIRTIO_CONSOLE_BAD_ID
) {
844 error_setg(errp
, "virtio-serial-bus: Maximum port limit for "
845 "this device reached");
851 max_nr_ports
= tswap32(port
->vser
->config
.max_nr_ports
);
852 if (port
->id
>= max_nr_ports
) {
853 error_setg(errp
, "virtio-serial-bus: Out-of-range port id specified, "
854 "max. allowed: %u", max_nr_ports
- 1);
858 vsc
->realize(dev
, &err
);
860 error_propagate(errp
, err
);
864 port
->elem
.out_num
= 0;
866 QTAILQ_INSERT_TAIL(&port
->vser
->ports
, port
, next
);
867 port
->ivq
= port
->vser
->ivqs
[port
->id
];
868 port
->ovq
= port
->vser
->ovqs
[port
->id
];
870 add_port(port
->vser
, port
->id
);
872 /* Send an update to the guest about this new port added */
873 virtio_notify_config(VIRTIO_DEVICE(port
->vser
));
876 static void virtser_port_device_unrealize(DeviceState
*dev
, Error
**errp
)
878 VirtIOSerialPort
*port
= VIRTIO_SERIAL_PORT(dev
);
879 VirtIOSerialPortClass
*vsc
= VIRTIO_SERIAL_PORT_GET_CLASS(dev
);
880 VirtIOSerial
*vser
= port
->vser
;
882 qemu_bh_delete(port
->bh
);
883 remove_port(port
->vser
, port
->id
);
885 QTAILQ_REMOVE(&vser
->ports
, port
, next
);
887 if (vsc
->unrealize
) {
888 vsc
->unrealize(dev
, errp
);
892 static void virtio_serial_device_realize(DeviceState
*dev
, Error
**errp
)
894 VirtIODevice
*vdev
= VIRTIO_DEVICE(dev
);
895 VirtIOSerial
*vser
= VIRTIO_SERIAL(dev
);
897 uint32_t i
, max_supported_ports
;
899 if (!vser
->serial
.max_virtserial_ports
) {
900 error_setg(errp
, "Maximum number of serial ports not specified");
904 /* Each port takes 2 queues, and one pair is for the control queue */
905 max_supported_ports
= VIRTIO_PCI_QUEUE_MAX
/ 2 - 1;
907 if (vser
->serial
.max_virtserial_ports
> max_supported_ports
) {
908 error_setg(errp
, "maximum ports supported: %u", max_supported_ports
);
912 virtio_init(vdev
, "virtio-serial", VIRTIO_ID_CONSOLE
,
913 sizeof(struct virtio_console_config
));
915 /* Spawn a new virtio-serial bus on which the ports will ride as devices */
916 qbus_create_inplace(&vser
->bus
, sizeof(vser
->bus
), TYPE_VIRTIO_SERIAL_BUS
,
917 dev
, vdev
->bus_name
);
918 bus
= BUS(&vser
->bus
);
919 bus
->allow_hotplug
= 1;
920 vser
->bus
.vser
= vser
;
921 QTAILQ_INIT(&vser
->ports
);
923 vser
->bus
.max_nr_ports
= vser
->serial
.max_virtserial_ports
;
924 vser
->ivqs
= g_malloc(vser
->serial
.max_virtserial_ports
925 * sizeof(VirtQueue
*));
926 vser
->ovqs
= g_malloc(vser
->serial
.max_virtserial_ports
927 * sizeof(VirtQueue
*));
929 /* Add a queue for host to guest transfers for port 0 (backward compat) */
930 vser
->ivqs
[0] = virtio_add_queue(vdev
, 128, handle_input
);
931 /* Add a queue for guest to host transfers for port 0 (backward compat) */
932 vser
->ovqs
[0] = virtio_add_queue(vdev
, 128, handle_output
);
934 /* TODO: host to guest notifications can get dropped
935 * if the queue fills up. Implement queueing in host,
936 * this might also make it possible to reduce the control
937 * queue size: as guest preposts buffers there,
938 * this will save 4Kbyte of guest memory per entry. */
940 /* control queue: host to guest */
941 vser
->c_ivq
= virtio_add_queue(vdev
, 32, control_in
);
942 /* control queue: guest to host */
943 vser
->c_ovq
= virtio_add_queue(vdev
, 32, control_out
);
945 for (i
= 1; i
< vser
->bus
.max_nr_ports
; i
++) {
946 /* Add a per-port queue for host to guest transfers */
947 vser
->ivqs
[i
] = virtio_add_queue(vdev
, 128, handle_input
);
948 /* Add a per-per queue for guest to host transfers */
949 vser
->ovqs
[i
] = virtio_add_queue(vdev
, 128, handle_output
);
952 vser
->config
.max_nr_ports
= tswap32(vser
->serial
.max_virtserial_ports
);
953 vser
->ports_map
= g_malloc0(((vser
->serial
.max_virtserial_ports
+ 31) / 32)
954 * sizeof(vser
->ports_map
[0]));
956 * Reserve location 0 for a console port for backward compat
957 * (old kernel, new qemu)
959 mark_port_added(vser
, 0);
961 vser
->post_load
= NULL
;
964 * Register for the savevm section with the virtio-console name
965 * to preserve backward compat
967 register_savevm(dev
, "virtio-console", -1, 3, virtio_serial_save
,
968 virtio_serial_load
, vser
);
971 static void virtio_serial_port_class_init(ObjectClass
*klass
, void *data
)
973 DeviceClass
*k
= DEVICE_CLASS(klass
);
975 set_bit(DEVICE_CATEGORY_INPUT
, k
->categories
);
976 k
->bus_type
= TYPE_VIRTIO_SERIAL_BUS
;
977 k
->realize
= virtser_port_device_realize
;
978 k
->unrealize
= virtser_port_device_unrealize
;
979 k
->unplug
= qdev_simple_unplug_cb
;
980 k
->props
= virtser_props
;
983 static const TypeInfo virtio_serial_port_type_info
= {
984 .name
= TYPE_VIRTIO_SERIAL_PORT
,
985 .parent
= TYPE_DEVICE
,
986 .instance_size
= sizeof(VirtIOSerialPort
),
988 .class_size
= sizeof(VirtIOSerialPortClass
),
989 .class_init
= virtio_serial_port_class_init
,
992 static void virtio_serial_device_unrealize(DeviceState
*dev
, Error
**errp
)
994 VirtIODevice
*vdev
= VIRTIO_DEVICE(dev
);
995 VirtIOSerial
*vser
= VIRTIO_SERIAL(dev
);
997 unregister_savevm(dev
, "virtio-console", vser
);
1001 g_free(vser
->ports_map
);
1002 if (vser
->post_load
) {
1003 g_free(vser
->post_load
->connected
);
1004 timer_del(vser
->post_load
->timer
);
1005 timer_free(vser
->post_load
->timer
);
1006 g_free(vser
->post_load
);
1008 virtio_cleanup(vdev
);
1011 static Property virtio_serial_properties
[] = {
1012 DEFINE_VIRTIO_SERIAL_PROPERTIES(VirtIOSerial
, serial
),
1013 DEFINE_PROP_END_OF_LIST(),
1016 static void virtio_serial_class_init(ObjectClass
*klass
, void *data
)
1018 DeviceClass
*dc
= DEVICE_CLASS(klass
);
1019 VirtioDeviceClass
*vdc
= VIRTIO_DEVICE_CLASS(klass
);
1021 dc
->props
= virtio_serial_properties
;
1022 set_bit(DEVICE_CATEGORY_INPUT
, dc
->categories
);
1023 vdc
->realize
= virtio_serial_device_realize
;
1024 vdc
->unrealize
= virtio_serial_device_unrealize
;
1025 vdc
->get_features
= get_features
;
1026 vdc
->get_config
= get_config
;
1027 vdc
->set_config
= set_config
;
1028 vdc
->set_status
= set_status
;
1029 vdc
->reset
= vser_reset
;
1032 static const TypeInfo virtio_device_info
= {
1033 .name
= TYPE_VIRTIO_SERIAL
,
1034 .parent
= TYPE_VIRTIO_DEVICE
,
1035 .instance_size
= sizeof(VirtIOSerial
),
1036 .class_init
= virtio_serial_class_init
,
1039 static void virtio_serial_register_types(void)
1041 type_register_static(&virtser_bus_info
);
1042 type_register_static(&virtio_serial_port_type_info
);
1043 type_register_static(&virtio_device_info
);
1046 type_init(virtio_serial_register_types
)