virtio-serial-bus.c 27.1 KB
Newer Older
1 2 3
/*
 * A bus for connecting virtio serial and console ports
 *
4
 * Copyright (C) 2009, 2010 Red Hat, Inc.
5 6 7 8 9 10 11 12 13 14 15
 *
 * Author(s):
 *  Amit Shah <amit.shah@redhat.com>
 *
 * Some earlier parts are:
 *  Copyright IBM, Corp. 2008
 * authored by
 *  Christian Ehrhardt <ehrhardt@linux.vnet.ibm.com>
 *
 * This work is licensed under the terms of the GNU GPL, version 2.  See
 * the COPYING file in the top-level directory.
16 17 18
 *
 * Contributions after 2012-01-13 are licensed under the terms of the
 * GNU GPL, version 2 or (at your option) any later version.
19 20
 */

21
#include "iov.h"
22 23 24
#include "monitor.h"
#include "qemu-queue.h"
#include "sysbus.h"
A
Amit Shah 已提交
25
#include "trace.h"
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
#include "virtio-serial.h"

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

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

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

struct VirtIOSerial {
    VirtIODevice vdev;

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

46
    VirtIOSerialBus bus;
47

48 49
    DeviceState *qdev;

50
    QTAILQ_HEAD(, VirtIOSerialPort) ports;
51 52 53 54

    /* bitmap for identifying active ports */
    uint32_t *ports_map;

55 56 57 58 59 60 61
    struct virtio_console_config config;
};

static VirtIOSerialPort *find_port_by_id(VirtIOSerial *vser, uint32_t id)
{
    VirtIOSerialPort *port;

62 63 64 65
    if (id == VIRTIO_CONSOLE_BAD_ID) {
        return NULL;
    }

66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83
    QTAILQ_FOREACH(port, &vser->ports, next) {
        if (port->id == id)
            return port;
    }
    return NULL;
}

static VirtIOSerialPort *find_port_by_vq(VirtIOSerial *vser, VirtQueue *vq)
{
    VirtIOSerialPort *port;

    QTAILQ_FOREACH(port, &vser->ports, next) {
        if (port->ivq == vq || port->ovq == vq)
            return port;
    }
    return NULL;
}

84 85 86 87 88
static bool use_multiport(VirtIOSerial *vser)
{
    return vser->vdev.guest_features & (1 << VIRTIO_CONSOLE_F_MULTIPORT);
}

89 90 91 92 93
static size_t write_to_port(VirtIOSerialPort *port,
                            const uint8_t *buf, size_t size)
{
    VirtQueueElement elem;
    VirtQueue *vq;
94
    size_t offset;
95 96 97 98 99 100

    vq = port->ivq;
    if (!virtio_queue_ready(vq)) {
        return 0;
    }

101
    offset = 0;
102
    while (offset < size) {
103
        size_t len;
104 105 106 107 108

        if (!virtqueue_pop(vq, &elem)) {
            break;
        }

109
        len = iov_from_buf(elem.in_sg, elem.in_num,
110
                           buf + offset, 0, size - offset);
111
        offset += len;
112 113 114 115 116 117 118 119

        virtqueue_push(vq, &elem, len);
    }

    virtio_notify(&port->vser->vdev, vq);
    return offset;
}

120
static void discard_vq_data(VirtQueue *vq, VirtIODevice *vdev)
121 122 123
{
    VirtQueueElement elem;

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

133
static void do_flush_queued_data(VirtIOSerialPort *port, VirtQueue *vq,
134
                                 VirtIODevice *vdev)
135
{
136
    VirtIOSerialPortClass *vsc;
137

138
    assert(port);
139
    assert(virtio_queue_ready(vq));
140

141
    vsc = VIRTIO_SERIAL_PORT_GET_CLASS(port);
142

143
    while (!port->throttled) {
144
        unsigned int i;
145

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

155 156 157 158 159
        for (i = port->iov_idx; i < port->elem.out_num; i++) {
            size_t buf_size;
            ssize_t ret;

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

199
static void flush_queued_data(VirtIOSerialPort *port)
200
{
201
    assert(port);
202

203 204 205
    if (!virtio_queue_ready(port->ovq)) {
        return;
    }
206
    do_flush_queued_data(port, port->ovq, &port->vser->vdev);
207 208
}

209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239
static size_t send_control_msg(VirtIOSerialPort *port, void *buf, size_t len)
{
    VirtQueueElement elem;
    VirtQueue *vq;
    struct virtio_console_control *cpkt;

    vq = port->vser->c_ivq;
    if (!virtio_queue_ready(vq)) {
        return 0;
    }
    if (!virtqueue_pop(vq, &elem)) {
        return 0;
    }

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

    virtqueue_push(vq, &elem, len);
    virtio_notify(&port->vser->vdev, vq);
    return len;
}

static size_t send_control_event(VirtIOSerialPort *port, uint16_t event,
                                 uint16_t value)
{
    struct virtio_console_control cpkt;

    stw_p(&cpkt.event, event);
    stw_p(&cpkt.value, value);

A
Amit Shah 已提交
240
    trace_virtio_serial_send_control_event(port->id, event, value);
241 242 243 244 245 246
    return send_control_msg(port, &cpkt, sizeof(cpkt));
}

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

255 256 257 258 259
    return 0;
}

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

268 269
    send_control_event(port, VIRTIO_CONSOLE_PORT_OPEN, 0);

270 271 272 273 274 275 276
    return 0;
}

/* Individual ports/apps call this function to write to the guest. */
ssize_t virtio_serial_write(VirtIOSerialPort *port, const uint8_t *buf,
                            size_t size)
{
277 278 279
    if (!port || !port->host_connected || !port->guest_connected) {
        return 0;
    }
280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295
    return write_to_port(port, buf, size);
}

/*
 * Readiness of the guest to accept data on a port.
 * Returns max. data the guest can receive
 */
size_t virtio_serial_guest_ready(VirtIOSerialPort *port)
{
    VirtQueue *vq = port->ivq;

    if (!virtio_queue_ready(vq) ||
        !(port->vser->vdev.status & VIRTIO_CONFIG_S_DRIVER_OK) ||
        virtio_queue_empty(vq)) {
        return 0;
    }
296 297 298
    if (use_multiport(port->vser) && !port->guest_connected) {
        return 0;
    }
299 300 301 302 303 304 305 306 307 308

    if (virtqueue_avail_bytes(vq, 4096, 0)) {
        return 4096;
    }
    if (virtqueue_avail_bytes(vq, 1, 0)) {
        return 1;
    }
    return 0;
}

309 310 311 312 313 314 315
static void flush_queued_data_bh(void *opaque)
{
    VirtIOSerialPort *port = opaque;

    flush_queued_data(port);
}

316 317 318 319 320 321
void virtio_serial_throttle_port(VirtIOSerialPort *port, bool throttle)
{
    if (!port) {
        return;
    }

A
Amit Shah 已提交
322
    trace_virtio_serial_throttle_port(port->id, throttle);
323 324 325 326
    port->throttled = throttle;
    if (throttle) {
        return;
    }
327
    qemu_bh_schedule(port->bh);
328 329
}

330
/* Guest wants to notify us of some event */
331
static void handle_control_message(VirtIOSerial *vser, void *buf, size_t len)
332 333
{
    struct VirtIOSerialPort *port;
334
    VirtIOSerialPortClass *vsc;
335
    struct virtio_console_control cpkt, *gcpkt;
336 337
    uint8_t *buffer;
    size_t buffer_len;
338 339 340

    gcpkt = buf;

341 342 343 344 345
    if (len < sizeof(cpkt)) {
        /* The guest sent an invalid control packet */
        return;
    }

346 347 348
    cpkt.event = lduw_p(&gcpkt->event);
    cpkt.value = lduw_p(&gcpkt->value);

A
Amit Shah 已提交
349 350
    trace_virtio_serial_handle_control_message(cpkt.event, cpkt.value);

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

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

A
Amit Shah 已提交
374 375
    trace_virtio_serial_handle_control_message_port(port->id);

376
    vsc = VIRTIO_SERIAL_PORT_GET_CLASS(port);
377 378

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

396 397 398 399 400
        if (port->name) {
            stw_p(&cpkt.event, VIRTIO_CONSOLE_PORT_NAME);
            stw_p(&cpkt.value, 1);

            buffer_len = sizeof(cpkt) + strlen(port->name) + 1;
401
            buffer = g_malloc(buffer_len);
402 403 404 405 406 407

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

            send_control_msg(port, buffer, buffer_len);
408
            g_free(buffer);
409 410
        }

411 412 413 414
        if (port->host_connected) {
            send_control_event(port, VIRTIO_CONSOLE_PORT_OPEN, 1);
        }

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

    case VIRTIO_CONSOLE_PORT_OPEN:
        port->guest_connected = cpkt.value;
428
        if (cpkt.value && vsc->guest_open) {
429
            /* Send the guest opened notification if an app is interested */
430
            vsc->guest_open(port);
431 432
        }

433
        if (!cpkt.value && vsc->guest_close) {
434
            /* Send the guest closed notification if an app is interested */
435
            vsc->guest_close(port);
436 437
        }
        break;
438 439 440 441 442 443 444 445 446 447 448
    }
}

static void control_in(VirtIODevice *vdev, VirtQueue *vq)
{
}

static void control_out(VirtIODevice *vdev, VirtQueue *vq)
{
    VirtQueueElement elem;
    VirtIOSerial *vser;
449 450
    uint8_t *buf;
    size_t len;
451 452 453

    vser = DO_UPCAST(VirtIOSerial, vdev, vdev);

454 455
    len = 0;
    buf = NULL;
456
    while (virtqueue_pop(vq, &elem)) {
457
        size_t cur_len;
458 459 460 461 462 463 464

        cur_len = iov_size(elem.out_sg, elem.out_num);
        /*
         * Allocate a new buf only if we didn't have one previously or
         * if the size of the buf differs
         */
        if (cur_len > len) {
465
            g_free(buf);
466

467
            buf = g_malloc(cur_len);
468 469
            len = cur_len;
        }
470
        iov_to_buf(elem.out_sg, elem.out_num, buf, 0, cur_len);
471

472
        handle_control_message(vser, buf, cur_len);
473
        virtqueue_push(vq, &elem, 0);
474
    }
475
    g_free(buf);
476 477 478 479 480 481 482
    virtio_notify(vdev, vq);
}

/* Guest wrote something to some port. */
static void handle_output(VirtIODevice *vdev, VirtQueue *vq)
{
    VirtIOSerial *vser;
483
    VirtIOSerialPort *port;
484 485

    vser = DO_UPCAST(VirtIOSerial, vdev, vdev);
486
    port = find_port_by_vq(vser, vq);
487

488
    if (!port || !port->host_connected) {
489 490 491
        discard_vq_data(vq, vdev);
        return;
    }
492 493 494

    if (!port->throttled) {
        do_flush_queued_data(port, vq, vdev);
495 496
        return;
    }
497 498 499 500 501 502 503 504
}

static void handle_input(VirtIODevice *vdev, VirtQueue *vq)
{
}

static uint32_t get_features(VirtIODevice *vdev, uint32_t features)
{
505 506 507 508
    VirtIOSerial *vser;

    vser = DO_UPCAST(VirtIOSerial, vdev, vdev);

509
    if (vser->bus.max_nr_ports > 1) {
510 511
        features |= (1 << VIRTIO_CONSOLE_F_MULTIPORT);
    }
512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530
    return features;
}

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

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

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

    memcpy(&config, config_data, sizeof(config));
}

531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546
static void guest_reset(VirtIOSerial *vser)
{
    VirtIOSerialPort *port;
    VirtIOSerialPortClass *vsc;

    QTAILQ_FOREACH(port, &vser->ports, next) {
        vsc = VIRTIO_SERIAL_PORT_GET_CLASS(port);
        if (port->guest_connected) {
            port->guest_connected = false;

            if (vsc->guest_close)
                vsc->guest_close(port);
        }
    }
}

547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564
static void set_status(VirtIODevice *vdev, uint8_t status)
{
    VirtIOSerial *vser;
    VirtIOSerialPort *port;

    vser = DO_UPCAST(VirtIOSerial, vdev, vdev);
    port = find_port_by_id(vser, 0);

    if (port && !use_multiport(port->vser)
        && (status & VIRTIO_CONFIG_S_DRIVER_OK)) {
        /*
         * Non-multiport guests won't be able to tell us guest
         * open/close status.  Such guests can only have a port at id
         * 0, so set guest_connected for such ports as soon as guest
         * is up.
         */
        port->guest_connected = true;
    }
565 566 567 568 569 570 571 572 573 574 575
    if (!(status & VIRTIO_CONFIG_S_DRIVER_OK)) {
        guest_reset(vser);
    }
}

static void vser_reset(VirtIODevice *vdev)
{
    VirtIOSerial *vser;

    vser = DO_UPCAST(VirtIOSerial, vdev, vdev);
    guest_reset(vser);
576 577
}

578 579 580
static void virtio_serial_save(QEMUFile *f, void *opaque)
{
    VirtIOSerial *s = opaque;
581 582
    VirtIOSerialPort *port;
    uint32_t nr_active_ports;
583
    unsigned int i, max_nr_ports;
584 585 586 587 588 589 590

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

    /* The config space */
    qemu_put_be16s(f, &s->config.cols);
    qemu_put_be16s(f, &s->config.rows);
591

592 593 594
    qemu_put_be32s(f, &s->config.max_nr_ports);

    /* The ports map */
595 596
    max_nr_ports = tswap32(s->config.max_nr_ports);
    for (i = 0; i < (max_nr_ports + 31) / 32; i++) {
597 598
        qemu_put_be32s(f, &s->ports_map[i]);
    }
599

600
    /* Ports */
601

602
    nr_active_ports = 0;
603
    QTAILQ_FOREACH(port, &s->ports, next) {
604
        nr_active_ports++;
605
    }
606 607 608 609 610 611 612

    qemu_put_be32s(f, &nr_active_ports);

    /*
     * Items in struct VirtIOSerialPort.
     */
    QTAILQ_FOREACH(port, &s->ports, next) {
613 614
        uint32_t elem_popped;

615 616
        qemu_put_be32s(f, &port->id);
        qemu_put_byte(f, port->guest_connected);
617
        qemu_put_byte(f, port->host_connected);
618 619 620 621 622 623 624 625 626 627 628 629 630

	elem_popped = 0;
        if (port->elem.out_num) {
            elem_popped = 1;
        }
        qemu_put_be32s(f, &elem_popped);
        if (elem_popped) {
            qemu_put_be32s(f, &port->iov_idx);
            qemu_put_be64s(f, &port->iov_offset);

            qemu_put_buffer(f, (unsigned char *)&port->elem,
                            sizeof(port->elem));
        }
631
    }
632 633 634 635 636
}

static int virtio_serial_load(QEMUFile *f, void *opaque, int version_id)
{
    VirtIOSerial *s = opaque;
637
    VirtIOSerialPort *port;
638
    uint32_t max_nr_ports, nr_active_ports, ports_map;
639
    unsigned int i;
640
    int ret;
641

642
    if (version_id > 3) {
643 644
        return -EINVAL;
    }
645

646
    /* The virtio device */
647 648 649 650
    ret = virtio_load(&s->vdev, f);
    if (ret) {
        return ret;
    }
651 652 653 654 655 656 657 658

    if (version_id < 2) {
        return 0;
    }

    /* The config space */
    qemu_get_be16s(f, &s->config.cols);
    qemu_get_be16s(f, &s->config.rows);
659

660
    qemu_get_be32s(f, &max_nr_ports);
661 662
    tswap32s(&max_nr_ports);
    if (max_nr_ports > tswap32(s->config.max_nr_ports)) {
663
        /* Source could have had more ports than us. Fail migration. */
664 665
        return -EINVAL;
    }
666

667
    for (i = 0; i < (max_nr_ports + 31) / 32; i++) {
668
        qemu_get_be32s(f, &ports_map);
669

670
        if (ports_map != s->ports_map[i]) {
671 672 673 674 675 676
            /*
             * Ports active on source and destination don't
             * match. Fail migration.
             */
            return -EINVAL;
        }
677 678
    }

679 680 681 682 683
    qemu_get_be32s(f, &nr_active_ports);

    /* Items in struct VirtIOSerialPort */
    for (i = 0; i < nr_active_ports; i++) {
        uint32_t id;
684
        bool host_connected;
685 686 687

        id = qemu_get_be32(f);
        port = find_port_by_id(s, id);
688 689 690
        if (!port) {
            return -EINVAL;
        }
691 692

        port->guest_connected = qemu_get_byte(f);
693 694 695 696 697 698 699 700 701
        host_connected = qemu_get_byte(f);
        if (host_connected != port->host_connected) {
            /*
             * We have to let the guest know of the host connection
             * status change
             */
            send_control_event(port, VIRTIO_CONSOLE_PORT_OPEN,
                               port->host_connected);
        }
702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724

        if (version_id > 2) {
            uint32_t elem_popped;

            qemu_get_be32s(f, &elem_popped);
            if (elem_popped) {
                qemu_get_be32s(f, &port->iov_idx);
                qemu_get_be64s(f, &port->iov_offset);

                qemu_get_buffer(f, (unsigned char *)&port->elem,
                                sizeof(port->elem));
                virtqueue_map_sg(port->elem.in_sg, port->elem.in_addr,
                                 port->elem.in_num, 1);
                virtqueue_map_sg(port->elem.out_sg, port->elem.out_addr,
                                 port->elem.out_num, 1);

                /*
                 *  Port was throttled on source machine.  Let's
                 *  unthrottle it here so data starts flowing again.
                 */
                virtio_serial_throttle_port(port, false);
            }
        }
725
    }
726 727 728 729 730 731 732 733 734
    return 0;
}

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

static struct BusInfo virtser_bus_info = {
    .name      = "virtio-serial-bus",
    .size      = sizeof(VirtIOSerialBus),
    .print_dev = virtser_bus_dev_print,
735 736 737 738 739
    .props      = (Property[]) {
        DEFINE_PROP_UINT32("nr", VirtIOSerialPort, id, VIRTIO_CONSOLE_BAD_ID),
        DEFINE_PROP_STRING("name", VirtIOSerialPort, name),
        DEFINE_PROP_END_OF_LIST()
    }
740 741 742 743
};

static void virtser_bus_dev_print(Monitor *mon, DeviceState *qdev, int indent)
{
744
    VirtIOSerialPort *port = DO_UPCAST(VirtIOSerialPort, dev, qdev);
745

746 747 748 749 750
    monitor_printf(mon, "%*sport %d, guest %s, host %s, throttle %s\n",
                   indent, "", port->id,
                   port->guest_connected ? "on" : "off",
                   port->host_connected ? "on" : "off",
                   port->throttled ? "on" : "off");
751 752
}

753 754 755
/* This function is only used if a port id is not provided by the user */
static uint32_t find_free_port_id(VirtIOSerial *vser)
{
756
    unsigned int i, max_nr_ports;
757

758 759
    max_nr_ports = tswap32(vser->config.max_nr_ports);
    for (i = 0; i < (max_nr_ports + 31) / 32; i++) {
760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788
        uint32_t map, bit;

        map = vser->ports_map[i];
        bit = ffs(~map);
        if (bit) {
            return (bit - 1) + i * 32;
        }
    }
    return VIRTIO_CONSOLE_BAD_ID;
}

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

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

static void add_port(VirtIOSerial *vser, uint32_t port_id)
{
    mark_port_added(vser, port_id);

    send_control_event(find_port_by_id(vser, port_id),
                       VIRTIO_CONSOLE_PORT_ADD, 1);
}

static void remove_port(VirtIOSerial *vser, uint32_t port_id)
{
789
    VirtIOSerialPort *port;
790 791 792 793 794
    unsigned int i;

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

795 796
    port = find_port_by_id(vser, port_id);
    /* Flush out any unconsumed buffers first */
797
    discard_vq_data(port->ovq, &port->vser->vdev);
798 799

    send_control_event(port, VIRTIO_CONSOLE_PORT_REMOVE, 1);
800 801
}

A
Anthony Liguori 已提交
802
static int virtser_port_qdev_init(DeviceState *qdev)
803
{
804
    VirtIOSerialPort *port = DO_UPCAST(VirtIOSerialPort, dev, qdev);
805
    VirtIOSerialPortClass *vsc = VIRTIO_SERIAL_PORT_GET_CLASS(port);
806
    VirtIOSerialBus *bus = DO_UPCAST(VirtIOSerialBus, qbus, qdev->parent_bus);
807
    int ret, max_nr_ports;
808 809 810
    bool plugging_port0;

    port->vser = bus->vser;
811
    port->bh = qemu_bh_new(flush_queued_data_bh, port);
812

813
    assert(vsc->have_data);
814

815 816 817 818 819
    /*
     * Is the first console port we're seeing? If so, put it up at
     * location 0. This is done for backward compatibility (old
     * kernel, new qemu).
     */
820
    plugging_port0 = vsc->is_console && !find_port_by_id(port->vser, 0);
821

822
    if (find_port_by_id(port->vser, port->id)) {
823
        error_report("virtio-serial-bus: A port already exists at id %u",
824
                     port->id);
825 826 827
        return -1;
    }

828 829 830 831 832 833
    if (port->id == VIRTIO_CONSOLE_BAD_ID) {
        if (plugging_port0) {
            port->id = 0;
        } else {
            port->id = find_free_port_id(port->vser);
            if (port->id == VIRTIO_CONSOLE_BAD_ID) {
834
                error_report("virtio-serial-bus: Maximum port limit for this device reached");
835 836 837 838 839
                return -1;
            }
        }
    }

840 841
    max_nr_ports = tswap32(port->vser->config.max_nr_ports);
    if (port->id >= max_nr_ports) {
842
        error_report("virtio-serial-bus: Out-of-range port id specified, max. allowed: %u",
843
                     max_nr_ports - 1);
844 845 846
        return -1;
    }

847
    ret = vsc->init(port);
848 849 850 851
    if (ret) {
        return ret;
    }

852 853
    port->elem.out_num = 0;

854 855 856 857
    QTAILQ_INSERT_TAIL(&port->vser->ports, port, next);
    port->ivq = port->vser->ivqs[port->id];
    port->ovq = port->vser->ovqs[port->id];

858 859
    add_port(port->vser, port->id);

860 861 862 863 864 865 866 867
    /* Send an update to the guest about this new port added */
    virtio_notify_config(&port->vser->vdev);

    return ret;
}

static int virtser_port_qdev_exit(DeviceState *qdev)
{
868
    VirtIOSerialPort *port = DO_UPCAST(VirtIOSerialPort, dev, qdev);
869
    VirtIOSerialPortClass *vsc = VIRTIO_SERIAL_PORT_GET_CLASS(port);
870 871
    VirtIOSerial *vser = port->vser;

872
    qemu_bh_delete(port->bh);
873
    remove_port(port->vser, port->id);
874

875 876
    QTAILQ_REMOVE(&vser->ports, port, next);

877 878
    if (vsc->exit) {
        vsc->exit(port);
879
    }
880 881 882
    return 0;
}

883
VirtIODevice *virtio_serial_init(DeviceState *dev, virtio_serial_conf *conf)
884 885 886
{
    VirtIOSerial *vser;
    VirtIODevice *vdev;
887
    uint32_t i, max_supported_ports;
888

889
    if (!conf->max_virtserial_ports)
890 891
        return NULL;

892 893 894
    /* Each port takes 2 queues, and one pair is for the control queue */
    max_supported_ports = VIRTIO_PCI_QUEUE_MAX / 2 - 1;

895
    if (conf->max_virtserial_ports > max_supported_ports) {
896 897 898 899
        error_report("maximum ports supported: %u", max_supported_ports);
        return NULL;
    }

900 901 902 903 904 905 906
    vdev = virtio_common_init("virtio-serial", VIRTIO_ID_CONSOLE,
                              sizeof(struct virtio_console_config),
                              sizeof(VirtIOSerial));

    vser = DO_UPCAST(VirtIOSerial, vdev, vdev);

    /* Spawn a new virtio-serial bus on which the ports will ride as devices */
907 908 909
    qbus_create_inplace(&vser->bus.qbus, &virtser_bus_info, dev, NULL);
    vser->bus.qbus.allow_hotplug = 1;
    vser->bus.vser = vser;
910 911
    QTAILQ_INIT(&vser->ports);

912
    vser->bus.max_nr_ports = conf->max_virtserial_ports;
913 914
    vser->ivqs = g_malloc(conf->max_virtserial_ports * sizeof(VirtQueue *));
    vser->ovqs = g_malloc(conf->max_virtserial_ports * sizeof(VirtQueue *));
915 916 917 918 919 920

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

921 922 923 924 925 926
    /* TODO: host to guest notifications can get dropped
     * if the queue fills up. Implement queueing in host,
     * this might also make it possible to reduce the control
     * queue size: as guest preposts buffers there,
     * this will save 4Kbyte of guest memory per entry. */

927
    /* control queue: host to guest */
928
    vser->c_ivq = virtio_add_queue(vdev, 32, control_in);
929
    /* control queue: guest to host */
930
    vser->c_ovq = virtio_add_queue(vdev, 32, control_out);
931

932
    for (i = 1; i < vser->bus.max_nr_ports; i++) {
933 934 935 936 937 938
        /* Add a per-port queue for host to guest transfers */
        vser->ivqs[i] = virtio_add_queue(vdev, 128, handle_input);
        /* Add a per-per queue for guest to host transfers */
        vser->ovqs[i] = virtio_add_queue(vdev, 128, handle_output);
    }

939
    vser->config.max_nr_ports = tswap32(conf->max_virtserial_ports);
940
    vser->ports_map = g_malloc0(((conf->max_virtserial_ports + 31) / 32)
941
        * sizeof(vser->ports_map[0]));
942 943 944 945
    /*
     * Reserve location 0 for a console port for backward compat
     * (old kernel, new qemu)
     */
946
    mark_port_added(vser, 0);
947 948 949 950

    vser->vdev.get_features = get_features;
    vser->vdev.get_config = get_config;
    vser->vdev.set_config = set_config;
951
    vser->vdev.set_status = set_status;
952
    vser->vdev.reset = vser_reset;
953

954 955
    vser->qdev = dev;

956 957 958 959
    /*
     * Register for the savevm section with the virtio-console name
     * to preserve backward compat
     */
960
    register_savevm(dev, "virtio-console", -1, 3, virtio_serial_save,
961 962 963 964
                    virtio_serial_load, vser);

    return vdev;
}
965 966 967 968 969 970 971

void virtio_serial_exit(VirtIODevice *vdev)
{
    VirtIOSerial *vser = DO_UPCAST(VirtIOSerial, vdev, vdev);

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

972 973 974
    g_free(vser->ivqs);
    g_free(vser->ovqs);
    g_free(vser->ports_map);
975 976 977

    virtio_cleanup(vdev);
}
978

979 980 981 982 983 984 985 986 987
static void virtio_serial_port_class_init(ObjectClass *klass, void *data)
{
    DeviceClass *k = DEVICE_CLASS(klass);
    k->init = virtser_port_qdev_init;
    k->bus_info = &virtser_bus_info;
    k->exit = virtser_port_qdev_exit;
    k->unplug = qdev_simple_unplug_cb;
}

988 989 990 991 992 993
static TypeInfo virtio_serial_port_type_info = {
    .name = TYPE_VIRTIO_SERIAL_PORT,
    .parent = TYPE_DEVICE,
    .instance_size = sizeof(VirtIOSerialPort),
    .abstract = true,
    .class_size = sizeof(VirtIOSerialPortClass),
994
    .class_init = virtio_serial_port_class_init,
995 996
};

A
Andreas Färber 已提交
997
static void virtio_serial_register_types(void)
998 999 1000 1001
{
    type_register_static(&virtio_serial_port_type_info);
}

A
Andreas Färber 已提交
1002
type_init(virtio_serial_register_types)