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