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