Merge git://git.kernel.org/pub/scm/virt/kvm/kvm
[sfrench/cifs-2.6.git] / drivers / block / xen-blkback / xenbus.c
1 /*  Xenbus code for blkif backend
2     Copyright (C) 2005 Rusty Russell <rusty@rustcorp.com.au>
3     Copyright (C) 2005 XenSource Ltd
4
5     This program is free software; you can redistribute it and/or modify
6     it under the terms of the GNU General Public License as published by
7     the Free Software Foundation; either version 2 of the License, or
8     (at your option) any later version.
9
10     This program is distributed in the hope that it will be useful,
11     but WITHOUT ANY WARRANTY; without even the implied warranty of
12     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13     GNU General Public License for more details.
14
15 */
16
17 #include <stdarg.h>
18 #include <linux/module.h>
19 #include <linux/kthread.h>
20 #include <xen/events.h>
21 #include <xen/grant_table.h>
22 #include "common.h"
23
24 struct backend_info {
25         struct xenbus_device    *dev;
26         struct xen_blkif        *blkif;
27         struct xenbus_watch     backend_watch;
28         unsigned                major;
29         unsigned                minor;
30         char                    *mode;
31 };
32
33 static struct kmem_cache *xen_blkif_cachep;
34 static void connect(struct backend_info *);
35 static int connect_ring(struct backend_info *);
36 static void backend_changed(struct xenbus_watch *, const char **,
37                             unsigned int);
38
39 struct xenbus_device *xen_blkbk_xenbus(struct backend_info *be)
40 {
41         return be->dev;
42 }
43
44 static int blkback_name(struct xen_blkif *blkif, char *buf)
45 {
46         char *devpath, *devname;
47         struct xenbus_device *dev = blkif->be->dev;
48
49         devpath = xenbus_read(XBT_NIL, dev->nodename, "dev", NULL);
50         if (IS_ERR(devpath))
51                 return PTR_ERR(devpath);
52
53         devname = strstr(devpath, "/dev/");
54         if (devname != NULL)
55                 devname += strlen("/dev/");
56         else
57                 devname  = devpath;
58
59         snprintf(buf, TASK_COMM_LEN, "blkback.%d.%s", blkif->domid, devname);
60         kfree(devpath);
61
62         return 0;
63 }
64
65 static void xen_update_blkif_status(struct xen_blkif *blkif)
66 {
67         int err;
68         char name[TASK_COMM_LEN];
69
70         /* Not ready to connect? */
71         if (!blkif->irq || !blkif->vbd.bdev)
72                 return;
73
74         /* Already connected? */
75         if (blkif->be->dev->state == XenbusStateConnected)
76                 return;
77
78         /* Attempt to connect: exit if we fail to. */
79         connect(blkif->be);
80         if (blkif->be->dev->state != XenbusStateConnected)
81                 return;
82
83         err = blkback_name(blkif, name);
84         if (err) {
85                 xenbus_dev_error(blkif->be->dev, err, "get blkback dev name");
86                 return;
87         }
88
89         err = filemap_write_and_wait(blkif->vbd.bdev->bd_inode->i_mapping);
90         if (err) {
91                 xenbus_dev_error(blkif->be->dev, err, "block flush");
92                 return;
93         }
94         invalidate_inode_pages2(blkif->vbd.bdev->bd_inode->i_mapping);
95
96         blkif->xenblkd = kthread_run(xen_blkif_schedule, blkif, name);
97         if (IS_ERR(blkif->xenblkd)) {
98                 err = PTR_ERR(blkif->xenblkd);
99                 blkif->xenblkd = NULL;
100                 xenbus_dev_error(blkif->be->dev, err, "start xenblkd");
101         }
102 }
103
104 static struct xen_blkif *xen_blkif_alloc(domid_t domid)
105 {
106         struct xen_blkif *blkif;
107
108         blkif = kmem_cache_zalloc(xen_blkif_cachep, GFP_KERNEL);
109         if (!blkif)
110                 return ERR_PTR(-ENOMEM);
111
112         blkif->domid = domid;
113         spin_lock_init(&blkif->blk_ring_lock);
114         atomic_set(&blkif->refcnt, 1);
115         init_waitqueue_head(&blkif->wq);
116         init_completion(&blkif->drain_complete);
117         atomic_set(&blkif->drain, 0);
118         blkif->st_print = jiffies;
119         init_waitqueue_head(&blkif->waiting_to_free);
120
121         return blkif;
122 }
123
124 static int xen_blkif_map(struct xen_blkif *blkif, unsigned long shared_page,
125                          unsigned int evtchn)
126 {
127         int err;
128
129         /* Already connected through? */
130         if (blkif->irq)
131                 return 0;
132
133         err = xenbus_map_ring_valloc(blkif->be->dev, shared_page, &blkif->blk_ring);
134         if (err < 0)
135                 return err;
136
137         switch (blkif->blk_protocol) {
138         case BLKIF_PROTOCOL_NATIVE:
139         {
140                 struct blkif_sring *sring;
141                 sring = (struct blkif_sring *)blkif->blk_ring;
142                 BACK_RING_INIT(&blkif->blk_rings.native, sring, PAGE_SIZE);
143                 break;
144         }
145         case BLKIF_PROTOCOL_X86_32:
146         {
147                 struct blkif_x86_32_sring *sring_x86_32;
148                 sring_x86_32 = (struct blkif_x86_32_sring *)blkif->blk_ring;
149                 BACK_RING_INIT(&blkif->blk_rings.x86_32, sring_x86_32, PAGE_SIZE);
150                 break;
151         }
152         case BLKIF_PROTOCOL_X86_64:
153         {
154                 struct blkif_x86_64_sring *sring_x86_64;
155                 sring_x86_64 = (struct blkif_x86_64_sring *)blkif->blk_ring;
156                 BACK_RING_INIT(&blkif->blk_rings.x86_64, sring_x86_64, PAGE_SIZE);
157                 break;
158         }
159         default:
160                 BUG();
161         }
162
163         err = bind_interdomain_evtchn_to_irqhandler(blkif->domid, evtchn,
164                                                     xen_blkif_be_int, 0,
165                                                     "blkif-backend", blkif);
166         if (err < 0) {
167                 xenbus_unmap_ring_vfree(blkif->be->dev, blkif->blk_ring);
168                 blkif->blk_rings.common.sring = NULL;
169                 return err;
170         }
171         blkif->irq = err;
172
173         return 0;
174 }
175
176 static void xen_blkif_disconnect(struct xen_blkif *blkif)
177 {
178         if (blkif->xenblkd) {
179                 kthread_stop(blkif->xenblkd);
180                 blkif->xenblkd = NULL;
181         }
182
183         atomic_dec(&blkif->refcnt);
184         wait_event(blkif->waiting_to_free, atomic_read(&blkif->refcnt) == 0);
185         atomic_inc(&blkif->refcnt);
186
187         if (blkif->irq) {
188                 unbind_from_irqhandler(blkif->irq, blkif);
189                 blkif->irq = 0;
190         }
191
192         if (blkif->blk_rings.common.sring) {
193                 xenbus_unmap_ring_vfree(blkif->be->dev, blkif->blk_ring);
194                 blkif->blk_rings.common.sring = NULL;
195         }
196 }
197
198 static void xen_blkif_free(struct xen_blkif *blkif)
199 {
200         if (!atomic_dec_and_test(&blkif->refcnt))
201                 BUG();
202         kmem_cache_free(xen_blkif_cachep, blkif);
203 }
204
205 int __init xen_blkif_interface_init(void)
206 {
207         xen_blkif_cachep = kmem_cache_create("blkif_cache",
208                                              sizeof(struct xen_blkif),
209                                              0, 0, NULL);
210         if (!xen_blkif_cachep)
211                 return -ENOMEM;
212
213         return 0;
214 }
215
216 /*
217  *  sysfs interface for VBD I/O requests
218  */
219
220 #define VBD_SHOW(name, format, args...)                                 \
221         static ssize_t show_##name(struct device *_dev,                 \
222                                    struct device_attribute *attr,       \
223                                    char *buf)                           \
224         {                                                               \
225                 struct xenbus_device *dev = to_xenbus_device(_dev);     \
226                 struct backend_info *be = dev_get_drvdata(&dev->dev);   \
227                                                                         \
228                 return sprintf(buf, format, ##args);                    \
229         }                                                               \
230         static DEVICE_ATTR(name, S_IRUGO, show_##name, NULL)
231
232 VBD_SHOW(oo_req,  "%d\n", be->blkif->st_oo_req);
233 VBD_SHOW(rd_req,  "%d\n", be->blkif->st_rd_req);
234 VBD_SHOW(wr_req,  "%d\n", be->blkif->st_wr_req);
235 VBD_SHOW(f_req,  "%d\n", be->blkif->st_f_req);
236 VBD_SHOW(ds_req,  "%d\n", be->blkif->st_ds_req);
237 VBD_SHOW(rd_sect, "%d\n", be->blkif->st_rd_sect);
238 VBD_SHOW(wr_sect, "%d\n", be->blkif->st_wr_sect);
239
240 static struct attribute *xen_vbdstat_attrs[] = {
241         &dev_attr_oo_req.attr,
242         &dev_attr_rd_req.attr,
243         &dev_attr_wr_req.attr,
244         &dev_attr_f_req.attr,
245         &dev_attr_ds_req.attr,
246         &dev_attr_rd_sect.attr,
247         &dev_attr_wr_sect.attr,
248         NULL
249 };
250
251 static struct attribute_group xen_vbdstat_group = {
252         .name = "statistics",
253         .attrs = xen_vbdstat_attrs,
254 };
255
256 VBD_SHOW(physical_device, "%x:%x\n", be->major, be->minor);
257 VBD_SHOW(mode, "%s\n", be->mode);
258
259 static int xenvbd_sysfs_addif(struct xenbus_device *dev)
260 {
261         int error;
262
263         error = device_create_file(&dev->dev, &dev_attr_physical_device);
264         if (error)
265                 goto fail1;
266
267         error = device_create_file(&dev->dev, &dev_attr_mode);
268         if (error)
269                 goto fail2;
270
271         error = sysfs_create_group(&dev->dev.kobj, &xen_vbdstat_group);
272         if (error)
273                 goto fail3;
274
275         return 0;
276
277 fail3:  sysfs_remove_group(&dev->dev.kobj, &xen_vbdstat_group);
278 fail2:  device_remove_file(&dev->dev, &dev_attr_mode);
279 fail1:  device_remove_file(&dev->dev, &dev_attr_physical_device);
280         return error;
281 }
282
283 static void xenvbd_sysfs_delif(struct xenbus_device *dev)
284 {
285         sysfs_remove_group(&dev->dev.kobj, &xen_vbdstat_group);
286         device_remove_file(&dev->dev, &dev_attr_mode);
287         device_remove_file(&dev->dev, &dev_attr_physical_device);
288 }
289
290
291 static void xen_vbd_free(struct xen_vbd *vbd)
292 {
293         if (vbd->bdev)
294                 blkdev_put(vbd->bdev, vbd->readonly ? FMODE_READ : FMODE_WRITE);
295         vbd->bdev = NULL;
296 }
297
298 static int xen_vbd_create(struct xen_blkif *blkif, blkif_vdev_t handle,
299                           unsigned major, unsigned minor, int readonly,
300                           int cdrom)
301 {
302         struct xen_vbd *vbd;
303         struct block_device *bdev;
304         struct request_queue *q;
305
306         vbd = &blkif->vbd;
307         vbd->handle   = handle;
308         vbd->readonly = readonly;
309         vbd->type     = 0;
310
311         vbd->pdevice  = MKDEV(major, minor);
312
313         bdev = blkdev_get_by_dev(vbd->pdevice, vbd->readonly ?
314                                  FMODE_READ : FMODE_WRITE, NULL);
315
316         if (IS_ERR(bdev)) {
317                 DPRINTK("xen_vbd_create: device %08x could not be opened.\n",
318                         vbd->pdevice);
319                 return -ENOENT;
320         }
321
322         vbd->bdev = bdev;
323         if (vbd->bdev->bd_disk == NULL) {
324                 DPRINTK("xen_vbd_create: device %08x doesn't exist.\n",
325                         vbd->pdevice);
326                 xen_vbd_free(vbd);
327                 return -ENOENT;
328         }
329         vbd->size = vbd_sz(vbd);
330
331         if (vbd->bdev->bd_disk->flags & GENHD_FL_CD || cdrom)
332                 vbd->type |= VDISK_CDROM;
333         if (vbd->bdev->bd_disk->flags & GENHD_FL_REMOVABLE)
334                 vbd->type |= VDISK_REMOVABLE;
335
336         q = bdev_get_queue(bdev);
337         if (q && q->flush_flags)
338                 vbd->flush_support = true;
339
340         if (q && blk_queue_secdiscard(q))
341                 vbd->discard_secure = true;
342
343         DPRINTK("Successful creation of handle=%04x (dom=%u)\n",
344                 handle, blkif->domid);
345         return 0;
346 }
347 static int xen_blkbk_remove(struct xenbus_device *dev)
348 {
349         struct backend_info *be = dev_get_drvdata(&dev->dev);
350
351         DPRINTK("");
352
353         if (be->major || be->minor)
354                 xenvbd_sysfs_delif(dev);
355
356         if (be->backend_watch.node) {
357                 unregister_xenbus_watch(&be->backend_watch);
358                 kfree(be->backend_watch.node);
359                 be->backend_watch.node = NULL;
360         }
361
362         if (be->blkif) {
363                 xen_blkif_disconnect(be->blkif);
364                 xen_vbd_free(&be->blkif->vbd);
365                 xen_blkif_free(be->blkif);
366                 be->blkif = NULL;
367         }
368
369         kfree(be);
370         dev_set_drvdata(&dev->dev, NULL);
371         return 0;
372 }
373
374 int xen_blkbk_flush_diskcache(struct xenbus_transaction xbt,
375                               struct backend_info *be, int state)
376 {
377         struct xenbus_device *dev = be->dev;
378         int err;
379
380         err = xenbus_printf(xbt, dev->nodename, "feature-flush-cache",
381                             "%d", state);
382         if (err)
383                 dev_warn(&dev->dev, "writing feature-flush-cache (%d)", err);
384
385         return err;
386 }
387
388 static void xen_blkbk_discard(struct xenbus_transaction xbt, struct backend_info *be)
389 {
390         struct xenbus_device *dev = be->dev;
391         struct xen_blkif *blkif = be->blkif;
392         int err;
393         int state = 0;
394         struct block_device *bdev = be->blkif->vbd.bdev;
395         struct request_queue *q = bdev_get_queue(bdev);
396
397         if (blk_queue_discard(q)) {
398                 err = xenbus_printf(xbt, dev->nodename,
399                         "discard-granularity", "%u",
400                         q->limits.discard_granularity);
401                 if (err) {
402                         dev_warn(&dev->dev, "writing discard-granularity (%d)", err);
403                         return;
404                 }
405                 err = xenbus_printf(xbt, dev->nodename,
406                         "discard-alignment", "%u",
407                         q->limits.discard_alignment);
408                 if (err) {
409                         dev_warn(&dev->dev, "writing discard-alignment (%d)", err);
410                         return;
411                 }
412                 state = 1;
413                 /* Optional. */
414                 err = xenbus_printf(xbt, dev->nodename,
415                                     "discard-secure", "%d",
416                                     blkif->vbd.discard_secure);
417                 if (err) {
418                         dev_warn(&dev->dev, "writing discard-secure (%d)", err);
419                         return;
420                 }
421         }
422         err = xenbus_printf(xbt, dev->nodename, "feature-discard",
423                             "%d", state);
424         if (err)
425                 dev_warn(&dev->dev, "writing feature-discard (%d)", err);
426 }
427 int xen_blkbk_barrier(struct xenbus_transaction xbt,
428                       struct backend_info *be, int state)
429 {
430         struct xenbus_device *dev = be->dev;
431         int err;
432
433         err = xenbus_printf(xbt, dev->nodename, "feature-barrier",
434                             "%d", state);
435         if (err)
436                 dev_warn(&dev->dev, "writing feature-barrier (%d)", err);
437
438         return err;
439 }
440
441 /*
442  * Entry point to this code when a new device is created.  Allocate the basic
443  * structures, and watch the store waiting for the hotplug scripts to tell us
444  * the device's physical major and minor numbers.  Switch to InitWait.
445  */
446 static int xen_blkbk_probe(struct xenbus_device *dev,
447                            const struct xenbus_device_id *id)
448 {
449         int err;
450         struct backend_info *be = kzalloc(sizeof(struct backend_info),
451                                           GFP_KERNEL);
452         if (!be) {
453                 xenbus_dev_fatal(dev, -ENOMEM,
454                                  "allocating backend structure");
455                 return -ENOMEM;
456         }
457         be->dev = dev;
458         dev_set_drvdata(&dev->dev, be);
459
460         be->blkif = xen_blkif_alloc(dev->otherend_id);
461         if (IS_ERR(be->blkif)) {
462                 err = PTR_ERR(be->blkif);
463                 be->blkif = NULL;
464                 xenbus_dev_fatal(dev, err, "creating block interface");
465                 goto fail;
466         }
467
468         /* setup back pointer */
469         be->blkif->be = be;
470
471         err = xenbus_watch_pathfmt(dev, &be->backend_watch, backend_changed,
472                                    "%s/%s", dev->nodename, "physical-device");
473         if (err)
474                 goto fail;
475
476         err = xenbus_switch_state(dev, XenbusStateInitWait);
477         if (err)
478                 goto fail;
479
480         return 0;
481
482 fail:
483         DPRINTK("failed");
484         xen_blkbk_remove(dev);
485         return err;
486 }
487
488
489 /*
490  * Callback received when the hotplug scripts have placed the physical-device
491  * node.  Read it and the mode node, and create a vbd.  If the frontend is
492  * ready, connect.
493  */
494 static void backend_changed(struct xenbus_watch *watch,
495                             const char **vec, unsigned int len)
496 {
497         int err;
498         unsigned major;
499         unsigned minor;
500         struct backend_info *be
501                 = container_of(watch, struct backend_info, backend_watch);
502         struct xenbus_device *dev = be->dev;
503         int cdrom = 0;
504         char *device_type;
505
506         DPRINTK("");
507
508         err = xenbus_scanf(XBT_NIL, dev->nodename, "physical-device", "%x:%x",
509                            &major, &minor);
510         if (XENBUS_EXIST_ERR(err)) {
511                 /*
512                  * Since this watch will fire once immediately after it is
513                  * registered, we expect this.  Ignore it, and wait for the
514                  * hotplug scripts.
515                  */
516                 return;
517         }
518         if (err != 2) {
519                 xenbus_dev_fatal(dev, err, "reading physical-device");
520                 return;
521         }
522
523         if ((be->major || be->minor) &&
524             ((be->major != major) || (be->minor != minor))) {
525                 pr_warn(DRV_PFX "changing physical device (from %x:%x to %x:%x) not supported.\n",
526                         be->major, be->minor, major, minor);
527                 return;
528         }
529
530         be->mode = xenbus_read(XBT_NIL, dev->nodename, "mode", NULL);
531         if (IS_ERR(be->mode)) {
532                 err = PTR_ERR(be->mode);
533                 be->mode = NULL;
534                 xenbus_dev_fatal(dev, err, "reading mode");
535                 return;
536         }
537
538         device_type = xenbus_read(XBT_NIL, dev->otherend, "device-type", NULL);
539         if (!IS_ERR(device_type)) {
540                 cdrom = strcmp(device_type, "cdrom") == 0;
541                 kfree(device_type);
542         }
543
544         if (be->major == 0 && be->minor == 0) {
545                 /* Front end dir is a number, which is used as the handle. */
546
547                 char *p = strrchr(dev->otherend, '/') + 1;
548                 long handle;
549                 err = strict_strtoul(p, 0, &handle);
550                 if (err)
551                         return;
552
553                 be->major = major;
554                 be->minor = minor;
555
556                 err = xen_vbd_create(be->blkif, handle, major, minor,
557                                  (NULL == strchr(be->mode, 'w')), cdrom);
558                 if (err) {
559                         be->major = 0;
560                         be->minor = 0;
561                         xenbus_dev_fatal(dev, err, "creating vbd structure");
562                         return;
563                 }
564
565                 err = xenvbd_sysfs_addif(dev);
566                 if (err) {
567                         xen_vbd_free(&be->blkif->vbd);
568                         be->major = 0;
569                         be->minor = 0;
570                         xenbus_dev_fatal(dev, err, "creating sysfs entries");
571                         return;
572                 }
573
574                 /* We're potentially connected now */
575                 xen_update_blkif_status(be->blkif);
576         }
577 }
578
579
580 /*
581  * Callback received when the frontend's state changes.
582  */
583 static void frontend_changed(struct xenbus_device *dev,
584                              enum xenbus_state frontend_state)
585 {
586         struct backend_info *be = dev_get_drvdata(&dev->dev);
587         int err;
588
589         DPRINTK("%s", xenbus_strstate(frontend_state));
590
591         switch (frontend_state) {
592         case XenbusStateInitialising:
593                 if (dev->state == XenbusStateClosed) {
594                         pr_info(DRV_PFX "%s: prepare for reconnect\n",
595                                 dev->nodename);
596                         xenbus_switch_state(dev, XenbusStateInitWait);
597                 }
598                 break;
599
600         case XenbusStateInitialised:
601         case XenbusStateConnected:
602                 /*
603                  * Ensure we connect even when two watches fire in
604                  * close succession and we miss the intermediate value
605                  * of frontend_state.
606                  */
607                 if (dev->state == XenbusStateConnected)
608                         break;
609
610                 /*
611                  * Enforce precondition before potential leak point.
612                  * xen_blkif_disconnect() is idempotent.
613                  */
614                 xen_blkif_disconnect(be->blkif);
615
616                 err = connect_ring(be);
617                 if (err)
618                         break;
619                 xen_update_blkif_status(be->blkif);
620                 break;
621
622         case XenbusStateClosing:
623                 xenbus_switch_state(dev, XenbusStateClosing);
624                 break;
625
626         case XenbusStateClosed:
627                 xen_blkif_disconnect(be->blkif);
628                 xenbus_switch_state(dev, XenbusStateClosed);
629                 if (xenbus_dev_is_online(dev))
630                         break;
631                 /* fall through if not online */
632         case XenbusStateUnknown:
633                 /* implies xen_blkif_disconnect() via xen_blkbk_remove() */
634                 device_unregister(&dev->dev);
635                 break;
636
637         default:
638                 xenbus_dev_fatal(dev, -EINVAL, "saw state %d at frontend",
639                                  frontend_state);
640                 break;
641         }
642 }
643
644
645 /* ** Connection ** */
646
647
648 /*
649  * Write the physical details regarding the block device to the store, and
650  * switch to Connected state.
651  */
652 static void connect(struct backend_info *be)
653 {
654         struct xenbus_transaction xbt;
655         int err;
656         struct xenbus_device *dev = be->dev;
657
658         DPRINTK("%s", dev->otherend);
659
660         /* Supply the information about the device the frontend needs */
661 again:
662         err = xenbus_transaction_start(&xbt);
663         if (err) {
664                 xenbus_dev_fatal(dev, err, "starting transaction");
665                 return;
666         }
667
668         /* If we can't advertise it is OK. */
669         xen_blkbk_flush_diskcache(xbt, be, be->blkif->vbd.flush_support);
670
671         xen_blkbk_discard(xbt, be);
672
673         xen_blkbk_barrier(xbt, be, be->blkif->vbd.flush_support);
674
675         err = xenbus_printf(xbt, dev->nodename, "sectors", "%llu",
676                             (unsigned long long)vbd_sz(&be->blkif->vbd));
677         if (err) {
678                 xenbus_dev_fatal(dev, err, "writing %s/sectors",
679                                  dev->nodename);
680                 goto abort;
681         }
682
683         /* FIXME: use a typename instead */
684         err = xenbus_printf(xbt, dev->nodename, "info", "%u",
685                             be->blkif->vbd.type |
686                             (be->blkif->vbd.readonly ? VDISK_READONLY : 0));
687         if (err) {
688                 xenbus_dev_fatal(dev, err, "writing %s/info",
689                                  dev->nodename);
690                 goto abort;
691         }
692         err = xenbus_printf(xbt, dev->nodename, "sector-size", "%lu",
693                             (unsigned long)
694                             bdev_logical_block_size(be->blkif->vbd.bdev));
695         if (err) {
696                 xenbus_dev_fatal(dev, err, "writing %s/sector-size",
697                                  dev->nodename);
698                 goto abort;
699         }
700
701         err = xenbus_transaction_end(xbt, 0);
702         if (err == -EAGAIN)
703                 goto again;
704         if (err)
705                 xenbus_dev_fatal(dev, err, "ending transaction");
706
707         err = xenbus_switch_state(dev, XenbusStateConnected);
708         if (err)
709                 xenbus_dev_fatal(dev, err, "%s: switching to Connected state",
710                                  dev->nodename);
711
712         return;
713  abort:
714         xenbus_transaction_end(xbt, 1);
715 }
716
717
718 static int connect_ring(struct backend_info *be)
719 {
720         struct xenbus_device *dev = be->dev;
721         unsigned long ring_ref;
722         unsigned int evtchn;
723         char protocol[64] = "";
724         int err;
725
726         DPRINTK("%s", dev->otherend);
727
728         err = xenbus_gather(XBT_NIL, dev->otherend, "ring-ref", "%lu",
729                             &ring_ref, "event-channel", "%u", &evtchn, NULL);
730         if (err) {
731                 xenbus_dev_fatal(dev, err,
732                                  "reading %s/ring-ref and event-channel",
733                                  dev->otherend);
734                 return err;
735         }
736
737         be->blkif->blk_protocol = BLKIF_PROTOCOL_NATIVE;
738         err = xenbus_gather(XBT_NIL, dev->otherend, "protocol",
739                             "%63s", protocol, NULL);
740         if (err)
741                 strcpy(protocol, "unspecified, assuming native");
742         else if (0 == strcmp(protocol, XEN_IO_PROTO_ABI_NATIVE))
743                 be->blkif->blk_protocol = BLKIF_PROTOCOL_NATIVE;
744         else if (0 == strcmp(protocol, XEN_IO_PROTO_ABI_X86_32))
745                 be->blkif->blk_protocol = BLKIF_PROTOCOL_X86_32;
746         else if (0 == strcmp(protocol, XEN_IO_PROTO_ABI_X86_64))
747                 be->blkif->blk_protocol = BLKIF_PROTOCOL_X86_64;
748         else {
749                 xenbus_dev_fatal(dev, err, "unknown fe protocol %s", protocol);
750                 return -1;
751         }
752         pr_info(DRV_PFX "ring-ref %ld, event-channel %d, protocol %d (%s)\n",
753                 ring_ref, evtchn, be->blkif->blk_protocol, protocol);
754
755         /* Map the shared frame, irq etc. */
756         err = xen_blkif_map(be->blkif, ring_ref, evtchn);
757         if (err) {
758                 xenbus_dev_fatal(dev, err, "mapping ring-ref %lu port %u",
759                                  ring_ref, evtchn);
760                 return err;
761         }
762
763         return 0;
764 }
765
766
767 /* ** Driver Registration ** */
768
769
770 static const struct xenbus_device_id xen_blkbk_ids[] = {
771         { "vbd" },
772         { "" }
773 };
774
775
776 static DEFINE_XENBUS_DRIVER(xen_blkbk, ,
777         .probe = xen_blkbk_probe,
778         .remove = xen_blkbk_remove,
779         .otherend_changed = frontend_changed
780 );
781
782
783 int xen_blkif_xenbus_init(void)
784 {
785         return xenbus_register_backend(&xen_blkbk_driver);
786 }