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