]> git.karo-electronics.de Git - karo-tx-linux.git/blobdiff - drivers/virtio/virtio_pci_common.c
Merge branch 'for-linus' into for-4.12/block
[karo-tx-linux.git] / drivers / virtio / virtio_pci_common.c
index 274dc1ff09c03a81948a830a896d5d7415092a3f..590534910dc617836e18c91b6576410a0299de26 100644 (file)
@@ -125,7 +125,7 @@ void vp_del_vqs(struct virtio_device *vdev)
 
        vp_remove_vqs(vdev);
 
-       if (vp_dev->msix_enabled) {
+       if (vp_dev->pci_dev->msix_enabled) {
                for (i = 0; i < vp_dev->msix_vectors; i++)
                        free_cpumask_var(vp_dev->msix_affinity_masks[i]);
 
@@ -142,27 +142,36 @@ void vp_del_vqs(struct virtio_device *vdev)
 }
 
 static int vp_find_vqs_msix(struct virtio_device *vdev, unsigned nvqs,
-                             struct virtqueue *vqs[],
-                             vq_callback_t *callbacks[],
-                             const char * const names[],
-                             bool per_vq_vectors)
+               struct virtqueue *vqs[], vq_callback_t *callbacks[],
+               const char * const names[], struct irq_affinity *desc)
 {
        struct virtio_pci_device *vp_dev = to_vp_device(vdev);
        const char *name = dev_name(&vp_dev->vdev.dev);
-       int i, err = -ENOMEM, allocated_vectors, nvectors;
+       int i, j, err = -ENOMEM, allocated_vectors, nvectors;
+       unsigned flags = PCI_IRQ_MSIX;
+       bool shared = false;
        u16 msix_vec;
 
+       if (desc) {
+               flags |= PCI_IRQ_AFFINITY;
+               desc->pre_vectors++; /* virtio config vector */
+       }
+
        nvectors = 1;
        for (i = 0; i < nvqs; i++)
                if (callbacks[i])
                        nvectors++;
 
-       if (per_vq_vectors) {
-               err = pci_alloc_irq_vectors(vp_dev->pci_dev, nvectors, nvectors,
-                               PCI_IRQ_MSIX);
-       } else {
+       /* Try one vector per queue first. */
+       err = pci_alloc_irq_vectors_affinity(vp_dev->pci_dev, nvectors,
+                       nvectors, flags, desc);
+       if (err < 0) {
+               /* Fallback to one vector for config, one shared for queues. */
+               shared = true;
                err = pci_alloc_irq_vectors(vp_dev->pci_dev, 2, 2,
                                PCI_IRQ_MSIX);
+               if (err < 0)
+                       return err;
        }
        if (err < 0)
                return err;
@@ -190,7 +199,7 @@ static int vp_find_vqs_msix(struct virtio_device *vdev, unsigned nvqs,
        err = request_irq(pci_irq_vector(vp_dev->pci_dev, 0), vp_config_changed,
                        0, vp_dev->msix_names[0], vp_dev);
        if (err)
-               goto out_free_irq_vectors;
+               goto out_free_msix_affinity_masks;
 
        /* Verify we had enough resources to assign the vector */
        if (vp_dev->config_vector(vp_dev, 0) == VIRTIO_MSI_NO_VECTOR) {
@@ -203,7 +212,7 @@ static int vp_find_vqs_msix(struct virtio_device *vdev, unsigned nvqs,
        if (!vp_dev->msix_vector_map)
                goto out_disable_config_irq;
 
-       allocated_vectors = 1; /* vector 0 is the config interrupt */
+       allocated_vectors = j = 1; /* vector 0 is the config interrupt */
        for (i = 0; i < nvqs; ++i) {
                if (!names[i]) {
                        vqs[i] = NULL;
@@ -227,24 +236,28 @@ static int vp_find_vqs_msix(struct virtio_device *vdev, unsigned nvqs,
                        continue;
                }
 
-               snprintf(vp_dev->msix_names[i + 1],
+               snprintf(vp_dev->msix_names[j],
                         sizeof(*vp_dev->msix_names), "%s-%s",
                         dev_name(&vp_dev->vdev.dev), names[i]);
                err = request_irq(pci_irq_vector(vp_dev->pci_dev, msix_vec),
                                  vring_interrupt, IRQF_SHARED,
-                                 vp_dev->msix_names[i + 1], vqs[i]);
+                                 vp_dev->msix_names[j], vqs[i]);
                if (err) {
                        /* don't free this irq on error */
                        vp_dev->msix_vector_map[i] = VIRTIO_MSI_NO_VECTOR;
                        goto out_remove_vqs;
                }
                vp_dev->msix_vector_map[i] = msix_vec;
+               j++;
 
-               if (per_vq_vectors)
+               /*
+                * Use a different vector for each queue if they are available,
+                * else share the same vector for all VQs.
+                */
+               if (!shared)
                        allocated_vectors++;
        }
 
-       vp_dev->msix_enabled = 1;
        return 0;
 
 out_remove_vqs:
@@ -302,21 +315,14 @@ out_remove_vqs:
 
 /* the config->find_vqs() implementation */
 int vp_find_vqs(struct virtio_device *vdev, unsigned nvqs,
-               struct virtqueue *vqs[],
-               vq_callback_t *callbacks[],
-               const char * const names[])
+               struct virtqueue *vqs[], vq_callback_t *callbacks[],
+               const char * const names[], struct irq_affinity *desc)
 {
        int err;
 
-       /* Try MSI-X with one vector per queue. */
-       err = vp_find_vqs_msix(vdev, nvqs, vqs, callbacks, names, true);
+       err = vp_find_vqs_msix(vdev, nvqs, vqs, callbacks, names, desc);
        if (!err)
                return 0;
-       /* Fallback: MSI-X with one vector for config, one shared for queues. */
-       err = vp_find_vqs_msix(vdev, nvqs, vqs, callbacks, names, false);
-       if (!err)
-               return 0;
-       /* Finally fall back to regular interrupts. */
        return vp_find_vqs_intx(vdev, nvqs, vqs, callbacks, names);
 }
 
@@ -340,7 +346,7 @@ int vp_set_vq_affinity(struct virtqueue *vq, int cpu)
        if (!vq->callback)
                return -EINVAL;
 
-       if (vp_dev->msix_enabled) {
+       if (vp_dev->pci_dev->msix_enabled) {
                int vec = vp_dev->msix_vector_map[vq->index];
                struct cpumask *mask = vp_dev->msix_affinity_masks[vec];
                unsigned int irq = pci_irq_vector(vp_dev->pci_dev, vec);
@@ -356,6 +362,17 @@ int vp_set_vq_affinity(struct virtqueue *vq, int cpu)
        return 0;
 }
 
+const struct cpumask *vp_get_vq_affinity(struct virtio_device *vdev, int index)
+{
+       struct virtio_pci_device *vp_dev = to_vp_device(vdev);
+       unsigned int *map = vp_dev->msix_vector_map;
+
+       if (!map || map[index] == VIRTIO_MSI_NO_VECTOR)
+               return NULL;
+
+       return pci_irq_get_affinity(vp_dev->pci_dev, map[index]);
+}
+
 #ifdef CONFIG_PM_SLEEP
 static int virtio_pci_freeze(struct device *dev)
 {