GNU Linux-libre 4.4.288-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/module.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 struct workqueue_struct *xen_pcibk_wq;
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         /* Note, the workqueue does not use spinlocks at all.*/
80         flush_workqueue(xen_pcibk_wq);
81
82         if (pdev->sh_info != NULL) {
83                 xenbus_unmap_ring_vfree(pdev->xdev, pdev->sh_info);
84                 pdev->sh_info = NULL;
85         }
86         mutex_unlock(&pdev->dev_lock);
87 }
88
89 static void free_pdev(struct xen_pcibk_device *pdev)
90 {
91         if (pdev->be_watching) {
92                 unregister_xenbus_watch(&pdev->be_watch);
93                 pdev->be_watching = 0;
94         }
95
96         xen_pcibk_disconnect(pdev);
97
98         /* N.B. This calls pcistub_put_pci_dev which does the FLR on all
99          * of the PCIe devices. */
100         xen_pcibk_release_devices(pdev);
101
102         dev_set_drvdata(&pdev->xdev->dev, NULL);
103         pdev->xdev = NULL;
104
105         kfree(pdev);
106 }
107
108 static int xen_pcibk_do_attach(struct xen_pcibk_device *pdev, int gnt_ref,
109                              int remote_evtchn)
110 {
111         int err = 0;
112         void *vaddr;
113
114         dev_dbg(&pdev->xdev->dev,
115                 "Attaching to frontend resources - gnt_ref=%d evtchn=%d\n",
116                 gnt_ref, remote_evtchn);
117
118         err = xenbus_map_ring_valloc(pdev->xdev, &gnt_ref, 1, &vaddr);
119         if (err < 0) {
120                 xenbus_dev_fatal(pdev->xdev, err,
121                                 "Error mapping other domain page in ours.");
122                 goto out;
123         }
124
125         pdev->sh_info = vaddr;
126
127         err = bind_interdomain_evtchn_to_irqhandler_lateeoi(
128                 pdev->xdev->otherend_id, remote_evtchn, xen_pcibk_handle_event,
129                 0, DRV_NAME, pdev);
130         if (err < 0) {
131                 xenbus_dev_fatal(pdev->xdev, err,
132                                  "Error binding event channel to IRQ");
133                 goto out;
134         }
135         pdev->evtchn_irq = err;
136         err = 0;
137
138         dev_dbg(&pdev->xdev->dev, "Attached!\n");
139 out:
140         return err;
141 }
142
143 static int xen_pcibk_attach(struct xen_pcibk_device *pdev)
144 {
145         int err = 0;
146         int gnt_ref, remote_evtchn;
147         char *magic = NULL;
148
149
150         mutex_lock(&pdev->dev_lock);
151         /* Make sure we only do this setup once */
152         if (xenbus_read_driver_state(pdev->xdev->nodename) !=
153             XenbusStateInitialised)
154                 goto out;
155
156         /* Wait for frontend to state that it has published the configuration */
157         if (xenbus_read_driver_state(pdev->xdev->otherend) !=
158             XenbusStateInitialised)
159                 goto out;
160
161         dev_dbg(&pdev->xdev->dev, "Reading frontend config\n");
162
163         err = xenbus_gather(XBT_NIL, pdev->xdev->otherend,
164                             "pci-op-ref", "%u", &gnt_ref,
165                             "event-channel", "%u", &remote_evtchn,
166                             "magic", NULL, &magic, NULL);
167         if (err) {
168                 /* If configuration didn't get read correctly, wait longer */
169                 xenbus_dev_fatal(pdev->xdev, err,
170                                  "Error reading configuration from frontend");
171                 goto out;
172         }
173
174         if (magic == NULL || strcmp(magic, XEN_PCI_MAGIC) != 0) {
175                 xenbus_dev_fatal(pdev->xdev, -EFAULT,
176                                  "version mismatch (%s/%s) with pcifront - "
177                                  "halting " DRV_NAME,
178                                  magic, XEN_PCI_MAGIC);
179                 err = -EFAULT;
180                 goto out;
181         }
182
183         err = xen_pcibk_do_attach(pdev, gnt_ref, remote_evtchn);
184         if (err)
185                 goto out;
186
187         dev_dbg(&pdev->xdev->dev, "Connecting...\n");
188
189         err = xenbus_switch_state(pdev->xdev, XenbusStateConnected);
190         if (err)
191                 xenbus_dev_fatal(pdev->xdev, err,
192                                  "Error switching to connected state!");
193
194         dev_dbg(&pdev->xdev->dev, "Connected? %d\n", err);
195 out:
196         mutex_unlock(&pdev->dev_lock);
197
198         kfree(magic);
199
200         return err;
201 }
202
203 static int xen_pcibk_publish_pci_dev(struct xen_pcibk_device *pdev,
204                                    unsigned int domain, unsigned int bus,
205                                    unsigned int devfn, unsigned int devid)
206 {
207         int err;
208         int len;
209         char str[64];
210
211         len = snprintf(str, sizeof(str), "vdev-%d", devid);
212         if (unlikely(len >= (sizeof(str) - 1))) {
213                 err = -ENOMEM;
214                 goto out;
215         }
216
217         /* Note: The PV protocol uses %02x, don't change it */
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.%d)! "
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_info(&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, "Stealing ownership from dom%d.\n",
256                         xen_find_device_domain_owner(dev));
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.%d)! 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         /* N.B. This ends up calling pcistub_put_pci_dev which ends up
295          * doing the FLR. */
296         xen_pcibk_release_pci_dev(pdev, dev, true /* use the lock. */);
297
298 out:
299         return err;
300 }
301
302 static int xen_pcibk_publish_pci_root(struct xen_pcibk_device *pdev,
303                                     unsigned int domain, unsigned int bus)
304 {
305         unsigned int d, b;
306         int i, root_num, len, err;
307         char str[64];
308
309         dev_dbg(&pdev->xdev->dev, "Publishing pci roots\n");
310
311         err = xenbus_scanf(XBT_NIL, pdev->xdev->nodename,
312                            "root_num", "%d", &root_num);
313         if (err == 0 || err == -ENOENT)
314                 root_num = 0;
315         else if (err < 0)
316                 goto out;
317
318         /* Verify that we haven't already published this pci root */
319         for (i = 0; i < root_num; i++) {
320                 len = snprintf(str, sizeof(str), "root-%d", i);
321                 if (unlikely(len >= (sizeof(str) - 1))) {
322                         err = -ENOMEM;
323                         goto out;
324                 }
325
326                 err = xenbus_scanf(XBT_NIL, pdev->xdev->nodename,
327                                    str, "%x:%x", &d, &b);
328                 if (err < 0)
329                         goto out;
330                 if (err != 2) {
331                         err = -EINVAL;
332                         goto out;
333                 }
334
335                 if (d == domain && b == bus) {
336                         err = 0;
337                         goto out;
338                 }
339         }
340
341         len = snprintf(str, sizeof(str), "root-%d", root_num);
342         if (unlikely(len >= (sizeof(str) - 1))) {
343                 err = -ENOMEM;
344                 goto out;
345         }
346
347         dev_dbg(&pdev->xdev->dev, "writing root %d at %04x:%02x\n",
348                 root_num, domain, bus);
349
350         err = xenbus_printf(XBT_NIL, pdev->xdev->nodename, str,
351                             "%04x:%02x", domain, bus);
352         if (err)
353                 goto out;
354
355         err = xenbus_printf(XBT_NIL, pdev->xdev->nodename,
356                             "root_num", "%d", (root_num + 1));
357
358 out:
359         return err;
360 }
361
362 static int xen_pcibk_reconfigure(struct xen_pcibk_device *pdev,
363                                  enum xenbus_state state)
364 {
365         int err = 0;
366         int num_devs;
367         int domain, bus, slot, func;
368         int substate;
369         int i, len;
370         char state_str[64];
371         char dev_str[64];
372
373
374         dev_dbg(&pdev->xdev->dev, "Reconfiguring device ...\n");
375
376         mutex_lock(&pdev->dev_lock);
377         if (xenbus_read_driver_state(pdev->xdev->nodename) != state)
378                 goto out;
379
380         err = xenbus_scanf(XBT_NIL, pdev->xdev->nodename, "num_devs", "%d",
381                            &num_devs);
382         if (err != 1) {
383                 if (err >= 0)
384                         err = -EINVAL;
385                 xenbus_dev_fatal(pdev->xdev, err,
386                                  "Error reading number of devices");
387                 goto out;
388         }
389
390         for (i = 0; i < num_devs; i++) {
391                 len = snprintf(state_str, sizeof(state_str), "state-%d", i);
392                 if (unlikely(len >= (sizeof(state_str) - 1))) {
393                         err = -ENOMEM;
394                         xenbus_dev_fatal(pdev->xdev, err,
395                                          "String overflow while reading "
396                                          "configuration");
397                         goto out;
398                 }
399                 err = xenbus_scanf(XBT_NIL, pdev->xdev->nodename, state_str,
400                                    "%d", &substate);
401                 if (err != 1)
402                         substate = XenbusStateUnknown;
403
404                 switch (substate) {
405                 case XenbusStateInitialising:
406                         dev_dbg(&pdev->xdev->dev, "Attaching dev-%d ...\n", i);
407
408                         len = snprintf(dev_str, sizeof(dev_str), "dev-%d", i);
409                         if (unlikely(len >= (sizeof(dev_str) - 1))) {
410                                 err = -ENOMEM;
411                                 xenbus_dev_fatal(pdev->xdev, err,
412                                                  "String overflow while "
413                                                  "reading configuration");
414                                 goto out;
415                         }
416                         err = xenbus_scanf(XBT_NIL, pdev->xdev->nodename,
417                                            dev_str, "%x:%x:%x.%x",
418                                            &domain, &bus, &slot, &func);
419                         if (err < 0) {
420                                 xenbus_dev_fatal(pdev->xdev, err,
421                                                  "Error reading device "
422                                                  "configuration");
423                                 goto out;
424                         }
425                         if (err != 4) {
426                                 err = -EINVAL;
427                                 xenbus_dev_fatal(pdev->xdev, err,
428                                                  "Error parsing pci device "
429                                                  "configuration");
430                                 goto out;
431                         }
432
433                         err = xen_pcibk_export_device(pdev, domain, bus, slot,
434                                                     func, i);
435                         if (err)
436                                 goto out;
437
438                         /* Publish pci roots. */
439                         err = xen_pcibk_publish_pci_roots(pdev,
440                                                 xen_pcibk_publish_pci_root);
441                         if (err) {
442                                 xenbus_dev_fatal(pdev->xdev, err,
443                                                  "Error while publish PCI root"
444                                                  "buses for frontend");
445                                 goto out;
446                         }
447
448                         err = xenbus_printf(XBT_NIL, pdev->xdev->nodename,
449                                             state_str, "%d",
450                                             XenbusStateInitialised);
451                         if (err) {
452                                 xenbus_dev_fatal(pdev->xdev, err,
453                                                  "Error switching substate of "
454                                                  "dev-%d\n", i);
455                                 goto out;
456                         }
457                         break;
458
459                 case XenbusStateClosing:
460                         dev_dbg(&pdev->xdev->dev, "Detaching dev-%d ...\n", i);
461
462                         len = snprintf(dev_str, sizeof(dev_str), "vdev-%d", i);
463                         if (unlikely(len >= (sizeof(dev_str) - 1))) {
464                                 err = -ENOMEM;
465                                 xenbus_dev_fatal(pdev->xdev, err,
466                                                  "String overflow while "
467                                                  "reading configuration");
468                                 goto out;
469                         }
470                         err = xenbus_scanf(XBT_NIL, pdev->xdev->nodename,
471                                            dev_str, "%x:%x:%x.%x",
472                                            &domain, &bus, &slot, &func);
473                         if (err < 0) {
474                                 xenbus_dev_fatal(pdev->xdev, err,
475                                                  "Error reading device "
476                                                  "configuration");
477                                 goto out;
478                         }
479                         if (err != 4) {
480                                 err = -EINVAL;
481                                 xenbus_dev_fatal(pdev->xdev, err,
482                                                  "Error parsing pci device "
483                                                  "configuration");
484                                 goto out;
485                         }
486
487                         err = xen_pcibk_remove_device(pdev, domain, bus, slot,
488                                                     func);
489                         if (err)
490                                 goto out;
491
492                         /* TODO: If at some point we implement support for pci
493                          * root hot-remove on pcifront side, we'll need to
494                          * remove unnecessary xenstore nodes of pci roots here.
495                          */
496
497                         break;
498
499                 default:
500                         break;
501                 }
502         }
503
504         if (state != XenbusStateReconfiguring)
505                 /* Make sure we only reconfigure once. */
506                 goto out;
507
508         err = xenbus_switch_state(pdev->xdev, XenbusStateReconfigured);
509         if (err) {
510                 xenbus_dev_fatal(pdev->xdev, err,
511                                  "Error switching to reconfigured state!");
512                 goto out;
513         }
514
515 out:
516         mutex_unlock(&pdev->dev_lock);
517         return 0;
518 }
519
520 static void xen_pcibk_frontend_changed(struct xenbus_device *xdev,
521                                      enum xenbus_state fe_state)
522 {
523         struct xen_pcibk_device *pdev = dev_get_drvdata(&xdev->dev);
524
525         dev_dbg(&xdev->dev, "fe state changed %d\n", fe_state);
526
527         switch (fe_state) {
528         case XenbusStateInitialised:
529                 xen_pcibk_attach(pdev);
530                 break;
531
532         case XenbusStateReconfiguring:
533                 xen_pcibk_reconfigure(pdev, XenbusStateReconfiguring);
534                 break;
535
536         case XenbusStateConnected:
537                 /* pcifront switched its state from reconfiguring to connected.
538                  * Then switch to connected state.
539                  */
540                 xenbus_switch_state(xdev, XenbusStateConnected);
541                 break;
542
543         case XenbusStateClosing:
544                 xen_pcibk_disconnect(pdev);
545                 xenbus_switch_state(xdev, XenbusStateClosing);
546                 break;
547
548         case XenbusStateClosed:
549                 xen_pcibk_disconnect(pdev);
550                 xenbus_switch_state(xdev, XenbusStateClosed);
551                 if (xenbus_dev_is_online(xdev))
552                         break;
553                 /* fall through if not online */
554         case XenbusStateUnknown:
555                 dev_dbg(&xdev->dev, "frontend is gone! unregister device\n");
556                 device_unregister(&xdev->dev);
557                 break;
558
559         default:
560                 break;
561         }
562 }
563
564 static int xen_pcibk_setup_backend(struct xen_pcibk_device *pdev)
565 {
566         /* Get configuration from xend (if available now) */
567         int domain, bus, slot, func;
568         int err = 0;
569         int i, num_devs;
570         char dev_str[64];
571         char state_str[64];
572
573         mutex_lock(&pdev->dev_lock);
574         /* It's possible we could get the call to setup twice, so make sure
575          * we're not already connected.
576          */
577         if (xenbus_read_driver_state(pdev->xdev->nodename) !=
578             XenbusStateInitWait)
579                 goto out;
580
581         dev_dbg(&pdev->xdev->dev, "getting be setup\n");
582
583         err = xenbus_scanf(XBT_NIL, pdev->xdev->nodename, "num_devs", "%d",
584                            &num_devs);
585         if (err != 1) {
586                 if (err >= 0)
587                         err = -EINVAL;
588                 xenbus_dev_fatal(pdev->xdev, err,
589                                  "Error reading number of devices");
590                 goto out;
591         }
592
593         for (i = 0; i < num_devs; i++) {
594                 int l = snprintf(dev_str, sizeof(dev_str), "dev-%d", i);
595                 if (unlikely(l >= (sizeof(dev_str) - 1))) {
596                         err = -ENOMEM;
597                         xenbus_dev_fatal(pdev->xdev, err,
598                                          "String overflow while reading "
599                                          "configuration");
600                         goto out;
601                 }
602
603                 err = xenbus_scanf(XBT_NIL, pdev->xdev->nodename, dev_str,
604                                    "%x:%x:%x.%x", &domain, &bus, &slot, &func);
605                 if (err < 0) {
606                         xenbus_dev_fatal(pdev->xdev, err,
607                                          "Error reading device configuration");
608                         goto out;
609                 }
610                 if (err != 4) {
611                         err = -EINVAL;
612                         xenbus_dev_fatal(pdev->xdev, err,
613                                          "Error parsing pci device "
614                                          "configuration");
615                         goto out;
616                 }
617
618                 err = xen_pcibk_export_device(pdev, domain, bus, slot, func, i);
619                 if (err)
620                         goto out;
621
622                 /* Switch substate of this device. */
623                 l = snprintf(state_str, sizeof(state_str), "state-%d", i);
624                 if (unlikely(l >= (sizeof(state_str) - 1))) {
625                         err = -ENOMEM;
626                         xenbus_dev_fatal(pdev->xdev, err,
627                                          "String overflow while reading "
628                                          "configuration");
629                         goto out;
630                 }
631                 err = xenbus_printf(XBT_NIL, pdev->xdev->nodename, state_str,
632                                     "%d", XenbusStateInitialised);
633                 if (err) {
634                         xenbus_dev_fatal(pdev->xdev, err, "Error switching "
635                                          "substate of dev-%d\n", i);
636                         goto out;
637                 }
638         }
639
640         err = xen_pcibk_publish_pci_roots(pdev, xen_pcibk_publish_pci_root);
641         if (err) {
642                 xenbus_dev_fatal(pdev->xdev, err,
643                                  "Error while publish PCI root buses "
644                                  "for frontend");
645                 goto out;
646         }
647
648         err = xenbus_switch_state(pdev->xdev, XenbusStateInitialised);
649         if (err)
650                 xenbus_dev_fatal(pdev->xdev, err,
651                                  "Error switching to initialised state!");
652
653 out:
654         mutex_unlock(&pdev->dev_lock);
655         if (!err)
656                 /* see if pcifront is already configured (if not, we'll wait) */
657                 xen_pcibk_attach(pdev);
658         return err;
659 }
660
661 static void xen_pcibk_be_watch(struct xenbus_watch *watch,
662                              const char **vec, unsigned int len)
663 {
664         struct xen_pcibk_device *pdev =
665             container_of(watch, struct xen_pcibk_device, be_watch);
666
667         switch (xenbus_read_driver_state(pdev->xdev->nodename)) {
668         case XenbusStateInitWait:
669                 xen_pcibk_setup_backend(pdev);
670                 break;
671
672         case XenbusStateInitialised:
673                 /*
674                  * We typically move to Initialised when the first device was
675                  * added. Hence subsequent devices getting added may need
676                  * reconfiguring.
677                  */
678                 xen_pcibk_reconfigure(pdev, XenbusStateInitialised);
679                 break;
680
681         default:
682                 break;
683         }
684 }
685
686 static int xen_pcibk_xenbus_probe(struct xenbus_device *dev,
687                                 const struct xenbus_device_id *id)
688 {
689         int err = 0;
690         struct xen_pcibk_device *pdev = alloc_pdev(dev);
691
692         if (pdev == NULL) {
693                 err = -ENOMEM;
694                 xenbus_dev_fatal(dev, err,
695                                  "Error allocating xen_pcibk_device struct");
696                 goto out;
697         }
698
699         /* wait for xend to configure us */
700         err = xenbus_switch_state(dev, XenbusStateInitWait);
701         if (err)
702                 goto out;
703
704         /* watch the backend node for backend configuration information */
705         err = xenbus_watch_path(dev, dev->nodename, &pdev->be_watch,
706                                 NULL, xen_pcibk_be_watch);
707         if (err)
708                 goto out;
709
710         pdev->be_watching = 1;
711
712         /* We need to force a call to our callback here in case
713          * xend already configured us!
714          */
715         xen_pcibk_be_watch(&pdev->be_watch, NULL, 0);
716
717 out:
718         return err;
719 }
720
721 static int xen_pcibk_xenbus_remove(struct xenbus_device *dev)
722 {
723         struct xen_pcibk_device *pdev = dev_get_drvdata(&dev->dev);
724
725         if (pdev != NULL)
726                 free_pdev(pdev);
727
728         return 0;
729 }
730
731 static const struct xenbus_device_id xen_pcibk_ids[] = {
732         {"pci"},
733         {""},
734 };
735
736 static struct xenbus_driver xen_pcibk_driver = {
737         .name                   = DRV_NAME,
738         .ids                    = xen_pcibk_ids,
739         .probe                  = xen_pcibk_xenbus_probe,
740         .remove                 = xen_pcibk_xenbus_remove,
741         .otherend_changed       = xen_pcibk_frontend_changed,
742 };
743
744 const struct xen_pcibk_backend *__read_mostly xen_pcibk_backend;
745
746 int __init xen_pcibk_xenbus_register(void)
747 {
748         xen_pcibk_wq = create_workqueue("xen_pciback_workqueue");
749         if (!xen_pcibk_wq) {
750                 pr_err("%s: create xen_pciback_workqueue failed\n", __func__);
751                 return -EFAULT;
752         }
753         xen_pcibk_backend = &xen_pcibk_vpci_backend;
754         if (passthrough)
755                 xen_pcibk_backend = &xen_pcibk_passthrough_backend;
756         pr_info("backend is %s\n", xen_pcibk_backend->name);
757         return xenbus_register_backend(&xen_pcibk_driver);
758 }
759
760 void __exit xen_pcibk_xenbus_unregister(void)
761 {
762         destroy_workqueue(xen_pcibk_wq);
763         xenbus_unregister_driver(&xen_pcibk_driver);
764 }