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 36*0b0f9dc5SMichael S. Tsirkin if (vp_dev->intx_enabled) 37*0b0f9dc5SMichael S. Tsirkin synchronize_irq(vp_dev->pci_dev->irq); 38*0b0f9dc5SMichael S. Tsirkin 39*0b0f9dc5SMichael S. Tsirkin for (i = 0; i < vp_dev->msix_vectors; ++i) 40fa3a3279SChristoph Hellwig synchronize_irq(pci_irq_vector(vp_dev->pci_dev, i)); 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 irqreturn_t ret = IRQ_NONE; 665c34d002SChristoph Hellwig struct virtqueue *vq; 675f4c9760SMichael S. Tsirkin 685c34d002SChristoph Hellwig list_for_each_entry(vq, &vp_dev->vdev.vqs, list) { 695c34d002SChristoph Hellwig if (vq->callback && vring_interrupt(irq, vq) == IRQ_HANDLED) 705f4c9760SMichael S. Tsirkin ret = IRQ_HANDLED; 715f4c9760SMichael S. Tsirkin } 725f4c9760SMichael S. Tsirkin 735f4c9760SMichael S. Tsirkin return ret; 745f4c9760SMichael S. Tsirkin } 755f4c9760SMichael S. Tsirkin 765f4c9760SMichael S. Tsirkin /* A small wrapper to also acknowledge the interrupt when it's handled. 775f4c9760SMichael S. Tsirkin * I really need an EIO hook for the vring so I can ack the interrupt once we 785f4c9760SMichael S. Tsirkin * know that we'll be handling the IRQ but before we invoke the callback since 795f4c9760SMichael S. Tsirkin * the callback may notify the host which results in the host attempting to 805f4c9760SMichael S. Tsirkin * raise an interrupt that we would then mask once we acknowledged the 815f4c9760SMichael S. Tsirkin * interrupt. */ 825f4c9760SMichael S. Tsirkin static irqreturn_t vp_interrupt(int irq, void *opaque) 835f4c9760SMichael S. Tsirkin { 845f4c9760SMichael S. Tsirkin struct virtio_pci_device *vp_dev = opaque; 855f4c9760SMichael S. Tsirkin u8 isr; 865f4c9760SMichael S. Tsirkin 875f4c9760SMichael S. Tsirkin /* reading the ISR has the effect of also clearing it so it's very 885f4c9760SMichael S. Tsirkin * important to save off the value. */ 895f4c9760SMichael S. Tsirkin isr = ioread8(vp_dev->isr); 905f4c9760SMichael S. Tsirkin 915f4c9760SMichael S. Tsirkin /* It's definitely not us if the ISR was not high */ 925f4c9760SMichael S. Tsirkin if (!isr) 935f4c9760SMichael S. Tsirkin return IRQ_NONE; 945f4c9760SMichael S. Tsirkin 955f4c9760SMichael S. Tsirkin /* Configuration change? Tell driver if it wants to know. */ 965f4c9760SMichael S. Tsirkin if (isr & VIRTIO_PCI_ISR_CONFIG) 975f4c9760SMichael S. Tsirkin vp_config_changed(irq, opaque); 985f4c9760SMichael S. Tsirkin 995f4c9760SMichael S. Tsirkin return vp_vring_interrupt(irq, opaque); 1005f4c9760SMichael S. Tsirkin } 1015f4c9760SMichael S. Tsirkin 102*0b0f9dc5SMichael S. Tsirkin static int vp_request_msix_vectors(struct virtio_device *vdev, int nvectors, 103*0b0f9dc5SMichael S. Tsirkin bool per_vq_vectors, struct irq_affinity *desc) 104*0b0f9dc5SMichael S. Tsirkin { 105*0b0f9dc5SMichael S. Tsirkin struct virtio_pci_device *vp_dev = to_vp_device(vdev); 106*0b0f9dc5SMichael S. Tsirkin const char *name = dev_name(&vp_dev->vdev.dev); 107*0b0f9dc5SMichael S. Tsirkin unsigned i, v; 108*0b0f9dc5SMichael S. Tsirkin int err = -ENOMEM; 109*0b0f9dc5SMichael S. Tsirkin 110*0b0f9dc5SMichael S. Tsirkin vp_dev->msix_vectors = nvectors; 111*0b0f9dc5SMichael S. Tsirkin 112*0b0f9dc5SMichael S. Tsirkin vp_dev->msix_names = kmalloc(nvectors * sizeof *vp_dev->msix_names, 113*0b0f9dc5SMichael S. Tsirkin GFP_KERNEL); 114*0b0f9dc5SMichael S. Tsirkin if (!vp_dev->msix_names) 115*0b0f9dc5SMichael S. Tsirkin goto error; 116*0b0f9dc5SMichael S. Tsirkin vp_dev->msix_affinity_masks 117*0b0f9dc5SMichael S. Tsirkin = kzalloc(nvectors * sizeof *vp_dev->msix_affinity_masks, 118*0b0f9dc5SMichael S. Tsirkin GFP_KERNEL); 119*0b0f9dc5SMichael S. Tsirkin if (!vp_dev->msix_affinity_masks) 120*0b0f9dc5SMichael S. Tsirkin goto error; 121*0b0f9dc5SMichael S. Tsirkin for (i = 0; i < nvectors; ++i) 122*0b0f9dc5SMichael S. Tsirkin if (!alloc_cpumask_var(&vp_dev->msix_affinity_masks[i], 123*0b0f9dc5SMichael S. Tsirkin GFP_KERNEL)) 124*0b0f9dc5SMichael S. Tsirkin goto error; 125*0b0f9dc5SMichael S. Tsirkin 126*0b0f9dc5SMichael S. Tsirkin err = pci_alloc_irq_vectors_affinity(vp_dev->pci_dev, nvectors, 127*0b0f9dc5SMichael S. Tsirkin nvectors, PCI_IRQ_MSIX | 128*0b0f9dc5SMichael S. Tsirkin (desc ? PCI_IRQ_AFFINITY : 0), 129*0b0f9dc5SMichael S. Tsirkin desc); 130*0b0f9dc5SMichael S. Tsirkin if (err < 0) 131*0b0f9dc5SMichael S. Tsirkin goto error; 132*0b0f9dc5SMichael S. Tsirkin vp_dev->msix_enabled = 1; 133*0b0f9dc5SMichael S. Tsirkin 134*0b0f9dc5SMichael S. Tsirkin /* Set the vector used for configuration */ 135*0b0f9dc5SMichael S. Tsirkin v = vp_dev->msix_used_vectors; 136*0b0f9dc5SMichael S. Tsirkin snprintf(vp_dev->msix_names[v], sizeof *vp_dev->msix_names, 137*0b0f9dc5SMichael S. Tsirkin "%s-config", name); 138*0b0f9dc5SMichael S. Tsirkin err = request_irq(pci_irq_vector(vp_dev->pci_dev, v), 139*0b0f9dc5SMichael S. Tsirkin vp_config_changed, 0, vp_dev->msix_names[v], 140*0b0f9dc5SMichael S. Tsirkin vp_dev); 141*0b0f9dc5SMichael S. Tsirkin if (err) 142*0b0f9dc5SMichael S. Tsirkin goto error; 143*0b0f9dc5SMichael S. Tsirkin ++vp_dev->msix_used_vectors; 144*0b0f9dc5SMichael S. Tsirkin 145*0b0f9dc5SMichael S. Tsirkin v = vp_dev->config_vector(vp_dev, v); 146*0b0f9dc5SMichael S. Tsirkin /* Verify we had enough resources to assign the vector */ 147*0b0f9dc5SMichael S. Tsirkin if (v == VIRTIO_MSI_NO_VECTOR) { 148*0b0f9dc5SMichael S. Tsirkin err = -EBUSY; 149*0b0f9dc5SMichael S. Tsirkin goto error; 150*0b0f9dc5SMichael S. Tsirkin } 151*0b0f9dc5SMichael S. Tsirkin 152*0b0f9dc5SMichael S. Tsirkin if (!per_vq_vectors) { 153*0b0f9dc5SMichael S. Tsirkin /* Shared vector for all VQs */ 154*0b0f9dc5SMichael S. Tsirkin v = vp_dev->msix_used_vectors; 155*0b0f9dc5SMichael S. Tsirkin snprintf(vp_dev->msix_names[v], sizeof *vp_dev->msix_names, 156*0b0f9dc5SMichael S. Tsirkin "%s-virtqueues", name); 157*0b0f9dc5SMichael S. Tsirkin err = request_irq(pci_irq_vector(vp_dev->pci_dev, v), 158*0b0f9dc5SMichael S. Tsirkin vp_vring_interrupt, 0, vp_dev->msix_names[v], 159*0b0f9dc5SMichael S. Tsirkin vp_dev); 160*0b0f9dc5SMichael S. Tsirkin if (err) 161*0b0f9dc5SMichael S. Tsirkin goto error; 162*0b0f9dc5SMichael S. Tsirkin ++vp_dev->msix_used_vectors; 163*0b0f9dc5SMichael S. Tsirkin } 164*0b0f9dc5SMichael S. Tsirkin return 0; 165*0b0f9dc5SMichael S. Tsirkin error: 166*0b0f9dc5SMichael S. Tsirkin return err; 167*0b0f9dc5SMichael S. Tsirkin } 168*0b0f9dc5SMichael S. Tsirkin 169*0b0f9dc5SMichael S. Tsirkin /* the config->del_vqs() implementation */ 170*0b0f9dc5SMichael S. Tsirkin void vp_del_vqs(struct virtio_device *vdev) 1715f4c9760SMichael S. Tsirkin { 1725f4c9760SMichael S. Tsirkin struct virtio_pci_device *vp_dev = to_vp_device(vdev); 1735f4c9760SMichael S. Tsirkin struct virtqueue *vq, *n; 174*0b0f9dc5SMichael S. Tsirkin int i; 1755f4c9760SMichael S. Tsirkin 1765f4c9760SMichael S. Tsirkin list_for_each_entry_safe(vq, n, &vdev->vqs, list) { 1775c34d002SChristoph Hellwig if (vp_dev->msix_vector_map) { 1785c34d002SChristoph Hellwig int v = vp_dev->msix_vector_map[vq->index]; 179fa3a3279SChristoph Hellwig 180fa3a3279SChristoph Hellwig if (v != VIRTIO_MSI_NO_VECTOR) 181fa3a3279SChristoph Hellwig free_irq(pci_irq_vector(vp_dev->pci_dev, v), 1825f4c9760SMichael S. Tsirkin vq); 183fa3a3279SChristoph Hellwig } 1845c34d002SChristoph Hellwig vp_dev->del_vq(vq); 1855f4c9760SMichael S. Tsirkin } 186*0b0f9dc5SMichael S. Tsirkin 187*0b0f9dc5SMichael S. Tsirkin if (vp_dev->intx_enabled) { 188*0b0f9dc5SMichael S. Tsirkin free_irq(vp_dev->pci_dev->irq, vp_dev); 189*0b0f9dc5SMichael S. Tsirkin vp_dev->intx_enabled = 0; 19066f2f555SChristoph Hellwig } 19166f2f555SChristoph Hellwig 192*0b0f9dc5SMichael S. Tsirkin for (i = 0; i < vp_dev->msix_used_vectors; ++i) 193*0b0f9dc5SMichael S. Tsirkin free_irq(pci_irq_vector(vp_dev->pci_dev, i), vp_dev); 19466f2f555SChristoph Hellwig 19507ec5148SChristoph Hellwig for (i = 0; i < vp_dev->msix_vectors; i++) 196*0b0f9dc5SMichael S. Tsirkin if (vp_dev->msix_affinity_masks[i]) 19707ec5148SChristoph Hellwig free_cpumask_var(vp_dev->msix_affinity_masks[i]); 19807ec5148SChristoph Hellwig 199*0b0f9dc5SMichael S. Tsirkin if (vp_dev->msix_enabled) { 20066f2f555SChristoph Hellwig /* Disable the vector used for configuration */ 20166f2f555SChristoph Hellwig vp_dev->config_vector(vp_dev, VIRTIO_MSI_NO_VECTOR); 20266f2f555SChristoph Hellwig 203*0b0f9dc5SMichael S. Tsirkin pci_free_irq_vectors(vp_dev->pci_dev); 204*0b0f9dc5SMichael S. Tsirkin vp_dev->msix_enabled = 0; 20566f2f555SChristoph Hellwig } 20666f2f555SChristoph Hellwig 207*0b0f9dc5SMichael S. Tsirkin vp_dev->msix_vectors = 0; 208*0b0f9dc5SMichael S. Tsirkin vp_dev->msix_used_vectors = 0; 209*0b0f9dc5SMichael S. Tsirkin kfree(vp_dev->msix_names); 210*0b0f9dc5SMichael S. Tsirkin vp_dev->msix_names = NULL; 211*0b0f9dc5SMichael S. Tsirkin kfree(vp_dev->msix_affinity_masks); 212*0b0f9dc5SMichael S. Tsirkin vp_dev->msix_affinity_masks = NULL; 213*0b0f9dc5SMichael S. Tsirkin kfree(vp_dev->msix_vector_map); 214*0b0f9dc5SMichael S. Tsirkin vp_dev->msix_vector_map = NULL; 2155f4c9760SMichael S. Tsirkin } 2165f4c9760SMichael S. Tsirkin 217a3cbec69SChristoph Hellwig static int vp_find_vqs_msix(struct virtio_device *vdev, unsigned nvqs, 21852a61516SChristoph Hellwig struct virtqueue *vqs[], vq_callback_t *callbacks[], 219bf951b10SMichael S. Tsirkin const char * const names[], bool per_vq_vectors, 220bf951b10SMichael S. Tsirkin struct irq_affinity *desc) 2215f4c9760SMichael S. Tsirkin { 2225f4c9760SMichael S. Tsirkin struct virtio_pci_device *vp_dev = to_vp_device(vdev); 2235f4c9760SMichael S. Tsirkin u16 msix_vec; 224*0b0f9dc5SMichael S. Tsirkin int i, err, nvectors, allocated_vectors; 2255f4c9760SMichael S. Tsirkin 226bf951b10SMichael S. Tsirkin if (per_vq_vectors) { 227*0b0f9dc5SMichael S. Tsirkin /* Best option: one for change interrupt, one per vq. */ 228*0b0f9dc5SMichael S. Tsirkin nvectors = 1; 229*0b0f9dc5SMichael S. Tsirkin for (i = 0; i < nvqs; ++i) 230*0b0f9dc5SMichael S. Tsirkin if (callbacks[i]) 231*0b0f9dc5SMichael S. Tsirkin ++nvectors; 232bf951b10SMichael S. Tsirkin } else { 233*0b0f9dc5SMichael S. Tsirkin /* Second best: one for change, shared for all vqs. */ 234*0b0f9dc5SMichael S. Tsirkin nvectors = 2; 2355f4c9760SMichael S. Tsirkin } 2365f4c9760SMichael S. Tsirkin 237*0b0f9dc5SMichael S. Tsirkin err = vp_request_msix_vectors(vdev, nvectors, per_vq_vectors, 238*0b0f9dc5SMichael S. Tsirkin per_vq_vectors ? desc : NULL); 2395f4c9760SMichael S. Tsirkin if (err) 240*0b0f9dc5SMichael S. Tsirkin goto error_find; 2415f4c9760SMichael S. Tsirkin 242*0b0f9dc5SMichael S. Tsirkin if (per_vq_vectors) { 2435c34d002SChristoph Hellwig vp_dev->msix_vector_map = kmalloc_array(nvqs, 2445c34d002SChristoph Hellwig sizeof(*vp_dev->msix_vector_map), GFP_KERNEL); 2455c34d002SChristoph Hellwig if (!vp_dev->msix_vector_map) 246*0b0f9dc5SMichael S. Tsirkin goto error_find; 247*0b0f9dc5SMichael S. Tsirkin } 2485c34d002SChristoph Hellwig 249*0b0f9dc5SMichael S. Tsirkin allocated_vectors = vp_dev->msix_used_vectors; 2505f4c9760SMichael S. Tsirkin for (i = 0; i < nvqs; ++i) { 2515f4c9760SMichael S. Tsirkin if (!names[i]) { 2525f4c9760SMichael S. Tsirkin vqs[i] = NULL; 2535f4c9760SMichael S. Tsirkin continue; 254a3cbec69SChristoph Hellwig } 255a3cbec69SChristoph Hellwig 256*0b0f9dc5SMichael S. Tsirkin if (!callbacks[i]) 25707ec5148SChristoph Hellwig msix_vec = VIRTIO_MSI_NO_VECTOR; 258*0b0f9dc5SMichael S. Tsirkin else if (per_vq_vectors) 259*0b0f9dc5SMichael S. Tsirkin msix_vec = allocated_vectors++; 260*0b0f9dc5SMichael S. Tsirkin else 261*0b0f9dc5SMichael S. Tsirkin msix_vec = VP_MSIX_VQ_VECTOR; 2625c34d002SChristoph Hellwig vqs[i] = vp_dev->setup_vq(vp_dev, i, callbacks[i], names[i], 2635c34d002SChristoph Hellwig msix_vec); 2645f4c9760SMichael S. Tsirkin if (IS_ERR(vqs[i])) { 2655f4c9760SMichael S. Tsirkin err = PTR_ERR(vqs[i]); 266*0b0f9dc5SMichael S. Tsirkin goto error_find; 2675f4c9760SMichael S. Tsirkin } 2685f4c9760SMichael S. Tsirkin 269*0b0f9dc5SMichael S. Tsirkin if (!per_vq_vectors) 270*0b0f9dc5SMichael S. Tsirkin continue; 271*0b0f9dc5SMichael S. Tsirkin 2725c34d002SChristoph Hellwig if (msix_vec == VIRTIO_MSI_NO_VECTOR) { 2735c34d002SChristoph Hellwig vp_dev->msix_vector_map[i] = VIRTIO_MSI_NO_VECTOR; 2745c34d002SChristoph Hellwig continue; 2755c34d002SChristoph Hellwig } 2765c34d002SChristoph Hellwig 277*0b0f9dc5SMichael S. Tsirkin /* allocate per-vq irq if available and necessary */ 278*0b0f9dc5SMichael S. Tsirkin snprintf(vp_dev->msix_names[msix_vec], 279*0b0f9dc5SMichael S. Tsirkin sizeof *vp_dev->msix_names, 280*0b0f9dc5SMichael S. Tsirkin "%s-%s", 2815f4c9760SMichael S. Tsirkin dev_name(&vp_dev->vdev.dev), names[i]); 282fa3a3279SChristoph Hellwig err = request_irq(pci_irq_vector(vp_dev->pci_dev, msix_vec), 283*0b0f9dc5SMichael S. Tsirkin vring_interrupt, 0, 284*0b0f9dc5SMichael S. Tsirkin vp_dev->msix_names[msix_vec], 285*0b0f9dc5SMichael S. Tsirkin vqs[i]); 2865c34d002SChristoph Hellwig if (err) { 2875c34d002SChristoph Hellwig /* don't free this irq on error */ 2885c34d002SChristoph Hellwig vp_dev->msix_vector_map[i] = VIRTIO_MSI_NO_VECTOR; 289*0b0f9dc5SMichael S. Tsirkin goto error_find; 2905f4c9760SMichael S. Tsirkin } 2915c34d002SChristoph Hellwig vp_dev->msix_vector_map[i] = msix_vec; 2925c34d002SChristoph Hellwig } 2935f4c9760SMichael S. Tsirkin return 0; 2945f4c9760SMichael S. Tsirkin 295*0b0f9dc5SMichael S. Tsirkin error_find: 296*0b0f9dc5SMichael S. Tsirkin vp_del_vqs(vdev); 2975f4c9760SMichael S. Tsirkin return err; 2985f4c9760SMichael S. Tsirkin } 2995f4c9760SMichael S. Tsirkin 300a3cbec69SChristoph Hellwig static int vp_find_vqs_intx(struct virtio_device *vdev, unsigned nvqs, 301a3cbec69SChristoph Hellwig struct virtqueue *vqs[], vq_callback_t *callbacks[], 302a3cbec69SChristoph Hellwig const char * const names[]) 303a3cbec69SChristoph Hellwig { 304a3cbec69SChristoph Hellwig struct virtio_pci_device *vp_dev = to_vp_device(vdev); 305a3cbec69SChristoph Hellwig int i, err; 306a3cbec69SChristoph Hellwig 307a3cbec69SChristoph Hellwig err = request_irq(vp_dev->pci_dev->irq, vp_interrupt, IRQF_SHARED, 308a3cbec69SChristoph Hellwig dev_name(&vdev->dev), vp_dev); 309a3cbec69SChristoph Hellwig if (err) 310*0b0f9dc5SMichael S. Tsirkin goto out_del_vqs; 311a3cbec69SChristoph Hellwig 312*0b0f9dc5SMichael S. Tsirkin vp_dev->intx_enabled = 1; 313a3cbec69SChristoph Hellwig for (i = 0; i < nvqs; ++i) { 314a3cbec69SChristoph Hellwig if (!names[i]) { 315a3cbec69SChristoph Hellwig vqs[i] = NULL; 316a3cbec69SChristoph Hellwig continue; 317a3cbec69SChristoph Hellwig } 3185c34d002SChristoph Hellwig vqs[i] = vp_dev->setup_vq(vp_dev, i, callbacks[i], names[i], 319a3cbec69SChristoph Hellwig VIRTIO_MSI_NO_VECTOR); 320a3cbec69SChristoph Hellwig if (IS_ERR(vqs[i])) { 321a3cbec69SChristoph Hellwig err = PTR_ERR(vqs[i]); 322*0b0f9dc5SMichael S. Tsirkin goto out_del_vqs; 323a3cbec69SChristoph Hellwig } 324a3cbec69SChristoph Hellwig } 325a3cbec69SChristoph Hellwig 326a3cbec69SChristoph Hellwig return 0; 327*0b0f9dc5SMichael S. Tsirkin out_del_vqs: 328*0b0f9dc5SMichael S. Tsirkin vp_del_vqs(vdev); 329a3cbec69SChristoph Hellwig return err; 330a3cbec69SChristoph Hellwig } 331a3cbec69SChristoph Hellwig 3325f4c9760SMichael S. Tsirkin /* the config->find_vqs() implementation */ 3335f4c9760SMichael S. Tsirkin int vp_find_vqs(struct virtio_device *vdev, unsigned nvqs, 334fb5e31d9SChristoph Hellwig struct virtqueue *vqs[], vq_callback_t *callbacks[], 335fb5e31d9SChristoph Hellwig const char * const names[], struct irq_affinity *desc) 3365f4c9760SMichael S. Tsirkin { 3375f4c9760SMichael S. Tsirkin int err; 3385f4c9760SMichael S. Tsirkin 339bf951b10SMichael S. Tsirkin /* Try MSI-X with one vector per queue. */ 340bf951b10SMichael S. Tsirkin err = vp_find_vqs_msix(vdev, nvqs, vqs, callbacks, names, true, desc); 3415f4c9760SMichael S. Tsirkin if (!err) 3425f4c9760SMichael S. Tsirkin return 0; 343bf951b10SMichael S. Tsirkin /* Fallback: MSI-X with one vector for config, one shared for queues. */ 344bf951b10SMichael S. Tsirkin err = vp_find_vqs_msix(vdev, nvqs, vqs, callbacks, names, false, desc); 345bf951b10SMichael S. Tsirkin if (!err) 346bf951b10SMichael S. Tsirkin return 0; 347bf951b10SMichael S. Tsirkin /* Finally fall back to regular interrupts. */ 348a3cbec69SChristoph Hellwig return vp_find_vqs_intx(vdev, nvqs, vqs, callbacks, names); 3495f4c9760SMichael S. Tsirkin } 3505f4c9760SMichael S. Tsirkin 3515f4c9760SMichael S. Tsirkin const char *vp_bus_name(struct virtio_device *vdev) 3525f4c9760SMichael S. Tsirkin { 3535f4c9760SMichael S. Tsirkin struct virtio_pci_device *vp_dev = to_vp_device(vdev); 3545f4c9760SMichael S. Tsirkin 3555f4c9760SMichael S. Tsirkin return pci_name(vp_dev->pci_dev); 3565f4c9760SMichael S. Tsirkin } 3575f4c9760SMichael S. Tsirkin 3585f4c9760SMichael S. Tsirkin /* Setup the affinity for a virtqueue: 3595f4c9760SMichael S. Tsirkin * - force the affinity for per vq vector 3605f4c9760SMichael S. Tsirkin * - OR over all affinities for shared MSI 3615f4c9760SMichael S. Tsirkin * - ignore the affinity request if we're using INTX 3625f4c9760SMichael S. Tsirkin */ 3635f4c9760SMichael S. Tsirkin int vp_set_vq_affinity(struct virtqueue *vq, int cpu) 3645f4c9760SMichael S. Tsirkin { 3655f4c9760SMichael S. Tsirkin struct virtio_device *vdev = vq->vdev; 3665f4c9760SMichael S. Tsirkin struct virtio_pci_device *vp_dev = to_vp_device(vdev); 3675f4c9760SMichael S. Tsirkin 3685f4c9760SMichael S. Tsirkin if (!vq->callback) 3695f4c9760SMichael S. Tsirkin return -EINVAL; 3705f4c9760SMichael S. Tsirkin 3712008c154SMichael S. Tsirkin if (vp_dev->msix_enabled) { 3725c34d002SChristoph Hellwig int vec = vp_dev->msix_vector_map[vq->index]; 3735c34d002SChristoph Hellwig struct cpumask *mask = vp_dev->msix_affinity_masks[vec]; 3745c34d002SChristoph Hellwig unsigned int irq = pci_irq_vector(vp_dev->pci_dev, vec); 3755c34d002SChristoph Hellwig 3765f4c9760SMichael S. Tsirkin if (cpu == -1) 3775f4c9760SMichael S. Tsirkin irq_set_affinity_hint(irq, NULL); 3785f4c9760SMichael S. Tsirkin else { 379210d150eSJiang Liu cpumask_clear(mask); 3805f4c9760SMichael S. Tsirkin cpumask_set_cpu(cpu, mask); 3815f4c9760SMichael S. Tsirkin irq_set_affinity_hint(irq, mask); 3825f4c9760SMichael S. Tsirkin } 3835f4c9760SMichael S. Tsirkin } 3845f4c9760SMichael S. Tsirkin return 0; 3855f4c9760SMichael S. Tsirkin } 3865f4c9760SMichael S. Tsirkin 387bbaba479SChristoph Hellwig const struct cpumask *vp_get_vq_affinity(struct virtio_device *vdev, int index) 388bbaba479SChristoph Hellwig { 389bbaba479SChristoph Hellwig struct virtio_pci_device *vp_dev = to_vp_device(vdev); 390bbaba479SChristoph Hellwig unsigned int *map = vp_dev->msix_vector_map; 391bbaba479SChristoph Hellwig 392bbaba479SChristoph Hellwig if (!map || map[index] == VIRTIO_MSI_NO_VECTOR) 393bbaba479SChristoph Hellwig return NULL; 394bbaba479SChristoph Hellwig 395bbaba479SChristoph Hellwig return pci_irq_get_affinity(vp_dev->pci_dev, map[index]); 396bbaba479SChristoph Hellwig } 397bbaba479SChristoph Hellwig 3985f4c9760SMichael S. Tsirkin #ifdef CONFIG_PM_SLEEP 3995f4c9760SMichael S. Tsirkin static int virtio_pci_freeze(struct device *dev) 4005f4c9760SMichael S. Tsirkin { 4015f4c9760SMichael S. Tsirkin struct pci_dev *pci_dev = to_pci_dev(dev); 4025f4c9760SMichael S. Tsirkin struct virtio_pci_device *vp_dev = pci_get_drvdata(pci_dev); 4035f4c9760SMichael S. Tsirkin int ret; 4045f4c9760SMichael S. Tsirkin 4055f4c9760SMichael S. Tsirkin ret = virtio_device_freeze(&vp_dev->vdev); 4065f4c9760SMichael S. Tsirkin 4075f4c9760SMichael S. Tsirkin if (!ret) 4085f4c9760SMichael S. Tsirkin pci_disable_device(pci_dev); 4095f4c9760SMichael S. Tsirkin return ret; 4105f4c9760SMichael S. Tsirkin } 4115f4c9760SMichael S. Tsirkin 4125f4c9760SMichael S. Tsirkin static int virtio_pci_restore(struct device *dev) 4135f4c9760SMichael S. Tsirkin { 4145f4c9760SMichael S. Tsirkin struct pci_dev *pci_dev = to_pci_dev(dev); 4155f4c9760SMichael S. Tsirkin struct virtio_pci_device *vp_dev = pci_get_drvdata(pci_dev); 4165f4c9760SMichael S. Tsirkin int ret; 4175f4c9760SMichael S. Tsirkin 4185f4c9760SMichael S. Tsirkin ret = pci_enable_device(pci_dev); 4195f4c9760SMichael S. Tsirkin if (ret) 4205f4c9760SMichael S. Tsirkin return ret; 4215f4c9760SMichael S. Tsirkin 4225f4c9760SMichael S. Tsirkin pci_set_master(pci_dev); 4235f4c9760SMichael S. Tsirkin return virtio_device_restore(&vp_dev->vdev); 4245f4c9760SMichael S. Tsirkin } 4255f4c9760SMichael S. Tsirkin 4269a4253dbSMichael S. Tsirkin static const struct dev_pm_ops virtio_pci_pm_ops = { 4275f4c9760SMichael S. Tsirkin SET_SYSTEM_SLEEP_PM_OPS(virtio_pci_freeze, virtio_pci_restore) 4285f4c9760SMichael S. Tsirkin }; 4295f4c9760SMichael S. Tsirkin #endif 4309a4253dbSMichael S. Tsirkin 4319a4253dbSMichael S. Tsirkin 4329a4253dbSMichael S. Tsirkin /* Qumranet donated their vendor ID for devices 0x1000 thru 0x10FF. */ 4339a4253dbSMichael S. Tsirkin static const struct pci_device_id virtio_pci_id_table[] = { 434caf02abfSRobin H. Johnson { PCI_DEVICE(PCI_VENDOR_ID_REDHAT_QUMRANET, PCI_ANY_ID) }, 4359a4253dbSMichael S. Tsirkin { 0 } 4369a4253dbSMichael S. Tsirkin }; 4379a4253dbSMichael S. Tsirkin 4389a4253dbSMichael S. Tsirkin MODULE_DEVICE_TABLE(pci, virtio_pci_id_table); 4399a4253dbSMichael S. Tsirkin 440ff31d2e2SMichael S. Tsirkin static void virtio_pci_release_dev(struct device *_d) 441ff31d2e2SMichael S. Tsirkin { 442ff31d2e2SMichael S. Tsirkin struct virtio_device *vdev = dev_to_virtio(_d); 443ff31d2e2SMichael S. Tsirkin struct virtio_pci_device *vp_dev = to_vp_device(vdev); 444ff31d2e2SMichael S. Tsirkin 445ff31d2e2SMichael S. Tsirkin /* As struct device is a kobject, it's not safe to 446ff31d2e2SMichael S. Tsirkin * free the memory (including the reference counter itself) 447ff31d2e2SMichael S. Tsirkin * until it's release callback. */ 448ff31d2e2SMichael S. Tsirkin kfree(vp_dev); 449ff31d2e2SMichael S. Tsirkin } 450ff31d2e2SMichael S. Tsirkin 4519a4253dbSMichael S. Tsirkin static int virtio_pci_probe(struct pci_dev *pci_dev, 4529a4253dbSMichael S. Tsirkin const struct pci_device_id *id) 4539a4253dbSMichael S. Tsirkin { 454ff31d2e2SMichael S. Tsirkin struct virtio_pci_device *vp_dev; 455ff31d2e2SMichael S. Tsirkin int rc; 456ff31d2e2SMichael S. Tsirkin 457ff31d2e2SMichael S. Tsirkin /* allocate our structure and fill it out */ 458ff31d2e2SMichael S. Tsirkin vp_dev = kzalloc(sizeof(struct virtio_pci_device), GFP_KERNEL); 459ff31d2e2SMichael S. Tsirkin if (!vp_dev) 460ff31d2e2SMichael S. Tsirkin return -ENOMEM; 461ff31d2e2SMichael S. Tsirkin 462ff31d2e2SMichael S. Tsirkin pci_set_drvdata(pci_dev, vp_dev); 463ff31d2e2SMichael S. Tsirkin vp_dev->vdev.dev.parent = &pci_dev->dev; 464ff31d2e2SMichael S. Tsirkin vp_dev->vdev.dev.release = virtio_pci_release_dev; 465ff31d2e2SMichael S. Tsirkin vp_dev->pci_dev = pci_dev; 466ff31d2e2SMichael S. Tsirkin 467ff31d2e2SMichael S. Tsirkin /* enable the device */ 468ff31d2e2SMichael S. Tsirkin rc = pci_enable_device(pci_dev); 469ff31d2e2SMichael S. Tsirkin if (rc) 470ff31d2e2SMichael S. Tsirkin goto err_enable_device; 471ff31d2e2SMichael S. Tsirkin 472ac399d8fSMichael S. Tsirkin if (force_legacy) { 473ac399d8fSMichael S. Tsirkin rc = virtio_pci_legacy_probe(vp_dev); 474ac399d8fSMichael S. Tsirkin /* Also try modern mode if we can't map BAR0 (no IO space). */ 475ac399d8fSMichael S. Tsirkin if (rc == -ENODEV || rc == -ENOMEM) 476ac399d8fSMichael S. Tsirkin rc = virtio_pci_modern_probe(vp_dev); 477ac399d8fSMichael S. Tsirkin if (rc) 478ac399d8fSMichael S. Tsirkin goto err_probe; 479ac399d8fSMichael S. Tsirkin } else { 4801fcf0512SMichael S. Tsirkin rc = virtio_pci_modern_probe(vp_dev); 4811fcf0512SMichael S. Tsirkin if (rc == -ENODEV) 482ff31d2e2SMichael S. Tsirkin rc = virtio_pci_legacy_probe(vp_dev); 483ff31d2e2SMichael S. Tsirkin if (rc) 484ff31d2e2SMichael S. Tsirkin goto err_probe; 485ac399d8fSMichael S. Tsirkin } 486ff31d2e2SMichael S. Tsirkin 487ff31d2e2SMichael S. Tsirkin pci_set_master(pci_dev); 488ff31d2e2SMichael S. Tsirkin 489ff31d2e2SMichael S. Tsirkin rc = register_virtio_device(&vp_dev->vdev); 490ff31d2e2SMichael S. Tsirkin if (rc) 491ff31d2e2SMichael S. Tsirkin goto err_register; 492ff31d2e2SMichael S. Tsirkin 493ff31d2e2SMichael S. Tsirkin return 0; 494ff31d2e2SMichael S. Tsirkin 495ff31d2e2SMichael S. Tsirkin err_register: 4961fcf0512SMichael S. Tsirkin if (vp_dev->ioaddr) 497ff31d2e2SMichael S. Tsirkin virtio_pci_legacy_remove(vp_dev); 4981fcf0512SMichael S. Tsirkin else 4991fcf0512SMichael S. Tsirkin virtio_pci_modern_remove(vp_dev); 500ff31d2e2SMichael S. Tsirkin err_probe: 501ff31d2e2SMichael S. Tsirkin pci_disable_device(pci_dev); 502ff31d2e2SMichael S. Tsirkin err_enable_device: 503ff31d2e2SMichael S. Tsirkin kfree(vp_dev); 504ff31d2e2SMichael S. Tsirkin return rc; 5059a4253dbSMichael S. Tsirkin } 5069a4253dbSMichael S. Tsirkin 5079a4253dbSMichael S. Tsirkin static void virtio_pci_remove(struct pci_dev *pci_dev) 5089a4253dbSMichael S. Tsirkin { 509ff31d2e2SMichael S. Tsirkin struct virtio_pci_device *vp_dev = pci_get_drvdata(pci_dev); 5102989be09SMichael S. Tsirkin struct device *dev = get_device(&vp_dev->vdev.dev); 511ff31d2e2SMichael S. Tsirkin 512ff31d2e2SMichael S. Tsirkin unregister_virtio_device(&vp_dev->vdev); 513ff31d2e2SMichael S. Tsirkin 5141fcf0512SMichael S. Tsirkin if (vp_dev->ioaddr) 5151fcf0512SMichael S. Tsirkin virtio_pci_legacy_remove(vp_dev); 5161fcf0512SMichael S. Tsirkin else 5171fcf0512SMichael S. Tsirkin virtio_pci_modern_remove(vp_dev); 518ff31d2e2SMichael S. Tsirkin 519ff31d2e2SMichael S. Tsirkin pci_disable_device(pci_dev); 5202989be09SMichael S. Tsirkin put_device(dev); 5219a4253dbSMichael S. Tsirkin } 5229a4253dbSMichael S. Tsirkin 5239a4253dbSMichael S. Tsirkin static struct pci_driver virtio_pci_driver = { 5249a4253dbSMichael S. Tsirkin .name = "virtio-pci", 5259a4253dbSMichael S. Tsirkin .id_table = virtio_pci_id_table, 5269a4253dbSMichael S. Tsirkin .probe = virtio_pci_probe, 5279a4253dbSMichael S. Tsirkin .remove = virtio_pci_remove, 5289a4253dbSMichael S. Tsirkin #ifdef CONFIG_PM_SLEEP 5299a4253dbSMichael S. Tsirkin .driver.pm = &virtio_pci_pm_ops, 5309a4253dbSMichael S. Tsirkin #endif 5319a4253dbSMichael S. Tsirkin }; 5329a4253dbSMichael S. Tsirkin 5339a4253dbSMichael S. Tsirkin module_pci_driver(virtio_pci_driver); 5345ff16110SHerbert Xu 5355ff16110SHerbert Xu MODULE_AUTHOR("Anthony Liguori <aliguori@us.ibm.com>"); 5365ff16110SHerbert Xu MODULE_DESCRIPTION("virtio-pci"); 5375ff16110SHerbert Xu MODULE_LICENSE("GPL"); 5385ff16110SHerbert Xu MODULE_VERSION("1"); 539