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