sd: limit 'req.cmd' while using as an array index
[qemu/kevin.git] / hw / char / virtio-serial-bus.c
blob99cb6836add183d2ae983055e6a639ee5b9dc9f3
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 "qemu/iov.h"
23 #include "monitor/monitor.h"
24 #include "qemu/error-report.h"
25 #include "qemu/queue.h"
26 #include "hw/sysbus.h"
27 #include "trace.h"
28 #include "hw/virtio/virtio-serial.h"
29 #include "hw/virtio/virtio-access.h"
31 static struct VirtIOSerialDevices {
32 QLIST_HEAD(, VirtIOSerial) devices;
33 } vserdevices;
35 static VirtIOSerialPort *find_port_by_id(VirtIOSerial *vser, uint32_t id)
37 VirtIOSerialPort *port;
39 if (id == VIRTIO_CONSOLE_BAD_ID) {
40 return NULL;
43 QTAILQ_FOREACH(port, &vser->ports, next) {
44 if (port->id == id)
45 return port;
47 return NULL;
50 static VirtIOSerialPort *find_port_by_vq(VirtIOSerial *vser, VirtQueue *vq)
52 VirtIOSerialPort *port;
54 QTAILQ_FOREACH(port, &vser->ports, next) {
55 if (port->ivq == vq || port->ovq == vq)
56 return port;
58 return NULL;
61 static VirtIOSerialPort *find_port_by_name(char *name)
63 VirtIOSerial *vser;
65 QLIST_FOREACH(vser, &vserdevices.devices, next) {
66 VirtIOSerialPort *port;
68 QTAILQ_FOREACH(port, &vser->ports, next) {
69 if (port->name && !strcmp(port->name, name)) {
70 return port;
74 return NULL;
77 static bool use_multiport(VirtIOSerial *vser)
79 VirtIODevice *vdev = VIRTIO_DEVICE(vser);
80 return virtio_vdev_has_feature(vdev, VIRTIO_CONSOLE_F_MULTIPORT);
83 static size_t write_to_port(VirtIOSerialPort *port,
84 const uint8_t *buf, size_t size)
86 VirtQueueElement *elem;
87 VirtQueue *vq;
88 size_t offset;
90 vq = port->ivq;
91 if (!virtio_queue_ready(vq)) {
92 return 0;
95 offset = 0;
96 while (offset < size) {
97 size_t len;
99 elem = virtqueue_pop(vq, sizeof(VirtQueueElement));
100 if (!elem) {
101 break;
104 len = iov_from_buf(elem->in_sg, elem->in_num, 0,
105 buf + offset, size - offset);
106 offset += len;
108 virtqueue_push(vq, elem, len);
109 g_free(elem);
112 virtio_notify(VIRTIO_DEVICE(port->vser), vq);
113 return offset;
116 static void discard_vq_data(VirtQueue *vq, VirtIODevice *vdev)
118 VirtQueueElement *elem;
120 if (!virtio_queue_ready(vq)) {
121 return;
123 for (;;) {
124 elem = virtqueue_pop(vq, sizeof(VirtQueueElement));
125 if (!elem) {
126 break;
128 virtqueue_push(vq, elem, 0);
129 g_free(elem);
131 virtio_notify(vdev, vq);
134 static void do_flush_queued_data(VirtIOSerialPort *port, VirtQueue *vq,
135 VirtIODevice *vdev)
137 VirtIOSerialPortClass *vsc;
139 assert(port);
140 assert(virtio_queue_ready(vq));
142 vsc = VIRTIO_SERIAL_PORT_GET_CLASS(port);
144 while (!port->throttled) {
145 unsigned int i;
147 /* Pop an elem only if we haven't left off a previous one mid-way */
148 if (!port->elem) {
149 port->elem = virtqueue_pop(vq, sizeof(VirtQueueElement));
150 if (!port->elem) {
151 break;
153 port->iov_idx = 0;
154 port->iov_offset = 0;
157 for (i = port->iov_idx; i < port->elem->out_num; i++) {
158 size_t buf_size;
159 ssize_t ret;
161 buf_size = port->elem->out_sg[i].iov_len - port->iov_offset;
162 ret = vsc->have_data(port,
163 port->elem->out_sg[i].iov_base
164 + port->iov_offset,
165 buf_size);
166 if (port->throttled) {
167 port->iov_idx = i;
168 if (ret > 0) {
169 port->iov_offset += ret;
171 break;
173 port->iov_offset = 0;
175 if (port->throttled) {
176 break;
178 virtqueue_push(vq, port->elem, 0);
179 g_free(port->elem);
180 port->elem = NULL;
182 virtio_notify(vdev, vq);
185 static void flush_queued_data(VirtIOSerialPort *port)
187 assert(port);
189 if (!virtio_queue_ready(port->ovq)) {
190 return;
192 do_flush_queued_data(port, port->ovq, VIRTIO_DEVICE(port->vser));
195 static size_t send_control_msg(VirtIOSerial *vser, void *buf, size_t len)
197 VirtQueueElement *elem;
198 VirtQueue *vq;
200 vq = vser->c_ivq;
201 if (!virtio_queue_ready(vq)) {
202 return 0;
205 elem = virtqueue_pop(vq, sizeof(VirtQueueElement));
206 if (!elem) {
207 return 0;
210 /* TODO: detect a buffer that's too short, set NEEDS_RESET */
211 iov_from_buf(elem->in_sg, elem->in_num, 0, buf, len);
213 virtqueue_push(vq, elem, len);
214 virtio_notify(VIRTIO_DEVICE(vser), vq);
215 g_free(elem);
217 return len;
220 static size_t send_control_event(VirtIOSerial *vser, uint32_t port_id,
221 uint16_t event, uint16_t value)
223 VirtIODevice *vdev = VIRTIO_DEVICE(vser);
224 struct virtio_console_control cpkt;
226 virtio_stl_p(vdev, &cpkt.id, port_id);
227 virtio_stw_p(vdev, &cpkt.event, event);
228 virtio_stw_p(vdev, &cpkt.value, value);
230 trace_virtio_serial_send_control_event(port_id, event, value);
231 return send_control_msg(vser, &cpkt, sizeof(cpkt));
234 /* Functions for use inside qemu to open and read from/write to ports */
235 int virtio_serial_open(VirtIOSerialPort *port)
237 /* Don't allow opening an already-open port */
238 if (port->host_connected) {
239 return 0;
241 /* Send port open notification to the guest */
242 port->host_connected = true;
243 send_control_event(port->vser, port->id, VIRTIO_CONSOLE_PORT_OPEN, 1);
245 return 0;
248 int virtio_serial_close(VirtIOSerialPort *port)
250 port->host_connected = false;
252 * If there's any data the guest sent which the app didn't
253 * consume, reset the throttling flag and discard the data.
255 port->throttled = false;
256 discard_vq_data(port->ovq, VIRTIO_DEVICE(port->vser));
258 send_control_event(port->vser, port->id, VIRTIO_CONSOLE_PORT_OPEN, 0);
260 return 0;
263 /* Individual ports/apps call this function to write to the guest. */
264 ssize_t virtio_serial_write(VirtIOSerialPort *port, const uint8_t *buf,
265 size_t size)
267 if (!port || !port->host_connected || !port->guest_connected) {
268 return 0;
270 return write_to_port(port, buf, size);
274 * Readiness of the guest to accept data on a port.
275 * Returns max. data the guest can receive
277 size_t virtio_serial_guest_ready(VirtIOSerialPort *port)
279 VirtIODevice *vdev = VIRTIO_DEVICE(port->vser);
280 VirtQueue *vq = port->ivq;
281 unsigned int bytes;
283 if (!virtio_queue_ready(vq) ||
284 !(vdev->status & VIRTIO_CONFIG_S_DRIVER_OK) ||
285 virtio_queue_empty(vq)) {
286 return 0;
288 if (use_multiport(port->vser) && !port->guest_connected) {
289 return 0;
291 virtqueue_get_avail_bytes(vq, &bytes, NULL, 4096, 0);
292 return bytes;
295 static void flush_queued_data_bh(void *opaque)
297 VirtIOSerialPort *port = opaque;
299 flush_queued_data(port);
302 void virtio_serial_throttle_port(VirtIOSerialPort *port, bool throttle)
304 if (!port) {
305 return;
308 trace_virtio_serial_throttle_port(port->id, throttle);
309 port->throttled = throttle;
310 if (throttle) {
311 return;
313 qemu_bh_schedule(port->bh);
316 /* Guest wants to notify us of some event */
317 static void handle_control_message(VirtIOSerial *vser, void *buf, size_t len)
319 VirtIODevice *vdev = VIRTIO_DEVICE(vser);
320 struct VirtIOSerialPort *port;
321 VirtIOSerialPortClass *vsc;
322 struct virtio_console_control cpkt, *gcpkt;
323 uint8_t *buffer;
324 size_t buffer_len;
326 gcpkt = buf;
328 if (len < sizeof(cpkt)) {
329 /* The guest sent an invalid control packet */
330 return;
333 cpkt.event = virtio_lduw_p(vdev, &gcpkt->event);
334 cpkt.value = virtio_lduw_p(vdev, &gcpkt->value);
336 trace_virtio_serial_handle_control_message(cpkt.event, cpkt.value);
338 if (cpkt.event == VIRTIO_CONSOLE_DEVICE_READY) {
339 if (!cpkt.value) {
340 error_report("virtio-serial-bus: Guest failure in adding device %s",
341 vser->bus.qbus.name);
342 return;
345 * The device is up, we can now tell the device about all the
346 * ports we have here.
348 QTAILQ_FOREACH(port, &vser->ports, next) {
349 send_control_event(vser, port->id, VIRTIO_CONSOLE_PORT_ADD, 1);
351 return;
354 port = find_port_by_id(vser, virtio_ldl_p(vdev, &gcpkt->id));
355 if (!port) {
356 error_report("virtio-serial-bus: Unexpected port id %u for device %s",
357 virtio_ldl_p(vdev, &gcpkt->id), vser->bus.qbus.name);
358 return;
361 trace_virtio_serial_handle_control_message_port(port->id);
363 vsc = VIRTIO_SERIAL_PORT_GET_CLASS(port);
365 switch(cpkt.event) {
366 case VIRTIO_CONSOLE_PORT_READY:
367 if (!cpkt.value) {
368 error_report("virtio-serial-bus: Guest failure in adding port %u for device %s",
369 port->id, vser->bus.qbus.name);
370 break;
373 * Now that we know the guest asked for the port name, we're
374 * sure the guest has initialised whatever state is necessary
375 * for this port. Now's a good time to let the guest know if
376 * this port is a console port so that the guest can hook it
377 * up to hvc.
379 if (vsc->is_console) {
380 send_control_event(vser, port->id, VIRTIO_CONSOLE_CONSOLE_PORT, 1);
383 if (port->name) {
384 virtio_stl_p(vdev, &cpkt.id, port->id);
385 virtio_stw_p(vdev, &cpkt.event, VIRTIO_CONSOLE_PORT_NAME);
386 virtio_stw_p(vdev, &cpkt.value, 1);
388 buffer_len = sizeof(cpkt) + strlen(port->name) + 1;
389 buffer = g_malloc(buffer_len);
391 memcpy(buffer, &cpkt, sizeof(cpkt));
392 memcpy(buffer + sizeof(cpkt), port->name, strlen(port->name));
393 buffer[buffer_len - 1] = 0;
395 send_control_msg(vser, buffer, buffer_len);
396 g_free(buffer);
399 if (port->host_connected) {
400 send_control_event(vser, port->id, VIRTIO_CONSOLE_PORT_OPEN, 1);
404 * When the guest has asked us for this information it means
405 * the guest is all setup and has its virtqueues
406 * initialised. If some app is interested in knowing about
407 * this event, let it know.
409 if (vsc->guest_ready) {
410 vsc->guest_ready(port);
412 break;
414 case VIRTIO_CONSOLE_PORT_OPEN:
415 port->guest_connected = cpkt.value;
416 if (vsc->set_guest_connected) {
417 /* Send the guest opened notification if an app is interested */
418 vsc->set_guest_connected(port, cpkt.value);
420 break;
424 static void control_in(VirtIODevice *vdev, VirtQueue *vq)
428 static void control_out(VirtIODevice *vdev, VirtQueue *vq)
430 VirtQueueElement *elem;
431 VirtIOSerial *vser;
432 uint8_t *buf;
433 size_t len;
435 vser = VIRTIO_SERIAL(vdev);
437 len = 0;
438 buf = NULL;
439 for (;;) {
440 size_t cur_len;
442 elem = virtqueue_pop(vq, sizeof(VirtQueueElement));
443 if (!elem) {
444 break;
447 cur_len = iov_size(elem->out_sg, elem->out_num);
449 * Allocate a new buf only if we didn't have one previously or
450 * if the size of the buf differs
452 if (cur_len > len) {
453 g_free(buf);
455 buf = g_malloc(cur_len);
456 len = cur_len;
458 iov_to_buf(elem->out_sg, elem->out_num, 0, buf, cur_len);
460 handle_control_message(vser, buf, cur_len);
461 virtqueue_push(vq, elem, 0);
462 g_free(elem);
464 g_free(buf);
465 virtio_notify(vdev, vq);
468 /* Guest wrote something to some port. */
469 static void handle_output(VirtIODevice *vdev, VirtQueue *vq)
471 VirtIOSerial *vser;
472 VirtIOSerialPort *port;
474 vser = VIRTIO_SERIAL(vdev);
475 port = find_port_by_vq(vser, vq);
477 if (!port || !port->host_connected) {
478 discard_vq_data(vq, vdev);
479 return;
482 if (!port->throttled) {
483 do_flush_queued_data(port, vq, vdev);
484 return;
488 static void handle_input(VirtIODevice *vdev, VirtQueue *vq)
491 * Users of virtio-serial would like to know when guest becomes
492 * writable again -- i.e. if a vq had stuff queued up and the
493 * guest wasn't reading at all, the host would not be able to
494 * write to the vq anymore. Once the guest reads off something,
495 * we can start queueing things up again. However, this call is
496 * made for each buffer addition by the guest -- even though free
497 * buffers existed prior to the current buffer addition. This is
498 * done so as not to maintain previous state, which will need
499 * additional live-migration-related changes.
501 VirtIOSerial *vser;
502 VirtIOSerialPort *port;
503 VirtIOSerialPortClass *vsc;
505 vser = VIRTIO_SERIAL(vdev);
506 port = find_port_by_vq(vser, vq);
508 if (!port) {
509 return;
511 vsc = VIRTIO_SERIAL_PORT_GET_CLASS(port);
514 * If guest_connected is false, this call is being made by the
515 * early-boot queueing up of descriptors, which is just noise for
516 * the host apps -- don't disturb them in that case.
518 if (port->guest_connected && port->host_connected && vsc->guest_writable) {
519 vsc->guest_writable(port);
523 static uint64_t get_features(VirtIODevice *vdev, uint64_t features,
524 Error **errp)
526 VirtIOSerial *vser;
528 vser = VIRTIO_SERIAL(vdev);
530 if (vser->bus.max_nr_ports > 1) {
531 virtio_add_feature(&features, VIRTIO_CONSOLE_F_MULTIPORT);
533 return features;
536 /* Guest requested config info */
537 static void get_config(VirtIODevice *vdev, uint8_t *config_data)
539 VirtIOSerial *vser = VIRTIO_SERIAL(vdev);
540 struct virtio_console_config *config =
541 (struct virtio_console_config *)config_data;
543 config->cols = 0;
544 config->rows = 0;
545 config->max_nr_ports = virtio_tswap32(vdev,
546 vser->serial.max_virtserial_ports);
549 static void guest_reset(VirtIOSerial *vser)
551 VirtIOSerialPort *port;
552 VirtIOSerialPortClass *vsc;
554 QTAILQ_FOREACH(port, &vser->ports, next) {
555 vsc = VIRTIO_SERIAL_PORT_GET_CLASS(port);
556 if (port->guest_connected) {
557 port->guest_connected = false;
558 if (vsc->set_guest_connected) {
559 vsc->set_guest_connected(port, false);
565 static void set_status(VirtIODevice *vdev, uint8_t status)
567 VirtIOSerial *vser;
568 VirtIOSerialPort *port;
570 vser = VIRTIO_SERIAL(vdev);
571 port = find_port_by_id(vser, 0);
573 if (port && !use_multiport(port->vser)
574 && (status & VIRTIO_CONFIG_S_DRIVER_OK)) {
576 * Non-multiport guests won't be able to tell us guest
577 * open/close status. Such guests can only have a port at id
578 * 0, so set guest_connected for such ports as soon as guest
579 * is up.
581 port->guest_connected = true;
583 if (!(status & VIRTIO_CONFIG_S_DRIVER_OK)) {
584 guest_reset(vser);
588 static void vser_reset(VirtIODevice *vdev)
590 VirtIOSerial *vser;
592 vser = VIRTIO_SERIAL(vdev);
593 guest_reset(vser);
596 static void virtio_serial_save(QEMUFile *f, void *opaque)
598 /* The virtio device */
599 virtio_save(VIRTIO_DEVICE(opaque), f);
602 static void virtio_serial_save_device(VirtIODevice *vdev, QEMUFile *f)
604 VirtIOSerial *s = VIRTIO_SERIAL(vdev);
605 VirtIOSerialPort *port;
606 uint32_t nr_active_ports;
607 unsigned int i, max_nr_ports;
608 struct virtio_console_config config;
610 /* The config space (ignored on the far end in current versions) */
611 get_config(vdev, (uint8_t *)&config);
612 qemu_put_be16s(f, &config.cols);
613 qemu_put_be16s(f, &config.rows);
614 qemu_put_be32s(f, &config.max_nr_ports);
616 /* The ports map */
617 max_nr_ports = s->serial.max_virtserial_ports;
618 for (i = 0; i < (max_nr_ports + 31) / 32; i++) {
619 qemu_put_be32s(f, &s->ports_map[i]);
622 /* Ports */
624 nr_active_ports = 0;
625 QTAILQ_FOREACH(port, &s->ports, next) {
626 nr_active_ports++;
629 qemu_put_be32s(f, &nr_active_ports);
632 * Items in struct VirtIOSerialPort.
634 QTAILQ_FOREACH(port, &s->ports, next) {
635 uint32_t elem_popped;
637 qemu_put_be32s(f, &port->id);
638 qemu_put_byte(f, port->guest_connected);
639 qemu_put_byte(f, port->host_connected);
641 elem_popped = 0;
642 if (port->elem) {
643 elem_popped = 1;
645 qemu_put_be32s(f, &elem_popped);
646 if (elem_popped) {
647 qemu_put_be32s(f, &port->iov_idx);
648 qemu_put_be64s(f, &port->iov_offset);
649 qemu_put_virtqueue_element(f, port->elem);
654 static void virtio_serial_post_load_timer_cb(void *opaque)
656 uint32_t i;
657 VirtIOSerial *s = VIRTIO_SERIAL(opaque);
658 VirtIOSerialPort *port;
659 uint8_t host_connected;
660 VirtIOSerialPortClass *vsc;
662 if (!s->post_load) {
663 return;
665 for (i = 0 ; i < s->post_load->nr_active_ports; ++i) {
666 port = s->post_load->connected[i].port;
667 host_connected = s->post_load->connected[i].host_connected;
668 if (host_connected != port->host_connected) {
670 * We have to let the guest know of the host connection
671 * status change
673 send_control_event(s, port->id, VIRTIO_CONSOLE_PORT_OPEN,
674 port->host_connected);
676 vsc = VIRTIO_SERIAL_PORT_GET_CLASS(port);
677 if (vsc->set_guest_connected) {
678 vsc->set_guest_connected(port, port->guest_connected);
681 g_free(s->post_load->connected);
682 timer_free(s->post_load->timer);
683 g_free(s->post_load);
684 s->post_load = NULL;
687 static int fetch_active_ports_list(QEMUFile *f, int version_id,
688 VirtIOSerial *s, uint32_t nr_active_ports)
690 uint32_t i;
692 s->post_load = g_malloc0(sizeof(*s->post_load));
693 s->post_load->nr_active_ports = nr_active_ports;
694 s->post_load->connected =
695 g_malloc0(sizeof(*s->post_load->connected) * nr_active_ports);
697 s->post_load->timer = timer_new_ns(QEMU_CLOCK_VIRTUAL,
698 virtio_serial_post_load_timer_cb,
701 /* Items in struct VirtIOSerialPort */
702 for (i = 0; i < nr_active_ports; i++) {
703 VirtIOSerialPort *port;
704 uint32_t id;
706 id = qemu_get_be32(f);
707 port = find_port_by_id(s, id);
708 if (!port) {
709 return -EINVAL;
712 port->guest_connected = qemu_get_byte(f);
713 s->post_load->connected[i].port = port;
714 s->post_load->connected[i].host_connected = qemu_get_byte(f);
716 if (version_id > 2) {
717 uint32_t elem_popped;
719 qemu_get_be32s(f, &elem_popped);
720 if (elem_popped) {
721 qemu_get_be32s(f, &port->iov_idx);
722 qemu_get_be64s(f, &port->iov_offset);
724 port->elem =
725 qemu_get_virtqueue_element(f, sizeof(VirtQueueElement));
728 * Port was throttled on source machine. Let's
729 * unthrottle it here so data starts flowing again.
731 virtio_serial_throttle_port(port, false);
735 timer_mod(s->post_load->timer, 1);
736 return 0;
739 static int virtio_serial_load(QEMUFile *f, void *opaque, int version_id)
741 if (version_id > 3) {
742 return -EINVAL;
745 /* The virtio device */
746 return virtio_load(VIRTIO_DEVICE(opaque), f, version_id);
749 static int virtio_serial_load_device(VirtIODevice *vdev, QEMUFile *f,
750 int version_id)
752 VirtIOSerial *s = VIRTIO_SERIAL(vdev);
753 uint32_t max_nr_ports, nr_active_ports, ports_map;
754 unsigned int i;
755 int ret;
756 uint32_t tmp;
758 if (version_id < 2) {
759 return 0;
762 /* Unused */
763 qemu_get_be16s(f, (uint16_t *) &tmp);
764 qemu_get_be16s(f, (uint16_t *) &tmp);
765 qemu_get_be32s(f, &tmp);
767 max_nr_ports = s->serial.max_virtserial_ports;
768 for (i = 0; i < (max_nr_ports + 31) / 32; i++) {
769 qemu_get_be32s(f, &ports_map);
771 if (ports_map != s->ports_map[i]) {
773 * Ports active on source and destination don't
774 * match. Fail migration.
776 return -EINVAL;
780 qemu_get_be32s(f, &nr_active_ports);
782 if (nr_active_ports) {
783 ret = fetch_active_ports_list(f, version_id, s, nr_active_ports);
784 if (ret) {
785 return ret;
788 return 0;
791 static void virtser_bus_dev_print(Monitor *mon, DeviceState *qdev, int indent);
793 static Property virtser_props[] = {
794 DEFINE_PROP_UINT32("nr", VirtIOSerialPort, id, VIRTIO_CONSOLE_BAD_ID),
795 DEFINE_PROP_STRING("name", VirtIOSerialPort, name),
796 DEFINE_PROP_END_OF_LIST()
799 #define TYPE_VIRTIO_SERIAL_BUS "virtio-serial-bus"
800 #define VIRTIO_SERIAL_BUS(obj) \
801 OBJECT_CHECK(VirtIOSerialBus, (obj), TYPE_VIRTIO_SERIAL_BUS)
803 static void virtser_bus_class_init(ObjectClass *klass, void *data)
805 BusClass *k = BUS_CLASS(klass);
806 k->print_dev = virtser_bus_dev_print;
809 static const TypeInfo virtser_bus_info = {
810 .name = TYPE_VIRTIO_SERIAL_BUS,
811 .parent = TYPE_BUS,
812 .instance_size = sizeof(VirtIOSerialBus),
813 .class_init = virtser_bus_class_init,
816 static void virtser_bus_dev_print(Monitor *mon, DeviceState *qdev, int indent)
818 VirtIOSerialPort *port = VIRTIO_SERIAL_PORT(qdev);
820 monitor_printf(mon, "%*sport %d, guest %s, host %s, throttle %s\n",
821 indent, "", port->id,
822 port->guest_connected ? "on" : "off",
823 port->host_connected ? "on" : "off",
824 port->throttled ? "on" : "off");
827 /* This function is only used if a port id is not provided by the user */
828 static uint32_t find_free_port_id(VirtIOSerial *vser)
830 unsigned int i, max_nr_ports;
832 max_nr_ports = vser->serial.max_virtserial_ports;
833 for (i = 0; i < (max_nr_ports + 31) / 32; i++) {
834 uint32_t map, zeroes;
836 map = vser->ports_map[i];
837 zeroes = ctz32(~map);
838 if (zeroes != 32) {
839 return zeroes + i * 32;
842 return VIRTIO_CONSOLE_BAD_ID;
845 static void mark_port_added(VirtIOSerial *vser, uint32_t port_id)
847 unsigned int i;
849 i = port_id / 32;
850 vser->ports_map[i] |= 1U << (port_id % 32);
853 static void add_port(VirtIOSerial *vser, uint32_t port_id)
855 mark_port_added(vser, port_id);
856 send_control_event(vser, port_id, VIRTIO_CONSOLE_PORT_ADD, 1);
859 static void remove_port(VirtIOSerial *vser, uint32_t port_id)
861 VirtIOSerialPort *port;
864 * Don't mark port 0 removed -- we explicitly reserve it for
865 * backward compat with older guests, ensure a virtconsole device
866 * unplug retains the reservation.
868 if (port_id) {
869 unsigned int i;
871 i = port_id / 32;
872 vser->ports_map[i] &= ~(1U << (port_id % 32));
875 port = find_port_by_id(vser, port_id);
877 * This function is only called from qdev's unplug callback; if we
878 * get a NULL port here, we're in trouble.
880 assert(port);
882 /* Flush out any unconsumed buffers first */
883 discard_vq_data(port->ovq, VIRTIO_DEVICE(port->vser));
885 send_control_event(vser, port->id, VIRTIO_CONSOLE_PORT_REMOVE, 1);
888 static void virtser_port_device_realize(DeviceState *dev, Error **errp)
890 VirtIOSerialPort *port = VIRTIO_SERIAL_PORT(dev);
891 VirtIOSerialPortClass *vsc = VIRTIO_SERIAL_PORT_GET_CLASS(port);
892 VirtIOSerialBus *bus = VIRTIO_SERIAL_BUS(qdev_get_parent_bus(dev));
893 int max_nr_ports;
894 bool plugging_port0;
895 Error *err = NULL;
897 port->vser = bus->vser;
898 port->bh = qemu_bh_new(flush_queued_data_bh, port);
900 assert(vsc->have_data);
903 * Is the first console port we're seeing? If so, put it up at
904 * location 0. This is done for backward compatibility (old
905 * kernel, new qemu).
907 plugging_port0 = vsc->is_console && !find_port_by_id(port->vser, 0);
909 if (find_port_by_id(port->vser, port->id)) {
910 error_setg(errp, "virtio-serial-bus: A port already exists at id %u",
911 port->id);
912 return;
915 if (port->name != NULL && find_port_by_name(port->name)) {
916 error_setg(errp, "virtio-serial-bus: A port already exists by name %s",
917 port->name);
918 return;
921 if (port->id == VIRTIO_CONSOLE_BAD_ID) {
922 if (plugging_port0) {
923 port->id = 0;
924 } else {
925 port->id = find_free_port_id(port->vser);
926 if (port->id == VIRTIO_CONSOLE_BAD_ID) {
927 error_setg(errp, "virtio-serial-bus: Maximum port limit for "
928 "this device reached");
929 return;
934 max_nr_ports = port->vser->serial.max_virtserial_ports;
935 if (port->id >= max_nr_ports) {
936 error_setg(errp, "virtio-serial-bus: Out-of-range port id specified, "
937 "max. allowed: %u", max_nr_ports - 1);
938 return;
941 vsc->realize(dev, &err);
942 if (err != NULL) {
943 error_propagate(errp, err);
944 return;
947 port->elem = NULL;
950 static void virtser_port_device_plug(HotplugHandler *hotplug_dev,
951 DeviceState *dev, Error **errp)
953 VirtIOSerialPort *port = VIRTIO_SERIAL_PORT(dev);
955 QTAILQ_INSERT_TAIL(&port->vser->ports, port, next);
956 port->ivq = port->vser->ivqs[port->id];
957 port->ovq = port->vser->ovqs[port->id];
959 add_port(port->vser, port->id);
961 /* Send an update to the guest about this new port added */
962 virtio_notify_config(VIRTIO_DEVICE(hotplug_dev));
965 static void virtser_port_device_unrealize(DeviceState *dev, Error **errp)
967 VirtIOSerialPort *port = VIRTIO_SERIAL_PORT(dev);
968 VirtIOSerialPortClass *vsc = VIRTIO_SERIAL_PORT_GET_CLASS(dev);
969 VirtIOSerial *vser = port->vser;
971 qemu_bh_delete(port->bh);
972 remove_port(port->vser, port->id);
974 QTAILQ_REMOVE(&vser->ports, port, next);
976 if (vsc->unrealize) {
977 vsc->unrealize(dev, errp);
981 static void virtio_serial_device_realize(DeviceState *dev, Error **errp)
983 VirtIODevice *vdev = VIRTIO_DEVICE(dev);
984 VirtIOSerial *vser = VIRTIO_SERIAL(dev);
985 uint32_t i, max_supported_ports;
987 if (!vser->serial.max_virtserial_ports) {
988 error_setg(errp, "Maximum number of serial ports not specified");
989 return;
992 /* Each port takes 2 queues, and one pair is for the control queue */
993 max_supported_ports = VIRTIO_QUEUE_MAX / 2 - 1;
995 if (vser->serial.max_virtserial_ports > max_supported_ports) {
996 error_setg(errp, "maximum ports supported: %u", max_supported_ports);
997 return;
1000 /* We don't support emergency write, skip it for now. */
1001 /* TODO: cleaner fix, depending on host features. */
1002 virtio_init(vdev, "virtio-serial", VIRTIO_ID_CONSOLE,
1003 offsetof(struct virtio_console_config, emerg_wr));
1005 /* Spawn a new virtio-serial bus on which the ports will ride as devices */
1006 qbus_create_inplace(&vser->bus, sizeof(vser->bus), TYPE_VIRTIO_SERIAL_BUS,
1007 dev, vdev->bus_name);
1008 qbus_set_hotplug_handler(BUS(&vser->bus), DEVICE(vser), errp);
1009 vser->bus.vser = vser;
1010 QTAILQ_INIT(&vser->ports);
1012 vser->bus.max_nr_ports = vser->serial.max_virtserial_ports;
1013 vser->ivqs = g_malloc(vser->serial.max_virtserial_ports
1014 * sizeof(VirtQueue *));
1015 vser->ovqs = g_malloc(vser->serial.max_virtserial_ports
1016 * sizeof(VirtQueue *));
1018 /* Add a queue for host to guest transfers for port 0 (backward compat) */
1019 vser->ivqs[0] = virtio_add_queue(vdev, 128, handle_input);
1020 /* Add a queue for guest to host transfers for port 0 (backward compat) */
1021 vser->ovqs[0] = virtio_add_queue(vdev, 128, handle_output);
1023 /* TODO: host to guest notifications can get dropped
1024 * if the queue fills up. Implement queueing in host,
1025 * this might also make it possible to reduce the control
1026 * queue size: as guest preposts buffers there,
1027 * this will save 4Kbyte of guest memory per entry. */
1029 /* control queue: host to guest */
1030 vser->c_ivq = virtio_add_queue(vdev, 32, control_in);
1031 /* control queue: guest to host */
1032 vser->c_ovq = virtio_add_queue(vdev, 32, control_out);
1034 for (i = 1; i < vser->bus.max_nr_ports; i++) {
1035 /* Add a per-port queue for host to guest transfers */
1036 vser->ivqs[i] = virtio_add_queue(vdev, 128, handle_input);
1037 /* Add a per-per queue for guest to host transfers */
1038 vser->ovqs[i] = virtio_add_queue(vdev, 128, handle_output);
1041 vser->ports_map = g_malloc0(((vser->serial.max_virtserial_ports + 31) / 32)
1042 * sizeof(vser->ports_map[0]));
1044 * Reserve location 0 for a console port for backward compat
1045 * (old kernel, new qemu)
1047 mark_port_added(vser, 0);
1049 vser->post_load = NULL;
1052 * Register for the savevm section with the virtio-console name
1053 * to preserve backward compat
1055 register_savevm(dev, "virtio-console", -1, 3, virtio_serial_save,
1056 virtio_serial_load, vser);
1058 QLIST_INSERT_HEAD(&vserdevices.devices, vser, next);
1061 static void virtio_serial_port_class_init(ObjectClass *klass, void *data)
1063 DeviceClass *k = DEVICE_CLASS(klass);
1065 set_bit(DEVICE_CATEGORY_INPUT, k->categories);
1066 k->bus_type = TYPE_VIRTIO_SERIAL_BUS;
1067 k->realize = virtser_port_device_realize;
1068 k->unrealize = virtser_port_device_unrealize;
1069 k->props = virtser_props;
1072 static const TypeInfo virtio_serial_port_type_info = {
1073 .name = TYPE_VIRTIO_SERIAL_PORT,
1074 .parent = TYPE_DEVICE,
1075 .instance_size = sizeof(VirtIOSerialPort),
1076 .abstract = true,
1077 .class_size = sizeof(VirtIOSerialPortClass),
1078 .class_init = virtio_serial_port_class_init,
1081 static void virtio_serial_device_unrealize(DeviceState *dev, Error **errp)
1083 VirtIODevice *vdev = VIRTIO_DEVICE(dev);
1084 VirtIOSerial *vser = VIRTIO_SERIAL(dev);
1086 QLIST_REMOVE(vser, next);
1088 unregister_savevm(dev, "virtio-console", vser);
1090 g_free(vser->ivqs);
1091 g_free(vser->ovqs);
1092 g_free(vser->ports_map);
1093 if (vser->post_load) {
1094 g_free(vser->post_load->connected);
1095 timer_del(vser->post_load->timer);
1096 timer_free(vser->post_load->timer);
1097 g_free(vser->post_load);
1099 virtio_cleanup(vdev);
1102 static Property virtio_serial_properties[] = {
1103 DEFINE_PROP_UINT32("max_ports", VirtIOSerial, serial.max_virtserial_ports,
1104 31),
1105 DEFINE_PROP_END_OF_LIST(),
1108 static void virtio_serial_class_init(ObjectClass *klass, void *data)
1110 DeviceClass *dc = DEVICE_CLASS(klass);
1111 VirtioDeviceClass *vdc = VIRTIO_DEVICE_CLASS(klass);
1112 HotplugHandlerClass *hc = HOTPLUG_HANDLER_CLASS(klass);
1114 QLIST_INIT(&vserdevices.devices);
1116 dc->props = virtio_serial_properties;
1117 set_bit(DEVICE_CATEGORY_INPUT, dc->categories);
1118 vdc->realize = virtio_serial_device_realize;
1119 vdc->unrealize = virtio_serial_device_unrealize;
1120 vdc->get_features = get_features;
1121 vdc->get_config = get_config;
1122 vdc->set_status = set_status;
1123 vdc->reset = vser_reset;
1124 vdc->save = virtio_serial_save_device;
1125 vdc->load = virtio_serial_load_device;
1126 hc->plug = virtser_port_device_plug;
1127 hc->unplug = qdev_simple_device_unplug_cb;
1130 static const TypeInfo virtio_device_info = {
1131 .name = TYPE_VIRTIO_SERIAL,
1132 .parent = TYPE_VIRTIO_DEVICE,
1133 .instance_size = sizeof(VirtIOSerial),
1134 .class_init = virtio_serial_class_init,
1135 .interfaces = (InterfaceInfo[]) {
1136 { TYPE_HOTPLUG_HANDLER },
1141 static void virtio_serial_register_types(void)
1143 type_register_static(&virtser_bus_info);
1144 type_register_static(&virtio_serial_port_type_info);
1145 type_register_static(&virtio_device_info);
1148 type_init(virtio_serial_register_types)