xref: /openbmc/qemu/hw/virtio/virtio-pci.c (revision 5cff81f098a57762ab937bfbedb298e658af2c7f)
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         qemu_set_irq(proxy->pci_dev.irq[0], 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         qemu_set_irq(proxy->pci_dev.irq[0], 0);
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,
804                                           kvm_msi_via_irqfd_enabled());
805         if (r < 0) {
806             goto assign_error;
807         }
808     }
809 
810     /* Must set vector notifier after guest notifier has been assigned */
811     if ((with_irqfd || k->guest_notifier_mask) && assign) {
812         if (with_irqfd) {
813             proxy->vector_irqfd =
814                 g_malloc0(sizeof(*proxy->vector_irqfd) *
815                           msix_nr_vectors_allocated(&proxy->pci_dev));
816             r = kvm_virtio_pci_vector_use(proxy, nvqs);
817             if (r < 0) {
818                 goto assign_error;
819             }
820         }
821         r = msix_set_vector_notifiers(&proxy->pci_dev,
822                                       virtio_pci_vector_unmask,
823                                       virtio_pci_vector_mask,
824                                       virtio_pci_vector_poll);
825         if (r < 0) {
826             goto notifiers_error;
827         }
828     }
829 
830     return 0;
831 
832 notifiers_error:
833     if (with_irqfd) {
834         assert(assign);
835         kvm_virtio_pci_vector_release(proxy, nvqs);
836     }
837 
838 assign_error:
839     /* We get here on assignment failure. Recover by undoing for VQs 0 .. n. */
840     assert(assign);
841     while (--n >= 0) {
842         virtio_pci_set_guest_notifier(d, n, !assign, with_irqfd);
843     }
844     return r;
845 }
846 
847 static int virtio_pci_set_host_notifier(DeviceState *d, int n, bool assign)
848 {
849     VirtIOPCIProxy *proxy = to_virtio_pci_proxy(d);
850 
851     /* Stop using ioeventfd for virtqueue kick if the device starts using host
852      * notifiers.  This makes it easy to avoid stepping on each others' toes.
853      */
854     proxy->ioeventfd_disabled = assign;
855     if (assign) {
856         virtio_pci_stop_ioeventfd(proxy);
857     }
858     /* We don't need to start here: it's not needed because backend
859      * currently only stops on status change away from ok,
860      * reset, vmstop and such. If we do add code to start here,
861      * need to check vmstate, device state etc. */
862     return virtio_pci_set_host_notifier_internal(proxy, n, assign, false);
863 }
864 
865 static void virtio_pci_vmstate_change(DeviceState *d, bool running)
866 {
867     VirtIOPCIProxy *proxy = to_virtio_pci_proxy(d);
868 
869     if (running) {
870         /* Try to find out if the guest has bus master disabled, but is
871            in ready state. Then we have a buggy guest OS. */
872         if ((proxy->vdev->status & VIRTIO_CONFIG_S_DRIVER_OK) &&
873             !(proxy->pci_dev.config[PCI_COMMAND] & PCI_COMMAND_MASTER)) {
874             proxy->flags |= VIRTIO_PCI_FLAG_BUS_MASTER_BUG;
875         }
876         virtio_pci_start_ioeventfd(proxy);
877     } else {
878         virtio_pci_stop_ioeventfd(proxy);
879     }
880 }
881 
882 #ifdef CONFIG_VIRTFS
883 static int virtio_9p_init_pci(VirtIOPCIProxy *vpci_dev)
884 {
885     V9fsPCIState *dev = VIRTIO_9P_PCI(vpci_dev);
886     DeviceState *vdev = DEVICE(&dev->vdev);
887 
888     qdev_set_parent_bus(vdev, BUS(&vpci_dev->bus));
889     if (qdev_init(vdev) < 0) {
890         return -1;
891     }
892     return 0;
893 }
894 
895 static Property virtio_9p_pci_properties[] = {
896     DEFINE_PROP_BIT("ioeventfd", VirtIOPCIProxy, flags,
897                     VIRTIO_PCI_FLAG_USE_IOEVENTFD_BIT, true),
898     DEFINE_PROP_UINT32("vectors", VirtIOPCIProxy, nvectors, 2),
899     DEFINE_VIRTIO_COMMON_FEATURES(VirtIOPCIProxy, host_features),
900     DEFINE_VIRTIO_9P_PROPERTIES(V9fsPCIState, vdev.fsconf),
901     DEFINE_PROP_END_OF_LIST(),
902 };
903 
904 static void virtio_9p_pci_class_init(ObjectClass *klass, void *data)
905 {
906     DeviceClass *dc = DEVICE_CLASS(klass);
907     PCIDeviceClass *pcidev_k = PCI_DEVICE_CLASS(klass);
908     VirtioPCIClass *k = VIRTIO_PCI_CLASS(klass);
909 
910     k->init = virtio_9p_init_pci;
911     pcidev_k->vendor_id = PCI_VENDOR_ID_REDHAT_QUMRANET;
912     pcidev_k->device_id = PCI_DEVICE_ID_VIRTIO_9P;
913     pcidev_k->revision = VIRTIO_PCI_ABI_VERSION;
914     pcidev_k->class_id = 0x2;
915     set_bit(DEVICE_CATEGORY_STORAGE, dc->categories);
916     dc->props = virtio_9p_pci_properties;
917 }
918 
919 static void virtio_9p_pci_instance_init(Object *obj)
920 {
921     V9fsPCIState *dev = VIRTIO_9P_PCI(obj);
922     object_initialize(&dev->vdev, sizeof(dev->vdev), TYPE_VIRTIO_9P);
923     object_property_add_child(obj, "virtio-backend", OBJECT(&dev->vdev), NULL);
924 }
925 
926 static const TypeInfo virtio_9p_pci_info = {
927     .name          = TYPE_VIRTIO_9P_PCI,
928     .parent        = TYPE_VIRTIO_PCI,
929     .instance_size = sizeof(V9fsPCIState),
930     .instance_init = virtio_9p_pci_instance_init,
931     .class_init    = virtio_9p_pci_class_init,
932 };
933 #endif /* CONFIG_VIRTFS */
934 
935 /*
936  * virtio-pci: This is the PCIDevice which has a virtio-pci-bus.
937  */
938 
939 /* This is called by virtio-bus just after the device is plugged. */
940 static void virtio_pci_device_plugged(DeviceState *d)
941 {
942     VirtIOPCIProxy *proxy = VIRTIO_PCI(d);
943     VirtioBusState *bus = &proxy->bus;
944     uint8_t *config;
945     uint32_t size;
946 
947     proxy->vdev = bus->vdev;
948 
949     config = proxy->pci_dev.config;
950     if (proxy->class_code) {
951         pci_config_set_class(config, proxy->class_code);
952     }
953     pci_set_word(config + PCI_SUBSYSTEM_VENDOR_ID,
954                  pci_get_word(config + PCI_VENDOR_ID));
955     pci_set_word(config + PCI_SUBSYSTEM_ID, virtio_bus_get_vdev_id(bus));
956     config[PCI_INTERRUPT_PIN] = 1;
957 
958     if (proxy->nvectors &&
959         msix_init_exclusive_bar(&proxy->pci_dev, proxy->nvectors, 1)) {
960         proxy->nvectors = 0;
961     }
962 
963     proxy->pci_dev.config_write = virtio_write_config;
964 
965     size = VIRTIO_PCI_REGION_SIZE(&proxy->pci_dev)
966          + virtio_bus_get_vdev_config_len(bus);
967     if (size & (size - 1)) {
968         size = 1 << qemu_fls(size);
969     }
970 
971     memory_region_init_io(&proxy->bar, OBJECT(proxy), &virtio_pci_config_ops,
972                           proxy, "virtio-pci", size);
973     pci_register_bar(&proxy->pci_dev, 0, PCI_BASE_ADDRESS_SPACE_IO,
974                      &proxy->bar);
975 
976     if (!kvm_has_many_ioeventfds()) {
977         proxy->flags &= ~VIRTIO_PCI_FLAG_USE_IOEVENTFD;
978     }
979 
980     proxy->host_features |= 0x1 << VIRTIO_F_NOTIFY_ON_EMPTY;
981     proxy->host_features |= 0x1 << VIRTIO_F_BAD_FEATURE;
982     proxy->host_features = virtio_bus_get_vdev_features(bus,
983                                                       proxy->host_features);
984 }
985 
986 static int virtio_pci_init(PCIDevice *pci_dev)
987 {
988     VirtIOPCIProxy *dev = VIRTIO_PCI(pci_dev);
989     VirtioPCIClass *k = VIRTIO_PCI_GET_CLASS(pci_dev);
990     virtio_pci_bus_new(&dev->bus, sizeof(dev->bus), dev);
991     if (k->init != NULL) {
992         return k->init(dev);
993     }
994     return 0;
995 }
996 
997 static void virtio_pci_exit(PCIDevice *pci_dev)
998 {
999     VirtIOPCIProxy *proxy = VIRTIO_PCI(pci_dev);
1000     virtio_pci_stop_ioeventfd(proxy);
1001     memory_region_destroy(&proxy->bar);
1002     msix_uninit_exclusive_bar(pci_dev);
1003 }
1004 
1005 static void virtio_pci_reset(DeviceState *qdev)
1006 {
1007     VirtIOPCIProxy *proxy = VIRTIO_PCI(qdev);
1008     VirtioBusState *bus = VIRTIO_BUS(&proxy->bus);
1009     virtio_pci_stop_ioeventfd(proxy);
1010     virtio_bus_reset(bus);
1011     msix_unuse_all_vectors(&proxy->pci_dev);
1012     proxy->flags &= ~VIRTIO_PCI_FLAG_BUS_MASTER_BUG;
1013 }
1014 
1015 static void virtio_pci_class_init(ObjectClass *klass, void *data)
1016 {
1017     DeviceClass *dc = DEVICE_CLASS(klass);
1018     PCIDeviceClass *k = PCI_DEVICE_CLASS(klass);
1019 
1020     k->init = virtio_pci_init;
1021     k->exit = virtio_pci_exit;
1022     k->vendor_id = PCI_VENDOR_ID_REDHAT_QUMRANET;
1023     k->revision = VIRTIO_PCI_ABI_VERSION;
1024     k->class_id = PCI_CLASS_OTHERS;
1025     dc->reset = virtio_pci_reset;
1026 }
1027 
1028 static const TypeInfo virtio_pci_info = {
1029     .name          = TYPE_VIRTIO_PCI,
1030     .parent        = TYPE_PCI_DEVICE,
1031     .instance_size = sizeof(VirtIOPCIProxy),
1032     .class_init    = virtio_pci_class_init,
1033     .class_size    = sizeof(VirtioPCIClass),
1034     .abstract      = true,
1035 };
1036 
1037 /* virtio-blk-pci */
1038 
1039 static Property virtio_blk_pci_properties[] = {
1040     DEFINE_PROP_HEX32("class", VirtIOPCIProxy, class_code, 0),
1041     DEFINE_PROP_BIT("ioeventfd", VirtIOPCIProxy, flags,
1042                     VIRTIO_PCI_FLAG_USE_IOEVENTFD_BIT, true),
1043     DEFINE_PROP_UINT32("vectors", VirtIOPCIProxy, nvectors, 2),
1044 #ifdef CONFIG_VIRTIO_BLK_DATA_PLANE
1045     DEFINE_PROP_BIT("x-data-plane", VirtIOBlkPCI, blk.data_plane, 0, false),
1046 #endif
1047     DEFINE_VIRTIO_BLK_FEATURES(VirtIOPCIProxy, host_features),
1048     DEFINE_VIRTIO_BLK_PROPERTIES(VirtIOBlkPCI, blk),
1049     DEFINE_PROP_END_OF_LIST(),
1050 };
1051 
1052 static int virtio_blk_pci_init(VirtIOPCIProxy *vpci_dev)
1053 {
1054     VirtIOBlkPCI *dev = VIRTIO_BLK_PCI(vpci_dev);
1055     DeviceState *vdev = DEVICE(&dev->vdev);
1056     virtio_blk_set_conf(vdev, &(dev->blk));
1057     qdev_set_parent_bus(vdev, BUS(&vpci_dev->bus));
1058     if (qdev_init(vdev) < 0) {
1059         return -1;
1060     }
1061     return 0;
1062 }
1063 
1064 static void virtio_blk_pci_class_init(ObjectClass *klass, void *data)
1065 {
1066     DeviceClass *dc = DEVICE_CLASS(klass);
1067     VirtioPCIClass *k = VIRTIO_PCI_CLASS(klass);
1068     PCIDeviceClass *pcidev_k = PCI_DEVICE_CLASS(klass);
1069 
1070     set_bit(DEVICE_CATEGORY_STORAGE, dc->categories);
1071     dc->props = virtio_blk_pci_properties;
1072     k->init = virtio_blk_pci_init;
1073     pcidev_k->vendor_id = PCI_VENDOR_ID_REDHAT_QUMRANET;
1074     pcidev_k->device_id = PCI_DEVICE_ID_VIRTIO_BLOCK;
1075     pcidev_k->revision = VIRTIO_PCI_ABI_VERSION;
1076     pcidev_k->class_id = PCI_CLASS_STORAGE_SCSI;
1077 }
1078 
1079 static void virtio_blk_pci_instance_init(Object *obj)
1080 {
1081     VirtIOBlkPCI *dev = VIRTIO_BLK_PCI(obj);
1082     object_initialize(&dev->vdev, sizeof(dev->vdev), TYPE_VIRTIO_BLK);
1083     object_property_add_child(obj, "virtio-backend", OBJECT(&dev->vdev), NULL);
1084 }
1085 
1086 static const TypeInfo virtio_blk_pci_info = {
1087     .name          = TYPE_VIRTIO_BLK_PCI,
1088     .parent        = TYPE_VIRTIO_PCI,
1089     .instance_size = sizeof(VirtIOBlkPCI),
1090     .instance_init = virtio_blk_pci_instance_init,
1091     .class_init    = virtio_blk_pci_class_init,
1092 };
1093 
1094 /* virtio-scsi-pci */
1095 
1096 static Property virtio_scsi_pci_properties[] = {
1097     DEFINE_PROP_BIT("ioeventfd", VirtIOPCIProxy, flags,
1098                     VIRTIO_PCI_FLAG_USE_IOEVENTFD_BIT, true),
1099     DEFINE_PROP_UINT32("vectors", VirtIOPCIProxy, nvectors,
1100                        DEV_NVECTORS_UNSPECIFIED),
1101     DEFINE_VIRTIO_SCSI_FEATURES(VirtIOPCIProxy, host_features),
1102     DEFINE_VIRTIO_SCSI_PROPERTIES(VirtIOSCSIPCI, vdev.parent_obj.conf),
1103     DEFINE_PROP_END_OF_LIST(),
1104 };
1105 
1106 static int virtio_scsi_pci_init_pci(VirtIOPCIProxy *vpci_dev)
1107 {
1108     VirtIOSCSIPCI *dev = VIRTIO_SCSI_PCI(vpci_dev);
1109     DeviceState *vdev = DEVICE(&dev->vdev);
1110     VirtIOSCSICommon *vs = VIRTIO_SCSI_COMMON(vdev);
1111     DeviceState *proxy = DEVICE(vpci_dev);
1112     char *bus_name;
1113 
1114     if (vpci_dev->nvectors == DEV_NVECTORS_UNSPECIFIED) {
1115         vpci_dev->nvectors = vs->conf.num_queues + 3;
1116     }
1117 
1118     /*
1119      * For command line compatibility, this sets the virtio-scsi-device bus
1120      * name as before.
1121      */
1122     if (proxy->id) {
1123         bus_name = g_strdup_printf("%s.0", proxy->id);
1124         virtio_device_set_child_bus_name(VIRTIO_DEVICE(vdev), bus_name);
1125         g_free(bus_name);
1126     }
1127 
1128     qdev_set_parent_bus(vdev, BUS(&vpci_dev->bus));
1129     if (qdev_init(vdev) < 0) {
1130         return -1;
1131     }
1132     return 0;
1133 }
1134 
1135 static void virtio_scsi_pci_class_init(ObjectClass *klass, void *data)
1136 {
1137     DeviceClass *dc = DEVICE_CLASS(klass);
1138     VirtioPCIClass *k = VIRTIO_PCI_CLASS(klass);
1139     PCIDeviceClass *pcidev_k = PCI_DEVICE_CLASS(klass);
1140     k->init = virtio_scsi_pci_init_pci;
1141     set_bit(DEVICE_CATEGORY_STORAGE, dc->categories);
1142     dc->props = virtio_scsi_pci_properties;
1143     pcidev_k->vendor_id = PCI_VENDOR_ID_REDHAT_QUMRANET;
1144     pcidev_k->device_id = PCI_DEVICE_ID_VIRTIO_SCSI;
1145     pcidev_k->revision = 0x00;
1146     pcidev_k->class_id = PCI_CLASS_STORAGE_SCSI;
1147 }
1148 
1149 static void virtio_scsi_pci_instance_init(Object *obj)
1150 {
1151     VirtIOSCSIPCI *dev = VIRTIO_SCSI_PCI(obj);
1152     object_initialize(&dev->vdev, sizeof(dev->vdev), TYPE_VIRTIO_SCSI);
1153     object_property_add_child(obj, "virtio-backend", OBJECT(&dev->vdev), NULL);
1154 }
1155 
1156 static const TypeInfo virtio_scsi_pci_info = {
1157     .name          = TYPE_VIRTIO_SCSI_PCI,
1158     .parent        = TYPE_VIRTIO_PCI,
1159     .instance_size = sizeof(VirtIOSCSIPCI),
1160     .instance_init = virtio_scsi_pci_instance_init,
1161     .class_init    = virtio_scsi_pci_class_init,
1162 };
1163 
1164 /* vhost-scsi-pci */
1165 
1166 #ifdef CONFIG_VHOST_SCSI
1167 static Property vhost_scsi_pci_properties[] = {
1168     DEFINE_PROP_UINT32("vectors", VirtIOPCIProxy, nvectors,
1169                        DEV_NVECTORS_UNSPECIFIED),
1170     DEFINE_VIRTIO_COMMON_FEATURES(VirtIOPCIProxy, host_features),
1171     DEFINE_VHOST_SCSI_PROPERTIES(VHostSCSIPCI, vdev.parent_obj.conf),
1172     DEFINE_PROP_END_OF_LIST(),
1173 };
1174 
1175 static int vhost_scsi_pci_init_pci(VirtIOPCIProxy *vpci_dev)
1176 {
1177     VHostSCSIPCI *dev = VHOST_SCSI_PCI(vpci_dev);
1178     DeviceState *vdev = DEVICE(&dev->vdev);
1179     VirtIOSCSICommon *vs = VIRTIO_SCSI_COMMON(vdev);
1180 
1181     if (vpci_dev->nvectors == DEV_NVECTORS_UNSPECIFIED) {
1182         vpci_dev->nvectors = vs->conf.num_queues + 3;
1183     }
1184 
1185     qdev_set_parent_bus(vdev, BUS(&vpci_dev->bus));
1186     if (qdev_init(vdev) < 0) {
1187         return -1;
1188     }
1189     return 0;
1190 }
1191 
1192 static void vhost_scsi_pci_class_init(ObjectClass *klass, void *data)
1193 {
1194     DeviceClass *dc = DEVICE_CLASS(klass);
1195     VirtioPCIClass *k = VIRTIO_PCI_CLASS(klass);
1196     PCIDeviceClass *pcidev_k = PCI_DEVICE_CLASS(klass);
1197     k->init = vhost_scsi_pci_init_pci;
1198     set_bit(DEVICE_CATEGORY_STORAGE, dc->categories);
1199     dc->props = vhost_scsi_pci_properties;
1200     pcidev_k->vendor_id = PCI_VENDOR_ID_REDHAT_QUMRANET;
1201     pcidev_k->device_id = PCI_DEVICE_ID_VIRTIO_SCSI;
1202     pcidev_k->revision = 0x00;
1203     pcidev_k->class_id = PCI_CLASS_STORAGE_SCSI;
1204 }
1205 
1206 static void vhost_scsi_pci_instance_init(Object *obj)
1207 {
1208     VHostSCSIPCI *dev = VHOST_SCSI_PCI(obj);
1209     object_initialize(&dev->vdev, sizeof(dev->vdev), TYPE_VHOST_SCSI);
1210     object_property_add_child(obj, "virtio-backend", OBJECT(&dev->vdev), NULL);
1211 }
1212 
1213 static const TypeInfo vhost_scsi_pci_info = {
1214     .name          = TYPE_VHOST_SCSI_PCI,
1215     .parent        = TYPE_VIRTIO_PCI,
1216     .instance_size = sizeof(VHostSCSIPCI),
1217     .instance_init = vhost_scsi_pci_instance_init,
1218     .class_init    = vhost_scsi_pci_class_init,
1219 };
1220 #endif
1221 
1222 /* virtio-balloon-pci */
1223 
1224 static void balloon_pci_stats_get_all(Object *obj, struct Visitor *v,
1225                                       void *opaque, const char *name,
1226                                       Error **errp)
1227 {
1228     VirtIOBalloonPCI *dev = opaque;
1229     object_property_get(OBJECT(&dev->vdev), v, "guest-stats", errp);
1230 }
1231 
1232 static void balloon_pci_stats_get_poll_interval(Object *obj, struct Visitor *v,
1233                                                 void *opaque, const char *name,
1234                                                 Error **errp)
1235 {
1236     VirtIOBalloonPCI *dev = opaque;
1237     object_property_get(OBJECT(&dev->vdev), v, "guest-stats-polling-interval",
1238                         errp);
1239 }
1240 
1241 static void balloon_pci_stats_set_poll_interval(Object *obj, struct Visitor *v,
1242                                                 void *opaque, const char *name,
1243                                                 Error **errp)
1244 {
1245     VirtIOBalloonPCI *dev = opaque;
1246     object_property_set(OBJECT(&dev->vdev), v, "guest-stats-polling-interval",
1247                         errp);
1248 }
1249 
1250 static Property virtio_balloon_pci_properties[] = {
1251     DEFINE_VIRTIO_COMMON_FEATURES(VirtIOPCIProxy, host_features),
1252     DEFINE_PROP_HEX32("class", VirtIOPCIProxy, class_code, 0),
1253     DEFINE_PROP_END_OF_LIST(),
1254 };
1255 
1256 static int virtio_balloon_pci_init(VirtIOPCIProxy *vpci_dev)
1257 {
1258     VirtIOBalloonPCI *dev = VIRTIO_BALLOON_PCI(vpci_dev);
1259     DeviceState *vdev = DEVICE(&dev->vdev);
1260 
1261     if (vpci_dev->class_code != PCI_CLASS_OTHERS &&
1262         vpci_dev->class_code != PCI_CLASS_MEMORY_RAM) { /* qemu < 1.1 */
1263         vpci_dev->class_code = PCI_CLASS_OTHERS;
1264     }
1265 
1266     qdev_set_parent_bus(vdev, BUS(&vpci_dev->bus));
1267     if (qdev_init(vdev) < 0) {
1268         return -1;
1269     }
1270     return 0;
1271 }
1272 
1273 static void virtio_balloon_pci_class_init(ObjectClass *klass, void *data)
1274 {
1275     DeviceClass *dc = DEVICE_CLASS(klass);
1276     VirtioPCIClass *k = VIRTIO_PCI_CLASS(klass);
1277     PCIDeviceClass *pcidev_k = PCI_DEVICE_CLASS(klass);
1278     k->init = virtio_balloon_pci_init;
1279     set_bit(DEVICE_CATEGORY_MISC, dc->categories);
1280     dc->props = virtio_balloon_pci_properties;
1281     pcidev_k->vendor_id = PCI_VENDOR_ID_REDHAT_QUMRANET;
1282     pcidev_k->device_id = PCI_DEVICE_ID_VIRTIO_BALLOON;
1283     pcidev_k->revision = VIRTIO_PCI_ABI_VERSION;
1284     pcidev_k->class_id = PCI_CLASS_OTHERS;
1285 }
1286 
1287 static void virtio_balloon_pci_instance_init(Object *obj)
1288 {
1289     VirtIOBalloonPCI *dev = VIRTIO_BALLOON_PCI(obj);
1290     object_initialize(&dev->vdev, sizeof(dev->vdev), TYPE_VIRTIO_BALLOON);
1291     object_property_add_child(obj, "virtio-backend", OBJECT(&dev->vdev), NULL);
1292 
1293     object_property_add(obj, "guest-stats", "guest statistics",
1294                         balloon_pci_stats_get_all, NULL, NULL, dev,
1295                         NULL);
1296 
1297     object_property_add(obj, "guest-stats-polling-interval", "int",
1298                         balloon_pci_stats_get_poll_interval,
1299                         balloon_pci_stats_set_poll_interval,
1300                         NULL, dev, NULL);
1301 }
1302 
1303 static const TypeInfo virtio_balloon_pci_info = {
1304     .name          = TYPE_VIRTIO_BALLOON_PCI,
1305     .parent        = TYPE_VIRTIO_PCI,
1306     .instance_size = sizeof(VirtIOBalloonPCI),
1307     .instance_init = virtio_balloon_pci_instance_init,
1308     .class_init    = virtio_balloon_pci_class_init,
1309 };
1310 
1311 /* virtio-serial-pci */
1312 
1313 static int virtio_serial_pci_init(VirtIOPCIProxy *vpci_dev)
1314 {
1315     VirtIOSerialPCI *dev = VIRTIO_SERIAL_PCI(vpci_dev);
1316     DeviceState *vdev = DEVICE(&dev->vdev);
1317     DeviceState *proxy = DEVICE(vpci_dev);
1318     char *bus_name;
1319 
1320     if (vpci_dev->class_code != PCI_CLASS_COMMUNICATION_OTHER &&
1321         vpci_dev->class_code != PCI_CLASS_DISPLAY_OTHER && /* qemu 0.10 */
1322         vpci_dev->class_code != PCI_CLASS_OTHERS) {        /* qemu-kvm  */
1323             vpci_dev->class_code = PCI_CLASS_COMMUNICATION_OTHER;
1324     }
1325 
1326     /* backwards-compatibility with machines that were created with
1327        DEV_NVECTORS_UNSPECIFIED */
1328     if (vpci_dev->nvectors == DEV_NVECTORS_UNSPECIFIED) {
1329         vpci_dev->nvectors = dev->vdev.serial.max_virtserial_ports + 1;
1330     }
1331 
1332     /*
1333      * For command line compatibility, this sets the virtio-serial-device bus
1334      * name as before.
1335      */
1336     if (proxy->id) {
1337         bus_name = g_strdup_printf("%s.0", proxy->id);
1338         virtio_device_set_child_bus_name(VIRTIO_DEVICE(vdev), bus_name);
1339         g_free(bus_name);
1340     }
1341 
1342     qdev_set_parent_bus(vdev, BUS(&vpci_dev->bus));
1343     if (qdev_init(vdev) < 0) {
1344         return -1;
1345     }
1346     return 0;
1347 }
1348 
1349 static Property virtio_serial_pci_properties[] = {
1350     DEFINE_PROP_BIT("ioeventfd", VirtIOPCIProxy, flags,
1351                     VIRTIO_PCI_FLAG_USE_IOEVENTFD_BIT, true),
1352     DEFINE_PROP_UINT32("vectors", VirtIOPCIProxy, nvectors, 2),
1353     DEFINE_PROP_HEX32("class", VirtIOPCIProxy, class_code, 0),
1354     DEFINE_VIRTIO_COMMON_FEATURES(VirtIOPCIProxy, host_features),
1355     DEFINE_VIRTIO_SERIAL_PROPERTIES(VirtIOSerialPCI, vdev.serial),
1356     DEFINE_PROP_END_OF_LIST(),
1357 };
1358 
1359 static void virtio_serial_pci_class_init(ObjectClass *klass, void *data)
1360 {
1361     DeviceClass *dc = DEVICE_CLASS(klass);
1362     VirtioPCIClass *k = VIRTIO_PCI_CLASS(klass);
1363     PCIDeviceClass *pcidev_k = PCI_DEVICE_CLASS(klass);
1364     k->init = virtio_serial_pci_init;
1365     set_bit(DEVICE_CATEGORY_INPUT, dc->categories);
1366     dc->props = virtio_serial_pci_properties;
1367     pcidev_k->vendor_id = PCI_VENDOR_ID_REDHAT_QUMRANET;
1368     pcidev_k->device_id = PCI_DEVICE_ID_VIRTIO_CONSOLE;
1369     pcidev_k->revision = VIRTIO_PCI_ABI_VERSION;
1370     pcidev_k->class_id = PCI_CLASS_COMMUNICATION_OTHER;
1371 }
1372 
1373 static void virtio_serial_pci_instance_init(Object *obj)
1374 {
1375     VirtIOSerialPCI *dev = VIRTIO_SERIAL_PCI(obj);
1376     object_initialize(&dev->vdev, sizeof(dev->vdev), TYPE_VIRTIO_SERIAL);
1377     object_property_add_child(obj, "virtio-backend", OBJECT(&dev->vdev), NULL);
1378 }
1379 
1380 static const TypeInfo virtio_serial_pci_info = {
1381     .name          = TYPE_VIRTIO_SERIAL_PCI,
1382     .parent        = TYPE_VIRTIO_PCI,
1383     .instance_size = sizeof(VirtIOSerialPCI),
1384     .instance_init = virtio_serial_pci_instance_init,
1385     .class_init    = virtio_serial_pci_class_init,
1386 };
1387 
1388 /* virtio-net-pci */
1389 
1390 static Property virtio_net_properties[] = {
1391     DEFINE_PROP_BIT("ioeventfd", VirtIOPCIProxy, flags,
1392                     VIRTIO_PCI_FLAG_USE_IOEVENTFD_BIT, false),
1393     DEFINE_PROP_UINT32("vectors", VirtIOPCIProxy, nvectors, 3),
1394     DEFINE_VIRTIO_NET_FEATURES(VirtIOPCIProxy, host_features),
1395     DEFINE_NIC_PROPERTIES(VirtIONetPCI, vdev.nic_conf),
1396     DEFINE_VIRTIO_NET_PROPERTIES(VirtIONetPCI, vdev.net_conf),
1397     DEFINE_PROP_END_OF_LIST(),
1398 };
1399 
1400 static int virtio_net_pci_init(VirtIOPCIProxy *vpci_dev)
1401 {
1402     DeviceState *qdev = DEVICE(vpci_dev);
1403     VirtIONetPCI *dev = VIRTIO_NET_PCI(vpci_dev);
1404     DeviceState *vdev = DEVICE(&dev->vdev);
1405 
1406     virtio_net_set_config_size(&dev->vdev, vpci_dev->host_features);
1407     virtio_net_set_netclient_name(&dev->vdev, qdev->id,
1408                                   object_get_typename(OBJECT(qdev)));
1409     qdev_set_parent_bus(vdev, BUS(&vpci_dev->bus));
1410     if (qdev_init(vdev) < 0) {
1411         return -1;
1412     }
1413     return 0;
1414 }
1415 
1416 static void virtio_net_pci_class_init(ObjectClass *klass, void *data)
1417 {
1418     DeviceClass *dc = DEVICE_CLASS(klass);
1419     PCIDeviceClass *k = PCI_DEVICE_CLASS(klass);
1420     VirtioPCIClass *vpciklass = VIRTIO_PCI_CLASS(klass);
1421 
1422     k->romfile = "efi-virtio.rom";
1423     k->vendor_id = PCI_VENDOR_ID_REDHAT_QUMRANET;
1424     k->device_id = PCI_DEVICE_ID_VIRTIO_NET;
1425     k->revision = VIRTIO_PCI_ABI_VERSION;
1426     k->class_id = PCI_CLASS_NETWORK_ETHERNET;
1427     set_bit(DEVICE_CATEGORY_NETWORK, dc->categories);
1428     dc->props = virtio_net_properties;
1429     vpciklass->init = virtio_net_pci_init;
1430 }
1431 
1432 static void virtio_net_pci_instance_init(Object *obj)
1433 {
1434     VirtIONetPCI *dev = VIRTIO_NET_PCI(obj);
1435     object_initialize(&dev->vdev, sizeof(dev->vdev), TYPE_VIRTIO_NET);
1436     object_property_add_child(obj, "virtio-backend", OBJECT(&dev->vdev), NULL);
1437 }
1438 
1439 static const TypeInfo virtio_net_pci_info = {
1440     .name          = TYPE_VIRTIO_NET_PCI,
1441     .parent        = TYPE_VIRTIO_PCI,
1442     .instance_size = sizeof(VirtIONetPCI),
1443     .instance_init = virtio_net_pci_instance_init,
1444     .class_init    = virtio_net_pci_class_init,
1445 };
1446 
1447 /* virtio-rng-pci */
1448 
1449 static Property virtio_rng_pci_properties[] = {
1450     DEFINE_VIRTIO_COMMON_FEATURES(VirtIOPCIProxy, host_features),
1451     DEFINE_VIRTIO_RNG_PROPERTIES(VirtIORngPCI, vdev.conf),
1452     DEFINE_PROP_END_OF_LIST(),
1453 };
1454 
1455 static int virtio_rng_pci_init(VirtIOPCIProxy *vpci_dev)
1456 {
1457     VirtIORngPCI *vrng = VIRTIO_RNG_PCI(vpci_dev);
1458     DeviceState *vdev = DEVICE(&vrng->vdev);
1459 
1460     qdev_set_parent_bus(vdev, BUS(&vpci_dev->bus));
1461     if (qdev_init(vdev) < 0) {
1462         return -1;
1463     }
1464 
1465     object_property_set_link(OBJECT(vrng),
1466                              OBJECT(vrng->vdev.conf.rng), "rng",
1467                              NULL);
1468 
1469     return 0;
1470 }
1471 
1472 static void virtio_rng_pci_class_init(ObjectClass *klass, void *data)
1473 {
1474     DeviceClass *dc = DEVICE_CLASS(klass);
1475     VirtioPCIClass *k = VIRTIO_PCI_CLASS(klass);
1476     PCIDeviceClass *pcidev_k = PCI_DEVICE_CLASS(klass);
1477 
1478     k->init = virtio_rng_pci_init;
1479     set_bit(DEVICE_CATEGORY_MISC, dc->categories);
1480     dc->props = virtio_rng_pci_properties;
1481 
1482     pcidev_k->vendor_id = PCI_VENDOR_ID_REDHAT_QUMRANET;
1483     pcidev_k->device_id = PCI_DEVICE_ID_VIRTIO_RNG;
1484     pcidev_k->revision = VIRTIO_PCI_ABI_VERSION;
1485     pcidev_k->class_id = PCI_CLASS_OTHERS;
1486 }
1487 
1488 static void virtio_rng_initfn(Object *obj)
1489 {
1490     VirtIORngPCI *dev = VIRTIO_RNG_PCI(obj);
1491     object_initialize(&dev->vdev, sizeof(dev->vdev), TYPE_VIRTIO_RNG);
1492     object_property_add_child(obj, "virtio-backend", OBJECT(&dev->vdev), NULL);
1493     object_property_add_link(obj, "rng", TYPE_RNG_BACKEND,
1494                              (Object **)&dev->vdev.conf.rng, NULL);
1495 
1496 }
1497 
1498 static const TypeInfo virtio_rng_pci_info = {
1499     .name          = TYPE_VIRTIO_RNG_PCI,
1500     .parent        = TYPE_VIRTIO_PCI,
1501     .instance_size = sizeof(VirtIORngPCI),
1502     .instance_init = virtio_rng_initfn,
1503     .class_init    = virtio_rng_pci_class_init,
1504 };
1505 
1506 /* virtio-pci-bus */
1507 
1508 static void virtio_pci_bus_new(VirtioBusState *bus, size_t bus_size,
1509                                VirtIOPCIProxy *dev)
1510 {
1511     DeviceState *qdev = DEVICE(dev);
1512     BusState *qbus;
1513     char virtio_bus_name[] = "virtio-bus";
1514 
1515     qbus_create_inplace(bus, bus_size, TYPE_VIRTIO_PCI_BUS, qdev,
1516                         virtio_bus_name);
1517     qbus = BUS(bus);
1518     qbus->allow_hotplug = 1;
1519 }
1520 
1521 static void virtio_pci_bus_class_init(ObjectClass *klass, void *data)
1522 {
1523     BusClass *bus_class = BUS_CLASS(klass);
1524     VirtioBusClass *k = VIRTIO_BUS_CLASS(klass);
1525     bus_class->max_dev = 1;
1526     k->notify = virtio_pci_notify;
1527     k->save_config = virtio_pci_save_config;
1528     k->load_config = virtio_pci_load_config;
1529     k->save_queue = virtio_pci_save_queue;
1530     k->load_queue = virtio_pci_load_queue;
1531     k->get_features = virtio_pci_get_features;
1532     k->query_guest_notifiers = virtio_pci_query_guest_notifiers;
1533     k->set_host_notifier = virtio_pci_set_host_notifier;
1534     k->set_guest_notifiers = virtio_pci_set_guest_notifiers;
1535     k->vmstate_change = virtio_pci_vmstate_change;
1536     k->device_plugged = virtio_pci_device_plugged;
1537 }
1538 
1539 static const TypeInfo virtio_pci_bus_info = {
1540     .name          = TYPE_VIRTIO_PCI_BUS,
1541     .parent        = TYPE_VIRTIO_BUS,
1542     .instance_size = sizeof(VirtioPCIBusState),
1543     .class_init    = virtio_pci_bus_class_init,
1544 };
1545 
1546 static void virtio_pci_register_types(void)
1547 {
1548     type_register_static(&virtio_rng_pci_info);
1549     type_register_static(&virtio_pci_bus_info);
1550     type_register_static(&virtio_pci_info);
1551 #ifdef CONFIG_VIRTFS
1552     type_register_static(&virtio_9p_pci_info);
1553 #endif
1554     type_register_static(&virtio_blk_pci_info);
1555     type_register_static(&virtio_scsi_pci_info);
1556     type_register_static(&virtio_balloon_pci_info);
1557     type_register_static(&virtio_serial_pci_info);
1558     type_register_static(&virtio_net_pci_info);
1559 #ifdef CONFIG_VHOST_SCSI
1560     type_register_static(&vhost_scsi_pci_info);
1561 #endif
1562 }
1563 
1564 type_init(virtio_pci_register_types)
1565