Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

3 Commits   0 Branches   0 Tags
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   1) // SPDX-License-Identifier: GPL-2.0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   3)  * PCI Backend Xenbus Setup - handles setup with frontend and xend
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   4)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   5)  *   Author: Ryan Wilson <hap9@epoch.ncsc.mil>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   6)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   7) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   8) #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   9) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  10) #include <linux/moduleparam.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  11) #include <linux/init.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  12) #include <linux/list.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  13) #include <linux/vmalloc.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  14) #include <linux/workqueue.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  15) #include <xen/xenbus.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  16) #include <xen/events.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  17) #include <asm/xen/pci.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  18) #include "pciback.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  19) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  20) #define INVALID_EVTCHN_IRQ  (-1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  21) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  22) static bool __read_mostly passthrough;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  23) module_param(passthrough, bool, S_IRUGO);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  24) MODULE_PARM_DESC(passthrough,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  25) 	"Option to specify how to export PCI topology to guest:\n"\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  26) 	" 0 - (default) Hide the true PCI topology and makes the frontend\n"\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  27) 	"   there is a single PCI bus with only the exported devices on it.\n"\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  28) 	"   For example, a device at 03:05.0 will be re-assigned to 00:00.0\n"\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  29) 	"   while second device at 02:1a.1 will be re-assigned to 00:01.1.\n"\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  30) 	" 1 - Passthrough provides a real view of the PCI topology to the\n"\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  31) 	"   frontend (for example, a device at 06:01.b will still appear at\n"\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  32) 	"   06:01.b to the frontend). This is similar to how Xen 2.0.x\n"\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  33) 	"   exposed PCI devices to its driver domains. This may be required\n"\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  34) 	"   for drivers which depend on finding their hardward in certain\n"\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  35) 	"   bus/slot locations.");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  36) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  37) static struct xen_pcibk_device *alloc_pdev(struct xenbus_device *xdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  38) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  39) 	struct xen_pcibk_device *pdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  40) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  41) 	pdev = kzalloc(sizeof(struct xen_pcibk_device), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  42) 	if (pdev == NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  43) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  44) 	dev_dbg(&xdev->dev, "allocated pdev @ 0x%p\n", pdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  45) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  46) 	pdev->xdev = xdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  47) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  48) 	mutex_init(&pdev->dev_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  49) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  50) 	pdev->sh_info = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  51) 	pdev->evtchn_irq = INVALID_EVTCHN_IRQ;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  52) 	pdev->be_watching = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  53) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  54) 	INIT_WORK(&pdev->op_work, xen_pcibk_do_op);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  55) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  56) 	if (xen_pcibk_init_devices(pdev)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  57) 		kfree(pdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  58) 		pdev = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  59) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  60) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  61) 	dev_set_drvdata(&xdev->dev, pdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  62) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  63) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  64) 	return pdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  65) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  66) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  67) static void xen_pcibk_disconnect(struct xen_pcibk_device *pdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  68) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  69) 	mutex_lock(&pdev->dev_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  70) 	/* Ensure the guest can't trigger our handler before removing devices */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  71) 	if (pdev->evtchn_irq != INVALID_EVTCHN_IRQ) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  72) 		unbind_from_irqhandler(pdev->evtchn_irq, pdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  73) 		pdev->evtchn_irq = INVALID_EVTCHN_IRQ;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  74) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  75) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  76) 	/* If the driver domain started an op, make sure we complete it
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  77) 	 * before releasing the shared memory */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  78) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  79) 	flush_work(&pdev->op_work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  80) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  81) 	if (pdev->sh_info != NULL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  82) 		xenbus_unmap_ring_vfree(pdev->xdev, pdev->sh_info);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  83) 		pdev->sh_info = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  84) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  85) 	mutex_unlock(&pdev->dev_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  86) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  87) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  88) static void free_pdev(struct xen_pcibk_device *pdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  89) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  90) 	if (pdev->be_watching) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  91) 		unregister_xenbus_watch(&pdev->be_watch);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  92) 		pdev->be_watching = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  93) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  94) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  95) 	xen_pcibk_disconnect(pdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  96) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  97) 	/* N.B. This calls pcistub_put_pci_dev which does the FLR on all
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  98) 	 * of the PCIe devices. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  99) 	xen_pcibk_release_devices(pdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) 	dev_set_drvdata(&pdev->xdev->dev, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) 	pdev->xdev = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) 	kfree(pdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) static int xen_pcibk_do_attach(struct xen_pcibk_device *pdev, int gnt_ref,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) 			     evtchn_port_t remote_evtchn)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) 	int err = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) 	void *vaddr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) 	dev_dbg(&pdev->xdev->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) 		"Attaching to frontend resources - gnt_ref=%d evtchn=%u\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) 		gnt_ref, remote_evtchn);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) 	err = xenbus_map_ring_valloc(pdev->xdev, &gnt_ref, 1, &vaddr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) 	if (err < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) 		xenbus_dev_fatal(pdev->xdev, err,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) 				"Error mapping other domain page in ours.");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) 	pdev->sh_info = vaddr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) 	err = bind_interdomain_evtchn_to_irqhandler_lateeoi(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) 		pdev->xdev->otherend_id, remote_evtchn, xen_pcibk_handle_event,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) 		0, DRV_NAME, pdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) 	if (err < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) 		xenbus_dev_fatal(pdev->xdev, err,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) 				 "Error binding event channel to IRQ");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) 	pdev->evtchn_irq = err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) 	err = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) 	dev_dbg(&pdev->xdev->dev, "Attached!\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) 	return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) static int xen_pcibk_attach(struct xen_pcibk_device *pdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) 	int err = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) 	int gnt_ref;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) 	evtchn_port_t remote_evtchn;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) 	char *magic = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) 	mutex_lock(&pdev->dev_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) 	/* Make sure we only do this setup once */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) 	if (xenbus_read_driver_state(pdev->xdev->nodename) !=
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) 	    XenbusStateInitialised)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) 	/* Wait for frontend to state that it has published the configuration */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) 	if (xenbus_read_driver_state(pdev->xdev->otherend) !=
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) 	    XenbusStateInitialised)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) 	dev_dbg(&pdev->xdev->dev, "Reading frontend config\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) 	err = xenbus_gather(XBT_NIL, pdev->xdev->otherend,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) 			    "pci-op-ref", "%u", &gnt_ref,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) 			    "event-channel", "%u", &remote_evtchn,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) 			    "magic", NULL, &magic, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) 	if (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) 		/* If configuration didn't get read correctly, wait longer */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) 		xenbus_dev_fatal(pdev->xdev, err,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) 				 "Error reading configuration from frontend");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) 	if (magic == NULL || strcmp(magic, XEN_PCI_MAGIC) != 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) 		xenbus_dev_fatal(pdev->xdev, -EFAULT,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) 				 "version mismatch (%s/%s) with pcifront - "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) 				 "halting " DRV_NAME,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) 				 magic, XEN_PCI_MAGIC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) 		err = -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) 	err = xen_pcibk_do_attach(pdev, gnt_ref, remote_evtchn);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) 	dev_dbg(&pdev->xdev->dev, "Connecting...\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) 	err = xenbus_switch_state(pdev->xdev, XenbusStateConnected);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) 		xenbus_dev_fatal(pdev->xdev, err,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) 				 "Error switching to connected state!");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) 	dev_dbg(&pdev->xdev->dev, "Connected? %d\n", err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) 	mutex_unlock(&pdev->dev_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) 	kfree(magic);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) 	return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) static int xen_pcibk_publish_pci_dev(struct xen_pcibk_device *pdev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) 				   unsigned int domain, unsigned int bus,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) 				   unsigned int devfn, unsigned int devid)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) 	int len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) 	char str[64];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) 	len = snprintf(str, sizeof(str), "vdev-%d", devid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) 	if (unlikely(len >= (sizeof(str) - 1))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) 		err = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) 	/* Note: The PV protocol uses %02x, don't change it */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) 	err = xenbus_printf(XBT_NIL, pdev->xdev->nodename, str,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) 			    "%04x:%02x:%02x.%02x", domain, bus,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) 			    PCI_SLOT(devfn), PCI_FUNC(devfn));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) 	return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) static int xen_pcibk_export_device(struct xen_pcibk_device *pdev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) 				 int domain, int bus, int slot, int func,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) 				 int devid)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) 	struct pci_dev *dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) 	int err = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) 	dev_dbg(&pdev->xdev->dev, "exporting dom %x bus %x slot %x func %x\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) 		domain, bus, slot, func);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) 	dev = pcistub_get_pci_dev_by_slot(pdev, domain, bus, slot, func);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) 	if (!dev) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) 		err = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) 		xenbus_dev_fatal(pdev->xdev, err,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) 				 "Couldn't locate PCI device "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) 				 "(%04x:%02x:%02x.%d)! "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) 				 "perhaps already in-use?",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) 				 domain, bus, slot, func);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) 	err = xen_pcibk_add_pci_dev(pdev, dev, devid,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) 				    xen_pcibk_publish_pci_dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) 	dev_info(&dev->dev, "registering for %d\n", pdev->xdev->otherend_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) 	if (xen_register_device_domain_owner(dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) 					     pdev->xdev->otherend_id) != 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) 		dev_err(&dev->dev, "Stealing ownership from dom%d.\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) 			xen_find_device_domain_owner(dev));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) 		xen_unregister_device_domain_owner(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) 		xen_register_device_domain_owner(dev, pdev->xdev->otherend_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) 	/* TODO: It'd be nice to export a bridge and have all of its children
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) 	 * get exported with it. This may be best done in xend (which will
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) 	 * have to calculate resource usage anyway) but we probably want to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) 	 * put something in here to ensure that if a bridge gets given to a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) 	 * driver domain, that all devices under that bridge are not given
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) 	 * to other driver domains (as he who controls the bridge can disable
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) 	 * it and stop the other devices from working).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) 	return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) static int xen_pcibk_remove_device(struct xen_pcibk_device *pdev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) 				 int domain, int bus, int slot, int func)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) 	int err = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) 	struct pci_dev *dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) 	dev_dbg(&pdev->xdev->dev, "removing dom %x bus %x slot %x func %x\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) 		domain, bus, slot, func);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) 	dev = xen_pcibk_get_pci_dev(pdev, domain, bus, PCI_DEVFN(slot, func));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) 	if (!dev) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) 		err = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) 		dev_dbg(&pdev->xdev->dev, "Couldn't locate PCI device "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) 			"(%04x:%02x:%02x.%d)! not owned by this domain\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) 			domain, bus, slot, func);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) 	dev_dbg(&dev->dev, "unregistering for %d\n", pdev->xdev->otherend_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) 	xen_unregister_device_domain_owner(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) 	/* N.B. This ends up calling pcistub_put_pci_dev which ends up
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) 	 * doing the FLR. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) 	xen_pcibk_release_pci_dev(pdev, dev, true /* use the lock. */);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) 	return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) static int xen_pcibk_publish_pci_root(struct xen_pcibk_device *pdev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) 				    unsigned int domain, unsigned int bus)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) 	unsigned int d, b;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) 	int i, root_num, len, err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) 	char str[64];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) 	dev_dbg(&pdev->xdev->dev, "Publishing pci roots\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) 	err = xenbus_scanf(XBT_NIL, pdev->xdev->nodename,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) 			   "root_num", "%d", &root_num);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) 	if (err == 0 || err == -ENOENT)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) 		root_num = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) 	else if (err < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) 	/* Verify that we haven't already published this pci root */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) 	for (i = 0; i < root_num; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) 		len = snprintf(str, sizeof(str), "root-%d", i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) 		if (unlikely(len >= (sizeof(str) - 1))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) 			err = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) 			goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) 		err = xenbus_scanf(XBT_NIL, pdev->xdev->nodename,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) 				   str, "%x:%x", &d, &b);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) 		if (err < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) 			goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) 		if (err != 2) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) 			err = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) 			goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) 		if (d == domain && b == bus) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) 			err = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) 			goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) 	len = snprintf(str, sizeof(str), "root-%d", root_num);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) 	if (unlikely(len >= (sizeof(str) - 1))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) 		err = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) 	dev_dbg(&pdev->xdev->dev, "writing root %d at %04x:%02x\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) 		root_num, domain, bus);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) 	err = xenbus_printf(XBT_NIL, pdev->xdev->nodename, str,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) 			    "%04x:%02x", domain, bus);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) 	err = xenbus_printf(XBT_NIL, pdev->xdev->nodename,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) 			    "root_num", "%d", (root_num + 1));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) 	return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) static int xen_pcibk_reconfigure(struct xen_pcibk_device *pdev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) 				 enum xenbus_state state)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) 	int err = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) 	int num_devs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) 	int domain, bus, slot, func;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) 	unsigned int substate;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) 	int i, len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) 	char state_str[64];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) 	char dev_str[64];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) 	dev_dbg(&pdev->xdev->dev, "Reconfiguring device ...\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) 	mutex_lock(&pdev->dev_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) 	if (xenbus_read_driver_state(pdev->xdev->nodename) != state)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) 	err = xenbus_scanf(XBT_NIL, pdev->xdev->nodename, "num_devs", "%d",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) 			   &num_devs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) 	if (err != 1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) 		if (err >= 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) 			err = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) 		xenbus_dev_fatal(pdev->xdev, err,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386) 				 "Error reading number of devices");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390) 	for (i = 0; i < num_devs; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391) 		len = snprintf(state_str, sizeof(state_str), "state-%d", i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) 		if (unlikely(len >= (sizeof(state_str) - 1))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393) 			err = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) 			xenbus_dev_fatal(pdev->xdev, err,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395) 					 "String overflow while reading "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396) 					 "configuration");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397) 			goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399) 		substate = xenbus_read_unsigned(pdev->xdev->nodename, state_str,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400) 						XenbusStateUnknown);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402) 		switch (substate) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403) 		case XenbusStateInitialising:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404) 			dev_dbg(&pdev->xdev->dev, "Attaching dev-%d ...\n", i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406) 			len = snprintf(dev_str, sizeof(dev_str), "dev-%d", i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407) 			if (unlikely(len >= (sizeof(dev_str) - 1))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408) 				err = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409) 				xenbus_dev_fatal(pdev->xdev, err,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410) 						 "String overflow while "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411) 						 "reading configuration");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412) 				goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414) 			err = xenbus_scanf(XBT_NIL, pdev->xdev->nodename,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 415) 					   dev_str, "%x:%x:%x.%x",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 416) 					   &domain, &bus, &slot, &func);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 417) 			if (err < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 418) 				xenbus_dev_fatal(pdev->xdev, err,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 419) 						 "Error reading device "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 420) 						 "configuration");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 421) 				goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 422) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 423) 			if (err != 4) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 424) 				err = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 425) 				xenbus_dev_fatal(pdev->xdev, err,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 426) 						 "Error parsing pci device "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 427) 						 "configuration");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 428) 				goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 429) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 430) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 431) 			err = xen_pcibk_export_device(pdev, domain, bus, slot,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 432) 						    func, i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 433) 			if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 434) 				goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 435) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 436) 			/* Publish pci roots. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 437) 			err = xen_pcibk_publish_pci_roots(pdev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 438) 						xen_pcibk_publish_pci_root);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 439) 			if (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 440) 				xenbus_dev_fatal(pdev->xdev, err,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 441) 						 "Error while publish PCI root"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 442) 						 "buses for frontend");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 443) 				goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 444) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 445) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 446) 			err = xenbus_printf(XBT_NIL, pdev->xdev->nodename,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 447) 					    state_str, "%d",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 448) 					    XenbusStateInitialised);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 449) 			if (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 450) 				xenbus_dev_fatal(pdev->xdev, err,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 451) 						 "Error switching substate of "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 452) 						 "dev-%d\n", i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 453) 				goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 454) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 455) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 456) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 457) 		case XenbusStateClosing:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 458) 			dev_dbg(&pdev->xdev->dev, "Detaching dev-%d ...\n", i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 459) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 460) 			len = snprintf(dev_str, sizeof(dev_str), "vdev-%d", i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 461) 			if (unlikely(len >= (sizeof(dev_str) - 1))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 462) 				err = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 463) 				xenbus_dev_fatal(pdev->xdev, err,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 464) 						 "String overflow while "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 465) 						 "reading configuration");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 466) 				goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 467) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 468) 			err = xenbus_scanf(XBT_NIL, pdev->xdev->nodename,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 469) 					   dev_str, "%x:%x:%x.%x",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 470) 					   &domain, &bus, &slot, &func);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 471) 			if (err < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 472) 				xenbus_dev_fatal(pdev->xdev, err,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 473) 						 "Error reading device "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 474) 						 "configuration");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 475) 				goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 476) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 477) 			if (err != 4) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 478) 				err = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 479) 				xenbus_dev_fatal(pdev->xdev, err,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 480) 						 "Error parsing pci device "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 481) 						 "configuration");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 482) 				goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 483) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 484) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 485) 			err = xen_pcibk_remove_device(pdev, domain, bus, slot,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 486) 						    func);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 487) 			if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 488) 				goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 489) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 490) 			/* TODO: If at some point we implement support for pci
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 491) 			 * root hot-remove on pcifront side, we'll need to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 492) 			 * remove unnecessary xenstore nodes of pci roots here.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 493) 			 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 494) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 495) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 496) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 497) 		default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 498) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 499) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 500) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 501) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 502) 	if (state != XenbusStateReconfiguring)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 503) 		/* Make sure we only reconfigure once. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 504) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 505) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 506) 	err = xenbus_switch_state(pdev->xdev, XenbusStateReconfigured);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 507) 	if (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 508) 		xenbus_dev_fatal(pdev->xdev, err,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 509) 				 "Error switching to reconfigured state!");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 510) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 511) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 512) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 513) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 514) 	mutex_unlock(&pdev->dev_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 515) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 516) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 517) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 518) static void xen_pcibk_frontend_changed(struct xenbus_device *xdev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 519) 				     enum xenbus_state fe_state)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 520) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 521) 	struct xen_pcibk_device *pdev = dev_get_drvdata(&xdev->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 522) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 523) 	dev_dbg(&xdev->dev, "fe state changed %d\n", fe_state);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 524) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 525) 	switch (fe_state) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 526) 	case XenbusStateInitialised:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 527) 		xen_pcibk_attach(pdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 528) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 529) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 530) 	case XenbusStateReconfiguring:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 531) 		xen_pcibk_reconfigure(pdev, XenbusStateReconfiguring);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 532) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 533) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 534) 	case XenbusStateConnected:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 535) 		/* pcifront switched its state from reconfiguring to connected.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 536) 		 * Then switch to connected state.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 537) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 538) 		xenbus_switch_state(xdev, XenbusStateConnected);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 539) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 540) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 541) 	case XenbusStateClosing:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 542) 		xen_pcibk_disconnect(pdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 543) 		xenbus_switch_state(xdev, XenbusStateClosing);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 544) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 545) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 546) 	case XenbusStateClosed:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 547) 		xen_pcibk_disconnect(pdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 548) 		xenbus_switch_state(xdev, XenbusStateClosed);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 549) 		if (xenbus_dev_is_online(xdev))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 550) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 551) 		fallthrough;	/* if not online */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 552) 	case XenbusStateUnknown:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 553) 		dev_dbg(&xdev->dev, "frontend is gone! unregister device\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 554) 		device_unregister(&xdev->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 555) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 556) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 557) 	default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 558) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 559) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 560) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 561) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 562) static int xen_pcibk_setup_backend(struct xen_pcibk_device *pdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 563) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 564) 	/* Get configuration from xend (if available now) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 565) 	int domain, bus, slot, func;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 566) 	int err = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 567) 	int i, num_devs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 568) 	char dev_str[64];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 569) 	char state_str[64];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 570) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 571) 	mutex_lock(&pdev->dev_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 572) 	/* It's possible we could get the call to setup twice, so make sure
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 573) 	 * we're not already connected.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 574) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 575) 	if (xenbus_read_driver_state(pdev->xdev->nodename) !=
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 576) 	    XenbusStateInitWait)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 577) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 578) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 579) 	dev_dbg(&pdev->xdev->dev, "getting be setup\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 580) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 581) 	err = xenbus_scanf(XBT_NIL, pdev->xdev->nodename, "num_devs", "%d",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 582) 			   &num_devs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 583) 	if (err != 1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 584) 		if (err >= 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 585) 			err = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 586) 		xenbus_dev_fatal(pdev->xdev, err,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 587) 				 "Error reading number of devices");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 588) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 589) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 590) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 591) 	for (i = 0; i < num_devs; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 592) 		int l = snprintf(dev_str, sizeof(dev_str), "dev-%d", i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 593) 		if (unlikely(l >= (sizeof(dev_str) - 1))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 594) 			err = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 595) 			xenbus_dev_fatal(pdev->xdev, err,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 596) 					 "String overflow while reading "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 597) 					 "configuration");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 598) 			goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 599) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 600) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 601) 		err = xenbus_scanf(XBT_NIL, pdev->xdev->nodename, dev_str,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 602) 				   "%x:%x:%x.%x", &domain, &bus, &slot, &func);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 603) 		if (err < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 604) 			xenbus_dev_fatal(pdev->xdev, err,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 605) 					 "Error reading device configuration");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 606) 			goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 607) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 608) 		if (err != 4) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 609) 			err = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 610) 			xenbus_dev_fatal(pdev->xdev, err,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 611) 					 "Error parsing pci device "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 612) 					 "configuration");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 613) 			goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 614) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 615) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 616) 		err = xen_pcibk_export_device(pdev, domain, bus, slot, func, i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 617) 		if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 618) 			goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 619) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 620) 		/* Switch substate of this device. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 621) 		l = snprintf(state_str, sizeof(state_str), "state-%d", i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 622) 		if (unlikely(l >= (sizeof(state_str) - 1))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 623) 			err = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 624) 			xenbus_dev_fatal(pdev->xdev, err,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 625) 					 "String overflow while reading "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 626) 					 "configuration");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 627) 			goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 628) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 629) 		err = xenbus_printf(XBT_NIL, pdev->xdev->nodename, state_str,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 630) 				    "%d", XenbusStateInitialised);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 631) 		if (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 632) 			xenbus_dev_fatal(pdev->xdev, err, "Error switching "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 633) 					 "substate of dev-%d\n", i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 634) 			goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 635) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 636) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 637) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 638) 	err = xen_pcibk_publish_pci_roots(pdev, xen_pcibk_publish_pci_root);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 639) 	if (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 640) 		xenbus_dev_fatal(pdev->xdev, err,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 641) 				 "Error while publish PCI root buses "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 642) 				 "for frontend");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 643) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 644) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 645) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 646) 	err = xenbus_switch_state(pdev->xdev, XenbusStateInitialised);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 647) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 648) 		xenbus_dev_fatal(pdev->xdev, err,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 649) 				 "Error switching to initialised state!");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 650) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 651) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 652) 	mutex_unlock(&pdev->dev_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 653) 	if (!err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 654) 		/* see if pcifront is already configured (if not, we'll wait) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 655) 		xen_pcibk_attach(pdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 656) 	return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 657) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 658) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 659) static void xen_pcibk_be_watch(struct xenbus_watch *watch,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 660) 			       const char *path, const char *token)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 661) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 662) 	struct xen_pcibk_device *pdev =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 663) 	    container_of(watch, struct xen_pcibk_device, be_watch);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 664) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 665) 	switch (xenbus_read_driver_state(pdev->xdev->nodename)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 666) 	case XenbusStateInitWait:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 667) 		xen_pcibk_setup_backend(pdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 668) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 669) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 670) 	case XenbusStateInitialised:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 671) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 672) 		 * We typically move to Initialised when the first device was
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 673) 		 * added. Hence subsequent devices getting added may need
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 674) 		 * reconfiguring.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 675) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 676) 		xen_pcibk_reconfigure(pdev, XenbusStateInitialised);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 677) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 678) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 679) 	default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 680) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 681) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 682) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 683) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 684) static int xen_pcibk_xenbus_probe(struct xenbus_device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 685) 				const struct xenbus_device_id *id)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 686) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 687) 	int err = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 688) 	struct xen_pcibk_device *pdev = alloc_pdev(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 689) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 690) 	if (pdev == NULL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 691) 		err = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 692) 		xenbus_dev_fatal(dev, err,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 693) 				 "Error allocating xen_pcibk_device struct");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 694) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 695) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 696) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 697) 	/* wait for xend to configure us */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 698) 	err = xenbus_switch_state(dev, XenbusStateInitWait);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 699) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 700) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 701) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 702) 	/* watch the backend node for backend configuration information */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 703) 	err = xenbus_watch_path(dev, dev->nodename, &pdev->be_watch,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 704) 				NULL, xen_pcibk_be_watch);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 705) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 706) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 707) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 708) 	pdev->be_watching = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 709) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 710) 	/* We need to force a call to our callback here in case
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 711) 	 * xend already configured us!
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 712) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 713) 	xen_pcibk_be_watch(&pdev->be_watch, NULL, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 714) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 715) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 716) 	return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 717) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 718) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 719) static int xen_pcibk_xenbus_remove(struct xenbus_device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 720) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 721) 	struct xen_pcibk_device *pdev = dev_get_drvdata(&dev->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 722) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 723) 	if (pdev != NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 724) 		free_pdev(pdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 725) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 726) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 727) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 728) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 729) static const struct xenbus_device_id xen_pcibk_ids[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 730) 	{"pci"},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 731) 	{""},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 732) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 733) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 734) static struct xenbus_driver xen_pcibk_driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 735) 	.name                   = DRV_NAME,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 736) 	.ids                    = xen_pcibk_ids,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 737) 	.probe			= xen_pcibk_xenbus_probe,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 738) 	.remove			= xen_pcibk_xenbus_remove,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 739) 	.otherend_changed	= xen_pcibk_frontend_changed,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 740) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 741) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 742) const struct xen_pcibk_backend *__read_mostly xen_pcibk_backend;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 743) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 744) int __init xen_pcibk_xenbus_register(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 745) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 746) 	xen_pcibk_backend = &xen_pcibk_vpci_backend;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 747) 	if (passthrough)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 748) 		xen_pcibk_backend = &xen_pcibk_passthrough_backend;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 749) 	pr_info("backend is %s\n", xen_pcibk_backend->name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 750) 	return xenbus_register_backend(&xen_pcibk_driver);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 751) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 752) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 753) void __exit xen_pcibk_xenbus_unregister(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 754) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 755) 	xenbus_unregister_driver(&xen_pcibk_driver);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 756) }