This commit was manufactured by cvs2svn to create branch 'vserver'.
[linux-2.6.git] / drivers / xen / pciback / passthrough.c
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
13 struct 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
19 struct 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
44 int pciback_add_pci_dev(struct pciback_device *pdev, struct pci_dev *dev)
45 {
46         struct passthrough_dev_data *dev_data = pdev->pci_dev_data;
47         struct pci_dev_entry *dev_entry;
48         unsigned long flags;
49
50         dev_entry = kmalloc(sizeof(*dev_entry), GFP_KERNEL);
51         if (!dev_entry)
52                 return -ENOMEM;
53         dev_entry->dev = dev;
54
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);
58
59         return 0;
60 }
61
62 void pciback_release_pci_dev(struct pciback_device *pdev, struct pci_dev *dev)
63 {
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;
67         unsigned long flags;
68
69         spin_lock_irqsave(&dev_data->lock, flags);
70
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;
75                         kfree(dev_entry);
76                 }
77         }
78
79         spin_unlock_irqrestore(&dev_data->lock, flags);
80
81         if (found_dev)
82                 pcistub_put_pci_dev(found_dev);
83 }
84
85 int pciback_init_devices(struct pciback_device *pdev)
86 {
87         struct passthrough_dev_data *dev_data;
88
89         dev_data = kmalloc(sizeof(*dev_data), GFP_KERNEL);
90         if (!dev_data)
91                 return -ENOMEM;
92
93         spin_lock_init(&dev_data->lock);
94
95         INIT_LIST_HEAD(&dev_data->dev_list);
96
97         pdev->pci_dev_data = dev_data;
98
99         return 0;
100 }
101
102 int pciback_publish_pci_roots(struct pciback_device *pdev,
103                               publish_pci_root_cb publish_root_cb)
104 {
105         int err = 0;
106         struct passthrough_dev_data *dev_data = pdev->pci_dev_data;
107         struct pci_dev_entry *dev_entry, *e;
108         struct pci_dev *dev;
109         int found;
110         unsigned int domain, bus;
111
112         spin_lock(&dev_data->lock);
113
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
117                  */
118                 found = 0;
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) {
122                                 if (dev == e->dev) {
123                                         found = 1;
124                                         break;
125                                 }
126                         }
127                 }
128
129                 domain = (unsigned int)pci_domain_nr(dev_entry->dev->bus);
130                 bus = (unsigned int)dev_entry->dev->bus->number;
131
132                 if (!found) {
133                         err = publish_root_cb(pdev, domain, bus);
134                         if (err)
135                                 break;
136                 }
137         }
138
139         spin_unlock(&dev_data->lock);
140
141         return err;
142 }
143
144 void pciback_release_devices(struct pciback_device *pdev)
145 {
146         struct passthrough_dev_data *dev_data = pdev->pci_dev_data;
147         struct pci_dev_entry *dev_entry, *t;
148
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);
152                 kfree(dev_entry);
153         }
154
155         kfree(dev_data);
156         pdev->pci_dev_data = NULL;
157 }