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