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 struct pci_dev *pciback_get_pci_dev(struct pciback_device *pdev,
28 unsigned int domain, unsigned int bus,
31 struct pci_dev_entry *entry;
32 struct pci_dev *dev = NULL;
33 struct vpci_dev_data *vpci_dev = pdev->pci_dev_data;
36 if (domain != 0 || bus != 0)
39 if (PCI_SLOT(devfn) < PCI_SLOT_MAX) {
40 spin_lock_irqsave(&vpci_dev->lock, flags);
42 list_for_each_entry(entry,
43 &vpci_dev->dev_list[PCI_SLOT(devfn)],
45 if (PCI_FUNC(entry->dev->devfn) == PCI_FUNC(devfn)) {
51 spin_unlock_irqrestore(&vpci_dev->lock, flags);
56 static inline int match_slot(struct pci_dev *l, struct pci_dev *r)
58 if (pci_domain_nr(l->bus) == pci_domain_nr(r->bus)
59 && l->bus == r->bus && PCI_SLOT(l->devfn) == PCI_SLOT(r->devfn))
65 int pciback_add_pci_dev(struct pciback_device *pdev, struct pci_dev *dev)
68 struct pci_dev_entry *t, *dev_entry;
69 struct vpci_dev_data *vpci_dev = pdev->pci_dev_data;
72 if ((dev->class >> 24) == PCI_BASE_CLASS_BRIDGE) {
74 xenbus_dev_fatal(pdev->xdev, err,
75 "Can't export bridges on the virtual PCI bus");
79 dev_entry = kmalloc(sizeof(*dev_entry), GFP_KERNEL);
82 xenbus_dev_fatal(pdev->xdev, err,
83 "Error adding entry to virtual PCI bus");
89 spin_lock_irqsave(&vpci_dev->lock, flags);
91 /* Keep multi-function devices together on the virtual PCI bus */
92 for (slot = 0; slot < PCI_SLOT_MAX; slot++) {
93 if (!list_empty(&vpci_dev->dev_list[slot])) {
94 t = list_entry(list_first(&vpci_dev->dev_list[slot]),
95 struct pci_dev_entry, list);
97 if (match_slot(dev, t->dev)) {
98 pr_info("pciback: vpci: %s: "
99 "assign to virtual slot %d func %d\n",
101 PCI_FUNC(dev->devfn));
102 list_add_tail(&dev_entry->list,
103 &vpci_dev->dev_list[slot]);
109 /* Assign to a new slot on the virtual PCI bus */
110 for (slot = 0; slot < PCI_SLOT_MAX; slot++) {
111 if (list_empty(&vpci_dev->dev_list[slot])) {
113 "pciback: vpci: %s: assign to virtual slot %d\n",
114 pci_name(dev), slot);
115 list_add_tail(&dev_entry->list,
116 &vpci_dev->dev_list[slot]);
122 xenbus_dev_fatal(pdev->xdev, err,
123 "No more space on root virtual PCI bus");
126 spin_unlock_irqrestore(&vpci_dev->lock, flags);
131 void pciback_release_pci_dev(struct pciback_device *pdev, struct pci_dev *dev)
134 struct vpci_dev_data *vpci_dev = pdev->pci_dev_data;
135 struct pci_dev *found_dev = NULL;
138 spin_lock_irqsave(&vpci_dev->lock, flags);
140 for (slot = 0; slot < PCI_SLOT_MAX; slot++) {
141 struct pci_dev_entry *e, *tmp;
142 list_for_each_entry_safe(e, tmp, &vpci_dev->dev_list[slot],
154 spin_unlock_irqrestore(&vpci_dev->lock, flags);
157 pcistub_put_pci_dev(found_dev);
160 int pciback_init_devices(struct pciback_device *pdev)
163 struct vpci_dev_data *vpci_dev;
165 vpci_dev = kmalloc(sizeof(*vpci_dev), GFP_KERNEL);
169 spin_lock_init(&vpci_dev->lock);
171 for (slot = 0; slot < PCI_SLOT_MAX; slot++) {
172 INIT_LIST_HEAD(&vpci_dev->dev_list[slot]);
175 pdev->pci_dev_data = vpci_dev;
180 int pciback_publish_pci_roots(struct pciback_device *pdev,
181 publish_pci_root_cb publish_cb)
183 /* The Virtual PCI bus has only one root */
184 return publish_cb(pdev, 0, 0);
187 void pciback_release_devices(struct pciback_device *pdev)
190 struct vpci_dev_data *vpci_dev = pdev->pci_dev_data;
192 for (slot = 0; slot < PCI_SLOT_MAX; slot++) {
193 struct pci_dev_entry *e, *tmp;
194 list_for_each_entry_safe(e, tmp, &vpci_dev->dev_list[slot],
197 pcistub_put_pci_dev(e->dev);
203 pdev->pci_dev_data = NULL;