1 // SPDX-License-Identifier: GPL-2.0
3 * PCI Backend - Provides a Virtual PCI bus (with real devices)
6 * Author: Ryan Wilson <hap9@epoch.ncsc.mil>
9 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
11 #include <linux/list.h>
12 #include <linux/slab.h>
13 #include <linux/pci.h>
14 #include <linux/mutex.h>
17 #define PCI_SLOT_MAX 32
19 struct vpci_dev_data {
20 /* Access to dev_list must be protected by lock */
21 struct list_head dev_list[PCI_SLOT_MAX];
25 static inline struct list_head *list_first(struct list_head *head)
30 static struct pci_dev *__xen_pcibk_get_pci_dev(struct xen_pcibk_device *pdev,
35 struct pci_dev_entry *entry;
36 struct pci_dev *dev = NULL;
37 struct vpci_dev_data *vpci_dev = pdev->pci_dev_data;
39 if (domain != 0 || bus != 0)
42 if (PCI_SLOT(devfn) < PCI_SLOT_MAX) {
43 mutex_lock(&vpci_dev->lock);
45 list_for_each_entry(entry,
46 &vpci_dev->dev_list[PCI_SLOT(devfn)],
48 if (PCI_FUNC(entry->dev->devfn) == PCI_FUNC(devfn)) {
54 mutex_unlock(&vpci_dev->lock);
59 static inline int match_slot(struct pci_dev *l, struct pci_dev *r)
61 if (pci_domain_nr(l->bus) == pci_domain_nr(r->bus)
62 && l->bus == r->bus && PCI_SLOT(l->devfn) == PCI_SLOT(r->devfn))
68 static int __xen_pcibk_add_pci_dev(struct xen_pcibk_device *pdev,
69 struct pci_dev *dev, int devid,
70 publish_pci_dev_cb publish_cb)
72 int err = 0, slot, func = PCI_FUNC(dev->devfn);
73 struct pci_dev_entry *t, *dev_entry;
74 struct vpci_dev_data *vpci_dev = pdev->pci_dev_data;
76 if ((dev->class >> 24) == PCI_BASE_CLASS_BRIDGE) {
78 xenbus_dev_fatal(pdev->xdev, err,
79 "Can't export bridges on the virtual PCI bus");
83 dev_entry = kmalloc(sizeof(*dev_entry), GFP_KERNEL);
86 xenbus_dev_fatal(pdev->xdev, err,
87 "Error adding entry to virtual PCI bus");
93 mutex_lock(&vpci_dev->lock);
96 * Keep multi-function devices together on the virtual PCI bus, except
97 * that we want to keep virtual functions at func 0 on their own. They
98 * aren't multi-function devices and hence their presence at func 0
99 * may cause guests to not scan the other functions.
101 if (!dev->is_virtfn || func) {
102 for (slot = 0; slot < PCI_SLOT_MAX; slot++) {
103 if (list_empty(&vpci_dev->dev_list[slot]))
106 t = list_entry(list_first(&vpci_dev->dev_list[slot]),
107 struct pci_dev_entry, list);
108 if (t->dev->is_virtfn && !PCI_FUNC(t->dev->devfn))
111 if (match_slot(dev, t->dev)) {
112 pr_info("vpci: %s: assign to virtual slot %d func %d\n",
115 list_add_tail(&dev_entry->list,
116 &vpci_dev->dev_list[slot]);
122 /* Assign to a new slot on the virtual PCI bus */
123 for (slot = 0; slot < PCI_SLOT_MAX; slot++) {
124 if (list_empty(&vpci_dev->dev_list[slot])) {
125 pr_info("vpci: %s: assign to virtual slot %d\n",
126 pci_name(dev), slot);
127 list_add_tail(&dev_entry->list,
128 &vpci_dev->dev_list[slot]);
134 xenbus_dev_fatal(pdev->xdev, err,
135 "No more space on root virtual PCI bus");
138 mutex_unlock(&vpci_dev->lock);
140 /* Publish this device. */
142 err = publish_cb(pdev, 0, 0, PCI_DEVFN(slot, func), devid);
150 static void __xen_pcibk_release_pci_dev(struct xen_pcibk_device *pdev,
151 struct pci_dev *dev, bool lock)
154 struct vpci_dev_data *vpci_dev = pdev->pci_dev_data;
155 struct pci_dev *found_dev = NULL;
157 mutex_lock(&vpci_dev->lock);
159 for (slot = 0; slot < PCI_SLOT_MAX; slot++) {
160 struct pci_dev_entry *e;
162 list_for_each_entry(e, &vpci_dev->dev_list[slot], list) {
173 mutex_unlock(&vpci_dev->lock);
177 device_lock(&found_dev->dev);
178 pcistub_put_pci_dev(found_dev);
180 device_unlock(&found_dev->dev);
184 static int __xen_pcibk_init_devices(struct xen_pcibk_device *pdev)
187 struct vpci_dev_data *vpci_dev;
189 vpci_dev = kmalloc(sizeof(*vpci_dev), GFP_KERNEL);
193 mutex_init(&vpci_dev->lock);
195 for (slot = 0; slot < PCI_SLOT_MAX; slot++)
196 INIT_LIST_HEAD(&vpci_dev->dev_list[slot]);
198 pdev->pci_dev_data = vpci_dev;
203 static int __xen_pcibk_publish_pci_roots(struct xen_pcibk_device *pdev,
204 publish_pci_root_cb publish_cb)
206 /* The Virtual PCI bus has only one root */
207 return publish_cb(pdev, 0, 0);
210 static void __xen_pcibk_release_devices(struct xen_pcibk_device *pdev)
213 struct vpci_dev_data *vpci_dev = pdev->pci_dev_data;
215 for (slot = 0; slot < PCI_SLOT_MAX; slot++) {
216 struct pci_dev_entry *e, *tmp;
217 list_for_each_entry_safe(e, tmp, &vpci_dev->dev_list[slot],
219 struct pci_dev *dev = e->dev;
221 device_lock(&dev->dev);
222 pcistub_put_pci_dev(dev);
223 device_unlock(&dev->dev);
229 pdev->pci_dev_data = NULL;
232 static int __xen_pcibk_get_pcifront_dev(struct pci_dev *pcidev,
233 struct xen_pcibk_device *pdev,
234 unsigned int *domain, unsigned int *bus,
237 struct pci_dev_entry *entry;
238 struct pci_dev *dev = NULL;
239 struct vpci_dev_data *vpci_dev = pdev->pci_dev_data;
242 mutex_lock(&vpci_dev->lock);
243 for (slot = 0; slot < PCI_SLOT_MAX; slot++) {
244 list_for_each_entry(entry,
245 &vpci_dev->dev_list[slot],
248 if (dev && dev->bus->number == pcidev->bus->number
249 && pci_domain_nr(dev->bus) ==
250 pci_domain_nr(pcidev->bus)
251 && dev->devfn == pcidev->devfn) {
255 *devfn = PCI_DEVFN(slot,
256 PCI_FUNC(pcidev->devfn));
260 mutex_unlock(&vpci_dev->lock);
264 const struct xen_pcibk_backend xen_pcibk_vpci_backend = {
266 .init = __xen_pcibk_init_devices,
267 .free = __xen_pcibk_release_devices,
268 .find = __xen_pcibk_get_pcifront_dev,
269 .publish = __xen_pcibk_publish_pci_roots,
270 .release = __xen_pcibk_release_pci_dev,
271 .add = __xen_pcibk_add_pci_dev,
272 .get = __xen_pcibk_get_pci_dev,