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