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.
17 #define pr_fmt(fmt) "xen-blkback: " fmt
20 #include <linux/module.h>
21 #include <linux/kthread.h>
22 #include <xen/events.h>
23 #include <xen/grant_table.h>
26 /* On the XenBus the max length of 'ring-ref%u'. */
27 #define RINGREF_NAME_LEN (20)
30 struct xenbus_device *dev;
31 struct xen_blkif *blkif;
32 struct xenbus_watch backend_watch;
38 static struct kmem_cache *xen_blkif_cachep;
39 static void connect(struct backend_info *);
40 static int connect_ring(struct backend_info *);
41 static void backend_changed(struct xenbus_watch *, const char *,
43 static void xen_blkif_free(struct xen_blkif *blkif);
44 static void xen_vbd_free(struct xen_vbd *vbd);
46 struct xenbus_device *xen_blkbk_xenbus(struct backend_info *be)
52 * The last request could free the device from softirq context and
53 * xen_blkif_free() can sleep.
55 static void xen_blkif_deferred_free(struct work_struct *work)
57 struct xen_blkif *blkif;
59 blkif = container_of(work, struct xen_blkif, free_work);
60 xen_blkif_free(blkif);
63 static int blkback_name(struct xen_blkif *blkif, char *buf)
65 char *devpath, *devname;
66 struct xenbus_device *dev = blkif->be->dev;
68 devpath = xenbus_read(XBT_NIL, dev->nodename, "dev", NULL);
70 return PTR_ERR(devpath);
72 devname = strstr(devpath, "/dev/");
74 devname += strlen("/dev/");
78 snprintf(buf, TASK_COMM_LEN, "%d.%s", blkif->domid, devname);
84 static void xen_update_blkif_status(struct xen_blkif *blkif)
87 char name[TASK_COMM_LEN];
88 struct xen_blkif_ring *ring;
91 /* Not ready to connect? */
92 if (!blkif->rings || !blkif->rings[0].irq || !blkif->vbd.bdev)
95 /* Already connected? */
96 if (blkif->be->dev->state == XenbusStateConnected)
99 /* Attempt to connect: exit if we fail to. */
101 if (blkif->be->dev->state != XenbusStateConnected)
104 err = blkback_name(blkif, name);
106 xenbus_dev_error(blkif->be->dev, err, "get blkback dev name");
110 err = filemap_write_and_wait(blkif->vbd.bdev->bd_inode->i_mapping);
112 xenbus_dev_error(blkif->be->dev, err, "block flush");
115 invalidate_inode_pages2(blkif->vbd.bdev->bd_inode->i_mapping);
117 for (i = 0; i < blkif->nr_rings; i++) {
118 ring = &blkif->rings[i];
119 ring->xenblkd = kthread_run(xen_blkif_schedule, ring, "%s-%d", name, i);
120 if (IS_ERR(ring->xenblkd)) {
121 err = PTR_ERR(ring->xenblkd);
122 ring->xenblkd = NULL;
123 xenbus_dev_fatal(blkif->be->dev, err,
124 "start %s-%d xenblkd", name, i);
132 ring = &blkif->rings[i];
133 kthread_stop(ring->xenblkd);
138 static int xen_blkif_alloc_rings(struct xen_blkif *blkif)
142 blkif->rings = kcalloc(blkif->nr_rings, sizeof(struct xen_blkif_ring),
147 for (r = 0; r < blkif->nr_rings; r++) {
148 struct xen_blkif_ring *ring = &blkif->rings[r];
150 spin_lock_init(&ring->blk_ring_lock);
151 init_waitqueue_head(&ring->wq);
152 INIT_LIST_HEAD(&ring->pending_free);
153 INIT_LIST_HEAD(&ring->persistent_purge_list);
154 INIT_WORK(&ring->persistent_purge_work, xen_blkbk_unmap_purged_grants);
155 spin_lock_init(&ring->free_pages_lock);
156 INIT_LIST_HEAD(&ring->free_pages);
158 spin_lock_init(&ring->pending_free_lock);
159 init_waitqueue_head(&ring->pending_free_wq);
160 init_waitqueue_head(&ring->shutdown_wq);
162 ring->st_print = jiffies;
169 static struct xen_blkif *xen_blkif_alloc(domid_t domid)
171 struct xen_blkif *blkif;
173 BUILD_BUG_ON(MAX_INDIRECT_PAGES > BLKIF_MAX_INDIRECT_PAGES_PER_REQUEST);
175 blkif = kmem_cache_zalloc(xen_blkif_cachep, GFP_KERNEL);
177 return ERR_PTR(-ENOMEM);
179 blkif->domid = domid;
180 atomic_set(&blkif->refcnt, 1);
181 init_completion(&blkif->drain_complete);
182 INIT_WORK(&blkif->free_work, xen_blkif_deferred_free);
187 static int xen_blkif_map(struct xen_blkif_ring *ring, grant_ref_t *gref,
188 unsigned int nr_grefs, unsigned int evtchn)
191 struct xen_blkif *blkif = ring->blkif;
193 /* Already connected through? */
197 err = xenbus_map_ring_valloc(blkif->be->dev, gref, nr_grefs,
202 switch (blkif->blk_protocol) {
203 case BLKIF_PROTOCOL_NATIVE:
205 struct blkif_sring *sring;
206 sring = (struct blkif_sring *)ring->blk_ring;
207 BACK_RING_INIT(&ring->blk_rings.native, sring,
208 XEN_PAGE_SIZE * nr_grefs);
211 case BLKIF_PROTOCOL_X86_32:
213 struct blkif_x86_32_sring *sring_x86_32;
214 sring_x86_32 = (struct blkif_x86_32_sring *)ring->blk_ring;
215 BACK_RING_INIT(&ring->blk_rings.x86_32, sring_x86_32,
216 XEN_PAGE_SIZE * nr_grefs);
219 case BLKIF_PROTOCOL_X86_64:
221 struct blkif_x86_64_sring *sring_x86_64;
222 sring_x86_64 = (struct blkif_x86_64_sring *)ring->blk_ring;
223 BACK_RING_INIT(&ring->blk_rings.x86_64, sring_x86_64,
224 XEN_PAGE_SIZE * nr_grefs);
231 err = bind_interdomain_evtchn_to_irqhandler(blkif->domid, evtchn,
233 "blkif-backend", ring);
235 xenbus_unmap_ring_vfree(blkif->be->dev, ring->blk_ring);
236 ring->blk_rings.common.sring = NULL;
244 static int xen_blkif_disconnect(struct xen_blkif *blkif)
246 struct pending_req *req, *n;
250 for (r = 0; r < blkif->nr_rings; r++) {
251 struct xen_blkif_ring *ring = &blkif->rings[r];
258 kthread_stop(ring->xenblkd);
259 wake_up(&ring->shutdown_wq);
262 /* The above kthread_stop() guarantees that at this point we
263 * don't have any discard_io or other_io requests. So, checking
264 * for inflight IO is enough.
266 if (atomic_read(&ring->inflight) > 0) {
272 unbind_from_irqhandler(ring->irq, ring);
276 if (ring->blk_rings.common.sring) {
277 xenbus_unmap_ring_vfree(blkif->be->dev, ring->blk_ring);
278 ring->blk_rings.common.sring = NULL;
281 /* Remove all persistent grants and the cache of ballooned pages. */
282 xen_blkbk_free_caches(ring);
284 /* Check that there is no request in use */
285 list_for_each_entry_safe(req, n, &ring->pending_free, free_list) {
286 list_del(&req->free_list);
288 for (j = 0; j < MAX_INDIRECT_SEGMENTS; j++)
289 kfree(req->segments[j]);
291 for (j = 0; j < MAX_INDIRECT_PAGES; j++)
292 kfree(req->indirect_pages[j]);
298 BUG_ON(atomic_read(&ring->persistent_gnt_in_use) != 0);
299 BUG_ON(!list_empty(&ring->persistent_purge_list));
300 BUG_ON(!RB_EMPTY_ROOT(&ring->persistent_gnts));
301 BUG_ON(!list_empty(&ring->free_pages));
302 BUG_ON(ring->free_pages_num != 0);
303 BUG_ON(ring->persistent_gnt_c != 0);
304 WARN_ON(i != (XEN_BLKIF_REQS_PER_PAGE * blkif->nr_ring_pages));
305 ring->active = false;
310 blkif->nr_ring_pages = 0;
312 * blkif->rings was allocated in connect_ring, so we should free it in
322 static void xen_blkif_free(struct xen_blkif *blkif)
324 WARN_ON(xen_blkif_disconnect(blkif));
325 xen_vbd_free(&blkif->vbd);
326 kfree(blkif->be->mode);
329 /* Make sure everything is drained before shutting down */
330 kmem_cache_free(xen_blkif_cachep, blkif);
333 int __init xen_blkif_interface_init(void)
335 xen_blkif_cachep = kmem_cache_create("blkif_cache",
336 sizeof(struct xen_blkif),
338 if (!xen_blkif_cachep)
345 * sysfs interface for VBD I/O requests
348 #define VBD_SHOW_ALLRING(name, format) \
349 static ssize_t show_##name(struct device *_dev, \
350 struct device_attribute *attr, \
353 struct xenbus_device *dev = to_xenbus_device(_dev); \
354 struct backend_info *be = dev_get_drvdata(&dev->dev); \
355 struct xen_blkif *blkif = be->blkif; \
357 unsigned long long result = 0; \
362 for (i = 0; i < blkif->nr_rings; i++) { \
363 struct xen_blkif_ring *ring = &blkif->rings[i]; \
365 result += ring->st_##name; \
369 return sprintf(buf, format, result); \
371 static DEVICE_ATTR(name, 0444, show_##name, NULL)
373 VBD_SHOW_ALLRING(oo_req, "%llu\n");
374 VBD_SHOW_ALLRING(rd_req, "%llu\n");
375 VBD_SHOW_ALLRING(wr_req, "%llu\n");
376 VBD_SHOW_ALLRING(f_req, "%llu\n");
377 VBD_SHOW_ALLRING(ds_req, "%llu\n");
378 VBD_SHOW_ALLRING(rd_sect, "%llu\n");
379 VBD_SHOW_ALLRING(wr_sect, "%llu\n");
381 static struct attribute *xen_vbdstat_attrs[] = {
382 &dev_attr_oo_req.attr,
383 &dev_attr_rd_req.attr,
384 &dev_attr_wr_req.attr,
385 &dev_attr_f_req.attr,
386 &dev_attr_ds_req.attr,
387 &dev_attr_rd_sect.attr,
388 &dev_attr_wr_sect.attr,
392 static const struct attribute_group xen_vbdstat_group = {
393 .name = "statistics",
394 .attrs = xen_vbdstat_attrs,
397 #define VBD_SHOW(name, format, args...) \
398 static ssize_t show_##name(struct device *_dev, \
399 struct device_attribute *attr, \
402 struct xenbus_device *dev = to_xenbus_device(_dev); \
403 struct backend_info *be = dev_get_drvdata(&dev->dev); \
405 return sprintf(buf, format, ##args); \
407 static DEVICE_ATTR(name, 0444, show_##name, NULL)
409 VBD_SHOW(physical_device, "%x:%x\n", be->major, be->minor);
410 VBD_SHOW(mode, "%s\n", be->mode);
412 static int xenvbd_sysfs_addif(struct xenbus_device *dev)
416 error = device_create_file(&dev->dev, &dev_attr_physical_device);
420 error = device_create_file(&dev->dev, &dev_attr_mode);
424 error = sysfs_create_group(&dev->dev.kobj, &xen_vbdstat_group);
430 fail3: sysfs_remove_group(&dev->dev.kobj, &xen_vbdstat_group);
431 fail2: device_remove_file(&dev->dev, &dev_attr_mode);
432 fail1: device_remove_file(&dev->dev, &dev_attr_physical_device);
436 static void xenvbd_sysfs_delif(struct xenbus_device *dev)
438 sysfs_remove_group(&dev->dev.kobj, &xen_vbdstat_group);
439 device_remove_file(&dev->dev, &dev_attr_mode);
440 device_remove_file(&dev->dev, &dev_attr_physical_device);
444 static void xen_vbd_free(struct xen_vbd *vbd)
447 blkdev_put(vbd->bdev, vbd->readonly ? FMODE_READ : FMODE_WRITE);
451 static int xen_vbd_create(struct xen_blkif *blkif, blkif_vdev_t handle,
452 unsigned major, unsigned minor, int readonly,
456 struct block_device *bdev;
457 struct request_queue *q;
460 vbd->handle = handle;
461 vbd->readonly = readonly;
464 vbd->pdevice = MKDEV(major, minor);
466 bdev = blkdev_get_by_dev(vbd->pdevice, vbd->readonly ?
467 FMODE_READ : FMODE_WRITE, NULL);
470 pr_warn("xen_vbd_create: device %08x could not be opened\n",
476 if (vbd->bdev->bd_disk == NULL) {
477 pr_warn("xen_vbd_create: device %08x doesn't exist\n",
482 vbd->size = vbd_sz(vbd);
484 if (vbd->bdev->bd_disk->flags & GENHD_FL_CD || cdrom)
485 vbd->type |= VDISK_CDROM;
486 if (vbd->bdev->bd_disk->flags & GENHD_FL_REMOVABLE)
487 vbd->type |= VDISK_REMOVABLE;
489 q = bdev_get_queue(bdev);
490 if (q && test_bit(QUEUE_FLAG_WC, &q->queue_flags))
491 vbd->flush_support = true;
493 if (q && blk_queue_secure_erase(q))
494 vbd->discard_secure = true;
496 pr_debug("Successful creation of handle=%04x (dom=%u)\n",
497 handle, blkif->domid);
500 static int xen_blkbk_remove(struct xenbus_device *dev)
502 struct backend_info *be = dev_get_drvdata(&dev->dev);
504 pr_debug("%s %p %d\n", __func__, dev, dev->otherend_id);
506 if (be->major || be->minor)
507 xenvbd_sysfs_delif(dev);
509 if (be->backend_watch.node) {
510 unregister_xenbus_watch(&be->backend_watch);
511 kfree(be->backend_watch.node);
512 be->backend_watch.node = NULL;
515 dev_set_drvdata(&dev->dev, NULL);
518 xen_blkif_disconnect(be->blkif);
520 /* Put the reference we set in xen_blkif_alloc(). */
521 xen_blkif_put(be->blkif);
527 int xen_blkbk_flush_diskcache(struct xenbus_transaction xbt,
528 struct backend_info *be, int state)
530 struct xenbus_device *dev = be->dev;
533 err = xenbus_printf(xbt, dev->nodename, "feature-flush-cache",
536 dev_warn(&dev->dev, "writing feature-flush-cache (%d)", err);
541 static void xen_blkbk_discard(struct xenbus_transaction xbt, struct backend_info *be)
543 struct xenbus_device *dev = be->dev;
544 struct xen_blkif *blkif = be->blkif;
547 struct block_device *bdev = be->blkif->vbd.bdev;
548 struct request_queue *q = bdev_get_queue(bdev);
550 if (!xenbus_read_unsigned(dev->nodename, "discard-enable", 1))
553 if (blk_queue_discard(q)) {
554 err = xenbus_printf(xbt, dev->nodename,
555 "discard-granularity", "%u",
556 q->limits.discard_granularity);
558 dev_warn(&dev->dev, "writing discard-granularity (%d)", err);
561 err = xenbus_printf(xbt, dev->nodename,
562 "discard-alignment", "%u",
563 q->limits.discard_alignment);
565 dev_warn(&dev->dev, "writing discard-alignment (%d)", err);
570 err = xenbus_printf(xbt, dev->nodename,
571 "discard-secure", "%d",
572 blkif->vbd.discard_secure);
574 dev_warn(&dev->dev, "writing discard-secure (%d)", err);
578 err = xenbus_printf(xbt, dev->nodename, "feature-discard",
581 dev_warn(&dev->dev, "writing feature-discard (%d)", err);
583 int xen_blkbk_barrier(struct xenbus_transaction xbt,
584 struct backend_info *be, int state)
586 struct xenbus_device *dev = be->dev;
589 err = xenbus_printf(xbt, dev->nodename, "feature-barrier",
592 dev_warn(&dev->dev, "writing feature-barrier (%d)", err);
598 * Entry point to this code when a new device is created. Allocate the basic
599 * structures, and watch the store waiting for the hotplug scripts to tell us
600 * the device's physical major and minor numbers. Switch to InitWait.
602 static int xen_blkbk_probe(struct xenbus_device *dev,
603 const struct xenbus_device_id *id)
606 struct backend_info *be = kzalloc(sizeof(struct backend_info),
609 /* match the pr_debug in xen_blkbk_remove */
610 pr_debug("%s %p %d\n", __func__, dev, dev->otherend_id);
613 xenbus_dev_fatal(dev, -ENOMEM,
614 "allocating backend structure");
618 dev_set_drvdata(&dev->dev, be);
620 be->blkif = xen_blkif_alloc(dev->otherend_id);
621 if (IS_ERR(be->blkif)) {
622 err = PTR_ERR(be->blkif);
624 xenbus_dev_fatal(dev, err, "creating block interface");
628 err = xenbus_printf(XBT_NIL, dev->nodename,
629 "feature-max-indirect-segments", "%u",
630 MAX_INDIRECT_SEGMENTS);
633 "writing %s/feature-max-indirect-segments (%d)",
636 /* Multi-queue: advertise how many queues are supported by us.*/
637 err = xenbus_printf(XBT_NIL, dev->nodename,
638 "multi-queue-max-queues", "%u", xenblk_max_queues);
640 pr_warn("Error writing multi-queue-max-queues\n");
642 /* setup back pointer */
645 err = xenbus_watch_pathfmt(dev, &be->backend_watch, backend_changed,
646 "%s/%s", dev->nodename, "physical-device");
650 err = xenbus_printf(XBT_NIL, dev->nodename, "max-ring-page-order", "%u",
651 xen_blkif_max_ring_order);
653 pr_warn("%s write out 'max-ring-page-order' failed\n", __func__);
655 err = xenbus_switch_state(dev, XenbusStateInitWait);
662 pr_warn("%s failed\n", __func__);
663 xen_blkbk_remove(dev);
669 * Callback received when the hotplug scripts have placed the physical-device
670 * node. Read it and the mode node, and create a vbd. If the frontend is
673 static void backend_changed(struct xenbus_watch *watch,
674 const char *path, const char *token)
679 struct backend_info *be
680 = container_of(watch, struct backend_info, backend_watch);
681 struct xenbus_device *dev = be->dev;
683 unsigned long handle;
686 pr_debug("%s %p %d\n", __func__, dev, dev->otherend_id);
688 err = xenbus_scanf(XBT_NIL, dev->nodename, "physical-device", "%x:%x",
690 if (XENBUS_EXIST_ERR(err)) {
692 * Since this watch will fire once immediately after it is
693 * registered, we expect this. Ignore it, and wait for the
699 xenbus_dev_fatal(dev, err, "reading physical-device");
703 if (be->major | be->minor) {
704 if (be->major != major || be->minor != minor)
705 pr_warn("changing physical device (from %x:%x to %x:%x) not supported.\n",
706 be->major, be->minor, major, minor);
710 be->mode = xenbus_read(XBT_NIL, dev->nodename, "mode", NULL);
711 if (IS_ERR(be->mode)) {
712 err = PTR_ERR(be->mode);
714 xenbus_dev_fatal(dev, err, "reading mode");
718 device_type = xenbus_read(XBT_NIL, dev->otherend, "device-type", NULL);
719 if (!IS_ERR(device_type)) {
720 cdrom = strcmp(device_type, "cdrom") == 0;
724 /* Front end dir is a number, which is used as the handle. */
725 err = kstrtoul(strrchr(dev->otherend, '/') + 1, 0, &handle);
735 err = xen_vbd_create(be->blkif, handle, major, minor,
736 !strchr(be->mode, 'w'), cdrom);
739 xenbus_dev_fatal(dev, err, "creating vbd structure");
741 err = xenvbd_sysfs_addif(dev);
743 xen_vbd_free(&be->blkif->vbd);
744 xenbus_dev_fatal(dev, err, "creating sysfs entries");
754 /* We're potentially connected now */
755 xen_update_blkif_status(be->blkif);
761 * Callback received when the frontend's state changes.
763 static void frontend_changed(struct xenbus_device *dev,
764 enum xenbus_state frontend_state)
766 struct backend_info *be = dev_get_drvdata(&dev->dev);
769 pr_debug("%s %p %s\n", __func__, dev, xenbus_strstate(frontend_state));
771 switch (frontend_state) {
772 case XenbusStateInitialising:
773 if (dev->state == XenbusStateClosed) {
774 pr_info("%s: prepare for reconnect\n", dev->nodename);
775 xenbus_switch_state(dev, XenbusStateInitWait);
779 case XenbusStateInitialised:
780 case XenbusStateConnected:
782 * Ensure we connect even when two watches fire in
783 * close succession and we miss the intermediate value
786 if (dev->state == XenbusStateConnected)
790 * Enforce precondition before potential leak point.
791 * xen_blkif_disconnect() is idempotent.
793 err = xen_blkif_disconnect(be->blkif);
795 xenbus_dev_fatal(dev, err, "pending I/O");
799 err = connect_ring(be);
802 * Clean up so that memory resources can be used by
803 * other devices. connect_ring reported already error.
805 xen_blkif_disconnect(be->blkif);
808 xen_update_blkif_status(be->blkif);
811 case XenbusStateClosing:
812 xenbus_switch_state(dev, XenbusStateClosing);
815 case XenbusStateClosed:
816 xen_blkif_disconnect(be->blkif);
817 xenbus_switch_state(dev, XenbusStateClosed);
818 if (xenbus_dev_is_online(dev))
822 case XenbusStateUnknown:
823 /* implies xen_blkif_disconnect() via xen_blkbk_remove() */
824 device_unregister(&dev->dev);
828 xenbus_dev_fatal(dev, -EINVAL, "saw state %d at frontend",
835 /* ** Connection ** */
839 * Write the physical details regarding the block device to the store, and
840 * switch to Connected state.
842 static void connect(struct backend_info *be)
844 struct xenbus_transaction xbt;
846 struct xenbus_device *dev = be->dev;
848 pr_debug("%s %s\n", __func__, dev->otherend);
850 /* Supply the information about the device the frontend needs */
852 err = xenbus_transaction_start(&xbt);
854 xenbus_dev_fatal(dev, err, "starting transaction");
858 /* If we can't advertise it is OK. */
859 xen_blkbk_flush_diskcache(xbt, be, be->blkif->vbd.flush_support);
861 xen_blkbk_discard(xbt, be);
863 xen_blkbk_barrier(xbt, be, be->blkif->vbd.flush_support);
865 err = xenbus_printf(xbt, dev->nodename, "feature-persistent", "%u", 1);
867 xenbus_dev_fatal(dev, err, "writing %s/feature-persistent",
872 err = xenbus_printf(xbt, dev->nodename, "sectors", "%llu",
873 (unsigned long long)vbd_sz(&be->blkif->vbd));
875 xenbus_dev_fatal(dev, err, "writing %s/sectors",
880 /* FIXME: use a typename instead */
881 err = xenbus_printf(xbt, dev->nodename, "info", "%u",
882 be->blkif->vbd.type |
883 (be->blkif->vbd.readonly ? VDISK_READONLY : 0));
885 xenbus_dev_fatal(dev, err, "writing %s/info",
889 err = xenbus_printf(xbt, dev->nodename, "sector-size", "%lu",
891 bdev_logical_block_size(be->blkif->vbd.bdev));
893 xenbus_dev_fatal(dev, err, "writing %s/sector-size",
897 err = xenbus_printf(xbt, dev->nodename, "physical-sector-size", "%u",
898 bdev_physical_block_size(be->blkif->vbd.bdev));
900 xenbus_dev_error(dev, err, "writing %s/physical-sector-size",
903 err = xenbus_transaction_end(xbt, 0);
907 xenbus_dev_fatal(dev, err, "ending transaction");
909 err = xenbus_switch_state(dev, XenbusStateConnected);
911 xenbus_dev_fatal(dev, err, "%s: switching to Connected state",
916 xenbus_transaction_end(xbt, 1);
920 * Each ring may have multi pages, depends on "ring-page-order".
922 static int read_per_ring_refs(struct xen_blkif_ring *ring, const char *dir)
924 unsigned int ring_ref[XENBUS_MAX_RING_GRANTS];
925 struct pending_req *req, *n;
927 struct xen_blkif *blkif = ring->blkif;
928 struct xenbus_device *dev = blkif->be->dev;
929 unsigned int ring_page_order, nr_grefs, evtchn;
931 err = xenbus_scanf(XBT_NIL, dir, "event-channel", "%u",
935 xenbus_dev_fatal(dev, err, "reading %s/event-channel", dir);
939 err = xenbus_scanf(XBT_NIL, dev->otherend, "ring-page-order", "%u",
942 err = xenbus_scanf(XBT_NIL, dir, "ring-ref", "%u", &ring_ref[0]);
945 xenbus_dev_fatal(dev, err, "reading %s/ring-ref", dir);
952 if (ring_page_order > xen_blkif_max_ring_order) {
954 xenbus_dev_fatal(dev, err, "%s/request %d ring page order exceed max:%d",
955 dir, ring_page_order,
956 xen_blkif_max_ring_order);
960 nr_grefs = 1 << ring_page_order;
961 for (i = 0; i < nr_grefs; i++) {
962 char ring_ref_name[RINGREF_NAME_LEN];
964 snprintf(ring_ref_name, RINGREF_NAME_LEN, "ring-ref%u", i);
965 err = xenbus_scanf(XBT_NIL, dir, ring_ref_name,
969 xenbus_dev_fatal(dev, err, "reading %s/%s",
975 blkif->nr_ring_pages = nr_grefs;
977 for (i = 0; i < nr_grefs * XEN_BLKIF_REQS_PER_PAGE; i++) {
978 req = kzalloc(sizeof(*req), GFP_KERNEL);
981 list_add_tail(&req->free_list, &ring->pending_free);
982 for (j = 0; j < MAX_INDIRECT_SEGMENTS; j++) {
983 req->segments[j] = kzalloc(sizeof(*req->segments[0]), GFP_KERNEL);
984 if (!req->segments[j])
987 for (j = 0; j < MAX_INDIRECT_PAGES; j++) {
988 req->indirect_pages[j] = kzalloc(sizeof(*req->indirect_pages[0]),
990 if (!req->indirect_pages[j])
995 /* Map the shared frame, irq etc. */
996 err = xen_blkif_map(ring, ring_ref, nr_grefs, evtchn);
998 xenbus_dev_fatal(dev, err, "mapping ring-ref port %u", evtchn);
1005 list_for_each_entry_safe(req, n, &ring->pending_free, free_list) {
1006 list_del(&req->free_list);
1007 for (j = 0; j < MAX_INDIRECT_SEGMENTS; j++) {
1008 if (!req->segments[j])
1010 kfree(req->segments[j]);
1012 for (j = 0; j < MAX_INDIRECT_PAGES; j++) {
1013 if (!req->indirect_pages[j])
1015 kfree(req->indirect_pages[j]);
1023 static int connect_ring(struct backend_info *be)
1025 struct xenbus_device *dev = be->dev;
1026 unsigned int pers_grants;
1027 char protocol[64] = "";
1031 const size_t xenstore_path_ext_size = 11; /* sufficient for "/queue-NNN" */
1032 unsigned int requested_num_queues = 0;
1034 pr_debug("%s %s\n", __func__, dev->otherend);
1036 be->blkif->blk_protocol = BLKIF_PROTOCOL_DEFAULT;
1037 err = xenbus_scanf(XBT_NIL, dev->otherend, "protocol",
1040 strcpy(protocol, "unspecified, assuming default");
1041 else if (0 == strcmp(protocol, XEN_IO_PROTO_ABI_NATIVE))
1042 be->blkif->blk_protocol = BLKIF_PROTOCOL_NATIVE;
1043 else if (0 == strcmp(protocol, XEN_IO_PROTO_ABI_X86_32))
1044 be->blkif->blk_protocol = BLKIF_PROTOCOL_X86_32;
1045 else if (0 == strcmp(protocol, XEN_IO_PROTO_ABI_X86_64))
1046 be->blkif->blk_protocol = BLKIF_PROTOCOL_X86_64;
1048 xenbus_dev_fatal(dev, err, "unknown fe protocol %s", protocol);
1051 pers_grants = xenbus_read_unsigned(dev->otherend, "feature-persistent",
1053 be->blkif->vbd.feature_gnt_persistent = pers_grants;
1054 be->blkif->vbd.overflow_max_grants = 0;
1057 * Read the number of hardware queues from frontend.
1059 requested_num_queues = xenbus_read_unsigned(dev->otherend,
1060 "multi-queue-num-queues",
1062 if (requested_num_queues > xenblk_max_queues
1063 || requested_num_queues == 0) {
1064 /* Buggy or malicious guest. */
1065 xenbus_dev_fatal(dev, err,
1066 "guest requested %u queues, exceeding the maximum of %u.",
1067 requested_num_queues, xenblk_max_queues);
1070 be->blkif->nr_rings = requested_num_queues;
1071 if (xen_blkif_alloc_rings(be->blkif))
1074 pr_info("%s: using %d queues, protocol %d (%s) %s\n", dev->nodename,
1075 be->blkif->nr_rings, be->blkif->blk_protocol, protocol,
1076 pers_grants ? "persistent grants" : "");
1078 if (be->blkif->nr_rings == 1)
1079 return read_per_ring_refs(&be->blkif->rings[0], dev->otherend);
1081 xspathsize = strlen(dev->otherend) + xenstore_path_ext_size;
1082 xspath = kmalloc(xspathsize, GFP_KERNEL);
1084 xenbus_dev_fatal(dev, -ENOMEM, "reading ring references");
1088 for (i = 0; i < be->blkif->nr_rings; i++) {
1089 memset(xspath, 0, xspathsize);
1090 snprintf(xspath, xspathsize, "%s/queue-%u", dev->otherend, i);
1091 err = read_per_ring_refs(&be->blkif->rings[i], xspath);
1102 static const struct xenbus_device_id xen_blkbk_ids[] = {
1107 static struct xenbus_driver xen_blkbk_driver = {
1108 .ids = xen_blkbk_ids,
1109 .probe = xen_blkbk_probe,
1110 .remove = xen_blkbk_remove,
1111 .otherend_changed = frontend_changed
1114 int xen_blkif_xenbus_init(void)
1116 return xenbus_register_backend(&xen_blkbk_driver);