blob: fc770b7d3beb1951e80c2f16956f3dd0897efe48 [file] [log] [blame]
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001/*
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>
Ian Campbell597592d2008-02-21 13:03:45 -080040#include <linux/hdreg.h>
Christian Limpach440a01a2008-06-17 10:47:08 +020041#include <linux/cdrom.h>
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -070042#include <linux/module.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090043#include <linux/slab.h>
Arnd Bergmann2a48fc02010-06-02 14:28:52 +020044#include <linux/mutex.h>
Jens Axboe9e973e62009-02-24 08:10:09 +010045#include <linux/scatterlist.h>
Akinobu Mita34ae2e42012-01-21 00:15:26 +090046#include <linux/bitmap.h>
Roger Pau Monne155b7ed2013-03-18 17:49:34 +010047#include <linux/list.h>
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -070048
Jeremy Fitzhardinge1ccbf532009-10-06 15:11:14 -070049#include <xen/xen.h>
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -070050#include <xen/xenbus.h>
51#include <xen/grant_table.h>
52#include <xen/events.h>
53#include <xen/page.h>
Stefano Stabellinic1c54132010-05-14 12:44:30 +010054#include <xen/platform_pci.h>
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -070055
56#include <xen/interface/grant_table.h>
57#include <xen/interface/io/blkif.h>
Markus Armbruster3e334232008-04-02 10:54:02 -070058#include <xen/interface/io/protocols.h>
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -070059
60#include <asm/xen/hypervisor.h>
61
62enum blkif_state {
63 BLKIF_STATE_DISCONNECTED,
64 BLKIF_STATE_CONNECTED,
65 BLKIF_STATE_SUSPENDED,
66};
67
Roger Pau Monne0a8704a2012-10-24 18:58:45 +020068struct grant {
69 grant_ref_t gref;
70 unsigned long pfn;
Roger Pau Monne155b7ed2013-03-18 17:49:34 +010071 struct list_head node;
Roger Pau Monne0a8704a2012-10-24 18:58:45 +020072};
73
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -070074struct blk_shadow {
75 struct blkif_request req;
Jeremy Fitzhardingea945b982010-11-01 17:03:14 -040076 struct request *request;
Roger Pau Monne402b27f2013-04-18 16:06:54 +020077 struct grant **grants_used;
78 struct grant **indirect_grants;
Roger Pau Monneb7649152013-05-02 10:58:50 +020079 struct scatterlist *sg;
Roger Pau Monne402b27f2013-04-18 16:06:54 +020080};
81
82struct split_bio {
83 struct bio *bio;
84 atomic_t pending;
85 int err;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -070086};
87
Arnd Bergmann2a48fc02010-06-02 14:28:52 +020088static DEFINE_MUTEX(blkfront_mutex);
Alexey Dobriyan83d5cde2009-09-21 17:01:13 -070089static const struct block_device_operations xlvbd_block_fops;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -070090
Roger Pau Monne402b27f2013-04-18 16:06:54 +020091/*
92 * Maximum number of segments in indirect requests, the actual value used by
93 * the frontend driver is the minimum of this value and the value provided
94 * by the backend driver.
95 */
96
97static unsigned int xen_blkif_max_segments = 32;
Konrad Rzeszutek Wilk2d5dc3b2013-05-15 10:39:34 -040098module_param_named(max, xen_blkif_max_segments, int, S_IRUGO);
99MODULE_PARM_DESC(max, "Maximum amount of segments in indirect requests (default is 32)");
Roger Pau Monne402b27f2013-04-18 16:06:54 +0200100
Bob Liu86839c52015-06-03 13:40:03 +0800101/*
102 * Maximum order of pages to be used for the shared ring between front and
103 * backend, 4KB page granularity is used.
104 */
105static unsigned int xen_blkif_max_ring_order;
106module_param_named(max_ring_page_order, xen_blkif_max_ring_order, int, S_IRUGO);
107MODULE_PARM_DESC(max_ring_page_order, "Maximum order of pages to be used for the shared ring");
108
109#define BLK_RING_SIZE(info) __CONST_RING_SIZE(blkif, PAGE_SIZE * (info)->nr_ring_pages)
110#define BLK_MAX_RING_SIZE __CONST_RING_SIZE(blkif, PAGE_SIZE * XENBUS_MAX_RING_PAGES)
111/*
112 * ring-ref%i i=(-1UL) would take 11 characters + 'ring-ref' is 8, so 19
113 * characters are enough. Define to 20 to keep consist with backend.
114 */
115#define RINGREF_NAME_LEN (20)
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700116
117/*
118 * We have one of these per vbd, whether ide, scsi or 'other'. They
119 * hang in private_data off the gendisk structure. We may end up
120 * putting all kinds of interesting stuff here :-)
121 */
122struct blkfront_info
123{
Steven Noonan34678112012-02-17 12:04:44 -0800124 spinlock_t io_lock;
Daniel Stoddenb70f5fa2010-04-30 22:01:19 +0000125 struct mutex mutex;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700126 struct xenbus_device *xbdev;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700127 struct gendisk *gd;
128 int vdevice;
129 blkif_vdev_t handle;
130 enum blkif_state connected;
Bob Liu86839c52015-06-03 13:40:03 +0800131 int ring_ref[XENBUS_MAX_RING_PAGES];
132 unsigned int nr_ring_pages;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700133 struct blkif_front_ring ring;
134 unsigned int evtchn, irq;
135 struct request_queue *rq;
136 struct work_struct work;
137 struct gnttab_free_callback callback;
Bob Liu86839c52015-06-03 13:40:03 +0800138 struct blk_shadow shadow[BLK_MAX_RING_SIZE];
Roger Pau Monnebfe11d62013-10-29 18:31:14 +0100139 struct list_head grants;
140 struct list_head indirect_pages;
Roger Pau Monne0a8704a2012-10-24 18:58:45 +0200141 unsigned int persistent_gnts_c;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700142 unsigned long shadow_free;
Tejun Heo4913efe2010-09-03 11:56:16 +0200143 unsigned int feature_flush;
Konrad Rzeszutek Wilk5ea42982011-10-12 16:23:30 -0400144 unsigned int feature_discard:1;
145 unsigned int feature_secdiscard:1;
Li Dongyanged30bf32011-09-01 18:39:09 +0800146 unsigned int discard_granularity;
147 unsigned int discard_alignment;
Roger Pau Monne0a8704a2012-10-24 18:58:45 +0200148 unsigned int feature_persistent:1;
Roger Pau Monne402b27f2013-04-18 16:06:54 +0200149 unsigned int max_indirect_segments;
Christian Limpach1d78d702008-04-02 10:54:04 -0700150 int is_ready;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700151};
152
Jan Beulich0e345822010-08-07 18:28:55 +0200153static unsigned int nr_minors;
154static unsigned long *minors;
155static DEFINE_SPINLOCK(minor_lock);
156
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700157#define GRANT_INVALID_REF 0
158
159#define PARTS_PER_DISK 16
Chris Lalancette9246b5f2008-09-17 14:30:32 -0700160#define PARTS_PER_EXT_DISK 256
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700161
162#define BLKIF_MAJOR(dev) ((dev)>>8)
163#define BLKIF_MINOR(dev) ((dev) & 0xff)
164
Chris Lalancette9246b5f2008-09-17 14:30:32 -0700165#define EXT_SHIFT 28
166#define EXTENDED (1<<EXT_SHIFT)
167#define VDEV_IS_EXTENDED(dev) ((dev)&(EXTENDED))
168#define BLKIF_MINOR_EXT(dev) ((dev)&(~EXTENDED))
Stefano Stabellinic80a4202010-12-02 17:55:00 +0000169#define EMULATED_HD_DISK_MINOR_OFFSET (0)
170#define EMULATED_HD_DISK_NAME_OFFSET (EMULATED_HD_DISK_MINOR_OFFSET / 256)
Stefan Bader196cfe22011-07-14 15:30:22 +0200171#define EMULATED_SD_DISK_MINOR_OFFSET (0)
172#define EMULATED_SD_DISK_NAME_OFFSET (EMULATED_SD_DISK_MINOR_OFFSET / 256)
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700173
Chris Lalancette9246b5f2008-09-17 14:30:32 -0700174#define DEV_NAME "xvd" /* name in /dev */
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700175
Roger Pau Monne402b27f2013-04-18 16:06:54 +0200176#define SEGS_PER_INDIRECT_FRAME \
Roger Pau Monne80bfa2f2014-02-04 11:26:15 +0100177 (PAGE_SIZE/sizeof(struct blkif_request_segment))
Roger Pau Monne402b27f2013-04-18 16:06:54 +0200178#define INDIRECT_GREFS(_segs) \
179 ((_segs + SEGS_PER_INDIRECT_FRAME - 1)/SEGS_PER_INDIRECT_FRAME)
180
181static int blkfront_setup_indirect(struct blkfront_info *info);
182
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700183static int get_id_from_freelist(struct blkfront_info *info)
184{
185 unsigned long free = info->shadow_free;
Bob Liu86839c52015-06-03 13:40:03 +0800186 BUG_ON(free >= BLK_RING_SIZE(info));
Konrad Rzeszutek Wilk97e36832011-10-12 12:12:36 -0400187 info->shadow_free = info->shadow[free].req.u.rw.id;
188 info->shadow[free].req.u.rw.id = 0x0fffffee; /* debug */
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700189 return free;
190}
191
Konrad Rzeszutek Wilk6878c322012-05-25 17:34:51 -0400192static int add_id_to_freelist(struct blkfront_info *info,
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700193 unsigned long id)
194{
Konrad Rzeszutek Wilk6878c322012-05-25 17:34:51 -0400195 if (info->shadow[id].req.u.rw.id != id)
196 return -EINVAL;
197 if (info->shadow[id].request == NULL)
198 return -EINVAL;
Konrad Rzeszutek Wilk97e36832011-10-12 12:12:36 -0400199 info->shadow[id].req.u.rw.id = info->shadow_free;
Jeremy Fitzhardingea945b982010-11-01 17:03:14 -0400200 info->shadow[id].request = NULL;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700201 info->shadow_free = id;
Konrad Rzeszutek Wilk6878c322012-05-25 17:34:51 -0400202 return 0;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700203}
204
Roger Pau Monne9c1e0502013-03-18 17:49:35 +0100205static int fill_grant_buffer(struct blkfront_info *info, int num)
206{
207 struct page *granted_page;
208 struct grant *gnt_list_entry, *n;
209 int i = 0;
210
211 while(i < num) {
212 gnt_list_entry = kzalloc(sizeof(struct grant), GFP_NOIO);
213 if (!gnt_list_entry)
214 goto out_of_memory;
215
Roger Pau Monnebfe11d62013-10-29 18:31:14 +0100216 if (info->feature_persistent) {
217 granted_page = alloc_page(GFP_NOIO);
218 if (!granted_page) {
219 kfree(gnt_list_entry);
220 goto out_of_memory;
221 }
222 gnt_list_entry->pfn = page_to_pfn(granted_page);
Roger Pau Monne9c1e0502013-03-18 17:49:35 +0100223 }
224
Roger Pau Monne9c1e0502013-03-18 17:49:35 +0100225 gnt_list_entry->gref = GRANT_INVALID_REF;
Roger Pau Monnebfe11d62013-10-29 18:31:14 +0100226 list_add(&gnt_list_entry->node, &info->grants);
Roger Pau Monne9c1e0502013-03-18 17:49:35 +0100227 i++;
228 }
229
230 return 0;
231
232out_of_memory:
233 list_for_each_entry_safe(gnt_list_entry, n,
Roger Pau Monnebfe11d62013-10-29 18:31:14 +0100234 &info->grants, node) {
Roger Pau Monne9c1e0502013-03-18 17:49:35 +0100235 list_del(&gnt_list_entry->node);
Roger Pau Monnebfe11d62013-10-29 18:31:14 +0100236 if (info->feature_persistent)
237 __free_page(pfn_to_page(gnt_list_entry->pfn));
Roger Pau Monne9c1e0502013-03-18 17:49:35 +0100238 kfree(gnt_list_entry);
239 i--;
240 }
241 BUG_ON(i != 0);
242 return -ENOMEM;
243}
244
245static struct grant *get_grant(grant_ref_t *gref_head,
Roger Pau Monnebfe11d62013-10-29 18:31:14 +0100246 unsigned long pfn,
Roger Pau Monne9c1e0502013-03-18 17:49:35 +0100247 struct blkfront_info *info)
248{
249 struct grant *gnt_list_entry;
250 unsigned long buffer_mfn;
251
Roger Pau Monnebfe11d62013-10-29 18:31:14 +0100252 BUG_ON(list_empty(&info->grants));
253 gnt_list_entry = list_first_entry(&info->grants, struct grant,
Roger Pau Monne9c1e0502013-03-18 17:49:35 +0100254 node);
255 list_del(&gnt_list_entry->node);
256
257 if (gnt_list_entry->gref != GRANT_INVALID_REF) {
258 info->persistent_gnts_c--;
259 return gnt_list_entry;
260 }
261
262 /* Assign a gref to this page */
263 gnt_list_entry->gref = gnttab_claim_grant_reference(gref_head);
264 BUG_ON(gnt_list_entry->gref == -ENOSPC);
Roger Pau Monnebfe11d62013-10-29 18:31:14 +0100265 if (!info->feature_persistent) {
266 BUG_ON(!pfn);
267 gnt_list_entry->pfn = pfn;
268 }
Roger Pau Monne9c1e0502013-03-18 17:49:35 +0100269 buffer_mfn = pfn_to_mfn(gnt_list_entry->pfn);
270 gnttab_grant_foreign_access_ref(gnt_list_entry->gref,
271 info->xbdev->otherend_id,
272 buffer_mfn, 0);
273 return gnt_list_entry;
274}
275
Konrad Rzeszutek Wilk6878c322012-05-25 17:34:51 -0400276static const char *op_name(int op)
277{
278 static const char *const names[] = {
279 [BLKIF_OP_READ] = "read",
280 [BLKIF_OP_WRITE] = "write",
281 [BLKIF_OP_WRITE_BARRIER] = "barrier",
282 [BLKIF_OP_FLUSH_DISKCACHE] = "flush",
283 [BLKIF_OP_DISCARD] = "discard" };
284
285 if (op < 0 || op >= ARRAY_SIZE(names))
286 return "unknown";
287
288 if (!names[op])
289 return "reserved";
290
291 return names[op];
292}
Jan Beulich0e345822010-08-07 18:28:55 +0200293static int xlbd_reserve_minors(unsigned int minor, unsigned int nr)
294{
295 unsigned int end = minor + nr;
296 int rc;
297
298 if (end > nr_minors) {
299 unsigned long *bitmap, *old;
300
Thomas Meyerf0941482011-11-29 22:08:00 +0100301 bitmap = kcalloc(BITS_TO_LONGS(end), sizeof(*bitmap),
Jan Beulich0e345822010-08-07 18:28:55 +0200302 GFP_KERNEL);
303 if (bitmap == NULL)
304 return -ENOMEM;
305
306 spin_lock(&minor_lock);
307 if (end > nr_minors) {
308 old = minors;
309 memcpy(bitmap, minors,
310 BITS_TO_LONGS(nr_minors) * sizeof(*bitmap));
311 minors = bitmap;
312 nr_minors = BITS_TO_LONGS(end) * BITS_PER_LONG;
313 } else
314 old = bitmap;
315 spin_unlock(&minor_lock);
316 kfree(old);
317 }
318
319 spin_lock(&minor_lock);
320 if (find_next_bit(minors, end, minor) >= end) {
Akinobu Mita34ae2e42012-01-21 00:15:26 +0900321 bitmap_set(minors, minor, nr);
Jan Beulich0e345822010-08-07 18:28:55 +0200322 rc = 0;
323 } else
324 rc = -EBUSY;
325 spin_unlock(&minor_lock);
326
327 return rc;
328}
329
330static void xlbd_release_minors(unsigned int minor, unsigned int nr)
331{
332 unsigned int end = minor + nr;
333
334 BUG_ON(end > nr_minors);
335 spin_lock(&minor_lock);
Akinobu Mita34ae2e42012-01-21 00:15:26 +0900336 bitmap_clear(minors, minor, nr);
Jan Beulich0e345822010-08-07 18:28:55 +0200337 spin_unlock(&minor_lock);
338}
339
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700340static void blkif_restart_queue_callback(void *arg)
341{
342 struct blkfront_info *info = (struct blkfront_info *)arg;
343 schedule_work(&info->work);
344}
345
Harvey Harrisonafe42d72008-04-29 00:59:47 -0700346static int blkif_getgeo(struct block_device *bd, struct hd_geometry *hg)
Ian Campbell597592d2008-02-21 13:03:45 -0800347{
348 /* We don't have real geometry info, but let's at least return
349 values consistent with the size of the device */
350 sector_t nsect = get_capacity(bd->bd_disk);
351 sector_t cylinders = nsect;
352
353 hg->heads = 0xff;
354 hg->sectors = 0x3f;
355 sector_div(cylinders, hg->heads * hg->sectors);
356 hg->cylinders = cylinders;
357 if ((sector_t)(hg->cylinders + 1) * hg->heads * hg->sectors < nsect)
358 hg->cylinders = 0xffff;
359 return 0;
360}
361
Al Viroa63c8482008-03-02 10:23:47 -0500362static int blkif_ioctl(struct block_device *bdev, fmode_t mode,
Adrian Bunk62aa0052008-08-04 11:59:05 +0200363 unsigned command, unsigned long argument)
Christian Limpach440a01a2008-06-17 10:47:08 +0200364{
Al Viroa63c8482008-03-02 10:23:47 -0500365 struct blkfront_info *info = bdev->bd_disk->private_data;
Christian Limpach440a01a2008-06-17 10:47:08 +0200366 int i;
367
368 dev_dbg(&info->xbdev->dev, "command: 0x%x, argument: 0x%lx\n",
369 command, (long)argument);
370
371 switch (command) {
372 case CDROMMULTISESSION:
373 dev_dbg(&info->xbdev->dev, "FIXME: support multisession CDs later\n");
374 for (i = 0; i < sizeof(struct cdrom_multisession); i++)
375 if (put_user(0, (char __user *)(argument + i)))
376 return -EFAULT;
377 return 0;
378
379 case CDROM_GET_CAPABILITY: {
380 struct gendisk *gd = info->gd;
381 if (gd->flags & GENHD_FL_CD)
382 return 0;
383 return -EINVAL;
384 }
385
386 default:
387 /*printk(KERN_ALERT "ioctl %08x not supported by Xen blkdev\n",
388 command);*/
389 return -EINVAL; /* same return as native Linux */
390 }
391
392 return 0;
393}
394
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700395/*
Jeremy Fitzhardingec64e38e2010-11-01 14:32:27 -0400396 * Generate a Xen blkfront IO request from a blk layer request. Reads
Konrad Rzeszutek Wilkedf6ef52011-05-03 12:01:11 -0400397 * and writes are handled as expected.
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700398 *
Jeremy Fitzhardingec64e38e2010-11-01 14:32:27 -0400399 * @req: a request struct
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700400 */
401static int blkif_queue_request(struct request *req)
402{
403 struct blkfront_info *info = req->rq_disk->private_data;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700404 struct blkif_request *ring_req;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700405 unsigned long id;
406 unsigned int fsect, lsect;
Roger Pau Monne402b27f2013-04-18 16:06:54 +0200407 int i, ref, n;
Roger Pau Monne80bfa2f2014-02-04 11:26:15 +0100408 struct blkif_request_segment *segments = NULL;
Roger Pau Monne0a8704a2012-10-24 18:58:45 +0200409
410 /*
411 * Used to store if we are able to queue the request by just using
412 * existing persistent grants, or if we have to get new grants,
413 * as there are not sufficiently many free.
414 */
415 bool new_persistent_gnts;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700416 grant_ref_t gref_head;
Roger Pau Monne0a8704a2012-10-24 18:58:45 +0200417 struct grant *gnt_list_entry = NULL;
Jens Axboe9e973e62009-02-24 08:10:09 +0100418 struct scatterlist *sg;
Roger Pau Monne402b27f2013-04-18 16:06:54 +0200419 int nseg, max_grefs;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700420
421 if (unlikely(info->connected != BLKIF_STATE_CONNECTED))
422 return 1;
423
Roger Pau Monnec47206e2013-08-12 12:53:43 +0200424 max_grefs = req->nr_phys_segments;
425 if (max_grefs > BLKIF_MAX_SEGMENTS_PER_REQUEST)
426 /*
427 * If we are using indirect segments we need to account
428 * for the indirect grefs used in the request.
429 */
430 max_grefs += INDIRECT_GREFS(req->nr_phys_segments);
Roger Pau Monne402b27f2013-04-18 16:06:54 +0200431
432 /* Check if we have enough grants to allocate a requests */
433 if (info->persistent_gnts_c < max_grefs) {
Roger Pau Monne0a8704a2012-10-24 18:58:45 +0200434 new_persistent_gnts = 1;
435 if (gnttab_alloc_grant_references(
Roger Pau Monne402b27f2013-04-18 16:06:54 +0200436 max_grefs - info->persistent_gnts_c,
Roger Pau Monne0a8704a2012-10-24 18:58:45 +0200437 &gref_head) < 0) {
438 gnttab_request_free_callback(
439 &info->callback,
440 blkif_restart_queue_callback,
441 info,
Roger Pau Monne402b27f2013-04-18 16:06:54 +0200442 max_grefs);
Roger Pau Monne0a8704a2012-10-24 18:58:45 +0200443 return 1;
444 }
445 } else
446 new_persistent_gnts = 0;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700447
448 /* Fill out a communications ring structure. */
449 ring_req = RING_GET_REQUEST(&info->ring, info->ring.req_prod_pvt);
450 id = get_id_from_freelist(info);
Jeremy Fitzhardingea945b982010-11-01 17:03:14 -0400451 info->shadow[id].request = req;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700452
Konrad Rzeszutek Wilk5ea42982011-10-12 16:23:30 -0400453 if (unlikely(req->cmd_flags & (REQ_DISCARD | REQ_SECURE))) {
Li Dongyanged30bf32011-09-01 18:39:09 +0800454 ring_req->operation = BLKIF_OP_DISCARD;
Li Dongyanged30bf32011-09-01 18:39:09 +0800455 ring_req->u.discard.nr_sectors = blk_rq_sectors(req);
Roger Pau Monne402b27f2013-04-18 16:06:54 +0200456 ring_req->u.discard.id = id;
457 ring_req->u.discard.sector_number = (blkif_sector_t)blk_rq_pos(req);
Konrad Rzeszutek Wilk5ea42982011-10-12 16:23:30 -0400458 if ((req->cmd_flags & REQ_SECURE) && info->feature_secdiscard)
459 ring_req->u.discard.flag = BLKIF_DISCARD_SECURE;
460 else
461 ring_req->u.discard.flag = 0;
Li Dongyanged30bf32011-09-01 18:39:09 +0800462 } else {
Roger Pau Monne402b27f2013-04-18 16:06:54 +0200463 BUG_ON(info->max_indirect_segments == 0 &&
464 req->nr_phys_segments > BLKIF_MAX_SEGMENTS_PER_REQUEST);
465 BUG_ON(info->max_indirect_segments &&
466 req->nr_phys_segments > info->max_indirect_segments);
Roger Pau Monneb7649152013-05-02 10:58:50 +0200467 nseg = blk_rq_map_sg(req->q, req, info->shadow[id].sg);
Roger Pau Monne402b27f2013-04-18 16:06:54 +0200468 ring_req->u.rw.id = id;
469 if (nseg > BLKIF_MAX_SEGMENTS_PER_REQUEST) {
470 /*
471 * The indirect operation can only be a BLKIF_OP_READ or
472 * BLKIF_OP_WRITE
473 */
474 BUG_ON(req->cmd_flags & (REQ_FLUSH | REQ_FUA));
475 ring_req->operation = BLKIF_OP_INDIRECT;
476 ring_req->u.indirect.indirect_op = rq_data_dir(req) ?
477 BLKIF_OP_WRITE : BLKIF_OP_READ;
478 ring_req->u.indirect.sector_number = (blkif_sector_t)blk_rq_pos(req);
479 ring_req->u.indirect.handle = info->handle;
480 ring_req->u.indirect.nr_segments = nseg;
481 } else {
482 ring_req->u.rw.sector_number = (blkif_sector_t)blk_rq_pos(req);
483 ring_req->u.rw.handle = info->handle;
484 ring_req->operation = rq_data_dir(req) ?
485 BLKIF_OP_WRITE : BLKIF_OP_READ;
486 if (req->cmd_flags & (REQ_FLUSH | REQ_FUA)) {
487 /*
488 * Ideally we can do an unordered flush-to-disk. In case the
489 * backend onlysupports barriers, use that. A barrier request
490 * a superset of FUA, so we can implement it the same
491 * way. (It's also a FLUSH+FUA, since it is
492 * guaranteed ordered WRT previous writes.)
493 */
Vitaly Kuznetsovfdf9b962014-12-09 15:25:10 +0100494 switch (info->feature_flush &
495 ((REQ_FLUSH|REQ_FUA))) {
496 case REQ_FLUSH|REQ_FUA:
497 ring_req->operation =
498 BLKIF_OP_WRITE_BARRIER;
499 break;
500 case REQ_FLUSH:
501 ring_req->operation =
502 BLKIF_OP_FLUSH_DISKCACHE;
503 break;
504 default:
505 ring_req->operation = 0;
506 }
Roger Pau Monne402b27f2013-04-18 16:06:54 +0200507 }
508 ring_req->u.rw.nr_segments = nseg;
509 }
Roger Pau Monneb7649152013-05-02 10:58:50 +0200510 for_each_sg(info->shadow[id].sg, sg, nseg, i) {
Li Dongyanged30bf32011-09-01 18:39:09 +0800511 fsect = sg->offset >> 9;
512 lsect = fsect + (sg->length >> 9) - 1;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700513
Roger Pau Monne402b27f2013-04-18 16:06:54 +0200514 if ((ring_req->operation == BLKIF_OP_INDIRECT) &&
515 (i % SEGS_PER_INDIRECT_FRAME == 0)) {
Tim Gardner427bfe02013-11-14 14:29:52 -0700516 unsigned long uninitialized_var(pfn);
Roger Pau Monnebfe11d62013-10-29 18:31:14 +0100517
Roger Pau Monne402b27f2013-04-18 16:06:54 +0200518 if (segments)
519 kunmap_atomic(segments);
520
521 n = i / SEGS_PER_INDIRECT_FRAME;
Roger Pau Monnebfe11d62013-10-29 18:31:14 +0100522 if (!info->feature_persistent) {
523 struct page *indirect_page;
524
525 /* Fetch a pre-allocated page to use for indirect grefs */
526 BUG_ON(list_empty(&info->indirect_pages));
527 indirect_page = list_first_entry(&info->indirect_pages,
528 struct page, lru);
529 list_del(&indirect_page->lru);
530 pfn = page_to_pfn(indirect_page);
531 }
532 gnt_list_entry = get_grant(&gref_head, pfn, info);
Roger Pau Monne402b27f2013-04-18 16:06:54 +0200533 info->shadow[id].indirect_grants[n] = gnt_list_entry;
534 segments = kmap_atomic(pfn_to_page(gnt_list_entry->pfn));
535 ring_req->u.indirect.indirect_grefs[n] = gnt_list_entry->gref;
536 }
537
Roger Pau Monnebfe11d62013-10-29 18:31:14 +0100538 gnt_list_entry = get_grant(&gref_head, page_to_pfn(sg_page(sg)), info);
Roger Pau Monne9c1e0502013-03-18 17:49:35 +0100539 ref = gnt_list_entry->gref;
Roger Pau Monne0a8704a2012-10-24 18:58:45 +0200540
541 info->shadow[id].grants_used[i] = gnt_list_entry;
542
Roger Pau Monnebfe11d62013-10-29 18:31:14 +0100543 if (rq_data_dir(req) && info->feature_persistent) {
Roger Pau Monne0a8704a2012-10-24 18:58:45 +0200544 char *bvec_data;
545 void *shared_data;
546
547 BUG_ON(sg->offset + sg->length > PAGE_SIZE);
548
Roger Pau Monne402b27f2013-04-18 16:06:54 +0200549 shared_data = kmap_atomic(pfn_to_page(gnt_list_entry->pfn));
Roger Pau Monne0a8704a2012-10-24 18:58:45 +0200550 bvec_data = kmap_atomic(sg_page(sg));
551
552 /*
553 * this does not wipe data stored outside the
554 * range sg->offset..sg->offset+sg->length.
555 * Therefore, blkback *could* see data from
556 * previous requests. This is OK as long as
557 * persistent grants are shared with just one
558 * domain. It may need refactoring if this
559 * changes
560 */
561 memcpy(shared_data + sg->offset,
562 bvec_data + sg->offset,
563 sg->length);
564
565 kunmap_atomic(bvec_data);
566 kunmap_atomic(shared_data);
567 }
Roger Pau Monne402b27f2013-04-18 16:06:54 +0200568 if (ring_req->operation != BLKIF_OP_INDIRECT) {
569 ring_req->u.rw.seg[i] =
570 (struct blkif_request_segment) {
571 .gref = ref,
572 .first_sect = fsect,
573 .last_sect = lsect };
574 } else {
575 n = i % SEGS_PER_INDIRECT_FRAME;
576 segments[n] =
Roger Pau Monne80bfa2f2014-02-04 11:26:15 +0100577 (struct blkif_request_segment) {
Roger Pau Monne402b27f2013-04-18 16:06:54 +0200578 .gref = ref,
579 .first_sect = fsect,
580 .last_sect = lsect };
581 }
Li Dongyanged30bf32011-09-01 18:39:09 +0800582 }
Roger Pau Monne402b27f2013-04-18 16:06:54 +0200583 if (segments)
584 kunmap_atomic(segments);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700585 }
586
587 info->ring.req_prod_pvt++;
588
589 /* Keep a private copy so we can reissue requests when recovering. */
590 info->shadow[id].req = *ring_req;
591
Roger Pau Monne0a8704a2012-10-24 18:58:45 +0200592 if (new_persistent_gnts)
593 gnttab_free_grant_references(gref_head);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700594
595 return 0;
596}
597
598
599static inline void flush_requests(struct blkfront_info *info)
600{
601 int notify;
602
603 RING_PUSH_REQUESTS_AND_CHECK_NOTIFY(&info->ring, notify);
604
605 if (notify)
606 notify_remote_via_irq(info->irq);
607}
608
Vitaly Kuznetsovad42d392014-12-01 14:01:13 +0100609static inline bool blkif_request_flush_invalid(struct request *req,
610 struct blkfront_info *info)
Arianna Avanzini0f1ca652014-08-22 13:20:02 +0200611{
612 return ((req->cmd_type != REQ_TYPE_FS) ||
Vitaly Kuznetsovad42d392014-12-01 14:01:13 +0100613 ((req->cmd_flags & REQ_FLUSH) &&
614 !(info->feature_flush & REQ_FLUSH)) ||
615 ((req->cmd_flags & REQ_FUA) &&
616 !(info->feature_flush & REQ_FUA)));
Arianna Avanzini0f1ca652014-08-22 13:20:02 +0200617}
618
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700619/*
620 * do_blkif_request
621 * read a block; request is in a request queue
622 */
Jens Axboe165125e2007-07-24 09:28:11 +0200623static void do_blkif_request(struct request_queue *rq)
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700624{
625 struct blkfront_info *info = NULL;
626 struct request *req;
627 int queued;
628
629 pr_debug("Entered do_blkif_request\n");
630
631 queued = 0;
632
Tejun Heo9934c8c2009-05-08 11:54:16 +0900633 while ((req = blk_peek_request(rq)) != NULL) {
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700634 info = req->rq_disk->private_data;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700635
636 if (RING_FULL(&info->ring))
637 goto wait;
638
Tejun Heo9934c8c2009-05-08 11:54:16 +0900639 blk_start_request(req);
Tejun Heo296b2f62009-05-08 11:54:15 +0900640
Vitaly Kuznetsovad42d392014-12-01 14:01:13 +0100641 if (blkif_request_flush_invalid(req, info)) {
642 __blk_end_request_all(req, -EOPNOTSUPP);
Tejun Heo296b2f62009-05-08 11:54:15 +0900643 continue;
644 }
645
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700646 pr_debug("do_blk_req %p: cmd %p, sec %lx, "
Jens Axboeb4f42e22014-04-10 09:46:28 -0600647 "(%u/%u) [%s]\n",
Tejun Heo83096eb2009-05-07 22:24:39 +0900648 req, req->cmd, (unsigned long)blk_rq_pos(req),
649 blk_rq_cur_sectors(req), blk_rq_sectors(req),
Jens Axboeb4f42e22014-04-10 09:46:28 -0600650 rq_data_dir(req) ? "write" : "read");
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700651
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700652 if (blkif_queue_request(req)) {
653 blk_requeue_request(rq, req);
654wait:
655 /* Avoid pointless unplugs. */
656 blk_stop_queue(rq);
657 break;
658 }
659
660 queued++;
661 }
662
663 if (queued != 0)
664 flush_requests(info);
665}
666
Roger Pau Monne402b27f2013-04-18 16:06:54 +0200667static int xlvbd_init_blk_queue(struct gendisk *gd, u16 sector_size,
Stefan Bader7c4d7d72013-05-13 16:28:15 +0200668 unsigned int physical_sector_size,
Roger Pau Monne402b27f2013-04-18 16:06:54 +0200669 unsigned int segments)
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700670{
Jens Axboe165125e2007-07-24 09:28:11 +0200671 struct request_queue *rq;
Li Dongyanged30bf32011-09-01 18:39:09 +0800672 struct blkfront_info *info = gd->private_data;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700673
Steven Noonan34678112012-02-17 12:04:44 -0800674 rq = blk_init_queue(do_blkif_request, &info->io_lock);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700675 if (rq == NULL)
676 return -1;
677
Fernando Luis Vázquez Cao66d352e2008-10-27 18:45:54 +0900678 queue_flag_set_unlocked(QUEUE_FLAG_VIRT, rq);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700679
Li Dongyanged30bf32011-09-01 18:39:09 +0800680 if (info->feature_discard) {
681 queue_flag_set_unlocked(QUEUE_FLAG_DISCARD, rq);
682 blk_queue_max_discard_sectors(rq, get_capacity(gd));
683 rq->limits.discard_granularity = info->discard_granularity;
684 rq->limits.discard_alignment = info->discard_alignment;
Konrad Rzeszutek Wilk5ea42982011-10-12 16:23:30 -0400685 if (info->feature_secdiscard)
686 queue_flag_set_unlocked(QUEUE_FLAG_SECDISCARD, rq);
Li Dongyanged30bf32011-09-01 18:39:09 +0800687 }
688
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700689 /* Hard sector size and max sectors impersonate the equiv. hardware. */
Martin K. Petersene1defc42009-05-22 17:17:49 -0400690 blk_queue_logical_block_size(rq, sector_size);
Stefan Bader7c4d7d72013-05-13 16:28:15 +0200691 blk_queue_physical_block_size(rq, physical_sector_size);
Roger Pau Monne294caaf2013-06-21 12:56:54 +0200692 blk_queue_max_hw_sectors(rq, (segments * PAGE_SIZE) / 512);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700693
694 /* Each segment in a request is up to an aligned page in size. */
695 blk_queue_segment_boundary(rq, PAGE_SIZE - 1);
696 blk_queue_max_segment_size(rq, PAGE_SIZE);
697
698 /* Ensure a merged request will fit in a single I/O ring slot. */
Roger Pau Monne402b27f2013-04-18 16:06:54 +0200699 blk_queue_max_segments(rq, segments);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700700
701 /* Make sure buffer addresses are sector-aligned. */
702 blk_queue_dma_alignment(rq, 511);
703
Ian Campbell1c91fe12008-06-17 10:47:08 +0200704 /* Make sure we don't use bounce buffers. */
705 blk_queue_bounce_limit(rq, BLK_BOUNCE_ANY);
706
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700707 gd->queue = rq;
708
709 return 0;
710}
711
Vitaly Kuznetsovfdf9b962014-12-09 15:25:10 +0100712static const char *flush_info(unsigned int feature_flush)
713{
714 switch (feature_flush & ((REQ_FLUSH | REQ_FUA))) {
715 case REQ_FLUSH|REQ_FUA:
716 return "barrier: enabled;";
717 case REQ_FLUSH:
718 return "flush diskcache: enabled;";
719 default:
720 return "barrier or flush: disabled;";
721 }
722}
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700723
Tejun Heo4913efe2010-09-03 11:56:16 +0200724static void xlvbd_flush(struct blkfront_info *info)
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700725{
Tejun Heo4913efe2010-09-03 11:56:16 +0200726 blk_queue_flush(info->rq, info->feature_flush);
Vitaly Kuznetsovfdf9b962014-12-09 15:25:10 +0100727 pr_info("blkfront: %s: %s %s %s %s %s\n",
728 info->gd->disk_name, flush_info(info->feature_flush),
729 "persistent grants:", info->feature_persistent ?
730 "enabled;" : "disabled;", "indirect descriptors:",
731 info->max_indirect_segments ? "enabled;" : "disabled;");
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700732}
733
Stefano Stabellinic80a4202010-12-02 17:55:00 +0000734static int xen_translate_vdev(int vdevice, int *minor, unsigned int *offset)
735{
736 int major;
737 major = BLKIF_MAJOR(vdevice);
738 *minor = BLKIF_MINOR(vdevice);
739 switch (major) {
740 case XEN_IDE0_MAJOR:
741 *offset = (*minor / 64) + EMULATED_HD_DISK_NAME_OFFSET;
742 *minor = ((*minor / 64) * PARTS_PER_DISK) +
743 EMULATED_HD_DISK_MINOR_OFFSET;
744 break;
745 case XEN_IDE1_MAJOR:
746 *offset = (*minor / 64) + 2 + EMULATED_HD_DISK_NAME_OFFSET;
747 *minor = (((*minor / 64) + 2) * PARTS_PER_DISK) +
748 EMULATED_HD_DISK_MINOR_OFFSET;
749 break;
750 case XEN_SCSI_DISK0_MAJOR:
751 *offset = (*minor / PARTS_PER_DISK) + EMULATED_SD_DISK_NAME_OFFSET;
752 *minor = *minor + EMULATED_SD_DISK_MINOR_OFFSET;
753 break;
754 case XEN_SCSI_DISK1_MAJOR:
755 case XEN_SCSI_DISK2_MAJOR:
756 case XEN_SCSI_DISK3_MAJOR:
757 case XEN_SCSI_DISK4_MAJOR:
758 case XEN_SCSI_DISK5_MAJOR:
759 case XEN_SCSI_DISK6_MAJOR:
760 case XEN_SCSI_DISK7_MAJOR:
761 *offset = (*minor / PARTS_PER_DISK) +
762 ((major - XEN_SCSI_DISK1_MAJOR + 1) * 16) +
763 EMULATED_SD_DISK_NAME_OFFSET;
764 *minor = *minor +
765 ((major - XEN_SCSI_DISK1_MAJOR + 1) * 16 * PARTS_PER_DISK) +
766 EMULATED_SD_DISK_MINOR_OFFSET;
767 break;
768 case XEN_SCSI_DISK8_MAJOR:
769 case XEN_SCSI_DISK9_MAJOR:
770 case XEN_SCSI_DISK10_MAJOR:
771 case XEN_SCSI_DISK11_MAJOR:
772 case XEN_SCSI_DISK12_MAJOR:
773 case XEN_SCSI_DISK13_MAJOR:
774 case XEN_SCSI_DISK14_MAJOR:
775 case XEN_SCSI_DISK15_MAJOR:
776 *offset = (*minor / PARTS_PER_DISK) +
777 ((major - XEN_SCSI_DISK8_MAJOR + 8) * 16) +
778 EMULATED_SD_DISK_NAME_OFFSET;
779 *minor = *minor +
780 ((major - XEN_SCSI_DISK8_MAJOR + 8) * 16 * PARTS_PER_DISK) +
781 EMULATED_SD_DISK_MINOR_OFFSET;
782 break;
783 case XENVBD_MAJOR:
784 *offset = *minor / PARTS_PER_DISK;
785 break;
786 default:
787 printk(KERN_WARNING "blkfront: your disk configuration is "
788 "incorrect, please use an xvd device instead\n");
789 return -ENODEV;
790 }
791 return 0;
792}
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700793
Jan Beuliche77c78c2012-04-05 16:37:22 +0100794static char *encode_disk_name(char *ptr, unsigned int n)
795{
796 if (n >= 26)
797 ptr = encode_disk_name(ptr, n / 26 - 1);
798 *ptr = 'a' + n % 26;
799 return ptr + 1;
800}
801
Chris Lalancette9246b5f2008-09-17 14:30:32 -0700802static int xlvbd_alloc_gendisk(blkif_sector_t capacity,
803 struct blkfront_info *info,
Stefan Bader7c4d7d72013-05-13 16:28:15 +0200804 u16 vdisk_info, u16 sector_size,
805 unsigned int physical_sector_size)
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700806{
807 struct gendisk *gd;
808 int nr_minors = 1;
Stefano Stabellinic80a4202010-12-02 17:55:00 +0000809 int err;
Chris Lalancette9246b5f2008-09-17 14:30:32 -0700810 unsigned int offset;
811 int minor;
812 int nr_parts;
Jan Beuliche77c78c2012-04-05 16:37:22 +0100813 char *ptr;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700814
815 BUG_ON(info->gd != NULL);
816 BUG_ON(info->rq != NULL);
817
Chris Lalancette9246b5f2008-09-17 14:30:32 -0700818 if ((info->vdevice>>EXT_SHIFT) > 1) {
819 /* this is above the extended range; something is wrong */
820 printk(KERN_WARNING "blkfront: vdevice 0x%x is above the extended range; ignoring\n", info->vdevice);
821 return -ENODEV;
822 }
823
824 if (!VDEV_IS_EXTENDED(info->vdevice)) {
Stefano Stabellinic80a4202010-12-02 17:55:00 +0000825 err = xen_translate_vdev(info->vdevice, &minor, &offset);
826 if (err)
827 return err;
828 nr_parts = PARTS_PER_DISK;
Chris Lalancette9246b5f2008-09-17 14:30:32 -0700829 } else {
830 minor = BLKIF_MINOR_EXT(info->vdevice);
831 nr_parts = PARTS_PER_EXT_DISK;
Stefano Stabellinic80a4202010-12-02 17:55:00 +0000832 offset = minor / nr_parts;
Stefan Bader89153b52011-07-14 15:30:37 +0200833 if (xen_hvm_domain() && offset < EMULATED_HD_DISK_NAME_OFFSET + 4)
Stefano Stabellinic80a4202010-12-02 17:55:00 +0000834 printk(KERN_WARNING "blkfront: vdevice 0x%x might conflict with "
835 "emulated IDE disks,\n\t choose an xvd device name"
836 "from xvde on\n", info->vdevice);
Chris Lalancette9246b5f2008-09-17 14:30:32 -0700837 }
Jan Beuliche77c78c2012-04-05 16:37:22 +0100838 if (minor >> MINORBITS) {
839 pr_warn("blkfront: %#x's minor (%#x) out of range; ignoring\n",
840 info->vdevice, minor);
841 return -ENODEV;
842 }
Chris Lalancette9246b5f2008-09-17 14:30:32 -0700843
844 if ((minor % nr_parts) == 0)
845 nr_minors = nr_parts;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700846
Jan Beulich0e345822010-08-07 18:28:55 +0200847 err = xlbd_reserve_minors(minor, nr_minors);
848 if (err)
849 goto out;
850 err = -ENODEV;
851
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700852 gd = alloc_disk(nr_minors);
853 if (gd == NULL)
Jan Beulich0e345822010-08-07 18:28:55 +0200854 goto release;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700855
Jan Beuliche77c78c2012-04-05 16:37:22 +0100856 strcpy(gd->disk_name, DEV_NAME);
857 ptr = encode_disk_name(gd->disk_name + sizeof(DEV_NAME) - 1, offset);
858 BUG_ON(ptr >= gd->disk_name + DISK_NAME_LEN);
859 if (nr_minors > 1)
860 *ptr = 0;
861 else
862 snprintf(ptr, gd->disk_name + DISK_NAME_LEN - ptr,
863 "%d", minor & (nr_parts - 1));
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700864
865 gd->major = XENVBD_MAJOR;
866 gd->first_minor = minor;
867 gd->fops = &xlvbd_block_fops;
868 gd->private_data = info;
869 gd->driverfs_dev = &(info->xbdev->dev);
870 set_capacity(gd, capacity);
871
Stefan Bader7c4d7d72013-05-13 16:28:15 +0200872 if (xlvbd_init_blk_queue(gd, sector_size, physical_sector_size,
Roger Pau Monne402b27f2013-04-18 16:06:54 +0200873 info->max_indirect_segments ? :
874 BLKIF_MAX_SEGMENTS_PER_REQUEST)) {
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700875 del_gendisk(gd);
Jan Beulich0e345822010-08-07 18:28:55 +0200876 goto release;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700877 }
878
879 info->rq = gd->queue;
880 info->gd = gd;
881
Tejun Heo4913efe2010-09-03 11:56:16 +0200882 xlvbd_flush(info);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700883
884 if (vdisk_info & VDISK_READONLY)
885 set_disk_ro(gd, 1);
886
887 if (vdisk_info & VDISK_REMOVABLE)
888 gd->flags |= GENHD_FL_REMOVABLE;
889
890 if (vdisk_info & VDISK_CDROM)
891 gd->flags |= GENHD_FL_CD;
892
893 return 0;
894
Jan Beulich0e345822010-08-07 18:28:55 +0200895 release:
896 xlbd_release_minors(minor, nr_minors);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700897 out:
898 return err;
899}
900
Daniel Stoddena66b5ae2010-08-07 18:33:17 +0200901static void xlvbd_release_gendisk(struct blkfront_info *info)
902{
903 unsigned int minor, nr_minors;
904 unsigned long flags;
905
906 if (info->rq == NULL)
907 return;
908
Steven Noonan34678112012-02-17 12:04:44 -0800909 spin_lock_irqsave(&info->io_lock, flags);
Daniel Stoddena66b5ae2010-08-07 18:33:17 +0200910
911 /* No more blkif_request(). */
912 blk_stop_queue(info->rq);
913
914 /* No more gnttab callback work. */
915 gnttab_cancel_free_callback(&info->callback);
Steven Noonan34678112012-02-17 12:04:44 -0800916 spin_unlock_irqrestore(&info->io_lock, flags);
Daniel Stoddena66b5ae2010-08-07 18:33:17 +0200917
918 /* Flush gnttab callback work. Must be done with no locks held. */
Tejun Heo43829732012-08-20 14:51:24 -0700919 flush_work(&info->work);
Daniel Stoddena66b5ae2010-08-07 18:33:17 +0200920
921 del_gendisk(info->gd);
922
923 minor = info->gd->first_minor;
924 nr_minors = info->gd->minors;
925 xlbd_release_minors(minor, nr_minors);
926
927 blk_cleanup_queue(info->rq);
928 info->rq = NULL;
929
930 put_disk(info->gd);
931 info->gd = NULL;
932}
933
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700934static void kick_pending_request_queues(struct blkfront_info *info)
935{
936 if (!RING_FULL(&info->ring)) {
937 /* Re-enable calldowns. */
938 blk_start_queue(info->rq);
939 /* Kick things off immediately. */
940 do_blkif_request(info->rq);
941 }
942}
943
944static void blkif_restart_queue(struct work_struct *work)
945{
946 struct blkfront_info *info = container_of(work, struct blkfront_info, work);
947
Steven Noonan34678112012-02-17 12:04:44 -0800948 spin_lock_irq(&info->io_lock);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700949 if (info->connected == BLKIF_STATE_CONNECTED)
950 kick_pending_request_queues(info);
Steven Noonan34678112012-02-17 12:04:44 -0800951 spin_unlock_irq(&info->io_lock);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700952}
953
954static void blkif_free(struct blkfront_info *info, int suspend)
955{
Roger Pau Monne155b7ed2013-03-18 17:49:34 +0100956 struct grant *persistent_gnt;
957 struct grant *n;
Roger Pau Monne402b27f2013-04-18 16:06:54 +0200958 int i, j, segs;
Roger Pau Monne0a8704a2012-10-24 18:58:45 +0200959
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700960 /* Prevent new requests being issued until we fix things up. */
Steven Noonan34678112012-02-17 12:04:44 -0800961 spin_lock_irq(&info->io_lock);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700962 info->connected = suspend ?
963 BLKIF_STATE_SUSPENDED : BLKIF_STATE_DISCONNECTED;
964 /* No more blkif_request(). */
965 if (info->rq)
966 blk_stop_queue(info->rq);
Roger Pau Monne0a8704a2012-10-24 18:58:45 +0200967
968 /* Remove all persistent grants */
Roger Pau Monnebfe11d62013-10-29 18:31:14 +0100969 if (!list_empty(&info->grants)) {
Roger Pau Monne155b7ed2013-03-18 17:49:34 +0100970 list_for_each_entry_safe(persistent_gnt, n,
Roger Pau Monnebfe11d62013-10-29 18:31:14 +0100971 &info->grants, node) {
Roger Pau Monne155b7ed2013-03-18 17:49:34 +0100972 list_del(&persistent_gnt->node);
Roger Pau Monne9c1e0502013-03-18 17:49:35 +0100973 if (persistent_gnt->gref != GRANT_INVALID_REF) {
974 gnttab_end_foreign_access(persistent_gnt->gref,
975 0, 0UL);
976 info->persistent_gnts_c--;
977 }
Roger Pau Monnebfe11d62013-10-29 18:31:14 +0100978 if (info->feature_persistent)
979 __free_page(pfn_to_page(persistent_gnt->pfn));
Roger Pau Monne155b7ed2013-03-18 17:49:34 +0100980 kfree(persistent_gnt);
Roger Pau Monne0a8704a2012-10-24 18:58:45 +0200981 }
Roger Pau Monne0a8704a2012-10-24 18:58:45 +0200982 }
Roger Pau Monne9c1e0502013-03-18 17:49:35 +0100983 BUG_ON(info->persistent_gnts_c != 0);
Roger Pau Monne0a8704a2012-10-24 18:58:45 +0200984
Roger Pau Monnebfe11d62013-10-29 18:31:14 +0100985 /*
986 * Remove indirect pages, this only happens when using indirect
987 * descriptors but not persistent grants
988 */
989 if (!list_empty(&info->indirect_pages)) {
990 struct page *indirect_page, *n;
991
992 BUG_ON(info->feature_persistent);
993 list_for_each_entry_safe(indirect_page, n, &info->indirect_pages, lru) {
994 list_del(&indirect_page->lru);
995 __free_page(indirect_page);
996 }
997 }
998
Bob Liu86839c52015-06-03 13:40:03 +0800999 for (i = 0; i < BLK_RING_SIZE(info); i++) {
Roger Pau Monne402b27f2013-04-18 16:06:54 +02001000 /*
1001 * Clear persistent grants present in requests already
1002 * on the shared ring
1003 */
1004 if (!info->shadow[i].request)
1005 goto free_shadow;
1006
1007 segs = info->shadow[i].req.operation == BLKIF_OP_INDIRECT ?
1008 info->shadow[i].req.u.indirect.nr_segments :
1009 info->shadow[i].req.u.rw.nr_segments;
1010 for (j = 0; j < segs; j++) {
1011 persistent_gnt = info->shadow[i].grants_used[j];
1012 gnttab_end_foreign_access(persistent_gnt->gref, 0, 0UL);
Roger Pau Monnebfe11d62013-10-29 18:31:14 +01001013 if (info->feature_persistent)
1014 __free_page(pfn_to_page(persistent_gnt->pfn));
Roger Pau Monne402b27f2013-04-18 16:06:54 +02001015 kfree(persistent_gnt);
1016 }
1017
1018 if (info->shadow[i].req.operation != BLKIF_OP_INDIRECT)
1019 /*
1020 * If this is not an indirect operation don't try to
1021 * free indirect segments
1022 */
1023 goto free_shadow;
1024
1025 for (j = 0; j < INDIRECT_GREFS(segs); j++) {
1026 persistent_gnt = info->shadow[i].indirect_grants[j];
1027 gnttab_end_foreign_access(persistent_gnt->gref, 0, 0UL);
1028 __free_page(pfn_to_page(persistent_gnt->pfn));
1029 kfree(persistent_gnt);
1030 }
1031
1032free_shadow:
1033 kfree(info->shadow[i].grants_used);
1034 info->shadow[i].grants_used = NULL;
1035 kfree(info->shadow[i].indirect_grants);
1036 info->shadow[i].indirect_grants = NULL;
Roger Pau Monneb7649152013-05-02 10:58:50 +02001037 kfree(info->shadow[i].sg);
1038 info->shadow[i].sg = NULL;
Roger Pau Monne402b27f2013-04-18 16:06:54 +02001039 }
1040
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001041 /* No more gnttab callback work. */
1042 gnttab_cancel_free_callback(&info->callback);
Steven Noonan34678112012-02-17 12:04:44 -08001043 spin_unlock_irq(&info->io_lock);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001044
1045 /* Flush gnttab callback work. Must be done with no locks held. */
Tejun Heo43829732012-08-20 14:51:24 -07001046 flush_work(&info->work);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001047
1048 /* Free resources associated with old device channel. */
Bob Liu86839c52015-06-03 13:40:03 +08001049 for (i = 0; i < info->nr_ring_pages; i++) {
1050 if (info->ring_ref[i] != GRANT_INVALID_REF) {
1051 gnttab_end_foreign_access(info->ring_ref[i], 0, 0);
1052 info->ring_ref[i] = GRANT_INVALID_REF;
1053 }
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001054 }
Bob Liu86839c52015-06-03 13:40:03 +08001055 free_pages((unsigned long)info->ring.sring, get_order(info->nr_ring_pages * PAGE_SIZE));
1056 info->ring.sring = NULL;
1057
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001058 if (info->irq)
1059 unbind_from_irqhandler(info->irq, info);
1060 info->evtchn = info->irq = 0;
1061
1062}
1063
Roger Pau Monne0a8704a2012-10-24 18:58:45 +02001064static void blkif_completion(struct blk_shadow *s, struct blkfront_info *info,
1065 struct blkif_response *bret)
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001066{
Roger Pau Monned62f6912012-12-07 19:00:31 +01001067 int i = 0;
Roger Pau Monneb7649152013-05-02 10:58:50 +02001068 struct scatterlist *sg;
Roger Pau Monne0a8704a2012-10-24 18:58:45 +02001069 char *bvec_data;
1070 void *shared_data;
Roger Pau Monne402b27f2013-04-18 16:06:54 +02001071 int nseg;
1072
1073 nseg = s->req.operation == BLKIF_OP_INDIRECT ?
1074 s->req.u.indirect.nr_segments : s->req.u.rw.nr_segments;
Roger Pau Monne0a8704a2012-10-24 18:58:45 +02001075
Roger Pau Monnebfe11d62013-10-29 18:31:14 +01001076 if (bret->operation == BLKIF_OP_READ && info->feature_persistent) {
Roger Pau Monne0a8704a2012-10-24 18:58:45 +02001077 /*
1078 * Copy the data received from the backend into the bvec.
1079 * Since bv_offset can be different than 0, and bv_len different
1080 * than PAGE_SIZE, we have to keep track of the current offset,
1081 * to be sure we are copying the data from the right shared page.
1082 */
Roger Pau Monneb7649152013-05-02 10:58:50 +02001083 for_each_sg(s->sg, sg, nseg, i) {
1084 BUG_ON(sg->offset + sg->length > PAGE_SIZE);
Roger Pau Monne0a8704a2012-10-24 18:58:45 +02001085 shared_data = kmap_atomic(
1086 pfn_to_page(s->grants_used[i]->pfn));
Roger Pau Monneb7649152013-05-02 10:58:50 +02001087 bvec_data = kmap_atomic(sg_page(sg));
1088 memcpy(bvec_data + sg->offset,
1089 shared_data + sg->offset,
1090 sg->length);
1091 kunmap_atomic(bvec_data);
Roger Pau Monne0a8704a2012-10-24 18:58:45 +02001092 kunmap_atomic(shared_data);
Roger Pau Monne0a8704a2012-10-24 18:58:45 +02001093 }
1094 }
1095 /* Add the persistent grant into the list of free grants */
Roger Pau Monne402b27f2013-04-18 16:06:54 +02001096 for (i = 0; i < nseg; i++) {
Roger Pau Monnefbe363c2013-08-12 12:53:44 +02001097 if (gnttab_query_foreign_access(s->grants_used[i]->gref)) {
1098 /*
1099 * If the grant is still mapped by the backend (the
1100 * backend has chosen to make this grant persistent)
1101 * we add it at the head of the list, so it will be
1102 * reused first.
1103 */
Roger Pau Monnebfe11d62013-10-29 18:31:14 +01001104 if (!info->feature_persistent)
1105 pr_alert_ratelimited("backed has not unmapped grant: %u\n",
1106 s->grants_used[i]->gref);
1107 list_add(&s->grants_used[i]->node, &info->grants);
Roger Pau Monnefbe363c2013-08-12 12:53:44 +02001108 info->persistent_gnts_c++;
1109 } else {
1110 /*
1111 * If the grant is not mapped by the backend we end the
1112 * foreign access and add it to the tail of the list,
1113 * so it will not be picked again unless we run out of
1114 * persistent grants.
1115 */
1116 gnttab_end_foreign_access(s->grants_used[i]->gref, 0, 0UL);
1117 s->grants_used[i]->gref = GRANT_INVALID_REF;
Roger Pau Monnebfe11d62013-10-29 18:31:14 +01001118 list_add_tail(&s->grants_used[i]->node, &info->grants);
Roger Pau Monnefbe363c2013-08-12 12:53:44 +02001119 }
Roger Pau Monne0a8704a2012-10-24 18:58:45 +02001120 }
Roger Pau Monne402b27f2013-04-18 16:06:54 +02001121 if (s->req.operation == BLKIF_OP_INDIRECT) {
1122 for (i = 0; i < INDIRECT_GREFS(nseg); i++) {
Roger Pau Monnefbe363c2013-08-12 12:53:44 +02001123 if (gnttab_query_foreign_access(s->indirect_grants[i]->gref)) {
Roger Pau Monnebfe11d62013-10-29 18:31:14 +01001124 if (!info->feature_persistent)
1125 pr_alert_ratelimited("backed has not unmapped grant: %u\n",
1126 s->indirect_grants[i]->gref);
1127 list_add(&s->indirect_grants[i]->node, &info->grants);
Roger Pau Monnefbe363c2013-08-12 12:53:44 +02001128 info->persistent_gnts_c++;
1129 } else {
Roger Pau Monnebfe11d62013-10-29 18:31:14 +01001130 struct page *indirect_page;
1131
Roger Pau Monnefbe363c2013-08-12 12:53:44 +02001132 gnttab_end_foreign_access(s->indirect_grants[i]->gref, 0, 0UL);
Roger Pau Monnebfe11d62013-10-29 18:31:14 +01001133 /*
1134 * Add the used indirect page back to the list of
1135 * available pages for indirect grefs.
1136 */
1137 indirect_page = pfn_to_page(s->indirect_grants[i]->pfn);
1138 list_add(&indirect_page->lru, &info->indirect_pages);
Roger Pau Monnefbe363c2013-08-12 12:53:44 +02001139 s->indirect_grants[i]->gref = GRANT_INVALID_REF;
Roger Pau Monnebfe11d62013-10-29 18:31:14 +01001140 list_add_tail(&s->indirect_grants[i]->node, &info->grants);
Roger Pau Monnefbe363c2013-08-12 12:53:44 +02001141 }
Roger Pau Monne402b27f2013-04-18 16:06:54 +02001142 }
1143 }
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001144}
1145
1146static irqreturn_t blkif_interrupt(int irq, void *dev_id)
1147{
1148 struct request *req;
1149 struct blkif_response *bret;
1150 RING_IDX i, rp;
1151 unsigned long flags;
1152 struct blkfront_info *info = (struct blkfront_info *)dev_id;
Kiyoshi Uedaf530f0362007-12-11 17:47:36 -05001153 int error;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001154
Steven Noonan34678112012-02-17 12:04:44 -08001155 spin_lock_irqsave(&info->io_lock, flags);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001156
1157 if (unlikely(info->connected != BLKIF_STATE_CONNECTED)) {
Steven Noonan34678112012-02-17 12:04:44 -08001158 spin_unlock_irqrestore(&info->io_lock, flags);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001159 return IRQ_HANDLED;
1160 }
1161
1162 again:
1163 rp = info->ring.sring->rsp_prod;
1164 rmb(); /* Ensure we see queued responses up to 'rp'. */
1165
1166 for (i = info->ring.rsp_cons; i != rp; i++) {
1167 unsigned long id;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001168
1169 bret = RING_GET_RESPONSE(&info->ring, i);
1170 id = bret->id;
Konrad Rzeszutek Wilk6878c322012-05-25 17:34:51 -04001171 /*
1172 * The backend has messed up and given us an id that we would
1173 * never have given to it (we stamp it up to BLK_RING_SIZE -
1174 * look in get_id_from_freelist.
1175 */
Bob Liu86839c52015-06-03 13:40:03 +08001176 if (id >= BLK_RING_SIZE(info)) {
Konrad Rzeszutek Wilk6878c322012-05-25 17:34:51 -04001177 WARN(1, "%s: response to %s has incorrect id (%ld)\n",
1178 info->gd->disk_name, op_name(bret->operation), id);
1179 /* We can't safely get the 'struct request' as
1180 * the id is busted. */
1181 continue;
1182 }
Jeremy Fitzhardingea945b982010-11-01 17:03:14 -04001183 req = info->shadow[id].request;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001184
Konrad Rzeszutek Wilk5ea42982011-10-12 16:23:30 -04001185 if (bret->operation != BLKIF_OP_DISCARD)
Roger Pau Monne0a8704a2012-10-24 18:58:45 +02001186 blkif_completion(&info->shadow[id], info, bret);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001187
Konrad Rzeszutek Wilk6878c322012-05-25 17:34:51 -04001188 if (add_id_to_freelist(info, id)) {
1189 WARN(1, "%s: response to %s (id %ld) couldn't be recycled!\n",
1190 info->gd->disk_name, op_name(bret->operation), id);
1191 continue;
1192 }
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001193
Kiyoshi Uedaf530f0362007-12-11 17:47:36 -05001194 error = (bret->status == BLKIF_RSP_OKAY) ? 0 : -EIO;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001195 switch (bret->operation) {
Li Dongyanged30bf32011-09-01 18:39:09 +08001196 case BLKIF_OP_DISCARD:
1197 if (unlikely(bret->status == BLKIF_RSP_EOPNOTSUPP)) {
1198 struct request_queue *rq = info->rq;
Konrad Rzeszutek Wilk6878c322012-05-25 17:34:51 -04001199 printk(KERN_WARNING "blkfront: %s: %s op failed\n",
1200 info->gd->disk_name, op_name(bret->operation));
Li Dongyanged30bf32011-09-01 18:39:09 +08001201 error = -EOPNOTSUPP;
1202 info->feature_discard = 0;
Konrad Rzeszutek Wilk5ea42982011-10-12 16:23:30 -04001203 info->feature_secdiscard = 0;
Li Dongyanged30bf32011-09-01 18:39:09 +08001204 queue_flag_clear(QUEUE_FLAG_DISCARD, rq);
Konrad Rzeszutek Wilk5ea42982011-10-12 16:23:30 -04001205 queue_flag_clear(QUEUE_FLAG_SECDISCARD, rq);
Li Dongyanged30bf32011-09-01 18:39:09 +08001206 }
1207 __blk_end_request_all(req, error);
1208 break;
Konrad Rzeszutek Wilkedf6ef52011-05-03 12:01:11 -04001209 case BLKIF_OP_FLUSH_DISKCACHE:
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001210 case BLKIF_OP_WRITE_BARRIER:
1211 if (unlikely(bret->status == BLKIF_RSP_EOPNOTSUPP)) {
Konrad Rzeszutek Wilk6878c322012-05-25 17:34:51 -04001212 printk(KERN_WARNING "blkfront: %s: %s op failed\n",
1213 info->gd->disk_name, op_name(bret->operation));
Kiyoshi Uedaf530f0362007-12-11 17:47:36 -05001214 error = -EOPNOTSUPP;
Jeremy Fitzhardingedcb8bae2010-11-02 11:55:58 -04001215 }
1216 if (unlikely(bret->status == BLKIF_RSP_ERROR &&
Konrad Rzeszutek Wilk97e36832011-10-12 12:12:36 -04001217 info->shadow[id].req.u.rw.nr_segments == 0)) {
Konrad Rzeszutek Wilk6878c322012-05-25 17:34:51 -04001218 printk(KERN_WARNING "blkfront: %s: empty %s op failed\n",
1219 info->gd->disk_name, op_name(bret->operation));
Jeremy Fitzhardingedcb8bae2010-11-02 11:55:58 -04001220 error = -EOPNOTSUPP;
1221 }
1222 if (unlikely(error)) {
1223 if (error == -EOPNOTSUPP)
1224 error = 0;
Tejun Heo4913efe2010-09-03 11:56:16 +02001225 info->feature_flush = 0;
1226 xlvbd_flush(info);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001227 }
1228 /* fall through */
1229 case BLKIF_OP_READ:
1230 case BLKIF_OP_WRITE:
1231 if (unlikely(bret->status != BLKIF_RSP_OKAY))
1232 dev_dbg(&info->xbdev->dev, "Bad return from blkdev data "
1233 "request: %x\n", bret->status);
1234
Tejun Heo40cbbb72009-04-23 11:05:19 +09001235 __blk_end_request_all(req, error);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001236 break;
1237 default:
1238 BUG();
1239 }
1240 }
1241
1242 info->ring.rsp_cons = i;
1243
1244 if (i != info->ring.req_prod_pvt) {
1245 int more_to_do;
1246 RING_FINAL_CHECK_FOR_RESPONSES(&info->ring, more_to_do);
1247 if (more_to_do)
1248 goto again;
1249 } else
1250 info->ring.sring->rsp_event = i + 1;
1251
1252 kick_pending_request_queues(info);
1253
Steven Noonan34678112012-02-17 12:04:44 -08001254 spin_unlock_irqrestore(&info->io_lock, flags);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001255
1256 return IRQ_HANDLED;
1257}
1258
1259
1260static int setup_blkring(struct xenbus_device *dev,
1261 struct blkfront_info *info)
1262{
1263 struct blkif_sring *sring;
Bob Liu86839c52015-06-03 13:40:03 +08001264 int err, i;
1265 unsigned long ring_size = info->nr_ring_pages * PAGE_SIZE;
1266 grant_ref_t gref[XENBUS_MAX_RING_PAGES];
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001267
Bob Liu86839c52015-06-03 13:40:03 +08001268 for (i = 0; i < info->nr_ring_pages; i++)
1269 info->ring_ref[i] = GRANT_INVALID_REF;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001270
Bob Liu86839c52015-06-03 13:40:03 +08001271 sring = (struct blkif_sring *)__get_free_pages(GFP_NOIO | __GFP_HIGH,
1272 get_order(ring_size));
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001273 if (!sring) {
1274 xenbus_dev_fatal(dev, -ENOMEM, "allocating shared ring");
1275 return -ENOMEM;
1276 }
1277 SHARED_RING_INIT(sring);
Bob Liu86839c52015-06-03 13:40:03 +08001278 FRONT_RING_INIT(&info->ring, sring, ring_size);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001279
Bob Liu86839c52015-06-03 13:40:03 +08001280 err = xenbus_grant_ring(dev, info->ring.sring, info->nr_ring_pages, gref);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001281 if (err < 0) {
Bob Liu86839c52015-06-03 13:40:03 +08001282 free_pages((unsigned long)sring, get_order(ring_size));
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001283 info->ring.sring = NULL;
1284 goto fail;
1285 }
Bob Liu86839c52015-06-03 13:40:03 +08001286 for (i = 0; i < info->nr_ring_pages; i++)
1287 info->ring_ref[i] = gref[i];
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001288
1289 err = xenbus_alloc_evtchn(dev, &info->evtchn);
1290 if (err)
1291 goto fail;
1292
Theodore Ts'o89c30f12012-07-17 13:46:19 -04001293 err = bind_evtchn_to_irqhandler(info->evtchn, blkif_interrupt, 0,
1294 "blkif", info);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001295 if (err <= 0) {
1296 xenbus_dev_fatal(dev, err,
1297 "bind_evtchn_to_irqhandler failed");
1298 goto fail;
1299 }
1300 info->irq = err;
1301
1302 return 0;
1303fail:
1304 blkif_free(info, 0);
1305 return err;
1306}
1307
1308
1309/* Common code used when first setting up, and when resuming. */
Ian Campbell203fd612009-12-04 15:33:54 +00001310static int talk_to_blkback(struct xenbus_device *dev,
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001311 struct blkfront_info *info)
1312{
1313 const char *message = NULL;
1314 struct xenbus_transaction xbt;
Bob Liu86839c52015-06-03 13:40:03 +08001315 int err, i;
1316 unsigned int max_page_order = 0;
1317 unsigned int ring_page_order = 0;
1318
1319 err = xenbus_scanf(XBT_NIL, info->xbdev->otherend,
1320 "max-ring-page-order", "%u", &max_page_order);
1321 if (err != 1)
1322 info->nr_ring_pages = 1;
1323 else {
1324 ring_page_order = min(xen_blkif_max_ring_order, max_page_order);
1325 info->nr_ring_pages = 1 << ring_page_order;
1326 }
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001327
1328 /* Create shared ring, alloc event channel. */
1329 err = setup_blkring(dev, info);
1330 if (err)
1331 goto out;
1332
1333again:
1334 err = xenbus_transaction_start(&xbt);
1335 if (err) {
1336 xenbus_dev_fatal(dev, err, "starting transaction");
1337 goto destroy_blkring;
1338 }
1339
Bob Liu86839c52015-06-03 13:40:03 +08001340 if (info->nr_ring_pages == 1) {
1341 err = xenbus_printf(xbt, dev->nodename,
1342 "ring-ref", "%u", info->ring_ref[0]);
1343 if (err) {
1344 message = "writing ring-ref";
1345 goto abort_transaction;
1346 }
1347 } else {
1348 err = xenbus_printf(xbt, dev->nodename,
1349 "ring-page-order", "%u", ring_page_order);
1350 if (err) {
1351 message = "writing ring-page-order";
1352 goto abort_transaction;
1353 }
1354
1355 for (i = 0; i < info->nr_ring_pages; i++) {
1356 char ring_ref_name[RINGREF_NAME_LEN];
1357
1358 snprintf(ring_ref_name, RINGREF_NAME_LEN, "ring-ref%u", i);
1359 err = xenbus_printf(xbt, dev->nodename, ring_ref_name,
1360 "%u", info->ring_ref[i]);
1361 if (err) {
1362 message = "writing ring-ref";
1363 goto abort_transaction;
1364 }
1365 }
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001366 }
1367 err = xenbus_printf(xbt, dev->nodename,
1368 "event-channel", "%u", info->evtchn);
1369 if (err) {
1370 message = "writing event-channel";
1371 goto abort_transaction;
1372 }
Markus Armbruster3e334232008-04-02 10:54:02 -07001373 err = xenbus_printf(xbt, dev->nodename, "protocol", "%s",
1374 XEN_IO_PROTO_ABI_NATIVE);
1375 if (err) {
1376 message = "writing protocol";
1377 goto abort_transaction;
1378 }
Roger Pau Monne0a8704a2012-10-24 18:58:45 +02001379 err = xenbus_printf(xbt, dev->nodename,
Roger Pau Monnecb5bd4d2012-11-02 16:43:04 +01001380 "feature-persistent", "%u", 1);
Roger Pau Monne0a8704a2012-10-24 18:58:45 +02001381 if (err)
1382 dev_warn(&dev->dev,
1383 "writing persistent grants feature to xenbus");
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001384
1385 err = xenbus_transaction_end(xbt, 0);
1386 if (err) {
1387 if (err == -EAGAIN)
1388 goto again;
1389 xenbus_dev_fatal(dev, err, "completing transaction");
1390 goto destroy_blkring;
1391 }
1392
Bob Liu86839c52015-06-03 13:40:03 +08001393 for (i = 0; i < BLK_RING_SIZE(info); i++)
1394 info->shadow[i].req.u.rw.id = i+1;
1395 info->shadow[BLK_RING_SIZE(info)-1].req.u.rw.id = 0x0fffffff;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001396 xenbus_switch_state(dev, XenbusStateInitialised);
1397
1398 return 0;
1399
1400 abort_transaction:
1401 xenbus_transaction_end(xbt, 1);
1402 if (message)
1403 xenbus_dev_fatal(dev, err, "%s", message);
1404 destroy_blkring:
1405 blkif_free(info, 0);
1406 out:
1407 return err;
1408}
1409
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001410/**
1411 * Entry point to this code when a new device is created. Allocate the basic
1412 * structures and the ring buffer for communication with the backend, and
1413 * inform the backend of the appropriate details for those. Switch to
1414 * Initialised state.
1415 */
1416static int blkfront_probe(struct xenbus_device *dev,
1417 const struct xenbus_device_id *id)
1418{
Bob Liu86839c52015-06-03 13:40:03 +08001419 int err, vdevice;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001420 struct blkfront_info *info;
1421
1422 /* FIXME: Use dynamic device id if this is not set. */
1423 err = xenbus_scanf(XBT_NIL, dev->nodename,
1424 "virtual-device", "%i", &vdevice);
1425 if (err != 1) {
Chris Lalancette9246b5f2008-09-17 14:30:32 -07001426 /* go looking in the extended area instead */
1427 err = xenbus_scanf(XBT_NIL, dev->nodename, "virtual-device-ext",
1428 "%i", &vdevice);
1429 if (err != 1) {
1430 xenbus_dev_fatal(dev, err, "reading virtual-device");
1431 return err;
1432 }
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001433 }
1434
Stefano Stabellinib98a4092010-07-29 14:53:16 +01001435 if (xen_hvm_domain()) {
1436 char *type;
1437 int len;
1438 /* no unplug has been done: do not hook devices != xen vbds */
Konrad Rzeszutek Wilk51c71a32013-11-26 15:05:40 -05001439 if (xen_has_pv_and_legacy_disk_devices()) {
Stefano Stabellinib98a4092010-07-29 14:53:16 +01001440 int major;
Stefano Stabellinic1c54132010-05-14 12:44:30 +01001441
Stefano Stabellinib98a4092010-07-29 14:53:16 +01001442 if (!VDEV_IS_EXTENDED(vdevice))
1443 major = BLKIF_MAJOR(vdevice);
1444 else
1445 major = XENVBD_MAJOR;
Stefano Stabellinic1c54132010-05-14 12:44:30 +01001446
Stefano Stabellinib98a4092010-07-29 14:53:16 +01001447 if (major != XENVBD_MAJOR) {
1448 printk(KERN_INFO
1449 "%s: HVM does not support vbd %d as xen block device\n",
Rasmus Villemoes02f1f212015-02-12 15:01:31 -08001450 __func__, vdevice);
Stefano Stabellinib98a4092010-07-29 14:53:16 +01001451 return -ENODEV;
1452 }
1453 }
1454 /* do not create a PV cdrom device if we are an HVM guest */
1455 type = xenbus_read(XBT_NIL, dev->nodename, "device-type", &len);
1456 if (IS_ERR(type))
1457 return -ENODEV;
1458 if (strncmp(type, "cdrom", 5) == 0) {
1459 kfree(type);
Stefano Stabellinic1c54132010-05-14 12:44:30 +01001460 return -ENODEV;
1461 }
Stefano Stabellinib98a4092010-07-29 14:53:16 +01001462 kfree(type);
Stefano Stabellinic1c54132010-05-14 12:44:30 +01001463 }
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001464 info = kzalloc(sizeof(*info), GFP_KERNEL);
1465 if (!info) {
1466 xenbus_dev_fatal(dev, -ENOMEM, "allocating info structure");
1467 return -ENOMEM;
1468 }
1469
Daniel Stoddenb70f5fa2010-04-30 22:01:19 +00001470 mutex_init(&info->mutex);
Steven Noonan34678112012-02-17 12:04:44 -08001471 spin_lock_init(&info->io_lock);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001472 info->xbdev = dev;
1473 info->vdevice = vdevice;
Roger Pau Monnebfe11d62013-10-29 18:31:14 +01001474 INIT_LIST_HEAD(&info->grants);
1475 INIT_LIST_HEAD(&info->indirect_pages);
Roger Pau Monne0a8704a2012-10-24 18:58:45 +02001476 info->persistent_gnts_c = 0;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001477 info->connected = BLKIF_STATE_DISCONNECTED;
1478 INIT_WORK(&info->work, blkif_restart_queue);
1479
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001480 /* Front end dir is a number, which is used as the id. */
1481 info->handle = simple_strtoul(strrchr(dev->nodename, '/')+1, NULL, 0);
Greg Kroah-Hartmana1b4b122009-04-30 14:43:31 -07001482 dev_set_drvdata(&dev->dev, info);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001483
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001484 return 0;
1485}
1486
Roger Pau Monne402b27f2013-04-18 16:06:54 +02001487static void split_bio_end(struct bio *bio, int error)
1488{
1489 struct split_bio *split_bio = bio->bi_private;
1490
1491 if (error)
1492 split_bio->err = error;
1493
1494 if (atomic_dec_and_test(&split_bio->pending)) {
1495 split_bio->bio->bi_phys_segments = 0;
1496 bio_endio(split_bio->bio, split_bio->err);
1497 kfree(split_bio);
1498 }
1499 bio_put(bio);
1500}
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001501
1502static int blkif_recover(struct blkfront_info *info)
1503{
1504 int i;
Roger Pau Monne402b27f2013-04-18 16:06:54 +02001505 struct request *req, *n;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001506 struct blk_shadow *copy;
Roger Pau Monne402b27f2013-04-18 16:06:54 +02001507 int rc;
1508 struct bio *bio, *cloned_bio;
1509 struct bio_list bio_list, merge_bio;
1510 unsigned int segs, offset;
1511 int pending, size;
1512 struct split_bio *split_bio;
1513 struct list_head requests;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001514
1515 /* Stage 1: Make a safe copy of the shadow state. */
Mihnea Dobrescu-Balaur29d0b212013-03-11 13:23:36 +02001516 copy = kmemdup(info->shadow, sizeof(info->shadow),
Ian Campbella144ff02008-06-17 10:47:08 +02001517 GFP_NOIO | __GFP_REPEAT | __GFP_HIGH);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001518 if (!copy)
1519 return -ENOMEM;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001520
1521 /* Stage 2: Set up free list. */
1522 memset(&info->shadow, 0, sizeof(info->shadow));
Bob Liu86839c52015-06-03 13:40:03 +08001523 for (i = 0; i < BLK_RING_SIZE(info); i++)
Konrad Rzeszutek Wilk97e36832011-10-12 12:12:36 -04001524 info->shadow[i].req.u.rw.id = i+1;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001525 info->shadow_free = info->ring.req_prod_pvt;
Bob Liu86839c52015-06-03 13:40:03 +08001526 info->shadow[BLK_RING_SIZE(info)-1].req.u.rw.id = 0x0fffffff;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001527
Roger Pau Monne402b27f2013-04-18 16:06:54 +02001528 rc = blkfront_setup_indirect(info);
1529 if (rc) {
1530 kfree(copy);
1531 return rc;
1532 }
1533
1534 segs = info->max_indirect_segments ? : BLKIF_MAX_SEGMENTS_PER_REQUEST;
1535 blk_queue_max_segments(info->rq, segs);
1536 bio_list_init(&bio_list);
1537 INIT_LIST_HEAD(&requests);
Bob Liu86839c52015-06-03 13:40:03 +08001538 for (i = 0; i < BLK_RING_SIZE(info); i++) {
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001539 /* Not in use? */
Jeremy Fitzhardingea945b982010-11-01 17:03:14 -04001540 if (!copy[i].request)
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001541 continue;
1542
Roger Pau Monne402b27f2013-04-18 16:06:54 +02001543 /*
1544 * Get the bios in the request so we can re-queue them.
1545 */
1546 if (copy[i].request->cmd_flags &
1547 (REQ_FLUSH | REQ_FUA | REQ_DISCARD | REQ_SECURE)) {
1548 /*
1549 * Flush operations don't contain bios, so
1550 * we need to requeue the whole request
1551 */
1552 list_add(&copy[i].request->queuelist, &requests);
1553 continue;
Konrad Rzeszutek Wilk5ea42982011-10-12 16:23:30 -04001554 }
Roger Pau Monne402b27f2013-04-18 16:06:54 +02001555 merge_bio.head = copy[i].request->bio;
1556 merge_bio.tail = copy[i].request->biotail;
1557 bio_list_merge(&bio_list, &merge_bio);
1558 copy[i].request->bio = NULL;
Roger Pau Monne3bb8c982015-02-02 11:28:21 +00001559 blk_end_request_all(copy[i].request, 0);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001560 }
1561
1562 kfree(copy);
1563
Roger Pau Monne402b27f2013-04-18 16:06:54 +02001564 /*
1565 * Empty the queue, this is important because we might have
1566 * requests in the queue with more segments than what we
1567 * can handle now.
1568 */
1569 spin_lock_irq(&info->io_lock);
1570 while ((req = blk_fetch_request(info->rq)) != NULL) {
1571 if (req->cmd_flags &
1572 (REQ_FLUSH | REQ_FUA | REQ_DISCARD | REQ_SECURE)) {
1573 list_add(&req->queuelist, &requests);
1574 continue;
1575 }
1576 merge_bio.head = req->bio;
1577 merge_bio.tail = req->biotail;
1578 bio_list_merge(&bio_list, &merge_bio);
1579 req->bio = NULL;
1580 if (req->cmd_flags & (REQ_FLUSH | REQ_FUA))
1581 pr_alert("diskcache flush request found!\n");
Roger Pau Monne3bb8c982015-02-02 11:28:21 +00001582 __blk_end_request_all(req, 0);
Roger Pau Monne402b27f2013-04-18 16:06:54 +02001583 }
1584 spin_unlock_irq(&info->io_lock);
1585
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001586 xenbus_switch_state(info->xbdev, XenbusStateConnected);
1587
Steven Noonan34678112012-02-17 12:04:44 -08001588 spin_lock_irq(&info->io_lock);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001589
1590 /* Now safe for us to use the shared ring */
1591 info->connected = BLKIF_STATE_CONNECTED;
1592
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001593 /* Kick any other new requests queued since we resumed */
1594 kick_pending_request_queues(info);
1595
Roger Pau Monne402b27f2013-04-18 16:06:54 +02001596 list_for_each_entry_safe(req, n, &requests, queuelist) {
1597 /* Requeue pending requests (flush or discard) */
1598 list_del_init(&req->queuelist);
1599 BUG_ON(req->nr_phys_segments > segs);
1600 blk_requeue_request(info->rq, req);
1601 }
Steven Noonan34678112012-02-17 12:04:44 -08001602 spin_unlock_irq(&info->io_lock);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001603
Roger Pau Monne402b27f2013-04-18 16:06:54 +02001604 while ((bio = bio_list_pop(&bio_list)) != NULL) {
1605 /* Traverse the list of pending bios and re-queue them */
1606 if (bio_segments(bio) > segs) {
1607 /*
1608 * This bio has more segments than what we can
1609 * handle, we have to split it.
1610 */
1611 pending = (bio_segments(bio) + segs - 1) / segs;
1612 split_bio = kzalloc(sizeof(*split_bio), GFP_NOIO);
1613 BUG_ON(split_bio == NULL);
1614 atomic_set(&split_bio->pending, pending);
1615 split_bio->bio = bio;
1616 for (i = 0; i < pending; i++) {
1617 offset = (i * segs * PAGE_SIZE) >> 9;
1618 size = min((unsigned int)(segs * PAGE_SIZE) >> 9,
Kent Overstreet4f024f32013-10-11 15:44:27 -07001619 (unsigned int)bio_sectors(bio) - offset);
Roger Pau Monne402b27f2013-04-18 16:06:54 +02001620 cloned_bio = bio_clone(bio, GFP_NOIO);
1621 BUG_ON(cloned_bio == NULL);
Kent Overstreet6678d832013-08-07 11:14:32 -07001622 bio_trim(cloned_bio, offset, size);
Roger Pau Monne402b27f2013-04-18 16:06:54 +02001623 cloned_bio->bi_private = split_bio;
1624 cloned_bio->bi_end_io = split_bio_end;
1625 submit_bio(cloned_bio->bi_rw, cloned_bio);
1626 }
1627 /*
1628 * Now we have to wait for all those smaller bios to
1629 * end, so we can also end the "parent" bio.
1630 */
1631 continue;
1632 }
1633 /* We don't need to split this bio */
1634 submit_bio(bio->bi_rw, bio);
1635 }
1636
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001637 return 0;
1638}
1639
1640/**
1641 * We are reconnecting to the backend, due to a suspend/resume, or a backend
1642 * driver restart. We tear down our blkif structure and recreate it, but
1643 * leave the device-layer structures intact so that this is transparent to the
1644 * rest of the kernel.
1645 */
1646static int blkfront_resume(struct xenbus_device *dev)
1647{
Greg Kroah-Hartmana1b4b122009-04-30 14:43:31 -07001648 struct blkfront_info *info = dev_get_drvdata(&dev->dev);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001649 int err;
1650
1651 dev_dbg(&dev->dev, "blkfront_resume: %s\n", dev->nodename);
1652
1653 blkif_free(info, info->connected == BLKIF_STATE_CONNECTED);
1654
Ian Campbell203fd612009-12-04 15:33:54 +00001655 err = talk_to_blkback(dev, info);
Roger Pau Monne402b27f2013-04-18 16:06:54 +02001656
1657 /*
1658 * We have to wait for the backend to switch to
1659 * connected state, since we want to read which
1660 * features it supports.
1661 */
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001662
1663 return err;
1664}
1665
Daniel Stoddenb70f5fa2010-04-30 22:01:19 +00001666static void
1667blkfront_closing(struct blkfront_info *info)
1668{
1669 struct xenbus_device *xbdev = info->xbdev;
1670 struct block_device *bdev = NULL;
1671
1672 mutex_lock(&info->mutex);
1673
1674 if (xbdev->state == XenbusStateClosing) {
1675 mutex_unlock(&info->mutex);
1676 return;
1677 }
1678
1679 if (info->gd)
1680 bdev = bdget_disk(info->gd, 0);
1681
1682 mutex_unlock(&info->mutex);
1683
1684 if (!bdev) {
1685 xenbus_frontend_closed(xbdev);
1686 return;
1687 }
1688
1689 mutex_lock(&bdev->bd_mutex);
1690
Daniel Stodden7b32d102010-04-30 22:01:23 +00001691 if (bdev->bd_openers) {
Daniel Stoddenb70f5fa2010-04-30 22:01:19 +00001692 xenbus_dev_error(xbdev, -EBUSY,
1693 "Device in use; refusing to close");
1694 xenbus_switch_state(xbdev, XenbusStateClosing);
1695 } else {
1696 xlvbd_release_gendisk(info);
1697 xenbus_frontend_closed(xbdev);
1698 }
1699
1700 mutex_unlock(&bdev->bd_mutex);
1701 bdput(bdev);
1702}
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001703
Li Dongyanged30bf32011-09-01 18:39:09 +08001704static void blkfront_setup_discard(struct blkfront_info *info)
1705{
1706 int err;
Li Dongyanged30bf32011-09-01 18:39:09 +08001707 unsigned int discard_granularity;
1708 unsigned int discard_alignment;
Konrad Rzeszutek Wilk5ea42982011-10-12 16:23:30 -04001709 unsigned int discard_secure;
Li Dongyanged30bf32011-09-01 18:39:09 +08001710
Olaf Hering1c8cad62014-05-21 16:32:40 +02001711 info->feature_discard = 1;
1712 err = xenbus_gather(XBT_NIL, info->xbdev->otherend,
1713 "discard-granularity", "%u", &discard_granularity,
1714 "discard-alignment", "%u", &discard_alignment,
1715 NULL);
1716 if (!err) {
1717 info->discard_granularity = discard_granularity;
1718 info->discard_alignment = discard_alignment;
1719 }
1720 err = xenbus_gather(XBT_NIL, info->xbdev->otherend,
1721 "discard-secure", "%d", &discard_secure,
1722 NULL);
1723 if (!err)
1724 info->feature_secdiscard = !!discard_secure;
Li Dongyanged30bf32011-09-01 18:39:09 +08001725}
1726
Roger Pau Monne402b27f2013-04-18 16:06:54 +02001727static int blkfront_setup_indirect(struct blkfront_info *info)
1728{
1729 unsigned int indirect_segments, segs;
1730 int err, i;
1731
1732 err = xenbus_gather(XBT_NIL, info->xbdev->otherend,
1733 "feature-max-indirect-segments", "%u", &indirect_segments,
1734 NULL);
1735 if (err) {
1736 info->max_indirect_segments = 0;
1737 segs = BLKIF_MAX_SEGMENTS_PER_REQUEST;
1738 } else {
1739 info->max_indirect_segments = min(indirect_segments,
1740 xen_blkif_max_segments);
1741 segs = info->max_indirect_segments;
1742 }
Roger Pau Monne402b27f2013-04-18 16:06:54 +02001743
Bob Liu86839c52015-06-03 13:40:03 +08001744 err = fill_grant_buffer(info, (segs + INDIRECT_GREFS(segs)) * BLK_RING_SIZE(info));
Roger Pau Monne402b27f2013-04-18 16:06:54 +02001745 if (err)
1746 goto out_of_memory;
1747
Roger Pau Monnebfe11d62013-10-29 18:31:14 +01001748 if (!info->feature_persistent && info->max_indirect_segments) {
1749 /*
1750 * We are using indirect descriptors but not persistent
1751 * grants, we need to allocate a set of pages that can be
1752 * used for mapping indirect grefs
1753 */
Bob Liu86839c52015-06-03 13:40:03 +08001754 int num = INDIRECT_GREFS(segs) * BLK_RING_SIZE(info);
Roger Pau Monnebfe11d62013-10-29 18:31:14 +01001755
1756 BUG_ON(!list_empty(&info->indirect_pages));
1757 for (i = 0; i < num; i++) {
1758 struct page *indirect_page = alloc_page(GFP_NOIO);
1759 if (!indirect_page)
1760 goto out_of_memory;
1761 list_add(&indirect_page->lru, &info->indirect_pages);
1762 }
1763 }
1764
Bob Liu86839c52015-06-03 13:40:03 +08001765 for (i = 0; i < BLK_RING_SIZE(info); i++) {
Roger Pau Monne402b27f2013-04-18 16:06:54 +02001766 info->shadow[i].grants_used = kzalloc(
1767 sizeof(info->shadow[i].grants_used[0]) * segs,
1768 GFP_NOIO);
Roger Pau Monneb7649152013-05-02 10:58:50 +02001769 info->shadow[i].sg = kzalloc(sizeof(info->shadow[i].sg[0]) * segs, GFP_NOIO);
Roger Pau Monne402b27f2013-04-18 16:06:54 +02001770 if (info->max_indirect_segments)
1771 info->shadow[i].indirect_grants = kzalloc(
1772 sizeof(info->shadow[i].indirect_grants[0]) *
1773 INDIRECT_GREFS(segs),
1774 GFP_NOIO);
1775 if ((info->shadow[i].grants_used == NULL) ||
Roger Pau Monneb7649152013-05-02 10:58:50 +02001776 (info->shadow[i].sg == NULL) ||
Roger Pau Monne402b27f2013-04-18 16:06:54 +02001777 (info->max_indirect_segments &&
1778 (info->shadow[i].indirect_grants == NULL)))
1779 goto out_of_memory;
Roger Pau Monneb7649152013-05-02 10:58:50 +02001780 sg_init_table(info->shadow[i].sg, segs);
Roger Pau Monne402b27f2013-04-18 16:06:54 +02001781 }
1782
1783
1784 return 0;
1785
1786out_of_memory:
Bob Liu86839c52015-06-03 13:40:03 +08001787 for (i = 0; i < BLK_RING_SIZE(info); i++) {
Roger Pau Monne402b27f2013-04-18 16:06:54 +02001788 kfree(info->shadow[i].grants_used);
1789 info->shadow[i].grants_used = NULL;
Roger Pau Monneb7649152013-05-02 10:58:50 +02001790 kfree(info->shadow[i].sg);
1791 info->shadow[i].sg = NULL;
Roger Pau Monne402b27f2013-04-18 16:06:54 +02001792 kfree(info->shadow[i].indirect_grants);
1793 info->shadow[i].indirect_grants = NULL;
1794 }
Roger Pau Monnebfe11d62013-10-29 18:31:14 +01001795 if (!list_empty(&info->indirect_pages)) {
1796 struct page *indirect_page, *n;
1797 list_for_each_entry_safe(indirect_page, n, &info->indirect_pages, lru) {
1798 list_del(&indirect_page->lru);
1799 __free_page(indirect_page);
1800 }
1801 }
Roger Pau Monne402b27f2013-04-18 16:06:54 +02001802 return -ENOMEM;
1803}
1804
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001805/*
1806 * Invoked when the backend is finally 'ready' (and has told produced
1807 * the details about the physical device - #sectors, size, etc).
1808 */
1809static void blkfront_connect(struct blkfront_info *info)
1810{
1811 unsigned long long sectors;
1812 unsigned long sector_size;
Stefan Bader7c4d7d72013-05-13 16:28:15 +02001813 unsigned int physical_sector_size;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001814 unsigned int binfo;
1815 int err;
Roger Pau Monne0a8704a2012-10-24 18:58:45 +02001816 int barrier, flush, discard, persistent;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001817
K. Y. Srinivasan1fa73be2010-03-11 13:42:26 -08001818 switch (info->connected) {
1819 case BLKIF_STATE_CONNECTED:
1820 /*
1821 * Potentially, the back-end may be signalling
1822 * a capacity change; update the capacity.
1823 */
1824 err = xenbus_scanf(XBT_NIL, info->xbdev->otherend,
1825 "sectors", "%Lu", &sectors);
1826 if (XENBUS_EXIST_ERR(err))
1827 return;
1828 printk(KERN_INFO "Setting capacity to %Lu\n",
1829 sectors);
1830 set_capacity(info->gd, sectors);
K. Y. Srinivasan2def1412010-03-18 15:00:54 -07001831 revalidate_disk(info->gd);
K. Y. Srinivasan1fa73be2010-03-11 13:42:26 -08001832
Roger Pau Monne402b27f2013-04-18 16:06:54 +02001833 return;
K. Y. Srinivasan1fa73be2010-03-11 13:42:26 -08001834 case BLKIF_STATE_SUSPENDED:
Roger Pau Monne402b27f2013-04-18 16:06:54 +02001835 /*
1836 * If we are recovering from suspension, we need to wait
1837 * for the backend to announce it's features before
1838 * reconnecting, at least we need to know if the backend
1839 * supports indirect descriptors, and how many.
1840 */
1841 blkif_recover(info);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001842 return;
1843
Jeremy Fitzhardingeb4dddb42010-03-11 15:10:40 -08001844 default:
1845 break;
K. Y. Srinivasan1fa73be2010-03-11 13:42:26 -08001846 }
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001847
1848 dev_dbg(&info->xbdev->dev, "%s:%s.\n",
1849 __func__, info->xbdev->otherend);
1850
1851 err = xenbus_gather(XBT_NIL, info->xbdev->otherend,
1852 "sectors", "%llu", &sectors,
1853 "info", "%u", &binfo,
1854 "sector-size", "%lu", &sector_size,
1855 NULL);
1856 if (err) {
1857 xenbus_dev_fatal(info->xbdev, err,
1858 "reading backend fields at %s",
1859 info->xbdev->otherend);
1860 return;
1861 }
1862
Stefan Bader7c4d7d72013-05-13 16:28:15 +02001863 /*
1864 * physcial-sector-size is a newer field, so old backends may not
1865 * provide this. Assume physical sector size to be the same as
1866 * sector_size in that case.
1867 */
1868 err = xenbus_scanf(XBT_NIL, info->xbdev->otherend,
1869 "physical-sector-size", "%u", &physical_sector_size);
1870 if (err != 1)
1871 physical_sector_size = sector_size;
1872
Konrad Rzeszutek Wilkedf6ef52011-05-03 12:01:11 -04001873 info->feature_flush = 0;
Konrad Rzeszutek Wilkedf6ef52011-05-03 12:01:11 -04001874
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001875 err = xenbus_gather(XBT_NIL, info->xbdev->otherend,
Marek Marczykowski4352b472011-05-03 12:04:52 -04001876 "feature-barrier", "%d", &barrier,
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001877 NULL);
Jeremy Fitzhardinge7901d142010-07-28 10:49:29 -07001878
1879 /*
1880 * If there's no "feature-barrier" defined, then it means
1881 * we're dealing with a very old backend which writes
Tejun Heo4913efe2010-09-03 11:56:16 +02001882 * synchronously; nothing to do.
Jeremy Fitzhardinge7901d142010-07-28 10:49:29 -07001883 *
Tejun Heo6958f142010-09-03 11:56:16 +02001884 * If there are barriers, then we use flush.
Jeremy Fitzhardinge7901d142010-07-28 10:49:29 -07001885 */
Vitaly Kuznetsovfdf9b962014-12-09 15:25:10 +01001886 if (!err && barrier)
Jeremy Fitzhardingebe2f8372010-11-02 10:38:33 -04001887 info->feature_flush = REQ_FLUSH | REQ_FUA;
Konrad Rzeszutek Wilkedf6ef52011-05-03 12:01:11 -04001888 /*
1889 * And if there is "feature-flush-cache" use that above
1890 * barriers.
1891 */
1892 err = xenbus_gather(XBT_NIL, info->xbdev->otherend,
1893 "feature-flush-cache", "%d", &flush,
1894 NULL);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001895
Vitaly Kuznetsovfdf9b962014-12-09 15:25:10 +01001896 if (!err && flush)
Konrad Rzeszutek Wilkedf6ef52011-05-03 12:01:11 -04001897 info->feature_flush = REQ_FLUSH;
Li Dongyanged30bf32011-09-01 18:39:09 +08001898
1899 err = xenbus_gather(XBT_NIL, info->xbdev->otherend,
1900 "feature-discard", "%d", &discard,
1901 NULL);
1902
1903 if (!err && discard)
1904 blkfront_setup_discard(info);
1905
Roger Pau Monne0a8704a2012-10-24 18:58:45 +02001906 err = xenbus_gather(XBT_NIL, info->xbdev->otherend,
1907 "feature-persistent", "%u", &persistent,
1908 NULL);
1909 if (err)
1910 info->feature_persistent = 0;
1911 else
1912 info->feature_persistent = persistent;
1913
Roger Pau Monne402b27f2013-04-18 16:06:54 +02001914 err = blkfront_setup_indirect(info);
1915 if (err) {
1916 xenbus_dev_fatal(info->xbdev, err, "setup_indirect at %s",
1917 info->xbdev->otherend);
1918 return;
1919 }
1920
Stefan Bader7c4d7d72013-05-13 16:28:15 +02001921 err = xlvbd_alloc_gendisk(sectors, info, binfo, sector_size,
1922 physical_sector_size);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001923 if (err) {
1924 xenbus_dev_fatal(info->xbdev, err, "xlvbd_add at %s",
1925 info->xbdev->otherend);
1926 return;
1927 }
1928
1929 xenbus_switch_state(info->xbdev, XenbusStateConnected);
1930
1931 /* Kick pending requests. */
Steven Noonan34678112012-02-17 12:04:44 -08001932 spin_lock_irq(&info->io_lock);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001933 info->connected = BLKIF_STATE_CONNECTED;
1934 kick_pending_request_queues(info);
Steven Noonan34678112012-02-17 12:04:44 -08001935 spin_unlock_irq(&info->io_lock);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001936
1937 add_disk(info->gd);
Christian Limpach1d78d702008-04-02 10:54:04 -07001938
1939 info->is_ready = 1;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001940}
1941
1942/**
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001943 * Callback received when the backend's state changes.
1944 */
Ian Campbell203fd612009-12-04 15:33:54 +00001945static void blkback_changed(struct xenbus_device *dev,
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001946 enum xenbus_state backend_state)
1947{
Greg Kroah-Hartmana1b4b122009-04-30 14:43:31 -07001948 struct blkfront_info *info = dev_get_drvdata(&dev->dev);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001949
Ian Campbell203fd612009-12-04 15:33:54 +00001950 dev_dbg(&dev->dev, "blkfront:blkback_changed to state %d.\n", backend_state);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001951
1952 switch (backend_state) {
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001953 case XenbusStateInitWait:
Bob Liua9b54bb2015-06-19 00:23:00 -04001954 if (dev->state != XenbusStateInitialising)
1955 break;
Bob Liu8ab01442015-06-03 13:40:02 +08001956 if (talk_to_blkback(dev, info)) {
1957 kfree(info);
1958 dev_set_drvdata(&dev->dev, NULL);
1959 break;
1960 }
1961 case XenbusStateInitialising:
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001962 case XenbusStateInitialised:
Noboru Iwamatsub78c9512009-10-13 17:22:29 -04001963 case XenbusStateReconfiguring:
1964 case XenbusStateReconfigured:
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001965 case XenbusStateUnknown:
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001966 break;
1967
1968 case XenbusStateConnected:
1969 blkfront_connect(info);
1970 break;
1971
David Vrabel36613712014-02-04 18:53:56 +00001972 case XenbusStateClosed:
1973 if (dev->state == XenbusStateClosed)
1974 break;
1975 /* Missed the backend's Closing state -- fallthrough */
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001976 case XenbusStateClosing:
Daniel Stoddenb70f5fa2010-04-30 22:01:19 +00001977 blkfront_closing(info);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001978 break;
1979 }
1980}
1981
Daniel Stoddenfa1bd352010-04-30 22:01:22 +00001982static int blkfront_remove(struct xenbus_device *xbdev)
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001983{
Daniel Stoddenfa1bd352010-04-30 22:01:22 +00001984 struct blkfront_info *info = dev_get_drvdata(&xbdev->dev);
1985 struct block_device *bdev = NULL;
1986 struct gendisk *disk;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001987
Daniel Stoddenfa1bd352010-04-30 22:01:22 +00001988 dev_dbg(&xbdev->dev, "%s removed", xbdev->nodename);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001989
1990 blkif_free(info, 0);
1991
Daniel Stoddenfa1bd352010-04-30 22:01:22 +00001992 mutex_lock(&info->mutex);
1993
1994 disk = info->gd;
1995 if (disk)
1996 bdev = bdget_disk(disk, 0);
1997
1998 info->xbdev = NULL;
1999 mutex_unlock(&info->mutex);
2000
2001 if (!bdev) {
Jan Beulich0e345822010-08-07 18:28:55 +02002002 kfree(info);
Daniel Stoddenfa1bd352010-04-30 22:01:22 +00002003 return 0;
2004 }
2005
2006 /*
2007 * The xbdev was removed before we reached the Closed
2008 * state. See if it's safe to remove the disk. If the bdev
2009 * isn't closed yet, we let release take care of it.
2010 */
2011
2012 mutex_lock(&bdev->bd_mutex);
2013 info = disk->private_data;
2014
Daniel Stoddend54142c2010-08-07 18:51:21 +02002015 dev_warn(disk_to_dev(disk),
2016 "%s was hot-unplugged, %d stale handles\n",
2017 xbdev->nodename, bdev->bd_openers);
2018
Daniel Stodden7b32d102010-04-30 22:01:23 +00002019 if (info && !bdev->bd_openers) {
Daniel Stoddenfa1bd352010-04-30 22:01:22 +00002020 xlvbd_release_gendisk(info);
2021 disk->private_data = NULL;
2022 kfree(info);
2023 }
2024
2025 mutex_unlock(&bdev->bd_mutex);
2026 bdput(bdev);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07002027
2028 return 0;
2029}
2030
Christian Limpach1d78d702008-04-02 10:54:04 -07002031static int blkfront_is_ready(struct xenbus_device *dev)
2032{
Greg Kroah-Hartmana1b4b122009-04-30 14:43:31 -07002033 struct blkfront_info *info = dev_get_drvdata(&dev->dev);
Christian Limpach1d78d702008-04-02 10:54:04 -07002034
Jan Beulich5d7ed202010-08-07 18:31:12 +02002035 return info->is_ready && info->xbdev;
Christian Limpach1d78d702008-04-02 10:54:04 -07002036}
2037
Al Viroa63c8482008-03-02 10:23:47 -05002038static int blkif_open(struct block_device *bdev, fmode_t mode)
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07002039{
Daniel Stodden13961742010-08-07 18:36:53 +02002040 struct gendisk *disk = bdev->bd_disk;
2041 struct blkfront_info *info;
2042 int err = 0;
Arnd Bergmann6e9624b2010-08-07 18:25:34 +02002043
Arnd Bergmann2a48fc02010-06-02 14:28:52 +02002044 mutex_lock(&blkfront_mutex);
Arnd Bergmann6e9624b2010-08-07 18:25:34 +02002045
Daniel Stodden13961742010-08-07 18:36:53 +02002046 info = disk->private_data;
2047 if (!info) {
2048 /* xbdev gone */
2049 err = -ERESTARTSYS;
2050 goto out;
2051 }
2052
2053 mutex_lock(&info->mutex);
2054
2055 if (!info->gd)
2056 /* xbdev is closed */
2057 err = -ERESTARTSYS;
2058
2059 mutex_unlock(&info->mutex);
2060
Daniel Stodden13961742010-08-07 18:36:53 +02002061out:
Arnd Bergmann2a48fc02010-06-02 14:28:52 +02002062 mutex_unlock(&blkfront_mutex);
Daniel Stodden13961742010-08-07 18:36:53 +02002063 return err;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07002064}
2065
Al Virodb2a1442013-05-05 21:52:57 -04002066static void blkif_release(struct gendisk *disk, fmode_t mode)
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07002067{
Al Viroa63c8482008-03-02 10:23:47 -05002068 struct blkfront_info *info = disk->private_data;
Daniel Stodden7fd152f2010-08-07 18:45:12 +02002069 struct block_device *bdev;
2070 struct xenbus_device *xbdev;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07002071
Arnd Bergmann2a48fc02010-06-02 14:28:52 +02002072 mutex_lock(&blkfront_mutex);
Daniel Stodden7fd152f2010-08-07 18:45:12 +02002073
2074 bdev = bdget_disk(disk, 0);
Daniel Stodden7fd152f2010-08-07 18:45:12 +02002075
Felipe Pena2f089cb2013-11-09 13:36:09 -02002076 if (!bdev) {
2077 WARN(1, "Block device %s yanked out from us!\n", disk->disk_name);
2078 goto out_mutex;
2079 }
Daniel Stoddenacfca3c2010-08-07 18:47:26 +02002080 if (bdev->bd_openers)
2081 goto out;
2082
Daniel Stodden7fd152f2010-08-07 18:45:12 +02002083 /*
2084 * Check if we have been instructed to close. We will have
2085 * deferred this request, because the bdev was still open.
2086 */
2087
2088 mutex_lock(&info->mutex);
2089 xbdev = info->xbdev;
2090
2091 if (xbdev && xbdev->state == XenbusStateClosing) {
2092 /* pending switch to state closed */
Daniel Stoddend54142c2010-08-07 18:51:21 +02002093 dev_info(disk_to_dev(bdev->bd_disk), "releasing disk\n");
Daniel Stodden7fd152f2010-08-07 18:45:12 +02002094 xlvbd_release_gendisk(info);
2095 xenbus_frontend_closed(info->xbdev);
2096 }
2097
2098 mutex_unlock(&info->mutex);
2099
2100 if (!xbdev) {
2101 /* sudden device removal */
Daniel Stoddend54142c2010-08-07 18:51:21 +02002102 dev_info(disk_to_dev(bdev->bd_disk), "releasing disk\n");
Daniel Stodden7fd152f2010-08-07 18:45:12 +02002103 xlvbd_release_gendisk(info);
2104 disk->private_data = NULL;
2105 kfree(info);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07002106 }
Daniel Stodden7fd152f2010-08-07 18:45:12 +02002107
Jens Axboea4cc14e2010-08-08 21:50:05 -04002108out:
Andrew Jonesdad5cf62012-02-16 13:16:25 +01002109 bdput(bdev);
Felipe Pena2f089cb2013-11-09 13:36:09 -02002110out_mutex:
Arnd Bergmann2a48fc02010-06-02 14:28:52 +02002111 mutex_unlock(&blkfront_mutex);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07002112}
2113
Alexey Dobriyan83d5cde2009-09-21 17:01:13 -07002114static const struct block_device_operations xlvbd_block_fops =
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07002115{
2116 .owner = THIS_MODULE,
Al Viroa63c8482008-03-02 10:23:47 -05002117 .open = blkif_open,
2118 .release = blkif_release,
Ian Campbell597592d2008-02-21 13:03:45 -08002119 .getgeo = blkif_getgeo,
Arnd Bergmann8a6cfeb2010-07-08 10:18:46 +02002120 .ioctl = blkif_ioctl,
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07002121};
2122
2123
Márton Némethec9c42e2010-01-10 13:39:52 +01002124static const struct xenbus_device_id blkfront_ids[] = {
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07002125 { "vbd" },
2126 { "" }
2127};
2128
David Vrabel95afae42014-09-08 17:30:41 +01002129static struct xenbus_driver blkfront_driver = {
2130 .ids = blkfront_ids,
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07002131 .probe = blkfront_probe,
2132 .remove = blkfront_remove,
2133 .resume = blkfront_resume,
Ian Campbell203fd612009-12-04 15:33:54 +00002134 .otherend_changed = blkback_changed,
Christian Limpach1d78d702008-04-02 10:54:04 -07002135 .is_ready = blkfront_is_ready,
David Vrabel95afae42014-09-08 17:30:41 +01002136};
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07002137
2138static int __init xlblk_init(void)
2139{
Laszlo Ersek469738e2011-10-07 21:34:38 +02002140 int ret;
2141
Jeremy Fitzhardinge6e833582008-08-19 13:16:17 -07002142 if (!xen_domain())
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07002143 return -ENODEV;
2144
Bob Liu86839c52015-06-03 13:40:03 +08002145 if (xen_blkif_max_ring_order > XENBUS_MAX_RING_PAGE_ORDER) {
2146 pr_info("Invalid max_ring_order (%d), will use default max: %d.\n",
2147 xen_blkif_max_ring_order, XENBUS_MAX_RING_PAGE_ORDER);
2148 xen_blkif_max_ring_order = 0;
2149 }
2150
Konrad Rzeszutek Wilk51c71a32013-11-26 15:05:40 -05002151 if (!xen_has_pv_disk_devices())
Igor Mammedovb9136d22012-03-21 15:08:38 +01002152 return -ENODEV;
2153
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07002154 if (register_blkdev(XENVBD_MAJOR, DEV_NAME)) {
2155 printk(KERN_WARNING "xen_blk: can't get major %d with name %s\n",
2156 XENVBD_MAJOR, DEV_NAME);
2157 return -ENODEV;
2158 }
2159
Jan Beulich73db1442011-12-22 09:08:13 +00002160 ret = xenbus_register_frontend(&blkfront_driver);
Laszlo Ersek469738e2011-10-07 21:34:38 +02002161 if (ret) {
2162 unregister_blkdev(XENVBD_MAJOR, DEV_NAME);
2163 return ret;
2164 }
2165
2166 return 0;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07002167}
2168module_init(xlblk_init);
2169
2170
Jan Beulich5a60d0c2008-06-17 10:47:08 +02002171static void __exit xlblk_exit(void)
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07002172{
Jan Beulich86050672012-04-05 16:04:52 +01002173 xenbus_unregister_driver(&blkfront_driver);
2174 unregister_blkdev(XENVBD_MAJOR, DEV_NAME);
2175 kfree(minors);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07002176}
2177module_exit(xlblk_exit);
2178
2179MODULE_DESCRIPTION("Xen virtual block device frontend");
2180MODULE_LICENSE("GPL");
2181MODULE_ALIAS_BLOCKDEV_MAJOR(XENVBD_MAJOR);
Mark McLoughlind2f0c522008-04-02 10:54:05 -07002182MODULE_ALIAS("xen:vbd");
Mark McLoughlin4f93f09b2008-04-02 10:54:06 -07002183MODULE_ALIAS("xenblk");