]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - drivers/block/xen-blkfront.c
block/xen-blkfront: Split blkif_queue_request in 2
[mirror_ubuntu-artful-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
63enum blkif_state {
64 BLKIF_STATE_DISCONNECTED,
65 BLKIF_STATE_CONNECTED,
66 BLKIF_STATE_SUSPENDED,
67};
68
0a8704a5
RPM
69struct grant {
70 grant_ref_t gref;
71 unsigned long pfn;
155b7edb 72 struct list_head node;
0a8704a5
RPM
73};
74
9f27ee59
JF
75struct blk_shadow {
76 struct blkif_request req;
a945b980 77 struct request *request;
402b27f9
RPM
78 struct grant **grants_used;
79 struct grant **indirect_grants;
b7649158 80 struct scatterlist *sg;
402b27f9
RPM
81};
82
83struct split_bio {
84 struct bio *bio;
85 atomic_t pending;
9f27ee59
JF
86};
87
2a48fc0a 88static DEFINE_MUTEX(blkfront_mutex);
83d5cde4 89static const struct block_device_operations xlvbd_block_fops;
9f27ee59 90
402b27f9
RPM
91/*
92 * Maximum number of segments in indirect requests, the actual value used by
93 * the frontend driver is the minimum of this value and the value provided
94 * by the backend driver.
95 */
96
97static unsigned int xen_blkif_max_segments = 32;
2d5dc3ba
KRW
98module_param_named(max, xen_blkif_max_segments, int, S_IRUGO);
99MODULE_PARM_DESC(max, "Maximum amount of segments in indirect requests (default is 32)");
402b27f9 100
86839c56
BL
101/*
102 * Maximum order of pages to be used for the shared ring between front and
103 * backend, 4KB page granularity is used.
104 */
105static unsigned int xen_blkif_max_ring_order;
106module_param_named(max_ring_page_order, xen_blkif_max_ring_order, int, S_IRUGO);
107MODULE_PARM_DESC(max_ring_page_order, "Maximum order of pages to be used for the shared ring");
108
109#define BLK_RING_SIZE(info) __CONST_RING_SIZE(blkif, PAGE_SIZE * (info)->nr_ring_pages)
110#define BLK_MAX_RING_SIZE __CONST_RING_SIZE(blkif, PAGE_SIZE * XENBUS_MAX_RING_PAGES)
111/*
112 * ring-ref%i i=(-1UL) would take 11 characters + 'ring-ref' is 8, so 19
113 * characters are enough. Define to 20 to keep consist with backend.
114 */
115#define RINGREF_NAME_LEN (20)
9f27ee59
JF
116
117/*
118 * We have one of these per vbd, whether ide, scsi or 'other'. They
119 * hang in private_data off the gendisk structure. We may end up
120 * putting all kinds of interesting stuff here :-)
121 */
122struct blkfront_info
123{
3467811e 124 spinlock_t io_lock;
b70f5fa0 125 struct mutex mutex;
9f27ee59 126 struct xenbus_device *xbdev;
9f27ee59
JF
127 struct gendisk *gd;
128 int vdevice;
129 blkif_vdev_t handle;
130 enum blkif_state connected;
86839c56
BL
131 int ring_ref[XENBUS_MAX_RING_PAGES];
132 unsigned int nr_ring_pages;
9f27ee59
JF
133 struct blkif_front_ring ring;
134 unsigned int evtchn, irq;
135 struct request_queue *rq;
136 struct work_struct work;
137 struct gnttab_free_callback callback;
86839c56 138 struct blk_shadow shadow[BLK_MAX_RING_SIZE];
bfe11d6d
RPM
139 struct list_head grants;
140 struct list_head indirect_pages;
0a8704a5 141 unsigned int persistent_gnts_c;
9f27ee59 142 unsigned long shadow_free;
4913efe4 143 unsigned int feature_flush;
5ea42986
KRW
144 unsigned int feature_discard:1;
145 unsigned int feature_secdiscard:1;
ed30bf31
LD
146 unsigned int discard_granularity;
147 unsigned int discard_alignment;
0a8704a5 148 unsigned int feature_persistent:1;
402b27f9 149 unsigned int max_indirect_segments;
1d78d705 150 int is_ready;
907c3eb1 151 struct blk_mq_tag_set tag_set;
9f27ee59
JF
152};
153
0e345826
JB
154static unsigned int nr_minors;
155static unsigned long *minors;
156static DEFINE_SPINLOCK(minor_lock);
157
9f27ee59
JF
158#define GRANT_INVALID_REF 0
159
160#define PARTS_PER_DISK 16
9246b5f0 161#define PARTS_PER_EXT_DISK 256
9f27ee59
JF
162
163#define BLKIF_MAJOR(dev) ((dev)>>8)
164#define BLKIF_MINOR(dev) ((dev) & 0xff)
165
9246b5f0
CL
166#define EXT_SHIFT 28
167#define EXTENDED (1<<EXT_SHIFT)
168#define VDEV_IS_EXTENDED(dev) ((dev)&(EXTENDED))
169#define BLKIF_MINOR_EXT(dev) ((dev)&(~EXTENDED))
c80a4209
SS
170#define EMULATED_HD_DISK_MINOR_OFFSET (0)
171#define EMULATED_HD_DISK_NAME_OFFSET (EMULATED_HD_DISK_MINOR_OFFSET / 256)
196cfe2a
SB
172#define EMULATED_SD_DISK_MINOR_OFFSET (0)
173#define EMULATED_SD_DISK_NAME_OFFSET (EMULATED_SD_DISK_MINOR_OFFSET / 256)
9f27ee59 174
9246b5f0 175#define DEV_NAME "xvd" /* name in /dev */
9f27ee59 176
402b27f9 177#define SEGS_PER_INDIRECT_FRAME \
80bfa2f6 178 (PAGE_SIZE/sizeof(struct blkif_request_segment))
402b27f9
RPM
179#define INDIRECT_GREFS(_segs) \
180 ((_segs + SEGS_PER_INDIRECT_FRAME - 1)/SEGS_PER_INDIRECT_FRAME)
181
182static int blkfront_setup_indirect(struct blkfront_info *info);
d50babbe 183static int blkfront_gather_backend_features(struct blkfront_info *info);
402b27f9 184
9f27ee59
JF
185static int get_id_from_freelist(struct blkfront_info *info)
186{
187 unsigned long free = info->shadow_free;
86839c56 188 BUG_ON(free >= BLK_RING_SIZE(info));
97e36834
KRW
189 info->shadow_free = info->shadow[free].req.u.rw.id;
190 info->shadow[free].req.u.rw.id = 0x0fffffee; /* debug */
9f27ee59
JF
191 return free;
192}
193
6878c32e 194static int add_id_to_freelist(struct blkfront_info *info,
9f27ee59
JF
195 unsigned long id)
196{
6878c32e
KRW
197 if (info->shadow[id].req.u.rw.id != id)
198 return -EINVAL;
199 if (info->shadow[id].request == NULL)
200 return -EINVAL;
97e36834 201 info->shadow[id].req.u.rw.id = info->shadow_free;
a945b980 202 info->shadow[id].request = NULL;
9f27ee59 203 info->shadow_free = id;
6878c32e 204 return 0;
9f27ee59
JF
205}
206
9c1e050c
RPM
207static int fill_grant_buffer(struct blkfront_info *info, int num)
208{
209 struct page *granted_page;
210 struct grant *gnt_list_entry, *n;
211 int i = 0;
212
213 while(i < num) {
214 gnt_list_entry = kzalloc(sizeof(struct grant), GFP_NOIO);
215 if (!gnt_list_entry)
216 goto out_of_memory;
217
bfe11d6d
RPM
218 if (info->feature_persistent) {
219 granted_page = alloc_page(GFP_NOIO);
220 if (!granted_page) {
221 kfree(gnt_list_entry);
222 goto out_of_memory;
223 }
224 gnt_list_entry->pfn = page_to_pfn(granted_page);
9c1e050c
RPM
225 }
226
9c1e050c 227 gnt_list_entry->gref = GRANT_INVALID_REF;
bfe11d6d 228 list_add(&gnt_list_entry->node, &info->grants);
9c1e050c
RPM
229 i++;
230 }
231
232 return 0;
233
234out_of_memory:
235 list_for_each_entry_safe(gnt_list_entry, n,
bfe11d6d 236 &info->grants, node) {
9c1e050c 237 list_del(&gnt_list_entry->node);
bfe11d6d
RPM
238 if (info->feature_persistent)
239 __free_page(pfn_to_page(gnt_list_entry->pfn));
9c1e050c
RPM
240 kfree(gnt_list_entry);
241 i--;
242 }
243 BUG_ON(i != 0);
244 return -ENOMEM;
245}
246
247static struct grant *get_grant(grant_ref_t *gref_head,
bfe11d6d 248 unsigned long pfn,
9c1e050c
RPM
249 struct blkfront_info *info)
250{
251 struct grant *gnt_list_entry;
0df4f266 252 unsigned long buffer_gfn;
9c1e050c 253
bfe11d6d
RPM
254 BUG_ON(list_empty(&info->grants));
255 gnt_list_entry = list_first_entry(&info->grants, struct grant,
9c1e050c
RPM
256 node);
257 list_del(&gnt_list_entry->node);
258
259 if (gnt_list_entry->gref != GRANT_INVALID_REF) {
260 info->persistent_gnts_c--;
261 return gnt_list_entry;
262 }
263
264 /* Assign a gref to this page */
265 gnt_list_entry->gref = gnttab_claim_grant_reference(gref_head);
266 BUG_ON(gnt_list_entry->gref == -ENOSPC);
bfe11d6d
RPM
267 if (!info->feature_persistent) {
268 BUG_ON(!pfn);
269 gnt_list_entry->pfn = pfn;
270 }
0df4f266 271 buffer_gfn = pfn_to_gfn(gnt_list_entry->pfn);
9c1e050c
RPM
272 gnttab_grant_foreign_access_ref(gnt_list_entry->gref,
273 info->xbdev->otherend_id,
0df4f266 274 buffer_gfn, 0);
9c1e050c
RPM
275 return gnt_list_entry;
276}
277
6878c32e
KRW
278static const char *op_name(int op)
279{
280 static const char *const names[] = {
281 [BLKIF_OP_READ] = "read",
282 [BLKIF_OP_WRITE] = "write",
283 [BLKIF_OP_WRITE_BARRIER] = "barrier",
284 [BLKIF_OP_FLUSH_DISKCACHE] = "flush",
285 [BLKIF_OP_DISCARD] = "discard" };
286
287 if (op < 0 || op >= ARRAY_SIZE(names))
288 return "unknown";
289
290 if (!names[op])
291 return "reserved";
292
293 return names[op];
294}
0e345826
JB
295static int xlbd_reserve_minors(unsigned int minor, unsigned int nr)
296{
297 unsigned int end = minor + nr;
298 int rc;
299
300 if (end > nr_minors) {
301 unsigned long *bitmap, *old;
302
f094148a 303 bitmap = kcalloc(BITS_TO_LONGS(end), sizeof(*bitmap),
0e345826
JB
304 GFP_KERNEL);
305 if (bitmap == NULL)
306 return -ENOMEM;
307
308 spin_lock(&minor_lock);
309 if (end > nr_minors) {
310 old = minors;
311 memcpy(bitmap, minors,
312 BITS_TO_LONGS(nr_minors) * sizeof(*bitmap));
313 minors = bitmap;
314 nr_minors = BITS_TO_LONGS(end) * BITS_PER_LONG;
315 } else
316 old = bitmap;
317 spin_unlock(&minor_lock);
318 kfree(old);
319 }
320
321 spin_lock(&minor_lock);
322 if (find_next_bit(minors, end, minor) >= end) {
34ae2e47 323 bitmap_set(minors, minor, nr);
0e345826
JB
324 rc = 0;
325 } else
326 rc = -EBUSY;
327 spin_unlock(&minor_lock);
328
329 return rc;
330}
331
332static void xlbd_release_minors(unsigned int minor, unsigned int nr)
333{
334 unsigned int end = minor + nr;
335
336 BUG_ON(end > nr_minors);
337 spin_lock(&minor_lock);
34ae2e47 338 bitmap_clear(minors, minor, nr);
0e345826
JB
339 spin_unlock(&minor_lock);
340}
341
9f27ee59
JF
342static void blkif_restart_queue_callback(void *arg)
343{
344 struct blkfront_info *info = (struct blkfront_info *)arg;
345 schedule_work(&info->work);
346}
347
afe42d7d 348static int blkif_getgeo(struct block_device *bd, struct hd_geometry *hg)
597592d9
IC
349{
350 /* We don't have real geometry info, but let's at least return
351 values consistent with the size of the device */
352 sector_t nsect = get_capacity(bd->bd_disk);
353 sector_t cylinders = nsect;
354
355 hg->heads = 0xff;
356 hg->sectors = 0x3f;
357 sector_div(cylinders, hg->heads * hg->sectors);
358 hg->cylinders = cylinders;
359 if ((sector_t)(hg->cylinders + 1) * hg->heads * hg->sectors < nsect)
360 hg->cylinders = 0xffff;
361 return 0;
362}
363
a63c848b 364static int blkif_ioctl(struct block_device *bdev, fmode_t mode,
62aa0054 365 unsigned command, unsigned long argument)
440a01a7 366{
a63c848b 367 struct blkfront_info *info = bdev->bd_disk->private_data;
440a01a7
CL
368 int i;
369
370 dev_dbg(&info->xbdev->dev, "command: 0x%x, argument: 0x%lx\n",
371 command, (long)argument);
372
373 switch (command) {
374 case CDROMMULTISESSION:
375 dev_dbg(&info->xbdev->dev, "FIXME: support multisession CDs later\n");
376 for (i = 0; i < sizeof(struct cdrom_multisession); i++)
377 if (put_user(0, (char __user *)(argument + i)))
378 return -EFAULT;
379 return 0;
380
381 case CDROM_GET_CAPABILITY: {
382 struct gendisk *gd = info->gd;
383 if (gd->flags & GENHD_FL_CD)
384 return 0;
385 return -EINVAL;
386 }
387
388 default:
389 /*printk(KERN_ALERT "ioctl %08x not supported by Xen blkdev\n",
390 command);*/
391 return -EINVAL; /* same return as native Linux */
392 }
393
394 return 0;
395}
396
33204663
JG
397static int blkif_queue_discard_req(struct request *req)
398{
399 struct blkfront_info *info = req->rq_disk->private_data;
400 struct blkif_request *ring_req;
401 unsigned long id;
402
403 /* Fill out a communications ring structure. */
404 ring_req = RING_GET_REQUEST(&info->ring, info->ring.req_prod_pvt);
405 id = get_id_from_freelist(info);
406 info->shadow[id].request = req;
407
408 ring_req->operation = BLKIF_OP_DISCARD;
409 ring_req->u.discard.nr_sectors = blk_rq_sectors(req);
410 ring_req->u.discard.id = id;
411 ring_req->u.discard.sector_number = (blkif_sector_t)blk_rq_pos(req);
412 if ((req->cmd_flags & REQ_SECURE) && info->feature_secdiscard)
413 ring_req->u.discard.flag = BLKIF_DISCARD_SECURE;
414 else
415 ring_req->u.discard.flag = 0;
416
417 info->ring.req_prod_pvt++;
418
419 /* Keep a private copy so we can reissue requests when recovering. */
420 info->shadow[id].req = *ring_req;
421
422 return 0;
423}
424
425static int blkif_queue_rw_req(struct request *req)
9f27ee59
JF
426{
427 struct blkfront_info *info = req->rq_disk->private_data;
9f27ee59 428 struct blkif_request *ring_req;
9f27ee59
JF
429 unsigned long id;
430 unsigned int fsect, lsect;
402b27f9 431 int i, ref, n;
80bfa2f6 432 struct blkif_request_segment *segments = NULL;
0a8704a5
RPM
433
434 /*
435 * Used to store if we are able to queue the request by just using
436 * existing persistent grants, or if we have to get new grants,
437 * as there are not sufficiently many free.
438 */
439 bool new_persistent_gnts;
9f27ee59 440 grant_ref_t gref_head;
0a8704a5 441 struct grant *gnt_list_entry = NULL;
9e973e64 442 struct scatterlist *sg;
402b27f9 443 int nseg, max_grefs;
9f27ee59 444
c47206e2
RPM
445 max_grefs = req->nr_phys_segments;
446 if (max_grefs > BLKIF_MAX_SEGMENTS_PER_REQUEST)
447 /*
448 * If we are using indirect segments we need to account
449 * for the indirect grefs used in the request.
450 */
451 max_grefs += INDIRECT_GREFS(req->nr_phys_segments);
402b27f9
RPM
452
453 /* Check if we have enough grants to allocate a requests */
454 if (info->persistent_gnts_c < max_grefs) {
0a8704a5
RPM
455 new_persistent_gnts = 1;
456 if (gnttab_alloc_grant_references(
402b27f9 457 max_grefs - info->persistent_gnts_c,
0a8704a5
RPM
458 &gref_head) < 0) {
459 gnttab_request_free_callback(
460 &info->callback,
461 blkif_restart_queue_callback,
462 info,
402b27f9 463 max_grefs);
0a8704a5
RPM
464 return 1;
465 }
466 } else
467 new_persistent_gnts = 0;
9f27ee59
JF
468
469 /* Fill out a communications ring structure. */
470 ring_req = RING_GET_REQUEST(&info->ring, info->ring.req_prod_pvt);
471 id = get_id_from_freelist(info);
a945b980 472 info->shadow[id].request = req;
9f27ee59 473
33204663
JG
474 BUG_ON(info->max_indirect_segments == 0 &&
475 req->nr_phys_segments > BLKIF_MAX_SEGMENTS_PER_REQUEST);
476 BUG_ON(info->max_indirect_segments &&
477 req->nr_phys_segments > info->max_indirect_segments);
478 nseg = blk_rq_map_sg(req->q, req, info->shadow[id].sg);
479 ring_req->u.rw.id = id;
480 if (nseg > BLKIF_MAX_SEGMENTS_PER_REQUEST) {
481 /*
482 * The indirect operation can only be a BLKIF_OP_READ or
483 * BLKIF_OP_WRITE
484 */
485 BUG_ON(req->cmd_flags & (REQ_FLUSH | REQ_FUA));
486 ring_req->operation = BLKIF_OP_INDIRECT;
487 ring_req->u.indirect.indirect_op = rq_data_dir(req) ?
488 BLKIF_OP_WRITE : BLKIF_OP_READ;
489 ring_req->u.indirect.sector_number = (blkif_sector_t)blk_rq_pos(req);
490 ring_req->u.indirect.handle = info->handle;
491 ring_req->u.indirect.nr_segments = nseg;
ed30bf31 492 } else {
33204663
JG
493 ring_req->u.rw.sector_number = (blkif_sector_t)blk_rq_pos(req);
494 ring_req->u.rw.handle = info->handle;
495 ring_req->operation = rq_data_dir(req) ?
496 BLKIF_OP_WRITE : BLKIF_OP_READ;
497 if (req->cmd_flags & (REQ_FLUSH | REQ_FUA)) {
402b27f9 498 /*
33204663
JG
499 * Ideally we can do an unordered flush-to-disk.
500 * In case the backend onlysupports barriers, use that.
501 * A barrier request a superset of FUA, so we can
502 * implement it the same way. (It's also a FLUSH+FUA,
503 * since it is guaranteed ordered WRT previous writes.)
402b27f9 504 */
33204663
JG
505 switch (info->feature_flush &
506 ((REQ_FLUSH|REQ_FUA))) {
507 case REQ_FLUSH|REQ_FUA:
508 ring_req->operation =
509 BLKIF_OP_WRITE_BARRIER;
510 break;
511 case REQ_FLUSH:
512 ring_req->operation =
513 BLKIF_OP_FLUSH_DISKCACHE;
514 break;
515 default:
516 ring_req->operation = 0;
517 }
518 }
519 ring_req->u.rw.nr_segments = nseg;
520 }
521 for_each_sg(info->shadow[id].sg, sg, nseg, i) {
522 fsect = sg->offset >> 9;
523 lsect = fsect + (sg->length >> 9) - 1;
524
525 if ((ring_req->operation == BLKIF_OP_INDIRECT) &&
526 (i % SEGS_PER_INDIRECT_FRAME == 0)) {
527 unsigned long uninitialized_var(pfn);
528
529 if (segments)
530 kunmap_atomic(segments);
531
532 n = i / SEGS_PER_INDIRECT_FRAME;
533 if (!info->feature_persistent) {
534 struct page *indirect_page;
535
402b27f9 536 /*
33204663
JG
537 * Fetch a pre-allocated page to use for
538 * indirect grefs
402b27f9 539 */
33204663
JG
540 BUG_ON(list_empty(&info->indirect_pages));
541 indirect_page = list_first_entry(&info->indirect_pages,
542 struct page, lru);
543 list_del(&indirect_page->lru);
544 pfn = page_to_pfn(indirect_page);
402b27f9 545 }
33204663
JG
546 gnt_list_entry = get_grant(&gref_head, pfn, info);
547 info->shadow[id].indirect_grants[n] = gnt_list_entry;
548 segments = kmap_atomic(pfn_to_page(gnt_list_entry->pfn));
549 ring_req->u.indirect.indirect_grefs[n] = gnt_list_entry->gref;
402b27f9 550 }
402b27f9 551
33204663
JG
552 gnt_list_entry = get_grant(&gref_head, page_to_pfn(sg_page(sg)), info);
553 ref = gnt_list_entry->gref;
0a8704a5 554
33204663 555 info->shadow[id].grants_used[i] = gnt_list_entry;
0a8704a5 556
33204663
JG
557 if (rq_data_dir(req) && info->feature_persistent) {
558 char *bvec_data;
559 void *shared_data;
0a8704a5 560
33204663 561 BUG_ON(sg->offset + sg->length > PAGE_SIZE);
0a8704a5 562
33204663
JG
563 shared_data = kmap_atomic(pfn_to_page(gnt_list_entry->pfn));
564 bvec_data = kmap_atomic(sg_page(sg));
0a8704a5 565
33204663
JG
566 /*
567 * this does not wipe data stored outside the
568 * range sg->offset..sg->offset+sg->length.
569 * Therefore, blkback *could* see data from
570 * previous requests. This is OK as long as
571 * persistent grants are shared with just one
572 * domain. It may need refactoring if this
573 * changes
574 */
575 memcpy(shared_data + sg->offset,
576 bvec_data + sg->offset,
577 sg->length);
0a8704a5 578
33204663
JG
579 kunmap_atomic(bvec_data);
580 kunmap_atomic(shared_data);
581 }
582 if (ring_req->operation != BLKIF_OP_INDIRECT) {
583 ring_req->u.rw.seg[i] =
80bfa2f6 584 (struct blkif_request_segment) {
33204663
JG
585 .gref = ref,
586 .first_sect = fsect,
587 .last_sect = lsect };
588 } else {
589 n = i % SEGS_PER_INDIRECT_FRAME;
590 segments[n] =
591 (struct blkif_request_segment) {
592 .gref = ref,
593 .first_sect = fsect,
594 .last_sect = lsect };
ed30bf31 595 }
9f27ee59 596 }
33204663
JG
597 if (segments)
598 kunmap_atomic(segments);
9f27ee59
JF
599
600 info->ring.req_prod_pvt++;
601
602 /* Keep a private copy so we can reissue requests when recovering. */
603 info->shadow[id].req = *ring_req;
604
0a8704a5
RPM
605 if (new_persistent_gnts)
606 gnttab_free_grant_references(gref_head);
9f27ee59
JF
607
608 return 0;
609}
610
33204663
JG
611/*
612 * Generate a Xen blkfront IO request from a blk layer request. Reads
613 * and writes are handled as expected.
614 *
615 * @req: a request struct
616 */
617static int blkif_queue_request(struct request *req)
618{
619 struct blkfront_info *info = req->rq_disk->private_data;
620
621 if (unlikely(info->connected != BLKIF_STATE_CONNECTED))
622 return 1;
623
624 if (unlikely(req->cmd_flags & (REQ_DISCARD | REQ_SECURE)))
625 return blkif_queue_discard_req(req);
626 else
627 return blkif_queue_rw_req(req);
628}
9f27ee59
JF
629
630static inline void flush_requests(struct blkfront_info *info)
631{
632 int notify;
633
634 RING_PUSH_REQUESTS_AND_CHECK_NOTIFY(&info->ring, notify);
635
636 if (notify)
637 notify_remote_via_irq(info->irq);
638}
639
ad42d391
VK
640static inline bool blkif_request_flush_invalid(struct request *req,
641 struct blkfront_info *info)
0f1ca65e
AA
642{
643 return ((req->cmd_type != REQ_TYPE_FS) ||
ad42d391
VK
644 ((req->cmd_flags & REQ_FLUSH) &&
645 !(info->feature_flush & REQ_FLUSH)) ||
646 ((req->cmd_flags & REQ_FUA) &&
647 !(info->feature_flush & REQ_FUA)));
0f1ca65e
AA
648}
649
907c3eb1
BL
650static int blkif_queue_rq(struct blk_mq_hw_ctx *hctx,
651 const struct blk_mq_queue_data *qd)
9f27ee59 652{
907c3eb1 653 struct blkfront_info *info = qd->rq->rq_disk->private_data;
9f27ee59 654
907c3eb1
BL
655 blk_mq_start_request(qd->rq);
656 spin_lock_irq(&info->io_lock);
657 if (RING_FULL(&info->ring))
658 goto out_busy;
9f27ee59 659
907c3eb1
BL
660 if (blkif_request_flush_invalid(qd->rq, info))
661 goto out_err;
296b2f6a 662
907c3eb1
BL
663 if (blkif_queue_request(qd->rq))
664 goto out_busy;
296b2f6a 665
907c3eb1
BL
666 flush_requests(info);
667 spin_unlock_irq(&info->io_lock);
668 return BLK_MQ_RQ_QUEUE_OK;
9f27ee59 669
907c3eb1
BL
670out_err:
671 spin_unlock_irq(&info->io_lock);
672 return BLK_MQ_RQ_QUEUE_ERROR;
9f27ee59 673
907c3eb1
BL
674out_busy:
675 spin_unlock_irq(&info->io_lock);
676 blk_mq_stop_hw_queue(hctx);
677 return BLK_MQ_RQ_QUEUE_BUSY;
9f27ee59
JF
678}
679
907c3eb1
BL
680static struct blk_mq_ops blkfront_mq_ops = {
681 .queue_rq = blkif_queue_rq,
682 .map_queue = blk_mq_map_queue,
683};
684
402b27f9 685static int xlvbd_init_blk_queue(struct gendisk *gd, u16 sector_size,
7c4d7d71 686 unsigned int physical_sector_size,
402b27f9 687 unsigned int segments)
9f27ee59 688{
165125e1 689 struct request_queue *rq;
ed30bf31 690 struct blkfront_info *info = gd->private_data;
9f27ee59 691
907c3eb1
BL
692 memset(&info->tag_set, 0, sizeof(info->tag_set));
693 info->tag_set.ops = &blkfront_mq_ops;
694 info->tag_set.nr_hw_queues = 1;
695 info->tag_set.queue_depth = BLK_RING_SIZE(info);
696 info->tag_set.numa_node = NUMA_NO_NODE;
697 info->tag_set.flags = BLK_MQ_F_SHOULD_MERGE | BLK_MQ_F_SG_MERGE;
698 info->tag_set.cmd_size = 0;
699 info->tag_set.driver_data = info;
700
701 if (blk_mq_alloc_tag_set(&info->tag_set))
9f27ee59 702 return -1;
907c3eb1
BL
703 rq = blk_mq_init_queue(&info->tag_set);
704 if (IS_ERR(rq)) {
705 blk_mq_free_tag_set(&info->tag_set);
706 return -1;
707 }
9f27ee59 708
66d352e1 709 queue_flag_set_unlocked(QUEUE_FLAG_VIRT, rq);
9f27ee59 710
ed30bf31
LD
711 if (info->feature_discard) {
712 queue_flag_set_unlocked(QUEUE_FLAG_DISCARD, rq);
713 blk_queue_max_discard_sectors(rq, get_capacity(gd));
714 rq->limits.discard_granularity = info->discard_granularity;
715 rq->limits.discard_alignment = info->discard_alignment;
5ea42986
KRW
716 if (info->feature_secdiscard)
717 queue_flag_set_unlocked(QUEUE_FLAG_SECDISCARD, rq);
ed30bf31
LD
718 }
719
9f27ee59 720 /* Hard sector size and max sectors impersonate the equiv. hardware. */
e1defc4f 721 blk_queue_logical_block_size(rq, sector_size);
7c4d7d71 722 blk_queue_physical_block_size(rq, physical_sector_size);
294caaf2 723 blk_queue_max_hw_sectors(rq, (segments * PAGE_SIZE) / 512);
9f27ee59
JF
724
725 /* Each segment in a request is up to an aligned page in size. */
726 blk_queue_segment_boundary(rq, PAGE_SIZE - 1);
727 blk_queue_max_segment_size(rq, PAGE_SIZE);
728
729 /* Ensure a merged request will fit in a single I/O ring slot. */
402b27f9 730 blk_queue_max_segments(rq, segments);
9f27ee59
JF
731
732 /* Make sure buffer addresses are sector-aligned. */
733 blk_queue_dma_alignment(rq, 511);
734
1c91fe1a
IC
735 /* Make sure we don't use bounce buffers. */
736 blk_queue_bounce_limit(rq, BLK_BOUNCE_ANY);
737
9f27ee59
JF
738 gd->queue = rq;
739
740 return 0;
741}
742
fdf9b965
VK
743static const char *flush_info(unsigned int feature_flush)
744{
745 switch (feature_flush & ((REQ_FLUSH | REQ_FUA))) {
746 case REQ_FLUSH|REQ_FUA:
747 return "barrier: enabled;";
748 case REQ_FLUSH:
749 return "flush diskcache: enabled;";
750 default:
751 return "barrier or flush: disabled;";
752 }
753}
9f27ee59 754
4913efe4 755static void xlvbd_flush(struct blkfront_info *info)
9f27ee59 756{
4913efe4 757 blk_queue_flush(info->rq, info->feature_flush);
fdf9b965
VK
758 pr_info("blkfront: %s: %s %s %s %s %s\n",
759 info->gd->disk_name, flush_info(info->feature_flush),
760 "persistent grants:", info->feature_persistent ?
761 "enabled;" : "disabled;", "indirect descriptors:",
762 info->max_indirect_segments ? "enabled;" : "disabled;");
9f27ee59
JF
763}
764
c80a4209
SS
765static int xen_translate_vdev(int vdevice, int *minor, unsigned int *offset)
766{
767 int major;
768 major = BLKIF_MAJOR(vdevice);
769 *minor = BLKIF_MINOR(vdevice);
770 switch (major) {
771 case XEN_IDE0_MAJOR:
772 *offset = (*minor / 64) + EMULATED_HD_DISK_NAME_OFFSET;
773 *minor = ((*minor / 64) * PARTS_PER_DISK) +
774 EMULATED_HD_DISK_MINOR_OFFSET;
775 break;
776 case XEN_IDE1_MAJOR:
777 *offset = (*minor / 64) + 2 + EMULATED_HD_DISK_NAME_OFFSET;
778 *minor = (((*minor / 64) + 2) * PARTS_PER_DISK) +
779 EMULATED_HD_DISK_MINOR_OFFSET;
780 break;
781 case XEN_SCSI_DISK0_MAJOR:
782 *offset = (*minor / PARTS_PER_DISK) + EMULATED_SD_DISK_NAME_OFFSET;
783 *minor = *minor + EMULATED_SD_DISK_MINOR_OFFSET;
784 break;
785 case XEN_SCSI_DISK1_MAJOR:
786 case XEN_SCSI_DISK2_MAJOR:
787 case XEN_SCSI_DISK3_MAJOR:
788 case XEN_SCSI_DISK4_MAJOR:
789 case XEN_SCSI_DISK5_MAJOR:
790 case XEN_SCSI_DISK6_MAJOR:
791 case XEN_SCSI_DISK7_MAJOR:
792 *offset = (*minor / PARTS_PER_DISK) +
793 ((major - XEN_SCSI_DISK1_MAJOR + 1) * 16) +
794 EMULATED_SD_DISK_NAME_OFFSET;
795 *minor = *minor +
796 ((major - XEN_SCSI_DISK1_MAJOR + 1) * 16 * PARTS_PER_DISK) +
797 EMULATED_SD_DISK_MINOR_OFFSET;
798 break;
799 case XEN_SCSI_DISK8_MAJOR:
800 case XEN_SCSI_DISK9_MAJOR:
801 case XEN_SCSI_DISK10_MAJOR:
802 case XEN_SCSI_DISK11_MAJOR:
803 case XEN_SCSI_DISK12_MAJOR:
804 case XEN_SCSI_DISK13_MAJOR:
805 case XEN_SCSI_DISK14_MAJOR:
806 case XEN_SCSI_DISK15_MAJOR:
807 *offset = (*minor / PARTS_PER_DISK) +
808 ((major - XEN_SCSI_DISK8_MAJOR + 8) * 16) +
809 EMULATED_SD_DISK_NAME_OFFSET;
810 *minor = *minor +
811 ((major - XEN_SCSI_DISK8_MAJOR + 8) * 16 * PARTS_PER_DISK) +
812 EMULATED_SD_DISK_MINOR_OFFSET;
813 break;
814 case XENVBD_MAJOR:
815 *offset = *minor / PARTS_PER_DISK;
816 break;
817 default:
818 printk(KERN_WARNING "blkfront: your disk configuration is "
819 "incorrect, please use an xvd device instead\n");
820 return -ENODEV;
821 }
822 return 0;
823}
9f27ee59 824
e77c78c0
JB
825static char *encode_disk_name(char *ptr, unsigned int n)
826{
827 if (n >= 26)
828 ptr = encode_disk_name(ptr, n / 26 - 1);
829 *ptr = 'a' + n % 26;
830 return ptr + 1;
831}
832
9246b5f0
CL
833static int xlvbd_alloc_gendisk(blkif_sector_t capacity,
834 struct blkfront_info *info,
7c4d7d71
SB
835 u16 vdisk_info, u16 sector_size,
836 unsigned int physical_sector_size)
9f27ee59
JF
837{
838 struct gendisk *gd;
839 int nr_minors = 1;
c80a4209 840 int err;
9246b5f0
CL
841 unsigned int offset;
842 int minor;
843 int nr_parts;
e77c78c0 844 char *ptr;
9f27ee59
JF
845
846 BUG_ON(info->gd != NULL);
847 BUG_ON(info->rq != NULL);
848
9246b5f0
CL
849 if ((info->vdevice>>EXT_SHIFT) > 1) {
850 /* this is above the extended range; something is wrong */
851 printk(KERN_WARNING "blkfront: vdevice 0x%x is above the extended range; ignoring\n", info->vdevice);
852 return -ENODEV;
853 }
854
855 if (!VDEV_IS_EXTENDED(info->vdevice)) {
c80a4209
SS
856 err = xen_translate_vdev(info->vdevice, &minor, &offset);
857 if (err)
858 return err;
859 nr_parts = PARTS_PER_DISK;
9246b5f0
CL
860 } else {
861 minor = BLKIF_MINOR_EXT(info->vdevice);
862 nr_parts = PARTS_PER_EXT_DISK;
c80a4209 863 offset = minor / nr_parts;
89153b5c 864 if (xen_hvm_domain() && offset < EMULATED_HD_DISK_NAME_OFFSET + 4)
c80a4209
SS
865 printk(KERN_WARNING "blkfront: vdevice 0x%x might conflict with "
866 "emulated IDE disks,\n\t choose an xvd device name"
867 "from xvde on\n", info->vdevice);
9246b5f0 868 }
e77c78c0
JB
869 if (minor >> MINORBITS) {
870 pr_warn("blkfront: %#x's minor (%#x) out of range; ignoring\n",
871 info->vdevice, minor);
872 return -ENODEV;
873 }
9246b5f0
CL
874
875 if ((minor % nr_parts) == 0)
876 nr_minors = nr_parts;
9f27ee59 877
0e345826
JB
878 err = xlbd_reserve_minors(minor, nr_minors);
879 if (err)
880 goto out;
881 err = -ENODEV;
882
9f27ee59
JF
883 gd = alloc_disk(nr_minors);
884 if (gd == NULL)
0e345826 885 goto release;
9f27ee59 886
e77c78c0
JB
887 strcpy(gd->disk_name, DEV_NAME);
888 ptr = encode_disk_name(gd->disk_name + sizeof(DEV_NAME) - 1, offset);
889 BUG_ON(ptr >= gd->disk_name + DISK_NAME_LEN);
890 if (nr_minors > 1)
891 *ptr = 0;
892 else
893 snprintf(ptr, gd->disk_name + DISK_NAME_LEN - ptr,
894 "%d", minor & (nr_parts - 1));
9f27ee59
JF
895
896 gd->major = XENVBD_MAJOR;
897 gd->first_minor = minor;
898 gd->fops = &xlvbd_block_fops;
899 gd->private_data = info;
900 gd->driverfs_dev = &(info->xbdev->dev);
901 set_capacity(gd, capacity);
902
7c4d7d71 903 if (xlvbd_init_blk_queue(gd, sector_size, physical_sector_size,
402b27f9
RPM
904 info->max_indirect_segments ? :
905 BLKIF_MAX_SEGMENTS_PER_REQUEST)) {
9f27ee59 906 del_gendisk(gd);
0e345826 907 goto release;
9f27ee59
JF
908 }
909
910 info->rq = gd->queue;
911 info->gd = gd;
912
4913efe4 913 xlvbd_flush(info);
9f27ee59
JF
914
915 if (vdisk_info & VDISK_READONLY)
916 set_disk_ro(gd, 1);
917
918 if (vdisk_info & VDISK_REMOVABLE)
919 gd->flags |= GENHD_FL_REMOVABLE;
920
921 if (vdisk_info & VDISK_CDROM)
922 gd->flags |= GENHD_FL_CD;
923
924 return 0;
925
0e345826
JB
926 release:
927 xlbd_release_minors(minor, nr_minors);
9f27ee59
JF
928 out:
929 return err;
930}
931
a66b5aeb
DS
932static void xlvbd_release_gendisk(struct blkfront_info *info)
933{
934 unsigned int minor, nr_minors;
a66b5aeb
DS
935
936 if (info->rq == NULL)
937 return;
938
a66b5aeb 939 /* No more blkif_request(). */
907c3eb1 940 blk_mq_stop_hw_queues(info->rq);
a66b5aeb
DS
941
942 /* No more gnttab callback work. */
943 gnttab_cancel_free_callback(&info->callback);
a66b5aeb
DS
944
945 /* Flush gnttab callback work. Must be done with no locks held. */
43829731 946 flush_work(&info->work);
a66b5aeb
DS
947
948 del_gendisk(info->gd);
949
950 minor = info->gd->first_minor;
951 nr_minors = info->gd->minors;
952 xlbd_release_minors(minor, nr_minors);
953
954 blk_cleanup_queue(info->rq);
907c3eb1 955 blk_mq_free_tag_set(&info->tag_set);
a66b5aeb
DS
956 info->rq = NULL;
957
958 put_disk(info->gd);
959 info->gd = NULL;
960}
961
907c3eb1 962/* Must be called with io_lock holded */
9f27ee59
JF
963static void kick_pending_request_queues(struct blkfront_info *info)
964{
907c3eb1
BL
965 if (!RING_FULL(&info->ring))
966 blk_mq_start_stopped_hw_queues(info->rq, true);
9f27ee59
JF
967}
968
969static void blkif_restart_queue(struct work_struct *work)
970{
971 struct blkfront_info *info = container_of(work, struct blkfront_info, work);
972
3467811e 973 spin_lock_irq(&info->io_lock);
9f27ee59
JF
974 if (info->connected == BLKIF_STATE_CONNECTED)
975 kick_pending_request_queues(info);
3467811e 976 spin_unlock_irq(&info->io_lock);
9f27ee59
JF
977}
978
979static void blkif_free(struct blkfront_info *info, int suspend)
980{
155b7edb
RPM
981 struct grant *persistent_gnt;
982 struct grant *n;
402b27f9 983 int i, j, segs;
0a8704a5 984
9f27ee59 985 /* Prevent new requests being issued until we fix things up. */
3467811e 986 spin_lock_irq(&info->io_lock);
9f27ee59
JF
987 info->connected = suspend ?
988 BLKIF_STATE_SUSPENDED : BLKIF_STATE_DISCONNECTED;
989 /* No more blkif_request(). */
990 if (info->rq)
907c3eb1 991 blk_mq_stop_hw_queues(info->rq);
0a8704a5
RPM
992
993 /* Remove all persistent grants */
bfe11d6d 994 if (!list_empty(&info->grants)) {
155b7edb 995 list_for_each_entry_safe(persistent_gnt, n,
bfe11d6d 996 &info->grants, node) {
155b7edb 997 list_del(&persistent_gnt->node);
9c1e050c
RPM
998 if (persistent_gnt->gref != GRANT_INVALID_REF) {
999 gnttab_end_foreign_access(persistent_gnt->gref,
1000 0, 0UL);
1001 info->persistent_gnts_c--;
1002 }
bfe11d6d
RPM
1003 if (info->feature_persistent)
1004 __free_page(pfn_to_page(persistent_gnt->pfn));
155b7edb 1005 kfree(persistent_gnt);
0a8704a5 1006 }
0a8704a5 1007 }
9c1e050c 1008 BUG_ON(info->persistent_gnts_c != 0);
0a8704a5 1009
bfe11d6d
RPM
1010 /*
1011 * Remove indirect pages, this only happens when using indirect
1012 * descriptors but not persistent grants
1013 */
1014 if (!list_empty(&info->indirect_pages)) {
1015 struct page *indirect_page, *n;
1016
1017 BUG_ON(info->feature_persistent);
1018 list_for_each_entry_safe(indirect_page, n, &info->indirect_pages, lru) {
1019 list_del(&indirect_page->lru);
1020 __free_page(indirect_page);
1021 }
1022 }
1023
86839c56 1024 for (i = 0; i < BLK_RING_SIZE(info); i++) {
402b27f9
RPM
1025 /*
1026 * Clear persistent grants present in requests already
1027 * on the shared ring
1028 */
1029 if (!info->shadow[i].request)
1030 goto free_shadow;
1031
1032 segs = info->shadow[i].req.operation == BLKIF_OP_INDIRECT ?
1033 info->shadow[i].req.u.indirect.nr_segments :
1034 info->shadow[i].req.u.rw.nr_segments;
1035 for (j = 0; j < segs; j++) {
1036 persistent_gnt = info->shadow[i].grants_used[j];
1037 gnttab_end_foreign_access(persistent_gnt->gref, 0, 0UL);
bfe11d6d
RPM
1038 if (info->feature_persistent)
1039 __free_page(pfn_to_page(persistent_gnt->pfn));
402b27f9
RPM
1040 kfree(persistent_gnt);
1041 }
1042
1043 if (info->shadow[i].req.operation != BLKIF_OP_INDIRECT)
1044 /*
1045 * If this is not an indirect operation don't try to
1046 * free indirect segments
1047 */
1048 goto free_shadow;
1049
1050 for (j = 0; j < INDIRECT_GREFS(segs); j++) {
1051 persistent_gnt = info->shadow[i].indirect_grants[j];
1052 gnttab_end_foreign_access(persistent_gnt->gref, 0, 0UL);
1053 __free_page(pfn_to_page(persistent_gnt->pfn));
1054 kfree(persistent_gnt);
1055 }
1056
1057free_shadow:
1058 kfree(info->shadow[i].grants_used);
1059 info->shadow[i].grants_used = NULL;
1060 kfree(info->shadow[i].indirect_grants);
1061 info->shadow[i].indirect_grants = NULL;
b7649158
RPM
1062 kfree(info->shadow[i].sg);
1063 info->shadow[i].sg = NULL;
402b27f9
RPM
1064 }
1065
9f27ee59
JF
1066 /* No more gnttab callback work. */
1067 gnttab_cancel_free_callback(&info->callback);
3467811e 1068 spin_unlock_irq(&info->io_lock);
9f27ee59
JF
1069
1070 /* Flush gnttab callback work. Must be done with no locks held. */
43829731 1071 flush_work(&info->work);
9f27ee59
JF
1072
1073 /* Free resources associated with old device channel. */
86839c56
BL
1074 for (i = 0; i < info->nr_ring_pages; i++) {
1075 if (info->ring_ref[i] != GRANT_INVALID_REF) {
1076 gnttab_end_foreign_access(info->ring_ref[i], 0, 0);
1077 info->ring_ref[i] = GRANT_INVALID_REF;
1078 }
9f27ee59 1079 }
86839c56
BL
1080 free_pages((unsigned long)info->ring.sring, get_order(info->nr_ring_pages * PAGE_SIZE));
1081 info->ring.sring = NULL;
1082
9f27ee59
JF
1083 if (info->irq)
1084 unbind_from_irqhandler(info->irq, info);
1085 info->evtchn = info->irq = 0;
1086
1087}
1088
0a8704a5
RPM
1089static void blkif_completion(struct blk_shadow *s, struct blkfront_info *info,
1090 struct blkif_response *bret)
9f27ee59 1091{
d62f6918 1092 int i = 0;
b7649158 1093 struct scatterlist *sg;
0a8704a5
RPM
1094 char *bvec_data;
1095 void *shared_data;
402b27f9
RPM
1096 int nseg;
1097
1098 nseg = s->req.operation == BLKIF_OP_INDIRECT ?
1099 s->req.u.indirect.nr_segments : s->req.u.rw.nr_segments;
0a8704a5 1100
bfe11d6d 1101 if (bret->operation == BLKIF_OP_READ && info->feature_persistent) {
b7649158
RPM
1102 for_each_sg(s->sg, sg, nseg, i) {
1103 BUG_ON(sg->offset + sg->length > PAGE_SIZE);
0a8704a5
RPM
1104 shared_data = kmap_atomic(
1105 pfn_to_page(s->grants_used[i]->pfn));
b7649158
RPM
1106 bvec_data = kmap_atomic(sg_page(sg));
1107 memcpy(bvec_data + sg->offset,
1108 shared_data + sg->offset,
1109 sg->length);
1110 kunmap_atomic(bvec_data);
0a8704a5 1111 kunmap_atomic(shared_data);
0a8704a5
RPM
1112 }
1113 }
1114 /* Add the persistent grant into the list of free grants */
402b27f9 1115 for (i = 0; i < nseg; i++) {
fbe363c4
RPM
1116 if (gnttab_query_foreign_access(s->grants_used[i]->gref)) {
1117 /*
1118 * If the grant is still mapped by the backend (the
1119 * backend has chosen to make this grant persistent)
1120 * we add it at the head of the list, so it will be
1121 * reused first.
1122 */
bfe11d6d
RPM
1123 if (!info->feature_persistent)
1124 pr_alert_ratelimited("backed has not unmapped grant: %u\n",
1125 s->grants_used[i]->gref);
1126 list_add(&s->grants_used[i]->node, &info->grants);
fbe363c4
RPM
1127 info->persistent_gnts_c++;
1128 } else {
1129 /*
1130 * If the grant is not mapped by the backend we end the
1131 * foreign access and add it to the tail of the list,
1132 * so it will not be picked again unless we run out of
1133 * persistent grants.
1134 */
1135 gnttab_end_foreign_access(s->grants_used[i]->gref, 0, 0UL);
1136 s->grants_used[i]->gref = GRANT_INVALID_REF;
bfe11d6d 1137 list_add_tail(&s->grants_used[i]->node, &info->grants);
fbe363c4 1138 }
0a8704a5 1139 }
402b27f9
RPM
1140 if (s->req.operation == BLKIF_OP_INDIRECT) {
1141 for (i = 0; i < INDIRECT_GREFS(nseg); i++) {
fbe363c4 1142 if (gnttab_query_foreign_access(s->indirect_grants[i]->gref)) {
bfe11d6d
RPM
1143 if (!info->feature_persistent)
1144 pr_alert_ratelimited("backed has not unmapped grant: %u\n",
1145 s->indirect_grants[i]->gref);
1146 list_add(&s->indirect_grants[i]->node, &info->grants);
fbe363c4
RPM
1147 info->persistent_gnts_c++;
1148 } else {
bfe11d6d
RPM
1149 struct page *indirect_page;
1150
fbe363c4 1151 gnttab_end_foreign_access(s->indirect_grants[i]->gref, 0, 0UL);
bfe11d6d
RPM
1152 /*
1153 * Add the used indirect page back to the list of
1154 * available pages for indirect grefs.
1155 */
7b076750
BL
1156 if (!info->feature_persistent) {
1157 indirect_page = pfn_to_page(s->indirect_grants[i]->pfn);
1158 list_add(&indirect_page->lru, &info->indirect_pages);
1159 }
fbe363c4 1160 s->indirect_grants[i]->gref = GRANT_INVALID_REF;
bfe11d6d 1161 list_add_tail(&s->indirect_grants[i]->node, &info->grants);
fbe363c4 1162 }
402b27f9
RPM
1163 }
1164 }
9f27ee59
JF
1165}
1166
1167static irqreturn_t blkif_interrupt(int irq, void *dev_id)
1168{
1169 struct request *req;
1170 struct blkif_response *bret;
1171 RING_IDX i, rp;
1172 unsigned long flags;
1173 struct blkfront_info *info = (struct blkfront_info *)dev_id;
f4829a9b 1174 int error;
9f27ee59 1175
3467811e 1176 spin_lock_irqsave(&info->io_lock, flags);
9f27ee59
JF
1177
1178 if (unlikely(info->connected != BLKIF_STATE_CONNECTED)) {
3467811e 1179 spin_unlock_irqrestore(&info->io_lock, flags);
9f27ee59
JF
1180 return IRQ_HANDLED;
1181 }
1182
1183 again:
1184 rp = info->ring.sring->rsp_prod;
1185 rmb(); /* Ensure we see queued responses up to 'rp'. */
1186
1187 for (i = info->ring.rsp_cons; i != rp; i++) {
1188 unsigned long id;
9f27ee59
JF
1189
1190 bret = RING_GET_RESPONSE(&info->ring, i);
1191 id = bret->id;
6878c32e
KRW
1192 /*
1193 * The backend has messed up and given us an id that we would
1194 * never have given to it (we stamp it up to BLK_RING_SIZE -
1195 * look in get_id_from_freelist.
1196 */
86839c56 1197 if (id >= BLK_RING_SIZE(info)) {
6878c32e
KRW
1198 WARN(1, "%s: response to %s has incorrect id (%ld)\n",
1199 info->gd->disk_name, op_name(bret->operation), id);
1200 /* We can't safely get the 'struct request' as
1201 * the id is busted. */
1202 continue;
1203 }
a945b980 1204 req = info->shadow[id].request;
9f27ee59 1205
5ea42986 1206 if (bret->operation != BLKIF_OP_DISCARD)
0a8704a5 1207 blkif_completion(&info->shadow[id], info, bret);
9f27ee59 1208
6878c32e
KRW
1209 if (add_id_to_freelist(info, id)) {
1210 WARN(1, "%s: response to %s (id %ld) couldn't be recycled!\n",
1211 info->gd->disk_name, op_name(bret->operation), id);
1212 continue;
1213 }
9f27ee59 1214
f4829a9b 1215 error = (bret->status == BLKIF_RSP_OKAY) ? 0 : -EIO;
9f27ee59 1216 switch (bret->operation) {
ed30bf31
LD
1217 case BLKIF_OP_DISCARD:
1218 if (unlikely(bret->status == BLKIF_RSP_EOPNOTSUPP)) {
1219 struct request_queue *rq = info->rq;
6878c32e
KRW
1220 printk(KERN_WARNING "blkfront: %s: %s op failed\n",
1221 info->gd->disk_name, op_name(bret->operation));
f4829a9b 1222 error = -EOPNOTSUPP;
ed30bf31 1223 info->feature_discard = 0;
5ea42986 1224 info->feature_secdiscard = 0;
ed30bf31 1225 queue_flag_clear(QUEUE_FLAG_DISCARD, rq);
5ea42986 1226 queue_flag_clear(QUEUE_FLAG_SECDISCARD, rq);
ed30bf31 1227 }
f4829a9b 1228 blk_mq_complete_request(req, error);
ed30bf31 1229 break;
edf6ef59 1230 case BLKIF_OP_FLUSH_DISKCACHE:
9f27ee59
JF
1231 case BLKIF_OP_WRITE_BARRIER:
1232 if (unlikely(bret->status == BLKIF_RSP_EOPNOTSUPP)) {
6878c32e
KRW
1233 printk(KERN_WARNING "blkfront: %s: %s op failed\n",
1234 info->gd->disk_name, op_name(bret->operation));
f4829a9b 1235 error = -EOPNOTSUPP;
dcb8baec
JF
1236 }
1237 if (unlikely(bret->status == BLKIF_RSP_ERROR &&
97e36834 1238 info->shadow[id].req.u.rw.nr_segments == 0)) {
6878c32e
KRW
1239 printk(KERN_WARNING "blkfront: %s: empty %s op failed\n",
1240 info->gd->disk_name, op_name(bret->operation));
f4829a9b 1241 error = -EOPNOTSUPP;
dcb8baec 1242 }
f4829a9b
CH
1243 if (unlikely(error)) {
1244 if (error == -EOPNOTSUPP)
1245 error = 0;
4913efe4
TH
1246 info->feature_flush = 0;
1247 xlvbd_flush(info);
9f27ee59
JF
1248 }
1249 /* fall through */
1250 case BLKIF_OP_READ:
1251 case BLKIF_OP_WRITE:
1252 if (unlikely(bret->status != BLKIF_RSP_OKAY))
1253 dev_dbg(&info->xbdev->dev, "Bad return from blkdev data "
1254 "request: %x\n", bret->status);
1255
f4829a9b 1256 blk_mq_complete_request(req, error);
9f27ee59
JF
1257 break;
1258 default:
1259 BUG();
1260 }
1261 }
1262
1263 info->ring.rsp_cons = i;
1264
1265 if (i != info->ring.req_prod_pvt) {
1266 int more_to_do;
1267 RING_FINAL_CHECK_FOR_RESPONSES(&info->ring, more_to_do);
1268 if (more_to_do)
1269 goto again;
1270 } else
1271 info->ring.sring->rsp_event = i + 1;
1272
1273 kick_pending_request_queues(info);
1274
3467811e 1275 spin_unlock_irqrestore(&info->io_lock, flags);
9f27ee59
JF
1276
1277 return IRQ_HANDLED;
1278}
1279
1280
1281static int setup_blkring(struct xenbus_device *dev,
1282 struct blkfront_info *info)
1283{
1284 struct blkif_sring *sring;
86839c56
BL
1285 int err, i;
1286 unsigned long ring_size = info->nr_ring_pages * PAGE_SIZE;
1287 grant_ref_t gref[XENBUS_MAX_RING_PAGES];
9f27ee59 1288
86839c56
BL
1289 for (i = 0; i < info->nr_ring_pages; i++)
1290 info->ring_ref[i] = GRANT_INVALID_REF;
9f27ee59 1291
86839c56
BL
1292 sring = (struct blkif_sring *)__get_free_pages(GFP_NOIO | __GFP_HIGH,
1293 get_order(ring_size));
9f27ee59
JF
1294 if (!sring) {
1295 xenbus_dev_fatal(dev, -ENOMEM, "allocating shared ring");
1296 return -ENOMEM;
1297 }
1298 SHARED_RING_INIT(sring);
86839c56 1299 FRONT_RING_INIT(&info->ring, sring, ring_size);
9e973e64 1300
86839c56 1301 err = xenbus_grant_ring(dev, info->ring.sring, info->nr_ring_pages, gref);
9f27ee59 1302 if (err < 0) {
86839c56 1303 free_pages((unsigned long)sring, get_order(ring_size));
9f27ee59
JF
1304 info->ring.sring = NULL;
1305 goto fail;
1306 }
86839c56
BL
1307 for (i = 0; i < info->nr_ring_pages; i++)
1308 info->ring_ref[i] = gref[i];
9f27ee59
JF
1309
1310 err = xenbus_alloc_evtchn(dev, &info->evtchn);
1311 if (err)
1312 goto fail;
1313
89c30f16
TT
1314 err = bind_evtchn_to_irqhandler(info->evtchn, blkif_interrupt, 0,
1315 "blkif", info);
9f27ee59
JF
1316 if (err <= 0) {
1317 xenbus_dev_fatal(dev, err,
1318 "bind_evtchn_to_irqhandler failed");
1319 goto fail;
1320 }
1321 info->irq = err;
1322
1323 return 0;
1324fail:
1325 blkif_free(info, 0);
1326 return err;
1327}
1328
1329
1330/* Common code used when first setting up, and when resuming. */
203fd61f 1331static int talk_to_blkback(struct xenbus_device *dev,
9f27ee59
JF
1332 struct blkfront_info *info)
1333{
1334 const char *message = NULL;
1335 struct xenbus_transaction xbt;
86839c56
BL
1336 int err, i;
1337 unsigned int max_page_order = 0;
1338 unsigned int ring_page_order = 0;
1339
1340 err = xenbus_scanf(XBT_NIL, info->xbdev->otherend,
1341 "max-ring-page-order", "%u", &max_page_order);
1342 if (err != 1)
1343 info->nr_ring_pages = 1;
1344 else {
1345 ring_page_order = min(xen_blkif_max_ring_order, max_page_order);
1346 info->nr_ring_pages = 1 << ring_page_order;
1347 }
9f27ee59
JF
1348
1349 /* Create shared ring, alloc event channel. */
1350 err = setup_blkring(dev, info);
1351 if (err)
1352 goto out;
1353
1354again:
1355 err = xenbus_transaction_start(&xbt);
1356 if (err) {
1357 xenbus_dev_fatal(dev, err, "starting transaction");
1358 goto destroy_blkring;
1359 }
1360
86839c56
BL
1361 if (info->nr_ring_pages == 1) {
1362 err = xenbus_printf(xbt, dev->nodename,
1363 "ring-ref", "%u", info->ring_ref[0]);
1364 if (err) {
1365 message = "writing ring-ref";
1366 goto abort_transaction;
1367 }
1368 } else {
1369 err = xenbus_printf(xbt, dev->nodename,
1370 "ring-page-order", "%u", ring_page_order);
1371 if (err) {
1372 message = "writing ring-page-order";
1373 goto abort_transaction;
1374 }
1375
1376 for (i = 0; i < info->nr_ring_pages; i++) {
1377 char ring_ref_name[RINGREF_NAME_LEN];
1378
1379 snprintf(ring_ref_name, RINGREF_NAME_LEN, "ring-ref%u", i);
1380 err = xenbus_printf(xbt, dev->nodename, ring_ref_name,
1381 "%u", info->ring_ref[i]);
1382 if (err) {
1383 message = "writing ring-ref";
1384 goto abort_transaction;
1385 }
1386 }
9f27ee59
JF
1387 }
1388 err = xenbus_printf(xbt, dev->nodename,
1389 "event-channel", "%u", info->evtchn);
1390 if (err) {
1391 message = "writing event-channel";
1392 goto abort_transaction;
1393 }
3e334239
MA
1394 err = xenbus_printf(xbt, dev->nodename, "protocol", "%s",
1395 XEN_IO_PROTO_ABI_NATIVE);
1396 if (err) {
1397 message = "writing protocol";
1398 goto abort_transaction;
1399 }
0a8704a5 1400 err = xenbus_printf(xbt, dev->nodename,
cb5bd4d1 1401 "feature-persistent", "%u", 1);
0a8704a5
RPM
1402 if (err)
1403 dev_warn(&dev->dev,
1404 "writing persistent grants feature to xenbus");
9f27ee59
JF
1405
1406 err = xenbus_transaction_end(xbt, 0);
1407 if (err) {
1408 if (err == -EAGAIN)
1409 goto again;
1410 xenbus_dev_fatal(dev, err, "completing transaction");
1411 goto destroy_blkring;
1412 }
1413
86839c56
BL
1414 for (i = 0; i < BLK_RING_SIZE(info); i++)
1415 info->shadow[i].req.u.rw.id = i+1;
1416 info->shadow[BLK_RING_SIZE(info)-1].req.u.rw.id = 0x0fffffff;
9f27ee59
JF
1417 xenbus_switch_state(dev, XenbusStateInitialised);
1418
1419 return 0;
1420
1421 abort_transaction:
1422 xenbus_transaction_end(xbt, 1);
1423 if (message)
1424 xenbus_dev_fatal(dev, err, "%s", message);
1425 destroy_blkring:
1426 blkif_free(info, 0);
1427 out:
1428 return err;
1429}
1430
9f27ee59
JF
1431/**
1432 * Entry point to this code when a new device is created. Allocate the basic
1433 * structures and the ring buffer for communication with the backend, and
1434 * inform the backend of the appropriate details for those. Switch to
1435 * Initialised state.
1436 */
1437static int blkfront_probe(struct xenbus_device *dev,
1438 const struct xenbus_device_id *id)
1439{
86839c56 1440 int err, vdevice;
9f27ee59
JF
1441 struct blkfront_info *info;
1442
1443 /* FIXME: Use dynamic device id if this is not set. */
1444 err = xenbus_scanf(XBT_NIL, dev->nodename,
1445 "virtual-device", "%i", &vdevice);
1446 if (err != 1) {
9246b5f0
CL
1447 /* go looking in the extended area instead */
1448 err = xenbus_scanf(XBT_NIL, dev->nodename, "virtual-device-ext",
1449 "%i", &vdevice);
1450 if (err != 1) {
1451 xenbus_dev_fatal(dev, err, "reading virtual-device");
1452 return err;
1453 }
9f27ee59
JF
1454 }
1455
b98a409b
SS
1456 if (xen_hvm_domain()) {
1457 char *type;
1458 int len;
1459 /* no unplug has been done: do not hook devices != xen vbds */
51c71a3b 1460 if (xen_has_pv_and_legacy_disk_devices()) {
b98a409b
SS
1461 int major;
1462
1463 if (!VDEV_IS_EXTENDED(vdevice))
1464 major = BLKIF_MAJOR(vdevice);
1465 else
1466 major = XENVBD_MAJOR;
1467
1468 if (major != XENVBD_MAJOR) {
1469 printk(KERN_INFO
1470 "%s: HVM does not support vbd %d as xen block device\n",
02f1f217 1471 __func__, vdevice);
b98a409b
SS
1472 return -ENODEV;
1473 }
1474 }
1475 /* do not create a PV cdrom device if we are an HVM guest */
1476 type = xenbus_read(XBT_NIL, dev->nodename, "device-type", &len);
1477 if (IS_ERR(type))
1478 return -ENODEV;
1479 if (strncmp(type, "cdrom", 5) == 0) {
1480 kfree(type);
c1c5413a
SS
1481 return -ENODEV;
1482 }
b98a409b 1483 kfree(type);
c1c5413a 1484 }
9f27ee59
JF
1485 info = kzalloc(sizeof(*info), GFP_KERNEL);
1486 if (!info) {
1487 xenbus_dev_fatal(dev, -ENOMEM, "allocating info structure");
1488 return -ENOMEM;
1489 }
1490
b70f5fa0 1491 mutex_init(&info->mutex);
3467811e 1492 spin_lock_init(&info->io_lock);
9f27ee59
JF
1493 info->xbdev = dev;
1494 info->vdevice = vdevice;
bfe11d6d
RPM
1495 INIT_LIST_HEAD(&info->grants);
1496 INIT_LIST_HEAD(&info->indirect_pages);
0a8704a5 1497 info->persistent_gnts_c = 0;
9f27ee59
JF
1498 info->connected = BLKIF_STATE_DISCONNECTED;
1499 INIT_WORK(&info->work, blkif_restart_queue);
1500
9f27ee59
JF
1501 /* Front end dir is a number, which is used as the id. */
1502 info->handle = simple_strtoul(strrchr(dev->nodename, '/')+1, NULL, 0);
a1b4b12b 1503 dev_set_drvdata(&dev->dev, info);
9f27ee59 1504
9f27ee59
JF
1505 return 0;
1506}
1507
4246a0b6 1508static void split_bio_end(struct bio *bio)
402b27f9
RPM
1509{
1510 struct split_bio *split_bio = bio->bi_private;
1511
402b27f9
RPM
1512 if (atomic_dec_and_test(&split_bio->pending)) {
1513 split_bio->bio->bi_phys_segments = 0;
4246a0b6
CH
1514 split_bio->bio->bi_error = bio->bi_error;
1515 bio_endio(split_bio->bio);
402b27f9
RPM
1516 kfree(split_bio);
1517 }
1518 bio_put(bio);
1519}
9f27ee59
JF
1520
1521static int blkif_recover(struct blkfront_info *info)
1522{
1523 int i;
402b27f9 1524 struct request *req, *n;
9f27ee59 1525 struct blk_shadow *copy;
402b27f9
RPM
1526 int rc;
1527 struct bio *bio, *cloned_bio;
1528 struct bio_list bio_list, merge_bio;
1529 unsigned int segs, offset;
1530 int pending, size;
1531 struct split_bio *split_bio;
1532 struct list_head requests;
9f27ee59
JF
1533
1534 /* Stage 1: Make a safe copy of the shadow state. */
29d0b218 1535 copy = kmemdup(info->shadow, sizeof(info->shadow),
a144ff09 1536 GFP_NOIO | __GFP_REPEAT | __GFP_HIGH);
9f27ee59
JF
1537 if (!copy)
1538 return -ENOMEM;
9f27ee59
JF
1539
1540 /* Stage 2: Set up free list. */
1541 memset(&info->shadow, 0, sizeof(info->shadow));
86839c56 1542 for (i = 0; i < BLK_RING_SIZE(info); i++)
97e36834 1543 info->shadow[i].req.u.rw.id = i+1;
9f27ee59 1544 info->shadow_free = info->ring.req_prod_pvt;
86839c56 1545 info->shadow[BLK_RING_SIZE(info)-1].req.u.rw.id = 0x0fffffff;
9f27ee59 1546
d50babbe 1547 rc = blkfront_gather_backend_features(info);
402b27f9
RPM
1548 if (rc) {
1549 kfree(copy);
1550 return rc;
1551 }
1552
1553 segs = info->max_indirect_segments ? : BLKIF_MAX_SEGMENTS_PER_REQUEST;
1554 blk_queue_max_segments(info->rq, segs);
1555 bio_list_init(&bio_list);
1556 INIT_LIST_HEAD(&requests);
86839c56 1557 for (i = 0; i < BLK_RING_SIZE(info); i++) {
9f27ee59 1558 /* Not in use? */
a945b980 1559 if (!copy[i].request)
9f27ee59
JF
1560 continue;
1561
402b27f9
RPM
1562 /*
1563 * Get the bios in the request so we can re-queue them.
1564 */
1565 if (copy[i].request->cmd_flags &
1566 (REQ_FLUSH | REQ_FUA | REQ_DISCARD | REQ_SECURE)) {
1567 /*
1568 * Flush operations don't contain bios, so
1569 * we need to requeue the whole request
1570 */
1571 list_add(&copy[i].request->queuelist, &requests);
1572 continue;
5ea42986 1573 }
402b27f9
RPM
1574 merge_bio.head = copy[i].request->bio;
1575 merge_bio.tail = copy[i].request->biotail;
1576 bio_list_merge(&bio_list, &merge_bio);
1577 copy[i].request->bio = NULL;
3bb8c98e 1578 blk_end_request_all(copy[i].request, 0);
9f27ee59
JF
1579 }
1580
1581 kfree(copy);
1582
1583 xenbus_switch_state(info->xbdev, XenbusStateConnected);
1584
3467811e 1585 spin_lock_irq(&info->io_lock);
9f27ee59
JF
1586
1587 /* Now safe for us to use the shared ring */
1588 info->connected = BLKIF_STATE_CONNECTED;
1589
9f27ee59
JF
1590 /* Kick any other new requests queued since we resumed */
1591 kick_pending_request_queues(info);
1592
402b27f9
RPM
1593 list_for_each_entry_safe(req, n, &requests, queuelist) {
1594 /* Requeue pending requests (flush or discard) */
1595 list_del_init(&req->queuelist);
1596 BUG_ON(req->nr_phys_segments > segs);
907c3eb1 1597 blk_mq_requeue_request(req);
402b27f9 1598 }
3467811e 1599 spin_unlock_irq(&info->io_lock);
907c3eb1 1600 blk_mq_kick_requeue_list(info->rq);
9f27ee59 1601
402b27f9
RPM
1602 while ((bio = bio_list_pop(&bio_list)) != NULL) {
1603 /* Traverse the list of pending bios and re-queue them */
1604 if (bio_segments(bio) > segs) {
1605 /*
1606 * This bio has more segments than what we can
1607 * handle, we have to split it.
1608 */
1609 pending = (bio_segments(bio) + segs - 1) / segs;
1610 split_bio = kzalloc(sizeof(*split_bio), GFP_NOIO);
1611 BUG_ON(split_bio == NULL);
1612 atomic_set(&split_bio->pending, pending);
1613 split_bio->bio = bio;
1614 for (i = 0; i < pending; i++) {
1615 offset = (i * segs * PAGE_SIZE) >> 9;
1616 size = min((unsigned int)(segs * PAGE_SIZE) >> 9,
4f024f37 1617 (unsigned int)bio_sectors(bio) - offset);
402b27f9
RPM
1618 cloned_bio = bio_clone(bio, GFP_NOIO);
1619 BUG_ON(cloned_bio == NULL);
6678d83f 1620 bio_trim(cloned_bio, offset, size);
402b27f9
RPM
1621 cloned_bio->bi_private = split_bio;
1622 cloned_bio->bi_end_io = split_bio_end;
1623 submit_bio(cloned_bio->bi_rw, cloned_bio);
1624 }
1625 /*
1626 * Now we have to wait for all those smaller bios to
1627 * end, so we can also end the "parent" bio.
1628 */
1629 continue;
1630 }
1631 /* We don't need to split this bio */
1632 submit_bio(bio->bi_rw, bio);
1633 }
1634
9f27ee59
JF
1635 return 0;
1636}
1637
1638/**
1639 * We are reconnecting to the backend, due to a suspend/resume, or a backend
1640 * driver restart. We tear down our blkif structure and recreate it, but
1641 * leave the device-layer structures intact so that this is transparent to the
1642 * rest of the kernel.
1643 */
1644static int blkfront_resume(struct xenbus_device *dev)
1645{
a1b4b12b 1646 struct blkfront_info *info = dev_get_drvdata(&dev->dev);
9f27ee59
JF
1647 int err;
1648
1649 dev_dbg(&dev->dev, "blkfront_resume: %s\n", dev->nodename);
1650
1651 blkif_free(info, info->connected == BLKIF_STATE_CONNECTED);
1652
203fd61f 1653 err = talk_to_blkback(dev, info);
402b27f9
RPM
1654
1655 /*
1656 * We have to wait for the backend to switch to
1657 * connected state, since we want to read which
1658 * features it supports.
1659 */
9f27ee59
JF
1660
1661 return err;
1662}
1663
b70f5fa0
DS
1664static void
1665blkfront_closing(struct blkfront_info *info)
1666{
1667 struct xenbus_device *xbdev = info->xbdev;
1668 struct block_device *bdev = NULL;
1669
1670 mutex_lock(&info->mutex);
1671
1672 if (xbdev->state == XenbusStateClosing) {
1673 mutex_unlock(&info->mutex);
1674 return;
1675 }
1676
1677 if (info->gd)
1678 bdev = bdget_disk(info->gd, 0);
1679
1680 mutex_unlock(&info->mutex);
1681
1682 if (!bdev) {
1683 xenbus_frontend_closed(xbdev);
1684 return;
1685 }
1686
1687 mutex_lock(&bdev->bd_mutex);
1688
7b32d104 1689 if (bdev->bd_openers) {
b70f5fa0
DS
1690 xenbus_dev_error(xbdev, -EBUSY,
1691 "Device in use; refusing to close");
1692 xenbus_switch_state(xbdev, XenbusStateClosing);
1693 } else {
1694 xlvbd_release_gendisk(info);
1695 xenbus_frontend_closed(xbdev);
1696 }
1697
1698 mutex_unlock(&bdev->bd_mutex);
1699 bdput(bdev);
1700}
9f27ee59 1701
ed30bf31
LD
1702static void blkfront_setup_discard(struct blkfront_info *info)
1703{
1704 int err;
ed30bf31
LD
1705 unsigned int discard_granularity;
1706 unsigned int discard_alignment;
5ea42986 1707 unsigned int discard_secure;
ed30bf31 1708
1c8cad6c
OH
1709 info->feature_discard = 1;
1710 err = xenbus_gather(XBT_NIL, info->xbdev->otherend,
1711 "discard-granularity", "%u", &discard_granularity,
1712 "discard-alignment", "%u", &discard_alignment,
1713 NULL);
1714 if (!err) {
1715 info->discard_granularity = discard_granularity;
1716 info->discard_alignment = discard_alignment;
1717 }
1718 err = xenbus_gather(XBT_NIL, info->xbdev->otherend,
1719 "discard-secure", "%d", &discard_secure,
1720 NULL);
1721 if (!err)
1722 info->feature_secdiscard = !!discard_secure;
ed30bf31
LD
1723}
1724
402b27f9
RPM
1725static int blkfront_setup_indirect(struct blkfront_info *info)
1726{
d50babbe 1727 unsigned int segs;
402b27f9
RPM
1728 int err, i;
1729
d50babbe 1730 if (info->max_indirect_segments == 0)
402b27f9 1731 segs = BLKIF_MAX_SEGMENTS_PER_REQUEST;
d50babbe 1732 else
402b27f9 1733 segs = info->max_indirect_segments;
402b27f9 1734
86839c56 1735 err = fill_grant_buffer(info, (segs + INDIRECT_GREFS(segs)) * BLK_RING_SIZE(info));
402b27f9
RPM
1736 if (err)
1737 goto out_of_memory;
1738
bfe11d6d
RPM
1739 if (!info->feature_persistent && info->max_indirect_segments) {
1740 /*
1741 * We are using indirect descriptors but not persistent
1742 * grants, we need to allocate a set of pages that can be
1743 * used for mapping indirect grefs
1744 */
86839c56 1745 int num = INDIRECT_GREFS(segs) * BLK_RING_SIZE(info);
bfe11d6d
RPM
1746
1747 BUG_ON(!list_empty(&info->indirect_pages));
1748 for (i = 0; i < num; i++) {
1749 struct page *indirect_page = alloc_page(GFP_NOIO);
1750 if (!indirect_page)
1751 goto out_of_memory;
1752 list_add(&indirect_page->lru, &info->indirect_pages);
1753 }
1754 }
1755
86839c56 1756 for (i = 0; i < BLK_RING_SIZE(info); i++) {
402b27f9
RPM
1757 info->shadow[i].grants_used = kzalloc(
1758 sizeof(info->shadow[i].grants_used[0]) * segs,
1759 GFP_NOIO);
b7649158 1760 info->shadow[i].sg = kzalloc(sizeof(info->shadow[i].sg[0]) * segs, GFP_NOIO);
402b27f9
RPM
1761 if (info->max_indirect_segments)
1762 info->shadow[i].indirect_grants = kzalloc(
1763 sizeof(info->shadow[i].indirect_grants[0]) *
1764 INDIRECT_GREFS(segs),
1765 GFP_NOIO);
1766 if ((info->shadow[i].grants_used == NULL) ||
b7649158 1767 (info->shadow[i].sg == NULL) ||
402b27f9
RPM
1768 (info->max_indirect_segments &&
1769 (info->shadow[i].indirect_grants == NULL)))
1770 goto out_of_memory;
b7649158 1771 sg_init_table(info->shadow[i].sg, segs);
402b27f9
RPM
1772 }
1773
1774
1775 return 0;
1776
1777out_of_memory:
86839c56 1778 for (i = 0; i < BLK_RING_SIZE(info); i++) {
402b27f9
RPM
1779 kfree(info->shadow[i].grants_used);
1780 info->shadow[i].grants_used = NULL;
b7649158
RPM
1781 kfree(info->shadow[i].sg);
1782 info->shadow[i].sg = NULL;
402b27f9
RPM
1783 kfree(info->shadow[i].indirect_grants);
1784 info->shadow[i].indirect_grants = NULL;
1785 }
bfe11d6d
RPM
1786 if (!list_empty(&info->indirect_pages)) {
1787 struct page *indirect_page, *n;
1788 list_for_each_entry_safe(indirect_page, n, &info->indirect_pages, lru) {
1789 list_del(&indirect_page->lru);
1790 __free_page(indirect_page);
1791 }
1792 }
402b27f9
RPM
1793 return -ENOMEM;
1794}
1795
d50babbe
BL
1796/*
1797 * Gather all backend feature-*
1798 */
1799static int blkfront_gather_backend_features(struct blkfront_info *info)
1800{
1801 int err;
1802 int barrier, flush, discard, persistent;
1803 unsigned int indirect_segments;
1804
1805 info->feature_flush = 0;
1806
1807 err = xenbus_gather(XBT_NIL, info->xbdev->otherend,
1808 "feature-barrier", "%d", &barrier,
1809 NULL);
1810
1811 /*
1812 * If there's no "feature-barrier" defined, then it means
1813 * we're dealing with a very old backend which writes
1814 * synchronously; nothing to do.
1815 *
1816 * If there are barriers, then we use flush.
1817 */
1818 if (!err && barrier)
1819 info->feature_flush = REQ_FLUSH | REQ_FUA;
1820 /*
1821 * And if there is "feature-flush-cache" use that above
1822 * barriers.
1823 */
1824 err = xenbus_gather(XBT_NIL, info->xbdev->otherend,
1825 "feature-flush-cache", "%d", &flush,
1826 NULL);
1827
1828 if (!err && flush)
1829 info->feature_flush = REQ_FLUSH;
1830
1831 err = xenbus_gather(XBT_NIL, info->xbdev->otherend,
1832 "feature-discard", "%d", &discard,
1833 NULL);
1834
1835 if (!err && discard)
1836 blkfront_setup_discard(info);
1837
1838 err = xenbus_gather(XBT_NIL, info->xbdev->otherend,
1839 "feature-persistent", "%u", &persistent,
1840 NULL);
1841 if (err)
1842 info->feature_persistent = 0;
1843 else
1844 info->feature_persistent = persistent;
1845
1846 err = xenbus_gather(XBT_NIL, info->xbdev->otherend,
1847 "feature-max-indirect-segments", "%u", &indirect_segments,
1848 NULL);
1849 if (err)
1850 info->max_indirect_segments = 0;
1851 else
1852 info->max_indirect_segments = min(indirect_segments,
1853 xen_blkif_max_segments);
1854
1855 return blkfront_setup_indirect(info);
1856}
1857
9f27ee59
JF
1858/*
1859 * Invoked when the backend is finally 'ready' (and has told produced
1860 * the details about the physical device - #sectors, size, etc).
1861 */
1862static void blkfront_connect(struct blkfront_info *info)
1863{
1864 unsigned long long sectors;
1865 unsigned long sector_size;
7c4d7d71 1866 unsigned int physical_sector_size;
9f27ee59
JF
1867 unsigned int binfo;
1868 int err;
1869
1fa73be6
S
1870 switch (info->connected) {
1871 case BLKIF_STATE_CONNECTED:
1872 /*
1873 * Potentially, the back-end may be signalling
1874 * a capacity change; update the capacity.
1875 */
1876 err = xenbus_scanf(XBT_NIL, info->xbdev->otherend,
1877 "sectors", "%Lu", &sectors);
1878 if (XENBUS_EXIST_ERR(err))
1879 return;
1880 printk(KERN_INFO "Setting capacity to %Lu\n",
1881 sectors);
1882 set_capacity(info->gd, sectors);
2def141e 1883 revalidate_disk(info->gd);
1fa73be6 1884
402b27f9 1885 return;
1fa73be6 1886 case BLKIF_STATE_SUSPENDED:
402b27f9
RPM
1887 /*
1888 * If we are recovering from suspension, we need to wait
1889 * for the backend to announce it's features before
1890 * reconnecting, at least we need to know if the backend
1891 * supports indirect descriptors, and how many.
1892 */
1893 blkif_recover(info);
9f27ee59
JF
1894 return;
1895
b4dddb49
JF
1896 default:
1897 break;
1fa73be6 1898 }
9f27ee59
JF
1899
1900 dev_dbg(&info->xbdev->dev, "%s:%s.\n",
1901 __func__, info->xbdev->otherend);
1902
1903 err = xenbus_gather(XBT_NIL, info->xbdev->otherend,
1904 "sectors", "%llu", &sectors,
1905 "info", "%u", &binfo,
1906 "sector-size", "%lu", &sector_size,
1907 NULL);
1908 if (err) {
1909 xenbus_dev_fatal(info->xbdev, err,
1910 "reading backend fields at %s",
1911 info->xbdev->otherend);
1912 return;
1913 }
1914
7c4d7d71
SB
1915 /*
1916 * physcial-sector-size is a newer field, so old backends may not
1917 * provide this. Assume physical sector size to be the same as
1918 * sector_size in that case.
1919 */
1920 err = xenbus_scanf(XBT_NIL, info->xbdev->otherend,
1921 "physical-sector-size", "%u", &physical_sector_size);
1922 if (err != 1)
1923 physical_sector_size = sector_size;
1924
d50babbe 1925 err = blkfront_gather_backend_features(info);
402b27f9
RPM
1926 if (err) {
1927 xenbus_dev_fatal(info->xbdev, err, "setup_indirect at %s",
1928 info->xbdev->otherend);
1929 return;
1930 }
1931
7c4d7d71
SB
1932 err = xlvbd_alloc_gendisk(sectors, info, binfo, sector_size,
1933 physical_sector_size);
9f27ee59
JF
1934 if (err) {
1935 xenbus_dev_fatal(info->xbdev, err, "xlvbd_add at %s",
1936 info->xbdev->otherend);
1937 return;
1938 }
1939
1940 xenbus_switch_state(info->xbdev, XenbusStateConnected);
1941
1942 /* Kick pending requests. */
3467811e 1943 spin_lock_irq(&info->io_lock);
9f27ee59
JF
1944 info->connected = BLKIF_STATE_CONNECTED;
1945 kick_pending_request_queues(info);
3467811e 1946 spin_unlock_irq(&info->io_lock);
9f27ee59
JF
1947
1948 add_disk(info->gd);
1d78d705
CL
1949
1950 info->is_ready = 1;
9f27ee59
JF
1951}
1952
9f27ee59
JF
1953/**
1954 * Callback received when the backend's state changes.
1955 */
203fd61f 1956static void blkback_changed(struct xenbus_device *dev,
9f27ee59
JF
1957 enum xenbus_state backend_state)
1958{
a1b4b12b 1959 struct blkfront_info *info = dev_get_drvdata(&dev->dev);
9f27ee59 1960
203fd61f 1961 dev_dbg(&dev->dev, "blkfront:blkback_changed to state %d.\n", backend_state);
9f27ee59
JF
1962
1963 switch (backend_state) {
9f27ee59 1964 case XenbusStateInitWait:
a9b54bb9
BL
1965 if (dev->state != XenbusStateInitialising)
1966 break;
8ab0144a
BL
1967 if (talk_to_blkback(dev, info)) {
1968 kfree(info);
1969 dev_set_drvdata(&dev->dev, NULL);
1970 break;
1971 }
1972 case XenbusStateInitialising:
9f27ee59 1973 case XenbusStateInitialised:
b78c9512
NI
1974 case XenbusStateReconfiguring:
1975 case XenbusStateReconfigured:
9f27ee59 1976 case XenbusStateUnknown:
9f27ee59
JF
1977 break;
1978
1979 case XenbusStateConnected:
1980 blkfront_connect(info);
1981 break;
1982
36613717
DV
1983 case XenbusStateClosed:
1984 if (dev->state == XenbusStateClosed)
1985 break;
1986 /* Missed the backend's Closing state -- fallthrough */
9f27ee59 1987 case XenbusStateClosing:
b70f5fa0 1988 blkfront_closing(info);
9f27ee59
JF
1989 break;
1990 }
1991}
1992
fa1bd359 1993static int blkfront_remove(struct xenbus_device *xbdev)
9f27ee59 1994{
fa1bd359
DS
1995 struct blkfront_info *info = dev_get_drvdata(&xbdev->dev);
1996 struct block_device *bdev = NULL;
1997 struct gendisk *disk;
9f27ee59 1998
fa1bd359 1999 dev_dbg(&xbdev->dev, "%s removed", xbdev->nodename);
9f27ee59
JF
2000
2001 blkif_free(info, 0);
2002
fa1bd359
DS
2003 mutex_lock(&info->mutex);
2004
2005 disk = info->gd;
2006 if (disk)
2007 bdev = bdget_disk(disk, 0);
2008
2009 info->xbdev = NULL;
2010 mutex_unlock(&info->mutex);
2011
2012 if (!bdev) {
2013 kfree(info);
2014 return 0;
2015 }
2016
2017 /*
2018 * The xbdev was removed before we reached the Closed
2019 * state. See if it's safe to remove the disk. If the bdev
2020 * isn't closed yet, we let release take care of it.
2021 */
2022
2023 mutex_lock(&bdev->bd_mutex);
2024 info = disk->private_data;
2025
d54142c7
DS
2026 dev_warn(disk_to_dev(disk),
2027 "%s was hot-unplugged, %d stale handles\n",
2028 xbdev->nodename, bdev->bd_openers);
2029
7b32d104 2030 if (info && !bdev->bd_openers) {
fa1bd359
DS
2031 xlvbd_release_gendisk(info);
2032 disk->private_data = NULL;
0e345826 2033 kfree(info);
fa1bd359
DS
2034 }
2035
2036 mutex_unlock(&bdev->bd_mutex);
2037 bdput(bdev);
9f27ee59
JF
2038
2039 return 0;
2040}
2041
1d78d705
CL
2042static int blkfront_is_ready(struct xenbus_device *dev)
2043{
a1b4b12b 2044 struct blkfront_info *info = dev_get_drvdata(&dev->dev);
1d78d705 2045
5d7ed20e 2046 return info->is_ready && info->xbdev;
1d78d705
CL
2047}
2048
a63c848b 2049static int blkif_open(struct block_device *bdev, fmode_t mode)
9f27ee59 2050{
13961743
DS
2051 struct gendisk *disk = bdev->bd_disk;
2052 struct blkfront_info *info;
2053 int err = 0;
6e9624b8 2054
2a48fc0a 2055 mutex_lock(&blkfront_mutex);
6e9624b8 2056
13961743
DS
2057 info = disk->private_data;
2058 if (!info) {
2059 /* xbdev gone */
2060 err = -ERESTARTSYS;
2061 goto out;
2062 }
2063
2064 mutex_lock(&info->mutex);
2065
2066 if (!info->gd)
2067 /* xbdev is closed */
2068 err = -ERESTARTSYS;
2069
2070 mutex_unlock(&info->mutex);
2071
13961743 2072out:
2a48fc0a 2073 mutex_unlock(&blkfront_mutex);
13961743 2074 return err;
9f27ee59
JF
2075}
2076
db2a144b 2077static void blkif_release(struct gendisk *disk, fmode_t mode)
9f27ee59 2078{
a63c848b 2079 struct blkfront_info *info = disk->private_data;
7fd152f4
DS
2080 struct block_device *bdev;
2081 struct xenbus_device *xbdev;
2082
2a48fc0a 2083 mutex_lock(&blkfront_mutex);
7fd152f4
DS
2084
2085 bdev = bdget_disk(disk, 0);
7fd152f4 2086
2f089cb8
FP
2087 if (!bdev) {
2088 WARN(1, "Block device %s yanked out from us!\n", disk->disk_name);
2089 goto out_mutex;
2090 }
acfca3c6
DS
2091 if (bdev->bd_openers)
2092 goto out;
2093
7fd152f4
DS
2094 /*
2095 * Check if we have been instructed to close. We will have
2096 * deferred this request, because the bdev was still open.
2097 */
2098
2099 mutex_lock(&info->mutex);
2100 xbdev = info->xbdev;
2101
2102 if (xbdev && xbdev->state == XenbusStateClosing) {
2103 /* pending switch to state closed */
d54142c7 2104 dev_info(disk_to_dev(bdev->bd_disk), "releasing disk\n");
7fd152f4
DS
2105 xlvbd_release_gendisk(info);
2106 xenbus_frontend_closed(info->xbdev);
2107 }
2108
2109 mutex_unlock(&info->mutex);
2110
2111 if (!xbdev) {
2112 /* sudden device removal */
d54142c7 2113 dev_info(disk_to_dev(bdev->bd_disk), "releasing disk\n");
7fd152f4
DS
2114 xlvbd_release_gendisk(info);
2115 disk->private_data = NULL;
2116 kfree(info);
9f27ee59 2117 }
7fd152f4 2118
a4cc14ec 2119out:
dad5cf65 2120 bdput(bdev);
2f089cb8 2121out_mutex:
2a48fc0a 2122 mutex_unlock(&blkfront_mutex);
9f27ee59
JF
2123}
2124
83d5cde4 2125static const struct block_device_operations xlvbd_block_fops =
9f27ee59
JF
2126{
2127 .owner = THIS_MODULE,
a63c848b
AV
2128 .open = blkif_open,
2129 .release = blkif_release,
597592d9 2130 .getgeo = blkif_getgeo,
8a6cfeb6 2131 .ioctl = blkif_ioctl,
9f27ee59
JF
2132};
2133
2134
ec9c42ec 2135static const struct xenbus_device_id blkfront_ids[] = {
9f27ee59
JF
2136 { "vbd" },
2137 { "" }
2138};
2139
95afae48
DV
2140static struct xenbus_driver blkfront_driver = {
2141 .ids = blkfront_ids,
9f27ee59
JF
2142 .probe = blkfront_probe,
2143 .remove = blkfront_remove,
2144 .resume = blkfront_resume,
203fd61f 2145 .otherend_changed = blkback_changed,
1d78d705 2146 .is_ready = blkfront_is_ready,
95afae48 2147};
9f27ee59
JF
2148
2149static int __init xlblk_init(void)
2150{
469738e6
LE
2151 int ret;
2152
6e833587 2153 if (!xen_domain())
9f27ee59
JF
2154 return -ENODEV;
2155
86839c56
BL
2156 if (xen_blkif_max_ring_order > XENBUS_MAX_RING_PAGE_ORDER) {
2157 pr_info("Invalid max_ring_order (%d), will use default max: %d.\n",
2158 xen_blkif_max_ring_order, XENBUS_MAX_RING_PAGE_ORDER);
2159 xen_blkif_max_ring_order = 0;
2160 }
2161
51c71a3b 2162 if (!xen_has_pv_disk_devices())
b9136d20
IM
2163 return -ENODEV;
2164
9f27ee59
JF
2165 if (register_blkdev(XENVBD_MAJOR, DEV_NAME)) {
2166 printk(KERN_WARNING "xen_blk: can't get major %d with name %s\n",
2167 XENVBD_MAJOR, DEV_NAME);
2168 return -ENODEV;
2169 }
2170
73db144b 2171 ret = xenbus_register_frontend(&blkfront_driver);
469738e6
LE
2172 if (ret) {
2173 unregister_blkdev(XENVBD_MAJOR, DEV_NAME);
2174 return ret;
2175 }
2176
2177 return 0;
9f27ee59
JF
2178}
2179module_init(xlblk_init);
2180
2181
5a60d0cd 2182static void __exit xlblk_exit(void)
9f27ee59 2183{
8605067f
JB
2184 xenbus_unregister_driver(&blkfront_driver);
2185 unregister_blkdev(XENVBD_MAJOR, DEV_NAME);
2186 kfree(minors);
9f27ee59
JF
2187}
2188module_exit(xlblk_exit);
2189
2190MODULE_DESCRIPTION("Xen virtual block device frontend");
2191MODULE_LICENSE("GPL");
2192MODULE_ALIAS_BLOCKDEV_MAJOR(XENVBD_MAJOR);
d2f0c52b 2193MODULE_ALIAS("xen:vbd");
4f93f09b 2194MODULE_ALIAS("xenblk");