Statistics
| Branch: | Revision:

root / hw / virtio-serial-bus.c @ 6663a195

History | View | Annotate | Download (16.3 kB)

1
/*
2
 * A bus for connecting virtio serial and console ports
3
 *
4
 * Copyright (C) 2009 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 "monitor.h"
19
#include "qemu-queue.h"
20
#include "sysbus.h"
21
#include "virtio-serial.h"
22

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

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

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

    
34
struct VirtIOSerial {
35
    VirtIODevice vdev;
36

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

    
41
    VirtIOSerialBus *bus;
42

    
43
    QTAILQ_HEAD(, VirtIOSerialPort) ports;
44
    struct virtio_console_config config;
45
};
46

    
47
static VirtIOSerialPort *find_port_by_id(VirtIOSerial *vser, uint32_t id)
48
{
49
    VirtIOSerialPort *port;
50

    
51
    QTAILQ_FOREACH(port, &vser->ports, next) {
52
        if (port->id == id)
53
            return port;
54
    }
55
    return NULL;
56
}
57

    
58
static VirtIOSerialPort *find_port_by_vq(VirtIOSerial *vser, VirtQueue *vq)
59
{
60
    VirtIOSerialPort *port;
61

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

    
69
static bool use_multiport(VirtIOSerial *vser)
70
{
71
    return vser->vdev.guest_features & (1 << VIRTIO_CONSOLE_F_MULTIPORT);
72
}
73

    
74
static size_t write_to_port(VirtIOSerialPort *port,
75
                            const uint8_t *buf, size_t size)
76
{
77
    VirtQueueElement elem;
78
    VirtQueue *vq;
79
    size_t offset = 0;
80
    size_t len = 0;
81

    
82
    vq = port->ivq;
83
    if (!virtio_queue_ready(vq)) {
84
        return 0;
85
    }
86
    if (!size) {
87
        return 0;
88
    }
89

    
90
    while (offset < size) {
91
        int i;
92

    
93
        if (!virtqueue_pop(vq, &elem)) {
94
            break;
95
        }
96

    
97
        for (i = 0; offset < size && i < elem.in_num; i++) {
98
            len = MIN(elem.in_sg[i].iov_len, size - offset);
99

    
100
            memcpy(elem.in_sg[i].iov_base, buf + offset, len);
101
            offset += len;
102
        }
103
        virtqueue_push(vq, &elem, len);
104
    }
105

    
106
    virtio_notify(&port->vser->vdev, vq);
107
    return offset;
108
}
109

    
110
static size_t send_control_msg(VirtIOSerialPort *port, void *buf, size_t len)
111
{
112
    VirtQueueElement elem;
113
    VirtQueue *vq;
114
    struct virtio_console_control *cpkt;
115

    
116
    vq = port->vser->c_ivq;
117
    if (!virtio_queue_ready(vq)) {
118
        return 0;
119
    }
120
    if (!virtqueue_pop(vq, &elem)) {
121
        return 0;
122
    }
123

    
124
    cpkt = (struct virtio_console_control *)buf;
125
    stl_p(&cpkt->id, port->id);
126
    memcpy(elem.in_sg[0].iov_base, buf, len);
127

    
128
    virtqueue_push(vq, &elem, len);
129
    virtio_notify(&port->vser->vdev, vq);
130
    return len;
131
}
132

    
133
static size_t send_control_event(VirtIOSerialPort *port, uint16_t event,
134
                                 uint16_t value)
135
{
136
    struct virtio_console_control cpkt;
137

    
138
    stw_p(&cpkt.event, event);
139
    stw_p(&cpkt.value, value);
140

    
141
    return send_control_msg(port, &cpkt, sizeof(cpkt));
142
}
143

    
144
/* Functions for use inside qemu to open and read from/write to ports */
145
int virtio_serial_open(VirtIOSerialPort *port)
146
{
147
    /* Don't allow opening an already-open port */
148
    if (port->host_connected) {
149
        return 0;
150
    }
151
    /* Send port open notification to the guest */
152
    port->host_connected = true;
153
    send_control_event(port, VIRTIO_CONSOLE_PORT_OPEN, 1);
154

    
155
    return 0;
156
}
157

    
158
int virtio_serial_close(VirtIOSerialPort *port)
159
{
160
    port->host_connected = false;
161
    send_control_event(port, VIRTIO_CONSOLE_PORT_OPEN, 0);
162

    
163
    return 0;
164
}
165

    
166
/* Individual ports/apps call this function to write to the guest. */
167
ssize_t virtio_serial_write(VirtIOSerialPort *port, const uint8_t *buf,
168
                            size_t size)
169
{
170
    if (!port || !port->host_connected || !port->guest_connected) {
171
        return 0;
172
    }
173
    return write_to_port(port, buf, size);
174
}
175

    
176
/*
177
 * Readiness of the guest to accept data on a port.
178
 * Returns max. data the guest can receive
179
 */
180
size_t virtio_serial_guest_ready(VirtIOSerialPort *port)
181
{
182
    VirtQueue *vq = port->ivq;
183

    
184
    if (!virtio_queue_ready(vq) ||
185
        !(port->vser->vdev.status & VIRTIO_CONFIG_S_DRIVER_OK) ||
186
        virtio_queue_empty(vq)) {
187
        return 0;
188
    }
189
    if (use_multiport(port->vser) && !port->guest_connected) {
190
        return 0;
191
    }
192

    
193
    if (virtqueue_avail_bytes(vq, 4096, 0)) {
194
        return 4096;
195
    }
196
    if (virtqueue_avail_bytes(vq, 1, 0)) {
197
        return 1;
198
    }
199
    return 0;
200
}
201

    
202
/* Guest wants to notify us of some event */
203
static void handle_control_message(VirtIOSerial *vser, void *buf)
204
{
205
    struct VirtIOSerialPort *port;
206
    struct virtio_console_control cpkt, *gcpkt;
207

    
208
    gcpkt = buf;
209
    port = find_port_by_id(vser, ldl_p(&gcpkt->id));
210
    if (!port)
211
        return;
212

    
213
    cpkt.event = lduw_p(&gcpkt->event);
214
    cpkt.value = lduw_p(&gcpkt->value);
215

    
216
    switch(cpkt.event) {
217
    case VIRTIO_CONSOLE_PORT_READY:
218
        /*
219
         * Now that we know the guest asked for the port name, we're
220
         * sure the guest has initialised whatever state is necessary
221
         * for this port. Now's a good time to let the guest know if
222
         * this port is a console port so that the guest can hook it
223
         * up to hvc.
224
         */
225
        if (port->is_console) {
226
            send_control_event(port, VIRTIO_CONSOLE_CONSOLE_PORT, 1);
227
        }
228

    
229
        if (port->host_connected) {
230
            send_control_event(port, VIRTIO_CONSOLE_PORT_OPEN, 1);
231
        }
232

    
233
        /*
234
         * When the guest has asked us for this information it means
235
         * the guest is all setup and has its virtqueues
236
         * initialised. If some app is interested in knowing about
237
         * this event, let it know.
238
         */
239
        if (port->info->guest_ready) {
240
            port->info->guest_ready(port);
241
        }
242
        break;
243

    
244
    case VIRTIO_CONSOLE_PORT_OPEN:
245
        port->guest_connected = cpkt.value;
246
        if (cpkt.value && port->info->guest_open) {
247
            /* Send the guest opened notification if an app is interested */
248
            port->info->guest_open(port);
249
        }
250

    
251
        if (!cpkt.value && port->info->guest_close) {
252
            /* Send the guest closed notification if an app is interested */
253
            port->info->guest_close(port);
254
        }
255
        break;
256
    }
257
}
258

    
259
static void control_in(VirtIODevice *vdev, VirtQueue *vq)
260
{
261
}
262

    
263
static void control_out(VirtIODevice *vdev, VirtQueue *vq)
264
{
265
    VirtQueueElement elem;
266
    VirtIOSerial *vser;
267

    
268
    vser = DO_UPCAST(VirtIOSerial, vdev, vdev);
269

    
270
    while (virtqueue_pop(vq, &elem)) {
271
        handle_control_message(vser, elem.out_sg[0].iov_base);
272
        virtqueue_push(vq, &elem, elem.out_sg[0].iov_len);
273
    }
274
    virtio_notify(vdev, vq);
275
}
276

    
277
/* Guest wrote something to some port. */
278
static void handle_output(VirtIODevice *vdev, VirtQueue *vq)
279
{
280
    VirtIOSerial *vser;
281
    VirtQueueElement elem;
282

    
283
    vser = DO_UPCAST(VirtIOSerial, vdev, vdev);
284

    
285
    while (virtqueue_pop(vq, &elem)) {
286
        VirtIOSerialPort *port;
287
        size_t ret;
288

    
289
        port = find_port_by_vq(vser, vq);
290
        if (!port) {
291
            ret = 0;
292
            goto next_buf;
293
        }
294

    
295
        /*
296
         * A port may not have any handler registered for consuming the
297
         * data that the guest sends or it may not have a chardev associated
298
         * with it. Just ignore the data in that case.
299
         */
300
        if (!port->info->have_data) {
301
            ret = 0;
302
            goto next_buf;
303
        }
304

    
305
        /* The guest always sends only one sg */
306
        ret = port->info->have_data(port, elem.out_sg[0].iov_base,
307
                                    elem.out_sg[0].iov_len);
308

    
309
    next_buf:
310
        virtqueue_push(vq, &elem, ret);
311
    }
312
    virtio_notify(vdev, vq);
313
}
314

    
315
static void handle_input(VirtIODevice *vdev, VirtQueue *vq)
316
{
317
}
318

    
319
static uint32_t get_features(VirtIODevice *vdev, uint32_t features)
320
{
321
    features |= (1 << VIRTIO_CONSOLE_F_MULTIPORT);
322

    
323
    return features;
324
}
325

    
326
/* Guest requested config info */
327
static void get_config(VirtIODevice *vdev, uint8_t *config_data)
328
{
329
    VirtIOSerial *vser;
330

    
331
    vser = DO_UPCAST(VirtIOSerial, vdev, vdev);
332
    memcpy(config_data, &vser->config, sizeof(struct virtio_console_config));
333
}
334

    
335
static void set_config(VirtIODevice *vdev, const uint8_t *config_data)
336
{
337
    struct virtio_console_config config;
338

    
339
    memcpy(&config, config_data, sizeof(config));
340
}
341

    
342
static void virtio_serial_save(QEMUFile *f, void *opaque)
343
{
344
    VirtIOSerial *s = opaque;
345
    VirtIOSerialPort *port;
346
    uint32_t nr_active_ports;
347

    
348
    /* The virtio device */
349
    virtio_save(&s->vdev, f);
350

    
351
    /* The config space */
352
    qemu_put_be16s(f, &s->config.cols);
353
    qemu_put_be16s(f, &s->config.rows);
354
    qemu_put_be32s(f, &s->config.nr_ports);
355

    
356
    /* Items in struct VirtIOSerial */
357

    
358
    /* Do this because we might have hot-unplugged some ports */
359
    nr_active_ports = 0;
360
    QTAILQ_FOREACH(port, &s->ports, next)
361
        nr_active_ports++;
362

    
363
    qemu_put_be32s(f, &nr_active_ports);
364

    
365
    /*
366
     * Items in struct VirtIOSerialPort.
367
     */
368
    QTAILQ_FOREACH(port, &s->ports, next) {
369
        /*
370
         * We put the port number because we may not have an active
371
         * port at id 0 that's reserved for a console port, or in case
372
         * of ports that might have gotten unplugged
373
         */
374
        qemu_put_be32s(f, &port->id);
375
        qemu_put_byte(f, port->guest_connected);
376
    }
377
}
378

    
379
static int virtio_serial_load(QEMUFile *f, void *opaque, int version_id)
380
{
381
    VirtIOSerial *s = opaque;
382
    VirtIOSerialPort *port;
383
    uint32_t nr_active_ports;
384
    unsigned int i;
385

    
386
    if (version_id > 2) {
387
        return -EINVAL;
388
    }
389

    
390
    /* The virtio device */
391
    virtio_load(&s->vdev, f);
392

    
393
    if (version_id < 2) {
394
        return 0;
395
    }
396

    
397
    /* The config space */
398
    qemu_get_be16s(f, &s->config.cols);
399
    qemu_get_be16s(f, &s->config.rows);
400
    s->config.nr_ports = qemu_get_be32(f);
401

    
402
    /* Items in struct VirtIOSerial */
403

    
404
    qemu_get_be32s(f, &nr_active_ports);
405

    
406
    /* Items in struct VirtIOSerialPort */
407
    for (i = 0; i < nr_active_ports; i++) {
408
        uint32_t id;
409

    
410
        id = qemu_get_be32(f);
411
        port = find_port_by_id(s, id);
412

    
413
        port->guest_connected = qemu_get_byte(f);
414
    }
415

    
416
    return 0;
417
}
418

    
419
static void virtser_bus_dev_print(Monitor *mon, DeviceState *qdev, int indent);
420

    
421
static struct BusInfo virtser_bus_info = {
422
    .name      = "virtio-serial-bus",
423
    .size      = sizeof(VirtIOSerialBus),
424
    .print_dev = virtser_bus_dev_print,
425
};
426

    
427
static VirtIOSerialBus *virtser_bus_new(DeviceState *dev)
428
{
429
    VirtIOSerialBus *bus;
430

    
431
    bus = FROM_QBUS(VirtIOSerialBus, qbus_create(&virtser_bus_info, dev,
432
                                                 "virtio-serial-bus"));
433
    bus->qbus.allow_hotplug = 1;
434

    
435
    return bus;
436
}
437

    
438
static void virtser_bus_dev_print(Monitor *mon, DeviceState *qdev, int indent)
439
{
440
    VirtIOSerialDevice *dev = DO_UPCAST(VirtIOSerialDevice, qdev, qdev);
441
    VirtIOSerialPort *port = DO_UPCAST(VirtIOSerialPort, dev, &dev->qdev);
442

    
443
    monitor_printf(mon, "%*s dev-prop-int: id: %u\n",
444
                   indent, "", port->id);
445
    monitor_printf(mon, "%*s dev-prop-int: guest_connected: %d\n",
446
                   indent, "", port->guest_connected);
447
    monitor_printf(mon, "%*s dev-prop-int: host_connected: %d\n",
448
                   indent, "", port->host_connected);
449
}
450

    
451
static int virtser_port_qdev_init(DeviceState *qdev, DeviceInfo *base)
452
{
453
    VirtIOSerialDevice *dev = DO_UPCAST(VirtIOSerialDevice, qdev, qdev);
454
    VirtIOSerialPortInfo *info = DO_UPCAST(VirtIOSerialPortInfo, qdev, base);
455
    VirtIOSerialPort *port = DO_UPCAST(VirtIOSerialPort, dev, &dev->qdev);
456
    VirtIOSerialBus *bus = DO_UPCAST(VirtIOSerialBus, qbus, qdev->parent_bus);
457
    int ret;
458
    bool plugging_port0;
459

    
460
    port->vser = bus->vser;
461

    
462
    /*
463
     * Is the first console port we're seeing? If so, put it up at
464
     * location 0. This is done for backward compatibility (old
465
     * kernel, new qemu).
466
     */
467
    plugging_port0 = port->is_console && !find_port_by_id(port->vser, 0);
468

    
469
    if (port->vser->config.nr_ports == bus->max_nr_ports && !plugging_port0) {
470
        qemu_error("virtio-serial-bus: Maximum device limit reached\n");
471
        return -1;
472
    }
473
    dev->info = info;
474

    
475
    ret = info->init(dev);
476
    if (ret) {
477
        return ret;
478
    }
479

    
480
    port->id = plugging_port0 ? 0 : port->vser->config.nr_ports++;
481

    
482
    if (!use_multiport(port->vser)) {
483
        /*
484
         * Allow writes to guest in this case; we have no way of
485
         * knowing if a guest port is connected.
486
         */
487
        port->guest_connected = true;
488
    }
489

    
490
    QTAILQ_INSERT_TAIL(&port->vser->ports, port, next);
491
    port->ivq = port->vser->ivqs[port->id];
492
    port->ovq = port->vser->ovqs[port->id];
493

    
494
    /* Send an update to the guest about this new port added */
495
    virtio_notify_config(&port->vser->vdev);
496

    
497
    return ret;
498
}
499

    
500
static int virtser_port_qdev_exit(DeviceState *qdev)
501
{
502
    VirtIOSerialDevice *dev = DO_UPCAST(VirtIOSerialDevice, qdev, qdev);
503
    VirtIOSerialPort *port = DO_UPCAST(VirtIOSerialPort, dev, &dev->qdev);
504
    VirtIOSerial *vser = port->vser;
505

    
506
    /*
507
     * Don't decrement nr_ports here; thus we keep a linearly
508
     * increasing port id. Not utilising an id again saves us a couple
509
     * of complications:
510
     *
511
     * - Not having to bother about sending the port id to the guest
512
     *   kernel on hotplug or on addition of new ports; the guest can
513
     *   also linearly increment the port number. This is preferable
514
     *   because the config space won't have the need to store a
515
     *   ports_map.
516
     *
517
     * - Extra state to be stored for all the "holes" that got created
518
     *   so that we keep filling in the ids from the least available
519
     *   index.
520
     *
521
     * When such a functionality is desired, a control message to add
522
     * a port can be introduced.
523
     */
524
    QTAILQ_REMOVE(&vser->ports, port, next);
525

    
526
    if (port->info->exit)
527
        port->info->exit(dev);
528

    
529
    return 0;
530
}
531

    
532
void virtio_serial_port_qdev_register(VirtIOSerialPortInfo *info)
533
{
534
    info->qdev.init = virtser_port_qdev_init;
535
    info->qdev.bus_info = &virtser_bus_info;
536
    info->qdev.exit = virtser_port_qdev_exit;
537
    info->qdev.unplug = qdev_simple_unplug_cb;
538
    qdev_register(&info->qdev);
539
}
540

    
541
VirtIODevice *virtio_serial_init(DeviceState *dev, uint32_t max_nr_ports)
542
{
543
    VirtIOSerial *vser;
544
    VirtIODevice *vdev;
545
    uint32_t i;
546

    
547
    if (!max_nr_ports)
548
        return NULL;
549

    
550
    vdev = virtio_common_init("virtio-serial", VIRTIO_ID_CONSOLE,
551
                              sizeof(struct virtio_console_config),
552
                              sizeof(VirtIOSerial));
553

    
554
    vser = DO_UPCAST(VirtIOSerial, vdev, vdev);
555

    
556
    /* Spawn a new virtio-serial bus on which the ports will ride as devices */
557
    vser->bus = virtser_bus_new(dev);
558
    vser->bus->vser = vser;
559
    QTAILQ_INIT(&vser->ports);
560

    
561
    vser->bus->max_nr_ports = max_nr_ports;
562
    vser->ivqs = qemu_malloc(max_nr_ports * sizeof(VirtQueue *));
563
    vser->ovqs = qemu_malloc(max_nr_ports * sizeof(VirtQueue *));
564

    
565
    /* Add a queue for host to guest transfers for port 0 (backward compat) */
566
    vser->ivqs[0] = virtio_add_queue(vdev, 128, handle_input);
567
    /* Add a queue for guest to host transfers for port 0 (backward compat) */
568
    vser->ovqs[0] = virtio_add_queue(vdev, 128, handle_output);
569

    
570
    /* control queue: host to guest */
571
    vser->c_ivq = virtio_add_queue(vdev, 16, control_in);
572
    /* control queue: guest to host */
573
    vser->c_ovq = virtio_add_queue(vdev, 16, control_out);
574

    
575
    for (i = 1; i < vser->bus->max_nr_ports; i++) {
576
        /* Add a per-port queue for host to guest transfers */
577
        vser->ivqs[i] = virtio_add_queue(vdev, 128, handle_input);
578
        /* Add a per-per queue for guest to host transfers */
579
        vser->ovqs[i] = virtio_add_queue(vdev, 128, handle_output);
580
    }
581

    
582
    vser->config.max_nr_ports = max_nr_ports;
583
    /*
584
     * Reserve location 0 for a console port for backward compat
585
     * (old kernel, new qemu)
586
     */
587
    vser->config.nr_ports = 1;
588

    
589
    vser->vdev.get_features = get_features;
590
    vser->vdev.get_config = get_config;
591
    vser->vdev.set_config = set_config;
592

    
593
    /*
594
     * Register for the savevm section with the virtio-console name
595
     * to preserve backward compat
596
     */
597
    register_savevm("virtio-console", -1, 2, virtio_serial_save,
598
                    virtio_serial_load, vser);
599

    
600
    return vdev;
601
}