blob: a894f88762d8d3a1f72315e805f9494cdad8294d [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>
Ian Campbell597592d2008-02-21 13:03:45 -080040#include <linux/hdreg.h>
Christian Limpach440a01a2008-06-17 10:47:08 +020041#include <linux/cdrom.h>
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -070042#include <linux/module.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090043#include <linux/slab.h>
Arnd Bergmann2a48fc02010-06-02 14:28:52 +020044#include <linux/mutex.h>
Jens Axboe9e973e62009-02-24 08:10:09 +010045#include <linux/scatterlist.h>
Akinobu Mita34ae2e42012-01-21 00:15:26 +090046#include <linux/bitmap.h>
Roger Pau Monne155b7ed2013-03-18 17:49:34 +010047#include <linux/list.h>
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -070048
Jeremy Fitzhardinge1ccbf532009-10-06 15:11:14 -070049#include <xen/xen.h>
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -070050#include <xen/xenbus.h>
51#include <xen/grant_table.h>
52#include <xen/events.h>
53#include <xen/page.h>
Stefano Stabellinic1c54132010-05-14 12:44:30 +010054#include <xen/platform_pci.h>
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -070055
56#include <xen/interface/grant_table.h>
57#include <xen/interface/io/blkif.h>
Markus Armbruster3e334232008-04-02 10:54:02 -070058#include <xen/interface/io/protocols.h>
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -070059
60#include <asm/xen/hypervisor.h>
61
62enum blkif_state {
63 BLKIF_STATE_DISCONNECTED,
64 BLKIF_STATE_CONNECTED,
65 BLKIF_STATE_SUSPENDED,
66};
67
Roger Pau Monne0a8704a2012-10-24 18:58:45 +020068struct grant {
69 grant_ref_t gref;
70 unsigned long pfn;
Roger Pau Monne155b7ed2013-03-18 17:49:34 +010071 struct list_head node;
Roger Pau Monne0a8704a2012-10-24 18:58:45 +020072};
73
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -070074struct blk_shadow {
75 struct blkif_request req;
Jeremy Fitzhardingea945b982010-11-01 17:03:14 -040076 struct request *request;
Roger Pau Monne0a8704a2012-10-24 18:58:45 +020077 struct grant *grants_used[BLKIF_MAX_SEGMENTS_PER_REQUEST];
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -070078};
79
Arnd Bergmann2a48fc02010-06-02 14:28:52 +020080static DEFINE_MUTEX(blkfront_mutex);
Alexey Dobriyan83d5cde2009-09-21 17:01:13 -070081static const struct block_device_operations xlvbd_block_fops;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -070082
Jeremy Fitzhardinge667c78af2010-12-08 12:39:12 -080083#define BLK_RING_SIZE __CONST_RING_SIZE(blkif, PAGE_SIZE)
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -070084
85/*
86 * We have one of these per vbd, whether ide, scsi or 'other'. They
87 * hang in private_data off the gendisk structure. We may end up
88 * putting all kinds of interesting stuff here :-)
89 */
90struct blkfront_info
91{
Steven Noonan34678112012-02-17 12:04:44 -080092 spinlock_t io_lock;
Daniel Stoddenb70f5fa2010-04-30 22:01:19 +000093 struct mutex mutex;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -070094 struct xenbus_device *xbdev;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -070095 struct gendisk *gd;
96 int vdevice;
97 blkif_vdev_t handle;
98 enum blkif_state connected;
99 int ring_ref;
100 struct blkif_front_ring ring;
Jens Axboe9e973e62009-02-24 08:10:09 +0100101 struct scatterlist sg[BLKIF_MAX_SEGMENTS_PER_REQUEST];
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700102 unsigned int evtchn, irq;
103 struct request_queue *rq;
104 struct work_struct work;
105 struct gnttab_free_callback callback;
106 struct blk_shadow shadow[BLK_RING_SIZE];
Roger Pau Monne155b7ed2013-03-18 17:49:34 +0100107 struct list_head persistent_gnts;
Roger Pau Monne0a8704a2012-10-24 18:58:45 +0200108 unsigned int persistent_gnts_c;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700109 unsigned long shadow_free;
Tejun Heo4913efe2010-09-03 11:56:16 +0200110 unsigned int feature_flush;
Konrad Rzeszutek Wilkedf6ef52011-05-03 12:01:11 -0400111 unsigned int flush_op;
Konrad Rzeszutek Wilk5ea42982011-10-12 16:23:30 -0400112 unsigned int feature_discard:1;
113 unsigned int feature_secdiscard:1;
Li Dongyanged30bf32011-09-01 18:39:09 +0800114 unsigned int discard_granularity;
115 unsigned int discard_alignment;
Roger Pau Monne0a8704a2012-10-24 18:58:45 +0200116 unsigned int feature_persistent:1;
Christian Limpach1d78d702008-04-02 10:54:04 -0700117 int is_ready;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700118};
119
Jan Beulich0e345822010-08-07 18:28:55 +0200120static unsigned int nr_minors;
121static unsigned long *minors;
122static DEFINE_SPINLOCK(minor_lock);
123
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700124#define MAXIMUM_OUTSTANDING_BLOCK_REQS \
125 (BLKIF_MAX_SEGMENTS_PER_REQUEST * BLK_RING_SIZE)
126#define GRANT_INVALID_REF 0
127
128#define PARTS_PER_DISK 16
Chris Lalancette9246b5f2008-09-17 14:30:32 -0700129#define PARTS_PER_EXT_DISK 256
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700130
131#define BLKIF_MAJOR(dev) ((dev)>>8)
132#define BLKIF_MINOR(dev) ((dev) & 0xff)
133
Chris Lalancette9246b5f2008-09-17 14:30:32 -0700134#define EXT_SHIFT 28
135#define EXTENDED (1<<EXT_SHIFT)
136#define VDEV_IS_EXTENDED(dev) ((dev)&(EXTENDED))
137#define BLKIF_MINOR_EXT(dev) ((dev)&(~EXTENDED))
Stefano Stabellinic80a4202010-12-02 17:55:00 +0000138#define EMULATED_HD_DISK_MINOR_OFFSET (0)
139#define EMULATED_HD_DISK_NAME_OFFSET (EMULATED_HD_DISK_MINOR_OFFSET / 256)
Stefan Bader196cfe22011-07-14 15:30:22 +0200140#define EMULATED_SD_DISK_MINOR_OFFSET (0)
141#define EMULATED_SD_DISK_NAME_OFFSET (EMULATED_SD_DISK_MINOR_OFFSET / 256)
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700142
Chris Lalancette9246b5f2008-09-17 14:30:32 -0700143#define DEV_NAME "xvd" /* name in /dev */
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700144
145static int get_id_from_freelist(struct blkfront_info *info)
146{
147 unsigned long free = info->shadow_free;
Roel Kluinb9ed7252009-05-22 09:25:32 +0200148 BUG_ON(free >= BLK_RING_SIZE);
Konrad Rzeszutek Wilk97e36832011-10-12 12:12:36 -0400149 info->shadow_free = info->shadow[free].req.u.rw.id;
150 info->shadow[free].req.u.rw.id = 0x0fffffee; /* debug */
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700151 return free;
152}
153
Konrad Rzeszutek Wilk6878c322012-05-25 17:34:51 -0400154static int add_id_to_freelist(struct blkfront_info *info,
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700155 unsigned long id)
156{
Konrad Rzeszutek Wilk6878c322012-05-25 17:34:51 -0400157 if (info->shadow[id].req.u.rw.id != id)
158 return -EINVAL;
159 if (info->shadow[id].request == NULL)
160 return -EINVAL;
Konrad Rzeszutek Wilk97e36832011-10-12 12:12:36 -0400161 info->shadow[id].req.u.rw.id = info->shadow_free;
Jeremy Fitzhardingea945b982010-11-01 17:03:14 -0400162 info->shadow[id].request = NULL;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700163 info->shadow_free = id;
Konrad Rzeszutek Wilk6878c322012-05-25 17:34:51 -0400164 return 0;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700165}
166
Roger Pau Monne9c1e0502013-03-18 17:49:35 +0100167static int fill_grant_buffer(struct blkfront_info *info, int num)
168{
169 struct page *granted_page;
170 struct grant *gnt_list_entry, *n;
171 int i = 0;
172
173 while(i < num) {
174 gnt_list_entry = kzalloc(sizeof(struct grant), GFP_NOIO);
175 if (!gnt_list_entry)
176 goto out_of_memory;
177
178 granted_page = alloc_page(GFP_NOIO);
179 if (!granted_page) {
180 kfree(gnt_list_entry);
181 goto out_of_memory;
182 }
183
184 gnt_list_entry->pfn = page_to_pfn(granted_page);
185 gnt_list_entry->gref = GRANT_INVALID_REF;
186 list_add(&gnt_list_entry->node, &info->persistent_gnts);
187 i++;
188 }
189
190 return 0;
191
192out_of_memory:
193 list_for_each_entry_safe(gnt_list_entry, n,
194 &info->persistent_gnts, node) {
195 list_del(&gnt_list_entry->node);
196 __free_page(pfn_to_page(gnt_list_entry->pfn));
197 kfree(gnt_list_entry);
198 i--;
199 }
200 BUG_ON(i != 0);
201 return -ENOMEM;
202}
203
204static struct grant *get_grant(grant_ref_t *gref_head,
205 struct blkfront_info *info)
206{
207 struct grant *gnt_list_entry;
208 unsigned long buffer_mfn;
209
210 BUG_ON(list_empty(&info->persistent_gnts));
211 gnt_list_entry = list_first_entry(&info->persistent_gnts, struct grant,
212 node);
213 list_del(&gnt_list_entry->node);
214
215 if (gnt_list_entry->gref != GRANT_INVALID_REF) {
216 info->persistent_gnts_c--;
217 return gnt_list_entry;
218 }
219
220 /* Assign a gref to this page */
221 gnt_list_entry->gref = gnttab_claim_grant_reference(gref_head);
222 BUG_ON(gnt_list_entry->gref == -ENOSPC);
223 buffer_mfn = pfn_to_mfn(gnt_list_entry->pfn);
224 gnttab_grant_foreign_access_ref(gnt_list_entry->gref,
225 info->xbdev->otherend_id,
226 buffer_mfn, 0);
227 return gnt_list_entry;
228}
229
Konrad Rzeszutek Wilk6878c322012-05-25 17:34:51 -0400230static const char *op_name(int op)
231{
232 static const char *const names[] = {
233 [BLKIF_OP_READ] = "read",
234 [BLKIF_OP_WRITE] = "write",
235 [BLKIF_OP_WRITE_BARRIER] = "barrier",
236 [BLKIF_OP_FLUSH_DISKCACHE] = "flush",
237 [BLKIF_OP_DISCARD] = "discard" };
238
239 if (op < 0 || op >= ARRAY_SIZE(names))
240 return "unknown";
241
242 if (!names[op])
243 return "reserved";
244
245 return names[op];
246}
Jan Beulich0e345822010-08-07 18:28:55 +0200247static int xlbd_reserve_minors(unsigned int minor, unsigned int nr)
248{
249 unsigned int end = minor + nr;
250 int rc;
251
252 if (end > nr_minors) {
253 unsigned long *bitmap, *old;
254
Thomas Meyerf0941482011-11-29 22:08:00 +0100255 bitmap = kcalloc(BITS_TO_LONGS(end), sizeof(*bitmap),
Jan Beulich0e345822010-08-07 18:28:55 +0200256 GFP_KERNEL);
257 if (bitmap == NULL)
258 return -ENOMEM;
259
260 spin_lock(&minor_lock);
261 if (end > nr_minors) {
262 old = minors;
263 memcpy(bitmap, minors,
264 BITS_TO_LONGS(nr_minors) * sizeof(*bitmap));
265 minors = bitmap;
266 nr_minors = BITS_TO_LONGS(end) * BITS_PER_LONG;
267 } else
268 old = bitmap;
269 spin_unlock(&minor_lock);
270 kfree(old);
271 }
272
273 spin_lock(&minor_lock);
274 if (find_next_bit(minors, end, minor) >= end) {
Akinobu Mita34ae2e42012-01-21 00:15:26 +0900275 bitmap_set(minors, minor, nr);
Jan Beulich0e345822010-08-07 18:28:55 +0200276 rc = 0;
277 } else
278 rc = -EBUSY;
279 spin_unlock(&minor_lock);
280
281 return rc;
282}
283
284static void xlbd_release_minors(unsigned int minor, unsigned int nr)
285{
286 unsigned int end = minor + nr;
287
288 BUG_ON(end > nr_minors);
289 spin_lock(&minor_lock);
Akinobu Mita34ae2e42012-01-21 00:15:26 +0900290 bitmap_clear(minors, minor, nr);
Jan Beulich0e345822010-08-07 18:28:55 +0200291 spin_unlock(&minor_lock);
292}
293
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700294static void blkif_restart_queue_callback(void *arg)
295{
296 struct blkfront_info *info = (struct blkfront_info *)arg;
297 schedule_work(&info->work);
298}
299
Harvey Harrisonafe42d72008-04-29 00:59:47 -0700300static int blkif_getgeo(struct block_device *bd, struct hd_geometry *hg)
Ian Campbell597592d2008-02-21 13:03:45 -0800301{
302 /* We don't have real geometry info, but let's at least return
303 values consistent with the size of the device */
304 sector_t nsect = get_capacity(bd->bd_disk);
305 sector_t cylinders = nsect;
306
307 hg->heads = 0xff;
308 hg->sectors = 0x3f;
309 sector_div(cylinders, hg->heads * hg->sectors);
310 hg->cylinders = cylinders;
311 if ((sector_t)(hg->cylinders + 1) * hg->heads * hg->sectors < nsect)
312 hg->cylinders = 0xffff;
313 return 0;
314}
315
Al Viroa63c8482008-03-02 10:23:47 -0500316static int blkif_ioctl(struct block_device *bdev, fmode_t mode,
Adrian Bunk62aa0052008-08-04 11:59:05 +0200317 unsigned command, unsigned long argument)
Christian Limpach440a01a2008-06-17 10:47:08 +0200318{
Al Viroa63c8482008-03-02 10:23:47 -0500319 struct blkfront_info *info = bdev->bd_disk->private_data;
Christian Limpach440a01a2008-06-17 10:47:08 +0200320 int i;
321
322 dev_dbg(&info->xbdev->dev, "command: 0x%x, argument: 0x%lx\n",
323 command, (long)argument);
324
325 switch (command) {
326 case CDROMMULTISESSION:
327 dev_dbg(&info->xbdev->dev, "FIXME: support multisession CDs later\n");
328 for (i = 0; i < sizeof(struct cdrom_multisession); i++)
329 if (put_user(0, (char __user *)(argument + i)))
330 return -EFAULT;
331 return 0;
332
333 case CDROM_GET_CAPABILITY: {
334 struct gendisk *gd = info->gd;
335 if (gd->flags & GENHD_FL_CD)
336 return 0;
337 return -EINVAL;
338 }
339
340 default:
341 /*printk(KERN_ALERT "ioctl %08x not supported by Xen blkdev\n",
342 command);*/
343 return -EINVAL; /* same return as native Linux */
344 }
345
346 return 0;
347}
348
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700349/*
Jeremy Fitzhardingec64e38e2010-11-01 14:32:27 -0400350 * Generate a Xen blkfront IO request from a blk layer request. Reads
Konrad Rzeszutek Wilkedf6ef52011-05-03 12:01:11 -0400351 * and writes are handled as expected.
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700352 *
Jeremy Fitzhardingec64e38e2010-11-01 14:32:27 -0400353 * @req: a request struct
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700354 */
355static int blkif_queue_request(struct request *req)
356{
357 struct blkfront_info *info = req->rq_disk->private_data;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700358 struct blkif_request *ring_req;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700359 unsigned long id;
360 unsigned int fsect, lsect;
Jens Axboe9e973e62009-02-24 08:10:09 +0100361 int i, ref;
Roger Pau Monne0a8704a2012-10-24 18:58:45 +0200362
363 /*
364 * Used to store if we are able to queue the request by just using
365 * existing persistent grants, or if we have to get new grants,
366 * as there are not sufficiently many free.
367 */
368 bool new_persistent_gnts;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700369 grant_ref_t gref_head;
Roger Pau Monne0a8704a2012-10-24 18:58:45 +0200370 struct grant *gnt_list_entry = NULL;
Jens Axboe9e973e62009-02-24 08:10:09 +0100371 struct scatterlist *sg;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700372
373 if (unlikely(info->connected != BLKIF_STATE_CONNECTED))
374 return 1;
375
Roger Pau Monne0a8704a2012-10-24 18:58:45 +0200376 /* Check if we have enought grants to allocate a requests */
377 if (info->persistent_gnts_c < BLKIF_MAX_SEGMENTS_PER_REQUEST) {
378 new_persistent_gnts = 1;
379 if (gnttab_alloc_grant_references(
380 BLKIF_MAX_SEGMENTS_PER_REQUEST - info->persistent_gnts_c,
381 &gref_head) < 0) {
382 gnttab_request_free_callback(
383 &info->callback,
384 blkif_restart_queue_callback,
385 info,
386 BLKIF_MAX_SEGMENTS_PER_REQUEST);
387 return 1;
388 }
389 } else
390 new_persistent_gnts = 0;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700391
392 /* Fill out a communications ring structure. */
393 ring_req = RING_GET_REQUEST(&info->ring, info->ring.req_prod_pvt);
394 id = get_id_from_freelist(info);
Jeremy Fitzhardingea945b982010-11-01 17:03:14 -0400395 info->shadow[id].request = req;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700396
Konrad Rzeszutek Wilk97e36832011-10-12 12:12:36 -0400397 ring_req->u.rw.id = id;
Owen Smith51de6952010-12-22 15:05:00 +0000398 ring_req->u.rw.sector_number = (blkif_sector_t)blk_rq_pos(req);
Konrad Rzeszutek Wilk97e36832011-10-12 12:12:36 -0400399 ring_req->u.rw.handle = info->handle;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700400
401 ring_req->operation = rq_data_dir(req) ?
402 BLKIF_OP_WRITE : BLKIF_OP_READ;
Jeremy Fitzhardingebe2f8372010-11-02 10:38:33 -0400403
404 if (req->cmd_flags & (REQ_FLUSH | REQ_FUA)) {
405 /*
Konrad Rzeszutek Wilkedf6ef52011-05-03 12:01:11 -0400406 * Ideally we can do an unordered flush-to-disk. In case the
407 * backend onlysupports barriers, use that. A barrier request
Jeremy Fitzhardingebe2f8372010-11-02 10:38:33 -0400408 * a superset of FUA, so we can implement it the same
409 * way. (It's also a FLUSH+FUA, since it is
410 * guaranteed ordered WRT previous writes.)
411 */
Konrad Rzeszutek Wilkedf6ef52011-05-03 12:01:11 -0400412 ring_req->operation = info->flush_op;
Jeremy Fitzhardingebe2f8372010-11-02 10:38:33 -0400413 }
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700414
Konrad Rzeszutek Wilk5ea42982011-10-12 16:23:30 -0400415 if (unlikely(req->cmd_flags & (REQ_DISCARD | REQ_SECURE))) {
Li Dongyanged30bf32011-09-01 18:39:09 +0800416 /* id, sector_number and handle are set above. */
417 ring_req->operation = BLKIF_OP_DISCARD;
Li Dongyanged30bf32011-09-01 18:39:09 +0800418 ring_req->u.discard.nr_sectors = blk_rq_sectors(req);
Konrad Rzeszutek Wilk5ea42982011-10-12 16:23:30 -0400419 if ((req->cmd_flags & REQ_SECURE) && info->feature_secdiscard)
420 ring_req->u.discard.flag = BLKIF_DISCARD_SECURE;
421 else
422 ring_req->u.discard.flag = 0;
Li Dongyanged30bf32011-09-01 18:39:09 +0800423 } else {
Konrad Rzeszutek Wilk97e36832011-10-12 12:12:36 -0400424 ring_req->u.rw.nr_segments = blk_rq_map_sg(req->q, req,
425 info->sg);
426 BUG_ON(ring_req->u.rw.nr_segments >
427 BLKIF_MAX_SEGMENTS_PER_REQUEST);
Jens Axboe9e973e62009-02-24 08:10:09 +0100428
Konrad Rzeszutek Wilk97e36832011-10-12 12:12:36 -0400429 for_each_sg(info->sg, sg, ring_req->u.rw.nr_segments, i) {
Li Dongyanged30bf32011-09-01 18:39:09 +0800430 fsect = sg->offset >> 9;
431 lsect = fsect + (sg->length >> 9) - 1;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700432
Roger Pau Monne9c1e0502013-03-18 17:49:35 +0100433 gnt_list_entry = get_grant(&gref_head, info);
434 ref = gnt_list_entry->gref;
Roger Pau Monne0a8704a2012-10-24 18:58:45 +0200435
436 info->shadow[id].grants_used[i] = gnt_list_entry;
437
438 if (rq_data_dir(req)) {
439 char *bvec_data;
440 void *shared_data;
441
442 BUG_ON(sg->offset + sg->length > PAGE_SIZE);
443
444 shared_data = kmap_atomic(
445 pfn_to_page(gnt_list_entry->pfn));
446 bvec_data = kmap_atomic(sg_page(sg));
447
448 /*
449 * this does not wipe data stored outside the
450 * range sg->offset..sg->offset+sg->length.
451 * Therefore, blkback *could* see data from
452 * previous requests. This is OK as long as
453 * persistent grants are shared with just one
454 * domain. It may need refactoring if this
455 * changes
456 */
457 memcpy(shared_data + sg->offset,
458 bvec_data + sg->offset,
459 sg->length);
460
461 kunmap_atomic(bvec_data);
462 kunmap_atomic(shared_data);
463 }
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700464
Li Dongyanged30bf32011-09-01 18:39:09 +0800465 ring_req->u.rw.seg[i] =
466 (struct blkif_request_segment) {
467 .gref = ref,
468 .first_sect = fsect,
469 .last_sect = lsect };
470 }
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700471 }
472
473 info->ring.req_prod_pvt++;
474
475 /* Keep a private copy so we can reissue requests when recovering. */
476 info->shadow[id].req = *ring_req;
477
Roger Pau Monne0a8704a2012-10-24 18:58:45 +0200478 if (new_persistent_gnts)
479 gnttab_free_grant_references(gref_head);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700480
481 return 0;
482}
483
484
485static inline void flush_requests(struct blkfront_info *info)
486{
487 int notify;
488
489 RING_PUSH_REQUESTS_AND_CHECK_NOTIFY(&info->ring, notify);
490
491 if (notify)
492 notify_remote_via_irq(info->irq);
493}
494
495/*
496 * do_blkif_request
497 * read a block; request is in a request queue
498 */
Jens Axboe165125e2007-07-24 09:28:11 +0200499static void do_blkif_request(struct request_queue *rq)
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700500{
501 struct blkfront_info *info = NULL;
502 struct request *req;
503 int queued;
504
505 pr_debug("Entered do_blkif_request\n");
506
507 queued = 0;
508
Tejun Heo9934c8c2009-05-08 11:54:16 +0900509 while ((req = blk_peek_request(rq)) != NULL) {
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700510 info = req->rq_disk->private_data;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700511
512 if (RING_FULL(&info->ring))
513 goto wait;
514
Tejun Heo9934c8c2009-05-08 11:54:16 +0900515 blk_start_request(req);
Tejun Heo296b2f62009-05-08 11:54:15 +0900516
Konrad Rzeszutek Wilkd11e6152011-09-16 15:15:14 -0400517 if ((req->cmd_type != REQ_TYPE_FS) ||
518 ((req->cmd_flags & (REQ_FLUSH | REQ_FUA)) &&
519 !info->flush_op)) {
Tejun Heo296b2f62009-05-08 11:54:15 +0900520 __blk_end_request_all(req, -EIO);
521 continue;
522 }
523
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700524 pr_debug("do_blk_req %p: cmd %p, sec %lx, "
Tejun Heo83096eb2009-05-07 22:24:39 +0900525 "(%u/%u) buffer:%p [%s]\n",
526 req, req->cmd, (unsigned long)blk_rq_pos(req),
527 blk_rq_cur_sectors(req), blk_rq_sectors(req),
528 req->buffer, rq_data_dir(req) ? "write" : "read");
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700529
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700530 if (blkif_queue_request(req)) {
531 blk_requeue_request(rq, req);
532wait:
533 /* Avoid pointless unplugs. */
534 blk_stop_queue(rq);
535 break;
536 }
537
538 queued++;
539 }
540
541 if (queued != 0)
542 flush_requests(info);
543}
544
545static int xlvbd_init_blk_queue(struct gendisk *gd, u16 sector_size)
546{
Jens Axboe165125e2007-07-24 09:28:11 +0200547 struct request_queue *rq;
Li Dongyanged30bf32011-09-01 18:39:09 +0800548 struct blkfront_info *info = gd->private_data;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700549
Steven Noonan34678112012-02-17 12:04:44 -0800550 rq = blk_init_queue(do_blkif_request, &info->io_lock);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700551 if (rq == NULL)
552 return -1;
553
Fernando Luis Vázquez Cao66d352e2008-10-27 18:45:54 +0900554 queue_flag_set_unlocked(QUEUE_FLAG_VIRT, rq);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700555
Li Dongyanged30bf32011-09-01 18:39:09 +0800556 if (info->feature_discard) {
557 queue_flag_set_unlocked(QUEUE_FLAG_DISCARD, rq);
558 blk_queue_max_discard_sectors(rq, get_capacity(gd));
559 rq->limits.discard_granularity = info->discard_granularity;
560 rq->limits.discard_alignment = info->discard_alignment;
Konrad Rzeszutek Wilk5ea42982011-10-12 16:23:30 -0400561 if (info->feature_secdiscard)
562 queue_flag_set_unlocked(QUEUE_FLAG_SECDISCARD, rq);
Li Dongyanged30bf32011-09-01 18:39:09 +0800563 }
564
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700565 /* Hard sector size and max sectors impersonate the equiv. hardware. */
Martin K. Petersene1defc42009-05-22 17:17:49 -0400566 blk_queue_logical_block_size(rq, sector_size);
Martin K. Petersen086fa5f2010-02-26 00:20:38 -0500567 blk_queue_max_hw_sectors(rq, 512);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700568
569 /* Each segment in a request is up to an aligned page in size. */
570 blk_queue_segment_boundary(rq, PAGE_SIZE - 1);
571 blk_queue_max_segment_size(rq, PAGE_SIZE);
572
573 /* Ensure a merged request will fit in a single I/O ring slot. */
Martin K. Petersen8a783622010-02-26 00:20:39 -0500574 blk_queue_max_segments(rq, BLKIF_MAX_SEGMENTS_PER_REQUEST);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700575
576 /* Make sure buffer addresses are sector-aligned. */
577 blk_queue_dma_alignment(rq, 511);
578
Ian Campbell1c91fe12008-06-17 10:47:08 +0200579 /* Make sure we don't use bounce buffers. */
580 blk_queue_bounce_limit(rq, BLK_BOUNCE_ANY);
581
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700582 gd->queue = rq;
583
584 return 0;
585}
586
587
Tejun Heo4913efe2010-09-03 11:56:16 +0200588static void xlvbd_flush(struct blkfront_info *info)
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700589{
Tejun Heo4913efe2010-09-03 11:56:16 +0200590 blk_queue_flush(info->rq, info->feature_flush);
Roger Pau Monne0a8704a2012-10-24 18:58:45 +0200591 printk(KERN_INFO "blkfront: %s: %s: %s %s\n",
Tejun Heo4913efe2010-09-03 11:56:16 +0200592 info->gd->disk_name,
Konrad Rzeszutek Wilkedf6ef52011-05-03 12:01:11 -0400593 info->flush_op == BLKIF_OP_WRITE_BARRIER ?
594 "barrier" : (info->flush_op == BLKIF_OP_FLUSH_DISKCACHE ?
595 "flush diskcache" : "barrier or flush"),
Roger Pau Monne0a8704a2012-10-24 18:58:45 +0200596 info->feature_flush ? "enabled" : "disabled",
597 info->feature_persistent ? "using persistent grants" : "");
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700598}
599
Stefano Stabellinic80a4202010-12-02 17:55:00 +0000600static int xen_translate_vdev(int vdevice, int *minor, unsigned int *offset)
601{
602 int major;
603 major = BLKIF_MAJOR(vdevice);
604 *minor = BLKIF_MINOR(vdevice);
605 switch (major) {
606 case XEN_IDE0_MAJOR:
607 *offset = (*minor / 64) + EMULATED_HD_DISK_NAME_OFFSET;
608 *minor = ((*minor / 64) * PARTS_PER_DISK) +
609 EMULATED_HD_DISK_MINOR_OFFSET;
610 break;
611 case XEN_IDE1_MAJOR:
612 *offset = (*minor / 64) + 2 + EMULATED_HD_DISK_NAME_OFFSET;
613 *minor = (((*minor / 64) + 2) * PARTS_PER_DISK) +
614 EMULATED_HD_DISK_MINOR_OFFSET;
615 break;
616 case XEN_SCSI_DISK0_MAJOR:
617 *offset = (*minor / PARTS_PER_DISK) + EMULATED_SD_DISK_NAME_OFFSET;
618 *minor = *minor + EMULATED_SD_DISK_MINOR_OFFSET;
619 break;
620 case XEN_SCSI_DISK1_MAJOR:
621 case XEN_SCSI_DISK2_MAJOR:
622 case XEN_SCSI_DISK3_MAJOR:
623 case XEN_SCSI_DISK4_MAJOR:
624 case XEN_SCSI_DISK5_MAJOR:
625 case XEN_SCSI_DISK6_MAJOR:
626 case XEN_SCSI_DISK7_MAJOR:
627 *offset = (*minor / PARTS_PER_DISK) +
628 ((major - XEN_SCSI_DISK1_MAJOR + 1) * 16) +
629 EMULATED_SD_DISK_NAME_OFFSET;
630 *minor = *minor +
631 ((major - XEN_SCSI_DISK1_MAJOR + 1) * 16 * PARTS_PER_DISK) +
632 EMULATED_SD_DISK_MINOR_OFFSET;
633 break;
634 case XEN_SCSI_DISK8_MAJOR:
635 case XEN_SCSI_DISK9_MAJOR:
636 case XEN_SCSI_DISK10_MAJOR:
637 case XEN_SCSI_DISK11_MAJOR:
638 case XEN_SCSI_DISK12_MAJOR:
639 case XEN_SCSI_DISK13_MAJOR:
640 case XEN_SCSI_DISK14_MAJOR:
641 case XEN_SCSI_DISK15_MAJOR:
642 *offset = (*minor / PARTS_PER_DISK) +
643 ((major - XEN_SCSI_DISK8_MAJOR + 8) * 16) +
644 EMULATED_SD_DISK_NAME_OFFSET;
645 *minor = *minor +
646 ((major - XEN_SCSI_DISK8_MAJOR + 8) * 16 * PARTS_PER_DISK) +
647 EMULATED_SD_DISK_MINOR_OFFSET;
648 break;
649 case XENVBD_MAJOR:
650 *offset = *minor / PARTS_PER_DISK;
651 break;
652 default:
653 printk(KERN_WARNING "blkfront: your disk configuration is "
654 "incorrect, please use an xvd device instead\n");
655 return -ENODEV;
656 }
657 return 0;
658}
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700659
Jan Beuliche77c78c2012-04-05 16:37:22 +0100660static char *encode_disk_name(char *ptr, unsigned int n)
661{
662 if (n >= 26)
663 ptr = encode_disk_name(ptr, n / 26 - 1);
664 *ptr = 'a' + n % 26;
665 return ptr + 1;
666}
667
Chris Lalancette9246b5f2008-09-17 14:30:32 -0700668static int xlvbd_alloc_gendisk(blkif_sector_t capacity,
669 struct blkfront_info *info,
670 u16 vdisk_info, u16 sector_size)
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700671{
672 struct gendisk *gd;
673 int nr_minors = 1;
Stefano Stabellinic80a4202010-12-02 17:55:00 +0000674 int err;
Chris Lalancette9246b5f2008-09-17 14:30:32 -0700675 unsigned int offset;
676 int minor;
677 int nr_parts;
Jan Beuliche77c78c2012-04-05 16:37:22 +0100678 char *ptr;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700679
680 BUG_ON(info->gd != NULL);
681 BUG_ON(info->rq != NULL);
682
Chris Lalancette9246b5f2008-09-17 14:30:32 -0700683 if ((info->vdevice>>EXT_SHIFT) > 1) {
684 /* this is above the extended range; something is wrong */
685 printk(KERN_WARNING "blkfront: vdevice 0x%x is above the extended range; ignoring\n", info->vdevice);
686 return -ENODEV;
687 }
688
689 if (!VDEV_IS_EXTENDED(info->vdevice)) {
Stefano Stabellinic80a4202010-12-02 17:55:00 +0000690 err = xen_translate_vdev(info->vdevice, &minor, &offset);
691 if (err)
692 return err;
693 nr_parts = PARTS_PER_DISK;
Chris Lalancette9246b5f2008-09-17 14:30:32 -0700694 } else {
695 minor = BLKIF_MINOR_EXT(info->vdevice);
696 nr_parts = PARTS_PER_EXT_DISK;
Stefano Stabellinic80a4202010-12-02 17:55:00 +0000697 offset = minor / nr_parts;
Stefan Bader89153b52011-07-14 15:30:37 +0200698 if (xen_hvm_domain() && offset < EMULATED_HD_DISK_NAME_OFFSET + 4)
Stefano Stabellinic80a4202010-12-02 17:55:00 +0000699 printk(KERN_WARNING "blkfront: vdevice 0x%x might conflict with "
700 "emulated IDE disks,\n\t choose an xvd device name"
701 "from xvde on\n", info->vdevice);
Chris Lalancette9246b5f2008-09-17 14:30:32 -0700702 }
Jan Beuliche77c78c2012-04-05 16:37:22 +0100703 if (minor >> MINORBITS) {
704 pr_warn("blkfront: %#x's minor (%#x) out of range; ignoring\n",
705 info->vdevice, minor);
706 return -ENODEV;
707 }
Chris Lalancette9246b5f2008-09-17 14:30:32 -0700708
709 if ((minor % nr_parts) == 0)
710 nr_minors = nr_parts;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700711
Jan Beulich0e345822010-08-07 18:28:55 +0200712 err = xlbd_reserve_minors(minor, nr_minors);
713 if (err)
714 goto out;
715 err = -ENODEV;
716
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700717 gd = alloc_disk(nr_minors);
718 if (gd == NULL)
Jan Beulich0e345822010-08-07 18:28:55 +0200719 goto release;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700720
Jan Beuliche77c78c2012-04-05 16:37:22 +0100721 strcpy(gd->disk_name, DEV_NAME);
722 ptr = encode_disk_name(gd->disk_name + sizeof(DEV_NAME) - 1, offset);
723 BUG_ON(ptr >= gd->disk_name + DISK_NAME_LEN);
724 if (nr_minors > 1)
725 *ptr = 0;
726 else
727 snprintf(ptr, gd->disk_name + DISK_NAME_LEN - ptr,
728 "%d", minor & (nr_parts - 1));
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700729
730 gd->major = XENVBD_MAJOR;
731 gd->first_minor = minor;
732 gd->fops = &xlvbd_block_fops;
733 gd->private_data = info;
734 gd->driverfs_dev = &(info->xbdev->dev);
735 set_capacity(gd, capacity);
736
737 if (xlvbd_init_blk_queue(gd, sector_size)) {
738 del_gendisk(gd);
Jan Beulich0e345822010-08-07 18:28:55 +0200739 goto release;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700740 }
741
742 info->rq = gd->queue;
743 info->gd = gd;
744
Tejun Heo4913efe2010-09-03 11:56:16 +0200745 xlvbd_flush(info);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700746
747 if (vdisk_info & VDISK_READONLY)
748 set_disk_ro(gd, 1);
749
750 if (vdisk_info & VDISK_REMOVABLE)
751 gd->flags |= GENHD_FL_REMOVABLE;
752
753 if (vdisk_info & VDISK_CDROM)
754 gd->flags |= GENHD_FL_CD;
755
756 return 0;
757
Jan Beulich0e345822010-08-07 18:28:55 +0200758 release:
759 xlbd_release_minors(minor, nr_minors);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700760 out:
761 return err;
762}
763
Daniel Stoddena66b5ae2010-08-07 18:33:17 +0200764static void xlvbd_release_gendisk(struct blkfront_info *info)
765{
766 unsigned int minor, nr_minors;
767 unsigned long flags;
768
769 if (info->rq == NULL)
770 return;
771
Steven Noonan34678112012-02-17 12:04:44 -0800772 spin_lock_irqsave(&info->io_lock, flags);
Daniel Stoddena66b5ae2010-08-07 18:33:17 +0200773
774 /* No more blkif_request(). */
775 blk_stop_queue(info->rq);
776
777 /* No more gnttab callback work. */
778 gnttab_cancel_free_callback(&info->callback);
Steven Noonan34678112012-02-17 12:04:44 -0800779 spin_unlock_irqrestore(&info->io_lock, flags);
Daniel Stoddena66b5ae2010-08-07 18:33:17 +0200780
781 /* Flush gnttab callback work. Must be done with no locks held. */
Tejun Heo43829732012-08-20 14:51:24 -0700782 flush_work(&info->work);
Daniel Stoddena66b5ae2010-08-07 18:33:17 +0200783
784 del_gendisk(info->gd);
785
786 minor = info->gd->first_minor;
787 nr_minors = info->gd->minors;
788 xlbd_release_minors(minor, nr_minors);
789
790 blk_cleanup_queue(info->rq);
791 info->rq = NULL;
792
793 put_disk(info->gd);
794 info->gd = NULL;
795}
796
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700797static void kick_pending_request_queues(struct blkfront_info *info)
798{
799 if (!RING_FULL(&info->ring)) {
800 /* Re-enable calldowns. */
801 blk_start_queue(info->rq);
802 /* Kick things off immediately. */
803 do_blkif_request(info->rq);
804 }
805}
806
807static void blkif_restart_queue(struct work_struct *work)
808{
809 struct blkfront_info *info = container_of(work, struct blkfront_info, work);
810
Steven Noonan34678112012-02-17 12:04:44 -0800811 spin_lock_irq(&info->io_lock);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700812 if (info->connected == BLKIF_STATE_CONNECTED)
813 kick_pending_request_queues(info);
Steven Noonan34678112012-02-17 12:04:44 -0800814 spin_unlock_irq(&info->io_lock);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700815}
816
817static void blkif_free(struct blkfront_info *info, int suspend)
818{
Roger Pau Monne155b7ed2013-03-18 17:49:34 +0100819 struct grant *persistent_gnt;
820 struct grant *n;
Roger Pau Monne0a8704a2012-10-24 18:58:45 +0200821
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700822 /* Prevent new requests being issued until we fix things up. */
Steven Noonan34678112012-02-17 12:04:44 -0800823 spin_lock_irq(&info->io_lock);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700824 info->connected = suspend ?
825 BLKIF_STATE_SUSPENDED : BLKIF_STATE_DISCONNECTED;
826 /* No more blkif_request(). */
827 if (info->rq)
828 blk_stop_queue(info->rq);
Roger Pau Monne0a8704a2012-10-24 18:58:45 +0200829
830 /* Remove all persistent grants */
Roger Pau Monne9c1e0502013-03-18 17:49:35 +0100831 if (!list_empty(&info->persistent_gnts)) {
Roger Pau Monne155b7ed2013-03-18 17:49:34 +0100832 list_for_each_entry_safe(persistent_gnt, n,
833 &info->persistent_gnts, node) {
834 list_del(&persistent_gnt->node);
Roger Pau Monne9c1e0502013-03-18 17:49:35 +0100835 if (persistent_gnt->gref != GRANT_INVALID_REF) {
836 gnttab_end_foreign_access(persistent_gnt->gref,
837 0, 0UL);
838 info->persistent_gnts_c--;
839 }
Roger Pau Monne07c540a2012-11-16 19:26:47 +0100840 __free_page(pfn_to_page(persistent_gnt->pfn));
Roger Pau Monne155b7ed2013-03-18 17:49:34 +0100841 kfree(persistent_gnt);
Roger Pau Monne0a8704a2012-10-24 18:58:45 +0200842 }
Roger Pau Monne0a8704a2012-10-24 18:58:45 +0200843 }
Roger Pau Monne9c1e0502013-03-18 17:49:35 +0100844 BUG_ON(info->persistent_gnts_c != 0);
Roger Pau Monne0a8704a2012-10-24 18:58:45 +0200845
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700846 /* No more gnttab callback work. */
847 gnttab_cancel_free_callback(&info->callback);
Steven Noonan34678112012-02-17 12:04:44 -0800848 spin_unlock_irq(&info->io_lock);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700849
850 /* Flush gnttab callback work. Must be done with no locks held. */
Tejun Heo43829732012-08-20 14:51:24 -0700851 flush_work(&info->work);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700852
853 /* Free resources associated with old device channel. */
854 if (info->ring_ref != GRANT_INVALID_REF) {
855 gnttab_end_foreign_access(info->ring_ref, 0,
856 (unsigned long)info->ring.sring);
857 info->ring_ref = GRANT_INVALID_REF;
858 info->ring.sring = NULL;
859 }
860 if (info->irq)
861 unbind_from_irqhandler(info->irq, info);
862 info->evtchn = info->irq = 0;
863
864}
865
Roger Pau Monne0a8704a2012-10-24 18:58:45 +0200866static void blkif_completion(struct blk_shadow *s, struct blkfront_info *info,
867 struct blkif_response *bret)
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700868{
Roger Pau Monned62f6912012-12-07 19:00:31 +0100869 int i = 0;
Roger Pau Monne0a8704a2012-10-24 18:58:45 +0200870 struct bio_vec *bvec;
871 struct req_iterator iter;
872 unsigned long flags;
873 char *bvec_data;
874 void *shared_data;
875 unsigned int offset = 0;
876
877 if (bret->operation == BLKIF_OP_READ) {
878 /*
879 * Copy the data received from the backend into the bvec.
880 * Since bv_offset can be different than 0, and bv_len different
881 * than PAGE_SIZE, we have to keep track of the current offset,
882 * to be sure we are copying the data from the right shared page.
883 */
884 rq_for_each_segment(bvec, s->request, iter) {
885 BUG_ON((bvec->bv_offset + bvec->bv_len) > PAGE_SIZE);
Roger Pau Monned62f6912012-12-07 19:00:31 +0100886 if (bvec->bv_offset < offset)
887 i++;
Roger Pau Monnecb5bd4d2012-11-02 16:43:04 +0100888 BUG_ON(i >= s->req.u.rw.nr_segments);
Roger Pau Monne0a8704a2012-10-24 18:58:45 +0200889 shared_data = kmap_atomic(
890 pfn_to_page(s->grants_used[i]->pfn));
891 bvec_data = bvec_kmap_irq(bvec, &flags);
892 memcpy(bvec_data, shared_data + bvec->bv_offset,
893 bvec->bv_len);
894 bvec_kunmap_irq(bvec_data, &flags);
895 kunmap_atomic(shared_data);
Roger Pau Monned62f6912012-12-07 19:00:31 +0100896 offset = bvec->bv_offset + bvec->bv_len;
Roger Pau Monne0a8704a2012-10-24 18:58:45 +0200897 }
898 }
899 /* Add the persistent grant into the list of free grants */
900 for (i = 0; i < s->req.u.rw.nr_segments; i++) {
Roger Pau Monne155b7ed2013-03-18 17:49:34 +0100901 list_add(&s->grants_used[i]->node, &info->persistent_gnts);
Roger Pau Monne0a8704a2012-10-24 18:58:45 +0200902 info->persistent_gnts_c++;
903 }
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700904}
905
906static irqreturn_t blkif_interrupt(int irq, void *dev_id)
907{
908 struct request *req;
909 struct blkif_response *bret;
910 RING_IDX i, rp;
911 unsigned long flags;
912 struct blkfront_info *info = (struct blkfront_info *)dev_id;
Kiyoshi Uedaf530f0362007-12-11 17:47:36 -0500913 int error;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700914
Steven Noonan34678112012-02-17 12:04:44 -0800915 spin_lock_irqsave(&info->io_lock, flags);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700916
917 if (unlikely(info->connected != BLKIF_STATE_CONNECTED)) {
Steven Noonan34678112012-02-17 12:04:44 -0800918 spin_unlock_irqrestore(&info->io_lock, flags);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700919 return IRQ_HANDLED;
920 }
921
922 again:
923 rp = info->ring.sring->rsp_prod;
924 rmb(); /* Ensure we see queued responses up to 'rp'. */
925
926 for (i = info->ring.rsp_cons; i != rp; i++) {
927 unsigned long id;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700928
929 bret = RING_GET_RESPONSE(&info->ring, i);
930 id = bret->id;
Konrad Rzeszutek Wilk6878c322012-05-25 17:34:51 -0400931 /*
932 * The backend has messed up and given us an id that we would
933 * never have given to it (we stamp it up to BLK_RING_SIZE -
934 * look in get_id_from_freelist.
935 */
936 if (id >= BLK_RING_SIZE) {
937 WARN(1, "%s: response to %s has incorrect id (%ld)\n",
938 info->gd->disk_name, op_name(bret->operation), id);
939 /* We can't safely get the 'struct request' as
940 * the id is busted. */
941 continue;
942 }
Jeremy Fitzhardingea945b982010-11-01 17:03:14 -0400943 req = info->shadow[id].request;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700944
Konrad Rzeszutek Wilk5ea42982011-10-12 16:23:30 -0400945 if (bret->operation != BLKIF_OP_DISCARD)
Roger Pau Monne0a8704a2012-10-24 18:58:45 +0200946 blkif_completion(&info->shadow[id], info, bret);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700947
Konrad Rzeszutek Wilk6878c322012-05-25 17:34:51 -0400948 if (add_id_to_freelist(info, id)) {
949 WARN(1, "%s: response to %s (id %ld) couldn't be recycled!\n",
950 info->gd->disk_name, op_name(bret->operation), id);
951 continue;
952 }
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700953
Kiyoshi Uedaf530f0362007-12-11 17:47:36 -0500954 error = (bret->status == BLKIF_RSP_OKAY) ? 0 : -EIO;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700955 switch (bret->operation) {
Li Dongyanged30bf32011-09-01 18:39:09 +0800956 case BLKIF_OP_DISCARD:
957 if (unlikely(bret->status == BLKIF_RSP_EOPNOTSUPP)) {
958 struct request_queue *rq = info->rq;
Konrad Rzeszutek Wilk6878c322012-05-25 17:34:51 -0400959 printk(KERN_WARNING "blkfront: %s: %s op failed\n",
960 info->gd->disk_name, op_name(bret->operation));
Li Dongyanged30bf32011-09-01 18:39:09 +0800961 error = -EOPNOTSUPP;
962 info->feature_discard = 0;
Konrad Rzeszutek Wilk5ea42982011-10-12 16:23:30 -0400963 info->feature_secdiscard = 0;
Li Dongyanged30bf32011-09-01 18:39:09 +0800964 queue_flag_clear(QUEUE_FLAG_DISCARD, rq);
Konrad Rzeszutek Wilk5ea42982011-10-12 16:23:30 -0400965 queue_flag_clear(QUEUE_FLAG_SECDISCARD, rq);
Li Dongyanged30bf32011-09-01 18:39:09 +0800966 }
967 __blk_end_request_all(req, error);
968 break;
Konrad Rzeszutek Wilkedf6ef52011-05-03 12:01:11 -0400969 case BLKIF_OP_FLUSH_DISKCACHE:
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700970 case BLKIF_OP_WRITE_BARRIER:
971 if (unlikely(bret->status == BLKIF_RSP_EOPNOTSUPP)) {
Konrad Rzeszutek Wilk6878c322012-05-25 17:34:51 -0400972 printk(KERN_WARNING "blkfront: %s: %s op failed\n",
973 info->gd->disk_name, op_name(bret->operation));
Kiyoshi Uedaf530f0362007-12-11 17:47:36 -0500974 error = -EOPNOTSUPP;
Jeremy Fitzhardingedcb8bae2010-11-02 11:55:58 -0400975 }
976 if (unlikely(bret->status == BLKIF_RSP_ERROR &&
Konrad Rzeszutek Wilk97e36832011-10-12 12:12:36 -0400977 info->shadow[id].req.u.rw.nr_segments == 0)) {
Konrad Rzeszutek Wilk6878c322012-05-25 17:34:51 -0400978 printk(KERN_WARNING "blkfront: %s: empty %s op failed\n",
979 info->gd->disk_name, op_name(bret->operation));
Jeremy Fitzhardingedcb8bae2010-11-02 11:55:58 -0400980 error = -EOPNOTSUPP;
981 }
982 if (unlikely(error)) {
983 if (error == -EOPNOTSUPP)
984 error = 0;
Tejun Heo4913efe2010-09-03 11:56:16 +0200985 info->feature_flush = 0;
Konrad Rzeszutek Wilkedf6ef52011-05-03 12:01:11 -0400986 info->flush_op = 0;
Tejun Heo4913efe2010-09-03 11:56:16 +0200987 xlvbd_flush(info);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700988 }
989 /* fall through */
990 case BLKIF_OP_READ:
991 case BLKIF_OP_WRITE:
992 if (unlikely(bret->status != BLKIF_RSP_OKAY))
993 dev_dbg(&info->xbdev->dev, "Bad return from blkdev data "
994 "request: %x\n", bret->status);
995
Tejun Heo40cbbb72009-04-23 11:05:19 +0900996 __blk_end_request_all(req, error);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700997 break;
998 default:
999 BUG();
1000 }
1001 }
1002
1003 info->ring.rsp_cons = i;
1004
1005 if (i != info->ring.req_prod_pvt) {
1006 int more_to_do;
1007 RING_FINAL_CHECK_FOR_RESPONSES(&info->ring, more_to_do);
1008 if (more_to_do)
1009 goto again;
1010 } else
1011 info->ring.sring->rsp_event = i + 1;
1012
1013 kick_pending_request_queues(info);
1014
Steven Noonan34678112012-02-17 12:04:44 -08001015 spin_unlock_irqrestore(&info->io_lock, flags);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001016
1017 return IRQ_HANDLED;
1018}
1019
1020
1021static int setup_blkring(struct xenbus_device *dev,
1022 struct blkfront_info *info)
1023{
1024 struct blkif_sring *sring;
1025 int err;
1026
1027 info->ring_ref = GRANT_INVALID_REF;
1028
Ian Campbella144ff02008-06-17 10:47:08 +02001029 sring = (struct blkif_sring *)__get_free_page(GFP_NOIO | __GFP_HIGH);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001030 if (!sring) {
1031 xenbus_dev_fatal(dev, -ENOMEM, "allocating shared ring");
1032 return -ENOMEM;
1033 }
1034 SHARED_RING_INIT(sring);
1035 FRONT_RING_INIT(&info->ring, sring, PAGE_SIZE);
1036
Jens Axboe9e973e62009-02-24 08:10:09 +01001037 sg_init_table(info->sg, BLKIF_MAX_SEGMENTS_PER_REQUEST);
1038
Roger Pau Monne9c1e0502013-03-18 17:49:35 +01001039 /* Allocate memory for grants */
1040 err = fill_grant_buffer(info, BLK_RING_SIZE *
1041 BLKIF_MAX_SEGMENTS_PER_REQUEST);
1042 if (err)
1043 goto fail;
1044
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001045 err = xenbus_grant_ring(dev, virt_to_mfn(info->ring.sring));
1046 if (err < 0) {
1047 free_page((unsigned long)sring);
1048 info->ring.sring = NULL;
1049 goto fail;
1050 }
1051 info->ring_ref = err;
1052
1053 err = xenbus_alloc_evtchn(dev, &info->evtchn);
1054 if (err)
1055 goto fail;
1056
Theodore Ts'o89c30f12012-07-17 13:46:19 -04001057 err = bind_evtchn_to_irqhandler(info->evtchn, blkif_interrupt, 0,
1058 "blkif", info);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001059 if (err <= 0) {
1060 xenbus_dev_fatal(dev, err,
1061 "bind_evtchn_to_irqhandler failed");
1062 goto fail;
1063 }
1064 info->irq = err;
1065
1066 return 0;
1067fail:
1068 blkif_free(info, 0);
1069 return err;
1070}
1071
1072
1073/* Common code used when first setting up, and when resuming. */
Ian Campbell203fd612009-12-04 15:33:54 +00001074static int talk_to_blkback(struct xenbus_device *dev,
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001075 struct blkfront_info *info)
1076{
1077 const char *message = NULL;
1078 struct xenbus_transaction xbt;
1079 int err;
1080
1081 /* Create shared ring, alloc event channel. */
1082 err = setup_blkring(dev, info);
1083 if (err)
1084 goto out;
1085
1086again:
1087 err = xenbus_transaction_start(&xbt);
1088 if (err) {
1089 xenbus_dev_fatal(dev, err, "starting transaction");
1090 goto destroy_blkring;
1091 }
1092
1093 err = xenbus_printf(xbt, dev->nodename,
1094 "ring-ref", "%u", info->ring_ref);
1095 if (err) {
1096 message = "writing ring-ref";
1097 goto abort_transaction;
1098 }
1099 err = xenbus_printf(xbt, dev->nodename,
1100 "event-channel", "%u", info->evtchn);
1101 if (err) {
1102 message = "writing event-channel";
1103 goto abort_transaction;
1104 }
Markus Armbruster3e334232008-04-02 10:54:02 -07001105 err = xenbus_printf(xbt, dev->nodename, "protocol", "%s",
1106 XEN_IO_PROTO_ABI_NATIVE);
1107 if (err) {
1108 message = "writing protocol";
1109 goto abort_transaction;
1110 }
Roger Pau Monne0a8704a2012-10-24 18:58:45 +02001111 err = xenbus_printf(xbt, dev->nodename,
Roger Pau Monnecb5bd4d2012-11-02 16:43:04 +01001112 "feature-persistent", "%u", 1);
Roger Pau Monne0a8704a2012-10-24 18:58:45 +02001113 if (err)
1114 dev_warn(&dev->dev,
1115 "writing persistent grants feature to xenbus");
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001116
1117 err = xenbus_transaction_end(xbt, 0);
1118 if (err) {
1119 if (err == -EAGAIN)
1120 goto again;
1121 xenbus_dev_fatal(dev, err, "completing transaction");
1122 goto destroy_blkring;
1123 }
1124
1125 xenbus_switch_state(dev, XenbusStateInitialised);
1126
1127 return 0;
1128
1129 abort_transaction:
1130 xenbus_transaction_end(xbt, 1);
1131 if (message)
1132 xenbus_dev_fatal(dev, err, "%s", message);
1133 destroy_blkring:
1134 blkif_free(info, 0);
1135 out:
1136 return err;
1137}
1138
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001139/**
1140 * Entry point to this code when a new device is created. Allocate the basic
1141 * structures and the ring buffer for communication with the backend, and
1142 * inform the backend of the appropriate details for those. Switch to
1143 * Initialised state.
1144 */
1145static int blkfront_probe(struct xenbus_device *dev,
1146 const struct xenbus_device_id *id)
1147{
1148 int err, vdevice, i;
1149 struct blkfront_info *info;
1150
1151 /* FIXME: Use dynamic device id if this is not set. */
1152 err = xenbus_scanf(XBT_NIL, dev->nodename,
1153 "virtual-device", "%i", &vdevice);
1154 if (err != 1) {
Chris Lalancette9246b5f2008-09-17 14:30:32 -07001155 /* go looking in the extended area instead */
1156 err = xenbus_scanf(XBT_NIL, dev->nodename, "virtual-device-ext",
1157 "%i", &vdevice);
1158 if (err != 1) {
1159 xenbus_dev_fatal(dev, err, "reading virtual-device");
1160 return err;
1161 }
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001162 }
1163
Stefano Stabellinib98a4092010-07-29 14:53:16 +01001164 if (xen_hvm_domain()) {
1165 char *type;
1166 int len;
1167 /* no unplug has been done: do not hook devices != xen vbds */
Ian Campbell1dc7ce92010-08-23 11:59:29 +01001168 if (xen_platform_pci_unplug & XEN_UNPLUG_UNNECESSARY) {
Stefano Stabellinib98a4092010-07-29 14:53:16 +01001169 int major;
Stefano Stabellinic1c54132010-05-14 12:44:30 +01001170
Stefano Stabellinib98a4092010-07-29 14:53:16 +01001171 if (!VDEV_IS_EXTENDED(vdevice))
1172 major = BLKIF_MAJOR(vdevice);
1173 else
1174 major = XENVBD_MAJOR;
Stefano Stabellinic1c54132010-05-14 12:44:30 +01001175
Stefano Stabellinib98a4092010-07-29 14:53:16 +01001176 if (major != XENVBD_MAJOR) {
1177 printk(KERN_INFO
1178 "%s: HVM does not support vbd %d as xen block device\n",
1179 __FUNCTION__, vdevice);
1180 return -ENODEV;
1181 }
1182 }
1183 /* do not create a PV cdrom device if we are an HVM guest */
1184 type = xenbus_read(XBT_NIL, dev->nodename, "device-type", &len);
1185 if (IS_ERR(type))
1186 return -ENODEV;
1187 if (strncmp(type, "cdrom", 5) == 0) {
1188 kfree(type);
Stefano Stabellinic1c54132010-05-14 12:44:30 +01001189 return -ENODEV;
1190 }
Stefano Stabellinib98a4092010-07-29 14:53:16 +01001191 kfree(type);
Stefano Stabellinic1c54132010-05-14 12:44:30 +01001192 }
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001193 info = kzalloc(sizeof(*info), GFP_KERNEL);
1194 if (!info) {
1195 xenbus_dev_fatal(dev, -ENOMEM, "allocating info structure");
1196 return -ENOMEM;
1197 }
1198
Daniel Stoddenb70f5fa2010-04-30 22:01:19 +00001199 mutex_init(&info->mutex);
Steven Noonan34678112012-02-17 12:04:44 -08001200 spin_lock_init(&info->io_lock);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001201 info->xbdev = dev;
1202 info->vdevice = vdevice;
Roger Pau Monne155b7ed2013-03-18 17:49:34 +01001203 INIT_LIST_HEAD(&info->persistent_gnts);
Roger Pau Monne0a8704a2012-10-24 18:58:45 +02001204 info->persistent_gnts_c = 0;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001205 info->connected = BLKIF_STATE_DISCONNECTED;
1206 INIT_WORK(&info->work, blkif_restart_queue);
1207
1208 for (i = 0; i < BLK_RING_SIZE; i++)
Konrad Rzeszutek Wilk97e36832011-10-12 12:12:36 -04001209 info->shadow[i].req.u.rw.id = i+1;
1210 info->shadow[BLK_RING_SIZE-1].req.u.rw.id = 0x0fffffff;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001211
1212 /* Front end dir is a number, which is used as the id. */
1213 info->handle = simple_strtoul(strrchr(dev->nodename, '/')+1, NULL, 0);
Greg Kroah-Hartmana1b4b122009-04-30 14:43:31 -07001214 dev_set_drvdata(&dev->dev, info);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001215
Ian Campbell203fd612009-12-04 15:33:54 +00001216 err = talk_to_blkback(dev, info);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001217 if (err) {
1218 kfree(info);
Greg Kroah-Hartmana1b4b122009-04-30 14:43:31 -07001219 dev_set_drvdata(&dev->dev, NULL);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001220 return err;
1221 }
1222
1223 return 0;
1224}
1225
1226
1227static int blkif_recover(struct blkfront_info *info)
1228{
1229 int i;
1230 struct blkif_request *req;
1231 struct blk_shadow *copy;
1232 int j;
1233
1234 /* Stage 1: Make a safe copy of the shadow state. */
Mihnea Dobrescu-Balaur29d0b212013-03-11 13:23:36 +02001235 copy = kmemdup(info->shadow, sizeof(info->shadow),
Ian Campbella144ff02008-06-17 10:47:08 +02001236 GFP_NOIO | __GFP_REPEAT | __GFP_HIGH);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001237 if (!copy)
1238 return -ENOMEM;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001239
1240 /* Stage 2: Set up free list. */
1241 memset(&info->shadow, 0, sizeof(info->shadow));
1242 for (i = 0; i < BLK_RING_SIZE; i++)
Konrad Rzeszutek Wilk97e36832011-10-12 12:12:36 -04001243 info->shadow[i].req.u.rw.id = i+1;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001244 info->shadow_free = info->ring.req_prod_pvt;
Konrad Rzeszutek Wilk97e36832011-10-12 12:12:36 -04001245 info->shadow[BLK_RING_SIZE-1].req.u.rw.id = 0x0fffffff;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001246
1247 /* Stage 3: Find pending requests and requeue them. */
1248 for (i = 0; i < BLK_RING_SIZE; i++) {
1249 /* Not in use? */
Jeremy Fitzhardingea945b982010-11-01 17:03:14 -04001250 if (!copy[i].request)
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001251 continue;
1252
1253 /* Grab a request slot and copy shadow state into it. */
1254 req = RING_GET_REQUEST(&info->ring, info->ring.req_prod_pvt);
1255 *req = copy[i].req;
1256
1257 /* We get a new request id, and must reset the shadow state. */
Konrad Rzeszutek Wilk97e36832011-10-12 12:12:36 -04001258 req->u.rw.id = get_id_from_freelist(info);
1259 memcpy(&info->shadow[req->u.rw.id], &copy[i], sizeof(copy[i]));
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001260
Konrad Rzeszutek Wilk5ea42982011-10-12 16:23:30 -04001261 if (req->operation != BLKIF_OP_DISCARD) {
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001262 /* Rewrite any grant references invalidated by susp/resume. */
Konrad Rzeszutek Wilk5ea42982011-10-12 16:23:30 -04001263 for (j = 0; j < req->u.rw.nr_segments; j++)
1264 gnttab_grant_foreign_access_ref(
1265 req->u.rw.seg[j].gref,
1266 info->xbdev->otherend_id,
Roger Pau Monneb1173e32013-03-18 17:49:36 +01001267 pfn_to_mfn(copy[i].grants_used[j]->pfn),
Roger Pau Monne0a8704a2012-10-24 18:58:45 +02001268 0);
Konrad Rzeszutek Wilk5ea42982011-10-12 16:23:30 -04001269 }
Konrad Rzeszutek Wilk97e36832011-10-12 12:12:36 -04001270 info->shadow[req->u.rw.id].req = *req;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001271
1272 info->ring.req_prod_pvt++;
1273 }
1274
1275 kfree(copy);
1276
1277 xenbus_switch_state(info->xbdev, XenbusStateConnected);
1278
Steven Noonan34678112012-02-17 12:04:44 -08001279 spin_lock_irq(&info->io_lock);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001280
1281 /* Now safe for us to use the shared ring */
1282 info->connected = BLKIF_STATE_CONNECTED;
1283
1284 /* Send off requeued requests */
1285 flush_requests(info);
1286
1287 /* Kick any other new requests queued since we resumed */
1288 kick_pending_request_queues(info);
1289
Steven Noonan34678112012-02-17 12:04:44 -08001290 spin_unlock_irq(&info->io_lock);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001291
1292 return 0;
1293}
1294
1295/**
1296 * We are reconnecting to the backend, due to a suspend/resume, or a backend
1297 * driver restart. We tear down our blkif structure and recreate it, but
1298 * leave the device-layer structures intact so that this is transparent to the
1299 * rest of the kernel.
1300 */
1301static int blkfront_resume(struct xenbus_device *dev)
1302{
Greg Kroah-Hartmana1b4b122009-04-30 14:43:31 -07001303 struct blkfront_info *info = dev_get_drvdata(&dev->dev);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001304 int err;
1305
1306 dev_dbg(&dev->dev, "blkfront_resume: %s\n", dev->nodename);
1307
1308 blkif_free(info, info->connected == BLKIF_STATE_CONNECTED);
1309
Ian Campbell203fd612009-12-04 15:33:54 +00001310 err = talk_to_blkback(dev, info);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001311 if (info->connected == BLKIF_STATE_SUSPENDED && !err)
1312 err = blkif_recover(info);
1313
1314 return err;
1315}
1316
Daniel Stoddenb70f5fa2010-04-30 22:01:19 +00001317static void
1318blkfront_closing(struct blkfront_info *info)
1319{
1320 struct xenbus_device *xbdev = info->xbdev;
1321 struct block_device *bdev = NULL;
1322
1323 mutex_lock(&info->mutex);
1324
1325 if (xbdev->state == XenbusStateClosing) {
1326 mutex_unlock(&info->mutex);
1327 return;
1328 }
1329
1330 if (info->gd)
1331 bdev = bdget_disk(info->gd, 0);
1332
1333 mutex_unlock(&info->mutex);
1334
1335 if (!bdev) {
1336 xenbus_frontend_closed(xbdev);
1337 return;
1338 }
1339
1340 mutex_lock(&bdev->bd_mutex);
1341
Daniel Stodden7b32d102010-04-30 22:01:23 +00001342 if (bdev->bd_openers) {
Daniel Stoddenb70f5fa2010-04-30 22:01:19 +00001343 xenbus_dev_error(xbdev, -EBUSY,
1344 "Device in use; refusing to close");
1345 xenbus_switch_state(xbdev, XenbusStateClosing);
1346 } else {
1347 xlvbd_release_gendisk(info);
1348 xenbus_frontend_closed(xbdev);
1349 }
1350
1351 mutex_unlock(&bdev->bd_mutex);
1352 bdput(bdev);
1353}
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001354
Li Dongyanged30bf32011-09-01 18:39:09 +08001355static void blkfront_setup_discard(struct blkfront_info *info)
1356{
1357 int err;
1358 char *type;
1359 unsigned int discard_granularity;
1360 unsigned int discard_alignment;
Konrad Rzeszutek Wilk5ea42982011-10-12 16:23:30 -04001361 unsigned int discard_secure;
Li Dongyanged30bf32011-09-01 18:39:09 +08001362
1363 type = xenbus_read(XBT_NIL, info->xbdev->otherend, "type", NULL);
1364 if (IS_ERR(type))
1365 return;
1366
Konrad Rzeszutek Wilk5ea42982011-10-12 16:23:30 -04001367 info->feature_secdiscard = 0;
Li Dongyanged30bf32011-09-01 18:39:09 +08001368 if (strncmp(type, "phy", 3) == 0) {
1369 err = xenbus_gather(XBT_NIL, info->xbdev->otherend,
1370 "discard-granularity", "%u", &discard_granularity,
1371 "discard-alignment", "%u", &discard_alignment,
1372 NULL);
1373 if (!err) {
1374 info->feature_discard = 1;
1375 info->discard_granularity = discard_granularity;
1376 info->discard_alignment = discard_alignment;
1377 }
Konrad Rzeszutek Wilk5ea42982011-10-12 16:23:30 -04001378 err = xenbus_gather(XBT_NIL, info->xbdev->otherend,
1379 "discard-secure", "%d", &discard_secure,
1380 NULL);
1381 if (!err)
1382 info->feature_secdiscard = discard_secure;
1383
Li Dongyanged30bf32011-09-01 18:39:09 +08001384 } else if (strncmp(type, "file", 4) == 0)
1385 info->feature_discard = 1;
1386
1387 kfree(type);
1388}
1389
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001390/*
1391 * Invoked when the backend is finally 'ready' (and has told produced
1392 * the details about the physical device - #sectors, size, etc).
1393 */
1394static void blkfront_connect(struct blkfront_info *info)
1395{
1396 unsigned long long sectors;
1397 unsigned long sector_size;
1398 unsigned int binfo;
1399 int err;
Roger Pau Monne0a8704a2012-10-24 18:58:45 +02001400 int barrier, flush, discard, persistent;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001401
K. Y. Srinivasan1fa73be2010-03-11 13:42:26 -08001402 switch (info->connected) {
1403 case BLKIF_STATE_CONNECTED:
1404 /*
1405 * Potentially, the back-end may be signalling
1406 * a capacity change; update the capacity.
1407 */
1408 err = xenbus_scanf(XBT_NIL, info->xbdev->otherend,
1409 "sectors", "%Lu", &sectors);
1410 if (XENBUS_EXIST_ERR(err))
1411 return;
1412 printk(KERN_INFO "Setting capacity to %Lu\n",
1413 sectors);
1414 set_capacity(info->gd, sectors);
K. Y. Srinivasan2def1412010-03-18 15:00:54 -07001415 revalidate_disk(info->gd);
K. Y. Srinivasan1fa73be2010-03-11 13:42:26 -08001416
1417 /* fall through */
1418 case BLKIF_STATE_SUSPENDED:
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001419 return;
1420
Jeremy Fitzhardingeb4dddb42010-03-11 15:10:40 -08001421 default:
1422 break;
K. Y. Srinivasan1fa73be2010-03-11 13:42:26 -08001423 }
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001424
1425 dev_dbg(&info->xbdev->dev, "%s:%s.\n",
1426 __func__, info->xbdev->otherend);
1427
1428 err = xenbus_gather(XBT_NIL, info->xbdev->otherend,
1429 "sectors", "%llu", &sectors,
1430 "info", "%u", &binfo,
1431 "sector-size", "%lu", &sector_size,
1432 NULL);
1433 if (err) {
1434 xenbus_dev_fatal(info->xbdev, err,
1435 "reading backend fields at %s",
1436 info->xbdev->otherend);
1437 return;
1438 }
1439
Konrad Rzeszutek Wilkedf6ef52011-05-03 12:01:11 -04001440 info->feature_flush = 0;
1441 info->flush_op = 0;
1442
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001443 err = xenbus_gather(XBT_NIL, info->xbdev->otherend,
Marek Marczykowski4352b472011-05-03 12:04:52 -04001444 "feature-barrier", "%d", &barrier,
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001445 NULL);
Jeremy Fitzhardinge7901d142010-07-28 10:49:29 -07001446
1447 /*
1448 * If there's no "feature-barrier" defined, then it means
1449 * we're dealing with a very old backend which writes
Tejun Heo4913efe2010-09-03 11:56:16 +02001450 * synchronously; nothing to do.
Jeremy Fitzhardinge7901d142010-07-28 10:49:29 -07001451 *
Tejun Heo6958f142010-09-03 11:56:16 +02001452 * If there are barriers, then we use flush.
Jeremy Fitzhardinge7901d142010-07-28 10:49:29 -07001453 */
Konrad Rzeszutek Wilkedf6ef52011-05-03 12:01:11 -04001454 if (!err && barrier) {
Jeremy Fitzhardingebe2f8372010-11-02 10:38:33 -04001455 info->feature_flush = REQ_FLUSH | REQ_FUA;
Konrad Rzeszutek Wilkedf6ef52011-05-03 12:01:11 -04001456 info->flush_op = BLKIF_OP_WRITE_BARRIER;
1457 }
1458 /*
1459 * And if there is "feature-flush-cache" use that above
1460 * barriers.
1461 */
1462 err = xenbus_gather(XBT_NIL, info->xbdev->otherend,
1463 "feature-flush-cache", "%d", &flush,
1464 NULL);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001465
Konrad Rzeszutek Wilkedf6ef52011-05-03 12:01:11 -04001466 if (!err && flush) {
1467 info->feature_flush = REQ_FLUSH;
1468 info->flush_op = BLKIF_OP_FLUSH_DISKCACHE;
1469 }
Li Dongyanged30bf32011-09-01 18:39:09 +08001470
1471 err = xenbus_gather(XBT_NIL, info->xbdev->otherend,
1472 "feature-discard", "%d", &discard,
1473 NULL);
1474
1475 if (!err && discard)
1476 blkfront_setup_discard(info);
1477
Roger Pau Monne0a8704a2012-10-24 18:58:45 +02001478 err = xenbus_gather(XBT_NIL, info->xbdev->otherend,
1479 "feature-persistent", "%u", &persistent,
1480 NULL);
1481 if (err)
1482 info->feature_persistent = 0;
1483 else
1484 info->feature_persistent = persistent;
1485
Chris Lalancette9246b5f2008-09-17 14:30:32 -07001486 err = xlvbd_alloc_gendisk(sectors, info, binfo, sector_size);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001487 if (err) {
1488 xenbus_dev_fatal(info->xbdev, err, "xlvbd_add at %s",
1489 info->xbdev->otherend);
1490 return;
1491 }
1492
1493 xenbus_switch_state(info->xbdev, XenbusStateConnected);
1494
1495 /* Kick pending requests. */
Steven Noonan34678112012-02-17 12:04:44 -08001496 spin_lock_irq(&info->io_lock);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001497 info->connected = BLKIF_STATE_CONNECTED;
1498 kick_pending_request_queues(info);
Steven Noonan34678112012-02-17 12:04:44 -08001499 spin_unlock_irq(&info->io_lock);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001500
1501 add_disk(info->gd);
Christian Limpach1d78d702008-04-02 10:54:04 -07001502
1503 info->is_ready = 1;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001504}
1505
1506/**
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001507 * Callback received when the backend's state changes.
1508 */
Ian Campbell203fd612009-12-04 15:33:54 +00001509static void blkback_changed(struct xenbus_device *dev,
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001510 enum xenbus_state backend_state)
1511{
Greg Kroah-Hartmana1b4b122009-04-30 14:43:31 -07001512 struct blkfront_info *info = dev_get_drvdata(&dev->dev);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001513
Ian Campbell203fd612009-12-04 15:33:54 +00001514 dev_dbg(&dev->dev, "blkfront:blkback_changed to state %d.\n", backend_state);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001515
1516 switch (backend_state) {
1517 case XenbusStateInitialising:
1518 case XenbusStateInitWait:
1519 case XenbusStateInitialised:
Noboru Iwamatsub78c9512009-10-13 17:22:29 -04001520 case XenbusStateReconfiguring:
1521 case XenbusStateReconfigured:
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001522 case XenbusStateUnknown:
1523 case XenbusStateClosed:
1524 break;
1525
1526 case XenbusStateConnected:
1527 blkfront_connect(info);
1528 break;
1529
1530 case XenbusStateClosing:
Daniel Stoddenb70f5fa2010-04-30 22:01:19 +00001531 blkfront_closing(info);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001532 break;
1533 }
1534}
1535
Daniel Stoddenfa1bd352010-04-30 22:01:22 +00001536static int blkfront_remove(struct xenbus_device *xbdev)
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001537{
Daniel Stoddenfa1bd352010-04-30 22:01:22 +00001538 struct blkfront_info *info = dev_get_drvdata(&xbdev->dev);
1539 struct block_device *bdev = NULL;
1540 struct gendisk *disk;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001541
Daniel Stoddenfa1bd352010-04-30 22:01:22 +00001542 dev_dbg(&xbdev->dev, "%s removed", xbdev->nodename);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001543
1544 blkif_free(info, 0);
1545
Daniel Stoddenfa1bd352010-04-30 22:01:22 +00001546 mutex_lock(&info->mutex);
1547
1548 disk = info->gd;
1549 if (disk)
1550 bdev = bdget_disk(disk, 0);
1551
1552 info->xbdev = NULL;
1553 mutex_unlock(&info->mutex);
1554
1555 if (!bdev) {
Jan Beulich0e345822010-08-07 18:28:55 +02001556 kfree(info);
Daniel Stoddenfa1bd352010-04-30 22:01:22 +00001557 return 0;
1558 }
1559
1560 /*
1561 * The xbdev was removed before we reached the Closed
1562 * state. See if it's safe to remove the disk. If the bdev
1563 * isn't closed yet, we let release take care of it.
1564 */
1565
1566 mutex_lock(&bdev->bd_mutex);
1567 info = disk->private_data;
1568
Daniel Stoddend54142c2010-08-07 18:51:21 +02001569 dev_warn(disk_to_dev(disk),
1570 "%s was hot-unplugged, %d stale handles\n",
1571 xbdev->nodename, bdev->bd_openers);
1572
Daniel Stodden7b32d102010-04-30 22:01:23 +00001573 if (info && !bdev->bd_openers) {
Daniel Stoddenfa1bd352010-04-30 22:01:22 +00001574 xlvbd_release_gendisk(info);
1575 disk->private_data = NULL;
1576 kfree(info);
1577 }
1578
1579 mutex_unlock(&bdev->bd_mutex);
1580 bdput(bdev);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001581
1582 return 0;
1583}
1584
Christian Limpach1d78d702008-04-02 10:54:04 -07001585static int blkfront_is_ready(struct xenbus_device *dev)
1586{
Greg Kroah-Hartmana1b4b122009-04-30 14:43:31 -07001587 struct blkfront_info *info = dev_get_drvdata(&dev->dev);
Christian Limpach1d78d702008-04-02 10:54:04 -07001588
Jan Beulich5d7ed202010-08-07 18:31:12 +02001589 return info->is_ready && info->xbdev;
Christian Limpach1d78d702008-04-02 10:54:04 -07001590}
1591
Al Viroa63c8482008-03-02 10:23:47 -05001592static int blkif_open(struct block_device *bdev, fmode_t mode)
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001593{
Daniel Stodden13961742010-08-07 18:36:53 +02001594 struct gendisk *disk = bdev->bd_disk;
1595 struct blkfront_info *info;
1596 int err = 0;
Arnd Bergmann6e9624b2010-08-07 18:25:34 +02001597
Arnd Bergmann2a48fc02010-06-02 14:28:52 +02001598 mutex_lock(&blkfront_mutex);
Arnd Bergmann6e9624b2010-08-07 18:25:34 +02001599
Daniel Stodden13961742010-08-07 18:36:53 +02001600 info = disk->private_data;
1601 if (!info) {
1602 /* xbdev gone */
1603 err = -ERESTARTSYS;
1604 goto out;
1605 }
1606
1607 mutex_lock(&info->mutex);
1608
1609 if (!info->gd)
1610 /* xbdev is closed */
1611 err = -ERESTARTSYS;
1612
1613 mutex_unlock(&info->mutex);
1614
Daniel Stodden13961742010-08-07 18:36:53 +02001615out:
Arnd Bergmann2a48fc02010-06-02 14:28:52 +02001616 mutex_unlock(&blkfront_mutex);
Daniel Stodden13961742010-08-07 18:36:53 +02001617 return err;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001618}
1619
Al Viroa63c8482008-03-02 10:23:47 -05001620static int blkif_release(struct gendisk *disk, fmode_t mode)
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001621{
Al Viroa63c8482008-03-02 10:23:47 -05001622 struct blkfront_info *info = disk->private_data;
Daniel Stodden7fd152f2010-08-07 18:45:12 +02001623 struct block_device *bdev;
1624 struct xenbus_device *xbdev;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001625
Arnd Bergmann2a48fc02010-06-02 14:28:52 +02001626 mutex_lock(&blkfront_mutex);
Daniel Stodden7fd152f2010-08-07 18:45:12 +02001627
1628 bdev = bdget_disk(disk, 0);
Daniel Stodden7fd152f2010-08-07 18:45:12 +02001629
Daniel Stoddenacfca3c2010-08-07 18:47:26 +02001630 if (bdev->bd_openers)
1631 goto out;
1632
Daniel Stodden7fd152f2010-08-07 18:45:12 +02001633 /*
1634 * Check if we have been instructed to close. We will have
1635 * deferred this request, because the bdev was still open.
1636 */
1637
1638 mutex_lock(&info->mutex);
1639 xbdev = info->xbdev;
1640
1641 if (xbdev && xbdev->state == XenbusStateClosing) {
1642 /* pending switch to state closed */
Daniel Stoddend54142c2010-08-07 18:51:21 +02001643 dev_info(disk_to_dev(bdev->bd_disk), "releasing disk\n");
Daniel Stodden7fd152f2010-08-07 18:45:12 +02001644 xlvbd_release_gendisk(info);
1645 xenbus_frontend_closed(info->xbdev);
1646 }
1647
1648 mutex_unlock(&info->mutex);
1649
1650 if (!xbdev) {
1651 /* sudden device removal */
Daniel Stoddend54142c2010-08-07 18:51:21 +02001652 dev_info(disk_to_dev(bdev->bd_disk), "releasing disk\n");
Daniel Stodden7fd152f2010-08-07 18:45:12 +02001653 xlvbd_release_gendisk(info);
1654 disk->private_data = NULL;
1655 kfree(info);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001656 }
Daniel Stodden7fd152f2010-08-07 18:45:12 +02001657
Jens Axboea4cc14e2010-08-08 21:50:05 -04001658out:
Andrew Jonesdad5cf62012-02-16 13:16:25 +01001659 bdput(bdev);
Arnd Bergmann2a48fc02010-06-02 14:28:52 +02001660 mutex_unlock(&blkfront_mutex);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001661 return 0;
1662}
1663
Alexey Dobriyan83d5cde2009-09-21 17:01:13 -07001664static const struct block_device_operations xlvbd_block_fops =
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001665{
1666 .owner = THIS_MODULE,
Al Viroa63c8482008-03-02 10:23:47 -05001667 .open = blkif_open,
1668 .release = blkif_release,
Ian Campbell597592d2008-02-21 13:03:45 -08001669 .getgeo = blkif_getgeo,
Arnd Bergmann8a6cfeb2010-07-08 10:18:46 +02001670 .ioctl = blkif_ioctl,
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001671};
1672
1673
Márton Némethec9c42e2010-01-10 13:39:52 +01001674static const struct xenbus_device_id blkfront_ids[] = {
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001675 { "vbd" },
1676 { "" }
1677};
1678
Jan Beulich73db1442011-12-22 09:08:13 +00001679static DEFINE_XENBUS_DRIVER(blkfront, ,
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001680 .probe = blkfront_probe,
1681 .remove = blkfront_remove,
1682 .resume = blkfront_resume,
Ian Campbell203fd612009-12-04 15:33:54 +00001683 .otherend_changed = blkback_changed,
Christian Limpach1d78d702008-04-02 10:54:04 -07001684 .is_ready = blkfront_is_ready,
Jan Beulich73db1442011-12-22 09:08:13 +00001685);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001686
1687static int __init xlblk_init(void)
1688{
Laszlo Ersek469738e2011-10-07 21:34:38 +02001689 int ret;
1690
Jeremy Fitzhardinge6e833582008-08-19 13:16:17 -07001691 if (!xen_domain())
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001692 return -ENODEV;
1693
Igor Mammedove95ae5a2012-03-27 19:31:08 +02001694 if (xen_hvm_domain() && !xen_platform_pci_unplug)
Igor Mammedovb9136d22012-03-21 15:08:38 +01001695 return -ENODEV;
1696
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001697 if (register_blkdev(XENVBD_MAJOR, DEV_NAME)) {
1698 printk(KERN_WARNING "xen_blk: can't get major %d with name %s\n",
1699 XENVBD_MAJOR, DEV_NAME);
1700 return -ENODEV;
1701 }
1702
Jan Beulich73db1442011-12-22 09:08:13 +00001703 ret = xenbus_register_frontend(&blkfront_driver);
Laszlo Ersek469738e2011-10-07 21:34:38 +02001704 if (ret) {
1705 unregister_blkdev(XENVBD_MAJOR, DEV_NAME);
1706 return ret;
1707 }
1708
1709 return 0;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001710}
1711module_init(xlblk_init);
1712
1713
Jan Beulich5a60d0c2008-06-17 10:47:08 +02001714static void __exit xlblk_exit(void)
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001715{
Jan Beulich86050672012-04-05 16:04:52 +01001716 xenbus_unregister_driver(&blkfront_driver);
1717 unregister_blkdev(XENVBD_MAJOR, DEV_NAME);
1718 kfree(minors);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001719}
1720module_exit(xlblk_exit);
1721
1722MODULE_DESCRIPTION("Xen virtual block device frontend");
1723MODULE_LICENSE("GPL");
1724MODULE_ALIAS_BLOCKDEV_MAJOR(XENVBD_MAJOR);
Mark McLoughlind2f0c522008-04-02 10:54:05 -07001725MODULE_ALIAS("xen:vbd");
Mark McLoughlin4f93f09b2008-04-02 10:54:06 -07001726MODULE_ALIAS("xenblk");