coredump: use task comm instead of (unknown)
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / xen / xen-pciback / vpci.c
blob4a42cfb0959d4f5dbcf6268e19e0b0a61388b9e0
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 #include <linux/list.h>
9 #include <linux/slab.h>
10 #include <linux/pci.h>
11 #include <linux/spinlock.h>
12 #include "pciback.h"
14 #define PCI_SLOT_MAX 32
15 #define DRV_NAME "xen-pciback"
17 struct vpci_dev_data {
18 /* Access to dev_list must be protected by lock */
19 struct list_head dev_list[PCI_SLOT_MAX];
20 spinlock_t lock;
23 static inline struct list_head *list_first(struct list_head *head)
25 return head->next;
28 static struct pci_dev *__xen_pcibk_get_pci_dev(struct xen_pcibk_device *pdev,
29 unsigned int domain,
30 unsigned int bus,
31 unsigned int devfn)
33 struct pci_dev_entry *entry;
34 struct pci_dev *dev = NULL;
35 struct vpci_dev_data *vpci_dev = pdev->pci_dev_data;
36 unsigned long flags;
38 if (domain != 0 || bus != 0)
39 return NULL;
41 if (PCI_SLOT(devfn) < PCI_SLOT_MAX) {
42 spin_lock_irqsave(&vpci_dev->lock, flags);
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 spin_unlock_irqrestore(&vpci_dev->lock, flags);
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;
74 unsigned long flags;
76 if ((dev->class >> 24) == PCI_BASE_CLASS_BRIDGE) {
77 err = -EFAULT;
78 xenbus_dev_fatal(pdev->xdev, err,
79 "Can't export bridges on the virtual PCI bus");
80 goto out;
83 dev_entry = kmalloc(sizeof(*dev_entry), GFP_KERNEL);
84 if (!dev_entry) {
85 err = -ENOMEM;
86 xenbus_dev_fatal(pdev->xdev, err,
87 "Error adding entry to virtual PCI bus");
88 goto out;
91 dev_entry->dev = dev;
93 spin_lock_irqsave(&vpci_dev->lock, flags);
95 /* Keep multi-function devices together on the virtual PCI bus */
96 for (slot = 0; slot < PCI_SLOT_MAX; slot++) {
97 if (!list_empty(&vpci_dev->dev_list[slot])) {
98 t = list_entry(list_first(&vpci_dev->dev_list[slot]),
99 struct pci_dev_entry, list);
101 if (match_slot(dev, t->dev)) {
102 pr_info(DRV_NAME ": vpci: %s: "
103 "assign to virtual slot %d func %d\n",
104 pci_name(dev), slot,
105 PCI_FUNC(dev->devfn));
106 list_add_tail(&dev_entry->list,
107 &vpci_dev->dev_list[slot]);
108 func = PCI_FUNC(dev->devfn);
109 goto unlock;
114 /* Assign to a new slot on the virtual PCI bus */
115 for (slot = 0; slot < PCI_SLOT_MAX; slot++) {
116 if (list_empty(&vpci_dev->dev_list[slot])) {
117 printk(KERN_INFO DRV_NAME
118 ": vpci: %s: assign to virtual slot %d\n",
119 pci_name(dev), slot);
120 list_add_tail(&dev_entry->list,
121 &vpci_dev->dev_list[slot]);
122 func = PCI_FUNC(dev->devfn);
123 goto unlock;
127 err = -ENOMEM;
128 xenbus_dev_fatal(pdev->xdev, err,
129 "No more space on root virtual PCI bus");
131 unlock:
132 spin_unlock_irqrestore(&vpci_dev->lock, flags);
134 /* Publish this device. */
135 if (!err)
136 err = publish_cb(pdev, 0, 0, PCI_DEVFN(slot, func), devid);
138 out:
139 return err;
142 static void __xen_pcibk_release_pci_dev(struct xen_pcibk_device *pdev,
143 struct pci_dev *dev)
145 int slot;
146 struct vpci_dev_data *vpci_dev = pdev->pci_dev_data;
147 struct pci_dev *found_dev = NULL;
148 unsigned long flags;
150 spin_lock_irqsave(&vpci_dev->lock, flags);
152 for (slot = 0; slot < PCI_SLOT_MAX; slot++) {
153 struct pci_dev_entry *e, *tmp;
154 list_for_each_entry_safe(e, tmp, &vpci_dev->dev_list[slot],
155 list) {
156 if (e->dev == dev) {
157 list_del(&e->list);
158 found_dev = e->dev;
159 kfree(e);
160 goto out;
165 out:
166 spin_unlock_irqrestore(&vpci_dev->lock, flags);
168 if (found_dev)
169 pcistub_put_pci_dev(found_dev);
172 static int __xen_pcibk_init_devices(struct xen_pcibk_device *pdev)
174 int slot;
175 struct vpci_dev_data *vpci_dev;
177 vpci_dev = kmalloc(sizeof(*vpci_dev), GFP_KERNEL);
178 if (!vpci_dev)
179 return -ENOMEM;
181 spin_lock_init(&vpci_dev->lock);
183 for (slot = 0; slot < PCI_SLOT_MAX; slot++)
184 INIT_LIST_HEAD(&vpci_dev->dev_list[slot]);
186 pdev->pci_dev_data = vpci_dev;
188 return 0;
191 static int __xen_pcibk_publish_pci_roots(struct xen_pcibk_device *pdev,
192 publish_pci_root_cb publish_cb)
194 /* The Virtual PCI bus has only one root */
195 return publish_cb(pdev, 0, 0);
198 static void __xen_pcibk_release_devices(struct xen_pcibk_device *pdev)
200 int slot;
201 struct vpci_dev_data *vpci_dev = pdev->pci_dev_data;
203 for (slot = 0; slot < PCI_SLOT_MAX; slot++) {
204 struct pci_dev_entry *e, *tmp;
205 list_for_each_entry_safe(e, tmp, &vpci_dev->dev_list[slot],
206 list) {
207 list_del(&e->list);
208 pcistub_put_pci_dev(e->dev);
209 kfree(e);
213 kfree(vpci_dev);
214 pdev->pci_dev_data = NULL;
217 static int __xen_pcibk_get_pcifront_dev(struct pci_dev *pcidev,
218 struct xen_pcibk_device *pdev,
219 unsigned int *domain, unsigned int *bus,
220 unsigned int *devfn)
222 struct pci_dev_entry *entry;
223 struct pci_dev *dev = NULL;
224 struct vpci_dev_data *vpci_dev = pdev->pci_dev_data;
225 unsigned long flags;
226 int found = 0, slot;
228 spin_lock_irqsave(&vpci_dev->lock, flags);
229 for (slot = 0; slot < PCI_SLOT_MAX; slot++) {
230 list_for_each_entry(entry,
231 &vpci_dev->dev_list[slot],
232 list) {
233 dev = entry->dev;
234 if (dev && dev->bus->number == pcidev->bus->number
235 && pci_domain_nr(dev->bus) ==
236 pci_domain_nr(pcidev->bus)
237 && dev->devfn == pcidev->devfn) {
238 found = 1;
239 *domain = 0;
240 *bus = 0;
241 *devfn = PCI_DEVFN(slot,
242 PCI_FUNC(pcidev->devfn));
246 spin_unlock_irqrestore(&vpci_dev->lock, flags);
247 return found;
250 struct xen_pcibk_backend xen_pcibk_vpci_backend = {
251 .name = "vpci",
252 .init = __xen_pcibk_init_devices,
253 .free = __xen_pcibk_release_devices,
254 .find = __xen_pcibk_get_pcifront_dev,
255 .publish = __xen_pcibk_publish_pci_roots,
256 .release = __xen_pcibk_release_pci_dev,
257 .add = __xen_pcibk_add_pci_dev,
258 .get = __xen_pcibk_get_pci_dev,