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