2 * PCI Backend - Provides a Virtual PCI bus (with real devices)
5 * Author: Ryan Wilson <hap9@epoch.ncsc.mil>
8 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
10 #include <linux/list.h>
11 #include <linux/slab.h>
12 #include <linux/pci.h>
13 #include <linux/mutex.h>
16 #define PCI_SLOT_MAX 32
18 struct vpci_dev_data {
19 /* Access to dev_list must be protected by lock */
20 struct list_head dev_list[PCI_SLOT_MAX];
24 static inline struct list_head *list_first(struct list_head *head)
29 static struct pci_dev *__xen_pcibk_get_pci_dev(struct xen_pcibk_device *pdev,
34 struct pci_dev_entry *entry;
35 struct pci_dev *dev = NULL;
36 struct vpci_dev_data *vpci_dev = pdev->pci_dev_data;
38 if (domain != 0 || bus != 0)
41 if (PCI_SLOT(devfn) < PCI_SLOT_MAX) {
42 mutex_lock(&vpci_dev->lock);
44 list_for_each_entry(entry,
45 &vpci_dev->dev_list[PCI_SLOT(devfn)],
47 if (PCI_FUNC(entry->dev->devfn) == PCI_FUNC(devfn)) {
53 mutex_unlock(&vpci_dev->lock);
58 static inline int match_slot(struct pci_dev *l, struct pci_dev *r)
60 if (pci_domain_nr(l->bus) == pci_domain_nr(r->bus)
61 && l->bus == r->bus && PCI_SLOT(l->devfn) == PCI_SLOT(r->devfn))
67 static int __xen_pcibk_add_pci_dev(struct xen_pcibk_device *pdev,
68 struct pci_dev *dev, int devid,
69 publish_pci_dev_cb publish_cb)
71 int err = 0, slot, func = PCI_FUNC(dev->devfn);
72 struct pci_dev_entry *t, *dev_entry;
73 struct vpci_dev_data *vpci_dev = pdev->pci_dev_data;
75 if ((dev->class >> 24) == PCI_BASE_CLASS_BRIDGE) {
77 xenbus_dev_fatal(pdev->xdev, err,
78 "Can't export bridges on the virtual PCI bus");
82 dev_entry = kmalloc(sizeof(*dev_entry), GFP_KERNEL);
85 xenbus_dev_fatal(pdev->xdev, err,
86 "Error adding entry to virtual PCI bus");
92 mutex_lock(&vpci_dev->lock);
95 * Keep multi-function devices together on the virtual PCI bus, except
96 * that we want to keep virtual functions at func 0 on their own. They
97 * aren't multi-function devices and hence their presence at func 0
98 * may cause guests to not scan the other functions.
100 if (!dev->is_virtfn || func) {
101 for (slot = 0; slot < PCI_SLOT_MAX; slot++) {
102 if (list_empty(&vpci_dev->dev_list[slot]))
105 t = list_entry(list_first(&vpci_dev->dev_list[slot]),
106 struct pci_dev_entry, list);
107 if (t->dev->is_virtfn && !PCI_FUNC(t->dev->devfn))
110 if (match_slot(dev, t->dev)) {
111 pr_info("vpci: %s: assign to virtual slot %d func %d\n",
114 list_add_tail(&dev_entry->list,
115 &vpci_dev->dev_list[slot]);
121 /* Assign to a new slot on the virtual PCI bus */
122 for (slot = 0; slot < PCI_SLOT_MAX; slot++) {
123 if (list_empty(&vpci_dev->dev_list[slot])) {
124 pr_info("vpci: %s: assign to virtual slot %d\n",
125 pci_name(dev), slot);
126 list_add_tail(&dev_entry->list,
127 &vpci_dev->dev_list[slot]);
133 xenbus_dev_fatal(pdev->xdev, err,
134 "No more space on root virtual PCI bus");
137 mutex_unlock(&vpci_dev->lock);
139 /* Publish this device. */
141 err = publish_cb(pdev, 0, 0, PCI_DEVFN(slot, func), devid);
149 static void __xen_pcibk_release_pci_dev(struct xen_pcibk_device *pdev,
150 struct pci_dev *dev, bool lock)
153 struct vpci_dev_data *vpci_dev = pdev->pci_dev_data;
154 struct pci_dev *found_dev = NULL;
156 mutex_lock(&vpci_dev->lock);
158 for (slot = 0; slot < PCI_SLOT_MAX; slot++) {
159 struct pci_dev_entry *e;
161 list_for_each_entry(e, &vpci_dev->dev_list[slot], list) {
172 mutex_unlock(&vpci_dev->lock);
176 device_lock(&found_dev->dev);
177 pcistub_put_pci_dev(found_dev);
179 device_unlock(&found_dev->dev);
183 static int __xen_pcibk_init_devices(struct xen_pcibk_device *pdev)
186 struct vpci_dev_data *vpci_dev;
188 vpci_dev = kmalloc(sizeof(*vpci_dev), GFP_KERNEL);
192 mutex_init(&vpci_dev->lock);
194 for (slot = 0; slot < PCI_SLOT_MAX; slot++)
195 INIT_LIST_HEAD(&vpci_dev->dev_list[slot]);
197 pdev->pci_dev_data = vpci_dev;
202 static int __xen_pcibk_publish_pci_roots(struct xen_pcibk_device *pdev,
203 publish_pci_root_cb publish_cb)
205 /* The Virtual PCI bus has only one root */
206 return publish_cb(pdev, 0, 0);
209 static void __xen_pcibk_release_devices(struct xen_pcibk_device *pdev)
212 struct vpci_dev_data *vpci_dev = pdev->pci_dev_data;
214 for (slot = 0; slot < PCI_SLOT_MAX; slot++) {
215 struct pci_dev_entry *e, *tmp;
216 list_for_each_entry_safe(e, tmp, &vpci_dev->dev_list[slot],
218 struct pci_dev *dev = e->dev;
220 device_lock(&dev->dev);
221 pcistub_put_pci_dev(dev);
222 device_unlock(&dev->dev);
228 pdev->pci_dev_data = NULL;
231 static int __xen_pcibk_get_pcifront_dev(struct pci_dev *pcidev,
232 struct xen_pcibk_device *pdev,
233 unsigned int *domain, unsigned int *bus,
236 struct pci_dev_entry *entry;
237 struct pci_dev *dev = NULL;
238 struct vpci_dev_data *vpci_dev = pdev->pci_dev_data;
241 mutex_lock(&vpci_dev->lock);
242 for (slot = 0; slot < PCI_SLOT_MAX; slot++) {
243 list_for_each_entry(entry,
244 &vpci_dev->dev_list[slot],
247 if (dev && dev->bus->number == pcidev->bus->number
248 && pci_domain_nr(dev->bus) ==
249 pci_domain_nr(pcidev->bus)
250 && dev->devfn == pcidev->devfn) {
254 *devfn = PCI_DEVFN(slot,
255 PCI_FUNC(pcidev->devfn));
259 mutex_unlock(&vpci_dev->lock);
263 const struct xen_pcibk_backend xen_pcibk_vpci_backend = {
265 .init = __xen_pcibk_init_devices,
266 .free = __xen_pcibk_release_devices,
267 .find = __xen_pcibk_get_pcifront_dev,
268 .publish = __xen_pcibk_publish_pci_roots,
269 .release = __xen_pcibk_release_pci_dev,
270 .add = __xen_pcibk_add_pci_dev,
271 .get = __xen_pcibk_get_pci_dev,