1 #include <linux/module.h>
3 #include <linux/moduleparam.h>
4 #include <linux/sched.h>
6 #include <linux/blkdev.h>
7 #include <linux/init.h>
8 #include <linux/slab.h>
9 #include <linux/blk-mq.h>
10 #include <linux/hrtimer.h>
11 #include <linux/lightnvm.h>
14 struct list_head list;
15 struct llist_node ll_list;
16 struct call_single_data csd;
20 struct nullb_queue *nq;
25 unsigned long *tag_map;
26 wait_queue_head_t wait;
27 unsigned int queue_depth;
29 struct nullb_cmd *cmds;
33 struct list_head list;
35 struct request_queue *q;
37 struct blk_mq_tag_set tag_set;
39 unsigned int queue_depth;
42 struct nullb_queue *queues;
43 unsigned int nr_queues;
44 char disk_name[DISK_NAME_LEN];
47 static LIST_HEAD(nullb_list);
48 static struct mutex lock;
49 static int null_major;
50 static int nullb_indexes;
51 static struct kmem_cache *ppa_cache;
65 static int submit_queues;
66 module_param(submit_queues, int, S_IRUGO);
67 MODULE_PARM_DESC(submit_queues, "Number of submission queues");
69 static int home_node = NUMA_NO_NODE;
70 module_param(home_node, int, S_IRUGO);
71 MODULE_PARM_DESC(home_node, "Home node for the device");
73 static int queue_mode = NULL_Q_MQ;
75 static int null_param_store_val(const char *str, int *val, int min, int max)
79 ret = kstrtoint(str, 10, &new_val);
83 if (new_val < min || new_val > max)
90 static int null_set_queue_mode(const char *str, const struct kernel_param *kp)
92 return null_param_store_val(str, &queue_mode, NULL_Q_BIO, NULL_Q_MQ);
95 static const struct kernel_param_ops null_queue_mode_param_ops = {
96 .set = null_set_queue_mode,
100 device_param_cb(queue_mode, &null_queue_mode_param_ops, &queue_mode, S_IRUGO);
101 MODULE_PARM_DESC(queue_mode, "Block interface to use (0=bio,1=rq,2=multiqueue)");
104 module_param(gb, int, S_IRUGO);
105 MODULE_PARM_DESC(gb, "Size in GB");
108 module_param(bs, int, S_IRUGO);
109 MODULE_PARM_DESC(bs, "Block size (in bytes)");
111 static int nr_devices = 2;
112 module_param(nr_devices, int, S_IRUGO);
113 MODULE_PARM_DESC(nr_devices, "Number of devices to register");
115 static bool use_lightnvm;
116 module_param(use_lightnvm, bool, S_IRUGO);
117 MODULE_PARM_DESC(use_lightnvm, "Register as a LightNVM device");
119 static int irqmode = NULL_IRQ_SOFTIRQ;
121 static int null_set_irqmode(const char *str, const struct kernel_param *kp)
123 return null_param_store_val(str, &irqmode, NULL_IRQ_NONE,
127 static const struct kernel_param_ops null_irqmode_param_ops = {
128 .set = null_set_irqmode,
129 .get = param_get_int,
132 device_param_cb(irqmode, &null_irqmode_param_ops, &irqmode, S_IRUGO);
133 MODULE_PARM_DESC(irqmode, "IRQ completion handler. 0-none, 1-softirq, 2-timer");
135 static unsigned long completion_nsec = 10000;
136 module_param(completion_nsec, ulong, S_IRUGO);
137 MODULE_PARM_DESC(completion_nsec, "Time in ns to complete a request in hardware. Default: 10,000ns");
139 static int hw_queue_depth = 64;
140 module_param(hw_queue_depth, int, S_IRUGO);
141 MODULE_PARM_DESC(hw_queue_depth, "Queue depth for each hardware queue. Default: 64");
143 static bool use_per_node_hctx = false;
144 module_param(use_per_node_hctx, bool, S_IRUGO);
145 MODULE_PARM_DESC(use_per_node_hctx, "Use per-node allocation for hardware context queues. Default: false");
147 static void put_tag(struct nullb_queue *nq, unsigned int tag)
149 clear_bit_unlock(tag, nq->tag_map);
151 if (waitqueue_active(&nq->wait))
155 static unsigned int get_tag(struct nullb_queue *nq)
160 tag = find_first_zero_bit(nq->tag_map, nq->queue_depth);
161 if (tag >= nq->queue_depth)
163 } while (test_and_set_bit_lock(tag, nq->tag_map));
168 static void free_cmd(struct nullb_cmd *cmd)
170 put_tag(cmd->nq, cmd->tag);
173 static enum hrtimer_restart null_cmd_timer_expired(struct hrtimer *timer);
175 static struct nullb_cmd *__alloc_cmd(struct nullb_queue *nq)
177 struct nullb_cmd *cmd;
182 cmd = &nq->cmds[tag];
185 if (irqmode == NULL_IRQ_TIMER) {
186 hrtimer_init(&cmd->timer, CLOCK_MONOTONIC,
188 cmd->timer.function = null_cmd_timer_expired;
196 static struct nullb_cmd *alloc_cmd(struct nullb_queue *nq, int can_wait)
198 struct nullb_cmd *cmd;
201 cmd = __alloc_cmd(nq);
202 if (cmd || !can_wait)
206 prepare_to_wait(&nq->wait, &wait, TASK_UNINTERRUPTIBLE);
207 cmd = __alloc_cmd(nq);
214 finish_wait(&nq->wait, &wait);
218 static void end_cmd(struct nullb_cmd *cmd)
220 struct request_queue *q = NULL;
225 switch (queue_mode) {
227 blk_mq_end_request(cmd->rq, 0);
230 INIT_LIST_HEAD(&cmd->rq->queuelist);
231 blk_end_request_all(cmd->rq, 0);
240 /* Restart queue if needed, as we are freeing a tag */
241 if (queue_mode == NULL_Q_RQ && blk_queue_stopped(q)) {
244 spin_lock_irqsave(q->queue_lock, flags);
245 blk_start_queue_async(q);
246 spin_unlock_irqrestore(q->queue_lock, flags);
250 static enum hrtimer_restart null_cmd_timer_expired(struct hrtimer *timer)
252 end_cmd(container_of(timer, struct nullb_cmd, timer));
254 return HRTIMER_NORESTART;
257 static void null_cmd_end_timer(struct nullb_cmd *cmd)
259 ktime_t kt = ktime_set(0, completion_nsec);
261 hrtimer_start(&cmd->timer, kt, HRTIMER_MODE_REL);
264 static void null_softirq_done_fn(struct request *rq)
266 if (queue_mode == NULL_Q_MQ)
267 end_cmd(blk_mq_rq_to_pdu(rq));
269 end_cmd(rq->special);
272 static inline void null_handle_cmd(struct nullb_cmd *cmd)
274 /* Complete IO by inline, softirq or timer */
276 case NULL_IRQ_SOFTIRQ:
277 switch (queue_mode) {
279 blk_mq_complete_request(cmd->rq, cmd->rq->errors);
282 blk_complete_request(cmd->rq);
286 * XXX: no proper submitting cpu information available.
296 null_cmd_end_timer(cmd);
301 static struct nullb_queue *nullb_to_queue(struct nullb *nullb)
305 if (nullb->nr_queues != 1)
306 index = raw_smp_processor_id() / ((nr_cpu_ids + nullb->nr_queues - 1) / nullb->nr_queues);
308 return &nullb->queues[index];
311 static blk_qc_t null_queue_bio(struct request_queue *q, struct bio *bio)
313 struct nullb *nullb = q->queuedata;
314 struct nullb_queue *nq = nullb_to_queue(nullb);
315 struct nullb_cmd *cmd;
317 cmd = alloc_cmd(nq, 1);
320 null_handle_cmd(cmd);
321 return BLK_QC_T_NONE;
324 static int null_rq_prep_fn(struct request_queue *q, struct request *req)
326 struct nullb *nullb = q->queuedata;
327 struct nullb_queue *nq = nullb_to_queue(nullb);
328 struct nullb_cmd *cmd;
330 cmd = alloc_cmd(nq, 0);
338 return BLKPREP_DEFER;
341 static void null_request_fn(struct request_queue *q)
345 while ((rq = blk_fetch_request(q)) != NULL) {
346 struct nullb_cmd *cmd = rq->special;
348 spin_unlock_irq(q->queue_lock);
349 null_handle_cmd(cmd);
350 spin_lock_irq(q->queue_lock);
354 static int null_queue_rq(struct blk_mq_hw_ctx *hctx,
355 const struct blk_mq_queue_data *bd)
357 struct nullb_cmd *cmd = blk_mq_rq_to_pdu(bd->rq);
359 if (irqmode == NULL_IRQ_TIMER) {
360 hrtimer_init(&cmd->timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
361 cmd->timer.function = null_cmd_timer_expired;
364 cmd->nq = hctx->driver_data;
366 blk_mq_start_request(bd->rq);
368 null_handle_cmd(cmd);
369 return BLK_MQ_RQ_QUEUE_OK;
372 static void null_init_queue(struct nullb *nullb, struct nullb_queue *nq)
377 init_waitqueue_head(&nq->wait);
378 nq->queue_depth = nullb->queue_depth;
381 static int null_init_hctx(struct blk_mq_hw_ctx *hctx, void *data,
384 struct nullb *nullb = data;
385 struct nullb_queue *nq = &nullb->queues[index];
387 hctx->driver_data = nq;
388 null_init_queue(nullb, nq);
394 static struct blk_mq_ops null_mq_ops = {
395 .queue_rq = null_queue_rq,
396 .map_queue = blk_mq_map_queue,
397 .init_hctx = null_init_hctx,
398 .complete = null_softirq_done_fn,
401 static void cleanup_queue(struct nullb_queue *nq)
407 static void cleanup_queues(struct nullb *nullb)
411 for (i = 0; i < nullb->nr_queues; i++)
412 cleanup_queue(&nullb->queues[i]);
414 kfree(nullb->queues);
417 static void null_del_dev(struct nullb *nullb)
419 list_del_init(&nullb->list);
422 nvm_unregister(nullb->disk_name);
424 del_gendisk(nullb->disk);
425 blk_cleanup_queue(nullb->q);
426 if (queue_mode == NULL_Q_MQ)
427 blk_mq_free_tag_set(&nullb->tag_set);
429 put_disk(nullb->disk);
430 cleanup_queues(nullb);
436 static void null_lnvm_end_io(struct request *rq, int error)
438 struct nvm_rq *rqd = rq->end_io_data;
439 struct nvm_dev *dev = rqd->dev;
441 dev->mt->end_io(rqd, error);
446 static int null_lnvm_submit_io(struct nvm_dev *dev, struct nvm_rq *rqd)
448 struct request_queue *q = dev->q;
450 struct bio *bio = rqd->bio;
452 rq = blk_mq_alloc_request(q, bio_rw(bio), 0);
456 rq->cmd_type = REQ_TYPE_DRV_PRIV;
457 rq->__sector = bio->bi_iter.bi_sector;
458 rq->ioprio = bio_prio(bio);
460 if (bio_has_data(bio))
461 rq->nr_phys_segments = bio_phys_segments(q, bio);
463 rq->__data_len = bio->bi_iter.bi_size;
464 rq->bio = rq->biotail = bio;
466 rq->end_io_data = rqd;
468 blk_execute_rq_nowait(q, NULL, rq, 0, null_lnvm_end_io);
473 static int null_lnvm_id(struct nvm_dev *dev, struct nvm_id *id)
475 sector_t size = gb * 1024 * 1024 * 1024ULL;
477 struct nvm_id_group *grp;
485 id->ppaf.blk_offset = 0;
486 id->ppaf.blk_len = 16;
487 id->ppaf.pg_offset = 16;
488 id->ppaf.pg_len = 16;
489 id->ppaf.sect_offset = 32;
490 id->ppaf.sect_len = 8;
491 id->ppaf.pln_offset = 40;
492 id->ppaf.pln_len = 8;
493 id->ppaf.lun_offset = 48;
494 id->ppaf.lun_len = 8;
495 id->ppaf.ch_offset = 56;
498 do_div(size, bs); /* convert size to pages */
499 do_div(size, 256); /* concert size to pgs pr blk */
500 grp = &id->groups[0];
506 do_div(size, (1 << 16));
507 grp->num_lun = size + 1;
508 do_div(blksize, grp->num_lun);
509 grp->num_blk = blksize;
520 grp->mpos = 0x010101; /* single plane rwe */
521 grp->cpar = hw_queue_depth;
526 static void *null_lnvm_create_dma_pool(struct nvm_dev *dev, char *name)
528 mempool_t *virtmem_pool;
530 virtmem_pool = mempool_create_slab_pool(64, ppa_cache);
532 pr_err("null_blk: Unable to create virtual memory pool\n");
539 static void null_lnvm_destroy_dma_pool(void *pool)
541 mempool_destroy(pool);
544 static void *null_lnvm_dev_dma_alloc(struct nvm_dev *dev, void *pool,
545 gfp_t mem_flags, dma_addr_t *dma_handler)
547 return mempool_alloc(pool, mem_flags);
550 static void null_lnvm_dev_dma_free(void *pool, void *entry,
551 dma_addr_t dma_handler)
553 mempool_free(entry, pool);
556 static struct nvm_dev_ops null_lnvm_dev_ops = {
557 .identity = null_lnvm_id,
558 .submit_io = null_lnvm_submit_io,
560 .create_dma_pool = null_lnvm_create_dma_pool,
561 .destroy_dma_pool = null_lnvm_destroy_dma_pool,
562 .dev_dma_alloc = null_lnvm_dev_dma_alloc,
563 .dev_dma_free = null_lnvm_dev_dma_free,
565 /* Simulate nvme protocol restriction */
569 static struct nvm_dev_ops null_lnvm_dev_ops;
570 #endif /* CONFIG_NVM */
572 static int null_open(struct block_device *bdev, fmode_t mode)
577 static void null_release(struct gendisk *disk, fmode_t mode)
581 static const struct block_device_operations null_fops = {
582 .owner = THIS_MODULE,
584 .release = null_release,
587 static int setup_commands(struct nullb_queue *nq)
589 struct nullb_cmd *cmd;
592 nq->cmds = kzalloc(nq->queue_depth * sizeof(*cmd), GFP_KERNEL);
596 tag_size = ALIGN(nq->queue_depth, BITS_PER_LONG) / BITS_PER_LONG;
597 nq->tag_map = kzalloc(tag_size * sizeof(unsigned long), GFP_KERNEL);
603 for (i = 0; i < nq->queue_depth; i++) {
605 INIT_LIST_HEAD(&cmd->list);
606 cmd->ll_list.next = NULL;
613 static int setup_queues(struct nullb *nullb)
615 nullb->queues = kzalloc(submit_queues * sizeof(struct nullb_queue),
620 nullb->nr_queues = 0;
621 nullb->queue_depth = hw_queue_depth;
626 static int init_driver_queues(struct nullb *nullb)
628 struct nullb_queue *nq;
631 for (i = 0; i < submit_queues; i++) {
632 nq = &nullb->queues[i];
634 null_init_queue(nullb, nq);
636 ret = setup_commands(nq);
644 static int null_add_dev(void)
646 struct gendisk *disk;
651 nullb = kzalloc_node(sizeof(*nullb), GFP_KERNEL, home_node);
657 spin_lock_init(&nullb->lock);
659 if (queue_mode == NULL_Q_MQ && use_per_node_hctx)
660 submit_queues = nr_online_nodes;
662 rv = setup_queues(nullb);
666 if (queue_mode == NULL_Q_MQ) {
667 nullb->tag_set.ops = &null_mq_ops;
668 nullb->tag_set.nr_hw_queues = submit_queues;
669 nullb->tag_set.queue_depth = hw_queue_depth;
670 nullb->tag_set.numa_node = home_node;
671 nullb->tag_set.cmd_size = sizeof(struct nullb_cmd);
672 nullb->tag_set.flags = BLK_MQ_F_SHOULD_MERGE;
673 nullb->tag_set.driver_data = nullb;
675 rv = blk_mq_alloc_tag_set(&nullb->tag_set);
677 goto out_cleanup_queues;
679 nullb->q = blk_mq_init_queue(&nullb->tag_set);
680 if (IS_ERR(nullb->q)) {
682 goto out_cleanup_tags;
684 } else if (queue_mode == NULL_Q_BIO) {
685 nullb->q = blk_alloc_queue_node(GFP_KERNEL, home_node);
688 goto out_cleanup_queues;
690 blk_queue_make_request(nullb->q, null_queue_bio);
691 rv = init_driver_queues(nullb);
693 goto out_cleanup_blk_queue;
695 nullb->q = blk_init_queue_node(null_request_fn, &nullb->lock, home_node);
698 goto out_cleanup_queues;
700 blk_queue_prep_rq(nullb->q, null_rq_prep_fn);
701 blk_queue_softirq_done(nullb->q, null_softirq_done_fn);
702 rv = init_driver_queues(nullb);
704 goto out_cleanup_blk_queue;
707 nullb->q->queuedata = nullb;
708 queue_flag_set_unlocked(QUEUE_FLAG_NONROT, nullb->q);
709 queue_flag_clear_unlocked(QUEUE_FLAG_ADD_RANDOM, nullb->q);
713 list_add_tail(&nullb->list, &nullb_list);
714 nullb->index = nullb_indexes++;
717 blk_queue_logical_block_size(nullb->q, bs);
718 blk_queue_physical_block_size(nullb->q, bs);
720 sprintf(nullb->disk_name, "nullb%d", nullb->index);
723 rv = nvm_register(nullb->q, nullb->disk_name,
726 goto out_cleanup_blk_queue;
730 disk = nullb->disk = alloc_disk_node(1, home_node);
733 goto out_cleanup_lightnvm;
735 size = gb * 1024 * 1024 * 1024ULL;
736 set_capacity(disk, size >> 9);
738 disk->flags |= GENHD_FL_EXT_DEVT | GENHD_FL_SUPPRESS_PARTITION_INFO;
739 disk->major = null_major;
740 disk->first_minor = nullb->index;
741 disk->fops = &null_fops;
742 disk->private_data = nullb;
743 disk->queue = nullb->q;
744 strncpy(disk->disk_name, nullb->disk_name, DISK_NAME_LEN);
750 out_cleanup_lightnvm:
752 nvm_unregister(nullb->disk_name);
753 out_cleanup_blk_queue:
754 blk_cleanup_queue(nullb->q);
756 if (queue_mode == NULL_Q_MQ)
757 blk_mq_free_tag_set(&nullb->tag_set);
759 cleanup_queues(nullb);
766 static int __init null_init(void)
772 if (bs > PAGE_SIZE) {
773 pr_warn("null_blk: invalid block size\n");
774 pr_warn("null_blk: defaults block size to %lu\n", PAGE_SIZE);
778 if (use_lightnvm && bs != 4096) {
779 pr_warn("null_blk: LightNVM only supports 4k block size\n");
780 pr_warn("null_blk: defaults block size to 4k\n");
784 if (use_lightnvm && queue_mode != NULL_Q_MQ) {
785 pr_warn("null_blk: LightNVM only supported for blk-mq\n");
786 pr_warn("null_blk: defaults queue mode to blk-mq\n");
787 queue_mode = NULL_Q_MQ;
790 if (queue_mode == NULL_Q_MQ && use_per_node_hctx) {
791 if (submit_queues < nr_online_nodes) {
792 pr_warn("null_blk: submit_queues param is set to %u.",
794 submit_queues = nr_online_nodes;
796 } else if (submit_queues > nr_cpu_ids)
797 submit_queues = nr_cpu_ids;
798 else if (!submit_queues)
803 null_major = register_blkdev(0, "nullb");
808 ppa_cache = kmem_cache_create("ppa_cache", 64 * sizeof(u64),
811 pr_err("null_blk: unable to create ppa cache\n");
817 for (i = 0; i < nr_devices; i++) {
818 ret = null_add_dev();
823 pr_info("null: module loaded\n");
827 while (!list_empty(&nullb_list)) {
828 nullb = list_entry(nullb_list.next, struct nullb, list);
831 kmem_cache_destroy(ppa_cache);
833 unregister_blkdev(null_major, "nullb");
837 static void __exit null_exit(void)
841 unregister_blkdev(null_major, "nullb");
844 while (!list_empty(&nullb_list)) {
845 nullb = list_entry(nullb_list.next, struct nullb, list);
850 kmem_cache_destroy(ppa_cache);
853 module_init(null_init);
854 module_exit(null_exit);
856 MODULE_AUTHOR("Jens Axboe <jaxboe@fusionio.com>");
857 MODULE_LICENSE("GPL");