gpio: rcar: Fix runtime PM imbalance on error
[linux/fpc-iii.git] / drivers / xen / xen-pciback / xenbus.c
blobf2115587855f7d629910f2ec57726227b9aa0844
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * PCI Backend Xenbus Setup - handles setup with frontend and xend
5 * Author: Ryan Wilson <hap9@epoch.ncsc.mil>
6 */
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>
18 #include "pciback.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);
42 if (pdev == NULL)
43 goto out;
44 dev_dbg(&xdev->dev, "allocated pdev @ 0x%p\n", pdev);
46 pdev->xdev = xdev;
48 mutex_init(&pdev->dev_lock);
50 pdev->sh_info = NULL;
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)) {
57 kfree(pdev);
58 pdev = NULL;
61 dev_set_drvdata(&xdev->dev, pdev);
63 out:
64 return 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);
83 pdev->sh_info = NULL;
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);
102 pdev->xdev = NULL;
104 kfree(pdev);
107 static int xen_pcibk_do_attach(struct xen_pcibk_device *pdev, int gnt_ref,
108 evtchn_port_t remote_evtchn)
110 int err = 0;
111 void *vaddr;
113 dev_dbg(&pdev->xdev->dev,
114 "Attaching to frontend resources - gnt_ref=%d evtchn=%u\n",
115 gnt_ref, remote_evtchn);
117 err = xenbus_map_ring_valloc(pdev->xdev, &gnt_ref, 1, &vaddr);
118 if (err < 0) {
119 xenbus_dev_fatal(pdev->xdev, err,
120 "Error mapping other domain page in ours.");
121 goto out;
124 pdev->sh_info = vaddr;
126 err = bind_interdomain_evtchn_to_irqhandler(
127 pdev->xdev->otherend_id, remote_evtchn, xen_pcibk_handle_event,
128 0, DRV_NAME, pdev);
129 if (err < 0) {
130 xenbus_dev_fatal(pdev->xdev, err,
131 "Error binding event channel to IRQ");
132 goto out;
134 pdev->evtchn_irq = err;
135 err = 0;
137 dev_dbg(&pdev->xdev->dev, "Attached!\n");
138 out:
139 return err;
142 static int xen_pcibk_attach(struct xen_pcibk_device *pdev)
144 int err = 0;
145 int gnt_ref;
146 evtchn_port_t remote_evtchn;
147 char *magic = NULL;
150 mutex_lock(&pdev->dev_lock);
151 /* Make sure we only do this setup once */
152 if (xenbus_read_driver_state(pdev->xdev->nodename) !=
153 XenbusStateInitialised)
154 goto out;
156 /* Wait for frontend to state that it has published the configuration */
157 if (xenbus_read_driver_state(pdev->xdev->otherend) !=
158 XenbusStateInitialised)
159 goto out;
161 dev_dbg(&pdev->xdev->dev, "Reading frontend config\n");
163 err = xenbus_gather(XBT_NIL, pdev->xdev->otherend,
164 "pci-op-ref", "%u", &gnt_ref,
165 "event-channel", "%u", &remote_evtchn,
166 "magic", NULL, &magic, NULL);
167 if (err) {
168 /* If configuration didn't get read correctly, wait longer */
169 xenbus_dev_fatal(pdev->xdev, err,
170 "Error reading configuration from frontend");
171 goto out;
174 if (magic == NULL || strcmp(magic, XEN_PCI_MAGIC) != 0) {
175 xenbus_dev_fatal(pdev->xdev, -EFAULT,
176 "version mismatch (%s/%s) with pcifront - "
177 "halting " DRV_NAME,
178 magic, XEN_PCI_MAGIC);
179 err = -EFAULT;
180 goto out;
183 err = xen_pcibk_do_attach(pdev, gnt_ref, remote_evtchn);
184 if (err)
185 goto out;
187 dev_dbg(&pdev->xdev->dev, "Connecting...\n");
189 err = xenbus_switch_state(pdev->xdev, XenbusStateConnected);
190 if (err)
191 xenbus_dev_fatal(pdev->xdev, err,
192 "Error switching to connected state!");
194 dev_dbg(&pdev->xdev->dev, "Connected? %d\n", err);
195 out:
196 mutex_unlock(&pdev->dev_lock);
198 kfree(magic);
200 return err;
203 static int xen_pcibk_publish_pci_dev(struct xen_pcibk_device *pdev,
204 unsigned int domain, unsigned int bus,
205 unsigned int devfn, unsigned int devid)
207 int err;
208 int len;
209 char str[64];
211 len = snprintf(str, sizeof(str), "vdev-%d", devid);
212 if (unlikely(len >= (sizeof(str) - 1))) {
213 err = -ENOMEM;
214 goto out;
217 /* Note: The PV protocol uses %02x, don't change it */
218 err = xenbus_printf(XBT_NIL, pdev->xdev->nodename, str,
219 "%04x:%02x:%02x.%02x", domain, bus,
220 PCI_SLOT(devfn), PCI_FUNC(devfn));
222 out:
223 return err;
226 static int xen_pcibk_export_device(struct xen_pcibk_device *pdev,
227 int domain, int bus, int slot, int func,
228 int devid)
230 struct pci_dev *dev;
231 int err = 0;
233 dev_dbg(&pdev->xdev->dev, "exporting dom %x bus %x slot %x func %x\n",
234 domain, bus, slot, func);
236 dev = pcistub_get_pci_dev_by_slot(pdev, domain, bus, slot, func);
237 if (!dev) {
238 err = -EINVAL;
239 xenbus_dev_fatal(pdev->xdev, err,
240 "Couldn't locate PCI device "
241 "(%04x:%02x:%02x.%d)! "
242 "perhaps already in-use?",
243 domain, bus, slot, func);
244 goto out;
247 err = xen_pcibk_add_pci_dev(pdev, dev, devid,
248 xen_pcibk_publish_pci_dev);
249 if (err)
250 goto out;
252 dev_info(&dev->dev, "registering for %d\n", pdev->xdev->otherend_id);
253 if (xen_register_device_domain_owner(dev,
254 pdev->xdev->otherend_id) != 0) {
255 dev_err(&dev->dev, "Stealing ownership from dom%d.\n",
256 xen_find_device_domain_owner(dev));
257 xen_unregister_device_domain_owner(dev);
258 xen_register_device_domain_owner(dev, pdev->xdev->otherend_id);
261 /* TODO: It'd be nice to export a bridge and have all of its children
262 * get exported with it. This may be best done in xend (which will
263 * have to calculate resource usage anyway) but we probably want to
264 * put something in here to ensure that if a bridge gets given to a
265 * driver domain, that all devices under that bridge are not given
266 * to other driver domains (as he who controls the bridge can disable
267 * it and stop the other devices from working).
269 out:
270 return err;
273 static int xen_pcibk_remove_device(struct xen_pcibk_device *pdev,
274 int domain, int bus, int slot, int func)
276 int err = 0;
277 struct pci_dev *dev;
279 dev_dbg(&pdev->xdev->dev, "removing dom %x bus %x slot %x func %x\n",
280 domain, bus, slot, func);
282 dev = xen_pcibk_get_pci_dev(pdev, domain, bus, PCI_DEVFN(slot, func));
283 if (!dev) {
284 err = -EINVAL;
285 dev_dbg(&pdev->xdev->dev, "Couldn't locate PCI device "
286 "(%04x:%02x:%02x.%d)! not owned by this domain\n",
287 domain, bus, slot, func);
288 goto out;
291 dev_dbg(&dev->dev, "unregistering for %d\n", pdev->xdev->otherend_id);
292 xen_unregister_device_domain_owner(dev);
294 /* N.B. This ends up calling pcistub_put_pci_dev which ends up
295 * doing the FLR. */
296 xen_pcibk_release_pci_dev(pdev, dev, true /* use the lock. */);
298 out:
299 return err;
302 static int xen_pcibk_publish_pci_root(struct xen_pcibk_device *pdev,
303 unsigned int domain, unsigned int bus)
305 unsigned int d, b;
306 int i, root_num, len, err;
307 char str[64];
309 dev_dbg(&pdev->xdev->dev, "Publishing pci roots\n");
311 err = xenbus_scanf(XBT_NIL, pdev->xdev->nodename,
312 "root_num", "%d", &root_num);
313 if (err == 0 || err == -ENOENT)
314 root_num = 0;
315 else if (err < 0)
316 goto out;
318 /* Verify that we haven't already published this pci root */
319 for (i = 0; i < root_num; i++) {
320 len = snprintf(str, sizeof(str), "root-%d", i);
321 if (unlikely(len >= (sizeof(str) - 1))) {
322 err = -ENOMEM;
323 goto out;
326 err = xenbus_scanf(XBT_NIL, pdev->xdev->nodename,
327 str, "%x:%x", &d, &b);
328 if (err < 0)
329 goto out;
330 if (err != 2) {
331 err = -EINVAL;
332 goto out;
335 if (d == domain && b == bus) {
336 err = 0;
337 goto out;
341 len = snprintf(str, sizeof(str), "root-%d", root_num);
342 if (unlikely(len >= (sizeof(str) - 1))) {
343 err = -ENOMEM;
344 goto out;
347 dev_dbg(&pdev->xdev->dev, "writing root %d at %04x:%02x\n",
348 root_num, domain, bus);
350 err = xenbus_printf(XBT_NIL, pdev->xdev->nodename, str,
351 "%04x:%02x", domain, bus);
352 if (err)
353 goto out;
355 err = xenbus_printf(XBT_NIL, pdev->xdev->nodename,
356 "root_num", "%d", (root_num + 1));
358 out:
359 return err;
362 static int xen_pcibk_reconfigure(struct xen_pcibk_device *pdev)
364 int err = 0;
365 int num_devs;
366 int domain, bus, slot, func;
367 unsigned int substate;
368 int i, len;
369 char state_str[64];
370 char dev_str[64];
373 dev_dbg(&pdev->xdev->dev, "Reconfiguring device ...\n");
375 mutex_lock(&pdev->dev_lock);
376 /* Make sure we only reconfigure once */
377 if (xenbus_read_driver_state(pdev->xdev->nodename) !=
378 XenbusStateReconfiguring)
379 goto out;
381 err = xenbus_scanf(XBT_NIL, pdev->xdev->nodename, "num_devs", "%d",
382 &num_devs);
383 if (err != 1) {
384 if (err >= 0)
385 err = -EINVAL;
386 xenbus_dev_fatal(pdev->xdev, err,
387 "Error reading number of devices");
388 goto out;
391 for (i = 0; i < num_devs; i++) {
392 len = snprintf(state_str, sizeof(state_str), "state-%d", i);
393 if (unlikely(len >= (sizeof(state_str) - 1))) {
394 err = -ENOMEM;
395 xenbus_dev_fatal(pdev->xdev, err,
396 "String overflow while reading "
397 "configuration");
398 goto out;
400 substate = xenbus_read_unsigned(pdev->xdev->nodename, state_str,
401 XenbusStateUnknown);
403 switch (substate) {
404 case XenbusStateInitialising:
405 dev_dbg(&pdev->xdev->dev, "Attaching dev-%d ...\n", i);
407 len = snprintf(dev_str, sizeof(dev_str), "dev-%d", i);
408 if (unlikely(len >= (sizeof(dev_str) - 1))) {
409 err = -ENOMEM;
410 xenbus_dev_fatal(pdev->xdev, err,
411 "String overflow while "
412 "reading configuration");
413 goto out;
415 err = xenbus_scanf(XBT_NIL, pdev->xdev->nodename,
416 dev_str, "%x:%x:%x.%x",
417 &domain, &bus, &slot, &func);
418 if (err < 0) {
419 xenbus_dev_fatal(pdev->xdev, err,
420 "Error reading device "
421 "configuration");
422 goto out;
424 if (err != 4) {
425 err = -EINVAL;
426 xenbus_dev_fatal(pdev->xdev, err,
427 "Error parsing pci device "
428 "configuration");
429 goto out;
432 err = xen_pcibk_export_device(pdev, domain, bus, slot,
433 func, i);
434 if (err)
435 goto out;
437 /* Publish pci roots. */
438 err = xen_pcibk_publish_pci_roots(pdev,
439 xen_pcibk_publish_pci_root);
440 if (err) {
441 xenbus_dev_fatal(pdev->xdev, err,
442 "Error while publish PCI root"
443 "buses for frontend");
444 goto out;
447 err = xenbus_printf(XBT_NIL, pdev->xdev->nodename,
448 state_str, "%d",
449 XenbusStateInitialised);
450 if (err) {
451 xenbus_dev_fatal(pdev->xdev, err,
452 "Error switching substate of "
453 "dev-%d\n", i);
454 goto out;
456 break;
458 case XenbusStateClosing:
459 dev_dbg(&pdev->xdev->dev, "Detaching dev-%d ...\n", i);
461 len = snprintf(dev_str, sizeof(dev_str), "vdev-%d", i);
462 if (unlikely(len >= (sizeof(dev_str) - 1))) {
463 err = -ENOMEM;
464 xenbus_dev_fatal(pdev->xdev, err,
465 "String overflow while "
466 "reading configuration");
467 goto out;
469 err = xenbus_scanf(XBT_NIL, pdev->xdev->nodename,
470 dev_str, "%x:%x:%x.%x",
471 &domain, &bus, &slot, &func);
472 if (err < 0) {
473 xenbus_dev_fatal(pdev->xdev, err,
474 "Error reading device "
475 "configuration");
476 goto out;
478 if (err != 4) {
479 err = -EINVAL;
480 xenbus_dev_fatal(pdev->xdev, err,
481 "Error parsing pci device "
482 "configuration");
483 goto out;
486 err = xen_pcibk_remove_device(pdev, domain, bus, slot,
487 func);
488 if (err)
489 goto out;
491 /* TODO: If at some point we implement support for pci
492 * root hot-remove on pcifront side, we'll need to
493 * remove unnecessary xenstore nodes of pci roots here.
496 break;
498 default:
499 break;
503 err = xenbus_switch_state(pdev->xdev, XenbusStateReconfigured);
504 if (err) {
505 xenbus_dev_fatal(pdev->xdev, err,
506 "Error switching to reconfigured state!");
507 goto out;
510 out:
511 mutex_unlock(&pdev->dev_lock);
512 return 0;
515 static void xen_pcibk_frontend_changed(struct xenbus_device *xdev,
516 enum xenbus_state fe_state)
518 struct xen_pcibk_device *pdev = dev_get_drvdata(&xdev->dev);
520 dev_dbg(&xdev->dev, "fe state changed %d\n", fe_state);
522 switch (fe_state) {
523 case XenbusStateInitialised:
524 xen_pcibk_attach(pdev);
525 break;
527 case XenbusStateReconfiguring:
528 xen_pcibk_reconfigure(pdev);
529 break;
531 case XenbusStateConnected:
532 /* pcifront switched its state from reconfiguring to connected.
533 * Then switch to connected state.
535 xenbus_switch_state(xdev, XenbusStateConnected);
536 break;
538 case XenbusStateClosing:
539 xen_pcibk_disconnect(pdev);
540 xenbus_switch_state(xdev, XenbusStateClosing);
541 break;
543 case XenbusStateClosed:
544 xen_pcibk_disconnect(pdev);
545 xenbus_switch_state(xdev, XenbusStateClosed);
546 if (xenbus_dev_is_online(xdev))
547 break;
548 /* fall through - if not online */
549 case XenbusStateUnknown:
550 dev_dbg(&xdev->dev, "frontend is gone! unregister device\n");
551 device_unregister(&xdev->dev);
552 break;
554 default:
555 break;
559 static int xen_pcibk_setup_backend(struct xen_pcibk_device *pdev)
561 /* Get configuration from xend (if available now) */
562 int domain, bus, slot, func;
563 int err = 0;
564 int i, num_devs;
565 char dev_str[64];
566 char state_str[64];
568 mutex_lock(&pdev->dev_lock);
569 /* It's possible we could get the call to setup twice, so make sure
570 * we're not already connected.
572 if (xenbus_read_driver_state(pdev->xdev->nodename) !=
573 XenbusStateInitWait)
574 goto out;
576 dev_dbg(&pdev->xdev->dev, "getting be setup\n");
578 err = xenbus_scanf(XBT_NIL, pdev->xdev->nodename, "num_devs", "%d",
579 &num_devs);
580 if (err != 1) {
581 if (err >= 0)
582 err = -EINVAL;
583 xenbus_dev_fatal(pdev->xdev, err,
584 "Error reading number of devices");
585 goto out;
588 for (i = 0; i < num_devs; i++) {
589 int l = snprintf(dev_str, sizeof(dev_str), "dev-%d", i);
590 if (unlikely(l >= (sizeof(dev_str) - 1))) {
591 err = -ENOMEM;
592 xenbus_dev_fatal(pdev->xdev, err,
593 "String overflow while reading "
594 "configuration");
595 goto out;
598 err = xenbus_scanf(XBT_NIL, pdev->xdev->nodename, dev_str,
599 "%x:%x:%x.%x", &domain, &bus, &slot, &func);
600 if (err < 0) {
601 xenbus_dev_fatal(pdev->xdev, err,
602 "Error reading device configuration");
603 goto out;
605 if (err != 4) {
606 err = -EINVAL;
607 xenbus_dev_fatal(pdev->xdev, err,
608 "Error parsing pci device "
609 "configuration");
610 goto out;
613 err = xen_pcibk_export_device(pdev, domain, bus, slot, func, i);
614 if (err)
615 goto out;
617 /* Switch substate of this device. */
618 l = snprintf(state_str, sizeof(state_str), "state-%d", i);
619 if (unlikely(l >= (sizeof(state_str) - 1))) {
620 err = -ENOMEM;
621 xenbus_dev_fatal(pdev->xdev, err,
622 "String overflow while reading "
623 "configuration");
624 goto out;
626 err = xenbus_printf(XBT_NIL, pdev->xdev->nodename, state_str,
627 "%d", XenbusStateInitialised);
628 if (err) {
629 xenbus_dev_fatal(pdev->xdev, err, "Error switching "
630 "substate of dev-%d\n", i);
631 goto out;
635 err = xen_pcibk_publish_pci_roots(pdev, xen_pcibk_publish_pci_root);
636 if (err) {
637 xenbus_dev_fatal(pdev->xdev, err,
638 "Error while publish PCI root buses "
639 "for frontend");
640 goto out;
643 err = xenbus_switch_state(pdev->xdev, XenbusStateInitialised);
644 if (err)
645 xenbus_dev_fatal(pdev->xdev, err,
646 "Error switching to initialised state!");
648 out:
649 mutex_unlock(&pdev->dev_lock);
650 if (!err)
651 /* see if pcifront is already configured (if not, we'll wait) */
652 xen_pcibk_attach(pdev);
653 return err;
656 static void xen_pcibk_be_watch(struct xenbus_watch *watch,
657 const char *path, const char *token)
659 struct xen_pcibk_device *pdev =
660 container_of(watch, struct xen_pcibk_device, be_watch);
662 switch (xenbus_read_driver_state(pdev->xdev->nodename)) {
663 case XenbusStateInitWait:
664 xen_pcibk_setup_backend(pdev);
665 break;
667 default:
668 break;
672 static int xen_pcibk_xenbus_probe(struct xenbus_device *dev,
673 const struct xenbus_device_id *id)
675 int err = 0;
676 struct xen_pcibk_device *pdev = alloc_pdev(dev);
678 if (pdev == NULL) {
679 err = -ENOMEM;
680 xenbus_dev_fatal(dev, err,
681 "Error allocating xen_pcibk_device struct");
682 goto out;
685 /* wait for xend to configure us */
686 err = xenbus_switch_state(dev, XenbusStateInitWait);
687 if (err)
688 goto out;
690 /* watch the backend node for backend configuration information */
691 err = xenbus_watch_path(dev, dev->nodename, &pdev->be_watch,
692 xen_pcibk_be_watch);
693 if (err)
694 goto out;
696 pdev->be_watching = 1;
698 /* We need to force a call to our callback here in case
699 * xend already configured us!
701 xen_pcibk_be_watch(&pdev->be_watch, NULL, NULL);
703 out:
704 return err;
707 static int xen_pcibk_xenbus_remove(struct xenbus_device *dev)
709 struct xen_pcibk_device *pdev = dev_get_drvdata(&dev->dev);
711 if (pdev != NULL)
712 free_pdev(pdev);
714 return 0;
717 static const struct xenbus_device_id xen_pcibk_ids[] = {
718 {"pci"},
719 {""},
722 static struct xenbus_driver xen_pcibk_driver = {
723 .name = DRV_NAME,
724 .ids = xen_pcibk_ids,
725 .probe = xen_pcibk_xenbus_probe,
726 .remove = xen_pcibk_xenbus_remove,
727 .otherend_changed = xen_pcibk_frontend_changed,
730 const struct xen_pcibk_backend *__read_mostly xen_pcibk_backend;
732 int __init xen_pcibk_xenbus_register(void)
734 xen_pcibk_backend = &xen_pcibk_vpci_backend;
735 if (passthrough)
736 xen_pcibk_backend = &xen_pcibk_passthrough_backend;
737 pr_info("backend is %s\n", xen_pcibk_backend->name);
738 return xenbus_register_backend(&xen_pcibk_driver);
741 void __exit xen_pcibk_xenbus_unregister(void)
743 xenbus_unregister_driver(&xen_pcibk_driver);