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