guest agent: remove uneeded dependencies
[qemu/mdroth.git] / hw / virtio-serial-bus.c
bloba4825b9eebd4275fb09f6557ff5af528b8ed1d9f
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 "trace.h"
23 #include "virtio-serial.h"
25 /* The virtio-serial bus on top of which the ports will ride as devices */
26 struct VirtIOSerialBus {
27 BusState qbus;
29 /* This is the parent device that provides the bus for ports. */
30 VirtIOSerial *vser;
32 /* The maximum number of ports that can ride on top of this bus */
33 uint32_t max_nr_ports;
36 struct VirtIOSerial {
37 VirtIODevice vdev;
39 VirtQueue *c_ivq, *c_ovq;
40 /* Arrays of ivqs and ovqs: one per port */
41 VirtQueue **ivqs, **ovqs;
43 VirtIOSerialBus bus;
45 DeviceState *qdev;
47 QTAILQ_HEAD(, VirtIOSerialPort) ports;
49 /* bitmap for identifying active ports */
50 uint32_t *ports_map;
52 struct virtio_console_config config;
55 static VirtIOSerialPort *find_port_by_id(VirtIOSerial *vser, uint32_t id)
57 VirtIOSerialPort *port;
59 if (id == VIRTIO_CONSOLE_BAD_ID) {
60 return NULL;
63 QTAILQ_FOREACH(port, &vser->ports, next) {
64 if (port->id == id)
65 return port;
67 return NULL;
70 static VirtIOSerialPort *find_port_by_vq(VirtIOSerial *vser, VirtQueue *vq)
72 VirtIOSerialPort *port;
74 QTAILQ_FOREACH(port, &vser->ports, next) {
75 if (port->ivq == vq || port->ovq == vq)
76 return port;
78 return NULL;
81 static bool use_multiport(VirtIOSerial *vser)
83 return vser->vdev.guest_features & (1 << VIRTIO_CONSOLE_F_MULTIPORT);
86 static size_t write_to_port(VirtIOSerialPort *port,
87 const uint8_t *buf, size_t size)
89 VirtQueueElement elem;
90 VirtQueue *vq;
91 size_t offset;
93 vq = port->ivq;
94 if (!virtio_queue_ready(vq)) {
95 return 0;
98 offset = 0;
99 while (offset < size) {
100 size_t len;
102 if (!virtqueue_pop(vq, &elem)) {
103 break;
106 len = iov_from_buf(elem.in_sg, elem.in_num,
107 buf + offset, 0, size - offset);
108 offset += len;
110 virtqueue_push(vq, &elem, len);
113 virtio_notify(&port->vser->vdev, vq);
114 return offset;
117 static void discard_vq_data(VirtQueue *vq, VirtIODevice *vdev)
119 VirtQueueElement elem;
121 if (!virtio_queue_ready(vq)) {
122 return;
124 while (virtqueue_pop(vq, &elem)) {
125 virtqueue_push(vq, &elem, 0);
127 virtio_notify(vdev, vq);
130 static void do_flush_queued_data(VirtIOSerialPort *port, VirtQueue *vq,
131 VirtIODevice *vdev)
133 VirtIOSerialPortInfo *info;
135 assert(port);
136 assert(virtio_queue_ready(vq));
138 info = DO_UPCAST(VirtIOSerialPortInfo, qdev, port->dev.info);
140 while (!port->throttled) {
141 unsigned int i;
143 /* Pop an elem only if we haven't left off a previous one mid-way */
144 if (!port->elem.out_num) {
145 if (!virtqueue_pop(vq, &port->elem)) {
146 break;
148 port->iov_idx = 0;
149 port->iov_offset = 0;
152 for (i = port->iov_idx; i < port->elem.out_num; i++) {
153 size_t buf_size;
154 ssize_t ret;
156 buf_size = port->elem.out_sg[i].iov_len - port->iov_offset;
157 ret = info->have_data(port,
158 port->elem.out_sg[i].iov_base
159 + port->iov_offset,
160 buf_size);
161 if (ret < 0 && ret != -EAGAIN) {
162 /* We don't handle any other type of errors here */
163 abort();
165 if (ret == -EAGAIN || (ret >= 0 && ret < buf_size)) {
166 virtio_serial_throttle_port(port, true);
167 port->iov_idx = i;
168 if (ret > 0) {
169 port->iov_offset += ret;
171 break;
173 port->iov_offset = 0;
175 if (port->throttled) {
176 break;
178 virtqueue_push(vq, &port->elem, 0);
179 port->elem.out_num = 0;
181 virtio_notify(vdev, vq);
184 static void flush_queued_data(VirtIOSerialPort *port)
186 assert(port);
188 if (!virtio_queue_ready(port->ovq)) {
189 return;
191 do_flush_queued_data(port, port->ovq, &port->vser->vdev);
194 static size_t send_control_msg(VirtIOSerialPort *port, void *buf, size_t len)
196 VirtQueueElement elem;
197 VirtQueue *vq;
198 struct virtio_console_control *cpkt;
200 vq = port->vser->c_ivq;
201 if (!virtio_queue_ready(vq)) {
202 return 0;
204 if (!virtqueue_pop(vq, &elem)) {
205 return 0;
208 cpkt = (struct virtio_console_control *)buf;
209 stl_p(&cpkt->id, port->id);
210 memcpy(elem.in_sg[0].iov_base, buf, len);
212 virtqueue_push(vq, &elem, len);
213 virtio_notify(&port->vser->vdev, vq);
214 return len;
217 static size_t send_control_event(VirtIOSerialPort *port, uint16_t event,
218 uint16_t value)
220 struct virtio_console_control cpkt;
222 stw_p(&cpkt.event, event);
223 stw_p(&cpkt.value, value);
225 trace_virtio_serial_send_control_event(port->id, event, value);
226 return send_control_msg(port, &cpkt, sizeof(cpkt));
229 /* Functions for use inside qemu to open and read from/write to ports */
230 int virtio_serial_open(VirtIOSerialPort *port)
232 /* Don't allow opening an already-open port */
233 if (port->host_connected) {
234 return 0;
236 /* Send port open notification to the guest */
237 port->host_connected = true;
238 send_control_event(port, VIRTIO_CONSOLE_PORT_OPEN, 1);
240 return 0;
243 int virtio_serial_close(VirtIOSerialPort *port)
245 port->host_connected = false;
247 * If there's any data the guest sent which the app didn't
248 * consume, reset the throttling flag and discard the data.
250 port->throttled = false;
251 discard_vq_data(port->ovq, &port->vser->vdev);
253 send_control_event(port, VIRTIO_CONSOLE_PORT_OPEN, 0);
255 return 0;
258 /* Individual ports/apps call this function to write to the guest. */
259 ssize_t virtio_serial_write(VirtIOSerialPort *port, const uint8_t *buf,
260 size_t size)
262 if (!port || !port->host_connected || !port->guest_connected) {
263 return 0;
265 return write_to_port(port, buf, size);
269 * Readiness of the guest to accept data on a port.
270 * Returns max. data the guest can receive
272 size_t virtio_serial_guest_ready(VirtIOSerialPort *port)
274 VirtQueue *vq = port->ivq;
276 if (!virtio_queue_ready(vq) ||
277 !(port->vser->vdev.status & VIRTIO_CONFIG_S_DRIVER_OK) ||
278 virtio_queue_empty(vq)) {
279 return 0;
281 if (use_multiport(port->vser) && !port->guest_connected) {
282 return 0;
285 if (virtqueue_avail_bytes(vq, 4096, 0)) {
286 return 4096;
288 if (virtqueue_avail_bytes(vq, 1, 0)) {
289 return 1;
291 return 0;
294 static void flush_queued_data_bh(void *opaque)
296 VirtIOSerialPort *port = opaque;
298 flush_queued_data(port);
301 void virtio_serial_throttle_port(VirtIOSerialPort *port, bool throttle)
303 if (!port) {
304 return;
307 trace_virtio_serial_throttle_port(port->id, throttle);
308 port->throttled = throttle;
309 if (throttle) {
310 return;
312 qemu_bh_schedule(port->bh);
315 /* Guest wants to notify us of some event */
316 static void handle_control_message(VirtIOSerial *vser, void *buf, size_t len)
318 struct VirtIOSerialPort *port;
319 struct VirtIOSerialPortInfo *info;
320 struct virtio_console_control cpkt, *gcpkt;
321 uint8_t *buffer;
322 size_t buffer_len;
324 gcpkt = buf;
326 if (len < sizeof(cpkt)) {
327 /* The guest sent an invalid control packet */
328 return;
331 cpkt.event = lduw_p(&gcpkt->event);
332 cpkt.value = lduw_p(&gcpkt->value);
334 trace_virtio_serial_handle_control_message(cpkt.event, cpkt.value);
336 if (cpkt.event == VIRTIO_CONSOLE_DEVICE_READY) {
337 if (!cpkt.value) {
338 error_report("virtio-serial-bus: Guest failure in adding device %s",
339 vser->bus.qbus.name);
340 return;
343 * The device is up, we can now tell the device about all the
344 * ports we have here.
346 QTAILQ_FOREACH(port, &vser->ports, next) {
347 send_control_event(port, VIRTIO_CONSOLE_PORT_ADD, 1);
349 return;
352 port = find_port_by_id(vser, ldl_p(&gcpkt->id));
353 if (!port) {
354 error_report("virtio-serial-bus: Unexpected port id %u for device %s",
355 ldl_p(&gcpkt->id), vser->bus.qbus.name);
356 return;
359 trace_virtio_serial_handle_control_message_port(port->id);
361 info = DO_UPCAST(VirtIOSerialPortInfo, qdev, port->dev.info);
363 switch(cpkt.event) {
364 case VIRTIO_CONSOLE_PORT_READY:
365 if (!cpkt.value) {
366 error_report("virtio-serial-bus: Guest failure in adding port %u for device %s",
367 port->id, vser->bus.qbus.name);
368 break;
371 * Now that we know the guest asked for the port name, we're
372 * sure the guest has initialised whatever state is necessary
373 * for this port. Now's a good time to let the guest know if
374 * this port is a console port so that the guest can hook it
375 * up to hvc.
377 if (info->is_console) {
378 send_control_event(port, VIRTIO_CONSOLE_CONSOLE_PORT, 1);
381 if (port->name) {
382 stw_p(&cpkt.event, VIRTIO_CONSOLE_PORT_NAME);
383 stw_p(&cpkt.value, 1);
385 buffer_len = sizeof(cpkt) + strlen(port->name) + 1;
386 buffer = g_malloc(buffer_len);
388 memcpy(buffer, &cpkt, sizeof(cpkt));
389 memcpy(buffer + sizeof(cpkt), port->name, strlen(port->name));
390 buffer[buffer_len - 1] = 0;
392 send_control_msg(port, buffer, buffer_len);
393 g_free(buffer);
396 if (port->host_connected) {
397 send_control_event(port, VIRTIO_CONSOLE_PORT_OPEN, 1);
401 * When the guest has asked us for this information it means
402 * the guest is all setup and has its virtqueues
403 * initialised. If some app is interested in knowing about
404 * this event, let it know.
406 if (info->guest_ready) {
407 info->guest_ready(port);
409 break;
411 case VIRTIO_CONSOLE_PORT_OPEN:
412 port->guest_connected = cpkt.value;
413 if (cpkt.value && info->guest_open) {
414 /* Send the guest opened notification if an app is interested */
415 info->guest_open(port);
418 if (!cpkt.value && info->guest_close) {
419 /* Send the guest closed notification if an app is interested */
420 info->guest_close(port);
422 break;
426 static void control_in(VirtIODevice *vdev, VirtQueue *vq)
430 static void control_out(VirtIODevice *vdev, VirtQueue *vq)
432 VirtQueueElement elem;
433 VirtIOSerial *vser;
434 uint8_t *buf;
435 size_t len;
437 vser = DO_UPCAST(VirtIOSerial, vdev, vdev);
439 len = 0;
440 buf = NULL;
441 while (virtqueue_pop(vq, &elem)) {
442 size_t cur_len, copied;
444 cur_len = iov_size(elem.out_sg, elem.out_num);
446 * Allocate a new buf only if we didn't have one previously or
447 * if the size of the buf differs
449 if (cur_len > len) {
450 g_free(buf);
452 buf = g_malloc(cur_len);
453 len = cur_len;
455 copied = iov_to_buf(elem.out_sg, elem.out_num, buf, 0, len);
457 handle_control_message(vser, buf, copied);
458 virtqueue_push(vq, &elem, 0);
460 g_free(buf);
461 virtio_notify(vdev, vq);
464 /* Guest wrote something to some port. */
465 static void handle_output(VirtIODevice *vdev, VirtQueue *vq)
467 VirtIOSerial *vser;
468 VirtIOSerialPort *port;
469 VirtIOSerialPortInfo *info;
471 vser = DO_UPCAST(VirtIOSerial, vdev, vdev);
472 port = find_port_by_vq(vser, vq);
473 info = port ? DO_UPCAST(VirtIOSerialPortInfo, qdev, port->dev.info) : NULL;
475 if (!port || !port->host_connected || !info->have_data) {
476 discard_vq_data(vq, vdev);
477 return;
480 if (!port->throttled) {
481 do_flush_queued_data(port, vq, vdev);
482 return;
486 static void handle_input(VirtIODevice *vdev, VirtQueue *vq)
490 static uint32_t get_features(VirtIODevice *vdev, uint32_t features)
492 VirtIOSerial *vser;
494 vser = DO_UPCAST(VirtIOSerial, vdev, vdev);
496 if (vser->bus.max_nr_ports > 1) {
497 features |= (1 << VIRTIO_CONSOLE_F_MULTIPORT);
499 return features;
502 /* Guest requested config info */
503 static void get_config(VirtIODevice *vdev, uint8_t *config_data)
505 VirtIOSerial *vser;
507 vser = DO_UPCAST(VirtIOSerial, vdev, vdev);
508 memcpy(config_data, &vser->config, sizeof(struct virtio_console_config));
511 static void set_config(VirtIODevice *vdev, const uint8_t *config_data)
513 struct virtio_console_config config;
515 memcpy(&config, config_data, sizeof(config));
518 static void virtio_serial_save(QEMUFile *f, void *opaque)
520 VirtIOSerial *s = opaque;
521 VirtIOSerialPort *port;
522 uint32_t nr_active_ports;
523 unsigned int i, max_nr_ports;
525 /* The virtio device */
526 virtio_save(&s->vdev, f);
528 /* The config space */
529 qemu_put_be16s(f, &s->config.cols);
530 qemu_put_be16s(f, &s->config.rows);
532 qemu_put_be32s(f, &s->config.max_nr_ports);
534 /* The ports map */
535 max_nr_ports = tswap32(s->config.max_nr_ports);
536 for (i = 0; i < (max_nr_ports + 31) / 32; i++) {
537 qemu_put_be32s(f, &s->ports_map[i]);
540 /* Ports */
542 nr_active_ports = 0;
543 QTAILQ_FOREACH(port, &s->ports, next) {
544 nr_active_ports++;
547 qemu_put_be32s(f, &nr_active_ports);
550 * Items in struct VirtIOSerialPort.
552 QTAILQ_FOREACH(port, &s->ports, next) {
553 uint32_t elem_popped;
555 qemu_put_be32s(f, &port->id);
556 qemu_put_byte(f, port->guest_connected);
557 qemu_put_byte(f, port->host_connected);
559 elem_popped = 0;
560 if (port->elem.out_num) {
561 elem_popped = 1;
563 qemu_put_be32s(f, &elem_popped);
564 if (elem_popped) {
565 qemu_put_be32s(f, &port->iov_idx);
566 qemu_put_be64s(f, &port->iov_offset);
568 qemu_put_buffer(f, (unsigned char *)&port->elem,
569 sizeof(port->elem));
574 static int virtio_serial_load(QEMUFile *f, void *opaque, int version_id)
576 VirtIOSerial *s = opaque;
577 VirtIOSerialPort *port;
578 uint32_t max_nr_ports, nr_active_ports, ports_map;
579 unsigned int i;
581 if (version_id > 3) {
582 return -EINVAL;
585 /* The virtio device */
586 virtio_load(&s->vdev, f);
588 if (version_id < 2) {
589 return 0;
592 /* The config space */
593 qemu_get_be16s(f, &s->config.cols);
594 qemu_get_be16s(f, &s->config.rows);
596 qemu_get_be32s(f, &max_nr_ports);
597 tswap32s(&max_nr_ports);
598 if (max_nr_ports > tswap32(s->config.max_nr_ports)) {
599 /* Source could have had more ports than us. Fail migration. */
600 return -EINVAL;
603 for (i = 0; i < (max_nr_ports + 31) / 32; i++) {
604 qemu_get_be32s(f, &ports_map);
606 if (ports_map != s->ports_map[i]) {
608 * Ports active on source and destination don't
609 * match. Fail migration.
611 return -EINVAL;
615 qemu_get_be32s(f, &nr_active_ports);
617 /* Items in struct VirtIOSerialPort */
618 for (i = 0; i < nr_active_ports; i++) {
619 uint32_t id;
620 bool host_connected;
622 id = qemu_get_be32(f);
623 port = find_port_by_id(s, id);
624 if (!port) {
625 return -EINVAL;
628 port->guest_connected = qemu_get_byte(f);
629 host_connected = qemu_get_byte(f);
630 if (host_connected != port->host_connected) {
632 * We have to let the guest know of the host connection
633 * status change
635 send_control_event(port, VIRTIO_CONSOLE_PORT_OPEN,
636 port->host_connected);
639 if (version_id > 2) {
640 uint32_t elem_popped;
642 qemu_get_be32s(f, &elem_popped);
643 if (elem_popped) {
644 qemu_get_be32s(f, &port->iov_idx);
645 qemu_get_be64s(f, &port->iov_offset);
647 qemu_get_buffer(f, (unsigned char *)&port->elem,
648 sizeof(port->elem));
649 virtqueue_map_sg(port->elem.in_sg, port->elem.in_addr,
650 port->elem.in_num, 1);
651 virtqueue_map_sg(port->elem.out_sg, port->elem.out_addr,
652 port->elem.out_num, 1);
655 * Port was throttled on source machine. Let's
656 * unthrottle it here so data starts flowing again.
658 virtio_serial_throttle_port(port, false);
662 return 0;
665 static void virtser_bus_dev_print(Monitor *mon, DeviceState *qdev, int indent);
667 static struct BusInfo virtser_bus_info = {
668 .name = "virtio-serial-bus",
669 .size = sizeof(VirtIOSerialBus),
670 .print_dev = virtser_bus_dev_print,
671 .props = (Property[]) {
672 DEFINE_PROP_UINT32("nr", VirtIOSerialPort, id, VIRTIO_CONSOLE_BAD_ID),
673 DEFINE_PROP_STRING("name", VirtIOSerialPort, name),
674 DEFINE_PROP_END_OF_LIST()
678 static void virtser_bus_dev_print(Monitor *mon, DeviceState *qdev, int indent)
680 VirtIOSerialPort *port = DO_UPCAST(VirtIOSerialPort, dev, qdev);
682 monitor_printf(mon, "%*sport %d, guest %s, host %s, throttle %s\n",
683 indent, "", port->id,
684 port->guest_connected ? "on" : "off",
685 port->host_connected ? "on" : "off",
686 port->throttled ? "on" : "off");
689 /* This function is only used if a port id is not provided by the user */
690 static uint32_t find_free_port_id(VirtIOSerial *vser)
692 unsigned int i, max_nr_ports;
694 max_nr_ports = tswap32(vser->config.max_nr_ports);
695 for (i = 0; i < (max_nr_ports + 31) / 32; i++) {
696 uint32_t map, bit;
698 map = vser->ports_map[i];
699 bit = ffs(~map);
700 if (bit) {
701 return (bit - 1) + i * 32;
704 return VIRTIO_CONSOLE_BAD_ID;
707 static void mark_port_added(VirtIOSerial *vser, uint32_t port_id)
709 unsigned int i;
711 i = port_id / 32;
712 vser->ports_map[i] |= 1U << (port_id % 32);
715 static void add_port(VirtIOSerial *vser, uint32_t port_id)
717 mark_port_added(vser, port_id);
719 send_control_event(find_port_by_id(vser, port_id),
720 VIRTIO_CONSOLE_PORT_ADD, 1);
723 static void remove_port(VirtIOSerial *vser, uint32_t port_id)
725 VirtIOSerialPort *port;
726 unsigned int i;
728 i = port_id / 32;
729 vser->ports_map[i] &= ~(1U << (port_id % 32));
731 port = find_port_by_id(vser, port_id);
732 /* Flush out any unconsumed buffers first */
733 discard_vq_data(port->ovq, &port->vser->vdev);
735 send_control_event(port, VIRTIO_CONSOLE_PORT_REMOVE, 1);
738 static int virtser_port_qdev_init(DeviceState *qdev, DeviceInfo *base)
740 VirtIOSerialPort *port = DO_UPCAST(VirtIOSerialPort, dev, qdev);
741 VirtIOSerialPortInfo *info = DO_UPCAST(VirtIOSerialPortInfo, qdev, base);
742 VirtIOSerialBus *bus = DO_UPCAST(VirtIOSerialBus, qbus, qdev->parent_bus);
743 int ret, max_nr_ports;
744 bool plugging_port0;
746 port->vser = bus->vser;
747 port->bh = qemu_bh_new(flush_queued_data_bh, port);
750 * Is the first console port we're seeing? If so, put it up at
751 * location 0. This is done for backward compatibility (old
752 * kernel, new qemu).
754 plugging_port0 = info->is_console && !find_port_by_id(port->vser, 0);
756 if (find_port_by_id(port->vser, port->id)) {
757 error_report("virtio-serial-bus: A port already exists at id %u",
758 port->id);
759 return -1;
762 if (port->id == VIRTIO_CONSOLE_BAD_ID) {
763 if (plugging_port0) {
764 port->id = 0;
765 } else {
766 port->id = find_free_port_id(port->vser);
767 if (port->id == VIRTIO_CONSOLE_BAD_ID) {
768 error_report("virtio-serial-bus: Maximum port limit for this device reached");
769 return -1;
774 max_nr_ports = tswap32(port->vser->config.max_nr_ports);
775 if (port->id >= max_nr_ports) {
776 error_report("virtio-serial-bus: Out-of-range port id specified, max. allowed: %u",
777 max_nr_ports - 1);
778 return -1;
781 ret = info->init(port);
782 if (ret) {
783 return ret;
786 if (!use_multiport(port->vser)) {
788 * Allow writes to guest in this case; we have no way of
789 * knowing if a guest port is connected.
791 port->guest_connected = true;
794 port->elem.out_num = 0;
796 QTAILQ_INSERT_TAIL(&port->vser->ports, port, next);
797 port->ivq = port->vser->ivqs[port->id];
798 port->ovq = port->vser->ovqs[port->id];
800 add_port(port->vser, port->id);
802 /* Send an update to the guest about this new port added */
803 virtio_notify_config(&port->vser->vdev);
805 return ret;
808 static int virtser_port_qdev_exit(DeviceState *qdev)
810 VirtIOSerialPort *port = DO_UPCAST(VirtIOSerialPort, dev, qdev);
811 VirtIOSerialPortInfo *info = DO_UPCAST(VirtIOSerialPortInfo, qdev,
812 port->dev.info);
813 VirtIOSerial *vser = port->vser;
815 qemu_bh_delete(port->bh);
816 remove_port(port->vser, port->id);
818 QTAILQ_REMOVE(&vser->ports, port, next);
820 if (info->exit) {
821 info->exit(port);
823 return 0;
826 void virtio_serial_port_qdev_register(VirtIOSerialPortInfo *info)
828 info->qdev.init = virtser_port_qdev_init;
829 info->qdev.bus_info = &virtser_bus_info;
830 info->qdev.exit = virtser_port_qdev_exit;
831 info->qdev.unplug = qdev_simple_unplug_cb;
832 qdev_register(&info->qdev);
835 VirtIODevice *virtio_serial_init(DeviceState *dev, virtio_serial_conf *conf)
837 VirtIOSerial *vser;
838 VirtIODevice *vdev;
839 uint32_t i, max_supported_ports;
841 if (!conf->max_virtserial_ports)
842 return NULL;
844 /* Each port takes 2 queues, and one pair is for the control queue */
845 max_supported_ports = VIRTIO_PCI_QUEUE_MAX / 2 - 1;
847 if (conf->max_virtserial_ports > max_supported_ports) {
848 error_report("maximum ports supported: %u", max_supported_ports);
849 return NULL;
852 vdev = virtio_common_init("virtio-serial", VIRTIO_ID_CONSOLE,
853 sizeof(struct virtio_console_config),
854 sizeof(VirtIOSerial));
856 vser = DO_UPCAST(VirtIOSerial, vdev, vdev);
858 /* Spawn a new virtio-serial bus on which the ports will ride as devices */
859 qbus_create_inplace(&vser->bus.qbus, &virtser_bus_info, dev, NULL);
860 vser->bus.qbus.allow_hotplug = 1;
861 vser->bus.vser = vser;
862 QTAILQ_INIT(&vser->ports);
864 vser->bus.max_nr_ports = conf->max_virtserial_ports;
865 vser->ivqs = g_malloc(conf->max_virtserial_ports * sizeof(VirtQueue *));
866 vser->ovqs = g_malloc(conf->max_virtserial_ports * sizeof(VirtQueue *));
868 /* Add a queue for host to guest transfers for port 0 (backward compat) */
869 vser->ivqs[0] = virtio_add_queue(vdev, 128, handle_input);
870 /* Add a queue for guest to host transfers for port 0 (backward compat) */
871 vser->ovqs[0] = virtio_add_queue(vdev, 128, handle_output);
873 /* TODO: host to guest notifications can get dropped
874 * if the queue fills up. Implement queueing in host,
875 * this might also make it possible to reduce the control
876 * queue size: as guest preposts buffers there,
877 * this will save 4Kbyte of guest memory per entry. */
879 /* control queue: host to guest */
880 vser->c_ivq = virtio_add_queue(vdev, 32, control_in);
881 /* control queue: guest to host */
882 vser->c_ovq = virtio_add_queue(vdev, 32, control_out);
884 for (i = 1; i < vser->bus.max_nr_ports; i++) {
885 /* Add a per-port queue for host to guest transfers */
886 vser->ivqs[i] = virtio_add_queue(vdev, 128, handle_input);
887 /* Add a per-per queue for guest to host transfers */
888 vser->ovqs[i] = virtio_add_queue(vdev, 128, handle_output);
891 vser->config.max_nr_ports = tswap32(conf->max_virtserial_ports);
892 vser->ports_map = g_malloc0(((conf->max_virtserial_ports + 31) / 32)
893 * sizeof(vser->ports_map[0]));
895 * Reserve location 0 for a console port for backward compat
896 * (old kernel, new qemu)
898 mark_port_added(vser, 0);
900 vser->vdev.get_features = get_features;
901 vser->vdev.get_config = get_config;
902 vser->vdev.set_config = set_config;
904 vser->qdev = dev;
907 * Register for the savevm section with the virtio-console name
908 * to preserve backward compat
910 register_savevm(dev, "virtio-console", -1, 3, virtio_serial_save,
911 virtio_serial_load, vser);
913 return vdev;
916 void virtio_serial_exit(VirtIODevice *vdev)
918 VirtIOSerial *vser = DO_UPCAST(VirtIOSerial, vdev, vdev);
920 unregister_savevm(vser->qdev, "virtio-console", vser);
922 g_free(vser->ivqs);
923 g_free(vser->ovqs);
924 g_free(vser->ports_map);
926 virtio_cleanup(vdev);