]>
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> |
2a48fc0a | 45 | #include <linux/mutex.h> |
9e973e64 | 46 | #include <linux/scatterlist.h> |
34ae2e47 | 47 | #include <linux/bitmap.h> |
155b7edb | 48 | #include <linux/list.h> |
a46b5367 | 49 | #include <linux/workqueue.h> |
9f27ee59 | 50 | |
1ccbf534 | 51 | #include <xen/xen.h> |
9f27ee59 JF |
52 | #include <xen/xenbus.h> |
53 | #include <xen/grant_table.h> | |
54 | #include <xen/events.h> | |
55 | #include <xen/page.h> | |
c1c5413a | 56 | #include <xen/platform_pci.h> |
9f27ee59 JF |
57 | |
58 | #include <xen/interface/grant_table.h> | |
59 | #include <xen/interface/io/blkif.h> | |
3e334239 | 60 | #include <xen/interface/io/protocols.h> |
9f27ee59 JF |
61 | |
62 | #include <asm/xen/hypervisor.h> | |
63 | ||
6cc56833 JG |
64 | /* |
65 | * The minimal size of segment supported by the block framework is PAGE_SIZE. | |
66 | * When Linux is using a different page size than Xen, it may not be possible | |
67 | * to put all the data in a single segment. | |
68 | * This can happen when the backend doesn't support indirect descriptor and | |
69 | * therefore the maximum amount of data that a request can carry is | |
70 | * BLKIF_MAX_SEGMENTS_PER_REQUEST * XEN_PAGE_SIZE = 44KB | |
71 | * | |
72 | * Note that we only support one extra request. So the Linux page size | |
73 | * should be <= ( 2 * BLKIF_MAX_SEGMENTS_PER_REQUEST * XEN_PAGE_SIZE) = | |
74 | * 88KB. | |
75 | */ | |
76 | #define HAS_EXTRA_REQ (BLKIF_MAX_SEGMENTS_PER_REQUEST < XEN_PFN_PER_PAGE) | |
77 | ||
9f27ee59 JF |
78 | enum blkif_state { |
79 | BLKIF_STATE_DISCONNECTED, | |
80 | BLKIF_STATE_CONNECTED, | |
81 | BLKIF_STATE_SUSPENDED, | |
82 | }; | |
83 | ||
0a8704a5 RPM |
84 | struct grant { |
85 | grant_ref_t gref; | |
a7a6df22 | 86 | struct page *page; |
155b7edb | 87 | struct list_head node; |
0a8704a5 RPM |
88 | }; |
89 | ||
6cc56833 JG |
90 | enum blk_req_status { |
91 | REQ_WAITING, | |
92 | REQ_DONE, | |
93 | REQ_ERROR, | |
94 | REQ_EOPNOTSUPP, | |
95 | }; | |
96 | ||
9f27ee59 JF |
97 | struct blk_shadow { |
98 | struct blkif_request req; | |
a945b980 | 99 | struct request *request; |
402b27f9 RPM |
100 | struct grant **grants_used; |
101 | struct grant **indirect_grants; | |
b7649158 | 102 | struct scatterlist *sg; |
c004a6fe | 103 | unsigned int num_sg; |
6cc56833 JG |
104 | enum blk_req_status status; |
105 | ||
106 | #define NO_ASSOCIATED_ID ~0UL | |
107 | /* | |
108 | * Id of the sibling if we ever need 2 requests when handling a | |
109 | * block I/O request | |
110 | */ | |
111 | unsigned long associated_id; | |
402b27f9 RPM |
112 | }; |
113 | ||
2609587c | 114 | struct blkif_req { |
31c4ccc3 | 115 | blk_status_t error; |
2609587c CH |
116 | }; |
117 | ||
118 | static inline struct blkif_req *blkif_req(struct request *rq) | |
119 | { | |
120 | return blk_mq_rq_to_pdu(rq); | |
121 | } | |
122 | ||
2a48fc0a | 123 | static DEFINE_MUTEX(blkfront_mutex); |
83d5cde4 | 124 | static const struct block_device_operations xlvbd_block_fops; |
a46b5367 JG |
125 | static struct delayed_work blkfront_work; |
126 | static LIST_HEAD(info_list); | |
9f27ee59 | 127 | |
402b27f9 RPM |
128 | /* |
129 | * Maximum number of segments in indirect requests, the actual value used by | |
130 | * the frontend driver is the minimum of this value and the value provided | |
131 | * by the backend driver. | |
132 | */ | |
133 | ||
134 | static unsigned int xen_blkif_max_segments = 32; | |
5657a819 | 135 | module_param_named(max_indirect_segments, xen_blkif_max_segments, uint, 0444); |
14e710fe JB |
136 | MODULE_PARM_DESC(max_indirect_segments, |
137 | "Maximum amount of segments in indirect requests (default is 32)"); | |
402b27f9 | 138 | |
28d949bc | 139 | static unsigned int xen_blkif_max_queues = 4; |
5657a819 | 140 | module_param_named(max_queues, xen_blkif_max_queues, uint, 0444); |
28d949bc BL |
141 | MODULE_PARM_DESC(max_queues, "Maximum number of hardware queues/rings used per virtual disk"); |
142 | ||
86839c56 BL |
143 | /* |
144 | * Maximum order of pages to be used for the shared ring between front and | |
145 | * backend, 4KB page granularity is used. | |
146 | */ | |
147 | static unsigned int xen_blkif_max_ring_order; | |
5657a819 | 148 | module_param_named(max_ring_page_order, xen_blkif_max_ring_order, int, 0444); |
86839c56 BL |
149 | MODULE_PARM_DESC(max_ring_page_order, "Maximum order of pages to be used for the shared ring"); |
150 | ||
c004a6fe JG |
151 | #define BLK_RING_SIZE(info) \ |
152 | __CONST_RING_SIZE(blkif, XEN_PAGE_SIZE * (info)->nr_ring_pages) | |
153 | ||
154 | #define BLK_MAX_RING_SIZE \ | |
9cce2914 | 155 | __CONST_RING_SIZE(blkif, XEN_PAGE_SIZE * XENBUS_MAX_RING_GRANTS) |
c004a6fe | 156 | |
86839c56 | 157 | /* |
6f03a7ff KRW |
158 | * ring-ref%u i=(-1UL) would take 11 characters + 'ring-ref' is 8, so 19 |
159 | * characters are enough. Define to 20 to keep consistent with backend. | |
86839c56 BL |
160 | */ |
161 | #define RINGREF_NAME_LEN (20) | |
28d949bc BL |
162 | /* |
163 | * queue-%u would take 7 + 10(UINT_MAX) = 17 characters. | |
164 | */ | |
165 | #define QUEUE_NAME_LEN (17) | |
9f27ee59 | 166 | |
81f35161 BL |
167 | /* |
168 | * Per-ring info. | |
169 | * Every blkfront device can associate with one or more blkfront_ring_info, | |
170 | * depending on how many hardware queues/rings to be used. | |
171 | */ | |
172 | struct blkfront_ring_info { | |
11659569 BL |
173 | /* Lock to protect data in every ring buffer. */ |
174 | spinlock_t ring_lock; | |
81f35161 BL |
175 | struct blkif_front_ring ring; |
176 | unsigned int ring_ref[XENBUS_MAX_RING_GRANTS]; | |
177 | unsigned int evtchn, irq; | |
178 | struct work_struct work; | |
179 | struct gnttab_free_callback callback; | |
180 | struct blk_shadow shadow[BLK_MAX_RING_SIZE]; | |
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; | |
186 | }; | |
187 | ||
9f27ee59 JF |
188 | /* |
189 | * We have one of these per vbd, whether ide, scsi or 'other'. They | |
190 | * hang in private_data off the gendisk structure. We may end up | |
191 | * putting all kinds of interesting stuff here :-) | |
192 | */ | |
193 | struct blkfront_info | |
194 | { | |
b70f5fa0 | 195 | struct mutex mutex; |
9f27ee59 | 196 | struct xenbus_device *xbdev; |
9f27ee59 | 197 | struct gendisk *gd; |
172335ad BL |
198 | u16 sector_size; |
199 | unsigned int physical_sector_size; | |
9f27ee59 JF |
200 | int vdevice; |
201 | blkif_vdev_t handle; | |
202 | enum blkif_state connected; | |
3df0e505 | 203 | /* Number of pages per ring buffer. */ |
86839c56 | 204 | unsigned int nr_ring_pages; |
9f27ee59 | 205 | struct request_queue *rq; |
b32728ff JB |
206 | unsigned int feature_flush:1; |
207 | unsigned int feature_fua:1; | |
5ea42986 KRW |
208 | unsigned int feature_discard:1; |
209 | unsigned int feature_secdiscard:1; | |
b32728ff | 210 | unsigned int feature_persistent:1; |
ed30bf31 LD |
211 | unsigned int discard_granularity; |
212 | unsigned int discard_alignment; | |
c004a6fe | 213 | /* Number of 4KB segments handled */ |
402b27f9 | 214 | unsigned int max_indirect_segments; |
1d78d705 | 215 | int is_ready; |
907c3eb1 | 216 | struct blk_mq_tag_set tag_set; |
3df0e505 BL |
217 | struct blkfront_ring_info *rinfo; |
218 | unsigned int nr_rings; | |
7b427a59 BL |
219 | /* Save uncomplete reqs and bios for migration. */ |
220 | struct list_head requests; | |
221 | struct bio_list bio_list; | |
a46b5367 | 222 | struct list_head info_list; |
9f27ee59 JF |
223 | }; |
224 | ||
0e345826 JB |
225 | static unsigned int nr_minors; |
226 | static unsigned long *minors; | |
227 | static DEFINE_SPINLOCK(minor_lock); | |
228 | ||
9f27ee59 JF |
229 | #define GRANT_INVALID_REF 0 |
230 | ||
231 | #define PARTS_PER_DISK 16 | |
9246b5f0 | 232 | #define PARTS_PER_EXT_DISK 256 |
9f27ee59 JF |
233 | |
234 | #define BLKIF_MAJOR(dev) ((dev)>>8) | |
235 | #define BLKIF_MINOR(dev) ((dev) & 0xff) | |
236 | ||
9246b5f0 CL |
237 | #define EXT_SHIFT 28 |
238 | #define EXTENDED (1<<EXT_SHIFT) | |
239 | #define VDEV_IS_EXTENDED(dev) ((dev)&(EXTENDED)) | |
240 | #define BLKIF_MINOR_EXT(dev) ((dev)&(~EXTENDED)) | |
c80a4209 SS |
241 | #define EMULATED_HD_DISK_MINOR_OFFSET (0) |
242 | #define EMULATED_HD_DISK_NAME_OFFSET (EMULATED_HD_DISK_MINOR_OFFSET / 256) | |
196cfe2a SB |
243 | #define EMULATED_SD_DISK_MINOR_OFFSET (0) |
244 | #define EMULATED_SD_DISK_NAME_OFFSET (EMULATED_SD_DISK_MINOR_OFFSET / 256) | |
9f27ee59 | 245 | |
9246b5f0 | 246 | #define DEV_NAME "xvd" /* name in /dev */ |
9f27ee59 | 247 | |
c004a6fe JG |
248 | /* |
249 | * Grants are always the same size as a Xen page (i.e 4KB). | |
250 | * A physical segment is always the same size as a Linux page. | |
251 | * Number of grants per physical segment | |
252 | */ | |
253 | #define GRANTS_PER_PSEG (PAGE_SIZE / XEN_PAGE_SIZE) | |
254 | ||
255 | #define GRANTS_PER_INDIRECT_FRAME \ | |
256 | (XEN_PAGE_SIZE / sizeof(struct blkif_request_segment)) | |
257 | ||
c004a6fe JG |
258 | #define INDIRECT_GREFS(_grants) \ |
259 | DIV_ROUND_UP(_grants, GRANTS_PER_INDIRECT_FRAME) | |
260 | ||
81f35161 | 261 | static int blkfront_setup_indirect(struct blkfront_ring_info *rinfo); |
3df0e505 | 262 | static void blkfront_gather_backend_features(struct blkfront_info *info); |
7ed8ce1c | 263 | static int negotiate_mq(struct blkfront_info *info); |
402b27f9 | 264 | |
81f35161 | 265 | static int get_id_from_freelist(struct blkfront_ring_info *rinfo) |
9f27ee59 | 266 | { |
81f35161 BL |
267 | unsigned long free = rinfo->shadow_free; |
268 | ||
269 | BUG_ON(free >= BLK_RING_SIZE(rinfo->dev_info)); | |
270 | rinfo->shadow_free = rinfo->shadow[free].req.u.rw.id; | |
271 | rinfo->shadow[free].req.u.rw.id = 0x0fffffee; /* debug */ | |
9f27ee59 JF |
272 | return free; |
273 | } | |
274 | ||
81f35161 | 275 | static int add_id_to_freelist(struct blkfront_ring_info *rinfo, |
6f03a7ff | 276 | unsigned long id) |
9f27ee59 | 277 | { |
81f35161 | 278 | if (rinfo->shadow[id].req.u.rw.id != id) |
6878c32e | 279 | return -EINVAL; |
81f35161 | 280 | if (rinfo->shadow[id].request == NULL) |
6878c32e | 281 | return -EINVAL; |
81f35161 BL |
282 | rinfo->shadow[id].req.u.rw.id = rinfo->shadow_free; |
283 | rinfo->shadow[id].request = NULL; | |
284 | rinfo->shadow_free = id; | |
6878c32e | 285 | return 0; |
9f27ee59 JF |
286 | } |
287 | ||
81f35161 | 288 | static int fill_grant_buffer(struct blkfront_ring_info *rinfo, int num) |
9c1e050c | 289 | { |
81f35161 | 290 | struct blkfront_info *info = rinfo->dev_info; |
9c1e050c RPM |
291 | struct page *granted_page; |
292 | struct grant *gnt_list_entry, *n; | |
293 | int i = 0; | |
294 | ||
6f03a7ff | 295 | while (i < num) { |
9c1e050c RPM |
296 | gnt_list_entry = kzalloc(sizeof(struct grant), GFP_NOIO); |
297 | if (!gnt_list_entry) | |
298 | goto out_of_memory; | |
299 | ||
bfe11d6d RPM |
300 | if (info->feature_persistent) { |
301 | granted_page = alloc_page(GFP_NOIO); | |
302 | if (!granted_page) { | |
303 | kfree(gnt_list_entry); | |
304 | goto out_of_memory; | |
305 | } | |
a7a6df22 | 306 | gnt_list_entry->page = granted_page; |
9c1e050c RPM |
307 | } |
308 | ||
9c1e050c | 309 | gnt_list_entry->gref = GRANT_INVALID_REF; |
73716df7 | 310 | list_add(&gnt_list_entry->node, &rinfo->grants); |
9c1e050c RPM |
311 | i++; |
312 | } | |
313 | ||
314 | return 0; | |
315 | ||
316 | out_of_memory: | |
317 | list_for_each_entry_safe(gnt_list_entry, n, | |
73716df7 | 318 | &rinfo->grants, node) { |
9c1e050c | 319 | list_del(&gnt_list_entry->node); |
bfe11d6d | 320 | if (info->feature_persistent) |
a7a6df22 | 321 | __free_page(gnt_list_entry->page); |
9c1e050c RPM |
322 | kfree(gnt_list_entry); |
323 | i--; | |
324 | } | |
325 | BUG_ON(i != 0); | |
326 | return -ENOMEM; | |
327 | } | |
328 | ||
73716df7 | 329 | static struct grant *get_free_grant(struct blkfront_ring_info *rinfo) |
9c1e050c RPM |
330 | { |
331 | struct grant *gnt_list_entry; | |
9c1e050c | 332 | |
73716df7 BL |
333 | BUG_ON(list_empty(&rinfo->grants)); |
334 | gnt_list_entry = list_first_entry(&rinfo->grants, struct grant, | |
4f503fbd | 335 | node); |
9c1e050c RPM |
336 | list_del(&gnt_list_entry->node); |
337 | ||
4f503fbd | 338 | if (gnt_list_entry->gref != GRANT_INVALID_REF) |
73716df7 | 339 | rinfo->persistent_gnts_c--; |
4f503fbd JG |
340 | |
341 | return gnt_list_entry; | |
342 | } | |
343 | ||
344 | static inline void grant_foreign_access(const struct grant *gnt_list_entry, | |
345 | const struct blkfront_info *info) | |
346 | { | |
347 | gnttab_page_grant_foreign_access_ref_one(gnt_list_entry->gref, | |
348 | info->xbdev->otherend_id, | |
349 | gnt_list_entry->page, | |
350 | 0); | |
351 | } | |
352 | ||
353 | static struct grant *get_grant(grant_ref_t *gref_head, | |
354 | unsigned long gfn, | |
73716df7 | 355 | struct blkfront_ring_info *rinfo) |
4f503fbd | 356 | { |
73716df7 BL |
357 | struct grant *gnt_list_entry = get_free_grant(rinfo); |
358 | struct blkfront_info *info = rinfo->dev_info; | |
4f503fbd JG |
359 | |
360 | if (gnt_list_entry->gref != GRANT_INVALID_REF) | |
9c1e050c | 361 | return gnt_list_entry; |
4f503fbd JG |
362 | |
363 | /* Assign a gref to this page */ | |
364 | gnt_list_entry->gref = gnttab_claim_grant_reference(gref_head); | |
365 | BUG_ON(gnt_list_entry->gref == -ENOSPC); | |
366 | if (info->feature_persistent) | |
367 | grant_foreign_access(gnt_list_entry, info); | |
368 | else { | |
369 | /* Grant access to the GFN passed by the caller */ | |
370 | gnttab_grant_foreign_access_ref(gnt_list_entry->gref, | |
371 | info->xbdev->otherend_id, | |
372 | gfn, 0); | |
9c1e050c RPM |
373 | } |
374 | ||
4f503fbd JG |
375 | return gnt_list_entry; |
376 | } | |
377 | ||
378 | static struct grant *get_indirect_grant(grant_ref_t *gref_head, | |
73716df7 | 379 | struct blkfront_ring_info *rinfo) |
4f503fbd | 380 | { |
73716df7 BL |
381 | struct grant *gnt_list_entry = get_free_grant(rinfo); |
382 | struct blkfront_info *info = rinfo->dev_info; | |
4f503fbd JG |
383 | |
384 | if (gnt_list_entry->gref != GRANT_INVALID_REF) | |
385 | return gnt_list_entry; | |
386 | ||
9c1e050c RPM |
387 | /* Assign a gref to this page */ |
388 | gnt_list_entry->gref = gnttab_claim_grant_reference(gref_head); | |
389 | BUG_ON(gnt_list_entry->gref == -ENOSPC); | |
bfe11d6d | 390 | if (!info->feature_persistent) { |
4f503fbd JG |
391 | struct page *indirect_page; |
392 | ||
393 | /* Fetch a pre-allocated page to use for indirect grefs */ | |
73716df7 BL |
394 | BUG_ON(list_empty(&rinfo->indirect_pages)); |
395 | indirect_page = list_first_entry(&rinfo->indirect_pages, | |
4f503fbd JG |
396 | struct page, lru); |
397 | list_del(&indirect_page->lru); | |
398 | gnt_list_entry->page = indirect_page; | |
bfe11d6d | 399 | } |
4f503fbd JG |
400 | grant_foreign_access(gnt_list_entry, info); |
401 | ||
9c1e050c RPM |
402 | return gnt_list_entry; |
403 | } | |
404 | ||
6878c32e KRW |
405 | static const char *op_name(int op) |
406 | { | |
407 | static const char *const names[] = { | |
408 | [BLKIF_OP_READ] = "read", | |
409 | [BLKIF_OP_WRITE] = "write", | |
410 | [BLKIF_OP_WRITE_BARRIER] = "barrier", | |
411 | [BLKIF_OP_FLUSH_DISKCACHE] = "flush", | |
412 | [BLKIF_OP_DISCARD] = "discard" }; | |
413 | ||
414 | if (op < 0 || op >= ARRAY_SIZE(names)) | |
415 | return "unknown"; | |
416 | ||
417 | if (!names[op]) | |
418 | return "reserved"; | |
419 | ||
420 | return names[op]; | |
421 | } | |
0e345826 JB |
422 | static int xlbd_reserve_minors(unsigned int minor, unsigned int nr) |
423 | { | |
424 | unsigned int end = minor + nr; | |
425 | int rc; | |
426 | ||
427 | if (end > nr_minors) { | |
428 | unsigned long *bitmap, *old; | |
429 | ||
f094148a | 430 | bitmap = kcalloc(BITS_TO_LONGS(end), sizeof(*bitmap), |
0e345826 JB |
431 | GFP_KERNEL); |
432 | if (bitmap == NULL) | |
433 | return -ENOMEM; | |
434 | ||
435 | spin_lock(&minor_lock); | |
436 | if (end > nr_minors) { | |
437 | old = minors; | |
438 | memcpy(bitmap, minors, | |
439 | BITS_TO_LONGS(nr_minors) * sizeof(*bitmap)); | |
440 | minors = bitmap; | |
441 | nr_minors = BITS_TO_LONGS(end) * BITS_PER_LONG; | |
442 | } else | |
443 | old = bitmap; | |
444 | spin_unlock(&minor_lock); | |
445 | kfree(old); | |
446 | } | |
447 | ||
448 | spin_lock(&minor_lock); | |
449 | if (find_next_bit(minors, end, minor) >= end) { | |
34ae2e47 | 450 | bitmap_set(minors, minor, nr); |
0e345826 JB |
451 | rc = 0; |
452 | } else | |
453 | rc = -EBUSY; | |
454 | spin_unlock(&minor_lock); | |
455 | ||
456 | return rc; | |
457 | } | |
458 | ||
459 | static void xlbd_release_minors(unsigned int minor, unsigned int nr) | |
460 | { | |
461 | unsigned int end = minor + nr; | |
462 | ||
463 | BUG_ON(end > nr_minors); | |
464 | spin_lock(&minor_lock); | |
34ae2e47 | 465 | bitmap_clear(minors, minor, nr); |
0e345826 JB |
466 | spin_unlock(&minor_lock); |
467 | } | |
468 | ||
9f27ee59 JF |
469 | static void blkif_restart_queue_callback(void *arg) |
470 | { | |
81f35161 BL |
471 | struct blkfront_ring_info *rinfo = (struct blkfront_ring_info *)arg; |
472 | schedule_work(&rinfo->work); | |
9f27ee59 JF |
473 | } |
474 | ||
afe42d7d | 475 | static int blkif_getgeo(struct block_device *bd, struct hd_geometry *hg) |
597592d9 IC |
476 | { |
477 | /* We don't have real geometry info, but let's at least return | |
478 | values consistent with the size of the device */ | |
479 | sector_t nsect = get_capacity(bd->bd_disk); | |
480 | sector_t cylinders = nsect; | |
481 | ||
482 | hg->heads = 0xff; | |
483 | hg->sectors = 0x3f; | |
484 | sector_div(cylinders, hg->heads * hg->sectors); | |
485 | hg->cylinders = cylinders; | |
486 | if ((sector_t)(hg->cylinders + 1) * hg->heads * hg->sectors < nsect) | |
487 | hg->cylinders = 0xffff; | |
488 | return 0; | |
489 | } | |
490 | ||
a63c848b | 491 | static int blkif_ioctl(struct block_device *bdev, fmode_t mode, |
62aa0054 | 492 | unsigned command, unsigned long argument) |
440a01a7 | 493 | { |
a63c848b | 494 | struct blkfront_info *info = bdev->bd_disk->private_data; |
440a01a7 CL |
495 | int i; |
496 | ||
497 | dev_dbg(&info->xbdev->dev, "command: 0x%x, argument: 0x%lx\n", | |
498 | command, (long)argument); | |
499 | ||
500 | switch (command) { | |
501 | case CDROMMULTISESSION: | |
502 | dev_dbg(&info->xbdev->dev, "FIXME: support multisession CDs later\n"); | |
503 | for (i = 0; i < sizeof(struct cdrom_multisession); i++) | |
504 | if (put_user(0, (char __user *)(argument + i))) | |
505 | return -EFAULT; | |
506 | return 0; | |
507 | ||
508 | case CDROM_GET_CAPABILITY: { | |
509 | struct gendisk *gd = info->gd; | |
510 | if (gd->flags & GENHD_FL_CD) | |
511 | return 0; | |
512 | return -EINVAL; | |
513 | } | |
514 | ||
515 | default: | |
516 | /*printk(KERN_ALERT "ioctl %08x not supported by Xen blkdev\n", | |
517 | command);*/ | |
518 | return -EINVAL; /* same return as native Linux */ | |
519 | } | |
520 | ||
521 | return 0; | |
522 | } | |
523 | ||
2e073969 JG |
524 | static unsigned long blkif_ring_get_request(struct blkfront_ring_info *rinfo, |
525 | struct request *req, | |
526 | struct blkif_request **ring_req) | |
527 | { | |
528 | unsigned long id; | |
529 | ||
530 | *ring_req = RING_GET_REQUEST(&rinfo->ring, rinfo->ring.req_prod_pvt); | |
531 | rinfo->ring.req_prod_pvt++; | |
532 | ||
533 | id = get_id_from_freelist(rinfo); | |
534 | rinfo->shadow[id].request = req; | |
6cc56833 JG |
535 | rinfo->shadow[id].status = REQ_WAITING; |
536 | rinfo->shadow[id].associated_id = NO_ASSOCIATED_ID; | |
2e073969 JG |
537 | |
538 | (*ring_req)->u.rw.id = id; | |
539 | ||
540 | return id; | |
541 | } | |
542 | ||
81f35161 | 543 | static int blkif_queue_discard_req(struct request *req, struct blkfront_ring_info *rinfo) |
9f27ee59 | 544 | { |
81f35161 | 545 | struct blkfront_info *info = rinfo->dev_info; |
9f27ee59 | 546 | struct blkif_request *ring_req; |
9f27ee59 | 547 | unsigned long id; |
33204663 JG |
548 | |
549 | /* Fill out a communications ring structure. */ | |
2e073969 | 550 | id = blkif_ring_get_request(rinfo, req, &ring_req); |
33204663 JG |
551 | |
552 | ring_req->operation = BLKIF_OP_DISCARD; | |
553 | ring_req->u.discard.nr_sectors = blk_rq_sectors(req); | |
554 | ring_req->u.discard.id = id; | |
555 | ring_req->u.discard.sector_number = (blkif_sector_t)blk_rq_pos(req); | |
288dab8a | 556 | if (req_op(req) == REQ_OP_SECURE_ERASE && info->feature_secdiscard) |
33204663 JG |
557 | ring_req->u.discard.flag = BLKIF_DISCARD_SECURE; |
558 | else | |
559 | ring_req->u.discard.flag = 0; | |
560 | ||
33204663 | 561 | /* Keep a private copy so we can reissue requests when recovering. */ |
81f35161 | 562 | rinfo->shadow[id].req = *ring_req; |
33204663 JG |
563 | |
564 | return 0; | |
565 | } | |
566 | ||
c004a6fe JG |
567 | struct setup_rw_req { |
568 | unsigned int grant_idx; | |
569 | struct blkif_request_segment *segments; | |
81f35161 | 570 | struct blkfront_ring_info *rinfo; |
c004a6fe JG |
571 | struct blkif_request *ring_req; |
572 | grant_ref_t gref_head; | |
573 | unsigned int id; | |
574 | /* Only used when persistent grant is used and it's a read request */ | |
575 | bool need_copy; | |
576 | unsigned int bvec_off; | |
577 | char *bvec_data; | |
6cc56833 JG |
578 | |
579 | bool require_extra_req; | |
580 | struct blkif_request *extra_ring_req; | |
c004a6fe JG |
581 | }; |
582 | ||
583 | static void blkif_setup_rw_req_grant(unsigned long gfn, unsigned int offset, | |
584 | unsigned int len, void *data) | |
585 | { | |
586 | struct setup_rw_req *setup = data; | |
587 | int n, ref; | |
588 | struct grant *gnt_list_entry; | |
9f27ee59 | 589 | unsigned int fsect, lsect; |
c004a6fe JG |
590 | /* Convenient aliases */ |
591 | unsigned int grant_idx = setup->grant_idx; | |
592 | struct blkif_request *ring_req = setup->ring_req; | |
81f35161 | 593 | struct blkfront_ring_info *rinfo = setup->rinfo; |
6cc56833 JG |
594 | /* |
595 | * We always use the shadow of the first request to store the list | |
596 | * of grant associated to the block I/O request. This made the | |
597 | * completion more easy to handle even if the block I/O request is | |
598 | * split. | |
599 | */ | |
81f35161 | 600 | struct blk_shadow *shadow = &rinfo->shadow[setup->id]; |
c004a6fe | 601 | |
6cc56833 JG |
602 | if (unlikely(setup->require_extra_req && |
603 | grant_idx >= BLKIF_MAX_SEGMENTS_PER_REQUEST)) { | |
604 | /* | |
605 | * We are using the second request, setup grant_idx | |
606 | * to be the index of the segment array. | |
607 | */ | |
608 | grant_idx -= BLKIF_MAX_SEGMENTS_PER_REQUEST; | |
609 | ring_req = setup->extra_ring_req; | |
610 | } | |
611 | ||
c004a6fe JG |
612 | if ((ring_req->operation == BLKIF_OP_INDIRECT) && |
613 | (grant_idx % GRANTS_PER_INDIRECT_FRAME == 0)) { | |
614 | if (setup->segments) | |
615 | kunmap_atomic(setup->segments); | |
616 | ||
617 | n = grant_idx / GRANTS_PER_INDIRECT_FRAME; | |
73716df7 | 618 | gnt_list_entry = get_indirect_grant(&setup->gref_head, rinfo); |
c004a6fe JG |
619 | shadow->indirect_grants[n] = gnt_list_entry; |
620 | setup->segments = kmap_atomic(gnt_list_entry->page); | |
621 | ring_req->u.indirect.indirect_grefs[n] = gnt_list_entry->gref; | |
622 | } | |
623 | ||
73716df7 | 624 | gnt_list_entry = get_grant(&setup->gref_head, gfn, rinfo); |
c004a6fe | 625 | ref = gnt_list_entry->gref; |
6cc56833 JG |
626 | /* |
627 | * All the grants are stored in the shadow of the first | |
628 | * request. Therefore we have to use the global index. | |
629 | */ | |
630 | shadow->grants_used[setup->grant_idx] = gnt_list_entry; | |
c004a6fe JG |
631 | |
632 | if (setup->need_copy) { | |
633 | void *shared_data; | |
634 | ||
635 | shared_data = kmap_atomic(gnt_list_entry->page); | |
636 | /* | |
637 | * this does not wipe data stored outside the | |
638 | * range sg->offset..sg->offset+sg->length. | |
639 | * Therefore, blkback *could* see data from | |
640 | * previous requests. This is OK as long as | |
641 | * persistent grants are shared with just one | |
642 | * domain. It may need refactoring if this | |
643 | * changes | |
644 | */ | |
645 | memcpy(shared_data + offset, | |
646 | setup->bvec_data + setup->bvec_off, | |
647 | len); | |
648 | ||
649 | kunmap_atomic(shared_data); | |
650 | setup->bvec_off += len; | |
651 | } | |
652 | ||
653 | fsect = offset >> 9; | |
654 | lsect = fsect + (len >> 9) - 1; | |
655 | if (ring_req->operation != BLKIF_OP_INDIRECT) { | |
656 | ring_req->u.rw.seg[grant_idx] = | |
657 | (struct blkif_request_segment) { | |
658 | .gref = ref, | |
659 | .first_sect = fsect, | |
660 | .last_sect = lsect }; | |
661 | } else { | |
662 | setup->segments[grant_idx % GRANTS_PER_INDIRECT_FRAME] = | |
663 | (struct blkif_request_segment) { | |
664 | .gref = ref, | |
665 | .first_sect = fsect, | |
666 | .last_sect = lsect }; | |
667 | } | |
668 | ||
669 | (setup->grant_idx)++; | |
670 | } | |
671 | ||
6cc56833 JG |
672 | static void blkif_setup_extra_req(struct blkif_request *first, |
673 | struct blkif_request *second) | |
674 | { | |
675 | uint16_t nr_segments = first->u.rw.nr_segments; | |
676 | ||
677 | /* | |
678 | * The second request is only present when the first request uses | |
679 | * all its segments. It's always the continuity of the first one. | |
680 | */ | |
681 | first->u.rw.nr_segments = BLKIF_MAX_SEGMENTS_PER_REQUEST; | |
682 | ||
683 | second->u.rw.nr_segments = nr_segments - BLKIF_MAX_SEGMENTS_PER_REQUEST; | |
684 | second->u.rw.sector_number = first->u.rw.sector_number + | |
685 | (BLKIF_MAX_SEGMENTS_PER_REQUEST * XEN_PAGE_SIZE) / 512; | |
686 | ||
687 | second->u.rw.handle = first->u.rw.handle; | |
688 | second->operation = first->operation; | |
689 | } | |
690 | ||
81f35161 | 691 | static int blkif_queue_rw_req(struct request *req, struct blkfront_ring_info *rinfo) |
9f27ee59 | 692 | { |
81f35161 | 693 | struct blkfront_info *info = rinfo->dev_info; |
6cc56833 JG |
694 | struct blkif_request *ring_req, *extra_ring_req = NULL; |
695 | unsigned long id, extra_id = NO_ASSOCIATED_ID; | |
696 | bool require_extra_req = false; | |
c004a6fe JG |
697 | int i; |
698 | struct setup_rw_req setup = { | |
699 | .grant_idx = 0, | |
700 | .segments = NULL, | |
81f35161 | 701 | .rinfo = rinfo, |
c004a6fe JG |
702 | .need_copy = rq_data_dir(req) && info->feature_persistent, |
703 | }; | |
0a8704a5 RPM |
704 | |
705 | /* | |
706 | * Used to store if we are able to queue the request by just using | |
707 | * existing persistent grants, or if we have to get new grants, | |
708 | * as there are not sufficiently many free. | |
709 | */ | |
bd912ef3 | 710 | bool new_persistent_gnts = false; |
9e973e64 | 711 | struct scatterlist *sg; |
c004a6fe | 712 | int num_sg, max_grefs, num_grant; |
9f27ee59 | 713 | |
c004a6fe | 714 | max_grefs = req->nr_phys_segments * GRANTS_PER_PSEG; |
c47206e2 RPM |
715 | if (max_grefs > BLKIF_MAX_SEGMENTS_PER_REQUEST) |
716 | /* | |
717 | * If we are using indirect segments we need to account | |
718 | * for the indirect grefs used in the request. | |
719 | */ | |
c004a6fe | 720 | max_grefs += INDIRECT_GREFS(max_grefs); |
402b27f9 | 721 | |
bd912ef3 DZ |
722 | /* Check if we have enough persistent grants to allocate a requests */ |
723 | if (rinfo->persistent_gnts_c < max_grefs) { | |
724 | new_persistent_gnts = true; | |
725 | ||
726 | if (gnttab_alloc_grant_references( | |
727 | max_grefs - rinfo->persistent_gnts_c, | |
728 | &setup.gref_head) < 0) { | |
0a8704a5 | 729 | gnttab_request_free_callback( |
81f35161 | 730 | &rinfo->callback, |
0a8704a5 | 731 | blkif_restart_queue_callback, |
81f35161 | 732 | rinfo, |
bd912ef3 | 733 | max_grefs - rinfo->persistent_gnts_c); |
0a8704a5 RPM |
734 | return 1; |
735 | } | |
bd912ef3 | 736 | } |
9f27ee59 JF |
737 | |
738 | /* Fill out a communications ring structure. */ | |
2e073969 | 739 | id = blkif_ring_get_request(rinfo, req, &ring_req); |
9f27ee59 | 740 | |
81f35161 | 741 | num_sg = blk_rq_map_sg(req->q, req, rinfo->shadow[id].sg); |
c004a6fe JG |
742 | num_grant = 0; |
743 | /* Calculate the number of grant used */ | |
81f35161 | 744 | for_each_sg(rinfo->shadow[id].sg, sg, num_sg, i) |
c004a6fe JG |
745 | num_grant += gnttab_count_grant(sg->offset, sg->length); |
746 | ||
6cc56833 JG |
747 | require_extra_req = info->max_indirect_segments == 0 && |
748 | num_grant > BLKIF_MAX_SEGMENTS_PER_REQUEST; | |
749 | BUG_ON(!HAS_EXTRA_REQ && require_extra_req); | |
750 | ||
81f35161 | 751 | rinfo->shadow[id].num_sg = num_sg; |
6cc56833 JG |
752 | if (num_grant > BLKIF_MAX_SEGMENTS_PER_REQUEST && |
753 | likely(!require_extra_req)) { | |
33204663 JG |
754 | /* |
755 | * The indirect operation can only be a BLKIF_OP_READ or | |
756 | * BLKIF_OP_WRITE | |
757 | */ | |
3a5e02ce | 758 | BUG_ON(req_op(req) == REQ_OP_FLUSH || req->cmd_flags & REQ_FUA); |
33204663 JG |
759 | ring_req->operation = BLKIF_OP_INDIRECT; |
760 | ring_req->u.indirect.indirect_op = rq_data_dir(req) ? | |
761 | BLKIF_OP_WRITE : BLKIF_OP_READ; | |
762 | ring_req->u.indirect.sector_number = (blkif_sector_t)blk_rq_pos(req); | |
763 | ring_req->u.indirect.handle = info->handle; | |
c004a6fe | 764 | ring_req->u.indirect.nr_segments = num_grant; |
ed30bf31 | 765 | } else { |
33204663 JG |
766 | ring_req->u.rw.sector_number = (blkif_sector_t)blk_rq_pos(req); |
767 | ring_req->u.rw.handle = info->handle; | |
768 | ring_req->operation = rq_data_dir(req) ? | |
769 | BLKIF_OP_WRITE : BLKIF_OP_READ; | |
3a5e02ce | 770 | if (req_op(req) == REQ_OP_FLUSH || req->cmd_flags & REQ_FUA) { |
402b27f9 | 771 | /* |
33204663 JG |
772 | * Ideally we can do an unordered flush-to-disk. |
773 | * In case the backend onlysupports barriers, use that. | |
774 | * A barrier request a superset of FUA, so we can | |
775 | * implement it the same way. (It's also a FLUSH+FUA, | |
776 | * since it is guaranteed ordered WRT previous writes.) | |
402b27f9 | 777 | */ |
a418090a | 778 | if (info->feature_flush && info->feature_fua) |
33204663 JG |
779 | ring_req->operation = |
780 | BLKIF_OP_WRITE_BARRIER; | |
a418090a | 781 | else if (info->feature_flush) |
33204663 JG |
782 | ring_req->operation = |
783 | BLKIF_OP_FLUSH_DISKCACHE; | |
a418090a | 784 | else |
33204663 | 785 | ring_req->operation = 0; |
402b27f9 | 786 | } |
c004a6fe | 787 | ring_req->u.rw.nr_segments = num_grant; |
6cc56833 JG |
788 | if (unlikely(require_extra_req)) { |
789 | extra_id = blkif_ring_get_request(rinfo, req, | |
790 | &extra_ring_req); | |
791 | /* | |
792 | * Only the first request contains the scatter-gather | |
793 | * list. | |
794 | */ | |
795 | rinfo->shadow[extra_id].num_sg = 0; | |
796 | ||
797 | blkif_setup_extra_req(ring_req, extra_ring_req); | |
798 | ||
799 | /* Link the 2 requests together */ | |
800 | rinfo->shadow[extra_id].associated_id = id; | |
801 | rinfo->shadow[id].associated_id = extra_id; | |
802 | } | |
33204663 | 803 | } |
0a8704a5 | 804 | |
c004a6fe JG |
805 | setup.ring_req = ring_req; |
806 | setup.id = id; | |
6cc56833 JG |
807 | |
808 | setup.require_extra_req = require_extra_req; | |
809 | if (unlikely(require_extra_req)) | |
810 | setup.extra_ring_req = extra_ring_req; | |
811 | ||
81f35161 | 812 | for_each_sg(rinfo->shadow[id].sg, sg, num_sg, i) { |
c004a6fe | 813 | BUG_ON(sg->offset + sg->length > PAGE_SIZE); |
0a8704a5 | 814 | |
c004a6fe JG |
815 | if (setup.need_copy) { |
816 | setup.bvec_off = sg->offset; | |
817 | setup.bvec_data = kmap_atomic(sg_page(sg)); | |
818 | } | |
0a8704a5 | 819 | |
c004a6fe JG |
820 | gnttab_foreach_grant_in_range(sg_page(sg), |
821 | sg->offset, | |
822 | sg->length, | |
823 | blkif_setup_rw_req_grant, | |
824 | &setup); | |
0a8704a5 | 825 | |
c004a6fe JG |
826 | if (setup.need_copy) |
827 | kunmap_atomic(setup.bvec_data); | |
9f27ee59 | 828 | } |
c004a6fe JG |
829 | if (setup.segments) |
830 | kunmap_atomic(setup.segments); | |
9f27ee59 | 831 | |
9f27ee59 | 832 | /* Keep a private copy so we can reissue requests when recovering. */ |
81f35161 | 833 | rinfo->shadow[id].req = *ring_req; |
6cc56833 JG |
834 | if (unlikely(require_extra_req)) |
835 | rinfo->shadow[extra_id].req = *extra_ring_req; | |
9f27ee59 | 836 | |
bd912ef3 | 837 | if (new_persistent_gnts) |
c004a6fe | 838 | gnttab_free_grant_references(setup.gref_head); |
9f27ee59 JF |
839 | |
840 | return 0; | |
841 | } | |
842 | ||
33204663 JG |
843 | /* |
844 | * Generate a Xen blkfront IO request from a blk layer request. Reads | |
845 | * and writes are handled as expected. | |
846 | * | |
847 | * @req: a request struct | |
848 | */ | |
81f35161 | 849 | static int blkif_queue_request(struct request *req, struct blkfront_ring_info *rinfo) |
33204663 | 850 | { |
81f35161 | 851 | if (unlikely(rinfo->dev_info->connected != BLKIF_STATE_CONNECTED)) |
33204663 JG |
852 | return 1; |
853 | ||
c2df40df | 854 | if (unlikely(req_op(req) == REQ_OP_DISCARD || |
288dab8a | 855 | req_op(req) == REQ_OP_SECURE_ERASE)) |
81f35161 | 856 | return blkif_queue_discard_req(req, rinfo); |
33204663 | 857 | else |
81f35161 | 858 | return blkif_queue_rw_req(req, rinfo); |
33204663 | 859 | } |
9f27ee59 | 860 | |
81f35161 | 861 | static inline void flush_requests(struct blkfront_ring_info *rinfo) |
9f27ee59 JF |
862 | { |
863 | int notify; | |
864 | ||
81f35161 | 865 | RING_PUSH_REQUESTS_AND_CHECK_NOTIFY(&rinfo->ring, notify); |
9f27ee59 JF |
866 | |
867 | if (notify) | |
81f35161 | 868 | notify_remote_via_irq(rinfo->irq); |
9f27ee59 JF |
869 | } |
870 | ||
ad42d391 VK |
871 | static inline bool blkif_request_flush_invalid(struct request *req, |
872 | struct blkfront_info *info) | |
0f1ca65e | 873 | { |
aebf526b | 874 | return (blk_rq_is_passthrough(req) || |
3a5e02ce | 875 | ((req_op(req) == REQ_OP_FLUSH) && |
a418090a | 876 | !info->feature_flush) || |
ad42d391 | 877 | ((req->cmd_flags & REQ_FUA) && |
a418090a | 878 | !info->feature_fua)); |
0f1ca65e AA |
879 | } |
880 | ||
fc17b653 | 881 | static blk_status_t blkif_queue_rq(struct blk_mq_hw_ctx *hctx, |
6f03a7ff | 882 | const struct blk_mq_queue_data *qd) |
9f27ee59 | 883 | { |
11659569 | 884 | unsigned long flags; |
2a6f71ad BL |
885 | int qid = hctx->queue_num; |
886 | struct blkfront_info *info = hctx->queue->queuedata; | |
887 | struct blkfront_ring_info *rinfo = NULL; | |
9f27ee59 | 888 | |
2a6f71ad BL |
889 | BUG_ON(info->nr_rings <= qid); |
890 | rinfo = &info->rinfo[qid]; | |
907c3eb1 | 891 | blk_mq_start_request(qd->rq); |
11659569 | 892 | spin_lock_irqsave(&rinfo->ring_lock, flags); |
81f35161 | 893 | if (RING_FULL(&rinfo->ring)) |
907c3eb1 | 894 | goto out_busy; |
9f27ee59 | 895 | |
81f35161 | 896 | if (blkif_request_flush_invalid(qd->rq, rinfo->dev_info)) |
907c3eb1 | 897 | goto out_err; |
296b2f6a | 898 | |
81f35161 | 899 | if (blkif_queue_request(qd->rq, rinfo)) |
907c3eb1 | 900 | goto out_busy; |
296b2f6a | 901 | |
81f35161 | 902 | flush_requests(rinfo); |
11659569 | 903 | spin_unlock_irqrestore(&rinfo->ring_lock, flags); |
fc17b653 | 904 | return BLK_STS_OK; |
9f27ee59 | 905 | |
907c3eb1 | 906 | out_err: |
11659569 | 907 | spin_unlock_irqrestore(&rinfo->ring_lock, flags); |
fc17b653 | 908 | return BLK_STS_IOERR; |
9f27ee59 | 909 | |
907c3eb1 | 910 | out_busy: |
907c3eb1 | 911 | blk_mq_stop_hw_queue(hctx); |
4b422cb9 | 912 | spin_unlock_irqrestore(&rinfo->ring_lock, flags); |
86ff7c2a | 913 | return BLK_STS_DEV_RESOURCE; |
9f27ee59 JF |
914 | } |
915 | ||
2609587c CH |
916 | static void blkif_complete_rq(struct request *rq) |
917 | { | |
918 | blk_mq_end_request(rq, blkif_req(rq)->error); | |
919 | } | |
920 | ||
f363b089 | 921 | static const struct blk_mq_ops blkfront_mq_ops = { |
907c3eb1 | 922 | .queue_rq = blkif_queue_rq, |
2609587c | 923 | .complete = blkif_complete_rq, |
907c3eb1 BL |
924 | }; |
925 | ||
172335ad BL |
926 | static void blkif_set_queue_limits(struct blkfront_info *info) |
927 | { | |
928 | struct request_queue *rq = info->rq; | |
929 | struct gendisk *gd = info->gd; | |
930 | unsigned int segments = info->max_indirect_segments ? : | |
931 | BLKIF_MAX_SEGMENTS_PER_REQUEST; | |
932 | ||
8b904b5b | 933 | blk_queue_flag_set(QUEUE_FLAG_VIRT, rq); |
172335ad BL |
934 | |
935 | if (info->feature_discard) { | |
8b904b5b | 936 | blk_queue_flag_set(QUEUE_FLAG_DISCARD, rq); |
172335ad BL |
937 | blk_queue_max_discard_sectors(rq, get_capacity(gd)); |
938 | rq->limits.discard_granularity = info->discard_granularity; | |
939 | rq->limits.discard_alignment = info->discard_alignment; | |
940 | if (info->feature_secdiscard) | |
8b904b5b | 941 | blk_queue_flag_set(QUEUE_FLAG_SECERASE, rq); |
172335ad BL |
942 | } |
943 | ||
944 | /* Hard sector size and max sectors impersonate the equiv. hardware. */ | |
945 | blk_queue_logical_block_size(rq, info->sector_size); | |
946 | blk_queue_physical_block_size(rq, info->physical_sector_size); | |
947 | blk_queue_max_hw_sectors(rq, (segments * XEN_PAGE_SIZE) / 512); | |
948 | ||
949 | /* Each segment in a request is up to an aligned page in size. */ | |
950 | blk_queue_segment_boundary(rq, PAGE_SIZE - 1); | |
951 | blk_queue_max_segment_size(rq, PAGE_SIZE); | |
952 | ||
953 | /* Ensure a merged request will fit in a single I/O ring slot. */ | |
954 | blk_queue_max_segments(rq, segments / GRANTS_PER_PSEG); | |
955 | ||
956 | /* Make sure buffer addresses are sector-aligned. */ | |
957 | blk_queue_dma_alignment(rq, 511); | |
172335ad BL |
958 | } |
959 | ||
402b27f9 | 960 | static int xlvbd_init_blk_queue(struct gendisk *gd, u16 sector_size, |
172335ad | 961 | unsigned int physical_sector_size) |
9f27ee59 | 962 | { |
165125e1 | 963 | struct request_queue *rq; |
ed30bf31 | 964 | struct blkfront_info *info = gd->private_data; |
9f27ee59 | 965 | |
907c3eb1 BL |
966 | memset(&info->tag_set, 0, sizeof(info->tag_set)); |
967 | info->tag_set.ops = &blkfront_mq_ops; | |
28d949bc | 968 | info->tag_set.nr_hw_queues = info->nr_rings; |
6cc56833 JG |
969 | if (HAS_EXTRA_REQ && info->max_indirect_segments == 0) { |
970 | /* | |
971 | * When indirect descriptior is not supported, the I/O request | |
972 | * will be split between multiple request in the ring. | |
973 | * To avoid problems when sending the request, divide by | |
974 | * 2 the depth of the queue. | |
975 | */ | |
976 | info->tag_set.queue_depth = BLK_RING_SIZE(info) / 2; | |
977 | } else | |
978 | info->tag_set.queue_depth = BLK_RING_SIZE(info); | |
907c3eb1 | 979 | info->tag_set.numa_node = NUMA_NO_NODE; |
56d18f62 | 980 | info->tag_set.flags = BLK_MQ_F_SHOULD_MERGE; |
2609587c | 981 | info->tag_set.cmd_size = sizeof(struct blkif_req); |
907c3eb1 BL |
982 | info->tag_set.driver_data = info; |
983 | ||
984 | if (blk_mq_alloc_tag_set(&info->tag_set)) | |
bde21f73 | 985 | return -EINVAL; |
907c3eb1 BL |
986 | rq = blk_mq_init_queue(&info->tag_set); |
987 | if (IS_ERR(rq)) { | |
988 | blk_mq_free_tag_set(&info->tag_set); | |
bde21f73 | 989 | return PTR_ERR(rq); |
907c3eb1 | 990 | } |
9f27ee59 | 991 | |
2a6f71ad | 992 | rq->queuedata = info; |
172335ad BL |
993 | info->rq = gd->queue = rq; |
994 | info->gd = gd; | |
995 | info->sector_size = sector_size; | |
996 | info->physical_sector_size = physical_sector_size; | |
997 | blkif_set_queue_limits(info); | |
9f27ee59 JF |
998 | |
999 | return 0; | |
1000 | } | |
1001 | ||
a418090a | 1002 | static const char *flush_info(struct blkfront_info *info) |
fdf9b965 | 1003 | { |
a418090a | 1004 | if (info->feature_flush && info->feature_fua) |
fdf9b965 | 1005 | return "barrier: enabled;"; |
a418090a | 1006 | else if (info->feature_flush) |
fdf9b965 | 1007 | return "flush diskcache: enabled;"; |
a418090a | 1008 | else |
fdf9b965 | 1009 | return "barrier or flush: disabled;"; |
fdf9b965 | 1010 | } |
9f27ee59 | 1011 | |
4913efe4 | 1012 | static void xlvbd_flush(struct blkfront_info *info) |
9f27ee59 | 1013 | { |
a418090a MC |
1014 | blk_queue_write_cache(info->rq, info->feature_flush ? true : false, |
1015 | info->feature_fua ? true : false); | |
fdf9b965 | 1016 | pr_info("blkfront: %s: %s %s %s %s %s\n", |
a418090a | 1017 | info->gd->disk_name, flush_info(info), |
fdf9b965 VK |
1018 | "persistent grants:", info->feature_persistent ? |
1019 | "enabled;" : "disabled;", "indirect descriptors:", | |
1020 | info->max_indirect_segments ? "enabled;" : "disabled;"); | |
9f27ee59 JF |
1021 | } |
1022 | ||
c80a4209 SS |
1023 | static int xen_translate_vdev(int vdevice, int *minor, unsigned int *offset) |
1024 | { | |
1025 | int major; | |
1026 | major = BLKIF_MAJOR(vdevice); | |
1027 | *minor = BLKIF_MINOR(vdevice); | |
1028 | switch (major) { | |
1029 | case XEN_IDE0_MAJOR: | |
1030 | *offset = (*minor / 64) + EMULATED_HD_DISK_NAME_OFFSET; | |
1031 | *minor = ((*minor / 64) * PARTS_PER_DISK) + | |
1032 | EMULATED_HD_DISK_MINOR_OFFSET; | |
1033 | break; | |
1034 | case XEN_IDE1_MAJOR: | |
1035 | *offset = (*minor / 64) + 2 + EMULATED_HD_DISK_NAME_OFFSET; | |
1036 | *minor = (((*minor / 64) + 2) * PARTS_PER_DISK) + | |
1037 | EMULATED_HD_DISK_MINOR_OFFSET; | |
1038 | break; | |
1039 | case XEN_SCSI_DISK0_MAJOR: | |
1040 | *offset = (*minor / PARTS_PER_DISK) + EMULATED_SD_DISK_NAME_OFFSET; | |
1041 | *minor = *minor + EMULATED_SD_DISK_MINOR_OFFSET; | |
1042 | break; | |
1043 | case XEN_SCSI_DISK1_MAJOR: | |
1044 | case XEN_SCSI_DISK2_MAJOR: | |
1045 | case XEN_SCSI_DISK3_MAJOR: | |
1046 | case XEN_SCSI_DISK4_MAJOR: | |
1047 | case XEN_SCSI_DISK5_MAJOR: | |
1048 | case XEN_SCSI_DISK6_MAJOR: | |
1049 | case XEN_SCSI_DISK7_MAJOR: | |
1050 | *offset = (*minor / PARTS_PER_DISK) + | |
1051 | ((major - XEN_SCSI_DISK1_MAJOR + 1) * 16) + | |
1052 | EMULATED_SD_DISK_NAME_OFFSET; | |
1053 | *minor = *minor + | |
1054 | ((major - XEN_SCSI_DISK1_MAJOR + 1) * 16 * PARTS_PER_DISK) + | |
1055 | EMULATED_SD_DISK_MINOR_OFFSET; | |
1056 | break; | |
1057 | case XEN_SCSI_DISK8_MAJOR: | |
1058 | case XEN_SCSI_DISK9_MAJOR: | |
1059 | case XEN_SCSI_DISK10_MAJOR: | |
1060 | case XEN_SCSI_DISK11_MAJOR: | |
1061 | case XEN_SCSI_DISK12_MAJOR: | |
1062 | case XEN_SCSI_DISK13_MAJOR: | |
1063 | case XEN_SCSI_DISK14_MAJOR: | |
1064 | case XEN_SCSI_DISK15_MAJOR: | |
1065 | *offset = (*minor / PARTS_PER_DISK) + | |
1066 | ((major - XEN_SCSI_DISK8_MAJOR + 8) * 16) + | |
1067 | EMULATED_SD_DISK_NAME_OFFSET; | |
1068 | *minor = *minor + | |
1069 | ((major - XEN_SCSI_DISK8_MAJOR + 8) * 16 * PARTS_PER_DISK) + | |
1070 | EMULATED_SD_DISK_MINOR_OFFSET; | |
1071 | break; | |
1072 | case XENVBD_MAJOR: | |
1073 | *offset = *minor / PARTS_PER_DISK; | |
1074 | break; | |
1075 | default: | |
1076 | printk(KERN_WARNING "blkfront: your disk configuration is " | |
1077 | "incorrect, please use an xvd device instead\n"); | |
1078 | return -ENODEV; | |
1079 | } | |
1080 | return 0; | |
1081 | } | |
9f27ee59 | 1082 | |
e77c78c0 JB |
1083 | static char *encode_disk_name(char *ptr, unsigned int n) |
1084 | { | |
1085 | if (n >= 26) | |
1086 | ptr = encode_disk_name(ptr, n / 26 - 1); | |
1087 | *ptr = 'a' + n % 26; | |
1088 | return ptr + 1; | |
1089 | } | |
1090 | ||
9246b5f0 CL |
1091 | static int xlvbd_alloc_gendisk(blkif_sector_t capacity, |
1092 | struct blkfront_info *info, | |
7c4d7d71 SB |
1093 | u16 vdisk_info, u16 sector_size, |
1094 | unsigned int physical_sector_size) | |
9f27ee59 JF |
1095 | { |
1096 | struct gendisk *gd; | |
1097 | int nr_minors = 1; | |
c80a4209 | 1098 | int err; |
9246b5f0 CL |
1099 | unsigned int offset; |
1100 | int minor; | |
1101 | int nr_parts; | |
e77c78c0 | 1102 | char *ptr; |
9f27ee59 JF |
1103 | |
1104 | BUG_ON(info->gd != NULL); | |
1105 | BUG_ON(info->rq != NULL); | |
1106 | ||
9246b5f0 CL |
1107 | if ((info->vdevice>>EXT_SHIFT) > 1) { |
1108 | /* this is above the extended range; something is wrong */ | |
1109 | printk(KERN_WARNING "blkfront: vdevice 0x%x is above the extended range; ignoring\n", info->vdevice); | |
1110 | return -ENODEV; | |
1111 | } | |
1112 | ||
1113 | if (!VDEV_IS_EXTENDED(info->vdevice)) { | |
c80a4209 SS |
1114 | err = xen_translate_vdev(info->vdevice, &minor, &offset); |
1115 | if (err) | |
1116 | return err; | |
1117 | nr_parts = PARTS_PER_DISK; | |
9246b5f0 CL |
1118 | } else { |
1119 | minor = BLKIF_MINOR_EXT(info->vdevice); | |
1120 | nr_parts = PARTS_PER_EXT_DISK; | |
c80a4209 | 1121 | offset = minor / nr_parts; |
89153b5c | 1122 | if (xen_hvm_domain() && offset < EMULATED_HD_DISK_NAME_OFFSET + 4) |
c80a4209 SS |
1123 | printk(KERN_WARNING "blkfront: vdevice 0x%x might conflict with " |
1124 | "emulated IDE disks,\n\t choose an xvd device name" | |
1125 | "from xvde on\n", info->vdevice); | |
9246b5f0 | 1126 | } |
e77c78c0 JB |
1127 | if (minor >> MINORBITS) { |
1128 | pr_warn("blkfront: %#x's minor (%#x) out of range; ignoring\n", | |
1129 | info->vdevice, minor); | |
1130 | return -ENODEV; | |
1131 | } | |
9246b5f0 CL |
1132 | |
1133 | if ((minor % nr_parts) == 0) | |
1134 | nr_minors = nr_parts; | |
9f27ee59 | 1135 | |
0e345826 JB |
1136 | err = xlbd_reserve_minors(minor, nr_minors); |
1137 | if (err) | |
1138 | goto out; | |
1139 | err = -ENODEV; | |
1140 | ||
9f27ee59 JF |
1141 | gd = alloc_disk(nr_minors); |
1142 | if (gd == NULL) | |
0e345826 | 1143 | goto release; |
9f27ee59 | 1144 | |
e77c78c0 JB |
1145 | strcpy(gd->disk_name, DEV_NAME); |
1146 | ptr = encode_disk_name(gd->disk_name + sizeof(DEV_NAME) - 1, offset); | |
1147 | BUG_ON(ptr >= gd->disk_name + DISK_NAME_LEN); | |
1148 | if (nr_minors > 1) | |
1149 | *ptr = 0; | |
1150 | else | |
1151 | snprintf(ptr, gd->disk_name + DISK_NAME_LEN - ptr, | |
1152 | "%d", minor & (nr_parts - 1)); | |
9f27ee59 JF |
1153 | |
1154 | gd->major = XENVBD_MAJOR; | |
1155 | gd->first_minor = minor; | |
1156 | gd->fops = &xlvbd_block_fops; | |
1157 | gd->private_data = info; | |
9f27ee59 JF |
1158 | set_capacity(gd, capacity); |
1159 | ||
172335ad | 1160 | if (xlvbd_init_blk_queue(gd, sector_size, physical_sector_size)) { |
9f27ee59 | 1161 | del_gendisk(gd); |
0e345826 | 1162 | goto release; |
9f27ee59 JF |
1163 | } |
1164 | ||
4913efe4 | 1165 | xlvbd_flush(info); |
9f27ee59 JF |
1166 | |
1167 | if (vdisk_info & VDISK_READONLY) | |
1168 | set_disk_ro(gd, 1); | |
1169 | ||
1170 | if (vdisk_info & VDISK_REMOVABLE) | |
1171 | gd->flags |= GENHD_FL_REMOVABLE; | |
1172 | ||
1173 | if (vdisk_info & VDISK_CDROM) | |
1174 | gd->flags |= GENHD_FL_CD; | |
1175 | ||
1176 | return 0; | |
1177 | ||
0e345826 JB |
1178 | release: |
1179 | xlbd_release_minors(minor, nr_minors); | |
9f27ee59 JF |
1180 | out: |
1181 | return err; | |
1182 | } | |
1183 | ||
a66b5aeb DS |
1184 | static void xlvbd_release_gendisk(struct blkfront_info *info) |
1185 | { | |
3df0e505 | 1186 | unsigned int minor, nr_minors, i; |
a66b5aeb DS |
1187 | |
1188 | if (info->rq == NULL) | |
1189 | return; | |
1190 | ||
a66b5aeb | 1191 | /* No more blkif_request(). */ |
907c3eb1 | 1192 | blk_mq_stop_hw_queues(info->rq); |
a66b5aeb | 1193 | |
3df0e505 BL |
1194 | for (i = 0; i < info->nr_rings; i++) { |
1195 | struct blkfront_ring_info *rinfo = &info->rinfo[i]; | |
a66b5aeb | 1196 | |
3df0e505 BL |
1197 | /* No more gnttab callback work. */ |
1198 | gnttab_cancel_free_callback(&rinfo->callback); | |
1199 | ||
1200 | /* Flush gnttab callback work. Must be done with no locks held. */ | |
1201 | flush_work(&rinfo->work); | |
1202 | } | |
a66b5aeb DS |
1203 | |
1204 | del_gendisk(info->gd); | |
1205 | ||
1206 | minor = info->gd->first_minor; | |
1207 | nr_minors = info->gd->minors; | |
1208 | xlbd_release_minors(minor, nr_minors); | |
1209 | ||
1210 | blk_cleanup_queue(info->rq); | |
907c3eb1 | 1211 | blk_mq_free_tag_set(&info->tag_set); |
a66b5aeb DS |
1212 | info->rq = NULL; |
1213 | ||
1214 | put_disk(info->gd); | |
1215 | info->gd = NULL; | |
1216 | } | |
1217 | ||
11659569 BL |
1218 | /* Already hold rinfo->ring_lock. */ |
1219 | static inline void kick_pending_request_queues_locked(struct blkfront_ring_info *rinfo) | |
9f27ee59 | 1220 | { |
81f35161 BL |
1221 | if (!RING_FULL(&rinfo->ring)) |
1222 | blk_mq_start_stopped_hw_queues(rinfo->dev_info->rq, true); | |
9f27ee59 JF |
1223 | } |
1224 | ||
11659569 BL |
1225 | static void kick_pending_request_queues(struct blkfront_ring_info *rinfo) |
1226 | { | |
1227 | unsigned long flags; | |
1228 | ||
1229 | spin_lock_irqsave(&rinfo->ring_lock, flags); | |
1230 | kick_pending_request_queues_locked(rinfo); | |
1231 | spin_unlock_irqrestore(&rinfo->ring_lock, flags); | |
1232 | } | |
1233 | ||
9f27ee59 JF |
1234 | static void blkif_restart_queue(struct work_struct *work) |
1235 | { | |
81f35161 | 1236 | struct blkfront_ring_info *rinfo = container_of(work, struct blkfront_ring_info, work); |
9f27ee59 | 1237 | |
81f35161 BL |
1238 | if (rinfo->dev_info->connected == BLKIF_STATE_CONNECTED) |
1239 | kick_pending_request_queues(rinfo); | |
9f27ee59 JF |
1240 | } |
1241 | ||
3df0e505 | 1242 | static void blkif_free_ring(struct blkfront_ring_info *rinfo) |
9f27ee59 | 1243 | { |
73716df7 | 1244 | struct grant *persistent_gnt, *n; |
3df0e505 | 1245 | struct blkfront_info *info = rinfo->dev_info; |
402b27f9 | 1246 | int i, j, segs; |
0a8704a5 | 1247 | |
bfe11d6d RPM |
1248 | /* |
1249 | * Remove indirect pages, this only happens when using indirect | |
1250 | * descriptors but not persistent grants | |
1251 | */ | |
81f35161 | 1252 | if (!list_empty(&rinfo->indirect_pages)) { |
bfe11d6d RPM |
1253 | struct page *indirect_page, *n; |
1254 | ||
1255 | BUG_ON(info->feature_persistent); | |
81f35161 | 1256 | list_for_each_entry_safe(indirect_page, n, &rinfo->indirect_pages, lru) { |
bfe11d6d RPM |
1257 | list_del(&indirect_page->lru); |
1258 | __free_page(indirect_page); | |
1259 | } | |
1260 | } | |
1261 | ||
73716df7 BL |
1262 | /* Remove all persistent grants. */ |
1263 | if (!list_empty(&rinfo->grants)) { | |
1264 | list_for_each_entry_safe(persistent_gnt, n, | |
1265 | &rinfo->grants, node) { | |
1266 | list_del(&persistent_gnt->node); | |
1267 | if (persistent_gnt->gref != GRANT_INVALID_REF) { | |
1268 | gnttab_end_foreign_access(persistent_gnt->gref, | |
1269 | 0, 0UL); | |
1270 | rinfo->persistent_gnts_c--; | |
1271 | } | |
1272 | if (info->feature_persistent) | |
1273 | __free_page(persistent_gnt->page); | |
1274 | kfree(persistent_gnt); | |
1275 | } | |
1276 | } | |
1277 | BUG_ON(rinfo->persistent_gnts_c != 0); | |
1278 | ||
86839c56 | 1279 | for (i = 0; i < BLK_RING_SIZE(info); i++) { |
402b27f9 RPM |
1280 | /* |
1281 | * Clear persistent grants present in requests already | |
1282 | * on the shared ring | |
1283 | */ | |
81f35161 | 1284 | if (!rinfo->shadow[i].request) |
402b27f9 RPM |
1285 | goto free_shadow; |
1286 | ||
81f35161 BL |
1287 | segs = rinfo->shadow[i].req.operation == BLKIF_OP_INDIRECT ? |
1288 | rinfo->shadow[i].req.u.indirect.nr_segments : | |
1289 | rinfo->shadow[i].req.u.rw.nr_segments; | |
402b27f9 | 1290 | for (j = 0; j < segs; j++) { |
81f35161 | 1291 | persistent_gnt = rinfo->shadow[i].grants_used[j]; |
402b27f9 | 1292 | gnttab_end_foreign_access(persistent_gnt->gref, 0, 0UL); |
bfe11d6d | 1293 | if (info->feature_persistent) |
a7a6df22 | 1294 | __free_page(persistent_gnt->page); |
402b27f9 RPM |
1295 | kfree(persistent_gnt); |
1296 | } | |
1297 | ||
81f35161 | 1298 | if (rinfo->shadow[i].req.operation != BLKIF_OP_INDIRECT) |
402b27f9 RPM |
1299 | /* |
1300 | * If this is not an indirect operation don't try to | |
1301 | * free indirect segments | |
1302 | */ | |
1303 | goto free_shadow; | |
1304 | ||
1305 | for (j = 0; j < INDIRECT_GREFS(segs); j++) { | |
81f35161 | 1306 | persistent_gnt = rinfo->shadow[i].indirect_grants[j]; |
402b27f9 | 1307 | gnttab_end_foreign_access(persistent_gnt->gref, 0, 0UL); |
a7a6df22 | 1308 | __free_page(persistent_gnt->page); |
402b27f9 RPM |
1309 | kfree(persistent_gnt); |
1310 | } | |
1311 | ||
1312 | free_shadow: | |
1d5c76e6 | 1313 | kvfree(rinfo->shadow[i].grants_used); |
81f35161 | 1314 | rinfo->shadow[i].grants_used = NULL; |
1d5c76e6 | 1315 | kvfree(rinfo->shadow[i].indirect_grants); |
81f35161 | 1316 | rinfo->shadow[i].indirect_grants = NULL; |
1d5c76e6 | 1317 | kvfree(rinfo->shadow[i].sg); |
81f35161 | 1318 | rinfo->shadow[i].sg = NULL; |
402b27f9 RPM |
1319 | } |
1320 | ||
9f27ee59 | 1321 | /* No more gnttab callback work. */ |
81f35161 | 1322 | gnttab_cancel_free_callback(&rinfo->callback); |
9f27ee59 JF |
1323 | |
1324 | /* Flush gnttab callback work. Must be done with no locks held. */ | |
81f35161 | 1325 | flush_work(&rinfo->work); |
9f27ee59 JF |
1326 | |
1327 | /* Free resources associated with old device channel. */ | |
86839c56 | 1328 | for (i = 0; i < info->nr_ring_pages; i++) { |
81f35161 BL |
1329 | if (rinfo->ring_ref[i] != GRANT_INVALID_REF) { |
1330 | gnttab_end_foreign_access(rinfo->ring_ref[i], 0, 0); | |
1331 | rinfo->ring_ref[i] = GRANT_INVALID_REF; | |
86839c56 | 1332 | } |
9f27ee59 | 1333 | } |
6c647b0e | 1334 | free_pages((unsigned long)rinfo->ring.sring, get_order(info->nr_ring_pages * XEN_PAGE_SIZE)); |
81f35161 | 1335 | rinfo->ring.sring = NULL; |
86839c56 | 1336 | |
81f35161 BL |
1337 | if (rinfo->irq) |
1338 | unbind_from_irqhandler(rinfo->irq, rinfo); | |
1339 | rinfo->evtchn = rinfo->irq = 0; | |
3df0e505 | 1340 | } |
9f27ee59 | 1341 | |
3df0e505 BL |
1342 | static void blkif_free(struct blkfront_info *info, int suspend) |
1343 | { | |
3df0e505 BL |
1344 | unsigned int i; |
1345 | ||
1346 | /* Prevent new requests being issued until we fix things up. */ | |
3df0e505 BL |
1347 | info->connected = suspend ? |
1348 | BLKIF_STATE_SUSPENDED : BLKIF_STATE_DISCONNECTED; | |
1349 | /* No more blkif_request(). */ | |
1350 | if (info->rq) | |
1351 | blk_mq_stop_hw_queues(info->rq); | |
1352 | ||
3df0e505 BL |
1353 | for (i = 0; i < info->nr_rings; i++) |
1354 | blkif_free_ring(&info->rinfo[i]); | |
1355 | ||
1d5c76e6 | 1356 | kvfree(info->rinfo); |
3df0e505 BL |
1357 | info->rinfo = NULL; |
1358 | info->nr_rings = 0; | |
9f27ee59 JF |
1359 | } |
1360 | ||
c004a6fe JG |
1361 | struct copy_from_grant { |
1362 | const struct blk_shadow *s; | |
1363 | unsigned int grant_idx; | |
1364 | unsigned int bvec_offset; | |
1365 | char *bvec_data; | |
1366 | }; | |
1367 | ||
1368 | static void blkif_copy_from_grant(unsigned long gfn, unsigned int offset, | |
1369 | unsigned int len, void *data) | |
1370 | { | |
1371 | struct copy_from_grant *info = data; | |
1372 | char *shared_data; | |
1373 | /* Convenient aliases */ | |
1374 | const struct blk_shadow *s = info->s; | |
1375 | ||
1376 | shared_data = kmap_atomic(s->grants_used[info->grant_idx]->page); | |
1377 | ||
1378 | memcpy(info->bvec_data + info->bvec_offset, | |
1379 | shared_data + offset, len); | |
1380 | ||
1381 | info->bvec_offset += len; | |
1382 | info->grant_idx++; | |
1383 | ||
1384 | kunmap_atomic(shared_data); | |
1385 | } | |
1386 | ||
6cc56833 JG |
1387 | static enum blk_req_status blkif_rsp_to_req_status(int rsp) |
1388 | { | |
1389 | switch (rsp) | |
1390 | { | |
1391 | case BLKIF_RSP_OKAY: | |
1392 | return REQ_DONE; | |
1393 | case BLKIF_RSP_EOPNOTSUPP: | |
1394 | return REQ_EOPNOTSUPP; | |
1395 | case BLKIF_RSP_ERROR: | |
1396 | /* Fallthrough. */ | |
1397 | default: | |
1398 | return REQ_ERROR; | |
1399 | } | |
1400 | } | |
1401 | ||
1402 | /* | |
1403 | * Get the final status of the block request based on two ring response | |
1404 | */ | |
1405 | static int blkif_get_final_status(enum blk_req_status s1, | |
1406 | enum blk_req_status s2) | |
1407 | { | |
1408 | BUG_ON(s1 == REQ_WAITING); | |
1409 | BUG_ON(s2 == REQ_WAITING); | |
1410 | ||
1411 | if (s1 == REQ_ERROR || s2 == REQ_ERROR) | |
1412 | return BLKIF_RSP_ERROR; | |
1413 | else if (s1 == REQ_EOPNOTSUPP || s2 == REQ_EOPNOTSUPP) | |
1414 | return BLKIF_RSP_EOPNOTSUPP; | |
1415 | return BLKIF_RSP_OKAY; | |
1416 | } | |
1417 | ||
1418 | static bool blkif_completion(unsigned long *id, | |
1419 | struct blkfront_ring_info *rinfo, | |
0a8704a5 | 1420 | struct blkif_response *bret) |
9f27ee59 | 1421 | { |
d62f6918 | 1422 | int i = 0; |
b7649158 | 1423 | struct scatterlist *sg; |
c004a6fe | 1424 | int num_sg, num_grant; |
81f35161 | 1425 | struct blkfront_info *info = rinfo->dev_info; |
6cc56833 | 1426 | struct blk_shadow *s = &rinfo->shadow[*id]; |
c004a6fe | 1427 | struct copy_from_grant data = { |
c004a6fe JG |
1428 | .grant_idx = 0, |
1429 | }; | |
402b27f9 | 1430 | |
c004a6fe | 1431 | num_grant = s->req.operation == BLKIF_OP_INDIRECT ? |
402b27f9 | 1432 | s->req.u.indirect.nr_segments : s->req.u.rw.nr_segments; |
6cc56833 JG |
1433 | |
1434 | /* The I/O request may be split in two. */ | |
1435 | if (unlikely(s->associated_id != NO_ASSOCIATED_ID)) { | |
1436 | struct blk_shadow *s2 = &rinfo->shadow[s->associated_id]; | |
1437 | ||
1438 | /* Keep the status of the current response in shadow. */ | |
1439 | s->status = blkif_rsp_to_req_status(bret->status); | |
1440 | ||
1441 | /* Wait the second response if not yet here. */ | |
1442 | if (s2->status == REQ_WAITING) | |
f87c30c9 | 1443 | return false; |
6cc56833 JG |
1444 | |
1445 | bret->status = blkif_get_final_status(s->status, | |
1446 | s2->status); | |
1447 | ||
1448 | /* | |
1449 | * All the grants is stored in the first shadow in order | |
1450 | * to make the completion code simpler. | |
1451 | */ | |
1452 | num_grant += s2->req.u.rw.nr_segments; | |
1453 | ||
1454 | /* | |
1455 | * The two responses may not come in order. Only the | |
1456 | * first request will store the scatter-gather list. | |
1457 | */ | |
1458 | if (s2->num_sg != 0) { | |
1459 | /* Update "id" with the ID of the first response. */ | |
1460 | *id = s->associated_id; | |
1461 | s = s2; | |
1462 | } | |
1463 | ||
1464 | /* | |
1465 | * We don't need anymore the second request, so recycling | |
1466 | * it now. | |
1467 | */ | |
1468 | if (add_id_to_freelist(rinfo, s->associated_id)) | |
1469 | WARN(1, "%s: can't recycle the second part (id = %ld) of the request\n", | |
1470 | info->gd->disk_name, s->associated_id); | |
1471 | } | |
1472 | ||
1473 | data.s = s; | |
c004a6fe | 1474 | num_sg = s->num_sg; |
0a8704a5 | 1475 | |
bfe11d6d | 1476 | if (bret->operation == BLKIF_OP_READ && info->feature_persistent) { |
c004a6fe | 1477 | for_each_sg(s->sg, sg, num_sg, i) { |
b7649158 | 1478 | BUG_ON(sg->offset + sg->length > PAGE_SIZE); |
c004a6fe JG |
1479 | |
1480 | data.bvec_offset = sg->offset; | |
1481 | data.bvec_data = kmap_atomic(sg_page(sg)); | |
1482 | ||
1483 | gnttab_foreach_grant_in_range(sg_page(sg), | |
1484 | sg->offset, | |
1485 | sg->length, | |
1486 | blkif_copy_from_grant, | |
1487 | &data); | |
1488 | ||
1489 | kunmap_atomic(data.bvec_data); | |
0a8704a5 RPM |
1490 | } |
1491 | } | |
1492 | /* Add the persistent grant into the list of free grants */ | |
c004a6fe | 1493 | for (i = 0; i < num_grant; i++) { |
fbe363c4 RPM |
1494 | if (gnttab_query_foreign_access(s->grants_used[i]->gref)) { |
1495 | /* | |
1496 | * If the grant is still mapped by the backend (the | |
1497 | * backend has chosen to make this grant persistent) | |
1498 | * we add it at the head of the list, so it will be | |
1499 | * reused first. | |
1500 | */ | |
bfe11d6d RPM |
1501 | if (!info->feature_persistent) |
1502 | pr_alert_ratelimited("backed has not unmapped grant: %u\n", | |
1503 | s->grants_used[i]->gref); | |
73716df7 BL |
1504 | list_add(&s->grants_used[i]->node, &rinfo->grants); |
1505 | rinfo->persistent_gnts_c++; | |
fbe363c4 RPM |
1506 | } else { |
1507 | /* | |
1508 | * If the grant is not mapped by the backend we end the | |
1509 | * foreign access and add it to the tail of the list, | |
1510 | * so it will not be picked again unless we run out of | |
1511 | * persistent grants. | |
1512 | */ | |
1513 | gnttab_end_foreign_access(s->grants_used[i]->gref, 0, 0UL); | |
1514 | s->grants_used[i]->gref = GRANT_INVALID_REF; | |
73716df7 | 1515 | list_add_tail(&s->grants_used[i]->node, &rinfo->grants); |
fbe363c4 | 1516 | } |
0a8704a5 | 1517 | } |
402b27f9 | 1518 | if (s->req.operation == BLKIF_OP_INDIRECT) { |
c004a6fe | 1519 | for (i = 0; i < INDIRECT_GREFS(num_grant); i++) { |
fbe363c4 | 1520 | if (gnttab_query_foreign_access(s->indirect_grants[i]->gref)) { |
bfe11d6d RPM |
1521 | if (!info->feature_persistent) |
1522 | pr_alert_ratelimited("backed has not unmapped grant: %u\n", | |
1523 | s->indirect_grants[i]->gref); | |
73716df7 BL |
1524 | list_add(&s->indirect_grants[i]->node, &rinfo->grants); |
1525 | rinfo->persistent_gnts_c++; | |
fbe363c4 | 1526 | } else { |
bfe11d6d RPM |
1527 | struct page *indirect_page; |
1528 | ||
fbe363c4 | 1529 | gnttab_end_foreign_access(s->indirect_grants[i]->gref, 0, 0UL); |
bfe11d6d RPM |
1530 | /* |
1531 | * Add the used indirect page back to the list of | |
1532 | * available pages for indirect grefs. | |
1533 | */ | |
7b076750 | 1534 | if (!info->feature_persistent) { |
a7a6df22 | 1535 | indirect_page = s->indirect_grants[i]->page; |
81f35161 | 1536 | list_add(&indirect_page->lru, &rinfo->indirect_pages); |
7b076750 | 1537 | } |
fbe363c4 | 1538 | s->indirect_grants[i]->gref = GRANT_INVALID_REF; |
73716df7 | 1539 | list_add_tail(&s->indirect_grants[i]->node, &rinfo->grants); |
fbe363c4 | 1540 | } |
402b27f9 RPM |
1541 | } |
1542 | } | |
6cc56833 | 1543 | |
f87c30c9 | 1544 | return true; |
9f27ee59 JF |
1545 | } |
1546 | ||
1547 | static irqreturn_t blkif_interrupt(int irq, void *dev_id) | |
1548 | { | |
1549 | struct request *req; | |
1550 | struct blkif_response *bret; | |
1551 | RING_IDX i, rp; | |
1552 | unsigned long flags; | |
81f35161 BL |
1553 | struct blkfront_ring_info *rinfo = (struct blkfront_ring_info *)dev_id; |
1554 | struct blkfront_info *info = rinfo->dev_info; | |
9f27ee59 | 1555 | |
11659569 | 1556 | if (unlikely(info->connected != BLKIF_STATE_CONNECTED)) |
9f27ee59 | 1557 | return IRQ_HANDLED; |
9f27ee59 | 1558 | |
11659569 | 1559 | spin_lock_irqsave(&rinfo->ring_lock, flags); |
9f27ee59 | 1560 | again: |
81f35161 | 1561 | rp = rinfo->ring.sring->rsp_prod; |
9f27ee59 JF |
1562 | rmb(); /* Ensure we see queued responses up to 'rp'. */ |
1563 | ||
81f35161 | 1564 | for (i = rinfo->ring.rsp_cons; i != rp; i++) { |
9f27ee59 | 1565 | unsigned long id; |
9f27ee59 | 1566 | |
81f35161 | 1567 | bret = RING_GET_RESPONSE(&rinfo->ring, i); |
9f27ee59 | 1568 | id = bret->id; |
6878c32e KRW |
1569 | /* |
1570 | * The backend has messed up and given us an id that we would | |
1571 | * never have given to it (we stamp it up to BLK_RING_SIZE - | |
1572 | * look in get_id_from_freelist. | |
1573 | */ | |
86839c56 | 1574 | if (id >= BLK_RING_SIZE(info)) { |
6878c32e KRW |
1575 | WARN(1, "%s: response to %s has incorrect id (%ld)\n", |
1576 | info->gd->disk_name, op_name(bret->operation), id); | |
1577 | /* We can't safely get the 'struct request' as | |
1578 | * the id is busted. */ | |
1579 | continue; | |
1580 | } | |
81f35161 | 1581 | req = rinfo->shadow[id].request; |
9f27ee59 | 1582 | |
6cc56833 JG |
1583 | if (bret->operation != BLKIF_OP_DISCARD) { |
1584 | /* | |
1585 | * We may need to wait for an extra response if the | |
1586 | * I/O request is split in 2 | |
1587 | */ | |
1588 | if (!blkif_completion(&id, rinfo, bret)) | |
1589 | continue; | |
1590 | } | |
9f27ee59 | 1591 | |
81f35161 | 1592 | if (add_id_to_freelist(rinfo, id)) { |
6878c32e KRW |
1593 | WARN(1, "%s: response to %s (id %ld) couldn't be recycled!\n", |
1594 | info->gd->disk_name, op_name(bret->operation), id); | |
1595 | continue; | |
1596 | } | |
9f27ee59 | 1597 | |
2a842aca CH |
1598 | if (bret->status == BLKIF_RSP_OKAY) |
1599 | blkif_req(req)->error = BLK_STS_OK; | |
1600 | else | |
1601 | blkif_req(req)->error = BLK_STS_IOERR; | |
1602 | ||
9f27ee59 | 1603 | switch (bret->operation) { |
ed30bf31 LD |
1604 | case BLKIF_OP_DISCARD: |
1605 | if (unlikely(bret->status == BLKIF_RSP_EOPNOTSUPP)) { | |
1606 | struct request_queue *rq = info->rq; | |
6878c32e KRW |
1607 | printk(KERN_WARNING "blkfront: %s: %s op failed\n", |
1608 | info->gd->disk_name, op_name(bret->operation)); | |
2a842aca | 1609 | blkif_req(req)->error = BLK_STS_NOTSUPP; |
ed30bf31 | 1610 | info->feature_discard = 0; |
5ea42986 | 1611 | info->feature_secdiscard = 0; |
8b904b5b BVA |
1612 | blk_queue_flag_clear(QUEUE_FLAG_DISCARD, rq); |
1613 | blk_queue_flag_clear(QUEUE_FLAG_SECERASE, rq); | |
ed30bf31 | 1614 | } |
ed30bf31 | 1615 | break; |
edf6ef59 | 1616 | case BLKIF_OP_FLUSH_DISKCACHE: |
9f27ee59 JF |
1617 | case BLKIF_OP_WRITE_BARRIER: |
1618 | if (unlikely(bret->status == BLKIF_RSP_EOPNOTSUPP)) { | |
6878c32e KRW |
1619 | printk(KERN_WARNING "blkfront: %s: %s op failed\n", |
1620 | info->gd->disk_name, op_name(bret->operation)); | |
31c4ccc3 | 1621 | blkif_req(req)->error = BLK_STS_NOTSUPP; |
dcb8baec JF |
1622 | } |
1623 | if (unlikely(bret->status == BLKIF_RSP_ERROR && | |
81f35161 | 1624 | rinfo->shadow[id].req.u.rw.nr_segments == 0)) { |
6878c32e KRW |
1625 | printk(KERN_WARNING "blkfront: %s: empty %s op failed\n", |
1626 | info->gd->disk_name, op_name(bret->operation)); | |
2a842aca | 1627 | blkif_req(req)->error = BLK_STS_NOTSUPP; |
dcb8baec | 1628 | } |
2609587c | 1629 | if (unlikely(blkif_req(req)->error)) { |
2a842aca CH |
1630 | if (blkif_req(req)->error == BLK_STS_NOTSUPP) |
1631 | blkif_req(req)->error = BLK_STS_OK; | |
a418090a | 1632 | info->feature_fua = 0; |
4913efe4 TH |
1633 | info->feature_flush = 0; |
1634 | xlvbd_flush(info); | |
9f27ee59 JF |
1635 | } |
1636 | /* fall through */ | |
1637 | case BLKIF_OP_READ: | |
1638 | case BLKIF_OP_WRITE: | |
1639 | if (unlikely(bret->status != BLKIF_RSP_OKAY)) | |
1640 | dev_dbg(&info->xbdev->dev, "Bad return from blkdev data " | |
1641 | "request: %x\n", bret->status); | |
1642 | ||
9f27ee59 JF |
1643 | break; |
1644 | default: | |
1645 | BUG(); | |
1646 | } | |
2609587c | 1647 | |
08e0029a | 1648 | blk_mq_complete_request(req); |
9f27ee59 JF |
1649 | } |
1650 | ||
81f35161 | 1651 | rinfo->ring.rsp_cons = i; |
9f27ee59 | 1652 | |
81f35161 | 1653 | if (i != rinfo->ring.req_prod_pvt) { |
9f27ee59 | 1654 | int more_to_do; |
81f35161 | 1655 | RING_FINAL_CHECK_FOR_RESPONSES(&rinfo->ring, more_to_do); |
9f27ee59 JF |
1656 | if (more_to_do) |
1657 | goto again; | |
1658 | } else | |
81f35161 | 1659 | rinfo->ring.sring->rsp_event = i + 1; |
9f27ee59 | 1660 | |
11659569 | 1661 | kick_pending_request_queues_locked(rinfo); |
9f27ee59 | 1662 | |
11659569 | 1663 | spin_unlock_irqrestore(&rinfo->ring_lock, flags); |
9f27ee59 JF |
1664 | |
1665 | return IRQ_HANDLED; | |
1666 | } | |
1667 | ||
1668 | ||
1669 | static int setup_blkring(struct xenbus_device *dev, | |
81f35161 | 1670 | struct blkfront_ring_info *rinfo) |
9f27ee59 JF |
1671 | { |
1672 | struct blkif_sring *sring; | |
86839c56 | 1673 | int err, i; |
81f35161 | 1674 | struct blkfront_info *info = rinfo->dev_info; |
c004a6fe | 1675 | unsigned long ring_size = info->nr_ring_pages * XEN_PAGE_SIZE; |
9cce2914 | 1676 | grant_ref_t gref[XENBUS_MAX_RING_GRANTS]; |
9f27ee59 | 1677 | |
86839c56 | 1678 | for (i = 0; i < info->nr_ring_pages; i++) |
81f35161 | 1679 | rinfo->ring_ref[i] = GRANT_INVALID_REF; |
9f27ee59 | 1680 | |
86839c56 BL |
1681 | sring = (struct blkif_sring *)__get_free_pages(GFP_NOIO | __GFP_HIGH, |
1682 | get_order(ring_size)); | |
9f27ee59 JF |
1683 | if (!sring) { |
1684 | xenbus_dev_fatal(dev, -ENOMEM, "allocating shared ring"); | |
1685 | return -ENOMEM; | |
1686 | } | |
1687 | SHARED_RING_INIT(sring); | |
81f35161 | 1688 | FRONT_RING_INIT(&rinfo->ring, sring, ring_size); |
9e973e64 | 1689 | |
81f35161 | 1690 | err = xenbus_grant_ring(dev, rinfo->ring.sring, info->nr_ring_pages, gref); |
9f27ee59 | 1691 | if (err < 0) { |
86839c56 | 1692 | free_pages((unsigned long)sring, get_order(ring_size)); |
81f35161 | 1693 | rinfo->ring.sring = NULL; |
9f27ee59 JF |
1694 | goto fail; |
1695 | } | |
86839c56 | 1696 | for (i = 0; i < info->nr_ring_pages; i++) |
81f35161 | 1697 | rinfo->ring_ref[i] = gref[i]; |
9f27ee59 | 1698 | |
81f35161 | 1699 | err = xenbus_alloc_evtchn(dev, &rinfo->evtchn); |
9f27ee59 JF |
1700 | if (err) |
1701 | goto fail; | |
1702 | ||
81f35161 BL |
1703 | err = bind_evtchn_to_irqhandler(rinfo->evtchn, blkif_interrupt, 0, |
1704 | "blkif", rinfo); | |
9f27ee59 JF |
1705 | if (err <= 0) { |
1706 | xenbus_dev_fatal(dev, err, | |
1707 | "bind_evtchn_to_irqhandler failed"); | |
1708 | goto fail; | |
1709 | } | |
81f35161 | 1710 | rinfo->irq = err; |
9f27ee59 JF |
1711 | |
1712 | return 0; | |
1713 | fail: | |
1714 | blkif_free(info, 0); | |
1715 | return err; | |
1716 | } | |
1717 | ||
28d949bc BL |
1718 | /* |
1719 | * Write out per-ring/queue nodes including ring-ref and event-channel, and each | |
1720 | * ring buffer may have multi pages depending on ->nr_ring_pages. | |
1721 | */ | |
1722 | static int write_per_ring_nodes(struct xenbus_transaction xbt, | |
1723 | struct blkfront_ring_info *rinfo, const char *dir) | |
1724 | { | |
1725 | int err; | |
1726 | unsigned int i; | |
1727 | const char *message = NULL; | |
1728 | struct blkfront_info *info = rinfo->dev_info; | |
1729 | ||
1730 | if (info->nr_ring_pages == 1) { | |
1731 | err = xenbus_printf(xbt, dir, "ring-ref", "%u", rinfo->ring_ref[0]); | |
1732 | if (err) { | |
1733 | message = "writing ring-ref"; | |
1734 | goto abort_transaction; | |
1735 | } | |
1736 | } else { | |
1737 | for (i = 0; i < info->nr_ring_pages; i++) { | |
1738 | char ring_ref_name[RINGREF_NAME_LEN]; | |
1739 | ||
1740 | snprintf(ring_ref_name, RINGREF_NAME_LEN, "ring-ref%u", i); | |
1741 | err = xenbus_printf(xbt, dir, ring_ref_name, | |
1742 | "%u", rinfo->ring_ref[i]); | |
1743 | if (err) { | |
1744 | message = "writing ring-ref"; | |
1745 | goto abort_transaction; | |
1746 | } | |
1747 | } | |
1748 | } | |
1749 | ||
1750 | err = xenbus_printf(xbt, dir, "event-channel", "%u", rinfo->evtchn); | |
1751 | if (err) { | |
1752 | message = "writing event-channel"; | |
1753 | goto abort_transaction; | |
1754 | } | |
1755 | ||
1756 | return 0; | |
1757 | ||
1758 | abort_transaction: | |
1759 | xenbus_transaction_end(xbt, 1); | |
1760 | if (message) | |
1761 | xenbus_dev_fatal(info->xbdev, err, "%s", message); | |
1762 | ||
1763 | return err; | |
1764 | } | |
9f27ee59 | 1765 | |
a46b5367 JG |
1766 | static void free_info(struct blkfront_info *info) |
1767 | { | |
1768 | list_del(&info->info_list); | |
1769 | kfree(info); | |
1770 | } | |
1771 | ||
9f27ee59 | 1772 | /* Common code used when first setting up, and when resuming. */ |
203fd61f | 1773 | static int talk_to_blkback(struct xenbus_device *dev, |
9f27ee59 JF |
1774 | struct blkfront_info *info) |
1775 | { | |
1776 | const char *message = NULL; | |
1777 | struct xenbus_transaction xbt; | |
28d949bc | 1778 | int err; |
f27dc1ac JG |
1779 | unsigned int i, max_page_order; |
1780 | unsigned int ring_page_order; | |
86839c56 | 1781 | |
7ed8ce1c BD |
1782 | if (!info) |
1783 | return -ENODEV; | |
1784 | ||
f27dc1ac JG |
1785 | max_page_order = xenbus_read_unsigned(info->xbdev->otherend, |
1786 | "max-ring-page-order", 0); | |
1787 | ring_page_order = min(xen_blkif_max_ring_order, max_page_order); | |
1788 | info->nr_ring_pages = 1 << ring_page_order; | |
9f27ee59 | 1789 | |
7ed8ce1c BD |
1790 | err = negotiate_mq(info); |
1791 | if (err) | |
1792 | goto destroy_blkring; | |
1793 | ||
3df0e505 | 1794 | for (i = 0; i < info->nr_rings; i++) { |
28d949bc BL |
1795 | struct blkfront_ring_info *rinfo = &info->rinfo[i]; |
1796 | ||
3df0e505 BL |
1797 | /* Create shared ring, alloc event channel. */ |
1798 | err = setup_blkring(dev, rinfo); | |
1799 | if (err) | |
1800 | goto destroy_blkring; | |
1801 | } | |
9f27ee59 JF |
1802 | |
1803 | again: | |
1804 | err = xenbus_transaction_start(&xbt); | |
1805 | if (err) { | |
1806 | xenbus_dev_fatal(dev, err, "starting transaction"); | |
1807 | goto destroy_blkring; | |
1808 | } | |
1809 | ||
28d949bc BL |
1810 | if (info->nr_ring_pages > 1) { |
1811 | err = xenbus_printf(xbt, dev->nodename, "ring-page-order", "%u", | |
1812 | ring_page_order); | |
1813 | if (err) { | |
1814 | message = "writing ring-page-order"; | |
1815 | goto abort_transaction; | |
1816 | } | |
1817 | } | |
3df0e505 | 1818 | |
28d949bc BL |
1819 | /* We already got the number of queues/rings in _probe */ |
1820 | if (info->nr_rings == 1) { | |
1821 | err = write_per_ring_nodes(xbt, &info->rinfo[0], dev->nodename); | |
1822 | if (err) | |
1823 | goto destroy_blkring; | |
1824 | } else { | |
1825 | char *path; | |
1826 | size_t pathsize; | |
3df0e505 | 1827 | |
28d949bc BL |
1828 | err = xenbus_printf(xbt, dev->nodename, "multi-queue-num-queues", "%u", |
1829 | info->nr_rings); | |
3df0e505 | 1830 | if (err) { |
28d949bc | 1831 | message = "writing multi-queue-num-queues"; |
3df0e505 BL |
1832 | goto abort_transaction; |
1833 | } | |
28d949bc BL |
1834 | |
1835 | pathsize = strlen(dev->nodename) + QUEUE_NAME_LEN; | |
1836 | path = kmalloc(pathsize, GFP_KERNEL); | |
1837 | if (!path) { | |
1838 | err = -ENOMEM; | |
1839 | message = "ENOMEM while writing ring references"; | |
1840 | goto abort_transaction; | |
1841 | } | |
1842 | ||
1843 | for (i = 0; i < info->nr_rings; i++) { | |
1844 | memset(path, 0, pathsize); | |
1845 | snprintf(path, pathsize, "%s/queue-%u", dev->nodename, i); | |
1846 | err = write_per_ring_nodes(xbt, &info->rinfo[i], path); | |
1847 | if (err) { | |
1848 | kfree(path); | |
1849 | goto destroy_blkring; | |
1850 | } | |
1851 | } | |
1852 | kfree(path); | |
9f27ee59 | 1853 | } |
3e334239 MA |
1854 | err = xenbus_printf(xbt, dev->nodename, "protocol", "%s", |
1855 | XEN_IO_PROTO_ABI_NATIVE); | |
1856 | if (err) { | |
1857 | message = "writing protocol"; | |
1858 | goto abort_transaction; | |
1859 | } | |
0a8704a5 | 1860 | err = xenbus_printf(xbt, dev->nodename, |
cb5bd4d1 | 1861 | "feature-persistent", "%u", 1); |
0a8704a5 RPM |
1862 | if (err) |
1863 | dev_warn(&dev->dev, | |
1864 | "writing persistent grants feature to xenbus"); | |
9f27ee59 JF |
1865 | |
1866 | err = xenbus_transaction_end(xbt, 0); | |
1867 | if (err) { | |
1868 | if (err == -EAGAIN) | |
1869 | goto again; | |
1870 | xenbus_dev_fatal(dev, err, "completing transaction"); | |
1871 | goto destroy_blkring; | |
1872 | } | |
1873 | ||
3df0e505 BL |
1874 | for (i = 0; i < info->nr_rings; i++) { |
1875 | unsigned int j; | |
28d949bc | 1876 | struct blkfront_ring_info *rinfo = &info->rinfo[i]; |
3df0e505 BL |
1877 | |
1878 | for (j = 0; j < BLK_RING_SIZE(info); j++) | |
1879 | rinfo->shadow[j].req.u.rw.id = j + 1; | |
1880 | rinfo->shadow[BLK_RING_SIZE(info)-1].req.u.rw.id = 0x0fffffff; | |
1881 | } | |
9f27ee59 JF |
1882 | xenbus_switch_state(dev, XenbusStateInitialised); |
1883 | ||
1884 | return 0; | |
1885 | ||
1886 | abort_transaction: | |
1887 | xenbus_transaction_end(xbt, 1); | |
1888 | if (message) | |
1889 | xenbus_dev_fatal(dev, err, "%s", message); | |
1890 | destroy_blkring: | |
1891 | blkif_free(info, 0); | |
3df0e505 | 1892 | |
a46b5367 JG |
1893 | mutex_lock(&blkfront_mutex); |
1894 | free_info(info); | |
1895 | mutex_unlock(&blkfront_mutex); | |
1896 | ||
c31ecf6c KRW |
1897 | dev_set_drvdata(&dev->dev, NULL); |
1898 | ||
9f27ee59 JF |
1899 | return err; |
1900 | } | |
1901 | ||
3db70a85 BL |
1902 | static int negotiate_mq(struct blkfront_info *info) |
1903 | { | |
f27dc1ac | 1904 | unsigned int backend_max_queues; |
3db70a85 BL |
1905 | unsigned int i; |
1906 | ||
1907 | BUG_ON(info->nr_rings); | |
1908 | ||
1909 | /* Check if backend supports multiple queues. */ | |
f27dc1ac JG |
1910 | backend_max_queues = xenbus_read_unsigned(info->xbdev->otherend, |
1911 | "multi-queue-max-queues", 1); | |
3db70a85 BL |
1912 | info->nr_rings = min(backend_max_queues, xen_blkif_max_queues); |
1913 | /* We need at least one ring. */ | |
1914 | if (!info->nr_rings) | |
1915 | info->nr_rings = 1; | |
1916 | ||
1d5c76e6 RPM |
1917 | info->rinfo = kvcalloc(info->nr_rings, |
1918 | sizeof(struct blkfront_ring_info), | |
1919 | GFP_KERNEL); | |
3db70a85 BL |
1920 | if (!info->rinfo) { |
1921 | xenbus_dev_fatal(info->xbdev, -ENOMEM, "allocating ring_info structure"); | |
6cc4a086 | 1922 | info->nr_rings = 0; |
3db70a85 BL |
1923 | return -ENOMEM; |
1924 | } | |
1925 | ||
1926 | for (i = 0; i < info->nr_rings; i++) { | |
1927 | struct blkfront_ring_info *rinfo; | |
1928 | ||
1929 | rinfo = &info->rinfo[i]; | |
1930 | INIT_LIST_HEAD(&rinfo->indirect_pages); | |
1931 | INIT_LIST_HEAD(&rinfo->grants); | |
1932 | rinfo->dev_info = info; | |
1933 | INIT_WORK(&rinfo->work, blkif_restart_queue); | |
1934 | spin_lock_init(&rinfo->ring_lock); | |
1935 | } | |
1936 | return 0; | |
1937 | } | |
9f27ee59 JF |
1938 | /** |
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 | |
9f27ee59 JF |
2004 | /* Front end dir is a number, which is used as the id. */ |
2005 | info->handle = simple_strtoul(strrchr(dev->nodename, '/')+1, NULL, 0); | |
a1b4b12b | 2006 | dev_set_drvdata(&dev->dev, info); |
9f27ee59 | 2007 | |
a46b5367 JG |
2008 | mutex_lock(&blkfront_mutex); |
2009 | list_add(&info->info_list, &info_list); | |
2010 | mutex_unlock(&blkfront_mutex); | |
2011 | ||
9f27ee59 JF |
2012 | return 0; |
2013 | } | |
2014 | ||
9f27ee59 JF |
2015 | static int blkif_recover(struct blkfront_info *info) |
2016 | { | |
4559fa55 | 2017 | unsigned int r_index; |
402b27f9 | 2018 | struct request *req, *n; |
402b27f9 | 2019 | int rc; |
4559fa55 N |
2020 | struct bio *bio; |
2021 | unsigned int segs; | |
402b27f9 | 2022 | |
3df0e505 | 2023 | blkfront_gather_backend_features(info); |
172335ad BL |
2024 | /* Reset limits changed by blk_mq_update_nr_hw_queues(). */ |
2025 | blkif_set_queue_limits(info); | |
402b27f9 | 2026 | segs = info->max_indirect_segments ? : BLKIF_MAX_SEGMENTS_PER_REQUEST; |
6c647b0e | 2027 | blk_queue_max_segments(info->rq, segs / GRANTS_PER_PSEG); |
9f27ee59 | 2028 | |
3df0e505 | 2029 | for (r_index = 0; r_index < info->nr_rings; r_index++) { |
7b427a59 | 2030 | struct blkfront_ring_info *rinfo = &info->rinfo[r_index]; |
3df0e505 BL |
2031 | |
2032 | rc = blkfront_setup_indirect(rinfo); | |
7b427a59 | 2033 | if (rc) |
3df0e505 | 2034 | return rc; |
3df0e505 | 2035 | } |
9f27ee59 JF |
2036 | xenbus_switch_state(info->xbdev, XenbusStateConnected); |
2037 | ||
9f27ee59 JF |
2038 | /* Now safe for us to use the shared ring */ |
2039 | info->connected = BLKIF_STATE_CONNECTED; | |
2040 | ||
3df0e505 BL |
2041 | for (r_index = 0; r_index < info->nr_rings; r_index++) { |
2042 | struct blkfront_ring_info *rinfo; | |
2043 | ||
2044 | rinfo = &info->rinfo[r_index]; | |
2045 | /* Kick any other new requests queued since we resumed */ | |
2046 | kick_pending_request_queues(rinfo); | |
2047 | } | |
9f27ee59 | 2048 | |
7b427a59 | 2049 | list_for_each_entry_safe(req, n, &info->requests, queuelist) { |
402b27f9 RPM |
2050 | /* Requeue pending requests (flush or discard) */ |
2051 | list_del_init(&req->queuelist); | |
2052 | BUG_ON(req->nr_phys_segments > segs); | |
2b053aca | 2053 | blk_mq_requeue_request(req, false); |
402b27f9 | 2054 | } |
52d7f1b5 | 2055 | blk_mq_start_stopped_hw_queues(info->rq, true); |
907c3eb1 | 2056 | blk_mq_kick_requeue_list(info->rq); |
9f27ee59 | 2057 | |
7b427a59 | 2058 | while ((bio = bio_list_pop(&info->bio_list)) != NULL) { |
402b27f9 | 2059 | /* Traverse the list of pending bios and re-queue them */ |
4e49ea4a | 2060 | submit_bio(bio); |
402b27f9 RPM |
2061 | } |
2062 | ||
9f27ee59 JF |
2063 | return 0; |
2064 | } | |
2065 | ||
2066 | /** | |
2067 | * We are reconnecting to the backend, due to a suspend/resume, or a backend | |
2068 | * driver restart. We tear down our blkif structure and recreate it, but | |
2069 | * leave the device-layer structures intact so that this is transparent to the | |
2070 | * rest of the kernel. | |
2071 | */ | |
2072 | static int blkfront_resume(struct xenbus_device *dev) | |
2073 | { | |
a1b4b12b | 2074 | struct blkfront_info *info = dev_get_drvdata(&dev->dev); |
3db70a85 | 2075 | int err = 0; |
7b427a59 | 2076 | unsigned int i, j; |
9f27ee59 JF |
2077 | |
2078 | dev_dbg(&dev->dev, "blkfront_resume: %s\n", dev->nodename); | |
2079 | ||
7b427a59 BL |
2080 | bio_list_init(&info->bio_list); |
2081 | INIT_LIST_HEAD(&info->requests); | |
2082 | for (i = 0; i < info->nr_rings; i++) { | |
2083 | struct blkfront_ring_info *rinfo = &info->rinfo[i]; | |
2084 | struct bio_list merge_bio; | |
2085 | struct blk_shadow *shadow = rinfo->shadow; | |
2086 | ||
2087 | for (j = 0; j < BLK_RING_SIZE(info); j++) { | |
2088 | /* Not in use? */ | |
2089 | if (!shadow[j].request) | |
2090 | continue; | |
2091 | ||
2092 | /* | |
2093 | * Get the bios in the request so we can re-queue them. | |
2094 | */ | |
b15bd8cb MK |
2095 | if (req_op(shadow[j].request) == REQ_OP_FLUSH || |
2096 | req_op(shadow[j].request) == REQ_OP_DISCARD || | |
2097 | req_op(shadow[j].request) == REQ_OP_SECURE_ERASE || | |
3fc9d690 | 2098 | shadow[j].request->cmd_flags & REQ_FUA) { |
7b427a59 BL |
2099 | /* |
2100 | * Flush operations don't contain bios, so | |
2101 | * we need to requeue the whole request | |
3fc9d690 LT |
2102 | * |
2103 | * XXX: but this doesn't make any sense for a | |
2104 | * write with the FUA flag set.. | |
7b427a59 BL |
2105 | */ |
2106 | list_add(&shadow[j].request->queuelist, &info->requests); | |
2107 | continue; | |
2108 | } | |
2109 | merge_bio.head = shadow[j].request->bio; | |
2110 | merge_bio.tail = shadow[j].request->biotail; | |
2111 | bio_list_merge(&info->bio_list, &merge_bio); | |
2112 | shadow[j].request->bio = NULL; | |
2a842aca | 2113 | blk_mq_end_request(shadow[j].request, BLK_STS_OK); |
7b427a59 BL |
2114 | } |
2115 | } | |
2116 | ||
9f27ee59 JF |
2117 | blkif_free(info, info->connected == BLKIF_STATE_CONNECTED); |
2118 | ||
203fd61f | 2119 | err = talk_to_blkback(dev, info); |
2a6f71ad BL |
2120 | if (!err) |
2121 | blk_mq_update_nr_hw_queues(&info->tag_set, info->nr_rings); | |
402b27f9 RPM |
2122 | |
2123 | /* | |
2124 | * We have to wait for the backend to switch to | |
2125 | * connected state, since we want to read which | |
2126 | * features it supports. | |
2127 | */ | |
9f27ee59 JF |
2128 | |
2129 | return err; | |
2130 | } | |
2131 | ||
6f03a7ff | 2132 | static void blkfront_closing(struct blkfront_info *info) |
b70f5fa0 DS |
2133 | { |
2134 | struct xenbus_device *xbdev = info->xbdev; | |
2135 | struct block_device *bdev = NULL; | |
2136 | ||
2137 | mutex_lock(&info->mutex); | |
2138 | ||
2139 | if (xbdev->state == XenbusStateClosing) { | |
2140 | mutex_unlock(&info->mutex); | |
2141 | return; | |
2142 | } | |
2143 | ||
2144 | if (info->gd) | |
2145 | bdev = bdget_disk(info->gd, 0); | |
2146 | ||
2147 | mutex_unlock(&info->mutex); | |
2148 | ||
2149 | if (!bdev) { | |
2150 | xenbus_frontend_closed(xbdev); | |
2151 | return; | |
2152 | } | |
2153 | ||
2154 | mutex_lock(&bdev->bd_mutex); | |
2155 | ||
7b32d104 | 2156 | if (bdev->bd_openers) { |
b70f5fa0 DS |
2157 | xenbus_dev_error(xbdev, -EBUSY, |
2158 | "Device in use; refusing to close"); | |
2159 | xenbus_switch_state(xbdev, XenbusStateClosing); | |
2160 | } else { | |
2161 | xlvbd_release_gendisk(info); | |
2162 | xenbus_frontend_closed(xbdev); | |
2163 | } | |
2164 | ||
2165 | mutex_unlock(&bdev->bd_mutex); | |
2166 | bdput(bdev); | |
2167 | } | |
9f27ee59 | 2168 | |
ed30bf31 LD |
2169 | static void blkfront_setup_discard(struct blkfront_info *info) |
2170 | { | |
2171 | int err; | |
ed30bf31 LD |
2172 | unsigned int discard_granularity; |
2173 | unsigned int discard_alignment; | |
2174 | ||
1c8cad6c OH |
2175 | info->feature_discard = 1; |
2176 | err = xenbus_gather(XBT_NIL, info->xbdev->otherend, | |
2177 | "discard-granularity", "%u", &discard_granularity, | |
2178 | "discard-alignment", "%u", &discard_alignment, | |
2179 | NULL); | |
2180 | if (!err) { | |
2181 | info->discard_granularity = discard_granularity; | |
2182 | info->discard_alignment = discard_alignment; | |
2183 | } | |
f27dc1ac JG |
2184 | info->feature_secdiscard = |
2185 | !!xenbus_read_unsigned(info->xbdev->otherend, "discard-secure", | |
2186 | 0); | |
ed30bf31 LD |
2187 | } |
2188 | ||
81f35161 | 2189 | static int blkfront_setup_indirect(struct blkfront_ring_info *rinfo) |
402b27f9 | 2190 | { |
c004a6fe | 2191 | unsigned int psegs, grants; |
402b27f9 | 2192 | int err, i; |
81f35161 | 2193 | struct blkfront_info *info = rinfo->dev_info; |
402b27f9 | 2194 | |
6cc56833 JG |
2195 | if (info->max_indirect_segments == 0) { |
2196 | if (!HAS_EXTRA_REQ) | |
2197 | grants = BLKIF_MAX_SEGMENTS_PER_REQUEST; | |
2198 | else { | |
2199 | /* | |
2200 | * When an extra req is required, the maximum | |
2201 | * grants supported is related to the size of the | |
2202 | * Linux block segment. | |
2203 | */ | |
2204 | grants = GRANTS_PER_PSEG; | |
2205 | } | |
2206 | } | |
d50babbe | 2207 | else |
c004a6fe | 2208 | grants = info->max_indirect_segments; |
3b4f1884 | 2209 | psegs = DIV_ROUND_UP(grants, GRANTS_PER_PSEG); |
402b27f9 | 2210 | |
81f35161 | 2211 | err = fill_grant_buffer(rinfo, |
c004a6fe | 2212 | (grants + INDIRECT_GREFS(grants)) * BLK_RING_SIZE(info)); |
402b27f9 RPM |
2213 | if (err) |
2214 | goto out_of_memory; | |
2215 | ||
bfe11d6d RPM |
2216 | if (!info->feature_persistent && info->max_indirect_segments) { |
2217 | /* | |
2218 | * We are using indirect descriptors but not persistent | |
2219 | * grants, we need to allocate a set of pages that can be | |
2220 | * used for mapping indirect grefs | |
2221 | */ | |
c004a6fe | 2222 | int num = INDIRECT_GREFS(grants) * BLK_RING_SIZE(info); |
bfe11d6d | 2223 | |
81f35161 | 2224 | BUG_ON(!list_empty(&rinfo->indirect_pages)); |
bfe11d6d RPM |
2225 | for (i = 0; i < num; i++) { |
2226 | struct page *indirect_page = alloc_page(GFP_NOIO); | |
2227 | if (!indirect_page) | |
2228 | goto out_of_memory; | |
81f35161 | 2229 | list_add(&indirect_page->lru, &rinfo->indirect_pages); |
bfe11d6d RPM |
2230 | } |
2231 | } | |
2232 | ||
86839c56 | 2233 | for (i = 0; i < BLK_RING_SIZE(info); i++) { |
6396bb22 | 2234 | rinfo->shadow[i].grants_used = |
1d5c76e6 RPM |
2235 | kvcalloc(grants, |
2236 | sizeof(rinfo->shadow[i].grants_used[0]), | |
2237 | GFP_NOIO); | |
2238 | rinfo->shadow[i].sg = kvcalloc(psegs, | |
2239 | sizeof(rinfo->shadow[i].sg[0]), | |
2240 | GFP_NOIO); | |
6396bb22 KC |
2241 | if (info->max_indirect_segments) |
2242 | rinfo->shadow[i].indirect_grants = | |
1d5c76e6 RPM |
2243 | kvcalloc(INDIRECT_GREFS(grants), |
2244 | sizeof(rinfo->shadow[i].indirect_grants[0]), | |
2245 | GFP_NOIO); | |
81f35161 BL |
2246 | if ((rinfo->shadow[i].grants_used == NULL) || |
2247 | (rinfo->shadow[i].sg == NULL) || | |
402b27f9 | 2248 | (info->max_indirect_segments && |
81f35161 | 2249 | (rinfo->shadow[i].indirect_grants == NULL))) |
402b27f9 | 2250 | goto out_of_memory; |
81f35161 | 2251 | sg_init_table(rinfo->shadow[i].sg, psegs); |
402b27f9 RPM |
2252 | } |
2253 | ||
2254 | ||
2255 | return 0; | |
2256 | ||
2257 | out_of_memory: | |
86839c56 | 2258 | for (i = 0; i < BLK_RING_SIZE(info); i++) { |
1d5c76e6 | 2259 | kvfree(rinfo->shadow[i].grants_used); |
81f35161 | 2260 | rinfo->shadow[i].grants_used = NULL; |
1d5c76e6 | 2261 | kvfree(rinfo->shadow[i].sg); |
81f35161 | 2262 | rinfo->shadow[i].sg = NULL; |
1d5c76e6 | 2263 | kvfree(rinfo->shadow[i].indirect_grants); |
81f35161 | 2264 | rinfo->shadow[i].indirect_grants = NULL; |
402b27f9 | 2265 | } |
81f35161 | 2266 | if (!list_empty(&rinfo->indirect_pages)) { |
bfe11d6d | 2267 | struct page *indirect_page, *n; |
81f35161 | 2268 | list_for_each_entry_safe(indirect_page, n, &rinfo->indirect_pages, lru) { |
bfe11d6d RPM |
2269 | list_del(&indirect_page->lru); |
2270 | __free_page(indirect_page); | |
2271 | } | |
2272 | } | |
402b27f9 RPM |
2273 | return -ENOMEM; |
2274 | } | |
2275 | ||
d50babbe BL |
2276 | /* |
2277 | * Gather all backend feature-* | |
2278 | */ | |
3df0e505 | 2279 | static void blkfront_gather_backend_features(struct blkfront_info *info) |
d50babbe | 2280 | { |
d50babbe BL |
2281 | unsigned int indirect_segments; |
2282 | ||
2283 | info->feature_flush = 0; | |
a418090a | 2284 | info->feature_fua = 0; |
d50babbe | 2285 | |
d50babbe BL |
2286 | /* |
2287 | * If there's no "feature-barrier" defined, then it means | |
2288 | * we're dealing with a very old backend which writes | |
2289 | * synchronously; nothing to do. | |
2290 | * | |
2291 | * If there are barriers, then we use flush. | |
2292 | */ | |
f27dc1ac | 2293 | if (xenbus_read_unsigned(info->xbdev->otherend, "feature-barrier", 0)) { |
a418090a MC |
2294 | info->feature_flush = 1; |
2295 | info->feature_fua = 1; | |
2296 | } | |
2297 | ||
d50babbe BL |
2298 | /* |
2299 | * And if there is "feature-flush-cache" use that above | |
2300 | * barriers. | |
2301 | */ | |
f27dc1ac JG |
2302 | if (xenbus_read_unsigned(info->xbdev->otherend, "feature-flush-cache", |
2303 | 0)) { | |
a418090a MC |
2304 | info->feature_flush = 1; |
2305 | info->feature_fua = 0; | |
2306 | } | |
d50babbe | 2307 | |
f27dc1ac | 2308 | if (xenbus_read_unsigned(info->xbdev->otherend, "feature-discard", 0)) |
d50babbe BL |
2309 | blkfront_setup_discard(info); |
2310 | ||
f27dc1ac | 2311 | info->feature_persistent = |
b32728ff JB |
2312 | !!xenbus_read_unsigned(info->xbdev->otherend, |
2313 | "feature-persistent", 0); | |
d50babbe | 2314 | |
f27dc1ac JG |
2315 | indirect_segments = xenbus_read_unsigned(info->xbdev->otherend, |
2316 | "feature-max-indirect-segments", 0); | |
3b4f1884 JB |
2317 | if (indirect_segments > xen_blkif_max_segments) |
2318 | indirect_segments = xen_blkif_max_segments; | |
2319 | if (indirect_segments <= BLKIF_MAX_SEGMENTS_PER_REQUEST) | |
2320 | indirect_segments = 0; | |
2321 | info->max_indirect_segments = indirect_segments; | |
a46b5367 JG |
2322 | |
2323 | if (info->feature_persistent) { | |
2324 | mutex_lock(&blkfront_mutex); | |
2325 | schedule_delayed_work(&blkfront_work, HZ * 10); | |
2326 | mutex_unlock(&blkfront_mutex); | |
2327 | } | |
d50babbe BL |
2328 | } |
2329 | ||
9f27ee59 JF |
2330 | /* |
2331 | * Invoked when the backend is finally 'ready' (and has told produced | |
2332 | * the details about the physical device - #sectors, size, etc). | |
2333 | */ | |
2334 | static void blkfront_connect(struct blkfront_info *info) | |
2335 | { | |
2336 | unsigned long long sectors; | |
2337 | unsigned long sector_size; | |
7c4d7d71 | 2338 | unsigned int physical_sector_size; |
9f27ee59 | 2339 | unsigned int binfo; |
89515d02 | 2340 | char *envp[] = { "RESIZE=1", NULL }; |
3df0e505 | 2341 | int err, i; |
9f27ee59 | 2342 | |
1fa73be6 S |
2343 | switch (info->connected) { |
2344 | case BLKIF_STATE_CONNECTED: | |
2345 | /* | |
2346 | * Potentially, the back-end may be signalling | |
2347 | * a capacity change; update the capacity. | |
2348 | */ | |
2349 | err = xenbus_scanf(XBT_NIL, info->xbdev->otherend, | |
2350 | "sectors", "%Lu", §ors); | |
2351 | if (XENBUS_EXIST_ERR(err)) | |
2352 | return; | |
2353 | printk(KERN_INFO "Setting capacity to %Lu\n", | |
2354 | sectors); | |
2355 | set_capacity(info->gd, sectors); | |
2def141e | 2356 | revalidate_disk(info->gd); |
89515d02 MO |
2357 | kobject_uevent_env(&disk_to_dev(info->gd)->kobj, |
2358 | KOBJ_CHANGE, envp); | |
1fa73be6 | 2359 | |
402b27f9 | 2360 | return; |
1fa73be6 | 2361 | case BLKIF_STATE_SUSPENDED: |
402b27f9 RPM |
2362 | /* |
2363 | * If we are recovering from suspension, we need to wait | |
2364 | * for the backend to announce it's features before | |
2365 | * reconnecting, at least we need to know if the backend | |
2366 | * supports indirect descriptors, and how many. | |
2367 | */ | |
2368 | blkif_recover(info); | |
9f27ee59 JF |
2369 | return; |
2370 | ||
b4dddb49 JF |
2371 | default: |
2372 | break; | |
1fa73be6 | 2373 | } |
9f27ee59 JF |
2374 | |
2375 | dev_dbg(&info->xbdev->dev, "%s:%s.\n", | |
2376 | __func__, info->xbdev->otherend); | |
2377 | ||
2378 | err = xenbus_gather(XBT_NIL, info->xbdev->otherend, | |
2379 | "sectors", "%llu", §ors, | |
2380 | "info", "%u", &binfo, | |
2381 | "sector-size", "%lu", §or_size, | |
2382 | NULL); | |
2383 | if (err) { | |
2384 | xenbus_dev_fatal(info->xbdev, err, | |
2385 | "reading backend fields at %s", | |
2386 | info->xbdev->otherend); | |
2387 | return; | |
2388 | } | |
2389 | ||
7c4d7d71 SB |
2390 | /* |
2391 | * physcial-sector-size is a newer field, so old backends may not | |
2392 | * provide this. Assume physical sector size to be the same as | |
2393 | * sector_size in that case. | |
2394 | */ | |
f27dc1ac JG |
2395 | physical_sector_size = xenbus_read_unsigned(info->xbdev->otherend, |
2396 | "physical-sector-size", | |
2397 | sector_size); | |
3df0e505 BL |
2398 | blkfront_gather_backend_features(info); |
2399 | for (i = 0; i < info->nr_rings; i++) { | |
2400 | err = blkfront_setup_indirect(&info->rinfo[i]); | |
2401 | if (err) { | |
2402 | xenbus_dev_fatal(info->xbdev, err, "setup_indirect at %s", | |
2403 | info->xbdev->otherend); | |
2404 | blkif_free(info, 0); | |
2405 | break; | |
2406 | } | |
402b27f9 RPM |
2407 | } |
2408 | ||
7c4d7d71 SB |
2409 | err = xlvbd_alloc_gendisk(sectors, info, binfo, sector_size, |
2410 | physical_sector_size); | |
9f27ee59 JF |
2411 | if (err) { |
2412 | xenbus_dev_fatal(info->xbdev, err, "xlvbd_add at %s", | |
2413 | info->xbdev->otherend); | |
4e876c2b | 2414 | goto fail; |
9f27ee59 JF |
2415 | } |
2416 | ||
2417 | xenbus_switch_state(info->xbdev, XenbusStateConnected); | |
2418 | ||
2419 | /* Kick pending requests. */ | |
9f27ee59 | 2420 | info->connected = BLKIF_STATE_CONNECTED; |
3df0e505 BL |
2421 | for (i = 0; i < info->nr_rings; i++) |
2422 | kick_pending_request_queues(&info->rinfo[i]); | |
9f27ee59 | 2423 | |
fef912bf | 2424 | device_add_disk(&info->xbdev->dev, info->gd, NULL); |
1d78d705 CL |
2425 | |
2426 | info->is_ready = 1; | |
4e876c2b BL |
2427 | return; |
2428 | ||
2429 | fail: | |
2430 | blkif_free(info, 0); | |
2431 | return; | |
9f27ee59 JF |
2432 | } |
2433 | ||
9f27ee59 JF |
2434 | /** |
2435 | * Callback received when the backend's state changes. | |
2436 | */ | |
203fd61f | 2437 | static void blkback_changed(struct xenbus_device *dev, |
9f27ee59 JF |
2438 | enum xenbus_state backend_state) |
2439 | { | |
a1b4b12b | 2440 | struct blkfront_info *info = dev_get_drvdata(&dev->dev); |
9f27ee59 | 2441 | |
203fd61f | 2442 | dev_dbg(&dev->dev, "blkfront:blkback_changed to state %d.\n", backend_state); |
9f27ee59 JF |
2443 | |
2444 | switch (backend_state) { | |
9f27ee59 | 2445 | case XenbusStateInitWait: |
a9b54bb9 BL |
2446 | if (dev->state != XenbusStateInitialising) |
2447 | break; | |
c31ecf6c | 2448 | if (talk_to_blkback(dev, info)) |
8ab0144a | 2449 | break; |
8ab0144a | 2450 | case XenbusStateInitialising: |
9f27ee59 | 2451 | case XenbusStateInitialised: |
b78c9512 NI |
2452 | case XenbusStateReconfiguring: |
2453 | case XenbusStateReconfigured: | |
9f27ee59 | 2454 | case XenbusStateUnknown: |
9f27ee59 JF |
2455 | break; |
2456 | ||
2457 | case XenbusStateConnected: | |
efd15352 BL |
2458 | /* |
2459 | * talk_to_blkback sets state to XenbusStateInitialised | |
2460 | * and blkfront_connect sets it to XenbusStateConnected | |
2461 | * (if connection went OK). | |
2462 | * | |
2463 | * If the backend (or toolstack) decides to poke at backend | |
2464 | * state (and re-trigger the watch by setting the state repeatedly | |
2465 | * to XenbusStateConnected (4)) we need to deal with this. | |
2466 | * This is allowed as this is used to communicate to the guest | |
2467 | * that the size of disk has changed! | |
2468 | */ | |
2469 | if ((dev->state != XenbusStateInitialised) && | |
2470 | (dev->state != XenbusStateConnected)) { | |
c31ecf6c KRW |
2471 | if (talk_to_blkback(dev, info)) |
2472 | break; | |
2473 | } | |
efd15352 | 2474 | |
9f27ee59 JF |
2475 | blkfront_connect(info); |
2476 | break; | |
2477 | ||
36613717 DV |
2478 | case XenbusStateClosed: |
2479 | if (dev->state == XenbusStateClosed) | |
2480 | break; | |
ccc22257 | 2481 | /* fall through */ |
9f27ee59 | 2482 | case XenbusStateClosing: |
a54c8f0f CA |
2483 | if (info) |
2484 | blkfront_closing(info); | |
9f27ee59 JF |
2485 | break; |
2486 | } | |
2487 | } | |
2488 | ||
fa1bd359 | 2489 | static int blkfront_remove(struct xenbus_device *xbdev) |
9f27ee59 | 2490 | { |
fa1bd359 DS |
2491 | struct blkfront_info *info = dev_get_drvdata(&xbdev->dev); |
2492 | struct block_device *bdev = NULL; | |
2493 | struct gendisk *disk; | |
9f27ee59 | 2494 | |
fa1bd359 | 2495 | dev_dbg(&xbdev->dev, "%s removed", xbdev->nodename); |
9f27ee59 | 2496 | |
f92898e7 VL |
2497 | if (!info) |
2498 | return 0; | |
2499 | ||
9f27ee59 JF |
2500 | blkif_free(info, 0); |
2501 | ||
fa1bd359 DS |
2502 | mutex_lock(&info->mutex); |
2503 | ||
2504 | disk = info->gd; | |
2505 | if (disk) | |
2506 | bdev = bdget_disk(disk, 0); | |
2507 | ||
2508 | info->xbdev = NULL; | |
2509 | mutex_unlock(&info->mutex); | |
2510 | ||
2511 | if (!bdev) { | |
a46b5367 JG |
2512 | mutex_lock(&blkfront_mutex); |
2513 | free_info(info); | |
2514 | mutex_unlock(&blkfront_mutex); | |
fa1bd359 DS |
2515 | return 0; |
2516 | } | |
2517 | ||
2518 | /* | |
2519 | * The xbdev was removed before we reached the Closed | |
2520 | * state. See if it's safe to remove the disk. If the bdev | |
2521 | * isn't closed yet, we let release take care of it. | |
2522 | */ | |
2523 | ||
2524 | mutex_lock(&bdev->bd_mutex); | |
2525 | info = disk->private_data; | |
2526 | ||
d54142c7 DS |
2527 | dev_warn(disk_to_dev(disk), |
2528 | "%s was hot-unplugged, %d stale handles\n", | |
2529 | xbdev->nodename, bdev->bd_openers); | |
2530 | ||
7b32d104 | 2531 | if (info && !bdev->bd_openers) { |
fa1bd359 DS |
2532 | xlvbd_release_gendisk(info); |
2533 | disk->private_data = NULL; | |
a46b5367 JG |
2534 | mutex_lock(&blkfront_mutex); |
2535 | free_info(info); | |
2536 | mutex_unlock(&blkfront_mutex); | |
fa1bd359 DS |
2537 | } |
2538 | ||
2539 | mutex_unlock(&bdev->bd_mutex); | |
2540 | bdput(bdev); | |
9f27ee59 JF |
2541 | |
2542 | return 0; | |
2543 | } | |
2544 | ||
1d78d705 CL |
2545 | static int blkfront_is_ready(struct xenbus_device *dev) |
2546 | { | |
a1b4b12b | 2547 | struct blkfront_info *info = dev_get_drvdata(&dev->dev); |
1d78d705 | 2548 | |
5d7ed20e | 2549 | return info->is_ready && info->xbdev; |
1d78d705 CL |
2550 | } |
2551 | ||
a63c848b | 2552 | static int blkif_open(struct block_device *bdev, fmode_t mode) |
9f27ee59 | 2553 | { |
13961743 DS |
2554 | struct gendisk *disk = bdev->bd_disk; |
2555 | struct blkfront_info *info; | |
2556 | int err = 0; | |
6e9624b8 | 2557 | |
2a48fc0a | 2558 | mutex_lock(&blkfront_mutex); |
6e9624b8 | 2559 | |
13961743 DS |
2560 | info = disk->private_data; |
2561 | if (!info) { | |
2562 | /* xbdev gone */ | |
2563 | err = -ERESTARTSYS; | |
2564 | goto out; | |
2565 | } | |
2566 | ||
2567 | mutex_lock(&info->mutex); | |
2568 | ||
2569 | if (!info->gd) | |
2570 | /* xbdev is closed */ | |
2571 | err = -ERESTARTSYS; | |
2572 | ||
2573 | mutex_unlock(&info->mutex); | |
2574 | ||
13961743 | 2575 | out: |
2a48fc0a | 2576 | mutex_unlock(&blkfront_mutex); |
13961743 | 2577 | return err; |
9f27ee59 JF |
2578 | } |
2579 | ||
db2a144b | 2580 | static void blkif_release(struct gendisk *disk, fmode_t mode) |
9f27ee59 | 2581 | { |
a63c848b | 2582 | struct blkfront_info *info = disk->private_data; |
7fd152f4 DS |
2583 | struct block_device *bdev; |
2584 | struct xenbus_device *xbdev; | |
2585 | ||
2a48fc0a | 2586 | mutex_lock(&blkfront_mutex); |
7fd152f4 DS |
2587 | |
2588 | bdev = bdget_disk(disk, 0); | |
7fd152f4 | 2589 | |
2f089cb8 FP |
2590 | if (!bdev) { |
2591 | WARN(1, "Block device %s yanked out from us!\n", disk->disk_name); | |
2592 | goto out_mutex; | |
2593 | } | |
acfca3c6 DS |
2594 | if (bdev->bd_openers) |
2595 | goto out; | |
2596 | ||
7fd152f4 DS |
2597 | /* |
2598 | * Check if we have been instructed to close. We will have | |
2599 | * deferred this request, because the bdev was still open. | |
2600 | */ | |
2601 | ||
2602 | mutex_lock(&info->mutex); | |
2603 | xbdev = info->xbdev; | |
2604 | ||
2605 | if (xbdev && xbdev->state == XenbusStateClosing) { | |
2606 | /* pending switch to state closed */ | |
d54142c7 | 2607 | dev_info(disk_to_dev(bdev->bd_disk), "releasing disk\n"); |
7fd152f4 DS |
2608 | xlvbd_release_gendisk(info); |
2609 | xenbus_frontend_closed(info->xbdev); | |
2610 | } | |
2611 | ||
2612 | mutex_unlock(&info->mutex); | |
2613 | ||
2614 | if (!xbdev) { | |
2615 | /* sudden device removal */ | |
d54142c7 | 2616 | dev_info(disk_to_dev(bdev->bd_disk), "releasing disk\n"); |
7fd152f4 DS |
2617 | xlvbd_release_gendisk(info); |
2618 | disk->private_data = NULL; | |
a46b5367 | 2619 | free_info(info); |
9f27ee59 | 2620 | } |
7fd152f4 | 2621 | |
a4cc14ec | 2622 | out: |
dad5cf65 | 2623 | bdput(bdev); |
2f089cb8 | 2624 | out_mutex: |
2a48fc0a | 2625 | mutex_unlock(&blkfront_mutex); |
9f27ee59 JF |
2626 | } |
2627 | ||
83d5cde4 | 2628 | static const struct block_device_operations xlvbd_block_fops = |
9f27ee59 JF |
2629 | { |
2630 | .owner = THIS_MODULE, | |
a63c848b AV |
2631 | .open = blkif_open, |
2632 | .release = blkif_release, | |
597592d9 | 2633 | .getgeo = blkif_getgeo, |
8a6cfeb6 | 2634 | .ioctl = blkif_ioctl, |
9f27ee59 JF |
2635 | }; |
2636 | ||
2637 | ||
ec9c42ec | 2638 | static const struct xenbus_device_id blkfront_ids[] = { |
9f27ee59 JF |
2639 | { "vbd" }, |
2640 | { "" } | |
2641 | }; | |
2642 | ||
95afae48 DV |
2643 | static struct xenbus_driver blkfront_driver = { |
2644 | .ids = blkfront_ids, | |
9f27ee59 JF |
2645 | .probe = blkfront_probe, |
2646 | .remove = blkfront_remove, | |
2647 | .resume = blkfront_resume, | |
203fd61f | 2648 | .otherend_changed = blkback_changed, |
1d78d705 | 2649 | .is_ready = blkfront_is_ready, |
95afae48 | 2650 | }; |
9f27ee59 | 2651 | |
a46b5367 JG |
2652 | static void purge_persistent_grants(struct blkfront_info *info) |
2653 | { | |
2654 | unsigned int i; | |
2655 | unsigned long flags; | |
2656 | ||
2657 | for (i = 0; i < info->nr_rings; i++) { | |
2658 | struct blkfront_ring_info *rinfo = &info->rinfo[i]; | |
2659 | struct grant *gnt_list_entry, *tmp; | |
2660 | ||
2661 | spin_lock_irqsave(&rinfo->ring_lock, flags); | |
2662 | ||
2663 | if (rinfo->persistent_gnts_c == 0) { | |
2664 | spin_unlock_irqrestore(&rinfo->ring_lock, flags); | |
2665 | continue; | |
2666 | } | |
2667 | ||
2668 | list_for_each_entry_safe(gnt_list_entry, tmp, &rinfo->grants, | |
2669 | node) { | |
2670 | if (gnt_list_entry->gref == GRANT_INVALID_REF || | |
2671 | gnttab_query_foreign_access(gnt_list_entry->gref)) | |
2672 | continue; | |
2673 | ||
2674 | list_del(&gnt_list_entry->node); | |
2675 | gnttab_end_foreign_access(gnt_list_entry->gref, 0, 0UL); | |
2676 | rinfo->persistent_gnts_c--; | |
6c767867 JG |
2677 | gnt_list_entry->gref = GRANT_INVALID_REF; |
2678 | list_add_tail(&gnt_list_entry->node, &rinfo->grants); | |
a46b5367 JG |
2679 | } |
2680 | ||
2681 | spin_unlock_irqrestore(&rinfo->ring_lock, flags); | |
2682 | } | |
2683 | } | |
2684 | ||
2685 | static void blkfront_delay_work(struct work_struct *work) | |
2686 | { | |
2687 | struct blkfront_info *info; | |
2688 | bool need_schedule_work = false; | |
2689 | ||
2690 | mutex_lock(&blkfront_mutex); | |
2691 | ||
2692 | list_for_each_entry(info, &info_list, info_list) { | |
2693 | if (info->feature_persistent) { | |
2694 | need_schedule_work = true; | |
2695 | mutex_lock(&info->mutex); | |
2696 | purge_persistent_grants(info); | |
2697 | mutex_unlock(&info->mutex); | |
2698 | } | |
2699 | } | |
2700 | ||
2701 | if (need_schedule_work) | |
2702 | schedule_delayed_work(&blkfront_work, HZ * 10); | |
2703 | ||
2704 | mutex_unlock(&blkfront_mutex); | |
2705 | } | |
2706 | ||
9f27ee59 JF |
2707 | static int __init xlblk_init(void) |
2708 | { | |
469738e6 | 2709 | int ret; |
28d949bc | 2710 | int nr_cpus = num_online_cpus(); |
469738e6 | 2711 | |
6e833587 | 2712 | if (!xen_domain()) |
9f27ee59 JF |
2713 | return -ENODEV; |
2714 | ||
4bcddbae JG |
2715 | if (!xen_has_pv_disk_devices()) |
2716 | return -ENODEV; | |
2717 | ||
2718 | if (register_blkdev(XENVBD_MAJOR, DEV_NAME)) { | |
2719 | pr_warn("xen_blk: can't get major %d with name %s\n", | |
2720 | XENVBD_MAJOR, DEV_NAME); | |
2721 | return -ENODEV; | |
2722 | } | |
2723 | ||
3b4f1884 JB |
2724 | if (xen_blkif_max_segments < BLKIF_MAX_SEGMENTS_PER_REQUEST) |
2725 | xen_blkif_max_segments = BLKIF_MAX_SEGMENTS_PER_REQUEST; | |
2726 | ||
9cce2914 | 2727 | if (xen_blkif_max_ring_order > XENBUS_MAX_RING_GRANT_ORDER) { |
86839c56 | 2728 | pr_info("Invalid max_ring_order (%d), will use default max: %d.\n", |
9cce2914 | 2729 | xen_blkif_max_ring_order, XENBUS_MAX_RING_GRANT_ORDER); |
45fc8264 | 2730 | xen_blkif_max_ring_order = XENBUS_MAX_RING_GRANT_ORDER; |
86839c56 BL |
2731 | } |
2732 | ||
28d949bc BL |
2733 | if (xen_blkif_max_queues > nr_cpus) { |
2734 | pr_info("Invalid max_queues (%d), will use default max: %d.\n", | |
2735 | xen_blkif_max_queues, nr_cpus); | |
2736 | xen_blkif_max_queues = nr_cpus; | |
2737 | } | |
2738 | ||
a46b5367 JG |
2739 | INIT_DELAYED_WORK(&blkfront_work, blkfront_delay_work); |
2740 | ||
73db144b | 2741 | ret = xenbus_register_frontend(&blkfront_driver); |
469738e6 LE |
2742 | if (ret) { |
2743 | unregister_blkdev(XENVBD_MAJOR, DEV_NAME); | |
2744 | return ret; | |
2745 | } | |
2746 | ||
2747 | return 0; | |
9f27ee59 JF |
2748 | } |
2749 | module_init(xlblk_init); | |
2750 | ||
2751 | ||
5a60d0cd | 2752 | static void __exit xlblk_exit(void) |
9f27ee59 | 2753 | { |
a46b5367 JG |
2754 | cancel_delayed_work_sync(&blkfront_work); |
2755 | ||
8605067f JB |
2756 | xenbus_unregister_driver(&blkfront_driver); |
2757 | unregister_blkdev(XENVBD_MAJOR, DEV_NAME); | |
2758 | kfree(minors); | |
9f27ee59 JF |
2759 | } |
2760 | module_exit(xlblk_exit); | |
2761 | ||
2762 | MODULE_DESCRIPTION("Xen virtual block device frontend"); | |
2763 | MODULE_LICENSE("GPL"); | |
2764 | MODULE_ALIAS_BLOCKDEV_MAJOR(XENVBD_MAJOR); | |
d2f0c52b | 2765 | MODULE_ALIAS("xen:vbd"); |
4f93f09b | 2766 | MODULE_ALIAS("xenblk"); |