null_blk.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857
  1. #include <linux/module.h>
  2. #include <linux/moduleparam.h>
  3. #include <linux/sched.h>
  4. #include <linux/fs.h>
  5. #include <linux/blkdev.h>
  6. #include <linux/init.h>
  7. #include <linux/slab.h>
  8. #include <linux/blk-mq.h>
  9. #include <linux/hrtimer.h>
  10. #include <linux/lightnvm.h>
  11. struct nullb_cmd {
  12. struct list_head list;
  13. struct llist_node ll_list;
  14. struct call_single_data csd;
  15. struct request *rq;
  16. struct bio *bio;
  17. unsigned int tag;
  18. struct nullb_queue *nq;
  19. struct hrtimer timer;
  20. };
  21. struct nullb_queue {
  22. unsigned long *tag_map;
  23. wait_queue_head_t wait;
  24. unsigned int queue_depth;
  25. struct nullb_cmd *cmds;
  26. };
  27. struct nullb {
  28. struct list_head list;
  29. unsigned int index;
  30. struct request_queue *q;
  31. struct gendisk *disk;
  32. struct blk_mq_tag_set tag_set;
  33. struct hrtimer timer;
  34. unsigned int queue_depth;
  35. spinlock_t lock;
  36. struct nullb_queue *queues;
  37. unsigned int nr_queues;
  38. char disk_name[DISK_NAME_LEN];
  39. };
  40. static LIST_HEAD(nullb_list);
  41. static struct mutex lock;
  42. static int null_major;
  43. static int nullb_indexes;
  44. static struct kmem_cache *ppa_cache;
  45. enum {
  46. NULL_IRQ_NONE = 0,
  47. NULL_IRQ_SOFTIRQ = 1,
  48. NULL_IRQ_TIMER = 2,
  49. };
  50. enum {
  51. NULL_Q_BIO = 0,
  52. NULL_Q_RQ = 1,
  53. NULL_Q_MQ = 2,
  54. };
  55. static int submit_queues;
  56. module_param(submit_queues, int, S_IRUGO);
  57. MODULE_PARM_DESC(submit_queues, "Number of submission queues");
  58. static int home_node = NUMA_NO_NODE;
  59. module_param(home_node, int, S_IRUGO);
  60. MODULE_PARM_DESC(home_node, "Home node for the device");
  61. static int queue_mode = NULL_Q_MQ;
  62. static int null_param_store_val(const char *str, int *val, int min, int max)
  63. {
  64. int ret, new_val;
  65. ret = kstrtoint(str, 10, &new_val);
  66. if (ret)
  67. return -EINVAL;
  68. if (new_val < min || new_val > max)
  69. return -EINVAL;
  70. *val = new_val;
  71. return 0;
  72. }
  73. static int null_set_queue_mode(const char *str, const struct kernel_param *kp)
  74. {
  75. return null_param_store_val(str, &queue_mode, NULL_Q_BIO, NULL_Q_MQ);
  76. }
  77. static const struct kernel_param_ops null_queue_mode_param_ops = {
  78. .set = null_set_queue_mode,
  79. .get = param_get_int,
  80. };
  81. device_param_cb(queue_mode, &null_queue_mode_param_ops, &queue_mode, S_IRUGO);
  82. MODULE_PARM_DESC(queue_mode, "Block interface to use (0=bio,1=rq,2=multiqueue)");
  83. static int gb = 250;
  84. module_param(gb, int, S_IRUGO);
  85. MODULE_PARM_DESC(gb, "Size in GB");
  86. static int bs = 512;
  87. module_param(bs, int, S_IRUGO);
  88. MODULE_PARM_DESC(bs, "Block size (in bytes)");
  89. static int nr_devices = 2;
  90. module_param(nr_devices, int, S_IRUGO);
  91. MODULE_PARM_DESC(nr_devices, "Number of devices to register");
  92. static bool use_lightnvm;
  93. module_param(use_lightnvm, bool, S_IRUGO);
  94. MODULE_PARM_DESC(use_lightnvm, "Register as a LightNVM device");
  95. static int irqmode = NULL_IRQ_SOFTIRQ;
  96. static int null_set_irqmode(const char *str, const struct kernel_param *kp)
  97. {
  98. return null_param_store_val(str, &irqmode, NULL_IRQ_NONE,
  99. NULL_IRQ_TIMER);
  100. }
  101. static const struct kernel_param_ops null_irqmode_param_ops = {
  102. .set = null_set_irqmode,
  103. .get = param_get_int,
  104. };
  105. device_param_cb(irqmode, &null_irqmode_param_ops, &irqmode, S_IRUGO);
  106. MODULE_PARM_DESC(irqmode, "IRQ completion handler. 0-none, 1-softirq, 2-timer");
  107. static unsigned long completion_nsec = 10000;
  108. module_param(completion_nsec, ulong, S_IRUGO);
  109. MODULE_PARM_DESC(completion_nsec, "Time in ns to complete a request in hardware. Default: 10,000ns");
  110. static int hw_queue_depth = 64;
  111. module_param(hw_queue_depth, int, S_IRUGO);
  112. MODULE_PARM_DESC(hw_queue_depth, "Queue depth for each hardware queue. Default: 64");
  113. static bool use_per_node_hctx = false;
  114. module_param(use_per_node_hctx, bool, S_IRUGO);
  115. MODULE_PARM_DESC(use_per_node_hctx, "Use per-node allocation for hardware context queues. Default: false");
  116. static void put_tag(struct nullb_queue *nq, unsigned int tag)
  117. {
  118. clear_bit_unlock(tag, nq->tag_map);
  119. if (waitqueue_active(&nq->wait))
  120. wake_up(&nq->wait);
  121. }
  122. static unsigned int get_tag(struct nullb_queue *nq)
  123. {
  124. unsigned int tag;
  125. do {
  126. tag = find_first_zero_bit(nq->tag_map, nq->queue_depth);
  127. if (tag >= nq->queue_depth)
  128. return -1U;
  129. } while (test_and_set_bit_lock(tag, nq->tag_map));
  130. return tag;
  131. }
  132. static void free_cmd(struct nullb_cmd *cmd)
  133. {
  134. put_tag(cmd->nq, cmd->tag);
  135. }
  136. static enum hrtimer_restart null_cmd_timer_expired(struct hrtimer *timer);
  137. static struct nullb_cmd *__alloc_cmd(struct nullb_queue *nq)
  138. {
  139. struct nullb_cmd *cmd;
  140. unsigned int tag;
  141. tag = get_tag(nq);
  142. if (tag != -1U) {
  143. cmd = &nq->cmds[tag];
  144. cmd->tag = tag;
  145. cmd->nq = nq;
  146. if (irqmode == NULL_IRQ_TIMER) {
  147. hrtimer_init(&cmd->timer, CLOCK_MONOTONIC,
  148. HRTIMER_MODE_REL);
  149. cmd->timer.function = null_cmd_timer_expired;
  150. }
  151. return cmd;
  152. }
  153. return NULL;
  154. }
  155. static struct nullb_cmd *alloc_cmd(struct nullb_queue *nq, int can_wait)
  156. {
  157. struct nullb_cmd *cmd;
  158. DEFINE_WAIT(wait);
  159. cmd = __alloc_cmd(nq);
  160. if (cmd || !can_wait)
  161. return cmd;
  162. do {
  163. prepare_to_wait(&nq->wait, &wait, TASK_UNINTERRUPTIBLE);
  164. cmd = __alloc_cmd(nq);
  165. if (cmd)
  166. break;
  167. io_schedule();
  168. } while (1);
  169. finish_wait(&nq->wait, &wait);
  170. return cmd;
  171. }
  172. static void end_cmd(struct nullb_cmd *cmd)
  173. {
  174. struct request_queue *q = NULL;
  175. if (cmd->rq)
  176. q = cmd->rq->q;
  177. switch (queue_mode) {
  178. case NULL_Q_MQ:
  179. blk_mq_end_request(cmd->rq, 0);
  180. return;
  181. case NULL_Q_RQ:
  182. INIT_LIST_HEAD(&cmd->rq->queuelist);
  183. blk_end_request_all(cmd->rq, 0);
  184. break;
  185. case NULL_Q_BIO:
  186. bio_endio(cmd->bio);
  187. break;
  188. }
  189. free_cmd(cmd);
  190. /* Restart queue if needed, as we are freeing a tag */
  191. if (queue_mode == NULL_Q_RQ && blk_queue_stopped(q)) {
  192. unsigned long flags;
  193. spin_lock_irqsave(q->queue_lock, flags);
  194. blk_start_queue_async(q);
  195. spin_unlock_irqrestore(q->queue_lock, flags);
  196. }
  197. }
  198. static enum hrtimer_restart null_cmd_timer_expired(struct hrtimer *timer)
  199. {
  200. end_cmd(container_of(timer, struct nullb_cmd, timer));
  201. return HRTIMER_NORESTART;
  202. }
  203. static void null_cmd_end_timer(struct nullb_cmd *cmd)
  204. {
  205. ktime_t kt = ktime_set(0, completion_nsec);
  206. hrtimer_start(&cmd->timer, kt, HRTIMER_MODE_REL);
  207. }
  208. static void null_softirq_done_fn(struct request *rq)
  209. {
  210. if (queue_mode == NULL_Q_MQ)
  211. end_cmd(blk_mq_rq_to_pdu(rq));
  212. else
  213. end_cmd(rq->special);
  214. }
  215. static inline void null_handle_cmd(struct nullb_cmd *cmd)
  216. {
  217. /* Complete IO by inline, softirq or timer */
  218. switch (irqmode) {
  219. case NULL_IRQ_SOFTIRQ:
  220. switch (queue_mode) {
  221. case NULL_Q_MQ:
  222. blk_mq_complete_request(cmd->rq, cmd->rq->errors);
  223. break;
  224. case NULL_Q_RQ:
  225. blk_complete_request(cmd->rq);
  226. break;
  227. case NULL_Q_BIO:
  228. /*
  229. * XXX: no proper submitting cpu information available.
  230. */
  231. end_cmd(cmd);
  232. break;
  233. }
  234. break;
  235. case NULL_IRQ_NONE:
  236. end_cmd(cmd);
  237. break;
  238. case NULL_IRQ_TIMER:
  239. null_cmd_end_timer(cmd);
  240. break;
  241. }
  242. }
  243. static struct nullb_queue *nullb_to_queue(struct nullb *nullb)
  244. {
  245. int index = 0;
  246. if (nullb->nr_queues != 1)
  247. index = raw_smp_processor_id() / ((nr_cpu_ids + nullb->nr_queues - 1) / nullb->nr_queues);
  248. return &nullb->queues[index];
  249. }
  250. static blk_qc_t null_queue_bio(struct request_queue *q, struct bio *bio)
  251. {
  252. struct nullb *nullb = q->queuedata;
  253. struct nullb_queue *nq = nullb_to_queue(nullb);
  254. struct nullb_cmd *cmd;
  255. cmd = alloc_cmd(nq, 1);
  256. cmd->bio = bio;
  257. null_handle_cmd(cmd);
  258. return BLK_QC_T_NONE;
  259. }
  260. static int null_rq_prep_fn(struct request_queue *q, struct request *req)
  261. {
  262. struct nullb *nullb = q->queuedata;
  263. struct nullb_queue *nq = nullb_to_queue(nullb);
  264. struct nullb_cmd *cmd;
  265. cmd = alloc_cmd(nq, 0);
  266. if (cmd) {
  267. cmd->rq = req;
  268. req->special = cmd;
  269. return BLKPREP_OK;
  270. }
  271. blk_stop_queue(q);
  272. return BLKPREP_DEFER;
  273. }
  274. static void null_request_fn(struct request_queue *q)
  275. {
  276. struct request *rq;
  277. while ((rq = blk_fetch_request(q)) != NULL) {
  278. struct nullb_cmd *cmd = rq->special;
  279. spin_unlock_irq(q->queue_lock);
  280. null_handle_cmd(cmd);
  281. spin_lock_irq(q->queue_lock);
  282. }
  283. }
  284. static int null_queue_rq(struct blk_mq_hw_ctx *hctx,
  285. const struct blk_mq_queue_data *bd)
  286. {
  287. struct nullb_cmd *cmd = blk_mq_rq_to_pdu(bd->rq);
  288. if (irqmode == NULL_IRQ_TIMER) {
  289. hrtimer_init(&cmd->timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
  290. cmd->timer.function = null_cmd_timer_expired;
  291. }
  292. cmd->rq = bd->rq;
  293. cmd->nq = hctx->driver_data;
  294. blk_mq_start_request(bd->rq);
  295. null_handle_cmd(cmd);
  296. return BLK_MQ_RQ_QUEUE_OK;
  297. }
  298. static void null_init_queue(struct nullb *nullb, struct nullb_queue *nq)
  299. {
  300. BUG_ON(!nullb);
  301. BUG_ON(!nq);
  302. init_waitqueue_head(&nq->wait);
  303. nq->queue_depth = nullb->queue_depth;
  304. }
  305. static int null_init_hctx(struct blk_mq_hw_ctx *hctx, void *data,
  306. unsigned int index)
  307. {
  308. struct nullb *nullb = data;
  309. struct nullb_queue *nq = &nullb->queues[index];
  310. hctx->driver_data = nq;
  311. null_init_queue(nullb, nq);
  312. nullb->nr_queues++;
  313. return 0;
  314. }
  315. static struct blk_mq_ops null_mq_ops = {
  316. .queue_rq = null_queue_rq,
  317. .map_queue = blk_mq_map_queue,
  318. .init_hctx = null_init_hctx,
  319. .complete = null_softirq_done_fn,
  320. };
  321. static void cleanup_queue(struct nullb_queue *nq)
  322. {
  323. kfree(nq->tag_map);
  324. kfree(nq->cmds);
  325. }
  326. static void cleanup_queues(struct nullb *nullb)
  327. {
  328. int i;
  329. for (i = 0; i < nullb->nr_queues; i++)
  330. cleanup_queue(&nullb->queues[i]);
  331. kfree(nullb->queues);
  332. }
  333. static void null_del_dev(struct nullb *nullb)
  334. {
  335. list_del_init(&nullb->list);
  336. if (use_lightnvm)
  337. nvm_unregister(nullb->disk_name);
  338. else
  339. del_gendisk(nullb->disk);
  340. blk_cleanup_queue(nullb->q);
  341. if (queue_mode == NULL_Q_MQ)
  342. blk_mq_free_tag_set(&nullb->tag_set);
  343. if (!use_lightnvm)
  344. put_disk(nullb->disk);
  345. cleanup_queues(nullb);
  346. kfree(nullb);
  347. }
  348. #ifdef CONFIG_NVM
  349. static void null_lnvm_end_io(struct request *rq, int error)
  350. {
  351. struct nvm_rq *rqd = rq->end_io_data;
  352. struct nvm_dev *dev = rqd->dev;
  353. dev->mt->end_io(rqd, error);
  354. blk_put_request(rq);
  355. }
  356. static int null_lnvm_submit_io(struct nvm_dev *dev, struct nvm_rq *rqd)
  357. {
  358. struct request_queue *q = dev->q;
  359. struct request *rq;
  360. struct bio *bio = rqd->bio;
  361. rq = blk_mq_alloc_request(q, bio_rw(bio), GFP_KERNEL, 0);
  362. if (IS_ERR(rq))
  363. return -ENOMEM;
  364. rq->cmd_type = REQ_TYPE_DRV_PRIV;
  365. rq->__sector = bio->bi_iter.bi_sector;
  366. rq->ioprio = bio_prio(bio);
  367. if (bio_has_data(bio))
  368. rq->nr_phys_segments = bio_phys_segments(q, bio);
  369. rq->__data_len = bio->bi_iter.bi_size;
  370. rq->bio = rq->biotail = bio;
  371. rq->end_io_data = rqd;
  372. blk_execute_rq_nowait(q, NULL, rq, 0, null_lnvm_end_io);
  373. return 0;
  374. }
  375. static int null_lnvm_id(struct nvm_dev *dev, struct nvm_id *id)
  376. {
  377. sector_t size = gb * 1024 * 1024 * 1024ULL;
  378. sector_t blksize;
  379. struct nvm_id_group *grp;
  380. id->ver_id = 0x1;
  381. id->vmnt = 0;
  382. id->cgrps = 1;
  383. id->cap = 0x3;
  384. id->dom = 0x1;
  385. id->ppaf.blk_offset = 0;
  386. id->ppaf.blk_len = 16;
  387. id->ppaf.pg_offset = 16;
  388. id->ppaf.pg_len = 16;
  389. id->ppaf.sect_offset = 32;
  390. id->ppaf.sect_len = 8;
  391. id->ppaf.pln_offset = 40;
  392. id->ppaf.pln_len = 8;
  393. id->ppaf.lun_offset = 48;
  394. id->ppaf.lun_len = 8;
  395. id->ppaf.ch_offset = 56;
  396. id->ppaf.ch_len = 8;
  397. do_div(size, bs); /* convert size to pages */
  398. do_div(size, 256); /* concert size to pgs pr blk */
  399. grp = &id->groups[0];
  400. grp->mtype = 0;
  401. grp->fmtype = 0;
  402. grp->num_ch = 1;
  403. grp->num_pg = 256;
  404. blksize = size;
  405. do_div(size, (1 << 16));
  406. grp->num_lun = size + 1;
  407. do_div(blksize, grp->num_lun);
  408. grp->num_blk = blksize;
  409. grp->num_pln = 1;
  410. grp->fpg_sz = bs;
  411. grp->csecs = bs;
  412. grp->trdt = 25000;
  413. grp->trdm = 25000;
  414. grp->tprt = 500000;
  415. grp->tprm = 500000;
  416. grp->tbet = 1500000;
  417. grp->tbem = 1500000;
  418. grp->mpos = 0x010101; /* single plane rwe */
  419. grp->cpar = hw_queue_depth;
  420. return 0;
  421. }
  422. static void *null_lnvm_create_dma_pool(struct nvm_dev *dev, char *name)
  423. {
  424. mempool_t *virtmem_pool;
  425. virtmem_pool = mempool_create_slab_pool(64, ppa_cache);
  426. if (!virtmem_pool) {
  427. pr_err("null_blk: Unable to create virtual memory pool\n");
  428. return NULL;
  429. }
  430. return virtmem_pool;
  431. }
  432. static void null_lnvm_destroy_dma_pool(void *pool)
  433. {
  434. mempool_destroy(pool);
  435. }
  436. static void *null_lnvm_dev_dma_alloc(struct nvm_dev *dev, void *pool,
  437. gfp_t mem_flags, dma_addr_t *dma_handler)
  438. {
  439. return mempool_alloc(pool, mem_flags);
  440. }
  441. static void null_lnvm_dev_dma_free(void *pool, void *entry,
  442. dma_addr_t dma_handler)
  443. {
  444. mempool_free(entry, pool);
  445. }
  446. static struct nvm_dev_ops null_lnvm_dev_ops = {
  447. .identity = null_lnvm_id,
  448. .submit_io = null_lnvm_submit_io,
  449. .create_dma_pool = null_lnvm_create_dma_pool,
  450. .destroy_dma_pool = null_lnvm_destroy_dma_pool,
  451. .dev_dma_alloc = null_lnvm_dev_dma_alloc,
  452. .dev_dma_free = null_lnvm_dev_dma_free,
  453. /* Simulate nvme protocol restriction */
  454. .max_phys_sect = 64,
  455. };
  456. #else
  457. static struct nvm_dev_ops null_lnvm_dev_ops;
  458. #endif /* CONFIG_NVM */
  459. static int null_open(struct block_device *bdev, fmode_t mode)
  460. {
  461. return 0;
  462. }
  463. static void null_release(struct gendisk *disk, fmode_t mode)
  464. {
  465. }
  466. static const struct block_device_operations null_fops = {
  467. .owner = THIS_MODULE,
  468. .open = null_open,
  469. .release = null_release,
  470. };
  471. static int setup_commands(struct nullb_queue *nq)
  472. {
  473. struct nullb_cmd *cmd;
  474. int i, tag_size;
  475. nq->cmds = kzalloc(nq->queue_depth * sizeof(*cmd), GFP_KERNEL);
  476. if (!nq->cmds)
  477. return -ENOMEM;
  478. tag_size = ALIGN(nq->queue_depth, BITS_PER_LONG) / BITS_PER_LONG;
  479. nq->tag_map = kzalloc(tag_size * sizeof(unsigned long), GFP_KERNEL);
  480. if (!nq->tag_map) {
  481. kfree(nq->cmds);
  482. return -ENOMEM;
  483. }
  484. for (i = 0; i < nq->queue_depth; i++) {
  485. cmd = &nq->cmds[i];
  486. INIT_LIST_HEAD(&cmd->list);
  487. cmd->ll_list.next = NULL;
  488. cmd->tag = -1U;
  489. }
  490. return 0;
  491. }
  492. static int setup_queues(struct nullb *nullb)
  493. {
  494. nullb->queues = kzalloc(submit_queues * sizeof(struct nullb_queue),
  495. GFP_KERNEL);
  496. if (!nullb->queues)
  497. return -ENOMEM;
  498. nullb->nr_queues = 0;
  499. nullb->queue_depth = hw_queue_depth;
  500. return 0;
  501. }
  502. static int init_driver_queues(struct nullb *nullb)
  503. {
  504. struct nullb_queue *nq;
  505. int i, ret = 0;
  506. for (i = 0; i < submit_queues; i++) {
  507. nq = &nullb->queues[i];
  508. null_init_queue(nullb, nq);
  509. ret = setup_commands(nq);
  510. if (ret)
  511. return ret;
  512. nullb->nr_queues++;
  513. }
  514. return 0;
  515. }
  516. static int null_add_dev(void)
  517. {
  518. struct gendisk *disk;
  519. struct nullb *nullb;
  520. sector_t size;
  521. int rv;
  522. nullb = kzalloc_node(sizeof(*nullb), GFP_KERNEL, home_node);
  523. if (!nullb) {
  524. rv = -ENOMEM;
  525. goto out;
  526. }
  527. spin_lock_init(&nullb->lock);
  528. if (queue_mode == NULL_Q_MQ && use_per_node_hctx)
  529. submit_queues = nr_online_nodes;
  530. rv = setup_queues(nullb);
  531. if (rv)
  532. goto out_free_nullb;
  533. if (queue_mode == NULL_Q_MQ) {
  534. nullb->tag_set.ops = &null_mq_ops;
  535. nullb->tag_set.nr_hw_queues = submit_queues;
  536. nullb->tag_set.queue_depth = hw_queue_depth;
  537. nullb->tag_set.numa_node = home_node;
  538. nullb->tag_set.cmd_size = sizeof(struct nullb_cmd);
  539. nullb->tag_set.flags = BLK_MQ_F_SHOULD_MERGE;
  540. nullb->tag_set.driver_data = nullb;
  541. rv = blk_mq_alloc_tag_set(&nullb->tag_set);
  542. if (rv)
  543. goto out_cleanup_queues;
  544. nullb->q = blk_mq_init_queue(&nullb->tag_set);
  545. if (IS_ERR(nullb->q)) {
  546. rv = -ENOMEM;
  547. goto out_cleanup_tags;
  548. }
  549. } else if (queue_mode == NULL_Q_BIO) {
  550. nullb->q = blk_alloc_queue_node(GFP_KERNEL, home_node);
  551. if (!nullb->q) {
  552. rv = -ENOMEM;
  553. goto out_cleanup_queues;
  554. }
  555. blk_queue_make_request(nullb->q, null_queue_bio);
  556. rv = init_driver_queues(nullb);
  557. if (rv)
  558. goto out_cleanup_blk_queue;
  559. } else {
  560. nullb->q = blk_init_queue_node(null_request_fn, &nullb->lock, home_node);
  561. if (!nullb->q) {
  562. rv = -ENOMEM;
  563. goto out_cleanup_queues;
  564. }
  565. blk_queue_prep_rq(nullb->q, null_rq_prep_fn);
  566. blk_queue_softirq_done(nullb->q, null_softirq_done_fn);
  567. rv = init_driver_queues(nullb);
  568. if (rv)
  569. goto out_cleanup_blk_queue;
  570. }
  571. nullb->q->queuedata = nullb;
  572. queue_flag_set_unlocked(QUEUE_FLAG_NONROT, nullb->q);
  573. queue_flag_clear_unlocked(QUEUE_FLAG_ADD_RANDOM, nullb->q);
  574. mutex_lock(&lock);
  575. list_add_tail(&nullb->list, &nullb_list);
  576. nullb->index = nullb_indexes++;
  577. mutex_unlock(&lock);
  578. blk_queue_logical_block_size(nullb->q, bs);
  579. blk_queue_physical_block_size(nullb->q, bs);
  580. sprintf(nullb->disk_name, "nullb%d", nullb->index);
  581. if (use_lightnvm) {
  582. rv = nvm_register(nullb->q, nullb->disk_name,
  583. &null_lnvm_dev_ops);
  584. if (rv)
  585. goto out_cleanup_blk_queue;
  586. goto done;
  587. }
  588. disk = nullb->disk = alloc_disk_node(1, home_node);
  589. if (!disk) {
  590. rv = -ENOMEM;
  591. goto out_cleanup_lightnvm;
  592. }
  593. size = gb * 1024 * 1024 * 1024ULL;
  594. set_capacity(disk, size >> 9);
  595. disk->flags |= GENHD_FL_EXT_DEVT | GENHD_FL_SUPPRESS_PARTITION_INFO;
  596. disk->major = null_major;
  597. disk->first_minor = nullb->index;
  598. disk->fops = &null_fops;
  599. disk->private_data = nullb;
  600. disk->queue = nullb->q;
  601. strncpy(disk->disk_name, nullb->disk_name, DISK_NAME_LEN);
  602. add_disk(disk);
  603. done:
  604. return 0;
  605. out_cleanup_lightnvm:
  606. if (use_lightnvm)
  607. nvm_unregister(nullb->disk_name);
  608. out_cleanup_blk_queue:
  609. blk_cleanup_queue(nullb->q);
  610. out_cleanup_tags:
  611. if (queue_mode == NULL_Q_MQ)
  612. blk_mq_free_tag_set(&nullb->tag_set);
  613. out_cleanup_queues:
  614. cleanup_queues(nullb);
  615. out_free_nullb:
  616. kfree(nullb);
  617. out:
  618. return rv;
  619. }
  620. static int __init null_init(void)
  621. {
  622. int ret = 0;
  623. unsigned int i;
  624. struct nullb *nullb;
  625. if (bs > PAGE_SIZE) {
  626. pr_warn("null_blk: invalid block size\n");
  627. pr_warn("null_blk: defaults block size to %lu\n", PAGE_SIZE);
  628. bs = PAGE_SIZE;
  629. }
  630. if (use_lightnvm && bs != 4096) {
  631. pr_warn("null_blk: LightNVM only supports 4k block size\n");
  632. pr_warn("null_blk: defaults block size to 4k\n");
  633. bs = 4096;
  634. }
  635. if (use_lightnvm && queue_mode != NULL_Q_MQ) {
  636. pr_warn("null_blk: LightNVM only supported for blk-mq\n");
  637. pr_warn("null_blk: defaults queue mode to blk-mq\n");
  638. queue_mode = NULL_Q_MQ;
  639. }
  640. if (queue_mode == NULL_Q_MQ && use_per_node_hctx) {
  641. if (submit_queues < nr_online_nodes) {
  642. pr_warn("null_blk: submit_queues param is set to %u.",
  643. nr_online_nodes);
  644. submit_queues = nr_online_nodes;
  645. }
  646. } else if (submit_queues > nr_cpu_ids)
  647. submit_queues = nr_cpu_ids;
  648. else if (!submit_queues)
  649. submit_queues = 1;
  650. mutex_init(&lock);
  651. null_major = register_blkdev(0, "nullb");
  652. if (null_major < 0)
  653. return null_major;
  654. if (use_lightnvm) {
  655. ppa_cache = kmem_cache_create("ppa_cache", 64 * sizeof(u64),
  656. 0, 0, NULL);
  657. if (!ppa_cache) {
  658. pr_err("null_blk: unable to create ppa cache\n");
  659. ret = -ENOMEM;
  660. goto err_ppa;
  661. }
  662. }
  663. for (i = 0; i < nr_devices; i++) {
  664. ret = null_add_dev();
  665. if (ret)
  666. goto err_dev;
  667. }
  668. pr_info("null: module loaded\n");
  669. return 0;
  670. err_dev:
  671. while (!list_empty(&nullb_list)) {
  672. nullb = list_entry(nullb_list.next, struct nullb, list);
  673. null_del_dev(nullb);
  674. }
  675. kmem_cache_destroy(ppa_cache);
  676. err_ppa:
  677. unregister_blkdev(null_major, "nullb");
  678. return ret;
  679. }
  680. static void __exit null_exit(void)
  681. {
  682. struct nullb *nullb;
  683. unregister_blkdev(null_major, "nullb");
  684. mutex_lock(&lock);
  685. while (!list_empty(&nullb_list)) {
  686. nullb = list_entry(nullb_list.next, struct nullb, list);
  687. null_del_dev(nullb);
  688. }
  689. mutex_unlock(&lock);
  690. kmem_cache_destroy(ppa_cache);
  691. }
  692. module_init(null_init);
  693. module_exit(null_exit);
  694. MODULE_AUTHOR("Jens Axboe <jaxboe@fusionio.com>");
  695. MODULE_LICENSE("GPL");