null_blk.c 22.3 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
};

struct nullb_queue {
	unsigned long *tag_map;
	wait_queue_head_t wait;
	unsigned int queue_depth;
S
Shaohua Li 已提交
28
	struct nullb_device *dev;
29 30 31 32

	struct nullb_cmd *cmds;
};

S
Shaohua Li 已提交
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
struct nullb_device {
	struct nullb *nullb;

	unsigned long size; /* device size in MB */
	unsigned long completion_nsec; /* time in ns to complete a request */
	unsigned int submit_queues; /* number of submission queues */
	unsigned int home_node; /* home node for the device */
	unsigned int queue_mode; /* block interface */
	unsigned int blocksize; /* block size */
	unsigned int irqmode; /* IRQ completion handler */
	unsigned int hw_queue_depth; /* queue depth */
	bool use_lightnvm; /* register as a LightNVM device */
	bool blocking; /* blocking blk-mq device */
	bool use_per_node_hctx; /* use per-node allocation for hardware context */
};

49
struct nullb {
S
Shaohua Li 已提交
50
	struct nullb_device *dev;
51 52 53 54
	struct list_head list;
	unsigned int index;
	struct request_queue *q;
	struct gendisk *disk;
55
	struct nvm_dev *ndev;
56 57
	struct blk_mq_tag_set *tag_set;
	struct blk_mq_tag_set __tag_set;
58 59 60 61 62 63
	struct hrtimer timer;
	unsigned int queue_depth;
	spinlock_t lock;

	struct nullb_queue *queues;
	unsigned int nr_queues;
64
	char disk_name[DISK_NAME_LEN];
65 66 67 68 69 70
};

static LIST_HEAD(nullb_list);
static struct mutex lock;
static int null_major;
static int nullb_indexes;
M
Matias Bjørling 已提交
71
static struct kmem_cache *ppa_cache;
72
static struct blk_mq_tag_set tag_set;
73 74 75 76 77

enum {
	NULL_IRQ_NONE		= 0,
	NULL_IRQ_SOFTIRQ	= 1,
	NULL_IRQ_TIMER		= 2,
78
};
79

80
enum {
81 82 83 84 85
	NULL_Q_BIO		= 0,
	NULL_Q_RQ		= 1,
	NULL_Q_MQ		= 2,
};

S
Shaohua Li 已提交
86 87
static int g_submit_queues = 1;
module_param_named(submit_queues, g_submit_queues, int, S_IRUGO);
88 89
MODULE_PARM_DESC(submit_queues, "Number of submission queues");

S
Shaohua Li 已提交
90 91
static int g_home_node = NUMA_NO_NODE;
module_param_named(home_node, g_home_node, int, S_IRUGO);
92 93
MODULE_PARM_DESC(home_node, "Home node for the device");

S
Shaohua Li 已提交
94
static int g_queue_mode = NULL_Q_MQ;
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112

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)
{
S
Shaohua Li 已提交
113
	return null_param_store_val(str, &g_queue_mode, NULL_Q_BIO, NULL_Q_MQ);
114 115
}

116
static const struct kernel_param_ops null_queue_mode_param_ops = {
117 118 119 120
	.set	= null_set_queue_mode,
	.get	= param_get_int,
};

S
Shaohua Li 已提交
121
device_param_cb(queue_mode, &null_queue_mode_param_ops, &g_queue_mode, S_IRUGO);
122
MODULE_PARM_DESC(queue_mode, "Block interface to use (0=bio,1=rq,2=multiqueue)");
123

S
Shaohua Li 已提交
124 125
static int g_gb = 250;
module_param_named(gb, g_gb, int, S_IRUGO);
126 127
MODULE_PARM_DESC(gb, "Size in GB");

S
Shaohua Li 已提交
128 129
static int g_bs = 512;
module_param_named(bs, g_bs, int, S_IRUGO);
130 131
MODULE_PARM_DESC(bs, "Block size (in bytes)");

132
static int nr_devices = 1;
133 134 135
module_param(nr_devices, int, S_IRUGO);
MODULE_PARM_DESC(nr_devices, "Number of devices to register");

S
Shaohua Li 已提交
136 137
static bool g_use_lightnvm;
module_param_named(use_lightnvm, g_use_lightnvm, bool, S_IRUGO);
138 139
MODULE_PARM_DESC(use_lightnvm, "Register as a LightNVM device");

S
Shaohua Li 已提交
140 141
static bool g_blocking;
module_param_named(blocking, g_blocking, bool, S_IRUGO);
J
Jens Axboe 已提交
142 143
MODULE_PARM_DESC(blocking, "Register as a blocking blk-mq driver device");

144 145 146 147
static bool shared_tags;
module_param(shared_tags, bool, S_IRUGO);
MODULE_PARM_DESC(shared_tags, "Share tag set between devices for blk-mq");

S
Shaohua Li 已提交
148
static int g_irqmode = NULL_IRQ_SOFTIRQ;
149 150 151

static int null_set_irqmode(const char *str, const struct kernel_param *kp)
{
S
Shaohua Li 已提交
152
	return null_param_store_val(str, &g_irqmode, NULL_IRQ_NONE,
153 154 155
					NULL_IRQ_TIMER);
}

156
static const struct kernel_param_ops null_irqmode_param_ops = {
157 158 159 160
	.set	= null_set_irqmode,
	.get	= param_get_int,
};

S
Shaohua Li 已提交
161
device_param_cb(irqmode, &null_irqmode_param_ops, &g_irqmode, S_IRUGO);
162 163
MODULE_PARM_DESC(irqmode, "IRQ completion handler. 0-none, 1-softirq, 2-timer");

S
Shaohua Li 已提交
164 165
static unsigned long g_completion_nsec = 10000;
module_param_named(completion_nsec, g_completion_nsec, ulong, S_IRUGO);
166 167
MODULE_PARM_DESC(completion_nsec, "Time in ns to complete a request in hardware. Default: 10,000ns");

S
Shaohua Li 已提交
168 169
static int g_hw_queue_depth = 64;
module_param_named(hw_queue_depth, g_hw_queue_depth, int, S_IRUGO);
170 171
MODULE_PARM_DESC(hw_queue_depth, "Queue depth for each hardware queue. Default: 64");

S
Shaohua Li 已提交
172 173
static bool g_use_per_node_hctx;
module_param_named(use_per_node_hctx, g_use_per_node_hctx, bool, S_IRUGO);
174
MODULE_PARM_DESC(use_per_node_hctx, "Use per-node allocation for hardware context queues. Default: false");
175

S
Shaohua Li 已提交
176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201
static struct nullb_device *null_alloc_dev(void)
{
	struct nullb_device *dev;

	dev = kzalloc(sizeof(*dev), GFP_KERNEL);
	if (!dev)
		return NULL;
	dev->size = g_gb * 1024;
	dev->completion_nsec = g_completion_nsec;
	dev->submit_queues = g_submit_queues;
	dev->home_node = g_home_node;
	dev->queue_mode = g_queue_mode;
	dev->blocksize = g_bs;
	dev->irqmode = g_irqmode;
	dev->hw_queue_depth = g_hw_queue_depth;
	dev->use_lightnvm = g_use_lightnvm;
	dev->blocking = g_blocking;
	dev->use_per_node_hctx = g_use_per_node_hctx;
	return dev;
}

static void null_free_dev(struct nullb_device *dev)
{
	kfree(dev);
}

202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227
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);
}

228 229
static enum hrtimer_restart null_cmd_timer_expired(struct hrtimer *timer);

230 231 232 233 234 235 236 237 238 239
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;
S
Shaohua Li 已提交
240
		if (nq->dev->irqmode == NULL_IRQ_TIMER) {
241 242 243 244
			hrtimer_init(&cmd->timer, CLOCK_MONOTONIC,
				     HRTIMER_MODE_REL);
			cmd->timer.function = null_cmd_timer_expired;
		}
245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274
		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)
{
275
	struct request_queue *q = NULL;
S
Shaohua Li 已提交
276
	int queue_mode = cmd->nq->dev->queue_mode;
277

M
Mike Krinkin 已提交
278 279 280
	if (cmd->rq)
		q = cmd->rq->q;

281 282
	switch (queue_mode)  {
	case NULL_Q_MQ:
283
		blk_mq_end_request(cmd->rq, BLK_STS_OK);
284 285 286
		return;
	case NULL_Q_RQ:
		INIT_LIST_HEAD(&cmd->rq->queuelist);
287
		blk_end_request_all(cmd->rq, BLK_STS_OK);
288 289
		break;
	case NULL_Q_BIO:
290
		bio_endio(cmd->bio);
291
		break;
292
	}
293

294 295
	free_cmd(cmd);

296
	/* Restart queue if needed, as we are freeing a tag */
297
	if (queue_mode == NULL_Q_RQ && blk_queue_stopped(q)) {
298 299 300
		unsigned long flags;

		spin_lock_irqsave(q->queue_lock, flags);
301
		blk_start_queue_async(q);
302
		spin_unlock_irqrestore(q->queue_lock, flags);
303
	}
304 305 306 307 308
}

static enum hrtimer_restart null_cmd_timer_expired(struct hrtimer *timer)
{
	end_cmd(container_of(timer, struct nullb_cmd, timer));
309 310 311 312 313 314

	return HRTIMER_NORESTART;
}

static void null_cmd_end_timer(struct nullb_cmd *cmd)
{
S
Shaohua Li 已提交
315
	ktime_t kt = cmd->nq->dev->completion_nsec;
316

317
	hrtimer_start(&cmd->timer, kt, HRTIMER_MODE_REL);
318 319 320 321
}

static void null_softirq_done_fn(struct request *rq)
{
S
Shaohua Li 已提交
322 323 324
	struct nullb *nullb = rq->q->queuedata;

	if (nullb->dev->queue_mode == NULL_Q_MQ)
325 326 327
		end_cmd(blk_mq_rq_to_pdu(rq));
	else
		end_cmd(rq->special);
328 329 330 331 332
}

static inline void null_handle_cmd(struct nullb_cmd *cmd)
{
	/* Complete IO by inline, softirq or timer */
S
Shaohua Li 已提交
333
	switch (cmd->nq->dev->irqmode) {
334
	case NULL_IRQ_SOFTIRQ:
S
Shaohua Li 已提交
335
		switch (cmd->nq->dev->queue_mode)  {
336
		case NULL_Q_MQ:
337
			blk_mq_complete_request(cmd->rq);
338 339 340 341 342 343 344 345 346 347 348 349 350
			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:
351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368
		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];
}

369
static blk_qc_t null_queue_bio(struct request_queue *q, struct bio *bio)
370 371 372 373 374 375 376 377 378
{
	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);
379
	return BLK_QC_T_NONE;
380 381 382 383 384 385 386 387 388 389 390 391 392 393
}

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;
	}
394
	blk_stop_queue(q);
395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411

	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);
	}
}

412
static blk_status_t null_queue_rq(struct blk_mq_hw_ctx *hctx,
413
			 const struct blk_mq_queue_data *bd)
414
{
415
	struct nullb_cmd *cmd = blk_mq_rq_to_pdu(bd->rq);
S
Shaohua Li 已提交
416
	struct nullb_queue *nq = hctx->driver_data;
417

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

S
Shaohua Li 已提交
420
	if (nq->dev->irqmode == NULL_IRQ_TIMER) {
421 422 423
		hrtimer_init(&cmd->timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
		cmd->timer.function = null_cmd_timer_expired;
	}
424
	cmd->rq = bd->rq;
S
Shaohua Li 已提交
425
	cmd->nq = nq;
426

427
	blk_mq_start_request(bd->rq);
428

429
	null_handle_cmd(cmd);
430
	return BLK_STS_OK;
431 432
}

433
static const struct blk_mq_ops null_mq_ops = {
434
	.queue_rq       = null_queue_rq,
435
	.complete	= null_softirq_done_fn,
436 437
};

438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453
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);
}

454 455
#ifdef CONFIG_NVM

456
static void null_lnvm_end_io(struct request *rq, blk_status_t status)
457 458 459
{
	struct nvm_rq *rqd = rq->end_io_data;

460 461
	/* XXX: lighnvm core seems to expect NVM_RSP_* values here.. */
	rqd->error = status ? -EIO : 0;
462
	nvm_end_io(rqd);
463 464 465 466

	blk_put_request(rq);
}

467
static int null_lnvm_submit_io(struct nvm_dev *dev, struct nvm_rq *rqd)
468
{
469
	struct request_queue *q = dev->q;
470 471 472
	struct request *rq;
	struct bio *bio = rqd->bio;

473 474
	rq = blk_mq_alloc_request(q,
		op_is_write(bio_op(bio)) ? REQ_OP_DRV_OUT : REQ_OP_DRV_IN, 0);
475 476 477
	if (IS_ERR(rq))
		return -ENOMEM;

478
	blk_init_request_from_bio(rq, bio);
479 480 481 482 483 484 485 486

	rq->end_io_data = rqd;

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

	return 0;
}

487
static int null_lnvm_id(struct nvm_dev *dev, struct nvm_id *id)
488
{
S
Shaohua Li 已提交
489 490
	struct nullb *nullb = dev->q->queuedata;
	sector_t size = (sector_t)nullb->dev->size * 1024 * 1024ULL;
491
	sector_t blksize;
492 493 494 495
	struct nvm_id_group *grp;

	id->ver_id = 0x1;
	id->vmnt = 0;
496
	id->cap = 0x2;
497
	id->dom = 0x1;
498 499 500 501 502 503 504 505 506 507 508 509 510

	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;
511

S
Shaohua Li 已提交
512
	sector_div(size, nullb->dev->blocksize); /* convert size to pages */
513
	size >>= 8; /* concert size to pgs pr blk */
514
	grp = &id->grp;
515
	grp->mtype = 0;
516
	grp->fmtype = 0;
517 518
	grp->num_ch = 1;
	grp->num_pg = 256;
519
	blksize = size;
520
	size >>= 16;
521
	grp->num_lun = size + 1;
522
	sector_div(blksize, grp->num_lun);
523 524 525
	grp->num_blk = blksize;
	grp->num_pln = 1;

S
Shaohua Li 已提交
526 527
	grp->fpg_sz = nullb->dev->blocksize;
	grp->csecs = nullb->dev->blocksize;
528 529 530 531 532 533 534
	grp->trdt = 25000;
	grp->trdm = 25000;
	grp->tprt = 500000;
	grp->tprm = 500000;
	grp->tbet = 1500000;
	grp->tbem = 1500000;
	grp->mpos = 0x010101; /* single plane rwe */
S
Shaohua Li 已提交
535
	grp->cpar = nullb->dev->hw_queue_depth;
536 537 538 539

	return 0;
}

540
static void *null_lnvm_create_dma_pool(struct nvm_dev *dev, char *name)
541 542 543
{
	mempool_t *virtmem_pool;

M
Matias Bjørling 已提交
544
	virtmem_pool = mempool_create_slab_pool(64, ppa_cache);
545 546 547 548 549 550 551 552 553 554 555 556 557
	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);
}

558
static void *null_lnvm_dev_dma_alloc(struct nvm_dev *dev, void *pool,
559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581
				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,
};
582 583 584

static int null_nvm_register(struct nullb *nullb)
{
585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602
	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;
603 604 605 606
}

static void null_nvm_unregister(struct nullb *nullb)
{
607
	nvm_unregister(nullb->ndev);
608
}
609
#else
610 611
static int null_nvm_register(struct nullb *nullb)
{
612
	pr_err("null_blk: CONFIG_NVM needs to be enabled for LightNVM\n");
613 614 615
	return -EINVAL;
}
static void null_nvm_unregister(struct nullb *nullb) {}
616 617
#endif /* CONFIG_NVM */

618 619
static void null_del_dev(struct nullb *nullb)
{
S
Shaohua Li 已提交
620 621
	struct nullb_device *dev = nullb->dev;

622 623
	list_del_init(&nullb->list);

S
Shaohua Li 已提交
624
	if (dev->use_lightnvm)
625 626 627 628
		null_nvm_unregister(nullb);
	else
		del_gendisk(nullb->disk);
	blk_cleanup_queue(nullb->q);
S
Shaohua Li 已提交
629 630
	if (dev->queue_mode == NULL_Q_MQ &&
	    nullb->tag_set == &nullb->__tag_set)
631
		blk_mq_free_tag_set(nullb->tag_set);
S
Shaohua Li 已提交
632
	if (!dev->use_lightnvm)
633 634 635
		put_disk(nullb->disk);
	cleanup_queues(nullb);
	kfree(nullb);
S
Shaohua Li 已提交
636
	dev->nullb = NULL;
637 638
}

639 640 641 642 643 644 645 646 647 648 649 650 651 652 653
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,
};

654 655 656 657 658 659 660
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;
S
Shaohua Li 已提交
661
	nq->dev = nullb->dev;
662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680
}

static void null_init_queues(struct nullb *nullb)
{
	struct request_queue *q = nullb->q;
	struct blk_mq_hw_ctx *hctx;
	struct nullb_queue *nq;
	int i;

	queue_for_each_hw_ctx(q, hctx, i) {
		if (!hctx->nr_ctx || !hctx->tags)
			continue;
		nq = &nullb->queues[i];
		hctx->driver_data = nq;
		null_init_queue(nullb, nq);
		nullb->nr_queues++;
	}
}

681 682 683 684 685 686 687
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)
688
		return -ENOMEM;
689 690 691 692 693

	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);
694
		return -ENOMEM;
695 696 697 698 699 700 701 702 703 704 705 706 707 708
	}

	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)
{
S
Shaohua Li 已提交
709 710
	nullb->queues = kzalloc(nullb->dev->submit_queues *
		sizeof(struct nullb_queue), GFP_KERNEL);
711
	if (!nullb->queues)
712
		return -ENOMEM;
713 714

	nullb->nr_queues = 0;
S
Shaohua Li 已提交
715
	nullb->queue_depth = nullb->dev->hw_queue_depth;
716

717 718 719 720 721 722 723
	return 0;
}

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

S
Shaohua Li 已提交
725
	for (i = 0; i < nullb->dev->submit_queues; i++) {
726
		nq = &nullb->queues[i];
727 728 729 730 731

		null_init_queue(nullb, nq);

		ret = setup_commands(nq);
		if (ret)
732
			return ret;
733 734
		nullb->nr_queues++;
	}
735
	return 0;
736 737
}

738
static int null_gendisk_register(struct nullb *nullb)
739 740 741
{
	struct gendisk *disk;
	sector_t size;
742

S
Shaohua Li 已提交
743
	disk = nullb->disk = alloc_disk_node(1, nullb->dev->home_node);
744 745
	if (!disk)
		return -ENOMEM;
S
Shaohua Li 已提交
746
	size = (sector_t)nullb->dev->size * 1024 * 1024ULL;
747 748 749 750 751 752 753 754 755 756 757 758 759 760
	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;
}

S
Shaohua Li 已提交
761
static int null_init_tag_set(struct nullb *nullb, struct blk_mq_tag_set *set)
762 763
{
	set->ops = &null_mq_ops;
S
Shaohua Li 已提交
764 765 766 767 768
	set->nr_hw_queues = nullb ? nullb->dev->submit_queues :
						g_submit_queues;
	set->queue_depth = nullb ? nullb->dev->hw_queue_depth :
						g_hw_queue_depth;
	set->numa_node = nullb ? nullb->dev->home_node : g_home_node;
769 770 771 772
	set->cmd_size	= sizeof(struct nullb_cmd);
	set->flags = BLK_MQ_F_SHOULD_MERGE;
	set->driver_data = NULL;

S
Shaohua Li 已提交
773
	if (nullb->dev->blocking)
774 775 776 777 778
		set->flags |= BLK_MQ_F_BLOCKING;

	return blk_mq_alloc_tag_set(set);
}

S
Shaohua Li 已提交
779
static int null_add_dev(struct nullb_device *dev)
780 781
{
	struct nullb *nullb;
782
	int rv;
783

S
Shaohua Li 已提交
784
	nullb = kzalloc_node(sizeof(*nullb), GFP_KERNEL, dev->home_node);
785 786
	if (!nullb) {
		rv = -ENOMEM;
787
		goto out;
788
	}
S
Shaohua Li 已提交
789 790
	nullb->dev = dev;
	dev->nullb = nullb;
791 792 793

	spin_lock_init(&nullb->lock);

794 795
	rv = setup_queues(nullb);
	if (rv)
796
		goto out_free_nullb;
797

S
Shaohua Li 已提交
798
	if (dev->queue_mode == NULL_Q_MQ) {
799 800 801 802 803
		if (shared_tags) {
			nullb->tag_set = &tag_set;
			rv = 0;
		} else {
			nullb->tag_set = &nullb->__tag_set;
S
Shaohua Li 已提交
804
			rv = null_init_tag_set(nullb, nullb->tag_set);
805 806
		}

807
		if (rv)
808 809
			goto out_cleanup_queues;

810
		nullb->q = blk_mq_init_queue(nullb->tag_set);
811
		if (IS_ERR(nullb->q)) {
812
			rv = -ENOMEM;
813
			goto out_cleanup_tags;
814
		}
815
		null_init_queues(nullb);
S
Shaohua Li 已提交
816 817
	} else if (dev->queue_mode == NULL_Q_BIO) {
		nullb->q = blk_alloc_queue_node(GFP_KERNEL, dev->home_node);
818 819
		if (!nullb->q) {
			rv = -ENOMEM;
820
			goto out_cleanup_queues;
821
		}
822
		blk_queue_make_request(nullb->q, null_queue_bio);
823 824 825
		rv = init_driver_queues(nullb);
		if (rv)
			goto out_cleanup_blk_queue;
826
	} else {
S
Shaohua Li 已提交
827 828
		nullb->q = blk_init_queue_node(null_request_fn, &nullb->lock,
						dev->home_node);
829 830
		if (!nullb->q) {
			rv = -ENOMEM;
831
			goto out_cleanup_queues;
832
		}
833
		blk_queue_prep_rq(nullb->q, null_rq_prep_fn);
834
		blk_queue_softirq_done(nullb->q, null_softirq_done_fn);
835 836 837
		rv = init_driver_queues(nullb);
		if (rv)
			goto out_cleanup_blk_queue;
838 839 840 841
	}

	nullb->q->queuedata = nullb;
	queue_flag_set_unlocked(QUEUE_FLAG_NONROT, nullb->q);
842
	queue_flag_clear_unlocked(QUEUE_FLAG_ADD_RANDOM, nullb->q);
843 844 845 846 847

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

S
Shaohua Li 已提交
848 849
	blk_queue_logical_block_size(nullb->q, dev->blocksize);
	blk_queue_physical_block_size(nullb->q, dev->blocksize);
850

851 852
	sprintf(nullb->disk_name, "nullb%d", nullb->index);

S
Shaohua Li 已提交
853
	if (dev->use_lightnvm)
854 855 856
		rv = null_nvm_register(nullb);
	else
		rv = null_gendisk_register(nullb);
857

858 859
	if (rv)
		goto out_cleanup_blk_queue;
860 861 862 863

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

865
	return 0;
866 867 868
out_cleanup_blk_queue:
	blk_cleanup_queue(nullb->q);
out_cleanup_tags:
S
Shaohua Li 已提交
869
	if (dev->queue_mode == NULL_Q_MQ && nullb->tag_set == &nullb->__tag_set)
870
		blk_mq_free_tag_set(nullb->tag_set);
871 872 873 874 875
out_cleanup_queues:
	cleanup_queues(nullb);
out_free_nullb:
	kfree(nullb);
out:
S
Shaohua Li 已提交
876
	null_free_dev(dev);
877
	return rv;
878 879 880 881
}

static int __init null_init(void)
{
882
	int ret = 0;
883
	unsigned int i;
884
	struct nullb *nullb;
S
Shaohua Li 已提交
885
	struct nullb_device *dev;
886

S
Shaohua Li 已提交
887
	if (g_bs > PAGE_SIZE) {
888 889
		pr_warn("null_blk: invalid block size\n");
		pr_warn("null_blk: defaults block size to %lu\n", PAGE_SIZE);
S
Shaohua Li 已提交
890
		g_bs = PAGE_SIZE;
891
	}
892

S
Shaohua Li 已提交
893
	if (g_use_lightnvm && g_bs != 4096) {
M
Matias Bjørling 已提交
894 895
		pr_warn("null_blk: LightNVM only supports 4k block size\n");
		pr_warn("null_blk: defaults block size to 4k\n");
S
Shaohua Li 已提交
896
		g_bs = 4096;
M
Matias Bjørling 已提交
897 898
	}

S
Shaohua Li 已提交
899
	if (g_use_lightnvm && g_queue_mode != NULL_Q_MQ) {
900 901
		pr_warn("null_blk: LightNVM only supported for blk-mq\n");
		pr_warn("null_blk: defaults queue mode to blk-mq\n");
S
Shaohua Li 已提交
902
		g_queue_mode = NULL_Q_MQ;
903 904
	}

S
Shaohua Li 已提交
905 906
	if (g_queue_mode == NULL_Q_MQ && g_use_per_node_hctx) {
		if (g_submit_queues != nr_online_nodes) {
907
			pr_warn("null_blk: submit_queues param is set to %u.\n",
908
							nr_online_nodes);
S
Shaohua Li 已提交
909
			g_submit_queues = nr_online_nodes;
910
		}
S
Shaohua Li 已提交
911 912 913 914
	} else if (g_submit_queues > nr_cpu_ids)
		g_submit_queues = nr_cpu_ids;
	else if (g_submit_queues <= 0)
		g_submit_queues = 1;
915

S
Shaohua Li 已提交
916 917
	if (g_queue_mode == NULL_Q_MQ && shared_tags) {
		ret = null_init_tag_set(NULL, &tag_set);
918 919 920 921
		if (ret)
			return ret;
	}

922 923 924
	mutex_init(&lock);

	null_major = register_blkdev(0, "nullb");
925 926 927 928
	if (null_major < 0) {
		ret = null_major;
		goto err_tagset;
	}
929

S
Shaohua Li 已提交
930
	if (g_use_lightnvm) {
M
Matias Bjørling 已提交
931 932 933 934
		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");
935 936
			ret = -ENOMEM;
			goto err_ppa;
M
Matias Bjørling 已提交
937 938 939
		}
	}

940
	for (i = 0; i < nr_devices; i++) {
S
Shaohua Li 已提交
941 942 943 944 945 946
		dev = null_alloc_dev();
		if (!dev)
			goto err_dev;
		ret = null_add_dev(dev);
		if (ret) {
			null_free_dev(dev);
947
			goto err_dev;
S
Shaohua Li 已提交
948
		}
949 950 951 952
	}

	pr_info("null: module loaded\n");
	return 0;
953 954 955 956

err_dev:
	while (!list_empty(&nullb_list)) {
		nullb = list_entry(nullb_list.next, struct nullb, list);
S
Shaohua Li 已提交
957
		dev = nullb->dev;
958
		null_del_dev(nullb);
S
Shaohua Li 已提交
959
		null_free_dev(dev);
960
	}
M
Matias Bjørling 已提交
961
	kmem_cache_destroy(ppa_cache);
962 963
err_ppa:
	unregister_blkdev(null_major, "nullb");
964
err_tagset:
S
Shaohua Li 已提交
965
	if (g_queue_mode == NULL_Q_MQ && shared_tags)
966
		blk_mq_free_tag_set(&tag_set);
967
	return ret;
968 969 970 971 972 973 974 975 976 977
}

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

	unregister_blkdev(null_major, "nullb");

	mutex_lock(&lock);
	while (!list_empty(&nullb_list)) {
S
Shaohua Li 已提交
978 979
		struct nullb_device *dev;

980
		nullb = list_entry(nullb_list.next, struct nullb, list);
S
Shaohua Li 已提交
981
		dev = nullb->dev;
982
		null_del_dev(nullb);
S
Shaohua Li 已提交
983
		null_free_dev(dev);
984 985
	}
	mutex_unlock(&lock);
M
Matias Bjørling 已提交
986

S
Shaohua Li 已提交
987
	if (g_queue_mode == NULL_Q_MQ && shared_tags)
988 989
		blk_mq_free_tag_set(&tag_set);

M
Matias Bjørling 已提交
990
	kmem_cache_destroy(ppa_cache);
991 992 993 994 995 996 997
}

module_init(null_init);
module_exit(null_exit);

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