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