xref: /openbmc/linux/drivers/virtio/virtio_pci_common.c (revision b181f7029bd71238ac2754ce7052dffd69432085)
1f33f5fe2SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
25f4c9760SMichael S. Tsirkin /*
35f4c9760SMichael S. Tsirkin  * Virtio PCI driver - common functionality for all device versions
45f4c9760SMichael S. Tsirkin  *
55f4c9760SMichael S. Tsirkin  * This module allows virtio devices to be used over a virtual PCI device.
65f4c9760SMichael S. Tsirkin  * This can be used with QEMU based VMMs like KVM or Xen.
75f4c9760SMichael S. Tsirkin  *
85f4c9760SMichael S. Tsirkin  * Copyright IBM Corp. 2007
95f4c9760SMichael S. Tsirkin  * Copyright Red Hat, Inc. 2014
105f4c9760SMichael S. Tsirkin  *
115f4c9760SMichael S. Tsirkin  * Authors:
125f4c9760SMichael S. Tsirkin  *  Anthony Liguori  <aliguori@us.ibm.com>
135f4c9760SMichael S. Tsirkin  *  Rusty Russell <rusty@rustcorp.com.au>
145f4c9760SMichael S. Tsirkin  *  Michael S. Tsirkin <mst@redhat.com>
155f4c9760SMichael S. Tsirkin  */
165f4c9760SMichael S. Tsirkin 
175f4c9760SMichael S. Tsirkin #include "virtio_pci_common.h"
185f4c9760SMichael S. Tsirkin 
19ac399d8fSMichael S. Tsirkin static bool force_legacy = false;
20ac399d8fSMichael S. Tsirkin 
21ac399d8fSMichael S. Tsirkin #if IS_ENABLED(CONFIG_VIRTIO_PCI_LEGACY)
22ac399d8fSMichael S. Tsirkin module_param(force_legacy, bool, 0444);
23ac399d8fSMichael S. Tsirkin MODULE_PARM_DESC(force_legacy,
24ac399d8fSMichael S. Tsirkin 		 "Force legacy mode for transitional virtio 1 devices");
25ac399d8fSMichael S. Tsirkin #endif
26ac399d8fSMichael S. Tsirkin 
27eb4cecb4SJason Wang /* wait for pending irq handlers */
vp_synchronize_vectors(struct virtio_device * vdev)28eb4cecb4SJason Wang void vp_synchronize_vectors(struct virtio_device *vdev)
295f4c9760SMichael S. Tsirkin {
305f4c9760SMichael S. Tsirkin 	struct virtio_pci_device *vp_dev = to_vp_device(vdev);
315f4c9760SMichael S. Tsirkin 	int i;
325f4c9760SMichael S. Tsirkin 
337b79edfbSJason Wang 	if (vp_dev->intx_enabled)
340b0f9dc5SMichael S. Tsirkin 		synchronize_irq(vp_dev->pci_dev->irq);
350b0f9dc5SMichael S. Tsirkin 
360b0f9dc5SMichael S. Tsirkin 	for (i = 0; i < vp_dev->msix_vectors; ++i)
37eb4cecb4SJason Wang 		synchronize_irq(pci_irq_vector(vp_dev->pci_dev, i));
385f4c9760SMichael S. Tsirkin }
395f4c9760SMichael S. Tsirkin 
405f4c9760SMichael S. Tsirkin /* the notify function used when creating a virt queue */
vp_notify(struct virtqueue * vq)415f4c9760SMichael S. Tsirkin bool vp_notify(struct virtqueue *vq)
425f4c9760SMichael S. Tsirkin {
435f4c9760SMichael S. Tsirkin 	/* we write the queue's selector into the notification register to
445f4c9760SMichael S. Tsirkin 	 * signal the other end */
455f4c9760SMichael S. Tsirkin 	iowrite16(vq->index, (void __iomem *)vq->priv);
465f4c9760SMichael S. Tsirkin 	return true;
475f4c9760SMichael S. Tsirkin }
485f4c9760SMichael S. Tsirkin 
495f4c9760SMichael S. Tsirkin /* Handle a configuration change: Tell driver if it wants to know. */
vp_config_changed(int irq,void * opaque)505f4c9760SMichael S. Tsirkin static irqreturn_t vp_config_changed(int irq, void *opaque)
515f4c9760SMichael S. Tsirkin {
525f4c9760SMichael S. Tsirkin 	struct virtio_pci_device *vp_dev = opaque;
535f4c9760SMichael S. Tsirkin 
545f4c9760SMichael S. Tsirkin 	virtio_config_changed(&vp_dev->vdev);
555f4c9760SMichael S. Tsirkin 	return IRQ_HANDLED;
565f4c9760SMichael S. Tsirkin }
575f4c9760SMichael S. Tsirkin 
585f4c9760SMichael S. Tsirkin /* Notify all virtqueues on an interrupt. */
vp_vring_interrupt(int irq,void * opaque)595f4c9760SMichael S. Tsirkin static irqreturn_t vp_vring_interrupt(int irq, void *opaque)
605f4c9760SMichael S. Tsirkin {
615f4c9760SMichael S. Tsirkin 	struct virtio_pci_device *vp_dev = opaque;
620a9b3f47SMichael S. Tsirkin 	struct virtio_pci_vq_info *info;
635f4c9760SMichael S. Tsirkin 	irqreturn_t ret = IRQ_NONE;
640a9b3f47SMichael S. Tsirkin 	unsigned long flags;
655f4c9760SMichael S. Tsirkin 
660a9b3f47SMichael S. Tsirkin 	spin_lock_irqsave(&vp_dev->lock, flags);
670a9b3f47SMichael S. Tsirkin 	list_for_each_entry(info, &vp_dev->virtqueues, node) {
680a9b3f47SMichael S. Tsirkin 		if (vring_interrupt(irq, info->vq) == IRQ_HANDLED)
695f4c9760SMichael S. Tsirkin 			ret = IRQ_HANDLED;
705f4c9760SMichael S. Tsirkin 	}
710a9b3f47SMichael S. Tsirkin 	spin_unlock_irqrestore(&vp_dev->lock, flags);
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. */
vp_interrupt(int irq,void * opaque)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 
vp_request_msix_vectors(struct virtio_device * vdev,int nvectors,bool per_vq_vectors,struct irq_affinity * desc)1020b0f9dc5SMichael S. Tsirkin static int vp_request_msix_vectors(struct virtio_device *vdev, int nvectors,
1030b0f9dc5SMichael S. Tsirkin 				   bool per_vq_vectors, struct irq_affinity *desc)
1040b0f9dc5SMichael S. Tsirkin {
1050b0f9dc5SMichael S. Tsirkin 	struct virtio_pci_device *vp_dev = to_vp_device(vdev);
1060b0f9dc5SMichael S. Tsirkin 	const char *name = dev_name(&vp_dev->vdev.dev);
10731532340SSolomon Tan 	unsigned int flags = PCI_IRQ_MSIX;
10831532340SSolomon Tan 	unsigned int i, v;
1090b0f9dc5SMichael S. Tsirkin 	int err = -ENOMEM;
1100b0f9dc5SMichael S. Tsirkin 
1110b0f9dc5SMichael S. Tsirkin 	vp_dev->msix_vectors = nvectors;
1120b0f9dc5SMichael S. Tsirkin 
1136da2ec56SKees Cook 	vp_dev->msix_names = kmalloc_array(nvectors,
1146da2ec56SKees Cook 					   sizeof(*vp_dev->msix_names),
1150b0f9dc5SMichael S. Tsirkin 					   GFP_KERNEL);
1160b0f9dc5SMichael S. Tsirkin 	if (!vp_dev->msix_names)
1170b0f9dc5SMichael S. Tsirkin 		goto error;
1180b0f9dc5SMichael S. Tsirkin 	vp_dev->msix_affinity_masks
1196396bb22SKees Cook 		= kcalloc(nvectors, sizeof(*vp_dev->msix_affinity_masks),
1200b0f9dc5SMichael S. Tsirkin 			  GFP_KERNEL);
1210b0f9dc5SMichael S. Tsirkin 	if (!vp_dev->msix_affinity_masks)
1220b0f9dc5SMichael S. Tsirkin 		goto error;
1230b0f9dc5SMichael S. Tsirkin 	for (i = 0; i < nvectors; ++i)
1240b0f9dc5SMichael S. Tsirkin 		if (!alloc_cpumask_var(&vp_dev->msix_affinity_masks[i],
1250b0f9dc5SMichael S. Tsirkin 					GFP_KERNEL))
1260b0f9dc5SMichael S. Tsirkin 			goto error;
1270b0f9dc5SMichael S. Tsirkin 
128ba74b6f7SChristoph Hellwig 	if (desc) {
129ba74b6f7SChristoph Hellwig 		flags |= PCI_IRQ_AFFINITY;
130ba74b6f7SChristoph Hellwig 		desc->pre_vectors++; /* virtio config vector */
131ba74b6f7SChristoph Hellwig 	}
132ba74b6f7SChristoph Hellwig 
1330b0f9dc5SMichael S. Tsirkin 	err = pci_alloc_irq_vectors_affinity(vp_dev->pci_dev, nvectors,
134ba74b6f7SChristoph Hellwig 					     nvectors, flags, desc);
1350b0f9dc5SMichael S. Tsirkin 	if (err < 0)
1360b0f9dc5SMichael S. Tsirkin 		goto error;
1370b0f9dc5SMichael S. Tsirkin 	vp_dev->msix_enabled = 1;
1380b0f9dc5SMichael S. Tsirkin 
1390b0f9dc5SMichael S. Tsirkin 	/* Set the vector used for configuration */
1400b0f9dc5SMichael S. Tsirkin 	v = vp_dev->msix_used_vectors;
1410b0f9dc5SMichael S. Tsirkin 	snprintf(vp_dev->msix_names[v], sizeof *vp_dev->msix_names,
1420b0f9dc5SMichael S. Tsirkin 		 "%s-config", name);
1430b0f9dc5SMichael S. Tsirkin 	err = request_irq(pci_irq_vector(vp_dev->pci_dev, v),
144eb4cecb4SJason Wang 			  vp_config_changed, 0, vp_dev->msix_names[v],
1450b0f9dc5SMichael S. Tsirkin 			  vp_dev);
1460b0f9dc5SMichael S. Tsirkin 	if (err)
1470b0f9dc5SMichael S. Tsirkin 		goto error;
1480b0f9dc5SMichael S. Tsirkin 	++vp_dev->msix_used_vectors;
1490b0f9dc5SMichael S. Tsirkin 
1500b0f9dc5SMichael S. Tsirkin 	v = vp_dev->config_vector(vp_dev, v);
1510b0f9dc5SMichael S. Tsirkin 	/* Verify we had enough resources to assign the vector */
1520b0f9dc5SMichael S. Tsirkin 	if (v == VIRTIO_MSI_NO_VECTOR) {
1530b0f9dc5SMichael S. Tsirkin 		err = -EBUSY;
1540b0f9dc5SMichael S. Tsirkin 		goto error;
1550b0f9dc5SMichael S. Tsirkin 	}
1560b0f9dc5SMichael S. Tsirkin 
1570b0f9dc5SMichael S. Tsirkin 	if (!per_vq_vectors) {
1580b0f9dc5SMichael S. Tsirkin 		/* Shared vector for all VQs */
1590b0f9dc5SMichael S. Tsirkin 		v = vp_dev->msix_used_vectors;
1600b0f9dc5SMichael S. Tsirkin 		snprintf(vp_dev->msix_names[v], sizeof *vp_dev->msix_names,
1610b0f9dc5SMichael S. Tsirkin 			 "%s-virtqueues", name);
1620b0f9dc5SMichael S. Tsirkin 		err = request_irq(pci_irq_vector(vp_dev->pci_dev, v),
163eb4cecb4SJason Wang 				  vp_vring_interrupt, 0, vp_dev->msix_names[v],
1640b0f9dc5SMichael S. Tsirkin 				  vp_dev);
1650b0f9dc5SMichael S. Tsirkin 		if (err)
1660b0f9dc5SMichael S. Tsirkin 			goto error;
1670b0f9dc5SMichael S. Tsirkin 		++vp_dev->msix_used_vectors;
1680b0f9dc5SMichael S. Tsirkin 	}
1690b0f9dc5SMichael S. Tsirkin 	return 0;
1700b0f9dc5SMichael S. Tsirkin error:
1710b0f9dc5SMichael S. Tsirkin 	return err;
1720b0f9dc5SMichael S. Tsirkin }
1730b0f9dc5SMichael S. Tsirkin 
vp_setup_vq(struct virtio_device * vdev,unsigned int index,void (* callback)(struct virtqueue * vq),const char * name,bool ctx,u16 msix_vec)17431532340SSolomon Tan static struct virtqueue *vp_setup_vq(struct virtio_device *vdev, unsigned int index,
1750a9b3f47SMichael S. Tsirkin 				     void (*callback)(struct virtqueue *vq),
1760a9b3f47SMichael S. Tsirkin 				     const char *name,
177f94682ddSMichael S. Tsirkin 				     bool ctx,
1780a9b3f47SMichael S. Tsirkin 				     u16 msix_vec)
1790a9b3f47SMichael S. Tsirkin {
1800a9b3f47SMichael S. Tsirkin 	struct virtio_pci_device *vp_dev = to_vp_device(vdev);
1810a9b3f47SMichael S. Tsirkin 	struct virtio_pci_vq_info *info = kmalloc(sizeof *info, GFP_KERNEL);
1820a9b3f47SMichael S. Tsirkin 	struct virtqueue *vq;
1830a9b3f47SMichael S. Tsirkin 	unsigned long flags;
1840a9b3f47SMichael S. Tsirkin 
1850a9b3f47SMichael S. Tsirkin 	/* fill out our structure that represents an active queue */
1860a9b3f47SMichael S. Tsirkin 	if (!info)
1870a9b3f47SMichael S. Tsirkin 		return ERR_PTR(-ENOMEM);
1880a9b3f47SMichael S. Tsirkin 
18913aa8c6cSMichael S. Tsirkin 	vq = vp_dev->setup_vq(vp_dev, info, index, callback, name, ctx,
1900a9b3f47SMichael S. Tsirkin 			      msix_vec);
1910a9b3f47SMichael S. Tsirkin 	if (IS_ERR(vq))
1920a9b3f47SMichael S. Tsirkin 		goto out_info;
1930a9b3f47SMichael S. Tsirkin 
1940a9b3f47SMichael S. Tsirkin 	info->vq = vq;
1950a9b3f47SMichael S. Tsirkin 	if (callback) {
1960a9b3f47SMichael S. Tsirkin 		spin_lock_irqsave(&vp_dev->lock, flags);
1970a9b3f47SMichael S. Tsirkin 		list_add(&info->node, &vp_dev->virtqueues);
1980a9b3f47SMichael S. Tsirkin 		spin_unlock_irqrestore(&vp_dev->lock, flags);
1990a9b3f47SMichael S. Tsirkin 	} else {
2000a9b3f47SMichael S. Tsirkin 		INIT_LIST_HEAD(&info->node);
2010a9b3f47SMichael S. Tsirkin 	}
2020a9b3f47SMichael S. Tsirkin 
2030a9b3f47SMichael S. Tsirkin 	vp_dev->vqs[index] = info;
2040a9b3f47SMichael S. Tsirkin 	return vq;
2050a9b3f47SMichael S. Tsirkin 
2060a9b3f47SMichael S. Tsirkin out_info:
2070a9b3f47SMichael S. Tsirkin 	kfree(info);
2080a9b3f47SMichael S. Tsirkin 	return vq;
2090a9b3f47SMichael S. Tsirkin }
2100a9b3f47SMichael S. Tsirkin 
vp_del_vq(struct virtqueue * vq)2110a9b3f47SMichael S. Tsirkin static void vp_del_vq(struct virtqueue *vq)
2120a9b3f47SMichael S. Tsirkin {
2130a9b3f47SMichael S. Tsirkin 	struct virtio_pci_device *vp_dev = to_vp_device(vq->vdev);
2140a9b3f47SMichael S. Tsirkin 	struct virtio_pci_vq_info *info = vp_dev->vqs[vq->index];
2150a9b3f47SMichael S. Tsirkin 	unsigned long flags;
2160a9b3f47SMichael S. Tsirkin 
21704ca0b0bSXuan Zhuo 	/*
21804ca0b0bSXuan Zhuo 	 * If it fails during re-enable reset vq. This way we won't rejoin
21904ca0b0bSXuan Zhuo 	 * info->node to the queue. Prevent unexpected irqs.
22004ca0b0bSXuan Zhuo 	 */
22104ca0b0bSXuan Zhuo 	if (!vq->reset) {
2220a9b3f47SMichael S. Tsirkin 		spin_lock_irqsave(&vp_dev->lock, flags);
2230a9b3f47SMichael S. Tsirkin 		list_del(&info->node);
2240a9b3f47SMichael S. Tsirkin 		spin_unlock_irqrestore(&vp_dev->lock, flags);
22504ca0b0bSXuan Zhuo 	}
2260a9b3f47SMichael S. Tsirkin 
2270a9b3f47SMichael S. Tsirkin 	vp_dev->del_vq(info);
2280a9b3f47SMichael S. Tsirkin 	kfree(info);
2290a9b3f47SMichael S. Tsirkin }
2300a9b3f47SMichael S. Tsirkin 
2310b0f9dc5SMichael S. Tsirkin /* the config->del_vqs() implementation */
vp_del_vqs(struct virtio_device * vdev)2320b0f9dc5SMichael S. Tsirkin void vp_del_vqs(struct virtio_device *vdev)
2335f4c9760SMichael S. Tsirkin {
2345f4c9760SMichael S. Tsirkin 	struct virtio_pci_device *vp_dev = to_vp_device(vdev);
2355f4c9760SMichael S. Tsirkin 	struct virtqueue *vq, *n;
2360b0f9dc5SMichael S. Tsirkin 	int i;
2375f4c9760SMichael S. Tsirkin 
2385f4c9760SMichael S. Tsirkin 	list_for_each_entry_safe(vq, n, &vdev->vqs, list) {
2390a9b3f47SMichael S. Tsirkin 		if (vp_dev->per_vq_vectors) {
2400a9b3f47SMichael S. Tsirkin 			int v = vp_dev->vqs[vq->index]->msix_vector;
241fa3a3279SChristoph Hellwig 
2422f8dc3a0SMarc Zyngier 			if (v != VIRTIO_MSI_NO_VECTOR) {
2432f8dc3a0SMarc Zyngier 				int irq = pci_irq_vector(vp_dev->pci_dev, v);
2442f8dc3a0SMarc Zyngier 
2452f8dc3a0SMarc Zyngier 				irq_set_affinity_hint(irq, NULL);
2462f8dc3a0SMarc Zyngier 				free_irq(irq, vq);
2472f8dc3a0SMarc Zyngier 			}
248fa3a3279SChristoph Hellwig 		}
2490a9b3f47SMichael S. Tsirkin 		vp_del_vq(vq);
2505f4c9760SMichael S. Tsirkin 	}
2510a9b3f47SMichael S. Tsirkin 	vp_dev->per_vq_vectors = false;
2520b0f9dc5SMichael S. Tsirkin 
2530b0f9dc5SMichael S. Tsirkin 	if (vp_dev->intx_enabled) {
2540b0f9dc5SMichael S. Tsirkin 		free_irq(vp_dev->pci_dev->irq, vp_dev);
2550b0f9dc5SMichael S. Tsirkin 		vp_dev->intx_enabled = 0;
25666f2f555SChristoph Hellwig 	}
25766f2f555SChristoph Hellwig 
2580b0f9dc5SMichael S. Tsirkin 	for (i = 0; i < vp_dev->msix_used_vectors; ++i)
2590b0f9dc5SMichael S. Tsirkin 		free_irq(pci_irq_vector(vp_dev->pci_dev, i), vp_dev);
26066f2f555SChristoph Hellwig 
2616a8aae68SLongpeng 	if (vp_dev->msix_affinity_masks) {
26207ec5148SChristoph Hellwig 		for (i = 0; i < vp_dev->msix_vectors; i++)
26307ec5148SChristoph Hellwig 			free_cpumask_var(vp_dev->msix_affinity_masks[i]);
2646a8aae68SLongpeng 	}
26507ec5148SChristoph Hellwig 
2660b0f9dc5SMichael S. Tsirkin 	if (vp_dev->msix_enabled) {
26766f2f555SChristoph Hellwig 		/* Disable the vector used for configuration */
26866f2f555SChristoph Hellwig 		vp_dev->config_vector(vp_dev, VIRTIO_MSI_NO_VECTOR);
26966f2f555SChristoph Hellwig 
2700b0f9dc5SMichael S. Tsirkin 		pci_free_irq_vectors(vp_dev->pci_dev);
2710b0f9dc5SMichael S. Tsirkin 		vp_dev->msix_enabled = 0;
27266f2f555SChristoph Hellwig 	}
27366f2f555SChristoph Hellwig 
2740b0f9dc5SMichael S. Tsirkin 	vp_dev->msix_vectors = 0;
2750b0f9dc5SMichael S. Tsirkin 	vp_dev->msix_used_vectors = 0;
2760b0f9dc5SMichael S. Tsirkin 	kfree(vp_dev->msix_names);
2770b0f9dc5SMichael S. Tsirkin 	vp_dev->msix_names = NULL;
2780b0f9dc5SMichael S. Tsirkin 	kfree(vp_dev->msix_affinity_masks);
2790b0f9dc5SMichael S. Tsirkin 	vp_dev->msix_affinity_masks = NULL;
2800a9b3f47SMichael S. Tsirkin 	kfree(vp_dev->vqs);
2810a9b3f47SMichael S. Tsirkin 	vp_dev->vqs = NULL;
2825f4c9760SMichael S. Tsirkin }
2835f4c9760SMichael S. Tsirkin 
vp_find_vqs_msix(struct virtio_device * vdev,unsigned int nvqs,struct virtqueue * vqs[],vq_callback_t * callbacks[],const char * const names[],bool per_vq_vectors,const bool * ctx,struct irq_affinity * desc)28431532340SSolomon Tan static int vp_find_vqs_msix(struct virtio_device *vdev, unsigned int nvqs,
28552a61516SChristoph Hellwig 		struct virtqueue *vqs[], vq_callback_t *callbacks[],
28613aa8c6cSMichael S. Tsirkin 		const char * const names[], bool per_vq_vectors,
287f94682ddSMichael S. Tsirkin 		const bool *ctx,
288bf951b10SMichael S. Tsirkin 		struct irq_affinity *desc)
2895f4c9760SMichael S. Tsirkin {
2905f4c9760SMichael S. Tsirkin 	struct virtio_pci_device *vp_dev = to_vp_device(vdev);
2915f4c9760SMichael S. Tsirkin 	u16 msix_vec;
292ddbeac07SWei Wang 	int i, err, nvectors, allocated_vectors, queue_idx = 0;
2935f4c9760SMichael S. Tsirkin 
2940a9b3f47SMichael S. Tsirkin 	vp_dev->vqs = kcalloc(nvqs, sizeof(*vp_dev->vqs), GFP_KERNEL);
2950a9b3f47SMichael S. Tsirkin 	if (!vp_dev->vqs)
2960a9b3f47SMichael S. Tsirkin 		return -ENOMEM;
2970a9b3f47SMichael S. Tsirkin 
298bf951b10SMichael S. Tsirkin 	if (per_vq_vectors) {
2990b0f9dc5SMichael S. Tsirkin 		/* Best option: one for change interrupt, one per vq. */
3000b0f9dc5SMichael S. Tsirkin 		nvectors = 1;
3010b0f9dc5SMichael S. Tsirkin 		for (i = 0; i < nvqs; ++i)
302303090b5SDaniel Verkamp 			if (names[i] && callbacks[i])
3030b0f9dc5SMichael S. Tsirkin 				++nvectors;
304bf951b10SMichael S. Tsirkin 	} else {
3050b0f9dc5SMichael S. Tsirkin 		/* Second best: one for change, shared for all vqs. */
3060b0f9dc5SMichael S. Tsirkin 		nvectors = 2;
3075f4c9760SMichael S. Tsirkin 	}
3085f4c9760SMichael S. Tsirkin 
3090b0f9dc5SMichael S. Tsirkin 	err = vp_request_msix_vectors(vdev, nvectors, per_vq_vectors,
3100b0f9dc5SMichael S. Tsirkin 				      per_vq_vectors ? desc : NULL);
3115f4c9760SMichael S. Tsirkin 	if (err)
3120b0f9dc5SMichael S. Tsirkin 		goto error_find;
3135f4c9760SMichael S. Tsirkin 
3140a9b3f47SMichael S. Tsirkin 	vp_dev->per_vq_vectors = per_vq_vectors;
3150b0f9dc5SMichael S. Tsirkin 	allocated_vectors = vp_dev->msix_used_vectors;
3165f4c9760SMichael S. Tsirkin 	for (i = 0; i < nvqs; ++i) {
3175f4c9760SMichael S. Tsirkin 		if (!names[i]) {
3185f4c9760SMichael S. Tsirkin 			vqs[i] = NULL;
3195f4c9760SMichael S. Tsirkin 			continue;
320a3cbec69SChristoph Hellwig 		}
321a3cbec69SChristoph Hellwig 
3220b0f9dc5SMichael S. Tsirkin 		if (!callbacks[i])
32307ec5148SChristoph Hellwig 			msix_vec = VIRTIO_MSI_NO_VECTOR;
3240a9b3f47SMichael S. Tsirkin 		else if (vp_dev->per_vq_vectors)
3250b0f9dc5SMichael S. Tsirkin 			msix_vec = allocated_vectors++;
3260b0f9dc5SMichael S. Tsirkin 		else
3270b0f9dc5SMichael S. Tsirkin 			msix_vec = VP_MSIX_VQ_VECTOR;
328ddbeac07SWei Wang 		vqs[i] = vp_setup_vq(vdev, queue_idx++, callbacks[i], names[i],
32913aa8c6cSMichael S. Tsirkin 				     ctx ? ctx[i] : false,
33013aa8c6cSMichael S. Tsirkin 				     msix_vec);
3315f4c9760SMichael S. Tsirkin 		if (IS_ERR(vqs[i])) {
3325f4c9760SMichael S. Tsirkin 			err = PTR_ERR(vqs[i]);
3330b0f9dc5SMichael S. Tsirkin 			goto error_find;
3345f4c9760SMichael S. Tsirkin 		}
3355f4c9760SMichael S. Tsirkin 
3360a9b3f47SMichael S. Tsirkin 		if (!vp_dev->per_vq_vectors || msix_vec == VIRTIO_MSI_NO_VECTOR)
3370b0f9dc5SMichael S. Tsirkin 			continue;
3380b0f9dc5SMichael S. Tsirkin 
3390b0f9dc5SMichael S. Tsirkin 		/* allocate per-vq irq if available and necessary */
3400b0f9dc5SMichael S. Tsirkin 		snprintf(vp_dev->msix_names[msix_vec],
3410b0f9dc5SMichael S. Tsirkin 			 sizeof *vp_dev->msix_names,
3420b0f9dc5SMichael S. Tsirkin 			 "%s-%s",
3435f4c9760SMichael S. Tsirkin 			 dev_name(&vp_dev->vdev.dev), names[i]);
344fa3a3279SChristoph Hellwig 		err = request_irq(pci_irq_vector(vp_dev->pci_dev, msix_vec),
345eb4cecb4SJason Wang 				  vring_interrupt, 0,
3460b0f9dc5SMichael S. Tsirkin 				  vp_dev->msix_names[msix_vec],
3470b0f9dc5SMichael S. Tsirkin 				  vqs[i]);
348*04207a9cSJiri Pirko 		if (err) {
349*04207a9cSJiri Pirko 			vp_del_vq(vqs[i]);
3500b0f9dc5SMichael S. Tsirkin 			goto error_find;
3515f4c9760SMichael S. Tsirkin 		}
352*04207a9cSJiri Pirko 	}
3535f4c9760SMichael S. Tsirkin 	return 0;
3545f4c9760SMichael S. Tsirkin 
3550b0f9dc5SMichael S. Tsirkin error_find:
3560b0f9dc5SMichael S. Tsirkin 	vp_del_vqs(vdev);
3575f4c9760SMichael S. Tsirkin 	return err;
3585f4c9760SMichael S. Tsirkin }
3595f4c9760SMichael S. Tsirkin 
vp_find_vqs_intx(struct virtio_device * vdev,unsigned int nvqs,struct virtqueue * vqs[],vq_callback_t * callbacks[],const char * const names[],const bool * ctx)36031532340SSolomon Tan static int vp_find_vqs_intx(struct virtio_device *vdev, unsigned int nvqs,
361a3cbec69SChristoph Hellwig 		struct virtqueue *vqs[], vq_callback_t *callbacks[],
36213aa8c6cSMichael S. Tsirkin 		const char * const names[], const bool *ctx)
363a3cbec69SChristoph Hellwig {
364a3cbec69SChristoph Hellwig 	struct virtio_pci_device *vp_dev = to_vp_device(vdev);
365ddbeac07SWei Wang 	int i, err, queue_idx = 0;
366a3cbec69SChristoph Hellwig 
3670a9b3f47SMichael S. Tsirkin 	vp_dev->vqs = kcalloc(nvqs, sizeof(*vp_dev->vqs), GFP_KERNEL);
3680a9b3f47SMichael S. Tsirkin 	if (!vp_dev->vqs)
3690a9b3f47SMichael S. Tsirkin 		return -ENOMEM;
3700a9b3f47SMichael S. Tsirkin 
371a3cbec69SChristoph Hellwig 	err = request_irq(vp_dev->pci_dev->irq, vp_interrupt, IRQF_SHARED,
372a3cbec69SChristoph Hellwig 			dev_name(&vdev->dev), vp_dev);
373a3cbec69SChristoph Hellwig 	if (err)
3740b0f9dc5SMichael S. Tsirkin 		goto out_del_vqs;
375a3cbec69SChristoph Hellwig 
3760b0f9dc5SMichael S. Tsirkin 	vp_dev->intx_enabled = 1;
3770a9b3f47SMichael S. Tsirkin 	vp_dev->per_vq_vectors = false;
378a3cbec69SChristoph Hellwig 	for (i = 0; i < nvqs; ++i) {
379a3cbec69SChristoph Hellwig 		if (!names[i]) {
380a3cbec69SChristoph Hellwig 			vqs[i] = NULL;
381a3cbec69SChristoph Hellwig 			continue;
382a3cbec69SChristoph Hellwig 		}
383ddbeac07SWei Wang 		vqs[i] = vp_setup_vq(vdev, queue_idx++, callbacks[i], names[i],
384f94682ddSMichael S. Tsirkin 				     ctx ? ctx[i] : false,
385a3cbec69SChristoph Hellwig 				     VIRTIO_MSI_NO_VECTOR);
386a3cbec69SChristoph Hellwig 		if (IS_ERR(vqs[i])) {
387a3cbec69SChristoph Hellwig 			err = PTR_ERR(vqs[i]);
3880b0f9dc5SMichael S. Tsirkin 			goto out_del_vqs;
389a3cbec69SChristoph Hellwig 		}
390a3cbec69SChristoph Hellwig 	}
391a3cbec69SChristoph Hellwig 
392a3cbec69SChristoph Hellwig 	return 0;
3930b0f9dc5SMichael S. Tsirkin out_del_vqs:
3940b0f9dc5SMichael S. Tsirkin 	vp_del_vqs(vdev);
395a3cbec69SChristoph Hellwig 	return err;
396a3cbec69SChristoph Hellwig }
397a3cbec69SChristoph Hellwig 
3985f4c9760SMichael S. Tsirkin /* the config->find_vqs() implementation */
vp_find_vqs(struct virtio_device * vdev,unsigned int nvqs,struct virtqueue * vqs[],vq_callback_t * callbacks[],const char * const names[],const bool * ctx,struct irq_affinity * desc)39931532340SSolomon Tan int vp_find_vqs(struct virtio_device *vdev, unsigned int nvqs,
400fb5e31d9SChristoph Hellwig 		struct virtqueue *vqs[], vq_callback_t *callbacks[],
4019993a4f9SMichael S. Tsirkin 		const char * const names[], const bool *ctx,
402f94682ddSMichael S. Tsirkin 		struct irq_affinity *desc)
4035f4c9760SMichael S. Tsirkin {
4045f4c9760SMichael S. Tsirkin 	int err;
4055f4c9760SMichael S. Tsirkin 
406bf951b10SMichael S. Tsirkin 	/* Try MSI-X with one vector per queue. */
40713aa8c6cSMichael S. Tsirkin 	err = vp_find_vqs_msix(vdev, nvqs, vqs, callbacks, names, true, ctx, desc);
4085f4c9760SMichael S. Tsirkin 	if (!err)
4095f4c9760SMichael S. Tsirkin 		return 0;
410bf951b10SMichael S. Tsirkin 	/* Fallback: MSI-X with one vector for config, one shared for queues. */
41113aa8c6cSMichael S. Tsirkin 	err = vp_find_vqs_msix(vdev, nvqs, vqs, callbacks, names, false, ctx, desc);
412bf951b10SMichael S. Tsirkin 	if (!err)
413bf951b10SMichael S. Tsirkin 		return 0;
4142145ab51SMichael S. Tsirkin 	/* Is there an interrupt? If not give up. */
4152145ab51SMichael S. Tsirkin 	if (!(to_vp_device(vdev)->pci_dev->irq))
41671491c54SAngus Chen 		return err;
417bf951b10SMichael S. Tsirkin 	/* Finally fall back to regular interrupts. */
41813aa8c6cSMichael S. Tsirkin 	return vp_find_vqs_intx(vdev, nvqs, vqs, callbacks, names, ctx);
4195f4c9760SMichael S. Tsirkin }
4205f4c9760SMichael S. Tsirkin 
vp_bus_name(struct virtio_device * vdev)4215f4c9760SMichael S. Tsirkin const char *vp_bus_name(struct virtio_device *vdev)
4225f4c9760SMichael S. Tsirkin {
4235f4c9760SMichael S. Tsirkin 	struct virtio_pci_device *vp_dev = to_vp_device(vdev);
4245f4c9760SMichael S. Tsirkin 
4255f4c9760SMichael S. Tsirkin 	return pci_name(vp_dev->pci_dev);
4265f4c9760SMichael S. Tsirkin }
4275f4c9760SMichael S. Tsirkin 
4285f4c9760SMichael S. Tsirkin /* Setup the affinity for a virtqueue:
4295f4c9760SMichael S. Tsirkin  * - force the affinity for per vq vector
4305f4c9760SMichael S. Tsirkin  * - OR over all affinities for shared MSI
4315f4c9760SMichael S. Tsirkin  * - ignore the affinity request if we're using INTX
4325f4c9760SMichael S. Tsirkin  */
vp_set_vq_affinity(struct virtqueue * vq,const struct cpumask * cpu_mask)43319e226e8SCaleb Raitto int vp_set_vq_affinity(struct virtqueue *vq, const struct cpumask *cpu_mask)
4345f4c9760SMichael S. Tsirkin {
4355f4c9760SMichael S. Tsirkin 	struct virtio_device *vdev = vq->vdev;
4365f4c9760SMichael S. Tsirkin 	struct virtio_pci_device *vp_dev = to_vp_device(vdev);
4370a9b3f47SMichael S. Tsirkin 	struct virtio_pci_vq_info *info = vp_dev->vqs[vq->index];
4380a9b3f47SMichael S. Tsirkin 	struct cpumask *mask;
4390a9b3f47SMichael S. Tsirkin 	unsigned int irq;
4405f4c9760SMichael S. Tsirkin 
4415f4c9760SMichael S. Tsirkin 	if (!vq->callback)
4425f4c9760SMichael S. Tsirkin 		return -EINVAL;
4435f4c9760SMichael S. Tsirkin 
4442008c154SMichael S. Tsirkin 	if (vp_dev->msix_enabled) {
4450a9b3f47SMichael S. Tsirkin 		mask = vp_dev->msix_affinity_masks[info->msix_vector];
4460a9b3f47SMichael S. Tsirkin 		irq = pci_irq_vector(vp_dev->pci_dev, info->msix_vector);
44719e226e8SCaleb Raitto 		if (!cpu_mask)
4485f4c9760SMichael S. Tsirkin 			irq_set_affinity_hint(irq, NULL);
4495f4c9760SMichael S. Tsirkin 		else {
45019e226e8SCaleb Raitto 			cpumask_copy(mask, cpu_mask);
4515f4c9760SMichael S. Tsirkin 			irq_set_affinity_hint(irq, mask);
4525f4c9760SMichael S. Tsirkin 		}
4535f4c9760SMichael S. Tsirkin 	}
4545f4c9760SMichael S. Tsirkin 	return 0;
4555f4c9760SMichael S. Tsirkin }
4565f4c9760SMichael S. Tsirkin 
vp_get_vq_affinity(struct virtio_device * vdev,int index)457bbaba479SChristoph Hellwig const struct cpumask *vp_get_vq_affinity(struct virtio_device *vdev, int index)
458bbaba479SChristoph Hellwig {
459bbaba479SChristoph Hellwig 	struct virtio_pci_device *vp_dev = to_vp_device(vdev);
460bbaba479SChristoph Hellwig 
4610a9b3f47SMichael S. Tsirkin 	if (!vp_dev->per_vq_vectors ||
4620a9b3f47SMichael S. Tsirkin 	    vp_dev->vqs[index]->msix_vector == VIRTIO_MSI_NO_VECTOR)
463bbaba479SChristoph Hellwig 		return NULL;
464bbaba479SChristoph Hellwig 
4650a9b3f47SMichael S. Tsirkin 	return pci_irq_get_affinity(vp_dev->pci_dev,
4660a9b3f47SMichael S. Tsirkin 				    vp_dev->vqs[index]->msix_vector);
467bbaba479SChristoph Hellwig }
468bbaba479SChristoph Hellwig 
4695f4c9760SMichael S. Tsirkin #ifdef CONFIG_PM_SLEEP
virtio_pci_freeze(struct device * dev)4705f4c9760SMichael S. Tsirkin static int virtio_pci_freeze(struct device *dev)
4715f4c9760SMichael S. Tsirkin {
4725f4c9760SMichael S. Tsirkin 	struct pci_dev *pci_dev = to_pci_dev(dev);
4735f4c9760SMichael S. Tsirkin 	struct virtio_pci_device *vp_dev = pci_get_drvdata(pci_dev);
4745f4c9760SMichael S. Tsirkin 	int ret;
4755f4c9760SMichael S. Tsirkin 
4765f4c9760SMichael S. Tsirkin 	ret = virtio_device_freeze(&vp_dev->vdev);
4775f4c9760SMichael S. Tsirkin 
4785f4c9760SMichael S. Tsirkin 	if (!ret)
4795f4c9760SMichael S. Tsirkin 		pci_disable_device(pci_dev);
4805f4c9760SMichael S. Tsirkin 	return ret;
4815f4c9760SMichael S. Tsirkin }
4825f4c9760SMichael S. Tsirkin 
virtio_pci_restore(struct device * dev)4835f4c9760SMichael S. Tsirkin static int virtio_pci_restore(struct device *dev)
4845f4c9760SMichael S. Tsirkin {
4855f4c9760SMichael S. Tsirkin 	struct pci_dev *pci_dev = to_pci_dev(dev);
4865f4c9760SMichael S. Tsirkin 	struct virtio_pci_device *vp_dev = pci_get_drvdata(pci_dev);
4875f4c9760SMichael S. Tsirkin 	int ret;
4885f4c9760SMichael S. Tsirkin 
4895f4c9760SMichael S. Tsirkin 	ret = pci_enable_device(pci_dev);
4905f4c9760SMichael S. Tsirkin 	if (ret)
4915f4c9760SMichael S. Tsirkin 		return ret;
4925f4c9760SMichael S. Tsirkin 
4935f4c9760SMichael S. Tsirkin 	pci_set_master(pci_dev);
4945f4c9760SMichael S. Tsirkin 	return virtio_device_restore(&vp_dev->vdev);
4955f4c9760SMichael S. Tsirkin }
4965f4c9760SMichael S. Tsirkin 
4979a4253dbSMichael S. Tsirkin static const struct dev_pm_ops virtio_pci_pm_ops = {
4985f4c9760SMichael S. Tsirkin 	SET_SYSTEM_SLEEP_PM_OPS(virtio_pci_freeze, virtio_pci_restore)
4995f4c9760SMichael S. Tsirkin };
5005f4c9760SMichael S. Tsirkin #endif
5019a4253dbSMichael S. Tsirkin 
5029a4253dbSMichael S. Tsirkin 
5039a4253dbSMichael S. Tsirkin /* Qumranet donated their vendor ID for devices 0x1000 thru 0x10FF. */
5049a4253dbSMichael S. Tsirkin static const struct pci_device_id virtio_pci_id_table[] = {
505caf02abfSRobin H. Johnson 	{ PCI_DEVICE(PCI_VENDOR_ID_REDHAT_QUMRANET, PCI_ANY_ID) },
5069a4253dbSMichael S. Tsirkin 	{ 0 }
5079a4253dbSMichael S. Tsirkin };
5089a4253dbSMichael S. Tsirkin 
5099a4253dbSMichael S. Tsirkin MODULE_DEVICE_TABLE(pci, virtio_pci_id_table);
5109a4253dbSMichael S. Tsirkin 
virtio_pci_release_dev(struct device * _d)511ff31d2e2SMichael S. Tsirkin static void virtio_pci_release_dev(struct device *_d)
512ff31d2e2SMichael S. Tsirkin {
513ff31d2e2SMichael S. Tsirkin 	struct virtio_device *vdev = dev_to_virtio(_d);
514ff31d2e2SMichael S. Tsirkin 	struct virtio_pci_device *vp_dev = to_vp_device(vdev);
515ff31d2e2SMichael S. Tsirkin 
516ff31d2e2SMichael S. Tsirkin 	/* As struct device is a kobject, it's not safe to
517ff31d2e2SMichael S. Tsirkin 	 * free the memory (including the reference counter itself)
518ff31d2e2SMichael S. Tsirkin 	 * until it's release callback. */
519ff31d2e2SMichael S. Tsirkin 	kfree(vp_dev);
520ff31d2e2SMichael S. Tsirkin }
521ff31d2e2SMichael S. Tsirkin 
virtio_pci_probe(struct pci_dev * pci_dev,const struct pci_device_id * id)5229a4253dbSMichael S. Tsirkin static int virtio_pci_probe(struct pci_dev *pci_dev,
5239a4253dbSMichael S. Tsirkin 			    const struct pci_device_id *id)
5249a4253dbSMichael S. Tsirkin {
52533635bd9Sweiping zhang 	struct virtio_pci_device *vp_dev, *reg_dev = NULL;
526ff31d2e2SMichael S. Tsirkin 	int rc;
527ff31d2e2SMichael S. Tsirkin 
528ff31d2e2SMichael S. Tsirkin 	/* allocate our structure and fill it out */
529ff31d2e2SMichael S. Tsirkin 	vp_dev = kzalloc(sizeof(struct virtio_pci_device), GFP_KERNEL);
530ff31d2e2SMichael S. Tsirkin 	if (!vp_dev)
531ff31d2e2SMichael S. Tsirkin 		return -ENOMEM;
532ff31d2e2SMichael S. Tsirkin 
533ff31d2e2SMichael S. Tsirkin 	pci_set_drvdata(pci_dev, vp_dev);
534ff31d2e2SMichael S. Tsirkin 	vp_dev->vdev.dev.parent = &pci_dev->dev;
535ff31d2e2SMichael S. Tsirkin 	vp_dev->vdev.dev.release = virtio_pci_release_dev;
536ff31d2e2SMichael S. Tsirkin 	vp_dev->pci_dev = pci_dev;
5370a9b3f47SMichael S. Tsirkin 	INIT_LIST_HEAD(&vp_dev->virtqueues);
5380a9b3f47SMichael S. Tsirkin 	spin_lock_init(&vp_dev->lock);
539ff31d2e2SMichael S. Tsirkin 
540ff31d2e2SMichael S. Tsirkin 	/* enable the device */
541ff31d2e2SMichael S. Tsirkin 	rc = pci_enable_device(pci_dev);
542ff31d2e2SMichael S. Tsirkin 	if (rc)
543ff31d2e2SMichael S. Tsirkin 		goto err_enable_device;
544ff31d2e2SMichael S. Tsirkin 
545ac399d8fSMichael S. Tsirkin 	if (force_legacy) {
546ac399d8fSMichael S. Tsirkin 		rc = virtio_pci_legacy_probe(vp_dev);
547ac399d8fSMichael S. Tsirkin 		/* Also try modern mode if we can't map BAR0 (no IO space). */
548ac399d8fSMichael S. Tsirkin 		if (rc == -ENODEV || rc == -ENOMEM)
549ac399d8fSMichael S. Tsirkin 			rc = virtio_pci_modern_probe(vp_dev);
550ac399d8fSMichael S. Tsirkin 		if (rc)
551ac399d8fSMichael S. Tsirkin 			goto err_probe;
552ac399d8fSMichael S. Tsirkin 	} else {
5531fcf0512SMichael S. Tsirkin 		rc = virtio_pci_modern_probe(vp_dev);
5541fcf0512SMichael S. Tsirkin 		if (rc == -ENODEV)
555ff31d2e2SMichael S. Tsirkin 			rc = virtio_pci_legacy_probe(vp_dev);
556ff31d2e2SMichael S. Tsirkin 		if (rc)
557ff31d2e2SMichael S. Tsirkin 			goto err_probe;
558ac399d8fSMichael S. Tsirkin 	}
559ff31d2e2SMichael S. Tsirkin 
560ff31d2e2SMichael S. Tsirkin 	pci_set_master(pci_dev);
561ff31d2e2SMichael S. Tsirkin 
562ff31d2e2SMichael S. Tsirkin 	rc = register_virtio_device(&vp_dev->vdev);
56333635bd9Sweiping zhang 	reg_dev = vp_dev;
564ff31d2e2SMichael S. Tsirkin 	if (rc)
565ff31d2e2SMichael S. Tsirkin 		goto err_register;
566ff31d2e2SMichael S. Tsirkin 
567ff31d2e2SMichael S. Tsirkin 	return 0;
568ff31d2e2SMichael S. Tsirkin 
569ff31d2e2SMichael S. Tsirkin err_register:
570d89c8169SWu Zongyong 	if (vp_dev->is_legacy)
571ff31d2e2SMichael S. Tsirkin 		virtio_pci_legacy_remove(vp_dev);
5721fcf0512SMichael S. Tsirkin 	else
5731fcf0512SMichael S. Tsirkin 		virtio_pci_modern_remove(vp_dev);
574ff31d2e2SMichael S. Tsirkin err_probe:
575ff31d2e2SMichael S. Tsirkin 	pci_disable_device(pci_dev);
576ff31d2e2SMichael S. Tsirkin err_enable_device:
57733635bd9Sweiping zhang 	if (reg_dev)
57833635bd9Sweiping zhang 		put_device(&vp_dev->vdev.dev);
57933635bd9Sweiping zhang 	else
580ff31d2e2SMichael S. Tsirkin 		kfree(vp_dev);
581ff31d2e2SMichael S. Tsirkin 	return rc;
5829a4253dbSMichael S. Tsirkin }
5839a4253dbSMichael S. Tsirkin 
virtio_pci_remove(struct pci_dev * pci_dev)5849a4253dbSMichael S. Tsirkin static void virtio_pci_remove(struct pci_dev *pci_dev)
5859a4253dbSMichael S. Tsirkin {
586ff31d2e2SMichael S. Tsirkin 	struct virtio_pci_device *vp_dev = pci_get_drvdata(pci_dev);
5872989be09SMichael S. Tsirkin 	struct device *dev = get_device(&vp_dev->vdev.dev);
588ff31d2e2SMichael S. Tsirkin 
58943bb40c5SParav Pandit 	/*
59043bb40c5SParav Pandit 	 * Device is marked broken on surprise removal so that virtio upper
59143bb40c5SParav Pandit 	 * layers can abort any ongoing operation.
59243bb40c5SParav Pandit 	 */
59343bb40c5SParav Pandit 	if (!pci_device_is_present(pci_dev))
59443bb40c5SParav Pandit 		virtio_break_device(&vp_dev->vdev);
59543bb40c5SParav Pandit 
596cfecc291STiwei Bie 	pci_disable_sriov(pci_dev);
597cfecc291STiwei Bie 
598ff31d2e2SMichael S. Tsirkin 	unregister_virtio_device(&vp_dev->vdev);
599ff31d2e2SMichael S. Tsirkin 
600d89c8169SWu Zongyong 	if (vp_dev->is_legacy)
6011fcf0512SMichael S. Tsirkin 		virtio_pci_legacy_remove(vp_dev);
6021fcf0512SMichael S. Tsirkin 	else
6031fcf0512SMichael S. Tsirkin 		virtio_pci_modern_remove(vp_dev);
604ff31d2e2SMichael S. Tsirkin 
605ff31d2e2SMichael S. Tsirkin 	pci_disable_device(pci_dev);
6062989be09SMichael S. Tsirkin 	put_device(dev);
6079a4253dbSMichael S. Tsirkin }
6089a4253dbSMichael S. Tsirkin 
virtio_pci_sriov_configure(struct pci_dev * pci_dev,int num_vfs)609cfecc291STiwei Bie static int virtio_pci_sriov_configure(struct pci_dev *pci_dev, int num_vfs)
610cfecc291STiwei Bie {
611cfecc291STiwei Bie 	struct virtio_pci_device *vp_dev = pci_get_drvdata(pci_dev);
612cfecc291STiwei Bie 	struct virtio_device *vdev = &vp_dev->vdev;
613cfecc291STiwei Bie 	int ret;
614cfecc291STiwei Bie 
615cfecc291STiwei Bie 	if (!(vdev->config->get_status(vdev) & VIRTIO_CONFIG_S_DRIVER_OK))
616cfecc291STiwei Bie 		return -EBUSY;
617cfecc291STiwei Bie 
618cfecc291STiwei Bie 	if (!__virtio_test_bit(vdev, VIRTIO_F_SR_IOV))
619cfecc291STiwei Bie 		return -EINVAL;
620cfecc291STiwei Bie 
621cfecc291STiwei Bie 	if (pci_vfs_assigned(pci_dev))
622cfecc291STiwei Bie 		return -EPERM;
623cfecc291STiwei Bie 
624cfecc291STiwei Bie 	if (num_vfs == 0) {
625cfecc291STiwei Bie 		pci_disable_sriov(pci_dev);
626cfecc291STiwei Bie 		return 0;
627cfecc291STiwei Bie 	}
628cfecc291STiwei Bie 
629cfecc291STiwei Bie 	ret = pci_enable_sriov(pci_dev, num_vfs);
630cfecc291STiwei Bie 	if (ret < 0)
631cfecc291STiwei Bie 		return ret;
632cfecc291STiwei Bie 
633cfecc291STiwei Bie 	return num_vfs;
634cfecc291STiwei Bie }
635cfecc291STiwei Bie 
6369a4253dbSMichael S. Tsirkin static struct pci_driver virtio_pci_driver = {
6379a4253dbSMichael S. Tsirkin 	.name		= "virtio-pci",
6389a4253dbSMichael S. Tsirkin 	.id_table	= virtio_pci_id_table,
6399a4253dbSMichael S. Tsirkin 	.probe		= virtio_pci_probe,
6409a4253dbSMichael S. Tsirkin 	.remove		= virtio_pci_remove,
6419a4253dbSMichael S. Tsirkin #ifdef CONFIG_PM_SLEEP
6429a4253dbSMichael S. Tsirkin 	.driver.pm	= &virtio_pci_pm_ops,
6439a4253dbSMichael S. Tsirkin #endif
644cfecc291STiwei Bie 	.sriov_configure = virtio_pci_sriov_configure,
6459a4253dbSMichael S. Tsirkin };
6469a4253dbSMichael S. Tsirkin 
6479a4253dbSMichael S. Tsirkin module_pci_driver(virtio_pci_driver);
6485ff16110SHerbert Xu 
6495ff16110SHerbert Xu MODULE_AUTHOR("Anthony Liguori <aliguori@us.ibm.com>");
6505ff16110SHerbert Xu MODULE_DESCRIPTION("virtio-pci");
6515ff16110SHerbert Xu MODULE_LICENSE("GPL");
6525ff16110SHerbert Xu MODULE_VERSION("1");
653