Lines Matching +full:pci +full:- +full:domain

1 // SPDX-License-Identifier: GPL-2.0
3 * PCI Backend Xenbus Setup - handles setup with frontend and xend
17 #include <asm/xen/pci.h>
20 #define INVALID_EVTCHN_IRQ (-1)
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"\
33 " exposed PCI devices to its driver domains. This may be required\n"\
44 dev_dbg(&xdev->dev, "allocated pdev @ 0x%p\n", pdev); in alloc_pdev()
46 pdev->xdev = xdev; in alloc_pdev()
48 mutex_init(&pdev->dev_lock); in alloc_pdev()
50 pdev->sh_info = NULL; in alloc_pdev()
51 pdev->evtchn_irq = INVALID_EVTCHN_IRQ; in alloc_pdev()
52 pdev->be_watching = 0; in alloc_pdev()
54 INIT_WORK(&pdev->op_work, xen_pcibk_do_op); in alloc_pdev()
61 dev_set_drvdata(&xdev->dev, pdev); in alloc_pdev()
69 mutex_lock(&pdev->dev_lock); in xen_pcibk_disconnect()
71 if (pdev->evtchn_irq != INVALID_EVTCHN_IRQ) { in xen_pcibk_disconnect()
72 unbind_from_irqhandler(pdev->evtchn_irq, pdev); in xen_pcibk_disconnect()
73 pdev->evtchn_irq = INVALID_EVTCHN_IRQ; in xen_pcibk_disconnect()
76 /* If the driver domain started an op, make sure we complete it in xen_pcibk_disconnect()
79 flush_work(&pdev->op_work); in xen_pcibk_disconnect()
81 if (pdev->sh_info != NULL) { in xen_pcibk_disconnect()
82 xenbus_unmap_ring_vfree(pdev->xdev, pdev->sh_info); in xen_pcibk_disconnect()
83 pdev->sh_info = NULL; in xen_pcibk_disconnect()
85 mutex_unlock(&pdev->dev_lock); in xen_pcibk_disconnect()
90 if (pdev->be_watching) { in free_pdev()
91 unregister_xenbus_watch(&pdev->be_watch); in free_pdev()
92 pdev->be_watching = 0; in free_pdev()
101 dev_set_drvdata(&pdev->xdev->dev, NULL); in free_pdev()
102 pdev->xdev = NULL; in free_pdev()
113 dev_dbg(&pdev->xdev->dev, in xen_pcibk_do_attach()
114 "Attaching to frontend resources - gnt_ref=%d evtchn=%u\n", in xen_pcibk_do_attach()
117 err = xenbus_map_ring_valloc(pdev->xdev, &gnt_ref, 1, &vaddr); in xen_pcibk_do_attach()
119 xenbus_dev_fatal(pdev->xdev, err, in xen_pcibk_do_attach()
120 "Error mapping other domain page in ours."); in xen_pcibk_do_attach()
124 pdev->sh_info = vaddr; in xen_pcibk_do_attach()
127 pdev->xdev->otherend_id, remote_evtchn, xen_pcibk_handle_event, in xen_pcibk_do_attach()
130 xenbus_dev_fatal(pdev->xdev, err, in xen_pcibk_do_attach()
134 pdev->evtchn_irq = err; in xen_pcibk_do_attach()
137 dev_dbg(&pdev->xdev->dev, "Attached!\n"); in xen_pcibk_do_attach()
150 mutex_lock(&pdev->dev_lock); in xen_pcibk_attach()
152 if (xenbus_read_driver_state(pdev->xdev->nodename) != in xen_pcibk_attach()
157 if (xenbus_read_driver_state(pdev->xdev->otherend) != in xen_pcibk_attach()
161 dev_dbg(&pdev->xdev->dev, "Reading frontend config\n"); in xen_pcibk_attach()
163 err = xenbus_gather(XBT_NIL, pdev->xdev->otherend, in xen_pcibk_attach()
164 "pci-op-ref", "%u", &gnt_ref, in xen_pcibk_attach()
165 "event-channel", "%u", &remote_evtchn, in xen_pcibk_attach()
169 xenbus_dev_fatal(pdev->xdev, err, in xen_pcibk_attach()
175 xenbus_dev_fatal(pdev->xdev, -EFAULT, in xen_pcibk_attach()
176 "version mismatch (%s/%s) with pcifront - " in xen_pcibk_attach()
179 err = -EFAULT; in xen_pcibk_attach()
187 dev_dbg(&pdev->xdev->dev, "Connecting...\n"); in xen_pcibk_attach()
189 err = xenbus_switch_state(pdev->xdev, XenbusStateConnected); in xen_pcibk_attach()
191 xenbus_dev_fatal(pdev->xdev, err, in xen_pcibk_attach()
194 dev_dbg(&pdev->xdev->dev, "Connected? %d\n", err); in xen_pcibk_attach()
196 mutex_unlock(&pdev->dev_lock); in xen_pcibk_attach()
204 unsigned int domain, unsigned int bus, in xen_pcibk_publish_pci_dev() argument
211 len = snprintf(str, sizeof(str), "vdev-%d", devid); in xen_pcibk_publish_pci_dev()
212 if (unlikely(len >= (sizeof(str) - 1))) { in xen_pcibk_publish_pci_dev()
213 err = -ENOMEM; in xen_pcibk_publish_pci_dev()
218 err = xenbus_printf(XBT_NIL, pdev->xdev->nodename, str, in xen_pcibk_publish_pci_dev()
219 "%04x:%02x:%02x.%02x", domain, bus, in xen_pcibk_publish_pci_dev()
227 int domain, int bus, int slot, int func, in xen_pcibk_export_device() argument
233 dev_dbg(&pdev->xdev->dev, "exporting dom %x bus %x slot %x func %x\n", in xen_pcibk_export_device()
234 domain, bus, slot, func); in xen_pcibk_export_device()
236 dev = pcistub_get_pci_dev_by_slot(pdev, domain, bus, slot, func); in xen_pcibk_export_device()
238 err = -EINVAL; in xen_pcibk_export_device()
239 xenbus_dev_fatal(pdev->xdev, err, in xen_pcibk_export_device()
240 "Couldn't locate PCI device " in xen_pcibk_export_device()
242 "perhaps already in-use?", in xen_pcibk_export_device()
243 domain, bus, slot, func); in xen_pcibk_export_device()
252 dev_info(&dev->dev, "registering for %d\n", pdev->xdev->otherend_id); in xen_pcibk_export_device()
254 pdev->xdev->otherend_id) != 0) { in xen_pcibk_export_device()
255 dev_err(&dev->dev, "Stealing ownership from dom%d.\n", in xen_pcibk_export_device()
258 xen_register_device_domain_owner(dev, pdev->xdev->otherend_id); in xen_pcibk_export_device()
265 * driver domain, that all devices under that bridge are not given in xen_pcibk_export_device()
274 int domain, int bus, int slot, int func) in xen_pcibk_remove_device() argument
279 dev_dbg(&pdev->xdev->dev, "removing dom %x bus %x slot %x func %x\n", in xen_pcibk_remove_device()
280 domain, bus, slot, func); in xen_pcibk_remove_device()
282 dev = xen_pcibk_get_pci_dev(pdev, domain, bus, PCI_DEVFN(slot, func)); in xen_pcibk_remove_device()
284 err = -EINVAL; in xen_pcibk_remove_device()
285 dev_dbg(&pdev->xdev->dev, "Couldn't locate PCI device " in xen_pcibk_remove_device()
286 "(%04x:%02x:%02x.%d)! not owned by this domain\n", in xen_pcibk_remove_device()
287 domain, bus, slot, func); in xen_pcibk_remove_device()
291 dev_dbg(&dev->dev, "unregistering for %d\n", pdev->xdev->otherend_id); in xen_pcibk_remove_device()
303 unsigned int domain, unsigned int bus) in xen_pcibk_publish_pci_root() argument
309 dev_dbg(&pdev->xdev->dev, "Publishing pci roots\n"); in xen_pcibk_publish_pci_root()
311 err = xenbus_scanf(XBT_NIL, pdev->xdev->nodename, in xen_pcibk_publish_pci_root()
313 if (err == 0 || err == -ENOENT) in xen_pcibk_publish_pci_root()
318 /* Verify that we haven't already published this pci root */ in xen_pcibk_publish_pci_root()
320 len = snprintf(str, sizeof(str), "root-%d", i); in xen_pcibk_publish_pci_root()
321 if (unlikely(len >= (sizeof(str) - 1))) { in xen_pcibk_publish_pci_root()
322 err = -ENOMEM; in xen_pcibk_publish_pci_root()
326 err = xenbus_scanf(XBT_NIL, pdev->xdev->nodename, in xen_pcibk_publish_pci_root()
331 err = -EINVAL; in xen_pcibk_publish_pci_root()
335 if (d == domain && b == bus) { in xen_pcibk_publish_pci_root()
341 len = snprintf(str, sizeof(str), "root-%d", root_num); in xen_pcibk_publish_pci_root()
342 if (unlikely(len >= (sizeof(str) - 1))) { in xen_pcibk_publish_pci_root()
343 err = -ENOMEM; in xen_pcibk_publish_pci_root()
347 dev_dbg(&pdev->xdev->dev, "writing root %d at %04x:%02x\n", in xen_pcibk_publish_pci_root()
348 root_num, domain, bus); in xen_pcibk_publish_pci_root()
350 err = xenbus_printf(XBT_NIL, pdev->xdev->nodename, str, in xen_pcibk_publish_pci_root()
351 "%04x:%02x", domain, bus); in xen_pcibk_publish_pci_root()
355 err = xenbus_printf(XBT_NIL, pdev->xdev->nodename, in xen_pcibk_publish_pci_root()
366 int domain, bus, slot, func; in xen_pcibk_reconfigure() local
373 dev_dbg(&pdev->xdev->dev, "Reconfiguring device ...\n"); in xen_pcibk_reconfigure()
375 mutex_lock(&pdev->dev_lock); in xen_pcibk_reconfigure()
377 if (xenbus_read_driver_state(pdev->xdev->nodename) != in xen_pcibk_reconfigure()
381 err = xenbus_scanf(XBT_NIL, pdev->xdev->nodename, "num_devs", "%d", in xen_pcibk_reconfigure()
385 err = -EINVAL; in xen_pcibk_reconfigure()
386 xenbus_dev_fatal(pdev->xdev, err, in xen_pcibk_reconfigure()
392 len = snprintf(state_str, sizeof(state_str), "state-%d", i); in xen_pcibk_reconfigure()
393 if (unlikely(len >= (sizeof(state_str) - 1))) { in xen_pcibk_reconfigure()
394 err = -ENOMEM; in xen_pcibk_reconfigure()
395 xenbus_dev_fatal(pdev->xdev, err, in xen_pcibk_reconfigure()
400 substate = xenbus_read_unsigned(pdev->xdev->nodename, state_str, in xen_pcibk_reconfigure()
405 dev_dbg(&pdev->xdev->dev, "Attaching dev-%d ...\n", i); in xen_pcibk_reconfigure()
407 len = snprintf(dev_str, sizeof(dev_str), "dev-%d", i); in xen_pcibk_reconfigure()
408 if (unlikely(len >= (sizeof(dev_str) - 1))) { in xen_pcibk_reconfigure()
409 err = -ENOMEM; in xen_pcibk_reconfigure()
410 xenbus_dev_fatal(pdev->xdev, err, in xen_pcibk_reconfigure()
415 err = xenbus_scanf(XBT_NIL, pdev->xdev->nodename, in xen_pcibk_reconfigure()
417 &domain, &bus, &slot, &func); in xen_pcibk_reconfigure()
419 xenbus_dev_fatal(pdev->xdev, err, in xen_pcibk_reconfigure()
425 err = -EINVAL; in xen_pcibk_reconfigure()
426 xenbus_dev_fatal(pdev->xdev, err, in xen_pcibk_reconfigure()
427 "Error parsing pci device " in xen_pcibk_reconfigure()
432 err = xen_pcibk_export_device(pdev, domain, bus, slot, in xen_pcibk_reconfigure()
437 /* Publish pci roots. */ in xen_pcibk_reconfigure()
441 xenbus_dev_fatal(pdev->xdev, err, in xen_pcibk_reconfigure()
442 "Error while publish PCI root" in xen_pcibk_reconfigure()
447 err = xenbus_printf(XBT_NIL, pdev->xdev->nodename, in xen_pcibk_reconfigure()
451 xenbus_dev_fatal(pdev->xdev, err, in xen_pcibk_reconfigure()
453 "dev-%d\n", i); in xen_pcibk_reconfigure()
459 dev_dbg(&pdev->xdev->dev, "Detaching dev-%d ...\n", i); in xen_pcibk_reconfigure()
461 len = snprintf(dev_str, sizeof(dev_str), "vdev-%d", i); in xen_pcibk_reconfigure()
462 if (unlikely(len >= (sizeof(dev_str) - 1))) { in xen_pcibk_reconfigure()
463 err = -ENOMEM; in xen_pcibk_reconfigure()
464 xenbus_dev_fatal(pdev->xdev, err, in xen_pcibk_reconfigure()
469 err = xenbus_scanf(XBT_NIL, pdev->xdev->nodename, in xen_pcibk_reconfigure()
471 &domain, &bus, &slot, &func); in xen_pcibk_reconfigure()
473 xenbus_dev_fatal(pdev->xdev, err, in xen_pcibk_reconfigure()
479 err = -EINVAL; in xen_pcibk_reconfigure()
480 xenbus_dev_fatal(pdev->xdev, err, in xen_pcibk_reconfigure()
481 "Error parsing pci device " in xen_pcibk_reconfigure()
486 err = xen_pcibk_remove_device(pdev, domain, bus, slot, in xen_pcibk_reconfigure()
491 /* TODO: If at some point we implement support for pci in xen_pcibk_reconfigure()
492 * root hot-remove on pcifront side, we'll need to in xen_pcibk_reconfigure()
493 * remove unnecessary xenstore nodes of pci roots here. in xen_pcibk_reconfigure()
503 err = xenbus_switch_state(pdev->xdev, XenbusStateReconfigured); in xen_pcibk_reconfigure()
505 xenbus_dev_fatal(pdev->xdev, err, in xen_pcibk_reconfigure()
511 mutex_unlock(&pdev->dev_lock); in xen_pcibk_reconfigure()
518 struct xen_pcibk_device *pdev = dev_get_drvdata(&xdev->dev); in xen_pcibk_frontend_changed()
520 dev_dbg(&xdev->dev, "fe state changed %d\n", fe_state); in xen_pcibk_frontend_changed()
550 dev_dbg(&xdev->dev, "frontend is gone! unregister device\n"); in xen_pcibk_frontend_changed()
551 device_unregister(&xdev->dev); in xen_pcibk_frontend_changed()
562 int domain, bus, slot, func; in xen_pcibk_setup_backend() local
568 mutex_lock(&pdev->dev_lock); in xen_pcibk_setup_backend()
572 if (xenbus_read_driver_state(pdev->xdev->nodename) != in xen_pcibk_setup_backend()
576 dev_dbg(&pdev->xdev->dev, "getting be setup\n"); in xen_pcibk_setup_backend()
578 err = xenbus_scanf(XBT_NIL, pdev->xdev->nodename, "num_devs", "%d", in xen_pcibk_setup_backend()
582 err = -EINVAL; in xen_pcibk_setup_backend()
583 xenbus_dev_fatal(pdev->xdev, err, in xen_pcibk_setup_backend()
589 int l = snprintf(dev_str, sizeof(dev_str), "dev-%d", i); in xen_pcibk_setup_backend()
590 if (unlikely(l >= (sizeof(dev_str) - 1))) { in xen_pcibk_setup_backend()
591 err = -ENOMEM; in xen_pcibk_setup_backend()
592 xenbus_dev_fatal(pdev->xdev, err, in xen_pcibk_setup_backend()
598 err = xenbus_scanf(XBT_NIL, pdev->xdev->nodename, dev_str, in xen_pcibk_setup_backend()
599 "%x:%x:%x.%x", &domain, &bus, &slot, &func); in xen_pcibk_setup_backend()
601 xenbus_dev_fatal(pdev->xdev, err, in xen_pcibk_setup_backend()
606 err = -EINVAL; in xen_pcibk_setup_backend()
607 xenbus_dev_fatal(pdev->xdev, err, in xen_pcibk_setup_backend()
608 "Error parsing pci device " in xen_pcibk_setup_backend()
613 err = xen_pcibk_export_device(pdev, domain, bus, slot, func, i); in xen_pcibk_setup_backend()
618 l = snprintf(state_str, sizeof(state_str), "state-%d", i); in xen_pcibk_setup_backend()
619 if (unlikely(l >= (sizeof(state_str) - 1))) { in xen_pcibk_setup_backend()
620 err = -ENOMEM; in xen_pcibk_setup_backend()
621 xenbus_dev_fatal(pdev->xdev, err, in xen_pcibk_setup_backend()
626 err = xenbus_printf(XBT_NIL, pdev->xdev->nodename, state_str, in xen_pcibk_setup_backend()
629 xenbus_dev_fatal(pdev->xdev, err, "Error switching " in xen_pcibk_setup_backend()
630 "substate of dev-%d\n", i); in xen_pcibk_setup_backend()
637 xenbus_dev_fatal(pdev->xdev, err, in xen_pcibk_setup_backend()
638 "Error while publish PCI root buses " in xen_pcibk_setup_backend()
643 err = xenbus_switch_state(pdev->xdev, XenbusStateInitialised); in xen_pcibk_setup_backend()
645 xenbus_dev_fatal(pdev->xdev, err, in xen_pcibk_setup_backend()
649 mutex_unlock(&pdev->dev_lock); in xen_pcibk_setup_backend()
662 switch (xenbus_read_driver_state(pdev->xdev->nodename)) { in xen_pcibk_be_watch()
679 err = -ENOMEM; in xen_pcibk_xenbus_probe()
691 err = xenbus_watch_path(dev, dev->nodename, &pdev->be_watch, in xen_pcibk_xenbus_probe()
696 pdev->be_watching = 1; in xen_pcibk_xenbus_probe()
701 xen_pcibk_be_watch(&pdev->be_watch, NULL, NULL); in xen_pcibk_xenbus_probe()
709 struct xen_pcibk_device *pdev = dev_get_drvdata(&dev->dev); in xen_pcibk_xenbus_remove()
718 {"pci"},
737 pr_info("backend is %s\n", xen_pcibk_backend->name); in xen_pcibk_xenbus_register()