ia64/xen-unstable

view linux-2.6-xen-sparse/drivers/xen/pciback/xenbus.c @ 10916:ab86a6f3b5ee

[PCI] Transparent virtualization for pcifront and pciback.
Signed-off-by: Tristan Gingold <tristan.gingold@bull.net>
author kfraser@localhost.localdomain
date Wed Aug 02 15:07:21 2006 +0100 (2006-08-02)
parents 49f65e2b0f1c
children 26576f1dbadb
line source
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 <xen/xenbus.h>
11 #include <xen/evtchn.h>
12 #include "pciback.h"
14 #define INVALID_EVTCHN_IRQ (-1)
16 static struct pciback_device *alloc_pdev(struct xenbus_device *xdev)
17 {
18 struct pciback_device *pdev;
20 pdev = kzalloc(sizeof(struct pciback_device), GFP_KERNEL);
21 if (pdev == NULL)
22 goto out;
23 dev_dbg(&xdev->dev, "allocated pdev @ 0x%p\n", pdev);
25 pdev->xdev = xdev;
26 xdev->dev.driver_data = pdev;
28 spin_lock_init(&pdev->dev_lock);
30 pdev->sh_area = NULL;
31 pdev->sh_info = NULL;
32 pdev->evtchn_irq = INVALID_EVTCHN_IRQ;
33 pdev->be_watching = 0;
35 INIT_WORK(&pdev->op_work, pciback_do_op, pdev);
37 if (pciback_init_devices(pdev)) {
38 kfree(pdev);
39 pdev = NULL;
40 }
41 out:
42 return pdev;
43 }
45 static void free_pdev(struct pciback_device *pdev)
46 {
47 if (pdev->be_watching)
48 unregister_xenbus_watch(&pdev->be_watch);
50 /* Ensure the guest can't trigger our handler before removing devices */
51 if (pdev->evtchn_irq != INVALID_EVTCHN_IRQ)
52 unbind_from_irqhandler(pdev->evtchn_irq, pdev);
54 /* If the driver domain started an op, make sure we complete it or
55 * delete it before releasing the shared memory */
56 cancel_delayed_work(&pdev->op_work);
57 flush_scheduled_work();
59 if (pdev->sh_info)
60 xenbus_unmap_ring_vfree(pdev->xdev, pdev->sh_area);
62 pciback_release_devices(pdev);
64 pdev->xdev->dev.driver_data = NULL;
65 pdev->xdev = NULL;
67 kfree(pdev);
68 }
70 static int pciback_do_attach(struct pciback_device *pdev, int gnt_ref,
71 int remote_evtchn)
72 {
73 int err = 0;
74 int evtchn;
75 struct vm_struct *area;
77 dev_dbg(&pdev->xdev->dev,
78 "Attaching to frontend resources - gnt_ref=%d evtchn=%d\n",
79 gnt_ref, remote_evtchn);
81 area = xenbus_map_ring_valloc(pdev->xdev, gnt_ref);
82 if (IS_ERR(area)) {
83 err = PTR_ERR(area);
84 goto out;
85 }
86 pdev->sh_area = area;
87 pdev->sh_info = area->addr;
89 err = xenbus_bind_evtchn(pdev->xdev, remote_evtchn, &evtchn);
90 if (err)
91 goto out;
93 err = bind_evtchn_to_irqhandler(evtchn, pciback_handle_event,
94 SA_SAMPLE_RANDOM, "pciback", pdev);
95 if (err < 0) {
96 xenbus_dev_fatal(pdev->xdev, err,
97 "Error binding event channel to IRQ");
98 goto out;
99 }
100 pdev->evtchn_irq = err;
101 err = 0;
103 dev_dbg(&pdev->xdev->dev, "Attached!\n");
104 out:
105 return err;
106 }
108 static int pciback_attach(struct pciback_device *pdev)
109 {
110 int err = 0;
111 int gnt_ref, remote_evtchn;
112 char *magic = NULL;
114 spin_lock(&pdev->dev_lock);
116 /* Make sure we only do this setup once */
117 if (xenbus_read_driver_state(pdev->xdev->nodename) !=
118 XenbusStateInitialised)
119 goto out;
121 /* Wait for frontend to state that it has published the configuration */
122 if (xenbus_read_driver_state(pdev->xdev->otherend) !=
123 XenbusStateInitialised)
124 goto out;
126 dev_dbg(&pdev->xdev->dev, "Reading frontend config\n");
128 err = xenbus_gather(XBT_NIL, pdev->xdev->otherend,
129 "pci-op-ref", "%u", &gnt_ref,
130 "event-channel", "%u", &remote_evtchn,
131 "magic", NULL, &magic, NULL);
132 if (err) {
133 /* If configuration didn't get read correctly, wait longer */
134 xenbus_dev_fatal(pdev->xdev, err,
135 "Error reading configuration from frontend");
136 goto out;
137 }
139 if (magic == NULL || strcmp(magic, XEN_PCI_MAGIC) != 0) {
140 xenbus_dev_fatal(pdev->xdev, -EFAULT,
141 "version mismatch (%s/%s) with pcifront - "
142 "halting pciback",
143 magic, XEN_PCI_MAGIC);
144 goto out;
145 }
147 err = pciback_do_attach(pdev, gnt_ref, remote_evtchn);
148 if (err)
149 goto out;
151 dev_dbg(&pdev->xdev->dev, "Connecting...\n");
153 err = xenbus_switch_state(pdev->xdev, XenbusStateConnected);
154 if (err)
155 xenbus_dev_fatal(pdev->xdev, err,
156 "Error switching to connected state!");
158 dev_dbg(&pdev->xdev->dev, "Connected? %d\n", err);
159 out:
160 spin_unlock(&pdev->dev_lock);
162 if (magic)
163 kfree(magic);
165 return err;
166 }
168 static void pciback_frontend_changed(struct xenbus_device *xdev,
169 enum xenbus_state fe_state)
170 {
171 struct pciback_device *pdev = xdev->dev.driver_data;
173 dev_dbg(&xdev->dev, "fe state changed %d\n", fe_state);
175 switch (fe_state) {
176 case XenbusStateInitialised:
177 pciback_attach(pdev);
178 break;
180 case XenbusStateClosing:
181 xenbus_switch_state(xdev, XenbusStateClosing);
182 break;
184 case XenbusStateClosed:
185 dev_dbg(&xdev->dev, "frontend is gone! unregister device\n");
186 device_unregister(&xdev->dev);
187 break;
189 default:
190 break;
191 }
192 }
194 static int pciback_publish_pci_root(struct pciback_device *pdev,
195 unsigned int domain, unsigned int bus)
196 {
197 unsigned int d, b;
198 int i, root_num, len, err;
199 char str[64];
201 dev_dbg(&pdev->xdev->dev, "Publishing pci roots\n");
203 err = xenbus_scanf(XBT_NIL, pdev->xdev->nodename,
204 "root_num", "%d", &root_num);
205 if (err == 0 || err == -ENOENT)
206 root_num = 0;
207 else if (err < 0)
208 goto out;
210 /* Verify that we haven't already published this pci root */
211 for (i = 0; i < root_num; i++) {
212 len = snprintf(str, sizeof(str), "root-%d", i);
213 if (unlikely(len >= (sizeof(str) - 1))) {
214 err = -ENOMEM;
215 goto out;
216 }
218 err = xenbus_scanf(XBT_NIL, pdev->xdev->nodename,
219 str, "%x:%x", &d, &b);
220 if (err < 0)
221 goto out;
222 if (err != 2) {
223 err = -EINVAL;
224 goto out;
225 }
227 if (d == domain && b == bus) {
228 err = 0;
229 goto out;
230 }
231 }
233 len = snprintf(str, sizeof(str), "root-%d", root_num);
234 if (unlikely(len >= (sizeof(str) - 1))) {
235 err = -ENOMEM;
236 goto out;
237 }
239 dev_dbg(&pdev->xdev->dev, "writing root %d at %04x:%02x\n",
240 root_num, domain, bus);
242 err = xenbus_printf(XBT_NIL, pdev->xdev->nodename, str,
243 "%04x:%02x", domain, bus);
244 if (err)
245 goto out;
247 err = xenbus_printf(XBT_NIL, pdev->xdev->nodename,
248 "root_num", "%d", (root_num + 1));
250 out:
251 return err;
252 }
254 static int pciback_export_device(struct pciback_device *pdev,
255 int domain, int bus, int slot, int func)
256 {
257 struct pci_dev *dev;
258 int err = 0;
260 dev_dbg(&pdev->xdev->dev, "exporting dom %x bus %x slot %x func %x\n",
261 domain, bus, slot, func);
263 dev = pcistub_get_pci_dev_by_slot(pdev, domain, bus, slot, func);
264 if (!dev) {
265 err = -EINVAL;
266 xenbus_dev_fatal(pdev->xdev, err,
267 "Couldn't locate PCI device "
268 "(%04x:%02x:%02x.%01x)! "
269 "perhaps already in-use?",
270 domain, bus, slot, func);
271 goto out;
272 }
274 err = pciback_add_pci_dev(pdev, dev);
275 if (err)
276 goto out;
278 /* TODO: It'd be nice to export a bridge and have all of its children
279 * get exported with it. This may be best done in xend (which will
280 * have to calculate resource usage anyway) but we probably want to
281 * put something in here to ensure that if a bridge gets given to a
282 * driver domain, that all devices under that bridge are not given
283 * to other driver domains (as he who controls the bridge can disable
284 * it and stop the other devices from working).
285 */
286 out:
287 return err;
288 }
290 static int pciback_setup_backend(struct pciback_device *pdev)
291 {
292 /* Get configuration from xend (if available now) */
293 int domain, bus, slot, func;
294 int err = 0;
295 int i, num_devs;
296 char dev_str[64];
298 spin_lock(&pdev->dev_lock);
300 /* It's possible we could get the call to setup twice, so make sure
301 * we're not already connected.
302 */
303 if (xenbus_read_driver_state(pdev->xdev->nodename) !=
304 XenbusStateInitWait)
305 goto out;
307 dev_dbg(&pdev->xdev->dev, "getting be setup\n");
309 err = xenbus_scanf(XBT_NIL, pdev->xdev->nodename, "num_devs", "%d",
310 &num_devs);
311 if (err != 1) {
312 if (err >= 0)
313 err = -EINVAL;
314 xenbus_dev_fatal(pdev->xdev, err,
315 "Error reading number of devices");
316 goto out;
317 }
319 for (i = 0; i < num_devs; i++) {
320 int l = snprintf(dev_str, sizeof(dev_str), "dev-%d", i);
321 if (unlikely(l >= (sizeof(dev_str) - 1))) {
322 err = -ENOMEM;
323 xenbus_dev_fatal(pdev->xdev, err,
324 "String overflow while reading "
325 "configuration");
326 goto out;
327 }
329 err = xenbus_scanf(XBT_NIL, pdev->xdev->nodename, dev_str,
330 "%x:%x:%x.%x", &domain, &bus, &slot, &func);
331 if (err < 0) {
332 xenbus_dev_fatal(pdev->xdev, err,
333 "Error reading device configuration");
334 goto out;
335 }
336 if (err != 4) {
337 err = -EINVAL;
338 xenbus_dev_fatal(pdev->xdev, err,
339 "Error parsing pci device "
340 "configuration");
341 goto out;
342 }
344 err = pciback_export_device(pdev, domain, bus, slot, func);
345 if (err)
346 goto out;
347 }
349 err = pciback_publish_pci_roots(pdev, pciback_publish_pci_root);
350 if (err) {
351 xenbus_dev_fatal(pdev->xdev, err,
352 "Error while publish PCI root buses "
353 "for frontend");
354 goto out;
355 }
357 err = xenbus_switch_state(pdev->xdev, XenbusStateInitialised);
358 if (err)
359 xenbus_dev_fatal(pdev->xdev, err,
360 "Error switching to initialised state!");
362 out:
363 spin_unlock(&pdev->dev_lock);
365 if (!err)
366 /* see if pcifront is already configured (if not, we'll wait) */
367 pciback_attach(pdev);
369 return err;
370 }
372 static void pciback_be_watch(struct xenbus_watch *watch,
373 const char **vec, unsigned int len)
374 {
375 struct pciback_device *pdev =
376 container_of(watch, struct pciback_device, be_watch);
378 switch (xenbus_read_driver_state(pdev->xdev->nodename)) {
379 case XenbusStateInitWait:
380 pciback_setup_backend(pdev);
381 break;
383 default:
384 break;
385 }
386 }
388 static int pciback_xenbus_probe(struct xenbus_device *dev,
389 const struct xenbus_device_id *id)
390 {
391 int err = 0;
392 struct pciback_device *pdev = alloc_pdev(dev);
394 if (pdev == NULL) {
395 err = -ENOMEM;
396 xenbus_dev_fatal(dev, err,
397 "Error allocating pciback_device struct");
398 goto out;
399 }
401 /* wait for xend to configure us */
402 err = xenbus_switch_state(dev, XenbusStateInitWait);
403 if (err)
404 goto out;
406 /* watch the backend node for backend configuration information */
407 err = xenbus_watch_path(dev, dev->nodename, &pdev->be_watch,
408 pciback_be_watch);
409 if (err)
410 goto out;
411 pdev->be_watching = 1;
413 /* We need to force a call to our callback here in case
414 * xend already configured us!
415 */
416 pciback_be_watch(&pdev->be_watch, NULL, 0);
418 out:
419 return err;
420 }
422 static int pciback_xenbus_remove(struct xenbus_device *dev)
423 {
424 struct pciback_device *pdev = dev->dev.driver_data;
426 if (pdev != NULL)
427 free_pdev(pdev);
429 return 0;
430 }
432 static struct xenbus_device_id xenpci_ids[] = {
433 {"pci"},
434 {{0}},
435 };
437 static struct xenbus_driver xenbus_pciback_driver = {
438 .name = "pciback",
439 .owner = THIS_MODULE,
440 .ids = xenpci_ids,
441 .probe = pciback_xenbus_probe,
442 .remove = pciback_xenbus_remove,
443 .otherend_changed = pciback_frontend_changed,
444 };
446 int __init pciback_xenbus_register(void)
447 {
448 if (!is_running_on_xen())
449 return -ENODEV;
451 return xenbus_register_backend(&xenbus_pciback_driver);
452 }
454 void __exit pciback_xenbus_unregister(void)
455 {
456 xenbus_unregister_driver(&xenbus_pciback_driver);
457 }