blob: 8986adab9bf58540bd8d2ab0744d317fba6c23a0 [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
Julien Grallc004a6f2015-07-22 16:44:54 +0100254#define INDIRECT_GREFS(_grants) \
255 DIV_ROUND_UP(_grants, GRANTS_PER_INDIRECT_FRAME)
256
Bob Liu81f35162015-11-14 11:12:11 +0800257static int blkfront_setup_indirect(struct blkfront_ring_info *rinfo);
Bob Liu3df0e502015-11-14 11:12:12 +0800258static void blkfront_gather_backend_features(struct blkfront_info *info);
Bhavesh Davda7ed8ce12017-12-22 14:17:13 -0800259static int negotiate_mq(struct blkfront_info *info);
Roger Pau Monne402b27f2013-04-18 16:06:54 +0200260
Bob Liu81f35162015-11-14 11:12:11 +0800261static int get_id_from_freelist(struct blkfront_ring_info *rinfo)
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700262{
Bob Liu81f35162015-11-14 11:12:11 +0800263 unsigned long free = rinfo->shadow_free;
264
265 BUG_ON(free >= BLK_RING_SIZE(rinfo->dev_info));
266 rinfo->shadow_free = rinfo->shadow[free].req.u.rw.id;
267 rinfo->shadow[free].req.u.rw.id = 0x0fffffee; /* debug */
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700268 return free;
269}
270
Bob Liu81f35162015-11-14 11:12:11 +0800271static int add_id_to_freelist(struct blkfront_ring_info *rinfo,
Konrad Rzeszutek Wilk6f03a7f2015-11-16 15:14:41 -0500272 unsigned long id)
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700273{
Bob Liu81f35162015-11-14 11:12:11 +0800274 if (rinfo->shadow[id].req.u.rw.id != id)
Konrad Rzeszutek Wilk6878c322012-05-25 17:34:51 -0400275 return -EINVAL;
Bob Liu81f35162015-11-14 11:12:11 +0800276 if (rinfo->shadow[id].request == NULL)
Konrad Rzeszutek Wilk6878c322012-05-25 17:34:51 -0400277 return -EINVAL;
Bob Liu81f35162015-11-14 11:12:11 +0800278 rinfo->shadow[id].req.u.rw.id = rinfo->shadow_free;
279 rinfo->shadow[id].request = NULL;
280 rinfo->shadow_free = id;
Konrad Rzeszutek Wilk6878c322012-05-25 17:34:51 -0400281 return 0;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700282}
283
Bob Liu81f35162015-11-14 11:12:11 +0800284static int fill_grant_buffer(struct blkfront_ring_info *rinfo, int num)
Roger Pau Monne9c1e0502013-03-18 17:49:35 +0100285{
Bob Liu81f35162015-11-14 11:12:11 +0800286 struct blkfront_info *info = rinfo->dev_info;
Roger Pau Monne9c1e0502013-03-18 17:49:35 +0100287 struct page *granted_page;
288 struct grant *gnt_list_entry, *n;
289 int i = 0;
290
Konrad Rzeszutek Wilk6f03a7f2015-11-16 15:14:41 -0500291 while (i < num) {
Roger Pau Monne9c1e0502013-03-18 17:49:35 +0100292 gnt_list_entry = kzalloc(sizeof(struct grant), GFP_NOIO);
293 if (!gnt_list_entry)
294 goto out_of_memory;
295
Roger Pau Monnebfe11d62013-10-29 18:31:14 +0100296 if (info->feature_persistent) {
297 granted_page = alloc_page(GFP_NOIO);
298 if (!granted_page) {
299 kfree(gnt_list_entry);
300 goto out_of_memory;
301 }
Julien Gralla7a6df22015-06-30 11:58:51 +0100302 gnt_list_entry->page = granted_page;
Roger Pau Monne9c1e0502013-03-18 17:49:35 +0100303 }
304
Roger Pau Monne9c1e0502013-03-18 17:49:35 +0100305 gnt_list_entry->gref = GRANT_INVALID_REF;
Bob Liu73716df2015-11-16 16:51:39 -0500306 list_add(&gnt_list_entry->node, &rinfo->grants);
Roger Pau Monne9c1e0502013-03-18 17:49:35 +0100307 i++;
308 }
309
310 return 0;
311
312out_of_memory:
313 list_for_each_entry_safe(gnt_list_entry, n,
Bob Liu73716df2015-11-16 16:51:39 -0500314 &rinfo->grants, node) {
Roger Pau Monne9c1e0502013-03-18 17:49:35 +0100315 list_del(&gnt_list_entry->node);
Roger Pau Monnebfe11d62013-10-29 18:31:14 +0100316 if (info->feature_persistent)
Julien Gralla7a6df22015-06-30 11:58:51 +0100317 __free_page(gnt_list_entry->page);
Roger Pau Monne9c1e0502013-03-18 17:49:35 +0100318 kfree(gnt_list_entry);
319 i--;
320 }
321 BUG_ON(i != 0);
322 return -ENOMEM;
323}
324
Bob Liu73716df2015-11-16 16:51:39 -0500325static struct grant *get_free_grant(struct blkfront_ring_info *rinfo)
Roger Pau Monne9c1e0502013-03-18 17:49:35 +0100326{
327 struct grant *gnt_list_entry;
Roger Pau Monne9c1e0502013-03-18 17:49:35 +0100328
Bob Liu73716df2015-11-16 16:51:39 -0500329 BUG_ON(list_empty(&rinfo->grants));
330 gnt_list_entry = list_first_entry(&rinfo->grants, struct grant,
Julien Grall4f503fb2015-07-01 14:10:38 +0100331 node);
Roger Pau Monne9c1e0502013-03-18 17:49:35 +0100332 list_del(&gnt_list_entry->node);
333
Julien Grall4f503fb2015-07-01 14:10:38 +0100334 if (gnt_list_entry->gref != GRANT_INVALID_REF)
Bob Liu73716df2015-11-16 16:51:39 -0500335 rinfo->persistent_gnts_c--;
Julien Grall4f503fb2015-07-01 14:10:38 +0100336
337 return gnt_list_entry;
338}
339
340static inline void grant_foreign_access(const struct grant *gnt_list_entry,
341 const struct blkfront_info *info)
342{
343 gnttab_page_grant_foreign_access_ref_one(gnt_list_entry->gref,
344 info->xbdev->otherend_id,
345 gnt_list_entry->page,
346 0);
347}
348
349static struct grant *get_grant(grant_ref_t *gref_head,
350 unsigned long gfn,
Bob Liu73716df2015-11-16 16:51:39 -0500351 struct blkfront_ring_info *rinfo)
Julien Grall4f503fb2015-07-01 14:10:38 +0100352{
Bob Liu73716df2015-11-16 16:51:39 -0500353 struct grant *gnt_list_entry = get_free_grant(rinfo);
354 struct blkfront_info *info = rinfo->dev_info;
Julien Grall4f503fb2015-07-01 14:10:38 +0100355
356 if (gnt_list_entry->gref != GRANT_INVALID_REF)
Roger Pau Monne9c1e0502013-03-18 17:49:35 +0100357 return gnt_list_entry;
Julien Grall4f503fb2015-07-01 14:10:38 +0100358
359 /* Assign a gref to this page */
360 gnt_list_entry->gref = gnttab_claim_grant_reference(gref_head);
361 BUG_ON(gnt_list_entry->gref == -ENOSPC);
362 if (info->feature_persistent)
363 grant_foreign_access(gnt_list_entry, info);
364 else {
365 /* Grant access to the GFN passed by the caller */
366 gnttab_grant_foreign_access_ref(gnt_list_entry->gref,
367 info->xbdev->otherend_id,
368 gfn, 0);
Roger Pau Monne9c1e0502013-03-18 17:49:35 +0100369 }
370
Julien Grall4f503fb2015-07-01 14:10:38 +0100371 return gnt_list_entry;
372}
373
374static struct grant *get_indirect_grant(grant_ref_t *gref_head,
Bob Liu73716df2015-11-16 16:51:39 -0500375 struct blkfront_ring_info *rinfo)
Julien Grall4f503fb2015-07-01 14:10:38 +0100376{
Bob Liu73716df2015-11-16 16:51:39 -0500377 struct grant *gnt_list_entry = get_free_grant(rinfo);
378 struct blkfront_info *info = rinfo->dev_info;
Julien Grall4f503fb2015-07-01 14:10:38 +0100379
380 if (gnt_list_entry->gref != GRANT_INVALID_REF)
381 return gnt_list_entry;
382
Roger Pau Monne9c1e0502013-03-18 17:49:35 +0100383 /* Assign a gref to this page */
384 gnt_list_entry->gref = gnttab_claim_grant_reference(gref_head);
385 BUG_ON(gnt_list_entry->gref == -ENOSPC);
Roger Pau Monnebfe11d62013-10-29 18:31:14 +0100386 if (!info->feature_persistent) {
Julien Grall4f503fb2015-07-01 14:10:38 +0100387 struct page *indirect_page;
388
389 /* Fetch a pre-allocated page to use for indirect grefs */
Bob Liu73716df2015-11-16 16:51:39 -0500390 BUG_ON(list_empty(&rinfo->indirect_pages));
391 indirect_page = list_first_entry(&rinfo->indirect_pages,
Julien Grall4f503fb2015-07-01 14:10:38 +0100392 struct page, lru);
393 list_del(&indirect_page->lru);
394 gnt_list_entry->page = indirect_page;
Roger Pau Monnebfe11d62013-10-29 18:31:14 +0100395 }
Julien Grall4f503fb2015-07-01 14:10:38 +0100396 grant_foreign_access(gnt_list_entry, info);
397
Roger Pau Monne9c1e0502013-03-18 17:49:35 +0100398 return gnt_list_entry;
399}
400
Konrad Rzeszutek Wilk6878c322012-05-25 17:34:51 -0400401static const char *op_name(int op)
402{
403 static const char *const names[] = {
404 [BLKIF_OP_READ] = "read",
405 [BLKIF_OP_WRITE] = "write",
406 [BLKIF_OP_WRITE_BARRIER] = "barrier",
407 [BLKIF_OP_FLUSH_DISKCACHE] = "flush",
408 [BLKIF_OP_DISCARD] = "discard" };
409
410 if (op < 0 || op >= ARRAY_SIZE(names))
411 return "unknown";
412
413 if (!names[op])
414 return "reserved";
415
416 return names[op];
417}
Jan Beulich0e345822010-08-07 18:28:55 +0200418static int xlbd_reserve_minors(unsigned int minor, unsigned int nr)
419{
420 unsigned int end = minor + nr;
421 int rc;
422
423 if (end > nr_minors) {
424 unsigned long *bitmap, *old;
425
Thomas Meyerf0941482011-11-29 22:08:00 +0100426 bitmap = kcalloc(BITS_TO_LONGS(end), sizeof(*bitmap),
Jan Beulich0e345822010-08-07 18:28:55 +0200427 GFP_KERNEL);
428 if (bitmap == NULL)
429 return -ENOMEM;
430
431 spin_lock(&minor_lock);
432 if (end > nr_minors) {
433 old = minors;
434 memcpy(bitmap, minors,
435 BITS_TO_LONGS(nr_minors) * sizeof(*bitmap));
436 minors = bitmap;
437 nr_minors = BITS_TO_LONGS(end) * BITS_PER_LONG;
438 } else
439 old = bitmap;
440 spin_unlock(&minor_lock);
441 kfree(old);
442 }
443
444 spin_lock(&minor_lock);
445 if (find_next_bit(minors, end, minor) >= end) {
Akinobu Mita34ae2e42012-01-21 00:15:26 +0900446 bitmap_set(minors, minor, nr);
Jan Beulich0e345822010-08-07 18:28:55 +0200447 rc = 0;
448 } else
449 rc = -EBUSY;
450 spin_unlock(&minor_lock);
451
452 return rc;
453}
454
455static void xlbd_release_minors(unsigned int minor, unsigned int nr)
456{
457 unsigned int end = minor + nr;
458
459 BUG_ON(end > nr_minors);
460 spin_lock(&minor_lock);
Akinobu Mita34ae2e42012-01-21 00:15:26 +0900461 bitmap_clear(minors, minor, nr);
Jan Beulich0e345822010-08-07 18:28:55 +0200462 spin_unlock(&minor_lock);
463}
464
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700465static void blkif_restart_queue_callback(void *arg)
466{
Bob Liu81f35162015-11-14 11:12:11 +0800467 struct blkfront_ring_info *rinfo = (struct blkfront_ring_info *)arg;
468 schedule_work(&rinfo->work);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700469}
470
Harvey Harrisonafe42d72008-04-29 00:59:47 -0700471static int blkif_getgeo(struct block_device *bd, struct hd_geometry *hg)
Ian Campbell597592d2008-02-21 13:03:45 -0800472{
473 /* We don't have real geometry info, but let's at least return
474 values consistent with the size of the device */
475 sector_t nsect = get_capacity(bd->bd_disk);
476 sector_t cylinders = nsect;
477
478 hg->heads = 0xff;
479 hg->sectors = 0x3f;
480 sector_div(cylinders, hg->heads * hg->sectors);
481 hg->cylinders = cylinders;
482 if ((sector_t)(hg->cylinders + 1) * hg->heads * hg->sectors < nsect)
483 hg->cylinders = 0xffff;
484 return 0;
485}
486
Al Viroa63c8482008-03-02 10:23:47 -0500487static int blkif_ioctl(struct block_device *bdev, fmode_t mode,
Adrian Bunk62aa0052008-08-04 11:59:05 +0200488 unsigned command, unsigned long argument)
Christian Limpach440a01a2008-06-17 10:47:08 +0200489{
Al Viroa63c8482008-03-02 10:23:47 -0500490 struct blkfront_info *info = bdev->bd_disk->private_data;
Christian Limpach440a01a2008-06-17 10:47:08 +0200491 int i;
492
493 dev_dbg(&info->xbdev->dev, "command: 0x%x, argument: 0x%lx\n",
494 command, (long)argument);
495
496 switch (command) {
497 case CDROMMULTISESSION:
498 dev_dbg(&info->xbdev->dev, "FIXME: support multisession CDs later\n");
499 for (i = 0; i < sizeof(struct cdrom_multisession); i++)
500 if (put_user(0, (char __user *)(argument + i)))
501 return -EFAULT;
502 return 0;
503
504 case CDROM_GET_CAPABILITY: {
505 struct gendisk *gd = info->gd;
506 if (gd->flags & GENHD_FL_CD)
507 return 0;
508 return -EINVAL;
509 }
510
511 default:
512 /*printk(KERN_ALERT "ioctl %08x not supported by Xen blkdev\n",
513 command);*/
514 return -EINVAL; /* same return as native Linux */
515 }
516
517 return 0;
518}
519
Julien Grall2e073962015-08-13 19:23:10 +0100520static unsigned long blkif_ring_get_request(struct blkfront_ring_info *rinfo,
521 struct request *req,
522 struct blkif_request **ring_req)
523{
524 unsigned long id;
525
526 *ring_req = RING_GET_REQUEST(&rinfo->ring, rinfo->ring.req_prod_pvt);
527 rinfo->ring.req_prod_pvt++;
528
529 id = get_id_from_freelist(rinfo);
530 rinfo->shadow[id].request = req;
Julien Grall6cc568332015-08-13 13:13:35 +0100531 rinfo->shadow[id].status = REQ_WAITING;
532 rinfo->shadow[id].associated_id = NO_ASSOCIATED_ID;
Julien Grall2e073962015-08-13 19:23:10 +0100533
534 (*ring_req)->u.rw.id = id;
535
536 return id;
537}
538
Bob Liu81f35162015-11-14 11:12:11 +0800539static int blkif_queue_discard_req(struct request *req, struct blkfront_ring_info *rinfo)
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700540{
Bob Liu81f35162015-11-14 11:12:11 +0800541 struct blkfront_info *info = rinfo->dev_info;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700542 struct blkif_request *ring_req;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700543 unsigned long id;
Julien Grall33204662015-06-29 17:35:24 +0100544
545 /* Fill out a communications ring structure. */
Julien Grall2e073962015-08-13 19:23:10 +0100546 id = blkif_ring_get_request(rinfo, req, &ring_req);
Julien Grall33204662015-06-29 17:35:24 +0100547
548 ring_req->operation = BLKIF_OP_DISCARD;
549 ring_req->u.discard.nr_sectors = blk_rq_sectors(req);
550 ring_req->u.discard.id = id;
551 ring_req->u.discard.sector_number = (blkif_sector_t)blk_rq_pos(req);
Christoph Hellwig288dab82016-06-09 16:00:36 +0200552 if (req_op(req) == REQ_OP_SECURE_ERASE && info->feature_secdiscard)
Julien Grall33204662015-06-29 17:35:24 +0100553 ring_req->u.discard.flag = BLKIF_DISCARD_SECURE;
554 else
555 ring_req->u.discard.flag = 0;
556
Julien Grall33204662015-06-29 17:35:24 +0100557 /* Keep a private copy so we can reissue requests when recovering. */
Bob Liu81f35162015-11-14 11:12:11 +0800558 rinfo->shadow[id].req = *ring_req;
Julien Grall33204662015-06-29 17:35:24 +0100559
560 return 0;
561}
562
Julien Grallc004a6f2015-07-22 16:44:54 +0100563struct setup_rw_req {
564 unsigned int grant_idx;
565 struct blkif_request_segment *segments;
Bob Liu81f35162015-11-14 11:12:11 +0800566 struct blkfront_ring_info *rinfo;
Julien Grallc004a6f2015-07-22 16:44:54 +0100567 struct blkif_request *ring_req;
568 grant_ref_t gref_head;
569 unsigned int id;
570 /* Only used when persistent grant is used and it's a read request */
571 bool need_copy;
572 unsigned int bvec_off;
573 char *bvec_data;
Julien Grall6cc568332015-08-13 13:13:35 +0100574
575 bool require_extra_req;
576 struct blkif_request *extra_ring_req;
Julien Grallc004a6f2015-07-22 16:44:54 +0100577};
578
579static void blkif_setup_rw_req_grant(unsigned long gfn, unsigned int offset,
580 unsigned int len, void *data)
581{
582 struct setup_rw_req *setup = data;
583 int n, ref;
584 struct grant *gnt_list_entry;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700585 unsigned int fsect, lsect;
Julien Grallc004a6f2015-07-22 16:44:54 +0100586 /* Convenient aliases */
587 unsigned int grant_idx = setup->grant_idx;
588 struct blkif_request *ring_req = setup->ring_req;
Bob Liu81f35162015-11-14 11:12:11 +0800589 struct blkfront_ring_info *rinfo = setup->rinfo;
Julien Grall6cc568332015-08-13 13:13:35 +0100590 /*
591 * We always use the shadow of the first request to store the list
592 * of grant associated to the block I/O request. This made the
593 * completion more easy to handle even if the block I/O request is
594 * split.
595 */
Bob Liu81f35162015-11-14 11:12:11 +0800596 struct blk_shadow *shadow = &rinfo->shadow[setup->id];
Julien Grallc004a6f2015-07-22 16:44:54 +0100597
Julien Grall6cc568332015-08-13 13:13:35 +0100598 if (unlikely(setup->require_extra_req &&
599 grant_idx >= BLKIF_MAX_SEGMENTS_PER_REQUEST)) {
600 /*
601 * We are using the second request, setup grant_idx
602 * to be the index of the segment array.
603 */
604 grant_idx -= BLKIF_MAX_SEGMENTS_PER_REQUEST;
605 ring_req = setup->extra_ring_req;
606 }
607
Julien Grallc004a6f2015-07-22 16:44:54 +0100608 if ((ring_req->operation == BLKIF_OP_INDIRECT) &&
609 (grant_idx % GRANTS_PER_INDIRECT_FRAME == 0)) {
610 if (setup->segments)
611 kunmap_atomic(setup->segments);
612
613 n = grant_idx / GRANTS_PER_INDIRECT_FRAME;
Bob Liu73716df2015-11-16 16:51:39 -0500614 gnt_list_entry = get_indirect_grant(&setup->gref_head, rinfo);
Julien Grallc004a6f2015-07-22 16:44:54 +0100615 shadow->indirect_grants[n] = gnt_list_entry;
616 setup->segments = kmap_atomic(gnt_list_entry->page);
617 ring_req->u.indirect.indirect_grefs[n] = gnt_list_entry->gref;
618 }
619
Bob Liu73716df2015-11-16 16:51:39 -0500620 gnt_list_entry = get_grant(&setup->gref_head, gfn, rinfo);
Julien Grallc004a6f2015-07-22 16:44:54 +0100621 ref = gnt_list_entry->gref;
Julien Grall6cc568332015-08-13 13:13:35 +0100622 /*
623 * All the grants are stored in the shadow of the first
624 * request. Therefore we have to use the global index.
625 */
626 shadow->grants_used[setup->grant_idx] = gnt_list_entry;
Julien Grallc004a6f2015-07-22 16:44:54 +0100627
628 if (setup->need_copy) {
629 void *shared_data;
630
631 shared_data = kmap_atomic(gnt_list_entry->page);
632 /*
633 * this does not wipe data stored outside the
634 * range sg->offset..sg->offset+sg->length.
635 * Therefore, blkback *could* see data from
636 * previous requests. This is OK as long as
637 * persistent grants are shared with just one
638 * domain. It may need refactoring if this
639 * changes
640 */
641 memcpy(shared_data + offset,
642 setup->bvec_data + setup->bvec_off,
643 len);
644
645 kunmap_atomic(shared_data);
646 setup->bvec_off += len;
647 }
648
649 fsect = offset >> 9;
650 lsect = fsect + (len >> 9) - 1;
651 if (ring_req->operation != BLKIF_OP_INDIRECT) {
652 ring_req->u.rw.seg[grant_idx] =
653 (struct blkif_request_segment) {
654 .gref = ref,
655 .first_sect = fsect,
656 .last_sect = lsect };
657 } else {
658 setup->segments[grant_idx % GRANTS_PER_INDIRECT_FRAME] =
659 (struct blkif_request_segment) {
660 .gref = ref,
661 .first_sect = fsect,
662 .last_sect = lsect };
663 }
664
665 (setup->grant_idx)++;
666}
667
Julien Grall6cc568332015-08-13 13:13:35 +0100668static void blkif_setup_extra_req(struct blkif_request *first,
669 struct blkif_request *second)
670{
671 uint16_t nr_segments = first->u.rw.nr_segments;
672
673 /*
674 * The second request is only present when the first request uses
675 * all its segments. It's always the continuity of the first one.
676 */
677 first->u.rw.nr_segments = BLKIF_MAX_SEGMENTS_PER_REQUEST;
678
679 second->u.rw.nr_segments = nr_segments - BLKIF_MAX_SEGMENTS_PER_REQUEST;
680 second->u.rw.sector_number = first->u.rw.sector_number +
681 (BLKIF_MAX_SEGMENTS_PER_REQUEST * XEN_PAGE_SIZE) / 512;
682
683 second->u.rw.handle = first->u.rw.handle;
684 second->operation = first->operation;
685}
686
Bob Liu81f35162015-11-14 11:12:11 +0800687static int blkif_queue_rw_req(struct request *req, struct blkfront_ring_info *rinfo)
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700688{
Bob Liu81f35162015-11-14 11:12:11 +0800689 struct blkfront_info *info = rinfo->dev_info;
Julien Grall6cc568332015-08-13 13:13:35 +0100690 struct blkif_request *ring_req, *extra_ring_req = NULL;
691 unsigned long id, extra_id = NO_ASSOCIATED_ID;
692 bool require_extra_req = false;
Julien Grallc004a6f2015-07-22 16:44:54 +0100693 int i;
694 struct setup_rw_req setup = {
695 .grant_idx = 0,
696 .segments = NULL,
Bob Liu81f35162015-11-14 11:12:11 +0800697 .rinfo = rinfo,
Julien Grallc004a6f2015-07-22 16:44:54 +0100698 .need_copy = rq_data_dir(req) && info->feature_persistent,
699 };
Roger Pau Monne0a8704a2012-10-24 18:58:45 +0200700
701 /*
702 * Used to store if we are able to queue the request by just using
703 * existing persistent grants, or if we have to get new grants,
704 * as there are not sufficiently many free.
705 */
Dongli Zhangbd912ef32017-06-28 20:57:28 +0800706 bool new_persistent_gnts = false;
Jens Axboe9e973e62009-02-24 08:10:09 +0100707 struct scatterlist *sg;
Julien Grallc004a6f2015-07-22 16:44:54 +0100708 int num_sg, max_grefs, num_grant;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700709
Julien Grallc004a6f2015-07-22 16:44:54 +0100710 max_grefs = req->nr_phys_segments * GRANTS_PER_PSEG;
Roger Pau Monnec47206e2013-08-12 12:53:43 +0200711 if (max_grefs > BLKIF_MAX_SEGMENTS_PER_REQUEST)
712 /*
713 * If we are using indirect segments we need to account
714 * for the indirect grefs used in the request.
715 */
Julien Grallc004a6f2015-07-22 16:44:54 +0100716 max_grefs += INDIRECT_GREFS(max_grefs);
Roger Pau Monne402b27f2013-04-18 16:06:54 +0200717
Dongli Zhangbd912ef32017-06-28 20:57:28 +0800718 /* Check if we have enough persistent grants to allocate a requests */
719 if (rinfo->persistent_gnts_c < max_grefs) {
720 new_persistent_gnts = true;
721
722 if (gnttab_alloc_grant_references(
723 max_grefs - rinfo->persistent_gnts_c,
724 &setup.gref_head) < 0) {
Roger Pau Monne0a8704a2012-10-24 18:58:45 +0200725 gnttab_request_free_callback(
Bob Liu81f35162015-11-14 11:12:11 +0800726 &rinfo->callback,
Roger Pau Monne0a8704a2012-10-24 18:58:45 +0200727 blkif_restart_queue_callback,
Bob Liu81f35162015-11-14 11:12:11 +0800728 rinfo,
Dongli Zhangbd912ef32017-06-28 20:57:28 +0800729 max_grefs - rinfo->persistent_gnts_c);
Roger Pau Monne0a8704a2012-10-24 18:58:45 +0200730 return 1;
731 }
Dongli Zhangbd912ef32017-06-28 20:57:28 +0800732 }
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700733
734 /* Fill out a communications ring structure. */
Julien Grall2e073962015-08-13 19:23:10 +0100735 id = blkif_ring_get_request(rinfo, req, &ring_req);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700736
Bob Liu81f35162015-11-14 11:12:11 +0800737 num_sg = blk_rq_map_sg(req->q, req, rinfo->shadow[id].sg);
Julien Grallc004a6f2015-07-22 16:44:54 +0100738 num_grant = 0;
739 /* Calculate the number of grant used */
Bob Liu81f35162015-11-14 11:12:11 +0800740 for_each_sg(rinfo->shadow[id].sg, sg, num_sg, i)
Julien Grallc004a6f2015-07-22 16:44:54 +0100741 num_grant += gnttab_count_grant(sg->offset, sg->length);
742
Julien Grall6cc568332015-08-13 13:13:35 +0100743 require_extra_req = info->max_indirect_segments == 0 &&
744 num_grant > BLKIF_MAX_SEGMENTS_PER_REQUEST;
745 BUG_ON(!HAS_EXTRA_REQ && require_extra_req);
746
Bob Liu81f35162015-11-14 11:12:11 +0800747 rinfo->shadow[id].num_sg = num_sg;
Julien Grall6cc568332015-08-13 13:13:35 +0100748 if (num_grant > BLKIF_MAX_SEGMENTS_PER_REQUEST &&
749 likely(!require_extra_req)) {
Julien Grall33204662015-06-29 17:35:24 +0100750 /*
751 * The indirect operation can only be a BLKIF_OP_READ or
752 * BLKIF_OP_WRITE
753 */
Mike Christie3a5e02c2016-06-05 14:32:23 -0500754 BUG_ON(req_op(req) == REQ_OP_FLUSH || req->cmd_flags & REQ_FUA);
Julien Grall33204662015-06-29 17:35:24 +0100755 ring_req->operation = BLKIF_OP_INDIRECT;
756 ring_req->u.indirect.indirect_op = rq_data_dir(req) ?
757 BLKIF_OP_WRITE : BLKIF_OP_READ;
758 ring_req->u.indirect.sector_number = (blkif_sector_t)blk_rq_pos(req);
759 ring_req->u.indirect.handle = info->handle;
Julien Grallc004a6f2015-07-22 16:44:54 +0100760 ring_req->u.indirect.nr_segments = num_grant;
Li Dongyanged30bf32011-09-01 18:39:09 +0800761 } else {
Julien Grall33204662015-06-29 17:35:24 +0100762 ring_req->u.rw.sector_number = (blkif_sector_t)blk_rq_pos(req);
763 ring_req->u.rw.handle = info->handle;
764 ring_req->operation = rq_data_dir(req) ?
765 BLKIF_OP_WRITE : BLKIF_OP_READ;
Mike Christie3a5e02c2016-06-05 14:32:23 -0500766 if (req_op(req) == REQ_OP_FLUSH || req->cmd_flags & REQ_FUA) {
Roger Pau Monne402b27f2013-04-18 16:06:54 +0200767 /*
Julien Grall33204662015-06-29 17:35:24 +0100768 * Ideally we can do an unordered flush-to-disk.
769 * In case the backend onlysupports barriers, use that.
770 * A barrier request a superset of FUA, so we can
771 * implement it the same way. (It's also a FLUSH+FUA,
772 * since it is guaranteed ordered WRT previous writes.)
Roger Pau Monne402b27f2013-04-18 16:06:54 +0200773 */
Mike Christiea4180902016-06-05 14:32:24 -0500774 if (info->feature_flush && info->feature_fua)
Julien Grall33204662015-06-29 17:35:24 +0100775 ring_req->operation =
776 BLKIF_OP_WRITE_BARRIER;
Mike Christiea4180902016-06-05 14:32:24 -0500777 else if (info->feature_flush)
Julien Grall33204662015-06-29 17:35:24 +0100778 ring_req->operation =
779 BLKIF_OP_FLUSH_DISKCACHE;
Mike Christiea4180902016-06-05 14:32:24 -0500780 else
Julien Grall33204662015-06-29 17:35:24 +0100781 ring_req->operation = 0;
Li Dongyanged30bf32011-09-01 18:39:09 +0800782 }
Julien Grallc004a6f2015-07-22 16:44:54 +0100783 ring_req->u.rw.nr_segments = num_grant;
Julien Grall6cc568332015-08-13 13:13:35 +0100784 if (unlikely(require_extra_req)) {
785 extra_id = blkif_ring_get_request(rinfo, req,
786 &extra_ring_req);
787 /*
788 * Only the first request contains the scatter-gather
789 * list.
790 */
791 rinfo->shadow[extra_id].num_sg = 0;
792
793 blkif_setup_extra_req(ring_req, extra_ring_req);
794
795 /* Link the 2 requests together */
796 rinfo->shadow[extra_id].associated_id = id;
797 rinfo->shadow[id].associated_id = extra_id;
798 }
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700799 }
800
Julien Grallc004a6f2015-07-22 16:44:54 +0100801 setup.ring_req = ring_req;
802 setup.id = id;
Julien Grall6cc568332015-08-13 13:13:35 +0100803
804 setup.require_extra_req = require_extra_req;
805 if (unlikely(require_extra_req))
806 setup.extra_ring_req = extra_ring_req;
807
Bob Liu81f35162015-11-14 11:12:11 +0800808 for_each_sg(rinfo->shadow[id].sg, sg, num_sg, i) {
Julien Grallc004a6f2015-07-22 16:44:54 +0100809 BUG_ON(sg->offset + sg->length > PAGE_SIZE);
Julien Grall33204662015-06-29 17:35:24 +0100810
Julien Grallc004a6f2015-07-22 16:44:54 +0100811 if (setup.need_copy) {
812 setup.bvec_off = sg->offset;
813 setup.bvec_data = kmap_atomic(sg_page(sg));
Julien Grall33204662015-06-29 17:35:24 +0100814 }
815
Julien Grallc004a6f2015-07-22 16:44:54 +0100816 gnttab_foreach_grant_in_range(sg_page(sg),
817 sg->offset,
818 sg->length,
819 blkif_setup_rw_req_grant,
820 &setup);
Julien Grall33204662015-06-29 17:35:24 +0100821
Julien Grallc004a6f2015-07-22 16:44:54 +0100822 if (setup.need_copy)
823 kunmap_atomic(setup.bvec_data);
Julien Grall33204662015-06-29 17:35:24 +0100824 }
Julien Grallc004a6f2015-07-22 16:44:54 +0100825 if (setup.segments)
826 kunmap_atomic(setup.segments);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700827
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700828 /* Keep a private copy so we can reissue requests when recovering. */
Bob Liu81f35162015-11-14 11:12:11 +0800829 rinfo->shadow[id].req = *ring_req;
Julien Grall6cc568332015-08-13 13:13:35 +0100830 if (unlikely(require_extra_req))
831 rinfo->shadow[extra_id].req = *extra_ring_req;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700832
Dongli Zhangbd912ef32017-06-28 20:57:28 +0800833 if (new_persistent_gnts)
Julien Grallc004a6f2015-07-22 16:44:54 +0100834 gnttab_free_grant_references(setup.gref_head);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700835
836 return 0;
837}
838
Julien Grall33204662015-06-29 17:35:24 +0100839/*
840 * Generate a Xen blkfront IO request from a blk layer request. Reads
841 * and writes are handled as expected.
842 *
843 * @req: a request struct
844 */
Bob Liu81f35162015-11-14 11:12:11 +0800845static int blkif_queue_request(struct request *req, struct blkfront_ring_info *rinfo)
Julien Grall33204662015-06-29 17:35:24 +0100846{
Bob Liu81f35162015-11-14 11:12:11 +0800847 if (unlikely(rinfo->dev_info->connected != BLKIF_STATE_CONNECTED))
Julien Grall33204662015-06-29 17:35:24 +0100848 return 1;
849
Mike Christiec2df40d2016-06-05 14:32:17 -0500850 if (unlikely(req_op(req) == REQ_OP_DISCARD ||
Christoph Hellwig288dab82016-06-09 16:00:36 +0200851 req_op(req) == REQ_OP_SECURE_ERASE))
Bob Liu81f35162015-11-14 11:12:11 +0800852 return blkif_queue_discard_req(req, rinfo);
Julien Grall33204662015-06-29 17:35:24 +0100853 else
Bob Liu81f35162015-11-14 11:12:11 +0800854 return blkif_queue_rw_req(req, rinfo);
Julien Grall33204662015-06-29 17:35:24 +0100855}
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700856
Bob Liu81f35162015-11-14 11:12:11 +0800857static inline void flush_requests(struct blkfront_ring_info *rinfo)
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700858{
859 int notify;
860
Bob Liu81f35162015-11-14 11:12:11 +0800861 RING_PUSH_REQUESTS_AND_CHECK_NOTIFY(&rinfo->ring, notify);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700862
863 if (notify)
Bob Liu81f35162015-11-14 11:12:11 +0800864 notify_remote_via_irq(rinfo->irq);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700865}
866
Vitaly Kuznetsovad42d392014-12-01 14:01:13 +0100867static inline bool blkif_request_flush_invalid(struct request *req,
868 struct blkfront_info *info)
Arianna Avanzini0f1ca652014-08-22 13:20:02 +0200869{
Christoph Hellwigaebf5262017-01-31 16:57:31 +0100870 return (blk_rq_is_passthrough(req) ||
Mike Christie3a5e02c2016-06-05 14:32:23 -0500871 ((req_op(req) == REQ_OP_FLUSH) &&
Mike Christiea4180902016-06-05 14:32:24 -0500872 !info->feature_flush) ||
Vitaly Kuznetsovad42d392014-12-01 14:01:13 +0100873 ((req->cmd_flags & REQ_FUA) &&
Mike Christiea4180902016-06-05 14:32:24 -0500874 !info->feature_fua));
Arianna Avanzini0f1ca652014-08-22 13:20:02 +0200875}
876
Christoph Hellwigfc17b652017-06-03 09:38:05 +0200877static blk_status_t blkif_queue_rq(struct blk_mq_hw_ctx *hctx,
Konrad Rzeszutek Wilk6f03a7f2015-11-16 15:14:41 -0500878 const struct blk_mq_queue_data *qd)
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700879{
Bob Liu11659562015-11-14 11:12:13 +0800880 unsigned long flags;
Bob Liu2a6f71a2016-05-31 16:59:17 +0800881 int qid = hctx->queue_num;
882 struct blkfront_info *info = hctx->queue->queuedata;
883 struct blkfront_ring_info *rinfo = NULL;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700884
Bob Liu2a6f71a2016-05-31 16:59:17 +0800885 BUG_ON(info->nr_rings <= qid);
886 rinfo = &info->rinfo[qid];
Bob Liu907c3eb2015-07-13 17:55:24 +0800887 blk_mq_start_request(qd->rq);
Bob Liu11659562015-11-14 11:12:13 +0800888 spin_lock_irqsave(&rinfo->ring_lock, flags);
Bob Liu81f35162015-11-14 11:12:11 +0800889 if (RING_FULL(&rinfo->ring))
Bob Liu907c3eb2015-07-13 17:55:24 +0800890 goto out_busy;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700891
Bob Liu81f35162015-11-14 11:12:11 +0800892 if (blkif_request_flush_invalid(qd->rq, rinfo->dev_info))
Bob Liu907c3eb2015-07-13 17:55:24 +0800893 goto out_err;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700894
Bob Liu81f35162015-11-14 11:12:11 +0800895 if (blkif_queue_request(qd->rq, rinfo))
Bob Liu907c3eb2015-07-13 17:55:24 +0800896 goto out_busy;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700897
Bob Liu81f35162015-11-14 11:12:11 +0800898 flush_requests(rinfo);
Bob Liu11659562015-11-14 11:12:13 +0800899 spin_unlock_irqrestore(&rinfo->ring_lock, flags);
Christoph Hellwigfc17b652017-06-03 09:38:05 +0200900 return BLK_STS_OK;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700901
Bob Liu907c3eb2015-07-13 17:55:24 +0800902out_err:
Bob Liu11659562015-11-14 11:12:13 +0800903 spin_unlock_irqrestore(&rinfo->ring_lock, flags);
Christoph Hellwigfc17b652017-06-03 09:38:05 +0200904 return BLK_STS_IOERR;
Tejun Heo296b2f62009-05-08 11:54:15 +0900905
Bob Liu907c3eb2015-07-13 17:55:24 +0800906out_busy:
Bob Liu907c3eb2015-07-13 17:55:24 +0800907 blk_mq_stop_hw_queue(hctx);
Junxiao Bi4b422cb2017-07-20 09:26:21 +0800908 spin_unlock_irqrestore(&rinfo->ring_lock, flags);
Ming Lei86ff7c22018-01-30 22:04:57 -0500909 return BLK_STS_DEV_RESOURCE;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700910}
911
Christoph Hellwig26095872017-04-20 16:03:08 +0200912static void blkif_complete_rq(struct request *rq)
913{
914 blk_mq_end_request(rq, blkif_req(rq)->error);
915}
916
Eric Biggersf363b082017-03-30 13:39:16 -0700917static const struct blk_mq_ops blkfront_mq_ops = {
Bob Liu907c3eb2015-07-13 17:55:24 +0800918 .queue_rq = blkif_queue_rq,
Christoph Hellwig26095872017-04-20 16:03:08 +0200919 .complete = blkif_complete_rq,
Bob Liu907c3eb2015-07-13 17:55:24 +0800920};
921
Bob Liu172335a2016-07-01 17:43:39 -0400922static void blkif_set_queue_limits(struct blkfront_info *info)
923{
924 struct request_queue *rq = info->rq;
925 struct gendisk *gd = info->gd;
926 unsigned int segments = info->max_indirect_segments ? :
927 BLKIF_MAX_SEGMENTS_PER_REQUEST;
928
Bart Van Assche8b904b52018-03-07 17:10:10 -0800929 blk_queue_flag_set(QUEUE_FLAG_VIRT, rq);
Bob Liu172335a2016-07-01 17:43:39 -0400930
931 if (info->feature_discard) {
Bart Van Assche8b904b52018-03-07 17:10:10 -0800932 blk_queue_flag_set(QUEUE_FLAG_DISCARD, rq);
Bob Liu172335a2016-07-01 17:43:39 -0400933 blk_queue_max_discard_sectors(rq, get_capacity(gd));
934 rq->limits.discard_granularity = info->discard_granularity;
935 rq->limits.discard_alignment = info->discard_alignment;
936 if (info->feature_secdiscard)
Bart Van Assche8b904b52018-03-07 17:10:10 -0800937 blk_queue_flag_set(QUEUE_FLAG_SECERASE, rq);
Bob Liu172335a2016-07-01 17:43:39 -0400938 }
939
940 /* Hard sector size and max sectors impersonate the equiv. hardware. */
941 blk_queue_logical_block_size(rq, info->sector_size);
942 blk_queue_physical_block_size(rq, info->physical_sector_size);
943 blk_queue_max_hw_sectors(rq, (segments * XEN_PAGE_SIZE) / 512);
944
945 /* Each segment in a request is up to an aligned page in size. */
946 blk_queue_segment_boundary(rq, PAGE_SIZE - 1);
947 blk_queue_max_segment_size(rq, PAGE_SIZE);
948
949 /* Ensure a merged request will fit in a single I/O ring slot. */
950 blk_queue_max_segments(rq, segments / GRANTS_PER_PSEG);
951
952 /* Make sure buffer addresses are sector-aligned. */
953 blk_queue_dma_alignment(rq, 511);
Bob Liu172335a2016-07-01 17:43:39 -0400954}
955
Roger Pau Monne402b27f2013-04-18 16:06:54 +0200956static int xlvbd_init_blk_queue(struct gendisk *gd, u16 sector_size,
Bob Liu172335a2016-07-01 17:43:39 -0400957 unsigned int physical_sector_size)
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700958{
Jens Axboe165125e2007-07-24 09:28:11 +0200959 struct request_queue *rq;
Li Dongyanged30bf32011-09-01 18:39:09 +0800960 struct blkfront_info *info = gd->private_data;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700961
Bob Liu907c3eb2015-07-13 17:55:24 +0800962 memset(&info->tag_set, 0, sizeof(info->tag_set));
963 info->tag_set.ops = &blkfront_mq_ops;
Bob Liu28d949b2015-11-14 11:12:14 +0800964 info->tag_set.nr_hw_queues = info->nr_rings;
Julien Grall6cc568332015-08-13 13:13:35 +0100965 if (HAS_EXTRA_REQ && info->max_indirect_segments == 0) {
966 /*
967 * When indirect descriptior is not supported, the I/O request
968 * will be split between multiple request in the ring.
969 * To avoid problems when sending the request, divide by
970 * 2 the depth of the queue.
971 */
972 info->tag_set.queue_depth = BLK_RING_SIZE(info) / 2;
973 } else
974 info->tag_set.queue_depth = BLK_RING_SIZE(info);
Bob Liu907c3eb2015-07-13 17:55:24 +0800975 info->tag_set.numa_node = NUMA_NO_NODE;
976 info->tag_set.flags = BLK_MQ_F_SHOULD_MERGE | BLK_MQ_F_SG_MERGE;
Christoph Hellwig26095872017-04-20 16:03:08 +0200977 info->tag_set.cmd_size = sizeof(struct blkif_req);
Bob Liu907c3eb2015-07-13 17:55:24 +0800978 info->tag_set.driver_data = info;
979
980 if (blk_mq_alloc_tag_set(&info->tag_set))
Konrad Rzeszutek Wilkbde21f72015-11-25 13:07:39 -0500981 return -EINVAL;
Bob Liu907c3eb2015-07-13 17:55:24 +0800982 rq = blk_mq_init_queue(&info->tag_set);
983 if (IS_ERR(rq)) {
984 blk_mq_free_tag_set(&info->tag_set);
Konrad Rzeszutek Wilkbde21f72015-11-25 13:07:39 -0500985 return PTR_ERR(rq);
Bob Liu907c3eb2015-07-13 17:55:24 +0800986 }
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700987
Bob Liu2a6f71a2016-05-31 16:59:17 +0800988 rq->queuedata = info;
Bob Liu172335a2016-07-01 17:43:39 -0400989 info->rq = gd->queue = rq;
990 info->gd = gd;
991 info->sector_size = sector_size;
992 info->physical_sector_size = physical_sector_size;
993 blkif_set_queue_limits(info);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700994
995 return 0;
996}
997
Mike Christiea4180902016-06-05 14:32:24 -0500998static const char *flush_info(struct blkfront_info *info)
Vitaly Kuznetsovfdf9b962014-12-09 15:25:10 +0100999{
Mike Christiea4180902016-06-05 14:32:24 -05001000 if (info->feature_flush && info->feature_fua)
Vitaly Kuznetsovfdf9b962014-12-09 15:25:10 +01001001 return "barrier: enabled;";
Mike Christiea4180902016-06-05 14:32:24 -05001002 else if (info->feature_flush)
Vitaly Kuznetsovfdf9b962014-12-09 15:25:10 +01001003 return "flush diskcache: enabled;";
Mike Christiea4180902016-06-05 14:32:24 -05001004 else
Vitaly Kuznetsovfdf9b962014-12-09 15:25:10 +01001005 return "barrier or flush: disabled;";
Vitaly Kuznetsovfdf9b962014-12-09 15:25:10 +01001006}
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001007
Tejun Heo4913efe2010-09-03 11:56:16 +02001008static void xlvbd_flush(struct blkfront_info *info)
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001009{
Mike Christiea4180902016-06-05 14:32:24 -05001010 blk_queue_write_cache(info->rq, info->feature_flush ? true : false,
1011 info->feature_fua ? true : false);
Vitaly Kuznetsovfdf9b962014-12-09 15:25:10 +01001012 pr_info("blkfront: %s: %s %s %s %s %s\n",
Mike Christiea4180902016-06-05 14:32:24 -05001013 info->gd->disk_name, flush_info(info),
Vitaly Kuznetsovfdf9b962014-12-09 15:25:10 +01001014 "persistent grants:", info->feature_persistent ?
1015 "enabled;" : "disabled;", "indirect descriptors:",
1016 info->max_indirect_segments ? "enabled;" : "disabled;");
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001017}
1018
Stefano Stabellinic80a4202010-12-02 17:55:00 +00001019static int xen_translate_vdev(int vdevice, int *minor, unsigned int *offset)
1020{
1021 int major;
1022 major = BLKIF_MAJOR(vdevice);
1023 *minor = BLKIF_MINOR(vdevice);
1024 switch (major) {
1025 case XEN_IDE0_MAJOR:
1026 *offset = (*minor / 64) + EMULATED_HD_DISK_NAME_OFFSET;
1027 *minor = ((*minor / 64) * PARTS_PER_DISK) +
1028 EMULATED_HD_DISK_MINOR_OFFSET;
1029 break;
1030 case XEN_IDE1_MAJOR:
1031 *offset = (*minor / 64) + 2 + EMULATED_HD_DISK_NAME_OFFSET;
1032 *minor = (((*minor / 64) + 2) * PARTS_PER_DISK) +
1033 EMULATED_HD_DISK_MINOR_OFFSET;
1034 break;
1035 case XEN_SCSI_DISK0_MAJOR:
1036 *offset = (*minor / PARTS_PER_DISK) + EMULATED_SD_DISK_NAME_OFFSET;
1037 *minor = *minor + EMULATED_SD_DISK_MINOR_OFFSET;
1038 break;
1039 case XEN_SCSI_DISK1_MAJOR:
1040 case XEN_SCSI_DISK2_MAJOR:
1041 case XEN_SCSI_DISK3_MAJOR:
1042 case XEN_SCSI_DISK4_MAJOR:
1043 case XEN_SCSI_DISK5_MAJOR:
1044 case XEN_SCSI_DISK6_MAJOR:
1045 case XEN_SCSI_DISK7_MAJOR:
1046 *offset = (*minor / PARTS_PER_DISK) +
1047 ((major - XEN_SCSI_DISK1_MAJOR + 1) * 16) +
1048 EMULATED_SD_DISK_NAME_OFFSET;
1049 *minor = *minor +
1050 ((major - XEN_SCSI_DISK1_MAJOR + 1) * 16 * PARTS_PER_DISK) +
1051 EMULATED_SD_DISK_MINOR_OFFSET;
1052 break;
1053 case XEN_SCSI_DISK8_MAJOR:
1054 case XEN_SCSI_DISK9_MAJOR:
1055 case XEN_SCSI_DISK10_MAJOR:
1056 case XEN_SCSI_DISK11_MAJOR:
1057 case XEN_SCSI_DISK12_MAJOR:
1058 case XEN_SCSI_DISK13_MAJOR:
1059 case XEN_SCSI_DISK14_MAJOR:
1060 case XEN_SCSI_DISK15_MAJOR:
1061 *offset = (*minor / PARTS_PER_DISK) +
1062 ((major - XEN_SCSI_DISK8_MAJOR + 8) * 16) +
1063 EMULATED_SD_DISK_NAME_OFFSET;
1064 *minor = *minor +
1065 ((major - XEN_SCSI_DISK8_MAJOR + 8) * 16 * PARTS_PER_DISK) +
1066 EMULATED_SD_DISK_MINOR_OFFSET;
1067 break;
1068 case XENVBD_MAJOR:
1069 *offset = *minor / PARTS_PER_DISK;
1070 break;
1071 default:
1072 printk(KERN_WARNING "blkfront: your disk configuration is "
1073 "incorrect, please use an xvd device instead\n");
1074 return -ENODEV;
1075 }
1076 return 0;
1077}
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001078
Jan Beuliche77c78c2012-04-05 16:37:22 +01001079static char *encode_disk_name(char *ptr, unsigned int n)
1080{
1081 if (n >= 26)
1082 ptr = encode_disk_name(ptr, n / 26 - 1);
1083 *ptr = 'a' + n % 26;
1084 return ptr + 1;
1085}
1086
Chris Lalancette9246b5f2008-09-17 14:30:32 -07001087static int xlvbd_alloc_gendisk(blkif_sector_t capacity,
1088 struct blkfront_info *info,
Stefan Bader7c4d7d72013-05-13 16:28:15 +02001089 u16 vdisk_info, u16 sector_size,
1090 unsigned int physical_sector_size)
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001091{
1092 struct gendisk *gd;
1093 int nr_minors = 1;
Stefano Stabellinic80a4202010-12-02 17:55:00 +00001094 int err;
Chris Lalancette9246b5f2008-09-17 14:30:32 -07001095 unsigned int offset;
1096 int minor;
1097 int nr_parts;
Jan Beuliche77c78c2012-04-05 16:37:22 +01001098 char *ptr;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001099
1100 BUG_ON(info->gd != NULL);
1101 BUG_ON(info->rq != NULL);
1102
Chris Lalancette9246b5f2008-09-17 14:30:32 -07001103 if ((info->vdevice>>EXT_SHIFT) > 1) {
1104 /* this is above the extended range; something is wrong */
1105 printk(KERN_WARNING "blkfront: vdevice 0x%x is above the extended range; ignoring\n", info->vdevice);
1106 return -ENODEV;
1107 }
1108
1109 if (!VDEV_IS_EXTENDED(info->vdevice)) {
Stefano Stabellinic80a4202010-12-02 17:55:00 +00001110 err = xen_translate_vdev(info->vdevice, &minor, &offset);
1111 if (err)
1112 return err;
1113 nr_parts = PARTS_PER_DISK;
Chris Lalancette9246b5f2008-09-17 14:30:32 -07001114 } else {
1115 minor = BLKIF_MINOR_EXT(info->vdevice);
1116 nr_parts = PARTS_PER_EXT_DISK;
Stefano Stabellinic80a4202010-12-02 17:55:00 +00001117 offset = minor / nr_parts;
Stefan Bader89153b52011-07-14 15:30:37 +02001118 if (xen_hvm_domain() && offset < EMULATED_HD_DISK_NAME_OFFSET + 4)
Stefano Stabellinic80a4202010-12-02 17:55:00 +00001119 printk(KERN_WARNING "blkfront: vdevice 0x%x might conflict with "
1120 "emulated IDE disks,\n\t choose an xvd device name"
1121 "from xvde on\n", info->vdevice);
Chris Lalancette9246b5f2008-09-17 14:30:32 -07001122 }
Jan Beuliche77c78c2012-04-05 16:37:22 +01001123 if (minor >> MINORBITS) {
1124 pr_warn("blkfront: %#x's minor (%#x) out of range; ignoring\n",
1125 info->vdevice, minor);
1126 return -ENODEV;
1127 }
Chris Lalancette9246b5f2008-09-17 14:30:32 -07001128
1129 if ((minor % nr_parts) == 0)
1130 nr_minors = nr_parts;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001131
Jan Beulich0e345822010-08-07 18:28:55 +02001132 err = xlbd_reserve_minors(minor, nr_minors);
1133 if (err)
1134 goto out;
1135 err = -ENODEV;
1136
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001137 gd = alloc_disk(nr_minors);
1138 if (gd == NULL)
Jan Beulich0e345822010-08-07 18:28:55 +02001139 goto release;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001140
Jan Beuliche77c78c2012-04-05 16:37:22 +01001141 strcpy(gd->disk_name, DEV_NAME);
1142 ptr = encode_disk_name(gd->disk_name + sizeof(DEV_NAME) - 1, offset);
1143 BUG_ON(ptr >= gd->disk_name + DISK_NAME_LEN);
1144 if (nr_minors > 1)
1145 *ptr = 0;
1146 else
1147 snprintf(ptr, gd->disk_name + DISK_NAME_LEN - ptr,
1148 "%d", minor & (nr_parts - 1));
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001149
1150 gd->major = XENVBD_MAJOR;
1151 gd->first_minor = minor;
1152 gd->fops = &xlvbd_block_fops;
1153 gd->private_data = info;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001154 set_capacity(gd, capacity);
1155
Bob Liu172335a2016-07-01 17:43:39 -04001156 if (xlvbd_init_blk_queue(gd, sector_size, physical_sector_size)) {
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001157 del_gendisk(gd);
Jan Beulich0e345822010-08-07 18:28:55 +02001158 goto release;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001159 }
1160
Tejun Heo4913efe2010-09-03 11:56:16 +02001161 xlvbd_flush(info);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001162
1163 if (vdisk_info & VDISK_READONLY)
1164 set_disk_ro(gd, 1);
1165
1166 if (vdisk_info & VDISK_REMOVABLE)
1167 gd->flags |= GENHD_FL_REMOVABLE;
1168
1169 if (vdisk_info & VDISK_CDROM)
1170 gd->flags |= GENHD_FL_CD;
1171
1172 return 0;
1173
Jan Beulich0e345822010-08-07 18:28:55 +02001174 release:
1175 xlbd_release_minors(minor, nr_minors);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001176 out:
1177 return err;
1178}
1179
Daniel Stoddena66b5ae2010-08-07 18:33:17 +02001180static void xlvbd_release_gendisk(struct blkfront_info *info)
1181{
Bob Liu3df0e502015-11-14 11:12:12 +08001182 unsigned int minor, nr_minors, i;
Daniel Stoddena66b5ae2010-08-07 18:33:17 +02001183
1184 if (info->rq == NULL)
1185 return;
1186
Daniel Stoddena66b5ae2010-08-07 18:33:17 +02001187 /* No more blkif_request(). */
Bob Liu907c3eb2015-07-13 17:55:24 +08001188 blk_mq_stop_hw_queues(info->rq);
Daniel Stoddena66b5ae2010-08-07 18:33:17 +02001189
Bob Liu3df0e502015-11-14 11:12:12 +08001190 for (i = 0; i < info->nr_rings; i++) {
1191 struct blkfront_ring_info *rinfo = &info->rinfo[i];
Daniel Stoddena66b5ae2010-08-07 18:33:17 +02001192
Bob Liu3df0e502015-11-14 11:12:12 +08001193 /* No more gnttab callback work. */
1194 gnttab_cancel_free_callback(&rinfo->callback);
1195
1196 /* Flush gnttab callback work. Must be done with no locks held. */
1197 flush_work(&rinfo->work);
1198 }
Daniel Stoddena66b5ae2010-08-07 18:33:17 +02001199
1200 del_gendisk(info->gd);
1201
1202 minor = info->gd->first_minor;
1203 nr_minors = info->gd->minors;
1204 xlbd_release_minors(minor, nr_minors);
1205
1206 blk_cleanup_queue(info->rq);
Bob Liu907c3eb2015-07-13 17:55:24 +08001207 blk_mq_free_tag_set(&info->tag_set);
Daniel Stoddena66b5ae2010-08-07 18:33:17 +02001208 info->rq = NULL;
1209
1210 put_disk(info->gd);
1211 info->gd = NULL;
1212}
1213
Bob Liu11659562015-11-14 11:12:13 +08001214/* Already hold rinfo->ring_lock. */
1215static inline void kick_pending_request_queues_locked(struct blkfront_ring_info *rinfo)
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001216{
Bob Liu81f35162015-11-14 11:12:11 +08001217 if (!RING_FULL(&rinfo->ring))
1218 blk_mq_start_stopped_hw_queues(rinfo->dev_info->rq, true);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001219}
1220
Bob Liu11659562015-11-14 11:12:13 +08001221static void kick_pending_request_queues(struct blkfront_ring_info *rinfo)
1222{
1223 unsigned long flags;
1224
1225 spin_lock_irqsave(&rinfo->ring_lock, flags);
1226 kick_pending_request_queues_locked(rinfo);
1227 spin_unlock_irqrestore(&rinfo->ring_lock, flags);
1228}
1229
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001230static void blkif_restart_queue(struct work_struct *work)
1231{
Bob Liu81f35162015-11-14 11:12:11 +08001232 struct blkfront_ring_info *rinfo = container_of(work, struct blkfront_ring_info, work);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001233
Bob Liu81f35162015-11-14 11:12:11 +08001234 if (rinfo->dev_info->connected == BLKIF_STATE_CONNECTED)
1235 kick_pending_request_queues(rinfo);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001236}
1237
Bob Liu3df0e502015-11-14 11:12:12 +08001238static void blkif_free_ring(struct blkfront_ring_info *rinfo)
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001239{
Bob Liu73716df2015-11-16 16:51:39 -05001240 struct grant *persistent_gnt, *n;
Bob Liu3df0e502015-11-14 11:12:12 +08001241 struct blkfront_info *info = rinfo->dev_info;
Roger Pau Monne402b27f2013-04-18 16:06:54 +02001242 int i, j, segs;
Roger Pau Monne0a8704a2012-10-24 18:58:45 +02001243
Roger Pau Monnebfe11d62013-10-29 18:31:14 +01001244 /*
1245 * Remove indirect pages, this only happens when using indirect
1246 * descriptors but not persistent grants
1247 */
Bob Liu81f35162015-11-14 11:12:11 +08001248 if (!list_empty(&rinfo->indirect_pages)) {
Roger Pau Monnebfe11d62013-10-29 18:31:14 +01001249 struct page *indirect_page, *n;
1250
1251 BUG_ON(info->feature_persistent);
Bob Liu81f35162015-11-14 11:12:11 +08001252 list_for_each_entry_safe(indirect_page, n, &rinfo->indirect_pages, lru) {
Roger Pau Monnebfe11d62013-10-29 18:31:14 +01001253 list_del(&indirect_page->lru);
1254 __free_page(indirect_page);
1255 }
1256 }
1257
Bob Liu73716df2015-11-16 16:51:39 -05001258 /* Remove all persistent grants. */
1259 if (!list_empty(&rinfo->grants)) {
1260 list_for_each_entry_safe(persistent_gnt, n,
1261 &rinfo->grants, node) {
1262 list_del(&persistent_gnt->node);
1263 if (persistent_gnt->gref != GRANT_INVALID_REF) {
1264 gnttab_end_foreign_access(persistent_gnt->gref,
1265 0, 0UL);
1266 rinfo->persistent_gnts_c--;
1267 }
1268 if (info->feature_persistent)
1269 __free_page(persistent_gnt->page);
1270 kfree(persistent_gnt);
1271 }
1272 }
1273 BUG_ON(rinfo->persistent_gnts_c != 0);
1274
Bob Liu86839c52015-06-03 13:40:03 +08001275 for (i = 0; i < BLK_RING_SIZE(info); i++) {
Roger Pau Monne402b27f2013-04-18 16:06:54 +02001276 /*
1277 * Clear persistent grants present in requests already
1278 * on the shared ring
1279 */
Bob Liu81f35162015-11-14 11:12:11 +08001280 if (!rinfo->shadow[i].request)
Roger Pau Monne402b27f2013-04-18 16:06:54 +02001281 goto free_shadow;
1282
Bob Liu81f35162015-11-14 11:12:11 +08001283 segs = rinfo->shadow[i].req.operation == BLKIF_OP_INDIRECT ?
1284 rinfo->shadow[i].req.u.indirect.nr_segments :
1285 rinfo->shadow[i].req.u.rw.nr_segments;
Roger Pau Monne402b27f2013-04-18 16:06:54 +02001286 for (j = 0; j < segs; j++) {
Bob Liu81f35162015-11-14 11:12:11 +08001287 persistent_gnt = rinfo->shadow[i].grants_used[j];
Roger Pau Monne402b27f2013-04-18 16:06:54 +02001288 gnttab_end_foreign_access(persistent_gnt->gref, 0, 0UL);
Roger Pau Monnebfe11d62013-10-29 18:31:14 +01001289 if (info->feature_persistent)
Julien Gralla7a6df22015-06-30 11:58:51 +01001290 __free_page(persistent_gnt->page);
Roger Pau Monne402b27f2013-04-18 16:06:54 +02001291 kfree(persistent_gnt);
1292 }
1293
Bob Liu81f35162015-11-14 11:12:11 +08001294 if (rinfo->shadow[i].req.operation != BLKIF_OP_INDIRECT)
Roger Pau Monne402b27f2013-04-18 16:06:54 +02001295 /*
1296 * If this is not an indirect operation don't try to
1297 * free indirect segments
1298 */
1299 goto free_shadow;
1300
1301 for (j = 0; j < INDIRECT_GREFS(segs); j++) {
Bob Liu81f35162015-11-14 11:12:11 +08001302 persistent_gnt = rinfo->shadow[i].indirect_grants[j];
Roger Pau Monne402b27f2013-04-18 16:06:54 +02001303 gnttab_end_foreign_access(persistent_gnt->gref, 0, 0UL);
Julien Gralla7a6df22015-06-30 11:58:51 +01001304 __free_page(persistent_gnt->page);
Roger Pau Monne402b27f2013-04-18 16:06:54 +02001305 kfree(persistent_gnt);
1306 }
1307
1308free_shadow:
Bob Liu81f35162015-11-14 11:12:11 +08001309 kfree(rinfo->shadow[i].grants_used);
1310 rinfo->shadow[i].grants_used = NULL;
1311 kfree(rinfo->shadow[i].indirect_grants);
1312 rinfo->shadow[i].indirect_grants = NULL;
1313 kfree(rinfo->shadow[i].sg);
1314 rinfo->shadow[i].sg = NULL;
Roger Pau Monne402b27f2013-04-18 16:06:54 +02001315 }
1316
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001317 /* No more gnttab callback work. */
Bob Liu81f35162015-11-14 11:12:11 +08001318 gnttab_cancel_free_callback(&rinfo->callback);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001319
1320 /* Flush gnttab callback work. Must be done with no locks held. */
Bob Liu81f35162015-11-14 11:12:11 +08001321 flush_work(&rinfo->work);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001322
1323 /* Free resources associated with old device channel. */
Bob Liu86839c52015-06-03 13:40:03 +08001324 for (i = 0; i < info->nr_ring_pages; i++) {
Bob Liu81f35162015-11-14 11:12:11 +08001325 if (rinfo->ring_ref[i] != GRANT_INVALID_REF) {
1326 gnttab_end_foreign_access(rinfo->ring_ref[i], 0, 0);
1327 rinfo->ring_ref[i] = GRANT_INVALID_REF;
Bob Liu86839c52015-06-03 13:40:03 +08001328 }
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001329 }
Bob Liu6c647b02016-07-01 15:45:57 -04001330 free_pages((unsigned long)rinfo->ring.sring, get_order(info->nr_ring_pages * XEN_PAGE_SIZE));
Bob Liu81f35162015-11-14 11:12:11 +08001331 rinfo->ring.sring = NULL;
Bob Liu86839c52015-06-03 13:40:03 +08001332
Bob Liu81f35162015-11-14 11:12:11 +08001333 if (rinfo->irq)
1334 unbind_from_irqhandler(rinfo->irq, rinfo);
1335 rinfo->evtchn = rinfo->irq = 0;
Bob Liu3df0e502015-11-14 11:12:12 +08001336}
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001337
Bob Liu3df0e502015-11-14 11:12:12 +08001338static void blkif_free(struct blkfront_info *info, int suspend)
1339{
Bob Liu3df0e502015-11-14 11:12:12 +08001340 unsigned int i;
1341
1342 /* Prevent new requests being issued until we fix things up. */
Bob Liu3df0e502015-11-14 11:12:12 +08001343 info->connected = suspend ?
1344 BLKIF_STATE_SUSPENDED : BLKIF_STATE_DISCONNECTED;
1345 /* No more blkif_request(). */
1346 if (info->rq)
1347 blk_mq_stop_hw_queues(info->rq);
1348
Bob Liu3df0e502015-11-14 11:12:12 +08001349 for (i = 0; i < info->nr_rings; i++)
1350 blkif_free_ring(&info->rinfo[i]);
1351
1352 kfree(info->rinfo);
1353 info->rinfo = NULL;
1354 info->nr_rings = 0;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001355}
1356
Julien Grallc004a6f2015-07-22 16:44:54 +01001357struct copy_from_grant {
1358 const struct blk_shadow *s;
1359 unsigned int grant_idx;
1360 unsigned int bvec_offset;
1361 char *bvec_data;
1362};
1363
1364static void blkif_copy_from_grant(unsigned long gfn, unsigned int offset,
1365 unsigned int len, void *data)
1366{
1367 struct copy_from_grant *info = data;
1368 char *shared_data;
1369 /* Convenient aliases */
1370 const struct blk_shadow *s = info->s;
1371
1372 shared_data = kmap_atomic(s->grants_used[info->grant_idx]->page);
1373
1374 memcpy(info->bvec_data + info->bvec_offset,
1375 shared_data + offset, len);
1376
1377 info->bvec_offset += len;
1378 info->grant_idx++;
1379
1380 kunmap_atomic(shared_data);
1381}
1382
Julien Grall6cc568332015-08-13 13:13:35 +01001383static enum blk_req_status blkif_rsp_to_req_status(int rsp)
1384{
1385 switch (rsp)
1386 {
1387 case BLKIF_RSP_OKAY:
1388 return REQ_DONE;
1389 case BLKIF_RSP_EOPNOTSUPP:
1390 return REQ_EOPNOTSUPP;
1391 case BLKIF_RSP_ERROR:
1392 /* Fallthrough. */
1393 default:
1394 return REQ_ERROR;
1395 }
1396}
1397
1398/*
1399 * Get the final status of the block request based on two ring response
1400 */
1401static int blkif_get_final_status(enum blk_req_status s1,
1402 enum blk_req_status s2)
1403{
1404 BUG_ON(s1 == REQ_WAITING);
1405 BUG_ON(s2 == REQ_WAITING);
1406
1407 if (s1 == REQ_ERROR || s2 == REQ_ERROR)
1408 return BLKIF_RSP_ERROR;
1409 else if (s1 == REQ_EOPNOTSUPP || s2 == REQ_EOPNOTSUPP)
1410 return BLKIF_RSP_EOPNOTSUPP;
1411 return BLKIF_RSP_OKAY;
1412}
1413
1414static bool blkif_completion(unsigned long *id,
1415 struct blkfront_ring_info *rinfo,
Roger Pau Monne0a8704a2012-10-24 18:58:45 +02001416 struct blkif_response *bret)
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001417{
Roger Pau Monned62f6912012-12-07 19:00:31 +01001418 int i = 0;
Roger Pau Monneb7649152013-05-02 10:58:50 +02001419 struct scatterlist *sg;
Julien Grallc004a6f2015-07-22 16:44:54 +01001420 int num_sg, num_grant;
Bob Liu81f35162015-11-14 11:12:11 +08001421 struct blkfront_info *info = rinfo->dev_info;
Julien Grall6cc568332015-08-13 13:13:35 +01001422 struct blk_shadow *s = &rinfo->shadow[*id];
Julien Grallc004a6f2015-07-22 16:44:54 +01001423 struct copy_from_grant data = {
Julien Grallc004a6f2015-07-22 16:44:54 +01001424 .grant_idx = 0,
1425 };
Roger Pau Monne402b27f2013-04-18 16:06:54 +02001426
Julien Grallc004a6f2015-07-22 16:44:54 +01001427 num_grant = s->req.operation == BLKIF_OP_INDIRECT ?
Roger Pau Monne402b27f2013-04-18 16:06:54 +02001428 s->req.u.indirect.nr_segments : s->req.u.rw.nr_segments;
Julien Grall6cc568332015-08-13 13:13:35 +01001429
1430 /* The I/O request may be split in two. */
1431 if (unlikely(s->associated_id != NO_ASSOCIATED_ID)) {
1432 struct blk_shadow *s2 = &rinfo->shadow[s->associated_id];
1433
1434 /* Keep the status of the current response in shadow. */
1435 s->status = blkif_rsp_to_req_status(bret->status);
1436
1437 /* Wait the second response if not yet here. */
1438 if (s2->status == REQ_WAITING)
Gustavo A. R. Silvaf87c30c2018-08-06 08:14:55 -06001439 return false;
Julien Grall6cc568332015-08-13 13:13:35 +01001440
1441 bret->status = blkif_get_final_status(s->status,
1442 s2->status);
1443
1444 /*
1445 * All the grants is stored in the first shadow in order
1446 * to make the completion code simpler.
1447 */
1448 num_grant += s2->req.u.rw.nr_segments;
1449
1450 /*
1451 * The two responses may not come in order. Only the
1452 * first request will store the scatter-gather list.
1453 */
1454 if (s2->num_sg != 0) {
1455 /* Update "id" with the ID of the first response. */
1456 *id = s->associated_id;
1457 s = s2;
1458 }
1459
1460 /*
1461 * We don't need anymore the second request, so recycling
1462 * it now.
1463 */
1464 if (add_id_to_freelist(rinfo, s->associated_id))
1465 WARN(1, "%s: can't recycle the second part (id = %ld) of the request\n",
1466 info->gd->disk_name, s->associated_id);
1467 }
1468
1469 data.s = s;
Julien Grallc004a6f2015-07-22 16:44:54 +01001470 num_sg = s->num_sg;
Roger Pau Monne0a8704a2012-10-24 18:58:45 +02001471
Roger Pau Monnebfe11d62013-10-29 18:31:14 +01001472 if (bret->operation == BLKIF_OP_READ && info->feature_persistent) {
Julien Grallc004a6f2015-07-22 16:44:54 +01001473 for_each_sg(s->sg, sg, num_sg, i) {
Roger Pau Monneb7649152013-05-02 10:58:50 +02001474 BUG_ON(sg->offset + sg->length > PAGE_SIZE);
Julien Grallc004a6f2015-07-22 16:44:54 +01001475
1476 data.bvec_offset = sg->offset;
1477 data.bvec_data = kmap_atomic(sg_page(sg));
1478
1479 gnttab_foreach_grant_in_range(sg_page(sg),
1480 sg->offset,
1481 sg->length,
1482 blkif_copy_from_grant,
1483 &data);
1484
1485 kunmap_atomic(data.bvec_data);
Roger Pau Monne0a8704a2012-10-24 18:58:45 +02001486 }
1487 }
1488 /* Add the persistent grant into the list of free grants */
Julien Grallc004a6f2015-07-22 16:44:54 +01001489 for (i = 0; i < num_grant; i++) {
Roger Pau Monnefbe363c2013-08-12 12:53:44 +02001490 if (gnttab_query_foreign_access(s->grants_used[i]->gref)) {
1491 /*
1492 * If the grant is still mapped by the backend (the
1493 * backend has chosen to make this grant persistent)
1494 * we add it at the head of the list, so it will be
1495 * reused first.
1496 */
Roger Pau Monnebfe11d62013-10-29 18:31:14 +01001497 if (!info->feature_persistent)
1498 pr_alert_ratelimited("backed has not unmapped grant: %u\n",
1499 s->grants_used[i]->gref);
Bob Liu73716df2015-11-16 16:51:39 -05001500 list_add(&s->grants_used[i]->node, &rinfo->grants);
1501 rinfo->persistent_gnts_c++;
Roger Pau Monnefbe363c2013-08-12 12:53:44 +02001502 } else {
1503 /*
1504 * If the grant is not mapped by the backend we end the
1505 * foreign access and add it to the tail of the list,
1506 * so it will not be picked again unless we run out of
1507 * persistent grants.
1508 */
1509 gnttab_end_foreign_access(s->grants_used[i]->gref, 0, 0UL);
1510 s->grants_used[i]->gref = GRANT_INVALID_REF;
Bob Liu73716df2015-11-16 16:51:39 -05001511 list_add_tail(&s->grants_used[i]->node, &rinfo->grants);
Roger Pau Monnefbe363c2013-08-12 12:53:44 +02001512 }
Roger Pau Monne0a8704a2012-10-24 18:58:45 +02001513 }
Roger Pau Monne402b27f2013-04-18 16:06:54 +02001514 if (s->req.operation == BLKIF_OP_INDIRECT) {
Julien Grallc004a6f2015-07-22 16:44:54 +01001515 for (i = 0; i < INDIRECT_GREFS(num_grant); i++) {
Roger Pau Monnefbe363c2013-08-12 12:53:44 +02001516 if (gnttab_query_foreign_access(s->indirect_grants[i]->gref)) {
Roger Pau Monnebfe11d62013-10-29 18:31:14 +01001517 if (!info->feature_persistent)
1518 pr_alert_ratelimited("backed has not unmapped grant: %u\n",
1519 s->indirect_grants[i]->gref);
Bob Liu73716df2015-11-16 16:51:39 -05001520 list_add(&s->indirect_grants[i]->node, &rinfo->grants);
1521 rinfo->persistent_gnts_c++;
Roger Pau Monnefbe363c2013-08-12 12:53:44 +02001522 } else {
Roger Pau Monnebfe11d62013-10-29 18:31:14 +01001523 struct page *indirect_page;
1524
Roger Pau Monnefbe363c2013-08-12 12:53:44 +02001525 gnttab_end_foreign_access(s->indirect_grants[i]->gref, 0, 0UL);
Roger Pau Monnebfe11d62013-10-29 18:31:14 +01001526 /*
1527 * Add the used indirect page back to the list of
1528 * available pages for indirect grefs.
1529 */
Bob Liu7b076752015-07-22 14:40:09 +08001530 if (!info->feature_persistent) {
Julien Gralla7a6df22015-06-30 11:58:51 +01001531 indirect_page = s->indirect_grants[i]->page;
Bob Liu81f35162015-11-14 11:12:11 +08001532 list_add(&indirect_page->lru, &rinfo->indirect_pages);
Bob Liu7b076752015-07-22 14:40:09 +08001533 }
Roger Pau Monnefbe363c2013-08-12 12:53:44 +02001534 s->indirect_grants[i]->gref = GRANT_INVALID_REF;
Bob Liu73716df2015-11-16 16:51:39 -05001535 list_add_tail(&s->indirect_grants[i]->node, &rinfo->grants);
Roger Pau Monnefbe363c2013-08-12 12:53:44 +02001536 }
Roger Pau Monne402b27f2013-04-18 16:06:54 +02001537 }
1538 }
Julien Grall6cc568332015-08-13 13:13:35 +01001539
Gustavo A. R. Silvaf87c30c2018-08-06 08:14:55 -06001540 return true;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001541}
1542
1543static irqreturn_t blkif_interrupt(int irq, void *dev_id)
1544{
1545 struct request *req;
1546 struct blkif_response *bret;
1547 RING_IDX i, rp;
1548 unsigned long flags;
Bob Liu81f35162015-11-14 11:12:11 +08001549 struct blkfront_ring_info *rinfo = (struct blkfront_ring_info *)dev_id;
1550 struct blkfront_info *info = rinfo->dev_info;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001551
Bob Liu11659562015-11-14 11:12:13 +08001552 if (unlikely(info->connected != BLKIF_STATE_CONNECTED))
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001553 return IRQ_HANDLED;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001554
Bob Liu11659562015-11-14 11:12:13 +08001555 spin_lock_irqsave(&rinfo->ring_lock, flags);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001556 again:
Bob Liu81f35162015-11-14 11:12:11 +08001557 rp = rinfo->ring.sring->rsp_prod;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001558 rmb(); /* Ensure we see queued responses up to 'rp'. */
1559
Bob Liu81f35162015-11-14 11:12:11 +08001560 for (i = rinfo->ring.rsp_cons; i != rp; i++) {
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001561 unsigned long id;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001562
Bob Liu81f35162015-11-14 11:12:11 +08001563 bret = RING_GET_RESPONSE(&rinfo->ring, i);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001564 id = bret->id;
Konrad Rzeszutek Wilk6878c322012-05-25 17:34:51 -04001565 /*
1566 * The backend has messed up and given us an id that we would
1567 * never have given to it (we stamp it up to BLK_RING_SIZE -
1568 * look in get_id_from_freelist.
1569 */
Bob Liu86839c52015-06-03 13:40:03 +08001570 if (id >= BLK_RING_SIZE(info)) {
Konrad Rzeszutek Wilk6878c322012-05-25 17:34:51 -04001571 WARN(1, "%s: response to %s has incorrect id (%ld)\n",
1572 info->gd->disk_name, op_name(bret->operation), id);
1573 /* We can't safely get the 'struct request' as
1574 * the id is busted. */
1575 continue;
1576 }
Bob Liu81f35162015-11-14 11:12:11 +08001577 req = rinfo->shadow[id].request;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001578
Julien Grall6cc568332015-08-13 13:13:35 +01001579 if (bret->operation != BLKIF_OP_DISCARD) {
1580 /*
1581 * We may need to wait for an extra response if the
1582 * I/O request is split in 2
1583 */
1584 if (!blkif_completion(&id, rinfo, bret))
1585 continue;
1586 }
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001587
Bob Liu81f35162015-11-14 11:12:11 +08001588 if (add_id_to_freelist(rinfo, id)) {
Konrad Rzeszutek Wilk6878c322012-05-25 17:34:51 -04001589 WARN(1, "%s: response to %s (id %ld) couldn't be recycled!\n",
1590 info->gd->disk_name, op_name(bret->operation), id);
1591 continue;
1592 }
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001593
Christoph Hellwig2a842ac2017-06-03 09:38:04 +02001594 if (bret->status == BLKIF_RSP_OKAY)
1595 blkif_req(req)->error = BLK_STS_OK;
1596 else
1597 blkif_req(req)->error = BLK_STS_IOERR;
1598
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001599 switch (bret->operation) {
Li Dongyanged30bf32011-09-01 18:39:09 +08001600 case BLKIF_OP_DISCARD:
1601 if (unlikely(bret->status == BLKIF_RSP_EOPNOTSUPP)) {
1602 struct request_queue *rq = info->rq;
Konrad Rzeszutek Wilk6878c322012-05-25 17:34:51 -04001603 printk(KERN_WARNING "blkfront: %s: %s op failed\n",
1604 info->gd->disk_name, op_name(bret->operation));
Christoph Hellwig2a842ac2017-06-03 09:38:04 +02001605 blkif_req(req)->error = BLK_STS_NOTSUPP;
Li Dongyanged30bf32011-09-01 18:39:09 +08001606 info->feature_discard = 0;
Konrad Rzeszutek Wilk5ea42982011-10-12 16:23:30 -04001607 info->feature_secdiscard = 0;
Bart Van Assche8b904b52018-03-07 17:10:10 -08001608 blk_queue_flag_clear(QUEUE_FLAG_DISCARD, rq);
1609 blk_queue_flag_clear(QUEUE_FLAG_SECERASE, rq);
Li Dongyanged30bf32011-09-01 18:39:09 +08001610 }
Li Dongyanged30bf32011-09-01 18:39:09 +08001611 break;
Konrad Rzeszutek Wilkedf6ef52011-05-03 12:01:11 -04001612 case BLKIF_OP_FLUSH_DISKCACHE:
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001613 case BLKIF_OP_WRITE_BARRIER:
1614 if (unlikely(bret->status == BLKIF_RSP_EOPNOTSUPP)) {
Konrad Rzeszutek Wilk6878c322012-05-25 17:34:51 -04001615 printk(KERN_WARNING "blkfront: %s: %s op failed\n",
1616 info->gd->disk_name, op_name(bret->operation));
Bart Van Assche31c4ccc2017-07-21 19:11:10 +02001617 blkif_req(req)->error = BLK_STS_NOTSUPP;
Jeremy Fitzhardingedcb8bae2010-11-02 11:55:58 -04001618 }
1619 if (unlikely(bret->status == BLKIF_RSP_ERROR &&
Bob Liu81f35162015-11-14 11:12:11 +08001620 rinfo->shadow[id].req.u.rw.nr_segments == 0)) {
Konrad Rzeszutek Wilk6878c322012-05-25 17:34:51 -04001621 printk(KERN_WARNING "blkfront: %s: empty %s op failed\n",
1622 info->gd->disk_name, op_name(bret->operation));
Christoph Hellwig2a842ac2017-06-03 09:38:04 +02001623 blkif_req(req)->error = BLK_STS_NOTSUPP;
Jeremy Fitzhardingedcb8bae2010-11-02 11:55:58 -04001624 }
Christoph Hellwig26095872017-04-20 16:03:08 +02001625 if (unlikely(blkif_req(req)->error)) {
Christoph Hellwig2a842ac2017-06-03 09:38:04 +02001626 if (blkif_req(req)->error == BLK_STS_NOTSUPP)
1627 blkif_req(req)->error = BLK_STS_OK;
Mike Christiea4180902016-06-05 14:32:24 -05001628 info->feature_fua = 0;
Tejun Heo4913efe2010-09-03 11:56:16 +02001629 info->feature_flush = 0;
1630 xlvbd_flush(info);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001631 }
1632 /* fall through */
1633 case BLKIF_OP_READ:
1634 case BLKIF_OP_WRITE:
1635 if (unlikely(bret->status != BLKIF_RSP_OKAY))
1636 dev_dbg(&info->xbdev->dev, "Bad return from blkdev data "
1637 "request: %x\n", bret->status);
1638
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001639 break;
1640 default:
1641 BUG();
1642 }
Christoph Hellwig26095872017-04-20 16:03:08 +02001643
Christoph Hellwig08e00292017-04-20 16:03:09 +02001644 blk_mq_complete_request(req);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001645 }
1646
Bob Liu81f35162015-11-14 11:12:11 +08001647 rinfo->ring.rsp_cons = i;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001648
Bob Liu81f35162015-11-14 11:12:11 +08001649 if (i != rinfo->ring.req_prod_pvt) {
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001650 int more_to_do;
Bob Liu81f35162015-11-14 11:12:11 +08001651 RING_FINAL_CHECK_FOR_RESPONSES(&rinfo->ring, more_to_do);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001652 if (more_to_do)
1653 goto again;
1654 } else
Bob Liu81f35162015-11-14 11:12:11 +08001655 rinfo->ring.sring->rsp_event = i + 1;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001656
Bob Liu11659562015-11-14 11:12:13 +08001657 kick_pending_request_queues_locked(rinfo);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001658
Bob Liu11659562015-11-14 11:12:13 +08001659 spin_unlock_irqrestore(&rinfo->ring_lock, flags);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001660
1661 return IRQ_HANDLED;
1662}
1663
1664
1665static int setup_blkring(struct xenbus_device *dev,
Bob Liu81f35162015-11-14 11:12:11 +08001666 struct blkfront_ring_info *rinfo)
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001667{
1668 struct blkif_sring *sring;
Bob Liu86839c52015-06-03 13:40:03 +08001669 int err, i;
Bob Liu81f35162015-11-14 11:12:11 +08001670 struct blkfront_info *info = rinfo->dev_info;
Julien Grallc004a6f2015-07-22 16:44:54 +01001671 unsigned long ring_size = info->nr_ring_pages * XEN_PAGE_SIZE;
Julien Grall9cce2912015-10-13 17:50:11 +01001672 grant_ref_t gref[XENBUS_MAX_RING_GRANTS];
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001673
Bob Liu86839c52015-06-03 13:40:03 +08001674 for (i = 0; i < info->nr_ring_pages; i++)
Bob Liu81f35162015-11-14 11:12:11 +08001675 rinfo->ring_ref[i] = GRANT_INVALID_REF;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001676
Bob Liu86839c52015-06-03 13:40:03 +08001677 sring = (struct blkif_sring *)__get_free_pages(GFP_NOIO | __GFP_HIGH,
1678 get_order(ring_size));
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001679 if (!sring) {
1680 xenbus_dev_fatal(dev, -ENOMEM, "allocating shared ring");
1681 return -ENOMEM;
1682 }
1683 SHARED_RING_INIT(sring);
Bob Liu81f35162015-11-14 11:12:11 +08001684 FRONT_RING_INIT(&rinfo->ring, sring, ring_size);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001685
Bob Liu81f35162015-11-14 11:12:11 +08001686 err = xenbus_grant_ring(dev, rinfo->ring.sring, info->nr_ring_pages, gref);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001687 if (err < 0) {
Bob Liu86839c52015-06-03 13:40:03 +08001688 free_pages((unsigned long)sring, get_order(ring_size));
Bob Liu81f35162015-11-14 11:12:11 +08001689 rinfo->ring.sring = NULL;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001690 goto fail;
1691 }
Bob Liu86839c52015-06-03 13:40:03 +08001692 for (i = 0; i < info->nr_ring_pages; i++)
Bob Liu81f35162015-11-14 11:12:11 +08001693 rinfo->ring_ref[i] = gref[i];
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001694
Bob Liu81f35162015-11-14 11:12:11 +08001695 err = xenbus_alloc_evtchn(dev, &rinfo->evtchn);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001696 if (err)
1697 goto fail;
1698
Bob Liu81f35162015-11-14 11:12:11 +08001699 err = bind_evtchn_to_irqhandler(rinfo->evtchn, blkif_interrupt, 0,
1700 "blkif", rinfo);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001701 if (err <= 0) {
1702 xenbus_dev_fatal(dev, err,
1703 "bind_evtchn_to_irqhandler failed");
1704 goto fail;
1705 }
Bob Liu81f35162015-11-14 11:12:11 +08001706 rinfo->irq = err;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001707
1708 return 0;
1709fail:
1710 blkif_free(info, 0);
1711 return err;
1712}
1713
Bob Liu28d949b2015-11-14 11:12:14 +08001714/*
1715 * Write out per-ring/queue nodes including ring-ref and event-channel, and each
1716 * ring buffer may have multi pages depending on ->nr_ring_pages.
1717 */
1718static int write_per_ring_nodes(struct xenbus_transaction xbt,
1719 struct blkfront_ring_info *rinfo, const char *dir)
1720{
1721 int err;
1722 unsigned int i;
1723 const char *message = NULL;
1724 struct blkfront_info *info = rinfo->dev_info;
1725
1726 if (info->nr_ring_pages == 1) {
1727 err = xenbus_printf(xbt, dir, "ring-ref", "%u", rinfo->ring_ref[0]);
1728 if (err) {
1729 message = "writing ring-ref";
1730 goto abort_transaction;
1731 }
1732 } else {
1733 for (i = 0; i < info->nr_ring_pages; i++) {
1734 char ring_ref_name[RINGREF_NAME_LEN];
1735
1736 snprintf(ring_ref_name, RINGREF_NAME_LEN, "ring-ref%u", i);
1737 err = xenbus_printf(xbt, dir, ring_ref_name,
1738 "%u", rinfo->ring_ref[i]);
1739 if (err) {
1740 message = "writing ring-ref";
1741 goto abort_transaction;
1742 }
1743 }
1744 }
1745
1746 err = xenbus_printf(xbt, dir, "event-channel", "%u", rinfo->evtchn);
1747 if (err) {
1748 message = "writing event-channel";
1749 goto abort_transaction;
1750 }
1751
1752 return 0;
1753
1754abort_transaction:
1755 xenbus_transaction_end(xbt, 1);
1756 if (message)
1757 xenbus_dev_fatal(info->xbdev, err, "%s", message);
1758
1759 return err;
1760}
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001761
1762/* Common code used when first setting up, and when resuming. */
Ian Campbell203fd612009-12-04 15:33:54 +00001763static int talk_to_blkback(struct xenbus_device *dev,
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001764 struct blkfront_info *info)
1765{
1766 const char *message = NULL;
1767 struct xenbus_transaction xbt;
Bob Liu28d949b2015-11-14 11:12:14 +08001768 int err;
Juergen Grossf27dc1a2016-10-31 14:58:40 +01001769 unsigned int i, max_page_order;
1770 unsigned int ring_page_order;
Bob Liu86839c52015-06-03 13:40:03 +08001771
Bhavesh Davda7ed8ce12017-12-22 14:17:13 -08001772 if (!info)
1773 return -ENODEV;
1774
Juergen Grossf27dc1a2016-10-31 14:58:40 +01001775 max_page_order = xenbus_read_unsigned(info->xbdev->otherend,
1776 "max-ring-page-order", 0);
1777 ring_page_order = min(xen_blkif_max_ring_order, max_page_order);
1778 info->nr_ring_pages = 1 << ring_page_order;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001779
Bhavesh Davda7ed8ce12017-12-22 14:17:13 -08001780 err = negotiate_mq(info);
1781 if (err)
1782 goto destroy_blkring;
1783
Bob Liu3df0e502015-11-14 11:12:12 +08001784 for (i = 0; i < info->nr_rings; i++) {
Bob Liu28d949b2015-11-14 11:12:14 +08001785 struct blkfront_ring_info *rinfo = &info->rinfo[i];
1786
Bob Liu3df0e502015-11-14 11:12:12 +08001787 /* Create shared ring, alloc event channel. */
1788 err = setup_blkring(dev, rinfo);
1789 if (err)
1790 goto destroy_blkring;
1791 }
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001792
1793again:
1794 err = xenbus_transaction_start(&xbt);
1795 if (err) {
1796 xenbus_dev_fatal(dev, err, "starting transaction");
1797 goto destroy_blkring;
1798 }
1799
Bob Liu28d949b2015-11-14 11:12:14 +08001800 if (info->nr_ring_pages > 1) {
1801 err = xenbus_printf(xbt, dev->nodename, "ring-page-order", "%u",
1802 ring_page_order);
Bob Liu3df0e502015-11-14 11:12:12 +08001803 if (err) {
Bob Liu28d949b2015-11-14 11:12:14 +08001804 message = "writing ring-page-order";
Bob Liu3df0e502015-11-14 11:12:12 +08001805 goto abort_transaction;
1806 }
Bob Liu28d949b2015-11-14 11:12:14 +08001807 }
1808
1809 /* We already got the number of queues/rings in _probe */
1810 if (info->nr_rings == 1) {
1811 err = write_per_ring_nodes(xbt, &info->rinfo[0], dev->nodename);
1812 if (err)
1813 goto destroy_blkring;
Bob Liu3df0e502015-11-14 11:12:12 +08001814 } else {
Bob Liu28d949b2015-11-14 11:12:14 +08001815 char *path;
1816 size_t pathsize;
1817
1818 err = xenbus_printf(xbt, dev->nodename, "multi-queue-num-queues", "%u",
1819 info->nr_rings);
1820 if (err) {
1821 message = "writing multi-queue-num-queues";
1822 goto abort_transaction;
1823 }
1824
1825 pathsize = strlen(dev->nodename) + QUEUE_NAME_LEN;
1826 path = kmalloc(pathsize, GFP_KERNEL);
1827 if (!path) {
1828 err = -ENOMEM;
1829 message = "ENOMEM while writing ring references";
1830 goto abort_transaction;
1831 }
1832
1833 for (i = 0; i < info->nr_rings; i++) {
1834 memset(path, 0, pathsize);
1835 snprintf(path, pathsize, "%s/queue-%u", dev->nodename, i);
1836 err = write_per_ring_nodes(xbt, &info->rinfo[i], path);
1837 if (err) {
1838 kfree(path);
1839 goto destroy_blkring;
1840 }
1841 }
1842 kfree(path);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001843 }
Markus Armbruster3e334232008-04-02 10:54:02 -07001844 err = xenbus_printf(xbt, dev->nodename, "protocol", "%s",
1845 XEN_IO_PROTO_ABI_NATIVE);
1846 if (err) {
1847 message = "writing protocol";
1848 goto abort_transaction;
1849 }
Roger Pau Monne0a8704a2012-10-24 18:58:45 +02001850 err = xenbus_printf(xbt, dev->nodename,
Roger Pau Monnecb5bd4d2012-11-02 16:43:04 +01001851 "feature-persistent", "%u", 1);
Roger Pau Monne0a8704a2012-10-24 18:58:45 +02001852 if (err)
1853 dev_warn(&dev->dev,
1854 "writing persistent grants feature to xenbus");
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001855
1856 err = xenbus_transaction_end(xbt, 0);
1857 if (err) {
1858 if (err == -EAGAIN)
1859 goto again;
1860 xenbus_dev_fatal(dev, err, "completing transaction");
1861 goto destroy_blkring;
1862 }
1863
Bob Liu3df0e502015-11-14 11:12:12 +08001864 for (i = 0; i < info->nr_rings; i++) {
1865 unsigned int j;
Bob Liu28d949b2015-11-14 11:12:14 +08001866 struct blkfront_ring_info *rinfo = &info->rinfo[i];
Bob Liu3df0e502015-11-14 11:12:12 +08001867
1868 for (j = 0; j < BLK_RING_SIZE(info); j++)
1869 rinfo->shadow[j].req.u.rw.id = j + 1;
1870 rinfo->shadow[BLK_RING_SIZE(info)-1].req.u.rw.id = 0x0fffffff;
1871 }
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001872 xenbus_switch_state(dev, XenbusStateInitialised);
1873
1874 return 0;
1875
1876 abort_transaction:
1877 xenbus_transaction_end(xbt, 1);
1878 if (message)
1879 xenbus_dev_fatal(dev, err, "%s", message);
1880 destroy_blkring:
1881 blkif_free(info, 0);
Bob Liu3df0e502015-11-14 11:12:12 +08001882
Konrad Rzeszutek Wilkc31ecf62015-12-18 16:28:53 -05001883 kfree(info);
1884 dev_set_drvdata(&dev->dev, NULL);
1885
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001886 return err;
1887}
1888
Bob Liu3db70a82015-11-25 17:52:55 -05001889static int negotiate_mq(struct blkfront_info *info)
1890{
Juergen Grossf27dc1a2016-10-31 14:58:40 +01001891 unsigned int backend_max_queues;
Bob Liu3db70a82015-11-25 17:52:55 -05001892 unsigned int i;
1893
1894 BUG_ON(info->nr_rings);
1895
1896 /* Check if backend supports multiple queues. */
Juergen Grossf27dc1a2016-10-31 14:58:40 +01001897 backend_max_queues = xenbus_read_unsigned(info->xbdev->otherend,
1898 "multi-queue-max-queues", 1);
Bob Liu3db70a82015-11-25 17:52:55 -05001899 info->nr_rings = min(backend_max_queues, xen_blkif_max_queues);
1900 /* We need at least one ring. */
1901 if (!info->nr_rings)
1902 info->nr_rings = 1;
1903
Kees Cook6396bb22018-06-12 14:03:40 -07001904 info->rinfo = kcalloc(info->nr_rings,
1905 sizeof(struct blkfront_ring_info),
1906 GFP_KERNEL);
Bob Liu3db70a82015-11-25 17:52:55 -05001907 if (!info->rinfo) {
1908 xenbus_dev_fatal(info->xbdev, -ENOMEM, "allocating ring_info structure");
1909 return -ENOMEM;
1910 }
1911
1912 for (i = 0; i < info->nr_rings; i++) {
1913 struct blkfront_ring_info *rinfo;
1914
1915 rinfo = &info->rinfo[i];
1916 INIT_LIST_HEAD(&rinfo->indirect_pages);
1917 INIT_LIST_HEAD(&rinfo->grants);
1918 rinfo->dev_info = info;
1919 INIT_WORK(&rinfo->work, blkif_restart_queue);
1920 spin_lock_init(&rinfo->ring_lock);
1921 }
1922 return 0;
1923}
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001924/**
1925 * Entry point to this code when a new device is created. Allocate the basic
1926 * structures and the ring buffer for communication with the backend, and
1927 * inform the backend of the appropriate details for those. Switch to
1928 * Initialised state.
1929 */
1930static int blkfront_probe(struct xenbus_device *dev,
1931 const struct xenbus_device_id *id)
1932{
Bob Liu86839c52015-06-03 13:40:03 +08001933 int err, vdevice;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001934 struct blkfront_info *info;
1935
1936 /* FIXME: Use dynamic device id if this is not set. */
1937 err = xenbus_scanf(XBT_NIL, dev->nodename,
1938 "virtual-device", "%i", &vdevice);
1939 if (err != 1) {
Chris Lalancette9246b5f2008-09-17 14:30:32 -07001940 /* go looking in the extended area instead */
1941 err = xenbus_scanf(XBT_NIL, dev->nodename, "virtual-device-ext",
1942 "%i", &vdevice);
1943 if (err != 1) {
1944 xenbus_dev_fatal(dev, err, "reading virtual-device");
1945 return err;
1946 }
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001947 }
1948
Stefano Stabellinib98a4092010-07-29 14:53:16 +01001949 if (xen_hvm_domain()) {
1950 char *type;
1951 int len;
1952 /* no unplug has been done: do not hook devices != xen vbds */
Konrad Rzeszutek Wilk51c71a32013-11-26 15:05:40 -05001953 if (xen_has_pv_and_legacy_disk_devices()) {
Stefano Stabellinib98a4092010-07-29 14:53:16 +01001954 int major;
Stefano Stabellinic1c54132010-05-14 12:44:30 +01001955
Stefano Stabellinib98a4092010-07-29 14:53:16 +01001956 if (!VDEV_IS_EXTENDED(vdevice))
1957 major = BLKIF_MAJOR(vdevice);
1958 else
1959 major = XENVBD_MAJOR;
Stefano Stabellinic1c54132010-05-14 12:44:30 +01001960
Stefano Stabellinib98a4092010-07-29 14:53:16 +01001961 if (major != XENVBD_MAJOR) {
1962 printk(KERN_INFO
1963 "%s: HVM does not support vbd %d as xen block device\n",
Rasmus Villemoes02f1f212015-02-12 15:01:31 -08001964 __func__, vdevice);
Stefano Stabellinib98a4092010-07-29 14:53:16 +01001965 return -ENODEV;
1966 }
1967 }
1968 /* do not create a PV cdrom device if we are an HVM guest */
1969 type = xenbus_read(XBT_NIL, dev->nodename, "device-type", &len);
1970 if (IS_ERR(type))
1971 return -ENODEV;
1972 if (strncmp(type, "cdrom", 5) == 0) {
1973 kfree(type);
Stefano Stabellinic1c54132010-05-14 12:44:30 +01001974 return -ENODEV;
1975 }
Stefano Stabellinib98a4092010-07-29 14:53:16 +01001976 kfree(type);
Stefano Stabellinic1c54132010-05-14 12:44:30 +01001977 }
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001978 info = kzalloc(sizeof(*info), GFP_KERNEL);
1979 if (!info) {
1980 xenbus_dev_fatal(dev, -ENOMEM, "allocating info structure");
1981 return -ENOMEM;
1982 }
1983
Bob Liu28d949b2015-11-14 11:12:14 +08001984 info->xbdev = dev;
Bob Liu81f35162015-11-14 11:12:11 +08001985
Daniel Stoddenb70f5fa2010-04-30 22:01:19 +00001986 mutex_init(&info->mutex);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001987 info->vdevice = vdevice;
1988 info->connected = BLKIF_STATE_DISCONNECTED;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001989
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001990 /* Front end dir is a number, which is used as the id. */
1991 info->handle = simple_strtoul(strrchr(dev->nodename, '/')+1, NULL, 0);
Greg Kroah-Hartmana1b4b122009-04-30 14:43:31 -07001992 dev_set_drvdata(&dev->dev, info);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001993
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001994 return 0;
1995}
1996
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001997static int blkif_recover(struct blkfront_info *info)
1998{
NeilBrown4559fa52017-06-18 14:38:59 +10001999 unsigned int r_index;
Roger Pau Monne402b27f2013-04-18 16:06:54 +02002000 struct request *req, *n;
Roger Pau Monne402b27f2013-04-18 16:06:54 +02002001 int rc;
NeilBrown4559fa52017-06-18 14:38:59 +10002002 struct bio *bio;
2003 unsigned int segs;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07002004
Bob Liu3df0e502015-11-14 11:12:12 +08002005 blkfront_gather_backend_features(info);
Bob Liu172335a2016-07-01 17:43:39 -04002006 /* Reset limits changed by blk_mq_update_nr_hw_queues(). */
2007 blkif_set_queue_limits(info);
Roger Pau Monne402b27f2013-04-18 16:06:54 +02002008 segs = info->max_indirect_segments ? : BLKIF_MAX_SEGMENTS_PER_REQUEST;
Bob Liu6c647b02016-07-01 15:45:57 -04002009 blk_queue_max_segments(info->rq, segs / GRANTS_PER_PSEG);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07002010
Bob Liu3df0e502015-11-14 11:12:12 +08002011 for (r_index = 0; r_index < info->nr_rings; r_index++) {
Bob Liu7b427a52016-06-27 16:33:24 +08002012 struct blkfront_ring_info *rinfo = &info->rinfo[r_index];
Bob Liu3df0e502015-11-14 11:12:12 +08002013
2014 rc = blkfront_setup_indirect(rinfo);
Bob Liu7b427a52016-06-27 16:33:24 +08002015 if (rc)
Bob Liu3df0e502015-11-14 11:12:12 +08002016 return rc;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07002017 }
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07002018 xenbus_switch_state(info->xbdev, XenbusStateConnected);
2019
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07002020 /* Now safe for us to use the shared ring */
2021 info->connected = BLKIF_STATE_CONNECTED;
2022
Bob Liu3df0e502015-11-14 11:12:12 +08002023 for (r_index = 0; r_index < info->nr_rings; r_index++) {
2024 struct blkfront_ring_info *rinfo;
2025
2026 rinfo = &info->rinfo[r_index];
2027 /* Kick any other new requests queued since we resumed */
2028 kick_pending_request_queues(rinfo);
2029 }
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07002030
Bob Liu7b427a52016-06-27 16:33:24 +08002031 list_for_each_entry_safe(req, n, &info->requests, queuelist) {
Roger Pau Monne402b27f2013-04-18 16:06:54 +02002032 /* Requeue pending requests (flush or discard) */
2033 list_del_init(&req->queuelist);
2034 BUG_ON(req->nr_phys_segments > segs);
Bart Van Assche2b053ac2016-10-28 17:21:41 -07002035 blk_mq_requeue_request(req, false);
Roger Pau Monne402b27f2013-04-18 16:06:54 +02002036 }
Bart Van Assche52d7f1b2016-10-28 17:20:32 -07002037 blk_mq_start_stopped_hw_queues(info->rq, true);
Bob Liu907c3eb2015-07-13 17:55:24 +08002038 blk_mq_kick_requeue_list(info->rq);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07002039
Bob Liu7b427a52016-06-27 16:33:24 +08002040 while ((bio = bio_list_pop(&info->bio_list)) != NULL) {
Roger Pau Monne402b27f2013-04-18 16:06:54 +02002041 /* Traverse the list of pending bios and re-queue them */
Mike Christie4e49ea42016-06-05 14:31:41 -05002042 submit_bio(bio);
Roger Pau Monne402b27f2013-04-18 16:06:54 +02002043 }
2044
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07002045 return 0;
2046}
2047
2048/**
2049 * We are reconnecting to the backend, due to a suspend/resume, or a backend
2050 * driver restart. We tear down our blkif structure and recreate it, but
2051 * leave the device-layer structures intact so that this is transparent to the
2052 * rest of the kernel.
2053 */
2054static int blkfront_resume(struct xenbus_device *dev)
2055{
Greg Kroah-Hartmana1b4b122009-04-30 14:43:31 -07002056 struct blkfront_info *info = dev_get_drvdata(&dev->dev);
Bob Liu3db70a82015-11-25 17:52:55 -05002057 int err = 0;
Bob Liu7b427a52016-06-27 16:33:24 +08002058 unsigned int i, j;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07002059
2060 dev_dbg(&dev->dev, "blkfront_resume: %s\n", dev->nodename);
2061
Bob Liu7b427a52016-06-27 16:33:24 +08002062 bio_list_init(&info->bio_list);
2063 INIT_LIST_HEAD(&info->requests);
2064 for (i = 0; i < info->nr_rings; i++) {
2065 struct blkfront_ring_info *rinfo = &info->rinfo[i];
2066 struct bio_list merge_bio;
2067 struct blk_shadow *shadow = rinfo->shadow;
2068
2069 for (j = 0; j < BLK_RING_SIZE(info); j++) {
2070 /* Not in use? */
2071 if (!shadow[j].request)
2072 continue;
2073
2074 /*
2075 * Get the bios in the request so we can re-queue them.
2076 */
Munehisa Kamatab15bd8c2017-08-09 15:31:40 -07002077 if (req_op(shadow[j].request) == REQ_OP_FLUSH ||
2078 req_op(shadow[j].request) == REQ_OP_DISCARD ||
2079 req_op(shadow[j].request) == REQ_OP_SECURE_ERASE ||
Linus Torvalds3fc9d692016-07-26 15:37:51 -07002080 shadow[j].request->cmd_flags & REQ_FUA) {
Bob Liu7b427a52016-06-27 16:33:24 +08002081 /*
2082 * Flush operations don't contain bios, so
2083 * we need to requeue the whole request
Linus Torvalds3fc9d692016-07-26 15:37:51 -07002084 *
2085 * XXX: but this doesn't make any sense for a
2086 * write with the FUA flag set..
Bob Liu7b427a52016-06-27 16:33:24 +08002087 */
2088 list_add(&shadow[j].request->queuelist, &info->requests);
2089 continue;
2090 }
2091 merge_bio.head = shadow[j].request->bio;
2092 merge_bio.tail = shadow[j].request->biotail;
2093 bio_list_merge(&info->bio_list, &merge_bio);
2094 shadow[j].request->bio = NULL;
Christoph Hellwig2a842ac2017-06-03 09:38:04 +02002095 blk_mq_end_request(shadow[j].request, BLK_STS_OK);
Bob Liu7b427a52016-06-27 16:33:24 +08002096 }
2097 }
2098
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07002099 blkif_free(info, info->connected == BLKIF_STATE_CONNECTED);
2100
Ian Campbell203fd612009-12-04 15:33:54 +00002101 err = talk_to_blkback(dev, info);
Bob Liu2a6f71a2016-05-31 16:59:17 +08002102 if (!err)
2103 blk_mq_update_nr_hw_queues(&info->tag_set, info->nr_rings);
Roger Pau Monne402b27f2013-04-18 16:06:54 +02002104
2105 /*
2106 * We have to wait for the backend to switch to
2107 * connected state, since we want to read which
2108 * features it supports.
2109 */
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07002110
2111 return err;
2112}
2113
Konrad Rzeszutek Wilk6f03a7f2015-11-16 15:14:41 -05002114static void blkfront_closing(struct blkfront_info *info)
Daniel Stoddenb70f5fa2010-04-30 22:01:19 +00002115{
2116 struct xenbus_device *xbdev = info->xbdev;
2117 struct block_device *bdev = NULL;
2118
2119 mutex_lock(&info->mutex);
2120
2121 if (xbdev->state == XenbusStateClosing) {
2122 mutex_unlock(&info->mutex);
2123 return;
2124 }
2125
2126 if (info->gd)
2127 bdev = bdget_disk(info->gd, 0);
2128
2129 mutex_unlock(&info->mutex);
2130
2131 if (!bdev) {
2132 xenbus_frontend_closed(xbdev);
2133 return;
2134 }
2135
2136 mutex_lock(&bdev->bd_mutex);
2137
Daniel Stodden7b32d102010-04-30 22:01:23 +00002138 if (bdev->bd_openers) {
Daniel Stoddenb70f5fa2010-04-30 22:01:19 +00002139 xenbus_dev_error(xbdev, -EBUSY,
2140 "Device in use; refusing to close");
2141 xenbus_switch_state(xbdev, XenbusStateClosing);
2142 } else {
2143 xlvbd_release_gendisk(info);
2144 xenbus_frontend_closed(xbdev);
2145 }
2146
2147 mutex_unlock(&bdev->bd_mutex);
2148 bdput(bdev);
2149}
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07002150
Li Dongyanged30bf32011-09-01 18:39:09 +08002151static void blkfront_setup_discard(struct blkfront_info *info)
2152{
2153 int err;
Li Dongyanged30bf32011-09-01 18:39:09 +08002154 unsigned int discard_granularity;
2155 unsigned int discard_alignment;
2156
Olaf Hering1c8cad62014-05-21 16:32:40 +02002157 info->feature_discard = 1;
2158 err = xenbus_gather(XBT_NIL, info->xbdev->otherend,
2159 "discard-granularity", "%u", &discard_granularity,
2160 "discard-alignment", "%u", &discard_alignment,
2161 NULL);
2162 if (!err) {
2163 info->discard_granularity = discard_granularity;
2164 info->discard_alignment = discard_alignment;
2165 }
Juergen Grossf27dc1a2016-10-31 14:58:40 +01002166 info->feature_secdiscard =
2167 !!xenbus_read_unsigned(info->xbdev->otherend, "discard-secure",
2168 0);
Li Dongyanged30bf32011-09-01 18:39:09 +08002169}
2170
Bob Liu81f35162015-11-14 11:12:11 +08002171static int blkfront_setup_indirect(struct blkfront_ring_info *rinfo)
Roger Pau Monne402b27f2013-04-18 16:06:54 +02002172{
Julien Grallc004a6f2015-07-22 16:44:54 +01002173 unsigned int psegs, grants;
Roger Pau Monne402b27f2013-04-18 16:06:54 +02002174 int err, i;
Bob Liu81f35162015-11-14 11:12:11 +08002175 struct blkfront_info *info = rinfo->dev_info;
Roger Pau Monne402b27f2013-04-18 16:06:54 +02002176
Julien Grall6cc568332015-08-13 13:13:35 +01002177 if (info->max_indirect_segments == 0) {
2178 if (!HAS_EXTRA_REQ)
2179 grants = BLKIF_MAX_SEGMENTS_PER_REQUEST;
2180 else {
2181 /*
2182 * When an extra req is required, the maximum
2183 * grants supported is related to the size of the
2184 * Linux block segment.
2185 */
2186 grants = GRANTS_PER_PSEG;
2187 }
2188 }
Bob Liud50babb2015-07-22 14:40:08 +08002189 else
Julien Grallc004a6f2015-07-22 16:44:54 +01002190 grants = info->max_indirect_segments;
Jan Beulich3b4f1882017-01-23 08:11:37 -07002191 psegs = DIV_ROUND_UP(grants, GRANTS_PER_PSEG);
Roger Pau Monne402b27f2013-04-18 16:06:54 +02002192
Bob Liu81f35162015-11-14 11:12:11 +08002193 err = fill_grant_buffer(rinfo,
Julien Grallc004a6f2015-07-22 16:44:54 +01002194 (grants + INDIRECT_GREFS(grants)) * BLK_RING_SIZE(info));
Roger Pau Monne402b27f2013-04-18 16:06:54 +02002195 if (err)
2196 goto out_of_memory;
2197
Roger Pau Monnebfe11d62013-10-29 18:31:14 +01002198 if (!info->feature_persistent && info->max_indirect_segments) {
2199 /*
2200 * We are using indirect descriptors but not persistent
2201 * grants, we need to allocate a set of pages that can be
2202 * used for mapping indirect grefs
2203 */
Julien Grallc004a6f2015-07-22 16:44:54 +01002204 int num = INDIRECT_GREFS(grants) * BLK_RING_SIZE(info);
Roger Pau Monnebfe11d62013-10-29 18:31:14 +01002205
Bob Liu81f35162015-11-14 11:12:11 +08002206 BUG_ON(!list_empty(&rinfo->indirect_pages));
Roger Pau Monnebfe11d62013-10-29 18:31:14 +01002207 for (i = 0; i < num; i++) {
2208 struct page *indirect_page = alloc_page(GFP_NOIO);
2209 if (!indirect_page)
2210 goto out_of_memory;
Bob Liu81f35162015-11-14 11:12:11 +08002211 list_add(&indirect_page->lru, &rinfo->indirect_pages);
Roger Pau Monnebfe11d62013-10-29 18:31:14 +01002212 }
2213 }
2214
Bob Liu86839c52015-06-03 13:40:03 +08002215 for (i = 0; i < BLK_RING_SIZE(info); i++) {
Kees Cook6396bb22018-06-12 14:03:40 -07002216 rinfo->shadow[i].grants_used =
2217 kcalloc(grants,
2218 sizeof(rinfo->shadow[i].grants_used[0]),
Roger Pau Monne402b27f2013-04-18 16:06:54 +02002219 GFP_NOIO);
Kees Cook6396bb22018-06-12 14:03:40 -07002220 rinfo->shadow[i].sg = kcalloc(psegs,
2221 sizeof(rinfo->shadow[i].sg[0]),
2222 GFP_NOIO);
2223 if (info->max_indirect_segments)
2224 rinfo->shadow[i].indirect_grants =
2225 kcalloc(INDIRECT_GREFS(grants),
2226 sizeof(rinfo->shadow[i].indirect_grants[0]),
2227 GFP_NOIO);
Bob Liu81f35162015-11-14 11:12:11 +08002228 if ((rinfo->shadow[i].grants_used == NULL) ||
2229 (rinfo->shadow[i].sg == NULL) ||
Roger Pau Monne402b27f2013-04-18 16:06:54 +02002230 (info->max_indirect_segments &&
Bob Liu81f35162015-11-14 11:12:11 +08002231 (rinfo->shadow[i].indirect_grants == NULL)))
Roger Pau Monne402b27f2013-04-18 16:06:54 +02002232 goto out_of_memory;
Bob Liu81f35162015-11-14 11:12:11 +08002233 sg_init_table(rinfo->shadow[i].sg, psegs);
Roger Pau Monne402b27f2013-04-18 16:06:54 +02002234 }
2235
2236
2237 return 0;
2238
2239out_of_memory:
Bob Liu86839c52015-06-03 13:40:03 +08002240 for (i = 0; i < BLK_RING_SIZE(info); i++) {
Bob Liu81f35162015-11-14 11:12:11 +08002241 kfree(rinfo->shadow[i].grants_used);
2242 rinfo->shadow[i].grants_used = NULL;
2243 kfree(rinfo->shadow[i].sg);
2244 rinfo->shadow[i].sg = NULL;
2245 kfree(rinfo->shadow[i].indirect_grants);
2246 rinfo->shadow[i].indirect_grants = NULL;
Roger Pau Monne402b27f2013-04-18 16:06:54 +02002247 }
Bob Liu81f35162015-11-14 11:12:11 +08002248 if (!list_empty(&rinfo->indirect_pages)) {
Roger Pau Monnebfe11d62013-10-29 18:31:14 +01002249 struct page *indirect_page, *n;
Bob Liu81f35162015-11-14 11:12:11 +08002250 list_for_each_entry_safe(indirect_page, n, &rinfo->indirect_pages, lru) {
Roger Pau Monnebfe11d62013-10-29 18:31:14 +01002251 list_del(&indirect_page->lru);
2252 __free_page(indirect_page);
2253 }
2254 }
Roger Pau Monne402b27f2013-04-18 16:06:54 +02002255 return -ENOMEM;
2256}
2257
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07002258/*
Bob Liud50babb2015-07-22 14:40:08 +08002259 * Gather all backend feature-*
2260 */
Bob Liu3df0e502015-11-14 11:12:12 +08002261static void blkfront_gather_backend_features(struct blkfront_info *info)
Bob Liud50babb2015-07-22 14:40:08 +08002262{
Bob Liud50babb2015-07-22 14:40:08 +08002263 unsigned int indirect_segments;
2264
2265 info->feature_flush = 0;
Mike Christiea4180902016-06-05 14:32:24 -05002266 info->feature_fua = 0;
Bob Liud50babb2015-07-22 14:40:08 +08002267
Bob Liud50babb2015-07-22 14:40:08 +08002268 /*
2269 * If there's no "feature-barrier" defined, then it means
2270 * we're dealing with a very old backend which writes
2271 * synchronously; nothing to do.
2272 *
2273 * If there are barriers, then we use flush.
2274 */
Juergen Grossf27dc1a2016-10-31 14:58:40 +01002275 if (xenbus_read_unsigned(info->xbdev->otherend, "feature-barrier", 0)) {
Mike Christiea4180902016-06-05 14:32:24 -05002276 info->feature_flush = 1;
2277 info->feature_fua = 1;
2278 }
2279
Bob Liud50babb2015-07-22 14:40:08 +08002280 /*
2281 * And if there is "feature-flush-cache" use that above
2282 * barriers.
2283 */
Juergen Grossf27dc1a2016-10-31 14:58:40 +01002284 if (xenbus_read_unsigned(info->xbdev->otherend, "feature-flush-cache",
2285 0)) {
Mike Christiea4180902016-06-05 14:32:24 -05002286 info->feature_flush = 1;
2287 info->feature_fua = 0;
2288 }
Bob Liud50babb2015-07-22 14:40:08 +08002289
Juergen Grossf27dc1a2016-10-31 14:58:40 +01002290 if (xenbus_read_unsigned(info->xbdev->otherend, "feature-discard", 0))
Bob Liud50babb2015-07-22 14:40:08 +08002291 blkfront_setup_discard(info);
2292
Juergen Grossf27dc1a2016-10-31 14:58:40 +01002293 info->feature_persistent =
Jan Beulichb32728f2017-01-23 08:12:19 -07002294 !!xenbus_read_unsigned(info->xbdev->otherend,
2295 "feature-persistent", 0);
Bob Liud50babb2015-07-22 14:40:08 +08002296
Juergen Grossf27dc1a2016-10-31 14:58:40 +01002297 indirect_segments = xenbus_read_unsigned(info->xbdev->otherend,
2298 "feature-max-indirect-segments", 0);
Jan Beulich3b4f1882017-01-23 08:11:37 -07002299 if (indirect_segments > xen_blkif_max_segments)
2300 indirect_segments = xen_blkif_max_segments;
2301 if (indirect_segments <= BLKIF_MAX_SEGMENTS_PER_REQUEST)
2302 indirect_segments = 0;
2303 info->max_indirect_segments = indirect_segments;
Bob Liud50babb2015-07-22 14:40:08 +08002304}
2305
2306/*
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07002307 * Invoked when the backend is finally 'ready' (and has told produced
2308 * the details about the physical device - #sectors, size, etc).
2309 */
2310static void blkfront_connect(struct blkfront_info *info)
2311{
2312 unsigned long long sectors;
2313 unsigned long sector_size;
Stefan Bader7c4d7d72013-05-13 16:28:15 +02002314 unsigned int physical_sector_size;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07002315 unsigned int binfo;
Marc Olson89515d02017-04-11 12:24:09 -07002316 char *envp[] = { "RESIZE=1", NULL };
Bob Liu3df0e502015-11-14 11:12:12 +08002317 int err, i;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07002318
K. Y. Srinivasan1fa73be2010-03-11 13:42:26 -08002319 switch (info->connected) {
2320 case BLKIF_STATE_CONNECTED:
2321 /*
2322 * Potentially, the back-end may be signalling
2323 * a capacity change; update the capacity.
2324 */
2325 err = xenbus_scanf(XBT_NIL, info->xbdev->otherend,
2326 "sectors", "%Lu", &sectors);
2327 if (XENBUS_EXIST_ERR(err))
2328 return;
2329 printk(KERN_INFO "Setting capacity to %Lu\n",
2330 sectors);
2331 set_capacity(info->gd, sectors);
K. Y. Srinivasan2def1412010-03-18 15:00:54 -07002332 revalidate_disk(info->gd);
Marc Olson89515d02017-04-11 12:24:09 -07002333 kobject_uevent_env(&disk_to_dev(info->gd)->kobj,
2334 KOBJ_CHANGE, envp);
K. Y. Srinivasan1fa73be2010-03-11 13:42:26 -08002335
Roger Pau Monne402b27f2013-04-18 16:06:54 +02002336 return;
K. Y. Srinivasan1fa73be2010-03-11 13:42:26 -08002337 case BLKIF_STATE_SUSPENDED:
Roger Pau Monne402b27f2013-04-18 16:06:54 +02002338 /*
2339 * If we are recovering from suspension, we need to wait
2340 * for the backend to announce it's features before
2341 * reconnecting, at least we need to know if the backend
2342 * supports indirect descriptors, and how many.
2343 */
2344 blkif_recover(info);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07002345 return;
2346
Jeremy Fitzhardingeb4dddb42010-03-11 15:10:40 -08002347 default:
2348 break;
K. Y. Srinivasan1fa73be2010-03-11 13:42:26 -08002349 }
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07002350
2351 dev_dbg(&info->xbdev->dev, "%s:%s.\n",
2352 __func__, info->xbdev->otherend);
2353
2354 err = xenbus_gather(XBT_NIL, info->xbdev->otherend,
2355 "sectors", "%llu", &sectors,
2356 "info", "%u", &binfo,
2357 "sector-size", "%lu", &sector_size,
2358 NULL);
2359 if (err) {
2360 xenbus_dev_fatal(info->xbdev, err,
2361 "reading backend fields at %s",
2362 info->xbdev->otherend);
2363 return;
2364 }
2365
Stefan Bader7c4d7d72013-05-13 16:28:15 +02002366 /*
2367 * physcial-sector-size is a newer field, so old backends may not
2368 * provide this. Assume physical sector size to be the same as
2369 * sector_size in that case.
2370 */
Juergen Grossf27dc1a2016-10-31 14:58:40 +01002371 physical_sector_size = xenbus_read_unsigned(info->xbdev->otherend,
2372 "physical-sector-size",
2373 sector_size);
Bob Liu3df0e502015-11-14 11:12:12 +08002374 blkfront_gather_backend_features(info);
2375 for (i = 0; i < info->nr_rings; i++) {
2376 err = blkfront_setup_indirect(&info->rinfo[i]);
2377 if (err) {
2378 xenbus_dev_fatal(info->xbdev, err, "setup_indirect at %s",
2379 info->xbdev->otherend);
2380 blkif_free(info, 0);
2381 break;
2382 }
Roger Pau Monne402b27f2013-04-18 16:06:54 +02002383 }
2384
Stefan Bader7c4d7d72013-05-13 16:28:15 +02002385 err = xlvbd_alloc_gendisk(sectors, info, binfo, sector_size,
2386 physical_sector_size);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07002387 if (err) {
2388 xenbus_dev_fatal(info->xbdev, err, "xlvbd_add at %s",
2389 info->xbdev->otherend);
Bob Liu4e876c22016-07-27 17:42:04 +08002390 goto fail;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07002391 }
2392
2393 xenbus_switch_state(info->xbdev, XenbusStateConnected);
2394
2395 /* Kick pending requests. */
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07002396 info->connected = BLKIF_STATE_CONNECTED;
Bob Liu3df0e502015-11-14 11:12:12 +08002397 for (i = 0; i < info->nr_rings; i++)
2398 kick_pending_request_queues(&info->rinfo[i]);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07002399
Dan Williams0d52c7562016-06-15 19:44:20 -07002400 device_add_disk(&info->xbdev->dev, info->gd);
Christian Limpach1d78d702008-04-02 10:54:04 -07002401
2402 info->is_ready = 1;
Bob Liu4e876c22016-07-27 17:42:04 +08002403 return;
2404
2405fail:
2406 blkif_free(info, 0);
2407 return;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07002408}
2409
2410/**
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07002411 * Callback received when the backend's state changes.
2412 */
Ian Campbell203fd612009-12-04 15:33:54 +00002413static void blkback_changed(struct xenbus_device *dev,
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07002414 enum xenbus_state backend_state)
2415{
Greg Kroah-Hartmana1b4b122009-04-30 14:43:31 -07002416 struct blkfront_info *info = dev_get_drvdata(&dev->dev);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07002417
Ian Campbell203fd612009-12-04 15:33:54 +00002418 dev_dbg(&dev->dev, "blkfront:blkback_changed to state %d.\n", backend_state);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07002419
2420 switch (backend_state) {
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07002421 case XenbusStateInitWait:
Bob Liua9b54bb2015-06-19 00:23:00 -04002422 if (dev->state != XenbusStateInitialising)
2423 break;
Konrad Rzeszutek Wilkc31ecf62015-12-18 16:28:53 -05002424 if (talk_to_blkback(dev, info))
Bob Liu8ab01442015-06-03 13:40:02 +08002425 break;
Bob Liu8ab01442015-06-03 13:40:02 +08002426 case XenbusStateInitialising:
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07002427 case XenbusStateInitialised:
Noboru Iwamatsub78c9512009-10-13 17:22:29 -04002428 case XenbusStateReconfiguring:
2429 case XenbusStateReconfigured:
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07002430 case XenbusStateUnknown:
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07002431 break;
2432
2433 case XenbusStateConnected:
Bob Liuefd15352016-06-07 10:43:15 -04002434 /*
2435 * talk_to_blkback sets state to XenbusStateInitialised
2436 * and blkfront_connect sets it to XenbusStateConnected
2437 * (if connection went OK).
2438 *
2439 * If the backend (or toolstack) decides to poke at backend
2440 * state (and re-trigger the watch by setting the state repeatedly
2441 * to XenbusStateConnected (4)) we need to deal with this.
2442 * This is allowed as this is used to communicate to the guest
2443 * that the size of disk has changed!
2444 */
2445 if ((dev->state != XenbusStateInitialised) &&
2446 (dev->state != XenbusStateConnected)) {
Konrad Rzeszutek Wilkc31ecf62015-12-18 16:28:53 -05002447 if (talk_to_blkback(dev, info))
2448 break;
2449 }
Bob Liuefd15352016-06-07 10:43:15 -04002450
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07002451 blkfront_connect(info);
2452 break;
2453
David Vrabel36613712014-02-04 18:53:56 +00002454 case XenbusStateClosed:
2455 if (dev->state == XenbusStateClosed)
2456 break;
Bart Van Asscheccc22252017-08-17 16:23:11 -07002457 /* fall through */
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07002458 case XenbusStateClosing:
Cathy Averya54c8f02015-10-02 09:35:01 -04002459 if (info)
2460 blkfront_closing(info);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07002461 break;
2462 }
2463}
2464
Daniel Stoddenfa1bd352010-04-30 22:01:22 +00002465static int blkfront_remove(struct xenbus_device *xbdev)
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07002466{
Daniel Stoddenfa1bd352010-04-30 22:01:22 +00002467 struct blkfront_info *info = dev_get_drvdata(&xbdev->dev);
2468 struct block_device *bdev = NULL;
2469 struct gendisk *disk;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07002470
Daniel Stoddenfa1bd352010-04-30 22:01:22 +00002471 dev_dbg(&xbdev->dev, "%s removed", xbdev->nodename);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07002472
2473 blkif_free(info, 0);
2474
Daniel Stoddenfa1bd352010-04-30 22:01:22 +00002475 mutex_lock(&info->mutex);
2476
2477 disk = info->gd;
2478 if (disk)
2479 bdev = bdget_disk(disk, 0);
2480
2481 info->xbdev = NULL;
2482 mutex_unlock(&info->mutex);
2483
2484 if (!bdev) {
Jan Beulich0e345822010-08-07 18:28:55 +02002485 kfree(info);
Daniel Stoddenfa1bd352010-04-30 22:01:22 +00002486 return 0;
2487 }
2488
2489 /*
2490 * The xbdev was removed before we reached the Closed
2491 * state. See if it's safe to remove the disk. If the bdev
2492 * isn't closed yet, we let release take care of it.
2493 */
2494
2495 mutex_lock(&bdev->bd_mutex);
2496 info = disk->private_data;
2497
Daniel Stoddend54142c2010-08-07 18:51:21 +02002498 dev_warn(disk_to_dev(disk),
2499 "%s was hot-unplugged, %d stale handles\n",
2500 xbdev->nodename, bdev->bd_openers);
2501
Daniel Stodden7b32d102010-04-30 22:01:23 +00002502 if (info && !bdev->bd_openers) {
Daniel Stoddenfa1bd352010-04-30 22:01:22 +00002503 xlvbd_release_gendisk(info);
2504 disk->private_data = NULL;
2505 kfree(info);
2506 }
2507
2508 mutex_unlock(&bdev->bd_mutex);
2509 bdput(bdev);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07002510
2511 return 0;
2512}
2513
Christian Limpach1d78d702008-04-02 10:54:04 -07002514static int blkfront_is_ready(struct xenbus_device *dev)
2515{
Greg Kroah-Hartmana1b4b122009-04-30 14:43:31 -07002516 struct blkfront_info *info = dev_get_drvdata(&dev->dev);
Christian Limpach1d78d702008-04-02 10:54:04 -07002517
Jan Beulich5d7ed202010-08-07 18:31:12 +02002518 return info->is_ready && info->xbdev;
Christian Limpach1d78d702008-04-02 10:54:04 -07002519}
2520
Al Viroa63c8482008-03-02 10:23:47 -05002521static int blkif_open(struct block_device *bdev, fmode_t mode)
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07002522{
Daniel Stodden13961742010-08-07 18:36:53 +02002523 struct gendisk *disk = bdev->bd_disk;
2524 struct blkfront_info *info;
2525 int err = 0;
Arnd Bergmann6e9624b2010-08-07 18:25:34 +02002526
Arnd Bergmann2a48fc02010-06-02 14:28:52 +02002527 mutex_lock(&blkfront_mutex);
Arnd Bergmann6e9624b2010-08-07 18:25:34 +02002528
Daniel Stodden13961742010-08-07 18:36:53 +02002529 info = disk->private_data;
2530 if (!info) {
2531 /* xbdev gone */
2532 err = -ERESTARTSYS;
2533 goto out;
2534 }
2535
2536 mutex_lock(&info->mutex);
2537
2538 if (!info->gd)
2539 /* xbdev is closed */
2540 err = -ERESTARTSYS;
2541
2542 mutex_unlock(&info->mutex);
2543
Daniel Stodden13961742010-08-07 18:36:53 +02002544out:
Arnd Bergmann2a48fc02010-06-02 14:28:52 +02002545 mutex_unlock(&blkfront_mutex);
Daniel Stodden13961742010-08-07 18:36:53 +02002546 return err;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07002547}
2548
Al Virodb2a1442013-05-05 21:52:57 -04002549static void blkif_release(struct gendisk *disk, fmode_t mode)
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07002550{
Al Viroa63c8482008-03-02 10:23:47 -05002551 struct blkfront_info *info = disk->private_data;
Daniel Stodden7fd152f2010-08-07 18:45:12 +02002552 struct block_device *bdev;
2553 struct xenbus_device *xbdev;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07002554
Arnd Bergmann2a48fc02010-06-02 14:28:52 +02002555 mutex_lock(&blkfront_mutex);
Daniel Stodden7fd152f2010-08-07 18:45:12 +02002556
2557 bdev = bdget_disk(disk, 0);
Daniel Stodden7fd152f2010-08-07 18:45:12 +02002558
Felipe Pena2f089cb2013-11-09 13:36:09 -02002559 if (!bdev) {
2560 WARN(1, "Block device %s yanked out from us!\n", disk->disk_name);
2561 goto out_mutex;
2562 }
Daniel Stoddenacfca3c2010-08-07 18:47:26 +02002563 if (bdev->bd_openers)
2564 goto out;
2565
Daniel Stodden7fd152f2010-08-07 18:45:12 +02002566 /*
2567 * Check if we have been instructed to close. We will have
2568 * deferred this request, because the bdev was still open.
2569 */
2570
2571 mutex_lock(&info->mutex);
2572 xbdev = info->xbdev;
2573
2574 if (xbdev && xbdev->state == XenbusStateClosing) {
2575 /* pending switch to state closed */
Daniel Stoddend54142c2010-08-07 18:51:21 +02002576 dev_info(disk_to_dev(bdev->bd_disk), "releasing disk\n");
Daniel Stodden7fd152f2010-08-07 18:45:12 +02002577 xlvbd_release_gendisk(info);
2578 xenbus_frontend_closed(info->xbdev);
2579 }
2580
2581 mutex_unlock(&info->mutex);
2582
2583 if (!xbdev) {
2584 /* sudden device removal */
Daniel Stoddend54142c2010-08-07 18:51:21 +02002585 dev_info(disk_to_dev(bdev->bd_disk), "releasing disk\n");
Daniel Stodden7fd152f2010-08-07 18:45:12 +02002586 xlvbd_release_gendisk(info);
2587 disk->private_data = NULL;
2588 kfree(info);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07002589 }
Daniel Stodden7fd152f2010-08-07 18:45:12 +02002590
Jens Axboea4cc14e2010-08-08 21:50:05 -04002591out:
Andrew Jonesdad5cf62012-02-16 13:16:25 +01002592 bdput(bdev);
Felipe Pena2f089cb2013-11-09 13:36:09 -02002593out_mutex:
Arnd Bergmann2a48fc02010-06-02 14:28:52 +02002594 mutex_unlock(&blkfront_mutex);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07002595}
2596
Alexey Dobriyan83d5cde2009-09-21 17:01:13 -07002597static const struct block_device_operations xlvbd_block_fops =
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07002598{
2599 .owner = THIS_MODULE,
Al Viroa63c8482008-03-02 10:23:47 -05002600 .open = blkif_open,
2601 .release = blkif_release,
Ian Campbell597592d2008-02-21 13:03:45 -08002602 .getgeo = blkif_getgeo,
Arnd Bergmann8a6cfeb2010-07-08 10:18:46 +02002603 .ioctl = blkif_ioctl,
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07002604};
2605
2606
Márton Némethec9c42e2010-01-10 13:39:52 +01002607static const struct xenbus_device_id blkfront_ids[] = {
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07002608 { "vbd" },
2609 { "" }
2610};
2611
David Vrabel95afae42014-09-08 17:30:41 +01002612static struct xenbus_driver blkfront_driver = {
2613 .ids = blkfront_ids,
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07002614 .probe = blkfront_probe,
2615 .remove = blkfront_remove,
2616 .resume = blkfront_resume,
Ian Campbell203fd612009-12-04 15:33:54 +00002617 .otherend_changed = blkback_changed,
Christian Limpach1d78d702008-04-02 10:54:04 -07002618 .is_ready = blkfront_is_ready,
David Vrabel95afae42014-09-08 17:30:41 +01002619};
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07002620
2621static int __init xlblk_init(void)
2622{
Laszlo Ersek469738e2011-10-07 21:34:38 +02002623 int ret;
Bob Liu28d949b2015-11-14 11:12:14 +08002624 int nr_cpus = num_online_cpus();
Laszlo Ersek469738e2011-10-07 21:34:38 +02002625
Jeremy Fitzhardinge6e833582008-08-19 13:16:17 -07002626 if (!xen_domain())
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07002627 return -ENODEV;
2628
Jan Beulich3b4f1882017-01-23 08:11:37 -07002629 if (xen_blkif_max_segments < BLKIF_MAX_SEGMENTS_PER_REQUEST)
2630 xen_blkif_max_segments = BLKIF_MAX_SEGMENTS_PER_REQUEST;
2631
Julien Grall9cce2912015-10-13 17:50:11 +01002632 if (xen_blkif_max_ring_order > XENBUS_MAX_RING_GRANT_ORDER) {
Bob Liu86839c52015-06-03 13:40:03 +08002633 pr_info("Invalid max_ring_order (%d), will use default max: %d.\n",
Julien Grall9cce2912015-10-13 17:50:11 +01002634 xen_blkif_max_ring_order, XENBUS_MAX_RING_GRANT_ORDER);
Peng Fan45fc8262015-11-25 18:26:01 +08002635 xen_blkif_max_ring_order = XENBUS_MAX_RING_GRANT_ORDER;
Bob Liu86839c52015-06-03 13:40:03 +08002636 }
2637
Bob Liu28d949b2015-11-14 11:12:14 +08002638 if (xen_blkif_max_queues > nr_cpus) {
2639 pr_info("Invalid max_queues (%d), will use default max: %d.\n",
2640 xen_blkif_max_queues, nr_cpus);
2641 xen_blkif_max_queues = nr_cpus;
2642 }
2643
Konrad Rzeszutek Wilk51c71a32013-11-26 15:05:40 -05002644 if (!xen_has_pv_disk_devices())
Igor Mammedovb9136d22012-03-21 15:08:38 +01002645 return -ENODEV;
2646
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07002647 if (register_blkdev(XENVBD_MAJOR, DEV_NAME)) {
2648 printk(KERN_WARNING "xen_blk: can't get major %d with name %s\n",
2649 XENVBD_MAJOR, DEV_NAME);
2650 return -ENODEV;
2651 }
2652
Jan Beulich73db1442011-12-22 09:08:13 +00002653 ret = xenbus_register_frontend(&blkfront_driver);
Laszlo Ersek469738e2011-10-07 21:34:38 +02002654 if (ret) {
2655 unregister_blkdev(XENVBD_MAJOR, DEV_NAME);
2656 return ret;
2657 }
2658
2659 return 0;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07002660}
2661module_init(xlblk_init);
2662
2663
Jan Beulich5a60d0c2008-06-17 10:47:08 +02002664static void __exit xlblk_exit(void)
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07002665{
Jan Beulich86050672012-04-05 16:04:52 +01002666 xenbus_unregister_driver(&blkfront_driver);
2667 unregister_blkdev(XENVBD_MAJOR, DEV_NAME);
2668 kfree(minors);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07002669}
2670module_exit(xlblk_exit);
2671
2672MODULE_DESCRIPTION("Xen virtual block device frontend");
2673MODULE_LICENSE("GPL");
2674MODULE_ALIAS_BLOCKDEV_MAJOR(XENVBD_MAJOR);
Mark McLoughlind2f0c522008-04-02 10:54:05 -07002675MODULE_ALIAS("xen:vbd");
Mark McLoughlin4f93f09b2008-04-02 10:54:06 -07002676MODULE_ALIAS("xenblk");