Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-or-later
0002 /*
0003  * Virtio PCI driver - common functionality for all device versions
0004  *
0005  * This module allows virtio devices to be used over a virtual PCI device.
0006  * This can be used with QEMU based VMMs like KVM or Xen.
0007  *
0008  * Copyright IBM Corp. 2007
0009  * Copyright Red Hat, Inc. 2014
0010  *
0011  * Authors:
0012  *  Anthony Liguori  <aliguori@us.ibm.com>
0013  *  Rusty Russell <rusty@rustcorp.com.au>
0014  *  Michael S. Tsirkin <mst@redhat.com>
0015  */
0016 
0017 #include "virtio_pci_common.h"
0018 
0019 static bool force_legacy = false;
0020 
0021 #if IS_ENABLED(CONFIG_VIRTIO_PCI_LEGACY)
0022 module_param(force_legacy, bool, 0444);
0023 MODULE_PARM_DESC(force_legacy,
0024          "Force legacy mode for transitional virtio 1 devices");
0025 #endif
0026 
0027 /* wait for pending irq handlers */
0028 void vp_synchronize_vectors(struct virtio_device *vdev)
0029 {
0030     struct virtio_pci_device *vp_dev = to_vp_device(vdev);
0031     int i;
0032 
0033     if (vp_dev->intx_enabled)
0034         synchronize_irq(vp_dev->pci_dev->irq);
0035 
0036     for (i = 0; i < vp_dev->msix_vectors; ++i)
0037         synchronize_irq(pci_irq_vector(vp_dev->pci_dev, i));
0038 }
0039 
0040 /* the notify function used when creating a virt queue */
0041 bool vp_notify(struct virtqueue *vq)
0042 {
0043     /* we write the queue's selector into the notification register to
0044      * signal the other end */
0045     iowrite16(vq->index, (void __iomem *)vq->priv);
0046     return true;
0047 }
0048 
0049 /* Handle a configuration change: Tell driver if it wants to know. */
0050 static irqreturn_t vp_config_changed(int irq, void *opaque)
0051 {
0052     struct virtio_pci_device *vp_dev = opaque;
0053 
0054     virtio_config_changed(&vp_dev->vdev);
0055     return IRQ_HANDLED;
0056 }
0057 
0058 /* Notify all virtqueues on an interrupt. */
0059 static irqreturn_t vp_vring_interrupt(int irq, void *opaque)
0060 {
0061     struct virtio_pci_device *vp_dev = opaque;
0062     struct virtio_pci_vq_info *info;
0063     irqreturn_t ret = IRQ_NONE;
0064     unsigned long flags;
0065 
0066     spin_lock_irqsave(&vp_dev->lock, flags);
0067     list_for_each_entry(info, &vp_dev->virtqueues, node) {
0068         if (vring_interrupt(irq, info->vq) == IRQ_HANDLED)
0069             ret = IRQ_HANDLED;
0070     }
0071     spin_unlock_irqrestore(&vp_dev->lock, flags);
0072 
0073     return ret;
0074 }
0075 
0076 /* A small wrapper to also acknowledge the interrupt when it's handled.
0077  * I really need an EIO hook for the vring so I can ack the interrupt once we
0078  * know that we'll be handling the IRQ but before we invoke the callback since
0079  * the callback may notify the host which results in the host attempting to
0080  * raise an interrupt that we would then mask once we acknowledged the
0081  * interrupt. */
0082 static irqreturn_t vp_interrupt(int irq, void *opaque)
0083 {
0084     struct virtio_pci_device *vp_dev = opaque;
0085     u8 isr;
0086 
0087     /* reading the ISR has the effect of also clearing it so it's very
0088      * important to save off the value. */
0089     isr = ioread8(vp_dev->isr);
0090 
0091     /* It's definitely not us if the ISR was not high */
0092     if (!isr)
0093         return IRQ_NONE;
0094 
0095     /* Configuration change?  Tell driver if it wants to know. */
0096     if (isr & VIRTIO_PCI_ISR_CONFIG)
0097         vp_config_changed(irq, opaque);
0098 
0099     return vp_vring_interrupt(irq, opaque);
0100 }
0101 
0102 static int vp_request_msix_vectors(struct virtio_device *vdev, int nvectors,
0103                    bool per_vq_vectors, struct irq_affinity *desc)
0104 {
0105     struct virtio_pci_device *vp_dev = to_vp_device(vdev);
0106     const char *name = dev_name(&vp_dev->vdev.dev);
0107     unsigned int flags = PCI_IRQ_MSIX;
0108     unsigned int i, v;
0109     int err = -ENOMEM;
0110 
0111     vp_dev->msix_vectors = nvectors;
0112 
0113     vp_dev->msix_names = kmalloc_array(nvectors,
0114                        sizeof(*vp_dev->msix_names),
0115                        GFP_KERNEL);
0116     if (!vp_dev->msix_names)
0117         goto error;
0118     vp_dev->msix_affinity_masks
0119         = kcalloc(nvectors, sizeof(*vp_dev->msix_affinity_masks),
0120               GFP_KERNEL);
0121     if (!vp_dev->msix_affinity_masks)
0122         goto error;
0123     for (i = 0; i < nvectors; ++i)
0124         if (!alloc_cpumask_var(&vp_dev->msix_affinity_masks[i],
0125                     GFP_KERNEL))
0126             goto error;
0127 
0128     if (desc) {
0129         flags |= PCI_IRQ_AFFINITY;
0130         desc->pre_vectors++; /* virtio config vector */
0131     }
0132 
0133     err = pci_alloc_irq_vectors_affinity(vp_dev->pci_dev, nvectors,
0134                          nvectors, flags, desc);
0135     if (err < 0)
0136         goto error;
0137     vp_dev->msix_enabled = 1;
0138 
0139     /* Set the vector used for configuration */
0140     v = vp_dev->msix_used_vectors;
0141     snprintf(vp_dev->msix_names[v], sizeof *vp_dev->msix_names,
0142          "%s-config", name);
0143     err = request_irq(pci_irq_vector(vp_dev->pci_dev, v),
0144               vp_config_changed, 0, vp_dev->msix_names[v],
0145               vp_dev);
0146     if (err)
0147         goto error;
0148     ++vp_dev->msix_used_vectors;
0149 
0150     v = vp_dev->config_vector(vp_dev, v);
0151     /* Verify we had enough resources to assign the vector */
0152     if (v == VIRTIO_MSI_NO_VECTOR) {
0153         err = -EBUSY;
0154         goto error;
0155     }
0156 
0157     if (!per_vq_vectors) {
0158         /* Shared vector for all VQs */
0159         v = vp_dev->msix_used_vectors;
0160         snprintf(vp_dev->msix_names[v], sizeof *vp_dev->msix_names,
0161              "%s-virtqueues", name);
0162         err = request_irq(pci_irq_vector(vp_dev->pci_dev, v),
0163                   vp_vring_interrupt, 0, vp_dev->msix_names[v],
0164                   vp_dev);
0165         if (err)
0166             goto error;
0167         ++vp_dev->msix_used_vectors;
0168     }
0169     return 0;
0170 error:
0171     return err;
0172 }
0173 
0174 static struct virtqueue *vp_setup_vq(struct virtio_device *vdev, unsigned int index,
0175                      void (*callback)(struct virtqueue *vq),
0176                      const char *name,
0177                      bool ctx,
0178                      u16 msix_vec)
0179 {
0180     struct virtio_pci_device *vp_dev = to_vp_device(vdev);
0181     struct virtio_pci_vq_info *info = kmalloc(sizeof *info, GFP_KERNEL);
0182     struct virtqueue *vq;
0183     unsigned long flags;
0184 
0185     /* fill out our structure that represents an active queue */
0186     if (!info)
0187         return ERR_PTR(-ENOMEM);
0188 
0189     vq = vp_dev->setup_vq(vp_dev, info, index, callback, name, ctx,
0190                   msix_vec);
0191     if (IS_ERR(vq))
0192         goto out_info;
0193 
0194     info->vq = vq;
0195     if (callback) {
0196         spin_lock_irqsave(&vp_dev->lock, flags);
0197         list_add(&info->node, &vp_dev->virtqueues);
0198         spin_unlock_irqrestore(&vp_dev->lock, flags);
0199     } else {
0200         INIT_LIST_HEAD(&info->node);
0201     }
0202 
0203     vp_dev->vqs[index] = info;
0204     return vq;
0205 
0206 out_info:
0207     kfree(info);
0208     return vq;
0209 }
0210 
0211 static void vp_del_vq(struct virtqueue *vq)
0212 {
0213     struct virtio_pci_device *vp_dev = to_vp_device(vq->vdev);
0214     struct virtio_pci_vq_info *info = vp_dev->vqs[vq->index];
0215     unsigned long flags;
0216 
0217     /*
0218      * If it fails during re-enable reset vq. This way we won't rejoin
0219      * info->node to the queue. Prevent unexpected irqs.
0220      */
0221     if (!vq->reset) {
0222         spin_lock_irqsave(&vp_dev->lock, flags);
0223         list_del(&info->node);
0224         spin_unlock_irqrestore(&vp_dev->lock, flags);
0225     }
0226 
0227     vp_dev->del_vq(info);
0228     kfree(info);
0229 }
0230 
0231 /* the config->del_vqs() implementation */
0232 void vp_del_vqs(struct virtio_device *vdev)
0233 {
0234     struct virtio_pci_device *vp_dev = to_vp_device(vdev);
0235     struct virtqueue *vq, *n;
0236     int i;
0237 
0238     list_for_each_entry_safe(vq, n, &vdev->vqs, list) {
0239         if (vp_dev->per_vq_vectors) {
0240             int v = vp_dev->vqs[vq->index]->msix_vector;
0241 
0242             if (v != VIRTIO_MSI_NO_VECTOR) {
0243                 int irq = pci_irq_vector(vp_dev->pci_dev, v);
0244 
0245                 irq_set_affinity_hint(irq, NULL);
0246                 free_irq(irq, vq);
0247             }
0248         }
0249         vp_del_vq(vq);
0250     }
0251     vp_dev->per_vq_vectors = false;
0252 
0253     if (vp_dev->intx_enabled) {
0254         free_irq(vp_dev->pci_dev->irq, vp_dev);
0255         vp_dev->intx_enabled = 0;
0256     }
0257 
0258     for (i = 0; i < vp_dev->msix_used_vectors; ++i)
0259         free_irq(pci_irq_vector(vp_dev->pci_dev, i), vp_dev);
0260 
0261     if (vp_dev->msix_affinity_masks) {
0262         for (i = 0; i < vp_dev->msix_vectors; i++)
0263             free_cpumask_var(vp_dev->msix_affinity_masks[i]);
0264     }
0265 
0266     if (vp_dev->msix_enabled) {
0267         /* Disable the vector used for configuration */
0268         vp_dev->config_vector(vp_dev, VIRTIO_MSI_NO_VECTOR);
0269 
0270         pci_free_irq_vectors(vp_dev->pci_dev);
0271         vp_dev->msix_enabled = 0;
0272     }
0273 
0274     vp_dev->msix_vectors = 0;
0275     vp_dev->msix_used_vectors = 0;
0276     kfree(vp_dev->msix_names);
0277     vp_dev->msix_names = NULL;
0278     kfree(vp_dev->msix_affinity_masks);
0279     vp_dev->msix_affinity_masks = NULL;
0280     kfree(vp_dev->vqs);
0281     vp_dev->vqs = NULL;
0282 }
0283 
0284 static int vp_find_vqs_msix(struct virtio_device *vdev, unsigned int nvqs,
0285         struct virtqueue *vqs[], vq_callback_t *callbacks[],
0286         const char * const names[], bool per_vq_vectors,
0287         const bool *ctx,
0288         struct irq_affinity *desc)
0289 {
0290     struct virtio_pci_device *vp_dev = to_vp_device(vdev);
0291     u16 msix_vec;
0292     int i, err, nvectors, allocated_vectors, queue_idx = 0;
0293 
0294     vp_dev->vqs = kcalloc(nvqs, sizeof(*vp_dev->vqs), GFP_KERNEL);
0295     if (!vp_dev->vqs)
0296         return -ENOMEM;
0297 
0298     if (per_vq_vectors) {
0299         /* Best option: one for change interrupt, one per vq. */
0300         nvectors = 1;
0301         for (i = 0; i < nvqs; ++i)
0302             if (names[i] && callbacks[i])
0303                 ++nvectors;
0304     } else {
0305         /* Second best: one for change, shared for all vqs. */
0306         nvectors = 2;
0307     }
0308 
0309     err = vp_request_msix_vectors(vdev, nvectors, per_vq_vectors,
0310                       per_vq_vectors ? desc : NULL);
0311     if (err)
0312         goto error_find;
0313 
0314     vp_dev->per_vq_vectors = per_vq_vectors;
0315     allocated_vectors = vp_dev->msix_used_vectors;
0316     for (i = 0; i < nvqs; ++i) {
0317         if (!names[i]) {
0318             vqs[i] = NULL;
0319             continue;
0320         }
0321 
0322         if (!callbacks[i])
0323             msix_vec = VIRTIO_MSI_NO_VECTOR;
0324         else if (vp_dev->per_vq_vectors)
0325             msix_vec = allocated_vectors++;
0326         else
0327             msix_vec = VP_MSIX_VQ_VECTOR;
0328         vqs[i] = vp_setup_vq(vdev, queue_idx++, callbacks[i], names[i],
0329                      ctx ? ctx[i] : false,
0330                      msix_vec);
0331         if (IS_ERR(vqs[i])) {
0332             err = PTR_ERR(vqs[i]);
0333             goto error_find;
0334         }
0335 
0336         if (!vp_dev->per_vq_vectors || msix_vec == VIRTIO_MSI_NO_VECTOR)
0337             continue;
0338 
0339         /* allocate per-vq irq if available and necessary */
0340         snprintf(vp_dev->msix_names[msix_vec],
0341              sizeof *vp_dev->msix_names,
0342              "%s-%s",
0343              dev_name(&vp_dev->vdev.dev), names[i]);
0344         err = request_irq(pci_irq_vector(vp_dev->pci_dev, msix_vec),
0345                   vring_interrupt, 0,
0346                   vp_dev->msix_names[msix_vec],
0347                   vqs[i]);
0348         if (err)
0349             goto error_find;
0350     }
0351     return 0;
0352 
0353 error_find:
0354     vp_del_vqs(vdev);
0355     return err;
0356 }
0357 
0358 static int vp_find_vqs_intx(struct virtio_device *vdev, unsigned int nvqs,
0359         struct virtqueue *vqs[], vq_callback_t *callbacks[],
0360         const char * const names[], const bool *ctx)
0361 {
0362     struct virtio_pci_device *vp_dev = to_vp_device(vdev);
0363     int i, err, queue_idx = 0;
0364 
0365     vp_dev->vqs = kcalloc(nvqs, sizeof(*vp_dev->vqs), GFP_KERNEL);
0366     if (!vp_dev->vqs)
0367         return -ENOMEM;
0368 
0369     err = request_irq(vp_dev->pci_dev->irq, vp_interrupt, IRQF_SHARED,
0370             dev_name(&vdev->dev), vp_dev);
0371     if (err)
0372         goto out_del_vqs;
0373 
0374     vp_dev->intx_enabled = 1;
0375     vp_dev->per_vq_vectors = false;
0376     for (i = 0; i < nvqs; ++i) {
0377         if (!names[i]) {
0378             vqs[i] = NULL;
0379             continue;
0380         }
0381         vqs[i] = vp_setup_vq(vdev, queue_idx++, callbacks[i], names[i],
0382                      ctx ? ctx[i] : false,
0383                      VIRTIO_MSI_NO_VECTOR);
0384         if (IS_ERR(vqs[i])) {
0385             err = PTR_ERR(vqs[i]);
0386             goto out_del_vqs;
0387         }
0388     }
0389 
0390     return 0;
0391 out_del_vqs:
0392     vp_del_vqs(vdev);
0393     return err;
0394 }
0395 
0396 /* the config->find_vqs() implementation */
0397 int vp_find_vqs(struct virtio_device *vdev, unsigned int nvqs,
0398         struct virtqueue *vqs[], vq_callback_t *callbacks[],
0399         const char * const names[], const bool *ctx,
0400         struct irq_affinity *desc)
0401 {
0402     int err;
0403 
0404     /* Try MSI-X with one vector per queue. */
0405     err = vp_find_vqs_msix(vdev, nvqs, vqs, callbacks, names, true, ctx, desc);
0406     if (!err)
0407         return 0;
0408     /* Fallback: MSI-X with one vector for config, one shared for queues. */
0409     err = vp_find_vqs_msix(vdev, nvqs, vqs, callbacks, names, false, ctx, desc);
0410     if (!err)
0411         return 0;
0412     /* Finally fall back to regular interrupts. */
0413     return vp_find_vqs_intx(vdev, nvqs, vqs, callbacks, names, ctx);
0414 }
0415 
0416 const char *vp_bus_name(struct virtio_device *vdev)
0417 {
0418     struct virtio_pci_device *vp_dev = to_vp_device(vdev);
0419 
0420     return pci_name(vp_dev->pci_dev);
0421 }
0422 
0423 /* Setup the affinity for a virtqueue:
0424  * - force the affinity for per vq vector
0425  * - OR over all affinities for shared MSI
0426  * - ignore the affinity request if we're using INTX
0427  */
0428 int vp_set_vq_affinity(struct virtqueue *vq, const struct cpumask *cpu_mask)
0429 {
0430     struct virtio_device *vdev = vq->vdev;
0431     struct virtio_pci_device *vp_dev = to_vp_device(vdev);
0432     struct virtio_pci_vq_info *info = vp_dev->vqs[vq->index];
0433     struct cpumask *mask;
0434     unsigned int irq;
0435 
0436     if (!vq->callback)
0437         return -EINVAL;
0438 
0439     if (vp_dev->msix_enabled) {
0440         mask = vp_dev->msix_affinity_masks[info->msix_vector];
0441         irq = pci_irq_vector(vp_dev->pci_dev, info->msix_vector);
0442         if (!cpu_mask)
0443             irq_set_affinity_hint(irq, NULL);
0444         else {
0445             cpumask_copy(mask, cpu_mask);
0446             irq_set_affinity_hint(irq, mask);
0447         }
0448     }
0449     return 0;
0450 }
0451 
0452 const struct cpumask *vp_get_vq_affinity(struct virtio_device *vdev, int index)
0453 {
0454     struct virtio_pci_device *vp_dev = to_vp_device(vdev);
0455 
0456     if (!vp_dev->per_vq_vectors ||
0457         vp_dev->vqs[index]->msix_vector == VIRTIO_MSI_NO_VECTOR)
0458         return NULL;
0459 
0460     return pci_irq_get_affinity(vp_dev->pci_dev,
0461                     vp_dev->vqs[index]->msix_vector);
0462 }
0463 
0464 #ifdef CONFIG_PM_SLEEP
0465 static int virtio_pci_freeze(struct device *dev)
0466 {
0467     struct pci_dev *pci_dev = to_pci_dev(dev);
0468     struct virtio_pci_device *vp_dev = pci_get_drvdata(pci_dev);
0469     int ret;
0470 
0471     ret = virtio_device_freeze(&vp_dev->vdev);
0472 
0473     if (!ret)
0474         pci_disable_device(pci_dev);
0475     return ret;
0476 }
0477 
0478 static int virtio_pci_restore(struct device *dev)
0479 {
0480     struct pci_dev *pci_dev = to_pci_dev(dev);
0481     struct virtio_pci_device *vp_dev = pci_get_drvdata(pci_dev);
0482     int ret;
0483 
0484     ret = pci_enable_device(pci_dev);
0485     if (ret)
0486         return ret;
0487 
0488     pci_set_master(pci_dev);
0489     return virtio_device_restore(&vp_dev->vdev);
0490 }
0491 
0492 static const struct dev_pm_ops virtio_pci_pm_ops = {
0493     SET_SYSTEM_SLEEP_PM_OPS(virtio_pci_freeze, virtio_pci_restore)
0494 };
0495 #endif
0496 
0497 
0498 /* Qumranet donated their vendor ID for devices 0x1000 thru 0x10FF. */
0499 static const struct pci_device_id virtio_pci_id_table[] = {
0500     { PCI_DEVICE(PCI_VENDOR_ID_REDHAT_QUMRANET, PCI_ANY_ID) },
0501     { 0 }
0502 };
0503 
0504 MODULE_DEVICE_TABLE(pci, virtio_pci_id_table);
0505 
0506 static void virtio_pci_release_dev(struct device *_d)
0507 {
0508     struct virtio_device *vdev = dev_to_virtio(_d);
0509     struct virtio_pci_device *vp_dev = to_vp_device(vdev);
0510 
0511     /* As struct device is a kobject, it's not safe to
0512      * free the memory (including the reference counter itself)
0513      * until it's release callback. */
0514     kfree(vp_dev);
0515 }
0516 
0517 static int virtio_pci_probe(struct pci_dev *pci_dev,
0518                 const struct pci_device_id *id)
0519 {
0520     struct virtio_pci_device *vp_dev, *reg_dev = NULL;
0521     int rc;
0522 
0523     /* allocate our structure and fill it out */
0524     vp_dev = kzalloc(sizeof(struct virtio_pci_device), GFP_KERNEL);
0525     if (!vp_dev)
0526         return -ENOMEM;
0527 
0528     pci_set_drvdata(pci_dev, vp_dev);
0529     vp_dev->vdev.dev.parent = &pci_dev->dev;
0530     vp_dev->vdev.dev.release = virtio_pci_release_dev;
0531     vp_dev->pci_dev = pci_dev;
0532     INIT_LIST_HEAD(&vp_dev->virtqueues);
0533     spin_lock_init(&vp_dev->lock);
0534 
0535     /* enable the device */
0536     rc = pci_enable_device(pci_dev);
0537     if (rc)
0538         goto err_enable_device;
0539 
0540     if (force_legacy) {
0541         rc = virtio_pci_legacy_probe(vp_dev);
0542         /* Also try modern mode if we can't map BAR0 (no IO space). */
0543         if (rc == -ENODEV || rc == -ENOMEM)
0544             rc = virtio_pci_modern_probe(vp_dev);
0545         if (rc)
0546             goto err_probe;
0547     } else {
0548         rc = virtio_pci_modern_probe(vp_dev);
0549         if (rc == -ENODEV)
0550             rc = virtio_pci_legacy_probe(vp_dev);
0551         if (rc)
0552             goto err_probe;
0553     }
0554 
0555     pci_set_master(pci_dev);
0556 
0557     vp_dev->is_legacy = vp_dev->ldev.ioaddr ? true : false;
0558 
0559     rc = register_virtio_device(&vp_dev->vdev);
0560     reg_dev = vp_dev;
0561     if (rc)
0562         goto err_register;
0563 
0564     return 0;
0565 
0566 err_register:
0567     if (vp_dev->is_legacy)
0568         virtio_pci_legacy_remove(vp_dev);
0569     else
0570         virtio_pci_modern_remove(vp_dev);
0571 err_probe:
0572     pci_disable_device(pci_dev);
0573 err_enable_device:
0574     if (reg_dev)
0575         put_device(&vp_dev->vdev.dev);
0576     else
0577         kfree(vp_dev);
0578     return rc;
0579 }
0580 
0581 static void virtio_pci_remove(struct pci_dev *pci_dev)
0582 {
0583     struct virtio_pci_device *vp_dev = pci_get_drvdata(pci_dev);
0584     struct device *dev = get_device(&vp_dev->vdev.dev);
0585 
0586     /*
0587      * Device is marked broken on surprise removal so that virtio upper
0588      * layers can abort any ongoing operation.
0589      */
0590     if (!pci_device_is_present(pci_dev))
0591         virtio_break_device(&vp_dev->vdev);
0592 
0593     pci_disable_sriov(pci_dev);
0594 
0595     unregister_virtio_device(&vp_dev->vdev);
0596 
0597     if (vp_dev->is_legacy)
0598         virtio_pci_legacy_remove(vp_dev);
0599     else
0600         virtio_pci_modern_remove(vp_dev);
0601 
0602     pci_disable_device(pci_dev);
0603     put_device(dev);
0604 }
0605 
0606 static int virtio_pci_sriov_configure(struct pci_dev *pci_dev, int num_vfs)
0607 {
0608     struct virtio_pci_device *vp_dev = pci_get_drvdata(pci_dev);
0609     struct virtio_device *vdev = &vp_dev->vdev;
0610     int ret;
0611 
0612     if (!(vdev->config->get_status(vdev) & VIRTIO_CONFIG_S_DRIVER_OK))
0613         return -EBUSY;
0614 
0615     if (!__virtio_test_bit(vdev, VIRTIO_F_SR_IOV))
0616         return -EINVAL;
0617 
0618     if (pci_vfs_assigned(pci_dev))
0619         return -EPERM;
0620 
0621     if (num_vfs == 0) {
0622         pci_disable_sriov(pci_dev);
0623         return 0;
0624     }
0625 
0626     ret = pci_enable_sriov(pci_dev, num_vfs);
0627     if (ret < 0)
0628         return ret;
0629 
0630     return num_vfs;
0631 }
0632 
0633 static struct pci_driver virtio_pci_driver = {
0634     .name       = "virtio-pci",
0635     .id_table   = virtio_pci_id_table,
0636     .probe      = virtio_pci_probe,
0637     .remove     = virtio_pci_remove,
0638 #ifdef CONFIG_PM_SLEEP
0639     .driver.pm  = &virtio_pci_pm_ops,
0640 #endif
0641     .sriov_configure = virtio_pci_sriov_configure,
0642 };
0643 
0644 module_pci_driver(virtio_pci_driver);
0645 
0646 MODULE_AUTHOR("Anthony Liguori <aliguori@us.ibm.com>");
0647 MODULE_DESCRIPTION("virtio-pci");
0648 MODULE_LICENSE("GPL");
0649 MODULE_VERSION("1");