GNU Linux-libre 4.9.309-gnu1
[releases.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
7 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
8
9 #include <linux/moduleparam.h>
10 #include <linux/init.h>
11 #include <linux/list.h>
12 #include <linux/vmalloc.h>
13 #include <linux/workqueue.h>
14 #include <xen/xenbus.h>
15 #include <xen/events.h>
16 #include <asm/xen/pci.h>
17 #include "pciback.h"
18
19 #define INVALID_EVTCHN_IRQ  (-1)
20
21 static bool __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
47         mutex_init(&pdev->dev_lock);
48
49         pdev->sh_info = NULL;
50         pdev->evtchn_irq = INVALID_EVTCHN_IRQ;
51         pdev->be_watching = 0;
52
53         INIT_WORK(&pdev->op_work, xen_pcibk_do_op);
54
55         if (xen_pcibk_init_devices(pdev)) {
56                 kfree(pdev);
57                 pdev = NULL;
58         }
59
60         dev_set_drvdata(&xdev->dev, pdev);
61
62 out:
63         return pdev;
64 }
65
66 static void xen_pcibk_disconnect(struct xen_pcibk_device *pdev)
67 {
68         mutex_lock(&pdev->dev_lock);
69         /* Ensure the guest can't trigger our handler before removing devices */
70         if (pdev->evtchn_irq != INVALID_EVTCHN_IRQ) {
71                 unbind_from_irqhandler(pdev->evtchn_irq, pdev);
72                 pdev->evtchn_irq = INVALID_EVTCHN_IRQ;
73         }
74
75         /* If the driver domain started an op, make sure we complete it
76          * before releasing the shared memory */
77
78         flush_work(&pdev->op_work);
79
80         if (pdev->sh_info != NULL) {
81                 xenbus_unmap_ring_vfree(pdev->xdev, pdev->sh_info);
82                 pdev->sh_info = NULL;
83         }
84         mutex_unlock(&pdev->dev_lock);
85 }
86
87 static void free_pdev(struct xen_pcibk_device *pdev)
88 {
89         if (pdev->be_watching) {
90                 unregister_xenbus_watch(&pdev->be_watch);
91                 pdev->be_watching = 0;
92         }
93
94         xen_pcibk_disconnect(pdev);
95
96         /* N.B. This calls pcistub_put_pci_dev which does the FLR on all
97          * of the PCIe devices. */
98         xen_pcibk_release_devices(pdev);
99
100         dev_set_drvdata(&pdev->xdev->dev, NULL);
101         pdev->xdev = NULL;
102
103         kfree(pdev);
104 }
105
106 static int xen_pcibk_do_attach(struct xen_pcibk_device *pdev, int gnt_ref,
107                              int remote_evtchn)
108 {
109         int err = 0;
110         void *vaddr;
111
112         dev_dbg(&pdev->xdev->dev,
113                 "Attaching to frontend resources - gnt_ref=%d evtchn=%d\n",
114                 gnt_ref, remote_evtchn);
115
116         err = xenbus_map_ring_valloc(pdev->xdev, &gnt_ref, 1, &vaddr);
117         if (err < 0) {
118                 xenbus_dev_fatal(pdev->xdev, err,
119                                 "Error mapping other domain page in ours.");
120                 goto out;
121         }
122
123         pdev->sh_info = vaddr;
124
125         err = bind_interdomain_evtchn_to_irqhandler_lateeoi(
126                 pdev->xdev->otherend_id, remote_evtchn, xen_pcibk_handle_event,
127                 0, DRV_NAME, pdev);
128         if (err < 0) {
129                 xenbus_dev_fatal(pdev->xdev, err,
130                                  "Error binding event channel to IRQ");
131                 goto out;
132         }
133         pdev->evtchn_irq = err;
134         err = 0;
135
136         dev_dbg(&pdev->xdev->dev, "Attached!\n");
137 out:
138         return err;
139 }
140
141 static int xen_pcibk_attach(struct xen_pcibk_device *pdev)
142 {
143         int err = 0;
144         int gnt_ref, remote_evtchn;
145         char *magic = NULL;
146
147
148         mutex_lock(&pdev->dev_lock);
149         /* Make sure we only do this setup once */
150         if (xenbus_read_driver_state(pdev->xdev->nodename) !=
151             XenbusStateInitialised)
152                 goto out;
153
154         /* Wait for frontend to state that it has published the configuration */
155         if (xenbus_read_driver_state(pdev->xdev->otherend) !=
156             XenbusStateInitialised)
157                 goto out;
158
159         dev_dbg(&pdev->xdev->dev, "Reading frontend config\n");
160
161         err = xenbus_gather(XBT_NIL, pdev->xdev->otherend,
162                             "pci-op-ref", "%u", &gnt_ref,
163                             "event-channel", "%u", &remote_evtchn,
164                             "magic", NULL, &magic, NULL);
165         if (err) {
166                 /* If configuration didn't get read correctly, wait longer */
167                 xenbus_dev_fatal(pdev->xdev, err,
168                                  "Error reading configuration from frontend");
169                 goto out;
170         }
171
172         if (magic == NULL || strcmp(magic, XEN_PCI_MAGIC) != 0) {
173                 xenbus_dev_fatal(pdev->xdev, -EFAULT,
174                                  "version mismatch (%s/%s) with pcifront - "
175                                  "halting " DRV_NAME,
176                                  magic, XEN_PCI_MAGIC);
177                 err = -EFAULT;
178                 goto out;
179         }
180
181         err = xen_pcibk_do_attach(pdev, gnt_ref, remote_evtchn);
182         if (err)
183                 goto out;
184
185         dev_dbg(&pdev->xdev->dev, "Connecting...\n");
186
187         err = xenbus_switch_state(pdev->xdev, XenbusStateConnected);
188         if (err)
189                 xenbus_dev_fatal(pdev->xdev, err,
190                                  "Error switching to connected state!");
191
192         dev_dbg(&pdev->xdev->dev, "Connected? %d\n", err);
193 out:
194         mutex_unlock(&pdev->dev_lock);
195
196         kfree(magic);
197
198         return err;
199 }
200
201 static int xen_pcibk_publish_pci_dev(struct xen_pcibk_device *pdev,
202                                    unsigned int domain, unsigned int bus,
203                                    unsigned int devfn, unsigned int devid)
204 {
205         int err;
206         int len;
207         char str[64];
208
209         len = snprintf(str, sizeof(str), "vdev-%d", devid);
210         if (unlikely(len >= (sizeof(str) - 1))) {
211                 err = -ENOMEM;
212                 goto out;
213         }
214
215         /* Note: The PV protocol uses %02x, don't change it */
216         err = xenbus_printf(XBT_NIL, pdev->xdev->nodename, str,
217                             "%04x:%02x:%02x.%02x", domain, bus,
218                             PCI_SLOT(devfn), PCI_FUNC(devfn));
219
220 out:
221         return err;
222 }
223
224 static int xen_pcibk_export_device(struct xen_pcibk_device *pdev,
225                                  int domain, int bus, int slot, int func,
226                                  int devid)
227 {
228         struct pci_dev *dev;
229         int err = 0;
230
231         dev_dbg(&pdev->xdev->dev, "exporting dom %x bus %x slot %x func %x\n",
232                 domain, bus, slot, func);
233
234         dev = pcistub_get_pci_dev_by_slot(pdev, domain, bus, slot, func);
235         if (!dev) {
236                 err = -EINVAL;
237                 xenbus_dev_fatal(pdev->xdev, err,
238                                  "Couldn't locate PCI device "
239                                  "(%04x:%02x:%02x.%d)! "
240                                  "perhaps already in-use?",
241                                  domain, bus, slot, func);
242                 goto out;
243         }
244
245         err = xen_pcibk_add_pci_dev(pdev, dev, devid,
246                                     xen_pcibk_publish_pci_dev);
247         if (err)
248                 goto out;
249
250         dev_info(&dev->dev, "registering for %d\n", pdev->xdev->otherend_id);
251         if (xen_register_device_domain_owner(dev,
252                                              pdev->xdev->otherend_id) != 0) {
253                 dev_err(&dev->dev, "Stealing ownership from dom%d.\n",
254                         xen_find_device_domain_owner(dev));
255                 xen_unregister_device_domain_owner(dev);
256                 xen_register_device_domain_owner(dev, pdev->xdev->otherend_id);
257         }
258
259         /* TODO: It'd be nice to export a bridge and have all of its children
260          * get exported with it. This may be best done in xend (which will
261          * have to calculate resource usage anyway) but we probably want to
262          * put something in here to ensure that if a bridge gets given to a
263          * driver domain, that all devices under that bridge are not given
264          * to other driver domains (as he who controls the bridge can disable
265          * it and stop the other devices from working).
266          */
267 out:
268         return err;
269 }
270
271 static int xen_pcibk_remove_device(struct xen_pcibk_device *pdev,
272                                  int domain, int bus, int slot, int func)
273 {
274         int err = 0;
275         struct pci_dev *dev;
276
277         dev_dbg(&pdev->xdev->dev, "removing dom %x bus %x slot %x func %x\n",
278                 domain, bus, slot, func);
279
280         dev = xen_pcibk_get_pci_dev(pdev, domain, bus, PCI_DEVFN(slot, func));
281         if (!dev) {
282                 err = -EINVAL;
283                 dev_dbg(&pdev->xdev->dev, "Couldn't locate PCI device "
284                         "(%04x:%02x:%02x.%d)! not owned by this domain\n",
285                         domain, bus, slot, func);
286                 goto out;
287         }
288
289         dev_dbg(&dev->dev, "unregistering for %d\n", pdev->xdev->otherend_id);
290         xen_unregister_device_domain_owner(dev);
291
292         /* N.B. This ends up calling pcistub_put_pci_dev which ends up
293          * doing the FLR. */
294         xen_pcibk_release_pci_dev(pdev, dev, true /* use the lock. */);
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                                  enum xenbus_state state)
362 {
363         int err = 0;
364         int num_devs;
365         int domain, bus, slot, func;
366         int substate;
367         int i, len;
368         char state_str[64];
369         char dev_str[64];
370
371
372         dev_dbg(&pdev->xdev->dev, "Reconfiguring device ...\n");
373
374         mutex_lock(&pdev->dev_lock);
375         if (xenbus_read_driver_state(pdev->xdev->nodename) != state)
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         if (state != XenbusStateReconfiguring)
503                 /* Make sure we only reconfigure once. */
504                 goto out;
505
506         err = xenbus_switch_state(pdev->xdev, XenbusStateReconfigured);
507         if (err) {
508                 xenbus_dev_fatal(pdev->xdev, err,
509                                  "Error switching to reconfigured state!");
510                 goto out;
511         }
512
513 out:
514         mutex_unlock(&pdev->dev_lock);
515         return 0;
516 }
517
518 static void xen_pcibk_frontend_changed(struct xenbus_device *xdev,
519                                      enum xenbus_state fe_state)
520 {
521         struct xen_pcibk_device *pdev = dev_get_drvdata(&xdev->dev);
522
523         dev_dbg(&xdev->dev, "fe state changed %d\n", fe_state);
524
525         switch (fe_state) {
526         case XenbusStateInitialised:
527                 xen_pcibk_attach(pdev);
528                 break;
529
530         case XenbusStateReconfiguring:
531                 xen_pcibk_reconfigure(pdev, XenbusStateReconfiguring);
532                 break;
533
534         case XenbusStateConnected:
535                 /* pcifront switched its state from reconfiguring to connected.
536                  * Then switch to connected state.
537                  */
538                 xenbus_switch_state(xdev, XenbusStateConnected);
539                 break;
540
541         case XenbusStateClosing:
542                 xen_pcibk_disconnect(pdev);
543                 xenbus_switch_state(xdev, XenbusStateClosing);
544                 break;
545
546         case XenbusStateClosed:
547                 xen_pcibk_disconnect(pdev);
548                 xenbus_switch_state(xdev, XenbusStateClosed);
549                 if (xenbus_dev_is_online(xdev))
550                         break;
551                 /* fall through if not online */
552         case XenbusStateUnknown:
553                 dev_dbg(&xdev->dev, "frontend is gone! unregister device\n");
554                 device_unregister(&xdev->dev);
555                 break;
556
557         default:
558                 break;
559         }
560 }
561
562 static int xen_pcibk_setup_backend(struct xen_pcibk_device *pdev)
563 {
564         /* Get configuration from xend (if available now) */
565         int domain, bus, slot, func;
566         int err = 0;
567         int i, num_devs;
568         char dev_str[64];
569         char state_str[64];
570
571         mutex_lock(&pdev->dev_lock);
572         /* It's possible we could get the call to setup twice, so make sure
573          * we're not already connected.
574          */
575         if (xenbus_read_driver_state(pdev->xdev->nodename) !=
576             XenbusStateInitWait)
577                 goto out;
578
579         dev_dbg(&pdev->xdev->dev, "getting be setup\n");
580
581         err = xenbus_scanf(XBT_NIL, pdev->xdev->nodename, "num_devs", "%d",
582                            &num_devs);
583         if (err != 1) {
584                 if (err >= 0)
585                         err = -EINVAL;
586                 xenbus_dev_fatal(pdev->xdev, err,
587                                  "Error reading number of devices");
588                 goto out;
589         }
590
591         for (i = 0; i < num_devs; i++) {
592                 int l = snprintf(dev_str, sizeof(dev_str), "dev-%d", i);
593                 if (unlikely(l >= (sizeof(dev_str) - 1))) {
594                         err = -ENOMEM;
595                         xenbus_dev_fatal(pdev->xdev, err,
596                                          "String overflow while reading "
597                                          "configuration");
598                         goto out;
599                 }
600
601                 err = xenbus_scanf(XBT_NIL, pdev->xdev->nodename, dev_str,
602                                    "%x:%x:%x.%x", &domain, &bus, &slot, &func);
603                 if (err < 0) {
604                         xenbus_dev_fatal(pdev->xdev, err,
605                                          "Error reading device configuration");
606                         goto out;
607                 }
608                 if (err != 4) {
609                         err = -EINVAL;
610                         xenbus_dev_fatal(pdev->xdev, err,
611                                          "Error parsing pci device "
612                                          "configuration");
613                         goto out;
614                 }
615
616                 err = xen_pcibk_export_device(pdev, domain, bus, slot, func, i);
617                 if (err)
618                         goto out;
619
620                 /* Switch substate of this device. */
621                 l = snprintf(state_str, sizeof(state_str), "state-%d", i);
622                 if (unlikely(l >= (sizeof(state_str) - 1))) {
623                         err = -ENOMEM;
624                         xenbus_dev_fatal(pdev->xdev, err,
625                                          "String overflow while reading "
626                                          "configuration");
627                         goto out;
628                 }
629                 err = xenbus_printf(XBT_NIL, pdev->xdev->nodename, state_str,
630                                     "%d", XenbusStateInitialised);
631                 if (err) {
632                         xenbus_dev_fatal(pdev->xdev, err, "Error switching "
633                                          "substate of dev-%d\n", i);
634                         goto out;
635                 }
636         }
637
638         err = xen_pcibk_publish_pci_roots(pdev, xen_pcibk_publish_pci_root);
639         if (err) {
640                 xenbus_dev_fatal(pdev->xdev, err,
641                                  "Error while publish PCI root buses "
642                                  "for frontend");
643                 goto out;
644         }
645
646         err = xenbus_switch_state(pdev->xdev, XenbusStateInitialised);
647         if (err)
648                 xenbus_dev_fatal(pdev->xdev, err,
649                                  "Error switching to initialised state!");
650
651 out:
652         mutex_unlock(&pdev->dev_lock);
653         if (!err)
654                 /* see if pcifront is already configured (if not, we'll wait) */
655                 xen_pcibk_attach(pdev);
656         return err;
657 }
658
659 static void xen_pcibk_be_watch(struct xenbus_watch *watch,
660                              const char **vec, unsigned int len)
661 {
662         struct xen_pcibk_device *pdev =
663             container_of(watch, struct xen_pcibk_device, be_watch);
664
665         switch (xenbus_read_driver_state(pdev->xdev->nodename)) {
666         case XenbusStateInitWait:
667                 xen_pcibk_setup_backend(pdev);
668                 break;
669
670         case XenbusStateInitialised:
671                 /*
672                  * We typically move to Initialised when the first device was
673                  * added. Hence subsequent devices getting added may need
674                  * reconfiguring.
675                  */
676                 xen_pcibk_reconfigure(pdev, XenbusStateInitialised);
677                 break;
678
679         default:
680                 break;
681         }
682 }
683
684 static int xen_pcibk_xenbus_probe(struct xenbus_device *dev,
685                                 const struct xenbus_device_id *id)
686 {
687         int err = 0;
688         struct xen_pcibk_device *pdev = alloc_pdev(dev);
689
690         if (pdev == NULL) {
691                 err = -ENOMEM;
692                 xenbus_dev_fatal(dev, err,
693                                  "Error allocating xen_pcibk_device struct");
694                 goto out;
695         }
696
697         /* wait for xend to configure us */
698         err = xenbus_switch_state(dev, XenbusStateInitWait);
699         if (err)
700                 goto out;
701
702         /* watch the backend node for backend configuration information */
703         err = xenbus_watch_path(dev, dev->nodename, &pdev->be_watch,
704                                 NULL, xen_pcibk_be_watch);
705         if (err)
706                 goto out;
707
708         pdev->be_watching = 1;
709
710         /* We need to force a call to our callback here in case
711          * xend already configured us!
712          */
713         xen_pcibk_be_watch(&pdev->be_watch, NULL, 0);
714
715 out:
716         return err;
717 }
718
719 static int xen_pcibk_xenbus_remove(struct xenbus_device *dev)
720 {
721         struct xen_pcibk_device *pdev = dev_get_drvdata(&dev->dev);
722
723         if (pdev != NULL)
724                 free_pdev(pdev);
725
726         return 0;
727 }
728
729 static const struct xenbus_device_id xen_pcibk_ids[] = {
730         {"pci"},
731         {""},
732 };
733
734 static struct xenbus_driver xen_pcibk_driver = {
735         .name                   = DRV_NAME,
736         .ids                    = xen_pcibk_ids,
737         .probe                  = xen_pcibk_xenbus_probe,
738         .remove                 = xen_pcibk_xenbus_remove,
739         .otherend_changed       = xen_pcibk_frontend_changed,
740 };
741
742 const struct xen_pcibk_backend *__read_mostly xen_pcibk_backend;
743
744 int __init xen_pcibk_xenbus_register(void)
745 {
746         xen_pcibk_backend = &xen_pcibk_vpci_backend;
747         if (passthrough)
748                 xen_pcibk_backend = &xen_pcibk_passthrough_backend;
749         pr_info("backend is %s\n", xen_pcibk_backend->name);
750         return xenbus_register_backend(&xen_pcibk_driver);
751 }
752
753 void __exit xen_pcibk_xenbus_unregister(void)
754 {
755         xenbus_unregister_driver(&xen_pcibk_driver);
756 }