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