]> git.proxmox.com Git - mirror_ubuntu-zesty-kernel.git/blame - drivers/xen/xen-pciback/passthrough.c
xen/pciback: Fine-grain the spinlocks and fix BUG: scheduling while atomic cases.
[mirror_ubuntu-zesty-kernel.git] / drivers / xen / xen-pciback / passthrough.c
CommitLineData
30edc14b
KRW
1/*
2 * PCI Backend - Provides restricted access to the real PCI bus topology
3 * to the frontend
4 *
5 * Author: Ryan Wilson <hap9@epoch.ncsc.mil>
6 */
7
8#include <linux/list.h>
9#include <linux/pci.h>
10#include <linux/spinlock.h>
11#include "pciback.h"
12
13struct passthrough_dev_data {
14 /* Access to dev_list must be protected by lock */
15 struct list_head dev_list;
16 spinlock_t lock;
17};
18
19struct pci_dev *pciback_get_pci_dev(struct pciback_device *pdev,
20 unsigned int domain, unsigned int bus,
21 unsigned int devfn)
22{
23 struct passthrough_dev_data *dev_data = pdev->pci_dev_data;
24 struct pci_dev_entry *dev_entry;
25 struct pci_dev *dev = NULL;
26 unsigned long flags;
27
28 spin_lock_irqsave(&dev_data->lock, flags);
29
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) {
34 dev = dev_entry->dev;
35 break;
36 }
37 }
38
39 spin_unlock_irqrestore(&dev_data->lock, flags);
40
41 return dev;
42}
43
44int pciback_add_pci_dev(struct pciback_device *pdev, struct pci_dev *dev,
45 int devid, publish_pci_dev_cb publish_cb)
46{
47 struct passthrough_dev_data *dev_data = pdev->pci_dev_data;
48 struct pci_dev_entry *dev_entry;
49 unsigned long flags;
50 unsigned int domain, bus, devfn;
51 int err;
52
53 dev_entry = kmalloc(sizeof(*dev_entry), GFP_KERNEL);
54 if (!dev_entry)
55 return -ENOMEM;
56 dev_entry->dev = dev;
57
58 spin_lock_irqsave(&dev_data->lock, flags);
59 list_add_tail(&dev_entry->list, &dev_data->dev_list);
60 spin_unlock_irqrestore(&dev_data->lock, flags);
61
62 /* Publish this device. */
63 domain = (unsigned int)pci_domain_nr(dev->bus);
64 bus = (unsigned int)dev->bus->number;
65 devfn = dev->devfn;
66 err = publish_cb(pdev, domain, bus, devfn, devid);
67
68 return err;
69}
70
71void pciback_release_pci_dev(struct pciback_device *pdev, struct pci_dev *dev)
72{
73 struct passthrough_dev_data *dev_data = pdev->pci_dev_data;
74 struct pci_dev_entry *dev_entry, *t;
75 struct pci_dev *found_dev = NULL;
76 unsigned long flags;
77
78 spin_lock_irqsave(&dev_data->lock, flags);
79
80 list_for_each_entry_safe(dev_entry, t, &dev_data->dev_list, list) {
81 if (dev_entry->dev == dev) {
82 list_del(&dev_entry->list);
83 found_dev = dev_entry->dev;
84 kfree(dev_entry);
85 }
86 }
87
88 spin_unlock_irqrestore(&dev_data->lock, flags);
89
90 if (found_dev)
91 pcistub_put_pci_dev(found_dev);
92}
93
94int pciback_init_devices(struct pciback_device *pdev)
95{
96 struct passthrough_dev_data *dev_data;
97
98 dev_data = kmalloc(sizeof(*dev_data), GFP_KERNEL);
99 if (!dev_data)
100 return -ENOMEM;
101
102 spin_lock_init(&dev_data->lock);
103
104 INIT_LIST_HEAD(&dev_data->dev_list);
105
106 pdev->pci_dev_data = dev_data;
107
108 return 0;
109}
110
111int pciback_publish_pci_roots(struct pciback_device *pdev,
112 publish_pci_root_cb publish_root_cb)
113{
114 int err = 0;
115 struct passthrough_dev_data *dev_data = pdev->pci_dev_data;
494ef20d 116 struct pci_dev_entry *dev_entry, *e, *tmp;
30edc14b
KRW
117 struct pci_dev *dev;
118 int found;
119 unsigned int domain, bus;
120
121 spin_lock(&dev_data->lock);
122
494ef20d 123 list_for_each_entry_safe(dev_entry, tmp, &dev_data->dev_list, list) {
30edc14b
KRW
124 /* Only publish this device as a root if none of its
125 * parent bridges are exported
126 */
127 found = 0;
128 dev = dev_entry->dev->bus->self;
129 for (; !found && dev != NULL; dev = dev->bus->self) {
130 list_for_each_entry(e, &dev_data->dev_list, list) {
131 if (dev == e->dev) {
132 found = 1;
133 break;
134 }
135 }
136 }
137
138 domain = (unsigned int)pci_domain_nr(dev_entry->dev->bus);
139 bus = (unsigned int)dev_entry->dev->bus->number;
140
141 if (!found) {
494ef20d 142 spin_unlock(&dev_data->lock);
30edc14b
KRW
143 err = publish_root_cb(pdev, domain, bus);
144 if (err)
145 break;
494ef20d 146 spin_lock(&dev_data->lock);
30edc14b
KRW
147 }
148 }
149
494ef20d
KRW
150 if (!err)
151 spin_unlock(&dev_data->lock);
30edc14b
KRW
152
153 return err;
154}
155
156void pciback_release_devices(struct pciback_device *pdev)
157{
158 struct passthrough_dev_data *dev_data = pdev->pci_dev_data;
159 struct pci_dev_entry *dev_entry, *t;
160
161 list_for_each_entry_safe(dev_entry, t, &dev_data->dev_list, list) {
162 list_del(&dev_entry->list);
163 pcistub_put_pci_dev(dev_entry->dev);
164 kfree(dev_entry);
165 }
166
167 kfree(dev_data);
168 pdev->pci_dev_data = NULL;
169}
170
171int pciback_get_pcifront_dev(struct pci_dev *pcidev,
172 struct pciback_device *pdev,
173 unsigned int *domain, unsigned int *bus,
174 unsigned int *devfn)
175
176{
177 *domain = pci_domain_nr(pcidev->bus);
178 *bus = pcidev->bus->number;
179 *devfn = pcidev->devfn;
180 return 1;
181}