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