blob: a74d03913822df88989b9b0198da99dea490eef1 [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>
Juergen Grossa46b5362018-08-13 16:01:11 +020049#include <linux/workqueue.h>
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -070050
Jeremy Fitzhardinge1ccbf532009-10-06 15:11:14 -070051#include <xen/xen.h>
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -070052#include <xen/xenbus.h>
53#include <xen/grant_table.h>
54#include <xen/events.h>
55#include <xen/page.h>
Stefano Stabellinic1c54132010-05-14 12:44:30 +010056#include <xen/platform_pci.h>
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -070057
58#include <xen/interface/grant_table.h>
59#include <xen/interface/io/blkif.h>
Markus Armbruster3e334232008-04-02 10:54:02 -070060#include <xen/interface/io/protocols.h>
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -070061
62#include <asm/xen/hypervisor.h>
63
Julien Grall6cc568332015-08-13 13:13:35 +010064/*
65 * The minimal size of segment supported by the block framework is PAGE_SIZE.
66 * When Linux is using a different page size than Xen, it may not be possible
67 * to put all the data in a single segment.
68 * This can happen when the backend doesn't support indirect descriptor and
69 * therefore the maximum amount of data that a request can carry is
70 * BLKIF_MAX_SEGMENTS_PER_REQUEST * XEN_PAGE_SIZE = 44KB
71 *
72 * Note that we only support one extra request. So the Linux page size
73 * should be <= ( 2 * BLKIF_MAX_SEGMENTS_PER_REQUEST * XEN_PAGE_SIZE) =
74 * 88KB.
75 */
76#define HAS_EXTRA_REQ (BLKIF_MAX_SEGMENTS_PER_REQUEST < XEN_PFN_PER_PAGE)
77
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -070078enum blkif_state {
79 BLKIF_STATE_DISCONNECTED,
80 BLKIF_STATE_CONNECTED,
81 BLKIF_STATE_SUSPENDED,
82};
83
Roger Pau Monne0a8704a2012-10-24 18:58:45 +020084struct grant {
85 grant_ref_t gref;
Julien Gralla7a6df22015-06-30 11:58:51 +010086 struct page *page;
Roger Pau Monne155b7ed2013-03-18 17:49:34 +010087 struct list_head node;
Roger Pau Monne0a8704a2012-10-24 18:58:45 +020088};
89
Julien Grall6cc568332015-08-13 13:13:35 +010090enum blk_req_status {
91 REQ_WAITING,
92 REQ_DONE,
93 REQ_ERROR,
94 REQ_EOPNOTSUPP,
95};
96
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -070097struct blk_shadow {
98 struct blkif_request req;
Jeremy Fitzhardingea945b982010-11-01 17:03:14 -040099 struct request *request;
Roger Pau Monne402b27f2013-04-18 16:06:54 +0200100 struct grant **grants_used;
101 struct grant **indirect_grants;
Roger Pau Monneb7649152013-05-02 10:58:50 +0200102 struct scatterlist *sg;
Julien Grallc004a6f2015-07-22 16:44:54 +0100103 unsigned int num_sg;
Julien Grall6cc568332015-08-13 13:13:35 +0100104 enum blk_req_status status;
105
106 #define NO_ASSOCIATED_ID ~0UL
107 /*
108 * Id of the sibling if we ever need 2 requests when handling a
109 * block I/O request
110 */
111 unsigned long associated_id;
Roger Pau Monne402b27f2013-04-18 16:06:54 +0200112};
113
Christoph Hellwig26095872017-04-20 16:03:08 +0200114struct blkif_req {
Bart Van Assche31c4ccc2017-07-21 19:11:10 +0200115 blk_status_t error;
Christoph Hellwig26095872017-04-20 16:03:08 +0200116};
117
118static inline struct blkif_req *blkif_req(struct request *rq)
119{
120 return blk_mq_rq_to_pdu(rq);
121}
122
Arnd Bergmann2a48fc02010-06-02 14:28:52 +0200123static DEFINE_MUTEX(blkfront_mutex);
Alexey Dobriyan83d5cde2009-09-21 17:01:13 -0700124static const struct block_device_operations xlvbd_block_fops;
Juergen Grossa46b5362018-08-13 16:01:11 +0200125static struct delayed_work blkfront_work;
126static LIST_HEAD(info_list);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700127
Roger Pau Monne402b27f2013-04-18 16:06:54 +0200128/*
129 * Maximum number of segments in indirect requests, the actual value used by
130 * the frontend driver is the minimum of this value and the value provided
131 * by the backend driver.
132 */
133
134static unsigned int xen_blkif_max_segments = 32;
Joe Perches5657a812018-05-24 13:38:59 -0600135module_param_named(max_indirect_segments, xen_blkif_max_segments, uint, 0444);
Jan Beulich14e710f2016-02-10 04:21:15 -0700136MODULE_PARM_DESC(max_indirect_segments,
137 "Maximum amount of segments in indirect requests (default is 32)");
Roger Pau Monne402b27f2013-04-18 16:06:54 +0200138
Bob Liu28d949b2015-11-14 11:12:14 +0800139static unsigned int xen_blkif_max_queues = 4;
Joe Perches5657a812018-05-24 13:38:59 -0600140module_param_named(max_queues, xen_blkif_max_queues, uint, 0444);
Bob Liu28d949b2015-11-14 11:12:14 +0800141MODULE_PARM_DESC(max_queues, "Maximum number of hardware queues/rings used per virtual disk");
142
Bob Liu86839c52015-06-03 13:40:03 +0800143/*
144 * Maximum order of pages to be used for the shared ring between front and
145 * backend, 4KB page granularity is used.
146 */
147static unsigned int xen_blkif_max_ring_order;
Joe Perches5657a812018-05-24 13:38:59 -0600148module_param_named(max_ring_page_order, xen_blkif_max_ring_order, int, 0444);
Bob Liu86839c52015-06-03 13:40:03 +0800149MODULE_PARM_DESC(max_ring_page_order, "Maximum order of pages to be used for the shared ring");
150
Julien Grallc004a6f2015-07-22 16:44:54 +0100151#define BLK_RING_SIZE(info) \
152 __CONST_RING_SIZE(blkif, XEN_PAGE_SIZE * (info)->nr_ring_pages)
153
154#define BLK_MAX_RING_SIZE \
Julien Grall9cce2912015-10-13 17:50:11 +0100155 __CONST_RING_SIZE(blkif, XEN_PAGE_SIZE * XENBUS_MAX_RING_GRANTS)
Julien Grallc004a6f2015-07-22 16:44:54 +0100156
Bob Liu86839c52015-06-03 13:40:03 +0800157/*
Konrad Rzeszutek Wilk6f03a7f2015-11-16 15:14:41 -0500158 * ring-ref%u i=(-1UL) would take 11 characters + 'ring-ref' is 8, so 19
159 * characters are enough. Define to 20 to keep consistent with backend.
Bob Liu86839c52015-06-03 13:40:03 +0800160 */
161#define RINGREF_NAME_LEN (20)
Bob Liu28d949b2015-11-14 11:12:14 +0800162/*
163 * queue-%u would take 7 + 10(UINT_MAX) = 17 characters.
164 */
165#define QUEUE_NAME_LEN (17)
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700166
167/*
Bob Liu81f35162015-11-14 11:12:11 +0800168 * Per-ring info.
169 * Every blkfront device can associate with one or more blkfront_ring_info,
170 * depending on how many hardware queues/rings to be used.
171 */
172struct blkfront_ring_info {
Bob Liu11659562015-11-14 11:12:13 +0800173 /* Lock to protect data in every ring buffer. */
174 spinlock_t ring_lock;
Bob Liu81f35162015-11-14 11:12:11 +0800175 struct blkif_front_ring ring;
176 unsigned int ring_ref[XENBUS_MAX_RING_GRANTS];
177 unsigned int evtchn, irq;
178 struct work_struct work;
179 struct gnttab_free_callback callback;
180 struct blk_shadow shadow[BLK_MAX_RING_SIZE];
181 struct list_head indirect_pages;
Bob Liu73716df2015-11-16 16:51:39 -0500182 struct list_head grants;
183 unsigned int persistent_gnts_c;
Bob Liu81f35162015-11-14 11:12:11 +0800184 unsigned long shadow_free;
185 struct blkfront_info *dev_info;
186};
187
188/*
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700189 * We have one of these per vbd, whether ide, scsi or 'other'. They
190 * hang in private_data off the gendisk structure. We may end up
191 * putting all kinds of interesting stuff here :-)
192 */
193struct blkfront_info
194{
Daniel Stoddenb70f5fa2010-04-30 22:01:19 +0000195 struct mutex mutex;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700196 struct xenbus_device *xbdev;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700197 struct gendisk *gd;
Bob Liu172335a2016-07-01 17:43:39 -0400198 u16 sector_size;
199 unsigned int physical_sector_size;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700200 int vdevice;
201 blkif_vdev_t handle;
202 enum blkif_state connected;
Bob Liu3df0e502015-11-14 11:12:12 +0800203 /* Number of pages per ring buffer. */
Bob Liu86839c52015-06-03 13:40:03 +0800204 unsigned int nr_ring_pages;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700205 struct request_queue *rq;
Jan Beulichb32728f2017-01-23 08:12:19 -0700206 unsigned int feature_flush:1;
207 unsigned int feature_fua:1;
Konrad Rzeszutek Wilk5ea42982011-10-12 16:23:30 -0400208 unsigned int feature_discard:1;
209 unsigned int feature_secdiscard:1;
Jan Beulichb32728f2017-01-23 08:12:19 -0700210 unsigned int feature_persistent:1;
Li Dongyanged30bf32011-09-01 18:39:09 +0800211 unsigned int discard_granularity;
212 unsigned int discard_alignment;
Julien Grallc004a6f2015-07-22 16:44:54 +0100213 /* Number of 4KB segments handled */
Roger Pau Monne402b27f2013-04-18 16:06:54 +0200214 unsigned int max_indirect_segments;
Christian Limpach1d78d702008-04-02 10:54:04 -0700215 int is_ready;
Bob Liu907c3eb2015-07-13 17:55:24 +0800216 struct blk_mq_tag_set tag_set;
Bob Liu3df0e502015-11-14 11:12:12 +0800217 struct blkfront_ring_info *rinfo;
218 unsigned int nr_rings;
Bob Liu7b427a52016-06-27 16:33:24 +0800219 /* Save uncomplete reqs and bios for migration. */
220 struct list_head requests;
221 struct bio_list bio_list;
Juergen Grossa46b5362018-08-13 16:01:11 +0200222 struct list_head info_list;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700223};
224
Jan Beulich0e345822010-08-07 18:28:55 +0200225static unsigned int nr_minors;
226static unsigned long *minors;
227static DEFINE_SPINLOCK(minor_lock);
228
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700229#define GRANT_INVALID_REF 0
230
231#define PARTS_PER_DISK 16
Chris Lalancette9246b5f2008-09-17 14:30:32 -0700232#define PARTS_PER_EXT_DISK 256
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700233
234#define BLKIF_MAJOR(dev) ((dev)>>8)
235#define BLKIF_MINOR(dev) ((dev) & 0xff)
236
Chris Lalancette9246b5f2008-09-17 14:30:32 -0700237#define EXT_SHIFT 28
238#define EXTENDED (1<<EXT_SHIFT)
239#define VDEV_IS_EXTENDED(dev) ((dev)&(EXTENDED))
240#define BLKIF_MINOR_EXT(dev) ((dev)&(~EXTENDED))
Stefano Stabellinic80a4202010-12-02 17:55:00 +0000241#define EMULATED_HD_DISK_MINOR_OFFSET (0)
242#define EMULATED_HD_DISK_NAME_OFFSET (EMULATED_HD_DISK_MINOR_OFFSET / 256)
Stefan Bader196cfe22011-07-14 15:30:22 +0200243#define EMULATED_SD_DISK_MINOR_OFFSET (0)
244#define EMULATED_SD_DISK_NAME_OFFSET (EMULATED_SD_DISK_MINOR_OFFSET / 256)
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700245
Chris Lalancette9246b5f2008-09-17 14:30:32 -0700246#define DEV_NAME "xvd" /* name in /dev */
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700247
Julien Grallc004a6f2015-07-22 16:44:54 +0100248/*
249 * Grants are always the same size as a Xen page (i.e 4KB).
250 * A physical segment is always the same size as a Linux page.
251 * Number of grants per physical segment
252 */
253#define GRANTS_PER_PSEG (PAGE_SIZE / XEN_PAGE_SIZE)
254
255#define GRANTS_PER_INDIRECT_FRAME \
256 (XEN_PAGE_SIZE / sizeof(struct blkif_request_segment))
257
Julien Grallc004a6f2015-07-22 16:44:54 +0100258#define INDIRECT_GREFS(_grants) \
259 DIV_ROUND_UP(_grants, GRANTS_PER_INDIRECT_FRAME)
260
Bob Liu81f35162015-11-14 11:12:11 +0800261static int blkfront_setup_indirect(struct blkfront_ring_info *rinfo);
Bob Liu3df0e502015-11-14 11:12:12 +0800262static void blkfront_gather_backend_features(struct blkfront_info *info);
Bhavesh Davda7ed8ce12017-12-22 14:17:13 -0800263static int negotiate_mq(struct blkfront_info *info);
Roger Pau Monne402b27f2013-04-18 16:06:54 +0200264
Bob Liu81f35162015-11-14 11:12:11 +0800265static int get_id_from_freelist(struct blkfront_ring_info *rinfo)
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700266{
Bob Liu81f35162015-11-14 11:12:11 +0800267 unsigned long free = rinfo->shadow_free;
268
269 BUG_ON(free >= BLK_RING_SIZE(rinfo->dev_info));
270 rinfo->shadow_free = rinfo->shadow[free].req.u.rw.id;
271 rinfo->shadow[free].req.u.rw.id = 0x0fffffee; /* debug */
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700272 return free;
273}
274
Bob Liu81f35162015-11-14 11:12:11 +0800275static int add_id_to_freelist(struct blkfront_ring_info *rinfo,
Konrad Rzeszutek Wilk6f03a7f2015-11-16 15:14:41 -0500276 unsigned long id)
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700277{
Bob Liu81f35162015-11-14 11:12:11 +0800278 if (rinfo->shadow[id].req.u.rw.id != id)
Konrad Rzeszutek Wilk6878c322012-05-25 17:34:51 -0400279 return -EINVAL;
Bob Liu81f35162015-11-14 11:12:11 +0800280 if (rinfo->shadow[id].request == NULL)
Konrad Rzeszutek Wilk6878c322012-05-25 17:34:51 -0400281 return -EINVAL;
Bob Liu81f35162015-11-14 11:12:11 +0800282 rinfo->shadow[id].req.u.rw.id = rinfo->shadow_free;
283 rinfo->shadow[id].request = NULL;
284 rinfo->shadow_free = id;
Konrad Rzeszutek Wilk6878c322012-05-25 17:34:51 -0400285 return 0;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700286}
287
Bob Liu81f35162015-11-14 11:12:11 +0800288static int fill_grant_buffer(struct blkfront_ring_info *rinfo, int num)
Roger Pau Monne9c1e0502013-03-18 17:49:35 +0100289{
Bob Liu81f35162015-11-14 11:12:11 +0800290 struct blkfront_info *info = rinfo->dev_info;
Roger Pau Monne9c1e0502013-03-18 17:49:35 +0100291 struct page *granted_page;
292 struct grant *gnt_list_entry, *n;
293 int i = 0;
294
Konrad Rzeszutek Wilk6f03a7f2015-11-16 15:14:41 -0500295 while (i < num) {
Roger Pau Monne9c1e0502013-03-18 17:49:35 +0100296 gnt_list_entry = kzalloc(sizeof(struct grant), GFP_NOIO);
297 if (!gnt_list_entry)
298 goto out_of_memory;
299
Roger Pau Monnebfe11d62013-10-29 18:31:14 +0100300 if (info->feature_persistent) {
301 granted_page = alloc_page(GFP_NOIO);
302 if (!granted_page) {
303 kfree(gnt_list_entry);
304 goto out_of_memory;
305 }
Julien Gralla7a6df22015-06-30 11:58:51 +0100306 gnt_list_entry->page = granted_page;
Roger Pau Monne9c1e0502013-03-18 17:49:35 +0100307 }
308
Roger Pau Monne9c1e0502013-03-18 17:49:35 +0100309 gnt_list_entry->gref = GRANT_INVALID_REF;
Bob Liu73716df2015-11-16 16:51:39 -0500310 list_add(&gnt_list_entry->node, &rinfo->grants);
Roger Pau Monne9c1e0502013-03-18 17:49:35 +0100311 i++;
312 }
313
314 return 0;
315
316out_of_memory:
317 list_for_each_entry_safe(gnt_list_entry, n,
Bob Liu73716df2015-11-16 16:51:39 -0500318 &rinfo->grants, node) {
Roger Pau Monne9c1e0502013-03-18 17:49:35 +0100319 list_del(&gnt_list_entry->node);
Roger Pau Monnebfe11d62013-10-29 18:31:14 +0100320 if (info->feature_persistent)
Julien Gralla7a6df22015-06-30 11:58:51 +0100321 __free_page(gnt_list_entry->page);
Roger Pau Monne9c1e0502013-03-18 17:49:35 +0100322 kfree(gnt_list_entry);
323 i--;
324 }
325 BUG_ON(i != 0);
326 return -ENOMEM;
327}
328
Bob Liu73716df2015-11-16 16:51:39 -0500329static struct grant *get_free_grant(struct blkfront_ring_info *rinfo)
Roger Pau Monne9c1e0502013-03-18 17:49:35 +0100330{
331 struct grant *gnt_list_entry;
Roger Pau Monne9c1e0502013-03-18 17:49:35 +0100332
Bob Liu73716df2015-11-16 16:51:39 -0500333 BUG_ON(list_empty(&rinfo->grants));
334 gnt_list_entry = list_first_entry(&rinfo->grants, struct grant,
Julien Grall4f503fb2015-07-01 14:10:38 +0100335 node);
Roger Pau Monne9c1e0502013-03-18 17:49:35 +0100336 list_del(&gnt_list_entry->node);
337
Julien Grall4f503fb2015-07-01 14:10:38 +0100338 if (gnt_list_entry->gref != GRANT_INVALID_REF)
Bob Liu73716df2015-11-16 16:51:39 -0500339 rinfo->persistent_gnts_c--;
Julien Grall4f503fb2015-07-01 14:10:38 +0100340
341 return gnt_list_entry;
342}
343
344static inline void grant_foreign_access(const struct grant *gnt_list_entry,
345 const struct blkfront_info *info)
346{
347 gnttab_page_grant_foreign_access_ref_one(gnt_list_entry->gref,
348 info->xbdev->otherend_id,
349 gnt_list_entry->page,
350 0);
351}
352
353static struct grant *get_grant(grant_ref_t *gref_head,
354 unsigned long gfn,
Bob Liu73716df2015-11-16 16:51:39 -0500355 struct blkfront_ring_info *rinfo)
Julien Grall4f503fb2015-07-01 14:10:38 +0100356{
Bob Liu73716df2015-11-16 16:51:39 -0500357 struct grant *gnt_list_entry = get_free_grant(rinfo);
358 struct blkfront_info *info = rinfo->dev_info;
Julien Grall4f503fb2015-07-01 14:10:38 +0100359
360 if (gnt_list_entry->gref != GRANT_INVALID_REF)
Roger Pau Monne9c1e0502013-03-18 17:49:35 +0100361 return gnt_list_entry;
Julien Grall4f503fb2015-07-01 14:10:38 +0100362
363 /* Assign a gref to this page */
364 gnt_list_entry->gref = gnttab_claim_grant_reference(gref_head);
365 BUG_ON(gnt_list_entry->gref == -ENOSPC);
366 if (info->feature_persistent)
367 grant_foreign_access(gnt_list_entry, info);
368 else {
369 /* Grant access to the GFN passed by the caller */
370 gnttab_grant_foreign_access_ref(gnt_list_entry->gref,
371 info->xbdev->otherend_id,
372 gfn, 0);
Roger Pau Monne9c1e0502013-03-18 17:49:35 +0100373 }
374
Julien Grall4f503fb2015-07-01 14:10:38 +0100375 return gnt_list_entry;
376}
377
378static struct grant *get_indirect_grant(grant_ref_t *gref_head,
Bob Liu73716df2015-11-16 16:51:39 -0500379 struct blkfront_ring_info *rinfo)
Julien Grall4f503fb2015-07-01 14:10:38 +0100380{
Bob Liu73716df2015-11-16 16:51:39 -0500381 struct grant *gnt_list_entry = get_free_grant(rinfo);
382 struct blkfront_info *info = rinfo->dev_info;
Julien Grall4f503fb2015-07-01 14:10:38 +0100383
384 if (gnt_list_entry->gref != GRANT_INVALID_REF)
385 return gnt_list_entry;
386
Roger Pau Monne9c1e0502013-03-18 17:49:35 +0100387 /* Assign a gref to this page */
388 gnt_list_entry->gref = gnttab_claim_grant_reference(gref_head);
389 BUG_ON(gnt_list_entry->gref == -ENOSPC);
Roger Pau Monnebfe11d62013-10-29 18:31:14 +0100390 if (!info->feature_persistent) {
Julien Grall4f503fb2015-07-01 14:10:38 +0100391 struct page *indirect_page;
392
393 /* Fetch a pre-allocated page to use for indirect grefs */
Bob Liu73716df2015-11-16 16:51:39 -0500394 BUG_ON(list_empty(&rinfo->indirect_pages));
395 indirect_page = list_first_entry(&rinfo->indirect_pages,
Julien Grall4f503fb2015-07-01 14:10:38 +0100396 struct page, lru);
397 list_del(&indirect_page->lru);
398 gnt_list_entry->page = indirect_page;
Roger Pau Monnebfe11d62013-10-29 18:31:14 +0100399 }
Julien Grall4f503fb2015-07-01 14:10:38 +0100400 grant_foreign_access(gnt_list_entry, info);
401
Roger Pau Monne9c1e0502013-03-18 17:49:35 +0100402 return gnt_list_entry;
403}
404
Konrad Rzeszutek Wilk6878c322012-05-25 17:34:51 -0400405static const char *op_name(int op)
406{
407 static const char *const names[] = {
408 [BLKIF_OP_READ] = "read",
409 [BLKIF_OP_WRITE] = "write",
410 [BLKIF_OP_WRITE_BARRIER] = "barrier",
411 [BLKIF_OP_FLUSH_DISKCACHE] = "flush",
412 [BLKIF_OP_DISCARD] = "discard" };
413
414 if (op < 0 || op >= ARRAY_SIZE(names))
415 return "unknown";
416
417 if (!names[op])
418 return "reserved";
419
420 return names[op];
421}
Jan Beulich0e345822010-08-07 18:28:55 +0200422static int xlbd_reserve_minors(unsigned int minor, unsigned int nr)
423{
424 unsigned int end = minor + nr;
425 int rc;
426
427 if (end > nr_minors) {
428 unsigned long *bitmap, *old;
429
Thomas Meyerf0941482011-11-29 22:08:00 +0100430 bitmap = kcalloc(BITS_TO_LONGS(end), sizeof(*bitmap),
Jan Beulich0e345822010-08-07 18:28:55 +0200431 GFP_KERNEL);
432 if (bitmap == NULL)
433 return -ENOMEM;
434
435 spin_lock(&minor_lock);
436 if (end > nr_minors) {
437 old = minors;
438 memcpy(bitmap, minors,
439 BITS_TO_LONGS(nr_minors) * sizeof(*bitmap));
440 minors = bitmap;
441 nr_minors = BITS_TO_LONGS(end) * BITS_PER_LONG;
442 } else
443 old = bitmap;
444 spin_unlock(&minor_lock);
445 kfree(old);
446 }
447
448 spin_lock(&minor_lock);
449 if (find_next_bit(minors, end, minor) >= end) {
Akinobu Mita34ae2e42012-01-21 00:15:26 +0900450 bitmap_set(minors, minor, nr);
Jan Beulich0e345822010-08-07 18:28:55 +0200451 rc = 0;
452 } else
453 rc = -EBUSY;
454 spin_unlock(&minor_lock);
455
456 return rc;
457}
458
459static void xlbd_release_minors(unsigned int minor, unsigned int nr)
460{
461 unsigned int end = minor + nr;
462
463 BUG_ON(end > nr_minors);
464 spin_lock(&minor_lock);
Akinobu Mita34ae2e42012-01-21 00:15:26 +0900465 bitmap_clear(minors, minor, nr);
Jan Beulich0e345822010-08-07 18:28:55 +0200466 spin_unlock(&minor_lock);
467}
468
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700469static void blkif_restart_queue_callback(void *arg)
470{
Bob Liu81f35162015-11-14 11:12:11 +0800471 struct blkfront_ring_info *rinfo = (struct blkfront_ring_info *)arg;
472 schedule_work(&rinfo->work);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700473}
474
Harvey Harrisonafe42d72008-04-29 00:59:47 -0700475static int blkif_getgeo(struct block_device *bd, struct hd_geometry *hg)
Ian Campbell597592d2008-02-21 13:03:45 -0800476{
477 /* We don't have real geometry info, but let's at least return
478 values consistent with the size of the device */
479 sector_t nsect = get_capacity(bd->bd_disk);
480 sector_t cylinders = nsect;
481
482 hg->heads = 0xff;
483 hg->sectors = 0x3f;
484 sector_div(cylinders, hg->heads * hg->sectors);
485 hg->cylinders = cylinders;
486 if ((sector_t)(hg->cylinders + 1) * hg->heads * hg->sectors < nsect)
487 hg->cylinders = 0xffff;
488 return 0;
489}
490
Al Viroa63c8482008-03-02 10:23:47 -0500491static int blkif_ioctl(struct block_device *bdev, fmode_t mode,
Adrian Bunk62aa0052008-08-04 11:59:05 +0200492 unsigned command, unsigned long argument)
Christian Limpach440a01a2008-06-17 10:47:08 +0200493{
Al Viroa63c8482008-03-02 10:23:47 -0500494 struct blkfront_info *info = bdev->bd_disk->private_data;
Christian Limpach440a01a2008-06-17 10:47:08 +0200495 int i;
496
497 dev_dbg(&info->xbdev->dev, "command: 0x%x, argument: 0x%lx\n",
498 command, (long)argument);
499
500 switch (command) {
501 case CDROMMULTISESSION:
502 dev_dbg(&info->xbdev->dev, "FIXME: support multisession CDs later\n");
503 for (i = 0; i < sizeof(struct cdrom_multisession); i++)
504 if (put_user(0, (char __user *)(argument + i)))
505 return -EFAULT;
506 return 0;
507
508 case CDROM_GET_CAPABILITY: {
509 struct gendisk *gd = info->gd;
510 if (gd->flags & GENHD_FL_CD)
511 return 0;
512 return -EINVAL;
513 }
514
515 default:
516 /*printk(KERN_ALERT "ioctl %08x not supported by Xen blkdev\n",
517 command);*/
518 return -EINVAL; /* same return as native Linux */
519 }
520
521 return 0;
522}
523
Julien Grall2e073962015-08-13 19:23:10 +0100524static unsigned long blkif_ring_get_request(struct blkfront_ring_info *rinfo,
525 struct request *req,
526 struct blkif_request **ring_req)
527{
528 unsigned long id;
529
530 *ring_req = RING_GET_REQUEST(&rinfo->ring, rinfo->ring.req_prod_pvt);
531 rinfo->ring.req_prod_pvt++;
532
533 id = get_id_from_freelist(rinfo);
534 rinfo->shadow[id].request = req;
Julien Grall6cc568332015-08-13 13:13:35 +0100535 rinfo->shadow[id].status = REQ_WAITING;
536 rinfo->shadow[id].associated_id = NO_ASSOCIATED_ID;
Julien Grall2e073962015-08-13 19:23:10 +0100537
538 (*ring_req)->u.rw.id = id;
539
540 return id;
541}
542
Bob Liu81f35162015-11-14 11:12:11 +0800543static int blkif_queue_discard_req(struct request *req, struct blkfront_ring_info *rinfo)
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700544{
Bob Liu81f35162015-11-14 11:12:11 +0800545 struct blkfront_info *info = rinfo->dev_info;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700546 struct blkif_request *ring_req;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700547 unsigned long id;
Julien Grall33204662015-06-29 17:35:24 +0100548
549 /* Fill out a communications ring structure. */
Julien Grall2e073962015-08-13 19:23:10 +0100550 id = blkif_ring_get_request(rinfo, req, &ring_req);
Julien Grall33204662015-06-29 17:35:24 +0100551
552 ring_req->operation = BLKIF_OP_DISCARD;
553 ring_req->u.discard.nr_sectors = blk_rq_sectors(req);
554 ring_req->u.discard.id = id;
555 ring_req->u.discard.sector_number = (blkif_sector_t)blk_rq_pos(req);
Christoph Hellwig288dab82016-06-09 16:00:36 +0200556 if (req_op(req) == REQ_OP_SECURE_ERASE && info->feature_secdiscard)
Julien Grall33204662015-06-29 17:35:24 +0100557 ring_req->u.discard.flag = BLKIF_DISCARD_SECURE;
558 else
559 ring_req->u.discard.flag = 0;
560
Julien Grall33204662015-06-29 17:35:24 +0100561 /* Keep a private copy so we can reissue requests when recovering. */
Bob Liu81f35162015-11-14 11:12:11 +0800562 rinfo->shadow[id].req = *ring_req;
Julien Grall33204662015-06-29 17:35:24 +0100563
564 return 0;
565}
566
Julien Grallc004a6f2015-07-22 16:44:54 +0100567struct setup_rw_req {
568 unsigned int grant_idx;
569 struct blkif_request_segment *segments;
Bob Liu81f35162015-11-14 11:12:11 +0800570 struct blkfront_ring_info *rinfo;
Julien Grallc004a6f2015-07-22 16:44:54 +0100571 struct blkif_request *ring_req;
572 grant_ref_t gref_head;
573 unsigned int id;
574 /* Only used when persistent grant is used and it's a read request */
575 bool need_copy;
576 unsigned int bvec_off;
577 char *bvec_data;
Julien Grall6cc568332015-08-13 13:13:35 +0100578
579 bool require_extra_req;
580 struct blkif_request *extra_ring_req;
Julien Grallc004a6f2015-07-22 16:44:54 +0100581};
582
583static void blkif_setup_rw_req_grant(unsigned long gfn, unsigned int offset,
584 unsigned int len, void *data)
585{
586 struct setup_rw_req *setup = data;
587 int n, ref;
588 struct grant *gnt_list_entry;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700589 unsigned int fsect, lsect;
Julien Grallc004a6f2015-07-22 16:44:54 +0100590 /* Convenient aliases */
591 unsigned int grant_idx = setup->grant_idx;
592 struct blkif_request *ring_req = setup->ring_req;
Bob Liu81f35162015-11-14 11:12:11 +0800593 struct blkfront_ring_info *rinfo = setup->rinfo;
Julien Grall6cc568332015-08-13 13:13:35 +0100594 /*
595 * We always use the shadow of the first request to store the list
596 * of grant associated to the block I/O request. This made the
597 * completion more easy to handle even if the block I/O request is
598 * split.
599 */
Bob Liu81f35162015-11-14 11:12:11 +0800600 struct blk_shadow *shadow = &rinfo->shadow[setup->id];
Julien Grallc004a6f2015-07-22 16:44:54 +0100601
Julien Grall6cc568332015-08-13 13:13:35 +0100602 if (unlikely(setup->require_extra_req &&
603 grant_idx >= BLKIF_MAX_SEGMENTS_PER_REQUEST)) {
604 /*
605 * We are using the second request, setup grant_idx
606 * to be the index of the segment array.
607 */
608 grant_idx -= BLKIF_MAX_SEGMENTS_PER_REQUEST;
609 ring_req = setup->extra_ring_req;
610 }
611
Julien Grallc004a6f2015-07-22 16:44:54 +0100612 if ((ring_req->operation == BLKIF_OP_INDIRECT) &&
613 (grant_idx % GRANTS_PER_INDIRECT_FRAME == 0)) {
614 if (setup->segments)
615 kunmap_atomic(setup->segments);
616
617 n = grant_idx / GRANTS_PER_INDIRECT_FRAME;
Bob Liu73716df2015-11-16 16:51:39 -0500618 gnt_list_entry = get_indirect_grant(&setup->gref_head, rinfo);
Julien Grallc004a6f2015-07-22 16:44:54 +0100619 shadow->indirect_grants[n] = gnt_list_entry;
620 setup->segments = kmap_atomic(gnt_list_entry->page);
621 ring_req->u.indirect.indirect_grefs[n] = gnt_list_entry->gref;
622 }
623
Bob Liu73716df2015-11-16 16:51:39 -0500624 gnt_list_entry = get_grant(&setup->gref_head, gfn, rinfo);
Julien Grallc004a6f2015-07-22 16:44:54 +0100625 ref = gnt_list_entry->gref;
Julien Grall6cc568332015-08-13 13:13:35 +0100626 /*
627 * All the grants are stored in the shadow of the first
628 * request. Therefore we have to use the global index.
629 */
630 shadow->grants_used[setup->grant_idx] = gnt_list_entry;
Julien Grallc004a6f2015-07-22 16:44:54 +0100631
632 if (setup->need_copy) {
633 void *shared_data;
634
635 shared_data = kmap_atomic(gnt_list_entry->page);
636 /*
637 * this does not wipe data stored outside the
638 * range sg->offset..sg->offset+sg->length.
639 * Therefore, blkback *could* see data from
640 * previous requests. This is OK as long as
641 * persistent grants are shared with just one
642 * domain. It may need refactoring if this
643 * changes
644 */
645 memcpy(shared_data + offset,
646 setup->bvec_data + setup->bvec_off,
647 len);
648
649 kunmap_atomic(shared_data);
650 setup->bvec_off += len;
651 }
652
653 fsect = offset >> 9;
654 lsect = fsect + (len >> 9) - 1;
655 if (ring_req->operation != BLKIF_OP_INDIRECT) {
656 ring_req->u.rw.seg[grant_idx] =
657 (struct blkif_request_segment) {
658 .gref = ref,
659 .first_sect = fsect,
660 .last_sect = lsect };
661 } else {
662 setup->segments[grant_idx % GRANTS_PER_INDIRECT_FRAME] =
663 (struct blkif_request_segment) {
664 .gref = ref,
665 .first_sect = fsect,
666 .last_sect = lsect };
667 }
668
669 (setup->grant_idx)++;
670}
671
Julien Grall6cc568332015-08-13 13:13:35 +0100672static void blkif_setup_extra_req(struct blkif_request *first,
673 struct blkif_request *second)
674{
675 uint16_t nr_segments = first->u.rw.nr_segments;
676
677 /*
678 * The second request is only present when the first request uses
679 * all its segments. It's always the continuity of the first one.
680 */
681 first->u.rw.nr_segments = BLKIF_MAX_SEGMENTS_PER_REQUEST;
682
683 second->u.rw.nr_segments = nr_segments - BLKIF_MAX_SEGMENTS_PER_REQUEST;
684 second->u.rw.sector_number = first->u.rw.sector_number +
685 (BLKIF_MAX_SEGMENTS_PER_REQUEST * XEN_PAGE_SIZE) / 512;
686
687 second->u.rw.handle = first->u.rw.handle;
688 second->operation = first->operation;
689}
690
Bob Liu81f35162015-11-14 11:12:11 +0800691static int blkif_queue_rw_req(struct request *req, struct blkfront_ring_info *rinfo)
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700692{
Bob Liu81f35162015-11-14 11:12:11 +0800693 struct blkfront_info *info = rinfo->dev_info;
Julien Grall6cc568332015-08-13 13:13:35 +0100694 struct blkif_request *ring_req, *extra_ring_req = NULL;
695 unsigned long id, extra_id = NO_ASSOCIATED_ID;
696 bool require_extra_req = false;
Julien Grallc004a6f2015-07-22 16:44:54 +0100697 int i;
698 struct setup_rw_req setup = {
699 .grant_idx = 0,
700 .segments = NULL,
Bob Liu81f35162015-11-14 11:12:11 +0800701 .rinfo = rinfo,
Julien Grallc004a6f2015-07-22 16:44:54 +0100702 .need_copy = rq_data_dir(req) && info->feature_persistent,
703 };
Roger Pau Monne0a8704a2012-10-24 18:58:45 +0200704
705 /*
706 * Used to store if we are able to queue the request by just using
707 * existing persistent grants, or if we have to get new grants,
708 * as there are not sufficiently many free.
709 */
Dongli Zhangbd912ef32017-06-28 20:57:28 +0800710 bool new_persistent_gnts = false;
Jens Axboe9e973e62009-02-24 08:10:09 +0100711 struct scatterlist *sg;
Julien Grallc004a6f2015-07-22 16:44:54 +0100712 int num_sg, max_grefs, num_grant;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700713
Julien Grallc004a6f2015-07-22 16:44:54 +0100714 max_grefs = req->nr_phys_segments * GRANTS_PER_PSEG;
Roger Pau Monnec47206e2013-08-12 12:53:43 +0200715 if (max_grefs > BLKIF_MAX_SEGMENTS_PER_REQUEST)
716 /*
717 * If we are using indirect segments we need to account
718 * for the indirect grefs used in the request.
719 */
Julien Grallc004a6f2015-07-22 16:44:54 +0100720 max_grefs += INDIRECT_GREFS(max_grefs);
Roger Pau Monne402b27f2013-04-18 16:06:54 +0200721
Dongli Zhangbd912ef32017-06-28 20:57:28 +0800722 /* Check if we have enough persistent grants to allocate a requests */
723 if (rinfo->persistent_gnts_c < max_grefs) {
724 new_persistent_gnts = true;
725
726 if (gnttab_alloc_grant_references(
727 max_grefs - rinfo->persistent_gnts_c,
728 &setup.gref_head) < 0) {
Roger Pau Monne0a8704a2012-10-24 18:58:45 +0200729 gnttab_request_free_callback(
Bob Liu81f35162015-11-14 11:12:11 +0800730 &rinfo->callback,
Roger Pau Monne0a8704a2012-10-24 18:58:45 +0200731 blkif_restart_queue_callback,
Bob Liu81f35162015-11-14 11:12:11 +0800732 rinfo,
Dongli Zhangbd912ef32017-06-28 20:57:28 +0800733 max_grefs - rinfo->persistent_gnts_c);
Roger Pau Monne0a8704a2012-10-24 18:58:45 +0200734 return 1;
735 }
Dongli Zhangbd912ef32017-06-28 20:57:28 +0800736 }
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700737
738 /* Fill out a communications ring structure. */
Julien Grall2e073962015-08-13 19:23:10 +0100739 id = blkif_ring_get_request(rinfo, req, &ring_req);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700740
Bob Liu81f35162015-11-14 11:12:11 +0800741 num_sg = blk_rq_map_sg(req->q, req, rinfo->shadow[id].sg);
Julien Grallc004a6f2015-07-22 16:44:54 +0100742 num_grant = 0;
743 /* Calculate the number of grant used */
Bob Liu81f35162015-11-14 11:12:11 +0800744 for_each_sg(rinfo->shadow[id].sg, sg, num_sg, i)
Julien Grallc004a6f2015-07-22 16:44:54 +0100745 num_grant += gnttab_count_grant(sg->offset, sg->length);
746
Julien Grall6cc568332015-08-13 13:13:35 +0100747 require_extra_req = info->max_indirect_segments == 0 &&
748 num_grant > BLKIF_MAX_SEGMENTS_PER_REQUEST;
749 BUG_ON(!HAS_EXTRA_REQ && require_extra_req);
750
Bob Liu81f35162015-11-14 11:12:11 +0800751 rinfo->shadow[id].num_sg = num_sg;
Julien Grall6cc568332015-08-13 13:13:35 +0100752 if (num_grant > BLKIF_MAX_SEGMENTS_PER_REQUEST &&
753 likely(!require_extra_req)) {
Julien Grall33204662015-06-29 17:35:24 +0100754 /*
755 * The indirect operation can only be a BLKIF_OP_READ or
756 * BLKIF_OP_WRITE
757 */
Mike Christie3a5e02c2016-06-05 14:32:23 -0500758 BUG_ON(req_op(req) == REQ_OP_FLUSH || req->cmd_flags & REQ_FUA);
Julien Grall33204662015-06-29 17:35:24 +0100759 ring_req->operation = BLKIF_OP_INDIRECT;
760 ring_req->u.indirect.indirect_op = rq_data_dir(req) ?
761 BLKIF_OP_WRITE : BLKIF_OP_READ;
762 ring_req->u.indirect.sector_number = (blkif_sector_t)blk_rq_pos(req);
763 ring_req->u.indirect.handle = info->handle;
Julien Grallc004a6f2015-07-22 16:44:54 +0100764 ring_req->u.indirect.nr_segments = num_grant;
Li Dongyanged30bf32011-09-01 18:39:09 +0800765 } else {
Julien Grall33204662015-06-29 17:35:24 +0100766 ring_req->u.rw.sector_number = (blkif_sector_t)blk_rq_pos(req);
767 ring_req->u.rw.handle = info->handle;
768 ring_req->operation = rq_data_dir(req) ?
769 BLKIF_OP_WRITE : BLKIF_OP_READ;
Mike Christie3a5e02c2016-06-05 14:32:23 -0500770 if (req_op(req) == REQ_OP_FLUSH || req->cmd_flags & REQ_FUA) {
Roger Pau Monne402b27f2013-04-18 16:06:54 +0200771 /*
Julien Grall33204662015-06-29 17:35:24 +0100772 * Ideally we can do an unordered flush-to-disk.
773 * In case the backend onlysupports barriers, use that.
774 * A barrier request a superset of FUA, so we can
775 * implement it the same way. (It's also a FLUSH+FUA,
776 * since it is guaranteed ordered WRT previous writes.)
Roger Pau Monne402b27f2013-04-18 16:06:54 +0200777 */
Mike Christiea4180902016-06-05 14:32:24 -0500778 if (info->feature_flush && info->feature_fua)
Julien Grall33204662015-06-29 17:35:24 +0100779 ring_req->operation =
780 BLKIF_OP_WRITE_BARRIER;
Mike Christiea4180902016-06-05 14:32:24 -0500781 else if (info->feature_flush)
Julien Grall33204662015-06-29 17:35:24 +0100782 ring_req->operation =
783 BLKIF_OP_FLUSH_DISKCACHE;
Mike Christiea4180902016-06-05 14:32:24 -0500784 else
Julien Grall33204662015-06-29 17:35:24 +0100785 ring_req->operation = 0;
Li Dongyanged30bf32011-09-01 18:39:09 +0800786 }
Julien Grallc004a6f2015-07-22 16:44:54 +0100787 ring_req->u.rw.nr_segments = num_grant;
Julien Grall6cc568332015-08-13 13:13:35 +0100788 if (unlikely(require_extra_req)) {
789 extra_id = blkif_ring_get_request(rinfo, req,
790 &extra_ring_req);
791 /*
792 * Only the first request contains the scatter-gather
793 * list.
794 */
795 rinfo->shadow[extra_id].num_sg = 0;
796
797 blkif_setup_extra_req(ring_req, extra_ring_req);
798
799 /* Link the 2 requests together */
800 rinfo->shadow[extra_id].associated_id = id;
801 rinfo->shadow[id].associated_id = extra_id;
802 }
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700803 }
804
Julien Grallc004a6f2015-07-22 16:44:54 +0100805 setup.ring_req = ring_req;
806 setup.id = id;
Julien Grall6cc568332015-08-13 13:13:35 +0100807
808 setup.require_extra_req = require_extra_req;
809 if (unlikely(require_extra_req))
810 setup.extra_ring_req = extra_ring_req;
811
Bob Liu81f35162015-11-14 11:12:11 +0800812 for_each_sg(rinfo->shadow[id].sg, sg, num_sg, i) {
Julien Grallc004a6f2015-07-22 16:44:54 +0100813 BUG_ON(sg->offset + sg->length > PAGE_SIZE);
Julien Grall33204662015-06-29 17:35:24 +0100814
Julien Grallc004a6f2015-07-22 16:44:54 +0100815 if (setup.need_copy) {
816 setup.bvec_off = sg->offset;
817 setup.bvec_data = kmap_atomic(sg_page(sg));
Julien Grall33204662015-06-29 17:35:24 +0100818 }
819
Julien Grallc004a6f2015-07-22 16:44:54 +0100820 gnttab_foreach_grant_in_range(sg_page(sg),
821 sg->offset,
822 sg->length,
823 blkif_setup_rw_req_grant,
824 &setup);
Julien Grall33204662015-06-29 17:35:24 +0100825
Julien Grallc004a6f2015-07-22 16:44:54 +0100826 if (setup.need_copy)
827 kunmap_atomic(setup.bvec_data);
Julien Grall33204662015-06-29 17:35:24 +0100828 }
Julien Grallc004a6f2015-07-22 16:44:54 +0100829 if (setup.segments)
830 kunmap_atomic(setup.segments);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700831
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700832 /* Keep a private copy so we can reissue requests when recovering. */
Bob Liu81f35162015-11-14 11:12:11 +0800833 rinfo->shadow[id].req = *ring_req;
Julien Grall6cc568332015-08-13 13:13:35 +0100834 if (unlikely(require_extra_req))
835 rinfo->shadow[extra_id].req = *extra_ring_req;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700836
Dongli Zhangbd912ef32017-06-28 20:57:28 +0800837 if (new_persistent_gnts)
Julien Grallc004a6f2015-07-22 16:44:54 +0100838 gnttab_free_grant_references(setup.gref_head);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700839
840 return 0;
841}
842
Julien Grall33204662015-06-29 17:35:24 +0100843/*
844 * Generate a Xen blkfront IO request from a blk layer request. Reads
845 * and writes are handled as expected.
846 *
847 * @req: a request struct
848 */
Bob Liu81f35162015-11-14 11:12:11 +0800849static int blkif_queue_request(struct request *req, struct blkfront_ring_info *rinfo)
Julien Grall33204662015-06-29 17:35:24 +0100850{
Bob Liu81f35162015-11-14 11:12:11 +0800851 if (unlikely(rinfo->dev_info->connected != BLKIF_STATE_CONNECTED))
Julien Grall33204662015-06-29 17:35:24 +0100852 return 1;
853
Mike Christiec2df40d2016-06-05 14:32:17 -0500854 if (unlikely(req_op(req) == REQ_OP_DISCARD ||
Christoph Hellwig288dab82016-06-09 16:00:36 +0200855 req_op(req) == REQ_OP_SECURE_ERASE))
Bob Liu81f35162015-11-14 11:12:11 +0800856 return blkif_queue_discard_req(req, rinfo);
Julien Grall33204662015-06-29 17:35:24 +0100857 else
Bob Liu81f35162015-11-14 11:12:11 +0800858 return blkif_queue_rw_req(req, rinfo);
Julien Grall33204662015-06-29 17:35:24 +0100859}
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700860
Bob Liu81f35162015-11-14 11:12:11 +0800861static inline void flush_requests(struct blkfront_ring_info *rinfo)
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700862{
863 int notify;
864
Bob Liu81f35162015-11-14 11:12:11 +0800865 RING_PUSH_REQUESTS_AND_CHECK_NOTIFY(&rinfo->ring, notify);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700866
867 if (notify)
Bob Liu81f35162015-11-14 11:12:11 +0800868 notify_remote_via_irq(rinfo->irq);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700869}
870
Vitaly Kuznetsovad42d392014-12-01 14:01:13 +0100871static inline bool blkif_request_flush_invalid(struct request *req,
872 struct blkfront_info *info)
Arianna Avanzini0f1ca652014-08-22 13:20:02 +0200873{
Christoph Hellwigaebf5262017-01-31 16:57:31 +0100874 return (blk_rq_is_passthrough(req) ||
Mike Christie3a5e02c2016-06-05 14:32:23 -0500875 ((req_op(req) == REQ_OP_FLUSH) &&
Mike Christiea4180902016-06-05 14:32:24 -0500876 !info->feature_flush) ||
Vitaly Kuznetsovad42d392014-12-01 14:01:13 +0100877 ((req->cmd_flags & REQ_FUA) &&
Mike Christiea4180902016-06-05 14:32:24 -0500878 !info->feature_fua));
Arianna Avanzini0f1ca652014-08-22 13:20:02 +0200879}
880
Christoph Hellwigfc17b652017-06-03 09:38:05 +0200881static blk_status_t blkif_queue_rq(struct blk_mq_hw_ctx *hctx,
Konrad Rzeszutek Wilk6f03a7f2015-11-16 15:14:41 -0500882 const struct blk_mq_queue_data *qd)
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700883{
Bob Liu11659562015-11-14 11:12:13 +0800884 unsigned long flags;
Bob Liu2a6f71a2016-05-31 16:59:17 +0800885 int qid = hctx->queue_num;
886 struct blkfront_info *info = hctx->queue->queuedata;
887 struct blkfront_ring_info *rinfo = NULL;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700888
Bob Liu2a6f71a2016-05-31 16:59:17 +0800889 BUG_ON(info->nr_rings <= qid);
890 rinfo = &info->rinfo[qid];
Bob Liu907c3eb2015-07-13 17:55:24 +0800891 blk_mq_start_request(qd->rq);
Bob Liu11659562015-11-14 11:12:13 +0800892 spin_lock_irqsave(&rinfo->ring_lock, flags);
Bob Liu81f35162015-11-14 11:12:11 +0800893 if (RING_FULL(&rinfo->ring))
Bob Liu907c3eb2015-07-13 17:55:24 +0800894 goto out_busy;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700895
Bob Liu81f35162015-11-14 11:12:11 +0800896 if (blkif_request_flush_invalid(qd->rq, rinfo->dev_info))
Bob Liu907c3eb2015-07-13 17:55:24 +0800897 goto out_err;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700898
Bob Liu81f35162015-11-14 11:12:11 +0800899 if (blkif_queue_request(qd->rq, rinfo))
Bob Liu907c3eb2015-07-13 17:55:24 +0800900 goto out_busy;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700901
Bob Liu81f35162015-11-14 11:12:11 +0800902 flush_requests(rinfo);
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_OK;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700905
Bob Liu907c3eb2015-07-13 17:55:24 +0800906out_err:
Bob Liu11659562015-11-14 11:12:13 +0800907 spin_unlock_irqrestore(&rinfo->ring_lock, flags);
Christoph Hellwigfc17b652017-06-03 09:38:05 +0200908 return BLK_STS_IOERR;
Tejun Heo296b2f62009-05-08 11:54:15 +0900909
Bob Liu907c3eb2015-07-13 17:55:24 +0800910out_busy:
Bob Liu907c3eb2015-07-13 17:55:24 +0800911 blk_mq_stop_hw_queue(hctx);
Junxiao Bi4b422cb2017-07-20 09:26:21 +0800912 spin_unlock_irqrestore(&rinfo->ring_lock, flags);
Ming Lei86ff7c22018-01-30 22:04:57 -0500913 return BLK_STS_DEV_RESOURCE;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700914}
915
Christoph Hellwig26095872017-04-20 16:03:08 +0200916static void blkif_complete_rq(struct request *rq)
917{
918 blk_mq_end_request(rq, blkif_req(rq)->error);
919}
920
Eric Biggersf363b082017-03-30 13:39:16 -0700921static const struct blk_mq_ops blkfront_mq_ops = {
Bob Liu907c3eb2015-07-13 17:55:24 +0800922 .queue_rq = blkif_queue_rq,
Christoph Hellwig26095872017-04-20 16:03:08 +0200923 .complete = blkif_complete_rq,
Bob Liu907c3eb2015-07-13 17:55:24 +0800924};
925
Bob Liu172335a2016-07-01 17:43:39 -0400926static void blkif_set_queue_limits(struct blkfront_info *info)
927{
928 struct request_queue *rq = info->rq;
929 struct gendisk *gd = info->gd;
930 unsigned int segments = info->max_indirect_segments ? :
931 BLKIF_MAX_SEGMENTS_PER_REQUEST;
932
Bart Van Assche8b904b52018-03-07 17:10:10 -0800933 blk_queue_flag_set(QUEUE_FLAG_VIRT, rq);
Bob Liu172335a2016-07-01 17:43:39 -0400934
935 if (info->feature_discard) {
Bart Van Assche8b904b52018-03-07 17:10:10 -0800936 blk_queue_flag_set(QUEUE_FLAG_DISCARD, rq);
Bob Liu172335a2016-07-01 17:43:39 -0400937 blk_queue_max_discard_sectors(rq, get_capacity(gd));
938 rq->limits.discard_granularity = info->discard_granularity;
939 rq->limits.discard_alignment = info->discard_alignment;
940 if (info->feature_secdiscard)
Bart Van Assche8b904b52018-03-07 17:10:10 -0800941 blk_queue_flag_set(QUEUE_FLAG_SECERASE, rq);
Bob Liu172335a2016-07-01 17:43:39 -0400942 }
943
944 /* Hard sector size and max sectors impersonate the equiv. hardware. */
945 blk_queue_logical_block_size(rq, info->sector_size);
946 blk_queue_physical_block_size(rq, info->physical_sector_size);
947 blk_queue_max_hw_sectors(rq, (segments * XEN_PAGE_SIZE) / 512);
948
949 /* Each segment in a request is up to an aligned page in size. */
950 blk_queue_segment_boundary(rq, PAGE_SIZE - 1);
951 blk_queue_max_segment_size(rq, PAGE_SIZE);
952
953 /* Ensure a merged request will fit in a single I/O ring slot. */
954 blk_queue_max_segments(rq, segments / GRANTS_PER_PSEG);
955
956 /* Make sure buffer addresses are sector-aligned. */
957 blk_queue_dma_alignment(rq, 511);
Bob Liu172335a2016-07-01 17:43:39 -0400958}
959
Roger Pau Monne402b27f2013-04-18 16:06:54 +0200960static int xlvbd_init_blk_queue(struct gendisk *gd, u16 sector_size,
Bob Liu172335a2016-07-01 17:43:39 -0400961 unsigned int physical_sector_size)
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700962{
Jens Axboe165125e2007-07-24 09:28:11 +0200963 struct request_queue *rq;
Li Dongyanged30bf32011-09-01 18:39:09 +0800964 struct blkfront_info *info = gd->private_data;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700965
Bob Liu907c3eb2015-07-13 17:55:24 +0800966 memset(&info->tag_set, 0, sizeof(info->tag_set));
967 info->tag_set.ops = &blkfront_mq_ops;
Bob Liu28d949b2015-11-14 11:12:14 +0800968 info->tag_set.nr_hw_queues = info->nr_rings;
Julien Grall6cc568332015-08-13 13:13:35 +0100969 if (HAS_EXTRA_REQ && info->max_indirect_segments == 0) {
970 /*
971 * When indirect descriptior is not supported, the I/O request
972 * will be split between multiple request in the ring.
973 * To avoid problems when sending the request, divide by
974 * 2 the depth of the queue.
975 */
976 info->tag_set.queue_depth = BLK_RING_SIZE(info) / 2;
977 } else
978 info->tag_set.queue_depth = BLK_RING_SIZE(info);
Bob Liu907c3eb2015-07-13 17:55:24 +0800979 info->tag_set.numa_node = NUMA_NO_NODE;
Ming Lei56d18f62019-02-15 19:13:24 +0800980 info->tag_set.flags = BLK_MQ_F_SHOULD_MERGE;
Christoph Hellwig26095872017-04-20 16:03:08 +0200981 info->tag_set.cmd_size = sizeof(struct blkif_req);
Bob Liu907c3eb2015-07-13 17:55:24 +0800982 info->tag_set.driver_data = info;
983
984 if (blk_mq_alloc_tag_set(&info->tag_set))
Konrad Rzeszutek Wilkbde21f72015-11-25 13:07:39 -0500985 return -EINVAL;
Bob Liu907c3eb2015-07-13 17:55:24 +0800986 rq = blk_mq_init_queue(&info->tag_set);
987 if (IS_ERR(rq)) {
988 blk_mq_free_tag_set(&info->tag_set);
Konrad Rzeszutek Wilkbde21f72015-11-25 13:07:39 -0500989 return PTR_ERR(rq);
Bob Liu907c3eb2015-07-13 17:55:24 +0800990 }
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700991
Bob Liu2a6f71a2016-05-31 16:59:17 +0800992 rq->queuedata = info;
Bob Liu172335a2016-07-01 17:43:39 -0400993 info->rq = gd->queue = rq;
994 info->gd = gd;
995 info->sector_size = sector_size;
996 info->physical_sector_size = physical_sector_size;
997 blkif_set_queue_limits(info);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700998
999 return 0;
1000}
1001
Mike Christiea4180902016-06-05 14:32:24 -05001002static const char *flush_info(struct blkfront_info *info)
Vitaly Kuznetsovfdf9b962014-12-09 15:25:10 +01001003{
Mike Christiea4180902016-06-05 14:32:24 -05001004 if (info->feature_flush && info->feature_fua)
Vitaly Kuznetsovfdf9b962014-12-09 15:25:10 +01001005 return "barrier: enabled;";
Mike Christiea4180902016-06-05 14:32:24 -05001006 else if (info->feature_flush)
Vitaly Kuznetsovfdf9b962014-12-09 15:25:10 +01001007 return "flush diskcache: enabled;";
Mike Christiea4180902016-06-05 14:32:24 -05001008 else
Vitaly Kuznetsovfdf9b962014-12-09 15:25:10 +01001009 return "barrier or flush: disabled;";
Vitaly Kuznetsovfdf9b962014-12-09 15:25:10 +01001010}
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001011
Tejun Heo4913efe2010-09-03 11:56:16 +02001012static void xlvbd_flush(struct blkfront_info *info)
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001013{
Mike Christiea4180902016-06-05 14:32:24 -05001014 blk_queue_write_cache(info->rq, info->feature_flush ? true : false,
1015 info->feature_fua ? true : false);
Vitaly Kuznetsovfdf9b962014-12-09 15:25:10 +01001016 pr_info("blkfront: %s: %s %s %s %s %s\n",
Mike Christiea4180902016-06-05 14:32:24 -05001017 info->gd->disk_name, flush_info(info),
Vitaly Kuznetsovfdf9b962014-12-09 15:25:10 +01001018 "persistent grants:", info->feature_persistent ?
1019 "enabled;" : "disabled;", "indirect descriptors:",
1020 info->max_indirect_segments ? "enabled;" : "disabled;");
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001021}
1022
Stefano Stabellinic80a4202010-12-02 17:55:00 +00001023static int xen_translate_vdev(int vdevice, int *minor, unsigned int *offset)
1024{
1025 int major;
1026 major = BLKIF_MAJOR(vdevice);
1027 *minor = BLKIF_MINOR(vdevice);
1028 switch (major) {
1029 case XEN_IDE0_MAJOR:
1030 *offset = (*minor / 64) + EMULATED_HD_DISK_NAME_OFFSET;
1031 *minor = ((*minor / 64) * PARTS_PER_DISK) +
1032 EMULATED_HD_DISK_MINOR_OFFSET;
1033 break;
1034 case XEN_IDE1_MAJOR:
1035 *offset = (*minor / 64) + 2 + EMULATED_HD_DISK_NAME_OFFSET;
1036 *minor = (((*minor / 64) + 2) * PARTS_PER_DISK) +
1037 EMULATED_HD_DISK_MINOR_OFFSET;
1038 break;
1039 case XEN_SCSI_DISK0_MAJOR:
1040 *offset = (*minor / PARTS_PER_DISK) + EMULATED_SD_DISK_NAME_OFFSET;
1041 *minor = *minor + EMULATED_SD_DISK_MINOR_OFFSET;
1042 break;
1043 case XEN_SCSI_DISK1_MAJOR:
1044 case XEN_SCSI_DISK2_MAJOR:
1045 case XEN_SCSI_DISK3_MAJOR:
1046 case XEN_SCSI_DISK4_MAJOR:
1047 case XEN_SCSI_DISK5_MAJOR:
1048 case XEN_SCSI_DISK6_MAJOR:
1049 case XEN_SCSI_DISK7_MAJOR:
1050 *offset = (*minor / PARTS_PER_DISK) +
1051 ((major - XEN_SCSI_DISK1_MAJOR + 1) * 16) +
1052 EMULATED_SD_DISK_NAME_OFFSET;
1053 *minor = *minor +
1054 ((major - XEN_SCSI_DISK1_MAJOR + 1) * 16 * PARTS_PER_DISK) +
1055 EMULATED_SD_DISK_MINOR_OFFSET;
1056 break;
1057 case XEN_SCSI_DISK8_MAJOR:
1058 case XEN_SCSI_DISK9_MAJOR:
1059 case XEN_SCSI_DISK10_MAJOR:
1060 case XEN_SCSI_DISK11_MAJOR:
1061 case XEN_SCSI_DISK12_MAJOR:
1062 case XEN_SCSI_DISK13_MAJOR:
1063 case XEN_SCSI_DISK14_MAJOR:
1064 case XEN_SCSI_DISK15_MAJOR:
1065 *offset = (*minor / PARTS_PER_DISK) +
1066 ((major - XEN_SCSI_DISK8_MAJOR + 8) * 16) +
1067 EMULATED_SD_DISK_NAME_OFFSET;
1068 *minor = *minor +
1069 ((major - XEN_SCSI_DISK8_MAJOR + 8) * 16 * PARTS_PER_DISK) +
1070 EMULATED_SD_DISK_MINOR_OFFSET;
1071 break;
1072 case XENVBD_MAJOR:
1073 *offset = *minor / PARTS_PER_DISK;
1074 break;
1075 default:
1076 printk(KERN_WARNING "blkfront: your disk configuration is "
1077 "incorrect, please use an xvd device instead\n");
1078 return -ENODEV;
1079 }
1080 return 0;
1081}
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001082
Jan Beuliche77c78c2012-04-05 16:37:22 +01001083static char *encode_disk_name(char *ptr, unsigned int n)
1084{
1085 if (n >= 26)
1086 ptr = encode_disk_name(ptr, n / 26 - 1);
1087 *ptr = 'a' + n % 26;
1088 return ptr + 1;
1089}
1090
Chris Lalancette9246b5f2008-09-17 14:30:32 -07001091static int xlvbd_alloc_gendisk(blkif_sector_t capacity,
1092 struct blkfront_info *info,
Stefan Bader7c4d7d72013-05-13 16:28:15 +02001093 u16 vdisk_info, u16 sector_size,
1094 unsigned int physical_sector_size)
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001095{
1096 struct gendisk *gd;
1097 int nr_minors = 1;
Stefano Stabellinic80a4202010-12-02 17:55:00 +00001098 int err;
Chris Lalancette9246b5f2008-09-17 14:30:32 -07001099 unsigned int offset;
1100 int minor;
1101 int nr_parts;
Jan Beuliche77c78c2012-04-05 16:37:22 +01001102 char *ptr;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001103
1104 BUG_ON(info->gd != NULL);
1105 BUG_ON(info->rq != NULL);
1106
Chris Lalancette9246b5f2008-09-17 14:30:32 -07001107 if ((info->vdevice>>EXT_SHIFT) > 1) {
1108 /* this is above the extended range; something is wrong */
1109 printk(KERN_WARNING "blkfront: vdevice 0x%x is above the extended range; ignoring\n", info->vdevice);
1110 return -ENODEV;
1111 }
1112
1113 if (!VDEV_IS_EXTENDED(info->vdevice)) {
Stefano Stabellinic80a4202010-12-02 17:55:00 +00001114 err = xen_translate_vdev(info->vdevice, &minor, &offset);
1115 if (err)
1116 return err;
1117 nr_parts = PARTS_PER_DISK;
Chris Lalancette9246b5f2008-09-17 14:30:32 -07001118 } else {
1119 minor = BLKIF_MINOR_EXT(info->vdevice);
1120 nr_parts = PARTS_PER_EXT_DISK;
Stefano Stabellinic80a4202010-12-02 17:55:00 +00001121 offset = minor / nr_parts;
Stefan Bader89153b52011-07-14 15:30:37 +02001122 if (xen_hvm_domain() && offset < EMULATED_HD_DISK_NAME_OFFSET + 4)
Stefano Stabellinic80a4202010-12-02 17:55:00 +00001123 printk(KERN_WARNING "blkfront: vdevice 0x%x might conflict with "
1124 "emulated IDE disks,\n\t choose an xvd device name"
1125 "from xvde on\n", info->vdevice);
Chris Lalancette9246b5f2008-09-17 14:30:32 -07001126 }
Jan Beuliche77c78c2012-04-05 16:37:22 +01001127 if (minor >> MINORBITS) {
1128 pr_warn("blkfront: %#x's minor (%#x) out of range; ignoring\n",
1129 info->vdevice, minor);
1130 return -ENODEV;
1131 }
Chris Lalancette9246b5f2008-09-17 14:30:32 -07001132
1133 if ((minor % nr_parts) == 0)
1134 nr_minors = nr_parts;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001135
Jan Beulich0e345822010-08-07 18:28:55 +02001136 err = xlbd_reserve_minors(minor, nr_minors);
1137 if (err)
1138 goto out;
1139 err = -ENODEV;
1140
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001141 gd = alloc_disk(nr_minors);
1142 if (gd == NULL)
Jan Beulich0e345822010-08-07 18:28:55 +02001143 goto release;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001144
Jan Beuliche77c78c2012-04-05 16:37:22 +01001145 strcpy(gd->disk_name, DEV_NAME);
1146 ptr = encode_disk_name(gd->disk_name + sizeof(DEV_NAME) - 1, offset);
1147 BUG_ON(ptr >= gd->disk_name + DISK_NAME_LEN);
1148 if (nr_minors > 1)
1149 *ptr = 0;
1150 else
1151 snprintf(ptr, gd->disk_name + DISK_NAME_LEN - ptr,
1152 "%d", minor & (nr_parts - 1));
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001153
1154 gd->major = XENVBD_MAJOR;
1155 gd->first_minor = minor;
1156 gd->fops = &xlvbd_block_fops;
1157 gd->private_data = info;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001158 set_capacity(gd, capacity);
1159
Bob Liu172335a2016-07-01 17:43:39 -04001160 if (xlvbd_init_blk_queue(gd, sector_size, physical_sector_size)) {
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001161 del_gendisk(gd);
Jan Beulich0e345822010-08-07 18:28:55 +02001162 goto release;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001163 }
1164
Tejun Heo4913efe2010-09-03 11:56:16 +02001165 xlvbd_flush(info);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001166
1167 if (vdisk_info & VDISK_READONLY)
1168 set_disk_ro(gd, 1);
1169
1170 if (vdisk_info & VDISK_REMOVABLE)
1171 gd->flags |= GENHD_FL_REMOVABLE;
1172
1173 if (vdisk_info & VDISK_CDROM)
1174 gd->flags |= GENHD_FL_CD;
1175
1176 return 0;
1177
Jan Beulich0e345822010-08-07 18:28:55 +02001178 release:
1179 xlbd_release_minors(minor, nr_minors);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001180 out:
1181 return err;
1182}
1183
Daniel Stoddena66b5ae2010-08-07 18:33:17 +02001184static void xlvbd_release_gendisk(struct blkfront_info *info)
1185{
Bob Liu3df0e502015-11-14 11:12:12 +08001186 unsigned int minor, nr_minors, i;
Daniel Stoddena66b5ae2010-08-07 18:33:17 +02001187
1188 if (info->rq == NULL)
1189 return;
1190
Daniel Stoddena66b5ae2010-08-07 18:33:17 +02001191 /* No more blkif_request(). */
Bob Liu907c3eb2015-07-13 17:55:24 +08001192 blk_mq_stop_hw_queues(info->rq);
Daniel Stoddena66b5ae2010-08-07 18:33:17 +02001193
Bob Liu3df0e502015-11-14 11:12:12 +08001194 for (i = 0; i < info->nr_rings; i++) {
1195 struct blkfront_ring_info *rinfo = &info->rinfo[i];
Daniel Stoddena66b5ae2010-08-07 18:33:17 +02001196
Bob Liu3df0e502015-11-14 11:12:12 +08001197 /* No more gnttab callback work. */
1198 gnttab_cancel_free_callback(&rinfo->callback);
1199
1200 /* Flush gnttab callback work. Must be done with no locks held. */
1201 flush_work(&rinfo->work);
1202 }
Daniel Stoddena66b5ae2010-08-07 18:33:17 +02001203
1204 del_gendisk(info->gd);
1205
1206 minor = info->gd->first_minor;
1207 nr_minors = info->gd->minors;
1208 xlbd_release_minors(minor, nr_minors);
1209
1210 blk_cleanup_queue(info->rq);
Bob Liu907c3eb2015-07-13 17:55:24 +08001211 blk_mq_free_tag_set(&info->tag_set);
Daniel Stoddena66b5ae2010-08-07 18:33:17 +02001212 info->rq = NULL;
1213
1214 put_disk(info->gd);
1215 info->gd = NULL;
1216}
1217
Bob Liu11659562015-11-14 11:12:13 +08001218/* Already hold rinfo->ring_lock. */
1219static inline void kick_pending_request_queues_locked(struct blkfront_ring_info *rinfo)
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001220{
Bob Liu81f35162015-11-14 11:12:11 +08001221 if (!RING_FULL(&rinfo->ring))
1222 blk_mq_start_stopped_hw_queues(rinfo->dev_info->rq, true);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001223}
1224
Bob Liu11659562015-11-14 11:12:13 +08001225static void kick_pending_request_queues(struct blkfront_ring_info *rinfo)
1226{
1227 unsigned long flags;
1228
1229 spin_lock_irqsave(&rinfo->ring_lock, flags);
1230 kick_pending_request_queues_locked(rinfo);
1231 spin_unlock_irqrestore(&rinfo->ring_lock, flags);
1232}
1233
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001234static void blkif_restart_queue(struct work_struct *work)
1235{
Bob Liu81f35162015-11-14 11:12:11 +08001236 struct blkfront_ring_info *rinfo = container_of(work, struct blkfront_ring_info, work);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001237
Bob Liu81f35162015-11-14 11:12:11 +08001238 if (rinfo->dev_info->connected == BLKIF_STATE_CONNECTED)
1239 kick_pending_request_queues(rinfo);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001240}
1241
Bob Liu3df0e502015-11-14 11:12:12 +08001242static void blkif_free_ring(struct blkfront_ring_info *rinfo)
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001243{
Bob Liu73716df2015-11-16 16:51:39 -05001244 struct grant *persistent_gnt, *n;
Bob Liu3df0e502015-11-14 11:12:12 +08001245 struct blkfront_info *info = rinfo->dev_info;
Roger Pau Monne402b27f2013-04-18 16:06:54 +02001246 int i, j, segs;
Roger Pau Monne0a8704a2012-10-24 18:58:45 +02001247
Roger Pau Monnebfe11d62013-10-29 18:31:14 +01001248 /*
1249 * Remove indirect pages, this only happens when using indirect
1250 * descriptors but not persistent grants
1251 */
Bob Liu81f35162015-11-14 11:12:11 +08001252 if (!list_empty(&rinfo->indirect_pages)) {
Roger Pau Monnebfe11d62013-10-29 18:31:14 +01001253 struct page *indirect_page, *n;
1254
1255 BUG_ON(info->feature_persistent);
Bob Liu81f35162015-11-14 11:12:11 +08001256 list_for_each_entry_safe(indirect_page, n, &rinfo->indirect_pages, lru) {
Roger Pau Monnebfe11d62013-10-29 18:31:14 +01001257 list_del(&indirect_page->lru);
1258 __free_page(indirect_page);
1259 }
1260 }
1261
Bob Liu73716df2015-11-16 16:51:39 -05001262 /* Remove all persistent grants. */
1263 if (!list_empty(&rinfo->grants)) {
1264 list_for_each_entry_safe(persistent_gnt, n,
1265 &rinfo->grants, node) {
1266 list_del(&persistent_gnt->node);
1267 if (persistent_gnt->gref != GRANT_INVALID_REF) {
1268 gnttab_end_foreign_access(persistent_gnt->gref,
1269 0, 0UL);
1270 rinfo->persistent_gnts_c--;
1271 }
1272 if (info->feature_persistent)
1273 __free_page(persistent_gnt->page);
1274 kfree(persistent_gnt);
1275 }
1276 }
1277 BUG_ON(rinfo->persistent_gnts_c != 0);
1278
Bob Liu86839c52015-06-03 13:40:03 +08001279 for (i = 0; i < BLK_RING_SIZE(info); i++) {
Roger Pau Monne402b27f2013-04-18 16:06:54 +02001280 /*
1281 * Clear persistent grants present in requests already
1282 * on the shared ring
1283 */
Bob Liu81f35162015-11-14 11:12:11 +08001284 if (!rinfo->shadow[i].request)
Roger Pau Monne402b27f2013-04-18 16:06:54 +02001285 goto free_shadow;
1286
Bob Liu81f35162015-11-14 11:12:11 +08001287 segs = rinfo->shadow[i].req.operation == BLKIF_OP_INDIRECT ?
1288 rinfo->shadow[i].req.u.indirect.nr_segments :
1289 rinfo->shadow[i].req.u.rw.nr_segments;
Roger Pau Monne402b27f2013-04-18 16:06:54 +02001290 for (j = 0; j < segs; j++) {
Bob Liu81f35162015-11-14 11:12:11 +08001291 persistent_gnt = rinfo->shadow[i].grants_used[j];
Roger Pau Monne402b27f2013-04-18 16:06:54 +02001292 gnttab_end_foreign_access(persistent_gnt->gref, 0, 0UL);
Roger Pau Monnebfe11d62013-10-29 18:31:14 +01001293 if (info->feature_persistent)
Julien Gralla7a6df22015-06-30 11:58:51 +01001294 __free_page(persistent_gnt->page);
Roger Pau Monne402b27f2013-04-18 16:06:54 +02001295 kfree(persistent_gnt);
1296 }
1297
Bob Liu81f35162015-11-14 11:12:11 +08001298 if (rinfo->shadow[i].req.operation != BLKIF_OP_INDIRECT)
Roger Pau Monne402b27f2013-04-18 16:06:54 +02001299 /*
1300 * If this is not an indirect operation don't try to
1301 * free indirect segments
1302 */
1303 goto free_shadow;
1304
1305 for (j = 0; j < INDIRECT_GREFS(segs); j++) {
Bob Liu81f35162015-11-14 11:12:11 +08001306 persistent_gnt = rinfo->shadow[i].indirect_grants[j];
Roger Pau Monne402b27f2013-04-18 16:06:54 +02001307 gnttab_end_foreign_access(persistent_gnt->gref, 0, 0UL);
Julien Gralla7a6df22015-06-30 11:58:51 +01001308 __free_page(persistent_gnt->page);
Roger Pau Monne402b27f2013-04-18 16:06:54 +02001309 kfree(persistent_gnt);
1310 }
1311
1312free_shadow:
Roger Pau Monne1d5c76e2019-05-03 17:04:01 +02001313 kvfree(rinfo->shadow[i].grants_used);
Bob Liu81f35162015-11-14 11:12:11 +08001314 rinfo->shadow[i].grants_used = NULL;
Roger Pau Monne1d5c76e2019-05-03 17:04:01 +02001315 kvfree(rinfo->shadow[i].indirect_grants);
Bob Liu81f35162015-11-14 11:12:11 +08001316 rinfo->shadow[i].indirect_grants = NULL;
Roger Pau Monne1d5c76e2019-05-03 17:04:01 +02001317 kvfree(rinfo->shadow[i].sg);
Bob Liu81f35162015-11-14 11:12:11 +08001318 rinfo->shadow[i].sg = NULL;
Roger Pau Monne402b27f2013-04-18 16:06:54 +02001319 }
1320
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001321 /* No more gnttab callback work. */
Bob Liu81f35162015-11-14 11:12:11 +08001322 gnttab_cancel_free_callback(&rinfo->callback);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001323
1324 /* Flush gnttab callback work. Must be done with no locks held. */
Bob Liu81f35162015-11-14 11:12:11 +08001325 flush_work(&rinfo->work);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001326
1327 /* Free resources associated with old device channel. */
Bob Liu86839c52015-06-03 13:40:03 +08001328 for (i = 0; i < info->nr_ring_pages; i++) {
Bob Liu81f35162015-11-14 11:12:11 +08001329 if (rinfo->ring_ref[i] != GRANT_INVALID_REF) {
1330 gnttab_end_foreign_access(rinfo->ring_ref[i], 0, 0);
1331 rinfo->ring_ref[i] = GRANT_INVALID_REF;
Bob Liu86839c52015-06-03 13:40:03 +08001332 }
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001333 }
Bob Liu6c647b02016-07-01 15:45:57 -04001334 free_pages((unsigned long)rinfo->ring.sring, get_order(info->nr_ring_pages * XEN_PAGE_SIZE));
Bob Liu81f35162015-11-14 11:12:11 +08001335 rinfo->ring.sring = NULL;
Bob Liu86839c52015-06-03 13:40:03 +08001336
Bob Liu81f35162015-11-14 11:12:11 +08001337 if (rinfo->irq)
1338 unbind_from_irqhandler(rinfo->irq, rinfo);
1339 rinfo->evtchn = rinfo->irq = 0;
Bob Liu3df0e502015-11-14 11:12:12 +08001340}
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001341
Bob Liu3df0e502015-11-14 11:12:12 +08001342static void blkif_free(struct blkfront_info *info, int suspend)
1343{
Bob Liu3df0e502015-11-14 11:12:12 +08001344 unsigned int i;
1345
1346 /* Prevent new requests being issued until we fix things up. */
Bob Liu3df0e502015-11-14 11:12:12 +08001347 info->connected = suspend ?
1348 BLKIF_STATE_SUSPENDED : BLKIF_STATE_DISCONNECTED;
1349 /* No more blkif_request(). */
1350 if (info->rq)
1351 blk_mq_stop_hw_queues(info->rq);
1352
Bob Liu3df0e502015-11-14 11:12:12 +08001353 for (i = 0; i < info->nr_rings; i++)
1354 blkif_free_ring(&info->rinfo[i]);
1355
Roger Pau Monne1d5c76e2019-05-03 17:04:01 +02001356 kvfree(info->rinfo);
Bob Liu3df0e502015-11-14 11:12:12 +08001357 info->rinfo = NULL;
1358 info->nr_rings = 0;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001359}
1360
Julien Grallc004a6f2015-07-22 16:44:54 +01001361struct copy_from_grant {
1362 const struct blk_shadow *s;
1363 unsigned int grant_idx;
1364 unsigned int bvec_offset;
1365 char *bvec_data;
1366};
1367
1368static void blkif_copy_from_grant(unsigned long gfn, unsigned int offset,
1369 unsigned int len, void *data)
1370{
1371 struct copy_from_grant *info = data;
1372 char *shared_data;
1373 /* Convenient aliases */
1374 const struct blk_shadow *s = info->s;
1375
1376 shared_data = kmap_atomic(s->grants_used[info->grant_idx]->page);
1377
1378 memcpy(info->bvec_data + info->bvec_offset,
1379 shared_data + offset, len);
1380
1381 info->bvec_offset += len;
1382 info->grant_idx++;
1383
1384 kunmap_atomic(shared_data);
1385}
1386
Julien Grall6cc568332015-08-13 13:13:35 +01001387static enum blk_req_status blkif_rsp_to_req_status(int rsp)
1388{
1389 switch (rsp)
1390 {
1391 case BLKIF_RSP_OKAY:
1392 return REQ_DONE;
1393 case BLKIF_RSP_EOPNOTSUPP:
1394 return REQ_EOPNOTSUPP;
1395 case BLKIF_RSP_ERROR:
1396 /* Fallthrough. */
1397 default:
1398 return REQ_ERROR;
1399 }
1400}
1401
1402/*
1403 * Get the final status of the block request based on two ring response
1404 */
1405static int blkif_get_final_status(enum blk_req_status s1,
1406 enum blk_req_status s2)
1407{
1408 BUG_ON(s1 == REQ_WAITING);
1409 BUG_ON(s2 == REQ_WAITING);
1410
1411 if (s1 == REQ_ERROR || s2 == REQ_ERROR)
1412 return BLKIF_RSP_ERROR;
1413 else if (s1 == REQ_EOPNOTSUPP || s2 == REQ_EOPNOTSUPP)
1414 return BLKIF_RSP_EOPNOTSUPP;
1415 return BLKIF_RSP_OKAY;
1416}
1417
1418static bool blkif_completion(unsigned long *id,
1419 struct blkfront_ring_info *rinfo,
Roger Pau Monne0a8704a2012-10-24 18:58:45 +02001420 struct blkif_response *bret)
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001421{
Roger Pau Monned62f6912012-12-07 19:00:31 +01001422 int i = 0;
Roger Pau Monneb7649152013-05-02 10:58:50 +02001423 struct scatterlist *sg;
Julien Grallc004a6f2015-07-22 16:44:54 +01001424 int num_sg, num_grant;
Bob Liu81f35162015-11-14 11:12:11 +08001425 struct blkfront_info *info = rinfo->dev_info;
Julien Grall6cc568332015-08-13 13:13:35 +01001426 struct blk_shadow *s = &rinfo->shadow[*id];
Julien Grallc004a6f2015-07-22 16:44:54 +01001427 struct copy_from_grant data = {
Julien Grallc004a6f2015-07-22 16:44:54 +01001428 .grant_idx = 0,
1429 };
Roger Pau Monne402b27f2013-04-18 16:06:54 +02001430
Julien Grallc004a6f2015-07-22 16:44:54 +01001431 num_grant = s->req.operation == BLKIF_OP_INDIRECT ?
Roger Pau Monne402b27f2013-04-18 16:06:54 +02001432 s->req.u.indirect.nr_segments : s->req.u.rw.nr_segments;
Julien Grall6cc568332015-08-13 13:13:35 +01001433
1434 /* The I/O request may be split in two. */
1435 if (unlikely(s->associated_id != NO_ASSOCIATED_ID)) {
1436 struct blk_shadow *s2 = &rinfo->shadow[s->associated_id];
1437
1438 /* Keep the status of the current response in shadow. */
1439 s->status = blkif_rsp_to_req_status(bret->status);
1440
1441 /* Wait the second response if not yet here. */
1442 if (s2->status == REQ_WAITING)
Gustavo A. R. Silvaf87c30c2018-08-06 08:14:55 -06001443 return false;
Julien Grall6cc568332015-08-13 13:13:35 +01001444
1445 bret->status = blkif_get_final_status(s->status,
1446 s2->status);
1447
1448 /*
1449 * All the grants is stored in the first shadow in order
1450 * to make the completion code simpler.
1451 */
1452 num_grant += s2->req.u.rw.nr_segments;
1453
1454 /*
1455 * The two responses may not come in order. Only the
1456 * first request will store the scatter-gather list.
1457 */
1458 if (s2->num_sg != 0) {
1459 /* Update "id" with the ID of the first response. */
1460 *id = s->associated_id;
1461 s = s2;
1462 }
1463
1464 /*
1465 * We don't need anymore the second request, so recycling
1466 * it now.
1467 */
1468 if (add_id_to_freelist(rinfo, s->associated_id))
1469 WARN(1, "%s: can't recycle the second part (id = %ld) of the request\n",
1470 info->gd->disk_name, s->associated_id);
1471 }
1472
1473 data.s = s;
Julien Grallc004a6f2015-07-22 16:44:54 +01001474 num_sg = s->num_sg;
Roger Pau Monne0a8704a2012-10-24 18:58:45 +02001475
Roger Pau Monnebfe11d62013-10-29 18:31:14 +01001476 if (bret->operation == BLKIF_OP_READ && info->feature_persistent) {
Julien Grallc004a6f2015-07-22 16:44:54 +01001477 for_each_sg(s->sg, sg, num_sg, i) {
Roger Pau Monneb7649152013-05-02 10:58:50 +02001478 BUG_ON(sg->offset + sg->length > PAGE_SIZE);
Julien Grallc004a6f2015-07-22 16:44:54 +01001479
1480 data.bvec_offset = sg->offset;
1481 data.bvec_data = kmap_atomic(sg_page(sg));
1482
1483 gnttab_foreach_grant_in_range(sg_page(sg),
1484 sg->offset,
1485 sg->length,
1486 blkif_copy_from_grant,
1487 &data);
1488
1489 kunmap_atomic(data.bvec_data);
Roger Pau Monne0a8704a2012-10-24 18:58:45 +02001490 }
1491 }
1492 /* Add the persistent grant into the list of free grants */
Julien Grallc004a6f2015-07-22 16:44:54 +01001493 for (i = 0; i < num_grant; i++) {
Roger Pau Monnefbe363c2013-08-12 12:53:44 +02001494 if (gnttab_query_foreign_access(s->grants_used[i]->gref)) {
1495 /*
1496 * If the grant is still mapped by the backend (the
1497 * backend has chosen to make this grant persistent)
1498 * we add it at the head of the list, so it will be
1499 * reused first.
1500 */
Roger Pau Monnebfe11d62013-10-29 18:31:14 +01001501 if (!info->feature_persistent)
1502 pr_alert_ratelimited("backed has not unmapped grant: %u\n",
1503 s->grants_used[i]->gref);
Bob Liu73716df2015-11-16 16:51:39 -05001504 list_add(&s->grants_used[i]->node, &rinfo->grants);
1505 rinfo->persistent_gnts_c++;
Roger Pau Monnefbe363c2013-08-12 12:53:44 +02001506 } else {
1507 /*
1508 * If the grant is not mapped by the backend we end the
1509 * foreign access and add it to the tail of the list,
1510 * so it will not be picked again unless we run out of
1511 * persistent grants.
1512 */
1513 gnttab_end_foreign_access(s->grants_used[i]->gref, 0, 0UL);
1514 s->grants_used[i]->gref = GRANT_INVALID_REF;
Bob Liu73716df2015-11-16 16:51:39 -05001515 list_add_tail(&s->grants_used[i]->node, &rinfo->grants);
Roger Pau Monnefbe363c2013-08-12 12:53:44 +02001516 }
Roger Pau Monne0a8704a2012-10-24 18:58:45 +02001517 }
Roger Pau Monne402b27f2013-04-18 16:06:54 +02001518 if (s->req.operation == BLKIF_OP_INDIRECT) {
Julien Grallc004a6f2015-07-22 16:44:54 +01001519 for (i = 0; i < INDIRECT_GREFS(num_grant); i++) {
Roger Pau Monnefbe363c2013-08-12 12:53:44 +02001520 if (gnttab_query_foreign_access(s->indirect_grants[i]->gref)) {
Roger Pau Monnebfe11d62013-10-29 18:31:14 +01001521 if (!info->feature_persistent)
1522 pr_alert_ratelimited("backed has not unmapped grant: %u\n",
1523 s->indirect_grants[i]->gref);
Bob Liu73716df2015-11-16 16:51:39 -05001524 list_add(&s->indirect_grants[i]->node, &rinfo->grants);
1525 rinfo->persistent_gnts_c++;
Roger Pau Monnefbe363c2013-08-12 12:53:44 +02001526 } else {
Roger Pau Monnebfe11d62013-10-29 18:31:14 +01001527 struct page *indirect_page;
1528
Roger Pau Monnefbe363c2013-08-12 12:53:44 +02001529 gnttab_end_foreign_access(s->indirect_grants[i]->gref, 0, 0UL);
Roger Pau Monnebfe11d62013-10-29 18:31:14 +01001530 /*
1531 * Add the used indirect page back to the list of
1532 * available pages for indirect grefs.
1533 */
Bob Liu7b076752015-07-22 14:40:09 +08001534 if (!info->feature_persistent) {
Julien Gralla7a6df22015-06-30 11:58:51 +01001535 indirect_page = s->indirect_grants[i]->page;
Bob Liu81f35162015-11-14 11:12:11 +08001536 list_add(&indirect_page->lru, &rinfo->indirect_pages);
Bob Liu7b076752015-07-22 14:40:09 +08001537 }
Roger Pau Monnefbe363c2013-08-12 12:53:44 +02001538 s->indirect_grants[i]->gref = GRANT_INVALID_REF;
Bob Liu73716df2015-11-16 16:51:39 -05001539 list_add_tail(&s->indirect_grants[i]->node, &rinfo->grants);
Roger Pau Monnefbe363c2013-08-12 12:53:44 +02001540 }
Roger Pau Monne402b27f2013-04-18 16:06:54 +02001541 }
1542 }
Julien Grall6cc568332015-08-13 13:13:35 +01001543
Gustavo A. R. Silvaf87c30c2018-08-06 08:14:55 -06001544 return true;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001545}
1546
1547static irqreturn_t blkif_interrupt(int irq, void *dev_id)
1548{
1549 struct request *req;
1550 struct blkif_response *bret;
1551 RING_IDX i, rp;
1552 unsigned long flags;
Bob Liu81f35162015-11-14 11:12:11 +08001553 struct blkfront_ring_info *rinfo = (struct blkfront_ring_info *)dev_id;
1554 struct blkfront_info *info = rinfo->dev_info;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001555
Bob Liu11659562015-11-14 11:12:13 +08001556 if (unlikely(info->connected != BLKIF_STATE_CONNECTED))
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001557 return IRQ_HANDLED;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001558
Bob Liu11659562015-11-14 11:12:13 +08001559 spin_lock_irqsave(&rinfo->ring_lock, flags);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001560 again:
Bob Liu81f35162015-11-14 11:12:11 +08001561 rp = rinfo->ring.sring->rsp_prod;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001562 rmb(); /* Ensure we see queued responses up to 'rp'. */
1563
Bob Liu81f35162015-11-14 11:12:11 +08001564 for (i = rinfo->ring.rsp_cons; i != rp; i++) {
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001565 unsigned long id;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001566
Bob Liu81f35162015-11-14 11:12:11 +08001567 bret = RING_GET_RESPONSE(&rinfo->ring, i);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001568 id = bret->id;
Konrad Rzeszutek Wilk6878c322012-05-25 17:34:51 -04001569 /*
1570 * The backend has messed up and given us an id that we would
1571 * never have given to it (we stamp it up to BLK_RING_SIZE -
1572 * look in get_id_from_freelist.
1573 */
Bob Liu86839c52015-06-03 13:40:03 +08001574 if (id >= BLK_RING_SIZE(info)) {
Konrad Rzeszutek Wilk6878c322012-05-25 17:34:51 -04001575 WARN(1, "%s: response to %s has incorrect id (%ld)\n",
1576 info->gd->disk_name, op_name(bret->operation), id);
1577 /* We can't safely get the 'struct request' as
1578 * the id is busted. */
1579 continue;
1580 }
Bob Liu81f35162015-11-14 11:12:11 +08001581 req = rinfo->shadow[id].request;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001582
Julien Grall6cc568332015-08-13 13:13:35 +01001583 if (bret->operation != BLKIF_OP_DISCARD) {
1584 /*
1585 * We may need to wait for an extra response if the
1586 * I/O request is split in 2
1587 */
1588 if (!blkif_completion(&id, rinfo, bret))
1589 continue;
1590 }
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001591
Bob Liu81f35162015-11-14 11:12:11 +08001592 if (add_id_to_freelist(rinfo, id)) {
Konrad Rzeszutek Wilk6878c322012-05-25 17:34:51 -04001593 WARN(1, "%s: response to %s (id %ld) couldn't be recycled!\n",
1594 info->gd->disk_name, op_name(bret->operation), id);
1595 continue;
1596 }
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001597
Christoph Hellwig2a842ac2017-06-03 09:38:04 +02001598 if (bret->status == BLKIF_RSP_OKAY)
1599 blkif_req(req)->error = BLK_STS_OK;
1600 else
1601 blkif_req(req)->error = BLK_STS_IOERR;
1602
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001603 switch (bret->operation) {
Li Dongyanged30bf32011-09-01 18:39:09 +08001604 case BLKIF_OP_DISCARD:
1605 if (unlikely(bret->status == BLKIF_RSP_EOPNOTSUPP)) {
1606 struct request_queue *rq = info->rq;
Konrad Rzeszutek Wilk6878c322012-05-25 17:34:51 -04001607 printk(KERN_WARNING "blkfront: %s: %s op failed\n",
1608 info->gd->disk_name, op_name(bret->operation));
Christoph Hellwig2a842ac2017-06-03 09:38:04 +02001609 blkif_req(req)->error = BLK_STS_NOTSUPP;
Li Dongyanged30bf32011-09-01 18:39:09 +08001610 info->feature_discard = 0;
Konrad Rzeszutek Wilk5ea42982011-10-12 16:23:30 -04001611 info->feature_secdiscard = 0;
Bart Van Assche8b904b52018-03-07 17:10:10 -08001612 blk_queue_flag_clear(QUEUE_FLAG_DISCARD, rq);
1613 blk_queue_flag_clear(QUEUE_FLAG_SECERASE, rq);
Li Dongyanged30bf32011-09-01 18:39:09 +08001614 }
Li Dongyanged30bf32011-09-01 18:39:09 +08001615 break;
Konrad Rzeszutek Wilkedf6ef52011-05-03 12:01:11 -04001616 case BLKIF_OP_FLUSH_DISKCACHE:
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001617 case BLKIF_OP_WRITE_BARRIER:
1618 if (unlikely(bret->status == BLKIF_RSP_EOPNOTSUPP)) {
Konrad Rzeszutek Wilk6878c322012-05-25 17:34:51 -04001619 printk(KERN_WARNING "blkfront: %s: %s op failed\n",
1620 info->gd->disk_name, op_name(bret->operation));
Bart Van Assche31c4ccc2017-07-21 19:11:10 +02001621 blkif_req(req)->error = BLK_STS_NOTSUPP;
Jeremy Fitzhardingedcb8bae2010-11-02 11:55:58 -04001622 }
1623 if (unlikely(bret->status == BLKIF_RSP_ERROR &&
Bob Liu81f35162015-11-14 11:12:11 +08001624 rinfo->shadow[id].req.u.rw.nr_segments == 0)) {
Konrad Rzeszutek Wilk6878c322012-05-25 17:34:51 -04001625 printk(KERN_WARNING "blkfront: %s: empty %s op failed\n",
1626 info->gd->disk_name, op_name(bret->operation));
Christoph Hellwig2a842ac2017-06-03 09:38:04 +02001627 blkif_req(req)->error = BLK_STS_NOTSUPP;
Jeremy Fitzhardingedcb8bae2010-11-02 11:55:58 -04001628 }
Christoph Hellwig26095872017-04-20 16:03:08 +02001629 if (unlikely(blkif_req(req)->error)) {
Christoph Hellwig2a842ac2017-06-03 09:38:04 +02001630 if (blkif_req(req)->error == BLK_STS_NOTSUPP)
1631 blkif_req(req)->error = BLK_STS_OK;
Mike Christiea4180902016-06-05 14:32:24 -05001632 info->feature_fua = 0;
Tejun Heo4913efe2010-09-03 11:56:16 +02001633 info->feature_flush = 0;
1634 xlvbd_flush(info);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001635 }
1636 /* fall through */
1637 case BLKIF_OP_READ:
1638 case BLKIF_OP_WRITE:
1639 if (unlikely(bret->status != BLKIF_RSP_OKAY))
1640 dev_dbg(&info->xbdev->dev, "Bad return from blkdev data "
1641 "request: %x\n", bret->status);
1642
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001643 break;
1644 default:
1645 BUG();
1646 }
Christoph Hellwig26095872017-04-20 16:03:08 +02001647
Christoph Hellwig08e00292017-04-20 16:03:09 +02001648 blk_mq_complete_request(req);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001649 }
1650
Bob Liu81f35162015-11-14 11:12:11 +08001651 rinfo->ring.rsp_cons = i;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001652
Bob Liu81f35162015-11-14 11:12:11 +08001653 if (i != rinfo->ring.req_prod_pvt) {
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001654 int more_to_do;
Bob Liu81f35162015-11-14 11:12:11 +08001655 RING_FINAL_CHECK_FOR_RESPONSES(&rinfo->ring, more_to_do);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001656 if (more_to_do)
1657 goto again;
1658 } else
Bob Liu81f35162015-11-14 11:12:11 +08001659 rinfo->ring.sring->rsp_event = i + 1;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001660
Bob Liu11659562015-11-14 11:12:13 +08001661 kick_pending_request_queues_locked(rinfo);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001662
Bob Liu11659562015-11-14 11:12:13 +08001663 spin_unlock_irqrestore(&rinfo->ring_lock, flags);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001664
1665 return IRQ_HANDLED;
1666}
1667
1668
1669static int setup_blkring(struct xenbus_device *dev,
Bob Liu81f35162015-11-14 11:12:11 +08001670 struct blkfront_ring_info *rinfo)
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001671{
1672 struct blkif_sring *sring;
Bob Liu86839c52015-06-03 13:40:03 +08001673 int err, i;
Bob Liu81f35162015-11-14 11:12:11 +08001674 struct blkfront_info *info = rinfo->dev_info;
Julien Grallc004a6f2015-07-22 16:44:54 +01001675 unsigned long ring_size = info->nr_ring_pages * XEN_PAGE_SIZE;
Julien Grall9cce2912015-10-13 17:50:11 +01001676 grant_ref_t gref[XENBUS_MAX_RING_GRANTS];
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001677
Bob Liu86839c52015-06-03 13:40:03 +08001678 for (i = 0; i < info->nr_ring_pages; i++)
Bob Liu81f35162015-11-14 11:12:11 +08001679 rinfo->ring_ref[i] = GRANT_INVALID_REF;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001680
Bob Liu86839c52015-06-03 13:40:03 +08001681 sring = (struct blkif_sring *)__get_free_pages(GFP_NOIO | __GFP_HIGH,
1682 get_order(ring_size));
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001683 if (!sring) {
1684 xenbus_dev_fatal(dev, -ENOMEM, "allocating shared ring");
1685 return -ENOMEM;
1686 }
1687 SHARED_RING_INIT(sring);
Bob Liu81f35162015-11-14 11:12:11 +08001688 FRONT_RING_INIT(&rinfo->ring, sring, ring_size);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001689
Bob Liu81f35162015-11-14 11:12:11 +08001690 err = xenbus_grant_ring(dev, rinfo->ring.sring, info->nr_ring_pages, gref);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001691 if (err < 0) {
Bob Liu86839c52015-06-03 13:40:03 +08001692 free_pages((unsigned long)sring, get_order(ring_size));
Bob Liu81f35162015-11-14 11:12:11 +08001693 rinfo->ring.sring = NULL;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001694 goto fail;
1695 }
Bob Liu86839c52015-06-03 13:40:03 +08001696 for (i = 0; i < info->nr_ring_pages; i++)
Bob Liu81f35162015-11-14 11:12:11 +08001697 rinfo->ring_ref[i] = gref[i];
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001698
Bob Liu81f35162015-11-14 11:12:11 +08001699 err = xenbus_alloc_evtchn(dev, &rinfo->evtchn);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001700 if (err)
1701 goto fail;
1702
Bob Liu81f35162015-11-14 11:12:11 +08001703 err = bind_evtchn_to_irqhandler(rinfo->evtchn, blkif_interrupt, 0,
1704 "blkif", rinfo);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001705 if (err <= 0) {
1706 xenbus_dev_fatal(dev, err,
1707 "bind_evtchn_to_irqhandler failed");
1708 goto fail;
1709 }
Bob Liu81f35162015-11-14 11:12:11 +08001710 rinfo->irq = err;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001711
1712 return 0;
1713fail:
1714 blkif_free(info, 0);
1715 return err;
1716}
1717
Bob Liu28d949b2015-11-14 11:12:14 +08001718/*
1719 * Write out per-ring/queue nodes including ring-ref and event-channel, and each
1720 * ring buffer may have multi pages depending on ->nr_ring_pages.
1721 */
1722static int write_per_ring_nodes(struct xenbus_transaction xbt,
1723 struct blkfront_ring_info *rinfo, const char *dir)
1724{
1725 int err;
1726 unsigned int i;
1727 const char *message = NULL;
1728 struct blkfront_info *info = rinfo->dev_info;
1729
1730 if (info->nr_ring_pages == 1) {
1731 err = xenbus_printf(xbt, dir, "ring-ref", "%u", rinfo->ring_ref[0]);
1732 if (err) {
1733 message = "writing ring-ref";
1734 goto abort_transaction;
1735 }
1736 } else {
1737 for (i = 0; i < info->nr_ring_pages; i++) {
1738 char ring_ref_name[RINGREF_NAME_LEN];
1739
1740 snprintf(ring_ref_name, RINGREF_NAME_LEN, "ring-ref%u", i);
1741 err = xenbus_printf(xbt, dir, ring_ref_name,
1742 "%u", rinfo->ring_ref[i]);
1743 if (err) {
1744 message = "writing ring-ref";
1745 goto abort_transaction;
1746 }
1747 }
1748 }
1749
1750 err = xenbus_printf(xbt, dir, "event-channel", "%u", rinfo->evtchn);
1751 if (err) {
1752 message = "writing event-channel";
1753 goto abort_transaction;
1754 }
1755
1756 return 0;
1757
1758abort_transaction:
1759 xenbus_transaction_end(xbt, 1);
1760 if (message)
1761 xenbus_dev_fatal(info->xbdev, err, "%s", message);
1762
1763 return err;
1764}
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001765
Juergen Grossa46b5362018-08-13 16:01:11 +02001766static void free_info(struct blkfront_info *info)
1767{
1768 list_del(&info->info_list);
1769 kfree(info);
1770}
1771
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001772/* Common code used when first setting up, and when resuming. */
Ian Campbell203fd612009-12-04 15:33:54 +00001773static int talk_to_blkback(struct xenbus_device *dev,
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001774 struct blkfront_info *info)
1775{
1776 const char *message = NULL;
1777 struct xenbus_transaction xbt;
Bob Liu28d949b2015-11-14 11:12:14 +08001778 int err;
Juergen Grossf27dc1a2016-10-31 14:58:40 +01001779 unsigned int i, max_page_order;
1780 unsigned int ring_page_order;
Bob Liu86839c52015-06-03 13:40:03 +08001781
Bhavesh Davda7ed8ce12017-12-22 14:17:13 -08001782 if (!info)
1783 return -ENODEV;
1784
Juergen Grossf27dc1a2016-10-31 14:58:40 +01001785 max_page_order = xenbus_read_unsigned(info->xbdev->otherend,
1786 "max-ring-page-order", 0);
1787 ring_page_order = min(xen_blkif_max_ring_order, max_page_order);
1788 info->nr_ring_pages = 1 << ring_page_order;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001789
Bhavesh Davda7ed8ce12017-12-22 14:17:13 -08001790 err = negotiate_mq(info);
1791 if (err)
1792 goto destroy_blkring;
1793
Bob Liu3df0e502015-11-14 11:12:12 +08001794 for (i = 0; i < info->nr_rings; i++) {
Bob Liu28d949b2015-11-14 11:12:14 +08001795 struct blkfront_ring_info *rinfo = &info->rinfo[i];
1796
Bob Liu3df0e502015-11-14 11:12:12 +08001797 /* Create shared ring, alloc event channel. */
1798 err = setup_blkring(dev, rinfo);
1799 if (err)
1800 goto destroy_blkring;
1801 }
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001802
1803again:
1804 err = xenbus_transaction_start(&xbt);
1805 if (err) {
1806 xenbus_dev_fatal(dev, err, "starting transaction");
1807 goto destroy_blkring;
1808 }
1809
Bob Liu28d949b2015-11-14 11:12:14 +08001810 if (info->nr_ring_pages > 1) {
1811 err = xenbus_printf(xbt, dev->nodename, "ring-page-order", "%u",
1812 ring_page_order);
Bob Liu3df0e502015-11-14 11:12:12 +08001813 if (err) {
Bob Liu28d949b2015-11-14 11:12:14 +08001814 message = "writing ring-page-order";
Bob Liu3df0e502015-11-14 11:12:12 +08001815 goto abort_transaction;
1816 }
Bob Liu28d949b2015-11-14 11:12:14 +08001817 }
1818
1819 /* We already got the number of queues/rings in _probe */
1820 if (info->nr_rings == 1) {
1821 err = write_per_ring_nodes(xbt, &info->rinfo[0], dev->nodename);
1822 if (err)
1823 goto destroy_blkring;
Bob Liu3df0e502015-11-14 11:12:12 +08001824 } else {
Bob Liu28d949b2015-11-14 11:12:14 +08001825 char *path;
1826 size_t pathsize;
1827
1828 err = xenbus_printf(xbt, dev->nodename, "multi-queue-num-queues", "%u",
1829 info->nr_rings);
1830 if (err) {
1831 message = "writing multi-queue-num-queues";
1832 goto abort_transaction;
1833 }
1834
1835 pathsize = strlen(dev->nodename) + QUEUE_NAME_LEN;
1836 path = kmalloc(pathsize, GFP_KERNEL);
1837 if (!path) {
1838 err = -ENOMEM;
1839 message = "ENOMEM while writing ring references";
1840 goto abort_transaction;
1841 }
1842
1843 for (i = 0; i < info->nr_rings; i++) {
1844 memset(path, 0, pathsize);
1845 snprintf(path, pathsize, "%s/queue-%u", dev->nodename, i);
1846 err = write_per_ring_nodes(xbt, &info->rinfo[i], path);
1847 if (err) {
1848 kfree(path);
1849 goto destroy_blkring;
1850 }
1851 }
1852 kfree(path);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001853 }
Markus Armbruster3e334232008-04-02 10:54:02 -07001854 err = xenbus_printf(xbt, dev->nodename, "protocol", "%s",
1855 XEN_IO_PROTO_ABI_NATIVE);
1856 if (err) {
1857 message = "writing protocol";
1858 goto abort_transaction;
1859 }
Roger Pau Monne0a8704a2012-10-24 18:58:45 +02001860 err = xenbus_printf(xbt, dev->nodename,
Roger Pau Monnecb5bd4d2012-11-02 16:43:04 +01001861 "feature-persistent", "%u", 1);
Roger Pau Monne0a8704a2012-10-24 18:58:45 +02001862 if (err)
1863 dev_warn(&dev->dev,
1864 "writing persistent grants feature to xenbus");
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001865
1866 err = xenbus_transaction_end(xbt, 0);
1867 if (err) {
1868 if (err == -EAGAIN)
1869 goto again;
1870 xenbus_dev_fatal(dev, err, "completing transaction");
1871 goto destroy_blkring;
1872 }
1873
Bob Liu3df0e502015-11-14 11:12:12 +08001874 for (i = 0; i < info->nr_rings; i++) {
1875 unsigned int j;
Bob Liu28d949b2015-11-14 11:12:14 +08001876 struct blkfront_ring_info *rinfo = &info->rinfo[i];
Bob Liu3df0e502015-11-14 11:12:12 +08001877
1878 for (j = 0; j < BLK_RING_SIZE(info); j++)
1879 rinfo->shadow[j].req.u.rw.id = j + 1;
1880 rinfo->shadow[BLK_RING_SIZE(info)-1].req.u.rw.id = 0x0fffffff;
1881 }
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001882 xenbus_switch_state(dev, XenbusStateInitialised);
1883
1884 return 0;
1885
1886 abort_transaction:
1887 xenbus_transaction_end(xbt, 1);
1888 if (message)
1889 xenbus_dev_fatal(dev, err, "%s", message);
1890 destroy_blkring:
1891 blkif_free(info, 0);
Bob Liu3df0e502015-11-14 11:12:12 +08001892
Juergen Grossa46b5362018-08-13 16:01:11 +02001893 mutex_lock(&blkfront_mutex);
1894 free_info(info);
1895 mutex_unlock(&blkfront_mutex);
1896
Konrad Rzeszutek Wilkc31ecf62015-12-18 16:28:53 -05001897 dev_set_drvdata(&dev->dev, NULL);
1898
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001899 return err;
1900}
1901
Bob Liu3db70a82015-11-25 17:52:55 -05001902static int negotiate_mq(struct blkfront_info *info)
1903{
Juergen Grossf27dc1a2016-10-31 14:58:40 +01001904 unsigned int backend_max_queues;
Bob Liu3db70a82015-11-25 17:52:55 -05001905 unsigned int i;
1906
1907 BUG_ON(info->nr_rings);
1908
1909 /* Check if backend supports multiple queues. */
Juergen Grossf27dc1a2016-10-31 14:58:40 +01001910 backend_max_queues = xenbus_read_unsigned(info->xbdev->otherend,
1911 "multi-queue-max-queues", 1);
Bob Liu3db70a82015-11-25 17:52:55 -05001912 info->nr_rings = min(backend_max_queues, xen_blkif_max_queues);
1913 /* We need at least one ring. */
1914 if (!info->nr_rings)
1915 info->nr_rings = 1;
1916
Roger Pau Monne1d5c76e2019-05-03 17:04:01 +02001917 info->rinfo = kvcalloc(info->nr_rings,
1918 sizeof(struct blkfront_ring_info),
1919 GFP_KERNEL);
Bob Liu3db70a82015-11-25 17:52:55 -05001920 if (!info->rinfo) {
1921 xenbus_dev_fatal(info->xbdev, -ENOMEM, "allocating ring_info structure");
Manjunath Patil6cc4a082018-10-30 09:49:21 -07001922 info->nr_rings = 0;
Bob Liu3db70a82015-11-25 17:52:55 -05001923 return -ENOMEM;
1924 }
1925
1926 for (i = 0; i < info->nr_rings; i++) {
1927 struct blkfront_ring_info *rinfo;
1928
1929 rinfo = &info->rinfo[i];
1930 INIT_LIST_HEAD(&rinfo->indirect_pages);
1931 INIT_LIST_HEAD(&rinfo->grants);
1932 rinfo->dev_info = info;
1933 INIT_WORK(&rinfo->work, blkif_restart_queue);
1934 spin_lock_init(&rinfo->ring_lock);
1935 }
1936 return 0;
1937}
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001938/**
1939 * Entry point to this code when a new device is created. Allocate the basic
1940 * structures and the ring buffer for communication with the backend, and
1941 * inform the backend of the appropriate details for those. Switch to
1942 * Initialised state.
1943 */
1944static int blkfront_probe(struct xenbus_device *dev,
1945 const struct xenbus_device_id *id)
1946{
Bob Liu86839c52015-06-03 13:40:03 +08001947 int err, vdevice;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001948 struct blkfront_info *info;
1949
1950 /* FIXME: Use dynamic device id if this is not set. */
1951 err = xenbus_scanf(XBT_NIL, dev->nodename,
1952 "virtual-device", "%i", &vdevice);
1953 if (err != 1) {
Chris Lalancette9246b5f2008-09-17 14:30:32 -07001954 /* go looking in the extended area instead */
1955 err = xenbus_scanf(XBT_NIL, dev->nodename, "virtual-device-ext",
1956 "%i", &vdevice);
1957 if (err != 1) {
1958 xenbus_dev_fatal(dev, err, "reading virtual-device");
1959 return err;
1960 }
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001961 }
1962
Stefano Stabellinib98a4092010-07-29 14:53:16 +01001963 if (xen_hvm_domain()) {
1964 char *type;
1965 int len;
1966 /* no unplug has been done: do not hook devices != xen vbds */
Konrad Rzeszutek Wilk51c71a32013-11-26 15:05:40 -05001967 if (xen_has_pv_and_legacy_disk_devices()) {
Stefano Stabellinib98a4092010-07-29 14:53:16 +01001968 int major;
Stefano Stabellinic1c54132010-05-14 12:44:30 +01001969
Stefano Stabellinib98a4092010-07-29 14:53:16 +01001970 if (!VDEV_IS_EXTENDED(vdevice))
1971 major = BLKIF_MAJOR(vdevice);
1972 else
1973 major = XENVBD_MAJOR;
Stefano Stabellinic1c54132010-05-14 12:44:30 +01001974
Stefano Stabellinib98a4092010-07-29 14:53:16 +01001975 if (major != XENVBD_MAJOR) {
1976 printk(KERN_INFO
1977 "%s: HVM does not support vbd %d as xen block device\n",
Rasmus Villemoes02f1f212015-02-12 15:01:31 -08001978 __func__, vdevice);
Stefano Stabellinib98a4092010-07-29 14:53:16 +01001979 return -ENODEV;
1980 }
1981 }
1982 /* do not create a PV cdrom device if we are an HVM guest */
1983 type = xenbus_read(XBT_NIL, dev->nodename, "device-type", &len);
1984 if (IS_ERR(type))
1985 return -ENODEV;
1986 if (strncmp(type, "cdrom", 5) == 0) {
1987 kfree(type);
Stefano Stabellinic1c54132010-05-14 12:44:30 +01001988 return -ENODEV;
1989 }
Stefano Stabellinib98a4092010-07-29 14:53:16 +01001990 kfree(type);
Stefano Stabellinic1c54132010-05-14 12:44:30 +01001991 }
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001992 info = kzalloc(sizeof(*info), GFP_KERNEL);
1993 if (!info) {
1994 xenbus_dev_fatal(dev, -ENOMEM, "allocating info structure");
1995 return -ENOMEM;
1996 }
1997
Bob Liu28d949b2015-11-14 11:12:14 +08001998 info->xbdev = dev;
Bob Liu81f35162015-11-14 11:12:11 +08001999
Daniel Stoddenb70f5fa2010-04-30 22:01:19 +00002000 mutex_init(&info->mutex);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07002001 info->vdevice = vdevice;
2002 info->connected = BLKIF_STATE_DISCONNECTED;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07002003
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07002004 /* Front end dir is a number, which is used as the id. */
2005 info->handle = simple_strtoul(strrchr(dev->nodename, '/')+1, NULL, 0);
Greg Kroah-Hartmana1b4b122009-04-30 14:43:31 -07002006 dev_set_drvdata(&dev->dev, info);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07002007
Juergen Grossa46b5362018-08-13 16:01:11 +02002008 mutex_lock(&blkfront_mutex);
2009 list_add(&info->info_list, &info_list);
2010 mutex_unlock(&blkfront_mutex);
2011
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07002012 return 0;
2013}
2014
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07002015static int blkif_recover(struct blkfront_info *info)
2016{
NeilBrown4559fa52017-06-18 14:38:59 +10002017 unsigned int r_index;
Roger Pau Monne402b27f2013-04-18 16:06:54 +02002018 struct request *req, *n;
Roger Pau Monne402b27f2013-04-18 16:06:54 +02002019 int rc;
NeilBrown4559fa52017-06-18 14:38:59 +10002020 struct bio *bio;
2021 unsigned int segs;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07002022
Bob Liu3df0e502015-11-14 11:12:12 +08002023 blkfront_gather_backend_features(info);
Bob Liu172335a2016-07-01 17:43:39 -04002024 /* Reset limits changed by blk_mq_update_nr_hw_queues(). */
2025 blkif_set_queue_limits(info);
Roger Pau Monne402b27f2013-04-18 16:06:54 +02002026 segs = info->max_indirect_segments ? : BLKIF_MAX_SEGMENTS_PER_REQUEST;
Bob Liu6c647b02016-07-01 15:45:57 -04002027 blk_queue_max_segments(info->rq, segs / GRANTS_PER_PSEG);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07002028
Bob Liu3df0e502015-11-14 11:12:12 +08002029 for (r_index = 0; r_index < info->nr_rings; r_index++) {
Bob Liu7b427a52016-06-27 16:33:24 +08002030 struct blkfront_ring_info *rinfo = &info->rinfo[r_index];
Bob Liu3df0e502015-11-14 11:12:12 +08002031
2032 rc = blkfront_setup_indirect(rinfo);
Bob Liu7b427a52016-06-27 16:33:24 +08002033 if (rc)
Bob Liu3df0e502015-11-14 11:12:12 +08002034 return rc;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07002035 }
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07002036 xenbus_switch_state(info->xbdev, XenbusStateConnected);
2037
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07002038 /* Now safe for us to use the shared ring */
2039 info->connected = BLKIF_STATE_CONNECTED;
2040
Bob Liu3df0e502015-11-14 11:12:12 +08002041 for (r_index = 0; r_index < info->nr_rings; r_index++) {
2042 struct blkfront_ring_info *rinfo;
2043
2044 rinfo = &info->rinfo[r_index];
2045 /* Kick any other new requests queued since we resumed */
2046 kick_pending_request_queues(rinfo);
2047 }
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07002048
Bob Liu7b427a52016-06-27 16:33:24 +08002049 list_for_each_entry_safe(req, n, &info->requests, queuelist) {
Roger Pau Monne402b27f2013-04-18 16:06:54 +02002050 /* Requeue pending requests (flush or discard) */
2051 list_del_init(&req->queuelist);
2052 BUG_ON(req->nr_phys_segments > segs);
Bart Van Assche2b053ac2016-10-28 17:21:41 -07002053 blk_mq_requeue_request(req, false);
Roger Pau Monne402b27f2013-04-18 16:06:54 +02002054 }
Bart Van Assche52d7f1b2016-10-28 17:20:32 -07002055 blk_mq_start_stopped_hw_queues(info->rq, true);
Bob Liu907c3eb2015-07-13 17:55:24 +08002056 blk_mq_kick_requeue_list(info->rq);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07002057
Bob Liu7b427a52016-06-27 16:33:24 +08002058 while ((bio = bio_list_pop(&info->bio_list)) != NULL) {
Roger Pau Monne402b27f2013-04-18 16:06:54 +02002059 /* Traverse the list of pending bios and re-queue them */
Mike Christie4e49ea42016-06-05 14:31:41 -05002060 submit_bio(bio);
Roger Pau Monne402b27f2013-04-18 16:06:54 +02002061 }
2062
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07002063 return 0;
2064}
2065
2066/**
2067 * We are reconnecting to the backend, due to a suspend/resume, or a backend
2068 * driver restart. We tear down our blkif structure and recreate it, but
2069 * leave the device-layer structures intact so that this is transparent to the
2070 * rest of the kernel.
2071 */
2072static int blkfront_resume(struct xenbus_device *dev)
2073{
Greg Kroah-Hartmana1b4b122009-04-30 14:43:31 -07002074 struct blkfront_info *info = dev_get_drvdata(&dev->dev);
Bob Liu3db70a82015-11-25 17:52:55 -05002075 int err = 0;
Bob Liu7b427a52016-06-27 16:33:24 +08002076 unsigned int i, j;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07002077
2078 dev_dbg(&dev->dev, "blkfront_resume: %s\n", dev->nodename);
2079
Bob Liu7b427a52016-06-27 16:33:24 +08002080 bio_list_init(&info->bio_list);
2081 INIT_LIST_HEAD(&info->requests);
2082 for (i = 0; i < info->nr_rings; i++) {
2083 struct blkfront_ring_info *rinfo = &info->rinfo[i];
2084 struct bio_list merge_bio;
2085 struct blk_shadow *shadow = rinfo->shadow;
2086
2087 for (j = 0; j < BLK_RING_SIZE(info); j++) {
2088 /* Not in use? */
2089 if (!shadow[j].request)
2090 continue;
2091
2092 /*
2093 * Get the bios in the request so we can re-queue them.
2094 */
Munehisa Kamatab15bd8c2017-08-09 15:31:40 -07002095 if (req_op(shadow[j].request) == REQ_OP_FLUSH ||
2096 req_op(shadow[j].request) == REQ_OP_DISCARD ||
2097 req_op(shadow[j].request) == REQ_OP_SECURE_ERASE ||
Linus Torvalds3fc9d692016-07-26 15:37:51 -07002098 shadow[j].request->cmd_flags & REQ_FUA) {
Bob Liu7b427a52016-06-27 16:33:24 +08002099 /*
2100 * Flush operations don't contain bios, so
2101 * we need to requeue the whole request
Linus Torvalds3fc9d692016-07-26 15:37:51 -07002102 *
2103 * XXX: but this doesn't make any sense for a
2104 * write with the FUA flag set..
Bob Liu7b427a52016-06-27 16:33:24 +08002105 */
2106 list_add(&shadow[j].request->queuelist, &info->requests);
2107 continue;
2108 }
2109 merge_bio.head = shadow[j].request->bio;
2110 merge_bio.tail = shadow[j].request->biotail;
2111 bio_list_merge(&info->bio_list, &merge_bio);
2112 shadow[j].request->bio = NULL;
Christoph Hellwig2a842ac2017-06-03 09:38:04 +02002113 blk_mq_end_request(shadow[j].request, BLK_STS_OK);
Bob Liu7b427a52016-06-27 16:33:24 +08002114 }
2115 }
2116
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07002117 blkif_free(info, info->connected == BLKIF_STATE_CONNECTED);
2118
Ian Campbell203fd612009-12-04 15:33:54 +00002119 err = talk_to_blkback(dev, info);
Bob Liu2a6f71a2016-05-31 16:59:17 +08002120 if (!err)
2121 blk_mq_update_nr_hw_queues(&info->tag_set, info->nr_rings);
Roger Pau Monne402b27f2013-04-18 16:06:54 +02002122
2123 /*
2124 * We have to wait for the backend to switch to
2125 * connected state, since we want to read which
2126 * features it supports.
2127 */
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07002128
2129 return err;
2130}
2131
Konrad Rzeszutek Wilk6f03a7f2015-11-16 15:14:41 -05002132static void blkfront_closing(struct blkfront_info *info)
Daniel Stoddenb70f5fa2010-04-30 22:01:19 +00002133{
2134 struct xenbus_device *xbdev = info->xbdev;
2135 struct block_device *bdev = NULL;
2136
2137 mutex_lock(&info->mutex);
2138
2139 if (xbdev->state == XenbusStateClosing) {
2140 mutex_unlock(&info->mutex);
2141 return;
2142 }
2143
2144 if (info->gd)
2145 bdev = bdget_disk(info->gd, 0);
2146
2147 mutex_unlock(&info->mutex);
2148
2149 if (!bdev) {
2150 xenbus_frontend_closed(xbdev);
2151 return;
2152 }
2153
2154 mutex_lock(&bdev->bd_mutex);
2155
Daniel Stodden7b32d102010-04-30 22:01:23 +00002156 if (bdev->bd_openers) {
Daniel Stoddenb70f5fa2010-04-30 22:01:19 +00002157 xenbus_dev_error(xbdev, -EBUSY,
2158 "Device in use; refusing to close");
2159 xenbus_switch_state(xbdev, XenbusStateClosing);
2160 } else {
2161 xlvbd_release_gendisk(info);
2162 xenbus_frontend_closed(xbdev);
2163 }
2164
2165 mutex_unlock(&bdev->bd_mutex);
2166 bdput(bdev);
2167}
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07002168
Li Dongyanged30bf32011-09-01 18:39:09 +08002169static void blkfront_setup_discard(struct blkfront_info *info)
2170{
2171 int err;
Li Dongyanged30bf32011-09-01 18:39:09 +08002172 unsigned int discard_granularity;
2173 unsigned int discard_alignment;
2174
Olaf Hering1c8cad62014-05-21 16:32:40 +02002175 info->feature_discard = 1;
2176 err = xenbus_gather(XBT_NIL, info->xbdev->otherend,
2177 "discard-granularity", "%u", &discard_granularity,
2178 "discard-alignment", "%u", &discard_alignment,
2179 NULL);
2180 if (!err) {
2181 info->discard_granularity = discard_granularity;
2182 info->discard_alignment = discard_alignment;
2183 }
Juergen Grossf27dc1a2016-10-31 14:58:40 +01002184 info->feature_secdiscard =
2185 !!xenbus_read_unsigned(info->xbdev->otherend, "discard-secure",
2186 0);
Li Dongyanged30bf32011-09-01 18:39:09 +08002187}
2188
Bob Liu81f35162015-11-14 11:12:11 +08002189static int blkfront_setup_indirect(struct blkfront_ring_info *rinfo)
Roger Pau Monne402b27f2013-04-18 16:06:54 +02002190{
Julien Grallc004a6f2015-07-22 16:44:54 +01002191 unsigned int psegs, grants;
Roger Pau Monne402b27f2013-04-18 16:06:54 +02002192 int err, i;
Bob Liu81f35162015-11-14 11:12:11 +08002193 struct blkfront_info *info = rinfo->dev_info;
Roger Pau Monne402b27f2013-04-18 16:06:54 +02002194
Julien Grall6cc568332015-08-13 13:13:35 +01002195 if (info->max_indirect_segments == 0) {
2196 if (!HAS_EXTRA_REQ)
2197 grants = BLKIF_MAX_SEGMENTS_PER_REQUEST;
2198 else {
2199 /*
2200 * When an extra req is required, the maximum
2201 * grants supported is related to the size of the
2202 * Linux block segment.
2203 */
2204 grants = GRANTS_PER_PSEG;
2205 }
2206 }
Bob Liud50babb2015-07-22 14:40:08 +08002207 else
Julien Grallc004a6f2015-07-22 16:44:54 +01002208 grants = info->max_indirect_segments;
Jan Beulich3b4f1882017-01-23 08:11:37 -07002209 psegs = DIV_ROUND_UP(grants, GRANTS_PER_PSEG);
Roger Pau Monne402b27f2013-04-18 16:06:54 +02002210
Bob Liu81f35162015-11-14 11:12:11 +08002211 err = fill_grant_buffer(rinfo,
Julien Grallc004a6f2015-07-22 16:44:54 +01002212 (grants + INDIRECT_GREFS(grants)) * BLK_RING_SIZE(info));
Roger Pau Monne402b27f2013-04-18 16:06:54 +02002213 if (err)
2214 goto out_of_memory;
2215
Roger Pau Monnebfe11d62013-10-29 18:31:14 +01002216 if (!info->feature_persistent && info->max_indirect_segments) {
2217 /*
2218 * We are using indirect descriptors but not persistent
2219 * grants, we need to allocate a set of pages that can be
2220 * used for mapping indirect grefs
2221 */
Julien Grallc004a6f2015-07-22 16:44:54 +01002222 int num = INDIRECT_GREFS(grants) * BLK_RING_SIZE(info);
Roger Pau Monnebfe11d62013-10-29 18:31:14 +01002223
Bob Liu81f35162015-11-14 11:12:11 +08002224 BUG_ON(!list_empty(&rinfo->indirect_pages));
Roger Pau Monnebfe11d62013-10-29 18:31:14 +01002225 for (i = 0; i < num; i++) {
2226 struct page *indirect_page = alloc_page(GFP_NOIO);
2227 if (!indirect_page)
2228 goto out_of_memory;
Bob Liu81f35162015-11-14 11:12:11 +08002229 list_add(&indirect_page->lru, &rinfo->indirect_pages);
Roger Pau Monnebfe11d62013-10-29 18:31:14 +01002230 }
2231 }
2232
Bob Liu86839c52015-06-03 13:40:03 +08002233 for (i = 0; i < BLK_RING_SIZE(info); i++) {
Kees Cook6396bb22018-06-12 14:03:40 -07002234 rinfo->shadow[i].grants_used =
Roger Pau Monne1d5c76e2019-05-03 17:04:01 +02002235 kvcalloc(grants,
2236 sizeof(rinfo->shadow[i].grants_used[0]),
2237 GFP_NOIO);
2238 rinfo->shadow[i].sg = kvcalloc(psegs,
2239 sizeof(rinfo->shadow[i].sg[0]),
2240 GFP_NOIO);
Kees Cook6396bb22018-06-12 14:03:40 -07002241 if (info->max_indirect_segments)
2242 rinfo->shadow[i].indirect_grants =
Roger Pau Monne1d5c76e2019-05-03 17:04:01 +02002243 kvcalloc(INDIRECT_GREFS(grants),
2244 sizeof(rinfo->shadow[i].indirect_grants[0]),
2245 GFP_NOIO);
Bob Liu81f35162015-11-14 11:12:11 +08002246 if ((rinfo->shadow[i].grants_used == NULL) ||
2247 (rinfo->shadow[i].sg == NULL) ||
Roger Pau Monne402b27f2013-04-18 16:06:54 +02002248 (info->max_indirect_segments &&
Bob Liu81f35162015-11-14 11:12:11 +08002249 (rinfo->shadow[i].indirect_grants == NULL)))
Roger Pau Monne402b27f2013-04-18 16:06:54 +02002250 goto out_of_memory;
Bob Liu81f35162015-11-14 11:12:11 +08002251 sg_init_table(rinfo->shadow[i].sg, psegs);
Roger Pau Monne402b27f2013-04-18 16:06:54 +02002252 }
2253
2254
2255 return 0;
2256
2257out_of_memory:
Bob Liu86839c52015-06-03 13:40:03 +08002258 for (i = 0; i < BLK_RING_SIZE(info); i++) {
Roger Pau Monne1d5c76e2019-05-03 17:04:01 +02002259 kvfree(rinfo->shadow[i].grants_used);
Bob Liu81f35162015-11-14 11:12:11 +08002260 rinfo->shadow[i].grants_used = NULL;
Roger Pau Monne1d5c76e2019-05-03 17:04:01 +02002261 kvfree(rinfo->shadow[i].sg);
Bob Liu81f35162015-11-14 11:12:11 +08002262 rinfo->shadow[i].sg = NULL;
Roger Pau Monne1d5c76e2019-05-03 17:04:01 +02002263 kvfree(rinfo->shadow[i].indirect_grants);
Bob Liu81f35162015-11-14 11:12:11 +08002264 rinfo->shadow[i].indirect_grants = NULL;
Roger Pau Monne402b27f2013-04-18 16:06:54 +02002265 }
Bob Liu81f35162015-11-14 11:12:11 +08002266 if (!list_empty(&rinfo->indirect_pages)) {
Roger Pau Monnebfe11d62013-10-29 18:31:14 +01002267 struct page *indirect_page, *n;
Bob Liu81f35162015-11-14 11:12:11 +08002268 list_for_each_entry_safe(indirect_page, n, &rinfo->indirect_pages, lru) {
Roger Pau Monnebfe11d62013-10-29 18:31:14 +01002269 list_del(&indirect_page->lru);
2270 __free_page(indirect_page);
2271 }
2272 }
Roger Pau Monne402b27f2013-04-18 16:06:54 +02002273 return -ENOMEM;
2274}
2275
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07002276/*
Bob Liud50babb2015-07-22 14:40:08 +08002277 * Gather all backend feature-*
2278 */
Bob Liu3df0e502015-11-14 11:12:12 +08002279static void blkfront_gather_backend_features(struct blkfront_info *info)
Bob Liud50babb2015-07-22 14:40:08 +08002280{
Bob Liud50babb2015-07-22 14:40:08 +08002281 unsigned int indirect_segments;
2282
2283 info->feature_flush = 0;
Mike Christiea4180902016-06-05 14:32:24 -05002284 info->feature_fua = 0;
Bob Liud50babb2015-07-22 14:40:08 +08002285
Bob Liud50babb2015-07-22 14:40:08 +08002286 /*
2287 * If there's no "feature-barrier" defined, then it means
2288 * we're dealing with a very old backend which writes
2289 * synchronously; nothing to do.
2290 *
2291 * If there are barriers, then we use flush.
2292 */
Juergen Grossf27dc1a2016-10-31 14:58:40 +01002293 if (xenbus_read_unsigned(info->xbdev->otherend, "feature-barrier", 0)) {
Mike Christiea4180902016-06-05 14:32:24 -05002294 info->feature_flush = 1;
2295 info->feature_fua = 1;
2296 }
2297
Bob Liud50babb2015-07-22 14:40:08 +08002298 /*
2299 * And if there is "feature-flush-cache" use that above
2300 * barriers.
2301 */
Juergen Grossf27dc1a2016-10-31 14:58:40 +01002302 if (xenbus_read_unsigned(info->xbdev->otherend, "feature-flush-cache",
2303 0)) {
Mike Christiea4180902016-06-05 14:32:24 -05002304 info->feature_flush = 1;
2305 info->feature_fua = 0;
2306 }
Bob Liud50babb2015-07-22 14:40:08 +08002307
Juergen Grossf27dc1a2016-10-31 14:58:40 +01002308 if (xenbus_read_unsigned(info->xbdev->otherend, "feature-discard", 0))
Bob Liud50babb2015-07-22 14:40:08 +08002309 blkfront_setup_discard(info);
2310
Juergen Grossf27dc1a2016-10-31 14:58:40 +01002311 info->feature_persistent =
Jan Beulichb32728f2017-01-23 08:12:19 -07002312 !!xenbus_read_unsigned(info->xbdev->otherend,
2313 "feature-persistent", 0);
Bob Liud50babb2015-07-22 14:40:08 +08002314
Juergen Grossf27dc1a2016-10-31 14:58:40 +01002315 indirect_segments = xenbus_read_unsigned(info->xbdev->otherend,
2316 "feature-max-indirect-segments", 0);
Jan Beulich3b4f1882017-01-23 08:11:37 -07002317 if (indirect_segments > xen_blkif_max_segments)
2318 indirect_segments = xen_blkif_max_segments;
2319 if (indirect_segments <= BLKIF_MAX_SEGMENTS_PER_REQUEST)
2320 indirect_segments = 0;
2321 info->max_indirect_segments = indirect_segments;
Juergen Grossa46b5362018-08-13 16:01:11 +02002322
2323 if (info->feature_persistent) {
2324 mutex_lock(&blkfront_mutex);
2325 schedule_delayed_work(&blkfront_work, HZ * 10);
2326 mutex_unlock(&blkfront_mutex);
2327 }
Bob Liud50babb2015-07-22 14:40:08 +08002328}
2329
2330/*
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07002331 * Invoked when the backend is finally 'ready' (and has told produced
2332 * the details about the physical device - #sectors, size, etc).
2333 */
2334static void blkfront_connect(struct blkfront_info *info)
2335{
2336 unsigned long long sectors;
2337 unsigned long sector_size;
Stefan Bader7c4d7d72013-05-13 16:28:15 +02002338 unsigned int physical_sector_size;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07002339 unsigned int binfo;
Marc Olson89515d02017-04-11 12:24:09 -07002340 char *envp[] = { "RESIZE=1", NULL };
Bob Liu3df0e502015-11-14 11:12:12 +08002341 int err, i;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07002342
K. Y. Srinivasan1fa73be2010-03-11 13:42:26 -08002343 switch (info->connected) {
2344 case BLKIF_STATE_CONNECTED:
2345 /*
2346 * Potentially, the back-end may be signalling
2347 * a capacity change; update the capacity.
2348 */
2349 err = xenbus_scanf(XBT_NIL, info->xbdev->otherend,
2350 "sectors", "%Lu", &sectors);
2351 if (XENBUS_EXIST_ERR(err))
2352 return;
2353 printk(KERN_INFO "Setting capacity to %Lu\n",
2354 sectors);
2355 set_capacity(info->gd, sectors);
K. Y. Srinivasan2def1412010-03-18 15:00:54 -07002356 revalidate_disk(info->gd);
Marc Olson89515d02017-04-11 12:24:09 -07002357 kobject_uevent_env(&disk_to_dev(info->gd)->kobj,
2358 KOBJ_CHANGE, envp);
K. Y. Srinivasan1fa73be2010-03-11 13:42:26 -08002359
Roger Pau Monne402b27f2013-04-18 16:06:54 +02002360 return;
K. Y. Srinivasan1fa73be2010-03-11 13:42:26 -08002361 case BLKIF_STATE_SUSPENDED:
Roger Pau Monne402b27f2013-04-18 16:06:54 +02002362 /*
2363 * If we are recovering from suspension, we need to wait
2364 * for the backend to announce it's features before
2365 * reconnecting, at least we need to know if the backend
2366 * supports indirect descriptors, and how many.
2367 */
2368 blkif_recover(info);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07002369 return;
2370
Jeremy Fitzhardingeb4dddb42010-03-11 15:10:40 -08002371 default:
2372 break;
K. Y. Srinivasan1fa73be2010-03-11 13:42:26 -08002373 }
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07002374
2375 dev_dbg(&info->xbdev->dev, "%s:%s.\n",
2376 __func__, info->xbdev->otherend);
2377
2378 err = xenbus_gather(XBT_NIL, info->xbdev->otherend,
2379 "sectors", "%llu", &sectors,
2380 "info", "%u", &binfo,
2381 "sector-size", "%lu", &sector_size,
2382 NULL);
2383 if (err) {
2384 xenbus_dev_fatal(info->xbdev, err,
2385 "reading backend fields at %s",
2386 info->xbdev->otherend);
2387 return;
2388 }
2389
Stefan Bader7c4d7d72013-05-13 16:28:15 +02002390 /*
2391 * physcial-sector-size is a newer field, so old backends may not
2392 * provide this. Assume physical sector size to be the same as
2393 * sector_size in that case.
2394 */
Juergen Grossf27dc1a2016-10-31 14:58:40 +01002395 physical_sector_size = xenbus_read_unsigned(info->xbdev->otherend,
2396 "physical-sector-size",
2397 sector_size);
Bob Liu3df0e502015-11-14 11:12:12 +08002398 blkfront_gather_backend_features(info);
2399 for (i = 0; i < info->nr_rings; i++) {
2400 err = blkfront_setup_indirect(&info->rinfo[i]);
2401 if (err) {
2402 xenbus_dev_fatal(info->xbdev, err, "setup_indirect at %s",
2403 info->xbdev->otherend);
2404 blkif_free(info, 0);
2405 break;
2406 }
Roger Pau Monne402b27f2013-04-18 16:06:54 +02002407 }
2408
Stefan Bader7c4d7d72013-05-13 16:28:15 +02002409 err = xlvbd_alloc_gendisk(sectors, info, binfo, sector_size,
2410 physical_sector_size);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07002411 if (err) {
2412 xenbus_dev_fatal(info->xbdev, err, "xlvbd_add at %s",
2413 info->xbdev->otherend);
Bob Liu4e876c22016-07-27 17:42:04 +08002414 goto fail;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07002415 }
2416
2417 xenbus_switch_state(info->xbdev, XenbusStateConnected);
2418
2419 /* Kick pending requests. */
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07002420 info->connected = BLKIF_STATE_CONNECTED;
Bob Liu3df0e502015-11-14 11:12:12 +08002421 for (i = 0; i < info->nr_rings; i++)
2422 kick_pending_request_queues(&info->rinfo[i]);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07002423
Hannes Reineckefef912b2018-09-28 08:17:19 +02002424 device_add_disk(&info->xbdev->dev, info->gd, NULL);
Christian Limpach1d78d702008-04-02 10:54:04 -07002425
2426 info->is_ready = 1;
Bob Liu4e876c22016-07-27 17:42:04 +08002427 return;
2428
2429fail:
2430 blkif_free(info, 0);
2431 return;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07002432}
2433
2434/**
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07002435 * Callback received when the backend's state changes.
2436 */
Ian Campbell203fd612009-12-04 15:33:54 +00002437static void blkback_changed(struct xenbus_device *dev,
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07002438 enum xenbus_state backend_state)
2439{
Greg Kroah-Hartmana1b4b122009-04-30 14:43:31 -07002440 struct blkfront_info *info = dev_get_drvdata(&dev->dev);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07002441
Ian Campbell203fd612009-12-04 15:33:54 +00002442 dev_dbg(&dev->dev, "blkfront:blkback_changed to state %d.\n", backend_state);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07002443
2444 switch (backend_state) {
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07002445 case XenbusStateInitWait:
Bob Liua9b54bb2015-06-19 00:23:00 -04002446 if (dev->state != XenbusStateInitialising)
2447 break;
Konrad Rzeszutek Wilkc31ecf62015-12-18 16:28:53 -05002448 if (talk_to_blkback(dev, info))
Bob Liu8ab01442015-06-03 13:40:02 +08002449 break;
Bob Liu8ab01442015-06-03 13:40:02 +08002450 case XenbusStateInitialising:
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07002451 case XenbusStateInitialised:
Noboru Iwamatsub78c9512009-10-13 17:22:29 -04002452 case XenbusStateReconfiguring:
2453 case XenbusStateReconfigured:
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07002454 case XenbusStateUnknown:
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07002455 break;
2456
2457 case XenbusStateConnected:
Bob Liuefd15352016-06-07 10:43:15 -04002458 /*
2459 * talk_to_blkback sets state to XenbusStateInitialised
2460 * and blkfront_connect sets it to XenbusStateConnected
2461 * (if connection went OK).
2462 *
2463 * If the backend (or toolstack) decides to poke at backend
2464 * state (and re-trigger the watch by setting the state repeatedly
2465 * to XenbusStateConnected (4)) we need to deal with this.
2466 * This is allowed as this is used to communicate to the guest
2467 * that the size of disk has changed!
2468 */
2469 if ((dev->state != XenbusStateInitialised) &&
2470 (dev->state != XenbusStateConnected)) {
Konrad Rzeszutek Wilkc31ecf62015-12-18 16:28:53 -05002471 if (talk_to_blkback(dev, info))
2472 break;
2473 }
Bob Liuefd15352016-06-07 10:43:15 -04002474
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07002475 blkfront_connect(info);
2476 break;
2477
David Vrabel36613712014-02-04 18:53:56 +00002478 case XenbusStateClosed:
2479 if (dev->state == XenbusStateClosed)
2480 break;
Bart Van Asscheccc22252017-08-17 16:23:11 -07002481 /* fall through */
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07002482 case XenbusStateClosing:
Cathy Averya54c8f02015-10-02 09:35:01 -04002483 if (info)
2484 blkfront_closing(info);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07002485 break;
2486 }
2487}
2488
Daniel Stoddenfa1bd352010-04-30 22:01:22 +00002489static int blkfront_remove(struct xenbus_device *xbdev)
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07002490{
Daniel Stoddenfa1bd352010-04-30 22:01:22 +00002491 struct blkfront_info *info = dev_get_drvdata(&xbdev->dev);
2492 struct block_device *bdev = NULL;
2493 struct gendisk *disk;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07002494
Daniel Stoddenfa1bd352010-04-30 22:01:22 +00002495 dev_dbg(&xbdev->dev, "%s removed", xbdev->nodename);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07002496
Vasilis Liaskovitisf92898e2018-10-15 15:25:08 +02002497 if (!info)
2498 return 0;
2499
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07002500 blkif_free(info, 0);
2501
Daniel Stoddenfa1bd352010-04-30 22:01:22 +00002502 mutex_lock(&info->mutex);
2503
2504 disk = info->gd;
2505 if (disk)
2506 bdev = bdget_disk(disk, 0);
2507
2508 info->xbdev = NULL;
2509 mutex_unlock(&info->mutex);
2510
2511 if (!bdev) {
Juergen Grossa46b5362018-08-13 16:01:11 +02002512 mutex_lock(&blkfront_mutex);
2513 free_info(info);
2514 mutex_unlock(&blkfront_mutex);
Daniel Stoddenfa1bd352010-04-30 22:01:22 +00002515 return 0;
2516 }
2517
2518 /*
2519 * The xbdev was removed before we reached the Closed
2520 * state. See if it's safe to remove the disk. If the bdev
2521 * isn't closed yet, we let release take care of it.
2522 */
2523
2524 mutex_lock(&bdev->bd_mutex);
2525 info = disk->private_data;
2526
Daniel Stoddend54142c2010-08-07 18:51:21 +02002527 dev_warn(disk_to_dev(disk),
2528 "%s was hot-unplugged, %d stale handles\n",
2529 xbdev->nodename, bdev->bd_openers);
2530
Daniel Stodden7b32d102010-04-30 22:01:23 +00002531 if (info && !bdev->bd_openers) {
Daniel Stoddenfa1bd352010-04-30 22:01:22 +00002532 xlvbd_release_gendisk(info);
2533 disk->private_data = NULL;
Juergen Grossa46b5362018-08-13 16:01:11 +02002534 mutex_lock(&blkfront_mutex);
2535 free_info(info);
2536 mutex_unlock(&blkfront_mutex);
Daniel Stoddenfa1bd352010-04-30 22:01:22 +00002537 }
2538
2539 mutex_unlock(&bdev->bd_mutex);
2540 bdput(bdev);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07002541
2542 return 0;
2543}
2544
Christian Limpach1d78d702008-04-02 10:54:04 -07002545static int blkfront_is_ready(struct xenbus_device *dev)
2546{
Greg Kroah-Hartmana1b4b122009-04-30 14:43:31 -07002547 struct blkfront_info *info = dev_get_drvdata(&dev->dev);
Christian Limpach1d78d702008-04-02 10:54:04 -07002548
Jan Beulich5d7ed202010-08-07 18:31:12 +02002549 return info->is_ready && info->xbdev;
Christian Limpach1d78d702008-04-02 10:54:04 -07002550}
2551
Al Viroa63c8482008-03-02 10:23:47 -05002552static int blkif_open(struct block_device *bdev, fmode_t mode)
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07002553{
Daniel Stodden13961742010-08-07 18:36:53 +02002554 struct gendisk *disk = bdev->bd_disk;
2555 struct blkfront_info *info;
2556 int err = 0;
Arnd Bergmann6e9624b2010-08-07 18:25:34 +02002557
Arnd Bergmann2a48fc02010-06-02 14:28:52 +02002558 mutex_lock(&blkfront_mutex);
Arnd Bergmann6e9624b2010-08-07 18:25:34 +02002559
Daniel Stodden13961742010-08-07 18:36:53 +02002560 info = disk->private_data;
2561 if (!info) {
2562 /* xbdev gone */
2563 err = -ERESTARTSYS;
2564 goto out;
2565 }
2566
2567 mutex_lock(&info->mutex);
2568
2569 if (!info->gd)
2570 /* xbdev is closed */
2571 err = -ERESTARTSYS;
2572
2573 mutex_unlock(&info->mutex);
2574
Daniel Stodden13961742010-08-07 18:36:53 +02002575out:
Arnd Bergmann2a48fc02010-06-02 14:28:52 +02002576 mutex_unlock(&blkfront_mutex);
Daniel Stodden13961742010-08-07 18:36:53 +02002577 return err;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07002578}
2579
Al Virodb2a1442013-05-05 21:52:57 -04002580static void blkif_release(struct gendisk *disk, fmode_t mode)
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07002581{
Al Viroa63c8482008-03-02 10:23:47 -05002582 struct blkfront_info *info = disk->private_data;
Daniel Stodden7fd152f2010-08-07 18:45:12 +02002583 struct block_device *bdev;
2584 struct xenbus_device *xbdev;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07002585
Arnd Bergmann2a48fc02010-06-02 14:28:52 +02002586 mutex_lock(&blkfront_mutex);
Daniel Stodden7fd152f2010-08-07 18:45:12 +02002587
2588 bdev = bdget_disk(disk, 0);
Daniel Stodden7fd152f2010-08-07 18:45:12 +02002589
Felipe Pena2f089cb2013-11-09 13:36:09 -02002590 if (!bdev) {
2591 WARN(1, "Block device %s yanked out from us!\n", disk->disk_name);
2592 goto out_mutex;
2593 }
Daniel Stoddenacfca3c2010-08-07 18:47:26 +02002594 if (bdev->bd_openers)
2595 goto out;
2596
Daniel Stodden7fd152f2010-08-07 18:45:12 +02002597 /*
2598 * Check if we have been instructed to close. We will have
2599 * deferred this request, because the bdev was still open.
2600 */
2601
2602 mutex_lock(&info->mutex);
2603 xbdev = info->xbdev;
2604
2605 if (xbdev && xbdev->state == XenbusStateClosing) {
2606 /* pending switch to state closed */
Daniel Stoddend54142c2010-08-07 18:51:21 +02002607 dev_info(disk_to_dev(bdev->bd_disk), "releasing disk\n");
Daniel Stodden7fd152f2010-08-07 18:45:12 +02002608 xlvbd_release_gendisk(info);
2609 xenbus_frontend_closed(info->xbdev);
2610 }
2611
2612 mutex_unlock(&info->mutex);
2613
2614 if (!xbdev) {
2615 /* sudden device removal */
Daniel Stoddend54142c2010-08-07 18:51:21 +02002616 dev_info(disk_to_dev(bdev->bd_disk), "releasing disk\n");
Daniel Stodden7fd152f2010-08-07 18:45:12 +02002617 xlvbd_release_gendisk(info);
2618 disk->private_data = NULL;
Juergen Grossa46b5362018-08-13 16:01:11 +02002619 free_info(info);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07002620 }
Daniel Stodden7fd152f2010-08-07 18:45:12 +02002621
Jens Axboea4cc14e2010-08-08 21:50:05 -04002622out:
Andrew Jonesdad5cf62012-02-16 13:16:25 +01002623 bdput(bdev);
Felipe Pena2f089cb2013-11-09 13:36:09 -02002624out_mutex:
Arnd Bergmann2a48fc02010-06-02 14:28:52 +02002625 mutex_unlock(&blkfront_mutex);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07002626}
2627
Alexey Dobriyan83d5cde2009-09-21 17:01:13 -07002628static const struct block_device_operations xlvbd_block_fops =
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07002629{
2630 .owner = THIS_MODULE,
Al Viroa63c8482008-03-02 10:23:47 -05002631 .open = blkif_open,
2632 .release = blkif_release,
Ian Campbell597592d2008-02-21 13:03:45 -08002633 .getgeo = blkif_getgeo,
Arnd Bergmann8a6cfeb2010-07-08 10:18:46 +02002634 .ioctl = blkif_ioctl,
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07002635};
2636
2637
Márton Némethec9c42e2010-01-10 13:39:52 +01002638static const struct xenbus_device_id blkfront_ids[] = {
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07002639 { "vbd" },
2640 { "" }
2641};
2642
David Vrabel95afae42014-09-08 17:30:41 +01002643static struct xenbus_driver blkfront_driver = {
2644 .ids = blkfront_ids,
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07002645 .probe = blkfront_probe,
2646 .remove = blkfront_remove,
2647 .resume = blkfront_resume,
Ian Campbell203fd612009-12-04 15:33:54 +00002648 .otherend_changed = blkback_changed,
Christian Limpach1d78d702008-04-02 10:54:04 -07002649 .is_ready = blkfront_is_ready,
David Vrabel95afae42014-09-08 17:30:41 +01002650};
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07002651
Juergen Grossa46b5362018-08-13 16:01:11 +02002652static void purge_persistent_grants(struct blkfront_info *info)
2653{
2654 unsigned int i;
2655 unsigned long flags;
2656
2657 for (i = 0; i < info->nr_rings; i++) {
2658 struct blkfront_ring_info *rinfo = &info->rinfo[i];
2659 struct grant *gnt_list_entry, *tmp;
2660
2661 spin_lock_irqsave(&rinfo->ring_lock, flags);
2662
2663 if (rinfo->persistent_gnts_c == 0) {
2664 spin_unlock_irqrestore(&rinfo->ring_lock, flags);
2665 continue;
2666 }
2667
2668 list_for_each_entry_safe(gnt_list_entry, tmp, &rinfo->grants,
2669 node) {
2670 if (gnt_list_entry->gref == GRANT_INVALID_REF ||
2671 gnttab_query_foreign_access(gnt_list_entry->gref))
2672 continue;
2673
2674 list_del(&gnt_list_entry->node);
2675 gnttab_end_foreign_access(gnt_list_entry->gref, 0, 0UL);
2676 rinfo->persistent_gnts_c--;
Juergen Gross6c767862018-09-28 09:28:27 +02002677 gnt_list_entry->gref = GRANT_INVALID_REF;
2678 list_add_tail(&gnt_list_entry->node, &rinfo->grants);
Juergen Grossa46b5362018-08-13 16:01:11 +02002679 }
2680
2681 spin_unlock_irqrestore(&rinfo->ring_lock, flags);
2682 }
2683}
2684
2685static void blkfront_delay_work(struct work_struct *work)
2686{
2687 struct blkfront_info *info;
2688 bool need_schedule_work = false;
2689
2690 mutex_lock(&blkfront_mutex);
2691
2692 list_for_each_entry(info, &info_list, info_list) {
2693 if (info->feature_persistent) {
2694 need_schedule_work = true;
2695 mutex_lock(&info->mutex);
2696 purge_persistent_grants(info);
2697 mutex_unlock(&info->mutex);
2698 }
2699 }
2700
2701 if (need_schedule_work)
2702 schedule_delayed_work(&blkfront_work, HZ * 10);
2703
2704 mutex_unlock(&blkfront_mutex);
2705}
2706
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07002707static int __init xlblk_init(void)
2708{
Laszlo Ersek469738e2011-10-07 21:34:38 +02002709 int ret;
Bob Liu28d949b2015-11-14 11:12:14 +08002710 int nr_cpus = num_online_cpus();
Laszlo Ersek469738e2011-10-07 21:34:38 +02002711
Jeremy Fitzhardinge6e833582008-08-19 13:16:17 -07002712 if (!xen_domain())
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07002713 return -ENODEV;
2714
Juergen Gross4bcddba2018-08-13 16:01:12 +02002715 if (!xen_has_pv_disk_devices())
2716 return -ENODEV;
2717
2718 if (register_blkdev(XENVBD_MAJOR, DEV_NAME)) {
2719 pr_warn("xen_blk: can't get major %d with name %s\n",
2720 XENVBD_MAJOR, DEV_NAME);
2721 return -ENODEV;
2722 }
2723
Jan Beulich3b4f1882017-01-23 08:11:37 -07002724 if (xen_blkif_max_segments < BLKIF_MAX_SEGMENTS_PER_REQUEST)
2725 xen_blkif_max_segments = BLKIF_MAX_SEGMENTS_PER_REQUEST;
2726
Julien Grall9cce2912015-10-13 17:50:11 +01002727 if (xen_blkif_max_ring_order > XENBUS_MAX_RING_GRANT_ORDER) {
Bob Liu86839c52015-06-03 13:40:03 +08002728 pr_info("Invalid max_ring_order (%d), will use default max: %d.\n",
Julien Grall9cce2912015-10-13 17:50:11 +01002729 xen_blkif_max_ring_order, XENBUS_MAX_RING_GRANT_ORDER);
Peng Fan45fc8262015-11-25 18:26:01 +08002730 xen_blkif_max_ring_order = XENBUS_MAX_RING_GRANT_ORDER;
Bob Liu86839c52015-06-03 13:40:03 +08002731 }
2732
Bob Liu28d949b2015-11-14 11:12:14 +08002733 if (xen_blkif_max_queues > nr_cpus) {
2734 pr_info("Invalid max_queues (%d), will use default max: %d.\n",
2735 xen_blkif_max_queues, nr_cpus);
2736 xen_blkif_max_queues = nr_cpus;
2737 }
2738
Juergen Grossa46b5362018-08-13 16:01:11 +02002739 INIT_DELAYED_WORK(&blkfront_work, blkfront_delay_work);
2740
Jan Beulich73db1442011-12-22 09:08:13 +00002741 ret = xenbus_register_frontend(&blkfront_driver);
Laszlo Ersek469738e2011-10-07 21:34:38 +02002742 if (ret) {
2743 unregister_blkdev(XENVBD_MAJOR, DEV_NAME);
2744 return ret;
2745 }
2746
2747 return 0;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07002748}
2749module_init(xlblk_init);
2750
2751
Jan Beulich5a60d0c2008-06-17 10:47:08 +02002752static void __exit xlblk_exit(void)
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07002753{
Juergen Grossa46b5362018-08-13 16:01:11 +02002754 cancel_delayed_work_sync(&blkfront_work);
2755
Jan Beulich86050672012-04-05 16:04:52 +01002756 xenbus_unregister_driver(&blkfront_driver);
2757 unregister_blkdev(XENVBD_MAJOR, DEV_NAME);
2758 kfree(minors);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07002759}
2760module_exit(xlblk_exit);
2761
2762MODULE_DESCRIPTION("Xen virtual block device frontend");
2763MODULE_LICENSE("GPL");
2764MODULE_ALIAS_BLOCKDEV_MAJOR(XENVBD_MAJOR);
Mark McLoughlind2f0c522008-04-02 10:54:05 -07002765MODULE_ALIAS("xen:vbd");
Mark McLoughlin4f93f09b2008-04-02 10:54:06 -07002766MODULE_ALIAS("xenblk");