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