blob: 2a8e7813bd1ad5522420a5babd801f3bda8ed4cd [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
Julien Grall6cc568332015-08-13 13:13:35 +010063/*
64 * The minimal size of segment supported by the block framework is PAGE_SIZE.
65 * When Linux is using a different page size than Xen, it may not be possible
66 * to put all the data in a single segment.
67 * This can happen when the backend doesn't support indirect descriptor and
68 * therefore the maximum amount of data that a request can carry is
69 * BLKIF_MAX_SEGMENTS_PER_REQUEST * XEN_PAGE_SIZE = 44KB
70 *
71 * Note that we only support one extra request. So the Linux page size
72 * should be <= ( 2 * BLKIF_MAX_SEGMENTS_PER_REQUEST * XEN_PAGE_SIZE) =
73 * 88KB.
74 */
75#define HAS_EXTRA_REQ (BLKIF_MAX_SEGMENTS_PER_REQUEST < XEN_PFN_PER_PAGE)
76
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -070077enum blkif_state {
78 BLKIF_STATE_DISCONNECTED,
79 BLKIF_STATE_CONNECTED,
80 BLKIF_STATE_SUSPENDED,
81};
82
Roger Pau Monne0a8704a2012-10-24 18:58:45 +020083struct grant {
84 grant_ref_t gref;
Julien Gralla7a6df22015-06-30 11:58:51 +010085 struct page *page;
Roger Pau Monne155b7ed2013-03-18 17:49:34 +010086 struct list_head node;
Roger Pau Monne0a8704a2012-10-24 18:58:45 +020087};
88
Julien Grall6cc568332015-08-13 13:13:35 +010089enum blk_req_status {
90 REQ_WAITING,
91 REQ_DONE,
92 REQ_ERROR,
93 REQ_EOPNOTSUPP,
94};
95
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -070096struct blk_shadow {
97 struct blkif_request req;
Jeremy Fitzhardingea945b982010-11-01 17:03:14 -040098 struct request *request;
Roger Pau Monne402b27f2013-04-18 16:06:54 +020099 struct grant **grants_used;
100 struct grant **indirect_grants;
Roger Pau Monneb7649152013-05-02 10:58:50 +0200101 struct scatterlist *sg;
Julien Grallc004a6f2015-07-22 16:44:54 +0100102 unsigned int num_sg;
Julien Grall6cc568332015-08-13 13:13:35 +0100103 enum blk_req_status status;
104
105 #define NO_ASSOCIATED_ID ~0UL
106 /*
107 * Id of the sibling if we ever need 2 requests when handling a
108 * block I/O request
109 */
110 unsigned long associated_id;
Roger Pau Monne402b27f2013-04-18 16:06:54 +0200111};
112
Christoph Hellwig26095872017-04-20 16:03:08 +0200113struct blkif_req {
Bart Van Assche31c4ccc2017-07-21 19:11:10 +0200114 blk_status_t error;
Christoph Hellwig26095872017-04-20 16:03:08 +0200115};
116
117static inline struct blkif_req *blkif_req(struct request *rq)
118{
119 return blk_mq_rq_to_pdu(rq);
120}
121
Arnd Bergmann2a48fc02010-06-02 14:28:52 +0200122static DEFINE_MUTEX(blkfront_mutex);
Alexey Dobriyan83d5cde2009-09-21 17:01:13 -0700123static const struct block_device_operations xlvbd_block_fops;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700124
Roger Pau Monne402b27f2013-04-18 16:06:54 +0200125/*
126 * Maximum number of segments in indirect requests, the actual value used by
127 * the frontend driver is the minimum of this value and the value provided
128 * by the backend driver.
129 */
130
131static unsigned int xen_blkif_max_segments = 32;
Jan Beulich14e710f2016-02-10 04:21:15 -0700132module_param_named(max_indirect_segments, xen_blkif_max_segments, uint,
133 S_IRUGO);
134MODULE_PARM_DESC(max_indirect_segments,
135 "Maximum amount of segments in indirect requests (default is 32)");
Roger Pau Monne402b27f2013-04-18 16:06:54 +0200136
Bob Liu28d949b2015-11-14 11:12:14 +0800137static unsigned int xen_blkif_max_queues = 4;
138module_param_named(max_queues, xen_blkif_max_queues, uint, S_IRUGO);
139MODULE_PARM_DESC(max_queues, "Maximum number of hardware queues/rings used per virtual disk");
140
Bob Liu86839c52015-06-03 13:40:03 +0800141/*
142 * Maximum order of pages to be used for the shared ring between front and
143 * backend, 4KB page granularity is used.
144 */
145static unsigned int xen_blkif_max_ring_order;
146module_param_named(max_ring_page_order, xen_blkif_max_ring_order, int, S_IRUGO);
147MODULE_PARM_DESC(max_ring_page_order, "Maximum order of pages to be used for the shared ring");
148
Julien Grallc004a6f2015-07-22 16:44:54 +0100149#define BLK_RING_SIZE(info) \
150 __CONST_RING_SIZE(blkif, XEN_PAGE_SIZE * (info)->nr_ring_pages)
151
152#define BLK_MAX_RING_SIZE \
Julien Grall9cce2912015-10-13 17:50:11 +0100153 __CONST_RING_SIZE(blkif, XEN_PAGE_SIZE * XENBUS_MAX_RING_GRANTS)
Julien Grallc004a6f2015-07-22 16:44:54 +0100154
Bob Liu86839c52015-06-03 13:40:03 +0800155/*
Konrad Rzeszutek Wilk6f03a7f2015-11-16 15:14:41 -0500156 * ring-ref%u i=(-1UL) would take 11 characters + 'ring-ref' is 8, so 19
157 * characters are enough. Define to 20 to keep consistent with backend.
Bob Liu86839c52015-06-03 13:40:03 +0800158 */
159#define RINGREF_NAME_LEN (20)
Bob Liu28d949b2015-11-14 11:12:14 +0800160/*
161 * queue-%u would take 7 + 10(UINT_MAX) = 17 characters.
162 */
163#define QUEUE_NAME_LEN (17)
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700164
165/*
Bob Liu81f35162015-11-14 11:12:11 +0800166 * Per-ring info.
167 * Every blkfront device can associate with one or more blkfront_ring_info,
168 * depending on how many hardware queues/rings to be used.
169 */
170struct blkfront_ring_info {
Bob Liu11659562015-11-14 11:12:13 +0800171 /* Lock to protect data in every ring buffer. */
172 spinlock_t ring_lock;
Bob Liu81f35162015-11-14 11:12:11 +0800173 struct blkif_front_ring ring;
174 unsigned int ring_ref[XENBUS_MAX_RING_GRANTS];
175 unsigned int evtchn, irq;
176 struct work_struct work;
177 struct gnttab_free_callback callback;
178 struct blk_shadow shadow[BLK_MAX_RING_SIZE];
179 struct list_head indirect_pages;
Bob Liu73716df2015-11-16 16:51:39 -0500180 struct list_head grants;
181 unsigned int persistent_gnts_c;
Bob Liu81f35162015-11-14 11:12:11 +0800182 unsigned long shadow_free;
183 struct blkfront_info *dev_info;
184};
185
186/*
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700187 * We have one of these per vbd, whether ide, scsi or 'other'. They
188 * hang in private_data off the gendisk structure. We may end up
189 * putting all kinds of interesting stuff here :-)
190 */
191struct blkfront_info
192{
Daniel Stoddenb70f5fa2010-04-30 22:01:19 +0000193 struct mutex mutex;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700194 struct xenbus_device *xbdev;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700195 struct gendisk *gd;
Bob Liu172335a2016-07-01 17:43:39 -0400196 u16 sector_size;
197 unsigned int physical_sector_size;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700198 int vdevice;
199 blkif_vdev_t handle;
200 enum blkif_state connected;
Bob Liu3df0e502015-11-14 11:12:12 +0800201 /* Number of pages per ring buffer. */
Bob Liu86839c52015-06-03 13:40:03 +0800202 unsigned int nr_ring_pages;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700203 struct request_queue *rq;
Jan Beulichb32728f2017-01-23 08:12:19 -0700204 unsigned int feature_flush:1;
205 unsigned int feature_fua:1;
Konrad Rzeszutek Wilk5ea42982011-10-12 16:23:30 -0400206 unsigned int feature_discard:1;
207 unsigned int feature_secdiscard:1;
Jan Beulichb32728f2017-01-23 08:12:19 -0700208 unsigned int feature_persistent:1;
Li Dongyanged30bf32011-09-01 18:39:09 +0800209 unsigned int discard_granularity;
210 unsigned int discard_alignment;
Julien Grallc004a6f2015-07-22 16:44:54 +0100211 /* Number of 4KB segments handled */
Roger Pau Monne402b27f2013-04-18 16:06:54 +0200212 unsigned int max_indirect_segments;
Christian Limpach1d78d702008-04-02 10:54:04 -0700213 int is_ready;
Bob Liu907c3eb2015-07-13 17:55:24 +0800214 struct blk_mq_tag_set tag_set;
Bob Liu3df0e502015-11-14 11:12:12 +0800215 struct blkfront_ring_info *rinfo;
216 unsigned int nr_rings;
Bob Liu7b427a52016-06-27 16:33:24 +0800217 /* Save uncomplete reqs and bios for migration. */
218 struct list_head requests;
219 struct bio_list bio_list;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700220};
221
Jan Beulich0e345822010-08-07 18:28:55 +0200222static unsigned int nr_minors;
223static unsigned long *minors;
224static DEFINE_SPINLOCK(minor_lock);
225
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700226#define GRANT_INVALID_REF 0
227
228#define PARTS_PER_DISK 16
Chris Lalancette9246b5f2008-09-17 14:30:32 -0700229#define PARTS_PER_EXT_DISK 256
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700230
231#define BLKIF_MAJOR(dev) ((dev)>>8)
232#define BLKIF_MINOR(dev) ((dev) & 0xff)
233
Chris Lalancette9246b5f2008-09-17 14:30:32 -0700234#define EXT_SHIFT 28
235#define EXTENDED (1<<EXT_SHIFT)
236#define VDEV_IS_EXTENDED(dev) ((dev)&(EXTENDED))
237#define BLKIF_MINOR_EXT(dev) ((dev)&(~EXTENDED))
Stefano Stabellinic80a4202010-12-02 17:55:00 +0000238#define EMULATED_HD_DISK_MINOR_OFFSET (0)
239#define EMULATED_HD_DISK_NAME_OFFSET (EMULATED_HD_DISK_MINOR_OFFSET / 256)
Stefan Bader196cfe22011-07-14 15:30:22 +0200240#define EMULATED_SD_DISK_MINOR_OFFSET (0)
241#define EMULATED_SD_DISK_NAME_OFFSET (EMULATED_SD_DISK_MINOR_OFFSET / 256)
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700242
Chris Lalancette9246b5f2008-09-17 14:30:32 -0700243#define DEV_NAME "xvd" /* name in /dev */
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700244
Julien Grallc004a6f2015-07-22 16:44:54 +0100245/*
246 * Grants are always the same size as a Xen page (i.e 4KB).
247 * A physical segment is always the same size as a Linux page.
248 * Number of grants per physical segment
249 */
250#define GRANTS_PER_PSEG (PAGE_SIZE / XEN_PAGE_SIZE)
251
252#define GRANTS_PER_INDIRECT_FRAME \
253 (XEN_PAGE_SIZE / sizeof(struct blkif_request_segment))
254
255#define PSEGS_PER_INDIRECT_FRAME \
256 (GRANTS_INDIRECT_FRAME / GRANTS_PSEGS)
257
258#define INDIRECT_GREFS(_grants) \
259 DIV_ROUND_UP(_grants, GRANTS_PER_INDIRECT_FRAME)
260
261#define GREFS(_psegs) ((_psegs) * GRANTS_PER_PSEG)
Roger Pau Monne402b27f2013-04-18 16:06:54 +0200262
Bob Liu81f35162015-11-14 11:12:11 +0800263static int blkfront_setup_indirect(struct blkfront_ring_info *rinfo);
Bob Liu3df0e502015-11-14 11:12:12 +0800264static void blkfront_gather_backend_features(struct blkfront_info *info);
Bhavesh Davda7ed8ce12017-12-22 14:17:13 -0800265static int negotiate_mq(struct blkfront_info *info);
Roger Pau Monne402b27f2013-04-18 16:06:54 +0200266
Bob Liu81f35162015-11-14 11:12:11 +0800267static int get_id_from_freelist(struct blkfront_ring_info *rinfo)
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700268{
Bob Liu81f35162015-11-14 11:12:11 +0800269 unsigned long free = rinfo->shadow_free;
270
271 BUG_ON(free >= BLK_RING_SIZE(rinfo->dev_info));
272 rinfo->shadow_free = rinfo->shadow[free].req.u.rw.id;
273 rinfo->shadow[free].req.u.rw.id = 0x0fffffee; /* debug */
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700274 return free;
275}
276
Bob Liu81f35162015-11-14 11:12:11 +0800277static int add_id_to_freelist(struct blkfront_ring_info *rinfo,
Konrad Rzeszutek Wilk6f03a7f2015-11-16 15:14:41 -0500278 unsigned long id)
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700279{
Bob Liu81f35162015-11-14 11:12:11 +0800280 if (rinfo->shadow[id].req.u.rw.id != id)
Konrad Rzeszutek Wilk6878c322012-05-25 17:34:51 -0400281 return -EINVAL;
Bob Liu81f35162015-11-14 11:12:11 +0800282 if (rinfo->shadow[id].request == NULL)
Konrad Rzeszutek Wilk6878c322012-05-25 17:34:51 -0400283 return -EINVAL;
Bob Liu81f35162015-11-14 11:12:11 +0800284 rinfo->shadow[id].req.u.rw.id = rinfo->shadow_free;
285 rinfo->shadow[id].request = NULL;
286 rinfo->shadow_free = id;
Konrad Rzeszutek Wilk6878c322012-05-25 17:34:51 -0400287 return 0;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700288}
289
Bob Liu81f35162015-11-14 11:12:11 +0800290static int fill_grant_buffer(struct blkfront_ring_info *rinfo, int num)
Roger Pau Monne9c1e0502013-03-18 17:49:35 +0100291{
Bob Liu81f35162015-11-14 11:12:11 +0800292 struct blkfront_info *info = rinfo->dev_info;
Roger Pau Monne9c1e0502013-03-18 17:49:35 +0100293 struct page *granted_page;
294 struct grant *gnt_list_entry, *n;
295 int i = 0;
296
Konrad Rzeszutek Wilk6f03a7f2015-11-16 15:14:41 -0500297 while (i < num) {
Roger Pau Monne9c1e0502013-03-18 17:49:35 +0100298 gnt_list_entry = kzalloc(sizeof(struct grant), GFP_NOIO);
299 if (!gnt_list_entry)
300 goto out_of_memory;
301
Roger Pau Monnebfe11d62013-10-29 18:31:14 +0100302 if (info->feature_persistent) {
303 granted_page = alloc_page(GFP_NOIO);
304 if (!granted_page) {
305 kfree(gnt_list_entry);
306 goto out_of_memory;
307 }
Julien Gralla7a6df22015-06-30 11:58:51 +0100308 gnt_list_entry->page = granted_page;
Roger Pau Monne9c1e0502013-03-18 17:49:35 +0100309 }
310
Roger Pau Monne9c1e0502013-03-18 17:49:35 +0100311 gnt_list_entry->gref = GRANT_INVALID_REF;
Bob Liu73716df2015-11-16 16:51:39 -0500312 list_add(&gnt_list_entry->node, &rinfo->grants);
Roger Pau Monne9c1e0502013-03-18 17:49:35 +0100313 i++;
314 }
315
316 return 0;
317
318out_of_memory:
319 list_for_each_entry_safe(gnt_list_entry, n,
Bob Liu73716df2015-11-16 16:51:39 -0500320 &rinfo->grants, node) {
Roger Pau Monne9c1e0502013-03-18 17:49:35 +0100321 list_del(&gnt_list_entry->node);
Roger Pau Monnebfe11d62013-10-29 18:31:14 +0100322 if (info->feature_persistent)
Julien Gralla7a6df22015-06-30 11:58:51 +0100323 __free_page(gnt_list_entry->page);
Roger Pau Monne9c1e0502013-03-18 17:49:35 +0100324 kfree(gnt_list_entry);
325 i--;
326 }
327 BUG_ON(i != 0);
328 return -ENOMEM;
329}
330
Bob Liu73716df2015-11-16 16:51:39 -0500331static struct grant *get_free_grant(struct blkfront_ring_info *rinfo)
Roger Pau Monne9c1e0502013-03-18 17:49:35 +0100332{
333 struct grant *gnt_list_entry;
Roger Pau Monne9c1e0502013-03-18 17:49:35 +0100334
Bob Liu73716df2015-11-16 16:51:39 -0500335 BUG_ON(list_empty(&rinfo->grants));
336 gnt_list_entry = list_first_entry(&rinfo->grants, struct grant,
Julien Grall4f503fb2015-07-01 14:10:38 +0100337 node);
Roger Pau Monne9c1e0502013-03-18 17:49:35 +0100338 list_del(&gnt_list_entry->node);
339
Julien Grall4f503fb2015-07-01 14:10:38 +0100340 if (gnt_list_entry->gref != GRANT_INVALID_REF)
Bob Liu73716df2015-11-16 16:51:39 -0500341 rinfo->persistent_gnts_c--;
Julien Grall4f503fb2015-07-01 14:10:38 +0100342
343 return gnt_list_entry;
344}
345
346static inline void grant_foreign_access(const struct grant *gnt_list_entry,
347 const struct blkfront_info *info)
348{
349 gnttab_page_grant_foreign_access_ref_one(gnt_list_entry->gref,
350 info->xbdev->otherend_id,
351 gnt_list_entry->page,
352 0);
353}
354
355static struct grant *get_grant(grant_ref_t *gref_head,
356 unsigned long gfn,
Bob Liu73716df2015-11-16 16:51:39 -0500357 struct blkfront_ring_info *rinfo)
Julien Grall4f503fb2015-07-01 14:10:38 +0100358{
Bob Liu73716df2015-11-16 16:51:39 -0500359 struct grant *gnt_list_entry = get_free_grant(rinfo);
360 struct blkfront_info *info = rinfo->dev_info;
Julien Grall4f503fb2015-07-01 14:10:38 +0100361
362 if (gnt_list_entry->gref != GRANT_INVALID_REF)
Roger Pau Monne9c1e0502013-03-18 17:49:35 +0100363 return gnt_list_entry;
Julien Grall4f503fb2015-07-01 14:10:38 +0100364
365 /* Assign a gref to this page */
366 gnt_list_entry->gref = gnttab_claim_grant_reference(gref_head);
367 BUG_ON(gnt_list_entry->gref == -ENOSPC);
368 if (info->feature_persistent)
369 grant_foreign_access(gnt_list_entry, info);
370 else {
371 /* Grant access to the GFN passed by the caller */
372 gnttab_grant_foreign_access_ref(gnt_list_entry->gref,
373 info->xbdev->otherend_id,
374 gfn, 0);
Roger Pau Monne9c1e0502013-03-18 17:49:35 +0100375 }
376
Julien Grall4f503fb2015-07-01 14:10:38 +0100377 return gnt_list_entry;
378}
379
380static struct grant *get_indirect_grant(grant_ref_t *gref_head,
Bob Liu73716df2015-11-16 16:51:39 -0500381 struct blkfront_ring_info *rinfo)
Julien Grall4f503fb2015-07-01 14:10:38 +0100382{
Bob Liu73716df2015-11-16 16:51:39 -0500383 struct grant *gnt_list_entry = get_free_grant(rinfo);
384 struct blkfront_info *info = rinfo->dev_info;
Julien Grall4f503fb2015-07-01 14:10:38 +0100385
386 if (gnt_list_entry->gref != GRANT_INVALID_REF)
387 return gnt_list_entry;
388
Roger Pau Monne9c1e0502013-03-18 17:49:35 +0100389 /* Assign a gref to this page */
390 gnt_list_entry->gref = gnttab_claim_grant_reference(gref_head);
391 BUG_ON(gnt_list_entry->gref == -ENOSPC);
Roger Pau Monnebfe11d62013-10-29 18:31:14 +0100392 if (!info->feature_persistent) {
Julien Grall4f503fb2015-07-01 14:10:38 +0100393 struct page *indirect_page;
394
395 /* Fetch a pre-allocated page to use for indirect grefs */
Bob Liu73716df2015-11-16 16:51:39 -0500396 BUG_ON(list_empty(&rinfo->indirect_pages));
397 indirect_page = list_first_entry(&rinfo->indirect_pages,
Julien Grall4f503fb2015-07-01 14:10:38 +0100398 struct page, lru);
399 list_del(&indirect_page->lru);
400 gnt_list_entry->page = indirect_page;
Roger Pau Monnebfe11d62013-10-29 18:31:14 +0100401 }
Julien Grall4f503fb2015-07-01 14:10:38 +0100402 grant_foreign_access(gnt_list_entry, info);
403
Roger Pau Monne9c1e0502013-03-18 17:49:35 +0100404 return gnt_list_entry;
405}
406
Konrad Rzeszutek Wilk6878c322012-05-25 17:34:51 -0400407static const char *op_name(int op)
408{
409 static const char *const names[] = {
410 [BLKIF_OP_READ] = "read",
411 [BLKIF_OP_WRITE] = "write",
412 [BLKIF_OP_WRITE_BARRIER] = "barrier",
413 [BLKIF_OP_FLUSH_DISKCACHE] = "flush",
414 [BLKIF_OP_DISCARD] = "discard" };
415
416 if (op < 0 || op >= ARRAY_SIZE(names))
417 return "unknown";
418
419 if (!names[op])
420 return "reserved";
421
422 return names[op];
423}
Jan Beulich0e345822010-08-07 18:28:55 +0200424static int xlbd_reserve_minors(unsigned int minor, unsigned int nr)
425{
426 unsigned int end = minor + nr;
427 int rc;
428
429 if (end > nr_minors) {
430 unsigned long *bitmap, *old;
431
Thomas Meyerf0941482011-11-29 22:08:00 +0100432 bitmap = kcalloc(BITS_TO_LONGS(end), sizeof(*bitmap),
Jan Beulich0e345822010-08-07 18:28:55 +0200433 GFP_KERNEL);
434 if (bitmap == NULL)
435 return -ENOMEM;
436
437 spin_lock(&minor_lock);
438 if (end > nr_minors) {
439 old = minors;
440 memcpy(bitmap, minors,
441 BITS_TO_LONGS(nr_minors) * sizeof(*bitmap));
442 minors = bitmap;
443 nr_minors = BITS_TO_LONGS(end) * BITS_PER_LONG;
444 } else
445 old = bitmap;
446 spin_unlock(&minor_lock);
447 kfree(old);
448 }
449
450 spin_lock(&minor_lock);
451 if (find_next_bit(minors, end, minor) >= end) {
Akinobu Mita34ae2e42012-01-21 00:15:26 +0900452 bitmap_set(minors, minor, nr);
Jan Beulich0e345822010-08-07 18:28:55 +0200453 rc = 0;
454 } else
455 rc = -EBUSY;
456 spin_unlock(&minor_lock);
457
458 return rc;
459}
460
461static void xlbd_release_minors(unsigned int minor, unsigned int nr)
462{
463 unsigned int end = minor + nr;
464
465 BUG_ON(end > nr_minors);
466 spin_lock(&minor_lock);
Akinobu Mita34ae2e42012-01-21 00:15:26 +0900467 bitmap_clear(minors, minor, nr);
Jan Beulich0e345822010-08-07 18:28:55 +0200468 spin_unlock(&minor_lock);
469}
470
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700471static void blkif_restart_queue_callback(void *arg)
472{
Bob Liu81f35162015-11-14 11:12:11 +0800473 struct blkfront_ring_info *rinfo = (struct blkfront_ring_info *)arg;
474 schedule_work(&rinfo->work);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700475}
476
Harvey Harrisonafe42d72008-04-29 00:59:47 -0700477static int blkif_getgeo(struct block_device *bd, struct hd_geometry *hg)
Ian Campbell597592d2008-02-21 13:03:45 -0800478{
479 /* We don't have real geometry info, but let's at least return
480 values consistent with the size of the device */
481 sector_t nsect = get_capacity(bd->bd_disk);
482 sector_t cylinders = nsect;
483
484 hg->heads = 0xff;
485 hg->sectors = 0x3f;
486 sector_div(cylinders, hg->heads * hg->sectors);
487 hg->cylinders = cylinders;
488 if ((sector_t)(hg->cylinders + 1) * hg->heads * hg->sectors < nsect)
489 hg->cylinders = 0xffff;
490 return 0;
491}
492
Al Viroa63c8482008-03-02 10:23:47 -0500493static int blkif_ioctl(struct block_device *bdev, fmode_t mode,
Adrian Bunk62aa0052008-08-04 11:59:05 +0200494 unsigned command, unsigned long argument)
Christian Limpach440a01a2008-06-17 10:47:08 +0200495{
Al Viroa63c8482008-03-02 10:23:47 -0500496 struct blkfront_info *info = bdev->bd_disk->private_data;
Christian Limpach440a01a2008-06-17 10:47:08 +0200497 int i;
498
499 dev_dbg(&info->xbdev->dev, "command: 0x%x, argument: 0x%lx\n",
500 command, (long)argument);
501
502 switch (command) {
503 case CDROMMULTISESSION:
504 dev_dbg(&info->xbdev->dev, "FIXME: support multisession CDs later\n");
505 for (i = 0; i < sizeof(struct cdrom_multisession); i++)
506 if (put_user(0, (char __user *)(argument + i)))
507 return -EFAULT;
508 return 0;
509
510 case CDROM_GET_CAPABILITY: {
511 struct gendisk *gd = info->gd;
512 if (gd->flags & GENHD_FL_CD)
513 return 0;
514 return -EINVAL;
515 }
516
517 default:
518 /*printk(KERN_ALERT "ioctl %08x not supported by Xen blkdev\n",
519 command);*/
520 return -EINVAL; /* same return as native Linux */
521 }
522
523 return 0;
524}
525
Julien Grall2e073962015-08-13 19:23:10 +0100526static unsigned long blkif_ring_get_request(struct blkfront_ring_info *rinfo,
527 struct request *req,
528 struct blkif_request **ring_req)
529{
530 unsigned long id;
531
532 *ring_req = RING_GET_REQUEST(&rinfo->ring, rinfo->ring.req_prod_pvt);
533 rinfo->ring.req_prod_pvt++;
534
535 id = get_id_from_freelist(rinfo);
536 rinfo->shadow[id].request = req;
Julien Grall6cc568332015-08-13 13:13:35 +0100537 rinfo->shadow[id].status = REQ_WAITING;
538 rinfo->shadow[id].associated_id = NO_ASSOCIATED_ID;
Julien Grall2e073962015-08-13 19:23:10 +0100539
540 (*ring_req)->u.rw.id = id;
541
542 return id;
543}
544
Bob Liu81f35162015-11-14 11:12:11 +0800545static int blkif_queue_discard_req(struct request *req, struct blkfront_ring_info *rinfo)
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700546{
Bob Liu81f35162015-11-14 11:12:11 +0800547 struct blkfront_info *info = rinfo->dev_info;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700548 struct blkif_request *ring_req;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700549 unsigned long id;
Julien Grall33204662015-06-29 17:35:24 +0100550
551 /* Fill out a communications ring structure. */
Julien Grall2e073962015-08-13 19:23:10 +0100552 id = blkif_ring_get_request(rinfo, req, &ring_req);
Julien Grall33204662015-06-29 17:35:24 +0100553
554 ring_req->operation = BLKIF_OP_DISCARD;
555 ring_req->u.discard.nr_sectors = blk_rq_sectors(req);
556 ring_req->u.discard.id = id;
557 ring_req->u.discard.sector_number = (blkif_sector_t)blk_rq_pos(req);
Christoph Hellwig288dab82016-06-09 16:00:36 +0200558 if (req_op(req) == REQ_OP_SECURE_ERASE && info->feature_secdiscard)
Julien Grall33204662015-06-29 17:35:24 +0100559 ring_req->u.discard.flag = BLKIF_DISCARD_SECURE;
560 else
561 ring_req->u.discard.flag = 0;
562
Julien Grall33204662015-06-29 17:35:24 +0100563 /* Keep a private copy so we can reissue requests when recovering. */
Bob Liu81f35162015-11-14 11:12:11 +0800564 rinfo->shadow[id].req = *ring_req;
Julien Grall33204662015-06-29 17:35:24 +0100565
566 return 0;
567}
568
Julien Grallc004a6f2015-07-22 16:44:54 +0100569struct setup_rw_req {
570 unsigned int grant_idx;
571 struct blkif_request_segment *segments;
Bob Liu81f35162015-11-14 11:12:11 +0800572 struct blkfront_ring_info *rinfo;
Julien Grallc004a6f2015-07-22 16:44:54 +0100573 struct blkif_request *ring_req;
574 grant_ref_t gref_head;
575 unsigned int id;
576 /* Only used when persistent grant is used and it's a read request */
577 bool need_copy;
578 unsigned int bvec_off;
579 char *bvec_data;
Julien Grall6cc568332015-08-13 13:13:35 +0100580
581 bool require_extra_req;
582 struct blkif_request *extra_ring_req;
Julien Grallc004a6f2015-07-22 16:44:54 +0100583};
584
585static void blkif_setup_rw_req_grant(unsigned long gfn, unsigned int offset,
586 unsigned int len, void *data)
587{
588 struct setup_rw_req *setup = data;
589 int n, ref;
590 struct grant *gnt_list_entry;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700591 unsigned int fsect, lsect;
Julien Grallc004a6f2015-07-22 16:44:54 +0100592 /* Convenient aliases */
593 unsigned int grant_idx = setup->grant_idx;
594 struct blkif_request *ring_req = setup->ring_req;
Bob Liu81f35162015-11-14 11:12:11 +0800595 struct blkfront_ring_info *rinfo = setup->rinfo;
Julien Grall6cc568332015-08-13 13:13:35 +0100596 /*
597 * We always use the shadow of the first request to store the list
598 * of grant associated to the block I/O request. This made the
599 * completion more easy to handle even if the block I/O request is
600 * split.
601 */
Bob Liu81f35162015-11-14 11:12:11 +0800602 struct blk_shadow *shadow = &rinfo->shadow[setup->id];
Julien Grallc004a6f2015-07-22 16:44:54 +0100603
Julien Grall6cc568332015-08-13 13:13:35 +0100604 if (unlikely(setup->require_extra_req &&
605 grant_idx >= BLKIF_MAX_SEGMENTS_PER_REQUEST)) {
606 /*
607 * We are using the second request, setup grant_idx
608 * to be the index of the segment array.
609 */
610 grant_idx -= BLKIF_MAX_SEGMENTS_PER_REQUEST;
611 ring_req = setup->extra_ring_req;
612 }
613
Julien Grallc004a6f2015-07-22 16:44:54 +0100614 if ((ring_req->operation == BLKIF_OP_INDIRECT) &&
615 (grant_idx % GRANTS_PER_INDIRECT_FRAME == 0)) {
616 if (setup->segments)
617 kunmap_atomic(setup->segments);
618
619 n = grant_idx / GRANTS_PER_INDIRECT_FRAME;
Bob Liu73716df2015-11-16 16:51:39 -0500620 gnt_list_entry = get_indirect_grant(&setup->gref_head, rinfo);
Julien Grallc004a6f2015-07-22 16:44:54 +0100621 shadow->indirect_grants[n] = gnt_list_entry;
622 setup->segments = kmap_atomic(gnt_list_entry->page);
623 ring_req->u.indirect.indirect_grefs[n] = gnt_list_entry->gref;
624 }
625
Bob Liu73716df2015-11-16 16:51:39 -0500626 gnt_list_entry = get_grant(&setup->gref_head, gfn, rinfo);
Julien Grallc004a6f2015-07-22 16:44:54 +0100627 ref = gnt_list_entry->gref;
Julien Grall6cc568332015-08-13 13:13:35 +0100628 /*
629 * All the grants are stored in the shadow of the first
630 * request. Therefore we have to use the global index.
631 */
632 shadow->grants_used[setup->grant_idx] = gnt_list_entry;
Julien Grallc004a6f2015-07-22 16:44:54 +0100633
634 if (setup->need_copy) {
635 void *shared_data;
636
637 shared_data = kmap_atomic(gnt_list_entry->page);
638 /*
639 * this does not wipe data stored outside the
640 * range sg->offset..sg->offset+sg->length.
641 * Therefore, blkback *could* see data from
642 * previous requests. This is OK as long as
643 * persistent grants are shared with just one
644 * domain. It may need refactoring if this
645 * changes
646 */
647 memcpy(shared_data + offset,
648 setup->bvec_data + setup->bvec_off,
649 len);
650
651 kunmap_atomic(shared_data);
652 setup->bvec_off += len;
653 }
654
655 fsect = offset >> 9;
656 lsect = fsect + (len >> 9) - 1;
657 if (ring_req->operation != BLKIF_OP_INDIRECT) {
658 ring_req->u.rw.seg[grant_idx] =
659 (struct blkif_request_segment) {
660 .gref = ref,
661 .first_sect = fsect,
662 .last_sect = lsect };
663 } else {
664 setup->segments[grant_idx % GRANTS_PER_INDIRECT_FRAME] =
665 (struct blkif_request_segment) {
666 .gref = ref,
667 .first_sect = fsect,
668 .last_sect = lsect };
669 }
670
671 (setup->grant_idx)++;
672}
673
Julien Grall6cc568332015-08-13 13:13:35 +0100674static void blkif_setup_extra_req(struct blkif_request *first,
675 struct blkif_request *second)
676{
677 uint16_t nr_segments = first->u.rw.nr_segments;
678
679 /*
680 * The second request is only present when the first request uses
681 * all its segments. It's always the continuity of the first one.
682 */
683 first->u.rw.nr_segments = BLKIF_MAX_SEGMENTS_PER_REQUEST;
684
685 second->u.rw.nr_segments = nr_segments - BLKIF_MAX_SEGMENTS_PER_REQUEST;
686 second->u.rw.sector_number = first->u.rw.sector_number +
687 (BLKIF_MAX_SEGMENTS_PER_REQUEST * XEN_PAGE_SIZE) / 512;
688
689 second->u.rw.handle = first->u.rw.handle;
690 second->operation = first->operation;
691}
692
Bob Liu81f35162015-11-14 11:12:11 +0800693static int blkif_queue_rw_req(struct request *req, struct blkfront_ring_info *rinfo)
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700694{
Bob Liu81f35162015-11-14 11:12:11 +0800695 struct blkfront_info *info = rinfo->dev_info;
Julien Grall6cc568332015-08-13 13:13:35 +0100696 struct blkif_request *ring_req, *extra_ring_req = NULL;
697 unsigned long id, extra_id = NO_ASSOCIATED_ID;
698 bool require_extra_req = false;
Julien Grallc004a6f2015-07-22 16:44:54 +0100699 int i;
700 struct setup_rw_req setup = {
701 .grant_idx = 0,
702 .segments = NULL,
Bob Liu81f35162015-11-14 11:12:11 +0800703 .rinfo = rinfo,
Julien Grallc004a6f2015-07-22 16:44:54 +0100704 .need_copy = rq_data_dir(req) && info->feature_persistent,
705 };
Roger Pau Monne0a8704a2012-10-24 18:58:45 +0200706
707 /*
708 * Used to store if we are able to queue the request by just using
709 * existing persistent grants, or if we have to get new grants,
710 * as there are not sufficiently many free.
711 */
Dongli Zhangbd912ef32017-06-28 20:57:28 +0800712 bool new_persistent_gnts = false;
Jens Axboe9e973e62009-02-24 08:10:09 +0100713 struct scatterlist *sg;
Julien Grallc004a6f2015-07-22 16:44:54 +0100714 int num_sg, max_grefs, num_grant;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700715
Julien Grallc004a6f2015-07-22 16:44:54 +0100716 max_grefs = req->nr_phys_segments * GRANTS_PER_PSEG;
Roger Pau Monnec47206e2013-08-12 12:53:43 +0200717 if (max_grefs > BLKIF_MAX_SEGMENTS_PER_REQUEST)
718 /*
719 * If we are using indirect segments we need to account
720 * for the indirect grefs used in the request.
721 */
Julien Grallc004a6f2015-07-22 16:44:54 +0100722 max_grefs += INDIRECT_GREFS(max_grefs);
Roger Pau Monne402b27f2013-04-18 16:06:54 +0200723
Dongli Zhangbd912ef32017-06-28 20:57:28 +0800724 /* Check if we have enough persistent grants to allocate a requests */
725 if (rinfo->persistent_gnts_c < max_grefs) {
726 new_persistent_gnts = true;
727
728 if (gnttab_alloc_grant_references(
729 max_grefs - rinfo->persistent_gnts_c,
730 &setup.gref_head) < 0) {
Roger Pau Monne0a8704a2012-10-24 18:58:45 +0200731 gnttab_request_free_callback(
Bob Liu81f35162015-11-14 11:12:11 +0800732 &rinfo->callback,
Roger Pau Monne0a8704a2012-10-24 18:58:45 +0200733 blkif_restart_queue_callback,
Bob Liu81f35162015-11-14 11:12:11 +0800734 rinfo,
Dongli Zhangbd912ef32017-06-28 20:57:28 +0800735 max_grefs - rinfo->persistent_gnts_c);
Roger Pau Monne0a8704a2012-10-24 18:58:45 +0200736 return 1;
737 }
Dongli Zhangbd912ef32017-06-28 20:57:28 +0800738 }
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700739
740 /* Fill out a communications ring structure. */
Julien Grall2e073962015-08-13 19:23:10 +0100741 id = blkif_ring_get_request(rinfo, req, &ring_req);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700742
Bob Liu81f35162015-11-14 11:12:11 +0800743 num_sg = blk_rq_map_sg(req->q, req, rinfo->shadow[id].sg);
Julien Grallc004a6f2015-07-22 16:44:54 +0100744 num_grant = 0;
745 /* Calculate the number of grant used */
Bob Liu81f35162015-11-14 11:12:11 +0800746 for_each_sg(rinfo->shadow[id].sg, sg, num_sg, i)
Julien Grallc004a6f2015-07-22 16:44:54 +0100747 num_grant += gnttab_count_grant(sg->offset, sg->length);
748
Julien Grall6cc568332015-08-13 13:13:35 +0100749 require_extra_req = info->max_indirect_segments == 0 &&
750 num_grant > BLKIF_MAX_SEGMENTS_PER_REQUEST;
751 BUG_ON(!HAS_EXTRA_REQ && require_extra_req);
752
Bob Liu81f35162015-11-14 11:12:11 +0800753 rinfo->shadow[id].num_sg = num_sg;
Julien Grall6cc568332015-08-13 13:13:35 +0100754 if (num_grant > BLKIF_MAX_SEGMENTS_PER_REQUEST &&
755 likely(!require_extra_req)) {
Julien Grall33204662015-06-29 17:35:24 +0100756 /*
757 * The indirect operation can only be a BLKIF_OP_READ or
758 * BLKIF_OP_WRITE
759 */
Mike Christie3a5e02c2016-06-05 14:32:23 -0500760 BUG_ON(req_op(req) == REQ_OP_FLUSH || req->cmd_flags & REQ_FUA);
Julien Grall33204662015-06-29 17:35:24 +0100761 ring_req->operation = BLKIF_OP_INDIRECT;
762 ring_req->u.indirect.indirect_op = rq_data_dir(req) ?
763 BLKIF_OP_WRITE : BLKIF_OP_READ;
764 ring_req->u.indirect.sector_number = (blkif_sector_t)blk_rq_pos(req);
765 ring_req->u.indirect.handle = info->handle;
Julien Grallc004a6f2015-07-22 16:44:54 +0100766 ring_req->u.indirect.nr_segments = num_grant;
Li Dongyanged30bf32011-09-01 18:39:09 +0800767 } else {
Julien Grall33204662015-06-29 17:35:24 +0100768 ring_req->u.rw.sector_number = (blkif_sector_t)blk_rq_pos(req);
769 ring_req->u.rw.handle = info->handle;
770 ring_req->operation = rq_data_dir(req) ?
771 BLKIF_OP_WRITE : BLKIF_OP_READ;
Mike Christie3a5e02c2016-06-05 14:32:23 -0500772 if (req_op(req) == REQ_OP_FLUSH || req->cmd_flags & REQ_FUA) {
Roger Pau Monne402b27f2013-04-18 16:06:54 +0200773 /*
Julien Grall33204662015-06-29 17:35:24 +0100774 * Ideally we can do an unordered flush-to-disk.
775 * In case the backend onlysupports barriers, use that.
776 * A barrier request a superset of FUA, so we can
777 * implement it the same way. (It's also a FLUSH+FUA,
778 * since it is guaranteed ordered WRT previous writes.)
Roger Pau Monne402b27f2013-04-18 16:06:54 +0200779 */
Mike Christiea4180902016-06-05 14:32:24 -0500780 if (info->feature_flush && info->feature_fua)
Julien Grall33204662015-06-29 17:35:24 +0100781 ring_req->operation =
782 BLKIF_OP_WRITE_BARRIER;
Mike Christiea4180902016-06-05 14:32:24 -0500783 else if (info->feature_flush)
Julien Grall33204662015-06-29 17:35:24 +0100784 ring_req->operation =
785 BLKIF_OP_FLUSH_DISKCACHE;
Mike Christiea4180902016-06-05 14:32:24 -0500786 else
Julien Grall33204662015-06-29 17:35:24 +0100787 ring_req->operation = 0;
Li Dongyanged30bf32011-09-01 18:39:09 +0800788 }
Julien Grallc004a6f2015-07-22 16:44:54 +0100789 ring_req->u.rw.nr_segments = num_grant;
Julien Grall6cc568332015-08-13 13:13:35 +0100790 if (unlikely(require_extra_req)) {
791 extra_id = blkif_ring_get_request(rinfo, req,
792 &extra_ring_req);
793 /*
794 * Only the first request contains the scatter-gather
795 * list.
796 */
797 rinfo->shadow[extra_id].num_sg = 0;
798
799 blkif_setup_extra_req(ring_req, extra_ring_req);
800
801 /* Link the 2 requests together */
802 rinfo->shadow[extra_id].associated_id = id;
803 rinfo->shadow[id].associated_id = extra_id;
804 }
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700805 }
806
Julien Grallc004a6f2015-07-22 16:44:54 +0100807 setup.ring_req = ring_req;
808 setup.id = id;
Julien Grall6cc568332015-08-13 13:13:35 +0100809
810 setup.require_extra_req = require_extra_req;
811 if (unlikely(require_extra_req))
812 setup.extra_ring_req = extra_ring_req;
813
Bob Liu81f35162015-11-14 11:12:11 +0800814 for_each_sg(rinfo->shadow[id].sg, sg, num_sg, i) {
Julien Grallc004a6f2015-07-22 16:44:54 +0100815 BUG_ON(sg->offset + sg->length > PAGE_SIZE);
Julien Grall33204662015-06-29 17:35:24 +0100816
Julien Grallc004a6f2015-07-22 16:44:54 +0100817 if (setup.need_copy) {
818 setup.bvec_off = sg->offset;
819 setup.bvec_data = kmap_atomic(sg_page(sg));
Julien Grall33204662015-06-29 17:35:24 +0100820 }
821
Julien Grallc004a6f2015-07-22 16:44:54 +0100822 gnttab_foreach_grant_in_range(sg_page(sg),
823 sg->offset,
824 sg->length,
825 blkif_setup_rw_req_grant,
826 &setup);
Julien Grall33204662015-06-29 17:35:24 +0100827
Julien Grallc004a6f2015-07-22 16:44:54 +0100828 if (setup.need_copy)
829 kunmap_atomic(setup.bvec_data);
Julien Grall33204662015-06-29 17:35:24 +0100830 }
Julien Grallc004a6f2015-07-22 16:44:54 +0100831 if (setup.segments)
832 kunmap_atomic(setup.segments);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700833
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700834 /* Keep a private copy so we can reissue requests when recovering. */
Bob Liu81f35162015-11-14 11:12:11 +0800835 rinfo->shadow[id].req = *ring_req;
Julien Grall6cc568332015-08-13 13:13:35 +0100836 if (unlikely(require_extra_req))
837 rinfo->shadow[extra_id].req = *extra_ring_req;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700838
Dongli Zhangbd912ef32017-06-28 20:57:28 +0800839 if (new_persistent_gnts)
Julien Grallc004a6f2015-07-22 16:44:54 +0100840 gnttab_free_grant_references(setup.gref_head);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700841
842 return 0;
843}
844
Julien Grall33204662015-06-29 17:35:24 +0100845/*
846 * Generate a Xen blkfront IO request from a blk layer request. Reads
847 * and writes are handled as expected.
848 *
849 * @req: a request struct
850 */
Bob Liu81f35162015-11-14 11:12:11 +0800851static int blkif_queue_request(struct request *req, struct blkfront_ring_info *rinfo)
Julien Grall33204662015-06-29 17:35:24 +0100852{
Bob Liu81f35162015-11-14 11:12:11 +0800853 if (unlikely(rinfo->dev_info->connected != BLKIF_STATE_CONNECTED))
Julien Grall33204662015-06-29 17:35:24 +0100854 return 1;
855
Mike Christiec2df40d2016-06-05 14:32:17 -0500856 if (unlikely(req_op(req) == REQ_OP_DISCARD ||
Christoph Hellwig288dab82016-06-09 16:00:36 +0200857 req_op(req) == REQ_OP_SECURE_ERASE))
Bob Liu81f35162015-11-14 11:12:11 +0800858 return blkif_queue_discard_req(req, rinfo);
Julien Grall33204662015-06-29 17:35:24 +0100859 else
Bob Liu81f35162015-11-14 11:12:11 +0800860 return blkif_queue_rw_req(req, rinfo);
Julien Grall33204662015-06-29 17:35:24 +0100861}
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700862
Bob Liu81f35162015-11-14 11:12:11 +0800863static inline void flush_requests(struct blkfront_ring_info *rinfo)
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700864{
865 int notify;
866
Bob Liu81f35162015-11-14 11:12:11 +0800867 RING_PUSH_REQUESTS_AND_CHECK_NOTIFY(&rinfo->ring, notify);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700868
869 if (notify)
Bob Liu81f35162015-11-14 11:12:11 +0800870 notify_remote_via_irq(rinfo->irq);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700871}
872
Vitaly Kuznetsovad42d392014-12-01 14:01:13 +0100873static inline bool blkif_request_flush_invalid(struct request *req,
874 struct blkfront_info *info)
Arianna Avanzini0f1ca652014-08-22 13:20:02 +0200875{
Christoph Hellwigaebf5262017-01-31 16:57:31 +0100876 return (blk_rq_is_passthrough(req) ||
Mike Christie3a5e02c2016-06-05 14:32:23 -0500877 ((req_op(req) == REQ_OP_FLUSH) &&
Mike Christiea4180902016-06-05 14:32:24 -0500878 !info->feature_flush) ||
Vitaly Kuznetsovad42d392014-12-01 14:01:13 +0100879 ((req->cmd_flags & REQ_FUA) &&
Mike Christiea4180902016-06-05 14:32:24 -0500880 !info->feature_fua));
Arianna Avanzini0f1ca652014-08-22 13:20:02 +0200881}
882
Christoph Hellwigfc17b652017-06-03 09:38:05 +0200883static blk_status_t blkif_queue_rq(struct blk_mq_hw_ctx *hctx,
Konrad Rzeszutek Wilk6f03a7f2015-11-16 15:14:41 -0500884 const struct blk_mq_queue_data *qd)
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700885{
Bob Liu11659562015-11-14 11:12:13 +0800886 unsigned long flags;
Bob Liu2a6f71a2016-05-31 16:59:17 +0800887 int qid = hctx->queue_num;
888 struct blkfront_info *info = hctx->queue->queuedata;
889 struct blkfront_ring_info *rinfo = NULL;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700890
Bob Liu2a6f71a2016-05-31 16:59:17 +0800891 BUG_ON(info->nr_rings <= qid);
892 rinfo = &info->rinfo[qid];
Bob Liu907c3eb2015-07-13 17:55:24 +0800893 blk_mq_start_request(qd->rq);
Bob Liu11659562015-11-14 11:12:13 +0800894 spin_lock_irqsave(&rinfo->ring_lock, flags);
Bob Liu81f35162015-11-14 11:12:11 +0800895 if (RING_FULL(&rinfo->ring))
Bob Liu907c3eb2015-07-13 17:55:24 +0800896 goto out_busy;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700897
Bob Liu81f35162015-11-14 11:12:11 +0800898 if (blkif_request_flush_invalid(qd->rq, rinfo->dev_info))
Bob Liu907c3eb2015-07-13 17:55:24 +0800899 goto out_err;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700900
Bob Liu81f35162015-11-14 11:12:11 +0800901 if (blkif_queue_request(qd->rq, rinfo))
Bob Liu907c3eb2015-07-13 17:55:24 +0800902 goto out_busy;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700903
Bob Liu81f35162015-11-14 11:12:11 +0800904 flush_requests(rinfo);
Bob Liu11659562015-11-14 11:12:13 +0800905 spin_unlock_irqrestore(&rinfo->ring_lock, flags);
Christoph Hellwigfc17b652017-06-03 09:38:05 +0200906 return BLK_STS_OK;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700907
Bob Liu907c3eb2015-07-13 17:55:24 +0800908out_err:
Bob Liu11659562015-11-14 11:12:13 +0800909 spin_unlock_irqrestore(&rinfo->ring_lock, flags);
Christoph Hellwigfc17b652017-06-03 09:38:05 +0200910 return BLK_STS_IOERR;
Tejun Heo296b2f62009-05-08 11:54:15 +0900911
Bob Liu907c3eb2015-07-13 17:55:24 +0800912out_busy:
Bob Liu907c3eb2015-07-13 17:55:24 +0800913 blk_mq_stop_hw_queue(hctx);
Junxiao Bi4b422cb2017-07-20 09:26:21 +0800914 spin_unlock_irqrestore(&rinfo->ring_lock, flags);
Ming Lei86ff7c22018-01-30 22:04:57 -0500915 return BLK_STS_DEV_RESOURCE;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700916}
917
Christoph Hellwig26095872017-04-20 16:03:08 +0200918static void blkif_complete_rq(struct request *rq)
919{
920 blk_mq_end_request(rq, blkif_req(rq)->error);
921}
922
Eric Biggersf363b082017-03-30 13:39:16 -0700923static const struct blk_mq_ops blkfront_mq_ops = {
Bob Liu907c3eb2015-07-13 17:55:24 +0800924 .queue_rq = blkif_queue_rq,
Christoph Hellwig26095872017-04-20 16:03:08 +0200925 .complete = blkif_complete_rq,
Bob Liu907c3eb2015-07-13 17:55:24 +0800926};
927
Bob Liu172335a2016-07-01 17:43:39 -0400928static void blkif_set_queue_limits(struct blkfront_info *info)
929{
930 struct request_queue *rq = info->rq;
931 struct gendisk *gd = info->gd;
932 unsigned int segments = info->max_indirect_segments ? :
933 BLKIF_MAX_SEGMENTS_PER_REQUEST;
934
Bart Van Assche8b904b52018-03-07 17:10:10 -0800935 blk_queue_flag_set(QUEUE_FLAG_VIRT, rq);
Bob Liu172335a2016-07-01 17:43:39 -0400936
937 if (info->feature_discard) {
Bart Van Assche8b904b52018-03-07 17:10:10 -0800938 blk_queue_flag_set(QUEUE_FLAG_DISCARD, rq);
Bob Liu172335a2016-07-01 17:43:39 -0400939 blk_queue_max_discard_sectors(rq, get_capacity(gd));
940 rq->limits.discard_granularity = info->discard_granularity;
941 rq->limits.discard_alignment = info->discard_alignment;
942 if (info->feature_secdiscard)
Bart Van Assche8b904b52018-03-07 17:10:10 -0800943 blk_queue_flag_set(QUEUE_FLAG_SECERASE, rq);
Bob Liu172335a2016-07-01 17:43:39 -0400944 }
945
946 /* Hard sector size and max sectors impersonate the equiv. hardware. */
947 blk_queue_logical_block_size(rq, info->sector_size);
948 blk_queue_physical_block_size(rq, info->physical_sector_size);
949 blk_queue_max_hw_sectors(rq, (segments * XEN_PAGE_SIZE) / 512);
950
951 /* Each segment in a request is up to an aligned page in size. */
952 blk_queue_segment_boundary(rq, PAGE_SIZE - 1);
953 blk_queue_max_segment_size(rq, PAGE_SIZE);
954
955 /* Ensure a merged request will fit in a single I/O ring slot. */
956 blk_queue_max_segments(rq, segments / GRANTS_PER_PSEG);
957
958 /* Make sure buffer addresses are sector-aligned. */
959 blk_queue_dma_alignment(rq, 511);
Bob Liu172335a2016-07-01 17:43:39 -0400960}
961
Roger Pau Monne402b27f2013-04-18 16:06:54 +0200962static int xlvbd_init_blk_queue(struct gendisk *gd, u16 sector_size,
Bob Liu172335a2016-07-01 17:43:39 -0400963 unsigned int physical_sector_size)
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700964{
Jens Axboe165125e2007-07-24 09:28:11 +0200965 struct request_queue *rq;
Li Dongyanged30bf32011-09-01 18:39:09 +0800966 struct blkfront_info *info = gd->private_data;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700967
Bob Liu907c3eb2015-07-13 17:55:24 +0800968 memset(&info->tag_set, 0, sizeof(info->tag_set));
969 info->tag_set.ops = &blkfront_mq_ops;
Bob Liu28d949b2015-11-14 11:12:14 +0800970 info->tag_set.nr_hw_queues = info->nr_rings;
Julien Grall6cc568332015-08-13 13:13:35 +0100971 if (HAS_EXTRA_REQ && info->max_indirect_segments == 0) {
972 /*
973 * When indirect descriptior is not supported, the I/O request
974 * will be split between multiple request in the ring.
975 * To avoid problems when sending the request, divide by
976 * 2 the depth of the queue.
977 */
978 info->tag_set.queue_depth = BLK_RING_SIZE(info) / 2;
979 } else
980 info->tag_set.queue_depth = BLK_RING_SIZE(info);
Bob Liu907c3eb2015-07-13 17:55:24 +0800981 info->tag_set.numa_node = NUMA_NO_NODE;
982 info->tag_set.flags = BLK_MQ_F_SHOULD_MERGE | BLK_MQ_F_SG_MERGE;
Christoph Hellwig26095872017-04-20 16:03:08 +0200983 info->tag_set.cmd_size = sizeof(struct blkif_req);
Bob Liu907c3eb2015-07-13 17:55:24 +0800984 info->tag_set.driver_data = info;
985
986 if (blk_mq_alloc_tag_set(&info->tag_set))
Konrad Rzeszutek Wilkbde21f72015-11-25 13:07:39 -0500987 return -EINVAL;
Bob Liu907c3eb2015-07-13 17:55:24 +0800988 rq = blk_mq_init_queue(&info->tag_set);
989 if (IS_ERR(rq)) {
990 blk_mq_free_tag_set(&info->tag_set);
Konrad Rzeszutek Wilkbde21f72015-11-25 13:07:39 -0500991 return PTR_ERR(rq);
Bob Liu907c3eb2015-07-13 17:55:24 +0800992 }
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700993
Bob Liu2a6f71a2016-05-31 16:59:17 +0800994 rq->queuedata = info;
Bob Liu172335a2016-07-01 17:43:39 -0400995 info->rq = gd->queue = rq;
996 info->gd = gd;
997 info->sector_size = sector_size;
998 info->physical_sector_size = physical_sector_size;
999 blkif_set_queue_limits(info);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001000
1001 return 0;
1002}
1003
Mike Christiea4180902016-06-05 14:32:24 -05001004static const char *flush_info(struct blkfront_info *info)
Vitaly Kuznetsovfdf9b962014-12-09 15:25:10 +01001005{
Mike Christiea4180902016-06-05 14:32:24 -05001006 if (info->feature_flush && info->feature_fua)
Vitaly Kuznetsovfdf9b962014-12-09 15:25:10 +01001007 return "barrier: enabled;";
Mike Christiea4180902016-06-05 14:32:24 -05001008 else if (info->feature_flush)
Vitaly Kuznetsovfdf9b962014-12-09 15:25:10 +01001009 return "flush diskcache: enabled;";
Mike Christiea4180902016-06-05 14:32:24 -05001010 else
Vitaly Kuznetsovfdf9b962014-12-09 15:25:10 +01001011 return "barrier or flush: disabled;";
Vitaly Kuznetsovfdf9b962014-12-09 15:25:10 +01001012}
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001013
Tejun Heo4913efe2010-09-03 11:56:16 +02001014static void xlvbd_flush(struct blkfront_info *info)
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001015{
Mike Christiea4180902016-06-05 14:32:24 -05001016 blk_queue_write_cache(info->rq, info->feature_flush ? true : false,
1017 info->feature_fua ? true : false);
Vitaly Kuznetsovfdf9b962014-12-09 15:25:10 +01001018 pr_info("blkfront: %s: %s %s %s %s %s\n",
Mike Christiea4180902016-06-05 14:32:24 -05001019 info->gd->disk_name, flush_info(info),
Vitaly Kuznetsovfdf9b962014-12-09 15:25:10 +01001020 "persistent grants:", info->feature_persistent ?
1021 "enabled;" : "disabled;", "indirect descriptors:",
1022 info->max_indirect_segments ? "enabled;" : "disabled;");
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001023}
1024
Stefano Stabellinic80a4202010-12-02 17:55:00 +00001025static int xen_translate_vdev(int vdevice, int *minor, unsigned int *offset)
1026{
1027 int major;
1028 major = BLKIF_MAJOR(vdevice);
1029 *minor = BLKIF_MINOR(vdevice);
1030 switch (major) {
1031 case XEN_IDE0_MAJOR:
1032 *offset = (*minor / 64) + EMULATED_HD_DISK_NAME_OFFSET;
1033 *minor = ((*minor / 64) * PARTS_PER_DISK) +
1034 EMULATED_HD_DISK_MINOR_OFFSET;
1035 break;
1036 case XEN_IDE1_MAJOR:
1037 *offset = (*minor / 64) + 2 + EMULATED_HD_DISK_NAME_OFFSET;
1038 *minor = (((*minor / 64) + 2) * PARTS_PER_DISK) +
1039 EMULATED_HD_DISK_MINOR_OFFSET;
1040 break;
1041 case XEN_SCSI_DISK0_MAJOR:
1042 *offset = (*minor / PARTS_PER_DISK) + EMULATED_SD_DISK_NAME_OFFSET;
1043 *minor = *minor + EMULATED_SD_DISK_MINOR_OFFSET;
1044 break;
1045 case XEN_SCSI_DISK1_MAJOR:
1046 case XEN_SCSI_DISK2_MAJOR:
1047 case XEN_SCSI_DISK3_MAJOR:
1048 case XEN_SCSI_DISK4_MAJOR:
1049 case XEN_SCSI_DISK5_MAJOR:
1050 case XEN_SCSI_DISK6_MAJOR:
1051 case XEN_SCSI_DISK7_MAJOR:
1052 *offset = (*minor / PARTS_PER_DISK) +
1053 ((major - XEN_SCSI_DISK1_MAJOR + 1) * 16) +
1054 EMULATED_SD_DISK_NAME_OFFSET;
1055 *minor = *minor +
1056 ((major - XEN_SCSI_DISK1_MAJOR + 1) * 16 * PARTS_PER_DISK) +
1057 EMULATED_SD_DISK_MINOR_OFFSET;
1058 break;
1059 case XEN_SCSI_DISK8_MAJOR:
1060 case XEN_SCSI_DISK9_MAJOR:
1061 case XEN_SCSI_DISK10_MAJOR:
1062 case XEN_SCSI_DISK11_MAJOR:
1063 case XEN_SCSI_DISK12_MAJOR:
1064 case XEN_SCSI_DISK13_MAJOR:
1065 case XEN_SCSI_DISK14_MAJOR:
1066 case XEN_SCSI_DISK15_MAJOR:
1067 *offset = (*minor / PARTS_PER_DISK) +
1068 ((major - XEN_SCSI_DISK8_MAJOR + 8) * 16) +
1069 EMULATED_SD_DISK_NAME_OFFSET;
1070 *minor = *minor +
1071 ((major - XEN_SCSI_DISK8_MAJOR + 8) * 16 * PARTS_PER_DISK) +
1072 EMULATED_SD_DISK_MINOR_OFFSET;
1073 break;
1074 case XENVBD_MAJOR:
1075 *offset = *minor / PARTS_PER_DISK;
1076 break;
1077 default:
1078 printk(KERN_WARNING "blkfront: your disk configuration is "
1079 "incorrect, please use an xvd device instead\n");
1080 return -ENODEV;
1081 }
1082 return 0;
1083}
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001084
Jan Beuliche77c78c2012-04-05 16:37:22 +01001085static char *encode_disk_name(char *ptr, unsigned int n)
1086{
1087 if (n >= 26)
1088 ptr = encode_disk_name(ptr, n / 26 - 1);
1089 *ptr = 'a' + n % 26;
1090 return ptr + 1;
1091}
1092
Chris Lalancette9246b5f2008-09-17 14:30:32 -07001093static int xlvbd_alloc_gendisk(blkif_sector_t capacity,
1094 struct blkfront_info *info,
Stefan Bader7c4d7d72013-05-13 16:28:15 +02001095 u16 vdisk_info, u16 sector_size,
1096 unsigned int physical_sector_size)
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001097{
1098 struct gendisk *gd;
1099 int nr_minors = 1;
Stefano Stabellinic80a4202010-12-02 17:55:00 +00001100 int err;
Chris Lalancette9246b5f2008-09-17 14:30:32 -07001101 unsigned int offset;
1102 int minor;
1103 int nr_parts;
Jan Beuliche77c78c2012-04-05 16:37:22 +01001104 char *ptr;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001105
1106 BUG_ON(info->gd != NULL);
1107 BUG_ON(info->rq != NULL);
1108
Chris Lalancette9246b5f2008-09-17 14:30:32 -07001109 if ((info->vdevice>>EXT_SHIFT) > 1) {
1110 /* this is above the extended range; something is wrong */
1111 printk(KERN_WARNING "blkfront: vdevice 0x%x is above the extended range; ignoring\n", info->vdevice);
1112 return -ENODEV;
1113 }
1114
1115 if (!VDEV_IS_EXTENDED(info->vdevice)) {
Stefano Stabellinic80a4202010-12-02 17:55:00 +00001116 err = xen_translate_vdev(info->vdevice, &minor, &offset);
1117 if (err)
1118 return err;
1119 nr_parts = PARTS_PER_DISK;
Chris Lalancette9246b5f2008-09-17 14:30:32 -07001120 } else {
1121 minor = BLKIF_MINOR_EXT(info->vdevice);
1122 nr_parts = PARTS_PER_EXT_DISK;
Stefano Stabellinic80a4202010-12-02 17:55:00 +00001123 offset = minor / nr_parts;
Stefan Bader89153b52011-07-14 15:30:37 +02001124 if (xen_hvm_domain() && offset < EMULATED_HD_DISK_NAME_OFFSET + 4)
Stefano Stabellinic80a4202010-12-02 17:55:00 +00001125 printk(KERN_WARNING "blkfront: vdevice 0x%x might conflict with "
1126 "emulated IDE disks,\n\t choose an xvd device name"
1127 "from xvde on\n", info->vdevice);
Chris Lalancette9246b5f2008-09-17 14:30:32 -07001128 }
Jan Beuliche77c78c2012-04-05 16:37:22 +01001129 if (minor >> MINORBITS) {
1130 pr_warn("blkfront: %#x's minor (%#x) out of range; ignoring\n",
1131 info->vdevice, minor);
1132 return -ENODEV;
1133 }
Chris Lalancette9246b5f2008-09-17 14:30:32 -07001134
1135 if ((minor % nr_parts) == 0)
1136 nr_minors = nr_parts;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001137
Jan Beulich0e345822010-08-07 18:28:55 +02001138 err = xlbd_reserve_minors(minor, nr_minors);
1139 if (err)
1140 goto out;
1141 err = -ENODEV;
1142
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001143 gd = alloc_disk(nr_minors);
1144 if (gd == NULL)
Jan Beulich0e345822010-08-07 18:28:55 +02001145 goto release;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001146
Jan Beuliche77c78c2012-04-05 16:37:22 +01001147 strcpy(gd->disk_name, DEV_NAME);
1148 ptr = encode_disk_name(gd->disk_name + sizeof(DEV_NAME) - 1, offset);
1149 BUG_ON(ptr >= gd->disk_name + DISK_NAME_LEN);
1150 if (nr_minors > 1)
1151 *ptr = 0;
1152 else
1153 snprintf(ptr, gd->disk_name + DISK_NAME_LEN - ptr,
1154 "%d", minor & (nr_parts - 1));
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001155
1156 gd->major = XENVBD_MAJOR;
1157 gd->first_minor = minor;
1158 gd->fops = &xlvbd_block_fops;
1159 gd->private_data = info;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001160 set_capacity(gd, capacity);
1161
Bob Liu172335a2016-07-01 17:43:39 -04001162 if (xlvbd_init_blk_queue(gd, sector_size, physical_sector_size)) {
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001163 del_gendisk(gd);
Jan Beulich0e345822010-08-07 18:28:55 +02001164 goto release;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001165 }
1166
Tejun Heo4913efe2010-09-03 11:56:16 +02001167 xlvbd_flush(info);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001168
1169 if (vdisk_info & VDISK_READONLY)
1170 set_disk_ro(gd, 1);
1171
1172 if (vdisk_info & VDISK_REMOVABLE)
1173 gd->flags |= GENHD_FL_REMOVABLE;
1174
1175 if (vdisk_info & VDISK_CDROM)
1176 gd->flags |= GENHD_FL_CD;
1177
1178 return 0;
1179
Jan Beulich0e345822010-08-07 18:28:55 +02001180 release:
1181 xlbd_release_minors(minor, nr_minors);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001182 out:
1183 return err;
1184}
1185
Daniel Stoddena66b5ae2010-08-07 18:33:17 +02001186static void xlvbd_release_gendisk(struct blkfront_info *info)
1187{
Bob Liu3df0e502015-11-14 11:12:12 +08001188 unsigned int minor, nr_minors, i;
Daniel Stoddena66b5ae2010-08-07 18:33:17 +02001189
1190 if (info->rq == NULL)
1191 return;
1192
Daniel Stoddena66b5ae2010-08-07 18:33:17 +02001193 /* No more blkif_request(). */
Bob Liu907c3eb2015-07-13 17:55:24 +08001194 blk_mq_stop_hw_queues(info->rq);
Daniel Stoddena66b5ae2010-08-07 18:33:17 +02001195
Bob Liu3df0e502015-11-14 11:12:12 +08001196 for (i = 0; i < info->nr_rings; i++) {
1197 struct blkfront_ring_info *rinfo = &info->rinfo[i];
Daniel Stoddena66b5ae2010-08-07 18:33:17 +02001198
Bob Liu3df0e502015-11-14 11:12:12 +08001199 /* No more gnttab callback work. */
1200 gnttab_cancel_free_callback(&rinfo->callback);
1201
1202 /* Flush gnttab callback work. Must be done with no locks held. */
1203 flush_work(&rinfo->work);
1204 }
Daniel Stoddena66b5ae2010-08-07 18:33:17 +02001205
1206 del_gendisk(info->gd);
1207
1208 minor = info->gd->first_minor;
1209 nr_minors = info->gd->minors;
1210 xlbd_release_minors(minor, nr_minors);
1211
1212 blk_cleanup_queue(info->rq);
Bob Liu907c3eb2015-07-13 17:55:24 +08001213 blk_mq_free_tag_set(&info->tag_set);
Daniel Stoddena66b5ae2010-08-07 18:33:17 +02001214 info->rq = NULL;
1215
1216 put_disk(info->gd);
1217 info->gd = NULL;
1218}
1219
Bob Liu11659562015-11-14 11:12:13 +08001220/* Already hold rinfo->ring_lock. */
1221static inline void kick_pending_request_queues_locked(struct blkfront_ring_info *rinfo)
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001222{
Bob Liu81f35162015-11-14 11:12:11 +08001223 if (!RING_FULL(&rinfo->ring))
1224 blk_mq_start_stopped_hw_queues(rinfo->dev_info->rq, true);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001225}
1226
Bob Liu11659562015-11-14 11:12:13 +08001227static void kick_pending_request_queues(struct blkfront_ring_info *rinfo)
1228{
1229 unsigned long flags;
1230
1231 spin_lock_irqsave(&rinfo->ring_lock, flags);
1232 kick_pending_request_queues_locked(rinfo);
1233 spin_unlock_irqrestore(&rinfo->ring_lock, flags);
1234}
1235
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001236static void blkif_restart_queue(struct work_struct *work)
1237{
Bob Liu81f35162015-11-14 11:12:11 +08001238 struct blkfront_ring_info *rinfo = container_of(work, struct blkfront_ring_info, work);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001239
Bob Liu81f35162015-11-14 11:12:11 +08001240 if (rinfo->dev_info->connected == BLKIF_STATE_CONNECTED)
1241 kick_pending_request_queues(rinfo);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001242}
1243
Bob Liu3df0e502015-11-14 11:12:12 +08001244static void blkif_free_ring(struct blkfront_ring_info *rinfo)
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001245{
Bob Liu73716df2015-11-16 16:51:39 -05001246 struct grant *persistent_gnt, *n;
Bob Liu3df0e502015-11-14 11:12:12 +08001247 struct blkfront_info *info = rinfo->dev_info;
Roger Pau Monne402b27f2013-04-18 16:06:54 +02001248 int i, j, segs;
Roger Pau Monne0a8704a2012-10-24 18:58:45 +02001249
Roger Pau Monnebfe11d62013-10-29 18:31:14 +01001250 /*
1251 * Remove indirect pages, this only happens when using indirect
1252 * descriptors but not persistent grants
1253 */
Bob Liu81f35162015-11-14 11:12:11 +08001254 if (!list_empty(&rinfo->indirect_pages)) {
Roger Pau Monnebfe11d62013-10-29 18:31:14 +01001255 struct page *indirect_page, *n;
1256
1257 BUG_ON(info->feature_persistent);
Bob Liu81f35162015-11-14 11:12:11 +08001258 list_for_each_entry_safe(indirect_page, n, &rinfo->indirect_pages, lru) {
Roger Pau Monnebfe11d62013-10-29 18:31:14 +01001259 list_del(&indirect_page->lru);
1260 __free_page(indirect_page);
1261 }
1262 }
1263
Bob Liu73716df2015-11-16 16:51:39 -05001264 /* Remove all persistent grants. */
1265 if (!list_empty(&rinfo->grants)) {
1266 list_for_each_entry_safe(persistent_gnt, n,
1267 &rinfo->grants, node) {
1268 list_del(&persistent_gnt->node);
1269 if (persistent_gnt->gref != GRANT_INVALID_REF) {
1270 gnttab_end_foreign_access(persistent_gnt->gref,
1271 0, 0UL);
1272 rinfo->persistent_gnts_c--;
1273 }
1274 if (info->feature_persistent)
1275 __free_page(persistent_gnt->page);
1276 kfree(persistent_gnt);
1277 }
1278 }
1279 BUG_ON(rinfo->persistent_gnts_c != 0);
1280
Bob Liu86839c52015-06-03 13:40:03 +08001281 for (i = 0; i < BLK_RING_SIZE(info); i++) {
Roger Pau Monne402b27f2013-04-18 16:06:54 +02001282 /*
1283 * Clear persistent grants present in requests already
1284 * on the shared ring
1285 */
Bob Liu81f35162015-11-14 11:12:11 +08001286 if (!rinfo->shadow[i].request)
Roger Pau Monne402b27f2013-04-18 16:06:54 +02001287 goto free_shadow;
1288
Bob Liu81f35162015-11-14 11:12:11 +08001289 segs = rinfo->shadow[i].req.operation == BLKIF_OP_INDIRECT ?
1290 rinfo->shadow[i].req.u.indirect.nr_segments :
1291 rinfo->shadow[i].req.u.rw.nr_segments;
Roger Pau Monne402b27f2013-04-18 16:06:54 +02001292 for (j = 0; j < segs; j++) {
Bob Liu81f35162015-11-14 11:12:11 +08001293 persistent_gnt = rinfo->shadow[i].grants_used[j];
Roger Pau Monne402b27f2013-04-18 16:06:54 +02001294 gnttab_end_foreign_access(persistent_gnt->gref, 0, 0UL);
Roger Pau Monnebfe11d62013-10-29 18:31:14 +01001295 if (info->feature_persistent)
Julien Gralla7a6df22015-06-30 11:58:51 +01001296 __free_page(persistent_gnt->page);
Roger Pau Monne402b27f2013-04-18 16:06:54 +02001297 kfree(persistent_gnt);
1298 }
1299
Bob Liu81f35162015-11-14 11:12:11 +08001300 if (rinfo->shadow[i].req.operation != BLKIF_OP_INDIRECT)
Roger Pau Monne402b27f2013-04-18 16:06:54 +02001301 /*
1302 * If this is not an indirect operation don't try to
1303 * free indirect segments
1304 */
1305 goto free_shadow;
1306
1307 for (j = 0; j < INDIRECT_GREFS(segs); j++) {
Bob Liu81f35162015-11-14 11:12:11 +08001308 persistent_gnt = rinfo->shadow[i].indirect_grants[j];
Roger Pau Monne402b27f2013-04-18 16:06:54 +02001309 gnttab_end_foreign_access(persistent_gnt->gref, 0, 0UL);
Julien Gralla7a6df22015-06-30 11:58:51 +01001310 __free_page(persistent_gnt->page);
Roger Pau Monne402b27f2013-04-18 16:06:54 +02001311 kfree(persistent_gnt);
1312 }
1313
1314free_shadow:
Bob Liu81f35162015-11-14 11:12:11 +08001315 kfree(rinfo->shadow[i].grants_used);
1316 rinfo->shadow[i].grants_used = NULL;
1317 kfree(rinfo->shadow[i].indirect_grants);
1318 rinfo->shadow[i].indirect_grants = NULL;
1319 kfree(rinfo->shadow[i].sg);
1320 rinfo->shadow[i].sg = NULL;
Roger Pau Monne402b27f2013-04-18 16:06:54 +02001321 }
1322
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001323 /* No more gnttab callback work. */
Bob Liu81f35162015-11-14 11:12:11 +08001324 gnttab_cancel_free_callback(&rinfo->callback);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001325
1326 /* Flush gnttab callback work. Must be done with no locks held. */
Bob Liu81f35162015-11-14 11:12:11 +08001327 flush_work(&rinfo->work);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001328
1329 /* Free resources associated with old device channel. */
Bob Liu86839c52015-06-03 13:40:03 +08001330 for (i = 0; i < info->nr_ring_pages; i++) {
Bob Liu81f35162015-11-14 11:12:11 +08001331 if (rinfo->ring_ref[i] != GRANT_INVALID_REF) {
1332 gnttab_end_foreign_access(rinfo->ring_ref[i], 0, 0);
1333 rinfo->ring_ref[i] = GRANT_INVALID_REF;
Bob Liu86839c52015-06-03 13:40:03 +08001334 }
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001335 }
Bob Liu6c647b02016-07-01 15:45:57 -04001336 free_pages((unsigned long)rinfo->ring.sring, get_order(info->nr_ring_pages * XEN_PAGE_SIZE));
Bob Liu81f35162015-11-14 11:12:11 +08001337 rinfo->ring.sring = NULL;
Bob Liu86839c52015-06-03 13:40:03 +08001338
Bob Liu81f35162015-11-14 11:12:11 +08001339 if (rinfo->irq)
1340 unbind_from_irqhandler(rinfo->irq, rinfo);
1341 rinfo->evtchn = rinfo->irq = 0;
Bob Liu3df0e502015-11-14 11:12:12 +08001342}
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001343
Bob Liu3df0e502015-11-14 11:12:12 +08001344static void blkif_free(struct blkfront_info *info, int suspend)
1345{
Bob Liu3df0e502015-11-14 11:12:12 +08001346 unsigned int i;
1347
1348 /* Prevent new requests being issued until we fix things up. */
Bob Liu3df0e502015-11-14 11:12:12 +08001349 info->connected = suspend ?
1350 BLKIF_STATE_SUSPENDED : BLKIF_STATE_DISCONNECTED;
1351 /* No more blkif_request(). */
1352 if (info->rq)
1353 blk_mq_stop_hw_queues(info->rq);
1354
Bob Liu3df0e502015-11-14 11:12:12 +08001355 for (i = 0; i < info->nr_rings; i++)
1356 blkif_free_ring(&info->rinfo[i]);
1357
1358 kfree(info->rinfo);
1359 info->rinfo = NULL;
1360 info->nr_rings = 0;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001361}
1362
Julien Grallc004a6f2015-07-22 16:44:54 +01001363struct copy_from_grant {
1364 const struct blk_shadow *s;
1365 unsigned int grant_idx;
1366 unsigned int bvec_offset;
1367 char *bvec_data;
1368};
1369
1370static void blkif_copy_from_grant(unsigned long gfn, unsigned int offset,
1371 unsigned int len, void *data)
1372{
1373 struct copy_from_grant *info = data;
1374 char *shared_data;
1375 /* Convenient aliases */
1376 const struct blk_shadow *s = info->s;
1377
1378 shared_data = kmap_atomic(s->grants_used[info->grant_idx]->page);
1379
1380 memcpy(info->bvec_data + info->bvec_offset,
1381 shared_data + offset, len);
1382
1383 info->bvec_offset += len;
1384 info->grant_idx++;
1385
1386 kunmap_atomic(shared_data);
1387}
1388
Julien Grall6cc568332015-08-13 13:13:35 +01001389static enum blk_req_status blkif_rsp_to_req_status(int rsp)
1390{
1391 switch (rsp)
1392 {
1393 case BLKIF_RSP_OKAY:
1394 return REQ_DONE;
1395 case BLKIF_RSP_EOPNOTSUPP:
1396 return REQ_EOPNOTSUPP;
1397 case BLKIF_RSP_ERROR:
1398 /* Fallthrough. */
1399 default:
1400 return REQ_ERROR;
1401 }
1402}
1403
1404/*
1405 * Get the final status of the block request based on two ring response
1406 */
1407static int blkif_get_final_status(enum blk_req_status s1,
1408 enum blk_req_status s2)
1409{
1410 BUG_ON(s1 == REQ_WAITING);
1411 BUG_ON(s2 == REQ_WAITING);
1412
1413 if (s1 == REQ_ERROR || s2 == REQ_ERROR)
1414 return BLKIF_RSP_ERROR;
1415 else if (s1 == REQ_EOPNOTSUPP || s2 == REQ_EOPNOTSUPP)
1416 return BLKIF_RSP_EOPNOTSUPP;
1417 return BLKIF_RSP_OKAY;
1418}
1419
1420static bool blkif_completion(unsigned long *id,
1421 struct blkfront_ring_info *rinfo,
Roger Pau Monne0a8704a2012-10-24 18:58:45 +02001422 struct blkif_response *bret)
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001423{
Roger Pau Monned62f6912012-12-07 19:00:31 +01001424 int i = 0;
Roger Pau Monneb7649152013-05-02 10:58:50 +02001425 struct scatterlist *sg;
Julien Grallc004a6f2015-07-22 16:44:54 +01001426 int num_sg, num_grant;
Bob Liu81f35162015-11-14 11:12:11 +08001427 struct blkfront_info *info = rinfo->dev_info;
Julien Grall6cc568332015-08-13 13:13:35 +01001428 struct blk_shadow *s = &rinfo->shadow[*id];
Julien Grallc004a6f2015-07-22 16:44:54 +01001429 struct copy_from_grant data = {
Julien Grallc004a6f2015-07-22 16:44:54 +01001430 .grant_idx = 0,
1431 };
Roger Pau Monne402b27f2013-04-18 16:06:54 +02001432
Julien Grallc004a6f2015-07-22 16:44:54 +01001433 num_grant = s->req.operation == BLKIF_OP_INDIRECT ?
Roger Pau Monne402b27f2013-04-18 16:06:54 +02001434 s->req.u.indirect.nr_segments : s->req.u.rw.nr_segments;
Julien Grall6cc568332015-08-13 13:13:35 +01001435
1436 /* The I/O request may be split in two. */
1437 if (unlikely(s->associated_id != NO_ASSOCIATED_ID)) {
1438 struct blk_shadow *s2 = &rinfo->shadow[s->associated_id];
1439
1440 /* Keep the status of the current response in shadow. */
1441 s->status = blkif_rsp_to_req_status(bret->status);
1442
1443 /* Wait the second response if not yet here. */
1444 if (s2->status == REQ_WAITING)
1445 return 0;
1446
1447 bret->status = blkif_get_final_status(s->status,
1448 s2->status);
1449
1450 /*
1451 * All the grants is stored in the first shadow in order
1452 * to make the completion code simpler.
1453 */
1454 num_grant += s2->req.u.rw.nr_segments;
1455
1456 /*
1457 * The two responses may not come in order. Only the
1458 * first request will store the scatter-gather list.
1459 */
1460 if (s2->num_sg != 0) {
1461 /* Update "id" with the ID of the first response. */
1462 *id = s->associated_id;
1463 s = s2;
1464 }
1465
1466 /*
1467 * We don't need anymore the second request, so recycling
1468 * it now.
1469 */
1470 if (add_id_to_freelist(rinfo, s->associated_id))
1471 WARN(1, "%s: can't recycle the second part (id = %ld) of the request\n",
1472 info->gd->disk_name, s->associated_id);
1473 }
1474
1475 data.s = s;
Julien Grallc004a6f2015-07-22 16:44:54 +01001476 num_sg = s->num_sg;
Roger Pau Monne0a8704a2012-10-24 18:58:45 +02001477
Roger Pau Monnebfe11d62013-10-29 18:31:14 +01001478 if (bret->operation == BLKIF_OP_READ && info->feature_persistent) {
Julien Grallc004a6f2015-07-22 16:44:54 +01001479 for_each_sg(s->sg, sg, num_sg, i) {
Roger Pau Monneb7649152013-05-02 10:58:50 +02001480 BUG_ON(sg->offset + sg->length > PAGE_SIZE);
Julien Grallc004a6f2015-07-22 16:44:54 +01001481
1482 data.bvec_offset = sg->offset;
1483 data.bvec_data = kmap_atomic(sg_page(sg));
1484
1485 gnttab_foreach_grant_in_range(sg_page(sg),
1486 sg->offset,
1487 sg->length,
1488 blkif_copy_from_grant,
1489 &data);
1490
1491 kunmap_atomic(data.bvec_data);
Roger Pau Monne0a8704a2012-10-24 18:58:45 +02001492 }
1493 }
1494 /* Add the persistent grant into the list of free grants */
Julien Grallc004a6f2015-07-22 16:44:54 +01001495 for (i = 0; i < num_grant; i++) {
Roger Pau Monnefbe363c2013-08-12 12:53:44 +02001496 if (gnttab_query_foreign_access(s->grants_used[i]->gref)) {
1497 /*
1498 * If the grant is still mapped by the backend (the
1499 * backend has chosen to make this grant persistent)
1500 * we add it at the head of the list, so it will be
1501 * reused first.
1502 */
Roger Pau Monnebfe11d62013-10-29 18:31:14 +01001503 if (!info->feature_persistent)
1504 pr_alert_ratelimited("backed has not unmapped grant: %u\n",
1505 s->grants_used[i]->gref);
Bob Liu73716df2015-11-16 16:51:39 -05001506 list_add(&s->grants_used[i]->node, &rinfo->grants);
1507 rinfo->persistent_gnts_c++;
Roger Pau Monnefbe363c2013-08-12 12:53:44 +02001508 } else {
1509 /*
1510 * If the grant is not mapped by the backend we end the
1511 * foreign access and add it to the tail of the list,
1512 * so it will not be picked again unless we run out of
1513 * persistent grants.
1514 */
1515 gnttab_end_foreign_access(s->grants_used[i]->gref, 0, 0UL);
1516 s->grants_used[i]->gref = GRANT_INVALID_REF;
Bob Liu73716df2015-11-16 16:51:39 -05001517 list_add_tail(&s->grants_used[i]->node, &rinfo->grants);
Roger Pau Monnefbe363c2013-08-12 12:53:44 +02001518 }
Roger Pau Monne0a8704a2012-10-24 18:58:45 +02001519 }
Roger Pau Monne402b27f2013-04-18 16:06:54 +02001520 if (s->req.operation == BLKIF_OP_INDIRECT) {
Julien Grallc004a6f2015-07-22 16:44:54 +01001521 for (i = 0; i < INDIRECT_GREFS(num_grant); i++) {
Roger Pau Monnefbe363c2013-08-12 12:53:44 +02001522 if (gnttab_query_foreign_access(s->indirect_grants[i]->gref)) {
Roger Pau Monnebfe11d62013-10-29 18:31:14 +01001523 if (!info->feature_persistent)
1524 pr_alert_ratelimited("backed has not unmapped grant: %u\n",
1525 s->indirect_grants[i]->gref);
Bob Liu73716df2015-11-16 16:51:39 -05001526 list_add(&s->indirect_grants[i]->node, &rinfo->grants);
1527 rinfo->persistent_gnts_c++;
Roger Pau Monnefbe363c2013-08-12 12:53:44 +02001528 } else {
Roger Pau Monnebfe11d62013-10-29 18:31:14 +01001529 struct page *indirect_page;
1530
Roger Pau Monnefbe363c2013-08-12 12:53:44 +02001531 gnttab_end_foreign_access(s->indirect_grants[i]->gref, 0, 0UL);
Roger Pau Monnebfe11d62013-10-29 18:31:14 +01001532 /*
1533 * Add the used indirect page back to the list of
1534 * available pages for indirect grefs.
1535 */
Bob Liu7b076752015-07-22 14:40:09 +08001536 if (!info->feature_persistent) {
Julien Gralla7a6df22015-06-30 11:58:51 +01001537 indirect_page = s->indirect_grants[i]->page;
Bob Liu81f35162015-11-14 11:12:11 +08001538 list_add(&indirect_page->lru, &rinfo->indirect_pages);
Bob Liu7b076752015-07-22 14:40:09 +08001539 }
Roger Pau Monnefbe363c2013-08-12 12:53:44 +02001540 s->indirect_grants[i]->gref = GRANT_INVALID_REF;
Bob Liu73716df2015-11-16 16:51:39 -05001541 list_add_tail(&s->indirect_grants[i]->node, &rinfo->grants);
Roger Pau Monnefbe363c2013-08-12 12:53:44 +02001542 }
Roger Pau Monne402b27f2013-04-18 16:06:54 +02001543 }
1544 }
Julien Grall6cc568332015-08-13 13:13:35 +01001545
1546 return 1;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001547}
1548
1549static irqreturn_t blkif_interrupt(int irq, void *dev_id)
1550{
1551 struct request *req;
1552 struct blkif_response *bret;
1553 RING_IDX i, rp;
1554 unsigned long flags;
Bob Liu81f35162015-11-14 11:12:11 +08001555 struct blkfront_ring_info *rinfo = (struct blkfront_ring_info *)dev_id;
1556 struct blkfront_info *info = rinfo->dev_info;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001557
Bob Liu11659562015-11-14 11:12:13 +08001558 if (unlikely(info->connected != BLKIF_STATE_CONNECTED))
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001559 return IRQ_HANDLED;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001560
Bob Liu11659562015-11-14 11:12:13 +08001561 spin_lock_irqsave(&rinfo->ring_lock, flags);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001562 again:
Bob Liu81f35162015-11-14 11:12:11 +08001563 rp = rinfo->ring.sring->rsp_prod;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001564 rmb(); /* Ensure we see queued responses up to 'rp'. */
1565
Bob Liu81f35162015-11-14 11:12:11 +08001566 for (i = rinfo->ring.rsp_cons; i != rp; i++) {
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001567 unsigned long id;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001568
Bob Liu81f35162015-11-14 11:12:11 +08001569 bret = RING_GET_RESPONSE(&rinfo->ring, i);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001570 id = bret->id;
Konrad Rzeszutek Wilk6878c322012-05-25 17:34:51 -04001571 /*
1572 * The backend has messed up and given us an id that we would
1573 * never have given to it (we stamp it up to BLK_RING_SIZE -
1574 * look in get_id_from_freelist.
1575 */
Bob Liu86839c52015-06-03 13:40:03 +08001576 if (id >= BLK_RING_SIZE(info)) {
Konrad Rzeszutek Wilk6878c322012-05-25 17:34:51 -04001577 WARN(1, "%s: response to %s has incorrect id (%ld)\n",
1578 info->gd->disk_name, op_name(bret->operation), id);
1579 /* We can't safely get the 'struct request' as
1580 * the id is busted. */
1581 continue;
1582 }
Bob Liu81f35162015-11-14 11:12:11 +08001583 req = rinfo->shadow[id].request;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001584
Julien Grall6cc568332015-08-13 13:13:35 +01001585 if (bret->operation != BLKIF_OP_DISCARD) {
1586 /*
1587 * We may need to wait for an extra response if the
1588 * I/O request is split in 2
1589 */
1590 if (!blkif_completion(&id, rinfo, bret))
1591 continue;
1592 }
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001593
Bob Liu81f35162015-11-14 11:12:11 +08001594 if (add_id_to_freelist(rinfo, id)) {
Konrad Rzeszutek Wilk6878c322012-05-25 17:34:51 -04001595 WARN(1, "%s: response to %s (id %ld) couldn't be recycled!\n",
1596 info->gd->disk_name, op_name(bret->operation), id);
1597 continue;
1598 }
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001599
Christoph Hellwig2a842ac2017-06-03 09:38:04 +02001600 if (bret->status == BLKIF_RSP_OKAY)
1601 blkif_req(req)->error = BLK_STS_OK;
1602 else
1603 blkif_req(req)->error = BLK_STS_IOERR;
1604
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001605 switch (bret->operation) {
Li Dongyanged30bf32011-09-01 18:39:09 +08001606 case BLKIF_OP_DISCARD:
1607 if (unlikely(bret->status == BLKIF_RSP_EOPNOTSUPP)) {
1608 struct request_queue *rq = info->rq;
Konrad Rzeszutek Wilk6878c322012-05-25 17:34:51 -04001609 printk(KERN_WARNING "blkfront: %s: %s op failed\n",
1610 info->gd->disk_name, op_name(bret->operation));
Christoph Hellwig2a842ac2017-06-03 09:38:04 +02001611 blkif_req(req)->error = BLK_STS_NOTSUPP;
Li Dongyanged30bf32011-09-01 18:39:09 +08001612 info->feature_discard = 0;
Konrad Rzeszutek Wilk5ea42982011-10-12 16:23:30 -04001613 info->feature_secdiscard = 0;
Bart Van Assche8b904b52018-03-07 17:10:10 -08001614 blk_queue_flag_clear(QUEUE_FLAG_DISCARD, rq);
1615 blk_queue_flag_clear(QUEUE_FLAG_SECERASE, rq);
Li Dongyanged30bf32011-09-01 18:39:09 +08001616 }
Li Dongyanged30bf32011-09-01 18:39:09 +08001617 break;
Konrad Rzeszutek Wilkedf6ef52011-05-03 12:01:11 -04001618 case BLKIF_OP_FLUSH_DISKCACHE:
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001619 case BLKIF_OP_WRITE_BARRIER:
1620 if (unlikely(bret->status == BLKIF_RSP_EOPNOTSUPP)) {
Konrad Rzeszutek Wilk6878c322012-05-25 17:34:51 -04001621 printk(KERN_WARNING "blkfront: %s: %s op failed\n",
1622 info->gd->disk_name, op_name(bret->operation));
Bart Van Assche31c4ccc2017-07-21 19:11:10 +02001623 blkif_req(req)->error = BLK_STS_NOTSUPP;
Jeremy Fitzhardingedcb8bae2010-11-02 11:55:58 -04001624 }
1625 if (unlikely(bret->status == BLKIF_RSP_ERROR &&
Bob Liu81f35162015-11-14 11:12:11 +08001626 rinfo->shadow[id].req.u.rw.nr_segments == 0)) {
Konrad Rzeszutek Wilk6878c322012-05-25 17:34:51 -04001627 printk(KERN_WARNING "blkfront: %s: empty %s op failed\n",
1628 info->gd->disk_name, op_name(bret->operation));
Christoph Hellwig2a842ac2017-06-03 09:38:04 +02001629 blkif_req(req)->error = BLK_STS_NOTSUPP;
Jeremy Fitzhardingedcb8bae2010-11-02 11:55:58 -04001630 }
Christoph Hellwig26095872017-04-20 16:03:08 +02001631 if (unlikely(blkif_req(req)->error)) {
Christoph Hellwig2a842ac2017-06-03 09:38:04 +02001632 if (blkif_req(req)->error == BLK_STS_NOTSUPP)
1633 blkif_req(req)->error = BLK_STS_OK;
Mike Christiea4180902016-06-05 14:32:24 -05001634 info->feature_fua = 0;
Tejun Heo4913efe2010-09-03 11:56:16 +02001635 info->feature_flush = 0;
1636 xlvbd_flush(info);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001637 }
1638 /* fall through */
1639 case BLKIF_OP_READ:
1640 case BLKIF_OP_WRITE:
1641 if (unlikely(bret->status != BLKIF_RSP_OKAY))
1642 dev_dbg(&info->xbdev->dev, "Bad return from blkdev data "
1643 "request: %x\n", bret->status);
1644
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001645 break;
1646 default:
1647 BUG();
1648 }
Christoph Hellwig26095872017-04-20 16:03:08 +02001649
Christoph Hellwig08e00292017-04-20 16:03:09 +02001650 blk_mq_complete_request(req);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001651 }
1652
Bob Liu81f35162015-11-14 11:12:11 +08001653 rinfo->ring.rsp_cons = i;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001654
Bob Liu81f35162015-11-14 11:12:11 +08001655 if (i != rinfo->ring.req_prod_pvt) {
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001656 int more_to_do;
Bob Liu81f35162015-11-14 11:12:11 +08001657 RING_FINAL_CHECK_FOR_RESPONSES(&rinfo->ring, more_to_do);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001658 if (more_to_do)
1659 goto again;
1660 } else
Bob Liu81f35162015-11-14 11:12:11 +08001661 rinfo->ring.sring->rsp_event = i + 1;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001662
Bob Liu11659562015-11-14 11:12:13 +08001663 kick_pending_request_queues_locked(rinfo);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001664
Bob Liu11659562015-11-14 11:12:13 +08001665 spin_unlock_irqrestore(&rinfo->ring_lock, flags);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001666
1667 return IRQ_HANDLED;
1668}
1669
1670
1671static int setup_blkring(struct xenbus_device *dev,
Bob Liu81f35162015-11-14 11:12:11 +08001672 struct blkfront_ring_info *rinfo)
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001673{
1674 struct blkif_sring *sring;
Bob Liu86839c52015-06-03 13:40:03 +08001675 int err, i;
Bob Liu81f35162015-11-14 11:12:11 +08001676 struct blkfront_info *info = rinfo->dev_info;
Julien Grallc004a6f2015-07-22 16:44:54 +01001677 unsigned long ring_size = info->nr_ring_pages * XEN_PAGE_SIZE;
Julien Grall9cce2912015-10-13 17:50:11 +01001678 grant_ref_t gref[XENBUS_MAX_RING_GRANTS];
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001679
Bob Liu86839c52015-06-03 13:40:03 +08001680 for (i = 0; i < info->nr_ring_pages; i++)
Bob Liu81f35162015-11-14 11:12:11 +08001681 rinfo->ring_ref[i] = GRANT_INVALID_REF;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001682
Bob Liu86839c52015-06-03 13:40:03 +08001683 sring = (struct blkif_sring *)__get_free_pages(GFP_NOIO | __GFP_HIGH,
1684 get_order(ring_size));
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001685 if (!sring) {
1686 xenbus_dev_fatal(dev, -ENOMEM, "allocating shared ring");
1687 return -ENOMEM;
1688 }
1689 SHARED_RING_INIT(sring);
Bob Liu81f35162015-11-14 11:12:11 +08001690 FRONT_RING_INIT(&rinfo->ring, sring, ring_size);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001691
Bob Liu81f35162015-11-14 11:12:11 +08001692 err = xenbus_grant_ring(dev, rinfo->ring.sring, info->nr_ring_pages, gref);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001693 if (err < 0) {
Bob Liu86839c52015-06-03 13:40:03 +08001694 free_pages((unsigned long)sring, get_order(ring_size));
Bob Liu81f35162015-11-14 11:12:11 +08001695 rinfo->ring.sring = NULL;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001696 goto fail;
1697 }
Bob Liu86839c52015-06-03 13:40:03 +08001698 for (i = 0; i < info->nr_ring_pages; i++)
Bob Liu81f35162015-11-14 11:12:11 +08001699 rinfo->ring_ref[i] = gref[i];
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001700
Bob Liu81f35162015-11-14 11:12:11 +08001701 err = xenbus_alloc_evtchn(dev, &rinfo->evtchn);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001702 if (err)
1703 goto fail;
1704
Bob Liu81f35162015-11-14 11:12:11 +08001705 err = bind_evtchn_to_irqhandler(rinfo->evtchn, blkif_interrupt, 0,
1706 "blkif", rinfo);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001707 if (err <= 0) {
1708 xenbus_dev_fatal(dev, err,
1709 "bind_evtchn_to_irqhandler failed");
1710 goto fail;
1711 }
Bob Liu81f35162015-11-14 11:12:11 +08001712 rinfo->irq = err;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001713
1714 return 0;
1715fail:
1716 blkif_free(info, 0);
1717 return err;
1718}
1719
Bob Liu28d949b2015-11-14 11:12:14 +08001720/*
1721 * Write out per-ring/queue nodes including ring-ref and event-channel, and each
1722 * ring buffer may have multi pages depending on ->nr_ring_pages.
1723 */
1724static int write_per_ring_nodes(struct xenbus_transaction xbt,
1725 struct blkfront_ring_info *rinfo, const char *dir)
1726{
1727 int err;
1728 unsigned int i;
1729 const char *message = NULL;
1730 struct blkfront_info *info = rinfo->dev_info;
1731
1732 if (info->nr_ring_pages == 1) {
1733 err = xenbus_printf(xbt, dir, "ring-ref", "%u", rinfo->ring_ref[0]);
1734 if (err) {
1735 message = "writing ring-ref";
1736 goto abort_transaction;
1737 }
1738 } else {
1739 for (i = 0; i < info->nr_ring_pages; i++) {
1740 char ring_ref_name[RINGREF_NAME_LEN];
1741
1742 snprintf(ring_ref_name, RINGREF_NAME_LEN, "ring-ref%u", i);
1743 err = xenbus_printf(xbt, dir, ring_ref_name,
1744 "%u", rinfo->ring_ref[i]);
1745 if (err) {
1746 message = "writing ring-ref";
1747 goto abort_transaction;
1748 }
1749 }
1750 }
1751
1752 err = xenbus_printf(xbt, dir, "event-channel", "%u", rinfo->evtchn);
1753 if (err) {
1754 message = "writing event-channel";
1755 goto abort_transaction;
1756 }
1757
1758 return 0;
1759
1760abort_transaction:
1761 xenbus_transaction_end(xbt, 1);
1762 if (message)
1763 xenbus_dev_fatal(info->xbdev, err, "%s", message);
1764
1765 return err;
1766}
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001767
1768/* Common code used when first setting up, and when resuming. */
Ian Campbell203fd612009-12-04 15:33:54 +00001769static int talk_to_blkback(struct xenbus_device *dev,
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001770 struct blkfront_info *info)
1771{
1772 const char *message = NULL;
1773 struct xenbus_transaction xbt;
Bob Liu28d949b2015-11-14 11:12:14 +08001774 int err;
Juergen Grossf27dc1a2016-10-31 14:58:40 +01001775 unsigned int i, max_page_order;
1776 unsigned int ring_page_order;
Bob Liu86839c52015-06-03 13:40:03 +08001777
Bhavesh Davda7ed8ce12017-12-22 14:17:13 -08001778 if (!info)
1779 return -ENODEV;
1780
Juergen Grossf27dc1a2016-10-31 14:58:40 +01001781 max_page_order = xenbus_read_unsigned(info->xbdev->otherend,
1782 "max-ring-page-order", 0);
1783 ring_page_order = min(xen_blkif_max_ring_order, max_page_order);
1784 info->nr_ring_pages = 1 << ring_page_order;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001785
Bhavesh Davda7ed8ce12017-12-22 14:17:13 -08001786 err = negotiate_mq(info);
1787 if (err)
1788 goto destroy_blkring;
1789
Bob Liu3df0e502015-11-14 11:12:12 +08001790 for (i = 0; i < info->nr_rings; i++) {
Bob Liu28d949b2015-11-14 11:12:14 +08001791 struct blkfront_ring_info *rinfo = &info->rinfo[i];
1792
Bob Liu3df0e502015-11-14 11:12:12 +08001793 /* Create shared ring, alloc event channel. */
1794 err = setup_blkring(dev, rinfo);
1795 if (err)
1796 goto destroy_blkring;
1797 }
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001798
1799again:
1800 err = xenbus_transaction_start(&xbt);
1801 if (err) {
1802 xenbus_dev_fatal(dev, err, "starting transaction");
1803 goto destroy_blkring;
1804 }
1805
Bob Liu28d949b2015-11-14 11:12:14 +08001806 if (info->nr_ring_pages > 1) {
1807 err = xenbus_printf(xbt, dev->nodename, "ring-page-order", "%u",
1808 ring_page_order);
Bob Liu3df0e502015-11-14 11:12:12 +08001809 if (err) {
Bob Liu28d949b2015-11-14 11:12:14 +08001810 message = "writing ring-page-order";
Bob Liu3df0e502015-11-14 11:12:12 +08001811 goto abort_transaction;
1812 }
Bob Liu28d949b2015-11-14 11:12:14 +08001813 }
1814
1815 /* We already got the number of queues/rings in _probe */
1816 if (info->nr_rings == 1) {
1817 err = write_per_ring_nodes(xbt, &info->rinfo[0], dev->nodename);
1818 if (err)
1819 goto destroy_blkring;
Bob Liu3df0e502015-11-14 11:12:12 +08001820 } else {
Bob Liu28d949b2015-11-14 11:12:14 +08001821 char *path;
1822 size_t pathsize;
1823
1824 err = xenbus_printf(xbt, dev->nodename, "multi-queue-num-queues", "%u",
1825 info->nr_rings);
1826 if (err) {
1827 message = "writing multi-queue-num-queues";
1828 goto abort_transaction;
1829 }
1830
1831 pathsize = strlen(dev->nodename) + QUEUE_NAME_LEN;
1832 path = kmalloc(pathsize, GFP_KERNEL);
1833 if (!path) {
1834 err = -ENOMEM;
1835 message = "ENOMEM while writing ring references";
1836 goto abort_transaction;
1837 }
1838
1839 for (i = 0; i < info->nr_rings; i++) {
1840 memset(path, 0, pathsize);
1841 snprintf(path, pathsize, "%s/queue-%u", dev->nodename, i);
1842 err = write_per_ring_nodes(xbt, &info->rinfo[i], path);
1843 if (err) {
1844 kfree(path);
1845 goto destroy_blkring;
1846 }
1847 }
1848 kfree(path);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001849 }
Markus Armbruster3e334232008-04-02 10:54:02 -07001850 err = xenbus_printf(xbt, dev->nodename, "protocol", "%s",
1851 XEN_IO_PROTO_ABI_NATIVE);
1852 if (err) {
1853 message = "writing protocol";
1854 goto abort_transaction;
1855 }
Roger Pau Monne0a8704a2012-10-24 18:58:45 +02001856 err = xenbus_printf(xbt, dev->nodename,
Roger Pau Monnecb5bd4d2012-11-02 16:43:04 +01001857 "feature-persistent", "%u", 1);
Roger Pau Monne0a8704a2012-10-24 18:58:45 +02001858 if (err)
1859 dev_warn(&dev->dev,
1860 "writing persistent grants feature to xenbus");
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001861
1862 err = xenbus_transaction_end(xbt, 0);
1863 if (err) {
1864 if (err == -EAGAIN)
1865 goto again;
1866 xenbus_dev_fatal(dev, err, "completing transaction");
1867 goto destroy_blkring;
1868 }
1869
Bob Liu3df0e502015-11-14 11:12:12 +08001870 for (i = 0; i < info->nr_rings; i++) {
1871 unsigned int j;
Bob Liu28d949b2015-11-14 11:12:14 +08001872 struct blkfront_ring_info *rinfo = &info->rinfo[i];
Bob Liu3df0e502015-11-14 11:12:12 +08001873
1874 for (j = 0; j < BLK_RING_SIZE(info); j++)
1875 rinfo->shadow[j].req.u.rw.id = j + 1;
1876 rinfo->shadow[BLK_RING_SIZE(info)-1].req.u.rw.id = 0x0fffffff;
1877 }
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001878 xenbus_switch_state(dev, XenbusStateInitialised);
1879
1880 return 0;
1881
1882 abort_transaction:
1883 xenbus_transaction_end(xbt, 1);
1884 if (message)
1885 xenbus_dev_fatal(dev, err, "%s", message);
1886 destroy_blkring:
1887 blkif_free(info, 0);
Bob Liu3df0e502015-11-14 11:12:12 +08001888
Konrad Rzeszutek Wilkc31ecf62015-12-18 16:28:53 -05001889 kfree(info);
1890 dev_set_drvdata(&dev->dev, NULL);
1891
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001892 return err;
1893}
1894
Bob Liu3db70a82015-11-25 17:52:55 -05001895static int negotiate_mq(struct blkfront_info *info)
1896{
Juergen Grossf27dc1a2016-10-31 14:58:40 +01001897 unsigned int backend_max_queues;
Bob Liu3db70a82015-11-25 17:52:55 -05001898 unsigned int i;
1899
1900 BUG_ON(info->nr_rings);
1901
1902 /* Check if backend supports multiple queues. */
Juergen Grossf27dc1a2016-10-31 14:58:40 +01001903 backend_max_queues = xenbus_read_unsigned(info->xbdev->otherend,
1904 "multi-queue-max-queues", 1);
Bob Liu3db70a82015-11-25 17:52:55 -05001905 info->nr_rings = min(backend_max_queues, xen_blkif_max_queues);
1906 /* We need at least one ring. */
1907 if (!info->nr_rings)
1908 info->nr_rings = 1;
1909
1910 info->rinfo = kzalloc(sizeof(struct blkfront_ring_info) * info->nr_rings, GFP_KERNEL);
1911 if (!info->rinfo) {
1912 xenbus_dev_fatal(info->xbdev, -ENOMEM, "allocating ring_info structure");
1913 return -ENOMEM;
1914 }
1915
1916 for (i = 0; i < info->nr_rings; i++) {
1917 struct blkfront_ring_info *rinfo;
1918
1919 rinfo = &info->rinfo[i];
1920 INIT_LIST_HEAD(&rinfo->indirect_pages);
1921 INIT_LIST_HEAD(&rinfo->grants);
1922 rinfo->dev_info = info;
1923 INIT_WORK(&rinfo->work, blkif_restart_queue);
1924 spin_lock_init(&rinfo->ring_lock);
1925 }
1926 return 0;
1927}
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001928/**
1929 * Entry point to this code when a new device is created. Allocate the basic
1930 * structures and the ring buffer for communication with the backend, and
1931 * inform the backend of the appropriate details for those. Switch to
1932 * Initialised state.
1933 */
1934static int blkfront_probe(struct xenbus_device *dev,
1935 const struct xenbus_device_id *id)
1936{
Bob Liu86839c52015-06-03 13:40:03 +08001937 int err, vdevice;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001938 struct blkfront_info *info;
1939
1940 /* FIXME: Use dynamic device id if this is not set. */
1941 err = xenbus_scanf(XBT_NIL, dev->nodename,
1942 "virtual-device", "%i", &vdevice);
1943 if (err != 1) {
Chris Lalancette9246b5f2008-09-17 14:30:32 -07001944 /* go looking in the extended area instead */
1945 err = xenbus_scanf(XBT_NIL, dev->nodename, "virtual-device-ext",
1946 "%i", &vdevice);
1947 if (err != 1) {
1948 xenbus_dev_fatal(dev, err, "reading virtual-device");
1949 return err;
1950 }
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001951 }
1952
Stefano Stabellinib98a4092010-07-29 14:53:16 +01001953 if (xen_hvm_domain()) {
1954 char *type;
1955 int len;
1956 /* no unplug has been done: do not hook devices != xen vbds */
Konrad Rzeszutek Wilk51c71a32013-11-26 15:05:40 -05001957 if (xen_has_pv_and_legacy_disk_devices()) {
Stefano Stabellinib98a4092010-07-29 14:53:16 +01001958 int major;
Stefano Stabellinic1c54132010-05-14 12:44:30 +01001959
Stefano Stabellinib98a4092010-07-29 14:53:16 +01001960 if (!VDEV_IS_EXTENDED(vdevice))
1961 major = BLKIF_MAJOR(vdevice);
1962 else
1963 major = XENVBD_MAJOR;
Stefano Stabellinic1c54132010-05-14 12:44:30 +01001964
Stefano Stabellinib98a4092010-07-29 14:53:16 +01001965 if (major != XENVBD_MAJOR) {
1966 printk(KERN_INFO
1967 "%s: HVM does not support vbd %d as xen block device\n",
Rasmus Villemoes02f1f212015-02-12 15:01:31 -08001968 __func__, vdevice);
Stefano Stabellinib98a4092010-07-29 14:53:16 +01001969 return -ENODEV;
1970 }
1971 }
1972 /* do not create a PV cdrom device if we are an HVM guest */
1973 type = xenbus_read(XBT_NIL, dev->nodename, "device-type", &len);
1974 if (IS_ERR(type))
1975 return -ENODEV;
1976 if (strncmp(type, "cdrom", 5) == 0) {
1977 kfree(type);
Stefano Stabellinic1c54132010-05-14 12:44:30 +01001978 return -ENODEV;
1979 }
Stefano Stabellinib98a4092010-07-29 14:53:16 +01001980 kfree(type);
Stefano Stabellinic1c54132010-05-14 12:44:30 +01001981 }
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001982 info = kzalloc(sizeof(*info), GFP_KERNEL);
1983 if (!info) {
1984 xenbus_dev_fatal(dev, -ENOMEM, "allocating info structure");
1985 return -ENOMEM;
1986 }
1987
Bob Liu28d949b2015-11-14 11:12:14 +08001988 info->xbdev = dev;
Bob Liu81f35162015-11-14 11:12:11 +08001989
Daniel Stoddenb70f5fa2010-04-30 22:01:19 +00001990 mutex_init(&info->mutex);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001991 info->vdevice = vdevice;
1992 info->connected = BLKIF_STATE_DISCONNECTED;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001993
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001994 /* Front end dir is a number, which is used as the id. */
1995 info->handle = simple_strtoul(strrchr(dev->nodename, '/')+1, NULL, 0);
Greg Kroah-Hartmana1b4b122009-04-30 14:43:31 -07001996 dev_set_drvdata(&dev->dev, info);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001997
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001998 return 0;
1999}
2000
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07002001static int blkif_recover(struct blkfront_info *info)
2002{
NeilBrown4559fa52017-06-18 14:38:59 +10002003 unsigned int r_index;
Roger Pau Monne402b27f2013-04-18 16:06:54 +02002004 struct request *req, *n;
Roger Pau Monne402b27f2013-04-18 16:06:54 +02002005 int rc;
NeilBrown4559fa52017-06-18 14:38:59 +10002006 struct bio *bio;
2007 unsigned int segs;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07002008
Bob Liu3df0e502015-11-14 11:12:12 +08002009 blkfront_gather_backend_features(info);
Bob Liu172335a2016-07-01 17:43:39 -04002010 /* Reset limits changed by blk_mq_update_nr_hw_queues(). */
2011 blkif_set_queue_limits(info);
Roger Pau Monne402b27f2013-04-18 16:06:54 +02002012 segs = info->max_indirect_segments ? : BLKIF_MAX_SEGMENTS_PER_REQUEST;
Bob Liu6c647b02016-07-01 15:45:57 -04002013 blk_queue_max_segments(info->rq, segs / GRANTS_PER_PSEG);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07002014
Bob Liu3df0e502015-11-14 11:12:12 +08002015 for (r_index = 0; r_index < info->nr_rings; r_index++) {
Bob Liu7b427a52016-06-27 16:33:24 +08002016 struct blkfront_ring_info *rinfo = &info->rinfo[r_index];
Bob Liu3df0e502015-11-14 11:12:12 +08002017
2018 rc = blkfront_setup_indirect(rinfo);
Bob Liu7b427a52016-06-27 16:33:24 +08002019 if (rc)
Bob Liu3df0e502015-11-14 11:12:12 +08002020 return rc;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07002021 }
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07002022 xenbus_switch_state(info->xbdev, XenbusStateConnected);
2023
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07002024 /* Now safe for us to use the shared ring */
2025 info->connected = BLKIF_STATE_CONNECTED;
2026
Bob Liu3df0e502015-11-14 11:12:12 +08002027 for (r_index = 0; r_index < info->nr_rings; r_index++) {
2028 struct blkfront_ring_info *rinfo;
2029
2030 rinfo = &info->rinfo[r_index];
2031 /* Kick any other new requests queued since we resumed */
2032 kick_pending_request_queues(rinfo);
2033 }
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07002034
Bob Liu7b427a52016-06-27 16:33:24 +08002035 list_for_each_entry_safe(req, n, &info->requests, queuelist) {
Roger Pau Monne402b27f2013-04-18 16:06:54 +02002036 /* Requeue pending requests (flush or discard) */
2037 list_del_init(&req->queuelist);
2038 BUG_ON(req->nr_phys_segments > segs);
Bart Van Assche2b053ac2016-10-28 17:21:41 -07002039 blk_mq_requeue_request(req, false);
Roger Pau Monne402b27f2013-04-18 16:06:54 +02002040 }
Bart Van Assche52d7f1b2016-10-28 17:20:32 -07002041 blk_mq_start_stopped_hw_queues(info->rq, true);
Bob Liu907c3eb2015-07-13 17:55:24 +08002042 blk_mq_kick_requeue_list(info->rq);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07002043
Bob Liu7b427a52016-06-27 16:33:24 +08002044 while ((bio = bio_list_pop(&info->bio_list)) != NULL) {
Roger Pau Monne402b27f2013-04-18 16:06:54 +02002045 /* Traverse the list of pending bios and re-queue them */
Mike Christie4e49ea42016-06-05 14:31:41 -05002046 submit_bio(bio);
Roger Pau Monne402b27f2013-04-18 16:06:54 +02002047 }
2048
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07002049 return 0;
2050}
2051
2052/**
2053 * We are reconnecting to the backend, due to a suspend/resume, or a backend
2054 * driver restart. We tear down our blkif structure and recreate it, but
2055 * leave the device-layer structures intact so that this is transparent to the
2056 * rest of the kernel.
2057 */
2058static int blkfront_resume(struct xenbus_device *dev)
2059{
Greg Kroah-Hartmana1b4b122009-04-30 14:43:31 -07002060 struct blkfront_info *info = dev_get_drvdata(&dev->dev);
Bob Liu3db70a82015-11-25 17:52:55 -05002061 int err = 0;
Bob Liu7b427a52016-06-27 16:33:24 +08002062 unsigned int i, j;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07002063
2064 dev_dbg(&dev->dev, "blkfront_resume: %s\n", dev->nodename);
2065
Bob Liu7b427a52016-06-27 16:33:24 +08002066 bio_list_init(&info->bio_list);
2067 INIT_LIST_HEAD(&info->requests);
2068 for (i = 0; i < info->nr_rings; i++) {
2069 struct blkfront_ring_info *rinfo = &info->rinfo[i];
2070 struct bio_list merge_bio;
2071 struct blk_shadow *shadow = rinfo->shadow;
2072
2073 for (j = 0; j < BLK_RING_SIZE(info); j++) {
2074 /* Not in use? */
2075 if (!shadow[j].request)
2076 continue;
2077
2078 /*
2079 * Get the bios in the request so we can re-queue them.
2080 */
Munehisa Kamatab15bd8c2017-08-09 15:31:40 -07002081 if (req_op(shadow[j].request) == REQ_OP_FLUSH ||
2082 req_op(shadow[j].request) == REQ_OP_DISCARD ||
2083 req_op(shadow[j].request) == REQ_OP_SECURE_ERASE ||
Linus Torvalds3fc9d692016-07-26 15:37:51 -07002084 shadow[j].request->cmd_flags & REQ_FUA) {
Bob Liu7b427a52016-06-27 16:33:24 +08002085 /*
2086 * Flush operations don't contain bios, so
2087 * we need to requeue the whole request
Linus Torvalds3fc9d692016-07-26 15:37:51 -07002088 *
2089 * XXX: but this doesn't make any sense for a
2090 * write with the FUA flag set..
Bob Liu7b427a52016-06-27 16:33:24 +08002091 */
2092 list_add(&shadow[j].request->queuelist, &info->requests);
2093 continue;
2094 }
2095 merge_bio.head = shadow[j].request->bio;
2096 merge_bio.tail = shadow[j].request->biotail;
2097 bio_list_merge(&info->bio_list, &merge_bio);
2098 shadow[j].request->bio = NULL;
Christoph Hellwig2a842ac2017-06-03 09:38:04 +02002099 blk_mq_end_request(shadow[j].request, BLK_STS_OK);
Bob Liu7b427a52016-06-27 16:33:24 +08002100 }
2101 }
2102
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07002103 blkif_free(info, info->connected == BLKIF_STATE_CONNECTED);
2104
Ian Campbell203fd612009-12-04 15:33:54 +00002105 err = talk_to_blkback(dev, info);
Bob Liu2a6f71a2016-05-31 16:59:17 +08002106 if (!err)
2107 blk_mq_update_nr_hw_queues(&info->tag_set, info->nr_rings);
Roger Pau Monne402b27f2013-04-18 16:06:54 +02002108
2109 /*
2110 * We have to wait for the backend to switch to
2111 * connected state, since we want to read which
2112 * features it supports.
2113 */
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07002114
2115 return err;
2116}
2117
Konrad Rzeszutek Wilk6f03a7f2015-11-16 15:14:41 -05002118static void blkfront_closing(struct blkfront_info *info)
Daniel Stoddenb70f5fa2010-04-30 22:01:19 +00002119{
2120 struct xenbus_device *xbdev = info->xbdev;
2121 struct block_device *bdev = NULL;
2122
2123 mutex_lock(&info->mutex);
2124
2125 if (xbdev->state == XenbusStateClosing) {
2126 mutex_unlock(&info->mutex);
2127 return;
2128 }
2129
2130 if (info->gd)
2131 bdev = bdget_disk(info->gd, 0);
2132
2133 mutex_unlock(&info->mutex);
2134
2135 if (!bdev) {
2136 xenbus_frontend_closed(xbdev);
2137 return;
2138 }
2139
2140 mutex_lock(&bdev->bd_mutex);
2141
Daniel Stodden7b32d102010-04-30 22:01:23 +00002142 if (bdev->bd_openers) {
Daniel Stoddenb70f5fa2010-04-30 22:01:19 +00002143 xenbus_dev_error(xbdev, -EBUSY,
2144 "Device in use; refusing to close");
2145 xenbus_switch_state(xbdev, XenbusStateClosing);
2146 } else {
2147 xlvbd_release_gendisk(info);
2148 xenbus_frontend_closed(xbdev);
2149 }
2150
2151 mutex_unlock(&bdev->bd_mutex);
2152 bdput(bdev);
2153}
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07002154
Li Dongyanged30bf32011-09-01 18:39:09 +08002155static void blkfront_setup_discard(struct blkfront_info *info)
2156{
2157 int err;
Li Dongyanged30bf32011-09-01 18:39:09 +08002158 unsigned int discard_granularity;
2159 unsigned int discard_alignment;
2160
Olaf Hering1c8cad62014-05-21 16:32:40 +02002161 info->feature_discard = 1;
2162 err = xenbus_gather(XBT_NIL, info->xbdev->otherend,
2163 "discard-granularity", "%u", &discard_granularity,
2164 "discard-alignment", "%u", &discard_alignment,
2165 NULL);
2166 if (!err) {
2167 info->discard_granularity = discard_granularity;
2168 info->discard_alignment = discard_alignment;
2169 }
Juergen Grossf27dc1a2016-10-31 14:58:40 +01002170 info->feature_secdiscard =
2171 !!xenbus_read_unsigned(info->xbdev->otherend, "discard-secure",
2172 0);
Li Dongyanged30bf32011-09-01 18:39:09 +08002173}
2174
Bob Liu81f35162015-11-14 11:12:11 +08002175static int blkfront_setup_indirect(struct blkfront_ring_info *rinfo)
Roger Pau Monne402b27f2013-04-18 16:06:54 +02002176{
Julien Grallc004a6f2015-07-22 16:44:54 +01002177 unsigned int psegs, grants;
Roger Pau Monne402b27f2013-04-18 16:06:54 +02002178 int err, i;
Bob Liu81f35162015-11-14 11:12:11 +08002179 struct blkfront_info *info = rinfo->dev_info;
Roger Pau Monne402b27f2013-04-18 16:06:54 +02002180
Julien Grall6cc568332015-08-13 13:13:35 +01002181 if (info->max_indirect_segments == 0) {
2182 if (!HAS_EXTRA_REQ)
2183 grants = BLKIF_MAX_SEGMENTS_PER_REQUEST;
2184 else {
2185 /*
2186 * When an extra req is required, the maximum
2187 * grants supported is related to the size of the
2188 * Linux block segment.
2189 */
2190 grants = GRANTS_PER_PSEG;
2191 }
2192 }
Bob Liud50babb2015-07-22 14:40:08 +08002193 else
Julien Grallc004a6f2015-07-22 16:44:54 +01002194 grants = info->max_indirect_segments;
Jan Beulich3b4f1882017-01-23 08:11:37 -07002195 psegs = DIV_ROUND_UP(grants, GRANTS_PER_PSEG);
Roger Pau Monne402b27f2013-04-18 16:06:54 +02002196
Bob Liu81f35162015-11-14 11:12:11 +08002197 err = fill_grant_buffer(rinfo,
Julien Grallc004a6f2015-07-22 16:44:54 +01002198 (grants + INDIRECT_GREFS(grants)) * BLK_RING_SIZE(info));
Roger Pau Monne402b27f2013-04-18 16:06:54 +02002199 if (err)
2200 goto out_of_memory;
2201
Roger Pau Monnebfe11d62013-10-29 18:31:14 +01002202 if (!info->feature_persistent && info->max_indirect_segments) {
2203 /*
2204 * We are using indirect descriptors but not persistent
2205 * grants, we need to allocate a set of pages that can be
2206 * used for mapping indirect grefs
2207 */
Julien Grallc004a6f2015-07-22 16:44:54 +01002208 int num = INDIRECT_GREFS(grants) * BLK_RING_SIZE(info);
Roger Pau Monnebfe11d62013-10-29 18:31:14 +01002209
Bob Liu81f35162015-11-14 11:12:11 +08002210 BUG_ON(!list_empty(&rinfo->indirect_pages));
Roger Pau Monnebfe11d62013-10-29 18:31:14 +01002211 for (i = 0; i < num; i++) {
2212 struct page *indirect_page = alloc_page(GFP_NOIO);
2213 if (!indirect_page)
2214 goto out_of_memory;
Bob Liu81f35162015-11-14 11:12:11 +08002215 list_add(&indirect_page->lru, &rinfo->indirect_pages);
Roger Pau Monnebfe11d62013-10-29 18:31:14 +01002216 }
2217 }
2218
Bob Liu86839c52015-06-03 13:40:03 +08002219 for (i = 0; i < BLK_RING_SIZE(info); i++) {
Bob Liu81f35162015-11-14 11:12:11 +08002220 rinfo->shadow[i].grants_used = kzalloc(
2221 sizeof(rinfo->shadow[i].grants_used[0]) * grants,
Roger Pau Monne402b27f2013-04-18 16:06:54 +02002222 GFP_NOIO);
Bob Liu81f35162015-11-14 11:12:11 +08002223 rinfo->shadow[i].sg = kzalloc(sizeof(rinfo->shadow[i].sg[0]) * psegs, GFP_NOIO);
Roger Pau Monne402b27f2013-04-18 16:06:54 +02002224 if (info->max_indirect_segments)
Bob Liu81f35162015-11-14 11:12:11 +08002225 rinfo->shadow[i].indirect_grants = kzalloc(
2226 sizeof(rinfo->shadow[i].indirect_grants[0]) *
Julien Grallc004a6f2015-07-22 16:44:54 +01002227 INDIRECT_GREFS(grants),
Roger Pau Monne402b27f2013-04-18 16:06:54 +02002228 GFP_NOIO);
Bob Liu81f35162015-11-14 11:12:11 +08002229 if ((rinfo->shadow[i].grants_used == NULL) ||
2230 (rinfo->shadow[i].sg == NULL) ||
Roger Pau Monne402b27f2013-04-18 16:06:54 +02002231 (info->max_indirect_segments &&
Bob Liu81f35162015-11-14 11:12:11 +08002232 (rinfo->shadow[i].indirect_grants == NULL)))
Roger Pau Monne402b27f2013-04-18 16:06:54 +02002233 goto out_of_memory;
Bob Liu81f35162015-11-14 11:12:11 +08002234 sg_init_table(rinfo->shadow[i].sg, psegs);
Roger Pau Monne402b27f2013-04-18 16:06:54 +02002235 }
2236
2237
2238 return 0;
2239
2240out_of_memory:
Bob Liu86839c52015-06-03 13:40:03 +08002241 for (i = 0; i < BLK_RING_SIZE(info); i++) {
Bob Liu81f35162015-11-14 11:12:11 +08002242 kfree(rinfo->shadow[i].grants_used);
2243 rinfo->shadow[i].grants_used = NULL;
2244 kfree(rinfo->shadow[i].sg);
2245 rinfo->shadow[i].sg = NULL;
2246 kfree(rinfo->shadow[i].indirect_grants);
2247 rinfo->shadow[i].indirect_grants = NULL;
Roger Pau Monne402b27f2013-04-18 16:06:54 +02002248 }
Bob Liu81f35162015-11-14 11:12:11 +08002249 if (!list_empty(&rinfo->indirect_pages)) {
Roger Pau Monnebfe11d62013-10-29 18:31:14 +01002250 struct page *indirect_page, *n;
Bob Liu81f35162015-11-14 11:12:11 +08002251 list_for_each_entry_safe(indirect_page, n, &rinfo->indirect_pages, lru) {
Roger Pau Monnebfe11d62013-10-29 18:31:14 +01002252 list_del(&indirect_page->lru);
2253 __free_page(indirect_page);
2254 }
2255 }
Roger Pau Monne402b27f2013-04-18 16:06:54 +02002256 return -ENOMEM;
2257}
2258
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07002259/*
Bob Liud50babb2015-07-22 14:40:08 +08002260 * Gather all backend feature-*
2261 */
Bob Liu3df0e502015-11-14 11:12:12 +08002262static void blkfront_gather_backend_features(struct blkfront_info *info)
Bob Liud50babb2015-07-22 14:40:08 +08002263{
Bob Liud50babb2015-07-22 14:40:08 +08002264 unsigned int indirect_segments;
2265
2266 info->feature_flush = 0;
Mike Christiea4180902016-06-05 14:32:24 -05002267 info->feature_fua = 0;
Bob Liud50babb2015-07-22 14:40:08 +08002268
Bob Liud50babb2015-07-22 14:40:08 +08002269 /*
2270 * If there's no "feature-barrier" defined, then it means
2271 * we're dealing with a very old backend which writes
2272 * synchronously; nothing to do.
2273 *
2274 * If there are barriers, then we use flush.
2275 */
Juergen Grossf27dc1a2016-10-31 14:58:40 +01002276 if (xenbus_read_unsigned(info->xbdev->otherend, "feature-barrier", 0)) {
Mike Christiea4180902016-06-05 14:32:24 -05002277 info->feature_flush = 1;
2278 info->feature_fua = 1;
2279 }
2280
Bob Liud50babb2015-07-22 14:40:08 +08002281 /*
2282 * And if there is "feature-flush-cache" use that above
2283 * barriers.
2284 */
Juergen Grossf27dc1a2016-10-31 14:58:40 +01002285 if (xenbus_read_unsigned(info->xbdev->otherend, "feature-flush-cache",
2286 0)) {
Mike Christiea4180902016-06-05 14:32:24 -05002287 info->feature_flush = 1;
2288 info->feature_fua = 0;
2289 }
Bob Liud50babb2015-07-22 14:40:08 +08002290
Juergen Grossf27dc1a2016-10-31 14:58:40 +01002291 if (xenbus_read_unsigned(info->xbdev->otherend, "feature-discard", 0))
Bob Liud50babb2015-07-22 14:40:08 +08002292 blkfront_setup_discard(info);
2293
Juergen Grossf27dc1a2016-10-31 14:58:40 +01002294 info->feature_persistent =
Jan Beulichb32728f2017-01-23 08:12:19 -07002295 !!xenbus_read_unsigned(info->xbdev->otherend,
2296 "feature-persistent", 0);
Bob Liud50babb2015-07-22 14:40:08 +08002297
Juergen Grossf27dc1a2016-10-31 14:58:40 +01002298 indirect_segments = xenbus_read_unsigned(info->xbdev->otherend,
2299 "feature-max-indirect-segments", 0);
Jan Beulich3b4f1882017-01-23 08:11:37 -07002300 if (indirect_segments > xen_blkif_max_segments)
2301 indirect_segments = xen_blkif_max_segments;
2302 if (indirect_segments <= BLKIF_MAX_SEGMENTS_PER_REQUEST)
2303 indirect_segments = 0;
2304 info->max_indirect_segments = indirect_segments;
Bob Liud50babb2015-07-22 14:40:08 +08002305}
2306
2307/*
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07002308 * Invoked when the backend is finally 'ready' (and has told produced
2309 * the details about the physical device - #sectors, size, etc).
2310 */
2311static void blkfront_connect(struct blkfront_info *info)
2312{
2313 unsigned long long sectors;
2314 unsigned long sector_size;
Stefan Bader7c4d7d72013-05-13 16:28:15 +02002315 unsigned int physical_sector_size;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07002316 unsigned int binfo;
Marc Olson89515d02017-04-11 12:24:09 -07002317 char *envp[] = { "RESIZE=1", NULL };
Bob Liu3df0e502015-11-14 11:12:12 +08002318 int err, i;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07002319
K. Y. Srinivasan1fa73be2010-03-11 13:42:26 -08002320 switch (info->connected) {
2321 case BLKIF_STATE_CONNECTED:
2322 /*
2323 * Potentially, the back-end may be signalling
2324 * a capacity change; update the capacity.
2325 */
2326 err = xenbus_scanf(XBT_NIL, info->xbdev->otherend,
2327 "sectors", "%Lu", &sectors);
2328 if (XENBUS_EXIST_ERR(err))
2329 return;
2330 printk(KERN_INFO "Setting capacity to %Lu\n",
2331 sectors);
2332 set_capacity(info->gd, sectors);
K. Y. Srinivasan2def1412010-03-18 15:00:54 -07002333 revalidate_disk(info->gd);
Marc Olson89515d02017-04-11 12:24:09 -07002334 kobject_uevent_env(&disk_to_dev(info->gd)->kobj,
2335 KOBJ_CHANGE, envp);
K. Y. Srinivasan1fa73be2010-03-11 13:42:26 -08002336
Roger Pau Monne402b27f2013-04-18 16:06:54 +02002337 return;
K. Y. Srinivasan1fa73be2010-03-11 13:42:26 -08002338 case BLKIF_STATE_SUSPENDED:
Roger Pau Monne402b27f2013-04-18 16:06:54 +02002339 /*
2340 * If we are recovering from suspension, we need to wait
2341 * for the backend to announce it's features before
2342 * reconnecting, at least we need to know if the backend
2343 * supports indirect descriptors, and how many.
2344 */
2345 blkif_recover(info);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07002346 return;
2347
Jeremy Fitzhardingeb4dddb42010-03-11 15:10:40 -08002348 default:
2349 break;
K. Y. Srinivasan1fa73be2010-03-11 13:42:26 -08002350 }
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07002351
2352 dev_dbg(&info->xbdev->dev, "%s:%s.\n",
2353 __func__, info->xbdev->otherend);
2354
2355 err = xenbus_gather(XBT_NIL, info->xbdev->otherend,
2356 "sectors", "%llu", &sectors,
2357 "info", "%u", &binfo,
2358 "sector-size", "%lu", &sector_size,
2359 NULL);
2360 if (err) {
2361 xenbus_dev_fatal(info->xbdev, err,
2362 "reading backend fields at %s",
2363 info->xbdev->otherend);
2364 return;
2365 }
2366
Stefan Bader7c4d7d72013-05-13 16:28:15 +02002367 /*
2368 * physcial-sector-size is a newer field, so old backends may not
2369 * provide this. Assume physical sector size to be the same as
2370 * sector_size in that case.
2371 */
Juergen Grossf27dc1a2016-10-31 14:58:40 +01002372 physical_sector_size = xenbus_read_unsigned(info->xbdev->otherend,
2373 "physical-sector-size",
2374 sector_size);
Bob Liu3df0e502015-11-14 11:12:12 +08002375 blkfront_gather_backend_features(info);
2376 for (i = 0; i < info->nr_rings; i++) {
2377 err = blkfront_setup_indirect(&info->rinfo[i]);
2378 if (err) {
2379 xenbus_dev_fatal(info->xbdev, err, "setup_indirect at %s",
2380 info->xbdev->otherend);
2381 blkif_free(info, 0);
2382 break;
2383 }
Roger Pau Monne402b27f2013-04-18 16:06:54 +02002384 }
2385
Stefan Bader7c4d7d72013-05-13 16:28:15 +02002386 err = xlvbd_alloc_gendisk(sectors, info, binfo, sector_size,
2387 physical_sector_size);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07002388 if (err) {
2389 xenbus_dev_fatal(info->xbdev, err, "xlvbd_add at %s",
2390 info->xbdev->otherend);
Bob Liu4e876c22016-07-27 17:42:04 +08002391 goto fail;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07002392 }
2393
2394 xenbus_switch_state(info->xbdev, XenbusStateConnected);
2395
2396 /* Kick pending requests. */
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07002397 info->connected = BLKIF_STATE_CONNECTED;
Bob Liu3df0e502015-11-14 11:12:12 +08002398 for (i = 0; i < info->nr_rings; i++)
2399 kick_pending_request_queues(&info->rinfo[i]);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07002400
Dan Williams0d52c7562016-06-15 19:44:20 -07002401 device_add_disk(&info->xbdev->dev, info->gd);
Christian Limpach1d78d702008-04-02 10:54:04 -07002402
2403 info->is_ready = 1;
Bob Liu4e876c22016-07-27 17:42:04 +08002404 return;
2405
2406fail:
2407 blkif_free(info, 0);
2408 return;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07002409}
2410
2411/**
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07002412 * Callback received when the backend's state changes.
2413 */
Ian Campbell203fd612009-12-04 15:33:54 +00002414static void blkback_changed(struct xenbus_device *dev,
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07002415 enum xenbus_state backend_state)
2416{
Greg Kroah-Hartmana1b4b122009-04-30 14:43:31 -07002417 struct blkfront_info *info = dev_get_drvdata(&dev->dev);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07002418
Ian Campbell203fd612009-12-04 15:33:54 +00002419 dev_dbg(&dev->dev, "blkfront:blkback_changed to state %d.\n", backend_state);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07002420
2421 switch (backend_state) {
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07002422 case XenbusStateInitWait:
Bob Liua9b54bb2015-06-19 00:23:00 -04002423 if (dev->state != XenbusStateInitialising)
2424 break;
Konrad Rzeszutek Wilkc31ecf62015-12-18 16:28:53 -05002425 if (talk_to_blkback(dev, info))
Bob Liu8ab01442015-06-03 13:40:02 +08002426 break;
Bob Liu8ab01442015-06-03 13:40:02 +08002427 case XenbusStateInitialising:
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07002428 case XenbusStateInitialised:
Noboru Iwamatsub78c9512009-10-13 17:22:29 -04002429 case XenbusStateReconfiguring:
2430 case XenbusStateReconfigured:
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07002431 case XenbusStateUnknown:
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07002432 break;
2433
2434 case XenbusStateConnected:
Bob Liuefd15352016-06-07 10:43:15 -04002435 /*
2436 * talk_to_blkback sets state to XenbusStateInitialised
2437 * and blkfront_connect sets it to XenbusStateConnected
2438 * (if connection went OK).
2439 *
2440 * If the backend (or toolstack) decides to poke at backend
2441 * state (and re-trigger the watch by setting the state repeatedly
2442 * to XenbusStateConnected (4)) we need to deal with this.
2443 * This is allowed as this is used to communicate to the guest
2444 * that the size of disk has changed!
2445 */
2446 if ((dev->state != XenbusStateInitialised) &&
2447 (dev->state != XenbusStateConnected)) {
Konrad Rzeszutek Wilkc31ecf62015-12-18 16:28:53 -05002448 if (talk_to_blkback(dev, info))
2449 break;
2450 }
Bob Liuefd15352016-06-07 10:43:15 -04002451
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07002452 blkfront_connect(info);
2453 break;
2454
David Vrabel36613712014-02-04 18:53:56 +00002455 case XenbusStateClosed:
2456 if (dev->state == XenbusStateClosed)
2457 break;
Bart Van Asscheccc22252017-08-17 16:23:11 -07002458 /* fall through */
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07002459 case XenbusStateClosing:
Cathy Averya54c8f02015-10-02 09:35:01 -04002460 if (info)
2461 blkfront_closing(info);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07002462 break;
2463 }
2464}
2465
Daniel Stoddenfa1bd352010-04-30 22:01:22 +00002466static int blkfront_remove(struct xenbus_device *xbdev)
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07002467{
Daniel Stoddenfa1bd352010-04-30 22:01:22 +00002468 struct blkfront_info *info = dev_get_drvdata(&xbdev->dev);
2469 struct block_device *bdev = NULL;
2470 struct gendisk *disk;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07002471
Daniel Stoddenfa1bd352010-04-30 22:01:22 +00002472 dev_dbg(&xbdev->dev, "%s removed", xbdev->nodename);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07002473
2474 blkif_free(info, 0);
2475
Daniel Stoddenfa1bd352010-04-30 22:01:22 +00002476 mutex_lock(&info->mutex);
2477
2478 disk = info->gd;
2479 if (disk)
2480 bdev = bdget_disk(disk, 0);
2481
2482 info->xbdev = NULL;
2483 mutex_unlock(&info->mutex);
2484
2485 if (!bdev) {
Jan Beulich0e345822010-08-07 18:28:55 +02002486 kfree(info);
Daniel Stoddenfa1bd352010-04-30 22:01:22 +00002487 return 0;
2488 }
2489
2490 /*
2491 * The xbdev was removed before we reached the Closed
2492 * state. See if it's safe to remove the disk. If the bdev
2493 * isn't closed yet, we let release take care of it.
2494 */
2495
2496 mutex_lock(&bdev->bd_mutex);
2497 info = disk->private_data;
2498
Daniel Stoddend54142c2010-08-07 18:51:21 +02002499 dev_warn(disk_to_dev(disk),
2500 "%s was hot-unplugged, %d stale handles\n",
2501 xbdev->nodename, bdev->bd_openers);
2502
Daniel Stodden7b32d102010-04-30 22:01:23 +00002503 if (info && !bdev->bd_openers) {
Daniel Stoddenfa1bd352010-04-30 22:01:22 +00002504 xlvbd_release_gendisk(info);
2505 disk->private_data = NULL;
2506 kfree(info);
2507 }
2508
2509 mutex_unlock(&bdev->bd_mutex);
2510 bdput(bdev);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07002511
2512 return 0;
2513}
2514
Christian Limpach1d78d702008-04-02 10:54:04 -07002515static int blkfront_is_ready(struct xenbus_device *dev)
2516{
Greg Kroah-Hartmana1b4b122009-04-30 14:43:31 -07002517 struct blkfront_info *info = dev_get_drvdata(&dev->dev);
Christian Limpach1d78d702008-04-02 10:54:04 -07002518
Jan Beulich5d7ed202010-08-07 18:31:12 +02002519 return info->is_ready && info->xbdev;
Christian Limpach1d78d702008-04-02 10:54:04 -07002520}
2521
Al Viroa63c8482008-03-02 10:23:47 -05002522static int blkif_open(struct block_device *bdev, fmode_t mode)
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07002523{
Daniel Stodden13961742010-08-07 18:36:53 +02002524 struct gendisk *disk = bdev->bd_disk;
2525 struct blkfront_info *info;
2526 int err = 0;
Arnd Bergmann6e9624b2010-08-07 18:25:34 +02002527
Arnd Bergmann2a48fc02010-06-02 14:28:52 +02002528 mutex_lock(&blkfront_mutex);
Arnd Bergmann6e9624b2010-08-07 18:25:34 +02002529
Daniel Stodden13961742010-08-07 18:36:53 +02002530 info = disk->private_data;
2531 if (!info) {
2532 /* xbdev gone */
2533 err = -ERESTARTSYS;
2534 goto out;
2535 }
2536
2537 mutex_lock(&info->mutex);
2538
2539 if (!info->gd)
2540 /* xbdev is closed */
2541 err = -ERESTARTSYS;
2542
2543 mutex_unlock(&info->mutex);
2544
Daniel Stodden13961742010-08-07 18:36:53 +02002545out:
Arnd Bergmann2a48fc02010-06-02 14:28:52 +02002546 mutex_unlock(&blkfront_mutex);
Daniel Stodden13961742010-08-07 18:36:53 +02002547 return err;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07002548}
2549
Al Virodb2a1442013-05-05 21:52:57 -04002550static void blkif_release(struct gendisk *disk, fmode_t mode)
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07002551{
Al Viroa63c8482008-03-02 10:23:47 -05002552 struct blkfront_info *info = disk->private_data;
Daniel Stodden7fd152f2010-08-07 18:45:12 +02002553 struct block_device *bdev;
2554 struct xenbus_device *xbdev;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07002555
Arnd Bergmann2a48fc02010-06-02 14:28:52 +02002556 mutex_lock(&blkfront_mutex);
Daniel Stodden7fd152f2010-08-07 18:45:12 +02002557
2558 bdev = bdget_disk(disk, 0);
Daniel Stodden7fd152f2010-08-07 18:45:12 +02002559
Felipe Pena2f089cb2013-11-09 13:36:09 -02002560 if (!bdev) {
2561 WARN(1, "Block device %s yanked out from us!\n", disk->disk_name);
2562 goto out_mutex;
2563 }
Daniel Stoddenacfca3c2010-08-07 18:47:26 +02002564 if (bdev->bd_openers)
2565 goto out;
2566
Daniel Stodden7fd152f2010-08-07 18:45:12 +02002567 /*
2568 * Check if we have been instructed to close. We will have
2569 * deferred this request, because the bdev was still open.
2570 */
2571
2572 mutex_lock(&info->mutex);
2573 xbdev = info->xbdev;
2574
2575 if (xbdev && xbdev->state == XenbusStateClosing) {
2576 /* pending switch to state closed */
Daniel Stoddend54142c2010-08-07 18:51:21 +02002577 dev_info(disk_to_dev(bdev->bd_disk), "releasing disk\n");
Daniel Stodden7fd152f2010-08-07 18:45:12 +02002578 xlvbd_release_gendisk(info);
2579 xenbus_frontend_closed(info->xbdev);
2580 }
2581
2582 mutex_unlock(&info->mutex);
2583
2584 if (!xbdev) {
2585 /* sudden device removal */
Daniel Stoddend54142c2010-08-07 18:51:21 +02002586 dev_info(disk_to_dev(bdev->bd_disk), "releasing disk\n");
Daniel Stodden7fd152f2010-08-07 18:45:12 +02002587 xlvbd_release_gendisk(info);
2588 disk->private_data = NULL;
2589 kfree(info);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07002590 }
Daniel Stodden7fd152f2010-08-07 18:45:12 +02002591
Jens Axboea4cc14e2010-08-08 21:50:05 -04002592out:
Andrew Jonesdad5cf62012-02-16 13:16:25 +01002593 bdput(bdev);
Felipe Pena2f089cb2013-11-09 13:36:09 -02002594out_mutex:
Arnd Bergmann2a48fc02010-06-02 14:28:52 +02002595 mutex_unlock(&blkfront_mutex);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07002596}
2597
Alexey Dobriyan83d5cde2009-09-21 17:01:13 -07002598static const struct block_device_operations xlvbd_block_fops =
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07002599{
2600 .owner = THIS_MODULE,
Al Viroa63c8482008-03-02 10:23:47 -05002601 .open = blkif_open,
2602 .release = blkif_release,
Ian Campbell597592d2008-02-21 13:03:45 -08002603 .getgeo = blkif_getgeo,
Arnd Bergmann8a6cfeb2010-07-08 10:18:46 +02002604 .ioctl = blkif_ioctl,
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07002605};
2606
2607
Márton Némethec9c42e2010-01-10 13:39:52 +01002608static const struct xenbus_device_id blkfront_ids[] = {
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07002609 { "vbd" },
2610 { "" }
2611};
2612
David Vrabel95afae42014-09-08 17:30:41 +01002613static struct xenbus_driver blkfront_driver = {
2614 .ids = blkfront_ids,
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07002615 .probe = blkfront_probe,
2616 .remove = blkfront_remove,
2617 .resume = blkfront_resume,
Ian Campbell203fd612009-12-04 15:33:54 +00002618 .otherend_changed = blkback_changed,
Christian Limpach1d78d702008-04-02 10:54:04 -07002619 .is_ready = blkfront_is_ready,
David Vrabel95afae42014-09-08 17:30:41 +01002620};
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07002621
2622static int __init xlblk_init(void)
2623{
Laszlo Ersek469738e2011-10-07 21:34:38 +02002624 int ret;
Bob Liu28d949b2015-11-14 11:12:14 +08002625 int nr_cpus = num_online_cpus();
Laszlo Ersek469738e2011-10-07 21:34:38 +02002626
Jeremy Fitzhardinge6e833582008-08-19 13:16:17 -07002627 if (!xen_domain())
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07002628 return -ENODEV;
2629
Jan Beulich3b4f1882017-01-23 08:11:37 -07002630 if (xen_blkif_max_segments < BLKIF_MAX_SEGMENTS_PER_REQUEST)
2631 xen_blkif_max_segments = BLKIF_MAX_SEGMENTS_PER_REQUEST;
2632
Julien Grall9cce2912015-10-13 17:50:11 +01002633 if (xen_blkif_max_ring_order > XENBUS_MAX_RING_GRANT_ORDER) {
Bob Liu86839c52015-06-03 13:40:03 +08002634 pr_info("Invalid max_ring_order (%d), will use default max: %d.\n",
Julien Grall9cce2912015-10-13 17:50:11 +01002635 xen_blkif_max_ring_order, XENBUS_MAX_RING_GRANT_ORDER);
Peng Fan45fc8262015-11-25 18:26:01 +08002636 xen_blkif_max_ring_order = XENBUS_MAX_RING_GRANT_ORDER;
Bob Liu86839c52015-06-03 13:40:03 +08002637 }
2638
Bob Liu28d949b2015-11-14 11:12:14 +08002639 if (xen_blkif_max_queues > nr_cpus) {
2640 pr_info("Invalid max_queues (%d), will use default max: %d.\n",
2641 xen_blkif_max_queues, nr_cpus);
2642 xen_blkif_max_queues = nr_cpus;
2643 }
2644
Konrad Rzeszutek Wilk51c71a32013-11-26 15:05:40 -05002645 if (!xen_has_pv_disk_devices())
Igor Mammedovb9136d22012-03-21 15:08:38 +01002646 return -ENODEV;
2647
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07002648 if (register_blkdev(XENVBD_MAJOR, DEV_NAME)) {
2649 printk(KERN_WARNING "xen_blk: can't get major %d with name %s\n",
2650 XENVBD_MAJOR, DEV_NAME);
2651 return -ENODEV;
2652 }
2653
Jan Beulich73db1442011-12-22 09:08:13 +00002654 ret = xenbus_register_frontend(&blkfront_driver);
Laszlo Ersek469738e2011-10-07 21:34:38 +02002655 if (ret) {
2656 unregister_blkdev(XENVBD_MAJOR, DEV_NAME);
2657 return ret;
2658 }
2659
2660 return 0;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07002661}
2662module_init(xlblk_init);
2663
2664
Jan Beulich5a60d0c2008-06-17 10:47:08 +02002665static void __exit xlblk_exit(void)
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07002666{
Jan Beulich86050672012-04-05 16:04:52 +01002667 xenbus_unregister_driver(&blkfront_driver);
2668 unregister_blkdev(XENVBD_MAJOR, DEV_NAME);
2669 kfree(minors);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07002670}
2671module_exit(xlblk_exit);
2672
2673MODULE_DESCRIPTION("Xen virtual block device frontend");
2674MODULE_LICENSE("GPL");
2675MODULE_ALIAS_BLOCKDEV_MAJOR(XENVBD_MAJOR);
Mark McLoughlind2f0c522008-04-02 10:54:05 -07002676MODULE_ALIAS("xen:vbd");
Mark McLoughlin4f93f09b2008-04-02 10:54:06 -07002677MODULE_ALIAS("xenblk");