null_blk.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859
  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. nvm_end_io(rqd, error);
  353. blk_put_request(rq);
  354. }
  355. static int null_lnvm_submit_io(struct nvm_dev *dev, struct nvm_rq *rqd)
  356. {
  357. struct request_queue *q = dev->q;
  358. struct request *rq;
  359. struct bio *bio = rqd->bio;
  360. rq = blk_mq_alloc_request(q, bio_rw(bio), 0);
  361. if (IS_ERR(rq))
  362. return -ENOMEM;
  363. rq->cmd_type = REQ_TYPE_DRV_PRIV;
  364. rq->__sector = bio->bi_iter.bi_sector;
  365. rq->ioprio = bio_prio(bio);
  366. if (bio_has_data(bio))
  367. rq->nr_phys_segments = bio_phys_segments(q, bio);
  368. rq->__data_len = bio->bi_iter.bi_size;
  369. rq->bio = rq->biotail = bio;
  370. rq->end_io_data = rqd;
  371. blk_execute_rq_nowait(q, NULL, rq, 0, null_lnvm_end_io);
  372. return 0;
  373. }
  374. static int null_lnvm_id(struct nvm_dev *dev, struct nvm_id *id)
  375. {
  376. sector_t size = gb * 1024 * 1024 * 1024ULL;
  377. sector_t blksize;
  378. struct nvm_id_group *grp;
  379. id->ver_id = 0x1;
  380. id->vmnt = 0;
  381. id->cgrps = 1;
  382. id->cap = 0x2;
  383. id->dom = 0x1;
  384. id->ppaf.blk_offset = 0;
  385. id->ppaf.blk_len = 16;
  386. id->ppaf.pg_offset = 16;
  387. id->ppaf.pg_len = 16;
  388. id->ppaf.sect_offset = 32;
  389. id->ppaf.sect_len = 8;
  390. id->ppaf.pln_offset = 40;
  391. id->ppaf.pln_len = 8;
  392. id->ppaf.lun_offset = 48;
  393. id->ppaf.lun_len = 8;
  394. id->ppaf.ch_offset = 56;
  395. id->ppaf.ch_len = 8;
  396. sector_div(size, bs); /* convert size to pages */
  397. size >>= 8; /* concert size to pgs pr blk */
  398. grp = &id->groups[0];
  399. grp->mtype = 0;
  400. grp->fmtype = 0;
  401. grp->num_ch = 1;
  402. grp->num_pg = 256;
  403. blksize = size;
  404. size >>= 16;
  405. grp->num_lun = size + 1;
  406. sector_div(blksize, grp->num_lun);
  407. grp->num_blk = blksize;
  408. grp->num_pln = 1;
  409. grp->fpg_sz = bs;
  410. grp->csecs = bs;
  411. grp->trdt = 25000;
  412. grp->trdm = 25000;
  413. grp->tprt = 500000;
  414. grp->tprm = 500000;
  415. grp->tbet = 1500000;
  416. grp->tbem = 1500000;
  417. grp->mpos = 0x010101; /* single plane rwe */
  418. grp->cpar = hw_queue_depth;
  419. return 0;
  420. }
  421. static void *null_lnvm_create_dma_pool(struct nvm_dev *dev, char *name)
  422. {
  423. mempool_t *virtmem_pool;
  424. virtmem_pool = mempool_create_slab_pool(64, ppa_cache);
  425. if (!virtmem_pool) {
  426. pr_err("null_blk: Unable to create virtual memory pool\n");
  427. return NULL;
  428. }
  429. return virtmem_pool;
  430. }
  431. static void null_lnvm_destroy_dma_pool(void *pool)
  432. {
  433. mempool_destroy(pool);
  434. }
  435. static void *null_lnvm_dev_dma_alloc(struct nvm_dev *dev, void *pool,
  436. gfp_t mem_flags, dma_addr_t *dma_handler)
  437. {
  438. return mempool_alloc(pool, mem_flags);
  439. }
  440. static void null_lnvm_dev_dma_free(void *pool, void *entry,
  441. dma_addr_t dma_handler)
  442. {
  443. mempool_free(entry, pool);
  444. }
  445. static struct nvm_dev_ops null_lnvm_dev_ops = {
  446. .identity = null_lnvm_id,
  447. .submit_io = null_lnvm_submit_io,
  448. .create_dma_pool = null_lnvm_create_dma_pool,
  449. .destroy_dma_pool = null_lnvm_destroy_dma_pool,
  450. .dev_dma_alloc = null_lnvm_dev_dma_alloc,
  451. .dev_dma_free = null_lnvm_dev_dma_free,
  452. /* Simulate nvme protocol restriction */
  453. .max_phys_sect = 64,
  454. };
  455. #else
  456. static struct nvm_dev_ops null_lnvm_dev_ops;
  457. #endif /* CONFIG_NVM */
  458. static int null_open(struct block_device *bdev, fmode_t mode)
  459. {
  460. return 0;
  461. }
  462. static void null_release(struct gendisk *disk, fmode_t mode)
  463. {
  464. }
  465. static const struct block_device_operations null_fops = {
  466. .owner = THIS_MODULE,
  467. .open = null_open,
  468. .release = null_release,
  469. };
  470. static int setup_commands(struct nullb_queue *nq)
  471. {
  472. struct nullb_cmd *cmd;
  473. int i, tag_size;
  474. nq->cmds = kzalloc(nq->queue_depth * sizeof(*cmd), GFP_KERNEL);
  475. if (!nq->cmds)
  476. return -ENOMEM;
  477. tag_size = ALIGN(nq->queue_depth, BITS_PER_LONG) / BITS_PER_LONG;
  478. nq->tag_map = kzalloc(tag_size * sizeof(unsigned long), GFP_KERNEL);
  479. if (!nq->tag_map) {
  480. kfree(nq->cmds);
  481. return -ENOMEM;
  482. }
  483. for (i = 0; i < nq->queue_depth; i++) {
  484. cmd = &nq->cmds[i];
  485. INIT_LIST_HEAD(&cmd->list);
  486. cmd->ll_list.next = NULL;
  487. cmd->tag = -1U;
  488. }
  489. return 0;
  490. }
  491. static int setup_queues(struct nullb *nullb)
  492. {
  493. nullb->queues = kzalloc(submit_queues * sizeof(struct nullb_queue),
  494. GFP_KERNEL);
  495. if (!nullb->queues)
  496. return -ENOMEM;
  497. nullb->nr_queues = 0;
  498. nullb->queue_depth = hw_queue_depth;
  499. return 0;
  500. }
  501. static int init_driver_queues(struct nullb *nullb)
  502. {
  503. struct nullb_queue *nq;
  504. int i, ret = 0;
  505. for (i = 0; i < submit_queues; i++) {
  506. nq = &nullb->queues[i];
  507. null_init_queue(nullb, nq);
  508. ret = setup_commands(nq);
  509. if (ret)
  510. return ret;
  511. nullb->nr_queues++;
  512. }
  513. return 0;
  514. }
  515. static int null_add_dev(void)
  516. {
  517. struct gendisk *disk;
  518. struct nullb *nullb;
  519. sector_t size;
  520. int rv;
  521. nullb = kzalloc_node(sizeof(*nullb), GFP_KERNEL, home_node);
  522. if (!nullb) {
  523. rv = -ENOMEM;
  524. goto out;
  525. }
  526. spin_lock_init(&nullb->lock);
  527. if (queue_mode == NULL_Q_MQ && use_per_node_hctx)
  528. submit_queues = nr_online_nodes;
  529. rv = setup_queues(nullb);
  530. if (rv)
  531. goto out_free_nullb;
  532. if (queue_mode == NULL_Q_MQ) {
  533. nullb->tag_set.ops = &null_mq_ops;
  534. nullb->tag_set.nr_hw_queues = submit_queues;
  535. nullb->tag_set.queue_depth = hw_queue_depth;
  536. nullb->tag_set.numa_node = home_node;
  537. nullb->tag_set.cmd_size = sizeof(struct nullb_cmd);
  538. nullb->tag_set.flags = BLK_MQ_F_SHOULD_MERGE;
  539. nullb->tag_set.driver_data = nullb;
  540. rv = blk_mq_alloc_tag_set(&nullb->tag_set);
  541. if (rv)
  542. goto out_cleanup_queues;
  543. nullb->q = blk_mq_init_queue(&nullb->tag_set);
  544. if (IS_ERR(nullb->q)) {
  545. rv = -ENOMEM;
  546. goto out_cleanup_tags;
  547. }
  548. } else if (queue_mode == NULL_Q_BIO) {
  549. nullb->q = blk_alloc_queue_node(GFP_KERNEL, home_node);
  550. if (!nullb->q) {
  551. rv = -ENOMEM;
  552. goto out_cleanup_queues;
  553. }
  554. blk_queue_make_request(nullb->q, null_queue_bio);
  555. rv = init_driver_queues(nullb);
  556. if (rv)
  557. goto out_cleanup_blk_queue;
  558. } else {
  559. nullb->q = blk_init_queue_node(null_request_fn, &nullb->lock, home_node);
  560. if (!nullb->q) {
  561. rv = -ENOMEM;
  562. goto out_cleanup_queues;
  563. }
  564. blk_queue_prep_rq(nullb->q, null_rq_prep_fn);
  565. blk_queue_softirq_done(nullb->q, null_softirq_done_fn);
  566. rv = init_driver_queues(nullb);
  567. if (rv)
  568. goto out_cleanup_blk_queue;
  569. }
  570. nullb->q->queuedata = nullb;
  571. queue_flag_set_unlocked(QUEUE_FLAG_NONROT, nullb->q);
  572. queue_flag_clear_unlocked(QUEUE_FLAG_ADD_RANDOM, nullb->q);
  573. mutex_lock(&lock);
  574. nullb->index = nullb_indexes++;
  575. mutex_unlock(&lock);
  576. blk_queue_logical_block_size(nullb->q, bs);
  577. blk_queue_physical_block_size(nullb->q, bs);
  578. sprintf(nullb->disk_name, "nullb%d", nullb->index);
  579. if (use_lightnvm) {
  580. rv = nvm_register(nullb->q, nullb->disk_name,
  581. &null_lnvm_dev_ops);
  582. if (rv)
  583. goto out_cleanup_blk_queue;
  584. goto done;
  585. }
  586. disk = nullb->disk = alloc_disk_node(1, home_node);
  587. if (!disk) {
  588. rv = -ENOMEM;
  589. goto out_cleanup_lightnvm;
  590. }
  591. size = gb * 1024 * 1024 * 1024ULL;
  592. set_capacity(disk, size >> 9);
  593. disk->flags |= GENHD_FL_EXT_DEVT | GENHD_FL_SUPPRESS_PARTITION_INFO;
  594. disk->major = null_major;
  595. disk->first_minor = nullb->index;
  596. disk->fops = &null_fops;
  597. disk->private_data = nullb;
  598. disk->queue = nullb->q;
  599. strncpy(disk->disk_name, nullb->disk_name, DISK_NAME_LEN);
  600. add_disk(disk);
  601. done:
  602. mutex_lock(&lock);
  603. list_add_tail(&nullb->list, &nullb_list);
  604. mutex_unlock(&lock);
  605. return 0;
  606. out_cleanup_lightnvm:
  607. if (use_lightnvm)
  608. nvm_unregister(nullb->disk_name);
  609. out_cleanup_blk_queue:
  610. blk_cleanup_queue(nullb->q);
  611. out_cleanup_tags:
  612. if (queue_mode == NULL_Q_MQ)
  613. blk_mq_free_tag_set(&nullb->tag_set);
  614. out_cleanup_queues:
  615. cleanup_queues(nullb);
  616. out_free_nullb:
  617. kfree(nullb);
  618. out:
  619. return rv;
  620. }
  621. static int __init null_init(void)
  622. {
  623. int ret = 0;
  624. unsigned int i;
  625. struct nullb *nullb;
  626. if (bs > PAGE_SIZE) {
  627. pr_warn("null_blk: invalid block size\n");
  628. pr_warn("null_blk: defaults block size to %lu\n", PAGE_SIZE);
  629. bs = PAGE_SIZE;
  630. }
  631. if (use_lightnvm && bs != 4096) {
  632. pr_warn("null_blk: LightNVM only supports 4k block size\n");
  633. pr_warn("null_blk: defaults block size to 4k\n");
  634. bs = 4096;
  635. }
  636. if (use_lightnvm && queue_mode != NULL_Q_MQ) {
  637. pr_warn("null_blk: LightNVM only supported for blk-mq\n");
  638. pr_warn("null_blk: defaults queue mode to blk-mq\n");
  639. queue_mode = NULL_Q_MQ;
  640. }
  641. if (queue_mode == NULL_Q_MQ && use_per_node_hctx) {
  642. if (submit_queues < nr_online_nodes) {
  643. pr_warn("null_blk: submit_queues param is set to %u.",
  644. nr_online_nodes);
  645. submit_queues = nr_online_nodes;
  646. }
  647. } else if (submit_queues > nr_cpu_ids)
  648. submit_queues = nr_cpu_ids;
  649. else if (!submit_queues)
  650. submit_queues = 1;
  651. mutex_init(&lock);
  652. null_major = register_blkdev(0, "nullb");
  653. if (null_major < 0)
  654. return null_major;
  655. if (use_lightnvm) {
  656. ppa_cache = kmem_cache_create("ppa_cache", 64 * sizeof(u64),
  657. 0, 0, NULL);
  658. if (!ppa_cache) {
  659. pr_err("null_blk: unable to create ppa cache\n");
  660. ret = -ENOMEM;
  661. goto err_ppa;
  662. }
  663. }
  664. for (i = 0; i < nr_devices; i++) {
  665. ret = null_add_dev();
  666. if (ret)
  667. goto err_dev;
  668. }
  669. pr_info("null: module loaded\n");
  670. return 0;
  671. err_dev:
  672. while (!list_empty(&nullb_list)) {
  673. nullb = list_entry(nullb_list.next, struct nullb, list);
  674. null_del_dev(nullb);
  675. }
  676. kmem_cache_destroy(ppa_cache);
  677. err_ppa:
  678. unregister_blkdev(null_major, "nullb");
  679. return ret;
  680. }
  681. static void __exit null_exit(void)
  682. {
  683. struct nullb *nullb;
  684. unregister_blkdev(null_major, "nullb");
  685. mutex_lock(&lock);
  686. while (!list_empty(&nullb_list)) {
  687. nullb = list_entry(nullb_list.next, struct nullb, list);
  688. null_del_dev(nullb);
  689. }
  690. mutex_unlock(&lock);
  691. kmem_cache_destroy(ppa_cache);
  692. }
  693. module_init(null_init);
  694. module_exit(null_exit);
  695. MODULE_AUTHOR("Jens Axboe <jaxboe@fusionio.com>");
  696. MODULE_LICENSE("GPL");