2 * PCI Backend - Provides a Virtual PCI bus (with real devices)
5 * Author: Ryan Wilson <hap9@epoch.ncsc.mil>
8 #include <linux/list.h>
9 #include <linux/slab.h>
10 #include <linux/pci.h>
11 #include <linux/spinlock.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
];
23 static inline struct list_head
*list_first(struct list_head
*head
)
28 static struct pci_dev
*__xen_pcibk_get_pci_dev(struct xen_pcibk_device
*pdev
,
33 struct pci_dev_entry
*entry
;
34 struct pci_dev
*dev
= NULL
;
35 struct vpci_dev_data
*vpci_dev
= pdev
->pci_dev_data
;
38 if (domain
!= 0 || bus
!= 0)
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
)],
47 if (PCI_FUNC(entry
->dev
->devfn
) == PCI_FUNC(devfn
)) {
53 spin_unlock_irqrestore(&vpci_dev
->lock
, flags
);
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
))
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
;
76 if ((dev
->class >> 24) == PCI_BASE_CLASS_BRIDGE
) {
78 xenbus_dev_fatal(pdev
->xdev
, err
,
79 "Can't export bridges on the virtual PCI bus");
83 dev_entry
= kmalloc(sizeof(*dev_entry
), GFP_KERNEL
);
86 xenbus_dev_fatal(pdev
->xdev
, err
,
87 "Error adding entry to virtual PCI bus");
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",
105 PCI_FUNC(dev
->devfn
));
106 list_add_tail(&dev_entry
->list
,
107 &vpci_dev
->dev_list
[slot
]);
108 func
= PCI_FUNC(dev
->devfn
);
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
);
128 xenbus_dev_fatal(pdev
->xdev
, err
,
129 "No more space on root virtual PCI bus");
132 spin_unlock_irqrestore(&vpci_dev
->lock
, flags
);
134 /* Publish this device. */
136 err
= publish_cb(pdev
, 0, 0, PCI_DEVFN(slot
, func
), devid
);
142 static void __xen_pcibk_release_pci_dev(struct xen_pcibk_device
*pdev
,
146 struct vpci_dev_data
*vpci_dev
= pdev
->pci_dev_data
;
147 struct pci_dev
*found_dev
= NULL
;
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
],
166 spin_unlock_irqrestore(&vpci_dev
->lock
, flags
);
169 pcistub_put_pci_dev(found_dev
);
172 static int __xen_pcibk_init_devices(struct xen_pcibk_device
*pdev
)
175 struct vpci_dev_data
*vpci_dev
;
177 vpci_dev
= kmalloc(sizeof(*vpci_dev
), GFP_KERNEL
);
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
;
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
)
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
],
208 pcistub_put_pci_dev(e
->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
,
222 struct pci_dev_entry
*entry
;
223 struct pci_dev
*dev
= NULL
;
224 struct vpci_dev_data
*vpci_dev
= pdev
->pci_dev_data
;
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
],
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
) {
241 *devfn
= PCI_DEVFN(slot
,
242 PCI_FUNC(pcidev
->devfn
));
246 spin_unlock_irqrestore(&vpci_dev
->lock
, flags
);
250 struct xen_pcibk_backend xen_pcibk_vpci_backend
= {
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
,