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 | */ | |
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> | |
6221a9b2 | 13 | #include <asm/xen/pci.h> |
30edc14b KRW |
14 | #include "pciback.h" |
15 | ||
16 | #define INVALID_EVTCHN_IRQ (-1) | |
a92336a1 | 17 | struct workqueue_struct *xen_pcibk_wq; |
30edc14b | 18 | |
2ebdc426 KRW |
19 | static int __read_mostly passthrough; |
20 | module_param(passthrough, bool, S_IRUGO); | |
21 | MODULE_PARM_DESC(passthrough, | |
22 | "Option to specify how to export PCI topology to guest:\n"\ | |
23 | " 0 - (default) Hide the true PCI topology and makes the frontend\n"\ | |
24 | " there is a single PCI bus with only the exported devices on it.\n"\ | |
25 | " For example, a device at 03:05.0 will be re-assigned to 00:00.0\n"\ | |
26 | " while second device at 02:1a.1 will be re-assigned to 00:01.1.\n"\ | |
27 | " 1 - Passthrough provides a real view of the PCI topology to the\n"\ | |
28 | " frontend (for example, a device at 06:01.b will still appear at\n"\ | |
29 | " 06:01.b to the frontend). This is similar to how Xen 2.0.x\n"\ | |
30 | " exposed PCI devices to its driver domains. This may be required\n"\ | |
31 | " for drivers which depend on finding their hardward in certain\n"\ | |
32 | " bus/slot locations."); | |
33 | ||
a92336a1 | 34 | static struct xen_pcibk_device *alloc_pdev(struct xenbus_device *xdev) |
30edc14b | 35 | { |
a92336a1 | 36 | struct xen_pcibk_device *pdev; |
30edc14b | 37 | |
a92336a1 | 38 | pdev = kzalloc(sizeof(struct xen_pcibk_device), GFP_KERNEL); |
30edc14b KRW |
39 | if (pdev == NULL) |
40 | goto out; | |
41 | dev_dbg(&xdev->dev, "allocated pdev @ 0x%p\n", pdev); | |
42 | ||
43 | pdev->xdev = xdev; | |
44 | dev_set_drvdata(&xdev->dev, pdev); | |
45 | ||
46 | spin_lock_init(&pdev->dev_lock); | |
47 | ||
48 | pdev->sh_info = NULL; | |
49 | pdev->evtchn_irq = INVALID_EVTCHN_IRQ; | |
50 | pdev->be_watching = 0; | |
51 | ||
a92336a1 | 52 | INIT_WORK(&pdev->op_work, xen_pcibk_do_op); |
30edc14b | 53 | |
a92336a1 | 54 | if (xen_pcibk_init_devices(pdev)) { |
30edc14b KRW |
55 | kfree(pdev); |
56 | pdev = NULL; | |
57 | } | |
58 | out: | |
59 | return pdev; | |
60 | } | |
61 | ||
a92336a1 | 62 | static void xen_pcibk_disconnect(struct xen_pcibk_device *pdev) |
30edc14b KRW |
63 | { |
64 | spin_lock(&pdev->dev_lock); | |
65 | ||
66 | /* Ensure the guest can't trigger our handler before removing devices */ | |
67 | if (pdev->evtchn_irq != INVALID_EVTCHN_IRQ) { | |
68 | unbind_from_irqhandler(pdev->evtchn_irq, pdev); | |
69 | pdev->evtchn_irq = INVALID_EVTCHN_IRQ; | |
70 | } | |
494ef20d | 71 | spin_unlock(&pdev->dev_lock); |
30edc14b KRW |
72 | |
73 | /* If the driver domain started an op, make sure we complete it | |
74 | * before releasing the shared memory */ | |
494ef20d KRW |
75 | |
76 | /* Note, the workqueue does not use spinlocks at all.*/ | |
a92336a1 | 77 | flush_workqueue(xen_pcibk_wq); |
30edc14b | 78 | |
494ef20d | 79 | spin_lock(&pdev->dev_lock); |
30edc14b KRW |
80 | if (pdev->sh_info != NULL) { |
81 | xenbus_unmap_ring_vfree(pdev->xdev, pdev->sh_info); | |
82 | pdev->sh_info = NULL; | |
83 | } | |
30edc14b | 84 | spin_unlock(&pdev->dev_lock); |
494ef20d | 85 | |
30edc14b KRW |
86 | } |
87 | ||
a92336a1 | 88 | static void free_pdev(struct xen_pcibk_device *pdev) |
30edc14b | 89 | { |
494ef20d | 90 | if (pdev->be_watching) { |
30edc14b | 91 | unregister_xenbus_watch(&pdev->be_watch); |
494ef20d KRW |
92 | pdev->be_watching = 0; |
93 | } | |
30edc14b | 94 | |
a92336a1 | 95 | xen_pcibk_disconnect(pdev); |
30edc14b | 96 | |
a92336a1 | 97 | xen_pcibk_release_devices(pdev); |
30edc14b KRW |
98 | |
99 | dev_set_drvdata(&pdev->xdev->dev, NULL); | |
100 | pdev->xdev = NULL; | |
101 | ||
102 | kfree(pdev); | |
103 | } | |
104 | ||
a92336a1 | 105 | static int xen_pcibk_do_attach(struct xen_pcibk_device *pdev, int gnt_ref, |
30edc14b KRW |
106 | int remote_evtchn) |
107 | { | |
108 | int err = 0; | |
109 | void *vaddr; | |
110 | ||
111 | dev_dbg(&pdev->xdev->dev, | |
112 | "Attaching to frontend resources - gnt_ref=%d evtchn=%d\n", | |
113 | gnt_ref, remote_evtchn); | |
114 | ||
115 | err = xenbus_map_ring_valloc(pdev->xdev, gnt_ref, &vaddr); | |
116 | if (err < 0) { | |
117 | xenbus_dev_fatal(pdev->xdev, err, | |
118 | "Error mapping other domain page in ours."); | |
119 | goto out; | |
120 | } | |
494ef20d KRW |
121 | |
122 | spin_lock(&pdev->dev_lock); | |
30edc14b | 123 | pdev->sh_info = vaddr; |
494ef20d | 124 | spin_unlock(&pdev->dev_lock); |
30edc14b KRW |
125 | |
126 | err = bind_interdomain_evtchn_to_irqhandler( | |
a92336a1 KRW |
127 | pdev->xdev->otherend_id, remote_evtchn, xen_pcibk_handle_event, |
128 | 0, DRV_NAME, pdev); | |
30edc14b KRW |
129 | if (err < 0) { |
130 | xenbus_dev_fatal(pdev->xdev, err, | |
131 | "Error binding event channel to IRQ"); | |
132 | goto out; | |
133 | } | |
494ef20d KRW |
134 | |
135 | spin_lock(&pdev->dev_lock); | |
30edc14b | 136 | pdev->evtchn_irq = err; |
494ef20d | 137 | spin_unlock(&pdev->dev_lock); |
30edc14b KRW |
138 | err = 0; |
139 | ||
140 | dev_dbg(&pdev->xdev->dev, "Attached!\n"); | |
141 | out: | |
142 | return err; | |
143 | } | |
144 | ||
a92336a1 | 145 | static int xen_pcibk_attach(struct xen_pcibk_device *pdev) |
30edc14b KRW |
146 | { |
147 | int err = 0; | |
148 | int gnt_ref, remote_evtchn; | |
149 | char *magic = NULL; | |
150 | ||
30edc14b KRW |
151 | |
152 | /* Make sure we only do this setup once */ | |
153 | if (xenbus_read_driver_state(pdev->xdev->nodename) != | |
154 | XenbusStateInitialised) | |
155 | goto out; | |
156 | ||
157 | /* Wait for frontend to state that it has published the configuration */ | |
158 | if (xenbus_read_driver_state(pdev->xdev->otherend) != | |
159 | XenbusStateInitialised) | |
160 | goto out; | |
161 | ||
162 | dev_dbg(&pdev->xdev->dev, "Reading frontend config\n"); | |
163 | ||
164 | err = xenbus_gather(XBT_NIL, pdev->xdev->otherend, | |
165 | "pci-op-ref", "%u", &gnt_ref, | |
166 | "event-channel", "%u", &remote_evtchn, | |
167 | "magic", NULL, &magic, NULL); | |
168 | if (err) { | |
169 | /* If configuration didn't get read correctly, wait longer */ | |
170 | xenbus_dev_fatal(pdev->xdev, err, | |
171 | "Error reading configuration from frontend"); | |
172 | goto out; | |
173 | } | |
174 | ||
175 | if (magic == NULL || strcmp(magic, XEN_PCI_MAGIC) != 0) { | |
176 | xenbus_dev_fatal(pdev->xdev, -EFAULT, | |
177 | "version mismatch (%s/%s) with pcifront - " | |
402c5e15 | 178 | "halting " DRV_NAME, |
30edc14b KRW |
179 | magic, XEN_PCI_MAGIC); |
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: | |
30edc14b KRW |
196 | |
197 | kfree(magic); | |
198 | ||
199 | return err; | |
200 | } | |
201 | ||
a92336a1 | 202 | static int xen_pcibk_publish_pci_dev(struct xen_pcibk_device *pdev, |
30edc14b KRW |
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 | ||
216 | err = xenbus_printf(XBT_NIL, pdev->xdev->nodename, str, | |
217 | "%04x:%02x:%02x.%02x", domain, bus, | |
218 | PCI_SLOT(devfn), PCI_FUNC(devfn)); | |
219 | ||
220 | out: | |
221 | return err; | |
222 | } | |
223 | ||
a92336a1 | 224 | static int xen_pcibk_export_device(struct xen_pcibk_device *pdev, |
30edc14b KRW |
225 | int domain, int bus, int slot, int func, |
226 | int devid) | |
227 | { | |
228 | struct pci_dev *dev; | |
229 | int err = 0; | |
230 | ||
231 | dev_dbg(&pdev->xdev->dev, "exporting dom %x bus %x slot %x func %x\n", | |
232 | domain, bus, slot, func); | |
233 | ||
234 | dev = pcistub_get_pci_dev_by_slot(pdev, domain, bus, slot, func); | |
235 | if (!dev) { | |
236 | err = -EINVAL; | |
237 | xenbus_dev_fatal(pdev->xdev, err, | |
238 | "Couldn't locate PCI device " | |
239 | "(%04x:%02x:%02x.%01x)! " | |
240 | "perhaps already in-use?", | |
241 | domain, bus, slot, func); | |
242 | goto out; | |
243 | } | |
244 | ||
a92336a1 KRW |
245 | err = xen_pcibk_add_pci_dev(pdev, dev, devid, |
246 | xen_pcibk_publish_pci_dev); | |
30edc14b KRW |
247 | if (err) |
248 | goto out; | |
249 | ||
6221a9b2 KRW |
250 | dev_dbg(&dev->dev, "registering for %d\n", pdev->xdev->otherend_id); |
251 | if (xen_register_device_domain_owner(dev, | |
252 | pdev->xdev->otherend_id) != 0) { | |
253 | dev_err(&dev->dev, "device has been assigned to another " \ | |
254 | "domain! Over-writting the ownership, but beware.\n"); | |
255 | xen_unregister_device_domain_owner(dev); | |
256 | xen_register_device_domain_owner(dev, pdev->xdev->otherend_id); | |
257 | } | |
258 | ||
30edc14b KRW |
259 | /* TODO: It'd be nice to export a bridge and have all of its children |
260 | * get exported with it. This may be best done in xend (which will | |
261 | * have to calculate resource usage anyway) but we probably want to | |
262 | * put something in here to ensure that if a bridge gets given to a | |
263 | * driver domain, that all devices under that bridge are not given | |
264 | * to other driver domains (as he who controls the bridge can disable | |
265 | * it and stop the other devices from working). | |
266 | */ | |
267 | out: | |
268 | return err; | |
269 | } | |
270 | ||
a92336a1 | 271 | static int xen_pcibk_remove_device(struct xen_pcibk_device *pdev, |
30edc14b KRW |
272 | int domain, int bus, int slot, int func) |
273 | { | |
274 | int err = 0; | |
275 | struct pci_dev *dev; | |
276 | ||
277 | dev_dbg(&pdev->xdev->dev, "removing dom %x bus %x slot %x func %x\n", | |
278 | domain, bus, slot, func); | |
279 | ||
a92336a1 | 280 | dev = xen_pcibk_get_pci_dev(pdev, domain, bus, PCI_DEVFN(slot, func)); |
30edc14b KRW |
281 | if (!dev) { |
282 | err = -EINVAL; | |
283 | dev_dbg(&pdev->xdev->dev, "Couldn't locate PCI device " | |
284 | "(%04x:%02x:%02x.%01x)! not owned by this domain\n", | |
285 | domain, bus, slot, func); | |
286 | goto out; | |
287 | } | |
288 | ||
6221a9b2 KRW |
289 | dev_dbg(&dev->dev, "unregistering for %d\n", pdev->xdev->otherend_id); |
290 | xen_unregister_device_domain_owner(dev); | |
291 | ||
a92336a1 | 292 | xen_pcibk_release_pci_dev(pdev, dev); |
30edc14b KRW |
293 | |
294 | out: | |
295 | return err; | |
296 | } | |
297 | ||
a92336a1 | 298 | static int xen_pcibk_publish_pci_root(struct xen_pcibk_device *pdev, |
30edc14b KRW |
299 | unsigned int domain, unsigned int bus) |
300 | { | |
301 | unsigned int d, b; | |
302 | int i, root_num, len, err; | |
303 | char str[64]; | |
304 | ||
305 | dev_dbg(&pdev->xdev->dev, "Publishing pci roots\n"); | |
306 | ||
307 | err = xenbus_scanf(XBT_NIL, pdev->xdev->nodename, | |
308 | "root_num", "%d", &root_num); | |
309 | if (err == 0 || err == -ENOENT) | |
310 | root_num = 0; | |
311 | else if (err < 0) | |
312 | goto out; | |
313 | ||
314 | /* Verify that we haven't already published this pci root */ | |
315 | for (i = 0; i < root_num; i++) { | |
316 | len = snprintf(str, sizeof(str), "root-%d", i); | |
317 | if (unlikely(len >= (sizeof(str) - 1))) { | |
318 | err = -ENOMEM; | |
319 | goto out; | |
320 | } | |
321 | ||
322 | err = xenbus_scanf(XBT_NIL, pdev->xdev->nodename, | |
323 | str, "%x:%x", &d, &b); | |
324 | if (err < 0) | |
325 | goto out; | |
326 | if (err != 2) { | |
327 | err = -EINVAL; | |
328 | goto out; | |
329 | } | |
330 | ||
331 | if (d == domain && b == bus) { | |
332 | err = 0; | |
333 | goto out; | |
334 | } | |
335 | } | |
336 | ||
337 | len = snprintf(str, sizeof(str), "root-%d", root_num); | |
338 | if (unlikely(len >= (sizeof(str) - 1))) { | |
339 | err = -ENOMEM; | |
340 | goto out; | |
341 | } | |
342 | ||
343 | dev_dbg(&pdev->xdev->dev, "writing root %d at %04x:%02x\n", | |
344 | root_num, domain, bus); | |
345 | ||
346 | err = xenbus_printf(XBT_NIL, pdev->xdev->nodename, str, | |
347 | "%04x:%02x", domain, bus); | |
348 | if (err) | |
349 | goto out; | |
350 | ||
351 | err = xenbus_printf(XBT_NIL, pdev->xdev->nodename, | |
352 | "root_num", "%d", (root_num + 1)); | |
353 | ||
354 | out: | |
355 | return err; | |
356 | } | |
357 | ||
a92336a1 | 358 | static int xen_pcibk_reconfigure(struct xen_pcibk_device *pdev) |
30edc14b KRW |
359 | { |
360 | int err = 0; | |
361 | int num_devs; | |
362 | int domain, bus, slot, func; | |
363 | int substate; | |
364 | int i, len; | |
365 | char state_str[64]; | |
366 | char dev_str[64]; | |
367 | ||
30edc14b KRW |
368 | |
369 | dev_dbg(&pdev->xdev->dev, "Reconfiguring device ...\n"); | |
370 | ||
371 | /* Make sure we only reconfigure once */ | |
372 | if (xenbus_read_driver_state(pdev->xdev->nodename) != | |
373 | XenbusStateReconfiguring) | |
374 | goto out; | |
375 | ||
376 | err = xenbus_scanf(XBT_NIL, pdev->xdev->nodename, "num_devs", "%d", | |
377 | &num_devs); | |
378 | if (err != 1) { | |
379 | if (err >= 0) | |
380 | err = -EINVAL; | |
381 | xenbus_dev_fatal(pdev->xdev, err, | |
382 | "Error reading number of devices"); | |
383 | goto out; | |
384 | } | |
385 | ||
386 | for (i = 0; i < num_devs; i++) { | |
387 | len = snprintf(state_str, sizeof(state_str), "state-%d", i); | |
388 | if (unlikely(len >= (sizeof(state_str) - 1))) { | |
389 | err = -ENOMEM; | |
390 | xenbus_dev_fatal(pdev->xdev, err, | |
391 | "String overflow while reading " | |
392 | "configuration"); | |
393 | goto out; | |
394 | } | |
395 | err = xenbus_scanf(XBT_NIL, pdev->xdev->nodename, state_str, | |
396 | "%d", &substate); | |
397 | if (err != 1) | |
398 | substate = XenbusStateUnknown; | |
399 | ||
400 | switch (substate) { | |
401 | case XenbusStateInitialising: | |
402 | dev_dbg(&pdev->xdev->dev, "Attaching dev-%d ...\n", i); | |
403 | ||
404 | len = snprintf(dev_str, sizeof(dev_str), "dev-%d", i); | |
405 | if (unlikely(len >= (sizeof(dev_str) - 1))) { | |
406 | err = -ENOMEM; | |
407 | xenbus_dev_fatal(pdev->xdev, err, | |
408 | "String overflow while " | |
409 | "reading configuration"); | |
410 | goto out; | |
411 | } | |
412 | err = xenbus_scanf(XBT_NIL, pdev->xdev->nodename, | |
413 | dev_str, "%x:%x:%x.%x", | |
414 | &domain, &bus, &slot, &func); | |
415 | if (err < 0) { | |
416 | xenbus_dev_fatal(pdev->xdev, err, | |
417 | "Error reading device " | |
418 | "configuration"); | |
419 | goto out; | |
420 | } | |
421 | if (err != 4) { | |
422 | err = -EINVAL; | |
423 | xenbus_dev_fatal(pdev->xdev, err, | |
424 | "Error parsing pci device " | |
425 | "configuration"); | |
426 | goto out; | |
427 | } | |
428 | ||
a92336a1 | 429 | err = xen_pcibk_export_device(pdev, domain, bus, slot, |
30edc14b KRW |
430 | func, i); |
431 | if (err) | |
432 | goto out; | |
433 | ||
434 | /* Publish pci roots. */ | |
a92336a1 KRW |
435 | err = xen_pcibk_publish_pci_roots(pdev, |
436 | xen_pcibk_publish_pci_root); | |
30edc14b KRW |
437 | if (err) { |
438 | xenbus_dev_fatal(pdev->xdev, err, | |
439 | "Error while publish PCI root" | |
440 | "buses for frontend"); | |
441 | goto out; | |
442 | } | |
443 | ||
444 | err = xenbus_printf(XBT_NIL, pdev->xdev->nodename, | |
445 | state_str, "%d", | |
446 | XenbusStateInitialised); | |
447 | if (err) { | |
448 | xenbus_dev_fatal(pdev->xdev, err, | |
449 | "Error switching substate of " | |
450 | "dev-%d\n", i); | |
451 | goto out; | |
452 | } | |
453 | break; | |
454 | ||
455 | case XenbusStateClosing: | |
456 | dev_dbg(&pdev->xdev->dev, "Detaching dev-%d ...\n", i); | |
457 | ||
458 | len = snprintf(dev_str, sizeof(dev_str), "vdev-%d", i); | |
459 | if (unlikely(len >= (sizeof(dev_str) - 1))) { | |
460 | err = -ENOMEM; | |
461 | xenbus_dev_fatal(pdev->xdev, err, | |
462 | "String overflow while " | |
463 | "reading configuration"); | |
464 | goto out; | |
465 | } | |
466 | err = xenbus_scanf(XBT_NIL, pdev->xdev->nodename, | |
467 | dev_str, "%x:%x:%x.%x", | |
468 | &domain, &bus, &slot, &func); | |
469 | if (err < 0) { | |
470 | xenbus_dev_fatal(pdev->xdev, err, | |
471 | "Error reading device " | |
472 | "configuration"); | |
473 | goto out; | |
474 | } | |
475 | if (err != 4) { | |
476 | err = -EINVAL; | |
477 | xenbus_dev_fatal(pdev->xdev, err, | |
478 | "Error parsing pci device " | |
479 | "configuration"); | |
480 | goto out; | |
481 | } | |
482 | ||
a92336a1 | 483 | err = xen_pcibk_remove_device(pdev, domain, bus, slot, |
30edc14b KRW |
484 | func); |
485 | if (err) | |
486 | goto out; | |
487 | ||
488 | /* TODO: If at some point we implement support for pci | |
489 | * root hot-remove on pcifront side, we'll need to | |
490 | * remove unnecessary xenstore nodes of pci roots here. | |
491 | */ | |
492 | ||
493 | break; | |
494 | ||
495 | default: | |
496 | break; | |
497 | } | |
498 | } | |
499 | ||
500 | err = xenbus_switch_state(pdev->xdev, XenbusStateReconfigured); | |
501 | if (err) { | |
502 | xenbus_dev_fatal(pdev->xdev, err, | |
503 | "Error switching to reconfigured state!"); | |
504 | goto out; | |
505 | } | |
506 | ||
507 | out: | |
30edc14b KRW |
508 | return 0; |
509 | } | |
510 | ||
a92336a1 | 511 | static void xen_pcibk_frontend_changed(struct xenbus_device *xdev, |
30edc14b KRW |
512 | enum xenbus_state fe_state) |
513 | { | |
a92336a1 | 514 | struct xen_pcibk_device *pdev = dev_get_drvdata(&xdev->dev); |
30edc14b KRW |
515 | |
516 | dev_dbg(&xdev->dev, "fe state changed %d\n", fe_state); | |
517 | ||
518 | switch (fe_state) { | |
519 | case XenbusStateInitialised: | |
a92336a1 | 520 | xen_pcibk_attach(pdev); |
30edc14b KRW |
521 | break; |
522 | ||
523 | case XenbusStateReconfiguring: | |
a92336a1 | 524 | xen_pcibk_reconfigure(pdev); |
30edc14b KRW |
525 | break; |
526 | ||
527 | case XenbusStateConnected: | |
528 | /* pcifront switched its state from reconfiguring to connected. | |
529 | * Then switch to connected state. | |
530 | */ | |
531 | xenbus_switch_state(xdev, XenbusStateConnected); | |
532 | break; | |
533 | ||
534 | case XenbusStateClosing: | |
a92336a1 | 535 | xen_pcibk_disconnect(pdev); |
30edc14b KRW |
536 | xenbus_switch_state(xdev, XenbusStateClosing); |
537 | break; | |
538 | ||
539 | case XenbusStateClosed: | |
a92336a1 | 540 | xen_pcibk_disconnect(pdev); |
30edc14b KRW |
541 | xenbus_switch_state(xdev, XenbusStateClosed); |
542 | if (xenbus_dev_is_online(xdev)) | |
543 | break; | |
544 | /* fall through if not online */ | |
545 | case XenbusStateUnknown: | |
546 | dev_dbg(&xdev->dev, "frontend is gone! unregister device\n"); | |
547 | device_unregister(&xdev->dev); | |
548 | break; | |
549 | ||
550 | default: | |
551 | break; | |
552 | } | |
553 | } | |
554 | ||
a92336a1 | 555 | static int xen_pcibk_setup_backend(struct xen_pcibk_device *pdev) |
30edc14b KRW |
556 | { |
557 | /* Get configuration from xend (if available now) */ | |
558 | int domain, bus, slot, func; | |
559 | int err = 0; | |
560 | int i, num_devs; | |
561 | char dev_str[64]; | |
562 | char state_str[64]; | |
563 | ||
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: | |
30edc14b KRW |
644 | if (!err) |
645 | /* see if pcifront is already configured (if not, we'll wait) */ | |
a92336a1 | 646 | xen_pcibk_attach(pdev); |
30edc14b KRW |
647 | |
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 | ||
712 | static const struct xenbus_device_id xenpci_ids[] = { | |
713 | {"pci"}, | |
714 | {""}, | |
715 | }; | |
716 | ||
a92336a1 KRW |
717 | static struct xenbus_driver xenbus_xen_pcibk_driver = { |
718 | .name = DRV_NAME, | |
8bfd4e02 KRW |
719 | .owner = THIS_MODULE, |
720 | .ids = xenpci_ids, | |
a92336a1 KRW |
721 | .probe = xen_pcibk_xenbus_probe, |
722 | .remove = xen_pcibk_xenbus_remove, | |
723 | .otherend_changed = xen_pcibk_frontend_changed, | |
30edc14b KRW |
724 | }; |
725 | ||
402c5e15 | 726 | const struct xen_pcibk_backend *__read_mostly xen_pcibk_backend; |
2ebdc426 | 727 | |
a92336a1 | 728 | int __init xen_pcibk_xenbus_register(void) |
30edc14b | 729 | { |
a92336a1 KRW |
730 | xen_pcibk_wq = create_workqueue("xen_pciback_workqueue"); |
731 | if (!xen_pcibk_wq) { | |
8bfd4e02 | 732 | printk(KERN_ERR "%s: create" |
a92336a1 | 733 | "xen_pciback_workqueue failed\n", __func__); |
30edc14b KRW |
734 | return -EFAULT; |
735 | } | |
2ebdc426 KRW |
736 | xen_pcibk_backend = &xen_pcibk_vpci_backend; |
737 | if (passthrough) | |
738 | xen_pcibk_backend = &xen_pcibk_passthrough_backend; | |
739 | pr_info(DRV_NAME ": backend is %s\n", xen_pcibk_backend->name); | |
a92336a1 | 740 | return xenbus_register_backend(&xenbus_xen_pcibk_driver); |
30edc14b KRW |
741 | } |
742 | ||
a92336a1 | 743 | void __exit xen_pcibk_xenbus_unregister(void) |
30edc14b | 744 | { |
a92336a1 KRW |
745 | destroy_workqueue(xen_pcibk_wq); |
746 | xenbus_unregister_driver(&xenbus_xen_pcibk_driver); | |
30edc14b | 747 | } |