virtio-serial: Assert for virtio queue ready before virtqueue operations
[qemu.git] / hw / virtio-serial-bus.c
blob8e611c03e03f73a22f637425886556b3a993ee7a
1 /*
2 * A bus for connecting virtio serial and console ports
4 * Copyright (C) 2009, 2010 Red Hat, Inc.
6 * Author(s):
7 * Amit Shah <amit.shah@redhat.com>
9 * Some earlier parts are:
10 * Copyright IBM, Corp. 2008
11 * authored by
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.
18 #include "iov.h"
19 #include "monitor.h"
20 #include "qemu-queue.h"
21 #include "sysbus.h"
22 #include "virtio-serial.h"
24 /* The virtio-serial bus on top of which the ports will ride as devices */
25 struct VirtIOSerialBus {
26 BusState qbus;
28 /* This is the parent device that provides the bus for ports. */
29 VirtIOSerial *vser;
31 /* The maximum number of ports that can ride on top of this bus */
32 uint32_t max_nr_ports;
35 struct VirtIOSerial {
36 VirtIODevice vdev;
38 VirtQueue *c_ivq, *c_ovq;
39 /* Arrays of ivqs and ovqs: one per port */
40 VirtQueue **ivqs, **ovqs;
42 VirtIOSerialBus *bus;
44 QTAILQ_HEAD(, VirtIOSerialPort) ports;
46 /* bitmap for identifying active ports */
47 uint32_t *ports_map;
49 struct virtio_console_config config;
52 static VirtIOSerialPort *find_port_by_id(VirtIOSerial *vser, uint32_t id)
54 VirtIOSerialPort *port;
56 if (id == VIRTIO_CONSOLE_BAD_ID) {
57 return NULL;
60 QTAILQ_FOREACH(port, &vser->ports, next) {
61 if (port->id == id)
62 return port;
64 return NULL;
67 static VirtIOSerialPort *find_port_by_vq(VirtIOSerial *vser, VirtQueue *vq)
69 VirtIOSerialPort *port;
71 QTAILQ_FOREACH(port, &vser->ports, next) {
72 if (port->ivq == vq || port->ovq == vq)
73 return port;
75 return NULL;
78 static bool use_multiport(VirtIOSerial *vser)
80 return vser->vdev.guest_features & (1 << VIRTIO_CONSOLE_F_MULTIPORT);
83 static size_t write_to_port(VirtIOSerialPort *port,
84 const uint8_t *buf, size_t size)
86 VirtQueueElement elem;
87 VirtQueue *vq;
88 size_t offset;
90 vq = port->ivq;
91 if (!virtio_queue_ready(vq)) {
92 return 0;
95 offset = 0;
96 while (offset < size) {
97 size_t len;
99 if (!virtqueue_pop(vq, &elem)) {
100 break;
103 len = iov_from_buf(elem.in_sg, elem.in_num,
104 buf + offset, size - offset);
105 offset += len;
107 virtqueue_push(vq, &elem, len);
110 virtio_notify(&port->vser->vdev, vq);
111 return offset;
114 static void do_flush_queued_data(VirtIOSerialPort *port, VirtQueue *vq,
115 VirtIODevice *vdev, bool discard)
117 VirtQueueElement elem;
119 assert(port || discard);
120 assert(virtio_queue_ready(vq));
122 while ((discard || !port->throttled) && virtqueue_pop(vq, &elem)) {
123 uint8_t *buf;
124 size_t ret, buf_size;
126 if (!discard) {
127 buf_size = iov_size(elem.out_sg, elem.out_num);
128 buf = qemu_malloc(buf_size);
129 ret = iov_to_buf(elem.out_sg, elem.out_num, buf, 0, buf_size);
131 port->info->have_data(port, buf, ret);
132 qemu_free(buf);
134 virtqueue_push(vq, &elem, 0);
136 virtio_notify(vdev, vq);
139 static void flush_queued_data(VirtIOSerialPort *port, bool discard)
141 assert(port);
143 if (!virtio_queue_ready(port->ovq)) {
144 return;
146 do_flush_queued_data(port, port->ovq, &port->vser->vdev, discard);
149 static size_t send_control_msg(VirtIOSerialPort *port, void *buf, size_t len)
151 VirtQueueElement elem;
152 VirtQueue *vq;
153 struct virtio_console_control *cpkt;
155 vq = port->vser->c_ivq;
156 if (!virtio_queue_ready(vq)) {
157 return 0;
159 if (!virtqueue_pop(vq, &elem)) {
160 return 0;
163 cpkt = (struct virtio_console_control *)buf;
164 stl_p(&cpkt->id, port->id);
165 memcpy(elem.in_sg[0].iov_base, buf, len);
167 virtqueue_push(vq, &elem, len);
168 virtio_notify(&port->vser->vdev, vq);
169 return len;
172 static size_t send_control_event(VirtIOSerialPort *port, uint16_t event,
173 uint16_t value)
175 struct virtio_console_control cpkt;
177 stw_p(&cpkt.event, event);
178 stw_p(&cpkt.value, value);
180 return send_control_msg(port, &cpkt, sizeof(cpkt));
183 /* Functions for use inside qemu to open and read from/write to ports */
184 int virtio_serial_open(VirtIOSerialPort *port)
186 /* Don't allow opening an already-open port */
187 if (port->host_connected) {
188 return 0;
190 /* Send port open notification to the guest */
191 port->host_connected = true;
192 send_control_event(port, VIRTIO_CONSOLE_PORT_OPEN, 1);
194 return 0;
197 int virtio_serial_close(VirtIOSerialPort *port)
199 port->host_connected = false;
201 * If there's any data the guest sent which the app didn't
202 * consume, reset the throttling flag and discard the data.
204 port->throttled = false;
205 flush_queued_data(port, true);
207 send_control_event(port, VIRTIO_CONSOLE_PORT_OPEN, 0);
209 return 0;
212 /* Individual ports/apps call this function to write to the guest. */
213 ssize_t virtio_serial_write(VirtIOSerialPort *port, const uint8_t *buf,
214 size_t size)
216 if (!port || !port->host_connected || !port->guest_connected) {
217 return 0;
219 return write_to_port(port, buf, size);
223 * Readiness of the guest to accept data on a port.
224 * Returns max. data the guest can receive
226 size_t virtio_serial_guest_ready(VirtIOSerialPort *port)
228 VirtQueue *vq = port->ivq;
230 if (!virtio_queue_ready(vq) ||
231 !(port->vser->vdev.status & VIRTIO_CONFIG_S_DRIVER_OK) ||
232 virtio_queue_empty(vq)) {
233 return 0;
235 if (use_multiport(port->vser) && !port->guest_connected) {
236 return 0;
239 if (virtqueue_avail_bytes(vq, 4096, 0)) {
240 return 4096;
242 if (virtqueue_avail_bytes(vq, 1, 0)) {
243 return 1;
245 return 0;
248 void virtio_serial_throttle_port(VirtIOSerialPort *port, bool throttle)
250 if (!port) {
251 return;
254 port->throttled = throttle;
255 if (throttle) {
256 return;
259 flush_queued_data(port, false);
262 /* Guest wants to notify us of some event */
263 static void handle_control_message(VirtIOSerial *vser, void *buf, size_t len)
265 struct VirtIOSerialPort *port;
266 struct virtio_console_control cpkt, *gcpkt;
267 uint8_t *buffer;
268 size_t buffer_len;
270 gcpkt = buf;
272 if (len < sizeof(cpkt)) {
273 /* The guest sent an invalid control packet */
274 return;
277 cpkt.event = lduw_p(&gcpkt->event);
278 cpkt.value = lduw_p(&gcpkt->value);
280 port = find_port_by_id(vser, ldl_p(&gcpkt->id));
281 if (!port && cpkt.event != VIRTIO_CONSOLE_DEVICE_READY)
282 return;
284 switch(cpkt.event) {
285 case VIRTIO_CONSOLE_DEVICE_READY:
286 if (!cpkt.value) {
287 error_report("virtio-serial-bus: Guest failure in adding device %s\n",
288 vser->bus->qbus.name);
289 break;
292 * The device is up, we can now tell the device about all the
293 * ports we have here.
295 QTAILQ_FOREACH(port, &vser->ports, next) {
296 send_control_event(port, VIRTIO_CONSOLE_PORT_ADD, 1);
298 break;
300 case VIRTIO_CONSOLE_PORT_READY:
301 if (!cpkt.value) {
302 error_report("virtio-serial-bus: Guest failure in adding port %u for device %s\n",
303 port->id, vser->bus->qbus.name);
304 break;
307 * Now that we know the guest asked for the port name, we're
308 * sure the guest has initialised whatever state is necessary
309 * for this port. Now's a good time to let the guest know if
310 * this port is a console port so that the guest can hook it
311 * up to hvc.
313 if (port->is_console) {
314 send_control_event(port, VIRTIO_CONSOLE_CONSOLE_PORT, 1);
317 if (port->name) {
318 stw_p(&cpkt.event, VIRTIO_CONSOLE_PORT_NAME);
319 stw_p(&cpkt.value, 1);
321 buffer_len = sizeof(cpkt) + strlen(port->name) + 1;
322 buffer = qemu_malloc(buffer_len);
324 memcpy(buffer, &cpkt, sizeof(cpkt));
325 memcpy(buffer + sizeof(cpkt), port->name, strlen(port->name));
326 buffer[buffer_len - 1] = 0;
328 send_control_msg(port, buffer, buffer_len);
329 qemu_free(buffer);
332 if (port->host_connected) {
333 send_control_event(port, VIRTIO_CONSOLE_PORT_OPEN, 1);
337 * When the guest has asked us for this information it means
338 * the guest is all setup and has its virtqueues
339 * initialised. If some app is interested in knowing about
340 * this event, let it know.
342 if (port->info->guest_ready) {
343 port->info->guest_ready(port);
345 break;
347 case VIRTIO_CONSOLE_PORT_OPEN:
348 port->guest_connected = cpkt.value;
349 if (cpkt.value && port->info->guest_open) {
350 /* Send the guest opened notification if an app is interested */
351 port->info->guest_open(port);
354 if (!cpkt.value && port->info->guest_close) {
355 /* Send the guest closed notification if an app is interested */
356 port->info->guest_close(port);
358 break;
362 static void control_in(VirtIODevice *vdev, VirtQueue *vq)
366 static void control_out(VirtIODevice *vdev, VirtQueue *vq)
368 VirtQueueElement elem;
369 VirtIOSerial *vser;
370 uint8_t *buf;
371 size_t len;
373 vser = DO_UPCAST(VirtIOSerial, vdev, vdev);
375 len = 0;
376 buf = NULL;
377 while (virtqueue_pop(vq, &elem)) {
378 size_t cur_len, copied;
380 cur_len = iov_size(elem.out_sg, elem.out_num);
382 * Allocate a new buf only if we didn't have one previously or
383 * if the size of the buf differs
385 if (cur_len > len) {
386 qemu_free(buf);
388 buf = qemu_malloc(cur_len);
389 len = cur_len;
391 copied = iov_to_buf(elem.out_sg, elem.out_num, buf, 0, len);
393 handle_control_message(vser, buf, copied);
394 virtqueue_push(vq, &elem, 0);
396 qemu_free(buf);
397 virtio_notify(vdev, vq);
400 /* Guest wrote something to some port. */
401 static void handle_output(VirtIODevice *vdev, VirtQueue *vq)
403 VirtIOSerial *vser;
404 VirtIOSerialPort *port;
405 bool discard;
407 vser = DO_UPCAST(VirtIOSerial, vdev, vdev);
408 port = find_port_by_vq(vser, vq);
410 discard = false;
411 if (!port || !port->host_connected || !port->info->have_data) {
412 discard = true;
415 if (!discard && port->throttled) {
416 return;
419 do_flush_queued_data(port, vq, vdev, discard);
422 static void handle_input(VirtIODevice *vdev, VirtQueue *vq)
426 static uint32_t get_features(VirtIODevice *vdev, uint32_t features)
428 VirtIOSerial *vser;
430 vser = DO_UPCAST(VirtIOSerial, vdev, vdev);
432 if (vser->bus->max_nr_ports > 1) {
433 features |= (1 << VIRTIO_CONSOLE_F_MULTIPORT);
435 return features;
438 /* Guest requested config info */
439 static void get_config(VirtIODevice *vdev, uint8_t *config_data)
441 VirtIOSerial *vser;
443 vser = DO_UPCAST(VirtIOSerial, vdev, vdev);
444 memcpy(config_data, &vser->config, sizeof(struct virtio_console_config));
447 static void set_config(VirtIODevice *vdev, const uint8_t *config_data)
449 struct virtio_console_config config;
451 memcpy(&config, config_data, sizeof(config));
454 static void virtio_serial_save(QEMUFile *f, void *opaque)
456 VirtIOSerial *s = opaque;
457 VirtIOSerialPort *port;
458 uint32_t nr_active_ports;
459 unsigned int i;
461 /* The virtio device */
462 virtio_save(&s->vdev, f);
464 /* The config space */
465 qemu_put_be16s(f, &s->config.cols);
466 qemu_put_be16s(f, &s->config.rows);
468 qemu_put_be32s(f, &s->config.max_nr_ports);
470 /* The ports map */
472 for (i = 0; i < (s->config.max_nr_ports + 31) / 32; i++) {
473 qemu_put_be32s(f, &s->ports_map[i]);
476 /* Ports */
478 nr_active_ports = 0;
479 QTAILQ_FOREACH(port, &s->ports, next) {
480 nr_active_ports++;
483 qemu_put_be32s(f, &nr_active_ports);
486 * Items in struct VirtIOSerialPort.
488 QTAILQ_FOREACH(port, &s->ports, next) {
489 qemu_put_be32s(f, &port->id);
490 qemu_put_byte(f, port->guest_connected);
491 qemu_put_byte(f, port->host_connected);
495 static int virtio_serial_load(QEMUFile *f, void *opaque, int version_id)
497 VirtIOSerial *s = opaque;
498 VirtIOSerialPort *port;
499 uint32_t max_nr_ports, nr_active_ports, ports_map;
500 unsigned int i;
502 if (version_id > 2) {
503 return -EINVAL;
506 /* The virtio device */
507 virtio_load(&s->vdev, f);
509 if (version_id < 2) {
510 return 0;
513 /* The config space */
514 qemu_get_be16s(f, &s->config.cols);
515 qemu_get_be16s(f, &s->config.rows);
517 qemu_get_be32s(f, &max_nr_ports);
518 if (max_nr_ports > s->config.max_nr_ports) {
519 /* Source could have had more ports than us. Fail migration. */
520 return -EINVAL;
523 for (i = 0; i < (max_nr_ports + 31) / 32; i++) {
524 qemu_get_be32s(f, &ports_map);
526 if (ports_map != s->ports_map[i]) {
528 * Ports active on source and destination don't
529 * match. Fail migration.
531 return -EINVAL;
535 qemu_get_be32s(f, &nr_active_ports);
537 /* Items in struct VirtIOSerialPort */
538 for (i = 0; i < nr_active_ports; i++) {
539 uint32_t id;
540 bool host_connected;
542 id = qemu_get_be32(f);
543 port = find_port_by_id(s, id);
545 port->guest_connected = qemu_get_byte(f);
546 host_connected = qemu_get_byte(f);
547 if (host_connected != port->host_connected) {
549 * We have to let the guest know of the host connection
550 * status change
552 send_control_event(port, VIRTIO_CONSOLE_PORT_OPEN,
553 port->host_connected);
556 return 0;
559 static void virtser_bus_dev_print(Monitor *mon, DeviceState *qdev, int indent);
561 static struct BusInfo virtser_bus_info = {
562 .name = "virtio-serial-bus",
563 .size = sizeof(VirtIOSerialBus),
564 .print_dev = virtser_bus_dev_print,
567 static VirtIOSerialBus *virtser_bus_new(DeviceState *dev)
569 VirtIOSerialBus *bus;
571 bus = FROM_QBUS(VirtIOSerialBus, qbus_create(&virtser_bus_info, dev, NULL));
572 bus->qbus.allow_hotplug = 1;
574 return bus;
577 static void virtser_bus_dev_print(Monitor *mon, DeviceState *qdev, int indent)
579 VirtIOSerialDevice *dev = DO_UPCAST(VirtIOSerialDevice, qdev, qdev);
580 VirtIOSerialPort *port = DO_UPCAST(VirtIOSerialPort, dev, &dev->qdev);
582 monitor_printf(mon, "%*s dev-prop-int: id: %u\n",
583 indent, "", port->id);
584 monitor_printf(mon, "%*s dev-prop-int: guest_connected: %d\n",
585 indent, "", port->guest_connected);
586 monitor_printf(mon, "%*s dev-prop-int: host_connected: %d\n",
587 indent, "", port->host_connected);
588 monitor_printf(mon, "%*s dev-prop-int: throttled: %d\n",
589 indent, "", port->throttled);
592 /* This function is only used if a port id is not provided by the user */
593 static uint32_t find_free_port_id(VirtIOSerial *vser)
595 unsigned int i;
597 for (i = 0; i < (vser->config.max_nr_ports + 31) / 32; i++) {
598 uint32_t map, bit;
600 map = vser->ports_map[i];
601 bit = ffs(~map);
602 if (bit) {
603 return (bit - 1) + i * 32;
606 return VIRTIO_CONSOLE_BAD_ID;
609 static void mark_port_added(VirtIOSerial *vser, uint32_t port_id)
611 unsigned int i;
613 i = port_id / 32;
614 vser->ports_map[i] |= 1U << (port_id % 32);
617 static void add_port(VirtIOSerial *vser, uint32_t port_id)
619 mark_port_added(vser, port_id);
621 send_control_event(find_port_by_id(vser, port_id),
622 VIRTIO_CONSOLE_PORT_ADD, 1);
625 static void remove_port(VirtIOSerial *vser, uint32_t port_id)
627 VirtIOSerialPort *port;
628 unsigned int i;
630 i = port_id / 32;
631 vser->ports_map[i] &= ~(1U << (port_id % 32));
633 port = find_port_by_id(vser, port_id);
634 /* Flush out any unconsumed buffers first */
635 flush_queued_data(port, true);
637 send_control_event(port, VIRTIO_CONSOLE_PORT_REMOVE, 1);
640 static int virtser_port_qdev_init(DeviceState *qdev, DeviceInfo *base)
642 VirtIOSerialDevice *dev = DO_UPCAST(VirtIOSerialDevice, qdev, qdev);
643 VirtIOSerialPortInfo *info = DO_UPCAST(VirtIOSerialPortInfo, qdev, base);
644 VirtIOSerialPort *port = DO_UPCAST(VirtIOSerialPort, dev, &dev->qdev);
645 VirtIOSerialBus *bus = DO_UPCAST(VirtIOSerialBus, qbus, qdev->parent_bus);
646 int ret;
647 bool plugging_port0;
649 port->vser = bus->vser;
652 * Is the first console port we're seeing? If so, put it up at
653 * location 0. This is done for backward compatibility (old
654 * kernel, new qemu).
656 plugging_port0 = port->is_console && !find_port_by_id(port->vser, 0);
658 if (find_port_by_id(port->vser, port->id)) {
659 error_report("virtio-serial-bus: A port already exists at id %u\n",
660 port->id);
661 return -1;
664 if (port->id == VIRTIO_CONSOLE_BAD_ID) {
665 if (plugging_port0) {
666 port->id = 0;
667 } else {
668 port->id = find_free_port_id(port->vser);
669 if (port->id == VIRTIO_CONSOLE_BAD_ID) {
670 error_report("virtio-serial-bus: Maximum port limit for this device reached\n");
671 return -1;
676 if (port->id >= port->vser->config.max_nr_ports) {
677 error_report("virtio-serial-bus: Out-of-range port id specified, max. allowed: %u\n",
678 port->vser->config.max_nr_ports - 1);
679 return -1;
682 dev->info = info;
683 ret = info->init(dev);
684 if (ret) {
685 return ret;
688 if (!use_multiport(port->vser)) {
690 * Allow writes to guest in this case; we have no way of
691 * knowing if a guest port is connected.
693 port->guest_connected = true;
696 QTAILQ_INSERT_TAIL(&port->vser->ports, port, next);
697 port->ivq = port->vser->ivqs[port->id];
698 port->ovq = port->vser->ovqs[port->id];
700 add_port(port->vser, port->id);
702 /* Send an update to the guest about this new port added */
703 virtio_notify_config(&port->vser->vdev);
705 return ret;
708 static int virtser_port_qdev_exit(DeviceState *qdev)
710 VirtIOSerialDevice *dev = DO_UPCAST(VirtIOSerialDevice, qdev, qdev);
711 VirtIOSerialPort *port = DO_UPCAST(VirtIOSerialPort, dev, &dev->qdev);
712 VirtIOSerial *vser = port->vser;
714 remove_port(port->vser, port->id);
716 QTAILQ_REMOVE(&vser->ports, port, next);
718 if (port->info->exit)
719 port->info->exit(dev);
721 return 0;
724 void virtio_serial_port_qdev_register(VirtIOSerialPortInfo *info)
726 info->qdev.init = virtser_port_qdev_init;
727 info->qdev.bus_info = &virtser_bus_info;
728 info->qdev.exit = virtser_port_qdev_exit;
729 info->qdev.unplug = qdev_simple_unplug_cb;
730 qdev_register(&info->qdev);
733 VirtIODevice *virtio_serial_init(DeviceState *dev, uint32_t max_nr_ports)
735 VirtIOSerial *vser;
736 VirtIODevice *vdev;
737 uint32_t i;
739 if (!max_nr_ports)
740 return NULL;
742 vdev = virtio_common_init("virtio-serial", VIRTIO_ID_CONSOLE,
743 sizeof(struct virtio_console_config),
744 sizeof(VirtIOSerial));
746 vser = DO_UPCAST(VirtIOSerial, vdev, vdev);
748 /* Spawn a new virtio-serial bus on which the ports will ride as devices */
749 vser->bus = virtser_bus_new(dev);
750 vser->bus->vser = vser;
751 QTAILQ_INIT(&vser->ports);
753 vser->bus->max_nr_ports = max_nr_ports;
754 vser->ivqs = qemu_malloc(max_nr_ports * sizeof(VirtQueue *));
755 vser->ovqs = qemu_malloc(max_nr_ports * sizeof(VirtQueue *));
757 /* Add a queue for host to guest transfers for port 0 (backward compat) */
758 vser->ivqs[0] = virtio_add_queue(vdev, 128, handle_input);
759 /* Add a queue for guest to host transfers for port 0 (backward compat) */
760 vser->ovqs[0] = virtio_add_queue(vdev, 128, handle_output);
762 /* control queue: host to guest */
763 vser->c_ivq = virtio_add_queue(vdev, 16, control_in);
764 /* control queue: guest to host */
765 vser->c_ovq = virtio_add_queue(vdev, 16, control_out);
767 for (i = 1; i < vser->bus->max_nr_ports; i++) {
768 /* Add a per-port queue for host to guest transfers */
769 vser->ivqs[i] = virtio_add_queue(vdev, 128, handle_input);
770 /* Add a per-per queue for guest to host transfers */
771 vser->ovqs[i] = virtio_add_queue(vdev, 128, handle_output);
774 vser->config.max_nr_ports = max_nr_ports;
775 vser->ports_map = qemu_mallocz(((max_nr_ports + 31) / 32)
776 * sizeof(vser->ports_map[0]));
778 * Reserve location 0 for a console port for backward compat
779 * (old kernel, new qemu)
781 mark_port_added(vser, 0);
783 vser->vdev.get_features = get_features;
784 vser->vdev.get_config = get_config;
785 vser->vdev.set_config = set_config;
788 * Register for the savevm section with the virtio-console name
789 * to preserve backward compat
791 register_savevm(dev, "virtio-console", -1, 2, virtio_serial_save,
792 virtio_serial_load, vser);
794 return vdev;