null_blk.c 18.9 KB
Newer Older
1
#include <linux/module.h>
2

3 4 5 6 7 8 9 10
#include <linux/moduleparam.h>
#include <linux/sched.h>
#include <linux/fs.h>
#include <linux/blkdev.h>
#include <linux/init.h>
#include <linux/slab.h>
#include <linux/blk-mq.h>
#include <linux/hrtimer.h>
11
#include <linux/lightnvm.h>
12 13 14 15 16 17 18 19 20

struct nullb_cmd {
	struct list_head list;
	struct llist_node ll_list;
	struct call_single_data csd;
	struct request *rq;
	struct bio *bio;
	unsigned int tag;
	struct nullb_queue *nq;
21
	struct hrtimer timer;
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
};

struct nullb_queue {
	unsigned long *tag_map;
	wait_queue_head_t wait;
	unsigned int queue_depth;

	struct nullb_cmd *cmds;
};

struct nullb {
	struct list_head list;
	unsigned int index;
	struct request_queue *q;
	struct gendisk *disk;
37
	struct nvm_dev *ndev;
38
	struct blk_mq_tag_set tag_set;
39 40 41 42 43 44
	struct hrtimer timer;
	unsigned int queue_depth;
	spinlock_t lock;

	struct nullb_queue *queues;
	unsigned int nr_queues;
45
	char disk_name[DISK_NAME_LEN];
46 47 48 49 50 51
};

static LIST_HEAD(nullb_list);
static struct mutex lock;
static int null_major;
static int nullb_indexes;
M
Matias Bjørling 已提交
52
static struct kmem_cache *ppa_cache;
53 54 55 56 57

enum {
	NULL_IRQ_NONE		= 0,
	NULL_IRQ_SOFTIRQ	= 1,
	NULL_IRQ_TIMER		= 2,
58
};
59

60
enum {
61 62 63 64 65
	NULL_Q_BIO		= 0,
	NULL_Q_RQ		= 1,
	NULL_Q_MQ		= 2,
};

66
static int submit_queues;
67 68 69 70 71 72 73 74
module_param(submit_queues, int, S_IRUGO);
MODULE_PARM_DESC(submit_queues, "Number of submission queues");

static int home_node = NUMA_NO_NODE;
module_param(home_node, int, S_IRUGO);
MODULE_PARM_DESC(home_node, "Home node for the device");

static int queue_mode = NULL_Q_MQ;
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95

static int null_param_store_val(const char *str, int *val, int min, int max)
{
	int ret, new_val;

	ret = kstrtoint(str, 10, &new_val);
	if (ret)
		return -EINVAL;

	if (new_val < min || new_val > max)
		return -EINVAL;

	*val = new_val;
	return 0;
}

static int null_set_queue_mode(const char *str, const struct kernel_param *kp)
{
	return null_param_store_val(str, &queue_mode, NULL_Q_BIO, NULL_Q_MQ);
}

96
static const struct kernel_param_ops null_queue_mode_param_ops = {
97 98 99 100 101
	.set	= null_set_queue_mode,
	.get	= param_get_int,
};

device_param_cb(queue_mode, &null_queue_mode_param_ops, &queue_mode, S_IRUGO);
102
MODULE_PARM_DESC(queue_mode, "Block interface to use (0=bio,1=rq,2=multiqueue)");
103 104 105 106 107 108 109 110 111 112 113 114 115

static int gb = 250;
module_param(gb, int, S_IRUGO);
MODULE_PARM_DESC(gb, "Size in GB");

static int bs = 512;
module_param(bs, int, S_IRUGO);
MODULE_PARM_DESC(bs, "Block size (in bytes)");

static int nr_devices = 2;
module_param(nr_devices, int, S_IRUGO);
MODULE_PARM_DESC(nr_devices, "Number of devices to register");

116 117 118 119
static bool use_lightnvm;
module_param(use_lightnvm, bool, S_IRUGO);
MODULE_PARM_DESC(use_lightnvm, "Register as a LightNVM device");

J
Jens Axboe 已提交
120 121 122 123
static bool blocking;
module_param(blocking, bool, S_IRUGO);
MODULE_PARM_DESC(blocking, "Register as a blocking blk-mq driver device");

124
static int irqmode = NULL_IRQ_SOFTIRQ;
125 126 127 128 129 130 131

static int null_set_irqmode(const char *str, const struct kernel_param *kp)
{
	return null_param_store_val(str, &irqmode, NULL_IRQ_NONE,
					NULL_IRQ_TIMER);
}

132
static const struct kernel_param_ops null_irqmode_param_ops = {
133 134 135 136 137
	.set	= null_set_irqmode,
	.get	= param_get_int,
};

device_param_cb(irqmode, &null_irqmode_param_ops, &irqmode, S_IRUGO);
138 139
MODULE_PARM_DESC(irqmode, "IRQ completion handler. 0-none, 1-softirq, 2-timer");

140 141
static unsigned long completion_nsec = 10000;
module_param(completion_nsec, ulong, S_IRUGO);
142 143 144 145 146 147
MODULE_PARM_DESC(completion_nsec, "Time in ns to complete a request in hardware. Default: 10,000ns");

static int hw_queue_depth = 64;
module_param(hw_queue_depth, int, S_IRUGO);
MODULE_PARM_DESC(hw_queue_depth, "Queue depth for each hardware queue. Default: 64");

148
static bool use_per_node_hctx = false;
149
module_param(use_per_node_hctx, bool, S_IRUGO);
150
MODULE_PARM_DESC(use_per_node_hctx, "Use per-node allocation for hardware context queues. Default: false");
151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177

static void put_tag(struct nullb_queue *nq, unsigned int tag)
{
	clear_bit_unlock(tag, nq->tag_map);

	if (waitqueue_active(&nq->wait))
		wake_up(&nq->wait);
}

static unsigned int get_tag(struct nullb_queue *nq)
{
	unsigned int tag;

	do {
		tag = find_first_zero_bit(nq->tag_map, nq->queue_depth);
		if (tag >= nq->queue_depth)
			return -1U;
	} while (test_and_set_bit_lock(tag, nq->tag_map));

	return tag;
}

static void free_cmd(struct nullb_cmd *cmd)
{
	put_tag(cmd->nq, cmd->tag);
}

178 179
static enum hrtimer_restart null_cmd_timer_expired(struct hrtimer *timer);

180 181 182 183 184 185 186 187 188 189
static struct nullb_cmd *__alloc_cmd(struct nullb_queue *nq)
{
	struct nullb_cmd *cmd;
	unsigned int tag;

	tag = get_tag(nq);
	if (tag != -1U) {
		cmd = &nq->cmds[tag];
		cmd->tag = tag;
		cmd->nq = nq;
190 191 192 193 194
		if (irqmode == NULL_IRQ_TIMER) {
			hrtimer_init(&cmd->timer, CLOCK_MONOTONIC,
				     HRTIMER_MODE_REL);
			cmd->timer.function = null_cmd_timer_expired;
		}
195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224
		return cmd;
	}

	return NULL;
}

static struct nullb_cmd *alloc_cmd(struct nullb_queue *nq, int can_wait)
{
	struct nullb_cmd *cmd;
	DEFINE_WAIT(wait);

	cmd = __alloc_cmd(nq);
	if (cmd || !can_wait)
		return cmd;

	do {
		prepare_to_wait(&nq->wait, &wait, TASK_UNINTERRUPTIBLE);
		cmd = __alloc_cmd(nq);
		if (cmd)
			break;

		io_schedule();
	} while (1);

	finish_wait(&nq->wait, &wait);
	return cmd;
}

static void end_cmd(struct nullb_cmd *cmd)
{
225 226
	struct request_queue *q = NULL;

M
Mike Krinkin 已提交
227 228 229
	if (cmd->rq)
		q = cmd->rq->q;

230 231
	switch (queue_mode)  {
	case NULL_Q_MQ:
232
		blk_mq_end_request(cmd->rq, 0);
233 234 235 236 237 238
		return;
	case NULL_Q_RQ:
		INIT_LIST_HEAD(&cmd->rq->queuelist);
		blk_end_request_all(cmd->rq, 0);
		break;
	case NULL_Q_BIO:
239
		bio_endio(cmd->bio);
240
		break;
241
	}
242

243 244
	free_cmd(cmd);

245
	/* Restart queue if needed, as we are freeing a tag */
246
	if (queue_mode == NULL_Q_RQ && blk_queue_stopped(q)) {
247 248 249
		unsigned long flags;

		spin_lock_irqsave(q->queue_lock, flags);
250
		blk_start_queue_async(q);
251
		spin_unlock_irqrestore(q->queue_lock, flags);
252
	}
253 254 255 256 257
}

static enum hrtimer_restart null_cmd_timer_expired(struct hrtimer *timer)
{
	end_cmd(container_of(timer, struct nullb_cmd, timer));
258 259 260 261 262 263

	return HRTIMER_NORESTART;
}

static void null_cmd_end_timer(struct nullb_cmd *cmd)
{
T
Thomas Gleixner 已提交
264
	ktime_t kt = completion_nsec;
265

266
	hrtimer_start(&cmd->timer, kt, HRTIMER_MODE_REL);
267 268 269 270
}

static void null_softirq_done_fn(struct request *rq)
{
271 272 273 274
	if (queue_mode == NULL_Q_MQ)
		end_cmd(blk_mq_rq_to_pdu(rq));
	else
		end_cmd(rq->special);
275 276 277 278 279 280 281
}

static inline void null_handle_cmd(struct nullb_cmd *cmd)
{
	/* Complete IO by inline, softirq or timer */
	switch (irqmode) {
	case NULL_IRQ_SOFTIRQ:
282 283
		switch (queue_mode)  {
		case NULL_Q_MQ:
284
			blk_mq_complete_request(cmd->rq, 0);
285 286 287 288 289 290 291 292 293 294 295 296 297
			break;
		case NULL_Q_RQ:
			blk_complete_request(cmd->rq);
			break;
		case NULL_Q_BIO:
			/*
			 * XXX: no proper submitting cpu information available.
			 */
			end_cmd(cmd);
			break;
		}
		break;
	case NULL_IRQ_NONE:
298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315
		end_cmd(cmd);
		break;
	case NULL_IRQ_TIMER:
		null_cmd_end_timer(cmd);
		break;
	}
}

static struct nullb_queue *nullb_to_queue(struct nullb *nullb)
{
	int index = 0;

	if (nullb->nr_queues != 1)
		index = raw_smp_processor_id() / ((nr_cpu_ids + nullb->nr_queues - 1) / nullb->nr_queues);

	return &nullb->queues[index];
}

316
static blk_qc_t null_queue_bio(struct request_queue *q, struct bio *bio)
317 318 319 320 321 322 323 324 325
{
	struct nullb *nullb = q->queuedata;
	struct nullb_queue *nq = nullb_to_queue(nullb);
	struct nullb_cmd *cmd;

	cmd = alloc_cmd(nq, 1);
	cmd->bio = bio;

	null_handle_cmd(cmd);
326
	return BLK_QC_T_NONE;
327 328 329 330 331 332 333 334 335 336 337 338 339 340
}

static int null_rq_prep_fn(struct request_queue *q, struct request *req)
{
	struct nullb *nullb = q->queuedata;
	struct nullb_queue *nq = nullb_to_queue(nullb);
	struct nullb_cmd *cmd;

	cmd = alloc_cmd(nq, 0);
	if (cmd) {
		cmd->rq = req;
		req->special = cmd;
		return BLKPREP_OK;
	}
341
	blk_stop_queue(q);
342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358

	return BLKPREP_DEFER;
}

static void null_request_fn(struct request_queue *q)
{
	struct request *rq;

	while ((rq = blk_fetch_request(q)) != NULL) {
		struct nullb_cmd *cmd = rq->special;

		spin_unlock_irq(q->queue_lock);
		null_handle_cmd(cmd);
		spin_lock_irq(q->queue_lock);
	}
}

359 360
static int null_queue_rq(struct blk_mq_hw_ctx *hctx,
			 const struct blk_mq_queue_data *bd)
361
{
362
	struct nullb_cmd *cmd = blk_mq_rq_to_pdu(bd->rq);
363

J
Jens Axboe 已提交
364 365
	might_sleep_if(hctx->flags & BLK_MQ_F_BLOCKING);

366 367 368 369
	if (irqmode == NULL_IRQ_TIMER) {
		hrtimer_init(&cmd->timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
		cmd->timer.function = null_cmd_timer_expired;
	}
370
	cmd->rq = bd->rq;
371 372
	cmd->nq = hctx->driver_data;

373
	blk_mq_start_request(bd->rq);
374

375 376 377 378
	null_handle_cmd(cmd);
	return BLK_MQ_RQ_QUEUE_OK;
}

379 380 381 382 383 384 385 386 387
static void null_init_queue(struct nullb *nullb, struct nullb_queue *nq)
{
	BUG_ON(!nullb);
	BUG_ON(!nq);

	init_waitqueue_head(&nq->wait);
	nq->queue_depth = nullb->queue_depth;
}

388 389 390 391 392 393 394
static int null_init_hctx(struct blk_mq_hw_ctx *hctx, void *data,
			  unsigned int index)
{
	struct nullb *nullb = data;
	struct nullb_queue *nq = &nullb->queues[index];

	hctx->driver_data = nq;
395 396
	null_init_queue(nullb, nq);
	nullb->nr_queues++;
397 398 399 400

	return 0;
}

401
static const struct blk_mq_ops null_mq_ops = {
402 403
	.queue_rq       = null_queue_rq,
	.init_hctx	= null_init_hctx,
404
	.complete	= null_softirq_done_fn,
405 406
};

407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422
static void cleanup_queue(struct nullb_queue *nq)
{
	kfree(nq->tag_map);
	kfree(nq->cmds);
}

static void cleanup_queues(struct nullb *nullb)
{
	int i;

	for (i = 0; i < nullb->nr_queues; i++)
		cleanup_queue(&nullb->queues[i]);

	kfree(nullb->queues);
}

423 424 425 426 427 428
#ifdef CONFIG_NVM

static void null_lnvm_end_io(struct request *rq, int error)
{
	struct nvm_rq *rqd = rq->end_io_data;

429 430
	rqd->error = error;
	nvm_end_io(rqd);
431 432 433 434

	blk_put_request(rq);
}

435
static int null_lnvm_submit_io(struct nvm_dev *dev, struct nvm_rq *rqd)
436
{
437
	struct request_queue *q = dev->q;
438 439 440
	struct request *rq;
	struct bio *bio = rqd->bio;

441 442
	rq = blk_mq_alloc_request(q,
		op_is_write(bio_op(bio)) ? REQ_OP_DRV_OUT : REQ_OP_DRV_IN, 0);
443 444 445
	if (IS_ERR(rq))
		return -ENOMEM;

446
	blk_init_request_from_bio(rq, bio);
447 448 449 450 451 452 453 454

	rq->end_io_data = rqd;

	blk_execute_rq_nowait(q, NULL, rq, 0, null_lnvm_end_io);

	return 0;
}

455
static int null_lnvm_id(struct nvm_dev *dev, struct nvm_id *id)
456 457
{
	sector_t size = gb * 1024 * 1024 * 1024ULL;
458
	sector_t blksize;
459 460 461 462
	struct nvm_id_group *grp;

	id->ver_id = 0x1;
	id->vmnt = 0;
463
	id->cap = 0x2;
464
	id->dom = 0x1;
465 466 467 468 469 470 471 472 473 474 475 476 477

	id->ppaf.blk_offset = 0;
	id->ppaf.blk_len = 16;
	id->ppaf.pg_offset = 16;
	id->ppaf.pg_len = 16;
	id->ppaf.sect_offset = 32;
	id->ppaf.sect_len = 8;
	id->ppaf.pln_offset = 40;
	id->ppaf.pln_len = 8;
	id->ppaf.lun_offset = 48;
	id->ppaf.lun_len = 8;
	id->ppaf.ch_offset = 56;
	id->ppaf.ch_len = 8;
478

479 480
	sector_div(size, bs); /* convert size to pages */
	size >>= 8; /* concert size to pgs pr blk */
481
	grp = &id->grp;
482
	grp->mtype = 0;
483
	grp->fmtype = 0;
484 485
	grp->num_ch = 1;
	grp->num_pg = 256;
486
	blksize = size;
487
	size >>= 16;
488
	grp->num_lun = size + 1;
489
	sector_div(blksize, grp->num_lun);
490 491 492
	grp->num_blk = blksize;
	grp->num_pln = 1;

493 494 495 496 497 498 499 500 501 502 503 504 505 506
	grp->fpg_sz = bs;
	grp->csecs = bs;
	grp->trdt = 25000;
	grp->trdm = 25000;
	grp->tprt = 500000;
	grp->tprm = 500000;
	grp->tbet = 1500000;
	grp->tbem = 1500000;
	grp->mpos = 0x010101; /* single plane rwe */
	grp->cpar = hw_queue_depth;

	return 0;
}

507
static void *null_lnvm_create_dma_pool(struct nvm_dev *dev, char *name)
508 509 510
{
	mempool_t *virtmem_pool;

M
Matias Bjørling 已提交
511
	virtmem_pool = mempool_create_slab_pool(64, ppa_cache);
512 513 514 515 516 517 518 519 520 521 522 523 524
	if (!virtmem_pool) {
		pr_err("null_blk: Unable to create virtual memory pool\n");
		return NULL;
	}

	return virtmem_pool;
}

static void null_lnvm_destroy_dma_pool(void *pool)
{
	mempool_destroy(pool);
}

525
static void *null_lnvm_dev_dma_alloc(struct nvm_dev *dev, void *pool,
526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548
				gfp_t mem_flags, dma_addr_t *dma_handler)
{
	return mempool_alloc(pool, mem_flags);
}

static void null_lnvm_dev_dma_free(void *pool, void *entry,
							dma_addr_t dma_handler)
{
	mempool_free(entry, pool);
}

static struct nvm_dev_ops null_lnvm_dev_ops = {
	.identity		= null_lnvm_id,
	.submit_io		= null_lnvm_submit_io,

	.create_dma_pool	= null_lnvm_create_dma_pool,
	.destroy_dma_pool	= null_lnvm_destroy_dma_pool,
	.dev_dma_alloc		= null_lnvm_dev_dma_alloc,
	.dev_dma_free		= null_lnvm_dev_dma_free,

	/* Simulate nvme protocol restriction */
	.max_phys_sect		= 64,
};
549 550 551

static int null_nvm_register(struct nullb *nullb)
{
552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569
	struct nvm_dev *dev;
	int rv;

	dev = nvm_alloc_dev(0);
	if (!dev)
		return -ENOMEM;

	dev->q = nullb->q;
	memcpy(dev->name, nullb->disk_name, DISK_NAME_LEN);
	dev->ops = &null_lnvm_dev_ops;

	rv = nvm_register(dev);
	if (rv) {
		kfree(dev);
		return rv;
	}
	nullb->ndev = dev;
	return 0;
570 571 572 573
}

static void null_nvm_unregister(struct nullb *nullb)
{
574
	nvm_unregister(nullb->ndev);
575
}
576
#else
577 578
static int null_nvm_register(struct nullb *nullb)
{
579
	pr_err("null_blk: CONFIG_NVM needs to be enabled for LightNVM\n");
580 581 582
	return -EINVAL;
}
static void null_nvm_unregister(struct nullb *nullb) {}
583 584
#endif /* CONFIG_NVM */

585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601
static void null_del_dev(struct nullb *nullb)
{
	list_del_init(&nullb->list);

	if (use_lightnvm)
		null_nvm_unregister(nullb);
	else
		del_gendisk(nullb->disk);
	blk_cleanup_queue(nullb->q);
	if (queue_mode == NULL_Q_MQ)
		blk_mq_free_tag_set(&nullb->tag_set);
	if (!use_lightnvm)
		put_disk(nullb->disk);
	cleanup_queues(nullb);
	kfree(nullb);
}

602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623
static int null_open(struct block_device *bdev, fmode_t mode)
{
	return 0;
}

static void null_release(struct gendisk *disk, fmode_t mode)
{
}

static const struct block_device_operations null_fops = {
	.owner =	THIS_MODULE,
	.open =		null_open,
	.release =	null_release,
};

static int setup_commands(struct nullb_queue *nq)
{
	struct nullb_cmd *cmd;
	int i, tag_size;

	nq->cmds = kzalloc(nq->queue_depth * sizeof(*cmd), GFP_KERNEL);
	if (!nq->cmds)
624
		return -ENOMEM;
625 626 627 628 629

	tag_size = ALIGN(nq->queue_depth, BITS_PER_LONG) / BITS_PER_LONG;
	nq->tag_map = kzalloc(tag_size * sizeof(unsigned long), GFP_KERNEL);
	if (!nq->tag_map) {
		kfree(nq->cmds);
630
		return -ENOMEM;
631 632 633 634 635 636 637 638 639 640 641 642 643 644
	}

	for (i = 0; i < nq->queue_depth; i++) {
		cmd = &nq->cmds[i];
		INIT_LIST_HEAD(&cmd->list);
		cmd->ll_list.next = NULL;
		cmd->tag = -1U;
	}

	return 0;
}

static int setup_queues(struct nullb *nullb)
{
645 646
	nullb->queues = kzalloc(submit_queues * sizeof(struct nullb_queue),
								GFP_KERNEL);
647
	if (!nullb->queues)
648
		return -ENOMEM;
649 650 651 652

	nullb->nr_queues = 0;
	nullb->queue_depth = hw_queue_depth;

653 654 655 656 657 658 659
	return 0;
}

static int init_driver_queues(struct nullb *nullb)
{
	struct nullb_queue *nq;
	int i, ret = 0;
660 661 662

	for (i = 0; i < submit_queues; i++) {
		nq = &nullb->queues[i];
663 664 665 666 667

		null_init_queue(nullb, nq);

		ret = setup_commands(nq);
		if (ret)
668
			return ret;
669 670
		nullb->nr_queues++;
	}
671
	return 0;
672 673
}

674
static int null_gendisk_register(struct nullb *nullb)
675 676 677
{
	struct gendisk *disk;
	sector_t size;
678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699

	disk = nullb->disk = alloc_disk_node(1, home_node);
	if (!disk)
		return -ENOMEM;
	size = gb * 1024 * 1024 * 1024ULL;
	set_capacity(disk, size >> 9);

	disk->flags |= GENHD_FL_EXT_DEVT | GENHD_FL_SUPPRESS_PARTITION_INFO;
	disk->major		= null_major;
	disk->first_minor	= nullb->index;
	disk->fops		= &null_fops;
	disk->private_data	= nullb;
	disk->queue		= nullb->q;
	strncpy(disk->disk_name, nullb->disk_name, DISK_NAME_LEN);

	add_disk(disk);
	return 0;
}

static int null_add_dev(void)
{
	struct nullb *nullb;
700
	int rv;
701 702

	nullb = kzalloc_node(sizeof(*nullb), GFP_KERNEL, home_node);
703 704
	if (!nullb) {
		rv = -ENOMEM;
705
		goto out;
706
	}
707 708 709

	spin_lock_init(&nullb->lock);

710 711 712
	if (queue_mode == NULL_Q_MQ && use_per_node_hctx)
		submit_queues = nr_online_nodes;

713 714
	rv = setup_queues(nullb);
	if (rv)
715
		goto out_free_nullb;
716 717

	if (queue_mode == NULL_Q_MQ) {
718
		nullb->tag_set.ops = &null_mq_ops;
719 720 721 722 723 724 725
		nullb->tag_set.nr_hw_queues = submit_queues;
		nullb->tag_set.queue_depth = hw_queue_depth;
		nullb->tag_set.numa_node = home_node;
		nullb->tag_set.cmd_size	= sizeof(struct nullb_cmd);
		nullb->tag_set.flags = BLK_MQ_F_SHOULD_MERGE;
		nullb->tag_set.driver_data = nullb;

J
Jens Axboe 已提交
726 727 728
		if (blocking)
			nullb->tag_set.flags |= BLK_MQ_F_BLOCKING;

729 730
		rv = blk_mq_alloc_tag_set(&nullb->tag_set);
		if (rv)
731 732 733
			goto out_cleanup_queues;

		nullb->q = blk_mq_init_queue(&nullb->tag_set);
734
		if (IS_ERR(nullb->q)) {
735
			rv = -ENOMEM;
736
			goto out_cleanup_tags;
737
		}
738 739
	} else if (queue_mode == NULL_Q_BIO) {
		nullb->q = blk_alloc_queue_node(GFP_KERNEL, home_node);
740 741
		if (!nullb->q) {
			rv = -ENOMEM;
742
			goto out_cleanup_queues;
743
		}
744
		blk_queue_make_request(nullb->q, null_queue_bio);
745 746 747
		rv = init_driver_queues(nullb);
		if (rv)
			goto out_cleanup_blk_queue;
748 749
	} else {
		nullb->q = blk_init_queue_node(null_request_fn, &nullb->lock, home_node);
750 751
		if (!nullb->q) {
			rv = -ENOMEM;
752
			goto out_cleanup_queues;
753
		}
754
		blk_queue_prep_rq(nullb->q, null_rq_prep_fn);
755
		blk_queue_softirq_done(nullb->q, null_softirq_done_fn);
756 757 758
		rv = init_driver_queues(nullb);
		if (rv)
			goto out_cleanup_blk_queue;
759 760 761 762
	}

	nullb->q->queuedata = nullb;
	queue_flag_set_unlocked(QUEUE_FLAG_NONROT, nullb->q);
763
	queue_flag_clear_unlocked(QUEUE_FLAG_ADD_RANDOM, nullb->q);
764 765 766 767 768 769 770 771

	mutex_lock(&lock);
	nullb->index = nullb_indexes++;
	mutex_unlock(&lock);

	blk_queue_logical_block_size(nullb->q, bs);
	blk_queue_physical_block_size(nullb->q, bs);

772 773
	sprintf(nullb->disk_name, "nullb%d", nullb->index);

774 775 776 777
	if (use_lightnvm)
		rv = null_nvm_register(nullb);
	else
		rv = null_gendisk_register(nullb);
778

779 780
	if (rv)
		goto out_cleanup_blk_queue;
781 782 783 784

	mutex_lock(&lock);
	list_add_tail(&nullb->list, &nullb_list);
	mutex_unlock(&lock);
785

786
	return 0;
787 788 789 790 791 792 793 794 795 796
out_cleanup_blk_queue:
	blk_cleanup_queue(nullb->q);
out_cleanup_tags:
	if (queue_mode == NULL_Q_MQ)
		blk_mq_free_tag_set(&nullb->tag_set);
out_cleanup_queues:
	cleanup_queues(nullb);
out_free_nullb:
	kfree(nullb);
out:
797
	return rv;
798 799 800 801
}

static int __init null_init(void)
{
802
	int ret = 0;
803
	unsigned int i;
804
	struct nullb *nullb;
805

806 807 808 809 810
	if (bs > PAGE_SIZE) {
		pr_warn("null_blk: invalid block size\n");
		pr_warn("null_blk: defaults block size to %lu\n", PAGE_SIZE);
		bs = PAGE_SIZE;
	}
811

M
Matias Bjørling 已提交
812 813 814 815 816 817
	if (use_lightnvm && bs != 4096) {
		pr_warn("null_blk: LightNVM only supports 4k block size\n");
		pr_warn("null_blk: defaults block size to 4k\n");
		bs = 4096;
	}

818 819 820 821 822 823
	if (use_lightnvm && queue_mode != NULL_Q_MQ) {
		pr_warn("null_blk: LightNVM only supported for blk-mq\n");
		pr_warn("null_blk: defaults queue mode to blk-mq\n");
		queue_mode = NULL_Q_MQ;
	}

824
	if (queue_mode == NULL_Q_MQ && use_per_node_hctx) {
825
		if (submit_queues < nr_online_nodes) {
826 827
			pr_warn("null_blk: submit_queues param is set to %u.",
							nr_online_nodes);
828 829
			submit_queues = nr_online_nodes;
		}
830
	} else if (submit_queues > nr_cpu_ids)
831 832 833 834 835 836 837 838 839 840
		submit_queues = nr_cpu_ids;
	else if (!submit_queues)
		submit_queues = 1;

	mutex_init(&lock);

	null_major = register_blkdev(0, "nullb");
	if (null_major < 0)
		return null_major;

M
Matias Bjørling 已提交
841 842 843 844 845
	if (use_lightnvm) {
		ppa_cache = kmem_cache_create("ppa_cache", 64 * sizeof(u64),
								0, 0, NULL);
		if (!ppa_cache) {
			pr_err("null_blk: unable to create ppa cache\n");
846 847
			ret = -ENOMEM;
			goto err_ppa;
M
Matias Bjørling 已提交
848 849 850
		}
	}

851
	for (i = 0; i < nr_devices; i++) {
852 853 854
		ret = null_add_dev();
		if (ret)
			goto err_dev;
855 856 857 858
	}

	pr_info("null: module loaded\n");
	return 0;
859 860 861 862 863 864

err_dev:
	while (!list_empty(&nullb_list)) {
		nullb = list_entry(nullb_list.next, struct nullb, list);
		null_del_dev(nullb);
	}
M
Matias Bjørling 已提交
865
	kmem_cache_destroy(ppa_cache);
866 867 868
err_ppa:
	unregister_blkdev(null_major, "nullb");
	return ret;
869 870 871 872 873 874 875 876 877 878 879 880 881 882
}

static void __exit null_exit(void)
{
	struct nullb *nullb;

	unregister_blkdev(null_major, "nullb");

	mutex_lock(&lock);
	while (!list_empty(&nullb_list)) {
		nullb = list_entry(nullb_list.next, struct nullb, list);
		null_del_dev(nullb);
	}
	mutex_unlock(&lock);
M
Matias Bjørling 已提交
883 884

	kmem_cache_destroy(ppa_cache);
885 886 887 888 889 890 891
}

module_init(null_init);
module_exit(null_exit);

MODULE_AUTHOR("Jens Axboe <jaxboe@fusionio.com>");
MODULE_LICENSE("GPL");