Anthony Liguori | 3343660 | 2007-11-12 21:30:26 -0600 | [diff] [blame] | 1 | /* |
| 2 | * Virtio PCI driver |
| 3 | * |
| 4 | * This module allows virtio devices to be used over a virtual PCI device. |
| 5 | * This can be used with QEMU based VMMs like KVM or Xen. |
| 6 | * |
| 7 | * Copyright IBM Corp. 2007 |
| 8 | * |
| 9 | * Authors: |
| 10 | * Anthony Liguori <aliguori@us.ibm.com> |
| 11 | * |
| 12 | * This work is licensed under the terms of the GNU GPL, version 2 or later. |
| 13 | * See the COPYING file in the top-level directory. |
| 14 | * |
| 15 | */ |
| 16 | |
| 17 | #include <linux/module.h> |
| 18 | #include <linux/list.h> |
| 19 | #include <linux/pci.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 20 | #include <linux/slab.h> |
Anthony Liguori | 3343660 | 2007-11-12 21:30:26 -0600 | [diff] [blame] | 21 | #include <linux/interrupt.h> |
| 22 | #include <linux/virtio.h> |
| 23 | #include <linux/virtio_config.h> |
| 24 | #include <linux/virtio_ring.h> |
| 25 | #include <linux/virtio_pci.h> |
| 26 | #include <linux/highmem.h> |
| 27 | #include <linux/spinlock.h> |
| 28 | |
| 29 | MODULE_AUTHOR("Anthony Liguori <aliguori@us.ibm.com>"); |
| 30 | MODULE_DESCRIPTION("virtio-pci"); |
| 31 | MODULE_LICENSE("GPL"); |
| 32 | MODULE_VERSION("1"); |
| 33 | |
| 34 | /* Our device structure */ |
| 35 | struct virtio_pci_device |
| 36 | { |
| 37 | struct virtio_device vdev; |
| 38 | struct pci_dev *pci_dev; |
| 39 | |
| 40 | /* the IO mapping for the PCI config space */ |
Al Viro | 9796835 | 2008-03-29 03:09:48 +0000 | [diff] [blame] | 41 | void __iomem *ioaddr; |
Anthony Liguori | 3343660 | 2007-11-12 21:30:26 -0600 | [diff] [blame] | 42 | |
| 43 | /* a list of queues so we can dispatch IRQs */ |
| 44 | spinlock_t lock; |
| 45 | struct list_head virtqueues; |
Michael S. Tsirkin | 82af8ce | 2009-05-14 13:55:41 +0300 | [diff] [blame] | 46 | |
| 47 | /* MSI-X support */ |
| 48 | int msix_enabled; |
| 49 | int intx_enabled; |
| 50 | struct msix_entry *msix_entries; |
| 51 | /* Name strings for interrupts. This size should be enough, |
| 52 | * and I'm too lazy to allocate each name separately. */ |
| 53 | char (*msix_names)[256]; |
| 54 | /* Number of available vectors */ |
| 55 | unsigned msix_vectors; |
Michael S. Tsirkin | e969fed | 2009-07-26 15:48:08 +0300 | [diff] [blame] | 56 | /* Vectors allocated, excluding per-vq vectors if any */ |
Michael S. Tsirkin | 82af8ce | 2009-05-14 13:55:41 +0300 | [diff] [blame] | 57 | unsigned msix_used_vectors; |
Michael S. Tsirkin | e969fed | 2009-07-26 15:48:08 +0300 | [diff] [blame] | 58 | /* Whether we have vector per vq */ |
| 59 | bool per_vq_vectors; |
Michael S. Tsirkin | 82af8ce | 2009-05-14 13:55:41 +0300 | [diff] [blame] | 60 | }; |
| 61 | |
| 62 | /* Constants for MSI-X */ |
| 63 | /* Use first vector for configuration changes, second and the rest for |
| 64 | * virtqueues Thus, we need at least 2 vectors for MSI. */ |
| 65 | enum { |
| 66 | VP_MSIX_CONFIG_VECTOR = 0, |
| 67 | VP_MSIX_VQ_VECTOR = 1, |
Anthony Liguori | 3343660 | 2007-11-12 21:30:26 -0600 | [diff] [blame] | 68 | }; |
| 69 | |
| 70 | struct virtio_pci_vq_info |
| 71 | { |
| 72 | /* the actual virtqueue */ |
| 73 | struct virtqueue *vq; |
| 74 | |
| 75 | /* the number of entries in the queue */ |
| 76 | int num; |
| 77 | |
| 78 | /* the index of the queue */ |
| 79 | int queue_index; |
| 80 | |
| 81 | /* the virtual address of the ring queue */ |
| 82 | void *queue; |
| 83 | |
| 84 | /* the list node for the virtqueues list */ |
| 85 | struct list_head node; |
Michael S. Tsirkin | 82af8ce | 2009-05-14 13:55:41 +0300 | [diff] [blame] | 86 | |
| 87 | /* MSI-X vector (or none) */ |
Rusty Russell | f68d240 | 2009-09-23 22:26:29 -0600 | [diff] [blame] | 88 | unsigned msix_vector; |
Anthony Liguori | 3343660 | 2007-11-12 21:30:26 -0600 | [diff] [blame] | 89 | }; |
| 90 | |
| 91 | /* Qumranet donated their vendor ID for devices 0x1000 thru 0x10FF. */ |
| 92 | static struct pci_device_id virtio_pci_id_table[] = { |
| 93 | { 0x1af4, PCI_ANY_ID, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0 }, |
| 94 | { 0 }, |
| 95 | }; |
| 96 | |
| 97 | MODULE_DEVICE_TABLE(pci, virtio_pci_id_table); |
| 98 | |
| 99 | /* A PCI device has it's own struct device and so does a virtio device so |
| 100 | * we create a place for the virtio devices to show up in sysfs. I think it |
| 101 | * would make more sense for virtio to not insist on having it's own device. */ |
Mark McLoughlin | 63d1255 | 2008-12-15 12:58:27 +0000 | [diff] [blame] | 102 | static struct device *virtio_pci_root; |
Anthony Liguori | 3343660 | 2007-11-12 21:30:26 -0600 | [diff] [blame] | 103 | |
Anthony Liguori | 3343660 | 2007-11-12 21:30:26 -0600 | [diff] [blame] | 104 | /* Convert a generic virtio device to our structure */ |
| 105 | static struct virtio_pci_device *to_vp_device(struct virtio_device *vdev) |
| 106 | { |
| 107 | return container_of(vdev, struct virtio_pci_device, vdev); |
| 108 | } |
| 109 | |
Rusty Russell | c45a681 | 2008-05-02 21:50:50 -0500 | [diff] [blame] | 110 | /* virtio config->get_features() implementation */ |
| 111 | static u32 vp_get_features(struct virtio_device *vdev) |
Anthony Liguori | 3343660 | 2007-11-12 21:30:26 -0600 | [diff] [blame] | 112 | { |
| 113 | struct virtio_pci_device *vp_dev = to_vp_device(vdev); |
Anthony Liguori | 3343660 | 2007-11-12 21:30:26 -0600 | [diff] [blame] | 114 | |
Rusty Russell | c45a681 | 2008-05-02 21:50:50 -0500 | [diff] [blame] | 115 | /* When someone needs more than 32 feature bits, we'll need to |
| 116 | * steal a bit to indicate that the rest are somewhere else. */ |
| 117 | return ioread32(vp_dev->ioaddr + VIRTIO_PCI_HOST_FEATURES); |
| 118 | } |
Anthony Liguori | 3343660 | 2007-11-12 21:30:26 -0600 | [diff] [blame] | 119 | |
Rusty Russell | c624896 | 2008-07-25 12:06:07 -0500 | [diff] [blame] | 120 | /* virtio config->finalize_features() implementation */ |
| 121 | static void vp_finalize_features(struct virtio_device *vdev) |
Rusty Russell | c45a681 | 2008-05-02 21:50:50 -0500 | [diff] [blame] | 122 | { |
| 123 | struct virtio_pci_device *vp_dev = to_vp_device(vdev); |
| 124 | |
Rusty Russell | e34f872 | 2008-07-25 12:06:13 -0500 | [diff] [blame] | 125 | /* Give virtio_ring a chance to accept features. */ |
| 126 | vring_transport_features(vdev); |
| 127 | |
Rusty Russell | c624896 | 2008-07-25 12:06:07 -0500 | [diff] [blame] | 128 | /* We only support 32 feature bits. */ |
| 129 | BUILD_BUG_ON(ARRAY_SIZE(vdev->features) != 1); |
| 130 | iowrite32(vdev->features[0], vp_dev->ioaddr+VIRTIO_PCI_GUEST_FEATURES); |
Anthony Liguori | 3343660 | 2007-11-12 21:30:26 -0600 | [diff] [blame] | 131 | } |
| 132 | |
| 133 | /* virtio config->get() implementation */ |
| 134 | static void vp_get(struct virtio_device *vdev, unsigned offset, |
| 135 | void *buf, unsigned len) |
| 136 | { |
| 137 | struct virtio_pci_device *vp_dev = to_vp_device(vdev); |
Michael S. Tsirkin | 82af8ce | 2009-05-14 13:55:41 +0300 | [diff] [blame] | 138 | void __iomem *ioaddr = vp_dev->ioaddr + |
| 139 | VIRTIO_PCI_CONFIG(vp_dev) + offset; |
Anthony Liguori | 3343660 | 2007-11-12 21:30:26 -0600 | [diff] [blame] | 140 | u8 *ptr = buf; |
| 141 | int i; |
| 142 | |
| 143 | for (i = 0; i < len; i++) |
| 144 | ptr[i] = ioread8(ioaddr + i); |
| 145 | } |
| 146 | |
| 147 | /* the config->set() implementation. it's symmetric to the config->get() |
| 148 | * implementation */ |
| 149 | static void vp_set(struct virtio_device *vdev, unsigned offset, |
| 150 | const void *buf, unsigned len) |
| 151 | { |
| 152 | struct virtio_pci_device *vp_dev = to_vp_device(vdev); |
Michael S. Tsirkin | 82af8ce | 2009-05-14 13:55:41 +0300 | [diff] [blame] | 153 | void __iomem *ioaddr = vp_dev->ioaddr + |
| 154 | VIRTIO_PCI_CONFIG(vp_dev) + offset; |
Anthony Liguori | 3343660 | 2007-11-12 21:30:26 -0600 | [diff] [blame] | 155 | const u8 *ptr = buf; |
| 156 | int i; |
| 157 | |
| 158 | for (i = 0; i < len; i++) |
| 159 | iowrite8(ptr[i], ioaddr + i); |
| 160 | } |
| 161 | |
| 162 | /* config->{get,set}_status() implementations */ |
| 163 | static u8 vp_get_status(struct virtio_device *vdev) |
| 164 | { |
| 165 | struct virtio_pci_device *vp_dev = to_vp_device(vdev); |
| 166 | return ioread8(vp_dev->ioaddr + VIRTIO_PCI_STATUS); |
| 167 | } |
| 168 | |
| 169 | static void vp_set_status(struct virtio_device *vdev, u8 status) |
| 170 | { |
| 171 | struct virtio_pci_device *vp_dev = to_vp_device(vdev); |
| 172 | /* We should never be setting status to 0. */ |
| 173 | BUG_ON(status == 0); |
Harvey Harrison | 597d56e | 2008-03-31 17:53:55 -0700 | [diff] [blame] | 174 | iowrite8(status, vp_dev->ioaddr + VIRTIO_PCI_STATUS); |
Anthony Liguori | 3343660 | 2007-11-12 21:30:26 -0600 | [diff] [blame] | 175 | } |
| 176 | |
| 177 | static void vp_reset(struct virtio_device *vdev) |
| 178 | { |
| 179 | struct virtio_pci_device *vp_dev = to_vp_device(vdev); |
| 180 | /* 0 status means a reset. */ |
Harvey Harrison | 597d56e | 2008-03-31 17:53:55 -0700 | [diff] [blame] | 181 | iowrite8(0, vp_dev->ioaddr + VIRTIO_PCI_STATUS); |
Anthony Liguori | 3343660 | 2007-11-12 21:30:26 -0600 | [diff] [blame] | 182 | } |
| 183 | |
| 184 | /* the notify function used when creating a virt queue */ |
| 185 | static void vp_notify(struct virtqueue *vq) |
| 186 | { |
| 187 | struct virtio_pci_device *vp_dev = to_vp_device(vq->vdev); |
| 188 | struct virtio_pci_vq_info *info = vq->priv; |
| 189 | |
| 190 | /* we write the queue's selector into the notification register to |
| 191 | * signal the other end */ |
| 192 | iowrite16(info->queue_index, vp_dev->ioaddr + VIRTIO_PCI_QUEUE_NOTIFY); |
| 193 | } |
| 194 | |
Michael S. Tsirkin | 77cf5246 | 2009-05-14 13:55:31 +0300 | [diff] [blame] | 195 | /* Handle a configuration change: Tell driver if it wants to know. */ |
| 196 | static irqreturn_t vp_config_changed(int irq, void *opaque) |
| 197 | { |
| 198 | struct virtio_pci_device *vp_dev = opaque; |
| 199 | struct virtio_driver *drv; |
| 200 | drv = container_of(vp_dev->vdev.dev.driver, |
| 201 | struct virtio_driver, driver); |
| 202 | |
| 203 | if (drv && drv->config_changed) |
| 204 | drv->config_changed(&vp_dev->vdev); |
| 205 | return IRQ_HANDLED; |
| 206 | } |
| 207 | |
| 208 | /* Notify all virtqueues on an interrupt. */ |
| 209 | static irqreturn_t vp_vring_interrupt(int irq, void *opaque) |
| 210 | { |
| 211 | struct virtio_pci_device *vp_dev = opaque; |
| 212 | struct virtio_pci_vq_info *info; |
| 213 | irqreturn_t ret = IRQ_NONE; |
| 214 | unsigned long flags; |
| 215 | |
| 216 | spin_lock_irqsave(&vp_dev->lock, flags); |
| 217 | list_for_each_entry(info, &vp_dev->virtqueues, node) { |
| 218 | if (vring_interrupt(irq, info->vq) == IRQ_HANDLED) |
| 219 | ret = IRQ_HANDLED; |
| 220 | } |
| 221 | spin_unlock_irqrestore(&vp_dev->lock, flags); |
| 222 | |
| 223 | return ret; |
| 224 | } |
| 225 | |
Anthony Liguori | 3343660 | 2007-11-12 21:30:26 -0600 | [diff] [blame] | 226 | /* A small wrapper to also acknowledge the interrupt when it's handled. |
| 227 | * I really need an EIO hook for the vring so I can ack the interrupt once we |
| 228 | * know that we'll be handling the IRQ but before we invoke the callback since |
| 229 | * the callback may notify the host which results in the host attempting to |
| 230 | * raise an interrupt that we would then mask once we acknowledged the |
| 231 | * interrupt. */ |
| 232 | static irqreturn_t vp_interrupt(int irq, void *opaque) |
| 233 | { |
| 234 | struct virtio_pci_device *vp_dev = opaque; |
Anthony Liguori | 3343660 | 2007-11-12 21:30:26 -0600 | [diff] [blame] | 235 | u8 isr; |
| 236 | |
| 237 | /* reading the ISR has the effect of also clearing it so it's very |
| 238 | * important to save off the value. */ |
| 239 | isr = ioread8(vp_dev->ioaddr + VIRTIO_PCI_ISR); |
| 240 | |
| 241 | /* It's definitely not us if the ISR was not high */ |
| 242 | if (!isr) |
| 243 | return IRQ_NONE; |
| 244 | |
| 245 | /* Configuration change? Tell driver if it wants to know. */ |
Michael S. Tsirkin | 77cf5246 | 2009-05-14 13:55:31 +0300 | [diff] [blame] | 246 | if (isr & VIRTIO_PCI_ISR_CONFIG) |
| 247 | vp_config_changed(irq, opaque); |
Anthony Liguori | 3343660 | 2007-11-12 21:30:26 -0600 | [diff] [blame] | 248 | |
Michael S. Tsirkin | 77cf5246 | 2009-05-14 13:55:31 +0300 | [diff] [blame] | 249 | return vp_vring_interrupt(irq, opaque); |
Anthony Liguori | 3343660 | 2007-11-12 21:30:26 -0600 | [diff] [blame] | 250 | } |
| 251 | |
Michael S. Tsirkin | 82af8ce | 2009-05-14 13:55:41 +0300 | [diff] [blame] | 252 | static void vp_free_vectors(struct virtio_device *vdev) |
| 253 | { |
| 254 | struct virtio_pci_device *vp_dev = to_vp_device(vdev); |
| 255 | int i; |
| 256 | |
| 257 | if (vp_dev->intx_enabled) { |
| 258 | free_irq(vp_dev->pci_dev->irq, vp_dev); |
| 259 | vp_dev->intx_enabled = 0; |
| 260 | } |
| 261 | |
| 262 | for (i = 0; i < vp_dev->msix_used_vectors; ++i) |
| 263 | free_irq(vp_dev->msix_entries[i].vector, vp_dev); |
Michael S. Tsirkin | 82af8ce | 2009-05-14 13:55:41 +0300 | [diff] [blame] | 264 | |
| 265 | if (vp_dev->msix_enabled) { |
| 266 | /* Disable the vector used for configuration */ |
| 267 | iowrite16(VIRTIO_MSI_NO_VECTOR, |
| 268 | vp_dev->ioaddr + VIRTIO_MSI_CONFIG_VECTOR); |
| 269 | /* Flush the write out to device */ |
| 270 | ioread16(vp_dev->ioaddr + VIRTIO_MSI_CONFIG_VECTOR); |
| 271 | |
Michael S. Tsirkin | 82af8ce | 2009-05-14 13:55:41 +0300 | [diff] [blame] | 272 | pci_disable_msix(vp_dev->pci_dev); |
Michael S. Tsirkin | ff52c3f | 2009-07-23 14:57:37 +0300 | [diff] [blame] | 273 | vp_dev->msix_enabled = 0; |
| 274 | vp_dev->msix_vectors = 0; |
Michael S. Tsirkin | 82af8ce | 2009-05-14 13:55:41 +0300 | [diff] [blame] | 275 | } |
Michael S. Tsirkin | ff52c3f | 2009-07-23 14:57:37 +0300 | [diff] [blame] | 276 | |
| 277 | vp_dev->msix_used_vectors = 0; |
| 278 | kfree(vp_dev->msix_names); |
| 279 | vp_dev->msix_names = NULL; |
| 280 | kfree(vp_dev->msix_entries); |
| 281 | vp_dev->msix_entries = NULL; |
Michael S. Tsirkin | 82af8ce | 2009-05-14 13:55:41 +0300 | [diff] [blame] | 282 | } |
| 283 | |
Rusty Russell | f68d240 | 2009-09-23 22:26:29 -0600 | [diff] [blame] | 284 | static int vp_request_msix_vectors(struct virtio_device *vdev, int nvectors, |
| 285 | bool per_vq_vectors) |
Michael S. Tsirkin | 82af8ce | 2009-05-14 13:55:41 +0300 | [diff] [blame] | 286 | { |
| 287 | struct virtio_pci_device *vp_dev = to_vp_device(vdev); |
| 288 | const char *name = dev_name(&vp_dev->vdev.dev); |
| 289 | unsigned i, v; |
| 290 | int err = -ENOMEM; |
Michael S. Tsirkin | e969fed | 2009-07-26 15:48:08 +0300 | [diff] [blame] | 291 | |
Michael S. Tsirkin | 82af8ce | 2009-05-14 13:55:41 +0300 | [diff] [blame] | 292 | vp_dev->msix_entries = kmalloc(nvectors * sizeof *vp_dev->msix_entries, |
| 293 | GFP_KERNEL); |
| 294 | if (!vp_dev->msix_entries) |
Michael S. Tsirkin | ff52c3f | 2009-07-23 14:57:37 +0300 | [diff] [blame] | 295 | goto error; |
Michael S. Tsirkin | 82af8ce | 2009-05-14 13:55:41 +0300 | [diff] [blame] | 296 | vp_dev->msix_names = kmalloc(nvectors * sizeof *vp_dev->msix_names, |
| 297 | GFP_KERNEL); |
| 298 | if (!vp_dev->msix_names) |
Michael S. Tsirkin | ff52c3f | 2009-07-23 14:57:37 +0300 | [diff] [blame] | 299 | goto error; |
Michael S. Tsirkin | 82af8ce | 2009-05-14 13:55:41 +0300 | [diff] [blame] | 300 | |
| 301 | for (i = 0; i < nvectors; ++i) |
| 302 | vp_dev->msix_entries[i].entry = i; |
| 303 | |
Rusty Russell | f68d240 | 2009-09-23 22:26:29 -0600 | [diff] [blame] | 304 | /* pci_enable_msix returns positive if we can't get this many. */ |
Michael S. Tsirkin | e969fed | 2009-07-26 15:48:08 +0300 | [diff] [blame] | 305 | err = pci_enable_msix(vp_dev->pci_dev, vp_dev->msix_entries, nvectors); |
| 306 | if (err > 0) |
| 307 | err = -ENOSPC; |
| 308 | if (err) |
| 309 | goto error; |
| 310 | vp_dev->msix_vectors = nvectors; |
| 311 | vp_dev->msix_enabled = 1; |
Michael S. Tsirkin | 82af8ce | 2009-05-14 13:55:41 +0300 | [diff] [blame] | 312 | |
Michael S. Tsirkin | e969fed | 2009-07-26 15:48:08 +0300 | [diff] [blame] | 313 | /* Set the vector used for configuration */ |
| 314 | v = vp_dev->msix_used_vectors; |
| 315 | snprintf(vp_dev->msix_names[v], sizeof *vp_dev->msix_names, |
| 316 | "%s-config", name); |
| 317 | err = request_irq(vp_dev->msix_entries[v].vector, |
| 318 | vp_config_changed, 0, vp_dev->msix_names[v], |
| 319 | vp_dev); |
| 320 | if (err) |
| 321 | goto error; |
| 322 | ++vp_dev->msix_used_vectors; |
Michael S. Tsirkin | 82af8ce | 2009-05-14 13:55:41 +0300 | [diff] [blame] | 323 | |
Michael S. Tsirkin | e969fed | 2009-07-26 15:48:08 +0300 | [diff] [blame] | 324 | iowrite16(v, vp_dev->ioaddr + VIRTIO_MSI_CONFIG_VECTOR); |
| 325 | /* Verify we had enough resources to assign the vector */ |
| 326 | v = ioread16(vp_dev->ioaddr + VIRTIO_MSI_CONFIG_VECTOR); |
| 327 | if (v == VIRTIO_MSI_NO_VECTOR) { |
| 328 | err = -EBUSY; |
| 329 | goto error; |
Michael S. Tsirkin | 82af8ce | 2009-05-14 13:55:41 +0300 | [diff] [blame] | 330 | } |
| 331 | |
Michael S. Tsirkin | e969fed | 2009-07-26 15:48:08 +0300 | [diff] [blame] | 332 | if (!per_vq_vectors) { |
Michael S. Tsirkin | 82af8ce | 2009-05-14 13:55:41 +0300 | [diff] [blame] | 333 | /* Shared vector for all VQs */ |
| 334 | v = vp_dev->msix_used_vectors; |
| 335 | snprintf(vp_dev->msix_names[v], sizeof *vp_dev->msix_names, |
| 336 | "%s-virtqueues", name); |
| 337 | err = request_irq(vp_dev->msix_entries[v].vector, |
| 338 | vp_vring_interrupt, 0, vp_dev->msix_names[v], |
| 339 | vp_dev); |
| 340 | if (err) |
Michael S. Tsirkin | ff52c3f | 2009-07-23 14:57:37 +0300 | [diff] [blame] | 341 | goto error; |
Michael S. Tsirkin | 82af8ce | 2009-05-14 13:55:41 +0300 | [diff] [blame] | 342 | ++vp_dev->msix_used_vectors; |
| 343 | } |
| 344 | return 0; |
Michael S. Tsirkin | ff52c3f | 2009-07-23 14:57:37 +0300 | [diff] [blame] | 345 | error: |
Michael S. Tsirkin | 82af8ce | 2009-05-14 13:55:41 +0300 | [diff] [blame] | 346 | vp_free_vectors(vdev); |
Michael S. Tsirkin | 82af8ce | 2009-05-14 13:55:41 +0300 | [diff] [blame] | 347 | return err; |
| 348 | } |
| 349 | |
Rusty Russell | f68d240 | 2009-09-23 22:26:29 -0600 | [diff] [blame] | 350 | static int vp_request_intx(struct virtio_device *vdev) |
| 351 | { |
| 352 | int err; |
| 353 | struct virtio_pci_device *vp_dev = to_vp_device(vdev); |
| 354 | |
| 355 | err = request_irq(vp_dev->pci_dev->irq, vp_interrupt, |
| 356 | IRQF_SHARED, dev_name(&vdev->dev), vp_dev); |
| 357 | if (!err) |
| 358 | vp_dev->intx_enabled = 1; |
| 359 | return err; |
| 360 | } |
| 361 | |
| 362 | static struct virtqueue *setup_vq(struct virtio_device *vdev, unsigned index, |
| 363 | void (*callback)(struct virtqueue *vq), |
| 364 | const char *name, |
| 365 | u16 msix_vec) |
Anthony Liguori | 3343660 | 2007-11-12 21:30:26 -0600 | [diff] [blame] | 366 | { |
| 367 | struct virtio_pci_device *vp_dev = to_vp_device(vdev); |
| 368 | struct virtio_pci_vq_info *info; |
| 369 | struct virtqueue *vq; |
Hollis Blanchard | 13b1eb3 | 2008-12-02 16:24:40 -0600 | [diff] [blame] | 370 | unsigned long flags, size; |
Michael S. Tsirkin | e969fed | 2009-07-26 15:48:08 +0300 | [diff] [blame] | 371 | u16 num; |
Anthony Liguori | 3343660 | 2007-11-12 21:30:26 -0600 | [diff] [blame] | 372 | int err; |
| 373 | |
| 374 | /* Select the queue we're interested in */ |
| 375 | iowrite16(index, vp_dev->ioaddr + VIRTIO_PCI_QUEUE_SEL); |
| 376 | |
| 377 | /* Check if queue is either not available or already active. */ |
| 378 | num = ioread16(vp_dev->ioaddr + VIRTIO_PCI_QUEUE_NUM); |
| 379 | if (!num || ioread32(vp_dev->ioaddr + VIRTIO_PCI_QUEUE_PFN)) |
| 380 | return ERR_PTR(-ENOENT); |
| 381 | |
| 382 | /* allocate and fill out our structure the represents an active |
| 383 | * queue */ |
| 384 | info = kmalloc(sizeof(struct virtio_pci_vq_info), GFP_KERNEL); |
| 385 | if (!info) |
| 386 | return ERR_PTR(-ENOMEM); |
| 387 | |
| 388 | info->queue_index = index; |
| 389 | info->num = num; |
Rusty Russell | f68d240 | 2009-09-23 22:26:29 -0600 | [diff] [blame] | 390 | info->msix_vector = msix_vec; |
Anthony Liguori | 3343660 | 2007-11-12 21:30:26 -0600 | [diff] [blame] | 391 | |
Rusty Russell | 498af14 | 2008-12-30 09:25:57 -0600 | [diff] [blame] | 392 | size = PAGE_ALIGN(vring_size(num, VIRTIO_PCI_VRING_ALIGN)); |
Hollis Blanchard | 13b1eb3 | 2008-12-02 16:24:40 -0600 | [diff] [blame] | 393 | info->queue = alloc_pages_exact(size, GFP_KERNEL|__GFP_ZERO); |
Anthony Liguori | 3343660 | 2007-11-12 21:30:26 -0600 | [diff] [blame] | 394 | if (info->queue == NULL) { |
| 395 | err = -ENOMEM; |
| 396 | goto out_info; |
| 397 | } |
| 398 | |
| 399 | /* activate the queue */ |
Rusty Russell | 480daab | 2008-12-30 09:25:56 -0600 | [diff] [blame] | 400 | iowrite32(virt_to_phys(info->queue) >> VIRTIO_PCI_QUEUE_ADDR_SHIFT, |
Anthony Liguori | 3343660 | 2007-11-12 21:30:26 -0600 | [diff] [blame] | 401 | vp_dev->ioaddr + VIRTIO_PCI_QUEUE_PFN); |
| 402 | |
| 403 | /* create the vring */ |
Rusty Russell | 87c7d57 | 2008-12-30 09:26:03 -0600 | [diff] [blame] | 404 | vq = vring_new_virtqueue(info->num, VIRTIO_PCI_VRING_ALIGN, |
Rusty Russell | 9499f5e | 2009-06-12 22:16:35 -0600 | [diff] [blame] | 405 | vdev, info->queue, vp_notify, callback, name); |
Anthony Liguori | 3343660 | 2007-11-12 21:30:26 -0600 | [diff] [blame] | 406 | if (!vq) { |
| 407 | err = -ENOMEM; |
| 408 | goto out_activate_queue; |
| 409 | } |
| 410 | |
| 411 | vq->priv = info; |
| 412 | info->vq = vq; |
| 413 | |
Rusty Russell | f68d240 | 2009-09-23 22:26:29 -0600 | [diff] [blame] | 414 | if (msix_vec != VIRTIO_MSI_NO_VECTOR) { |
| 415 | iowrite16(msix_vec, vp_dev->ioaddr + VIRTIO_MSI_QUEUE_VECTOR); |
| 416 | msix_vec = ioread16(vp_dev->ioaddr + VIRTIO_MSI_QUEUE_VECTOR); |
| 417 | if (msix_vec == VIRTIO_MSI_NO_VECTOR) { |
Michael S. Tsirkin | 82af8ce | 2009-05-14 13:55:41 +0300 | [diff] [blame] | 418 | err = -EBUSY; |
| 419 | goto out_assign; |
| 420 | } |
| 421 | } |
| 422 | |
Anthony Liguori | 27ebe30 | 2008-03-02 16:37:48 -0600 | [diff] [blame] | 423 | spin_lock_irqsave(&vp_dev->lock, flags); |
Anthony Liguori | 3343660 | 2007-11-12 21:30:26 -0600 | [diff] [blame] | 424 | list_add(&info->node, &vp_dev->virtqueues); |
Anthony Liguori | 27ebe30 | 2008-03-02 16:37:48 -0600 | [diff] [blame] | 425 | spin_unlock_irqrestore(&vp_dev->lock, flags); |
Anthony Liguori | 3343660 | 2007-11-12 21:30:26 -0600 | [diff] [blame] | 426 | |
| 427 | return vq; |
| 428 | |
Michael S. Tsirkin | 82af8ce | 2009-05-14 13:55:41 +0300 | [diff] [blame] | 429 | out_assign: |
Michael S. Tsirkin | 82af8ce | 2009-05-14 13:55:41 +0300 | [diff] [blame] | 430 | vring_del_virtqueue(vq); |
Anthony Liguori | 3343660 | 2007-11-12 21:30:26 -0600 | [diff] [blame] | 431 | out_activate_queue: |
| 432 | iowrite32(0, vp_dev->ioaddr + VIRTIO_PCI_QUEUE_PFN); |
Hollis Blanchard | 13b1eb3 | 2008-12-02 16:24:40 -0600 | [diff] [blame] | 433 | free_pages_exact(info->queue, size); |
Anthony Liguori | 3343660 | 2007-11-12 21:30:26 -0600 | [diff] [blame] | 434 | out_info: |
| 435 | kfree(info); |
| 436 | return ERR_PTR(err); |
| 437 | } |
| 438 | |
Anthony Liguori | 3343660 | 2007-11-12 21:30:26 -0600 | [diff] [blame] | 439 | static void vp_del_vq(struct virtqueue *vq) |
| 440 | { |
| 441 | struct virtio_pci_device *vp_dev = to_vp_device(vq->vdev); |
| 442 | struct virtio_pci_vq_info *info = vq->priv; |
Michael S. Tsirkin | f6c8250 | 2009-07-26 15:48:01 +0300 | [diff] [blame] | 443 | unsigned long flags, size; |
| 444 | |
| 445 | spin_lock_irqsave(&vp_dev->lock, flags); |
| 446 | list_del(&info->node); |
| 447 | spin_unlock_irqrestore(&vp_dev->lock, flags); |
Anthony Liguori | 3343660 | 2007-11-12 21:30:26 -0600 | [diff] [blame] | 448 | |
Michael S. Tsirkin | 82af8ce | 2009-05-14 13:55:41 +0300 | [diff] [blame] | 449 | iowrite16(info->queue_index, vp_dev->ioaddr + VIRTIO_PCI_QUEUE_SEL); |
| 450 | |
Michael S. Tsirkin | 82af8ce | 2009-05-14 13:55:41 +0300 | [diff] [blame] | 451 | if (vp_dev->msix_enabled) { |
| 452 | iowrite16(VIRTIO_MSI_NO_VECTOR, |
| 453 | vp_dev->ioaddr + VIRTIO_MSI_QUEUE_VECTOR); |
| 454 | /* Flush the write out to device */ |
| 455 | ioread8(vp_dev->ioaddr + VIRTIO_PCI_ISR); |
| 456 | } |
| 457 | |
Anthony Liguori | 3343660 | 2007-11-12 21:30:26 -0600 | [diff] [blame] | 458 | vring_del_virtqueue(vq); |
| 459 | |
| 460 | /* Select and deactivate the queue */ |
Anthony Liguori | 3343660 | 2007-11-12 21:30:26 -0600 | [diff] [blame] | 461 | iowrite32(0, vp_dev->ioaddr + VIRTIO_PCI_QUEUE_PFN); |
| 462 | |
Rusty Russell | 498af14 | 2008-12-30 09:25:57 -0600 | [diff] [blame] | 463 | size = PAGE_ALIGN(vring_size(info->num, VIRTIO_PCI_VRING_ALIGN)); |
Hollis Blanchard | 13b1eb3 | 2008-12-02 16:24:40 -0600 | [diff] [blame] | 464 | free_pages_exact(info->queue, size); |
Anthony Liguori | 3343660 | 2007-11-12 21:30:26 -0600 | [diff] [blame] | 465 | kfree(info); |
| 466 | } |
| 467 | |
Michael S. Tsirkin | 82af8ce | 2009-05-14 13:55:41 +0300 | [diff] [blame] | 468 | /* the config->del_vqs() implementation */ |
Michael S. Tsirkin | d2a7ddd | 2009-06-12 22:16:36 -0600 | [diff] [blame] | 469 | static void vp_del_vqs(struct virtio_device *vdev) |
| 470 | { |
Michael S. Tsirkin | e969fed | 2009-07-26 15:48:08 +0300 | [diff] [blame] | 471 | struct virtio_pci_device *vp_dev = to_vp_device(vdev); |
Michael S. Tsirkin | d2a7ddd | 2009-06-12 22:16:36 -0600 | [diff] [blame] | 472 | struct virtqueue *vq, *n; |
Michael S. Tsirkin | e969fed | 2009-07-26 15:48:08 +0300 | [diff] [blame] | 473 | struct virtio_pci_vq_info *info; |
Michael S. Tsirkin | d2a7ddd | 2009-06-12 22:16:36 -0600 | [diff] [blame] | 474 | |
Michael S. Tsirkin | e969fed | 2009-07-26 15:48:08 +0300 | [diff] [blame] | 475 | list_for_each_entry_safe(vq, n, &vdev->vqs, list) { |
| 476 | info = vq->priv; |
Michael S. Tsirkin | 3119815 | 2010-02-25 19:08:55 +0200 | [diff] [blame] | 477 | if (vp_dev->per_vq_vectors && |
| 478 | info->msix_vector != VIRTIO_MSI_NO_VECTOR) |
Rusty Russell | f68d240 | 2009-09-23 22:26:29 -0600 | [diff] [blame] | 479 | free_irq(vp_dev->msix_entries[info->msix_vector].vector, |
| 480 | vq); |
Michael S. Tsirkin | d2a7ddd | 2009-06-12 22:16:36 -0600 | [diff] [blame] | 481 | vp_del_vq(vq); |
Michael S. Tsirkin | e969fed | 2009-07-26 15:48:08 +0300 | [diff] [blame] | 482 | } |
| 483 | vp_dev->per_vq_vectors = false; |
Michael S. Tsirkin | 82af8ce | 2009-05-14 13:55:41 +0300 | [diff] [blame] | 484 | |
| 485 | vp_free_vectors(vdev); |
Michael S. Tsirkin | d2a7ddd | 2009-06-12 22:16:36 -0600 | [diff] [blame] | 486 | } |
| 487 | |
Michael S. Tsirkin | e969fed | 2009-07-26 15:48:08 +0300 | [diff] [blame] | 488 | static int vp_try_to_find_vqs(struct virtio_device *vdev, unsigned nvqs, |
| 489 | struct virtqueue *vqs[], |
| 490 | vq_callback_t *callbacks[], |
| 491 | const char *names[], |
Rusty Russell | f68d240 | 2009-09-23 22:26:29 -0600 | [diff] [blame] | 492 | bool use_msix, |
Michael S. Tsirkin | e969fed | 2009-07-26 15:48:08 +0300 | [diff] [blame] | 493 | bool per_vq_vectors) |
| 494 | { |
| 495 | struct virtio_pci_device *vp_dev = to_vp_device(vdev); |
Rusty Russell | f68d240 | 2009-09-23 22:26:29 -0600 | [diff] [blame] | 496 | u16 msix_vec; |
| 497 | int i, err, nvectors, allocated_vectors; |
Michael S. Tsirkin | e969fed | 2009-07-26 15:48:08 +0300 | [diff] [blame] | 498 | |
Rusty Russell | f68d240 | 2009-09-23 22:26:29 -0600 | [diff] [blame] | 499 | if (!use_msix) { |
| 500 | /* Old style: one normal interrupt for change and all vqs. */ |
| 501 | err = vp_request_intx(vdev); |
| 502 | if (err) |
| 503 | goto error_request; |
| 504 | } else { |
| 505 | if (per_vq_vectors) { |
| 506 | /* Best option: one for change interrupt, one per vq. */ |
| 507 | nvectors = 1; |
| 508 | for (i = 0; i < nvqs; ++i) |
| 509 | if (callbacks[i]) |
| 510 | ++nvectors; |
| 511 | } else { |
| 512 | /* Second best: one for change, shared for all vqs. */ |
| 513 | nvectors = 2; |
| 514 | } |
| 515 | |
| 516 | err = vp_request_msix_vectors(vdev, nvectors, per_vq_vectors); |
| 517 | if (err) |
| 518 | goto error_request; |
| 519 | } |
Michael S. Tsirkin | e969fed | 2009-07-26 15:48:08 +0300 | [diff] [blame] | 520 | |
| 521 | vp_dev->per_vq_vectors = per_vq_vectors; |
| 522 | allocated_vectors = vp_dev->msix_used_vectors; |
| 523 | for (i = 0; i < nvqs; ++i) { |
| 524 | if (!callbacks[i] || !vp_dev->msix_enabled) |
Rusty Russell | f68d240 | 2009-09-23 22:26:29 -0600 | [diff] [blame] | 525 | msix_vec = VIRTIO_MSI_NO_VECTOR; |
Michael S. Tsirkin | e969fed | 2009-07-26 15:48:08 +0300 | [diff] [blame] | 526 | else if (vp_dev->per_vq_vectors) |
Rusty Russell | f68d240 | 2009-09-23 22:26:29 -0600 | [diff] [blame] | 527 | msix_vec = allocated_vectors++; |
Michael S. Tsirkin | e969fed | 2009-07-26 15:48:08 +0300 | [diff] [blame] | 528 | else |
Rusty Russell | f68d240 | 2009-09-23 22:26:29 -0600 | [diff] [blame] | 529 | msix_vec = VP_MSIX_VQ_VECTOR; |
| 530 | vqs[i] = setup_vq(vdev, i, callbacks[i], names[i], msix_vec); |
Michael S. Tsirkin | e969fed | 2009-07-26 15:48:08 +0300 | [diff] [blame] | 531 | if (IS_ERR(vqs[i])) { |
| 532 | err = PTR_ERR(vqs[i]); |
| 533 | goto error_find; |
| 534 | } |
Michael S. Tsirkin | 0b22bd0 | 2009-10-22 15:06:06 +0200 | [diff] [blame] | 535 | |
| 536 | if (!vp_dev->per_vq_vectors || msix_vec == VIRTIO_MSI_NO_VECTOR) |
| 537 | continue; |
| 538 | |
Michael S. Tsirkin | e969fed | 2009-07-26 15:48:08 +0300 | [diff] [blame] | 539 | /* allocate per-vq irq if available and necessary */ |
Michael S. Tsirkin | 0b22bd0 | 2009-10-22 15:06:06 +0200 | [diff] [blame] | 540 | snprintf(vp_dev->msix_names[msix_vec], |
| 541 | sizeof *vp_dev->msix_names, |
| 542 | "%s-%s", |
| 543 | dev_name(&vp_dev->vdev.dev), names[i]); |
| 544 | err = request_irq(vp_dev->msix_entries[msix_vec].vector, |
| 545 | vring_interrupt, 0, |
| 546 | vp_dev->msix_names[msix_vec], |
| 547 | vqs[i]); |
| 548 | if (err) { |
| 549 | vp_del_vq(vqs[i]); |
| 550 | goto error_find; |
Michael S. Tsirkin | e969fed | 2009-07-26 15:48:08 +0300 | [diff] [blame] | 551 | } |
| 552 | } |
| 553 | return 0; |
| 554 | |
| 555 | error_find: |
| 556 | vp_del_vqs(vdev); |
| 557 | |
| 558 | error_request: |
| 559 | return err; |
| 560 | } |
| 561 | |
Michael S. Tsirkin | 82af8ce | 2009-05-14 13:55:41 +0300 | [diff] [blame] | 562 | /* the config->find_vqs() implementation */ |
Michael S. Tsirkin | d2a7ddd | 2009-06-12 22:16:36 -0600 | [diff] [blame] | 563 | static int vp_find_vqs(struct virtio_device *vdev, unsigned nvqs, |
| 564 | struct virtqueue *vqs[], |
| 565 | vq_callback_t *callbacks[], |
| 566 | const char *names[]) |
| 567 | { |
Rusty Russell | f68d240 | 2009-09-23 22:26:29 -0600 | [diff] [blame] | 568 | int err; |
Michael S. Tsirkin | 82af8ce | 2009-05-14 13:55:41 +0300 | [diff] [blame] | 569 | |
Rusty Russell | f68d240 | 2009-09-23 22:26:29 -0600 | [diff] [blame] | 570 | /* Try MSI-X with one vector per queue. */ |
| 571 | err = vp_try_to_find_vqs(vdev, nvqs, vqs, callbacks, names, true, true); |
Michael S. Tsirkin | e969fed | 2009-07-26 15:48:08 +0300 | [diff] [blame] | 572 | if (!err) |
| 573 | return 0; |
Rusty Russell | f68d240 | 2009-09-23 22:26:29 -0600 | [diff] [blame] | 574 | /* Fallback: MSI-X with one vector for config, one shared for queues. */ |
Michael S. Tsirkin | e969fed | 2009-07-26 15:48:08 +0300 | [diff] [blame] | 575 | err = vp_try_to_find_vqs(vdev, nvqs, vqs, callbacks, names, |
Rusty Russell | f68d240 | 2009-09-23 22:26:29 -0600 | [diff] [blame] | 576 | true, false); |
Michael S. Tsirkin | e969fed | 2009-07-26 15:48:08 +0300 | [diff] [blame] | 577 | if (!err) |
| 578 | return 0; |
| 579 | /* Finally fall back to regular interrupts. */ |
Rusty Russell | f68d240 | 2009-09-23 22:26:29 -0600 | [diff] [blame] | 580 | return vp_try_to_find_vqs(vdev, nvqs, vqs, callbacks, names, |
| 581 | false, false); |
Michael S. Tsirkin | d2a7ddd | 2009-06-12 22:16:36 -0600 | [diff] [blame] | 582 | } |
| 583 | |
Anthony Liguori | 3343660 | 2007-11-12 21:30:26 -0600 | [diff] [blame] | 584 | static struct virtio_config_ops virtio_pci_config_ops = { |
Anthony Liguori | 3343660 | 2007-11-12 21:30:26 -0600 | [diff] [blame] | 585 | .get = vp_get, |
| 586 | .set = vp_set, |
| 587 | .get_status = vp_get_status, |
| 588 | .set_status = vp_set_status, |
| 589 | .reset = vp_reset, |
Michael S. Tsirkin | d2a7ddd | 2009-06-12 22:16:36 -0600 | [diff] [blame] | 590 | .find_vqs = vp_find_vqs, |
| 591 | .del_vqs = vp_del_vqs, |
Rusty Russell | c45a681 | 2008-05-02 21:50:50 -0500 | [diff] [blame] | 592 | .get_features = vp_get_features, |
Rusty Russell | c624896 | 2008-07-25 12:06:07 -0500 | [diff] [blame] | 593 | .finalize_features = vp_finalize_features, |
Anthony Liguori | 3343660 | 2007-11-12 21:30:26 -0600 | [diff] [blame] | 594 | }; |
| 595 | |
Mark McLoughlin | 29f9f12 | 2008-12-10 17:45:34 +0000 | [diff] [blame] | 596 | static void virtio_pci_release_dev(struct device *_d) |
| 597 | { |
| 598 | struct virtio_device *dev = container_of(_d, struct virtio_device, dev); |
| 599 | struct virtio_pci_device *vp_dev = to_vp_device(dev); |
| 600 | struct pci_dev *pci_dev = vp_dev->pci_dev; |
| 601 | |
Michael S. Tsirkin | 82af8ce | 2009-05-14 13:55:41 +0300 | [diff] [blame] | 602 | vp_del_vqs(dev); |
Mark McLoughlin | 29f9f12 | 2008-12-10 17:45:34 +0000 | [diff] [blame] | 603 | pci_set_drvdata(pci_dev, NULL); |
| 604 | pci_iounmap(pci_dev, vp_dev->ioaddr); |
| 605 | pci_release_regions(pci_dev); |
| 606 | pci_disable_device(pci_dev); |
| 607 | kfree(vp_dev); |
| 608 | } |
| 609 | |
Anthony Liguori | 3343660 | 2007-11-12 21:30:26 -0600 | [diff] [blame] | 610 | /* the PCI probing function */ |
| 611 | static int __devinit virtio_pci_probe(struct pci_dev *pci_dev, |
| 612 | const struct pci_device_id *id) |
| 613 | { |
| 614 | struct virtio_pci_device *vp_dev; |
| 615 | int err; |
| 616 | |
| 617 | /* We only own devices >= 0x1000 and <= 0x103f: leave the rest. */ |
| 618 | if (pci_dev->device < 0x1000 || pci_dev->device > 0x103f) |
| 619 | return -ENODEV; |
| 620 | |
Anthony Liguori | 55a7c06 | 2008-01-28 09:59:59 -0600 | [diff] [blame] | 621 | if (pci_dev->revision != VIRTIO_PCI_ABI_VERSION) { |
| 622 | printk(KERN_ERR "virtio_pci: expected ABI version %d, got %d\n", |
| 623 | VIRTIO_PCI_ABI_VERSION, pci_dev->revision); |
| 624 | return -ENODEV; |
| 625 | } |
| 626 | |
Anthony Liguori | 3343660 | 2007-11-12 21:30:26 -0600 | [diff] [blame] | 627 | /* allocate our structure and fill it out */ |
| 628 | vp_dev = kzalloc(sizeof(struct virtio_pci_device), GFP_KERNEL); |
| 629 | if (vp_dev == NULL) |
| 630 | return -ENOMEM; |
| 631 | |
Mark McLoughlin | 63d1255 | 2008-12-15 12:58:27 +0000 | [diff] [blame] | 632 | vp_dev->vdev.dev.parent = virtio_pci_root; |
Mark McLoughlin | 29f9f12 | 2008-12-10 17:45:34 +0000 | [diff] [blame] | 633 | vp_dev->vdev.dev.release = virtio_pci_release_dev; |
Anthony Liguori | 3343660 | 2007-11-12 21:30:26 -0600 | [diff] [blame] | 634 | vp_dev->vdev.config = &virtio_pci_config_ops; |
| 635 | vp_dev->pci_dev = pci_dev; |
| 636 | INIT_LIST_HEAD(&vp_dev->virtqueues); |
| 637 | spin_lock_init(&vp_dev->lock); |
| 638 | |
Michael S. Tsirkin | b03214d | 2010-06-23 22:49:06 -0600 | [diff] [blame] | 639 | /* Disable MSI/MSIX to bring device to a known good state. */ |
| 640 | pci_msi_off(pci_dev); |
| 641 | |
Anthony Liguori | 3343660 | 2007-11-12 21:30:26 -0600 | [diff] [blame] | 642 | /* enable the device */ |
| 643 | err = pci_enable_device(pci_dev); |
| 644 | if (err) |
| 645 | goto out; |
| 646 | |
| 647 | err = pci_request_regions(pci_dev, "virtio-pci"); |
| 648 | if (err) |
| 649 | goto out_enable_device; |
| 650 | |
| 651 | vp_dev->ioaddr = pci_iomap(pci_dev, 0, 0); |
| 652 | if (vp_dev->ioaddr == NULL) |
| 653 | goto out_req_regions; |
| 654 | |
| 655 | pci_set_drvdata(pci_dev, vp_dev); |
Michael S. Tsirkin | bc505f3 | 2009-11-29 17:52:00 +0200 | [diff] [blame] | 656 | pci_set_master(pci_dev); |
Anthony Liguori | 3343660 | 2007-11-12 21:30:26 -0600 | [diff] [blame] | 657 | |
| 658 | /* we use the subsystem vendor/device id as the virtio vendor/device |
| 659 | * id. this allows us to use the same PCI vendor/device id for all |
| 660 | * virtio devices and to identify the particular virtio driver by |
Thomas Weber | 8839316 | 2010-03-16 11:47:56 +0100 | [diff] [blame] | 661 | * the subsystem ids */ |
Anthony Liguori | 3343660 | 2007-11-12 21:30:26 -0600 | [diff] [blame] | 662 | vp_dev->vdev.id.vendor = pci_dev->subsystem_vendor; |
| 663 | vp_dev->vdev.id.device = pci_dev->subsystem_device; |
| 664 | |
Anthony Liguori | 3343660 | 2007-11-12 21:30:26 -0600 | [diff] [blame] | 665 | /* finally register the virtio device */ |
| 666 | err = register_virtio_device(&vp_dev->vdev); |
| 667 | if (err) |
Michael S. Tsirkin | 82af8ce | 2009-05-14 13:55:41 +0300 | [diff] [blame] | 668 | goto out_set_drvdata; |
Anthony Liguori | 3343660 | 2007-11-12 21:30:26 -0600 | [diff] [blame] | 669 | |
| 670 | return 0; |
| 671 | |
Anthony Liguori | 3343660 | 2007-11-12 21:30:26 -0600 | [diff] [blame] | 672 | out_set_drvdata: |
| 673 | pci_set_drvdata(pci_dev, NULL); |
| 674 | pci_iounmap(pci_dev, vp_dev->ioaddr); |
| 675 | out_req_regions: |
| 676 | pci_release_regions(pci_dev); |
| 677 | out_enable_device: |
| 678 | pci_disable_device(pci_dev); |
| 679 | out: |
| 680 | kfree(vp_dev); |
| 681 | return err; |
| 682 | } |
| 683 | |
| 684 | static void __devexit virtio_pci_remove(struct pci_dev *pci_dev) |
| 685 | { |
| 686 | struct virtio_pci_device *vp_dev = pci_get_drvdata(pci_dev); |
| 687 | |
Anthony Liguori | bd6c269 | 2008-03-19 20:35:04 -0500 | [diff] [blame] | 688 | unregister_virtio_device(&vp_dev->vdev); |
Anthony Liguori | 3343660 | 2007-11-12 21:30:26 -0600 | [diff] [blame] | 689 | } |
| 690 | |
| 691 | #ifdef CONFIG_PM |
| 692 | static int virtio_pci_suspend(struct pci_dev *pci_dev, pm_message_t state) |
| 693 | { |
| 694 | pci_save_state(pci_dev); |
| 695 | pci_set_power_state(pci_dev, PCI_D3hot); |
| 696 | return 0; |
| 697 | } |
| 698 | |
| 699 | static int virtio_pci_resume(struct pci_dev *pci_dev) |
| 700 | { |
| 701 | pci_restore_state(pci_dev); |
| 702 | pci_set_power_state(pci_dev, PCI_D0); |
| 703 | return 0; |
| 704 | } |
| 705 | #endif |
| 706 | |
| 707 | static struct pci_driver virtio_pci_driver = { |
| 708 | .name = "virtio-pci", |
| 709 | .id_table = virtio_pci_id_table, |
| 710 | .probe = virtio_pci_probe, |
Jamie Lokier | 1f08b83 | 2010-01-08 22:01:43 +0000 | [diff] [blame] | 711 | .remove = __devexit_p(virtio_pci_remove), |
Anthony Liguori | 3343660 | 2007-11-12 21:30:26 -0600 | [diff] [blame] | 712 | #ifdef CONFIG_PM |
| 713 | .suspend = virtio_pci_suspend, |
| 714 | .resume = virtio_pci_resume, |
| 715 | #endif |
| 716 | }; |
| 717 | |
| 718 | static int __init virtio_pci_init(void) |
| 719 | { |
| 720 | int err; |
| 721 | |
Mark McLoughlin | 63d1255 | 2008-12-15 12:58:27 +0000 | [diff] [blame] | 722 | virtio_pci_root = root_device_register("virtio-pci"); |
| 723 | if (IS_ERR(virtio_pci_root)) |
| 724 | return PTR_ERR(virtio_pci_root); |
Anthony Liguori | 3343660 | 2007-11-12 21:30:26 -0600 | [diff] [blame] | 725 | |
| 726 | err = pci_register_driver(&virtio_pci_driver); |
| 727 | if (err) |
Mark McLoughlin | 4b892e6 | 2009-07-07 08:26:45 +0100 | [diff] [blame] | 728 | root_device_unregister(virtio_pci_root); |
Anthony Liguori | 3343660 | 2007-11-12 21:30:26 -0600 | [diff] [blame] | 729 | |
| 730 | return err; |
| 731 | } |
| 732 | |
| 733 | module_init(virtio_pci_init); |
| 734 | |
| 735 | static void __exit virtio_pci_exit(void) |
| 736 | { |
Anthony Liguori | 3343660 | 2007-11-12 21:30:26 -0600 | [diff] [blame] | 737 | pci_unregister_driver(&virtio_pci_driver); |
Mark McLoughlin | 63d1255 | 2008-12-15 12:58:27 +0000 | [diff] [blame] | 738 | root_device_unregister(virtio_pci_root); |
Anthony Liguori | 3343660 | 2007-11-12 21:30:26 -0600 | [diff] [blame] | 739 | } |
| 740 | |
| 741 | module_exit(virtio_pci_exit); |