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