savevm: Add DeviceState param
[qemu/kraxel.git] / hw / virtio-serial-bus.c
blob26d5841154f85074d7b66484986c4f82633acdd2
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);
121 while ((discard || !port->throttled) && virtqueue_pop(vq, &elem)) {
122 uint8_t *buf;
123 size_t ret, buf_size;
125 if (!discard) {
126 buf_size = iov_size(elem.out_sg, elem.out_num);
127 buf = qemu_malloc(buf_size);
128 ret = iov_to_buf(elem.out_sg, elem.out_num, buf, 0, buf_size);
130 port->info->have_data(port, buf, ret);
131 qemu_free(buf);
133 virtqueue_push(vq, &elem, 0);
135 virtio_notify(vdev, vq);
138 static void flush_queued_data(VirtIOSerialPort *port, bool discard)
140 assert(port);
142 do_flush_queued_data(port, port->ovq, &port->vser->vdev, discard);
145 static size_t send_control_msg(VirtIOSerialPort *port, void *buf, size_t len)
147 VirtQueueElement elem;
148 VirtQueue *vq;
149 struct virtio_console_control *cpkt;
151 vq = port->vser->c_ivq;
152 if (!virtio_queue_ready(vq)) {
153 return 0;
155 if (!virtqueue_pop(vq, &elem)) {
156 return 0;
159 cpkt = (struct virtio_console_control *)buf;
160 stl_p(&cpkt->id, port->id);
161 memcpy(elem.in_sg[0].iov_base, buf, len);
163 virtqueue_push(vq, &elem, len);
164 virtio_notify(&port->vser->vdev, vq);
165 return len;
168 static size_t send_control_event(VirtIOSerialPort *port, uint16_t event,
169 uint16_t value)
171 struct virtio_console_control cpkt;
173 stw_p(&cpkt.event, event);
174 stw_p(&cpkt.value, value);
176 return send_control_msg(port, &cpkt, sizeof(cpkt));
179 /* Functions for use inside qemu to open and read from/write to ports */
180 int virtio_serial_open(VirtIOSerialPort *port)
182 /* Don't allow opening an already-open port */
183 if (port->host_connected) {
184 return 0;
186 /* Send port open notification to the guest */
187 port->host_connected = true;
188 send_control_event(port, VIRTIO_CONSOLE_PORT_OPEN, 1);
190 return 0;
193 int virtio_serial_close(VirtIOSerialPort *port)
195 port->host_connected = false;
197 * If there's any data the guest sent which the app didn't
198 * consume, reset the throttling flag and discard the data.
200 port->throttled = false;
201 flush_queued_data(port, true);
203 send_control_event(port, VIRTIO_CONSOLE_PORT_OPEN, 0);
205 return 0;
208 /* Individual ports/apps call this function to write to the guest. */
209 ssize_t virtio_serial_write(VirtIOSerialPort *port, const uint8_t *buf,
210 size_t size)
212 if (!port || !port->host_connected || !port->guest_connected) {
213 return 0;
215 return write_to_port(port, buf, size);
219 * Readiness of the guest to accept data on a port.
220 * Returns max. data the guest can receive
222 size_t virtio_serial_guest_ready(VirtIOSerialPort *port)
224 VirtQueue *vq = port->ivq;
226 if (!virtio_queue_ready(vq) ||
227 !(port->vser->vdev.status & VIRTIO_CONFIG_S_DRIVER_OK) ||
228 virtio_queue_empty(vq)) {
229 return 0;
231 if (use_multiport(port->vser) && !port->guest_connected) {
232 return 0;
235 if (virtqueue_avail_bytes(vq, 4096, 0)) {
236 return 4096;
238 if (virtqueue_avail_bytes(vq, 1, 0)) {
239 return 1;
241 return 0;
244 void virtio_serial_throttle_port(VirtIOSerialPort *port, bool throttle)
246 if (!port) {
247 return;
250 port->throttled = throttle;
251 if (throttle) {
252 return;
255 flush_queued_data(port, false);
258 /* Guest wants to notify us of some event */
259 static void handle_control_message(VirtIOSerial *vser, void *buf, size_t len)
261 struct VirtIOSerialPort *port;
262 struct virtio_console_control cpkt, *gcpkt;
263 uint8_t *buffer;
264 size_t buffer_len;
266 gcpkt = buf;
268 if (len < sizeof(cpkt)) {
269 /* The guest sent an invalid control packet */
270 return;
273 cpkt.event = lduw_p(&gcpkt->event);
274 cpkt.value = lduw_p(&gcpkt->value);
276 port = find_port_by_id(vser, ldl_p(&gcpkt->id));
277 if (!port && cpkt.event != VIRTIO_CONSOLE_DEVICE_READY)
278 return;
280 switch(cpkt.event) {
281 case VIRTIO_CONSOLE_DEVICE_READY:
282 if (!cpkt.value) {
283 error_report("virtio-serial-bus: Guest failure in adding device %s\n",
284 vser->bus->qbus.name);
285 break;
288 * The device is up, we can now tell the device about all the
289 * ports we have here.
291 QTAILQ_FOREACH(port, &vser->ports, next) {
292 send_control_event(port, VIRTIO_CONSOLE_PORT_ADD, 1);
294 break;
296 case VIRTIO_CONSOLE_PORT_READY:
297 if (!cpkt.value) {
298 error_report("virtio-serial-bus: Guest failure in adding port %u for device %s\n",
299 port->id, vser->bus->qbus.name);
300 break;
303 * Now that we know the guest asked for the port name, we're
304 * sure the guest has initialised whatever state is necessary
305 * for this port. Now's a good time to let the guest know if
306 * this port is a console port so that the guest can hook it
307 * up to hvc.
309 if (port->is_console) {
310 send_control_event(port, VIRTIO_CONSOLE_CONSOLE_PORT, 1);
313 if (port->name) {
314 stw_p(&cpkt.event, VIRTIO_CONSOLE_PORT_NAME);
315 stw_p(&cpkt.value, 1);
317 buffer_len = sizeof(cpkt) + strlen(port->name) + 1;
318 buffer = qemu_malloc(buffer_len);
320 memcpy(buffer, &cpkt, sizeof(cpkt));
321 memcpy(buffer + sizeof(cpkt), port->name, strlen(port->name));
322 buffer[buffer_len - 1] = 0;
324 send_control_msg(port, buffer, buffer_len);
325 qemu_free(buffer);
328 if (port->host_connected) {
329 send_control_event(port, VIRTIO_CONSOLE_PORT_OPEN, 1);
333 * When the guest has asked us for this information it means
334 * the guest is all setup and has its virtqueues
335 * initialised. If some app is interested in knowing about
336 * this event, let it know.
338 if (port->info->guest_ready) {
339 port->info->guest_ready(port);
341 break;
343 case VIRTIO_CONSOLE_PORT_OPEN:
344 port->guest_connected = cpkt.value;
345 if (cpkt.value && port->info->guest_open) {
346 /* Send the guest opened notification if an app is interested */
347 port->info->guest_open(port);
350 if (!cpkt.value && port->info->guest_close) {
351 /* Send the guest closed notification if an app is interested */
352 port->info->guest_close(port);
354 break;
358 static void control_in(VirtIODevice *vdev, VirtQueue *vq)
362 static void control_out(VirtIODevice *vdev, VirtQueue *vq)
364 VirtQueueElement elem;
365 VirtIOSerial *vser;
366 uint8_t *buf;
367 size_t len;
369 vser = DO_UPCAST(VirtIOSerial, vdev, vdev);
371 len = 0;
372 buf = NULL;
373 while (virtqueue_pop(vq, &elem)) {
374 size_t cur_len, copied;
376 cur_len = iov_size(elem.out_sg, elem.out_num);
378 * Allocate a new buf only if we didn't have one previously or
379 * if the size of the buf differs
381 if (cur_len > len) {
382 qemu_free(buf);
384 buf = qemu_malloc(cur_len);
385 len = cur_len;
387 copied = iov_to_buf(elem.out_sg, elem.out_num, buf, 0, len);
389 handle_control_message(vser, buf, copied);
390 virtqueue_push(vq, &elem, 0);
392 qemu_free(buf);
393 virtio_notify(vdev, vq);
396 /* Guest wrote something to some port. */
397 static void handle_output(VirtIODevice *vdev, VirtQueue *vq)
399 VirtIOSerial *vser;
400 VirtIOSerialPort *port;
401 bool discard;
403 vser = DO_UPCAST(VirtIOSerial, vdev, vdev);
404 port = find_port_by_vq(vser, vq);
406 discard = false;
407 if (!port || !port->host_connected || !port->info->have_data) {
408 discard = true;
411 if (!discard && port->throttled) {
412 return;
415 do_flush_queued_data(port, vq, vdev, discard);
418 static void handle_input(VirtIODevice *vdev, VirtQueue *vq)
422 static uint32_t get_features(VirtIODevice *vdev, uint32_t features)
424 VirtIOSerial *vser;
426 vser = DO_UPCAST(VirtIOSerial, vdev, vdev);
428 if (vser->bus->max_nr_ports > 1) {
429 features |= (1 << VIRTIO_CONSOLE_F_MULTIPORT);
431 return features;
434 /* Guest requested config info */
435 static void get_config(VirtIODevice *vdev, uint8_t *config_data)
437 VirtIOSerial *vser;
439 vser = DO_UPCAST(VirtIOSerial, vdev, vdev);
440 memcpy(config_data, &vser->config, sizeof(struct virtio_console_config));
443 static void set_config(VirtIODevice *vdev, const uint8_t *config_data)
445 struct virtio_console_config config;
447 memcpy(&config, config_data, sizeof(config));
450 static void virtio_serial_save(QEMUFile *f, void *opaque)
452 VirtIOSerial *s = opaque;
453 VirtIOSerialPort *port;
454 uint32_t nr_active_ports;
455 unsigned int i;
457 /* The virtio device */
458 virtio_save(&s->vdev, f);
460 /* The config space */
461 qemu_put_be16s(f, &s->config.cols);
462 qemu_put_be16s(f, &s->config.rows);
464 qemu_put_be32s(f, &s->config.max_nr_ports);
466 /* The ports map */
468 for (i = 0; i < (s->config.max_nr_ports + 31) / 32; i++) {
469 qemu_put_be32s(f, &s->ports_map[i]);
472 /* Ports */
474 nr_active_ports = 0;
475 QTAILQ_FOREACH(port, &s->ports, next) {
476 nr_active_ports++;
479 qemu_put_be32s(f, &nr_active_ports);
482 * Items in struct VirtIOSerialPort.
484 QTAILQ_FOREACH(port, &s->ports, next) {
485 qemu_put_be32s(f, &port->id);
486 qemu_put_byte(f, port->guest_connected);
487 qemu_put_byte(f, port->host_connected);
491 static int virtio_serial_load(QEMUFile *f, void *opaque, int version_id)
493 VirtIOSerial *s = opaque;
494 VirtIOSerialPort *port;
495 uint32_t max_nr_ports, nr_active_ports, ports_map;
496 unsigned int i;
498 if (version_id > 2) {
499 return -EINVAL;
502 /* The virtio device */
503 virtio_load(&s->vdev, f);
505 if (version_id < 2) {
506 return 0;
509 /* The config space */
510 qemu_get_be16s(f, &s->config.cols);
511 qemu_get_be16s(f, &s->config.rows);
513 qemu_get_be32s(f, &max_nr_ports);
514 if (max_nr_ports > s->config.max_nr_ports) {
515 /* Source could have had more ports than us. Fail migration. */
516 return -EINVAL;
519 for (i = 0; i < (max_nr_ports + 31) / 32; i++) {
520 qemu_get_be32s(f, &ports_map);
522 if (ports_map != s->ports_map[i]) {
524 * Ports active on source and destination don't
525 * match. Fail migration.
527 return -EINVAL;
531 qemu_get_be32s(f, &nr_active_ports);
533 /* Items in struct VirtIOSerialPort */
534 for (i = 0; i < nr_active_ports; i++) {
535 uint32_t id;
536 bool host_connected;
538 id = qemu_get_be32(f);
539 port = find_port_by_id(s, id);
541 port->guest_connected = qemu_get_byte(f);
542 host_connected = qemu_get_byte(f);
543 if (host_connected != port->host_connected) {
545 * We have to let the guest know of the host connection
546 * status change
548 send_control_event(port, VIRTIO_CONSOLE_PORT_OPEN,
549 port->host_connected);
552 return 0;
555 static void virtser_bus_dev_print(Monitor *mon, DeviceState *qdev, int indent);
557 static struct BusInfo virtser_bus_info = {
558 .name = "virtio-serial-bus",
559 .size = sizeof(VirtIOSerialBus),
560 .print_dev = virtser_bus_dev_print,
563 static VirtIOSerialBus *virtser_bus_new(DeviceState *dev)
565 VirtIOSerialBus *bus;
567 bus = FROM_QBUS(VirtIOSerialBus, qbus_create(&virtser_bus_info, dev, NULL));
568 bus->qbus.allow_hotplug = 1;
570 return bus;
573 static void virtser_bus_dev_print(Monitor *mon, DeviceState *qdev, int indent)
575 VirtIOSerialDevice *dev = DO_UPCAST(VirtIOSerialDevice, qdev, qdev);
576 VirtIOSerialPort *port = DO_UPCAST(VirtIOSerialPort, dev, &dev->qdev);
578 monitor_printf(mon, "%*s dev-prop-int: id: %u\n",
579 indent, "", port->id);
580 monitor_printf(mon, "%*s dev-prop-int: guest_connected: %d\n",
581 indent, "", port->guest_connected);
582 monitor_printf(mon, "%*s dev-prop-int: host_connected: %d\n",
583 indent, "", port->host_connected);
584 monitor_printf(mon, "%*s dev-prop-int: throttled: %d\n",
585 indent, "", port->throttled);
588 /* This function is only used if a port id is not provided by the user */
589 static uint32_t find_free_port_id(VirtIOSerial *vser)
591 unsigned int i;
593 for (i = 0; i < (vser->config.max_nr_ports + 31) / 32; i++) {
594 uint32_t map, bit;
596 map = vser->ports_map[i];
597 bit = ffs(~map);
598 if (bit) {
599 return (bit - 1) + i * 32;
602 return VIRTIO_CONSOLE_BAD_ID;
605 static void mark_port_added(VirtIOSerial *vser, uint32_t port_id)
607 unsigned int i;
609 i = port_id / 32;
610 vser->ports_map[i] |= 1U << (port_id % 32);
613 static void add_port(VirtIOSerial *vser, uint32_t port_id)
615 mark_port_added(vser, port_id);
617 send_control_event(find_port_by_id(vser, port_id),
618 VIRTIO_CONSOLE_PORT_ADD, 1);
621 static void remove_port(VirtIOSerial *vser, uint32_t port_id)
623 VirtIOSerialPort *port;
624 unsigned int i;
626 i = port_id / 32;
627 vser->ports_map[i] &= ~(1U << (port_id % 32));
629 port = find_port_by_id(vser, port_id);
630 /* Flush out any unconsumed buffers first */
631 flush_queued_data(port, true);
633 send_control_event(port, VIRTIO_CONSOLE_PORT_REMOVE, 1);
636 static int virtser_port_qdev_init(DeviceState *qdev, DeviceInfo *base)
638 VirtIOSerialDevice *dev = DO_UPCAST(VirtIOSerialDevice, qdev, qdev);
639 VirtIOSerialPortInfo *info = DO_UPCAST(VirtIOSerialPortInfo, qdev, base);
640 VirtIOSerialPort *port = DO_UPCAST(VirtIOSerialPort, dev, &dev->qdev);
641 VirtIOSerialBus *bus = DO_UPCAST(VirtIOSerialBus, qbus, qdev->parent_bus);
642 int ret;
643 bool plugging_port0;
645 port->vser = bus->vser;
648 * Is the first console port we're seeing? If so, put it up at
649 * location 0. This is done for backward compatibility (old
650 * kernel, new qemu).
652 plugging_port0 = port->is_console && !find_port_by_id(port->vser, 0);
654 if (find_port_by_id(port->vser, port->id)) {
655 error_report("virtio-serial-bus: A port already exists at id %u\n",
656 port->id);
657 return -1;
660 if (port->id == VIRTIO_CONSOLE_BAD_ID) {
661 if (plugging_port0) {
662 port->id = 0;
663 } else {
664 port->id = find_free_port_id(port->vser);
665 if (port->id == VIRTIO_CONSOLE_BAD_ID) {
666 error_report("virtio-serial-bus: Maximum port limit for this device reached\n");
667 return -1;
672 if (port->id >= port->vser->config.max_nr_ports) {
673 error_report("virtio-serial-bus: Out-of-range port id specified, max. allowed: %u\n",
674 port->vser->config.max_nr_ports - 1);
675 return -1;
678 dev->info = info;
679 ret = info->init(dev);
680 if (ret) {
681 return ret;
684 if (!use_multiport(port->vser)) {
686 * Allow writes to guest in this case; we have no way of
687 * knowing if a guest port is connected.
689 port->guest_connected = true;
692 QTAILQ_INSERT_TAIL(&port->vser->ports, port, next);
693 port->ivq = port->vser->ivqs[port->id];
694 port->ovq = port->vser->ovqs[port->id];
696 add_port(port->vser, port->id);
698 /* Send an update to the guest about this new port added */
699 virtio_notify_config(&port->vser->vdev);
701 return ret;
704 static int virtser_port_qdev_exit(DeviceState *qdev)
706 VirtIOSerialDevice *dev = DO_UPCAST(VirtIOSerialDevice, qdev, qdev);
707 VirtIOSerialPort *port = DO_UPCAST(VirtIOSerialPort, dev, &dev->qdev);
708 VirtIOSerial *vser = port->vser;
710 remove_port(port->vser, port->id);
712 QTAILQ_REMOVE(&vser->ports, port, next);
714 if (port->info->exit)
715 port->info->exit(dev);
717 return 0;
720 void virtio_serial_port_qdev_register(VirtIOSerialPortInfo *info)
722 info->qdev.init = virtser_port_qdev_init;
723 info->qdev.bus_info = &virtser_bus_info;
724 info->qdev.exit = virtser_port_qdev_exit;
725 info->qdev.unplug = qdev_simple_unplug_cb;
726 qdev_register(&info->qdev);
729 VirtIODevice *virtio_serial_init(DeviceState *dev, uint32_t max_nr_ports)
731 VirtIOSerial *vser;
732 VirtIODevice *vdev;
733 uint32_t i;
735 if (!max_nr_ports)
736 return NULL;
738 vdev = virtio_common_init("virtio-serial", VIRTIO_ID_CONSOLE,
739 sizeof(struct virtio_console_config),
740 sizeof(VirtIOSerial));
742 vser = DO_UPCAST(VirtIOSerial, vdev, vdev);
744 /* Spawn a new virtio-serial bus on which the ports will ride as devices */
745 vser->bus = virtser_bus_new(dev);
746 vser->bus->vser = vser;
747 QTAILQ_INIT(&vser->ports);
749 vser->bus->max_nr_ports = max_nr_ports;
750 vser->ivqs = qemu_malloc(max_nr_ports * sizeof(VirtQueue *));
751 vser->ovqs = qemu_malloc(max_nr_ports * sizeof(VirtQueue *));
753 /* Add a queue for host to guest transfers for port 0 (backward compat) */
754 vser->ivqs[0] = virtio_add_queue(vdev, 128, handle_input);
755 /* Add a queue for guest to host transfers for port 0 (backward compat) */
756 vser->ovqs[0] = virtio_add_queue(vdev, 128, handle_output);
758 /* control queue: host to guest */
759 vser->c_ivq = virtio_add_queue(vdev, 16, control_in);
760 /* control queue: guest to host */
761 vser->c_ovq = virtio_add_queue(vdev, 16, control_out);
763 for (i = 1; i < vser->bus->max_nr_ports; i++) {
764 /* Add a per-port queue for host to guest transfers */
765 vser->ivqs[i] = virtio_add_queue(vdev, 128, handle_input);
766 /* Add a per-per queue for guest to host transfers */
767 vser->ovqs[i] = virtio_add_queue(vdev, 128, handle_output);
770 vser->config.max_nr_ports = max_nr_ports;
771 vser->ports_map = qemu_mallocz(((max_nr_ports + 31) / 32)
772 * sizeof(vser->ports_map[0]));
774 * Reserve location 0 for a console port for backward compat
775 * (old kernel, new qemu)
777 mark_port_added(vser, 0);
779 vser->vdev.get_features = get_features;
780 vser->vdev.get_config = get_config;
781 vser->vdev.set_config = set_config;
784 * Register for the savevm section with the virtio-console name
785 * to preserve backward compat
787 register_savevm(dev, "virtio-console", -1, 2, virtio_serial_save,
788 virtio_serial_load, vser);
790 return vdev;