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.
19 #include "qemu-queue.h"
21 #include "virtio-serial.h"
23 /* The virtio-serial bus on top of which the ports will ride as devices */
24 struct VirtIOSerialBus
{
27 /* This is the parent device that provides the bus for ports. */
30 /* The maximum number of ports that can ride on top of this bus */
31 uint32_t max_nr_ports
;
37 VirtQueue
*c_ivq
, *c_ovq
;
38 /* Arrays of ivqs and ovqs: one per port */
39 VirtQueue
**ivqs
, **ovqs
;
43 QTAILQ_HEAD(, VirtIOSerialPort
) ports
;
45 /* bitmap for identifying active ports */
48 struct virtio_console_config config
;
51 static VirtIOSerialPort
*find_port_by_id(VirtIOSerial
*vser
, uint32_t id
)
53 VirtIOSerialPort
*port
;
55 if (id
== VIRTIO_CONSOLE_BAD_ID
) {
59 QTAILQ_FOREACH(port
, &vser
->ports
, next
) {
66 static VirtIOSerialPort
*find_port_by_vq(VirtIOSerial
*vser
, VirtQueue
*vq
)
68 VirtIOSerialPort
*port
;
70 QTAILQ_FOREACH(port
, &vser
->ports
, next
) {
71 if (port
->ivq
== vq
|| port
->ovq
== vq
)
77 static bool use_multiport(VirtIOSerial
*vser
)
79 return vser
->vdev
.guest_features
& (1 << VIRTIO_CONSOLE_F_MULTIPORT
);
82 static size_t write_to_port(VirtIOSerialPort
*port
,
83 const uint8_t *buf
, size_t size
)
85 VirtQueueElement elem
;
91 if (!virtio_queue_ready(vq
)) {
95 while (offset
< size
) {
98 if (!virtqueue_pop(vq
, &elem
)) {
102 for (i
= 0; offset
< size
&& i
< elem
.in_num
; i
++) {
103 len
= MIN(elem
.in_sg
[i
].iov_len
, size
- offset
);
105 memcpy(elem
.in_sg
[i
].iov_base
, buf
+ offset
, len
);
108 virtqueue_push(vq
, &elem
, len
);
111 virtio_notify(&port
->vser
->vdev
, vq
);
115 static size_t send_control_msg(VirtIOSerialPort
*port
, void *buf
, size_t len
)
117 VirtQueueElement elem
;
119 struct virtio_console_control
*cpkt
;
121 vq
= port
->vser
->c_ivq
;
122 if (!virtio_queue_ready(vq
)) {
125 if (!virtqueue_pop(vq
, &elem
)) {
129 cpkt
= (struct virtio_console_control
*)buf
;
130 stl_p(&cpkt
->id
, port
->id
);
131 memcpy(elem
.in_sg
[0].iov_base
, buf
, len
);
133 virtqueue_push(vq
, &elem
, len
);
134 virtio_notify(&port
->vser
->vdev
, vq
);
138 static size_t send_control_event(VirtIOSerialPort
*port
, uint16_t event
,
141 struct virtio_console_control cpkt
;
143 stw_p(&cpkt
.event
, event
);
144 stw_p(&cpkt
.value
, value
);
146 return send_control_msg(port
, &cpkt
, sizeof(cpkt
));
149 /* Functions for use inside qemu to open and read from/write to ports */
150 int virtio_serial_open(VirtIOSerialPort
*port
)
152 /* Don't allow opening an already-open port */
153 if (port
->host_connected
) {
156 /* Send port open notification to the guest */
157 port
->host_connected
= true;
158 send_control_event(port
, VIRTIO_CONSOLE_PORT_OPEN
, 1);
163 int virtio_serial_close(VirtIOSerialPort
*port
)
165 port
->host_connected
= false;
166 send_control_event(port
, VIRTIO_CONSOLE_PORT_OPEN
, 0);
171 /* Individual ports/apps call this function to write to the guest. */
172 ssize_t
virtio_serial_write(VirtIOSerialPort
*port
, const uint8_t *buf
,
175 if (!port
|| !port
->host_connected
|| !port
->guest_connected
) {
178 return write_to_port(port
, buf
, size
);
182 * Readiness of the guest to accept data on a port.
183 * Returns max. data the guest can receive
185 size_t virtio_serial_guest_ready(VirtIOSerialPort
*port
)
187 VirtQueue
*vq
= port
->ivq
;
189 if (!virtio_queue_ready(vq
) ||
190 !(port
->vser
->vdev
.status
& VIRTIO_CONFIG_S_DRIVER_OK
) ||
191 virtio_queue_empty(vq
)) {
194 if (use_multiport(port
->vser
) && !port
->guest_connected
) {
198 if (virtqueue_avail_bytes(vq
, 4096, 0)) {
201 if (virtqueue_avail_bytes(vq
, 1, 0)) {
207 /* Guest wants to notify us of some event */
208 static void handle_control_message(VirtIOSerial
*vser
, void *buf
)
210 struct VirtIOSerialPort
*port
;
211 struct virtio_console_control cpkt
, *gcpkt
;
217 cpkt
.event
= lduw_p(&gcpkt
->event
);
218 cpkt
.value
= lduw_p(&gcpkt
->value
);
220 port
= find_port_by_id(vser
, ldl_p(&gcpkt
->id
));
221 if (!port
&& cpkt
.event
!= VIRTIO_CONSOLE_DEVICE_READY
)
225 case VIRTIO_CONSOLE_DEVICE_READY
:
227 * The device is up, we can now tell the device about all the
228 * ports we have here.
230 QTAILQ_FOREACH(port
, &vser
->ports
, next
) {
231 send_control_event(port
, VIRTIO_CONSOLE_PORT_ADD
, 1);
235 case VIRTIO_CONSOLE_PORT_READY
:
237 * Now that we know the guest asked for the port name, we're
238 * sure the guest has initialised whatever state is necessary
239 * for this port. Now's a good time to let the guest know if
240 * this port is a console port so that the guest can hook it
243 if (port
->is_console
) {
244 send_control_event(port
, VIRTIO_CONSOLE_CONSOLE_PORT
, 1);
248 stw_p(&cpkt
.event
, VIRTIO_CONSOLE_PORT_NAME
);
249 stw_p(&cpkt
.value
, 1);
251 buffer_len
= sizeof(cpkt
) + strlen(port
->name
) + 1;
252 buffer
= qemu_malloc(buffer_len
);
254 memcpy(buffer
, &cpkt
, sizeof(cpkt
));
255 memcpy(buffer
+ sizeof(cpkt
), port
->name
, strlen(port
->name
));
256 buffer
[buffer_len
- 1] = 0;
258 send_control_msg(port
, buffer
, buffer_len
);
262 if (port
->host_connected
) {
263 send_control_event(port
, VIRTIO_CONSOLE_PORT_OPEN
, 1);
267 * When the guest has asked us for this information it means
268 * the guest is all setup and has its virtqueues
269 * initialised. If some app is interested in knowing about
270 * this event, let it know.
272 if (port
->info
->guest_ready
) {
273 port
->info
->guest_ready(port
);
277 case VIRTIO_CONSOLE_PORT_OPEN
:
278 port
->guest_connected
= cpkt
.value
;
279 if (cpkt
.value
&& port
->info
->guest_open
) {
280 /* Send the guest opened notification if an app is interested */
281 port
->info
->guest_open(port
);
284 if (!cpkt
.value
&& port
->info
->guest_close
) {
285 /* Send the guest closed notification if an app is interested */
286 port
->info
->guest_close(port
);
292 static void control_in(VirtIODevice
*vdev
, VirtQueue
*vq
)
296 static void control_out(VirtIODevice
*vdev
, VirtQueue
*vq
)
298 VirtQueueElement elem
;
301 vser
= DO_UPCAST(VirtIOSerial
, vdev
, vdev
);
303 while (virtqueue_pop(vq
, &elem
)) {
304 handle_control_message(vser
, elem
.out_sg
[0].iov_base
);
305 virtqueue_push(vq
, &elem
, elem
.out_sg
[0].iov_len
);
307 virtio_notify(vdev
, vq
);
310 /* Guest wrote something to some port. */
311 static void handle_output(VirtIODevice
*vdev
, VirtQueue
*vq
)
314 VirtQueueElement elem
;
316 vser
= DO_UPCAST(VirtIOSerial
, vdev
, vdev
);
318 while (virtqueue_pop(vq
, &elem
)) {
319 VirtIOSerialPort
*port
;
322 port
= find_port_by_vq(vser
, vq
);
329 * A port may not have any handler registered for consuming the
330 * data that the guest sends or it may not have a chardev associated
331 * with it. Just ignore the data in that case.
333 if (!port
->info
->have_data
) {
338 /* The guest always sends only one sg */
339 ret
= port
->info
->have_data(port
, elem
.out_sg
[0].iov_base
,
340 elem
.out_sg
[0].iov_len
);
343 virtqueue_push(vq
, &elem
, ret
);
345 virtio_notify(vdev
, vq
);
348 static void handle_input(VirtIODevice
*vdev
, VirtQueue
*vq
)
352 static uint32_t get_features(VirtIODevice
*vdev
, uint32_t features
)
356 vser
= DO_UPCAST(VirtIOSerial
, vdev
, vdev
);
358 if (vser
->bus
->max_nr_ports
> 1) {
359 features
|= (1 << VIRTIO_CONSOLE_F_MULTIPORT
);
364 /* Guest requested config info */
365 static void get_config(VirtIODevice
*vdev
, uint8_t *config_data
)
369 vser
= DO_UPCAST(VirtIOSerial
, vdev
, vdev
);
370 memcpy(config_data
, &vser
->config
, sizeof(struct virtio_console_config
));
373 static void set_config(VirtIODevice
*vdev
, const uint8_t *config_data
)
375 struct virtio_console_config config
;
377 memcpy(&config
, config_data
, sizeof(config
));
380 static void virtio_serial_save(QEMUFile
*f
, void *opaque
)
382 VirtIOSerial
*s
= opaque
;
383 VirtIOSerialPort
*port
;
384 uint32_t nr_active_ports
;
387 /* The virtio device */
388 virtio_save(&s
->vdev
, f
);
390 /* The config space */
391 qemu_put_be16s(f
, &s
->config
.cols
);
392 qemu_put_be16s(f
, &s
->config
.rows
);
394 qemu_put_be32s(f
, &s
->config
.max_nr_ports
);
398 for (i
= 0; i
< (s
->config
.max_nr_ports
+ 31) / 32; i
++) {
399 qemu_put_be32s(f
, &s
->ports_map
[i
]);
405 QTAILQ_FOREACH(port
, &s
->ports
, next
) {
409 qemu_put_be32s(f
, &nr_active_ports
);
412 * Items in struct VirtIOSerialPort.
414 QTAILQ_FOREACH(port
, &s
->ports
, next
) {
415 qemu_put_be32s(f
, &port
->id
);
416 qemu_put_byte(f
, port
->guest_connected
);
417 qemu_put_byte(f
, port
->host_connected
);
421 static int virtio_serial_load(QEMUFile
*f
, void *opaque
, int version_id
)
423 VirtIOSerial
*s
= opaque
;
424 VirtIOSerialPort
*port
;
425 size_t ports_map_size
;
426 uint32_t max_nr_ports
, nr_active_ports
, *ports_map
;
429 if (version_id
> 2) {
433 /* The virtio device */
434 virtio_load(&s
->vdev
, f
);
436 if (version_id
< 2) {
440 /* The config space */
441 qemu_get_be16s(f
, &s
->config
.cols
);
442 qemu_get_be16s(f
, &s
->config
.rows
);
444 qemu_get_be32s(f
, &max_nr_ports
);
445 if (max_nr_ports
> s
->config
.max_nr_ports
) {
446 /* Source could have had more ports than us. Fail migration. */
450 ports_map_size
= sizeof(uint32_t) * (max_nr_ports
+ 31) / 32;
451 ports_map
= qemu_malloc(ports_map_size
);
453 for (i
= 0; i
< (max_nr_ports
+ 31) / 32; i
++) {
454 qemu_get_be32s(f
, &ports_map
[i
]);
456 if (ports_map
[i
] != s
->ports_map
[i
]) {
458 * Ports active on source and destination don't
459 * match. Fail migration.
461 qemu_free(ports_map
);
465 qemu_free(ports_map
);
467 qemu_get_be32s(f
, &nr_active_ports
);
469 /* Items in struct VirtIOSerialPort */
470 for (i
= 0; i
< nr_active_ports
; i
++) {
474 id
= qemu_get_be32(f
);
475 port
= find_port_by_id(s
, id
);
477 port
->guest_connected
= qemu_get_byte(f
);
478 host_connected
= qemu_get_byte(f
);
479 if (host_connected
!= port
->host_connected
) {
481 * We have to let the guest know of the host connection
484 send_control_event(port
, VIRTIO_CONSOLE_PORT_OPEN
,
485 port
->host_connected
);
491 static void virtser_bus_dev_print(Monitor
*mon
, DeviceState
*qdev
, int indent
);
493 static struct BusInfo virtser_bus_info
= {
494 .name
= "virtio-serial-bus",
495 .size
= sizeof(VirtIOSerialBus
),
496 .print_dev
= virtser_bus_dev_print
,
499 static VirtIOSerialBus
*virtser_bus_new(DeviceState
*dev
)
501 VirtIOSerialBus
*bus
;
503 bus
= FROM_QBUS(VirtIOSerialBus
, qbus_create(&virtser_bus_info
, dev
, NULL
));
504 bus
->qbus
.allow_hotplug
= 1;
509 static void virtser_bus_dev_print(Monitor
*mon
, DeviceState
*qdev
, int indent
)
511 VirtIOSerialDevice
*dev
= DO_UPCAST(VirtIOSerialDevice
, qdev
, qdev
);
512 VirtIOSerialPort
*port
= DO_UPCAST(VirtIOSerialPort
, dev
, &dev
->qdev
);
514 monitor_printf(mon
, "%*s dev-prop-int: id: %u\n",
515 indent
, "", port
->id
);
516 monitor_printf(mon
, "%*s dev-prop-int: guest_connected: %d\n",
517 indent
, "", port
->guest_connected
);
518 monitor_printf(mon
, "%*s dev-prop-int: host_connected: %d\n",
519 indent
, "", port
->host_connected
);
522 /* This function is only used if a port id is not provided by the user */
523 static uint32_t find_free_port_id(VirtIOSerial
*vser
)
527 for (i
= 0; i
< (vser
->config
.max_nr_ports
+ 31) / 32; i
++) {
530 map
= vser
->ports_map
[i
];
533 return (bit
- 1) + i
* 32;
536 return VIRTIO_CONSOLE_BAD_ID
;
539 static void mark_port_added(VirtIOSerial
*vser
, uint32_t port_id
)
544 vser
->ports_map
[i
] |= 1U << (port_id
% 32);
547 static void add_port(VirtIOSerial
*vser
, uint32_t port_id
)
549 mark_port_added(vser
, port_id
);
551 send_control_event(find_port_by_id(vser
, port_id
),
552 VIRTIO_CONSOLE_PORT_ADD
, 1);
555 static void remove_port(VirtIOSerial
*vser
, uint32_t port_id
)
560 vser
->ports_map
[i
] &= ~(1U << (port_id
% 32));
562 send_control_event(find_port_by_id(vser
, port_id
),
563 VIRTIO_CONSOLE_PORT_REMOVE
, 1);
566 static int virtser_port_qdev_init(DeviceState
*qdev
, DeviceInfo
*base
)
568 VirtIOSerialDevice
*dev
= DO_UPCAST(VirtIOSerialDevice
, qdev
, qdev
);
569 VirtIOSerialPortInfo
*info
= DO_UPCAST(VirtIOSerialPortInfo
, qdev
, base
);
570 VirtIOSerialPort
*port
= DO_UPCAST(VirtIOSerialPort
, dev
, &dev
->qdev
);
571 VirtIOSerialBus
*bus
= DO_UPCAST(VirtIOSerialBus
, qbus
, qdev
->parent_bus
);
575 port
->vser
= bus
->vser
;
578 * Is the first console port we're seeing? If so, put it up at
579 * location 0. This is done for backward compatibility (old
582 plugging_port0
= port
->is_console
&& !find_port_by_id(port
->vser
, 0);
584 if (find_port_by_id(port
->vser
, port
->id
)) {
585 error_report("virtio-serial-bus: A port already exists at id %u\n",
590 if (port
->id
== VIRTIO_CONSOLE_BAD_ID
) {
591 if (plugging_port0
) {
594 port
->id
= find_free_port_id(port
->vser
);
595 if (port
->id
== VIRTIO_CONSOLE_BAD_ID
) {
596 error_report("virtio-serial-bus: Maximum port limit for this device reached\n");
602 if (port
->id
>= port
->vser
->config
.max_nr_ports
) {
603 error_report("virtio-serial-bus: Out-of-range port id specified, max. allowed: %u\n",
604 port
->vser
->config
.max_nr_ports
- 1);
609 ret
= info
->init(dev
);
614 if (!use_multiport(port
->vser
)) {
616 * Allow writes to guest in this case; we have no way of
617 * knowing if a guest port is connected.
619 port
->guest_connected
= true;
622 QTAILQ_INSERT_TAIL(&port
->vser
->ports
, port
, next
);
623 port
->ivq
= port
->vser
->ivqs
[port
->id
];
624 port
->ovq
= port
->vser
->ovqs
[port
->id
];
626 add_port(port
->vser
, port
->id
);
628 /* Send an update to the guest about this new port added */
629 virtio_notify_config(&port
->vser
->vdev
);
634 static int virtser_port_qdev_exit(DeviceState
*qdev
)
636 VirtIOSerialDevice
*dev
= DO_UPCAST(VirtIOSerialDevice
, qdev
, qdev
);
637 VirtIOSerialPort
*port
= DO_UPCAST(VirtIOSerialPort
, dev
, &dev
->qdev
);
638 VirtIOSerial
*vser
= port
->vser
;
640 remove_port(port
->vser
, port
->id
);
642 QTAILQ_REMOVE(&vser
->ports
, port
, next
);
644 if (port
->info
->exit
)
645 port
->info
->exit(dev
);
650 void virtio_serial_port_qdev_register(VirtIOSerialPortInfo
*info
)
652 info
->qdev
.init
= virtser_port_qdev_init
;
653 info
->qdev
.bus_info
= &virtser_bus_info
;
654 info
->qdev
.exit
= virtser_port_qdev_exit
;
655 info
->qdev
.unplug
= qdev_simple_unplug_cb
;
656 qdev_register(&info
->qdev
);
659 VirtIODevice
*virtio_serial_init(DeviceState
*dev
, uint32_t max_nr_ports
)
668 vdev
= virtio_common_init("virtio-serial", VIRTIO_ID_CONSOLE
,
669 sizeof(struct virtio_console_config
),
670 sizeof(VirtIOSerial
));
672 vser
= DO_UPCAST(VirtIOSerial
, vdev
, vdev
);
674 /* Spawn a new virtio-serial bus on which the ports will ride as devices */
675 vser
->bus
= virtser_bus_new(dev
);
676 vser
->bus
->vser
= vser
;
677 QTAILQ_INIT(&vser
->ports
);
679 vser
->bus
->max_nr_ports
= max_nr_ports
;
680 vser
->ivqs
= qemu_malloc(max_nr_ports
* sizeof(VirtQueue
*));
681 vser
->ovqs
= qemu_malloc(max_nr_ports
* sizeof(VirtQueue
*));
683 /* Add a queue for host to guest transfers for port 0 (backward compat) */
684 vser
->ivqs
[0] = virtio_add_queue(vdev
, 128, handle_input
);
685 /* Add a queue for guest to host transfers for port 0 (backward compat) */
686 vser
->ovqs
[0] = virtio_add_queue(vdev
, 128, handle_output
);
688 /* control queue: host to guest */
689 vser
->c_ivq
= virtio_add_queue(vdev
, 16, control_in
);
690 /* control queue: guest to host */
691 vser
->c_ovq
= virtio_add_queue(vdev
, 16, control_out
);
693 for (i
= 1; i
< vser
->bus
->max_nr_ports
; i
++) {
694 /* Add a per-port queue for host to guest transfers */
695 vser
->ivqs
[i
] = virtio_add_queue(vdev
, 128, handle_input
);
696 /* Add a per-per queue for guest to host transfers */
697 vser
->ovqs
[i
] = virtio_add_queue(vdev
, 128, handle_output
);
700 vser
->config
.max_nr_ports
= max_nr_ports
;
701 vser
->ports_map
= qemu_mallocz((max_nr_ports
+ 31) / 32);
703 * Reserve location 0 for a console port for backward compat
704 * (old kernel, new qemu)
706 mark_port_added(vser
, 0);
708 vser
->vdev
.get_features
= get_features
;
709 vser
->vdev
.get_config
= get_config
;
710 vser
->vdev
.set_config
= set_config
;
713 * Register for the savevm section with the virtio-console name
714 * to preserve backward compat
716 register_savevm("virtio-console", -1, 2, virtio_serial_save
,
717 virtio_serial_load
, vser
);