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