blob: 64d9c6dfc63401352e1d71f74c026c50c2044159 [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>
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -070046
Jeremy Fitzhardinge1ccbf532009-10-06 15:11:14 -070047#include <xen/xen.h>
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -070048#include <xen/xenbus.h>
49#include <xen/grant_table.h>
50#include <xen/events.h>
51#include <xen/page.h>
Stefano Stabellinic1c54132010-05-14 12:44:30 +010052#include <xen/platform_pci.h>
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -070053
54#include <xen/interface/grant_table.h>
55#include <xen/interface/io/blkif.h>
Markus Armbruster3e334232008-04-02 10:54:02 -070056#include <xen/interface/io/protocols.h>
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -070057
58#include <asm/xen/hypervisor.h>
59
60enum blkif_state {
61 BLKIF_STATE_DISCONNECTED,
62 BLKIF_STATE_CONNECTED,
63 BLKIF_STATE_SUSPENDED,
64};
65
66struct blk_shadow {
67 struct blkif_request req;
Jeremy Fitzhardingea945b982010-11-01 17:03:14 -040068 struct request *request;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -070069 unsigned long frame[BLKIF_MAX_SEGMENTS_PER_REQUEST];
70};
71
Arnd Bergmann2a48fc02010-06-02 14:28:52 +020072static DEFINE_MUTEX(blkfront_mutex);
Alexey Dobriyan83d5cde2009-09-21 17:01:13 -070073static const struct block_device_operations xlvbd_block_fops;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -070074
Jeremy Fitzhardinge667c78af2010-12-08 12:39:12 -080075#define BLK_RING_SIZE __CONST_RING_SIZE(blkif, PAGE_SIZE)
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -070076
77/*
78 * We have one of these per vbd, whether ide, scsi or 'other'. They
79 * hang in private_data off the gendisk structure. We may end up
80 * putting all kinds of interesting stuff here :-)
81 */
82struct blkfront_info
83{
Daniel Stoddenb70f5fa2010-04-30 22:01:19 +000084 struct mutex mutex;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -070085 struct xenbus_device *xbdev;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -070086 struct gendisk *gd;
87 int vdevice;
88 blkif_vdev_t handle;
89 enum blkif_state connected;
90 int ring_ref;
91 struct blkif_front_ring ring;
Jens Axboe9e973e62009-02-24 08:10:09 +010092 struct scatterlist sg[BLKIF_MAX_SEGMENTS_PER_REQUEST];
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -070093 unsigned int evtchn, irq;
94 struct request_queue *rq;
95 struct work_struct work;
96 struct gnttab_free_callback callback;
97 struct blk_shadow shadow[BLK_RING_SIZE];
98 unsigned long shadow_free;
Tejun Heo4913efe2010-09-03 11:56:16 +020099 unsigned int feature_flush;
Christian Limpach1d78d702008-04-02 10:54:04 -0700100 int is_ready;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700101};
102
103static DEFINE_SPINLOCK(blkif_io_lock);
104
Jan Beulich0e345822010-08-07 18:28:55 +0200105static unsigned int nr_minors;
106static unsigned long *minors;
107static DEFINE_SPINLOCK(minor_lock);
108
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700109#define MAXIMUM_OUTSTANDING_BLOCK_REQS \
110 (BLKIF_MAX_SEGMENTS_PER_REQUEST * BLK_RING_SIZE)
111#define GRANT_INVALID_REF 0
112
113#define PARTS_PER_DISK 16
Chris Lalancette9246b5f2008-09-17 14:30:32 -0700114#define PARTS_PER_EXT_DISK 256
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700115
116#define BLKIF_MAJOR(dev) ((dev)>>8)
117#define BLKIF_MINOR(dev) ((dev) & 0xff)
118
Chris Lalancette9246b5f2008-09-17 14:30:32 -0700119#define EXT_SHIFT 28
120#define EXTENDED (1<<EXT_SHIFT)
121#define VDEV_IS_EXTENDED(dev) ((dev)&(EXTENDED))
122#define BLKIF_MINOR_EXT(dev) ((dev)&(~EXTENDED))
Stefano Stabellinic80a4202010-12-02 17:55:00 +0000123#define EMULATED_HD_DISK_MINOR_OFFSET (0)
124#define EMULATED_HD_DISK_NAME_OFFSET (EMULATED_HD_DISK_MINOR_OFFSET / 256)
125#define EMULATED_SD_DISK_MINOR_OFFSET (EMULATED_HD_DISK_MINOR_OFFSET + (4 * 16))
126#define EMULATED_SD_DISK_NAME_OFFSET (EMULATED_HD_DISK_NAME_OFFSET + 4)
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700127
Chris Lalancette9246b5f2008-09-17 14:30:32 -0700128#define DEV_NAME "xvd" /* name in /dev */
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700129
130static int get_id_from_freelist(struct blkfront_info *info)
131{
132 unsigned long free = info->shadow_free;
Roel Kluinb9ed7252009-05-22 09:25:32 +0200133 BUG_ON(free >= BLK_RING_SIZE);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700134 info->shadow_free = info->shadow[free].req.id;
135 info->shadow[free].req.id = 0x0fffffee; /* debug */
136 return free;
137}
138
139static void add_id_to_freelist(struct blkfront_info *info,
140 unsigned long id)
141{
142 info->shadow[id].req.id = info->shadow_free;
Jeremy Fitzhardingea945b982010-11-01 17:03:14 -0400143 info->shadow[id].request = NULL;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700144 info->shadow_free = id;
145}
146
Jan Beulich0e345822010-08-07 18:28:55 +0200147static int xlbd_reserve_minors(unsigned int minor, unsigned int nr)
148{
149 unsigned int end = minor + nr;
150 int rc;
151
152 if (end > nr_minors) {
153 unsigned long *bitmap, *old;
154
155 bitmap = kzalloc(BITS_TO_LONGS(end) * sizeof(*bitmap),
156 GFP_KERNEL);
157 if (bitmap == NULL)
158 return -ENOMEM;
159
160 spin_lock(&minor_lock);
161 if (end > nr_minors) {
162 old = minors;
163 memcpy(bitmap, minors,
164 BITS_TO_LONGS(nr_minors) * sizeof(*bitmap));
165 minors = bitmap;
166 nr_minors = BITS_TO_LONGS(end) * BITS_PER_LONG;
167 } else
168 old = bitmap;
169 spin_unlock(&minor_lock);
170 kfree(old);
171 }
172
173 spin_lock(&minor_lock);
174 if (find_next_bit(minors, end, minor) >= end) {
175 for (; minor < end; ++minor)
176 __set_bit(minor, minors);
177 rc = 0;
178 } else
179 rc = -EBUSY;
180 spin_unlock(&minor_lock);
181
182 return rc;
183}
184
185static void xlbd_release_minors(unsigned int minor, unsigned int nr)
186{
187 unsigned int end = minor + nr;
188
189 BUG_ON(end > nr_minors);
190 spin_lock(&minor_lock);
191 for (; minor < end; ++minor)
192 __clear_bit(minor, minors);
193 spin_unlock(&minor_lock);
194}
195
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700196static void blkif_restart_queue_callback(void *arg)
197{
198 struct blkfront_info *info = (struct blkfront_info *)arg;
199 schedule_work(&info->work);
200}
201
Harvey Harrisonafe42d72008-04-29 00:59:47 -0700202static int blkif_getgeo(struct block_device *bd, struct hd_geometry *hg)
Ian Campbell597592d2008-02-21 13:03:45 -0800203{
204 /* We don't have real geometry info, but let's at least return
205 values consistent with the size of the device */
206 sector_t nsect = get_capacity(bd->bd_disk);
207 sector_t cylinders = nsect;
208
209 hg->heads = 0xff;
210 hg->sectors = 0x3f;
211 sector_div(cylinders, hg->heads * hg->sectors);
212 hg->cylinders = cylinders;
213 if ((sector_t)(hg->cylinders + 1) * hg->heads * hg->sectors < nsect)
214 hg->cylinders = 0xffff;
215 return 0;
216}
217
Al Viroa63c8482008-03-02 10:23:47 -0500218static int blkif_ioctl(struct block_device *bdev, fmode_t mode,
Adrian Bunk62aa0052008-08-04 11:59:05 +0200219 unsigned command, unsigned long argument)
Christian Limpach440a01a2008-06-17 10:47:08 +0200220{
Al Viroa63c8482008-03-02 10:23:47 -0500221 struct blkfront_info *info = bdev->bd_disk->private_data;
Christian Limpach440a01a2008-06-17 10:47:08 +0200222 int i;
223
224 dev_dbg(&info->xbdev->dev, "command: 0x%x, argument: 0x%lx\n",
225 command, (long)argument);
226
227 switch (command) {
228 case CDROMMULTISESSION:
229 dev_dbg(&info->xbdev->dev, "FIXME: support multisession CDs later\n");
230 for (i = 0; i < sizeof(struct cdrom_multisession); i++)
231 if (put_user(0, (char __user *)(argument + i)))
232 return -EFAULT;
233 return 0;
234
235 case CDROM_GET_CAPABILITY: {
236 struct gendisk *gd = info->gd;
237 if (gd->flags & GENHD_FL_CD)
238 return 0;
239 return -EINVAL;
240 }
241
242 default:
243 /*printk(KERN_ALERT "ioctl %08x not supported by Xen blkdev\n",
244 command);*/
245 return -EINVAL; /* same return as native Linux */
246 }
247
248 return 0;
249}
250
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700251/*
Jeremy Fitzhardingec64e38e2010-11-01 14:32:27 -0400252 * Generate a Xen blkfront IO request from a blk layer request. Reads
253 * and writes are handled as expected. Since we lack a loose flush
254 * request, we map flushes into a full ordered barrier.
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700255 *
Jeremy Fitzhardingec64e38e2010-11-01 14:32:27 -0400256 * @req: a request struct
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700257 */
258static int blkif_queue_request(struct request *req)
259{
260 struct blkfront_info *info = req->rq_disk->private_data;
261 unsigned long buffer_mfn;
262 struct blkif_request *ring_req;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700263 unsigned long id;
264 unsigned int fsect, lsect;
Jens Axboe9e973e62009-02-24 08:10:09 +0100265 int i, ref;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700266 grant_ref_t gref_head;
Jens Axboe9e973e62009-02-24 08:10:09 +0100267 struct scatterlist *sg;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700268
269 if (unlikely(info->connected != BLKIF_STATE_CONNECTED))
270 return 1;
271
272 if (gnttab_alloc_grant_references(
273 BLKIF_MAX_SEGMENTS_PER_REQUEST, &gref_head) < 0) {
274 gnttab_request_free_callback(
275 &info->callback,
276 blkif_restart_queue_callback,
277 info,
278 BLKIF_MAX_SEGMENTS_PER_REQUEST);
279 return 1;
280 }
281
282 /* Fill out a communications ring structure. */
283 ring_req = RING_GET_REQUEST(&info->ring, info->ring.req_prod_pvt);
284 id = get_id_from_freelist(info);
Jeremy Fitzhardingea945b982010-11-01 17:03:14 -0400285 info->shadow[id].request = req;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700286
287 ring_req->id = id;
Tejun Heo83096eb2009-05-07 22:24:39 +0900288 ring_req->sector_number = (blkif_sector_t)blk_rq_pos(req);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700289 ring_req->handle = info->handle;
290
291 ring_req->operation = rq_data_dir(req) ?
292 BLKIF_OP_WRITE : BLKIF_OP_READ;
Jeremy Fitzhardingebe2f8372010-11-02 10:38:33 -0400293
294 if (req->cmd_flags & (REQ_FLUSH | REQ_FUA)) {
295 /*
296 * Ideally we could just do an unordered
297 * flush-to-disk, but all we have is a full write
298 * barrier at the moment. However, a barrier write is
299 * a superset of FUA, so we can implement it the same
300 * way. (It's also a FLUSH+FUA, since it is
301 * guaranteed ordered WRT previous writes.)
302 */
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700303 ring_req->operation = BLKIF_OP_WRITE_BARRIER;
Jeremy Fitzhardingebe2f8372010-11-02 10:38:33 -0400304 }
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700305
Jens Axboe9e973e62009-02-24 08:10:09 +0100306 ring_req->nr_segments = blk_rq_map_sg(req->q, req, info->sg);
307 BUG_ON(ring_req->nr_segments > BLKIF_MAX_SEGMENTS_PER_REQUEST);
308
309 for_each_sg(info->sg, sg, ring_req->nr_segments, i) {
310 buffer_mfn = pfn_to_mfn(page_to_pfn(sg_page(sg)));
311 fsect = sg->offset >> 9;
312 lsect = fsect + (sg->length >> 9) - 1;
Jens Axboe6c92e692007-08-16 13:43:12 +0200313 /* install a grant reference. */
314 ref = gnttab_claim_grant_reference(&gref_head);
315 BUG_ON(ref == -ENOSPC);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700316
Jens Axboe6c92e692007-08-16 13:43:12 +0200317 gnttab_grant_foreign_access_ref(
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700318 ref,
319 info->xbdev->otherend_id,
320 buffer_mfn,
321 rq_data_dir(req) );
322
Jens Axboe9e973e62009-02-24 08:10:09 +0100323 info->shadow[id].frame[i] = mfn_to_pfn(buffer_mfn);
324 ring_req->seg[i] =
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700325 (struct blkif_request_segment) {
326 .gref = ref,
327 .first_sect = fsect,
328 .last_sect = lsect };
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700329 }
330
331 info->ring.req_prod_pvt++;
332
333 /* Keep a private copy so we can reissue requests when recovering. */
334 info->shadow[id].req = *ring_req;
335
336 gnttab_free_grant_references(gref_head);
337
338 return 0;
339}
340
341
342static inline void flush_requests(struct blkfront_info *info)
343{
344 int notify;
345
346 RING_PUSH_REQUESTS_AND_CHECK_NOTIFY(&info->ring, notify);
347
348 if (notify)
349 notify_remote_via_irq(info->irq);
350}
351
352/*
353 * do_blkif_request
354 * read a block; request is in a request queue
355 */
Jens Axboe165125e2007-07-24 09:28:11 +0200356static void do_blkif_request(struct request_queue *rq)
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700357{
358 struct blkfront_info *info = NULL;
359 struct request *req;
360 int queued;
361
362 pr_debug("Entered do_blkif_request\n");
363
364 queued = 0;
365
Tejun Heo9934c8c2009-05-08 11:54:16 +0900366 while ((req = blk_peek_request(rq)) != NULL) {
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700367 info = req->rq_disk->private_data;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700368
369 if (RING_FULL(&info->ring))
370 goto wait;
371
Tejun Heo9934c8c2009-05-08 11:54:16 +0900372 blk_start_request(req);
Tejun Heo296b2f62009-05-08 11:54:15 +0900373
Christoph Hellwig33659eb2010-08-07 18:17:56 +0200374 if (req->cmd_type != REQ_TYPE_FS) {
Tejun Heo296b2f62009-05-08 11:54:15 +0900375 __blk_end_request_all(req, -EIO);
376 continue;
377 }
378
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700379 pr_debug("do_blk_req %p: cmd %p, sec %lx, "
Tejun Heo83096eb2009-05-07 22:24:39 +0900380 "(%u/%u) buffer:%p [%s]\n",
381 req, req->cmd, (unsigned long)blk_rq_pos(req),
382 blk_rq_cur_sectors(req), blk_rq_sectors(req),
383 req->buffer, rq_data_dir(req) ? "write" : "read");
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700384
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700385 if (blkif_queue_request(req)) {
386 blk_requeue_request(rq, req);
387wait:
388 /* Avoid pointless unplugs. */
389 blk_stop_queue(rq);
390 break;
391 }
392
393 queued++;
394 }
395
396 if (queued != 0)
397 flush_requests(info);
398}
399
400static int xlvbd_init_blk_queue(struct gendisk *gd, u16 sector_size)
401{
Jens Axboe165125e2007-07-24 09:28:11 +0200402 struct request_queue *rq;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700403
404 rq = blk_init_queue(do_blkif_request, &blkif_io_lock);
405 if (rq == NULL)
406 return -1;
407
Fernando Luis Vázquez Cao66d352e2008-10-27 18:45:54 +0900408 queue_flag_set_unlocked(QUEUE_FLAG_VIRT, rq);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700409
410 /* Hard sector size and max sectors impersonate the equiv. hardware. */
Martin K. Petersene1defc42009-05-22 17:17:49 -0400411 blk_queue_logical_block_size(rq, sector_size);
Martin K. Petersen086fa5f2010-02-26 00:20:38 -0500412 blk_queue_max_hw_sectors(rq, 512);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700413
414 /* Each segment in a request is up to an aligned page in size. */
415 blk_queue_segment_boundary(rq, PAGE_SIZE - 1);
416 blk_queue_max_segment_size(rq, PAGE_SIZE);
417
418 /* Ensure a merged request will fit in a single I/O ring slot. */
Martin K. Petersen8a783622010-02-26 00:20:39 -0500419 blk_queue_max_segments(rq, BLKIF_MAX_SEGMENTS_PER_REQUEST);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700420
421 /* Make sure buffer addresses are sector-aligned. */
422 blk_queue_dma_alignment(rq, 511);
423
Ian Campbell1c91fe12008-06-17 10:47:08 +0200424 /* Make sure we don't use bounce buffers. */
425 blk_queue_bounce_limit(rq, BLK_BOUNCE_ANY);
426
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700427 gd->queue = rq;
428
429 return 0;
430}
431
432
Tejun Heo4913efe2010-09-03 11:56:16 +0200433static void xlvbd_flush(struct blkfront_info *info)
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700434{
Tejun Heo4913efe2010-09-03 11:56:16 +0200435 blk_queue_flush(info->rq, info->feature_flush);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700436 printk(KERN_INFO "blkfront: %s: barriers %s\n",
Tejun Heo4913efe2010-09-03 11:56:16 +0200437 info->gd->disk_name,
438 info->feature_flush ? "enabled" : "disabled");
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700439}
440
Stefano Stabellinic80a4202010-12-02 17:55:00 +0000441static int xen_translate_vdev(int vdevice, int *minor, unsigned int *offset)
442{
443 int major;
444 major = BLKIF_MAJOR(vdevice);
445 *minor = BLKIF_MINOR(vdevice);
446 switch (major) {
447 case XEN_IDE0_MAJOR:
448 *offset = (*minor / 64) + EMULATED_HD_DISK_NAME_OFFSET;
449 *minor = ((*minor / 64) * PARTS_PER_DISK) +
450 EMULATED_HD_DISK_MINOR_OFFSET;
451 break;
452 case XEN_IDE1_MAJOR:
453 *offset = (*minor / 64) + 2 + EMULATED_HD_DISK_NAME_OFFSET;
454 *minor = (((*minor / 64) + 2) * PARTS_PER_DISK) +
455 EMULATED_HD_DISK_MINOR_OFFSET;
456 break;
457 case XEN_SCSI_DISK0_MAJOR:
458 *offset = (*minor / PARTS_PER_DISK) + EMULATED_SD_DISK_NAME_OFFSET;
459 *minor = *minor + EMULATED_SD_DISK_MINOR_OFFSET;
460 break;
461 case XEN_SCSI_DISK1_MAJOR:
462 case XEN_SCSI_DISK2_MAJOR:
463 case XEN_SCSI_DISK3_MAJOR:
464 case XEN_SCSI_DISK4_MAJOR:
465 case XEN_SCSI_DISK5_MAJOR:
466 case XEN_SCSI_DISK6_MAJOR:
467 case XEN_SCSI_DISK7_MAJOR:
468 *offset = (*minor / PARTS_PER_DISK) +
469 ((major - XEN_SCSI_DISK1_MAJOR + 1) * 16) +
470 EMULATED_SD_DISK_NAME_OFFSET;
471 *minor = *minor +
472 ((major - XEN_SCSI_DISK1_MAJOR + 1) * 16 * PARTS_PER_DISK) +
473 EMULATED_SD_DISK_MINOR_OFFSET;
474 break;
475 case XEN_SCSI_DISK8_MAJOR:
476 case XEN_SCSI_DISK9_MAJOR:
477 case XEN_SCSI_DISK10_MAJOR:
478 case XEN_SCSI_DISK11_MAJOR:
479 case XEN_SCSI_DISK12_MAJOR:
480 case XEN_SCSI_DISK13_MAJOR:
481 case XEN_SCSI_DISK14_MAJOR:
482 case XEN_SCSI_DISK15_MAJOR:
483 *offset = (*minor / PARTS_PER_DISK) +
484 ((major - XEN_SCSI_DISK8_MAJOR + 8) * 16) +
485 EMULATED_SD_DISK_NAME_OFFSET;
486 *minor = *minor +
487 ((major - XEN_SCSI_DISK8_MAJOR + 8) * 16 * PARTS_PER_DISK) +
488 EMULATED_SD_DISK_MINOR_OFFSET;
489 break;
490 case XENVBD_MAJOR:
491 *offset = *minor / PARTS_PER_DISK;
492 break;
493 default:
494 printk(KERN_WARNING "blkfront: your disk configuration is "
495 "incorrect, please use an xvd device instead\n");
496 return -ENODEV;
497 }
498 return 0;
499}
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700500
Chris Lalancette9246b5f2008-09-17 14:30:32 -0700501static int xlvbd_alloc_gendisk(blkif_sector_t capacity,
502 struct blkfront_info *info,
503 u16 vdisk_info, u16 sector_size)
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700504{
505 struct gendisk *gd;
506 int nr_minors = 1;
Stefano Stabellinic80a4202010-12-02 17:55:00 +0000507 int err;
Chris Lalancette9246b5f2008-09-17 14:30:32 -0700508 unsigned int offset;
509 int minor;
510 int nr_parts;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700511
512 BUG_ON(info->gd != NULL);
513 BUG_ON(info->rq != NULL);
514
Chris Lalancette9246b5f2008-09-17 14:30:32 -0700515 if ((info->vdevice>>EXT_SHIFT) > 1) {
516 /* this is above the extended range; something is wrong */
517 printk(KERN_WARNING "blkfront: vdevice 0x%x is above the extended range; ignoring\n", info->vdevice);
518 return -ENODEV;
519 }
520
521 if (!VDEV_IS_EXTENDED(info->vdevice)) {
Stefano Stabellinic80a4202010-12-02 17:55:00 +0000522 err = xen_translate_vdev(info->vdevice, &minor, &offset);
523 if (err)
524 return err;
525 nr_parts = PARTS_PER_DISK;
Chris Lalancette9246b5f2008-09-17 14:30:32 -0700526 } else {
527 minor = BLKIF_MINOR_EXT(info->vdevice);
528 nr_parts = PARTS_PER_EXT_DISK;
Stefano Stabellinic80a4202010-12-02 17:55:00 +0000529 offset = minor / nr_parts;
530 if (xen_hvm_domain() && offset <= EMULATED_HD_DISK_NAME_OFFSET + 4)
531 printk(KERN_WARNING "blkfront: vdevice 0x%x might conflict with "
532 "emulated IDE disks,\n\t choose an xvd device name"
533 "from xvde on\n", info->vdevice);
Chris Lalancette9246b5f2008-09-17 14:30:32 -0700534 }
Stefano Stabellinic80a4202010-12-02 17:55:00 +0000535 err = -ENODEV;
Chris Lalancette9246b5f2008-09-17 14:30:32 -0700536
537 if ((minor % nr_parts) == 0)
538 nr_minors = nr_parts;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700539
Jan Beulich0e345822010-08-07 18:28:55 +0200540 err = xlbd_reserve_minors(minor, nr_minors);
541 if (err)
542 goto out;
543 err = -ENODEV;
544
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700545 gd = alloc_disk(nr_minors);
546 if (gd == NULL)
Jan Beulich0e345822010-08-07 18:28:55 +0200547 goto release;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700548
Chris Lalancette9246b5f2008-09-17 14:30:32 -0700549 if (nr_minors > 1) {
550 if (offset < 26)
551 sprintf(gd->disk_name, "%s%c", DEV_NAME, 'a' + offset);
552 else
553 sprintf(gd->disk_name, "%s%c%c", DEV_NAME,
554 'a' + ((offset / 26)-1), 'a' + (offset % 26));
555 } else {
556 if (offset < 26)
557 sprintf(gd->disk_name, "%s%c%d", DEV_NAME,
558 'a' + offset,
559 minor & (nr_parts - 1));
560 else
561 sprintf(gd->disk_name, "%s%c%c%d", DEV_NAME,
562 'a' + ((offset / 26) - 1),
563 'a' + (offset % 26),
564 minor & (nr_parts - 1));
565 }
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700566
567 gd->major = XENVBD_MAJOR;
568 gd->first_minor = minor;
569 gd->fops = &xlvbd_block_fops;
570 gd->private_data = info;
571 gd->driverfs_dev = &(info->xbdev->dev);
572 set_capacity(gd, capacity);
573
574 if (xlvbd_init_blk_queue(gd, sector_size)) {
575 del_gendisk(gd);
Jan Beulich0e345822010-08-07 18:28:55 +0200576 goto release;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700577 }
578
579 info->rq = gd->queue;
580 info->gd = gd;
581
Tejun Heo4913efe2010-09-03 11:56:16 +0200582 xlvbd_flush(info);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700583
584 if (vdisk_info & VDISK_READONLY)
585 set_disk_ro(gd, 1);
586
587 if (vdisk_info & VDISK_REMOVABLE)
588 gd->flags |= GENHD_FL_REMOVABLE;
589
590 if (vdisk_info & VDISK_CDROM)
591 gd->flags |= GENHD_FL_CD;
592
593 return 0;
594
Jan Beulich0e345822010-08-07 18:28:55 +0200595 release:
596 xlbd_release_minors(minor, nr_minors);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700597 out:
598 return err;
599}
600
Daniel Stoddena66b5ae2010-08-07 18:33:17 +0200601static void xlvbd_release_gendisk(struct blkfront_info *info)
602{
603 unsigned int minor, nr_minors;
604 unsigned long flags;
605
606 if (info->rq == NULL)
607 return;
608
609 spin_lock_irqsave(&blkif_io_lock, flags);
610
611 /* No more blkif_request(). */
612 blk_stop_queue(info->rq);
613
614 /* No more gnttab callback work. */
615 gnttab_cancel_free_callback(&info->callback);
616 spin_unlock_irqrestore(&blkif_io_lock, flags);
617
618 /* Flush gnttab callback work. Must be done with no locks held. */
Tejun Heo30d65032010-12-24 15:59:06 +0100619 flush_work_sync(&info->work);
Daniel Stoddena66b5ae2010-08-07 18:33:17 +0200620
621 del_gendisk(info->gd);
622
623 minor = info->gd->first_minor;
624 nr_minors = info->gd->minors;
625 xlbd_release_minors(minor, nr_minors);
626
627 blk_cleanup_queue(info->rq);
628 info->rq = NULL;
629
630 put_disk(info->gd);
631 info->gd = NULL;
632}
633
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700634static void kick_pending_request_queues(struct blkfront_info *info)
635{
636 if (!RING_FULL(&info->ring)) {
637 /* Re-enable calldowns. */
638 blk_start_queue(info->rq);
639 /* Kick things off immediately. */
640 do_blkif_request(info->rq);
641 }
642}
643
644static void blkif_restart_queue(struct work_struct *work)
645{
646 struct blkfront_info *info = container_of(work, struct blkfront_info, work);
647
648 spin_lock_irq(&blkif_io_lock);
649 if (info->connected == BLKIF_STATE_CONNECTED)
650 kick_pending_request_queues(info);
651 spin_unlock_irq(&blkif_io_lock);
652}
653
654static void blkif_free(struct blkfront_info *info, int suspend)
655{
656 /* Prevent new requests being issued until we fix things up. */
657 spin_lock_irq(&blkif_io_lock);
658 info->connected = suspend ?
659 BLKIF_STATE_SUSPENDED : BLKIF_STATE_DISCONNECTED;
660 /* No more blkif_request(). */
661 if (info->rq)
662 blk_stop_queue(info->rq);
663 /* No more gnttab callback work. */
664 gnttab_cancel_free_callback(&info->callback);
665 spin_unlock_irq(&blkif_io_lock);
666
667 /* Flush gnttab callback work. Must be done with no locks held. */
Tejun Heo30d65032010-12-24 15:59:06 +0100668 flush_work_sync(&info->work);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700669
670 /* Free resources associated with old device channel. */
671 if (info->ring_ref != GRANT_INVALID_REF) {
672 gnttab_end_foreign_access(info->ring_ref, 0,
673 (unsigned long)info->ring.sring);
674 info->ring_ref = GRANT_INVALID_REF;
675 info->ring.sring = NULL;
676 }
677 if (info->irq)
678 unbind_from_irqhandler(info->irq, info);
679 info->evtchn = info->irq = 0;
680
681}
682
683static void blkif_completion(struct blk_shadow *s)
684{
685 int i;
686 for (i = 0; i < s->req.nr_segments; i++)
687 gnttab_end_foreign_access(s->req.seg[i].gref, 0, 0UL);
688}
689
690static irqreturn_t blkif_interrupt(int irq, void *dev_id)
691{
692 struct request *req;
693 struct blkif_response *bret;
694 RING_IDX i, rp;
695 unsigned long flags;
696 struct blkfront_info *info = (struct blkfront_info *)dev_id;
Kiyoshi Uedaf530f0362007-12-11 17:47:36 -0500697 int error;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700698
699 spin_lock_irqsave(&blkif_io_lock, flags);
700
701 if (unlikely(info->connected != BLKIF_STATE_CONNECTED)) {
702 spin_unlock_irqrestore(&blkif_io_lock, flags);
703 return IRQ_HANDLED;
704 }
705
706 again:
707 rp = info->ring.sring->rsp_prod;
708 rmb(); /* Ensure we see queued responses up to 'rp'. */
709
710 for (i = info->ring.rsp_cons; i != rp; i++) {
711 unsigned long id;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700712
713 bret = RING_GET_RESPONSE(&info->ring, i);
714 id = bret->id;
Jeremy Fitzhardingea945b982010-11-01 17:03:14 -0400715 req = info->shadow[id].request;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700716
717 blkif_completion(&info->shadow[id]);
718
719 add_id_to_freelist(info, id);
720
Kiyoshi Uedaf530f0362007-12-11 17:47:36 -0500721 error = (bret->status == BLKIF_RSP_OKAY) ? 0 : -EIO;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700722 switch (bret->operation) {
723 case BLKIF_OP_WRITE_BARRIER:
724 if (unlikely(bret->status == BLKIF_RSP_EOPNOTSUPP)) {
725 printk(KERN_WARNING "blkfront: %s: write barrier op failed\n",
726 info->gd->disk_name);
Kiyoshi Uedaf530f0362007-12-11 17:47:36 -0500727 error = -EOPNOTSUPP;
Jeremy Fitzhardingedcb8bae2010-11-02 11:55:58 -0400728 }
729 if (unlikely(bret->status == BLKIF_RSP_ERROR &&
730 info->shadow[id].req.nr_segments == 0)) {
731 printk(KERN_WARNING "blkfront: %s: empty write barrier op failed\n",
732 info->gd->disk_name);
733 error = -EOPNOTSUPP;
734 }
735 if (unlikely(error)) {
736 if (error == -EOPNOTSUPP)
737 error = 0;
Tejun Heo4913efe2010-09-03 11:56:16 +0200738 info->feature_flush = 0;
739 xlvbd_flush(info);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700740 }
741 /* fall through */
742 case BLKIF_OP_READ:
743 case BLKIF_OP_WRITE:
744 if (unlikely(bret->status != BLKIF_RSP_OKAY))
745 dev_dbg(&info->xbdev->dev, "Bad return from blkdev data "
746 "request: %x\n", bret->status);
747
Tejun Heo40cbbb72009-04-23 11:05:19 +0900748 __blk_end_request_all(req, error);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700749 break;
750 default:
751 BUG();
752 }
753 }
754
755 info->ring.rsp_cons = i;
756
757 if (i != info->ring.req_prod_pvt) {
758 int more_to_do;
759 RING_FINAL_CHECK_FOR_RESPONSES(&info->ring, more_to_do);
760 if (more_to_do)
761 goto again;
762 } else
763 info->ring.sring->rsp_event = i + 1;
764
765 kick_pending_request_queues(info);
766
767 spin_unlock_irqrestore(&blkif_io_lock, flags);
768
769 return IRQ_HANDLED;
770}
771
772
773static int setup_blkring(struct xenbus_device *dev,
774 struct blkfront_info *info)
775{
776 struct blkif_sring *sring;
777 int err;
778
779 info->ring_ref = GRANT_INVALID_REF;
780
Ian Campbella144ff02008-06-17 10:47:08 +0200781 sring = (struct blkif_sring *)__get_free_page(GFP_NOIO | __GFP_HIGH);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700782 if (!sring) {
783 xenbus_dev_fatal(dev, -ENOMEM, "allocating shared ring");
784 return -ENOMEM;
785 }
786 SHARED_RING_INIT(sring);
787 FRONT_RING_INIT(&info->ring, sring, PAGE_SIZE);
788
Jens Axboe9e973e62009-02-24 08:10:09 +0100789 sg_init_table(info->sg, BLKIF_MAX_SEGMENTS_PER_REQUEST);
790
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700791 err = xenbus_grant_ring(dev, virt_to_mfn(info->ring.sring));
792 if (err < 0) {
793 free_page((unsigned long)sring);
794 info->ring.sring = NULL;
795 goto fail;
796 }
797 info->ring_ref = err;
798
799 err = xenbus_alloc_evtchn(dev, &info->evtchn);
800 if (err)
801 goto fail;
802
803 err = bind_evtchn_to_irqhandler(info->evtchn,
804 blkif_interrupt,
805 IRQF_SAMPLE_RANDOM, "blkif", info);
806 if (err <= 0) {
807 xenbus_dev_fatal(dev, err,
808 "bind_evtchn_to_irqhandler failed");
809 goto fail;
810 }
811 info->irq = err;
812
813 return 0;
814fail:
815 blkif_free(info, 0);
816 return err;
817}
818
819
820/* Common code used when first setting up, and when resuming. */
Ian Campbell203fd612009-12-04 15:33:54 +0000821static int talk_to_blkback(struct xenbus_device *dev,
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700822 struct blkfront_info *info)
823{
824 const char *message = NULL;
825 struct xenbus_transaction xbt;
826 int err;
827
828 /* Create shared ring, alloc event channel. */
829 err = setup_blkring(dev, info);
830 if (err)
831 goto out;
832
833again:
834 err = xenbus_transaction_start(&xbt);
835 if (err) {
836 xenbus_dev_fatal(dev, err, "starting transaction");
837 goto destroy_blkring;
838 }
839
840 err = xenbus_printf(xbt, dev->nodename,
841 "ring-ref", "%u", info->ring_ref);
842 if (err) {
843 message = "writing ring-ref";
844 goto abort_transaction;
845 }
846 err = xenbus_printf(xbt, dev->nodename,
847 "event-channel", "%u", info->evtchn);
848 if (err) {
849 message = "writing event-channel";
850 goto abort_transaction;
851 }
Markus Armbruster3e334232008-04-02 10:54:02 -0700852 err = xenbus_printf(xbt, dev->nodename, "protocol", "%s",
853 XEN_IO_PROTO_ABI_NATIVE);
854 if (err) {
855 message = "writing protocol";
856 goto abort_transaction;
857 }
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700858
859 err = xenbus_transaction_end(xbt, 0);
860 if (err) {
861 if (err == -EAGAIN)
862 goto again;
863 xenbus_dev_fatal(dev, err, "completing transaction");
864 goto destroy_blkring;
865 }
866
867 xenbus_switch_state(dev, XenbusStateInitialised);
868
869 return 0;
870
871 abort_transaction:
872 xenbus_transaction_end(xbt, 1);
873 if (message)
874 xenbus_dev_fatal(dev, err, "%s", message);
875 destroy_blkring:
876 blkif_free(info, 0);
877 out:
878 return err;
879}
880
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700881/**
882 * Entry point to this code when a new device is created. Allocate the basic
883 * structures and the ring buffer for communication with the backend, and
884 * inform the backend of the appropriate details for those. Switch to
885 * Initialised state.
886 */
887static int blkfront_probe(struct xenbus_device *dev,
888 const struct xenbus_device_id *id)
889{
890 int err, vdevice, i;
891 struct blkfront_info *info;
892
893 /* FIXME: Use dynamic device id if this is not set. */
894 err = xenbus_scanf(XBT_NIL, dev->nodename,
895 "virtual-device", "%i", &vdevice);
896 if (err != 1) {
Chris Lalancette9246b5f2008-09-17 14:30:32 -0700897 /* go looking in the extended area instead */
898 err = xenbus_scanf(XBT_NIL, dev->nodename, "virtual-device-ext",
899 "%i", &vdevice);
900 if (err != 1) {
901 xenbus_dev_fatal(dev, err, "reading virtual-device");
902 return err;
903 }
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700904 }
905
Stefano Stabellinib98a4092010-07-29 14:53:16 +0100906 if (xen_hvm_domain()) {
907 char *type;
908 int len;
909 /* no unplug has been done: do not hook devices != xen vbds */
Ian Campbell1dc7ce92010-08-23 11:59:29 +0100910 if (xen_platform_pci_unplug & XEN_UNPLUG_UNNECESSARY) {
Stefano Stabellinib98a4092010-07-29 14:53:16 +0100911 int major;
Stefano Stabellinic1c54132010-05-14 12:44:30 +0100912
Stefano Stabellinib98a4092010-07-29 14:53:16 +0100913 if (!VDEV_IS_EXTENDED(vdevice))
914 major = BLKIF_MAJOR(vdevice);
915 else
916 major = XENVBD_MAJOR;
Stefano Stabellinic1c54132010-05-14 12:44:30 +0100917
Stefano Stabellinib98a4092010-07-29 14:53:16 +0100918 if (major != XENVBD_MAJOR) {
919 printk(KERN_INFO
920 "%s: HVM does not support vbd %d as xen block device\n",
921 __FUNCTION__, vdevice);
922 return -ENODEV;
923 }
924 }
925 /* do not create a PV cdrom device if we are an HVM guest */
926 type = xenbus_read(XBT_NIL, dev->nodename, "device-type", &len);
927 if (IS_ERR(type))
928 return -ENODEV;
929 if (strncmp(type, "cdrom", 5) == 0) {
930 kfree(type);
Stefano Stabellinic1c54132010-05-14 12:44:30 +0100931 return -ENODEV;
932 }
Stefano Stabellinib98a4092010-07-29 14:53:16 +0100933 kfree(type);
Stefano Stabellinic1c54132010-05-14 12:44:30 +0100934 }
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700935 info = kzalloc(sizeof(*info), GFP_KERNEL);
936 if (!info) {
937 xenbus_dev_fatal(dev, -ENOMEM, "allocating info structure");
938 return -ENOMEM;
939 }
940
Daniel Stoddenb70f5fa2010-04-30 22:01:19 +0000941 mutex_init(&info->mutex);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700942 info->xbdev = dev;
943 info->vdevice = vdevice;
944 info->connected = BLKIF_STATE_DISCONNECTED;
945 INIT_WORK(&info->work, blkif_restart_queue);
946
947 for (i = 0; i < BLK_RING_SIZE; i++)
948 info->shadow[i].req.id = i+1;
949 info->shadow[BLK_RING_SIZE-1].req.id = 0x0fffffff;
950
951 /* Front end dir is a number, which is used as the id. */
952 info->handle = simple_strtoul(strrchr(dev->nodename, '/')+1, NULL, 0);
Greg Kroah-Hartmana1b4b122009-04-30 14:43:31 -0700953 dev_set_drvdata(&dev->dev, info);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700954
Ian Campbell203fd612009-12-04 15:33:54 +0000955 err = talk_to_blkback(dev, info);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700956 if (err) {
957 kfree(info);
Greg Kroah-Hartmana1b4b122009-04-30 14:43:31 -0700958 dev_set_drvdata(&dev->dev, NULL);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700959 return err;
960 }
961
962 return 0;
963}
964
965
966static int blkif_recover(struct blkfront_info *info)
967{
968 int i;
969 struct blkif_request *req;
970 struct blk_shadow *copy;
971 int j;
972
973 /* Stage 1: Make a safe copy of the shadow state. */
Ian Campbella144ff02008-06-17 10:47:08 +0200974 copy = kmalloc(sizeof(info->shadow),
975 GFP_NOIO | __GFP_REPEAT | __GFP_HIGH);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700976 if (!copy)
977 return -ENOMEM;
978 memcpy(copy, info->shadow, sizeof(info->shadow));
979
980 /* Stage 2: Set up free list. */
981 memset(&info->shadow, 0, sizeof(info->shadow));
982 for (i = 0; i < BLK_RING_SIZE; i++)
983 info->shadow[i].req.id = i+1;
984 info->shadow_free = info->ring.req_prod_pvt;
985 info->shadow[BLK_RING_SIZE-1].req.id = 0x0fffffff;
986
987 /* Stage 3: Find pending requests and requeue them. */
988 for (i = 0; i < BLK_RING_SIZE; i++) {
989 /* Not in use? */
Jeremy Fitzhardingea945b982010-11-01 17:03:14 -0400990 if (!copy[i].request)
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700991 continue;
992
993 /* Grab a request slot and copy shadow state into it. */
994 req = RING_GET_REQUEST(&info->ring, info->ring.req_prod_pvt);
995 *req = copy[i].req;
996
997 /* We get a new request id, and must reset the shadow state. */
998 req->id = get_id_from_freelist(info);
999 memcpy(&info->shadow[req->id], &copy[i], sizeof(copy[i]));
1000
1001 /* Rewrite any grant references invalidated by susp/resume. */
1002 for (j = 0; j < req->nr_segments; j++)
1003 gnttab_grant_foreign_access_ref(
1004 req->seg[j].gref,
1005 info->xbdev->otherend_id,
1006 pfn_to_mfn(info->shadow[req->id].frame[j]),
Jeremy Fitzhardingea945b982010-11-01 17:03:14 -04001007 rq_data_dir(info->shadow[req->id].request));
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001008 info->shadow[req->id].req = *req;
1009
1010 info->ring.req_prod_pvt++;
1011 }
1012
1013 kfree(copy);
1014
1015 xenbus_switch_state(info->xbdev, XenbusStateConnected);
1016
1017 spin_lock_irq(&blkif_io_lock);
1018
1019 /* Now safe for us to use the shared ring */
1020 info->connected = BLKIF_STATE_CONNECTED;
1021
1022 /* Send off requeued requests */
1023 flush_requests(info);
1024
1025 /* Kick any other new requests queued since we resumed */
1026 kick_pending_request_queues(info);
1027
1028 spin_unlock_irq(&blkif_io_lock);
1029
1030 return 0;
1031}
1032
1033/**
1034 * We are reconnecting to the backend, due to a suspend/resume, or a backend
1035 * driver restart. We tear down our blkif structure and recreate it, but
1036 * leave the device-layer structures intact so that this is transparent to the
1037 * rest of the kernel.
1038 */
1039static int blkfront_resume(struct xenbus_device *dev)
1040{
Greg Kroah-Hartmana1b4b122009-04-30 14:43:31 -07001041 struct blkfront_info *info = dev_get_drvdata(&dev->dev);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001042 int err;
1043
1044 dev_dbg(&dev->dev, "blkfront_resume: %s\n", dev->nodename);
1045
1046 blkif_free(info, info->connected == BLKIF_STATE_CONNECTED);
1047
Ian Campbell203fd612009-12-04 15:33:54 +00001048 err = talk_to_blkback(dev, info);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001049 if (info->connected == BLKIF_STATE_SUSPENDED && !err)
1050 err = blkif_recover(info);
1051
1052 return err;
1053}
1054
Daniel Stoddenb70f5fa2010-04-30 22:01:19 +00001055static void
1056blkfront_closing(struct blkfront_info *info)
1057{
1058 struct xenbus_device *xbdev = info->xbdev;
1059 struct block_device *bdev = NULL;
1060
1061 mutex_lock(&info->mutex);
1062
1063 if (xbdev->state == XenbusStateClosing) {
1064 mutex_unlock(&info->mutex);
1065 return;
1066 }
1067
1068 if (info->gd)
1069 bdev = bdget_disk(info->gd, 0);
1070
1071 mutex_unlock(&info->mutex);
1072
1073 if (!bdev) {
1074 xenbus_frontend_closed(xbdev);
1075 return;
1076 }
1077
1078 mutex_lock(&bdev->bd_mutex);
1079
Daniel Stodden7b32d102010-04-30 22:01:23 +00001080 if (bdev->bd_openers) {
Daniel Stoddenb70f5fa2010-04-30 22:01:19 +00001081 xenbus_dev_error(xbdev, -EBUSY,
1082 "Device in use; refusing to close");
1083 xenbus_switch_state(xbdev, XenbusStateClosing);
1084 } else {
1085 xlvbd_release_gendisk(info);
1086 xenbus_frontend_closed(xbdev);
1087 }
1088
1089 mutex_unlock(&bdev->bd_mutex);
1090 bdput(bdev);
1091}
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001092
1093/*
1094 * Invoked when the backend is finally 'ready' (and has told produced
1095 * the details about the physical device - #sectors, size, etc).
1096 */
1097static void blkfront_connect(struct blkfront_info *info)
1098{
1099 unsigned long long sectors;
1100 unsigned long sector_size;
1101 unsigned int binfo;
1102 int err;
Jeremy Fitzhardinge7901d142010-07-28 10:49:29 -07001103 int barrier;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001104
K. Y. Srinivasan1fa73be2010-03-11 13:42:26 -08001105 switch (info->connected) {
1106 case BLKIF_STATE_CONNECTED:
1107 /*
1108 * Potentially, the back-end may be signalling
1109 * a capacity change; update the capacity.
1110 */
1111 err = xenbus_scanf(XBT_NIL, info->xbdev->otherend,
1112 "sectors", "%Lu", &sectors);
1113 if (XENBUS_EXIST_ERR(err))
1114 return;
1115 printk(KERN_INFO "Setting capacity to %Lu\n",
1116 sectors);
1117 set_capacity(info->gd, sectors);
K. Y. Srinivasan2def1412010-03-18 15:00:54 -07001118 revalidate_disk(info->gd);
K. Y. Srinivasan1fa73be2010-03-11 13:42:26 -08001119
1120 /* fall through */
1121 case BLKIF_STATE_SUSPENDED:
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001122 return;
1123
Jeremy Fitzhardingeb4dddb42010-03-11 15:10:40 -08001124 default:
1125 break;
K. Y. Srinivasan1fa73be2010-03-11 13:42:26 -08001126 }
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001127
1128 dev_dbg(&info->xbdev->dev, "%s:%s.\n",
1129 __func__, info->xbdev->otherend);
1130
1131 err = xenbus_gather(XBT_NIL, info->xbdev->otherend,
1132 "sectors", "%llu", &sectors,
1133 "info", "%u", &binfo,
1134 "sector-size", "%lu", &sector_size,
1135 NULL);
1136 if (err) {
1137 xenbus_dev_fatal(info->xbdev, err,
1138 "reading backend fields at %s",
1139 info->xbdev->otherend);
1140 return;
1141 }
1142
1143 err = xenbus_gather(XBT_NIL, info->xbdev->otherend,
Jeremy Fitzhardinge7901d142010-07-28 10:49:29 -07001144 "feature-barrier", "%lu", &barrier,
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001145 NULL);
Jeremy Fitzhardinge7901d142010-07-28 10:49:29 -07001146
1147 /*
1148 * If there's no "feature-barrier" defined, then it means
1149 * we're dealing with a very old backend which writes
Tejun Heo4913efe2010-09-03 11:56:16 +02001150 * synchronously; nothing to do.
Jeremy Fitzhardinge7901d142010-07-28 10:49:29 -07001151 *
Tejun Heo6958f142010-09-03 11:56:16 +02001152 * If there are barriers, then we use flush.
Jeremy Fitzhardinge7901d142010-07-28 10:49:29 -07001153 */
Tejun Heo4913efe2010-09-03 11:56:16 +02001154 info->feature_flush = 0;
Jens Axboe005a1d12010-10-22 10:58:33 +02001155
Tejun Heo4913efe2010-09-03 11:56:16 +02001156 if (!err && barrier)
Jeremy Fitzhardingebe2f8372010-11-02 10:38:33 -04001157 info->feature_flush = REQ_FLUSH | REQ_FUA;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001158
Chris Lalancette9246b5f2008-09-17 14:30:32 -07001159 err = xlvbd_alloc_gendisk(sectors, info, binfo, sector_size);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001160 if (err) {
1161 xenbus_dev_fatal(info->xbdev, err, "xlvbd_add at %s",
1162 info->xbdev->otherend);
1163 return;
1164 }
1165
1166 xenbus_switch_state(info->xbdev, XenbusStateConnected);
1167
1168 /* Kick pending requests. */
1169 spin_lock_irq(&blkif_io_lock);
1170 info->connected = BLKIF_STATE_CONNECTED;
1171 kick_pending_request_queues(info);
1172 spin_unlock_irq(&blkif_io_lock);
1173
1174 add_disk(info->gd);
Christian Limpach1d78d702008-04-02 10:54:04 -07001175
1176 info->is_ready = 1;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001177}
1178
1179/**
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001180 * Callback received when the backend's state changes.
1181 */
Ian Campbell203fd612009-12-04 15:33:54 +00001182static void blkback_changed(struct xenbus_device *dev,
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001183 enum xenbus_state backend_state)
1184{
Greg Kroah-Hartmana1b4b122009-04-30 14:43:31 -07001185 struct blkfront_info *info = dev_get_drvdata(&dev->dev);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001186
Ian Campbell203fd612009-12-04 15:33:54 +00001187 dev_dbg(&dev->dev, "blkfront:blkback_changed to state %d.\n", backend_state);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001188
1189 switch (backend_state) {
1190 case XenbusStateInitialising:
1191 case XenbusStateInitWait:
1192 case XenbusStateInitialised:
Noboru Iwamatsub78c9512009-10-13 17:22:29 -04001193 case XenbusStateReconfiguring:
1194 case XenbusStateReconfigured:
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001195 case XenbusStateUnknown:
1196 case XenbusStateClosed:
1197 break;
1198
1199 case XenbusStateConnected:
1200 blkfront_connect(info);
1201 break;
1202
1203 case XenbusStateClosing:
Daniel Stoddenb70f5fa2010-04-30 22:01:19 +00001204 blkfront_closing(info);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001205 break;
1206 }
1207}
1208
Daniel Stoddenfa1bd352010-04-30 22:01:22 +00001209static int blkfront_remove(struct xenbus_device *xbdev)
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001210{
Daniel Stoddenfa1bd352010-04-30 22:01:22 +00001211 struct blkfront_info *info = dev_get_drvdata(&xbdev->dev);
1212 struct block_device *bdev = NULL;
1213 struct gendisk *disk;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001214
Daniel Stoddenfa1bd352010-04-30 22:01:22 +00001215 dev_dbg(&xbdev->dev, "%s removed", xbdev->nodename);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001216
1217 blkif_free(info, 0);
1218
Daniel Stoddenfa1bd352010-04-30 22:01:22 +00001219 mutex_lock(&info->mutex);
1220
1221 disk = info->gd;
1222 if (disk)
1223 bdev = bdget_disk(disk, 0);
1224
1225 info->xbdev = NULL;
1226 mutex_unlock(&info->mutex);
1227
1228 if (!bdev) {
Jan Beulich0e345822010-08-07 18:28:55 +02001229 kfree(info);
Daniel Stoddenfa1bd352010-04-30 22:01:22 +00001230 return 0;
1231 }
1232
1233 /*
1234 * The xbdev was removed before we reached the Closed
1235 * state. See if it's safe to remove the disk. If the bdev
1236 * isn't closed yet, we let release take care of it.
1237 */
1238
1239 mutex_lock(&bdev->bd_mutex);
1240 info = disk->private_data;
1241
Daniel Stoddend54142c2010-08-07 18:51:21 +02001242 dev_warn(disk_to_dev(disk),
1243 "%s was hot-unplugged, %d stale handles\n",
1244 xbdev->nodename, bdev->bd_openers);
1245
Daniel Stodden7b32d102010-04-30 22:01:23 +00001246 if (info && !bdev->bd_openers) {
Daniel Stoddenfa1bd352010-04-30 22:01:22 +00001247 xlvbd_release_gendisk(info);
1248 disk->private_data = NULL;
1249 kfree(info);
1250 }
1251
1252 mutex_unlock(&bdev->bd_mutex);
1253 bdput(bdev);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001254
1255 return 0;
1256}
1257
Christian Limpach1d78d702008-04-02 10:54:04 -07001258static int blkfront_is_ready(struct xenbus_device *dev)
1259{
Greg Kroah-Hartmana1b4b122009-04-30 14:43:31 -07001260 struct blkfront_info *info = dev_get_drvdata(&dev->dev);
Christian Limpach1d78d702008-04-02 10:54:04 -07001261
Jan Beulich5d7ed202010-08-07 18:31:12 +02001262 return info->is_ready && info->xbdev;
Christian Limpach1d78d702008-04-02 10:54:04 -07001263}
1264
Al Viroa63c8482008-03-02 10:23:47 -05001265static int blkif_open(struct block_device *bdev, fmode_t mode)
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001266{
Daniel Stodden13961742010-08-07 18:36:53 +02001267 struct gendisk *disk = bdev->bd_disk;
1268 struct blkfront_info *info;
1269 int err = 0;
Arnd Bergmann6e9624b2010-08-07 18:25:34 +02001270
Arnd Bergmann2a48fc02010-06-02 14:28:52 +02001271 mutex_lock(&blkfront_mutex);
Arnd Bergmann6e9624b2010-08-07 18:25:34 +02001272
Daniel Stodden13961742010-08-07 18:36:53 +02001273 info = disk->private_data;
1274 if (!info) {
1275 /* xbdev gone */
1276 err = -ERESTARTSYS;
1277 goto out;
1278 }
1279
1280 mutex_lock(&info->mutex);
1281
1282 if (!info->gd)
1283 /* xbdev is closed */
1284 err = -ERESTARTSYS;
1285
1286 mutex_unlock(&info->mutex);
1287
Daniel Stodden13961742010-08-07 18:36:53 +02001288out:
Arnd Bergmann2a48fc02010-06-02 14:28:52 +02001289 mutex_unlock(&blkfront_mutex);
Daniel Stodden13961742010-08-07 18:36:53 +02001290 return err;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001291}
1292
Al Viroa63c8482008-03-02 10:23:47 -05001293static int blkif_release(struct gendisk *disk, fmode_t mode)
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001294{
Al Viroa63c8482008-03-02 10:23:47 -05001295 struct blkfront_info *info = disk->private_data;
Daniel Stodden7fd152f2010-08-07 18:45:12 +02001296 struct block_device *bdev;
1297 struct xenbus_device *xbdev;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001298
Arnd Bergmann2a48fc02010-06-02 14:28:52 +02001299 mutex_lock(&blkfront_mutex);
Daniel Stodden7fd152f2010-08-07 18:45:12 +02001300
1301 bdev = bdget_disk(disk, 0);
1302 bdput(bdev);
1303
Daniel Stoddenacfca3c2010-08-07 18:47:26 +02001304 if (bdev->bd_openers)
1305 goto out;
1306
Daniel Stodden7fd152f2010-08-07 18:45:12 +02001307 /*
1308 * Check if we have been instructed to close. We will have
1309 * deferred this request, because the bdev was still open.
1310 */
1311
1312 mutex_lock(&info->mutex);
1313 xbdev = info->xbdev;
1314
1315 if (xbdev && xbdev->state == XenbusStateClosing) {
1316 /* pending switch to state closed */
Daniel Stoddend54142c2010-08-07 18:51:21 +02001317 dev_info(disk_to_dev(bdev->bd_disk), "releasing disk\n");
Daniel Stodden7fd152f2010-08-07 18:45:12 +02001318 xlvbd_release_gendisk(info);
1319 xenbus_frontend_closed(info->xbdev);
1320 }
1321
1322 mutex_unlock(&info->mutex);
1323
1324 if (!xbdev) {
1325 /* sudden device removal */
Daniel Stoddend54142c2010-08-07 18:51:21 +02001326 dev_info(disk_to_dev(bdev->bd_disk), "releasing disk\n");
Daniel Stodden7fd152f2010-08-07 18:45:12 +02001327 xlvbd_release_gendisk(info);
1328 disk->private_data = NULL;
1329 kfree(info);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001330 }
Daniel Stodden7fd152f2010-08-07 18:45:12 +02001331
Jens Axboea4cc14e2010-08-08 21:50:05 -04001332out:
Arnd Bergmann2a48fc02010-06-02 14:28:52 +02001333 mutex_unlock(&blkfront_mutex);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001334 return 0;
1335}
1336
Alexey Dobriyan83d5cde2009-09-21 17:01:13 -07001337static const struct block_device_operations xlvbd_block_fops =
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001338{
1339 .owner = THIS_MODULE,
Al Viroa63c8482008-03-02 10:23:47 -05001340 .open = blkif_open,
1341 .release = blkif_release,
Ian Campbell597592d2008-02-21 13:03:45 -08001342 .getgeo = blkif_getgeo,
Arnd Bergmann8a6cfeb2010-07-08 10:18:46 +02001343 .ioctl = blkif_ioctl,
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001344};
1345
1346
Márton Némethec9c42e2010-01-10 13:39:52 +01001347static const struct xenbus_device_id blkfront_ids[] = {
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001348 { "vbd" },
1349 { "" }
1350};
1351
1352static struct xenbus_driver blkfront = {
1353 .name = "vbd",
1354 .owner = THIS_MODULE,
1355 .ids = blkfront_ids,
1356 .probe = blkfront_probe,
1357 .remove = blkfront_remove,
1358 .resume = blkfront_resume,
Ian Campbell203fd612009-12-04 15:33:54 +00001359 .otherend_changed = blkback_changed,
Christian Limpach1d78d702008-04-02 10:54:04 -07001360 .is_ready = blkfront_is_ready,
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001361};
1362
1363static int __init xlblk_init(void)
1364{
Jeremy Fitzhardinge6e833582008-08-19 13:16:17 -07001365 if (!xen_domain())
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001366 return -ENODEV;
1367
1368 if (register_blkdev(XENVBD_MAJOR, DEV_NAME)) {
1369 printk(KERN_WARNING "xen_blk: can't get major %d with name %s\n",
1370 XENVBD_MAJOR, DEV_NAME);
1371 return -ENODEV;
1372 }
1373
1374 return xenbus_register_frontend(&blkfront);
1375}
1376module_init(xlblk_init);
1377
1378
Jan Beulich5a60d0c2008-06-17 10:47:08 +02001379static void __exit xlblk_exit(void)
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001380{
1381 return xenbus_unregister_driver(&blkfront);
1382}
1383module_exit(xlblk_exit);
1384
1385MODULE_DESCRIPTION("Xen virtual block device frontend");
1386MODULE_LICENSE("GPL");
1387MODULE_ALIAS_BLOCKDEV_MAJOR(XENVBD_MAJOR);
Mark McLoughlind2f0c522008-04-02 10:54:05 -07001388MODULE_ALIAS("xen:vbd");
Mark McLoughlin4f93f09b2008-04-02 10:54:06 -07001389MODULE_ALIAS("xenblk");