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