SPARC64: introduce a convenience function for getting physical addresses
[qemu.git] / hw / virtio-serial-bus.c
blob6d73386441c8a0d1b92fb591c0c3e413a02a63cb
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, 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 = qemu_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 qemu_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 qemu_free(buf);
452 buf = qemu_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 qemu_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,
673 static void virtser_bus_dev_print(Monitor *mon, DeviceState *qdev, int indent)
675 VirtIOSerialPort *port = DO_UPCAST(VirtIOSerialPort, dev, qdev);
677 monitor_printf(mon, "%*s dev-prop-int: id: %u\n",
678 indent, "", port->id);
679 monitor_printf(mon, "%*s dev-prop-int: guest_connected: %d\n",
680 indent, "", port->guest_connected);
681 monitor_printf(mon, "%*s dev-prop-int: host_connected: %d\n",
682 indent, "", port->host_connected);
683 monitor_printf(mon, "%*s dev-prop-int: throttled: %d\n",
684 indent, "", port->throttled);
687 /* This function is only used if a port id is not provided by the user */
688 static uint32_t find_free_port_id(VirtIOSerial *vser)
690 unsigned int i, max_nr_ports;
692 max_nr_ports = tswap32(vser->config.max_nr_ports);
693 for (i = 0; i < (max_nr_ports + 31) / 32; i++) {
694 uint32_t map, bit;
696 map = vser->ports_map[i];
697 bit = ffs(~map);
698 if (bit) {
699 return (bit - 1) + i * 32;
702 return VIRTIO_CONSOLE_BAD_ID;
705 static void mark_port_added(VirtIOSerial *vser, uint32_t port_id)
707 unsigned int i;
709 i = port_id / 32;
710 vser->ports_map[i] |= 1U << (port_id % 32);
713 static void add_port(VirtIOSerial *vser, uint32_t port_id)
715 mark_port_added(vser, port_id);
717 send_control_event(find_port_by_id(vser, port_id),
718 VIRTIO_CONSOLE_PORT_ADD, 1);
721 static void remove_port(VirtIOSerial *vser, uint32_t port_id)
723 VirtIOSerialPort *port;
724 unsigned int i;
726 i = port_id / 32;
727 vser->ports_map[i] &= ~(1U << (port_id % 32));
729 port = find_port_by_id(vser, port_id);
730 /* Flush out any unconsumed buffers first */
731 discard_vq_data(port->ovq, &port->vser->vdev);
733 send_control_event(port, VIRTIO_CONSOLE_PORT_REMOVE, 1);
736 static int virtser_port_qdev_init(DeviceState *qdev, DeviceInfo *base)
738 VirtIOSerialPort *port = DO_UPCAST(VirtIOSerialPort, dev, qdev);
739 VirtIOSerialPortInfo *info = DO_UPCAST(VirtIOSerialPortInfo, qdev, base);
740 VirtIOSerialBus *bus = DO_UPCAST(VirtIOSerialBus, qbus, qdev->parent_bus);
741 int ret, max_nr_ports;
742 bool plugging_port0;
744 port->vser = bus->vser;
745 port->bh = qemu_bh_new(flush_queued_data_bh, port);
748 * Is the first console port we're seeing? If so, put it up at
749 * location 0. This is done for backward compatibility (old
750 * kernel, new qemu).
752 plugging_port0 = info->is_console && !find_port_by_id(port->vser, 0);
754 if (find_port_by_id(port->vser, port->id)) {
755 error_report("virtio-serial-bus: A port already exists at id %u",
756 port->id);
757 return -1;
760 if (port->id == VIRTIO_CONSOLE_BAD_ID) {
761 if (plugging_port0) {
762 port->id = 0;
763 } else {
764 port->id = find_free_port_id(port->vser);
765 if (port->id == VIRTIO_CONSOLE_BAD_ID) {
766 error_report("virtio-serial-bus: Maximum port limit for this device reached");
767 return -1;
772 max_nr_ports = tswap32(port->vser->config.max_nr_ports);
773 if (port->id >= max_nr_ports) {
774 error_report("virtio-serial-bus: Out-of-range port id specified, max. allowed: %u",
775 max_nr_ports - 1);
776 return -1;
779 ret = info->init(port);
780 if (ret) {
781 return ret;
784 if (!use_multiport(port->vser)) {
786 * Allow writes to guest in this case; we have no way of
787 * knowing if a guest port is connected.
789 port->guest_connected = true;
792 port->elem.out_num = 0;
794 QTAILQ_INSERT_TAIL(&port->vser->ports, port, next);
795 port->ivq = port->vser->ivqs[port->id];
796 port->ovq = port->vser->ovqs[port->id];
798 add_port(port->vser, port->id);
800 /* Send an update to the guest about this new port added */
801 virtio_notify_config(&port->vser->vdev);
803 return ret;
806 static int virtser_port_qdev_exit(DeviceState *qdev)
808 VirtIOSerialPort *port = DO_UPCAST(VirtIOSerialPort, dev, qdev);
809 VirtIOSerialPortInfo *info = DO_UPCAST(VirtIOSerialPortInfo, qdev,
810 port->dev.info);
811 VirtIOSerial *vser = port->vser;
813 qemu_bh_delete(port->bh);
814 remove_port(port->vser, port->id);
816 QTAILQ_REMOVE(&vser->ports, port, next);
818 if (info->exit) {
819 info->exit(port);
821 return 0;
824 void virtio_serial_port_qdev_register(VirtIOSerialPortInfo *info)
826 info->qdev.init = virtser_port_qdev_init;
827 info->qdev.bus_info = &virtser_bus_info;
828 info->qdev.exit = virtser_port_qdev_exit;
829 info->qdev.unplug = qdev_simple_unplug_cb;
830 qdev_register(&info->qdev);
833 VirtIODevice *virtio_serial_init(DeviceState *dev, virtio_serial_conf *conf)
835 VirtIOSerial *vser;
836 VirtIODevice *vdev;
837 uint32_t i, max_supported_ports;
839 if (!conf->max_virtserial_ports)
840 return NULL;
842 /* Each port takes 2 queues, and one pair is for the control queue */
843 max_supported_ports = VIRTIO_PCI_QUEUE_MAX / 2 - 1;
845 if (conf->max_virtserial_ports > max_supported_ports) {
846 error_report("maximum ports supported: %u", max_supported_ports);
847 return NULL;
850 vdev = virtio_common_init("virtio-serial", VIRTIO_ID_CONSOLE,
851 sizeof(struct virtio_console_config),
852 sizeof(VirtIOSerial));
854 vser = DO_UPCAST(VirtIOSerial, vdev, vdev);
856 /* Spawn a new virtio-serial bus on which the ports will ride as devices */
857 qbus_create_inplace(&vser->bus.qbus, &virtser_bus_info, dev, NULL);
858 vser->bus.qbus.allow_hotplug = 1;
859 vser->bus.vser = vser;
860 QTAILQ_INIT(&vser->ports);
862 vser->bus.max_nr_ports = conf->max_virtserial_ports;
863 vser->ivqs = qemu_malloc(conf->max_virtserial_ports * sizeof(VirtQueue *));
864 vser->ovqs = qemu_malloc(conf->max_virtserial_ports * sizeof(VirtQueue *));
866 /* Add a queue for host to guest transfers for port 0 (backward compat) */
867 vser->ivqs[0] = virtio_add_queue(vdev, 128, handle_input);
868 /* Add a queue for guest to host transfers for port 0 (backward compat) */
869 vser->ovqs[0] = virtio_add_queue(vdev, 128, handle_output);
871 /* TODO: host to guest notifications can get dropped
872 * if the queue fills up. Implement queueing in host,
873 * this might also make it possible to reduce the control
874 * queue size: as guest preposts buffers there,
875 * this will save 4Kbyte of guest memory per entry. */
877 /* control queue: host to guest */
878 vser->c_ivq = virtio_add_queue(vdev, 32, control_in);
879 /* control queue: guest to host */
880 vser->c_ovq = virtio_add_queue(vdev, 32, control_out);
882 for (i = 1; i < vser->bus.max_nr_ports; i++) {
883 /* Add a per-port queue for host to guest transfers */
884 vser->ivqs[i] = virtio_add_queue(vdev, 128, handle_input);
885 /* Add a per-per queue for guest to host transfers */
886 vser->ovqs[i] = virtio_add_queue(vdev, 128, handle_output);
889 vser->config.max_nr_ports = tswap32(conf->max_virtserial_ports);
890 vser->ports_map = qemu_mallocz(((conf->max_virtserial_ports + 31) / 32)
891 * sizeof(vser->ports_map[0]));
893 * Reserve location 0 for a console port for backward compat
894 * (old kernel, new qemu)
896 mark_port_added(vser, 0);
898 vser->vdev.get_features = get_features;
899 vser->vdev.get_config = get_config;
900 vser->vdev.set_config = set_config;
902 vser->qdev = dev;
905 * Register for the savevm section with the virtio-console name
906 * to preserve backward compat
908 register_savevm(dev, "virtio-console", -1, 3, virtio_serial_save,
909 virtio_serial_load, vser);
911 return vdev;
914 void virtio_serial_exit(VirtIODevice *vdev)
916 VirtIOSerial *vser = DO_UPCAST(VirtIOSerial, vdev, vdev);
918 unregister_savevm(vser->qdev, "virtio-console", vser);
920 qemu_free(vser->ivqs);
921 qemu_free(vser->ovqs);
922 qemu_free(vser->ports_map);
924 virtio_cleanup(vdev);