blob: e4fb3374dcd2aaa6d0f834cd9394564a3c022a38 [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>
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -070047
Jeremy Fitzhardinge1ccbf532009-10-06 15:11:14 -070048#include <xen/xen.h>
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -070049#include <xen/xenbus.h>
50#include <xen/grant_table.h>
51#include <xen/events.h>
52#include <xen/page.h>
Stefano Stabellinic1c54132010-05-14 12:44:30 +010053#include <xen/platform_pci.h>
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -070054
55#include <xen/interface/grant_table.h>
56#include <xen/interface/io/blkif.h>
Markus Armbruster3e334232008-04-02 10:54:02 -070057#include <xen/interface/io/protocols.h>
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -070058
59#include <asm/xen/hypervisor.h>
60
61enum blkif_state {
62 BLKIF_STATE_DISCONNECTED,
63 BLKIF_STATE_CONNECTED,
64 BLKIF_STATE_SUSPENDED,
65};
66
67struct blk_shadow {
68 struct blkif_request req;
Jeremy Fitzhardingea945b982010-11-01 17:03:14 -040069 struct request *request;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -070070 unsigned long frame[BLKIF_MAX_SEGMENTS_PER_REQUEST];
71};
72
Arnd Bergmann2a48fc02010-06-02 14:28:52 +020073static DEFINE_MUTEX(blkfront_mutex);
Alexey Dobriyan83d5cde2009-09-21 17:01:13 -070074static const struct block_device_operations xlvbd_block_fops;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -070075
Jeremy Fitzhardinge667c78af2010-12-08 12:39:12 -080076#define BLK_RING_SIZE __CONST_RING_SIZE(blkif, PAGE_SIZE)
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -070077
78/*
79 * We have one of these per vbd, whether ide, scsi or 'other'. They
80 * hang in private_data off the gendisk structure. We may end up
81 * putting all kinds of interesting stuff here :-)
82 */
83struct blkfront_info
84{
Steven Noonan34678112012-02-17 12:04:44 -080085 spinlock_t io_lock;
Daniel Stoddenb70f5fa2010-04-30 22:01:19 +000086 struct mutex mutex;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -070087 struct xenbus_device *xbdev;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -070088 struct gendisk *gd;
89 int vdevice;
90 blkif_vdev_t handle;
91 enum blkif_state connected;
92 int ring_ref;
93 struct blkif_front_ring ring;
Jens Axboe9e973e62009-02-24 08:10:09 +010094 struct scatterlist sg[BLKIF_MAX_SEGMENTS_PER_REQUEST];
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -070095 unsigned int evtchn, irq;
96 struct request_queue *rq;
97 struct work_struct work;
98 struct gnttab_free_callback callback;
99 struct blk_shadow shadow[BLK_RING_SIZE];
100 unsigned long shadow_free;
Tejun Heo4913efe2010-09-03 11:56:16 +0200101 unsigned int feature_flush;
Konrad Rzeszutek Wilkedf6ef52011-05-03 12:01:11 -0400102 unsigned int flush_op;
Konrad Rzeszutek Wilk5ea42982011-10-12 16:23:30 -0400103 unsigned int feature_discard:1;
104 unsigned int feature_secdiscard:1;
Li Dongyanged30bf32011-09-01 18:39:09 +0800105 unsigned int discard_granularity;
106 unsigned int discard_alignment;
Christian Limpach1d78d702008-04-02 10:54:04 -0700107 int is_ready;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700108};
109
Jan Beulich0e345822010-08-07 18:28:55 +0200110static unsigned int nr_minors;
111static unsigned long *minors;
112static DEFINE_SPINLOCK(minor_lock);
113
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700114#define MAXIMUM_OUTSTANDING_BLOCK_REQS \
115 (BLKIF_MAX_SEGMENTS_PER_REQUEST * BLK_RING_SIZE)
116#define GRANT_INVALID_REF 0
117
118#define PARTS_PER_DISK 16
Chris Lalancette9246b5f2008-09-17 14:30:32 -0700119#define PARTS_PER_EXT_DISK 256
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700120
121#define BLKIF_MAJOR(dev) ((dev)>>8)
122#define BLKIF_MINOR(dev) ((dev) & 0xff)
123
Chris Lalancette9246b5f2008-09-17 14:30:32 -0700124#define EXT_SHIFT 28
125#define EXTENDED (1<<EXT_SHIFT)
126#define VDEV_IS_EXTENDED(dev) ((dev)&(EXTENDED))
127#define BLKIF_MINOR_EXT(dev) ((dev)&(~EXTENDED))
Stefano Stabellinic80a4202010-12-02 17:55:00 +0000128#define EMULATED_HD_DISK_MINOR_OFFSET (0)
129#define EMULATED_HD_DISK_NAME_OFFSET (EMULATED_HD_DISK_MINOR_OFFSET / 256)
Stefan Bader196cfe22011-07-14 15:30:22 +0200130#define EMULATED_SD_DISK_MINOR_OFFSET (0)
131#define EMULATED_SD_DISK_NAME_OFFSET (EMULATED_SD_DISK_MINOR_OFFSET / 256)
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700132
Chris Lalancette9246b5f2008-09-17 14:30:32 -0700133#define DEV_NAME "xvd" /* name in /dev */
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700134
135static int get_id_from_freelist(struct blkfront_info *info)
136{
137 unsigned long free = info->shadow_free;
Roel Kluinb9ed7252009-05-22 09:25:32 +0200138 BUG_ON(free >= BLK_RING_SIZE);
Konrad Rzeszutek Wilk97e36832011-10-12 12:12:36 -0400139 info->shadow_free = info->shadow[free].req.u.rw.id;
140 info->shadow[free].req.u.rw.id = 0x0fffffee; /* debug */
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700141 return free;
142}
143
Konrad Rzeszutek Wilk6878c322012-05-25 17:34:51 -0400144static int add_id_to_freelist(struct blkfront_info *info,
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700145 unsigned long id)
146{
Konrad Rzeszutek Wilk6878c322012-05-25 17:34:51 -0400147 if (info->shadow[id].req.u.rw.id != id)
148 return -EINVAL;
149 if (info->shadow[id].request == NULL)
150 return -EINVAL;
Konrad Rzeszutek Wilk97e36832011-10-12 12:12:36 -0400151 info->shadow[id].req.u.rw.id = info->shadow_free;
Jeremy Fitzhardingea945b982010-11-01 17:03:14 -0400152 info->shadow[id].request = NULL;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700153 info->shadow_free = id;
Konrad Rzeszutek Wilk6878c322012-05-25 17:34:51 -0400154 return 0;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700155}
156
Konrad Rzeszutek Wilk6878c322012-05-25 17:34:51 -0400157static const char *op_name(int op)
158{
159 static const char *const names[] = {
160 [BLKIF_OP_READ] = "read",
161 [BLKIF_OP_WRITE] = "write",
162 [BLKIF_OP_WRITE_BARRIER] = "barrier",
163 [BLKIF_OP_FLUSH_DISKCACHE] = "flush",
164 [BLKIF_OP_DISCARD] = "discard" };
165
166 if (op < 0 || op >= ARRAY_SIZE(names))
167 return "unknown";
168
169 if (!names[op])
170 return "reserved";
171
172 return names[op];
173}
Jan Beulich0e345822010-08-07 18:28:55 +0200174static int xlbd_reserve_minors(unsigned int minor, unsigned int nr)
175{
176 unsigned int end = minor + nr;
177 int rc;
178
179 if (end > nr_minors) {
180 unsigned long *bitmap, *old;
181
Thomas Meyerf0941482011-11-29 22:08:00 +0100182 bitmap = kcalloc(BITS_TO_LONGS(end), sizeof(*bitmap),
Jan Beulich0e345822010-08-07 18:28:55 +0200183 GFP_KERNEL);
184 if (bitmap == NULL)
185 return -ENOMEM;
186
187 spin_lock(&minor_lock);
188 if (end > nr_minors) {
189 old = minors;
190 memcpy(bitmap, minors,
191 BITS_TO_LONGS(nr_minors) * sizeof(*bitmap));
192 minors = bitmap;
193 nr_minors = BITS_TO_LONGS(end) * BITS_PER_LONG;
194 } else
195 old = bitmap;
196 spin_unlock(&minor_lock);
197 kfree(old);
198 }
199
200 spin_lock(&minor_lock);
201 if (find_next_bit(minors, end, minor) >= end) {
Akinobu Mita34ae2e42012-01-21 00:15:26 +0900202 bitmap_set(minors, minor, nr);
Jan Beulich0e345822010-08-07 18:28:55 +0200203 rc = 0;
204 } else
205 rc = -EBUSY;
206 spin_unlock(&minor_lock);
207
208 return rc;
209}
210
211static void xlbd_release_minors(unsigned int minor, unsigned int nr)
212{
213 unsigned int end = minor + nr;
214
215 BUG_ON(end > nr_minors);
216 spin_lock(&minor_lock);
Akinobu Mita34ae2e42012-01-21 00:15:26 +0900217 bitmap_clear(minors, minor, nr);
Jan Beulich0e345822010-08-07 18:28:55 +0200218 spin_unlock(&minor_lock);
219}
220
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700221static void blkif_restart_queue_callback(void *arg)
222{
223 struct blkfront_info *info = (struct blkfront_info *)arg;
224 schedule_work(&info->work);
225}
226
Harvey Harrisonafe42d72008-04-29 00:59:47 -0700227static int blkif_getgeo(struct block_device *bd, struct hd_geometry *hg)
Ian Campbell597592d2008-02-21 13:03:45 -0800228{
229 /* We don't have real geometry info, but let's at least return
230 values consistent with the size of the device */
231 sector_t nsect = get_capacity(bd->bd_disk);
232 sector_t cylinders = nsect;
233
234 hg->heads = 0xff;
235 hg->sectors = 0x3f;
236 sector_div(cylinders, hg->heads * hg->sectors);
237 hg->cylinders = cylinders;
238 if ((sector_t)(hg->cylinders + 1) * hg->heads * hg->sectors < nsect)
239 hg->cylinders = 0xffff;
240 return 0;
241}
242
Al Viroa63c8482008-03-02 10:23:47 -0500243static int blkif_ioctl(struct block_device *bdev, fmode_t mode,
Adrian Bunk62aa0052008-08-04 11:59:05 +0200244 unsigned command, unsigned long argument)
Christian Limpach440a01a2008-06-17 10:47:08 +0200245{
Al Viroa63c8482008-03-02 10:23:47 -0500246 struct blkfront_info *info = bdev->bd_disk->private_data;
Christian Limpach440a01a2008-06-17 10:47:08 +0200247 int i;
248
249 dev_dbg(&info->xbdev->dev, "command: 0x%x, argument: 0x%lx\n",
250 command, (long)argument);
251
252 switch (command) {
253 case CDROMMULTISESSION:
254 dev_dbg(&info->xbdev->dev, "FIXME: support multisession CDs later\n");
255 for (i = 0; i < sizeof(struct cdrom_multisession); i++)
256 if (put_user(0, (char __user *)(argument + i)))
257 return -EFAULT;
258 return 0;
259
260 case CDROM_GET_CAPABILITY: {
261 struct gendisk *gd = info->gd;
262 if (gd->flags & GENHD_FL_CD)
263 return 0;
264 return -EINVAL;
265 }
266
267 default:
268 /*printk(KERN_ALERT "ioctl %08x not supported by Xen blkdev\n",
269 command);*/
270 return -EINVAL; /* same return as native Linux */
271 }
272
273 return 0;
274}
275
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700276/*
Jeremy Fitzhardingec64e38e2010-11-01 14:32:27 -0400277 * Generate a Xen blkfront IO request from a blk layer request. Reads
Konrad Rzeszutek Wilkedf6ef52011-05-03 12:01:11 -0400278 * and writes are handled as expected.
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700279 *
Jeremy Fitzhardingec64e38e2010-11-01 14:32:27 -0400280 * @req: a request struct
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700281 */
282static int blkif_queue_request(struct request *req)
283{
284 struct blkfront_info *info = req->rq_disk->private_data;
285 unsigned long buffer_mfn;
286 struct blkif_request *ring_req;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700287 unsigned long id;
288 unsigned int fsect, lsect;
Jens Axboe9e973e62009-02-24 08:10:09 +0100289 int i, ref;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700290 grant_ref_t gref_head;
Jens Axboe9e973e62009-02-24 08:10:09 +0100291 struct scatterlist *sg;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700292
293 if (unlikely(info->connected != BLKIF_STATE_CONNECTED))
294 return 1;
295
296 if (gnttab_alloc_grant_references(
297 BLKIF_MAX_SEGMENTS_PER_REQUEST, &gref_head) < 0) {
298 gnttab_request_free_callback(
299 &info->callback,
300 blkif_restart_queue_callback,
301 info,
302 BLKIF_MAX_SEGMENTS_PER_REQUEST);
303 return 1;
304 }
305
306 /* Fill out a communications ring structure. */
307 ring_req = RING_GET_REQUEST(&info->ring, info->ring.req_prod_pvt);
308 id = get_id_from_freelist(info);
Jeremy Fitzhardingea945b982010-11-01 17:03:14 -0400309 info->shadow[id].request = req;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700310
Konrad Rzeszutek Wilk97e36832011-10-12 12:12:36 -0400311 ring_req->u.rw.id = id;
Owen Smith51de6952010-12-22 15:05:00 +0000312 ring_req->u.rw.sector_number = (blkif_sector_t)blk_rq_pos(req);
Konrad Rzeszutek Wilk97e36832011-10-12 12:12:36 -0400313 ring_req->u.rw.handle = info->handle;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700314
315 ring_req->operation = rq_data_dir(req) ?
316 BLKIF_OP_WRITE : BLKIF_OP_READ;
Jeremy Fitzhardingebe2f8372010-11-02 10:38:33 -0400317
318 if (req->cmd_flags & (REQ_FLUSH | REQ_FUA)) {
319 /*
Konrad Rzeszutek Wilkedf6ef52011-05-03 12:01:11 -0400320 * Ideally we can do an unordered flush-to-disk. In case the
321 * backend onlysupports barriers, use that. A barrier request
Jeremy Fitzhardingebe2f8372010-11-02 10:38:33 -0400322 * a superset of FUA, so we can implement it the same
323 * way. (It's also a FLUSH+FUA, since it is
324 * guaranteed ordered WRT previous writes.)
325 */
Konrad Rzeszutek Wilkedf6ef52011-05-03 12:01:11 -0400326 ring_req->operation = info->flush_op;
Jeremy Fitzhardingebe2f8372010-11-02 10:38:33 -0400327 }
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700328
Konrad Rzeszutek Wilk5ea42982011-10-12 16:23:30 -0400329 if (unlikely(req->cmd_flags & (REQ_DISCARD | REQ_SECURE))) {
Li Dongyanged30bf32011-09-01 18:39:09 +0800330 /* id, sector_number and handle are set above. */
331 ring_req->operation = BLKIF_OP_DISCARD;
Li Dongyanged30bf32011-09-01 18:39:09 +0800332 ring_req->u.discard.nr_sectors = blk_rq_sectors(req);
Konrad Rzeszutek Wilk5ea42982011-10-12 16:23:30 -0400333 if ((req->cmd_flags & REQ_SECURE) && info->feature_secdiscard)
334 ring_req->u.discard.flag = BLKIF_DISCARD_SECURE;
335 else
336 ring_req->u.discard.flag = 0;
Li Dongyanged30bf32011-09-01 18:39:09 +0800337 } else {
Konrad Rzeszutek Wilk97e36832011-10-12 12:12:36 -0400338 ring_req->u.rw.nr_segments = blk_rq_map_sg(req->q, req,
339 info->sg);
340 BUG_ON(ring_req->u.rw.nr_segments >
341 BLKIF_MAX_SEGMENTS_PER_REQUEST);
Jens Axboe9e973e62009-02-24 08:10:09 +0100342
Konrad Rzeszutek Wilk97e36832011-10-12 12:12:36 -0400343 for_each_sg(info->sg, sg, ring_req->u.rw.nr_segments, i) {
Li Dongyanged30bf32011-09-01 18:39:09 +0800344 buffer_mfn = pfn_to_mfn(page_to_pfn(sg_page(sg)));
345 fsect = sg->offset >> 9;
346 lsect = fsect + (sg->length >> 9) - 1;
347 /* install a grant reference. */
348 ref = gnttab_claim_grant_reference(&gref_head);
349 BUG_ON(ref == -ENOSPC);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700350
Li Dongyanged30bf32011-09-01 18:39:09 +0800351 gnttab_grant_foreign_access_ref(
352 ref,
353 info->xbdev->otherend_id,
354 buffer_mfn,
355 rq_data_dir(req));
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700356
Li Dongyanged30bf32011-09-01 18:39:09 +0800357 info->shadow[id].frame[i] = mfn_to_pfn(buffer_mfn);
358 ring_req->u.rw.seg[i] =
359 (struct blkif_request_segment) {
360 .gref = ref,
361 .first_sect = fsect,
362 .last_sect = lsect };
363 }
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700364 }
365
366 info->ring.req_prod_pvt++;
367
368 /* Keep a private copy so we can reissue requests when recovering. */
369 info->shadow[id].req = *ring_req;
370
371 gnttab_free_grant_references(gref_head);
372
373 return 0;
374}
375
376
377static inline void flush_requests(struct blkfront_info *info)
378{
379 int notify;
380
381 RING_PUSH_REQUESTS_AND_CHECK_NOTIFY(&info->ring, notify);
382
383 if (notify)
384 notify_remote_via_irq(info->irq);
385}
386
387/*
388 * do_blkif_request
389 * read a block; request is in a request queue
390 */
Jens Axboe165125e2007-07-24 09:28:11 +0200391static void do_blkif_request(struct request_queue *rq)
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700392{
393 struct blkfront_info *info = NULL;
394 struct request *req;
395 int queued;
396
397 pr_debug("Entered do_blkif_request\n");
398
399 queued = 0;
400
Tejun Heo9934c8c2009-05-08 11:54:16 +0900401 while ((req = blk_peek_request(rq)) != NULL) {
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700402 info = req->rq_disk->private_data;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700403
404 if (RING_FULL(&info->ring))
405 goto wait;
406
Tejun Heo9934c8c2009-05-08 11:54:16 +0900407 blk_start_request(req);
Tejun Heo296b2f62009-05-08 11:54:15 +0900408
Konrad Rzeszutek Wilkd11e6152011-09-16 15:15:14 -0400409 if ((req->cmd_type != REQ_TYPE_FS) ||
410 ((req->cmd_flags & (REQ_FLUSH | REQ_FUA)) &&
411 !info->flush_op)) {
Tejun Heo296b2f62009-05-08 11:54:15 +0900412 __blk_end_request_all(req, -EIO);
413 continue;
414 }
415
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700416 pr_debug("do_blk_req %p: cmd %p, sec %lx, "
Tejun Heo83096eb2009-05-07 22:24:39 +0900417 "(%u/%u) buffer:%p [%s]\n",
418 req, req->cmd, (unsigned long)blk_rq_pos(req),
419 blk_rq_cur_sectors(req), blk_rq_sectors(req),
420 req->buffer, rq_data_dir(req) ? "write" : "read");
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700421
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700422 if (blkif_queue_request(req)) {
423 blk_requeue_request(rq, req);
424wait:
425 /* Avoid pointless unplugs. */
426 blk_stop_queue(rq);
427 break;
428 }
429
430 queued++;
431 }
432
433 if (queued != 0)
434 flush_requests(info);
435}
436
437static int xlvbd_init_blk_queue(struct gendisk *gd, u16 sector_size)
438{
Jens Axboe165125e2007-07-24 09:28:11 +0200439 struct request_queue *rq;
Li Dongyanged30bf32011-09-01 18:39:09 +0800440 struct blkfront_info *info = gd->private_data;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700441
Steven Noonan34678112012-02-17 12:04:44 -0800442 rq = blk_init_queue(do_blkif_request, &info->io_lock);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700443 if (rq == NULL)
444 return -1;
445
Fernando Luis Vázquez Cao66d352e2008-10-27 18:45:54 +0900446 queue_flag_set_unlocked(QUEUE_FLAG_VIRT, rq);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700447
Li Dongyanged30bf32011-09-01 18:39:09 +0800448 if (info->feature_discard) {
449 queue_flag_set_unlocked(QUEUE_FLAG_DISCARD, rq);
450 blk_queue_max_discard_sectors(rq, get_capacity(gd));
451 rq->limits.discard_granularity = info->discard_granularity;
452 rq->limits.discard_alignment = info->discard_alignment;
Konrad Rzeszutek Wilk5ea42982011-10-12 16:23:30 -0400453 if (info->feature_secdiscard)
454 queue_flag_set_unlocked(QUEUE_FLAG_SECDISCARD, rq);
Li Dongyanged30bf32011-09-01 18:39:09 +0800455 }
456
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700457 /* Hard sector size and max sectors impersonate the equiv. hardware. */
Martin K. Petersene1defc42009-05-22 17:17:49 -0400458 blk_queue_logical_block_size(rq, sector_size);
Martin K. Petersen086fa5f2010-02-26 00:20:38 -0500459 blk_queue_max_hw_sectors(rq, 512);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700460
461 /* Each segment in a request is up to an aligned page in size. */
462 blk_queue_segment_boundary(rq, PAGE_SIZE - 1);
463 blk_queue_max_segment_size(rq, PAGE_SIZE);
464
465 /* Ensure a merged request will fit in a single I/O ring slot. */
Martin K. Petersen8a783622010-02-26 00:20:39 -0500466 blk_queue_max_segments(rq, BLKIF_MAX_SEGMENTS_PER_REQUEST);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700467
468 /* Make sure buffer addresses are sector-aligned. */
469 blk_queue_dma_alignment(rq, 511);
470
Ian Campbell1c91fe12008-06-17 10:47:08 +0200471 /* Make sure we don't use bounce buffers. */
472 blk_queue_bounce_limit(rq, BLK_BOUNCE_ANY);
473
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700474 gd->queue = rq;
475
476 return 0;
477}
478
479
Tejun Heo4913efe2010-09-03 11:56:16 +0200480static void xlvbd_flush(struct blkfront_info *info)
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700481{
Tejun Heo4913efe2010-09-03 11:56:16 +0200482 blk_queue_flush(info->rq, info->feature_flush);
Konrad Rzeszutek Wilkedf6ef52011-05-03 12:01:11 -0400483 printk(KERN_INFO "blkfront: %s: %s: %s\n",
Tejun Heo4913efe2010-09-03 11:56:16 +0200484 info->gd->disk_name,
Konrad Rzeszutek Wilkedf6ef52011-05-03 12:01:11 -0400485 info->flush_op == BLKIF_OP_WRITE_BARRIER ?
486 "barrier" : (info->flush_op == BLKIF_OP_FLUSH_DISKCACHE ?
487 "flush diskcache" : "barrier or flush"),
Tejun Heo4913efe2010-09-03 11:56:16 +0200488 info->feature_flush ? "enabled" : "disabled");
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700489}
490
Stefano Stabellinic80a4202010-12-02 17:55:00 +0000491static int xen_translate_vdev(int vdevice, int *minor, unsigned int *offset)
492{
493 int major;
494 major = BLKIF_MAJOR(vdevice);
495 *minor = BLKIF_MINOR(vdevice);
496 switch (major) {
497 case XEN_IDE0_MAJOR:
498 *offset = (*minor / 64) + EMULATED_HD_DISK_NAME_OFFSET;
499 *minor = ((*minor / 64) * PARTS_PER_DISK) +
500 EMULATED_HD_DISK_MINOR_OFFSET;
501 break;
502 case XEN_IDE1_MAJOR:
503 *offset = (*minor / 64) + 2 + EMULATED_HD_DISK_NAME_OFFSET;
504 *minor = (((*minor / 64) + 2) * PARTS_PER_DISK) +
505 EMULATED_HD_DISK_MINOR_OFFSET;
506 break;
507 case XEN_SCSI_DISK0_MAJOR:
508 *offset = (*minor / PARTS_PER_DISK) + EMULATED_SD_DISK_NAME_OFFSET;
509 *minor = *minor + EMULATED_SD_DISK_MINOR_OFFSET;
510 break;
511 case XEN_SCSI_DISK1_MAJOR:
512 case XEN_SCSI_DISK2_MAJOR:
513 case XEN_SCSI_DISK3_MAJOR:
514 case XEN_SCSI_DISK4_MAJOR:
515 case XEN_SCSI_DISK5_MAJOR:
516 case XEN_SCSI_DISK6_MAJOR:
517 case XEN_SCSI_DISK7_MAJOR:
518 *offset = (*minor / PARTS_PER_DISK) +
519 ((major - XEN_SCSI_DISK1_MAJOR + 1) * 16) +
520 EMULATED_SD_DISK_NAME_OFFSET;
521 *minor = *minor +
522 ((major - XEN_SCSI_DISK1_MAJOR + 1) * 16 * PARTS_PER_DISK) +
523 EMULATED_SD_DISK_MINOR_OFFSET;
524 break;
525 case XEN_SCSI_DISK8_MAJOR:
526 case XEN_SCSI_DISK9_MAJOR:
527 case XEN_SCSI_DISK10_MAJOR:
528 case XEN_SCSI_DISK11_MAJOR:
529 case XEN_SCSI_DISK12_MAJOR:
530 case XEN_SCSI_DISK13_MAJOR:
531 case XEN_SCSI_DISK14_MAJOR:
532 case XEN_SCSI_DISK15_MAJOR:
533 *offset = (*minor / PARTS_PER_DISK) +
534 ((major - XEN_SCSI_DISK8_MAJOR + 8) * 16) +
535 EMULATED_SD_DISK_NAME_OFFSET;
536 *minor = *minor +
537 ((major - XEN_SCSI_DISK8_MAJOR + 8) * 16 * PARTS_PER_DISK) +
538 EMULATED_SD_DISK_MINOR_OFFSET;
539 break;
540 case XENVBD_MAJOR:
541 *offset = *minor / PARTS_PER_DISK;
542 break;
543 default:
544 printk(KERN_WARNING "blkfront: your disk configuration is "
545 "incorrect, please use an xvd device instead\n");
546 return -ENODEV;
547 }
548 return 0;
549}
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700550
Jan Beuliche77c78c2012-04-05 16:37:22 +0100551static char *encode_disk_name(char *ptr, unsigned int n)
552{
553 if (n >= 26)
554 ptr = encode_disk_name(ptr, n / 26 - 1);
555 *ptr = 'a' + n % 26;
556 return ptr + 1;
557}
558
Chris Lalancette9246b5f2008-09-17 14:30:32 -0700559static int xlvbd_alloc_gendisk(blkif_sector_t capacity,
560 struct blkfront_info *info,
561 u16 vdisk_info, u16 sector_size)
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700562{
563 struct gendisk *gd;
564 int nr_minors = 1;
Stefano Stabellinic80a4202010-12-02 17:55:00 +0000565 int err;
Chris Lalancette9246b5f2008-09-17 14:30:32 -0700566 unsigned int offset;
567 int minor;
568 int nr_parts;
Jan Beuliche77c78c2012-04-05 16:37:22 +0100569 char *ptr;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700570
571 BUG_ON(info->gd != NULL);
572 BUG_ON(info->rq != NULL);
573
Chris Lalancette9246b5f2008-09-17 14:30:32 -0700574 if ((info->vdevice>>EXT_SHIFT) > 1) {
575 /* this is above the extended range; something is wrong */
576 printk(KERN_WARNING "blkfront: vdevice 0x%x is above the extended range; ignoring\n", info->vdevice);
577 return -ENODEV;
578 }
579
580 if (!VDEV_IS_EXTENDED(info->vdevice)) {
Stefano Stabellinic80a4202010-12-02 17:55:00 +0000581 err = xen_translate_vdev(info->vdevice, &minor, &offset);
582 if (err)
583 return err;
584 nr_parts = PARTS_PER_DISK;
Chris Lalancette9246b5f2008-09-17 14:30:32 -0700585 } else {
586 minor = BLKIF_MINOR_EXT(info->vdevice);
587 nr_parts = PARTS_PER_EXT_DISK;
Stefano Stabellinic80a4202010-12-02 17:55:00 +0000588 offset = minor / nr_parts;
Stefan Bader89153b52011-07-14 15:30:37 +0200589 if (xen_hvm_domain() && offset < EMULATED_HD_DISK_NAME_OFFSET + 4)
Stefano Stabellinic80a4202010-12-02 17:55:00 +0000590 printk(KERN_WARNING "blkfront: vdevice 0x%x might conflict with "
591 "emulated IDE disks,\n\t choose an xvd device name"
592 "from xvde on\n", info->vdevice);
Chris Lalancette9246b5f2008-09-17 14:30:32 -0700593 }
Jan Beuliche77c78c2012-04-05 16:37:22 +0100594 if (minor >> MINORBITS) {
595 pr_warn("blkfront: %#x's minor (%#x) out of range; ignoring\n",
596 info->vdevice, minor);
597 return -ENODEV;
598 }
Chris Lalancette9246b5f2008-09-17 14:30:32 -0700599
600 if ((minor % nr_parts) == 0)
601 nr_minors = nr_parts;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700602
Jan Beulich0e345822010-08-07 18:28:55 +0200603 err = xlbd_reserve_minors(minor, nr_minors);
604 if (err)
605 goto out;
606 err = -ENODEV;
607
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700608 gd = alloc_disk(nr_minors);
609 if (gd == NULL)
Jan Beulich0e345822010-08-07 18:28:55 +0200610 goto release;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700611
Jan Beuliche77c78c2012-04-05 16:37:22 +0100612 strcpy(gd->disk_name, DEV_NAME);
613 ptr = encode_disk_name(gd->disk_name + sizeof(DEV_NAME) - 1, offset);
614 BUG_ON(ptr >= gd->disk_name + DISK_NAME_LEN);
615 if (nr_minors > 1)
616 *ptr = 0;
617 else
618 snprintf(ptr, gd->disk_name + DISK_NAME_LEN - ptr,
619 "%d", minor & (nr_parts - 1));
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700620
621 gd->major = XENVBD_MAJOR;
622 gd->first_minor = minor;
623 gd->fops = &xlvbd_block_fops;
624 gd->private_data = info;
625 gd->driverfs_dev = &(info->xbdev->dev);
626 set_capacity(gd, capacity);
627
628 if (xlvbd_init_blk_queue(gd, sector_size)) {
629 del_gendisk(gd);
Jan Beulich0e345822010-08-07 18:28:55 +0200630 goto release;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700631 }
632
633 info->rq = gd->queue;
634 info->gd = gd;
635
Tejun Heo4913efe2010-09-03 11:56:16 +0200636 xlvbd_flush(info);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700637
638 if (vdisk_info & VDISK_READONLY)
639 set_disk_ro(gd, 1);
640
641 if (vdisk_info & VDISK_REMOVABLE)
642 gd->flags |= GENHD_FL_REMOVABLE;
643
644 if (vdisk_info & VDISK_CDROM)
645 gd->flags |= GENHD_FL_CD;
646
647 return 0;
648
Jan Beulich0e345822010-08-07 18:28:55 +0200649 release:
650 xlbd_release_minors(minor, nr_minors);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700651 out:
652 return err;
653}
654
Daniel Stoddena66b5ae2010-08-07 18:33:17 +0200655static void xlvbd_release_gendisk(struct blkfront_info *info)
656{
657 unsigned int minor, nr_minors;
658 unsigned long flags;
659
660 if (info->rq == NULL)
661 return;
662
Steven Noonan34678112012-02-17 12:04:44 -0800663 spin_lock_irqsave(&info->io_lock, flags);
Daniel Stoddena66b5ae2010-08-07 18:33:17 +0200664
665 /* No more blkif_request(). */
666 blk_stop_queue(info->rq);
667
668 /* No more gnttab callback work. */
669 gnttab_cancel_free_callback(&info->callback);
Steven Noonan34678112012-02-17 12:04:44 -0800670 spin_unlock_irqrestore(&info->io_lock, flags);
Daniel Stoddena66b5ae2010-08-07 18:33:17 +0200671
672 /* Flush gnttab callback work. Must be done with no locks held. */
Tejun Heo30d65032010-12-24 15:59:06 +0100673 flush_work_sync(&info->work);
Daniel Stoddena66b5ae2010-08-07 18:33:17 +0200674
675 del_gendisk(info->gd);
676
677 minor = info->gd->first_minor;
678 nr_minors = info->gd->minors;
679 xlbd_release_minors(minor, nr_minors);
680
681 blk_cleanup_queue(info->rq);
682 info->rq = NULL;
683
684 put_disk(info->gd);
685 info->gd = NULL;
686}
687
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700688static void kick_pending_request_queues(struct blkfront_info *info)
689{
690 if (!RING_FULL(&info->ring)) {
691 /* Re-enable calldowns. */
692 blk_start_queue(info->rq);
693 /* Kick things off immediately. */
694 do_blkif_request(info->rq);
695 }
696}
697
698static void blkif_restart_queue(struct work_struct *work)
699{
700 struct blkfront_info *info = container_of(work, struct blkfront_info, work);
701
Steven Noonan34678112012-02-17 12:04:44 -0800702 spin_lock_irq(&info->io_lock);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700703 if (info->connected == BLKIF_STATE_CONNECTED)
704 kick_pending_request_queues(info);
Steven Noonan34678112012-02-17 12:04:44 -0800705 spin_unlock_irq(&info->io_lock);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700706}
707
708static void blkif_free(struct blkfront_info *info, int suspend)
709{
710 /* Prevent new requests being issued until we fix things up. */
Steven Noonan34678112012-02-17 12:04:44 -0800711 spin_lock_irq(&info->io_lock);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700712 info->connected = suspend ?
713 BLKIF_STATE_SUSPENDED : BLKIF_STATE_DISCONNECTED;
714 /* No more blkif_request(). */
715 if (info->rq)
716 blk_stop_queue(info->rq);
717 /* No more gnttab callback work. */
718 gnttab_cancel_free_callback(&info->callback);
Steven Noonan34678112012-02-17 12:04:44 -0800719 spin_unlock_irq(&info->io_lock);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700720
721 /* Flush gnttab callback work. Must be done with no locks held. */
Tejun Heo30d65032010-12-24 15:59:06 +0100722 flush_work_sync(&info->work);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700723
724 /* Free resources associated with old device channel. */
725 if (info->ring_ref != GRANT_INVALID_REF) {
726 gnttab_end_foreign_access(info->ring_ref, 0,
727 (unsigned long)info->ring.sring);
728 info->ring_ref = GRANT_INVALID_REF;
729 info->ring.sring = NULL;
730 }
731 if (info->irq)
732 unbind_from_irqhandler(info->irq, info);
733 info->evtchn = info->irq = 0;
734
735}
736
737static void blkif_completion(struct blk_shadow *s)
738{
739 int i;
Konrad Rzeszutek Wilk5ea42982011-10-12 16:23:30 -0400740 /* Do not let BLKIF_OP_DISCARD as nr_segment is in the same place
741 * flag. */
Konrad Rzeszutek Wilk97e36832011-10-12 12:12:36 -0400742 for (i = 0; i < s->req.u.rw.nr_segments; i++)
Owen Smith51de6952010-12-22 15:05:00 +0000743 gnttab_end_foreign_access(s->req.u.rw.seg[i].gref, 0, 0UL);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700744}
745
746static irqreturn_t blkif_interrupt(int irq, void *dev_id)
747{
748 struct request *req;
749 struct blkif_response *bret;
750 RING_IDX i, rp;
751 unsigned long flags;
752 struct blkfront_info *info = (struct blkfront_info *)dev_id;
Kiyoshi Uedaf530f0362007-12-11 17:47:36 -0500753 int error;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700754
Steven Noonan34678112012-02-17 12:04:44 -0800755 spin_lock_irqsave(&info->io_lock, flags);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700756
757 if (unlikely(info->connected != BLKIF_STATE_CONNECTED)) {
Steven Noonan34678112012-02-17 12:04:44 -0800758 spin_unlock_irqrestore(&info->io_lock, flags);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700759 return IRQ_HANDLED;
760 }
761
762 again:
763 rp = info->ring.sring->rsp_prod;
764 rmb(); /* Ensure we see queued responses up to 'rp'. */
765
766 for (i = info->ring.rsp_cons; i != rp; i++) {
767 unsigned long id;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700768
769 bret = RING_GET_RESPONSE(&info->ring, i);
770 id = bret->id;
Konrad Rzeszutek Wilk6878c322012-05-25 17:34:51 -0400771 /*
772 * The backend has messed up and given us an id that we would
773 * never have given to it (we stamp it up to BLK_RING_SIZE -
774 * look in get_id_from_freelist.
775 */
776 if (id >= BLK_RING_SIZE) {
777 WARN(1, "%s: response to %s has incorrect id (%ld)\n",
778 info->gd->disk_name, op_name(bret->operation), id);
779 /* We can't safely get the 'struct request' as
780 * the id is busted. */
781 continue;
782 }
Jeremy Fitzhardingea945b982010-11-01 17:03:14 -0400783 req = info->shadow[id].request;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700784
Konrad Rzeszutek Wilk5ea42982011-10-12 16:23:30 -0400785 if (bret->operation != BLKIF_OP_DISCARD)
786 blkif_completion(&info->shadow[id]);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700787
Konrad Rzeszutek Wilk6878c322012-05-25 17:34:51 -0400788 if (add_id_to_freelist(info, id)) {
789 WARN(1, "%s: response to %s (id %ld) couldn't be recycled!\n",
790 info->gd->disk_name, op_name(bret->operation), id);
791 continue;
792 }
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700793
Kiyoshi Uedaf530f0362007-12-11 17:47:36 -0500794 error = (bret->status == BLKIF_RSP_OKAY) ? 0 : -EIO;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700795 switch (bret->operation) {
Li Dongyanged30bf32011-09-01 18:39:09 +0800796 case BLKIF_OP_DISCARD:
797 if (unlikely(bret->status == BLKIF_RSP_EOPNOTSUPP)) {
798 struct request_queue *rq = info->rq;
Konrad Rzeszutek Wilk6878c322012-05-25 17:34:51 -0400799 printk(KERN_WARNING "blkfront: %s: %s op failed\n",
800 info->gd->disk_name, op_name(bret->operation));
Li Dongyanged30bf32011-09-01 18:39:09 +0800801 error = -EOPNOTSUPP;
802 info->feature_discard = 0;
Konrad Rzeszutek Wilk5ea42982011-10-12 16:23:30 -0400803 info->feature_secdiscard = 0;
Li Dongyanged30bf32011-09-01 18:39:09 +0800804 queue_flag_clear(QUEUE_FLAG_DISCARD, rq);
Konrad Rzeszutek Wilk5ea42982011-10-12 16:23:30 -0400805 queue_flag_clear(QUEUE_FLAG_SECDISCARD, rq);
Li Dongyanged30bf32011-09-01 18:39:09 +0800806 }
807 __blk_end_request_all(req, error);
808 break;
Konrad Rzeszutek Wilkedf6ef52011-05-03 12:01:11 -0400809 case BLKIF_OP_FLUSH_DISKCACHE:
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700810 case BLKIF_OP_WRITE_BARRIER:
811 if (unlikely(bret->status == BLKIF_RSP_EOPNOTSUPP)) {
Konrad Rzeszutek Wilk6878c322012-05-25 17:34:51 -0400812 printk(KERN_WARNING "blkfront: %s: %s op failed\n",
813 info->gd->disk_name, op_name(bret->operation));
Kiyoshi Uedaf530f0362007-12-11 17:47:36 -0500814 error = -EOPNOTSUPP;
Jeremy Fitzhardingedcb8bae2010-11-02 11:55:58 -0400815 }
816 if (unlikely(bret->status == BLKIF_RSP_ERROR &&
Konrad Rzeszutek Wilk97e36832011-10-12 12:12:36 -0400817 info->shadow[id].req.u.rw.nr_segments == 0)) {
Konrad Rzeszutek Wilk6878c322012-05-25 17:34:51 -0400818 printk(KERN_WARNING "blkfront: %s: empty %s op failed\n",
819 info->gd->disk_name, op_name(bret->operation));
Jeremy Fitzhardingedcb8bae2010-11-02 11:55:58 -0400820 error = -EOPNOTSUPP;
821 }
822 if (unlikely(error)) {
823 if (error == -EOPNOTSUPP)
824 error = 0;
Tejun Heo4913efe2010-09-03 11:56:16 +0200825 info->feature_flush = 0;
Konrad Rzeszutek Wilkedf6ef52011-05-03 12:01:11 -0400826 info->flush_op = 0;
Tejun Heo4913efe2010-09-03 11:56:16 +0200827 xlvbd_flush(info);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700828 }
829 /* fall through */
830 case BLKIF_OP_READ:
831 case BLKIF_OP_WRITE:
832 if (unlikely(bret->status != BLKIF_RSP_OKAY))
833 dev_dbg(&info->xbdev->dev, "Bad return from blkdev data "
834 "request: %x\n", bret->status);
835
Tejun Heo40cbbb72009-04-23 11:05:19 +0900836 __blk_end_request_all(req, error);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700837 break;
838 default:
839 BUG();
840 }
841 }
842
843 info->ring.rsp_cons = i;
844
845 if (i != info->ring.req_prod_pvt) {
846 int more_to_do;
847 RING_FINAL_CHECK_FOR_RESPONSES(&info->ring, more_to_do);
848 if (more_to_do)
849 goto again;
850 } else
851 info->ring.sring->rsp_event = i + 1;
852
853 kick_pending_request_queues(info);
854
Steven Noonan34678112012-02-17 12:04:44 -0800855 spin_unlock_irqrestore(&info->io_lock, flags);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700856
857 return IRQ_HANDLED;
858}
859
860
861static int setup_blkring(struct xenbus_device *dev,
862 struct blkfront_info *info)
863{
864 struct blkif_sring *sring;
865 int err;
866
867 info->ring_ref = GRANT_INVALID_REF;
868
Ian Campbella144ff02008-06-17 10:47:08 +0200869 sring = (struct blkif_sring *)__get_free_page(GFP_NOIO | __GFP_HIGH);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700870 if (!sring) {
871 xenbus_dev_fatal(dev, -ENOMEM, "allocating shared ring");
872 return -ENOMEM;
873 }
874 SHARED_RING_INIT(sring);
875 FRONT_RING_INIT(&info->ring, sring, PAGE_SIZE);
876
Jens Axboe9e973e62009-02-24 08:10:09 +0100877 sg_init_table(info->sg, BLKIF_MAX_SEGMENTS_PER_REQUEST);
878
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700879 err = xenbus_grant_ring(dev, virt_to_mfn(info->ring.sring));
880 if (err < 0) {
881 free_page((unsigned long)sring);
882 info->ring.sring = NULL;
883 goto fail;
884 }
885 info->ring_ref = err;
886
887 err = xenbus_alloc_evtchn(dev, &info->evtchn);
888 if (err)
889 goto fail;
890
891 err = bind_evtchn_to_irqhandler(info->evtchn,
892 blkif_interrupt,
893 IRQF_SAMPLE_RANDOM, "blkif", info);
894 if (err <= 0) {
895 xenbus_dev_fatal(dev, err,
896 "bind_evtchn_to_irqhandler failed");
897 goto fail;
898 }
899 info->irq = err;
900
901 return 0;
902fail:
903 blkif_free(info, 0);
904 return err;
905}
906
907
908/* Common code used when first setting up, and when resuming. */
Ian Campbell203fd612009-12-04 15:33:54 +0000909static int talk_to_blkback(struct xenbus_device *dev,
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700910 struct blkfront_info *info)
911{
912 const char *message = NULL;
913 struct xenbus_transaction xbt;
914 int err;
915
916 /* Create shared ring, alloc event channel. */
917 err = setup_blkring(dev, info);
918 if (err)
919 goto out;
920
921again:
922 err = xenbus_transaction_start(&xbt);
923 if (err) {
924 xenbus_dev_fatal(dev, err, "starting transaction");
925 goto destroy_blkring;
926 }
927
928 err = xenbus_printf(xbt, dev->nodename,
929 "ring-ref", "%u", info->ring_ref);
930 if (err) {
931 message = "writing ring-ref";
932 goto abort_transaction;
933 }
934 err = xenbus_printf(xbt, dev->nodename,
935 "event-channel", "%u", info->evtchn);
936 if (err) {
937 message = "writing event-channel";
938 goto abort_transaction;
939 }
Markus Armbruster3e334232008-04-02 10:54:02 -0700940 err = xenbus_printf(xbt, dev->nodename, "protocol", "%s",
941 XEN_IO_PROTO_ABI_NATIVE);
942 if (err) {
943 message = "writing protocol";
944 goto abort_transaction;
945 }
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700946
947 err = xenbus_transaction_end(xbt, 0);
948 if (err) {
949 if (err == -EAGAIN)
950 goto again;
951 xenbus_dev_fatal(dev, err, "completing transaction");
952 goto destroy_blkring;
953 }
954
955 xenbus_switch_state(dev, XenbusStateInitialised);
956
957 return 0;
958
959 abort_transaction:
960 xenbus_transaction_end(xbt, 1);
961 if (message)
962 xenbus_dev_fatal(dev, err, "%s", message);
963 destroy_blkring:
964 blkif_free(info, 0);
965 out:
966 return err;
967}
968
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700969/**
970 * Entry point to this code when a new device is created. Allocate the basic
971 * structures and the ring buffer for communication with the backend, and
972 * inform the backend of the appropriate details for those. Switch to
973 * Initialised state.
974 */
975static int blkfront_probe(struct xenbus_device *dev,
976 const struct xenbus_device_id *id)
977{
978 int err, vdevice, i;
979 struct blkfront_info *info;
980
981 /* FIXME: Use dynamic device id if this is not set. */
982 err = xenbus_scanf(XBT_NIL, dev->nodename,
983 "virtual-device", "%i", &vdevice);
984 if (err != 1) {
Chris Lalancette9246b5f2008-09-17 14:30:32 -0700985 /* go looking in the extended area instead */
986 err = xenbus_scanf(XBT_NIL, dev->nodename, "virtual-device-ext",
987 "%i", &vdevice);
988 if (err != 1) {
989 xenbus_dev_fatal(dev, err, "reading virtual-device");
990 return err;
991 }
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700992 }
993
Stefano Stabellinib98a4092010-07-29 14:53:16 +0100994 if (xen_hvm_domain()) {
995 char *type;
996 int len;
997 /* no unplug has been done: do not hook devices != xen vbds */
Ian Campbell1dc7ce92010-08-23 11:59:29 +0100998 if (xen_platform_pci_unplug & XEN_UNPLUG_UNNECESSARY) {
Stefano Stabellinib98a4092010-07-29 14:53:16 +0100999 int major;
Stefano Stabellinic1c54132010-05-14 12:44:30 +01001000
Stefano Stabellinib98a4092010-07-29 14:53:16 +01001001 if (!VDEV_IS_EXTENDED(vdevice))
1002 major = BLKIF_MAJOR(vdevice);
1003 else
1004 major = XENVBD_MAJOR;
Stefano Stabellinic1c54132010-05-14 12:44:30 +01001005
Stefano Stabellinib98a4092010-07-29 14:53:16 +01001006 if (major != XENVBD_MAJOR) {
1007 printk(KERN_INFO
1008 "%s: HVM does not support vbd %d as xen block device\n",
1009 __FUNCTION__, vdevice);
1010 return -ENODEV;
1011 }
1012 }
1013 /* do not create a PV cdrom device if we are an HVM guest */
1014 type = xenbus_read(XBT_NIL, dev->nodename, "device-type", &len);
1015 if (IS_ERR(type))
1016 return -ENODEV;
1017 if (strncmp(type, "cdrom", 5) == 0) {
1018 kfree(type);
Stefano Stabellinic1c54132010-05-14 12:44:30 +01001019 return -ENODEV;
1020 }
Stefano Stabellinib98a4092010-07-29 14:53:16 +01001021 kfree(type);
Stefano Stabellinic1c54132010-05-14 12:44:30 +01001022 }
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001023 info = kzalloc(sizeof(*info), GFP_KERNEL);
1024 if (!info) {
1025 xenbus_dev_fatal(dev, -ENOMEM, "allocating info structure");
1026 return -ENOMEM;
1027 }
1028
Daniel Stoddenb70f5fa2010-04-30 22:01:19 +00001029 mutex_init(&info->mutex);
Steven Noonan34678112012-02-17 12:04:44 -08001030 spin_lock_init(&info->io_lock);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001031 info->xbdev = dev;
1032 info->vdevice = vdevice;
1033 info->connected = BLKIF_STATE_DISCONNECTED;
1034 INIT_WORK(&info->work, blkif_restart_queue);
1035
1036 for (i = 0; i < BLK_RING_SIZE; i++)
Konrad Rzeszutek Wilk97e36832011-10-12 12:12:36 -04001037 info->shadow[i].req.u.rw.id = i+1;
1038 info->shadow[BLK_RING_SIZE-1].req.u.rw.id = 0x0fffffff;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001039
1040 /* Front end dir is a number, which is used as the id. */
1041 info->handle = simple_strtoul(strrchr(dev->nodename, '/')+1, NULL, 0);
Greg Kroah-Hartmana1b4b122009-04-30 14:43:31 -07001042 dev_set_drvdata(&dev->dev, info);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001043
Ian Campbell203fd612009-12-04 15:33:54 +00001044 err = talk_to_blkback(dev, info);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001045 if (err) {
1046 kfree(info);
Greg Kroah-Hartmana1b4b122009-04-30 14:43:31 -07001047 dev_set_drvdata(&dev->dev, NULL);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001048 return err;
1049 }
1050
1051 return 0;
1052}
1053
1054
1055static int blkif_recover(struct blkfront_info *info)
1056{
1057 int i;
1058 struct blkif_request *req;
1059 struct blk_shadow *copy;
1060 int j;
1061
1062 /* Stage 1: Make a safe copy of the shadow state. */
Ian Campbella144ff02008-06-17 10:47:08 +02001063 copy = kmalloc(sizeof(info->shadow),
1064 GFP_NOIO | __GFP_REPEAT | __GFP_HIGH);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001065 if (!copy)
1066 return -ENOMEM;
1067 memcpy(copy, info->shadow, sizeof(info->shadow));
1068
1069 /* Stage 2: Set up free list. */
1070 memset(&info->shadow, 0, sizeof(info->shadow));
1071 for (i = 0; i < BLK_RING_SIZE; i++)
Konrad Rzeszutek Wilk97e36832011-10-12 12:12:36 -04001072 info->shadow[i].req.u.rw.id = i+1;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001073 info->shadow_free = info->ring.req_prod_pvt;
Konrad Rzeszutek Wilk97e36832011-10-12 12:12:36 -04001074 info->shadow[BLK_RING_SIZE-1].req.u.rw.id = 0x0fffffff;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001075
1076 /* Stage 3: Find pending requests and requeue them. */
1077 for (i = 0; i < BLK_RING_SIZE; i++) {
1078 /* Not in use? */
Jeremy Fitzhardingea945b982010-11-01 17:03:14 -04001079 if (!copy[i].request)
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001080 continue;
1081
1082 /* Grab a request slot and copy shadow state into it. */
1083 req = RING_GET_REQUEST(&info->ring, info->ring.req_prod_pvt);
1084 *req = copy[i].req;
1085
1086 /* We get a new request id, and must reset the shadow state. */
Konrad Rzeszutek Wilk97e36832011-10-12 12:12:36 -04001087 req->u.rw.id = get_id_from_freelist(info);
1088 memcpy(&info->shadow[req->u.rw.id], &copy[i], sizeof(copy[i]));
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001089
Konrad Rzeszutek Wilk5ea42982011-10-12 16:23:30 -04001090 if (req->operation != BLKIF_OP_DISCARD) {
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001091 /* Rewrite any grant references invalidated by susp/resume. */
Konrad Rzeszutek Wilk5ea42982011-10-12 16:23:30 -04001092 for (j = 0; j < req->u.rw.nr_segments; j++)
1093 gnttab_grant_foreign_access_ref(
1094 req->u.rw.seg[j].gref,
1095 info->xbdev->otherend_id,
1096 pfn_to_mfn(info->shadow[req->u.rw.id].frame[j]),
1097 rq_data_dir(info->shadow[req->u.rw.id].request));
1098 }
Konrad Rzeszutek Wilk97e36832011-10-12 12:12:36 -04001099 info->shadow[req->u.rw.id].req = *req;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001100
1101 info->ring.req_prod_pvt++;
1102 }
1103
1104 kfree(copy);
1105
1106 xenbus_switch_state(info->xbdev, XenbusStateConnected);
1107
Steven Noonan34678112012-02-17 12:04:44 -08001108 spin_lock_irq(&info->io_lock);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001109
1110 /* Now safe for us to use the shared ring */
1111 info->connected = BLKIF_STATE_CONNECTED;
1112
1113 /* Send off requeued requests */
1114 flush_requests(info);
1115
1116 /* Kick any other new requests queued since we resumed */
1117 kick_pending_request_queues(info);
1118
Steven Noonan34678112012-02-17 12:04:44 -08001119 spin_unlock_irq(&info->io_lock);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001120
1121 return 0;
1122}
1123
1124/**
1125 * We are reconnecting to the backend, due to a suspend/resume, or a backend
1126 * driver restart. We tear down our blkif structure and recreate it, but
1127 * leave the device-layer structures intact so that this is transparent to the
1128 * rest of the kernel.
1129 */
1130static int blkfront_resume(struct xenbus_device *dev)
1131{
Greg Kroah-Hartmana1b4b122009-04-30 14:43:31 -07001132 struct blkfront_info *info = dev_get_drvdata(&dev->dev);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001133 int err;
1134
1135 dev_dbg(&dev->dev, "blkfront_resume: %s\n", dev->nodename);
1136
1137 blkif_free(info, info->connected == BLKIF_STATE_CONNECTED);
1138
Ian Campbell203fd612009-12-04 15:33:54 +00001139 err = talk_to_blkback(dev, info);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001140 if (info->connected == BLKIF_STATE_SUSPENDED && !err)
1141 err = blkif_recover(info);
1142
1143 return err;
1144}
1145
Daniel Stoddenb70f5fa2010-04-30 22:01:19 +00001146static void
1147blkfront_closing(struct blkfront_info *info)
1148{
1149 struct xenbus_device *xbdev = info->xbdev;
1150 struct block_device *bdev = NULL;
1151
1152 mutex_lock(&info->mutex);
1153
1154 if (xbdev->state == XenbusStateClosing) {
1155 mutex_unlock(&info->mutex);
1156 return;
1157 }
1158
1159 if (info->gd)
1160 bdev = bdget_disk(info->gd, 0);
1161
1162 mutex_unlock(&info->mutex);
1163
1164 if (!bdev) {
1165 xenbus_frontend_closed(xbdev);
1166 return;
1167 }
1168
1169 mutex_lock(&bdev->bd_mutex);
1170
Daniel Stodden7b32d102010-04-30 22:01:23 +00001171 if (bdev->bd_openers) {
Daniel Stoddenb70f5fa2010-04-30 22:01:19 +00001172 xenbus_dev_error(xbdev, -EBUSY,
1173 "Device in use; refusing to close");
1174 xenbus_switch_state(xbdev, XenbusStateClosing);
1175 } else {
1176 xlvbd_release_gendisk(info);
1177 xenbus_frontend_closed(xbdev);
1178 }
1179
1180 mutex_unlock(&bdev->bd_mutex);
1181 bdput(bdev);
1182}
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001183
Li Dongyanged30bf32011-09-01 18:39:09 +08001184static void blkfront_setup_discard(struct blkfront_info *info)
1185{
1186 int err;
1187 char *type;
1188 unsigned int discard_granularity;
1189 unsigned int discard_alignment;
Konrad Rzeszutek Wilk5ea42982011-10-12 16:23:30 -04001190 unsigned int discard_secure;
Li Dongyanged30bf32011-09-01 18:39:09 +08001191
1192 type = xenbus_read(XBT_NIL, info->xbdev->otherend, "type", NULL);
1193 if (IS_ERR(type))
1194 return;
1195
Konrad Rzeszutek Wilk5ea42982011-10-12 16:23:30 -04001196 info->feature_secdiscard = 0;
Li Dongyanged30bf32011-09-01 18:39:09 +08001197 if (strncmp(type, "phy", 3) == 0) {
1198 err = xenbus_gather(XBT_NIL, info->xbdev->otherend,
1199 "discard-granularity", "%u", &discard_granularity,
1200 "discard-alignment", "%u", &discard_alignment,
1201 NULL);
1202 if (!err) {
1203 info->feature_discard = 1;
1204 info->discard_granularity = discard_granularity;
1205 info->discard_alignment = discard_alignment;
1206 }
Konrad Rzeszutek Wilk5ea42982011-10-12 16:23:30 -04001207 err = xenbus_gather(XBT_NIL, info->xbdev->otherend,
1208 "discard-secure", "%d", &discard_secure,
1209 NULL);
1210 if (!err)
1211 info->feature_secdiscard = discard_secure;
1212
Li Dongyanged30bf32011-09-01 18:39:09 +08001213 } else if (strncmp(type, "file", 4) == 0)
1214 info->feature_discard = 1;
1215
1216 kfree(type);
1217}
1218
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001219/*
1220 * Invoked when the backend is finally 'ready' (and has told produced
1221 * the details about the physical device - #sectors, size, etc).
1222 */
1223static void blkfront_connect(struct blkfront_info *info)
1224{
1225 unsigned long long sectors;
1226 unsigned long sector_size;
1227 unsigned int binfo;
1228 int err;
Li Dongyanged30bf32011-09-01 18:39:09 +08001229 int barrier, flush, discard;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001230
K. Y. Srinivasan1fa73be2010-03-11 13:42:26 -08001231 switch (info->connected) {
1232 case BLKIF_STATE_CONNECTED:
1233 /*
1234 * Potentially, the back-end may be signalling
1235 * a capacity change; update the capacity.
1236 */
1237 err = xenbus_scanf(XBT_NIL, info->xbdev->otherend,
1238 "sectors", "%Lu", &sectors);
1239 if (XENBUS_EXIST_ERR(err))
1240 return;
1241 printk(KERN_INFO "Setting capacity to %Lu\n",
1242 sectors);
1243 set_capacity(info->gd, sectors);
K. Y. Srinivasan2def1412010-03-18 15:00:54 -07001244 revalidate_disk(info->gd);
K. Y. Srinivasan1fa73be2010-03-11 13:42:26 -08001245
1246 /* fall through */
1247 case BLKIF_STATE_SUSPENDED:
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001248 return;
1249
Jeremy Fitzhardingeb4dddb42010-03-11 15:10:40 -08001250 default:
1251 break;
K. Y. Srinivasan1fa73be2010-03-11 13:42:26 -08001252 }
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001253
1254 dev_dbg(&info->xbdev->dev, "%s:%s.\n",
1255 __func__, info->xbdev->otherend);
1256
1257 err = xenbus_gather(XBT_NIL, info->xbdev->otherend,
1258 "sectors", "%llu", &sectors,
1259 "info", "%u", &binfo,
1260 "sector-size", "%lu", &sector_size,
1261 NULL);
1262 if (err) {
1263 xenbus_dev_fatal(info->xbdev, err,
1264 "reading backend fields at %s",
1265 info->xbdev->otherend);
1266 return;
1267 }
1268
Konrad Rzeszutek Wilkedf6ef52011-05-03 12:01:11 -04001269 info->feature_flush = 0;
1270 info->flush_op = 0;
1271
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001272 err = xenbus_gather(XBT_NIL, info->xbdev->otherend,
Marek Marczykowski4352b472011-05-03 12:04:52 -04001273 "feature-barrier", "%d", &barrier,
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001274 NULL);
Jeremy Fitzhardinge7901d142010-07-28 10:49:29 -07001275
1276 /*
1277 * If there's no "feature-barrier" defined, then it means
1278 * we're dealing with a very old backend which writes
Tejun Heo4913efe2010-09-03 11:56:16 +02001279 * synchronously; nothing to do.
Jeremy Fitzhardinge7901d142010-07-28 10:49:29 -07001280 *
Tejun Heo6958f142010-09-03 11:56:16 +02001281 * If there are barriers, then we use flush.
Jeremy Fitzhardinge7901d142010-07-28 10:49:29 -07001282 */
Konrad Rzeszutek Wilkedf6ef52011-05-03 12:01:11 -04001283 if (!err && barrier) {
Jeremy Fitzhardingebe2f8372010-11-02 10:38:33 -04001284 info->feature_flush = REQ_FLUSH | REQ_FUA;
Konrad Rzeszutek Wilkedf6ef52011-05-03 12:01:11 -04001285 info->flush_op = BLKIF_OP_WRITE_BARRIER;
1286 }
1287 /*
1288 * And if there is "feature-flush-cache" use that above
1289 * barriers.
1290 */
1291 err = xenbus_gather(XBT_NIL, info->xbdev->otherend,
1292 "feature-flush-cache", "%d", &flush,
1293 NULL);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001294
Konrad Rzeszutek Wilkedf6ef52011-05-03 12:01:11 -04001295 if (!err && flush) {
1296 info->feature_flush = REQ_FLUSH;
1297 info->flush_op = BLKIF_OP_FLUSH_DISKCACHE;
1298 }
Li Dongyanged30bf32011-09-01 18:39:09 +08001299
1300 err = xenbus_gather(XBT_NIL, info->xbdev->otherend,
1301 "feature-discard", "%d", &discard,
1302 NULL);
1303
1304 if (!err && discard)
1305 blkfront_setup_discard(info);
1306
Chris Lalancette9246b5f2008-09-17 14:30:32 -07001307 err = xlvbd_alloc_gendisk(sectors, info, binfo, sector_size);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001308 if (err) {
1309 xenbus_dev_fatal(info->xbdev, err, "xlvbd_add at %s",
1310 info->xbdev->otherend);
1311 return;
1312 }
1313
1314 xenbus_switch_state(info->xbdev, XenbusStateConnected);
1315
1316 /* Kick pending requests. */
Steven Noonan34678112012-02-17 12:04:44 -08001317 spin_lock_irq(&info->io_lock);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001318 info->connected = BLKIF_STATE_CONNECTED;
1319 kick_pending_request_queues(info);
Steven Noonan34678112012-02-17 12:04:44 -08001320 spin_unlock_irq(&info->io_lock);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001321
1322 add_disk(info->gd);
Christian Limpach1d78d702008-04-02 10:54:04 -07001323
1324 info->is_ready = 1;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001325}
1326
1327/**
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001328 * Callback received when the backend's state changes.
1329 */
Ian Campbell203fd612009-12-04 15:33:54 +00001330static void blkback_changed(struct xenbus_device *dev,
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001331 enum xenbus_state backend_state)
1332{
Greg Kroah-Hartmana1b4b122009-04-30 14:43:31 -07001333 struct blkfront_info *info = dev_get_drvdata(&dev->dev);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001334
Ian Campbell203fd612009-12-04 15:33:54 +00001335 dev_dbg(&dev->dev, "blkfront:blkback_changed to state %d.\n", backend_state);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001336
1337 switch (backend_state) {
1338 case XenbusStateInitialising:
1339 case XenbusStateInitWait:
1340 case XenbusStateInitialised:
Noboru Iwamatsub78c9512009-10-13 17:22:29 -04001341 case XenbusStateReconfiguring:
1342 case XenbusStateReconfigured:
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001343 case XenbusStateUnknown:
1344 case XenbusStateClosed:
1345 break;
1346
1347 case XenbusStateConnected:
1348 blkfront_connect(info);
1349 break;
1350
1351 case XenbusStateClosing:
Daniel Stoddenb70f5fa2010-04-30 22:01:19 +00001352 blkfront_closing(info);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001353 break;
1354 }
1355}
1356
Daniel Stoddenfa1bd352010-04-30 22:01:22 +00001357static int blkfront_remove(struct xenbus_device *xbdev)
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001358{
Daniel Stoddenfa1bd352010-04-30 22:01:22 +00001359 struct blkfront_info *info = dev_get_drvdata(&xbdev->dev);
1360 struct block_device *bdev = NULL;
1361 struct gendisk *disk;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001362
Daniel Stoddenfa1bd352010-04-30 22:01:22 +00001363 dev_dbg(&xbdev->dev, "%s removed", xbdev->nodename);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001364
1365 blkif_free(info, 0);
1366
Daniel Stoddenfa1bd352010-04-30 22:01:22 +00001367 mutex_lock(&info->mutex);
1368
1369 disk = info->gd;
1370 if (disk)
1371 bdev = bdget_disk(disk, 0);
1372
1373 info->xbdev = NULL;
1374 mutex_unlock(&info->mutex);
1375
1376 if (!bdev) {
Jan Beulich0e345822010-08-07 18:28:55 +02001377 kfree(info);
Daniel Stoddenfa1bd352010-04-30 22:01:22 +00001378 return 0;
1379 }
1380
1381 /*
1382 * The xbdev was removed before we reached the Closed
1383 * state. See if it's safe to remove the disk. If the bdev
1384 * isn't closed yet, we let release take care of it.
1385 */
1386
1387 mutex_lock(&bdev->bd_mutex);
1388 info = disk->private_data;
1389
Daniel Stoddend54142c2010-08-07 18:51:21 +02001390 dev_warn(disk_to_dev(disk),
1391 "%s was hot-unplugged, %d stale handles\n",
1392 xbdev->nodename, bdev->bd_openers);
1393
Daniel Stodden7b32d102010-04-30 22:01:23 +00001394 if (info && !bdev->bd_openers) {
Daniel Stoddenfa1bd352010-04-30 22:01:22 +00001395 xlvbd_release_gendisk(info);
1396 disk->private_data = NULL;
1397 kfree(info);
1398 }
1399
1400 mutex_unlock(&bdev->bd_mutex);
1401 bdput(bdev);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001402
1403 return 0;
1404}
1405
Christian Limpach1d78d702008-04-02 10:54:04 -07001406static int blkfront_is_ready(struct xenbus_device *dev)
1407{
Greg Kroah-Hartmana1b4b122009-04-30 14:43:31 -07001408 struct blkfront_info *info = dev_get_drvdata(&dev->dev);
Christian Limpach1d78d702008-04-02 10:54:04 -07001409
Jan Beulich5d7ed202010-08-07 18:31:12 +02001410 return info->is_ready && info->xbdev;
Christian Limpach1d78d702008-04-02 10:54:04 -07001411}
1412
Al Viroa63c8482008-03-02 10:23:47 -05001413static int blkif_open(struct block_device *bdev, fmode_t mode)
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001414{
Daniel Stodden13961742010-08-07 18:36:53 +02001415 struct gendisk *disk = bdev->bd_disk;
1416 struct blkfront_info *info;
1417 int err = 0;
Arnd Bergmann6e9624b2010-08-07 18:25:34 +02001418
Arnd Bergmann2a48fc02010-06-02 14:28:52 +02001419 mutex_lock(&blkfront_mutex);
Arnd Bergmann6e9624b2010-08-07 18:25:34 +02001420
Daniel Stodden13961742010-08-07 18:36:53 +02001421 info = disk->private_data;
1422 if (!info) {
1423 /* xbdev gone */
1424 err = -ERESTARTSYS;
1425 goto out;
1426 }
1427
1428 mutex_lock(&info->mutex);
1429
1430 if (!info->gd)
1431 /* xbdev is closed */
1432 err = -ERESTARTSYS;
1433
1434 mutex_unlock(&info->mutex);
1435
Daniel Stodden13961742010-08-07 18:36:53 +02001436out:
Arnd Bergmann2a48fc02010-06-02 14:28:52 +02001437 mutex_unlock(&blkfront_mutex);
Daniel Stodden13961742010-08-07 18:36:53 +02001438 return err;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001439}
1440
Al Viroa63c8482008-03-02 10:23:47 -05001441static int blkif_release(struct gendisk *disk, fmode_t mode)
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001442{
Al Viroa63c8482008-03-02 10:23:47 -05001443 struct blkfront_info *info = disk->private_data;
Daniel Stodden7fd152f2010-08-07 18:45:12 +02001444 struct block_device *bdev;
1445 struct xenbus_device *xbdev;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001446
Arnd Bergmann2a48fc02010-06-02 14:28:52 +02001447 mutex_lock(&blkfront_mutex);
Daniel Stodden7fd152f2010-08-07 18:45:12 +02001448
1449 bdev = bdget_disk(disk, 0);
Daniel Stodden7fd152f2010-08-07 18:45:12 +02001450
Daniel Stoddenacfca3c2010-08-07 18:47:26 +02001451 if (bdev->bd_openers)
1452 goto out;
1453
Daniel Stodden7fd152f2010-08-07 18:45:12 +02001454 /*
1455 * Check if we have been instructed to close. We will have
1456 * deferred this request, because the bdev was still open.
1457 */
1458
1459 mutex_lock(&info->mutex);
1460 xbdev = info->xbdev;
1461
1462 if (xbdev && xbdev->state == XenbusStateClosing) {
1463 /* pending switch to state closed */
Daniel Stoddend54142c2010-08-07 18:51:21 +02001464 dev_info(disk_to_dev(bdev->bd_disk), "releasing disk\n");
Daniel Stodden7fd152f2010-08-07 18:45:12 +02001465 xlvbd_release_gendisk(info);
1466 xenbus_frontend_closed(info->xbdev);
1467 }
1468
1469 mutex_unlock(&info->mutex);
1470
1471 if (!xbdev) {
1472 /* sudden device removal */
Daniel Stoddend54142c2010-08-07 18:51:21 +02001473 dev_info(disk_to_dev(bdev->bd_disk), "releasing disk\n");
Daniel Stodden7fd152f2010-08-07 18:45:12 +02001474 xlvbd_release_gendisk(info);
1475 disk->private_data = NULL;
1476 kfree(info);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001477 }
Daniel Stodden7fd152f2010-08-07 18:45:12 +02001478
Jens Axboea4cc14e2010-08-08 21:50:05 -04001479out:
Andrew Jonesdad5cf62012-02-16 13:16:25 +01001480 bdput(bdev);
Arnd Bergmann2a48fc02010-06-02 14:28:52 +02001481 mutex_unlock(&blkfront_mutex);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001482 return 0;
1483}
1484
Alexey Dobriyan83d5cde2009-09-21 17:01:13 -07001485static const struct block_device_operations xlvbd_block_fops =
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001486{
1487 .owner = THIS_MODULE,
Al Viroa63c8482008-03-02 10:23:47 -05001488 .open = blkif_open,
1489 .release = blkif_release,
Ian Campbell597592d2008-02-21 13:03:45 -08001490 .getgeo = blkif_getgeo,
Arnd Bergmann8a6cfeb2010-07-08 10:18:46 +02001491 .ioctl = blkif_ioctl,
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001492};
1493
1494
Márton Némethec9c42e2010-01-10 13:39:52 +01001495static const struct xenbus_device_id blkfront_ids[] = {
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001496 { "vbd" },
1497 { "" }
1498};
1499
Jan Beulich73db1442011-12-22 09:08:13 +00001500static DEFINE_XENBUS_DRIVER(blkfront, ,
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001501 .probe = blkfront_probe,
1502 .remove = blkfront_remove,
1503 .resume = blkfront_resume,
Ian Campbell203fd612009-12-04 15:33:54 +00001504 .otherend_changed = blkback_changed,
Christian Limpach1d78d702008-04-02 10:54:04 -07001505 .is_ready = blkfront_is_ready,
Jan Beulich73db1442011-12-22 09:08:13 +00001506);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001507
1508static int __init xlblk_init(void)
1509{
Laszlo Ersek469738e2011-10-07 21:34:38 +02001510 int ret;
1511
Jeremy Fitzhardinge6e833582008-08-19 13:16:17 -07001512 if (!xen_domain())
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001513 return -ENODEV;
1514
Igor Mammedove95ae5a2012-03-27 19:31:08 +02001515 if (xen_hvm_domain() && !xen_platform_pci_unplug)
Igor Mammedovb9136d22012-03-21 15:08:38 +01001516 return -ENODEV;
1517
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001518 if (register_blkdev(XENVBD_MAJOR, DEV_NAME)) {
1519 printk(KERN_WARNING "xen_blk: can't get major %d with name %s\n",
1520 XENVBD_MAJOR, DEV_NAME);
1521 return -ENODEV;
1522 }
1523
Jan Beulich73db1442011-12-22 09:08:13 +00001524 ret = xenbus_register_frontend(&blkfront_driver);
Laszlo Ersek469738e2011-10-07 21:34:38 +02001525 if (ret) {
1526 unregister_blkdev(XENVBD_MAJOR, DEV_NAME);
1527 return ret;
1528 }
1529
1530 return 0;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001531}
1532module_init(xlblk_init);
1533
1534
Jan Beulich5a60d0c2008-06-17 10:47:08 +02001535static void __exit xlblk_exit(void)
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001536{
Jan Beulich86050672012-04-05 16:04:52 +01001537 xenbus_unregister_driver(&blkfront_driver);
1538 unregister_blkdev(XENVBD_MAJOR, DEV_NAME);
1539 kfree(minors);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001540}
1541module_exit(xlblk_exit);
1542
1543MODULE_DESCRIPTION("Xen virtual block device frontend");
1544MODULE_LICENSE("GPL");
1545MODULE_ALIAS_BLOCKDEV_MAJOR(XENVBD_MAJOR);
Mark McLoughlind2f0c522008-04-02 10:54:05 -07001546MODULE_ALIAS("xen:vbd");
Mark McLoughlin4f93f09b2008-04-02 10:54:06 -07001547MODULE_ALIAS("xenblk");