Commit | Line | Data |
---|---|---|
9f27ee59 JF |
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> | |
907c3eb1 | 40 | #include <linux/blk-mq.h> |
597592d9 | 41 | #include <linux/hdreg.h> |
440a01a7 | 42 | #include <linux/cdrom.h> |
9f27ee59 | 43 | #include <linux/module.h> |
5a0e3ad6 | 44 | #include <linux/slab.h> |
b81e0c23 | 45 | #include <linux/major.h> |
2a48fc0a | 46 | #include <linux/mutex.h> |
9e973e64 | 47 | #include <linux/scatterlist.h> |
34ae2e47 | 48 | #include <linux/bitmap.h> |
155b7edb | 49 | #include <linux/list.h> |
a46b5367 | 50 | #include <linux/workqueue.h> |
3a169c0b | 51 | #include <linux/sched/mm.h> |
9f27ee59 | 52 | |
1ccbf534 | 53 | #include <xen/xen.h> |
9f27ee59 JF |
54 | #include <xen/xenbus.h> |
55 | #include <xen/grant_table.h> | |
56 | #include <xen/events.h> | |
57 | #include <xen/page.h> | |
c1c5413a | 58 | #include <xen/platform_pci.h> |
9f27ee59 JF |
59 | |
60 | #include <xen/interface/grant_table.h> | |
61 | #include <xen/interface/io/blkif.h> | |
3e334239 | 62 | #include <xen/interface/io/protocols.h> |
9f27ee59 JF |
63 | |
64 | #include <asm/xen/hypervisor.h> | |
65 | ||
6cc56833 JG |
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 | ||
9f27ee59 JF |
80 | enum blkif_state { |
81 | BLKIF_STATE_DISCONNECTED, | |
82 | BLKIF_STATE_CONNECTED, | |
83 | BLKIF_STATE_SUSPENDED, | |
b94e4b14 | 84 | BLKIF_STATE_ERROR, |
9f27ee59 JF |
85 | }; |
86 | ||
0a8704a5 RPM |
87 | struct grant { |
88 | grant_ref_t gref; | |
a7a6df22 | 89 | struct page *page; |
155b7edb | 90 | struct list_head node; |
0a8704a5 RPM |
91 | }; |
92 | ||
6cc56833 | 93 | enum blk_req_status { |
b94e4b14 | 94 | REQ_PROCESSING, |
6cc56833 JG |
95 | REQ_WAITING, |
96 | REQ_DONE, | |
97 | REQ_ERROR, | |
98 | REQ_EOPNOTSUPP, | |
99 | }; | |
100 | ||
9f27ee59 JF |
101 | struct blk_shadow { |
102 | struct blkif_request req; | |
a945b980 | 103 | struct request *request; |
402b27f9 RPM |
104 | struct grant **grants_used; |
105 | struct grant **indirect_grants; | |
b7649158 | 106 | struct scatterlist *sg; |
c004a6fe | 107 | unsigned int num_sg; |
6cc56833 JG |
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; | |
402b27f9 RPM |
116 | }; |
117 | ||
2609587c | 118 | struct blkif_req { |
31c4ccc3 | 119 | blk_status_t error; |
2609587c CH |
120 | }; |
121 | ||
122 | static inline struct blkif_req *blkif_req(struct request *rq) | |
123 | { | |
124 | return blk_mq_rq_to_pdu(rq); | |
125 | } | |
126 | ||
2a48fc0a | 127 | static DEFINE_MUTEX(blkfront_mutex); |
83d5cde4 | 128 | static const struct block_device_operations xlvbd_block_fops; |
a46b5367 JG |
129 | static struct delayed_work blkfront_work; |
130 | static LIST_HEAD(info_list); | |
9f27ee59 | 131 | |
402b27f9 RPM |
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; | |
5657a819 | 139 | module_param_named(max_indirect_segments, xen_blkif_max_segments, uint, 0444); |
14e710fe JB |
140 | MODULE_PARM_DESC(max_indirect_segments, |
141 | "Maximum amount of segments in indirect requests (default is 32)"); | |
402b27f9 | 142 | |
28d949bc | 143 | static unsigned int xen_blkif_max_queues = 4; |
5657a819 | 144 | module_param_named(max_queues, xen_blkif_max_queues, uint, 0444); |
28d949bc BL |
145 | MODULE_PARM_DESC(max_queues, "Maximum number of hardware queues/rings used per virtual disk"); |
146 | ||
86839c56 BL |
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; | |
5657a819 | 152 | module_param_named(max_ring_page_order, xen_blkif_max_ring_order, int, 0444); |
86839c56 BL |
153 | MODULE_PARM_DESC(max_ring_page_order, "Maximum order of pages to be used for the shared ring"); |
154 | ||
c004a6fe JG |
155 | #define BLK_RING_SIZE(info) \ |
156 | __CONST_RING_SIZE(blkif, XEN_PAGE_SIZE * (info)->nr_ring_pages) | |
157 | ||
86839c56 | 158 | /* |
6f03a7ff KRW |
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. | |
86839c56 BL |
161 | */ |
162 | #define RINGREF_NAME_LEN (20) | |
28d949bc BL |
163 | /* |
164 | * queue-%u would take 7 + 10(UINT_MAX) = 17 characters. | |
165 | */ | |
166 | #define QUEUE_NAME_LEN (17) | |
9f27ee59 | 167 | |
81f35161 BL |
168 | /* |
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 { | |
11659569 BL |
174 | /* Lock to protect data in every ring buffer. */ |
175 | spinlock_t ring_lock; | |
81f35161 BL |
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; | |
81f35161 | 181 | struct list_head indirect_pages; |
73716df7 BL |
182 | struct list_head grants; |
183 | unsigned int persistent_gnts_c; | |
81f35161 BL |
184 | unsigned long shadow_free; |
185 | struct blkfront_info *dev_info; | |
0265d6e8 | 186 | struct blk_shadow shadow[]; |
81f35161 BL |
187 | }; |
188 | ||
9f27ee59 JF |
189 | /* |
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 | { | |
b70f5fa0 | 196 | struct mutex mutex; |
9f27ee59 | 197 | struct xenbus_device *xbdev; |
9f27ee59 | 198 | struct gendisk *gd; |
172335ad BL |
199 | u16 sector_size; |
200 | unsigned int physical_sector_size; | |
1a827ce1 | 201 | unsigned long vdisk_info; |
9f27ee59 JF |
202 | int vdevice; |
203 | blkif_vdev_t handle; | |
204 | enum blkif_state connected; | |
3df0e505 | 205 | /* Number of pages per ring buffer. */ |
86839c56 | 206 | unsigned int nr_ring_pages; |
9f27ee59 | 207 | struct request_queue *rq; |
b32728ff JB |
208 | unsigned int feature_flush:1; |
209 | unsigned int feature_fua:1; | |
5ea42986 KRW |
210 | unsigned int feature_discard:1; |
211 | unsigned int feature_secdiscard:1; | |
b32728ff | 212 | unsigned int feature_persistent:1; |
ed30bf31 LD |
213 | unsigned int discard_granularity; |
214 | unsigned int discard_alignment; | |
c004a6fe | 215 | /* Number of 4KB segments handled */ |
402b27f9 | 216 | unsigned int max_indirect_segments; |
1d78d705 | 217 | int is_ready; |
907c3eb1 | 218 | struct blk_mq_tag_set tag_set; |
3df0e505 BL |
219 | struct blkfront_ring_info *rinfo; |
220 | unsigned int nr_rings; | |
4ab50af6 | 221 | unsigned int rinfo_size; |
7b427a59 BL |
222 | /* Save uncomplete reqs and bios for migration. */ |
223 | struct list_head requests; | |
224 | struct bio_list bio_list; | |
a46b5367 | 225 | struct list_head info_list; |
9f27ee59 JF |
226 | }; |
227 | ||
0e345826 JB |
228 | static unsigned int nr_minors; |
229 | static unsigned long *minors; | |
230 | static DEFINE_SPINLOCK(minor_lock); | |
231 | ||
9f27ee59 JF |
232 | #define GRANT_INVALID_REF 0 |
233 | ||
234 | #define PARTS_PER_DISK 16 | |
9246b5f0 | 235 | #define PARTS_PER_EXT_DISK 256 |
9f27ee59 JF |
236 | |
237 | #define BLKIF_MAJOR(dev) ((dev)>>8) | |
238 | #define BLKIF_MINOR(dev) ((dev) & 0xff) | |
239 | ||
9246b5f0 CL |
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)) | |
c80a4209 SS |
244 | #define EMULATED_HD_DISK_MINOR_OFFSET (0) |
245 | #define EMULATED_HD_DISK_NAME_OFFSET (EMULATED_HD_DISK_MINOR_OFFSET / 256) | |
196cfe2a SB |
246 | #define EMULATED_SD_DISK_MINOR_OFFSET (0) |
247 | #define EMULATED_SD_DISK_NAME_OFFSET (EMULATED_SD_DISK_MINOR_OFFSET / 256) | |
9f27ee59 | 248 | |
9246b5f0 | 249 | #define DEV_NAME "xvd" /* name in /dev */ |
9f27ee59 | 250 | |
c004a6fe JG |
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 | ||
c004a6fe JG |
261 | #define INDIRECT_GREFS(_grants) \ |
262 | DIV_ROUND_UP(_grants, GRANTS_PER_INDIRECT_FRAME) | |
263 | ||
81f35161 | 264 | static int blkfront_setup_indirect(struct blkfront_ring_info *rinfo); |
3df0e505 | 265 | static void blkfront_gather_backend_features(struct blkfront_info *info); |
7ed8ce1c | 266 | static int negotiate_mq(struct blkfront_info *info); |
402b27f9 | 267 | |
4ab50af6 JG |
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 | ||
81f35161 | 280 | static int get_id_from_freelist(struct blkfront_ring_info *rinfo) |
9f27ee59 | 281 | { |
81f35161 BL |
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 */ | |
9f27ee59 JF |
287 | return free; |
288 | } | |
289 | ||
81f35161 | 290 | static int add_id_to_freelist(struct blkfront_ring_info *rinfo, |
6f03a7ff | 291 | unsigned long id) |
9f27ee59 | 292 | { |
81f35161 | 293 | if (rinfo->shadow[id].req.u.rw.id != id) |
6878c32e | 294 | return -EINVAL; |
81f35161 | 295 | if (rinfo->shadow[id].request == NULL) |
6878c32e | 296 | return -EINVAL; |
81f35161 BL |
297 | rinfo->shadow[id].req.u.rw.id = rinfo->shadow_free; |
298 | rinfo->shadow[id].request = NULL; | |
299 | rinfo->shadow_free = id; | |
6878c32e | 300 | return 0; |
9f27ee59 JF |
301 | } |
302 | ||
81f35161 | 303 | static int fill_grant_buffer(struct blkfront_ring_info *rinfo, int num) |
9c1e050c | 304 | { |
81f35161 | 305 | struct blkfront_info *info = rinfo->dev_info; |
9c1e050c RPM |
306 | struct page *granted_page; |
307 | struct grant *gnt_list_entry, *n; | |
308 | int i = 0; | |
309 | ||
6f03a7ff | 310 | while (i < num) { |
9c1e050c RPM |
311 | gnt_list_entry = kzalloc(sizeof(struct grant), GFP_NOIO); |
312 | if (!gnt_list_entry) | |
313 | goto out_of_memory; | |
314 | ||
bfe11d6d RPM |
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 | } | |
a7a6df22 | 321 | gnt_list_entry->page = granted_page; |
9c1e050c RPM |
322 | } |
323 | ||
9c1e050c | 324 | gnt_list_entry->gref = GRANT_INVALID_REF; |
73716df7 | 325 | list_add(&gnt_list_entry->node, &rinfo->grants); |
9c1e050c RPM |
326 | i++; |
327 | } | |
328 | ||
329 | return 0; | |
330 | ||
331 | out_of_memory: | |
332 | list_for_each_entry_safe(gnt_list_entry, n, | |
73716df7 | 333 | &rinfo->grants, node) { |
9c1e050c | 334 | list_del(&gnt_list_entry->node); |
bfe11d6d | 335 | if (info->feature_persistent) |
a7a6df22 | 336 | __free_page(gnt_list_entry->page); |
9c1e050c RPM |
337 | kfree(gnt_list_entry); |
338 | i--; | |
339 | } | |
340 | BUG_ON(i != 0); | |
341 | return -ENOMEM; | |
342 | } | |
343 | ||
73716df7 | 344 | static struct grant *get_free_grant(struct blkfront_ring_info *rinfo) |
9c1e050c RPM |
345 | { |
346 | struct grant *gnt_list_entry; | |
9c1e050c | 347 | |
73716df7 BL |
348 | BUG_ON(list_empty(&rinfo->grants)); |
349 | gnt_list_entry = list_first_entry(&rinfo->grants, struct grant, | |
4f503fbd | 350 | node); |
9c1e050c RPM |
351 | list_del(&gnt_list_entry->node); |
352 | ||
4f503fbd | 353 | if (gnt_list_entry->gref != GRANT_INVALID_REF) |
73716df7 | 354 | rinfo->persistent_gnts_c--; |
4f503fbd JG |
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, | |
73716df7 | 370 | struct blkfront_ring_info *rinfo) |
4f503fbd | 371 | { |
73716df7 BL |
372 | struct grant *gnt_list_entry = get_free_grant(rinfo); |
373 | struct blkfront_info *info = rinfo->dev_info; | |
4f503fbd JG |
374 | |
375 | if (gnt_list_entry->gref != GRANT_INVALID_REF) | |
9c1e050c | 376 | return gnt_list_entry; |
4f503fbd JG |
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); | |
9c1e050c RPM |
388 | } |
389 | ||
4f503fbd JG |
390 | return gnt_list_entry; |
391 | } | |
392 | ||
393 | static struct grant *get_indirect_grant(grant_ref_t *gref_head, | |
73716df7 | 394 | struct blkfront_ring_info *rinfo) |
4f503fbd | 395 | { |
73716df7 BL |
396 | struct grant *gnt_list_entry = get_free_grant(rinfo); |
397 | struct blkfront_info *info = rinfo->dev_info; | |
4f503fbd JG |
398 | |
399 | if (gnt_list_entry->gref != GRANT_INVALID_REF) | |
400 | return gnt_list_entry; | |
401 | ||
9c1e050c RPM |
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); | |
bfe11d6d | 405 | if (!info->feature_persistent) { |
4f503fbd JG |
406 | struct page *indirect_page; |
407 | ||
408 | /* Fetch a pre-allocated page to use for indirect grefs */ | |
73716df7 BL |
409 | BUG_ON(list_empty(&rinfo->indirect_pages)); |
410 | indirect_page = list_first_entry(&rinfo->indirect_pages, | |
4f503fbd JG |
411 | struct page, lru); |
412 | list_del(&indirect_page->lru); | |
413 | gnt_list_entry->page = indirect_page; | |
bfe11d6d | 414 | } |
4f503fbd JG |
415 | grant_foreign_access(gnt_list_entry, info); |
416 | ||
9c1e050c RPM |
417 | return gnt_list_entry; |
418 | } | |
419 | ||
6878c32e KRW |
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 | } | |
0e345826 JB |
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 | ||
f094148a | 445 | bitmap = kcalloc(BITS_TO_LONGS(end), sizeof(*bitmap), |
0e345826 JB |
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) { | |
34ae2e47 | 465 | bitmap_set(minors, minor, nr); |
0e345826 JB |
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); | |
34ae2e47 | 480 | bitmap_clear(minors, minor, nr); |
0e345826 JB |
481 | spin_unlock(&minor_lock); |
482 | } | |
483 | ||
9f27ee59 JF |
484 | static void blkif_restart_queue_callback(void *arg) |
485 | { | |
81f35161 BL |
486 | struct blkfront_ring_info *rinfo = (struct blkfront_ring_info *)arg; |
487 | schedule_work(&rinfo->work); | |
9f27ee59 JF |
488 | } |
489 | ||
afe42d7d | 490 | static int blkif_getgeo(struct block_device *bd, struct hd_geometry *hg) |
597592d9 IC |
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 | ||
a63c848b | 506 | static int blkif_ioctl(struct block_device *bdev, fmode_t mode, |
62aa0054 | 507 | unsigned command, unsigned long argument) |
440a01a7 | 508 | { |
1a827ce1 | 509 | struct blkfront_info *info = bdev->bd_disk->private_data; |
440a01a7 CL |
510 | int i; |
511 | ||
440a01a7 CL |
512 | switch (command) { |
513 | case CDROMMULTISESSION: | |
440a01a7 CL |
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; | |
05d69d95 | 518 | case CDROM_GET_CAPABILITY: |
1a827ce1 CH |
519 | if (!(info->vdisk_info & VDISK_CDROM)) |
520 | return -EINVAL; | |
521 | return 0; | |
440a01a7 | 522 | default: |
05d69d95 | 523 | return -EINVAL; |
440a01a7 | 524 | } |
440a01a7 CL |
525 | } |
526 | ||
2e073969 JG |
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; | |
b94e4b14 | 538 | rinfo->shadow[id].status = REQ_PROCESSING; |
6cc56833 | 539 | rinfo->shadow[id].associated_id = NO_ASSOCIATED_ID; |
2e073969 | 540 | |
8f5a695d | 541 | rinfo->shadow[id].req.u.rw.id = id; |
2e073969 JG |
542 | |
543 | return id; | |
544 | } | |
545 | ||
81f35161 | 546 | static int blkif_queue_discard_req(struct request *req, struct blkfront_ring_info *rinfo) |
9f27ee59 | 547 | { |
81f35161 | 548 | struct blkfront_info *info = rinfo->dev_info; |
8f5a695d | 549 | struct blkif_request *ring_req, *final_ring_req; |
9f27ee59 | 550 | unsigned long id; |
33204663 JG |
551 | |
552 | /* Fill out a communications ring structure. */ | |
8f5a695d JG |
553 | id = blkif_ring_get_request(rinfo, req, &final_ring_req); |
554 | ring_req = &rinfo->shadow[id].req; | |
33204663 JG |
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); | |
288dab8a | 560 | if (req_op(req) == REQ_OP_SECURE_ERASE && info->feature_secdiscard) |
33204663 JG |
561 | ring_req->u.discard.flag = BLKIF_DISCARD_SECURE; |
562 | else | |
563 | ring_req->u.discard.flag = 0; | |
564 | ||
8f5a695d JG |
565 | /* Copy the request to the ring page. */ |
566 | *final_ring_req = *ring_req; | |
b94e4b14 | 567 | rinfo->shadow[id].status = REQ_WAITING; |
33204663 JG |
568 | |
569 | return 0; | |
570 | } | |
571 | ||
c004a6fe JG |
572 | struct setup_rw_req { |
573 | unsigned int grant_idx; | |
574 | struct blkif_request_segment *segments; | |
81f35161 | 575 | struct blkfront_ring_info *rinfo; |
c004a6fe JG |
576 | struct blkif_request *ring_req; |
577 | grant_ref_t gref_head; | |
578 | unsigned int id; | |
08719dd9 | 579 | /* Only used when persistent grant is used and it's a write request */ |
c004a6fe JG |
580 | bool need_copy; |
581 | unsigned int bvec_off; | |
582 | char *bvec_data; | |
6cc56833 JG |
583 | |
584 | bool require_extra_req; | |
585 | struct blkif_request *extra_ring_req; | |
c004a6fe JG |
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; | |
9f27ee59 | 594 | unsigned int fsect, lsect; |
c004a6fe JG |
595 | /* Convenient aliases */ |
596 | unsigned int grant_idx = setup->grant_idx; | |
597 | struct blkif_request *ring_req = setup->ring_req; | |
81f35161 | 598 | struct blkfront_ring_info *rinfo = setup->rinfo; |
6cc56833 JG |
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 | */ | |
81f35161 | 605 | struct blk_shadow *shadow = &rinfo->shadow[setup->id]; |
c004a6fe | 606 | |
6cc56833 JG |
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 | ||
c004a6fe JG |
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; | |
73716df7 | 623 | gnt_list_entry = get_indirect_grant(&setup->gref_head, rinfo); |
c004a6fe JG |
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 | ||
73716df7 | 629 | gnt_list_entry = get_grant(&setup->gref_head, gfn, rinfo); |
c004a6fe | 630 | ref = gnt_list_entry->gref; |
6cc56833 JG |
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; | |
c004a6fe JG |
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 | ||
6cc56833 JG |
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 | ||
81f35161 | 696 | static int blkif_queue_rw_req(struct request *req, struct blkfront_ring_info *rinfo) |
9f27ee59 | 697 | { |
81f35161 | 698 | struct blkfront_info *info = rinfo->dev_info; |
6cc56833 | 699 | struct blkif_request *ring_req, *extra_ring_req = NULL; |
8f5a695d | 700 | struct blkif_request *final_ring_req, *final_extra_ring_req = NULL; |
6cc56833 JG |
701 | unsigned long id, extra_id = NO_ASSOCIATED_ID; |
702 | bool require_extra_req = false; | |
c004a6fe JG |
703 | int i; |
704 | struct setup_rw_req setup = { | |
705 | .grant_idx = 0, | |
706 | .segments = NULL, | |
81f35161 | 707 | .rinfo = rinfo, |
c004a6fe JG |
708 | .need_copy = rq_data_dir(req) && info->feature_persistent, |
709 | }; | |
0a8704a5 RPM |
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 | */ | |
bd912ef3 | 716 | bool new_persistent_gnts = false; |
9e973e64 | 717 | struct scatterlist *sg; |
c004a6fe | 718 | int num_sg, max_grefs, num_grant; |
9f27ee59 | 719 | |
c004a6fe | 720 | max_grefs = req->nr_phys_segments * GRANTS_PER_PSEG; |
c47206e2 RPM |
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 | */ | |
c004a6fe | 726 | max_grefs += INDIRECT_GREFS(max_grefs); |
402b27f9 | 727 | |
bd912ef3 DZ |
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) { | |
0a8704a5 | 735 | gnttab_request_free_callback( |
81f35161 | 736 | &rinfo->callback, |
0a8704a5 | 737 | blkif_restart_queue_callback, |
81f35161 | 738 | rinfo, |
bd912ef3 | 739 | max_grefs - rinfo->persistent_gnts_c); |
0a8704a5 RPM |
740 | return 1; |
741 | } | |
bd912ef3 | 742 | } |
9f27ee59 JF |
743 | |
744 | /* Fill out a communications ring structure. */ | |
8f5a695d JG |
745 | id = blkif_ring_get_request(rinfo, req, &final_ring_req); |
746 | ring_req = &rinfo->shadow[id].req; | |
9f27ee59 | 747 | |
81f35161 | 748 | num_sg = blk_rq_map_sg(req->q, req, rinfo->shadow[id].sg); |
c004a6fe JG |
749 | num_grant = 0; |
750 | /* Calculate the number of grant used */ | |
81f35161 | 751 | for_each_sg(rinfo->shadow[id].sg, sg, num_sg, i) |
c004a6fe JG |
752 | num_grant += gnttab_count_grant(sg->offset, sg->length); |
753 | ||
6cc56833 JG |
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 | ||
81f35161 | 758 | rinfo->shadow[id].num_sg = num_sg; |
6cc56833 JG |
759 | if (num_grant > BLKIF_MAX_SEGMENTS_PER_REQUEST && |
760 | likely(!require_extra_req)) { | |
33204663 JG |
761 | /* |
762 | * The indirect operation can only be a BLKIF_OP_READ or | |
763 | * BLKIF_OP_WRITE | |
764 | */ | |
3a5e02ce | 765 | BUG_ON(req_op(req) == REQ_OP_FLUSH || req->cmd_flags & REQ_FUA); |
33204663 JG |
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; | |
c004a6fe | 771 | ring_req->u.indirect.nr_segments = num_grant; |
ed30bf31 | 772 | } else { |
33204663 JG |
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; | |
3a5e02ce | 777 | if (req_op(req) == REQ_OP_FLUSH || req->cmd_flags & REQ_FUA) { |
402b27f9 | 778 | /* |
33204663 JG |
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.) | |
402b27f9 | 784 | */ |
a418090a | 785 | if (info->feature_flush && info->feature_fua) |
33204663 JG |
786 | ring_req->operation = |
787 | BLKIF_OP_WRITE_BARRIER; | |
a418090a | 788 | else if (info->feature_flush) |
33204663 JG |
789 | ring_req->operation = |
790 | BLKIF_OP_FLUSH_DISKCACHE; | |
a418090a | 791 | else |
33204663 | 792 | ring_req->operation = 0; |
402b27f9 | 793 | } |
c004a6fe | 794 | ring_req->u.rw.nr_segments = num_grant; |
6cc56833 JG |
795 | if (unlikely(require_extra_req)) { |
796 | extra_id = blkif_ring_get_request(rinfo, req, | |
8f5a695d JG |
797 | &final_extra_ring_req); |
798 | extra_ring_req = &rinfo->shadow[extra_id].req; | |
799 | ||
6cc56833 JG |
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 | } | |
33204663 | 812 | } |
0a8704a5 | 813 | |
c004a6fe JG |
814 | setup.ring_req = ring_req; |
815 | setup.id = id; | |
6cc56833 JG |
816 | |
817 | setup.require_extra_req = require_extra_req; | |
818 | if (unlikely(require_extra_req)) | |
819 | setup.extra_ring_req = extra_ring_req; | |
820 | ||
81f35161 | 821 | for_each_sg(rinfo->shadow[id].sg, sg, num_sg, i) { |
c004a6fe | 822 | BUG_ON(sg->offset + sg->length > PAGE_SIZE); |
0a8704a5 | 823 | |
c004a6fe JG |
824 | if (setup.need_copy) { |
825 | setup.bvec_off = sg->offset; | |
826 | setup.bvec_data = kmap_atomic(sg_page(sg)); | |
827 | } | |
0a8704a5 | 828 | |
c004a6fe JG |
829 | gnttab_foreach_grant_in_range(sg_page(sg), |
830 | sg->offset, | |
831 | sg->length, | |
832 | blkif_setup_rw_req_grant, | |
833 | &setup); | |
0a8704a5 | 834 | |
c004a6fe JG |
835 | if (setup.need_copy) |
836 | kunmap_atomic(setup.bvec_data); | |
9f27ee59 | 837 | } |
c004a6fe JG |
838 | if (setup.segments) |
839 | kunmap_atomic(setup.segments); | |
9f27ee59 | 840 | |
8f5a695d JG |
841 | /* Copy request(s) to the ring page. */ |
842 | *final_ring_req = *ring_req; | |
b94e4b14 JG |
843 | rinfo->shadow[id].status = REQ_WAITING; |
844 | if (unlikely(require_extra_req)) { | |
8f5a695d | 845 | *final_extra_ring_req = *extra_ring_req; |
b94e4b14 JG |
846 | rinfo->shadow[extra_id].status = REQ_WAITING; |
847 | } | |
9f27ee59 | 848 | |
bd912ef3 | 849 | if (new_persistent_gnts) |
c004a6fe | 850 | gnttab_free_grant_references(setup.gref_head); |
9f27ee59 JF |
851 | |
852 | return 0; | |
853 | } | |
854 | ||
33204663 JG |
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 | */ | |
81f35161 | 861 | static int blkif_queue_request(struct request *req, struct blkfront_ring_info *rinfo) |
33204663 | 862 | { |
81f35161 | 863 | if (unlikely(rinfo->dev_info->connected != BLKIF_STATE_CONNECTED)) |
33204663 JG |
864 | return 1; |
865 | ||
c2df40df | 866 | if (unlikely(req_op(req) == REQ_OP_DISCARD || |
288dab8a | 867 | req_op(req) == REQ_OP_SECURE_ERASE)) |
81f35161 | 868 | return blkif_queue_discard_req(req, rinfo); |
33204663 | 869 | else |
81f35161 | 870 | return blkif_queue_rw_req(req, rinfo); |
33204663 | 871 | } |
9f27ee59 | 872 | |
81f35161 | 873 | static inline void flush_requests(struct blkfront_ring_info *rinfo) |
9f27ee59 JF |
874 | { |
875 | int notify; | |
876 | ||
81f35161 | 877 | RING_PUSH_REQUESTS_AND_CHECK_NOTIFY(&rinfo->ring, notify); |
9f27ee59 JF |
878 | |
879 | if (notify) | |
81f35161 | 880 | notify_remote_via_irq(rinfo->irq); |
9f27ee59 JF |
881 | } |
882 | ||
ad42d391 VK |
883 | static inline bool blkif_request_flush_invalid(struct request *req, |
884 | struct blkfront_info *info) | |
0f1ca65e | 885 | { |
aebf526b | 886 | return (blk_rq_is_passthrough(req) || |
3a5e02ce | 887 | ((req_op(req) == REQ_OP_FLUSH) && |
a418090a | 888 | !info->feature_flush) || |
ad42d391 | 889 | ((req->cmd_flags & REQ_FUA) && |
a418090a | 890 | !info->feature_fua)); |
0f1ca65e AA |
891 | } |
892 | ||
fc17b653 | 893 | static blk_status_t blkif_queue_rq(struct blk_mq_hw_ctx *hctx, |
6f03a7ff | 894 | const struct blk_mq_queue_data *qd) |
9f27ee59 | 895 | { |
11659569 | 896 | unsigned long flags; |
2a6f71ad BL |
897 | int qid = hctx->queue_num; |
898 | struct blkfront_info *info = hctx->queue->queuedata; | |
899 | struct blkfront_ring_info *rinfo = NULL; | |
9f27ee59 | 900 | |
4ab50af6 | 901 | rinfo = get_rinfo(info, qid); |
907c3eb1 | 902 | blk_mq_start_request(qd->rq); |
11659569 | 903 | spin_lock_irqsave(&rinfo->ring_lock, flags); |
81f35161 | 904 | if (RING_FULL(&rinfo->ring)) |
907c3eb1 | 905 | goto out_busy; |
9f27ee59 | 906 | |
81f35161 | 907 | if (blkif_request_flush_invalid(qd->rq, rinfo->dev_info)) |
907c3eb1 | 908 | goto out_err; |
296b2f6a | 909 | |
81f35161 | 910 | if (blkif_queue_request(qd->rq, rinfo)) |
907c3eb1 | 911 | goto out_busy; |
296b2f6a | 912 | |
81f35161 | 913 | flush_requests(rinfo); |
11659569 | 914 | spin_unlock_irqrestore(&rinfo->ring_lock, flags); |
fc17b653 | 915 | return BLK_STS_OK; |
9f27ee59 | 916 | |
907c3eb1 | 917 | out_err: |
11659569 | 918 | spin_unlock_irqrestore(&rinfo->ring_lock, flags); |
fc17b653 | 919 | return BLK_STS_IOERR; |
9f27ee59 | 920 | |
907c3eb1 | 921 | out_busy: |
907c3eb1 | 922 | blk_mq_stop_hw_queue(hctx); |
4b422cb9 | 923 | spin_unlock_irqrestore(&rinfo->ring_lock, flags); |
86ff7c2a | 924 | return BLK_STS_DEV_RESOURCE; |
9f27ee59 JF |
925 | } |
926 | ||
2609587c CH |
927 | static void blkif_complete_rq(struct request *rq) |
928 | { | |
929 | blk_mq_end_request(rq, blkif_req(rq)->error); | |
930 | } | |
931 | ||
f363b089 | 932 | static const struct blk_mq_ops blkfront_mq_ops = { |
907c3eb1 | 933 | .queue_rq = blkif_queue_rq, |
2609587c | 934 | .complete = blkif_complete_rq, |
907c3eb1 BL |
935 | }; |
936 | ||
172335ad BL |
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 | ||
8b904b5b | 944 | blk_queue_flag_set(QUEUE_FLAG_VIRT, rq); |
172335ad BL |
945 | |
946 | if (info->feature_discard) { | |
172335ad | 947 | blk_queue_max_discard_sectors(rq, get_capacity(gd)); |
0549cd67 RPM |
948 | rq->limits.discard_granularity = info->discard_granularity ?: |
949 | info->physical_sector_size; | |
172335ad BL |
950 | rq->limits.discard_alignment = info->discard_alignment; |
951 | if (info->feature_secdiscard) | |
44abff2c CH |
952 | blk_queue_max_secure_erase_sectors(rq, |
953 | get_capacity(gd)); | |
172335ad BL |
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); | |
172335ad BL |
970 | } |
971 | ||
a418090a | 972 | static const char *flush_info(struct blkfront_info *info) |
fdf9b965 | 973 | { |
a418090a | 974 | if (info->feature_flush && info->feature_fua) |
fdf9b965 | 975 | return "barrier: enabled;"; |
a418090a | 976 | else if (info->feature_flush) |
fdf9b965 | 977 | return "flush diskcache: enabled;"; |
a418090a | 978 | else |
fdf9b965 | 979 | return "barrier or flush: disabled;"; |
fdf9b965 | 980 | } |
9f27ee59 | 981 | |
4913efe4 | 982 | static void xlvbd_flush(struct blkfront_info *info) |
9f27ee59 | 983 | { |
a418090a MC |
984 | blk_queue_write_cache(info->rq, info->feature_flush ? true : false, |
985 | info->feature_fua ? true : false); | |
fdf9b965 | 986 | pr_info("blkfront: %s: %s %s %s %s %s\n", |
a418090a | 987 | info->gd->disk_name, flush_info(info), |
fdf9b965 VK |
988 | "persistent grants:", info->feature_persistent ? |
989 | "enabled;" : "disabled;", "indirect descriptors:", | |
990 | info->max_indirect_segments ? "enabled;" : "disabled;"); | |
9f27ee59 JF |
991 | } |
992 | ||
c80a4209 SS |
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 | } | |
9f27ee59 | 1052 | |
e77c78c0 JB |
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 | ||
9246b5f0 | 1061 | static int xlvbd_alloc_gendisk(blkif_sector_t capacity, |
1a827ce1 CH |
1062 | struct blkfront_info *info, u16 sector_size, |
1063 | unsigned int physical_sector_size) | |
9f27ee59 JF |
1064 | { |
1065 | struct gendisk *gd; | |
1066 | int nr_minors = 1; | |
c80a4209 | 1067 | int err; |
9246b5f0 CL |
1068 | unsigned int offset; |
1069 | int minor; | |
1070 | int nr_parts; | |
e77c78c0 | 1071 | char *ptr; |
9f27ee59 JF |
1072 | |
1073 | BUG_ON(info->gd != NULL); | |
1074 | BUG_ON(info->rq != NULL); | |
1075 | ||
9246b5f0 CL |
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)) { | |
c80a4209 SS |
1083 | err = xen_translate_vdev(info->vdevice, &minor, &offset); |
1084 | if (err) | |
589b7289 NC |
1085 | return err; |
1086 | nr_parts = PARTS_PER_DISK; | |
9246b5f0 CL |
1087 | } else { |
1088 | minor = BLKIF_MINOR_EXT(info->vdevice); | |
1089 | nr_parts = PARTS_PER_EXT_DISK; | |
c80a4209 | 1090 | offset = minor / nr_parts; |
89153b5c | 1091 | if (xen_hvm_domain() && offset < EMULATED_HD_DISK_NAME_OFFSET + 4) |
c80a4209 SS |
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); | |
9246b5f0 | 1095 | } |
e77c78c0 JB |
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 | } | |
9246b5f0 CL |
1101 | |
1102 | if ((minor % nr_parts) == 0) | |
1103 | nr_minors = nr_parts; | |
9f27ee59 | 1104 | |
0e345826 JB |
1105 | err = xlbd_reserve_minors(minor, nr_minors); |
1106 | if (err) | |
3b62c140 | 1107 | return err; |
0e345826 | 1108 | |
3b62c140 CH |
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 | } | |
9f27ee59 | 1136 | |
e77c78c0 JB |
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)); | |
9f27ee59 JF |
1145 | |
1146 | gd->major = XENVBD_MAJOR; | |
1147 | gd->first_minor = minor; | |
3b62c140 | 1148 | gd->minors = nr_minors; |
9f27ee59 JF |
1149 | gd->fops = &xlvbd_block_fops; |
1150 | gd->private_data = info; | |
9f27ee59 JF |
1151 | set_capacity(gd, capacity); |
1152 | ||
3b62c140 CH |
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); | |
9f27ee59 | 1158 | |
4913efe4 | 1159 | xlvbd_flush(info); |
9f27ee59 | 1160 | |
1a827ce1 | 1161 | if (info->vdisk_info & VDISK_READONLY) |
9f27ee59 | 1162 | set_disk_ro(gd, 1); |
1a827ce1 | 1163 | if (info->vdisk_info & VDISK_REMOVABLE) |
9f27ee59 JF |
1164 | gd->flags |= GENHD_FL_REMOVABLE; |
1165 | ||
9f27ee59 JF |
1166 | return 0; |
1167 | ||
3b62c140 CH |
1168 | out_free_tag_set: |
1169 | blk_mq_free_tag_set(&info->tag_set); | |
1170 | out_release_minors: | |
0e345826 | 1171 | xlbd_release_minors(minor, nr_minors); |
9f27ee59 JF |
1172 | return err; |
1173 | } | |
1174 | ||
11659569 BL |
1175 | /* Already hold rinfo->ring_lock. */ |
1176 | static inline void kick_pending_request_queues_locked(struct blkfront_ring_info *rinfo) | |
9f27ee59 | 1177 | { |
81f35161 BL |
1178 | if (!RING_FULL(&rinfo->ring)) |
1179 | blk_mq_start_stopped_hw_queues(rinfo->dev_info->rq, true); | |
9f27ee59 JF |
1180 | } |
1181 | ||
11659569 BL |
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 | ||
9f27ee59 JF |
1191 | static void blkif_restart_queue(struct work_struct *work) |
1192 | { | |
81f35161 | 1193 | struct blkfront_ring_info *rinfo = container_of(work, struct blkfront_ring_info, work); |
9f27ee59 | 1194 | |
81f35161 BL |
1195 | if (rinfo->dev_info->connected == BLKIF_STATE_CONNECTED) |
1196 | kick_pending_request_queues(rinfo); | |
9f27ee59 JF |
1197 | } |
1198 | ||
3df0e505 | 1199 | static void blkif_free_ring(struct blkfront_ring_info *rinfo) |
9f27ee59 | 1200 | { |
73716df7 | 1201 | struct grant *persistent_gnt, *n; |
3df0e505 | 1202 | struct blkfront_info *info = rinfo->dev_info; |
402b27f9 | 1203 | int i, j, segs; |
0a8704a5 | 1204 | |
bfe11d6d RPM |
1205 | /* |
1206 | * Remove indirect pages, this only happens when using indirect | |
1207 | * descriptors but not persistent grants | |
1208 | */ | |
81f35161 | 1209 | if (!list_empty(&rinfo->indirect_pages)) { |
bfe11d6d RPM |
1210 | struct page *indirect_page, *n; |
1211 | ||
1212 | BUG_ON(info->feature_persistent); | |
81f35161 | 1213 | list_for_each_entry_safe(indirect_page, n, &rinfo->indirect_pages, lru) { |
bfe11d6d RPM |
1214 | list_del(&indirect_page->lru); |
1215 | __free_page(indirect_page); | |
1216 | } | |
1217 | } | |
1218 | ||
73716df7 BL |
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, | |
c94b731d | 1226 | 0UL); |
73716df7 BL |
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 | ||
86839c56 | 1236 | for (i = 0; i < BLK_RING_SIZE(info); i++) { |
402b27f9 RPM |
1237 | /* |
1238 | * Clear persistent grants present in requests already | |
1239 | * on the shared ring | |
1240 | */ | |
81f35161 | 1241 | if (!rinfo->shadow[i].request) |
402b27f9 RPM |
1242 | goto free_shadow; |
1243 | ||
81f35161 BL |
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; | |
402b27f9 | 1247 | for (j = 0; j < segs; j++) { |
81f35161 | 1248 | persistent_gnt = rinfo->shadow[i].grants_used[j]; |
c94b731d | 1249 | gnttab_end_foreign_access(persistent_gnt->gref, 0UL); |
bfe11d6d | 1250 | if (info->feature_persistent) |
a7a6df22 | 1251 | __free_page(persistent_gnt->page); |
402b27f9 RPM |
1252 | kfree(persistent_gnt); |
1253 | } | |
1254 | ||
81f35161 | 1255 | if (rinfo->shadow[i].req.operation != BLKIF_OP_INDIRECT) |
402b27f9 RPM |
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++) { | |
81f35161 | 1263 | persistent_gnt = rinfo->shadow[i].indirect_grants[j]; |
c94b731d | 1264 | gnttab_end_foreign_access(persistent_gnt->gref, 0UL); |
a7a6df22 | 1265 | __free_page(persistent_gnt->page); |
402b27f9 RPM |
1266 | kfree(persistent_gnt); |
1267 | } | |
1268 | ||
1269 | free_shadow: | |
1d5c76e6 | 1270 | kvfree(rinfo->shadow[i].grants_used); |
81f35161 | 1271 | rinfo->shadow[i].grants_used = NULL; |
1d5c76e6 | 1272 | kvfree(rinfo->shadow[i].indirect_grants); |
81f35161 | 1273 | rinfo->shadow[i].indirect_grants = NULL; |
1d5c76e6 | 1274 | kvfree(rinfo->shadow[i].sg); |
81f35161 | 1275 | rinfo->shadow[i].sg = NULL; |
402b27f9 RPM |
1276 | } |
1277 | ||
9f27ee59 | 1278 | /* No more gnttab callback work. */ |
81f35161 | 1279 | gnttab_cancel_free_callback(&rinfo->callback); |
9f27ee59 JF |
1280 | |
1281 | /* Flush gnttab callback work. Must be done with no locks held. */ | |
81f35161 | 1282 | flush_work(&rinfo->work); |
9f27ee59 JF |
1283 | |
1284 | /* Free resources associated with old device channel. */ | |
86839c56 | 1285 | for (i = 0; i < info->nr_ring_pages; i++) { |
81f35161 | 1286 | if (rinfo->ring_ref[i] != GRANT_INVALID_REF) { |
c94b731d | 1287 | gnttab_end_foreign_access(rinfo->ring_ref[i], 0); |
81f35161 | 1288 | rinfo->ring_ref[i] = GRANT_INVALID_REF; |
86839c56 | 1289 | } |
9f27ee59 | 1290 | } |
abf1fd59 JG |
1291 | free_pages_exact(rinfo->ring.sring, |
1292 | info->nr_ring_pages * XEN_PAGE_SIZE); | |
81f35161 | 1293 | rinfo->ring.sring = NULL; |
86839c56 | 1294 | |
81f35161 BL |
1295 | if (rinfo->irq) |
1296 | unbind_from_irqhandler(rinfo->irq, rinfo); | |
1297 | rinfo->evtchn = rinfo->irq = 0; | |
3df0e505 | 1298 | } |
9f27ee59 | 1299 | |
3df0e505 BL |
1300 | static void blkif_free(struct blkfront_info *info, int suspend) |
1301 | { | |
3df0e505 | 1302 | unsigned int i; |
4ab50af6 | 1303 | struct blkfront_ring_info *rinfo; |
3df0e505 BL |
1304 | |
1305 | /* Prevent new requests being issued until we fix things up. */ | |
3df0e505 BL |
1306 | info->connected = suspend ? |
1307 | BLKIF_STATE_SUSPENDED : BLKIF_STATE_DISCONNECTED; | |
1308 | /* No more blkif_request(). */ | |
1309 | if (info->rq) | |
1310 | blk_mq_stop_hw_queues(info->rq); | |
1311 | ||
4ab50af6 JG |
1312 | for_each_rinfo(info, rinfo, i) |
1313 | blkif_free_ring(rinfo); | |
3df0e505 | 1314 | |
1d5c76e6 | 1315 | kvfree(info->rinfo); |
3df0e505 BL |
1316 | info->rinfo = NULL; |
1317 | info->nr_rings = 0; | |
9f27ee59 JF |
1318 | } |
1319 | ||
c004a6fe JG |
1320 | struct copy_from_grant { |
1321 | const struct blk_shadow *s; | |
1322 | unsigned int grant_idx; | |
1323 | unsigned int bvec_offset; | |
1324 | char *bvec_data; | |
1325 | }; | |
1326 | ||
1327 | static void blkif_copy_from_grant(unsigned long gfn, unsigned int offset, | |
1328 | unsigned int len, void *data) | |
1329 | { | |
1330 | struct copy_from_grant *info = data; | |
1331 | char *shared_data; | |
1332 | /* Convenient aliases */ | |
1333 | const struct blk_shadow *s = info->s; | |
1334 | ||
1335 | shared_data = kmap_atomic(s->grants_used[info->grant_idx]->page); | |
1336 | ||
1337 | memcpy(info->bvec_data + info->bvec_offset, | |
1338 | shared_data + offset, len); | |
1339 | ||
1340 | info->bvec_offset += len; | |
1341 | info->grant_idx++; | |
1342 | ||
1343 | kunmap_atomic(shared_data); | |
1344 | } | |
1345 | ||
6cc56833 JG |
1346 | static enum blk_req_status blkif_rsp_to_req_status(int rsp) |
1347 | { | |
1348 | switch (rsp) | |
1349 | { | |
1350 | case BLKIF_RSP_OKAY: | |
1351 | return REQ_DONE; | |
1352 | case BLKIF_RSP_EOPNOTSUPP: | |
1353 | return REQ_EOPNOTSUPP; | |
1354 | case BLKIF_RSP_ERROR: | |
6cc56833 JG |
1355 | default: |
1356 | return REQ_ERROR; | |
1357 | } | |
1358 | } | |
1359 | ||
1360 | /* | |
1361 | * Get the final status of the block request based on two ring response | |
1362 | */ | |
1363 | static int blkif_get_final_status(enum blk_req_status s1, | |
1364 | enum blk_req_status s2) | |
1365 | { | |
b94e4b14 JG |
1366 | BUG_ON(s1 < REQ_DONE); |
1367 | BUG_ON(s2 < REQ_DONE); | |
6cc56833 JG |
1368 | |
1369 | if (s1 == REQ_ERROR || s2 == REQ_ERROR) | |
1370 | return BLKIF_RSP_ERROR; | |
1371 | else if (s1 == REQ_EOPNOTSUPP || s2 == REQ_EOPNOTSUPP) | |
1372 | return BLKIF_RSP_EOPNOTSUPP; | |
1373 | return BLKIF_RSP_OKAY; | |
1374 | } | |
1375 | ||
abf1fd59 JG |
1376 | /* |
1377 | * Return values: | |
1378 | * 1 response processed. | |
1379 | * 0 missing further responses. | |
1380 | * -1 error while processing. | |
1381 | */ | |
1382 | static int blkif_completion(unsigned long *id, | |
1383 | struct blkfront_ring_info *rinfo, | |
1384 | struct blkif_response *bret) | |
9f27ee59 | 1385 | { |
d62f6918 | 1386 | int i = 0; |
b7649158 | 1387 | struct scatterlist *sg; |
c004a6fe | 1388 | int num_sg, num_grant; |
81f35161 | 1389 | struct blkfront_info *info = rinfo->dev_info; |
6cc56833 | 1390 | struct blk_shadow *s = &rinfo->shadow[*id]; |
c004a6fe | 1391 | struct copy_from_grant data = { |
c004a6fe JG |
1392 | .grant_idx = 0, |
1393 | }; | |
402b27f9 | 1394 | |
c004a6fe | 1395 | num_grant = s->req.operation == BLKIF_OP_INDIRECT ? |
402b27f9 | 1396 | s->req.u.indirect.nr_segments : s->req.u.rw.nr_segments; |
6cc56833 JG |
1397 | |
1398 | /* The I/O request may be split in two. */ | |
1399 | if (unlikely(s->associated_id != NO_ASSOCIATED_ID)) { | |
1400 | struct blk_shadow *s2 = &rinfo->shadow[s->associated_id]; | |
1401 | ||
1402 | /* Keep the status of the current response in shadow. */ | |
1403 | s->status = blkif_rsp_to_req_status(bret->status); | |
1404 | ||
1405 | /* Wait the second response if not yet here. */ | |
b94e4b14 | 1406 | if (s2->status < REQ_DONE) |
abf1fd59 | 1407 | return 0; |
6cc56833 JG |
1408 | |
1409 | bret->status = blkif_get_final_status(s->status, | |
1410 | s2->status); | |
1411 | ||
1412 | /* | |
1413 | * All the grants is stored in the first shadow in order | |
1414 | * to make the completion code simpler. | |
1415 | */ | |
1416 | num_grant += s2->req.u.rw.nr_segments; | |
1417 | ||
1418 | /* | |
1419 | * The two responses may not come in order. Only the | |
1420 | * first request will store the scatter-gather list. | |
1421 | */ | |
1422 | if (s2->num_sg != 0) { | |
1423 | /* Update "id" with the ID of the first response. */ | |
1424 | *id = s->associated_id; | |
1425 | s = s2; | |
1426 | } | |
1427 | ||
1428 | /* | |
1429 | * We don't need anymore the second request, so recycling | |
1430 | * it now. | |
1431 | */ | |
1432 | if (add_id_to_freelist(rinfo, s->associated_id)) | |
1433 | WARN(1, "%s: can't recycle the second part (id = %ld) of the request\n", | |
1434 | info->gd->disk_name, s->associated_id); | |
1435 | } | |
1436 | ||
1437 | data.s = s; | |
c004a6fe | 1438 | num_sg = s->num_sg; |
0a8704a5 | 1439 | |
bfe11d6d | 1440 | if (bret->operation == BLKIF_OP_READ && info->feature_persistent) { |
c004a6fe | 1441 | for_each_sg(s->sg, sg, num_sg, i) { |
b7649158 | 1442 | BUG_ON(sg->offset + sg->length > PAGE_SIZE); |
c004a6fe JG |
1443 | |
1444 | data.bvec_offset = sg->offset; | |
1445 | data.bvec_data = kmap_atomic(sg_page(sg)); | |
1446 | ||
1447 | gnttab_foreach_grant_in_range(sg_page(sg), | |
1448 | sg->offset, | |
1449 | sg->length, | |
1450 | blkif_copy_from_grant, | |
1451 | &data); | |
1452 | ||
1453 | kunmap_atomic(data.bvec_data); | |
0a8704a5 RPM |
1454 | } |
1455 | } | |
1456 | /* Add the persistent grant into the list of free grants */ | |
c004a6fe | 1457 | for (i = 0; i < num_grant; i++) { |
abf1fd59 | 1458 | if (!gnttab_try_end_foreign_access(s->grants_used[i]->gref)) { |
fbe363c4 RPM |
1459 | /* |
1460 | * If the grant is still mapped by the backend (the | |
1461 | * backend has chosen to make this grant persistent) | |
1462 | * we add it at the head of the list, so it will be | |
1463 | * reused first. | |
1464 | */ | |
abf1fd59 JG |
1465 | if (!info->feature_persistent) { |
1466 | pr_alert("backed has not unmapped grant: %u\n", | |
1467 | s->grants_used[i]->gref); | |
1468 | return -1; | |
1469 | } | |
73716df7 BL |
1470 | list_add(&s->grants_used[i]->node, &rinfo->grants); |
1471 | rinfo->persistent_gnts_c++; | |
fbe363c4 RPM |
1472 | } else { |
1473 | /* | |
abf1fd59 JG |
1474 | * If the grant is not mapped by the backend we add it |
1475 | * to the tail of the list, so it will not be picked | |
1476 | * again unless we run out of persistent grants. | |
fbe363c4 | 1477 | */ |
fbe363c4 | 1478 | s->grants_used[i]->gref = GRANT_INVALID_REF; |
73716df7 | 1479 | list_add_tail(&s->grants_used[i]->node, &rinfo->grants); |
fbe363c4 | 1480 | } |
0a8704a5 | 1481 | } |
402b27f9 | 1482 | if (s->req.operation == BLKIF_OP_INDIRECT) { |
c004a6fe | 1483 | for (i = 0; i < INDIRECT_GREFS(num_grant); i++) { |
abf1fd59 JG |
1484 | if (!gnttab_try_end_foreign_access(s->indirect_grants[i]->gref)) { |
1485 | if (!info->feature_persistent) { | |
1486 | pr_alert("backed has not unmapped grant: %u\n", | |
1487 | s->indirect_grants[i]->gref); | |
1488 | return -1; | |
1489 | } | |
73716df7 BL |
1490 | list_add(&s->indirect_grants[i]->node, &rinfo->grants); |
1491 | rinfo->persistent_gnts_c++; | |
fbe363c4 | 1492 | } else { |
bfe11d6d RPM |
1493 | struct page *indirect_page; |
1494 | ||
bfe11d6d RPM |
1495 | /* |
1496 | * Add the used indirect page back to the list of | |
1497 | * available pages for indirect grefs. | |
1498 | */ | |
7b076750 | 1499 | if (!info->feature_persistent) { |
a7a6df22 | 1500 | indirect_page = s->indirect_grants[i]->page; |
81f35161 | 1501 | list_add(&indirect_page->lru, &rinfo->indirect_pages); |
7b076750 | 1502 | } |
fbe363c4 | 1503 | s->indirect_grants[i]->gref = GRANT_INVALID_REF; |
73716df7 | 1504 | list_add_tail(&s->indirect_grants[i]->node, &rinfo->grants); |
fbe363c4 | 1505 | } |
402b27f9 RPM |
1506 | } |
1507 | } | |
6cc56833 | 1508 | |
abf1fd59 | 1509 | return 1; |
9f27ee59 JF |
1510 | } |
1511 | ||
1512 | static irqreturn_t blkif_interrupt(int irq, void *dev_id) | |
1513 | { | |
1514 | struct request *req; | |
71b66243 | 1515 | struct blkif_response bret; |
9f27ee59 JF |
1516 | RING_IDX i, rp; |
1517 | unsigned long flags; | |
81f35161 BL |
1518 | struct blkfront_ring_info *rinfo = (struct blkfront_ring_info *)dev_id; |
1519 | struct blkfront_info *info = rinfo->dev_info; | |
0fd08a34 | 1520 | unsigned int eoiflag = XEN_EOI_FLAG_SPURIOUS; |
9f27ee59 | 1521 | |
0fd08a34 JG |
1522 | if (unlikely(info->connected != BLKIF_STATE_CONNECTED)) { |
1523 | xen_irq_lateeoi(irq, XEN_EOI_FLAG_SPURIOUS); | |
9f27ee59 | 1524 | return IRQ_HANDLED; |
0fd08a34 | 1525 | } |
9f27ee59 | 1526 | |
11659569 | 1527 | spin_lock_irqsave(&rinfo->ring_lock, flags); |
9f27ee59 | 1528 | again: |
b94e4b14 JG |
1529 | rp = READ_ONCE(rinfo->ring.sring->rsp_prod); |
1530 | virt_rmb(); /* Ensure we see queued responses up to 'rp'. */ | |
1531 | if (RING_RESPONSE_PROD_OVERFLOW(&rinfo->ring, rp)) { | |
1532 | pr_alert("%s: illegal number of responses %u\n", | |
1533 | info->gd->disk_name, rp - rinfo->ring.rsp_cons); | |
1534 | goto err; | |
1535 | } | |
9f27ee59 | 1536 | |
81f35161 | 1537 | for (i = rinfo->ring.rsp_cons; i != rp; i++) { |
9f27ee59 | 1538 | unsigned long id; |
b94e4b14 | 1539 | unsigned int op; |
9f27ee59 | 1540 | |
0fd08a34 JG |
1541 | eoiflag = 0; |
1542 | ||
71b66243 JG |
1543 | RING_COPY_RESPONSE(&rinfo->ring, i, &bret); |
1544 | id = bret.id; | |
9f27ee59 | 1545 | |
6878c32e KRW |
1546 | /* |
1547 | * The backend has messed up and given us an id that we would | |
1548 | * never have given to it (we stamp it up to BLK_RING_SIZE - | |
1549 | * look in get_id_from_freelist. | |
1550 | */ | |
86839c56 | 1551 | if (id >= BLK_RING_SIZE(info)) { |
b94e4b14 JG |
1552 | pr_alert("%s: response has incorrect id (%ld)\n", |
1553 | info->gd->disk_name, id); | |
1554 | goto err; | |
6878c32e | 1555 | } |
b94e4b14 JG |
1556 | if (rinfo->shadow[id].status != REQ_WAITING) { |
1557 | pr_alert("%s: response references no pending request\n", | |
1558 | info->gd->disk_name); | |
1559 | goto err; | |
6878c32e | 1560 | } |
b94e4b14 JG |
1561 | |
1562 | rinfo->shadow[id].status = REQ_PROCESSING; | |
81f35161 | 1563 | req = rinfo->shadow[id].request; |
9f27ee59 | 1564 | |
b94e4b14 JG |
1565 | op = rinfo->shadow[id].req.operation; |
1566 | if (op == BLKIF_OP_INDIRECT) | |
1567 | op = rinfo->shadow[id].req.u.indirect.indirect_op; | |
1568 | if (bret.operation != op) { | |
1569 | pr_alert("%s: response has wrong operation (%u instead of %u)\n", | |
1570 | info->gd->disk_name, bret.operation, op); | |
1571 | goto err; | |
1572 | } | |
1573 | ||
71b66243 | 1574 | if (bret.operation != BLKIF_OP_DISCARD) { |
abf1fd59 JG |
1575 | int ret; |
1576 | ||
6cc56833 JG |
1577 | /* |
1578 | * We may need to wait for an extra response if the | |
1579 | * I/O request is split in 2 | |
1580 | */ | |
abf1fd59 JG |
1581 | ret = blkif_completion(&id, rinfo, &bret); |
1582 | if (!ret) | |
6cc56833 | 1583 | continue; |
abf1fd59 JG |
1584 | if (unlikely(ret < 0)) |
1585 | goto err; | |
6cc56833 | 1586 | } |
9f27ee59 | 1587 | |
81f35161 | 1588 | if (add_id_to_freelist(rinfo, id)) { |
6878c32e | 1589 | WARN(1, "%s: response to %s (id %ld) couldn't be recycled!\n", |
71b66243 | 1590 | info->gd->disk_name, op_name(bret.operation), id); |
6878c32e KRW |
1591 | continue; |
1592 | } | |
9f27ee59 | 1593 | |
71b66243 | 1594 | if (bret.status == BLKIF_RSP_OKAY) |
2a842aca CH |
1595 | blkif_req(req)->error = BLK_STS_OK; |
1596 | else | |
1597 | blkif_req(req)->error = BLK_STS_IOERR; | |
1598 | ||
71b66243 | 1599 | switch (bret.operation) { |
ed30bf31 | 1600 | case BLKIF_OP_DISCARD: |
71b66243 | 1601 | if (unlikely(bret.status == BLKIF_RSP_EOPNOTSUPP)) { |
ed30bf31 | 1602 | struct request_queue *rq = info->rq; |
b94e4b14 JG |
1603 | |
1604 | pr_warn_ratelimited("blkfront: %s: %s op failed\n", | |
71b66243 | 1605 | info->gd->disk_name, op_name(bret.operation)); |
2a842aca | 1606 | blkif_req(req)->error = BLK_STS_NOTSUPP; |
ed30bf31 | 1607 | info->feature_discard = 0; |
5ea42986 | 1608 | info->feature_secdiscard = 0; |
70200574 | 1609 | blk_queue_max_discard_sectors(rq, 0); |
44abff2c | 1610 | blk_queue_max_secure_erase_sectors(rq, 0); |
ed30bf31 | 1611 | } |
ed30bf31 | 1612 | break; |
edf6ef59 | 1613 | case BLKIF_OP_FLUSH_DISKCACHE: |
9f27ee59 | 1614 | case BLKIF_OP_WRITE_BARRIER: |
71b66243 | 1615 | if (unlikely(bret.status == BLKIF_RSP_EOPNOTSUPP)) { |
b94e4b14 | 1616 | pr_warn_ratelimited("blkfront: %s: %s op failed\n", |
71b66243 | 1617 | info->gd->disk_name, op_name(bret.operation)); |
31c4ccc3 | 1618 | blkif_req(req)->error = BLK_STS_NOTSUPP; |
dcb8baec | 1619 | } |
71b66243 | 1620 | if (unlikely(bret.status == BLKIF_RSP_ERROR && |
81f35161 | 1621 | rinfo->shadow[id].req.u.rw.nr_segments == 0)) { |
b94e4b14 | 1622 | pr_warn_ratelimited("blkfront: %s: empty %s op failed\n", |
71b66243 | 1623 | info->gd->disk_name, op_name(bret.operation)); |
2a842aca | 1624 | blkif_req(req)->error = BLK_STS_NOTSUPP; |
dcb8baec | 1625 | } |
2609587c | 1626 | if (unlikely(blkif_req(req)->error)) { |
2a842aca CH |
1627 | if (blkif_req(req)->error == BLK_STS_NOTSUPP) |
1628 | blkif_req(req)->error = BLK_STS_OK; | |
a418090a | 1629 | info->feature_fua = 0; |
4913efe4 TH |
1630 | info->feature_flush = 0; |
1631 | xlvbd_flush(info); | |
9f27ee59 | 1632 | } |
df561f66 | 1633 | fallthrough; |
9f27ee59 JF |
1634 | case BLKIF_OP_READ: |
1635 | case BLKIF_OP_WRITE: | |
71b66243 | 1636 | if (unlikely(bret.status != BLKIF_RSP_OKAY)) |
b94e4b14 JG |
1637 | dev_dbg_ratelimited(&info->xbdev->dev, |
1638 | "Bad return from blkdev data request: %#x\n", | |
1639 | bret.status); | |
9f27ee59 | 1640 | |
9f27ee59 JF |
1641 | break; |
1642 | default: | |
1643 | BUG(); | |
1644 | } | |
2609587c | 1645 | |
15f73f5b CH |
1646 | if (likely(!blk_should_fake_timeout(req->q))) |
1647 | blk_mq_complete_request(req); | |
9f27ee59 JF |
1648 | } |
1649 | ||
81f35161 | 1650 | rinfo->ring.rsp_cons = i; |
9f27ee59 | 1651 | |
81f35161 | 1652 | if (i != rinfo->ring.req_prod_pvt) { |
9f27ee59 | 1653 | int more_to_do; |
81f35161 | 1654 | RING_FINAL_CHECK_FOR_RESPONSES(&rinfo->ring, more_to_do); |
9f27ee59 JF |
1655 | if (more_to_do) |
1656 | goto again; | |
1657 | } else | |
81f35161 | 1658 | rinfo->ring.sring->rsp_event = i + 1; |
9f27ee59 | 1659 | |
11659569 | 1660 | kick_pending_request_queues_locked(rinfo); |
9f27ee59 | 1661 | |
11659569 | 1662 | spin_unlock_irqrestore(&rinfo->ring_lock, flags); |
9f27ee59 | 1663 | |
0fd08a34 JG |
1664 | xen_irq_lateeoi(irq, eoiflag); |
1665 | ||
9f27ee59 | 1666 | return IRQ_HANDLED; |
b94e4b14 JG |
1667 | |
1668 | err: | |
1669 | info->connected = BLKIF_STATE_ERROR; | |
1670 | ||
1671 | spin_unlock_irqrestore(&rinfo->ring_lock, flags); | |
1672 | ||
0fd08a34 JG |
1673 | /* No EOI in order to avoid further interrupts. */ |
1674 | ||
b94e4b14 JG |
1675 | pr_alert("%s disabled for further use\n", info->gd->disk_name); |
1676 | return IRQ_HANDLED; | |
9f27ee59 JF |
1677 | } |
1678 | ||
1679 | ||
1680 | static int setup_blkring(struct xenbus_device *dev, | |
81f35161 | 1681 | struct blkfront_ring_info *rinfo) |
9f27ee59 JF |
1682 | { |
1683 | struct blkif_sring *sring; | |
86839c56 | 1684 | int err, i; |
81f35161 | 1685 | struct blkfront_info *info = rinfo->dev_info; |
c004a6fe | 1686 | unsigned long ring_size = info->nr_ring_pages * XEN_PAGE_SIZE; |
9cce2914 | 1687 | grant_ref_t gref[XENBUS_MAX_RING_GRANTS]; |
9f27ee59 | 1688 | |
86839c56 | 1689 | for (i = 0; i < info->nr_ring_pages; i++) |
81f35161 | 1690 | rinfo->ring_ref[i] = GRANT_INVALID_REF; |
9f27ee59 | 1691 | |
abf1fd59 | 1692 | sring = alloc_pages_exact(ring_size, GFP_NOIO); |
9f27ee59 JF |
1693 | if (!sring) { |
1694 | xenbus_dev_fatal(dev, -ENOMEM, "allocating shared ring"); | |
1695 | return -ENOMEM; | |
1696 | } | |
1697 | SHARED_RING_INIT(sring); | |
81f35161 | 1698 | FRONT_RING_INIT(&rinfo->ring, sring, ring_size); |
9e973e64 | 1699 | |
81f35161 | 1700 | err = xenbus_grant_ring(dev, rinfo->ring.sring, info->nr_ring_pages, gref); |
9f27ee59 | 1701 | if (err < 0) { |
abf1fd59 | 1702 | free_pages_exact(sring, ring_size); |
81f35161 | 1703 | rinfo->ring.sring = NULL; |
9f27ee59 JF |
1704 | goto fail; |
1705 | } | |
86839c56 | 1706 | for (i = 0; i < info->nr_ring_pages; i++) |
81f35161 | 1707 | rinfo->ring_ref[i] = gref[i]; |
9f27ee59 | 1708 | |
81f35161 | 1709 | err = xenbus_alloc_evtchn(dev, &rinfo->evtchn); |
9f27ee59 JF |
1710 | if (err) |
1711 | goto fail; | |
1712 | ||
0fd08a34 JG |
1713 | err = bind_evtchn_to_irqhandler_lateeoi(rinfo->evtchn, blkif_interrupt, |
1714 | 0, "blkif", rinfo); | |
9f27ee59 JF |
1715 | if (err <= 0) { |
1716 | xenbus_dev_fatal(dev, err, | |
1717 | "bind_evtchn_to_irqhandler failed"); | |
1718 | goto fail; | |
1719 | } | |
81f35161 | 1720 | rinfo->irq = err; |
9f27ee59 JF |
1721 | |
1722 | return 0; | |
1723 | fail: | |
1724 | blkif_free(info, 0); | |
1725 | return err; | |
1726 | } | |
1727 | ||
28d949bc BL |
1728 | /* |
1729 | * Write out per-ring/queue nodes including ring-ref and event-channel, and each | |
1730 | * ring buffer may have multi pages depending on ->nr_ring_pages. | |
1731 | */ | |
1732 | static int write_per_ring_nodes(struct xenbus_transaction xbt, | |
1733 | struct blkfront_ring_info *rinfo, const char *dir) | |
1734 | { | |
1735 | int err; | |
1736 | unsigned int i; | |
1737 | const char *message = NULL; | |
1738 | struct blkfront_info *info = rinfo->dev_info; | |
1739 | ||
1740 | if (info->nr_ring_pages == 1) { | |
1741 | err = xenbus_printf(xbt, dir, "ring-ref", "%u", rinfo->ring_ref[0]); | |
1742 | if (err) { | |
1743 | message = "writing ring-ref"; | |
1744 | goto abort_transaction; | |
1745 | } | |
1746 | } else { | |
1747 | for (i = 0; i < info->nr_ring_pages; i++) { | |
1748 | char ring_ref_name[RINGREF_NAME_LEN]; | |
1749 | ||
1750 | snprintf(ring_ref_name, RINGREF_NAME_LEN, "ring-ref%u", i); | |
1751 | err = xenbus_printf(xbt, dir, ring_ref_name, | |
1752 | "%u", rinfo->ring_ref[i]); | |
1753 | if (err) { | |
1754 | message = "writing ring-ref"; | |
1755 | goto abort_transaction; | |
1756 | } | |
1757 | } | |
1758 | } | |
1759 | ||
1760 | err = xenbus_printf(xbt, dir, "event-channel", "%u", rinfo->evtchn); | |
1761 | if (err) { | |
1762 | message = "writing event-channel"; | |
1763 | goto abort_transaction; | |
1764 | } | |
1765 | ||
1766 | return 0; | |
1767 | ||
1768 | abort_transaction: | |
1769 | xenbus_transaction_end(xbt, 1); | |
1770 | if (message) | |
1771 | xenbus_dev_fatal(info->xbdev, err, "%s", message); | |
1772 | ||
1773 | return err; | |
1774 | } | |
9f27ee59 JF |
1775 | |
1776 | /* Common code used when first setting up, and when resuming. */ | |
203fd61f | 1777 | static int talk_to_blkback(struct xenbus_device *dev, |
9f27ee59 JF |
1778 | struct blkfront_info *info) |
1779 | { | |
1780 | const char *message = NULL; | |
1781 | struct xenbus_transaction xbt; | |
28d949bc | 1782 | int err; |
f27dc1ac JG |
1783 | unsigned int i, max_page_order; |
1784 | unsigned int ring_page_order; | |
4ab50af6 | 1785 | struct blkfront_ring_info *rinfo; |
86839c56 | 1786 | |
7ed8ce1c BD |
1787 | if (!info) |
1788 | return -ENODEV; | |
1789 | ||
f27dc1ac JG |
1790 | max_page_order = xenbus_read_unsigned(info->xbdev->otherend, |
1791 | "max-ring-page-order", 0); | |
1792 | ring_page_order = min(xen_blkif_max_ring_order, max_page_order); | |
1793 | info->nr_ring_pages = 1 << ring_page_order; | |
9f27ee59 | 1794 | |
7ed8ce1c BD |
1795 | err = negotiate_mq(info); |
1796 | if (err) | |
1797 | goto destroy_blkring; | |
1798 | ||
4ab50af6 | 1799 | for_each_rinfo(info, rinfo, i) { |
3df0e505 BL |
1800 | /* Create shared ring, alloc event channel. */ |
1801 | err = setup_blkring(dev, rinfo); | |
1802 | if (err) | |
1803 | goto destroy_blkring; | |
1804 | } | |
9f27ee59 JF |
1805 | |
1806 | again: | |
1807 | err = xenbus_transaction_start(&xbt); | |
1808 | if (err) { | |
1809 | xenbus_dev_fatal(dev, err, "starting transaction"); | |
1810 | goto destroy_blkring; | |
1811 | } | |
1812 | ||
28d949bc BL |
1813 | if (info->nr_ring_pages > 1) { |
1814 | err = xenbus_printf(xbt, dev->nodename, "ring-page-order", "%u", | |
1815 | ring_page_order); | |
1816 | if (err) { | |
1817 | message = "writing ring-page-order"; | |
1818 | goto abort_transaction; | |
1819 | } | |
1820 | } | |
3df0e505 | 1821 | |
28d949bc BL |
1822 | /* We already got the number of queues/rings in _probe */ |
1823 | if (info->nr_rings == 1) { | |
4ab50af6 | 1824 | err = write_per_ring_nodes(xbt, info->rinfo, dev->nodename); |
28d949bc BL |
1825 | if (err) |
1826 | goto destroy_blkring; | |
1827 | } else { | |
1828 | char *path; | |
1829 | size_t pathsize; | |
3df0e505 | 1830 | |
28d949bc BL |
1831 | err = xenbus_printf(xbt, dev->nodename, "multi-queue-num-queues", "%u", |
1832 | info->nr_rings); | |
3df0e505 | 1833 | if (err) { |
28d949bc | 1834 | message = "writing multi-queue-num-queues"; |
3df0e505 BL |
1835 | goto abort_transaction; |
1836 | } | |
28d949bc BL |
1837 | |
1838 | pathsize = strlen(dev->nodename) + QUEUE_NAME_LEN; | |
1839 | path = kmalloc(pathsize, GFP_KERNEL); | |
1840 | if (!path) { | |
1841 | err = -ENOMEM; | |
1842 | message = "ENOMEM while writing ring references"; | |
1843 | goto abort_transaction; | |
1844 | } | |
1845 | ||
4ab50af6 | 1846 | for_each_rinfo(info, rinfo, i) { |
28d949bc BL |
1847 | memset(path, 0, pathsize); |
1848 | snprintf(path, pathsize, "%s/queue-%u", dev->nodename, i); | |
4ab50af6 | 1849 | err = write_per_ring_nodes(xbt, rinfo, path); |
28d949bc BL |
1850 | if (err) { |
1851 | kfree(path); | |
1852 | goto destroy_blkring; | |
1853 | } | |
1854 | } | |
1855 | kfree(path); | |
9f27ee59 | 1856 | } |
3e334239 MA |
1857 | err = xenbus_printf(xbt, dev->nodename, "protocol", "%s", |
1858 | XEN_IO_PROTO_ABI_NATIVE); | |
1859 | if (err) { | |
1860 | message = "writing protocol"; | |
1861 | goto abort_transaction; | |
1862 | } | |
74a85247 SP |
1863 | err = xenbus_printf(xbt, dev->nodename, "feature-persistent", "%u", |
1864 | info->feature_persistent); | |
0a8704a5 RPM |
1865 | if (err) |
1866 | dev_warn(&dev->dev, | |
1867 | "writing persistent grants feature to xenbus"); | |
9f27ee59 JF |
1868 | |
1869 | err = xenbus_transaction_end(xbt, 0); | |
1870 | if (err) { | |
1871 | if (err == -EAGAIN) | |
1872 | goto again; | |
1873 | xenbus_dev_fatal(dev, err, "completing transaction"); | |
1874 | goto destroy_blkring; | |
1875 | } | |
1876 | ||
4ab50af6 | 1877 | for_each_rinfo(info, rinfo, i) { |
3df0e505 | 1878 | unsigned int j; |
3df0e505 BL |
1879 | |
1880 | for (j = 0; j < BLK_RING_SIZE(info); j++) | |
1881 | rinfo->shadow[j].req.u.rw.id = j + 1; | |
1882 | rinfo->shadow[BLK_RING_SIZE(info)-1].req.u.rw.id = 0x0fffffff; | |
1883 | } | |
9f27ee59 JF |
1884 | xenbus_switch_state(dev, XenbusStateInitialised); |
1885 | ||
1886 | return 0; | |
1887 | ||
1888 | abort_transaction: | |
1889 | xenbus_transaction_end(xbt, 1); | |
1890 | if (message) | |
1891 | xenbus_dev_fatal(dev, err, "%s", message); | |
1892 | destroy_blkring: | |
1893 | blkif_free(info, 0); | |
9f27ee59 JF |
1894 | return err; |
1895 | } | |
1896 | ||
3db70a85 BL |
1897 | static int negotiate_mq(struct blkfront_info *info) |
1898 | { | |
f27dc1ac | 1899 | unsigned int backend_max_queues; |
3db70a85 | 1900 | unsigned int i; |
4ab50af6 | 1901 | struct blkfront_ring_info *rinfo; |
3db70a85 BL |
1902 | |
1903 | BUG_ON(info->nr_rings); | |
1904 | ||
1905 | /* Check if backend supports multiple queues. */ | |
f27dc1ac JG |
1906 | backend_max_queues = xenbus_read_unsigned(info->xbdev->otherend, |
1907 | "multi-queue-max-queues", 1); | |
3db70a85 BL |
1908 | info->nr_rings = min(backend_max_queues, xen_blkif_max_queues); |
1909 | /* We need at least one ring. */ | |
1910 | if (!info->nr_rings) | |
1911 | info->nr_rings = 1; | |
1912 | ||
4ab50af6 JG |
1913 | info->rinfo_size = struct_size(info->rinfo, shadow, |
1914 | BLK_RING_SIZE(info)); | |
1915 | info->rinfo = kvcalloc(info->nr_rings, info->rinfo_size, GFP_KERNEL); | |
3db70a85 BL |
1916 | if (!info->rinfo) { |
1917 | xenbus_dev_fatal(info->xbdev, -ENOMEM, "allocating ring_info structure"); | |
6cc4a086 | 1918 | info->nr_rings = 0; |
3db70a85 BL |
1919 | return -ENOMEM; |
1920 | } | |
1921 | ||
4ab50af6 | 1922 | for_each_rinfo(info, rinfo, i) { |
3db70a85 BL |
1923 | INIT_LIST_HEAD(&rinfo->indirect_pages); |
1924 | INIT_LIST_HEAD(&rinfo->grants); | |
1925 | rinfo->dev_info = info; | |
1926 | INIT_WORK(&rinfo->work, blkif_restart_queue); | |
1927 | spin_lock_init(&rinfo->ring_lock); | |
1928 | } | |
1929 | return 0; | |
1930 | } | |
74a85247 SP |
1931 | |
1932 | /* Enable the persistent grants feature. */ | |
1933 | static bool feature_persistent = true; | |
1934 | module_param(feature_persistent, bool, 0644); | |
1935 | MODULE_PARM_DESC(feature_persistent, | |
1936 | "Enables the persistent grants feature"); | |
1937 | ||
5fdbd5bc | 1938 | /* |
9f27ee59 JF |
1939 | * Entry point to this code when a new device is created. Allocate the basic |
1940 | * structures and the ring buffer for communication with the backend, and | |
1941 | * inform the backend of the appropriate details for those. Switch to | |
1942 | * Initialised state. | |
1943 | */ | |
1944 | static int blkfront_probe(struct xenbus_device *dev, | |
1945 | const struct xenbus_device_id *id) | |
1946 | { | |
86839c56 | 1947 | int err, vdevice; |
9f27ee59 JF |
1948 | struct blkfront_info *info; |
1949 | ||
1950 | /* FIXME: Use dynamic device id if this is not set. */ | |
1951 | err = xenbus_scanf(XBT_NIL, dev->nodename, | |
1952 | "virtual-device", "%i", &vdevice); | |
1953 | if (err != 1) { | |
9246b5f0 CL |
1954 | /* go looking in the extended area instead */ |
1955 | err = xenbus_scanf(XBT_NIL, dev->nodename, "virtual-device-ext", | |
1956 | "%i", &vdevice); | |
1957 | if (err != 1) { | |
1958 | xenbus_dev_fatal(dev, err, "reading virtual-device"); | |
1959 | return err; | |
1960 | } | |
9f27ee59 JF |
1961 | } |
1962 | ||
b98a409b SS |
1963 | if (xen_hvm_domain()) { |
1964 | char *type; | |
1965 | int len; | |
1966 | /* no unplug has been done: do not hook devices != xen vbds */ | |
51c71a3b | 1967 | if (xen_has_pv_and_legacy_disk_devices()) { |
b98a409b SS |
1968 | int major; |
1969 | ||
1970 | if (!VDEV_IS_EXTENDED(vdevice)) | |
1971 | major = BLKIF_MAJOR(vdevice); | |
1972 | else | |
1973 | major = XENVBD_MAJOR; | |
1974 | ||
1975 | if (major != XENVBD_MAJOR) { | |
1976 | printk(KERN_INFO | |
1977 | "%s: HVM does not support vbd %d as xen block device\n", | |
02f1f217 | 1978 | __func__, vdevice); |
b98a409b SS |
1979 | return -ENODEV; |
1980 | } | |
1981 | } | |
1982 | /* do not create a PV cdrom device if we are an HVM guest */ | |
1983 | type = xenbus_read(XBT_NIL, dev->nodename, "device-type", &len); | |
1984 | if (IS_ERR(type)) | |
1985 | return -ENODEV; | |
1986 | if (strncmp(type, "cdrom", 5) == 0) { | |
1987 | kfree(type); | |
c1c5413a SS |
1988 | return -ENODEV; |
1989 | } | |
b98a409b | 1990 | kfree(type); |
c1c5413a | 1991 | } |
9f27ee59 JF |
1992 | info = kzalloc(sizeof(*info), GFP_KERNEL); |
1993 | if (!info) { | |
1994 | xenbus_dev_fatal(dev, -ENOMEM, "allocating info structure"); | |
1995 | return -ENOMEM; | |
1996 | } | |
1997 | ||
28d949bc | 1998 | info->xbdev = dev; |
81f35161 | 1999 | |
b70f5fa0 | 2000 | mutex_init(&info->mutex); |
9f27ee59 JF |
2001 | info->vdevice = vdevice; |
2002 | info->connected = BLKIF_STATE_DISCONNECTED; | |
9f27ee59 | 2003 | |
74a85247 SP |
2004 | info->feature_persistent = feature_persistent; |
2005 | ||
9f27ee59 JF |
2006 | /* Front end dir is a number, which is used as the id. */ |
2007 | info->handle = simple_strtoul(strrchr(dev->nodename, '/')+1, NULL, 0); | |
a1b4b12b | 2008 | dev_set_drvdata(&dev->dev, info); |
9f27ee59 | 2009 | |
a46b5367 JG |
2010 | mutex_lock(&blkfront_mutex); |
2011 | list_add(&info->info_list, &info_list); | |
2012 | mutex_unlock(&blkfront_mutex); | |
2013 | ||
9f27ee59 JF |
2014 | return 0; |
2015 | } | |
2016 | ||
9f27ee59 JF |
2017 | static int blkif_recover(struct blkfront_info *info) |
2018 | { | |
4559fa55 | 2019 | unsigned int r_index; |
402b27f9 | 2020 | struct request *req, *n; |
402b27f9 | 2021 | int rc; |
4559fa55 N |
2022 | struct bio *bio; |
2023 | unsigned int segs; | |
4ab50af6 | 2024 | struct blkfront_ring_info *rinfo; |
402b27f9 | 2025 | |
3df0e505 | 2026 | blkfront_gather_backend_features(info); |
172335ad BL |
2027 | /* Reset limits changed by blk_mq_update_nr_hw_queues(). */ |
2028 | blkif_set_queue_limits(info); | |
402b27f9 | 2029 | segs = info->max_indirect_segments ? : BLKIF_MAX_SEGMENTS_PER_REQUEST; |
6c647b0e | 2030 | blk_queue_max_segments(info->rq, segs / GRANTS_PER_PSEG); |
9f27ee59 | 2031 | |
4ab50af6 | 2032 | for_each_rinfo(info, rinfo, r_index) { |
3df0e505 | 2033 | rc = blkfront_setup_indirect(rinfo); |
7b427a59 | 2034 | if (rc) |
3df0e505 | 2035 | return rc; |
3df0e505 | 2036 | } |
9f27ee59 JF |
2037 | xenbus_switch_state(info->xbdev, XenbusStateConnected); |
2038 | ||
9f27ee59 JF |
2039 | /* Now safe for us to use the shared ring */ |
2040 | info->connected = BLKIF_STATE_CONNECTED; | |
2041 | ||
4ab50af6 | 2042 | for_each_rinfo(info, rinfo, r_index) { |
3df0e505 BL |
2043 | /* Kick any other new requests queued since we resumed */ |
2044 | kick_pending_request_queues(rinfo); | |
2045 | } | |
9f27ee59 | 2046 | |
7b427a59 | 2047 | list_for_each_entry_safe(req, n, &info->requests, queuelist) { |
402b27f9 RPM |
2048 | /* Requeue pending requests (flush or discard) */ |
2049 | list_del_init(&req->queuelist); | |
2050 | BUG_ON(req->nr_phys_segments > segs); | |
2b053aca | 2051 | blk_mq_requeue_request(req, false); |
402b27f9 | 2052 | } |
52d7f1b5 | 2053 | blk_mq_start_stopped_hw_queues(info->rq, true); |
907c3eb1 | 2054 | blk_mq_kick_requeue_list(info->rq); |
9f27ee59 | 2055 | |
7b427a59 | 2056 | while ((bio = bio_list_pop(&info->bio_list)) != NULL) { |
402b27f9 | 2057 | /* Traverse the list of pending bios and re-queue them */ |
4e49ea4a | 2058 | submit_bio(bio); |
402b27f9 RPM |
2059 | } |
2060 | ||
9f27ee59 JF |
2061 | return 0; |
2062 | } | |
2063 | ||
5fdbd5bc | 2064 | /* |
9f27ee59 JF |
2065 | * We are reconnecting to the backend, due to a suspend/resume, or a backend |
2066 | * driver restart. We tear down our blkif structure and recreate it, but | |
2067 | * leave the device-layer structures intact so that this is transparent to the | |
2068 | * rest of the kernel. | |
2069 | */ | |
2070 | static int blkfront_resume(struct xenbus_device *dev) | |
2071 | { | |
a1b4b12b | 2072 | struct blkfront_info *info = dev_get_drvdata(&dev->dev); |
3db70a85 | 2073 | int err = 0; |
7b427a59 | 2074 | unsigned int i, j; |
4ab50af6 | 2075 | struct blkfront_ring_info *rinfo; |
9f27ee59 JF |
2076 | |
2077 | dev_dbg(&dev->dev, "blkfront_resume: %s\n", dev->nodename); | |
2078 | ||
7b427a59 BL |
2079 | bio_list_init(&info->bio_list); |
2080 | INIT_LIST_HEAD(&info->requests); | |
4ab50af6 | 2081 | for_each_rinfo(info, rinfo, i) { |
7b427a59 BL |
2082 | struct bio_list merge_bio; |
2083 | struct blk_shadow *shadow = rinfo->shadow; | |
2084 | ||
2085 | for (j = 0; j < BLK_RING_SIZE(info); j++) { | |
2086 | /* Not in use? */ | |
2087 | if (!shadow[j].request) | |
2088 | continue; | |
2089 | ||
2090 | /* | |
2091 | * Get the bios in the request so we can re-queue them. | |
2092 | */ | |
b15bd8cb MK |
2093 | if (req_op(shadow[j].request) == REQ_OP_FLUSH || |
2094 | req_op(shadow[j].request) == REQ_OP_DISCARD || | |
2095 | req_op(shadow[j].request) == REQ_OP_SECURE_ERASE || | |
3fc9d690 | 2096 | shadow[j].request->cmd_flags & REQ_FUA) { |
7b427a59 BL |
2097 | /* |
2098 | * Flush operations don't contain bios, so | |
2099 | * we need to requeue the whole request | |
3fc9d690 LT |
2100 | * |
2101 | * XXX: but this doesn't make any sense for a | |
2102 | * write with the FUA flag set.. | |
7b427a59 BL |
2103 | */ |
2104 | list_add(&shadow[j].request->queuelist, &info->requests); | |
2105 | continue; | |
2106 | } | |
2107 | merge_bio.head = shadow[j].request->bio; | |
2108 | merge_bio.tail = shadow[j].request->biotail; | |
2109 | bio_list_merge(&info->bio_list, &merge_bio); | |
2110 | shadow[j].request->bio = NULL; | |
2a842aca | 2111 | blk_mq_end_request(shadow[j].request, BLK_STS_OK); |
7b427a59 BL |
2112 | } |
2113 | } | |
2114 | ||
9f27ee59 JF |
2115 | blkif_free(info, info->connected == BLKIF_STATE_CONNECTED); |
2116 | ||
203fd61f | 2117 | err = talk_to_blkback(dev, info); |
2a6f71ad BL |
2118 | if (!err) |
2119 | blk_mq_update_nr_hw_queues(&info->tag_set, info->nr_rings); | |
402b27f9 RPM |
2120 | |
2121 | /* | |
2122 | * We have to wait for the backend to switch to | |
2123 | * connected state, since we want to read which | |
2124 | * features it supports. | |
2125 | */ | |
9f27ee59 JF |
2126 | |
2127 | return err; | |
2128 | } | |
2129 | ||
6f03a7ff | 2130 | static void blkfront_closing(struct blkfront_info *info) |
b70f5fa0 DS |
2131 | { |
2132 | struct xenbus_device *xbdev = info->xbdev; | |
05d69d95 CH |
2133 | struct blkfront_ring_info *rinfo; |
2134 | unsigned int i; | |
b70f5fa0 | 2135 | |
05d69d95 | 2136 | if (xbdev->state == XenbusStateClosing) |
b70f5fa0 | 2137 | return; |
b70f5fa0 | 2138 | |
05d69d95 CH |
2139 | /* No more blkif_request(). */ |
2140 | blk_mq_stop_hw_queues(info->rq); | |
7a5428dc | 2141 | blk_mark_disk_dead(info->gd); |
05d69d95 | 2142 | set_capacity(info->gd, 0); |
b70f5fa0 | 2143 | |
05d69d95 CH |
2144 | for_each_rinfo(info, rinfo, i) { |
2145 | /* No more gnttab callback work. */ | |
2146 | gnttab_cancel_free_callback(&rinfo->callback); | |
b70f5fa0 | 2147 | |
05d69d95 CH |
2148 | /* Flush gnttab callback work. Must be done with no locks held. */ |
2149 | flush_work(&rinfo->work); | |
b70f5fa0 DS |
2150 | } |
2151 | ||
05d69d95 | 2152 | xenbus_frontend_closed(xbdev); |
b70f5fa0 | 2153 | } |
9f27ee59 | 2154 | |
ed30bf31 LD |
2155 | static void blkfront_setup_discard(struct blkfront_info *info) |
2156 | { | |
1c8cad6c | 2157 | info->feature_discard = 1; |
0549cd67 RPM |
2158 | info->discard_granularity = xenbus_read_unsigned(info->xbdev->otherend, |
2159 | "discard-granularity", | |
2160 | 0); | |
2161 | info->discard_alignment = xenbus_read_unsigned(info->xbdev->otherend, | |
2162 | "discard-alignment", 0); | |
f27dc1ac JG |
2163 | info->feature_secdiscard = |
2164 | !!xenbus_read_unsigned(info->xbdev->otherend, "discard-secure", | |
2165 | 0); | |
ed30bf31 LD |
2166 | } |
2167 | ||
81f35161 | 2168 | static int blkfront_setup_indirect(struct blkfront_ring_info *rinfo) |
402b27f9 | 2169 | { |
3a169c0b | 2170 | unsigned int psegs, grants, memflags; |
402b27f9 | 2171 | int err, i; |
81f35161 | 2172 | struct blkfront_info *info = rinfo->dev_info; |
402b27f9 | 2173 | |
3a169c0b JG |
2174 | memflags = memalloc_noio_save(); |
2175 | ||
6cc56833 JG |
2176 | if (info->max_indirect_segments == 0) { |
2177 | if (!HAS_EXTRA_REQ) | |
2178 | grants = BLKIF_MAX_SEGMENTS_PER_REQUEST; | |
2179 | else { | |
2180 | /* | |
2181 | * When an extra req is required, the maximum | |
2182 | * grants supported is related to the size of the | |
2183 | * Linux block segment. | |
2184 | */ | |
2185 | grants = GRANTS_PER_PSEG; | |
2186 | } | |
2187 | } | |
d50babbe | 2188 | else |
c004a6fe | 2189 | grants = info->max_indirect_segments; |
3b4f1884 | 2190 | psegs = DIV_ROUND_UP(grants, GRANTS_PER_PSEG); |
402b27f9 | 2191 | |
81f35161 | 2192 | err = fill_grant_buffer(rinfo, |
c004a6fe | 2193 | (grants + INDIRECT_GREFS(grants)) * BLK_RING_SIZE(info)); |
402b27f9 RPM |
2194 | if (err) |
2195 | goto out_of_memory; | |
2196 | ||
bfe11d6d RPM |
2197 | if (!info->feature_persistent && info->max_indirect_segments) { |
2198 | /* | |
2199 | * We are using indirect descriptors but not persistent | |
2200 | * grants, we need to allocate a set of pages that can be | |
2201 | * used for mapping indirect grefs | |
2202 | */ | |
c004a6fe | 2203 | int num = INDIRECT_GREFS(grants) * BLK_RING_SIZE(info); |
bfe11d6d | 2204 | |
81f35161 | 2205 | BUG_ON(!list_empty(&rinfo->indirect_pages)); |
bfe11d6d | 2206 | for (i = 0; i < num; i++) { |
3a169c0b | 2207 | struct page *indirect_page = alloc_page(GFP_KERNEL); |
bfe11d6d RPM |
2208 | if (!indirect_page) |
2209 | goto out_of_memory; | |
81f35161 | 2210 | list_add(&indirect_page->lru, &rinfo->indirect_pages); |
bfe11d6d RPM |
2211 | } |
2212 | } | |
2213 | ||
86839c56 | 2214 | for (i = 0; i < BLK_RING_SIZE(info); i++) { |
6396bb22 | 2215 | rinfo->shadow[i].grants_used = |
1d5c76e6 RPM |
2216 | kvcalloc(grants, |
2217 | sizeof(rinfo->shadow[i].grants_used[0]), | |
3a169c0b | 2218 | GFP_KERNEL); |
1d5c76e6 RPM |
2219 | rinfo->shadow[i].sg = kvcalloc(psegs, |
2220 | sizeof(rinfo->shadow[i].sg[0]), | |
3a169c0b | 2221 | GFP_KERNEL); |
6396bb22 KC |
2222 | if (info->max_indirect_segments) |
2223 | rinfo->shadow[i].indirect_grants = | |
1d5c76e6 RPM |
2224 | kvcalloc(INDIRECT_GREFS(grants), |
2225 | sizeof(rinfo->shadow[i].indirect_grants[0]), | |
3a169c0b | 2226 | GFP_KERNEL); |
81f35161 BL |
2227 | if ((rinfo->shadow[i].grants_used == NULL) || |
2228 | (rinfo->shadow[i].sg == NULL) || | |
402b27f9 | 2229 | (info->max_indirect_segments && |
81f35161 | 2230 | (rinfo->shadow[i].indirect_grants == NULL))) |
402b27f9 | 2231 | goto out_of_memory; |
81f35161 | 2232 | sg_init_table(rinfo->shadow[i].sg, psegs); |
402b27f9 RPM |
2233 | } |
2234 | ||
3a169c0b | 2235 | memalloc_noio_restore(memflags); |
402b27f9 RPM |
2236 | |
2237 | return 0; | |
2238 | ||
2239 | out_of_memory: | |
86839c56 | 2240 | for (i = 0; i < BLK_RING_SIZE(info); i++) { |
1d5c76e6 | 2241 | kvfree(rinfo->shadow[i].grants_used); |
81f35161 | 2242 | rinfo->shadow[i].grants_used = NULL; |
1d5c76e6 | 2243 | kvfree(rinfo->shadow[i].sg); |
81f35161 | 2244 | rinfo->shadow[i].sg = NULL; |
1d5c76e6 | 2245 | kvfree(rinfo->shadow[i].indirect_grants); |
81f35161 | 2246 | rinfo->shadow[i].indirect_grants = NULL; |
402b27f9 | 2247 | } |
81f35161 | 2248 | if (!list_empty(&rinfo->indirect_pages)) { |
bfe11d6d | 2249 | struct page *indirect_page, *n; |
81f35161 | 2250 | list_for_each_entry_safe(indirect_page, n, &rinfo->indirect_pages, lru) { |
bfe11d6d RPM |
2251 | list_del(&indirect_page->lru); |
2252 | __free_page(indirect_page); | |
2253 | } | |
2254 | } | |
3a169c0b JG |
2255 | |
2256 | memalloc_noio_restore(memflags); | |
2257 | ||
402b27f9 RPM |
2258 | return -ENOMEM; |
2259 | } | |
2260 | ||
d50babbe BL |
2261 | /* |
2262 | * Gather all backend feature-* | |
2263 | */ | |
3df0e505 | 2264 | static void blkfront_gather_backend_features(struct blkfront_info *info) |
d50babbe | 2265 | { |
d50babbe BL |
2266 | unsigned int indirect_segments; |
2267 | ||
2268 | info->feature_flush = 0; | |
a418090a | 2269 | info->feature_fua = 0; |
d50babbe | 2270 | |
d50babbe BL |
2271 | /* |
2272 | * If there's no "feature-barrier" defined, then it means | |
2273 | * we're dealing with a very old backend which writes | |
2274 | * synchronously; nothing to do. | |
2275 | * | |
2276 | * If there are barriers, then we use flush. | |
2277 | */ | |
f27dc1ac | 2278 | if (xenbus_read_unsigned(info->xbdev->otherend, "feature-barrier", 0)) { |
a418090a MC |
2279 | info->feature_flush = 1; |
2280 | info->feature_fua = 1; | |
2281 | } | |
2282 | ||
d50babbe BL |
2283 | /* |
2284 | * And if there is "feature-flush-cache" use that above | |
2285 | * barriers. | |
2286 | */ | |
f27dc1ac JG |
2287 | if (xenbus_read_unsigned(info->xbdev->otherend, "feature-flush-cache", |
2288 | 0)) { | |
a418090a MC |
2289 | info->feature_flush = 1; |
2290 | info->feature_fua = 0; | |
2291 | } | |
d50babbe | 2292 | |
f27dc1ac | 2293 | if (xenbus_read_unsigned(info->xbdev->otherend, "feature-discard", 0)) |
d50babbe BL |
2294 | blkfront_setup_discard(info); |
2295 | ||
74a85247 SP |
2296 | if (info->feature_persistent) |
2297 | info->feature_persistent = | |
2298 | !!xenbus_read_unsigned(info->xbdev->otherend, | |
2299 | "feature-persistent", 0); | |
d50babbe | 2300 | |
f27dc1ac JG |
2301 | indirect_segments = xenbus_read_unsigned(info->xbdev->otherend, |
2302 | "feature-max-indirect-segments", 0); | |
3b4f1884 JB |
2303 | if (indirect_segments > xen_blkif_max_segments) |
2304 | indirect_segments = xen_blkif_max_segments; | |
2305 | if (indirect_segments <= BLKIF_MAX_SEGMENTS_PER_REQUEST) | |
2306 | indirect_segments = 0; | |
2307 | info->max_indirect_segments = indirect_segments; | |
a46b5367 JG |
2308 | |
2309 | if (info->feature_persistent) { | |
2310 | mutex_lock(&blkfront_mutex); | |
2311 | schedule_delayed_work(&blkfront_work, HZ * 10); | |
2312 | mutex_unlock(&blkfront_mutex); | |
2313 | } | |
d50babbe BL |
2314 | } |
2315 | ||
9f27ee59 JF |
2316 | /* |
2317 | * Invoked when the backend is finally 'ready' (and has told produced | |
2318 | * the details about the physical device - #sectors, size, etc). | |
2319 | */ | |
2320 | static void blkfront_connect(struct blkfront_info *info) | |
2321 | { | |
2322 | unsigned long long sectors; | |
2323 | unsigned long sector_size; | |
7c4d7d71 | 2324 | unsigned int physical_sector_size; |
3df0e505 | 2325 | int err, i; |
4ab50af6 | 2326 | struct blkfront_ring_info *rinfo; |
9f27ee59 | 2327 | |
1fa73be6 S |
2328 | switch (info->connected) { |
2329 | case BLKIF_STATE_CONNECTED: | |
2330 | /* | |
2331 | * Potentially, the back-end may be signalling | |
2332 | * a capacity change; update the capacity. | |
2333 | */ | |
2334 | err = xenbus_scanf(XBT_NIL, info->xbdev->otherend, | |
2335 | "sectors", "%Lu", §ors); | |
2336 | if (XENBUS_EXIST_ERR(err)) | |
2337 | return; | |
2338 | printk(KERN_INFO "Setting capacity to %Lu\n", | |
2339 | sectors); | |
449f4ec9 | 2340 | set_capacity_and_notify(info->gd, sectors); |
1fa73be6 | 2341 | |
402b27f9 | 2342 | return; |
1fa73be6 | 2343 | case BLKIF_STATE_SUSPENDED: |
402b27f9 RPM |
2344 | /* |
2345 | * If we are recovering from suspension, we need to wait | |
2346 | * for the backend to announce it's features before | |
2347 | * reconnecting, at least we need to know if the backend | |
2348 | * supports indirect descriptors, and how many. | |
2349 | */ | |
2350 | blkif_recover(info); | |
9f27ee59 JF |
2351 | return; |
2352 | ||
b4dddb49 JF |
2353 | default: |
2354 | break; | |
1fa73be6 | 2355 | } |
9f27ee59 JF |
2356 | |
2357 | dev_dbg(&info->xbdev->dev, "%s:%s.\n", | |
2358 | __func__, info->xbdev->otherend); | |
2359 | ||
2360 | err = xenbus_gather(XBT_NIL, info->xbdev->otherend, | |
2361 | "sectors", "%llu", §ors, | |
1a827ce1 | 2362 | "info", "%u", &info->vdisk_info, |
9f27ee59 JF |
2363 | "sector-size", "%lu", §or_size, |
2364 | NULL); | |
2365 | if (err) { | |
2366 | xenbus_dev_fatal(info->xbdev, err, | |
2367 | "reading backend fields at %s", | |
2368 | info->xbdev->otherend); | |
2369 | return; | |
2370 | } | |
2371 | ||
7c4d7d71 | 2372 | /* |
ec3307a5 | 2373 | * physical-sector-size is a newer field, so old backends may not |
7c4d7d71 SB |
2374 | * provide this. Assume physical sector size to be the same as |
2375 | * sector_size in that case. | |
2376 | */ | |
f27dc1ac JG |
2377 | physical_sector_size = xenbus_read_unsigned(info->xbdev->otherend, |
2378 | "physical-sector-size", | |
2379 | sector_size); | |
3df0e505 | 2380 | blkfront_gather_backend_features(info); |
4ab50af6 JG |
2381 | for_each_rinfo(info, rinfo, i) { |
2382 | err = blkfront_setup_indirect(rinfo); | |
3df0e505 BL |
2383 | if (err) { |
2384 | xenbus_dev_fatal(info->xbdev, err, "setup_indirect at %s", | |
2385 | info->xbdev->otherend); | |
2386 | blkif_free(info, 0); | |
2387 | break; | |
2388 | } | |
402b27f9 RPM |
2389 | } |
2390 | ||
1a827ce1 | 2391 | err = xlvbd_alloc_gendisk(sectors, info, sector_size, |
7c4d7d71 | 2392 | physical_sector_size); |
9f27ee59 JF |
2393 | if (err) { |
2394 | xenbus_dev_fatal(info->xbdev, err, "xlvbd_add at %s", | |
2395 | info->xbdev->otherend); | |
4e876c2b | 2396 | goto fail; |
9f27ee59 JF |
2397 | } |
2398 | ||
2399 | xenbus_switch_state(info->xbdev, XenbusStateConnected); | |
2400 | ||
2401 | /* Kick pending requests. */ | |
9f27ee59 | 2402 | info->connected = BLKIF_STATE_CONNECTED; |
4ab50af6 JG |
2403 | for_each_rinfo(info, rinfo, i) |
2404 | kick_pending_request_queues(rinfo); | |
9f27ee59 | 2405 | |
293a7c52 LC |
2406 | err = device_add_disk(&info->xbdev->dev, info->gd, NULL); |
2407 | if (err) { | |
2408 | blk_cleanup_disk(info->gd); | |
2409 | blk_mq_free_tag_set(&info->tag_set); | |
2410 | info->rq = NULL; | |
2411 | goto fail; | |
2412 | } | |
1d78d705 CL |
2413 | |
2414 | info->is_ready = 1; | |
4e876c2b BL |
2415 | return; |
2416 | ||
2417 | fail: | |
2418 | blkif_free(info, 0); | |
2419 | return; | |
9f27ee59 JF |
2420 | } |
2421 | ||
5fdbd5bc | 2422 | /* |
9f27ee59 JF |
2423 | * Callback received when the backend's state changes. |
2424 | */ | |
203fd61f | 2425 | static void blkback_changed(struct xenbus_device *dev, |
9f27ee59 JF |
2426 | enum xenbus_state backend_state) |
2427 | { | |
a1b4b12b | 2428 | struct blkfront_info *info = dev_get_drvdata(&dev->dev); |
9f27ee59 | 2429 | |
203fd61f | 2430 | dev_dbg(&dev->dev, "blkfront:blkback_changed to state %d.\n", backend_state); |
9f27ee59 JF |
2431 | |
2432 | switch (backend_state) { | |
9f27ee59 | 2433 | case XenbusStateInitWait: |
a9b54bb9 BL |
2434 | if (dev->state != XenbusStateInitialising) |
2435 | break; | |
c31ecf6c | 2436 | if (talk_to_blkback(dev, info)) |
8ab0144a | 2437 | break; |
3955bcbf | 2438 | break; |
8ab0144a | 2439 | case XenbusStateInitialising: |
9f27ee59 | 2440 | case XenbusStateInitialised: |
b78c9512 NI |
2441 | case XenbusStateReconfiguring: |
2442 | case XenbusStateReconfigured: | |
9f27ee59 | 2443 | case XenbusStateUnknown: |
9f27ee59 JF |
2444 | break; |
2445 | ||
2446 | case XenbusStateConnected: | |
efd15352 BL |
2447 | /* |
2448 | * talk_to_blkback sets state to XenbusStateInitialised | |
2449 | * and blkfront_connect sets it to XenbusStateConnected | |
2450 | * (if connection went OK). | |
2451 | * | |
2452 | * If the backend (or toolstack) decides to poke at backend | |
2453 | * state (and re-trigger the watch by setting the state repeatedly | |
2454 | * to XenbusStateConnected (4)) we need to deal with this. | |
2455 | * This is allowed as this is used to communicate to the guest | |
2456 | * that the size of disk has changed! | |
2457 | */ | |
2458 | if ((dev->state != XenbusStateInitialised) && | |
2459 | (dev->state != XenbusStateConnected)) { | |
c31ecf6c KRW |
2460 | if (talk_to_blkback(dev, info)) |
2461 | break; | |
2462 | } | |
efd15352 | 2463 | |
9f27ee59 JF |
2464 | blkfront_connect(info); |
2465 | break; | |
2466 | ||
36613717 DV |
2467 | case XenbusStateClosed: |
2468 | if (dev->state == XenbusStateClosed) | |
2469 | break; | |
df561f66 | 2470 | fallthrough; |
9f27ee59 | 2471 | case XenbusStateClosing: |
05d69d95 | 2472 | blkfront_closing(info); |
9f27ee59 JF |
2473 | break; |
2474 | } | |
2475 | } | |
2476 | ||
fa1bd359 | 2477 | static int blkfront_remove(struct xenbus_device *xbdev) |
9f27ee59 | 2478 | { |
fa1bd359 | 2479 | struct blkfront_info *info = dev_get_drvdata(&xbdev->dev); |
9f27ee59 | 2480 | |
fa1bd359 | 2481 | dev_dbg(&xbdev->dev, "%s removed", xbdev->nodename); |
9f27ee59 | 2482 | |
05d69d95 | 2483 | del_gendisk(info->gd); |
d54142c7 | 2484 | |
05d69d95 CH |
2485 | mutex_lock(&blkfront_mutex); |
2486 | list_del(&info->info_list); | |
2487 | mutex_unlock(&blkfront_mutex); | |
fa1bd359 | 2488 | |
05d69d95 CH |
2489 | blkif_free(info, 0); |
2490 | xlbd_release_minors(info->gd->first_minor, info->gd->minors); | |
2491 | blk_cleanup_disk(info->gd); | |
2492 | blk_mq_free_tag_set(&info->tag_set); | |
9f27ee59 | 2493 | |
05d69d95 | 2494 | kfree(info); |
9f27ee59 JF |
2495 | return 0; |
2496 | } | |
2497 | ||
1d78d705 CL |
2498 | static int blkfront_is_ready(struct xenbus_device *dev) |
2499 | { | |
a1b4b12b | 2500 | struct blkfront_info *info = dev_get_drvdata(&dev->dev); |
1d78d705 | 2501 | |
5d7ed20e | 2502 | return info->is_ready && info->xbdev; |
1d78d705 CL |
2503 | } |
2504 | ||
83d5cde4 | 2505 | static const struct block_device_operations xlvbd_block_fops = |
9f27ee59 JF |
2506 | { |
2507 | .owner = THIS_MODULE, | |
597592d9 | 2508 | .getgeo = blkif_getgeo, |
8a6cfeb6 | 2509 | .ioctl = blkif_ioctl, |
9452b1a3 | 2510 | .compat_ioctl = blkdev_compat_ptr_ioctl, |
9f27ee59 JF |
2511 | }; |
2512 | ||
2513 | ||
ec9c42ec | 2514 | static const struct xenbus_device_id blkfront_ids[] = { |
9f27ee59 JF |
2515 | { "vbd" }, |
2516 | { "" } | |
2517 | }; | |
2518 | ||
95afae48 DV |
2519 | static struct xenbus_driver blkfront_driver = { |
2520 | .ids = blkfront_ids, | |
9f27ee59 JF |
2521 | .probe = blkfront_probe, |
2522 | .remove = blkfront_remove, | |
2523 | .resume = blkfront_resume, | |
203fd61f | 2524 | .otherend_changed = blkback_changed, |
1d78d705 | 2525 | .is_ready = blkfront_is_ready, |
95afae48 | 2526 | }; |
9f27ee59 | 2527 | |
a46b5367 JG |
2528 | static void purge_persistent_grants(struct blkfront_info *info) |
2529 | { | |
2530 | unsigned int i; | |
2531 | unsigned long flags; | |
4ab50af6 | 2532 | struct blkfront_ring_info *rinfo; |
a46b5367 | 2533 | |
4ab50af6 | 2534 | for_each_rinfo(info, rinfo, i) { |
a46b5367 | 2535 | struct grant *gnt_list_entry, *tmp; |
85d9abcd | 2536 | LIST_HEAD(grants); |
a46b5367 JG |
2537 | |
2538 | spin_lock_irqsave(&rinfo->ring_lock, flags); | |
2539 | ||
2540 | if (rinfo->persistent_gnts_c == 0) { | |
2541 | spin_unlock_irqrestore(&rinfo->ring_lock, flags); | |
2542 | continue; | |
2543 | } | |
2544 | ||
2545 | list_for_each_entry_safe(gnt_list_entry, tmp, &rinfo->grants, | |
2546 | node) { | |
2547 | if (gnt_list_entry->gref == GRANT_INVALID_REF || | |
abf1fd59 | 2548 | !gnttab_try_end_foreign_access(gnt_list_entry->gref)) |
a46b5367 JG |
2549 | continue; |
2550 | ||
2551 | list_del(&gnt_list_entry->node); | |
a46b5367 | 2552 | rinfo->persistent_gnts_c--; |
6c767867 | 2553 | gnt_list_entry->gref = GRANT_INVALID_REF; |
85d9abcd | 2554 | list_add_tail(&gnt_list_entry->node, &grants); |
a46b5367 JG |
2555 | } |
2556 | ||
85d9abcd JG |
2557 | list_splice_tail(&grants, &rinfo->grants); |
2558 | ||
a46b5367 JG |
2559 | spin_unlock_irqrestore(&rinfo->ring_lock, flags); |
2560 | } | |
2561 | } | |
2562 | ||
2563 | static void blkfront_delay_work(struct work_struct *work) | |
2564 | { | |
2565 | struct blkfront_info *info; | |
2566 | bool need_schedule_work = false; | |
2567 | ||
2568 | mutex_lock(&blkfront_mutex); | |
2569 | ||
2570 | list_for_each_entry(info, &info_list, info_list) { | |
2571 | if (info->feature_persistent) { | |
2572 | need_schedule_work = true; | |
2573 | mutex_lock(&info->mutex); | |
2574 | purge_persistent_grants(info); | |
2575 | mutex_unlock(&info->mutex); | |
2576 | } | |
2577 | } | |
2578 | ||
2579 | if (need_schedule_work) | |
2580 | schedule_delayed_work(&blkfront_work, HZ * 10); | |
2581 | ||
2582 | mutex_unlock(&blkfront_mutex); | |
2583 | } | |
2584 | ||
9f27ee59 JF |
2585 | static int __init xlblk_init(void) |
2586 | { | |
469738e6 | 2587 | int ret; |
28d949bc | 2588 | int nr_cpus = num_online_cpus(); |
469738e6 | 2589 | |
6e833587 | 2590 | if (!xen_domain()) |
9f27ee59 JF |
2591 | return -ENODEV; |
2592 | ||
4bcddbae JG |
2593 | if (!xen_has_pv_disk_devices()) |
2594 | return -ENODEV; | |
2595 | ||
2596 | if (register_blkdev(XENVBD_MAJOR, DEV_NAME)) { | |
2597 | pr_warn("xen_blk: can't get major %d with name %s\n", | |
2598 | XENVBD_MAJOR, DEV_NAME); | |
2599 | return -ENODEV; | |
2600 | } | |
2601 | ||
3b4f1884 JB |
2602 | if (xen_blkif_max_segments < BLKIF_MAX_SEGMENTS_PER_REQUEST) |
2603 | xen_blkif_max_segments = BLKIF_MAX_SEGMENTS_PER_REQUEST; | |
2604 | ||
9cce2914 | 2605 | if (xen_blkif_max_ring_order > XENBUS_MAX_RING_GRANT_ORDER) { |
86839c56 | 2606 | pr_info("Invalid max_ring_order (%d), will use default max: %d.\n", |
9cce2914 | 2607 | xen_blkif_max_ring_order, XENBUS_MAX_RING_GRANT_ORDER); |
45fc8264 | 2608 | xen_blkif_max_ring_order = XENBUS_MAX_RING_GRANT_ORDER; |
86839c56 BL |
2609 | } |
2610 | ||
28d949bc BL |
2611 | if (xen_blkif_max_queues > nr_cpus) { |
2612 | pr_info("Invalid max_queues (%d), will use default max: %d.\n", | |
2613 | xen_blkif_max_queues, nr_cpus); | |
2614 | xen_blkif_max_queues = nr_cpus; | |
2615 | } | |
2616 | ||
a46b5367 JG |
2617 | INIT_DELAYED_WORK(&blkfront_work, blkfront_delay_work); |
2618 | ||
73db144b | 2619 | ret = xenbus_register_frontend(&blkfront_driver); |
469738e6 LE |
2620 | if (ret) { |
2621 | unregister_blkdev(XENVBD_MAJOR, DEV_NAME); | |
2622 | return ret; | |
2623 | } | |
2624 | ||
2625 | return 0; | |
9f27ee59 JF |
2626 | } |
2627 | module_init(xlblk_init); | |
2628 | ||
2629 | ||
5a60d0cd | 2630 | static void __exit xlblk_exit(void) |
9f27ee59 | 2631 | { |
a46b5367 JG |
2632 | cancel_delayed_work_sync(&blkfront_work); |
2633 | ||
8605067f JB |
2634 | xenbus_unregister_driver(&blkfront_driver); |
2635 | unregister_blkdev(XENVBD_MAJOR, DEV_NAME); | |
2636 | kfree(minors); | |
9f27ee59 JF |
2637 | } |
2638 | module_exit(xlblk_exit); | |
2639 | ||
2640 | MODULE_DESCRIPTION("Xen virtual block device frontend"); | |
2641 | MODULE_LICENSE("GPL"); | |
2642 | MODULE_ALIAS_BLOCKDEV_MAJOR(XENVBD_MAJOR); | |
d2f0c52b | 2643 | MODULE_ALIAS("xen:vbd"); |
4f93f09b | 2644 | MODULE_ALIAS("xenblk"); |