aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/virtio/virtio_pci.c
diff options
context:
space:
mode:
Diffstat (limited to 'drivers/virtio/virtio_pci.c')
-rw-r--r--drivers/virtio/virtio_pci.c420
1 files changed, 12 insertions, 408 deletions
diff --git a/drivers/virtio/virtio_pci.c b/drivers/virtio/virtio_pci.c
index 4de3cbc0746d..d73ceecaf1c3 100644
--- a/drivers/virtio/virtio_pci.c
+++ b/drivers/virtio/virtio_pci.c
@@ -14,179 +14,10 @@
14 * 14 *
15 */ 15 */
16 16
17#include <linux/module.h> 17#include "virtio_pci_legacy.c"
18#include <linux/list.h>
19#include <linux/pci.h>
20#include <linux/slab.h>
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
29MODULE_AUTHOR("Anthony Liguori <aliguori@us.ibm.com>");
30MODULE_DESCRIPTION("virtio-pci");
31MODULE_LICENSE("GPL");
32MODULE_VERSION("1");
33
34struct virtio_pci_vq_info {
35 /* the actual virtqueue */
36 struct virtqueue *vq;
37
38 /* the number of entries in the queue */
39 int num;
40
41 /* the virtual address of the ring queue */
42 void *queue;
43
44 /* the list node for the virtqueues list */
45 struct list_head node;
46
47 /* MSI-X vector (or none) */
48 unsigned msix_vector;
49};
50
51/* Our device structure */
52struct virtio_pci_device {
53 struct virtio_device vdev;
54 struct pci_dev *pci_dev;
55
56 /* the IO mapping for the PCI config space */
57 void __iomem *ioaddr;
58
59 /* the IO mapping for ISR operation */
60 void __iomem *isr;
61
62 /* a list of queues so we can dispatch IRQs */
63 spinlock_t lock;
64 struct list_head virtqueues;
65
66 /* array of all queues for house-keeping */
67 struct virtio_pci_vq_info **vqs;
68
69 /* MSI-X support */
70 int msix_enabled;
71 int intx_enabled;
72 struct msix_entry *msix_entries;
73 cpumask_var_t *msix_affinity_masks;
74 /* Name strings for interrupts. This size should be enough,
75 * and I'm too lazy to allocate each name separately. */
76 char (*msix_names)[256];
77 /* Number of available vectors */
78 unsigned msix_vectors;
79 /* Vectors allocated, excluding per-vq vectors if any */
80 unsigned msix_used_vectors;
81
82 /* Whether we have vector per vq */
83 bool per_vq_vectors;
84
85 struct virtqueue *(*setup_vq)(struct virtio_pci_device *vp_dev,
86 struct virtio_pci_vq_info *info,
87 unsigned idx,
88 void (*callback)(struct virtqueue *vq),
89 const char *name,
90 u16 msix_vec);
91 void (*del_vq)(struct virtio_pci_vq_info *info);
92 u16 (*config_vector)(struct virtio_pci_device *vp_dev, u16 vector);
93};
94
95/* Constants for MSI-X */
96/* Use first vector for configuration changes, second and the rest for
97 * virtqueues Thus, we need at least 2 vectors for MSI. */
98enum {
99 VP_MSIX_CONFIG_VECTOR = 0,
100 VP_MSIX_VQ_VECTOR = 1,
101};
102
103/* Qumranet donated their vendor ID for devices 0x1000 thru 0x10FF. */
104static const struct pci_device_id virtio_pci_id_table[] = {
105 { PCI_DEVICE(0x1af4, PCI_ANY_ID) },
106 { 0 }
107};
108
109MODULE_DEVICE_TABLE(pci, virtio_pci_id_table);
110
111/* Convert a generic virtio device to our structure */
112static struct virtio_pci_device *to_vp_device(struct virtio_device *vdev)
113{
114 return container_of(vdev, struct virtio_pci_device, vdev);
115}
116
117/* virtio config->get_features() implementation */
118static u64 vp_get_features(struct virtio_device *vdev)
119{
120 struct virtio_pci_device *vp_dev = to_vp_device(vdev);
121
122 /* When someone needs more than 32 feature bits, we'll need to
123 * steal a bit to indicate that the rest are somewhere else. */
124 return ioread32(vp_dev->ioaddr + VIRTIO_PCI_HOST_FEATURES);
125}
126
127/* virtio config->finalize_features() implementation */
128static int vp_finalize_features(struct virtio_device *vdev)
129{
130 struct virtio_pci_device *vp_dev = to_vp_device(vdev);
131
132 /* Give virtio_ring a chance to accept features. */
133 vring_transport_features(vdev);
134
135 /* Make sure we don't have any features > 32 bits! */
136 BUG_ON((u32)vdev->features != vdev->features);
137
138 /* We only support 32 feature bits. */
139 iowrite32(vdev->features, vp_dev->ioaddr + VIRTIO_PCI_GUEST_FEATURES);
140
141 return 0;
142}
143
144/* virtio config->get() implementation */
145static void vp_get(struct virtio_device *vdev, unsigned offset,
146 void *buf, unsigned len)
147{
148 struct virtio_pci_device *vp_dev = to_vp_device(vdev);
149 void __iomem *ioaddr = vp_dev->ioaddr +
150 VIRTIO_PCI_CONFIG(vp_dev) + offset;
151 u8 *ptr = buf;
152 int i;
153
154 for (i = 0; i < len; i++)
155 ptr[i] = ioread8(ioaddr + i);
156}
157
158/* the config->set() implementation. it's symmetric to the config->get()
159 * implementation */
160static void vp_set(struct virtio_device *vdev, unsigned offset,
161 const void *buf, unsigned len)
162{
163 struct virtio_pci_device *vp_dev = to_vp_device(vdev);
164 void __iomem *ioaddr = vp_dev->ioaddr +
165 VIRTIO_PCI_CONFIG(vp_dev) + offset;
166 const u8 *ptr = buf;
167 int i;
168
169 for (i = 0; i < len; i++)
170 iowrite8(ptr[i], ioaddr + i);
171}
172
173/* config->{get,set}_status() implementations */
174static u8 vp_get_status(struct virtio_device *vdev)
175{
176 struct virtio_pci_device *vp_dev = to_vp_device(vdev);
177 return ioread8(vp_dev->ioaddr + VIRTIO_PCI_STATUS);
178}
179
180static void vp_set_status(struct virtio_device *vdev, u8 status)
181{
182 struct virtio_pci_device *vp_dev = to_vp_device(vdev);
183 /* We should never be setting status to 0. */
184 BUG_ON(status == 0);
185 iowrite8(status, vp_dev->ioaddr + VIRTIO_PCI_STATUS);
186}
187 18
188/* wait for pending irq handlers */ 19/* wait for pending irq handlers */
189static void vp_synchronize_vectors(struct virtio_device *vdev) 20void vp_synchronize_vectors(struct virtio_device *vdev)
190{ 21{
191 struct virtio_pci_device *vp_dev = to_vp_device(vdev); 22 struct virtio_pci_device *vp_dev = to_vp_device(vdev);
192 int i; 23 int i;
@@ -198,20 +29,8 @@ static void vp_synchronize_vectors(struct virtio_device *vdev)
198 synchronize_irq(vp_dev->msix_entries[i].vector); 29 synchronize_irq(vp_dev->msix_entries[i].vector);
199} 30}
200 31
201static void vp_reset(struct virtio_device *vdev)
202{
203 struct virtio_pci_device *vp_dev = to_vp_device(vdev);
204 /* 0 status means a reset. */
205 iowrite8(0, vp_dev->ioaddr + VIRTIO_PCI_STATUS);
206 /* Flush out the status write, and flush in device writes,
207 * including MSi-X interrupts, if any. */
208 ioread8(vp_dev->ioaddr + VIRTIO_PCI_STATUS);
209 /* Flush pending VQ/configuration callbacks. */
210 vp_synchronize_vectors(vdev);
211}
212
213/* the notify function used when creating a virt queue */ 32/* the notify function used when creating a virt queue */
214static bool vp_notify(struct virtqueue *vq) 33bool vp_notify(struct virtqueue *vq)
215{ 34{
216 /* we write the queue's selector into the notification register to 35 /* we write the queue's selector into the notification register to
217 * signal the other end */ 36 * signal the other end */
@@ -272,15 +91,6 @@ static irqreturn_t vp_interrupt(int irq, void *opaque)
272 return vp_vring_interrupt(irq, opaque); 91 return vp_vring_interrupt(irq, opaque);
273} 92}
274 93
275static u16 vp_config_vector(struct virtio_pci_device *vp_dev, u16 vector)
276{
277 /* Setup the vector used for configuration events */
278 iowrite16(vector, vp_dev->ioaddr + VIRTIO_MSI_CONFIG_VECTOR);
279 /* Verify we had enough resources to assign the vector */
280 /* Will also flush the write out to device */
281 return ioread16(vp_dev->ioaddr + VIRTIO_MSI_CONFIG_VECTOR);
282}
283
284static void vp_free_vectors(struct virtio_device *vdev) 94static void vp_free_vectors(struct virtio_device *vdev)
285{ 95{
286 struct virtio_pci_device *vp_dev = to_vp_device(vdev); 96 struct virtio_pci_device *vp_dev = to_vp_device(vdev);
@@ -401,68 +211,6 @@ static int vp_request_intx(struct virtio_device *vdev)
401 return err; 211 return err;
402} 212}
403 213
404static struct virtqueue *setup_vq(struct virtio_pci_device *vp_dev,
405 struct virtio_pci_vq_info *info,
406 unsigned index,
407 void (*callback)(struct virtqueue *vq),
408 const char *name,
409 u16 msix_vec)
410{
411 struct virtqueue *vq;
412 unsigned long size;
413 u16 num;
414 int err;
415
416 /* Select the queue we're interested in */
417 iowrite16(index, vp_dev->ioaddr + VIRTIO_PCI_QUEUE_SEL);
418
419 /* Check if queue is either not available or already active. */
420 num = ioread16(vp_dev->ioaddr + VIRTIO_PCI_QUEUE_NUM);
421 if (!num || ioread32(vp_dev->ioaddr + VIRTIO_PCI_QUEUE_PFN))
422 return ERR_PTR(-ENOENT);
423
424 info->num = num;
425 info->msix_vector = msix_vec;
426
427 size = PAGE_ALIGN(vring_size(num, VIRTIO_PCI_VRING_ALIGN));
428 info->queue = alloc_pages_exact(size, GFP_KERNEL|__GFP_ZERO);
429 if (info->queue == NULL)
430 return ERR_PTR(-ENOMEM);
431
432 /* activate the queue */
433 iowrite32(virt_to_phys(info->queue) >> VIRTIO_PCI_QUEUE_ADDR_SHIFT,
434 vp_dev->ioaddr + VIRTIO_PCI_QUEUE_PFN);
435
436 /* create the vring */
437 vq = vring_new_virtqueue(index, info->num,
438 VIRTIO_PCI_VRING_ALIGN, &vp_dev->vdev,
439 true, info->queue, vp_notify, callback, name);
440 if (!vq) {
441 err = -ENOMEM;
442 goto out_activate_queue;
443 }
444
445 vq->priv = (void __force *)vp_dev->ioaddr + VIRTIO_PCI_QUEUE_NOTIFY;
446
447 if (msix_vec != VIRTIO_MSI_NO_VECTOR) {
448 iowrite16(msix_vec, vp_dev->ioaddr + VIRTIO_MSI_QUEUE_VECTOR);
449 msix_vec = ioread16(vp_dev->ioaddr + VIRTIO_MSI_QUEUE_VECTOR);
450 if (msix_vec == VIRTIO_MSI_NO_VECTOR) {
451 err = -EBUSY;
452 goto out_assign;
453 }
454 }
455
456 return vq;
457
458out_assign:
459 vring_del_virtqueue(vq);
460out_activate_queue:
461 iowrite32(0, vp_dev->ioaddr + VIRTIO_PCI_QUEUE_PFN);
462 free_pages_exact(info->queue, size);
463 return ERR_PTR(err);
464}
465
466static struct virtqueue *vp_setup_vq(struct virtio_device *vdev, unsigned index, 214static struct virtqueue *vp_setup_vq(struct virtio_device *vdev, unsigned index,
467 void (*callback)(struct virtqueue *vq), 215 void (*callback)(struct virtqueue *vq),
468 const char *name, 216 const char *name,
@@ -498,30 +246,6 @@ out_info:
498 return vq; 246 return vq;
499} 247}
500 248
501static void del_vq(struct virtio_pci_vq_info *info)
502{
503 struct virtqueue *vq = info->vq;
504 struct virtio_pci_device *vp_dev = to_vp_device(vq->vdev);
505 unsigned long size;
506
507 iowrite16(vq->index, vp_dev->ioaddr + VIRTIO_PCI_QUEUE_SEL);
508
509 if (vp_dev->msix_enabled) {
510 iowrite16(VIRTIO_MSI_NO_VECTOR,
511 vp_dev->ioaddr + VIRTIO_MSI_QUEUE_VECTOR);
512 /* Flush the write out to device */
513 ioread8(vp_dev->ioaddr + VIRTIO_PCI_ISR);
514 }
515
516 vring_del_virtqueue(vq);
517
518 /* Select and deactivate the queue */
519 iowrite32(0, vp_dev->ioaddr + VIRTIO_PCI_QUEUE_PFN);
520
521 size = PAGE_ALIGN(vring_size(info->num, VIRTIO_PCI_VRING_ALIGN));
522 free_pages_exact(info->queue, size);
523}
524
525static void vp_del_vq(struct virtqueue *vq) 249static void vp_del_vq(struct virtqueue *vq)
526{ 250{
527 struct virtio_pci_device *vp_dev = to_vp_device(vq->vdev); 251 struct virtio_pci_device *vp_dev = to_vp_device(vq->vdev);
@@ -537,7 +261,7 @@ static void vp_del_vq(struct virtqueue *vq)
537} 261}
538 262
539/* the config->del_vqs() implementation */ 263/* the config->del_vqs() implementation */
540static void vp_del_vqs(struct virtio_device *vdev) 264void vp_del_vqs(struct virtio_device *vdev)
541{ 265{
542 struct virtio_pci_device *vp_dev = to_vp_device(vdev); 266 struct virtio_pci_device *vp_dev = to_vp_device(vdev);
543 struct virtqueue *vq, *n; 267 struct virtqueue *vq, *n;
@@ -637,10 +361,10 @@ error_find:
637} 361}
638 362
639/* the config->find_vqs() implementation */ 363/* the config->find_vqs() implementation */
640static int vp_find_vqs(struct virtio_device *vdev, unsigned nvqs, 364int vp_find_vqs(struct virtio_device *vdev, unsigned nvqs,
641 struct virtqueue *vqs[], 365 struct virtqueue *vqs[],
642 vq_callback_t *callbacks[], 366 vq_callback_t *callbacks[],
643 const char *names[]) 367 const char *names[])
644{ 368{
645 int err; 369 int err;
646 370
@@ -658,7 +382,7 @@ static int vp_find_vqs(struct virtio_device *vdev, unsigned nvqs,
658 false, false); 382 false, false);
659} 383}
660 384
661static const char *vp_bus_name(struct virtio_device *vdev) 385const char *vp_bus_name(struct virtio_device *vdev)
662{ 386{
663 struct virtio_pci_device *vp_dev = to_vp_device(vdev); 387 struct virtio_pci_device *vp_dev = to_vp_device(vdev);
664 388
@@ -670,7 +394,7 @@ static const char *vp_bus_name(struct virtio_device *vdev)
670 * - OR over all affinities for shared MSI 394 * - OR over all affinities for shared MSI
671 * - ignore the affinity request if we're using INTX 395 * - ignore the affinity request if we're using INTX
672 */ 396 */
673static int vp_set_vq_affinity(struct virtqueue *vq, int cpu) 397int vp_set_vq_affinity(struct virtqueue *vq, int cpu)
674{ 398{
675 struct virtio_device *vdev = vq->vdev; 399 struct virtio_device *vdev = vq->vdev;
676 struct virtio_pci_device *vp_dev = to_vp_device(vdev); 400 struct virtio_pci_device *vp_dev = to_vp_device(vdev);
@@ -694,21 +418,7 @@ static int vp_set_vq_affinity(struct virtqueue *vq, int cpu)
694 return 0; 418 return 0;
695} 419}
696 420
697static const struct virtio_config_ops virtio_pci_config_ops = { 421void virtio_pci_release_dev(struct device *_d)
698 .get = vp_get,
699 .set = vp_set,
700 .get_status = vp_get_status,
701 .set_status = vp_set_status,
702 .reset = vp_reset,
703 .find_vqs = vp_find_vqs,
704 .del_vqs = vp_del_vqs,
705 .get_features = vp_get_features,
706 .finalize_features = vp_finalize_features,
707 .bus_name = vp_bus_name,
708 .set_vq_affinity = vp_set_vq_affinity,
709};
710
711static void virtio_pci_release_dev(struct device *_d)
712{ 422{
713 /* 423 /*
714 * No need for a release method as we allocate/free 424 * No need for a release method as we allocate/free
@@ -717,100 +427,6 @@ static void virtio_pci_release_dev(struct device *_d)
717 */ 427 */
718} 428}
719 429
720/* the PCI probing function */
721static int virtio_pci_probe(struct pci_dev *pci_dev,
722 const struct pci_device_id *id)
723{
724 struct virtio_pci_device *vp_dev;
725 int err;
726
727 /* We only own devices >= 0x1000 and <= 0x103f: leave the rest. */
728 if (pci_dev->device < 0x1000 || pci_dev->device > 0x103f)
729 return -ENODEV;
730
731 if (pci_dev->revision != VIRTIO_PCI_ABI_VERSION) {
732 printk(KERN_ERR "virtio_pci: expected ABI version %d, got %d\n",
733 VIRTIO_PCI_ABI_VERSION, pci_dev->revision);
734 return -ENODEV;
735 }
736
737 /* allocate our structure and fill it out */
738 vp_dev = kzalloc(sizeof(struct virtio_pci_device), GFP_KERNEL);
739 if (vp_dev == NULL)
740 return -ENOMEM;
741
742 vp_dev->vdev.dev.parent = &pci_dev->dev;
743 vp_dev->vdev.dev.release = virtio_pci_release_dev;
744 vp_dev->vdev.config = &virtio_pci_config_ops;
745 vp_dev->pci_dev = pci_dev;
746 INIT_LIST_HEAD(&vp_dev->virtqueues);
747 spin_lock_init(&vp_dev->lock);
748
749 /* Disable MSI/MSIX to bring device to a known good state. */
750 pci_msi_off(pci_dev);
751
752 /* enable the device */
753 err = pci_enable_device(pci_dev);
754 if (err)
755 goto out;
756
757 err = pci_request_regions(pci_dev, "virtio-pci");
758 if (err)
759 goto out_enable_device;
760
761 vp_dev->ioaddr = pci_iomap(pci_dev, 0, 0);
762 if (vp_dev->ioaddr == NULL) {
763 err = -ENOMEM;
764 goto out_req_regions;
765 }
766
767 vp_dev->isr = vp_dev->ioaddr + VIRTIO_PCI_ISR;
768
769 pci_set_drvdata(pci_dev, vp_dev);
770 pci_set_master(pci_dev);
771
772 /* we use the subsystem vendor/device id as the virtio vendor/device
773 * id. this allows us to use the same PCI vendor/device id for all
774 * virtio devices and to identify the particular virtio driver by
775 * the subsystem ids */
776 vp_dev->vdev.id.vendor = pci_dev->subsystem_vendor;
777 vp_dev->vdev.id.device = pci_dev->subsystem_device;
778
779 vp_dev->config_vector = vp_config_vector;
780 vp_dev->setup_vq = setup_vq;
781 vp_dev->del_vq = del_vq;
782
783 /* finally register the virtio device */
784 err = register_virtio_device(&vp_dev->vdev);
785 if (err)
786 goto out_set_drvdata;
787
788 return 0;
789
790out_set_drvdata:
791 pci_iounmap(pci_dev, vp_dev->ioaddr);
792out_req_regions:
793 pci_release_regions(pci_dev);
794out_enable_device:
795 pci_disable_device(pci_dev);
796out:
797 kfree(vp_dev);
798 return err;
799}
800
801static void virtio_pci_remove(struct pci_dev *pci_dev)
802{
803 struct virtio_pci_device *vp_dev = pci_get_drvdata(pci_dev);
804
805 unregister_virtio_device(&vp_dev->vdev);
806
807 vp_del_vqs(&vp_dev->vdev);
808 pci_iounmap(pci_dev, vp_dev->ioaddr);
809 pci_release_regions(pci_dev);
810 pci_disable_device(pci_dev);
811 kfree(vp_dev);
812}
813
814#ifdef CONFIG_PM_SLEEP 430#ifdef CONFIG_PM_SLEEP
815static int virtio_pci_freeze(struct device *dev) 431static int virtio_pci_freeze(struct device *dev)
816{ 432{
@@ -839,19 +455,7 @@ static int virtio_pci_restore(struct device *dev)
839 return virtio_device_restore(&vp_dev->vdev); 455 return virtio_device_restore(&vp_dev->vdev);
840} 456}
841 457
842static const struct dev_pm_ops virtio_pci_pm_ops = { 458const struct dev_pm_ops virtio_pci_pm_ops = {
843 SET_SYSTEM_SLEEP_PM_OPS(virtio_pci_freeze, virtio_pci_restore) 459 SET_SYSTEM_SLEEP_PM_OPS(virtio_pci_freeze, virtio_pci_restore)
844}; 460};
845#endif 461#endif
846
847static struct pci_driver virtio_pci_driver = {
848 .name = "virtio-pci",
849 .id_table = virtio_pci_id_table,
850 .probe = virtio_pci_probe,
851 .remove = virtio_pci_remove,
852#ifdef CONFIG_PM_SLEEP
853 .driver.pm = &virtio_pci_pm_ops,
854#endif
855};
856
857module_pci_driver(virtio_pci_driver);