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