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