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 <linux/workqueue.h>
11 #include <xen/xenbus.h>
12 #include <xen/events.h>
13 #include <asm/xen/pci.h>
14 #include <linux/workqueue.h>
17 #define DRV_NAME "xen-pciback"
18 #define INVALID_EVTCHN_IRQ (-1)
19 struct workqueue_struct *xen_pcibk_wq;
21 static int __read_mostly passthrough;
22 module_param(passthrough, bool, S_IRUGO);
23 MODULE_PARM_DESC(passthrough,
24 "Option to specify how to export PCI topology to guest:\n"\
25 " 0 - (default) Hide the true PCI topology and makes the frontend\n"\
26 " there is a single PCI bus with only the exported devices on it.\n"\
27 " For example, a device at 03:05.0 will be re-assigned to 00:00.0\n"\
28 " while second device at 02:1a.1 will be re-assigned to 00:01.1.\n"\
29 " 1 - Passthrough provides a real view of the PCI topology to the\n"\
30 " frontend (for example, a device at 06:01.b will still appear at\n"\
31 " 06:01.b to the frontend). This is similar to how Xen 2.0.x\n"\
32 " exposed PCI devices to its driver domains. This may be required\n"\
33 " for drivers which depend on finding their hardward in certain\n"\
34 " bus/slot locations.");
36 static struct xen_pcibk_device *alloc_pdev(struct xenbus_device *xdev)
38 struct xen_pcibk_device *pdev;
40 pdev = kzalloc(sizeof(struct xen_pcibk_device), GFP_KERNEL);
43 dev_dbg(&xdev->dev, "allocated pdev @ 0x%p\n", pdev);
46 dev_set_drvdata(&xdev->dev, pdev);
48 spin_lock_init(&pdev->dev_lock);
51 pdev->evtchn_irq = INVALID_EVTCHN_IRQ;
52 pdev->be_watching = 0;
54 INIT_WORK(&pdev->op_work, xen_pcibk_do_op);
56 if (xen_pcibk_init_devices(pdev)) {
64 static void xen_pcibk_disconnect(struct xen_pcibk_device *pdev)
66 spin_lock(&pdev->dev_lock);
68 /* Ensure the guest can't trigger our handler before removing devices */
69 if (pdev->evtchn_irq != INVALID_EVTCHN_IRQ) {
70 unbind_from_irqhandler(pdev->evtchn_irq, pdev);
71 pdev->evtchn_irq = INVALID_EVTCHN_IRQ;
73 spin_unlock(&pdev->dev_lock);
75 /* If the driver domain started an op, make sure we complete it
76 * before releasing the shared memory */
78 /* Note, the workqueue does not use spinlocks at all.*/
79 flush_workqueue(xen_pcibk_wq);
81 spin_lock(&pdev->dev_lock);
82 if (pdev->sh_info != NULL) {
83 xenbus_unmap_ring_vfree(pdev->xdev, pdev->sh_info);
86 spin_unlock(&pdev->dev_lock);
90 static void free_pdev(struct xen_pcibk_device *pdev)
92 if (pdev->be_watching) {
93 unregister_xenbus_watch(&pdev->be_watch);
94 pdev->be_watching = 0;
97 xen_pcibk_disconnect(pdev);
99 xen_pcibk_release_devices(pdev);
101 dev_set_drvdata(&pdev->xdev->dev, NULL);
107 static int xen_pcibk_do_attach(struct xen_pcibk_device *pdev, int gnt_ref,
113 dev_dbg(&pdev->xdev->dev,
114 "Attaching to frontend resources - gnt_ref=%d evtchn=%d\n",
115 gnt_ref, remote_evtchn);
117 err = xenbus_map_ring_valloc(pdev->xdev, gnt_ref, &vaddr);
119 xenbus_dev_fatal(pdev->xdev, err,
120 "Error mapping other domain page in ours.");
124 spin_lock(&pdev->dev_lock);
125 pdev->sh_info = vaddr;
126 spin_unlock(&pdev->dev_lock);
128 err = bind_interdomain_evtchn_to_irqhandler(
129 pdev->xdev->otherend_id, remote_evtchn, xen_pcibk_handle_event,
132 xenbus_dev_fatal(pdev->xdev, err,
133 "Error binding event channel to IRQ");
137 spin_lock(&pdev->dev_lock);
138 pdev->evtchn_irq = err;
139 spin_unlock(&pdev->dev_lock);
142 dev_dbg(&pdev->xdev->dev, "Attached!\n");
147 static int xen_pcibk_attach(struct xen_pcibk_device *pdev)
150 int gnt_ref, remote_evtchn;
154 /* Make sure we only do this setup once */
155 if (xenbus_read_driver_state(pdev->xdev->nodename) !=
156 XenbusStateInitialised)
159 /* Wait for frontend to state that it has published the configuration */
160 if (xenbus_read_driver_state(pdev->xdev->otherend) !=
161 XenbusStateInitialised)
164 dev_dbg(&pdev->xdev->dev, "Reading frontend config\n");
166 err = xenbus_gather(XBT_NIL, pdev->xdev->otherend,
167 "pci-op-ref", "%u", &gnt_ref,
168 "event-channel", "%u", &remote_evtchn,
169 "magic", NULL, &magic, NULL);
171 /* If configuration didn't get read correctly, wait longer */
172 xenbus_dev_fatal(pdev->xdev, err,
173 "Error reading configuration from frontend");
177 if (magic == NULL || strcmp(magic, XEN_PCI_MAGIC) != 0) {
178 xenbus_dev_fatal(pdev->xdev, -EFAULT,
179 "version mismatch (%s/%s) with pcifront - "
181 magic, XEN_PCI_MAGIC);
185 err = xen_pcibk_do_attach(pdev, gnt_ref, remote_evtchn);
189 dev_dbg(&pdev->xdev->dev, "Connecting...\n");
191 err = xenbus_switch_state(pdev->xdev, XenbusStateConnected);
193 xenbus_dev_fatal(pdev->xdev, err,
194 "Error switching to connected state!");
196 dev_dbg(&pdev->xdev->dev, "Connected? %d\n", err);
204 static int xen_pcibk_publish_pci_dev(struct xen_pcibk_device *pdev,
205 unsigned int domain, unsigned int bus,
206 unsigned int devfn, unsigned int devid)
212 len = snprintf(str, sizeof(str), "vdev-%d", devid);
213 if (unlikely(len >= (sizeof(str) - 1))) {
218 err = xenbus_printf(XBT_NIL, pdev->xdev->nodename, str,
219 "%04x:%02x:%02x.%02x", domain, bus,
220 PCI_SLOT(devfn), PCI_FUNC(devfn));
226 static int xen_pcibk_export_device(struct xen_pcibk_device *pdev,
227 int domain, int bus, int slot, int func,
233 dev_dbg(&pdev->xdev->dev, "exporting dom %x bus %x slot %x func %x\n",
234 domain, bus, slot, func);
236 dev = pcistub_get_pci_dev_by_slot(pdev, domain, bus, slot, func);
239 xenbus_dev_fatal(pdev->xdev, err,
240 "Couldn't locate PCI device "
241 "(%04x:%02x:%02x.%01x)! "
242 "perhaps already in-use?",
243 domain, bus, slot, func);
247 err = xen_pcibk_add_pci_dev(pdev, dev, devid,
248 xen_pcibk_publish_pci_dev);
252 dev_dbg(&dev->dev, "registering for %d\n", pdev->xdev->otherend_id);
253 if (xen_register_device_domain_owner(dev,
254 pdev->xdev->otherend_id) != 0) {
255 dev_err(&dev->dev, "device has been assigned to another " \
256 "domain! Over-writting the ownership, but beware.\n");
257 xen_unregister_device_domain_owner(dev);
258 xen_register_device_domain_owner(dev, pdev->xdev->otherend_id);
261 /* TODO: It'd be nice to export a bridge and have all of its children
262 * get exported with it. This may be best done in xend (which will
263 * have to calculate resource usage anyway) but we probably want to
264 * put something in here to ensure that if a bridge gets given to a
265 * driver domain, that all devices under that bridge are not given
266 * to other driver domains (as he who controls the bridge can disable
267 * it and stop the other devices from working).
273 static int xen_pcibk_remove_device(struct xen_pcibk_device *pdev,
274 int domain, int bus, int slot, int func)
279 dev_dbg(&pdev->xdev->dev, "removing dom %x bus %x slot %x func %x\n",
280 domain, bus, slot, func);
282 dev = xen_pcibk_get_pci_dev(pdev, domain, bus, PCI_DEVFN(slot, func));
285 dev_dbg(&pdev->xdev->dev, "Couldn't locate PCI device "
286 "(%04x:%02x:%02x.%01x)! not owned by this domain\n",
287 domain, bus, slot, func);
291 dev_dbg(&dev->dev, "unregistering for %d\n", pdev->xdev->otherend_id);
292 xen_unregister_device_domain_owner(dev);
294 xen_pcibk_release_pci_dev(pdev, dev);
300 static int xen_pcibk_publish_pci_root(struct xen_pcibk_device *pdev,
301 unsigned int domain, unsigned int bus)
304 int i, root_num, len, err;
307 dev_dbg(&pdev->xdev->dev, "Publishing pci roots\n");
309 err = xenbus_scanf(XBT_NIL, pdev->xdev->nodename,
310 "root_num", "%d", &root_num);
311 if (err == 0 || err == -ENOENT)
316 /* Verify that we haven't already published this pci root */
317 for (i = 0; i < root_num; i++) {
318 len = snprintf(str, sizeof(str), "root-%d", i);
319 if (unlikely(len >= (sizeof(str) - 1))) {
324 err = xenbus_scanf(XBT_NIL, pdev->xdev->nodename,
325 str, "%x:%x", &d, &b);
333 if (d == domain && b == bus) {
339 len = snprintf(str, sizeof(str), "root-%d", root_num);
340 if (unlikely(len >= (sizeof(str) - 1))) {
345 dev_dbg(&pdev->xdev->dev, "writing root %d at %04x:%02x\n",
346 root_num, domain, bus);
348 err = xenbus_printf(XBT_NIL, pdev->xdev->nodename, str,
349 "%04x:%02x", domain, bus);
353 err = xenbus_printf(XBT_NIL, pdev->xdev->nodename,
354 "root_num", "%d", (root_num + 1));
360 static int xen_pcibk_reconfigure(struct xen_pcibk_device *pdev)
364 int domain, bus, slot, func;
371 dev_dbg(&pdev->xdev->dev, "Reconfiguring device ...\n");
373 /* Make sure we only reconfigure once */
374 if (xenbus_read_driver_state(pdev->xdev->nodename) !=
375 XenbusStateReconfiguring)
378 err = xenbus_scanf(XBT_NIL, pdev->xdev->nodename, "num_devs", "%d",
383 xenbus_dev_fatal(pdev->xdev, err,
384 "Error reading number of devices");
388 for (i = 0; i < num_devs; i++) {
389 len = snprintf(state_str, sizeof(state_str), "state-%d", i);
390 if (unlikely(len >= (sizeof(state_str) - 1))) {
392 xenbus_dev_fatal(pdev->xdev, err,
393 "String overflow while reading "
397 err = xenbus_scanf(XBT_NIL, pdev->xdev->nodename, state_str,
400 substate = XenbusStateUnknown;
403 case XenbusStateInitialising:
404 dev_dbg(&pdev->xdev->dev, "Attaching dev-%d ...\n", i);
406 len = snprintf(dev_str, sizeof(dev_str), "dev-%d", i);
407 if (unlikely(len >= (sizeof(dev_str) - 1))) {
409 xenbus_dev_fatal(pdev->xdev, err,
410 "String overflow while "
411 "reading configuration");
414 err = xenbus_scanf(XBT_NIL, pdev->xdev->nodename,
415 dev_str, "%x:%x:%x.%x",
416 &domain, &bus, &slot, &func);
418 xenbus_dev_fatal(pdev->xdev, err,
419 "Error reading device "
425 xenbus_dev_fatal(pdev->xdev, err,
426 "Error parsing pci device "
431 err = xen_pcibk_export_device(pdev, domain, bus, slot,
436 /* Publish pci roots. */
437 err = xen_pcibk_publish_pci_roots(pdev,
438 xen_pcibk_publish_pci_root);
440 xenbus_dev_fatal(pdev->xdev, err,
441 "Error while publish PCI root"
442 "buses for frontend");
446 err = xenbus_printf(XBT_NIL, pdev->xdev->nodename,
448 XenbusStateInitialised);
450 xenbus_dev_fatal(pdev->xdev, err,
451 "Error switching substate of "
457 case XenbusStateClosing:
458 dev_dbg(&pdev->xdev->dev, "Detaching dev-%d ...\n", i);
460 len = snprintf(dev_str, sizeof(dev_str), "vdev-%d", i);
461 if (unlikely(len >= (sizeof(dev_str) - 1))) {
463 xenbus_dev_fatal(pdev->xdev, err,
464 "String overflow while "
465 "reading configuration");
468 err = xenbus_scanf(XBT_NIL, pdev->xdev->nodename,
469 dev_str, "%x:%x:%x.%x",
470 &domain, &bus, &slot, &func);
472 xenbus_dev_fatal(pdev->xdev, err,
473 "Error reading device "
479 xenbus_dev_fatal(pdev->xdev, err,
480 "Error parsing pci device "
485 err = xen_pcibk_remove_device(pdev, domain, bus, slot,
490 /* TODO: If at some point we implement support for pci
491 * root hot-remove on pcifront side, we'll need to
492 * remove unnecessary xenstore nodes of pci roots here.
502 err = xenbus_switch_state(pdev->xdev, XenbusStateReconfigured);
504 xenbus_dev_fatal(pdev->xdev, err,
505 "Error switching to reconfigured state!");
513 static void xen_pcibk_frontend_changed(struct xenbus_device *xdev,
514 enum xenbus_state fe_state)
516 struct xen_pcibk_device *pdev = dev_get_drvdata(&xdev->dev);
518 dev_dbg(&xdev->dev, "fe state changed %d\n", fe_state);
521 case XenbusStateInitialised:
522 xen_pcibk_attach(pdev);
525 case XenbusStateReconfiguring:
526 xen_pcibk_reconfigure(pdev);
529 case XenbusStateConnected:
530 /* pcifront switched its state from reconfiguring to connected.
531 * Then switch to connected state.
533 xenbus_switch_state(xdev, XenbusStateConnected);
536 case XenbusStateClosing:
537 xen_pcibk_disconnect(pdev);
538 xenbus_switch_state(xdev, XenbusStateClosing);
541 case XenbusStateClosed:
542 xen_pcibk_disconnect(pdev);
543 xenbus_switch_state(xdev, XenbusStateClosed);
544 if (xenbus_dev_is_online(xdev))
546 /* fall through if not online */
547 case XenbusStateUnknown:
548 dev_dbg(&xdev->dev, "frontend is gone! unregister device\n");
549 device_unregister(&xdev->dev);
557 static int xen_pcibk_setup_backend(struct xen_pcibk_device *pdev)
559 /* Get configuration from xend (if available now) */
560 int domain, bus, slot, func;
566 /* It's possible we could get the call to setup twice, so make sure
567 * we're not already connected.
569 if (xenbus_read_driver_state(pdev->xdev->nodename) !=
573 dev_dbg(&pdev->xdev->dev, "getting be setup\n");
575 err = xenbus_scanf(XBT_NIL, pdev->xdev->nodename, "num_devs", "%d",
580 xenbus_dev_fatal(pdev->xdev, err,
581 "Error reading number of devices");
585 for (i = 0; i < num_devs; i++) {
586 int l = snprintf(dev_str, sizeof(dev_str), "dev-%d", i);
587 if (unlikely(l >= (sizeof(dev_str) - 1))) {
589 xenbus_dev_fatal(pdev->xdev, err,
590 "String overflow while reading "
595 err = xenbus_scanf(XBT_NIL, pdev->xdev->nodename, dev_str,
596 "%x:%x:%x.%x", &domain, &bus, &slot, &func);
598 xenbus_dev_fatal(pdev->xdev, err,
599 "Error reading device configuration");
604 xenbus_dev_fatal(pdev->xdev, err,
605 "Error parsing pci device "
610 err = xen_pcibk_export_device(pdev, domain, bus, slot, func, i);
614 /* Switch substate of this device. */
615 l = snprintf(state_str, sizeof(state_str), "state-%d", i);
616 if (unlikely(l >= (sizeof(state_str) - 1))) {
618 xenbus_dev_fatal(pdev->xdev, err,
619 "String overflow while reading "
623 err = xenbus_printf(XBT_NIL, pdev->xdev->nodename, state_str,
624 "%d", XenbusStateInitialised);
626 xenbus_dev_fatal(pdev->xdev, err, "Error switching "
627 "substate of dev-%d\n", i);
632 err = xen_pcibk_publish_pci_roots(pdev, xen_pcibk_publish_pci_root);
634 xenbus_dev_fatal(pdev->xdev, err,
635 "Error while publish PCI root buses "
640 err = xenbus_switch_state(pdev->xdev, XenbusStateInitialised);
642 xenbus_dev_fatal(pdev->xdev, err,
643 "Error switching to initialised state!");
647 /* see if pcifront is already configured (if not, we'll wait) */
648 xen_pcibk_attach(pdev);
653 static void xen_pcibk_be_watch(struct xenbus_watch *watch,
654 const char **vec, unsigned int len)
656 struct xen_pcibk_device *pdev =
657 container_of(watch, struct xen_pcibk_device, be_watch);
659 switch (xenbus_read_driver_state(pdev->xdev->nodename)) {
660 case XenbusStateInitWait:
661 xen_pcibk_setup_backend(pdev);
669 static int xen_pcibk_xenbus_probe(struct xenbus_device *dev,
670 const struct xenbus_device_id *id)
673 struct xen_pcibk_device *pdev = alloc_pdev(dev);
677 xenbus_dev_fatal(dev, err,
678 "Error allocating xen_pcibk_device struct");
682 /* wait for xend to configure us */
683 err = xenbus_switch_state(dev, XenbusStateInitWait);
687 /* watch the backend node for backend configuration information */
688 err = xenbus_watch_path(dev, dev->nodename, &pdev->be_watch,
693 pdev->be_watching = 1;
695 /* We need to force a call to our callback here in case
696 * xend already configured us!
698 xen_pcibk_be_watch(&pdev->be_watch, NULL, 0);
704 static int xen_pcibk_xenbus_remove(struct xenbus_device *dev)
706 struct xen_pcibk_device *pdev = dev_get_drvdata(&dev->dev);
714 static const struct xenbus_device_id xenpci_ids[] = {
719 static struct xenbus_driver xenbus_xen_pcibk_driver = {
721 .owner = THIS_MODULE,
723 .probe = xen_pcibk_xenbus_probe,
724 .remove = xen_pcibk_xenbus_remove,
725 .otherend_changed = xen_pcibk_frontend_changed,
728 struct xen_pcibk_backend *xen_pcibk_backend;
730 int __init xen_pcibk_xenbus_register(void)
732 xen_pcibk_wq = create_workqueue("xen_pciback_workqueue");
734 printk(KERN_ERR "%s: create"
735 "xen_pciback_workqueue failed\n", __func__);
738 xen_pcibk_backend = &xen_pcibk_vpci_backend;
740 xen_pcibk_backend = &xen_pcibk_passthrough_backend;
741 pr_info(DRV_NAME ": backend is %s\n", xen_pcibk_backend->name);
742 return xenbus_register_backend(&xenbus_xen_pcibk_driver);
745 void __exit xen_pcibk_xenbus_unregister(void)
747 destroy_workqueue(xen_pcibk_wq);
748 xenbus_unregister_driver(&xenbus_xen_pcibk_driver);