blob: 6eb2989a9d0ac587fe6ab8621e00fda287e4ab22 [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>
Jens Axboe9e973e62009-02-24 08:10:09 +010044#include <linux/scatterlist.h>
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -070045
Jeremy Fitzhardinge1ccbf532009-10-06 15:11:14 -070046#include <xen/xen.h>
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -070047#include <xen/xenbus.h>
48#include <xen/grant_table.h>
49#include <xen/events.h>
50#include <xen/page.h>
Stefano Stabellinic1c54132010-05-14 12:44:30 +010051#include <xen/platform_pci.h>
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -070052
53#include <xen/interface/grant_table.h>
54#include <xen/interface/io/blkif.h>
Markus Armbruster3e334232008-04-02 10:54:02 -070055#include <xen/interface/io/protocols.h>
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -070056
57#include <asm/xen/hypervisor.h>
58
59enum blkif_state {
60 BLKIF_STATE_DISCONNECTED,
61 BLKIF_STATE_CONNECTED,
62 BLKIF_STATE_SUSPENDED,
63};
64
65struct blk_shadow {
66 struct blkif_request req;
67 unsigned long request;
68 unsigned long frame[BLKIF_MAX_SEGMENTS_PER_REQUEST];
69};
70
Alexey Dobriyan83d5cde2009-09-21 17:01:13 -070071static const struct block_device_operations xlvbd_block_fops;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -070072
73#define BLK_RING_SIZE __RING_SIZE((struct blkif_sring *)0, PAGE_SIZE)
74
75/*
76 * We have one of these per vbd, whether ide, scsi or 'other'. They
77 * hang in private_data off the gendisk structure. We may end up
78 * putting all kinds of interesting stuff here :-)
79 */
80struct blkfront_info
81{
82 struct xenbus_device *xbdev;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -070083 struct gendisk *gd;
84 int vdevice;
85 blkif_vdev_t handle;
86 enum blkif_state connected;
87 int ring_ref;
88 struct blkif_front_ring ring;
Jens Axboe9e973e62009-02-24 08:10:09 +010089 struct scatterlist sg[BLKIF_MAX_SEGMENTS_PER_REQUEST];
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -070090 unsigned int evtchn, irq;
91 struct request_queue *rq;
92 struct work_struct work;
93 struct gnttab_free_callback callback;
94 struct blk_shadow shadow[BLK_RING_SIZE];
95 unsigned long shadow_free;
96 int feature_barrier;
Christian Limpach1d78d702008-04-02 10:54:04 -070097 int is_ready;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -070098
99 /**
100 * The number of people holding this device open. We won't allow a
101 * hot-unplug unless this is 0.
102 */
103 int users;
104};
105
106static DEFINE_SPINLOCK(blkif_io_lock);
107
108#define MAXIMUM_OUTSTANDING_BLOCK_REQS \
109 (BLKIF_MAX_SEGMENTS_PER_REQUEST * BLK_RING_SIZE)
110#define GRANT_INVALID_REF 0
111
112#define PARTS_PER_DISK 16
Chris Lalancette9246b5f2008-09-17 14:30:32 -0700113#define PARTS_PER_EXT_DISK 256
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700114
115#define BLKIF_MAJOR(dev) ((dev)>>8)
116#define BLKIF_MINOR(dev) ((dev) & 0xff)
117
Chris Lalancette9246b5f2008-09-17 14:30:32 -0700118#define EXT_SHIFT 28
119#define EXTENDED (1<<EXT_SHIFT)
120#define VDEV_IS_EXTENDED(dev) ((dev)&(EXTENDED))
121#define BLKIF_MINOR_EXT(dev) ((dev)&(~EXTENDED))
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700122
Chris Lalancette9246b5f2008-09-17 14:30:32 -0700123#define DEV_NAME "xvd" /* name in /dev */
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700124
125static int get_id_from_freelist(struct blkfront_info *info)
126{
127 unsigned long free = info->shadow_free;
Roel Kluinb9ed7252009-05-22 09:25:32 +0200128 BUG_ON(free >= BLK_RING_SIZE);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700129 info->shadow_free = info->shadow[free].req.id;
130 info->shadow[free].req.id = 0x0fffffee; /* debug */
131 return free;
132}
133
134static void add_id_to_freelist(struct blkfront_info *info,
135 unsigned long id)
136{
137 info->shadow[id].req.id = info->shadow_free;
138 info->shadow[id].request = 0;
139 info->shadow_free = id;
140}
141
142static void blkif_restart_queue_callback(void *arg)
143{
144 struct blkfront_info *info = (struct blkfront_info *)arg;
145 schedule_work(&info->work);
146}
147
Harvey Harrisonafe42d72008-04-29 00:59:47 -0700148static int blkif_getgeo(struct block_device *bd, struct hd_geometry *hg)
Ian Campbell597592d2008-02-21 13:03:45 -0800149{
150 /* We don't have real geometry info, but let's at least return
151 values consistent with the size of the device */
152 sector_t nsect = get_capacity(bd->bd_disk);
153 sector_t cylinders = nsect;
154
155 hg->heads = 0xff;
156 hg->sectors = 0x3f;
157 sector_div(cylinders, hg->heads * hg->sectors);
158 hg->cylinders = cylinders;
159 if ((sector_t)(hg->cylinders + 1) * hg->heads * hg->sectors < nsect)
160 hg->cylinders = 0xffff;
161 return 0;
162}
163
Al Viroa63c8482008-03-02 10:23:47 -0500164static int blkif_ioctl(struct block_device *bdev, fmode_t mode,
Adrian Bunk62aa00542008-08-04 11:59:05 +0200165 unsigned command, unsigned long argument)
Christian Limpach440a01a2008-06-17 10:47:08 +0200166{
Al Viroa63c8482008-03-02 10:23:47 -0500167 struct blkfront_info *info = bdev->bd_disk->private_data;
Christian Limpach440a01a2008-06-17 10:47:08 +0200168 int i;
169
170 dev_dbg(&info->xbdev->dev, "command: 0x%x, argument: 0x%lx\n",
171 command, (long)argument);
172
173 switch (command) {
174 case CDROMMULTISESSION:
175 dev_dbg(&info->xbdev->dev, "FIXME: support multisession CDs later\n");
176 for (i = 0; i < sizeof(struct cdrom_multisession); i++)
177 if (put_user(0, (char __user *)(argument + i)))
178 return -EFAULT;
179 return 0;
180
181 case CDROM_GET_CAPABILITY: {
182 struct gendisk *gd = info->gd;
183 if (gd->flags & GENHD_FL_CD)
184 return 0;
185 return -EINVAL;
186 }
187
188 default:
189 /*printk(KERN_ALERT "ioctl %08x not supported by Xen blkdev\n",
190 command);*/
191 return -EINVAL; /* same return as native Linux */
192 }
193
194 return 0;
195}
196
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700197/*
198 * blkif_queue_request
199 *
200 * request block io
201 *
202 * id: for guest use only.
203 * operation: BLKIF_OP_{READ,WRITE,PROBE}
204 * buffer: buffer to read/write into. this should be a
205 * virtual address in the guest os.
206 */
207static int blkif_queue_request(struct request *req)
208{
209 struct blkfront_info *info = req->rq_disk->private_data;
210 unsigned long buffer_mfn;
211 struct blkif_request *ring_req;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700212 unsigned long id;
213 unsigned int fsect, lsect;
Jens Axboe9e973e62009-02-24 08:10:09 +0100214 int i, ref;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700215 grant_ref_t gref_head;
Jens Axboe9e973e62009-02-24 08:10:09 +0100216 struct scatterlist *sg;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700217
218 if (unlikely(info->connected != BLKIF_STATE_CONNECTED))
219 return 1;
220
221 if (gnttab_alloc_grant_references(
222 BLKIF_MAX_SEGMENTS_PER_REQUEST, &gref_head) < 0) {
223 gnttab_request_free_callback(
224 &info->callback,
225 blkif_restart_queue_callback,
226 info,
227 BLKIF_MAX_SEGMENTS_PER_REQUEST);
228 return 1;
229 }
230
231 /* Fill out a communications ring structure. */
232 ring_req = RING_GET_REQUEST(&info->ring, info->ring.req_prod_pvt);
233 id = get_id_from_freelist(info);
234 info->shadow[id].request = (unsigned long)req;
235
236 ring_req->id = id;
Tejun Heo83096eb2009-05-07 22:24:39 +0900237 ring_req->sector_number = (blkif_sector_t)blk_rq_pos(req);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700238 ring_req->handle = info->handle;
239
240 ring_req->operation = rq_data_dir(req) ?
241 BLKIF_OP_WRITE : BLKIF_OP_READ;
242 if (blk_barrier_rq(req))
243 ring_req->operation = BLKIF_OP_WRITE_BARRIER;
244
Jens Axboe9e973e62009-02-24 08:10:09 +0100245 ring_req->nr_segments = blk_rq_map_sg(req->q, req, info->sg);
246 BUG_ON(ring_req->nr_segments > BLKIF_MAX_SEGMENTS_PER_REQUEST);
247
248 for_each_sg(info->sg, sg, ring_req->nr_segments, i) {
249 buffer_mfn = pfn_to_mfn(page_to_pfn(sg_page(sg)));
250 fsect = sg->offset >> 9;
251 lsect = fsect + (sg->length >> 9) - 1;
Jens Axboe6c92e692007-08-16 13:43:12 +0200252 /* install a grant reference. */
253 ref = gnttab_claim_grant_reference(&gref_head);
254 BUG_ON(ref == -ENOSPC);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700255
Jens Axboe6c92e692007-08-16 13:43:12 +0200256 gnttab_grant_foreign_access_ref(
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700257 ref,
258 info->xbdev->otherend_id,
259 buffer_mfn,
260 rq_data_dir(req) );
261
Jens Axboe9e973e62009-02-24 08:10:09 +0100262 info->shadow[id].frame[i] = mfn_to_pfn(buffer_mfn);
263 ring_req->seg[i] =
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700264 (struct blkif_request_segment) {
265 .gref = ref,
266 .first_sect = fsect,
267 .last_sect = lsect };
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700268 }
269
270 info->ring.req_prod_pvt++;
271
272 /* Keep a private copy so we can reissue requests when recovering. */
273 info->shadow[id].req = *ring_req;
274
275 gnttab_free_grant_references(gref_head);
276
277 return 0;
278}
279
280
281static inline void flush_requests(struct blkfront_info *info)
282{
283 int notify;
284
285 RING_PUSH_REQUESTS_AND_CHECK_NOTIFY(&info->ring, notify);
286
287 if (notify)
288 notify_remote_via_irq(info->irq);
289}
290
291/*
292 * do_blkif_request
293 * read a block; request is in a request queue
294 */
Jens Axboe165125e2007-07-24 09:28:11 +0200295static void do_blkif_request(struct request_queue *rq)
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700296{
297 struct blkfront_info *info = NULL;
298 struct request *req;
299 int queued;
300
301 pr_debug("Entered do_blkif_request\n");
302
303 queued = 0;
304
Tejun Heo9934c8c2009-05-08 11:54:16 +0900305 while ((req = blk_peek_request(rq)) != NULL) {
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700306 info = req->rq_disk->private_data;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700307
308 if (RING_FULL(&info->ring))
309 goto wait;
310
Tejun Heo9934c8c2009-05-08 11:54:16 +0900311 blk_start_request(req);
Tejun Heo296b2f62009-05-08 11:54:15 +0900312
313 if (!blk_fs_request(req)) {
314 __blk_end_request_all(req, -EIO);
315 continue;
316 }
317
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700318 pr_debug("do_blk_req %p: cmd %p, sec %lx, "
Tejun Heo83096eb2009-05-07 22:24:39 +0900319 "(%u/%u) buffer:%p [%s]\n",
320 req, req->cmd, (unsigned long)blk_rq_pos(req),
321 blk_rq_cur_sectors(req), blk_rq_sectors(req),
322 req->buffer, rq_data_dir(req) ? "write" : "read");
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700323
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700324 if (blkif_queue_request(req)) {
325 blk_requeue_request(rq, req);
326wait:
327 /* Avoid pointless unplugs. */
328 blk_stop_queue(rq);
329 break;
330 }
331
332 queued++;
333 }
334
335 if (queued != 0)
336 flush_requests(info);
337}
338
339static int xlvbd_init_blk_queue(struct gendisk *gd, u16 sector_size)
340{
Jens Axboe165125e2007-07-24 09:28:11 +0200341 struct request_queue *rq;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700342
343 rq = blk_init_queue(do_blkif_request, &blkif_io_lock);
344 if (rq == NULL)
345 return -1;
346
Fernando Luis Vázquez Cao66d352e2008-10-27 18:45:54 +0900347 queue_flag_set_unlocked(QUEUE_FLAG_VIRT, rq);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700348
349 /* Hard sector size and max sectors impersonate the equiv. hardware. */
Martin K. Petersene1defc42009-05-22 17:17:49 -0400350 blk_queue_logical_block_size(rq, sector_size);
Martin K. Petersen086fa5f2010-02-26 00:20:38 -0500351 blk_queue_max_hw_sectors(rq, 512);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700352
353 /* Each segment in a request is up to an aligned page in size. */
354 blk_queue_segment_boundary(rq, PAGE_SIZE - 1);
355 blk_queue_max_segment_size(rq, PAGE_SIZE);
356
357 /* Ensure a merged request will fit in a single I/O ring slot. */
Martin K. Petersen8a783622010-02-26 00:20:39 -0500358 blk_queue_max_segments(rq, BLKIF_MAX_SEGMENTS_PER_REQUEST);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700359
360 /* Make sure buffer addresses are sector-aligned. */
361 blk_queue_dma_alignment(rq, 511);
362
Ian Campbell1c91fe12008-06-17 10:47:08 +0200363 /* Make sure we don't use bounce buffers. */
364 blk_queue_bounce_limit(rq, BLK_BOUNCE_ANY);
365
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700366 gd->queue = rq;
367
368 return 0;
369}
370
371
372static int xlvbd_barrier(struct blkfront_info *info)
373{
374 int err;
375
376 err = blk_queue_ordered(info->rq,
377 info->feature_barrier ? QUEUE_ORDERED_DRAIN : QUEUE_ORDERED_NONE,
378 NULL);
379
380 if (err)
381 return err;
382
383 printk(KERN_INFO "blkfront: %s: barriers %s\n",
384 info->gd->disk_name,
385 info->feature_barrier ? "enabled" : "disabled");
386 return 0;
387}
388
389
Chris Lalancette9246b5f2008-09-17 14:30:32 -0700390static int xlvbd_alloc_gendisk(blkif_sector_t capacity,
391 struct blkfront_info *info,
392 u16 vdisk_info, u16 sector_size)
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700393{
394 struct gendisk *gd;
395 int nr_minors = 1;
396 int err = -ENODEV;
Chris Lalancette9246b5f2008-09-17 14:30:32 -0700397 unsigned int offset;
398 int minor;
399 int nr_parts;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700400
401 BUG_ON(info->gd != NULL);
402 BUG_ON(info->rq != NULL);
403
Chris Lalancette9246b5f2008-09-17 14:30:32 -0700404 if ((info->vdevice>>EXT_SHIFT) > 1) {
405 /* this is above the extended range; something is wrong */
406 printk(KERN_WARNING "blkfront: vdevice 0x%x is above the extended range; ignoring\n", info->vdevice);
407 return -ENODEV;
408 }
409
410 if (!VDEV_IS_EXTENDED(info->vdevice)) {
411 minor = BLKIF_MINOR(info->vdevice);
412 nr_parts = PARTS_PER_DISK;
413 } else {
414 minor = BLKIF_MINOR_EXT(info->vdevice);
415 nr_parts = PARTS_PER_EXT_DISK;
416 }
417
418 if ((minor % nr_parts) == 0)
419 nr_minors = nr_parts;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700420
421 gd = alloc_disk(nr_minors);
422 if (gd == NULL)
423 goto out;
424
Chris Lalancette9246b5f2008-09-17 14:30:32 -0700425 offset = minor / nr_parts;
426
427 if (nr_minors > 1) {
428 if (offset < 26)
429 sprintf(gd->disk_name, "%s%c", DEV_NAME, 'a' + offset);
430 else
431 sprintf(gd->disk_name, "%s%c%c", DEV_NAME,
432 'a' + ((offset / 26)-1), 'a' + (offset % 26));
433 } else {
434 if (offset < 26)
435 sprintf(gd->disk_name, "%s%c%d", DEV_NAME,
436 'a' + offset,
437 minor & (nr_parts - 1));
438 else
439 sprintf(gd->disk_name, "%s%c%c%d", DEV_NAME,
440 'a' + ((offset / 26) - 1),
441 'a' + (offset % 26),
442 minor & (nr_parts - 1));
443 }
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700444
445 gd->major = XENVBD_MAJOR;
446 gd->first_minor = minor;
447 gd->fops = &xlvbd_block_fops;
448 gd->private_data = info;
449 gd->driverfs_dev = &(info->xbdev->dev);
450 set_capacity(gd, capacity);
451
452 if (xlvbd_init_blk_queue(gd, sector_size)) {
453 del_gendisk(gd);
454 goto out;
455 }
456
457 info->rq = gd->queue;
458 info->gd = gd;
459
460 if (info->feature_barrier)
461 xlvbd_barrier(info);
462
463 if (vdisk_info & VDISK_READONLY)
464 set_disk_ro(gd, 1);
465
466 if (vdisk_info & VDISK_REMOVABLE)
467 gd->flags |= GENHD_FL_REMOVABLE;
468
469 if (vdisk_info & VDISK_CDROM)
470 gd->flags |= GENHD_FL_CD;
471
472 return 0;
473
474 out:
475 return err;
476}
477
478static void kick_pending_request_queues(struct blkfront_info *info)
479{
480 if (!RING_FULL(&info->ring)) {
481 /* Re-enable calldowns. */
482 blk_start_queue(info->rq);
483 /* Kick things off immediately. */
484 do_blkif_request(info->rq);
485 }
486}
487
488static void blkif_restart_queue(struct work_struct *work)
489{
490 struct blkfront_info *info = container_of(work, struct blkfront_info, work);
491
492 spin_lock_irq(&blkif_io_lock);
493 if (info->connected == BLKIF_STATE_CONNECTED)
494 kick_pending_request_queues(info);
495 spin_unlock_irq(&blkif_io_lock);
496}
497
498static void blkif_free(struct blkfront_info *info, int suspend)
499{
500 /* Prevent new requests being issued until we fix things up. */
501 spin_lock_irq(&blkif_io_lock);
502 info->connected = suspend ?
503 BLKIF_STATE_SUSPENDED : BLKIF_STATE_DISCONNECTED;
504 /* No more blkif_request(). */
505 if (info->rq)
506 blk_stop_queue(info->rq);
507 /* No more gnttab callback work. */
508 gnttab_cancel_free_callback(&info->callback);
509 spin_unlock_irq(&blkif_io_lock);
510
511 /* Flush gnttab callback work. Must be done with no locks held. */
512 flush_scheduled_work();
513
514 /* Free resources associated with old device channel. */
515 if (info->ring_ref != GRANT_INVALID_REF) {
516 gnttab_end_foreign_access(info->ring_ref, 0,
517 (unsigned long)info->ring.sring);
518 info->ring_ref = GRANT_INVALID_REF;
519 info->ring.sring = NULL;
520 }
521 if (info->irq)
522 unbind_from_irqhandler(info->irq, info);
523 info->evtchn = info->irq = 0;
524
525}
526
527static void blkif_completion(struct blk_shadow *s)
528{
529 int i;
530 for (i = 0; i < s->req.nr_segments; i++)
531 gnttab_end_foreign_access(s->req.seg[i].gref, 0, 0UL);
532}
533
534static irqreturn_t blkif_interrupt(int irq, void *dev_id)
535{
536 struct request *req;
537 struct blkif_response *bret;
538 RING_IDX i, rp;
539 unsigned long flags;
540 struct blkfront_info *info = (struct blkfront_info *)dev_id;
Kiyoshi Uedaf530f0362007-12-11 17:47:36 -0500541 int error;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700542
543 spin_lock_irqsave(&blkif_io_lock, flags);
544
545 if (unlikely(info->connected != BLKIF_STATE_CONNECTED)) {
546 spin_unlock_irqrestore(&blkif_io_lock, flags);
547 return IRQ_HANDLED;
548 }
549
550 again:
551 rp = info->ring.sring->rsp_prod;
552 rmb(); /* Ensure we see queued responses up to 'rp'. */
553
554 for (i = info->ring.rsp_cons; i != rp; i++) {
555 unsigned long id;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700556
557 bret = RING_GET_RESPONSE(&info->ring, i);
558 id = bret->id;
559 req = (struct request *)info->shadow[id].request;
560
561 blkif_completion(&info->shadow[id]);
562
563 add_id_to_freelist(info, id);
564
Kiyoshi Uedaf530f0362007-12-11 17:47:36 -0500565 error = (bret->status == BLKIF_RSP_OKAY) ? 0 : -EIO;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700566 switch (bret->operation) {
567 case BLKIF_OP_WRITE_BARRIER:
568 if (unlikely(bret->status == BLKIF_RSP_EOPNOTSUPP)) {
569 printk(KERN_WARNING "blkfront: %s: write barrier op failed\n",
570 info->gd->disk_name);
Kiyoshi Uedaf530f0362007-12-11 17:47:36 -0500571 error = -EOPNOTSUPP;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700572 info->feature_barrier = 0;
573 xlvbd_barrier(info);
574 }
575 /* fall through */
576 case BLKIF_OP_READ:
577 case BLKIF_OP_WRITE:
578 if (unlikely(bret->status != BLKIF_RSP_OKAY))
579 dev_dbg(&info->xbdev->dev, "Bad return from blkdev data "
580 "request: %x\n", bret->status);
581
Tejun Heo40cbbb72009-04-23 11:05:19 +0900582 __blk_end_request_all(req, error);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700583 break;
584 default:
585 BUG();
586 }
587 }
588
589 info->ring.rsp_cons = i;
590
591 if (i != info->ring.req_prod_pvt) {
592 int more_to_do;
593 RING_FINAL_CHECK_FOR_RESPONSES(&info->ring, more_to_do);
594 if (more_to_do)
595 goto again;
596 } else
597 info->ring.sring->rsp_event = i + 1;
598
599 kick_pending_request_queues(info);
600
601 spin_unlock_irqrestore(&blkif_io_lock, flags);
602
603 return IRQ_HANDLED;
604}
605
606
607static int setup_blkring(struct xenbus_device *dev,
608 struct blkfront_info *info)
609{
610 struct blkif_sring *sring;
611 int err;
612
613 info->ring_ref = GRANT_INVALID_REF;
614
Ian Campbella144ff02008-06-17 10:47:08 +0200615 sring = (struct blkif_sring *)__get_free_page(GFP_NOIO | __GFP_HIGH);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700616 if (!sring) {
617 xenbus_dev_fatal(dev, -ENOMEM, "allocating shared ring");
618 return -ENOMEM;
619 }
620 SHARED_RING_INIT(sring);
621 FRONT_RING_INIT(&info->ring, sring, PAGE_SIZE);
622
Jens Axboe9e973e62009-02-24 08:10:09 +0100623 sg_init_table(info->sg, BLKIF_MAX_SEGMENTS_PER_REQUEST);
624
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700625 err = xenbus_grant_ring(dev, virt_to_mfn(info->ring.sring));
626 if (err < 0) {
627 free_page((unsigned long)sring);
628 info->ring.sring = NULL;
629 goto fail;
630 }
631 info->ring_ref = err;
632
633 err = xenbus_alloc_evtchn(dev, &info->evtchn);
634 if (err)
635 goto fail;
636
637 err = bind_evtchn_to_irqhandler(info->evtchn,
638 blkif_interrupt,
639 IRQF_SAMPLE_RANDOM, "blkif", info);
640 if (err <= 0) {
641 xenbus_dev_fatal(dev, err,
642 "bind_evtchn_to_irqhandler failed");
643 goto fail;
644 }
645 info->irq = err;
646
647 return 0;
648fail:
649 blkif_free(info, 0);
650 return err;
651}
652
653
654/* Common code used when first setting up, and when resuming. */
655static int talk_to_backend(struct xenbus_device *dev,
656 struct blkfront_info *info)
657{
658 const char *message = NULL;
659 struct xenbus_transaction xbt;
660 int err;
661
662 /* Create shared ring, alloc event channel. */
663 err = setup_blkring(dev, info);
664 if (err)
665 goto out;
666
667again:
668 err = xenbus_transaction_start(&xbt);
669 if (err) {
670 xenbus_dev_fatal(dev, err, "starting transaction");
671 goto destroy_blkring;
672 }
673
674 err = xenbus_printf(xbt, dev->nodename,
675 "ring-ref", "%u", info->ring_ref);
676 if (err) {
677 message = "writing ring-ref";
678 goto abort_transaction;
679 }
680 err = xenbus_printf(xbt, dev->nodename,
681 "event-channel", "%u", info->evtchn);
682 if (err) {
683 message = "writing event-channel";
684 goto abort_transaction;
685 }
Markus Armbruster3e334232008-04-02 10:54:02 -0700686 err = xenbus_printf(xbt, dev->nodename, "protocol", "%s",
687 XEN_IO_PROTO_ABI_NATIVE);
688 if (err) {
689 message = "writing protocol";
690 goto abort_transaction;
691 }
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700692
693 err = xenbus_transaction_end(xbt, 0);
694 if (err) {
695 if (err == -EAGAIN)
696 goto again;
697 xenbus_dev_fatal(dev, err, "completing transaction");
698 goto destroy_blkring;
699 }
700
701 xenbus_switch_state(dev, XenbusStateInitialised);
702
703 return 0;
704
705 abort_transaction:
706 xenbus_transaction_end(xbt, 1);
707 if (message)
708 xenbus_dev_fatal(dev, err, "%s", message);
709 destroy_blkring:
710 blkif_free(info, 0);
711 out:
712 return err;
713}
714
715
716/**
717 * Entry point to this code when a new device is created. Allocate the basic
718 * structures and the ring buffer for communication with the backend, and
719 * inform the backend of the appropriate details for those. Switch to
720 * Initialised state.
721 */
722static int blkfront_probe(struct xenbus_device *dev,
723 const struct xenbus_device_id *id)
724{
725 int err, vdevice, i;
726 struct blkfront_info *info;
727
728 /* FIXME: Use dynamic device id if this is not set. */
729 err = xenbus_scanf(XBT_NIL, dev->nodename,
730 "virtual-device", "%i", &vdevice);
731 if (err != 1) {
Chris Lalancette9246b5f2008-09-17 14:30:32 -0700732 /* go looking in the extended area instead */
733 err = xenbus_scanf(XBT_NIL, dev->nodename, "virtual-device-ext",
734 "%i", &vdevice);
735 if (err != 1) {
736 xenbus_dev_fatal(dev, err, "reading virtual-device");
737 return err;
738 }
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700739 }
740
Stefano Stabellinic1c54132010-05-14 12:44:30 +0100741 /* no unplug has been done: do not hook devices != xen vbds */
742 if (xen_hvm_domain() && (xen_platform_pci_unplug & XEN_UNPLUG_IGNORE)) {
743 int major;
744
745 if (!VDEV_IS_EXTENDED(vdevice))
746 major = BLKIF_MAJOR(vdevice);
747 else
748 major = XENVBD_MAJOR;
749
750 if (major != XENVBD_MAJOR) {
751 printk(KERN_INFO
752 "%s: HVM does not support vbd %d as xen block device\n",
753 __FUNCTION__, vdevice);
754 return -ENODEV;
755 }
756 }
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700757 info = kzalloc(sizeof(*info), GFP_KERNEL);
758 if (!info) {
759 xenbus_dev_fatal(dev, -ENOMEM, "allocating info structure");
760 return -ENOMEM;
761 }
762
763 info->xbdev = dev;
764 info->vdevice = vdevice;
765 info->connected = BLKIF_STATE_DISCONNECTED;
766 INIT_WORK(&info->work, blkif_restart_queue);
767
768 for (i = 0; i < BLK_RING_SIZE; i++)
769 info->shadow[i].req.id = i+1;
770 info->shadow[BLK_RING_SIZE-1].req.id = 0x0fffffff;
771
772 /* Front end dir is a number, which is used as the id. */
773 info->handle = simple_strtoul(strrchr(dev->nodename, '/')+1, NULL, 0);
Greg Kroah-Hartmana1b4b122009-04-30 14:43:31 -0700774 dev_set_drvdata(&dev->dev, info);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700775
776 err = talk_to_backend(dev, info);
777 if (err) {
778 kfree(info);
Greg Kroah-Hartmana1b4b122009-04-30 14:43:31 -0700779 dev_set_drvdata(&dev->dev, NULL);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700780 return err;
781 }
782
783 return 0;
784}
785
786
787static int blkif_recover(struct blkfront_info *info)
788{
789 int i;
790 struct blkif_request *req;
791 struct blk_shadow *copy;
792 int j;
793
794 /* Stage 1: Make a safe copy of the shadow state. */
Ian Campbella144ff02008-06-17 10:47:08 +0200795 copy = kmalloc(sizeof(info->shadow),
796 GFP_NOIO | __GFP_REPEAT | __GFP_HIGH);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700797 if (!copy)
798 return -ENOMEM;
799 memcpy(copy, info->shadow, sizeof(info->shadow));
800
801 /* Stage 2: Set up free list. */
802 memset(&info->shadow, 0, sizeof(info->shadow));
803 for (i = 0; i < BLK_RING_SIZE; i++)
804 info->shadow[i].req.id = i+1;
805 info->shadow_free = info->ring.req_prod_pvt;
806 info->shadow[BLK_RING_SIZE-1].req.id = 0x0fffffff;
807
808 /* Stage 3: Find pending requests and requeue them. */
809 for (i = 0; i < BLK_RING_SIZE; i++) {
810 /* Not in use? */
811 if (copy[i].request == 0)
812 continue;
813
814 /* Grab a request slot and copy shadow state into it. */
815 req = RING_GET_REQUEST(&info->ring, info->ring.req_prod_pvt);
816 *req = copy[i].req;
817
818 /* We get a new request id, and must reset the shadow state. */
819 req->id = get_id_from_freelist(info);
820 memcpy(&info->shadow[req->id], &copy[i], sizeof(copy[i]));
821
822 /* Rewrite any grant references invalidated by susp/resume. */
823 for (j = 0; j < req->nr_segments; j++)
824 gnttab_grant_foreign_access_ref(
825 req->seg[j].gref,
826 info->xbdev->otherend_id,
827 pfn_to_mfn(info->shadow[req->id].frame[j]),
828 rq_data_dir(
829 (struct request *)
830 info->shadow[req->id].request));
831 info->shadow[req->id].req = *req;
832
833 info->ring.req_prod_pvt++;
834 }
835
836 kfree(copy);
837
838 xenbus_switch_state(info->xbdev, XenbusStateConnected);
839
840 spin_lock_irq(&blkif_io_lock);
841
842 /* Now safe for us to use the shared ring */
843 info->connected = BLKIF_STATE_CONNECTED;
844
845 /* Send off requeued requests */
846 flush_requests(info);
847
848 /* Kick any other new requests queued since we resumed */
849 kick_pending_request_queues(info);
850
851 spin_unlock_irq(&blkif_io_lock);
852
853 return 0;
854}
855
856/**
857 * We are reconnecting to the backend, due to a suspend/resume, or a backend
858 * driver restart. We tear down our blkif structure and recreate it, but
859 * leave the device-layer structures intact so that this is transparent to the
860 * rest of the kernel.
861 */
862static int blkfront_resume(struct xenbus_device *dev)
863{
Greg Kroah-Hartmana1b4b122009-04-30 14:43:31 -0700864 struct blkfront_info *info = dev_get_drvdata(&dev->dev);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700865 int err;
866
867 dev_dbg(&dev->dev, "blkfront_resume: %s\n", dev->nodename);
868
869 blkif_free(info, info->connected == BLKIF_STATE_CONNECTED);
870
871 err = talk_to_backend(dev, info);
872 if (info->connected == BLKIF_STATE_SUSPENDED && !err)
873 err = blkif_recover(info);
874
875 return err;
876}
877
878
879/*
880 * Invoked when the backend is finally 'ready' (and has told produced
881 * the details about the physical device - #sectors, size, etc).
882 */
883static void blkfront_connect(struct blkfront_info *info)
884{
885 unsigned long long sectors;
886 unsigned long sector_size;
887 unsigned int binfo;
888 int err;
889
890 if ((info->connected == BLKIF_STATE_CONNECTED) ||
891 (info->connected == BLKIF_STATE_SUSPENDED) )
892 return;
893
894 dev_dbg(&info->xbdev->dev, "%s:%s.\n",
895 __func__, info->xbdev->otherend);
896
897 err = xenbus_gather(XBT_NIL, info->xbdev->otherend,
898 "sectors", "%llu", &sectors,
899 "info", "%u", &binfo,
900 "sector-size", "%lu", &sector_size,
901 NULL);
902 if (err) {
903 xenbus_dev_fatal(info->xbdev, err,
904 "reading backend fields at %s",
905 info->xbdev->otherend);
906 return;
907 }
908
909 err = xenbus_gather(XBT_NIL, info->xbdev->otherend,
910 "feature-barrier", "%lu", &info->feature_barrier,
911 NULL);
912 if (err)
913 info->feature_barrier = 0;
914
Chris Lalancette9246b5f2008-09-17 14:30:32 -0700915 err = xlvbd_alloc_gendisk(sectors, info, binfo, sector_size);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700916 if (err) {
917 xenbus_dev_fatal(info->xbdev, err, "xlvbd_add at %s",
918 info->xbdev->otherend);
919 return;
920 }
921
922 xenbus_switch_state(info->xbdev, XenbusStateConnected);
923
924 /* Kick pending requests. */
925 spin_lock_irq(&blkif_io_lock);
926 info->connected = BLKIF_STATE_CONNECTED;
927 kick_pending_request_queues(info);
928 spin_unlock_irq(&blkif_io_lock);
929
930 add_disk(info->gd);
Christian Limpach1d78d702008-04-02 10:54:04 -0700931
932 info->is_ready = 1;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700933}
934
935/**
936 * Handle the change of state of the backend to Closing. We must delete our
937 * device-layer structures now, to ensure that writes are flushed through to
938 * the backend. Once is this done, we can switch to Closed in
939 * acknowledgement.
940 */
941static void blkfront_closing(struct xenbus_device *dev)
942{
Greg Kroah-Hartmana1b4b122009-04-30 14:43:31 -0700943 struct blkfront_info *info = dev_get_drvdata(&dev->dev);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700944 unsigned long flags;
945
946 dev_dbg(&dev->dev, "blkfront_closing: %s removed\n", dev->nodename);
947
948 if (info->rq == NULL)
949 goto out;
950
951 spin_lock_irqsave(&blkif_io_lock, flags);
952
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700953 /* No more blkif_request(). */
954 blk_stop_queue(info->rq);
955
956 /* No more gnttab callback work. */
957 gnttab_cancel_free_callback(&info->callback);
958 spin_unlock_irqrestore(&blkif_io_lock, flags);
959
960 /* Flush gnttab callback work. Must be done with no locks held. */
961 flush_scheduled_work();
962
963 blk_cleanup_queue(info->rq);
964 info->rq = NULL;
965
Ian Campbell31a14402009-05-19 08:27:42 +0200966 del_gendisk(info->gd);
967
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700968 out:
969 xenbus_frontend_closed(dev);
970}
971
972/**
973 * Callback received when the backend's state changes.
974 */
975static void backend_changed(struct xenbus_device *dev,
976 enum xenbus_state backend_state)
977{
Greg Kroah-Hartmana1b4b122009-04-30 14:43:31 -0700978 struct blkfront_info *info = dev_get_drvdata(&dev->dev);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -0700979 struct block_device *bd;
980
981 dev_dbg(&dev->dev, "blkfront:backend_changed.\n");
982
983 switch (backend_state) {
984 case XenbusStateInitialising:
985 case XenbusStateInitWait:
986 case XenbusStateInitialised:
987 case XenbusStateUnknown:
988 case XenbusStateClosed:
989 break;
990
991 case XenbusStateConnected:
992 blkfront_connect(info);
993 break;
994
995 case XenbusStateClosing:
Ian Campbell28afea52009-05-19 08:25:48 +0200996 if (info->gd == NULL) {
997 xenbus_frontend_closed(dev);
998 break;
999 }
Jeremy Fitzhardinge53f0e8a2008-04-02 10:54:03 -07001000 bd = bdget_disk(info->gd, 0);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001001 if (bd == NULL)
1002 xenbus_dev_fatal(dev, -ENODEV, "bdget failed");
1003
1004 mutex_lock(&bd->bd_mutex);
1005 if (info->users > 0)
1006 xenbus_dev_error(dev, -EBUSY,
1007 "Device in use; refusing to close");
1008 else
1009 blkfront_closing(dev);
1010 mutex_unlock(&bd->bd_mutex);
1011 bdput(bd);
1012 break;
1013 }
1014}
1015
1016static int blkfront_remove(struct xenbus_device *dev)
1017{
Greg Kroah-Hartmana1b4b122009-04-30 14:43:31 -07001018 struct blkfront_info *info = dev_get_drvdata(&dev->dev);
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001019
1020 dev_dbg(&dev->dev, "blkfront_remove: %s removed\n", dev->nodename);
1021
1022 blkif_free(info, 0);
1023
1024 kfree(info);
1025
1026 return 0;
1027}
1028
Christian Limpach1d78d702008-04-02 10:54:04 -07001029static int blkfront_is_ready(struct xenbus_device *dev)
1030{
Greg Kroah-Hartmana1b4b122009-04-30 14:43:31 -07001031 struct blkfront_info *info = dev_get_drvdata(&dev->dev);
Christian Limpach1d78d702008-04-02 10:54:04 -07001032
1033 return info->is_ready;
1034}
1035
Al Viroa63c8482008-03-02 10:23:47 -05001036static int blkif_open(struct block_device *bdev, fmode_t mode)
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001037{
Al Viroa63c8482008-03-02 10:23:47 -05001038 struct blkfront_info *info = bdev->bd_disk->private_data;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001039 info->users++;
1040 return 0;
1041}
1042
Al Viroa63c8482008-03-02 10:23:47 -05001043static int blkif_release(struct gendisk *disk, fmode_t mode)
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001044{
Al Viroa63c8482008-03-02 10:23:47 -05001045 struct blkfront_info *info = disk->private_data;
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001046 info->users--;
1047 if (info->users == 0) {
1048 /* Check whether we have been instructed to close. We will
1049 have ignored this request initially, as the device was
1050 still mounted. */
1051 struct xenbus_device *dev = info->xbdev;
1052 enum xenbus_state state = xenbus_read_driver_state(dev->otherend);
1053
Wim Colgate04c06352008-06-17 10:47:08 +02001054 if (state == XenbusStateClosing && info->is_ready)
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001055 blkfront_closing(dev);
1056 }
1057 return 0;
1058}
1059
Alexey Dobriyan83d5cde2009-09-21 17:01:13 -07001060static const struct block_device_operations xlvbd_block_fops =
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001061{
1062 .owner = THIS_MODULE,
Al Viroa63c8482008-03-02 10:23:47 -05001063 .open = blkif_open,
1064 .release = blkif_release,
Ian Campbell597592d2008-02-21 13:03:45 -08001065 .getgeo = blkif_getgeo,
Al Viroa63c8482008-03-02 10:23:47 -05001066 .locked_ioctl = blkif_ioctl,
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001067};
1068
1069
Márton Némethec9c42e2010-01-10 13:39:52 +01001070static const struct xenbus_device_id blkfront_ids[] = {
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001071 { "vbd" },
1072 { "" }
1073};
1074
1075static struct xenbus_driver blkfront = {
1076 .name = "vbd",
1077 .owner = THIS_MODULE,
1078 .ids = blkfront_ids,
1079 .probe = blkfront_probe,
1080 .remove = blkfront_remove,
1081 .resume = blkfront_resume,
1082 .otherend_changed = backend_changed,
Christian Limpach1d78d702008-04-02 10:54:04 -07001083 .is_ready = blkfront_is_ready,
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001084};
1085
1086static int __init xlblk_init(void)
1087{
Jeremy Fitzhardinge6e833582008-08-19 13:16:17 -07001088 if (!xen_domain())
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001089 return -ENODEV;
1090
1091 if (register_blkdev(XENVBD_MAJOR, DEV_NAME)) {
1092 printk(KERN_WARNING "xen_blk: can't get major %d with name %s\n",
1093 XENVBD_MAJOR, DEV_NAME);
1094 return -ENODEV;
1095 }
1096
1097 return xenbus_register_frontend(&blkfront);
1098}
1099module_init(xlblk_init);
1100
1101
Jan Beulich5a60d0c2008-06-17 10:47:08 +02001102static void __exit xlblk_exit(void)
Jeremy Fitzhardinge9f27ee52007-07-17 18:37:06 -07001103{
1104 return xenbus_unregister_driver(&blkfront);
1105}
1106module_exit(xlblk_exit);
1107
1108MODULE_DESCRIPTION("Xen virtual block device frontend");
1109MODULE_LICENSE("GPL");
1110MODULE_ALIAS_BLOCKDEV_MAJOR(XENVBD_MAJOR);
Mark McLoughlind2f0c522008-04-02 10:54:05 -07001111MODULE_ALIAS("xen:vbd");
Mark McLoughlin4f93f09b2008-04-02 10:54:06 -07001112MODULE_ALIAS("xenblk");