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 INVALID_EVTCHN_IRQ (-1)
17 struct workqueue_struct *xen_pcibk_wq;
19 static bool __read_mostly passthrough;
20 module_param(passthrough, bool, S_IRUGO);
21 MODULE_PARM_DESC(passthrough,
22 "Option to specify how to export PCI topology to guest:\n"\
23 " 0 - (default) Hide the true PCI topology and makes the frontend\n"\
24 " there is a single PCI bus with only the exported devices on it.\n"\
25 " For example, a device at 03:05.0 will be re-assigned to 00:00.0\n"\
26 " while second device at 02:1a.1 will be re-assigned to 00:01.1.\n"\
27 " 1 - Passthrough provides a real view of the PCI topology to the\n"\
28 " frontend (for example, a device at 06:01.b will still appear at\n"\
29 " 06:01.b to the frontend). This is similar to how Xen 2.0.x\n"\
30 " exposed PCI devices to its driver domains. This may be required\n"\
31 " for drivers which depend on finding their hardward in certain\n"\
32 " bus/slot locations.");
34 static struct xen_pcibk_device *alloc_pdev(struct xenbus_device *xdev)
36 struct xen_pcibk_device *pdev;
38 pdev = kzalloc(sizeof(struct xen_pcibk_device), GFP_KERNEL);
41 dev_dbg(&xdev->dev, "allocated pdev @ 0x%p\n", pdev);
44 dev_set_drvdata(&xdev->dev, pdev);
46 mutex_init(&pdev->dev_lock);
49 pdev->evtchn_irq = INVALID_EVTCHN_IRQ;
50 pdev->be_watching = 0;
52 INIT_WORK(&pdev->op_work, xen_pcibk_do_op);
54 if (xen_pcibk_init_devices(pdev)) {
62 static void xen_pcibk_disconnect(struct xen_pcibk_device *pdev)
64 mutex_lock(&pdev->dev_lock);
65 /* Ensure the guest can't trigger our handler before removing devices */
66 if (pdev->evtchn_irq != INVALID_EVTCHN_IRQ) {
67 unbind_from_irqhandler(pdev->evtchn_irq, pdev);
68 pdev->evtchn_irq = INVALID_EVTCHN_IRQ;
71 /* If the driver domain started an op, make sure we complete it
72 * before releasing the shared memory */
74 /* Note, the workqueue does not use spinlocks at all.*/
75 flush_workqueue(xen_pcibk_wq);
77 if (pdev->sh_info != NULL) {
78 xenbus_unmap_ring_vfree(pdev->xdev, pdev->sh_info);
81 mutex_unlock(&pdev->dev_lock);
84 static void free_pdev(struct xen_pcibk_device *pdev)
86 if (pdev->be_watching) {
87 unregister_xenbus_watch(&pdev->be_watch);
88 pdev->be_watching = 0;
91 xen_pcibk_disconnect(pdev);
93 xen_pcibk_release_devices(pdev);
95 dev_set_drvdata(&pdev->xdev->dev, NULL);
101 static int xen_pcibk_do_attach(struct xen_pcibk_device *pdev, int gnt_ref,
107 dev_dbg(&pdev->xdev->dev,
108 "Attaching to frontend resources - gnt_ref=%d evtchn=%d\n",
109 gnt_ref, remote_evtchn);
111 err = xenbus_map_ring_valloc(pdev->xdev, gnt_ref, &vaddr);
113 xenbus_dev_fatal(pdev->xdev, err,
114 "Error mapping other domain page in ours.");
118 pdev->sh_info = vaddr;
120 err = bind_interdomain_evtchn_to_irqhandler(
121 pdev->xdev->otherend_id, remote_evtchn, xen_pcibk_handle_event,
124 xenbus_dev_fatal(pdev->xdev, err,
125 "Error binding event channel to IRQ");
128 pdev->evtchn_irq = err;
131 dev_dbg(&pdev->xdev->dev, "Attached!\n");
136 static int xen_pcibk_attach(struct xen_pcibk_device *pdev)
139 int gnt_ref, remote_evtchn;
143 mutex_lock(&pdev->dev_lock);
144 /* Make sure we only do this setup once */
145 if (xenbus_read_driver_state(pdev->xdev->nodename) !=
146 XenbusStateInitialised)
149 /* Wait for frontend to state that it has published the configuration */
150 if (xenbus_read_driver_state(pdev->xdev->otherend) !=
151 XenbusStateInitialised)
154 dev_dbg(&pdev->xdev->dev, "Reading frontend config\n");
156 err = xenbus_gather(XBT_NIL, pdev->xdev->otherend,
157 "pci-op-ref", "%u", &gnt_ref,
158 "event-channel", "%u", &remote_evtchn,
159 "magic", NULL, &magic, NULL);
161 /* If configuration didn't get read correctly, wait longer */
162 xenbus_dev_fatal(pdev->xdev, err,
163 "Error reading configuration from frontend");
167 if (magic == NULL || strcmp(magic, XEN_PCI_MAGIC) != 0) {
168 xenbus_dev_fatal(pdev->xdev, -EFAULT,
169 "version mismatch (%s/%s) with pcifront - "
171 magic, XEN_PCI_MAGIC);
175 err = xen_pcibk_do_attach(pdev, gnt_ref, remote_evtchn);
179 dev_dbg(&pdev->xdev->dev, "Connecting...\n");
181 err = xenbus_switch_state(pdev->xdev, XenbusStateConnected);
183 xenbus_dev_fatal(pdev->xdev, err,
184 "Error switching to connected state!");
186 dev_dbg(&pdev->xdev->dev, "Connected? %d\n", err);
188 mutex_unlock(&pdev->dev_lock);
195 static int xen_pcibk_publish_pci_dev(struct xen_pcibk_device *pdev,
196 unsigned int domain, unsigned int bus,
197 unsigned int devfn, unsigned int devid)
203 len = snprintf(str, sizeof(str), "vdev-%d", devid);
204 if (unlikely(len >= (sizeof(str) - 1))) {
209 err = xenbus_printf(XBT_NIL, pdev->xdev->nodename, str,
210 "%04x:%02x:%02x.%02x", domain, bus,
211 PCI_SLOT(devfn), PCI_FUNC(devfn));
217 static int xen_pcibk_export_device(struct xen_pcibk_device *pdev,
218 int domain, int bus, int slot, int func,
224 dev_dbg(&pdev->xdev->dev, "exporting dom %x bus %x slot %x func %x\n",
225 domain, bus, slot, func);
227 dev = pcistub_get_pci_dev_by_slot(pdev, domain, bus, slot, func);
230 xenbus_dev_fatal(pdev->xdev, err,
231 "Couldn't locate PCI device "
232 "(%04x:%02x:%02x.%01x)! "
233 "perhaps already in-use?",
234 domain, bus, slot, func);
238 err = xen_pcibk_add_pci_dev(pdev, dev, devid,
239 xen_pcibk_publish_pci_dev);
243 dev_dbg(&dev->dev, "registering for %d\n", pdev->xdev->otherend_id);
244 if (xen_register_device_domain_owner(dev,
245 pdev->xdev->otherend_id) != 0) {
246 dev_err(&dev->dev, "Stealing ownership from dom%d.\n",
247 xen_find_device_domain_owner(dev));
248 xen_unregister_device_domain_owner(dev);
249 xen_register_device_domain_owner(dev, pdev->xdev->otherend_id);
252 /* TODO: It'd be nice to export a bridge and have all of its children
253 * get exported with it. This may be best done in xend (which will
254 * have to calculate resource usage anyway) but we probably want to
255 * put something in here to ensure that if a bridge gets given to a
256 * driver domain, that all devices under that bridge are not given
257 * to other driver domains (as he who controls the bridge can disable
258 * it and stop the other devices from working).
264 static int xen_pcibk_remove_device(struct xen_pcibk_device *pdev,
265 int domain, int bus, int slot, int func)
270 dev_dbg(&pdev->xdev->dev, "removing dom %x bus %x slot %x func %x\n",
271 domain, bus, slot, func);
273 dev = xen_pcibk_get_pci_dev(pdev, domain, bus, PCI_DEVFN(slot, func));
276 dev_dbg(&pdev->xdev->dev, "Couldn't locate PCI device "
277 "(%04x:%02x:%02x.%01x)! not owned by this domain\n",
278 domain, bus, slot, func);
282 dev_dbg(&dev->dev, "unregistering for %d\n", pdev->xdev->otherend_id);
283 xen_unregister_device_domain_owner(dev);
285 xen_pcibk_release_pci_dev(pdev, dev);
291 static int xen_pcibk_publish_pci_root(struct xen_pcibk_device *pdev,
292 unsigned int domain, unsigned int bus)
295 int i, root_num, len, err;
298 dev_dbg(&pdev->xdev->dev, "Publishing pci roots\n");
300 err = xenbus_scanf(XBT_NIL, pdev->xdev->nodename,
301 "root_num", "%d", &root_num);
302 if (err == 0 || err == -ENOENT)
307 /* Verify that we haven't already published this pci root */
308 for (i = 0; i < root_num; i++) {
309 len = snprintf(str, sizeof(str), "root-%d", i);
310 if (unlikely(len >= (sizeof(str) - 1))) {
315 err = xenbus_scanf(XBT_NIL, pdev->xdev->nodename,
316 str, "%x:%x", &d, &b);
324 if (d == domain && b == bus) {
330 len = snprintf(str, sizeof(str), "root-%d", root_num);
331 if (unlikely(len >= (sizeof(str) - 1))) {
336 dev_dbg(&pdev->xdev->dev, "writing root %d at %04x:%02x\n",
337 root_num, domain, bus);
339 err = xenbus_printf(XBT_NIL, pdev->xdev->nodename, str,
340 "%04x:%02x", domain, bus);
344 err = xenbus_printf(XBT_NIL, pdev->xdev->nodename,
345 "root_num", "%d", (root_num + 1));
351 static int xen_pcibk_reconfigure(struct xen_pcibk_device *pdev)
355 int domain, bus, slot, func;
362 dev_dbg(&pdev->xdev->dev, "Reconfiguring device ...\n");
364 mutex_lock(&pdev->dev_lock);
365 /* Make sure we only reconfigure once */
366 if (xenbus_read_driver_state(pdev->xdev->nodename) !=
367 XenbusStateReconfiguring)
370 err = xenbus_scanf(XBT_NIL, pdev->xdev->nodename, "num_devs", "%d",
375 xenbus_dev_fatal(pdev->xdev, err,
376 "Error reading number of devices");
380 for (i = 0; i < num_devs; i++) {
381 len = snprintf(state_str, sizeof(state_str), "state-%d", i);
382 if (unlikely(len >= (sizeof(state_str) - 1))) {
384 xenbus_dev_fatal(pdev->xdev, err,
385 "String overflow while reading "
389 err = xenbus_scanf(XBT_NIL, pdev->xdev->nodename, state_str,
392 substate = XenbusStateUnknown;
395 case XenbusStateInitialising:
396 dev_dbg(&pdev->xdev->dev, "Attaching dev-%d ...\n", i);
398 len = snprintf(dev_str, sizeof(dev_str), "dev-%d", i);
399 if (unlikely(len >= (sizeof(dev_str) - 1))) {
401 xenbus_dev_fatal(pdev->xdev, err,
402 "String overflow while "
403 "reading configuration");
406 err = xenbus_scanf(XBT_NIL, pdev->xdev->nodename,
407 dev_str, "%x:%x:%x.%x",
408 &domain, &bus, &slot, &func);
410 xenbus_dev_fatal(pdev->xdev, err,
411 "Error reading device "
417 xenbus_dev_fatal(pdev->xdev, err,
418 "Error parsing pci device "
423 err = xen_pcibk_export_device(pdev, domain, bus, slot,
428 /* Publish pci roots. */
429 err = xen_pcibk_publish_pci_roots(pdev,
430 xen_pcibk_publish_pci_root);
432 xenbus_dev_fatal(pdev->xdev, err,
433 "Error while publish PCI root"
434 "buses for frontend");
438 err = xenbus_printf(XBT_NIL, pdev->xdev->nodename,
440 XenbusStateInitialised);
442 xenbus_dev_fatal(pdev->xdev, err,
443 "Error switching substate of "
449 case XenbusStateClosing:
450 dev_dbg(&pdev->xdev->dev, "Detaching dev-%d ...\n", i);
452 len = snprintf(dev_str, sizeof(dev_str), "vdev-%d", i);
453 if (unlikely(len >= (sizeof(dev_str) - 1))) {
455 xenbus_dev_fatal(pdev->xdev, err,
456 "String overflow while "
457 "reading configuration");
460 err = xenbus_scanf(XBT_NIL, pdev->xdev->nodename,
461 dev_str, "%x:%x:%x.%x",
462 &domain, &bus, &slot, &func);
464 xenbus_dev_fatal(pdev->xdev, err,
465 "Error reading device "
471 xenbus_dev_fatal(pdev->xdev, err,
472 "Error parsing pci device "
477 err = xen_pcibk_remove_device(pdev, domain, bus, slot,
482 /* TODO: If at some point we implement support for pci
483 * root hot-remove on pcifront side, we'll need to
484 * remove unnecessary xenstore nodes of pci roots here.
494 err = xenbus_switch_state(pdev->xdev, XenbusStateReconfigured);
496 xenbus_dev_fatal(pdev->xdev, err,
497 "Error switching to reconfigured state!");
502 mutex_unlock(&pdev->dev_lock);
506 static void xen_pcibk_frontend_changed(struct xenbus_device *xdev,
507 enum xenbus_state fe_state)
509 struct xen_pcibk_device *pdev = dev_get_drvdata(&xdev->dev);
511 dev_dbg(&xdev->dev, "fe state changed %d\n", fe_state);
514 case XenbusStateInitialised:
515 xen_pcibk_attach(pdev);
518 case XenbusStateReconfiguring:
519 xen_pcibk_reconfigure(pdev);
522 case XenbusStateConnected:
523 /* pcifront switched its state from reconfiguring to connected.
524 * Then switch to connected state.
526 xenbus_switch_state(xdev, XenbusStateConnected);
529 case XenbusStateClosing:
530 xen_pcibk_disconnect(pdev);
531 xenbus_switch_state(xdev, XenbusStateClosing);
534 case XenbusStateClosed:
535 xen_pcibk_disconnect(pdev);
536 xenbus_switch_state(xdev, XenbusStateClosed);
537 if (xenbus_dev_is_online(xdev))
539 /* fall through if not online */
540 case XenbusStateUnknown:
541 dev_dbg(&xdev->dev, "frontend is gone! unregister device\n");
542 device_unregister(&xdev->dev);
550 static int xen_pcibk_setup_backend(struct xen_pcibk_device *pdev)
552 /* Get configuration from xend (if available now) */
553 int domain, bus, slot, func;
559 mutex_lock(&pdev->dev_lock);
560 /* It's possible we could get the call to setup twice, so make sure
561 * we're not already connected.
563 if (xenbus_read_driver_state(pdev->xdev->nodename) !=
567 dev_dbg(&pdev->xdev->dev, "getting be setup\n");
569 err = xenbus_scanf(XBT_NIL, pdev->xdev->nodename, "num_devs", "%d",
574 xenbus_dev_fatal(pdev->xdev, err,
575 "Error reading number of devices");
579 for (i = 0; i < num_devs; i++) {
580 int l = snprintf(dev_str, sizeof(dev_str), "dev-%d", i);
581 if (unlikely(l >= (sizeof(dev_str) - 1))) {
583 xenbus_dev_fatal(pdev->xdev, err,
584 "String overflow while reading "
589 err = xenbus_scanf(XBT_NIL, pdev->xdev->nodename, dev_str,
590 "%x:%x:%x.%x", &domain, &bus, &slot, &func);
592 xenbus_dev_fatal(pdev->xdev, err,
593 "Error reading device configuration");
598 xenbus_dev_fatal(pdev->xdev, err,
599 "Error parsing pci device "
604 err = xen_pcibk_export_device(pdev, domain, bus, slot, func, i);
608 /* Switch substate of this device. */
609 l = snprintf(state_str, sizeof(state_str), "state-%d", i);
610 if (unlikely(l >= (sizeof(state_str) - 1))) {
612 xenbus_dev_fatal(pdev->xdev, err,
613 "String overflow while reading "
617 err = xenbus_printf(XBT_NIL, pdev->xdev->nodename, state_str,
618 "%d", XenbusStateInitialised);
620 xenbus_dev_fatal(pdev->xdev, err, "Error switching "
621 "substate of dev-%d\n", i);
626 err = xen_pcibk_publish_pci_roots(pdev, xen_pcibk_publish_pci_root);
628 xenbus_dev_fatal(pdev->xdev, err,
629 "Error while publish PCI root buses "
634 err = xenbus_switch_state(pdev->xdev, XenbusStateInitialised);
636 xenbus_dev_fatal(pdev->xdev, err,
637 "Error switching to initialised state!");
640 mutex_unlock(&pdev->dev_lock);
642 /* see if pcifront is already configured (if not, we'll wait) */
643 xen_pcibk_attach(pdev);
647 static void xen_pcibk_be_watch(struct xenbus_watch *watch,
648 const char **vec, unsigned int len)
650 struct xen_pcibk_device *pdev =
651 container_of(watch, struct xen_pcibk_device, be_watch);
653 switch (xenbus_read_driver_state(pdev->xdev->nodename)) {
654 case XenbusStateInitWait:
655 xen_pcibk_setup_backend(pdev);
663 static int xen_pcibk_xenbus_probe(struct xenbus_device *dev,
664 const struct xenbus_device_id *id)
667 struct xen_pcibk_device *pdev = alloc_pdev(dev);
671 xenbus_dev_fatal(dev, err,
672 "Error allocating xen_pcibk_device struct");
676 /* wait for xend to configure us */
677 err = xenbus_switch_state(dev, XenbusStateInitWait);
681 /* watch the backend node for backend configuration information */
682 err = xenbus_watch_path(dev, dev->nodename, &pdev->be_watch,
687 pdev->be_watching = 1;
689 /* We need to force a call to our callback here in case
690 * xend already configured us!
692 xen_pcibk_be_watch(&pdev->be_watch, NULL, 0);
698 static int xen_pcibk_xenbus_remove(struct xenbus_device *dev)
700 struct xen_pcibk_device *pdev = dev_get_drvdata(&dev->dev);
708 static const struct xenbus_device_id xen_pcibk_ids[] = {
713 static DEFINE_XENBUS_DRIVER(xen_pcibk, DRV_NAME,
714 .probe = xen_pcibk_xenbus_probe,
715 .remove = xen_pcibk_xenbus_remove,
716 .otherend_changed = xen_pcibk_frontend_changed,
719 const struct xen_pcibk_backend *__read_mostly xen_pcibk_backend;
721 int __init xen_pcibk_xenbus_register(void)
723 xen_pcibk_wq = create_workqueue("xen_pciback_workqueue");
725 printk(KERN_ERR "%s: create"
726 "xen_pciback_workqueue failed\n", __func__);
729 xen_pcibk_backend = &xen_pcibk_vpci_backend;
731 xen_pcibk_backend = &xen_pcibk_passthrough_backend;
732 pr_info(DRV_NAME ": backend is %s\n", xen_pcibk_backend->name);
733 return xenbus_register_backend(&xen_pcibk_driver);
736 void __exit xen_pcibk_xenbus_unregister(void)
738 destroy_workqueue(xen_pcibk_wq);
739 xenbus_unregister_driver(&xen_pcibk_driver);