Lines Matching +full:bus +full:- +full:err
1 // SPDX-License-Identifier: GPL-2.0
3 * PCI Backend Xenbus Setup - handles setup with frontend and xend
20 #define INVALID_EVTCHN_IRQ (-1)
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"\
35 " bus/slot locations.");
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()
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()
110 int err = 0; in xen_pcibk_do_attach() local
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()
118 if (err < 0) { in xen_pcibk_do_attach()
119 xenbus_dev_fatal(pdev->xdev, err, in xen_pcibk_do_attach()
124 pdev->sh_info = vaddr; in xen_pcibk_do_attach()
126 err = bind_interdomain_evtchn_to_irqhandler_lateeoi( in xen_pcibk_do_attach()
127 pdev->xdev, remote_evtchn, xen_pcibk_handle_event, in xen_pcibk_do_attach()
129 if (err < 0) { 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()
135 err = 0; in xen_pcibk_do_attach()
137 dev_dbg(&pdev->xdev->dev, "Attached!\n"); in xen_pcibk_do_attach()
139 return err; in xen_pcibk_do_attach()
144 int err = 0; in xen_pcibk_attach() local
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()
167 if (err) { 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()
183 err = xen_pcibk_do_attach(pdev, gnt_ref, remote_evtchn); in xen_pcibk_attach()
184 if (err) 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()
190 if (err) 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()
200 return err; in xen_pcibk_attach()
204 unsigned int domain, unsigned int bus, in xen_pcibk_publish_pci_dev() argument
207 int err; in xen_pcibk_publish_pci_dev() local
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()
223 return err; in xen_pcibk_publish_pci_dev()
227 int domain, int bus, int slot, int func, in xen_pcibk_export_device() argument
231 int err = 0; in xen_pcibk_export_device() local
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()
242 "perhaps already in-use?", in xen_pcibk_export_device()
243 domain, bus, slot, func); in xen_pcibk_export_device()
247 err = xen_pcibk_add_pci_dev(pdev, dev, devid, in xen_pcibk_export_device()
249 if (err) 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()
270 return err; in xen_pcibk_export_device()
274 int domain, int bus, int slot, int func) in xen_pcibk_remove_device() argument
276 int err = 0; in xen_pcibk_remove_device() local
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()
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()
299 return err; in xen_pcibk_remove_device()
303 unsigned int domain, unsigned int bus) in xen_pcibk_publish_pci_root() argument
306 int i, root_num, len, err; in xen_pcibk_publish_pci_root() local
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()
315 else if (err < 0) 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()
328 if (err < 0) in xen_pcibk_publish_pci_root()
330 if (err != 2) { 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()
336 err = 0; 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()
352 if (err) in xen_pcibk_publish_pci_root()
355 err = xenbus_printf(XBT_NIL, pdev->xdev->nodename, in xen_pcibk_publish_pci_root()
359 return err; in xen_pcibk_publish_pci_root()
365 int err = 0; in xen_pcibk_reconfigure() local
367 int domain, bus, slot, func; in xen_pcibk_reconfigure() local
374 dev_dbg(&pdev->xdev->dev, "Reconfiguring device ...\n"); in xen_pcibk_reconfigure()
376 mutex_lock(&pdev->dev_lock); in xen_pcibk_reconfigure()
377 if (xenbus_read_driver_state(pdev->xdev->nodename) != state) in xen_pcibk_reconfigure()
380 err = xenbus_scanf(XBT_NIL, pdev->xdev->nodename, "num_devs", "%d", in xen_pcibk_reconfigure()
382 if (err != 1) { in xen_pcibk_reconfigure()
383 if (err >= 0) in xen_pcibk_reconfigure()
384 err = -EINVAL; in xen_pcibk_reconfigure()
385 xenbus_dev_fatal(pdev->xdev, err, in xen_pcibk_reconfigure()
391 len = snprintf(state_str, sizeof(state_str), "state-%d", i); in xen_pcibk_reconfigure()
392 if (unlikely(len >= (sizeof(state_str) - 1))) { in xen_pcibk_reconfigure()
393 err = -ENOMEM; in xen_pcibk_reconfigure()
394 xenbus_dev_fatal(pdev->xdev, err, in xen_pcibk_reconfigure()
399 substate = xenbus_read_unsigned(pdev->xdev->nodename, state_str, in xen_pcibk_reconfigure()
404 dev_dbg(&pdev->xdev->dev, "Attaching dev-%d ...\n", i); in xen_pcibk_reconfigure()
406 len = snprintf(dev_str, sizeof(dev_str), "dev-%d", i); in xen_pcibk_reconfigure()
407 if (unlikely(len >= (sizeof(dev_str) - 1))) { in xen_pcibk_reconfigure()
408 err = -ENOMEM; in xen_pcibk_reconfigure()
409 xenbus_dev_fatal(pdev->xdev, err, in xen_pcibk_reconfigure()
414 err = xenbus_scanf(XBT_NIL, pdev->xdev->nodename, in xen_pcibk_reconfigure()
416 &domain, &bus, &slot, &func); in xen_pcibk_reconfigure()
417 if (err < 0) { in xen_pcibk_reconfigure()
418 xenbus_dev_fatal(pdev->xdev, err, in xen_pcibk_reconfigure()
423 if (err != 4) { in xen_pcibk_reconfigure()
424 err = -EINVAL; in xen_pcibk_reconfigure()
425 xenbus_dev_fatal(pdev->xdev, err, in xen_pcibk_reconfigure()
431 err = xen_pcibk_export_device(pdev, domain, bus, slot, in xen_pcibk_reconfigure()
433 if (err) in xen_pcibk_reconfigure()
437 err = xen_pcibk_publish_pci_roots(pdev, in xen_pcibk_reconfigure()
439 if (err) { in xen_pcibk_reconfigure()
440 xenbus_dev_fatal(pdev->xdev, err, in xen_pcibk_reconfigure()
446 err = xenbus_printf(XBT_NIL, pdev->xdev->nodename, in xen_pcibk_reconfigure()
449 if (err) { in xen_pcibk_reconfigure()
450 xenbus_dev_fatal(pdev->xdev, err, in xen_pcibk_reconfigure()
452 "dev-%d\n", i); in xen_pcibk_reconfigure()
458 dev_dbg(&pdev->xdev->dev, "Detaching dev-%d ...\n", i); in xen_pcibk_reconfigure()
460 len = snprintf(dev_str, sizeof(dev_str), "vdev-%d", i); in xen_pcibk_reconfigure()
461 if (unlikely(len >= (sizeof(dev_str) - 1))) { in xen_pcibk_reconfigure()
462 err = -ENOMEM; in xen_pcibk_reconfigure()
463 xenbus_dev_fatal(pdev->xdev, err, in xen_pcibk_reconfigure()
468 err = xenbus_scanf(XBT_NIL, pdev->xdev->nodename, in xen_pcibk_reconfigure()
470 &domain, &bus, &slot, &func); in xen_pcibk_reconfigure()
471 if (err < 0) { in xen_pcibk_reconfigure()
472 xenbus_dev_fatal(pdev->xdev, err, in xen_pcibk_reconfigure()
477 if (err != 4) { in xen_pcibk_reconfigure()
478 err = -EINVAL; in xen_pcibk_reconfigure()
479 xenbus_dev_fatal(pdev->xdev, err, in xen_pcibk_reconfigure()
485 err = xen_pcibk_remove_device(pdev, domain, bus, slot, in xen_pcibk_reconfigure()
487 if (err) in xen_pcibk_reconfigure()
491 * root hot-remove on pcifront side, we'll need to in xen_pcibk_reconfigure()
506 err = xenbus_switch_state(pdev->xdev, XenbusStateReconfigured); in xen_pcibk_reconfigure()
507 if (err) { in xen_pcibk_reconfigure()
508 xenbus_dev_fatal(pdev->xdev, err, in xen_pcibk_reconfigure()
514 mutex_unlock(&pdev->dev_lock); in xen_pcibk_reconfigure()
521 struct xen_pcibk_device *pdev = dev_get_drvdata(&xdev->dev); in xen_pcibk_frontend_changed()
523 dev_dbg(&xdev->dev, "fe state changed %d\n", fe_state); in xen_pcibk_frontend_changed()
553 dev_dbg(&xdev->dev, "frontend is gone! unregister device\n"); in xen_pcibk_frontend_changed()
554 device_unregister(&xdev->dev); in xen_pcibk_frontend_changed()
565 int domain, bus, slot, func; in xen_pcibk_setup_backend() local
566 int err = 0; in xen_pcibk_setup_backend() local
571 mutex_lock(&pdev->dev_lock); in xen_pcibk_setup_backend()
575 if (xenbus_read_driver_state(pdev->xdev->nodename) != in xen_pcibk_setup_backend()
579 dev_dbg(&pdev->xdev->dev, "getting be setup\n"); in xen_pcibk_setup_backend()
581 err = xenbus_scanf(XBT_NIL, pdev->xdev->nodename, "num_devs", "%d", in xen_pcibk_setup_backend()
583 if (err != 1) { in xen_pcibk_setup_backend()
584 if (err >= 0) in xen_pcibk_setup_backend()
585 err = -EINVAL; in xen_pcibk_setup_backend()
586 xenbus_dev_fatal(pdev->xdev, err, in xen_pcibk_setup_backend()
592 int l = snprintf(dev_str, sizeof(dev_str), "dev-%d", i); in xen_pcibk_setup_backend()
593 if (unlikely(l >= (sizeof(dev_str) - 1))) { in xen_pcibk_setup_backend()
594 err = -ENOMEM; in xen_pcibk_setup_backend()
595 xenbus_dev_fatal(pdev->xdev, err, in xen_pcibk_setup_backend()
601 err = xenbus_scanf(XBT_NIL, pdev->xdev->nodename, dev_str, in xen_pcibk_setup_backend()
602 "%x:%x:%x.%x", &domain, &bus, &slot, &func); in xen_pcibk_setup_backend()
603 if (err < 0) { in xen_pcibk_setup_backend()
604 xenbus_dev_fatal(pdev->xdev, err, in xen_pcibk_setup_backend()
608 if (err != 4) { in xen_pcibk_setup_backend()
609 err = -EINVAL; in xen_pcibk_setup_backend()
610 xenbus_dev_fatal(pdev->xdev, err, in xen_pcibk_setup_backend()
616 err = xen_pcibk_export_device(pdev, domain, bus, slot, func, i); in xen_pcibk_setup_backend()
617 if (err) in xen_pcibk_setup_backend()
621 l = snprintf(state_str, sizeof(state_str), "state-%d", i); in xen_pcibk_setup_backend()
622 if (unlikely(l >= (sizeof(state_str) - 1))) { in xen_pcibk_setup_backend()
623 err = -ENOMEM; in xen_pcibk_setup_backend()
624 xenbus_dev_fatal(pdev->xdev, err, in xen_pcibk_setup_backend()
629 err = xenbus_printf(XBT_NIL, pdev->xdev->nodename, state_str, in xen_pcibk_setup_backend()
631 if (err) { in xen_pcibk_setup_backend()
632 xenbus_dev_fatal(pdev->xdev, err, "Error switching " in xen_pcibk_setup_backend()
633 "substate of dev-%d\n", i); in xen_pcibk_setup_backend()
638 err = xen_pcibk_publish_pci_roots(pdev, xen_pcibk_publish_pci_root); in xen_pcibk_setup_backend()
639 if (err) { in xen_pcibk_setup_backend()
640 xenbus_dev_fatal(pdev->xdev, err, in xen_pcibk_setup_backend()
646 err = xenbus_switch_state(pdev->xdev, XenbusStateInitialised); in xen_pcibk_setup_backend()
647 if (err) in xen_pcibk_setup_backend()
648 xenbus_dev_fatal(pdev->xdev, err, in xen_pcibk_setup_backend()
652 mutex_unlock(&pdev->dev_lock); in xen_pcibk_setup_backend()
653 if (!err) in xen_pcibk_setup_backend()
656 return err; in xen_pcibk_setup_backend()
665 switch (xenbus_read_driver_state(pdev->xdev->nodename)) { in xen_pcibk_be_watch()
687 int err = 0; in xen_pcibk_xenbus_probe() local
691 err = -ENOMEM; in xen_pcibk_xenbus_probe()
692 xenbus_dev_fatal(dev, err, in xen_pcibk_xenbus_probe()
698 err = xenbus_switch_state(dev, XenbusStateInitWait); in xen_pcibk_xenbus_probe()
699 if (err) in xen_pcibk_xenbus_probe()
703 err = xenbus_watch_path(dev, dev->nodename, &pdev->be_watch, in xen_pcibk_xenbus_probe()
705 if (err) in xen_pcibk_xenbus_probe()
708 pdev->be_watching = 1; in xen_pcibk_xenbus_probe()
713 xen_pcibk_be_watch(&pdev->be_watch, NULL, NULL); in xen_pcibk_xenbus_probe()
716 return err; in xen_pcibk_xenbus_probe()
721 struct xen_pcibk_device *pdev = dev_get_drvdata(&dev->dev); in xen_pcibk_xenbus_remove()
750 pr_info("backend is %s\n", xen_pcibk_backend->name); in xen_pcibk_xenbus_register()