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