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