apic: Fix legacy vmstate loading for KVM
[qemu/ar7.git] / hw / virtio-serial-bus.c
blob61176299a3892cf0cef342e305cd39fa7d556331
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.
17 * Contributions after 2012-01-13 are licensed under the terms of the
18 * GNU GPL, version 2 or (at your option) any later version.
21 #include "iov.h"
22 #include "monitor.h"
23 #include "qemu-queue.h"
24 #include "sysbus.h"
25 #include "trace.h"
26 #include "virtio-serial.h"
28 /* The virtio-serial bus on top of which the ports will ride as devices */
29 struct VirtIOSerialBus {
30 BusState qbus;
32 /* This is the parent device that provides the bus for ports. */
33 VirtIOSerial *vser;
35 /* The maximum number of ports that can ride on top of this bus */
36 uint32_t max_nr_ports;
39 struct VirtIOSerial {
40 VirtIODevice vdev;
42 VirtQueue *c_ivq, *c_ovq;
43 /* Arrays of ivqs and ovqs: one per port */
44 VirtQueue **ivqs, **ovqs;
46 VirtIOSerialBus bus;
48 DeviceState *qdev;
50 QTAILQ_HEAD(, VirtIOSerialPort) ports;
52 /* bitmap for identifying active ports */
53 uint32_t *ports_map;
55 struct virtio_console_config config;
58 static VirtIOSerialPort *find_port_by_id(VirtIOSerial *vser, uint32_t id)
60 VirtIOSerialPort *port;
62 if (id == VIRTIO_CONSOLE_BAD_ID) {
63 return NULL;
66 QTAILQ_FOREACH(port, &vser->ports, next) {
67 if (port->id == id)
68 return port;
70 return NULL;
73 static VirtIOSerialPort *find_port_by_vq(VirtIOSerial *vser, VirtQueue *vq)
75 VirtIOSerialPort *port;
77 QTAILQ_FOREACH(port, &vser->ports, next) {
78 if (port->ivq == vq || port->ovq == vq)
79 return port;
81 return NULL;
84 static bool use_multiport(VirtIOSerial *vser)
86 return vser->vdev.guest_features & (1 << VIRTIO_CONSOLE_F_MULTIPORT);
89 static size_t write_to_port(VirtIOSerialPort *port,
90 const uint8_t *buf, size_t size)
92 VirtQueueElement elem;
93 VirtQueue *vq;
94 size_t offset;
96 vq = port->ivq;
97 if (!virtio_queue_ready(vq)) {
98 return 0;
101 offset = 0;
102 while (offset < size) {
103 size_t len;
105 if (!virtqueue_pop(vq, &elem)) {
106 break;
109 len = iov_from_buf(elem.in_sg, elem.in_num,
110 buf + offset, 0, size - offset);
111 offset += len;
113 virtqueue_push(vq, &elem, len);
116 virtio_notify(&port->vser->vdev, vq);
117 return offset;
120 static void discard_vq_data(VirtQueue *vq, VirtIODevice *vdev)
122 VirtQueueElement elem;
124 if (!virtio_queue_ready(vq)) {
125 return;
127 while (virtqueue_pop(vq, &elem)) {
128 virtqueue_push(vq, &elem, 0);
130 virtio_notify(vdev, vq);
133 static void do_flush_queued_data(VirtIOSerialPort *port, VirtQueue *vq,
134 VirtIODevice *vdev)
136 VirtIOSerialPortClass *vsc;
138 assert(port);
139 assert(virtio_queue_ready(vq));
141 vsc = VIRTIO_SERIAL_PORT_GET_CLASS(port);
143 while (!port->throttled) {
144 unsigned int i;
146 /* Pop an elem only if we haven't left off a previous one mid-way */
147 if (!port->elem.out_num) {
148 if (!virtqueue_pop(vq, &port->elem)) {
149 break;
151 port->iov_idx = 0;
152 port->iov_offset = 0;
155 for (i = port->iov_idx; i < port->elem.out_num; i++) {
156 size_t buf_size;
157 ssize_t ret;
159 buf_size = port->elem.out_sg[i].iov_len - port->iov_offset;
160 ret = vsc->have_data(port,
161 port->elem.out_sg[i].iov_base
162 + port->iov_offset,
163 buf_size);
164 if (ret < 0 && ret != -EAGAIN) {
165 /* We don't handle any other type of errors here */
166 abort();
168 if (ret == -EAGAIN || (ret >= 0 && ret < buf_size)) {
170 * this is a temporary check until chardevs can signal to
171 * frontends that they are writable again. This prevents
172 * the console from going into throttled mode (forever)
173 * if virtio-console is connected to a pty without a
174 * listener. Otherwise the guest spins forever.
175 * We can revert this if
176 * 1: chardevs can notify frondends
177 * 2: the guest driver does not spin in these cases
179 if (!vsc->is_console) {
180 virtio_serial_throttle_port(port, true);
182 port->iov_idx = i;
183 if (ret > 0) {
184 port->iov_offset += ret;
186 break;
188 port->iov_offset = 0;
190 if (port->throttled) {
191 break;
193 virtqueue_push(vq, &port->elem, 0);
194 port->elem.out_num = 0;
196 virtio_notify(vdev, vq);
199 static void flush_queued_data(VirtIOSerialPort *port)
201 assert(port);
203 if (!virtio_queue_ready(port->ovq)) {
204 return;
206 do_flush_queued_data(port, port->ovq, &port->vser->vdev);
209 static size_t send_control_msg(VirtIOSerialPort *port, void *buf, size_t len)
211 VirtQueueElement elem;
212 VirtQueue *vq;
213 struct virtio_console_control *cpkt;
215 vq = port->vser->c_ivq;
216 if (!virtio_queue_ready(vq)) {
217 return 0;
219 if (!virtqueue_pop(vq, &elem)) {
220 return 0;
223 cpkt = (struct virtio_console_control *)buf;
224 stl_p(&cpkt->id, port->id);
225 memcpy(elem.in_sg[0].iov_base, buf, len);
227 virtqueue_push(vq, &elem, len);
228 virtio_notify(&port->vser->vdev, vq);
229 return len;
232 static size_t send_control_event(VirtIOSerialPort *port, uint16_t event,
233 uint16_t value)
235 struct virtio_console_control cpkt;
237 stw_p(&cpkt.event, event);
238 stw_p(&cpkt.value, value);
240 trace_virtio_serial_send_control_event(port->id, event, value);
241 return send_control_msg(port, &cpkt, sizeof(cpkt));
244 /* Functions for use inside qemu to open and read from/write to ports */
245 int virtio_serial_open(VirtIOSerialPort *port)
247 /* Don't allow opening an already-open port */
248 if (port->host_connected) {
249 return 0;
251 /* Send port open notification to the guest */
252 port->host_connected = true;
253 send_control_event(port, VIRTIO_CONSOLE_PORT_OPEN, 1);
255 return 0;
258 int virtio_serial_close(VirtIOSerialPort *port)
260 port->host_connected = false;
262 * If there's any data the guest sent which the app didn't
263 * consume, reset the throttling flag and discard the data.
265 port->throttled = false;
266 discard_vq_data(port->ovq, &port->vser->vdev);
268 send_control_event(port, VIRTIO_CONSOLE_PORT_OPEN, 0);
270 return 0;
273 /* Individual ports/apps call this function to write to the guest. */
274 ssize_t virtio_serial_write(VirtIOSerialPort *port, const uint8_t *buf,
275 size_t size)
277 if (!port || !port->host_connected || !port->guest_connected) {
278 return 0;
280 return write_to_port(port, buf, size);
284 * Readiness of the guest to accept data on a port.
285 * Returns max. data the guest can receive
287 size_t virtio_serial_guest_ready(VirtIOSerialPort *port)
289 VirtQueue *vq = port->ivq;
291 if (!virtio_queue_ready(vq) ||
292 !(port->vser->vdev.status & VIRTIO_CONFIG_S_DRIVER_OK) ||
293 virtio_queue_empty(vq)) {
294 return 0;
296 if (use_multiport(port->vser) && !port->guest_connected) {
297 return 0;
300 if (virtqueue_avail_bytes(vq, 4096, 0)) {
301 return 4096;
303 if (virtqueue_avail_bytes(vq, 1, 0)) {
304 return 1;
306 return 0;
309 static void flush_queued_data_bh(void *opaque)
311 VirtIOSerialPort *port = opaque;
313 flush_queued_data(port);
316 void virtio_serial_throttle_port(VirtIOSerialPort *port, bool throttle)
318 if (!port) {
319 return;
322 trace_virtio_serial_throttle_port(port->id, throttle);
323 port->throttled = throttle;
324 if (throttle) {
325 return;
327 qemu_bh_schedule(port->bh);
330 /* Guest wants to notify us of some event */
331 static void handle_control_message(VirtIOSerial *vser, void *buf, size_t len)
333 struct VirtIOSerialPort *port;
334 VirtIOSerialPortClass *vsc;
335 struct virtio_console_control cpkt, *gcpkt;
336 uint8_t *buffer;
337 size_t buffer_len;
339 gcpkt = buf;
341 if (len < sizeof(cpkt)) {
342 /* The guest sent an invalid control packet */
343 return;
346 cpkt.event = lduw_p(&gcpkt->event);
347 cpkt.value = lduw_p(&gcpkt->value);
349 trace_virtio_serial_handle_control_message(cpkt.event, cpkt.value);
351 if (cpkt.event == VIRTIO_CONSOLE_DEVICE_READY) {
352 if (!cpkt.value) {
353 error_report("virtio-serial-bus: Guest failure in adding device %s",
354 vser->bus.qbus.name);
355 return;
358 * The device is up, we can now tell the device about all the
359 * ports we have here.
361 QTAILQ_FOREACH(port, &vser->ports, next) {
362 send_control_event(port, VIRTIO_CONSOLE_PORT_ADD, 1);
364 return;
367 port = find_port_by_id(vser, ldl_p(&gcpkt->id));
368 if (!port) {
369 error_report("virtio-serial-bus: Unexpected port id %u for device %s",
370 ldl_p(&gcpkt->id), vser->bus.qbus.name);
371 return;
374 trace_virtio_serial_handle_control_message_port(port->id);
376 vsc = VIRTIO_SERIAL_PORT_GET_CLASS(port);
378 switch(cpkt.event) {
379 case VIRTIO_CONSOLE_PORT_READY:
380 if (!cpkt.value) {
381 error_report("virtio-serial-bus: Guest failure in adding port %u for device %s",
382 port->id, vser->bus.qbus.name);
383 break;
386 * Now that we know the guest asked for the port name, we're
387 * sure the guest has initialised whatever state is necessary
388 * for this port. Now's a good time to let the guest know if
389 * this port is a console port so that the guest can hook it
390 * up to hvc.
392 if (vsc->is_console) {
393 send_control_event(port, VIRTIO_CONSOLE_CONSOLE_PORT, 1);
396 if (port->name) {
397 stw_p(&cpkt.event, VIRTIO_CONSOLE_PORT_NAME);
398 stw_p(&cpkt.value, 1);
400 buffer_len = sizeof(cpkt) + strlen(port->name) + 1;
401 buffer = g_malloc(buffer_len);
403 memcpy(buffer, &cpkt, sizeof(cpkt));
404 memcpy(buffer + sizeof(cpkt), port->name, strlen(port->name));
405 buffer[buffer_len - 1] = 0;
407 send_control_msg(port, buffer, buffer_len);
408 g_free(buffer);
411 if (port->host_connected) {
412 send_control_event(port, VIRTIO_CONSOLE_PORT_OPEN, 1);
416 * When the guest has asked us for this information it means
417 * the guest is all setup and has its virtqueues
418 * initialised. If some app is interested in knowing about
419 * this event, let it know.
421 if (vsc->guest_ready) {
422 vsc->guest_ready(port);
424 break;
426 case VIRTIO_CONSOLE_PORT_OPEN:
427 port->guest_connected = cpkt.value;
428 if (cpkt.value && vsc->guest_open) {
429 /* Send the guest opened notification if an app is interested */
430 vsc->guest_open(port);
433 if (!cpkt.value && vsc->guest_close) {
434 /* Send the guest closed notification if an app is interested */
435 vsc->guest_close(port);
437 break;
441 static void control_in(VirtIODevice *vdev, VirtQueue *vq)
445 static void control_out(VirtIODevice *vdev, VirtQueue *vq)
447 VirtQueueElement elem;
448 VirtIOSerial *vser;
449 uint8_t *buf;
450 size_t len;
452 vser = DO_UPCAST(VirtIOSerial, vdev, vdev);
454 len = 0;
455 buf = NULL;
456 while (virtqueue_pop(vq, &elem)) {
457 size_t cur_len, copied;
459 cur_len = iov_size(elem.out_sg, elem.out_num);
461 * Allocate a new buf only if we didn't have one previously or
462 * if the size of the buf differs
464 if (cur_len > len) {
465 g_free(buf);
467 buf = g_malloc(cur_len);
468 len = cur_len;
470 copied = iov_to_buf(elem.out_sg, elem.out_num, buf, 0, len);
472 handle_control_message(vser, buf, copied);
473 virtqueue_push(vq, &elem, 0);
475 g_free(buf);
476 virtio_notify(vdev, vq);
479 /* Guest wrote something to some port. */
480 static void handle_output(VirtIODevice *vdev, VirtQueue *vq)
482 VirtIOSerial *vser;
483 VirtIOSerialPort *port;
485 vser = DO_UPCAST(VirtIOSerial, vdev, vdev);
486 port = find_port_by_vq(vser, vq);
488 if (!port || !port->host_connected) {
489 discard_vq_data(vq, vdev);
490 return;
493 if (!port->throttled) {
494 do_flush_queued_data(port, vq, vdev);
495 return;
499 static void handle_input(VirtIODevice *vdev, VirtQueue *vq)
503 static uint32_t get_features(VirtIODevice *vdev, uint32_t features)
505 VirtIOSerial *vser;
507 vser = DO_UPCAST(VirtIOSerial, vdev, vdev);
509 if (vser->bus.max_nr_ports > 1) {
510 features |= (1 << VIRTIO_CONSOLE_F_MULTIPORT);
512 return features;
515 /* Guest requested config info */
516 static void get_config(VirtIODevice *vdev, uint8_t *config_data)
518 VirtIOSerial *vser;
520 vser = DO_UPCAST(VirtIOSerial, vdev, vdev);
521 memcpy(config_data, &vser->config, sizeof(struct virtio_console_config));
524 static void set_config(VirtIODevice *vdev, const uint8_t *config_data)
526 struct virtio_console_config config;
528 memcpy(&config, config_data, sizeof(config));
531 static void virtio_serial_save(QEMUFile *f, void *opaque)
533 VirtIOSerial *s = opaque;
534 VirtIOSerialPort *port;
535 uint32_t nr_active_ports;
536 unsigned int i, max_nr_ports;
538 /* The virtio device */
539 virtio_save(&s->vdev, f);
541 /* The config space */
542 qemu_put_be16s(f, &s->config.cols);
543 qemu_put_be16s(f, &s->config.rows);
545 qemu_put_be32s(f, &s->config.max_nr_ports);
547 /* The ports map */
548 max_nr_ports = tswap32(s->config.max_nr_ports);
549 for (i = 0; i < (max_nr_ports + 31) / 32; i++) {
550 qemu_put_be32s(f, &s->ports_map[i]);
553 /* Ports */
555 nr_active_ports = 0;
556 QTAILQ_FOREACH(port, &s->ports, next) {
557 nr_active_ports++;
560 qemu_put_be32s(f, &nr_active_ports);
563 * Items in struct VirtIOSerialPort.
565 QTAILQ_FOREACH(port, &s->ports, next) {
566 uint32_t elem_popped;
568 qemu_put_be32s(f, &port->id);
569 qemu_put_byte(f, port->guest_connected);
570 qemu_put_byte(f, port->host_connected);
572 elem_popped = 0;
573 if (port->elem.out_num) {
574 elem_popped = 1;
576 qemu_put_be32s(f, &elem_popped);
577 if (elem_popped) {
578 qemu_put_be32s(f, &port->iov_idx);
579 qemu_put_be64s(f, &port->iov_offset);
581 qemu_put_buffer(f, (unsigned char *)&port->elem,
582 sizeof(port->elem));
587 static int virtio_serial_load(QEMUFile *f, void *opaque, int version_id)
589 VirtIOSerial *s = opaque;
590 VirtIOSerialPort *port;
591 uint32_t max_nr_ports, nr_active_ports, ports_map;
592 unsigned int i;
594 if (version_id > 3) {
595 return -EINVAL;
598 /* The virtio device */
599 virtio_load(&s->vdev, f);
601 if (version_id < 2) {
602 return 0;
605 /* The config space */
606 qemu_get_be16s(f, &s->config.cols);
607 qemu_get_be16s(f, &s->config.rows);
609 qemu_get_be32s(f, &max_nr_ports);
610 tswap32s(&max_nr_ports);
611 if (max_nr_ports > tswap32(s->config.max_nr_ports)) {
612 /* Source could have had more ports than us. Fail migration. */
613 return -EINVAL;
616 for (i = 0; i < (max_nr_ports + 31) / 32; i++) {
617 qemu_get_be32s(f, &ports_map);
619 if (ports_map != s->ports_map[i]) {
621 * Ports active on source and destination don't
622 * match. Fail migration.
624 return -EINVAL;
628 qemu_get_be32s(f, &nr_active_ports);
630 /* Items in struct VirtIOSerialPort */
631 for (i = 0; i < nr_active_ports; i++) {
632 uint32_t id;
633 bool host_connected;
635 id = qemu_get_be32(f);
636 port = find_port_by_id(s, id);
637 if (!port) {
638 return -EINVAL;
641 port->guest_connected = qemu_get_byte(f);
642 host_connected = qemu_get_byte(f);
643 if (host_connected != port->host_connected) {
645 * We have to let the guest know of the host connection
646 * status change
648 send_control_event(port, VIRTIO_CONSOLE_PORT_OPEN,
649 port->host_connected);
652 if (version_id > 2) {
653 uint32_t elem_popped;
655 qemu_get_be32s(f, &elem_popped);
656 if (elem_popped) {
657 qemu_get_be32s(f, &port->iov_idx);
658 qemu_get_be64s(f, &port->iov_offset);
660 qemu_get_buffer(f, (unsigned char *)&port->elem,
661 sizeof(port->elem));
662 virtqueue_map_sg(port->elem.in_sg, port->elem.in_addr,
663 port->elem.in_num, 1);
664 virtqueue_map_sg(port->elem.out_sg, port->elem.out_addr,
665 port->elem.out_num, 1);
668 * Port was throttled on source machine. Let's
669 * unthrottle it here so data starts flowing again.
671 virtio_serial_throttle_port(port, false);
675 return 0;
678 static void virtser_bus_dev_print(Monitor *mon, DeviceState *qdev, int indent);
680 static struct BusInfo virtser_bus_info = {
681 .name = "virtio-serial-bus",
682 .size = sizeof(VirtIOSerialBus),
683 .print_dev = virtser_bus_dev_print,
684 .props = (Property[]) {
685 DEFINE_PROP_UINT32("nr", VirtIOSerialPort, id, VIRTIO_CONSOLE_BAD_ID),
686 DEFINE_PROP_STRING("name", VirtIOSerialPort, name),
687 DEFINE_PROP_END_OF_LIST()
691 static void virtser_bus_dev_print(Monitor *mon, DeviceState *qdev, int indent)
693 VirtIOSerialPort *port = DO_UPCAST(VirtIOSerialPort, dev, qdev);
695 monitor_printf(mon, "%*sport %d, guest %s, host %s, throttle %s\n",
696 indent, "", port->id,
697 port->guest_connected ? "on" : "off",
698 port->host_connected ? "on" : "off",
699 port->throttled ? "on" : "off");
702 /* This function is only used if a port id is not provided by the user */
703 static uint32_t find_free_port_id(VirtIOSerial *vser)
705 unsigned int i, max_nr_ports;
707 max_nr_ports = tswap32(vser->config.max_nr_ports);
708 for (i = 0; i < (max_nr_ports + 31) / 32; i++) {
709 uint32_t map, bit;
711 map = vser->ports_map[i];
712 bit = ffs(~map);
713 if (bit) {
714 return (bit - 1) + i * 32;
717 return VIRTIO_CONSOLE_BAD_ID;
720 static void mark_port_added(VirtIOSerial *vser, uint32_t port_id)
722 unsigned int i;
724 i = port_id / 32;
725 vser->ports_map[i] |= 1U << (port_id % 32);
728 static void add_port(VirtIOSerial *vser, uint32_t port_id)
730 mark_port_added(vser, port_id);
732 send_control_event(find_port_by_id(vser, port_id),
733 VIRTIO_CONSOLE_PORT_ADD, 1);
736 static void remove_port(VirtIOSerial *vser, uint32_t port_id)
738 VirtIOSerialPort *port;
739 unsigned int i;
741 i = port_id / 32;
742 vser->ports_map[i] &= ~(1U << (port_id % 32));
744 port = find_port_by_id(vser, port_id);
745 /* Flush out any unconsumed buffers first */
746 discard_vq_data(port->ovq, &port->vser->vdev);
748 send_control_event(port, VIRTIO_CONSOLE_PORT_REMOVE, 1);
751 static int virtser_port_qdev_init(DeviceState *qdev)
753 VirtIOSerialPort *port = DO_UPCAST(VirtIOSerialPort, dev, qdev);
754 VirtIOSerialPortClass *vsc = VIRTIO_SERIAL_PORT_GET_CLASS(port);
755 VirtIOSerialBus *bus = DO_UPCAST(VirtIOSerialBus, qbus, qdev->parent_bus);
756 int ret, max_nr_ports;
757 bool plugging_port0;
759 port->vser = bus->vser;
760 port->bh = qemu_bh_new(flush_queued_data_bh, port);
762 assert(vsc->have_data);
765 * Is the first console port we're seeing? If so, put it up at
766 * location 0. This is done for backward compatibility (old
767 * kernel, new qemu).
769 plugging_port0 = vsc->is_console && !find_port_by_id(port->vser, 0);
771 if (find_port_by_id(port->vser, port->id)) {
772 error_report("virtio-serial-bus: A port already exists at id %u",
773 port->id);
774 return -1;
777 if (port->id == VIRTIO_CONSOLE_BAD_ID) {
778 if (plugging_port0) {
779 port->id = 0;
780 } else {
781 port->id = find_free_port_id(port->vser);
782 if (port->id == VIRTIO_CONSOLE_BAD_ID) {
783 error_report("virtio-serial-bus: Maximum port limit for this device reached");
784 return -1;
789 max_nr_ports = tswap32(port->vser->config.max_nr_ports);
790 if (port->id >= max_nr_ports) {
791 error_report("virtio-serial-bus: Out-of-range port id specified, max. allowed: %u",
792 max_nr_ports - 1);
793 return -1;
796 ret = vsc->init(port);
797 if (ret) {
798 return ret;
801 if (!use_multiport(port->vser)) {
803 * Allow writes to guest in this case; we have no way of
804 * knowing if a guest port is connected.
806 port->guest_connected = true;
809 port->elem.out_num = 0;
811 QTAILQ_INSERT_TAIL(&port->vser->ports, port, next);
812 port->ivq = port->vser->ivqs[port->id];
813 port->ovq = port->vser->ovqs[port->id];
815 add_port(port->vser, port->id);
817 /* Send an update to the guest about this new port added */
818 virtio_notify_config(&port->vser->vdev);
820 return ret;
823 static int virtser_port_qdev_exit(DeviceState *qdev)
825 VirtIOSerialPort *port = DO_UPCAST(VirtIOSerialPort, dev, qdev);
826 VirtIOSerialPortClass *vsc = VIRTIO_SERIAL_PORT_GET_CLASS(port);
827 VirtIOSerial *vser = port->vser;
829 qemu_bh_delete(port->bh);
830 remove_port(port->vser, port->id);
832 QTAILQ_REMOVE(&vser->ports, port, next);
834 if (vsc->exit) {
835 vsc->exit(port);
837 return 0;
840 VirtIODevice *virtio_serial_init(DeviceState *dev, virtio_serial_conf *conf)
842 VirtIOSerial *vser;
843 VirtIODevice *vdev;
844 uint32_t i, max_supported_ports;
846 if (!conf->max_virtserial_ports)
847 return NULL;
849 /* Each port takes 2 queues, and one pair is for the control queue */
850 max_supported_ports = VIRTIO_PCI_QUEUE_MAX / 2 - 1;
852 if (conf->max_virtserial_ports > max_supported_ports) {
853 error_report("maximum ports supported: %u", max_supported_ports);
854 return NULL;
857 vdev = virtio_common_init("virtio-serial", VIRTIO_ID_CONSOLE,
858 sizeof(struct virtio_console_config),
859 sizeof(VirtIOSerial));
861 vser = DO_UPCAST(VirtIOSerial, vdev, vdev);
863 /* Spawn a new virtio-serial bus on which the ports will ride as devices */
864 qbus_create_inplace(&vser->bus.qbus, &virtser_bus_info, dev, NULL);
865 vser->bus.qbus.allow_hotplug = 1;
866 vser->bus.vser = vser;
867 QTAILQ_INIT(&vser->ports);
869 vser->bus.max_nr_ports = conf->max_virtserial_ports;
870 vser->ivqs = g_malloc(conf->max_virtserial_ports * sizeof(VirtQueue *));
871 vser->ovqs = g_malloc(conf->max_virtserial_ports * sizeof(VirtQueue *));
873 /* Add a queue for host to guest transfers for port 0 (backward compat) */
874 vser->ivqs[0] = virtio_add_queue(vdev, 128, handle_input);
875 /* Add a queue for guest to host transfers for port 0 (backward compat) */
876 vser->ovqs[0] = virtio_add_queue(vdev, 128, handle_output);
878 /* TODO: host to guest notifications can get dropped
879 * if the queue fills up. Implement queueing in host,
880 * this might also make it possible to reduce the control
881 * queue size: as guest preposts buffers there,
882 * this will save 4Kbyte of guest memory per entry. */
884 /* control queue: host to guest */
885 vser->c_ivq = virtio_add_queue(vdev, 32, control_in);
886 /* control queue: guest to host */
887 vser->c_ovq = virtio_add_queue(vdev, 32, control_out);
889 for (i = 1; i < vser->bus.max_nr_ports; i++) {
890 /* Add a per-port queue for host to guest transfers */
891 vser->ivqs[i] = virtio_add_queue(vdev, 128, handle_input);
892 /* Add a per-per queue for guest to host transfers */
893 vser->ovqs[i] = virtio_add_queue(vdev, 128, handle_output);
896 vser->config.max_nr_ports = tswap32(conf->max_virtserial_ports);
897 vser->ports_map = g_malloc0(((conf->max_virtserial_ports + 31) / 32)
898 * sizeof(vser->ports_map[0]));
900 * Reserve location 0 for a console port for backward compat
901 * (old kernel, new qemu)
903 mark_port_added(vser, 0);
905 vser->vdev.get_features = get_features;
906 vser->vdev.get_config = get_config;
907 vser->vdev.set_config = set_config;
909 vser->qdev = dev;
912 * Register for the savevm section with the virtio-console name
913 * to preserve backward compat
915 register_savevm(dev, "virtio-console", -1, 3, virtio_serial_save,
916 virtio_serial_load, vser);
918 return vdev;
921 void virtio_serial_exit(VirtIODevice *vdev)
923 VirtIOSerial *vser = DO_UPCAST(VirtIOSerial, vdev, vdev);
925 unregister_savevm(vser->qdev, "virtio-console", vser);
927 g_free(vser->ivqs);
928 g_free(vser->ovqs);
929 g_free(vser->ports_map);
931 virtio_cleanup(vdev);
934 static void virtio_serial_port_class_init(ObjectClass *klass, void *data)
936 DeviceClass *k = DEVICE_CLASS(klass);
937 k->init = virtser_port_qdev_init;
938 k->bus_info = &virtser_bus_info;
939 k->exit = virtser_port_qdev_exit;
940 k->unplug = qdev_simple_unplug_cb;
943 static TypeInfo virtio_serial_port_type_info = {
944 .name = TYPE_VIRTIO_SERIAL_PORT,
945 .parent = TYPE_DEVICE,
946 .instance_size = sizeof(VirtIOSerialPort),
947 .abstract = true,
948 .class_size = sizeof(VirtIOSerialPortClass),
949 .class_init = virtio_serial_port_class_init,
952 static void virtio_serial_register_devices(void)
954 type_register_static(&virtio_serial_port_type_info);
957 device_init(virtio_serial_register_devices);