1 /* Xenbus code for blkif backend
2 Copyright (C) 2005 Rusty Russell <rusty@rustcorp.com.au>
3 Copyright (C) 2005 XenSource Ltd
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.
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.
18 #include <linux/module.h>
19 #include <linux/kthread.h>
20 #include <xen/events.h>
21 #include <xen/grant_table.h>
25 struct xenbus_device
*dev
;
26 struct xen_blkif
*blkif
;
27 struct xenbus_watch backend_watch
;
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 **,
39 struct xenbus_device
*xen_blkbk_xenbus(struct backend_info
*be
)
44 static int blkback_name(struct xen_blkif
*blkif
, char *buf
)
46 char *devpath
, *devname
;
47 struct xenbus_device
*dev
= blkif
->be
->dev
;
49 devpath
= xenbus_read(XBT_NIL
, dev
->nodename
, "dev", NULL
);
51 return PTR_ERR(devpath
);
53 devname
= strstr(devpath
, "/dev/");
55 devname
+= strlen("/dev/");
59 snprintf(buf
, TASK_COMM_LEN
, "blkback.%d.%s", blkif
->domid
, devname
);
65 static void xen_update_blkif_status(struct xen_blkif
*blkif
)
68 char name
[TASK_COMM_LEN
];
70 /* Not ready to connect? */
71 if (!blkif
->irq
|| !blkif
->vbd
.bdev
)
74 /* Already connected? */
75 if (blkif
->be
->dev
->state
== XenbusStateConnected
)
78 /* Attempt to connect: exit if we fail to. */
80 if (blkif
->be
->dev
->state
!= XenbusStateConnected
)
83 err
= blkback_name(blkif
, name
);
85 xenbus_dev_error(blkif
->be
->dev
, err
, "get blkback dev name");
89 err
= filemap_write_and_wait(blkif
->vbd
.bdev
->bd_inode
->i_mapping
);
91 xenbus_dev_error(blkif
->be
->dev
, err
, "block flush");
94 invalidate_inode_pages2(blkif
->vbd
.bdev
->bd_inode
->i_mapping
);
96 blkif
->xenblkd
= kthread_run(xen_blkif_schedule
, blkif
, "%s", 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");
105 static struct xen_blkif
*xen_blkif_alloc(domid_t domid
)
107 struct xen_blkif
*blkif
;
108 struct pending_req
*req
, *n
;
111 BUILD_BUG_ON(MAX_INDIRECT_PAGES
> BLKIF_MAX_INDIRECT_PAGES_PER_REQUEST
);
113 blkif
= kmem_cache_zalloc(xen_blkif_cachep
, GFP_KERNEL
);
115 return ERR_PTR(-ENOMEM
);
117 blkif
->domid
= domid
;
118 spin_lock_init(&blkif
->blk_ring_lock
);
119 atomic_set(&blkif
->refcnt
, 1);
120 init_waitqueue_head(&blkif
->wq
);
121 init_completion(&blkif
->drain_complete
);
122 atomic_set(&blkif
->drain
, 0);
123 blkif
->st_print
= jiffies
;
124 init_waitqueue_head(&blkif
->waiting_to_free
);
125 blkif
->persistent_gnts
.rb_node
= NULL
;
126 spin_lock_init(&blkif
->free_pages_lock
);
127 INIT_LIST_HEAD(&blkif
->free_pages
);
128 INIT_LIST_HEAD(&blkif
->persistent_purge_list
);
129 blkif
->free_pages_num
= 0;
130 atomic_set(&blkif
->persistent_gnt_in_use
, 0);
131 atomic_set(&blkif
->inflight
, 0);
132 INIT_WORK(&blkif
->persistent_purge_work
, xen_blkbk_unmap_purged_grants
);
134 INIT_LIST_HEAD(&blkif
->pending_free
);
136 for (i
= 0; i
< XEN_BLKIF_REQS
; i
++) {
137 req
= kzalloc(sizeof(*req
), GFP_KERNEL
);
140 list_add_tail(&req
->free_list
,
141 &blkif
->pending_free
);
142 for (j
= 0; j
< MAX_INDIRECT_SEGMENTS
; j
++) {
143 req
->segments
[j
] = kzalloc(sizeof(*req
->segments
[0]),
145 if (!req
->segments
[j
])
148 for (j
= 0; j
< MAX_INDIRECT_PAGES
; j
++) {
149 req
->indirect_pages
[j
] = kzalloc(sizeof(*req
->indirect_pages
[0]),
151 if (!req
->indirect_pages
[j
])
155 spin_lock_init(&blkif
->pending_free_lock
);
156 init_waitqueue_head(&blkif
->pending_free_wq
);
157 init_waitqueue_head(&blkif
->shutdown_wq
);
162 list_for_each_entry_safe(req
, n
, &blkif
->pending_free
, free_list
) {
163 list_del(&req
->free_list
);
164 for (j
= 0; j
< MAX_INDIRECT_SEGMENTS
; j
++) {
165 if (!req
->segments
[j
])
167 kfree(req
->segments
[j
]);
169 for (j
= 0; j
< MAX_INDIRECT_PAGES
; j
++) {
170 if (!req
->indirect_pages
[j
])
172 kfree(req
->indirect_pages
[j
]);
177 kmem_cache_free(xen_blkif_cachep
, blkif
);
179 return ERR_PTR(-ENOMEM
);
182 static int xen_blkif_map(struct xen_blkif
*blkif
, unsigned long shared_page
,
187 /* Already connected through? */
191 err
= xenbus_map_ring_valloc(blkif
->be
->dev
, shared_page
, &blkif
->blk_ring
);
195 switch (blkif
->blk_protocol
) {
196 case BLKIF_PROTOCOL_NATIVE
:
198 struct blkif_sring
*sring
;
199 sring
= (struct blkif_sring
*)blkif
->blk_ring
;
200 BACK_RING_INIT(&blkif
->blk_rings
.native
, sring
, PAGE_SIZE
);
203 case BLKIF_PROTOCOL_X86_32
:
205 struct blkif_x86_32_sring
*sring_x86_32
;
206 sring_x86_32
= (struct blkif_x86_32_sring
*)blkif
->blk_ring
;
207 BACK_RING_INIT(&blkif
->blk_rings
.x86_32
, sring_x86_32
, PAGE_SIZE
);
210 case BLKIF_PROTOCOL_X86_64
:
212 struct blkif_x86_64_sring
*sring_x86_64
;
213 sring_x86_64
= (struct blkif_x86_64_sring
*)blkif
->blk_ring
;
214 BACK_RING_INIT(&blkif
->blk_rings
.x86_64
, sring_x86_64
, PAGE_SIZE
);
221 err
= bind_interdomain_evtchn_to_irqhandler(blkif
->domid
, evtchn
,
223 "blkif-backend", blkif
);
225 xenbus_unmap_ring_vfree(blkif
->be
->dev
, blkif
->blk_ring
);
226 blkif
->blk_rings
.common
.sring
= NULL
;
234 static void xen_blkif_disconnect(struct xen_blkif
*blkif
)
236 if (blkif
->xenblkd
) {
237 kthread_stop(blkif
->xenblkd
);
238 wake_up(&blkif
->shutdown_wq
);
239 blkif
->xenblkd
= NULL
;
242 atomic_dec(&blkif
->refcnt
);
243 wait_event(blkif
->waiting_to_free
, atomic_read(&blkif
->refcnt
) == 0);
244 atomic_inc(&blkif
->refcnt
);
247 unbind_from_irqhandler(blkif
->irq
, blkif
);
251 if (blkif
->blk_rings
.common
.sring
) {
252 xenbus_unmap_ring_vfree(blkif
->be
->dev
, blkif
->blk_ring
);
253 blkif
->blk_rings
.common
.sring
= NULL
;
257 static void xen_blkif_free(struct xen_blkif
*blkif
)
259 struct pending_req
*req
, *n
;
262 if (!atomic_dec_and_test(&blkif
->refcnt
))
265 /* Remove all persistent grants and the cache of ballooned pages. */
266 xen_blkbk_free_caches(blkif
);
268 /* Make sure everything is drained before shutting down */
269 BUG_ON(blkif
->persistent_gnt_c
!= 0);
270 BUG_ON(atomic_read(&blkif
->persistent_gnt_in_use
) != 0);
271 BUG_ON(blkif
->free_pages_num
!= 0);
272 BUG_ON(!list_empty(&blkif
->persistent_purge_list
));
273 BUG_ON(!list_empty(&blkif
->free_pages
));
274 BUG_ON(!RB_EMPTY_ROOT(&blkif
->persistent_gnts
));
276 /* Check that there is no request in use */
277 list_for_each_entry_safe(req
, n
, &blkif
->pending_free
, free_list
) {
278 list_del(&req
->free_list
);
280 for (j
= 0; j
< MAX_INDIRECT_SEGMENTS
; j
++)
281 kfree(req
->segments
[j
]);
283 for (j
= 0; j
< MAX_INDIRECT_PAGES
; j
++)
284 kfree(req
->indirect_pages
[j
]);
290 WARN_ON(i
!= XEN_BLKIF_REQS
);
292 kmem_cache_free(xen_blkif_cachep
, blkif
);
295 int __init
xen_blkif_interface_init(void)
297 xen_blkif_cachep
= kmem_cache_create("blkif_cache",
298 sizeof(struct xen_blkif
),
300 if (!xen_blkif_cachep
)
307 * sysfs interface for VBD I/O requests
310 #define VBD_SHOW(name, format, args...) \
311 static ssize_t show_##name(struct device *_dev, \
312 struct device_attribute *attr, \
315 struct xenbus_device *dev = to_xenbus_device(_dev); \
316 struct backend_info *be = dev_get_drvdata(&dev->dev); \
318 return sprintf(buf, format, ##args); \
320 static DEVICE_ATTR(name, S_IRUGO, show_##name, NULL)
322 VBD_SHOW(oo_req
, "%llu\n", be
->blkif
->st_oo_req
);
323 VBD_SHOW(rd_req
, "%llu\n", be
->blkif
->st_rd_req
);
324 VBD_SHOW(wr_req
, "%llu\n", be
->blkif
->st_wr_req
);
325 VBD_SHOW(f_req
, "%llu\n", be
->blkif
->st_f_req
);
326 VBD_SHOW(ds_req
, "%llu\n", be
->blkif
->st_ds_req
);
327 VBD_SHOW(rd_sect
, "%llu\n", be
->blkif
->st_rd_sect
);
328 VBD_SHOW(wr_sect
, "%llu\n", be
->blkif
->st_wr_sect
);
330 static struct attribute
*xen_vbdstat_attrs
[] = {
331 &dev_attr_oo_req
.attr
,
332 &dev_attr_rd_req
.attr
,
333 &dev_attr_wr_req
.attr
,
334 &dev_attr_f_req
.attr
,
335 &dev_attr_ds_req
.attr
,
336 &dev_attr_rd_sect
.attr
,
337 &dev_attr_wr_sect
.attr
,
341 static struct attribute_group xen_vbdstat_group
= {
342 .name
= "statistics",
343 .attrs
= xen_vbdstat_attrs
,
346 VBD_SHOW(physical_device
, "%x:%x\n", be
->major
, be
->minor
);
347 VBD_SHOW(mode
, "%s\n", be
->mode
);
349 static int xenvbd_sysfs_addif(struct xenbus_device
*dev
)
353 error
= device_create_file(&dev
->dev
, &dev_attr_physical_device
);
357 error
= device_create_file(&dev
->dev
, &dev_attr_mode
);
361 error
= sysfs_create_group(&dev
->dev
.kobj
, &xen_vbdstat_group
);
367 fail3
: sysfs_remove_group(&dev
->dev
.kobj
, &xen_vbdstat_group
);
368 fail2
: device_remove_file(&dev
->dev
, &dev_attr_mode
);
369 fail1
: device_remove_file(&dev
->dev
, &dev_attr_physical_device
);
373 static void xenvbd_sysfs_delif(struct xenbus_device
*dev
)
375 sysfs_remove_group(&dev
->dev
.kobj
, &xen_vbdstat_group
);
376 device_remove_file(&dev
->dev
, &dev_attr_mode
);
377 device_remove_file(&dev
->dev
, &dev_attr_physical_device
);
381 static void xen_vbd_free(struct xen_vbd
*vbd
)
384 blkdev_put(vbd
->bdev
, vbd
->readonly
? FMODE_READ
: FMODE_WRITE
);
388 static int xen_vbd_create(struct xen_blkif
*blkif
, blkif_vdev_t handle
,
389 unsigned major
, unsigned minor
, int readonly
,
393 struct block_device
*bdev
;
394 struct request_queue
*q
;
397 vbd
->handle
= handle
;
398 vbd
->readonly
= readonly
;
401 vbd
->pdevice
= MKDEV(major
, minor
);
403 bdev
= blkdev_get_by_dev(vbd
->pdevice
, vbd
->readonly
?
404 FMODE_READ
: FMODE_WRITE
, NULL
);
407 DPRINTK("xen_vbd_create: device %08x could not be opened.\n",
413 if (vbd
->bdev
->bd_disk
== NULL
) {
414 DPRINTK("xen_vbd_create: device %08x doesn't exist.\n",
419 vbd
->size
= vbd_sz(vbd
);
421 if (vbd
->bdev
->bd_disk
->flags
& GENHD_FL_CD
|| cdrom
)
422 vbd
->type
|= VDISK_CDROM
;
423 if (vbd
->bdev
->bd_disk
->flags
& GENHD_FL_REMOVABLE
)
424 vbd
->type
|= VDISK_REMOVABLE
;
426 q
= bdev_get_queue(bdev
);
427 if (q
&& q
->flush_flags
)
428 vbd
->flush_support
= true;
430 if (q
&& blk_queue_secdiscard(q
))
431 vbd
->discard_secure
= true;
433 DPRINTK("Successful creation of handle=%04x (dom=%u)\n",
434 handle
, blkif
->domid
);
437 static int xen_blkbk_remove(struct xenbus_device
*dev
)
439 struct backend_info
*be
= dev_get_drvdata(&dev
->dev
);
443 if (be
->major
|| be
->minor
)
444 xenvbd_sysfs_delif(dev
);
446 if (be
->backend_watch
.node
) {
447 unregister_xenbus_watch(&be
->backend_watch
);
448 kfree(be
->backend_watch
.node
);
449 be
->backend_watch
.node
= NULL
;
453 xen_blkif_disconnect(be
->blkif
);
454 xen_vbd_free(&be
->blkif
->vbd
);
455 xen_blkif_free(be
->blkif
);
461 dev_set_drvdata(&dev
->dev
, NULL
);
465 int xen_blkbk_flush_diskcache(struct xenbus_transaction xbt
,
466 struct backend_info
*be
, int state
)
468 struct xenbus_device
*dev
= be
->dev
;
471 err
= xenbus_printf(xbt
, dev
->nodename
, "feature-flush-cache",
474 dev_warn(&dev
->dev
, "writing feature-flush-cache (%d)", err
);
479 static void xen_blkbk_discard(struct xenbus_transaction xbt
, struct backend_info
*be
)
481 struct xenbus_device
*dev
= be
->dev
;
482 struct xen_blkif
*blkif
= be
->blkif
;
485 struct block_device
*bdev
= be
->blkif
->vbd
.bdev
;
486 struct request_queue
*q
= bdev_get_queue(bdev
);
488 if (blk_queue_discard(q
)) {
489 err
= xenbus_printf(xbt
, dev
->nodename
,
490 "discard-granularity", "%u",
491 q
->limits
.discard_granularity
);
493 dev_warn(&dev
->dev
, "writing discard-granularity (%d)", err
);
496 err
= xenbus_printf(xbt
, dev
->nodename
,
497 "discard-alignment", "%u",
498 q
->limits
.discard_alignment
);
500 dev_warn(&dev
->dev
, "writing discard-alignment (%d)", err
);
505 err
= xenbus_printf(xbt
, dev
->nodename
,
506 "discard-secure", "%d",
507 blkif
->vbd
.discard_secure
);
509 dev_warn(&dev
->dev
, "writing discard-secure (%d)", err
);
513 err
= xenbus_printf(xbt
, dev
->nodename
, "feature-discard",
516 dev_warn(&dev
->dev
, "writing feature-discard (%d)", err
);
518 int xen_blkbk_barrier(struct xenbus_transaction xbt
,
519 struct backend_info
*be
, int state
)
521 struct xenbus_device
*dev
= be
->dev
;
524 err
= xenbus_printf(xbt
, dev
->nodename
, "feature-barrier",
527 dev_warn(&dev
->dev
, "writing feature-barrier (%d)", err
);
533 * Entry point to this code when a new device is created. Allocate the basic
534 * structures, and watch the store waiting for the hotplug scripts to tell us
535 * the device's physical major and minor numbers. Switch to InitWait.
537 static int xen_blkbk_probe(struct xenbus_device
*dev
,
538 const struct xenbus_device_id
*id
)
541 struct backend_info
*be
= kzalloc(sizeof(struct backend_info
),
544 xenbus_dev_fatal(dev
, -ENOMEM
,
545 "allocating backend structure");
549 dev_set_drvdata(&dev
->dev
, be
);
551 be
->blkif
= xen_blkif_alloc(dev
->otherend_id
);
552 if (IS_ERR(be
->blkif
)) {
553 err
= PTR_ERR(be
->blkif
);
555 xenbus_dev_fatal(dev
, err
, "creating block interface");
559 /* setup back pointer */
562 err
= xenbus_watch_pathfmt(dev
, &be
->backend_watch
, backend_changed
,
563 "%s/%s", dev
->nodename
, "physical-device");
567 err
= xenbus_switch_state(dev
, XenbusStateInitWait
);
575 xen_blkbk_remove(dev
);
581 * Callback received when the hotplug scripts have placed the physical-device
582 * node. Read it and the mode node, and create a vbd. If the frontend is
585 static void backend_changed(struct xenbus_watch
*watch
,
586 const char **vec
, unsigned int len
)
591 struct backend_info
*be
592 = container_of(watch
, struct backend_info
, backend_watch
);
593 struct xenbus_device
*dev
= be
->dev
;
595 unsigned long handle
;
600 err
= xenbus_scanf(XBT_NIL
, dev
->nodename
, "physical-device", "%x:%x",
602 if (XENBUS_EXIST_ERR(err
)) {
604 * Since this watch will fire once immediately after it is
605 * registered, we expect this. Ignore it, and wait for the
611 xenbus_dev_fatal(dev
, err
, "reading physical-device");
615 if (be
->major
| be
->minor
) {
616 if (be
->major
!= major
|| be
->minor
!= minor
)
617 pr_warn(DRV_PFX
"changing physical device (from %x:%x to %x:%x) not supported.\n",
618 be
->major
, be
->minor
, major
, minor
);
622 be
->mode
= xenbus_read(XBT_NIL
, dev
->nodename
, "mode", NULL
);
623 if (IS_ERR(be
->mode
)) {
624 err
= PTR_ERR(be
->mode
);
626 xenbus_dev_fatal(dev
, err
, "reading mode");
630 device_type
= xenbus_read(XBT_NIL
, dev
->otherend
, "device-type", NULL
);
631 if (!IS_ERR(device_type
)) {
632 cdrom
= strcmp(device_type
, "cdrom") == 0;
636 /* Front end dir is a number, which is used as the handle. */
637 err
= kstrtoul(strrchr(dev
->otherend
, '/') + 1, 0, &handle
);
644 err
= xen_vbd_create(be
->blkif
, handle
, major
, minor
,
645 !strchr(be
->mode
, 'w'), cdrom
);
648 xenbus_dev_fatal(dev
, err
, "creating vbd structure");
650 err
= xenvbd_sysfs_addif(dev
);
652 xen_vbd_free(&be
->blkif
->vbd
);
653 xenbus_dev_fatal(dev
, err
, "creating sysfs entries");
663 /* We're potentially connected now */
664 xen_update_blkif_status(be
->blkif
);
670 * Callback received when the frontend's state changes.
672 static void frontend_changed(struct xenbus_device
*dev
,
673 enum xenbus_state frontend_state
)
675 struct backend_info
*be
= dev_get_drvdata(&dev
->dev
);
678 DPRINTK("%s", xenbus_strstate(frontend_state
));
680 switch (frontend_state
) {
681 case XenbusStateInitialising
:
682 if (dev
->state
== XenbusStateClosed
) {
683 pr_info(DRV_PFX
"%s: prepare for reconnect\n",
685 xenbus_switch_state(dev
, XenbusStateInitWait
);
689 case XenbusStateInitialised
:
690 case XenbusStateConnected
:
692 * Ensure we connect even when two watches fire in
693 * close succession and we miss the intermediate value
696 if (dev
->state
== XenbusStateConnected
)
700 * Enforce precondition before potential leak point.
701 * xen_blkif_disconnect() is idempotent.
703 xen_blkif_disconnect(be
->blkif
);
705 err
= connect_ring(be
);
708 xen_update_blkif_status(be
->blkif
);
711 case XenbusStateClosing
:
712 xenbus_switch_state(dev
, XenbusStateClosing
);
715 case XenbusStateClosed
:
716 xen_blkif_disconnect(be
->blkif
);
717 xenbus_switch_state(dev
, XenbusStateClosed
);
718 if (xenbus_dev_is_online(dev
))
720 /* fall through if not online */
721 case XenbusStateUnknown
:
722 /* implies xen_blkif_disconnect() via xen_blkbk_remove() */
723 device_unregister(&dev
->dev
);
727 xenbus_dev_fatal(dev
, -EINVAL
, "saw state %d at frontend",
734 /* ** Connection ** */
738 * Write the physical details regarding the block device to the store, and
739 * switch to Connected state.
741 static void connect(struct backend_info
*be
)
743 struct xenbus_transaction xbt
;
745 struct xenbus_device
*dev
= be
->dev
;
747 DPRINTK("%s", dev
->otherend
);
749 /* Supply the information about the device the frontend needs */
751 err
= xenbus_transaction_start(&xbt
);
753 xenbus_dev_fatal(dev
, err
, "starting transaction");
757 /* If we can't advertise it is OK. */
758 xen_blkbk_flush_diskcache(xbt
, be
, be
->blkif
->vbd
.flush_support
);
760 xen_blkbk_discard(xbt
, be
);
762 xen_blkbk_barrier(xbt
, be
, be
->blkif
->vbd
.flush_support
);
764 err
= xenbus_printf(xbt
, dev
->nodename
, "feature-persistent", "%u", 1);
766 xenbus_dev_fatal(dev
, err
, "writing %s/feature-persistent",
770 err
= xenbus_printf(xbt
, dev
->nodename
, "feature-max-indirect-segments", "%u",
771 MAX_INDIRECT_SEGMENTS
);
773 dev_warn(&dev
->dev
, "writing %s/feature-max-indirect-segments (%d)",
776 err
= xenbus_printf(xbt
, dev
->nodename
, "sectors", "%llu",
777 (unsigned long long)vbd_sz(&be
->blkif
->vbd
));
779 xenbus_dev_fatal(dev
, err
, "writing %s/sectors",
784 /* FIXME: use a typename instead */
785 err
= xenbus_printf(xbt
, dev
->nodename
, "info", "%u",
786 be
->blkif
->vbd
.type
|
787 (be
->blkif
->vbd
.readonly
? VDISK_READONLY
: 0));
789 xenbus_dev_fatal(dev
, err
, "writing %s/info",
793 err
= xenbus_printf(xbt
, dev
->nodename
, "sector-size", "%lu",
795 bdev_logical_block_size(be
->blkif
->vbd
.bdev
));
797 xenbus_dev_fatal(dev
, err
, "writing %s/sector-size",
801 err
= xenbus_printf(xbt
, dev
->nodename
, "physical-sector-size", "%u",
802 bdev_physical_block_size(be
->blkif
->vbd
.bdev
));
804 xenbus_dev_error(dev
, err
, "writing %s/physical-sector-size",
807 err
= xenbus_transaction_end(xbt
, 0);
811 xenbus_dev_fatal(dev
, err
, "ending transaction");
813 err
= xenbus_switch_state(dev
, XenbusStateConnected
);
815 xenbus_dev_fatal(dev
, err
, "%s: switching to Connected state",
820 xenbus_transaction_end(xbt
, 1);
824 static int connect_ring(struct backend_info
*be
)
826 struct xenbus_device
*dev
= be
->dev
;
827 unsigned long ring_ref
;
829 unsigned int pers_grants
;
830 char protocol
[64] = "";
833 DPRINTK("%s", dev
->otherend
);
835 err
= xenbus_gather(XBT_NIL
, dev
->otherend
, "ring-ref", "%lu",
836 &ring_ref
, "event-channel", "%u", &evtchn
, NULL
);
838 xenbus_dev_fatal(dev
, err
,
839 "reading %s/ring-ref and event-channel",
844 be
->blkif
->blk_protocol
= BLKIF_PROTOCOL_NATIVE
;
845 err
= xenbus_gather(XBT_NIL
, dev
->otherend
, "protocol",
846 "%63s", protocol
, NULL
);
848 strcpy(protocol
, "unspecified, assuming native");
849 else if (0 == strcmp(protocol
, XEN_IO_PROTO_ABI_NATIVE
))
850 be
->blkif
->blk_protocol
= BLKIF_PROTOCOL_NATIVE
;
851 else if (0 == strcmp(protocol
, XEN_IO_PROTO_ABI_X86_32
))
852 be
->blkif
->blk_protocol
= BLKIF_PROTOCOL_X86_32
;
853 else if (0 == strcmp(protocol
, XEN_IO_PROTO_ABI_X86_64
))
854 be
->blkif
->blk_protocol
= BLKIF_PROTOCOL_X86_64
;
856 xenbus_dev_fatal(dev
, err
, "unknown fe protocol %s", protocol
);
859 err
= xenbus_gather(XBT_NIL
, dev
->otherend
,
860 "feature-persistent", "%u",
865 be
->blkif
->vbd
.feature_gnt_persistent
= pers_grants
;
866 be
->blkif
->vbd
.overflow_max_grants
= 0;
868 pr_info(DRV_PFX
"ring-ref %ld, event-channel %d, protocol %d (%s) %s\n",
869 ring_ref
, evtchn
, be
->blkif
->blk_protocol
, protocol
,
870 pers_grants
? "persistent grants" : "");
872 /* Map the shared frame, irq etc. */
873 err
= xen_blkif_map(be
->blkif
, ring_ref
, evtchn
);
875 xenbus_dev_fatal(dev
, err
, "mapping ring-ref %lu port %u",
884 /* ** Driver Registration ** */
887 static const struct xenbus_device_id xen_blkbk_ids
[] = {
893 static DEFINE_XENBUS_DRIVER(xen_blkbk
, ,
894 .probe
= xen_blkbk_probe
,
895 .remove
= xen_blkbk_remove
,
896 .otherend_changed
= frontend_changed
900 int xen_blkif_xenbus_init(void)
902 return xenbus_register_backend(&xen_blkbk_driver
);