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