]> Pileus Git - ~andy/linux/blob - drivers/block/xen-blkback/xenbus.c
Merge branch 'exynos/pwm-clocksource' into late/multiplatform
[~andy/linux] / 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         blkif->persistent_gnts.rb_node = NULL;
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 static 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,  "%llu\n", be->blkif->st_oo_req);
234 VBD_SHOW(rd_req,  "%llu\n", be->blkif->st_rd_req);
235 VBD_SHOW(wr_req,  "%llu\n", be->blkif->st_wr_req);
236 VBD_SHOW(f_req,  "%llu\n", be->blkif->st_f_req);
237 VBD_SHOW(ds_req,  "%llu\n", be->blkif->st_ds_req);
238 VBD_SHOW(rd_sect, "%llu\n", be->blkif->st_rd_sect);
239 VBD_SHOW(wr_sect, "%llu\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 static 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 static 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         if (q && blk_queue_secdiscard(q))
342                 vbd->discard_secure = true;
343
344         DPRINTK("Successful creation of handle=%04x (dom=%u)\n",
345                 handle, blkif->domid);
346         return 0;
347 }
348 static int xen_blkbk_remove(struct xenbus_device *dev)
349 {
350         struct backend_info *be = dev_get_drvdata(&dev->dev);
351
352         DPRINTK("");
353
354         if (be->major || be->minor)
355                 xenvbd_sysfs_delif(dev);
356
357         if (be->backend_watch.node) {
358                 unregister_xenbus_watch(&be->backend_watch);
359                 kfree(be->backend_watch.node);
360                 be->backend_watch.node = NULL;
361         }
362
363         if (be->blkif) {
364                 xen_blkif_disconnect(be->blkif);
365                 xen_vbd_free(&be->blkif->vbd);
366                 xen_blkif_free(be->blkif);
367                 be->blkif = NULL;
368         }
369
370         kfree(be->mode);
371         kfree(be);
372         dev_set_drvdata(&dev->dev, NULL);
373         return 0;
374 }
375
376 int xen_blkbk_flush_diskcache(struct xenbus_transaction xbt,
377                               struct backend_info *be, int state)
378 {
379         struct xenbus_device *dev = be->dev;
380         int err;
381
382         err = xenbus_printf(xbt, dev->nodename, "feature-flush-cache",
383                             "%d", state);
384         if (err)
385                 dev_warn(&dev->dev, "writing feature-flush-cache (%d)", err);
386
387         return err;
388 }
389
390 static void xen_blkbk_discard(struct xenbus_transaction xbt, struct backend_info *be)
391 {
392         struct xenbus_device *dev = be->dev;
393         struct xen_blkif *blkif = be->blkif;
394         int err;
395         int state = 0;
396         struct block_device *bdev = be->blkif->vbd.bdev;
397         struct request_queue *q = bdev_get_queue(bdev);
398
399         if (blk_queue_discard(q)) {
400                 err = xenbus_printf(xbt, dev->nodename,
401                         "discard-granularity", "%u",
402                         q->limits.discard_granularity);
403                 if (err) {
404                         dev_warn(&dev->dev, "writing discard-granularity (%d)", err);
405                         return;
406                 }
407                 err = xenbus_printf(xbt, dev->nodename,
408                         "discard-alignment", "%u",
409                         q->limits.discard_alignment);
410                 if (err) {
411                         dev_warn(&dev->dev, "writing discard-alignment (%d)", err);
412                         return;
413                 }
414                 state = 1;
415                 /* Optional. */
416                 err = xenbus_printf(xbt, dev->nodename,
417                                     "discard-secure", "%d",
418                                     blkif->vbd.discard_secure);
419                 if (err) {
420                         dev_warn(&dev->dev, "writing discard-secure (%d)", err);
421                         return;
422                 }
423         }
424         err = xenbus_printf(xbt, dev->nodename, "feature-discard",
425                             "%d", state);
426         if (err)
427                 dev_warn(&dev->dev, "writing feature-discard (%d)", err);
428 }
429 int xen_blkbk_barrier(struct xenbus_transaction xbt,
430                       struct backend_info *be, int state)
431 {
432         struct xenbus_device *dev = be->dev;
433         int err;
434
435         err = xenbus_printf(xbt, dev->nodename, "feature-barrier",
436                             "%d", state);
437         if (err)
438                 dev_warn(&dev->dev, "writing feature-barrier (%d)", err);
439
440         return err;
441 }
442
443 /*
444  * Entry point to this code when a new device is created.  Allocate the basic
445  * structures, and watch the store waiting for the hotplug scripts to tell us
446  * the device's physical major and minor numbers.  Switch to InitWait.
447  */
448 static int xen_blkbk_probe(struct xenbus_device *dev,
449                            const struct xenbus_device_id *id)
450 {
451         int err;
452         struct backend_info *be = kzalloc(sizeof(struct backend_info),
453                                           GFP_KERNEL);
454         if (!be) {
455                 xenbus_dev_fatal(dev, -ENOMEM,
456                                  "allocating backend structure");
457                 return -ENOMEM;
458         }
459         be->dev = dev;
460         dev_set_drvdata(&dev->dev, be);
461
462         be->blkif = xen_blkif_alloc(dev->otherend_id);
463         if (IS_ERR(be->blkif)) {
464                 err = PTR_ERR(be->blkif);
465                 be->blkif = NULL;
466                 xenbus_dev_fatal(dev, err, "creating block interface");
467                 goto fail;
468         }
469
470         /* setup back pointer */
471         be->blkif->be = be;
472
473         err = xenbus_watch_pathfmt(dev, &be->backend_watch, backend_changed,
474                                    "%s/%s", dev->nodename, "physical-device");
475         if (err)
476                 goto fail;
477
478         err = xenbus_switch_state(dev, XenbusStateInitWait);
479         if (err)
480                 goto fail;
481
482         return 0;
483
484 fail:
485         DPRINTK("failed");
486         xen_blkbk_remove(dev);
487         return err;
488 }
489
490
491 /*
492  * Callback received when the hotplug scripts have placed the physical-device
493  * node.  Read it and the mode node, and create a vbd.  If the frontend is
494  * ready, connect.
495  */
496 static void backend_changed(struct xenbus_watch *watch,
497                             const char **vec, unsigned int len)
498 {
499         int err;
500         unsigned major;
501         unsigned minor;
502         struct backend_info *be
503                 = container_of(watch, struct backend_info, backend_watch);
504         struct xenbus_device *dev = be->dev;
505         int cdrom = 0;
506         unsigned long handle;
507         char *device_type;
508
509         DPRINTK("");
510
511         err = xenbus_scanf(XBT_NIL, dev->nodename, "physical-device", "%x:%x",
512                            &major, &minor);
513         if (XENBUS_EXIST_ERR(err)) {
514                 /*
515                  * Since this watch will fire once immediately after it is
516                  * registered, we expect this.  Ignore it, and wait for the
517                  * hotplug scripts.
518                  */
519                 return;
520         }
521         if (err != 2) {
522                 xenbus_dev_fatal(dev, err, "reading physical-device");
523                 return;
524         }
525
526         if (be->major | be->minor) {
527                 if (be->major != major || be->minor != minor)
528                         pr_warn(DRV_PFX "changing physical device (from %x:%x to %x:%x) not supported.\n",
529                                 be->major, be->minor, major, minor);
530                 return;
531         }
532
533         be->mode = xenbus_read(XBT_NIL, dev->nodename, "mode", NULL);
534         if (IS_ERR(be->mode)) {
535                 err = PTR_ERR(be->mode);
536                 be->mode = NULL;
537                 xenbus_dev_fatal(dev, err, "reading mode");
538                 return;
539         }
540
541         device_type = xenbus_read(XBT_NIL, dev->otherend, "device-type", NULL);
542         if (!IS_ERR(device_type)) {
543                 cdrom = strcmp(device_type, "cdrom") == 0;
544                 kfree(device_type);
545         }
546
547         /* Front end dir is a number, which is used as the handle. */
548         err = strict_strtoul(strrchr(dev->otherend, '/') + 1, 0, &handle);
549         if (err)
550                 return;
551
552         be->major = major;
553         be->minor = minor;
554
555         err = xen_vbd_create(be->blkif, handle, major, minor,
556                              !strchr(be->mode, 'w'), cdrom);
557
558         if (err)
559                 xenbus_dev_fatal(dev, err, "creating vbd structure");
560         else {
561                 err = xenvbd_sysfs_addif(dev);
562                 if (err) {
563                         xen_vbd_free(&be->blkif->vbd);
564                         xenbus_dev_fatal(dev, err, "creating sysfs entries");
565                 }
566         }
567
568         if (err) {
569                 kfree(be->mode);
570                 be->mode = NULL;
571                 be->major = 0;
572                 be->minor = 0;
573         } else {
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, "feature-persistent", "%u", 1);
676         if (err) {
677                 xenbus_dev_fatal(dev, err, "writing %s/feature-persistent",
678                                  dev->nodename);
679                 goto abort;
680         }
681
682         err = xenbus_printf(xbt, dev->nodename, "sectors", "%llu",
683                             (unsigned long long)vbd_sz(&be->blkif->vbd));
684         if (err) {
685                 xenbus_dev_fatal(dev, err, "writing %s/sectors",
686                                  dev->nodename);
687                 goto abort;
688         }
689
690         /* FIXME: use a typename instead */
691         err = xenbus_printf(xbt, dev->nodename, "info", "%u",
692                             be->blkif->vbd.type |
693                             (be->blkif->vbd.readonly ? VDISK_READONLY : 0));
694         if (err) {
695                 xenbus_dev_fatal(dev, err, "writing %s/info",
696                                  dev->nodename);
697                 goto abort;
698         }
699         err = xenbus_printf(xbt, dev->nodename, "sector-size", "%lu",
700                             (unsigned long)
701                             bdev_logical_block_size(be->blkif->vbd.bdev));
702         if (err) {
703                 xenbus_dev_fatal(dev, err, "writing %s/sector-size",
704                                  dev->nodename);
705                 goto abort;
706         }
707
708         err = xenbus_transaction_end(xbt, 0);
709         if (err == -EAGAIN)
710                 goto again;
711         if (err)
712                 xenbus_dev_fatal(dev, err, "ending transaction");
713
714         err = xenbus_switch_state(dev, XenbusStateConnected);
715         if (err)
716                 xenbus_dev_fatal(dev, err, "%s: switching to Connected state",
717                                  dev->nodename);
718
719         return;
720  abort:
721         xenbus_transaction_end(xbt, 1);
722 }
723
724
725 static int connect_ring(struct backend_info *be)
726 {
727         struct xenbus_device *dev = be->dev;
728         unsigned long ring_ref;
729         unsigned int evtchn;
730         unsigned int pers_grants;
731         char protocol[64] = "";
732         int err;
733
734         DPRINTK("%s", dev->otherend);
735
736         err = xenbus_gather(XBT_NIL, dev->otherend, "ring-ref", "%lu",
737                             &ring_ref, "event-channel", "%u", &evtchn, NULL);
738         if (err) {
739                 xenbus_dev_fatal(dev, err,
740                                  "reading %s/ring-ref and event-channel",
741                                  dev->otherend);
742                 return err;
743         }
744
745         be->blkif->blk_protocol = BLKIF_PROTOCOL_NATIVE;
746         err = xenbus_gather(XBT_NIL, dev->otherend, "protocol",
747                             "%63s", protocol, NULL);
748         if (err)
749                 strcpy(protocol, "unspecified, assuming native");
750         else if (0 == strcmp(protocol, XEN_IO_PROTO_ABI_NATIVE))
751                 be->blkif->blk_protocol = BLKIF_PROTOCOL_NATIVE;
752         else if (0 == strcmp(protocol, XEN_IO_PROTO_ABI_X86_32))
753                 be->blkif->blk_protocol = BLKIF_PROTOCOL_X86_32;
754         else if (0 == strcmp(protocol, XEN_IO_PROTO_ABI_X86_64))
755                 be->blkif->blk_protocol = BLKIF_PROTOCOL_X86_64;
756         else {
757                 xenbus_dev_fatal(dev, err, "unknown fe protocol %s", protocol);
758                 return -1;
759         }
760         err = xenbus_gather(XBT_NIL, dev->otherend,
761                             "feature-persistent", "%u",
762                             &pers_grants, NULL);
763         if (err)
764                 pers_grants = 0;
765
766         be->blkif->vbd.feature_gnt_persistent = pers_grants;
767         be->blkif->vbd.overflow_max_grants = 0;
768
769         pr_info(DRV_PFX "ring-ref %ld, event-channel %d, protocol %d (%s) %s\n",
770                 ring_ref, evtchn, be->blkif->blk_protocol, protocol,
771                 pers_grants ? "persistent grants" : "");
772
773         /* Map the shared frame, irq etc. */
774         err = xen_blkif_map(be->blkif, ring_ref, evtchn);
775         if (err) {
776                 xenbus_dev_fatal(dev, err, "mapping ring-ref %lu port %u",
777                                  ring_ref, evtchn);
778                 return err;
779         }
780
781         return 0;
782 }
783
784
785 /* ** Driver Registration ** */
786
787
788 static const struct xenbus_device_id xen_blkbk_ids[] = {
789         { "vbd" },
790         { "" }
791 };
792
793
794 static DEFINE_XENBUS_DRIVER(xen_blkbk, ,
795         .probe = xen_blkbk_probe,
796         .remove = xen_blkbk_remove,
797         .otherend_changed = frontend_changed
798 );
799
800
801 int xen_blkif_xenbus_init(void)
802 {
803         return xenbus_register_backend(&xen_blkbk_driver);
804 }