null_blk.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889
  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 int irqmode = NULL_IRQ_SOFTIRQ;
  97. static int null_set_irqmode(const char *str, const struct kernel_param *kp)
  98. {
  99. return null_param_store_val(str, &irqmode, NULL_IRQ_NONE,
  100. NULL_IRQ_TIMER);
  101. }
  102. static const struct kernel_param_ops null_irqmode_param_ops = {
  103. .set = null_set_irqmode,
  104. .get = param_get_int,
  105. };
  106. device_param_cb(irqmode, &null_irqmode_param_ops, &irqmode, S_IRUGO);
  107. MODULE_PARM_DESC(irqmode, "IRQ completion handler. 0-none, 1-softirq, 2-timer");
  108. static unsigned long completion_nsec = 10000;
  109. module_param(completion_nsec, ulong, S_IRUGO);
  110. MODULE_PARM_DESC(completion_nsec, "Time in ns to complete a request in hardware. Default: 10,000ns");
  111. static int hw_queue_depth = 64;
  112. module_param(hw_queue_depth, int, S_IRUGO);
  113. MODULE_PARM_DESC(hw_queue_depth, "Queue depth for each hardware queue. Default: 64");
  114. static bool use_per_node_hctx = false;
  115. module_param(use_per_node_hctx, bool, S_IRUGO);
  116. MODULE_PARM_DESC(use_per_node_hctx, "Use per-node allocation for hardware context queues. Default: false");
  117. static void put_tag(struct nullb_queue *nq, unsigned int tag)
  118. {
  119. clear_bit_unlock(tag, nq->tag_map);
  120. if (waitqueue_active(&nq->wait))
  121. wake_up(&nq->wait);
  122. }
  123. static unsigned int get_tag(struct nullb_queue *nq)
  124. {
  125. unsigned int tag;
  126. do {
  127. tag = find_first_zero_bit(nq->tag_map, nq->queue_depth);
  128. if (tag >= nq->queue_depth)
  129. return -1U;
  130. } while (test_and_set_bit_lock(tag, nq->tag_map));
  131. return tag;
  132. }
  133. static void free_cmd(struct nullb_cmd *cmd)
  134. {
  135. put_tag(cmd->nq, cmd->tag);
  136. }
  137. static enum hrtimer_restart null_cmd_timer_expired(struct hrtimer *timer);
  138. static struct nullb_cmd *__alloc_cmd(struct nullb_queue *nq)
  139. {
  140. struct nullb_cmd *cmd;
  141. unsigned int tag;
  142. tag = get_tag(nq);
  143. if (tag != -1U) {
  144. cmd = &nq->cmds[tag];
  145. cmd->tag = tag;
  146. cmd->nq = nq;
  147. if (irqmode == NULL_IRQ_TIMER) {
  148. hrtimer_init(&cmd->timer, CLOCK_MONOTONIC,
  149. HRTIMER_MODE_REL);
  150. cmd->timer.function = null_cmd_timer_expired;
  151. }
  152. return cmd;
  153. }
  154. return NULL;
  155. }
  156. static struct nullb_cmd *alloc_cmd(struct nullb_queue *nq, int can_wait)
  157. {
  158. struct nullb_cmd *cmd;
  159. DEFINE_WAIT(wait);
  160. cmd = __alloc_cmd(nq);
  161. if (cmd || !can_wait)
  162. return cmd;
  163. do {
  164. prepare_to_wait(&nq->wait, &wait, TASK_UNINTERRUPTIBLE);
  165. cmd = __alloc_cmd(nq);
  166. if (cmd)
  167. break;
  168. io_schedule();
  169. } while (1);
  170. finish_wait(&nq->wait, &wait);
  171. return cmd;
  172. }
  173. static void end_cmd(struct nullb_cmd *cmd)
  174. {
  175. struct request_queue *q = NULL;
  176. if (cmd->rq)
  177. q = cmd->rq->q;
  178. switch (queue_mode) {
  179. case NULL_Q_MQ:
  180. blk_mq_end_request(cmd->rq, 0);
  181. return;
  182. case NULL_Q_RQ:
  183. INIT_LIST_HEAD(&cmd->rq->queuelist);
  184. blk_end_request_all(cmd->rq, 0);
  185. break;
  186. case NULL_Q_BIO:
  187. bio_endio(cmd->bio);
  188. break;
  189. }
  190. free_cmd(cmd);
  191. /* Restart queue if needed, as we are freeing a tag */
  192. if (queue_mode == NULL_Q_RQ && blk_queue_stopped(q)) {
  193. unsigned long flags;
  194. spin_lock_irqsave(q->queue_lock, flags);
  195. blk_start_queue_async(q);
  196. spin_unlock_irqrestore(q->queue_lock, flags);
  197. }
  198. }
  199. static enum hrtimer_restart null_cmd_timer_expired(struct hrtimer *timer)
  200. {
  201. end_cmd(container_of(timer, struct nullb_cmd, timer));
  202. return HRTIMER_NORESTART;
  203. }
  204. static void null_cmd_end_timer(struct nullb_cmd *cmd)
  205. {
  206. ktime_t kt = ktime_set(0, completion_nsec);
  207. hrtimer_start(&cmd->timer, kt, HRTIMER_MODE_REL);
  208. }
  209. static void null_softirq_done_fn(struct request *rq)
  210. {
  211. if (queue_mode == NULL_Q_MQ)
  212. end_cmd(blk_mq_rq_to_pdu(rq));
  213. else
  214. end_cmd(rq->special);
  215. }
  216. static inline void null_handle_cmd(struct nullb_cmd *cmd)
  217. {
  218. /* Complete IO by inline, softirq or timer */
  219. switch (irqmode) {
  220. case NULL_IRQ_SOFTIRQ:
  221. switch (queue_mode) {
  222. case NULL_Q_MQ:
  223. blk_mq_complete_request(cmd->rq, cmd->rq->errors);
  224. break;
  225. case NULL_Q_RQ:
  226. blk_complete_request(cmd->rq);
  227. break;
  228. case NULL_Q_BIO:
  229. /*
  230. * XXX: no proper submitting cpu information available.
  231. */
  232. end_cmd(cmd);
  233. break;
  234. }
  235. break;
  236. case NULL_IRQ_NONE:
  237. end_cmd(cmd);
  238. break;
  239. case NULL_IRQ_TIMER:
  240. null_cmd_end_timer(cmd);
  241. break;
  242. }
  243. }
  244. static struct nullb_queue *nullb_to_queue(struct nullb *nullb)
  245. {
  246. int index = 0;
  247. if (nullb->nr_queues != 1)
  248. index = raw_smp_processor_id() / ((nr_cpu_ids + nullb->nr_queues - 1) / nullb->nr_queues);
  249. return &nullb->queues[index];
  250. }
  251. static blk_qc_t null_queue_bio(struct request_queue *q, struct bio *bio)
  252. {
  253. struct nullb *nullb = q->queuedata;
  254. struct nullb_queue *nq = nullb_to_queue(nullb);
  255. struct nullb_cmd *cmd;
  256. cmd = alloc_cmd(nq, 1);
  257. cmd->bio = bio;
  258. null_handle_cmd(cmd);
  259. return BLK_QC_T_NONE;
  260. }
  261. static int null_rq_prep_fn(struct request_queue *q, struct request *req)
  262. {
  263. struct nullb *nullb = q->queuedata;
  264. struct nullb_queue *nq = nullb_to_queue(nullb);
  265. struct nullb_cmd *cmd;
  266. cmd = alloc_cmd(nq, 0);
  267. if (cmd) {
  268. cmd->rq = req;
  269. req->special = cmd;
  270. return BLKPREP_OK;
  271. }
  272. blk_stop_queue(q);
  273. return BLKPREP_DEFER;
  274. }
  275. static void null_request_fn(struct request_queue *q)
  276. {
  277. struct request *rq;
  278. while ((rq = blk_fetch_request(q)) != NULL) {
  279. struct nullb_cmd *cmd = rq->special;
  280. spin_unlock_irq(q->queue_lock);
  281. null_handle_cmd(cmd);
  282. spin_lock_irq(q->queue_lock);
  283. }
  284. }
  285. static int null_queue_rq(struct blk_mq_hw_ctx *hctx,
  286. const struct blk_mq_queue_data *bd)
  287. {
  288. struct nullb_cmd *cmd = blk_mq_rq_to_pdu(bd->rq);
  289. if (irqmode == NULL_IRQ_TIMER) {
  290. hrtimer_init(&cmd->timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
  291. cmd->timer.function = null_cmd_timer_expired;
  292. }
  293. cmd->rq = bd->rq;
  294. cmd->nq = hctx->driver_data;
  295. blk_mq_start_request(bd->rq);
  296. null_handle_cmd(cmd);
  297. return BLK_MQ_RQ_QUEUE_OK;
  298. }
  299. static void null_init_queue(struct nullb *nullb, struct nullb_queue *nq)
  300. {
  301. BUG_ON(!nullb);
  302. BUG_ON(!nq);
  303. init_waitqueue_head(&nq->wait);
  304. nq->queue_depth = nullb->queue_depth;
  305. }
  306. static int null_init_hctx(struct blk_mq_hw_ctx *hctx, void *data,
  307. unsigned int index)
  308. {
  309. struct nullb *nullb = data;
  310. struct nullb_queue *nq = &nullb->queues[index];
  311. hctx->driver_data = nq;
  312. null_init_queue(nullb, nq);
  313. nullb->nr_queues++;
  314. return 0;
  315. }
  316. static struct blk_mq_ops null_mq_ops = {
  317. .queue_rq = null_queue_rq,
  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. #ifdef CONFIG_NVM
  334. static void null_lnvm_end_io(struct request *rq, int error)
  335. {
  336. struct nvm_rq *rqd = rq->end_io_data;
  337. nvm_end_io(rqd, error);
  338. blk_put_request(rq);
  339. }
  340. static int null_lnvm_submit_io(struct nvm_dev *dev, struct nvm_rq *rqd)
  341. {
  342. struct request_queue *q = dev->q;
  343. struct request *rq;
  344. struct bio *bio = rqd->bio;
  345. rq = blk_mq_alloc_request(q, bio_data_dir(bio), 0);
  346. if (IS_ERR(rq))
  347. return -ENOMEM;
  348. rq->cmd_type = REQ_TYPE_DRV_PRIV;
  349. rq->__sector = bio->bi_iter.bi_sector;
  350. rq->ioprio = bio_prio(bio);
  351. if (bio_has_data(bio))
  352. rq->nr_phys_segments = bio_phys_segments(q, bio);
  353. rq->__data_len = bio->bi_iter.bi_size;
  354. rq->bio = rq->biotail = 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->cgrps = 1;
  367. id->cap = 0x2;
  368. id->dom = 0x1;
  369. id->ppaf.blk_offset = 0;
  370. id->ppaf.blk_len = 16;
  371. id->ppaf.pg_offset = 16;
  372. id->ppaf.pg_len = 16;
  373. id->ppaf.sect_offset = 32;
  374. id->ppaf.sect_len = 8;
  375. id->ppaf.pln_offset = 40;
  376. id->ppaf.pln_len = 8;
  377. id->ppaf.lun_offset = 48;
  378. id->ppaf.lun_len = 8;
  379. id->ppaf.ch_offset = 56;
  380. id->ppaf.ch_len = 8;
  381. sector_div(size, bs); /* convert size to pages */
  382. size >>= 8; /* concert size to pgs pr blk */
  383. grp = &id->groups[0];
  384. grp->mtype = 0;
  385. grp->fmtype = 0;
  386. grp->num_ch = 1;
  387. grp->num_pg = 256;
  388. blksize = size;
  389. size >>= 16;
  390. grp->num_lun = size + 1;
  391. sector_div(blksize, grp->num_lun);
  392. grp->num_blk = blksize;
  393. grp->num_pln = 1;
  394. grp->fpg_sz = bs;
  395. grp->csecs = bs;
  396. grp->trdt = 25000;
  397. grp->trdm = 25000;
  398. grp->tprt = 500000;
  399. grp->tprm = 500000;
  400. grp->tbet = 1500000;
  401. grp->tbem = 1500000;
  402. grp->mpos = 0x010101; /* single plane rwe */
  403. grp->cpar = hw_queue_depth;
  404. return 0;
  405. }
  406. static void *null_lnvm_create_dma_pool(struct nvm_dev *dev, char *name)
  407. {
  408. mempool_t *virtmem_pool;
  409. virtmem_pool = mempool_create_slab_pool(64, ppa_cache);
  410. if (!virtmem_pool) {
  411. pr_err("null_blk: Unable to create virtual memory pool\n");
  412. return NULL;
  413. }
  414. return virtmem_pool;
  415. }
  416. static void null_lnvm_destroy_dma_pool(void *pool)
  417. {
  418. mempool_destroy(pool);
  419. }
  420. static void *null_lnvm_dev_dma_alloc(struct nvm_dev *dev, void *pool,
  421. gfp_t mem_flags, dma_addr_t *dma_handler)
  422. {
  423. return mempool_alloc(pool, mem_flags);
  424. }
  425. static void null_lnvm_dev_dma_free(void *pool, void *entry,
  426. dma_addr_t dma_handler)
  427. {
  428. mempool_free(entry, pool);
  429. }
  430. static struct nvm_dev_ops null_lnvm_dev_ops = {
  431. .identity = null_lnvm_id,
  432. .submit_io = null_lnvm_submit_io,
  433. .create_dma_pool = null_lnvm_create_dma_pool,
  434. .destroy_dma_pool = null_lnvm_destroy_dma_pool,
  435. .dev_dma_alloc = null_lnvm_dev_dma_alloc,
  436. .dev_dma_free = null_lnvm_dev_dma_free,
  437. /* Simulate nvme protocol restriction */
  438. .max_phys_sect = 64,
  439. };
  440. static int null_nvm_register(struct nullb *nullb)
  441. {
  442. struct nvm_dev *dev;
  443. int rv;
  444. dev = nvm_alloc_dev(0);
  445. if (!dev)
  446. return -ENOMEM;
  447. dev->q = nullb->q;
  448. memcpy(dev->name, nullb->disk_name, DISK_NAME_LEN);
  449. dev->ops = &null_lnvm_dev_ops;
  450. rv = nvm_register(dev);
  451. if (rv) {
  452. kfree(dev);
  453. return rv;
  454. }
  455. nullb->ndev = dev;
  456. return 0;
  457. }
  458. static void null_nvm_unregister(struct nullb *nullb)
  459. {
  460. nvm_unregister(nullb->ndev);
  461. }
  462. #else
  463. static int null_nvm_register(struct nullb *nullb)
  464. {
  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. rv = blk_mq_alloc_tag_set(&nullb->tag_set);
  584. if (rv)
  585. goto out_cleanup_queues;
  586. nullb->q = blk_mq_init_queue(&nullb->tag_set);
  587. if (IS_ERR(nullb->q)) {
  588. rv = -ENOMEM;
  589. goto out_cleanup_tags;
  590. }
  591. } else if (queue_mode == NULL_Q_BIO) {
  592. nullb->q = blk_alloc_queue_node(GFP_KERNEL, home_node);
  593. if (!nullb->q) {
  594. rv = -ENOMEM;
  595. goto out_cleanup_queues;
  596. }
  597. blk_queue_make_request(nullb->q, null_queue_bio);
  598. rv = init_driver_queues(nullb);
  599. if (rv)
  600. goto out_cleanup_blk_queue;
  601. } else {
  602. nullb->q = blk_init_queue_node(null_request_fn, &nullb->lock, home_node);
  603. if (!nullb->q) {
  604. rv = -ENOMEM;
  605. goto out_cleanup_queues;
  606. }
  607. blk_queue_prep_rq(nullb->q, null_rq_prep_fn);
  608. blk_queue_softirq_done(nullb->q, null_softirq_done_fn);
  609. rv = init_driver_queues(nullb);
  610. if (rv)
  611. goto out_cleanup_blk_queue;
  612. }
  613. nullb->q->queuedata = nullb;
  614. queue_flag_set_unlocked(QUEUE_FLAG_NONROT, nullb->q);
  615. queue_flag_clear_unlocked(QUEUE_FLAG_ADD_RANDOM, nullb->q);
  616. mutex_lock(&lock);
  617. nullb->index = nullb_indexes++;
  618. mutex_unlock(&lock);
  619. blk_queue_logical_block_size(nullb->q, bs);
  620. blk_queue_physical_block_size(nullb->q, bs);
  621. sprintf(nullb->disk_name, "nullb%d", nullb->index);
  622. if (use_lightnvm)
  623. rv = null_nvm_register(nullb);
  624. else
  625. rv = null_gendisk_register(nullb);
  626. if (rv)
  627. goto out_cleanup_blk_queue;
  628. mutex_lock(&lock);
  629. list_add_tail(&nullb->list, &nullb_list);
  630. mutex_unlock(&lock);
  631. return 0;
  632. out_cleanup_blk_queue:
  633. blk_cleanup_queue(nullb->q);
  634. out_cleanup_tags:
  635. if (queue_mode == NULL_Q_MQ)
  636. blk_mq_free_tag_set(&nullb->tag_set);
  637. out_cleanup_queues:
  638. cleanup_queues(nullb);
  639. out_free_nullb:
  640. kfree(nullb);
  641. out:
  642. return rv;
  643. }
  644. static int __init null_init(void)
  645. {
  646. int ret = 0;
  647. unsigned int i;
  648. struct nullb *nullb;
  649. if (bs > PAGE_SIZE) {
  650. pr_warn("null_blk: invalid block size\n");
  651. pr_warn("null_blk: defaults block size to %lu\n", PAGE_SIZE);
  652. bs = PAGE_SIZE;
  653. }
  654. if (use_lightnvm && bs != 4096) {
  655. pr_warn("null_blk: LightNVM only supports 4k block size\n");
  656. pr_warn("null_blk: defaults block size to 4k\n");
  657. bs = 4096;
  658. }
  659. if (use_lightnvm && queue_mode != NULL_Q_MQ) {
  660. pr_warn("null_blk: LightNVM only supported for blk-mq\n");
  661. pr_warn("null_blk: defaults queue mode to blk-mq\n");
  662. queue_mode = NULL_Q_MQ;
  663. }
  664. if (queue_mode == NULL_Q_MQ && use_per_node_hctx) {
  665. if (submit_queues < nr_online_nodes) {
  666. pr_warn("null_blk: submit_queues param is set to %u.",
  667. nr_online_nodes);
  668. submit_queues = nr_online_nodes;
  669. }
  670. } else if (submit_queues > nr_cpu_ids)
  671. submit_queues = nr_cpu_ids;
  672. else if (!submit_queues)
  673. submit_queues = 1;
  674. mutex_init(&lock);
  675. null_major = register_blkdev(0, "nullb");
  676. if (null_major < 0)
  677. return null_major;
  678. if (use_lightnvm) {
  679. ppa_cache = kmem_cache_create("ppa_cache", 64 * sizeof(u64),
  680. 0, 0, NULL);
  681. if (!ppa_cache) {
  682. pr_err("null_blk: unable to create ppa cache\n");
  683. ret = -ENOMEM;
  684. goto err_ppa;
  685. }
  686. }
  687. for (i = 0; i < nr_devices; i++) {
  688. ret = null_add_dev();
  689. if (ret)
  690. goto err_dev;
  691. }
  692. pr_info("null: module loaded\n");
  693. return 0;
  694. err_dev:
  695. while (!list_empty(&nullb_list)) {
  696. nullb = list_entry(nullb_list.next, struct nullb, list);
  697. null_del_dev(nullb);
  698. }
  699. kmem_cache_destroy(ppa_cache);
  700. err_ppa:
  701. unregister_blkdev(null_major, "nullb");
  702. return ret;
  703. }
  704. static void __exit null_exit(void)
  705. {
  706. struct nullb *nullb;
  707. unregister_blkdev(null_major, "nullb");
  708. mutex_lock(&lock);
  709. while (!list_empty(&nullb_list)) {
  710. nullb = list_entry(nullb_list.next, struct nullb, list);
  711. null_del_dev(nullb);
  712. }
  713. mutex_unlock(&lock);
  714. kmem_cache_destroy(ppa_cache);
  715. }
  716. module_init(null_init);
  717. module_exit(null_exit);
  718. MODULE_AUTHOR("Jens Axboe <jaxboe@fusionio.com>");
  719. MODULE_LICENSE("GPL");