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