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