]> git.proxmox.com Git - qemu.git/blob - hw/virtio-serial-bus.c
Avoid asprintf() which is not available on mingw
[qemu.git] / hw / virtio-serial-bus.c
1 /*
2 * A bus for connecting virtio serial and console ports
3 *
4 * Copyright (C) 2009, 2010 Red Hat, Inc.
5 *
6 * Author(s):
7 * Amit Shah <amit.shah@redhat.com>
8 *
9 * Some earlier parts are:
10 * Copyright IBM, Corp. 2008
11 * authored by
12 * Christian Ehrhardt <ehrhardt@linux.vnet.ibm.com>
13 *
14 * This work is licensed under the terms of the GNU GPL, version 2. See
15 * the COPYING file in the top-level directory.
16 *
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.
19 */
20
21 #include "iov.h"
22 #include "monitor.h"
23 #include "qemu-queue.h"
24 #include "sysbus.h"
25 #include "trace.h"
26 #include "virtio-serial.h"
27
28 /* The virtio-serial bus on top of which the ports will ride as devices */
29 struct VirtIOSerialBus {
30 BusState qbus;
31
32 /* This is the parent device that provides the bus for ports. */
33 VirtIOSerial *vser;
34
35 /* The maximum number of ports that can ride on top of this bus */
36 uint32_t max_nr_ports;
37 };
38
39 struct VirtIOSerial {
40 VirtIODevice vdev;
41
42 VirtQueue *c_ivq, *c_ovq;
43 /* Arrays of ivqs and ovqs: one per port */
44 VirtQueue **ivqs, **ovqs;
45
46 VirtIOSerialBus bus;
47
48 DeviceState *qdev;
49
50 QTAILQ_HEAD(, VirtIOSerialPort) ports;
51
52 /* bitmap for identifying active ports */
53 uint32_t *ports_map;
54
55 struct virtio_console_config config;
56 };
57
58 static VirtIOSerialPort *find_port_by_id(VirtIOSerial *vser, uint32_t id)
59 {
60 VirtIOSerialPort *port;
61
62 if (id == VIRTIO_CONSOLE_BAD_ID) {
63 return NULL;
64 }
65
66 QTAILQ_FOREACH(port, &vser->ports, next) {
67 if (port->id == id)
68 return port;
69 }
70 return NULL;
71 }
72
73 static VirtIOSerialPort *find_port_by_vq(VirtIOSerial *vser, VirtQueue *vq)
74 {
75 VirtIOSerialPort *port;
76
77 QTAILQ_FOREACH(port, &vser->ports, next) {
78 if (port->ivq == vq || port->ovq == vq)
79 return port;
80 }
81 return NULL;
82 }
83
84 static bool use_multiport(VirtIOSerial *vser)
85 {
86 return vser->vdev.guest_features & (1 << VIRTIO_CONSOLE_F_MULTIPORT);
87 }
88
89 static size_t write_to_port(VirtIOSerialPort *port,
90 const uint8_t *buf, size_t size)
91 {
92 VirtQueueElement elem;
93 VirtQueue *vq;
94 size_t offset;
95
96 vq = port->ivq;
97 if (!virtio_queue_ready(vq)) {
98 return 0;
99 }
100
101 offset = 0;
102 while (offset < size) {
103 size_t len;
104
105 if (!virtqueue_pop(vq, &elem)) {
106 break;
107 }
108
109 len = iov_from_buf(elem.in_sg, elem.in_num, 0,
110 buf + offset, size - offset);
111 offset += len;
112
113 virtqueue_push(vq, &elem, len);
114 }
115
116 virtio_notify(&port->vser->vdev, vq);
117 return offset;
118 }
119
120 static void discard_vq_data(VirtQueue *vq, VirtIODevice *vdev)
121 {
122 VirtQueueElement elem;
123
124 if (!virtio_queue_ready(vq)) {
125 return;
126 }
127 while (virtqueue_pop(vq, &elem)) {
128 virtqueue_push(vq, &elem, 0);
129 }
130 virtio_notify(vdev, vq);
131 }
132
133 static void do_flush_queued_data(VirtIOSerialPort *port, VirtQueue *vq,
134 VirtIODevice *vdev)
135 {
136 VirtIOSerialPortClass *vsc;
137
138 assert(port);
139 assert(virtio_queue_ready(vq));
140
141 vsc = VIRTIO_SERIAL_PORT_GET_CLASS(port);
142
143 while (!port->throttled) {
144 unsigned int i;
145
146 /* Pop an elem only if we haven't left off a previous one mid-way */
147 if (!port->elem.out_num) {
148 if (!virtqueue_pop(vq, &port->elem)) {
149 break;
150 }
151 port->iov_idx = 0;
152 port->iov_offset = 0;
153 }
154
155 for (i = port->iov_idx; i < port->elem.out_num; i++) {
156 size_t buf_size;
157 ssize_t ret;
158
159 buf_size = port->elem.out_sg[i].iov_len - port->iov_offset;
160 ret = vsc->have_data(port,
161 port->elem.out_sg[i].iov_base
162 + port->iov_offset,
163 buf_size);
164 if (ret < 0 && ret != -EAGAIN) {
165 /* We don't handle any other type of errors here */
166 abort();
167 }
168 if (ret == -EAGAIN || (ret >= 0 && ret < buf_size)) {
169 /*
170 * this is a temporary check until chardevs can signal to
171 * frontends that they are writable again. This prevents
172 * the console from going into throttled mode (forever)
173 * if virtio-console is connected to a pty without a
174 * listener. Otherwise the guest spins forever.
175 * We can revert this if
176 * 1: chardevs can notify frondends
177 * 2: the guest driver does not spin in these cases
178 */
179 if (!vsc->is_console) {
180 virtio_serial_throttle_port(port, true);
181 }
182 port->iov_idx = i;
183 if (ret > 0) {
184 port->iov_offset += ret;
185 }
186 break;
187 }
188 port->iov_offset = 0;
189 }
190 if (port->throttled) {
191 break;
192 }
193 virtqueue_push(vq, &port->elem, 0);
194 port->elem.out_num = 0;
195 }
196 virtio_notify(vdev, vq);
197 }
198
199 static void flush_queued_data(VirtIOSerialPort *port)
200 {
201 assert(port);
202
203 if (!virtio_queue_ready(port->ovq)) {
204 return;
205 }
206 do_flush_queued_data(port, port->ovq, &port->vser->vdev);
207 }
208
209 static size_t send_control_msg(VirtIOSerialPort *port, void *buf, size_t len)
210 {
211 VirtQueueElement elem;
212 VirtQueue *vq;
213 struct virtio_console_control *cpkt;
214
215 vq = port->vser->c_ivq;
216 if (!virtio_queue_ready(vq)) {
217 return 0;
218 }
219 if (!virtqueue_pop(vq, &elem)) {
220 return 0;
221 }
222
223 cpkt = (struct virtio_console_control *)buf;
224 stl_p(&cpkt->id, port->id);
225 memcpy(elem.in_sg[0].iov_base, buf, len);
226
227 virtqueue_push(vq, &elem, len);
228 virtio_notify(&port->vser->vdev, vq);
229 return len;
230 }
231
232 static size_t send_control_event(VirtIOSerialPort *port, uint16_t event,
233 uint16_t value)
234 {
235 struct virtio_console_control cpkt;
236
237 stw_p(&cpkt.event, event);
238 stw_p(&cpkt.value, value);
239
240 trace_virtio_serial_send_control_event(port->id, event, value);
241 return send_control_msg(port, &cpkt, sizeof(cpkt));
242 }
243
244 /* Functions for use inside qemu to open and read from/write to ports */
245 int virtio_serial_open(VirtIOSerialPort *port)
246 {
247 /* Don't allow opening an already-open port */
248 if (port->host_connected) {
249 return 0;
250 }
251 /* Send port open notification to the guest */
252 port->host_connected = true;
253 send_control_event(port, VIRTIO_CONSOLE_PORT_OPEN, 1);
254
255 return 0;
256 }
257
258 int virtio_serial_close(VirtIOSerialPort *port)
259 {
260 port->host_connected = false;
261 /*
262 * If there's any data the guest sent which the app didn't
263 * consume, reset the throttling flag and discard the data.
264 */
265 port->throttled = false;
266 discard_vq_data(port->ovq, &port->vser->vdev);
267
268 send_control_event(port, VIRTIO_CONSOLE_PORT_OPEN, 0);
269
270 return 0;
271 }
272
273 /* Individual ports/apps call this function to write to the guest. */
274 ssize_t virtio_serial_write(VirtIOSerialPort *port, const uint8_t *buf,
275 size_t size)
276 {
277 if (!port || !port->host_connected || !port->guest_connected) {
278 return 0;
279 }
280 return write_to_port(port, buf, size);
281 }
282
283 /*
284 * Readiness of the guest to accept data on a port.
285 * Returns max. data the guest can receive
286 */
287 size_t virtio_serial_guest_ready(VirtIOSerialPort *port)
288 {
289 VirtQueue *vq = port->ivq;
290
291 if (!virtio_queue_ready(vq) ||
292 !(port->vser->vdev.status & VIRTIO_CONFIG_S_DRIVER_OK) ||
293 virtio_queue_empty(vq)) {
294 return 0;
295 }
296 if (use_multiport(port->vser) && !port->guest_connected) {
297 return 0;
298 }
299
300 if (virtqueue_avail_bytes(vq, 4096, 0)) {
301 return 4096;
302 }
303 if (virtqueue_avail_bytes(vq, 1, 0)) {
304 return 1;
305 }
306 return 0;
307 }
308
309 static void flush_queued_data_bh(void *opaque)
310 {
311 VirtIOSerialPort *port = opaque;
312
313 flush_queued_data(port);
314 }
315
316 void virtio_serial_throttle_port(VirtIOSerialPort *port, bool throttle)
317 {
318 if (!port) {
319 return;
320 }
321
322 trace_virtio_serial_throttle_port(port->id, throttle);
323 port->throttled = throttle;
324 if (throttle) {
325 return;
326 }
327 qemu_bh_schedule(port->bh);
328 }
329
330 /* Guest wants to notify us of some event */
331 static void handle_control_message(VirtIOSerial *vser, void *buf, size_t len)
332 {
333 struct VirtIOSerialPort *port;
334 VirtIOSerialPortClass *vsc;
335 struct virtio_console_control cpkt, *gcpkt;
336 uint8_t *buffer;
337 size_t buffer_len;
338
339 gcpkt = buf;
340
341 if (len < sizeof(cpkt)) {
342 /* The guest sent an invalid control packet */
343 return;
344 }
345
346 cpkt.event = lduw_p(&gcpkt->event);
347 cpkt.value = lduw_p(&gcpkt->value);
348
349 trace_virtio_serial_handle_control_message(cpkt.event, cpkt.value);
350
351 if (cpkt.event == VIRTIO_CONSOLE_DEVICE_READY) {
352 if (!cpkt.value) {
353 error_report("virtio-serial-bus: Guest failure in adding device %s",
354 vser->bus.qbus.name);
355 return;
356 }
357 /*
358 * The device is up, we can now tell the device about all the
359 * ports we have here.
360 */
361 QTAILQ_FOREACH(port, &vser->ports, next) {
362 send_control_event(port, VIRTIO_CONSOLE_PORT_ADD, 1);
363 }
364 return;
365 }
366
367 port = find_port_by_id(vser, ldl_p(&gcpkt->id));
368 if (!port) {
369 error_report("virtio-serial-bus: Unexpected port id %u for device %s",
370 ldl_p(&gcpkt->id), vser->bus.qbus.name);
371 return;
372 }
373
374 trace_virtio_serial_handle_control_message_port(port->id);
375
376 vsc = VIRTIO_SERIAL_PORT_GET_CLASS(port);
377
378 switch(cpkt.event) {
379 case VIRTIO_CONSOLE_PORT_READY:
380 if (!cpkt.value) {
381 error_report("virtio-serial-bus: Guest failure in adding port %u for device %s",
382 port->id, vser->bus.qbus.name);
383 break;
384 }
385 /*
386 * Now that we know the guest asked for the port name, we're
387 * sure the guest has initialised whatever state is necessary
388 * for this port. Now's a good time to let the guest know if
389 * this port is a console port so that the guest can hook it
390 * up to hvc.
391 */
392 if (vsc->is_console) {
393 send_control_event(port, VIRTIO_CONSOLE_CONSOLE_PORT, 1);
394 }
395
396 if (port->name) {
397 stw_p(&cpkt.event, VIRTIO_CONSOLE_PORT_NAME);
398 stw_p(&cpkt.value, 1);
399
400 buffer_len = sizeof(cpkt) + strlen(port->name) + 1;
401 buffer = g_malloc(buffer_len);
402
403 memcpy(buffer, &cpkt, sizeof(cpkt));
404 memcpy(buffer + sizeof(cpkt), port->name, strlen(port->name));
405 buffer[buffer_len - 1] = 0;
406
407 send_control_msg(port, buffer, buffer_len);
408 g_free(buffer);
409 }
410
411 if (port->host_connected) {
412 send_control_event(port, VIRTIO_CONSOLE_PORT_OPEN, 1);
413 }
414
415 /*
416 * When the guest has asked us for this information it means
417 * the guest is all setup and has its virtqueues
418 * initialised. If some app is interested in knowing about
419 * this event, let it know.
420 */
421 if (vsc->guest_ready) {
422 vsc->guest_ready(port);
423 }
424 break;
425
426 case VIRTIO_CONSOLE_PORT_OPEN:
427 port->guest_connected = cpkt.value;
428 if (cpkt.value && vsc->guest_open) {
429 /* Send the guest opened notification if an app is interested */
430 vsc->guest_open(port);
431 }
432
433 if (!cpkt.value && vsc->guest_close) {
434 /* Send the guest closed notification if an app is interested */
435 vsc->guest_close(port);
436 }
437 break;
438 }
439 }
440
441 static void control_in(VirtIODevice *vdev, VirtQueue *vq)
442 {
443 }
444
445 static void control_out(VirtIODevice *vdev, VirtQueue *vq)
446 {
447 VirtQueueElement elem;
448 VirtIOSerial *vser;
449 uint8_t *buf;
450 size_t len;
451
452 vser = DO_UPCAST(VirtIOSerial, vdev, vdev);
453
454 len = 0;
455 buf = NULL;
456 while (virtqueue_pop(vq, &elem)) {
457 size_t cur_len;
458
459 cur_len = iov_size(elem.out_sg, elem.out_num);
460 /*
461 * Allocate a new buf only if we didn't have one previously or
462 * if the size of the buf differs
463 */
464 if (cur_len > len) {
465 g_free(buf);
466
467 buf = g_malloc(cur_len);
468 len = cur_len;
469 }
470 iov_to_buf(elem.out_sg, elem.out_num, 0, buf, cur_len);
471
472 handle_control_message(vser, buf, cur_len);
473 virtqueue_push(vq, &elem, 0);
474 }
475 g_free(buf);
476 virtio_notify(vdev, vq);
477 }
478
479 /* Guest wrote something to some port. */
480 static void handle_output(VirtIODevice *vdev, VirtQueue *vq)
481 {
482 VirtIOSerial *vser;
483 VirtIOSerialPort *port;
484
485 vser = DO_UPCAST(VirtIOSerial, vdev, vdev);
486 port = find_port_by_vq(vser, vq);
487
488 if (!port || !port->host_connected) {
489 discard_vq_data(vq, vdev);
490 return;
491 }
492
493 if (!port->throttled) {
494 do_flush_queued_data(port, vq, vdev);
495 return;
496 }
497 }
498
499 static void handle_input(VirtIODevice *vdev, VirtQueue *vq)
500 {
501 }
502
503 static uint32_t get_features(VirtIODevice *vdev, uint32_t features)
504 {
505 VirtIOSerial *vser;
506
507 vser = DO_UPCAST(VirtIOSerial, vdev, vdev);
508
509 if (vser->bus.max_nr_ports > 1) {
510 features |= (1 << VIRTIO_CONSOLE_F_MULTIPORT);
511 }
512 return features;
513 }
514
515 /* Guest requested config info */
516 static void get_config(VirtIODevice *vdev, uint8_t *config_data)
517 {
518 VirtIOSerial *vser;
519
520 vser = DO_UPCAST(VirtIOSerial, vdev, vdev);
521 memcpy(config_data, &vser->config, sizeof(struct virtio_console_config));
522 }
523
524 static void set_config(VirtIODevice *vdev, const uint8_t *config_data)
525 {
526 struct virtio_console_config config;
527
528 memcpy(&config, config_data, sizeof(config));
529 }
530
531 static void guest_reset(VirtIOSerial *vser)
532 {
533 VirtIOSerialPort *port;
534 VirtIOSerialPortClass *vsc;
535
536 QTAILQ_FOREACH(port, &vser->ports, next) {
537 vsc = VIRTIO_SERIAL_PORT_GET_CLASS(port);
538 if (port->guest_connected) {
539 port->guest_connected = false;
540
541 if (vsc->guest_close)
542 vsc->guest_close(port);
543 }
544 }
545 }
546
547 static void set_status(VirtIODevice *vdev, uint8_t status)
548 {
549 VirtIOSerial *vser;
550 VirtIOSerialPort *port;
551
552 vser = DO_UPCAST(VirtIOSerial, vdev, vdev);
553 port = find_port_by_id(vser, 0);
554
555 if (port && !use_multiport(port->vser)
556 && (status & VIRTIO_CONFIG_S_DRIVER_OK)) {
557 /*
558 * Non-multiport guests won't be able to tell us guest
559 * open/close status. Such guests can only have a port at id
560 * 0, so set guest_connected for such ports as soon as guest
561 * is up.
562 */
563 port->guest_connected = true;
564 }
565 if (!(status & VIRTIO_CONFIG_S_DRIVER_OK)) {
566 guest_reset(vser);
567 }
568 }
569
570 static void vser_reset(VirtIODevice *vdev)
571 {
572 VirtIOSerial *vser;
573
574 vser = DO_UPCAST(VirtIOSerial, vdev, vdev);
575 guest_reset(vser);
576 }
577
578 static void virtio_serial_save(QEMUFile *f, void *opaque)
579 {
580 VirtIOSerial *s = opaque;
581 VirtIOSerialPort *port;
582 uint32_t nr_active_ports;
583 unsigned int i, max_nr_ports;
584
585 /* The virtio device */
586 virtio_save(&s->vdev, f);
587
588 /* The config space */
589 qemu_put_be16s(f, &s->config.cols);
590 qemu_put_be16s(f, &s->config.rows);
591
592 qemu_put_be32s(f, &s->config.max_nr_ports);
593
594 /* The ports map */
595 max_nr_ports = tswap32(s->config.max_nr_ports);
596 for (i = 0; i < (max_nr_ports + 31) / 32; i++) {
597 qemu_put_be32s(f, &s->ports_map[i]);
598 }
599
600 /* Ports */
601
602 nr_active_ports = 0;
603 QTAILQ_FOREACH(port, &s->ports, next) {
604 nr_active_ports++;
605 }
606
607 qemu_put_be32s(f, &nr_active_ports);
608
609 /*
610 * Items in struct VirtIOSerialPort.
611 */
612 QTAILQ_FOREACH(port, &s->ports, next) {
613 uint32_t elem_popped;
614
615 qemu_put_be32s(f, &port->id);
616 qemu_put_byte(f, port->guest_connected);
617 qemu_put_byte(f, port->host_connected);
618
619 elem_popped = 0;
620 if (port->elem.out_num) {
621 elem_popped = 1;
622 }
623 qemu_put_be32s(f, &elem_popped);
624 if (elem_popped) {
625 qemu_put_be32s(f, &port->iov_idx);
626 qemu_put_be64s(f, &port->iov_offset);
627
628 qemu_put_buffer(f, (unsigned char *)&port->elem,
629 sizeof(port->elem));
630 }
631 }
632 }
633
634 static int virtio_serial_load(QEMUFile *f, void *opaque, int version_id)
635 {
636 VirtIOSerial *s = opaque;
637 VirtIOSerialPort *port;
638 uint32_t max_nr_ports, nr_active_ports, ports_map;
639 unsigned int i;
640 int ret;
641
642 if (version_id > 3) {
643 return -EINVAL;
644 }
645
646 /* The virtio device */
647 ret = virtio_load(&s->vdev, f);
648 if (ret) {
649 return ret;
650 }
651
652 if (version_id < 2) {
653 return 0;
654 }
655
656 /* The config space */
657 qemu_get_be16s(f, &s->config.cols);
658 qemu_get_be16s(f, &s->config.rows);
659
660 qemu_get_be32s(f, &max_nr_ports);
661 tswap32s(&max_nr_ports);
662 if (max_nr_ports > tswap32(s->config.max_nr_ports)) {
663 /* Source could have had more ports than us. Fail migration. */
664 return -EINVAL;
665 }
666
667 for (i = 0; i < (max_nr_ports + 31) / 32; i++) {
668 qemu_get_be32s(f, &ports_map);
669
670 if (ports_map != s->ports_map[i]) {
671 /*
672 * Ports active on source and destination don't
673 * match. Fail migration.
674 */
675 return -EINVAL;
676 }
677 }
678
679 qemu_get_be32s(f, &nr_active_ports);
680
681 /* Items in struct VirtIOSerialPort */
682 for (i = 0; i < nr_active_ports; i++) {
683 uint32_t id;
684 bool host_connected;
685
686 id = qemu_get_be32(f);
687 port = find_port_by_id(s, id);
688 if (!port) {
689 return -EINVAL;
690 }
691
692 port->guest_connected = qemu_get_byte(f);
693 host_connected = qemu_get_byte(f);
694 if (host_connected != port->host_connected) {
695 /*
696 * We have to let the guest know of the host connection
697 * status change
698 */
699 send_control_event(port, VIRTIO_CONSOLE_PORT_OPEN,
700 port->host_connected);
701 }
702
703 if (version_id > 2) {
704 uint32_t elem_popped;
705
706 qemu_get_be32s(f, &elem_popped);
707 if (elem_popped) {
708 qemu_get_be32s(f, &port->iov_idx);
709 qemu_get_be64s(f, &port->iov_offset);
710
711 qemu_get_buffer(f, (unsigned char *)&port->elem,
712 sizeof(port->elem));
713 virtqueue_map_sg(port->elem.in_sg, port->elem.in_addr,
714 port->elem.in_num, 1);
715 virtqueue_map_sg(port->elem.out_sg, port->elem.out_addr,
716 port->elem.out_num, 1);
717
718 /*
719 * Port was throttled on source machine. Let's
720 * unthrottle it here so data starts flowing again.
721 */
722 virtio_serial_throttle_port(port, false);
723 }
724 }
725 }
726 return 0;
727 }
728
729 static void virtser_bus_dev_print(Monitor *mon, DeviceState *qdev, int indent);
730
731 static Property virtser_props[] = {
732 DEFINE_PROP_UINT32("nr", VirtIOSerialPort, id, VIRTIO_CONSOLE_BAD_ID),
733 DEFINE_PROP_STRING("name", VirtIOSerialPort, name),
734 DEFINE_PROP_END_OF_LIST()
735 };
736
737 #define TYPE_VIRTIO_SERIAL_BUS "virtio-serial-bus"
738 #define VIRTIO_SERIAL_BUS(obj) \
739 OBJECT_CHECK(VirtIOSerialBus, (obj), TYPE_VIRTIO_SERIAL_BUS)
740
741 static void virtser_bus_class_init(ObjectClass *klass, void *data)
742 {
743 BusClass *k = BUS_CLASS(klass);
744 k->print_dev = virtser_bus_dev_print;
745 }
746
747 static const TypeInfo virtser_bus_info = {
748 .name = TYPE_VIRTIO_SERIAL_BUS,
749 .parent = TYPE_BUS,
750 .instance_size = sizeof(VirtIOSerialBus),
751 .class_init = virtser_bus_class_init,
752 };
753
754 static void virtser_bus_dev_print(Monitor *mon, DeviceState *qdev, int indent)
755 {
756 VirtIOSerialPort *port = DO_UPCAST(VirtIOSerialPort, dev, qdev);
757
758 monitor_printf(mon, "%*sport %d, guest %s, host %s, throttle %s\n",
759 indent, "", port->id,
760 port->guest_connected ? "on" : "off",
761 port->host_connected ? "on" : "off",
762 port->throttled ? "on" : "off");
763 }
764
765 /* This function is only used if a port id is not provided by the user */
766 static uint32_t find_free_port_id(VirtIOSerial *vser)
767 {
768 unsigned int i, max_nr_ports;
769
770 max_nr_ports = tswap32(vser->config.max_nr_ports);
771 for (i = 0; i < (max_nr_ports + 31) / 32; i++) {
772 uint32_t map, bit;
773
774 map = vser->ports_map[i];
775 bit = ffs(~map);
776 if (bit) {
777 return (bit - 1) + i * 32;
778 }
779 }
780 return VIRTIO_CONSOLE_BAD_ID;
781 }
782
783 static void mark_port_added(VirtIOSerial *vser, uint32_t port_id)
784 {
785 unsigned int i;
786
787 i = port_id / 32;
788 vser->ports_map[i] |= 1U << (port_id % 32);
789 }
790
791 static void add_port(VirtIOSerial *vser, uint32_t port_id)
792 {
793 mark_port_added(vser, port_id);
794
795 send_control_event(find_port_by_id(vser, port_id),
796 VIRTIO_CONSOLE_PORT_ADD, 1);
797 }
798
799 static void remove_port(VirtIOSerial *vser, uint32_t port_id)
800 {
801 VirtIOSerialPort *port;
802 unsigned int i;
803
804 i = port_id / 32;
805 vser->ports_map[i] &= ~(1U << (port_id % 32));
806
807 port = find_port_by_id(vser, port_id);
808 /* Flush out any unconsumed buffers first */
809 discard_vq_data(port->ovq, &port->vser->vdev);
810
811 send_control_event(port, VIRTIO_CONSOLE_PORT_REMOVE, 1);
812 }
813
814 static int virtser_port_qdev_init(DeviceState *qdev)
815 {
816 VirtIOSerialPort *port = DO_UPCAST(VirtIOSerialPort, dev, qdev);
817 VirtIOSerialPortClass *vsc = VIRTIO_SERIAL_PORT_GET_CLASS(port);
818 VirtIOSerialBus *bus = DO_UPCAST(VirtIOSerialBus, qbus, qdev->parent_bus);
819 int ret, max_nr_ports;
820 bool plugging_port0;
821
822 port->vser = bus->vser;
823 port->bh = qemu_bh_new(flush_queued_data_bh, port);
824
825 assert(vsc->have_data);
826
827 /*
828 * Is the first console port we're seeing? If so, put it up at
829 * location 0. This is done for backward compatibility (old
830 * kernel, new qemu).
831 */
832 plugging_port0 = vsc->is_console && !find_port_by_id(port->vser, 0);
833
834 if (find_port_by_id(port->vser, port->id)) {
835 error_report("virtio-serial-bus: A port already exists at id %u",
836 port->id);
837 return -1;
838 }
839
840 if (port->id == VIRTIO_CONSOLE_BAD_ID) {
841 if (plugging_port0) {
842 port->id = 0;
843 } else {
844 port->id = find_free_port_id(port->vser);
845 if (port->id == VIRTIO_CONSOLE_BAD_ID) {
846 error_report("virtio-serial-bus: Maximum port limit for this device reached");
847 return -1;
848 }
849 }
850 }
851
852 max_nr_ports = tswap32(port->vser->config.max_nr_ports);
853 if (port->id >= max_nr_ports) {
854 error_report("virtio-serial-bus: Out-of-range port id specified, max. allowed: %u",
855 max_nr_ports - 1);
856 return -1;
857 }
858
859 ret = vsc->init(port);
860 if (ret) {
861 return ret;
862 }
863
864 port->elem.out_num = 0;
865
866 QTAILQ_INSERT_TAIL(&port->vser->ports, port, next);
867 port->ivq = port->vser->ivqs[port->id];
868 port->ovq = port->vser->ovqs[port->id];
869
870 add_port(port->vser, port->id);
871
872 /* Send an update to the guest about this new port added */
873 virtio_notify_config(&port->vser->vdev);
874
875 return ret;
876 }
877
878 static int virtser_port_qdev_exit(DeviceState *qdev)
879 {
880 VirtIOSerialPort *port = DO_UPCAST(VirtIOSerialPort, dev, qdev);
881 VirtIOSerialPortClass *vsc = VIRTIO_SERIAL_PORT_GET_CLASS(port);
882 VirtIOSerial *vser = port->vser;
883
884 qemu_bh_delete(port->bh);
885 remove_port(port->vser, port->id);
886
887 QTAILQ_REMOVE(&vser->ports, port, next);
888
889 if (vsc->exit) {
890 vsc->exit(port);
891 }
892 return 0;
893 }
894
895 VirtIODevice *virtio_serial_init(DeviceState *dev, virtio_serial_conf *conf)
896 {
897 VirtIOSerial *vser;
898 VirtIODevice *vdev;
899 uint32_t i, max_supported_ports;
900
901 if (!conf->max_virtserial_ports)
902 return NULL;
903
904 /* Each port takes 2 queues, and one pair is for the control queue */
905 max_supported_ports = VIRTIO_PCI_QUEUE_MAX / 2 - 1;
906
907 if (conf->max_virtserial_ports > max_supported_ports) {
908 error_report("maximum ports supported: %u", max_supported_ports);
909 return NULL;
910 }
911
912 vdev = virtio_common_init("virtio-serial", VIRTIO_ID_CONSOLE,
913 sizeof(struct virtio_console_config),
914 sizeof(VirtIOSerial));
915
916 vser = DO_UPCAST(VirtIOSerial, vdev, vdev);
917
918 /* Spawn a new virtio-serial bus on which the ports will ride as devices */
919 qbus_create_inplace(&vser->bus.qbus, TYPE_VIRTIO_SERIAL_BUS, dev, NULL);
920 vser->bus.qbus.allow_hotplug = 1;
921 vser->bus.vser = vser;
922 QTAILQ_INIT(&vser->ports);
923
924 vser->bus.max_nr_ports = conf->max_virtserial_ports;
925 vser->ivqs = g_malloc(conf->max_virtserial_ports * sizeof(VirtQueue *));
926 vser->ovqs = g_malloc(conf->max_virtserial_ports * sizeof(VirtQueue *));
927
928 /* Add a queue for host to guest transfers for port 0 (backward compat) */
929 vser->ivqs[0] = virtio_add_queue(vdev, 128, handle_input);
930 /* Add a queue for guest to host transfers for port 0 (backward compat) */
931 vser->ovqs[0] = virtio_add_queue(vdev, 128, handle_output);
932
933 /* TODO: host to guest notifications can get dropped
934 * if the queue fills up. Implement queueing in host,
935 * this might also make it possible to reduce the control
936 * queue size: as guest preposts buffers there,
937 * this will save 4Kbyte of guest memory per entry. */
938
939 /* control queue: host to guest */
940 vser->c_ivq = virtio_add_queue(vdev, 32, control_in);
941 /* control queue: guest to host */
942 vser->c_ovq = virtio_add_queue(vdev, 32, control_out);
943
944 for (i = 1; i < vser->bus.max_nr_ports; i++) {
945 /* Add a per-port queue for host to guest transfers */
946 vser->ivqs[i] = virtio_add_queue(vdev, 128, handle_input);
947 /* Add a per-per queue for guest to host transfers */
948 vser->ovqs[i] = virtio_add_queue(vdev, 128, handle_output);
949 }
950
951 vser->config.max_nr_ports = tswap32(conf->max_virtserial_ports);
952 vser->ports_map = g_malloc0(((conf->max_virtserial_ports + 31) / 32)
953 * sizeof(vser->ports_map[0]));
954 /*
955 * Reserve location 0 for a console port for backward compat
956 * (old kernel, new qemu)
957 */
958 mark_port_added(vser, 0);
959
960 vser->vdev.get_features = get_features;
961 vser->vdev.get_config = get_config;
962 vser->vdev.set_config = set_config;
963 vser->vdev.set_status = set_status;
964 vser->vdev.reset = vser_reset;
965
966 vser->qdev = dev;
967
968 /*
969 * Register for the savevm section with the virtio-console name
970 * to preserve backward compat
971 */
972 register_savevm(dev, "virtio-console", -1, 3, virtio_serial_save,
973 virtio_serial_load, vser);
974
975 return vdev;
976 }
977
978 void virtio_serial_exit(VirtIODevice *vdev)
979 {
980 VirtIOSerial *vser = DO_UPCAST(VirtIOSerial, vdev, vdev);
981
982 unregister_savevm(vser->qdev, "virtio-console", vser);
983
984 g_free(vser->ivqs);
985 g_free(vser->ovqs);
986 g_free(vser->ports_map);
987
988 virtio_cleanup(vdev);
989 }
990
991 static void virtio_serial_port_class_init(ObjectClass *klass, void *data)
992 {
993 DeviceClass *k = DEVICE_CLASS(klass);
994 k->init = virtser_port_qdev_init;
995 k->bus_type = TYPE_VIRTIO_SERIAL_BUS;
996 k->exit = virtser_port_qdev_exit;
997 k->unplug = qdev_simple_unplug_cb;
998 k->props = virtser_props;
999 }
1000
1001 static TypeInfo virtio_serial_port_type_info = {
1002 .name = TYPE_VIRTIO_SERIAL_PORT,
1003 .parent = TYPE_DEVICE,
1004 .instance_size = sizeof(VirtIOSerialPort),
1005 .abstract = true,
1006 .class_size = sizeof(VirtIOSerialPortClass),
1007 .class_init = virtio_serial_port_class_init,
1008 };
1009
1010 static void virtio_serial_register_types(void)
1011 {
1012 type_register_static(&virtser_bus_info);
1013 type_register_static(&virtio_serial_port_type_info);
1014 }
1015
1016 type_init(virtio_serial_register_types)