Michael S. Tsirkin | 38eb4a2 | 2014-12-07 18:41:16 +0200 | [diff] [blame^] | 1 | #ifndef _DRIVERS_VIRTIO_VIRTIO_PCI_H |
| 2 | #define _DRIVERS_VIRTIO_VIRTIO_PCI_H |
| 3 | /* |
| 4 | * Virtio PCI driver |
| 5 | * |
| 6 | * This module allows virtio devices to be used over a virtual PCI device. |
| 7 | * This can be used with QEMU based VMMs like KVM or Xen. |
| 8 | * |
| 9 | * Copyright IBM Corp. 2007 |
| 10 | * |
| 11 | * Authors: |
| 12 | * Anthony Liguori <aliguori@us.ibm.com> |
| 13 | * |
| 14 | * This work is licensed under the terms of the GNU GPL, version 2 or later. |
| 15 | * See the COPYING file in the top-level directory. |
| 16 | * |
| 17 | */ |
| 18 | |
| 19 | #include <linux/module.h> |
| 20 | #include <linux/list.h> |
| 21 | #include <linux/pci.h> |
| 22 | #include <linux/slab.h> |
| 23 | #include <linux/interrupt.h> |
| 24 | #include <linux/virtio.h> |
| 25 | #include <linux/virtio_config.h> |
| 26 | #include <linux/virtio_ring.h> |
| 27 | #define VIRTIO_PCI_NO_LEGACY |
| 28 | #include <linux/virtio_pci.h> |
| 29 | #include <linux/highmem.h> |
| 30 | #include <linux/spinlock.h> |
| 31 | |
| 32 | struct virtio_pci_vq_info { |
| 33 | /* the actual virtqueue */ |
| 34 | struct virtqueue *vq; |
| 35 | |
| 36 | /* the number of entries in the queue */ |
| 37 | int num; |
| 38 | |
| 39 | /* the virtual address of the ring queue */ |
| 40 | void *queue; |
| 41 | |
| 42 | /* the list node for the virtqueues list */ |
| 43 | struct list_head node; |
| 44 | |
| 45 | /* MSI-X vector (or none) */ |
| 46 | unsigned msix_vector; |
| 47 | }; |
| 48 | |
| 49 | /* Our device structure */ |
| 50 | struct virtio_pci_device { |
| 51 | struct virtio_device vdev; |
| 52 | struct pci_dev *pci_dev; |
| 53 | |
| 54 | /* the IO mapping for the PCI config space */ |
| 55 | void __iomem *ioaddr; |
| 56 | |
| 57 | /* the IO mapping for ISR operation */ |
| 58 | void __iomem *isr; |
| 59 | |
| 60 | /* a list of queues so we can dispatch IRQs */ |
| 61 | spinlock_t lock; |
| 62 | struct list_head virtqueues; |
| 63 | |
| 64 | /* array of all queues for house-keeping */ |
| 65 | struct virtio_pci_vq_info **vqs; |
| 66 | |
| 67 | /* MSI-X support */ |
| 68 | int msix_enabled; |
| 69 | int intx_enabled; |
| 70 | struct msix_entry *msix_entries; |
| 71 | cpumask_var_t *msix_affinity_masks; |
| 72 | /* Name strings for interrupts. This size should be enough, |
| 73 | * and I'm too lazy to allocate each name separately. */ |
| 74 | char (*msix_names)[256]; |
| 75 | /* Number of available vectors */ |
| 76 | unsigned msix_vectors; |
| 77 | /* Vectors allocated, excluding per-vq vectors if any */ |
| 78 | unsigned msix_used_vectors; |
| 79 | |
| 80 | /* Whether we have vector per vq */ |
| 81 | bool per_vq_vectors; |
| 82 | |
| 83 | struct virtqueue *(*setup_vq)(struct virtio_pci_device *vp_dev, |
| 84 | struct virtio_pci_vq_info *info, |
| 85 | unsigned idx, |
| 86 | void (*callback)(struct virtqueue *vq), |
| 87 | const char *name, |
| 88 | u16 msix_vec); |
| 89 | void (*del_vq)(struct virtio_pci_vq_info *info); |
| 90 | |
| 91 | u16 (*config_vector)(struct virtio_pci_device *vp_dev, u16 vector); |
| 92 | }; |
| 93 | |
| 94 | /* Constants for MSI-X */ |
| 95 | /* Use first vector for configuration changes, second and the rest for |
| 96 | * virtqueues Thus, we need at least 2 vectors for MSI. */ |
| 97 | enum { |
| 98 | VP_MSIX_CONFIG_VECTOR = 0, |
| 99 | VP_MSIX_VQ_VECTOR = 1, |
| 100 | }; |
| 101 | |
| 102 | /* Convert a generic virtio device to our structure */ |
| 103 | static struct virtio_pci_device *to_vp_device(struct virtio_device *vdev) |
| 104 | { |
| 105 | return container_of(vdev, struct virtio_pci_device, vdev); |
| 106 | } |
| 107 | |
| 108 | /* wait for pending irq handlers */ |
| 109 | void vp_synchronize_vectors(struct virtio_device *vdev); |
| 110 | /* the notify function used when creating a virt queue */ |
| 111 | bool vp_notify(struct virtqueue *vq); |
| 112 | /* the config->del_vqs() implementation */ |
| 113 | void vp_del_vqs(struct virtio_device *vdev); |
| 114 | /* the config->find_vqs() implementation */ |
| 115 | int vp_find_vqs(struct virtio_device *vdev, unsigned nvqs, |
| 116 | struct virtqueue *vqs[], |
| 117 | vq_callback_t *callbacks[], |
| 118 | const char *names[]); |
| 119 | const char *vp_bus_name(struct virtio_device *vdev); |
| 120 | |
| 121 | /* Setup the affinity for a virtqueue: |
| 122 | * - force the affinity for per vq vector |
| 123 | * - OR over all affinities for shared MSI |
| 124 | * - ignore the affinity request if we're using INTX |
| 125 | */ |
| 126 | int vp_set_vq_affinity(struct virtqueue *vq, int cpu); |
| 127 | void virtio_pci_release_dev(struct device *); |
| 128 | |
| 129 | #ifdef CONFIG_PM_SLEEP |
| 130 | extern const struct dev_pm_ops virtio_pci_pm_ops; |
| 131 | #endif |
| 132 | |
| 133 | #endif |