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