2 * PCI Backend - Provides restricted access to the real PCI bus topology
5 * Author: Ryan Wilson <hap9@epoch.ncsc.mil>
8 #include <linux/list.h>
10 #include <linux/spinlock.h>
13 struct passthrough_dev_data {
14 /* Access to dev_list must be protected by lock */
15 struct list_head dev_list;
19 struct pci_dev *pciback_get_pci_dev(struct pciback_device *pdev,
20 unsigned int domain, unsigned int bus,
23 struct passthrough_dev_data *dev_data = pdev->pci_dev_data;
24 struct pci_dev_entry *dev_entry;
25 struct pci_dev *dev = NULL;
28 spin_lock_irqsave(&dev_data->lock, flags);
30 list_for_each_entry(dev_entry, &dev_data->dev_list, list) {
31 if (domain == (unsigned int)pci_domain_nr(dev_entry->dev->bus)
32 && bus == (unsigned int)dev_entry->dev->bus->number
33 && devfn == dev_entry->dev->devfn) {
39 spin_unlock_irqrestore(&dev_data->lock, flags);
44 int pciback_add_pci_dev(struct pciback_device *pdev, struct pci_dev *dev)
46 struct passthrough_dev_data *dev_data = pdev->pci_dev_data;
47 struct pci_dev_entry *dev_entry;
50 dev_entry = kmalloc(sizeof(*dev_entry), GFP_KERNEL);
55 spin_lock_irqsave(&dev_data->lock, flags);
56 list_add_tail(&dev_entry->list, &dev_data->dev_list);
57 spin_unlock_irqrestore(&dev_data->lock, flags);
62 void pciback_release_pci_dev(struct pciback_device *pdev, struct pci_dev *dev)
64 struct passthrough_dev_data *dev_data = pdev->pci_dev_data;
65 struct pci_dev_entry *dev_entry, *t;
66 struct pci_dev *found_dev = NULL;
69 spin_lock_irqsave(&dev_data->lock, flags);
71 list_for_each_entry_safe(dev_entry, t, &dev_data->dev_list, list) {
72 if (dev_entry->dev == dev) {
73 list_del(&dev_entry->list);
74 found_dev = dev_entry->dev;
79 spin_unlock_irqrestore(&dev_data->lock, flags);
82 pcistub_put_pci_dev(found_dev);
85 int pciback_init_devices(struct pciback_device *pdev)
87 struct passthrough_dev_data *dev_data;
89 dev_data = kmalloc(sizeof(*dev_data), GFP_KERNEL);
93 spin_lock_init(&dev_data->lock);
95 INIT_LIST_HEAD(&dev_data->dev_list);
97 pdev->pci_dev_data = dev_data;
102 int pciback_publish_pci_roots(struct pciback_device *pdev,
103 publish_pci_root_cb publish_root_cb)
106 struct passthrough_dev_data *dev_data = pdev->pci_dev_data;
107 struct pci_dev_entry *dev_entry, *e;
110 unsigned int domain, bus;
112 spin_lock(&dev_data->lock);
114 list_for_each_entry(dev_entry, &dev_data->dev_list, list) {
115 /* Only publish this device as a root if none of its
116 * parent bridges are exported
119 dev = dev_entry->dev->bus->self;
120 for (; !found && dev != NULL; dev = dev->bus->self) {
121 list_for_each_entry(e, &dev_data->dev_list, list) {
129 domain = (unsigned int)pci_domain_nr(dev_entry->dev->bus);
130 bus = (unsigned int)dev_entry->dev->bus->number;
133 err = publish_root_cb(pdev, domain, bus);
139 spin_unlock(&dev_data->lock);
144 void pciback_release_devices(struct pciback_device *pdev)
146 struct passthrough_dev_data *dev_data = pdev->pci_dev_data;
147 struct pci_dev_entry *dev_entry, *t;
149 list_for_each_entry_safe(dev_entry, t, &dev_data->dev_list, list) {
150 list_del(&dev_entry->list);
151 pcistub_put_pci_dev(dev_entry->dev);
156 pdev->pci_dev_data = NULL;