17 #include <linux/module.h>
18 #include <linux/list.h>
19 #include <linux/pci.h>
20 #include <linux/slab.h>
22 #include <linux/virtio.h>
23 #include <linux/virtio_config.h>
24 #include <linux/virtio_ring.h>
132 void *
buf,
unsigned len)
140 for (i = 0; i < len; i++)
146 static void vp_set(
struct virtio_device *vdev,
unsigned offset,
147 const void *buf,
unsigned len)
155 for (i = 0; i < len; i++)
175 static void vp_synchronize_vectors(
struct virtio_device *vdev)
196 vp_synchronize_vectors(vdev);
200 static void vp_notify(
struct virtqueue *vq)
211 static irqreturn_t vp_config_changed(
int irq,
void *opaque)
224 static irqreturn_t vp_vring_interrupt(
int irq,
void *opaque)
236 spin_unlock_irqrestore(&vp_dev->
lock, flags);
247 static irqreturn_t vp_interrupt(
int irq,
void *opaque)
262 vp_config_changed(irq, opaque);
264 return vp_vring_interrupt(irq, opaque);
305 static int vp_request_msix_vectors(
struct virtio_device *vdev,
int nvectors,
309 const char *
name = dev_name(&vp_dev->
vdev.dev);
326 for (i = 0; i < nvectors; ++
i)
331 for (i = 0; i < nvectors; ++
i)
362 if (!per_vq_vectors) {
366 "%s-virtqueues", name);
376 vp_free_vectors(vdev);
437 goto out_activate_queue;
455 spin_unlock_irqrestore(&vp_dev->
lock, flags);
457 INIT_LIST_HEAD(&info->
node);
472 static void vp_del_vq(
struct virtqueue *vq)
480 spin_unlock_irqrestore(&vp_dev->
lock, flags);
519 vp_free_vectors(vdev);
522 static int vp_try_to_find_vqs(
struct virtio_device *vdev,
unsigned nvqs,
531 int i,
err, nvectors, allocated_vectors;
535 err = vp_request_intx(vdev);
539 if (per_vq_vectors) {
542 for (i = 0; i < nvqs; ++
i)
550 err = vp_request_msix_vectors(vdev, nvectors, per_vq_vectors);
557 for (i = 0; i < nvqs; ++
i) {
564 msix_vec = allocated_vectors++;
567 vqs[
i] = setup_vq(vdev, i, callbacks[i], names[i], msix_vec);
568 if (IS_ERR(vqs[i])) {
569 err = PTR_ERR(vqs[i]);
580 dev_name(&vp_dev->
vdev.dev), names[i]);
600 static int vp_find_vqs(
struct virtio_device *vdev,
unsigned nvqs,
608 err = vp_try_to_find_vqs(vdev, nvqs, vqs, callbacks, names,
true,
true);
612 err = vp_try_to_find_vqs(vdev, nvqs, vqs, callbacks, names,
617 return vp_try_to_find_vqs(vdev, nvqs, vqs, callbacks, names,
625 return pci_name(vp_dev->
pci_dev);
633 static int vp_set_vq_affinity(
struct virtqueue *vq,
int cpu)
648 irq_set_affinity_hint(irq,
NULL);
650 cpumask_set_cpu(cpu, mask);
651 irq_set_affinity_hint(irq, mask);
660 .get_status = vp_get_status,
661 .set_status = vp_set_status,
663 .find_vqs = vp_find_vqs,
664 .del_vqs = vp_del_vqs,
665 .get_features = vp_get_features,
666 .finalize_features = vp_finalize_features,
667 .bus_name = vp_bus_name,
668 .set_vq_affinity = vp_set_vq_affinity,
671 static void virtio_pci_release_dev(
struct device *
_d)
688 if (pci_dev->
device < 0x1000 || pci_dev->
device > 0x103f)
702 vp_dev->
vdev.dev.parent = &pci_dev->
dev;
703 vp_dev->
vdev.dev.release = virtio_pci_release_dev;
704 vp_dev->
vdev.config = &virtio_pci_config_ops;
719 goto out_enable_device;
721 vp_dev->
ioaddr = pci_iomap(pci_dev, 0, 0);
724 goto out_req_regions;
727 pci_set_drvdata(pci_dev, vp_dev);
740 goto out_set_drvdata;
745 pci_set_drvdata(pci_dev,
NULL);
756 static void __devexit virtio_pci_remove(
struct pci_dev *pci_dev)
762 vp_del_vqs(&vp_dev->
vdev);
763 pci_set_drvdata(pci_dev,
NULL);
771 static int virtio_pci_freeze(
struct device *
dev)
783 if (drv && drv->freeze)
784 ret = drv->freeze(&vp_dev->
vdev);
791 static int virtio_pci_restore(
struct device *dev)
806 vp_finalize_features(&vp_dev->
vdev);
808 if (drv && drv->restore)
809 ret = drv->restore(&vp_dev->
vdev);
818 static const struct dev_pm_ops virtio_pci_pm_ops = {
823 static struct pci_driver virtio_pci_driver = {
824 .name =
"virtio-pci",
825 .id_table = virtio_pci_id_table,
826 .probe = virtio_pci_probe,
829 .driver.pm = &virtio_pci_pm_ops,
833 static int __init virtio_pci_init(
void)
835 return pci_register_driver(&virtio_pci_driver);
840 static void __exit virtio_pci_exit(
void)