2 * PCI Backend Xenbus Setup - handles setup with frontend and xend
4 * Author: Ryan Wilson <hap9@epoch.ncsc.mil>
6 #include <linux/module.h>
7 #include <linux/init.h>
8 #include <linux/list.h>
9 #include <linux/vmalloc.h>
10 #include <xen/xenbus.h>
11 #include <xen/evtchn.h>
14 #define INVALID_EVTCHN_IRQ (-1)
16 static struct pciback_device *alloc_pdev(struct xenbus_device *xdev)
18 struct pciback_device *pdev;
20 pdev = kzalloc(sizeof(struct pciback_device), GFP_KERNEL);
23 dev_dbg(&xdev->dev, "allocated pdev @ 0x%p\n", pdev);
26 xdev->dev.driver_data = pdev;
28 spin_lock_init(&pdev->dev_lock);
32 pdev->evtchn_irq = INVALID_EVTCHN_IRQ;
33 pdev->be_watching = 0;
35 INIT_WORK(&pdev->op_work, pciback_do_op);
37 if (pciback_init_devices(pdev)) {
45 static void free_pdev(struct pciback_device *pdev)
47 if (pdev->be_watching)
48 unregister_xenbus_watch(&pdev->be_watch);
50 /* Ensure the guest can't trigger our handler before removing devices */
51 if (pdev->evtchn_irq != INVALID_EVTCHN_IRQ)
52 unbind_from_irqhandler(pdev->evtchn_irq, pdev);
54 /* If the driver domain started an op, make sure we complete it or
55 * delete it before releasing the shared memory */
56 flush_scheduled_work();
59 xenbus_unmap_ring_vfree(pdev->xdev, pdev->sh_area);
61 pciback_release_devices(pdev);
63 pdev->xdev->dev.driver_data = NULL;
69 static int pciback_do_attach(struct pciback_device *pdev, int gnt_ref,
73 struct vm_struct *area;
75 dev_dbg(&pdev->xdev->dev,
76 "Attaching to frontend resources - gnt_ref=%d evtchn=%d\n",
77 gnt_ref, remote_evtchn);
79 area = xenbus_map_ring_valloc(pdev->xdev, gnt_ref);
85 pdev->sh_info = area->addr;
87 err = bind_interdomain_evtchn_to_irqhandler(
88 pdev->xdev->otherend_id, remote_evtchn, pciback_handle_event,
89 SA_SAMPLE_RANDOM, "pciback", pdev);
91 xenbus_dev_fatal(pdev->xdev, err,
92 "Error binding event channel to IRQ");
95 pdev->evtchn_irq = err;
98 dev_dbg(&pdev->xdev->dev, "Attached!\n");
103 static int pciback_attach(struct pciback_device *pdev)
106 int gnt_ref, remote_evtchn;
109 spin_lock(&pdev->dev_lock);
111 /* Make sure we only do this setup once */
112 if (xenbus_read_driver_state(pdev->xdev->nodename) !=
113 XenbusStateInitialised)
116 /* Wait for frontend to state that it has published the configuration */
117 if (xenbus_read_driver_state(pdev->xdev->otherend) !=
118 XenbusStateInitialised)
121 dev_dbg(&pdev->xdev->dev, "Reading frontend config\n");
123 err = xenbus_gather(XBT_NIL, pdev->xdev->otherend,
124 "pci-op-ref", "%u", &gnt_ref,
125 "event-channel", "%u", &remote_evtchn,
126 "magic", NULL, &magic, NULL);
128 /* If configuration didn't get read correctly, wait longer */
129 xenbus_dev_fatal(pdev->xdev, err,
130 "Error reading configuration from frontend");
134 if (magic == NULL || strcmp(magic, XEN_PCI_MAGIC) != 0) {
135 xenbus_dev_fatal(pdev->xdev, -EFAULT,
136 "version mismatch (%s/%s) with pcifront - "
138 magic, XEN_PCI_MAGIC);
142 err = pciback_do_attach(pdev, gnt_ref, remote_evtchn);
146 dev_dbg(&pdev->xdev->dev, "Connecting...\n");
148 err = xenbus_switch_state(pdev->xdev, XenbusStateConnected);
150 xenbus_dev_fatal(pdev->xdev, err,
151 "Error switching to connected state!");
153 dev_dbg(&pdev->xdev->dev, "Connected? %d\n", err);
155 spin_unlock(&pdev->dev_lock);
163 static void pciback_frontend_changed(struct xenbus_device *xdev,
164 enum xenbus_state fe_state)
166 struct pciback_device *pdev = xdev->dev.driver_data;
168 dev_dbg(&xdev->dev, "fe state changed %d\n", fe_state);
171 case XenbusStateInitialised:
172 pciback_attach(pdev);
175 case XenbusStateClosing:
176 xenbus_switch_state(xdev, XenbusStateClosing);
179 case XenbusStateUnknown:
180 case XenbusStateClosed:
181 dev_dbg(&xdev->dev, "frontend is gone! unregister device\n");
182 device_unregister(&xdev->dev);
190 static int pciback_publish_pci_root(struct pciback_device *pdev,
191 unsigned int domain, unsigned int bus)
194 int i, root_num, len, err;
197 dev_dbg(&pdev->xdev->dev, "Publishing pci roots\n");
199 err = xenbus_scanf(XBT_NIL, pdev->xdev->nodename,
200 "root_num", "%d", &root_num);
201 if (err == 0 || err == -ENOENT)
206 /* Verify that we haven't already published this pci root */
207 for (i = 0; i < root_num; i++) {
208 len = snprintf(str, sizeof(str), "root-%d", i);
209 if (unlikely(len >= (sizeof(str) - 1))) {
214 err = xenbus_scanf(XBT_NIL, pdev->xdev->nodename,
215 str, "%x:%x", &d, &b);
223 if (d == domain && b == bus) {
229 len = snprintf(str, sizeof(str), "root-%d", root_num);
230 if (unlikely(len >= (sizeof(str) - 1))) {
235 dev_dbg(&pdev->xdev->dev, "writing root %d at %04x:%02x\n",
236 root_num, domain, bus);
238 err = xenbus_printf(XBT_NIL, pdev->xdev->nodename, str,
239 "%04x:%02x", domain, bus);
243 err = xenbus_printf(XBT_NIL, pdev->xdev->nodename,
244 "root_num", "%d", (root_num + 1));
250 static int pciback_export_device(struct pciback_device *pdev,
251 int domain, int bus, int slot, int func)
256 dev_dbg(&pdev->xdev->dev, "exporting dom %x bus %x slot %x func %x\n",
257 domain, bus, slot, func);
259 dev = pcistub_get_pci_dev_by_slot(pdev, domain, bus, slot, func);
262 xenbus_dev_fatal(pdev->xdev, err,
263 "Couldn't locate PCI device "
264 "(%04x:%02x:%02x.%01x)! "
265 "perhaps already in-use?",
266 domain, bus, slot, func);
270 err = pciback_add_pci_dev(pdev, dev);
274 /* TODO: It'd be nice to export a bridge and have all of its children
275 * get exported with it. This may be best done in xend (which will
276 * have to calculate resource usage anyway) but we probably want to
277 * put something in here to ensure that if a bridge gets given to a
278 * driver domain, that all devices under that bridge are not given
279 * to other driver domains (as he who controls the bridge can disable
280 * it and stop the other devices from working).
286 static int pciback_setup_backend(struct pciback_device *pdev)
288 /* Get configuration from xend (if available now) */
289 int domain, bus, slot, func;
294 spin_lock(&pdev->dev_lock);
296 /* It's possible we could get the call to setup twice, so make sure
297 * we're not already connected.
299 if (xenbus_read_driver_state(pdev->xdev->nodename) !=
303 dev_dbg(&pdev->xdev->dev, "getting be setup\n");
305 err = xenbus_scanf(XBT_NIL, pdev->xdev->nodename, "num_devs", "%d",
310 xenbus_dev_fatal(pdev->xdev, err,
311 "Error reading number of devices");
315 for (i = 0; i < num_devs; i++) {
316 int l = snprintf(dev_str, sizeof(dev_str), "dev-%d", i);
317 if (unlikely(l >= (sizeof(dev_str) - 1))) {
319 xenbus_dev_fatal(pdev->xdev, err,
320 "String overflow while reading "
325 err = xenbus_scanf(XBT_NIL, pdev->xdev->nodename, dev_str,
326 "%x:%x:%x.%x", &domain, &bus, &slot, &func);
328 xenbus_dev_fatal(pdev->xdev, err,
329 "Error reading device configuration");
334 xenbus_dev_fatal(pdev->xdev, err,
335 "Error parsing pci device "
340 err = pciback_export_device(pdev, domain, bus, slot, func);
345 err = pciback_publish_pci_roots(pdev, pciback_publish_pci_root);
347 xenbus_dev_fatal(pdev->xdev, err,
348 "Error while publish PCI root buses "
353 err = xenbus_switch_state(pdev->xdev, XenbusStateInitialised);
355 xenbus_dev_fatal(pdev->xdev, err,
356 "Error switching to initialised state!");
359 spin_unlock(&pdev->dev_lock);
362 /* see if pcifront is already configured (if not, we'll wait) */
363 pciback_attach(pdev);
368 static void pciback_be_watch(struct xenbus_watch *watch,
369 const char **vec, unsigned int len)
371 struct pciback_device *pdev =
372 container_of(watch, struct pciback_device, be_watch);
374 switch (xenbus_read_driver_state(pdev->xdev->nodename)) {
375 case XenbusStateInitWait:
376 pciback_setup_backend(pdev);
384 static int pciback_xenbus_probe(struct xenbus_device *dev,
385 const struct xenbus_device_id *id)
388 struct pciback_device *pdev = alloc_pdev(dev);
392 xenbus_dev_fatal(dev, err,
393 "Error allocating pciback_device struct");
397 /* wait for xend to configure us */
398 err = xenbus_switch_state(dev, XenbusStateInitWait);
402 /* watch the backend node for backend configuration information */
403 err = xenbus_watch_path(dev, dev->nodename, &pdev->be_watch,
407 pdev->be_watching = 1;
409 /* We need to force a call to our callback here in case
410 * xend already configured us!
412 pciback_be_watch(&pdev->be_watch, NULL, 0);
418 static int pciback_xenbus_remove(struct xenbus_device *dev)
420 struct pciback_device *pdev = dev->dev.driver_data;
428 static struct xenbus_device_id xenpci_ids[] = {
433 static struct xenbus_driver xenbus_pciback_driver = {
435 .owner = THIS_MODULE,
437 .probe = pciback_xenbus_probe,
438 .remove = pciback_xenbus_remove,
439 .otherend_changed = pciback_frontend_changed,
442 int __init pciback_xenbus_register(void)
444 if (!is_running_on_xen())
447 return xenbus_register_backend(&xenbus_pciback_driver);
450 void __exit pciback_xenbus_unregister(void)
452 xenbus_unregister_driver(&xenbus_pciback_driver);