Statistics
| Branch: | Revision:

root / hw / virtio-serial-bus.c @ ed8e5a85

History | View | Annotate | Download (25.7 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 "trace.h"
23
#include "virtio-serial.h"
24

    
25
/* The virtio-serial bus on top of which the ports will ride as devices */
26
struct VirtIOSerialBus {
27
    BusState qbus;
28

    
29
    /* This is the parent device that provides the bus for ports. */
30
    VirtIOSerial *vser;
31

    
32
    /* The maximum number of ports that can ride on top of this bus */
33
    uint32_t max_nr_ports;
34
};
35

    
36
struct VirtIOSerial {
37
    VirtIODevice vdev;
38

    
39
    VirtQueue *c_ivq, *c_ovq;
40
    /* Arrays of ivqs and ovqs: one per port */
41
    VirtQueue **ivqs, **ovqs;
42

    
43
    VirtIOSerialBus bus;
44

    
45
    DeviceState *qdev;
46

    
47
    QTAILQ_HEAD(, VirtIOSerialPort) ports;
48

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

    
52
    struct virtio_console_config config;
53
};
54

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

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

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

    
70
static VirtIOSerialPort *find_port_by_vq(VirtIOSerial *vser, VirtQueue *vq)
71
{
72
    VirtIOSerialPort *port;
73

    
74
    QTAILQ_FOREACH(port, &vser->ports, next) {
75
        if (port->ivq == vq || port->ovq == vq)
76
            return port;
77
    }
78
    return NULL;
79
}
80

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

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

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

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

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

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

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

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

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

    
121
    if (!virtio_queue_ready(vq)) {
122
        return;
123
    }
124
    while (virtqueue_pop(vq, &elem)) {
125
        virtqueue_push(vq, &elem, 0);
126
    }
127
    virtio_notify(vdev, vq);
128
}
129

    
130
static void do_flush_queued_data(VirtIOSerialPort *port, VirtQueue *vq,
131
                                 VirtIODevice *vdev)
132
{
133
    VirtIOSerialPortInfo *info;
134

    
135
    assert(port);
136
    assert(virtio_queue_ready(vq));
137

    
138
    info = DO_UPCAST(VirtIOSerialPortInfo, qdev, port->dev.info);
139

    
140
    while (!port->throttled) {
141
        unsigned int i;
142

    
143
        /* Pop an elem only if we haven't left off a previous one mid-way */
144
        if (!port->elem.out_num) {
145
            if (!virtqueue_pop(vq, &port->elem)) {
146
                break;
147
            }
148
            port->iov_idx = 0;
149
            port->iov_offset = 0;
150
        }
151

    
152
        for (i = port->iov_idx; i < port->elem.out_num; i++) {
153
            size_t buf_size;
154
            ssize_t ret;
155

    
156
            buf_size = port->elem.out_sg[i].iov_len - port->iov_offset;
157
            ret = info->have_data(port,
158
                                  port->elem.out_sg[i].iov_base
159
                                  + port->iov_offset,
160
                                  buf_size);
161
            if (ret < 0 && ret != -EAGAIN) {
162
                /* We don't handle any other type of errors here */
163
                abort();
164
            }
165
            if (ret == -EAGAIN || (ret >= 0 && ret < buf_size)) {
166
                /*
167
                 * this is a temporary check until chardevs can signal to
168
                 * frontends that they are writable again. This prevents
169
                 * the console from going into throttled mode (forever)
170
                 * if virtio-console is connected to a pty without a
171
                 * listener. Otherwise the guest spins forever.
172
                 * We can revert this if
173
                 * 1: chardevs can notify frondends
174
                 * 2: the guest driver does not spin in these cases
175
                 */
176
                if (!info->is_console) {
177
                    virtio_serial_throttle_port(port, true);
178
                }
179
                port->iov_idx = i;
180
                if (ret > 0) {
181
                    port->iov_offset += ret;
182
                }
183
                break;
184
            }
185
            port->iov_offset = 0;
186
        }
187
        if (port->throttled) {
188
            break;
189
        }
190
        virtqueue_push(vq, &port->elem, 0);
191
        port->elem.out_num = 0;
192
    }
193
    virtio_notify(vdev, vq);
194
}
195

    
196
static void flush_queued_data(VirtIOSerialPort *port)
197
{
198
    assert(port);
199

    
200
    if (!virtio_queue_ready(port->ovq)) {
201
        return;
202
    }
203
    do_flush_queued_data(port, port->ovq, &port->vser->vdev);
204
}
205

    
206
static size_t send_control_msg(VirtIOSerialPort *port, void *buf, size_t len)
207
{
208
    VirtQueueElement elem;
209
    VirtQueue *vq;
210
    struct virtio_console_control *cpkt;
211

    
212
    vq = port->vser->c_ivq;
213
    if (!virtio_queue_ready(vq)) {
214
        return 0;
215
    }
216
    if (!virtqueue_pop(vq, &elem)) {
217
        return 0;
218
    }
219

    
220
    cpkt = (struct virtio_console_control *)buf;
221
    stl_p(&cpkt->id, port->id);
222
    memcpy(elem.in_sg[0].iov_base, buf, len);
223

    
224
    virtqueue_push(vq, &elem, len);
225
    virtio_notify(&port->vser->vdev, vq);
226
    return len;
227
}
228

    
229
static size_t send_control_event(VirtIOSerialPort *port, uint16_t event,
230
                                 uint16_t value)
231
{
232
    struct virtio_console_control cpkt;
233

    
234
    stw_p(&cpkt.event, event);
235
    stw_p(&cpkt.value, value);
236

    
237
    trace_virtio_serial_send_control_event(port->id, event, value);
238
    return send_control_msg(port, &cpkt, sizeof(cpkt));
239
}
240

    
241
/* Functions for use inside qemu to open and read from/write to ports */
242
int virtio_serial_open(VirtIOSerialPort *port)
243
{
244
    /* Don't allow opening an already-open port */
245
    if (port->host_connected) {
246
        return 0;
247
    }
248
    /* Send port open notification to the guest */
249
    port->host_connected = true;
250
    send_control_event(port, VIRTIO_CONSOLE_PORT_OPEN, 1);
251

    
252
    return 0;
253
}
254

    
255
int virtio_serial_close(VirtIOSerialPort *port)
256
{
257
    port->host_connected = false;
258
    /*
259
     * If there's any data the guest sent which the app didn't
260
     * consume, reset the throttling flag and discard the data.
261
     */
262
    port->throttled = false;
263
    discard_vq_data(port->ovq, &port->vser->vdev);
264

    
265
    send_control_event(port, VIRTIO_CONSOLE_PORT_OPEN, 0);
266

    
267
    return 0;
268
}
269

    
270
/* Individual ports/apps call this function to write to the guest. */
271
ssize_t virtio_serial_write(VirtIOSerialPort *port, const uint8_t *buf,
272
                            size_t size)
273
{
274
    if (!port || !port->host_connected || !port->guest_connected) {
275
        return 0;
276
    }
277
    return write_to_port(port, buf, size);
278
}
279

    
280
/*
281
 * Readiness of the guest to accept data on a port.
282
 * Returns max. data the guest can receive
283
 */
284
size_t virtio_serial_guest_ready(VirtIOSerialPort *port)
285
{
286
    VirtQueue *vq = port->ivq;
287

    
288
    if (!virtio_queue_ready(vq) ||
289
        !(port->vser->vdev.status & VIRTIO_CONFIG_S_DRIVER_OK) ||
290
        virtio_queue_empty(vq)) {
291
        return 0;
292
    }
293
    if (use_multiport(port->vser) && !port->guest_connected) {
294
        return 0;
295
    }
296

    
297
    if (virtqueue_avail_bytes(vq, 4096, 0)) {
298
        return 4096;
299
    }
300
    if (virtqueue_avail_bytes(vq, 1, 0)) {
301
        return 1;
302
    }
303
    return 0;
304
}
305

    
306
static void flush_queued_data_bh(void *opaque)
307
{
308
    VirtIOSerialPort *port = opaque;
309

    
310
    flush_queued_data(port);
311
}
312

    
313
void virtio_serial_throttle_port(VirtIOSerialPort *port, bool throttle)
314
{
315
    if (!port) {
316
        return;
317
    }
318

    
319
    trace_virtio_serial_throttle_port(port->id, throttle);
320
    port->throttled = throttle;
321
    if (throttle) {
322
        return;
323
    }
324
    qemu_bh_schedule(port->bh);
325
}
326

    
327
/* Guest wants to notify us of some event */
328
static void handle_control_message(VirtIOSerial *vser, void *buf, size_t len)
329
{
330
    struct VirtIOSerialPort *port;
331
    struct VirtIOSerialPortInfo *info;
332
    struct virtio_console_control cpkt, *gcpkt;
333
    uint8_t *buffer;
334
    size_t buffer_len;
335

    
336
    gcpkt = buf;
337

    
338
    if (len < sizeof(cpkt)) {
339
        /* The guest sent an invalid control packet */
340
        return;
341
    }
342

    
343
    cpkt.event = lduw_p(&gcpkt->event);
344
    cpkt.value = lduw_p(&gcpkt->value);
345

    
346
    trace_virtio_serial_handle_control_message(cpkt.event, cpkt.value);
347

    
348
    if (cpkt.event == VIRTIO_CONSOLE_DEVICE_READY) {
349
        if (!cpkt.value) {
350
            error_report("virtio-serial-bus: Guest failure in adding device %s",
351
                         vser->bus.qbus.name);
352
            return;
353
        }
354
        /*
355
         * The device is up, we can now tell the device about all the
356
         * ports we have here.
357
         */
358
        QTAILQ_FOREACH(port, &vser->ports, next) {
359
            send_control_event(port, VIRTIO_CONSOLE_PORT_ADD, 1);
360
        }
361
        return;
362
    }
363

    
364
    port = find_port_by_id(vser, ldl_p(&gcpkt->id));
365
    if (!port) {
366
        error_report("virtio-serial-bus: Unexpected port id %u for device %s",
367
                     ldl_p(&gcpkt->id), vser->bus.qbus.name);
368
        return;
369
    }
370

    
371
    trace_virtio_serial_handle_control_message_port(port->id);
372

    
373
    info = DO_UPCAST(VirtIOSerialPortInfo, qdev, port->dev.info);
374

    
375
    switch(cpkt.event) {
376
    case VIRTIO_CONSOLE_PORT_READY:
377
        if (!cpkt.value) {
378
            error_report("virtio-serial-bus: Guest failure in adding port %u for device %s",
379
                         port->id, vser->bus.qbus.name);
380
            break;
381
        }
382
        /*
383
         * Now that we know the guest asked for the port name, we're
384
         * sure the guest has initialised whatever state is necessary
385
         * for this port. Now's a good time to let the guest know if
386
         * this port is a console port so that the guest can hook it
387
         * up to hvc.
388
         */
389
        if (info->is_console) {
390
            send_control_event(port, VIRTIO_CONSOLE_CONSOLE_PORT, 1);
391
        }
392

    
393
        if (port->name) {
394
            stw_p(&cpkt.event, VIRTIO_CONSOLE_PORT_NAME);
395
            stw_p(&cpkt.value, 1);
396

    
397
            buffer_len = sizeof(cpkt) + strlen(port->name) + 1;
398
            buffer = g_malloc(buffer_len);
399

    
400
            memcpy(buffer, &cpkt, sizeof(cpkt));
401
            memcpy(buffer + sizeof(cpkt), port->name, strlen(port->name));
402
            buffer[buffer_len - 1] = 0;
403

    
404
            send_control_msg(port, buffer, buffer_len);
405
            g_free(buffer);
406
        }
407

    
408
        if (port->host_connected) {
409
            send_control_event(port, VIRTIO_CONSOLE_PORT_OPEN, 1);
410
        }
411

    
412
        /*
413
         * When the guest has asked us for this information it means
414
         * the guest is all setup and has its virtqueues
415
         * initialised. If some app is interested in knowing about
416
         * this event, let it know.
417
         */
418
        if (info->guest_ready) {
419
            info->guest_ready(port);
420
        }
421
        break;
422

    
423
    case VIRTIO_CONSOLE_PORT_OPEN:
424
        port->guest_connected = cpkt.value;
425
        if (cpkt.value && info->guest_open) {
426
            /* Send the guest opened notification if an app is interested */
427
            info->guest_open(port);
428
        }
429

    
430
        if (!cpkt.value && info->guest_close) {
431
            /* Send the guest closed notification if an app is interested */
432
            info->guest_close(port);
433
        }
434
        break;
435
    }
436
}
437

    
438
static void control_in(VirtIODevice *vdev, VirtQueue *vq)
439
{
440
}
441

    
442
static void control_out(VirtIODevice *vdev, VirtQueue *vq)
443
{
444
    VirtQueueElement elem;
445
    VirtIOSerial *vser;
446
    uint8_t *buf;
447
    size_t len;
448

    
449
    vser = DO_UPCAST(VirtIOSerial, vdev, vdev);
450

    
451
    len = 0;
452
    buf = NULL;
453
    while (virtqueue_pop(vq, &elem)) {
454
        size_t cur_len, copied;
455

    
456
        cur_len = iov_size(elem.out_sg, elem.out_num);
457
        /*
458
         * Allocate a new buf only if we didn't have one previously or
459
         * if the size of the buf differs
460
         */
461
        if (cur_len > len) {
462
            g_free(buf);
463

    
464
            buf = g_malloc(cur_len);
465
            len = cur_len;
466
        }
467
        copied = iov_to_buf(elem.out_sg, elem.out_num, buf, 0, len);
468

    
469
        handle_control_message(vser, buf, copied);
470
        virtqueue_push(vq, &elem, 0);
471
    }
472
    g_free(buf);
473
    virtio_notify(vdev, vq);
474
}
475

    
476
/* Guest wrote something to some port. */
477
static void handle_output(VirtIODevice *vdev, VirtQueue *vq)
478
{
479
    VirtIOSerial *vser;
480
    VirtIOSerialPort *port;
481

    
482
    vser = DO_UPCAST(VirtIOSerial, vdev, vdev);
483
    port = find_port_by_vq(vser, vq);
484

    
485
    if (!port || !port->host_connected) {
486
        discard_vq_data(vq, vdev);
487
        return;
488
    }
489

    
490
    if (!port->throttled) {
491
        do_flush_queued_data(port, vq, vdev);
492
        return;
493
    }
494
}
495

    
496
static void handle_input(VirtIODevice *vdev, VirtQueue *vq)
497
{
498
}
499

    
500
static uint32_t get_features(VirtIODevice *vdev, uint32_t features)
501
{
502
    VirtIOSerial *vser;
503

    
504
    vser = DO_UPCAST(VirtIOSerial, vdev, vdev);
505

    
506
    if (vser->bus.max_nr_ports > 1) {
507
        features |= (1 << VIRTIO_CONSOLE_F_MULTIPORT);
508
    }
509
    return features;
510
}
511

    
512
/* Guest requested config info */
513
static void get_config(VirtIODevice *vdev, uint8_t *config_data)
514
{
515
    VirtIOSerial *vser;
516

    
517
    vser = DO_UPCAST(VirtIOSerial, vdev, vdev);
518
    memcpy(config_data, &vser->config, sizeof(struct virtio_console_config));
519
}
520

    
521
static void set_config(VirtIODevice *vdev, const uint8_t *config_data)
522
{
523
    struct virtio_console_config config;
524

    
525
    memcpy(&config, config_data, sizeof(config));
526
}
527

    
528
static void virtio_serial_save(QEMUFile *f, void *opaque)
529
{
530
    VirtIOSerial *s = opaque;
531
    VirtIOSerialPort *port;
532
    uint32_t nr_active_ports;
533
    unsigned int i, max_nr_ports;
534

    
535
    /* The virtio device */
536
    virtio_save(&s->vdev, f);
537

    
538
    /* The config space */
539
    qemu_put_be16s(f, &s->config.cols);
540
    qemu_put_be16s(f, &s->config.rows);
541

    
542
    qemu_put_be32s(f, &s->config.max_nr_ports);
543

    
544
    /* The ports map */
545
    max_nr_ports = tswap32(s->config.max_nr_ports);
546
    for (i = 0; i < (max_nr_ports + 31) / 32; i++) {
547
        qemu_put_be32s(f, &s->ports_map[i]);
548
    }
549

    
550
    /* Ports */
551

    
552
    nr_active_ports = 0;
553
    QTAILQ_FOREACH(port, &s->ports, next) {
554
        nr_active_ports++;
555
    }
556

    
557
    qemu_put_be32s(f, &nr_active_ports);
558

    
559
    /*
560
     * Items in struct VirtIOSerialPort.
561
     */
562
    QTAILQ_FOREACH(port, &s->ports, next) {
563
        uint32_t elem_popped;
564

    
565
        qemu_put_be32s(f, &port->id);
566
        qemu_put_byte(f, port->guest_connected);
567
        qemu_put_byte(f, port->host_connected);
568

    
569
        elem_popped = 0;
570
        if (port->elem.out_num) {
571
            elem_popped = 1;
572
        }
573
        qemu_put_be32s(f, &elem_popped);
574
        if (elem_popped) {
575
            qemu_put_be32s(f, &port->iov_idx);
576
            qemu_put_be64s(f, &port->iov_offset);
577

    
578
            qemu_put_buffer(f, (unsigned char *)&port->elem,
579
                            sizeof(port->elem));
580
        }
581
    }
582
}
583

    
584
static int virtio_serial_load(QEMUFile *f, void *opaque, int version_id)
585
{
586
    VirtIOSerial *s = opaque;
587
    VirtIOSerialPort *port;
588
    uint32_t max_nr_ports, nr_active_ports, ports_map;
589
    unsigned int i;
590

    
591
    if (version_id > 3) {
592
        return -EINVAL;
593
    }
594

    
595
    /* The virtio device */
596
    virtio_load(&s->vdev, f);
597

    
598
    if (version_id < 2) {
599
        return 0;
600
    }
601

    
602
    /* The config space */
603
    qemu_get_be16s(f, &s->config.cols);
604
    qemu_get_be16s(f, &s->config.rows);
605

    
606
    qemu_get_be32s(f, &max_nr_ports);
607
    tswap32s(&max_nr_ports);
608
    if (max_nr_ports > tswap32(s->config.max_nr_ports)) {
609
        /* Source could have had more ports than us. Fail migration. */
610
        return -EINVAL;
611
    }
612

    
613
    for (i = 0; i < (max_nr_ports + 31) / 32; i++) {
614
        qemu_get_be32s(f, &ports_map);
615

    
616
        if (ports_map != s->ports_map[i]) {
617
            /*
618
             * Ports active on source and destination don't
619
             * match. Fail migration.
620
             */
621
            return -EINVAL;
622
        }
623
    }
624

    
625
    qemu_get_be32s(f, &nr_active_ports);
626

    
627
    /* Items in struct VirtIOSerialPort */
628
    for (i = 0; i < nr_active_ports; i++) {
629
        uint32_t id;
630
        bool host_connected;
631

    
632
        id = qemu_get_be32(f);
633
        port = find_port_by_id(s, id);
634
        if (!port) {
635
            return -EINVAL;
636
        }
637

    
638
        port->guest_connected = qemu_get_byte(f);
639
        host_connected = qemu_get_byte(f);
640
        if (host_connected != port->host_connected) {
641
            /*
642
             * We have to let the guest know of the host connection
643
             * status change
644
             */
645
            send_control_event(port, VIRTIO_CONSOLE_PORT_OPEN,
646
                               port->host_connected);
647
        }
648

    
649
        if (version_id > 2) {
650
            uint32_t elem_popped;
651

    
652
            qemu_get_be32s(f, &elem_popped);
653
            if (elem_popped) {
654
                qemu_get_be32s(f, &port->iov_idx);
655
                qemu_get_be64s(f, &port->iov_offset);
656

    
657
                qemu_get_buffer(f, (unsigned char *)&port->elem,
658
                                sizeof(port->elem));
659
                virtqueue_map_sg(port->elem.in_sg, port->elem.in_addr,
660
                                 port->elem.in_num, 1);
661
                virtqueue_map_sg(port->elem.out_sg, port->elem.out_addr,
662
                                 port->elem.out_num, 1);
663

    
664
                /*
665
                 *  Port was throttled on source machine.  Let's
666
                 *  unthrottle it here so data starts flowing again.
667
                 */
668
                virtio_serial_throttle_port(port, false);
669
            }
670
        }
671
    }
672
    return 0;
673
}
674

    
675
static void virtser_bus_dev_print(Monitor *mon, DeviceState *qdev, int indent);
676

    
677
static struct BusInfo virtser_bus_info = {
678
    .name      = "virtio-serial-bus",
679
    .size      = sizeof(VirtIOSerialBus),
680
    .print_dev = virtser_bus_dev_print,
681
    .props      = (Property[]) {
682
        DEFINE_PROP_UINT32("nr", VirtIOSerialPort, id, VIRTIO_CONSOLE_BAD_ID),
683
        DEFINE_PROP_STRING("name", VirtIOSerialPort, name),
684
        DEFINE_PROP_END_OF_LIST()
685
    }
686
};
687

    
688
static void virtser_bus_dev_print(Monitor *mon, DeviceState *qdev, int indent)
689
{
690
    VirtIOSerialPort *port = DO_UPCAST(VirtIOSerialPort, dev, qdev);
691

    
692
    monitor_printf(mon, "%*sport %d, guest %s, host %s, throttle %s\n",
693
                   indent, "", port->id,
694
                   port->guest_connected ? "on" : "off",
695
                   port->host_connected ? "on" : "off",
696
                   port->throttled ? "on" : "off");
697
}
698

    
699
/* This function is only used if a port id is not provided by the user */
700
static uint32_t find_free_port_id(VirtIOSerial *vser)
701
{
702
    unsigned int i, max_nr_ports;
703

    
704
    max_nr_ports = tswap32(vser->config.max_nr_ports);
705
    for (i = 0; i < (max_nr_ports + 31) / 32; i++) {
706
        uint32_t map, bit;
707

    
708
        map = vser->ports_map[i];
709
        bit = ffs(~map);
710
        if (bit) {
711
            return (bit - 1) + i * 32;
712
        }
713
    }
714
    return VIRTIO_CONSOLE_BAD_ID;
715
}
716

    
717
static void mark_port_added(VirtIOSerial *vser, uint32_t port_id)
718
{
719
    unsigned int i;
720

    
721
    i = port_id / 32;
722
    vser->ports_map[i] |= 1U << (port_id % 32);
723
}
724

    
725
static void add_port(VirtIOSerial *vser, uint32_t port_id)
726
{
727
    mark_port_added(vser, port_id);
728

    
729
    send_control_event(find_port_by_id(vser, port_id),
730
                       VIRTIO_CONSOLE_PORT_ADD, 1);
731
}
732

    
733
static void remove_port(VirtIOSerial *vser, uint32_t port_id)
734
{
735
    VirtIOSerialPort *port;
736
    unsigned int i;
737

    
738
    i = port_id / 32;
739
    vser->ports_map[i] &= ~(1U << (port_id % 32));
740

    
741
    port = find_port_by_id(vser, port_id);
742
    /* Flush out any unconsumed buffers first */
743
    discard_vq_data(port->ovq, &port->vser->vdev);
744

    
745
    send_control_event(port, VIRTIO_CONSOLE_PORT_REMOVE, 1);
746
}
747

    
748
static int virtser_port_qdev_init(DeviceState *qdev, DeviceInfo *base)
749
{
750
    VirtIOSerialPort *port = DO_UPCAST(VirtIOSerialPort, dev, qdev);
751
    VirtIOSerialPortInfo *info = DO_UPCAST(VirtIOSerialPortInfo, qdev, base);
752
    VirtIOSerialBus *bus = DO_UPCAST(VirtIOSerialBus, qbus, qdev->parent_bus);
753
    int ret, max_nr_ports;
754
    bool plugging_port0;
755

    
756
    port->vser = bus->vser;
757
    port->bh = qemu_bh_new(flush_queued_data_bh, port);
758

    
759
    assert(info->have_data);
760

    
761
    /*
762
     * Is the first console port we're seeing? If so, put it up at
763
     * location 0. This is done for backward compatibility (old
764
     * kernel, new qemu).
765
     */
766
    plugging_port0 = info->is_console && !find_port_by_id(port->vser, 0);
767

    
768
    if (find_port_by_id(port->vser, port->id)) {
769
        error_report("virtio-serial-bus: A port already exists at id %u",
770
                     port->id);
771
        return -1;
772
    }
773

    
774
    if (port->id == VIRTIO_CONSOLE_BAD_ID) {
775
        if (plugging_port0) {
776
            port->id = 0;
777
        } else {
778
            port->id = find_free_port_id(port->vser);
779
            if (port->id == VIRTIO_CONSOLE_BAD_ID) {
780
                error_report("virtio-serial-bus: Maximum port limit for this device reached");
781
                return -1;
782
            }
783
        }
784
    }
785

    
786
    max_nr_ports = tswap32(port->vser->config.max_nr_ports);
787
    if (port->id >= max_nr_ports) {
788
        error_report("virtio-serial-bus: Out-of-range port id specified, max. allowed: %u",
789
                     max_nr_ports - 1);
790
        return -1;
791
    }
792

    
793
    ret = info->init(port);
794
    if (ret) {
795
        return ret;
796
    }
797

    
798
    if (!use_multiport(port->vser)) {
799
        /*
800
         * Allow writes to guest in this case; we have no way of
801
         * knowing if a guest port is connected.
802
         */
803
        port->guest_connected = true;
804
    }
805

    
806
    port->elem.out_num = 0;
807

    
808
    QTAILQ_INSERT_TAIL(&port->vser->ports, port, next);
809
    port->ivq = port->vser->ivqs[port->id];
810
    port->ovq = port->vser->ovqs[port->id];
811

    
812
    add_port(port->vser, port->id);
813

    
814
    /* Send an update to the guest about this new port added */
815
    virtio_notify_config(&port->vser->vdev);
816

    
817
    return ret;
818
}
819

    
820
static int virtser_port_qdev_exit(DeviceState *qdev)
821
{
822
    VirtIOSerialPort *port = DO_UPCAST(VirtIOSerialPort, dev, qdev);
823
    VirtIOSerialPortInfo *info = DO_UPCAST(VirtIOSerialPortInfo, qdev,
824
                                           port->dev.info);
825
    VirtIOSerial *vser = port->vser;
826

    
827
    qemu_bh_delete(port->bh);
828
    remove_port(port->vser, port->id);
829

    
830
    QTAILQ_REMOVE(&vser->ports, port, next);
831

    
832
    if (info->exit) {
833
        info->exit(port);
834
    }
835
    return 0;
836
}
837

    
838
void virtio_serial_port_qdev_register(VirtIOSerialPortInfo *info)
839
{
840
    info->qdev.init = virtser_port_qdev_init;
841
    info->qdev.bus_info = &virtser_bus_info;
842
    info->qdev.exit = virtser_port_qdev_exit;
843
    info->qdev.unplug = qdev_simple_unplug_cb;
844
    qdev_register(&info->qdev);
845
}
846

    
847
VirtIODevice *virtio_serial_init(DeviceState *dev, virtio_serial_conf *conf)
848
{
849
    VirtIOSerial *vser;
850
    VirtIODevice *vdev;
851
    uint32_t i, max_supported_ports;
852

    
853
    if (!conf->max_virtserial_ports)
854
        return NULL;
855

    
856
    /* Each port takes 2 queues, and one pair is for the control queue */
857
    max_supported_ports = VIRTIO_PCI_QUEUE_MAX / 2 - 1;
858

    
859
    if (conf->max_virtserial_ports > max_supported_ports) {
860
        error_report("maximum ports supported: %u", max_supported_ports);
861
        return NULL;
862
    }
863

    
864
    vdev = virtio_common_init("virtio-serial", VIRTIO_ID_CONSOLE,
865
                              sizeof(struct virtio_console_config),
866
                              sizeof(VirtIOSerial));
867

    
868
    vser = DO_UPCAST(VirtIOSerial, vdev, vdev);
869

    
870
    /* Spawn a new virtio-serial bus on which the ports will ride as devices */
871
    qbus_create_inplace(&vser->bus.qbus, &virtser_bus_info, dev, NULL);
872
    vser->bus.qbus.allow_hotplug = 1;
873
    vser->bus.vser = vser;
874
    QTAILQ_INIT(&vser->ports);
875

    
876
    vser->bus.max_nr_ports = conf->max_virtserial_ports;
877
    vser->ivqs = g_malloc(conf->max_virtserial_ports * sizeof(VirtQueue *));
878
    vser->ovqs = g_malloc(conf->max_virtserial_ports * sizeof(VirtQueue *));
879

    
880
    /* Add a queue for host to guest transfers for port 0 (backward compat) */
881
    vser->ivqs[0] = virtio_add_queue(vdev, 128, handle_input);
882
    /* Add a queue for guest to host transfers for port 0 (backward compat) */
883
    vser->ovqs[0] = virtio_add_queue(vdev, 128, handle_output);
884

    
885
    /* TODO: host to guest notifications can get dropped
886
     * if the queue fills up. Implement queueing in host,
887
     * this might also make it possible to reduce the control
888
     * queue size: as guest preposts buffers there,
889
     * this will save 4Kbyte of guest memory per entry. */
890

    
891
    /* control queue: host to guest */
892
    vser->c_ivq = virtio_add_queue(vdev, 32, control_in);
893
    /* control queue: guest to host */
894
    vser->c_ovq = virtio_add_queue(vdev, 32, control_out);
895

    
896
    for (i = 1; i < vser->bus.max_nr_ports; i++) {
897
        /* Add a per-port queue for host to guest transfers */
898
        vser->ivqs[i] = virtio_add_queue(vdev, 128, handle_input);
899
        /* Add a per-per queue for guest to host transfers */
900
        vser->ovqs[i] = virtio_add_queue(vdev, 128, handle_output);
901
    }
902

    
903
    vser->config.max_nr_ports = tswap32(conf->max_virtserial_ports);
904
    vser->ports_map = g_malloc0(((conf->max_virtserial_ports + 31) / 32)
905
        * sizeof(vser->ports_map[0]));
906
    /*
907
     * Reserve location 0 for a console port for backward compat
908
     * (old kernel, new qemu)
909
     */
910
    mark_port_added(vser, 0);
911

    
912
    vser->vdev.get_features = get_features;
913
    vser->vdev.get_config = get_config;
914
    vser->vdev.set_config = set_config;
915

    
916
    vser->qdev = dev;
917

    
918
    /*
919
     * Register for the savevm section with the virtio-console name
920
     * to preserve backward compat
921
     */
922
    register_savevm(dev, "virtio-console", -1, 3, virtio_serial_save,
923
                    virtio_serial_load, vser);
924

    
925
    return vdev;
926
}
927

    
928
void virtio_serial_exit(VirtIODevice *vdev)
929
{
930
    VirtIOSerial *vser = DO_UPCAST(VirtIOSerial, vdev, vdev);
931

    
932
    unregister_savevm(vser->qdev, "virtio-console", vser);
933

    
934
    g_free(vser->ivqs);
935
    g_free(vser->ovqs);
936
    g_free(vser->ports_map);
937

    
938
    virtio_cleanup(vdev);
939
}