sched: Remove double_rq_lock() from __migrate_task()
[linux/fpc-iii.git] / drivers / xen / xen-pciback / vpci.c
blob51afff96c515c05b6722937188a8fd317639217f
1 /*
2 * PCI Backend - Provides a Virtual PCI bus (with real devices)
3 * to the frontend
5 * Author: Ryan Wilson <hap9@epoch.ncsc.mil>
6 */
8 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
10 #include <linux/list.h>
11 #include <linux/slab.h>
12 #include <linux/pci.h>
13 #include <linux/mutex.h>
14 #include "pciback.h"
16 #define PCI_SLOT_MAX 32
18 struct vpci_dev_data {
19 /* Access to dev_list must be protected by lock */
20 struct list_head dev_list[PCI_SLOT_MAX];
21 struct mutex lock;
24 static inline struct list_head *list_first(struct list_head *head)
26 return head->next;
29 static struct pci_dev *__xen_pcibk_get_pci_dev(struct xen_pcibk_device *pdev,
30 unsigned int domain,
31 unsigned int bus,
32 unsigned int devfn)
34 struct pci_dev_entry *entry;
35 struct pci_dev *dev = NULL;
36 struct vpci_dev_data *vpci_dev = pdev->pci_dev_data;
38 if (domain != 0 || bus != 0)
39 return NULL;
41 if (PCI_SLOT(devfn) < PCI_SLOT_MAX) {
42 mutex_lock(&vpci_dev->lock);
44 list_for_each_entry(entry,
45 &vpci_dev->dev_list[PCI_SLOT(devfn)],
46 list) {
47 if (PCI_FUNC(entry->dev->devfn) == PCI_FUNC(devfn)) {
48 dev = entry->dev;
49 break;
53 mutex_unlock(&vpci_dev->lock);
55 return dev;
58 static inline int match_slot(struct pci_dev *l, struct pci_dev *r)
60 if (pci_domain_nr(l->bus) == pci_domain_nr(r->bus)
61 && l->bus == r->bus && PCI_SLOT(l->devfn) == PCI_SLOT(r->devfn))
62 return 1;
64 return 0;
67 static int __xen_pcibk_add_pci_dev(struct xen_pcibk_device *pdev,
68 struct pci_dev *dev, int devid,
69 publish_pci_dev_cb publish_cb)
71 int err = 0, slot, func = -1;
72 struct pci_dev_entry *t, *dev_entry;
73 struct vpci_dev_data *vpci_dev = pdev->pci_dev_data;
75 if ((dev->class >> 24) == PCI_BASE_CLASS_BRIDGE) {
76 err = -EFAULT;
77 xenbus_dev_fatal(pdev->xdev, err,
78 "Can't export bridges on the virtual PCI bus");
79 goto out;
82 dev_entry = kmalloc(sizeof(*dev_entry), GFP_KERNEL);
83 if (!dev_entry) {
84 err = -ENOMEM;
85 xenbus_dev_fatal(pdev->xdev, err,
86 "Error adding entry to virtual PCI bus");
87 goto out;
90 dev_entry->dev = dev;
92 mutex_lock(&vpci_dev->lock);
95 * Keep multi-function devices together on the virtual PCI bus, except
96 * virtual functions.
98 if (!dev->is_virtfn) {
99 for (slot = 0; slot < PCI_SLOT_MAX; slot++) {
100 if (list_empty(&vpci_dev->dev_list[slot]))
101 continue;
103 t = list_entry(list_first(&vpci_dev->dev_list[slot]),
104 struct pci_dev_entry, list);
106 if (match_slot(dev, t->dev)) {
107 pr_info("vpci: %s: assign to virtual slot %d func %d\n",
108 pci_name(dev), slot,
109 PCI_FUNC(dev->devfn));
110 list_add_tail(&dev_entry->list,
111 &vpci_dev->dev_list[slot]);
112 func = PCI_FUNC(dev->devfn);
113 goto unlock;
118 /* Assign to a new slot on the virtual PCI bus */
119 for (slot = 0; slot < PCI_SLOT_MAX; slot++) {
120 if (list_empty(&vpci_dev->dev_list[slot])) {
121 pr_info("vpci: %s: assign to virtual slot %d\n",
122 pci_name(dev), slot);
123 list_add_tail(&dev_entry->list,
124 &vpci_dev->dev_list[slot]);
125 func = dev->is_virtfn ? 0 : PCI_FUNC(dev->devfn);
126 goto unlock;
130 err = -ENOMEM;
131 xenbus_dev_fatal(pdev->xdev, err,
132 "No more space on root virtual PCI bus");
134 unlock:
135 mutex_unlock(&vpci_dev->lock);
137 /* Publish this device. */
138 if (!err)
139 err = publish_cb(pdev, 0, 0, PCI_DEVFN(slot, func), devid);
140 else
141 kfree(dev_entry);
143 out:
144 return err;
147 static void __xen_pcibk_release_pci_dev(struct xen_pcibk_device *pdev,
148 struct pci_dev *dev)
150 int slot;
151 struct vpci_dev_data *vpci_dev = pdev->pci_dev_data;
152 struct pci_dev *found_dev = NULL;
154 mutex_lock(&vpci_dev->lock);
156 for (slot = 0; slot < PCI_SLOT_MAX; slot++) {
157 struct pci_dev_entry *e;
159 list_for_each_entry(e, &vpci_dev->dev_list[slot], list) {
160 if (e->dev == dev) {
161 list_del(&e->list);
162 found_dev = e->dev;
163 kfree(e);
164 goto out;
169 out:
170 mutex_unlock(&vpci_dev->lock);
172 if (found_dev)
173 pcistub_put_pci_dev(found_dev);
176 static int __xen_pcibk_init_devices(struct xen_pcibk_device *pdev)
178 int slot;
179 struct vpci_dev_data *vpci_dev;
181 vpci_dev = kmalloc(sizeof(*vpci_dev), GFP_KERNEL);
182 if (!vpci_dev)
183 return -ENOMEM;
185 mutex_init(&vpci_dev->lock);
187 for (slot = 0; slot < PCI_SLOT_MAX; slot++)
188 INIT_LIST_HEAD(&vpci_dev->dev_list[slot]);
190 pdev->pci_dev_data = vpci_dev;
192 return 0;
195 static int __xen_pcibk_publish_pci_roots(struct xen_pcibk_device *pdev,
196 publish_pci_root_cb publish_cb)
198 /* The Virtual PCI bus has only one root */
199 return publish_cb(pdev, 0, 0);
202 static void __xen_pcibk_release_devices(struct xen_pcibk_device *pdev)
204 int slot;
205 struct vpci_dev_data *vpci_dev = pdev->pci_dev_data;
207 for (slot = 0; slot < PCI_SLOT_MAX; slot++) {
208 struct pci_dev_entry *e, *tmp;
209 list_for_each_entry_safe(e, tmp, &vpci_dev->dev_list[slot],
210 list) {
211 list_del(&e->list);
212 pcistub_put_pci_dev(e->dev);
213 kfree(e);
217 kfree(vpci_dev);
218 pdev->pci_dev_data = NULL;
221 static int __xen_pcibk_get_pcifront_dev(struct pci_dev *pcidev,
222 struct xen_pcibk_device *pdev,
223 unsigned int *domain, unsigned int *bus,
224 unsigned int *devfn)
226 struct pci_dev_entry *entry;
227 struct pci_dev *dev = NULL;
228 struct vpci_dev_data *vpci_dev = pdev->pci_dev_data;
229 int found = 0, slot;
231 mutex_lock(&vpci_dev->lock);
232 for (slot = 0; slot < PCI_SLOT_MAX; slot++) {
233 list_for_each_entry(entry,
234 &vpci_dev->dev_list[slot],
235 list) {
236 dev = entry->dev;
237 if (dev && dev->bus->number == pcidev->bus->number
238 && pci_domain_nr(dev->bus) ==
239 pci_domain_nr(pcidev->bus)
240 && dev->devfn == pcidev->devfn) {
241 found = 1;
242 *domain = 0;
243 *bus = 0;
244 *devfn = PCI_DEVFN(slot,
245 PCI_FUNC(pcidev->devfn));
249 mutex_unlock(&vpci_dev->lock);
250 return found;
253 const struct xen_pcibk_backend xen_pcibk_vpci_backend = {
254 .name = "vpci",
255 .init = __xen_pcibk_init_devices,
256 .free = __xen_pcibk_release_devices,
257 .find = __xen_pcibk_get_pcifront_dev,
258 .publish = __xen_pcibk_publish_pci_roots,
259 .release = __xen_pcibk_release_pci_dev,
260 .add = __xen_pcibk_add_pci_dev,
261 .get = __xen_pcibk_get_pci_dev,