Statistics
| Branch: | Revision:

root / hw / virtio-serial-bus.c @ 1ecda02b

History | View | Annotate | Download (16.9 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
    uint8_t *buffer;
208
    size_t buffer_len;
209

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

    
215
    cpkt.event = lduw_p(&gcpkt->event);
216
    cpkt.value = lduw_p(&gcpkt->value);
217

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

    
231
        if (port->name) {
232
            stw_p(&cpkt.event, VIRTIO_CONSOLE_PORT_NAME);
233
            stw_p(&cpkt.value, 1);
234

    
235
            buffer_len = sizeof(cpkt) + strlen(port->name) + 1;
236
            buffer = qemu_malloc(buffer_len);
237

    
238
            memcpy(buffer, &cpkt, sizeof(cpkt));
239
            memcpy(buffer + sizeof(cpkt), port->name, strlen(port->name));
240
            buffer[buffer_len - 1] = 0;
241

    
242
            send_control_msg(port, buffer, buffer_len);
243
            qemu_free(buffer);
244
        }
245

    
246
        if (port->host_connected) {
247
            send_control_event(port, VIRTIO_CONSOLE_PORT_OPEN, 1);
248
        }
249

    
250
        /*
251
         * When the guest has asked us for this information it means
252
         * the guest is all setup and has its virtqueues
253
         * initialised. If some app is interested in knowing about
254
         * this event, let it know.
255
         */
256
        if (port->info->guest_ready) {
257
            port->info->guest_ready(port);
258
        }
259
        break;
260

    
261
    case VIRTIO_CONSOLE_PORT_OPEN:
262
        port->guest_connected = cpkt.value;
263
        if (cpkt.value && port->info->guest_open) {
264
            /* Send the guest opened notification if an app is interested */
265
            port->info->guest_open(port);
266
        }
267

    
268
        if (!cpkt.value && port->info->guest_close) {
269
            /* Send the guest closed notification if an app is interested */
270
            port->info->guest_close(port);
271
        }
272
        break;
273
    }
274
}
275

    
276
static void control_in(VirtIODevice *vdev, VirtQueue *vq)
277
{
278
}
279

    
280
static void control_out(VirtIODevice *vdev, VirtQueue *vq)
281
{
282
    VirtQueueElement elem;
283
    VirtIOSerial *vser;
284

    
285
    vser = DO_UPCAST(VirtIOSerial, vdev, vdev);
286

    
287
    while (virtqueue_pop(vq, &elem)) {
288
        handle_control_message(vser, elem.out_sg[0].iov_base);
289
        virtqueue_push(vq, &elem, elem.out_sg[0].iov_len);
290
    }
291
    virtio_notify(vdev, vq);
292
}
293

    
294
/* Guest wrote something to some port. */
295
static void handle_output(VirtIODevice *vdev, VirtQueue *vq)
296
{
297
    VirtIOSerial *vser;
298
    VirtQueueElement elem;
299

    
300
    vser = DO_UPCAST(VirtIOSerial, vdev, vdev);
301

    
302
    while (virtqueue_pop(vq, &elem)) {
303
        VirtIOSerialPort *port;
304
        size_t ret;
305

    
306
        port = find_port_by_vq(vser, vq);
307
        if (!port) {
308
            ret = 0;
309
            goto next_buf;
310
        }
311

    
312
        /*
313
         * A port may not have any handler registered for consuming the
314
         * data that the guest sends or it may not have a chardev associated
315
         * with it. Just ignore the data in that case.
316
         */
317
        if (!port->info->have_data) {
318
            ret = 0;
319
            goto next_buf;
320
        }
321

    
322
        /* The guest always sends only one sg */
323
        ret = port->info->have_data(port, elem.out_sg[0].iov_base,
324
                                    elem.out_sg[0].iov_len);
325

    
326
    next_buf:
327
        virtqueue_push(vq, &elem, ret);
328
    }
329
    virtio_notify(vdev, vq);
330
}
331

    
332
static void handle_input(VirtIODevice *vdev, VirtQueue *vq)
333
{
334
}
335

    
336
static uint32_t get_features(VirtIODevice *vdev, uint32_t features)
337
{
338
    VirtIOSerial *vser = DO_UPCAST(VirtIOSerial, vdev, vdev);
339
    if (vser->bus->max_nr_ports > 1) {
340
        features |= (1 << VIRTIO_CONSOLE_F_MULTIPORT);
341
    }
342
    return features;
343
}
344

    
345
/* Guest requested config info */
346
static void get_config(VirtIODevice *vdev, uint8_t *config_data)
347
{
348
    VirtIOSerial *vser;
349

    
350
    vser = DO_UPCAST(VirtIOSerial, vdev, vdev);
351
    memcpy(config_data, &vser->config, sizeof(struct virtio_console_config));
352
}
353

    
354
static void set_config(VirtIODevice *vdev, const uint8_t *config_data)
355
{
356
    struct virtio_console_config config;
357

    
358
    memcpy(&config, config_data, sizeof(config));
359
}
360

    
361
static void virtio_serial_save(QEMUFile *f, void *opaque)
362
{
363
    VirtIOSerial *s = opaque;
364
    VirtIOSerialPort *port;
365
    uint32_t nr_active_ports;
366

    
367
    /* The virtio device */
368
    virtio_save(&s->vdev, f);
369

    
370
    /* The config space */
371
    qemu_put_be16s(f, &s->config.cols);
372
    qemu_put_be16s(f, &s->config.rows);
373
    qemu_put_be32s(f, &s->config.nr_ports);
374

    
375
    /* Items in struct VirtIOSerial */
376

    
377
    /* Do this because we might have hot-unplugged some ports */
378
    nr_active_ports = 0;
379
    QTAILQ_FOREACH(port, &s->ports, next)
380
        nr_active_ports++;
381

    
382
    qemu_put_be32s(f, &nr_active_ports);
383

    
384
    /*
385
     * Items in struct VirtIOSerialPort.
386
     */
387
    QTAILQ_FOREACH(port, &s->ports, next) {
388
        /*
389
         * We put the port number because we may not have an active
390
         * port at id 0 that's reserved for a console port, or in case
391
         * of ports that might have gotten unplugged
392
         */
393
        qemu_put_be32s(f, &port->id);
394
        qemu_put_byte(f, port->guest_connected);
395
    }
396
}
397

    
398
static int virtio_serial_load(QEMUFile *f, void *opaque, int version_id)
399
{
400
    VirtIOSerial *s = opaque;
401
    VirtIOSerialPort *port;
402
    uint32_t nr_active_ports;
403
    unsigned int i;
404

    
405
    if (version_id > 2) {
406
        return -EINVAL;
407
    }
408

    
409
    /* The virtio device */
410
    virtio_load(&s->vdev, f);
411

    
412
    if (version_id < 2) {
413
        return 0;
414
    }
415

    
416
    /* The config space */
417
    qemu_get_be16s(f, &s->config.cols);
418
    qemu_get_be16s(f, &s->config.rows);
419
    s->config.nr_ports = qemu_get_be32(f);
420

    
421
    /* Items in struct VirtIOSerial */
422

    
423
    qemu_get_be32s(f, &nr_active_ports);
424

    
425
    /* Items in struct VirtIOSerialPort */
426
    for (i = 0; i < nr_active_ports; i++) {
427
        uint32_t id;
428

    
429
        id = qemu_get_be32(f);
430
        port = find_port_by_id(s, id);
431

    
432
        port->guest_connected = qemu_get_byte(f);
433
    }
434

    
435
    return 0;
436
}
437

    
438
static void virtser_bus_dev_print(Monitor *mon, DeviceState *qdev, int indent);
439

    
440
static struct BusInfo virtser_bus_info = {
441
    .name      = "virtio-serial-bus",
442
    .size      = sizeof(VirtIOSerialBus),
443
    .print_dev = virtser_bus_dev_print,
444
};
445

    
446
static VirtIOSerialBus *virtser_bus_new(DeviceState *dev)
447
{
448
    VirtIOSerialBus *bus;
449

    
450
    bus = FROM_QBUS(VirtIOSerialBus, qbus_create(&virtser_bus_info, dev, NULL));
451
    bus->qbus.allow_hotplug = 1;
452

    
453
    return bus;
454
}
455

    
456
static void virtser_bus_dev_print(Monitor *mon, DeviceState *qdev, int indent)
457
{
458
    VirtIOSerialDevice *dev = DO_UPCAST(VirtIOSerialDevice, qdev, qdev);
459
    VirtIOSerialPort *port = DO_UPCAST(VirtIOSerialPort, dev, &dev->qdev);
460

    
461
    monitor_printf(mon, "%*s dev-prop-int: id: %u\n",
462
                   indent, "", port->id);
463
    monitor_printf(mon, "%*s dev-prop-int: guest_connected: %d\n",
464
                   indent, "", port->guest_connected);
465
    monitor_printf(mon, "%*s dev-prop-int: host_connected: %d\n",
466
                   indent, "", port->host_connected);
467
}
468

    
469
static int virtser_port_qdev_init(DeviceState *qdev, DeviceInfo *base)
470
{
471
    VirtIOSerialDevice *dev = DO_UPCAST(VirtIOSerialDevice, qdev, qdev);
472
    VirtIOSerialPortInfo *info = DO_UPCAST(VirtIOSerialPortInfo, qdev, base);
473
    VirtIOSerialPort *port = DO_UPCAST(VirtIOSerialPort, dev, &dev->qdev);
474
    VirtIOSerialBus *bus = DO_UPCAST(VirtIOSerialBus, qbus, qdev->parent_bus);
475
    int ret;
476
    bool plugging_port0;
477

    
478
    port->vser = bus->vser;
479

    
480
    /*
481
     * Is the first console port we're seeing? If so, put it up at
482
     * location 0. This is done for backward compatibility (old
483
     * kernel, new qemu).
484
     */
485
    plugging_port0 = port->is_console && !find_port_by_id(port->vser, 0);
486

    
487
    if (port->vser->config.nr_ports == bus->max_nr_ports && !plugging_port0) {
488
        error_report("virtio-serial-bus: Maximum device limit reached");
489
        return -1;
490
    }
491
    dev->info = info;
492

    
493
    ret = info->init(dev);
494
    if (ret) {
495
        return ret;
496
    }
497

    
498
    port->id = plugging_port0 ? 0 : port->vser->config.nr_ports++;
499

    
500
    if (!use_multiport(port->vser)) {
501
        /*
502
         * Allow writes to guest in this case; we have no way of
503
         * knowing if a guest port is connected.
504
         */
505
        port->guest_connected = true;
506
    }
507

    
508
    QTAILQ_INSERT_TAIL(&port->vser->ports, port, next);
509
    port->ivq = port->vser->ivqs[port->id];
510
    port->ovq = port->vser->ovqs[port->id];
511

    
512
    /* Send an update to the guest about this new port added */
513
    virtio_notify_config(&port->vser->vdev);
514

    
515
    return ret;
516
}
517

    
518
static int virtser_port_qdev_exit(DeviceState *qdev)
519
{
520
    VirtIOSerialDevice *dev = DO_UPCAST(VirtIOSerialDevice, qdev, qdev);
521
    VirtIOSerialPort *port = DO_UPCAST(VirtIOSerialPort, dev, &dev->qdev);
522
    VirtIOSerial *vser = port->vser;
523

    
524
    send_control_event(port, VIRTIO_CONSOLE_PORT_REMOVE, 1);
525

    
526
    /*
527
     * Don't decrement nr_ports here; thus we keep a linearly
528
     * increasing port id. Not utilising an id again saves us a couple
529
     * of complications:
530
     *
531
     * - Not having to bother about sending the port id to the guest
532
     *   kernel on hotplug or on addition of new ports; the guest can
533
     *   also linearly increment the port number. This is preferable
534
     *   because the config space won't have the need to store a
535
     *   ports_map.
536
     *
537
     * - Extra state to be stored for all the "holes" that got created
538
     *   so that we keep filling in the ids from the least available
539
     *   index.
540
     *
541
     * When such a functionality is desired, a control message to add
542
     * a port can be introduced.
543
     */
544
    QTAILQ_REMOVE(&vser->ports, port, next);
545

    
546
    if (port->info->exit)
547
        port->info->exit(dev);
548

    
549
    return 0;
550
}
551

    
552
void virtio_serial_port_qdev_register(VirtIOSerialPortInfo *info)
553
{
554
    info->qdev.init = virtser_port_qdev_init;
555
    info->qdev.bus_info = &virtser_bus_info;
556
    info->qdev.exit = virtser_port_qdev_exit;
557
    info->qdev.unplug = qdev_simple_unplug_cb;
558
    qdev_register(&info->qdev);
559
}
560

    
561
VirtIODevice *virtio_serial_init(DeviceState *dev, uint32_t max_nr_ports)
562
{
563
    VirtIOSerial *vser;
564
    VirtIODevice *vdev;
565
    uint32_t i;
566

    
567
    if (!max_nr_ports)
568
        return NULL;
569

    
570
    vdev = virtio_common_init("virtio-serial", VIRTIO_ID_CONSOLE,
571
                              sizeof(struct virtio_console_config),
572
                              sizeof(VirtIOSerial));
573

    
574
    vser = DO_UPCAST(VirtIOSerial, vdev, vdev);
575

    
576
    /* Spawn a new virtio-serial bus on which the ports will ride as devices */
577
    vser->bus = virtser_bus_new(dev);
578
    vser->bus->vser = vser;
579
    QTAILQ_INIT(&vser->ports);
580

    
581
    vser->bus->max_nr_ports = max_nr_ports;
582
    vser->ivqs = qemu_malloc(max_nr_ports * sizeof(VirtQueue *));
583
    vser->ovqs = qemu_malloc(max_nr_ports * sizeof(VirtQueue *));
584

    
585
    /* Add a queue for host to guest transfers for port 0 (backward compat) */
586
    vser->ivqs[0] = virtio_add_queue(vdev, 128, handle_input);
587
    /* Add a queue for guest to host transfers for port 0 (backward compat) */
588
    vser->ovqs[0] = virtio_add_queue(vdev, 128, handle_output);
589

    
590
    /* control queue: host to guest */
591
    vser->c_ivq = virtio_add_queue(vdev, 16, control_in);
592
    /* control queue: guest to host */
593
    vser->c_ovq = virtio_add_queue(vdev, 16, control_out);
594

    
595
    for (i = 1; i < vser->bus->max_nr_ports; i++) {
596
        /* Add a per-port queue for host to guest transfers */
597
        vser->ivqs[i] = virtio_add_queue(vdev, 128, handle_input);
598
        /* Add a per-per queue for guest to host transfers */
599
        vser->ovqs[i] = virtio_add_queue(vdev, 128, handle_output);
600
    }
601

    
602
    vser->config.max_nr_ports = max_nr_ports;
603
    /*
604
     * Reserve location 0 for a console port for backward compat
605
     * (old kernel, new qemu)
606
     */
607
    vser->config.nr_ports = 1;
608

    
609
    vser->vdev.get_features = get_features;
610
    vser->vdev.get_config = get_config;
611
    vser->vdev.set_config = set_config;
612

    
613
    /*
614
     * Register for the savevm section with the virtio-console name
615
     * to preserve backward compat
616
     */
617
    register_savevm("virtio-console", -1, 2, virtio_serial_save,
618
                    virtio_serial_load, vser);
619

    
620
    return vdev;
621
}