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