target/mips: Remove XBurst Media eXtension Unit dead code
[qemu/ar7.git] / hw / char / virtio-serial-bus.c
blobb20038991a6c26228f1e5378542029c3cdb66517
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/osdep.h"
22 #include "qapi/error.h"
23 #include "qemu/iov.h"
24 #include "qemu/main-loop.h"
25 #include "qemu/module.h"
26 #include "migration/qemu-file-types.h"
27 #include "monitor/monitor.h"
28 #include "qemu/error-report.h"
29 #include "qemu/queue.h"
30 #include "hw/qdev-properties.h"
31 #include "hw/sysbus.h"
32 #include "trace.h"
33 #include "hw/virtio/virtio-serial.h"
34 #include "hw/virtio/virtio-access.h"
36 static struct VirtIOSerialDevices {
37 QLIST_HEAD(, VirtIOSerial) devices;
38 } vserdevices;
40 static VirtIOSerialPort *find_port_by_id(VirtIOSerial *vser, uint32_t id)
42 VirtIOSerialPort *port;
44 if (id == VIRTIO_CONSOLE_BAD_ID) {
45 return NULL;
48 QTAILQ_FOREACH(port, &vser->ports, next) {
49 if (port->id == id)
50 return port;
52 return NULL;
55 static VirtIOSerialPort *find_port_by_vq(VirtIOSerial *vser, VirtQueue *vq)
57 VirtIOSerialPort *port;
59 QTAILQ_FOREACH(port, &vser->ports, next) {
60 if (port->ivq == vq || port->ovq == vq)
61 return port;
63 return NULL;
66 static VirtIOSerialPort *find_port_by_name(char *name)
68 VirtIOSerial *vser;
70 QLIST_FOREACH(vser, &vserdevices.devices, next) {
71 VirtIOSerialPort *port;
73 QTAILQ_FOREACH(port, &vser->ports, next) {
74 if (port->name && !strcmp(port->name, name)) {
75 return port;
79 return NULL;
82 static VirtIOSerialPort *find_first_connected_console(VirtIOSerial *vser)
84 VirtIOSerialPort *port;
86 QTAILQ_FOREACH(port, &vser->ports, next) {
87 VirtIOSerialPortClass const *vsc = VIRTIO_SERIAL_PORT_GET_CLASS(port);
88 if (vsc->is_console && port->host_connected) {
89 return port;
92 return NULL;
95 static bool use_multiport(VirtIOSerial *vser)
97 VirtIODevice *vdev = VIRTIO_DEVICE(vser);
98 return virtio_vdev_has_feature(vdev, VIRTIO_CONSOLE_F_MULTIPORT);
101 static size_t write_to_port(VirtIOSerialPort *port,
102 const uint8_t *buf, size_t size)
104 VirtQueueElement *elem;
105 VirtQueue *vq;
106 size_t offset;
108 vq = port->ivq;
109 if (!virtio_queue_ready(vq)) {
110 return 0;
113 offset = 0;
114 while (offset < size) {
115 size_t len;
117 elem = virtqueue_pop(vq, sizeof(VirtQueueElement));
118 if (!elem) {
119 break;
122 len = iov_from_buf(elem->in_sg, elem->in_num, 0,
123 buf + offset, size - offset);
124 offset += len;
126 virtqueue_push(vq, elem, len);
127 g_free(elem);
130 virtio_notify(VIRTIO_DEVICE(port->vser), vq);
131 return offset;
134 static void discard_vq_data(VirtQueue *vq, VirtIODevice *vdev)
136 VirtQueueElement *elem;
138 if (!virtio_queue_ready(vq)) {
139 return;
141 for (;;) {
142 elem = virtqueue_pop(vq, sizeof(VirtQueueElement));
143 if (!elem) {
144 break;
146 virtqueue_push(vq, elem, 0);
147 g_free(elem);
149 virtio_notify(vdev, vq);
152 static void discard_throttle_data(VirtIOSerialPort *port)
154 if (port->elem) {
155 virtqueue_detach_element(port->ovq, port->elem, 0);
156 g_free(port->elem);
157 port->elem = NULL;
161 static void do_flush_queued_data(VirtIOSerialPort *port, VirtQueue *vq,
162 VirtIODevice *vdev)
164 VirtIOSerialPortClass *vsc;
166 assert(port);
167 assert(virtio_queue_ready(vq));
169 vsc = VIRTIO_SERIAL_PORT_GET_CLASS(port);
171 while (!port->throttled) {
172 unsigned int i;
174 /* Pop an elem only if we haven't left off a previous one mid-way */
175 if (!port->elem) {
176 port->elem = virtqueue_pop(vq, sizeof(VirtQueueElement));
177 if (!port->elem) {
178 break;
180 port->iov_idx = 0;
181 port->iov_offset = 0;
184 for (i = port->iov_idx; i < port->elem->out_num; i++) {
185 size_t buf_size;
186 ssize_t ret;
188 buf_size = port->elem->out_sg[i].iov_len - port->iov_offset;
189 ret = vsc->have_data(port,
190 port->elem->out_sg[i].iov_base
191 + port->iov_offset,
192 buf_size);
193 if (!port->elem) { /* bail if we got disconnected */
194 return;
196 if (port->throttled) {
197 port->iov_idx = i;
198 if (ret > 0) {
199 port->iov_offset += ret;
201 break;
203 port->iov_offset = 0;
205 if (port->throttled) {
206 break;
208 virtqueue_push(vq, port->elem, 0);
209 g_free(port->elem);
210 port->elem = NULL;
212 virtio_notify(vdev, vq);
215 static void flush_queued_data(VirtIOSerialPort *port)
217 assert(port);
219 if (!virtio_queue_ready(port->ovq)) {
220 return;
222 do_flush_queued_data(port, port->ovq, VIRTIO_DEVICE(port->vser));
225 static size_t send_control_msg(VirtIOSerial *vser, void *buf, size_t len)
227 VirtQueueElement *elem;
228 VirtQueue *vq;
230 vq = vser->c_ivq;
231 if (!virtio_queue_ready(vq)) {
232 return 0;
235 elem = virtqueue_pop(vq, sizeof(VirtQueueElement));
236 if (!elem) {
237 return 0;
240 /* TODO: detect a buffer that's too short, set NEEDS_RESET */
241 iov_from_buf(elem->in_sg, elem->in_num, 0, buf, len);
243 virtqueue_push(vq, elem, len);
244 virtio_notify(VIRTIO_DEVICE(vser), vq);
245 g_free(elem);
247 return len;
250 static size_t send_control_event(VirtIOSerial *vser, uint32_t port_id,
251 uint16_t event, uint16_t value)
253 VirtIODevice *vdev = VIRTIO_DEVICE(vser);
254 struct virtio_console_control cpkt;
256 virtio_stl_p(vdev, &cpkt.id, port_id);
257 virtio_stw_p(vdev, &cpkt.event, event);
258 virtio_stw_p(vdev, &cpkt.value, value);
260 trace_virtio_serial_send_control_event(port_id, event, value);
261 return send_control_msg(vser, &cpkt, sizeof(cpkt));
264 /* Functions for use inside qemu to open and read from/write to ports */
265 int virtio_serial_open(VirtIOSerialPort *port)
267 /* Don't allow opening an already-open port */
268 if (port->host_connected) {
269 return 0;
271 /* Send port open notification to the guest */
272 port->host_connected = true;
273 send_control_event(port->vser, port->id, VIRTIO_CONSOLE_PORT_OPEN, 1);
275 return 0;
278 int virtio_serial_close(VirtIOSerialPort *port)
280 port->host_connected = false;
282 * If there's any data the guest sent which the app didn't
283 * consume, reset the throttling flag and discard the data.
285 port->throttled = false;
286 discard_throttle_data(port);
287 discard_vq_data(port->ovq, VIRTIO_DEVICE(port->vser));
289 send_control_event(port->vser, port->id, VIRTIO_CONSOLE_PORT_OPEN, 0);
291 return 0;
294 /* Individual ports/apps call this function to write to the guest. */
295 ssize_t virtio_serial_write(VirtIOSerialPort *port, const uint8_t *buf,
296 size_t size)
298 if (!port || !port->host_connected || !port->guest_connected) {
299 return 0;
301 return write_to_port(port, buf, size);
305 * Readiness of the guest to accept data on a port.
306 * Returns max. data the guest can receive
308 size_t virtio_serial_guest_ready(VirtIOSerialPort *port)
310 VirtIODevice *vdev = VIRTIO_DEVICE(port->vser);
311 VirtQueue *vq = port->ivq;
312 unsigned int bytes;
314 if (!virtio_queue_ready(vq) ||
315 !(vdev->status & VIRTIO_CONFIG_S_DRIVER_OK) ||
316 virtio_queue_empty(vq)) {
317 return 0;
319 if (use_multiport(port->vser) && !port->guest_connected) {
320 return 0;
322 virtqueue_get_avail_bytes(vq, &bytes, NULL, 4096, 0);
323 return bytes;
326 static void flush_queued_data_bh(void *opaque)
328 VirtIOSerialPort *port = opaque;
330 flush_queued_data(port);
333 void virtio_serial_throttle_port(VirtIOSerialPort *port, bool throttle)
335 if (!port) {
336 return;
339 trace_virtio_serial_throttle_port(port->id, throttle);
340 port->throttled = throttle;
341 if (throttle) {
342 return;
344 qemu_bh_schedule(port->bh);
347 /* Guest wants to notify us of some event */
348 static void handle_control_message(VirtIOSerial *vser, void *buf, size_t len)
350 VirtIODevice *vdev = VIRTIO_DEVICE(vser);
351 struct VirtIOSerialPort *port;
352 VirtIOSerialPortClass *vsc;
353 struct virtio_console_control cpkt, *gcpkt;
354 uint8_t *buffer;
355 size_t buffer_len;
357 gcpkt = buf;
359 if (len < sizeof(cpkt)) {
360 /* The guest sent an invalid control packet */
361 return;
364 cpkt.event = virtio_lduw_p(vdev, &gcpkt->event);
365 cpkt.value = virtio_lduw_p(vdev, &gcpkt->value);
367 trace_virtio_serial_handle_control_message(cpkt.event, cpkt.value);
369 if (cpkt.event == VIRTIO_CONSOLE_DEVICE_READY) {
370 if (!cpkt.value) {
371 error_report("virtio-serial-bus: Guest failure in adding device %s",
372 vser->bus.qbus.name);
373 return;
376 * The device is up, we can now tell the device about all the
377 * ports we have here.
379 QTAILQ_FOREACH(port, &vser->ports, next) {
380 send_control_event(vser, port->id, VIRTIO_CONSOLE_PORT_ADD, 1);
382 return;
385 port = find_port_by_id(vser, virtio_ldl_p(vdev, &gcpkt->id));
386 if (!port) {
387 error_report("virtio-serial-bus: Unexpected port id %u for device %s",
388 virtio_ldl_p(vdev, &gcpkt->id), vser->bus.qbus.name);
389 return;
392 trace_virtio_serial_handle_control_message_port(port->id);
394 vsc = VIRTIO_SERIAL_PORT_GET_CLASS(port);
396 switch(cpkt.event) {
397 case VIRTIO_CONSOLE_PORT_READY:
398 if (!cpkt.value) {
399 error_report("virtio-serial-bus: Guest failure in adding port %u for device %s",
400 port->id, vser->bus.qbus.name);
401 break;
404 * Now that we know the guest asked for the port name, we're
405 * sure the guest has initialised whatever state is necessary
406 * for this port. Now's a good time to let the guest know if
407 * this port is a console port so that the guest can hook it
408 * up to hvc.
410 if (vsc->is_console) {
411 send_control_event(vser, port->id, VIRTIO_CONSOLE_CONSOLE_PORT, 1);
414 if (port->name) {
415 virtio_stl_p(vdev, &cpkt.id, port->id);
416 virtio_stw_p(vdev, &cpkt.event, VIRTIO_CONSOLE_PORT_NAME);
417 virtio_stw_p(vdev, &cpkt.value, 1);
419 buffer_len = sizeof(cpkt) + strlen(port->name) + 1;
420 buffer = g_malloc(buffer_len);
422 memcpy(buffer, &cpkt, sizeof(cpkt));
423 memcpy(buffer + sizeof(cpkt), port->name, strlen(port->name));
424 buffer[buffer_len - 1] = 0;
426 send_control_msg(vser, buffer, buffer_len);
427 g_free(buffer);
430 if (port->host_connected) {
431 send_control_event(vser, port->id, VIRTIO_CONSOLE_PORT_OPEN, 1);
435 * When the guest has asked us for this information it means
436 * the guest is all setup and has its virtqueues
437 * initialised. If some app is interested in knowing about
438 * this event, let it know.
440 if (vsc->guest_ready) {
441 vsc->guest_ready(port);
443 break;
445 case VIRTIO_CONSOLE_PORT_OPEN:
446 port->guest_connected = cpkt.value;
447 if (vsc->set_guest_connected) {
448 /* Send the guest opened notification if an app is interested */
449 vsc->set_guest_connected(port, cpkt.value);
451 break;
455 static void control_in(VirtIODevice *vdev, VirtQueue *vq)
459 static void control_out(VirtIODevice *vdev, VirtQueue *vq)
461 VirtQueueElement *elem;
462 VirtIOSerial *vser;
463 uint8_t *buf;
464 size_t len;
466 vser = VIRTIO_SERIAL(vdev);
468 len = 0;
469 buf = NULL;
470 for (;;) {
471 size_t cur_len;
473 elem = virtqueue_pop(vq, sizeof(VirtQueueElement));
474 if (!elem) {
475 break;
478 cur_len = iov_size(elem->out_sg, elem->out_num);
480 * Allocate a new buf only if we didn't have one previously or
481 * if the size of the buf differs
483 if (cur_len > len) {
484 g_free(buf);
486 buf = g_malloc(cur_len);
487 len = cur_len;
489 iov_to_buf(elem->out_sg, elem->out_num, 0, buf, cur_len);
491 handle_control_message(vser, buf, cur_len);
492 virtqueue_push(vq, elem, 0);
493 g_free(elem);
495 g_free(buf);
496 virtio_notify(vdev, vq);
499 /* Guest wrote something to some port. */
500 static void handle_output(VirtIODevice *vdev, VirtQueue *vq)
502 VirtIOSerial *vser;
503 VirtIOSerialPort *port;
505 vser = VIRTIO_SERIAL(vdev);
506 port = find_port_by_vq(vser, vq);
508 if (!port || !port->host_connected) {
509 discard_vq_data(vq, vdev);
510 return;
513 if (!port->throttled) {
514 do_flush_queued_data(port, vq, vdev);
515 return;
519 static void handle_input(VirtIODevice *vdev, VirtQueue *vq)
522 * Users of virtio-serial would like to know when guest becomes
523 * writable again -- i.e. if a vq had stuff queued up and the
524 * guest wasn't reading at all, the host would not be able to
525 * write to the vq anymore. Once the guest reads off something,
526 * we can start queueing things up again. However, this call is
527 * made for each buffer addition by the guest -- even though free
528 * buffers existed prior to the current buffer addition. This is
529 * done so as not to maintain previous state, which will need
530 * additional live-migration-related changes.
532 VirtIOSerial *vser;
533 VirtIOSerialPort *port;
534 VirtIOSerialPortClass *vsc;
536 vser = VIRTIO_SERIAL(vdev);
537 port = find_port_by_vq(vser, vq);
539 if (!port) {
540 return;
542 vsc = VIRTIO_SERIAL_PORT_GET_CLASS(port);
545 * If guest_connected is false, this call is being made by the
546 * early-boot queueing up of descriptors, which is just noise for
547 * the host apps -- don't disturb them in that case.
549 if (port->guest_connected && port->host_connected && vsc->guest_writable) {
550 vsc->guest_writable(port);
554 static uint64_t get_features(VirtIODevice *vdev, uint64_t features,
555 Error **errp)
557 VirtIOSerial *vser;
559 vser = VIRTIO_SERIAL(vdev);
561 features |= vser->host_features;
562 if (vser->bus.max_nr_ports > 1) {
563 virtio_add_feature(&features, VIRTIO_CONSOLE_F_MULTIPORT);
565 return features;
568 /* Guest requested config info */
569 static void get_config(VirtIODevice *vdev, uint8_t *config_data)
571 VirtIOSerial *vser = VIRTIO_SERIAL(vdev);
572 struct virtio_console_config *config =
573 (struct virtio_console_config *)config_data;
575 config->cols = 0;
576 config->rows = 0;
577 config->max_nr_ports = virtio_tswap32(vdev,
578 vser->serial.max_virtserial_ports);
581 /* Guest sent new config info */
582 static void set_config(VirtIODevice *vdev, const uint8_t *config_data)
584 VirtIOSerial *vser = VIRTIO_SERIAL(vdev);
585 struct virtio_console_config *config =
586 (struct virtio_console_config *)config_data;
587 VirtIOSerialPort *port = find_first_connected_console(vser);
588 VirtIOSerialPortClass *vsc;
589 uint8_t emerg_wr_lo;
591 if (!virtio_has_feature(vser->host_features,
592 VIRTIO_CONSOLE_F_EMERG_WRITE) || !config->emerg_wr) {
593 return;
596 emerg_wr_lo = le32_to_cpu(config->emerg_wr);
597 /* Make sure we don't misdetect an emergency write when the guest
598 * does a short config write after an emergency write. */
599 config->emerg_wr = 0;
600 if (!port) {
601 return;
603 vsc = VIRTIO_SERIAL_PORT_GET_CLASS(port);
604 (void)vsc->have_data(port, &emerg_wr_lo, 1);
607 static void guest_reset(VirtIOSerial *vser)
609 VirtIOSerialPort *port;
610 VirtIOSerialPortClass *vsc;
612 QTAILQ_FOREACH(port, &vser->ports, next) {
613 vsc = VIRTIO_SERIAL_PORT_GET_CLASS(port);
615 discard_throttle_data(port);
617 if (port->guest_connected) {
618 port->guest_connected = false;
619 if (vsc->set_guest_connected) {
620 vsc->set_guest_connected(port, false);
626 static void set_status(VirtIODevice *vdev, uint8_t status)
628 VirtIOSerial *vser;
629 VirtIOSerialPort *port;
631 vser = VIRTIO_SERIAL(vdev);
632 port = find_port_by_id(vser, 0);
634 if (port && !use_multiport(port->vser)
635 && (status & VIRTIO_CONFIG_S_DRIVER_OK)) {
637 * Non-multiport guests won't be able to tell us guest
638 * open/close status. Such guests can only have a port at id
639 * 0, so set guest_connected for such ports as soon as guest
640 * is up.
642 port->guest_connected = true;
644 if (!(status & VIRTIO_CONFIG_S_DRIVER_OK)) {
645 guest_reset(vser);
648 QTAILQ_FOREACH(port, &vser->ports, next) {
649 VirtIOSerialPortClass *vsc = VIRTIO_SERIAL_PORT_GET_CLASS(port);
650 if (vsc->enable_backend) {
651 vsc->enable_backend(port, vdev->vm_running);
656 static void vser_reset(VirtIODevice *vdev)
658 VirtIOSerial *vser;
660 vser = VIRTIO_SERIAL(vdev);
661 guest_reset(vser);
664 static void virtio_serial_save_device(VirtIODevice *vdev, QEMUFile *f)
666 VirtIOSerial *s = VIRTIO_SERIAL(vdev);
667 VirtIOSerialPort *port;
668 uint32_t nr_active_ports;
669 unsigned int i, max_nr_ports;
670 struct virtio_console_config config;
672 /* The config space (ignored on the far end in current versions) */
673 get_config(vdev, (uint8_t *)&config);
674 qemu_put_be16(f, config.cols);
675 qemu_put_be16(f, config.rows);
676 qemu_put_be32(f, config.max_nr_ports);
678 /* The ports map */
679 max_nr_ports = s->serial.max_virtserial_ports;
680 for (i = 0; i < DIV_ROUND_UP(max_nr_ports, 32); i++) {
681 qemu_put_be32s(f, &s->ports_map[i]);
684 /* Ports */
686 nr_active_ports = 0;
687 QTAILQ_FOREACH(port, &s->ports, next) {
688 nr_active_ports++;
691 qemu_put_be32s(f, &nr_active_ports);
694 * Items in struct VirtIOSerialPort.
696 QTAILQ_FOREACH(port, &s->ports, next) {
697 uint32_t elem_popped;
699 qemu_put_be32s(f, &port->id);
700 qemu_put_byte(f, port->guest_connected);
701 qemu_put_byte(f, port->host_connected);
703 elem_popped = 0;
704 if (port->elem) {
705 elem_popped = 1;
707 qemu_put_be32s(f, &elem_popped);
708 if (elem_popped) {
709 qemu_put_be32s(f, &port->iov_idx);
710 qemu_put_be64s(f, &port->iov_offset);
711 qemu_put_virtqueue_element(vdev, f, port->elem);
716 static void virtio_serial_post_load_timer_cb(void *opaque)
718 uint32_t i;
719 VirtIOSerial *s = VIRTIO_SERIAL(opaque);
720 VirtIOSerialPort *port;
721 uint8_t host_connected;
722 VirtIOSerialPortClass *vsc;
724 if (!s->post_load) {
725 return;
727 for (i = 0 ; i < s->post_load->nr_active_ports; ++i) {
728 port = s->post_load->connected[i].port;
729 host_connected = s->post_load->connected[i].host_connected;
730 if (host_connected != port->host_connected) {
732 * We have to let the guest know of the host connection
733 * status change
735 send_control_event(s, port->id, VIRTIO_CONSOLE_PORT_OPEN,
736 port->host_connected);
738 vsc = VIRTIO_SERIAL_PORT_GET_CLASS(port);
739 if (vsc->set_guest_connected) {
740 vsc->set_guest_connected(port, port->guest_connected);
743 g_free(s->post_load->connected);
744 timer_free(s->post_load->timer);
745 g_free(s->post_load);
746 s->post_load = NULL;
749 static int fetch_active_ports_list(QEMUFile *f,
750 VirtIOSerial *s, uint32_t nr_active_ports)
752 VirtIODevice *vdev = VIRTIO_DEVICE(s);
753 uint32_t i;
755 s->post_load = g_malloc0(sizeof(*s->post_load));
756 s->post_load->nr_active_ports = nr_active_ports;
757 s->post_load->connected =
758 g_malloc0(sizeof(*s->post_load->connected) * nr_active_ports);
760 s->post_load->timer = timer_new_ns(QEMU_CLOCK_VIRTUAL,
761 virtio_serial_post_load_timer_cb,
764 /* Items in struct VirtIOSerialPort */
765 for (i = 0; i < nr_active_ports; i++) {
766 VirtIOSerialPort *port;
767 uint32_t elem_popped;
768 uint32_t id;
770 id = qemu_get_be32(f);
771 port = find_port_by_id(s, id);
772 if (!port) {
773 return -EINVAL;
776 port->guest_connected = qemu_get_byte(f);
777 s->post_load->connected[i].port = port;
778 s->post_load->connected[i].host_connected = qemu_get_byte(f);
780 qemu_get_be32s(f, &elem_popped);
781 if (elem_popped) {
782 qemu_get_be32s(f, &port->iov_idx);
783 qemu_get_be64s(f, &port->iov_offset);
785 port->elem =
786 qemu_get_virtqueue_element(vdev, f, sizeof(VirtQueueElement));
789 * Port was throttled on source machine. Let's
790 * unthrottle it here so data starts flowing again.
792 virtio_serial_throttle_port(port, false);
795 timer_mod(s->post_load->timer, 1);
796 return 0;
799 static int virtio_serial_load_device(VirtIODevice *vdev, QEMUFile *f,
800 int version_id)
802 VirtIOSerial *s = VIRTIO_SERIAL(vdev);
803 uint32_t max_nr_ports, nr_active_ports, ports_map;
804 unsigned int i;
805 int ret;
806 uint32_t tmp;
808 /* Unused */
809 qemu_get_be16s(f, (uint16_t *) &tmp);
810 qemu_get_be16s(f, (uint16_t *) &tmp);
811 qemu_get_be32s(f, &tmp);
813 max_nr_ports = s->serial.max_virtserial_ports;
814 for (i = 0; i < DIV_ROUND_UP(max_nr_ports, 32); i++) {
815 qemu_get_be32s(f, &ports_map);
817 if (ports_map != s->ports_map[i]) {
819 * Ports active on source and destination don't
820 * match. Fail migration.
822 return -EINVAL;
826 qemu_get_be32s(f, &nr_active_ports);
828 if (nr_active_ports) {
829 ret = fetch_active_ports_list(f, s, nr_active_ports);
830 if (ret) {
831 return ret;
834 return 0;
837 static void virtser_bus_dev_print(Monitor *mon, DeviceState *qdev, int indent);
839 static Property virtser_props[] = {
840 DEFINE_PROP_UINT32("nr", VirtIOSerialPort, id, VIRTIO_CONSOLE_BAD_ID),
841 DEFINE_PROP_STRING("name", VirtIOSerialPort, name),
842 DEFINE_PROP_END_OF_LIST()
845 static void virtser_bus_class_init(ObjectClass *klass, void *data)
847 BusClass *k = BUS_CLASS(klass);
848 k->print_dev = virtser_bus_dev_print;
851 static const TypeInfo virtser_bus_info = {
852 .name = TYPE_VIRTIO_SERIAL_BUS,
853 .parent = TYPE_BUS,
854 .instance_size = sizeof(VirtIOSerialBus),
855 .class_init = virtser_bus_class_init,
858 static void virtser_bus_dev_print(Monitor *mon, DeviceState *qdev, int indent)
860 VirtIOSerialPort *port = VIRTIO_SERIAL_PORT(qdev);
862 monitor_printf(mon, "%*sport %d, guest %s, host %s, throttle %s\n",
863 indent, "", port->id,
864 port->guest_connected ? "on" : "off",
865 port->host_connected ? "on" : "off",
866 port->throttled ? "on" : "off");
869 /* This function is only used if a port id is not provided by the user */
870 static uint32_t find_free_port_id(VirtIOSerial *vser)
872 unsigned int i, max_nr_ports;
874 max_nr_ports = vser->serial.max_virtserial_ports;
875 for (i = 0; i < DIV_ROUND_UP(max_nr_ports, 32); i++) {
876 uint32_t map, zeroes;
878 map = vser->ports_map[i];
879 zeroes = ctz32(~map);
880 if (zeroes != 32) {
881 return zeroes + i * 32;
884 return VIRTIO_CONSOLE_BAD_ID;
887 static void mark_port_added(VirtIOSerial *vser, uint32_t port_id)
889 unsigned int i;
891 i = port_id / 32;
892 vser->ports_map[i] |= 1U << (port_id % 32);
895 static void add_port(VirtIOSerial *vser, uint32_t port_id)
897 mark_port_added(vser, port_id);
898 send_control_event(vser, port_id, VIRTIO_CONSOLE_PORT_ADD, 1);
901 static void remove_port(VirtIOSerial *vser, uint32_t port_id)
903 VirtIOSerialPort *port;
906 * Don't mark port 0 removed -- we explicitly reserve it for
907 * backward compat with older guests, ensure a virtconsole device
908 * unplug retains the reservation.
910 if (port_id) {
911 unsigned int i;
913 i = port_id / 32;
914 vser->ports_map[i] &= ~(1U << (port_id % 32));
917 port = find_port_by_id(vser, port_id);
919 * This function is only called from qdev's unplug callback; if we
920 * get a NULL port here, we're in trouble.
922 assert(port);
924 /* Flush out any unconsumed buffers first */
925 discard_throttle_data(port);
926 discard_vq_data(port->ovq, VIRTIO_DEVICE(port->vser));
928 send_control_event(vser, port->id, VIRTIO_CONSOLE_PORT_REMOVE, 1);
931 static void virtser_port_device_realize(DeviceState *dev, Error **errp)
933 VirtIOSerialPort *port = VIRTIO_SERIAL_PORT(dev);
934 VirtIOSerialPortClass *vsc = VIRTIO_SERIAL_PORT_GET_CLASS(port);
935 VirtIOSerialBus *bus = VIRTIO_SERIAL_BUS(qdev_get_parent_bus(dev));
936 int max_nr_ports;
937 bool plugging_port0;
938 Error *err = NULL;
940 port->vser = bus->vser;
942 assert(vsc->have_data);
945 * Is the first console port we're seeing? If so, put it up at
946 * location 0. This is done for backward compatibility (old
947 * kernel, new qemu).
949 plugging_port0 = vsc->is_console && !find_port_by_id(port->vser, 0);
951 if (find_port_by_id(port->vser, port->id)) {
952 error_setg(errp, "virtio-serial-bus: A port already exists at id %u",
953 port->id);
954 return;
957 if (port->name != NULL && find_port_by_name(port->name)) {
958 error_setg(errp, "virtio-serial-bus: A port already exists by name %s",
959 port->name);
960 return;
963 if (port->id == VIRTIO_CONSOLE_BAD_ID) {
964 if (plugging_port0) {
965 port->id = 0;
966 } else {
967 port->id = find_free_port_id(port->vser);
968 if (port->id == VIRTIO_CONSOLE_BAD_ID) {
969 error_setg(errp, "virtio-serial-bus: Maximum port limit for "
970 "this device reached");
971 return;
976 max_nr_ports = port->vser->serial.max_virtserial_ports;
977 if (port->id >= max_nr_ports) {
978 error_setg(errp, "virtio-serial-bus: Out-of-range port id specified, "
979 "max. allowed: %u", max_nr_ports - 1);
980 return;
983 vsc->realize(dev, &err);
984 if (err != NULL) {
985 error_propagate(errp, err);
986 return;
989 port->bh = qemu_bh_new(flush_queued_data_bh, port);
990 port->elem = NULL;
993 static void virtser_port_device_plug(HotplugHandler *hotplug_dev,
994 DeviceState *dev, Error **errp)
996 VirtIOSerialPort *port = VIRTIO_SERIAL_PORT(dev);
998 QTAILQ_INSERT_TAIL(&port->vser->ports, port, next);
999 port->ivq = port->vser->ivqs[port->id];
1000 port->ovq = port->vser->ovqs[port->id];
1002 add_port(port->vser, port->id);
1004 /* Send an update to the guest about this new port added */
1005 virtio_notify_config(VIRTIO_DEVICE(hotplug_dev));
1008 static void virtser_port_device_unrealize(DeviceState *dev)
1010 VirtIOSerialPort *port = VIRTIO_SERIAL_PORT(dev);
1011 VirtIOSerialPortClass *vsc = VIRTIO_SERIAL_PORT_GET_CLASS(dev);
1012 VirtIOSerial *vser = port->vser;
1014 qemu_bh_delete(port->bh);
1015 remove_port(port->vser, port->id);
1017 QTAILQ_REMOVE(&vser->ports, port, next);
1019 if (vsc->unrealize) {
1020 vsc->unrealize(dev);
1024 static void virtio_serial_device_realize(DeviceState *dev, Error **errp)
1026 VirtIODevice *vdev = VIRTIO_DEVICE(dev);
1027 VirtIOSerial *vser = VIRTIO_SERIAL(dev);
1028 uint32_t i, max_supported_ports;
1029 size_t config_size = sizeof(struct virtio_console_config);
1031 if (!vser->serial.max_virtserial_ports) {
1032 error_setg(errp, "Maximum number of serial ports not specified");
1033 return;
1036 /* Each port takes 2 queues, and one pair is for the control queue */
1037 max_supported_ports = VIRTIO_QUEUE_MAX / 2 - 1;
1039 if (vser->serial.max_virtserial_ports > max_supported_ports) {
1040 error_setg(errp, "maximum ports supported: %u", max_supported_ports);
1041 return;
1044 if (!virtio_has_feature(vser->host_features,
1045 VIRTIO_CONSOLE_F_EMERG_WRITE)) {
1046 config_size = offsetof(struct virtio_console_config, emerg_wr);
1048 virtio_init(vdev, "virtio-serial", VIRTIO_ID_CONSOLE,
1049 config_size);
1051 /* Spawn a new virtio-serial bus on which the ports will ride as devices */
1052 qbus_create_inplace(&vser->bus, sizeof(vser->bus), TYPE_VIRTIO_SERIAL_BUS,
1053 dev, vdev->bus_name);
1054 qbus_set_hotplug_handler(BUS(&vser->bus), OBJECT(vser));
1055 vser->bus.vser = vser;
1056 QTAILQ_INIT(&vser->ports);
1058 vser->bus.max_nr_ports = vser->serial.max_virtserial_ports;
1059 vser->ivqs = g_malloc(vser->serial.max_virtserial_ports
1060 * sizeof(VirtQueue *));
1061 vser->ovqs = g_malloc(vser->serial.max_virtserial_ports
1062 * sizeof(VirtQueue *));
1064 /* Add a queue for host to guest transfers for port 0 (backward compat) */
1065 vser->ivqs[0] = virtio_add_queue(vdev, 128, handle_input);
1066 /* Add a queue for guest to host transfers for port 0 (backward compat) */
1067 vser->ovqs[0] = virtio_add_queue(vdev, 128, handle_output);
1069 /* TODO: host to guest notifications can get dropped
1070 * if the queue fills up. Implement queueing in host,
1071 * this might also make it possible to reduce the control
1072 * queue size: as guest preposts buffers there,
1073 * this will save 4Kbyte of guest memory per entry. */
1075 /* control queue: host to guest */
1076 vser->c_ivq = virtio_add_queue(vdev, 32, control_in);
1077 /* control queue: guest to host */
1078 vser->c_ovq = virtio_add_queue(vdev, 32, control_out);
1080 for (i = 1; i < vser->bus.max_nr_ports; i++) {
1081 /* Add a per-port queue for host to guest transfers */
1082 vser->ivqs[i] = virtio_add_queue(vdev, 128, handle_input);
1083 /* Add a per-per queue for guest to host transfers */
1084 vser->ovqs[i] = virtio_add_queue(vdev, 128, handle_output);
1087 vser->ports_map = g_malloc0((DIV_ROUND_UP(vser->serial.max_virtserial_ports, 32))
1088 * sizeof(vser->ports_map[0]));
1090 * Reserve location 0 for a console port for backward compat
1091 * (old kernel, new qemu)
1093 mark_port_added(vser, 0);
1095 vser->post_load = NULL;
1097 QLIST_INSERT_HEAD(&vserdevices.devices, vser, next);
1100 static void virtio_serial_port_class_init(ObjectClass *klass, void *data)
1102 DeviceClass *k = DEVICE_CLASS(klass);
1104 set_bit(DEVICE_CATEGORY_INPUT, k->categories);
1105 k->bus_type = TYPE_VIRTIO_SERIAL_BUS;
1106 k->realize = virtser_port_device_realize;
1107 k->unrealize = virtser_port_device_unrealize;
1108 device_class_set_props(k, virtser_props);
1111 static const TypeInfo virtio_serial_port_type_info = {
1112 .name = TYPE_VIRTIO_SERIAL_PORT,
1113 .parent = TYPE_DEVICE,
1114 .instance_size = sizeof(VirtIOSerialPort),
1115 .abstract = true,
1116 .class_size = sizeof(VirtIOSerialPortClass),
1117 .class_init = virtio_serial_port_class_init,
1120 static void virtio_serial_device_unrealize(DeviceState *dev)
1122 VirtIODevice *vdev = VIRTIO_DEVICE(dev);
1123 VirtIOSerial *vser = VIRTIO_SERIAL(dev);
1124 int i;
1126 QLIST_REMOVE(vser, next);
1128 virtio_delete_queue(vser->c_ivq);
1129 virtio_delete_queue(vser->c_ovq);
1130 for (i = 0; i < vser->bus.max_nr_ports; i++) {
1131 virtio_delete_queue(vser->ivqs[i]);
1132 virtio_delete_queue(vser->ovqs[i]);
1135 g_free(vser->ivqs);
1136 g_free(vser->ovqs);
1137 g_free(vser->ports_map);
1138 if (vser->post_load) {
1139 g_free(vser->post_load->connected);
1140 timer_free(vser->post_load->timer);
1141 g_free(vser->post_load);
1144 qbus_set_hotplug_handler(BUS(&vser->bus), NULL);
1146 virtio_cleanup(vdev);
1149 /* Note: 'console' is used for backwards compatibility */
1150 static const VMStateDescription vmstate_virtio_console = {
1151 .name = "virtio-console",
1152 .minimum_version_id = 3,
1153 .version_id = 3,
1154 .fields = (VMStateField[]) {
1155 VMSTATE_VIRTIO_DEVICE,
1156 VMSTATE_END_OF_LIST()
1160 static Property virtio_serial_properties[] = {
1161 DEFINE_PROP_UINT32("max_ports", VirtIOSerial, serial.max_virtserial_ports,
1162 31),
1163 DEFINE_PROP_BIT64("emergency-write", VirtIOSerial, host_features,
1164 VIRTIO_CONSOLE_F_EMERG_WRITE, true),
1165 DEFINE_PROP_END_OF_LIST(),
1168 static void virtio_serial_class_init(ObjectClass *klass, void *data)
1170 DeviceClass *dc = DEVICE_CLASS(klass);
1171 VirtioDeviceClass *vdc = VIRTIO_DEVICE_CLASS(klass);
1172 HotplugHandlerClass *hc = HOTPLUG_HANDLER_CLASS(klass);
1174 QLIST_INIT(&vserdevices.devices);
1176 device_class_set_props(dc, virtio_serial_properties);
1177 dc->vmsd = &vmstate_virtio_console;
1178 set_bit(DEVICE_CATEGORY_INPUT, dc->categories);
1179 vdc->realize = virtio_serial_device_realize;
1180 vdc->unrealize = virtio_serial_device_unrealize;
1181 vdc->get_features = get_features;
1182 vdc->get_config = get_config;
1183 vdc->set_config = set_config;
1184 vdc->set_status = set_status;
1185 vdc->reset = vser_reset;
1186 vdc->save = virtio_serial_save_device;
1187 vdc->load = virtio_serial_load_device;
1188 hc->plug = virtser_port_device_plug;
1189 hc->unplug = qdev_simple_device_unplug_cb;
1192 static const TypeInfo virtio_device_info = {
1193 .name = TYPE_VIRTIO_SERIAL,
1194 .parent = TYPE_VIRTIO_DEVICE,
1195 .instance_size = sizeof(VirtIOSerial),
1196 .class_init = virtio_serial_class_init,
1197 .interfaces = (InterfaceInfo[]) {
1198 { TYPE_HOTPLUG_HANDLER },
1203 static void virtio_serial_register_types(void)
1205 type_register_static(&virtser_bus_info);
1206 type_register_static(&virtio_serial_port_type_info);
1207 type_register_static(&virtio_device_info);
1210 type_init(virtio_serial_register_types)