qemu-ga: qmp_guest_network_get_interfaces(): get rid of snprintf() + error_set()
[qemu/ar7.git] / hw / virtio-serial-bus.c
blob7272bfd5fe111f147b2fb752876ddf1a39e53d41
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 "qemu/iov.h"
22 #include "monitor/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 typedef struct VirtIOSerialPostLoad {
40 QEMUTimer *timer;
41 uint32_t nr_active_ports;
42 struct {
43 VirtIOSerialPort *port;
44 uint8_t host_connected;
45 } *connected;
46 } VirtIOSerialPostLoad;
48 struct VirtIOSerial {
49 VirtIODevice vdev;
51 VirtQueue *c_ivq, *c_ovq;
52 /* Arrays of ivqs and ovqs: one per port */
53 VirtQueue **ivqs, **ovqs;
55 VirtIOSerialBus bus;
57 DeviceState *qdev;
59 QTAILQ_HEAD(, VirtIOSerialPort) ports;
61 /* bitmap for identifying active ports */
62 uint32_t *ports_map;
64 struct virtio_console_config config;
66 struct VirtIOSerialPostLoad *post_load;
69 static VirtIOSerialPort *find_port_by_id(VirtIOSerial *vser, uint32_t id)
71 VirtIOSerialPort *port;
73 if (id == VIRTIO_CONSOLE_BAD_ID) {
74 return NULL;
77 QTAILQ_FOREACH(port, &vser->ports, next) {
78 if (port->id == id)
79 return port;
81 return NULL;
84 static VirtIOSerialPort *find_port_by_vq(VirtIOSerial *vser, VirtQueue *vq)
86 VirtIOSerialPort *port;
88 QTAILQ_FOREACH(port, &vser->ports, next) {
89 if (port->ivq == vq || port->ovq == vq)
90 return port;
92 return NULL;
95 static bool use_multiport(VirtIOSerial *vser)
97 return vser->vdev.guest_features & (1 << VIRTIO_CONSOLE_F_MULTIPORT);
100 static size_t write_to_port(VirtIOSerialPort *port,
101 const uint8_t *buf, size_t size)
103 VirtQueueElement elem;
104 VirtQueue *vq;
105 size_t offset;
107 vq = port->ivq;
108 if (!virtio_queue_ready(vq)) {
109 return 0;
112 offset = 0;
113 while (offset < size) {
114 size_t len;
116 if (!virtqueue_pop(vq, &elem)) {
117 break;
120 len = iov_from_buf(elem.in_sg, elem.in_num, 0,
121 buf + offset, size - offset);
122 offset += len;
124 virtqueue_push(vq, &elem, len);
127 virtio_notify(&port->vser->vdev, vq);
128 return offset;
131 static void discard_vq_data(VirtQueue *vq, VirtIODevice *vdev)
133 VirtQueueElement elem;
135 if (!virtio_queue_ready(vq)) {
136 return;
138 while (virtqueue_pop(vq, &elem)) {
139 virtqueue_push(vq, &elem, 0);
141 virtio_notify(vdev, vq);
144 static void do_flush_queued_data(VirtIOSerialPort *port, VirtQueue *vq,
145 VirtIODevice *vdev)
147 VirtIOSerialPortClass *vsc;
149 assert(port);
150 assert(virtio_queue_ready(vq));
152 vsc = VIRTIO_SERIAL_PORT_GET_CLASS(port);
154 while (!port->throttled) {
155 unsigned int i;
157 /* Pop an elem only if we haven't left off a previous one mid-way */
158 if (!port->elem.out_num) {
159 if (!virtqueue_pop(vq, &port->elem)) {
160 break;
162 port->iov_idx = 0;
163 port->iov_offset = 0;
166 for (i = port->iov_idx; i < port->elem.out_num; i++) {
167 size_t buf_size;
168 ssize_t ret;
170 buf_size = port->elem.out_sg[i].iov_len - port->iov_offset;
171 ret = vsc->have_data(port,
172 port->elem.out_sg[i].iov_base
173 + port->iov_offset,
174 buf_size);
175 if (ret < 0 && ret != -EAGAIN) {
176 /* We don't handle any other type of errors here */
177 abort();
179 if (ret == -EAGAIN || (ret >= 0 && ret < buf_size)) {
181 * this is a temporary check until chardevs can signal to
182 * frontends that they are writable again. This prevents
183 * the console from going into throttled mode (forever)
184 * if virtio-console is connected to a pty without a
185 * listener. Otherwise the guest spins forever.
186 * We can revert this if
187 * 1: chardevs can notify frondends
188 * 2: the guest driver does not spin in these cases
190 if (!vsc->is_console) {
191 virtio_serial_throttle_port(port, true);
193 port->iov_idx = i;
194 if (ret > 0) {
195 port->iov_offset += ret;
197 break;
199 port->iov_offset = 0;
201 if (port->throttled) {
202 break;
204 virtqueue_push(vq, &port->elem, 0);
205 port->elem.out_num = 0;
207 virtio_notify(vdev, vq);
210 static void flush_queued_data(VirtIOSerialPort *port)
212 assert(port);
214 if (!virtio_queue_ready(port->ovq)) {
215 return;
217 do_flush_queued_data(port, port->ovq, &port->vser->vdev);
220 static size_t send_control_msg(VirtIOSerial *vser, void *buf, size_t len)
222 VirtQueueElement elem;
223 VirtQueue *vq;
225 vq = vser->c_ivq;
226 if (!virtio_queue_ready(vq)) {
227 return 0;
229 if (!virtqueue_pop(vq, &elem)) {
230 return 0;
233 memcpy(elem.in_sg[0].iov_base, buf, len);
235 virtqueue_push(vq, &elem, len);
236 virtio_notify(&vser->vdev, vq);
237 return len;
240 static size_t send_control_event(VirtIOSerial *vser, uint32_t port_id,
241 uint16_t event, uint16_t value)
243 struct virtio_console_control cpkt;
245 stl_p(&cpkt.id, port_id);
246 stw_p(&cpkt.event, event);
247 stw_p(&cpkt.value, value);
249 trace_virtio_serial_send_control_event(port_id, event, value);
250 return send_control_msg(vser, &cpkt, sizeof(cpkt));
253 /* Functions for use inside qemu to open and read from/write to ports */
254 int virtio_serial_open(VirtIOSerialPort *port)
256 /* Don't allow opening an already-open port */
257 if (port->host_connected) {
258 return 0;
260 /* Send port open notification to the guest */
261 port->host_connected = true;
262 send_control_event(port->vser, port->id, VIRTIO_CONSOLE_PORT_OPEN, 1);
264 return 0;
267 int virtio_serial_close(VirtIOSerialPort *port)
269 port->host_connected = false;
271 * If there's any data the guest sent which the app didn't
272 * consume, reset the throttling flag and discard the data.
274 port->throttled = false;
275 discard_vq_data(port->ovq, &port->vser->vdev);
277 send_control_event(port->vser, port->id, VIRTIO_CONSOLE_PORT_OPEN, 0);
279 return 0;
282 /* Individual ports/apps call this function to write to the guest. */
283 ssize_t virtio_serial_write(VirtIOSerialPort *port, const uint8_t *buf,
284 size_t size)
286 if (!port || !port->host_connected || !port->guest_connected) {
287 return 0;
289 return write_to_port(port, buf, size);
293 * Readiness of the guest to accept data on a port.
294 * Returns max. data the guest can receive
296 size_t virtio_serial_guest_ready(VirtIOSerialPort *port)
298 VirtQueue *vq = port->ivq;
299 unsigned int bytes;
301 if (!virtio_queue_ready(vq) ||
302 !(port->vser->vdev.status & VIRTIO_CONFIG_S_DRIVER_OK) ||
303 virtio_queue_empty(vq)) {
304 return 0;
306 if (use_multiport(port->vser) && !port->guest_connected) {
307 return 0;
309 virtqueue_get_avail_bytes(vq, &bytes, NULL, 4096, 0);
310 return bytes;
313 static void flush_queued_data_bh(void *opaque)
315 VirtIOSerialPort *port = opaque;
317 flush_queued_data(port);
320 void virtio_serial_throttle_port(VirtIOSerialPort *port, bool throttle)
322 if (!port) {
323 return;
326 trace_virtio_serial_throttle_port(port->id, throttle);
327 port->throttled = throttle;
328 if (throttle) {
329 return;
331 qemu_bh_schedule(port->bh);
334 /* Guest wants to notify us of some event */
335 static void handle_control_message(VirtIOSerial *vser, void *buf, size_t len)
337 struct VirtIOSerialPort *port;
338 VirtIOSerialPortClass *vsc;
339 struct virtio_console_control cpkt, *gcpkt;
340 uint8_t *buffer;
341 size_t buffer_len;
343 gcpkt = buf;
345 if (len < sizeof(cpkt)) {
346 /* The guest sent an invalid control packet */
347 return;
350 cpkt.event = lduw_p(&gcpkt->event);
351 cpkt.value = lduw_p(&gcpkt->value);
353 trace_virtio_serial_handle_control_message(cpkt.event, cpkt.value);
355 if (cpkt.event == VIRTIO_CONSOLE_DEVICE_READY) {
356 if (!cpkt.value) {
357 error_report("virtio-serial-bus: Guest failure in adding device %s",
358 vser->bus.qbus.name);
359 return;
362 * The device is up, we can now tell the device about all the
363 * ports we have here.
365 QTAILQ_FOREACH(port, &vser->ports, next) {
366 send_control_event(vser, port->id, VIRTIO_CONSOLE_PORT_ADD, 1);
368 return;
371 port = find_port_by_id(vser, ldl_p(&gcpkt->id));
372 if (!port) {
373 error_report("virtio-serial-bus: Unexpected port id %u for device %s",
374 ldl_p(&gcpkt->id), vser->bus.qbus.name);
375 return;
378 trace_virtio_serial_handle_control_message_port(port->id);
380 vsc = VIRTIO_SERIAL_PORT_GET_CLASS(port);
382 switch(cpkt.event) {
383 case VIRTIO_CONSOLE_PORT_READY:
384 if (!cpkt.value) {
385 error_report("virtio-serial-bus: Guest failure in adding port %u for device %s",
386 port->id, vser->bus.qbus.name);
387 break;
390 * Now that we know the guest asked for the port name, we're
391 * sure the guest has initialised whatever state is necessary
392 * for this port. Now's a good time to let the guest know if
393 * this port is a console port so that the guest can hook it
394 * up to hvc.
396 if (vsc->is_console) {
397 send_control_event(vser, port->id, VIRTIO_CONSOLE_CONSOLE_PORT, 1);
400 if (port->name) {
401 stl_p(&cpkt.id, port->id);
402 stw_p(&cpkt.event, VIRTIO_CONSOLE_PORT_NAME);
403 stw_p(&cpkt.value, 1);
405 buffer_len = sizeof(cpkt) + strlen(port->name) + 1;
406 buffer = g_malloc(buffer_len);
408 memcpy(buffer, &cpkt, sizeof(cpkt));
409 memcpy(buffer + sizeof(cpkt), port->name, strlen(port->name));
410 buffer[buffer_len - 1] = 0;
412 send_control_msg(vser, buffer, buffer_len);
413 g_free(buffer);
416 if (port->host_connected) {
417 send_control_event(vser, port->id, VIRTIO_CONSOLE_PORT_OPEN, 1);
421 * When the guest has asked us for this information it means
422 * the guest is all setup and has its virtqueues
423 * initialised. If some app is interested in knowing about
424 * this event, let it know.
426 if (vsc->guest_ready) {
427 vsc->guest_ready(port);
429 break;
431 case VIRTIO_CONSOLE_PORT_OPEN:
432 port->guest_connected = cpkt.value;
433 if (cpkt.value && vsc->guest_open) {
434 /* Send the guest opened notification if an app is interested */
435 vsc->guest_open(port);
438 if (!cpkt.value && vsc->guest_close) {
439 /* Send the guest closed notification if an app is interested */
440 vsc->guest_close(port);
442 break;
446 static void control_in(VirtIODevice *vdev, VirtQueue *vq)
450 static void control_out(VirtIODevice *vdev, VirtQueue *vq)
452 VirtQueueElement elem;
453 VirtIOSerial *vser;
454 uint8_t *buf;
455 size_t len;
457 vser = DO_UPCAST(VirtIOSerial, vdev, vdev);
459 len = 0;
460 buf = NULL;
461 while (virtqueue_pop(vq, &elem)) {
462 size_t cur_len;
464 cur_len = iov_size(elem.out_sg, elem.out_num);
466 * Allocate a new buf only if we didn't have one previously or
467 * if the size of the buf differs
469 if (cur_len > len) {
470 g_free(buf);
472 buf = g_malloc(cur_len);
473 len = cur_len;
475 iov_to_buf(elem.out_sg, elem.out_num, 0, buf, cur_len);
477 handle_control_message(vser, buf, cur_len);
478 virtqueue_push(vq, &elem, 0);
480 g_free(buf);
481 virtio_notify(vdev, vq);
484 /* Guest wrote something to some port. */
485 static void handle_output(VirtIODevice *vdev, VirtQueue *vq)
487 VirtIOSerial *vser;
488 VirtIOSerialPort *port;
490 vser = DO_UPCAST(VirtIOSerial, vdev, vdev);
491 port = find_port_by_vq(vser, vq);
493 if (!port || !port->host_connected) {
494 discard_vq_data(vq, vdev);
495 return;
498 if (!port->throttled) {
499 do_flush_queued_data(port, vq, vdev);
500 return;
504 static void handle_input(VirtIODevice *vdev, VirtQueue *vq)
508 static uint32_t get_features(VirtIODevice *vdev, uint32_t features)
510 VirtIOSerial *vser;
512 vser = DO_UPCAST(VirtIOSerial, vdev, vdev);
514 if (vser->bus.max_nr_ports > 1) {
515 features |= (1 << VIRTIO_CONSOLE_F_MULTIPORT);
517 return features;
520 /* Guest requested config info */
521 static void get_config(VirtIODevice *vdev, uint8_t *config_data)
523 VirtIOSerial *vser;
525 vser = DO_UPCAST(VirtIOSerial, vdev, vdev);
526 memcpy(config_data, &vser->config, sizeof(struct virtio_console_config));
529 static void set_config(VirtIODevice *vdev, const uint8_t *config_data)
531 struct virtio_console_config config;
533 memcpy(&config, config_data, sizeof(config));
536 static void guest_reset(VirtIOSerial *vser)
538 VirtIOSerialPort *port;
539 VirtIOSerialPortClass *vsc;
541 QTAILQ_FOREACH(port, &vser->ports, next) {
542 vsc = VIRTIO_SERIAL_PORT_GET_CLASS(port);
543 if (port->guest_connected) {
544 port->guest_connected = false;
546 if (vsc->guest_close)
547 vsc->guest_close(port);
552 static void set_status(VirtIODevice *vdev, uint8_t status)
554 VirtIOSerial *vser;
555 VirtIOSerialPort *port;
557 vser = DO_UPCAST(VirtIOSerial, vdev, vdev);
558 port = find_port_by_id(vser, 0);
560 if (port && !use_multiport(port->vser)
561 && (status & VIRTIO_CONFIG_S_DRIVER_OK)) {
563 * Non-multiport guests won't be able to tell us guest
564 * open/close status. Such guests can only have a port at id
565 * 0, so set guest_connected for such ports as soon as guest
566 * is up.
568 port->guest_connected = true;
570 if (!(status & VIRTIO_CONFIG_S_DRIVER_OK)) {
571 guest_reset(vser);
575 static void vser_reset(VirtIODevice *vdev)
577 VirtIOSerial *vser;
579 vser = DO_UPCAST(VirtIOSerial, vdev, vdev);
580 guest_reset(vser);
583 static void virtio_serial_save(QEMUFile *f, void *opaque)
585 VirtIOSerial *s = opaque;
586 VirtIOSerialPort *port;
587 uint32_t nr_active_ports;
588 unsigned int i, max_nr_ports;
590 /* The virtio device */
591 virtio_save(&s->vdev, f);
593 /* The config space */
594 qemu_put_be16s(f, &s->config.cols);
595 qemu_put_be16s(f, &s->config.rows);
597 qemu_put_be32s(f, &s->config.max_nr_ports);
599 /* The ports map */
600 max_nr_ports = tswap32(s->config.max_nr_ports);
601 for (i = 0; i < (max_nr_ports + 31) / 32; i++) {
602 qemu_put_be32s(f, &s->ports_map[i]);
605 /* Ports */
607 nr_active_ports = 0;
608 QTAILQ_FOREACH(port, &s->ports, next) {
609 nr_active_ports++;
612 qemu_put_be32s(f, &nr_active_ports);
615 * Items in struct VirtIOSerialPort.
617 QTAILQ_FOREACH(port, &s->ports, next) {
618 uint32_t elem_popped;
620 qemu_put_be32s(f, &port->id);
621 qemu_put_byte(f, port->guest_connected);
622 qemu_put_byte(f, port->host_connected);
624 elem_popped = 0;
625 if (port->elem.out_num) {
626 elem_popped = 1;
628 qemu_put_be32s(f, &elem_popped);
629 if (elem_popped) {
630 qemu_put_be32s(f, &port->iov_idx);
631 qemu_put_be64s(f, &port->iov_offset);
633 qemu_put_buffer(f, (unsigned char *)&port->elem,
634 sizeof(port->elem));
639 static void virtio_serial_post_load_timer_cb(void *opaque)
641 uint32_t i;
642 VirtIOSerial *s = opaque;
643 VirtIOSerialPort *port;
644 uint8_t host_connected;
646 if (!s->post_load) {
647 return;
649 for (i = 0 ; i < s->post_load->nr_active_ports; ++i) {
650 port = s->post_load->connected[i].port;
651 host_connected = s->post_load->connected[i].host_connected;
652 if (host_connected != port->host_connected) {
654 * We have to let the guest know of the host connection
655 * status change
657 send_control_event(s, port->id, VIRTIO_CONSOLE_PORT_OPEN,
658 port->host_connected);
661 g_free(s->post_load->connected);
662 qemu_free_timer(s->post_load->timer);
663 g_free(s->post_load);
664 s->post_load = NULL;
667 static int fetch_active_ports_list(QEMUFile *f, int version_id,
668 VirtIOSerial *s, uint32_t nr_active_ports)
670 uint32_t i;
672 s->post_load = g_malloc0(sizeof(*s->post_load));
673 s->post_load->nr_active_ports = nr_active_ports;
674 s->post_load->connected =
675 g_malloc0(sizeof(*s->post_load->connected) * nr_active_ports);
677 s->post_load->timer = qemu_new_timer_ns(vm_clock,
678 virtio_serial_post_load_timer_cb,
681 /* Items in struct VirtIOSerialPort */
682 for (i = 0; i < nr_active_ports; i++) {
683 VirtIOSerialPort *port;
684 uint32_t id;
686 id = qemu_get_be32(f);
687 port = find_port_by_id(s, id);
688 if (!port) {
689 return -EINVAL;
692 port->guest_connected = qemu_get_byte(f);
693 s->post_load->connected[i].port = port;
694 s->post_load->connected[i].host_connected = qemu_get_byte(f);
696 if (version_id > 2) {
697 uint32_t elem_popped;
699 qemu_get_be32s(f, &elem_popped);
700 if (elem_popped) {
701 qemu_get_be32s(f, &port->iov_idx);
702 qemu_get_be64s(f, &port->iov_offset);
704 qemu_get_buffer(f, (unsigned char *)&port->elem,
705 sizeof(port->elem));
706 virtqueue_map_sg(port->elem.in_sg, port->elem.in_addr,
707 port->elem.in_num, 1);
708 virtqueue_map_sg(port->elem.out_sg, port->elem.out_addr,
709 port->elem.out_num, 1);
712 * Port was throttled on source machine. Let's
713 * unthrottle it here so data starts flowing again.
715 virtio_serial_throttle_port(port, false);
719 qemu_mod_timer(s->post_load->timer, 1);
720 return 0;
723 static int virtio_serial_load(QEMUFile *f, void *opaque, int version_id)
725 VirtIOSerial *s = opaque;
726 uint32_t max_nr_ports, nr_active_ports, ports_map;
727 unsigned int i;
728 int ret;
730 if (version_id > 3) {
731 return -EINVAL;
734 /* The virtio device */
735 ret = virtio_load(&s->vdev, f);
736 if (ret) {
737 return ret;
740 if (version_id < 2) {
741 return 0;
744 /* The config space */
745 qemu_get_be16s(f, &s->config.cols);
746 qemu_get_be16s(f, &s->config.rows);
748 qemu_get_be32s(f, &max_nr_ports);
749 tswap32s(&max_nr_ports);
750 if (max_nr_ports > tswap32(s->config.max_nr_ports)) {
751 /* Source could have had more ports than us. Fail migration. */
752 return -EINVAL;
755 for (i = 0; i < (max_nr_ports + 31) / 32; i++) {
756 qemu_get_be32s(f, &ports_map);
758 if (ports_map != s->ports_map[i]) {
760 * Ports active on source and destination don't
761 * match. Fail migration.
763 return -EINVAL;
767 qemu_get_be32s(f, &nr_active_ports);
769 if (nr_active_ports) {
770 ret = fetch_active_ports_list(f, version_id, s, nr_active_ports);
771 if (ret) {
772 return ret;
775 return 0;
778 static void virtser_bus_dev_print(Monitor *mon, DeviceState *qdev, int indent);
780 static Property virtser_props[] = {
781 DEFINE_PROP_UINT32("nr", VirtIOSerialPort, id, VIRTIO_CONSOLE_BAD_ID),
782 DEFINE_PROP_STRING("name", VirtIOSerialPort, name),
783 DEFINE_PROP_END_OF_LIST()
786 #define TYPE_VIRTIO_SERIAL_BUS "virtio-serial-bus"
787 #define VIRTIO_SERIAL_BUS(obj) \
788 OBJECT_CHECK(VirtIOSerialBus, (obj), TYPE_VIRTIO_SERIAL_BUS)
790 static void virtser_bus_class_init(ObjectClass *klass, void *data)
792 BusClass *k = BUS_CLASS(klass);
793 k->print_dev = virtser_bus_dev_print;
796 static const TypeInfo virtser_bus_info = {
797 .name = TYPE_VIRTIO_SERIAL_BUS,
798 .parent = TYPE_BUS,
799 .instance_size = sizeof(VirtIOSerialBus),
800 .class_init = virtser_bus_class_init,
803 static void virtser_bus_dev_print(Monitor *mon, DeviceState *qdev, int indent)
805 VirtIOSerialPort *port = DO_UPCAST(VirtIOSerialPort, dev, qdev);
807 monitor_printf(mon, "%*sport %d, guest %s, host %s, throttle %s\n",
808 indent, "", port->id,
809 port->guest_connected ? "on" : "off",
810 port->host_connected ? "on" : "off",
811 port->throttled ? "on" : "off");
814 /* This function is only used if a port id is not provided by the user */
815 static uint32_t find_free_port_id(VirtIOSerial *vser)
817 unsigned int i, max_nr_ports;
819 max_nr_ports = tswap32(vser->config.max_nr_ports);
820 for (i = 0; i < (max_nr_ports + 31) / 32; i++) {
821 uint32_t map, bit;
823 map = vser->ports_map[i];
824 bit = ffs(~map);
825 if (bit) {
826 return (bit - 1) + i * 32;
829 return VIRTIO_CONSOLE_BAD_ID;
832 static void mark_port_added(VirtIOSerial *vser, uint32_t port_id)
834 unsigned int i;
836 i = port_id / 32;
837 vser->ports_map[i] |= 1U << (port_id % 32);
840 static void add_port(VirtIOSerial *vser, uint32_t port_id)
842 mark_port_added(vser, port_id);
843 send_control_event(vser, port_id, VIRTIO_CONSOLE_PORT_ADD, 1);
846 static void remove_port(VirtIOSerial *vser, uint32_t port_id)
848 VirtIOSerialPort *port;
849 unsigned int i;
851 i = port_id / 32;
852 vser->ports_map[i] &= ~(1U << (port_id % 32));
854 port = find_port_by_id(vser, port_id);
856 * This function is only called from qdev's unplug callback; if we
857 * get a NULL port here, we're in trouble.
859 assert(port);
861 /* Flush out any unconsumed buffers first */
862 discard_vq_data(port->ovq, &port->vser->vdev);
864 send_control_event(vser, port->id, VIRTIO_CONSOLE_PORT_REMOVE, 1);
867 static int virtser_port_qdev_init(DeviceState *qdev)
869 VirtIOSerialPort *port = DO_UPCAST(VirtIOSerialPort, dev, qdev);
870 VirtIOSerialPortClass *vsc = VIRTIO_SERIAL_PORT_GET_CLASS(port);
871 VirtIOSerialBus *bus = DO_UPCAST(VirtIOSerialBus, qbus, qdev->parent_bus);
872 int ret, max_nr_ports;
873 bool plugging_port0;
875 port->vser = bus->vser;
876 port->bh = qemu_bh_new(flush_queued_data_bh, port);
878 assert(vsc->have_data);
881 * Is the first console port we're seeing? If so, put it up at
882 * location 0. This is done for backward compatibility (old
883 * kernel, new qemu).
885 plugging_port0 = vsc->is_console && !find_port_by_id(port->vser, 0);
887 if (find_port_by_id(port->vser, port->id)) {
888 error_report("virtio-serial-bus: A port already exists at id %u",
889 port->id);
890 return -1;
893 if (port->id == VIRTIO_CONSOLE_BAD_ID) {
894 if (plugging_port0) {
895 port->id = 0;
896 } else {
897 port->id = find_free_port_id(port->vser);
898 if (port->id == VIRTIO_CONSOLE_BAD_ID) {
899 error_report("virtio-serial-bus: Maximum port limit for this device reached");
900 return -1;
905 max_nr_ports = tswap32(port->vser->config.max_nr_ports);
906 if (port->id >= max_nr_ports) {
907 error_report("virtio-serial-bus: Out-of-range port id specified, max. allowed: %u",
908 max_nr_ports - 1);
909 return -1;
912 ret = vsc->init(port);
913 if (ret) {
914 return ret;
917 port->elem.out_num = 0;
919 QTAILQ_INSERT_TAIL(&port->vser->ports, port, next);
920 port->ivq = port->vser->ivqs[port->id];
921 port->ovq = port->vser->ovqs[port->id];
923 add_port(port->vser, port->id);
925 /* Send an update to the guest about this new port added */
926 virtio_notify_config(&port->vser->vdev);
928 return ret;
931 static int virtser_port_qdev_exit(DeviceState *qdev)
933 VirtIOSerialPort *port = DO_UPCAST(VirtIOSerialPort, dev, qdev);
934 VirtIOSerialPortClass *vsc = VIRTIO_SERIAL_PORT_GET_CLASS(port);
935 VirtIOSerial *vser = port->vser;
937 qemu_bh_delete(port->bh);
938 remove_port(port->vser, port->id);
940 QTAILQ_REMOVE(&vser->ports, port, next);
942 if (vsc->exit) {
943 vsc->exit(port);
945 return 0;
948 VirtIODevice *virtio_serial_init(DeviceState *dev, virtio_serial_conf *conf)
950 VirtIOSerial *vser;
951 VirtIODevice *vdev;
952 uint32_t i, max_supported_ports;
954 if (!conf->max_virtserial_ports)
955 return NULL;
957 /* Each port takes 2 queues, and one pair is for the control queue */
958 max_supported_ports = VIRTIO_PCI_QUEUE_MAX / 2 - 1;
960 if (conf->max_virtserial_ports > max_supported_ports) {
961 error_report("maximum ports supported: %u", max_supported_ports);
962 return NULL;
965 vdev = virtio_common_init("virtio-serial", VIRTIO_ID_CONSOLE,
966 sizeof(struct virtio_console_config),
967 sizeof(VirtIOSerial));
969 vser = DO_UPCAST(VirtIOSerial, vdev, vdev);
971 /* Spawn a new virtio-serial bus on which the ports will ride as devices */
972 qbus_create_inplace(&vser->bus.qbus, TYPE_VIRTIO_SERIAL_BUS, dev, NULL);
973 vser->bus.qbus.allow_hotplug = 1;
974 vser->bus.vser = vser;
975 QTAILQ_INIT(&vser->ports);
977 vser->bus.max_nr_ports = conf->max_virtserial_ports;
978 vser->ivqs = g_malloc(conf->max_virtserial_ports * sizeof(VirtQueue *));
979 vser->ovqs = g_malloc(conf->max_virtserial_ports * sizeof(VirtQueue *));
981 /* Add a queue for host to guest transfers for port 0 (backward compat) */
982 vser->ivqs[0] = virtio_add_queue(vdev, 128, handle_input);
983 /* Add a queue for guest to host transfers for port 0 (backward compat) */
984 vser->ovqs[0] = virtio_add_queue(vdev, 128, handle_output);
986 /* TODO: host to guest notifications can get dropped
987 * if the queue fills up. Implement queueing in host,
988 * this might also make it possible to reduce the control
989 * queue size: as guest preposts buffers there,
990 * this will save 4Kbyte of guest memory per entry. */
992 /* control queue: host to guest */
993 vser->c_ivq = virtio_add_queue(vdev, 32, control_in);
994 /* control queue: guest to host */
995 vser->c_ovq = virtio_add_queue(vdev, 32, control_out);
997 for (i = 1; i < vser->bus.max_nr_ports; i++) {
998 /* Add a per-port queue for host to guest transfers */
999 vser->ivqs[i] = virtio_add_queue(vdev, 128, handle_input);
1000 /* Add a per-per queue for guest to host transfers */
1001 vser->ovqs[i] = virtio_add_queue(vdev, 128, handle_output);
1004 vser->config.max_nr_ports = tswap32(conf->max_virtserial_ports);
1005 vser->ports_map = g_malloc0(((conf->max_virtserial_ports + 31) / 32)
1006 * sizeof(vser->ports_map[0]));
1008 * Reserve location 0 for a console port for backward compat
1009 * (old kernel, new qemu)
1011 mark_port_added(vser, 0);
1013 vser->vdev.get_features = get_features;
1014 vser->vdev.get_config = get_config;
1015 vser->vdev.set_config = set_config;
1016 vser->vdev.set_status = set_status;
1017 vser->vdev.reset = vser_reset;
1019 vser->qdev = dev;
1021 vser->post_load = NULL;
1024 * Register for the savevm section with the virtio-console name
1025 * to preserve backward compat
1027 register_savevm(dev, "virtio-console", -1, 3, virtio_serial_save,
1028 virtio_serial_load, vser);
1030 return vdev;
1033 void virtio_serial_exit(VirtIODevice *vdev)
1035 VirtIOSerial *vser = DO_UPCAST(VirtIOSerial, vdev, vdev);
1037 unregister_savevm(vser->qdev, "virtio-console", vser);
1039 g_free(vser->ivqs);
1040 g_free(vser->ovqs);
1041 g_free(vser->ports_map);
1042 if (vser->post_load) {
1043 g_free(vser->post_load->connected);
1044 qemu_del_timer(vser->post_load->timer);
1045 qemu_free_timer(vser->post_load->timer);
1046 g_free(vser->post_load);
1048 virtio_cleanup(vdev);
1051 static void virtio_serial_port_class_init(ObjectClass *klass, void *data)
1053 DeviceClass *k = DEVICE_CLASS(klass);
1054 k->init = virtser_port_qdev_init;
1055 k->bus_type = TYPE_VIRTIO_SERIAL_BUS;
1056 k->exit = virtser_port_qdev_exit;
1057 k->unplug = qdev_simple_unplug_cb;
1058 k->props = virtser_props;
1061 static TypeInfo virtio_serial_port_type_info = {
1062 .name = TYPE_VIRTIO_SERIAL_PORT,
1063 .parent = TYPE_DEVICE,
1064 .instance_size = sizeof(VirtIOSerialPort),
1065 .abstract = true,
1066 .class_size = sizeof(VirtIOSerialPortClass),
1067 .class_init = virtio_serial_port_class_init,
1070 static void virtio_serial_register_types(void)
1072 type_register_static(&virtser_bus_info);
1073 type_register_static(&virtio_serial_port_type_info);
1076 type_init(virtio_serial_register_types)