2 * PCI Backend - Provides a Virtual PCI bus (with real devices)
5 * Author: Ryan Wilson <hap9@epoch.ncsc.mil>
8 #include <linux/list.h>
9 #include <linux/slab.h>
10 #include <linux/pci.h>
11 #include <linux/spinlock.h>
14 #define PCI_SLOT_MAX 32
16 struct vpci_dev_data {
17 /* Access to dev_list must be protected by lock */
18 struct list_head dev_list[PCI_SLOT_MAX];
22 static inline struct list_head *list_first(struct list_head *head)
27 static struct pci_dev *__xen_pcibk_get_pci_dev(struct xen_pcibk_device *pdev,
32 struct pci_dev_entry *entry;
33 struct pci_dev *dev = NULL;
34 struct vpci_dev_data *vpci_dev = pdev->pci_dev_data;
37 if (domain != 0 || bus != 0)
40 if (PCI_SLOT(devfn) < PCI_SLOT_MAX) {
41 spin_lock_irqsave(&vpci_dev->lock, flags);
43 list_for_each_entry(entry,
44 &vpci_dev->dev_list[PCI_SLOT(devfn)],
46 if (PCI_FUNC(entry->dev->devfn) == PCI_FUNC(devfn)) {
52 spin_unlock_irqrestore(&vpci_dev->lock, flags);
57 static inline int match_slot(struct pci_dev *l, struct pci_dev *r)
59 if (pci_domain_nr(l->bus) == pci_domain_nr(r->bus)
60 && l->bus == r->bus && PCI_SLOT(l->devfn) == PCI_SLOT(r->devfn))
66 static int __xen_pcibk_add_pci_dev(struct xen_pcibk_device *pdev,
67 struct pci_dev *dev, int devid,
68 publish_pci_dev_cb publish_cb)
70 int err = 0, slot, func = -1;
71 struct pci_dev_entry *t, *dev_entry;
72 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 spin_lock_irqsave(&vpci_dev->lock, flags);
94 /* Keep multi-function devices together on the virtual PCI bus */
95 for (slot = 0; slot < PCI_SLOT_MAX; slot++) {
96 if (!list_empty(&vpci_dev->dev_list[slot])) {
97 t = list_entry(list_first(&vpci_dev->dev_list[slot]),
98 struct pci_dev_entry, list);
100 if (match_slot(dev, t->dev)) {
101 pr_info(DRV_NAME ": vpci: %s: "
102 "assign to virtual slot %d func %d\n",
104 PCI_FUNC(dev->devfn));
105 list_add_tail(&dev_entry->list,
106 &vpci_dev->dev_list[slot]);
107 func = PCI_FUNC(dev->devfn);
113 /* Assign to a new slot on the virtual PCI bus */
114 for (slot = 0; slot < PCI_SLOT_MAX; slot++) {
115 if (list_empty(&vpci_dev->dev_list[slot])) {
116 printk(KERN_INFO DRV_NAME
117 ": vpci: %s: assign to virtual slot %d\n",
118 pci_name(dev), slot);
119 list_add_tail(&dev_entry->list,
120 &vpci_dev->dev_list[slot]);
121 func = PCI_FUNC(dev->devfn);
127 xenbus_dev_fatal(pdev->xdev, err,
128 "No more space on root virtual PCI bus");
131 spin_unlock_irqrestore(&vpci_dev->lock, flags);
133 /* Publish this device. */
135 err = publish_cb(pdev, 0, 0, PCI_DEVFN(slot, func), devid);
141 static void __xen_pcibk_release_pci_dev(struct xen_pcibk_device *pdev,
145 struct vpci_dev_data *vpci_dev = pdev->pci_dev_data;
146 struct pci_dev *found_dev = NULL;
149 spin_lock_irqsave(&vpci_dev->lock, flags);
151 for (slot = 0; slot < PCI_SLOT_MAX; slot++) {
152 struct pci_dev_entry *e;
154 list_for_each_entry(e, &vpci_dev->dev_list[slot], list) {
165 spin_unlock_irqrestore(&vpci_dev->lock, flags);
168 pcistub_put_pci_dev(found_dev);
171 static int __xen_pcibk_init_devices(struct xen_pcibk_device *pdev)
174 struct vpci_dev_data *vpci_dev;
176 vpci_dev = kmalloc(sizeof(*vpci_dev), GFP_KERNEL);
180 spin_lock_init(&vpci_dev->lock);
182 for (slot = 0; slot < PCI_SLOT_MAX; slot++)
183 INIT_LIST_HEAD(&vpci_dev->dev_list[slot]);
185 pdev->pci_dev_data = vpci_dev;
190 static int __xen_pcibk_publish_pci_roots(struct xen_pcibk_device *pdev,
191 publish_pci_root_cb publish_cb)
193 /* The Virtual PCI bus has only one root */
194 return publish_cb(pdev, 0, 0);
197 static void __xen_pcibk_release_devices(struct xen_pcibk_device *pdev)
200 struct vpci_dev_data *vpci_dev = pdev->pci_dev_data;
202 for (slot = 0; slot < PCI_SLOT_MAX; slot++) {
203 struct pci_dev_entry *e, *tmp;
204 list_for_each_entry_safe(e, tmp, &vpci_dev->dev_list[slot],
207 pcistub_put_pci_dev(e->dev);
213 pdev->pci_dev_data = NULL;
216 static int __xen_pcibk_get_pcifront_dev(struct pci_dev *pcidev,
217 struct xen_pcibk_device *pdev,
218 unsigned int *domain, unsigned int *bus,
221 struct pci_dev_entry *entry;
222 struct pci_dev *dev = NULL;
223 struct vpci_dev_data *vpci_dev = pdev->pci_dev_data;
227 spin_lock_irqsave(&vpci_dev->lock, flags);
228 for (slot = 0; slot < PCI_SLOT_MAX; slot++) {
229 list_for_each_entry(entry,
230 &vpci_dev->dev_list[slot],
233 if (dev && dev->bus->number == pcidev->bus->number
234 && pci_domain_nr(dev->bus) ==
235 pci_domain_nr(pcidev->bus)
236 && dev->devfn == pcidev->devfn) {
240 *devfn = PCI_DEVFN(slot,
241 PCI_FUNC(pcidev->devfn));
245 spin_unlock_irqrestore(&vpci_dev->lock, flags);
249 const struct xen_pcibk_backend xen_pcibk_vpci_backend = {
251 .init = __xen_pcibk_init_devices,
252 .free = __xen_pcibk_release_devices,
253 .find = __xen_pcibk_get_pcifront_dev,
254 .publish = __xen_pcibk_publish_pci_roots,
255 .release = __xen_pcibk_release_pci_dev,
256 .add = __xen_pcibk_add_pci_dev,
257 .get = __xen_pcibk_get_pci_dev,