2 * A bus for connecting virtio serial and console ports
4 * Copyright (C) 2009, 2010 Red Hat, Inc.
7 * Amit Shah <amit.shah@redhat.com>
9 * Some earlier parts are:
10 * Copyright IBM, Corp. 2008
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.
23 #include "qemu-queue.h"
26 #include "virtio-serial.h"
27 #include "virtio-transport.h"
29 /* The virtio-serial bus on top of which the ports will ride as devices */
30 struct VirtIOSerialBus {
33 /* This is the parent device that provides the bus for ports. */
36 /* The maximum number of ports that can ride on top of this bus */
37 uint32_t max_nr_ports;
43 VirtQueue *c_ivq, *c_ovq;
44 /* Arrays of ivqs and ovqs: one per port */
45 VirtQueue **ivqs, **ovqs;
51 QTAILQ_HEAD(, VirtIOSerialPort) ports;
53 /* bitmap for identifying active ports */
56 struct virtio_console_config config;
59 static VirtIOSerialPort *find_port_by_id(VirtIOSerial *vser, uint32_t id)
61 VirtIOSerialPort *port;
63 if (id == VIRTIO_CONSOLE_BAD_ID) {
67 QTAILQ_FOREACH(port, &vser->ports, next) {
74 static VirtIOSerialPort *find_port_by_vq(VirtIOSerial *vser, VirtQueue *vq)
76 VirtIOSerialPort *port;
78 QTAILQ_FOREACH(port, &vser->ports, next) {
79 if (port->ivq == vq || port->ovq == vq)
85 static bool use_multiport(VirtIOSerial *vser)
87 return vser->vdev.guest_features & (1 << VIRTIO_CONSOLE_F_MULTIPORT);
90 static size_t write_to_port(VirtIOSerialPort *port,
91 const uint8_t *buf, size_t size)
93 VirtQueueElement elem;
98 if (!virtio_queue_ready(vq)) {
103 while (offset < size) {
106 if (!virtqueue_pop(vq, &elem)) {
110 len = iov_from_buf(elem.in_sg, elem.in_num, 0,
111 buf + offset, size - offset);
114 virtqueue_push(vq, &elem, len);
117 virtio_notify(&port->vser->vdev, vq);
121 static void discard_vq_data(VirtQueue *vq, VirtIODevice *vdev)
123 VirtQueueElement elem;
125 if (!virtio_queue_ready(vq)) {
128 while (virtqueue_pop(vq, &elem)) {
129 virtqueue_push(vq, &elem, 0);
131 virtio_notify(vdev, vq);
134 static void do_flush_queued_data(VirtIOSerialPort *port, VirtQueue *vq,
137 VirtIOSerialPortClass *vsc;
140 assert(virtio_queue_ready(vq));
142 vsc = VIRTIO_SERIAL_PORT_GET_CLASS(port);
144 while (!port->throttled) {
147 /* Pop an elem only if we haven't left off a previous one mid-way */
148 if (!port->elem.out_num) {
149 if (!virtqueue_pop(vq, &port->elem)) {
153 port->iov_offset = 0;
156 for (i = port->iov_idx; i < port->elem.out_num; i++) {
160 buf_size = port->elem.out_sg[i].iov_len - port->iov_offset;
161 ret = vsc->have_data(port,
162 port->elem.out_sg[i].iov_base
165 if (ret < 0 && ret != -EAGAIN) {
166 /* We don't handle any other type of errors here */
169 if (ret == -EAGAIN || (ret >= 0 && ret < buf_size)) {
171 * this is a temporary check until chardevs can signal to
172 * frontends that they are writable again. This prevents
173 * the console from going into throttled mode (forever)
174 * if virtio-console is connected to a pty without a
175 * listener. Otherwise the guest spins forever.
176 * We can revert this if
177 * 1: chardevs can notify frondends
178 * 2: the guest driver does not spin in these cases
180 if (!vsc->is_console) {
181 virtio_serial_throttle_port(port, true);
185 port->iov_offset += ret;
189 port->iov_offset = 0;
191 if (port->throttled) {
194 virtqueue_push(vq, &port->elem, 0);
195 port->elem.out_num = 0;
197 virtio_notify(vdev, vq);
200 static void flush_queued_data(VirtIOSerialPort *port)
204 if (!virtio_queue_ready(port->ovq)) {
207 do_flush_queued_data(port, port->ovq, &port->vser->vdev);
210 static size_t send_control_msg(VirtIOSerialPort *port, void *buf, size_t len)
212 VirtQueueElement elem;
214 struct virtio_console_control *cpkt;
216 vq = port->vser->c_ivq;
217 if (!virtio_queue_ready(vq)) {
220 if (!virtqueue_pop(vq, &elem)) {
224 cpkt = (struct virtio_console_control *)buf;
225 stl_p(&cpkt->id, port->id);
226 memcpy(elem.in_sg[0].iov_base, buf, len);
228 virtqueue_push(vq, &elem, len);
229 virtio_notify(&port->vser->vdev, vq);
233 static size_t send_control_event(VirtIOSerialPort *port, uint16_t event,
236 struct virtio_console_control cpkt;
238 stw_p(&cpkt.event, event);
239 stw_p(&cpkt.value, value);
241 trace_virtio_serial_send_control_event(port->id, event, value);
242 return send_control_msg(port, &cpkt, sizeof(cpkt));
245 /* Functions for use inside qemu to open and read from/write to ports */
246 int virtio_serial_open(VirtIOSerialPort *port)
248 /* Don't allow opening an already-open port */
249 if (port->host_connected) {
252 /* Send port open notification to the guest */
253 port->host_connected = true;
254 send_control_event(port, VIRTIO_CONSOLE_PORT_OPEN, 1);
259 int virtio_serial_close(VirtIOSerialPort *port)
261 port->host_connected = false;
263 * If there's any data the guest sent which the app didn't
264 * consume, reset the throttling flag and discard the data.
266 port->throttled = false;
267 discard_vq_data(port->ovq, &port->vser->vdev);
269 send_control_event(port, VIRTIO_CONSOLE_PORT_OPEN, 0);
274 /* Individual ports/apps call this function to write to the guest. */
275 ssize_t virtio_serial_write(VirtIOSerialPort *port, const uint8_t *buf,
278 if (!port || !port->host_connected || !port->guest_connected) {
281 return write_to_port(port, buf, size);
285 * Readiness of the guest to accept data on a port.
286 * Returns max. data the guest can receive
288 size_t virtio_serial_guest_ready(VirtIOSerialPort *port)
290 VirtQueue *vq = port->ivq;
292 if (!virtio_queue_ready(vq) ||
293 !(port->vser->vdev.status & VIRTIO_CONFIG_S_DRIVER_OK) ||
294 virtio_queue_empty(vq)) {
297 if (use_multiport(port->vser) && !port->guest_connected) {
301 if (virtqueue_avail_bytes(vq, 4096, 0)) {
304 if (virtqueue_avail_bytes(vq, 1, 0)) {
310 static void flush_queued_data_bh(void *opaque)
312 VirtIOSerialPort *port = opaque;
314 flush_queued_data(port);
317 void virtio_serial_throttle_port(VirtIOSerialPort *port, bool throttle)
323 trace_virtio_serial_throttle_port(port->id, throttle);
324 port->throttled = throttle;
328 qemu_bh_schedule(port->bh);
331 /* Guest wants to notify us of some event */
332 static void handle_control_message(VirtIOSerial *vser, void *buf, size_t len)
334 struct VirtIOSerialPort *port;
335 VirtIOSerialPortClass *vsc;
336 struct virtio_console_control cpkt, *gcpkt;
342 if (len < sizeof(cpkt)) {
343 /* The guest sent an invalid control packet */
347 cpkt.event = lduw_p(&gcpkt->event);
348 cpkt.value = lduw_p(&gcpkt->value);
350 trace_virtio_serial_handle_control_message(cpkt.event, cpkt.value);
352 if (cpkt.event == VIRTIO_CONSOLE_DEVICE_READY) {
354 error_report("virtio-serial-bus: Guest failure in adding device %s",
355 vser->bus.qbus.name);
359 * The device is up, we can now tell the device about all the
360 * ports we have here.
362 QTAILQ_FOREACH(port, &vser->ports, next) {
363 send_control_event(port, VIRTIO_CONSOLE_PORT_ADD, 1);
368 port = find_port_by_id(vser, ldl_p(&gcpkt->id));
370 error_report("virtio-serial-bus: Unexpected port id %u for device %s",
371 ldl_p(&gcpkt->id), vser->bus.qbus.name);
375 trace_virtio_serial_handle_control_message_port(port->id);
377 vsc = VIRTIO_SERIAL_PORT_GET_CLASS(port);
380 case VIRTIO_CONSOLE_PORT_READY:
382 error_report("virtio-serial-bus: Guest failure in adding port %u for device %s",
383 port->id, vser->bus.qbus.name);
387 * Now that we know the guest asked for the port name, we're
388 * sure the guest has initialised whatever state is necessary
389 * for this port. Now's a good time to let the guest know if
390 * this port is a console port so that the guest can hook it
393 if (vsc->is_console) {
394 send_control_event(port, VIRTIO_CONSOLE_CONSOLE_PORT, 1);
398 stw_p(&cpkt.event, VIRTIO_CONSOLE_PORT_NAME);
399 stw_p(&cpkt.value, 1);
401 buffer_len = sizeof(cpkt) + strlen(port->name) + 1;
402 buffer = g_malloc(buffer_len);
404 memcpy(buffer, &cpkt, sizeof(cpkt));
405 memcpy(buffer + sizeof(cpkt), port->name, strlen(port->name));
406 buffer[buffer_len - 1] = 0;
408 send_control_msg(port, buffer, buffer_len);
412 if (port->host_connected) {
413 send_control_event(port, VIRTIO_CONSOLE_PORT_OPEN, 1);
417 * When the guest has asked us for this information it means
418 * the guest is all setup and has its virtqueues
419 * initialised. If some app is interested in knowing about
420 * this event, let it know.
422 if (vsc->guest_ready) {
423 vsc->guest_ready(port);
427 case VIRTIO_CONSOLE_PORT_OPEN:
428 port->guest_connected = cpkt.value;
429 if (cpkt.value && vsc->guest_open) {
430 /* Send the guest opened notification if an app is interested */
431 vsc->guest_open(port);
434 if (!cpkt.value && vsc->guest_close) {
435 /* Send the guest closed notification if an app is interested */
436 vsc->guest_close(port);
442 static void control_in(VirtIODevice *vdev, VirtQueue *vq)
446 static void control_out(VirtIODevice *vdev, VirtQueue *vq)
448 VirtQueueElement elem;
453 vser = DO_UPCAST(VirtIOSerial, vdev, vdev);
457 while (virtqueue_pop(vq, &elem)) {
460 cur_len = iov_size(elem.out_sg, elem.out_num);
462 * Allocate a new buf only if we didn't have one previously or
463 * if the size of the buf differs
468 buf = g_malloc(cur_len);
471 iov_to_buf(elem.out_sg, elem.out_num, 0, buf, cur_len);
473 handle_control_message(vser, buf, cur_len);
474 virtqueue_push(vq, &elem, 0);
477 virtio_notify(vdev, vq);
480 /* Guest wrote something to some port. */
481 static void handle_output(VirtIODevice *vdev, VirtQueue *vq)
484 VirtIOSerialPort *port;
486 vser = DO_UPCAST(VirtIOSerial, vdev, vdev);
487 port = find_port_by_vq(vser, vq);
489 if (!port || !port->host_connected) {
490 discard_vq_data(vq, vdev);
494 if (!port->throttled) {
495 do_flush_queued_data(port, vq, vdev);
500 static void handle_input(VirtIODevice *vdev, VirtQueue *vq)
504 static uint32_t get_features(VirtIODevice *vdev, uint32_t features)
508 vser = DO_UPCAST(VirtIOSerial, vdev, vdev);
510 if (vser->bus.max_nr_ports > 1) {
511 features |= (1 << VIRTIO_CONSOLE_F_MULTIPORT);
516 /* Guest requested config info */
517 static void get_config(VirtIODevice *vdev, uint8_t *config_data)
521 vser = DO_UPCAST(VirtIOSerial, vdev, vdev);
522 memcpy(config_data, &vser->config, sizeof(struct virtio_console_config));
525 static void set_config(VirtIODevice *vdev, const uint8_t *config_data)
527 struct virtio_console_config config;
529 memcpy(&config, config_data, sizeof(config));
532 static void guest_reset(VirtIOSerial *vser)
534 VirtIOSerialPort *port;
535 VirtIOSerialPortClass *vsc;
537 QTAILQ_FOREACH(port, &vser->ports, next) {
538 vsc = VIRTIO_SERIAL_PORT_GET_CLASS(port);
539 if (port->guest_connected) {
540 port->guest_connected = false;
542 if (vsc->guest_close)
543 vsc->guest_close(port);
548 static void set_status(VirtIODevice *vdev, uint8_t status)
551 VirtIOSerialPort *port;
553 vser = DO_UPCAST(VirtIOSerial, vdev, vdev);
554 port = find_port_by_id(vser, 0);
556 if (port && !use_multiport(port->vser)
557 && (status & VIRTIO_CONFIG_S_DRIVER_OK)) {
559 * Non-multiport guests won't be able to tell us guest
560 * open/close status. Such guests can only have a port at id
561 * 0, so set guest_connected for such ports as soon as guest
564 port->guest_connected = true;
566 if (!(status & VIRTIO_CONFIG_S_DRIVER_OK)) {
571 static void vser_reset(VirtIODevice *vdev)
575 vser = DO_UPCAST(VirtIOSerial, vdev, vdev);
579 static void virtio_serial_save(QEMUFile *f, void *opaque)
581 VirtIOSerial *s = opaque;
582 VirtIOSerialPort *port;
583 uint32_t nr_active_ports;
584 unsigned int i, max_nr_ports;
586 /* The virtio device */
587 virtio_save(&s->vdev, f);
589 /* The config space */
590 qemu_put_be16s(f, &s->config.cols);
591 qemu_put_be16s(f, &s->config.rows);
593 qemu_put_be32s(f, &s->config.max_nr_ports);
596 max_nr_ports = tswap32(s->config.max_nr_ports);
597 for (i = 0; i < (max_nr_ports + 31) / 32; i++) {
598 qemu_put_be32s(f, &s->ports_map[i]);
604 QTAILQ_FOREACH(port, &s->ports, next) {
608 qemu_put_be32s(f, &nr_active_ports);
611 * Items in struct VirtIOSerialPort.
613 QTAILQ_FOREACH(port, &s->ports, next) {
614 uint32_t elem_popped;
616 qemu_put_be32s(f, &port->id);
617 qemu_put_byte(f, port->guest_connected);
618 qemu_put_byte(f, port->host_connected);
621 if (port->elem.out_num) {
624 qemu_put_be32s(f, &elem_popped);
626 qemu_put_be32s(f, &port->iov_idx);
627 qemu_put_be64s(f, &port->iov_offset);
629 qemu_put_buffer(f, (unsigned char *)&port->elem,
635 static int virtio_serial_load(QEMUFile *f, void *opaque, int version_id)
637 VirtIOSerial *s = opaque;
638 VirtIOSerialPort *port;
639 uint32_t max_nr_ports, nr_active_ports, ports_map;
643 if (version_id > 3) {
647 /* The virtio device */
648 ret = virtio_load(&s->vdev, f);
653 if (version_id < 2) {
657 /* The config space */
658 qemu_get_be16s(f, &s->config.cols);
659 qemu_get_be16s(f, &s->config.rows);
661 qemu_get_be32s(f, &max_nr_ports);
662 tswap32s(&max_nr_ports);
663 if (max_nr_ports > tswap32(s->config.max_nr_ports)) {
664 /* Source could have had more ports than us. Fail migration. */
668 for (i = 0; i < (max_nr_ports + 31) / 32; i++) {
669 qemu_get_be32s(f, &ports_map);
671 if (ports_map != s->ports_map[i]) {
673 * Ports active on source and destination don't
674 * match. Fail migration.
680 qemu_get_be32s(f, &nr_active_ports);
682 /* Items in struct VirtIOSerialPort */
683 for (i = 0; i < nr_active_ports; i++) {
687 id = qemu_get_be32(f);
688 port = find_port_by_id(s, id);
693 port->guest_connected = qemu_get_byte(f);
694 host_connected = qemu_get_byte(f);
695 if (host_connected != port->host_connected) {
697 * We have to let the guest know of the host connection
700 send_control_event(port, VIRTIO_CONSOLE_PORT_OPEN,
701 port->host_connected);
704 if (version_id > 2) {
705 uint32_t elem_popped;
707 qemu_get_be32s(f, &elem_popped);
709 qemu_get_be32s(f, &port->iov_idx);
710 qemu_get_be64s(f, &port->iov_offset);
712 qemu_get_buffer(f, (unsigned char *)&port->elem,
714 virtqueue_map_sg(port->elem.in_sg, port->elem.in_addr,
715 port->elem.in_num, 1);
716 virtqueue_map_sg(port->elem.out_sg, port->elem.out_addr,
717 port->elem.out_num, 1);
720 * Port was throttled on source machine. Let's
721 * unthrottle it here so data starts flowing again.
723 virtio_serial_throttle_port(port, false);
730 static void virtser_bus_dev_print(Monitor *mon, DeviceState *qdev, int indent);
732 static Property virtser_props[] = {
733 DEFINE_PROP_UINT32("nr", VirtIOSerialPort, id, VIRTIO_CONSOLE_BAD_ID),
734 DEFINE_PROP_STRING("name", VirtIOSerialPort, name),
735 DEFINE_PROP_END_OF_LIST()
738 #define TYPE_VIRTIO_SERIAL_BUS "virtio-serial-bus"
739 #define VIRTIO_SERIAL_BUS(obj) \
740 OBJECT_CHECK(VirtIOSerialBus, (obj), TYPE_VIRTIO_SERIAL_BUS)
742 static void virtser_bus_class_init(ObjectClass *klass, void *data)
744 BusClass *k = BUS_CLASS(klass);
745 k->print_dev = virtser_bus_dev_print;
748 static const TypeInfo virtser_bus_info = {
749 .name = TYPE_VIRTIO_SERIAL_BUS,
751 .instance_size = sizeof(VirtIOSerialBus),
752 .class_init = virtser_bus_class_init,
755 static void virtser_bus_dev_print(Monitor *mon, DeviceState *qdev, int indent)
757 VirtIOSerialPort *port = DO_UPCAST(VirtIOSerialPort, dev, qdev);
759 monitor_printf(mon, "%*sport %d, guest %s, host %s, throttle %s\n",
760 indent, "", port->id,
761 port->guest_connected ? "on" : "off",
762 port->host_connected ? "on" : "off",
763 port->throttled ? "on" : "off");
766 /* This function is only used if a port id is not provided by the user */
767 static uint32_t find_free_port_id(VirtIOSerial *vser)
769 unsigned int i, max_nr_ports;
771 max_nr_ports = tswap32(vser->config.max_nr_ports);
772 for (i = 0; i < (max_nr_ports + 31) / 32; i++) {
775 map = vser->ports_map[i];
778 return (bit - 1) + i * 32;
781 return VIRTIO_CONSOLE_BAD_ID;
784 static void mark_port_added(VirtIOSerial *vser, uint32_t port_id)
789 vser->ports_map[i] |= 1U << (port_id % 32);
792 static void add_port(VirtIOSerial *vser, uint32_t port_id)
794 mark_port_added(vser, port_id);
796 send_control_event(find_port_by_id(vser, port_id),
797 VIRTIO_CONSOLE_PORT_ADD, 1);
800 static void remove_port(VirtIOSerial *vser, uint32_t port_id)
802 VirtIOSerialPort *port;
806 vser->ports_map[i] &= ~(1U << (port_id % 32));
808 port = find_port_by_id(vser, port_id);
809 /* Flush out any unconsumed buffers first */
810 discard_vq_data(port->ovq, &port->vser->vdev);
812 send_control_event(port, VIRTIO_CONSOLE_PORT_REMOVE, 1);
815 static int virtser_port_qdev_init(DeviceState *qdev)
817 VirtIOSerialPort *port = DO_UPCAST(VirtIOSerialPort, dev, qdev);
818 VirtIOSerialPortClass *vsc = VIRTIO_SERIAL_PORT_GET_CLASS(port);
819 VirtIOSerialBus *bus = DO_UPCAST(VirtIOSerialBus, qbus, qdev->parent_bus);
820 int ret, max_nr_ports;
823 port->vser = bus->vser;
824 port->bh = qemu_bh_new(flush_queued_data_bh, port);
826 assert(vsc->have_data);
829 * Is the first console port we're seeing? If so, put it up at
830 * location 0. This is done for backward compatibility (old
833 plugging_port0 = vsc->is_console && !find_port_by_id(port->vser, 0);
835 if (find_port_by_id(port->vser, port->id)) {
836 error_report("virtio-serial-bus: A port already exists at id %u",
841 if (port->id == VIRTIO_CONSOLE_BAD_ID) {
842 if (plugging_port0) {
845 port->id = find_free_port_id(port->vser);
846 if (port->id == VIRTIO_CONSOLE_BAD_ID) {
847 error_report("virtio-serial-bus: Maximum port limit for this device reached");
853 max_nr_ports = tswap32(port->vser->config.max_nr_ports);
854 if (port->id >= max_nr_ports) {
855 error_report("virtio-serial-bus: Out-of-range port id specified, max. allowed: %u",
860 ret = vsc->init(port);
865 port->elem.out_num = 0;
867 QTAILQ_INSERT_TAIL(&port->vser->ports, port, next);
868 port->ivq = port->vser->ivqs[port->id];
869 port->ovq = port->vser->ovqs[port->id];
871 add_port(port->vser, port->id);
873 /* Send an update to the guest about this new port added */
874 virtio_notify_config(&port->vser->vdev);
879 static int virtser_port_qdev_exit(DeviceState *qdev)
881 VirtIOSerialPort *port = DO_UPCAST(VirtIOSerialPort, dev, qdev);
882 VirtIOSerialPortClass *vsc = VIRTIO_SERIAL_PORT_GET_CLASS(port);
883 VirtIOSerial *vser = port->vser;
885 qemu_bh_delete(port->bh);
886 remove_port(port->vser, port->id);
888 QTAILQ_REMOVE(&vser->ports, port, next);
896 VirtIODevice *virtio_serial_init(DeviceState *dev, virtio_serial_conf *conf)
900 uint32_t i, max_supported_ports;
902 if (!conf->max_virtserial_ports)
905 /* Each port takes 2 queues, and one pair is for the control queue */
906 max_supported_ports = VIRTIO_PCI_QUEUE_MAX / 2 - 1;
908 if (conf->max_virtserial_ports > max_supported_ports) {
909 error_report("maximum ports supported: %u", max_supported_ports);
913 vdev = virtio_common_init("virtio-serial", VIRTIO_ID_CONSOLE,
914 sizeof(struct virtio_console_config),
915 sizeof(VirtIOSerial));
917 vser = DO_UPCAST(VirtIOSerial, vdev, vdev);
919 /* Spawn a new virtio-serial bus on which the ports will ride as devices */
920 qbus_create_inplace(&vser->bus.qbus, TYPE_VIRTIO_SERIAL_BUS, dev, NULL);
921 vser->bus.qbus.allow_hotplug = 1;
922 vser->bus.vser = vser;
923 QTAILQ_INIT(&vser->ports);
925 vser->bus.max_nr_ports = conf->max_virtserial_ports;
926 vser->ivqs = g_malloc(conf->max_virtserial_ports * sizeof(VirtQueue *));
927 vser->ovqs = g_malloc(conf->max_virtserial_ports * sizeof(VirtQueue *));
929 /* Add a queue for host to guest transfers for port 0 (backward compat) */
930 vser->ivqs[0] = virtio_add_queue(vdev, 128, handle_input);
931 /* Add a queue for guest to host transfers for port 0 (backward compat) */
932 vser->ovqs[0] = virtio_add_queue(vdev, 128, handle_output);
934 /* TODO: host to guest notifications can get dropped
935 * if the queue fills up. Implement queueing in host,
936 * this might also make it possible to reduce the control
937 * queue size: as guest preposts buffers there,
938 * this will save 4Kbyte of guest memory per entry. */
940 /* control queue: host to guest */
941 vser->c_ivq = virtio_add_queue(vdev, 32, control_in);
942 /* control queue: guest to host */
943 vser->c_ovq = virtio_add_queue(vdev, 32, control_out);
945 for (i = 1; i < vser->bus.max_nr_ports; i++) {
946 /* Add a per-port queue for host to guest transfers */
947 vser->ivqs[i] = virtio_add_queue(vdev, 128, handle_input);
948 /* Add a per-per queue for guest to host transfers */
949 vser->ovqs[i] = virtio_add_queue(vdev, 128, handle_output);
952 vser->config.max_nr_ports = tswap32(conf->max_virtserial_ports);
953 vser->ports_map = g_malloc0(((conf->max_virtserial_ports + 31) / 32)
954 * sizeof(vser->ports_map[0]));
956 * Reserve location 0 for a console port for backward compat
957 * (old kernel, new qemu)
959 mark_port_added(vser, 0);
961 vser->vdev.get_features = get_features;
962 vser->vdev.get_config = get_config;
963 vser->vdev.set_config = set_config;
964 vser->vdev.set_status = set_status;
965 vser->vdev.reset = vser_reset;
970 * Register for the savevm section with the virtio-console name
971 * to preserve backward compat
973 register_savevm(dev, "virtio-console", -1, 3, virtio_serial_save,
974 virtio_serial_load, vser);
979 void virtio_serial_exit(VirtIODevice *vdev)
981 VirtIOSerial *vser = DO_UPCAST(VirtIOSerial, vdev, vdev);
983 unregister_savevm(vser->qdev, "virtio-console", vser);
987 g_free(vser->ports_map);
989 virtio_cleanup(vdev);
992 static void virtio_serial_port_class_init(ObjectClass *klass, void *data)
994 DeviceClass *k = DEVICE_CLASS(klass);
995 k->init = virtser_port_qdev_init;
996 k->bus_type = TYPE_VIRTIO_SERIAL_BUS;
997 k->exit = virtser_port_qdev_exit;
998 k->unplug = qdev_simple_unplug_cb;
999 k->props = virtser_props;
1002 static TypeInfo virtio_serial_port_type_info = {
1003 .name = TYPE_VIRTIO_SERIAL_PORT,
1004 .parent = TYPE_DEVICE,
1005 .instance_size = sizeof(VirtIOSerialPort),
1007 .class_size = sizeof(VirtIOSerialPortClass),
1008 .class_init = virtio_serial_port_class_init,
1011 static void virtio_serial_register_types(void)
1013 type_register_static(&virtser_bus_info);
1014 type_register_static(&virtio_serial_port_type_info);
1017 type_init(virtio_serial_register_types)
1019 /******************** VirtIOSer Device **********************/
1021 static int virtio_serialdev_init(DeviceState *dev)
1024 VirtIOSerState *s = VIRTIO_SERIAL_FROM_QDEV(dev);
1025 vdev = virtio_serial_init(dev, &s->serial);
1030 assert(s->trl != NULL);
1032 return virtio_call_backend_init_cb(dev, s->trl, vdev);
1035 static Property virtio_serial_properties[] = {
1036 DEFINE_PROP_UINT32("max_ports", VirtIOSerState,
1037 serial.max_virtserial_ports, 31),
1038 DEFINE_PROP_END_OF_LIST(),
1041 static void virtio_serial_class_init(ObjectClass *klass, void *data)
1043 DeviceClass *dc = DEVICE_CLASS(klass);
1044 dc->init = virtio_serialdev_init;
1045 dc->props = virtio_serial_properties;
1048 static TypeInfo virtio_serial_info = {
1049 .name = "virtio-serial",
1050 .parent = TYPE_DEVICE,
1051 .instance_size = sizeof(VirtIOSerState),
1052 .class_init = virtio_serial_class_init,
1055 static void virtio_ser_register_types(void)
1057 type_register_static(&virtio_serial_info);
1060 type_init(virtio_ser_register_types)