Jeremy Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 1 | /* |
| 2 | * blkfront.c |
| 3 | * |
| 4 | * XenLinux virtual block device driver. |
| 5 | * |
| 6 | * Copyright (c) 2003-2004, Keir Fraser & Steve Hand |
| 7 | * Modifications by Mark A. Williamson are (c) Intel Research Cambridge |
| 8 | * Copyright (c) 2004, Christian Limpach |
| 9 | * Copyright (c) 2004, Andrew Warfield |
| 10 | * Copyright (c) 2005, Christopher Clark |
| 11 | * Copyright (c) 2005, XenSource Ltd |
| 12 | * |
| 13 | * This program is free software; you can redistribute it and/or |
| 14 | * modify it under the terms of the GNU General Public License version 2 |
| 15 | * as published by the Free Software Foundation; or, when distributed |
| 16 | * separately from the Linux kernel or incorporated into other |
| 17 | * software packages, subject to the following license: |
| 18 | * |
| 19 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 20 | * of this source file (the "Software"), to deal in the Software without |
| 21 | * restriction, including without limitation the rights to use, copy, modify, |
| 22 | * merge, publish, distribute, sublicense, and/or sell copies of the Software, |
| 23 | * and to permit persons to whom the Software is furnished to do so, subject to |
| 24 | * the following conditions: |
| 25 | * |
| 26 | * The above copyright notice and this permission notice shall be included in |
| 27 | * all copies or substantial portions of the Software. |
| 28 | * |
| 29 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 30 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 31 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 32 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 33 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
| 34 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS |
| 35 | * IN THE SOFTWARE. |
| 36 | */ |
| 37 | |
| 38 | #include <linux/interrupt.h> |
| 39 | #include <linux/blkdev.h> |
Bob Liu | 907c3eb | 2015-07-13 17:55:24 +0800 | [diff] [blame] | 40 | #include <linux/blk-mq.h> |
Ian Campbell | 597592d | 2008-02-21 13:03:45 -0800 | [diff] [blame] | 41 | #include <linux/hdreg.h> |
Christian Limpach | 440a01a | 2008-06-17 10:47:08 +0200 | [diff] [blame] | 42 | #include <linux/cdrom.h> |
Jeremy Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 43 | #include <linux/module.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 44 | #include <linux/slab.h> |
Christoph Hellwig | b81e0c2 | 2021-09-20 14:33:25 +0200 | [diff] [blame] | 45 | #include <linux/major.h> |
Arnd Bergmann | 2a48fc0 | 2010-06-02 14:28:52 +0200 | [diff] [blame] | 46 | #include <linux/mutex.h> |
Jens Axboe | 9e973e6 | 2009-02-24 08:10:09 +0100 | [diff] [blame] | 47 | #include <linux/scatterlist.h> |
Akinobu Mita | 34ae2e4 | 2012-01-21 00:15:26 +0900 | [diff] [blame] | 48 | #include <linux/bitmap.h> |
Roger Pau Monne | 155b7ed | 2013-03-18 17:49:34 +0100 | [diff] [blame] | 49 | #include <linux/list.h> |
Juergen Gross | a46b536 | 2018-08-13 16:01:11 +0200 | [diff] [blame] | 50 | #include <linux/workqueue.h> |
Juergen Gross | 3a169c0 | 2020-04-03 11:00:34 +0200 | [diff] [blame] | 51 | #include <linux/sched/mm.h> |
Jeremy Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 52 | |
Jeremy Fitzhardinge | 1ccbf53 | 2009-10-06 15:11:14 -0700 | [diff] [blame] | 53 | #include <xen/xen.h> |
Jeremy Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 54 | #include <xen/xenbus.h> |
| 55 | #include <xen/grant_table.h> |
| 56 | #include <xen/events.h> |
| 57 | #include <xen/page.h> |
Stefano Stabellini | c1c5413 | 2010-05-14 12:44:30 +0100 | [diff] [blame] | 58 | #include <xen/platform_pci.h> |
Jeremy Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 59 | |
| 60 | #include <xen/interface/grant_table.h> |
| 61 | #include <xen/interface/io/blkif.h> |
Markus Armbruster | 3e33423 | 2008-04-02 10:54:02 -0700 | [diff] [blame] | 62 | #include <xen/interface/io/protocols.h> |
Jeremy Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 63 | |
| 64 | #include <asm/xen/hypervisor.h> |
| 65 | |
Julien Grall | 6cc56833 | 2015-08-13 13:13:35 +0100 | [diff] [blame] | 66 | /* |
| 67 | * The minimal size of segment supported by the block framework is PAGE_SIZE. |
| 68 | * When Linux is using a different page size than Xen, it may not be possible |
| 69 | * to put all the data in a single segment. |
| 70 | * This can happen when the backend doesn't support indirect descriptor and |
| 71 | * therefore the maximum amount of data that a request can carry is |
| 72 | * BLKIF_MAX_SEGMENTS_PER_REQUEST * XEN_PAGE_SIZE = 44KB |
| 73 | * |
| 74 | * Note that we only support one extra request. So the Linux page size |
| 75 | * should be <= ( 2 * BLKIF_MAX_SEGMENTS_PER_REQUEST * XEN_PAGE_SIZE) = |
| 76 | * 88KB. |
| 77 | */ |
| 78 | #define HAS_EXTRA_REQ (BLKIF_MAX_SEGMENTS_PER_REQUEST < XEN_PFN_PER_PAGE) |
| 79 | |
Jeremy Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 80 | enum blkif_state { |
| 81 | BLKIF_STATE_DISCONNECTED, |
| 82 | BLKIF_STATE_CONNECTED, |
| 83 | BLKIF_STATE_SUSPENDED, |
Juergen Gross | b94e4b1 | 2021-07-30 12:38:54 +0200 | [diff] [blame] | 84 | BLKIF_STATE_ERROR, |
Jeremy Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 85 | }; |
| 86 | |
Roger Pau Monne | 0a8704a | 2012-10-24 18:58:45 +0200 | [diff] [blame] | 87 | struct grant { |
| 88 | grant_ref_t gref; |
Julien Grall | a7a6df2 | 2015-06-30 11:58:51 +0100 | [diff] [blame] | 89 | struct page *page; |
Roger Pau Monne | 155b7ed | 2013-03-18 17:49:34 +0100 | [diff] [blame] | 90 | struct list_head node; |
Roger Pau Monne | 0a8704a | 2012-10-24 18:58:45 +0200 | [diff] [blame] | 91 | }; |
| 92 | |
Julien Grall | 6cc56833 | 2015-08-13 13:13:35 +0100 | [diff] [blame] | 93 | enum blk_req_status { |
Juergen Gross | b94e4b1 | 2021-07-30 12:38:54 +0200 | [diff] [blame] | 94 | REQ_PROCESSING, |
Julien Grall | 6cc56833 | 2015-08-13 13:13:35 +0100 | [diff] [blame] | 95 | REQ_WAITING, |
| 96 | REQ_DONE, |
| 97 | REQ_ERROR, |
| 98 | REQ_EOPNOTSUPP, |
| 99 | }; |
| 100 | |
Jeremy Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 101 | struct blk_shadow { |
| 102 | struct blkif_request req; |
Jeremy Fitzhardinge | a945b98 | 2010-11-01 17:03:14 -0400 | [diff] [blame] | 103 | struct request *request; |
Roger Pau Monne | 402b27f | 2013-04-18 16:06:54 +0200 | [diff] [blame] | 104 | struct grant **grants_used; |
| 105 | struct grant **indirect_grants; |
Roger Pau Monne | b764915 | 2013-05-02 10:58:50 +0200 | [diff] [blame] | 106 | struct scatterlist *sg; |
Julien Grall | c004a6f | 2015-07-22 16:44:54 +0100 | [diff] [blame] | 107 | unsigned int num_sg; |
Julien Grall | 6cc56833 | 2015-08-13 13:13:35 +0100 | [diff] [blame] | 108 | enum blk_req_status status; |
| 109 | |
| 110 | #define NO_ASSOCIATED_ID ~0UL |
| 111 | /* |
| 112 | * Id of the sibling if we ever need 2 requests when handling a |
| 113 | * block I/O request |
| 114 | */ |
| 115 | unsigned long associated_id; |
Roger Pau Monne | 402b27f | 2013-04-18 16:06:54 +0200 | [diff] [blame] | 116 | }; |
| 117 | |
Christoph Hellwig | 2609587 | 2017-04-20 16:03:08 +0200 | [diff] [blame] | 118 | struct blkif_req { |
Bart Van Assche | 31c4ccc | 2017-07-21 19:11:10 +0200 | [diff] [blame] | 119 | blk_status_t error; |
Christoph Hellwig | 2609587 | 2017-04-20 16:03:08 +0200 | [diff] [blame] | 120 | }; |
| 121 | |
| 122 | static inline struct blkif_req *blkif_req(struct request *rq) |
| 123 | { |
| 124 | return blk_mq_rq_to_pdu(rq); |
| 125 | } |
| 126 | |
Arnd Bergmann | 2a48fc0 | 2010-06-02 14:28:52 +0200 | [diff] [blame] | 127 | static DEFINE_MUTEX(blkfront_mutex); |
Alexey Dobriyan | 83d5cde | 2009-09-21 17:01:13 -0700 | [diff] [blame] | 128 | static const struct block_device_operations xlvbd_block_fops; |
Juergen Gross | a46b536 | 2018-08-13 16:01:11 +0200 | [diff] [blame] | 129 | static struct delayed_work blkfront_work; |
| 130 | static LIST_HEAD(info_list); |
Jeremy Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 131 | |
Roger Pau Monne | 402b27f | 2013-04-18 16:06:54 +0200 | [diff] [blame] | 132 | /* |
| 133 | * Maximum number of segments in indirect requests, the actual value used by |
| 134 | * the frontend driver is the minimum of this value and the value provided |
| 135 | * by the backend driver. |
| 136 | */ |
| 137 | |
| 138 | static unsigned int xen_blkif_max_segments = 32; |
Joe Perches | 5657a81 | 2018-05-24 13:38:59 -0600 | [diff] [blame] | 139 | module_param_named(max_indirect_segments, xen_blkif_max_segments, uint, 0444); |
Jan Beulich | 14e710f | 2016-02-10 04:21:15 -0700 | [diff] [blame] | 140 | MODULE_PARM_DESC(max_indirect_segments, |
| 141 | "Maximum amount of segments in indirect requests (default is 32)"); |
Roger Pau Monne | 402b27f | 2013-04-18 16:06:54 +0200 | [diff] [blame] | 142 | |
Bob Liu | 28d949b | 2015-11-14 11:12:14 +0800 | [diff] [blame] | 143 | static unsigned int xen_blkif_max_queues = 4; |
Joe Perches | 5657a81 | 2018-05-24 13:38:59 -0600 | [diff] [blame] | 144 | module_param_named(max_queues, xen_blkif_max_queues, uint, 0444); |
Bob Liu | 28d949b | 2015-11-14 11:12:14 +0800 | [diff] [blame] | 145 | MODULE_PARM_DESC(max_queues, "Maximum number of hardware queues/rings used per virtual disk"); |
| 146 | |
Bob Liu | 86839c5 | 2015-06-03 13:40:03 +0800 | [diff] [blame] | 147 | /* |
| 148 | * Maximum order of pages to be used for the shared ring between front and |
| 149 | * backend, 4KB page granularity is used. |
| 150 | */ |
| 151 | static unsigned int xen_blkif_max_ring_order; |
Joe Perches | 5657a81 | 2018-05-24 13:38:59 -0600 | [diff] [blame] | 152 | module_param_named(max_ring_page_order, xen_blkif_max_ring_order, int, 0444); |
Bob Liu | 86839c5 | 2015-06-03 13:40:03 +0800 | [diff] [blame] | 153 | MODULE_PARM_DESC(max_ring_page_order, "Maximum order of pages to be used for the shared ring"); |
| 154 | |
Julien Grall | c004a6f | 2015-07-22 16:44:54 +0100 | [diff] [blame] | 155 | #define BLK_RING_SIZE(info) \ |
| 156 | __CONST_RING_SIZE(blkif, XEN_PAGE_SIZE * (info)->nr_ring_pages) |
| 157 | |
Bob Liu | 86839c5 | 2015-06-03 13:40:03 +0800 | [diff] [blame] | 158 | /* |
Konrad Rzeszutek Wilk | 6f03a7f | 2015-11-16 15:14:41 -0500 | [diff] [blame] | 159 | * ring-ref%u i=(-1UL) would take 11 characters + 'ring-ref' is 8, so 19 |
| 160 | * characters are enough. Define to 20 to keep consistent with backend. |
Bob Liu | 86839c5 | 2015-06-03 13:40:03 +0800 | [diff] [blame] | 161 | */ |
| 162 | #define RINGREF_NAME_LEN (20) |
Bob Liu | 28d949b | 2015-11-14 11:12:14 +0800 | [diff] [blame] | 163 | /* |
| 164 | * queue-%u would take 7 + 10(UINT_MAX) = 17 characters. |
| 165 | */ |
| 166 | #define QUEUE_NAME_LEN (17) |
Jeremy Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 167 | |
| 168 | /* |
Bob Liu | 81f3516 | 2015-11-14 11:12:11 +0800 | [diff] [blame] | 169 | * Per-ring info. |
| 170 | * Every blkfront device can associate with one or more blkfront_ring_info, |
| 171 | * depending on how many hardware queues/rings to be used. |
| 172 | */ |
| 173 | struct blkfront_ring_info { |
Bob Liu | 1165956 | 2015-11-14 11:12:13 +0800 | [diff] [blame] | 174 | /* Lock to protect data in every ring buffer. */ |
| 175 | spinlock_t ring_lock; |
Bob Liu | 81f3516 | 2015-11-14 11:12:11 +0800 | [diff] [blame] | 176 | struct blkif_front_ring ring; |
| 177 | unsigned int ring_ref[XENBUS_MAX_RING_GRANTS]; |
| 178 | unsigned int evtchn, irq; |
| 179 | struct work_struct work; |
| 180 | struct gnttab_free_callback callback; |
Bob Liu | 81f3516 | 2015-11-14 11:12:11 +0800 | [diff] [blame] | 181 | struct list_head indirect_pages; |
Bob Liu | 73716df | 2015-11-16 16:51:39 -0500 | [diff] [blame] | 182 | struct list_head grants; |
| 183 | unsigned int persistent_gnts_c; |
Bob Liu | 81f3516 | 2015-11-14 11:12:11 +0800 | [diff] [blame] | 184 | unsigned long shadow_free; |
| 185 | struct blkfront_info *dev_info; |
Juergen Gross | 0265d6e | 2020-01-17 15:39:55 +0100 | [diff] [blame] | 186 | struct blk_shadow shadow[]; |
Bob Liu | 81f3516 | 2015-11-14 11:12:11 +0800 | [diff] [blame] | 187 | }; |
| 188 | |
| 189 | /* |
Jeremy Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 190 | * We have one of these per vbd, whether ide, scsi or 'other'. They |
| 191 | * hang in private_data off the gendisk structure. We may end up |
| 192 | * putting all kinds of interesting stuff here :-) |
| 193 | */ |
| 194 | struct blkfront_info |
| 195 | { |
Daniel Stodden | b70f5fa | 2010-04-30 22:01:19 +0000 | [diff] [blame] | 196 | struct mutex mutex; |
Jeremy Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 197 | struct xenbus_device *xbdev; |
Jeremy Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 198 | struct gendisk *gd; |
Bob Liu | 172335a | 2016-07-01 17:43:39 -0400 | [diff] [blame] | 199 | u16 sector_size; |
| 200 | unsigned int physical_sector_size; |
Christoph Hellwig | 1a827ce1 | 2021-11-22 14:06:14 +0100 | [diff] [blame] | 201 | unsigned long vdisk_info; |
Jeremy Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 202 | int vdevice; |
| 203 | blkif_vdev_t handle; |
| 204 | enum blkif_state connected; |
Bob Liu | 3df0e50 | 2015-11-14 11:12:12 +0800 | [diff] [blame] | 205 | /* Number of pages per ring buffer. */ |
Bob Liu | 86839c5 | 2015-06-03 13:40:03 +0800 | [diff] [blame] | 206 | unsigned int nr_ring_pages; |
Jeremy Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 207 | struct request_queue *rq; |
Jan Beulich | b32728f | 2017-01-23 08:12:19 -0700 | [diff] [blame] | 208 | unsigned int feature_flush:1; |
| 209 | unsigned int feature_fua:1; |
Konrad Rzeszutek Wilk | 5ea4298 | 2011-10-12 16:23:30 -0400 | [diff] [blame] | 210 | unsigned int feature_discard:1; |
| 211 | unsigned int feature_secdiscard:1; |
Jan Beulich | b32728f | 2017-01-23 08:12:19 -0700 | [diff] [blame] | 212 | unsigned int feature_persistent:1; |
Li Dongyang | ed30bf3 | 2011-09-01 18:39:09 +0800 | [diff] [blame] | 213 | unsigned int discard_granularity; |
| 214 | unsigned int discard_alignment; |
Julien Grall | c004a6f | 2015-07-22 16:44:54 +0100 | [diff] [blame] | 215 | /* Number of 4KB segments handled */ |
Roger Pau Monne | 402b27f | 2013-04-18 16:06:54 +0200 | [diff] [blame] | 216 | unsigned int max_indirect_segments; |
Christian Limpach | 1d78d70 | 2008-04-02 10:54:04 -0700 | [diff] [blame] | 217 | int is_ready; |
Bob Liu | 907c3eb | 2015-07-13 17:55:24 +0800 | [diff] [blame] | 218 | struct blk_mq_tag_set tag_set; |
Bob Liu | 3df0e50 | 2015-11-14 11:12:12 +0800 | [diff] [blame] | 219 | struct blkfront_ring_info *rinfo; |
| 220 | unsigned int nr_rings; |
Juergen Gross | 4ab50af | 2020-03-05 16:51:29 +0100 | [diff] [blame] | 221 | unsigned int rinfo_size; |
Bob Liu | 7b427a5 | 2016-06-27 16:33:24 +0800 | [diff] [blame] | 222 | /* Save uncomplete reqs and bios for migration. */ |
| 223 | struct list_head requests; |
| 224 | struct bio_list bio_list; |
Juergen Gross | a46b536 | 2018-08-13 16:01:11 +0200 | [diff] [blame] | 225 | struct list_head info_list; |
Jeremy Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 226 | }; |
| 227 | |
Jan Beulich | 0e34582 | 2010-08-07 18:28:55 +0200 | [diff] [blame] | 228 | static unsigned int nr_minors; |
| 229 | static unsigned long *minors; |
| 230 | static DEFINE_SPINLOCK(minor_lock); |
| 231 | |
Jeremy Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 232 | #define GRANT_INVALID_REF 0 |
| 233 | |
| 234 | #define PARTS_PER_DISK 16 |
Chris Lalancette | 9246b5f | 2008-09-17 14:30:32 -0700 | [diff] [blame] | 235 | #define PARTS_PER_EXT_DISK 256 |
Jeremy Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 236 | |
| 237 | #define BLKIF_MAJOR(dev) ((dev)>>8) |
| 238 | #define BLKIF_MINOR(dev) ((dev) & 0xff) |
| 239 | |
Chris Lalancette | 9246b5f | 2008-09-17 14:30:32 -0700 | [diff] [blame] | 240 | #define EXT_SHIFT 28 |
| 241 | #define EXTENDED (1<<EXT_SHIFT) |
| 242 | #define VDEV_IS_EXTENDED(dev) ((dev)&(EXTENDED)) |
| 243 | #define BLKIF_MINOR_EXT(dev) ((dev)&(~EXTENDED)) |
Stefano Stabellini | c80a420 | 2010-12-02 17:55:00 +0000 | [diff] [blame] | 244 | #define EMULATED_HD_DISK_MINOR_OFFSET (0) |
| 245 | #define EMULATED_HD_DISK_NAME_OFFSET (EMULATED_HD_DISK_MINOR_OFFSET / 256) |
Stefan Bader | 196cfe2 | 2011-07-14 15:30:22 +0200 | [diff] [blame] | 246 | #define EMULATED_SD_DISK_MINOR_OFFSET (0) |
| 247 | #define EMULATED_SD_DISK_NAME_OFFSET (EMULATED_SD_DISK_MINOR_OFFSET / 256) |
Jeremy Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 248 | |
Chris Lalancette | 9246b5f | 2008-09-17 14:30:32 -0700 | [diff] [blame] | 249 | #define DEV_NAME "xvd" /* name in /dev */ |
Jeremy Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 250 | |
Julien Grall | c004a6f | 2015-07-22 16:44:54 +0100 | [diff] [blame] | 251 | /* |
| 252 | * Grants are always the same size as a Xen page (i.e 4KB). |
| 253 | * A physical segment is always the same size as a Linux page. |
| 254 | * Number of grants per physical segment |
| 255 | */ |
| 256 | #define GRANTS_PER_PSEG (PAGE_SIZE / XEN_PAGE_SIZE) |
| 257 | |
| 258 | #define GRANTS_PER_INDIRECT_FRAME \ |
| 259 | (XEN_PAGE_SIZE / sizeof(struct blkif_request_segment)) |
| 260 | |
Julien Grall | c004a6f | 2015-07-22 16:44:54 +0100 | [diff] [blame] | 261 | #define INDIRECT_GREFS(_grants) \ |
| 262 | DIV_ROUND_UP(_grants, GRANTS_PER_INDIRECT_FRAME) |
| 263 | |
Bob Liu | 81f3516 | 2015-11-14 11:12:11 +0800 | [diff] [blame] | 264 | static int blkfront_setup_indirect(struct blkfront_ring_info *rinfo); |
Bob Liu | 3df0e50 | 2015-11-14 11:12:12 +0800 | [diff] [blame] | 265 | static void blkfront_gather_backend_features(struct blkfront_info *info); |
Bhavesh Davda | 7ed8ce1 | 2017-12-22 14:17:13 -0800 | [diff] [blame] | 266 | static int negotiate_mq(struct blkfront_info *info); |
Roger Pau Monne | 402b27f | 2013-04-18 16:06:54 +0200 | [diff] [blame] | 267 | |
Juergen Gross | 4ab50af | 2020-03-05 16:51:29 +0100 | [diff] [blame] | 268 | #define for_each_rinfo(info, ptr, idx) \ |
| 269 | for ((ptr) = (info)->rinfo, (idx) = 0; \ |
| 270 | (idx) < (info)->nr_rings; \ |
| 271 | (idx)++, (ptr) = (void *)(ptr) + (info)->rinfo_size) |
| 272 | |
| 273 | static inline struct blkfront_ring_info * |
| 274 | get_rinfo(const struct blkfront_info *info, unsigned int i) |
| 275 | { |
| 276 | BUG_ON(i >= info->nr_rings); |
| 277 | return (void *)info->rinfo + i * info->rinfo_size; |
| 278 | } |
| 279 | |
Bob Liu | 81f3516 | 2015-11-14 11:12:11 +0800 | [diff] [blame] | 280 | static int get_id_from_freelist(struct blkfront_ring_info *rinfo) |
Jeremy Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 281 | { |
Bob Liu | 81f3516 | 2015-11-14 11:12:11 +0800 | [diff] [blame] | 282 | unsigned long free = rinfo->shadow_free; |
| 283 | |
| 284 | BUG_ON(free >= BLK_RING_SIZE(rinfo->dev_info)); |
| 285 | rinfo->shadow_free = rinfo->shadow[free].req.u.rw.id; |
| 286 | rinfo->shadow[free].req.u.rw.id = 0x0fffffee; /* debug */ |
Jeremy Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 287 | return free; |
| 288 | } |
| 289 | |
Bob Liu | 81f3516 | 2015-11-14 11:12:11 +0800 | [diff] [blame] | 290 | static int add_id_to_freelist(struct blkfront_ring_info *rinfo, |
Konrad Rzeszutek Wilk | 6f03a7f | 2015-11-16 15:14:41 -0500 | [diff] [blame] | 291 | unsigned long id) |
Jeremy Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 292 | { |
Bob Liu | 81f3516 | 2015-11-14 11:12:11 +0800 | [diff] [blame] | 293 | if (rinfo->shadow[id].req.u.rw.id != id) |
Konrad Rzeszutek Wilk | 6878c32 | 2012-05-25 17:34:51 -0400 | [diff] [blame] | 294 | return -EINVAL; |
Bob Liu | 81f3516 | 2015-11-14 11:12:11 +0800 | [diff] [blame] | 295 | if (rinfo->shadow[id].request == NULL) |
Konrad Rzeszutek Wilk | 6878c32 | 2012-05-25 17:34:51 -0400 | [diff] [blame] | 296 | return -EINVAL; |
Bob Liu | 81f3516 | 2015-11-14 11:12:11 +0800 | [diff] [blame] | 297 | rinfo->shadow[id].req.u.rw.id = rinfo->shadow_free; |
| 298 | rinfo->shadow[id].request = NULL; |
| 299 | rinfo->shadow_free = id; |
Konrad Rzeszutek Wilk | 6878c32 | 2012-05-25 17:34:51 -0400 | [diff] [blame] | 300 | return 0; |
Jeremy Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 301 | } |
| 302 | |
Bob Liu | 81f3516 | 2015-11-14 11:12:11 +0800 | [diff] [blame] | 303 | static int fill_grant_buffer(struct blkfront_ring_info *rinfo, int num) |
Roger Pau Monne | 9c1e050 | 2013-03-18 17:49:35 +0100 | [diff] [blame] | 304 | { |
Bob Liu | 81f3516 | 2015-11-14 11:12:11 +0800 | [diff] [blame] | 305 | struct blkfront_info *info = rinfo->dev_info; |
Roger Pau Monne | 9c1e050 | 2013-03-18 17:49:35 +0100 | [diff] [blame] | 306 | struct page *granted_page; |
| 307 | struct grant *gnt_list_entry, *n; |
| 308 | int i = 0; |
| 309 | |
Konrad Rzeszutek Wilk | 6f03a7f | 2015-11-16 15:14:41 -0500 | [diff] [blame] | 310 | while (i < num) { |
Roger Pau Monne | 9c1e050 | 2013-03-18 17:49:35 +0100 | [diff] [blame] | 311 | gnt_list_entry = kzalloc(sizeof(struct grant), GFP_NOIO); |
| 312 | if (!gnt_list_entry) |
| 313 | goto out_of_memory; |
| 314 | |
Roger Pau Monne | bfe11d6 | 2013-10-29 18:31:14 +0100 | [diff] [blame] | 315 | if (info->feature_persistent) { |
| 316 | granted_page = alloc_page(GFP_NOIO); |
| 317 | if (!granted_page) { |
| 318 | kfree(gnt_list_entry); |
| 319 | goto out_of_memory; |
| 320 | } |
Julien Grall | a7a6df2 | 2015-06-30 11:58:51 +0100 | [diff] [blame] | 321 | gnt_list_entry->page = granted_page; |
Roger Pau Monne | 9c1e050 | 2013-03-18 17:49:35 +0100 | [diff] [blame] | 322 | } |
| 323 | |
Roger Pau Monne | 9c1e050 | 2013-03-18 17:49:35 +0100 | [diff] [blame] | 324 | gnt_list_entry->gref = GRANT_INVALID_REF; |
Bob Liu | 73716df | 2015-11-16 16:51:39 -0500 | [diff] [blame] | 325 | list_add(&gnt_list_entry->node, &rinfo->grants); |
Roger Pau Monne | 9c1e050 | 2013-03-18 17:49:35 +0100 | [diff] [blame] | 326 | i++; |
| 327 | } |
| 328 | |
| 329 | return 0; |
| 330 | |
| 331 | out_of_memory: |
| 332 | list_for_each_entry_safe(gnt_list_entry, n, |
Bob Liu | 73716df | 2015-11-16 16:51:39 -0500 | [diff] [blame] | 333 | &rinfo->grants, node) { |
Roger Pau Monne | 9c1e050 | 2013-03-18 17:49:35 +0100 | [diff] [blame] | 334 | list_del(&gnt_list_entry->node); |
Roger Pau Monne | bfe11d6 | 2013-10-29 18:31:14 +0100 | [diff] [blame] | 335 | if (info->feature_persistent) |
Julien Grall | a7a6df2 | 2015-06-30 11:58:51 +0100 | [diff] [blame] | 336 | __free_page(gnt_list_entry->page); |
Roger Pau Monne | 9c1e050 | 2013-03-18 17:49:35 +0100 | [diff] [blame] | 337 | kfree(gnt_list_entry); |
| 338 | i--; |
| 339 | } |
| 340 | BUG_ON(i != 0); |
| 341 | return -ENOMEM; |
| 342 | } |
| 343 | |
Bob Liu | 73716df | 2015-11-16 16:51:39 -0500 | [diff] [blame] | 344 | static struct grant *get_free_grant(struct blkfront_ring_info *rinfo) |
Roger Pau Monne | 9c1e050 | 2013-03-18 17:49:35 +0100 | [diff] [blame] | 345 | { |
| 346 | struct grant *gnt_list_entry; |
Roger Pau Monne | 9c1e050 | 2013-03-18 17:49:35 +0100 | [diff] [blame] | 347 | |
Bob Liu | 73716df | 2015-11-16 16:51:39 -0500 | [diff] [blame] | 348 | BUG_ON(list_empty(&rinfo->grants)); |
| 349 | gnt_list_entry = list_first_entry(&rinfo->grants, struct grant, |
Julien Grall | 4f503fb | 2015-07-01 14:10:38 +0100 | [diff] [blame] | 350 | node); |
Roger Pau Monne | 9c1e050 | 2013-03-18 17:49:35 +0100 | [diff] [blame] | 351 | list_del(&gnt_list_entry->node); |
| 352 | |
Julien Grall | 4f503fb | 2015-07-01 14:10:38 +0100 | [diff] [blame] | 353 | if (gnt_list_entry->gref != GRANT_INVALID_REF) |
Bob Liu | 73716df | 2015-11-16 16:51:39 -0500 | [diff] [blame] | 354 | rinfo->persistent_gnts_c--; |
Julien Grall | 4f503fb | 2015-07-01 14:10:38 +0100 | [diff] [blame] | 355 | |
| 356 | return gnt_list_entry; |
| 357 | } |
| 358 | |
| 359 | static inline void grant_foreign_access(const struct grant *gnt_list_entry, |
| 360 | const struct blkfront_info *info) |
| 361 | { |
| 362 | gnttab_page_grant_foreign_access_ref_one(gnt_list_entry->gref, |
| 363 | info->xbdev->otherend_id, |
| 364 | gnt_list_entry->page, |
| 365 | 0); |
| 366 | } |
| 367 | |
| 368 | static struct grant *get_grant(grant_ref_t *gref_head, |
| 369 | unsigned long gfn, |
Bob Liu | 73716df | 2015-11-16 16:51:39 -0500 | [diff] [blame] | 370 | struct blkfront_ring_info *rinfo) |
Julien Grall | 4f503fb | 2015-07-01 14:10:38 +0100 | [diff] [blame] | 371 | { |
Bob Liu | 73716df | 2015-11-16 16:51:39 -0500 | [diff] [blame] | 372 | struct grant *gnt_list_entry = get_free_grant(rinfo); |
| 373 | struct blkfront_info *info = rinfo->dev_info; |
Julien Grall | 4f503fb | 2015-07-01 14:10:38 +0100 | [diff] [blame] | 374 | |
| 375 | if (gnt_list_entry->gref != GRANT_INVALID_REF) |
Roger Pau Monne | 9c1e050 | 2013-03-18 17:49:35 +0100 | [diff] [blame] | 376 | return gnt_list_entry; |
Julien Grall | 4f503fb | 2015-07-01 14:10:38 +0100 | [diff] [blame] | 377 | |
| 378 | /* Assign a gref to this page */ |
| 379 | gnt_list_entry->gref = gnttab_claim_grant_reference(gref_head); |
| 380 | BUG_ON(gnt_list_entry->gref == -ENOSPC); |
| 381 | if (info->feature_persistent) |
| 382 | grant_foreign_access(gnt_list_entry, info); |
| 383 | else { |
| 384 | /* Grant access to the GFN passed by the caller */ |
| 385 | gnttab_grant_foreign_access_ref(gnt_list_entry->gref, |
| 386 | info->xbdev->otherend_id, |
| 387 | gfn, 0); |
Roger Pau Monne | 9c1e050 | 2013-03-18 17:49:35 +0100 | [diff] [blame] | 388 | } |
| 389 | |
Julien Grall | 4f503fb | 2015-07-01 14:10:38 +0100 | [diff] [blame] | 390 | return gnt_list_entry; |
| 391 | } |
| 392 | |
| 393 | static struct grant *get_indirect_grant(grant_ref_t *gref_head, |
Bob Liu | 73716df | 2015-11-16 16:51:39 -0500 | [diff] [blame] | 394 | struct blkfront_ring_info *rinfo) |
Julien Grall | 4f503fb | 2015-07-01 14:10:38 +0100 | [diff] [blame] | 395 | { |
Bob Liu | 73716df | 2015-11-16 16:51:39 -0500 | [diff] [blame] | 396 | struct grant *gnt_list_entry = get_free_grant(rinfo); |
| 397 | struct blkfront_info *info = rinfo->dev_info; |
Julien Grall | 4f503fb | 2015-07-01 14:10:38 +0100 | [diff] [blame] | 398 | |
| 399 | if (gnt_list_entry->gref != GRANT_INVALID_REF) |
| 400 | return gnt_list_entry; |
| 401 | |
Roger Pau Monne | 9c1e050 | 2013-03-18 17:49:35 +0100 | [diff] [blame] | 402 | /* Assign a gref to this page */ |
| 403 | gnt_list_entry->gref = gnttab_claim_grant_reference(gref_head); |
| 404 | BUG_ON(gnt_list_entry->gref == -ENOSPC); |
Roger Pau Monne | bfe11d6 | 2013-10-29 18:31:14 +0100 | [diff] [blame] | 405 | if (!info->feature_persistent) { |
Julien Grall | 4f503fb | 2015-07-01 14:10:38 +0100 | [diff] [blame] | 406 | struct page *indirect_page; |
| 407 | |
| 408 | /* Fetch a pre-allocated page to use for indirect grefs */ |
Bob Liu | 73716df | 2015-11-16 16:51:39 -0500 | [diff] [blame] | 409 | BUG_ON(list_empty(&rinfo->indirect_pages)); |
| 410 | indirect_page = list_first_entry(&rinfo->indirect_pages, |
Julien Grall | 4f503fb | 2015-07-01 14:10:38 +0100 | [diff] [blame] | 411 | struct page, lru); |
| 412 | list_del(&indirect_page->lru); |
| 413 | gnt_list_entry->page = indirect_page; |
Roger Pau Monne | bfe11d6 | 2013-10-29 18:31:14 +0100 | [diff] [blame] | 414 | } |
Julien Grall | 4f503fb | 2015-07-01 14:10:38 +0100 | [diff] [blame] | 415 | grant_foreign_access(gnt_list_entry, info); |
| 416 | |
Roger Pau Monne | 9c1e050 | 2013-03-18 17:49:35 +0100 | [diff] [blame] | 417 | return gnt_list_entry; |
| 418 | } |
| 419 | |
Konrad Rzeszutek Wilk | 6878c32 | 2012-05-25 17:34:51 -0400 | [diff] [blame] | 420 | static const char *op_name(int op) |
| 421 | { |
| 422 | static const char *const names[] = { |
| 423 | [BLKIF_OP_READ] = "read", |
| 424 | [BLKIF_OP_WRITE] = "write", |
| 425 | [BLKIF_OP_WRITE_BARRIER] = "barrier", |
| 426 | [BLKIF_OP_FLUSH_DISKCACHE] = "flush", |
| 427 | [BLKIF_OP_DISCARD] = "discard" }; |
| 428 | |
| 429 | if (op < 0 || op >= ARRAY_SIZE(names)) |
| 430 | return "unknown"; |
| 431 | |
| 432 | if (!names[op]) |
| 433 | return "reserved"; |
| 434 | |
| 435 | return names[op]; |
| 436 | } |
Jan Beulich | 0e34582 | 2010-08-07 18:28:55 +0200 | [diff] [blame] | 437 | static int xlbd_reserve_minors(unsigned int minor, unsigned int nr) |
| 438 | { |
| 439 | unsigned int end = minor + nr; |
| 440 | int rc; |
| 441 | |
| 442 | if (end > nr_minors) { |
| 443 | unsigned long *bitmap, *old; |
| 444 | |
Thomas Meyer | f094148 | 2011-11-29 22:08:00 +0100 | [diff] [blame] | 445 | bitmap = kcalloc(BITS_TO_LONGS(end), sizeof(*bitmap), |
Jan Beulich | 0e34582 | 2010-08-07 18:28:55 +0200 | [diff] [blame] | 446 | GFP_KERNEL); |
| 447 | if (bitmap == NULL) |
| 448 | return -ENOMEM; |
| 449 | |
| 450 | spin_lock(&minor_lock); |
| 451 | if (end > nr_minors) { |
| 452 | old = minors; |
| 453 | memcpy(bitmap, minors, |
| 454 | BITS_TO_LONGS(nr_minors) * sizeof(*bitmap)); |
| 455 | minors = bitmap; |
| 456 | nr_minors = BITS_TO_LONGS(end) * BITS_PER_LONG; |
| 457 | } else |
| 458 | old = bitmap; |
| 459 | spin_unlock(&minor_lock); |
| 460 | kfree(old); |
| 461 | } |
| 462 | |
| 463 | spin_lock(&minor_lock); |
| 464 | if (find_next_bit(minors, end, minor) >= end) { |
Akinobu Mita | 34ae2e4 | 2012-01-21 00:15:26 +0900 | [diff] [blame] | 465 | bitmap_set(minors, minor, nr); |
Jan Beulich | 0e34582 | 2010-08-07 18:28:55 +0200 | [diff] [blame] | 466 | rc = 0; |
| 467 | } else |
| 468 | rc = -EBUSY; |
| 469 | spin_unlock(&minor_lock); |
| 470 | |
| 471 | return rc; |
| 472 | } |
| 473 | |
| 474 | static void xlbd_release_minors(unsigned int minor, unsigned int nr) |
| 475 | { |
| 476 | unsigned int end = minor + nr; |
| 477 | |
| 478 | BUG_ON(end > nr_minors); |
| 479 | spin_lock(&minor_lock); |
Akinobu Mita | 34ae2e4 | 2012-01-21 00:15:26 +0900 | [diff] [blame] | 480 | bitmap_clear(minors, minor, nr); |
Jan Beulich | 0e34582 | 2010-08-07 18:28:55 +0200 | [diff] [blame] | 481 | spin_unlock(&minor_lock); |
| 482 | } |
| 483 | |
Jeremy Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 484 | static void blkif_restart_queue_callback(void *arg) |
| 485 | { |
Bob Liu | 81f3516 | 2015-11-14 11:12:11 +0800 | [diff] [blame] | 486 | struct blkfront_ring_info *rinfo = (struct blkfront_ring_info *)arg; |
| 487 | schedule_work(&rinfo->work); |
Jeremy Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 488 | } |
| 489 | |
Harvey Harrison | afe42d7 | 2008-04-29 00:59:47 -0700 | [diff] [blame] | 490 | static int blkif_getgeo(struct block_device *bd, struct hd_geometry *hg) |
Ian Campbell | 597592d | 2008-02-21 13:03:45 -0800 | [diff] [blame] | 491 | { |
| 492 | /* We don't have real geometry info, but let's at least return |
| 493 | values consistent with the size of the device */ |
| 494 | sector_t nsect = get_capacity(bd->bd_disk); |
| 495 | sector_t cylinders = nsect; |
| 496 | |
| 497 | hg->heads = 0xff; |
| 498 | hg->sectors = 0x3f; |
| 499 | sector_div(cylinders, hg->heads * hg->sectors); |
| 500 | hg->cylinders = cylinders; |
| 501 | if ((sector_t)(hg->cylinders + 1) * hg->heads * hg->sectors < nsect) |
| 502 | hg->cylinders = 0xffff; |
| 503 | return 0; |
| 504 | } |
| 505 | |
Al Viro | a63c848 | 2008-03-02 10:23:47 -0500 | [diff] [blame] | 506 | static int blkif_ioctl(struct block_device *bdev, fmode_t mode, |
Adrian Bunk | 62aa005 | 2008-08-04 11:59:05 +0200 | [diff] [blame] | 507 | unsigned command, unsigned long argument) |
Christian Limpach | 440a01a | 2008-06-17 10:47:08 +0200 | [diff] [blame] | 508 | { |
Christoph Hellwig | 1a827ce1 | 2021-11-22 14:06:14 +0100 | [diff] [blame] | 509 | struct blkfront_info *info = bdev->bd_disk->private_data; |
Christian Limpach | 440a01a | 2008-06-17 10:47:08 +0200 | [diff] [blame] | 510 | int i; |
| 511 | |
Christian Limpach | 440a01a | 2008-06-17 10:47:08 +0200 | [diff] [blame] | 512 | switch (command) { |
| 513 | case CDROMMULTISESSION: |
Christian Limpach | 440a01a | 2008-06-17 10:47:08 +0200 | [diff] [blame] | 514 | for (i = 0; i < sizeof(struct cdrom_multisession); i++) |
| 515 | if (put_user(0, (char __user *)(argument + i))) |
| 516 | return -EFAULT; |
| 517 | return 0; |
Christoph Hellwig | 05d69d9 | 2021-07-15 16:17:11 +0200 | [diff] [blame] | 518 | case CDROM_GET_CAPABILITY: |
Christoph Hellwig | 1a827ce1 | 2021-11-22 14:06:14 +0100 | [diff] [blame] | 519 | if (!(info->vdisk_info & VDISK_CDROM)) |
| 520 | return -EINVAL; |
| 521 | return 0; |
Christian Limpach | 440a01a | 2008-06-17 10:47:08 +0200 | [diff] [blame] | 522 | default: |
Christoph Hellwig | 05d69d9 | 2021-07-15 16:17:11 +0200 | [diff] [blame] | 523 | return -EINVAL; |
Christian Limpach | 440a01a | 2008-06-17 10:47:08 +0200 | [diff] [blame] | 524 | } |
Christian Limpach | 440a01a | 2008-06-17 10:47:08 +0200 | [diff] [blame] | 525 | } |
| 526 | |
Julien Grall | 2e07396 | 2015-08-13 19:23:10 +0100 | [diff] [blame] | 527 | static unsigned long blkif_ring_get_request(struct blkfront_ring_info *rinfo, |
| 528 | struct request *req, |
| 529 | struct blkif_request **ring_req) |
| 530 | { |
| 531 | unsigned long id; |
| 532 | |
| 533 | *ring_req = RING_GET_REQUEST(&rinfo->ring, rinfo->ring.req_prod_pvt); |
| 534 | rinfo->ring.req_prod_pvt++; |
| 535 | |
| 536 | id = get_id_from_freelist(rinfo); |
| 537 | rinfo->shadow[id].request = req; |
Juergen Gross | b94e4b1 | 2021-07-30 12:38:54 +0200 | [diff] [blame] | 538 | rinfo->shadow[id].status = REQ_PROCESSING; |
Julien Grall | 6cc56833 | 2015-08-13 13:13:35 +0100 | [diff] [blame] | 539 | rinfo->shadow[id].associated_id = NO_ASSOCIATED_ID; |
Julien Grall | 2e07396 | 2015-08-13 19:23:10 +0100 | [diff] [blame] | 540 | |
Juergen Gross | 8f5a695 | 2021-07-30 12:38:53 +0200 | [diff] [blame] | 541 | rinfo->shadow[id].req.u.rw.id = id; |
Julien Grall | 2e07396 | 2015-08-13 19:23:10 +0100 | [diff] [blame] | 542 | |
| 543 | return id; |
| 544 | } |
| 545 | |
Bob Liu | 81f3516 | 2015-11-14 11:12:11 +0800 | [diff] [blame] | 546 | static int blkif_queue_discard_req(struct request *req, struct blkfront_ring_info *rinfo) |
Jeremy Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 547 | { |
Bob Liu | 81f3516 | 2015-11-14 11:12:11 +0800 | [diff] [blame] | 548 | struct blkfront_info *info = rinfo->dev_info; |
Juergen Gross | 8f5a695 | 2021-07-30 12:38:53 +0200 | [diff] [blame] | 549 | struct blkif_request *ring_req, *final_ring_req; |
Jeremy Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 550 | unsigned long id; |
Julien Grall | 3320466 | 2015-06-29 17:35:24 +0100 | [diff] [blame] | 551 | |
| 552 | /* Fill out a communications ring structure. */ |
Juergen Gross | 8f5a695 | 2021-07-30 12:38:53 +0200 | [diff] [blame] | 553 | id = blkif_ring_get_request(rinfo, req, &final_ring_req); |
| 554 | ring_req = &rinfo->shadow[id].req; |
Julien Grall | 3320466 | 2015-06-29 17:35:24 +0100 | [diff] [blame] | 555 | |
| 556 | ring_req->operation = BLKIF_OP_DISCARD; |
| 557 | ring_req->u.discard.nr_sectors = blk_rq_sectors(req); |
| 558 | ring_req->u.discard.id = id; |
| 559 | ring_req->u.discard.sector_number = (blkif_sector_t)blk_rq_pos(req); |
Christoph Hellwig | 288dab8 | 2016-06-09 16:00:36 +0200 | [diff] [blame] | 560 | if (req_op(req) == REQ_OP_SECURE_ERASE && info->feature_secdiscard) |
Julien Grall | 3320466 | 2015-06-29 17:35:24 +0100 | [diff] [blame] | 561 | ring_req->u.discard.flag = BLKIF_DISCARD_SECURE; |
| 562 | else |
| 563 | ring_req->u.discard.flag = 0; |
| 564 | |
Juergen Gross | 8f5a695 | 2021-07-30 12:38:53 +0200 | [diff] [blame] | 565 | /* Copy the request to the ring page. */ |
| 566 | *final_ring_req = *ring_req; |
Juergen Gross | b94e4b1 | 2021-07-30 12:38:54 +0200 | [diff] [blame] | 567 | rinfo->shadow[id].status = REQ_WAITING; |
Julien Grall | 3320466 | 2015-06-29 17:35:24 +0100 | [diff] [blame] | 568 | |
| 569 | return 0; |
| 570 | } |
| 571 | |
Julien Grall | c004a6f | 2015-07-22 16:44:54 +0100 | [diff] [blame] | 572 | struct setup_rw_req { |
| 573 | unsigned int grant_idx; |
| 574 | struct blkif_request_segment *segments; |
Bob Liu | 81f3516 | 2015-11-14 11:12:11 +0800 | [diff] [blame] | 575 | struct blkfront_ring_info *rinfo; |
Julien Grall | c004a6f | 2015-07-22 16:44:54 +0100 | [diff] [blame] | 576 | struct blkif_request *ring_req; |
| 577 | grant_ref_t gref_head; |
| 578 | unsigned int id; |
| 579 | /* Only used when persistent grant is used and it's a read request */ |
| 580 | bool need_copy; |
| 581 | unsigned int bvec_off; |
| 582 | char *bvec_data; |
Julien Grall | 6cc56833 | 2015-08-13 13:13:35 +0100 | [diff] [blame] | 583 | |
| 584 | bool require_extra_req; |
| 585 | struct blkif_request *extra_ring_req; |
Julien Grall | c004a6f | 2015-07-22 16:44:54 +0100 | [diff] [blame] | 586 | }; |
| 587 | |
| 588 | static void blkif_setup_rw_req_grant(unsigned long gfn, unsigned int offset, |
| 589 | unsigned int len, void *data) |
| 590 | { |
| 591 | struct setup_rw_req *setup = data; |
| 592 | int n, ref; |
| 593 | struct grant *gnt_list_entry; |
Jeremy Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 594 | unsigned int fsect, lsect; |
Julien Grall | c004a6f | 2015-07-22 16:44:54 +0100 | [diff] [blame] | 595 | /* Convenient aliases */ |
| 596 | unsigned int grant_idx = setup->grant_idx; |
| 597 | struct blkif_request *ring_req = setup->ring_req; |
Bob Liu | 81f3516 | 2015-11-14 11:12:11 +0800 | [diff] [blame] | 598 | struct blkfront_ring_info *rinfo = setup->rinfo; |
Julien Grall | 6cc56833 | 2015-08-13 13:13:35 +0100 | [diff] [blame] | 599 | /* |
| 600 | * We always use the shadow of the first request to store the list |
| 601 | * of grant associated to the block I/O request. This made the |
| 602 | * completion more easy to handle even if the block I/O request is |
| 603 | * split. |
| 604 | */ |
Bob Liu | 81f3516 | 2015-11-14 11:12:11 +0800 | [diff] [blame] | 605 | struct blk_shadow *shadow = &rinfo->shadow[setup->id]; |
Julien Grall | c004a6f | 2015-07-22 16:44:54 +0100 | [diff] [blame] | 606 | |
Julien Grall | 6cc56833 | 2015-08-13 13:13:35 +0100 | [diff] [blame] | 607 | if (unlikely(setup->require_extra_req && |
| 608 | grant_idx >= BLKIF_MAX_SEGMENTS_PER_REQUEST)) { |
| 609 | /* |
| 610 | * We are using the second request, setup grant_idx |
| 611 | * to be the index of the segment array. |
| 612 | */ |
| 613 | grant_idx -= BLKIF_MAX_SEGMENTS_PER_REQUEST; |
| 614 | ring_req = setup->extra_ring_req; |
| 615 | } |
| 616 | |
Julien Grall | c004a6f | 2015-07-22 16:44:54 +0100 | [diff] [blame] | 617 | if ((ring_req->operation == BLKIF_OP_INDIRECT) && |
| 618 | (grant_idx % GRANTS_PER_INDIRECT_FRAME == 0)) { |
| 619 | if (setup->segments) |
| 620 | kunmap_atomic(setup->segments); |
| 621 | |
| 622 | n = grant_idx / GRANTS_PER_INDIRECT_FRAME; |
Bob Liu | 73716df | 2015-11-16 16:51:39 -0500 | [diff] [blame] | 623 | gnt_list_entry = get_indirect_grant(&setup->gref_head, rinfo); |
Julien Grall | c004a6f | 2015-07-22 16:44:54 +0100 | [diff] [blame] | 624 | shadow->indirect_grants[n] = gnt_list_entry; |
| 625 | setup->segments = kmap_atomic(gnt_list_entry->page); |
| 626 | ring_req->u.indirect.indirect_grefs[n] = gnt_list_entry->gref; |
| 627 | } |
| 628 | |
Bob Liu | 73716df | 2015-11-16 16:51:39 -0500 | [diff] [blame] | 629 | gnt_list_entry = get_grant(&setup->gref_head, gfn, rinfo); |
Julien Grall | c004a6f | 2015-07-22 16:44:54 +0100 | [diff] [blame] | 630 | ref = gnt_list_entry->gref; |
Julien Grall | 6cc56833 | 2015-08-13 13:13:35 +0100 | [diff] [blame] | 631 | /* |
| 632 | * All the grants are stored in the shadow of the first |
| 633 | * request. Therefore we have to use the global index. |
| 634 | */ |
| 635 | shadow->grants_used[setup->grant_idx] = gnt_list_entry; |
Julien Grall | c004a6f | 2015-07-22 16:44:54 +0100 | [diff] [blame] | 636 | |
| 637 | if (setup->need_copy) { |
| 638 | void *shared_data; |
| 639 | |
| 640 | shared_data = kmap_atomic(gnt_list_entry->page); |
| 641 | /* |
| 642 | * this does not wipe data stored outside the |
| 643 | * range sg->offset..sg->offset+sg->length. |
| 644 | * Therefore, blkback *could* see data from |
| 645 | * previous requests. This is OK as long as |
| 646 | * persistent grants are shared with just one |
| 647 | * domain. It may need refactoring if this |
| 648 | * changes |
| 649 | */ |
| 650 | memcpy(shared_data + offset, |
| 651 | setup->bvec_data + setup->bvec_off, |
| 652 | len); |
| 653 | |
| 654 | kunmap_atomic(shared_data); |
| 655 | setup->bvec_off += len; |
| 656 | } |
| 657 | |
| 658 | fsect = offset >> 9; |
| 659 | lsect = fsect + (len >> 9) - 1; |
| 660 | if (ring_req->operation != BLKIF_OP_INDIRECT) { |
| 661 | ring_req->u.rw.seg[grant_idx] = |
| 662 | (struct blkif_request_segment) { |
| 663 | .gref = ref, |
| 664 | .first_sect = fsect, |
| 665 | .last_sect = lsect }; |
| 666 | } else { |
| 667 | setup->segments[grant_idx % GRANTS_PER_INDIRECT_FRAME] = |
| 668 | (struct blkif_request_segment) { |
| 669 | .gref = ref, |
| 670 | .first_sect = fsect, |
| 671 | .last_sect = lsect }; |
| 672 | } |
| 673 | |
| 674 | (setup->grant_idx)++; |
| 675 | } |
| 676 | |
Julien Grall | 6cc56833 | 2015-08-13 13:13:35 +0100 | [diff] [blame] | 677 | static void blkif_setup_extra_req(struct blkif_request *first, |
| 678 | struct blkif_request *second) |
| 679 | { |
| 680 | uint16_t nr_segments = first->u.rw.nr_segments; |
| 681 | |
| 682 | /* |
| 683 | * The second request is only present when the first request uses |
| 684 | * all its segments. It's always the continuity of the first one. |
| 685 | */ |
| 686 | first->u.rw.nr_segments = BLKIF_MAX_SEGMENTS_PER_REQUEST; |
| 687 | |
| 688 | second->u.rw.nr_segments = nr_segments - BLKIF_MAX_SEGMENTS_PER_REQUEST; |
| 689 | second->u.rw.sector_number = first->u.rw.sector_number + |
| 690 | (BLKIF_MAX_SEGMENTS_PER_REQUEST * XEN_PAGE_SIZE) / 512; |
| 691 | |
| 692 | second->u.rw.handle = first->u.rw.handle; |
| 693 | second->operation = first->operation; |
| 694 | } |
| 695 | |
Bob Liu | 81f3516 | 2015-11-14 11:12:11 +0800 | [diff] [blame] | 696 | static int blkif_queue_rw_req(struct request *req, struct blkfront_ring_info *rinfo) |
Jeremy Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 697 | { |
Bob Liu | 81f3516 | 2015-11-14 11:12:11 +0800 | [diff] [blame] | 698 | struct blkfront_info *info = rinfo->dev_info; |
Julien Grall | 6cc56833 | 2015-08-13 13:13:35 +0100 | [diff] [blame] | 699 | struct blkif_request *ring_req, *extra_ring_req = NULL; |
Juergen Gross | 8f5a695 | 2021-07-30 12:38:53 +0200 | [diff] [blame] | 700 | struct blkif_request *final_ring_req, *final_extra_ring_req = NULL; |
Julien Grall | 6cc56833 | 2015-08-13 13:13:35 +0100 | [diff] [blame] | 701 | unsigned long id, extra_id = NO_ASSOCIATED_ID; |
| 702 | bool require_extra_req = false; |
Julien Grall | c004a6f | 2015-07-22 16:44:54 +0100 | [diff] [blame] | 703 | int i; |
| 704 | struct setup_rw_req setup = { |
| 705 | .grant_idx = 0, |
| 706 | .segments = NULL, |
Bob Liu | 81f3516 | 2015-11-14 11:12:11 +0800 | [diff] [blame] | 707 | .rinfo = rinfo, |
Julien Grall | c004a6f | 2015-07-22 16:44:54 +0100 | [diff] [blame] | 708 | .need_copy = rq_data_dir(req) && info->feature_persistent, |
| 709 | }; |
Roger Pau Monne | 0a8704a | 2012-10-24 18:58:45 +0200 | [diff] [blame] | 710 | |
| 711 | /* |
| 712 | * Used to store if we are able to queue the request by just using |
| 713 | * existing persistent grants, or if we have to get new grants, |
| 714 | * as there are not sufficiently many free. |
| 715 | */ |
Dongli Zhang | bd912ef3 | 2017-06-28 20:57:28 +0800 | [diff] [blame] | 716 | bool new_persistent_gnts = false; |
Jens Axboe | 9e973e6 | 2009-02-24 08:10:09 +0100 | [diff] [blame] | 717 | struct scatterlist *sg; |
Julien Grall | c004a6f | 2015-07-22 16:44:54 +0100 | [diff] [blame] | 718 | int num_sg, max_grefs, num_grant; |
Jeremy Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 719 | |
Julien Grall | c004a6f | 2015-07-22 16:44:54 +0100 | [diff] [blame] | 720 | max_grefs = req->nr_phys_segments * GRANTS_PER_PSEG; |
Roger Pau Monne | c47206e | 2013-08-12 12:53:43 +0200 | [diff] [blame] | 721 | if (max_grefs > BLKIF_MAX_SEGMENTS_PER_REQUEST) |
| 722 | /* |
| 723 | * If we are using indirect segments we need to account |
| 724 | * for the indirect grefs used in the request. |
| 725 | */ |
Julien Grall | c004a6f | 2015-07-22 16:44:54 +0100 | [diff] [blame] | 726 | max_grefs += INDIRECT_GREFS(max_grefs); |
Roger Pau Monne | 402b27f | 2013-04-18 16:06:54 +0200 | [diff] [blame] | 727 | |
Dongli Zhang | bd912ef3 | 2017-06-28 20:57:28 +0800 | [diff] [blame] | 728 | /* Check if we have enough persistent grants to allocate a requests */ |
| 729 | if (rinfo->persistent_gnts_c < max_grefs) { |
| 730 | new_persistent_gnts = true; |
| 731 | |
| 732 | if (gnttab_alloc_grant_references( |
| 733 | max_grefs - rinfo->persistent_gnts_c, |
| 734 | &setup.gref_head) < 0) { |
Roger Pau Monne | 0a8704a | 2012-10-24 18:58:45 +0200 | [diff] [blame] | 735 | gnttab_request_free_callback( |
Bob Liu | 81f3516 | 2015-11-14 11:12:11 +0800 | [diff] [blame] | 736 | &rinfo->callback, |
Roger Pau Monne | 0a8704a | 2012-10-24 18:58:45 +0200 | [diff] [blame] | 737 | blkif_restart_queue_callback, |
Bob Liu | 81f3516 | 2015-11-14 11:12:11 +0800 | [diff] [blame] | 738 | rinfo, |
Dongli Zhang | bd912ef3 | 2017-06-28 20:57:28 +0800 | [diff] [blame] | 739 | max_grefs - rinfo->persistent_gnts_c); |
Roger Pau Monne | 0a8704a | 2012-10-24 18:58:45 +0200 | [diff] [blame] | 740 | return 1; |
| 741 | } |
Dongli Zhang | bd912ef3 | 2017-06-28 20:57:28 +0800 | [diff] [blame] | 742 | } |
Jeremy Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 743 | |
| 744 | /* Fill out a communications ring structure. */ |
Juergen Gross | 8f5a695 | 2021-07-30 12:38:53 +0200 | [diff] [blame] | 745 | id = blkif_ring_get_request(rinfo, req, &final_ring_req); |
| 746 | ring_req = &rinfo->shadow[id].req; |
Jeremy Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 747 | |
Bob Liu | 81f3516 | 2015-11-14 11:12:11 +0800 | [diff] [blame] | 748 | num_sg = blk_rq_map_sg(req->q, req, rinfo->shadow[id].sg); |
Julien Grall | c004a6f | 2015-07-22 16:44:54 +0100 | [diff] [blame] | 749 | num_grant = 0; |
| 750 | /* Calculate the number of grant used */ |
Bob Liu | 81f3516 | 2015-11-14 11:12:11 +0800 | [diff] [blame] | 751 | for_each_sg(rinfo->shadow[id].sg, sg, num_sg, i) |
Julien Grall | c004a6f | 2015-07-22 16:44:54 +0100 | [diff] [blame] | 752 | num_grant += gnttab_count_grant(sg->offset, sg->length); |
| 753 | |
Julien Grall | 6cc56833 | 2015-08-13 13:13:35 +0100 | [diff] [blame] | 754 | require_extra_req = info->max_indirect_segments == 0 && |
| 755 | num_grant > BLKIF_MAX_SEGMENTS_PER_REQUEST; |
| 756 | BUG_ON(!HAS_EXTRA_REQ && require_extra_req); |
| 757 | |
Bob Liu | 81f3516 | 2015-11-14 11:12:11 +0800 | [diff] [blame] | 758 | rinfo->shadow[id].num_sg = num_sg; |
Julien Grall | 6cc56833 | 2015-08-13 13:13:35 +0100 | [diff] [blame] | 759 | if (num_grant > BLKIF_MAX_SEGMENTS_PER_REQUEST && |
| 760 | likely(!require_extra_req)) { |
Julien Grall | 3320466 | 2015-06-29 17:35:24 +0100 | [diff] [blame] | 761 | /* |
| 762 | * The indirect operation can only be a BLKIF_OP_READ or |
| 763 | * BLKIF_OP_WRITE |
| 764 | */ |
Mike Christie | 3a5e02c | 2016-06-05 14:32:23 -0500 | [diff] [blame] | 765 | BUG_ON(req_op(req) == REQ_OP_FLUSH || req->cmd_flags & REQ_FUA); |
Julien Grall | 3320466 | 2015-06-29 17:35:24 +0100 | [diff] [blame] | 766 | ring_req->operation = BLKIF_OP_INDIRECT; |
| 767 | ring_req->u.indirect.indirect_op = rq_data_dir(req) ? |
| 768 | BLKIF_OP_WRITE : BLKIF_OP_READ; |
| 769 | ring_req->u.indirect.sector_number = (blkif_sector_t)blk_rq_pos(req); |
| 770 | ring_req->u.indirect.handle = info->handle; |
Julien Grall | c004a6f | 2015-07-22 16:44:54 +0100 | [diff] [blame] | 771 | ring_req->u.indirect.nr_segments = num_grant; |
Li Dongyang | ed30bf3 | 2011-09-01 18:39:09 +0800 | [diff] [blame] | 772 | } else { |
Julien Grall | 3320466 | 2015-06-29 17:35:24 +0100 | [diff] [blame] | 773 | ring_req->u.rw.sector_number = (blkif_sector_t)blk_rq_pos(req); |
| 774 | ring_req->u.rw.handle = info->handle; |
| 775 | ring_req->operation = rq_data_dir(req) ? |
| 776 | BLKIF_OP_WRITE : BLKIF_OP_READ; |
Mike Christie | 3a5e02c | 2016-06-05 14:32:23 -0500 | [diff] [blame] | 777 | if (req_op(req) == REQ_OP_FLUSH || req->cmd_flags & REQ_FUA) { |
Roger Pau Monne | 402b27f | 2013-04-18 16:06:54 +0200 | [diff] [blame] | 778 | /* |
Julien Grall | 3320466 | 2015-06-29 17:35:24 +0100 | [diff] [blame] | 779 | * Ideally we can do an unordered flush-to-disk. |
| 780 | * In case the backend onlysupports barriers, use that. |
| 781 | * A barrier request a superset of FUA, so we can |
| 782 | * implement it the same way. (It's also a FLUSH+FUA, |
| 783 | * since it is guaranteed ordered WRT previous writes.) |
Roger Pau Monne | 402b27f | 2013-04-18 16:06:54 +0200 | [diff] [blame] | 784 | */ |
Mike Christie | a418090 | 2016-06-05 14:32:24 -0500 | [diff] [blame] | 785 | if (info->feature_flush && info->feature_fua) |
Julien Grall | 3320466 | 2015-06-29 17:35:24 +0100 | [diff] [blame] | 786 | ring_req->operation = |
| 787 | BLKIF_OP_WRITE_BARRIER; |
Mike Christie | a418090 | 2016-06-05 14:32:24 -0500 | [diff] [blame] | 788 | else if (info->feature_flush) |
Julien Grall | 3320466 | 2015-06-29 17:35:24 +0100 | [diff] [blame] | 789 | ring_req->operation = |
| 790 | BLKIF_OP_FLUSH_DISKCACHE; |
Mike Christie | a418090 | 2016-06-05 14:32:24 -0500 | [diff] [blame] | 791 | else |
Julien Grall | 3320466 | 2015-06-29 17:35:24 +0100 | [diff] [blame] | 792 | ring_req->operation = 0; |
Li Dongyang | ed30bf3 | 2011-09-01 18:39:09 +0800 | [diff] [blame] | 793 | } |
Julien Grall | c004a6f | 2015-07-22 16:44:54 +0100 | [diff] [blame] | 794 | ring_req->u.rw.nr_segments = num_grant; |
Julien Grall | 6cc56833 | 2015-08-13 13:13:35 +0100 | [diff] [blame] | 795 | if (unlikely(require_extra_req)) { |
| 796 | extra_id = blkif_ring_get_request(rinfo, req, |
Juergen Gross | 8f5a695 | 2021-07-30 12:38:53 +0200 | [diff] [blame] | 797 | &final_extra_ring_req); |
| 798 | extra_ring_req = &rinfo->shadow[extra_id].req; |
| 799 | |
Julien Grall | 6cc56833 | 2015-08-13 13:13:35 +0100 | [diff] [blame] | 800 | /* |
| 801 | * Only the first request contains the scatter-gather |
| 802 | * list. |
| 803 | */ |
| 804 | rinfo->shadow[extra_id].num_sg = 0; |
| 805 | |
| 806 | blkif_setup_extra_req(ring_req, extra_ring_req); |
| 807 | |
| 808 | /* Link the 2 requests together */ |
| 809 | rinfo->shadow[extra_id].associated_id = id; |
| 810 | rinfo->shadow[id].associated_id = extra_id; |
| 811 | } |
Jeremy Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 812 | } |
| 813 | |
Julien Grall | c004a6f | 2015-07-22 16:44:54 +0100 | [diff] [blame] | 814 | setup.ring_req = ring_req; |
| 815 | setup.id = id; |
Julien Grall | 6cc56833 | 2015-08-13 13:13:35 +0100 | [diff] [blame] | 816 | |
| 817 | setup.require_extra_req = require_extra_req; |
| 818 | if (unlikely(require_extra_req)) |
| 819 | setup.extra_ring_req = extra_ring_req; |
| 820 | |
Bob Liu | 81f3516 | 2015-11-14 11:12:11 +0800 | [diff] [blame] | 821 | for_each_sg(rinfo->shadow[id].sg, sg, num_sg, i) { |
Julien Grall | c004a6f | 2015-07-22 16:44:54 +0100 | [diff] [blame] | 822 | BUG_ON(sg->offset + sg->length > PAGE_SIZE); |
Julien Grall | 3320466 | 2015-06-29 17:35:24 +0100 | [diff] [blame] | 823 | |
Julien Grall | c004a6f | 2015-07-22 16:44:54 +0100 | [diff] [blame] | 824 | if (setup.need_copy) { |
| 825 | setup.bvec_off = sg->offset; |
| 826 | setup.bvec_data = kmap_atomic(sg_page(sg)); |
Julien Grall | 3320466 | 2015-06-29 17:35:24 +0100 | [diff] [blame] | 827 | } |
| 828 | |
Julien Grall | c004a6f | 2015-07-22 16:44:54 +0100 | [diff] [blame] | 829 | gnttab_foreach_grant_in_range(sg_page(sg), |
| 830 | sg->offset, |
| 831 | sg->length, |
| 832 | blkif_setup_rw_req_grant, |
| 833 | &setup); |
Julien Grall | 3320466 | 2015-06-29 17:35:24 +0100 | [diff] [blame] | 834 | |
Julien Grall | c004a6f | 2015-07-22 16:44:54 +0100 | [diff] [blame] | 835 | if (setup.need_copy) |
| 836 | kunmap_atomic(setup.bvec_data); |
Julien Grall | 3320466 | 2015-06-29 17:35:24 +0100 | [diff] [blame] | 837 | } |
Julien Grall | c004a6f | 2015-07-22 16:44:54 +0100 | [diff] [blame] | 838 | if (setup.segments) |
| 839 | kunmap_atomic(setup.segments); |
Jeremy Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 840 | |
Juergen Gross | 8f5a695 | 2021-07-30 12:38:53 +0200 | [diff] [blame] | 841 | /* Copy request(s) to the ring page. */ |
| 842 | *final_ring_req = *ring_req; |
Juergen Gross | b94e4b1 | 2021-07-30 12:38:54 +0200 | [diff] [blame] | 843 | rinfo->shadow[id].status = REQ_WAITING; |
| 844 | if (unlikely(require_extra_req)) { |
Juergen Gross | 8f5a695 | 2021-07-30 12:38:53 +0200 | [diff] [blame] | 845 | *final_extra_ring_req = *extra_ring_req; |
Juergen Gross | b94e4b1 | 2021-07-30 12:38:54 +0200 | [diff] [blame] | 846 | rinfo->shadow[extra_id].status = REQ_WAITING; |
| 847 | } |
Jeremy Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 848 | |
Dongli Zhang | bd912ef3 | 2017-06-28 20:57:28 +0800 | [diff] [blame] | 849 | if (new_persistent_gnts) |
Julien Grall | c004a6f | 2015-07-22 16:44:54 +0100 | [diff] [blame] | 850 | gnttab_free_grant_references(setup.gref_head); |
Jeremy Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 851 | |
| 852 | return 0; |
| 853 | } |
| 854 | |
Julien Grall | 3320466 | 2015-06-29 17:35:24 +0100 | [diff] [blame] | 855 | /* |
| 856 | * Generate a Xen blkfront IO request from a blk layer request. Reads |
| 857 | * and writes are handled as expected. |
| 858 | * |
| 859 | * @req: a request struct |
| 860 | */ |
Bob Liu | 81f3516 | 2015-11-14 11:12:11 +0800 | [diff] [blame] | 861 | static int blkif_queue_request(struct request *req, struct blkfront_ring_info *rinfo) |
Julien Grall | 3320466 | 2015-06-29 17:35:24 +0100 | [diff] [blame] | 862 | { |
Bob Liu | 81f3516 | 2015-11-14 11:12:11 +0800 | [diff] [blame] | 863 | if (unlikely(rinfo->dev_info->connected != BLKIF_STATE_CONNECTED)) |
Julien Grall | 3320466 | 2015-06-29 17:35:24 +0100 | [diff] [blame] | 864 | return 1; |
| 865 | |
Mike Christie | c2df40d | 2016-06-05 14:32:17 -0500 | [diff] [blame] | 866 | if (unlikely(req_op(req) == REQ_OP_DISCARD || |
Christoph Hellwig | 288dab8 | 2016-06-09 16:00:36 +0200 | [diff] [blame] | 867 | req_op(req) == REQ_OP_SECURE_ERASE)) |
Bob Liu | 81f3516 | 2015-11-14 11:12:11 +0800 | [diff] [blame] | 868 | return blkif_queue_discard_req(req, rinfo); |
Julien Grall | 3320466 | 2015-06-29 17:35:24 +0100 | [diff] [blame] | 869 | else |
Bob Liu | 81f3516 | 2015-11-14 11:12:11 +0800 | [diff] [blame] | 870 | return blkif_queue_rw_req(req, rinfo); |
Julien Grall | 3320466 | 2015-06-29 17:35:24 +0100 | [diff] [blame] | 871 | } |
Jeremy Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 872 | |
Bob Liu | 81f3516 | 2015-11-14 11:12:11 +0800 | [diff] [blame] | 873 | static inline void flush_requests(struct blkfront_ring_info *rinfo) |
Jeremy Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 874 | { |
| 875 | int notify; |
| 876 | |
Bob Liu | 81f3516 | 2015-11-14 11:12:11 +0800 | [diff] [blame] | 877 | RING_PUSH_REQUESTS_AND_CHECK_NOTIFY(&rinfo->ring, notify); |
Jeremy Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 878 | |
| 879 | if (notify) |
Bob Liu | 81f3516 | 2015-11-14 11:12:11 +0800 | [diff] [blame] | 880 | notify_remote_via_irq(rinfo->irq); |
Jeremy Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 881 | } |
| 882 | |
Vitaly Kuznetsov | ad42d39 | 2014-12-01 14:01:13 +0100 | [diff] [blame] | 883 | static inline bool blkif_request_flush_invalid(struct request *req, |
| 884 | struct blkfront_info *info) |
Arianna Avanzini | 0f1ca65 | 2014-08-22 13:20:02 +0200 | [diff] [blame] | 885 | { |
Christoph Hellwig | aebf526 | 2017-01-31 16:57:31 +0100 | [diff] [blame] | 886 | return (blk_rq_is_passthrough(req) || |
Mike Christie | 3a5e02c | 2016-06-05 14:32:23 -0500 | [diff] [blame] | 887 | ((req_op(req) == REQ_OP_FLUSH) && |
Mike Christie | a418090 | 2016-06-05 14:32:24 -0500 | [diff] [blame] | 888 | !info->feature_flush) || |
Vitaly Kuznetsov | ad42d39 | 2014-12-01 14:01:13 +0100 | [diff] [blame] | 889 | ((req->cmd_flags & REQ_FUA) && |
Mike Christie | a418090 | 2016-06-05 14:32:24 -0500 | [diff] [blame] | 890 | !info->feature_fua)); |
Arianna Avanzini | 0f1ca65 | 2014-08-22 13:20:02 +0200 | [diff] [blame] | 891 | } |
| 892 | |
Christoph Hellwig | fc17b65 | 2017-06-03 09:38:05 +0200 | [diff] [blame] | 893 | static blk_status_t blkif_queue_rq(struct blk_mq_hw_ctx *hctx, |
Konrad Rzeszutek Wilk | 6f03a7f | 2015-11-16 15:14:41 -0500 | [diff] [blame] | 894 | const struct blk_mq_queue_data *qd) |
Jeremy Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 895 | { |
Bob Liu | 1165956 | 2015-11-14 11:12:13 +0800 | [diff] [blame] | 896 | unsigned long flags; |
Bob Liu | 2a6f71a | 2016-05-31 16:59:17 +0800 | [diff] [blame] | 897 | int qid = hctx->queue_num; |
| 898 | struct blkfront_info *info = hctx->queue->queuedata; |
| 899 | struct blkfront_ring_info *rinfo = NULL; |
Jeremy Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 900 | |
Juergen Gross | 4ab50af | 2020-03-05 16:51:29 +0100 | [diff] [blame] | 901 | rinfo = get_rinfo(info, qid); |
Bob Liu | 907c3eb | 2015-07-13 17:55:24 +0800 | [diff] [blame] | 902 | blk_mq_start_request(qd->rq); |
Bob Liu | 1165956 | 2015-11-14 11:12:13 +0800 | [diff] [blame] | 903 | spin_lock_irqsave(&rinfo->ring_lock, flags); |
Bob Liu | 81f3516 | 2015-11-14 11:12:11 +0800 | [diff] [blame] | 904 | if (RING_FULL(&rinfo->ring)) |
Bob Liu | 907c3eb | 2015-07-13 17:55:24 +0800 | [diff] [blame] | 905 | goto out_busy; |
Jeremy Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 906 | |
Bob Liu | 81f3516 | 2015-11-14 11:12:11 +0800 | [diff] [blame] | 907 | if (blkif_request_flush_invalid(qd->rq, rinfo->dev_info)) |
Bob Liu | 907c3eb | 2015-07-13 17:55:24 +0800 | [diff] [blame] | 908 | goto out_err; |
Jeremy Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 909 | |
Bob Liu | 81f3516 | 2015-11-14 11:12:11 +0800 | [diff] [blame] | 910 | if (blkif_queue_request(qd->rq, rinfo)) |
Bob Liu | 907c3eb | 2015-07-13 17:55:24 +0800 | [diff] [blame] | 911 | goto out_busy; |
Jeremy Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 912 | |
Bob Liu | 81f3516 | 2015-11-14 11:12:11 +0800 | [diff] [blame] | 913 | flush_requests(rinfo); |
Bob Liu | 1165956 | 2015-11-14 11:12:13 +0800 | [diff] [blame] | 914 | spin_unlock_irqrestore(&rinfo->ring_lock, flags); |
Christoph Hellwig | fc17b65 | 2017-06-03 09:38:05 +0200 | [diff] [blame] | 915 | return BLK_STS_OK; |
Jeremy Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 916 | |
Bob Liu | 907c3eb | 2015-07-13 17:55:24 +0800 | [diff] [blame] | 917 | out_err: |
Bob Liu | 1165956 | 2015-11-14 11:12:13 +0800 | [diff] [blame] | 918 | spin_unlock_irqrestore(&rinfo->ring_lock, flags); |
Christoph Hellwig | fc17b65 | 2017-06-03 09:38:05 +0200 | [diff] [blame] | 919 | return BLK_STS_IOERR; |
Tejun Heo | 296b2f6 | 2009-05-08 11:54:15 +0900 | [diff] [blame] | 920 | |
Bob Liu | 907c3eb | 2015-07-13 17:55:24 +0800 | [diff] [blame] | 921 | out_busy: |
Bob Liu | 907c3eb | 2015-07-13 17:55:24 +0800 | [diff] [blame] | 922 | blk_mq_stop_hw_queue(hctx); |
Junxiao Bi | 4b422cb | 2017-07-20 09:26:21 +0800 | [diff] [blame] | 923 | spin_unlock_irqrestore(&rinfo->ring_lock, flags); |
Ming Lei | 86ff7c2 | 2018-01-30 22:04:57 -0500 | [diff] [blame] | 924 | return BLK_STS_DEV_RESOURCE; |
Jeremy Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 925 | } |
| 926 | |
Christoph Hellwig | 2609587 | 2017-04-20 16:03:08 +0200 | [diff] [blame] | 927 | static void blkif_complete_rq(struct request *rq) |
| 928 | { |
| 929 | blk_mq_end_request(rq, blkif_req(rq)->error); |
| 930 | } |
| 931 | |
Eric Biggers | f363b08 | 2017-03-30 13:39:16 -0700 | [diff] [blame] | 932 | static const struct blk_mq_ops blkfront_mq_ops = { |
Bob Liu | 907c3eb | 2015-07-13 17:55:24 +0800 | [diff] [blame] | 933 | .queue_rq = blkif_queue_rq, |
Christoph Hellwig | 2609587 | 2017-04-20 16:03:08 +0200 | [diff] [blame] | 934 | .complete = blkif_complete_rq, |
Bob Liu | 907c3eb | 2015-07-13 17:55:24 +0800 | [diff] [blame] | 935 | }; |
| 936 | |
Bob Liu | 172335a | 2016-07-01 17:43:39 -0400 | [diff] [blame] | 937 | static void blkif_set_queue_limits(struct blkfront_info *info) |
| 938 | { |
| 939 | struct request_queue *rq = info->rq; |
| 940 | struct gendisk *gd = info->gd; |
| 941 | unsigned int segments = info->max_indirect_segments ? : |
| 942 | BLKIF_MAX_SEGMENTS_PER_REQUEST; |
| 943 | |
Bart Van Assche | 8b904b5 | 2018-03-07 17:10:10 -0800 | [diff] [blame] | 944 | blk_queue_flag_set(QUEUE_FLAG_VIRT, rq); |
Bob Liu | 172335a | 2016-07-01 17:43:39 -0400 | [diff] [blame] | 945 | |
| 946 | if (info->feature_discard) { |
Bart Van Assche | 8b904b5 | 2018-03-07 17:10:10 -0800 | [diff] [blame] | 947 | blk_queue_flag_set(QUEUE_FLAG_DISCARD, rq); |
Bob Liu | 172335a | 2016-07-01 17:43:39 -0400 | [diff] [blame] | 948 | blk_queue_max_discard_sectors(rq, get_capacity(gd)); |
Roger Pau Monne | 0549cd6 | 2021-01-19 11:57:27 +0100 | [diff] [blame] | 949 | rq->limits.discard_granularity = info->discard_granularity ?: |
| 950 | info->physical_sector_size; |
Bob Liu | 172335a | 2016-07-01 17:43:39 -0400 | [diff] [blame] | 951 | rq->limits.discard_alignment = info->discard_alignment; |
| 952 | if (info->feature_secdiscard) |
Bart Van Assche | 8b904b5 | 2018-03-07 17:10:10 -0800 | [diff] [blame] | 953 | blk_queue_flag_set(QUEUE_FLAG_SECERASE, rq); |
Bob Liu | 172335a | 2016-07-01 17:43:39 -0400 | [diff] [blame] | 954 | } |
| 955 | |
| 956 | /* Hard sector size and max sectors impersonate the equiv. hardware. */ |
| 957 | blk_queue_logical_block_size(rq, info->sector_size); |
| 958 | blk_queue_physical_block_size(rq, info->physical_sector_size); |
| 959 | blk_queue_max_hw_sectors(rq, (segments * XEN_PAGE_SIZE) / 512); |
| 960 | |
| 961 | /* Each segment in a request is up to an aligned page in size. */ |
| 962 | blk_queue_segment_boundary(rq, PAGE_SIZE - 1); |
| 963 | blk_queue_max_segment_size(rq, PAGE_SIZE); |
| 964 | |
| 965 | /* Ensure a merged request will fit in a single I/O ring slot. */ |
| 966 | blk_queue_max_segments(rq, segments / GRANTS_PER_PSEG); |
| 967 | |
| 968 | /* Make sure buffer addresses are sector-aligned. */ |
| 969 | blk_queue_dma_alignment(rq, 511); |
Bob Liu | 172335a | 2016-07-01 17:43:39 -0400 | [diff] [blame] | 970 | } |
| 971 | |
Mike Christie | a418090 | 2016-06-05 14:32:24 -0500 | [diff] [blame] | 972 | static const char *flush_info(struct blkfront_info *info) |
Vitaly Kuznetsov | fdf9b96 | 2014-12-09 15:25:10 +0100 | [diff] [blame] | 973 | { |
Mike Christie | a418090 | 2016-06-05 14:32:24 -0500 | [diff] [blame] | 974 | if (info->feature_flush && info->feature_fua) |
Vitaly Kuznetsov | fdf9b96 | 2014-12-09 15:25:10 +0100 | [diff] [blame] | 975 | return "barrier: enabled;"; |
Mike Christie | a418090 | 2016-06-05 14:32:24 -0500 | [diff] [blame] | 976 | else if (info->feature_flush) |
Vitaly Kuznetsov | fdf9b96 | 2014-12-09 15:25:10 +0100 | [diff] [blame] | 977 | return "flush diskcache: enabled;"; |
Mike Christie | a418090 | 2016-06-05 14:32:24 -0500 | [diff] [blame] | 978 | else |
Vitaly Kuznetsov | fdf9b96 | 2014-12-09 15:25:10 +0100 | [diff] [blame] | 979 | return "barrier or flush: disabled;"; |
Vitaly Kuznetsov | fdf9b96 | 2014-12-09 15:25:10 +0100 | [diff] [blame] | 980 | } |
Jeremy Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 981 | |
Tejun Heo | 4913efe | 2010-09-03 11:56:16 +0200 | [diff] [blame] | 982 | static void xlvbd_flush(struct blkfront_info *info) |
Jeremy Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 983 | { |
Mike Christie | a418090 | 2016-06-05 14:32:24 -0500 | [diff] [blame] | 984 | blk_queue_write_cache(info->rq, info->feature_flush ? true : false, |
| 985 | info->feature_fua ? true : false); |
Vitaly Kuznetsov | fdf9b96 | 2014-12-09 15:25:10 +0100 | [diff] [blame] | 986 | pr_info("blkfront: %s: %s %s %s %s %s\n", |
Mike Christie | a418090 | 2016-06-05 14:32:24 -0500 | [diff] [blame] | 987 | info->gd->disk_name, flush_info(info), |
Vitaly Kuznetsov | fdf9b96 | 2014-12-09 15:25:10 +0100 | [diff] [blame] | 988 | "persistent grants:", info->feature_persistent ? |
| 989 | "enabled;" : "disabled;", "indirect descriptors:", |
| 990 | info->max_indirect_segments ? "enabled;" : "disabled;"); |
Jeremy Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 991 | } |
| 992 | |
Stefano Stabellini | c80a420 | 2010-12-02 17:55:00 +0000 | [diff] [blame] | 993 | static int xen_translate_vdev(int vdevice, int *minor, unsigned int *offset) |
| 994 | { |
| 995 | int major; |
| 996 | major = BLKIF_MAJOR(vdevice); |
| 997 | *minor = BLKIF_MINOR(vdevice); |
| 998 | switch (major) { |
| 999 | case XEN_IDE0_MAJOR: |
| 1000 | *offset = (*minor / 64) + EMULATED_HD_DISK_NAME_OFFSET; |
| 1001 | *minor = ((*minor / 64) * PARTS_PER_DISK) + |
| 1002 | EMULATED_HD_DISK_MINOR_OFFSET; |
| 1003 | break; |
| 1004 | case XEN_IDE1_MAJOR: |
| 1005 | *offset = (*minor / 64) + 2 + EMULATED_HD_DISK_NAME_OFFSET; |
| 1006 | *minor = (((*minor / 64) + 2) * PARTS_PER_DISK) + |
| 1007 | EMULATED_HD_DISK_MINOR_OFFSET; |
| 1008 | break; |
| 1009 | case XEN_SCSI_DISK0_MAJOR: |
| 1010 | *offset = (*minor / PARTS_PER_DISK) + EMULATED_SD_DISK_NAME_OFFSET; |
| 1011 | *minor = *minor + EMULATED_SD_DISK_MINOR_OFFSET; |
| 1012 | break; |
| 1013 | case XEN_SCSI_DISK1_MAJOR: |
| 1014 | case XEN_SCSI_DISK2_MAJOR: |
| 1015 | case XEN_SCSI_DISK3_MAJOR: |
| 1016 | case XEN_SCSI_DISK4_MAJOR: |
| 1017 | case XEN_SCSI_DISK5_MAJOR: |
| 1018 | case XEN_SCSI_DISK6_MAJOR: |
| 1019 | case XEN_SCSI_DISK7_MAJOR: |
| 1020 | *offset = (*minor / PARTS_PER_DISK) + |
| 1021 | ((major - XEN_SCSI_DISK1_MAJOR + 1) * 16) + |
| 1022 | EMULATED_SD_DISK_NAME_OFFSET; |
| 1023 | *minor = *minor + |
| 1024 | ((major - XEN_SCSI_DISK1_MAJOR + 1) * 16 * PARTS_PER_DISK) + |
| 1025 | EMULATED_SD_DISK_MINOR_OFFSET; |
| 1026 | break; |
| 1027 | case XEN_SCSI_DISK8_MAJOR: |
| 1028 | case XEN_SCSI_DISK9_MAJOR: |
| 1029 | case XEN_SCSI_DISK10_MAJOR: |
| 1030 | case XEN_SCSI_DISK11_MAJOR: |
| 1031 | case XEN_SCSI_DISK12_MAJOR: |
| 1032 | case XEN_SCSI_DISK13_MAJOR: |
| 1033 | case XEN_SCSI_DISK14_MAJOR: |
| 1034 | case XEN_SCSI_DISK15_MAJOR: |
| 1035 | *offset = (*minor / PARTS_PER_DISK) + |
| 1036 | ((major - XEN_SCSI_DISK8_MAJOR + 8) * 16) + |
| 1037 | EMULATED_SD_DISK_NAME_OFFSET; |
| 1038 | *minor = *minor + |
| 1039 | ((major - XEN_SCSI_DISK8_MAJOR + 8) * 16 * PARTS_PER_DISK) + |
| 1040 | EMULATED_SD_DISK_MINOR_OFFSET; |
| 1041 | break; |
| 1042 | case XENVBD_MAJOR: |
| 1043 | *offset = *minor / PARTS_PER_DISK; |
| 1044 | break; |
| 1045 | default: |
| 1046 | printk(KERN_WARNING "blkfront: your disk configuration is " |
| 1047 | "incorrect, please use an xvd device instead\n"); |
| 1048 | return -ENODEV; |
| 1049 | } |
| 1050 | return 0; |
| 1051 | } |
Jeremy Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 1052 | |
Jan Beulich | e77c78c | 2012-04-05 16:37:22 +0100 | [diff] [blame] | 1053 | static char *encode_disk_name(char *ptr, unsigned int n) |
| 1054 | { |
| 1055 | if (n >= 26) |
| 1056 | ptr = encode_disk_name(ptr, n / 26 - 1); |
| 1057 | *ptr = 'a' + n % 26; |
| 1058 | return ptr + 1; |
| 1059 | } |
| 1060 | |
Chris Lalancette | 9246b5f | 2008-09-17 14:30:32 -0700 | [diff] [blame] | 1061 | static int xlvbd_alloc_gendisk(blkif_sector_t capacity, |
Christoph Hellwig | 1a827ce1 | 2021-11-22 14:06:14 +0100 | [diff] [blame] | 1062 | struct blkfront_info *info, u16 sector_size, |
| 1063 | unsigned int physical_sector_size) |
Jeremy Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 1064 | { |
| 1065 | struct gendisk *gd; |
| 1066 | int nr_minors = 1; |
Stefano Stabellini | c80a420 | 2010-12-02 17:55:00 +0000 | [diff] [blame] | 1067 | int err; |
Chris Lalancette | 9246b5f | 2008-09-17 14:30:32 -0700 | [diff] [blame] | 1068 | unsigned int offset; |
| 1069 | int minor; |
| 1070 | int nr_parts; |
Jan Beulich | e77c78c | 2012-04-05 16:37:22 +0100 | [diff] [blame] | 1071 | char *ptr; |
Jeremy Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 1072 | |
| 1073 | BUG_ON(info->gd != NULL); |
| 1074 | BUG_ON(info->rq != NULL); |
| 1075 | |
Chris Lalancette | 9246b5f | 2008-09-17 14:30:32 -0700 | [diff] [blame] | 1076 | if ((info->vdevice>>EXT_SHIFT) > 1) { |
| 1077 | /* this is above the extended range; something is wrong */ |
| 1078 | printk(KERN_WARNING "blkfront: vdevice 0x%x is above the extended range; ignoring\n", info->vdevice); |
| 1079 | return -ENODEV; |
| 1080 | } |
| 1081 | |
| 1082 | if (!VDEV_IS_EXTENDED(info->vdevice)) { |
Stefano Stabellini | c80a420 | 2010-12-02 17:55:00 +0000 | [diff] [blame] | 1083 | err = xen_translate_vdev(info->vdevice, &minor, &offset); |
| 1084 | if (err) |
Nathan Chancellor | 589b728 | 2019-12-09 13:14:44 -0700 | [diff] [blame] | 1085 | return err; |
| 1086 | nr_parts = PARTS_PER_DISK; |
Chris Lalancette | 9246b5f | 2008-09-17 14:30:32 -0700 | [diff] [blame] | 1087 | } else { |
| 1088 | minor = BLKIF_MINOR_EXT(info->vdevice); |
| 1089 | nr_parts = PARTS_PER_EXT_DISK; |
Stefano Stabellini | c80a420 | 2010-12-02 17:55:00 +0000 | [diff] [blame] | 1090 | offset = minor / nr_parts; |
Stefan Bader | 89153b5 | 2011-07-14 15:30:37 +0200 | [diff] [blame] | 1091 | if (xen_hvm_domain() && offset < EMULATED_HD_DISK_NAME_OFFSET + 4) |
Stefano Stabellini | c80a420 | 2010-12-02 17:55:00 +0000 | [diff] [blame] | 1092 | printk(KERN_WARNING "blkfront: vdevice 0x%x might conflict with " |
| 1093 | "emulated IDE disks,\n\t choose an xvd device name" |
| 1094 | "from xvde on\n", info->vdevice); |
Chris Lalancette | 9246b5f | 2008-09-17 14:30:32 -0700 | [diff] [blame] | 1095 | } |
Jan Beulich | e77c78c | 2012-04-05 16:37:22 +0100 | [diff] [blame] | 1096 | if (minor >> MINORBITS) { |
| 1097 | pr_warn("blkfront: %#x's minor (%#x) out of range; ignoring\n", |
| 1098 | info->vdevice, minor); |
| 1099 | return -ENODEV; |
| 1100 | } |
Chris Lalancette | 9246b5f | 2008-09-17 14:30:32 -0700 | [diff] [blame] | 1101 | |
| 1102 | if ((minor % nr_parts) == 0) |
| 1103 | nr_minors = nr_parts; |
Jeremy Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 1104 | |
Jan Beulich | 0e34582 | 2010-08-07 18:28:55 +0200 | [diff] [blame] | 1105 | err = xlbd_reserve_minors(minor, nr_minors); |
| 1106 | if (err) |
Christoph Hellwig | 3b62c14 | 2021-06-02 09:53:40 +0300 | [diff] [blame] | 1107 | return err; |
Jan Beulich | 0e34582 | 2010-08-07 18:28:55 +0200 | [diff] [blame] | 1108 | |
Christoph Hellwig | 3b62c14 | 2021-06-02 09:53:40 +0300 | [diff] [blame] | 1109 | memset(&info->tag_set, 0, sizeof(info->tag_set)); |
| 1110 | info->tag_set.ops = &blkfront_mq_ops; |
| 1111 | info->tag_set.nr_hw_queues = info->nr_rings; |
| 1112 | if (HAS_EXTRA_REQ && info->max_indirect_segments == 0) { |
| 1113 | /* |
| 1114 | * When indirect descriptior is not supported, the I/O request |
| 1115 | * will be split between multiple request in the ring. |
| 1116 | * To avoid problems when sending the request, divide by |
| 1117 | * 2 the depth of the queue. |
| 1118 | */ |
| 1119 | info->tag_set.queue_depth = BLK_RING_SIZE(info) / 2; |
| 1120 | } else |
| 1121 | info->tag_set.queue_depth = BLK_RING_SIZE(info); |
| 1122 | info->tag_set.numa_node = NUMA_NO_NODE; |
| 1123 | info->tag_set.flags = BLK_MQ_F_SHOULD_MERGE; |
| 1124 | info->tag_set.cmd_size = sizeof(struct blkif_req); |
| 1125 | info->tag_set.driver_data = info; |
| 1126 | |
| 1127 | err = blk_mq_alloc_tag_set(&info->tag_set); |
| 1128 | if (err) |
| 1129 | goto out_release_minors; |
| 1130 | |
| 1131 | gd = blk_mq_alloc_disk(&info->tag_set, info); |
| 1132 | if (IS_ERR(gd)) { |
| 1133 | err = PTR_ERR(gd); |
| 1134 | goto out_free_tag_set; |
| 1135 | } |
Jeremy Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 1136 | |
Jan Beulich | e77c78c | 2012-04-05 16:37:22 +0100 | [diff] [blame] | 1137 | strcpy(gd->disk_name, DEV_NAME); |
| 1138 | ptr = encode_disk_name(gd->disk_name + sizeof(DEV_NAME) - 1, offset); |
| 1139 | BUG_ON(ptr >= gd->disk_name + DISK_NAME_LEN); |
| 1140 | if (nr_minors > 1) |
| 1141 | *ptr = 0; |
| 1142 | else |
| 1143 | snprintf(ptr, gd->disk_name + DISK_NAME_LEN - ptr, |
| 1144 | "%d", minor & (nr_parts - 1)); |
Jeremy Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 1145 | |
| 1146 | gd->major = XENVBD_MAJOR; |
| 1147 | gd->first_minor = minor; |
Christoph Hellwig | 3b62c14 | 2021-06-02 09:53:40 +0300 | [diff] [blame] | 1148 | gd->minors = nr_minors; |
Jeremy Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 1149 | gd->fops = &xlvbd_block_fops; |
| 1150 | gd->private_data = info; |
Jeremy Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 1151 | set_capacity(gd, capacity); |
| 1152 | |
Christoph Hellwig | 3b62c14 | 2021-06-02 09:53:40 +0300 | [diff] [blame] | 1153 | info->rq = gd->queue; |
| 1154 | info->gd = gd; |
| 1155 | info->sector_size = sector_size; |
| 1156 | info->physical_sector_size = physical_sector_size; |
| 1157 | blkif_set_queue_limits(info); |
Jeremy Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 1158 | |
Tejun Heo | 4913efe | 2010-09-03 11:56:16 +0200 | [diff] [blame] | 1159 | xlvbd_flush(info); |
Jeremy Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 1160 | |
Christoph Hellwig | 1a827ce1 | 2021-11-22 14:06:14 +0100 | [diff] [blame] | 1161 | if (info->vdisk_info & VDISK_READONLY) |
Jeremy Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 1162 | set_disk_ro(gd, 1); |
Christoph Hellwig | 1a827ce1 | 2021-11-22 14:06:14 +0100 | [diff] [blame] | 1163 | if (info->vdisk_info & VDISK_REMOVABLE) |
Jeremy Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 1164 | gd->flags |= GENHD_FL_REMOVABLE; |
| 1165 | |
Jeremy Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 1166 | return 0; |
| 1167 | |
Christoph Hellwig | 3b62c14 | 2021-06-02 09:53:40 +0300 | [diff] [blame] | 1168 | out_free_tag_set: |
| 1169 | blk_mq_free_tag_set(&info->tag_set); |
| 1170 | out_release_minors: |
Jan Beulich | 0e34582 | 2010-08-07 18:28:55 +0200 | [diff] [blame] | 1171 | xlbd_release_minors(minor, nr_minors); |
Jeremy Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 1172 | return err; |
| 1173 | } |
| 1174 | |
Bob Liu | 1165956 | 2015-11-14 11:12:13 +0800 | [diff] [blame] | 1175 | /* Already hold rinfo->ring_lock. */ |
| 1176 | static inline void kick_pending_request_queues_locked(struct blkfront_ring_info *rinfo) |
Jeremy Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 1177 | { |
Bob Liu | 81f3516 | 2015-11-14 11:12:11 +0800 | [diff] [blame] | 1178 | if (!RING_FULL(&rinfo->ring)) |
| 1179 | blk_mq_start_stopped_hw_queues(rinfo->dev_info->rq, true); |
Jeremy Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 1180 | } |
| 1181 | |
Bob Liu | 1165956 | 2015-11-14 11:12:13 +0800 | [diff] [blame] | 1182 | static void kick_pending_request_queues(struct blkfront_ring_info *rinfo) |
| 1183 | { |
| 1184 | unsigned long flags; |
| 1185 | |
| 1186 | spin_lock_irqsave(&rinfo->ring_lock, flags); |
| 1187 | kick_pending_request_queues_locked(rinfo); |
| 1188 | spin_unlock_irqrestore(&rinfo->ring_lock, flags); |
| 1189 | } |
| 1190 | |
Jeremy Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 1191 | static void blkif_restart_queue(struct work_struct *work) |
| 1192 | { |
Bob Liu | 81f3516 | 2015-11-14 11:12:11 +0800 | [diff] [blame] | 1193 | struct blkfront_ring_info *rinfo = container_of(work, struct blkfront_ring_info, work); |
Jeremy Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 1194 | |
Bob Liu | 81f3516 | 2015-11-14 11:12:11 +0800 | [diff] [blame] | 1195 | if (rinfo->dev_info->connected == BLKIF_STATE_CONNECTED) |
| 1196 | kick_pending_request_queues(rinfo); |
Jeremy Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 1197 | } |
| 1198 | |
Bob Liu | 3df0e50 | 2015-11-14 11:12:12 +0800 | [diff] [blame] | 1199 | static void blkif_free_ring(struct blkfront_ring_info *rinfo) |
Jeremy Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 1200 | { |
Bob Liu | 73716df | 2015-11-16 16:51:39 -0500 | [diff] [blame] | 1201 | struct grant *persistent_gnt, *n; |
Bob Liu | 3df0e50 | 2015-11-14 11:12:12 +0800 | [diff] [blame] | 1202 | struct blkfront_info *info = rinfo->dev_info; |
Roger Pau Monne | 402b27f | 2013-04-18 16:06:54 +0200 | [diff] [blame] | 1203 | int i, j, segs; |
Roger Pau Monne | 0a8704a | 2012-10-24 18:58:45 +0200 | [diff] [blame] | 1204 | |
Roger Pau Monne | bfe11d6 | 2013-10-29 18:31:14 +0100 | [diff] [blame] | 1205 | /* |
| 1206 | * Remove indirect pages, this only happens when using indirect |
| 1207 | * descriptors but not persistent grants |
| 1208 | */ |
Bob Liu | 81f3516 | 2015-11-14 11:12:11 +0800 | [diff] [blame] | 1209 | if (!list_empty(&rinfo->indirect_pages)) { |
Roger Pau Monne | bfe11d6 | 2013-10-29 18:31:14 +0100 | [diff] [blame] | 1210 | struct page *indirect_page, *n; |
| 1211 | |
| 1212 | BUG_ON(info->feature_persistent); |
Bob Liu | 81f3516 | 2015-11-14 11:12:11 +0800 | [diff] [blame] | 1213 | list_for_each_entry_safe(indirect_page, n, &rinfo->indirect_pages, lru) { |
Roger Pau Monne | bfe11d6 | 2013-10-29 18:31:14 +0100 | [diff] [blame] | 1214 | list_del(&indirect_page->lru); |
| 1215 | __free_page(indirect_page); |
| 1216 | } |
| 1217 | } |
| 1218 | |
Bob Liu | 73716df | 2015-11-16 16:51:39 -0500 | [diff] [blame] | 1219 | /* Remove all persistent grants. */ |
| 1220 | if (!list_empty(&rinfo->grants)) { |
| 1221 | list_for_each_entry_safe(persistent_gnt, n, |
| 1222 | &rinfo->grants, node) { |
| 1223 | list_del(&persistent_gnt->node); |
| 1224 | if (persistent_gnt->gref != GRANT_INVALID_REF) { |
| 1225 | gnttab_end_foreign_access(persistent_gnt->gref, |
| 1226 | 0, 0UL); |
| 1227 | rinfo->persistent_gnts_c--; |
| 1228 | } |
| 1229 | if (info->feature_persistent) |
| 1230 | __free_page(persistent_gnt->page); |
| 1231 | kfree(persistent_gnt); |
| 1232 | } |
| 1233 | } |
| 1234 | BUG_ON(rinfo->persistent_gnts_c != 0); |
| 1235 | |
Bob Liu | 86839c5 | 2015-06-03 13:40:03 +0800 | [diff] [blame] | 1236 | for (i = 0; i < BLK_RING_SIZE(info); i++) { |
Roger Pau Monne | 402b27f | 2013-04-18 16:06:54 +0200 | [diff] [blame] | 1237 | /* |
| 1238 | * Clear persistent grants present in requests already |
| 1239 | * on the shared ring |
| 1240 | */ |
Bob Liu | 81f3516 | 2015-11-14 11:12:11 +0800 | [diff] [blame] | 1241 | if (!rinfo->shadow[i].request) |
Roger Pau Monne | 402b27f | 2013-04-18 16:06:54 +0200 | [diff] [blame] | 1242 | goto free_shadow; |
| 1243 | |
Bob Liu | 81f3516 | 2015-11-14 11:12:11 +0800 | [diff] [blame] | 1244 | segs = rinfo->shadow[i].req.operation == BLKIF_OP_INDIRECT ? |
| 1245 | rinfo->shadow[i].req.u.indirect.nr_segments : |
| 1246 | rinfo->shadow[i].req.u.rw.nr_segments; |
Roger Pau Monne | 402b27f | 2013-04-18 16:06:54 +0200 | [diff] [blame] | 1247 | for (j = 0; j < segs; j++) { |
Bob Liu | 81f3516 | 2015-11-14 11:12:11 +0800 | [diff] [blame] | 1248 | persistent_gnt = rinfo->shadow[i].grants_used[j]; |
Roger Pau Monne | 402b27f | 2013-04-18 16:06:54 +0200 | [diff] [blame] | 1249 | gnttab_end_foreign_access(persistent_gnt->gref, 0, 0UL); |
Roger Pau Monne | bfe11d6 | 2013-10-29 18:31:14 +0100 | [diff] [blame] | 1250 | if (info->feature_persistent) |
Julien Grall | a7a6df2 | 2015-06-30 11:58:51 +0100 | [diff] [blame] | 1251 | __free_page(persistent_gnt->page); |
Roger Pau Monne | 402b27f | 2013-04-18 16:06:54 +0200 | [diff] [blame] | 1252 | kfree(persistent_gnt); |
| 1253 | } |
| 1254 | |
Bob Liu | 81f3516 | 2015-11-14 11:12:11 +0800 | [diff] [blame] | 1255 | if (rinfo->shadow[i].req.operation != BLKIF_OP_INDIRECT) |
Roger Pau Monne | 402b27f | 2013-04-18 16:06:54 +0200 | [diff] [blame] | 1256 | /* |
| 1257 | * If this is not an indirect operation don't try to |
| 1258 | * free indirect segments |
| 1259 | */ |
| 1260 | goto free_shadow; |
| 1261 | |
| 1262 | for (j = 0; j < INDIRECT_GREFS(segs); j++) { |
Bob Liu | 81f3516 | 2015-11-14 11:12:11 +0800 | [diff] [blame] | 1263 | persistent_gnt = rinfo->shadow[i].indirect_grants[j]; |
Roger Pau Monne | 402b27f | 2013-04-18 16:06:54 +0200 | [diff] [blame] | 1264 | gnttab_end_foreign_access(persistent_gnt->gref, 0, 0UL); |
Julien Grall | a7a6df2 | 2015-06-30 11:58:51 +0100 | [diff] [blame] | 1265 | __free_page(persistent_gnt->page); |
Roger Pau Monne | 402b27f | 2013-04-18 16:06:54 +0200 | [diff] [blame] | 1266 | kfree(persistent_gnt); |
| 1267 | } |
| 1268 | |
| 1269 | free_shadow: |
Roger Pau Monne | 1d5c76e | 2019-05-03 17:04:01 +0200 | [diff] [blame] | 1270 | kvfree(rinfo->shadow[i].grants_used); |
Bob Liu | 81f3516 | 2015-11-14 11:12:11 +0800 | [diff] [blame] | 1271 | rinfo->shadow[i].grants_used = NULL; |
Roger Pau Monne | 1d5c76e | 2019-05-03 17:04:01 +0200 | [diff] [blame] | 1272 | kvfree(rinfo->shadow[i].indirect_grants); |
Bob Liu | 81f3516 | 2015-11-14 11:12:11 +0800 | [diff] [blame] | 1273 | rinfo->shadow[i].indirect_grants = NULL; |
Roger Pau Monne | 1d5c76e | 2019-05-03 17:04:01 +0200 | [diff] [blame] | 1274 | kvfree(rinfo->shadow[i].sg); |
Bob Liu | 81f3516 | 2015-11-14 11:12:11 +0800 | [diff] [blame] | 1275 | rinfo->shadow[i].sg = NULL; |
Roger Pau Monne | 402b27f | 2013-04-18 16:06:54 +0200 | [diff] [blame] | 1276 | } |
| 1277 | |
Jeremy Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 1278 | /* No more gnttab callback work. */ |
Bob Liu | 81f3516 | 2015-11-14 11:12:11 +0800 | [diff] [blame] | 1279 | gnttab_cancel_free_callback(&rinfo->callback); |
Jeremy Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 1280 | |
| 1281 | /* Flush gnttab callback work. Must be done with no locks held. */ |
Bob Liu | 81f3516 | 2015-11-14 11:12:11 +0800 | [diff] [blame] | 1282 | flush_work(&rinfo->work); |
Jeremy Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 1283 | |
| 1284 | /* Free resources associated with old device channel. */ |
Bob Liu | 86839c5 | 2015-06-03 13:40:03 +0800 | [diff] [blame] | 1285 | for (i = 0; i < info->nr_ring_pages; i++) { |
Bob Liu | 81f3516 | 2015-11-14 11:12:11 +0800 | [diff] [blame] | 1286 | if (rinfo->ring_ref[i] != GRANT_INVALID_REF) { |
| 1287 | gnttab_end_foreign_access(rinfo->ring_ref[i], 0, 0); |
| 1288 | rinfo->ring_ref[i] = GRANT_INVALID_REF; |
Bob Liu | 86839c5 | 2015-06-03 13:40:03 +0800 | [diff] [blame] | 1289 | } |
Jeremy Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 1290 | } |
Bob Liu | 6c647b0 | 2016-07-01 15:45:57 -0400 | [diff] [blame] | 1291 | free_pages((unsigned long)rinfo->ring.sring, get_order(info->nr_ring_pages * XEN_PAGE_SIZE)); |
Bob Liu | 81f3516 | 2015-11-14 11:12:11 +0800 | [diff] [blame] | 1292 | rinfo->ring.sring = NULL; |
Bob Liu | 86839c5 | 2015-06-03 13:40:03 +0800 | [diff] [blame] | 1293 | |
Bob Liu | 81f3516 | 2015-11-14 11:12:11 +0800 | [diff] [blame] | 1294 | if (rinfo->irq) |
| 1295 | unbind_from_irqhandler(rinfo->irq, rinfo); |
| 1296 | rinfo->evtchn = rinfo->irq = 0; |
Bob Liu | 3df0e50 | 2015-11-14 11:12:12 +0800 | [diff] [blame] | 1297 | } |
Jeremy Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 1298 | |
Bob Liu | 3df0e50 | 2015-11-14 11:12:12 +0800 | [diff] [blame] | 1299 | static void blkif_free(struct blkfront_info *info, int suspend) |
| 1300 | { |
Bob Liu | 3df0e50 | 2015-11-14 11:12:12 +0800 | [diff] [blame] | 1301 | unsigned int i; |
Juergen Gross | 4ab50af | 2020-03-05 16:51:29 +0100 | [diff] [blame] | 1302 | struct blkfront_ring_info *rinfo; |
Bob Liu | 3df0e50 | 2015-11-14 11:12:12 +0800 | [diff] [blame] | 1303 | |
| 1304 | /* Prevent new requests being issued until we fix things up. */ |
Bob Liu | 3df0e50 | 2015-11-14 11:12:12 +0800 | [diff] [blame] | 1305 | info->connected = suspend ? |
| 1306 | BLKIF_STATE_SUSPENDED : BLKIF_STATE_DISCONNECTED; |
| 1307 | /* No more blkif_request(). */ |
| 1308 | if (info->rq) |
| 1309 | blk_mq_stop_hw_queues(info->rq); |
| 1310 | |
Juergen Gross | 4ab50af | 2020-03-05 16:51:29 +0100 | [diff] [blame] | 1311 | for_each_rinfo(info, rinfo, i) |
| 1312 | blkif_free_ring(rinfo); |
Bob Liu | 3df0e50 | 2015-11-14 11:12:12 +0800 | [diff] [blame] | 1313 | |
Roger Pau Monne | 1d5c76e | 2019-05-03 17:04:01 +0200 | [diff] [blame] | 1314 | kvfree(info->rinfo); |
Bob Liu | 3df0e50 | 2015-11-14 11:12:12 +0800 | [diff] [blame] | 1315 | info->rinfo = NULL; |
| 1316 | info->nr_rings = 0; |
Jeremy Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 1317 | } |
| 1318 | |
Julien Grall | c004a6f | 2015-07-22 16:44:54 +0100 | [diff] [blame] | 1319 | struct copy_from_grant { |
| 1320 | const struct blk_shadow *s; |
| 1321 | unsigned int grant_idx; |
| 1322 | unsigned int bvec_offset; |
| 1323 | char *bvec_data; |
| 1324 | }; |
| 1325 | |
| 1326 | static void blkif_copy_from_grant(unsigned long gfn, unsigned int offset, |
| 1327 | unsigned int len, void *data) |
| 1328 | { |
| 1329 | struct copy_from_grant *info = data; |
| 1330 | char *shared_data; |
| 1331 | /* Convenient aliases */ |
| 1332 | const struct blk_shadow *s = info->s; |
| 1333 | |
| 1334 | shared_data = kmap_atomic(s->grants_used[info->grant_idx]->page); |
| 1335 | |
| 1336 | memcpy(info->bvec_data + info->bvec_offset, |
| 1337 | shared_data + offset, len); |
| 1338 | |
| 1339 | info->bvec_offset += len; |
| 1340 | info->grant_idx++; |
| 1341 | |
| 1342 | kunmap_atomic(shared_data); |
| 1343 | } |
| 1344 | |
Julien Grall | 6cc56833 | 2015-08-13 13:13:35 +0100 | [diff] [blame] | 1345 | static enum blk_req_status blkif_rsp_to_req_status(int rsp) |
| 1346 | { |
| 1347 | switch (rsp) |
| 1348 | { |
| 1349 | case BLKIF_RSP_OKAY: |
| 1350 | return REQ_DONE; |
| 1351 | case BLKIF_RSP_EOPNOTSUPP: |
| 1352 | return REQ_EOPNOTSUPP; |
| 1353 | case BLKIF_RSP_ERROR: |
Julien Grall | 6cc56833 | 2015-08-13 13:13:35 +0100 | [diff] [blame] | 1354 | default: |
| 1355 | return REQ_ERROR; |
| 1356 | } |
| 1357 | } |
| 1358 | |
| 1359 | /* |
| 1360 | * Get the final status of the block request based on two ring response |
| 1361 | */ |
| 1362 | static int blkif_get_final_status(enum blk_req_status s1, |
| 1363 | enum blk_req_status s2) |
| 1364 | { |
Juergen Gross | b94e4b1 | 2021-07-30 12:38:54 +0200 | [diff] [blame] | 1365 | BUG_ON(s1 < REQ_DONE); |
| 1366 | BUG_ON(s2 < REQ_DONE); |
Julien Grall | 6cc56833 | 2015-08-13 13:13:35 +0100 | [diff] [blame] | 1367 | |
| 1368 | if (s1 == REQ_ERROR || s2 == REQ_ERROR) |
| 1369 | return BLKIF_RSP_ERROR; |
| 1370 | else if (s1 == REQ_EOPNOTSUPP || s2 == REQ_EOPNOTSUPP) |
| 1371 | return BLKIF_RSP_EOPNOTSUPP; |
| 1372 | return BLKIF_RSP_OKAY; |
| 1373 | } |
| 1374 | |
| 1375 | static bool blkif_completion(unsigned long *id, |
| 1376 | struct blkfront_ring_info *rinfo, |
Roger Pau Monne | 0a8704a | 2012-10-24 18:58:45 +0200 | [diff] [blame] | 1377 | struct blkif_response *bret) |
Jeremy Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 1378 | { |
Roger Pau Monne | d62f691 | 2012-12-07 19:00:31 +0100 | [diff] [blame] | 1379 | int i = 0; |
Roger Pau Monne | b764915 | 2013-05-02 10:58:50 +0200 | [diff] [blame] | 1380 | struct scatterlist *sg; |
Julien Grall | c004a6f | 2015-07-22 16:44:54 +0100 | [diff] [blame] | 1381 | int num_sg, num_grant; |
Bob Liu | 81f3516 | 2015-11-14 11:12:11 +0800 | [diff] [blame] | 1382 | struct blkfront_info *info = rinfo->dev_info; |
Julien Grall | 6cc56833 | 2015-08-13 13:13:35 +0100 | [diff] [blame] | 1383 | struct blk_shadow *s = &rinfo->shadow[*id]; |
Julien Grall | c004a6f | 2015-07-22 16:44:54 +0100 | [diff] [blame] | 1384 | struct copy_from_grant data = { |
Julien Grall | c004a6f | 2015-07-22 16:44:54 +0100 | [diff] [blame] | 1385 | .grant_idx = 0, |
| 1386 | }; |
Roger Pau Monne | 402b27f | 2013-04-18 16:06:54 +0200 | [diff] [blame] | 1387 | |
Julien Grall | c004a6f | 2015-07-22 16:44:54 +0100 | [diff] [blame] | 1388 | num_grant = s->req.operation == BLKIF_OP_INDIRECT ? |
Roger Pau Monne | 402b27f | 2013-04-18 16:06:54 +0200 | [diff] [blame] | 1389 | s->req.u.indirect.nr_segments : s->req.u.rw.nr_segments; |
Julien Grall | 6cc56833 | 2015-08-13 13:13:35 +0100 | [diff] [blame] | 1390 | |
| 1391 | /* The I/O request may be split in two. */ |
| 1392 | if (unlikely(s->associated_id != NO_ASSOCIATED_ID)) { |
| 1393 | struct blk_shadow *s2 = &rinfo->shadow[s->associated_id]; |
| 1394 | |
| 1395 | /* Keep the status of the current response in shadow. */ |
| 1396 | s->status = blkif_rsp_to_req_status(bret->status); |
| 1397 | |
| 1398 | /* Wait the second response if not yet here. */ |
Juergen Gross | b94e4b1 | 2021-07-30 12:38:54 +0200 | [diff] [blame] | 1399 | if (s2->status < REQ_DONE) |
Gustavo A. R. Silva | f87c30c | 2018-08-06 08:14:55 -0600 | [diff] [blame] | 1400 | return false; |
Julien Grall | 6cc56833 | 2015-08-13 13:13:35 +0100 | [diff] [blame] | 1401 | |
| 1402 | bret->status = blkif_get_final_status(s->status, |
| 1403 | s2->status); |
| 1404 | |
| 1405 | /* |
| 1406 | * All the grants is stored in the first shadow in order |
| 1407 | * to make the completion code simpler. |
| 1408 | */ |
| 1409 | num_grant += s2->req.u.rw.nr_segments; |
| 1410 | |
| 1411 | /* |
| 1412 | * The two responses may not come in order. Only the |
| 1413 | * first request will store the scatter-gather list. |
| 1414 | */ |
| 1415 | if (s2->num_sg != 0) { |
| 1416 | /* Update "id" with the ID of the first response. */ |
| 1417 | *id = s->associated_id; |
| 1418 | s = s2; |
| 1419 | } |
| 1420 | |
| 1421 | /* |
| 1422 | * We don't need anymore the second request, so recycling |
| 1423 | * it now. |
| 1424 | */ |
| 1425 | if (add_id_to_freelist(rinfo, s->associated_id)) |
| 1426 | WARN(1, "%s: can't recycle the second part (id = %ld) of the request\n", |
| 1427 | info->gd->disk_name, s->associated_id); |
| 1428 | } |
| 1429 | |
| 1430 | data.s = s; |
Julien Grall | c004a6f | 2015-07-22 16:44:54 +0100 | [diff] [blame] | 1431 | num_sg = s->num_sg; |
Roger Pau Monne | 0a8704a | 2012-10-24 18:58:45 +0200 | [diff] [blame] | 1432 | |
Roger Pau Monne | bfe11d6 | 2013-10-29 18:31:14 +0100 | [diff] [blame] | 1433 | if (bret->operation == BLKIF_OP_READ && info->feature_persistent) { |
Julien Grall | c004a6f | 2015-07-22 16:44:54 +0100 | [diff] [blame] | 1434 | for_each_sg(s->sg, sg, num_sg, i) { |
Roger Pau Monne | b764915 | 2013-05-02 10:58:50 +0200 | [diff] [blame] | 1435 | BUG_ON(sg->offset + sg->length > PAGE_SIZE); |
Julien Grall | c004a6f | 2015-07-22 16:44:54 +0100 | [diff] [blame] | 1436 | |
| 1437 | data.bvec_offset = sg->offset; |
| 1438 | data.bvec_data = kmap_atomic(sg_page(sg)); |
| 1439 | |
| 1440 | gnttab_foreach_grant_in_range(sg_page(sg), |
| 1441 | sg->offset, |
| 1442 | sg->length, |
| 1443 | blkif_copy_from_grant, |
| 1444 | &data); |
| 1445 | |
| 1446 | kunmap_atomic(data.bvec_data); |
Roger Pau Monne | 0a8704a | 2012-10-24 18:58:45 +0200 | [diff] [blame] | 1447 | } |
| 1448 | } |
| 1449 | /* Add the persistent grant into the list of free grants */ |
Julien Grall | c004a6f | 2015-07-22 16:44:54 +0100 | [diff] [blame] | 1450 | for (i = 0; i < num_grant; i++) { |
Roger Pau Monne | fbe363c | 2013-08-12 12:53:44 +0200 | [diff] [blame] | 1451 | if (gnttab_query_foreign_access(s->grants_used[i]->gref)) { |
| 1452 | /* |
| 1453 | * If the grant is still mapped by the backend (the |
| 1454 | * backend has chosen to make this grant persistent) |
| 1455 | * we add it at the head of the list, so it will be |
| 1456 | * reused first. |
| 1457 | */ |
Roger Pau Monne | bfe11d6 | 2013-10-29 18:31:14 +0100 | [diff] [blame] | 1458 | if (!info->feature_persistent) |
| 1459 | pr_alert_ratelimited("backed has not unmapped grant: %u\n", |
| 1460 | s->grants_used[i]->gref); |
Bob Liu | 73716df | 2015-11-16 16:51:39 -0500 | [diff] [blame] | 1461 | list_add(&s->grants_used[i]->node, &rinfo->grants); |
| 1462 | rinfo->persistent_gnts_c++; |
Roger Pau Monne | fbe363c | 2013-08-12 12:53:44 +0200 | [diff] [blame] | 1463 | } else { |
| 1464 | /* |
| 1465 | * If the grant is not mapped by the backend we end the |
| 1466 | * foreign access and add it to the tail of the list, |
| 1467 | * so it will not be picked again unless we run out of |
| 1468 | * persistent grants. |
| 1469 | */ |
| 1470 | gnttab_end_foreign_access(s->grants_used[i]->gref, 0, 0UL); |
| 1471 | s->grants_used[i]->gref = GRANT_INVALID_REF; |
Bob Liu | 73716df | 2015-11-16 16:51:39 -0500 | [diff] [blame] | 1472 | list_add_tail(&s->grants_used[i]->node, &rinfo->grants); |
Roger Pau Monne | fbe363c | 2013-08-12 12:53:44 +0200 | [diff] [blame] | 1473 | } |
Roger Pau Monne | 0a8704a | 2012-10-24 18:58:45 +0200 | [diff] [blame] | 1474 | } |
Roger Pau Monne | 402b27f | 2013-04-18 16:06:54 +0200 | [diff] [blame] | 1475 | if (s->req.operation == BLKIF_OP_INDIRECT) { |
Julien Grall | c004a6f | 2015-07-22 16:44:54 +0100 | [diff] [blame] | 1476 | for (i = 0; i < INDIRECT_GREFS(num_grant); i++) { |
Roger Pau Monne | fbe363c | 2013-08-12 12:53:44 +0200 | [diff] [blame] | 1477 | if (gnttab_query_foreign_access(s->indirect_grants[i]->gref)) { |
Roger Pau Monne | bfe11d6 | 2013-10-29 18:31:14 +0100 | [diff] [blame] | 1478 | if (!info->feature_persistent) |
| 1479 | pr_alert_ratelimited("backed has not unmapped grant: %u\n", |
| 1480 | s->indirect_grants[i]->gref); |
Bob Liu | 73716df | 2015-11-16 16:51:39 -0500 | [diff] [blame] | 1481 | list_add(&s->indirect_grants[i]->node, &rinfo->grants); |
| 1482 | rinfo->persistent_gnts_c++; |
Roger Pau Monne | fbe363c | 2013-08-12 12:53:44 +0200 | [diff] [blame] | 1483 | } else { |
Roger Pau Monne | bfe11d6 | 2013-10-29 18:31:14 +0100 | [diff] [blame] | 1484 | struct page *indirect_page; |
| 1485 | |
Roger Pau Monne | fbe363c | 2013-08-12 12:53:44 +0200 | [diff] [blame] | 1486 | gnttab_end_foreign_access(s->indirect_grants[i]->gref, 0, 0UL); |
Roger Pau Monne | bfe11d6 | 2013-10-29 18:31:14 +0100 | [diff] [blame] | 1487 | /* |
| 1488 | * Add the used indirect page back to the list of |
| 1489 | * available pages for indirect grefs. |
| 1490 | */ |
Bob Liu | 7b07675 | 2015-07-22 14:40:09 +0800 | [diff] [blame] | 1491 | if (!info->feature_persistent) { |
Julien Grall | a7a6df2 | 2015-06-30 11:58:51 +0100 | [diff] [blame] | 1492 | indirect_page = s->indirect_grants[i]->page; |
Bob Liu | 81f3516 | 2015-11-14 11:12:11 +0800 | [diff] [blame] | 1493 | list_add(&indirect_page->lru, &rinfo->indirect_pages); |
Bob Liu | 7b07675 | 2015-07-22 14:40:09 +0800 | [diff] [blame] | 1494 | } |
Roger Pau Monne | fbe363c | 2013-08-12 12:53:44 +0200 | [diff] [blame] | 1495 | s->indirect_grants[i]->gref = GRANT_INVALID_REF; |
Bob Liu | 73716df | 2015-11-16 16:51:39 -0500 | [diff] [blame] | 1496 | list_add_tail(&s->indirect_grants[i]->node, &rinfo->grants); |
Roger Pau Monne | fbe363c | 2013-08-12 12:53:44 +0200 | [diff] [blame] | 1497 | } |
Roger Pau Monne | 402b27f | 2013-04-18 16:06:54 +0200 | [diff] [blame] | 1498 | } |
| 1499 | } |
Julien Grall | 6cc56833 | 2015-08-13 13:13:35 +0100 | [diff] [blame] | 1500 | |
Gustavo A. R. Silva | f87c30c | 2018-08-06 08:14:55 -0600 | [diff] [blame] | 1501 | return true; |
Jeremy Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 1502 | } |
| 1503 | |
| 1504 | static irqreturn_t blkif_interrupt(int irq, void *dev_id) |
| 1505 | { |
| 1506 | struct request *req; |
Juergen Gross | 71b6624 | 2021-07-30 12:38:52 +0200 | [diff] [blame] | 1507 | struct blkif_response bret; |
Jeremy Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 1508 | RING_IDX i, rp; |
| 1509 | unsigned long flags; |
Bob Liu | 81f3516 | 2015-11-14 11:12:11 +0800 | [diff] [blame] | 1510 | struct blkfront_ring_info *rinfo = (struct blkfront_ring_info *)dev_id; |
| 1511 | struct blkfront_info *info = rinfo->dev_info; |
Juergen Gross | 0fd08a3 | 2021-12-16 08:24:08 +0100 | [diff] [blame] | 1512 | unsigned int eoiflag = XEN_EOI_FLAG_SPURIOUS; |
Jeremy Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 1513 | |
Juergen Gross | 0fd08a3 | 2021-12-16 08:24:08 +0100 | [diff] [blame] | 1514 | if (unlikely(info->connected != BLKIF_STATE_CONNECTED)) { |
| 1515 | xen_irq_lateeoi(irq, XEN_EOI_FLAG_SPURIOUS); |
Jeremy Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 1516 | return IRQ_HANDLED; |
Juergen Gross | 0fd08a3 | 2021-12-16 08:24:08 +0100 | [diff] [blame] | 1517 | } |
Jeremy Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 1518 | |
Bob Liu | 1165956 | 2015-11-14 11:12:13 +0800 | [diff] [blame] | 1519 | spin_lock_irqsave(&rinfo->ring_lock, flags); |
Jeremy Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 1520 | again: |
Juergen Gross | b94e4b1 | 2021-07-30 12:38:54 +0200 | [diff] [blame] | 1521 | rp = READ_ONCE(rinfo->ring.sring->rsp_prod); |
| 1522 | virt_rmb(); /* Ensure we see queued responses up to 'rp'. */ |
| 1523 | if (RING_RESPONSE_PROD_OVERFLOW(&rinfo->ring, rp)) { |
| 1524 | pr_alert("%s: illegal number of responses %u\n", |
| 1525 | info->gd->disk_name, rp - rinfo->ring.rsp_cons); |
| 1526 | goto err; |
| 1527 | } |
Jeremy Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 1528 | |
Bob Liu | 81f3516 | 2015-11-14 11:12:11 +0800 | [diff] [blame] | 1529 | for (i = rinfo->ring.rsp_cons; i != rp; i++) { |
Jeremy Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 1530 | unsigned long id; |
Juergen Gross | b94e4b1 | 2021-07-30 12:38:54 +0200 | [diff] [blame] | 1531 | unsigned int op; |
Jeremy Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 1532 | |
Juergen Gross | 0fd08a3 | 2021-12-16 08:24:08 +0100 | [diff] [blame] | 1533 | eoiflag = 0; |
| 1534 | |
Juergen Gross | 71b6624 | 2021-07-30 12:38:52 +0200 | [diff] [blame] | 1535 | RING_COPY_RESPONSE(&rinfo->ring, i, &bret); |
| 1536 | id = bret.id; |
| 1537 | |
Konrad Rzeszutek Wilk | 6878c32 | 2012-05-25 17:34:51 -0400 | [diff] [blame] | 1538 | /* |
| 1539 | * The backend has messed up and given us an id that we would |
| 1540 | * never have given to it (we stamp it up to BLK_RING_SIZE - |
| 1541 | * look in get_id_from_freelist. |
| 1542 | */ |
Bob Liu | 86839c5 | 2015-06-03 13:40:03 +0800 | [diff] [blame] | 1543 | if (id >= BLK_RING_SIZE(info)) { |
Juergen Gross | b94e4b1 | 2021-07-30 12:38:54 +0200 | [diff] [blame] | 1544 | pr_alert("%s: response has incorrect id (%ld)\n", |
| 1545 | info->gd->disk_name, id); |
| 1546 | goto err; |
Konrad Rzeszutek Wilk | 6878c32 | 2012-05-25 17:34:51 -0400 | [diff] [blame] | 1547 | } |
Juergen Gross | b94e4b1 | 2021-07-30 12:38:54 +0200 | [diff] [blame] | 1548 | if (rinfo->shadow[id].status != REQ_WAITING) { |
| 1549 | pr_alert("%s: response references no pending request\n", |
| 1550 | info->gd->disk_name); |
| 1551 | goto err; |
| 1552 | } |
| 1553 | |
| 1554 | rinfo->shadow[id].status = REQ_PROCESSING; |
Bob Liu | 81f3516 | 2015-11-14 11:12:11 +0800 | [diff] [blame] | 1555 | req = rinfo->shadow[id].request; |
Jeremy Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 1556 | |
Juergen Gross | b94e4b1 | 2021-07-30 12:38:54 +0200 | [diff] [blame] | 1557 | op = rinfo->shadow[id].req.operation; |
| 1558 | if (op == BLKIF_OP_INDIRECT) |
| 1559 | op = rinfo->shadow[id].req.u.indirect.indirect_op; |
| 1560 | if (bret.operation != op) { |
| 1561 | pr_alert("%s: response has wrong operation (%u instead of %u)\n", |
| 1562 | info->gd->disk_name, bret.operation, op); |
| 1563 | goto err; |
| 1564 | } |
| 1565 | |
Juergen Gross | 71b6624 | 2021-07-30 12:38:52 +0200 | [diff] [blame] | 1566 | if (bret.operation != BLKIF_OP_DISCARD) { |
Julien Grall | 6cc56833 | 2015-08-13 13:13:35 +0100 | [diff] [blame] | 1567 | /* |
| 1568 | * We may need to wait for an extra response if the |
| 1569 | * I/O request is split in 2 |
| 1570 | */ |
Juergen Gross | 71b6624 | 2021-07-30 12:38:52 +0200 | [diff] [blame] | 1571 | if (!blkif_completion(&id, rinfo, &bret)) |
Julien Grall | 6cc56833 | 2015-08-13 13:13:35 +0100 | [diff] [blame] | 1572 | continue; |
| 1573 | } |
Jeremy Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 1574 | |
Bob Liu | 81f3516 | 2015-11-14 11:12:11 +0800 | [diff] [blame] | 1575 | if (add_id_to_freelist(rinfo, id)) { |
Konrad Rzeszutek Wilk | 6878c32 | 2012-05-25 17:34:51 -0400 | [diff] [blame] | 1576 | WARN(1, "%s: response to %s (id %ld) couldn't be recycled!\n", |
Juergen Gross | 71b6624 | 2021-07-30 12:38:52 +0200 | [diff] [blame] | 1577 | info->gd->disk_name, op_name(bret.operation), id); |
Konrad Rzeszutek Wilk | 6878c32 | 2012-05-25 17:34:51 -0400 | [diff] [blame] | 1578 | continue; |
| 1579 | } |
Jeremy Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 1580 | |
Juergen Gross | 71b6624 | 2021-07-30 12:38:52 +0200 | [diff] [blame] | 1581 | if (bret.status == BLKIF_RSP_OKAY) |
Christoph Hellwig | 2a842ac | 2017-06-03 09:38:04 +0200 | [diff] [blame] | 1582 | blkif_req(req)->error = BLK_STS_OK; |
| 1583 | else |
| 1584 | blkif_req(req)->error = BLK_STS_IOERR; |
| 1585 | |
Juergen Gross | 71b6624 | 2021-07-30 12:38:52 +0200 | [diff] [blame] | 1586 | switch (bret.operation) { |
Li Dongyang | ed30bf3 | 2011-09-01 18:39:09 +0800 | [diff] [blame] | 1587 | case BLKIF_OP_DISCARD: |
Juergen Gross | 71b6624 | 2021-07-30 12:38:52 +0200 | [diff] [blame] | 1588 | if (unlikely(bret.status == BLKIF_RSP_EOPNOTSUPP)) { |
Li Dongyang | ed30bf3 | 2011-09-01 18:39:09 +0800 | [diff] [blame] | 1589 | struct request_queue *rq = info->rq; |
Juergen Gross | b94e4b1 | 2021-07-30 12:38:54 +0200 | [diff] [blame] | 1590 | |
| 1591 | pr_warn_ratelimited("blkfront: %s: %s op failed\n", |
Juergen Gross | 71b6624 | 2021-07-30 12:38:52 +0200 | [diff] [blame] | 1592 | info->gd->disk_name, op_name(bret.operation)); |
Christoph Hellwig | 2a842ac | 2017-06-03 09:38:04 +0200 | [diff] [blame] | 1593 | blkif_req(req)->error = BLK_STS_NOTSUPP; |
Li Dongyang | ed30bf3 | 2011-09-01 18:39:09 +0800 | [diff] [blame] | 1594 | info->feature_discard = 0; |
Konrad Rzeszutek Wilk | 5ea4298 | 2011-10-12 16:23:30 -0400 | [diff] [blame] | 1595 | info->feature_secdiscard = 0; |
Bart Van Assche | 8b904b5 | 2018-03-07 17:10:10 -0800 | [diff] [blame] | 1596 | blk_queue_flag_clear(QUEUE_FLAG_DISCARD, rq); |
| 1597 | blk_queue_flag_clear(QUEUE_FLAG_SECERASE, rq); |
Li Dongyang | ed30bf3 | 2011-09-01 18:39:09 +0800 | [diff] [blame] | 1598 | } |
Li Dongyang | ed30bf3 | 2011-09-01 18:39:09 +0800 | [diff] [blame] | 1599 | break; |
Konrad Rzeszutek Wilk | edf6ef5 | 2011-05-03 12:01:11 -0400 | [diff] [blame] | 1600 | case BLKIF_OP_FLUSH_DISKCACHE: |
Jeremy Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 1601 | case BLKIF_OP_WRITE_BARRIER: |
Juergen Gross | 71b6624 | 2021-07-30 12:38:52 +0200 | [diff] [blame] | 1602 | if (unlikely(bret.status == BLKIF_RSP_EOPNOTSUPP)) { |
Juergen Gross | b94e4b1 | 2021-07-30 12:38:54 +0200 | [diff] [blame] | 1603 | pr_warn_ratelimited("blkfront: %s: %s op failed\n", |
Juergen Gross | 71b6624 | 2021-07-30 12:38:52 +0200 | [diff] [blame] | 1604 | info->gd->disk_name, op_name(bret.operation)); |
Bart Van Assche | 31c4ccc | 2017-07-21 19:11:10 +0200 | [diff] [blame] | 1605 | blkif_req(req)->error = BLK_STS_NOTSUPP; |
Jeremy Fitzhardinge | dcb8bae | 2010-11-02 11:55:58 -0400 | [diff] [blame] | 1606 | } |
Juergen Gross | 71b6624 | 2021-07-30 12:38:52 +0200 | [diff] [blame] | 1607 | if (unlikely(bret.status == BLKIF_RSP_ERROR && |
Bob Liu | 81f3516 | 2015-11-14 11:12:11 +0800 | [diff] [blame] | 1608 | rinfo->shadow[id].req.u.rw.nr_segments == 0)) { |
Juergen Gross | b94e4b1 | 2021-07-30 12:38:54 +0200 | [diff] [blame] | 1609 | pr_warn_ratelimited("blkfront: %s: empty %s op failed\n", |
Juergen Gross | 71b6624 | 2021-07-30 12:38:52 +0200 | [diff] [blame] | 1610 | info->gd->disk_name, op_name(bret.operation)); |
Christoph Hellwig | 2a842ac | 2017-06-03 09:38:04 +0200 | [diff] [blame] | 1611 | blkif_req(req)->error = BLK_STS_NOTSUPP; |
Jeremy Fitzhardinge | dcb8bae | 2010-11-02 11:55:58 -0400 | [diff] [blame] | 1612 | } |
Christoph Hellwig | 2609587 | 2017-04-20 16:03:08 +0200 | [diff] [blame] | 1613 | if (unlikely(blkif_req(req)->error)) { |
Christoph Hellwig | 2a842ac | 2017-06-03 09:38:04 +0200 | [diff] [blame] | 1614 | if (blkif_req(req)->error == BLK_STS_NOTSUPP) |
| 1615 | blkif_req(req)->error = BLK_STS_OK; |
Mike Christie | a418090 | 2016-06-05 14:32:24 -0500 | [diff] [blame] | 1616 | info->feature_fua = 0; |
Tejun Heo | 4913efe | 2010-09-03 11:56:16 +0200 | [diff] [blame] | 1617 | info->feature_flush = 0; |
| 1618 | xlvbd_flush(info); |
Jeremy Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 1619 | } |
Gustavo A. R. Silva | df561f66 | 2020-08-23 17:36:59 -0500 | [diff] [blame] | 1620 | fallthrough; |
Jeremy Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 1621 | case BLKIF_OP_READ: |
| 1622 | case BLKIF_OP_WRITE: |
Juergen Gross | 71b6624 | 2021-07-30 12:38:52 +0200 | [diff] [blame] | 1623 | if (unlikely(bret.status != BLKIF_RSP_OKAY)) |
Juergen Gross | b94e4b1 | 2021-07-30 12:38:54 +0200 | [diff] [blame] | 1624 | dev_dbg_ratelimited(&info->xbdev->dev, |
| 1625 | "Bad return from blkdev data request: %#x\n", |
| 1626 | bret.status); |
Jeremy Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 1627 | |
Jeremy Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 1628 | break; |
| 1629 | default: |
| 1630 | BUG(); |
| 1631 | } |
Christoph Hellwig | 2609587 | 2017-04-20 16:03:08 +0200 | [diff] [blame] | 1632 | |
Christoph Hellwig | 15f73f5 | 2020-06-11 08:44:47 +0200 | [diff] [blame] | 1633 | if (likely(!blk_should_fake_timeout(req->q))) |
| 1634 | blk_mq_complete_request(req); |
Jeremy Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 1635 | } |
| 1636 | |
Bob Liu | 81f3516 | 2015-11-14 11:12:11 +0800 | [diff] [blame] | 1637 | rinfo->ring.rsp_cons = i; |
Jeremy Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 1638 | |
Bob Liu | 81f3516 | 2015-11-14 11:12:11 +0800 | [diff] [blame] | 1639 | if (i != rinfo->ring.req_prod_pvt) { |
Jeremy Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 1640 | int more_to_do; |
Bob Liu | 81f3516 | 2015-11-14 11:12:11 +0800 | [diff] [blame] | 1641 | RING_FINAL_CHECK_FOR_RESPONSES(&rinfo->ring, more_to_do); |
Jeremy Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 1642 | if (more_to_do) |
| 1643 | goto again; |
| 1644 | } else |
Bob Liu | 81f3516 | 2015-11-14 11:12:11 +0800 | [diff] [blame] | 1645 | rinfo->ring.sring->rsp_event = i + 1; |
Jeremy Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 1646 | |
Bob Liu | 1165956 | 2015-11-14 11:12:13 +0800 | [diff] [blame] | 1647 | kick_pending_request_queues_locked(rinfo); |
Jeremy Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 1648 | |
Bob Liu | 1165956 | 2015-11-14 11:12:13 +0800 | [diff] [blame] | 1649 | spin_unlock_irqrestore(&rinfo->ring_lock, flags); |
Jeremy Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 1650 | |
Juergen Gross | 0fd08a3 | 2021-12-16 08:24:08 +0100 | [diff] [blame] | 1651 | xen_irq_lateeoi(irq, eoiflag); |
| 1652 | |
Jeremy Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 1653 | return IRQ_HANDLED; |
Juergen Gross | b94e4b1 | 2021-07-30 12:38:54 +0200 | [diff] [blame] | 1654 | |
| 1655 | err: |
| 1656 | info->connected = BLKIF_STATE_ERROR; |
| 1657 | |
| 1658 | spin_unlock_irqrestore(&rinfo->ring_lock, flags); |
| 1659 | |
Juergen Gross | 0fd08a3 | 2021-12-16 08:24:08 +0100 | [diff] [blame] | 1660 | /* No EOI in order to avoid further interrupts. */ |
| 1661 | |
Juergen Gross | b94e4b1 | 2021-07-30 12:38:54 +0200 | [diff] [blame] | 1662 | pr_alert("%s disabled for further use\n", info->gd->disk_name); |
| 1663 | return IRQ_HANDLED; |
Jeremy Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 1664 | } |
| 1665 | |
| 1666 | |
| 1667 | static int setup_blkring(struct xenbus_device *dev, |
Bob Liu | 81f3516 | 2015-11-14 11:12:11 +0800 | [diff] [blame] | 1668 | struct blkfront_ring_info *rinfo) |
Jeremy Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 1669 | { |
| 1670 | struct blkif_sring *sring; |
Bob Liu | 86839c5 | 2015-06-03 13:40:03 +0800 | [diff] [blame] | 1671 | int err, i; |
Bob Liu | 81f3516 | 2015-11-14 11:12:11 +0800 | [diff] [blame] | 1672 | struct blkfront_info *info = rinfo->dev_info; |
Julien Grall | c004a6f | 2015-07-22 16:44:54 +0100 | [diff] [blame] | 1673 | unsigned long ring_size = info->nr_ring_pages * XEN_PAGE_SIZE; |
Julien Grall | 9cce291 | 2015-10-13 17:50:11 +0100 | [diff] [blame] | 1674 | grant_ref_t gref[XENBUS_MAX_RING_GRANTS]; |
Jeremy Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 1675 | |
Bob Liu | 86839c5 | 2015-06-03 13:40:03 +0800 | [diff] [blame] | 1676 | for (i = 0; i < info->nr_ring_pages; i++) |
Bob Liu | 81f3516 | 2015-11-14 11:12:11 +0800 | [diff] [blame] | 1677 | rinfo->ring_ref[i] = GRANT_INVALID_REF; |
Jeremy Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 1678 | |
Bob Liu | 86839c5 | 2015-06-03 13:40:03 +0800 | [diff] [blame] | 1679 | sring = (struct blkif_sring *)__get_free_pages(GFP_NOIO | __GFP_HIGH, |
| 1680 | get_order(ring_size)); |
Jeremy Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 1681 | if (!sring) { |
| 1682 | xenbus_dev_fatal(dev, -ENOMEM, "allocating shared ring"); |
| 1683 | return -ENOMEM; |
| 1684 | } |
| 1685 | SHARED_RING_INIT(sring); |
Bob Liu | 81f3516 | 2015-11-14 11:12:11 +0800 | [diff] [blame] | 1686 | FRONT_RING_INIT(&rinfo->ring, sring, ring_size); |
Jeremy Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 1687 | |
Bob Liu | 81f3516 | 2015-11-14 11:12:11 +0800 | [diff] [blame] | 1688 | err = xenbus_grant_ring(dev, rinfo->ring.sring, info->nr_ring_pages, gref); |
Jeremy Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 1689 | if (err < 0) { |
Bob Liu | 86839c5 | 2015-06-03 13:40:03 +0800 | [diff] [blame] | 1690 | free_pages((unsigned long)sring, get_order(ring_size)); |
Bob Liu | 81f3516 | 2015-11-14 11:12:11 +0800 | [diff] [blame] | 1691 | rinfo->ring.sring = NULL; |
Jeremy Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 1692 | goto fail; |
| 1693 | } |
Bob Liu | 86839c5 | 2015-06-03 13:40:03 +0800 | [diff] [blame] | 1694 | for (i = 0; i < info->nr_ring_pages; i++) |
Bob Liu | 81f3516 | 2015-11-14 11:12:11 +0800 | [diff] [blame] | 1695 | rinfo->ring_ref[i] = gref[i]; |
Jeremy Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 1696 | |
Bob Liu | 81f3516 | 2015-11-14 11:12:11 +0800 | [diff] [blame] | 1697 | err = xenbus_alloc_evtchn(dev, &rinfo->evtchn); |
Jeremy Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 1698 | if (err) |
| 1699 | goto fail; |
| 1700 | |
Juergen Gross | 0fd08a3 | 2021-12-16 08:24:08 +0100 | [diff] [blame] | 1701 | err = bind_evtchn_to_irqhandler_lateeoi(rinfo->evtchn, blkif_interrupt, |
| 1702 | 0, "blkif", rinfo); |
Jeremy Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 1703 | if (err <= 0) { |
| 1704 | xenbus_dev_fatal(dev, err, |
| 1705 | "bind_evtchn_to_irqhandler failed"); |
| 1706 | goto fail; |
| 1707 | } |
Bob Liu | 81f3516 | 2015-11-14 11:12:11 +0800 | [diff] [blame] | 1708 | rinfo->irq = err; |
Jeremy Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 1709 | |
| 1710 | return 0; |
| 1711 | fail: |
| 1712 | blkif_free(info, 0); |
| 1713 | return err; |
| 1714 | } |
| 1715 | |
Bob Liu | 28d949b | 2015-11-14 11:12:14 +0800 | [diff] [blame] | 1716 | /* |
| 1717 | * Write out per-ring/queue nodes including ring-ref and event-channel, and each |
| 1718 | * ring buffer may have multi pages depending on ->nr_ring_pages. |
| 1719 | */ |
| 1720 | static int write_per_ring_nodes(struct xenbus_transaction xbt, |
| 1721 | struct blkfront_ring_info *rinfo, const char *dir) |
| 1722 | { |
| 1723 | int err; |
| 1724 | unsigned int i; |
| 1725 | const char *message = NULL; |
| 1726 | struct blkfront_info *info = rinfo->dev_info; |
| 1727 | |
| 1728 | if (info->nr_ring_pages == 1) { |
| 1729 | err = xenbus_printf(xbt, dir, "ring-ref", "%u", rinfo->ring_ref[0]); |
| 1730 | if (err) { |
| 1731 | message = "writing ring-ref"; |
| 1732 | goto abort_transaction; |
| 1733 | } |
| 1734 | } else { |
| 1735 | for (i = 0; i < info->nr_ring_pages; i++) { |
| 1736 | char ring_ref_name[RINGREF_NAME_LEN]; |
| 1737 | |
| 1738 | snprintf(ring_ref_name, RINGREF_NAME_LEN, "ring-ref%u", i); |
| 1739 | err = xenbus_printf(xbt, dir, ring_ref_name, |
| 1740 | "%u", rinfo->ring_ref[i]); |
| 1741 | if (err) { |
| 1742 | message = "writing ring-ref"; |
| 1743 | goto abort_transaction; |
| 1744 | } |
| 1745 | } |
| 1746 | } |
| 1747 | |
| 1748 | err = xenbus_printf(xbt, dir, "event-channel", "%u", rinfo->evtchn); |
| 1749 | if (err) { |
| 1750 | message = "writing event-channel"; |
| 1751 | goto abort_transaction; |
| 1752 | } |
| 1753 | |
| 1754 | return 0; |
| 1755 | |
| 1756 | abort_transaction: |
| 1757 | xenbus_transaction_end(xbt, 1); |
| 1758 | if (message) |
| 1759 | xenbus_dev_fatal(info->xbdev, err, "%s", message); |
| 1760 | |
| 1761 | return err; |
| 1762 | } |
Jeremy Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 1763 | |
| 1764 | /* Common code used when first setting up, and when resuming. */ |
Ian Campbell | 203fd61 | 2009-12-04 15:33:54 +0000 | [diff] [blame] | 1765 | static int talk_to_blkback(struct xenbus_device *dev, |
Jeremy Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 1766 | struct blkfront_info *info) |
| 1767 | { |
| 1768 | const char *message = NULL; |
| 1769 | struct xenbus_transaction xbt; |
Bob Liu | 28d949b | 2015-11-14 11:12:14 +0800 | [diff] [blame] | 1770 | int err; |
Juergen Gross | f27dc1a | 2016-10-31 14:58:40 +0100 | [diff] [blame] | 1771 | unsigned int i, max_page_order; |
| 1772 | unsigned int ring_page_order; |
Juergen Gross | 4ab50af | 2020-03-05 16:51:29 +0100 | [diff] [blame] | 1773 | struct blkfront_ring_info *rinfo; |
Bob Liu | 86839c5 | 2015-06-03 13:40:03 +0800 | [diff] [blame] | 1774 | |
Bhavesh Davda | 7ed8ce1 | 2017-12-22 14:17:13 -0800 | [diff] [blame] | 1775 | if (!info) |
| 1776 | return -ENODEV; |
| 1777 | |
Juergen Gross | f27dc1a | 2016-10-31 14:58:40 +0100 | [diff] [blame] | 1778 | max_page_order = xenbus_read_unsigned(info->xbdev->otherend, |
| 1779 | "max-ring-page-order", 0); |
| 1780 | ring_page_order = min(xen_blkif_max_ring_order, max_page_order); |
| 1781 | info->nr_ring_pages = 1 << ring_page_order; |
Jeremy Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 1782 | |
Bhavesh Davda | 7ed8ce1 | 2017-12-22 14:17:13 -0800 | [diff] [blame] | 1783 | err = negotiate_mq(info); |
| 1784 | if (err) |
| 1785 | goto destroy_blkring; |
| 1786 | |
Juergen Gross | 4ab50af | 2020-03-05 16:51:29 +0100 | [diff] [blame] | 1787 | for_each_rinfo(info, rinfo, i) { |
Bob Liu | 3df0e50 | 2015-11-14 11:12:12 +0800 | [diff] [blame] | 1788 | /* Create shared ring, alloc event channel. */ |
| 1789 | err = setup_blkring(dev, rinfo); |
| 1790 | if (err) |
| 1791 | goto destroy_blkring; |
| 1792 | } |
Jeremy Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 1793 | |
| 1794 | again: |
| 1795 | err = xenbus_transaction_start(&xbt); |
| 1796 | if (err) { |
| 1797 | xenbus_dev_fatal(dev, err, "starting transaction"); |
| 1798 | goto destroy_blkring; |
| 1799 | } |
| 1800 | |
Bob Liu | 28d949b | 2015-11-14 11:12:14 +0800 | [diff] [blame] | 1801 | if (info->nr_ring_pages > 1) { |
| 1802 | err = xenbus_printf(xbt, dev->nodename, "ring-page-order", "%u", |
| 1803 | ring_page_order); |
Bob Liu | 3df0e50 | 2015-11-14 11:12:12 +0800 | [diff] [blame] | 1804 | if (err) { |
Bob Liu | 28d949b | 2015-11-14 11:12:14 +0800 | [diff] [blame] | 1805 | message = "writing ring-page-order"; |
Bob Liu | 3df0e50 | 2015-11-14 11:12:12 +0800 | [diff] [blame] | 1806 | goto abort_transaction; |
| 1807 | } |
Bob Liu | 28d949b | 2015-11-14 11:12:14 +0800 | [diff] [blame] | 1808 | } |
| 1809 | |
| 1810 | /* We already got the number of queues/rings in _probe */ |
| 1811 | if (info->nr_rings == 1) { |
Juergen Gross | 4ab50af | 2020-03-05 16:51:29 +0100 | [diff] [blame] | 1812 | err = write_per_ring_nodes(xbt, info->rinfo, dev->nodename); |
Bob Liu | 28d949b | 2015-11-14 11:12:14 +0800 | [diff] [blame] | 1813 | if (err) |
| 1814 | goto destroy_blkring; |
Bob Liu | 3df0e50 | 2015-11-14 11:12:12 +0800 | [diff] [blame] | 1815 | } else { |
Bob Liu | 28d949b | 2015-11-14 11:12:14 +0800 | [diff] [blame] | 1816 | char *path; |
| 1817 | size_t pathsize; |
| 1818 | |
| 1819 | err = xenbus_printf(xbt, dev->nodename, "multi-queue-num-queues", "%u", |
| 1820 | info->nr_rings); |
| 1821 | if (err) { |
| 1822 | message = "writing multi-queue-num-queues"; |
| 1823 | goto abort_transaction; |
| 1824 | } |
| 1825 | |
| 1826 | pathsize = strlen(dev->nodename) + QUEUE_NAME_LEN; |
| 1827 | path = kmalloc(pathsize, GFP_KERNEL); |
| 1828 | if (!path) { |
| 1829 | err = -ENOMEM; |
| 1830 | message = "ENOMEM while writing ring references"; |
| 1831 | goto abort_transaction; |
| 1832 | } |
| 1833 | |
Juergen Gross | 4ab50af | 2020-03-05 16:51:29 +0100 | [diff] [blame] | 1834 | for_each_rinfo(info, rinfo, i) { |
Bob Liu | 28d949b | 2015-11-14 11:12:14 +0800 | [diff] [blame] | 1835 | memset(path, 0, pathsize); |
| 1836 | snprintf(path, pathsize, "%s/queue-%u", dev->nodename, i); |
Juergen Gross | 4ab50af | 2020-03-05 16:51:29 +0100 | [diff] [blame] | 1837 | err = write_per_ring_nodes(xbt, rinfo, path); |
Bob Liu | 28d949b | 2015-11-14 11:12:14 +0800 | [diff] [blame] | 1838 | if (err) { |
| 1839 | kfree(path); |
| 1840 | goto destroy_blkring; |
| 1841 | } |
| 1842 | } |
| 1843 | kfree(path); |
Jeremy Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 1844 | } |
Markus Armbruster | 3e33423 | 2008-04-02 10:54:02 -0700 | [diff] [blame] | 1845 | err = xenbus_printf(xbt, dev->nodename, "protocol", "%s", |
| 1846 | XEN_IO_PROTO_ABI_NATIVE); |
| 1847 | if (err) { |
| 1848 | message = "writing protocol"; |
| 1849 | goto abort_transaction; |
| 1850 | } |
SeongJae Park | 74a8524 | 2020-09-23 08:18:40 +0200 | [diff] [blame] | 1851 | err = xenbus_printf(xbt, dev->nodename, "feature-persistent", "%u", |
| 1852 | info->feature_persistent); |
Roger Pau Monne | 0a8704a | 2012-10-24 18:58:45 +0200 | [diff] [blame] | 1853 | if (err) |
| 1854 | dev_warn(&dev->dev, |
| 1855 | "writing persistent grants feature to xenbus"); |
Jeremy Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 1856 | |
| 1857 | err = xenbus_transaction_end(xbt, 0); |
| 1858 | if (err) { |
| 1859 | if (err == -EAGAIN) |
| 1860 | goto again; |
| 1861 | xenbus_dev_fatal(dev, err, "completing transaction"); |
| 1862 | goto destroy_blkring; |
| 1863 | } |
| 1864 | |
Juergen Gross | 4ab50af | 2020-03-05 16:51:29 +0100 | [diff] [blame] | 1865 | for_each_rinfo(info, rinfo, i) { |
Bob Liu | 3df0e50 | 2015-11-14 11:12:12 +0800 | [diff] [blame] | 1866 | unsigned int j; |
Bob Liu | 3df0e50 | 2015-11-14 11:12:12 +0800 | [diff] [blame] | 1867 | |
| 1868 | for (j = 0; j < BLK_RING_SIZE(info); j++) |
| 1869 | rinfo->shadow[j].req.u.rw.id = j + 1; |
| 1870 | rinfo->shadow[BLK_RING_SIZE(info)-1].req.u.rw.id = 0x0fffffff; |
| 1871 | } |
Jeremy Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 1872 | xenbus_switch_state(dev, XenbusStateInitialised); |
| 1873 | |
| 1874 | return 0; |
| 1875 | |
| 1876 | abort_transaction: |
| 1877 | xenbus_transaction_end(xbt, 1); |
| 1878 | if (message) |
| 1879 | xenbus_dev_fatal(dev, err, "%s", message); |
| 1880 | destroy_blkring: |
| 1881 | blkif_free(info, 0); |
Jeremy Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 1882 | return err; |
| 1883 | } |
| 1884 | |
Bob Liu | 3db70a8 | 2015-11-25 17:52:55 -0500 | [diff] [blame] | 1885 | static int negotiate_mq(struct blkfront_info *info) |
| 1886 | { |
Juergen Gross | f27dc1a | 2016-10-31 14:58:40 +0100 | [diff] [blame] | 1887 | unsigned int backend_max_queues; |
Bob Liu | 3db70a8 | 2015-11-25 17:52:55 -0500 | [diff] [blame] | 1888 | unsigned int i; |
Juergen Gross | 4ab50af | 2020-03-05 16:51:29 +0100 | [diff] [blame] | 1889 | struct blkfront_ring_info *rinfo; |
Bob Liu | 3db70a8 | 2015-11-25 17:52:55 -0500 | [diff] [blame] | 1890 | |
| 1891 | BUG_ON(info->nr_rings); |
| 1892 | |
| 1893 | /* Check if backend supports multiple queues. */ |
Juergen Gross | f27dc1a | 2016-10-31 14:58:40 +0100 | [diff] [blame] | 1894 | backend_max_queues = xenbus_read_unsigned(info->xbdev->otherend, |
| 1895 | "multi-queue-max-queues", 1); |
Bob Liu | 3db70a8 | 2015-11-25 17:52:55 -0500 | [diff] [blame] | 1896 | info->nr_rings = min(backend_max_queues, xen_blkif_max_queues); |
| 1897 | /* We need at least one ring. */ |
| 1898 | if (!info->nr_rings) |
| 1899 | info->nr_rings = 1; |
| 1900 | |
Juergen Gross | 4ab50af | 2020-03-05 16:51:29 +0100 | [diff] [blame] | 1901 | info->rinfo_size = struct_size(info->rinfo, shadow, |
| 1902 | BLK_RING_SIZE(info)); |
| 1903 | info->rinfo = kvcalloc(info->nr_rings, info->rinfo_size, GFP_KERNEL); |
Bob Liu | 3db70a8 | 2015-11-25 17:52:55 -0500 | [diff] [blame] | 1904 | if (!info->rinfo) { |
| 1905 | xenbus_dev_fatal(info->xbdev, -ENOMEM, "allocating ring_info structure"); |
Manjunath Patil | 6cc4a08 | 2018-10-30 09:49:21 -0700 | [diff] [blame] | 1906 | info->nr_rings = 0; |
Bob Liu | 3db70a8 | 2015-11-25 17:52:55 -0500 | [diff] [blame] | 1907 | return -ENOMEM; |
| 1908 | } |
| 1909 | |
Juergen Gross | 4ab50af | 2020-03-05 16:51:29 +0100 | [diff] [blame] | 1910 | for_each_rinfo(info, rinfo, i) { |
Bob Liu | 3db70a8 | 2015-11-25 17:52:55 -0500 | [diff] [blame] | 1911 | INIT_LIST_HEAD(&rinfo->indirect_pages); |
| 1912 | INIT_LIST_HEAD(&rinfo->grants); |
| 1913 | rinfo->dev_info = info; |
| 1914 | INIT_WORK(&rinfo->work, blkif_restart_queue); |
| 1915 | spin_lock_init(&rinfo->ring_lock); |
| 1916 | } |
| 1917 | return 0; |
| 1918 | } |
SeongJae Park | 74a8524 | 2020-09-23 08:18:40 +0200 | [diff] [blame] | 1919 | |
| 1920 | /* Enable the persistent grants feature. */ |
| 1921 | static bool feature_persistent = true; |
| 1922 | module_param(feature_persistent, bool, 0644); |
| 1923 | MODULE_PARM_DESC(feature_persistent, |
| 1924 | "Enables the persistent grants feature"); |
| 1925 | |
Lee Jones | 5fdbd5b | 2021-03-12 10:55:29 +0000 | [diff] [blame] | 1926 | /* |
Jeremy Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 1927 | * Entry point to this code when a new device is created. Allocate the basic |
| 1928 | * structures and the ring buffer for communication with the backend, and |
| 1929 | * inform the backend of the appropriate details for those. Switch to |
| 1930 | * Initialised state. |
| 1931 | */ |
| 1932 | static int blkfront_probe(struct xenbus_device *dev, |
| 1933 | const struct xenbus_device_id *id) |
| 1934 | { |
Bob Liu | 86839c5 | 2015-06-03 13:40:03 +0800 | [diff] [blame] | 1935 | int err, vdevice; |
Jeremy Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 1936 | struct blkfront_info *info; |
| 1937 | |
| 1938 | /* FIXME: Use dynamic device id if this is not set. */ |
| 1939 | err = xenbus_scanf(XBT_NIL, dev->nodename, |
| 1940 | "virtual-device", "%i", &vdevice); |
| 1941 | if (err != 1) { |
Chris Lalancette | 9246b5f | 2008-09-17 14:30:32 -0700 | [diff] [blame] | 1942 | /* go looking in the extended area instead */ |
| 1943 | err = xenbus_scanf(XBT_NIL, dev->nodename, "virtual-device-ext", |
| 1944 | "%i", &vdevice); |
| 1945 | if (err != 1) { |
| 1946 | xenbus_dev_fatal(dev, err, "reading virtual-device"); |
| 1947 | return err; |
| 1948 | } |
Jeremy Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 1949 | } |
| 1950 | |
Stefano Stabellini | b98a409 | 2010-07-29 14:53:16 +0100 | [diff] [blame] | 1951 | if (xen_hvm_domain()) { |
| 1952 | char *type; |
| 1953 | int len; |
| 1954 | /* no unplug has been done: do not hook devices != xen vbds */ |
Konrad Rzeszutek Wilk | 51c71a3 | 2013-11-26 15:05:40 -0500 | [diff] [blame] | 1955 | if (xen_has_pv_and_legacy_disk_devices()) { |
Stefano Stabellini | b98a409 | 2010-07-29 14:53:16 +0100 | [diff] [blame] | 1956 | int major; |
Stefano Stabellini | c1c5413 | 2010-05-14 12:44:30 +0100 | [diff] [blame] | 1957 | |
Stefano Stabellini | b98a409 | 2010-07-29 14:53:16 +0100 | [diff] [blame] | 1958 | if (!VDEV_IS_EXTENDED(vdevice)) |
| 1959 | major = BLKIF_MAJOR(vdevice); |
| 1960 | else |
| 1961 | major = XENVBD_MAJOR; |
Stefano Stabellini | c1c5413 | 2010-05-14 12:44:30 +0100 | [diff] [blame] | 1962 | |
Stefano Stabellini | b98a409 | 2010-07-29 14:53:16 +0100 | [diff] [blame] | 1963 | if (major != XENVBD_MAJOR) { |
| 1964 | printk(KERN_INFO |
| 1965 | "%s: HVM does not support vbd %d as xen block device\n", |
Rasmus Villemoes | 02f1f21 | 2015-02-12 15:01:31 -0800 | [diff] [blame] | 1966 | __func__, vdevice); |
Stefano Stabellini | b98a409 | 2010-07-29 14:53:16 +0100 | [diff] [blame] | 1967 | return -ENODEV; |
| 1968 | } |
| 1969 | } |
| 1970 | /* do not create a PV cdrom device if we are an HVM guest */ |
| 1971 | type = xenbus_read(XBT_NIL, dev->nodename, "device-type", &len); |
| 1972 | if (IS_ERR(type)) |
| 1973 | return -ENODEV; |
| 1974 | if (strncmp(type, "cdrom", 5) == 0) { |
| 1975 | kfree(type); |
Stefano Stabellini | c1c5413 | 2010-05-14 12:44:30 +0100 | [diff] [blame] | 1976 | return -ENODEV; |
| 1977 | } |
Stefano Stabellini | b98a409 | 2010-07-29 14:53:16 +0100 | [diff] [blame] | 1978 | kfree(type); |
Stefano Stabellini | c1c5413 | 2010-05-14 12:44:30 +0100 | [diff] [blame] | 1979 | } |
Jeremy Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 1980 | info = kzalloc(sizeof(*info), GFP_KERNEL); |
| 1981 | if (!info) { |
| 1982 | xenbus_dev_fatal(dev, -ENOMEM, "allocating info structure"); |
| 1983 | return -ENOMEM; |
| 1984 | } |
| 1985 | |
Bob Liu | 28d949b | 2015-11-14 11:12:14 +0800 | [diff] [blame] | 1986 | info->xbdev = dev; |
Bob Liu | 81f3516 | 2015-11-14 11:12:11 +0800 | [diff] [blame] | 1987 | |
Daniel Stodden | b70f5fa | 2010-04-30 22:01:19 +0000 | [diff] [blame] | 1988 | mutex_init(&info->mutex); |
Jeremy Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 1989 | info->vdevice = vdevice; |
| 1990 | info->connected = BLKIF_STATE_DISCONNECTED; |
Jeremy Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 1991 | |
SeongJae Park | 74a8524 | 2020-09-23 08:18:40 +0200 | [diff] [blame] | 1992 | info->feature_persistent = feature_persistent; |
| 1993 | |
Jeremy Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 1994 | /* Front end dir is a number, which is used as the id. */ |
| 1995 | info->handle = simple_strtoul(strrchr(dev->nodename, '/')+1, NULL, 0); |
Greg Kroah-Hartman | a1b4b12 | 2009-04-30 14:43:31 -0700 | [diff] [blame] | 1996 | dev_set_drvdata(&dev->dev, info); |
Jeremy Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 1997 | |
Juergen Gross | a46b536 | 2018-08-13 16:01:11 +0200 | [diff] [blame] | 1998 | mutex_lock(&blkfront_mutex); |
| 1999 | list_add(&info->info_list, &info_list); |
| 2000 | mutex_unlock(&blkfront_mutex); |
| 2001 | |
Jeremy Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 2002 | return 0; |
| 2003 | } |
| 2004 | |
Jeremy Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 2005 | static int blkif_recover(struct blkfront_info *info) |
| 2006 | { |
NeilBrown | 4559fa5 | 2017-06-18 14:38:59 +1000 | [diff] [blame] | 2007 | unsigned int r_index; |
Roger Pau Monne | 402b27f | 2013-04-18 16:06:54 +0200 | [diff] [blame] | 2008 | struct request *req, *n; |
Roger Pau Monne | 402b27f | 2013-04-18 16:06:54 +0200 | [diff] [blame] | 2009 | int rc; |
NeilBrown | 4559fa5 | 2017-06-18 14:38:59 +1000 | [diff] [blame] | 2010 | struct bio *bio; |
| 2011 | unsigned int segs; |
Juergen Gross | 4ab50af | 2020-03-05 16:51:29 +0100 | [diff] [blame] | 2012 | struct blkfront_ring_info *rinfo; |
Jeremy Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 2013 | |
Bob Liu | 3df0e50 | 2015-11-14 11:12:12 +0800 | [diff] [blame] | 2014 | blkfront_gather_backend_features(info); |
Bob Liu | 172335a | 2016-07-01 17:43:39 -0400 | [diff] [blame] | 2015 | /* Reset limits changed by blk_mq_update_nr_hw_queues(). */ |
| 2016 | blkif_set_queue_limits(info); |
Roger Pau Monne | 402b27f | 2013-04-18 16:06:54 +0200 | [diff] [blame] | 2017 | segs = info->max_indirect_segments ? : BLKIF_MAX_SEGMENTS_PER_REQUEST; |
Bob Liu | 6c647b0 | 2016-07-01 15:45:57 -0400 | [diff] [blame] | 2018 | blk_queue_max_segments(info->rq, segs / GRANTS_PER_PSEG); |
Jeremy Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 2019 | |
Juergen Gross | 4ab50af | 2020-03-05 16:51:29 +0100 | [diff] [blame] | 2020 | for_each_rinfo(info, rinfo, r_index) { |
Bob Liu | 3df0e50 | 2015-11-14 11:12:12 +0800 | [diff] [blame] | 2021 | rc = blkfront_setup_indirect(rinfo); |
Bob Liu | 7b427a5 | 2016-06-27 16:33:24 +0800 | [diff] [blame] | 2022 | if (rc) |
Bob Liu | 3df0e50 | 2015-11-14 11:12:12 +0800 | [diff] [blame] | 2023 | return rc; |
Jeremy Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 2024 | } |
Jeremy Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 2025 | xenbus_switch_state(info->xbdev, XenbusStateConnected); |
| 2026 | |
Jeremy Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 2027 | /* Now safe for us to use the shared ring */ |
| 2028 | info->connected = BLKIF_STATE_CONNECTED; |
| 2029 | |
Juergen Gross | 4ab50af | 2020-03-05 16:51:29 +0100 | [diff] [blame] | 2030 | for_each_rinfo(info, rinfo, r_index) { |
Bob Liu | 3df0e50 | 2015-11-14 11:12:12 +0800 | [diff] [blame] | 2031 | /* Kick any other new requests queued since we resumed */ |
| 2032 | kick_pending_request_queues(rinfo); |
| 2033 | } |
Jeremy Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 2034 | |
Bob Liu | 7b427a5 | 2016-06-27 16:33:24 +0800 | [diff] [blame] | 2035 | list_for_each_entry_safe(req, n, &info->requests, queuelist) { |
Roger Pau Monne | 402b27f | 2013-04-18 16:06:54 +0200 | [diff] [blame] | 2036 | /* Requeue pending requests (flush or discard) */ |
| 2037 | list_del_init(&req->queuelist); |
| 2038 | BUG_ON(req->nr_phys_segments > segs); |
Bart Van Assche | 2b053ac | 2016-10-28 17:21:41 -0700 | [diff] [blame] | 2039 | blk_mq_requeue_request(req, false); |
Roger Pau Monne | 402b27f | 2013-04-18 16:06:54 +0200 | [diff] [blame] | 2040 | } |
Bart Van Assche | 52d7f1b | 2016-10-28 17:20:32 -0700 | [diff] [blame] | 2041 | blk_mq_start_stopped_hw_queues(info->rq, true); |
Bob Liu | 907c3eb | 2015-07-13 17:55:24 +0800 | [diff] [blame] | 2042 | blk_mq_kick_requeue_list(info->rq); |
Jeremy Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 2043 | |
Bob Liu | 7b427a5 | 2016-06-27 16:33:24 +0800 | [diff] [blame] | 2044 | while ((bio = bio_list_pop(&info->bio_list)) != NULL) { |
Roger Pau Monne | 402b27f | 2013-04-18 16:06:54 +0200 | [diff] [blame] | 2045 | /* Traverse the list of pending bios and re-queue them */ |
Mike Christie | 4e49ea4 | 2016-06-05 14:31:41 -0500 | [diff] [blame] | 2046 | submit_bio(bio); |
Roger Pau Monne | 402b27f | 2013-04-18 16:06:54 +0200 | [diff] [blame] | 2047 | } |
| 2048 | |
Jeremy Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 2049 | return 0; |
| 2050 | } |
| 2051 | |
Lee Jones | 5fdbd5b | 2021-03-12 10:55:29 +0000 | [diff] [blame] | 2052 | /* |
Jeremy Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 2053 | * We are reconnecting to the backend, due to a suspend/resume, or a backend |
| 2054 | * driver restart. We tear down our blkif structure and recreate it, but |
| 2055 | * leave the device-layer structures intact so that this is transparent to the |
| 2056 | * rest of the kernel. |
| 2057 | */ |
| 2058 | static int blkfront_resume(struct xenbus_device *dev) |
| 2059 | { |
Greg Kroah-Hartman | a1b4b12 | 2009-04-30 14:43:31 -0700 | [diff] [blame] | 2060 | struct blkfront_info *info = dev_get_drvdata(&dev->dev); |
Bob Liu | 3db70a8 | 2015-11-25 17:52:55 -0500 | [diff] [blame] | 2061 | int err = 0; |
Bob Liu | 7b427a5 | 2016-06-27 16:33:24 +0800 | [diff] [blame] | 2062 | unsigned int i, j; |
Juergen Gross | 4ab50af | 2020-03-05 16:51:29 +0100 | [diff] [blame] | 2063 | struct blkfront_ring_info *rinfo; |
Jeremy Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 2064 | |
| 2065 | dev_dbg(&dev->dev, "blkfront_resume: %s\n", dev->nodename); |
| 2066 | |
Bob Liu | 7b427a5 | 2016-06-27 16:33:24 +0800 | [diff] [blame] | 2067 | bio_list_init(&info->bio_list); |
| 2068 | INIT_LIST_HEAD(&info->requests); |
Juergen Gross | 4ab50af | 2020-03-05 16:51:29 +0100 | [diff] [blame] | 2069 | for_each_rinfo(info, rinfo, i) { |
Bob Liu | 7b427a5 | 2016-06-27 16:33:24 +0800 | [diff] [blame] | 2070 | struct bio_list merge_bio; |
| 2071 | struct blk_shadow *shadow = rinfo->shadow; |
| 2072 | |
| 2073 | for (j = 0; j < BLK_RING_SIZE(info); j++) { |
| 2074 | /* Not in use? */ |
| 2075 | if (!shadow[j].request) |
| 2076 | continue; |
| 2077 | |
| 2078 | /* |
| 2079 | * Get the bios in the request so we can re-queue them. |
| 2080 | */ |
Munehisa Kamata | b15bd8c | 2017-08-09 15:31:40 -0700 | [diff] [blame] | 2081 | if (req_op(shadow[j].request) == REQ_OP_FLUSH || |
| 2082 | req_op(shadow[j].request) == REQ_OP_DISCARD || |
| 2083 | req_op(shadow[j].request) == REQ_OP_SECURE_ERASE || |
Linus Torvalds | 3fc9d69 | 2016-07-26 15:37:51 -0700 | [diff] [blame] | 2084 | shadow[j].request->cmd_flags & REQ_FUA) { |
Bob Liu | 7b427a5 | 2016-06-27 16:33:24 +0800 | [diff] [blame] | 2085 | /* |
| 2086 | * Flush operations don't contain bios, so |
| 2087 | * we need to requeue the whole request |
Linus Torvalds | 3fc9d69 | 2016-07-26 15:37:51 -0700 | [diff] [blame] | 2088 | * |
| 2089 | * XXX: but this doesn't make any sense for a |
| 2090 | * write with the FUA flag set.. |
Bob Liu | 7b427a5 | 2016-06-27 16:33:24 +0800 | [diff] [blame] | 2091 | */ |
| 2092 | list_add(&shadow[j].request->queuelist, &info->requests); |
| 2093 | continue; |
| 2094 | } |
| 2095 | merge_bio.head = shadow[j].request->bio; |
| 2096 | merge_bio.tail = shadow[j].request->biotail; |
| 2097 | bio_list_merge(&info->bio_list, &merge_bio); |
| 2098 | shadow[j].request->bio = NULL; |
Christoph Hellwig | 2a842ac | 2017-06-03 09:38:04 +0200 | [diff] [blame] | 2099 | blk_mq_end_request(shadow[j].request, BLK_STS_OK); |
Bob Liu | 7b427a5 | 2016-06-27 16:33:24 +0800 | [diff] [blame] | 2100 | } |
| 2101 | } |
| 2102 | |
Jeremy Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 2103 | blkif_free(info, info->connected == BLKIF_STATE_CONNECTED); |
| 2104 | |
Ian Campbell | 203fd61 | 2009-12-04 15:33:54 +0000 | [diff] [blame] | 2105 | err = talk_to_blkback(dev, info); |
Bob Liu | 2a6f71a | 2016-05-31 16:59:17 +0800 | [diff] [blame] | 2106 | if (!err) |
| 2107 | blk_mq_update_nr_hw_queues(&info->tag_set, info->nr_rings); |
Roger Pau Monne | 402b27f | 2013-04-18 16:06:54 +0200 | [diff] [blame] | 2108 | |
| 2109 | /* |
| 2110 | * We have to wait for the backend to switch to |
| 2111 | * connected state, since we want to read which |
| 2112 | * features it supports. |
| 2113 | */ |
Jeremy Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 2114 | |
| 2115 | return err; |
| 2116 | } |
| 2117 | |
Konrad Rzeszutek Wilk | 6f03a7f | 2015-11-16 15:14:41 -0500 | [diff] [blame] | 2118 | static void blkfront_closing(struct blkfront_info *info) |
Daniel Stodden | b70f5fa | 2010-04-30 22:01:19 +0000 | [diff] [blame] | 2119 | { |
| 2120 | struct xenbus_device *xbdev = info->xbdev; |
Christoph Hellwig | 05d69d9 | 2021-07-15 16:17:11 +0200 | [diff] [blame] | 2121 | struct blkfront_ring_info *rinfo; |
| 2122 | unsigned int i; |
Daniel Stodden | b70f5fa | 2010-04-30 22:01:19 +0000 | [diff] [blame] | 2123 | |
Christoph Hellwig | 05d69d9 | 2021-07-15 16:17:11 +0200 | [diff] [blame] | 2124 | if (xbdev->state == XenbusStateClosing) |
Daniel Stodden | b70f5fa | 2010-04-30 22:01:19 +0000 | [diff] [blame] | 2125 | return; |
Christoph Hellwig | 05d69d9 | 2021-07-15 16:17:11 +0200 | [diff] [blame] | 2126 | |
| 2127 | /* No more blkif_request(). */ |
| 2128 | blk_mq_stop_hw_queues(info->rq); |
| 2129 | blk_set_queue_dying(info->rq); |
| 2130 | set_capacity(info->gd, 0); |
| 2131 | |
| 2132 | for_each_rinfo(info, rinfo, i) { |
| 2133 | /* No more gnttab callback work. */ |
| 2134 | gnttab_cancel_free_callback(&rinfo->callback); |
| 2135 | |
| 2136 | /* Flush gnttab callback work. Must be done with no locks held. */ |
| 2137 | flush_work(&rinfo->work); |
Daniel Stodden | b70f5fa | 2010-04-30 22:01:19 +0000 | [diff] [blame] | 2138 | } |
| 2139 | |
Christoph Hellwig | 05d69d9 | 2021-07-15 16:17:11 +0200 | [diff] [blame] | 2140 | xenbus_frontend_closed(xbdev); |
Daniel Stodden | b70f5fa | 2010-04-30 22:01:19 +0000 | [diff] [blame] | 2141 | } |
Jeremy Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 2142 | |
Li Dongyang | ed30bf3 | 2011-09-01 18:39:09 +0800 | [diff] [blame] | 2143 | static void blkfront_setup_discard(struct blkfront_info *info) |
| 2144 | { |
Olaf Hering | 1c8cad6 | 2014-05-21 16:32:40 +0200 | [diff] [blame] | 2145 | info->feature_discard = 1; |
Roger Pau Monne | 0549cd6 | 2021-01-19 11:57:27 +0100 | [diff] [blame] | 2146 | info->discard_granularity = xenbus_read_unsigned(info->xbdev->otherend, |
| 2147 | "discard-granularity", |
| 2148 | 0); |
| 2149 | info->discard_alignment = xenbus_read_unsigned(info->xbdev->otherend, |
| 2150 | "discard-alignment", 0); |
Juergen Gross | f27dc1a | 2016-10-31 14:58:40 +0100 | [diff] [blame] | 2151 | info->feature_secdiscard = |
| 2152 | !!xenbus_read_unsigned(info->xbdev->otherend, "discard-secure", |
| 2153 | 0); |
Li Dongyang | ed30bf3 | 2011-09-01 18:39:09 +0800 | [diff] [blame] | 2154 | } |
| 2155 | |
Bob Liu | 81f3516 | 2015-11-14 11:12:11 +0800 | [diff] [blame] | 2156 | static int blkfront_setup_indirect(struct blkfront_ring_info *rinfo) |
Roger Pau Monne | 402b27f | 2013-04-18 16:06:54 +0200 | [diff] [blame] | 2157 | { |
Juergen Gross | 3a169c0 | 2020-04-03 11:00:34 +0200 | [diff] [blame] | 2158 | unsigned int psegs, grants, memflags; |
Roger Pau Monne | 402b27f | 2013-04-18 16:06:54 +0200 | [diff] [blame] | 2159 | int err, i; |
Bob Liu | 81f3516 | 2015-11-14 11:12:11 +0800 | [diff] [blame] | 2160 | struct blkfront_info *info = rinfo->dev_info; |
Roger Pau Monne | 402b27f | 2013-04-18 16:06:54 +0200 | [diff] [blame] | 2161 | |
Juergen Gross | 3a169c0 | 2020-04-03 11:00:34 +0200 | [diff] [blame] | 2162 | memflags = memalloc_noio_save(); |
| 2163 | |
Julien Grall | 6cc56833 | 2015-08-13 13:13:35 +0100 | [diff] [blame] | 2164 | if (info->max_indirect_segments == 0) { |
| 2165 | if (!HAS_EXTRA_REQ) |
| 2166 | grants = BLKIF_MAX_SEGMENTS_PER_REQUEST; |
| 2167 | else { |
| 2168 | /* |
| 2169 | * When an extra req is required, the maximum |
| 2170 | * grants supported is related to the size of the |
| 2171 | * Linux block segment. |
| 2172 | */ |
| 2173 | grants = GRANTS_PER_PSEG; |
| 2174 | } |
| 2175 | } |
Bob Liu | d50babb | 2015-07-22 14:40:08 +0800 | [diff] [blame] | 2176 | else |
Julien Grall | c004a6f | 2015-07-22 16:44:54 +0100 | [diff] [blame] | 2177 | grants = info->max_indirect_segments; |
Jan Beulich | 3b4f188 | 2017-01-23 08:11:37 -0700 | [diff] [blame] | 2178 | psegs = DIV_ROUND_UP(grants, GRANTS_PER_PSEG); |
Roger Pau Monne | 402b27f | 2013-04-18 16:06:54 +0200 | [diff] [blame] | 2179 | |
Bob Liu | 81f3516 | 2015-11-14 11:12:11 +0800 | [diff] [blame] | 2180 | err = fill_grant_buffer(rinfo, |
Julien Grall | c004a6f | 2015-07-22 16:44:54 +0100 | [diff] [blame] | 2181 | (grants + INDIRECT_GREFS(grants)) * BLK_RING_SIZE(info)); |
Roger Pau Monne | 402b27f | 2013-04-18 16:06:54 +0200 | [diff] [blame] | 2182 | if (err) |
| 2183 | goto out_of_memory; |
| 2184 | |
Roger Pau Monne | bfe11d6 | 2013-10-29 18:31:14 +0100 | [diff] [blame] | 2185 | if (!info->feature_persistent && info->max_indirect_segments) { |
| 2186 | /* |
| 2187 | * We are using indirect descriptors but not persistent |
| 2188 | * grants, we need to allocate a set of pages that can be |
| 2189 | * used for mapping indirect grefs |
| 2190 | */ |
Julien Grall | c004a6f | 2015-07-22 16:44:54 +0100 | [diff] [blame] | 2191 | int num = INDIRECT_GREFS(grants) * BLK_RING_SIZE(info); |
Roger Pau Monne | bfe11d6 | 2013-10-29 18:31:14 +0100 | [diff] [blame] | 2192 | |
Bob Liu | 81f3516 | 2015-11-14 11:12:11 +0800 | [diff] [blame] | 2193 | BUG_ON(!list_empty(&rinfo->indirect_pages)); |
Roger Pau Monne | bfe11d6 | 2013-10-29 18:31:14 +0100 | [diff] [blame] | 2194 | for (i = 0; i < num; i++) { |
Juergen Gross | 3a169c0 | 2020-04-03 11:00:34 +0200 | [diff] [blame] | 2195 | struct page *indirect_page = alloc_page(GFP_KERNEL); |
Roger Pau Monne | bfe11d6 | 2013-10-29 18:31:14 +0100 | [diff] [blame] | 2196 | if (!indirect_page) |
| 2197 | goto out_of_memory; |
Bob Liu | 81f3516 | 2015-11-14 11:12:11 +0800 | [diff] [blame] | 2198 | list_add(&indirect_page->lru, &rinfo->indirect_pages); |
Roger Pau Monne | bfe11d6 | 2013-10-29 18:31:14 +0100 | [diff] [blame] | 2199 | } |
| 2200 | } |
| 2201 | |
Bob Liu | 86839c5 | 2015-06-03 13:40:03 +0800 | [diff] [blame] | 2202 | for (i = 0; i < BLK_RING_SIZE(info); i++) { |
Kees Cook | 6396bb2 | 2018-06-12 14:03:40 -0700 | [diff] [blame] | 2203 | rinfo->shadow[i].grants_used = |
Roger Pau Monne | 1d5c76e | 2019-05-03 17:04:01 +0200 | [diff] [blame] | 2204 | kvcalloc(grants, |
| 2205 | sizeof(rinfo->shadow[i].grants_used[0]), |
Juergen Gross | 3a169c0 | 2020-04-03 11:00:34 +0200 | [diff] [blame] | 2206 | GFP_KERNEL); |
Roger Pau Monne | 1d5c76e | 2019-05-03 17:04:01 +0200 | [diff] [blame] | 2207 | rinfo->shadow[i].sg = kvcalloc(psegs, |
| 2208 | sizeof(rinfo->shadow[i].sg[0]), |
Juergen Gross | 3a169c0 | 2020-04-03 11:00:34 +0200 | [diff] [blame] | 2209 | GFP_KERNEL); |
Kees Cook | 6396bb2 | 2018-06-12 14:03:40 -0700 | [diff] [blame] | 2210 | if (info->max_indirect_segments) |
| 2211 | rinfo->shadow[i].indirect_grants = |
Roger Pau Monne | 1d5c76e | 2019-05-03 17:04:01 +0200 | [diff] [blame] | 2212 | kvcalloc(INDIRECT_GREFS(grants), |
| 2213 | sizeof(rinfo->shadow[i].indirect_grants[0]), |
Juergen Gross | 3a169c0 | 2020-04-03 11:00:34 +0200 | [diff] [blame] | 2214 | GFP_KERNEL); |
Bob Liu | 81f3516 | 2015-11-14 11:12:11 +0800 | [diff] [blame] | 2215 | if ((rinfo->shadow[i].grants_used == NULL) || |
| 2216 | (rinfo->shadow[i].sg == NULL) || |
Roger Pau Monne | 402b27f | 2013-04-18 16:06:54 +0200 | [diff] [blame] | 2217 | (info->max_indirect_segments && |
Bob Liu | 81f3516 | 2015-11-14 11:12:11 +0800 | [diff] [blame] | 2218 | (rinfo->shadow[i].indirect_grants == NULL))) |
Roger Pau Monne | 402b27f | 2013-04-18 16:06:54 +0200 | [diff] [blame] | 2219 | goto out_of_memory; |
Bob Liu | 81f3516 | 2015-11-14 11:12:11 +0800 | [diff] [blame] | 2220 | sg_init_table(rinfo->shadow[i].sg, psegs); |
Roger Pau Monne | 402b27f | 2013-04-18 16:06:54 +0200 | [diff] [blame] | 2221 | } |
| 2222 | |
Juergen Gross | 3a169c0 | 2020-04-03 11:00:34 +0200 | [diff] [blame] | 2223 | memalloc_noio_restore(memflags); |
Roger Pau Monne | 402b27f | 2013-04-18 16:06:54 +0200 | [diff] [blame] | 2224 | |
| 2225 | return 0; |
| 2226 | |
| 2227 | out_of_memory: |
Bob Liu | 86839c5 | 2015-06-03 13:40:03 +0800 | [diff] [blame] | 2228 | for (i = 0; i < BLK_RING_SIZE(info); i++) { |
Roger Pau Monne | 1d5c76e | 2019-05-03 17:04:01 +0200 | [diff] [blame] | 2229 | kvfree(rinfo->shadow[i].grants_used); |
Bob Liu | 81f3516 | 2015-11-14 11:12:11 +0800 | [diff] [blame] | 2230 | rinfo->shadow[i].grants_used = NULL; |
Roger Pau Monne | 1d5c76e | 2019-05-03 17:04:01 +0200 | [diff] [blame] | 2231 | kvfree(rinfo->shadow[i].sg); |
Bob Liu | 81f3516 | 2015-11-14 11:12:11 +0800 | [diff] [blame] | 2232 | rinfo->shadow[i].sg = NULL; |
Roger Pau Monne | 1d5c76e | 2019-05-03 17:04:01 +0200 | [diff] [blame] | 2233 | kvfree(rinfo->shadow[i].indirect_grants); |
Bob Liu | 81f3516 | 2015-11-14 11:12:11 +0800 | [diff] [blame] | 2234 | rinfo->shadow[i].indirect_grants = NULL; |
Roger Pau Monne | 402b27f | 2013-04-18 16:06:54 +0200 | [diff] [blame] | 2235 | } |
Bob Liu | 81f3516 | 2015-11-14 11:12:11 +0800 | [diff] [blame] | 2236 | if (!list_empty(&rinfo->indirect_pages)) { |
Roger Pau Monne | bfe11d6 | 2013-10-29 18:31:14 +0100 | [diff] [blame] | 2237 | struct page *indirect_page, *n; |
Bob Liu | 81f3516 | 2015-11-14 11:12:11 +0800 | [diff] [blame] | 2238 | list_for_each_entry_safe(indirect_page, n, &rinfo->indirect_pages, lru) { |
Roger Pau Monne | bfe11d6 | 2013-10-29 18:31:14 +0100 | [diff] [blame] | 2239 | list_del(&indirect_page->lru); |
| 2240 | __free_page(indirect_page); |
| 2241 | } |
| 2242 | } |
Juergen Gross | 3a169c0 | 2020-04-03 11:00:34 +0200 | [diff] [blame] | 2243 | |
| 2244 | memalloc_noio_restore(memflags); |
| 2245 | |
Roger Pau Monne | 402b27f | 2013-04-18 16:06:54 +0200 | [diff] [blame] | 2246 | return -ENOMEM; |
| 2247 | } |
| 2248 | |
Jeremy Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 2249 | /* |
Bob Liu | d50babb | 2015-07-22 14:40:08 +0800 | [diff] [blame] | 2250 | * Gather all backend feature-* |
| 2251 | */ |
Bob Liu | 3df0e50 | 2015-11-14 11:12:12 +0800 | [diff] [blame] | 2252 | static void blkfront_gather_backend_features(struct blkfront_info *info) |
Bob Liu | d50babb | 2015-07-22 14:40:08 +0800 | [diff] [blame] | 2253 | { |
Bob Liu | d50babb | 2015-07-22 14:40:08 +0800 | [diff] [blame] | 2254 | unsigned int indirect_segments; |
| 2255 | |
| 2256 | info->feature_flush = 0; |
Mike Christie | a418090 | 2016-06-05 14:32:24 -0500 | [diff] [blame] | 2257 | info->feature_fua = 0; |
Bob Liu | d50babb | 2015-07-22 14:40:08 +0800 | [diff] [blame] | 2258 | |
Bob Liu | d50babb | 2015-07-22 14:40:08 +0800 | [diff] [blame] | 2259 | /* |
| 2260 | * If there's no "feature-barrier" defined, then it means |
| 2261 | * we're dealing with a very old backend which writes |
| 2262 | * synchronously; nothing to do. |
| 2263 | * |
| 2264 | * If there are barriers, then we use flush. |
| 2265 | */ |
Juergen Gross | f27dc1a | 2016-10-31 14:58:40 +0100 | [diff] [blame] | 2266 | if (xenbus_read_unsigned(info->xbdev->otherend, "feature-barrier", 0)) { |
Mike Christie | a418090 | 2016-06-05 14:32:24 -0500 | [diff] [blame] | 2267 | info->feature_flush = 1; |
| 2268 | info->feature_fua = 1; |
| 2269 | } |
| 2270 | |
Bob Liu | d50babb | 2015-07-22 14:40:08 +0800 | [diff] [blame] | 2271 | /* |
| 2272 | * And if there is "feature-flush-cache" use that above |
| 2273 | * barriers. |
| 2274 | */ |
Juergen Gross | f27dc1a | 2016-10-31 14:58:40 +0100 | [diff] [blame] | 2275 | if (xenbus_read_unsigned(info->xbdev->otherend, "feature-flush-cache", |
| 2276 | 0)) { |
Mike Christie | a418090 | 2016-06-05 14:32:24 -0500 | [diff] [blame] | 2277 | info->feature_flush = 1; |
| 2278 | info->feature_fua = 0; |
| 2279 | } |
Bob Liu | d50babb | 2015-07-22 14:40:08 +0800 | [diff] [blame] | 2280 | |
Juergen Gross | f27dc1a | 2016-10-31 14:58:40 +0100 | [diff] [blame] | 2281 | if (xenbus_read_unsigned(info->xbdev->otherend, "feature-discard", 0)) |
Bob Liu | d50babb | 2015-07-22 14:40:08 +0800 | [diff] [blame] | 2282 | blkfront_setup_discard(info); |
| 2283 | |
SeongJae Park | 74a8524 | 2020-09-23 08:18:40 +0200 | [diff] [blame] | 2284 | if (info->feature_persistent) |
| 2285 | info->feature_persistent = |
| 2286 | !!xenbus_read_unsigned(info->xbdev->otherend, |
| 2287 | "feature-persistent", 0); |
Bob Liu | d50babb | 2015-07-22 14:40:08 +0800 | [diff] [blame] | 2288 | |
Juergen Gross | f27dc1a | 2016-10-31 14:58:40 +0100 | [diff] [blame] | 2289 | indirect_segments = xenbus_read_unsigned(info->xbdev->otherend, |
| 2290 | "feature-max-indirect-segments", 0); |
Jan Beulich | 3b4f188 | 2017-01-23 08:11:37 -0700 | [diff] [blame] | 2291 | if (indirect_segments > xen_blkif_max_segments) |
| 2292 | indirect_segments = xen_blkif_max_segments; |
| 2293 | if (indirect_segments <= BLKIF_MAX_SEGMENTS_PER_REQUEST) |
| 2294 | indirect_segments = 0; |
| 2295 | info->max_indirect_segments = indirect_segments; |
Juergen Gross | a46b536 | 2018-08-13 16:01:11 +0200 | [diff] [blame] | 2296 | |
| 2297 | if (info->feature_persistent) { |
| 2298 | mutex_lock(&blkfront_mutex); |
| 2299 | schedule_delayed_work(&blkfront_work, HZ * 10); |
| 2300 | mutex_unlock(&blkfront_mutex); |
| 2301 | } |
Bob Liu | d50babb | 2015-07-22 14:40:08 +0800 | [diff] [blame] | 2302 | } |
| 2303 | |
| 2304 | /* |
Jeremy Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 2305 | * Invoked when the backend is finally 'ready' (and has told produced |
| 2306 | * the details about the physical device - #sectors, size, etc). |
| 2307 | */ |
| 2308 | static void blkfront_connect(struct blkfront_info *info) |
| 2309 | { |
| 2310 | unsigned long long sectors; |
| 2311 | unsigned long sector_size; |
Stefan Bader | 7c4d7d7 | 2013-05-13 16:28:15 +0200 | [diff] [blame] | 2312 | unsigned int physical_sector_size; |
Bob Liu | 3df0e50 | 2015-11-14 11:12:12 +0800 | [diff] [blame] | 2313 | int err, i; |
Juergen Gross | 4ab50af | 2020-03-05 16:51:29 +0100 | [diff] [blame] | 2314 | struct blkfront_ring_info *rinfo; |
Jeremy Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 2315 | |
K. Y. Srinivasan | 1fa73be | 2010-03-11 13:42:26 -0800 | [diff] [blame] | 2316 | switch (info->connected) { |
| 2317 | case BLKIF_STATE_CONNECTED: |
| 2318 | /* |
| 2319 | * Potentially, the back-end may be signalling |
| 2320 | * a capacity change; update the capacity. |
| 2321 | */ |
| 2322 | err = xenbus_scanf(XBT_NIL, info->xbdev->otherend, |
| 2323 | "sectors", "%Lu", §ors); |
| 2324 | if (XENBUS_EXIST_ERR(err)) |
| 2325 | return; |
| 2326 | printk(KERN_INFO "Setting capacity to %Lu\n", |
| 2327 | sectors); |
Christoph Hellwig | 449f4ec | 2020-11-16 15:56:56 +0100 | [diff] [blame] | 2328 | set_capacity_and_notify(info->gd, sectors); |
K. Y. Srinivasan | 1fa73be | 2010-03-11 13:42:26 -0800 | [diff] [blame] | 2329 | |
Roger Pau Monne | 402b27f | 2013-04-18 16:06:54 +0200 | [diff] [blame] | 2330 | return; |
K. Y. Srinivasan | 1fa73be | 2010-03-11 13:42:26 -0800 | [diff] [blame] | 2331 | case BLKIF_STATE_SUSPENDED: |
Roger Pau Monne | 402b27f | 2013-04-18 16:06:54 +0200 | [diff] [blame] | 2332 | /* |
| 2333 | * If we are recovering from suspension, we need to wait |
| 2334 | * for the backend to announce it's features before |
| 2335 | * reconnecting, at least we need to know if the backend |
| 2336 | * supports indirect descriptors, and how many. |
| 2337 | */ |
| 2338 | blkif_recover(info); |
Jeremy Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 2339 | return; |
| 2340 | |
Jeremy Fitzhardinge | b4dddb4 | 2010-03-11 15:10:40 -0800 | [diff] [blame] | 2341 | default: |
| 2342 | break; |
K. Y. Srinivasan | 1fa73be | 2010-03-11 13:42:26 -0800 | [diff] [blame] | 2343 | } |
Jeremy Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 2344 | |
| 2345 | dev_dbg(&info->xbdev->dev, "%s:%s.\n", |
| 2346 | __func__, info->xbdev->otherend); |
| 2347 | |
| 2348 | err = xenbus_gather(XBT_NIL, info->xbdev->otherend, |
| 2349 | "sectors", "%llu", §ors, |
Christoph Hellwig | 1a827ce1 | 2021-11-22 14:06:14 +0100 | [diff] [blame] | 2350 | "info", "%u", &info->vdisk_info, |
Jeremy Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 2351 | "sector-size", "%lu", §or_size, |
| 2352 | NULL); |
| 2353 | if (err) { |
| 2354 | xenbus_dev_fatal(info->xbdev, err, |
| 2355 | "reading backend fields at %s", |
| 2356 | info->xbdev->otherend); |
| 2357 | return; |
| 2358 | } |
| 2359 | |
Stefan Bader | 7c4d7d7 | 2013-05-13 16:28:15 +0200 | [diff] [blame] | 2360 | /* |
Bjorn Helgaas | ec3307a | 2021-01-26 14:55:09 -0600 | [diff] [blame] | 2361 | * physical-sector-size is a newer field, so old backends may not |
Stefan Bader | 7c4d7d7 | 2013-05-13 16:28:15 +0200 | [diff] [blame] | 2362 | * provide this. Assume physical sector size to be the same as |
| 2363 | * sector_size in that case. |
| 2364 | */ |
Juergen Gross | f27dc1a | 2016-10-31 14:58:40 +0100 | [diff] [blame] | 2365 | physical_sector_size = xenbus_read_unsigned(info->xbdev->otherend, |
| 2366 | "physical-sector-size", |
| 2367 | sector_size); |
Bob Liu | 3df0e50 | 2015-11-14 11:12:12 +0800 | [diff] [blame] | 2368 | blkfront_gather_backend_features(info); |
Juergen Gross | 4ab50af | 2020-03-05 16:51:29 +0100 | [diff] [blame] | 2369 | for_each_rinfo(info, rinfo, i) { |
| 2370 | err = blkfront_setup_indirect(rinfo); |
Bob Liu | 3df0e50 | 2015-11-14 11:12:12 +0800 | [diff] [blame] | 2371 | if (err) { |
| 2372 | xenbus_dev_fatal(info->xbdev, err, "setup_indirect at %s", |
| 2373 | info->xbdev->otherend); |
| 2374 | blkif_free(info, 0); |
| 2375 | break; |
| 2376 | } |
Roger Pau Monne | 402b27f | 2013-04-18 16:06:54 +0200 | [diff] [blame] | 2377 | } |
| 2378 | |
Christoph Hellwig | 1a827ce1 | 2021-11-22 14:06:14 +0100 | [diff] [blame] | 2379 | err = xlvbd_alloc_gendisk(sectors, info, sector_size, |
Stefan Bader | 7c4d7d7 | 2013-05-13 16:28:15 +0200 | [diff] [blame] | 2380 | physical_sector_size); |
Jeremy Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 2381 | if (err) { |
| 2382 | xenbus_dev_fatal(info->xbdev, err, "xlvbd_add at %s", |
| 2383 | info->xbdev->otherend); |
Bob Liu | 4e876c2 | 2016-07-27 17:42:04 +0800 | [diff] [blame] | 2384 | goto fail; |
Jeremy Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 2385 | } |
| 2386 | |
| 2387 | xenbus_switch_state(info->xbdev, XenbusStateConnected); |
| 2388 | |
| 2389 | /* Kick pending requests. */ |
Jeremy Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 2390 | info->connected = BLKIF_STATE_CONNECTED; |
Juergen Gross | 4ab50af | 2020-03-05 16:51:29 +0100 | [diff] [blame] | 2391 | for_each_rinfo(info, rinfo, i) |
| 2392 | kick_pending_request_queues(rinfo); |
Jeremy Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 2393 | |
Luis Chamberlain | 293a7c5 | 2021-10-15 16:30:24 -0700 | [diff] [blame] | 2394 | err = device_add_disk(&info->xbdev->dev, info->gd, NULL); |
| 2395 | if (err) { |
| 2396 | blk_cleanup_disk(info->gd); |
| 2397 | blk_mq_free_tag_set(&info->tag_set); |
| 2398 | info->rq = NULL; |
| 2399 | goto fail; |
| 2400 | } |
Christian Limpach | 1d78d70 | 2008-04-02 10:54:04 -0700 | [diff] [blame] | 2401 | |
| 2402 | info->is_ready = 1; |
Bob Liu | 4e876c2 | 2016-07-27 17:42:04 +0800 | [diff] [blame] | 2403 | return; |
| 2404 | |
| 2405 | fail: |
| 2406 | blkif_free(info, 0); |
| 2407 | return; |
Jeremy Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 2408 | } |
| 2409 | |
Lee Jones | 5fdbd5b | 2021-03-12 10:55:29 +0000 | [diff] [blame] | 2410 | /* |
Jeremy Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 2411 | * Callback received when the backend's state changes. |
| 2412 | */ |
Ian Campbell | 203fd61 | 2009-12-04 15:33:54 +0000 | [diff] [blame] | 2413 | static void blkback_changed(struct xenbus_device *dev, |
Jeremy Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 2414 | enum xenbus_state backend_state) |
| 2415 | { |
Greg Kroah-Hartman | a1b4b12 | 2009-04-30 14:43:31 -0700 | [diff] [blame] | 2416 | struct blkfront_info *info = dev_get_drvdata(&dev->dev); |
Jeremy Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 2417 | |
Ian Campbell | 203fd61 | 2009-12-04 15:33:54 +0000 | [diff] [blame] | 2418 | dev_dbg(&dev->dev, "blkfront:blkback_changed to state %d.\n", backend_state); |
Jeremy Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 2419 | |
| 2420 | switch (backend_state) { |
Jeremy Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 2421 | case XenbusStateInitWait: |
Bob Liu | a9b54bb | 2015-06-19 00:23:00 -0400 | [diff] [blame] | 2422 | if (dev->state != XenbusStateInitialising) |
| 2423 | break; |
Konrad Rzeszutek Wilk | c31ecf6 | 2015-12-18 16:28:53 -0500 | [diff] [blame] | 2424 | if (talk_to_blkback(dev, info)) |
Bob Liu | 8ab0144 | 2015-06-03 13:40:02 +0800 | [diff] [blame] | 2425 | break; |
Gustavo A. R. Silva | 3955bcb | 2020-11-20 12:32:58 -0600 | [diff] [blame] | 2426 | break; |
Bob Liu | 8ab0144 | 2015-06-03 13:40:02 +0800 | [diff] [blame] | 2427 | case XenbusStateInitialising: |
Jeremy Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 2428 | case XenbusStateInitialised: |
Noboru Iwamatsu | b78c951 | 2009-10-13 17:22:29 -0400 | [diff] [blame] | 2429 | case XenbusStateReconfiguring: |
| 2430 | case XenbusStateReconfigured: |
Jeremy Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 2431 | case XenbusStateUnknown: |
Jeremy Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 2432 | break; |
| 2433 | |
| 2434 | case XenbusStateConnected: |
Bob Liu | efd1535 | 2016-06-07 10:43:15 -0400 | [diff] [blame] | 2435 | /* |
| 2436 | * talk_to_blkback sets state to XenbusStateInitialised |
| 2437 | * and blkfront_connect sets it to XenbusStateConnected |
| 2438 | * (if connection went OK). |
| 2439 | * |
| 2440 | * If the backend (or toolstack) decides to poke at backend |
| 2441 | * state (and re-trigger the watch by setting the state repeatedly |
| 2442 | * to XenbusStateConnected (4)) we need to deal with this. |
| 2443 | * This is allowed as this is used to communicate to the guest |
| 2444 | * that the size of disk has changed! |
| 2445 | */ |
| 2446 | if ((dev->state != XenbusStateInitialised) && |
| 2447 | (dev->state != XenbusStateConnected)) { |
Konrad Rzeszutek Wilk | c31ecf6 | 2015-12-18 16:28:53 -0500 | [diff] [blame] | 2448 | if (talk_to_blkback(dev, info)) |
| 2449 | break; |
| 2450 | } |
Bob Liu | efd1535 | 2016-06-07 10:43:15 -0400 | [diff] [blame] | 2451 | |
Jeremy Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 2452 | blkfront_connect(info); |
| 2453 | break; |
| 2454 | |
David Vrabel | 3661371 | 2014-02-04 18:53:56 +0000 | [diff] [blame] | 2455 | case XenbusStateClosed: |
| 2456 | if (dev->state == XenbusStateClosed) |
| 2457 | break; |
Gustavo A. R. Silva | df561f66 | 2020-08-23 17:36:59 -0500 | [diff] [blame] | 2458 | fallthrough; |
Jeremy Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 2459 | case XenbusStateClosing: |
Christoph Hellwig | 05d69d9 | 2021-07-15 16:17:11 +0200 | [diff] [blame] | 2460 | blkfront_closing(info); |
Jeremy Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 2461 | break; |
| 2462 | } |
| 2463 | } |
| 2464 | |
Daniel Stodden | fa1bd35 | 2010-04-30 22:01:22 +0000 | [diff] [blame] | 2465 | static int blkfront_remove(struct xenbus_device *xbdev) |
Jeremy Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 2466 | { |
Daniel Stodden | fa1bd35 | 2010-04-30 22:01:22 +0000 | [diff] [blame] | 2467 | struct blkfront_info *info = dev_get_drvdata(&xbdev->dev); |
Jeremy Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 2468 | |
Daniel Stodden | fa1bd35 | 2010-04-30 22:01:22 +0000 | [diff] [blame] | 2469 | dev_dbg(&xbdev->dev, "%s removed", xbdev->nodename); |
Jeremy Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 2470 | |
Christoph Hellwig | 05d69d9 | 2021-07-15 16:17:11 +0200 | [diff] [blame] | 2471 | del_gendisk(info->gd); |
| 2472 | |
| 2473 | mutex_lock(&blkfront_mutex); |
| 2474 | list_del(&info->info_list); |
| 2475 | mutex_unlock(&blkfront_mutex); |
Vasilis Liaskovitis | f92898e | 2018-10-15 15:25:08 +0200 | [diff] [blame] | 2476 | |
Jeremy Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 2477 | blkif_free(info, 0); |
Christoph Hellwig | 05d69d9 | 2021-07-15 16:17:11 +0200 | [diff] [blame] | 2478 | xlbd_release_minors(info->gd->first_minor, info->gd->minors); |
| 2479 | blk_cleanup_disk(info->gd); |
| 2480 | blk_mq_free_tag_set(&info->tag_set); |
Jeremy Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 2481 | |
Christoph Hellwig | 05d69d9 | 2021-07-15 16:17:11 +0200 | [diff] [blame] | 2482 | kfree(info); |
Jeremy Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 2483 | return 0; |
| 2484 | } |
| 2485 | |
Christian Limpach | 1d78d70 | 2008-04-02 10:54:04 -0700 | [diff] [blame] | 2486 | static int blkfront_is_ready(struct xenbus_device *dev) |
| 2487 | { |
Greg Kroah-Hartman | a1b4b12 | 2009-04-30 14:43:31 -0700 | [diff] [blame] | 2488 | struct blkfront_info *info = dev_get_drvdata(&dev->dev); |
Christian Limpach | 1d78d70 | 2008-04-02 10:54:04 -0700 | [diff] [blame] | 2489 | |
Jan Beulich | 5d7ed20 | 2010-08-07 18:31:12 +0200 | [diff] [blame] | 2490 | return info->is_ready && info->xbdev; |
Christian Limpach | 1d78d70 | 2008-04-02 10:54:04 -0700 | [diff] [blame] | 2491 | } |
| 2492 | |
Alexey Dobriyan | 83d5cde | 2009-09-21 17:01:13 -0700 | [diff] [blame] | 2493 | static const struct block_device_operations xlvbd_block_fops = |
Jeremy Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 2494 | { |
| 2495 | .owner = THIS_MODULE, |
Ian Campbell | 597592d | 2008-02-21 13:03:45 -0800 | [diff] [blame] | 2496 | .getgeo = blkif_getgeo, |
Arnd Bergmann | 8a6cfeb | 2010-07-08 10:18:46 +0200 | [diff] [blame] | 2497 | .ioctl = blkif_ioctl, |
Arnd Bergmann | 9452b1a | 2019-11-28 15:48:10 +0100 | [diff] [blame] | 2498 | .compat_ioctl = blkdev_compat_ptr_ioctl, |
Jeremy Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 2499 | }; |
| 2500 | |
| 2501 | |
Márton Németh | ec9c42e | 2010-01-10 13:39:52 +0100 | [diff] [blame] | 2502 | static const struct xenbus_device_id blkfront_ids[] = { |
Jeremy Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 2503 | { "vbd" }, |
| 2504 | { "" } |
| 2505 | }; |
| 2506 | |
David Vrabel | 95afae4 | 2014-09-08 17:30:41 +0100 | [diff] [blame] | 2507 | static struct xenbus_driver blkfront_driver = { |
| 2508 | .ids = blkfront_ids, |
Jeremy Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 2509 | .probe = blkfront_probe, |
| 2510 | .remove = blkfront_remove, |
| 2511 | .resume = blkfront_resume, |
Ian Campbell | 203fd61 | 2009-12-04 15:33:54 +0000 | [diff] [blame] | 2512 | .otherend_changed = blkback_changed, |
Christian Limpach | 1d78d70 | 2008-04-02 10:54:04 -0700 | [diff] [blame] | 2513 | .is_ready = blkfront_is_ready, |
David Vrabel | 95afae4 | 2014-09-08 17:30:41 +0100 | [diff] [blame] | 2514 | }; |
Jeremy Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 2515 | |
Juergen Gross | a46b536 | 2018-08-13 16:01:11 +0200 | [diff] [blame] | 2516 | static void purge_persistent_grants(struct blkfront_info *info) |
| 2517 | { |
| 2518 | unsigned int i; |
| 2519 | unsigned long flags; |
Juergen Gross | 4ab50af | 2020-03-05 16:51:29 +0100 | [diff] [blame] | 2520 | struct blkfront_ring_info *rinfo; |
Juergen Gross | a46b536 | 2018-08-13 16:01:11 +0200 | [diff] [blame] | 2521 | |
Juergen Gross | 4ab50af | 2020-03-05 16:51:29 +0100 | [diff] [blame] | 2522 | for_each_rinfo(info, rinfo, i) { |
Juergen Gross | a46b536 | 2018-08-13 16:01:11 +0200 | [diff] [blame] | 2523 | struct grant *gnt_list_entry, *tmp; |
| 2524 | |
| 2525 | spin_lock_irqsave(&rinfo->ring_lock, flags); |
| 2526 | |
| 2527 | if (rinfo->persistent_gnts_c == 0) { |
| 2528 | spin_unlock_irqrestore(&rinfo->ring_lock, flags); |
| 2529 | continue; |
| 2530 | } |
| 2531 | |
| 2532 | list_for_each_entry_safe(gnt_list_entry, tmp, &rinfo->grants, |
| 2533 | node) { |
| 2534 | if (gnt_list_entry->gref == GRANT_INVALID_REF || |
| 2535 | gnttab_query_foreign_access(gnt_list_entry->gref)) |
| 2536 | continue; |
| 2537 | |
| 2538 | list_del(&gnt_list_entry->node); |
| 2539 | gnttab_end_foreign_access(gnt_list_entry->gref, 0, 0UL); |
| 2540 | rinfo->persistent_gnts_c--; |
Juergen Gross | 6c76786 | 2018-09-28 09:28:27 +0200 | [diff] [blame] | 2541 | gnt_list_entry->gref = GRANT_INVALID_REF; |
| 2542 | list_add_tail(&gnt_list_entry->node, &rinfo->grants); |
Juergen Gross | a46b536 | 2018-08-13 16:01:11 +0200 | [diff] [blame] | 2543 | } |
| 2544 | |
| 2545 | spin_unlock_irqrestore(&rinfo->ring_lock, flags); |
| 2546 | } |
| 2547 | } |
| 2548 | |
| 2549 | static void blkfront_delay_work(struct work_struct *work) |
| 2550 | { |
| 2551 | struct blkfront_info *info; |
| 2552 | bool need_schedule_work = false; |
| 2553 | |
| 2554 | mutex_lock(&blkfront_mutex); |
| 2555 | |
| 2556 | list_for_each_entry(info, &info_list, info_list) { |
| 2557 | if (info->feature_persistent) { |
| 2558 | need_schedule_work = true; |
| 2559 | mutex_lock(&info->mutex); |
| 2560 | purge_persistent_grants(info); |
| 2561 | mutex_unlock(&info->mutex); |
| 2562 | } |
| 2563 | } |
| 2564 | |
| 2565 | if (need_schedule_work) |
| 2566 | schedule_delayed_work(&blkfront_work, HZ * 10); |
| 2567 | |
| 2568 | mutex_unlock(&blkfront_mutex); |
| 2569 | } |
| 2570 | |
Jeremy Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 2571 | static int __init xlblk_init(void) |
| 2572 | { |
Laszlo Ersek | 469738e | 2011-10-07 21:34:38 +0200 | [diff] [blame] | 2573 | int ret; |
Bob Liu | 28d949b | 2015-11-14 11:12:14 +0800 | [diff] [blame] | 2574 | int nr_cpus = num_online_cpus(); |
Laszlo Ersek | 469738e | 2011-10-07 21:34:38 +0200 | [diff] [blame] | 2575 | |
Jeremy Fitzhardinge | 6e83358 | 2008-08-19 13:16:17 -0700 | [diff] [blame] | 2576 | if (!xen_domain()) |
Jeremy Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 2577 | return -ENODEV; |
| 2578 | |
Juergen Gross | 4bcddba | 2018-08-13 16:01:12 +0200 | [diff] [blame] | 2579 | if (!xen_has_pv_disk_devices()) |
| 2580 | return -ENODEV; |
| 2581 | |
| 2582 | if (register_blkdev(XENVBD_MAJOR, DEV_NAME)) { |
| 2583 | pr_warn("xen_blk: can't get major %d with name %s\n", |
| 2584 | XENVBD_MAJOR, DEV_NAME); |
| 2585 | return -ENODEV; |
| 2586 | } |
| 2587 | |
Jan Beulich | 3b4f188 | 2017-01-23 08:11:37 -0700 | [diff] [blame] | 2588 | if (xen_blkif_max_segments < BLKIF_MAX_SEGMENTS_PER_REQUEST) |
| 2589 | xen_blkif_max_segments = BLKIF_MAX_SEGMENTS_PER_REQUEST; |
| 2590 | |
Julien Grall | 9cce291 | 2015-10-13 17:50:11 +0100 | [diff] [blame] | 2591 | if (xen_blkif_max_ring_order > XENBUS_MAX_RING_GRANT_ORDER) { |
Bob Liu | 86839c5 | 2015-06-03 13:40:03 +0800 | [diff] [blame] | 2592 | pr_info("Invalid max_ring_order (%d), will use default max: %d.\n", |
Julien Grall | 9cce291 | 2015-10-13 17:50:11 +0100 | [diff] [blame] | 2593 | xen_blkif_max_ring_order, XENBUS_MAX_RING_GRANT_ORDER); |
Peng Fan | 45fc826 | 2015-11-25 18:26:01 +0800 | [diff] [blame] | 2594 | xen_blkif_max_ring_order = XENBUS_MAX_RING_GRANT_ORDER; |
Bob Liu | 86839c5 | 2015-06-03 13:40:03 +0800 | [diff] [blame] | 2595 | } |
| 2596 | |
Bob Liu | 28d949b | 2015-11-14 11:12:14 +0800 | [diff] [blame] | 2597 | if (xen_blkif_max_queues > nr_cpus) { |
| 2598 | pr_info("Invalid max_queues (%d), will use default max: %d.\n", |
| 2599 | xen_blkif_max_queues, nr_cpus); |
| 2600 | xen_blkif_max_queues = nr_cpus; |
| 2601 | } |
| 2602 | |
Juergen Gross | a46b536 | 2018-08-13 16:01:11 +0200 | [diff] [blame] | 2603 | INIT_DELAYED_WORK(&blkfront_work, blkfront_delay_work); |
| 2604 | |
Jan Beulich | 73db144 | 2011-12-22 09:08:13 +0000 | [diff] [blame] | 2605 | ret = xenbus_register_frontend(&blkfront_driver); |
Laszlo Ersek | 469738e | 2011-10-07 21:34:38 +0200 | [diff] [blame] | 2606 | if (ret) { |
| 2607 | unregister_blkdev(XENVBD_MAJOR, DEV_NAME); |
| 2608 | return ret; |
| 2609 | } |
| 2610 | |
| 2611 | return 0; |
Jeremy Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 2612 | } |
| 2613 | module_init(xlblk_init); |
| 2614 | |
| 2615 | |
Jan Beulich | 5a60d0c | 2008-06-17 10:47:08 +0200 | [diff] [blame] | 2616 | static void __exit xlblk_exit(void) |
Jeremy Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 2617 | { |
Juergen Gross | a46b536 | 2018-08-13 16:01:11 +0200 | [diff] [blame] | 2618 | cancel_delayed_work_sync(&blkfront_work); |
| 2619 | |
Jan Beulich | 8605067 | 2012-04-05 16:04:52 +0100 | [diff] [blame] | 2620 | xenbus_unregister_driver(&blkfront_driver); |
| 2621 | unregister_blkdev(XENVBD_MAJOR, DEV_NAME); |
| 2622 | kfree(minors); |
Jeremy Fitzhardinge | 9f27ee5 | 2007-07-17 18:37:06 -0700 | [diff] [blame] | 2623 | } |
| 2624 | module_exit(xlblk_exit); |
| 2625 | |
| 2626 | MODULE_DESCRIPTION("Xen virtual block device frontend"); |
| 2627 | MODULE_LICENSE("GPL"); |
| 2628 | MODULE_ALIAS_BLOCKDEV_MAJOR(XENVBD_MAJOR); |
Mark McLoughlin | d2f0c52 | 2008-04-02 10:54:05 -0700 | [diff] [blame] | 2629 | MODULE_ALIAS("xen:vbd"); |
Mark McLoughlin | 4f93f09b | 2008-04-02 10:54:06 -0700 | [diff] [blame] | 2630 | MODULE_ALIAS("xenblk"); |