This commit was manufactured by cvs2svn to create branch 'vserver'.
[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 XenbusStateClosed:
185                 dev_dbg(&xdev->dev, "frontend is gone! unregister device\n");
186                 device_unregister(&xdev->dev);
187                 break;
188
189         default:
190                 break;
191         }
192 }
193
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];
200
201         dev_dbg(&pdev->xdev->dev, "Publishing pci roots\n");
202
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;
209
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                 }
217
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                 }
226
227                 if (d == domain && b == bus) {
228                         err = 0;
229                         goto out;
230                 }
231         }
232
233         len = snprintf(str, sizeof(str), "root-%d", root_num);
234         if (unlikely(len >= (sizeof(str) - 1))) {
235                 err = -ENOMEM;
236                 goto out;
237         }
238
239         dev_dbg(&pdev->xdev->dev, "writing root %d at %04x:%02x\n",
240                 root_num, domain, bus);
241
242         err = xenbus_printf(XBT_NIL, pdev->xdev->nodename, str,
243                             "%04x:%02x", domain, bus);
244         if (err)
245                 goto out;
246
247         err = xenbus_printf(XBT_NIL, pdev->xdev->nodename,
248                             "root_num", "%d", (root_num + 1));
249
250       out:
251         return err;
252 }
253
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;
259
260         dev_dbg(&pdev->xdev->dev, "exporting dom %x bus %x slot %x func %x\n",
261                 domain, bus, slot, func);
262
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         }
273
274         err = pciback_add_pci_dev(pdev, dev);
275         if (err)
276                 goto out;
277
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 }
289
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];
297
298         spin_lock(&pdev->dev_lock);
299
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;
306
307         dev_dbg(&pdev->xdev->dev, "getting be setup\n");
308
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         }
318
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                 }
328
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                 }
343
344                 err = pciback_export_device(pdev, domain, bus, slot, func);
345                 if (err)
346                         goto out;
347         }
348
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         }
356
357         err = xenbus_switch_state(pdev->xdev, XenbusStateInitialised);
358         if (err)
359                 xenbus_dev_fatal(pdev->xdev, err,
360                                  "Error switching to initialised state!");
361
362       out:
363         spin_unlock(&pdev->dev_lock);
364
365         if (!err)
366                 /* see if pcifront is already configured (if not, we'll wait) */
367                 pciback_attach(pdev);
368
369         return err;
370 }
371
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);
377
378         switch (xenbus_read_driver_state(pdev->xdev->nodename)) {
379         case XenbusStateInitWait:
380                 pciback_setup_backend(pdev);
381                 break;
382
383         default:
384                 break;
385         }
386 }
387
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);
393
394         if (pdev == NULL) {
395                 err = -ENOMEM;
396                 xenbus_dev_fatal(dev, err,
397                                  "Error allocating pciback_device struct");
398                 goto out;
399         }
400
401         /* wait for xend to configure us */
402         err = xenbus_switch_state(dev, XenbusStateInitWait);
403         if (err)
404                 goto out;
405
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;
412
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);
417
418       out:
419         return err;
420 }
421
422 static int pciback_xenbus_remove(struct xenbus_device *dev)
423 {
424         struct pciback_device *pdev = dev->dev.driver_data;
425
426         if (pdev != NULL)
427                 free_pdev(pdev);
428
429         return 0;
430 }
431
432 static struct xenbus_device_id xenpci_ids[] = {
433         {"pci"},
434         {{0}},
435 };
436
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 };
445
446 int __init pciback_xenbus_register(void)
447 {
448         if (!is_running_on_xen())
449                 return -ENODEV;
450
451         return xenbus_register_backend(&xenbus_pciback_driver);
452 }
453
454 void __exit pciback_xenbus_unregister(void)
455 {
456         xenbus_unregister_driver(&xenbus_pciback_driver);
457 }