blob: 81c87e69cbe9cd1706eac74fb7e9dc37e83d4fd9 [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>
Bob Liu907c3eb2015-07-13 17:55:24 +080040#include <linux/blk-mq.h>
Ian Campbell597592d2008-02-21 13:03:45 -080041#include <linux/hdreg.h>
Christian Limpach440a01a2008-06-17 10:47:08 +020042#include <linux/cdrom.h>
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -070043#include <linux/module.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090044#include <linux/slab.h>
Arnd Bergmann2a48fc02010-06-02 14:28:52 +020045#include <linux/mutex.h>
Jens Axboe9e973e62009-02-24 08:10:09 +010046#include <linux/scatterlist.h>
Akinobu Mita34ae2e42012-01-21 00:15:26 +090047#include <linux/bitmap.h>
Roger Pau Monne155b7ed2013-03-18 17:49:34 +010048#include <linux/list.h>
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -070049
Jeremy Fitzhardinge1ccbf532009-10-06 15:11:14 -070050#include <xen/xen.h>
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -070051#include <xen/xenbus.h>
52#include <xen/grant_table.h>
53#include <xen/events.h>
54#include <xen/page.h>
Stefano Stabellinic1c54132010-05-14 12:44:30 +010055#include <xen/platform_pci.h>
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -070056
57#include <xen/interface/grant_table.h>
58#include <xen/interface/io/blkif.h>
Markus Armbruster3e334232008-04-02 10:54:02 -070059#include <xen/interface/io/protocols.h>
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -070060
61#include <asm/xen/hypervisor.h>
62
63enum blkif_state {
64 BLKIF_STATE_DISCONNECTED,
65 BLKIF_STATE_CONNECTED,
66 BLKIF_STATE_SUSPENDED,
67};
68
Roger Pau Monne0a8704a52012-10-24 18:58:45 +020069struct grant {
70 grant_ref_t gref;
Julien Gralla7a6df22015-06-30 11:58:51 +010071 struct page *page;
Roger Pau Monne155b7ed2013-03-18 17:49:34 +010072 struct list_head node;
Roger Pau Monne0a8704a52012-10-24 18:58:45 +020073};
74
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -070075struct blk_shadow {
76 struct blkif_request req;
Jeremy Fitzhardingea945b982010-11-01 17:03:14 -040077 struct request *request;
Roger Pau Monne402b27f2013-04-18 16:06:54 +020078 struct grant **grants_used;
79 struct grant **indirect_grants;
Roger Pau Monneb7649152013-05-02 10:58:50 +020080 struct scatterlist *sg;
Julien Grallc004a6f2015-07-22 16:44:54 +010081 unsigned int num_sg;
Roger Pau Monne402b27f2013-04-18 16:06:54 +020082};
83
84struct split_bio {
85 struct bio *bio;
86 atomic_t pending;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -070087};
88
Arnd Bergmann2a48fc02010-06-02 14:28:52 +020089static DEFINE_MUTEX(blkfront_mutex);
Alexey Dobriyan83d5cde2009-09-21 17:01:13 -070090static const struct block_device_operations xlvbd_block_fops;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -070091
Roger Pau Monne402b27f2013-04-18 16:06:54 +020092/*
93 * Maximum number of segments in indirect requests, the actual value used by
94 * the frontend driver is the minimum of this value and the value provided
95 * by the backend driver.
96 */
97
98static unsigned int xen_blkif_max_segments = 32;
Konrad Rzeszutek Wilk2d5dc3b2013-05-15 10:39:34 -040099module_param_named(max, xen_blkif_max_segments, int, S_IRUGO);
100MODULE_PARM_DESC(max, "Maximum amount of segments in indirect requests (default is 32)");
Roger Pau Monne402b27f2013-04-18 16:06:54 +0200101
Bob Liu28d949b2015-11-14 11:12:14 +0800102static unsigned int xen_blkif_max_queues = 4;
103module_param_named(max_queues, xen_blkif_max_queues, uint, S_IRUGO);
104MODULE_PARM_DESC(max_queues, "Maximum number of hardware queues/rings used per virtual disk");
105
Bob Liu86839c52015-06-03 13:40:03 +0800106/*
107 * Maximum order of pages to be used for the shared ring between front and
108 * backend, 4KB page granularity is used.
109 */
110static unsigned int xen_blkif_max_ring_order;
111module_param_named(max_ring_page_order, xen_blkif_max_ring_order, int, S_IRUGO);
112MODULE_PARM_DESC(max_ring_page_order, "Maximum order of pages to be used for the shared ring");
113
Julien Grallc004a6f2015-07-22 16:44:54 +0100114#define BLK_RING_SIZE(info) \
115 __CONST_RING_SIZE(blkif, XEN_PAGE_SIZE * (info)->nr_ring_pages)
116
117#define BLK_MAX_RING_SIZE \
Julien Grall9cce2912015-10-13 17:50:11 +0100118 __CONST_RING_SIZE(blkif, XEN_PAGE_SIZE * XENBUS_MAX_RING_GRANTS)
Julien Grallc004a6f2015-07-22 16:44:54 +0100119
Bob Liu86839c52015-06-03 13:40:03 +0800120/*
121 * ring-ref%i i=(-1UL) would take 11 characters + 'ring-ref' is 8, so 19
122 * characters are enough. Define to 20 to keep consist with backend.
123 */
124#define RINGREF_NAME_LEN (20)
Bob Liu28d949b2015-11-14 11:12:14 +0800125/*
126 * queue-%u would take 7 + 10(UINT_MAX) = 17 characters.
127 */
128#define QUEUE_NAME_LEN (17)
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700129
130/*
Bob Liu81f35162015-11-14 11:12:11 +0800131 * Per-ring info.
132 * Every blkfront device can associate with one or more blkfront_ring_info,
133 * depending on how many hardware queues/rings to be used.
134 */
135struct blkfront_ring_info {
Bob Liu11659562015-11-14 11:12:13 +0800136 /* Lock to protect data in every ring buffer. */
137 spinlock_t ring_lock;
Bob Liu81f35162015-11-14 11:12:11 +0800138 struct blkif_front_ring ring;
139 unsigned int ring_ref[XENBUS_MAX_RING_GRANTS];
140 unsigned int evtchn, irq;
141 struct work_struct work;
142 struct gnttab_free_callback callback;
143 struct blk_shadow shadow[BLK_MAX_RING_SIZE];
144 struct list_head indirect_pages;
145 unsigned long shadow_free;
146 struct blkfront_info *dev_info;
147};
148
149/*
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700150 * We have one of these per vbd, whether ide, scsi or 'other'. They
151 * hang in private_data off the gendisk structure. We may end up
152 * putting all kinds of interesting stuff here :-)
153 */
154struct blkfront_info
155{
Daniel Stoddenb70f5fa2010-04-30 22:01:19 +0000156 struct mutex mutex;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700157 struct xenbus_device *xbdev;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700158 struct gendisk *gd;
159 int vdevice;
160 blkif_vdev_t handle;
161 enum blkif_state connected;
Bob Liu3df0e502015-11-14 11:12:12 +0800162 /* Number of pages per ring buffer. */
Bob Liu86839c52015-06-03 13:40:03 +0800163 unsigned int nr_ring_pages;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700164 struct request_queue *rq;
Bob Liu11659562015-11-14 11:12:13 +0800165 /*
166 * Lock to protect info->grants list and persistent_gnts_c shared by all
167 * rings.
168 */
169 spinlock_t dev_lock;
Roger Pau Monnebfe11d62013-10-29 18:31:14 +0100170 struct list_head grants;
Roger Pau Monne0a8704a52012-10-24 18:58:45 +0200171 unsigned int persistent_gnts_c;
Tejun Heo4913efe2010-09-03 11:56:16 +0200172 unsigned int feature_flush;
Konrad Rzeszutek Wilk5ea42982011-10-12 16:23:30 -0400173 unsigned int feature_discard:1;
174 unsigned int feature_secdiscard:1;
Li Dongyanged30bf32011-09-01 18:39:09 +0800175 unsigned int discard_granularity;
176 unsigned int discard_alignment;
Roger Pau Monne0a8704a52012-10-24 18:58:45 +0200177 unsigned int feature_persistent:1;
Julien Grallc004a6f2015-07-22 16:44:54 +0100178 /* Number of 4KB segments handled */
Roger Pau Monne402b27f2013-04-18 16:06:54 +0200179 unsigned int max_indirect_segments;
Christian Limpach1d78d702008-04-02 10:54:04 -0700180 int is_ready;
Bob Liu907c3eb2015-07-13 17:55:24 +0800181 struct blk_mq_tag_set tag_set;
Bob Liu3df0e502015-11-14 11:12:12 +0800182 struct blkfront_ring_info *rinfo;
183 unsigned int nr_rings;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700184};
185
Jan Beulich0e345822010-08-07 18:28:55 +0200186static unsigned int nr_minors;
187static unsigned long *minors;
188static DEFINE_SPINLOCK(minor_lock);
189
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700190#define GRANT_INVALID_REF 0
191
192#define PARTS_PER_DISK 16
Chris Lalancette9246b5f2008-09-17 14:30:32 -0700193#define PARTS_PER_EXT_DISK 256
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700194
195#define BLKIF_MAJOR(dev) ((dev)>>8)
196#define BLKIF_MINOR(dev) ((dev) & 0xff)
197
Chris Lalancette9246b5f2008-09-17 14:30:32 -0700198#define EXT_SHIFT 28
199#define EXTENDED (1<<EXT_SHIFT)
200#define VDEV_IS_EXTENDED(dev) ((dev)&(EXTENDED))
201#define BLKIF_MINOR_EXT(dev) ((dev)&(~EXTENDED))
Stefano Stabellinic80a4202010-12-02 17:55:00 +0000202#define EMULATED_HD_DISK_MINOR_OFFSET (0)
203#define EMULATED_HD_DISK_NAME_OFFSET (EMULATED_HD_DISK_MINOR_OFFSET / 256)
Stefan Bader196cfe22011-07-14 15:30:22 +0200204#define EMULATED_SD_DISK_MINOR_OFFSET (0)
205#define EMULATED_SD_DISK_NAME_OFFSET (EMULATED_SD_DISK_MINOR_OFFSET / 256)
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700206
Chris Lalancette9246b5f2008-09-17 14:30:32 -0700207#define DEV_NAME "xvd" /* name in /dev */
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700208
Julien Grallc004a6f2015-07-22 16:44:54 +0100209/*
210 * Grants are always the same size as a Xen page (i.e 4KB).
211 * A physical segment is always the same size as a Linux page.
212 * Number of grants per physical segment
213 */
214#define GRANTS_PER_PSEG (PAGE_SIZE / XEN_PAGE_SIZE)
215
216#define GRANTS_PER_INDIRECT_FRAME \
217 (XEN_PAGE_SIZE / sizeof(struct blkif_request_segment))
218
219#define PSEGS_PER_INDIRECT_FRAME \
220 (GRANTS_INDIRECT_FRAME / GRANTS_PSEGS)
221
222#define INDIRECT_GREFS(_grants) \
223 DIV_ROUND_UP(_grants, GRANTS_PER_INDIRECT_FRAME)
224
225#define GREFS(_psegs) ((_psegs) * GRANTS_PER_PSEG)
Roger Pau Monne402b27f2013-04-18 16:06:54 +0200226
Bob Liu81f35162015-11-14 11:12:11 +0800227static int blkfront_setup_indirect(struct blkfront_ring_info *rinfo);
Bob Liu3df0e502015-11-14 11:12:12 +0800228static void blkfront_gather_backend_features(struct blkfront_info *info);
Roger Pau Monne402b27f2013-04-18 16:06:54 +0200229
Bob Liu81f35162015-11-14 11:12:11 +0800230static int get_id_from_freelist(struct blkfront_ring_info *rinfo)
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700231{
Bob Liu81f35162015-11-14 11:12:11 +0800232 unsigned long free = rinfo->shadow_free;
233
234 BUG_ON(free >= BLK_RING_SIZE(rinfo->dev_info));
235 rinfo->shadow_free = rinfo->shadow[free].req.u.rw.id;
236 rinfo->shadow[free].req.u.rw.id = 0x0fffffee; /* debug */
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700237 return free;
238}
239
Bob Liu81f35162015-11-14 11:12:11 +0800240static int add_id_to_freelist(struct blkfront_ring_info *rinfo,
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700241 unsigned long id)
242{
Bob Liu81f35162015-11-14 11:12:11 +0800243 if (rinfo->shadow[id].req.u.rw.id != id)
Konrad Rzeszutek Wilk6878c322012-05-25 17:34:51 -0400244 return -EINVAL;
Bob Liu81f35162015-11-14 11:12:11 +0800245 if (rinfo->shadow[id].request == NULL)
Konrad Rzeszutek Wilk6878c322012-05-25 17:34:51 -0400246 return -EINVAL;
Bob Liu81f35162015-11-14 11:12:11 +0800247 rinfo->shadow[id].req.u.rw.id = rinfo->shadow_free;
248 rinfo->shadow[id].request = NULL;
249 rinfo->shadow_free = id;
Konrad Rzeszutek Wilk6878c322012-05-25 17:34:51 -0400250 return 0;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700251}
252
Bob Liu81f35162015-11-14 11:12:11 +0800253static int fill_grant_buffer(struct blkfront_ring_info *rinfo, int num)
Roger Pau Monne9c1e0502013-03-18 17:49:35 +0100254{
Bob Liu81f35162015-11-14 11:12:11 +0800255 struct blkfront_info *info = rinfo->dev_info;
Roger Pau Monne9c1e0502013-03-18 17:49:35 +0100256 struct page *granted_page;
257 struct grant *gnt_list_entry, *n;
258 int i = 0;
259
260 while(i < num) {
261 gnt_list_entry = kzalloc(sizeof(struct grant), GFP_NOIO);
262 if (!gnt_list_entry)
263 goto out_of_memory;
264
Roger Pau Monnebfe11d62013-10-29 18:31:14 +0100265 if (info->feature_persistent) {
266 granted_page = alloc_page(GFP_NOIO);
267 if (!granted_page) {
268 kfree(gnt_list_entry);
269 goto out_of_memory;
270 }
Julien Gralla7a6df22015-06-30 11:58:51 +0100271 gnt_list_entry->page = granted_page;
Roger Pau Monne9c1e0502013-03-18 17:49:35 +0100272 }
273
Roger Pau Monne9c1e0502013-03-18 17:49:35 +0100274 gnt_list_entry->gref = GRANT_INVALID_REF;
Bob Liu11659562015-11-14 11:12:13 +0800275 spin_lock_irq(&info->dev_lock);
Roger Pau Monnebfe11d62013-10-29 18:31:14 +0100276 list_add(&gnt_list_entry->node, &info->grants);
Bob Liu11659562015-11-14 11:12:13 +0800277 spin_unlock_irq(&info->dev_lock);
Roger Pau Monne9c1e0502013-03-18 17:49:35 +0100278 i++;
279 }
280
281 return 0;
282
283out_of_memory:
284 list_for_each_entry_safe(gnt_list_entry, n,
Roger Pau Monnebfe11d62013-10-29 18:31:14 +0100285 &info->grants, node) {
Bob Liu11659562015-11-14 11:12:13 +0800286 spin_lock_irq(&info->dev_lock);
Roger Pau Monne9c1e0502013-03-18 17:49:35 +0100287 list_del(&gnt_list_entry->node);
Bob Liu11659562015-11-14 11:12:13 +0800288 spin_unlock_irq(&info->dev_lock);
Roger Pau Monnebfe11d62013-10-29 18:31:14 +0100289 if (info->feature_persistent)
Julien Gralla7a6df22015-06-30 11:58:51 +0100290 __free_page(gnt_list_entry->page);
Roger Pau Monne9c1e0502013-03-18 17:49:35 +0100291 kfree(gnt_list_entry);
292 i--;
293 }
294 BUG_ON(i != 0);
295 return -ENOMEM;
296}
297
Julien Grall4f503fb2015-07-01 14:10:38 +0100298static struct grant *get_free_grant(struct blkfront_info *info)
Roger Pau Monne9c1e0502013-03-18 17:49:35 +0100299{
300 struct grant *gnt_list_entry;
Bob Liu11659562015-11-14 11:12:13 +0800301 unsigned long flags;
Roger Pau Monne9c1e0502013-03-18 17:49:35 +0100302
Bob Liu11659562015-11-14 11:12:13 +0800303 spin_lock_irqsave(&info->dev_lock, flags);
Roger Pau Monnebfe11d62013-10-29 18:31:14 +0100304 BUG_ON(list_empty(&info->grants));
305 gnt_list_entry = list_first_entry(&info->grants, struct grant,
Julien Grall4f503fb2015-07-01 14:10:38 +0100306 node);
Roger Pau Monne9c1e0502013-03-18 17:49:35 +0100307 list_del(&gnt_list_entry->node);
308
Julien Grall4f503fb2015-07-01 14:10:38 +0100309 if (gnt_list_entry->gref != GRANT_INVALID_REF)
Roger Pau Monne9c1e0502013-03-18 17:49:35 +0100310 info->persistent_gnts_c--;
Bob Liu11659562015-11-14 11:12:13 +0800311 spin_unlock_irqrestore(&info->dev_lock, flags);
Julien Grall4f503fb2015-07-01 14:10:38 +0100312
313 return gnt_list_entry;
314}
315
316static inline void grant_foreign_access(const struct grant *gnt_list_entry,
317 const struct blkfront_info *info)
318{
319 gnttab_page_grant_foreign_access_ref_one(gnt_list_entry->gref,
320 info->xbdev->otherend_id,
321 gnt_list_entry->page,
322 0);
323}
324
325static struct grant *get_grant(grant_ref_t *gref_head,
326 unsigned long gfn,
327 struct blkfront_info *info)
328{
329 struct grant *gnt_list_entry = get_free_grant(info);
330
331 if (gnt_list_entry->gref != GRANT_INVALID_REF)
Roger Pau Monne9c1e0502013-03-18 17:49:35 +0100332 return gnt_list_entry;
Julien Grall4f503fb2015-07-01 14:10:38 +0100333
334 /* Assign a gref to this page */
335 gnt_list_entry->gref = gnttab_claim_grant_reference(gref_head);
336 BUG_ON(gnt_list_entry->gref == -ENOSPC);
337 if (info->feature_persistent)
338 grant_foreign_access(gnt_list_entry, info);
339 else {
340 /* Grant access to the GFN passed by the caller */
341 gnttab_grant_foreign_access_ref(gnt_list_entry->gref,
342 info->xbdev->otherend_id,
343 gfn, 0);
Roger Pau Monne9c1e0502013-03-18 17:49:35 +0100344 }
345
Julien Grall4f503fb2015-07-01 14:10:38 +0100346 return gnt_list_entry;
347}
348
349static struct grant *get_indirect_grant(grant_ref_t *gref_head,
350 struct blkfront_info *info)
351{
352 struct grant *gnt_list_entry = get_free_grant(info);
353
354 if (gnt_list_entry->gref != GRANT_INVALID_REF)
355 return gnt_list_entry;
356
Roger Pau Monne9c1e0502013-03-18 17:49:35 +0100357 /* Assign a gref to this page */
358 gnt_list_entry->gref = gnttab_claim_grant_reference(gref_head);
359 BUG_ON(gnt_list_entry->gref == -ENOSPC);
Roger Pau Monnebfe11d62013-10-29 18:31:14 +0100360 if (!info->feature_persistent) {
Julien Grall4f503fb2015-07-01 14:10:38 +0100361 struct page *indirect_page;
362
363 /* Fetch a pre-allocated page to use for indirect grefs */
Bob Liu3df0e502015-11-14 11:12:12 +0800364 BUG_ON(list_empty(&info->rinfo->indirect_pages));
365 indirect_page = list_first_entry(&info->rinfo->indirect_pages,
Julien Grall4f503fb2015-07-01 14:10:38 +0100366 struct page, lru);
367 list_del(&indirect_page->lru);
368 gnt_list_entry->page = indirect_page;
Roger Pau Monnebfe11d62013-10-29 18:31:14 +0100369 }
Julien Grall4f503fb2015-07-01 14:10:38 +0100370 grant_foreign_access(gnt_list_entry, info);
371
Roger Pau Monne9c1e0502013-03-18 17:49:35 +0100372 return gnt_list_entry;
373}
374
Konrad Rzeszutek Wilk6878c322012-05-25 17:34:51 -0400375static const char *op_name(int op)
376{
377 static const char *const names[] = {
378 [BLKIF_OP_READ] = "read",
379 [BLKIF_OP_WRITE] = "write",
380 [BLKIF_OP_WRITE_BARRIER] = "barrier",
381 [BLKIF_OP_FLUSH_DISKCACHE] = "flush",
382 [BLKIF_OP_DISCARD] = "discard" };
383
384 if (op < 0 || op >= ARRAY_SIZE(names))
385 return "unknown";
386
387 if (!names[op])
388 return "reserved";
389
390 return names[op];
391}
Jan Beulich0e345822010-08-07 18:28:55 +0200392static int xlbd_reserve_minors(unsigned int minor, unsigned int nr)
393{
394 unsigned int end = minor + nr;
395 int rc;
396
397 if (end > nr_minors) {
398 unsigned long *bitmap, *old;
399
Thomas Meyerf0941482011-11-29 22:08:00 +0100400 bitmap = kcalloc(BITS_TO_LONGS(end), sizeof(*bitmap),
Jan Beulich0e345822010-08-07 18:28:55 +0200401 GFP_KERNEL);
402 if (bitmap == NULL)
403 return -ENOMEM;
404
405 spin_lock(&minor_lock);
406 if (end > nr_minors) {
407 old = minors;
408 memcpy(bitmap, minors,
409 BITS_TO_LONGS(nr_minors) * sizeof(*bitmap));
410 minors = bitmap;
411 nr_minors = BITS_TO_LONGS(end) * BITS_PER_LONG;
412 } else
413 old = bitmap;
414 spin_unlock(&minor_lock);
415 kfree(old);
416 }
417
418 spin_lock(&minor_lock);
419 if (find_next_bit(minors, end, minor) >= end) {
Akinobu Mita34ae2e42012-01-21 00:15:26 +0900420 bitmap_set(minors, minor, nr);
Jan Beulich0e345822010-08-07 18:28:55 +0200421 rc = 0;
422 } else
423 rc = -EBUSY;
424 spin_unlock(&minor_lock);
425
426 return rc;
427}
428
429static void xlbd_release_minors(unsigned int minor, unsigned int nr)
430{
431 unsigned int end = minor + nr;
432
433 BUG_ON(end > nr_minors);
434 spin_lock(&minor_lock);
Akinobu Mita34ae2e42012-01-21 00:15:26 +0900435 bitmap_clear(minors, minor, nr);
Jan Beulich0e345822010-08-07 18:28:55 +0200436 spin_unlock(&minor_lock);
437}
438
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700439static void blkif_restart_queue_callback(void *arg)
440{
Bob Liu81f35162015-11-14 11:12:11 +0800441 struct blkfront_ring_info *rinfo = (struct blkfront_ring_info *)arg;
442 schedule_work(&rinfo->work);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700443}
444
Harvey Harrisonafe42d72008-04-29 00:59:47 -0700445static int blkif_getgeo(struct block_device *bd, struct hd_geometry *hg)
Ian Campbell597592d2008-02-21 13:03:45 -0800446{
447 /* We don't have real geometry info, but let's at least return
448 values consistent with the size of the device */
449 sector_t nsect = get_capacity(bd->bd_disk);
450 sector_t cylinders = nsect;
451
452 hg->heads = 0xff;
453 hg->sectors = 0x3f;
454 sector_div(cylinders, hg->heads * hg->sectors);
455 hg->cylinders = cylinders;
456 if ((sector_t)(hg->cylinders + 1) * hg->heads * hg->sectors < nsect)
457 hg->cylinders = 0xffff;
458 return 0;
459}
460
Al Viroa63c8482008-03-02 10:23:47 -0500461static int blkif_ioctl(struct block_device *bdev, fmode_t mode,
Adrian Bunk62aa00542008-08-04 11:59:05 +0200462 unsigned command, unsigned long argument)
Christian Limpach440a01a2008-06-17 10:47:08 +0200463{
Al Viroa63c8482008-03-02 10:23:47 -0500464 struct blkfront_info *info = bdev->bd_disk->private_data;
Christian Limpach440a01a2008-06-17 10:47:08 +0200465 int i;
466
467 dev_dbg(&info->xbdev->dev, "command: 0x%x, argument: 0x%lx\n",
468 command, (long)argument);
469
470 switch (command) {
471 case CDROMMULTISESSION:
472 dev_dbg(&info->xbdev->dev, "FIXME: support multisession CDs later\n");
473 for (i = 0; i < sizeof(struct cdrom_multisession); i++)
474 if (put_user(0, (char __user *)(argument + i)))
475 return -EFAULT;
476 return 0;
477
478 case CDROM_GET_CAPABILITY: {
479 struct gendisk *gd = info->gd;
480 if (gd->flags & GENHD_FL_CD)
481 return 0;
482 return -EINVAL;
483 }
484
485 default:
486 /*printk(KERN_ALERT "ioctl %08x not supported by Xen blkdev\n",
487 command);*/
488 return -EINVAL; /* same return as native Linux */
489 }
490
491 return 0;
492}
493
Bob Liu81f35162015-11-14 11:12:11 +0800494static int blkif_queue_discard_req(struct request *req, struct blkfront_ring_info *rinfo)
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700495{
Bob Liu81f35162015-11-14 11:12:11 +0800496 struct blkfront_info *info = rinfo->dev_info;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700497 struct blkif_request *ring_req;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700498 unsigned long id;
Julien Grall33204662015-06-29 17:35:24 +0100499
500 /* Fill out a communications ring structure. */
Bob Liu81f35162015-11-14 11:12:11 +0800501 ring_req = RING_GET_REQUEST(&rinfo->ring, rinfo->ring.req_prod_pvt);
502 id = get_id_from_freelist(rinfo);
503 rinfo->shadow[id].request = req;
Julien Grall33204662015-06-29 17:35:24 +0100504
505 ring_req->operation = BLKIF_OP_DISCARD;
506 ring_req->u.discard.nr_sectors = blk_rq_sectors(req);
507 ring_req->u.discard.id = id;
508 ring_req->u.discard.sector_number = (blkif_sector_t)blk_rq_pos(req);
509 if ((req->cmd_flags & REQ_SECURE) && info->feature_secdiscard)
510 ring_req->u.discard.flag = BLKIF_DISCARD_SECURE;
511 else
512 ring_req->u.discard.flag = 0;
513
Bob Liu81f35162015-11-14 11:12:11 +0800514 rinfo->ring.req_prod_pvt++;
Julien Grall33204662015-06-29 17:35:24 +0100515
516 /* Keep a private copy so we can reissue requests when recovering. */
Bob Liu81f35162015-11-14 11:12:11 +0800517 rinfo->shadow[id].req = *ring_req;
Julien Grall33204662015-06-29 17:35:24 +0100518
519 return 0;
520}
521
Julien Grallc004a6f2015-07-22 16:44:54 +0100522struct setup_rw_req {
523 unsigned int grant_idx;
524 struct blkif_request_segment *segments;
Bob Liu81f35162015-11-14 11:12:11 +0800525 struct blkfront_ring_info *rinfo;
Julien Grallc004a6f2015-07-22 16:44:54 +0100526 struct blkif_request *ring_req;
527 grant_ref_t gref_head;
528 unsigned int id;
529 /* Only used when persistent grant is used and it's a read request */
530 bool need_copy;
531 unsigned int bvec_off;
532 char *bvec_data;
533};
534
535static void blkif_setup_rw_req_grant(unsigned long gfn, unsigned int offset,
536 unsigned int len, void *data)
537{
538 struct setup_rw_req *setup = data;
539 int n, ref;
540 struct grant *gnt_list_entry;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700541 unsigned int fsect, lsect;
Julien Grallc004a6f2015-07-22 16:44:54 +0100542 /* Convenient aliases */
543 unsigned int grant_idx = setup->grant_idx;
544 struct blkif_request *ring_req = setup->ring_req;
Bob Liu81f35162015-11-14 11:12:11 +0800545 struct blkfront_ring_info *rinfo = setup->rinfo;
546 struct blkfront_info *info = rinfo->dev_info;
547 struct blk_shadow *shadow = &rinfo->shadow[setup->id];
Julien Grallc004a6f2015-07-22 16:44:54 +0100548
549 if ((ring_req->operation == BLKIF_OP_INDIRECT) &&
550 (grant_idx % GRANTS_PER_INDIRECT_FRAME == 0)) {
551 if (setup->segments)
552 kunmap_atomic(setup->segments);
553
554 n = grant_idx / GRANTS_PER_INDIRECT_FRAME;
555 gnt_list_entry = get_indirect_grant(&setup->gref_head, info);
556 shadow->indirect_grants[n] = gnt_list_entry;
557 setup->segments = kmap_atomic(gnt_list_entry->page);
558 ring_req->u.indirect.indirect_grefs[n] = gnt_list_entry->gref;
559 }
560
561 gnt_list_entry = get_grant(&setup->gref_head, gfn, info);
562 ref = gnt_list_entry->gref;
563 shadow->grants_used[grant_idx] = gnt_list_entry;
564
565 if (setup->need_copy) {
566 void *shared_data;
567
568 shared_data = kmap_atomic(gnt_list_entry->page);
569 /*
570 * this does not wipe data stored outside the
571 * range sg->offset..sg->offset+sg->length.
572 * Therefore, blkback *could* see data from
573 * previous requests. This is OK as long as
574 * persistent grants are shared with just one
575 * domain. It may need refactoring if this
576 * changes
577 */
578 memcpy(shared_data + offset,
579 setup->bvec_data + setup->bvec_off,
580 len);
581
582 kunmap_atomic(shared_data);
583 setup->bvec_off += len;
584 }
585
586 fsect = offset >> 9;
587 lsect = fsect + (len >> 9) - 1;
588 if (ring_req->operation != BLKIF_OP_INDIRECT) {
589 ring_req->u.rw.seg[grant_idx] =
590 (struct blkif_request_segment) {
591 .gref = ref,
592 .first_sect = fsect,
593 .last_sect = lsect };
594 } else {
595 setup->segments[grant_idx % GRANTS_PER_INDIRECT_FRAME] =
596 (struct blkif_request_segment) {
597 .gref = ref,
598 .first_sect = fsect,
599 .last_sect = lsect };
600 }
601
602 (setup->grant_idx)++;
603}
604
Bob Liu81f35162015-11-14 11:12:11 +0800605static int blkif_queue_rw_req(struct request *req, struct blkfront_ring_info *rinfo)
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700606{
Bob Liu81f35162015-11-14 11:12:11 +0800607 struct blkfront_info *info = rinfo->dev_info;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700608 struct blkif_request *ring_req;
609 unsigned long id;
Julien Grallc004a6f2015-07-22 16:44:54 +0100610 int i;
611 struct setup_rw_req setup = {
612 .grant_idx = 0,
613 .segments = NULL,
Bob Liu81f35162015-11-14 11:12:11 +0800614 .rinfo = rinfo,
Julien Grallc004a6f2015-07-22 16:44:54 +0100615 .need_copy = rq_data_dir(req) && info->feature_persistent,
616 };
Roger Pau Monne0a8704a52012-10-24 18:58:45 +0200617
618 /*
619 * Used to store if we are able to queue the request by just using
620 * existing persistent grants, or if we have to get new grants,
621 * as there are not sufficiently many free.
622 */
Jens Axboe9e973e62009-02-24 08:10:09 +0100623 struct scatterlist *sg;
Julien Grallc004a6f2015-07-22 16:44:54 +0100624 int num_sg, max_grefs, num_grant;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700625
Julien Grallc004a6f2015-07-22 16:44:54 +0100626 max_grefs = req->nr_phys_segments * GRANTS_PER_PSEG;
Roger Pau Monnec47206e2013-08-12 12:53:43 +0200627 if (max_grefs > BLKIF_MAX_SEGMENTS_PER_REQUEST)
628 /*
629 * If we are using indirect segments we need to account
630 * for the indirect grefs used in the request.
631 */
Julien Grallc004a6f2015-07-22 16:44:54 +0100632 max_grefs += INDIRECT_GREFS(max_grefs);
Roger Pau Monne402b27f2013-04-18 16:06:54 +0200633
Bob Liu3df0e502015-11-14 11:12:12 +0800634 /*
635 * We have to reserve 'max_grefs' grants because persistent
636 * grants are shared by all rings.
637 */
638 if (max_grefs > 0)
639 if (gnttab_alloc_grant_references(max_grefs, &setup.gref_head) < 0) {
Roger Pau Monne0a8704a52012-10-24 18:58:45 +0200640 gnttab_request_free_callback(
Bob Liu81f35162015-11-14 11:12:11 +0800641 &rinfo->callback,
Roger Pau Monne0a8704a52012-10-24 18:58:45 +0200642 blkif_restart_queue_callback,
Bob Liu81f35162015-11-14 11:12:11 +0800643 rinfo,
Roger Pau Monne402b27f2013-04-18 16:06:54 +0200644 max_grefs);
Roger Pau Monne0a8704a52012-10-24 18:58:45 +0200645 return 1;
646 }
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700647
648 /* Fill out a communications ring structure. */
Bob Liu81f35162015-11-14 11:12:11 +0800649 ring_req = RING_GET_REQUEST(&rinfo->ring, rinfo->ring.req_prod_pvt);
650 id = get_id_from_freelist(rinfo);
651 rinfo->shadow[id].request = req;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700652
Julien Grall33204662015-06-29 17:35:24 +0100653 BUG_ON(info->max_indirect_segments == 0 &&
Julien Grallc004a6f2015-07-22 16:44:54 +0100654 GREFS(req->nr_phys_segments) > BLKIF_MAX_SEGMENTS_PER_REQUEST);
Julien Grall33204662015-06-29 17:35:24 +0100655 BUG_ON(info->max_indirect_segments &&
Julien Grallc004a6f2015-07-22 16:44:54 +0100656 GREFS(req->nr_phys_segments) > info->max_indirect_segments);
657
Bob Liu81f35162015-11-14 11:12:11 +0800658 num_sg = blk_rq_map_sg(req->q, req, rinfo->shadow[id].sg);
Julien Grallc004a6f2015-07-22 16:44:54 +0100659 num_grant = 0;
660 /* Calculate the number of grant used */
Bob Liu81f35162015-11-14 11:12:11 +0800661 for_each_sg(rinfo->shadow[id].sg, sg, num_sg, i)
Julien Grallc004a6f2015-07-22 16:44:54 +0100662 num_grant += gnttab_count_grant(sg->offset, sg->length);
663
Julien Grall33204662015-06-29 17:35:24 +0100664 ring_req->u.rw.id = id;
Bob Liu81f35162015-11-14 11:12:11 +0800665 rinfo->shadow[id].num_sg = num_sg;
Julien Grallc004a6f2015-07-22 16:44:54 +0100666 if (num_grant > BLKIF_MAX_SEGMENTS_PER_REQUEST) {
Julien Grall33204662015-06-29 17:35:24 +0100667 /*
668 * The indirect operation can only be a BLKIF_OP_READ or
669 * BLKIF_OP_WRITE
670 */
671 BUG_ON(req->cmd_flags & (REQ_FLUSH | REQ_FUA));
672 ring_req->operation = BLKIF_OP_INDIRECT;
673 ring_req->u.indirect.indirect_op = rq_data_dir(req) ?
674 BLKIF_OP_WRITE : BLKIF_OP_READ;
675 ring_req->u.indirect.sector_number = (blkif_sector_t)blk_rq_pos(req);
676 ring_req->u.indirect.handle = info->handle;
Julien Grallc004a6f2015-07-22 16:44:54 +0100677 ring_req->u.indirect.nr_segments = num_grant;
Li Dongyanged30bf32011-09-01 18:39:09 +0800678 } else {
Julien Grall33204662015-06-29 17:35:24 +0100679 ring_req->u.rw.sector_number = (blkif_sector_t)blk_rq_pos(req);
680 ring_req->u.rw.handle = info->handle;
681 ring_req->operation = rq_data_dir(req) ?
682 BLKIF_OP_WRITE : BLKIF_OP_READ;
683 if (req->cmd_flags & (REQ_FLUSH | REQ_FUA)) {
Roger Pau Monne402b27f2013-04-18 16:06:54 +0200684 /*
Julien Grall33204662015-06-29 17:35:24 +0100685 * Ideally we can do an unordered flush-to-disk.
686 * In case the backend onlysupports barriers, use that.
687 * A barrier request a superset of FUA, so we can
688 * implement it the same way. (It's also a FLUSH+FUA,
689 * since it is guaranteed ordered WRT previous writes.)
Roger Pau Monne402b27f2013-04-18 16:06:54 +0200690 */
Julien Grall33204662015-06-29 17:35:24 +0100691 switch (info->feature_flush &
692 ((REQ_FLUSH|REQ_FUA))) {
693 case REQ_FLUSH|REQ_FUA:
694 ring_req->operation =
695 BLKIF_OP_WRITE_BARRIER;
696 break;
697 case REQ_FLUSH:
698 ring_req->operation =
699 BLKIF_OP_FLUSH_DISKCACHE;
700 break;
701 default:
702 ring_req->operation = 0;
Roger Pau Monne402b27f2013-04-18 16:06:54 +0200703 }
Li Dongyanged30bf32011-09-01 18:39:09 +0800704 }
Julien Grallc004a6f2015-07-22 16:44:54 +0100705 ring_req->u.rw.nr_segments = num_grant;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700706 }
707
Julien Grallc004a6f2015-07-22 16:44:54 +0100708 setup.ring_req = ring_req;
709 setup.id = id;
Bob Liu81f35162015-11-14 11:12:11 +0800710 for_each_sg(rinfo->shadow[id].sg, sg, num_sg, i) {
Julien Grallc004a6f2015-07-22 16:44:54 +0100711 BUG_ON(sg->offset + sg->length > PAGE_SIZE);
Julien Grall33204662015-06-29 17:35:24 +0100712
Julien Grallc004a6f2015-07-22 16:44:54 +0100713 if (setup.need_copy) {
714 setup.bvec_off = sg->offset;
715 setup.bvec_data = kmap_atomic(sg_page(sg));
Julien Grall33204662015-06-29 17:35:24 +0100716 }
717
Julien Grallc004a6f2015-07-22 16:44:54 +0100718 gnttab_foreach_grant_in_range(sg_page(sg),
719 sg->offset,
720 sg->length,
721 blkif_setup_rw_req_grant,
722 &setup);
Julien Grall33204662015-06-29 17:35:24 +0100723
Julien Grallc004a6f2015-07-22 16:44:54 +0100724 if (setup.need_copy)
725 kunmap_atomic(setup.bvec_data);
Julien Grall33204662015-06-29 17:35:24 +0100726 }
Julien Grallc004a6f2015-07-22 16:44:54 +0100727 if (setup.segments)
728 kunmap_atomic(setup.segments);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700729
Bob Liu81f35162015-11-14 11:12:11 +0800730 rinfo->ring.req_prod_pvt++;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700731
732 /* Keep a private copy so we can reissue requests when recovering. */
Bob Liu81f35162015-11-14 11:12:11 +0800733 rinfo->shadow[id].req = *ring_req;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700734
Bob Liu3df0e502015-11-14 11:12:12 +0800735 if (max_grefs > 0)
Julien Grallc004a6f2015-07-22 16:44:54 +0100736 gnttab_free_grant_references(setup.gref_head);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700737
738 return 0;
739}
740
Julien Grall33204662015-06-29 17:35:24 +0100741/*
742 * Generate a Xen blkfront IO request from a blk layer request. Reads
743 * and writes are handled as expected.
744 *
745 * @req: a request struct
746 */
Bob Liu81f35162015-11-14 11:12:11 +0800747static int blkif_queue_request(struct request *req, struct blkfront_ring_info *rinfo)
Julien Grall33204662015-06-29 17:35:24 +0100748{
Bob Liu81f35162015-11-14 11:12:11 +0800749 if (unlikely(rinfo->dev_info->connected != BLKIF_STATE_CONNECTED))
Julien Grall33204662015-06-29 17:35:24 +0100750 return 1;
751
752 if (unlikely(req->cmd_flags & (REQ_DISCARD | REQ_SECURE)))
Bob Liu81f35162015-11-14 11:12:11 +0800753 return blkif_queue_discard_req(req, rinfo);
Julien Grall33204662015-06-29 17:35:24 +0100754 else
Bob Liu81f35162015-11-14 11:12:11 +0800755 return blkif_queue_rw_req(req, rinfo);
Julien Grall33204662015-06-29 17:35:24 +0100756}
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700757
Bob Liu81f35162015-11-14 11:12:11 +0800758static inline void flush_requests(struct blkfront_ring_info *rinfo)
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700759{
760 int notify;
761
Bob Liu81f35162015-11-14 11:12:11 +0800762 RING_PUSH_REQUESTS_AND_CHECK_NOTIFY(&rinfo->ring, notify);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700763
764 if (notify)
Bob Liu81f35162015-11-14 11:12:11 +0800765 notify_remote_via_irq(rinfo->irq);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700766}
767
Vitaly Kuznetsovad42d392014-12-01 14:01:13 +0100768static inline bool blkif_request_flush_invalid(struct request *req,
769 struct blkfront_info *info)
Arianna Avanzini0f1ca652014-08-22 13:20:02 +0200770{
771 return ((req->cmd_type != REQ_TYPE_FS) ||
Vitaly Kuznetsovad42d392014-12-01 14:01:13 +0100772 ((req->cmd_flags & REQ_FLUSH) &&
773 !(info->feature_flush & REQ_FLUSH)) ||
774 ((req->cmd_flags & REQ_FUA) &&
775 !(info->feature_flush & REQ_FUA)));
Arianna Avanzini0f1ca652014-08-22 13:20:02 +0200776}
777
Bob Liu907c3eb2015-07-13 17:55:24 +0800778static int blkif_queue_rq(struct blk_mq_hw_ctx *hctx,
779 const struct blk_mq_queue_data *qd)
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700780{
Bob Liu11659562015-11-14 11:12:13 +0800781 unsigned long flags;
Bob Liu81f35162015-11-14 11:12:11 +0800782 struct blkfront_ring_info *rinfo = (struct blkfront_ring_info *)hctx->driver_data;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700783
Bob Liu907c3eb2015-07-13 17:55:24 +0800784 blk_mq_start_request(qd->rq);
Bob Liu11659562015-11-14 11:12:13 +0800785 spin_lock_irqsave(&rinfo->ring_lock, flags);
Bob Liu81f35162015-11-14 11:12:11 +0800786 if (RING_FULL(&rinfo->ring))
Bob Liu907c3eb2015-07-13 17:55:24 +0800787 goto out_busy;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700788
Bob Liu81f35162015-11-14 11:12:11 +0800789 if (blkif_request_flush_invalid(qd->rq, rinfo->dev_info))
Bob Liu907c3eb2015-07-13 17:55:24 +0800790 goto out_err;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700791
Bob Liu81f35162015-11-14 11:12:11 +0800792 if (blkif_queue_request(qd->rq, rinfo))
Bob Liu907c3eb2015-07-13 17:55:24 +0800793 goto out_busy;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700794
Bob Liu81f35162015-11-14 11:12:11 +0800795 flush_requests(rinfo);
Bob Liu11659562015-11-14 11:12:13 +0800796 spin_unlock_irqrestore(&rinfo->ring_lock, flags);
Bob Liu907c3eb2015-07-13 17:55:24 +0800797 return BLK_MQ_RQ_QUEUE_OK;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700798
Bob Liu907c3eb2015-07-13 17:55:24 +0800799out_err:
Bob Liu11659562015-11-14 11:12:13 +0800800 spin_unlock_irqrestore(&rinfo->ring_lock, flags);
Bob Liu907c3eb2015-07-13 17:55:24 +0800801 return BLK_MQ_RQ_QUEUE_ERROR;
Tejun Heo296b2f62009-05-08 11:54:15 +0900802
Bob Liu907c3eb2015-07-13 17:55:24 +0800803out_busy:
Bob Liu11659562015-11-14 11:12:13 +0800804 spin_unlock_irqrestore(&rinfo->ring_lock, flags);
Bob Liu907c3eb2015-07-13 17:55:24 +0800805 blk_mq_stop_hw_queue(hctx);
806 return BLK_MQ_RQ_QUEUE_BUSY;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700807}
808
Bob Liu81f35162015-11-14 11:12:11 +0800809static int blk_mq_init_hctx(struct blk_mq_hw_ctx *hctx, void *data,
810 unsigned int index)
811{
812 struct blkfront_info *info = (struct blkfront_info *)data;
813
Bob Liu3df0e502015-11-14 11:12:12 +0800814 BUG_ON(info->nr_rings <= index);
815 hctx->driver_data = &info->rinfo[index];
Bob Liu81f35162015-11-14 11:12:11 +0800816 return 0;
817}
818
Bob Liu907c3eb2015-07-13 17:55:24 +0800819static struct blk_mq_ops blkfront_mq_ops = {
820 .queue_rq = blkif_queue_rq,
821 .map_queue = blk_mq_map_queue,
Bob Liu81f35162015-11-14 11:12:11 +0800822 .init_hctx = blk_mq_init_hctx,
Bob Liu907c3eb2015-07-13 17:55:24 +0800823};
824
Roger Pau Monne402b27f2013-04-18 16:06:54 +0200825static int xlvbd_init_blk_queue(struct gendisk *gd, u16 sector_size,
Stefan Bader7c4d7d72013-05-13 16:28:15 +0200826 unsigned int physical_sector_size,
Roger Pau Monne402b27f2013-04-18 16:06:54 +0200827 unsigned int segments)
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700828{
Jens Axboe165125e2007-07-24 09:28:11 +0200829 struct request_queue *rq;
Li Dongyanged30bf32011-09-01 18:39:09 +0800830 struct blkfront_info *info = gd->private_data;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700831
Bob Liu907c3eb2015-07-13 17:55:24 +0800832 memset(&info->tag_set, 0, sizeof(info->tag_set));
833 info->tag_set.ops = &blkfront_mq_ops;
Bob Liu28d949b2015-11-14 11:12:14 +0800834 info->tag_set.nr_hw_queues = info->nr_rings;
Bob Liu907c3eb2015-07-13 17:55:24 +0800835 info->tag_set.queue_depth = BLK_RING_SIZE(info);
836 info->tag_set.numa_node = NUMA_NO_NODE;
837 info->tag_set.flags = BLK_MQ_F_SHOULD_MERGE | BLK_MQ_F_SG_MERGE;
838 info->tag_set.cmd_size = 0;
839 info->tag_set.driver_data = info;
840
841 if (blk_mq_alloc_tag_set(&info->tag_set))
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700842 return -1;
Bob Liu907c3eb2015-07-13 17:55:24 +0800843 rq = blk_mq_init_queue(&info->tag_set);
844 if (IS_ERR(rq)) {
845 blk_mq_free_tag_set(&info->tag_set);
846 return -1;
847 }
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700848
Fernando Luis Vázquez Cao66d352e2008-10-27 18:45:54 +0900849 queue_flag_set_unlocked(QUEUE_FLAG_VIRT, rq);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700850
Li Dongyanged30bf32011-09-01 18:39:09 +0800851 if (info->feature_discard) {
852 queue_flag_set_unlocked(QUEUE_FLAG_DISCARD, rq);
853 blk_queue_max_discard_sectors(rq, get_capacity(gd));
854 rq->limits.discard_granularity = info->discard_granularity;
855 rq->limits.discard_alignment = info->discard_alignment;
Konrad Rzeszutek Wilk5ea42982011-10-12 16:23:30 -0400856 if (info->feature_secdiscard)
857 queue_flag_set_unlocked(QUEUE_FLAG_SECDISCARD, rq);
Li Dongyanged30bf32011-09-01 18:39:09 +0800858 }
859
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700860 /* Hard sector size and max sectors impersonate the equiv. hardware. */
Martin K. Petersene1defc42009-05-22 17:17:49 -0400861 blk_queue_logical_block_size(rq, sector_size);
Stefan Bader7c4d7d72013-05-13 16:28:15 +0200862 blk_queue_physical_block_size(rq, physical_sector_size);
Julien Grallc004a6f2015-07-22 16:44:54 +0100863 blk_queue_max_hw_sectors(rq, (segments * XEN_PAGE_SIZE) / 512);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700864
865 /* Each segment in a request is up to an aligned page in size. */
866 blk_queue_segment_boundary(rq, PAGE_SIZE - 1);
867 blk_queue_max_segment_size(rq, PAGE_SIZE);
868
869 /* Ensure a merged request will fit in a single I/O ring slot. */
Julien Grallc004a6f2015-07-22 16:44:54 +0100870 blk_queue_max_segments(rq, segments / GRANTS_PER_PSEG);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700871
872 /* Make sure buffer addresses are sector-aligned. */
873 blk_queue_dma_alignment(rq, 511);
874
Ian Campbell1c91fe12008-06-17 10:47:08 +0200875 /* Make sure we don't use bounce buffers. */
876 blk_queue_bounce_limit(rq, BLK_BOUNCE_ANY);
877
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700878 gd->queue = rq;
879
880 return 0;
881}
882
Vitaly Kuznetsovfdf9b962014-12-09 15:25:10 +0100883static const char *flush_info(unsigned int feature_flush)
884{
885 switch (feature_flush & ((REQ_FLUSH | REQ_FUA))) {
886 case REQ_FLUSH|REQ_FUA:
887 return "barrier: enabled;";
888 case REQ_FLUSH:
889 return "flush diskcache: enabled;";
890 default:
891 return "barrier or flush: disabled;";
892 }
893}
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700894
Tejun Heo4913efe2010-09-03 11:56:16 +0200895static void xlvbd_flush(struct blkfront_info *info)
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700896{
Tejun Heo4913efe2010-09-03 11:56:16 +0200897 blk_queue_flush(info->rq, info->feature_flush);
Vitaly Kuznetsovfdf9b962014-12-09 15:25:10 +0100898 pr_info("blkfront: %s: %s %s %s %s %s\n",
899 info->gd->disk_name, flush_info(info->feature_flush),
900 "persistent grants:", info->feature_persistent ?
901 "enabled;" : "disabled;", "indirect descriptors:",
902 info->max_indirect_segments ? "enabled;" : "disabled;");
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700903}
904
Stefano Stabellinic80a4202010-12-02 17:55:00 +0000905static int xen_translate_vdev(int vdevice, int *minor, unsigned int *offset)
906{
907 int major;
908 major = BLKIF_MAJOR(vdevice);
909 *minor = BLKIF_MINOR(vdevice);
910 switch (major) {
911 case XEN_IDE0_MAJOR:
912 *offset = (*minor / 64) + EMULATED_HD_DISK_NAME_OFFSET;
913 *minor = ((*minor / 64) * PARTS_PER_DISK) +
914 EMULATED_HD_DISK_MINOR_OFFSET;
915 break;
916 case XEN_IDE1_MAJOR:
917 *offset = (*minor / 64) + 2 + EMULATED_HD_DISK_NAME_OFFSET;
918 *minor = (((*minor / 64) + 2) * PARTS_PER_DISK) +
919 EMULATED_HD_DISK_MINOR_OFFSET;
920 break;
921 case XEN_SCSI_DISK0_MAJOR:
922 *offset = (*minor / PARTS_PER_DISK) + EMULATED_SD_DISK_NAME_OFFSET;
923 *minor = *minor + EMULATED_SD_DISK_MINOR_OFFSET;
924 break;
925 case XEN_SCSI_DISK1_MAJOR:
926 case XEN_SCSI_DISK2_MAJOR:
927 case XEN_SCSI_DISK3_MAJOR:
928 case XEN_SCSI_DISK4_MAJOR:
929 case XEN_SCSI_DISK5_MAJOR:
930 case XEN_SCSI_DISK6_MAJOR:
931 case XEN_SCSI_DISK7_MAJOR:
932 *offset = (*minor / PARTS_PER_DISK) +
933 ((major - XEN_SCSI_DISK1_MAJOR + 1) * 16) +
934 EMULATED_SD_DISK_NAME_OFFSET;
935 *minor = *minor +
936 ((major - XEN_SCSI_DISK1_MAJOR + 1) * 16 * PARTS_PER_DISK) +
937 EMULATED_SD_DISK_MINOR_OFFSET;
938 break;
939 case XEN_SCSI_DISK8_MAJOR:
940 case XEN_SCSI_DISK9_MAJOR:
941 case XEN_SCSI_DISK10_MAJOR:
942 case XEN_SCSI_DISK11_MAJOR:
943 case XEN_SCSI_DISK12_MAJOR:
944 case XEN_SCSI_DISK13_MAJOR:
945 case XEN_SCSI_DISK14_MAJOR:
946 case XEN_SCSI_DISK15_MAJOR:
947 *offset = (*minor / PARTS_PER_DISK) +
948 ((major - XEN_SCSI_DISK8_MAJOR + 8) * 16) +
949 EMULATED_SD_DISK_NAME_OFFSET;
950 *minor = *minor +
951 ((major - XEN_SCSI_DISK8_MAJOR + 8) * 16 * PARTS_PER_DISK) +
952 EMULATED_SD_DISK_MINOR_OFFSET;
953 break;
954 case XENVBD_MAJOR:
955 *offset = *minor / PARTS_PER_DISK;
956 break;
957 default:
958 printk(KERN_WARNING "blkfront: your disk configuration is "
959 "incorrect, please use an xvd device instead\n");
960 return -ENODEV;
961 }
962 return 0;
963}
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700964
Jan Beuliche77c78c2012-04-05 16:37:22 +0100965static char *encode_disk_name(char *ptr, unsigned int n)
966{
967 if (n >= 26)
968 ptr = encode_disk_name(ptr, n / 26 - 1);
969 *ptr = 'a' + n % 26;
970 return ptr + 1;
971}
972
Chris Lalancette9246b5f2008-09-17 14:30:32 -0700973static int xlvbd_alloc_gendisk(blkif_sector_t capacity,
974 struct blkfront_info *info,
Stefan Bader7c4d7d72013-05-13 16:28:15 +0200975 u16 vdisk_info, u16 sector_size,
976 unsigned int physical_sector_size)
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700977{
978 struct gendisk *gd;
979 int nr_minors = 1;
Stefano Stabellinic80a4202010-12-02 17:55:00 +0000980 int err;
Chris Lalancette9246b5f2008-09-17 14:30:32 -0700981 unsigned int offset;
982 int minor;
983 int nr_parts;
Jan Beuliche77c78c2012-04-05 16:37:22 +0100984 char *ptr;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700985
986 BUG_ON(info->gd != NULL);
987 BUG_ON(info->rq != NULL);
988
Chris Lalancette9246b5f2008-09-17 14:30:32 -0700989 if ((info->vdevice>>EXT_SHIFT) > 1) {
990 /* this is above the extended range; something is wrong */
991 printk(KERN_WARNING "blkfront: vdevice 0x%x is above the extended range; ignoring\n", info->vdevice);
992 return -ENODEV;
993 }
994
995 if (!VDEV_IS_EXTENDED(info->vdevice)) {
Stefano Stabellinic80a4202010-12-02 17:55:00 +0000996 err = xen_translate_vdev(info->vdevice, &minor, &offset);
997 if (err)
998 return err;
999 nr_parts = PARTS_PER_DISK;
Chris Lalancette9246b5f2008-09-17 14:30:32 -07001000 } else {
1001 minor = BLKIF_MINOR_EXT(info->vdevice);
1002 nr_parts = PARTS_PER_EXT_DISK;
Stefano Stabellinic80a4202010-12-02 17:55:00 +00001003 offset = minor / nr_parts;
Stefan Bader89153b52011-07-14 15:30:37 +02001004 if (xen_hvm_domain() && offset < EMULATED_HD_DISK_NAME_OFFSET + 4)
Stefano Stabellinic80a4202010-12-02 17:55:00 +00001005 printk(KERN_WARNING "blkfront: vdevice 0x%x might conflict with "
1006 "emulated IDE disks,\n\t choose an xvd device name"
1007 "from xvde on\n", info->vdevice);
Chris Lalancette9246b5f2008-09-17 14:30:32 -07001008 }
Jan Beuliche77c78c2012-04-05 16:37:22 +01001009 if (minor >> MINORBITS) {
1010 pr_warn("blkfront: %#x's minor (%#x) out of range; ignoring\n",
1011 info->vdevice, minor);
1012 return -ENODEV;
1013 }
Chris Lalancette9246b5f2008-09-17 14:30:32 -07001014
1015 if ((minor % nr_parts) == 0)
1016 nr_minors = nr_parts;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001017
Jan Beulich0e345822010-08-07 18:28:55 +02001018 err = xlbd_reserve_minors(minor, nr_minors);
1019 if (err)
1020 goto out;
1021 err = -ENODEV;
1022
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001023 gd = alloc_disk(nr_minors);
1024 if (gd == NULL)
Jan Beulich0e345822010-08-07 18:28:55 +02001025 goto release;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001026
Jan Beuliche77c78c2012-04-05 16:37:22 +01001027 strcpy(gd->disk_name, DEV_NAME);
1028 ptr = encode_disk_name(gd->disk_name + sizeof(DEV_NAME) - 1, offset);
1029 BUG_ON(ptr >= gd->disk_name + DISK_NAME_LEN);
1030 if (nr_minors > 1)
1031 *ptr = 0;
1032 else
1033 snprintf(ptr, gd->disk_name + DISK_NAME_LEN - ptr,
1034 "%d", minor & (nr_parts - 1));
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001035
1036 gd->major = XENVBD_MAJOR;
1037 gd->first_minor = minor;
1038 gd->fops = &xlvbd_block_fops;
1039 gd->private_data = info;
1040 gd->driverfs_dev = &(info->xbdev->dev);
1041 set_capacity(gd, capacity);
1042
Stefan Bader7c4d7d72013-05-13 16:28:15 +02001043 if (xlvbd_init_blk_queue(gd, sector_size, physical_sector_size,
Roger Pau Monne402b27f2013-04-18 16:06:54 +02001044 info->max_indirect_segments ? :
1045 BLKIF_MAX_SEGMENTS_PER_REQUEST)) {
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001046 del_gendisk(gd);
Jan Beulich0e345822010-08-07 18:28:55 +02001047 goto release;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001048 }
1049
1050 info->rq = gd->queue;
1051 info->gd = gd;
1052
Tejun Heo4913efe2010-09-03 11:56:16 +02001053 xlvbd_flush(info);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001054
1055 if (vdisk_info & VDISK_READONLY)
1056 set_disk_ro(gd, 1);
1057
1058 if (vdisk_info & VDISK_REMOVABLE)
1059 gd->flags |= GENHD_FL_REMOVABLE;
1060
1061 if (vdisk_info & VDISK_CDROM)
1062 gd->flags |= GENHD_FL_CD;
1063
1064 return 0;
1065
Jan Beulich0e345822010-08-07 18:28:55 +02001066 release:
1067 xlbd_release_minors(minor, nr_minors);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001068 out:
1069 return err;
1070}
1071
Daniel Stoddena66b5ae2010-08-07 18:33:17 +02001072static void xlvbd_release_gendisk(struct blkfront_info *info)
1073{
Bob Liu3df0e502015-11-14 11:12:12 +08001074 unsigned int minor, nr_minors, i;
Daniel Stoddena66b5ae2010-08-07 18:33:17 +02001075
1076 if (info->rq == NULL)
1077 return;
1078
Daniel Stoddena66b5ae2010-08-07 18:33:17 +02001079 /* No more blkif_request(). */
Bob Liu907c3eb2015-07-13 17:55:24 +08001080 blk_mq_stop_hw_queues(info->rq);
Daniel Stoddena66b5ae2010-08-07 18:33:17 +02001081
Bob Liu3df0e502015-11-14 11:12:12 +08001082 for (i = 0; i < info->nr_rings; i++) {
1083 struct blkfront_ring_info *rinfo = &info->rinfo[i];
Daniel Stoddena66b5ae2010-08-07 18:33:17 +02001084
Bob Liu3df0e502015-11-14 11:12:12 +08001085 /* No more gnttab callback work. */
1086 gnttab_cancel_free_callback(&rinfo->callback);
1087
1088 /* Flush gnttab callback work. Must be done with no locks held. */
1089 flush_work(&rinfo->work);
1090 }
Daniel Stoddena66b5ae2010-08-07 18:33:17 +02001091
1092 del_gendisk(info->gd);
1093
1094 minor = info->gd->first_minor;
1095 nr_minors = info->gd->minors;
1096 xlbd_release_minors(minor, nr_minors);
1097
1098 blk_cleanup_queue(info->rq);
Bob Liu907c3eb2015-07-13 17:55:24 +08001099 blk_mq_free_tag_set(&info->tag_set);
Daniel Stoddena66b5ae2010-08-07 18:33:17 +02001100 info->rq = NULL;
1101
1102 put_disk(info->gd);
1103 info->gd = NULL;
1104}
1105
Bob Liu11659562015-11-14 11:12:13 +08001106/* Already hold rinfo->ring_lock. */
1107static inline void kick_pending_request_queues_locked(struct blkfront_ring_info *rinfo)
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001108{
Bob Liu81f35162015-11-14 11:12:11 +08001109 if (!RING_FULL(&rinfo->ring))
1110 blk_mq_start_stopped_hw_queues(rinfo->dev_info->rq, true);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001111}
1112
Bob Liu11659562015-11-14 11:12:13 +08001113static void kick_pending_request_queues(struct blkfront_ring_info *rinfo)
1114{
1115 unsigned long flags;
1116
1117 spin_lock_irqsave(&rinfo->ring_lock, flags);
1118 kick_pending_request_queues_locked(rinfo);
1119 spin_unlock_irqrestore(&rinfo->ring_lock, flags);
1120}
1121
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001122static void blkif_restart_queue(struct work_struct *work)
1123{
Bob Liu81f35162015-11-14 11:12:11 +08001124 struct blkfront_ring_info *rinfo = container_of(work, struct blkfront_ring_info, work);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001125
Bob Liu81f35162015-11-14 11:12:11 +08001126 if (rinfo->dev_info->connected == BLKIF_STATE_CONNECTED)
1127 kick_pending_request_queues(rinfo);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001128}
1129
Bob Liu3df0e502015-11-14 11:12:12 +08001130static void blkif_free_ring(struct blkfront_ring_info *rinfo)
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001131{
Roger Pau Monne155b7ed2013-03-18 17:49:34 +01001132 struct grant *persistent_gnt;
Bob Liu3df0e502015-11-14 11:12:12 +08001133 struct blkfront_info *info = rinfo->dev_info;
Roger Pau Monne402b27f2013-04-18 16:06:54 +02001134 int i, j, segs;
Roger Pau Monne0a8704a52012-10-24 18:58:45 +02001135
Roger Pau Monnebfe11d62013-10-29 18:31:14 +01001136 /*
1137 * Remove indirect pages, this only happens when using indirect
1138 * descriptors but not persistent grants
1139 */
Bob Liu81f35162015-11-14 11:12:11 +08001140 if (!list_empty(&rinfo->indirect_pages)) {
Roger Pau Monnebfe11d62013-10-29 18:31:14 +01001141 struct page *indirect_page, *n;
1142
1143 BUG_ON(info->feature_persistent);
Bob Liu81f35162015-11-14 11:12:11 +08001144 list_for_each_entry_safe(indirect_page, n, &rinfo->indirect_pages, lru) {
Roger Pau Monnebfe11d62013-10-29 18:31:14 +01001145 list_del(&indirect_page->lru);
1146 __free_page(indirect_page);
1147 }
1148 }
1149
Bob Liu86839c52015-06-03 13:40:03 +08001150 for (i = 0; i < BLK_RING_SIZE(info); i++) {
Roger Pau Monne402b27f2013-04-18 16:06:54 +02001151 /*
1152 * Clear persistent grants present in requests already
1153 * on the shared ring
1154 */
Bob Liu81f35162015-11-14 11:12:11 +08001155 if (!rinfo->shadow[i].request)
Roger Pau Monne402b27f2013-04-18 16:06:54 +02001156 goto free_shadow;
1157
Bob Liu81f35162015-11-14 11:12:11 +08001158 segs = rinfo->shadow[i].req.operation == BLKIF_OP_INDIRECT ?
1159 rinfo->shadow[i].req.u.indirect.nr_segments :
1160 rinfo->shadow[i].req.u.rw.nr_segments;
Roger Pau Monne402b27f2013-04-18 16:06:54 +02001161 for (j = 0; j < segs; j++) {
Bob Liu81f35162015-11-14 11:12:11 +08001162 persistent_gnt = rinfo->shadow[i].grants_used[j];
Roger Pau Monne402b27f2013-04-18 16:06:54 +02001163 gnttab_end_foreign_access(persistent_gnt->gref, 0, 0UL);
Roger Pau Monnebfe11d62013-10-29 18:31:14 +01001164 if (info->feature_persistent)
Julien Gralla7a6df22015-06-30 11:58:51 +01001165 __free_page(persistent_gnt->page);
Roger Pau Monne402b27f2013-04-18 16:06:54 +02001166 kfree(persistent_gnt);
1167 }
1168
Bob Liu81f35162015-11-14 11:12:11 +08001169 if (rinfo->shadow[i].req.operation != BLKIF_OP_INDIRECT)
Roger Pau Monne402b27f2013-04-18 16:06:54 +02001170 /*
1171 * If this is not an indirect operation don't try to
1172 * free indirect segments
1173 */
1174 goto free_shadow;
1175
1176 for (j = 0; j < INDIRECT_GREFS(segs); j++) {
Bob Liu81f35162015-11-14 11:12:11 +08001177 persistent_gnt = rinfo->shadow[i].indirect_grants[j];
Roger Pau Monne402b27f2013-04-18 16:06:54 +02001178 gnttab_end_foreign_access(persistent_gnt->gref, 0, 0UL);
Julien Gralla7a6df22015-06-30 11:58:51 +01001179 __free_page(persistent_gnt->page);
Roger Pau Monne402b27f2013-04-18 16:06:54 +02001180 kfree(persistent_gnt);
1181 }
1182
1183free_shadow:
Bob Liu81f35162015-11-14 11:12:11 +08001184 kfree(rinfo->shadow[i].grants_used);
1185 rinfo->shadow[i].grants_used = NULL;
1186 kfree(rinfo->shadow[i].indirect_grants);
1187 rinfo->shadow[i].indirect_grants = NULL;
1188 kfree(rinfo->shadow[i].sg);
1189 rinfo->shadow[i].sg = NULL;
Roger Pau Monne402b27f2013-04-18 16:06:54 +02001190 }
1191
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001192 /* No more gnttab callback work. */
Bob Liu81f35162015-11-14 11:12:11 +08001193 gnttab_cancel_free_callback(&rinfo->callback);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001194
1195 /* Flush gnttab callback work. Must be done with no locks held. */
Bob Liu81f35162015-11-14 11:12:11 +08001196 flush_work(&rinfo->work);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001197
1198 /* Free resources associated with old device channel. */
Bob Liu86839c52015-06-03 13:40:03 +08001199 for (i = 0; i < info->nr_ring_pages; i++) {
Bob Liu81f35162015-11-14 11:12:11 +08001200 if (rinfo->ring_ref[i] != GRANT_INVALID_REF) {
1201 gnttab_end_foreign_access(rinfo->ring_ref[i], 0, 0);
1202 rinfo->ring_ref[i] = GRANT_INVALID_REF;
Bob Liu86839c52015-06-03 13:40:03 +08001203 }
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001204 }
Bob Liu81f35162015-11-14 11:12:11 +08001205 free_pages((unsigned long)rinfo->ring.sring, get_order(info->nr_ring_pages * PAGE_SIZE));
1206 rinfo->ring.sring = NULL;
Bob Liu86839c52015-06-03 13:40:03 +08001207
Bob Liu81f35162015-11-14 11:12:11 +08001208 if (rinfo->irq)
1209 unbind_from_irqhandler(rinfo->irq, rinfo);
1210 rinfo->evtchn = rinfo->irq = 0;
Bob Liu3df0e502015-11-14 11:12:12 +08001211}
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001212
Bob Liu3df0e502015-11-14 11:12:12 +08001213static void blkif_free(struct blkfront_info *info, int suspend)
1214{
1215 struct grant *persistent_gnt, *n;
1216 unsigned int i;
1217
1218 /* Prevent new requests being issued until we fix things up. */
Bob Liu3df0e502015-11-14 11:12:12 +08001219 info->connected = suspend ?
1220 BLKIF_STATE_SUSPENDED : BLKIF_STATE_DISCONNECTED;
1221 /* No more blkif_request(). */
1222 if (info->rq)
1223 blk_mq_stop_hw_queues(info->rq);
1224
1225 /* Remove all persistent grants */
Bob Liu11659562015-11-14 11:12:13 +08001226 spin_lock_irq(&info->dev_lock);
Bob Liu3df0e502015-11-14 11:12:12 +08001227 if (!list_empty(&info->grants)) {
1228 list_for_each_entry_safe(persistent_gnt, n,
1229 &info->grants, node) {
1230 list_del(&persistent_gnt->node);
1231 if (persistent_gnt->gref != GRANT_INVALID_REF) {
1232 gnttab_end_foreign_access(persistent_gnt->gref,
1233 0, 0UL);
1234 info->persistent_gnts_c--;
1235 }
1236 if (info->feature_persistent)
1237 __free_page(persistent_gnt->page);
1238 kfree(persistent_gnt);
1239 }
1240 }
1241 BUG_ON(info->persistent_gnts_c != 0);
Bob Liu11659562015-11-14 11:12:13 +08001242 spin_unlock_irq(&info->dev_lock);
Bob Liu3df0e502015-11-14 11:12:12 +08001243
1244 for (i = 0; i < info->nr_rings; i++)
1245 blkif_free_ring(&info->rinfo[i]);
1246
1247 kfree(info->rinfo);
1248 info->rinfo = NULL;
1249 info->nr_rings = 0;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001250}
1251
Julien Grallc004a6f2015-07-22 16:44:54 +01001252struct copy_from_grant {
1253 const struct blk_shadow *s;
1254 unsigned int grant_idx;
1255 unsigned int bvec_offset;
1256 char *bvec_data;
1257};
1258
1259static void blkif_copy_from_grant(unsigned long gfn, unsigned int offset,
1260 unsigned int len, void *data)
1261{
1262 struct copy_from_grant *info = data;
1263 char *shared_data;
1264 /* Convenient aliases */
1265 const struct blk_shadow *s = info->s;
1266
1267 shared_data = kmap_atomic(s->grants_used[info->grant_idx]->page);
1268
1269 memcpy(info->bvec_data + info->bvec_offset,
1270 shared_data + offset, len);
1271
1272 info->bvec_offset += len;
1273 info->grant_idx++;
1274
1275 kunmap_atomic(shared_data);
1276}
1277
Bob Liu81f35162015-11-14 11:12:11 +08001278static void blkif_completion(struct blk_shadow *s, struct blkfront_ring_info *rinfo,
Roger Pau Monne0a8704a52012-10-24 18:58:45 +02001279 struct blkif_response *bret)
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001280{
Roger Pau Monned62f6912012-12-07 19:00:31 +01001281 int i = 0;
Roger Pau Monneb7649152013-05-02 10:58:50 +02001282 struct scatterlist *sg;
Julien Grallc004a6f2015-07-22 16:44:54 +01001283 int num_sg, num_grant;
Bob Liu11659562015-11-14 11:12:13 +08001284 unsigned long flags;
Bob Liu81f35162015-11-14 11:12:11 +08001285 struct blkfront_info *info = rinfo->dev_info;
Julien Grallc004a6f2015-07-22 16:44:54 +01001286 struct copy_from_grant data = {
1287 .s = s,
1288 .grant_idx = 0,
1289 };
Roger Pau Monne402b27f2013-04-18 16:06:54 +02001290
Julien Grallc004a6f2015-07-22 16:44:54 +01001291 num_grant = s->req.operation == BLKIF_OP_INDIRECT ?
Roger Pau Monne402b27f2013-04-18 16:06:54 +02001292 s->req.u.indirect.nr_segments : s->req.u.rw.nr_segments;
Julien Grallc004a6f2015-07-22 16:44:54 +01001293 num_sg = s->num_sg;
Roger Pau Monne0a8704a52012-10-24 18:58:45 +02001294
Roger Pau Monnebfe11d62013-10-29 18:31:14 +01001295 if (bret->operation == BLKIF_OP_READ && info->feature_persistent) {
Julien Grallc004a6f2015-07-22 16:44:54 +01001296 for_each_sg(s->sg, sg, num_sg, i) {
Roger Pau Monneb7649152013-05-02 10:58:50 +02001297 BUG_ON(sg->offset + sg->length > PAGE_SIZE);
Julien Grallc004a6f2015-07-22 16:44:54 +01001298
1299 data.bvec_offset = sg->offset;
1300 data.bvec_data = kmap_atomic(sg_page(sg));
1301
1302 gnttab_foreach_grant_in_range(sg_page(sg),
1303 sg->offset,
1304 sg->length,
1305 blkif_copy_from_grant,
1306 &data);
1307
1308 kunmap_atomic(data.bvec_data);
Roger Pau Monne0a8704a52012-10-24 18:58:45 +02001309 }
1310 }
1311 /* Add the persistent grant into the list of free grants */
Julien Grallc004a6f2015-07-22 16:44:54 +01001312 for (i = 0; i < num_grant; i++) {
Roger Pau Monnefbe363c2013-08-12 12:53:44 +02001313 if (gnttab_query_foreign_access(s->grants_used[i]->gref)) {
1314 /*
1315 * If the grant is still mapped by the backend (the
1316 * backend has chosen to make this grant persistent)
1317 * we add it at the head of the list, so it will be
1318 * reused first.
1319 */
Roger Pau Monnebfe11d62013-10-29 18:31:14 +01001320 if (!info->feature_persistent)
1321 pr_alert_ratelimited("backed has not unmapped grant: %u\n",
1322 s->grants_used[i]->gref);
Bob Liu11659562015-11-14 11:12:13 +08001323 spin_lock_irqsave(&info->dev_lock, flags);
Roger Pau Monnebfe11d62013-10-29 18:31:14 +01001324 list_add(&s->grants_used[i]->node, &info->grants);
Roger Pau Monnefbe363c2013-08-12 12:53:44 +02001325 info->persistent_gnts_c++;
Bob Liu11659562015-11-14 11:12:13 +08001326 spin_unlock_irqrestore(&info->dev_lock, flags);
Roger Pau Monnefbe363c2013-08-12 12:53:44 +02001327 } else {
1328 /*
1329 * If the grant is not mapped by the backend we end the
1330 * foreign access and add it to the tail of the list,
1331 * so it will not be picked again unless we run out of
1332 * persistent grants.
1333 */
1334 gnttab_end_foreign_access(s->grants_used[i]->gref, 0, 0UL);
1335 s->grants_used[i]->gref = GRANT_INVALID_REF;
Bob Liu11659562015-11-14 11:12:13 +08001336 spin_lock_irqsave(&info->dev_lock, flags);
Roger Pau Monnebfe11d62013-10-29 18:31:14 +01001337 list_add_tail(&s->grants_used[i]->node, &info->grants);
Bob Liu11659562015-11-14 11:12:13 +08001338 spin_unlock_irqrestore(&info->dev_lock, flags);
Roger Pau Monnefbe363c2013-08-12 12:53:44 +02001339 }
Roger Pau Monne0a8704a52012-10-24 18:58:45 +02001340 }
Roger Pau Monne402b27f2013-04-18 16:06:54 +02001341 if (s->req.operation == BLKIF_OP_INDIRECT) {
Julien Grallc004a6f2015-07-22 16:44:54 +01001342 for (i = 0; i < INDIRECT_GREFS(num_grant); i++) {
Roger Pau Monnefbe363c2013-08-12 12:53:44 +02001343 if (gnttab_query_foreign_access(s->indirect_grants[i]->gref)) {
Roger Pau Monnebfe11d62013-10-29 18:31:14 +01001344 if (!info->feature_persistent)
1345 pr_alert_ratelimited("backed has not unmapped grant: %u\n",
1346 s->indirect_grants[i]->gref);
Bob Liu11659562015-11-14 11:12:13 +08001347 spin_lock_irqsave(&info->dev_lock, flags);
Roger Pau Monnebfe11d62013-10-29 18:31:14 +01001348 list_add(&s->indirect_grants[i]->node, &info->grants);
Roger Pau Monnefbe363c2013-08-12 12:53:44 +02001349 info->persistent_gnts_c++;
Bob Liu11659562015-11-14 11:12:13 +08001350 spin_unlock_irqrestore(&info->dev_lock, flags);
Roger Pau Monnefbe363c2013-08-12 12:53:44 +02001351 } else {
Roger Pau Monnebfe11d62013-10-29 18:31:14 +01001352 struct page *indirect_page;
1353
Roger Pau Monnefbe363c2013-08-12 12:53:44 +02001354 gnttab_end_foreign_access(s->indirect_grants[i]->gref, 0, 0UL);
Roger Pau Monnebfe11d62013-10-29 18:31:14 +01001355 /*
1356 * Add the used indirect page back to the list of
1357 * available pages for indirect grefs.
1358 */
Bob Liu7b076752015-07-22 14:40:09 +08001359 if (!info->feature_persistent) {
Julien Gralla7a6df22015-06-30 11:58:51 +01001360 indirect_page = s->indirect_grants[i]->page;
Bob Liu81f35162015-11-14 11:12:11 +08001361 list_add(&indirect_page->lru, &rinfo->indirect_pages);
Bob Liu7b076752015-07-22 14:40:09 +08001362 }
Roger Pau Monnefbe363c2013-08-12 12:53:44 +02001363 s->indirect_grants[i]->gref = GRANT_INVALID_REF;
Bob Liu11659562015-11-14 11:12:13 +08001364 spin_lock_irqsave(&info->dev_lock, flags);
Roger Pau Monnebfe11d62013-10-29 18:31:14 +01001365 list_add_tail(&s->indirect_grants[i]->node, &info->grants);
Bob Liu11659562015-11-14 11:12:13 +08001366 spin_unlock_irqrestore(&info->dev_lock, flags);
Roger Pau Monnefbe363c2013-08-12 12:53:44 +02001367 }
Roger Pau Monne402b27f2013-04-18 16:06:54 +02001368 }
1369 }
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001370}
1371
1372static irqreturn_t blkif_interrupt(int irq, void *dev_id)
1373{
1374 struct request *req;
1375 struct blkif_response *bret;
1376 RING_IDX i, rp;
1377 unsigned long flags;
Bob Liu81f35162015-11-14 11:12:11 +08001378 struct blkfront_ring_info *rinfo = (struct blkfront_ring_info *)dev_id;
1379 struct blkfront_info *info = rinfo->dev_info;
Christoph Hellwigf4829a92015-09-27 21:01:50 +02001380 int error;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001381
Bob Liu11659562015-11-14 11:12:13 +08001382 if (unlikely(info->connected != BLKIF_STATE_CONNECTED))
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001383 return IRQ_HANDLED;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001384
Bob Liu11659562015-11-14 11:12:13 +08001385 spin_lock_irqsave(&rinfo->ring_lock, flags);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001386 again:
Bob Liu81f35162015-11-14 11:12:11 +08001387 rp = rinfo->ring.sring->rsp_prod;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001388 rmb(); /* Ensure we see queued responses up to 'rp'. */
1389
Bob Liu81f35162015-11-14 11:12:11 +08001390 for (i = rinfo->ring.rsp_cons; i != rp; i++) {
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001391 unsigned long id;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001392
Bob Liu81f35162015-11-14 11:12:11 +08001393 bret = RING_GET_RESPONSE(&rinfo->ring, i);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001394 id = bret->id;
Konrad Rzeszutek Wilk6878c322012-05-25 17:34:51 -04001395 /*
1396 * The backend has messed up and given us an id that we would
1397 * never have given to it (we stamp it up to BLK_RING_SIZE -
1398 * look in get_id_from_freelist.
1399 */
Bob Liu86839c52015-06-03 13:40:03 +08001400 if (id >= BLK_RING_SIZE(info)) {
Konrad Rzeszutek Wilk6878c322012-05-25 17:34:51 -04001401 WARN(1, "%s: response to %s has incorrect id (%ld)\n",
1402 info->gd->disk_name, op_name(bret->operation), id);
1403 /* We can't safely get the 'struct request' as
1404 * the id is busted. */
1405 continue;
1406 }
Bob Liu81f35162015-11-14 11:12:11 +08001407 req = rinfo->shadow[id].request;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001408
Konrad Rzeszutek Wilk5ea42982011-10-12 16:23:30 -04001409 if (bret->operation != BLKIF_OP_DISCARD)
Bob Liu81f35162015-11-14 11:12:11 +08001410 blkif_completion(&rinfo->shadow[id], rinfo, bret);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001411
Bob Liu81f35162015-11-14 11:12:11 +08001412 if (add_id_to_freelist(rinfo, id)) {
Konrad Rzeszutek Wilk6878c322012-05-25 17:34:51 -04001413 WARN(1, "%s: response to %s (id %ld) couldn't be recycled!\n",
1414 info->gd->disk_name, op_name(bret->operation), id);
1415 continue;
1416 }
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001417
Christoph Hellwigf4829a92015-09-27 21:01:50 +02001418 error = (bret->status == BLKIF_RSP_OKAY) ? 0 : -EIO;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001419 switch (bret->operation) {
Li Dongyanged30bf32011-09-01 18:39:09 +08001420 case BLKIF_OP_DISCARD:
1421 if (unlikely(bret->status == BLKIF_RSP_EOPNOTSUPP)) {
1422 struct request_queue *rq = info->rq;
Konrad Rzeszutek Wilk6878c322012-05-25 17:34:51 -04001423 printk(KERN_WARNING "blkfront: %s: %s op failed\n",
1424 info->gd->disk_name, op_name(bret->operation));
Christoph Hellwigf4829a92015-09-27 21:01:50 +02001425 error = -EOPNOTSUPP;
Li Dongyanged30bf32011-09-01 18:39:09 +08001426 info->feature_discard = 0;
Konrad Rzeszutek Wilk5ea42982011-10-12 16:23:30 -04001427 info->feature_secdiscard = 0;
Li Dongyanged30bf32011-09-01 18:39:09 +08001428 queue_flag_clear(QUEUE_FLAG_DISCARD, rq);
Konrad Rzeszutek Wilk5ea42982011-10-12 16:23:30 -04001429 queue_flag_clear(QUEUE_FLAG_SECDISCARD, rq);
Li Dongyanged30bf32011-09-01 18:39:09 +08001430 }
Christoph Hellwigf4829a92015-09-27 21:01:50 +02001431 blk_mq_complete_request(req, error);
Li Dongyanged30bf32011-09-01 18:39:09 +08001432 break;
Konrad Rzeszutek Wilkedf6ef52011-05-03 12:01:11 -04001433 case BLKIF_OP_FLUSH_DISKCACHE:
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001434 case BLKIF_OP_WRITE_BARRIER:
1435 if (unlikely(bret->status == BLKIF_RSP_EOPNOTSUPP)) {
Konrad Rzeszutek Wilk6878c322012-05-25 17:34:51 -04001436 printk(KERN_WARNING "blkfront: %s: %s op failed\n",
1437 info->gd->disk_name, op_name(bret->operation));
Christoph Hellwigf4829a92015-09-27 21:01:50 +02001438 error = -EOPNOTSUPP;
Jeremy Fitzhardingedcb8bae2010-11-02 11:55:58 -04001439 }
1440 if (unlikely(bret->status == BLKIF_RSP_ERROR &&
Bob Liu81f35162015-11-14 11:12:11 +08001441 rinfo->shadow[id].req.u.rw.nr_segments == 0)) {
Konrad Rzeszutek Wilk6878c322012-05-25 17:34:51 -04001442 printk(KERN_WARNING "blkfront: %s: empty %s op failed\n",
1443 info->gd->disk_name, op_name(bret->operation));
Christoph Hellwigf4829a92015-09-27 21:01:50 +02001444 error = -EOPNOTSUPP;
Jeremy Fitzhardingedcb8bae2010-11-02 11:55:58 -04001445 }
Christoph Hellwigf4829a92015-09-27 21:01:50 +02001446 if (unlikely(error)) {
1447 if (error == -EOPNOTSUPP)
1448 error = 0;
Tejun Heo4913efe2010-09-03 11:56:16 +02001449 info->feature_flush = 0;
1450 xlvbd_flush(info);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001451 }
1452 /* fall through */
1453 case BLKIF_OP_READ:
1454 case BLKIF_OP_WRITE:
1455 if (unlikely(bret->status != BLKIF_RSP_OKAY))
1456 dev_dbg(&info->xbdev->dev, "Bad return from blkdev data "
1457 "request: %x\n", bret->status);
1458
Christoph Hellwigf4829a92015-09-27 21:01:50 +02001459 blk_mq_complete_request(req, error);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001460 break;
1461 default:
1462 BUG();
1463 }
1464 }
1465
Bob Liu81f35162015-11-14 11:12:11 +08001466 rinfo->ring.rsp_cons = i;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001467
Bob Liu81f35162015-11-14 11:12:11 +08001468 if (i != rinfo->ring.req_prod_pvt) {
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001469 int more_to_do;
Bob Liu81f35162015-11-14 11:12:11 +08001470 RING_FINAL_CHECK_FOR_RESPONSES(&rinfo->ring, more_to_do);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001471 if (more_to_do)
1472 goto again;
1473 } else
Bob Liu81f35162015-11-14 11:12:11 +08001474 rinfo->ring.sring->rsp_event = i + 1;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001475
Bob Liu11659562015-11-14 11:12:13 +08001476 kick_pending_request_queues_locked(rinfo);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001477
Bob Liu11659562015-11-14 11:12:13 +08001478 spin_unlock_irqrestore(&rinfo->ring_lock, flags);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001479
1480 return IRQ_HANDLED;
1481}
1482
1483
1484static int setup_blkring(struct xenbus_device *dev,
Bob Liu81f35162015-11-14 11:12:11 +08001485 struct blkfront_ring_info *rinfo)
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001486{
1487 struct blkif_sring *sring;
Bob Liu86839c52015-06-03 13:40:03 +08001488 int err, i;
Bob Liu81f35162015-11-14 11:12:11 +08001489 struct blkfront_info *info = rinfo->dev_info;
Julien Grallc004a6f2015-07-22 16:44:54 +01001490 unsigned long ring_size = info->nr_ring_pages * XEN_PAGE_SIZE;
Julien Grall9cce2912015-10-13 17:50:11 +01001491 grant_ref_t gref[XENBUS_MAX_RING_GRANTS];
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001492
Bob Liu86839c52015-06-03 13:40:03 +08001493 for (i = 0; i < info->nr_ring_pages; i++)
Bob Liu81f35162015-11-14 11:12:11 +08001494 rinfo->ring_ref[i] = GRANT_INVALID_REF;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001495
Bob Liu86839c52015-06-03 13:40:03 +08001496 sring = (struct blkif_sring *)__get_free_pages(GFP_NOIO | __GFP_HIGH,
1497 get_order(ring_size));
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001498 if (!sring) {
1499 xenbus_dev_fatal(dev, -ENOMEM, "allocating shared ring");
1500 return -ENOMEM;
1501 }
1502 SHARED_RING_INIT(sring);
Bob Liu81f35162015-11-14 11:12:11 +08001503 FRONT_RING_INIT(&rinfo->ring, sring, ring_size);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001504
Bob Liu81f35162015-11-14 11:12:11 +08001505 err = xenbus_grant_ring(dev, rinfo->ring.sring, info->nr_ring_pages, gref);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001506 if (err < 0) {
Bob Liu86839c52015-06-03 13:40:03 +08001507 free_pages((unsigned long)sring, get_order(ring_size));
Bob Liu81f35162015-11-14 11:12:11 +08001508 rinfo->ring.sring = NULL;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001509 goto fail;
1510 }
Bob Liu86839c52015-06-03 13:40:03 +08001511 for (i = 0; i < info->nr_ring_pages; i++)
Bob Liu81f35162015-11-14 11:12:11 +08001512 rinfo->ring_ref[i] = gref[i];
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001513
Bob Liu81f35162015-11-14 11:12:11 +08001514 err = xenbus_alloc_evtchn(dev, &rinfo->evtchn);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001515 if (err)
1516 goto fail;
1517
Bob Liu81f35162015-11-14 11:12:11 +08001518 err = bind_evtchn_to_irqhandler(rinfo->evtchn, blkif_interrupt, 0,
1519 "blkif", rinfo);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001520 if (err <= 0) {
1521 xenbus_dev_fatal(dev, err,
1522 "bind_evtchn_to_irqhandler failed");
1523 goto fail;
1524 }
Bob Liu81f35162015-11-14 11:12:11 +08001525 rinfo->irq = err;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001526
1527 return 0;
1528fail:
1529 blkif_free(info, 0);
1530 return err;
1531}
1532
Bob Liu28d949b2015-11-14 11:12:14 +08001533/*
1534 * Write out per-ring/queue nodes including ring-ref and event-channel, and each
1535 * ring buffer may have multi pages depending on ->nr_ring_pages.
1536 */
1537static int write_per_ring_nodes(struct xenbus_transaction xbt,
1538 struct blkfront_ring_info *rinfo, const char *dir)
1539{
1540 int err;
1541 unsigned int i;
1542 const char *message = NULL;
1543 struct blkfront_info *info = rinfo->dev_info;
1544
1545 if (info->nr_ring_pages == 1) {
1546 err = xenbus_printf(xbt, dir, "ring-ref", "%u", rinfo->ring_ref[0]);
1547 if (err) {
1548 message = "writing ring-ref";
1549 goto abort_transaction;
1550 }
1551 } else {
1552 for (i = 0; i < info->nr_ring_pages; i++) {
1553 char ring_ref_name[RINGREF_NAME_LEN];
1554
1555 snprintf(ring_ref_name, RINGREF_NAME_LEN, "ring-ref%u", i);
1556 err = xenbus_printf(xbt, dir, ring_ref_name,
1557 "%u", rinfo->ring_ref[i]);
1558 if (err) {
1559 message = "writing ring-ref";
1560 goto abort_transaction;
1561 }
1562 }
1563 }
1564
1565 err = xenbus_printf(xbt, dir, "event-channel", "%u", rinfo->evtchn);
1566 if (err) {
1567 message = "writing event-channel";
1568 goto abort_transaction;
1569 }
1570
1571 return 0;
1572
1573abort_transaction:
1574 xenbus_transaction_end(xbt, 1);
1575 if (message)
1576 xenbus_dev_fatal(info->xbdev, err, "%s", message);
1577
1578 return err;
1579}
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001580
1581/* Common code used when first setting up, and when resuming. */
Ian Campbell203fd612009-12-04 15:33:54 +00001582static int talk_to_blkback(struct xenbus_device *dev,
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001583 struct blkfront_info *info)
1584{
1585 const char *message = NULL;
1586 struct xenbus_transaction xbt;
Bob Liu28d949b2015-11-14 11:12:14 +08001587 int err;
1588 unsigned int i, max_page_order = 0;
Bob Liu86839c52015-06-03 13:40:03 +08001589 unsigned int ring_page_order = 0;
1590
1591 err = xenbus_scanf(XBT_NIL, info->xbdev->otherend,
1592 "max-ring-page-order", "%u", &max_page_order);
1593 if (err != 1)
1594 info->nr_ring_pages = 1;
1595 else {
1596 ring_page_order = min(xen_blkif_max_ring_order, max_page_order);
1597 info->nr_ring_pages = 1 << ring_page_order;
1598 }
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001599
Bob Liu3df0e502015-11-14 11:12:12 +08001600 for (i = 0; i < info->nr_rings; i++) {
Bob Liu28d949b2015-11-14 11:12:14 +08001601 struct blkfront_ring_info *rinfo = &info->rinfo[i];
1602
Bob Liu3df0e502015-11-14 11:12:12 +08001603 /* Create shared ring, alloc event channel. */
1604 err = setup_blkring(dev, rinfo);
1605 if (err)
1606 goto destroy_blkring;
1607 }
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001608
1609again:
1610 err = xenbus_transaction_start(&xbt);
1611 if (err) {
1612 xenbus_dev_fatal(dev, err, "starting transaction");
1613 goto destroy_blkring;
1614 }
1615
Bob Liu28d949b2015-11-14 11:12:14 +08001616 if (info->nr_ring_pages > 1) {
1617 err = xenbus_printf(xbt, dev->nodename, "ring-page-order", "%u",
1618 ring_page_order);
Bob Liu3df0e502015-11-14 11:12:12 +08001619 if (err) {
Bob Liu28d949b2015-11-14 11:12:14 +08001620 message = "writing ring-page-order";
Bob Liu3df0e502015-11-14 11:12:12 +08001621 goto abort_transaction;
1622 }
Bob Liu28d949b2015-11-14 11:12:14 +08001623 }
1624
1625 /* We already got the number of queues/rings in _probe */
1626 if (info->nr_rings == 1) {
1627 err = write_per_ring_nodes(xbt, &info->rinfo[0], dev->nodename);
1628 if (err)
1629 goto destroy_blkring;
Bob Liu3df0e502015-11-14 11:12:12 +08001630 } else {
Bob Liu28d949b2015-11-14 11:12:14 +08001631 char *path;
1632 size_t pathsize;
1633
1634 err = xenbus_printf(xbt, dev->nodename, "multi-queue-num-queues", "%u",
1635 info->nr_rings);
1636 if (err) {
1637 message = "writing multi-queue-num-queues";
1638 goto abort_transaction;
1639 }
1640
1641 pathsize = strlen(dev->nodename) + QUEUE_NAME_LEN;
1642 path = kmalloc(pathsize, GFP_KERNEL);
1643 if (!path) {
1644 err = -ENOMEM;
1645 message = "ENOMEM while writing ring references";
1646 goto abort_transaction;
1647 }
1648
1649 for (i = 0; i < info->nr_rings; i++) {
1650 memset(path, 0, pathsize);
1651 snprintf(path, pathsize, "%s/queue-%u", dev->nodename, i);
1652 err = write_per_ring_nodes(xbt, &info->rinfo[i], path);
1653 if (err) {
1654 kfree(path);
1655 goto destroy_blkring;
1656 }
1657 }
1658 kfree(path);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001659 }
Markus Armbruster3e334232008-04-02 10:54:02 -07001660 err = xenbus_printf(xbt, dev->nodename, "protocol", "%s",
1661 XEN_IO_PROTO_ABI_NATIVE);
1662 if (err) {
1663 message = "writing protocol";
1664 goto abort_transaction;
1665 }
Roger Pau Monne0a8704a52012-10-24 18:58:45 +02001666 err = xenbus_printf(xbt, dev->nodename,
Roger Pau Monnecb5bd4d2012-11-02 16:43:04 +01001667 "feature-persistent", "%u", 1);
Roger Pau Monne0a8704a52012-10-24 18:58:45 +02001668 if (err)
1669 dev_warn(&dev->dev,
1670 "writing persistent grants feature to xenbus");
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001671
1672 err = xenbus_transaction_end(xbt, 0);
1673 if (err) {
1674 if (err == -EAGAIN)
1675 goto again;
1676 xenbus_dev_fatal(dev, err, "completing transaction");
1677 goto destroy_blkring;
1678 }
1679
Bob Liu3df0e502015-11-14 11:12:12 +08001680 for (i = 0; i < info->nr_rings; i++) {
1681 unsigned int j;
Bob Liu28d949b2015-11-14 11:12:14 +08001682 struct blkfront_ring_info *rinfo = &info->rinfo[i];
Bob Liu3df0e502015-11-14 11:12:12 +08001683
1684 for (j = 0; j < BLK_RING_SIZE(info); j++)
1685 rinfo->shadow[j].req.u.rw.id = j + 1;
1686 rinfo->shadow[BLK_RING_SIZE(info)-1].req.u.rw.id = 0x0fffffff;
1687 }
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001688 xenbus_switch_state(dev, XenbusStateInitialised);
1689
1690 return 0;
1691
1692 abort_transaction:
1693 xenbus_transaction_end(xbt, 1);
1694 if (message)
1695 xenbus_dev_fatal(dev, err, "%s", message);
1696 destroy_blkring:
1697 blkif_free(info, 0);
Bob Liu3df0e502015-11-14 11:12:12 +08001698
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001699 return err;
1700}
1701
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001702/**
1703 * Entry point to this code when a new device is created. Allocate the basic
1704 * structures and the ring buffer for communication with the backend, and
1705 * inform the backend of the appropriate details for those. Switch to
1706 * Initialised state.
1707 */
1708static int blkfront_probe(struct xenbus_device *dev,
1709 const struct xenbus_device_id *id)
1710{
Bob Liu86839c52015-06-03 13:40:03 +08001711 int err, vdevice;
Bob Liu3df0e502015-11-14 11:12:12 +08001712 unsigned int r_index;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001713 struct blkfront_info *info;
Bob Liu28d949b2015-11-14 11:12:14 +08001714 unsigned int backend_max_queues = 0;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001715
1716 /* FIXME: Use dynamic device id if this is not set. */
1717 err = xenbus_scanf(XBT_NIL, dev->nodename,
1718 "virtual-device", "%i", &vdevice);
1719 if (err != 1) {
Chris Lalancette9246b5f2008-09-17 14:30:32 -07001720 /* go looking in the extended area instead */
1721 err = xenbus_scanf(XBT_NIL, dev->nodename, "virtual-device-ext",
1722 "%i", &vdevice);
1723 if (err != 1) {
1724 xenbus_dev_fatal(dev, err, "reading virtual-device");
1725 return err;
1726 }
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001727 }
1728
Stefano Stabellinib98a4092010-07-29 14:53:16 +01001729 if (xen_hvm_domain()) {
1730 char *type;
1731 int len;
1732 /* no unplug has been done: do not hook devices != xen vbds */
Konrad Rzeszutek Wilk51c71a32013-11-26 15:05:40 -05001733 if (xen_has_pv_and_legacy_disk_devices()) {
Stefano Stabellinib98a4092010-07-29 14:53:16 +01001734 int major;
Stefano Stabellinic1c54132010-05-14 12:44:30 +01001735
Stefano Stabellinib98a4092010-07-29 14:53:16 +01001736 if (!VDEV_IS_EXTENDED(vdevice))
1737 major = BLKIF_MAJOR(vdevice);
1738 else
1739 major = XENVBD_MAJOR;
Stefano Stabellinic1c54132010-05-14 12:44:30 +01001740
Stefano Stabellinib98a4092010-07-29 14:53:16 +01001741 if (major != XENVBD_MAJOR) {
1742 printk(KERN_INFO
1743 "%s: HVM does not support vbd %d as xen block device\n",
Rasmus Villemoes02f1f212015-02-12 15:01:31 -08001744 __func__, vdevice);
Stefano Stabellinib98a4092010-07-29 14:53:16 +01001745 return -ENODEV;
1746 }
1747 }
1748 /* do not create a PV cdrom device if we are an HVM guest */
1749 type = xenbus_read(XBT_NIL, dev->nodename, "device-type", &len);
1750 if (IS_ERR(type))
1751 return -ENODEV;
1752 if (strncmp(type, "cdrom", 5) == 0) {
1753 kfree(type);
Stefano Stabellinic1c54132010-05-14 12:44:30 +01001754 return -ENODEV;
1755 }
Stefano Stabellinib98a4092010-07-29 14:53:16 +01001756 kfree(type);
Stefano Stabellinic1c54132010-05-14 12:44:30 +01001757 }
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001758 info = kzalloc(sizeof(*info), GFP_KERNEL);
1759 if (!info) {
1760 xenbus_dev_fatal(dev, -ENOMEM, "allocating info structure");
1761 return -ENOMEM;
1762 }
1763
Bob Liu28d949b2015-11-14 11:12:14 +08001764 info->xbdev = dev;
1765 /* Check if backend supports multiple queues. */
1766 err = xenbus_scanf(XBT_NIL, info->xbdev->otherend,
1767 "multi-queue-max-queues", "%u", &backend_max_queues);
1768 if (err < 0)
1769 backend_max_queues = 1;
1770
1771 info->nr_rings = min(backend_max_queues, xen_blkif_max_queues);
1772 /* We need at least one ring. */
1773 if (!info->nr_rings)
1774 info->nr_rings = 1;
1775
Bob Liu3df0e502015-11-14 11:12:12 +08001776 info->rinfo = kzalloc(sizeof(struct blkfront_ring_info) * info->nr_rings, GFP_KERNEL);
1777 if (!info->rinfo) {
1778 xenbus_dev_fatal(dev, -ENOMEM, "allocating ring_info structure");
1779 kfree(info);
1780 return -ENOMEM;
1781 }
1782
1783 for (r_index = 0; r_index < info->nr_rings; r_index++) {
1784 struct blkfront_ring_info *rinfo;
1785
1786 rinfo = &info->rinfo[r_index];
1787 INIT_LIST_HEAD(&rinfo->indirect_pages);
1788 rinfo->dev_info = info;
1789 INIT_WORK(&rinfo->work, blkif_restart_queue);
Bob Liu11659562015-11-14 11:12:13 +08001790 spin_lock_init(&rinfo->ring_lock);
Bob Liu3df0e502015-11-14 11:12:12 +08001791 }
Bob Liu81f35162015-11-14 11:12:11 +08001792
Daniel Stoddenb70f5fa2010-04-30 22:01:19 +00001793 mutex_init(&info->mutex);
Bob Liu11659562015-11-14 11:12:13 +08001794 spin_lock_init(&info->dev_lock);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001795 info->xbdev = dev;
1796 info->vdevice = vdevice;
Roger Pau Monnebfe11d62013-10-29 18:31:14 +01001797 INIT_LIST_HEAD(&info->grants);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001798 info->connected = BLKIF_STATE_DISCONNECTED;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001799
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001800 /* Front end dir is a number, which is used as the id. */
1801 info->handle = simple_strtoul(strrchr(dev->nodename, '/')+1, NULL, 0);
Greg Kroah-Hartmana1b4b122009-04-30 14:43:31 -07001802 dev_set_drvdata(&dev->dev, info);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001803
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001804 return 0;
1805}
1806
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02001807static void split_bio_end(struct bio *bio)
Roger Pau Monne402b27f2013-04-18 16:06:54 +02001808{
1809 struct split_bio *split_bio = bio->bi_private;
1810
Roger Pau Monne402b27f2013-04-18 16:06:54 +02001811 if (atomic_dec_and_test(&split_bio->pending)) {
1812 split_bio->bio->bi_phys_segments = 0;
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02001813 split_bio->bio->bi_error = bio->bi_error;
1814 bio_endio(split_bio->bio);
Roger Pau Monne402b27f2013-04-18 16:06:54 +02001815 kfree(split_bio);
1816 }
1817 bio_put(bio);
1818}
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001819
1820static int blkif_recover(struct blkfront_info *info)
1821{
Bob Liu3df0e502015-11-14 11:12:12 +08001822 unsigned int i, r_index;
Roger Pau Monne402b27f2013-04-18 16:06:54 +02001823 struct request *req, *n;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001824 struct blk_shadow *copy;
Roger Pau Monne402b27f2013-04-18 16:06:54 +02001825 int rc;
1826 struct bio *bio, *cloned_bio;
1827 struct bio_list bio_list, merge_bio;
1828 unsigned int segs, offset;
1829 int pending, size;
1830 struct split_bio *split_bio;
1831 struct list_head requests;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001832
Bob Liu3df0e502015-11-14 11:12:12 +08001833 blkfront_gather_backend_features(info);
Roger Pau Monne402b27f2013-04-18 16:06:54 +02001834 segs = info->max_indirect_segments ? : BLKIF_MAX_SEGMENTS_PER_REQUEST;
1835 blk_queue_max_segments(info->rq, segs);
1836 bio_list_init(&bio_list);
1837 INIT_LIST_HEAD(&requests);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001838
Bob Liu3df0e502015-11-14 11:12:12 +08001839 for (r_index = 0; r_index < info->nr_rings; r_index++) {
1840 struct blkfront_ring_info *rinfo;
1841
1842 rinfo = &info->rinfo[r_index];
1843 /* Stage 1: Make a safe copy of the shadow state. */
1844 copy = kmemdup(rinfo->shadow, sizeof(rinfo->shadow),
1845 GFP_NOIO | __GFP_REPEAT | __GFP_HIGH);
1846 if (!copy)
1847 return -ENOMEM;
1848
1849 /* Stage 2: Set up free list. */
1850 memset(&rinfo->shadow, 0, sizeof(rinfo->shadow));
1851 for (i = 0; i < BLK_RING_SIZE(info); i++)
1852 rinfo->shadow[i].req.u.rw.id = i+1;
1853 rinfo->shadow_free = rinfo->ring.req_prod_pvt;
1854 rinfo->shadow[BLK_RING_SIZE(info)-1].req.u.rw.id = 0x0fffffff;
1855
1856 rc = blkfront_setup_indirect(rinfo);
1857 if (rc) {
1858 kfree(copy);
1859 return rc;
Konrad Rzeszutek Wilk5ea42982011-10-12 16:23:30 -04001860 }
Bob Liu3df0e502015-11-14 11:12:12 +08001861
1862 for (i = 0; i < BLK_RING_SIZE(info); i++) {
1863 /* Not in use? */
1864 if (!copy[i].request)
1865 continue;
1866
1867 /*
1868 * Get the bios in the request so we can re-queue them.
1869 */
1870 if (copy[i].request->cmd_flags &
1871 (REQ_FLUSH | REQ_FUA | REQ_DISCARD | REQ_SECURE)) {
1872 /*
1873 * Flush operations don't contain bios, so
1874 * we need to requeue the whole request
1875 */
1876 list_add(&copy[i].request->queuelist, &requests);
1877 continue;
1878 }
1879 merge_bio.head = copy[i].request->bio;
1880 merge_bio.tail = copy[i].request->biotail;
1881 bio_list_merge(&bio_list, &merge_bio);
1882 copy[i].request->bio = NULL;
1883 blk_end_request_all(copy[i].request, 0);
1884 }
1885
1886 kfree(copy);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001887 }
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001888 xenbus_switch_state(info->xbdev, XenbusStateConnected);
1889
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001890 /* Now safe for us to use the shared ring */
1891 info->connected = BLKIF_STATE_CONNECTED;
1892
Bob Liu3df0e502015-11-14 11:12:12 +08001893 for (r_index = 0; r_index < info->nr_rings; r_index++) {
1894 struct blkfront_ring_info *rinfo;
1895
1896 rinfo = &info->rinfo[r_index];
1897 /* Kick any other new requests queued since we resumed */
1898 kick_pending_request_queues(rinfo);
1899 }
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001900
Roger Pau Monne402b27f2013-04-18 16:06:54 +02001901 list_for_each_entry_safe(req, n, &requests, queuelist) {
1902 /* Requeue pending requests (flush or discard) */
1903 list_del_init(&req->queuelist);
1904 BUG_ON(req->nr_phys_segments > segs);
Bob Liu907c3eb2015-07-13 17:55:24 +08001905 blk_mq_requeue_request(req);
Roger Pau Monne402b27f2013-04-18 16:06:54 +02001906 }
Bob Liu907c3eb2015-07-13 17:55:24 +08001907 blk_mq_kick_requeue_list(info->rq);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001908
Roger Pau Monne402b27f2013-04-18 16:06:54 +02001909 while ((bio = bio_list_pop(&bio_list)) != NULL) {
1910 /* Traverse the list of pending bios and re-queue them */
1911 if (bio_segments(bio) > segs) {
1912 /*
1913 * This bio has more segments than what we can
1914 * handle, we have to split it.
1915 */
1916 pending = (bio_segments(bio) + segs - 1) / segs;
1917 split_bio = kzalloc(sizeof(*split_bio), GFP_NOIO);
1918 BUG_ON(split_bio == NULL);
1919 atomic_set(&split_bio->pending, pending);
1920 split_bio->bio = bio;
1921 for (i = 0; i < pending; i++) {
Julien Grallc004a6f2015-07-22 16:44:54 +01001922 offset = (i * segs * XEN_PAGE_SIZE) >> 9;
1923 size = min((unsigned int)(segs * XEN_PAGE_SIZE) >> 9,
Kent Overstreet4f024f32013-10-11 15:44:27 -07001924 (unsigned int)bio_sectors(bio) - offset);
Roger Pau Monne402b27f2013-04-18 16:06:54 +02001925 cloned_bio = bio_clone(bio, GFP_NOIO);
1926 BUG_ON(cloned_bio == NULL);
Kent Overstreet6678d832013-08-07 11:14:32 -07001927 bio_trim(cloned_bio, offset, size);
Roger Pau Monne402b27f2013-04-18 16:06:54 +02001928 cloned_bio->bi_private = split_bio;
1929 cloned_bio->bi_end_io = split_bio_end;
1930 submit_bio(cloned_bio->bi_rw, cloned_bio);
1931 }
1932 /*
1933 * Now we have to wait for all those smaller bios to
1934 * end, so we can also end the "parent" bio.
1935 */
1936 continue;
1937 }
1938 /* We don't need to split this bio */
1939 submit_bio(bio->bi_rw, bio);
1940 }
1941
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001942 return 0;
1943}
1944
1945/**
1946 * We are reconnecting to the backend, due to a suspend/resume, or a backend
1947 * driver restart. We tear down our blkif structure and recreate it, but
1948 * leave the device-layer structures intact so that this is transparent to the
1949 * rest of the kernel.
1950 */
1951static int blkfront_resume(struct xenbus_device *dev)
1952{
Greg Kroah-Hartmana1b4b122009-04-30 14:43:31 -07001953 struct blkfront_info *info = dev_get_drvdata(&dev->dev);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001954 int err;
1955
1956 dev_dbg(&dev->dev, "blkfront_resume: %s\n", dev->nodename);
1957
1958 blkif_free(info, info->connected == BLKIF_STATE_CONNECTED);
1959
Ian Campbell203fd612009-12-04 15:33:54 +00001960 err = talk_to_blkback(dev, info);
Roger Pau Monne402b27f2013-04-18 16:06:54 +02001961
1962 /*
1963 * We have to wait for the backend to switch to
1964 * connected state, since we want to read which
1965 * features it supports.
1966 */
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001967
1968 return err;
1969}
1970
Daniel Stoddenb70f5fa2010-04-30 22:01:19 +00001971static void
1972blkfront_closing(struct blkfront_info *info)
1973{
1974 struct xenbus_device *xbdev = info->xbdev;
1975 struct block_device *bdev = NULL;
1976
1977 mutex_lock(&info->mutex);
1978
1979 if (xbdev->state == XenbusStateClosing) {
1980 mutex_unlock(&info->mutex);
1981 return;
1982 }
1983
1984 if (info->gd)
1985 bdev = bdget_disk(info->gd, 0);
1986
1987 mutex_unlock(&info->mutex);
1988
1989 if (!bdev) {
1990 xenbus_frontend_closed(xbdev);
1991 return;
1992 }
1993
1994 mutex_lock(&bdev->bd_mutex);
1995
Daniel Stodden7b32d102010-04-30 22:01:23 +00001996 if (bdev->bd_openers) {
Daniel Stoddenb70f5fa2010-04-30 22:01:19 +00001997 xenbus_dev_error(xbdev, -EBUSY,
1998 "Device in use; refusing to close");
1999 xenbus_switch_state(xbdev, XenbusStateClosing);
2000 } else {
2001 xlvbd_release_gendisk(info);
2002 xenbus_frontend_closed(xbdev);
2003 }
2004
2005 mutex_unlock(&bdev->bd_mutex);
2006 bdput(bdev);
2007}
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07002008
Li Dongyanged30bf32011-09-01 18:39:09 +08002009static void blkfront_setup_discard(struct blkfront_info *info)
2010{
2011 int err;
Li Dongyanged30bf32011-09-01 18:39:09 +08002012 unsigned int discard_granularity;
2013 unsigned int discard_alignment;
Konrad Rzeszutek Wilk5ea42982011-10-12 16:23:30 -04002014 unsigned int discard_secure;
Li Dongyanged30bf32011-09-01 18:39:09 +08002015
Olaf Hering1c8cad62014-05-21 16:32:40 +02002016 info->feature_discard = 1;
2017 err = xenbus_gather(XBT_NIL, info->xbdev->otherend,
2018 "discard-granularity", "%u", &discard_granularity,
2019 "discard-alignment", "%u", &discard_alignment,
2020 NULL);
2021 if (!err) {
2022 info->discard_granularity = discard_granularity;
2023 info->discard_alignment = discard_alignment;
2024 }
2025 err = xenbus_gather(XBT_NIL, info->xbdev->otherend,
2026 "discard-secure", "%d", &discard_secure,
2027 NULL);
2028 if (!err)
2029 info->feature_secdiscard = !!discard_secure;
Li Dongyanged30bf32011-09-01 18:39:09 +08002030}
2031
Bob Liu81f35162015-11-14 11:12:11 +08002032static int blkfront_setup_indirect(struct blkfront_ring_info *rinfo)
Roger Pau Monne402b27f2013-04-18 16:06:54 +02002033{
Julien Grallc004a6f2015-07-22 16:44:54 +01002034 unsigned int psegs, grants;
Roger Pau Monne402b27f2013-04-18 16:06:54 +02002035 int err, i;
Bob Liu81f35162015-11-14 11:12:11 +08002036 struct blkfront_info *info = rinfo->dev_info;
Roger Pau Monne402b27f2013-04-18 16:06:54 +02002037
Bob Liud50babb2015-07-22 14:40:08 +08002038 if (info->max_indirect_segments == 0)
Julien Grallc004a6f2015-07-22 16:44:54 +01002039 grants = BLKIF_MAX_SEGMENTS_PER_REQUEST;
Bob Liud50babb2015-07-22 14:40:08 +08002040 else
Julien Grallc004a6f2015-07-22 16:44:54 +01002041 grants = info->max_indirect_segments;
2042 psegs = grants / GRANTS_PER_PSEG;
Roger Pau Monne402b27f2013-04-18 16:06:54 +02002043
Bob Liu81f35162015-11-14 11:12:11 +08002044 err = fill_grant_buffer(rinfo,
Julien Grallc004a6f2015-07-22 16:44:54 +01002045 (grants + INDIRECT_GREFS(grants)) * BLK_RING_SIZE(info));
Roger Pau Monne402b27f2013-04-18 16:06:54 +02002046 if (err)
2047 goto out_of_memory;
2048
Roger Pau Monnebfe11d62013-10-29 18:31:14 +01002049 if (!info->feature_persistent && info->max_indirect_segments) {
2050 /*
2051 * We are using indirect descriptors but not persistent
2052 * grants, we need to allocate a set of pages that can be
2053 * used for mapping indirect grefs
2054 */
Julien Grallc004a6f2015-07-22 16:44:54 +01002055 int num = INDIRECT_GREFS(grants) * BLK_RING_SIZE(info);
Roger Pau Monnebfe11d62013-10-29 18:31:14 +01002056
Bob Liu81f35162015-11-14 11:12:11 +08002057 BUG_ON(!list_empty(&rinfo->indirect_pages));
Roger Pau Monnebfe11d62013-10-29 18:31:14 +01002058 for (i = 0; i < num; i++) {
2059 struct page *indirect_page = alloc_page(GFP_NOIO);
2060 if (!indirect_page)
2061 goto out_of_memory;
Bob Liu81f35162015-11-14 11:12:11 +08002062 list_add(&indirect_page->lru, &rinfo->indirect_pages);
Roger Pau Monnebfe11d62013-10-29 18:31:14 +01002063 }
2064 }
2065
Bob Liu86839c52015-06-03 13:40:03 +08002066 for (i = 0; i < BLK_RING_SIZE(info); i++) {
Bob Liu81f35162015-11-14 11:12:11 +08002067 rinfo->shadow[i].grants_used = kzalloc(
2068 sizeof(rinfo->shadow[i].grants_used[0]) * grants,
Roger Pau Monne402b27f2013-04-18 16:06:54 +02002069 GFP_NOIO);
Bob Liu81f35162015-11-14 11:12:11 +08002070 rinfo->shadow[i].sg = kzalloc(sizeof(rinfo->shadow[i].sg[0]) * psegs, GFP_NOIO);
Roger Pau Monne402b27f2013-04-18 16:06:54 +02002071 if (info->max_indirect_segments)
Bob Liu81f35162015-11-14 11:12:11 +08002072 rinfo->shadow[i].indirect_grants = kzalloc(
2073 sizeof(rinfo->shadow[i].indirect_grants[0]) *
Julien Grallc004a6f2015-07-22 16:44:54 +01002074 INDIRECT_GREFS(grants),
Roger Pau Monne402b27f2013-04-18 16:06:54 +02002075 GFP_NOIO);
Bob Liu81f35162015-11-14 11:12:11 +08002076 if ((rinfo->shadow[i].grants_used == NULL) ||
2077 (rinfo->shadow[i].sg == NULL) ||
Roger Pau Monne402b27f2013-04-18 16:06:54 +02002078 (info->max_indirect_segments &&
Bob Liu81f35162015-11-14 11:12:11 +08002079 (rinfo->shadow[i].indirect_grants == NULL)))
Roger Pau Monne402b27f2013-04-18 16:06:54 +02002080 goto out_of_memory;
Bob Liu81f35162015-11-14 11:12:11 +08002081 sg_init_table(rinfo->shadow[i].sg, psegs);
Roger Pau Monne402b27f2013-04-18 16:06:54 +02002082 }
2083
2084
2085 return 0;
2086
2087out_of_memory:
Bob Liu86839c52015-06-03 13:40:03 +08002088 for (i = 0; i < BLK_RING_SIZE(info); i++) {
Bob Liu81f35162015-11-14 11:12:11 +08002089 kfree(rinfo->shadow[i].grants_used);
2090 rinfo->shadow[i].grants_used = NULL;
2091 kfree(rinfo->shadow[i].sg);
2092 rinfo->shadow[i].sg = NULL;
2093 kfree(rinfo->shadow[i].indirect_grants);
2094 rinfo->shadow[i].indirect_grants = NULL;
Roger Pau Monne402b27f2013-04-18 16:06:54 +02002095 }
Bob Liu81f35162015-11-14 11:12:11 +08002096 if (!list_empty(&rinfo->indirect_pages)) {
Roger Pau Monnebfe11d62013-10-29 18:31:14 +01002097 struct page *indirect_page, *n;
Bob Liu81f35162015-11-14 11:12:11 +08002098 list_for_each_entry_safe(indirect_page, n, &rinfo->indirect_pages, lru) {
Roger Pau Monnebfe11d62013-10-29 18:31:14 +01002099 list_del(&indirect_page->lru);
2100 __free_page(indirect_page);
2101 }
2102 }
Roger Pau Monne402b27f2013-04-18 16:06:54 +02002103 return -ENOMEM;
2104}
2105
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07002106/*
Bob Liud50babb2015-07-22 14:40:08 +08002107 * Gather all backend feature-*
2108 */
Bob Liu3df0e502015-11-14 11:12:12 +08002109static void blkfront_gather_backend_features(struct blkfront_info *info)
Bob Liud50babb2015-07-22 14:40:08 +08002110{
2111 int err;
2112 int barrier, flush, discard, persistent;
2113 unsigned int indirect_segments;
2114
2115 info->feature_flush = 0;
2116
2117 err = xenbus_gather(XBT_NIL, info->xbdev->otherend,
2118 "feature-barrier", "%d", &barrier,
2119 NULL);
2120
2121 /*
2122 * If there's no "feature-barrier" defined, then it means
2123 * we're dealing with a very old backend which writes
2124 * synchronously; nothing to do.
2125 *
2126 * If there are barriers, then we use flush.
2127 */
2128 if (!err && barrier)
2129 info->feature_flush = REQ_FLUSH | REQ_FUA;
2130 /*
2131 * And if there is "feature-flush-cache" use that above
2132 * barriers.
2133 */
2134 err = xenbus_gather(XBT_NIL, info->xbdev->otherend,
2135 "feature-flush-cache", "%d", &flush,
2136 NULL);
2137
2138 if (!err && flush)
2139 info->feature_flush = REQ_FLUSH;
2140
2141 err = xenbus_gather(XBT_NIL, info->xbdev->otherend,
2142 "feature-discard", "%d", &discard,
2143 NULL);
2144
2145 if (!err && discard)
2146 blkfront_setup_discard(info);
2147
2148 err = xenbus_gather(XBT_NIL, info->xbdev->otherend,
2149 "feature-persistent", "%u", &persistent,
2150 NULL);
2151 if (err)
2152 info->feature_persistent = 0;
2153 else
2154 info->feature_persistent = persistent;
2155
2156 err = xenbus_gather(XBT_NIL, info->xbdev->otherend,
2157 "feature-max-indirect-segments", "%u", &indirect_segments,
2158 NULL);
2159 if (err)
2160 info->max_indirect_segments = 0;
2161 else
2162 info->max_indirect_segments = min(indirect_segments,
2163 xen_blkif_max_segments);
Bob Liud50babb2015-07-22 14:40:08 +08002164}
2165
2166/*
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07002167 * Invoked when the backend is finally 'ready' (and has told produced
2168 * the details about the physical device - #sectors, size, etc).
2169 */
2170static void blkfront_connect(struct blkfront_info *info)
2171{
2172 unsigned long long sectors;
2173 unsigned long sector_size;
Stefan Bader7c4d7d72013-05-13 16:28:15 +02002174 unsigned int physical_sector_size;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07002175 unsigned int binfo;
Bob Liu3df0e502015-11-14 11:12:12 +08002176 int err, i;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07002177
K. Y. Srinivasan1fa73be2010-03-11 13:42:26 -08002178 switch (info->connected) {
2179 case BLKIF_STATE_CONNECTED:
2180 /*
2181 * Potentially, the back-end may be signalling
2182 * a capacity change; update the capacity.
2183 */
2184 err = xenbus_scanf(XBT_NIL, info->xbdev->otherend,
2185 "sectors", "%Lu", &sectors);
2186 if (XENBUS_EXIST_ERR(err))
2187 return;
2188 printk(KERN_INFO "Setting capacity to %Lu\n",
2189 sectors);
2190 set_capacity(info->gd, sectors);
K. Y. Srinivasan2def1412010-03-18 15:00:54 -07002191 revalidate_disk(info->gd);
K. Y. Srinivasan1fa73be2010-03-11 13:42:26 -08002192
Roger Pau Monne402b27f2013-04-18 16:06:54 +02002193 return;
K. Y. Srinivasan1fa73be2010-03-11 13:42:26 -08002194 case BLKIF_STATE_SUSPENDED:
Roger Pau Monne402b27f2013-04-18 16:06:54 +02002195 /*
2196 * If we are recovering from suspension, we need to wait
2197 * for the backend to announce it's features before
2198 * reconnecting, at least we need to know if the backend
2199 * supports indirect descriptors, and how many.
2200 */
2201 blkif_recover(info);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07002202 return;
2203
Jeremy Fitzhardingeb4dddb42010-03-11 15:10:40 -08002204 default:
2205 break;
K. Y. Srinivasan1fa73be2010-03-11 13:42:26 -08002206 }
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07002207
2208 dev_dbg(&info->xbdev->dev, "%s:%s.\n",
2209 __func__, info->xbdev->otherend);
2210
2211 err = xenbus_gather(XBT_NIL, info->xbdev->otherend,
2212 "sectors", "%llu", &sectors,
2213 "info", "%u", &binfo,
2214 "sector-size", "%lu", &sector_size,
2215 NULL);
2216 if (err) {
2217 xenbus_dev_fatal(info->xbdev, err,
2218 "reading backend fields at %s",
2219 info->xbdev->otherend);
2220 return;
2221 }
2222
Stefan Bader7c4d7d72013-05-13 16:28:15 +02002223 /*
2224 * physcial-sector-size is a newer field, so old backends may not
2225 * provide this. Assume physical sector size to be the same as
2226 * sector_size in that case.
2227 */
2228 err = xenbus_scanf(XBT_NIL, info->xbdev->otherend,
2229 "physical-sector-size", "%u", &physical_sector_size);
2230 if (err != 1)
2231 physical_sector_size = sector_size;
2232
Bob Liu3df0e502015-11-14 11:12:12 +08002233 blkfront_gather_backend_features(info);
2234 for (i = 0; i < info->nr_rings; i++) {
2235 err = blkfront_setup_indirect(&info->rinfo[i]);
2236 if (err) {
2237 xenbus_dev_fatal(info->xbdev, err, "setup_indirect at %s",
2238 info->xbdev->otherend);
2239 blkif_free(info, 0);
2240 break;
2241 }
Roger Pau Monne402b27f2013-04-18 16:06:54 +02002242 }
2243
Stefan Bader7c4d7d72013-05-13 16:28:15 +02002244 err = xlvbd_alloc_gendisk(sectors, info, binfo, sector_size,
2245 physical_sector_size);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07002246 if (err) {
2247 xenbus_dev_fatal(info->xbdev, err, "xlvbd_add at %s",
2248 info->xbdev->otherend);
2249 return;
2250 }
2251
2252 xenbus_switch_state(info->xbdev, XenbusStateConnected);
2253
2254 /* Kick pending requests. */
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07002255 info->connected = BLKIF_STATE_CONNECTED;
Bob Liu3df0e502015-11-14 11:12:12 +08002256 for (i = 0; i < info->nr_rings; i++)
2257 kick_pending_request_queues(&info->rinfo[i]);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07002258
2259 add_disk(info->gd);
Christian Limpach1d78d702008-04-02 10:54:04 -07002260
2261 info->is_ready = 1;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07002262}
2263
2264/**
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07002265 * Callback received when the backend's state changes.
2266 */
Ian Campbell203fd612009-12-04 15:33:54 +00002267static void blkback_changed(struct xenbus_device *dev,
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07002268 enum xenbus_state backend_state)
2269{
Greg Kroah-Hartmana1b4b122009-04-30 14:43:31 -07002270 struct blkfront_info *info = dev_get_drvdata(&dev->dev);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07002271
Ian Campbell203fd612009-12-04 15:33:54 +00002272 dev_dbg(&dev->dev, "blkfront:blkback_changed to state %d.\n", backend_state);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07002273
2274 switch (backend_state) {
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07002275 case XenbusStateInitWait:
Bob Liua9b54bb2015-06-19 00:23:00 -04002276 if (dev->state != XenbusStateInitialising)
2277 break;
Bob Liu8ab01442015-06-03 13:40:02 +08002278 if (talk_to_blkback(dev, info)) {
2279 kfree(info);
2280 dev_set_drvdata(&dev->dev, NULL);
2281 break;
2282 }
2283 case XenbusStateInitialising:
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07002284 case XenbusStateInitialised:
Noboru Iwamatsub78c9512009-10-13 17:22:29 -04002285 case XenbusStateReconfiguring:
2286 case XenbusStateReconfigured:
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07002287 case XenbusStateUnknown:
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07002288 break;
2289
2290 case XenbusStateConnected:
2291 blkfront_connect(info);
2292 break;
2293
David Vrabel36613712014-02-04 18:53:56 +00002294 case XenbusStateClosed:
2295 if (dev->state == XenbusStateClosed)
2296 break;
2297 /* Missed the backend's Closing state -- fallthrough */
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07002298 case XenbusStateClosing:
Cathy Averya54c8f02015-10-02 09:35:01 -04002299 if (info)
2300 blkfront_closing(info);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07002301 break;
2302 }
2303}
2304
Daniel Stoddenfa1bd352010-04-30 22:01:22 +00002305static int blkfront_remove(struct xenbus_device *xbdev)
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07002306{
Daniel Stoddenfa1bd352010-04-30 22:01:22 +00002307 struct blkfront_info *info = dev_get_drvdata(&xbdev->dev);
2308 struct block_device *bdev = NULL;
2309 struct gendisk *disk;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07002310
Daniel Stoddenfa1bd352010-04-30 22:01:22 +00002311 dev_dbg(&xbdev->dev, "%s removed", xbdev->nodename);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07002312
2313 blkif_free(info, 0);
2314
Daniel Stoddenfa1bd352010-04-30 22:01:22 +00002315 mutex_lock(&info->mutex);
2316
2317 disk = info->gd;
2318 if (disk)
2319 bdev = bdget_disk(disk, 0);
2320
2321 info->xbdev = NULL;
2322 mutex_unlock(&info->mutex);
2323
2324 if (!bdev) {
Jan Beulich0e345822010-08-07 18:28:55 +02002325 kfree(info);
Daniel Stoddenfa1bd352010-04-30 22:01:22 +00002326 return 0;
2327 }
2328
2329 /*
2330 * The xbdev was removed before we reached the Closed
2331 * state. See if it's safe to remove the disk. If the bdev
2332 * isn't closed yet, we let release take care of it.
2333 */
2334
2335 mutex_lock(&bdev->bd_mutex);
2336 info = disk->private_data;
2337
Daniel Stoddend54142c2010-08-07 18:51:21 +02002338 dev_warn(disk_to_dev(disk),
2339 "%s was hot-unplugged, %d stale handles\n",
2340 xbdev->nodename, bdev->bd_openers);
2341
Daniel Stodden7b32d102010-04-30 22:01:23 +00002342 if (info && !bdev->bd_openers) {
Daniel Stoddenfa1bd352010-04-30 22:01:22 +00002343 xlvbd_release_gendisk(info);
2344 disk->private_data = NULL;
2345 kfree(info);
2346 }
2347
2348 mutex_unlock(&bdev->bd_mutex);
2349 bdput(bdev);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07002350
2351 return 0;
2352}
2353
Christian Limpach1d78d702008-04-02 10:54:04 -07002354static int blkfront_is_ready(struct xenbus_device *dev)
2355{
Greg Kroah-Hartmana1b4b122009-04-30 14:43:31 -07002356 struct blkfront_info *info = dev_get_drvdata(&dev->dev);
Christian Limpach1d78d702008-04-02 10:54:04 -07002357
Jan Beulich5d7ed202010-08-07 18:31:12 +02002358 return info->is_ready && info->xbdev;
Christian Limpach1d78d702008-04-02 10:54:04 -07002359}
2360
Al Viroa63c8482008-03-02 10:23:47 -05002361static int blkif_open(struct block_device *bdev, fmode_t mode)
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07002362{
Daniel Stodden13961742010-08-07 18:36:53 +02002363 struct gendisk *disk = bdev->bd_disk;
2364 struct blkfront_info *info;
2365 int err = 0;
Arnd Bergmann6e9624b2010-08-07 18:25:34 +02002366
Arnd Bergmann2a48fc02010-06-02 14:28:52 +02002367 mutex_lock(&blkfront_mutex);
Arnd Bergmann6e9624b2010-08-07 18:25:34 +02002368
Daniel Stodden13961742010-08-07 18:36:53 +02002369 info = disk->private_data;
2370 if (!info) {
2371 /* xbdev gone */
2372 err = -ERESTARTSYS;
2373 goto out;
2374 }
2375
2376 mutex_lock(&info->mutex);
2377
2378 if (!info->gd)
2379 /* xbdev is closed */
2380 err = -ERESTARTSYS;
2381
2382 mutex_unlock(&info->mutex);
2383
Daniel Stodden13961742010-08-07 18:36:53 +02002384out:
Arnd Bergmann2a48fc02010-06-02 14:28:52 +02002385 mutex_unlock(&blkfront_mutex);
Daniel Stodden13961742010-08-07 18:36:53 +02002386 return err;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07002387}
2388
Al Virodb2a1442013-05-05 21:52:57 -04002389static void blkif_release(struct gendisk *disk, fmode_t mode)
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07002390{
Al Viroa63c8482008-03-02 10:23:47 -05002391 struct blkfront_info *info = disk->private_data;
Daniel Stodden7fd152f2010-08-07 18:45:12 +02002392 struct block_device *bdev;
2393 struct xenbus_device *xbdev;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07002394
Arnd Bergmann2a48fc02010-06-02 14:28:52 +02002395 mutex_lock(&blkfront_mutex);
Daniel Stodden7fd152f2010-08-07 18:45:12 +02002396
2397 bdev = bdget_disk(disk, 0);
Daniel Stodden7fd152f2010-08-07 18:45:12 +02002398
Felipe Pena2f089cb2013-11-09 13:36:09 -02002399 if (!bdev) {
2400 WARN(1, "Block device %s yanked out from us!\n", disk->disk_name);
2401 goto out_mutex;
2402 }
Daniel Stoddenacfca3c2010-08-07 18:47:26 +02002403 if (bdev->bd_openers)
2404 goto out;
2405
Daniel Stodden7fd152f2010-08-07 18:45:12 +02002406 /*
2407 * Check if we have been instructed to close. We will have
2408 * deferred this request, because the bdev was still open.
2409 */
2410
2411 mutex_lock(&info->mutex);
2412 xbdev = info->xbdev;
2413
2414 if (xbdev && xbdev->state == XenbusStateClosing) {
2415 /* pending switch to state closed */
Daniel Stoddend54142c2010-08-07 18:51:21 +02002416 dev_info(disk_to_dev(bdev->bd_disk), "releasing disk\n");
Daniel Stodden7fd152f2010-08-07 18:45:12 +02002417 xlvbd_release_gendisk(info);
2418 xenbus_frontend_closed(info->xbdev);
2419 }
2420
2421 mutex_unlock(&info->mutex);
2422
2423 if (!xbdev) {
2424 /* sudden device removal */
Daniel Stoddend54142c2010-08-07 18:51:21 +02002425 dev_info(disk_to_dev(bdev->bd_disk), "releasing disk\n");
Daniel Stodden7fd152f2010-08-07 18:45:12 +02002426 xlvbd_release_gendisk(info);
2427 disk->private_data = NULL;
2428 kfree(info);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07002429 }
Daniel Stodden7fd152f2010-08-07 18:45:12 +02002430
Jens Axboea4cc14e2010-08-08 21:50:05 -04002431out:
Andrew Jonesdad5cf62012-02-16 13:16:25 +01002432 bdput(bdev);
Felipe Pena2f089cb2013-11-09 13:36:09 -02002433out_mutex:
Arnd Bergmann2a48fc02010-06-02 14:28:52 +02002434 mutex_unlock(&blkfront_mutex);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07002435}
2436
Alexey Dobriyan83d5cde2009-09-21 17:01:13 -07002437static const struct block_device_operations xlvbd_block_fops =
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07002438{
2439 .owner = THIS_MODULE,
Al Viroa63c8482008-03-02 10:23:47 -05002440 .open = blkif_open,
2441 .release = blkif_release,
Ian Campbell597592d2008-02-21 13:03:45 -08002442 .getgeo = blkif_getgeo,
Arnd Bergmann8a6cfeb2010-07-08 10:18:46 +02002443 .ioctl = blkif_ioctl,
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07002444};
2445
2446
Márton Némethec9c42e2010-01-10 13:39:52 +01002447static const struct xenbus_device_id blkfront_ids[] = {
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07002448 { "vbd" },
2449 { "" }
2450};
2451
David Vrabel95afae42014-09-08 17:30:41 +01002452static struct xenbus_driver blkfront_driver = {
2453 .ids = blkfront_ids,
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07002454 .probe = blkfront_probe,
2455 .remove = blkfront_remove,
2456 .resume = blkfront_resume,
Ian Campbell203fd612009-12-04 15:33:54 +00002457 .otherend_changed = blkback_changed,
Christian Limpach1d78d702008-04-02 10:54:04 -07002458 .is_ready = blkfront_is_ready,
David Vrabel95afae42014-09-08 17:30:41 +01002459};
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07002460
2461static int __init xlblk_init(void)
2462{
Laszlo Ersek469738e2011-10-07 21:34:38 +02002463 int ret;
Bob Liu28d949b2015-11-14 11:12:14 +08002464 int nr_cpus = num_online_cpus();
Laszlo Ersek469738e2011-10-07 21:34:38 +02002465
Jeremy Fitzhardinge6e833582008-08-19 13:16:17 -07002466 if (!xen_domain())
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07002467 return -ENODEV;
2468
Julien Grall9cce2912015-10-13 17:50:11 +01002469 if (xen_blkif_max_ring_order > XENBUS_MAX_RING_GRANT_ORDER) {
Bob Liu86839c52015-06-03 13:40:03 +08002470 pr_info("Invalid max_ring_order (%d), will use default max: %d.\n",
Julien Grall9cce2912015-10-13 17:50:11 +01002471 xen_blkif_max_ring_order, XENBUS_MAX_RING_GRANT_ORDER);
Bob Liu86839c52015-06-03 13:40:03 +08002472 xen_blkif_max_ring_order = 0;
2473 }
2474
Bob Liu28d949b2015-11-14 11:12:14 +08002475 if (xen_blkif_max_queues > nr_cpus) {
2476 pr_info("Invalid max_queues (%d), will use default max: %d.\n",
2477 xen_blkif_max_queues, nr_cpus);
2478 xen_blkif_max_queues = nr_cpus;
2479 }
2480
Konrad Rzeszutek Wilk51c71a32013-11-26 15:05:40 -05002481 if (!xen_has_pv_disk_devices())
Igor Mammedovb9136d22012-03-21 15:08:38 +01002482 return -ENODEV;
2483
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07002484 if (register_blkdev(XENVBD_MAJOR, DEV_NAME)) {
2485 printk(KERN_WARNING "xen_blk: can't get major %d with name %s\n",
2486 XENVBD_MAJOR, DEV_NAME);
2487 return -ENODEV;
2488 }
2489
Jan Beulich73db1442011-12-22 09:08:13 +00002490 ret = xenbus_register_frontend(&blkfront_driver);
Laszlo Ersek469738e2011-10-07 21:34:38 +02002491 if (ret) {
2492 unregister_blkdev(XENVBD_MAJOR, DEV_NAME);
2493 return ret;
2494 }
2495
2496 return 0;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07002497}
2498module_init(xlblk_init);
2499
2500
Jan Beulich5a60d0c2008-06-17 10:47:08 +02002501static void __exit xlblk_exit(void)
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07002502{
Jan Beulich86050672012-04-05 16:04:52 +01002503 xenbus_unregister_driver(&blkfront_driver);
2504 unregister_blkdev(XENVBD_MAJOR, DEV_NAME);
2505 kfree(minors);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07002506}
2507module_exit(xlblk_exit);
2508
2509MODULE_DESCRIPTION("Xen virtual block device frontend");
2510MODULE_LICENSE("GPL");
2511MODULE_ALIAS_BLOCKDEV_MAJOR(XENVBD_MAJOR);
Mark McLoughlind2f0c522008-04-02 10:54:05 -07002512MODULE_ALIAS("xen:vbd");
Mark McLoughlin4f93f09b2008-04-02 10:54:06 -07002513MODULE_ALIAS("xenblk");