blob: b5cedccb5d7db10ec9d75632e221acd73308933b [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;
Joe Perches5657a812018-05-24 13:38:59 -0600132module_param_named(max_indirect_segments, xen_blkif_max_segments, uint, 0444);
Jan Beulich14e710f2016-02-10 04:21:15 -0700133MODULE_PARM_DESC(max_indirect_segments,
134 "Maximum amount of segments in indirect requests (default is 32)");
Roger Pau Monne402b27f2013-04-18 16:06:54 +0200135
Bob Liu28d949b2015-11-14 11:12:14 +0800136static unsigned int xen_blkif_max_queues = 4;
Joe Perches5657a812018-05-24 13:38:59 -0600137module_param_named(max_queues, xen_blkif_max_queues, uint, 0444);
Bob Liu28d949b2015-11-14 11:12:14 +0800138MODULE_PARM_DESC(max_queues, "Maximum number of hardware queues/rings used per virtual disk");
139
Bob Liu86839c52015-06-03 13:40:03 +0800140/*
141 * Maximum order of pages to be used for the shared ring between front and
142 * backend, 4KB page granularity is used.
143 */
144static unsigned int xen_blkif_max_ring_order;
Joe Perches5657a812018-05-24 13:38:59 -0600145module_param_named(max_ring_page_order, xen_blkif_max_ring_order, int, 0444);
Bob Liu86839c52015-06-03 13:40:03 +0800146MODULE_PARM_DESC(max_ring_page_order, "Maximum order of pages to be used for the shared ring");
147
Julien Grallc004a6f2015-07-22 16:44:54 +0100148#define BLK_RING_SIZE(info) \
149 __CONST_RING_SIZE(blkif, XEN_PAGE_SIZE * (info)->nr_ring_pages)
150
151#define BLK_MAX_RING_SIZE \
Julien Grall9cce2912015-10-13 17:50:11 +0100152 __CONST_RING_SIZE(blkif, XEN_PAGE_SIZE * XENBUS_MAX_RING_GRANTS)
Julien Grallc004a6f2015-07-22 16:44:54 +0100153
Bob Liu86839c52015-06-03 13:40:03 +0800154/*
Konrad Rzeszutek Wilk6f03a7f2015-11-16 15:14:41 -0500155 * ring-ref%u i=(-1UL) would take 11 characters + 'ring-ref' is 8, so 19
156 * characters are enough. Define to 20 to keep consistent with backend.
Bob Liu86839c52015-06-03 13:40:03 +0800157 */
158#define RINGREF_NAME_LEN (20)
Bob Liu28d949b2015-11-14 11:12:14 +0800159/*
160 * queue-%u would take 7 + 10(UINT_MAX) = 17 characters.
161 */
162#define QUEUE_NAME_LEN (17)
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700163
164/*
Bob Liu81f35162015-11-14 11:12:11 +0800165 * Per-ring info.
166 * Every blkfront device can associate with one or more blkfront_ring_info,
167 * depending on how many hardware queues/rings to be used.
168 */
169struct blkfront_ring_info {
Bob Liu11659562015-11-14 11:12:13 +0800170 /* Lock to protect data in every ring buffer. */
171 spinlock_t ring_lock;
Bob Liu81f35162015-11-14 11:12:11 +0800172 struct blkif_front_ring ring;
173 unsigned int ring_ref[XENBUS_MAX_RING_GRANTS];
174 unsigned int evtchn, irq;
175 struct work_struct work;
176 struct gnttab_free_callback callback;
177 struct blk_shadow shadow[BLK_MAX_RING_SIZE];
178 struct list_head indirect_pages;
Bob Liu73716df2015-11-16 16:51:39 -0500179 struct list_head grants;
180 unsigned int persistent_gnts_c;
Bob Liu81f35162015-11-14 11:12:11 +0800181 unsigned long shadow_free;
182 struct blkfront_info *dev_info;
183};
184
185/*
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700186 * We have one of these per vbd, whether ide, scsi or 'other'. They
187 * hang in private_data off the gendisk structure. We may end up
188 * putting all kinds of interesting stuff here :-)
189 */
190struct blkfront_info
191{
Daniel Stoddenb70f5fa2010-04-30 22:01:19 +0000192 struct mutex mutex;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700193 struct xenbus_device *xbdev;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700194 struct gendisk *gd;
Bob Liu172335a2016-07-01 17:43:39 -0400195 u16 sector_size;
196 unsigned int physical_sector_size;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700197 int vdevice;
198 blkif_vdev_t handle;
199 enum blkif_state connected;
Bob Liu3df0e502015-11-14 11:12:12 +0800200 /* Number of pages per ring buffer. */
Bob Liu86839c52015-06-03 13:40:03 +0800201 unsigned int nr_ring_pages;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700202 struct request_queue *rq;
Jan Beulichb32728f2017-01-23 08:12:19 -0700203 unsigned int feature_flush:1;
204 unsigned int feature_fua:1;
Konrad Rzeszutek Wilk5ea42982011-10-12 16:23:30 -0400205 unsigned int feature_discard:1;
206 unsigned int feature_secdiscard:1;
Jan Beulichb32728f2017-01-23 08:12:19 -0700207 unsigned int feature_persistent:1;
Li Dongyanged30bf32011-09-01 18:39:09 +0800208 unsigned int discard_granularity;
209 unsigned int discard_alignment;
Julien Grallc004a6f2015-07-22 16:44:54 +0100210 /* Number of 4KB segments handled */
Roger Pau Monne402b27f2013-04-18 16:06:54 +0200211 unsigned int max_indirect_segments;
Christian Limpach1d78d702008-04-02 10:54:04 -0700212 int is_ready;
Bob Liu907c3eb2015-07-13 17:55:24 +0800213 struct blk_mq_tag_set tag_set;
Bob Liu3df0e502015-11-14 11:12:12 +0800214 struct blkfront_ring_info *rinfo;
215 unsigned int nr_rings;
Bob Liu7b427a52016-06-27 16:33:24 +0800216 /* Save uncomplete reqs and bios for migration. */
217 struct list_head requests;
218 struct bio_list bio_list;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700219};
220
Jan Beulich0e345822010-08-07 18:28:55 +0200221static unsigned int nr_minors;
222static unsigned long *minors;
223static DEFINE_SPINLOCK(minor_lock);
224
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700225#define GRANT_INVALID_REF 0
226
227#define PARTS_PER_DISK 16
Chris Lalancette9246b5f2008-09-17 14:30:32 -0700228#define PARTS_PER_EXT_DISK 256
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700229
230#define BLKIF_MAJOR(dev) ((dev)>>8)
231#define BLKIF_MINOR(dev) ((dev) & 0xff)
232
Chris Lalancette9246b5f2008-09-17 14:30:32 -0700233#define EXT_SHIFT 28
234#define EXTENDED (1<<EXT_SHIFT)
235#define VDEV_IS_EXTENDED(dev) ((dev)&(EXTENDED))
236#define BLKIF_MINOR_EXT(dev) ((dev)&(~EXTENDED))
Stefano Stabellinic80a4202010-12-02 17:55:00 +0000237#define EMULATED_HD_DISK_MINOR_OFFSET (0)
238#define EMULATED_HD_DISK_NAME_OFFSET (EMULATED_HD_DISK_MINOR_OFFSET / 256)
Stefan Bader196cfe22011-07-14 15:30:22 +0200239#define EMULATED_SD_DISK_MINOR_OFFSET (0)
240#define EMULATED_SD_DISK_NAME_OFFSET (EMULATED_SD_DISK_MINOR_OFFSET / 256)
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700241
Chris Lalancette9246b5f2008-09-17 14:30:32 -0700242#define DEV_NAME "xvd" /* name in /dev */
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700243
Julien Grallc004a6f2015-07-22 16:44:54 +0100244/*
245 * Grants are always the same size as a Xen page (i.e 4KB).
246 * A physical segment is always the same size as a Linux page.
247 * Number of grants per physical segment
248 */
249#define GRANTS_PER_PSEG (PAGE_SIZE / XEN_PAGE_SIZE)
250
251#define GRANTS_PER_INDIRECT_FRAME \
252 (XEN_PAGE_SIZE / sizeof(struct blkif_request_segment))
253
254#define PSEGS_PER_INDIRECT_FRAME \
255 (GRANTS_INDIRECT_FRAME / GRANTS_PSEGS)
256
257#define INDIRECT_GREFS(_grants) \
258 DIV_ROUND_UP(_grants, GRANTS_PER_INDIRECT_FRAME)
259
260#define GREFS(_psegs) ((_psegs) * GRANTS_PER_PSEG)
Roger Pau Monne402b27f2013-04-18 16:06:54 +0200261
Bob Liu81f35162015-11-14 11:12:11 +0800262static int blkfront_setup_indirect(struct blkfront_ring_info *rinfo);
Bob Liu3df0e502015-11-14 11:12:12 +0800263static void blkfront_gather_backend_features(struct blkfront_info *info);
Bhavesh Davda7ed8ce12017-12-22 14:17:13 -0800264static int negotiate_mq(struct blkfront_info *info);
Roger Pau Monne402b27f2013-04-18 16:06:54 +0200265
Bob Liu81f35162015-11-14 11:12:11 +0800266static int get_id_from_freelist(struct blkfront_ring_info *rinfo)
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700267{
Bob Liu81f35162015-11-14 11:12:11 +0800268 unsigned long free = rinfo->shadow_free;
269
270 BUG_ON(free >= BLK_RING_SIZE(rinfo->dev_info));
271 rinfo->shadow_free = rinfo->shadow[free].req.u.rw.id;
272 rinfo->shadow[free].req.u.rw.id = 0x0fffffee; /* debug */
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700273 return free;
274}
275
Bob Liu81f35162015-11-14 11:12:11 +0800276static int add_id_to_freelist(struct blkfront_ring_info *rinfo,
Konrad Rzeszutek Wilk6f03a7f2015-11-16 15:14:41 -0500277 unsigned long id)
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700278{
Bob Liu81f35162015-11-14 11:12:11 +0800279 if (rinfo->shadow[id].req.u.rw.id != id)
Konrad Rzeszutek Wilk6878c322012-05-25 17:34:51 -0400280 return -EINVAL;
Bob Liu81f35162015-11-14 11:12:11 +0800281 if (rinfo->shadow[id].request == NULL)
Konrad Rzeszutek Wilk6878c322012-05-25 17:34:51 -0400282 return -EINVAL;
Bob Liu81f35162015-11-14 11:12:11 +0800283 rinfo->shadow[id].req.u.rw.id = rinfo->shadow_free;
284 rinfo->shadow[id].request = NULL;
285 rinfo->shadow_free = id;
Konrad Rzeszutek Wilk6878c322012-05-25 17:34:51 -0400286 return 0;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700287}
288
Bob Liu81f35162015-11-14 11:12:11 +0800289static int fill_grant_buffer(struct blkfront_ring_info *rinfo, int num)
Roger Pau Monne9c1e0502013-03-18 17:49:35 +0100290{
Bob Liu81f35162015-11-14 11:12:11 +0800291 struct blkfront_info *info = rinfo->dev_info;
Roger Pau Monne9c1e0502013-03-18 17:49:35 +0100292 struct page *granted_page;
293 struct grant *gnt_list_entry, *n;
294 int i = 0;
295
Konrad Rzeszutek Wilk6f03a7f2015-11-16 15:14:41 -0500296 while (i < num) {
Roger Pau Monne9c1e0502013-03-18 17:49:35 +0100297 gnt_list_entry = kzalloc(sizeof(struct grant), GFP_NOIO);
298 if (!gnt_list_entry)
299 goto out_of_memory;
300
Roger Pau Monnebfe11d62013-10-29 18:31:14 +0100301 if (info->feature_persistent) {
302 granted_page = alloc_page(GFP_NOIO);
303 if (!granted_page) {
304 kfree(gnt_list_entry);
305 goto out_of_memory;
306 }
Julien Gralla7a6df22015-06-30 11:58:51 +0100307 gnt_list_entry->page = granted_page;
Roger Pau Monne9c1e0502013-03-18 17:49:35 +0100308 }
309
Roger Pau Monne9c1e0502013-03-18 17:49:35 +0100310 gnt_list_entry->gref = GRANT_INVALID_REF;
Bob Liu73716df2015-11-16 16:51:39 -0500311 list_add(&gnt_list_entry->node, &rinfo->grants);
Roger Pau Monne9c1e0502013-03-18 17:49:35 +0100312 i++;
313 }
314
315 return 0;
316
317out_of_memory:
318 list_for_each_entry_safe(gnt_list_entry, n,
Bob Liu73716df2015-11-16 16:51:39 -0500319 &rinfo->grants, node) {
Roger Pau Monne9c1e0502013-03-18 17:49:35 +0100320 list_del(&gnt_list_entry->node);
Roger Pau Monnebfe11d62013-10-29 18:31:14 +0100321 if (info->feature_persistent)
Julien Gralla7a6df22015-06-30 11:58:51 +0100322 __free_page(gnt_list_entry->page);
Roger Pau Monne9c1e0502013-03-18 17:49:35 +0100323 kfree(gnt_list_entry);
324 i--;
325 }
326 BUG_ON(i != 0);
327 return -ENOMEM;
328}
329
Bob Liu73716df2015-11-16 16:51:39 -0500330static struct grant *get_free_grant(struct blkfront_ring_info *rinfo)
Roger Pau Monne9c1e0502013-03-18 17:49:35 +0100331{
332 struct grant *gnt_list_entry;
Roger Pau Monne9c1e0502013-03-18 17:49:35 +0100333
Bob Liu73716df2015-11-16 16:51:39 -0500334 BUG_ON(list_empty(&rinfo->grants));
335 gnt_list_entry = list_first_entry(&rinfo->grants, struct grant,
Julien Grall4f503fb2015-07-01 14:10:38 +0100336 node);
Roger Pau Monne9c1e0502013-03-18 17:49:35 +0100337 list_del(&gnt_list_entry->node);
338
Julien Grall4f503fb2015-07-01 14:10:38 +0100339 if (gnt_list_entry->gref != GRANT_INVALID_REF)
Bob Liu73716df2015-11-16 16:51:39 -0500340 rinfo->persistent_gnts_c--;
Julien Grall4f503fb2015-07-01 14:10:38 +0100341
342 return gnt_list_entry;
343}
344
345static inline void grant_foreign_access(const struct grant *gnt_list_entry,
346 const struct blkfront_info *info)
347{
348 gnttab_page_grant_foreign_access_ref_one(gnt_list_entry->gref,
349 info->xbdev->otherend_id,
350 gnt_list_entry->page,
351 0);
352}
353
354static struct grant *get_grant(grant_ref_t *gref_head,
355 unsigned long gfn,
Bob Liu73716df2015-11-16 16:51:39 -0500356 struct blkfront_ring_info *rinfo)
Julien Grall4f503fb2015-07-01 14:10:38 +0100357{
Bob Liu73716df2015-11-16 16:51:39 -0500358 struct grant *gnt_list_entry = get_free_grant(rinfo);
359 struct blkfront_info *info = rinfo->dev_info;
Julien Grall4f503fb2015-07-01 14:10:38 +0100360
361 if (gnt_list_entry->gref != GRANT_INVALID_REF)
Roger Pau Monne9c1e0502013-03-18 17:49:35 +0100362 return gnt_list_entry;
Julien Grall4f503fb2015-07-01 14:10:38 +0100363
364 /* Assign a gref to this page */
365 gnt_list_entry->gref = gnttab_claim_grant_reference(gref_head);
366 BUG_ON(gnt_list_entry->gref == -ENOSPC);
367 if (info->feature_persistent)
368 grant_foreign_access(gnt_list_entry, info);
369 else {
370 /* Grant access to the GFN passed by the caller */
371 gnttab_grant_foreign_access_ref(gnt_list_entry->gref,
372 info->xbdev->otherend_id,
373 gfn, 0);
Roger Pau Monne9c1e0502013-03-18 17:49:35 +0100374 }
375
Julien Grall4f503fb2015-07-01 14:10:38 +0100376 return gnt_list_entry;
377}
378
379static struct grant *get_indirect_grant(grant_ref_t *gref_head,
Bob Liu73716df2015-11-16 16:51:39 -0500380 struct blkfront_ring_info *rinfo)
Julien Grall4f503fb2015-07-01 14:10:38 +0100381{
Bob Liu73716df2015-11-16 16:51:39 -0500382 struct grant *gnt_list_entry = get_free_grant(rinfo);
383 struct blkfront_info *info = rinfo->dev_info;
Julien Grall4f503fb2015-07-01 14:10:38 +0100384
385 if (gnt_list_entry->gref != GRANT_INVALID_REF)
386 return gnt_list_entry;
387
Roger Pau Monne9c1e0502013-03-18 17:49:35 +0100388 /* Assign a gref to this page */
389 gnt_list_entry->gref = gnttab_claim_grant_reference(gref_head);
390 BUG_ON(gnt_list_entry->gref == -ENOSPC);
Roger Pau Monnebfe11d62013-10-29 18:31:14 +0100391 if (!info->feature_persistent) {
Julien Grall4f503fb2015-07-01 14:10:38 +0100392 struct page *indirect_page;
393
394 /* Fetch a pre-allocated page to use for indirect grefs */
Bob Liu73716df2015-11-16 16:51:39 -0500395 BUG_ON(list_empty(&rinfo->indirect_pages));
396 indirect_page = list_first_entry(&rinfo->indirect_pages,
Julien Grall4f503fb2015-07-01 14:10:38 +0100397 struct page, lru);
398 list_del(&indirect_page->lru);
399 gnt_list_entry->page = indirect_page;
Roger Pau Monnebfe11d62013-10-29 18:31:14 +0100400 }
Julien Grall4f503fb2015-07-01 14:10:38 +0100401 grant_foreign_access(gnt_list_entry, info);
402
Roger Pau Monne9c1e0502013-03-18 17:49:35 +0100403 return gnt_list_entry;
404}
405
Konrad Rzeszutek Wilk6878c322012-05-25 17:34:51 -0400406static const char *op_name(int op)
407{
408 static const char *const names[] = {
409 [BLKIF_OP_READ] = "read",
410 [BLKIF_OP_WRITE] = "write",
411 [BLKIF_OP_WRITE_BARRIER] = "barrier",
412 [BLKIF_OP_FLUSH_DISKCACHE] = "flush",
413 [BLKIF_OP_DISCARD] = "discard" };
414
415 if (op < 0 || op >= ARRAY_SIZE(names))
416 return "unknown";
417
418 if (!names[op])
419 return "reserved";
420
421 return names[op];
422}
Jan Beulich0e345822010-08-07 18:28:55 +0200423static int xlbd_reserve_minors(unsigned int minor, unsigned int nr)
424{
425 unsigned int end = minor + nr;
426 int rc;
427
428 if (end > nr_minors) {
429 unsigned long *bitmap, *old;
430
Thomas Meyerf0941482011-11-29 22:08:00 +0100431 bitmap = kcalloc(BITS_TO_LONGS(end), sizeof(*bitmap),
Jan Beulich0e345822010-08-07 18:28:55 +0200432 GFP_KERNEL);
433 if (bitmap == NULL)
434 return -ENOMEM;
435
436 spin_lock(&minor_lock);
437 if (end > nr_minors) {
438 old = minors;
439 memcpy(bitmap, minors,
440 BITS_TO_LONGS(nr_minors) * sizeof(*bitmap));
441 minors = bitmap;
442 nr_minors = BITS_TO_LONGS(end) * BITS_PER_LONG;
443 } else
444 old = bitmap;
445 spin_unlock(&minor_lock);
446 kfree(old);
447 }
448
449 spin_lock(&minor_lock);
450 if (find_next_bit(minors, end, minor) >= end) {
Akinobu Mita34ae2e42012-01-21 00:15:26 +0900451 bitmap_set(minors, minor, nr);
Jan Beulich0e345822010-08-07 18:28:55 +0200452 rc = 0;
453 } else
454 rc = -EBUSY;
455 spin_unlock(&minor_lock);
456
457 return rc;
458}
459
460static void xlbd_release_minors(unsigned int minor, unsigned int nr)
461{
462 unsigned int end = minor + nr;
463
464 BUG_ON(end > nr_minors);
465 spin_lock(&minor_lock);
Akinobu Mita34ae2e42012-01-21 00:15:26 +0900466 bitmap_clear(minors, minor, nr);
Jan Beulich0e345822010-08-07 18:28:55 +0200467 spin_unlock(&minor_lock);
468}
469
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700470static void blkif_restart_queue_callback(void *arg)
471{
Bob Liu81f35162015-11-14 11:12:11 +0800472 struct blkfront_ring_info *rinfo = (struct blkfront_ring_info *)arg;
473 schedule_work(&rinfo->work);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700474}
475
Harvey Harrisonafe42d72008-04-29 00:59:47 -0700476static int blkif_getgeo(struct block_device *bd, struct hd_geometry *hg)
Ian Campbell597592d2008-02-21 13:03:45 -0800477{
478 /* We don't have real geometry info, but let's at least return
479 values consistent with the size of the device */
480 sector_t nsect = get_capacity(bd->bd_disk);
481 sector_t cylinders = nsect;
482
483 hg->heads = 0xff;
484 hg->sectors = 0x3f;
485 sector_div(cylinders, hg->heads * hg->sectors);
486 hg->cylinders = cylinders;
487 if ((sector_t)(hg->cylinders + 1) * hg->heads * hg->sectors < nsect)
488 hg->cylinders = 0xffff;
489 return 0;
490}
491
Al Viroa63c8482008-03-02 10:23:47 -0500492static int blkif_ioctl(struct block_device *bdev, fmode_t mode,
Adrian Bunk62aa0052008-08-04 11:59:05 +0200493 unsigned command, unsigned long argument)
Christian Limpach440a01a2008-06-17 10:47:08 +0200494{
Al Viroa63c8482008-03-02 10:23:47 -0500495 struct blkfront_info *info = bdev->bd_disk->private_data;
Christian Limpach440a01a2008-06-17 10:47:08 +0200496 int i;
497
498 dev_dbg(&info->xbdev->dev, "command: 0x%x, argument: 0x%lx\n",
499 command, (long)argument);
500
501 switch (command) {
502 case CDROMMULTISESSION:
503 dev_dbg(&info->xbdev->dev, "FIXME: support multisession CDs later\n");
504 for (i = 0; i < sizeof(struct cdrom_multisession); i++)
505 if (put_user(0, (char __user *)(argument + i)))
506 return -EFAULT;
507 return 0;
508
509 case CDROM_GET_CAPABILITY: {
510 struct gendisk *gd = info->gd;
511 if (gd->flags & GENHD_FL_CD)
512 return 0;
513 return -EINVAL;
514 }
515
516 default:
517 /*printk(KERN_ALERT "ioctl %08x not supported by Xen blkdev\n",
518 command);*/
519 return -EINVAL; /* same return as native Linux */
520 }
521
522 return 0;
523}
524
Julien Grall2e073962015-08-13 19:23:10 +0100525static unsigned long blkif_ring_get_request(struct blkfront_ring_info *rinfo,
526 struct request *req,
527 struct blkif_request **ring_req)
528{
529 unsigned long id;
530
531 *ring_req = RING_GET_REQUEST(&rinfo->ring, rinfo->ring.req_prod_pvt);
532 rinfo->ring.req_prod_pvt++;
533
534 id = get_id_from_freelist(rinfo);
535 rinfo->shadow[id].request = req;
Julien Grall6cc568332015-08-13 13:13:35 +0100536 rinfo->shadow[id].status = REQ_WAITING;
537 rinfo->shadow[id].associated_id = NO_ASSOCIATED_ID;
Julien Grall2e073962015-08-13 19:23:10 +0100538
539 (*ring_req)->u.rw.id = id;
540
541 return id;
542}
543
Bob Liu81f35162015-11-14 11:12:11 +0800544static int blkif_queue_discard_req(struct request *req, struct blkfront_ring_info *rinfo)
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700545{
Bob Liu81f35162015-11-14 11:12:11 +0800546 struct blkfront_info *info = rinfo->dev_info;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700547 struct blkif_request *ring_req;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700548 unsigned long id;
Julien Grall33204662015-06-29 17:35:24 +0100549
550 /* Fill out a communications ring structure. */
Julien Grall2e073962015-08-13 19:23:10 +0100551 id = blkif_ring_get_request(rinfo, req, &ring_req);
Julien Grall33204662015-06-29 17:35:24 +0100552
553 ring_req->operation = BLKIF_OP_DISCARD;
554 ring_req->u.discard.nr_sectors = blk_rq_sectors(req);
555 ring_req->u.discard.id = id;
556 ring_req->u.discard.sector_number = (blkif_sector_t)blk_rq_pos(req);
Christoph Hellwig288dab82016-06-09 16:00:36 +0200557 if (req_op(req) == REQ_OP_SECURE_ERASE && info->feature_secdiscard)
Julien Grall33204662015-06-29 17:35:24 +0100558 ring_req->u.discard.flag = BLKIF_DISCARD_SECURE;
559 else
560 ring_req->u.discard.flag = 0;
561
Julien Grall33204662015-06-29 17:35:24 +0100562 /* Keep a private copy so we can reissue requests when recovering. */
Bob Liu81f35162015-11-14 11:12:11 +0800563 rinfo->shadow[id].req = *ring_req;
Julien Grall33204662015-06-29 17:35:24 +0100564
565 return 0;
566}
567
Julien Grallc004a6f2015-07-22 16:44:54 +0100568struct setup_rw_req {
569 unsigned int grant_idx;
570 struct blkif_request_segment *segments;
Bob Liu81f35162015-11-14 11:12:11 +0800571 struct blkfront_ring_info *rinfo;
Julien Grallc004a6f2015-07-22 16:44:54 +0100572 struct blkif_request *ring_req;
573 grant_ref_t gref_head;
574 unsigned int id;
575 /* Only used when persistent grant is used and it's a read request */
576 bool need_copy;
577 unsigned int bvec_off;
578 char *bvec_data;
Julien Grall6cc568332015-08-13 13:13:35 +0100579
580 bool require_extra_req;
581 struct blkif_request *extra_ring_req;
Julien Grallc004a6f2015-07-22 16:44:54 +0100582};
583
584static void blkif_setup_rw_req_grant(unsigned long gfn, unsigned int offset,
585 unsigned int len, void *data)
586{
587 struct setup_rw_req *setup = data;
588 int n, ref;
589 struct grant *gnt_list_entry;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700590 unsigned int fsect, lsect;
Julien Grallc004a6f2015-07-22 16:44:54 +0100591 /* Convenient aliases */
592 unsigned int grant_idx = setup->grant_idx;
593 struct blkif_request *ring_req = setup->ring_req;
Bob Liu81f35162015-11-14 11:12:11 +0800594 struct blkfront_ring_info *rinfo = setup->rinfo;
Julien Grall6cc568332015-08-13 13:13:35 +0100595 /*
596 * We always use the shadow of the first request to store the list
597 * of grant associated to the block I/O request. This made the
598 * completion more easy to handle even if the block I/O request is
599 * split.
600 */
Bob Liu81f35162015-11-14 11:12:11 +0800601 struct blk_shadow *shadow = &rinfo->shadow[setup->id];
Julien Grallc004a6f2015-07-22 16:44:54 +0100602
Julien Grall6cc568332015-08-13 13:13:35 +0100603 if (unlikely(setup->require_extra_req &&
604 grant_idx >= BLKIF_MAX_SEGMENTS_PER_REQUEST)) {
605 /*
606 * We are using the second request, setup grant_idx
607 * to be the index of the segment array.
608 */
609 grant_idx -= BLKIF_MAX_SEGMENTS_PER_REQUEST;
610 ring_req = setup->extra_ring_req;
611 }
612
Julien Grallc004a6f2015-07-22 16:44:54 +0100613 if ((ring_req->operation == BLKIF_OP_INDIRECT) &&
614 (grant_idx % GRANTS_PER_INDIRECT_FRAME == 0)) {
615 if (setup->segments)
616 kunmap_atomic(setup->segments);
617
618 n = grant_idx / GRANTS_PER_INDIRECT_FRAME;
Bob Liu73716df2015-11-16 16:51:39 -0500619 gnt_list_entry = get_indirect_grant(&setup->gref_head, rinfo);
Julien Grallc004a6f2015-07-22 16:44:54 +0100620 shadow->indirect_grants[n] = gnt_list_entry;
621 setup->segments = kmap_atomic(gnt_list_entry->page);
622 ring_req->u.indirect.indirect_grefs[n] = gnt_list_entry->gref;
623 }
624
Bob Liu73716df2015-11-16 16:51:39 -0500625 gnt_list_entry = get_grant(&setup->gref_head, gfn, rinfo);
Julien Grallc004a6f2015-07-22 16:44:54 +0100626 ref = gnt_list_entry->gref;
Julien Grall6cc568332015-08-13 13:13:35 +0100627 /*
628 * All the grants are stored in the shadow of the first
629 * request. Therefore we have to use the global index.
630 */
631 shadow->grants_used[setup->grant_idx] = gnt_list_entry;
Julien Grallc004a6f2015-07-22 16:44:54 +0100632
633 if (setup->need_copy) {
634 void *shared_data;
635
636 shared_data = kmap_atomic(gnt_list_entry->page);
637 /*
638 * this does not wipe data stored outside the
639 * range sg->offset..sg->offset+sg->length.
640 * Therefore, blkback *could* see data from
641 * previous requests. This is OK as long as
642 * persistent grants are shared with just one
643 * domain. It may need refactoring if this
644 * changes
645 */
646 memcpy(shared_data + offset,
647 setup->bvec_data + setup->bvec_off,
648 len);
649
650 kunmap_atomic(shared_data);
651 setup->bvec_off += len;
652 }
653
654 fsect = offset >> 9;
655 lsect = fsect + (len >> 9) - 1;
656 if (ring_req->operation != BLKIF_OP_INDIRECT) {
657 ring_req->u.rw.seg[grant_idx] =
658 (struct blkif_request_segment) {
659 .gref = ref,
660 .first_sect = fsect,
661 .last_sect = lsect };
662 } else {
663 setup->segments[grant_idx % GRANTS_PER_INDIRECT_FRAME] =
664 (struct blkif_request_segment) {
665 .gref = ref,
666 .first_sect = fsect,
667 .last_sect = lsect };
668 }
669
670 (setup->grant_idx)++;
671}
672
Julien Grall6cc568332015-08-13 13:13:35 +0100673static void blkif_setup_extra_req(struct blkif_request *first,
674 struct blkif_request *second)
675{
676 uint16_t nr_segments = first->u.rw.nr_segments;
677
678 /*
679 * The second request is only present when the first request uses
680 * all its segments. It's always the continuity of the first one.
681 */
682 first->u.rw.nr_segments = BLKIF_MAX_SEGMENTS_PER_REQUEST;
683
684 second->u.rw.nr_segments = nr_segments - BLKIF_MAX_SEGMENTS_PER_REQUEST;
685 second->u.rw.sector_number = first->u.rw.sector_number +
686 (BLKIF_MAX_SEGMENTS_PER_REQUEST * XEN_PAGE_SIZE) / 512;
687
688 second->u.rw.handle = first->u.rw.handle;
689 second->operation = first->operation;
690}
691
Bob Liu81f35162015-11-14 11:12:11 +0800692static int blkif_queue_rw_req(struct request *req, struct blkfront_ring_info *rinfo)
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700693{
Bob Liu81f35162015-11-14 11:12:11 +0800694 struct blkfront_info *info = rinfo->dev_info;
Julien Grall6cc568332015-08-13 13:13:35 +0100695 struct blkif_request *ring_req, *extra_ring_req = NULL;
696 unsigned long id, extra_id = NO_ASSOCIATED_ID;
697 bool require_extra_req = false;
Julien Grallc004a6f2015-07-22 16:44:54 +0100698 int i;
699 struct setup_rw_req setup = {
700 .grant_idx = 0,
701 .segments = NULL,
Bob Liu81f35162015-11-14 11:12:11 +0800702 .rinfo = rinfo,
Julien Grallc004a6f2015-07-22 16:44:54 +0100703 .need_copy = rq_data_dir(req) && info->feature_persistent,
704 };
Roger Pau Monne0a8704a2012-10-24 18:58:45 +0200705
706 /*
707 * Used to store if we are able to queue the request by just using
708 * existing persistent grants, or if we have to get new grants,
709 * as there are not sufficiently many free.
710 */
Dongli Zhangbd912ef32017-06-28 20:57:28 +0800711 bool new_persistent_gnts = false;
Jens Axboe9e973e62009-02-24 08:10:09 +0100712 struct scatterlist *sg;
Julien Grallc004a6f2015-07-22 16:44:54 +0100713 int num_sg, max_grefs, num_grant;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700714
Julien Grallc004a6f2015-07-22 16:44:54 +0100715 max_grefs = req->nr_phys_segments * GRANTS_PER_PSEG;
Roger Pau Monnec47206e2013-08-12 12:53:43 +0200716 if (max_grefs > BLKIF_MAX_SEGMENTS_PER_REQUEST)
717 /*
718 * If we are using indirect segments we need to account
719 * for the indirect grefs used in the request.
720 */
Julien Grallc004a6f2015-07-22 16:44:54 +0100721 max_grefs += INDIRECT_GREFS(max_grefs);
Roger Pau Monne402b27f2013-04-18 16:06:54 +0200722
Dongli Zhangbd912ef32017-06-28 20:57:28 +0800723 /* Check if we have enough persistent grants to allocate a requests */
724 if (rinfo->persistent_gnts_c < max_grefs) {
725 new_persistent_gnts = true;
726
727 if (gnttab_alloc_grant_references(
728 max_grefs - rinfo->persistent_gnts_c,
729 &setup.gref_head) < 0) {
Roger Pau Monne0a8704a2012-10-24 18:58:45 +0200730 gnttab_request_free_callback(
Bob Liu81f35162015-11-14 11:12:11 +0800731 &rinfo->callback,
Roger Pau Monne0a8704a2012-10-24 18:58:45 +0200732 blkif_restart_queue_callback,
Bob Liu81f35162015-11-14 11:12:11 +0800733 rinfo,
Dongli Zhangbd912ef32017-06-28 20:57:28 +0800734 max_grefs - rinfo->persistent_gnts_c);
Roger Pau Monne0a8704a2012-10-24 18:58:45 +0200735 return 1;
736 }
Dongli Zhangbd912ef32017-06-28 20:57:28 +0800737 }
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700738
739 /* Fill out a communications ring structure. */
Julien Grall2e073962015-08-13 19:23:10 +0100740 id = blkif_ring_get_request(rinfo, req, &ring_req);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700741
Bob Liu81f35162015-11-14 11:12:11 +0800742 num_sg = blk_rq_map_sg(req->q, req, rinfo->shadow[id].sg);
Julien Grallc004a6f2015-07-22 16:44:54 +0100743 num_grant = 0;
744 /* Calculate the number of grant used */
Bob Liu81f35162015-11-14 11:12:11 +0800745 for_each_sg(rinfo->shadow[id].sg, sg, num_sg, i)
Julien Grallc004a6f2015-07-22 16:44:54 +0100746 num_grant += gnttab_count_grant(sg->offset, sg->length);
747
Julien Grall6cc568332015-08-13 13:13:35 +0100748 require_extra_req = info->max_indirect_segments == 0 &&
749 num_grant > BLKIF_MAX_SEGMENTS_PER_REQUEST;
750 BUG_ON(!HAS_EXTRA_REQ && require_extra_req);
751
Bob Liu81f35162015-11-14 11:12:11 +0800752 rinfo->shadow[id].num_sg = num_sg;
Julien Grall6cc568332015-08-13 13:13:35 +0100753 if (num_grant > BLKIF_MAX_SEGMENTS_PER_REQUEST &&
754 likely(!require_extra_req)) {
Julien Grall33204662015-06-29 17:35:24 +0100755 /*
756 * The indirect operation can only be a BLKIF_OP_READ or
757 * BLKIF_OP_WRITE
758 */
Mike Christie3a5e02c2016-06-05 14:32:23 -0500759 BUG_ON(req_op(req) == REQ_OP_FLUSH || req->cmd_flags & REQ_FUA);
Julien Grall33204662015-06-29 17:35:24 +0100760 ring_req->operation = BLKIF_OP_INDIRECT;
761 ring_req->u.indirect.indirect_op = rq_data_dir(req) ?
762 BLKIF_OP_WRITE : BLKIF_OP_READ;
763 ring_req->u.indirect.sector_number = (blkif_sector_t)blk_rq_pos(req);
764 ring_req->u.indirect.handle = info->handle;
Julien Grallc004a6f2015-07-22 16:44:54 +0100765 ring_req->u.indirect.nr_segments = num_grant;
Li Dongyanged30bf32011-09-01 18:39:09 +0800766 } else {
Julien Grall33204662015-06-29 17:35:24 +0100767 ring_req->u.rw.sector_number = (blkif_sector_t)blk_rq_pos(req);
768 ring_req->u.rw.handle = info->handle;
769 ring_req->operation = rq_data_dir(req) ?
770 BLKIF_OP_WRITE : BLKIF_OP_READ;
Mike Christie3a5e02c2016-06-05 14:32:23 -0500771 if (req_op(req) == REQ_OP_FLUSH || req->cmd_flags & REQ_FUA) {
Roger Pau Monne402b27f2013-04-18 16:06:54 +0200772 /*
Julien Grall33204662015-06-29 17:35:24 +0100773 * Ideally we can do an unordered flush-to-disk.
774 * In case the backend onlysupports barriers, use that.
775 * A barrier request a superset of FUA, so we can
776 * implement it the same way. (It's also a FLUSH+FUA,
777 * since it is guaranteed ordered WRT previous writes.)
Roger Pau Monne402b27f2013-04-18 16:06:54 +0200778 */
Mike Christiea4180902016-06-05 14:32:24 -0500779 if (info->feature_flush && info->feature_fua)
Julien Grall33204662015-06-29 17:35:24 +0100780 ring_req->operation =
781 BLKIF_OP_WRITE_BARRIER;
Mike Christiea4180902016-06-05 14:32:24 -0500782 else if (info->feature_flush)
Julien Grall33204662015-06-29 17:35:24 +0100783 ring_req->operation =
784 BLKIF_OP_FLUSH_DISKCACHE;
Mike Christiea4180902016-06-05 14:32:24 -0500785 else
Julien Grall33204662015-06-29 17:35:24 +0100786 ring_req->operation = 0;
Li Dongyanged30bf32011-09-01 18:39:09 +0800787 }
Julien Grallc004a6f2015-07-22 16:44:54 +0100788 ring_req->u.rw.nr_segments = num_grant;
Julien Grall6cc568332015-08-13 13:13:35 +0100789 if (unlikely(require_extra_req)) {
790 extra_id = blkif_ring_get_request(rinfo, req,
791 &extra_ring_req);
792 /*
793 * Only the first request contains the scatter-gather
794 * list.
795 */
796 rinfo->shadow[extra_id].num_sg = 0;
797
798 blkif_setup_extra_req(ring_req, extra_ring_req);
799
800 /* Link the 2 requests together */
801 rinfo->shadow[extra_id].associated_id = id;
802 rinfo->shadow[id].associated_id = extra_id;
803 }
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700804 }
805
Julien Grallc004a6f2015-07-22 16:44:54 +0100806 setup.ring_req = ring_req;
807 setup.id = id;
Julien Grall6cc568332015-08-13 13:13:35 +0100808
809 setup.require_extra_req = require_extra_req;
810 if (unlikely(require_extra_req))
811 setup.extra_ring_req = extra_ring_req;
812
Bob Liu81f35162015-11-14 11:12:11 +0800813 for_each_sg(rinfo->shadow[id].sg, sg, num_sg, i) {
Julien Grallc004a6f2015-07-22 16:44:54 +0100814 BUG_ON(sg->offset + sg->length > PAGE_SIZE);
Julien Grall33204662015-06-29 17:35:24 +0100815
Julien Grallc004a6f2015-07-22 16:44:54 +0100816 if (setup.need_copy) {
817 setup.bvec_off = sg->offset;
818 setup.bvec_data = kmap_atomic(sg_page(sg));
Julien Grall33204662015-06-29 17:35:24 +0100819 }
820
Julien Grallc004a6f2015-07-22 16:44:54 +0100821 gnttab_foreach_grant_in_range(sg_page(sg),
822 sg->offset,
823 sg->length,
824 blkif_setup_rw_req_grant,
825 &setup);
Julien Grall33204662015-06-29 17:35:24 +0100826
Julien Grallc004a6f2015-07-22 16:44:54 +0100827 if (setup.need_copy)
828 kunmap_atomic(setup.bvec_data);
Julien Grall33204662015-06-29 17:35:24 +0100829 }
Julien Grallc004a6f2015-07-22 16:44:54 +0100830 if (setup.segments)
831 kunmap_atomic(setup.segments);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700832
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700833 /* Keep a private copy so we can reissue requests when recovering. */
Bob Liu81f35162015-11-14 11:12:11 +0800834 rinfo->shadow[id].req = *ring_req;
Julien Grall6cc568332015-08-13 13:13:35 +0100835 if (unlikely(require_extra_req))
836 rinfo->shadow[extra_id].req = *extra_ring_req;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700837
Dongli Zhangbd912ef32017-06-28 20:57:28 +0800838 if (new_persistent_gnts)
Julien Grallc004a6f2015-07-22 16:44:54 +0100839 gnttab_free_grant_references(setup.gref_head);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700840
841 return 0;
842}
843
Julien Grall33204662015-06-29 17:35:24 +0100844/*
845 * Generate a Xen blkfront IO request from a blk layer request. Reads
846 * and writes are handled as expected.
847 *
848 * @req: a request struct
849 */
Bob Liu81f35162015-11-14 11:12:11 +0800850static int blkif_queue_request(struct request *req, struct blkfront_ring_info *rinfo)
Julien Grall33204662015-06-29 17:35:24 +0100851{
Bob Liu81f35162015-11-14 11:12:11 +0800852 if (unlikely(rinfo->dev_info->connected != BLKIF_STATE_CONNECTED))
Julien Grall33204662015-06-29 17:35:24 +0100853 return 1;
854
Mike Christiec2df40d2016-06-05 14:32:17 -0500855 if (unlikely(req_op(req) == REQ_OP_DISCARD ||
Christoph Hellwig288dab82016-06-09 16:00:36 +0200856 req_op(req) == REQ_OP_SECURE_ERASE))
Bob Liu81f35162015-11-14 11:12:11 +0800857 return blkif_queue_discard_req(req, rinfo);
Julien Grall33204662015-06-29 17:35:24 +0100858 else
Bob Liu81f35162015-11-14 11:12:11 +0800859 return blkif_queue_rw_req(req, rinfo);
Julien Grall33204662015-06-29 17:35:24 +0100860}
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700861
Bob Liu81f35162015-11-14 11:12:11 +0800862static inline void flush_requests(struct blkfront_ring_info *rinfo)
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700863{
864 int notify;
865
Bob Liu81f35162015-11-14 11:12:11 +0800866 RING_PUSH_REQUESTS_AND_CHECK_NOTIFY(&rinfo->ring, notify);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700867
868 if (notify)
Bob Liu81f35162015-11-14 11:12:11 +0800869 notify_remote_via_irq(rinfo->irq);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700870}
871
Vitaly Kuznetsovad42d392014-12-01 14:01:13 +0100872static inline bool blkif_request_flush_invalid(struct request *req,
873 struct blkfront_info *info)
Arianna Avanzini0f1ca652014-08-22 13:20:02 +0200874{
Christoph Hellwigaebf5262017-01-31 16:57:31 +0100875 return (blk_rq_is_passthrough(req) ||
Mike Christie3a5e02c2016-06-05 14:32:23 -0500876 ((req_op(req) == REQ_OP_FLUSH) &&
Mike Christiea4180902016-06-05 14:32:24 -0500877 !info->feature_flush) ||
Vitaly Kuznetsovad42d392014-12-01 14:01:13 +0100878 ((req->cmd_flags & REQ_FUA) &&
Mike Christiea4180902016-06-05 14:32:24 -0500879 !info->feature_fua));
Arianna Avanzini0f1ca652014-08-22 13:20:02 +0200880}
881
Christoph Hellwigfc17b652017-06-03 09:38:05 +0200882static blk_status_t blkif_queue_rq(struct blk_mq_hw_ctx *hctx,
Konrad Rzeszutek Wilk6f03a7f2015-11-16 15:14:41 -0500883 const struct blk_mq_queue_data *qd)
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700884{
Bob Liu11659562015-11-14 11:12:13 +0800885 unsigned long flags;
Bob Liu2a6f71a2016-05-31 16:59:17 +0800886 int qid = hctx->queue_num;
887 struct blkfront_info *info = hctx->queue->queuedata;
888 struct blkfront_ring_info *rinfo = NULL;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700889
Bob Liu2a6f71a2016-05-31 16:59:17 +0800890 BUG_ON(info->nr_rings <= qid);
891 rinfo = &info->rinfo[qid];
Bob Liu907c3eb2015-07-13 17:55:24 +0800892 blk_mq_start_request(qd->rq);
Bob Liu11659562015-11-14 11:12:13 +0800893 spin_lock_irqsave(&rinfo->ring_lock, flags);
Bob Liu81f35162015-11-14 11:12:11 +0800894 if (RING_FULL(&rinfo->ring))
Bob Liu907c3eb2015-07-13 17:55:24 +0800895 goto out_busy;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700896
Bob Liu81f35162015-11-14 11:12:11 +0800897 if (blkif_request_flush_invalid(qd->rq, rinfo->dev_info))
Bob Liu907c3eb2015-07-13 17:55:24 +0800898 goto out_err;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700899
Bob Liu81f35162015-11-14 11:12:11 +0800900 if (blkif_queue_request(qd->rq, rinfo))
Bob Liu907c3eb2015-07-13 17:55:24 +0800901 goto out_busy;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700902
Bob Liu81f35162015-11-14 11:12:11 +0800903 flush_requests(rinfo);
Bob Liu11659562015-11-14 11:12:13 +0800904 spin_unlock_irqrestore(&rinfo->ring_lock, flags);
Christoph Hellwigfc17b652017-06-03 09:38:05 +0200905 return BLK_STS_OK;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700906
Bob Liu907c3eb2015-07-13 17:55:24 +0800907out_err:
Bob Liu11659562015-11-14 11:12:13 +0800908 spin_unlock_irqrestore(&rinfo->ring_lock, flags);
Christoph Hellwigfc17b652017-06-03 09:38:05 +0200909 return BLK_STS_IOERR;
Tejun Heo296b2f62009-05-08 11:54:15 +0900910
Bob Liu907c3eb2015-07-13 17:55:24 +0800911out_busy:
Bob Liu907c3eb2015-07-13 17:55:24 +0800912 blk_mq_stop_hw_queue(hctx);
Junxiao Bi4b422cb2017-07-20 09:26:21 +0800913 spin_unlock_irqrestore(&rinfo->ring_lock, flags);
Ming Lei86ff7c22018-01-30 22:04:57 -0500914 return BLK_STS_DEV_RESOURCE;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700915}
916
Christoph Hellwig26095872017-04-20 16:03:08 +0200917static void blkif_complete_rq(struct request *rq)
918{
919 blk_mq_end_request(rq, blkif_req(rq)->error);
920}
921
Eric Biggersf363b082017-03-30 13:39:16 -0700922static const struct blk_mq_ops blkfront_mq_ops = {
Bob Liu907c3eb2015-07-13 17:55:24 +0800923 .queue_rq = blkif_queue_rq,
Christoph Hellwig26095872017-04-20 16:03:08 +0200924 .complete = blkif_complete_rq,
Bob Liu907c3eb2015-07-13 17:55:24 +0800925};
926
Bob Liu172335a2016-07-01 17:43:39 -0400927static void blkif_set_queue_limits(struct blkfront_info *info)
928{
929 struct request_queue *rq = info->rq;
930 struct gendisk *gd = info->gd;
931 unsigned int segments = info->max_indirect_segments ? :
932 BLKIF_MAX_SEGMENTS_PER_REQUEST;
933
Bart Van Assche8b904b52018-03-07 17:10:10 -0800934 blk_queue_flag_set(QUEUE_FLAG_VIRT, rq);
Bob Liu172335a2016-07-01 17:43:39 -0400935
936 if (info->feature_discard) {
Bart Van Assche8b904b52018-03-07 17:10:10 -0800937 blk_queue_flag_set(QUEUE_FLAG_DISCARD, rq);
Bob Liu172335a2016-07-01 17:43:39 -0400938 blk_queue_max_discard_sectors(rq, get_capacity(gd));
939 rq->limits.discard_granularity = info->discard_granularity;
940 rq->limits.discard_alignment = info->discard_alignment;
941 if (info->feature_secdiscard)
Bart Van Assche8b904b52018-03-07 17:10:10 -0800942 blk_queue_flag_set(QUEUE_FLAG_SECERASE, rq);
Bob Liu172335a2016-07-01 17:43:39 -0400943 }
944
945 /* Hard sector size and max sectors impersonate the equiv. hardware. */
946 blk_queue_logical_block_size(rq, info->sector_size);
947 blk_queue_physical_block_size(rq, info->physical_sector_size);
948 blk_queue_max_hw_sectors(rq, (segments * XEN_PAGE_SIZE) / 512);
949
950 /* Each segment in a request is up to an aligned page in size. */
951 blk_queue_segment_boundary(rq, PAGE_SIZE - 1);
952 blk_queue_max_segment_size(rq, PAGE_SIZE);
953
954 /* Ensure a merged request will fit in a single I/O ring slot. */
955 blk_queue_max_segments(rq, segments / GRANTS_PER_PSEG);
956
957 /* Make sure buffer addresses are sector-aligned. */
958 blk_queue_dma_alignment(rq, 511);
Bob Liu172335a2016-07-01 17:43:39 -0400959}
960
Roger Pau Monne402b27f2013-04-18 16:06:54 +0200961static int xlvbd_init_blk_queue(struct gendisk *gd, u16 sector_size,
Bob Liu172335a2016-07-01 17:43:39 -0400962 unsigned int physical_sector_size)
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700963{
Jens Axboe165125e2007-07-24 09:28:11 +0200964 struct request_queue *rq;
Li Dongyanged30bf32011-09-01 18:39:09 +0800965 struct blkfront_info *info = gd->private_data;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700966
Bob Liu907c3eb2015-07-13 17:55:24 +0800967 memset(&info->tag_set, 0, sizeof(info->tag_set));
968 info->tag_set.ops = &blkfront_mq_ops;
Bob Liu28d949b2015-11-14 11:12:14 +0800969 info->tag_set.nr_hw_queues = info->nr_rings;
Julien Grall6cc568332015-08-13 13:13:35 +0100970 if (HAS_EXTRA_REQ && info->max_indirect_segments == 0) {
971 /*
972 * When indirect descriptior is not supported, the I/O request
973 * will be split between multiple request in the ring.
974 * To avoid problems when sending the request, divide by
975 * 2 the depth of the queue.
976 */
977 info->tag_set.queue_depth = BLK_RING_SIZE(info) / 2;
978 } else
979 info->tag_set.queue_depth = BLK_RING_SIZE(info);
Bob Liu907c3eb2015-07-13 17:55:24 +0800980 info->tag_set.numa_node = NUMA_NO_NODE;
981 info->tag_set.flags = BLK_MQ_F_SHOULD_MERGE | BLK_MQ_F_SG_MERGE;
Christoph Hellwig26095872017-04-20 16:03:08 +0200982 info->tag_set.cmd_size = sizeof(struct blkif_req);
Bob Liu907c3eb2015-07-13 17:55:24 +0800983 info->tag_set.driver_data = info;
984
985 if (blk_mq_alloc_tag_set(&info->tag_set))
Konrad Rzeszutek Wilkbde21f72015-11-25 13:07:39 -0500986 return -EINVAL;
Bob Liu907c3eb2015-07-13 17:55:24 +0800987 rq = blk_mq_init_queue(&info->tag_set);
988 if (IS_ERR(rq)) {
989 blk_mq_free_tag_set(&info->tag_set);
Konrad Rzeszutek Wilkbde21f72015-11-25 13:07:39 -0500990 return PTR_ERR(rq);
Bob Liu907c3eb2015-07-13 17:55:24 +0800991 }
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700992
Bob Liu2a6f71a2016-05-31 16:59:17 +0800993 rq->queuedata = info;
Bob Liu172335a2016-07-01 17:43:39 -0400994 info->rq = gd->queue = rq;
995 info->gd = gd;
996 info->sector_size = sector_size;
997 info->physical_sector_size = physical_sector_size;
998 blkif_set_queue_limits(info);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700999
1000 return 0;
1001}
1002
Mike Christiea4180902016-06-05 14:32:24 -05001003static const char *flush_info(struct blkfront_info *info)
Vitaly Kuznetsovfdf9b962014-12-09 15:25:10 +01001004{
Mike Christiea4180902016-06-05 14:32:24 -05001005 if (info->feature_flush && info->feature_fua)
Vitaly Kuznetsovfdf9b962014-12-09 15:25:10 +01001006 return "barrier: enabled;";
Mike Christiea4180902016-06-05 14:32:24 -05001007 else if (info->feature_flush)
Vitaly Kuznetsovfdf9b962014-12-09 15:25:10 +01001008 return "flush diskcache: enabled;";
Mike Christiea4180902016-06-05 14:32:24 -05001009 else
Vitaly Kuznetsovfdf9b962014-12-09 15:25:10 +01001010 return "barrier or flush: disabled;";
Vitaly Kuznetsovfdf9b962014-12-09 15:25:10 +01001011}
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001012
Tejun Heo4913efe2010-09-03 11:56:16 +02001013static void xlvbd_flush(struct blkfront_info *info)
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001014{
Mike Christiea4180902016-06-05 14:32:24 -05001015 blk_queue_write_cache(info->rq, info->feature_flush ? true : false,
1016 info->feature_fua ? true : false);
Vitaly Kuznetsovfdf9b962014-12-09 15:25:10 +01001017 pr_info("blkfront: %s: %s %s %s %s %s\n",
Mike Christiea4180902016-06-05 14:32:24 -05001018 info->gd->disk_name, flush_info(info),
Vitaly Kuznetsovfdf9b962014-12-09 15:25:10 +01001019 "persistent grants:", info->feature_persistent ?
1020 "enabled;" : "disabled;", "indirect descriptors:",
1021 info->max_indirect_segments ? "enabled;" : "disabled;");
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001022}
1023
Stefano Stabellinic80a4202010-12-02 17:55:00 +00001024static int xen_translate_vdev(int vdevice, int *minor, unsigned int *offset)
1025{
1026 int major;
1027 major = BLKIF_MAJOR(vdevice);
1028 *minor = BLKIF_MINOR(vdevice);
1029 switch (major) {
1030 case XEN_IDE0_MAJOR:
1031 *offset = (*minor / 64) + EMULATED_HD_DISK_NAME_OFFSET;
1032 *minor = ((*minor / 64) * PARTS_PER_DISK) +
1033 EMULATED_HD_DISK_MINOR_OFFSET;
1034 break;
1035 case XEN_IDE1_MAJOR:
1036 *offset = (*minor / 64) + 2 + EMULATED_HD_DISK_NAME_OFFSET;
1037 *minor = (((*minor / 64) + 2) * PARTS_PER_DISK) +
1038 EMULATED_HD_DISK_MINOR_OFFSET;
1039 break;
1040 case XEN_SCSI_DISK0_MAJOR:
1041 *offset = (*minor / PARTS_PER_DISK) + EMULATED_SD_DISK_NAME_OFFSET;
1042 *minor = *minor + EMULATED_SD_DISK_MINOR_OFFSET;
1043 break;
1044 case XEN_SCSI_DISK1_MAJOR:
1045 case XEN_SCSI_DISK2_MAJOR:
1046 case XEN_SCSI_DISK3_MAJOR:
1047 case XEN_SCSI_DISK4_MAJOR:
1048 case XEN_SCSI_DISK5_MAJOR:
1049 case XEN_SCSI_DISK6_MAJOR:
1050 case XEN_SCSI_DISK7_MAJOR:
1051 *offset = (*minor / PARTS_PER_DISK) +
1052 ((major - XEN_SCSI_DISK1_MAJOR + 1) * 16) +
1053 EMULATED_SD_DISK_NAME_OFFSET;
1054 *minor = *minor +
1055 ((major - XEN_SCSI_DISK1_MAJOR + 1) * 16 * PARTS_PER_DISK) +
1056 EMULATED_SD_DISK_MINOR_OFFSET;
1057 break;
1058 case XEN_SCSI_DISK8_MAJOR:
1059 case XEN_SCSI_DISK9_MAJOR:
1060 case XEN_SCSI_DISK10_MAJOR:
1061 case XEN_SCSI_DISK11_MAJOR:
1062 case XEN_SCSI_DISK12_MAJOR:
1063 case XEN_SCSI_DISK13_MAJOR:
1064 case XEN_SCSI_DISK14_MAJOR:
1065 case XEN_SCSI_DISK15_MAJOR:
1066 *offset = (*minor / PARTS_PER_DISK) +
1067 ((major - XEN_SCSI_DISK8_MAJOR + 8) * 16) +
1068 EMULATED_SD_DISK_NAME_OFFSET;
1069 *minor = *minor +
1070 ((major - XEN_SCSI_DISK8_MAJOR + 8) * 16 * PARTS_PER_DISK) +
1071 EMULATED_SD_DISK_MINOR_OFFSET;
1072 break;
1073 case XENVBD_MAJOR:
1074 *offset = *minor / PARTS_PER_DISK;
1075 break;
1076 default:
1077 printk(KERN_WARNING "blkfront: your disk configuration is "
1078 "incorrect, please use an xvd device instead\n");
1079 return -ENODEV;
1080 }
1081 return 0;
1082}
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001083
Jan Beuliche77c78c2012-04-05 16:37:22 +01001084static char *encode_disk_name(char *ptr, unsigned int n)
1085{
1086 if (n >= 26)
1087 ptr = encode_disk_name(ptr, n / 26 - 1);
1088 *ptr = 'a' + n % 26;
1089 return ptr + 1;
1090}
1091
Chris Lalancette9246b5f2008-09-17 14:30:32 -07001092static int xlvbd_alloc_gendisk(blkif_sector_t capacity,
1093 struct blkfront_info *info,
Stefan Bader7c4d7d72013-05-13 16:28:15 +02001094 u16 vdisk_info, u16 sector_size,
1095 unsigned int physical_sector_size)
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001096{
1097 struct gendisk *gd;
1098 int nr_minors = 1;
Stefano Stabellinic80a4202010-12-02 17:55:00 +00001099 int err;
Chris Lalancette9246b5f2008-09-17 14:30:32 -07001100 unsigned int offset;
1101 int minor;
1102 int nr_parts;
Jan Beuliche77c78c2012-04-05 16:37:22 +01001103 char *ptr;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001104
1105 BUG_ON(info->gd != NULL);
1106 BUG_ON(info->rq != NULL);
1107
Chris Lalancette9246b5f2008-09-17 14:30:32 -07001108 if ((info->vdevice>>EXT_SHIFT) > 1) {
1109 /* this is above the extended range; something is wrong */
1110 printk(KERN_WARNING "blkfront: vdevice 0x%x is above the extended range; ignoring\n", info->vdevice);
1111 return -ENODEV;
1112 }
1113
1114 if (!VDEV_IS_EXTENDED(info->vdevice)) {
Stefano Stabellinic80a4202010-12-02 17:55:00 +00001115 err = xen_translate_vdev(info->vdevice, &minor, &offset);
1116 if (err)
1117 return err;
1118 nr_parts = PARTS_PER_DISK;
Chris Lalancette9246b5f2008-09-17 14:30:32 -07001119 } else {
1120 minor = BLKIF_MINOR_EXT(info->vdevice);
1121 nr_parts = PARTS_PER_EXT_DISK;
Stefano Stabellinic80a4202010-12-02 17:55:00 +00001122 offset = minor / nr_parts;
Stefan Bader89153b52011-07-14 15:30:37 +02001123 if (xen_hvm_domain() && offset < EMULATED_HD_DISK_NAME_OFFSET + 4)
Stefano Stabellinic80a4202010-12-02 17:55:00 +00001124 printk(KERN_WARNING "blkfront: vdevice 0x%x might conflict with "
1125 "emulated IDE disks,\n\t choose an xvd device name"
1126 "from xvde on\n", info->vdevice);
Chris Lalancette9246b5f2008-09-17 14:30:32 -07001127 }
Jan Beuliche77c78c2012-04-05 16:37:22 +01001128 if (minor >> MINORBITS) {
1129 pr_warn("blkfront: %#x's minor (%#x) out of range; ignoring\n",
1130 info->vdevice, minor);
1131 return -ENODEV;
1132 }
Chris Lalancette9246b5f2008-09-17 14:30:32 -07001133
1134 if ((minor % nr_parts) == 0)
1135 nr_minors = nr_parts;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001136
Jan Beulich0e345822010-08-07 18:28:55 +02001137 err = xlbd_reserve_minors(minor, nr_minors);
1138 if (err)
1139 goto out;
1140 err = -ENODEV;
1141
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001142 gd = alloc_disk(nr_minors);
1143 if (gd == NULL)
Jan Beulich0e345822010-08-07 18:28:55 +02001144 goto release;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001145
Jan Beuliche77c78c2012-04-05 16:37:22 +01001146 strcpy(gd->disk_name, DEV_NAME);
1147 ptr = encode_disk_name(gd->disk_name + sizeof(DEV_NAME) - 1, offset);
1148 BUG_ON(ptr >= gd->disk_name + DISK_NAME_LEN);
1149 if (nr_minors > 1)
1150 *ptr = 0;
1151 else
1152 snprintf(ptr, gd->disk_name + DISK_NAME_LEN - ptr,
1153 "%d", minor & (nr_parts - 1));
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001154
1155 gd->major = XENVBD_MAJOR;
1156 gd->first_minor = minor;
1157 gd->fops = &xlvbd_block_fops;
1158 gd->private_data = info;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001159 set_capacity(gd, capacity);
1160
Bob Liu172335a2016-07-01 17:43:39 -04001161 if (xlvbd_init_blk_queue(gd, sector_size, physical_sector_size)) {
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001162 del_gendisk(gd);
Jan Beulich0e345822010-08-07 18:28:55 +02001163 goto release;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001164 }
1165
Tejun Heo4913efe2010-09-03 11:56:16 +02001166 xlvbd_flush(info);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001167
1168 if (vdisk_info & VDISK_READONLY)
1169 set_disk_ro(gd, 1);
1170
1171 if (vdisk_info & VDISK_REMOVABLE)
1172 gd->flags |= GENHD_FL_REMOVABLE;
1173
1174 if (vdisk_info & VDISK_CDROM)
1175 gd->flags |= GENHD_FL_CD;
1176
1177 return 0;
1178
Jan Beulich0e345822010-08-07 18:28:55 +02001179 release:
1180 xlbd_release_minors(minor, nr_minors);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001181 out:
1182 return err;
1183}
1184
Daniel Stoddena66b5ae2010-08-07 18:33:17 +02001185static void xlvbd_release_gendisk(struct blkfront_info *info)
1186{
Bob Liu3df0e502015-11-14 11:12:12 +08001187 unsigned int minor, nr_minors, i;
Daniel Stoddena66b5ae2010-08-07 18:33:17 +02001188
1189 if (info->rq == NULL)
1190 return;
1191
Daniel Stoddena66b5ae2010-08-07 18:33:17 +02001192 /* No more blkif_request(). */
Bob Liu907c3eb2015-07-13 17:55:24 +08001193 blk_mq_stop_hw_queues(info->rq);
Daniel Stoddena66b5ae2010-08-07 18:33:17 +02001194
Bob Liu3df0e502015-11-14 11:12:12 +08001195 for (i = 0; i < info->nr_rings; i++) {
1196 struct blkfront_ring_info *rinfo = &info->rinfo[i];
Daniel Stoddena66b5ae2010-08-07 18:33:17 +02001197
Bob Liu3df0e502015-11-14 11:12:12 +08001198 /* No more gnttab callback work. */
1199 gnttab_cancel_free_callback(&rinfo->callback);
1200
1201 /* Flush gnttab callback work. Must be done with no locks held. */
1202 flush_work(&rinfo->work);
1203 }
Daniel Stoddena66b5ae2010-08-07 18:33:17 +02001204
1205 del_gendisk(info->gd);
1206
1207 minor = info->gd->first_minor;
1208 nr_minors = info->gd->minors;
1209 xlbd_release_minors(minor, nr_minors);
1210
1211 blk_cleanup_queue(info->rq);
Bob Liu907c3eb2015-07-13 17:55:24 +08001212 blk_mq_free_tag_set(&info->tag_set);
Daniel Stoddena66b5ae2010-08-07 18:33:17 +02001213 info->rq = NULL;
1214
1215 put_disk(info->gd);
1216 info->gd = NULL;
1217}
1218
Bob Liu11659562015-11-14 11:12:13 +08001219/* Already hold rinfo->ring_lock. */
1220static inline void kick_pending_request_queues_locked(struct blkfront_ring_info *rinfo)
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001221{
Bob Liu81f35162015-11-14 11:12:11 +08001222 if (!RING_FULL(&rinfo->ring))
1223 blk_mq_start_stopped_hw_queues(rinfo->dev_info->rq, true);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001224}
1225
Bob Liu11659562015-11-14 11:12:13 +08001226static void kick_pending_request_queues(struct blkfront_ring_info *rinfo)
1227{
1228 unsigned long flags;
1229
1230 spin_lock_irqsave(&rinfo->ring_lock, flags);
1231 kick_pending_request_queues_locked(rinfo);
1232 spin_unlock_irqrestore(&rinfo->ring_lock, flags);
1233}
1234
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001235static void blkif_restart_queue(struct work_struct *work)
1236{
Bob Liu81f35162015-11-14 11:12:11 +08001237 struct blkfront_ring_info *rinfo = container_of(work, struct blkfront_ring_info, work);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001238
Bob Liu81f35162015-11-14 11:12:11 +08001239 if (rinfo->dev_info->connected == BLKIF_STATE_CONNECTED)
1240 kick_pending_request_queues(rinfo);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001241}
1242
Bob Liu3df0e502015-11-14 11:12:12 +08001243static void blkif_free_ring(struct blkfront_ring_info *rinfo)
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001244{
Bob Liu73716df2015-11-16 16:51:39 -05001245 struct grant *persistent_gnt, *n;
Bob Liu3df0e502015-11-14 11:12:12 +08001246 struct blkfront_info *info = rinfo->dev_info;
Roger Pau Monne402b27f2013-04-18 16:06:54 +02001247 int i, j, segs;
Roger Pau Monne0a8704a2012-10-24 18:58:45 +02001248
Roger Pau Monnebfe11d62013-10-29 18:31:14 +01001249 /*
1250 * Remove indirect pages, this only happens when using indirect
1251 * descriptors but not persistent grants
1252 */
Bob Liu81f35162015-11-14 11:12:11 +08001253 if (!list_empty(&rinfo->indirect_pages)) {
Roger Pau Monnebfe11d62013-10-29 18:31:14 +01001254 struct page *indirect_page, *n;
1255
1256 BUG_ON(info->feature_persistent);
Bob Liu81f35162015-11-14 11:12:11 +08001257 list_for_each_entry_safe(indirect_page, n, &rinfo->indirect_pages, lru) {
Roger Pau Monnebfe11d62013-10-29 18:31:14 +01001258 list_del(&indirect_page->lru);
1259 __free_page(indirect_page);
1260 }
1261 }
1262
Bob Liu73716df2015-11-16 16:51:39 -05001263 /* Remove all persistent grants. */
1264 if (!list_empty(&rinfo->grants)) {
1265 list_for_each_entry_safe(persistent_gnt, n,
1266 &rinfo->grants, node) {
1267 list_del(&persistent_gnt->node);
1268 if (persistent_gnt->gref != GRANT_INVALID_REF) {
1269 gnttab_end_foreign_access(persistent_gnt->gref,
1270 0, 0UL);
1271 rinfo->persistent_gnts_c--;
1272 }
1273 if (info->feature_persistent)
1274 __free_page(persistent_gnt->page);
1275 kfree(persistent_gnt);
1276 }
1277 }
1278 BUG_ON(rinfo->persistent_gnts_c != 0);
1279
Bob Liu86839c52015-06-03 13:40:03 +08001280 for (i = 0; i < BLK_RING_SIZE(info); i++) {
Roger Pau Monne402b27f2013-04-18 16:06:54 +02001281 /*
1282 * Clear persistent grants present in requests already
1283 * on the shared ring
1284 */
Bob Liu81f35162015-11-14 11:12:11 +08001285 if (!rinfo->shadow[i].request)
Roger Pau Monne402b27f2013-04-18 16:06:54 +02001286 goto free_shadow;
1287
Bob Liu81f35162015-11-14 11:12:11 +08001288 segs = rinfo->shadow[i].req.operation == BLKIF_OP_INDIRECT ?
1289 rinfo->shadow[i].req.u.indirect.nr_segments :
1290 rinfo->shadow[i].req.u.rw.nr_segments;
Roger Pau Monne402b27f2013-04-18 16:06:54 +02001291 for (j = 0; j < segs; j++) {
Bob Liu81f35162015-11-14 11:12:11 +08001292 persistent_gnt = rinfo->shadow[i].grants_used[j];
Roger Pau Monne402b27f2013-04-18 16:06:54 +02001293 gnttab_end_foreign_access(persistent_gnt->gref, 0, 0UL);
Roger Pau Monnebfe11d62013-10-29 18:31:14 +01001294 if (info->feature_persistent)
Julien Gralla7a6df22015-06-30 11:58:51 +01001295 __free_page(persistent_gnt->page);
Roger Pau Monne402b27f2013-04-18 16:06:54 +02001296 kfree(persistent_gnt);
1297 }
1298
Bob Liu81f35162015-11-14 11:12:11 +08001299 if (rinfo->shadow[i].req.operation != BLKIF_OP_INDIRECT)
Roger Pau Monne402b27f2013-04-18 16:06:54 +02001300 /*
1301 * If this is not an indirect operation don't try to
1302 * free indirect segments
1303 */
1304 goto free_shadow;
1305
1306 for (j = 0; j < INDIRECT_GREFS(segs); j++) {
Bob Liu81f35162015-11-14 11:12:11 +08001307 persistent_gnt = rinfo->shadow[i].indirect_grants[j];
Roger Pau Monne402b27f2013-04-18 16:06:54 +02001308 gnttab_end_foreign_access(persistent_gnt->gref, 0, 0UL);
Julien Gralla7a6df22015-06-30 11:58:51 +01001309 __free_page(persistent_gnt->page);
Roger Pau Monne402b27f2013-04-18 16:06:54 +02001310 kfree(persistent_gnt);
1311 }
1312
1313free_shadow:
Bob Liu81f35162015-11-14 11:12:11 +08001314 kfree(rinfo->shadow[i].grants_used);
1315 rinfo->shadow[i].grants_used = NULL;
1316 kfree(rinfo->shadow[i].indirect_grants);
1317 rinfo->shadow[i].indirect_grants = NULL;
1318 kfree(rinfo->shadow[i].sg);
1319 rinfo->shadow[i].sg = NULL;
Roger Pau Monne402b27f2013-04-18 16:06:54 +02001320 }
1321
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001322 /* No more gnttab callback work. */
Bob Liu81f35162015-11-14 11:12:11 +08001323 gnttab_cancel_free_callback(&rinfo->callback);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001324
1325 /* Flush gnttab callback work. Must be done with no locks held. */
Bob Liu81f35162015-11-14 11:12:11 +08001326 flush_work(&rinfo->work);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001327
1328 /* Free resources associated with old device channel. */
Bob Liu86839c52015-06-03 13:40:03 +08001329 for (i = 0; i < info->nr_ring_pages; i++) {
Bob Liu81f35162015-11-14 11:12:11 +08001330 if (rinfo->ring_ref[i] != GRANT_INVALID_REF) {
1331 gnttab_end_foreign_access(rinfo->ring_ref[i], 0, 0);
1332 rinfo->ring_ref[i] = GRANT_INVALID_REF;
Bob Liu86839c52015-06-03 13:40:03 +08001333 }
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001334 }
Bob Liu6c647b02016-07-01 15:45:57 -04001335 free_pages((unsigned long)rinfo->ring.sring, get_order(info->nr_ring_pages * XEN_PAGE_SIZE));
Bob Liu81f35162015-11-14 11:12:11 +08001336 rinfo->ring.sring = NULL;
Bob Liu86839c52015-06-03 13:40:03 +08001337
Bob Liu81f35162015-11-14 11:12:11 +08001338 if (rinfo->irq)
1339 unbind_from_irqhandler(rinfo->irq, rinfo);
1340 rinfo->evtchn = rinfo->irq = 0;
Bob Liu3df0e502015-11-14 11:12:12 +08001341}
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001342
Bob Liu3df0e502015-11-14 11:12:12 +08001343static void blkif_free(struct blkfront_info *info, int suspend)
1344{
Bob Liu3df0e502015-11-14 11:12:12 +08001345 unsigned int i;
1346
1347 /* Prevent new requests being issued until we fix things up. */
Bob Liu3df0e502015-11-14 11:12:12 +08001348 info->connected = suspend ?
1349 BLKIF_STATE_SUSPENDED : BLKIF_STATE_DISCONNECTED;
1350 /* No more blkif_request(). */
1351 if (info->rq)
1352 blk_mq_stop_hw_queues(info->rq);
1353
Bob Liu3df0e502015-11-14 11:12:12 +08001354 for (i = 0; i < info->nr_rings; i++)
1355 blkif_free_ring(&info->rinfo[i]);
1356
1357 kfree(info->rinfo);
1358 info->rinfo = NULL;
1359 info->nr_rings = 0;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001360}
1361
Julien Grallc004a6f2015-07-22 16:44:54 +01001362struct copy_from_grant {
1363 const struct blk_shadow *s;
1364 unsigned int grant_idx;
1365 unsigned int bvec_offset;
1366 char *bvec_data;
1367};
1368
1369static void blkif_copy_from_grant(unsigned long gfn, unsigned int offset,
1370 unsigned int len, void *data)
1371{
1372 struct copy_from_grant *info = data;
1373 char *shared_data;
1374 /* Convenient aliases */
1375 const struct blk_shadow *s = info->s;
1376
1377 shared_data = kmap_atomic(s->grants_used[info->grant_idx]->page);
1378
1379 memcpy(info->bvec_data + info->bvec_offset,
1380 shared_data + offset, len);
1381
1382 info->bvec_offset += len;
1383 info->grant_idx++;
1384
1385 kunmap_atomic(shared_data);
1386}
1387
Julien Grall6cc568332015-08-13 13:13:35 +01001388static enum blk_req_status blkif_rsp_to_req_status(int rsp)
1389{
1390 switch (rsp)
1391 {
1392 case BLKIF_RSP_OKAY:
1393 return REQ_DONE;
1394 case BLKIF_RSP_EOPNOTSUPP:
1395 return REQ_EOPNOTSUPP;
1396 case BLKIF_RSP_ERROR:
1397 /* Fallthrough. */
1398 default:
1399 return REQ_ERROR;
1400 }
1401}
1402
1403/*
1404 * Get the final status of the block request based on two ring response
1405 */
1406static int blkif_get_final_status(enum blk_req_status s1,
1407 enum blk_req_status s2)
1408{
1409 BUG_ON(s1 == REQ_WAITING);
1410 BUG_ON(s2 == REQ_WAITING);
1411
1412 if (s1 == REQ_ERROR || s2 == REQ_ERROR)
1413 return BLKIF_RSP_ERROR;
1414 else if (s1 == REQ_EOPNOTSUPP || s2 == REQ_EOPNOTSUPP)
1415 return BLKIF_RSP_EOPNOTSUPP;
1416 return BLKIF_RSP_OKAY;
1417}
1418
1419static bool blkif_completion(unsigned long *id,
1420 struct blkfront_ring_info *rinfo,
Roger Pau Monne0a8704a2012-10-24 18:58:45 +02001421 struct blkif_response *bret)
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001422{
Roger Pau Monned62f6912012-12-07 19:00:31 +01001423 int i = 0;
Roger Pau Monneb7649152013-05-02 10:58:50 +02001424 struct scatterlist *sg;
Julien Grallc004a6f2015-07-22 16:44:54 +01001425 int num_sg, num_grant;
Bob Liu81f35162015-11-14 11:12:11 +08001426 struct blkfront_info *info = rinfo->dev_info;
Julien Grall6cc568332015-08-13 13:13:35 +01001427 struct blk_shadow *s = &rinfo->shadow[*id];
Julien Grallc004a6f2015-07-22 16:44:54 +01001428 struct copy_from_grant data = {
Julien Grallc004a6f2015-07-22 16:44:54 +01001429 .grant_idx = 0,
1430 };
Roger Pau Monne402b27f2013-04-18 16:06:54 +02001431
Julien Grallc004a6f2015-07-22 16:44:54 +01001432 num_grant = s->req.operation == BLKIF_OP_INDIRECT ?
Roger Pau Monne402b27f2013-04-18 16:06:54 +02001433 s->req.u.indirect.nr_segments : s->req.u.rw.nr_segments;
Julien Grall6cc568332015-08-13 13:13:35 +01001434
1435 /* The I/O request may be split in two. */
1436 if (unlikely(s->associated_id != NO_ASSOCIATED_ID)) {
1437 struct blk_shadow *s2 = &rinfo->shadow[s->associated_id];
1438
1439 /* Keep the status of the current response in shadow. */
1440 s->status = blkif_rsp_to_req_status(bret->status);
1441
1442 /* Wait the second response if not yet here. */
1443 if (s2->status == REQ_WAITING)
1444 return 0;
1445
1446 bret->status = blkif_get_final_status(s->status,
1447 s2->status);
1448
1449 /*
1450 * All the grants is stored in the first shadow in order
1451 * to make the completion code simpler.
1452 */
1453 num_grant += s2->req.u.rw.nr_segments;
1454
1455 /*
1456 * The two responses may not come in order. Only the
1457 * first request will store the scatter-gather list.
1458 */
1459 if (s2->num_sg != 0) {
1460 /* Update "id" with the ID of the first response. */
1461 *id = s->associated_id;
1462 s = s2;
1463 }
1464
1465 /*
1466 * We don't need anymore the second request, so recycling
1467 * it now.
1468 */
1469 if (add_id_to_freelist(rinfo, s->associated_id))
1470 WARN(1, "%s: can't recycle the second part (id = %ld) of the request\n",
1471 info->gd->disk_name, s->associated_id);
1472 }
1473
1474 data.s = s;
Julien Grallc004a6f2015-07-22 16:44:54 +01001475 num_sg = s->num_sg;
Roger Pau Monne0a8704a2012-10-24 18:58:45 +02001476
Roger Pau Monnebfe11d62013-10-29 18:31:14 +01001477 if (bret->operation == BLKIF_OP_READ && info->feature_persistent) {
Julien Grallc004a6f2015-07-22 16:44:54 +01001478 for_each_sg(s->sg, sg, num_sg, i) {
Roger Pau Monneb7649152013-05-02 10:58:50 +02001479 BUG_ON(sg->offset + sg->length > PAGE_SIZE);
Julien Grallc004a6f2015-07-22 16:44:54 +01001480
1481 data.bvec_offset = sg->offset;
1482 data.bvec_data = kmap_atomic(sg_page(sg));
1483
1484 gnttab_foreach_grant_in_range(sg_page(sg),
1485 sg->offset,
1486 sg->length,
1487 blkif_copy_from_grant,
1488 &data);
1489
1490 kunmap_atomic(data.bvec_data);
Roger Pau Monne0a8704a2012-10-24 18:58:45 +02001491 }
1492 }
1493 /* Add the persistent grant into the list of free grants */
Julien Grallc004a6f2015-07-22 16:44:54 +01001494 for (i = 0; i < num_grant; i++) {
Roger Pau Monnefbe363c2013-08-12 12:53:44 +02001495 if (gnttab_query_foreign_access(s->grants_used[i]->gref)) {
1496 /*
1497 * If the grant is still mapped by the backend (the
1498 * backend has chosen to make this grant persistent)
1499 * we add it at the head of the list, so it will be
1500 * reused first.
1501 */
Roger Pau Monnebfe11d62013-10-29 18:31:14 +01001502 if (!info->feature_persistent)
1503 pr_alert_ratelimited("backed has not unmapped grant: %u\n",
1504 s->grants_used[i]->gref);
Bob Liu73716df2015-11-16 16:51:39 -05001505 list_add(&s->grants_used[i]->node, &rinfo->grants);
1506 rinfo->persistent_gnts_c++;
Roger Pau Monnefbe363c2013-08-12 12:53:44 +02001507 } else {
1508 /*
1509 * If the grant is not mapped by the backend we end the
1510 * foreign access and add it to the tail of the list,
1511 * so it will not be picked again unless we run out of
1512 * persistent grants.
1513 */
1514 gnttab_end_foreign_access(s->grants_used[i]->gref, 0, 0UL);
1515 s->grants_used[i]->gref = GRANT_INVALID_REF;
Bob Liu73716df2015-11-16 16:51:39 -05001516 list_add_tail(&s->grants_used[i]->node, &rinfo->grants);
Roger Pau Monnefbe363c2013-08-12 12:53:44 +02001517 }
Roger Pau Monne0a8704a2012-10-24 18:58:45 +02001518 }
Roger Pau Monne402b27f2013-04-18 16:06:54 +02001519 if (s->req.operation == BLKIF_OP_INDIRECT) {
Julien Grallc004a6f2015-07-22 16:44:54 +01001520 for (i = 0; i < INDIRECT_GREFS(num_grant); i++) {
Roger Pau Monnefbe363c2013-08-12 12:53:44 +02001521 if (gnttab_query_foreign_access(s->indirect_grants[i]->gref)) {
Roger Pau Monnebfe11d62013-10-29 18:31:14 +01001522 if (!info->feature_persistent)
1523 pr_alert_ratelimited("backed has not unmapped grant: %u\n",
1524 s->indirect_grants[i]->gref);
Bob Liu73716df2015-11-16 16:51:39 -05001525 list_add(&s->indirect_grants[i]->node, &rinfo->grants);
1526 rinfo->persistent_gnts_c++;
Roger Pau Monnefbe363c2013-08-12 12:53:44 +02001527 } else {
Roger Pau Monnebfe11d62013-10-29 18:31:14 +01001528 struct page *indirect_page;
1529
Roger Pau Monnefbe363c2013-08-12 12:53:44 +02001530 gnttab_end_foreign_access(s->indirect_grants[i]->gref, 0, 0UL);
Roger Pau Monnebfe11d62013-10-29 18:31:14 +01001531 /*
1532 * Add the used indirect page back to the list of
1533 * available pages for indirect grefs.
1534 */
Bob Liu7b076752015-07-22 14:40:09 +08001535 if (!info->feature_persistent) {
Julien Gralla7a6df22015-06-30 11:58:51 +01001536 indirect_page = s->indirect_grants[i]->page;
Bob Liu81f35162015-11-14 11:12:11 +08001537 list_add(&indirect_page->lru, &rinfo->indirect_pages);
Bob Liu7b076752015-07-22 14:40:09 +08001538 }
Roger Pau Monnefbe363c2013-08-12 12:53:44 +02001539 s->indirect_grants[i]->gref = GRANT_INVALID_REF;
Bob Liu73716df2015-11-16 16:51:39 -05001540 list_add_tail(&s->indirect_grants[i]->node, &rinfo->grants);
Roger Pau Monnefbe363c2013-08-12 12:53:44 +02001541 }
Roger Pau Monne402b27f2013-04-18 16:06:54 +02001542 }
1543 }
Julien Grall6cc568332015-08-13 13:13:35 +01001544
1545 return 1;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001546}
1547
1548static irqreturn_t blkif_interrupt(int irq, void *dev_id)
1549{
1550 struct request *req;
1551 struct blkif_response *bret;
1552 RING_IDX i, rp;
1553 unsigned long flags;
Bob Liu81f35162015-11-14 11:12:11 +08001554 struct blkfront_ring_info *rinfo = (struct blkfront_ring_info *)dev_id;
1555 struct blkfront_info *info = rinfo->dev_info;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001556
Bob Liu11659562015-11-14 11:12:13 +08001557 if (unlikely(info->connected != BLKIF_STATE_CONNECTED))
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001558 return IRQ_HANDLED;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001559
Bob Liu11659562015-11-14 11:12:13 +08001560 spin_lock_irqsave(&rinfo->ring_lock, flags);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001561 again:
Bob Liu81f35162015-11-14 11:12:11 +08001562 rp = rinfo->ring.sring->rsp_prod;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001563 rmb(); /* Ensure we see queued responses up to 'rp'. */
1564
Bob Liu81f35162015-11-14 11:12:11 +08001565 for (i = rinfo->ring.rsp_cons; i != rp; i++) {
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001566 unsigned long id;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001567
Bob Liu81f35162015-11-14 11:12:11 +08001568 bret = RING_GET_RESPONSE(&rinfo->ring, i);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001569 id = bret->id;
Konrad Rzeszutek Wilk6878c322012-05-25 17:34:51 -04001570 /*
1571 * The backend has messed up and given us an id that we would
1572 * never have given to it (we stamp it up to BLK_RING_SIZE -
1573 * look in get_id_from_freelist.
1574 */
Bob Liu86839c52015-06-03 13:40:03 +08001575 if (id >= BLK_RING_SIZE(info)) {
Konrad Rzeszutek Wilk6878c322012-05-25 17:34:51 -04001576 WARN(1, "%s: response to %s has incorrect id (%ld)\n",
1577 info->gd->disk_name, op_name(bret->operation), id);
1578 /* We can't safely get the 'struct request' as
1579 * the id is busted. */
1580 continue;
1581 }
Bob Liu81f35162015-11-14 11:12:11 +08001582 req = rinfo->shadow[id].request;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001583
Julien Grall6cc568332015-08-13 13:13:35 +01001584 if (bret->operation != BLKIF_OP_DISCARD) {
1585 /*
1586 * We may need to wait for an extra response if the
1587 * I/O request is split in 2
1588 */
1589 if (!blkif_completion(&id, rinfo, bret))
1590 continue;
1591 }
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001592
Bob Liu81f35162015-11-14 11:12:11 +08001593 if (add_id_to_freelist(rinfo, id)) {
Konrad Rzeszutek Wilk6878c322012-05-25 17:34:51 -04001594 WARN(1, "%s: response to %s (id %ld) couldn't be recycled!\n",
1595 info->gd->disk_name, op_name(bret->operation), id);
1596 continue;
1597 }
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001598
Christoph Hellwig2a842ac2017-06-03 09:38:04 +02001599 if (bret->status == BLKIF_RSP_OKAY)
1600 blkif_req(req)->error = BLK_STS_OK;
1601 else
1602 blkif_req(req)->error = BLK_STS_IOERR;
1603
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001604 switch (bret->operation) {
Li Dongyanged30bf32011-09-01 18:39:09 +08001605 case BLKIF_OP_DISCARD:
1606 if (unlikely(bret->status == BLKIF_RSP_EOPNOTSUPP)) {
1607 struct request_queue *rq = info->rq;
Konrad Rzeszutek Wilk6878c322012-05-25 17:34:51 -04001608 printk(KERN_WARNING "blkfront: %s: %s op failed\n",
1609 info->gd->disk_name, op_name(bret->operation));
Christoph Hellwig2a842ac2017-06-03 09:38:04 +02001610 blkif_req(req)->error = BLK_STS_NOTSUPP;
Li Dongyanged30bf32011-09-01 18:39:09 +08001611 info->feature_discard = 0;
Konrad Rzeszutek Wilk5ea42982011-10-12 16:23:30 -04001612 info->feature_secdiscard = 0;
Bart Van Assche8b904b52018-03-07 17:10:10 -08001613 blk_queue_flag_clear(QUEUE_FLAG_DISCARD, rq);
1614 blk_queue_flag_clear(QUEUE_FLAG_SECERASE, rq);
Li Dongyanged30bf32011-09-01 18:39:09 +08001615 }
Li Dongyanged30bf32011-09-01 18:39:09 +08001616 break;
Konrad Rzeszutek Wilkedf6ef52011-05-03 12:01:11 -04001617 case BLKIF_OP_FLUSH_DISKCACHE:
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001618 case BLKIF_OP_WRITE_BARRIER:
1619 if (unlikely(bret->status == BLKIF_RSP_EOPNOTSUPP)) {
Konrad Rzeszutek Wilk6878c322012-05-25 17:34:51 -04001620 printk(KERN_WARNING "blkfront: %s: %s op failed\n",
1621 info->gd->disk_name, op_name(bret->operation));
Bart Van Assche31c4ccc2017-07-21 19:11:10 +02001622 blkif_req(req)->error = BLK_STS_NOTSUPP;
Jeremy Fitzhardingedcb8bae2010-11-02 11:55:58 -04001623 }
1624 if (unlikely(bret->status == BLKIF_RSP_ERROR &&
Bob Liu81f35162015-11-14 11:12:11 +08001625 rinfo->shadow[id].req.u.rw.nr_segments == 0)) {
Konrad Rzeszutek Wilk6878c322012-05-25 17:34:51 -04001626 printk(KERN_WARNING "blkfront: %s: empty %s op failed\n",
1627 info->gd->disk_name, op_name(bret->operation));
Christoph Hellwig2a842ac2017-06-03 09:38:04 +02001628 blkif_req(req)->error = BLK_STS_NOTSUPP;
Jeremy Fitzhardingedcb8bae2010-11-02 11:55:58 -04001629 }
Christoph Hellwig26095872017-04-20 16:03:08 +02001630 if (unlikely(blkif_req(req)->error)) {
Christoph Hellwig2a842ac2017-06-03 09:38:04 +02001631 if (blkif_req(req)->error == BLK_STS_NOTSUPP)
1632 blkif_req(req)->error = BLK_STS_OK;
Mike Christiea4180902016-06-05 14:32:24 -05001633 info->feature_fua = 0;
Tejun Heo4913efe2010-09-03 11:56:16 +02001634 info->feature_flush = 0;
1635 xlvbd_flush(info);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001636 }
1637 /* fall through */
1638 case BLKIF_OP_READ:
1639 case BLKIF_OP_WRITE:
1640 if (unlikely(bret->status != BLKIF_RSP_OKAY))
1641 dev_dbg(&info->xbdev->dev, "Bad return from blkdev data "
1642 "request: %x\n", bret->status);
1643
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001644 break;
1645 default:
1646 BUG();
1647 }
Christoph Hellwig26095872017-04-20 16:03:08 +02001648
Christoph Hellwig08e00292017-04-20 16:03:09 +02001649 blk_mq_complete_request(req);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001650 }
1651
Bob Liu81f35162015-11-14 11:12:11 +08001652 rinfo->ring.rsp_cons = i;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001653
Bob Liu81f35162015-11-14 11:12:11 +08001654 if (i != rinfo->ring.req_prod_pvt) {
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001655 int more_to_do;
Bob Liu81f35162015-11-14 11:12:11 +08001656 RING_FINAL_CHECK_FOR_RESPONSES(&rinfo->ring, more_to_do);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001657 if (more_to_do)
1658 goto again;
1659 } else
Bob Liu81f35162015-11-14 11:12:11 +08001660 rinfo->ring.sring->rsp_event = i + 1;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001661
Bob Liu11659562015-11-14 11:12:13 +08001662 kick_pending_request_queues_locked(rinfo);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001663
Bob Liu11659562015-11-14 11:12:13 +08001664 spin_unlock_irqrestore(&rinfo->ring_lock, flags);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001665
1666 return IRQ_HANDLED;
1667}
1668
1669
1670static int setup_blkring(struct xenbus_device *dev,
Bob Liu81f35162015-11-14 11:12:11 +08001671 struct blkfront_ring_info *rinfo)
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001672{
1673 struct blkif_sring *sring;
Bob Liu86839c52015-06-03 13:40:03 +08001674 int err, i;
Bob Liu81f35162015-11-14 11:12:11 +08001675 struct blkfront_info *info = rinfo->dev_info;
Julien Grallc004a6f2015-07-22 16:44:54 +01001676 unsigned long ring_size = info->nr_ring_pages * XEN_PAGE_SIZE;
Julien Grall9cce2912015-10-13 17:50:11 +01001677 grant_ref_t gref[XENBUS_MAX_RING_GRANTS];
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001678
Bob Liu86839c52015-06-03 13:40:03 +08001679 for (i = 0; i < info->nr_ring_pages; i++)
Bob Liu81f35162015-11-14 11:12:11 +08001680 rinfo->ring_ref[i] = GRANT_INVALID_REF;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001681
Bob Liu86839c52015-06-03 13:40:03 +08001682 sring = (struct blkif_sring *)__get_free_pages(GFP_NOIO | __GFP_HIGH,
1683 get_order(ring_size));
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001684 if (!sring) {
1685 xenbus_dev_fatal(dev, -ENOMEM, "allocating shared ring");
1686 return -ENOMEM;
1687 }
1688 SHARED_RING_INIT(sring);
Bob Liu81f35162015-11-14 11:12:11 +08001689 FRONT_RING_INIT(&rinfo->ring, sring, ring_size);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001690
Bob Liu81f35162015-11-14 11:12:11 +08001691 err = xenbus_grant_ring(dev, rinfo->ring.sring, info->nr_ring_pages, gref);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001692 if (err < 0) {
Bob Liu86839c52015-06-03 13:40:03 +08001693 free_pages((unsigned long)sring, get_order(ring_size));
Bob Liu81f35162015-11-14 11:12:11 +08001694 rinfo->ring.sring = NULL;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001695 goto fail;
1696 }
Bob Liu86839c52015-06-03 13:40:03 +08001697 for (i = 0; i < info->nr_ring_pages; i++)
Bob Liu81f35162015-11-14 11:12:11 +08001698 rinfo->ring_ref[i] = gref[i];
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001699
Bob Liu81f35162015-11-14 11:12:11 +08001700 err = xenbus_alloc_evtchn(dev, &rinfo->evtchn);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001701 if (err)
1702 goto fail;
1703
Bob Liu81f35162015-11-14 11:12:11 +08001704 err = bind_evtchn_to_irqhandler(rinfo->evtchn, blkif_interrupt, 0,
1705 "blkif", rinfo);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001706 if (err <= 0) {
1707 xenbus_dev_fatal(dev, err,
1708 "bind_evtchn_to_irqhandler failed");
1709 goto fail;
1710 }
Bob Liu81f35162015-11-14 11:12:11 +08001711 rinfo->irq = err;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001712
1713 return 0;
1714fail:
1715 blkif_free(info, 0);
1716 return err;
1717}
1718
Bob Liu28d949b2015-11-14 11:12:14 +08001719/*
1720 * Write out per-ring/queue nodes including ring-ref and event-channel, and each
1721 * ring buffer may have multi pages depending on ->nr_ring_pages.
1722 */
1723static int write_per_ring_nodes(struct xenbus_transaction xbt,
1724 struct blkfront_ring_info *rinfo, const char *dir)
1725{
1726 int err;
1727 unsigned int i;
1728 const char *message = NULL;
1729 struct blkfront_info *info = rinfo->dev_info;
1730
1731 if (info->nr_ring_pages == 1) {
1732 err = xenbus_printf(xbt, dir, "ring-ref", "%u", rinfo->ring_ref[0]);
1733 if (err) {
1734 message = "writing ring-ref";
1735 goto abort_transaction;
1736 }
1737 } else {
1738 for (i = 0; i < info->nr_ring_pages; i++) {
1739 char ring_ref_name[RINGREF_NAME_LEN];
1740
1741 snprintf(ring_ref_name, RINGREF_NAME_LEN, "ring-ref%u", i);
1742 err = xenbus_printf(xbt, dir, ring_ref_name,
1743 "%u", rinfo->ring_ref[i]);
1744 if (err) {
1745 message = "writing ring-ref";
1746 goto abort_transaction;
1747 }
1748 }
1749 }
1750
1751 err = xenbus_printf(xbt, dir, "event-channel", "%u", rinfo->evtchn);
1752 if (err) {
1753 message = "writing event-channel";
1754 goto abort_transaction;
1755 }
1756
1757 return 0;
1758
1759abort_transaction:
1760 xenbus_transaction_end(xbt, 1);
1761 if (message)
1762 xenbus_dev_fatal(info->xbdev, err, "%s", message);
1763
1764 return err;
1765}
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001766
1767/* Common code used when first setting up, and when resuming. */
Ian Campbell203fd612009-12-04 15:33:54 +00001768static int talk_to_blkback(struct xenbus_device *dev,
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001769 struct blkfront_info *info)
1770{
1771 const char *message = NULL;
1772 struct xenbus_transaction xbt;
Bob Liu28d949b2015-11-14 11:12:14 +08001773 int err;
Juergen Grossf27dc1a2016-10-31 14:58:40 +01001774 unsigned int i, max_page_order;
1775 unsigned int ring_page_order;
Bob Liu86839c52015-06-03 13:40:03 +08001776
Bhavesh Davda7ed8ce12017-12-22 14:17:13 -08001777 if (!info)
1778 return -ENODEV;
1779
Juergen Grossf27dc1a2016-10-31 14:58:40 +01001780 max_page_order = xenbus_read_unsigned(info->xbdev->otherend,
1781 "max-ring-page-order", 0);
1782 ring_page_order = min(xen_blkif_max_ring_order, max_page_order);
1783 info->nr_ring_pages = 1 << ring_page_order;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001784
Bhavesh Davda7ed8ce12017-12-22 14:17:13 -08001785 err = negotiate_mq(info);
1786 if (err)
1787 goto destroy_blkring;
1788
Bob Liu3df0e502015-11-14 11:12:12 +08001789 for (i = 0; i < info->nr_rings; i++) {
Bob Liu28d949b2015-11-14 11:12:14 +08001790 struct blkfront_ring_info *rinfo = &info->rinfo[i];
1791
Bob Liu3df0e502015-11-14 11:12:12 +08001792 /* Create shared ring, alloc event channel. */
1793 err = setup_blkring(dev, rinfo);
1794 if (err)
1795 goto destroy_blkring;
1796 }
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001797
1798again:
1799 err = xenbus_transaction_start(&xbt);
1800 if (err) {
1801 xenbus_dev_fatal(dev, err, "starting transaction");
1802 goto destroy_blkring;
1803 }
1804
Bob Liu28d949b2015-11-14 11:12:14 +08001805 if (info->nr_ring_pages > 1) {
1806 err = xenbus_printf(xbt, dev->nodename, "ring-page-order", "%u",
1807 ring_page_order);
Bob Liu3df0e502015-11-14 11:12:12 +08001808 if (err) {
Bob Liu28d949b2015-11-14 11:12:14 +08001809 message = "writing ring-page-order";
Bob Liu3df0e502015-11-14 11:12:12 +08001810 goto abort_transaction;
1811 }
Bob Liu28d949b2015-11-14 11:12:14 +08001812 }
1813
1814 /* We already got the number of queues/rings in _probe */
1815 if (info->nr_rings == 1) {
1816 err = write_per_ring_nodes(xbt, &info->rinfo[0], dev->nodename);
1817 if (err)
1818 goto destroy_blkring;
Bob Liu3df0e502015-11-14 11:12:12 +08001819 } else {
Bob Liu28d949b2015-11-14 11:12:14 +08001820 char *path;
1821 size_t pathsize;
1822
1823 err = xenbus_printf(xbt, dev->nodename, "multi-queue-num-queues", "%u",
1824 info->nr_rings);
1825 if (err) {
1826 message = "writing multi-queue-num-queues";
1827 goto abort_transaction;
1828 }
1829
1830 pathsize = strlen(dev->nodename) + QUEUE_NAME_LEN;
1831 path = kmalloc(pathsize, GFP_KERNEL);
1832 if (!path) {
1833 err = -ENOMEM;
1834 message = "ENOMEM while writing ring references";
1835 goto abort_transaction;
1836 }
1837
1838 for (i = 0; i < info->nr_rings; i++) {
1839 memset(path, 0, pathsize);
1840 snprintf(path, pathsize, "%s/queue-%u", dev->nodename, i);
1841 err = write_per_ring_nodes(xbt, &info->rinfo[i], path);
1842 if (err) {
1843 kfree(path);
1844 goto destroy_blkring;
1845 }
1846 }
1847 kfree(path);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001848 }
Markus Armbruster3e334232008-04-02 10:54:02 -07001849 err = xenbus_printf(xbt, dev->nodename, "protocol", "%s",
1850 XEN_IO_PROTO_ABI_NATIVE);
1851 if (err) {
1852 message = "writing protocol";
1853 goto abort_transaction;
1854 }
Roger Pau Monne0a8704a2012-10-24 18:58:45 +02001855 err = xenbus_printf(xbt, dev->nodename,
Roger Pau Monnecb5bd4d2012-11-02 16:43:04 +01001856 "feature-persistent", "%u", 1);
Roger Pau Monne0a8704a2012-10-24 18:58:45 +02001857 if (err)
1858 dev_warn(&dev->dev,
1859 "writing persistent grants feature to xenbus");
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001860
1861 err = xenbus_transaction_end(xbt, 0);
1862 if (err) {
1863 if (err == -EAGAIN)
1864 goto again;
1865 xenbus_dev_fatal(dev, err, "completing transaction");
1866 goto destroy_blkring;
1867 }
1868
Bob Liu3df0e502015-11-14 11:12:12 +08001869 for (i = 0; i < info->nr_rings; i++) {
1870 unsigned int j;
Bob Liu28d949b2015-11-14 11:12:14 +08001871 struct blkfront_ring_info *rinfo = &info->rinfo[i];
Bob Liu3df0e502015-11-14 11:12:12 +08001872
1873 for (j = 0; j < BLK_RING_SIZE(info); j++)
1874 rinfo->shadow[j].req.u.rw.id = j + 1;
1875 rinfo->shadow[BLK_RING_SIZE(info)-1].req.u.rw.id = 0x0fffffff;
1876 }
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001877 xenbus_switch_state(dev, XenbusStateInitialised);
1878
1879 return 0;
1880
1881 abort_transaction:
1882 xenbus_transaction_end(xbt, 1);
1883 if (message)
1884 xenbus_dev_fatal(dev, err, "%s", message);
1885 destroy_blkring:
1886 blkif_free(info, 0);
Bob Liu3df0e502015-11-14 11:12:12 +08001887
Konrad Rzeszutek Wilkc31ecf62015-12-18 16:28:53 -05001888 kfree(info);
1889 dev_set_drvdata(&dev->dev, NULL);
1890
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001891 return err;
1892}
1893
Bob Liu3db70a82015-11-25 17:52:55 -05001894static int negotiate_mq(struct blkfront_info *info)
1895{
Juergen Grossf27dc1a2016-10-31 14:58:40 +01001896 unsigned int backend_max_queues;
Bob Liu3db70a82015-11-25 17:52:55 -05001897 unsigned int i;
1898
1899 BUG_ON(info->nr_rings);
1900
1901 /* Check if backend supports multiple queues. */
Juergen Grossf27dc1a2016-10-31 14:58:40 +01001902 backend_max_queues = xenbus_read_unsigned(info->xbdev->otherend,
1903 "multi-queue-max-queues", 1);
Bob Liu3db70a82015-11-25 17:52:55 -05001904 info->nr_rings = min(backend_max_queues, xen_blkif_max_queues);
1905 /* We need at least one ring. */
1906 if (!info->nr_rings)
1907 info->nr_rings = 1;
1908
Kees Cook6396bb22018-06-12 14:03:40 -07001909 info->rinfo = kcalloc(info->nr_rings,
1910 sizeof(struct blkfront_ring_info),
1911 GFP_KERNEL);
Bob Liu3db70a82015-11-25 17:52:55 -05001912 if (!info->rinfo) {
1913 xenbus_dev_fatal(info->xbdev, -ENOMEM, "allocating ring_info structure");
1914 return -ENOMEM;
1915 }
1916
1917 for (i = 0; i < info->nr_rings; i++) {
1918 struct blkfront_ring_info *rinfo;
1919
1920 rinfo = &info->rinfo[i];
1921 INIT_LIST_HEAD(&rinfo->indirect_pages);
1922 INIT_LIST_HEAD(&rinfo->grants);
1923 rinfo->dev_info = info;
1924 INIT_WORK(&rinfo->work, blkif_restart_queue);
1925 spin_lock_init(&rinfo->ring_lock);
1926 }
1927 return 0;
1928}
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001929/**
1930 * Entry point to this code when a new device is created. Allocate the basic
1931 * structures and the ring buffer for communication with the backend, and
1932 * inform the backend of the appropriate details for those. Switch to
1933 * Initialised state.
1934 */
1935static int blkfront_probe(struct xenbus_device *dev,
1936 const struct xenbus_device_id *id)
1937{
Bob Liu86839c52015-06-03 13:40:03 +08001938 int err, vdevice;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001939 struct blkfront_info *info;
1940
1941 /* FIXME: Use dynamic device id if this is not set. */
1942 err = xenbus_scanf(XBT_NIL, dev->nodename,
1943 "virtual-device", "%i", &vdevice);
1944 if (err != 1) {
Chris Lalancette9246b5f2008-09-17 14:30:32 -07001945 /* go looking in the extended area instead */
1946 err = xenbus_scanf(XBT_NIL, dev->nodename, "virtual-device-ext",
1947 "%i", &vdevice);
1948 if (err != 1) {
1949 xenbus_dev_fatal(dev, err, "reading virtual-device");
1950 return err;
1951 }
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001952 }
1953
Stefano Stabellinib98a4092010-07-29 14:53:16 +01001954 if (xen_hvm_domain()) {
1955 char *type;
1956 int len;
1957 /* no unplug has been done: do not hook devices != xen vbds */
Konrad Rzeszutek Wilk51c71a32013-11-26 15:05:40 -05001958 if (xen_has_pv_and_legacy_disk_devices()) {
Stefano Stabellinib98a4092010-07-29 14:53:16 +01001959 int major;
Stefano Stabellinic1c54132010-05-14 12:44:30 +01001960
Stefano Stabellinib98a4092010-07-29 14:53:16 +01001961 if (!VDEV_IS_EXTENDED(vdevice))
1962 major = BLKIF_MAJOR(vdevice);
1963 else
1964 major = XENVBD_MAJOR;
Stefano Stabellinic1c54132010-05-14 12:44:30 +01001965
Stefano Stabellinib98a4092010-07-29 14:53:16 +01001966 if (major != XENVBD_MAJOR) {
1967 printk(KERN_INFO
1968 "%s: HVM does not support vbd %d as xen block device\n",
Rasmus Villemoes02f1f212015-02-12 15:01:31 -08001969 __func__, vdevice);
Stefano Stabellinib98a4092010-07-29 14:53:16 +01001970 return -ENODEV;
1971 }
1972 }
1973 /* do not create a PV cdrom device if we are an HVM guest */
1974 type = xenbus_read(XBT_NIL, dev->nodename, "device-type", &len);
1975 if (IS_ERR(type))
1976 return -ENODEV;
1977 if (strncmp(type, "cdrom", 5) == 0) {
1978 kfree(type);
Stefano Stabellinic1c54132010-05-14 12:44:30 +01001979 return -ENODEV;
1980 }
Stefano Stabellinib98a4092010-07-29 14:53:16 +01001981 kfree(type);
Stefano Stabellinic1c54132010-05-14 12:44:30 +01001982 }
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001983 info = kzalloc(sizeof(*info), GFP_KERNEL);
1984 if (!info) {
1985 xenbus_dev_fatal(dev, -ENOMEM, "allocating info structure");
1986 return -ENOMEM;
1987 }
1988
Bob Liu28d949b2015-11-14 11:12:14 +08001989 info->xbdev = dev;
Bob Liu81f35162015-11-14 11:12:11 +08001990
Daniel Stoddenb70f5fa2010-04-30 22:01:19 +00001991 mutex_init(&info->mutex);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001992 info->vdevice = vdevice;
1993 info->connected = BLKIF_STATE_DISCONNECTED;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001994
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001995 /* Front end dir is a number, which is used as the id. */
1996 info->handle = simple_strtoul(strrchr(dev->nodename, '/')+1, NULL, 0);
Greg Kroah-Hartmana1b4b122009-04-30 14:43:31 -07001997 dev_set_drvdata(&dev->dev, info);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001998
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001999 return 0;
2000}
2001
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07002002static int blkif_recover(struct blkfront_info *info)
2003{
NeilBrown4559fa52017-06-18 14:38:59 +10002004 unsigned int r_index;
Roger Pau Monne402b27f2013-04-18 16:06:54 +02002005 struct request *req, *n;
Roger Pau Monne402b27f2013-04-18 16:06:54 +02002006 int rc;
NeilBrown4559fa52017-06-18 14:38:59 +10002007 struct bio *bio;
2008 unsigned int segs;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07002009
Bob Liu3df0e502015-11-14 11:12:12 +08002010 blkfront_gather_backend_features(info);
Bob Liu172335a2016-07-01 17:43:39 -04002011 /* Reset limits changed by blk_mq_update_nr_hw_queues(). */
2012 blkif_set_queue_limits(info);
Roger Pau Monne402b27f2013-04-18 16:06:54 +02002013 segs = info->max_indirect_segments ? : BLKIF_MAX_SEGMENTS_PER_REQUEST;
Bob Liu6c647b02016-07-01 15:45:57 -04002014 blk_queue_max_segments(info->rq, segs / GRANTS_PER_PSEG);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07002015
Bob Liu3df0e502015-11-14 11:12:12 +08002016 for (r_index = 0; r_index < info->nr_rings; r_index++) {
Bob Liu7b427a52016-06-27 16:33:24 +08002017 struct blkfront_ring_info *rinfo = &info->rinfo[r_index];
Bob Liu3df0e502015-11-14 11:12:12 +08002018
2019 rc = blkfront_setup_indirect(rinfo);
Bob Liu7b427a52016-06-27 16:33:24 +08002020 if (rc)
Bob Liu3df0e502015-11-14 11:12:12 +08002021 return rc;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07002022 }
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07002023 xenbus_switch_state(info->xbdev, XenbusStateConnected);
2024
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07002025 /* Now safe for us to use the shared ring */
2026 info->connected = BLKIF_STATE_CONNECTED;
2027
Bob Liu3df0e502015-11-14 11:12:12 +08002028 for (r_index = 0; r_index < info->nr_rings; r_index++) {
2029 struct blkfront_ring_info *rinfo;
2030
2031 rinfo = &info->rinfo[r_index];
2032 /* Kick any other new requests queued since we resumed */
2033 kick_pending_request_queues(rinfo);
2034 }
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07002035
Bob Liu7b427a52016-06-27 16:33:24 +08002036 list_for_each_entry_safe(req, n, &info->requests, queuelist) {
Roger Pau Monne402b27f2013-04-18 16:06:54 +02002037 /* Requeue pending requests (flush or discard) */
2038 list_del_init(&req->queuelist);
2039 BUG_ON(req->nr_phys_segments > segs);
Bart Van Assche2b053ac2016-10-28 17:21:41 -07002040 blk_mq_requeue_request(req, false);
Roger Pau Monne402b27f2013-04-18 16:06:54 +02002041 }
Bart Van Assche52d7f1b2016-10-28 17:20:32 -07002042 blk_mq_start_stopped_hw_queues(info->rq, true);
Bob Liu907c3eb2015-07-13 17:55:24 +08002043 blk_mq_kick_requeue_list(info->rq);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07002044
Bob Liu7b427a52016-06-27 16:33:24 +08002045 while ((bio = bio_list_pop(&info->bio_list)) != NULL) {
Roger Pau Monne402b27f2013-04-18 16:06:54 +02002046 /* Traverse the list of pending bios and re-queue them */
Mike Christie4e49ea42016-06-05 14:31:41 -05002047 submit_bio(bio);
Roger Pau Monne402b27f2013-04-18 16:06:54 +02002048 }
2049
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07002050 return 0;
2051}
2052
2053/**
2054 * We are reconnecting to the backend, due to a suspend/resume, or a backend
2055 * driver restart. We tear down our blkif structure and recreate it, but
2056 * leave the device-layer structures intact so that this is transparent to the
2057 * rest of the kernel.
2058 */
2059static int blkfront_resume(struct xenbus_device *dev)
2060{
Greg Kroah-Hartmana1b4b122009-04-30 14:43:31 -07002061 struct blkfront_info *info = dev_get_drvdata(&dev->dev);
Bob Liu3db70a82015-11-25 17:52:55 -05002062 int err = 0;
Bob Liu7b427a52016-06-27 16:33:24 +08002063 unsigned int i, j;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07002064
2065 dev_dbg(&dev->dev, "blkfront_resume: %s\n", dev->nodename);
2066
Bob Liu7b427a52016-06-27 16:33:24 +08002067 bio_list_init(&info->bio_list);
2068 INIT_LIST_HEAD(&info->requests);
2069 for (i = 0; i < info->nr_rings; i++) {
2070 struct blkfront_ring_info *rinfo = &info->rinfo[i];
2071 struct bio_list merge_bio;
2072 struct blk_shadow *shadow = rinfo->shadow;
2073
2074 for (j = 0; j < BLK_RING_SIZE(info); j++) {
2075 /* Not in use? */
2076 if (!shadow[j].request)
2077 continue;
2078
2079 /*
2080 * Get the bios in the request so we can re-queue them.
2081 */
Munehisa Kamatab15bd8c2017-08-09 15:31:40 -07002082 if (req_op(shadow[j].request) == REQ_OP_FLUSH ||
2083 req_op(shadow[j].request) == REQ_OP_DISCARD ||
2084 req_op(shadow[j].request) == REQ_OP_SECURE_ERASE ||
Linus Torvalds3fc9d692016-07-26 15:37:51 -07002085 shadow[j].request->cmd_flags & REQ_FUA) {
Bob Liu7b427a52016-06-27 16:33:24 +08002086 /*
2087 * Flush operations don't contain bios, so
2088 * we need to requeue the whole request
Linus Torvalds3fc9d692016-07-26 15:37:51 -07002089 *
2090 * XXX: but this doesn't make any sense for a
2091 * write with the FUA flag set..
Bob Liu7b427a52016-06-27 16:33:24 +08002092 */
2093 list_add(&shadow[j].request->queuelist, &info->requests);
2094 continue;
2095 }
2096 merge_bio.head = shadow[j].request->bio;
2097 merge_bio.tail = shadow[j].request->biotail;
2098 bio_list_merge(&info->bio_list, &merge_bio);
2099 shadow[j].request->bio = NULL;
Christoph Hellwig2a842ac2017-06-03 09:38:04 +02002100 blk_mq_end_request(shadow[j].request, BLK_STS_OK);
Bob Liu7b427a52016-06-27 16:33:24 +08002101 }
2102 }
2103
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07002104 blkif_free(info, info->connected == BLKIF_STATE_CONNECTED);
2105
Ian Campbell203fd612009-12-04 15:33:54 +00002106 err = talk_to_blkback(dev, info);
Bob Liu2a6f71a2016-05-31 16:59:17 +08002107 if (!err)
2108 blk_mq_update_nr_hw_queues(&info->tag_set, info->nr_rings);
Roger Pau Monne402b27f2013-04-18 16:06:54 +02002109
2110 /*
2111 * We have to wait for the backend to switch to
2112 * connected state, since we want to read which
2113 * features it supports.
2114 */
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07002115
2116 return err;
2117}
2118
Konrad Rzeszutek Wilk6f03a7f2015-11-16 15:14:41 -05002119static void blkfront_closing(struct blkfront_info *info)
Daniel Stoddenb70f5fa2010-04-30 22:01:19 +00002120{
2121 struct xenbus_device *xbdev = info->xbdev;
2122 struct block_device *bdev = NULL;
2123
2124 mutex_lock(&info->mutex);
2125
2126 if (xbdev->state == XenbusStateClosing) {
2127 mutex_unlock(&info->mutex);
2128 return;
2129 }
2130
2131 if (info->gd)
2132 bdev = bdget_disk(info->gd, 0);
2133
2134 mutex_unlock(&info->mutex);
2135
2136 if (!bdev) {
2137 xenbus_frontend_closed(xbdev);
2138 return;
2139 }
2140
2141 mutex_lock(&bdev->bd_mutex);
2142
Daniel Stodden7b32d102010-04-30 22:01:23 +00002143 if (bdev->bd_openers) {
Daniel Stoddenb70f5fa2010-04-30 22:01:19 +00002144 xenbus_dev_error(xbdev, -EBUSY,
2145 "Device in use; refusing to close");
2146 xenbus_switch_state(xbdev, XenbusStateClosing);
2147 } else {
2148 xlvbd_release_gendisk(info);
2149 xenbus_frontend_closed(xbdev);
2150 }
2151
2152 mutex_unlock(&bdev->bd_mutex);
2153 bdput(bdev);
2154}
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07002155
Li Dongyanged30bf32011-09-01 18:39:09 +08002156static void blkfront_setup_discard(struct blkfront_info *info)
2157{
2158 int err;
Li Dongyanged30bf32011-09-01 18:39:09 +08002159 unsigned int discard_granularity;
2160 unsigned int discard_alignment;
2161
Olaf Hering1c8cad62014-05-21 16:32:40 +02002162 info->feature_discard = 1;
2163 err = xenbus_gather(XBT_NIL, info->xbdev->otherend,
2164 "discard-granularity", "%u", &discard_granularity,
2165 "discard-alignment", "%u", &discard_alignment,
2166 NULL);
2167 if (!err) {
2168 info->discard_granularity = discard_granularity;
2169 info->discard_alignment = discard_alignment;
2170 }
Juergen Grossf27dc1a2016-10-31 14:58:40 +01002171 info->feature_secdiscard =
2172 !!xenbus_read_unsigned(info->xbdev->otherend, "discard-secure",
2173 0);
Li Dongyanged30bf32011-09-01 18:39:09 +08002174}
2175
Bob Liu81f35162015-11-14 11:12:11 +08002176static int blkfront_setup_indirect(struct blkfront_ring_info *rinfo)
Roger Pau Monne402b27f2013-04-18 16:06:54 +02002177{
Julien Grallc004a6f2015-07-22 16:44:54 +01002178 unsigned int psegs, grants;
Roger Pau Monne402b27f2013-04-18 16:06:54 +02002179 int err, i;
Bob Liu81f35162015-11-14 11:12:11 +08002180 struct blkfront_info *info = rinfo->dev_info;
Roger Pau Monne402b27f2013-04-18 16:06:54 +02002181
Julien Grall6cc568332015-08-13 13:13:35 +01002182 if (info->max_indirect_segments == 0) {
2183 if (!HAS_EXTRA_REQ)
2184 grants = BLKIF_MAX_SEGMENTS_PER_REQUEST;
2185 else {
2186 /*
2187 * When an extra req is required, the maximum
2188 * grants supported is related to the size of the
2189 * Linux block segment.
2190 */
2191 grants = GRANTS_PER_PSEG;
2192 }
2193 }
Bob Liud50babb2015-07-22 14:40:08 +08002194 else
Julien Grallc004a6f2015-07-22 16:44:54 +01002195 grants = info->max_indirect_segments;
Jan Beulich3b4f1882017-01-23 08:11:37 -07002196 psegs = DIV_ROUND_UP(grants, GRANTS_PER_PSEG);
Roger Pau Monne402b27f2013-04-18 16:06:54 +02002197
Bob Liu81f35162015-11-14 11:12:11 +08002198 err = fill_grant_buffer(rinfo,
Julien Grallc004a6f2015-07-22 16:44:54 +01002199 (grants + INDIRECT_GREFS(grants)) * BLK_RING_SIZE(info));
Roger Pau Monne402b27f2013-04-18 16:06:54 +02002200 if (err)
2201 goto out_of_memory;
2202
Roger Pau Monnebfe11d62013-10-29 18:31:14 +01002203 if (!info->feature_persistent && info->max_indirect_segments) {
2204 /*
2205 * We are using indirect descriptors but not persistent
2206 * grants, we need to allocate a set of pages that can be
2207 * used for mapping indirect grefs
2208 */
Julien Grallc004a6f2015-07-22 16:44:54 +01002209 int num = INDIRECT_GREFS(grants) * BLK_RING_SIZE(info);
Roger Pau Monnebfe11d62013-10-29 18:31:14 +01002210
Bob Liu81f35162015-11-14 11:12:11 +08002211 BUG_ON(!list_empty(&rinfo->indirect_pages));
Roger Pau Monnebfe11d62013-10-29 18:31:14 +01002212 for (i = 0; i < num; i++) {
2213 struct page *indirect_page = alloc_page(GFP_NOIO);
2214 if (!indirect_page)
2215 goto out_of_memory;
Bob Liu81f35162015-11-14 11:12:11 +08002216 list_add(&indirect_page->lru, &rinfo->indirect_pages);
Roger Pau Monnebfe11d62013-10-29 18:31:14 +01002217 }
2218 }
2219
Bob Liu86839c52015-06-03 13:40:03 +08002220 for (i = 0; i < BLK_RING_SIZE(info); i++) {
Kees Cook6396bb22018-06-12 14:03:40 -07002221 rinfo->shadow[i].grants_used =
2222 kcalloc(grants,
2223 sizeof(rinfo->shadow[i].grants_used[0]),
Roger Pau Monne402b27f2013-04-18 16:06:54 +02002224 GFP_NOIO);
Kees Cook6396bb22018-06-12 14:03:40 -07002225 rinfo->shadow[i].sg = kcalloc(psegs,
2226 sizeof(rinfo->shadow[i].sg[0]),
2227 GFP_NOIO);
2228 if (info->max_indirect_segments)
2229 rinfo->shadow[i].indirect_grants =
2230 kcalloc(INDIRECT_GREFS(grants),
2231 sizeof(rinfo->shadow[i].indirect_grants[0]),
2232 GFP_NOIO);
Bob Liu81f35162015-11-14 11:12:11 +08002233 if ((rinfo->shadow[i].grants_used == NULL) ||
2234 (rinfo->shadow[i].sg == NULL) ||
Roger Pau Monne402b27f2013-04-18 16:06:54 +02002235 (info->max_indirect_segments &&
Bob Liu81f35162015-11-14 11:12:11 +08002236 (rinfo->shadow[i].indirect_grants == NULL)))
Roger Pau Monne402b27f2013-04-18 16:06:54 +02002237 goto out_of_memory;
Bob Liu81f35162015-11-14 11:12:11 +08002238 sg_init_table(rinfo->shadow[i].sg, psegs);
Roger Pau Monne402b27f2013-04-18 16:06:54 +02002239 }
2240
2241
2242 return 0;
2243
2244out_of_memory:
Bob Liu86839c52015-06-03 13:40:03 +08002245 for (i = 0; i < BLK_RING_SIZE(info); i++) {
Bob Liu81f35162015-11-14 11:12:11 +08002246 kfree(rinfo->shadow[i].grants_used);
2247 rinfo->shadow[i].grants_used = NULL;
2248 kfree(rinfo->shadow[i].sg);
2249 rinfo->shadow[i].sg = NULL;
2250 kfree(rinfo->shadow[i].indirect_grants);
2251 rinfo->shadow[i].indirect_grants = NULL;
Roger Pau Monne402b27f2013-04-18 16:06:54 +02002252 }
Bob Liu81f35162015-11-14 11:12:11 +08002253 if (!list_empty(&rinfo->indirect_pages)) {
Roger Pau Monnebfe11d62013-10-29 18:31:14 +01002254 struct page *indirect_page, *n;
Bob Liu81f35162015-11-14 11:12:11 +08002255 list_for_each_entry_safe(indirect_page, n, &rinfo->indirect_pages, lru) {
Roger Pau Monnebfe11d62013-10-29 18:31:14 +01002256 list_del(&indirect_page->lru);
2257 __free_page(indirect_page);
2258 }
2259 }
Roger Pau Monne402b27f2013-04-18 16:06:54 +02002260 return -ENOMEM;
2261}
2262
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07002263/*
Bob Liud50babb2015-07-22 14:40:08 +08002264 * Gather all backend feature-*
2265 */
Bob Liu3df0e502015-11-14 11:12:12 +08002266static void blkfront_gather_backend_features(struct blkfront_info *info)
Bob Liud50babb2015-07-22 14:40:08 +08002267{
Bob Liud50babb2015-07-22 14:40:08 +08002268 unsigned int indirect_segments;
2269
2270 info->feature_flush = 0;
Mike Christiea4180902016-06-05 14:32:24 -05002271 info->feature_fua = 0;
Bob Liud50babb2015-07-22 14:40:08 +08002272
Bob Liud50babb2015-07-22 14:40:08 +08002273 /*
2274 * If there's no "feature-barrier" defined, then it means
2275 * we're dealing with a very old backend which writes
2276 * synchronously; nothing to do.
2277 *
2278 * If there are barriers, then we use flush.
2279 */
Juergen Grossf27dc1a2016-10-31 14:58:40 +01002280 if (xenbus_read_unsigned(info->xbdev->otherend, "feature-barrier", 0)) {
Mike Christiea4180902016-06-05 14:32:24 -05002281 info->feature_flush = 1;
2282 info->feature_fua = 1;
2283 }
2284
Bob Liud50babb2015-07-22 14:40:08 +08002285 /*
2286 * And if there is "feature-flush-cache" use that above
2287 * barriers.
2288 */
Juergen Grossf27dc1a2016-10-31 14:58:40 +01002289 if (xenbus_read_unsigned(info->xbdev->otherend, "feature-flush-cache",
2290 0)) {
Mike Christiea4180902016-06-05 14:32:24 -05002291 info->feature_flush = 1;
2292 info->feature_fua = 0;
2293 }
Bob Liud50babb2015-07-22 14:40:08 +08002294
Juergen Grossf27dc1a2016-10-31 14:58:40 +01002295 if (xenbus_read_unsigned(info->xbdev->otherend, "feature-discard", 0))
Bob Liud50babb2015-07-22 14:40:08 +08002296 blkfront_setup_discard(info);
2297
Juergen Grossf27dc1a2016-10-31 14:58:40 +01002298 info->feature_persistent =
Jan Beulichb32728f2017-01-23 08:12:19 -07002299 !!xenbus_read_unsigned(info->xbdev->otherend,
2300 "feature-persistent", 0);
Bob Liud50babb2015-07-22 14:40:08 +08002301
Juergen Grossf27dc1a2016-10-31 14:58:40 +01002302 indirect_segments = xenbus_read_unsigned(info->xbdev->otherend,
2303 "feature-max-indirect-segments", 0);
Jan Beulich3b4f1882017-01-23 08:11:37 -07002304 if (indirect_segments > xen_blkif_max_segments)
2305 indirect_segments = xen_blkif_max_segments;
2306 if (indirect_segments <= BLKIF_MAX_SEGMENTS_PER_REQUEST)
2307 indirect_segments = 0;
2308 info->max_indirect_segments = indirect_segments;
Bob Liud50babb2015-07-22 14:40:08 +08002309}
2310
2311/*
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07002312 * Invoked when the backend is finally 'ready' (and has told produced
2313 * the details about the physical device - #sectors, size, etc).
2314 */
2315static void blkfront_connect(struct blkfront_info *info)
2316{
2317 unsigned long long sectors;
2318 unsigned long sector_size;
Stefan Bader7c4d7d72013-05-13 16:28:15 +02002319 unsigned int physical_sector_size;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07002320 unsigned int binfo;
Marc Olson89515d02017-04-11 12:24:09 -07002321 char *envp[] = { "RESIZE=1", NULL };
Bob Liu3df0e502015-11-14 11:12:12 +08002322 int err, i;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07002323
K. Y. Srinivasan1fa73be2010-03-11 13:42:26 -08002324 switch (info->connected) {
2325 case BLKIF_STATE_CONNECTED:
2326 /*
2327 * Potentially, the back-end may be signalling
2328 * a capacity change; update the capacity.
2329 */
2330 err = xenbus_scanf(XBT_NIL, info->xbdev->otherend,
2331 "sectors", "%Lu", &sectors);
2332 if (XENBUS_EXIST_ERR(err))
2333 return;
2334 printk(KERN_INFO "Setting capacity to %Lu\n",
2335 sectors);
2336 set_capacity(info->gd, sectors);
K. Y. Srinivasan2def1412010-03-18 15:00:54 -07002337 revalidate_disk(info->gd);
Marc Olson89515d02017-04-11 12:24:09 -07002338 kobject_uevent_env(&disk_to_dev(info->gd)->kobj,
2339 KOBJ_CHANGE, envp);
K. Y. Srinivasan1fa73be2010-03-11 13:42:26 -08002340
Roger Pau Monne402b27f2013-04-18 16:06:54 +02002341 return;
K. Y. Srinivasan1fa73be2010-03-11 13:42:26 -08002342 case BLKIF_STATE_SUSPENDED:
Roger Pau Monne402b27f2013-04-18 16:06:54 +02002343 /*
2344 * If we are recovering from suspension, we need to wait
2345 * for the backend to announce it's features before
2346 * reconnecting, at least we need to know if the backend
2347 * supports indirect descriptors, and how many.
2348 */
2349 blkif_recover(info);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07002350 return;
2351
Jeremy Fitzhardingeb4dddb42010-03-11 15:10:40 -08002352 default:
2353 break;
K. Y. Srinivasan1fa73be2010-03-11 13:42:26 -08002354 }
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07002355
2356 dev_dbg(&info->xbdev->dev, "%s:%s.\n",
2357 __func__, info->xbdev->otherend);
2358
2359 err = xenbus_gather(XBT_NIL, info->xbdev->otherend,
2360 "sectors", "%llu", &sectors,
2361 "info", "%u", &binfo,
2362 "sector-size", "%lu", &sector_size,
2363 NULL);
2364 if (err) {
2365 xenbus_dev_fatal(info->xbdev, err,
2366 "reading backend fields at %s",
2367 info->xbdev->otherend);
2368 return;
2369 }
2370
Stefan Bader7c4d7d72013-05-13 16:28:15 +02002371 /*
2372 * physcial-sector-size is a newer field, so old backends may not
2373 * provide this. Assume physical sector size to be the same as
2374 * sector_size in that case.
2375 */
Juergen Grossf27dc1a2016-10-31 14:58:40 +01002376 physical_sector_size = xenbus_read_unsigned(info->xbdev->otherend,
2377 "physical-sector-size",
2378 sector_size);
Bob Liu3df0e502015-11-14 11:12:12 +08002379 blkfront_gather_backend_features(info);
2380 for (i = 0; i < info->nr_rings; i++) {
2381 err = blkfront_setup_indirect(&info->rinfo[i]);
2382 if (err) {
2383 xenbus_dev_fatal(info->xbdev, err, "setup_indirect at %s",
2384 info->xbdev->otherend);
2385 blkif_free(info, 0);
2386 break;
2387 }
Roger Pau Monne402b27f2013-04-18 16:06:54 +02002388 }
2389
Stefan Bader7c4d7d72013-05-13 16:28:15 +02002390 err = xlvbd_alloc_gendisk(sectors, info, binfo, sector_size,
2391 physical_sector_size);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07002392 if (err) {
2393 xenbus_dev_fatal(info->xbdev, err, "xlvbd_add at %s",
2394 info->xbdev->otherend);
Bob Liu4e876c22016-07-27 17:42:04 +08002395 goto fail;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07002396 }
2397
2398 xenbus_switch_state(info->xbdev, XenbusStateConnected);
2399
2400 /* Kick pending requests. */
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07002401 info->connected = BLKIF_STATE_CONNECTED;
Bob Liu3df0e502015-11-14 11:12:12 +08002402 for (i = 0; i < info->nr_rings; i++)
2403 kick_pending_request_queues(&info->rinfo[i]);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07002404
Dan Williams0d52c7562016-06-15 19:44:20 -07002405 device_add_disk(&info->xbdev->dev, info->gd);
Christian Limpach1d78d702008-04-02 10:54:04 -07002406
2407 info->is_ready = 1;
Bob Liu4e876c22016-07-27 17:42:04 +08002408 return;
2409
2410fail:
2411 blkif_free(info, 0);
2412 return;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07002413}
2414
2415/**
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07002416 * Callback received when the backend's state changes.
2417 */
Ian Campbell203fd612009-12-04 15:33:54 +00002418static void blkback_changed(struct xenbus_device *dev,
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07002419 enum xenbus_state backend_state)
2420{
Greg Kroah-Hartmana1b4b122009-04-30 14:43:31 -07002421 struct blkfront_info *info = dev_get_drvdata(&dev->dev);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07002422
Ian Campbell203fd612009-12-04 15:33:54 +00002423 dev_dbg(&dev->dev, "blkfront:blkback_changed to state %d.\n", backend_state);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07002424
2425 switch (backend_state) {
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07002426 case XenbusStateInitWait:
Bob Liua9b54bb2015-06-19 00:23:00 -04002427 if (dev->state != XenbusStateInitialising)
2428 break;
Konrad Rzeszutek Wilkc31ecf62015-12-18 16:28:53 -05002429 if (talk_to_blkback(dev, info))
Bob Liu8ab01442015-06-03 13:40:02 +08002430 break;
Bob Liu8ab01442015-06-03 13:40:02 +08002431 case XenbusStateInitialising:
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07002432 case XenbusStateInitialised:
Noboru Iwamatsub78c9512009-10-13 17:22:29 -04002433 case XenbusStateReconfiguring:
2434 case XenbusStateReconfigured:
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07002435 case XenbusStateUnknown:
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07002436 break;
2437
2438 case XenbusStateConnected:
Bob Liuefd15352016-06-07 10:43:15 -04002439 /*
2440 * talk_to_blkback sets state to XenbusStateInitialised
2441 * and blkfront_connect sets it to XenbusStateConnected
2442 * (if connection went OK).
2443 *
2444 * If the backend (or toolstack) decides to poke at backend
2445 * state (and re-trigger the watch by setting the state repeatedly
2446 * to XenbusStateConnected (4)) we need to deal with this.
2447 * This is allowed as this is used to communicate to the guest
2448 * that the size of disk has changed!
2449 */
2450 if ((dev->state != XenbusStateInitialised) &&
2451 (dev->state != XenbusStateConnected)) {
Konrad Rzeszutek Wilkc31ecf62015-12-18 16:28:53 -05002452 if (talk_to_blkback(dev, info))
2453 break;
2454 }
Bob Liuefd15352016-06-07 10:43:15 -04002455
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07002456 blkfront_connect(info);
2457 break;
2458
David Vrabel36613712014-02-04 18:53:56 +00002459 case XenbusStateClosed:
2460 if (dev->state == XenbusStateClosed)
2461 break;
Bart Van Asscheccc22252017-08-17 16:23:11 -07002462 /* fall through */
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07002463 case XenbusStateClosing:
Cathy Averya54c8f02015-10-02 09:35:01 -04002464 if (info)
2465 blkfront_closing(info);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07002466 break;
2467 }
2468}
2469
Daniel Stoddenfa1bd352010-04-30 22:01:22 +00002470static int blkfront_remove(struct xenbus_device *xbdev)
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07002471{
Daniel Stoddenfa1bd352010-04-30 22:01:22 +00002472 struct blkfront_info *info = dev_get_drvdata(&xbdev->dev);
2473 struct block_device *bdev = NULL;
2474 struct gendisk *disk;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07002475
Daniel Stoddenfa1bd352010-04-30 22:01:22 +00002476 dev_dbg(&xbdev->dev, "%s removed", xbdev->nodename);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07002477
2478 blkif_free(info, 0);
2479
Daniel Stoddenfa1bd352010-04-30 22:01:22 +00002480 mutex_lock(&info->mutex);
2481
2482 disk = info->gd;
2483 if (disk)
2484 bdev = bdget_disk(disk, 0);
2485
2486 info->xbdev = NULL;
2487 mutex_unlock(&info->mutex);
2488
2489 if (!bdev) {
Jan Beulich0e345822010-08-07 18:28:55 +02002490 kfree(info);
Daniel Stoddenfa1bd352010-04-30 22:01:22 +00002491 return 0;
2492 }
2493
2494 /*
2495 * The xbdev was removed before we reached the Closed
2496 * state. See if it's safe to remove the disk. If the bdev
2497 * isn't closed yet, we let release take care of it.
2498 */
2499
2500 mutex_lock(&bdev->bd_mutex);
2501 info = disk->private_data;
2502
Daniel Stoddend54142c2010-08-07 18:51:21 +02002503 dev_warn(disk_to_dev(disk),
2504 "%s was hot-unplugged, %d stale handles\n",
2505 xbdev->nodename, bdev->bd_openers);
2506
Daniel Stodden7b32d102010-04-30 22:01:23 +00002507 if (info && !bdev->bd_openers) {
Daniel Stoddenfa1bd352010-04-30 22:01:22 +00002508 xlvbd_release_gendisk(info);
2509 disk->private_data = NULL;
2510 kfree(info);
2511 }
2512
2513 mutex_unlock(&bdev->bd_mutex);
2514 bdput(bdev);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07002515
2516 return 0;
2517}
2518
Christian Limpach1d78d702008-04-02 10:54:04 -07002519static int blkfront_is_ready(struct xenbus_device *dev)
2520{
Greg Kroah-Hartmana1b4b122009-04-30 14:43:31 -07002521 struct blkfront_info *info = dev_get_drvdata(&dev->dev);
Christian Limpach1d78d702008-04-02 10:54:04 -07002522
Jan Beulich5d7ed202010-08-07 18:31:12 +02002523 return info->is_ready && info->xbdev;
Christian Limpach1d78d702008-04-02 10:54:04 -07002524}
2525
Al Viroa63c8482008-03-02 10:23:47 -05002526static int blkif_open(struct block_device *bdev, fmode_t mode)
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07002527{
Daniel Stodden13961742010-08-07 18:36:53 +02002528 struct gendisk *disk = bdev->bd_disk;
2529 struct blkfront_info *info;
2530 int err = 0;
Arnd Bergmann6e9624b2010-08-07 18:25:34 +02002531
Arnd Bergmann2a48fc02010-06-02 14:28:52 +02002532 mutex_lock(&blkfront_mutex);
Arnd Bergmann6e9624b2010-08-07 18:25:34 +02002533
Daniel Stodden13961742010-08-07 18:36:53 +02002534 info = disk->private_data;
2535 if (!info) {
2536 /* xbdev gone */
2537 err = -ERESTARTSYS;
2538 goto out;
2539 }
2540
2541 mutex_lock(&info->mutex);
2542
2543 if (!info->gd)
2544 /* xbdev is closed */
2545 err = -ERESTARTSYS;
2546
2547 mutex_unlock(&info->mutex);
2548
Daniel Stodden13961742010-08-07 18:36:53 +02002549out:
Arnd Bergmann2a48fc02010-06-02 14:28:52 +02002550 mutex_unlock(&blkfront_mutex);
Daniel Stodden13961742010-08-07 18:36:53 +02002551 return err;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07002552}
2553
Al Virodb2a1442013-05-05 21:52:57 -04002554static void blkif_release(struct gendisk *disk, fmode_t mode)
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07002555{
Al Viroa63c8482008-03-02 10:23:47 -05002556 struct blkfront_info *info = disk->private_data;
Daniel Stodden7fd152f2010-08-07 18:45:12 +02002557 struct block_device *bdev;
2558 struct xenbus_device *xbdev;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07002559
Arnd Bergmann2a48fc02010-06-02 14:28:52 +02002560 mutex_lock(&blkfront_mutex);
Daniel Stodden7fd152f2010-08-07 18:45:12 +02002561
2562 bdev = bdget_disk(disk, 0);
Daniel Stodden7fd152f2010-08-07 18:45:12 +02002563
Felipe Pena2f089cb2013-11-09 13:36:09 -02002564 if (!bdev) {
2565 WARN(1, "Block device %s yanked out from us!\n", disk->disk_name);
2566 goto out_mutex;
2567 }
Daniel Stoddenacfca3c2010-08-07 18:47:26 +02002568 if (bdev->bd_openers)
2569 goto out;
2570
Daniel Stodden7fd152f2010-08-07 18:45:12 +02002571 /*
2572 * Check if we have been instructed to close. We will have
2573 * deferred this request, because the bdev was still open.
2574 */
2575
2576 mutex_lock(&info->mutex);
2577 xbdev = info->xbdev;
2578
2579 if (xbdev && xbdev->state == XenbusStateClosing) {
2580 /* pending switch to state closed */
Daniel Stoddend54142c2010-08-07 18:51:21 +02002581 dev_info(disk_to_dev(bdev->bd_disk), "releasing disk\n");
Daniel Stodden7fd152f2010-08-07 18:45:12 +02002582 xlvbd_release_gendisk(info);
2583 xenbus_frontend_closed(info->xbdev);
2584 }
2585
2586 mutex_unlock(&info->mutex);
2587
2588 if (!xbdev) {
2589 /* sudden device removal */
Daniel Stoddend54142c2010-08-07 18:51:21 +02002590 dev_info(disk_to_dev(bdev->bd_disk), "releasing disk\n");
Daniel Stodden7fd152f2010-08-07 18:45:12 +02002591 xlvbd_release_gendisk(info);
2592 disk->private_data = NULL;
2593 kfree(info);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07002594 }
Daniel Stodden7fd152f2010-08-07 18:45:12 +02002595
Jens Axboea4cc14e2010-08-08 21:50:05 -04002596out:
Andrew Jonesdad5cf62012-02-16 13:16:25 +01002597 bdput(bdev);
Felipe Pena2f089cb2013-11-09 13:36:09 -02002598out_mutex:
Arnd Bergmann2a48fc02010-06-02 14:28:52 +02002599 mutex_unlock(&blkfront_mutex);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07002600}
2601
Alexey Dobriyan83d5cde2009-09-21 17:01:13 -07002602static const struct block_device_operations xlvbd_block_fops =
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07002603{
2604 .owner = THIS_MODULE,
Al Viroa63c8482008-03-02 10:23:47 -05002605 .open = blkif_open,
2606 .release = blkif_release,
Ian Campbell597592d2008-02-21 13:03:45 -08002607 .getgeo = blkif_getgeo,
Arnd Bergmann8a6cfeb2010-07-08 10:18:46 +02002608 .ioctl = blkif_ioctl,
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07002609};
2610
2611
Márton Némethec9c42e2010-01-10 13:39:52 +01002612static const struct xenbus_device_id blkfront_ids[] = {
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07002613 { "vbd" },
2614 { "" }
2615};
2616
David Vrabel95afae42014-09-08 17:30:41 +01002617static struct xenbus_driver blkfront_driver = {
2618 .ids = blkfront_ids,
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07002619 .probe = blkfront_probe,
2620 .remove = blkfront_remove,
2621 .resume = blkfront_resume,
Ian Campbell203fd612009-12-04 15:33:54 +00002622 .otherend_changed = blkback_changed,
Christian Limpach1d78d702008-04-02 10:54:04 -07002623 .is_ready = blkfront_is_ready,
David Vrabel95afae42014-09-08 17:30:41 +01002624};
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07002625
2626static int __init xlblk_init(void)
2627{
Laszlo Ersek469738e2011-10-07 21:34:38 +02002628 int ret;
Bob Liu28d949b2015-11-14 11:12:14 +08002629 int nr_cpus = num_online_cpus();
Laszlo Ersek469738e2011-10-07 21:34:38 +02002630
Jeremy Fitzhardinge6e833582008-08-19 13:16:17 -07002631 if (!xen_domain())
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07002632 return -ENODEV;
2633
Jan Beulich3b4f1882017-01-23 08:11:37 -07002634 if (xen_blkif_max_segments < BLKIF_MAX_SEGMENTS_PER_REQUEST)
2635 xen_blkif_max_segments = BLKIF_MAX_SEGMENTS_PER_REQUEST;
2636
Julien Grall9cce2912015-10-13 17:50:11 +01002637 if (xen_blkif_max_ring_order > XENBUS_MAX_RING_GRANT_ORDER) {
Bob Liu86839c52015-06-03 13:40:03 +08002638 pr_info("Invalid max_ring_order (%d), will use default max: %d.\n",
Julien Grall9cce2912015-10-13 17:50:11 +01002639 xen_blkif_max_ring_order, XENBUS_MAX_RING_GRANT_ORDER);
Peng Fan45fc8262015-11-25 18:26:01 +08002640 xen_blkif_max_ring_order = XENBUS_MAX_RING_GRANT_ORDER;
Bob Liu86839c52015-06-03 13:40:03 +08002641 }
2642
Bob Liu28d949b2015-11-14 11:12:14 +08002643 if (xen_blkif_max_queues > nr_cpus) {
2644 pr_info("Invalid max_queues (%d), will use default max: %d.\n",
2645 xen_blkif_max_queues, nr_cpus);
2646 xen_blkif_max_queues = nr_cpus;
2647 }
2648
Konrad Rzeszutek Wilk51c71a32013-11-26 15:05:40 -05002649 if (!xen_has_pv_disk_devices())
Igor Mammedovb9136d22012-03-21 15:08:38 +01002650 return -ENODEV;
2651
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07002652 if (register_blkdev(XENVBD_MAJOR, DEV_NAME)) {
2653 printk(KERN_WARNING "xen_blk: can't get major %d with name %s\n",
2654 XENVBD_MAJOR, DEV_NAME);
2655 return -ENODEV;
2656 }
2657
Jan Beulich73db1442011-12-22 09:08:13 +00002658 ret = xenbus_register_frontend(&blkfront_driver);
Laszlo Ersek469738e2011-10-07 21:34:38 +02002659 if (ret) {
2660 unregister_blkdev(XENVBD_MAJOR, DEV_NAME);
2661 return ret;
2662 }
2663
2664 return 0;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07002665}
2666module_init(xlblk_init);
2667
2668
Jan Beulich5a60d0c2008-06-17 10:47:08 +02002669static void __exit xlblk_exit(void)
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07002670{
Jan Beulich86050672012-04-05 16:04:52 +01002671 xenbus_unregister_driver(&blkfront_driver);
2672 unregister_blkdev(XENVBD_MAJOR, DEV_NAME);
2673 kfree(minors);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07002674}
2675module_exit(xlblk_exit);
2676
2677MODULE_DESCRIPTION("Xen virtual block device frontend");
2678MODULE_LICENSE("GPL");
2679MODULE_ALIAS_BLOCKDEV_MAJOR(XENVBD_MAJOR);
Mark McLoughlind2f0c522008-04-02 10:54:05 -07002680MODULE_ALIAS("xen:vbd");
Mark McLoughlin4f93f09b2008-04-02 10:54:06 -07002681MODULE_ALIAS("xenblk");