Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

3 Commits   0 Branches   0 Tags
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   1) // SPDX-License-Identifier: GPL-2.0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   3)  * PCI Backend - Provides a Virtual PCI bus (with real devices)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   4)  *               to the frontend
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   5)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   6)  *   Author: Ryan Wilson <hap9@epoch.ncsc.mil>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   7)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   8) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   9) #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  10) #define dev_fmt pr_fmt
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  11) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  12) #include <linux/list.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  13) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  14) #include <linux/pci.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  15) #include <linux/mutex.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  16) #include "pciback.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  17) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  18) #define PCI_SLOT_MAX 32
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  19) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  20) struct vpci_dev_data {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  21) 	/* Access to dev_list must be protected by lock */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  22) 	struct list_head dev_list[PCI_SLOT_MAX];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  23) 	struct mutex lock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  24) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  25) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  26) static inline struct list_head *list_first(struct list_head *head)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  27) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  28) 	return head->next;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  29) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  30) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  31) static struct pci_dev *__xen_pcibk_get_pci_dev(struct xen_pcibk_device *pdev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  32) 					       unsigned int domain,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  33) 					       unsigned int bus,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  34) 					       unsigned int devfn)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  35) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  36) 	struct pci_dev_entry *entry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  37) 	struct pci_dev *dev = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  38) 	struct vpci_dev_data *vpci_dev = pdev->pci_dev_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  39) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  40) 	if (domain != 0 || bus != 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  41) 		return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  42) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  43) 	if (PCI_SLOT(devfn) < PCI_SLOT_MAX) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  44) 		mutex_lock(&vpci_dev->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  45) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  46) 		list_for_each_entry(entry,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  47) 				    &vpci_dev->dev_list[PCI_SLOT(devfn)],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  48) 				    list) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  49) 			if (PCI_FUNC(entry->dev->devfn) == PCI_FUNC(devfn)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  50) 				dev = entry->dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  51) 				break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  52) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  53) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  54) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  55) 		mutex_unlock(&vpci_dev->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  56) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  57) 	return dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  58) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  59) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  60) static inline int match_slot(struct pci_dev *l, struct pci_dev *r)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  61) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  62) 	if (pci_domain_nr(l->bus) == pci_domain_nr(r->bus)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  63) 	    && l->bus == r->bus && PCI_SLOT(l->devfn) == PCI_SLOT(r->devfn))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  64) 		return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  65) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  66) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  67) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  68) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  69) static int __xen_pcibk_add_pci_dev(struct xen_pcibk_device *pdev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  70) 				   struct pci_dev *dev, int devid,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  71) 				   publish_pci_dev_cb publish_cb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  72) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  73) 	int err = 0, slot, func = PCI_FUNC(dev->devfn);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  74) 	struct pci_dev_entry *t, *dev_entry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  75) 	struct vpci_dev_data *vpci_dev = pdev->pci_dev_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  76) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  77) 	if ((dev->class >> 24) == PCI_BASE_CLASS_BRIDGE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  78) 		err = -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  79) 		xenbus_dev_fatal(pdev->xdev, err,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  80) 				 "Can't export bridges on the virtual PCI bus");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  81) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  82) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  83) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  84) 	dev_entry = kmalloc(sizeof(*dev_entry), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  85) 	if (!dev_entry) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  86) 		err = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  87) 		xenbus_dev_fatal(pdev->xdev, err,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  88) 				 "Error adding entry to virtual PCI bus");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  89) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  90) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  91) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  92) 	dev_entry->dev = dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  93) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  94) 	mutex_lock(&vpci_dev->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  95) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  96) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  97) 	 * Keep multi-function devices together on the virtual PCI bus, except
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  98) 	 * that we want to keep virtual functions at func 0 on their own. They
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  99) 	 * aren't multi-function devices and hence their presence at func 0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) 	 * may cause guests to not scan the other functions.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) 	if (!dev->is_virtfn || func) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) 		for (slot = 0; slot < PCI_SLOT_MAX; slot++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) 			if (list_empty(&vpci_dev->dev_list[slot]))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) 				continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) 			t = list_entry(list_first(&vpci_dev->dev_list[slot]),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) 				       struct pci_dev_entry, list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) 			if (t->dev->is_virtfn && !PCI_FUNC(t->dev->devfn))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) 				continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) 			if (match_slot(dev, t->dev)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) 				dev_info(&dev->dev, "vpci: assign to virtual slot %d func %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) 					 slot, func);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) 				list_add_tail(&dev_entry->list,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) 					      &vpci_dev->dev_list[slot]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) 				goto unlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) 	/* Assign to a new slot on the virtual PCI bus */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) 	for (slot = 0; slot < PCI_SLOT_MAX; slot++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) 		if (list_empty(&vpci_dev->dev_list[slot])) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) 			dev_info(&dev->dev, "vpci: assign to virtual slot %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) 				 slot);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) 			list_add_tail(&dev_entry->list,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) 				      &vpci_dev->dev_list[slot]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) 			goto unlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) 	err = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) 	xenbus_dev_fatal(pdev->xdev, err,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) 			 "No more space on root virtual PCI bus");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) unlock:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) 	mutex_unlock(&vpci_dev->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) 	/* Publish this device. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) 	if (!err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) 		err = publish_cb(pdev, 0, 0, PCI_DEVFN(slot, func), devid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) 		kfree(dev_entry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) 	return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) static void __xen_pcibk_release_pci_dev(struct xen_pcibk_device *pdev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) 					struct pci_dev *dev, bool lock)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) 	int slot;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) 	struct vpci_dev_data *vpci_dev = pdev->pci_dev_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) 	struct pci_dev *found_dev = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) 	mutex_lock(&vpci_dev->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) 	for (slot = 0; slot < PCI_SLOT_MAX; slot++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) 		struct pci_dev_entry *e;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) 		list_for_each_entry(e, &vpci_dev->dev_list[slot], list) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) 			if (e->dev == dev) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) 				list_del(&e->list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) 				found_dev = e->dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) 				kfree(e);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) 				goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) 	mutex_unlock(&vpci_dev->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) 	if (found_dev) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) 		if (lock)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) 			device_lock(&found_dev->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) 		pcistub_put_pci_dev(found_dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) 		if (lock)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) 			device_unlock(&found_dev->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) static int __xen_pcibk_init_devices(struct xen_pcibk_device *pdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) 	int slot;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) 	struct vpci_dev_data *vpci_dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) 	vpci_dev = kmalloc(sizeof(*vpci_dev), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) 	if (!vpci_dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) 	mutex_init(&vpci_dev->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) 	for (slot = 0; slot < PCI_SLOT_MAX; slot++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) 		INIT_LIST_HEAD(&vpci_dev->dev_list[slot]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) 	pdev->pci_dev_data = vpci_dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) static int __xen_pcibk_publish_pci_roots(struct xen_pcibk_device *pdev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) 					 publish_pci_root_cb publish_cb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) 	/* The Virtual PCI bus has only one root */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) 	return publish_cb(pdev, 0, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) static void __xen_pcibk_release_devices(struct xen_pcibk_device *pdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) 	int slot;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) 	struct vpci_dev_data *vpci_dev = pdev->pci_dev_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) 	for (slot = 0; slot < PCI_SLOT_MAX; slot++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) 		struct pci_dev_entry *e, *tmp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) 		list_for_each_entry_safe(e, tmp, &vpci_dev->dev_list[slot],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) 					 list) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) 			struct pci_dev *dev = e->dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) 			list_del(&e->list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) 			device_lock(&dev->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) 			pcistub_put_pci_dev(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) 			device_unlock(&dev->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) 			kfree(e);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) 	kfree(vpci_dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) 	pdev->pci_dev_data = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) static int __xen_pcibk_get_pcifront_dev(struct pci_dev *pcidev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) 					struct xen_pcibk_device *pdev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) 					unsigned int *domain, unsigned int *bus,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) 					unsigned int *devfn)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) 	struct pci_dev_entry *entry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) 	struct pci_dev *dev = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) 	struct vpci_dev_data *vpci_dev = pdev->pci_dev_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) 	int found = 0, slot;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) 	mutex_lock(&vpci_dev->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) 	for (slot = 0; slot < PCI_SLOT_MAX; slot++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) 		list_for_each_entry(entry,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) 			    &vpci_dev->dev_list[slot],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) 			    list) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) 			dev = entry->dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) 			if (dev && dev->bus->number == pcidev->bus->number
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) 				&& pci_domain_nr(dev->bus) ==
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) 					pci_domain_nr(pcidev->bus)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) 				&& dev->devfn == pcidev->devfn) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) 				found = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) 				*domain = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) 				*bus = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) 				*devfn = PCI_DEVFN(slot,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) 					 PCI_FUNC(pcidev->devfn));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) 	mutex_unlock(&vpci_dev->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) 	return found;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) const struct xen_pcibk_backend xen_pcibk_vpci_backend = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) 	.name		= "vpci",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) 	.init		= __xen_pcibk_init_devices,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) 	.free		= __xen_pcibk_release_devices,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) 	.find		= __xen_pcibk_get_pcifront_dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) 	.publish	= __xen_pcibk_publish_pci_roots,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) 	.release	= __xen_pcibk_release_pci_dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) 	.add		= __xen_pcibk_add_pci_dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) 	.get		= __xen_pcibk_get_pci_dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) };