~ubuntu-branches/ubuntu/trusty/qemu/trusty

« back to all changes in this revision

Viewing changes to .pc/ubuntu/CVE-2013-4377.patch/hw/virtio/virtio-pci.c

  • Committer: Package Import Robot
  • Author(s): Serge Hallyn
  • Date: 2014-02-04 12:13:08 UTC
  • mfrom: (10.1.45 sid)
  • Revision ID: package-import@ubuntu.com-20140204121308-1xq92lrfs75agw2g
Tags: 1.7.0+dfsg-3ubuntu1~ppa1
* Merge 1.7.0+dfsg-3 from debian.  Remaining changes:
  - debian/patches/ubuntu:
    * expose-vmx_qemu64cpu.patch
    * linaro (omap3) and arm64 patches
    * ubuntu/target-ppc-add-stubs-for-kvm-breakpoints: fix FTBFS
      on ppc
    * ubuntu/CVE-2013-4377.patch: fix denial of service via virtio
  - debian/qemu-system-x86.modprobe: set kvm_intel nested=1 options
  - debian/control:
    * add arm64 to Architectures
    * add qemu-common and qemu-system-aarch64 packages
  - debian/qemu-system-common.install: add debian/tmp/usr/lib
  - debian/qemu-system-common.preinst: add kvm group
  - debian/qemu-system-common.postinst: remove acl placed by udev,
    and add udevadm trigger.
  - qemu-system-x86.links: add eepro100.rom, remove pxe-virtio,
    pxe-e1000 and pxe-rtl8139.
  - add qemu-system-x86.qemu-kvm.upstart and .default
  - qemu-user-static.postinst-in: remove arm64 binfmt
  - debian/rules:
    * allow parallel build
    * add aarch64 to system_targets and sys_systems
    * add qemu-kvm-spice links
    * install qemu-system-x86.modprobe
  - add debian/qemu-system-common.links for OVMF.fd link
* Remove kvm-img, kvm-nbd, kvm-ifup and kvm-ifdown symlinks.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Virtio PCI Bindings
 
3
 *
 
4
 * Copyright IBM, Corp. 2007
 
5
 * Copyright (c) 2009 CodeSourcery
 
6
 *
 
7
 * Authors:
 
8
 *  Anthony Liguori   <aliguori@us.ibm.com>
 
9
 *  Paul Brook        <paul@codesourcery.com>
 
10
 *
 
11
 * This work is licensed under the terms of the GNU GPL, version 2.  See
 
12
 * the COPYING file in the top-level directory.
 
13
 *
 
14
 * Contributions after 2012-01-13 are licensed under the terms of the
 
15
 * GNU GPL, version 2 or (at your option) any later version.
 
16
 */
 
17
 
 
18
#include <inttypes.h>
 
19
 
 
20
#include "hw/virtio/virtio.h"
 
21
#include "hw/virtio/virtio-blk.h"
 
22
#include "hw/virtio/virtio-net.h"
 
23
#include "hw/virtio/virtio-serial.h"
 
24
#include "hw/virtio/virtio-scsi.h"
 
25
#include "hw/virtio/virtio-balloon.h"
 
26
#include "hw/pci/pci.h"
 
27
#include "qemu/error-report.h"
 
28
#include "hw/pci/msi.h"
 
29
#include "hw/pci/msix.h"
 
30
#include "hw/loader.h"
 
31
#include "sysemu/kvm.h"
 
32
#include "sysemu/blockdev.h"
 
33
#include "virtio-pci.h"
 
34
#include "qemu/range.h"
 
35
#include "hw/virtio/virtio-bus.h"
 
36
#include "qapi/visitor.h"
 
37
 
 
38
/* from Linux's linux/virtio_pci.h */
 
39
 
 
40
/* A 32-bit r/o bitmask of the features supported by the host */
 
41
#define VIRTIO_PCI_HOST_FEATURES        0
 
42
 
 
43
/* A 32-bit r/w bitmask of features activated by the guest */
 
44
#define VIRTIO_PCI_GUEST_FEATURES       4
 
45
 
 
46
/* A 32-bit r/w PFN for the currently selected queue */
 
47
#define VIRTIO_PCI_QUEUE_PFN            8
 
48
 
 
49
/* A 16-bit r/o queue size for the currently selected queue */
 
50
#define VIRTIO_PCI_QUEUE_NUM            12
 
51
 
 
52
/* A 16-bit r/w queue selector */
 
53
#define VIRTIO_PCI_QUEUE_SEL            14
 
54
 
 
55
/* A 16-bit r/w queue notifier */
 
56
#define VIRTIO_PCI_QUEUE_NOTIFY         16
 
57
 
 
58
/* An 8-bit device status register.  */
 
59
#define VIRTIO_PCI_STATUS               18
 
60
 
 
61
/* An 8-bit r/o interrupt status register.  Reading the value will return the
 
62
 * current contents of the ISR and will also clear it.  This is effectively
 
63
 * a read-and-acknowledge. */
 
64
#define VIRTIO_PCI_ISR                  19
 
65
 
 
66
/* MSI-X registers: only enabled if MSI-X is enabled. */
 
67
/* A 16-bit vector for configuration changes. */
 
68
#define VIRTIO_MSI_CONFIG_VECTOR        20
 
69
/* A 16-bit vector for selected queue notifications. */
 
70
#define VIRTIO_MSI_QUEUE_VECTOR         22
 
71
 
 
72
/* Config space size */
 
73
#define VIRTIO_PCI_CONFIG_NOMSI         20
 
74
#define VIRTIO_PCI_CONFIG_MSI           24
 
75
#define VIRTIO_PCI_REGION_SIZE(dev)     (msix_present(dev) ? \
 
76
                                         VIRTIO_PCI_CONFIG_MSI : \
 
77
                                         VIRTIO_PCI_CONFIG_NOMSI)
 
78
 
 
79
/* The remaining space is defined by each driver as the per-driver
 
80
 * configuration space */
 
81
#define VIRTIO_PCI_CONFIG(dev)          (msix_enabled(dev) ? \
 
82
                                         VIRTIO_PCI_CONFIG_MSI : \
 
83
                                         VIRTIO_PCI_CONFIG_NOMSI)
 
84
 
 
85
/* How many bits to shift physical queue address written to QUEUE_PFN.
 
86
 * 12 is historical, and due to x86 page size. */
 
87
#define VIRTIO_PCI_QUEUE_ADDR_SHIFT    12
 
88
 
 
89
/* Flags track per-device state like workarounds for quirks in older guests. */
 
90
#define VIRTIO_PCI_FLAG_BUS_MASTER_BUG  (1 << 0)
 
91
 
 
92
/* HACK for virtio to determine if it's running a big endian guest */
 
93
bool virtio_is_big_endian(void);
 
94
 
 
95
static void virtio_pci_bus_new(VirtioBusState *bus, size_t bus_size,
 
96
                               VirtIOPCIProxy *dev);
 
97
 
 
98
/* virtio device */
 
99
/* DeviceState to VirtIOPCIProxy. For use off data-path. TODO: use QOM. */
 
100
static inline VirtIOPCIProxy *to_virtio_pci_proxy(DeviceState *d)
 
101
{
 
102
    return container_of(d, VirtIOPCIProxy, pci_dev.qdev);
 
103
}
 
104
 
 
105
/* DeviceState to VirtIOPCIProxy. Note: used on datapath,
 
106
 * be careful and test performance if you change this.
 
107
 */
 
108
static inline VirtIOPCIProxy *to_virtio_pci_proxy_fast(DeviceState *d)
 
109
{
 
110
    return container_of(d, VirtIOPCIProxy, pci_dev.qdev);
 
111
}
 
112
 
 
113
static void virtio_pci_notify(DeviceState *d, uint16_t vector)
 
114
{
 
115
    VirtIOPCIProxy *proxy = to_virtio_pci_proxy_fast(d);
 
116
    if (msix_enabled(&proxy->pci_dev))
 
117
        msix_notify(&proxy->pci_dev, vector);
 
118
    else
 
119
        pci_set_irq(&proxy->pci_dev, proxy->vdev->isr & 1);
 
120
}
 
121
 
 
122
static void virtio_pci_save_config(DeviceState *d, QEMUFile *f)
 
123
{
 
124
    VirtIOPCIProxy *proxy = to_virtio_pci_proxy(d);
 
125
    pci_device_save(&proxy->pci_dev, f);
 
126
    msix_save(&proxy->pci_dev, f);
 
127
    if (msix_present(&proxy->pci_dev))
 
128
        qemu_put_be16(f, proxy->vdev->config_vector);
 
129
}
 
130
 
 
131
static void virtio_pci_save_queue(DeviceState *d, int n, QEMUFile *f)
 
132
{
 
133
    VirtIOPCIProxy *proxy = to_virtio_pci_proxy(d);
 
134
    if (msix_present(&proxy->pci_dev))
 
135
        qemu_put_be16(f, virtio_queue_vector(proxy->vdev, n));
 
136
}
 
137
 
 
138
static int virtio_pci_load_config(DeviceState *d, QEMUFile *f)
 
139
{
 
140
    VirtIOPCIProxy *proxy = to_virtio_pci_proxy(d);
 
141
    int ret;
 
142
    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
    } else {
 
151
        proxy->vdev->config_vector = VIRTIO_NO_VECTOR;
 
152
    }
 
153
    if (proxy->vdev->config_vector != VIRTIO_NO_VECTOR) {
 
154
        return msix_vector_use(&proxy->pci_dev, proxy->vdev->config_vector);
 
155
    }
 
156
    return 0;
 
157
}
 
158
 
 
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
    if (msix_present(&proxy->pci_dev)) {
 
164
        qemu_get_be16s(f, &vector);
 
165
    } else {
 
166
        vector = VIRTIO_NO_VECTOR;
 
167
    }
 
168
    virtio_queue_set_vector(proxy->vdev, n, vector);
 
169
    if (vector != VIRTIO_NO_VECTOR) {
 
170
        return msix_vector_use(&proxy->pci_dev, vector);
 
171
    }
 
172
    return 0;
 
173
}
 
174
 
 
175
static int virtio_pci_set_host_notifier_internal(VirtIOPCIProxy *proxy,
 
176
                                                 int n, bool assign, bool set_handler)
 
177
{
 
178
    VirtQueue *vq = virtio_get_queue(proxy->vdev, n);
 
179
    EventNotifier *notifier = virtio_queue_get_host_notifier(vq);
 
180
    int r = 0;
 
181
 
 
182
    if (assign) {
 
183
        r = event_notifier_init(notifier, 1);
 
184
        if (r < 0) {
 
185
            error_report("%s: unable to init event notifier: %d",
 
186
                         __func__, r);
 
187
            return r;
 
188
        }
 
189
        virtio_queue_set_host_notifier_fd_handler(vq, true, set_handler);
 
190
        memory_region_add_eventfd(&proxy->bar, VIRTIO_PCI_QUEUE_NOTIFY, 2,
 
191
                                  true, n, notifier);
 
192
    } else {
 
193
        memory_region_del_eventfd(&proxy->bar, VIRTIO_PCI_QUEUE_NOTIFY, 2,
 
194
                                  true, n, notifier);
 
195
        virtio_queue_set_host_notifier_fd_handler(vq, false, false);
 
196
        event_notifier_cleanup(notifier);
 
197
    }
 
198
    return r;
 
199
}
 
200
 
 
201
static void virtio_pci_start_ioeventfd(VirtIOPCIProxy *proxy)
 
202
{
 
203
    int n, r;
 
204
 
 
205
    if (!(proxy->flags & VIRTIO_PCI_FLAG_USE_IOEVENTFD) ||
 
206
        proxy->ioeventfd_disabled ||
 
207
        proxy->ioeventfd_started) {
 
208
        return;
 
209
    }
 
210
 
 
211
    for (n = 0; n < VIRTIO_PCI_QUEUE_MAX; n++) {
 
212
        if (!virtio_queue_get_num(proxy->vdev, n)) {
 
213
            continue;
 
214
        }
 
215
 
 
216
        r = virtio_pci_set_host_notifier_internal(proxy, n, true, true);
 
217
        if (r < 0) {
 
218
            goto assign_error;
 
219
        }
 
220
    }
 
221
    proxy->ioeventfd_started = true;
 
222
    return;
 
223
 
 
224
assign_error:
 
225
    while (--n >= 0) {
 
226
        if (!virtio_queue_get_num(proxy->vdev, n)) {
 
227
            continue;
 
228
        }
 
229
 
 
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
    int n;
 
241
 
 
242
    if (!proxy->ioeventfd_started) {
 
243
        return;
 
244
    }
 
245
 
 
246
    for (n = 0; n < VIRTIO_PCI_QUEUE_MAX; n++) {
 
247
        if (!virtio_queue_get_num(proxy->vdev, n)) {
 
248
            continue;
 
249
        }
 
250
 
 
251
        r = virtio_pci_set_host_notifier_internal(proxy, n, false, false);
 
252
        assert(r >= 0);
 
253
    }
 
254
    proxy->ioeventfd_started = false;
 
255
}
 
256
 
 
257
static void virtio_ioport_write(void *opaque, uint32_t addr, uint32_t val)
 
258
{
 
259
    VirtIOPCIProxy *proxy = opaque;
 
260
    VirtIODevice *vdev = proxy->vdev;
 
261
    hwaddr pa;
 
262
 
 
263
    switch (addr) {
 
264
    case VIRTIO_PCI_GUEST_FEATURES:
 
265
        /* Guest does not negotiate properly?  We have to assume nothing. */
 
266
        if (val & (1 << VIRTIO_F_BAD_FEATURE)) {
 
267
            val = virtio_bus_get_vdev_bad_features(&proxy->bus);
 
268
        }
 
269
        virtio_set_features(vdev, val);
 
270
        break;
 
271
    case VIRTIO_PCI_QUEUE_PFN:
 
272
        pa = (hwaddr)val << VIRTIO_PCI_QUEUE_ADDR_SHIFT;
 
273
        if (pa == 0) {
 
274
            virtio_pci_stop_ioeventfd(proxy);
 
275
            virtio_reset(proxy->vdev);
 
276
            msix_unuse_all_vectors(&proxy->pci_dev);
 
277
        }
 
278
        else
 
279
            virtio_queue_set_addr(vdev, vdev->queue_sel, pa);
 
280
        break;
 
281
    case VIRTIO_PCI_QUEUE_SEL:
 
282
        if (val < VIRTIO_PCI_QUEUE_MAX)
 
283
            vdev->queue_sel = val;
 
284
        break;
 
285
    case VIRTIO_PCI_QUEUE_NOTIFY:
 
286
        if (val < VIRTIO_PCI_QUEUE_MAX) {
 
287
            virtio_queue_notify(vdev, val);
 
288
        }
 
289
        break;
 
290
    case VIRTIO_PCI_STATUS:
 
291
        if (!(val & VIRTIO_CONFIG_S_DRIVER_OK)) {
 
292
            virtio_pci_stop_ioeventfd(proxy);
 
293
        }
 
294
 
 
295
        virtio_set_status(vdev, val & 0xFF);
 
296
 
 
297
        if (val & VIRTIO_CONFIG_S_DRIVER_OK) {
 
298
            virtio_pci_start_ioeventfd(proxy);
 
299
        }
 
300
 
 
301
        if (vdev->status == 0) {
 
302
            virtio_reset(proxy->vdev);
 
303
            msix_unuse_all_vectors(&proxy->pci_dev);
 
304
        }
 
305
 
 
306
        /* Linux before 2.6.34 sets the device as OK without enabling
 
307
           the PCI device bus master bit. In this case we need to disable
 
308
           some safety checks. */
 
309
        if ((val & VIRTIO_CONFIG_S_DRIVER_OK) &&
 
310
            !(proxy->pci_dev.config[PCI_COMMAND] & PCI_COMMAND_MASTER)) {
 
311
            proxy->flags |= VIRTIO_PCI_FLAG_BUS_MASTER_BUG;
 
312
        }
 
313
        break;
 
314
    case VIRTIO_MSI_CONFIG_VECTOR:
 
315
        msix_vector_unuse(&proxy->pci_dev, vdev->config_vector);
 
316
        /* Make it possible for guest to discover an error took place. */
 
317
        if (msix_vector_use(&proxy->pci_dev, val) < 0)
 
318
            val = VIRTIO_NO_VECTOR;
 
319
        vdev->config_vector = val;
 
320
        break;
 
321
    case VIRTIO_MSI_QUEUE_VECTOR:
 
322
        msix_vector_unuse(&proxy->pci_dev,
 
323
                          virtio_queue_vector(vdev, vdev->queue_sel));
 
324
        /* Make it possible for guest to discover an error took place. */
 
325
        if (msix_vector_use(&proxy->pci_dev, val) < 0)
 
326
            val = VIRTIO_NO_VECTOR;
 
327
        virtio_queue_set_vector(vdev, vdev->queue_sel, val);
 
328
        break;
 
329
    default:
 
330
        error_report("%s: unexpected address 0x%x value 0x%x",
 
331
                     __func__, addr, val);
 
332
        break;
 
333
    }
 
334
}
 
335
 
 
336
static uint32_t virtio_ioport_read(VirtIOPCIProxy *proxy, uint32_t addr)
 
337
{
 
338
    VirtIODevice *vdev = proxy->vdev;
 
339
    uint32_t ret = 0xFFFFFFFF;
 
340
 
 
341
    switch (addr) {
 
342
    case VIRTIO_PCI_HOST_FEATURES:
 
343
        ret = proxy->host_features;
 
344
        break;
 
345
    case VIRTIO_PCI_GUEST_FEATURES:
 
346
        ret = vdev->guest_features;
 
347
        break;
 
348
    case VIRTIO_PCI_QUEUE_PFN:
 
349
        ret = virtio_queue_get_addr(vdev, vdev->queue_sel)
 
350
              >> VIRTIO_PCI_QUEUE_ADDR_SHIFT;
 
351
        break;
 
352
    case VIRTIO_PCI_QUEUE_NUM:
 
353
        ret = virtio_queue_get_num(vdev, vdev->queue_sel);
 
354
        break;
 
355
    case VIRTIO_PCI_QUEUE_SEL:
 
356
        ret = vdev->queue_sel;
 
357
        break;
 
358
    case VIRTIO_PCI_STATUS:
 
359
        ret = vdev->status;
 
360
        break;
 
361
    case VIRTIO_PCI_ISR:
 
362
        /* reading from the ISR also clears it. */
 
363
        ret = vdev->isr;
 
364
        vdev->isr = 0;
 
365
        pci_irq_deassert(&proxy->pci_dev);
 
366
        break;
 
367
    case VIRTIO_MSI_CONFIG_VECTOR:
 
368
        ret = vdev->config_vector;
 
369
        break;
 
370
    case VIRTIO_MSI_QUEUE_VECTOR:
 
371
        ret = virtio_queue_vector(vdev, vdev->queue_sel);
 
372
        break;
 
373
    default:
 
374
        break;
 
375
    }
 
376
 
 
377
    return ret;
 
378
}
 
379
 
 
380
static uint64_t virtio_pci_config_read(void *opaque, hwaddr addr,
 
381
                                       unsigned size)
 
382
{
 
383
    VirtIOPCIProxy *proxy = opaque;
 
384
    uint32_t config = VIRTIO_PCI_CONFIG(&proxy->pci_dev);
 
385
    uint64_t val = 0;
 
386
    if (addr < config) {
 
387
        return virtio_ioport_read(proxy, addr);
 
388
    }
 
389
    addr -= config;
 
390
 
 
391
    switch (size) {
 
392
    case 1:
 
393
        val = virtio_config_readb(proxy->vdev, addr);
 
394
        break;
 
395
    case 2:
 
396
        val = virtio_config_readw(proxy->vdev, addr);
 
397
        if (virtio_is_big_endian()) {
 
398
            val = bswap16(val);
 
399
        }
 
400
        break;
 
401
    case 4:
 
402
        val = virtio_config_readl(proxy->vdev, addr);
 
403
        if (virtio_is_big_endian()) {
 
404
            val = bswap32(val);
 
405
        }
 
406
        break;
 
407
    }
 
408
    return val;
 
409
}
 
410
 
 
411
static void virtio_pci_config_write(void *opaque, hwaddr addr,
 
412
                                    uint64_t val, unsigned size)
 
413
{
 
414
    VirtIOPCIProxy *proxy = opaque;
 
415
    uint32_t config = VIRTIO_PCI_CONFIG(&proxy->pci_dev);
 
416
    if (addr < config) {
 
417
        virtio_ioport_write(proxy, addr, val);
 
418
        return;
 
419
    }
 
420
    addr -= config;
 
421
    /*
 
422
     * Virtio-PCI is odd. Ioports are LE but config space is target native
 
423
     * endian.
 
424
     */
 
425
    switch (size) {
 
426
    case 1:
 
427
        virtio_config_writeb(proxy->vdev, addr, val);
 
428
        break;
 
429
    case 2:
 
430
        if (virtio_is_big_endian()) {
 
431
            val = bswap16(val);
 
432
        }
 
433
        virtio_config_writew(proxy->vdev, addr, val);
 
434
        break;
 
435
    case 4:
 
436
        if (virtio_is_big_endian()) {
 
437
            val = bswap32(val);
 
438
        }
 
439
        virtio_config_writel(proxy->vdev, addr, val);
 
440
        break;
 
441
    }
 
442
}
 
443
 
 
444
static const MemoryRegionOps virtio_pci_config_ops = {
 
445
    .read = virtio_pci_config_read,
 
446
    .write = virtio_pci_config_write,
 
447
    .impl = {
 
448
        .min_access_size = 1,
 
449
        .max_access_size = 4,
 
450
    },
 
451
    .endianness = DEVICE_LITTLE_ENDIAN,
 
452
};
 
453
 
 
454
static void virtio_write_config(PCIDevice *pci_dev, uint32_t address,
 
455
                                uint32_t val, int len)
 
456
{
 
457
    VirtIOPCIProxy *proxy = DO_UPCAST(VirtIOPCIProxy, pci_dev, pci_dev);
 
458
 
 
459
    pci_default_write_config(pci_dev, address, val, len);
 
460
 
 
461
    if (range_covers_byte(address, len, PCI_COMMAND) &&
 
462
        !(pci_dev->config[PCI_COMMAND] & PCI_COMMAND_MASTER) &&
 
463
        !(proxy->flags & VIRTIO_PCI_FLAG_BUS_MASTER_BUG)) {
 
464
        virtio_pci_stop_ioeventfd(proxy);
 
465
        virtio_set_status(proxy->vdev,
 
466
                          proxy->vdev->status & ~VIRTIO_CONFIG_S_DRIVER_OK);
 
467
    }
 
468
}
 
469
 
 
470
static unsigned virtio_pci_get_features(DeviceState *d)
 
471
{
 
472
    VirtIOPCIProxy *proxy = to_virtio_pci_proxy(d);
 
473
    return proxy->host_features;
 
474
}
 
475
 
 
476
static int kvm_virtio_pci_vq_vector_use(VirtIOPCIProxy *proxy,
 
477
                                        unsigned int queue_no,
 
478
                                        unsigned int vector,
 
479
                                        MSIMessage msg)
 
480
{
 
481
    VirtIOIRQFD *irqfd = &proxy->vector_irqfd[vector];
 
482
    int ret;
 
483
 
 
484
    if (irqfd->users == 0) {
 
485
        ret = kvm_irqchip_add_msi_route(kvm_state, msg);
 
486
        if (ret < 0) {
 
487
            return ret;
 
488
        }
 
489
        irqfd->virq = ret;
 
490
    }
 
491
    irqfd->users++;
 
492
    return 0;
 
493
}
 
494
 
 
495
static void kvm_virtio_pci_vq_vector_release(VirtIOPCIProxy *proxy,
 
496
                                             unsigned int vector)
 
497
{
 
498
    VirtIOIRQFD *irqfd = &proxy->vector_irqfd[vector];
 
499
    if (--irqfd->users == 0) {
 
500
        kvm_irqchip_release_virq(kvm_state, irqfd->virq);
 
501
    }
 
502
}
 
503
 
 
504
static int kvm_virtio_pci_irqfd_use(VirtIOPCIProxy *proxy,
 
505
                                 unsigned int queue_no,
 
506
                                 unsigned int vector)
 
507
{
 
508
    VirtIOIRQFD *irqfd = &proxy->vector_irqfd[vector];
 
509
    VirtQueue *vq = virtio_get_queue(proxy->vdev, queue_no);
 
510
    EventNotifier *n = virtio_queue_get_guest_notifier(vq);
 
511
    int ret;
 
512
    ret = kvm_irqchip_add_irqfd_notifier(kvm_state, n, NULL, irqfd->virq);
 
513
    return ret;
 
514
}
 
515
 
 
516
static void kvm_virtio_pci_irqfd_release(VirtIOPCIProxy *proxy,
 
517
                                      unsigned int queue_no,
 
518
                                      unsigned int vector)
 
519
{
 
520
    VirtQueue *vq = virtio_get_queue(proxy->vdev, queue_no);
 
521
    EventNotifier *n = virtio_queue_get_guest_notifier(vq);
 
522
    VirtIOIRQFD *irqfd = &proxy->vector_irqfd[vector];
 
523
    int ret;
 
524
 
 
525
    ret = kvm_irqchip_remove_irqfd_notifier(kvm_state, n, irqfd->virq);
 
526
    assert(ret == 0);
 
527
}
 
528
 
 
529
static int kvm_virtio_pci_vector_use(VirtIOPCIProxy *proxy, int nvqs)
 
530
{
 
531
    PCIDevice *dev = &proxy->pci_dev;
 
532
    VirtIODevice *vdev = proxy->vdev;
 
533
    VirtioDeviceClass *k = VIRTIO_DEVICE_GET_CLASS(vdev);
 
534
    unsigned int vector;
 
535
    int ret, queue_no;
 
536
    MSIMessage msg;
 
537
 
 
538
    for (queue_no = 0; queue_no < nvqs; queue_no++) {
 
539
        if (!virtio_queue_get_num(vdev, queue_no)) {
 
540
            break;
 
541
        }
 
542
        vector = virtio_queue_vector(vdev, queue_no);
 
543
        if (vector >= msix_nr_vectors_allocated(dev)) {
 
544
            continue;
 
545
        }
 
546
        msg = msix_get_message(dev, vector);
 
547
        ret = kvm_virtio_pci_vq_vector_use(proxy, queue_no, vector, msg);
 
548
        if (ret < 0) {
 
549
            goto undo;
 
550
        }
 
551
        /* If guest supports masking, set up irqfd now.
 
552
         * Otherwise, delay until unmasked in the frontend.
 
553
         */
 
554
        if (k->guest_notifier_mask) {
 
555
            ret = kvm_virtio_pci_irqfd_use(proxy, queue_no, vector);
 
556
            if (ret < 0) {
 
557
                kvm_virtio_pci_vq_vector_release(proxy, vector);
 
558
                goto undo;
 
559
            }
 
560
        }
 
561
    }
 
562
    return 0;
 
563
 
 
564
undo:
 
565
    while (--queue_no >= 0) {
 
566
        vector = virtio_queue_vector(vdev, queue_no);
 
567
        if (vector >= msix_nr_vectors_allocated(dev)) {
 
568
            continue;
 
569
        }
 
570
        if (k->guest_notifier_mask) {
 
571
            kvm_virtio_pci_irqfd_release(proxy, queue_no, vector);
 
572
        }
 
573
        kvm_virtio_pci_vq_vector_release(proxy, vector);
 
574
    }
 
575
    return ret;
 
576
}
 
577
 
 
578
static void kvm_virtio_pci_vector_release(VirtIOPCIProxy *proxy, int nvqs)
 
579
{
 
580
    PCIDevice *dev = &proxy->pci_dev;
 
581
    VirtIODevice *vdev = proxy->vdev;
 
582
    unsigned int vector;
 
583
    int queue_no;
 
584
    VirtioDeviceClass *k = VIRTIO_DEVICE_GET_CLASS(vdev);
 
585
 
 
586
    for (queue_no = 0; queue_no < nvqs; queue_no++) {
 
587
        if (!virtio_queue_get_num(vdev, queue_no)) {
 
588
            break;
 
589
        }
 
590
        vector = virtio_queue_vector(vdev, queue_no);
 
591
        if (vector >= msix_nr_vectors_allocated(dev)) {
 
592
            continue;
 
593
        }
 
594
        /* If guest supports masking, clean up irqfd now.
 
595
         * Otherwise, it was cleaned when masked in the frontend.
 
596
         */
 
597
        if (k->guest_notifier_mask) {
 
598
            kvm_virtio_pci_irqfd_release(proxy, queue_no, vector);
 
599
        }
 
600
        kvm_virtio_pci_vq_vector_release(proxy, vector);
 
601
    }
 
602
}
 
603
 
 
604
static int virtio_pci_vq_vector_unmask(VirtIOPCIProxy *proxy,
 
605
                                       unsigned int queue_no,
 
606
                                       unsigned int vector,
 
607
                                       MSIMessage msg)
 
608
{
 
609
    VirtioDeviceClass *k = VIRTIO_DEVICE_GET_CLASS(proxy->vdev);
 
610
    VirtQueue *vq = virtio_get_queue(proxy->vdev, queue_no);
 
611
    EventNotifier *n = virtio_queue_get_guest_notifier(vq);
 
612
    VirtIOIRQFD *irqfd;
 
613
    int ret = 0;
 
614
 
 
615
    if (proxy->vector_irqfd) {
 
616
        irqfd = &proxy->vector_irqfd[vector];
 
617
        if (irqfd->msg.data != msg.data || irqfd->msg.address != msg.address) {
 
618
            ret = kvm_irqchip_update_msi_route(kvm_state, irqfd->virq, msg);
 
619
            if (ret < 0) {
 
620
                return ret;
 
621
            }
 
622
        }
 
623
    }
 
624
 
 
625
    /* If guest supports masking, irqfd is already setup, unmask it.
 
626
     * Otherwise, set it up now.
 
627
     */
 
628
    if (k->guest_notifier_mask) {
 
629
        k->guest_notifier_mask(proxy->vdev, queue_no, false);
 
630
        /* Test after unmasking to avoid losing events. */
 
631
        if (k->guest_notifier_pending &&
 
632
            k->guest_notifier_pending(proxy->vdev, queue_no)) {
 
633
            event_notifier_set(n);
 
634
        }
 
635
    } else {
 
636
        ret = kvm_virtio_pci_irqfd_use(proxy, queue_no, vector);
 
637
    }
 
638
    return ret;
 
639
}
 
640
 
 
641
static void virtio_pci_vq_vector_mask(VirtIOPCIProxy *proxy,
 
642
                                             unsigned int queue_no,
 
643
                                             unsigned int vector)
 
644
{
 
645
    VirtioDeviceClass *k = VIRTIO_DEVICE_GET_CLASS(proxy->vdev);
 
646
 
 
647
    /* If guest supports masking, keep irqfd but mask it.
 
648
     * Otherwise, clean it up now.
 
649
     */ 
 
650
    if (k->guest_notifier_mask) {
 
651
        k->guest_notifier_mask(proxy->vdev, queue_no, true);
 
652
    } else {
 
653
        kvm_virtio_pci_irqfd_release(proxy, queue_no, vector);
 
654
    }
 
655
}
 
656
 
 
657
static int virtio_pci_vector_unmask(PCIDevice *dev, unsigned vector,
 
658
                                    MSIMessage msg)
 
659
{
 
660
    VirtIOPCIProxy *proxy = container_of(dev, VirtIOPCIProxy, pci_dev);
 
661
    VirtIODevice *vdev = proxy->vdev;
 
662
    int ret, queue_no;
 
663
 
 
664
    for (queue_no = 0; queue_no < proxy->nvqs_with_notifiers; queue_no++) {
 
665
        if (!virtio_queue_get_num(vdev, queue_no)) {
 
666
            break;
 
667
        }
 
668
        if (virtio_queue_vector(vdev, queue_no) != vector) {
 
669
            continue;
 
670
        }
 
671
        ret = virtio_pci_vq_vector_unmask(proxy, queue_no, vector, msg);
 
672
        if (ret < 0) {
 
673
            goto undo;
 
674
        }
 
675
    }
 
676
    return 0;
 
677
 
 
678
undo:
 
679
    while (--queue_no >= 0) {
 
680
        if (virtio_queue_vector(vdev, queue_no) != vector) {
 
681
            continue;
 
682
        }
 
683
        virtio_pci_vq_vector_mask(proxy, queue_no, vector);
 
684
    }
 
685
    return ret;
 
686
}
 
687
 
 
688
static void virtio_pci_vector_mask(PCIDevice *dev, unsigned vector)
 
689
{
 
690
    VirtIOPCIProxy *proxy = container_of(dev, VirtIOPCIProxy, pci_dev);
 
691
    VirtIODevice *vdev = proxy->vdev;
 
692
    int queue_no;
 
693
 
 
694
    for (queue_no = 0; queue_no < proxy->nvqs_with_notifiers; queue_no++) {
 
695
        if (!virtio_queue_get_num(vdev, queue_no)) {
 
696
            break;
 
697
        }
 
698
        if (virtio_queue_vector(vdev, queue_no) != vector) {
 
699
            continue;
 
700
        }
 
701
        virtio_pci_vq_vector_mask(proxy, queue_no, vector);
 
702
    }
 
703
}
 
704
 
 
705
static void virtio_pci_vector_poll(PCIDevice *dev,
 
706
                                   unsigned int vector_start,
 
707
                                   unsigned int vector_end)
 
708
{
 
709
    VirtIOPCIProxy *proxy = container_of(dev, VirtIOPCIProxy, pci_dev);
 
710
    VirtIODevice *vdev = proxy->vdev;
 
711
    VirtioDeviceClass *k = VIRTIO_DEVICE_GET_CLASS(vdev);
 
712
    int queue_no;
 
713
    unsigned int vector;
 
714
    EventNotifier *notifier;
 
715
    VirtQueue *vq;
 
716
 
 
717
    for (queue_no = 0; queue_no < proxy->nvqs_with_notifiers; queue_no++) {
 
718
        if (!virtio_queue_get_num(vdev, queue_no)) {
 
719
            break;
 
720
        }
 
721
        vector = virtio_queue_vector(vdev, queue_no);
 
722
        if (vector < vector_start || vector >= vector_end ||
 
723
            !msix_is_masked(dev, vector)) {
 
724
            continue;
 
725
        }
 
726
        vq = virtio_get_queue(vdev, queue_no);
 
727
        notifier = virtio_queue_get_guest_notifier(vq);
 
728
        if (k->guest_notifier_pending) {
 
729
            if (k->guest_notifier_pending(vdev, queue_no)) {
 
730
                msix_set_pending(dev, vector);
 
731
            }
 
732
        } else if (event_notifier_test_and_clear(notifier)) {
 
733
            msix_set_pending(dev, vector);
 
734
        }
 
735
    }
 
736
}
 
737
 
 
738
static int virtio_pci_set_guest_notifier(DeviceState *d, int n, bool assign,
 
739
                                         bool with_irqfd)
 
740
{
 
741
    VirtIOPCIProxy *proxy = to_virtio_pci_proxy(d);
 
742
    VirtioDeviceClass *vdc = VIRTIO_DEVICE_GET_CLASS(proxy->vdev);
 
743
    VirtQueue *vq = virtio_get_queue(proxy->vdev, n);
 
744
    EventNotifier *notifier = virtio_queue_get_guest_notifier(vq);
 
745
 
 
746
    if (assign) {
 
747
        int r = event_notifier_init(notifier, 0);
 
748
        if (r < 0) {
 
749
            return r;
 
750
        }
 
751
        virtio_queue_set_guest_notifier_fd_handler(vq, true, with_irqfd);
 
752
    } else {
 
753
        virtio_queue_set_guest_notifier_fd_handler(vq, false, with_irqfd);
 
754
        event_notifier_cleanup(notifier);
 
755
    }
 
756
 
 
757
    if (!msix_enabled(&proxy->pci_dev) && vdc->guest_notifier_mask) {
 
758
        vdc->guest_notifier_mask(proxy->vdev, n, !assign);
 
759
    }
 
760
 
 
761
    return 0;
 
762
}
 
763
 
 
764
static bool virtio_pci_query_guest_notifiers(DeviceState *d)
 
765
{
 
766
    VirtIOPCIProxy *proxy = to_virtio_pci_proxy(d);
 
767
    return msix_enabled(&proxy->pci_dev);
 
768
}
 
769
 
 
770
static int virtio_pci_set_guest_notifiers(DeviceState *d, int nvqs, bool assign)
 
771
{
 
772
    VirtIOPCIProxy *proxy = to_virtio_pci_proxy(d);
 
773
    VirtIODevice *vdev = proxy->vdev;
 
774
    VirtioDeviceClass *k = VIRTIO_DEVICE_GET_CLASS(vdev);
 
775
    int r, n;
 
776
    bool with_irqfd = msix_enabled(&proxy->pci_dev) &&
 
777
        kvm_msi_via_irqfd_enabled();
 
778
 
 
779
    nvqs = MIN(nvqs, VIRTIO_PCI_QUEUE_MAX);
 
780
 
 
781
    /* When deassigning, pass a consistent nvqs value
 
782
     * to avoid leaking notifiers.
 
783
     */
 
784
    assert(assign || nvqs == proxy->nvqs_with_notifiers);
 
785
 
 
786
    proxy->nvqs_with_notifiers = nvqs;
 
787
 
 
788
    /* Must unset vector notifier while guest notifier is still assigned */
 
789
    if ((proxy->vector_irqfd || k->guest_notifier_mask) && !assign) {
 
790
        msix_unset_vector_notifiers(&proxy->pci_dev);
 
791
        if (proxy->vector_irqfd) {
 
792
            kvm_virtio_pci_vector_release(proxy, nvqs);
 
793
            g_free(proxy->vector_irqfd);
 
794
            proxy->vector_irqfd = NULL;
 
795
        }
 
796
    }
 
797
 
 
798
    for (n = 0; n < nvqs; n++) {
 
799
        if (!virtio_queue_get_num(vdev, n)) {
 
800
            break;
 
801
        }
 
802
 
 
803
        r = virtio_pci_set_guest_notifier(d, n, assign, with_irqfd);
 
804
        if (r < 0) {
 
805
            goto assign_error;
 
806
        }
 
807
    }
 
808
 
 
809
    /* Must set vector notifier after guest notifier has been assigned */
 
810
    if ((with_irqfd || k->guest_notifier_mask) && assign) {
 
811
        if (with_irqfd) {
 
812
            proxy->vector_irqfd =
 
813
                g_malloc0(sizeof(*proxy->vector_irqfd) *
 
814
                          msix_nr_vectors_allocated(&proxy->pci_dev));
 
815
            r = kvm_virtio_pci_vector_use(proxy, nvqs);
 
816
            if (r < 0) {
 
817
                goto assign_error;
 
818
            }
 
819
        }
 
820
        r = msix_set_vector_notifiers(&proxy->pci_dev,
 
821
                                      virtio_pci_vector_unmask,
 
822
                                      virtio_pci_vector_mask,
 
823
                                      virtio_pci_vector_poll);
 
824
        if (r < 0) {
 
825
            goto notifiers_error;
 
826
        }
 
827
    }
 
828
 
 
829
    return 0;
 
830
 
 
831
notifiers_error:
 
832
    if (with_irqfd) {
 
833
        assert(assign);
 
834
        kvm_virtio_pci_vector_release(proxy, nvqs);
 
835
    }
 
836
 
 
837
assign_error:
 
838
    /* We get here on assignment failure. Recover by undoing for VQs 0 .. n. */
 
839
    assert(assign);
 
840
    while (--n >= 0) {
 
841
        virtio_pci_set_guest_notifier(d, n, !assign, with_irqfd);
 
842
    }
 
843
    return r;
 
844
}
 
845
 
 
846
static int virtio_pci_set_host_notifier(DeviceState *d, int n, bool assign)
 
847
{
 
848
    VirtIOPCIProxy *proxy = to_virtio_pci_proxy(d);
 
849
 
 
850
    /* Stop using ioeventfd for virtqueue kick if the device starts using host
 
851
     * notifiers.  This makes it easy to avoid stepping on each others' toes.
 
852
     */
 
853
    proxy->ioeventfd_disabled = assign;
 
854
    if (assign) {
 
855
        virtio_pci_stop_ioeventfd(proxy);
 
856
    }
 
857
    /* We don't need to start here: it's not needed because backend
 
858
     * currently only stops on status change away from ok,
 
859
     * reset, vmstop and such. If we do add code to start here,
 
860
     * need to check vmstate, device state etc. */
 
861
    return virtio_pci_set_host_notifier_internal(proxy, n, assign, false);
 
862
}
 
863
 
 
864
static void virtio_pci_vmstate_change(DeviceState *d, bool running)
 
865
{
 
866
    VirtIOPCIProxy *proxy = to_virtio_pci_proxy(d);
 
867
 
 
868
    if (running) {
 
869
        /* Try to find out if the guest has bus master disabled, but is
 
870
           in ready state. Then we have a buggy guest OS. */
 
871
        if ((proxy->vdev->status & VIRTIO_CONFIG_S_DRIVER_OK) &&
 
872
            !(proxy->pci_dev.config[PCI_COMMAND] & PCI_COMMAND_MASTER)) {
 
873
            proxy->flags |= VIRTIO_PCI_FLAG_BUS_MASTER_BUG;
 
874
        }
 
875
        virtio_pci_start_ioeventfd(proxy);
 
876
    } else {
 
877
        virtio_pci_stop_ioeventfd(proxy);
 
878
    }
 
879
}
 
880
 
 
881
#ifdef CONFIG_VIRTFS
 
882
static int virtio_9p_init_pci(VirtIOPCIProxy *vpci_dev)
 
883
{
 
884
    V9fsPCIState *dev = VIRTIO_9P_PCI(vpci_dev);
 
885
    DeviceState *vdev = DEVICE(&dev->vdev);
 
886
 
 
887
    qdev_set_parent_bus(vdev, BUS(&vpci_dev->bus));
 
888
    if (qdev_init(vdev) < 0) {
 
889
        return -1;
 
890
    }
 
891
    return 0;
 
892
}
 
893
 
 
894
static Property virtio_9p_pci_properties[] = {
 
895
    DEFINE_PROP_BIT("ioeventfd", VirtIOPCIProxy, flags,
 
896
                    VIRTIO_PCI_FLAG_USE_IOEVENTFD_BIT, true),
 
897
    DEFINE_PROP_UINT32("vectors", VirtIOPCIProxy, nvectors, 2),
 
898
    DEFINE_VIRTIO_COMMON_FEATURES(VirtIOPCIProxy, host_features),
 
899
    DEFINE_VIRTIO_9P_PROPERTIES(V9fsPCIState, vdev.fsconf),
 
900
    DEFINE_PROP_END_OF_LIST(),
 
901
};
 
902
 
 
903
static void virtio_9p_pci_class_init(ObjectClass *klass, void *data)
 
904
{
 
905
    DeviceClass *dc = DEVICE_CLASS(klass);
 
906
    PCIDeviceClass *pcidev_k = PCI_DEVICE_CLASS(klass);
 
907
    VirtioPCIClass *k = VIRTIO_PCI_CLASS(klass);
 
908
 
 
909
    k->init = virtio_9p_init_pci;
 
910
    pcidev_k->vendor_id = PCI_VENDOR_ID_REDHAT_QUMRANET;
 
911
    pcidev_k->device_id = PCI_DEVICE_ID_VIRTIO_9P;
 
912
    pcidev_k->revision = VIRTIO_PCI_ABI_VERSION;
 
913
    pcidev_k->class_id = 0x2;
 
914
    set_bit(DEVICE_CATEGORY_STORAGE, dc->categories);
 
915
    dc->props = virtio_9p_pci_properties;
 
916
}
 
917
 
 
918
static void virtio_9p_pci_instance_init(Object *obj)
 
919
{
 
920
    V9fsPCIState *dev = VIRTIO_9P_PCI(obj);
 
921
    object_initialize(&dev->vdev, sizeof(dev->vdev), TYPE_VIRTIO_9P);
 
922
    object_property_add_child(obj, "virtio-backend", OBJECT(&dev->vdev), NULL);
 
923
}
 
924
 
 
925
static const TypeInfo virtio_9p_pci_info = {
 
926
    .name          = TYPE_VIRTIO_9P_PCI,
 
927
    .parent        = TYPE_VIRTIO_PCI,
 
928
    .instance_size = sizeof(V9fsPCIState),
 
929
    .instance_init = virtio_9p_pci_instance_init,
 
930
    .class_init    = virtio_9p_pci_class_init,
 
931
};
 
932
#endif /* CONFIG_VIRTFS */
 
933
 
 
934
/*
 
935
 * virtio-pci: This is the PCIDevice which has a virtio-pci-bus.
 
936
 */
 
937
 
 
938
/* This is called by virtio-bus just after the device is plugged. */
 
939
static void virtio_pci_device_plugged(DeviceState *d)
 
940
{
 
941
    VirtIOPCIProxy *proxy = VIRTIO_PCI(d);
 
942
    VirtioBusState *bus = &proxy->bus;
 
943
    uint8_t *config;
 
944
    uint32_t size;
 
945
 
 
946
    proxy->vdev = bus->vdev;
 
947
 
 
948
    config = proxy->pci_dev.config;
 
949
    if (proxy->class_code) {
 
950
        pci_config_set_class(config, proxy->class_code);
 
951
    }
 
952
    pci_set_word(config + PCI_SUBSYSTEM_VENDOR_ID,
 
953
                 pci_get_word(config + PCI_VENDOR_ID));
 
954
    pci_set_word(config + PCI_SUBSYSTEM_ID, virtio_bus_get_vdev_id(bus));
 
955
    config[PCI_INTERRUPT_PIN] = 1;
 
956
 
 
957
    if (proxy->nvectors &&
 
958
        msix_init_exclusive_bar(&proxy->pci_dev, proxy->nvectors, 1)) {
 
959
        proxy->nvectors = 0;
 
960
    }
 
961
 
 
962
    proxy->pci_dev.config_write = virtio_write_config;
 
963
 
 
964
    size = VIRTIO_PCI_REGION_SIZE(&proxy->pci_dev)
 
965
         + virtio_bus_get_vdev_config_len(bus);
 
966
    if (size & (size - 1)) {
 
967
        size = 1 << qemu_fls(size);
 
968
    }
 
969
 
 
970
    memory_region_init_io(&proxy->bar, OBJECT(proxy), &virtio_pci_config_ops,
 
971
                          proxy, "virtio-pci", size);
 
972
    pci_register_bar(&proxy->pci_dev, 0, PCI_BASE_ADDRESS_SPACE_IO,
 
973
                     &proxy->bar);
 
974
 
 
975
    if (!kvm_has_many_ioeventfds()) {
 
976
        proxy->flags &= ~VIRTIO_PCI_FLAG_USE_IOEVENTFD;
 
977
    }
 
978
 
 
979
    proxy->host_features |= 0x1 << VIRTIO_F_NOTIFY_ON_EMPTY;
 
980
    proxy->host_features |= 0x1 << VIRTIO_F_BAD_FEATURE;
 
981
    proxy->host_features = virtio_bus_get_vdev_features(bus,
 
982
                                                      proxy->host_features);
 
983
}
 
984
 
 
985
static int virtio_pci_init(PCIDevice *pci_dev)
 
986
{
 
987
    VirtIOPCIProxy *dev = VIRTIO_PCI(pci_dev);
 
988
    VirtioPCIClass *k = VIRTIO_PCI_GET_CLASS(pci_dev);
 
989
    virtio_pci_bus_new(&dev->bus, sizeof(dev->bus), dev);
 
990
    if (k->init != NULL) {
 
991
        return k->init(dev);
 
992
    }
 
993
    return 0;
 
994
}
 
995
 
 
996
static void virtio_pci_exit(PCIDevice *pci_dev)
 
997
{
 
998
    VirtIOPCIProxy *proxy = VIRTIO_PCI(pci_dev);
 
999
    virtio_pci_stop_ioeventfd(proxy);
 
1000
    memory_region_destroy(&proxy->bar);
 
1001
    msix_uninit_exclusive_bar(pci_dev);
 
1002
}
 
1003
 
 
1004
static void virtio_pci_reset(DeviceState *qdev)
 
1005
{
 
1006
    VirtIOPCIProxy *proxy = VIRTIO_PCI(qdev);
 
1007
    VirtioBusState *bus = VIRTIO_BUS(&proxy->bus);
 
1008
    virtio_pci_stop_ioeventfd(proxy);
 
1009
    virtio_bus_reset(bus);
 
1010
    msix_unuse_all_vectors(&proxy->pci_dev);
 
1011
    proxy->flags &= ~VIRTIO_PCI_FLAG_BUS_MASTER_BUG;
 
1012
}
 
1013
 
 
1014
static void virtio_pci_class_init(ObjectClass *klass, void *data)
 
1015
{
 
1016
    DeviceClass *dc = DEVICE_CLASS(klass);
 
1017
    PCIDeviceClass *k = PCI_DEVICE_CLASS(klass);
 
1018
 
 
1019
    k->init = virtio_pci_init;
 
1020
    k->exit = virtio_pci_exit;
 
1021
    k->vendor_id = PCI_VENDOR_ID_REDHAT_QUMRANET;
 
1022
    k->revision = VIRTIO_PCI_ABI_VERSION;
 
1023
    k->class_id = PCI_CLASS_OTHERS;
 
1024
    dc->reset = virtio_pci_reset;
 
1025
}
 
1026
 
 
1027
static const TypeInfo virtio_pci_info = {
 
1028
    .name          = TYPE_VIRTIO_PCI,
 
1029
    .parent        = TYPE_PCI_DEVICE,
 
1030
    .instance_size = sizeof(VirtIOPCIProxy),
 
1031
    .class_init    = virtio_pci_class_init,
 
1032
    .class_size    = sizeof(VirtioPCIClass),
 
1033
    .abstract      = true,
 
1034
};
 
1035
 
 
1036
/* virtio-blk-pci */
 
1037
 
 
1038
static Property virtio_blk_pci_properties[] = {
 
1039
    DEFINE_PROP_HEX32("class", VirtIOPCIProxy, class_code, 0),
 
1040
    DEFINE_PROP_BIT("ioeventfd", VirtIOPCIProxy, flags,
 
1041
                    VIRTIO_PCI_FLAG_USE_IOEVENTFD_BIT, true),
 
1042
    DEFINE_PROP_UINT32("vectors", VirtIOPCIProxy, nvectors, 2),
 
1043
#ifdef CONFIG_VIRTIO_BLK_DATA_PLANE
 
1044
    DEFINE_PROP_BIT("x-data-plane", VirtIOBlkPCI, blk.data_plane, 0, false),
 
1045
#endif
 
1046
    DEFINE_VIRTIO_BLK_FEATURES(VirtIOPCIProxy, host_features),
 
1047
    DEFINE_VIRTIO_BLK_PROPERTIES(VirtIOBlkPCI, blk),
 
1048
    DEFINE_PROP_END_OF_LIST(),
 
1049
};
 
1050
 
 
1051
static int virtio_blk_pci_init(VirtIOPCIProxy *vpci_dev)
 
1052
{
 
1053
    VirtIOBlkPCI *dev = VIRTIO_BLK_PCI(vpci_dev);
 
1054
    DeviceState *vdev = DEVICE(&dev->vdev);
 
1055
    virtio_blk_set_conf(vdev, &(dev->blk));
 
1056
    qdev_set_parent_bus(vdev, BUS(&vpci_dev->bus));
 
1057
    if (qdev_init(vdev) < 0) {
 
1058
        return -1;
 
1059
    }
 
1060
    return 0;
 
1061
}
 
1062
 
 
1063
static void virtio_blk_pci_class_init(ObjectClass *klass, void *data)
 
1064
{
 
1065
    DeviceClass *dc = DEVICE_CLASS(klass);
 
1066
    VirtioPCIClass *k = VIRTIO_PCI_CLASS(klass);
 
1067
    PCIDeviceClass *pcidev_k = PCI_DEVICE_CLASS(klass);
 
1068
 
 
1069
    set_bit(DEVICE_CATEGORY_STORAGE, dc->categories);
 
1070
    dc->props = virtio_blk_pci_properties;
 
1071
    k->init = virtio_blk_pci_init;
 
1072
    pcidev_k->vendor_id = PCI_VENDOR_ID_REDHAT_QUMRANET;
 
1073
    pcidev_k->device_id = PCI_DEVICE_ID_VIRTIO_BLOCK;
 
1074
    pcidev_k->revision = VIRTIO_PCI_ABI_VERSION;
 
1075
    pcidev_k->class_id = PCI_CLASS_STORAGE_SCSI;
 
1076
}
 
1077
 
 
1078
static void virtio_blk_pci_instance_init(Object *obj)
 
1079
{
 
1080
    VirtIOBlkPCI *dev = VIRTIO_BLK_PCI(obj);
 
1081
    object_initialize(&dev->vdev, sizeof(dev->vdev), TYPE_VIRTIO_BLK);
 
1082
    object_property_add_child(obj, "virtio-backend", OBJECT(&dev->vdev), NULL);
 
1083
}
 
1084
 
 
1085
static const TypeInfo virtio_blk_pci_info = {
 
1086
    .name          = TYPE_VIRTIO_BLK_PCI,
 
1087
    .parent        = TYPE_VIRTIO_PCI,
 
1088
    .instance_size = sizeof(VirtIOBlkPCI),
 
1089
    .instance_init = virtio_blk_pci_instance_init,
 
1090
    .class_init    = virtio_blk_pci_class_init,
 
1091
};
 
1092
 
 
1093
/* virtio-scsi-pci */
 
1094
 
 
1095
static Property virtio_scsi_pci_properties[] = {
 
1096
    DEFINE_PROP_BIT("ioeventfd", VirtIOPCIProxy, flags,
 
1097
                    VIRTIO_PCI_FLAG_USE_IOEVENTFD_BIT, true),
 
1098
    DEFINE_PROP_UINT32("vectors", VirtIOPCIProxy, nvectors,
 
1099
                       DEV_NVECTORS_UNSPECIFIED),
 
1100
    DEFINE_VIRTIO_SCSI_FEATURES(VirtIOPCIProxy, host_features),
 
1101
    DEFINE_VIRTIO_SCSI_PROPERTIES(VirtIOSCSIPCI, vdev.parent_obj.conf),
 
1102
    DEFINE_PROP_END_OF_LIST(),
 
1103
};
 
1104
 
 
1105
static int virtio_scsi_pci_init_pci(VirtIOPCIProxy *vpci_dev)
 
1106
{
 
1107
    VirtIOSCSIPCI *dev = VIRTIO_SCSI_PCI(vpci_dev);
 
1108
    DeviceState *vdev = DEVICE(&dev->vdev);
 
1109
    VirtIOSCSICommon *vs = VIRTIO_SCSI_COMMON(vdev);
 
1110
    DeviceState *proxy = DEVICE(vpci_dev);
 
1111
    char *bus_name;
 
1112
 
 
1113
    if (vpci_dev->nvectors == DEV_NVECTORS_UNSPECIFIED) {
 
1114
        vpci_dev->nvectors = vs->conf.num_queues + 3;
 
1115
    }
 
1116
 
 
1117
    /*
 
1118
     * For command line compatibility, this sets the virtio-scsi-device bus
 
1119
     * name as before.
 
1120
     */
 
1121
    if (proxy->id) {
 
1122
        bus_name = g_strdup_printf("%s.0", proxy->id);
 
1123
        virtio_device_set_child_bus_name(VIRTIO_DEVICE(vdev), bus_name);
 
1124
        g_free(bus_name);
 
1125
    }
 
1126
 
 
1127
    qdev_set_parent_bus(vdev, BUS(&vpci_dev->bus));
 
1128
    if (qdev_init(vdev) < 0) {
 
1129
        return -1;
 
1130
    }
 
1131
    return 0;
 
1132
}
 
1133
 
 
1134
static void virtio_scsi_pci_class_init(ObjectClass *klass, void *data)
 
1135
{
 
1136
    DeviceClass *dc = DEVICE_CLASS(klass);
 
1137
    VirtioPCIClass *k = VIRTIO_PCI_CLASS(klass);
 
1138
    PCIDeviceClass *pcidev_k = PCI_DEVICE_CLASS(klass);
 
1139
    k->init = virtio_scsi_pci_init_pci;
 
1140
    set_bit(DEVICE_CATEGORY_STORAGE, dc->categories);
 
1141
    dc->props = virtio_scsi_pci_properties;
 
1142
    pcidev_k->vendor_id = PCI_VENDOR_ID_REDHAT_QUMRANET;
 
1143
    pcidev_k->device_id = PCI_DEVICE_ID_VIRTIO_SCSI;
 
1144
    pcidev_k->revision = 0x00;
 
1145
    pcidev_k->class_id = PCI_CLASS_STORAGE_SCSI;
 
1146
}
 
1147
 
 
1148
static void virtio_scsi_pci_instance_init(Object *obj)
 
1149
{
 
1150
    VirtIOSCSIPCI *dev = VIRTIO_SCSI_PCI(obj);
 
1151
    object_initialize(&dev->vdev, sizeof(dev->vdev), TYPE_VIRTIO_SCSI);
 
1152
    object_property_add_child(obj, "virtio-backend", OBJECT(&dev->vdev), NULL);
 
1153
}
 
1154
 
 
1155
static const TypeInfo virtio_scsi_pci_info = {
 
1156
    .name          = TYPE_VIRTIO_SCSI_PCI,
 
1157
    .parent        = TYPE_VIRTIO_PCI,
 
1158
    .instance_size = sizeof(VirtIOSCSIPCI),
 
1159
    .instance_init = virtio_scsi_pci_instance_init,
 
1160
    .class_init    = virtio_scsi_pci_class_init,
 
1161
};
 
1162
 
 
1163
/* vhost-scsi-pci */
 
1164
 
 
1165
#ifdef CONFIG_VHOST_SCSI
 
1166
static Property vhost_scsi_pci_properties[] = {
 
1167
    DEFINE_PROP_UINT32("vectors", VirtIOPCIProxy, nvectors,
 
1168
                       DEV_NVECTORS_UNSPECIFIED),
 
1169
    DEFINE_VIRTIO_COMMON_FEATURES(VirtIOPCIProxy, host_features),
 
1170
    DEFINE_VHOST_SCSI_PROPERTIES(VHostSCSIPCI, vdev.parent_obj.conf),
 
1171
    DEFINE_PROP_END_OF_LIST(),
 
1172
};
 
1173
 
 
1174
static int vhost_scsi_pci_init_pci(VirtIOPCIProxy *vpci_dev)
 
1175
{
 
1176
    VHostSCSIPCI *dev = VHOST_SCSI_PCI(vpci_dev);
 
1177
    DeviceState *vdev = DEVICE(&dev->vdev);
 
1178
    VirtIOSCSICommon *vs = VIRTIO_SCSI_COMMON(vdev);
 
1179
 
 
1180
    if (vpci_dev->nvectors == DEV_NVECTORS_UNSPECIFIED) {
 
1181
        vpci_dev->nvectors = vs->conf.num_queues + 3;
 
1182
    }
 
1183
 
 
1184
    qdev_set_parent_bus(vdev, BUS(&vpci_dev->bus));
 
1185
    if (qdev_init(vdev) < 0) {
 
1186
        return -1;
 
1187
    }
 
1188
    return 0;
 
1189
}
 
1190
 
 
1191
static void vhost_scsi_pci_class_init(ObjectClass *klass, void *data)
 
1192
{
 
1193
    DeviceClass *dc = DEVICE_CLASS(klass);
 
1194
    VirtioPCIClass *k = VIRTIO_PCI_CLASS(klass);
 
1195
    PCIDeviceClass *pcidev_k = PCI_DEVICE_CLASS(klass);
 
1196
    k->init = vhost_scsi_pci_init_pci;
 
1197
    set_bit(DEVICE_CATEGORY_STORAGE, dc->categories);
 
1198
    dc->props = vhost_scsi_pci_properties;
 
1199
    pcidev_k->vendor_id = PCI_VENDOR_ID_REDHAT_QUMRANET;
 
1200
    pcidev_k->device_id = PCI_DEVICE_ID_VIRTIO_SCSI;
 
1201
    pcidev_k->revision = 0x00;
 
1202
    pcidev_k->class_id = PCI_CLASS_STORAGE_SCSI;
 
1203
}
 
1204
 
 
1205
static void vhost_scsi_pci_instance_init(Object *obj)
 
1206
{
 
1207
    VHostSCSIPCI *dev = VHOST_SCSI_PCI(obj);
 
1208
    object_initialize(&dev->vdev, sizeof(dev->vdev), TYPE_VHOST_SCSI);
 
1209
    object_property_add_child(obj, "virtio-backend", OBJECT(&dev->vdev), NULL);
 
1210
}
 
1211
 
 
1212
static const TypeInfo vhost_scsi_pci_info = {
 
1213
    .name          = TYPE_VHOST_SCSI_PCI,
 
1214
    .parent        = TYPE_VIRTIO_PCI,
 
1215
    .instance_size = sizeof(VHostSCSIPCI),
 
1216
    .instance_init = vhost_scsi_pci_instance_init,
 
1217
    .class_init    = vhost_scsi_pci_class_init,
 
1218
};
 
1219
#endif
 
1220
 
 
1221
/* virtio-balloon-pci */
 
1222
 
 
1223
static void balloon_pci_stats_get_all(Object *obj, struct Visitor *v,
 
1224
                                      void *opaque, const char *name,
 
1225
                                      Error **errp)
 
1226
{
 
1227
    VirtIOBalloonPCI *dev = opaque;
 
1228
    object_property_get(OBJECT(&dev->vdev), v, "guest-stats", errp);
 
1229
}
 
1230
 
 
1231
static void balloon_pci_stats_get_poll_interval(Object *obj, struct Visitor *v,
 
1232
                                                void *opaque, const char *name,
 
1233
                                                Error **errp)
 
1234
{
 
1235
    VirtIOBalloonPCI *dev = opaque;
 
1236
    object_property_get(OBJECT(&dev->vdev), v, "guest-stats-polling-interval",
 
1237
                        errp);
 
1238
}
 
1239
 
 
1240
static void balloon_pci_stats_set_poll_interval(Object *obj, struct Visitor *v,
 
1241
                                                void *opaque, const char *name,
 
1242
                                                Error **errp)
 
1243
{
 
1244
    VirtIOBalloonPCI *dev = opaque;
 
1245
    object_property_set(OBJECT(&dev->vdev), v, "guest-stats-polling-interval",
 
1246
                        errp);
 
1247
}
 
1248
 
 
1249
static Property virtio_balloon_pci_properties[] = {
 
1250
    DEFINE_VIRTIO_COMMON_FEATURES(VirtIOPCIProxy, host_features),
 
1251
    DEFINE_PROP_HEX32("class", VirtIOPCIProxy, class_code, 0),
 
1252
    DEFINE_PROP_END_OF_LIST(),
 
1253
};
 
1254
 
 
1255
static int virtio_balloon_pci_init(VirtIOPCIProxy *vpci_dev)
 
1256
{
 
1257
    VirtIOBalloonPCI *dev = VIRTIO_BALLOON_PCI(vpci_dev);
 
1258
    DeviceState *vdev = DEVICE(&dev->vdev);
 
1259
 
 
1260
    if (vpci_dev->class_code != PCI_CLASS_OTHERS &&
 
1261
        vpci_dev->class_code != PCI_CLASS_MEMORY_RAM) { /* qemu < 1.1 */
 
1262
        vpci_dev->class_code = PCI_CLASS_OTHERS;
 
1263
    }
 
1264
 
 
1265
    qdev_set_parent_bus(vdev, BUS(&vpci_dev->bus));
 
1266
    if (qdev_init(vdev) < 0) {
 
1267
        return -1;
 
1268
    }
 
1269
    return 0;
 
1270
}
 
1271
 
 
1272
static void virtio_balloon_pci_class_init(ObjectClass *klass, void *data)
 
1273
{
 
1274
    DeviceClass *dc = DEVICE_CLASS(klass);
 
1275
    VirtioPCIClass *k = VIRTIO_PCI_CLASS(klass);
 
1276
    PCIDeviceClass *pcidev_k = PCI_DEVICE_CLASS(klass);
 
1277
    k->init = virtio_balloon_pci_init;
 
1278
    set_bit(DEVICE_CATEGORY_MISC, dc->categories);
 
1279
    dc->props = virtio_balloon_pci_properties;
 
1280
    pcidev_k->vendor_id = PCI_VENDOR_ID_REDHAT_QUMRANET;
 
1281
    pcidev_k->device_id = PCI_DEVICE_ID_VIRTIO_BALLOON;
 
1282
    pcidev_k->revision = VIRTIO_PCI_ABI_VERSION;
 
1283
    pcidev_k->class_id = PCI_CLASS_OTHERS;
 
1284
}
 
1285
 
 
1286
static void virtio_balloon_pci_instance_init(Object *obj)
 
1287
{
 
1288
    VirtIOBalloonPCI *dev = VIRTIO_BALLOON_PCI(obj);
 
1289
    object_initialize(&dev->vdev, sizeof(dev->vdev), TYPE_VIRTIO_BALLOON);
 
1290
    object_property_add_child(obj, "virtio-backend", OBJECT(&dev->vdev), NULL);
 
1291
 
 
1292
    object_property_add(obj, "guest-stats", "guest statistics",
 
1293
                        balloon_pci_stats_get_all, NULL, NULL, dev,
 
1294
                        NULL);
 
1295
 
 
1296
    object_property_add(obj, "guest-stats-polling-interval", "int",
 
1297
                        balloon_pci_stats_get_poll_interval,
 
1298
                        balloon_pci_stats_set_poll_interval,
 
1299
                        NULL, dev, NULL);
 
1300
}
 
1301
 
 
1302
static const TypeInfo virtio_balloon_pci_info = {
 
1303
    .name          = TYPE_VIRTIO_BALLOON_PCI,
 
1304
    .parent        = TYPE_VIRTIO_PCI,
 
1305
    .instance_size = sizeof(VirtIOBalloonPCI),
 
1306
    .instance_init = virtio_balloon_pci_instance_init,
 
1307
    .class_init    = virtio_balloon_pci_class_init,
 
1308
};
 
1309
 
 
1310
/* virtio-serial-pci */
 
1311
 
 
1312
static int virtio_serial_pci_init(VirtIOPCIProxy *vpci_dev)
 
1313
{
 
1314
    VirtIOSerialPCI *dev = VIRTIO_SERIAL_PCI(vpci_dev);
 
1315
    DeviceState *vdev = DEVICE(&dev->vdev);
 
1316
    DeviceState *proxy = DEVICE(vpci_dev);
 
1317
    char *bus_name;
 
1318
 
 
1319
    if (vpci_dev->class_code != PCI_CLASS_COMMUNICATION_OTHER &&
 
1320
        vpci_dev->class_code != PCI_CLASS_DISPLAY_OTHER && /* qemu 0.10 */
 
1321
        vpci_dev->class_code != PCI_CLASS_OTHERS) {        /* qemu-kvm  */
 
1322
            vpci_dev->class_code = PCI_CLASS_COMMUNICATION_OTHER;
 
1323
    }
 
1324
 
 
1325
    /* backwards-compatibility with machines that were created with
 
1326
       DEV_NVECTORS_UNSPECIFIED */
 
1327
    if (vpci_dev->nvectors == DEV_NVECTORS_UNSPECIFIED) {
 
1328
        vpci_dev->nvectors = dev->vdev.serial.max_virtserial_ports + 1;
 
1329
    }
 
1330
 
 
1331
    /*
 
1332
     * For command line compatibility, this sets the virtio-serial-device bus
 
1333
     * name as before.
 
1334
     */
 
1335
    if (proxy->id) {
 
1336
        bus_name = g_strdup_printf("%s.0", proxy->id);
 
1337
        virtio_device_set_child_bus_name(VIRTIO_DEVICE(vdev), bus_name);
 
1338
        g_free(bus_name);
 
1339
    }
 
1340
 
 
1341
    qdev_set_parent_bus(vdev, BUS(&vpci_dev->bus));
 
1342
    if (qdev_init(vdev) < 0) {
 
1343
        return -1;
 
1344
    }
 
1345
    return 0;
 
1346
}
 
1347
 
 
1348
static Property virtio_serial_pci_properties[] = {
 
1349
    DEFINE_PROP_BIT("ioeventfd", VirtIOPCIProxy, flags,
 
1350
                    VIRTIO_PCI_FLAG_USE_IOEVENTFD_BIT, true),
 
1351
    DEFINE_PROP_UINT32("vectors", VirtIOPCIProxy, nvectors, 2),
 
1352
    DEFINE_PROP_HEX32("class", VirtIOPCIProxy, class_code, 0),
 
1353
    DEFINE_VIRTIO_COMMON_FEATURES(VirtIOPCIProxy, host_features),
 
1354
    DEFINE_VIRTIO_SERIAL_PROPERTIES(VirtIOSerialPCI, vdev.serial),
 
1355
    DEFINE_PROP_END_OF_LIST(),
 
1356
};
 
1357
 
 
1358
static void virtio_serial_pci_class_init(ObjectClass *klass, void *data)
 
1359
{
 
1360
    DeviceClass *dc = DEVICE_CLASS(klass);
 
1361
    VirtioPCIClass *k = VIRTIO_PCI_CLASS(klass);
 
1362
    PCIDeviceClass *pcidev_k = PCI_DEVICE_CLASS(klass);
 
1363
    k->init = virtio_serial_pci_init;
 
1364
    set_bit(DEVICE_CATEGORY_INPUT, dc->categories);
 
1365
    dc->props = virtio_serial_pci_properties;
 
1366
    pcidev_k->vendor_id = PCI_VENDOR_ID_REDHAT_QUMRANET;
 
1367
    pcidev_k->device_id = PCI_DEVICE_ID_VIRTIO_CONSOLE;
 
1368
    pcidev_k->revision = VIRTIO_PCI_ABI_VERSION;
 
1369
    pcidev_k->class_id = PCI_CLASS_COMMUNICATION_OTHER;
 
1370
}
 
1371
 
 
1372
static void virtio_serial_pci_instance_init(Object *obj)
 
1373
{
 
1374
    VirtIOSerialPCI *dev = VIRTIO_SERIAL_PCI(obj);
 
1375
    object_initialize(&dev->vdev, sizeof(dev->vdev), TYPE_VIRTIO_SERIAL);
 
1376
    object_property_add_child(obj, "virtio-backend", OBJECT(&dev->vdev), NULL);
 
1377
}
 
1378
 
 
1379
static const TypeInfo virtio_serial_pci_info = {
 
1380
    .name          = TYPE_VIRTIO_SERIAL_PCI,
 
1381
    .parent        = TYPE_VIRTIO_PCI,
 
1382
    .instance_size = sizeof(VirtIOSerialPCI),
 
1383
    .instance_init = virtio_serial_pci_instance_init,
 
1384
    .class_init    = virtio_serial_pci_class_init,
 
1385
};
 
1386
 
 
1387
/* virtio-net-pci */
 
1388
 
 
1389
static Property virtio_net_properties[] = {
 
1390
    DEFINE_PROP_BIT("ioeventfd", VirtIOPCIProxy, flags,
 
1391
                    VIRTIO_PCI_FLAG_USE_IOEVENTFD_BIT, false),
 
1392
    DEFINE_PROP_UINT32("vectors", VirtIOPCIProxy, nvectors, 3),
 
1393
    DEFINE_VIRTIO_NET_FEATURES(VirtIOPCIProxy, host_features),
 
1394
    DEFINE_NIC_PROPERTIES(VirtIONetPCI, vdev.nic_conf),
 
1395
    DEFINE_VIRTIO_NET_PROPERTIES(VirtIONetPCI, vdev.net_conf),
 
1396
    DEFINE_PROP_END_OF_LIST(),
 
1397
};
 
1398
 
 
1399
static int virtio_net_pci_init(VirtIOPCIProxy *vpci_dev)
 
1400
{
 
1401
    DeviceState *qdev = DEVICE(vpci_dev);
 
1402
    VirtIONetPCI *dev = VIRTIO_NET_PCI(vpci_dev);
 
1403
    DeviceState *vdev = DEVICE(&dev->vdev);
 
1404
 
 
1405
    virtio_net_set_config_size(&dev->vdev, vpci_dev->host_features);
 
1406
    virtio_net_set_netclient_name(&dev->vdev, qdev->id,
 
1407
                                  object_get_typename(OBJECT(qdev)));
 
1408
    qdev_set_parent_bus(vdev, BUS(&vpci_dev->bus));
 
1409
    if (qdev_init(vdev) < 0) {
 
1410
        return -1;
 
1411
    }
 
1412
    return 0;
 
1413
}
 
1414
 
 
1415
static void virtio_net_pci_class_init(ObjectClass *klass, void *data)
 
1416
{
 
1417
    DeviceClass *dc = DEVICE_CLASS(klass);
 
1418
    PCIDeviceClass *k = PCI_DEVICE_CLASS(klass);
 
1419
    VirtioPCIClass *vpciklass = VIRTIO_PCI_CLASS(klass);
 
1420
 
 
1421
    k->romfile = "efi-virtio.rom";
 
1422
    k->vendor_id = PCI_VENDOR_ID_REDHAT_QUMRANET;
 
1423
    k->device_id = PCI_DEVICE_ID_VIRTIO_NET;
 
1424
    k->revision = VIRTIO_PCI_ABI_VERSION;
 
1425
    k->class_id = PCI_CLASS_NETWORK_ETHERNET;
 
1426
    set_bit(DEVICE_CATEGORY_NETWORK, dc->categories);
 
1427
    dc->props = virtio_net_properties;
 
1428
    vpciklass->init = virtio_net_pci_init;
 
1429
}
 
1430
 
 
1431
static void virtio_net_pci_instance_init(Object *obj)
 
1432
{
 
1433
    VirtIONetPCI *dev = VIRTIO_NET_PCI(obj);
 
1434
    object_initialize(&dev->vdev, sizeof(dev->vdev), TYPE_VIRTIO_NET);
 
1435
    object_property_add_child(obj, "virtio-backend", OBJECT(&dev->vdev), NULL);
 
1436
}
 
1437
 
 
1438
static const TypeInfo virtio_net_pci_info = {
 
1439
    .name          = TYPE_VIRTIO_NET_PCI,
 
1440
    .parent        = TYPE_VIRTIO_PCI,
 
1441
    .instance_size = sizeof(VirtIONetPCI),
 
1442
    .instance_init = virtio_net_pci_instance_init,
 
1443
    .class_init    = virtio_net_pci_class_init,
 
1444
};
 
1445
 
 
1446
/* virtio-rng-pci */
 
1447
 
 
1448
static Property virtio_rng_pci_properties[] = {
 
1449
    DEFINE_VIRTIO_COMMON_FEATURES(VirtIOPCIProxy, host_features),
 
1450
    DEFINE_VIRTIO_RNG_PROPERTIES(VirtIORngPCI, vdev.conf),
 
1451
    DEFINE_PROP_END_OF_LIST(),
 
1452
};
 
1453
 
 
1454
static int virtio_rng_pci_init(VirtIOPCIProxy *vpci_dev)
 
1455
{
 
1456
    VirtIORngPCI *vrng = VIRTIO_RNG_PCI(vpci_dev);
 
1457
    DeviceState *vdev = DEVICE(&vrng->vdev);
 
1458
 
 
1459
    qdev_set_parent_bus(vdev, BUS(&vpci_dev->bus));
 
1460
    if (qdev_init(vdev) < 0) {
 
1461
        return -1;
 
1462
    }
 
1463
 
 
1464
    object_property_set_link(OBJECT(vrng),
 
1465
                             OBJECT(vrng->vdev.conf.rng), "rng",
 
1466
                             NULL);
 
1467
 
 
1468
    return 0;
 
1469
}
 
1470
 
 
1471
static void virtio_rng_pci_class_init(ObjectClass *klass, void *data)
 
1472
{
 
1473
    DeviceClass *dc = DEVICE_CLASS(klass);
 
1474
    VirtioPCIClass *k = VIRTIO_PCI_CLASS(klass);
 
1475
    PCIDeviceClass *pcidev_k = PCI_DEVICE_CLASS(klass);
 
1476
 
 
1477
    k->init = virtio_rng_pci_init;
 
1478
    set_bit(DEVICE_CATEGORY_MISC, dc->categories);
 
1479
    dc->props = virtio_rng_pci_properties;
 
1480
 
 
1481
    pcidev_k->vendor_id = PCI_VENDOR_ID_REDHAT_QUMRANET;
 
1482
    pcidev_k->device_id = PCI_DEVICE_ID_VIRTIO_RNG;
 
1483
    pcidev_k->revision = VIRTIO_PCI_ABI_VERSION;
 
1484
    pcidev_k->class_id = PCI_CLASS_OTHERS;
 
1485
}
 
1486
 
 
1487
static void virtio_rng_initfn(Object *obj)
 
1488
{
 
1489
    VirtIORngPCI *dev = VIRTIO_RNG_PCI(obj);
 
1490
    object_initialize(&dev->vdev, sizeof(dev->vdev), TYPE_VIRTIO_RNG);
 
1491
    object_property_add_child(obj, "virtio-backend", OBJECT(&dev->vdev), NULL);
 
1492
    object_property_add_link(obj, "rng", TYPE_RNG_BACKEND,
 
1493
                             (Object **)&dev->vdev.conf.rng, NULL);
 
1494
 
 
1495
}
 
1496
 
 
1497
static const TypeInfo virtio_rng_pci_info = {
 
1498
    .name          = TYPE_VIRTIO_RNG_PCI,
 
1499
    .parent        = TYPE_VIRTIO_PCI,
 
1500
    .instance_size = sizeof(VirtIORngPCI),
 
1501
    .instance_init = virtio_rng_initfn,
 
1502
    .class_init    = virtio_rng_pci_class_init,
 
1503
};
 
1504
 
 
1505
/* virtio-pci-bus */
 
1506
 
 
1507
static void virtio_pci_bus_new(VirtioBusState *bus, size_t bus_size,
 
1508
                               VirtIOPCIProxy *dev)
 
1509
{
 
1510
    DeviceState *qdev = DEVICE(dev);
 
1511
    BusState *qbus;
 
1512
    char virtio_bus_name[] = "virtio-bus";
 
1513
 
 
1514
    qbus_create_inplace(bus, bus_size, TYPE_VIRTIO_PCI_BUS, qdev,
 
1515
                        virtio_bus_name);
 
1516
    qbus = BUS(bus);
 
1517
    qbus->allow_hotplug = 1;
 
1518
}
 
1519
 
 
1520
static void virtio_pci_bus_class_init(ObjectClass *klass, void *data)
 
1521
{
 
1522
    BusClass *bus_class = BUS_CLASS(klass);
 
1523
    VirtioBusClass *k = VIRTIO_BUS_CLASS(klass);
 
1524
    bus_class->max_dev = 1;
 
1525
    k->notify = virtio_pci_notify;
 
1526
    k->save_config = virtio_pci_save_config;
 
1527
    k->load_config = virtio_pci_load_config;
 
1528
    k->save_queue = virtio_pci_save_queue;
 
1529
    k->load_queue = virtio_pci_load_queue;
 
1530
    k->get_features = virtio_pci_get_features;
 
1531
    k->query_guest_notifiers = virtio_pci_query_guest_notifiers;
 
1532
    k->set_host_notifier = virtio_pci_set_host_notifier;
 
1533
    k->set_guest_notifiers = virtio_pci_set_guest_notifiers;
 
1534
    k->vmstate_change = virtio_pci_vmstate_change;
 
1535
    k->device_plugged = virtio_pci_device_plugged;
 
1536
}
 
1537
 
 
1538
static const TypeInfo virtio_pci_bus_info = {
 
1539
    .name          = TYPE_VIRTIO_PCI_BUS,
 
1540
    .parent        = TYPE_VIRTIO_BUS,
 
1541
    .instance_size = sizeof(VirtioPCIBusState),
 
1542
    .class_init    = virtio_pci_bus_class_init,
 
1543
};
 
1544
 
 
1545
static void virtio_pci_register_types(void)
 
1546
{
 
1547
    type_register_static(&virtio_rng_pci_info);
 
1548
    type_register_static(&virtio_pci_bus_info);
 
1549
    type_register_static(&virtio_pci_info);
 
1550
#ifdef CONFIG_VIRTFS
 
1551
    type_register_static(&virtio_9p_pci_info);
 
1552
#endif
 
1553
    type_register_static(&virtio_blk_pci_info);
 
1554
    type_register_static(&virtio_scsi_pci_info);
 
1555
    type_register_static(&virtio_balloon_pci_info);
 
1556
    type_register_static(&virtio_serial_pci_info);
 
1557
    type_register_static(&virtio_net_pci_info);
 
1558
#ifdef CONFIG_VHOST_SCSI
 
1559
    type_register_static(&vhost_scsi_pci_info);
 
1560
#endif
 
1561
}
 
1562
 
 
1563
type_init(virtio_pci_register_types)