15f4c9760SMichael S. Tsirkin /* 25f4c9760SMichael S. Tsirkin * Virtio PCI driver - common functionality for all device versions 35f4c9760SMichael S. Tsirkin * 45f4c9760SMichael S. Tsirkin * This module allows virtio devices to be used over a virtual PCI device. 55f4c9760SMichael S. Tsirkin * This can be used with QEMU based VMMs like KVM or Xen. 65f4c9760SMichael S. Tsirkin * 75f4c9760SMichael S. Tsirkin * Copyright IBM Corp. 2007 85f4c9760SMichael S. Tsirkin * Copyright Red Hat, Inc. 2014 95f4c9760SMichael S. Tsirkin * 105f4c9760SMichael S. Tsirkin * Authors: 115f4c9760SMichael S. Tsirkin * Anthony Liguori <aliguori@us.ibm.com> 125f4c9760SMichael S. Tsirkin * Rusty Russell <rusty@rustcorp.com.au> 135f4c9760SMichael S. Tsirkin * Michael S. Tsirkin <mst@redhat.com> 145f4c9760SMichael S. Tsirkin * 155f4c9760SMichael S. Tsirkin * This work is licensed under the terms of the GNU GPL, version 2 or later. 165f4c9760SMichael S. Tsirkin * See the COPYING file in the top-level directory. 175f4c9760SMichael S. Tsirkin * 185f4c9760SMichael S. Tsirkin */ 195f4c9760SMichael S. Tsirkin 205f4c9760SMichael S. Tsirkin #include "virtio_pci_common.h" 215f4c9760SMichael S. Tsirkin 22ac399d8fSMichael S. Tsirkin static bool force_legacy = false; 23ac399d8fSMichael S. Tsirkin 24ac399d8fSMichael S. Tsirkin #if IS_ENABLED(CONFIG_VIRTIO_PCI_LEGACY) 25ac399d8fSMichael S. Tsirkin module_param(force_legacy, bool, 0444); 26ac399d8fSMichael S. Tsirkin MODULE_PARM_DESC(force_legacy, 27ac399d8fSMichael S. Tsirkin "Force legacy mode for transitional virtio 1 devices"); 28ac399d8fSMichael S. Tsirkin #endif 29ac399d8fSMichael S. Tsirkin 305f4c9760SMichael S. Tsirkin /* wait for pending irq handlers */ 315f4c9760SMichael S. Tsirkin void vp_synchronize_vectors(struct virtio_device *vdev) 325f4c9760SMichael S. Tsirkin { 335f4c9760SMichael S. Tsirkin struct virtio_pci_device *vp_dev = to_vp_device(vdev); 345f4c9760SMichael S. Tsirkin int i; 355f4c9760SMichael S. Tsirkin 365f4c9760SMichael S. Tsirkin if (vp_dev->intx_enabled) 375f4c9760SMichael S. Tsirkin synchronize_irq(vp_dev->pci_dev->irq); 385f4c9760SMichael S. Tsirkin 395f4c9760SMichael S. Tsirkin for (i = 0; i < vp_dev->msix_vectors; ++i) 405f4c9760SMichael S. Tsirkin synchronize_irq(vp_dev->msix_entries[i].vector); 415f4c9760SMichael S. Tsirkin } 425f4c9760SMichael S. Tsirkin 435f4c9760SMichael S. Tsirkin /* the notify function used when creating a virt queue */ 445f4c9760SMichael S. Tsirkin bool vp_notify(struct virtqueue *vq) 455f4c9760SMichael S. Tsirkin { 465f4c9760SMichael S. Tsirkin /* we write the queue's selector into the notification register to 475f4c9760SMichael S. Tsirkin * signal the other end */ 485f4c9760SMichael S. Tsirkin iowrite16(vq->index, (void __iomem *)vq->priv); 495f4c9760SMichael S. Tsirkin return true; 505f4c9760SMichael S. Tsirkin } 515f4c9760SMichael S. Tsirkin 525f4c9760SMichael S. Tsirkin /* Handle a configuration change: Tell driver if it wants to know. */ 535f4c9760SMichael S. Tsirkin static irqreturn_t vp_config_changed(int irq, void *opaque) 545f4c9760SMichael S. Tsirkin { 555f4c9760SMichael S. Tsirkin struct virtio_pci_device *vp_dev = opaque; 565f4c9760SMichael S. Tsirkin 575f4c9760SMichael S. Tsirkin virtio_config_changed(&vp_dev->vdev); 585f4c9760SMichael S. Tsirkin return IRQ_HANDLED; 595f4c9760SMichael S. Tsirkin } 605f4c9760SMichael S. Tsirkin 615f4c9760SMichael S. Tsirkin /* Notify all virtqueues on an interrupt. */ 625f4c9760SMichael S. Tsirkin static irqreturn_t vp_vring_interrupt(int irq, void *opaque) 635f4c9760SMichael S. Tsirkin { 645f4c9760SMichael S. Tsirkin struct virtio_pci_device *vp_dev = opaque; 655f4c9760SMichael S. Tsirkin struct virtio_pci_vq_info *info; 665f4c9760SMichael S. Tsirkin irqreturn_t ret = IRQ_NONE; 675f4c9760SMichael S. Tsirkin unsigned long flags; 685f4c9760SMichael S. Tsirkin 695f4c9760SMichael S. Tsirkin spin_lock_irqsave(&vp_dev->lock, flags); 705f4c9760SMichael S. Tsirkin list_for_each_entry(info, &vp_dev->virtqueues, node) { 715f4c9760SMichael S. Tsirkin if (vring_interrupt(irq, info->vq) == IRQ_HANDLED) 725f4c9760SMichael S. Tsirkin ret = IRQ_HANDLED; 735f4c9760SMichael S. Tsirkin } 745f4c9760SMichael S. Tsirkin spin_unlock_irqrestore(&vp_dev->lock, flags); 755f4c9760SMichael S. Tsirkin 765f4c9760SMichael S. Tsirkin return ret; 775f4c9760SMichael S. Tsirkin } 785f4c9760SMichael S. Tsirkin 795f4c9760SMichael S. Tsirkin /* A small wrapper to also acknowledge the interrupt when it's handled. 805f4c9760SMichael S. Tsirkin * I really need an EIO hook for the vring so I can ack the interrupt once we 815f4c9760SMichael S. Tsirkin * know that we'll be handling the IRQ but before we invoke the callback since 825f4c9760SMichael S. Tsirkin * the callback may notify the host which results in the host attempting to 835f4c9760SMichael S. Tsirkin * raise an interrupt that we would then mask once we acknowledged the 845f4c9760SMichael S. Tsirkin * interrupt. */ 855f4c9760SMichael S. Tsirkin static irqreturn_t vp_interrupt(int irq, void *opaque) 865f4c9760SMichael S. Tsirkin { 875f4c9760SMichael S. Tsirkin struct virtio_pci_device *vp_dev = opaque; 885f4c9760SMichael S. Tsirkin u8 isr; 895f4c9760SMichael S. Tsirkin 905f4c9760SMichael S. Tsirkin /* reading the ISR has the effect of also clearing it so it's very 915f4c9760SMichael S. Tsirkin * important to save off the value. */ 925f4c9760SMichael S. Tsirkin isr = ioread8(vp_dev->isr); 935f4c9760SMichael S. Tsirkin 945f4c9760SMichael S. Tsirkin /* It's definitely not us if the ISR was not high */ 955f4c9760SMichael S. Tsirkin if (!isr) 965f4c9760SMichael S. Tsirkin return IRQ_NONE; 975f4c9760SMichael S. Tsirkin 985f4c9760SMichael S. Tsirkin /* Configuration change? Tell driver if it wants to know. */ 995f4c9760SMichael S. Tsirkin if (isr & VIRTIO_PCI_ISR_CONFIG) 1005f4c9760SMichael S. Tsirkin vp_config_changed(irq, opaque); 1015f4c9760SMichael S. Tsirkin 1025f4c9760SMichael S. Tsirkin return vp_vring_interrupt(irq, opaque); 1035f4c9760SMichael S. Tsirkin } 1045f4c9760SMichael S. Tsirkin 1055f4c9760SMichael S. Tsirkin static void vp_free_vectors(struct virtio_device *vdev) 1065f4c9760SMichael S. Tsirkin { 1075f4c9760SMichael S. Tsirkin struct virtio_pci_device *vp_dev = to_vp_device(vdev); 1085f4c9760SMichael S. Tsirkin int i; 1095f4c9760SMichael S. Tsirkin 1105f4c9760SMichael S. Tsirkin if (vp_dev->intx_enabled) { 1115f4c9760SMichael S. Tsirkin free_irq(vp_dev->pci_dev->irq, vp_dev); 1125f4c9760SMichael S. Tsirkin vp_dev->intx_enabled = 0; 1135f4c9760SMichael S. Tsirkin } 1145f4c9760SMichael S. Tsirkin 1155f4c9760SMichael S. Tsirkin for (i = 0; i < vp_dev->msix_used_vectors; ++i) 1165f4c9760SMichael S. Tsirkin free_irq(vp_dev->msix_entries[i].vector, vp_dev); 1175f4c9760SMichael S. Tsirkin 1185f4c9760SMichael S. Tsirkin for (i = 0; i < vp_dev->msix_vectors; i++) 1195f4c9760SMichael S. Tsirkin if (vp_dev->msix_affinity_masks[i]) 1205f4c9760SMichael S. Tsirkin free_cpumask_var(vp_dev->msix_affinity_masks[i]); 1215f4c9760SMichael S. Tsirkin 1225f4c9760SMichael S. Tsirkin if (vp_dev->msix_enabled) { 1235f4c9760SMichael S. Tsirkin /* Disable the vector used for configuration */ 1245f4c9760SMichael S. Tsirkin vp_dev->config_vector(vp_dev, VIRTIO_MSI_NO_VECTOR); 1255f4c9760SMichael S. Tsirkin 1265f4c9760SMichael S. Tsirkin pci_disable_msix(vp_dev->pci_dev); 1275f4c9760SMichael S. Tsirkin vp_dev->msix_enabled = 0; 1285f4c9760SMichael S. Tsirkin } 1295f4c9760SMichael S. Tsirkin 1305f4c9760SMichael S. Tsirkin vp_dev->msix_vectors = 0; 1315f4c9760SMichael S. Tsirkin vp_dev->msix_used_vectors = 0; 1325f4c9760SMichael S. Tsirkin kfree(vp_dev->msix_names); 1335f4c9760SMichael S. Tsirkin vp_dev->msix_names = NULL; 1345f4c9760SMichael S. Tsirkin kfree(vp_dev->msix_entries); 1355f4c9760SMichael S. Tsirkin vp_dev->msix_entries = NULL; 1365f4c9760SMichael S. Tsirkin kfree(vp_dev->msix_affinity_masks); 1375f4c9760SMichael S. Tsirkin vp_dev->msix_affinity_masks = NULL; 1385f4c9760SMichael S. Tsirkin } 1395f4c9760SMichael S. Tsirkin 1405f4c9760SMichael S. Tsirkin static int vp_request_msix_vectors(struct virtio_device *vdev, int nvectors, 1415f4c9760SMichael S. Tsirkin bool per_vq_vectors) 1425f4c9760SMichael S. Tsirkin { 1435f4c9760SMichael S. Tsirkin struct virtio_pci_device *vp_dev = to_vp_device(vdev); 1445f4c9760SMichael S. Tsirkin const char *name = dev_name(&vp_dev->vdev.dev); 1455f4c9760SMichael S. Tsirkin unsigned i, v; 1465f4c9760SMichael S. Tsirkin int err = -ENOMEM; 1475f4c9760SMichael S. Tsirkin 1485f4c9760SMichael S. Tsirkin vp_dev->msix_vectors = nvectors; 1495f4c9760SMichael S. Tsirkin 1505f4c9760SMichael S. Tsirkin vp_dev->msix_entries = kmalloc(nvectors * sizeof *vp_dev->msix_entries, 1515f4c9760SMichael S. Tsirkin GFP_KERNEL); 1525f4c9760SMichael S. Tsirkin if (!vp_dev->msix_entries) 1535f4c9760SMichael S. Tsirkin goto error; 1545f4c9760SMichael S. Tsirkin vp_dev->msix_names = kmalloc(nvectors * sizeof *vp_dev->msix_names, 1555f4c9760SMichael S. Tsirkin GFP_KERNEL); 1565f4c9760SMichael S. Tsirkin if (!vp_dev->msix_names) 1575f4c9760SMichael S. Tsirkin goto error; 1585f4c9760SMichael S. Tsirkin vp_dev->msix_affinity_masks 1595f4c9760SMichael S. Tsirkin = kzalloc(nvectors * sizeof *vp_dev->msix_affinity_masks, 1605f4c9760SMichael S. Tsirkin GFP_KERNEL); 1615f4c9760SMichael S. Tsirkin if (!vp_dev->msix_affinity_masks) 1625f4c9760SMichael S. Tsirkin goto error; 1635f4c9760SMichael S. Tsirkin for (i = 0; i < nvectors; ++i) 1645f4c9760SMichael S. Tsirkin if (!alloc_cpumask_var(&vp_dev->msix_affinity_masks[i], 1655f4c9760SMichael S. Tsirkin GFP_KERNEL)) 1665f4c9760SMichael S. Tsirkin goto error; 1675f4c9760SMichael S. Tsirkin 1685f4c9760SMichael S. Tsirkin for (i = 0; i < nvectors; ++i) 1695f4c9760SMichael S. Tsirkin vp_dev->msix_entries[i].entry = i; 1705f4c9760SMichael S. Tsirkin 1715f4c9760SMichael S. Tsirkin err = pci_enable_msix_exact(vp_dev->pci_dev, 1725f4c9760SMichael S. Tsirkin vp_dev->msix_entries, nvectors); 1735f4c9760SMichael S. Tsirkin if (err) 1745f4c9760SMichael S. Tsirkin goto error; 1755f4c9760SMichael S. Tsirkin vp_dev->msix_enabled = 1; 1765f4c9760SMichael S. Tsirkin 1775f4c9760SMichael S. Tsirkin /* Set the vector used for configuration */ 1785f4c9760SMichael S. Tsirkin v = vp_dev->msix_used_vectors; 1795f4c9760SMichael S. Tsirkin snprintf(vp_dev->msix_names[v], sizeof *vp_dev->msix_names, 1805f4c9760SMichael S. Tsirkin "%s-config", name); 1815f4c9760SMichael S. Tsirkin err = request_irq(vp_dev->msix_entries[v].vector, 1825f4c9760SMichael S. Tsirkin vp_config_changed, 0, vp_dev->msix_names[v], 1835f4c9760SMichael S. Tsirkin vp_dev); 1845f4c9760SMichael S. Tsirkin if (err) 1855f4c9760SMichael S. Tsirkin goto error; 1865f4c9760SMichael S. Tsirkin ++vp_dev->msix_used_vectors; 1875f4c9760SMichael S. Tsirkin 1885f4c9760SMichael S. Tsirkin v = vp_dev->config_vector(vp_dev, v); 1895f4c9760SMichael S. Tsirkin /* Verify we had enough resources to assign the vector */ 1905f4c9760SMichael S. Tsirkin if (v == VIRTIO_MSI_NO_VECTOR) { 1915f4c9760SMichael S. Tsirkin err = -EBUSY; 1925f4c9760SMichael S. Tsirkin goto error; 1935f4c9760SMichael S. Tsirkin } 1945f4c9760SMichael S. Tsirkin 1955f4c9760SMichael S. Tsirkin if (!per_vq_vectors) { 1965f4c9760SMichael S. Tsirkin /* Shared vector for all VQs */ 1975f4c9760SMichael S. Tsirkin v = vp_dev->msix_used_vectors; 1985f4c9760SMichael S. Tsirkin snprintf(vp_dev->msix_names[v], sizeof *vp_dev->msix_names, 1995f4c9760SMichael S. Tsirkin "%s-virtqueues", name); 2005f4c9760SMichael S. Tsirkin err = request_irq(vp_dev->msix_entries[v].vector, 2015f4c9760SMichael S. Tsirkin vp_vring_interrupt, 0, vp_dev->msix_names[v], 2025f4c9760SMichael S. Tsirkin vp_dev); 2035f4c9760SMichael S. Tsirkin if (err) 2045f4c9760SMichael S. Tsirkin goto error; 2055f4c9760SMichael S. Tsirkin ++vp_dev->msix_used_vectors; 2065f4c9760SMichael S. Tsirkin } 2075f4c9760SMichael S. Tsirkin return 0; 2085f4c9760SMichael S. Tsirkin error: 2095f4c9760SMichael S. Tsirkin vp_free_vectors(vdev); 2105f4c9760SMichael S. Tsirkin return err; 2115f4c9760SMichael S. Tsirkin } 2125f4c9760SMichael S. Tsirkin 2135f4c9760SMichael S. Tsirkin static int vp_request_intx(struct virtio_device *vdev) 2145f4c9760SMichael S. Tsirkin { 2155f4c9760SMichael S. Tsirkin int err; 2165f4c9760SMichael S. Tsirkin struct virtio_pci_device *vp_dev = to_vp_device(vdev); 2175f4c9760SMichael S. Tsirkin 2185f4c9760SMichael S. Tsirkin err = request_irq(vp_dev->pci_dev->irq, vp_interrupt, 2195f4c9760SMichael S. Tsirkin IRQF_SHARED, dev_name(&vdev->dev), vp_dev); 2205f4c9760SMichael S. Tsirkin if (!err) 2215f4c9760SMichael S. Tsirkin vp_dev->intx_enabled = 1; 2225f4c9760SMichael S. Tsirkin return err; 2235f4c9760SMichael S. Tsirkin } 2245f4c9760SMichael S. Tsirkin 2255f4c9760SMichael S. Tsirkin static struct virtqueue *vp_setup_vq(struct virtio_device *vdev, unsigned index, 2265f4c9760SMichael S. Tsirkin void (*callback)(struct virtqueue *vq), 2275f4c9760SMichael S. Tsirkin const char *name, 2285f4c9760SMichael S. Tsirkin u16 msix_vec) 2295f4c9760SMichael S. Tsirkin { 2305f4c9760SMichael S. Tsirkin struct virtio_pci_device *vp_dev = to_vp_device(vdev); 2315f4c9760SMichael S. Tsirkin struct virtio_pci_vq_info *info = kmalloc(sizeof *info, GFP_KERNEL); 2325f4c9760SMichael S. Tsirkin struct virtqueue *vq; 2335f4c9760SMichael S. Tsirkin unsigned long flags; 2345f4c9760SMichael S. Tsirkin 2355f4c9760SMichael S. Tsirkin /* fill out our structure that represents an active queue */ 2365f4c9760SMichael S. Tsirkin if (!info) 2375f4c9760SMichael S. Tsirkin return ERR_PTR(-ENOMEM); 2385f4c9760SMichael S. Tsirkin 2395f4c9760SMichael S. Tsirkin vq = vp_dev->setup_vq(vp_dev, info, index, callback, name, msix_vec); 2405f4c9760SMichael S. Tsirkin if (IS_ERR(vq)) 2415f4c9760SMichael S. Tsirkin goto out_info; 2425f4c9760SMichael S. Tsirkin 2435f4c9760SMichael S. Tsirkin info->vq = vq; 2445f4c9760SMichael S. Tsirkin if (callback) { 2455f4c9760SMichael S. Tsirkin spin_lock_irqsave(&vp_dev->lock, flags); 2465f4c9760SMichael S. Tsirkin list_add(&info->node, &vp_dev->virtqueues); 2475f4c9760SMichael S. Tsirkin spin_unlock_irqrestore(&vp_dev->lock, flags); 2485f4c9760SMichael S. Tsirkin } else { 2495f4c9760SMichael S. Tsirkin INIT_LIST_HEAD(&info->node); 2505f4c9760SMichael S. Tsirkin } 2515f4c9760SMichael S. Tsirkin 2525f4c9760SMichael S. Tsirkin vp_dev->vqs[index] = info; 2535f4c9760SMichael S. Tsirkin return vq; 2545f4c9760SMichael S. Tsirkin 2555f4c9760SMichael S. Tsirkin out_info: 2565f4c9760SMichael S. Tsirkin kfree(info); 2575f4c9760SMichael S. Tsirkin return vq; 2585f4c9760SMichael S. Tsirkin } 2595f4c9760SMichael S. Tsirkin 2605f4c9760SMichael S. Tsirkin static void vp_del_vq(struct virtqueue *vq) 2615f4c9760SMichael S. Tsirkin { 2625f4c9760SMichael S. Tsirkin struct virtio_pci_device *vp_dev = to_vp_device(vq->vdev); 2635f4c9760SMichael S. Tsirkin struct virtio_pci_vq_info *info = vp_dev->vqs[vq->index]; 2645f4c9760SMichael S. Tsirkin unsigned long flags; 2655f4c9760SMichael S. Tsirkin 2665f4c9760SMichael S. Tsirkin spin_lock_irqsave(&vp_dev->lock, flags); 2675f4c9760SMichael S. Tsirkin list_del(&info->node); 2685f4c9760SMichael S. Tsirkin spin_unlock_irqrestore(&vp_dev->lock, flags); 2695f4c9760SMichael S. Tsirkin 2705f4c9760SMichael S. Tsirkin vp_dev->del_vq(info); 2715f4c9760SMichael S. Tsirkin kfree(info); 2725f4c9760SMichael S. Tsirkin } 2735f4c9760SMichael S. Tsirkin 2745f4c9760SMichael S. Tsirkin /* the config->del_vqs() implementation */ 2755f4c9760SMichael S. Tsirkin void vp_del_vqs(struct virtio_device *vdev) 2765f4c9760SMichael S. Tsirkin { 2775f4c9760SMichael S. Tsirkin struct virtio_pci_device *vp_dev = to_vp_device(vdev); 2785f4c9760SMichael S. Tsirkin struct virtqueue *vq, *n; 2795f4c9760SMichael S. Tsirkin struct virtio_pci_vq_info *info; 2805f4c9760SMichael S. Tsirkin 2815f4c9760SMichael S. Tsirkin list_for_each_entry_safe(vq, n, &vdev->vqs, list) { 2825f4c9760SMichael S. Tsirkin info = vp_dev->vqs[vq->index]; 2835f4c9760SMichael S. Tsirkin if (vp_dev->per_vq_vectors && 2845f4c9760SMichael S. Tsirkin info->msix_vector != VIRTIO_MSI_NO_VECTOR) 2855f4c9760SMichael S. Tsirkin free_irq(vp_dev->msix_entries[info->msix_vector].vector, 2865f4c9760SMichael S. Tsirkin vq); 2875f4c9760SMichael S. Tsirkin vp_del_vq(vq); 2885f4c9760SMichael S. Tsirkin } 2895f4c9760SMichael S. Tsirkin vp_dev->per_vq_vectors = false; 2905f4c9760SMichael S. Tsirkin 2915f4c9760SMichael S. Tsirkin vp_free_vectors(vdev); 2925f4c9760SMichael S. Tsirkin kfree(vp_dev->vqs); 29380e9541fSMichael S. Tsirkin vp_dev->vqs = NULL; 2945f4c9760SMichael S. Tsirkin } 2955f4c9760SMichael S. Tsirkin 2965f4c9760SMichael S. Tsirkin static int vp_try_to_find_vqs(struct virtio_device *vdev, unsigned nvqs, 2975f4c9760SMichael S. Tsirkin struct virtqueue *vqs[], 2985f4c9760SMichael S. Tsirkin vq_callback_t *callbacks[], 299f7ad26ffSStefan Hajnoczi const char * const names[], 3005f4c9760SMichael S. Tsirkin bool use_msix, 3015f4c9760SMichael S. Tsirkin bool per_vq_vectors) 3025f4c9760SMichael S. Tsirkin { 3035f4c9760SMichael S. Tsirkin struct virtio_pci_device *vp_dev = to_vp_device(vdev); 3045f4c9760SMichael S. Tsirkin u16 msix_vec; 3055f4c9760SMichael S. Tsirkin int i, err, nvectors, allocated_vectors; 3065f4c9760SMichael S. Tsirkin 3075f4c9760SMichael S. Tsirkin vp_dev->vqs = kmalloc(nvqs * sizeof *vp_dev->vqs, GFP_KERNEL); 3085f4c9760SMichael S. Tsirkin if (!vp_dev->vqs) 3095f4c9760SMichael S. Tsirkin return -ENOMEM; 3105f4c9760SMichael S. Tsirkin 3115f4c9760SMichael S. Tsirkin if (!use_msix) { 3125f4c9760SMichael S. Tsirkin /* Old style: one normal interrupt for change and all vqs. */ 3135f4c9760SMichael S. Tsirkin err = vp_request_intx(vdev); 3145f4c9760SMichael S. Tsirkin if (err) 3155f4c9760SMichael S. Tsirkin goto error_find; 3165f4c9760SMichael S. Tsirkin } else { 3175f4c9760SMichael S. Tsirkin if (per_vq_vectors) { 3185f4c9760SMichael S. Tsirkin /* Best option: one for change interrupt, one per vq. */ 3195f4c9760SMichael S. Tsirkin nvectors = 1; 3205f4c9760SMichael S. Tsirkin for (i = 0; i < nvqs; ++i) 3215f4c9760SMichael S. Tsirkin if (callbacks[i]) 3225f4c9760SMichael S. Tsirkin ++nvectors; 3235f4c9760SMichael S. Tsirkin } else { 3245f4c9760SMichael S. Tsirkin /* Second best: one for change, shared for all vqs. */ 3255f4c9760SMichael S. Tsirkin nvectors = 2; 3265f4c9760SMichael S. Tsirkin } 3275f4c9760SMichael S. Tsirkin 3285f4c9760SMichael S. Tsirkin err = vp_request_msix_vectors(vdev, nvectors, per_vq_vectors); 3295f4c9760SMichael S. Tsirkin if (err) 3305f4c9760SMichael S. Tsirkin goto error_find; 3315f4c9760SMichael S. Tsirkin } 3325f4c9760SMichael S. Tsirkin 3335f4c9760SMichael S. Tsirkin vp_dev->per_vq_vectors = per_vq_vectors; 3345f4c9760SMichael S. Tsirkin allocated_vectors = vp_dev->msix_used_vectors; 3355f4c9760SMichael S. Tsirkin for (i = 0; i < nvqs; ++i) { 3365f4c9760SMichael S. Tsirkin if (!names[i]) { 3375f4c9760SMichael S. Tsirkin vqs[i] = NULL; 3385f4c9760SMichael S. Tsirkin continue; 3395f4c9760SMichael S. Tsirkin } else if (!callbacks[i] || !vp_dev->msix_enabled) 3405f4c9760SMichael S. Tsirkin msix_vec = VIRTIO_MSI_NO_VECTOR; 3415f4c9760SMichael S. Tsirkin else if (vp_dev->per_vq_vectors) 3425f4c9760SMichael S. Tsirkin msix_vec = allocated_vectors++; 3435f4c9760SMichael S. Tsirkin else 3445f4c9760SMichael S. Tsirkin msix_vec = VP_MSIX_VQ_VECTOR; 3455f4c9760SMichael S. Tsirkin vqs[i] = vp_setup_vq(vdev, i, callbacks[i], names[i], msix_vec); 3465f4c9760SMichael S. Tsirkin if (IS_ERR(vqs[i])) { 3475f4c9760SMichael S. Tsirkin err = PTR_ERR(vqs[i]); 3485f4c9760SMichael S. Tsirkin goto error_find; 3495f4c9760SMichael S. Tsirkin } 3505f4c9760SMichael S. Tsirkin 3515f4c9760SMichael S. Tsirkin if (!vp_dev->per_vq_vectors || msix_vec == VIRTIO_MSI_NO_VECTOR) 3525f4c9760SMichael S. Tsirkin continue; 3535f4c9760SMichael S. Tsirkin 3545f4c9760SMichael S. Tsirkin /* allocate per-vq irq if available and necessary */ 3555f4c9760SMichael S. Tsirkin snprintf(vp_dev->msix_names[msix_vec], 3565f4c9760SMichael S. Tsirkin sizeof *vp_dev->msix_names, 3575f4c9760SMichael S. Tsirkin "%s-%s", 3585f4c9760SMichael S. Tsirkin dev_name(&vp_dev->vdev.dev), names[i]); 3595f4c9760SMichael S. Tsirkin err = request_irq(vp_dev->msix_entries[msix_vec].vector, 3605f4c9760SMichael S. Tsirkin vring_interrupt, 0, 3615f4c9760SMichael S. Tsirkin vp_dev->msix_names[msix_vec], 3625f4c9760SMichael S. Tsirkin vqs[i]); 3635f4c9760SMichael S. Tsirkin if (err) { 3645f4c9760SMichael S. Tsirkin vp_del_vq(vqs[i]); 3655f4c9760SMichael S. Tsirkin goto error_find; 3665f4c9760SMichael S. Tsirkin } 3675f4c9760SMichael S. Tsirkin } 3685f4c9760SMichael S. Tsirkin return 0; 3695f4c9760SMichael S. Tsirkin 3705f4c9760SMichael S. Tsirkin error_find: 3715f4c9760SMichael S. Tsirkin vp_del_vqs(vdev); 3725f4c9760SMichael S. Tsirkin return err; 3735f4c9760SMichael S. Tsirkin } 3745f4c9760SMichael S. Tsirkin 3755f4c9760SMichael S. Tsirkin /* the config->find_vqs() implementation */ 3765f4c9760SMichael S. Tsirkin int vp_find_vqs(struct virtio_device *vdev, unsigned nvqs, 3775f4c9760SMichael S. Tsirkin struct virtqueue *vqs[], 3785f4c9760SMichael S. Tsirkin vq_callback_t *callbacks[], 379f7ad26ffSStefan Hajnoczi const char * const names[]) 3805f4c9760SMichael S. Tsirkin { 3815f4c9760SMichael S. Tsirkin int err; 3825f4c9760SMichael S. Tsirkin 3835f4c9760SMichael S. Tsirkin /* Try MSI-X with one vector per queue. */ 3845f4c9760SMichael S. Tsirkin err = vp_try_to_find_vqs(vdev, nvqs, vqs, callbacks, names, true, true); 3855f4c9760SMichael S. Tsirkin if (!err) 3865f4c9760SMichael S. Tsirkin return 0; 3875f4c9760SMichael S. Tsirkin /* Fallback: MSI-X with one vector for config, one shared for queues. */ 3885f4c9760SMichael S. Tsirkin err = vp_try_to_find_vqs(vdev, nvqs, vqs, callbacks, names, 3895f4c9760SMichael S. Tsirkin true, false); 3905f4c9760SMichael S. Tsirkin if (!err) 3915f4c9760SMichael S. Tsirkin return 0; 3925f4c9760SMichael S. Tsirkin /* Finally fall back to regular interrupts. */ 3935f4c9760SMichael S. Tsirkin return vp_try_to_find_vqs(vdev, nvqs, vqs, callbacks, names, 3945f4c9760SMichael S. Tsirkin false, false); 3955f4c9760SMichael S. Tsirkin } 3965f4c9760SMichael S. Tsirkin 3975f4c9760SMichael S. Tsirkin const char *vp_bus_name(struct virtio_device *vdev) 3985f4c9760SMichael S. Tsirkin { 3995f4c9760SMichael S. Tsirkin struct virtio_pci_device *vp_dev = to_vp_device(vdev); 4005f4c9760SMichael S. Tsirkin 4015f4c9760SMichael S. Tsirkin return pci_name(vp_dev->pci_dev); 4025f4c9760SMichael S. Tsirkin } 4035f4c9760SMichael S. Tsirkin 4045f4c9760SMichael S. Tsirkin /* Setup the affinity for a virtqueue: 4055f4c9760SMichael S. Tsirkin * - force the affinity for per vq vector 4065f4c9760SMichael S. Tsirkin * - OR over all affinities for shared MSI 4075f4c9760SMichael S. Tsirkin * - ignore the affinity request if we're using INTX 4085f4c9760SMichael S. Tsirkin */ 4095f4c9760SMichael S. Tsirkin int vp_set_vq_affinity(struct virtqueue *vq, int cpu) 4105f4c9760SMichael S. Tsirkin { 4115f4c9760SMichael S. Tsirkin struct virtio_device *vdev = vq->vdev; 4125f4c9760SMichael S. Tsirkin struct virtio_pci_device *vp_dev = to_vp_device(vdev); 4135f4c9760SMichael S. Tsirkin struct virtio_pci_vq_info *info = vp_dev->vqs[vq->index]; 4145f4c9760SMichael S. Tsirkin struct cpumask *mask; 4155f4c9760SMichael S. Tsirkin unsigned int irq; 4165f4c9760SMichael S. Tsirkin 4175f4c9760SMichael S. Tsirkin if (!vq->callback) 4185f4c9760SMichael S. Tsirkin return -EINVAL; 4195f4c9760SMichael S. Tsirkin 4205f4c9760SMichael S. Tsirkin if (vp_dev->msix_enabled) { 4215f4c9760SMichael S. Tsirkin mask = vp_dev->msix_affinity_masks[info->msix_vector]; 4225f4c9760SMichael S. Tsirkin irq = vp_dev->msix_entries[info->msix_vector].vector; 4235f4c9760SMichael S. Tsirkin if (cpu == -1) 4245f4c9760SMichael S. Tsirkin irq_set_affinity_hint(irq, NULL); 4255f4c9760SMichael S. Tsirkin else { 426210d150eSJiang Liu cpumask_clear(mask); 4275f4c9760SMichael S. Tsirkin cpumask_set_cpu(cpu, mask); 4285f4c9760SMichael S. Tsirkin irq_set_affinity_hint(irq, mask); 4295f4c9760SMichael S. Tsirkin } 4305f4c9760SMichael S. Tsirkin } 4315f4c9760SMichael S. Tsirkin return 0; 4325f4c9760SMichael S. Tsirkin } 4335f4c9760SMichael S. Tsirkin 4345f4c9760SMichael S. Tsirkin #ifdef CONFIG_PM_SLEEP 4355f4c9760SMichael S. Tsirkin static int virtio_pci_freeze(struct device *dev) 4365f4c9760SMichael S. Tsirkin { 4375f4c9760SMichael S. Tsirkin struct pci_dev *pci_dev = to_pci_dev(dev); 4385f4c9760SMichael S. Tsirkin struct virtio_pci_device *vp_dev = pci_get_drvdata(pci_dev); 4395f4c9760SMichael S. Tsirkin int ret; 4405f4c9760SMichael S. Tsirkin 4415f4c9760SMichael S. Tsirkin ret = virtio_device_freeze(&vp_dev->vdev); 4425f4c9760SMichael S. Tsirkin 4435f4c9760SMichael S. Tsirkin if (!ret) 4445f4c9760SMichael S. Tsirkin pci_disable_device(pci_dev); 4455f4c9760SMichael S. Tsirkin return ret; 4465f4c9760SMichael S. Tsirkin } 4475f4c9760SMichael S. Tsirkin 4485f4c9760SMichael S. Tsirkin static int virtio_pci_restore(struct device *dev) 4495f4c9760SMichael S. Tsirkin { 4505f4c9760SMichael S. Tsirkin struct pci_dev *pci_dev = to_pci_dev(dev); 4515f4c9760SMichael S. Tsirkin struct virtio_pci_device *vp_dev = pci_get_drvdata(pci_dev); 4525f4c9760SMichael S. Tsirkin int ret; 4535f4c9760SMichael S. Tsirkin 4545f4c9760SMichael S. Tsirkin ret = pci_enable_device(pci_dev); 4555f4c9760SMichael S. Tsirkin if (ret) 4565f4c9760SMichael S. Tsirkin return ret; 4575f4c9760SMichael S. Tsirkin 4585f4c9760SMichael S. Tsirkin pci_set_master(pci_dev); 4595f4c9760SMichael S. Tsirkin return virtio_device_restore(&vp_dev->vdev); 4605f4c9760SMichael S. Tsirkin } 4615f4c9760SMichael S. Tsirkin 4629a4253dbSMichael S. Tsirkin static const struct dev_pm_ops virtio_pci_pm_ops = { 4635f4c9760SMichael S. Tsirkin SET_SYSTEM_SLEEP_PM_OPS(virtio_pci_freeze, virtio_pci_restore) 4645f4c9760SMichael S. Tsirkin }; 4655f4c9760SMichael S. Tsirkin #endif 4669a4253dbSMichael S. Tsirkin 4679a4253dbSMichael S. Tsirkin 4689a4253dbSMichael S. Tsirkin /* Qumranet donated their vendor ID for devices 0x1000 thru 0x10FF. */ 4699a4253dbSMichael S. Tsirkin static const struct pci_device_id virtio_pci_id_table[] = { 4709a4253dbSMichael S. Tsirkin { PCI_DEVICE(0x1af4, PCI_ANY_ID) }, 4719a4253dbSMichael S. Tsirkin { 0 } 4729a4253dbSMichael S. Tsirkin }; 4739a4253dbSMichael S. Tsirkin 4749a4253dbSMichael S. Tsirkin MODULE_DEVICE_TABLE(pci, virtio_pci_id_table); 4759a4253dbSMichael S. Tsirkin 476ff31d2e2SMichael S. Tsirkin static void virtio_pci_release_dev(struct device *_d) 477ff31d2e2SMichael S. Tsirkin { 478ff31d2e2SMichael S. Tsirkin struct virtio_device *vdev = dev_to_virtio(_d); 479ff31d2e2SMichael S. Tsirkin struct virtio_pci_device *vp_dev = to_vp_device(vdev); 480ff31d2e2SMichael S. Tsirkin 481ff31d2e2SMichael S. Tsirkin /* As struct device is a kobject, it's not safe to 482ff31d2e2SMichael S. Tsirkin * free the memory (including the reference counter itself) 483ff31d2e2SMichael S. Tsirkin * until it's release callback. */ 484ff31d2e2SMichael S. Tsirkin kfree(vp_dev); 485ff31d2e2SMichael S. Tsirkin } 486ff31d2e2SMichael S. Tsirkin 4879a4253dbSMichael S. Tsirkin static int virtio_pci_probe(struct pci_dev *pci_dev, 4889a4253dbSMichael S. Tsirkin const struct pci_device_id *id) 4899a4253dbSMichael S. Tsirkin { 490ff31d2e2SMichael S. Tsirkin struct virtio_pci_device *vp_dev; 491ff31d2e2SMichael S. Tsirkin int rc; 492ff31d2e2SMichael S. Tsirkin 493ff31d2e2SMichael S. Tsirkin /* allocate our structure and fill it out */ 494ff31d2e2SMichael S. Tsirkin vp_dev = kzalloc(sizeof(struct virtio_pci_device), GFP_KERNEL); 495ff31d2e2SMichael S. Tsirkin if (!vp_dev) 496ff31d2e2SMichael S. Tsirkin return -ENOMEM; 497ff31d2e2SMichael S. Tsirkin 498ff31d2e2SMichael S. Tsirkin pci_set_drvdata(pci_dev, vp_dev); 499ff31d2e2SMichael S. Tsirkin vp_dev->vdev.dev.parent = &pci_dev->dev; 500ff31d2e2SMichael S. Tsirkin vp_dev->vdev.dev.release = virtio_pci_release_dev; 501ff31d2e2SMichael S. Tsirkin vp_dev->pci_dev = pci_dev; 502ff31d2e2SMichael S. Tsirkin INIT_LIST_HEAD(&vp_dev->virtqueues); 503ff31d2e2SMichael S. Tsirkin spin_lock_init(&vp_dev->lock); 504ff31d2e2SMichael S. Tsirkin 505ff31d2e2SMichael S. Tsirkin /* enable the device */ 506ff31d2e2SMichael S. Tsirkin rc = pci_enable_device(pci_dev); 507ff31d2e2SMichael S. Tsirkin if (rc) 508ff31d2e2SMichael S. Tsirkin goto err_enable_device; 509ff31d2e2SMichael S. Tsirkin 510ac399d8fSMichael S. Tsirkin if (force_legacy) { 511ac399d8fSMichael S. Tsirkin rc = virtio_pci_legacy_probe(vp_dev); 512ac399d8fSMichael S. Tsirkin /* Also try modern mode if we can't map BAR0 (no IO space). */ 513ac399d8fSMichael S. Tsirkin if (rc == -ENODEV || rc == -ENOMEM) 514ac399d8fSMichael S. Tsirkin rc = virtio_pci_modern_probe(vp_dev); 515ac399d8fSMichael S. Tsirkin if (rc) 516ac399d8fSMichael S. Tsirkin goto err_probe; 517ac399d8fSMichael S. Tsirkin } else { 5181fcf0512SMichael S. Tsirkin rc = virtio_pci_modern_probe(vp_dev); 5191fcf0512SMichael S. Tsirkin if (rc == -ENODEV) 520ff31d2e2SMichael S. Tsirkin rc = virtio_pci_legacy_probe(vp_dev); 521ff31d2e2SMichael S. Tsirkin if (rc) 522ff31d2e2SMichael S. Tsirkin goto err_probe; 523ac399d8fSMichael S. Tsirkin } 524ff31d2e2SMichael S. Tsirkin 525ff31d2e2SMichael S. Tsirkin pci_set_master(pci_dev); 526ff31d2e2SMichael S. Tsirkin 527ff31d2e2SMichael S. Tsirkin rc = register_virtio_device(&vp_dev->vdev); 528ff31d2e2SMichael S. Tsirkin if (rc) 529ff31d2e2SMichael S. Tsirkin goto err_register; 530ff31d2e2SMichael S. Tsirkin 531ff31d2e2SMichael S. Tsirkin return 0; 532ff31d2e2SMichael S. Tsirkin 533ff31d2e2SMichael S. Tsirkin err_register: 5341fcf0512SMichael S. Tsirkin if (vp_dev->ioaddr) 535ff31d2e2SMichael S. Tsirkin virtio_pci_legacy_remove(vp_dev); 5361fcf0512SMichael S. Tsirkin else 5371fcf0512SMichael S. Tsirkin virtio_pci_modern_remove(vp_dev); 538ff31d2e2SMichael S. Tsirkin err_probe: 539ff31d2e2SMichael S. Tsirkin pci_disable_device(pci_dev); 540ff31d2e2SMichael S. Tsirkin err_enable_device: 541ff31d2e2SMichael S. Tsirkin kfree(vp_dev); 542ff31d2e2SMichael S. Tsirkin return rc; 5439a4253dbSMichael S. Tsirkin } 5449a4253dbSMichael S. Tsirkin 5459a4253dbSMichael S. Tsirkin static void virtio_pci_remove(struct pci_dev *pci_dev) 5469a4253dbSMichael S. Tsirkin { 547ff31d2e2SMichael S. Tsirkin struct virtio_pci_device *vp_dev = pci_get_drvdata(pci_dev); 548*2989be09SMichael S. Tsirkin struct device *dev = get_device(&vp_dev->vdev.dev); 549ff31d2e2SMichael S. Tsirkin 550ff31d2e2SMichael S. Tsirkin unregister_virtio_device(&vp_dev->vdev); 551ff31d2e2SMichael S. Tsirkin 5521fcf0512SMichael S. Tsirkin if (vp_dev->ioaddr) 5531fcf0512SMichael S. Tsirkin virtio_pci_legacy_remove(vp_dev); 5541fcf0512SMichael S. Tsirkin else 5551fcf0512SMichael S. Tsirkin virtio_pci_modern_remove(vp_dev); 556ff31d2e2SMichael S. Tsirkin 557ff31d2e2SMichael S. Tsirkin pci_disable_device(pci_dev); 558*2989be09SMichael S. Tsirkin put_device(dev); 5599a4253dbSMichael S. Tsirkin } 5609a4253dbSMichael S. Tsirkin 5619a4253dbSMichael S. Tsirkin static struct pci_driver virtio_pci_driver = { 5629a4253dbSMichael S. Tsirkin .name = "virtio-pci", 5639a4253dbSMichael S. Tsirkin .id_table = virtio_pci_id_table, 5649a4253dbSMichael S. Tsirkin .probe = virtio_pci_probe, 5659a4253dbSMichael S. Tsirkin .remove = virtio_pci_remove, 5669a4253dbSMichael S. Tsirkin #ifdef CONFIG_PM_SLEEP 5679a4253dbSMichael S. Tsirkin .driver.pm = &virtio_pci_pm_ops, 5689a4253dbSMichael S. Tsirkin #endif 5699a4253dbSMichael S. Tsirkin }; 5709a4253dbSMichael S. Tsirkin 5719a4253dbSMichael S. Tsirkin module_pci_driver(virtio_pci_driver); 5725ff16110SHerbert Xu 5735ff16110SHerbert Xu MODULE_AUTHOR("Anthony Liguori <aliguori@us.ibm.com>"); 5745ff16110SHerbert Xu MODULE_DESCRIPTION("virtio-pci"); 5755ff16110SHerbert Xu MODULE_LICENSE("GPL"); 5765ff16110SHerbert Xu MODULE_VERSION("1"); 577