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