null_blk.c 19 KB

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