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 int __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 spin_lock_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 spin_lock(&pdev->dev_lock);
66 /* Ensure the guest can't trigger our handler before removing devices */
67 if (pdev->evtchn_irq != INVALID_EVTCHN_IRQ) {
68 unbind_from_irqhandler(pdev->evtchn_irq, pdev);
69 pdev->evtchn_irq = INVALID_EVTCHN_IRQ;
71 spin_unlock(&pdev->dev_lock);
73 /* If the driver domain started an op, make sure we complete it
74 * before releasing the shared memory */
76 /* Note, the workqueue does not use spinlocks at all.*/
77 flush_workqueue(xen_pcibk_wq);
79 spin_lock(&pdev->dev_lock);
80 if (pdev->sh_info != NULL) {
81 xenbus_unmap_ring_vfree(pdev->xdev, pdev->sh_info);
84 spin_unlock(&pdev->dev_lock);
88 static void free_pdev(struct xen_pcibk_device *pdev)
90 if (pdev->be_watching) {
91 unregister_xenbus_watch(&pdev->be_watch);
92 pdev->be_watching = 0;
95 xen_pcibk_disconnect(pdev);
97 xen_pcibk_release_devices(pdev);
99 dev_set_drvdata(&pdev->xdev->dev, NULL);
105 static int xen_pcibk_do_attach(struct xen_pcibk_device *pdev, int gnt_ref,
111 dev_dbg(&pdev->xdev->dev,
112 "Attaching to frontend resources - gnt_ref=%d evtchn=%d\n",
113 gnt_ref, remote_evtchn);
115 err = xenbus_map_ring_valloc(pdev->xdev, gnt_ref, &vaddr);
117 xenbus_dev_fatal(pdev->xdev, err,
118 "Error mapping other domain page in ours.");
122 spin_lock(&pdev->dev_lock);
123 pdev->sh_info = vaddr;
124 spin_unlock(&pdev->dev_lock);
126 err = bind_interdomain_evtchn_to_irqhandler(
127 pdev->xdev->otherend_id, remote_evtchn, xen_pcibk_handle_event,
130 xenbus_dev_fatal(pdev->xdev, err,
131 "Error binding event channel to IRQ");
135 spin_lock(&pdev->dev_lock);
136 pdev->evtchn_irq = err;
137 spin_unlock(&pdev->dev_lock);
140 dev_dbg(&pdev->xdev->dev, "Attached!\n");
145 static int xen_pcibk_attach(struct xen_pcibk_device *pdev)
148 int gnt_ref, remote_evtchn;
152 /* Make sure we only do this setup once */
153 if (xenbus_read_driver_state(pdev->xdev->nodename) !=
154 XenbusStateInitialised)
157 /* Wait for frontend to state that it has published the configuration */
158 if (xenbus_read_driver_state(pdev->xdev->otherend) !=
159 XenbusStateInitialised)
162 dev_dbg(&pdev->xdev->dev, "Reading frontend config\n");
164 err = xenbus_gather(XBT_NIL, pdev->xdev->otherend,
165 "pci-op-ref", "%u", &gnt_ref,
166 "event-channel", "%u", &remote_evtchn,
167 "magic", NULL, &magic, NULL);
169 /* If configuration didn't get read correctly, wait longer */
170 xenbus_dev_fatal(pdev->xdev, err,
171 "Error reading configuration from frontend");
175 if (magic == NULL || strcmp(magic, XEN_PCI_MAGIC) != 0) {
176 xenbus_dev_fatal(pdev->xdev, -EFAULT,
177 "version mismatch (%s/%s) with pcifront - "
179 magic, XEN_PCI_MAGIC);
183 err = xen_pcibk_do_attach(pdev, gnt_ref, remote_evtchn);
187 dev_dbg(&pdev->xdev->dev, "Connecting...\n");
189 err = xenbus_switch_state(pdev->xdev, XenbusStateConnected);
191 xenbus_dev_fatal(pdev->xdev, err,
192 "Error switching to connected state!");
194 dev_dbg(&pdev->xdev->dev, "Connected? %d\n", err);
202 static int xen_pcibk_publish_pci_dev(struct xen_pcibk_device *pdev,
203 unsigned int domain, unsigned int bus,
204 unsigned int devfn, unsigned int devid)
210 len = snprintf(str, sizeof(str), "vdev-%d", devid);
211 if (unlikely(len >= (sizeof(str) - 1))) {
216 err = xenbus_printf(XBT_NIL, pdev->xdev->nodename, str,
217 "%04x:%02x:%02x.%02x", domain, bus,
218 PCI_SLOT(devfn), PCI_FUNC(devfn));
224 static int xen_pcibk_export_device(struct xen_pcibk_device *pdev,
225 int domain, int bus, int slot, int func,
231 dev_dbg(&pdev->xdev->dev, "exporting dom %x bus %x slot %x func %x\n",
232 domain, bus, slot, func);
234 dev = pcistub_get_pci_dev_by_slot(pdev, domain, bus, slot, func);
237 xenbus_dev_fatal(pdev->xdev, err,
238 "Couldn't locate PCI device "
239 "(%04x:%02x:%02x.%01x)! "
240 "perhaps already in-use?",
241 domain, bus, slot, func);
245 err = xen_pcibk_add_pci_dev(pdev, dev, devid,
246 xen_pcibk_publish_pci_dev);
250 dev_dbg(&dev->dev, "registering for %d\n", pdev->xdev->otherend_id);
251 if (xen_register_device_domain_owner(dev,
252 pdev->xdev->otherend_id) != 0) {
253 dev_err(&dev->dev, "device has been assigned to another " \
254 "domain! Over-writting the ownership, but beware.\n");
255 xen_unregister_device_domain_owner(dev);
256 xen_register_device_domain_owner(dev, pdev->xdev->otherend_id);
259 /* TODO: It'd be nice to export a bridge and have all of its children
260 * get exported with it. This may be best done in xend (which will
261 * have to calculate resource usage anyway) but we probably want to
262 * put something in here to ensure that if a bridge gets given to a
263 * driver domain, that all devices under that bridge are not given
264 * to other driver domains (as he who controls the bridge can disable
265 * it and stop the other devices from working).
271 static int xen_pcibk_remove_device(struct xen_pcibk_device *pdev,
272 int domain, int bus, int slot, int func)
277 dev_dbg(&pdev->xdev->dev, "removing dom %x bus %x slot %x func %x\n",
278 domain, bus, slot, func);
280 dev = xen_pcibk_get_pci_dev(pdev, domain, bus, PCI_DEVFN(slot, func));
283 dev_dbg(&pdev->xdev->dev, "Couldn't locate PCI device "
284 "(%04x:%02x:%02x.%01x)! not owned by this domain\n",
285 domain, bus, slot, func);
289 dev_dbg(&dev->dev, "unregistering for %d\n", pdev->xdev->otherend_id);
290 xen_unregister_device_domain_owner(dev);
292 xen_pcibk_release_pci_dev(pdev, dev);
298 static int xen_pcibk_publish_pci_root(struct xen_pcibk_device *pdev,
299 unsigned int domain, unsigned int bus)
302 int i, root_num, len, err;
305 dev_dbg(&pdev->xdev->dev, "Publishing pci roots\n");
307 err = xenbus_scanf(XBT_NIL, pdev->xdev->nodename,
308 "root_num", "%d", &root_num);
309 if (err == 0 || err == -ENOENT)
314 /* Verify that we haven't already published this pci root */
315 for (i = 0; i < root_num; i++) {
316 len = snprintf(str, sizeof(str), "root-%d", i);
317 if (unlikely(len >= (sizeof(str) - 1))) {
322 err = xenbus_scanf(XBT_NIL, pdev->xdev->nodename,
323 str, "%x:%x", &d, &b);
331 if (d == domain && b == bus) {
337 len = snprintf(str, sizeof(str), "root-%d", root_num);
338 if (unlikely(len >= (sizeof(str) - 1))) {
343 dev_dbg(&pdev->xdev->dev, "writing root %d at %04x:%02x\n",
344 root_num, domain, bus);
346 err = xenbus_printf(XBT_NIL, pdev->xdev->nodename, str,
347 "%04x:%02x", domain, bus);
351 err = xenbus_printf(XBT_NIL, pdev->xdev->nodename,
352 "root_num", "%d", (root_num + 1));
358 static int xen_pcibk_reconfigure(struct xen_pcibk_device *pdev)
362 int domain, bus, slot, func;
369 dev_dbg(&pdev->xdev->dev, "Reconfiguring device ...\n");
371 /* Make sure we only reconfigure once */
372 if (xenbus_read_driver_state(pdev->xdev->nodename) !=
373 XenbusStateReconfiguring)
376 err = xenbus_scanf(XBT_NIL, pdev->xdev->nodename, "num_devs", "%d",
381 xenbus_dev_fatal(pdev->xdev, err,
382 "Error reading number of devices");
386 for (i = 0; i < num_devs; i++) {
387 len = snprintf(state_str, sizeof(state_str), "state-%d", i);
388 if (unlikely(len >= (sizeof(state_str) - 1))) {
390 xenbus_dev_fatal(pdev->xdev, err,
391 "String overflow while reading "
395 err = xenbus_scanf(XBT_NIL, pdev->xdev->nodename, state_str,
398 substate = XenbusStateUnknown;
401 case XenbusStateInitialising:
402 dev_dbg(&pdev->xdev->dev, "Attaching dev-%d ...\n", i);
404 len = snprintf(dev_str, sizeof(dev_str), "dev-%d", i);
405 if (unlikely(len >= (sizeof(dev_str) - 1))) {
407 xenbus_dev_fatal(pdev->xdev, err,
408 "String overflow while "
409 "reading configuration");
412 err = xenbus_scanf(XBT_NIL, pdev->xdev->nodename,
413 dev_str, "%x:%x:%x.%x",
414 &domain, &bus, &slot, &func);
416 xenbus_dev_fatal(pdev->xdev, err,
417 "Error reading device "
423 xenbus_dev_fatal(pdev->xdev, err,
424 "Error parsing pci device "
429 err = xen_pcibk_export_device(pdev, domain, bus, slot,
434 /* Publish pci roots. */
435 err = xen_pcibk_publish_pci_roots(pdev,
436 xen_pcibk_publish_pci_root);
438 xenbus_dev_fatal(pdev->xdev, err,
439 "Error while publish PCI root"
440 "buses for frontend");
444 err = xenbus_printf(XBT_NIL, pdev->xdev->nodename,
446 XenbusStateInitialised);
448 xenbus_dev_fatal(pdev->xdev, err,
449 "Error switching substate of "
455 case XenbusStateClosing:
456 dev_dbg(&pdev->xdev->dev, "Detaching dev-%d ...\n", i);
458 len = snprintf(dev_str, sizeof(dev_str), "vdev-%d", i);
459 if (unlikely(len >= (sizeof(dev_str) - 1))) {
461 xenbus_dev_fatal(pdev->xdev, err,
462 "String overflow while "
463 "reading configuration");
466 err = xenbus_scanf(XBT_NIL, pdev->xdev->nodename,
467 dev_str, "%x:%x:%x.%x",
468 &domain, &bus, &slot, &func);
470 xenbus_dev_fatal(pdev->xdev, err,
471 "Error reading device "
477 xenbus_dev_fatal(pdev->xdev, err,
478 "Error parsing pci device "
483 err = xen_pcibk_remove_device(pdev, domain, bus, slot,
488 /* TODO: If at some point we implement support for pci
489 * root hot-remove on pcifront side, we'll need to
490 * remove unnecessary xenstore nodes of pci roots here.
500 err = xenbus_switch_state(pdev->xdev, XenbusStateReconfigured);
502 xenbus_dev_fatal(pdev->xdev, err,
503 "Error switching to reconfigured state!");
511 static void xen_pcibk_frontend_changed(struct xenbus_device *xdev,
512 enum xenbus_state fe_state)
514 struct xen_pcibk_device *pdev = dev_get_drvdata(&xdev->dev);
516 dev_dbg(&xdev->dev, "fe state changed %d\n", fe_state);
519 case XenbusStateInitialised:
520 xen_pcibk_attach(pdev);
523 case XenbusStateReconfiguring:
524 xen_pcibk_reconfigure(pdev);
527 case XenbusStateConnected:
528 /* pcifront switched its state from reconfiguring to connected.
529 * Then switch to connected state.
531 xenbus_switch_state(xdev, XenbusStateConnected);
534 case XenbusStateClosing:
535 xen_pcibk_disconnect(pdev);
536 xenbus_switch_state(xdev, XenbusStateClosing);
539 case XenbusStateClosed:
540 xen_pcibk_disconnect(pdev);
541 xenbus_switch_state(xdev, XenbusStateClosed);
542 if (xenbus_dev_is_online(xdev))
544 /* fall through if not online */
545 case XenbusStateUnknown:
546 dev_dbg(&xdev->dev, "frontend is gone! unregister device\n");
547 device_unregister(&xdev->dev);
555 static int xen_pcibk_setup_backend(struct xen_pcibk_device *pdev)
557 /* Get configuration from xend (if available now) */
558 int domain, bus, slot, func;
564 /* It's possible we could get the call to setup twice, so make sure
565 * we're not already connected.
567 if (xenbus_read_driver_state(pdev->xdev->nodename) !=
571 dev_dbg(&pdev->xdev->dev, "getting be setup\n");
573 err = xenbus_scanf(XBT_NIL, pdev->xdev->nodename, "num_devs", "%d",
578 xenbus_dev_fatal(pdev->xdev, err,
579 "Error reading number of devices");
583 for (i = 0; i < num_devs; i++) {
584 int l = snprintf(dev_str, sizeof(dev_str), "dev-%d", i);
585 if (unlikely(l >= (sizeof(dev_str) - 1))) {
587 xenbus_dev_fatal(pdev->xdev, err,
588 "String overflow while reading "
593 err = xenbus_scanf(XBT_NIL, pdev->xdev->nodename, dev_str,
594 "%x:%x:%x.%x", &domain, &bus, &slot, &func);
596 xenbus_dev_fatal(pdev->xdev, err,
597 "Error reading device configuration");
602 xenbus_dev_fatal(pdev->xdev, err,
603 "Error parsing pci device "
608 err = xen_pcibk_export_device(pdev, domain, bus, slot, func, i);
612 /* Switch substate of this device. */
613 l = snprintf(state_str, sizeof(state_str), "state-%d", i);
614 if (unlikely(l >= (sizeof(state_str) - 1))) {
616 xenbus_dev_fatal(pdev->xdev, err,
617 "String overflow while reading "
621 err = xenbus_printf(XBT_NIL, pdev->xdev->nodename, state_str,
622 "%d", XenbusStateInitialised);
624 xenbus_dev_fatal(pdev->xdev, err, "Error switching "
625 "substate of dev-%d\n", i);
630 err = xen_pcibk_publish_pci_roots(pdev, xen_pcibk_publish_pci_root);
632 xenbus_dev_fatal(pdev->xdev, err,
633 "Error while publish PCI root buses "
638 err = xenbus_switch_state(pdev->xdev, XenbusStateInitialised);
640 xenbus_dev_fatal(pdev->xdev, err,
641 "Error switching to initialised state!");
645 /* see if pcifront is already configured (if not, we'll wait) */
646 xen_pcibk_attach(pdev);
651 static void xen_pcibk_be_watch(struct xenbus_watch *watch,
652 const char **vec, unsigned int len)
654 struct xen_pcibk_device *pdev =
655 container_of(watch, struct xen_pcibk_device, be_watch);
657 switch (xenbus_read_driver_state(pdev->xdev->nodename)) {
658 case XenbusStateInitWait:
659 xen_pcibk_setup_backend(pdev);
667 static int xen_pcibk_xenbus_probe(struct xenbus_device *dev,
668 const struct xenbus_device_id *id)
671 struct xen_pcibk_device *pdev = alloc_pdev(dev);
675 xenbus_dev_fatal(dev, err,
676 "Error allocating xen_pcibk_device struct");
680 /* wait for xend to configure us */
681 err = xenbus_switch_state(dev, XenbusStateInitWait);
685 /* watch the backend node for backend configuration information */
686 err = xenbus_watch_path(dev, dev->nodename, &pdev->be_watch,
691 pdev->be_watching = 1;
693 /* We need to force a call to our callback here in case
694 * xend already configured us!
696 xen_pcibk_be_watch(&pdev->be_watch, NULL, 0);
702 static int xen_pcibk_xenbus_remove(struct xenbus_device *dev)
704 struct xen_pcibk_device *pdev = dev_get_drvdata(&dev->dev);
712 static const struct xenbus_device_id xenpci_ids[] = {
717 static struct xenbus_driver xenbus_xen_pcibk_driver = {
719 .owner = THIS_MODULE,
721 .probe = xen_pcibk_xenbus_probe,
722 .remove = xen_pcibk_xenbus_remove,
723 .otherend_changed = xen_pcibk_frontend_changed,
726 const struct xen_pcibk_backend *__read_mostly xen_pcibk_backend;
728 int __init xen_pcibk_xenbus_register(void)
730 xen_pcibk_wq = create_workqueue("xen_pciback_workqueue");
732 printk(KERN_ERR "%s: create"
733 "xen_pciback_workqueue failed\n", __func__);
736 xen_pcibk_backend = &xen_pcibk_vpci_backend;
738 xen_pcibk_backend = &xen_pcibk_passthrough_backend;
739 pr_info(DRV_NAME ": backend is %s\n", xen_pcibk_backend->name);
740 return xenbus_register_backend(&xenbus_xen_pcibk_driver);
743 void __exit xen_pcibk_xenbus_unregister(void)
745 destroy_workqueue(xen_pcibk_wq);
746 xenbus_unregister_driver(&xenbus_xen_pcibk_driver);