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 <linux/workqueue.h>
16 #define INVALID_EVTCHN_IRQ (-1)
17 struct workqueue_struct *pciback_wq;
19 static struct pciback_device *alloc_pdev(struct xenbus_device *xdev)
21 struct pciback_device *pdev;
23 pdev = kzalloc(sizeof(struct pciback_device), GFP_KERNEL);
26 dev_dbg(&xdev->dev, "allocated pdev @ 0x%p\n", pdev);
29 dev_set_drvdata(&xdev->dev, pdev);
31 spin_lock_init(&pdev->dev_lock);
34 pdev->evtchn_irq = INVALID_EVTCHN_IRQ;
35 pdev->be_watching = 0;
37 INIT_WORK(&pdev->op_work, pciback_do_op);
39 if (pciback_init_devices(pdev)) {
47 static void pciback_disconnect(struct pciback_device *pdev)
49 spin_lock(&pdev->dev_lock);
51 /* Ensure the guest can't trigger our handler before removing devices */
52 if (pdev->evtchn_irq != INVALID_EVTCHN_IRQ) {
53 unbind_from_irqhandler(pdev->evtchn_irq, pdev);
54 pdev->evtchn_irq = INVALID_EVTCHN_IRQ;
57 /* If the driver domain started an op, make sure we complete it
58 * before releasing the shared memory */
59 flush_workqueue(pciback_wq);
61 if (pdev->sh_info != NULL) {
62 xenbus_unmap_ring_vfree(pdev->xdev, pdev->sh_info);
66 spin_unlock(&pdev->dev_lock);
69 static void free_pdev(struct pciback_device *pdev)
71 if (pdev->be_watching)
72 unregister_xenbus_watch(&pdev->be_watch);
74 pciback_disconnect(pdev);
76 pciback_release_devices(pdev);
78 dev_set_drvdata(&pdev->xdev->dev, NULL);
84 static int pciback_do_attach(struct pciback_device *pdev, int gnt_ref,
90 dev_dbg(&pdev->xdev->dev,
91 "Attaching to frontend resources - gnt_ref=%d evtchn=%d\n",
92 gnt_ref, remote_evtchn);
94 err = xenbus_map_ring_valloc(pdev->xdev, gnt_ref, &vaddr);
96 xenbus_dev_fatal(pdev->xdev, err,
97 "Error mapping other domain page in ours.");
100 pdev->sh_info = vaddr;
102 err = bind_interdomain_evtchn_to_irqhandler(
103 pdev->xdev->otherend_id, remote_evtchn, pciback_handle_event,
106 xenbus_dev_fatal(pdev->xdev, err,
107 "Error binding event channel to IRQ");
110 pdev->evtchn_irq = err;
113 dev_dbg(&pdev->xdev->dev, "Attached!\n");
118 static int pciback_attach(struct pciback_device *pdev)
121 int gnt_ref, remote_evtchn;
124 spin_lock(&pdev->dev_lock);
126 /* Make sure we only do this setup once */
127 if (xenbus_read_driver_state(pdev->xdev->nodename) !=
128 XenbusStateInitialised)
131 /* Wait for frontend to state that it has published the configuration */
132 if (xenbus_read_driver_state(pdev->xdev->otherend) !=
133 XenbusStateInitialised)
136 dev_dbg(&pdev->xdev->dev, "Reading frontend config\n");
138 err = xenbus_gather(XBT_NIL, pdev->xdev->otherend,
139 "pci-op-ref", "%u", &gnt_ref,
140 "event-channel", "%u", &remote_evtchn,
141 "magic", NULL, &magic, NULL);
143 /* If configuration didn't get read correctly, wait longer */
144 xenbus_dev_fatal(pdev->xdev, err,
145 "Error reading configuration from frontend");
149 if (magic == NULL || strcmp(magic, XEN_PCI_MAGIC) != 0) {
150 xenbus_dev_fatal(pdev->xdev, -EFAULT,
151 "version mismatch (%s/%s) with pcifront - "
153 magic, XEN_PCI_MAGIC);
157 err = pciback_do_attach(pdev, gnt_ref, remote_evtchn);
161 dev_dbg(&pdev->xdev->dev, "Connecting...\n");
163 err = xenbus_switch_state(pdev->xdev, XenbusStateConnected);
165 xenbus_dev_fatal(pdev->xdev, err,
166 "Error switching to connected state!");
168 dev_dbg(&pdev->xdev->dev, "Connected? %d\n", err);
170 spin_unlock(&pdev->dev_lock);
177 static int pciback_publish_pci_dev(struct pciback_device *pdev,
178 unsigned int domain, unsigned int bus,
179 unsigned int devfn, unsigned int devid)
185 len = snprintf(str, sizeof(str), "vdev-%d", devid);
186 if (unlikely(len >= (sizeof(str) - 1))) {
191 err = xenbus_printf(XBT_NIL, pdev->xdev->nodename, str,
192 "%04x:%02x:%02x.%02x", domain, bus,
193 PCI_SLOT(devfn), PCI_FUNC(devfn));
199 static int pciback_export_device(struct pciback_device *pdev,
200 int domain, int bus, int slot, int func,
206 dev_dbg(&pdev->xdev->dev, "exporting dom %x bus %x slot %x func %x\n",
207 domain, bus, slot, func);
209 dev = pcistub_get_pci_dev_by_slot(pdev, domain, bus, slot, func);
212 xenbus_dev_fatal(pdev->xdev, err,
213 "Couldn't locate PCI device "
214 "(%04x:%02x:%02x.%01x)! "
215 "perhaps already in-use?",
216 domain, bus, slot, func);
220 err = pciback_add_pci_dev(pdev, dev, devid, pciback_publish_pci_dev);
224 /* TODO: It'd be nice to export a bridge and have all of its children
225 * get exported with it. This may be best done in xend (which will
226 * have to calculate resource usage anyway) but we probably want to
227 * put something in here to ensure that if a bridge gets given to a
228 * driver domain, that all devices under that bridge are not given
229 * to other driver domains (as he who controls the bridge can disable
230 * it and stop the other devices from working).
236 static int pciback_remove_device(struct pciback_device *pdev,
237 int domain, int bus, int slot, int func)
242 dev_dbg(&pdev->xdev->dev, "removing dom %x bus %x slot %x func %x\n",
243 domain, bus, slot, func);
245 dev = pciback_get_pci_dev(pdev, domain, bus, PCI_DEVFN(slot, func));
248 dev_dbg(&pdev->xdev->dev, "Couldn't locate PCI device "
249 "(%04x:%02x:%02x.%01x)! not owned by this domain\n",
250 domain, bus, slot, func);
254 pciback_release_pci_dev(pdev, dev);
260 static int pciback_publish_pci_root(struct pciback_device *pdev,
261 unsigned int domain, unsigned int bus)
264 int i, root_num, len, err;
267 dev_dbg(&pdev->xdev->dev, "Publishing pci roots\n");
269 err = xenbus_scanf(XBT_NIL, pdev->xdev->nodename,
270 "root_num", "%d", &root_num);
271 if (err == 0 || err == -ENOENT)
276 /* Verify that we haven't already published this pci root */
277 for (i = 0; i < root_num; i++) {
278 len = snprintf(str, sizeof(str), "root-%d", i);
279 if (unlikely(len >= (sizeof(str) - 1))) {
284 err = xenbus_scanf(XBT_NIL, pdev->xdev->nodename,
285 str, "%x:%x", &d, &b);
293 if (d == domain && b == bus) {
299 len = snprintf(str, sizeof(str), "root-%d", root_num);
300 if (unlikely(len >= (sizeof(str) - 1))) {
305 dev_dbg(&pdev->xdev->dev, "writing root %d at %04x:%02x\n",
306 root_num, domain, bus);
308 err = xenbus_printf(XBT_NIL, pdev->xdev->nodename, str,
309 "%04x:%02x", domain, bus);
313 err = xenbus_printf(XBT_NIL, pdev->xdev->nodename,
314 "root_num", "%d", (root_num + 1));
320 static int pciback_reconfigure(struct pciback_device *pdev)
324 int domain, bus, slot, func;
330 spin_lock(&pdev->dev_lock);
332 dev_dbg(&pdev->xdev->dev, "Reconfiguring device ...\n");
334 /* Make sure we only reconfigure once */
335 if (xenbus_read_driver_state(pdev->xdev->nodename) !=
336 XenbusStateReconfiguring)
339 err = xenbus_scanf(XBT_NIL, pdev->xdev->nodename, "num_devs", "%d",
344 xenbus_dev_fatal(pdev->xdev, err,
345 "Error reading number of devices");
349 for (i = 0; i < num_devs; i++) {
350 len = snprintf(state_str, sizeof(state_str), "state-%d", i);
351 if (unlikely(len >= (sizeof(state_str) - 1))) {
353 xenbus_dev_fatal(pdev->xdev, err,
354 "String overflow while reading "
358 err = xenbus_scanf(XBT_NIL, pdev->xdev->nodename, state_str,
361 substate = XenbusStateUnknown;
364 case XenbusStateInitialising:
365 dev_dbg(&pdev->xdev->dev, "Attaching dev-%d ...\n", i);
367 len = snprintf(dev_str, sizeof(dev_str), "dev-%d", i);
368 if (unlikely(len >= (sizeof(dev_str) - 1))) {
370 xenbus_dev_fatal(pdev->xdev, err,
371 "String overflow while "
372 "reading configuration");
375 err = xenbus_scanf(XBT_NIL, pdev->xdev->nodename,
376 dev_str, "%x:%x:%x.%x",
377 &domain, &bus, &slot, &func);
379 xenbus_dev_fatal(pdev->xdev, err,
380 "Error reading device "
386 xenbus_dev_fatal(pdev->xdev, err,
387 "Error parsing pci device "
392 err = pciback_export_device(pdev, domain, bus, slot,
397 /* Publish pci roots. */
398 err = pciback_publish_pci_roots(pdev,
399 pciback_publish_pci_root);
401 xenbus_dev_fatal(pdev->xdev, err,
402 "Error while publish PCI root"
403 "buses for frontend");
407 err = xenbus_printf(XBT_NIL, pdev->xdev->nodename,
409 XenbusStateInitialised);
411 xenbus_dev_fatal(pdev->xdev, err,
412 "Error switching substate of "
418 case XenbusStateClosing:
419 dev_dbg(&pdev->xdev->dev, "Detaching dev-%d ...\n", i);
421 len = snprintf(dev_str, sizeof(dev_str), "vdev-%d", i);
422 if (unlikely(len >= (sizeof(dev_str) - 1))) {
424 xenbus_dev_fatal(pdev->xdev, err,
425 "String overflow while "
426 "reading configuration");
429 err = xenbus_scanf(XBT_NIL, pdev->xdev->nodename,
430 dev_str, "%x:%x:%x.%x",
431 &domain, &bus, &slot, &func);
433 xenbus_dev_fatal(pdev->xdev, err,
434 "Error reading device "
440 xenbus_dev_fatal(pdev->xdev, err,
441 "Error parsing pci device "
446 err = pciback_remove_device(pdev, domain, bus, slot,
451 /* TODO: If at some point we implement support for pci
452 * root hot-remove on pcifront side, we'll need to
453 * remove unnecessary xenstore nodes of pci roots here.
463 err = xenbus_switch_state(pdev->xdev, XenbusStateReconfigured);
465 xenbus_dev_fatal(pdev->xdev, err,
466 "Error switching to reconfigured state!");
471 spin_unlock(&pdev->dev_lock);
476 static void pciback_frontend_changed(struct xenbus_device *xdev,
477 enum xenbus_state fe_state)
479 struct pciback_device *pdev = dev_get_drvdata(&xdev->dev);
481 dev_dbg(&xdev->dev, "fe state changed %d\n", fe_state);
484 case XenbusStateInitialised:
485 pciback_attach(pdev);
488 case XenbusStateReconfiguring:
489 pciback_reconfigure(pdev);
492 case XenbusStateConnected:
493 /* pcifront switched its state from reconfiguring to connected.
494 * Then switch to connected state.
496 xenbus_switch_state(xdev, XenbusStateConnected);
499 case XenbusStateClosing:
500 pciback_disconnect(pdev);
501 xenbus_switch_state(xdev, XenbusStateClosing);
504 case XenbusStateClosed:
505 pciback_disconnect(pdev);
506 xenbus_switch_state(xdev, XenbusStateClosed);
507 if (xenbus_dev_is_online(xdev))
509 /* fall through if not online */
510 case XenbusStateUnknown:
511 dev_dbg(&xdev->dev, "frontend is gone! unregister device\n");
512 device_unregister(&xdev->dev);
520 static int pciback_setup_backend(struct pciback_device *pdev)
522 /* Get configuration from xend (if available now) */
523 int domain, bus, slot, func;
529 spin_lock(&pdev->dev_lock);
531 /* It's possible we could get the call to setup twice, so make sure
532 * we're not already connected.
534 if (xenbus_read_driver_state(pdev->xdev->nodename) !=
538 dev_dbg(&pdev->xdev->dev, "getting be setup\n");
540 err = xenbus_scanf(XBT_NIL, pdev->xdev->nodename, "num_devs", "%d",
545 xenbus_dev_fatal(pdev->xdev, err,
546 "Error reading number of devices");
550 for (i = 0; i < num_devs; i++) {
551 int l = snprintf(dev_str, sizeof(dev_str), "dev-%d", i);
552 if (unlikely(l >= (sizeof(dev_str) - 1))) {
554 xenbus_dev_fatal(pdev->xdev, err,
555 "String overflow while reading "
560 err = xenbus_scanf(XBT_NIL, pdev->xdev->nodename, dev_str,
561 "%x:%x:%x.%x", &domain, &bus, &slot, &func);
563 xenbus_dev_fatal(pdev->xdev, err,
564 "Error reading device configuration");
569 xenbus_dev_fatal(pdev->xdev, err,
570 "Error parsing pci device "
575 err = pciback_export_device(pdev, domain, bus, slot, func, i);
579 /* Switch substate of this device. */
580 l = snprintf(state_str, sizeof(state_str), "state-%d", i);
581 if (unlikely(l >= (sizeof(state_str) - 1))) {
583 xenbus_dev_fatal(pdev->xdev, err,
584 "String overflow while reading "
588 err = xenbus_printf(XBT_NIL, pdev->xdev->nodename, state_str,
589 "%d", XenbusStateInitialised);
591 xenbus_dev_fatal(pdev->xdev, err, "Error switching "
592 "substate of dev-%d\n", i);
597 err = pciback_publish_pci_roots(pdev, pciback_publish_pci_root);
599 xenbus_dev_fatal(pdev->xdev, err,
600 "Error while publish PCI root buses "
605 err = xenbus_switch_state(pdev->xdev, XenbusStateInitialised);
607 xenbus_dev_fatal(pdev->xdev, err,
608 "Error switching to initialised state!");
611 spin_unlock(&pdev->dev_lock);
614 /* see if pcifront is already configured (if not, we'll wait) */
615 pciback_attach(pdev);
620 static void pciback_be_watch(struct xenbus_watch *watch,
621 const char **vec, unsigned int len)
623 struct pciback_device *pdev =
624 container_of(watch, struct pciback_device, be_watch);
626 switch (xenbus_read_driver_state(pdev->xdev->nodename)) {
627 case XenbusStateInitWait:
628 pciback_setup_backend(pdev);
636 static int pciback_xenbus_probe(struct xenbus_device *dev,
637 const struct xenbus_device_id *id)
640 struct pciback_device *pdev = alloc_pdev(dev);
644 xenbus_dev_fatal(dev, err,
645 "Error allocating pciback_device struct");
649 /* wait for xend to configure us */
650 err = xenbus_switch_state(dev, XenbusStateInitWait);
654 /* watch the backend node for backend configuration information */
655 err = xenbus_watch_path(dev, dev->nodename, &pdev->be_watch,
659 pdev->be_watching = 1;
661 /* We need to force a call to our callback here in case
662 * xend already configured us!
664 pciback_be_watch(&pdev->be_watch, NULL, 0);
670 static int pciback_xenbus_remove(struct xenbus_device *dev)
672 struct pciback_device *pdev = dev_get_drvdata(&dev->dev);
680 static const struct xenbus_device_id xenpci_ids[] = {
685 static struct xenbus_driver xenbus_pciback_driver = {
687 .owner = THIS_MODULE,
689 .probe = pciback_xenbus_probe,
690 .remove = pciback_xenbus_remove,
691 .otherend_changed = pciback_frontend_changed,
694 int __init pciback_xenbus_register(void)
696 pciback_wq = create_workqueue("pciback_workqueue");
698 printk(KERN_ERR "%s: create"
699 "pciback_workqueue failed\n", __func__);
702 return xenbus_register_backend(&xenbus_pciback_driver);
705 void __exit pciback_xenbus_unregister(void)
707 destroy_workqueue(pciback_wq);
708 xenbus_unregister_driver(&xenbus_pciback_driver);