virtio-pci.c 36.3 KB
Newer Older
P
Paul Brook 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13
/*
 * Virtio PCI Bindings
 *
 * Copyright IBM, Corp. 2007
 * Copyright (c) 2009 CodeSourcery
 *
 * Authors:
 *  Anthony Liguori   <aliguori@us.ibm.com>
 *  Paul Brook        <paul@codesourcery.com>
 *
 * This work is licensed under the terms of the GNU GPL, version 2.  See
 * the COPYING file in the top-level directory.
 *
14 15
 * Contributions after 2012-01-13 are licensed under the terms of the
 * GNU GPL, version 2 or (at your option) any later version.
P
Paul Brook 已提交
16 17 18 19 20
 */

#include <inttypes.h>

#include "virtio.h"
21 22
#include "virtio-blk.h"
#include "virtio-net.h"
23
#include "virtio-serial.h"
24
#include "virtio-scsi.h"
25
#include "pci/pci.h"
26
#include "qemu/error-report.h"
27 28
#include "pci/msi.h"
#include "pci/msix.h"
29
#include "loader.h"
30 31
#include "sysemu/kvm.h"
#include "sysemu/blockdev.h"
32
#include "virtio-pci.h"
33
#include "qemu/range.h"
P
Paul Brook 已提交
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62

/* from Linux's linux/virtio_pci.h */

/* A 32-bit r/o bitmask of the features supported by the host */
#define VIRTIO_PCI_HOST_FEATURES        0

/* A 32-bit r/w bitmask of features activated by the guest */
#define VIRTIO_PCI_GUEST_FEATURES       4

/* A 32-bit r/w PFN for the currently selected queue */
#define VIRTIO_PCI_QUEUE_PFN            8

/* A 16-bit r/o queue size for the currently selected queue */
#define VIRTIO_PCI_QUEUE_NUM            12

/* A 16-bit r/w queue selector */
#define VIRTIO_PCI_QUEUE_SEL            14

/* A 16-bit r/w queue notifier */
#define VIRTIO_PCI_QUEUE_NOTIFY         16

/* An 8-bit device status register.  */
#define VIRTIO_PCI_STATUS               18

/* An 8-bit r/o interrupt status register.  Reading the value will return the
 * current contents of the ISR and will also clear it.  This is effectively
 * a read-and-acknowledge. */
#define VIRTIO_PCI_ISR                  19

63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
/* MSI-X registers: only enabled if MSI-X is enabled. */
/* A 16-bit vector for configuration changes. */
#define VIRTIO_MSI_CONFIG_VECTOR        20
/* A 16-bit vector for selected queue notifications. */
#define VIRTIO_MSI_QUEUE_VECTOR         22

/* Config space size */
#define VIRTIO_PCI_CONFIG_NOMSI         20
#define VIRTIO_PCI_CONFIG_MSI           24
#define VIRTIO_PCI_REGION_SIZE(dev)     (msix_present(dev) ? \
                                         VIRTIO_PCI_CONFIG_MSI : \
                                         VIRTIO_PCI_CONFIG_NOMSI)

/* The remaining space is defined by each driver as the per-driver
 * configuration space */
#define VIRTIO_PCI_CONFIG(dev)          (msix_enabled(dev) ? \
                                         VIRTIO_PCI_CONFIG_MSI : \
                                         VIRTIO_PCI_CONFIG_NOMSI)
P
Paul Brook 已提交
81 82 83 84 85

/* How many bits to shift physical queue address written to QUEUE_PFN.
 * 12 is historical, and due to x86 page size. */
#define VIRTIO_PCI_QUEUE_ADDR_SHIFT    12

86 87
/* Flags track per-device state like workarounds for quirks in older guests. */
#define VIRTIO_PCI_FLAG_BUS_MASTER_BUG  (1 << 0)
88

P
Paul Brook 已提交
89 90 91 92 93 94
/* QEMU doesn't strictly need write barriers since everything runs in
 * lock-step.  We'll leave the calls to wmb() in though to make it obvious for
 * KVM or if kqemu gets SMP support.
 */
#define wmb() do { } while (0)

95 96 97
/* HACK for virtio to determine if it's running a big endian guest */
bool virtio_is_big_endian(void);

P
Paul Brook 已提交
98
/* virtio device */
99 100 101 102 103
/* DeviceState to VirtIOPCIProxy. For use off data-path. TODO: use QOM. */
static inline VirtIOPCIProxy *to_virtio_pci_proxy(DeviceState *d)
{
    return container_of(d, VirtIOPCIProxy, pci_dev.qdev);
}
P
Paul Brook 已提交
104

105 106 107 108
/* DeviceState to VirtIOPCIProxy. Note: used on datapath,
 * be careful and test performance if you change this.
 */
static inline VirtIOPCIProxy *to_virtio_pci_proxy_fast(DeviceState *d)
P
Paul Brook 已提交
109
{
110 111 112 113 114 115
    return container_of(d, VirtIOPCIProxy, pci_dev.qdev);
}

static void virtio_pci_notify(DeviceState *d, uint16_t vector)
{
    VirtIOPCIProxy *proxy = to_virtio_pci_proxy_fast(d);
116 117 118 119
    if (msix_enabled(&proxy->pci_dev))
        msix_notify(&proxy->pci_dev, vector);
    else
        qemu_set_irq(proxy->pci_dev.irq[0], proxy->vdev->isr & 1);
P
Paul Brook 已提交
120 121
}

122
static void virtio_pci_save_config(DeviceState *d, QEMUFile *f)
123
{
124
    VirtIOPCIProxy *proxy = to_virtio_pci_proxy(d);
125 126 127 128 129 130
    pci_device_save(&proxy->pci_dev, f);
    msix_save(&proxy->pci_dev, f);
    if (msix_present(&proxy->pci_dev))
        qemu_put_be16(f, proxy->vdev->config_vector);
}

131
static void virtio_pci_save_queue(DeviceState *d, int n, QEMUFile *f)
132
{
133
    VirtIOPCIProxy *proxy = to_virtio_pci_proxy(d);
134 135 136 137
    if (msix_present(&proxy->pci_dev))
        qemu_put_be16(f, virtio_queue_vector(proxy->vdev, n));
}

138
static int virtio_pci_load_config(DeviceState *d, QEMUFile *f)
139
{
140
    VirtIOPCIProxy *proxy = to_virtio_pci_proxy(d);
141 142
    int ret;
    ret = pci_device_load(&proxy->pci_dev, f);
143
    if (ret) {
144
        return ret;
145
    }
146
    msix_unuse_all_vectors(&proxy->pci_dev);
147
    msix_load(&proxy->pci_dev, f);
148
    if (msix_present(&proxy->pci_dev)) {
149
        qemu_get_be16s(f, &proxy->vdev->config_vector);
150 151 152 153 154 155
    } else {
        proxy->vdev->config_vector = VIRTIO_NO_VECTOR;
    }
    if (proxy->vdev->config_vector != VIRTIO_NO_VECTOR) {
        return msix_vector_use(&proxy->pci_dev, proxy->vdev->config_vector);
    }
156 157 158
    return 0;
}

159
static int virtio_pci_load_queue(DeviceState *d, int n, QEMUFile *f)
160
{
161
    VirtIOPCIProxy *proxy = to_virtio_pci_proxy(d);
162
    uint16_t vector;
163 164 165 166 167
    if (msix_present(&proxy->pci_dev)) {
        qemu_get_be16s(f, &vector);
    } else {
        vector = VIRTIO_NO_VECTOR;
    }
168
    virtio_queue_set_vector(proxy->vdev, n, vector);
169 170 171
    if (vector != VIRTIO_NO_VECTOR) {
        return msix_vector_use(&proxy->pci_dev, vector);
    }
172 173 174
    return 0;
}

175
static int virtio_pci_set_host_notifier_internal(VirtIOPCIProxy *proxy,
P
Paolo Bonzini 已提交
176
                                                 int n, bool assign, bool set_handler)
177 178 179
{
    VirtQueue *vq = virtio_get_queue(proxy->vdev, n);
    EventNotifier *notifier = virtio_queue_get_host_notifier(vq);
A
Avi Kivity 已提交
180 181
    int r = 0;

182 183 184
    if (assign) {
        r = event_notifier_init(notifier, 1);
        if (r < 0) {
185 186
            error_report("%s: unable to init event notifier: %d",
                         __func__, r);
187 188
            return r;
        }
P
Paolo Bonzini 已提交
189
        virtio_queue_set_host_notifier_fd_handler(vq, true, set_handler);
A
Avi Kivity 已提交
190
        memory_region_add_eventfd(&proxy->bar, VIRTIO_PCI_QUEUE_NOTIFY, 2,
191
                                  true, n, notifier);
192
    } else {
A
Avi Kivity 已提交
193
        memory_region_del_eventfd(&proxy->bar, VIRTIO_PCI_QUEUE_NOTIFY, 2,
194
                                  true, n, notifier);
P
Paolo Bonzini 已提交
195
        virtio_queue_set_host_notifier_fd_handler(vq, false, false);
196 197 198 199 200
        event_notifier_cleanup(notifier);
    }
    return r;
}

201
static void virtio_pci_start_ioeventfd(VirtIOPCIProxy *proxy)
202 203 204 205 206 207
{
    int n, r;

    if (!(proxy->flags & VIRTIO_PCI_FLAG_USE_IOEVENTFD) ||
        proxy->ioeventfd_disabled ||
        proxy->ioeventfd_started) {
208
        return;
209 210 211 212 213 214 215
    }

    for (n = 0; n < VIRTIO_PCI_QUEUE_MAX; n++) {
        if (!virtio_queue_get_num(proxy->vdev, n)) {
            continue;
        }

P
Paolo Bonzini 已提交
216
        r = virtio_pci_set_host_notifier_internal(proxy, n, true, true);
217 218 219 220 221
        if (r < 0) {
            goto assign_error;
        }
    }
    proxy->ioeventfd_started = true;
222
    return;
223 224 225 226 227 228 229

assign_error:
    while (--n >= 0) {
        if (!virtio_queue_get_num(proxy->vdev, n)) {
            continue;
        }

P
Paolo Bonzini 已提交
230
        r = virtio_pci_set_host_notifier_internal(proxy, n, false, false);
231
        assert(r >= 0);
232 233
    }
    proxy->ioeventfd_started = false;
234
    error_report("%s: failed. Fallback to a userspace (slower).", __func__);
235 236
}

237
static void virtio_pci_stop_ioeventfd(VirtIOPCIProxy *proxy)
238
{
239
    int r;
240 241 242
    int n;

    if (!proxy->ioeventfd_started) {
243
        return;
244 245 246 247 248 249 250
    }

    for (n = 0; n < VIRTIO_PCI_QUEUE_MAX; n++) {
        if (!virtio_queue_get_num(proxy->vdev, n)) {
            continue;
        }

P
Paolo Bonzini 已提交
251
        r = virtio_pci_set_host_notifier_internal(proxy, n, false, false);
252
        assert(r >= 0);
253 254 255 256
    }
    proxy->ioeventfd_started = false;
}

257
void virtio_pci_reset(DeviceState *d)
258
{
259
    VirtIOPCIProxy *proxy = to_virtio_pci_proxy(d);
260
    virtio_pci_stop_ioeventfd(proxy);
261
    virtio_reset(proxy->vdev);
262
    msix_unuse_all_vectors(&proxy->pci_dev);
263
    proxy->flags &= ~VIRTIO_PCI_FLAG_BUS_MASTER_BUG;
264 265
}

P
Paul Brook 已提交
266 267 268 269
static void virtio_ioport_write(void *opaque, uint32_t addr, uint32_t val)
{
    VirtIOPCIProxy *proxy = opaque;
    VirtIODevice *vdev = proxy->vdev;
A
Avi Kivity 已提交
270
    hwaddr pa;
P
Paul Brook 已提交
271 272 273 274 275

    switch (addr) {
    case VIRTIO_PCI_GUEST_FEATURES:
	/* Guest does not negotiate properly?  We have to assume nothing. */
	if (val & (1 << VIRTIO_F_BAD_FEATURE)) {
276
            val = vdev->bad_features ? vdev->bad_features(vdev) : 0;
P
Paul Brook 已提交
277
	}
278
        virtio_set_features(vdev, val);
P
Paul Brook 已提交
279 280
        break;
    case VIRTIO_PCI_QUEUE_PFN:
A
Avi Kivity 已提交
281
        pa = (hwaddr)val << VIRTIO_PCI_QUEUE_ADDR_SHIFT;
282
        if (pa == 0) {
283
            virtio_pci_stop_ioeventfd(proxy);
284 285 286
            virtio_reset(proxy->vdev);
            msix_unuse_all_vectors(&proxy->pci_dev);
        }
287 288
        else
            virtio_queue_set_addr(vdev, vdev->queue_sel, pa);
P
Paul Brook 已提交
289 290 291 292 293 294
        break;
    case VIRTIO_PCI_QUEUE_SEL:
        if (val < VIRTIO_PCI_QUEUE_MAX)
            vdev->queue_sel = val;
        break;
    case VIRTIO_PCI_QUEUE_NOTIFY:
295 296 297
        if (val < VIRTIO_PCI_QUEUE_MAX) {
            virtio_queue_notify(vdev, val);
        }
P
Paul Brook 已提交
298 299
        break;
    case VIRTIO_PCI_STATUS:
300 301 302 303
        if (!(val & VIRTIO_CONFIG_S_DRIVER_OK)) {
            virtio_pci_stop_ioeventfd(proxy);
        }

304
        virtio_set_status(vdev, val & 0xFF);
305 306 307 308 309

        if (val & VIRTIO_CONFIG_S_DRIVER_OK) {
            virtio_pci_start_ioeventfd(proxy);
        }

310 311 312 313
        if (vdev->status == 0) {
            virtio_reset(proxy->vdev);
            msix_unuse_all_vectors(&proxy->pci_dev);
        }
314 315 316 317 318 319

        /* Linux before 2.6.34 sets the device as OK without enabling
           the PCI device bus master bit. In this case we need to disable
           some safety checks. */
        if ((val & VIRTIO_CONFIG_S_DRIVER_OK) &&
            !(proxy->pci_dev.config[PCI_COMMAND] & PCI_COMMAND_MASTER)) {
320
            proxy->flags |= VIRTIO_PCI_FLAG_BUS_MASTER_BUG;
321
        }
P
Paul Brook 已提交
322
        break;
323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338
    case VIRTIO_MSI_CONFIG_VECTOR:
        msix_vector_unuse(&proxy->pci_dev, vdev->config_vector);
        /* Make it possible for guest to discover an error took place. */
        if (msix_vector_use(&proxy->pci_dev, val) < 0)
            val = VIRTIO_NO_VECTOR;
        vdev->config_vector = val;
        break;
    case VIRTIO_MSI_QUEUE_VECTOR:
        msix_vector_unuse(&proxy->pci_dev,
                          virtio_queue_vector(vdev, vdev->queue_sel));
        /* Make it possible for guest to discover an error took place. */
        if (msix_vector_use(&proxy->pci_dev, val) < 0)
            val = VIRTIO_NO_VECTOR;
        virtio_queue_set_vector(vdev, vdev->queue_sel, val);
        break;
    default:
339 340
        error_report("%s: unexpected address 0x%x value 0x%x",
                     __func__, addr, val);
341
        break;
P
Paul Brook 已提交
342 343 344
    }
}

345
static uint32_t virtio_ioport_read(VirtIOPCIProxy *proxy, uint32_t addr)
P
Paul Brook 已提交
346 347 348 349 350 351
{
    VirtIODevice *vdev = proxy->vdev;
    uint32_t ret = 0xFFFFFFFF;

    switch (addr) {
    case VIRTIO_PCI_HOST_FEATURES:
352
        ret = proxy->host_features;
P
Paul Brook 已提交
353 354
        break;
    case VIRTIO_PCI_GUEST_FEATURES:
355
        ret = vdev->guest_features;
P
Paul Brook 已提交
356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373
        break;
    case VIRTIO_PCI_QUEUE_PFN:
        ret = virtio_queue_get_addr(vdev, vdev->queue_sel)
              >> VIRTIO_PCI_QUEUE_ADDR_SHIFT;
        break;
    case VIRTIO_PCI_QUEUE_NUM:
        ret = virtio_queue_get_num(vdev, vdev->queue_sel);
        break;
    case VIRTIO_PCI_QUEUE_SEL:
        ret = vdev->queue_sel;
        break;
    case VIRTIO_PCI_STATUS:
        ret = vdev->status;
        break;
    case VIRTIO_PCI_ISR:
        /* reading from the ISR also clears it. */
        ret = vdev->isr;
        vdev->isr = 0;
374
        qemu_set_irq(proxy->pci_dev.irq[0], 0);
P
Paul Brook 已提交
375
        break;
376 377 378 379 380 381
    case VIRTIO_MSI_CONFIG_VECTOR:
        ret = vdev->config_vector;
        break;
    case VIRTIO_MSI_QUEUE_VECTOR:
        ret = virtio_queue_vector(vdev, vdev->queue_sel);
        break;
P
Paul Brook 已提交
382 383 384 385 386 387 388
    default:
        break;
    }

    return ret;
}

389 390
static uint64_t virtio_pci_config_read(void *opaque, hwaddr addr,
                                       unsigned size)
P
Paul Brook 已提交
391 392
{
    VirtIOPCIProxy *proxy = opaque;
393
    uint32_t config = VIRTIO_PCI_CONFIG(&proxy->pci_dev);
394
    uint64_t val = 0;
395
    if (addr < config) {
396
        return virtio_ioport_read(proxy, addr);
397 398
    }
    addr -= config;
P
Paul Brook 已提交
399

400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415
    switch (size) {
    case 1:
        val = virtio_config_readb(proxy->vdev, addr);
        break;
    case 2:
        val = virtio_config_readw(proxy->vdev, addr);
        if (virtio_is_big_endian()) {
            val = bswap16(val);
        }
        break;
    case 4:
        val = virtio_config_readl(proxy->vdev, addr);
        if (virtio_is_big_endian()) {
            val = bswap32(val);
        }
        break;
416
    }
417
    return val;
P
Paul Brook 已提交
418 419
}

420 421
static void virtio_pci_config_write(void *opaque, hwaddr addr,
                                    uint64_t val, unsigned size)
P
Paul Brook 已提交
422 423
{
    VirtIOPCIProxy *proxy = opaque;
424 425 426 427 428 429
    uint32_t config = VIRTIO_PCI_CONFIG(&proxy->pci_dev);
    if (addr < config) {
        virtio_ioport_write(proxy, addr, val);
        return;
    }
    addr -= config;
430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449
    /*
     * Virtio-PCI is odd. Ioports are LE but config space is target native
     * endian.
     */
    switch (size) {
    case 1:
        virtio_config_writeb(proxy->vdev, addr, val);
        break;
    case 2:
        if (virtio_is_big_endian()) {
            val = bswap16(val);
        }
        virtio_config_writew(proxy->vdev, addr, val);
        break;
    case 4:
        if (virtio_is_big_endian()) {
            val = bswap32(val);
        }
        virtio_config_writel(proxy->vdev, addr, val);
        break;
450
    }
P
Paul Brook 已提交
451 452
}

A
Avi Kivity 已提交
453
static const MemoryRegionOps virtio_pci_config_ops = {
454 455 456 457 458 459
    .read = virtio_pci_config_read,
    .write = virtio_pci_config_write,
    .impl = {
        .min_access_size = 1,
        .max_access_size = 4,
    },
A
Avi Kivity 已提交
460 461
    .endianness = DEVICE_LITTLE_ENDIAN,
};
462 463 464 465

static void virtio_write_config(PCIDevice *pci_dev, uint32_t address,
                                uint32_t val, int len)
{
466 467
    VirtIOPCIProxy *proxy = DO_UPCAST(VirtIOPCIProxy, pci_dev, pci_dev);

468 469 470 471 472 473 474 475
    pci_default_write_config(pci_dev, address, val, len);

    if (range_covers_byte(address, len, PCI_COMMAND) &&
        !(pci_dev->config[PCI_COMMAND] & PCI_COMMAND_MASTER) &&
        !(proxy->flags & VIRTIO_PCI_FLAG_BUS_MASTER_BUG)) {
        virtio_pci_stop_ioeventfd(proxy);
        virtio_set_status(proxy->vdev,
                          proxy->vdev->status & ~VIRTIO_CONFIG_S_DRIVER_OK);
476
    }
P
Paul Brook 已提交
477 478
}

479
static unsigned virtio_pci_get_features(DeviceState *d)
480
{
481
    VirtIOPCIProxy *proxy = to_virtio_pci_proxy(d);
482
    return proxy->host_features;
483 484
}

485 486 487 488 489 490
static int kvm_virtio_pci_vq_vector_use(VirtIOPCIProxy *proxy,
                                        unsigned int queue_no,
                                        unsigned int vector,
                                        MSIMessage msg)
{
    VirtQueue *vq = virtio_get_queue(proxy->vdev, queue_no);
491
    EventNotifier *n = virtio_queue_get_guest_notifier(vq);
492
    VirtIOIRQFD *irqfd = &proxy->vector_irqfd[vector];
493
    int ret;
494 495 496 497 498 499 500 501 502 503

    if (irqfd->users == 0) {
        ret = kvm_irqchip_add_msi_route(kvm_state, msg);
        if (ret < 0) {
            return ret;
        }
        irqfd->virq = ret;
    }
    irqfd->users++;

J
Jan Kiszka 已提交
504
    ret = kvm_irqchip_add_irqfd_notifier(kvm_state, n, irqfd->virq);
505 506 507 508 509 510 511 512 513 514 515 516 517 518
    if (ret < 0) {
        if (--irqfd->users == 0) {
            kvm_irqchip_release_virq(kvm_state, irqfd->virq);
        }
        return ret;
    }
    return 0;
}

static void kvm_virtio_pci_vq_vector_release(VirtIOPCIProxy *proxy,
                                             unsigned int queue_no,
                                             unsigned int vector)
{
    VirtQueue *vq = virtio_get_queue(proxy->vdev, queue_no);
519
    EventNotifier *n = virtio_queue_get_guest_notifier(vq);
520
    VirtIOIRQFD *irqfd = &proxy->vector_irqfd[vector];
521
    int ret;
522

J
Jan Kiszka 已提交
523
    ret = kvm_irqchip_remove_irqfd_notifier(kvm_state, n, irqfd->virq);
524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578
    assert(ret == 0);

    if (--irqfd->users == 0) {
        kvm_irqchip_release_virq(kvm_state, irqfd->virq);
    }
}

static int kvm_virtio_pci_vector_use(PCIDevice *dev, unsigned vector,
                                     MSIMessage msg)
{
    VirtIOPCIProxy *proxy = container_of(dev, VirtIOPCIProxy, pci_dev);
    VirtIODevice *vdev = proxy->vdev;
    int ret, queue_no;

    for (queue_no = 0; queue_no < VIRTIO_PCI_QUEUE_MAX; queue_no++) {
        if (!virtio_queue_get_num(vdev, queue_no)) {
            break;
        }
        if (virtio_queue_vector(vdev, queue_no) != vector) {
            continue;
        }
        ret = kvm_virtio_pci_vq_vector_use(proxy, queue_no, vector, msg);
        if (ret < 0) {
            goto undo;
        }
    }
    return 0;

undo:
    while (--queue_no >= 0) {
        if (virtio_queue_vector(vdev, queue_no) != vector) {
            continue;
        }
        kvm_virtio_pci_vq_vector_release(proxy, queue_no, vector);
    }
    return ret;
}

static void kvm_virtio_pci_vector_release(PCIDevice *dev, unsigned vector)
{
    VirtIOPCIProxy *proxy = container_of(dev, VirtIOPCIProxy, pci_dev);
    VirtIODevice *vdev = proxy->vdev;
    int queue_no;

    for (queue_no = 0; queue_no < VIRTIO_PCI_QUEUE_MAX; queue_no++) {
        if (!virtio_queue_get_num(vdev, queue_no)) {
            break;
        }
        if (virtio_queue_vector(vdev, queue_no) != vector) {
            continue;
        }
        kvm_virtio_pci_vq_vector_release(proxy, queue_no, vector);
    }
}

579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608
static void kvm_virtio_pci_vector_poll(PCIDevice *dev,
                                       unsigned int vector_start,
                                       unsigned int vector_end)
{
    VirtIOPCIProxy *proxy = container_of(dev, VirtIOPCIProxy, pci_dev);
    VirtIODevice *vdev = proxy->vdev;
    int queue_no;
    unsigned int vector;
    EventNotifier *notifier;
    VirtQueue *vq;

    for (queue_no = 0; queue_no < VIRTIO_PCI_QUEUE_MAX; queue_no++) {
        if (!virtio_queue_get_num(vdev, queue_no)) {
            break;
        }
        vector = virtio_queue_vector(vdev, queue_no);
        if (vector < vector_start || vector >= vector_end ||
            !msix_is_masked(dev, vector)) {
            continue;
        }
        vq = virtio_get_queue(vdev, queue_no);
        notifier = virtio_queue_get_guest_notifier(vq);
        if (event_notifier_test_and_clear(notifier)) {
            msix_set_pending(dev, vector);
        }
    }
}

static int virtio_pci_set_guest_notifier(DeviceState *d, int n, bool assign,
                                         bool with_irqfd)
609
{
610
    VirtIOPCIProxy *proxy = to_virtio_pci_proxy(d);
611 612 613 614 615 616 617 618
    VirtQueue *vq = virtio_get_queue(proxy->vdev, n);
    EventNotifier *notifier = virtio_queue_get_guest_notifier(vq);

    if (assign) {
        int r = event_notifier_init(notifier, 0);
        if (r < 0) {
            return r;
        }
619
        virtio_queue_set_guest_notifier_fd_handler(vq, true, with_irqfd);
620
    } else {
621
        virtio_queue_set_guest_notifier_fd_handler(vq, false, with_irqfd);
622 623 624 625 626 627
        event_notifier_cleanup(notifier);
    }

    return 0;
}

628
static bool virtio_pci_query_guest_notifiers(DeviceState *d)
629
{
630
    VirtIOPCIProxy *proxy = to_virtio_pci_proxy(d);
631 632 633
    return msix_enabled(&proxy->pci_dev);
}

634
static int virtio_pci_set_guest_notifiers(DeviceState *d, bool assign)
635
{
636
    VirtIOPCIProxy *proxy = to_virtio_pci_proxy(d);
637 638
    VirtIODevice *vdev = proxy->vdev;
    int r, n;
639 640
    bool with_irqfd = msix_enabled(&proxy->pci_dev) &&
        kvm_msi_via_irqfd_enabled();
641

642
    /* Must unset vector notifier while guest notifier is still assigned */
643
    if (proxy->vector_irqfd && !assign) {
644 645 646 647 648
        msix_unset_vector_notifiers(&proxy->pci_dev);
        g_free(proxy->vector_irqfd);
        proxy->vector_irqfd = NULL;
    }

649 650 651 652 653
    for (n = 0; n < VIRTIO_PCI_QUEUE_MAX; n++) {
        if (!virtio_queue_get_num(vdev, n)) {
            break;
        }

654 655
        r = virtio_pci_set_guest_notifier(d, n, assign,
                                          kvm_msi_via_irqfd_enabled());
656 657 658 659 660
        if (r < 0) {
            goto assign_error;
        }
    }

661
    /* Must set vector notifier after guest notifier has been assigned */
662
    if (with_irqfd && assign) {
663 664 665 666 667
        proxy->vector_irqfd =
            g_malloc0(sizeof(*proxy->vector_irqfd) *
                      msix_nr_vectors_allocated(&proxy->pci_dev));
        r = msix_set_vector_notifiers(&proxy->pci_dev,
                                      kvm_virtio_pci_vector_use,
668
                                      kvm_virtio_pci_vector_release,
669
                                      kvm_virtio_pci_vector_poll);
670 671 672 673 674
        if (r < 0) {
            goto assign_error;
        }
    }

675 676 677 678
    return 0;

assign_error:
    /* We get here on assignment failure. Recover by undoing for VQs 0 .. n. */
679
    assert(assign);
680
    while (--n >= 0) {
681
        virtio_pci_set_guest_notifier(d, n, !assign, with_irqfd);
682 683 684 685
    }
    return r;
}

686
static int virtio_pci_set_host_notifier(DeviceState *d, int n, bool assign)
687
{
688
    VirtIOPCIProxy *proxy = to_virtio_pci_proxy(d);
689 690 691 692 693

    /* Stop using ioeventfd for virtqueue kick if the device starts using host
     * notifiers.  This makes it easy to avoid stepping on each others' toes.
     */
    proxy->ioeventfd_disabled = assign;
694
    if (assign) {
695 696 697 698 699 700
        virtio_pci_stop_ioeventfd(proxy);
    }
    /* We don't need to start here: it's not needed because backend
     * currently only stops on status change away from ok,
     * reset, vmstop and such. If we do add code to start here,
     * need to check vmstate, device state etc. */
P
Paolo Bonzini 已提交
701
    return virtio_pci_set_host_notifier_internal(proxy, n, assign, false);
702 703
}

704
static void virtio_pci_vmstate_change(DeviceState *d, bool running)
705
{
706
    VirtIOPCIProxy *proxy = to_virtio_pci_proxy(d);
707 708

    if (running) {
709 710 711 712 713 714
        /* Try to find out if the guest has bus master disabled, but is
           in ready state. Then we have a buggy guest OS. */
        if ((proxy->vdev->status & VIRTIO_CONFIG_S_DRIVER_OK) &&
            !(proxy->pci_dev.config[PCI_COMMAND] & PCI_COMMAND_MASTER)) {
            proxy->flags |= VIRTIO_PCI_FLAG_BUS_MASTER_BUG;
        }
715
        virtio_pci_start_ioeventfd(proxy);
716
    } else {
717
        virtio_pci_stop_ioeventfd(proxy);
718 719 720
    }
}

P
Paul Brook 已提交
721
static const VirtIOBindings virtio_pci_bindings = {
722 723 724 725 726
    .notify = virtio_pci_notify,
    .save_config = virtio_pci_save_config,
    .load_config = virtio_pci_load_config,
    .save_queue = virtio_pci_save_queue,
    .load_queue = virtio_pci_load_queue,
727
    .get_features = virtio_pci_get_features,
728
    .query_guest_notifiers = virtio_pci_query_guest_notifiers,
729
    .set_host_notifier = virtio_pci_set_host_notifier,
730
    .set_guest_notifiers = virtio_pci_set_guest_notifiers,
731
    .vmstate_change = virtio_pci_vmstate_change,
P
Paul Brook 已提交
732 733
};

734
void virtio_init_pci(VirtIOPCIProxy *proxy, VirtIODevice *vdev)
P
Paul Brook 已提交
735 736 737 738 739 740 741 742
{
    uint8_t *config;
    uint32_t size;

    proxy->vdev = vdev;

    config = proxy->pci_dev.config;

743 744 745
    if (proxy->class_code) {
        pci_config_set_class(config, proxy->class_code);
    }
H
Hui Kai Ran 已提交
746 747 748 749
    pci_set_word(config + PCI_SUBSYSTEM_VENDOR_ID,
                 pci_get_word(config + PCI_VENDOR_ID));
    pci_set_word(config + PCI_SUBSYSTEM_ID, vdev->device_id);
    config[PCI_INTERRUPT_PIN] = 1;
P
Paul Brook 已提交
750

751 752
    if (vdev->nvectors &&
        msix_init_exclusive_bar(&proxy->pci_dev, vdev->nvectors, 1)) {
753
        vdev->nvectors = 0;
754
    }
755

756 757
    proxy->pci_dev.config_write = virtio_write_config;

758
    size = VIRTIO_PCI_REGION_SIZE(&proxy->pci_dev) + vdev->config_len;
P
Paul Brook 已提交
759 760 761
    if (size & (size-1))
        size = 1 << qemu_fls(size);

A
Avi Kivity 已提交
762 763
    memory_region_init_io(&proxy->bar, &virtio_pci_config_ops, proxy,
                          "virtio-pci", size);
764 765
    pci_register_bar(&proxy->pci_dev, 0, PCI_BASE_ADDRESS_SPACE_IO,
                     &proxy->bar);
P
Paul Brook 已提交
766

767 768 769 770
    if (!kvm_has_many_ioeventfds()) {
        proxy->flags &= ~VIRTIO_PCI_FLAG_USE_IOEVENTFD;
    }

771
    virtio_bind_device(vdev, &virtio_pci_bindings, DEVICE(proxy));
772 773 774
    proxy->host_features |= 0x1 << VIRTIO_F_NOTIFY_ON_EMPTY;
    proxy->host_features |= 0x1 << VIRTIO_F_BAD_FEATURE;
    proxy->host_features = vdev->get_features(vdev, proxy->host_features);
P
Paul Brook 已提交
775 776
}

777
static int virtio_blk_init_pci(PCIDevice *pci_dev)
P
Paul Brook 已提交
778 779 780 781
{
    VirtIOPCIProxy *proxy = DO_UPCAST(VirtIOPCIProxy, pci_dev, pci_dev);
    VirtIODevice *vdev;

782 783 784
    if (proxy->class_code != PCI_CLASS_STORAGE_SCSI &&
        proxy->class_code != PCI_CLASS_STORAGE_OTHER)
        proxy->class_code = PCI_CLASS_STORAGE_SCSI;
P
Paul Brook 已提交
785

P
Paolo Bonzini 已提交
786
    vdev = virtio_blk_init(&pci_dev->qdev, &proxy->blk);
787 788 789
    if (!vdev) {
        return -1;
    }
G
Gerd Hoffmann 已提交
790
    vdev->nvectors = proxy->nvectors;
791
    virtio_init_pci(proxy, vdev);
G
Gerd Hoffmann 已提交
792 793
    /* make the actual value visible */
    proxy->nvectors = vdev->nvectors;
794
    return 0;
795 796
}

797
static void virtio_exit_pci(PCIDevice *pci_dev)
798
{
A
Avi Kivity 已提交
799 800 801
    VirtIOPCIProxy *proxy = DO_UPCAST(VirtIOPCIProxy, pci_dev, pci_dev);

    memory_region_destroy(&proxy->bar);
802
    msix_uninit_exclusive_bar(pci_dev);
803 804
}

805
static void virtio_blk_exit_pci(PCIDevice *pci_dev)
G
Gerd Hoffmann 已提交
806 807 808
{
    VirtIOPCIProxy *proxy = DO_UPCAST(VirtIOPCIProxy, pci_dev, pci_dev);

809
    virtio_pci_stop_ioeventfd(proxy);
810
    virtio_blk_exit(proxy->vdev);
811
    virtio_exit_pci(pci_dev);
G
Gerd Hoffmann 已提交
812 813
}

814
static int virtio_serial_init_pci(PCIDevice *pci_dev)
815
{
816
    VirtIOPCIProxy *proxy = DO_UPCAST(VirtIOPCIProxy, pci_dev, pci_dev);
817 818
    VirtIODevice *vdev;

819 820 821 822 823
    if (proxy->class_code != PCI_CLASS_COMMUNICATION_OTHER &&
        proxy->class_code != PCI_CLASS_DISPLAY_OTHER && /* qemu 0.10 */
        proxy->class_code != PCI_CLASS_OTHERS)          /* qemu-kvm  */
        proxy->class_code = PCI_CLASS_COMMUNICATION_OTHER;

824
    vdev = virtio_serial_init(&pci_dev->qdev, &proxy->serial);
825 826 827
    if (!vdev) {
        return -1;
    }
828
    vdev->nvectors = proxy->nvectors == DEV_NVECTORS_UNSPECIFIED
829
                                        ? proxy->serial.max_virtserial_ports + 1
830
                                        : proxy->nvectors;
831
    virtio_init_pci(proxy, vdev);
832
    proxy->nvectors = vdev->nvectors;
833
    return 0;
P
Paul Brook 已提交
834 835
}

836
static void virtio_serial_exit_pci(PCIDevice *pci_dev)
837 838 839
{
    VirtIOPCIProxy *proxy = DO_UPCAST(VirtIOPCIProxy, pci_dev, pci_dev);

A
Amit Shah 已提交
840
    virtio_pci_stop_ioeventfd(proxy);
841
    virtio_serial_exit(proxy->vdev);
842
    virtio_exit_pci(pci_dev);
843 844
}

845
static int virtio_net_init_pci(PCIDevice *pci_dev)
P
Paul Brook 已提交
846 847 848 849
{
    VirtIOPCIProxy *proxy = DO_UPCAST(VirtIOPCIProxy, pci_dev, pci_dev);
    VirtIODevice *vdev;

850
    vdev = virtio_net_init(&pci_dev->qdev, &proxy->nic, &proxy->net);
851

852
    vdev->nvectors = proxy->nvectors;
853
    virtio_init_pci(proxy, vdev);
854 855 856

    /* make the actual value visible */
    proxy->nvectors = vdev->nvectors;
857
    return 0;
P
Paul Brook 已提交
858 859
}

860
static void virtio_net_exit_pci(PCIDevice *pci_dev)
861 862 863
{
    VirtIOPCIProxy *proxy = DO_UPCAST(VirtIOPCIProxy, pci_dev, pci_dev);

864
    virtio_pci_stop_ioeventfd(proxy);
865
    virtio_net_exit(proxy->vdev);
866
    virtio_exit_pci(pci_dev);
867 868
}

869
static int virtio_balloon_init_pci(PCIDevice *pci_dev)
P
Paul Brook 已提交
870 871 872 873
{
    VirtIOPCIProxy *proxy = DO_UPCAST(VirtIOPCIProxy, pci_dev, pci_dev);
    VirtIODevice *vdev;

874 875 876 877 878
    if (proxy->class_code != PCI_CLASS_OTHERS &&
        proxy->class_code != PCI_CLASS_MEMORY_RAM) { /* qemu < 1.1 */
        proxy->class_code = PCI_CLASS_OTHERS;
    }

P
Paul Brook 已提交
879
    vdev = virtio_balloon_init(&pci_dev->qdev);
880 881 882
    if (!vdev) {
        return -1;
    }
883
    virtio_init_pci(proxy, vdev);
884
    return 0;
P
Paul Brook 已提交
885 886
}

887
static void virtio_balloon_exit_pci(PCIDevice *pci_dev)
888 889 890 891 892
{
    VirtIOPCIProxy *proxy = DO_UPCAST(VirtIOPCIProxy, pci_dev, pci_dev);

    virtio_pci_stop_ioeventfd(proxy);
    virtio_balloon_exit(proxy->vdev);
893
    virtio_exit_pci(pci_dev);
894 895
}

896 897 898 899 900
static int virtio_rng_init_pci(PCIDevice *pci_dev)
{
    VirtIOPCIProxy *proxy = DO_UPCAST(VirtIOPCIProxy, pci_dev, pci_dev);
    VirtIODevice *vdev;

901 902 903 904 905 906 907 908 909 910 911 912 913
    if (proxy->rng.rng == NULL) {
        proxy->rng.default_backend = RNG_RANDOM(object_new(TYPE_RNG_RANDOM));

        object_property_add_child(OBJECT(pci_dev),
                                  "default-backend",
                                  OBJECT(proxy->rng.default_backend),
                                  NULL);

        object_property_set_link(OBJECT(pci_dev),
                                 OBJECT(proxy->rng.default_backend),
                                 "rng", NULL);
    }

914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930
    vdev = virtio_rng_init(&pci_dev->qdev, &proxy->rng);
    if (!vdev) {
        return -1;
    }
    virtio_init_pci(proxy, vdev);
    return 0;
}

static void virtio_rng_exit_pci(PCIDevice *pci_dev)
{
    VirtIOPCIProxy *proxy = DO_UPCAST(VirtIOPCIProxy, pci_dev, pci_dev);

    virtio_pci_stop_ioeventfd(proxy);
    virtio_rng_exit(proxy->vdev);
    virtio_exit_pci(pci_dev);
}

931 932
static Property virtio_blk_properties[] = {
    DEFINE_PROP_HEX32("class", VirtIOPCIProxy, class_code, 0),
P
Paolo Bonzini 已提交
933
    DEFINE_BLOCK_PROPERTIES(VirtIOPCIProxy, blk.conf),
934
    DEFINE_BLOCK_CHS_PROPERTIES(VirtIOPCIProxy, blk.conf),
P
Paolo Bonzini 已提交
935
    DEFINE_PROP_STRING("serial", VirtIOPCIProxy, blk.serial),
936 937 938
#ifdef __linux__
    DEFINE_PROP_BIT("scsi", VirtIOPCIProxy, blk.scsi, 0, true),
#endif
939 940 941 942
    DEFINE_PROP_BIT("ioeventfd", VirtIOPCIProxy, flags, VIRTIO_PCI_FLAG_USE_IOEVENTFD_BIT, true),
    DEFINE_PROP_UINT32("vectors", VirtIOPCIProxy, nvectors, 2),
    DEFINE_VIRTIO_BLK_FEATURES(VirtIOPCIProxy, host_features),
    DEFINE_PROP_END_OF_LIST(),
943 944
};

945 946
static void virtio_blk_class_init(ObjectClass *klass, void *data)
{
947
    DeviceClass *dc = DEVICE_CLASS(klass);
948 949 950 951 952 953 954 955
    PCIDeviceClass *k = PCI_DEVICE_CLASS(klass);

    k->init = virtio_blk_init_pci;
    k->exit = virtio_blk_exit_pci;
    k->vendor_id = PCI_VENDOR_ID_REDHAT_QUMRANET;
    k->device_id = PCI_DEVICE_ID_VIRTIO_BLOCK;
    k->revision = VIRTIO_PCI_ABI_VERSION;
    k->class_id = PCI_CLASS_STORAGE_SCSI;
956 957
    dc->reset = virtio_pci_reset;
    dc->props = virtio_blk_properties;
958 959
}

960 961 962 963 964
static TypeInfo virtio_blk_info = {
    .name          = "virtio-blk-pci",
    .parent        = TYPE_PCI_DEVICE,
    .instance_size = sizeof(VirtIOPCIProxy),
    .class_init    = virtio_blk_class_init,
965 966 967 968 969 970 971 972 973 974 975 976 977 978 979
};

static Property virtio_net_properties[] = {
    DEFINE_PROP_BIT("ioeventfd", VirtIOPCIProxy, flags, VIRTIO_PCI_FLAG_USE_IOEVENTFD_BIT, false),
    DEFINE_PROP_UINT32("vectors", VirtIOPCIProxy, nvectors, 3),
    DEFINE_VIRTIO_NET_FEATURES(VirtIOPCIProxy, host_features),
    DEFINE_NIC_PROPERTIES(VirtIOPCIProxy, nic),
    DEFINE_PROP_UINT32("x-txtimer", VirtIOPCIProxy, net.txtimer, TX_TIMER_INTERVAL),
    DEFINE_PROP_INT32("x-txburst", VirtIOPCIProxy, net.txburst, TX_BURST),
    DEFINE_PROP_STRING("tx", VirtIOPCIProxy, net.tx),
    DEFINE_PROP_END_OF_LIST(),
};

static void virtio_net_class_init(ObjectClass *klass, void *data)
{
980
    DeviceClass *dc = DEVICE_CLASS(klass);
981 982 983 984 985 986 987 988 989
    PCIDeviceClass *k = PCI_DEVICE_CLASS(klass);

    k->init = virtio_net_init_pci;
    k->exit = virtio_net_exit_pci;
    k->romfile = "pxe-virtio.rom";
    k->vendor_id = PCI_VENDOR_ID_REDHAT_QUMRANET;
    k->device_id = PCI_DEVICE_ID_VIRTIO_NET;
    k->revision = VIRTIO_PCI_ABI_VERSION;
    k->class_id = PCI_CLASS_NETWORK_ETHERNET;
990 991
    dc->reset = virtio_pci_reset;
    dc->props = virtio_net_properties;
992 993
}

994 995 996 997 998
static TypeInfo virtio_net_info = {
    .name          = "virtio-net-pci",
    .parent        = TYPE_PCI_DEVICE,
    .instance_size = sizeof(VirtIOPCIProxy),
    .class_init    = virtio_net_class_init,
999 1000
};

1001 1002 1003 1004 1005 1006 1007
static Property virtio_serial_properties[] = {
    DEFINE_PROP_BIT("ioeventfd", VirtIOPCIProxy, flags, VIRTIO_PCI_FLAG_USE_IOEVENTFD_BIT, true),
    DEFINE_PROP_UINT32("vectors", VirtIOPCIProxy, nvectors, DEV_NVECTORS_UNSPECIFIED),
    DEFINE_PROP_HEX32("class", VirtIOPCIProxy, class_code, 0),
    DEFINE_VIRTIO_COMMON_FEATURES(VirtIOPCIProxy, host_features),
    DEFINE_PROP_UINT32("max_ports", VirtIOPCIProxy, serial.max_virtserial_ports, 31),
    DEFINE_PROP_END_OF_LIST(),
1008 1009
};

1010 1011
static void virtio_serial_class_init(ObjectClass *klass, void *data)
{
1012
    DeviceClass *dc = DEVICE_CLASS(klass);
1013 1014 1015 1016 1017 1018 1019 1020
    PCIDeviceClass *k = PCI_DEVICE_CLASS(klass);

    k->init = virtio_serial_init_pci;
    k->exit = virtio_serial_exit_pci;
    k->vendor_id = PCI_VENDOR_ID_REDHAT_QUMRANET;
    k->device_id = PCI_DEVICE_ID_VIRTIO_CONSOLE;
    k->revision = VIRTIO_PCI_ABI_VERSION;
    k->class_id = PCI_CLASS_COMMUNICATION_OTHER;
1021 1022
    dc->reset = virtio_pci_reset;
    dc->props = virtio_serial_properties;
1023 1024
}

1025 1026 1027 1028 1029
static TypeInfo virtio_serial_info = {
    .name          = "virtio-serial-pci",
    .parent        = TYPE_PCI_DEVICE,
    .instance_size = sizeof(VirtIOPCIProxy),
    .class_init    = virtio_serial_class_init,
1030 1031 1032 1033
};

static Property virtio_balloon_properties[] = {
    DEFINE_VIRTIO_COMMON_FEATURES(VirtIOPCIProxy, host_features),
1034
    DEFINE_PROP_HEX32("class", VirtIOPCIProxy, class_code, 0),
1035 1036 1037 1038 1039
    DEFINE_PROP_END_OF_LIST(),
};

static void virtio_balloon_class_init(ObjectClass *klass, void *data)
{
1040
    DeviceClass *dc = DEVICE_CLASS(klass);
1041 1042 1043 1044 1045 1046 1047
    PCIDeviceClass *k = PCI_DEVICE_CLASS(klass);

    k->init = virtio_balloon_init_pci;
    k->exit = virtio_balloon_exit_pci;
    k->vendor_id = PCI_VENDOR_ID_REDHAT_QUMRANET;
    k->device_id = PCI_DEVICE_ID_VIRTIO_BALLOON;
    k->revision = VIRTIO_PCI_ABI_VERSION;
1048
    k->class_id = PCI_CLASS_OTHERS;
1049 1050
    dc->reset = virtio_pci_reset;
    dc->props = virtio_balloon_properties;
1051 1052
}

1053 1054 1055 1056 1057
static TypeInfo virtio_balloon_info = {
    .name          = "virtio-balloon-pci",
    .parent        = TYPE_PCI_DEVICE,
    .instance_size = sizeof(VirtIOPCIProxy),
    .class_init    = virtio_balloon_class_init,
1058 1059
};

1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070
static void virtio_rng_initfn(Object *obj)
{
    PCIDevice *pci_dev = PCI_DEVICE(obj);
    VirtIOPCIProxy *proxy = DO_UPCAST(VirtIOPCIProxy, pci_dev, pci_dev);

    object_property_add_link(obj, "rng", TYPE_RNG_BACKEND,
                             (Object **)&proxy->rng.rng, NULL);
}

static Property virtio_rng_properties[] = {
    DEFINE_VIRTIO_COMMON_FEATURES(VirtIOPCIProxy, host_features),
1071 1072 1073 1074 1075 1076 1077
    /* Set a default rate limit of 2^47 bytes per minute or roughly 2TB/s.  If
       you have an entropy source capable of generating more entropy than this
       and you can pass it through via virtio-rng, then hats off to you.  Until
       then, this is unlimited for all practical purposes.
    */
    DEFINE_PROP_UINT64("max-bytes", VirtIOPCIProxy, rng.max_bytes, INT64_MAX),
    DEFINE_PROP_UINT32("period", VirtIOPCIProxy, rng.period_ms, 1 << 16),
1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103
    DEFINE_PROP_END_OF_LIST(),
};

static void virtio_rng_class_init(ObjectClass *klass, void *data)
{
    DeviceClass *dc = DEVICE_CLASS(klass);
    PCIDeviceClass *k = PCI_DEVICE_CLASS(klass);

    k->init = virtio_rng_init_pci;
    k->exit = virtio_rng_exit_pci;
    k->vendor_id = PCI_VENDOR_ID_REDHAT_QUMRANET;
    k->device_id = PCI_DEVICE_ID_VIRTIO_RNG;
    k->revision = VIRTIO_PCI_ABI_VERSION;
    k->class_id = PCI_CLASS_OTHERS;
    dc->reset = virtio_pci_reset;
    dc->props = virtio_rng_properties;
}

static TypeInfo virtio_rng_info = {
    .name          = "virtio-rng-pci",
    .parent        = TYPE_PCI_DEVICE,
    .instance_size = sizeof(VirtIOPCIProxy),
    .instance_init = virtio_rng_initfn,
    .class_init    = virtio_rng_class_init,
};

1104 1105 1106 1107 1108 1109 1110 1111 1112 1113
static int virtio_scsi_init_pci(PCIDevice *pci_dev)
{
    VirtIOPCIProxy *proxy = DO_UPCAST(VirtIOPCIProxy, pci_dev, pci_dev);
    VirtIODevice *vdev;

    vdev = virtio_scsi_init(&pci_dev->qdev, &proxy->scsi);
    if (!vdev) {
        return -EINVAL;
    }

1114 1115 1116
    vdev->nvectors = proxy->nvectors == DEV_NVECTORS_UNSPECIFIED
                                        ? proxy->scsi.num_queues + 3
                                        : proxy->nvectors;
1117 1118 1119 1120 1121 1122 1123
    virtio_init_pci(proxy, vdev);

    /* make the actual value visible */
    proxy->nvectors = vdev->nvectors;
    return 0;
}

1124
static void virtio_scsi_exit_pci(PCIDevice *pci_dev)
1125 1126 1127 1128
{
    VirtIOPCIProxy *proxy = DO_UPCAST(VirtIOPCIProxy, pci_dev, pci_dev);

    virtio_scsi_exit(proxy->vdev);
1129
    virtio_exit_pci(pci_dev);
1130 1131 1132
}

static Property virtio_scsi_properties[] = {
1133
    DEFINE_PROP_BIT("ioeventfd", VirtIOPCIProxy, flags, VIRTIO_PCI_FLAG_USE_IOEVENTFD_BIT, true),
1134
    DEFINE_PROP_UINT32("vectors", VirtIOPCIProxy, nvectors, DEV_NVECTORS_UNSPECIFIED),
1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160
    DEFINE_VIRTIO_SCSI_PROPERTIES(VirtIOPCIProxy, host_features, scsi),
    DEFINE_PROP_END_OF_LIST(),
};

static void virtio_scsi_class_init(ObjectClass *klass, void *data)
{
    DeviceClass *dc = DEVICE_CLASS(klass);
    PCIDeviceClass *k = PCI_DEVICE_CLASS(klass);

    k->init = virtio_scsi_init_pci;
    k->exit = virtio_scsi_exit_pci;
    k->vendor_id = PCI_VENDOR_ID_REDHAT_QUMRANET;
    k->device_id = PCI_DEVICE_ID_VIRTIO_SCSI;
    k->revision = 0x00;
    k->class_id = PCI_CLASS_STORAGE_SCSI;
    dc->reset = virtio_pci_reset;
    dc->props = virtio_scsi_properties;
}

static TypeInfo virtio_scsi_info = {
    .name          = "virtio-scsi-pci",
    .parent        = TYPE_PCI_DEVICE,
    .instance_size = sizeof(VirtIOPCIProxy),
    .class_init    = virtio_scsi_class_init,
};

A
Andreas Färber 已提交
1161
static void virtio_pci_register_types(void)
P
Paul Brook 已提交
1162
{
1163 1164 1165 1166
    type_register_static(&virtio_blk_info);
    type_register_static(&virtio_net_info);
    type_register_static(&virtio_serial_info);
    type_register_static(&virtio_balloon_info);
1167
    type_register_static(&virtio_scsi_info);
1168
    type_register_static(&virtio_rng_info);
P
Paul Brook 已提交
1169 1170
}

A
Andreas Färber 已提交
1171
type_init(virtio_pci_register_types)