4 * Copyright IBM, Corp. 2007
5 * Copyright (c) 2009 CodeSourcery
8 * Anthony Liguori <aliguori@us.ibm.com>
9 * Paul Brook <paul@codesourcery.com>
11 * This work is licensed under the terms of the GNU GPL, version 2. See
12 * the COPYING file in the top-level directory.
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.
21 #include "virtio-transport.h"
22 #include "virtio-blk.h"
23 #include "virtio-net.h"
24 #include "virtio-serial.h"
25 #include "virtio-scsi.h"
26 #include "virtio-balloon.h"
28 #include "qemu-error.h"
35 #include "virtio-pci.h"
38 /* from Linux's linux/virtio_pci.h */
40 /* A 32-bit r/o bitmask of the features supported by the host */
41 #define VIRTIO_PCI_HOST_FEATURES 0
43 /* A 32-bit r/w bitmask of features activated by the guest */
44 #define VIRTIO_PCI_GUEST_FEATURES 4
46 /* A 32-bit r/w PFN for the currently selected queue */
47 #define VIRTIO_PCI_QUEUE_PFN 8
49 /* A 16-bit r/o queue size for the currently selected queue */
50 #define VIRTIO_PCI_QUEUE_NUM 12
52 /* A 16-bit r/w queue selector */
53 #define VIRTIO_PCI_QUEUE_SEL 14
55 /* A 16-bit r/w queue notifier */
56 #define VIRTIO_PCI_QUEUE_NOTIFY 16
58 /* An 8-bit device status register. */
59 #define VIRTIO_PCI_STATUS 18
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
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
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)
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)
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
89 /* Flags track per-device state like workarounds for quirks in older guests. */
90 #define VIRTIO_PCI_FLAG_BUS_MASTER_BUG (1 << 0)
92 /* QEMU doesn't strictly need write barriers since everything runs in
93 * lock-step. We'll leave the calls to wmb() in though to make it obvious for
94 * KVM or if kqemu gets SMP support.
96 #define wmb() do { } while (0)
98 /* HACK for virtio to determine if it's running a big endian guest */
99 bool virtio_is_big_endian(void);
103 static void virtio_pci_notify(void *opaque, uint16_t vector)
105 VirtIOPCI *s = opaque;
106 if (msix_enabled(&s->pci_dev)) {
107 msix_notify(&s->pci_dev, vector);
110 qemu_set_irq(s->pci_dev.irq[0], s->vdev->isr & 1);
114 static void virtio_pci_save_config(void * opaque, QEMUFile *f)
116 VirtIOPCI *s = opaque;
117 pci_device_save(&s->pci_dev, f);
118 msix_save(&s->pci_dev, f);
119 if (msix_present(&s->pci_dev)) {
120 qemu_put_be16(f, s->vdev->config_vector);
124 static void virtio_pci_save_queue(void * opaque, int n, QEMUFile *f)
126 VirtIOPCI *s = opaque;
127 if (msix_present(&s->pci_dev)) {
128 qemu_put_be16(f, virtio_queue_vector(s->vdev, n));
132 static int virtio_pci_load_config(void * opaque, QEMUFile *f)
134 VirtIOPCI *s = opaque;
136 ret = pci_device_load(&s->pci_dev, f);
140 msix_unuse_all_vectors(&s->pci_dev);
141 msix_load(&s->pci_dev, f);
142 if (msix_present(&s->pci_dev)) {
143 qemu_get_be16s(f, &s->vdev->config_vector);
145 s->vdev->config_vector = VIRTIO_NO_VECTOR;
147 if (s->vdev->config_vector != VIRTIO_NO_VECTOR) {
148 return msix_vector_use(&s->pci_dev, s->vdev->config_vector);
153 static int virtio_pci_load_queue(void * opaque, int n, QEMUFile *f)
155 VirtIOPCI *s = opaque;
157 if (msix_present(&s->pci_dev)) {
158 qemu_get_be16s(f, &vector);
160 vector = VIRTIO_NO_VECTOR;
162 virtio_queue_set_vector(s->vdev, n, vector);
163 if (vector != VIRTIO_NO_VECTOR) {
164 return msix_vector_use(&s->pci_dev, vector);
169 static int virtio_pci_set_host_notifier_internal(VirtIOPCI *s, int n,
170 bool assign, bool set_handler)
172 VirtQueue *vq = virtio_get_queue(s->vdev, n);
173 EventNotifier *notifier = virtio_queue_get_host_notifier(vq);
177 r = event_notifier_init(notifier, 1);
179 error_report("%s: unable to init event notifier: %d", __func__, r);
182 virtio_queue_set_host_notifier_fd_handler(vq, true, set_handler);
183 memory_region_add_eventfd(&s->bar, VIRTIO_PCI_QUEUE_NOTIFY, 2, true, n,
186 memory_region_del_eventfd(&s->bar, VIRTIO_PCI_QUEUE_NOTIFY, 2, true, n,
188 virtio_queue_set_host_notifier_fd_handler(vq, false, false);
189 event_notifier_cleanup(notifier);
194 static void virtio_pci_start_ioeventfd(VirtIOPCI *s)
198 if (!(s->flags & VIRTIO_PCI_FLAG_USE_IOEVENTFD) ||
199 s->ioeventfd_disabled ||
200 s->ioeventfd_started) {
204 for (n = 0; n < VIRTIO_PCI_QUEUE_MAX; n++) {
205 if (!virtio_queue_get_num(s->vdev, n)) {
209 r = virtio_pci_set_host_notifier_internal(s, n, true, true);
214 s->ioeventfd_started = true;
219 if (!virtio_queue_get_num(s->vdev, n)) {
223 r = virtio_pci_set_host_notifier_internal(s, n, false, false);
226 s->ioeventfd_started = false;
227 error_report("%s: failed. Fallback to a userspace (slower).", __func__);
230 static void virtio_pci_stop_ioeventfd(VirtIOPCI *s)
235 if (!s->ioeventfd_started) {
239 for (n = 0; n < VIRTIO_PCI_QUEUE_MAX; n++) {
240 if (!virtio_queue_get_num(s->vdev, n)) {
244 r = virtio_pci_set_host_notifier_internal(s, n, false, false);
247 s->ioeventfd_started = false;
250 void virtio_pci_reset_(DeviceState *d)
253 container_of(d, VirtIOPCI, pci_dev.qdev);
254 virtio_pci_stop_ioeventfd(s);
256 virtio_reset(s->vdev);
258 msix_unuse_all_vectors(&s->pci_dev);
259 s->flags &= ~VIRTIO_PCI_FLAG_BUS_MASTER_BUG;
262 static void virtio_ioport_write(void *opaque, uint32_t addr, uint32_t val)
264 VirtIOPCI *s = opaque;
265 VirtIODevice *vdev = s->vdev;
266 target_phys_addr_t pa;
269 case VIRTIO_PCI_GUEST_FEATURES:
270 /* Guest does not negotiate properly? We have to assume nothing. */
271 if (val & (1 << VIRTIO_F_BAD_FEATURE)) {
272 val = vdev->bad_features ? vdev->bad_features(vdev) : 0;
274 virtio_set_features(vdev, val);
276 case VIRTIO_PCI_QUEUE_PFN:
277 pa = (target_phys_addr_t)val << VIRTIO_PCI_QUEUE_ADDR_SHIFT;
279 virtio_pci_stop_ioeventfd(s);
280 virtio_reset(s->vdev);
281 msix_unuse_all_vectors(&s->pci_dev);
284 virtio_queue_set_addr(vdev, vdev->queue_sel, pa);
286 case VIRTIO_PCI_QUEUE_SEL:
287 if (val < VIRTIO_PCI_QUEUE_MAX)
288 vdev->queue_sel = val;
290 case VIRTIO_PCI_QUEUE_NOTIFY:
291 if (val < VIRTIO_PCI_QUEUE_MAX) {
292 virtio_queue_notify(vdev, val);
295 case VIRTIO_PCI_STATUS:
296 if (!(val & VIRTIO_CONFIG_S_DRIVER_OK)) {
297 virtio_pci_stop_ioeventfd(s);
300 virtio_set_status(vdev, val & 0xFF);
302 if (val & VIRTIO_CONFIG_S_DRIVER_OK) {
303 virtio_pci_start_ioeventfd(s);
306 if (vdev->status == 0) {
307 virtio_reset(s->vdev);
308 msix_unuse_all_vectors(&s->pci_dev);
311 /* Linux before 2.6.34 sets the device as OK without enabling
312 the PCI device bus master bit. In this case we need to disable
313 some safety checks. */
314 if ((val & VIRTIO_CONFIG_S_DRIVER_OK) &&
315 !(s->pci_dev.config[PCI_COMMAND] & PCI_COMMAND_MASTER)) {
316 s->flags |= VIRTIO_PCI_FLAG_BUS_MASTER_BUG;
319 case VIRTIO_MSI_CONFIG_VECTOR:
320 msix_vector_unuse(&s->pci_dev, vdev->config_vector);
321 /* Make it possible for guest to discover an error took place. */
322 if (msix_vector_use(&s->pci_dev, val) < 0)
323 val = VIRTIO_NO_VECTOR;
324 vdev->config_vector = val;
326 case VIRTIO_MSI_QUEUE_VECTOR:
327 msix_vector_unuse(&s->pci_dev,
328 virtio_queue_vector(vdev, vdev->queue_sel));
329 /* Make it possible for guest to discover an error took place. */
330 if (msix_vector_use(&s->pci_dev, val) < 0)
331 val = VIRTIO_NO_VECTOR;
332 virtio_queue_set_vector(vdev, vdev->queue_sel, val);
335 error_report("%s: unexpected address 0x%x value 0x%x",
336 __func__, addr, val);
341 static uint32_t virtio_ioport_read(VirtIOPCI *s, uint32_t addr)
343 VirtIODevice *vdev = s->vdev;
344 uint32_t ret = 0xFFFFFFFF;
347 case VIRTIO_PCI_HOST_FEATURES:
348 ret = s->host_features;
350 case VIRTIO_PCI_GUEST_FEATURES:
351 ret = vdev->guest_features;
353 case VIRTIO_PCI_QUEUE_PFN:
354 ret = virtio_queue_get_addr(vdev, vdev->queue_sel)
355 >> VIRTIO_PCI_QUEUE_ADDR_SHIFT;
357 case VIRTIO_PCI_QUEUE_NUM:
358 ret = virtio_queue_get_num(vdev, vdev->queue_sel);
360 case VIRTIO_PCI_QUEUE_SEL:
361 ret = vdev->queue_sel;
363 case VIRTIO_PCI_STATUS:
367 /* reading from the ISR also clears it. */
370 qemu_set_irq(s->pci_dev.irq[0], 0);
372 case VIRTIO_MSI_CONFIG_VECTOR:
373 ret = vdev->config_vector;
375 case VIRTIO_MSI_QUEUE_VECTOR:
376 ret = virtio_queue_vector(vdev, vdev->queue_sel);
385 static uint32_t virtio_pci_config_readb(void *opaque, uint32_t addr)
387 VirtIOPCI *s = opaque;
388 uint32_t config = VIRTIO_PCI_CONFIG(&s->pci_dev);
390 return virtio_ioport_read(s, addr);
392 return virtio_config_readb(s->vdev, addr);
395 static uint32_t virtio_pci_config_readw(void *opaque, uint32_t addr)
397 VirtIOPCI *s = opaque;
398 uint32_t config = VIRTIO_PCI_CONFIG(&s->pci_dev);
401 return virtio_ioport_read(s, addr);
403 val = virtio_config_readw(s->vdev, addr);
404 if (virtio_is_big_endian()) {
406 * virtio is odd, ioports are LE but config space is target native
407 * endian. However, in qemu, all PIO is LE, so we need to re-swap
415 static uint32_t virtio_pci_config_readl(void *opaque, uint32_t addr)
417 VirtIOPCI *s = opaque;
418 uint32_t config = VIRTIO_PCI_CONFIG(&s->pci_dev);
421 return virtio_ioport_read(s, addr);
423 val = virtio_config_readl(s->vdev, addr);
424 if (virtio_is_big_endian()) {
430 static void virtio_pci_config_writeb(void *opaque, uint32_t addr, uint32_t val)
432 VirtIOPCI *s = opaque;
433 uint32_t config = VIRTIO_PCI_CONFIG(&s->pci_dev);
435 virtio_ioport_write(s, addr, val);
439 virtio_config_writeb(s->vdev, addr, val);
442 static void virtio_pci_config_writew(void *opaque, uint32_t addr, uint32_t val)
444 VirtIOPCI *s = opaque;
445 uint32_t config = VIRTIO_PCI_CONFIG(&s->pci_dev);
447 virtio_ioport_write(s, addr, val);
451 if (virtio_is_big_endian()) {
454 virtio_config_writew(s->vdev, addr, val);
457 static void virtio_pci_config_writel(void *opaque, uint32_t addr, uint32_t val)
459 VirtIOPCI *s = opaque;
460 uint32_t config = VIRTIO_PCI_CONFIG(&s->pci_dev);
462 virtio_ioport_write(s, addr, val);
466 if (virtio_is_big_endian()) {
469 virtio_config_writel(s->vdev, addr, val);
472 static const MemoryRegionPortio virtio_portio[] = {
473 { 0, 0x10000, 1, .write = virtio_pci_config_writeb, },
474 { 0, 0x10000, 2, .write = virtio_pci_config_writew, },
475 { 0, 0x10000, 4, .write = virtio_pci_config_writel, },
476 { 0, 0x10000, 1, .read = virtio_pci_config_readb, },
477 { 0, 0x10000, 2, .read = virtio_pci_config_readw, },
478 { 0, 0x10000, 4, .read = virtio_pci_config_readl, },
482 static const MemoryRegionOps virtio_pci_config_ops = {
483 .old_portio = virtio_portio,
484 .endianness = DEVICE_LITTLE_ENDIAN,
487 static void virtio_write_config(PCIDevice *pci_dev, uint32_t address,
488 uint32_t val, int len)
490 VirtIOPCI *s = DO_UPCAST(VirtIOPCI,
493 pci_default_write_config(pci_dev, address, val, len);
495 if (range_covers_byte(address, len, PCI_COMMAND) &&
496 !(pci_dev->config[PCI_COMMAND] & PCI_COMMAND_MASTER) &&
497 !(s->flags & VIRTIO_PCI_FLAG_BUS_MASTER_BUG)) {
498 virtio_pci_stop_ioeventfd(s);
499 virtio_set_status(s->vdev,
500 s->vdev->status & ~VIRTIO_CONFIG_S_DRIVER_OK);
504 static unsigned virtio_pci_get_features(void *opaque)
506 VirtIOPCI *s = opaque;
507 return s->host_features;
510 static int kvm_virtio_pci_vq_vector_use(VirtIOPCI *s,
511 unsigned int queue_no,
515 VirtQueue *vq = virtio_get_queue(s->vdev, queue_no);
516 EventNotifier *n = virtio_queue_get_guest_notifier(vq);
517 VirtIOIRQFD *irqfd = &s->vector_irqfd[vector];
520 if (irqfd->users == 0) {
521 ret = kvm_irqchip_add_msi_route(kvm_state, msg);
529 ret = kvm_irqchip_add_irq_notifier(kvm_state, n, irqfd->virq);
531 if (--irqfd->users == 0) {
532 kvm_irqchip_release_virq(kvm_state, irqfd->virq);
537 virtio_queue_set_guest_notifier_fd_handler(vq, true, true);
541 static void kvm_virtio_pci_vq_vector_release(VirtIOPCI *s,
542 unsigned int queue_no,
545 VirtQueue *vq = virtio_get_queue(s->vdev, queue_no);
546 EventNotifier *n = virtio_queue_get_guest_notifier(vq);
547 VirtIOIRQFD *irqfd = &s->vector_irqfd[vector];
550 ret = kvm_irqchip_remove_irq_notifier(kvm_state, n, irqfd->virq);
553 if (--irqfd->users == 0) {
554 kvm_irqchip_release_virq(kvm_state, irqfd->virq);
557 virtio_queue_set_guest_notifier_fd_handler(vq, true, false);
560 static int kvm_virtio_pci_vector_use(PCIDevice *dev, unsigned vector,
563 VirtIOPCI *s = container_of(dev, VirtIOPCI, pci_dev);
564 VirtIODevice *vdev = s->vdev;
567 for (queue_no = 0; queue_no < VIRTIO_PCI_QUEUE_MAX; queue_no++) {
568 if (!virtio_queue_get_num(vdev, queue_no)) {
571 if (virtio_queue_vector(vdev, queue_no) != vector) {
574 ret = kvm_virtio_pci_vq_vector_use(s, queue_no, vector, msg);
582 while (--queue_no >= 0) {
583 if (virtio_queue_vector(vdev, queue_no) != vector) {
586 kvm_virtio_pci_vq_vector_release(s, queue_no, vector);
591 static void kvm_virtio_pci_vector_release(PCIDevice *dev, unsigned vector)
593 VirtIOPCI *s = container_of(dev, VirtIOPCI, pci_dev);
594 VirtIODevice *vdev = s->vdev;
597 for (queue_no = 0; queue_no < VIRTIO_PCI_QUEUE_MAX; queue_no++) {
598 if (!virtio_queue_get_num(vdev, queue_no)) {
601 if (virtio_queue_vector(vdev, queue_no) != vector) {
604 kvm_virtio_pci_vq_vector_release(s, queue_no, vector);
608 static int virtio_pci_set_guest_notifier(void *opaque, int n, bool assign)
610 VirtIOPCI *s = opaque;
611 VirtQueue *vq = virtio_get_queue(s->vdev, n);
612 EventNotifier *notifier = virtio_queue_get_guest_notifier(vq);
615 int r = event_notifier_init(notifier, 0);
619 virtio_queue_set_guest_notifier_fd_handler(vq, true, false);
621 virtio_queue_set_guest_notifier_fd_handler(vq, false, false);
622 event_notifier_cleanup(notifier);
628 static bool virtio_pci_query_guest_notifiers(void *opaque)
630 VirtIOPCI *s = opaque;
631 return msix_enabled(&s->pci_dev);
634 static int virtio_pci_set_guest_notifiers(void *opaque, bool assign)
636 VirtIOPCI *s = opaque;
637 VirtIODevice *vdev = s->vdev;
640 /* Must unset vector notifier while guest notifier is still assigned */
641 if (kvm_msi_via_irqfd_enabled() && !assign) {
642 msix_unset_vector_notifiers(&s->pci_dev);
643 g_free(s->vector_irqfd);
644 s->vector_irqfd = NULL;
647 for (n = 0; n < VIRTIO_PCI_QUEUE_MAX; n++) {
648 if (!virtio_queue_get_num(vdev, n)) {
652 r = virtio_pci_set_guest_notifier(opaque, n, assign);
658 /* Must set vector notifier after guest notifier has been assigned */
659 if (kvm_msi_via_irqfd_enabled() && assign) {
661 g_malloc0(sizeof(*s->vector_irqfd) *
662 msix_nr_vectors_allocated(&s->pci_dev));
663 r = msix_set_vector_notifiers(&s->pci_dev,
664 kvm_virtio_pci_vector_use,
665 kvm_virtio_pci_vector_release);
674 /* We get here on assignment failure. Recover by undoing for VQs 0 .. n. */
677 virtio_pci_set_guest_notifier(opaque, n, !assign);
682 static int virtio_pci_set_host_notifier(void *opaque, int n, bool assign)
684 VirtIOPCI *s = opaque;
686 /* Stop using ioeventfd for virtqueue kick if the device starts using host
687 * notifiers. This makes it easy to avoid stepping on each others' toes.
689 s->ioeventfd_disabled = assign;
691 virtio_pci_stop_ioeventfd(s);
693 /* We don't need to start here: it's not needed because backend
694 * currently only stops on status change away from ok,
695 * reset, vmstop and such. If we do add code to start here,
696 * need to check vmstate, device state etc. */
697 return virtio_pci_set_host_notifier_internal(s, n, assign, false);
700 static void virtio_pci_vmstate_change(void *opaque, bool running)
702 VirtIOPCI *s = opaque;
705 /* Try to find out if the guest has bus master disabled, but is
706 in ready state. Then we have a buggy guest OS. */
707 if ((s->vdev->status & VIRTIO_CONFIG_S_DRIVER_OK) &&
708 !(s->pci_dev.config[PCI_COMMAND] & PCI_COMMAND_MASTER)) {
709 s->flags |= VIRTIO_PCI_FLAG_BUS_MASTER_BUG;
711 virtio_pci_start_ioeventfd(s);
713 virtio_pci_stop_ioeventfd(s);
717 static const VirtIOBindings virtio_pci_bindings = {
718 .notify = virtio_pci_notify,
719 .save_config = virtio_pci_save_config,
720 .load_config = virtio_pci_load_config,
721 .save_queue = virtio_pci_save_queue,
722 .load_queue = virtio_pci_load_queue,
723 .get_features = virtio_pci_get_features,
724 .query_guest_notifiers = virtio_pci_query_guest_notifiers,
725 .set_host_notifier = virtio_pci_set_host_notifier,
726 .set_guest_notifiers = virtio_pci_set_guest_notifiers,
727 .vmstate_change = virtio_pci_vmstate_change,
730 static void virtio_init_pci_(VirtIOPCI *s, VirtIODevice *vdev)
737 config = s->pci_dev.config;
740 pci_config_set_class(config, s->class_code);
742 pci_set_word(config + PCI_SUBSYSTEM_VENDOR_ID,
743 pci_get_word(config + PCI_VENDOR_ID));
744 pci_set_word(config + PCI_SUBSYSTEM_ID, vdev->device_id);
745 config[PCI_INTERRUPT_PIN] = 1;
747 if (vdev->nvectors &&
748 msix_init_exclusive_bar(&s->pci_dev, vdev->nvectors, 1)) {
752 s->pci_dev.config_write = virtio_write_config;
754 size = VIRTIO_PCI_REGION_SIZE(&s->pci_dev) + vdev->config_len;
756 size = 1 << qemu_fls(size);
758 memory_region_init_io(&s->bar, &virtio_pci_config_ops, s,
760 pci_register_bar(&s->pci_dev, 0, PCI_BASE_ADDRESS_SPACE_IO,
763 if (!kvm_has_many_ioeventfds()) {
764 s->flags &= ~VIRTIO_PCI_FLAG_USE_IOEVENTFD;
767 virtio_bind_device(vdev, &virtio_pci_bindings, s);
768 s->host_features |= 0x1 << VIRTIO_F_NOTIFY_ON_EMPTY;
769 s->host_features |= 0x1 << VIRTIO_F_BAD_FEATURE;
770 s->host_features = vdev->get_features(vdev, s->host_features);
773 static void virtio_exit_pci(PCIDevice *pci_dev)
775 VirtIOPCI *s = DO_UPCAST(VirtIOPCI, pci_dev, pci_dev);
777 memory_region_destroy(&s->bar);
778 msix_uninit_exclusive_bar(pci_dev);
781 static int virtio_pci_transport_cb(DeviceState *dev, VirtIODevice *vdev,
782 VirtIOTransportLink *trl)
784 PCIDevice *pci_dev = DO_UPCAST(PCIDevice, qdev, trl->tr);
785 VirtIOPCI *s = DO_UPCAST(VirtIOPCI, pci_dev, pci_dev);
787 virtio_plug_into_transport(dev, trl);
789 // TODO: Figure out if props were explicitly set before
791 /* Get default host_features passed from back-end */
792 s->host_features = s->trl->host_features;
794 switch (vdev->device_id) {
795 case VIRTIO_ID_BLOCK:
796 s->flags |= VIRTIO_PCI_FLAG_USE_IOEVENTFD;
799 if (s->class_code != PCI_CLASS_STORAGE_SCSI &&
800 s->class_code != PCI_CLASS_STORAGE_OTHER) {
801 s->class_code = PCI_CLASS_STORAGE_SCSI;
804 vdev->nvectors = s->nvectors;
805 pci_config_set_device_id(s->pci_dev.config, PCI_DEVICE_ID_VIRTIO_BLOCK);
806 pci_config_set_class(s->pci_dev.config, PCI_CLASS_STORAGE_SCSI);
807 virtio_init_pci_(s, vdev);
808 s->nvectors = vdev->nvectors;
814 pci_dev->romfile = g_strdup("pxe-virtio.rom");
815 pci_add_option_rom(pci_dev, false);
817 vdev->nvectors = s->nvectors;
818 pci_config_set_device_id(s->pci_dev.config, PCI_DEVICE_ID_VIRTIO_NET);
819 pci_config_set_class(s->pci_dev.config, PCI_CLASS_NETWORK_ETHERNET);
820 virtio_init_pci_(s, vdev);
821 s->nvectors = vdev->nvectors;
823 case VIRTIO_ID_BALLOON:
827 case VIRTIO_ID_CONSOLE:
833 "Unknown back-end device id: 0x%" PRIx16 "\n", vdev->device_id);
840 static int virtio_pci_device_init(PCIDevice *pci_dev)
843 DO_UPCAST(VirtIOPCI, pci_dev, pci_dev);
845 virtio_init_transport(&pci_dev->qdev, &s->trl, VIRTIO_PCI,
846 virtio_pci_transport_cb);
851 static void virtio_pci_device_exit(PCIDevice *pci_dev)
854 DO_UPCAST(VirtIOPCI, pci_dev, pci_dev);
856 switch (s->vdev->device_id) {
857 case VIRTIO_ID_BLOCK:
858 virtio_pci_stop_ioeventfd(s);
859 virtio_blk_exit(s->vdev);
862 virtio_pci_stop_ioeventfd(s);
863 virtio_net_exit(s->vdev);
865 case VIRTIO_ID_BALLOON:
866 virtio_pci_stop_ioeventfd(s);
867 virtio_balloon_exit(s->vdev);
870 virtio_scsi_exit(s->vdev);
872 case VIRTIO_ID_CONSOLE:
873 virtio_pci_stop_ioeventfd(s);
874 virtio_serial_exit(s->vdev);
878 "Unknown back-end device id: 0x%" PRIx16 "\n",
883 virtio_exit_pci(pci_dev);
888 /******************** VirtIOPCI Device **********************/
890 static Property virtio_pci_properties[] = {
891 DEFINE_PROP_HEX32("class", VirtIOPCI, class_code, 0),
892 DEFINE_PROP_BIT("ioeventfd", VirtIOPCI, flags,
893 VIRTIO_PCI_FLAG_USE_IOEVENTFD_BIT, false),
894 DEFINE_PROP_UINT32("vectors", VirtIOPCI, nvectors, 0),
895 DEFINE_PROP_END_OF_LIST(),
898 static void virtio_pci_class_init(ObjectClass *klass, void *data)
900 DeviceClass *dc = DEVICE_CLASS(klass);
901 PCIDeviceClass *k = PCI_DEVICE_CLASS(klass);
902 k->init = virtio_pci_device_init;
903 k->exit = virtio_pci_device_exit;
904 k->vendor_id = PCI_VENDOR_ID_REDHAT_QUMRANET;
905 k->revision = VIRTIO_PCI_ABI_VERSION;
906 k->class_id = PCI_CLASS_OTHERS;
907 dc->reset = virtio_pci_reset_;
908 dc->props = virtio_pci_properties;
911 static TypeInfo virtio_pci_info = {
913 .parent = TYPE_PCI_DEVICE,
914 .instance_size = sizeof(VirtIOPCI),
915 .class_init = virtio_pci_class_init,
918 /************************************************************/
920 static void virtio_pci_register_types(void)
922 type_register_static(&virtio_pci_info);
925 type_init(virtio_pci_register_types)