Merge to Fedora kernel-2.6.18-1.2224_FC5 patched with stable patch-2.6.18.1-vs2.0...
[linux-2.6.git] / drivers / xen / pciback / xenbus.c
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"
13
14 #define INVALID_EVTCHN_IRQ  (-1)
15
16 static struct pciback_device *alloc_pdev(struct xenbus_device *xdev)
17 {
18         struct pciback_device *pdev;
19
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);
24
25         pdev->xdev = xdev;
26         xdev->dev.driver_data = pdev;
27
28         spin_lock_init(&pdev->dev_lock);
29
30         pdev->sh_area = NULL;
31         pdev->sh_info = NULL;
32         pdev->evtchn_irq = INVALID_EVTCHN_IRQ;
33         pdev->be_watching = 0;
34
35         INIT_WORK(&pdev->op_work, pciback_do_op, pdev);
36
37         if (pciback_init_devices(pdev)) {
38                 kfree(pdev);
39                 pdev = NULL;
40         }
41       out:
42         return pdev;
43 }
44
45 static void free_pdev(struct pciback_device *pdev)
46 {
47         if (pdev->be_watching)
48                 unregister_xenbus_watch(&pdev->be_watch);
49
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);
53
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();
58
59         if (pdev->sh_info)
60                 xenbus_unmap_ring_vfree(pdev->xdev, pdev->sh_area);
61
62         pciback_release_devices(pdev);
63
64         pdev->xdev->dev.driver_data = NULL;
65         pdev->xdev = NULL;
66
67         kfree(pdev);
68 }
69
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;
76
77         dev_dbg(&pdev->xdev->dev,
78                 "Attaching to frontend resources - gnt_ref=%d evtchn=%d\n",
79                 gnt_ref, remote_evtchn);
80
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;
88
89         err = xenbus_bind_evtchn(pdev->xdev, remote_evtchn, &evtchn);
90         if (err)
91                 goto out;
92
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;
102
103         dev_dbg(&pdev->xdev->dev, "Attached!\n");
104       out:
105         return err;
106 }
107
108 static int pciback_attach(struct pciback_device *pdev)
109 {
110         int err = 0;
111         int gnt_ref, remote_evtchn;
112         char *magic = NULL;
113
114         spin_lock(&pdev->dev_lock);
115
116         /* Make sure we only do this setup once */
117         if (xenbus_read_driver_state(pdev->xdev->nodename) !=
118             XenbusStateInitialised)
119                 goto out;
120
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;
125
126         dev_dbg(&pdev->xdev->dev, "Reading frontend config\n");
127
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         }
138
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         }
146
147         err = pciback_do_attach(pdev, gnt_ref, remote_evtchn);
148         if (err)
149                 goto out;
150
151         dev_dbg(&pdev->xdev->dev, "Connecting...\n");
152
153         err = xenbus_switch_state(pdev->xdev, XenbusStateConnected);
154         if (err)
155                 xenbus_dev_fatal(pdev->xdev, err,
156                                  "Error switching to connected state!");
157
158         dev_dbg(&pdev->xdev->dev, "Connected? %d\n", err);
159       out:
160         spin_unlock(&pdev->dev_lock);
161
162         if (magic)
163                 kfree(magic);
164
165         return err;
166 }
167
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;
172
173         dev_dbg(&xdev->dev, "fe state changed %d\n", fe_state);
174
175         switch (fe_state) {
176         case XenbusStateInitialised:
177                 pciback_attach(pdev);
178                 break;
179
180         case XenbusStateClosing:
181                 xenbus_switch_state(xdev, XenbusStateClosing);
182                 break;
183
184         case XenbusStateUnknown:
185         case XenbusStateClosed:
186                 dev_dbg(&xdev->dev, "frontend is gone! unregister device\n");
187                 device_unregister(&xdev->dev);
188                 break;
189
190         default:
191                 break;
192         }
193 }
194
195 static int pciback_publish_pci_root(struct pciback_device *pdev,
196                                     unsigned int domain, unsigned int bus)
197 {
198         unsigned int d, b;
199         int i, root_num, len, err;
200         char str[64];
201
202         dev_dbg(&pdev->xdev->dev, "Publishing pci roots\n");
203
204         err = xenbus_scanf(XBT_NIL, pdev->xdev->nodename,
205                            "root_num", "%d", &root_num);
206         if (err == 0 || err == -ENOENT)
207                 root_num = 0;
208         else if (err < 0)
209                 goto out;
210
211         /* Verify that we haven't already published this pci root */
212         for (i = 0; i < root_num; i++) {
213                 len = snprintf(str, sizeof(str), "root-%d", i);
214                 if (unlikely(len >= (sizeof(str) - 1))) {
215                         err = -ENOMEM;
216                         goto out;
217                 }
218
219                 err = xenbus_scanf(XBT_NIL, pdev->xdev->nodename,
220                                    str, "%x:%x", &d, &b);
221                 if (err < 0)
222                         goto out;
223                 if (err != 2) {
224                         err = -EINVAL;
225                         goto out;
226                 }
227
228                 if (d == domain && b == bus) {
229                         err = 0;
230                         goto out;
231                 }
232         }
233
234         len = snprintf(str, sizeof(str), "root-%d", root_num);
235         if (unlikely(len >= (sizeof(str) - 1))) {
236                 err = -ENOMEM;
237                 goto out;
238         }
239
240         dev_dbg(&pdev->xdev->dev, "writing root %d at %04x:%02x\n",
241                 root_num, domain, bus);
242
243         err = xenbus_printf(XBT_NIL, pdev->xdev->nodename, str,
244                             "%04x:%02x", domain, bus);
245         if (err)
246                 goto out;
247
248         err = xenbus_printf(XBT_NIL, pdev->xdev->nodename,
249                             "root_num", "%d", (root_num + 1));
250
251       out:
252         return err;
253 }
254
255 static int pciback_export_device(struct pciback_device *pdev,
256                                  int domain, int bus, int slot, int func)
257 {
258         struct pci_dev *dev;
259         int err = 0;
260
261         dev_dbg(&pdev->xdev->dev, "exporting dom %x bus %x slot %x func %x\n",
262                 domain, bus, slot, func);
263
264         dev = pcistub_get_pci_dev_by_slot(pdev, domain, bus, slot, func);
265         if (!dev) {
266                 err = -EINVAL;
267                 xenbus_dev_fatal(pdev->xdev, err,
268                                  "Couldn't locate PCI device "
269                                  "(%04x:%02x:%02x.%01x)! "
270                                  "perhaps already in-use?",
271                                  domain, bus, slot, func);
272                 goto out;
273         }
274
275         err = pciback_add_pci_dev(pdev, dev);
276         if (err)
277                 goto out;
278
279         /* TODO: It'd be nice to export a bridge and have all of its children
280          * get exported with it. This may be best done in xend (which will
281          * have to calculate resource usage anyway) but we probably want to
282          * put something in here to ensure that if a bridge gets given to a
283          * driver domain, that all devices under that bridge are not given
284          * to other driver domains (as he who controls the bridge can disable
285          * it and stop the other devices from working).
286          */
287       out:
288         return err;
289 }
290
291 static int pciback_setup_backend(struct pciback_device *pdev)
292 {
293         /* Get configuration from xend (if available now) */
294         int domain, bus, slot, func;
295         int err = 0;
296         int i, num_devs;
297         char dev_str[64];
298
299         spin_lock(&pdev->dev_lock);
300
301         /* It's possible we could get the call to setup twice, so make sure
302          * we're not already connected.
303          */
304         if (xenbus_read_driver_state(pdev->xdev->nodename) !=
305             XenbusStateInitWait)
306                 goto out;
307
308         dev_dbg(&pdev->xdev->dev, "getting be setup\n");
309
310         err = xenbus_scanf(XBT_NIL, pdev->xdev->nodename, "num_devs", "%d",
311                            &num_devs);
312         if (err != 1) {
313                 if (err >= 0)
314                         err = -EINVAL;
315                 xenbus_dev_fatal(pdev->xdev, err,
316                                  "Error reading number of devices");
317                 goto out;
318         }
319
320         for (i = 0; i < num_devs; i++) {
321                 int l = snprintf(dev_str, sizeof(dev_str), "dev-%d", i);
322                 if (unlikely(l >= (sizeof(dev_str) - 1))) {
323                         err = -ENOMEM;
324                         xenbus_dev_fatal(pdev->xdev, err,
325                                          "String overflow while reading "
326                                          "configuration");
327                         goto out;
328                 }
329
330                 err = xenbus_scanf(XBT_NIL, pdev->xdev->nodename, dev_str,
331                                    "%x:%x:%x.%x", &domain, &bus, &slot, &func);
332                 if (err < 0) {
333                         xenbus_dev_fatal(pdev->xdev, err,
334                                          "Error reading device configuration");
335                         goto out;
336                 }
337                 if (err != 4) {
338                         err = -EINVAL;
339                         xenbus_dev_fatal(pdev->xdev, err,
340                                          "Error parsing pci device "
341                                          "configuration");
342                         goto out;
343                 }
344
345                 err = pciback_export_device(pdev, domain, bus, slot, func);
346                 if (err)
347                         goto out;
348         }
349
350         err = pciback_publish_pci_roots(pdev, pciback_publish_pci_root);
351         if (err) {
352                 xenbus_dev_fatal(pdev->xdev, err,
353                                  "Error while publish PCI root buses "
354                                  "for frontend");
355                 goto out;
356         }
357
358         err = xenbus_switch_state(pdev->xdev, XenbusStateInitialised);
359         if (err)
360                 xenbus_dev_fatal(pdev->xdev, err,
361                                  "Error switching to initialised state!");
362
363       out:
364         spin_unlock(&pdev->dev_lock);
365
366         if (!err)
367                 /* see if pcifront is already configured (if not, we'll wait) */
368                 pciback_attach(pdev);
369
370         return err;
371 }
372
373 static void pciback_be_watch(struct xenbus_watch *watch,
374                              const char **vec, unsigned int len)
375 {
376         struct pciback_device *pdev =
377             container_of(watch, struct pciback_device, be_watch);
378
379         switch (xenbus_read_driver_state(pdev->xdev->nodename)) {
380         case XenbusStateInitWait:
381                 pciback_setup_backend(pdev);
382                 break;
383
384         default:
385                 break;
386         }
387 }
388
389 static int pciback_xenbus_probe(struct xenbus_device *dev,
390                                 const struct xenbus_device_id *id)
391 {
392         int err = 0;
393         struct pciback_device *pdev = alloc_pdev(dev);
394
395         if (pdev == NULL) {
396                 err = -ENOMEM;
397                 xenbus_dev_fatal(dev, err,
398                                  "Error allocating pciback_device struct");
399                 goto out;
400         }
401
402         /* wait for xend to configure us */
403         err = xenbus_switch_state(dev, XenbusStateInitWait);
404         if (err)
405                 goto out;
406
407         /* watch the backend node for backend configuration information */
408         err = xenbus_watch_path(dev, dev->nodename, &pdev->be_watch,
409                                 pciback_be_watch);
410         if (err)
411                 goto out;
412         pdev->be_watching = 1;
413
414         /* We need to force a call to our callback here in case
415          * xend already configured us!
416          */
417         pciback_be_watch(&pdev->be_watch, NULL, 0);
418
419       out:
420         return err;
421 }
422
423 static int pciback_xenbus_remove(struct xenbus_device *dev)
424 {
425         struct pciback_device *pdev = dev->dev.driver_data;
426
427         if (pdev != NULL)
428                 free_pdev(pdev);
429
430         return 0;
431 }
432
433 static struct xenbus_device_id xenpci_ids[] = {
434         {"pci"},
435         {{0}},
436 };
437
438 static struct xenbus_driver xenbus_pciback_driver = {
439         .name                   = "pciback",
440         .owner                  = THIS_MODULE,
441         .ids                    = xenpci_ids,
442         .probe                  = pciback_xenbus_probe,
443         .remove                 = pciback_xenbus_remove,
444         .otherend_changed       = pciback_frontend_changed,
445 };
446
447 int __init pciback_xenbus_register(void)
448 {
449         if (!is_running_on_xen())
450                 return -ENODEV;
451
452         return xenbus_register_backend(&xenbus_pciback_driver);
453 }
454
455 void __exit pciback_xenbus_unregister(void)
456 {
457         xenbus_unregister_driver(&xenbus_pciback_driver);
458 }