Konrad Rzeszutek Wilk | 4d05a28 | 2011-04-14 18:25:47 -0400 | [diff] [blame] | 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 | |
Konrad Rzeszutek Wilk | 4d05a28 | 2011-04-14 18:25:47 -0400 | [diff] [blame] | 15 | */ |
| 16 | |
Tao Chen | 77387b8 | 2015-04-01 15:04:22 +0000 | [diff] [blame] | 17 | #define pr_fmt(fmt) "xen-blkback: " fmt |
| 18 | |
Konrad Rzeszutek Wilk | 4d05a28 | 2011-04-14 18:25:47 -0400 | [diff] [blame] | 19 | #include <stdarg.h> |
| 20 | #include <linux/module.h> |
| 21 | #include <linux/kthread.h> |
Konrad Rzeszutek Wilk | ee9ff85 | 2011-04-20 10:57:29 -0400 | [diff] [blame] | 22 | #include <xen/events.h> |
| 23 | #include <xen/grant_table.h> |
Konrad Rzeszutek Wilk | 4d05a28 | 2011-04-14 18:25:47 -0400 | [diff] [blame] | 24 | #include "common.h" |
| 25 | |
Tao Chen | 1375590 | 2015-03-27 13:15:54 +0000 | [diff] [blame] | 26 | /* Enlarge the array size in order to fully show blkback name. */ |
| 27 | #define BLKBACK_NAME_LEN (20) |
Bob Liu | 86839c5 | 2015-06-03 13:40:03 +0800 | [diff] [blame] | 28 | #define RINGREF_NAME_LEN (20) |
Tao Chen | 1375590 | 2015-03-27 13:15:54 +0000 | [diff] [blame] | 29 | |
Konrad Rzeszutek Wilk | d6091b2 | 2011-04-14 17:33:30 -0400 | [diff] [blame] | 30 | struct backend_info { |
Konrad Rzeszutek Wilk | 01f37f2 | 2011-05-11 15:57:09 -0400 | [diff] [blame] | 31 | struct xenbus_device *dev; |
Konrad Rzeszutek Wilk | 5185432 | 2011-05-12 18:02:28 -0400 | [diff] [blame] | 32 | struct xen_blkif *blkif; |
Konrad Rzeszutek Wilk | 01f37f2 | 2011-05-11 15:57:09 -0400 | [diff] [blame] | 33 | struct xenbus_watch backend_watch; |
| 34 | unsigned major; |
| 35 | unsigned minor; |
| 36 | char *mode; |
Konrad Rzeszutek Wilk | 4d05a28 | 2011-04-14 18:25:47 -0400 | [diff] [blame] | 37 | }; |
| 38 | |
Konrad Rzeszutek Wilk | 8b6bf74 | 2011-04-20 11:50:43 -0400 | [diff] [blame] | 39 | static struct kmem_cache *xen_blkif_cachep; |
Konrad Rzeszutek Wilk | 4d05a28 | 2011-04-14 18:25:47 -0400 | [diff] [blame] | 40 | static void connect(struct backend_info *); |
| 41 | static int connect_ring(struct backend_info *); |
| 42 | static void backend_changed(struct xenbus_watch *, const char **, |
| 43 | unsigned int); |
Valentin Priescu | 814d04e | 2014-05-20 22:28:50 +0200 | [diff] [blame] | 44 | static void xen_blkif_free(struct xen_blkif *blkif); |
| 45 | static void xen_vbd_free(struct xen_vbd *vbd); |
Konrad Rzeszutek Wilk | 4d05a28 | 2011-04-14 18:25:47 -0400 | [diff] [blame] | 46 | |
Konrad Rzeszutek Wilk | 8b6bf74 | 2011-04-20 11:50:43 -0400 | [diff] [blame] | 47 | struct xenbus_device *xen_blkbk_xenbus(struct backend_info *be) |
Jeremy Fitzhardinge | 98e036a | 2010-03-18 15:35:05 -0700 | [diff] [blame] | 48 | { |
| 49 | return be->dev; |
| 50 | } |
| 51 | |
Valentin Priescu | 814d04e | 2014-05-20 22:28:50 +0200 | [diff] [blame] | 52 | /* |
| 53 | * The last request could free the device from softirq context and |
| 54 | * xen_blkif_free() can sleep. |
| 55 | */ |
| 56 | static void xen_blkif_deferred_free(struct work_struct *work) |
| 57 | { |
| 58 | struct xen_blkif *blkif; |
| 59 | |
| 60 | blkif = container_of(work, struct xen_blkif, free_work); |
| 61 | xen_blkif_free(blkif); |
| 62 | } |
| 63 | |
Konrad Rzeszutek Wilk | 30fd150 | 2011-05-12 16:47:48 -0400 | [diff] [blame] | 64 | static int blkback_name(struct xen_blkif *blkif, char *buf) |
Konrad Rzeszutek Wilk | 4d05a28 | 2011-04-14 18:25:47 -0400 | [diff] [blame] | 65 | { |
| 66 | char *devpath, *devname; |
| 67 | struct xenbus_device *dev = blkif->be->dev; |
| 68 | |
| 69 | devpath = xenbus_read(XBT_NIL, dev->nodename, "dev", NULL); |
| 70 | if (IS_ERR(devpath)) |
| 71 | return PTR_ERR(devpath); |
| 72 | |
Konrad Rzeszutek Wilk | d6091b2 | 2011-04-14 17:33:30 -0400 | [diff] [blame] | 73 | devname = strstr(devpath, "/dev/"); |
| 74 | if (devname != NULL) |
Konrad Rzeszutek Wilk | 4d05a28 | 2011-04-14 18:25:47 -0400 | [diff] [blame] | 75 | devname += strlen("/dev/"); |
| 76 | else |
| 77 | devname = devpath; |
| 78 | |
Tao Chen | 1375590 | 2015-03-27 13:15:54 +0000 | [diff] [blame] | 79 | snprintf(buf, BLKBACK_NAME_LEN, "blkback.%d.%s", blkif->domid, devname); |
Konrad Rzeszutek Wilk | 4d05a28 | 2011-04-14 18:25:47 -0400 | [diff] [blame] | 80 | kfree(devpath); |
| 81 | |
| 82 | return 0; |
| 83 | } |
| 84 | |
Konrad Rzeszutek Wilk | 30fd150 | 2011-05-12 16:47:48 -0400 | [diff] [blame] | 85 | static void xen_update_blkif_status(struct xen_blkif *blkif) |
Konrad Rzeszutek Wilk | 4d05a28 | 2011-04-14 18:25:47 -0400 | [diff] [blame] | 86 | { |
| 87 | int err; |
Tao Chen | 1375590 | 2015-03-27 13:15:54 +0000 | [diff] [blame] | 88 | char name[BLKBACK_NAME_LEN]; |
Konrad Rzeszutek Wilk | 4d05a28 | 2011-04-14 18:25:47 -0400 | [diff] [blame] | 89 | |
| 90 | /* Not ready to connect? */ |
| 91 | if (!blkif->irq || !blkif->vbd.bdev) |
| 92 | return; |
| 93 | |
| 94 | /* Already connected? */ |
| 95 | if (blkif->be->dev->state == XenbusStateConnected) |
| 96 | return; |
| 97 | |
| 98 | /* Attempt to connect: exit if we fail to. */ |
| 99 | connect(blkif->be); |
| 100 | if (blkif->be->dev->state != XenbusStateConnected) |
| 101 | return; |
| 102 | |
| 103 | err = blkback_name(blkif, name); |
| 104 | if (err) { |
| 105 | xenbus_dev_error(blkif->be->dev, err, "get blkback dev name"); |
| 106 | return; |
| 107 | } |
| 108 | |
Chris Lalancette | cbf4629 | 2010-07-21 12:41:45 -0700 | [diff] [blame] | 109 | err = filemap_write_and_wait(blkif->vbd.bdev->bd_inode->i_mapping); |
| 110 | if (err) { |
| 111 | xenbus_dev_error(blkif->be->dev, err, "block flush"); |
| 112 | return; |
| 113 | } |
| 114 | invalidate_inode_pages2(blkif->vbd.bdev->bd_inode->i_mapping); |
| 115 | |
Kees Cook | f170168 | 2013-07-03 15:04:58 -0700 | [diff] [blame] | 116 | blkif->xenblkd = kthread_run(xen_blkif_schedule, blkif, "%s", name); |
Konrad Rzeszutek Wilk | 4d05a28 | 2011-04-14 18:25:47 -0400 | [diff] [blame] | 117 | if (IS_ERR(blkif->xenblkd)) { |
| 118 | err = PTR_ERR(blkif->xenblkd); |
| 119 | blkif->xenblkd = NULL; |
| 120 | xenbus_dev_error(blkif->be->dev, err, "start xenblkd"); |
Roger Pau Monne | 3f3aad5 | 2013-04-17 20:18:57 +0200 | [diff] [blame] | 121 | return; |
Konrad Rzeszutek Wilk | 4d05a28 | 2011-04-14 18:25:47 -0400 | [diff] [blame] | 122 | } |
| 123 | } |
| 124 | |
Konrad Rzeszutek Wilk | 30fd150 | 2011-05-12 16:47:48 -0400 | [diff] [blame] | 125 | static struct xen_blkif *xen_blkif_alloc(domid_t domid) |
Konrad Rzeszutek Wilk | ee9ff85 | 2011-04-20 10:57:29 -0400 | [diff] [blame] | 126 | { |
Konrad Rzeszutek Wilk | 30fd150 | 2011-05-12 16:47:48 -0400 | [diff] [blame] | 127 | struct xen_blkif *blkif; |
Konrad Rzeszutek Wilk | ee9ff85 | 2011-04-20 10:57:29 -0400 | [diff] [blame] | 128 | |
Roger Pau Monne | 402b27f | 2013-04-18 16:06:54 +0200 | [diff] [blame] | 129 | BUILD_BUG_ON(MAX_INDIRECT_PAGES > BLKIF_MAX_INDIRECT_PAGES_PER_REQUEST); |
Konrad Rzeszutek Wilk | ee9ff85 | 2011-04-20 10:57:29 -0400 | [diff] [blame] | 130 | |
Wei Yongjun | 654dbef | 2012-08-27 12:28:57 +0800 | [diff] [blame] | 131 | blkif = kmem_cache_zalloc(xen_blkif_cachep, GFP_KERNEL); |
Konrad Rzeszutek Wilk | ee9ff85 | 2011-04-20 10:57:29 -0400 | [diff] [blame] | 132 | if (!blkif) |
| 133 | return ERR_PTR(-ENOMEM); |
| 134 | |
Konrad Rzeszutek Wilk | ee9ff85 | 2011-04-20 10:57:29 -0400 | [diff] [blame] | 135 | blkif->domid = domid; |
| 136 | spin_lock_init(&blkif->blk_ring_lock); |
| 137 | atomic_set(&blkif->refcnt, 1); |
| 138 | init_waitqueue_head(&blkif->wq); |
Konrad Rzeszutek Wilk | 29bde09 | 2011-10-10 00:42:22 -0400 | [diff] [blame] | 139 | init_completion(&blkif->drain_complete); |
| 140 | atomic_set(&blkif->drain, 0); |
Konrad Rzeszutek Wilk | ee9ff85 | 2011-04-20 10:57:29 -0400 | [diff] [blame] | 141 | blkif->st_print = jiffies; |
Roger Pau Monne | 0a8704a | 2012-10-24 18:58:45 +0200 | [diff] [blame] | 142 | blkif->persistent_gnts.rb_node = NULL; |
Roger Pau Monne | c6cc142 | 2013-04-17 20:18:56 +0200 | [diff] [blame] | 143 | spin_lock_init(&blkif->free_pages_lock); |
| 144 | INIT_LIST_HEAD(&blkif->free_pages); |
Roger Pau Monne | ef75341 | 2014-02-04 11:26:13 +0100 | [diff] [blame] | 145 | INIT_LIST_HEAD(&blkif->persistent_purge_list); |
Roger Pau Monne | c6cc142 | 2013-04-17 20:18:56 +0200 | [diff] [blame] | 146 | blkif->free_pages_num = 0; |
Roger Pau Monne | 3f3aad5 | 2013-04-17 20:18:57 +0200 | [diff] [blame] | 147 | atomic_set(&blkif->persistent_gnt_in_use, 0); |
Roger Pau Monne | c05f3e3 | 2014-02-04 11:26:14 +0100 | [diff] [blame] | 148 | atomic_set(&blkif->inflight, 0); |
Roger Pau Monne | abb97b8 | 2014-02-11 20:34:03 -0700 | [diff] [blame] | 149 | INIT_WORK(&blkif->persistent_purge_work, xen_blkbk_unmap_purged_grants); |
Konrad Rzeszutek Wilk | ee9ff85 | 2011-04-20 10:57:29 -0400 | [diff] [blame] | 150 | |
Roger Pau Monne | bf0720c | 2013-04-17 20:18:59 +0200 | [diff] [blame] | 151 | INIT_LIST_HEAD(&blkif->pending_free); |
Valentin Priescu | 814d04e | 2014-05-20 22:28:50 +0200 | [diff] [blame] | 152 | INIT_WORK(&blkif->free_work, xen_blkif_deferred_free); |
Roger Pau Monne | bf0720c | 2013-04-17 20:18:59 +0200 | [diff] [blame] | 153 | spin_lock_init(&blkif->pending_free_lock); |
| 154 | init_waitqueue_head(&blkif->pending_free_wq); |
Konrad Rzeszutek Wilk | 8e3f875 | 2013-01-23 16:54:32 -0500 | [diff] [blame] | 155 | init_waitqueue_head(&blkif->shutdown_wq); |
Konrad Rzeszutek Wilk | ee9ff85 | 2011-04-20 10:57:29 -0400 | [diff] [blame] | 156 | |
| 157 | return blkif; |
| 158 | } |
| 159 | |
Bob Liu | 86839c5 | 2015-06-03 13:40:03 +0800 | [diff] [blame] | 160 | static int xen_blkif_map(struct xen_blkif *blkif, grant_ref_t *gref, |
| 161 | unsigned int nr_grefs, unsigned int evtchn) |
Konrad Rzeszutek Wilk | ee9ff85 | 2011-04-20 10:57:29 -0400 | [diff] [blame] | 162 | { |
| 163 | int err; |
| 164 | |
| 165 | /* Already connected through? */ |
| 166 | if (blkif->irq) |
| 167 | return 0; |
| 168 | |
Bob Liu | 86839c5 | 2015-06-03 13:40:03 +0800 | [diff] [blame] | 169 | err = xenbus_map_ring_valloc(blkif->be->dev, gref, nr_grefs, |
Wei Liu | ccc9d90 | 2015-04-03 14:44:59 +0800 | [diff] [blame] | 170 | &blkif->blk_ring); |
David Vrabel | 2d07384 | 2011-09-29 16:53:30 +0100 | [diff] [blame] | 171 | if (err < 0) |
Konrad Rzeszutek Wilk | ee9ff85 | 2011-04-20 10:57:29 -0400 | [diff] [blame] | 172 | return err; |
Konrad Rzeszutek Wilk | ee9ff85 | 2011-04-20 10:57:29 -0400 | [diff] [blame] | 173 | |
| 174 | switch (blkif->blk_protocol) { |
| 175 | case BLKIF_PROTOCOL_NATIVE: |
| 176 | { |
| 177 | struct blkif_sring *sring; |
David Vrabel | 2d07384 | 2011-09-29 16:53:30 +0100 | [diff] [blame] | 178 | sring = (struct blkif_sring *)blkif->blk_ring; |
Julien Grall | 67de5df | 2015-05-05 16:25:56 +0100 | [diff] [blame] | 179 | BACK_RING_INIT(&blkif->blk_rings.native, sring, |
| 180 | XEN_PAGE_SIZE * nr_grefs); |
Konrad Rzeszutek Wilk | ee9ff85 | 2011-04-20 10:57:29 -0400 | [diff] [blame] | 181 | break; |
| 182 | } |
| 183 | case BLKIF_PROTOCOL_X86_32: |
| 184 | { |
| 185 | struct blkif_x86_32_sring *sring_x86_32; |
David Vrabel | 2d07384 | 2011-09-29 16:53:30 +0100 | [diff] [blame] | 186 | sring_x86_32 = (struct blkif_x86_32_sring *)blkif->blk_ring; |
Julien Grall | 67de5df | 2015-05-05 16:25:56 +0100 | [diff] [blame] | 187 | BACK_RING_INIT(&blkif->blk_rings.x86_32, sring_x86_32, |
| 188 | XEN_PAGE_SIZE * nr_grefs); |
Konrad Rzeszutek Wilk | ee9ff85 | 2011-04-20 10:57:29 -0400 | [diff] [blame] | 189 | break; |
| 190 | } |
| 191 | case BLKIF_PROTOCOL_X86_64: |
| 192 | { |
| 193 | struct blkif_x86_64_sring *sring_x86_64; |
David Vrabel | 2d07384 | 2011-09-29 16:53:30 +0100 | [diff] [blame] | 194 | sring_x86_64 = (struct blkif_x86_64_sring *)blkif->blk_ring; |
Julien Grall | 67de5df | 2015-05-05 16:25:56 +0100 | [diff] [blame] | 195 | BACK_RING_INIT(&blkif->blk_rings.x86_64, sring_x86_64, |
| 196 | XEN_PAGE_SIZE * nr_grefs); |
Konrad Rzeszutek Wilk | ee9ff85 | 2011-04-20 10:57:29 -0400 | [diff] [blame] | 197 | break; |
| 198 | } |
| 199 | default: |
| 200 | BUG(); |
| 201 | } |
| 202 | |
Konrad Rzeszutek Wilk | 8b6bf74 | 2011-04-20 11:50:43 -0400 | [diff] [blame] | 203 | err = bind_interdomain_evtchn_to_irqhandler(blkif->domid, evtchn, |
| 204 | xen_blkif_be_int, 0, |
| 205 | "blkif-backend", blkif); |
Konrad Rzeszutek Wilk | ee9ff85 | 2011-04-20 10:57:29 -0400 | [diff] [blame] | 206 | if (err < 0) { |
David Vrabel | 2d07384 | 2011-09-29 16:53:30 +0100 | [diff] [blame] | 207 | xenbus_unmap_ring_vfree(blkif->be->dev, blkif->blk_ring); |
Konrad Rzeszutek Wilk | ee9ff85 | 2011-04-20 10:57:29 -0400 | [diff] [blame] | 208 | blkif->blk_rings.common.sring = NULL; |
| 209 | return err; |
| 210 | } |
| 211 | blkif->irq = err; |
| 212 | |
| 213 | return 0; |
| 214 | } |
| 215 | |
Valentin Priescu | 814d04e | 2014-05-20 22:28:50 +0200 | [diff] [blame] | 216 | static int xen_blkif_disconnect(struct xen_blkif *blkif) |
Konrad Rzeszutek Wilk | ee9ff85 | 2011-04-20 10:57:29 -0400 | [diff] [blame] | 217 | { |
Roger Pau Monne | f929d42 | 2015-09-04 12:08:07 +0200 | [diff] [blame] | 218 | struct pending_req *req, *n; |
| 219 | int i = 0, j; |
| 220 | |
Konrad Rzeszutek Wilk | ee9ff85 | 2011-04-20 10:57:29 -0400 | [diff] [blame] | 221 | if (blkif->xenblkd) { |
| 222 | kthread_stop(blkif->xenblkd); |
Konrad Rzeszutek Wilk | 8e3f875 | 2013-01-23 16:54:32 -0500 | [diff] [blame] | 223 | wake_up(&blkif->shutdown_wq); |
Konrad Rzeszutek Wilk | ee9ff85 | 2011-04-20 10:57:29 -0400 | [diff] [blame] | 224 | blkif->xenblkd = NULL; |
| 225 | } |
| 226 | |
Valentin Priescu | 814d04e | 2014-05-20 22:28:50 +0200 | [diff] [blame] | 227 | /* The above kthread_stop() guarantees that at this point we |
| 228 | * don't have any discard_io or other_io requests. So, checking |
| 229 | * for inflight IO is enough. |
| 230 | */ |
| 231 | if (atomic_read(&blkif->inflight) > 0) |
| 232 | return -EBUSY; |
Konrad Rzeszutek Wilk | ee9ff85 | 2011-04-20 10:57:29 -0400 | [diff] [blame] | 233 | |
| 234 | if (blkif->irq) { |
| 235 | unbind_from_irqhandler(blkif->irq, blkif); |
| 236 | blkif->irq = 0; |
| 237 | } |
| 238 | |
| 239 | if (blkif->blk_rings.common.sring) { |
David Vrabel | 2d07384 | 2011-09-29 16:53:30 +0100 | [diff] [blame] | 240 | xenbus_unmap_ring_vfree(blkif->be->dev, blkif->blk_ring); |
Konrad Rzeszutek Wilk | ee9ff85 | 2011-04-20 10:57:29 -0400 | [diff] [blame] | 241 | blkif->blk_rings.common.sring = NULL; |
| 242 | } |
Valentin Priescu | 814d04e | 2014-05-20 22:28:50 +0200 | [diff] [blame] | 243 | |
Vitaly Kuznetsov | 12ea729 | 2014-09-08 15:21:33 +0200 | [diff] [blame] | 244 | /* Remove all persistent grants and the cache of ballooned pages. */ |
| 245 | xen_blkbk_free_caches(blkif); |
| 246 | |
Roger Pau Monne | bf0720c | 2013-04-17 20:18:59 +0200 | [diff] [blame] | 247 | /* Check that there is no request in use */ |
Roger Pau Monne | bb642e8 | 2013-05-02 10:21:17 +0200 | [diff] [blame] | 248 | list_for_each_entry_safe(req, n, &blkif->pending_free, free_list) { |
| 249 | list_del(&req->free_list); |
Roger Pau Monne | bf0720c | 2013-04-17 20:18:59 +0200 | [diff] [blame] | 250 | |
Roger Pau Monne | bb642e8 | 2013-05-02 10:21:17 +0200 | [diff] [blame] | 251 | for (j = 0; j < MAX_INDIRECT_SEGMENTS; j++) |
| 252 | kfree(req->segments[j]); |
| 253 | |
| 254 | for (j = 0; j < MAX_INDIRECT_PAGES; j++) |
| 255 | kfree(req->indirect_pages[j]); |
| 256 | |
| 257 | kfree(req); |
| 258 | i++; |
| 259 | } |
| 260 | |
Bob Liu | 86839c5 | 2015-06-03 13:40:03 +0800 | [diff] [blame] | 261 | WARN_ON(i != (XEN_BLKIF_REQS_PER_PAGE * blkif->nr_ring_pages)); |
Roger Pau Monne | f929d42 | 2015-09-04 12:08:07 +0200 | [diff] [blame] | 262 | blkif->nr_ring_pages = 0; |
| 263 | |
| 264 | return 0; |
| 265 | } |
| 266 | |
| 267 | static void xen_blkif_free(struct xen_blkif *blkif) |
| 268 | { |
| 269 | |
| 270 | xen_blkif_disconnect(blkif); |
| 271 | xen_vbd_free(&blkif->vbd); |
| 272 | |
| 273 | /* Make sure everything is drained before shutting down */ |
| 274 | BUG_ON(blkif->persistent_gnt_c != 0); |
| 275 | BUG_ON(atomic_read(&blkif->persistent_gnt_in_use) != 0); |
| 276 | BUG_ON(blkif->free_pages_num != 0); |
| 277 | BUG_ON(!list_empty(&blkif->persistent_purge_list)); |
| 278 | BUG_ON(!list_empty(&blkif->free_pages)); |
| 279 | BUG_ON(!RB_EMPTY_ROOT(&blkif->persistent_gnts)); |
Roger Pau Monne | bb642e8 | 2013-05-02 10:21:17 +0200 | [diff] [blame] | 280 | |
Konrad Rzeszutek Wilk | 8b6bf74 | 2011-04-20 11:50:43 -0400 | [diff] [blame] | 281 | kmem_cache_free(xen_blkif_cachep, blkif); |
Konrad Rzeszutek Wilk | ee9ff85 | 2011-04-20 10:57:29 -0400 | [diff] [blame] | 282 | } |
| 283 | |
Konrad Rzeszutek Wilk | 8b6bf74 | 2011-04-20 11:50:43 -0400 | [diff] [blame] | 284 | int __init xen_blkif_interface_init(void) |
Konrad Rzeszutek Wilk | ee9ff85 | 2011-04-20 10:57:29 -0400 | [diff] [blame] | 285 | { |
Konrad Rzeszutek Wilk | 8b6bf74 | 2011-04-20 11:50:43 -0400 | [diff] [blame] | 286 | xen_blkif_cachep = kmem_cache_create("blkif_cache", |
Konrad Rzeszutek Wilk | 30fd150 | 2011-05-12 16:47:48 -0400 | [diff] [blame] | 287 | sizeof(struct xen_blkif), |
Konrad Rzeszutek Wilk | 8b6bf74 | 2011-04-20 11:50:43 -0400 | [diff] [blame] | 288 | 0, 0, NULL); |
| 289 | if (!xen_blkif_cachep) |
Konrad Rzeszutek Wilk | ee9ff85 | 2011-04-20 10:57:29 -0400 | [diff] [blame] | 290 | return -ENOMEM; |
| 291 | |
| 292 | return 0; |
| 293 | } |
Konrad Rzeszutek Wilk | 4d05a28 | 2011-04-14 18:25:47 -0400 | [diff] [blame] | 294 | |
Konrad Rzeszutek Wilk | a1397fa | 2011-04-14 17:05:23 -0400 | [diff] [blame] | 295 | /* |
Konrad Rzeszutek Wilk | 4d05a28 | 2011-04-14 18:25:47 -0400 | [diff] [blame] | 296 | * sysfs interface for VBD I/O requests |
| 297 | */ |
| 298 | |
| 299 | #define VBD_SHOW(name, format, args...) \ |
| 300 | static ssize_t show_##name(struct device *_dev, \ |
| 301 | struct device_attribute *attr, \ |
| 302 | char *buf) \ |
| 303 | { \ |
| 304 | struct xenbus_device *dev = to_xenbus_device(_dev); \ |
Jeremy Fitzhardinge | 5cf6e4f | 2010-02-11 16:07:31 -0800 | [diff] [blame] | 305 | struct backend_info *be = dev_get_drvdata(&dev->dev); \ |
Konrad Rzeszutek Wilk | 4d05a28 | 2011-04-14 18:25:47 -0400 | [diff] [blame] | 306 | \ |
| 307 | return sprintf(buf, format, ##args); \ |
| 308 | } \ |
| 309 | static DEVICE_ATTR(name, S_IRUGO, show_##name, NULL) |
| 310 | |
Zoltan Kiss | 986cacb | 2013-03-11 16:15:50 +0000 | [diff] [blame] | 311 | VBD_SHOW(oo_req, "%llu\n", be->blkif->st_oo_req); |
| 312 | VBD_SHOW(rd_req, "%llu\n", be->blkif->st_rd_req); |
| 313 | VBD_SHOW(wr_req, "%llu\n", be->blkif->st_wr_req); |
| 314 | VBD_SHOW(f_req, "%llu\n", be->blkif->st_f_req); |
| 315 | VBD_SHOW(ds_req, "%llu\n", be->blkif->st_ds_req); |
| 316 | VBD_SHOW(rd_sect, "%llu\n", be->blkif->st_rd_sect); |
| 317 | VBD_SHOW(wr_sect, "%llu\n", be->blkif->st_wr_sect); |
Konrad Rzeszutek Wilk | 4d05a28 | 2011-04-14 18:25:47 -0400 | [diff] [blame] | 318 | |
Konrad Rzeszutek Wilk | 3d81473 | 2011-05-12 16:53:56 -0400 | [diff] [blame] | 319 | static struct attribute *xen_vbdstat_attrs[] = { |
Konrad Rzeszutek Wilk | 4d05a28 | 2011-04-14 18:25:47 -0400 | [diff] [blame] | 320 | &dev_attr_oo_req.attr, |
| 321 | &dev_attr_rd_req.attr, |
| 322 | &dev_attr_wr_req.attr, |
Konrad Rzeszutek Wilk | 24f567f | 2011-05-04 17:07:27 -0400 | [diff] [blame] | 323 | &dev_attr_f_req.attr, |
Li Dongyang | b3cb0d6 | 2011-09-01 18:39:10 +0800 | [diff] [blame] | 324 | &dev_attr_ds_req.attr, |
Konrad Rzeszutek Wilk | 4d05a28 | 2011-04-14 18:25:47 -0400 | [diff] [blame] | 325 | &dev_attr_rd_sect.attr, |
| 326 | &dev_attr_wr_sect.attr, |
| 327 | NULL |
| 328 | }; |
| 329 | |
Konrad Rzeszutek Wilk | 3d81473 | 2011-05-12 16:53:56 -0400 | [diff] [blame] | 330 | static struct attribute_group xen_vbdstat_group = { |
Konrad Rzeszutek Wilk | 4d05a28 | 2011-04-14 18:25:47 -0400 | [diff] [blame] | 331 | .name = "statistics", |
Konrad Rzeszutek Wilk | 3d81473 | 2011-05-12 16:53:56 -0400 | [diff] [blame] | 332 | .attrs = xen_vbdstat_attrs, |
Konrad Rzeszutek Wilk | 4d05a28 | 2011-04-14 18:25:47 -0400 | [diff] [blame] | 333 | }; |
| 334 | |
| 335 | VBD_SHOW(physical_device, "%x:%x\n", be->major, be->minor); |
| 336 | VBD_SHOW(mode, "%s\n", be->mode); |
| 337 | |
Konrad Rzeszutek Wilk | 2911758 | 2012-08-13 10:53:17 -0400 | [diff] [blame] | 338 | static int xenvbd_sysfs_addif(struct xenbus_device *dev) |
Konrad Rzeszutek Wilk | 4d05a28 | 2011-04-14 18:25:47 -0400 | [diff] [blame] | 339 | { |
| 340 | int error; |
| 341 | |
| 342 | error = device_create_file(&dev->dev, &dev_attr_physical_device); |
Konrad Rzeszutek Wilk | d6091b2 | 2011-04-14 17:33:30 -0400 | [diff] [blame] | 343 | if (error) |
Konrad Rzeszutek Wilk | 4d05a28 | 2011-04-14 18:25:47 -0400 | [diff] [blame] | 344 | goto fail1; |
| 345 | |
| 346 | error = device_create_file(&dev->dev, &dev_attr_mode); |
| 347 | if (error) |
| 348 | goto fail2; |
| 349 | |
Konrad Rzeszutek Wilk | 3d81473 | 2011-05-12 16:53:56 -0400 | [diff] [blame] | 350 | error = sysfs_create_group(&dev->dev.kobj, &xen_vbdstat_group); |
Konrad Rzeszutek Wilk | 4d05a28 | 2011-04-14 18:25:47 -0400 | [diff] [blame] | 351 | if (error) |
| 352 | goto fail3; |
| 353 | |
| 354 | return 0; |
| 355 | |
Konrad Rzeszutek Wilk | 3d81473 | 2011-05-12 16:53:56 -0400 | [diff] [blame] | 356 | fail3: sysfs_remove_group(&dev->dev.kobj, &xen_vbdstat_group); |
Konrad Rzeszutek Wilk | 4d05a28 | 2011-04-14 18:25:47 -0400 | [diff] [blame] | 357 | fail2: device_remove_file(&dev->dev, &dev_attr_mode); |
| 358 | fail1: device_remove_file(&dev->dev, &dev_attr_physical_device); |
| 359 | return error; |
| 360 | } |
| 361 | |
Konrad Rzeszutek Wilk | 2911758 | 2012-08-13 10:53:17 -0400 | [diff] [blame] | 362 | static void xenvbd_sysfs_delif(struct xenbus_device *dev) |
Konrad Rzeszutek Wilk | 4d05a28 | 2011-04-14 18:25:47 -0400 | [diff] [blame] | 363 | { |
Konrad Rzeszutek Wilk | 3d81473 | 2011-05-12 16:53:56 -0400 | [diff] [blame] | 364 | sysfs_remove_group(&dev->dev.kobj, &xen_vbdstat_group); |
Konrad Rzeszutek Wilk | 4d05a28 | 2011-04-14 18:25:47 -0400 | [diff] [blame] | 365 | device_remove_file(&dev->dev, &dev_attr_mode); |
| 366 | device_remove_file(&dev->dev, &dev_attr_physical_device); |
| 367 | } |
| 368 | |
Konrad Rzeszutek Wilk | 42c7841 | 2011-04-20 11:21:43 -0400 | [diff] [blame] | 369 | |
Konrad Rzeszutek Wilk | 3d81473 | 2011-05-12 16:53:56 -0400 | [diff] [blame] | 370 | static void xen_vbd_free(struct xen_vbd *vbd) |
Konrad Rzeszutek Wilk | 42c7841 | 2011-04-20 11:21:43 -0400 | [diff] [blame] | 371 | { |
| 372 | if (vbd->bdev) |
| 373 | blkdev_put(vbd->bdev, vbd->readonly ? FMODE_READ : FMODE_WRITE); |
| 374 | vbd->bdev = NULL; |
| 375 | } |
| 376 | |
Konrad Rzeszutek Wilk | 3d81473 | 2011-05-12 16:53:56 -0400 | [diff] [blame] | 377 | static int xen_vbd_create(struct xen_blkif *blkif, blkif_vdev_t handle, |
| 378 | unsigned major, unsigned minor, int readonly, |
| 379 | int cdrom) |
Konrad Rzeszutek Wilk | 42c7841 | 2011-04-20 11:21:43 -0400 | [diff] [blame] | 380 | { |
Konrad Rzeszutek Wilk | 3d81473 | 2011-05-12 16:53:56 -0400 | [diff] [blame] | 381 | struct xen_vbd *vbd; |
Konrad Rzeszutek Wilk | 42c7841 | 2011-04-20 11:21:43 -0400 | [diff] [blame] | 382 | struct block_device *bdev; |
Konrad Rzeszutek Wilk | 24f567f | 2011-05-04 17:07:27 -0400 | [diff] [blame] | 383 | struct request_queue *q; |
Konrad Rzeszutek Wilk | 42c7841 | 2011-04-20 11:21:43 -0400 | [diff] [blame] | 384 | |
| 385 | vbd = &blkif->vbd; |
| 386 | vbd->handle = handle; |
| 387 | vbd->readonly = readonly; |
| 388 | vbd->type = 0; |
| 389 | |
| 390 | vbd->pdevice = MKDEV(major, minor); |
| 391 | |
| 392 | bdev = blkdev_get_by_dev(vbd->pdevice, vbd->readonly ? |
| 393 | FMODE_READ : FMODE_WRITE, NULL); |
| 394 | |
| 395 | if (IS_ERR(bdev)) { |
Tao Chen | 77387b8 | 2015-04-01 15:04:22 +0000 | [diff] [blame] | 396 | pr_warn("xen_vbd_create: device %08x could not be opened\n", |
Konrad Rzeszutek Wilk | 42c7841 | 2011-04-20 11:21:43 -0400 | [diff] [blame] | 397 | vbd->pdevice); |
| 398 | return -ENOENT; |
| 399 | } |
| 400 | |
| 401 | vbd->bdev = bdev; |
Konrad Rzeszutek Wilk | 42c7841 | 2011-04-20 11:21:43 -0400 | [diff] [blame] | 402 | if (vbd->bdev->bd_disk == NULL) { |
Tao Chen | 77387b8 | 2015-04-01 15:04:22 +0000 | [diff] [blame] | 403 | pr_warn("xen_vbd_create: device %08x doesn't exist\n", |
Konrad Rzeszutek Wilk | 42c7841 | 2011-04-20 11:21:43 -0400 | [diff] [blame] | 404 | vbd->pdevice); |
Konrad Rzeszutek Wilk | 3d81473 | 2011-05-12 16:53:56 -0400 | [diff] [blame] | 405 | xen_vbd_free(vbd); |
Konrad Rzeszutek Wilk | 42c7841 | 2011-04-20 11:21:43 -0400 | [diff] [blame] | 406 | return -ENOENT; |
| 407 | } |
Laszlo Ersek | 6464920 | 2011-05-25 12:24:25 +0200 | [diff] [blame] | 408 | vbd->size = vbd_sz(vbd); |
Konrad Rzeszutek Wilk | 42c7841 | 2011-04-20 11:21:43 -0400 | [diff] [blame] | 409 | |
| 410 | if (vbd->bdev->bd_disk->flags & GENHD_FL_CD || cdrom) |
| 411 | vbd->type |= VDISK_CDROM; |
| 412 | if (vbd->bdev->bd_disk->flags & GENHD_FL_REMOVABLE) |
| 413 | vbd->type |= VDISK_REMOVABLE; |
| 414 | |
Konrad Rzeszutek Wilk | 24f567f | 2011-05-04 17:07:27 -0400 | [diff] [blame] | 415 | q = bdev_get_queue(bdev); |
| 416 | if (q && q->flush_flags) |
| 417 | vbd->flush_support = true; |
| 418 | |
Konrad Rzeszutek Wilk | 5ea4298 | 2011-10-12 16:23:30 -0400 | [diff] [blame] | 419 | if (q && blk_queue_secdiscard(q)) |
| 420 | vbd->discard_secure = true; |
| 421 | |
Tao Chen | 77387b8 | 2015-04-01 15:04:22 +0000 | [diff] [blame] | 422 | pr_debug("Successful creation of handle=%04x (dom=%u)\n", |
Konrad Rzeszutek Wilk | 42c7841 | 2011-04-20 11:21:43 -0400 | [diff] [blame] | 423 | handle, blkif->domid); |
| 424 | return 0; |
| 425 | } |
Konrad Rzeszutek Wilk | 8b6bf74 | 2011-04-20 11:50:43 -0400 | [diff] [blame] | 426 | static int xen_blkbk_remove(struct xenbus_device *dev) |
Konrad Rzeszutek Wilk | 4d05a28 | 2011-04-14 18:25:47 -0400 | [diff] [blame] | 427 | { |
Jeremy Fitzhardinge | 5cf6e4f | 2010-02-11 16:07:31 -0800 | [diff] [blame] | 428 | struct backend_info *be = dev_get_drvdata(&dev->dev); |
Konrad Rzeszutek Wilk | 4d05a28 | 2011-04-14 18:25:47 -0400 | [diff] [blame] | 429 | |
Tao Chen | 77387b8 | 2015-04-01 15:04:22 +0000 | [diff] [blame] | 430 | pr_debug("%s %p %d\n", __func__, dev, dev->otherend_id); |
Konrad Rzeszutek Wilk | 4d05a28 | 2011-04-14 18:25:47 -0400 | [diff] [blame] | 431 | |
| 432 | if (be->major || be->minor) |
| 433 | xenvbd_sysfs_delif(dev); |
| 434 | |
| 435 | if (be->backend_watch.node) { |
| 436 | unregister_xenbus_watch(&be->backend_watch); |
| 437 | kfree(be->backend_watch.node); |
| 438 | be->backend_watch.node = NULL; |
| 439 | } |
| 440 | |
Valentin Priescu | 814d04e | 2014-05-20 22:28:50 +0200 | [diff] [blame] | 441 | dev_set_drvdata(&dev->dev, NULL); |
| 442 | |
Konrad Rzeszutek Wilk | 4d05a28 | 2011-04-14 18:25:47 -0400 | [diff] [blame] | 443 | if (be->blkif) { |
Konrad Rzeszutek Wilk | 8b6bf74 | 2011-04-20 11:50:43 -0400 | [diff] [blame] | 444 | xen_blkif_disconnect(be->blkif); |
Valentin Priescu | 814d04e | 2014-05-20 22:28:50 +0200 | [diff] [blame] | 445 | xen_blkif_put(be->blkif); |
Konrad Rzeszutek Wilk | 4d05a28 | 2011-04-14 18:25:47 -0400 | [diff] [blame] | 446 | } |
| 447 | |
Jan Beulich | 9d09260 | 2012-12-20 10:31:11 +0000 | [diff] [blame] | 448 | kfree(be->mode); |
Konrad Rzeszutek Wilk | 4d05a28 | 2011-04-14 18:25:47 -0400 | [diff] [blame] | 449 | kfree(be); |
Konrad Rzeszutek Wilk | 4d05a28 | 2011-04-14 18:25:47 -0400 | [diff] [blame] | 450 | return 0; |
| 451 | } |
| 452 | |
Konrad Rzeszutek Wilk | 24f567f | 2011-05-04 17:07:27 -0400 | [diff] [blame] | 453 | int xen_blkbk_flush_diskcache(struct xenbus_transaction xbt, |
| 454 | struct backend_info *be, int state) |
Konrad Rzeszutek Wilk | 4d05a28 | 2011-04-14 18:25:47 -0400 | [diff] [blame] | 455 | { |
| 456 | struct xenbus_device *dev = be->dev; |
| 457 | int err; |
| 458 | |
Konrad Rzeszutek Wilk | 24f567f | 2011-05-04 17:07:27 -0400 | [diff] [blame] | 459 | err = xenbus_printf(xbt, dev->nodename, "feature-flush-cache", |
Konrad Rzeszutek Wilk | 4d05a28 | 2011-04-14 18:25:47 -0400 | [diff] [blame] | 460 | "%d", state); |
| 461 | if (err) |
Konrad Rzeszutek Wilk | 3389bb8 | 2012-03-14 13:04:00 -0400 | [diff] [blame] | 462 | dev_warn(&dev->dev, "writing feature-flush-cache (%d)", err); |
Konrad Rzeszutek Wilk | 4d05a28 | 2011-04-14 18:25:47 -0400 | [diff] [blame] | 463 | |
| 464 | return err; |
| 465 | } |
| 466 | |
Konrad Rzeszutek Wilk | 3389bb8 | 2012-03-14 13:04:00 -0400 | [diff] [blame] | 467 | static void xen_blkbk_discard(struct xenbus_transaction xbt, struct backend_info *be) |
Li Dongyang | b3cb0d6 | 2011-09-01 18:39:10 +0800 | [diff] [blame] | 468 | { |
| 469 | struct xenbus_device *dev = be->dev; |
| 470 | struct xen_blkif *blkif = be->blkif; |
Li Dongyang | b3cb0d6 | 2011-09-01 18:39:10 +0800 | [diff] [blame] | 471 | int err; |
Olaf Hering | c926b70 | 2014-05-21 16:32:42 +0200 | [diff] [blame] | 472 | int state = 0, discard_enable; |
Konrad Rzeszutek Wilk | 4dae767 | 2012-03-13 18:43:23 -0400 | [diff] [blame] | 473 | struct block_device *bdev = be->blkif->vbd.bdev; |
| 474 | struct request_queue *q = bdev_get_queue(bdev); |
Li Dongyang | b3cb0d6 | 2011-09-01 18:39:10 +0800 | [diff] [blame] | 475 | |
Olaf Hering | c926b70 | 2014-05-21 16:32:42 +0200 | [diff] [blame] | 476 | err = xenbus_scanf(XBT_NIL, dev->nodename, "discard-enable", "%d", |
| 477 | &discard_enable); |
| 478 | if (err == 1 && !discard_enable) |
| 479 | return; |
| 480 | |
Konrad Rzeszutek Wilk | 4dae767 | 2012-03-13 18:43:23 -0400 | [diff] [blame] | 481 | if (blk_queue_discard(q)) { |
| 482 | err = xenbus_printf(xbt, dev->nodename, |
| 483 | "discard-granularity", "%u", |
| 484 | q->limits.discard_granularity); |
| 485 | if (err) { |
Konrad Rzeszutek Wilk | 3389bb8 | 2012-03-14 13:04:00 -0400 | [diff] [blame] | 486 | dev_warn(&dev->dev, "writing discard-granularity (%d)", err); |
| 487 | return; |
Li Dongyang | b3cb0d6 | 2011-09-01 18:39:10 +0800 | [diff] [blame] | 488 | } |
Konrad Rzeszutek Wilk | 4dae767 | 2012-03-13 18:43:23 -0400 | [diff] [blame] | 489 | err = xenbus_printf(xbt, dev->nodename, |
| 490 | "discard-alignment", "%u", |
| 491 | q->limits.discard_alignment); |
| 492 | if (err) { |
Konrad Rzeszutek Wilk | 3389bb8 | 2012-03-14 13:04:00 -0400 | [diff] [blame] | 493 | dev_warn(&dev->dev, "writing discard-alignment (%d)", err); |
| 494 | return; |
Konrad Rzeszutek Wilk | 4dae767 | 2012-03-13 18:43:23 -0400 | [diff] [blame] | 495 | } |
| 496 | state = 1; |
| 497 | /* Optional. */ |
| 498 | err = xenbus_printf(xbt, dev->nodename, |
| 499 | "discard-secure", "%d", |
| 500 | blkif->vbd.discard_secure); |
| 501 | if (err) { |
Konrad Rzeszutek Wilk | a71e23d | 2012-04-16 21:55:04 -0400 | [diff] [blame] | 502 | dev_warn(&dev->dev, "writing discard-secure (%d)", err); |
Konrad Rzeszutek Wilk | 3389bb8 | 2012-03-14 13:04:00 -0400 | [diff] [blame] | 503 | return; |
Li Dongyang | b3cb0d6 | 2011-09-01 18:39:10 +0800 | [diff] [blame] | 504 | } |
Li Dongyang | b3cb0d6 | 2011-09-01 18:39:10 +0800 | [diff] [blame] | 505 | } |
Li Dongyang | b3cb0d6 | 2011-09-01 18:39:10 +0800 | [diff] [blame] | 506 | err = xenbus_printf(xbt, dev->nodename, "feature-discard", |
| 507 | "%d", state); |
| 508 | if (err) |
Konrad Rzeszutek Wilk | 3389bb8 | 2012-03-14 13:04:00 -0400 | [diff] [blame] | 509 | dev_warn(&dev->dev, "writing feature-discard (%d)", err); |
Li Dongyang | b3cb0d6 | 2011-09-01 18:39:10 +0800 | [diff] [blame] | 510 | } |
Konrad Rzeszutek Wilk | 29bde09 | 2011-10-10 00:42:22 -0400 | [diff] [blame] | 511 | int xen_blkbk_barrier(struct xenbus_transaction xbt, |
| 512 | struct backend_info *be, int state) |
| 513 | { |
| 514 | struct xenbus_device *dev = be->dev; |
| 515 | int err; |
| 516 | |
| 517 | err = xenbus_printf(xbt, dev->nodename, "feature-barrier", |
| 518 | "%d", state); |
| 519 | if (err) |
Konrad Rzeszutek Wilk | 3389bb8 | 2012-03-14 13:04:00 -0400 | [diff] [blame] | 520 | dev_warn(&dev->dev, "writing feature-barrier (%d)", err); |
Konrad Rzeszutek Wilk | 29bde09 | 2011-10-10 00:42:22 -0400 | [diff] [blame] | 521 | |
| 522 | return err; |
| 523 | } |
Li Dongyang | b3cb0d6 | 2011-09-01 18:39:10 +0800 | [diff] [blame] | 524 | |
Konrad Rzeszutek Wilk | 01f37f2 | 2011-05-11 15:57:09 -0400 | [diff] [blame] | 525 | /* |
Konrad Rzeszutek Wilk | 4d05a28 | 2011-04-14 18:25:47 -0400 | [diff] [blame] | 526 | * Entry point to this code when a new device is created. Allocate the basic |
| 527 | * structures, and watch the store waiting for the hotplug scripts to tell us |
| 528 | * the device's physical major and minor numbers. Switch to InitWait. |
| 529 | */ |
Konrad Rzeszutek Wilk | 8b6bf74 | 2011-04-20 11:50:43 -0400 | [diff] [blame] | 530 | static int xen_blkbk_probe(struct xenbus_device *dev, |
| 531 | const struct xenbus_device_id *id) |
Konrad Rzeszutek Wilk | 4d05a28 | 2011-04-14 18:25:47 -0400 | [diff] [blame] | 532 | { |
| 533 | int err; |
| 534 | struct backend_info *be = kzalloc(sizeof(struct backend_info), |
| 535 | GFP_KERNEL); |
Tao Chen | 77387b8 | 2015-04-01 15:04:22 +0000 | [diff] [blame] | 536 | |
| 537 | /* match the pr_debug in xen_blkbk_remove */ |
| 538 | pr_debug("%s %p %d\n", __func__, dev, dev->otherend_id); |
| 539 | |
Konrad Rzeszutek Wilk | 4d05a28 | 2011-04-14 18:25:47 -0400 | [diff] [blame] | 540 | if (!be) { |
| 541 | xenbus_dev_fatal(dev, -ENOMEM, |
| 542 | "allocating backend structure"); |
| 543 | return -ENOMEM; |
| 544 | } |
| 545 | be->dev = dev; |
Jeremy Fitzhardinge | 5cf6e4f | 2010-02-11 16:07:31 -0800 | [diff] [blame] | 546 | dev_set_drvdata(&dev->dev, be); |
Konrad Rzeszutek Wilk | 4d05a28 | 2011-04-14 18:25:47 -0400 | [diff] [blame] | 547 | |
Konrad Rzeszutek Wilk | 8b6bf74 | 2011-04-20 11:50:43 -0400 | [diff] [blame] | 548 | be->blkif = xen_blkif_alloc(dev->otherend_id); |
Konrad Rzeszutek Wilk | 4d05a28 | 2011-04-14 18:25:47 -0400 | [diff] [blame] | 549 | if (IS_ERR(be->blkif)) { |
| 550 | err = PTR_ERR(be->blkif); |
| 551 | be->blkif = NULL; |
| 552 | xenbus_dev_fatal(dev, err, "creating block interface"); |
| 553 | goto fail; |
| 554 | } |
| 555 | |
| 556 | /* setup back pointer */ |
| 557 | be->blkif->be = be; |
| 558 | |
Jeremy Fitzhardinge | 8812293 | 2009-02-09 12:05:51 -0800 | [diff] [blame] | 559 | err = xenbus_watch_pathfmt(dev, &be->backend_watch, backend_changed, |
| 560 | "%s/%s", dev->nodename, "physical-device"); |
Konrad Rzeszutek Wilk | 4d05a28 | 2011-04-14 18:25:47 -0400 | [diff] [blame] | 561 | if (err) |
| 562 | goto fail; |
| 563 | |
Bob Liu | 86839c5 | 2015-06-03 13:40:03 +0800 | [diff] [blame] | 564 | err = xenbus_printf(XBT_NIL, dev->nodename, "max-ring-page-order", "%u", |
| 565 | xen_blkif_max_ring_order); |
| 566 | if (err) |
| 567 | pr_warn("%s write out 'max-ring-page-order' failed\n", __func__); |
| 568 | |
Konrad Rzeszutek Wilk | 4d05a28 | 2011-04-14 18:25:47 -0400 | [diff] [blame] | 569 | err = xenbus_switch_state(dev, XenbusStateInitWait); |
| 570 | if (err) |
| 571 | goto fail; |
| 572 | |
| 573 | return 0; |
| 574 | |
| 575 | fail: |
Tao Chen | 77387b8 | 2015-04-01 15:04:22 +0000 | [diff] [blame] | 576 | pr_warn("%s failed\n", __func__); |
Konrad Rzeszutek Wilk | 8b6bf74 | 2011-04-20 11:50:43 -0400 | [diff] [blame] | 577 | xen_blkbk_remove(dev); |
Konrad Rzeszutek Wilk | 4d05a28 | 2011-04-14 18:25:47 -0400 | [diff] [blame] | 578 | return err; |
| 579 | } |
| 580 | |
| 581 | |
Konrad Rzeszutek Wilk | 01f37f2 | 2011-05-11 15:57:09 -0400 | [diff] [blame] | 582 | /* |
Konrad Rzeszutek Wilk | 4d05a28 | 2011-04-14 18:25:47 -0400 | [diff] [blame] | 583 | * Callback received when the hotplug scripts have placed the physical-device |
| 584 | * node. Read it and the mode node, and create a vbd. If the frontend is |
| 585 | * ready, connect. |
| 586 | */ |
| 587 | static void backend_changed(struct xenbus_watch *watch, |
| 588 | const char **vec, unsigned int len) |
| 589 | { |
| 590 | int err; |
| 591 | unsigned major; |
| 592 | unsigned minor; |
| 593 | struct backend_info *be |
| 594 | = container_of(watch, struct backend_info, backend_watch); |
| 595 | struct xenbus_device *dev = be->dev; |
| 596 | int cdrom = 0; |
Jan Beulich | 9d09260 | 2012-12-20 10:31:11 +0000 | [diff] [blame] | 597 | unsigned long handle; |
Konrad Rzeszutek Wilk | 4d05a28 | 2011-04-14 18:25:47 -0400 | [diff] [blame] | 598 | char *device_type; |
| 599 | |
Tao Chen | 77387b8 | 2015-04-01 15:04:22 +0000 | [diff] [blame] | 600 | pr_debug("%s %p %d\n", __func__, dev, dev->otherend_id); |
Konrad Rzeszutek Wilk | 4d05a28 | 2011-04-14 18:25:47 -0400 | [diff] [blame] | 601 | |
| 602 | err = xenbus_scanf(XBT_NIL, dev->nodename, "physical-device", "%x:%x", |
| 603 | &major, &minor); |
| 604 | if (XENBUS_EXIST_ERR(err)) { |
Konrad Rzeszutek Wilk | 01f37f2 | 2011-05-11 15:57:09 -0400 | [diff] [blame] | 605 | /* |
| 606 | * Since this watch will fire once immediately after it is |
| 607 | * registered, we expect this. Ignore it, and wait for the |
| 608 | * hotplug scripts. |
| 609 | */ |
Konrad Rzeszutek Wilk | 4d05a28 | 2011-04-14 18:25:47 -0400 | [diff] [blame] | 610 | return; |
| 611 | } |
| 612 | if (err != 2) { |
| 613 | xenbus_dev_fatal(dev, err, "reading physical-device"); |
| 614 | return; |
| 615 | } |
| 616 | |
Jan Beulich | 9d09260 | 2012-12-20 10:31:11 +0000 | [diff] [blame] | 617 | if (be->major | be->minor) { |
| 618 | if (be->major != major || be->minor != minor) |
Tao Chen | 77387b8 | 2015-04-01 15:04:22 +0000 | [diff] [blame] | 619 | pr_warn("changing physical device (from %x:%x to %x:%x) not supported.\n", |
Jan Beulich | 9d09260 | 2012-12-20 10:31:11 +0000 | [diff] [blame] | 620 | be->major, be->minor, major, minor); |
Konrad Rzeszutek Wilk | 4d05a28 | 2011-04-14 18:25:47 -0400 | [diff] [blame] | 621 | return; |
| 622 | } |
| 623 | |
| 624 | be->mode = xenbus_read(XBT_NIL, dev->nodename, "mode", NULL); |
| 625 | if (IS_ERR(be->mode)) { |
| 626 | err = PTR_ERR(be->mode); |
| 627 | be->mode = NULL; |
| 628 | xenbus_dev_fatal(dev, err, "reading mode"); |
| 629 | return; |
| 630 | } |
| 631 | |
| 632 | device_type = xenbus_read(XBT_NIL, dev->otherend, "device-type", NULL); |
| 633 | if (!IS_ERR(device_type)) { |
| 634 | cdrom = strcmp(device_type, "cdrom") == 0; |
| 635 | kfree(device_type); |
| 636 | } |
| 637 | |
Jan Beulich | 9d09260 | 2012-12-20 10:31:11 +0000 | [diff] [blame] | 638 | /* Front end dir is a number, which is used as the handle. */ |
Jingoo Han | bb8e0e8 | 2013-09-11 14:20:07 -0700 | [diff] [blame] | 639 | err = kstrtoul(strrchr(dev->otherend, '/') + 1, 0, &handle); |
Jan Beulich | 9d09260 | 2012-12-20 10:31:11 +0000 | [diff] [blame] | 640 | if (err) |
| 641 | return; |
Konrad Rzeszutek Wilk | 4d05a28 | 2011-04-14 18:25:47 -0400 | [diff] [blame] | 642 | |
Jan Beulich | 9d09260 | 2012-12-20 10:31:11 +0000 | [diff] [blame] | 643 | be->major = major; |
| 644 | be->minor = minor; |
Konrad Rzeszutek Wilk | 4d05a28 | 2011-04-14 18:25:47 -0400 | [diff] [blame] | 645 | |
Jan Beulich | 9d09260 | 2012-12-20 10:31:11 +0000 | [diff] [blame] | 646 | err = xen_vbd_create(be->blkif, handle, major, minor, |
| 647 | !strchr(be->mode, 'w'), cdrom); |
Konrad Rzeszutek Wilk | 4d05a28 | 2011-04-14 18:25:47 -0400 | [diff] [blame] | 648 | |
Jan Beulich | 9d09260 | 2012-12-20 10:31:11 +0000 | [diff] [blame] | 649 | if (err) |
| 650 | xenbus_dev_fatal(dev, err, "creating vbd structure"); |
| 651 | else { |
Konrad Rzeszutek Wilk | 4d05a28 | 2011-04-14 18:25:47 -0400 | [diff] [blame] | 652 | err = xenvbd_sysfs_addif(dev); |
| 653 | if (err) { |
Konrad Rzeszutek Wilk | 3d81473 | 2011-05-12 16:53:56 -0400 | [diff] [blame] | 654 | xen_vbd_free(&be->blkif->vbd); |
Konrad Rzeszutek Wilk | 4d05a28 | 2011-04-14 18:25:47 -0400 | [diff] [blame] | 655 | xenbus_dev_fatal(dev, err, "creating sysfs entries"); |
Konrad Rzeszutek Wilk | 4d05a28 | 2011-04-14 18:25:47 -0400 | [diff] [blame] | 656 | } |
Jan Beulich | 9d09260 | 2012-12-20 10:31:11 +0000 | [diff] [blame] | 657 | } |
Konrad Rzeszutek Wilk | 4d05a28 | 2011-04-14 18:25:47 -0400 | [diff] [blame] | 658 | |
Jan Beulich | 9d09260 | 2012-12-20 10:31:11 +0000 | [diff] [blame] | 659 | if (err) { |
| 660 | kfree(be->mode); |
| 661 | be->mode = NULL; |
| 662 | be->major = 0; |
| 663 | be->minor = 0; |
| 664 | } else { |
Konrad Rzeszutek Wilk | 4d05a28 | 2011-04-14 18:25:47 -0400 | [diff] [blame] | 665 | /* We're potentially connected now */ |
Konrad Rzeszutek Wilk | 8b6bf74 | 2011-04-20 11:50:43 -0400 | [diff] [blame] | 666 | xen_update_blkif_status(be->blkif); |
Konrad Rzeszutek Wilk | 4d05a28 | 2011-04-14 18:25:47 -0400 | [diff] [blame] | 667 | } |
| 668 | } |
| 669 | |
| 670 | |
Konrad Rzeszutek Wilk | 01f37f2 | 2011-05-11 15:57:09 -0400 | [diff] [blame] | 671 | /* |
Konrad Rzeszutek Wilk | 4d05a28 | 2011-04-14 18:25:47 -0400 | [diff] [blame] | 672 | * Callback received when the frontend's state changes. |
| 673 | */ |
| 674 | static void frontend_changed(struct xenbus_device *dev, |
| 675 | enum xenbus_state frontend_state) |
| 676 | { |
Jeremy Fitzhardinge | 5cf6e4f | 2010-02-11 16:07:31 -0800 | [diff] [blame] | 677 | struct backend_info *be = dev_get_drvdata(&dev->dev); |
Konrad Rzeszutek Wilk | 4d05a28 | 2011-04-14 18:25:47 -0400 | [diff] [blame] | 678 | int err; |
| 679 | |
Tao Chen | 77387b8 | 2015-04-01 15:04:22 +0000 | [diff] [blame] | 680 | pr_debug("%s %p %s\n", __func__, dev, xenbus_strstate(frontend_state)); |
Konrad Rzeszutek Wilk | 4d05a28 | 2011-04-14 18:25:47 -0400 | [diff] [blame] | 681 | |
| 682 | switch (frontend_state) { |
| 683 | case XenbusStateInitialising: |
| 684 | if (dev->state == XenbusStateClosed) { |
Tao Chen | 77387b8 | 2015-04-01 15:04:22 +0000 | [diff] [blame] | 685 | pr_info("%s: prepare for reconnect\n", dev->nodename); |
Konrad Rzeszutek Wilk | 4d05a28 | 2011-04-14 18:25:47 -0400 | [diff] [blame] | 686 | xenbus_switch_state(dev, XenbusStateInitWait); |
| 687 | } |
| 688 | break; |
| 689 | |
| 690 | case XenbusStateInitialised: |
| 691 | case XenbusStateConnected: |
Konrad Rzeszutek Wilk | 01f37f2 | 2011-05-11 15:57:09 -0400 | [diff] [blame] | 692 | /* |
| 693 | * Ensure we connect even when two watches fire in |
Justin P. Mattock | 42b2aa8 | 2011-11-28 20:31:00 -0800 | [diff] [blame] | 694 | * close succession and we miss the intermediate value |
Konrad Rzeszutek Wilk | 01f37f2 | 2011-05-11 15:57:09 -0400 | [diff] [blame] | 695 | * of frontend_state. |
| 696 | */ |
Konrad Rzeszutek Wilk | 4d05a28 | 2011-04-14 18:25:47 -0400 | [diff] [blame] | 697 | if (dev->state == XenbusStateConnected) |
| 698 | break; |
| 699 | |
Konrad Rzeszutek Wilk | 01f37f2 | 2011-05-11 15:57:09 -0400 | [diff] [blame] | 700 | /* |
| 701 | * Enforce precondition before potential leak point. |
Joe Jin | 1bc05b0 | 2011-08-15 12:57:07 +0800 | [diff] [blame] | 702 | * xen_blkif_disconnect() is idempotent. |
Keir Fraser | 313d7b0 | 2010-11-24 22:08:20 -0800 | [diff] [blame] | 703 | */ |
Valentin Priescu | 814d04e | 2014-05-20 22:28:50 +0200 | [diff] [blame] | 704 | err = xen_blkif_disconnect(be->blkif); |
| 705 | if (err) { |
| 706 | xenbus_dev_fatal(dev, err, "pending I/O"); |
| 707 | break; |
| 708 | } |
Keir Fraser | 313d7b0 | 2010-11-24 22:08:20 -0800 | [diff] [blame] | 709 | |
Konrad Rzeszutek Wilk | 4d05a28 | 2011-04-14 18:25:47 -0400 | [diff] [blame] | 710 | err = connect_ring(be); |
| 711 | if (err) |
| 712 | break; |
Konrad Rzeszutek Wilk | 8b6bf74 | 2011-04-20 11:50:43 -0400 | [diff] [blame] | 713 | xen_update_blkif_status(be->blkif); |
Konrad Rzeszutek Wilk | 4d05a28 | 2011-04-14 18:25:47 -0400 | [diff] [blame] | 714 | break; |
| 715 | |
| 716 | case XenbusStateClosing: |
Konrad Rzeszutek Wilk | 4d05a28 | 2011-04-14 18:25:47 -0400 | [diff] [blame] | 717 | xenbus_switch_state(dev, XenbusStateClosing); |
| 718 | break; |
| 719 | |
| 720 | case XenbusStateClosed: |
Joe Jin | 6f5986b | 2011-08-15 12:51:31 +0800 | [diff] [blame] | 721 | xen_blkif_disconnect(be->blkif); |
Konrad Rzeszutek Wilk | 4d05a28 | 2011-04-14 18:25:47 -0400 | [diff] [blame] | 722 | xenbus_switch_state(dev, XenbusStateClosed); |
| 723 | if (xenbus_dev_is_online(dev)) |
| 724 | break; |
| 725 | /* fall through if not online */ |
| 726 | case XenbusStateUnknown: |
Joe Jin | 1bc05b0 | 2011-08-15 12:57:07 +0800 | [diff] [blame] | 727 | /* implies xen_blkif_disconnect() via xen_blkbk_remove() */ |
Konrad Rzeszutek Wilk | 4d05a28 | 2011-04-14 18:25:47 -0400 | [diff] [blame] | 728 | device_unregister(&dev->dev); |
| 729 | break; |
| 730 | |
| 731 | default: |
| 732 | xenbus_dev_fatal(dev, -EINVAL, "saw state %d at frontend", |
| 733 | frontend_state); |
| 734 | break; |
| 735 | } |
| 736 | } |
| 737 | |
| 738 | |
| 739 | /* ** Connection ** */ |
| 740 | |
| 741 | |
Konrad Rzeszutek Wilk | 01f37f2 | 2011-05-11 15:57:09 -0400 | [diff] [blame] | 742 | /* |
Konrad Rzeszutek Wilk | 4d05a28 | 2011-04-14 18:25:47 -0400 | [diff] [blame] | 743 | * Write the physical details regarding the block device to the store, and |
| 744 | * switch to Connected state. |
| 745 | */ |
| 746 | static void connect(struct backend_info *be) |
| 747 | { |
| 748 | struct xenbus_transaction xbt; |
| 749 | int err; |
| 750 | struct xenbus_device *dev = be->dev; |
| 751 | |
Tao Chen | 77387b8 | 2015-04-01 15:04:22 +0000 | [diff] [blame] | 752 | pr_debug("%s %s\n", __func__, dev->otherend); |
Konrad Rzeszutek Wilk | 4d05a28 | 2011-04-14 18:25:47 -0400 | [diff] [blame] | 753 | |
| 754 | /* Supply the information about the device the frontend needs */ |
| 755 | again: |
| 756 | err = xenbus_transaction_start(&xbt); |
| 757 | if (err) { |
| 758 | xenbus_dev_fatal(dev, err, "starting transaction"); |
| 759 | return; |
| 760 | } |
| 761 | |
Konrad Rzeszutek Wilk | 29bde09 | 2011-10-10 00:42:22 -0400 | [diff] [blame] | 762 | /* If we can't advertise it is OK. */ |
Konrad Rzeszutek Wilk | 3389bb8 | 2012-03-14 13:04:00 -0400 | [diff] [blame] | 763 | xen_blkbk_flush_diskcache(xbt, be, be->blkif->vbd.flush_support); |
| 764 | |
| 765 | xen_blkbk_discard(xbt, be); |
| 766 | |
| 767 | xen_blkbk_barrier(xbt, be, be->blkif->vbd.flush_support); |
Konrad Rzeszutek Wilk | 29bde09 | 2011-10-10 00:42:22 -0400 | [diff] [blame] | 768 | |
Roger Pau Monne | 0a8704a | 2012-10-24 18:58:45 +0200 | [diff] [blame] | 769 | err = xenbus_printf(xbt, dev->nodename, "feature-persistent", "%u", 1); |
| 770 | if (err) { |
| 771 | xenbus_dev_fatal(dev, err, "writing %s/feature-persistent", |
| 772 | dev->nodename); |
| 773 | goto abort; |
| 774 | } |
Roger Pau Monne | 402b27f | 2013-04-18 16:06:54 +0200 | [diff] [blame] | 775 | err = xenbus_printf(xbt, dev->nodename, "feature-max-indirect-segments", "%u", |
| 776 | MAX_INDIRECT_SEGMENTS); |
| 777 | if (err) |
| 778 | dev_warn(&dev->dev, "writing %s/feature-max-indirect-segments (%d)", |
| 779 | dev->nodename, err); |
Roger Pau Monne | 0a8704a | 2012-10-24 18:58:45 +0200 | [diff] [blame] | 780 | |
Konrad Rzeszutek Wilk | 4d05a28 | 2011-04-14 18:25:47 -0400 | [diff] [blame] | 781 | err = xenbus_printf(xbt, dev->nodename, "sectors", "%llu", |
Konrad Rzeszutek Wilk | 42c7841 | 2011-04-20 11:21:43 -0400 | [diff] [blame] | 782 | (unsigned long long)vbd_sz(&be->blkif->vbd)); |
Konrad Rzeszutek Wilk | 4d05a28 | 2011-04-14 18:25:47 -0400 | [diff] [blame] | 783 | if (err) { |
| 784 | xenbus_dev_fatal(dev, err, "writing %s/sectors", |
| 785 | dev->nodename); |
| 786 | goto abort; |
| 787 | } |
| 788 | |
| 789 | /* FIXME: use a typename instead */ |
| 790 | err = xenbus_printf(xbt, dev->nodename, "info", "%u", |
Konrad Rzeszutek Wilk | 42c7841 | 2011-04-20 11:21:43 -0400 | [diff] [blame] | 791 | be->blkif->vbd.type | |
| 792 | (be->blkif->vbd.readonly ? VDISK_READONLY : 0)); |
Konrad Rzeszutek Wilk | 4d05a28 | 2011-04-14 18:25:47 -0400 | [diff] [blame] | 793 | if (err) { |
| 794 | xenbus_dev_fatal(dev, err, "writing %s/info", |
| 795 | dev->nodename); |
| 796 | goto abort; |
| 797 | } |
| 798 | err = xenbus_printf(xbt, dev->nodename, "sector-size", "%lu", |
Konrad Rzeszutek Wilk | 42c7841 | 2011-04-20 11:21:43 -0400 | [diff] [blame] | 799 | (unsigned long) |
| 800 | bdev_logical_block_size(be->blkif->vbd.bdev)); |
Konrad Rzeszutek Wilk | 4d05a28 | 2011-04-14 18:25:47 -0400 | [diff] [blame] | 801 | if (err) { |
| 802 | xenbus_dev_fatal(dev, err, "writing %s/sector-size", |
| 803 | dev->nodename); |
| 804 | goto abort; |
| 805 | } |
Stefan Bader | 7c4d7d7 | 2013-05-13 16:28:15 +0200 | [diff] [blame] | 806 | err = xenbus_printf(xbt, dev->nodename, "physical-sector-size", "%u", |
| 807 | bdev_physical_block_size(be->blkif->vbd.bdev)); |
| 808 | if (err) |
| 809 | xenbus_dev_error(dev, err, "writing %s/physical-sector-size", |
| 810 | dev->nodename); |
Konrad Rzeszutek Wilk | 4d05a28 | 2011-04-14 18:25:47 -0400 | [diff] [blame] | 811 | |
| 812 | err = xenbus_transaction_end(xbt, 0); |
| 813 | if (err == -EAGAIN) |
| 814 | goto again; |
| 815 | if (err) |
| 816 | xenbus_dev_fatal(dev, err, "ending transaction"); |
| 817 | |
| 818 | err = xenbus_switch_state(dev, XenbusStateConnected); |
| 819 | if (err) |
Joe Perches | 08b8bfc | 2011-06-12 09:21:13 -0700 | [diff] [blame] | 820 | xenbus_dev_fatal(dev, err, "%s: switching to Connected state", |
Konrad Rzeszutek Wilk | 4d05a28 | 2011-04-14 18:25:47 -0400 | [diff] [blame] | 821 | dev->nodename); |
| 822 | |
| 823 | return; |
| 824 | abort: |
| 825 | xenbus_transaction_end(xbt, 1); |
| 826 | } |
| 827 | |
| 828 | |
| 829 | static int connect_ring(struct backend_info *be) |
| 830 | { |
| 831 | struct xenbus_device *dev = be->dev; |
Julien Grall | 9cce291 | 2015-10-13 17:50:11 +0100 | [diff] [blame^] | 832 | unsigned int ring_ref[XENBUS_MAX_RING_GRANTS]; |
Bob Liu | 86839c5 | 2015-06-03 13:40:03 +0800 | [diff] [blame] | 833 | unsigned int evtchn, nr_grefs, ring_page_order; |
Roger Pau Monne | 0a8704a | 2012-10-24 18:58:45 +0200 | [diff] [blame] | 834 | unsigned int pers_grants; |
Konrad Rzeszutek Wilk | 4d05a28 | 2011-04-14 18:25:47 -0400 | [diff] [blame] | 835 | char protocol[64] = ""; |
Bob Liu | 69b91ed | 2015-06-03 13:40:01 +0800 | [diff] [blame] | 836 | struct pending_req *req, *n; |
| 837 | int err, i, j; |
Konrad Rzeszutek Wilk | 4d05a28 | 2011-04-14 18:25:47 -0400 | [diff] [blame] | 838 | |
Tao Chen | 77387b8 | 2015-04-01 15:04:22 +0000 | [diff] [blame] | 839 | pr_debug("%s %s\n", __func__, dev->otherend); |
Konrad Rzeszutek Wilk | 4d05a28 | 2011-04-14 18:25:47 -0400 | [diff] [blame] | 840 | |
Bob Liu | 86839c5 | 2015-06-03 13:40:03 +0800 | [diff] [blame] | 841 | err = xenbus_scanf(XBT_NIL, dev->otherend, "event-channel", "%u", |
| 842 | &evtchn); |
| 843 | if (err != 1) { |
| 844 | err = -EINVAL; |
| 845 | xenbus_dev_fatal(dev, err, "reading %s/event-channel", |
Konrad Rzeszutek Wilk | 4d05a28 | 2011-04-14 18:25:47 -0400 | [diff] [blame] | 846 | dev->otherend); |
| 847 | return err; |
| 848 | } |
Bob Liu | 86839c5 | 2015-06-03 13:40:03 +0800 | [diff] [blame] | 849 | pr_info("event-channel %u\n", evtchn); |
| 850 | |
| 851 | err = xenbus_scanf(XBT_NIL, dev->otherend, "ring-page-order", "%u", |
| 852 | &ring_page_order); |
| 853 | if (err != 1) { |
| 854 | err = xenbus_scanf(XBT_NIL, dev->otherend, "ring-ref", |
| 855 | "%u", &ring_ref[0]); |
| 856 | if (err != 1) { |
| 857 | err = -EINVAL; |
| 858 | xenbus_dev_fatal(dev, err, "reading %s/ring-ref", |
| 859 | dev->otherend); |
| 860 | return err; |
| 861 | } |
| 862 | nr_grefs = 1; |
| 863 | pr_info("%s:using single page: ring-ref %d\n", dev->otherend, |
| 864 | ring_ref[0]); |
| 865 | } else { |
| 866 | unsigned int i; |
| 867 | |
| 868 | if (ring_page_order > xen_blkif_max_ring_order) { |
| 869 | err = -EINVAL; |
| 870 | xenbus_dev_fatal(dev, err, "%s/request %d ring page order exceed max:%d", |
| 871 | dev->otherend, ring_page_order, |
| 872 | xen_blkif_max_ring_order); |
| 873 | return err; |
| 874 | } |
| 875 | |
| 876 | nr_grefs = 1 << ring_page_order; |
| 877 | for (i = 0; i < nr_grefs; i++) { |
| 878 | char ring_ref_name[RINGREF_NAME_LEN]; |
| 879 | |
| 880 | snprintf(ring_ref_name, RINGREF_NAME_LEN, "ring-ref%u", i); |
| 881 | err = xenbus_scanf(XBT_NIL, dev->otherend, ring_ref_name, |
| 882 | "%u", &ring_ref[i]); |
| 883 | if (err != 1) { |
| 884 | err = -EINVAL; |
| 885 | xenbus_dev_fatal(dev, err, "reading %s/%s", |
| 886 | dev->otherend, ring_ref_name); |
| 887 | return err; |
| 888 | } |
| 889 | pr_info("ring-ref%u: %u\n", i, ring_ref[i]); |
| 890 | } |
| 891 | } |
Konrad Rzeszutek Wilk | 4d05a28 | 2011-04-14 18:25:47 -0400 | [diff] [blame] | 892 | |
David Vrabel | b042a3c | 2015-02-05 17:09:56 +0000 | [diff] [blame] | 893 | be->blkif->blk_protocol = BLKIF_PROTOCOL_DEFAULT; |
Konrad Rzeszutek Wilk | 4d05a28 | 2011-04-14 18:25:47 -0400 | [diff] [blame] | 894 | err = xenbus_gather(XBT_NIL, dev->otherend, "protocol", |
| 895 | "%63s", protocol, NULL); |
| 896 | if (err) |
David Vrabel | b042a3c | 2015-02-05 17:09:56 +0000 | [diff] [blame] | 897 | strcpy(protocol, "unspecified, assuming default"); |
Konrad Rzeszutek Wilk | 4d05a28 | 2011-04-14 18:25:47 -0400 | [diff] [blame] | 898 | else if (0 == strcmp(protocol, XEN_IO_PROTO_ABI_NATIVE)) |
| 899 | be->blkif->blk_protocol = BLKIF_PROTOCOL_NATIVE; |
| 900 | else if (0 == strcmp(protocol, XEN_IO_PROTO_ABI_X86_32)) |
| 901 | be->blkif->blk_protocol = BLKIF_PROTOCOL_X86_32; |
| 902 | else if (0 == strcmp(protocol, XEN_IO_PROTO_ABI_X86_64)) |
| 903 | be->blkif->blk_protocol = BLKIF_PROTOCOL_X86_64; |
| 904 | else { |
| 905 | xenbus_dev_fatal(dev, err, "unknown fe protocol %s", protocol); |
| 906 | return -1; |
| 907 | } |
Roger Pau Monne | 0a8704a | 2012-10-24 18:58:45 +0200 | [diff] [blame] | 908 | err = xenbus_gather(XBT_NIL, dev->otherend, |
Roger Pau Monne | cb5bd4d | 2012-11-02 16:43:04 +0100 | [diff] [blame] | 909 | "feature-persistent", "%u", |
Roger Pau Monne | 0a8704a | 2012-10-24 18:58:45 +0200 | [diff] [blame] | 910 | &pers_grants, NULL); |
| 911 | if (err) |
| 912 | pers_grants = 0; |
| 913 | |
| 914 | be->blkif->vbd.feature_gnt_persistent = pers_grants; |
| 915 | be->blkif->vbd.overflow_max_grants = 0; |
Bob Liu | 86839c5 | 2015-06-03 13:40:03 +0800 | [diff] [blame] | 916 | be->blkif->nr_ring_pages = nr_grefs; |
Roger Pau Monne | 0a8704a | 2012-10-24 18:58:45 +0200 | [diff] [blame] | 917 | |
Bob Liu | 86839c5 | 2015-06-03 13:40:03 +0800 | [diff] [blame] | 918 | pr_info("ring-pages:%d, event-channel %d, protocol %d (%s) %s\n", |
| 919 | nr_grefs, evtchn, be->blkif->blk_protocol, protocol, |
Roger Pau Monne | 0a8704a | 2012-10-24 18:58:45 +0200 | [diff] [blame] | 920 | pers_grants ? "persistent grants" : ""); |
Konrad Rzeszutek Wilk | 4d05a28 | 2011-04-14 18:25:47 -0400 | [diff] [blame] | 921 | |
Bob Liu | 86839c5 | 2015-06-03 13:40:03 +0800 | [diff] [blame] | 922 | for (i = 0; i < nr_grefs * XEN_BLKIF_REQS_PER_PAGE; i++) { |
Bob Liu | 69b91ed | 2015-06-03 13:40:01 +0800 | [diff] [blame] | 923 | req = kzalloc(sizeof(*req), GFP_KERNEL); |
| 924 | if (!req) |
| 925 | goto fail; |
| 926 | list_add_tail(&req->free_list, &be->blkif->pending_free); |
| 927 | for (j = 0; j < MAX_INDIRECT_SEGMENTS; j++) { |
| 928 | req->segments[j] = kzalloc(sizeof(*req->segments[0]), GFP_KERNEL); |
| 929 | if (!req->segments[j]) |
| 930 | goto fail; |
| 931 | } |
| 932 | for (j = 0; j < MAX_INDIRECT_PAGES; j++) { |
| 933 | req->indirect_pages[j] = kzalloc(sizeof(*req->indirect_pages[0]), |
| 934 | GFP_KERNEL); |
| 935 | if (!req->indirect_pages[j]) |
| 936 | goto fail; |
| 937 | } |
| 938 | } |
| 939 | |
Konrad Rzeszutek Wilk | 4d05a28 | 2011-04-14 18:25:47 -0400 | [diff] [blame] | 940 | /* Map the shared frame, irq etc. */ |
Bob Liu | 86839c5 | 2015-06-03 13:40:03 +0800 | [diff] [blame] | 941 | err = xen_blkif_map(be->blkif, ring_ref, nr_grefs, evtchn); |
Konrad Rzeszutek Wilk | 4d05a28 | 2011-04-14 18:25:47 -0400 | [diff] [blame] | 942 | if (err) { |
Bob Liu | 86839c5 | 2015-06-03 13:40:03 +0800 | [diff] [blame] | 943 | xenbus_dev_fatal(dev, err, "mapping ring-ref port %u", evtchn); |
Konrad Rzeszutek Wilk | 4d05a28 | 2011-04-14 18:25:47 -0400 | [diff] [blame] | 944 | return err; |
| 945 | } |
| 946 | |
| 947 | return 0; |
Bob Liu | 69b91ed | 2015-06-03 13:40:01 +0800 | [diff] [blame] | 948 | |
| 949 | fail: |
| 950 | list_for_each_entry_safe(req, n, &be->blkif->pending_free, free_list) { |
| 951 | list_del(&req->free_list); |
| 952 | for (j = 0; j < MAX_INDIRECT_SEGMENTS; j++) { |
| 953 | if (!req->segments[j]) |
| 954 | break; |
| 955 | kfree(req->segments[j]); |
| 956 | } |
| 957 | for (j = 0; j < MAX_INDIRECT_PAGES; j++) { |
| 958 | if (!req->indirect_pages[j]) |
| 959 | break; |
| 960 | kfree(req->indirect_pages[j]); |
| 961 | } |
| 962 | kfree(req); |
| 963 | } |
| 964 | return -ENOMEM; |
Konrad Rzeszutek Wilk | 4d05a28 | 2011-04-14 18:25:47 -0400 | [diff] [blame] | 965 | } |
| 966 | |
Konrad Rzeszutek Wilk | 8b6bf74 | 2011-04-20 11:50:43 -0400 | [diff] [blame] | 967 | static const struct xenbus_device_id xen_blkbk_ids[] = { |
Konrad Rzeszutek Wilk | 4d05a28 | 2011-04-14 18:25:47 -0400 | [diff] [blame] | 968 | { "vbd" }, |
| 969 | { "" } |
| 970 | }; |
| 971 | |
David Vrabel | 95afae4 | 2014-09-08 17:30:41 +0100 | [diff] [blame] | 972 | static struct xenbus_driver xen_blkbk_driver = { |
| 973 | .ids = xen_blkbk_ids, |
Konrad Rzeszutek Wilk | 8b6bf74 | 2011-04-20 11:50:43 -0400 | [diff] [blame] | 974 | .probe = xen_blkbk_probe, |
| 975 | .remove = xen_blkbk_remove, |
Konrad Rzeszutek Wilk | 4d05a28 | 2011-04-14 18:25:47 -0400 | [diff] [blame] | 976 | .otherend_changed = frontend_changed |
David Vrabel | 95afae4 | 2014-09-08 17:30:41 +0100 | [diff] [blame] | 977 | }; |
Konrad Rzeszutek Wilk | 4d05a28 | 2011-04-14 18:25:47 -0400 | [diff] [blame] | 978 | |
Konrad Rzeszutek Wilk | 8b6bf74 | 2011-04-20 11:50:43 -0400 | [diff] [blame] | 979 | int xen_blkif_xenbus_init(void) |
Konrad Rzeszutek Wilk | 4d05a28 | 2011-04-14 18:25:47 -0400 | [diff] [blame] | 980 | { |
Jan Beulich | 73db144 | 2011-12-22 09:08:13 +0000 | [diff] [blame] | 981 | return xenbus_register_backend(&xen_blkbk_driver); |
Konrad Rzeszutek Wilk | 4d05a28 | 2011-04-14 18:25:47 -0400 | [diff] [blame] | 982 | } |