gennvm.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683
  1. /*
  2. * Copyright (C) 2015 Matias Bjorling <m@bjorling.me>
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public License version
  6. * 2 as published by the Free Software Foundation.
  7. *
  8. * This program is distributed in the hope that it will be useful, but
  9. * WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. * General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU General Public License
  14. * along with this program; see the file COPYING. If not, write to
  15. * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139,
  16. * USA.
  17. *
  18. * Implementation of a general nvm manager for Open-Channel SSDs.
  19. */
  20. #include "gennvm.h"
  21. static struct nvm_target *gen_find_target(struct gen_dev *gn, const char *name)
  22. {
  23. struct nvm_target *tgt;
  24. list_for_each_entry(tgt, &gn->targets, list)
  25. if (!strcmp(name, tgt->disk->disk_name))
  26. return tgt;
  27. return NULL;
  28. }
  29. static const struct block_device_operations gen_fops = {
  30. .owner = THIS_MODULE,
  31. };
  32. static int gen_create_tgt(struct nvm_dev *dev, struct nvm_ioctl_create *create)
  33. {
  34. struct gen_dev *gn = dev->mp;
  35. struct nvm_ioctl_create_simple *s = &create->conf.s;
  36. struct request_queue *tqueue;
  37. struct gendisk *tdisk;
  38. struct nvm_tgt_type *tt;
  39. struct nvm_target *t;
  40. void *targetdata;
  41. tt = nvm_find_target_type(create->tgttype, 1);
  42. if (!tt) {
  43. pr_err("nvm: target type %s not found\n", create->tgttype);
  44. return -EINVAL;
  45. }
  46. mutex_lock(&gn->lock);
  47. t = gen_find_target(gn, create->tgtname);
  48. if (t) {
  49. pr_err("nvm: target name already exists.\n");
  50. mutex_unlock(&gn->lock);
  51. return -EINVAL;
  52. }
  53. mutex_unlock(&gn->lock);
  54. t = kmalloc(sizeof(struct nvm_target), GFP_KERNEL);
  55. if (!t)
  56. return -ENOMEM;
  57. tqueue = blk_alloc_queue_node(GFP_KERNEL, dev->q->node);
  58. if (!tqueue)
  59. goto err_t;
  60. blk_queue_make_request(tqueue, tt->make_rq);
  61. tdisk = alloc_disk(0);
  62. if (!tdisk)
  63. goto err_queue;
  64. sprintf(tdisk->disk_name, "%s", create->tgtname);
  65. tdisk->flags = GENHD_FL_EXT_DEVT;
  66. tdisk->major = 0;
  67. tdisk->first_minor = 0;
  68. tdisk->fops = &gen_fops;
  69. tdisk->queue = tqueue;
  70. targetdata = tt->init(dev, tdisk, s->lun_begin, s->lun_end);
  71. if (IS_ERR(targetdata))
  72. goto err_init;
  73. tdisk->private_data = targetdata;
  74. tqueue->queuedata = targetdata;
  75. blk_queue_max_hw_sectors(tqueue, 8 * dev->ops->max_phys_sect);
  76. set_capacity(tdisk, tt->capacity(targetdata));
  77. add_disk(tdisk);
  78. t->type = tt;
  79. t->disk = tdisk;
  80. t->dev = dev;
  81. mutex_lock(&gn->lock);
  82. list_add_tail(&t->list, &gn->targets);
  83. mutex_unlock(&gn->lock);
  84. return 0;
  85. err_init:
  86. put_disk(tdisk);
  87. err_queue:
  88. blk_cleanup_queue(tqueue);
  89. err_t:
  90. kfree(t);
  91. return -ENOMEM;
  92. }
  93. static void __gen_remove_target(struct nvm_target *t)
  94. {
  95. struct nvm_tgt_type *tt = t->type;
  96. struct gendisk *tdisk = t->disk;
  97. struct request_queue *q = tdisk->queue;
  98. del_gendisk(tdisk);
  99. blk_cleanup_queue(q);
  100. if (tt->exit)
  101. tt->exit(tdisk->private_data);
  102. put_disk(tdisk);
  103. list_del(&t->list);
  104. kfree(t);
  105. }
  106. /**
  107. * gen_remove_tgt - Removes a target from the media manager
  108. * @dev: device
  109. * @remove: ioctl structure with target name to remove.
  110. *
  111. * Returns:
  112. * 0: on success
  113. * 1: on not found
  114. * <0: on error
  115. */
  116. static int gen_remove_tgt(struct nvm_dev *dev, struct nvm_ioctl_remove *remove)
  117. {
  118. struct gen_dev *gn = dev->mp;
  119. struct nvm_target *t;
  120. if (!gn)
  121. return 1;
  122. mutex_lock(&gn->lock);
  123. t = gen_find_target(gn, remove->tgtname);
  124. if (!t) {
  125. mutex_unlock(&gn->lock);
  126. return 1;
  127. }
  128. __gen_remove_target(t);
  129. mutex_unlock(&gn->lock);
  130. return 0;
  131. }
  132. static int gen_get_area(struct nvm_dev *dev, sector_t *lba, sector_t len)
  133. {
  134. struct gen_dev *gn = dev->mp;
  135. struct gen_area *area, *prev, *next;
  136. sector_t begin = 0;
  137. sector_t max_sectors = (dev->sec_size * dev->total_secs) >> 9;
  138. if (len > max_sectors)
  139. return -EINVAL;
  140. area = kmalloc(sizeof(struct gen_area), GFP_KERNEL);
  141. if (!area)
  142. return -ENOMEM;
  143. prev = NULL;
  144. spin_lock(&dev->lock);
  145. list_for_each_entry(next, &gn->area_list, list) {
  146. if (begin + len > next->begin) {
  147. begin = next->end;
  148. prev = next;
  149. continue;
  150. }
  151. break;
  152. }
  153. if ((begin + len) > max_sectors) {
  154. spin_unlock(&dev->lock);
  155. kfree(area);
  156. return -EINVAL;
  157. }
  158. area->begin = *lba = begin;
  159. area->end = begin + len;
  160. if (prev) /* insert into sorted order */
  161. list_add(&area->list, &prev->list);
  162. else
  163. list_add(&area->list, &gn->area_list);
  164. spin_unlock(&dev->lock);
  165. return 0;
  166. }
  167. static void gen_put_area(struct nvm_dev *dev, sector_t begin)
  168. {
  169. struct gen_dev *gn = dev->mp;
  170. struct gen_area *area;
  171. spin_lock(&dev->lock);
  172. list_for_each_entry(area, &gn->area_list, list) {
  173. if (area->begin != begin)
  174. continue;
  175. list_del(&area->list);
  176. spin_unlock(&dev->lock);
  177. kfree(area);
  178. return;
  179. }
  180. spin_unlock(&dev->lock);
  181. }
  182. static void gen_blocks_free(struct nvm_dev *dev)
  183. {
  184. struct gen_dev *gn = dev->mp;
  185. struct gen_lun *lun;
  186. int i;
  187. gen_for_each_lun(gn, lun, i) {
  188. if (!lun->vlun.blocks)
  189. break;
  190. vfree(lun->vlun.blocks);
  191. }
  192. }
  193. static void gen_luns_free(struct nvm_dev *dev)
  194. {
  195. struct gen_dev *gn = dev->mp;
  196. kfree(gn->luns);
  197. }
  198. static int gen_luns_init(struct nvm_dev *dev, struct gen_dev *gn)
  199. {
  200. struct gen_lun *lun;
  201. int i;
  202. gn->luns = kcalloc(dev->nr_luns, sizeof(struct gen_lun), GFP_KERNEL);
  203. if (!gn->luns)
  204. return -ENOMEM;
  205. gen_for_each_lun(gn, lun, i) {
  206. spin_lock_init(&lun->vlun.lock);
  207. INIT_LIST_HEAD(&lun->free_list);
  208. INIT_LIST_HEAD(&lun->used_list);
  209. INIT_LIST_HEAD(&lun->bb_list);
  210. lun->reserved_blocks = 2; /* for GC only */
  211. lun->vlun.id = i;
  212. lun->vlun.lun_id = i % dev->luns_per_chnl;
  213. lun->vlun.chnl_id = i / dev->luns_per_chnl;
  214. lun->vlun.nr_free_blocks = dev->blks_per_lun;
  215. }
  216. return 0;
  217. }
  218. static int gen_block_bb(struct gen_dev *gn, struct ppa_addr ppa,
  219. u8 *blks, int nr_blks)
  220. {
  221. struct nvm_dev *dev = gn->dev;
  222. struct gen_lun *lun;
  223. struct nvm_block *blk;
  224. int i;
  225. nr_blks = nvm_bb_tbl_fold(dev, blks, nr_blks);
  226. if (nr_blks < 0)
  227. return nr_blks;
  228. lun = &gn->luns[(dev->luns_per_chnl * ppa.g.ch) + ppa.g.lun];
  229. for (i = 0; i < nr_blks; i++) {
  230. if (blks[i] == 0)
  231. continue;
  232. blk = &lun->vlun.blocks[i];
  233. list_move_tail(&blk->list, &lun->bb_list);
  234. lun->vlun.nr_free_blocks--;
  235. }
  236. return 0;
  237. }
  238. static int gen_block_map(u64 slba, u32 nlb, __le64 *entries, void *private)
  239. {
  240. struct nvm_dev *dev = private;
  241. struct gen_dev *gn = dev->mp;
  242. u64 elba = slba + nlb;
  243. struct gen_lun *lun;
  244. struct nvm_block *blk;
  245. u64 i;
  246. int lun_id;
  247. if (unlikely(elba > dev->total_secs)) {
  248. pr_err("gen: L2P data from device is out of bounds!\n");
  249. return -EINVAL;
  250. }
  251. for (i = 0; i < nlb; i++) {
  252. u64 pba = le64_to_cpu(entries[i]);
  253. if (unlikely(pba >= dev->total_secs && pba != U64_MAX)) {
  254. pr_err("gen: L2P data entry is out of bounds!\n");
  255. return -EINVAL;
  256. }
  257. /* Address zero is a special one. The first page on a disk is
  258. * protected. It often holds internal device boot
  259. * information.
  260. */
  261. if (!pba)
  262. continue;
  263. /* resolve block from physical address */
  264. lun_id = div_u64(pba, dev->sec_per_lun);
  265. lun = &gn->luns[lun_id];
  266. /* Calculate block offset into lun */
  267. pba = pba - (dev->sec_per_lun * lun_id);
  268. blk = &lun->vlun.blocks[div_u64(pba, dev->sec_per_blk)];
  269. if (!blk->state) {
  270. /* at this point, we don't know anything about the
  271. * block. It's up to the FTL on top to re-etablish the
  272. * block state. The block is assumed to be open.
  273. */
  274. list_move_tail(&blk->list, &lun->used_list);
  275. blk->state = NVM_BLK_ST_TGT;
  276. lun->vlun.nr_free_blocks--;
  277. }
  278. }
  279. return 0;
  280. }
  281. static int gen_blocks_init(struct nvm_dev *dev, struct gen_dev *gn)
  282. {
  283. struct gen_lun *lun;
  284. struct nvm_block *block;
  285. sector_t lun_iter, blk_iter, cur_block_id = 0;
  286. int ret, nr_blks;
  287. u8 *blks;
  288. nr_blks = dev->blks_per_lun * dev->plane_mode;
  289. blks = kmalloc(nr_blks, GFP_KERNEL);
  290. if (!blks)
  291. return -ENOMEM;
  292. gen_for_each_lun(gn, lun, lun_iter) {
  293. lun->vlun.blocks = vzalloc(sizeof(struct nvm_block) *
  294. dev->blks_per_lun);
  295. if (!lun->vlun.blocks) {
  296. kfree(blks);
  297. return -ENOMEM;
  298. }
  299. for (blk_iter = 0; blk_iter < dev->blks_per_lun; blk_iter++) {
  300. block = &lun->vlun.blocks[blk_iter];
  301. INIT_LIST_HEAD(&block->list);
  302. block->lun = &lun->vlun;
  303. block->id = cur_block_id++;
  304. /* First block is reserved for device */
  305. if (unlikely(lun_iter == 0 && blk_iter == 0)) {
  306. lun->vlun.nr_free_blocks--;
  307. continue;
  308. }
  309. list_add_tail(&block->list, &lun->free_list);
  310. }
  311. if (dev->ops->get_bb_tbl) {
  312. struct ppa_addr ppa;
  313. ppa.ppa = 0;
  314. ppa.g.ch = lun->vlun.chnl_id;
  315. ppa.g.lun = lun->vlun.lun_id;
  316. ret = nvm_get_bb_tbl(dev, ppa, blks);
  317. if (ret)
  318. pr_err("gen: could not get BB table\n");
  319. ret = gen_block_bb(gn, ppa, blks, nr_blks);
  320. if (ret)
  321. pr_err("gen: BB table map failed\n");
  322. }
  323. }
  324. if ((dev->identity.dom & NVM_RSP_L2P) && dev->ops->get_l2p_tbl) {
  325. ret = dev->ops->get_l2p_tbl(dev, 0, dev->total_secs,
  326. gen_block_map, dev);
  327. if (ret) {
  328. pr_err("gen: could not read L2P table.\n");
  329. pr_warn("gen: default block initialization");
  330. }
  331. }
  332. kfree(blks);
  333. return 0;
  334. }
  335. static void gen_free(struct nvm_dev *dev)
  336. {
  337. gen_blocks_free(dev);
  338. gen_luns_free(dev);
  339. kfree(dev->mp);
  340. dev->mp = NULL;
  341. }
  342. static int gen_register(struct nvm_dev *dev)
  343. {
  344. struct gen_dev *gn;
  345. int ret;
  346. if (!try_module_get(THIS_MODULE))
  347. return -ENODEV;
  348. gn = kzalloc(sizeof(struct gen_dev), GFP_KERNEL);
  349. if (!gn)
  350. return -ENOMEM;
  351. gn->dev = dev;
  352. gn->nr_luns = dev->nr_luns;
  353. INIT_LIST_HEAD(&gn->area_list);
  354. mutex_init(&gn->lock);
  355. INIT_LIST_HEAD(&gn->targets);
  356. dev->mp = gn;
  357. ret = gen_luns_init(dev, gn);
  358. if (ret) {
  359. pr_err("gen: could not initialize luns\n");
  360. goto err;
  361. }
  362. ret = gen_blocks_init(dev, gn);
  363. if (ret) {
  364. pr_err("gen: could not initialize blocks\n");
  365. goto err;
  366. }
  367. return 1;
  368. err:
  369. gen_free(dev);
  370. module_put(THIS_MODULE);
  371. return ret;
  372. }
  373. static void gen_unregister(struct nvm_dev *dev)
  374. {
  375. struct gen_dev *gn = dev->mp;
  376. struct nvm_target *t, *tmp;
  377. mutex_lock(&gn->lock);
  378. list_for_each_entry_safe(t, tmp, &gn->targets, list) {
  379. if (t->dev != dev)
  380. continue;
  381. __gen_remove_target(t);
  382. }
  383. mutex_unlock(&gn->lock);
  384. gen_free(dev);
  385. module_put(THIS_MODULE);
  386. }
  387. static struct nvm_block *gen_get_blk(struct nvm_dev *dev,
  388. struct nvm_lun *vlun, unsigned long flags)
  389. {
  390. struct gen_lun *lun = container_of(vlun, struct gen_lun, vlun);
  391. struct nvm_block *blk = NULL;
  392. int is_gc = flags & NVM_IOTYPE_GC;
  393. spin_lock(&vlun->lock);
  394. if (list_empty(&lun->free_list)) {
  395. pr_err_ratelimited("gen: lun %u have no free pages available",
  396. lun->vlun.id);
  397. goto out;
  398. }
  399. if (!is_gc && lun->vlun.nr_free_blocks < lun->reserved_blocks)
  400. goto out;
  401. blk = list_first_entry(&lun->free_list, struct nvm_block, list);
  402. list_move_tail(&blk->list, &lun->used_list);
  403. blk->state = NVM_BLK_ST_TGT;
  404. lun->vlun.nr_free_blocks--;
  405. out:
  406. spin_unlock(&vlun->lock);
  407. return blk;
  408. }
  409. static void gen_put_blk(struct nvm_dev *dev, struct nvm_block *blk)
  410. {
  411. struct nvm_lun *vlun = blk->lun;
  412. struct gen_lun *lun = container_of(vlun, struct gen_lun, vlun);
  413. spin_lock(&vlun->lock);
  414. if (blk->state & NVM_BLK_ST_TGT) {
  415. list_move_tail(&blk->list, &lun->free_list);
  416. lun->vlun.nr_free_blocks++;
  417. blk->state = NVM_BLK_ST_FREE;
  418. } else if (blk->state & NVM_BLK_ST_BAD) {
  419. list_move_tail(&blk->list, &lun->bb_list);
  420. blk->state = NVM_BLK_ST_BAD;
  421. } else {
  422. WARN_ON_ONCE(1);
  423. pr_err("gen: erroneous block type (%lu -> %u)\n",
  424. blk->id, blk->state);
  425. list_move_tail(&blk->list, &lun->bb_list);
  426. }
  427. spin_unlock(&vlun->lock);
  428. }
  429. static void gen_mark_blk(struct nvm_dev *dev, struct ppa_addr ppa, int type)
  430. {
  431. struct gen_dev *gn = dev->mp;
  432. struct gen_lun *lun;
  433. struct nvm_block *blk;
  434. pr_debug("gen: ppa (ch: %u lun: %u blk: %u pg: %u) -> %u\n",
  435. ppa.g.ch, ppa.g.lun, ppa.g.blk, ppa.g.pg, type);
  436. if (unlikely(ppa.g.ch > dev->nr_chnls ||
  437. ppa.g.lun > dev->luns_per_chnl ||
  438. ppa.g.blk > dev->blks_per_lun)) {
  439. WARN_ON_ONCE(1);
  440. pr_err("gen: ppa broken (ch: %u > %u lun: %u > %u blk: %u > %u",
  441. ppa.g.ch, dev->nr_chnls,
  442. ppa.g.lun, dev->luns_per_chnl,
  443. ppa.g.blk, dev->blks_per_lun);
  444. return;
  445. }
  446. lun = &gn->luns[(dev->luns_per_chnl * ppa.g.ch) + ppa.g.lun];
  447. blk = &lun->vlun.blocks[ppa.g.blk];
  448. /* will be moved to bb list on put_blk from target */
  449. blk->state = type;
  450. }
  451. /*
  452. * mark block bad in gen. It is expected that the target recovers separately
  453. */
  454. static void gen_mark_blk_bad(struct nvm_dev *dev, struct nvm_rq *rqd)
  455. {
  456. int bit = -1;
  457. int max_secs = dev->ops->max_phys_sect;
  458. void *comp_bits = &rqd->ppa_status;
  459. nvm_addr_to_generic_mode(dev, rqd);
  460. /* look up blocks and mark them as bad */
  461. if (rqd->nr_ppas == 1) {
  462. gen_mark_blk(dev, rqd->ppa_addr, NVM_BLK_ST_BAD);
  463. return;
  464. }
  465. while ((bit = find_next_bit(comp_bits, max_secs, bit + 1)) < max_secs)
  466. gen_mark_blk(dev, rqd->ppa_list[bit], NVM_BLK_ST_BAD);
  467. }
  468. static void gen_end_io(struct nvm_rq *rqd)
  469. {
  470. struct nvm_tgt_instance *ins = rqd->ins;
  471. if (rqd->error == NVM_RSP_ERR_FAILWRITE)
  472. gen_mark_blk_bad(rqd->dev, rqd);
  473. ins->tt->end_io(rqd);
  474. }
  475. static int gen_submit_io(struct nvm_dev *dev, struct nvm_rq *rqd)
  476. {
  477. if (!dev->ops->submit_io)
  478. return -ENODEV;
  479. /* Convert address space */
  480. nvm_generic_to_addr_mode(dev, rqd);
  481. rqd->dev = dev;
  482. rqd->end_io = gen_end_io;
  483. return dev->ops->submit_io(dev, rqd);
  484. }
  485. static int gen_erase_blk(struct nvm_dev *dev, struct nvm_block *blk,
  486. unsigned long flags)
  487. {
  488. struct ppa_addr addr = block_to_ppa(dev, blk);
  489. return nvm_erase_ppa(dev, &addr, 1);
  490. }
  491. static int gen_reserve_lun(struct nvm_dev *dev, int lunid)
  492. {
  493. return test_and_set_bit(lunid, dev->lun_map);
  494. }
  495. static void gen_release_lun(struct nvm_dev *dev, int lunid)
  496. {
  497. WARN_ON(!test_and_clear_bit(lunid, dev->lun_map));
  498. }
  499. static struct nvm_lun *gen_get_lun(struct nvm_dev *dev, int lunid)
  500. {
  501. struct gen_dev *gn = dev->mp;
  502. if (unlikely(lunid >= dev->nr_luns))
  503. return NULL;
  504. return &gn->luns[lunid].vlun;
  505. }
  506. static void gen_lun_info_print(struct nvm_dev *dev)
  507. {
  508. struct gen_dev *gn = dev->mp;
  509. struct gen_lun *lun;
  510. unsigned int i;
  511. gen_for_each_lun(gn, lun, i) {
  512. spin_lock(&lun->vlun.lock);
  513. pr_info("%s: lun%8u\t%u\n", dev->name, i,
  514. lun->vlun.nr_free_blocks);
  515. spin_unlock(&lun->vlun.lock);
  516. }
  517. }
  518. static struct nvmm_type gen = {
  519. .name = "gennvm",
  520. .version = {0, 1, 0},
  521. .register_mgr = gen_register,
  522. .unregister_mgr = gen_unregister,
  523. .create_tgt = gen_create_tgt,
  524. .remove_tgt = gen_remove_tgt,
  525. .get_blk = gen_get_blk,
  526. .put_blk = gen_put_blk,
  527. .submit_io = gen_submit_io,
  528. .erase_blk = gen_erase_blk,
  529. .mark_blk = gen_mark_blk,
  530. .get_lun = gen_get_lun,
  531. .reserve_lun = gen_reserve_lun,
  532. .release_lun = gen_release_lun,
  533. .lun_info_print = gen_lun_info_print,
  534. .get_area = gen_get_area,
  535. .put_area = gen_put_area,
  536. };
  537. static int __init gen_module_init(void)
  538. {
  539. return nvm_register_mgr(&gen);
  540. }
  541. static void gen_module_exit(void)
  542. {
  543. nvm_unregister_mgr(&gen);
  544. }
  545. module_init(gen_module_init);
  546. module_exit(gen_module_exit);
  547. MODULE_LICENSE("GPL v2");
  548. MODULE_DESCRIPTION("General media manager for Open-Channel SSDs");