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> |
| 20 | #include <linux/interrupt.h> |
| 21 | #include <linux/virtio.h> |
| 22 | #include <linux/virtio_config.h> |
| 23 | #include <linux/virtio_ring.h> |
| 24 | #include <linux/virtio_pci.h> |
| 25 | #include <linux/highmem.h> |
| 26 | #include <linux/spinlock.h> |
| 27 | |
| 28 | MODULE_AUTHOR("Anthony Liguori <aliguori@us.ibm.com>"); |
| 29 | MODULE_DESCRIPTION("virtio-pci"); |
| 30 | MODULE_LICENSE("GPL"); |
| 31 | MODULE_VERSION("1"); |
| 32 | |
| 33 | /* Our device structure */ |
| 34 | struct virtio_pci_device |
| 35 | { |
| 36 | struct virtio_device vdev; |
| 37 | struct pci_dev *pci_dev; |
| 38 | |
| 39 | /* the IO mapping for the PCI config space */ |
Al Viro | 9796835 | 2008-03-29 03:09:48 +0000 | [diff] [blame] | 40 | void __iomem *ioaddr; |
Anthony Liguori | 3343660 | 2007-11-12 21:30:26 -0600 | [diff] [blame] | 41 | |
| 42 | /* a list of queues so we can dispatch IRQs */ |
| 43 | spinlock_t lock; |
| 44 | struct list_head virtqueues; |
| 45 | }; |
| 46 | |
| 47 | struct virtio_pci_vq_info |
| 48 | { |
| 49 | /* the actual virtqueue */ |
| 50 | struct virtqueue *vq; |
| 51 | |
| 52 | /* the number of entries in the queue */ |
| 53 | int num; |
| 54 | |
| 55 | /* the index of the queue */ |
| 56 | int queue_index; |
| 57 | |
| 58 | /* the virtual address of the ring queue */ |
| 59 | void *queue; |
| 60 | |
| 61 | /* the list node for the virtqueues list */ |
| 62 | struct list_head node; |
| 63 | }; |
| 64 | |
| 65 | /* Qumranet donated their vendor ID for devices 0x1000 thru 0x10FF. */ |
| 66 | static struct pci_device_id virtio_pci_id_table[] = { |
| 67 | { 0x1af4, PCI_ANY_ID, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0 }, |
| 68 | { 0 }, |
| 69 | }; |
| 70 | |
| 71 | MODULE_DEVICE_TABLE(pci, virtio_pci_id_table); |
| 72 | |
| 73 | /* A PCI device has it's own struct device and so does a virtio device so |
| 74 | * we create a place for the virtio devices to show up in sysfs. I think it |
| 75 | * 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] | 76 | static struct device *virtio_pci_root; |
Anthony Liguori | 3343660 | 2007-11-12 21:30:26 -0600 | [diff] [blame] | 77 | |
Anthony Liguori | 3343660 | 2007-11-12 21:30:26 -0600 | [diff] [blame] | 78 | /* Convert a generic virtio device to our structure */ |
| 79 | static struct virtio_pci_device *to_vp_device(struct virtio_device *vdev) |
| 80 | { |
| 81 | return container_of(vdev, struct virtio_pci_device, vdev); |
| 82 | } |
| 83 | |
Rusty Russell | c45a681 | 2008-05-02 21:50:50 -0500 | [diff] [blame] | 84 | /* virtio config->get_features() implementation */ |
| 85 | static u32 vp_get_features(struct virtio_device *vdev) |
Anthony Liguori | 3343660 | 2007-11-12 21:30:26 -0600 | [diff] [blame] | 86 | { |
| 87 | struct virtio_pci_device *vp_dev = to_vp_device(vdev); |
Anthony Liguori | 3343660 | 2007-11-12 21:30:26 -0600 | [diff] [blame] | 88 | |
Rusty Russell | c45a681 | 2008-05-02 21:50:50 -0500 | [diff] [blame] | 89 | /* When someone needs more than 32 feature bits, we'll need to |
| 90 | * steal a bit to indicate that the rest are somewhere else. */ |
| 91 | return ioread32(vp_dev->ioaddr + VIRTIO_PCI_HOST_FEATURES); |
| 92 | } |
Anthony Liguori | 3343660 | 2007-11-12 21:30:26 -0600 | [diff] [blame] | 93 | |
Rusty Russell | c624896 | 2008-07-25 12:06:07 -0500 | [diff] [blame] | 94 | /* virtio config->finalize_features() implementation */ |
| 95 | static void vp_finalize_features(struct virtio_device *vdev) |
Rusty Russell | c45a681 | 2008-05-02 21:50:50 -0500 | [diff] [blame] | 96 | { |
| 97 | struct virtio_pci_device *vp_dev = to_vp_device(vdev); |
| 98 | |
Rusty Russell | e34f872 | 2008-07-25 12:06:13 -0500 | [diff] [blame] | 99 | /* Give virtio_ring a chance to accept features. */ |
| 100 | vring_transport_features(vdev); |
| 101 | |
Rusty Russell | c624896 | 2008-07-25 12:06:07 -0500 | [diff] [blame] | 102 | /* We only support 32 feature bits. */ |
| 103 | BUILD_BUG_ON(ARRAY_SIZE(vdev->features) != 1); |
| 104 | iowrite32(vdev->features[0], vp_dev->ioaddr+VIRTIO_PCI_GUEST_FEATURES); |
Anthony Liguori | 3343660 | 2007-11-12 21:30:26 -0600 | [diff] [blame] | 105 | } |
| 106 | |
| 107 | /* virtio config->get() implementation */ |
| 108 | static void vp_get(struct virtio_device *vdev, unsigned offset, |
| 109 | void *buf, unsigned len) |
| 110 | { |
| 111 | struct virtio_pci_device *vp_dev = to_vp_device(vdev); |
Al Viro | 9796835 | 2008-03-29 03:09:48 +0000 | [diff] [blame] | 112 | void __iomem *ioaddr = vp_dev->ioaddr + VIRTIO_PCI_CONFIG + offset; |
Anthony Liguori | 3343660 | 2007-11-12 21:30:26 -0600 | [diff] [blame] | 113 | u8 *ptr = buf; |
| 114 | int i; |
| 115 | |
| 116 | for (i = 0; i < len; i++) |
| 117 | ptr[i] = ioread8(ioaddr + i); |
| 118 | } |
| 119 | |
| 120 | /* the config->set() implementation. it's symmetric to the config->get() |
| 121 | * implementation */ |
| 122 | static void vp_set(struct virtio_device *vdev, unsigned offset, |
| 123 | const void *buf, unsigned len) |
| 124 | { |
| 125 | struct virtio_pci_device *vp_dev = to_vp_device(vdev); |
Al Viro | 9796835 | 2008-03-29 03:09:48 +0000 | [diff] [blame] | 126 | void __iomem *ioaddr = vp_dev->ioaddr + VIRTIO_PCI_CONFIG + offset; |
Anthony Liguori | 3343660 | 2007-11-12 21:30:26 -0600 | [diff] [blame] | 127 | const u8 *ptr = buf; |
| 128 | int i; |
| 129 | |
| 130 | for (i = 0; i < len; i++) |
| 131 | iowrite8(ptr[i], ioaddr + i); |
| 132 | } |
| 133 | |
| 134 | /* config->{get,set}_status() implementations */ |
| 135 | static u8 vp_get_status(struct virtio_device *vdev) |
| 136 | { |
| 137 | struct virtio_pci_device *vp_dev = to_vp_device(vdev); |
| 138 | return ioread8(vp_dev->ioaddr + VIRTIO_PCI_STATUS); |
| 139 | } |
| 140 | |
| 141 | static void vp_set_status(struct virtio_device *vdev, u8 status) |
| 142 | { |
| 143 | struct virtio_pci_device *vp_dev = to_vp_device(vdev); |
| 144 | /* We should never be setting status to 0. */ |
| 145 | BUG_ON(status == 0); |
Harvey Harrison | 597d56e | 2008-03-31 17:53:55 -0700 | [diff] [blame] | 146 | iowrite8(status, vp_dev->ioaddr + VIRTIO_PCI_STATUS); |
Anthony Liguori | 3343660 | 2007-11-12 21:30:26 -0600 | [diff] [blame] | 147 | } |
| 148 | |
| 149 | static void vp_reset(struct virtio_device *vdev) |
| 150 | { |
| 151 | struct virtio_pci_device *vp_dev = to_vp_device(vdev); |
| 152 | /* 0 status means a reset. */ |
Harvey Harrison | 597d56e | 2008-03-31 17:53:55 -0700 | [diff] [blame] | 153 | iowrite8(0, vp_dev->ioaddr + VIRTIO_PCI_STATUS); |
Anthony Liguori | 3343660 | 2007-11-12 21:30:26 -0600 | [diff] [blame] | 154 | } |
| 155 | |
| 156 | /* the notify function used when creating a virt queue */ |
| 157 | static void vp_notify(struct virtqueue *vq) |
| 158 | { |
| 159 | struct virtio_pci_device *vp_dev = to_vp_device(vq->vdev); |
| 160 | struct virtio_pci_vq_info *info = vq->priv; |
| 161 | |
| 162 | /* we write the queue's selector into the notification register to |
| 163 | * signal the other end */ |
| 164 | iowrite16(info->queue_index, vp_dev->ioaddr + VIRTIO_PCI_QUEUE_NOTIFY); |
| 165 | } |
| 166 | |
| 167 | /* A small wrapper to also acknowledge the interrupt when it's handled. |
| 168 | * I really need an EIO hook for the vring so I can ack the interrupt once we |
| 169 | * know that we'll be handling the IRQ but before we invoke the callback since |
| 170 | * the callback may notify the host which results in the host attempting to |
| 171 | * raise an interrupt that we would then mask once we acknowledged the |
| 172 | * interrupt. */ |
| 173 | static irqreturn_t vp_interrupt(int irq, void *opaque) |
| 174 | { |
| 175 | struct virtio_pci_device *vp_dev = opaque; |
| 176 | struct virtio_pci_vq_info *info; |
| 177 | irqreturn_t ret = IRQ_NONE; |
Anthony Liguori | 27ebe30 | 2008-03-02 16:37:48 -0600 | [diff] [blame] | 178 | unsigned long flags; |
Anthony Liguori | 3343660 | 2007-11-12 21:30:26 -0600 | [diff] [blame] | 179 | u8 isr; |
| 180 | |
| 181 | /* reading the ISR has the effect of also clearing it so it's very |
| 182 | * important to save off the value. */ |
| 183 | isr = ioread8(vp_dev->ioaddr + VIRTIO_PCI_ISR); |
| 184 | |
| 185 | /* It's definitely not us if the ISR was not high */ |
| 186 | if (!isr) |
| 187 | return IRQ_NONE; |
| 188 | |
| 189 | /* Configuration change? Tell driver if it wants to know. */ |
| 190 | if (isr & VIRTIO_PCI_ISR_CONFIG) { |
| 191 | struct virtio_driver *drv; |
| 192 | drv = container_of(vp_dev->vdev.dev.driver, |
| 193 | struct virtio_driver, driver); |
| 194 | |
Mark McLoughlin | 3fff017 | 2009-02-03 13:33:53 +1030 | [diff] [blame^] | 195 | if (drv && drv->config_changed) |
Anthony Liguori | 3343660 | 2007-11-12 21:30:26 -0600 | [diff] [blame] | 196 | drv->config_changed(&vp_dev->vdev); |
| 197 | } |
| 198 | |
Anthony Liguori | 27ebe30 | 2008-03-02 16:37:48 -0600 | [diff] [blame] | 199 | spin_lock_irqsave(&vp_dev->lock, flags); |
Anthony Liguori | 3343660 | 2007-11-12 21:30:26 -0600 | [diff] [blame] | 200 | list_for_each_entry(info, &vp_dev->virtqueues, node) { |
| 201 | if (vring_interrupt(irq, info->vq) == IRQ_HANDLED) |
| 202 | ret = IRQ_HANDLED; |
| 203 | } |
Anthony Liguori | 27ebe30 | 2008-03-02 16:37:48 -0600 | [diff] [blame] | 204 | spin_unlock_irqrestore(&vp_dev->lock, flags); |
Anthony Liguori | 3343660 | 2007-11-12 21:30:26 -0600 | [diff] [blame] | 205 | |
| 206 | return ret; |
| 207 | } |
| 208 | |
| 209 | /* the config->find_vq() implementation */ |
| 210 | static struct virtqueue *vp_find_vq(struct virtio_device *vdev, unsigned index, |
| 211 | void (*callback)(struct virtqueue *vq)) |
| 212 | { |
| 213 | struct virtio_pci_device *vp_dev = to_vp_device(vdev); |
| 214 | struct virtio_pci_vq_info *info; |
| 215 | struct virtqueue *vq; |
Hollis Blanchard | 13b1eb3 | 2008-12-02 16:24:40 -0600 | [diff] [blame] | 216 | unsigned long flags, size; |
Anthony Liguori | 3343660 | 2007-11-12 21:30:26 -0600 | [diff] [blame] | 217 | u16 num; |
| 218 | int err; |
| 219 | |
| 220 | /* Select the queue we're interested in */ |
| 221 | iowrite16(index, vp_dev->ioaddr + VIRTIO_PCI_QUEUE_SEL); |
| 222 | |
| 223 | /* Check if queue is either not available or already active. */ |
| 224 | num = ioread16(vp_dev->ioaddr + VIRTIO_PCI_QUEUE_NUM); |
| 225 | if (!num || ioread32(vp_dev->ioaddr + VIRTIO_PCI_QUEUE_PFN)) |
| 226 | return ERR_PTR(-ENOENT); |
| 227 | |
| 228 | /* allocate and fill out our structure the represents an active |
| 229 | * queue */ |
| 230 | info = kmalloc(sizeof(struct virtio_pci_vq_info), GFP_KERNEL); |
| 231 | if (!info) |
| 232 | return ERR_PTR(-ENOMEM); |
| 233 | |
| 234 | info->queue_index = index; |
| 235 | info->num = num; |
| 236 | |
Rusty Russell | 498af14 | 2008-12-30 09:25:57 -0600 | [diff] [blame] | 237 | size = PAGE_ALIGN(vring_size(num, VIRTIO_PCI_VRING_ALIGN)); |
Hollis Blanchard | 13b1eb3 | 2008-12-02 16:24:40 -0600 | [diff] [blame] | 238 | info->queue = alloc_pages_exact(size, GFP_KERNEL|__GFP_ZERO); |
Anthony Liguori | 3343660 | 2007-11-12 21:30:26 -0600 | [diff] [blame] | 239 | if (info->queue == NULL) { |
| 240 | err = -ENOMEM; |
| 241 | goto out_info; |
| 242 | } |
| 243 | |
| 244 | /* activate the queue */ |
Rusty Russell | 480daab | 2008-12-30 09:25:56 -0600 | [diff] [blame] | 245 | iowrite32(virt_to_phys(info->queue) >> VIRTIO_PCI_QUEUE_ADDR_SHIFT, |
Anthony Liguori | 3343660 | 2007-11-12 21:30:26 -0600 | [diff] [blame] | 246 | vp_dev->ioaddr + VIRTIO_PCI_QUEUE_PFN); |
| 247 | |
| 248 | /* create the vring */ |
Rusty Russell | 87c7d57 | 2008-12-30 09:26:03 -0600 | [diff] [blame] | 249 | vq = vring_new_virtqueue(info->num, VIRTIO_PCI_VRING_ALIGN, |
| 250 | vdev, info->queue, vp_notify, callback); |
Anthony Liguori | 3343660 | 2007-11-12 21:30:26 -0600 | [diff] [blame] | 251 | if (!vq) { |
| 252 | err = -ENOMEM; |
| 253 | goto out_activate_queue; |
| 254 | } |
| 255 | |
| 256 | vq->priv = info; |
| 257 | info->vq = vq; |
| 258 | |
Anthony Liguori | 27ebe30 | 2008-03-02 16:37:48 -0600 | [diff] [blame] | 259 | spin_lock_irqsave(&vp_dev->lock, flags); |
Anthony Liguori | 3343660 | 2007-11-12 21:30:26 -0600 | [diff] [blame] | 260 | list_add(&info->node, &vp_dev->virtqueues); |
Anthony Liguori | 27ebe30 | 2008-03-02 16:37:48 -0600 | [diff] [blame] | 261 | spin_unlock_irqrestore(&vp_dev->lock, flags); |
Anthony Liguori | 3343660 | 2007-11-12 21:30:26 -0600 | [diff] [blame] | 262 | |
| 263 | return vq; |
| 264 | |
| 265 | out_activate_queue: |
| 266 | iowrite32(0, vp_dev->ioaddr + VIRTIO_PCI_QUEUE_PFN); |
Hollis Blanchard | 13b1eb3 | 2008-12-02 16:24:40 -0600 | [diff] [blame] | 267 | free_pages_exact(info->queue, size); |
Anthony Liguori | 3343660 | 2007-11-12 21:30:26 -0600 | [diff] [blame] | 268 | out_info: |
| 269 | kfree(info); |
| 270 | return ERR_PTR(err); |
| 271 | } |
| 272 | |
| 273 | /* the config->del_vq() implementation */ |
| 274 | static void vp_del_vq(struct virtqueue *vq) |
| 275 | { |
| 276 | struct virtio_pci_device *vp_dev = to_vp_device(vq->vdev); |
| 277 | struct virtio_pci_vq_info *info = vq->priv; |
Hollis Blanchard | 13b1eb3 | 2008-12-02 16:24:40 -0600 | [diff] [blame] | 278 | unsigned long flags, size; |
Anthony Liguori | 3343660 | 2007-11-12 21:30:26 -0600 | [diff] [blame] | 279 | |
Anthony Liguori | 27ebe30 | 2008-03-02 16:37:48 -0600 | [diff] [blame] | 280 | spin_lock_irqsave(&vp_dev->lock, flags); |
Anthony Liguori | 3343660 | 2007-11-12 21:30:26 -0600 | [diff] [blame] | 281 | list_del(&info->node); |
Anthony Liguori | 27ebe30 | 2008-03-02 16:37:48 -0600 | [diff] [blame] | 282 | spin_unlock_irqrestore(&vp_dev->lock, flags); |
Anthony Liguori | 3343660 | 2007-11-12 21:30:26 -0600 | [diff] [blame] | 283 | |
| 284 | vring_del_virtqueue(vq); |
| 285 | |
| 286 | /* Select and deactivate the queue */ |
| 287 | iowrite16(info->queue_index, vp_dev->ioaddr + VIRTIO_PCI_QUEUE_SEL); |
| 288 | iowrite32(0, vp_dev->ioaddr + VIRTIO_PCI_QUEUE_PFN); |
| 289 | |
Rusty Russell | 498af14 | 2008-12-30 09:25:57 -0600 | [diff] [blame] | 290 | size = PAGE_ALIGN(vring_size(info->num, VIRTIO_PCI_VRING_ALIGN)); |
Hollis Blanchard | 13b1eb3 | 2008-12-02 16:24:40 -0600 | [diff] [blame] | 291 | free_pages_exact(info->queue, size); |
Anthony Liguori | 3343660 | 2007-11-12 21:30:26 -0600 | [diff] [blame] | 292 | kfree(info); |
| 293 | } |
| 294 | |
| 295 | static struct virtio_config_ops virtio_pci_config_ops = { |
Anthony Liguori | 3343660 | 2007-11-12 21:30:26 -0600 | [diff] [blame] | 296 | .get = vp_get, |
| 297 | .set = vp_set, |
| 298 | .get_status = vp_get_status, |
| 299 | .set_status = vp_set_status, |
| 300 | .reset = vp_reset, |
| 301 | .find_vq = vp_find_vq, |
| 302 | .del_vq = vp_del_vq, |
Rusty Russell | c45a681 | 2008-05-02 21:50:50 -0500 | [diff] [blame] | 303 | .get_features = vp_get_features, |
Rusty Russell | c624896 | 2008-07-25 12:06:07 -0500 | [diff] [blame] | 304 | .finalize_features = vp_finalize_features, |
Anthony Liguori | 3343660 | 2007-11-12 21:30:26 -0600 | [diff] [blame] | 305 | }; |
| 306 | |
Mark McLoughlin | 29f9f12 | 2008-12-10 17:45:34 +0000 | [diff] [blame] | 307 | static void virtio_pci_release_dev(struct device *_d) |
| 308 | { |
| 309 | struct virtio_device *dev = container_of(_d, struct virtio_device, dev); |
| 310 | struct virtio_pci_device *vp_dev = to_vp_device(dev); |
| 311 | struct pci_dev *pci_dev = vp_dev->pci_dev; |
| 312 | |
| 313 | free_irq(pci_dev->irq, vp_dev); |
| 314 | pci_set_drvdata(pci_dev, NULL); |
| 315 | pci_iounmap(pci_dev, vp_dev->ioaddr); |
| 316 | pci_release_regions(pci_dev); |
| 317 | pci_disable_device(pci_dev); |
| 318 | kfree(vp_dev); |
| 319 | } |
| 320 | |
Anthony Liguori | 3343660 | 2007-11-12 21:30:26 -0600 | [diff] [blame] | 321 | /* the PCI probing function */ |
| 322 | static int __devinit virtio_pci_probe(struct pci_dev *pci_dev, |
| 323 | const struct pci_device_id *id) |
| 324 | { |
| 325 | struct virtio_pci_device *vp_dev; |
| 326 | int err; |
| 327 | |
| 328 | /* We only own devices >= 0x1000 and <= 0x103f: leave the rest. */ |
| 329 | if (pci_dev->device < 0x1000 || pci_dev->device > 0x103f) |
| 330 | return -ENODEV; |
| 331 | |
Anthony Liguori | 55a7c06 | 2008-01-28 09:59:59 -0600 | [diff] [blame] | 332 | if (pci_dev->revision != VIRTIO_PCI_ABI_VERSION) { |
| 333 | printk(KERN_ERR "virtio_pci: expected ABI version %d, got %d\n", |
| 334 | VIRTIO_PCI_ABI_VERSION, pci_dev->revision); |
| 335 | return -ENODEV; |
| 336 | } |
| 337 | |
Anthony Liguori | 3343660 | 2007-11-12 21:30:26 -0600 | [diff] [blame] | 338 | /* allocate our structure and fill it out */ |
| 339 | vp_dev = kzalloc(sizeof(struct virtio_pci_device), GFP_KERNEL); |
| 340 | if (vp_dev == NULL) |
| 341 | return -ENOMEM; |
| 342 | |
Mark McLoughlin | 63d1255 | 2008-12-15 12:58:27 +0000 | [diff] [blame] | 343 | vp_dev->vdev.dev.parent = virtio_pci_root; |
Mark McLoughlin | 29f9f12 | 2008-12-10 17:45:34 +0000 | [diff] [blame] | 344 | vp_dev->vdev.dev.release = virtio_pci_release_dev; |
Anthony Liguori | 3343660 | 2007-11-12 21:30:26 -0600 | [diff] [blame] | 345 | vp_dev->vdev.config = &virtio_pci_config_ops; |
| 346 | vp_dev->pci_dev = pci_dev; |
| 347 | INIT_LIST_HEAD(&vp_dev->virtqueues); |
| 348 | spin_lock_init(&vp_dev->lock); |
| 349 | |
| 350 | /* enable the device */ |
| 351 | err = pci_enable_device(pci_dev); |
| 352 | if (err) |
| 353 | goto out; |
| 354 | |
| 355 | err = pci_request_regions(pci_dev, "virtio-pci"); |
| 356 | if (err) |
| 357 | goto out_enable_device; |
| 358 | |
| 359 | vp_dev->ioaddr = pci_iomap(pci_dev, 0, 0); |
| 360 | if (vp_dev->ioaddr == NULL) |
| 361 | goto out_req_regions; |
| 362 | |
| 363 | pci_set_drvdata(pci_dev, vp_dev); |
| 364 | |
| 365 | /* we use the subsystem vendor/device id as the virtio vendor/device |
| 366 | * id. this allows us to use the same PCI vendor/device id for all |
| 367 | * virtio devices and to identify the particular virtio driver by |
| 368 | * the subsytem ids */ |
| 369 | vp_dev->vdev.id.vendor = pci_dev->subsystem_vendor; |
| 370 | vp_dev->vdev.id.device = pci_dev->subsystem_device; |
| 371 | |
| 372 | /* register a handler for the queue with the PCI device's interrupt */ |
| 373 | err = request_irq(vp_dev->pci_dev->irq, vp_interrupt, IRQF_SHARED, |
Kay Sievers | 99e0b6c | 2008-12-30 09:25:56 -0600 | [diff] [blame] | 374 | dev_name(&vp_dev->vdev.dev), vp_dev); |
Anthony Liguori | 3343660 | 2007-11-12 21:30:26 -0600 | [diff] [blame] | 375 | if (err) |
| 376 | goto out_set_drvdata; |
| 377 | |
| 378 | /* finally register the virtio device */ |
| 379 | err = register_virtio_device(&vp_dev->vdev); |
| 380 | if (err) |
| 381 | goto out_req_irq; |
| 382 | |
| 383 | return 0; |
| 384 | |
| 385 | out_req_irq: |
| 386 | free_irq(pci_dev->irq, vp_dev); |
| 387 | out_set_drvdata: |
| 388 | pci_set_drvdata(pci_dev, NULL); |
| 389 | pci_iounmap(pci_dev, vp_dev->ioaddr); |
| 390 | out_req_regions: |
| 391 | pci_release_regions(pci_dev); |
| 392 | out_enable_device: |
| 393 | pci_disable_device(pci_dev); |
| 394 | out: |
| 395 | kfree(vp_dev); |
| 396 | return err; |
| 397 | } |
| 398 | |
| 399 | static void __devexit virtio_pci_remove(struct pci_dev *pci_dev) |
| 400 | { |
| 401 | struct virtio_pci_device *vp_dev = pci_get_drvdata(pci_dev); |
| 402 | |
Anthony Liguori | bd6c269 | 2008-03-19 20:35:04 -0500 | [diff] [blame] | 403 | unregister_virtio_device(&vp_dev->vdev); |
Anthony Liguori | 3343660 | 2007-11-12 21:30:26 -0600 | [diff] [blame] | 404 | } |
| 405 | |
| 406 | #ifdef CONFIG_PM |
| 407 | static int virtio_pci_suspend(struct pci_dev *pci_dev, pm_message_t state) |
| 408 | { |
| 409 | pci_save_state(pci_dev); |
| 410 | pci_set_power_state(pci_dev, PCI_D3hot); |
| 411 | return 0; |
| 412 | } |
| 413 | |
| 414 | static int virtio_pci_resume(struct pci_dev *pci_dev) |
| 415 | { |
| 416 | pci_restore_state(pci_dev); |
| 417 | pci_set_power_state(pci_dev, PCI_D0); |
| 418 | return 0; |
| 419 | } |
| 420 | #endif |
| 421 | |
| 422 | static struct pci_driver virtio_pci_driver = { |
| 423 | .name = "virtio-pci", |
| 424 | .id_table = virtio_pci_id_table, |
| 425 | .probe = virtio_pci_probe, |
| 426 | .remove = virtio_pci_remove, |
| 427 | #ifdef CONFIG_PM |
| 428 | .suspend = virtio_pci_suspend, |
| 429 | .resume = virtio_pci_resume, |
| 430 | #endif |
| 431 | }; |
| 432 | |
| 433 | static int __init virtio_pci_init(void) |
| 434 | { |
| 435 | int err; |
| 436 | |
Mark McLoughlin | 63d1255 | 2008-12-15 12:58:27 +0000 | [diff] [blame] | 437 | virtio_pci_root = root_device_register("virtio-pci"); |
| 438 | if (IS_ERR(virtio_pci_root)) |
| 439 | return PTR_ERR(virtio_pci_root); |
Anthony Liguori | 3343660 | 2007-11-12 21:30:26 -0600 | [diff] [blame] | 440 | |
| 441 | err = pci_register_driver(&virtio_pci_driver); |
| 442 | if (err) |
Mark McLoughlin | 63d1255 | 2008-12-15 12:58:27 +0000 | [diff] [blame] | 443 | device_unregister(virtio_pci_root); |
Anthony Liguori | 3343660 | 2007-11-12 21:30:26 -0600 | [diff] [blame] | 444 | |
| 445 | return err; |
| 446 | } |
| 447 | |
| 448 | module_init(virtio_pci_init); |
| 449 | |
| 450 | static void __exit virtio_pci_exit(void) |
| 451 | { |
Anthony Liguori | 3343660 | 2007-11-12 21:30:26 -0600 | [diff] [blame] | 452 | pci_unregister_driver(&virtio_pci_driver); |
Mark McLoughlin | 63d1255 | 2008-12-15 12:58:27 +0000 | [diff] [blame] | 453 | root_device_unregister(virtio_pci_root); |
Anthony Liguori | 3343660 | 2007-11-12 21:30:26 -0600 | [diff] [blame] | 454 | } |
| 455 | |
| 456 | module_exit(virtio_pci_exit); |