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