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