lguest_blk.c 14.3 KB
Newer Older
1 2
/*D:400
 * The Guest block driver
R
Rusty Russell 已提交
3
 *
4 5 6 7 8 9
 * This is a simple block driver, which appears as /dev/lgba, lgbb, lgbc etc.
 * The mechanism is simple: we place the information about the request in the
 * device page, then use SEND_DMA (containing the data for a write, or an empty
 * "ping" DMA for a read).
 :*/
/* Copyright 2006 Rusty Russell <rusty@rustcorp.com.au> IBM Corporation
R
Rusty Russell 已提交
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */
//#define DEBUG
#include <linux/init.h>
#include <linux/types.h>
#include <linux/blkdev.h>
#include <linux/interrupt.h>
#include <linux/lguest_bus.h>

static char next_block_index = 'a';

34 35 36 37 38 39 40
/*D:420 Here is the structure which holds all the information we need about
 * each Guest block device.
 *
 * I'm sure at this stage, you're wondering "hey, where was the adventure I was
 * promised?" and thinking "Rusty sucks, I shall say nasty things about him on
 * my blog".  I think Real adventures have boring bits, too, and you're in the
 * middle of one.  But it gets better.  Just not quite yet. */
R
Rusty Russell 已提交
41 42
struct blockdev
{
43 44 45
	/* The block queue infrastructure wants a spinlock: it is held while it
	 * calls our block request function.  We grab it in our interrupt
	 * handler so the responses don't mess with new requests. */
R
Rusty Russell 已提交
46 47
	spinlock_t lock;

48
	/* The disk structure registered with kernel. */
R
Rusty Russell 已提交
49 50
	struct gendisk *disk;

51 52 53
	/* The major device number for this disk, and the interrupt.  We only
	 * really keep them here for completeness; we'd need them if we
	 * supported device unplugging. */
R
Rusty Russell 已提交
54 55 56
	int major;
	int irq;

57
	/* The physical address of this device's memory page */
R
Rusty Russell 已提交
58
	unsigned long phys_addr;
59
	/* The mapped memory page for convenient acces. */
R
Rusty Russell 已提交
60 61
	struct lguest_block_page *lb_page;

62
	/* We only have a single request outstanding at a time: this is it. */
R
Rusty Russell 已提交
63 64 65 66
	struct lguest_dma dma;
	struct request *req;
};

67 68 69 70 71 72 73 74 75 76 77
/*D:495 We originally used end_request() throughout the driver, but it turns
 * out that end_request() is deprecated, and doesn't actually end the request
 * (which seems like a good reason to deprecate it!).  It simply ends the first
 * bio.  So if we had 3 bios in a "struct request" we would do all 3,
 * end_request(), do 2, end_request(), do 1 and end_request(): twice as much
 * work as we needed to do.
 *
 * This reinforced to me that I do not understand the block layer.
 *
 * Nonetheless, Jens Axboe gave me this nice helper to end all chunks of a
 * request.  This improved disk speed by 130%. */
R
Rusty Russell 已提交
78 79 80 81 82 83 84 85 86
static void end_entire_request(struct request *req, int uptodate)
{
	if (end_that_request_first(req, uptodate, req->hard_nr_sectors))
		BUG();
	add_disk_randomness(req->rq_disk);
	blkdev_dequeue_request(req);
	end_that_request_last(req, uptodate);
}

87 88 89 90 91 92 93 94 95 96
/* I'm told there are only two stories in the world worth telling: love and
 * hate.  So there used to be a love scene here like this:
 *
 *  Launcher:	We could make beautiful I/O together, you and I.
 *  Guest:	My, that's a big disk!
 *
 * Unfortunately, it was just too raunchy for our otherwise-gentle tale. */

/*D:490 This is the interrupt handler, called when a block read or write has
 * been completed for us. */
R
Rusty Russell 已提交
97 98
static irqreturn_t lgb_irq(int irq, void *_bd)
{
99 100 101
	/* We handed our "struct blockdev" as the argument to request_irq(), so
	 * it is passed through to us here.  This tells us which device we're
	 * dealing with in case we have more than one. */
R
Rusty Russell 已提交
102 103 104
	struct blockdev *bd = _bd;
	unsigned long flags;

105 106
	/* We weren't doing anything?  Strange, but could happen if we shared
	 * interrupts (we don't!). */
R
Rusty Russell 已提交
107 108 109 110 111
	if (!bd->req) {
		pr_debug("No work!\n");
		return IRQ_NONE;
	}

112
	/* Not done yet?  That's equally strange. */
R
Rusty Russell 已提交
113 114 115 116 117
	if (!bd->lb_page->result) {
		pr_debug("No result!\n");
		return IRQ_NONE;
	}

118
	/* We have to grab the lock before ending the request. */
R
Rusty Russell 已提交
119
	spin_lock_irqsave(&bd->lock, flags);
120 121
	/* "result" is 1 for success, 2 for failure: end_entire_request() wants
	 * to know whether this succeeded or not. */
R
Rusty Russell 已提交
122
	end_entire_request(bd->req, bd->lb_page->result == 1);
123
	/* Clear out request, it's done. */
R
Rusty Russell 已提交
124
	bd->req = NULL;
125
	/* Reset incoming DMA for next time. */
R
Rusty Russell 已提交
126
	bd->dma.used_len = 0;
127
	/* Ready for more reads or writes */
R
Rusty Russell 已提交
128 129
	blk_start_queue(bd->disk->queue);
	spin_unlock_irqrestore(&bd->lock, flags);
130 131

	/* The interrupt was for us, we dealt with it. */
R
Rusty Russell 已提交
132 133 134
	return IRQ_HANDLED;
}

135 136 137 138 139 140 141 142
/*D:480 The block layer's "struct request" contains a number of "struct bio"s,
 * each of which contains "struct bio_vec"s, each of which contains a page, an
 * offset and a length.
 *
 * Fortunately there are iterators to help us walk through the "struct
 * request".  Even more fortunately, there were plenty of places to steal the
 * code from.  We pack the "struct request" into our "struct lguest_dma" and
 * return the total length. */
R
Rusty Russell 已提交
143 144
static unsigned int req_to_dma(struct request *req, struct lguest_dma *dma)
{
145 146 147
	unsigned int i = 0, len = 0;
	struct req_iterator iter;
	struct bio_vec *bvec;
R
Rusty Russell 已提交
148

149
	rq_for_each_segment(bvec, req, iter) {
150 151 152 153 154 155 156 157 158 159 160 161
		/* We told the block layer not to give us too many. */
		BUG_ON(i == LGUEST_MAX_DMA_SECTIONS);
		/* If we had a zero-length segment, it would look like
		 * the end of the data referred to by the "struct
		 * lguest_dma", so make sure that doesn't happen. */
		BUG_ON(!bvec->bv_len);
		/* Convert page & offset to a physical address */
		dma->addr[i] = page_to_phys(bvec->bv_page)
			+ bvec->bv_offset;
		dma->len[i] = bvec->bv_len;
		len += bvec->bv_len;
		i++;
R
Rusty Russell 已提交
162
	}
163
	/* If the array isn't full, we mark the end with a 0 length */
R
Rusty Russell 已提交
164 165 166 167 168
	if (i < LGUEST_MAX_DMA_SECTIONS)
		dma->len[i] = 0;
	return len;
}

169 170
/* This creates an empty DMA, useful for prodding the Host without sending data
 * (ie. when we want to do a read) */
R
Rusty Russell 已提交
171 172 173 174 175
static void empty_dma(struct lguest_dma *dma)
{
	dma->len[0] = 0;
}

176
/*D:470 Setting up a request is fairly easy: */
R
Rusty Russell 已提交
177 178 179
static void setup_req(struct blockdev *bd,
		      int type, struct request *req, struct lguest_dma *dma)
{
180
	/* The type is 1 (write) or 0 (read). */
R
Rusty Russell 已提交
181
	bd->lb_page->type = type;
182
	/* The sector on disk where the read or write starts. */
R
Rusty Russell 已提交
183
	bd->lb_page->sector = req->sector;
184
	/* The result is initialized to 0 (unfinished). */
R
Rusty Russell 已提交
185
	bd->lb_page->result = 0;
186
	/* The current request (so we can end it in the interrupt handler). */
R
Rusty Russell 已提交
187
	bd->req = req;
188 189 190
	/* The number of bytes: returned as a side-effect of req_to_dma(),
	 * which packs the block layer's "struct request" into our "struct
	 * lguest_dma" */
R
Rusty Russell 已提交
191 192 193
	bd->lb_page->bytes = req_to_dma(req, dma);
}

194 195
/*D:450 Write is pretty straightforward: we pack the request into a "struct
 * lguest_dma", then use SEND_DMA to send the request. */
R
Rusty Russell 已提交
196 197 198 199 200 201 202 203 204 205
static void do_write(struct blockdev *bd, struct request *req)
{
	struct lguest_dma send;

	pr_debug("lgb: WRITE sector %li\n", (long)req->sector);
	setup_req(bd, 1, req, &send);

	lguest_send_dma(bd->phys_addr, &send);
}

206 207 208
/* Read is similar to write, except we pack the request into our receive
 * "struct lguest_dma" and send through an empty DMA just to tell the Host that
 * there's a request pending. */
R
Rusty Russell 已提交
209 210 211 212 213 214 215 216 217 218 219
static void do_read(struct blockdev *bd, struct request *req)
{
	struct lguest_dma ping;

	pr_debug("lgb: READ sector %li\n", (long)req->sector);
	setup_req(bd, 0, req, &bd->dma);

	empty_dma(&ping);
	lguest_send_dma(bd->phys_addr, &ping);
}

220 221 222
/*D:440 This where requests come in: we get handed the request queue and are
 * expected to pull a "struct request" off it until we've finished them or
 * we're waiting for a reply: */
223
static void do_lgb_request(struct request_queue *q)
R
Rusty Russell 已提交
224 225 226 227 228
{
	struct blockdev *bd;
	struct request *req;

again:
229 230 231
	/* This sometimes returns NULL even on the very first time around.  I
	 * wonder if it's something to do with letting elves handle the request
	 * queue... */
R
Rusty Russell 已提交
232 233 234 235
	req = elv_next_request(q);
	if (!req)
		return;

236
	/* We attached the struct blockdev to the disk: get it back */
R
Rusty Russell 已提交
237
	bd = req->rq_disk->private_data;
238 239
	/* Sometimes we get repeated requests after blk_stop_queue(), but we
	 * can only handle one at a time. */
R
Rusty Russell 已提交
240 241 242
	if (bd->req)
		return;

243
	/* We only do reads and writes: no tricky business! */
R
Rusty Russell 已提交
244 245 246 247 248 249 250 251 252 253 254 255
	if (!blk_fs_request(req)) {
		pr_debug("Got non-command 0x%08x\n", req->cmd_type);
		req->errors++;
		end_entire_request(req, 0);
		goto again;
	}

	if (rq_data_dir(req) == WRITE)
		do_write(bd, req);
	else
		do_read(bd, req);

256 257
	/* We've put out the request, so stop any more coming in until we get
	 * an interrupt, which takes us to lgb_irq() to re-enable the queue. */
R
Rusty Russell 已提交
258 259 260
	blk_stop_queue(q);
}

261 262
/*D:430 This is the "struct block_device_operations" we attach to the disk at
 * the end of lguestblk_probe().  It doesn't seem to want much. */
R
Rusty Russell 已提交
263 264 265 266
static struct block_device_operations lguestblk_fops = {
	.owner = THIS_MODULE,
};

267 268 269 270 271 272
/*D:425 Setting up a disk device seems to involve a lot of code.  I'm not sure
 * quite why.  I do know that the IDE code sent two or three of the maintainers
 * insane, perhaps this is the fringe of the same disease?
 *
 * As in the console code, the probe function gets handed the generic
 * lguest_device from lguest_bus.c: */
R
Rusty Russell 已提交
273 274 275 276 277 278
static int lguestblk_probe(struct lguest_device *lgdev)
{
	struct blockdev *bd;
	int err;
	int irqflags = IRQF_SHARED;

279 280
	/* First we allocate our own "struct blockdev" and initialize the easy
	 * fields. */
R
Rusty Russell 已提交
281 282 283 284 285 286 287 288 289
	bd = kmalloc(sizeof(*bd), GFP_KERNEL);
	if (!bd)
		return -ENOMEM;

	spin_lock_init(&bd->lock);
	bd->irq = lgdev_irq(lgdev);
	bd->req = NULL;
	bd->dma.used_len = 0;
	bd->dma.len[0] = 0;
290 291
	/* The descriptor in the lguest_devices array provided by the Host
	 * gives the Guest the physical page number of the device's page. */
R
Rusty Russell 已提交
292 293
	bd->phys_addr = (lguest_devices[lgdev->index].pfn << PAGE_SHIFT);

294
	/* We use lguest_map() to get a pointer to the device page */
R
Rusty Russell 已提交
295 296 297 298 299 300
	bd->lb_page = lguest_map(bd->phys_addr, 1);
	if (!bd->lb_page) {
		err = -ENOMEM;
		goto out_free_bd;
	}

301
	/* We need a major device number: 0 means "assign one dynamically". */
R
Rusty Russell 已提交
302 303 304 305 306 307
	bd->major = register_blkdev(0, "lguestblk");
	if (bd->major < 0) {
		err = bd->major;
		goto out_unmap;
	}

308
	/* This allocates a "struct gendisk" where we pack all the information
309 310 311 312 313 314
	 * about the disk which the rest of Linux sees.  The argument is the
	 * number of minor devices desired: we need one minor for the main
	 * disk, and one for each partition.  Of course, we can't possibly know
	 * how many partitions are on the disk (add_disk does that).
	 */
	bd->disk = alloc_disk(16);
R
Rusty Russell 已提交
315 316 317 318 319
	if (!bd->disk) {
		err = -ENOMEM;
		goto out_unregister_blkdev;
	}

320 321 322
	/* Every disk needs a queue for requests to come in: we set up the
	 * queue with a callback function (the core of our driver) and the lock
	 * to use. */
R
Rusty Russell 已提交
323 324 325 326 327 328
	bd->disk->queue = blk_init_queue(do_lgb_request, &bd->lock);
	if (!bd->disk->queue) {
		err = -ENOMEM;
		goto out_put_disk;
	}

329 330 331 332 333 334 335
	/* We can only handle a certain number of pointers in our SEND_DMA
	 * call, so we set that with blk_queue_max_hw_segments().  This is not
	 * to be confused with blk_queue_max_phys_segments() of course!  I
	 * know, who could possibly confuse the two?
	 *
	 * Well, it's simple to tell them apart: this one seems to work and the
	 * other one didn't. */
R
Rusty Russell 已提交
336
	blk_queue_max_hw_segments(bd->disk->queue, LGUEST_MAX_DMA_SECTIONS);
337 338 339 340 341

	/* Due to technical limitations of our Host (and simple coding) we
	 * can't have a single buffer which crosses a page boundary.  Tell it
	 * here.  This means that our maximum request size is 16
	 * (LGUEST_MAX_DMA_SECTIONS) pages. */
R
Rusty Russell 已提交
342 343
	blk_queue_segment_boundary(bd->disk->queue, PAGE_SIZE-1);

344 345 346 347 348 349
	/* We name our disk: this becomes the device name when udev does its
	 * magic thing and creates the device node, such as /dev/lgba.
	 * next_block_index is a global which starts at 'a'.  Unfortunately
	 * this simple increment logic means that the 27th disk will be called
	 * "/dev/lgb{".  In that case, I recommend having at least 29 disks, so
	 * your /dev directory will be balanced. */
R
Rusty Russell 已提交
350
	sprintf(bd->disk->disk_name, "lgb%c", next_block_index++);
351 352 353 354

	/* We look to the device descriptor again to see if this device's
	 * interrupts are expected to be random.  If they are, we tell the irq
	 * subsystem.  At the moment this bit is always set. */
R
Rusty Russell 已提交
355 356
	if (lguest_devices[lgdev->index].features & LGUEST_DEVICE_F_RANDOMNESS)
		irqflags |= IRQF_SAMPLE_RANDOM;
357 358 359 360

	/* Now we have the name and irqflags, we can request the interrupt; we
	 * give it the "struct blockdev" we have set up to pass to lgb_irq()
	 * when there is an interrupt. */
R
Rusty Russell 已提交
361 362 363 364
	err = request_irq(bd->irq, lgb_irq, irqflags, bd->disk->disk_name, bd);
	if (err)
		goto out_cleanup_queue;

365 366 367 368
	/* We bind our one-entry DMA pool to the key for this block device so
	 * the Host can reply to our requests.  The key is equal to the
	 * physical address of the device's page, which is conveniently
	 * unique. */
R
Rusty Russell 已提交
369 370 371 372
	err = lguest_bind_dma(bd->phys_addr, &bd->dma, 1, bd->irq);
	if (err)
		goto out_free_irq;

373
	/* We finish our disk initialization and add the disk to the system. */
R
Rusty Russell 已提交
374 375 376 377
	bd->disk->major = bd->major;
	bd->disk->first_minor = 0;
	bd->disk->private_data = bd;
	bd->disk->fops = &lguestblk_fops;
378
	/* This is initialized to the disk size by the Launcher. */
R
Rusty Russell 已提交
379 380 381 382 383 384
	set_capacity(bd->disk, bd->lb_page->num_sectors);
	add_disk(bd->disk);

	printk(KERN_INFO "%s: device %i at major %d\n",
	       bd->disk->disk_name, lgdev->index, bd->major);

385 386
	/* We don't need to keep the "struct blockdev" around, but if we ever
	 * implemented device removal, we'd need this. */
R
Rusty Russell 已提交
387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404
	lgdev->private = bd;
	return 0;

out_free_irq:
	free_irq(bd->irq, bd);
out_cleanup_queue:
	blk_cleanup_queue(bd->disk->queue);
out_put_disk:
	put_disk(bd->disk);
out_unregister_blkdev:
	unregister_blkdev(bd->major, "lguestblk");
out_unmap:
	lguest_unmap(bd->lb_page);
out_free_bd:
	kfree(bd);
	return err;
}

405 406
/*D:410 The boilerplate code for registering the lguest block driver is just
 * like the console: */
R
Rusty Russell 已提交
407 408 409 410 411 412 413 414 415 416 417 418 419 420 421
static struct lguest_driver lguestblk_drv = {
	.name = "lguestblk",
	.owner = THIS_MODULE,
	.device_type = LGUEST_DEVICE_T_BLOCK,
	.probe = lguestblk_probe,
};

static __init int lguestblk_init(void)
{
	return register_lguest_driver(&lguestblk_drv);
}
module_init(lguestblk_init);

MODULE_DESCRIPTION("Lguest block driver");
MODULE_LICENSE("GPL");