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