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