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>
16 #define DRV_NAME "xen-pciback"
17 #define INVALID_EVTCHN_IRQ (-1)
18 struct workqueue_struct *xen_pcibk_wq;
20 static int __read_mostly passthrough;
21 module_param(passthrough, bool, S_IRUGO);
22 MODULE_PARM_DESC(passthrough,
23 "Option to specify how to export PCI topology to guest:\n"\
24 " 0 - (default) Hide the true PCI topology and makes the frontend\n"\
25 " there is a single PCI bus with only the exported devices on it.\n"\
26 " For example, a device at 03:05.0 will be re-assigned to 00:00.0\n"\
27 " while second device at 02:1a.1 will be re-assigned to 00:01.1.\n"\
28 " 1 - Passthrough provides a real view of the PCI topology to the\n"\
29 " frontend (for example, a device at 06:01.b will still appear at\n"\
30 " 06:01.b to the frontend). This is similar to how Xen 2.0.x\n"\
31 " exposed PCI devices to its driver domains. This may be required\n"\
32 " for drivers which depend on finding their hardward in certain\n"\
33 " bus/slot locations.");
35 static struct xen_pcibk_device *alloc_pdev(struct xenbus_device *xdev)
37 struct xen_pcibk_device *pdev;
39 pdev = kzalloc(sizeof(struct xen_pcibk_device), GFP_KERNEL);
42 dev_dbg(&xdev->dev, "allocated pdev @ 0x%p\n", pdev);
45 dev_set_drvdata(&xdev->dev, pdev);
47 spin_lock_init(&pdev->dev_lock);
50 pdev->evtchn_irq = INVALID_EVTCHN_IRQ;
51 pdev->be_watching = 0;
53 INIT_WORK(&pdev->op_work, xen_pcibk_do_op);
55 if (xen_pcibk_init_devices(pdev)) {
63 static void xen_pcibk_disconnect(struct xen_pcibk_device *pdev)
65 spin_lock(&pdev->dev_lock);
67 /* Ensure the guest can't trigger our handler before removing devices */
68 if (pdev->evtchn_irq != INVALID_EVTCHN_IRQ) {
69 unbind_from_irqhandler(pdev->evtchn_irq, pdev);
70 pdev->evtchn_irq = INVALID_EVTCHN_IRQ;
72 spin_unlock(&pdev->dev_lock);
74 /* If the driver domain started an op, make sure we complete it
75 * before releasing the shared memory */
77 /* Note, the workqueue does not use spinlocks at all.*/
78 flush_workqueue(xen_pcibk_wq);
80 spin_lock(&pdev->dev_lock);
81 if (pdev->sh_info != NULL) {
82 xenbus_unmap_ring_vfree(pdev->xdev, pdev->sh_info);
85 spin_unlock(&pdev->dev_lock);
89 static void free_pdev(struct xen_pcibk_device *pdev)
91 if (pdev->be_watching) {
92 unregister_xenbus_watch(&pdev->be_watch);
93 pdev->be_watching = 0;
96 xen_pcibk_disconnect(pdev);
98 xen_pcibk_release_devices(pdev);
100 dev_set_drvdata(&pdev->xdev->dev, NULL);
106 static int xen_pcibk_do_attach(struct xen_pcibk_device *pdev, int gnt_ref,
112 dev_dbg(&pdev->xdev->dev,
113 "Attaching to frontend resources - gnt_ref=%d evtchn=%d\n",
114 gnt_ref, remote_evtchn);
116 err = xenbus_map_ring_valloc(pdev->xdev, gnt_ref, &vaddr);
118 xenbus_dev_fatal(pdev->xdev, err,
119 "Error mapping other domain page in ours.");
123 spin_lock(&pdev->dev_lock);
124 pdev->sh_info = vaddr;
125 spin_unlock(&pdev->dev_lock);
127 err = bind_interdomain_evtchn_to_irqhandler(
128 pdev->xdev->otherend_id, remote_evtchn, xen_pcibk_handle_event,
131 xenbus_dev_fatal(pdev->xdev, err,
132 "Error binding event channel to IRQ");
136 spin_lock(&pdev->dev_lock);
137 pdev->evtchn_irq = err;
138 spin_unlock(&pdev->dev_lock);
141 dev_dbg(&pdev->xdev->dev, "Attached!\n");
146 static int xen_pcibk_attach(struct xen_pcibk_device *pdev)
149 int gnt_ref, remote_evtchn;
153 /* Make sure we only do this setup once */
154 if (xenbus_read_driver_state(pdev->xdev->nodename) !=
155 XenbusStateInitialised)
158 /* Wait for frontend to state that it has published the configuration */
159 if (xenbus_read_driver_state(pdev->xdev->otherend) !=
160 XenbusStateInitialised)
163 dev_dbg(&pdev->xdev->dev, "Reading frontend config\n");
165 err = xenbus_gather(XBT_NIL, pdev->xdev->otherend,
166 "pci-op-ref", "%u", &gnt_ref,
167 "event-channel", "%u", &remote_evtchn,
168 "magic", NULL, &magic, NULL);
170 /* If configuration didn't get read correctly, wait longer */
171 xenbus_dev_fatal(pdev->xdev, err,
172 "Error reading configuration from frontend");
176 if (magic == NULL || strcmp(magic, XEN_PCI_MAGIC) != 0) {
177 xenbus_dev_fatal(pdev->xdev, -EFAULT,
178 "version mismatch (%s/%s) with pcifront - "
180 magic, XEN_PCI_MAGIC);
184 err = xen_pcibk_do_attach(pdev, gnt_ref, remote_evtchn);
188 dev_dbg(&pdev->xdev->dev, "Connecting...\n");
190 err = xenbus_switch_state(pdev->xdev, XenbusStateConnected);
192 xenbus_dev_fatal(pdev->xdev, err,
193 "Error switching to connected state!");
195 dev_dbg(&pdev->xdev->dev, "Connected? %d\n", err);
203 static int xen_pcibk_publish_pci_dev(struct xen_pcibk_device *pdev,
204 unsigned int domain, unsigned int bus,
205 unsigned int devfn, unsigned int devid)
211 len = snprintf(str, sizeof(str), "vdev-%d", devid);
212 if (unlikely(len >= (sizeof(str) - 1))) {
217 err = xenbus_printf(XBT_NIL, pdev->xdev->nodename, str,
218 "%04x:%02x:%02x.%02x", domain, bus,
219 PCI_SLOT(devfn), PCI_FUNC(devfn));
225 static int xen_pcibk_export_device(struct xen_pcibk_device *pdev,
226 int domain, int bus, int slot, int func,
232 dev_dbg(&pdev->xdev->dev, "exporting dom %x bus %x slot %x func %x\n",
233 domain, bus, slot, func);
235 dev = pcistub_get_pci_dev_by_slot(pdev, domain, bus, slot, func);
238 xenbus_dev_fatal(pdev->xdev, err,
239 "Couldn't locate PCI device "
240 "(%04x:%02x:%02x.%01x)! "
241 "perhaps already in-use?",
242 domain, bus, slot, func);
246 err = xen_pcibk_add_pci_dev(pdev, dev, devid,
247 xen_pcibk_publish_pci_dev);
251 dev_dbg(&dev->dev, "registering for %d\n", pdev->xdev->otherend_id);
252 dev->dev_flags |= PCI_DEV_FLAGS_ASSIGNED;
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 dev->dev_flags &= ~PCI_DEV_FLAGS_ASSIGNED;
293 xen_unregister_device_domain_owner(dev);
295 xen_pcibk_release_pci_dev(pdev, dev);
301 static int xen_pcibk_publish_pci_root(struct xen_pcibk_device *pdev,
302 unsigned int domain, unsigned int bus)
305 int i, root_num, len, err;
308 dev_dbg(&pdev->xdev->dev, "Publishing pci roots\n");
310 err = xenbus_scanf(XBT_NIL, pdev->xdev->nodename,
311 "root_num", "%d", &root_num);
312 if (err == 0 || err == -ENOENT)
317 /* Verify that we haven't already published this pci root */
318 for (i = 0; i < root_num; i++) {
319 len = snprintf(str, sizeof(str), "root-%d", i);
320 if (unlikely(len >= (sizeof(str) - 1))) {
325 err = xenbus_scanf(XBT_NIL, pdev->xdev->nodename,
326 str, "%x:%x", &d, &b);
334 if (d == domain && b == bus) {
340 len = snprintf(str, sizeof(str), "root-%d", root_num);
341 if (unlikely(len >= (sizeof(str) - 1))) {
346 dev_dbg(&pdev->xdev->dev, "writing root %d at %04x:%02x\n",
347 root_num, domain, bus);
349 err = xenbus_printf(XBT_NIL, pdev->xdev->nodename, str,
350 "%04x:%02x", domain, bus);
354 err = xenbus_printf(XBT_NIL, pdev->xdev->nodename,
355 "root_num", "%d", (root_num + 1));
361 static int xen_pcibk_reconfigure(struct xen_pcibk_device *pdev)
365 int domain, bus, slot, func;
372 dev_dbg(&pdev->xdev->dev, "Reconfiguring device ...\n");
374 /* Make sure we only reconfigure once */
375 if (xenbus_read_driver_state(pdev->xdev->nodename) !=
376 XenbusStateReconfiguring)
379 err = xenbus_scanf(XBT_NIL, pdev->xdev->nodename, "num_devs", "%d",
384 xenbus_dev_fatal(pdev->xdev, err,
385 "Error reading number of devices");
389 for (i = 0; i < num_devs; i++) {
390 len = snprintf(state_str, sizeof(state_str), "state-%d", i);
391 if (unlikely(len >= (sizeof(state_str) - 1))) {
393 xenbus_dev_fatal(pdev->xdev, err,
394 "String overflow while reading "
398 err = xenbus_scanf(XBT_NIL, pdev->xdev->nodename, state_str,
401 substate = XenbusStateUnknown;
404 case XenbusStateInitialising:
405 dev_dbg(&pdev->xdev->dev, "Attaching dev-%d ...\n", i);
407 len = snprintf(dev_str, sizeof(dev_str), "dev-%d", i);
408 if (unlikely(len >= (sizeof(dev_str) - 1))) {
410 xenbus_dev_fatal(pdev->xdev, err,
411 "String overflow while "
412 "reading configuration");
415 err = xenbus_scanf(XBT_NIL, pdev->xdev->nodename,
416 dev_str, "%x:%x:%x.%x",
417 &domain, &bus, &slot, &func);
419 xenbus_dev_fatal(pdev->xdev, err,
420 "Error reading device "
426 xenbus_dev_fatal(pdev->xdev, err,
427 "Error parsing pci device "
432 err = xen_pcibk_export_device(pdev, domain, bus, slot,
437 /* Publish pci roots. */
438 err = xen_pcibk_publish_pci_roots(pdev,
439 xen_pcibk_publish_pci_root);
441 xenbus_dev_fatal(pdev->xdev, err,
442 "Error while publish PCI root"
443 "buses for frontend");
447 err = xenbus_printf(XBT_NIL, pdev->xdev->nodename,
449 XenbusStateInitialised);
451 xenbus_dev_fatal(pdev->xdev, err,
452 "Error switching substate of "
458 case XenbusStateClosing:
459 dev_dbg(&pdev->xdev->dev, "Detaching dev-%d ...\n", i);
461 len = snprintf(dev_str, sizeof(dev_str), "vdev-%d", i);
462 if (unlikely(len >= (sizeof(dev_str) - 1))) {
464 xenbus_dev_fatal(pdev->xdev, err,
465 "String overflow while "
466 "reading configuration");
469 err = xenbus_scanf(XBT_NIL, pdev->xdev->nodename,
470 dev_str, "%x:%x:%x.%x",
471 &domain, &bus, &slot, &func);
473 xenbus_dev_fatal(pdev->xdev, err,
474 "Error reading device "
480 xenbus_dev_fatal(pdev->xdev, err,
481 "Error parsing pci device "
486 err = xen_pcibk_remove_device(pdev, domain, bus, slot,
491 /* TODO: If at some point we implement support for pci
492 * root hot-remove on pcifront side, we'll need to
493 * remove unnecessary xenstore nodes of pci roots here.
503 err = xenbus_switch_state(pdev->xdev, XenbusStateReconfigured);
505 xenbus_dev_fatal(pdev->xdev, err,
506 "Error switching to reconfigured state!");
514 static void xen_pcibk_frontend_changed(struct xenbus_device *xdev,
515 enum xenbus_state fe_state)
517 struct xen_pcibk_device *pdev = dev_get_drvdata(&xdev->dev);
519 dev_dbg(&xdev->dev, "fe state changed %d\n", fe_state);
522 case XenbusStateInitialised:
523 xen_pcibk_attach(pdev);
526 case XenbusStateReconfiguring:
527 xen_pcibk_reconfigure(pdev);
530 case XenbusStateConnected:
531 /* pcifront switched its state from reconfiguring to connected.
532 * Then switch to connected state.
534 xenbus_switch_state(xdev, XenbusStateConnected);
537 case XenbusStateClosing:
538 xen_pcibk_disconnect(pdev);
539 xenbus_switch_state(xdev, XenbusStateClosing);
542 case XenbusStateClosed:
543 xen_pcibk_disconnect(pdev);
544 xenbus_switch_state(xdev, XenbusStateClosed);
545 if (xenbus_dev_is_online(xdev))
547 /* fall through if not online */
548 case XenbusStateUnknown:
549 dev_dbg(&xdev->dev, "frontend is gone! unregister device\n");
550 device_unregister(&xdev->dev);
558 static int xen_pcibk_setup_backend(struct xen_pcibk_device *pdev)
560 /* Get configuration from xend (if available now) */
561 int domain, bus, slot, func;
567 /* It's possible we could get the call to setup twice, so make sure
568 * we're not already connected.
570 if (xenbus_read_driver_state(pdev->xdev->nodename) !=
574 dev_dbg(&pdev->xdev->dev, "getting be setup\n");
576 err = xenbus_scanf(XBT_NIL, pdev->xdev->nodename, "num_devs", "%d",
581 xenbus_dev_fatal(pdev->xdev, err,
582 "Error reading number of devices");
586 for (i = 0; i < num_devs; i++) {
587 int l = snprintf(dev_str, sizeof(dev_str), "dev-%d", i);
588 if (unlikely(l >= (sizeof(dev_str) - 1))) {
590 xenbus_dev_fatal(pdev->xdev, err,
591 "String overflow while reading "
596 err = xenbus_scanf(XBT_NIL, pdev->xdev->nodename, dev_str,
597 "%x:%x:%x.%x", &domain, &bus, &slot, &func);
599 xenbus_dev_fatal(pdev->xdev, err,
600 "Error reading device configuration");
605 xenbus_dev_fatal(pdev->xdev, err,
606 "Error parsing pci device "
611 err = xen_pcibk_export_device(pdev, domain, bus, slot, func, i);
615 /* Switch substate of this device. */
616 l = snprintf(state_str, sizeof(state_str), "state-%d", i);
617 if (unlikely(l >= (sizeof(state_str) - 1))) {
619 xenbus_dev_fatal(pdev->xdev, err,
620 "String overflow while reading "
624 err = xenbus_printf(XBT_NIL, pdev->xdev->nodename, state_str,
625 "%d", XenbusStateInitialised);
627 xenbus_dev_fatal(pdev->xdev, err, "Error switching "
628 "substate of dev-%d\n", i);
633 err = xen_pcibk_publish_pci_roots(pdev, xen_pcibk_publish_pci_root);
635 xenbus_dev_fatal(pdev->xdev, err,
636 "Error while publish PCI root buses "
641 err = xenbus_switch_state(pdev->xdev, XenbusStateInitialised);
643 xenbus_dev_fatal(pdev->xdev, err,
644 "Error switching to initialised state!");
648 /* see if pcifront is already configured (if not, we'll wait) */
649 xen_pcibk_attach(pdev);
654 static void xen_pcibk_be_watch(struct xenbus_watch *watch,
655 const char **vec, unsigned int len)
657 struct xen_pcibk_device *pdev =
658 container_of(watch, struct xen_pcibk_device, be_watch);
660 switch (xenbus_read_driver_state(pdev->xdev->nodename)) {
661 case XenbusStateInitWait:
662 xen_pcibk_setup_backend(pdev);
670 static int xen_pcibk_xenbus_probe(struct xenbus_device *dev,
671 const struct xenbus_device_id *id)
674 struct xen_pcibk_device *pdev = alloc_pdev(dev);
678 xenbus_dev_fatal(dev, err,
679 "Error allocating xen_pcibk_device struct");
683 /* wait for xend to configure us */
684 err = xenbus_switch_state(dev, XenbusStateInitWait);
688 /* watch the backend node for backend configuration information */
689 err = xenbus_watch_path(dev, dev->nodename, &pdev->be_watch,
694 pdev->be_watching = 1;
696 /* We need to force a call to our callback here in case
697 * xend already configured us!
699 xen_pcibk_be_watch(&pdev->be_watch, NULL, 0);
705 static int xen_pcibk_xenbus_remove(struct xenbus_device *dev)
707 struct xen_pcibk_device *pdev = dev_get_drvdata(&dev->dev);
715 static const struct xenbus_device_id xenpci_ids[] = {
720 static struct xenbus_driver xenbus_xen_pcibk_driver = {
722 .owner = THIS_MODULE,
724 .probe = xen_pcibk_xenbus_probe,
725 .remove = xen_pcibk_xenbus_remove,
726 .otherend_changed = xen_pcibk_frontend_changed,
729 struct xen_pcibk_backend *xen_pcibk_backend;
731 int __init xen_pcibk_xenbus_register(void)
733 xen_pcibk_wq = create_workqueue("xen_pciback_workqueue");
735 printk(KERN_ERR "%s: create"
736 "xen_pciback_workqueue failed\n", __func__);
739 xen_pcibk_backend = &xen_pcibk_vpci_backend;
741 xen_pcibk_backend = &xen_pcibk_passthrough_backend;
742 pr_info(DRV_NAME ": backend is %s\n", xen_pcibk_backend->name);
743 return xenbus_register_backend(&xenbus_xen_pcibk_driver);
746 void __exit xen_pcibk_xenbus_unregister(void)
748 destroy_workqueue(xen_pcibk_wq);
749 xenbus_unregister_driver(&xenbus_xen_pcibk_driver);