Statistics
| Branch: | Revision:

root / hw / virtio-serial-bus.c @ 6bff8656

History | View | Annotate | Download (21.8 kB)

1
/*
2
 * A bus for connecting virtio serial and console ports
3
 *
4
 * Copyright (C) 2009, 2010 Red Hat, Inc.
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

    
18
#include "iov.h"
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 */
25
struct 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

    
35
struct 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

    
44
    DeviceState *qdev;
45

    
46
    QTAILQ_HEAD(, VirtIOSerialPort) ports;
47

    
48
    /* bitmap for identifying active ports */
49
    uint32_t *ports_map;
50

    
51
    struct virtio_console_config config;
52
};
53

    
54
static VirtIOSerialPort *find_port_by_id(VirtIOSerial *vser, uint32_t id)
55
{
56
    VirtIOSerialPort *port;
57

    
58
    if (id == VIRTIO_CONSOLE_BAD_ID) {
59
        return NULL;
60
    }
61

    
62
    QTAILQ_FOREACH(port, &vser->ports, next) {
63
        if (port->id == id)
64
            return port;
65
    }
66
    return NULL;
67
}
68

    
69
static 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

    
80
static bool use_multiport(VirtIOSerial *vser)
81
{
82
    return vser->vdev.guest_features & (1 << VIRTIO_CONSOLE_F_MULTIPORT);
83
}
84

    
85
static size_t write_to_port(VirtIOSerialPort *port,
86
                            const uint8_t *buf, size_t size)
87
{
88
    VirtQueueElement elem;
89
    VirtQueue *vq;
90
    size_t offset;
91

    
92
    vq = port->ivq;
93
    if (!virtio_queue_ready(vq)) {
94
        return 0;
95
    }
96

    
97
    offset = 0;
98
    while (offset < size) {
99
        size_t len;
100

    
101
        if (!virtqueue_pop(vq, &elem)) {
102
            break;
103
        }
104

    
105
        len = iov_from_buf(elem.in_sg, elem.in_num,
106
                           buf + offset, size - offset);
107
        offset += len;
108

    
109
        virtqueue_push(vq, &elem, len);
110
    }
111

    
112
    virtio_notify(&port->vser->vdev, vq);
113
    return offset;
114
}
115

    
116
static void discard_vq_data(VirtQueue *vq, VirtIODevice *vdev)
117
{
118
    VirtQueueElement elem;
119

    
120
    while (virtqueue_pop(vq, &elem)) {
121
        virtqueue_push(vq, &elem, 0);
122
    }
123
    virtio_notify(vdev, vq);
124
}
125

    
126
static void do_flush_queued_data(VirtIOSerialPort *port, VirtQueue *vq,
127
                                 VirtIODevice *vdev)
128
{
129
    VirtQueueElement elem;
130

    
131
    assert(port);
132
    assert(virtio_queue_ready(vq));
133

    
134
    while (!port->throttled && virtqueue_pop(vq, &elem)) {
135
        uint8_t *buf;
136
        size_t ret, buf_size;
137

    
138
        buf_size = iov_size(elem.out_sg, elem.out_num);
139
        buf = qemu_malloc(buf_size);
140
        ret = iov_to_buf(elem.out_sg, elem.out_num, buf, 0, buf_size);
141

    
142
        port->info->have_data(port, buf, ret);
143
        qemu_free(buf);
144

    
145
        virtqueue_push(vq, &elem, 0);
146
    }
147
    virtio_notify(vdev, vq);
148
}
149

    
150
static void flush_queued_data(VirtIOSerialPort *port)
151
{
152
    assert(port);
153

    
154
    if (!virtio_queue_ready(port->ovq)) {
155
        return;
156
    }
157
    do_flush_queued_data(port, port->ovq, &port->vser->vdev);
158
}
159

    
160
static size_t send_control_msg(VirtIOSerialPort *port, void *buf, size_t len)
161
{
162
    VirtQueueElement elem;
163
    VirtQueue *vq;
164
    struct virtio_console_control *cpkt;
165

    
166
    vq = port->vser->c_ivq;
167
    if (!virtio_queue_ready(vq)) {
168
        return 0;
169
    }
170
    if (!virtqueue_pop(vq, &elem)) {
171
        return 0;
172
    }
173

    
174
    cpkt = (struct virtio_console_control *)buf;
175
    stl_p(&cpkt->id, port->id);
176
    memcpy(elem.in_sg[0].iov_base, buf, len);
177

    
178
    virtqueue_push(vq, &elem, len);
179
    virtio_notify(&port->vser->vdev, vq);
180
    return len;
181
}
182

    
183
static size_t send_control_event(VirtIOSerialPort *port, uint16_t event,
184
                                 uint16_t value)
185
{
186
    struct virtio_console_control cpkt;
187

    
188
    stw_p(&cpkt.event, event);
189
    stw_p(&cpkt.value, value);
190

    
191
    return send_control_msg(port, &cpkt, sizeof(cpkt));
192
}
193

    
194
/* Functions for use inside qemu to open and read from/write to ports */
195
int virtio_serial_open(VirtIOSerialPort *port)
196
{
197
    /* Don't allow opening an already-open port */
198
    if (port->host_connected) {
199
        return 0;
200
    }
201
    /* Send port open notification to the guest */
202
    port->host_connected = true;
203
    send_control_event(port, VIRTIO_CONSOLE_PORT_OPEN, 1);
204

    
205
    return 0;
206
}
207

    
208
int virtio_serial_close(VirtIOSerialPort *port)
209
{
210
    port->host_connected = false;
211
    /*
212
     * If there's any data the guest sent which the app didn't
213
     * consume, reset the throttling flag and discard the data.
214
     */
215
    port->throttled = false;
216
    discard_vq_data(port->ovq, &port->vser->vdev);
217

    
218
    send_control_event(port, VIRTIO_CONSOLE_PORT_OPEN, 0);
219

    
220
    return 0;
221
}
222

    
223
/* Individual ports/apps call this function to write to the guest. */
224
ssize_t virtio_serial_write(VirtIOSerialPort *port, const uint8_t *buf,
225
                            size_t size)
226
{
227
    if (!port || !port->host_connected || !port->guest_connected) {
228
        return 0;
229
    }
230
    return write_to_port(port, buf, size);
231
}
232

    
233
/*
234
 * Readiness of the guest to accept data on a port.
235
 * Returns max. data the guest can receive
236
 */
237
size_t virtio_serial_guest_ready(VirtIOSerialPort *port)
238
{
239
    VirtQueue *vq = port->ivq;
240

    
241
    if (!virtio_queue_ready(vq) ||
242
        !(port->vser->vdev.status & VIRTIO_CONFIG_S_DRIVER_OK) ||
243
        virtio_queue_empty(vq)) {
244
        return 0;
245
    }
246
    if (use_multiport(port->vser) && !port->guest_connected) {
247
        return 0;
248
    }
249

    
250
    if (virtqueue_avail_bytes(vq, 4096, 0)) {
251
        return 4096;
252
    }
253
    if (virtqueue_avail_bytes(vq, 1, 0)) {
254
        return 1;
255
    }
256
    return 0;
257
}
258

    
259
void virtio_serial_throttle_port(VirtIOSerialPort *port, bool throttle)
260
{
261
    if (!port) {
262
        return;
263
    }
264

    
265
    port->throttled = throttle;
266
    if (throttle) {
267
        return;
268
    }
269

    
270
    flush_queued_data(port);
271
}
272

    
273
/* Guest wants to notify us of some event */
274
static void handle_control_message(VirtIOSerial *vser, void *buf, size_t len)
275
{
276
    struct VirtIOSerialPort *port;
277
    struct virtio_console_control cpkt, *gcpkt;
278
    uint8_t *buffer;
279
    size_t buffer_len;
280

    
281
    gcpkt = buf;
282

    
283
    if (len < sizeof(cpkt)) {
284
        /* The guest sent an invalid control packet */
285
        return;
286
    }
287

    
288
    cpkt.event = lduw_p(&gcpkt->event);
289
    cpkt.value = lduw_p(&gcpkt->value);
290

    
291
    port = find_port_by_id(vser, ldl_p(&gcpkt->id));
292
    if (!port && cpkt.event != VIRTIO_CONSOLE_DEVICE_READY)
293
        return;
294

    
295
    switch(cpkt.event) {
296
    case VIRTIO_CONSOLE_DEVICE_READY:
297
        if (!cpkt.value) {
298
            error_report("virtio-serial-bus: Guest failure in adding device %s\n",
299
                         vser->bus->qbus.name);
300
            break;
301
        }
302
        /*
303
         * The device is up, we can now tell the device about all the
304
         * ports we have here.
305
         */
306
        QTAILQ_FOREACH(port, &vser->ports, next) {
307
            send_control_event(port, VIRTIO_CONSOLE_PORT_ADD, 1);
308
        }
309
        break;
310

    
311
    case VIRTIO_CONSOLE_PORT_READY:
312
        if (!cpkt.value) {
313
            error_report("virtio-serial-bus: Guest failure in adding port %u for device %s\n",
314
                         port->id, vser->bus->qbus.name);
315
            break;
316
        }
317
        /*
318
         * Now that we know the guest asked for the port name, we're
319
         * sure the guest has initialised whatever state is necessary
320
         * for this port. Now's a good time to let the guest know if
321
         * this port is a console port so that the guest can hook it
322
         * up to hvc.
323
         */
324
        if (port->is_console) {
325
            send_control_event(port, VIRTIO_CONSOLE_CONSOLE_PORT, 1);
326
        }
327

    
328
        if (port->name) {
329
            stw_p(&cpkt.event, VIRTIO_CONSOLE_PORT_NAME);
330
            stw_p(&cpkt.value, 1);
331

    
332
            buffer_len = sizeof(cpkt) + strlen(port->name) + 1;
333
            buffer = qemu_malloc(buffer_len);
334

    
335
            memcpy(buffer, &cpkt, sizeof(cpkt));
336
            memcpy(buffer + sizeof(cpkt), port->name, strlen(port->name));
337
            buffer[buffer_len - 1] = 0;
338

    
339
            send_control_msg(port, buffer, buffer_len);
340
            qemu_free(buffer);
341
        }
342

    
343
        if (port->host_connected) {
344
            send_control_event(port, VIRTIO_CONSOLE_PORT_OPEN, 1);
345
        }
346

    
347
        /*
348
         * When the guest has asked us for this information it means
349
         * the guest is all setup and has its virtqueues
350
         * initialised. If some app is interested in knowing about
351
         * this event, let it know.
352
         */
353
        if (port->info->guest_ready) {
354
            port->info->guest_ready(port);
355
        }
356
        break;
357

    
358
    case VIRTIO_CONSOLE_PORT_OPEN:
359
        port->guest_connected = cpkt.value;
360
        if (cpkt.value && port->info->guest_open) {
361
            /* Send the guest opened notification if an app is interested */
362
            port->info->guest_open(port);
363
        }
364

    
365
        if (!cpkt.value && port->info->guest_close) {
366
            /* Send the guest closed notification if an app is interested */
367
            port->info->guest_close(port);
368
        }
369
        break;
370
    }
371
}
372

    
373
static void control_in(VirtIODevice *vdev, VirtQueue *vq)
374
{
375
}
376

    
377
static void control_out(VirtIODevice *vdev, VirtQueue *vq)
378
{
379
    VirtQueueElement elem;
380
    VirtIOSerial *vser;
381
    uint8_t *buf;
382
    size_t len;
383

    
384
    vser = DO_UPCAST(VirtIOSerial, vdev, vdev);
385

    
386
    len = 0;
387
    buf = NULL;
388
    while (virtqueue_pop(vq, &elem)) {
389
        size_t cur_len, copied;
390

    
391
        cur_len = iov_size(elem.out_sg, elem.out_num);
392
        /*
393
         * Allocate a new buf only if we didn't have one previously or
394
         * if the size of the buf differs
395
         */
396
        if (cur_len > len) {
397
            qemu_free(buf);
398

    
399
            buf = qemu_malloc(cur_len);
400
            len = cur_len;
401
        }
402
        copied = iov_to_buf(elem.out_sg, elem.out_num, buf, 0, len);
403

    
404
        handle_control_message(vser, buf, copied);
405
        virtqueue_push(vq, &elem, 0);
406
    }
407
    qemu_free(buf);
408
    virtio_notify(vdev, vq);
409
}
410

    
411
/* Guest wrote something to some port. */
412
static void handle_output(VirtIODevice *vdev, VirtQueue *vq)
413
{
414
    VirtIOSerial *vser;
415
    VirtIOSerialPort *port;
416
    bool discard;
417

    
418
    vser = DO_UPCAST(VirtIOSerial, vdev, vdev);
419
    port = find_port_by_vq(vser, vq);
420

    
421
    discard = false;
422
    if (!port || !port->host_connected || !port->info->have_data) {
423
        discard = true;
424
    }
425

    
426
    if (discard) {
427
        discard_vq_data(vq, vdev);
428
        return;
429
    }
430
    if (port->throttled) {
431
        return;
432
    }
433

    
434
    do_flush_queued_data(port, vq, vdev);
435
}
436

    
437
static void handle_input(VirtIODevice *vdev, VirtQueue *vq)
438
{
439
}
440

    
441
static uint32_t get_features(VirtIODevice *vdev, uint32_t features)
442
{
443
    VirtIOSerial *vser;
444

    
445
    vser = DO_UPCAST(VirtIOSerial, vdev, vdev);
446

    
447
    if (vser->bus->max_nr_ports > 1) {
448
        features |= (1 << VIRTIO_CONSOLE_F_MULTIPORT);
449
    }
450
    return features;
451
}
452

    
453
/* Guest requested config info */
454
static void get_config(VirtIODevice *vdev, uint8_t *config_data)
455
{
456
    VirtIOSerial *vser;
457

    
458
    vser = DO_UPCAST(VirtIOSerial, vdev, vdev);
459
    memcpy(config_data, &vser->config, sizeof(struct virtio_console_config));
460
}
461

    
462
static void set_config(VirtIODevice *vdev, const uint8_t *config_data)
463
{
464
    struct virtio_console_config config;
465

    
466
    memcpy(&config, config_data, sizeof(config));
467
}
468

    
469
static void virtio_serial_save(QEMUFile *f, void *opaque)
470
{
471
    VirtIOSerial *s = opaque;
472
    VirtIOSerialPort *port;
473
    uint32_t nr_active_ports;
474
    unsigned int i;
475

    
476
    /* The virtio device */
477
    virtio_save(&s->vdev, f);
478

    
479
    /* The config space */
480
    qemu_put_be16s(f, &s->config.cols);
481
    qemu_put_be16s(f, &s->config.rows);
482

    
483
    qemu_put_be32s(f, &s->config.max_nr_ports);
484

    
485
    /* The ports map */
486

    
487
    for (i = 0; i < (s->config.max_nr_ports + 31) / 32; i++) {
488
        qemu_put_be32s(f, &s->ports_map[i]);
489
    }
490

    
491
    /* Ports */
492

    
493
    nr_active_ports = 0;
494
    QTAILQ_FOREACH(port, &s->ports, next) {
495
        nr_active_ports++;
496
    }
497

    
498
    qemu_put_be32s(f, &nr_active_ports);
499

    
500
    /*
501
     * Items in struct VirtIOSerialPort.
502
     */
503
    QTAILQ_FOREACH(port, &s->ports, next) {
504
        qemu_put_be32s(f, &port->id);
505
        qemu_put_byte(f, port->guest_connected);
506
        qemu_put_byte(f, port->host_connected);
507
    }
508
}
509

    
510
static int virtio_serial_load(QEMUFile *f, void *opaque, int version_id)
511
{
512
    VirtIOSerial *s = opaque;
513
    VirtIOSerialPort *port;
514
    uint32_t max_nr_ports, nr_active_ports, ports_map;
515
    unsigned int i;
516

    
517
    if (version_id > 2) {
518
        return -EINVAL;
519
    }
520

    
521
    /* The virtio device */
522
    virtio_load(&s->vdev, f);
523

    
524
    if (version_id < 2) {
525
        return 0;
526
    }
527

    
528
    /* The config space */
529
    qemu_get_be16s(f, &s->config.cols);
530
    qemu_get_be16s(f, &s->config.rows);
531

    
532
    qemu_get_be32s(f, &max_nr_ports);
533
    if (max_nr_ports > s->config.max_nr_ports) {
534
        /* Source could have had more ports than us. Fail migration. */
535
        return -EINVAL;
536
    }
537

    
538
    for (i = 0; i < (max_nr_ports + 31) / 32; i++) {
539
        qemu_get_be32s(f, &ports_map);
540

    
541
        if (ports_map != s->ports_map[i]) {
542
            /*
543
             * Ports active on source and destination don't
544
             * match. Fail migration.
545
             */
546
            return -EINVAL;
547
        }
548
    }
549

    
550
    qemu_get_be32s(f, &nr_active_ports);
551

    
552
    /* Items in struct VirtIOSerialPort */
553
    for (i = 0; i < nr_active_ports; i++) {
554
        uint32_t id;
555
        bool host_connected;
556

    
557
        id = qemu_get_be32(f);
558
        port = find_port_by_id(s, id);
559

    
560
        port->guest_connected = qemu_get_byte(f);
561
        host_connected = qemu_get_byte(f);
562
        if (host_connected != port->host_connected) {
563
            /*
564
             * We have to let the guest know of the host connection
565
             * status change
566
             */
567
            send_control_event(port, VIRTIO_CONSOLE_PORT_OPEN,
568
                               port->host_connected);
569
        }
570
    }
571
    return 0;
572
}
573

    
574
static void virtser_bus_dev_print(Monitor *mon, DeviceState *qdev, int indent);
575

    
576
static struct BusInfo virtser_bus_info = {
577
    .name      = "virtio-serial-bus",
578
    .size      = sizeof(VirtIOSerialBus),
579
    .print_dev = virtser_bus_dev_print,
580
};
581

    
582
static VirtIOSerialBus *virtser_bus_new(DeviceState *dev)
583
{
584
    VirtIOSerialBus *bus;
585

    
586
    bus = FROM_QBUS(VirtIOSerialBus, qbus_create(&virtser_bus_info, dev, NULL));
587
    bus->qbus.allow_hotplug = 1;
588

    
589
    return bus;
590
}
591

    
592
static void virtser_bus_dev_print(Monitor *mon, DeviceState *qdev, int indent)
593
{
594
    VirtIOSerialDevice *dev = DO_UPCAST(VirtIOSerialDevice, qdev, qdev);
595
    VirtIOSerialPort *port = DO_UPCAST(VirtIOSerialPort, dev, &dev->qdev);
596

    
597
    monitor_printf(mon, "%*s dev-prop-int: id: %u\n",
598
                   indent, "", port->id);
599
    monitor_printf(mon, "%*s dev-prop-int: guest_connected: %d\n",
600
                   indent, "", port->guest_connected);
601
    monitor_printf(mon, "%*s dev-prop-int: host_connected: %d\n",
602
                   indent, "", port->host_connected);
603
    monitor_printf(mon, "%*s dev-prop-int: throttled: %d\n",
604
                   indent, "", port->throttled);
605
}
606

    
607
/* This function is only used if a port id is not provided by the user */
608
static uint32_t find_free_port_id(VirtIOSerial *vser)
609
{
610
    unsigned int i;
611

    
612
    for (i = 0; i < (vser->config.max_nr_ports + 31) / 32; i++) {
613
        uint32_t map, bit;
614

    
615
        map = vser->ports_map[i];
616
        bit = ffs(~map);
617
        if (bit) {
618
            return (bit - 1) + i * 32;
619
        }
620
    }
621
    return VIRTIO_CONSOLE_BAD_ID;
622
}
623

    
624
static void mark_port_added(VirtIOSerial *vser, uint32_t port_id)
625
{
626
    unsigned int i;
627

    
628
    i = port_id / 32;
629
    vser->ports_map[i] |= 1U << (port_id % 32);
630
}
631

    
632
static void add_port(VirtIOSerial *vser, uint32_t port_id)
633
{
634
    mark_port_added(vser, port_id);
635

    
636
    send_control_event(find_port_by_id(vser, port_id),
637
                       VIRTIO_CONSOLE_PORT_ADD, 1);
638
}
639

    
640
static void remove_port(VirtIOSerial *vser, uint32_t port_id)
641
{
642
    VirtIOSerialPort *port;
643
    unsigned int i;
644

    
645
    i = port_id / 32;
646
    vser->ports_map[i] &= ~(1U << (port_id % 32));
647

    
648
    port = find_port_by_id(vser, port_id);
649
    /* Flush out any unconsumed buffers first */
650
    discard_vq_data(port->ovq, &port->vser->vdev);
651

    
652
    send_control_event(port, VIRTIO_CONSOLE_PORT_REMOVE, 1);
653
}
654

    
655
static int virtser_port_qdev_init(DeviceState *qdev, DeviceInfo *base)
656
{
657
    VirtIOSerialDevice *dev = DO_UPCAST(VirtIOSerialDevice, qdev, qdev);
658
    VirtIOSerialPortInfo *info = DO_UPCAST(VirtIOSerialPortInfo, qdev, base);
659
    VirtIOSerialPort *port = DO_UPCAST(VirtIOSerialPort, dev, &dev->qdev);
660
    VirtIOSerialBus *bus = DO_UPCAST(VirtIOSerialBus, qbus, qdev->parent_bus);
661
    int ret;
662
    bool plugging_port0;
663

    
664
    port->vser = bus->vser;
665

    
666
    /*
667
     * Is the first console port we're seeing? If so, put it up at
668
     * location 0. This is done for backward compatibility (old
669
     * kernel, new qemu).
670
     */
671
    plugging_port0 = port->is_console && !find_port_by_id(port->vser, 0);
672

    
673
    if (find_port_by_id(port->vser, port->id)) {
674
        error_report("virtio-serial-bus: A port already exists at id %u\n",
675
                     port->id);
676
        return -1;
677
    }
678

    
679
    if (port->id == VIRTIO_CONSOLE_BAD_ID) {
680
        if (plugging_port0) {
681
            port->id = 0;
682
        } else {
683
            port->id = find_free_port_id(port->vser);
684
            if (port->id == VIRTIO_CONSOLE_BAD_ID) {
685
                error_report("virtio-serial-bus: Maximum port limit for this device reached\n");
686
                return -1;
687
            }
688
        }
689
    }
690

    
691
    if (port->id >= port->vser->config.max_nr_ports) {
692
        error_report("virtio-serial-bus: Out-of-range port id specified, max. allowed: %u\n",
693
                     port->vser->config.max_nr_ports - 1);
694
        return -1;
695
    }
696

    
697
    dev->info = info;
698
    ret = info->init(dev);
699
    if (ret) {
700
        return ret;
701
    }
702

    
703
    if (!use_multiport(port->vser)) {
704
        /*
705
         * Allow writes to guest in this case; we have no way of
706
         * knowing if a guest port is connected.
707
         */
708
        port->guest_connected = true;
709
    }
710

    
711
    QTAILQ_INSERT_TAIL(&port->vser->ports, port, next);
712
    port->ivq = port->vser->ivqs[port->id];
713
    port->ovq = port->vser->ovqs[port->id];
714

    
715
    add_port(port->vser, port->id);
716

    
717
    /* Send an update to the guest about this new port added */
718
    virtio_notify_config(&port->vser->vdev);
719

    
720
    return ret;
721
}
722

    
723
static int virtser_port_qdev_exit(DeviceState *qdev)
724
{
725
    VirtIOSerialDevice *dev = DO_UPCAST(VirtIOSerialDevice, qdev, qdev);
726
    VirtIOSerialPort *port = DO_UPCAST(VirtIOSerialPort, dev, &dev->qdev);
727
    VirtIOSerial *vser = port->vser;
728

    
729
    remove_port(port->vser, port->id);
730

    
731
    QTAILQ_REMOVE(&vser->ports, port, next);
732

    
733
    if (port->info->exit)
734
        port->info->exit(dev);
735

    
736
    return 0;
737
}
738

    
739
void virtio_serial_port_qdev_register(VirtIOSerialPortInfo *info)
740
{
741
    info->qdev.init = virtser_port_qdev_init;
742
    info->qdev.bus_info = &virtser_bus_info;
743
    info->qdev.exit = virtser_port_qdev_exit;
744
    info->qdev.unplug = qdev_simple_unplug_cb;
745
    qdev_register(&info->qdev);
746
}
747

    
748
VirtIODevice *virtio_serial_init(DeviceState *dev, uint32_t max_nr_ports)
749
{
750
    VirtIOSerial *vser;
751
    VirtIODevice *vdev;
752
    uint32_t i, max_supported_ports;
753

    
754
    if (!max_nr_ports)
755
        return NULL;
756

    
757
    /* Each port takes 2 queues, and one pair is for the control queue */
758
    max_supported_ports = VIRTIO_PCI_QUEUE_MAX / 2 - 1;
759

    
760
    if (max_nr_ports > max_supported_ports) {
761
        error_report("maximum ports supported: %u", max_supported_ports);
762
        return NULL;
763
    }
764

    
765
    vdev = virtio_common_init("virtio-serial", VIRTIO_ID_CONSOLE,
766
                              sizeof(struct virtio_console_config),
767
                              sizeof(VirtIOSerial));
768

    
769
    vser = DO_UPCAST(VirtIOSerial, vdev, vdev);
770

    
771
    /* Spawn a new virtio-serial bus on which the ports will ride as devices */
772
    vser->bus = virtser_bus_new(dev);
773
    vser->bus->vser = vser;
774
    QTAILQ_INIT(&vser->ports);
775

    
776
    vser->bus->max_nr_ports = max_nr_ports;
777
    vser->ivqs = qemu_malloc(max_nr_ports * sizeof(VirtQueue *));
778
    vser->ovqs = qemu_malloc(max_nr_ports * sizeof(VirtQueue *));
779

    
780
    /* Add a queue for host to guest transfers for port 0 (backward compat) */
781
    vser->ivqs[0] = virtio_add_queue(vdev, 128, handle_input);
782
    /* Add a queue for guest to host transfers for port 0 (backward compat) */
783
    vser->ovqs[0] = virtio_add_queue(vdev, 128, handle_output);
784

    
785
    /* control queue: host to guest */
786
    vser->c_ivq = virtio_add_queue(vdev, 16, control_in);
787
    /* control queue: guest to host */
788
    vser->c_ovq = virtio_add_queue(vdev, 16, control_out);
789

    
790
    for (i = 1; i < vser->bus->max_nr_ports; i++) {
791
        /* Add a per-port queue for host to guest transfers */
792
        vser->ivqs[i] = virtio_add_queue(vdev, 128, handle_input);
793
        /* Add a per-per queue for guest to host transfers */
794
        vser->ovqs[i] = virtio_add_queue(vdev, 128, handle_output);
795
    }
796

    
797
    vser->config.max_nr_ports = max_nr_ports;
798
    vser->ports_map = qemu_mallocz(((max_nr_ports + 31) / 32)
799
        * sizeof(vser->ports_map[0]));
800
    /*
801
     * Reserve location 0 for a console port for backward compat
802
     * (old kernel, new qemu)
803
     */
804
    mark_port_added(vser, 0);
805

    
806
    vser->vdev.get_features = get_features;
807
    vser->vdev.get_config = get_config;
808
    vser->vdev.set_config = set_config;
809

    
810
    vser->qdev = dev;
811

    
812
    /*
813
     * Register for the savevm section with the virtio-console name
814
     * to preserve backward compat
815
     */
816
    register_savevm(dev, "virtio-console", -1, 2, virtio_serial_save,
817
                    virtio_serial_load, vser);
818

    
819
    return vdev;
820
}
821

    
822
void virtio_serial_exit(VirtIODevice *vdev)
823
{
824
    VirtIOSerial *vser = DO_UPCAST(VirtIOSerial, vdev, vdev);
825

    
826
    unregister_savevm(vser->qdev, "virtio-console", vser);
827

    
828
    qemu_free(vser->ivqs);
829
    qemu_free(vser->ovqs);
830
    qemu_free(vser->ports_map);
831

    
832
    virtio_cleanup(vdev);
833
}