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