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