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