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