1 // SPDX-License-Identifier: GPL-2.0
3 * PCI Backend Xenbus Setup - handles setup with frontend and xend
8 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
10 #include <linux/moduleparam.h>
11 #include <linux/init.h>
12 #include <linux/list.h>
13 #include <linux/vmalloc.h>
14 #include <linux/workqueue.h>
15 #include <xen/xenbus.h>
16 #include <xen/events.h>
17 #include <asm/xen/pci.h>
20 #define INVALID_EVTCHN_IRQ (-1)
22 static bool __read_mostly passthrough;
23 module_param(passthrough, bool, S_IRUGO);
24 MODULE_PARM_DESC(passthrough,
25 "Option to specify how to export PCI topology to guest:\n"\
26 " 0 - (default) Hide the true PCI topology and makes the frontend\n"\
27 " there is a single PCI bus with only the exported devices on it.\n"\
28 " For example, a device at 03:05.0 will be re-assigned to 00:00.0\n"\
29 " while second device at 02:1a.1 will be re-assigned to 00:01.1.\n"\
30 " 1 - Passthrough provides a real view of the PCI topology to the\n"\
31 " frontend (for example, a device at 06:01.b will still appear at\n"\
32 " 06:01.b to the frontend). This is similar to how Xen 2.0.x\n"\
33 " exposed PCI devices to its driver domains. This may be required\n"\
34 " for drivers which depend on finding their hardward in certain\n"\
35 " bus/slot locations.");
37 static struct xen_pcibk_device *alloc_pdev(struct xenbus_device *xdev)
39 struct xen_pcibk_device *pdev;
41 pdev = kzalloc(sizeof(struct xen_pcibk_device), GFP_KERNEL);
44 dev_dbg(&xdev->dev, "allocated pdev @ 0x%p\n", pdev);
48 mutex_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)) {
61 dev_set_drvdata(&xdev->dev, pdev);
67 static void xen_pcibk_disconnect(struct xen_pcibk_device *pdev)
69 mutex_lock(&pdev->dev_lock);
70 /* Ensure the guest can't trigger our handler before removing devices */
71 if (pdev->evtchn_irq != INVALID_EVTCHN_IRQ) {
72 unbind_from_irqhandler(pdev->evtchn_irq, pdev);
73 pdev->evtchn_irq = INVALID_EVTCHN_IRQ;
76 /* If the driver domain started an op, make sure we complete it
77 * before releasing the shared memory */
79 flush_work(&pdev->op_work);
81 if (pdev->sh_info != NULL) {
82 xenbus_unmap_ring_vfree(pdev->xdev, pdev->sh_info);
85 mutex_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 /* N.B. This calls pcistub_put_pci_dev which does the FLR on all
98 * of the PCIe devices. */
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, 1, &vaddr);
119 xenbus_dev_fatal(pdev->xdev, err,
120 "Error mapping other domain page in ours.");
124 pdev->sh_info = vaddr;
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");
134 pdev->evtchn_irq = err;
137 dev_dbg(&pdev->xdev->dev, "Attached!\n");
142 static int xen_pcibk_attach(struct xen_pcibk_device *pdev)
145 int gnt_ref, remote_evtchn;
149 mutex_lock(&pdev->dev_lock);
150 /* Make sure we only do this setup once */
151 if (xenbus_read_driver_state(pdev->xdev->nodename) !=
152 XenbusStateInitialised)
155 /* Wait for frontend to state that it has published the configuration */
156 if (xenbus_read_driver_state(pdev->xdev->otherend) !=
157 XenbusStateInitialised)
160 dev_dbg(&pdev->xdev->dev, "Reading frontend config\n");
162 err = xenbus_gather(XBT_NIL, pdev->xdev->otherend,
163 "pci-op-ref", "%u", &gnt_ref,
164 "event-channel", "%u", &remote_evtchn,
165 "magic", NULL, &magic, NULL);
167 /* If configuration didn't get read correctly, wait longer */
168 xenbus_dev_fatal(pdev->xdev, err,
169 "Error reading configuration from frontend");
173 if (magic == NULL || strcmp(magic, XEN_PCI_MAGIC) != 0) {
174 xenbus_dev_fatal(pdev->xdev, -EFAULT,
175 "version mismatch (%s/%s) with pcifront - "
177 magic, XEN_PCI_MAGIC);
182 err = xen_pcibk_do_attach(pdev, gnt_ref, remote_evtchn);
186 dev_dbg(&pdev->xdev->dev, "Connecting...\n");
188 err = xenbus_switch_state(pdev->xdev, XenbusStateConnected);
190 xenbus_dev_fatal(pdev->xdev, err,
191 "Error switching to connected state!");
193 dev_dbg(&pdev->xdev->dev, "Connected? %d\n", err);
195 mutex_unlock(&pdev->dev_lock);
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 /* Note: The PV protocol uses %02x, don't change it */
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.%d)! "
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_info(&dev->dev, "registering for %d\n", pdev->xdev->otherend_id);
252 if (xen_register_device_domain_owner(dev,
253 pdev->xdev->otherend_id) != 0) {
254 dev_err(&dev->dev, "Stealing ownership from dom%d.\n",
255 xen_find_device_domain_owner(dev));
256 xen_unregister_device_domain_owner(dev);
257 xen_register_device_domain_owner(dev, pdev->xdev->otherend_id);
260 /* TODO: It'd be nice to export a bridge and have all of its children
261 * get exported with it. This may be best done in xend (which will
262 * have to calculate resource usage anyway) but we probably want to
263 * put something in here to ensure that if a bridge gets given to a
264 * driver domain, that all devices under that bridge are not given
265 * to other driver domains (as he who controls the bridge can disable
266 * it and stop the other devices from working).
272 static int xen_pcibk_remove_device(struct xen_pcibk_device *pdev,
273 int domain, int bus, int slot, int func)
278 dev_dbg(&pdev->xdev->dev, "removing dom %x bus %x slot %x func %x\n",
279 domain, bus, slot, func);
281 dev = xen_pcibk_get_pci_dev(pdev, domain, bus, PCI_DEVFN(slot, func));
284 dev_dbg(&pdev->xdev->dev, "Couldn't locate PCI device "
285 "(%04x:%02x:%02x.%d)! not owned by this domain\n",
286 domain, bus, slot, func);
290 dev_dbg(&dev->dev, "unregistering for %d\n", pdev->xdev->otherend_id);
291 xen_unregister_device_domain_owner(dev);
293 /* N.B. This ends up calling pcistub_put_pci_dev which ends up
295 xen_pcibk_release_pci_dev(pdev, dev, true /* use the lock. */);
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;
366 unsigned int substate;
372 dev_dbg(&pdev->xdev->dev, "Reconfiguring device ...\n");
374 mutex_lock(&pdev->dev_lock);
375 /* Make sure we only reconfigure once */
376 if (xenbus_read_driver_state(pdev->xdev->nodename) !=
377 XenbusStateReconfiguring)
380 err = xenbus_scanf(XBT_NIL, pdev->xdev->nodename, "num_devs", "%d",
385 xenbus_dev_fatal(pdev->xdev, err,
386 "Error reading number of devices");
390 for (i = 0; i < num_devs; i++) {
391 len = snprintf(state_str, sizeof(state_str), "state-%d", i);
392 if (unlikely(len >= (sizeof(state_str) - 1))) {
394 xenbus_dev_fatal(pdev->xdev, err,
395 "String overflow while reading "
399 substate = xenbus_read_unsigned(pdev->xdev->nodename, state_str,
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!");
510 mutex_unlock(&pdev->dev_lock);
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 mutex_lock(&pdev->dev_lock);
568 /* It's possible we could get the call to setup twice, so make sure
569 * we're not already connected.
571 if (xenbus_read_driver_state(pdev->xdev->nodename) !=
575 dev_dbg(&pdev->xdev->dev, "getting be setup\n");
577 err = xenbus_scanf(XBT_NIL, pdev->xdev->nodename, "num_devs", "%d",
582 xenbus_dev_fatal(pdev->xdev, err,
583 "Error reading number of devices");
587 for (i = 0; i < num_devs; i++) {
588 int l = snprintf(dev_str, sizeof(dev_str), "dev-%d", i);
589 if (unlikely(l >= (sizeof(dev_str) - 1))) {
591 xenbus_dev_fatal(pdev->xdev, err,
592 "String overflow while reading "
597 err = xenbus_scanf(XBT_NIL, pdev->xdev->nodename, dev_str,
598 "%x:%x:%x.%x", &domain, &bus, &slot, &func);
600 xenbus_dev_fatal(pdev->xdev, err,
601 "Error reading device configuration");
606 xenbus_dev_fatal(pdev->xdev, err,
607 "Error parsing pci device "
612 err = xen_pcibk_export_device(pdev, domain, bus, slot, func, i);
616 /* Switch substate of this device. */
617 l = snprintf(state_str, sizeof(state_str), "state-%d", i);
618 if (unlikely(l >= (sizeof(state_str) - 1))) {
620 xenbus_dev_fatal(pdev->xdev, err,
621 "String overflow while reading "
625 err = xenbus_printf(XBT_NIL, pdev->xdev->nodename, state_str,
626 "%d", XenbusStateInitialised);
628 xenbus_dev_fatal(pdev->xdev, err, "Error switching "
629 "substate of dev-%d\n", i);
634 err = xen_pcibk_publish_pci_roots(pdev, xen_pcibk_publish_pci_root);
636 xenbus_dev_fatal(pdev->xdev, err,
637 "Error while publish PCI root buses "
642 err = xenbus_switch_state(pdev->xdev, XenbusStateInitialised);
644 xenbus_dev_fatal(pdev->xdev, err,
645 "Error switching to initialised state!");
648 mutex_unlock(&pdev->dev_lock);
650 /* see if pcifront is already configured (if not, we'll wait) */
651 xen_pcibk_attach(pdev);
655 static void xen_pcibk_be_watch(struct xenbus_watch *watch,
656 const char *path, const char *token)
658 struct xen_pcibk_device *pdev =
659 container_of(watch, struct xen_pcibk_device, be_watch);
661 switch (xenbus_read_driver_state(pdev->xdev->nodename)) {
662 case XenbusStateInitWait:
663 xen_pcibk_setup_backend(pdev);
671 static int xen_pcibk_xenbus_probe(struct xenbus_device *dev,
672 const struct xenbus_device_id *id)
675 struct xen_pcibk_device *pdev = alloc_pdev(dev);
679 xenbus_dev_fatal(dev, err,
680 "Error allocating xen_pcibk_device struct");
684 /* wait for xend to configure us */
685 err = xenbus_switch_state(dev, XenbusStateInitWait);
689 /* watch the backend node for backend configuration information */
690 err = xenbus_watch_path(dev, dev->nodename, &pdev->be_watch,
695 pdev->be_watching = 1;
697 /* We need to force a call to our callback here in case
698 * xend already configured us!
700 xen_pcibk_be_watch(&pdev->be_watch, NULL, NULL);
706 static int xen_pcibk_xenbus_remove(struct xenbus_device *dev)
708 struct xen_pcibk_device *pdev = dev_get_drvdata(&dev->dev);
716 static const struct xenbus_device_id xen_pcibk_ids[] = {
721 static struct xenbus_driver xen_pcibk_driver = {
723 .ids = xen_pcibk_ids,
724 .probe = xen_pcibk_xenbus_probe,
725 .remove = xen_pcibk_xenbus_remove,
726 .otherend_changed = xen_pcibk_frontend_changed,
729 const struct xen_pcibk_backend *__read_mostly xen_pcibk_backend;
731 int __init xen_pcibk_xenbus_register(void)
733 xen_pcibk_backend = &xen_pcibk_vpci_backend;
735 xen_pcibk_backend = &xen_pcibk_passthrough_backend;
736 pr_info("backend is %s\n", xen_pcibk_backend->name);
737 return xenbus_register_backend(&xen_pcibk_driver);
740 void __exit xen_pcibk_xenbus_unregister(void)
742 xenbus_unregister_driver(&xen_pcibk_driver);