rrpc.c 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482
  1. /*
  2. * Copyright (C) 2015 IT University of Copenhagen
  3. * Initial release: Matias Bjorling <m@bjorling.me>
  4. *
  5. * This program is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU General Public License version
  7. * 2 as published by the Free Software Foundation.
  8. *
  9. * This program is distributed in the hope that it will be useful, but
  10. * WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * General Public License for more details.
  13. *
  14. * Implementation of a Round-robin page-based Hybrid FTL for Open-channel SSDs.
  15. */
  16. #include "rrpc.h"
  17. static struct kmem_cache *rrpc_gcb_cache, *rrpc_rq_cache;
  18. static DECLARE_RWSEM(rrpc_lock);
  19. static int rrpc_submit_io(struct rrpc *rrpc, struct bio *bio,
  20. struct nvm_rq *rqd, unsigned long flags);
  21. #define rrpc_for_each_lun(rrpc, rlun, i) \
  22. for ((i) = 0, rlun = &(rrpc)->luns[0]; \
  23. (i) < (rrpc)->nr_luns; (i)++, rlun = &(rrpc)->luns[(i)])
  24. static void rrpc_page_invalidate(struct rrpc *rrpc, struct rrpc_addr *a)
  25. {
  26. struct rrpc_block *rblk = a->rblk;
  27. unsigned int pg_offset;
  28. lockdep_assert_held(&rrpc->rev_lock);
  29. if (a->addr == ADDR_EMPTY || !rblk)
  30. return;
  31. spin_lock(&rblk->lock);
  32. div_u64_rem(a->addr, rrpc->dev->sec_per_blk, &pg_offset);
  33. WARN_ON(test_and_set_bit(pg_offset, rblk->invalid_pages));
  34. rblk->nr_invalid_pages++;
  35. spin_unlock(&rblk->lock);
  36. rrpc->rev_trans_map[a->addr - rrpc->poffset].addr = ADDR_EMPTY;
  37. }
  38. static void rrpc_invalidate_range(struct rrpc *rrpc, sector_t slba,
  39. unsigned int len)
  40. {
  41. sector_t i;
  42. spin_lock(&rrpc->rev_lock);
  43. for (i = slba; i < slba + len; i++) {
  44. struct rrpc_addr *gp = &rrpc->trans_map[i];
  45. rrpc_page_invalidate(rrpc, gp);
  46. gp->rblk = NULL;
  47. }
  48. spin_unlock(&rrpc->rev_lock);
  49. }
  50. static struct nvm_rq *rrpc_inflight_laddr_acquire(struct rrpc *rrpc,
  51. sector_t laddr, unsigned int pages)
  52. {
  53. struct nvm_rq *rqd;
  54. struct rrpc_inflight_rq *inf;
  55. rqd = mempool_alloc(rrpc->rq_pool, GFP_ATOMIC);
  56. if (!rqd)
  57. return ERR_PTR(-ENOMEM);
  58. inf = rrpc_get_inflight_rq(rqd);
  59. if (rrpc_lock_laddr(rrpc, laddr, pages, inf)) {
  60. mempool_free(rqd, rrpc->rq_pool);
  61. return NULL;
  62. }
  63. return rqd;
  64. }
  65. static void rrpc_inflight_laddr_release(struct rrpc *rrpc, struct nvm_rq *rqd)
  66. {
  67. struct rrpc_inflight_rq *inf = rrpc_get_inflight_rq(rqd);
  68. rrpc_unlock_laddr(rrpc, inf);
  69. mempool_free(rqd, rrpc->rq_pool);
  70. }
  71. static void rrpc_discard(struct rrpc *rrpc, struct bio *bio)
  72. {
  73. sector_t slba = bio->bi_iter.bi_sector / NR_PHY_IN_LOG;
  74. sector_t len = bio->bi_iter.bi_size / RRPC_EXPOSED_PAGE_SIZE;
  75. struct nvm_rq *rqd;
  76. while (1) {
  77. rqd = rrpc_inflight_laddr_acquire(rrpc, slba, len);
  78. if (rqd)
  79. break;
  80. schedule();
  81. }
  82. if (IS_ERR(rqd)) {
  83. pr_err("rrpc: unable to acquire inflight IO\n");
  84. bio_io_error(bio);
  85. return;
  86. }
  87. rrpc_invalidate_range(rrpc, slba, len);
  88. rrpc_inflight_laddr_release(rrpc, rqd);
  89. }
  90. static int block_is_full(struct rrpc *rrpc, struct rrpc_block *rblk)
  91. {
  92. return (rblk->next_page == rrpc->dev->sec_per_blk);
  93. }
  94. /* Calculate relative addr for the given block, considering instantiated LUNs */
  95. static u64 block_to_rel_addr(struct rrpc *rrpc, struct rrpc_block *rblk)
  96. {
  97. struct nvm_block *blk = rblk->parent;
  98. int lun_blk = blk->id % (rrpc->dev->blks_per_lun * rrpc->nr_luns);
  99. return lun_blk * rrpc->dev->sec_per_blk;
  100. }
  101. /* Calculate global addr for the given block */
  102. static u64 block_to_addr(struct rrpc *rrpc, struct rrpc_block *rblk)
  103. {
  104. struct nvm_block *blk = rblk->parent;
  105. return blk->id * rrpc->dev->sec_per_blk;
  106. }
  107. static struct ppa_addr linear_to_generic_addr(struct nvm_dev *dev,
  108. struct ppa_addr r)
  109. {
  110. struct ppa_addr l;
  111. int secs, pgs, blks, luns;
  112. sector_t ppa = r.ppa;
  113. l.ppa = 0;
  114. div_u64_rem(ppa, dev->sec_per_pg, &secs);
  115. l.g.sec = secs;
  116. sector_div(ppa, dev->sec_per_pg);
  117. div_u64_rem(ppa, dev->pgs_per_blk, &pgs);
  118. l.g.pg = pgs;
  119. sector_div(ppa, dev->pgs_per_blk);
  120. div_u64_rem(ppa, dev->blks_per_lun, &blks);
  121. l.g.blk = blks;
  122. sector_div(ppa, dev->blks_per_lun);
  123. div_u64_rem(ppa, dev->luns_per_chnl, &luns);
  124. l.g.lun = luns;
  125. sector_div(ppa, dev->luns_per_chnl);
  126. l.g.ch = ppa;
  127. return l;
  128. }
  129. static struct ppa_addr rrpc_ppa_to_gaddr(struct nvm_dev *dev, u64 addr)
  130. {
  131. struct ppa_addr paddr;
  132. paddr.ppa = addr;
  133. return linear_to_generic_addr(dev, paddr);
  134. }
  135. /* requires lun->lock taken */
  136. static void rrpc_set_lun_cur(struct rrpc_lun *rlun, struct rrpc_block *new_rblk,
  137. struct rrpc_block **cur_rblk)
  138. {
  139. struct rrpc *rrpc = rlun->rrpc;
  140. if (*cur_rblk) {
  141. spin_lock(&(*cur_rblk)->lock);
  142. WARN_ON(!block_is_full(rrpc, *cur_rblk));
  143. spin_unlock(&(*cur_rblk)->lock);
  144. }
  145. *cur_rblk = new_rblk;
  146. }
  147. static struct rrpc_block *rrpc_get_blk(struct rrpc *rrpc, struct rrpc_lun *rlun,
  148. unsigned long flags)
  149. {
  150. struct nvm_block *blk;
  151. struct rrpc_block *rblk;
  152. blk = nvm_get_blk(rrpc->dev, rlun->parent, flags);
  153. if (!blk) {
  154. pr_err("nvm: rrpc: cannot get new block from media manager\n");
  155. return NULL;
  156. }
  157. rblk = rrpc_get_rblk(rlun, blk->id);
  158. blk->priv = rblk;
  159. bitmap_zero(rblk->invalid_pages, rrpc->dev->sec_per_blk);
  160. rblk->next_page = 0;
  161. rblk->nr_invalid_pages = 0;
  162. atomic_set(&rblk->data_cmnt_size, 0);
  163. return rblk;
  164. }
  165. static void rrpc_put_blk(struct rrpc *rrpc, struct rrpc_block *rblk)
  166. {
  167. nvm_put_blk(rrpc->dev, rblk->parent);
  168. }
  169. static void rrpc_put_blks(struct rrpc *rrpc)
  170. {
  171. struct rrpc_lun *rlun;
  172. int i;
  173. for (i = 0; i < rrpc->nr_luns; i++) {
  174. rlun = &rrpc->luns[i];
  175. if (rlun->cur)
  176. rrpc_put_blk(rrpc, rlun->cur);
  177. if (rlun->gc_cur)
  178. rrpc_put_blk(rrpc, rlun->gc_cur);
  179. }
  180. }
  181. static struct rrpc_lun *get_next_lun(struct rrpc *rrpc)
  182. {
  183. int next = atomic_inc_return(&rrpc->next_lun);
  184. return &rrpc->luns[next % rrpc->nr_luns];
  185. }
  186. static void rrpc_gc_kick(struct rrpc *rrpc)
  187. {
  188. struct rrpc_lun *rlun;
  189. unsigned int i;
  190. for (i = 0; i < rrpc->nr_luns; i++) {
  191. rlun = &rrpc->luns[i];
  192. queue_work(rrpc->krqd_wq, &rlun->ws_gc);
  193. }
  194. }
  195. /*
  196. * timed GC every interval.
  197. */
  198. static void rrpc_gc_timer(unsigned long data)
  199. {
  200. struct rrpc *rrpc = (struct rrpc *)data;
  201. rrpc_gc_kick(rrpc);
  202. mod_timer(&rrpc->gc_timer, jiffies + msecs_to_jiffies(10));
  203. }
  204. static void rrpc_end_sync_bio(struct bio *bio)
  205. {
  206. struct completion *waiting = bio->bi_private;
  207. if (bio->bi_error)
  208. pr_err("nvm: gc request failed (%u).\n", bio->bi_error);
  209. complete(waiting);
  210. }
  211. /*
  212. * rrpc_move_valid_pages -- migrate live data off the block
  213. * @rrpc: the 'rrpc' structure
  214. * @block: the block from which to migrate live pages
  215. *
  216. * Description:
  217. * GC algorithms may call this function to migrate remaining live
  218. * pages off the block prior to erasing it. This function blocks
  219. * further execution until the operation is complete.
  220. */
  221. static int rrpc_move_valid_pages(struct rrpc *rrpc, struct rrpc_block *rblk)
  222. {
  223. struct request_queue *q = rrpc->dev->q;
  224. struct rrpc_rev_addr *rev;
  225. struct nvm_rq *rqd;
  226. struct bio *bio;
  227. struct page *page;
  228. int slot;
  229. int nr_sec_per_blk = rrpc->dev->sec_per_blk;
  230. u64 phys_addr;
  231. DECLARE_COMPLETION_ONSTACK(wait);
  232. if (bitmap_full(rblk->invalid_pages, nr_sec_per_blk))
  233. return 0;
  234. bio = bio_alloc(GFP_NOIO, 1);
  235. if (!bio) {
  236. pr_err("nvm: could not alloc bio to gc\n");
  237. return -ENOMEM;
  238. }
  239. page = mempool_alloc(rrpc->page_pool, GFP_NOIO);
  240. if (!page) {
  241. bio_put(bio);
  242. return -ENOMEM;
  243. }
  244. while ((slot = find_first_zero_bit(rblk->invalid_pages,
  245. nr_sec_per_blk)) < nr_sec_per_blk) {
  246. /* Lock laddr */
  247. phys_addr = rblk->parent->id * nr_sec_per_blk + slot;
  248. try:
  249. spin_lock(&rrpc->rev_lock);
  250. /* Get logical address from physical to logical table */
  251. rev = &rrpc->rev_trans_map[phys_addr - rrpc->poffset];
  252. /* already updated by previous regular write */
  253. if (rev->addr == ADDR_EMPTY) {
  254. spin_unlock(&rrpc->rev_lock);
  255. continue;
  256. }
  257. rqd = rrpc_inflight_laddr_acquire(rrpc, rev->addr, 1);
  258. if (IS_ERR_OR_NULL(rqd)) {
  259. spin_unlock(&rrpc->rev_lock);
  260. schedule();
  261. goto try;
  262. }
  263. spin_unlock(&rrpc->rev_lock);
  264. /* Perform read to do GC */
  265. bio->bi_iter.bi_sector = rrpc_get_sector(rev->addr);
  266. bio_set_op_attrs(bio, REQ_OP_READ, 0);
  267. bio->bi_private = &wait;
  268. bio->bi_end_io = rrpc_end_sync_bio;
  269. /* TODO: may fail when EXP_PG_SIZE > PAGE_SIZE */
  270. bio_add_pc_page(q, bio, page, RRPC_EXPOSED_PAGE_SIZE, 0);
  271. if (rrpc_submit_io(rrpc, bio, rqd, NVM_IOTYPE_GC)) {
  272. pr_err("rrpc: gc read failed.\n");
  273. rrpc_inflight_laddr_release(rrpc, rqd);
  274. goto finished;
  275. }
  276. wait_for_completion_io(&wait);
  277. if (bio->bi_error) {
  278. rrpc_inflight_laddr_release(rrpc, rqd);
  279. goto finished;
  280. }
  281. bio_reset(bio);
  282. reinit_completion(&wait);
  283. bio->bi_iter.bi_sector = rrpc_get_sector(rev->addr);
  284. bio_set_op_attrs(bio, REQ_OP_WRITE, 0);
  285. bio->bi_private = &wait;
  286. bio->bi_end_io = rrpc_end_sync_bio;
  287. bio_add_pc_page(q, bio, page, RRPC_EXPOSED_PAGE_SIZE, 0);
  288. /* turn the command around and write the data back to a new
  289. * address
  290. */
  291. if (rrpc_submit_io(rrpc, bio, rqd, NVM_IOTYPE_GC)) {
  292. pr_err("rrpc: gc write failed.\n");
  293. rrpc_inflight_laddr_release(rrpc, rqd);
  294. goto finished;
  295. }
  296. wait_for_completion_io(&wait);
  297. rrpc_inflight_laddr_release(rrpc, rqd);
  298. if (bio->bi_error)
  299. goto finished;
  300. bio_reset(bio);
  301. }
  302. finished:
  303. mempool_free(page, rrpc->page_pool);
  304. bio_put(bio);
  305. if (!bitmap_full(rblk->invalid_pages, nr_sec_per_blk)) {
  306. pr_err("nvm: failed to garbage collect block\n");
  307. return -EIO;
  308. }
  309. return 0;
  310. }
  311. static void rrpc_block_gc(struct work_struct *work)
  312. {
  313. struct rrpc_block_gc *gcb = container_of(work, struct rrpc_block_gc,
  314. ws_gc);
  315. struct rrpc *rrpc = gcb->rrpc;
  316. struct rrpc_block *rblk = gcb->rblk;
  317. struct rrpc_lun *rlun = rblk->rlun;
  318. struct nvm_dev *dev = rrpc->dev;
  319. mempool_free(gcb, rrpc->gcb_pool);
  320. pr_debug("nvm: block '%lu' being reclaimed\n", rblk->parent->id);
  321. if (rrpc_move_valid_pages(rrpc, rblk))
  322. goto put_back;
  323. if (nvm_erase_blk(dev, rblk->parent))
  324. goto put_back;
  325. rrpc_put_blk(rrpc, rblk);
  326. return;
  327. put_back:
  328. spin_lock(&rlun->lock);
  329. list_add_tail(&rblk->prio, &rlun->prio_list);
  330. spin_unlock(&rlun->lock);
  331. }
  332. /* the block with highest number of invalid pages, will be in the beginning
  333. * of the list
  334. */
  335. static struct rrpc_block *rblock_max_invalid(struct rrpc_block *ra,
  336. struct rrpc_block *rb)
  337. {
  338. if (ra->nr_invalid_pages == rb->nr_invalid_pages)
  339. return ra;
  340. return (ra->nr_invalid_pages < rb->nr_invalid_pages) ? rb : ra;
  341. }
  342. /* linearly find the block with highest number of invalid pages
  343. * requires lun->lock
  344. */
  345. static struct rrpc_block *block_prio_find_max(struct rrpc_lun *rlun)
  346. {
  347. struct list_head *prio_list = &rlun->prio_list;
  348. struct rrpc_block *rblock, *max;
  349. BUG_ON(list_empty(prio_list));
  350. max = list_first_entry(prio_list, struct rrpc_block, prio);
  351. list_for_each_entry(rblock, prio_list, prio)
  352. max = rblock_max_invalid(max, rblock);
  353. return max;
  354. }
  355. static void rrpc_lun_gc(struct work_struct *work)
  356. {
  357. struct rrpc_lun *rlun = container_of(work, struct rrpc_lun, ws_gc);
  358. struct rrpc *rrpc = rlun->rrpc;
  359. struct nvm_lun *lun = rlun->parent;
  360. struct rrpc_block_gc *gcb;
  361. unsigned int nr_blocks_need;
  362. nr_blocks_need = rrpc->dev->blks_per_lun / GC_LIMIT_INVERSE;
  363. if (nr_blocks_need < rrpc->nr_luns)
  364. nr_blocks_need = rrpc->nr_luns;
  365. spin_lock(&rlun->lock);
  366. while (nr_blocks_need > lun->nr_free_blocks &&
  367. !list_empty(&rlun->prio_list)) {
  368. struct rrpc_block *rblock = block_prio_find_max(rlun);
  369. struct nvm_block *block = rblock->parent;
  370. if (!rblock->nr_invalid_pages)
  371. break;
  372. gcb = mempool_alloc(rrpc->gcb_pool, GFP_ATOMIC);
  373. if (!gcb)
  374. break;
  375. list_del_init(&rblock->prio);
  376. BUG_ON(!block_is_full(rrpc, rblock));
  377. pr_debug("rrpc: selected block '%lu' for GC\n", block->id);
  378. gcb->rrpc = rrpc;
  379. gcb->rblk = rblock;
  380. INIT_WORK(&gcb->ws_gc, rrpc_block_gc);
  381. queue_work(rrpc->kgc_wq, &gcb->ws_gc);
  382. nr_blocks_need--;
  383. }
  384. spin_unlock(&rlun->lock);
  385. /* TODO: Hint that request queue can be started again */
  386. }
  387. static void rrpc_gc_queue(struct work_struct *work)
  388. {
  389. struct rrpc_block_gc *gcb = container_of(work, struct rrpc_block_gc,
  390. ws_gc);
  391. struct rrpc *rrpc = gcb->rrpc;
  392. struct rrpc_block *rblk = gcb->rblk;
  393. struct rrpc_lun *rlun = rblk->rlun;
  394. spin_lock(&rlun->lock);
  395. list_add_tail(&rblk->prio, &rlun->prio_list);
  396. spin_unlock(&rlun->lock);
  397. mempool_free(gcb, rrpc->gcb_pool);
  398. pr_debug("nvm: block '%lu' is full, allow GC (sched)\n",
  399. rblk->parent->id);
  400. }
  401. static const struct block_device_operations rrpc_fops = {
  402. .owner = THIS_MODULE,
  403. };
  404. static struct rrpc_lun *rrpc_get_lun_rr(struct rrpc *rrpc, int is_gc)
  405. {
  406. unsigned int i;
  407. struct rrpc_lun *rlun, *max_free;
  408. if (!is_gc)
  409. return get_next_lun(rrpc);
  410. /* during GC, we don't care about RR, instead we want to make
  411. * sure that we maintain evenness between the block luns.
  412. */
  413. max_free = &rrpc->luns[0];
  414. /* prevent GC-ing lun from devouring pages of a lun with
  415. * little free blocks. We don't take the lock as we only need an
  416. * estimate.
  417. */
  418. rrpc_for_each_lun(rrpc, rlun, i) {
  419. if (rlun->parent->nr_free_blocks >
  420. max_free->parent->nr_free_blocks)
  421. max_free = rlun;
  422. }
  423. return max_free;
  424. }
  425. static struct rrpc_addr *rrpc_update_map(struct rrpc *rrpc, sector_t laddr,
  426. struct rrpc_block *rblk, u64 paddr)
  427. {
  428. struct rrpc_addr *gp;
  429. struct rrpc_rev_addr *rev;
  430. BUG_ON(laddr >= rrpc->nr_sects);
  431. gp = &rrpc->trans_map[laddr];
  432. spin_lock(&rrpc->rev_lock);
  433. if (gp->rblk)
  434. rrpc_page_invalidate(rrpc, gp);
  435. gp->addr = paddr;
  436. gp->rblk = rblk;
  437. rev = &rrpc->rev_trans_map[gp->addr - rrpc->poffset];
  438. rev->addr = laddr;
  439. spin_unlock(&rrpc->rev_lock);
  440. return gp;
  441. }
  442. static u64 rrpc_alloc_addr(struct rrpc *rrpc, struct rrpc_block *rblk)
  443. {
  444. u64 addr = ADDR_EMPTY;
  445. spin_lock(&rblk->lock);
  446. if (block_is_full(rrpc, rblk))
  447. goto out;
  448. addr = block_to_addr(rrpc, rblk) + rblk->next_page;
  449. rblk->next_page++;
  450. out:
  451. spin_unlock(&rblk->lock);
  452. return addr;
  453. }
  454. /* Map logical address to a physical page. The mapping implements a round robin
  455. * approach and allocates a page from the next lun available.
  456. *
  457. * Returns rrpc_addr with the physical address and block. Returns NULL if no
  458. * blocks in the next rlun are available.
  459. */
  460. static struct rrpc_addr *rrpc_map_page(struct rrpc *rrpc, sector_t laddr,
  461. int is_gc)
  462. {
  463. struct rrpc_lun *rlun;
  464. struct rrpc_block *rblk, **cur_rblk;
  465. struct nvm_lun *lun;
  466. u64 paddr;
  467. int gc_force = 0;
  468. rlun = rrpc_get_lun_rr(rrpc, is_gc);
  469. lun = rlun->parent;
  470. if (!is_gc && lun->nr_free_blocks < rrpc->nr_luns * 4)
  471. return NULL;
  472. /*
  473. * page allocation steps:
  474. * 1. Try to allocate new page from current rblk
  475. * 2a. If succeed, proceed to map it in and return
  476. * 2b. If fail, first try to allocate a new block from media manger,
  477. * and then retry step 1. Retry until the normal block pool is
  478. * exhausted.
  479. * 3. If exhausted, and garbage collector is requesting the block,
  480. * go to the reserved block and retry step 1.
  481. * In the case that this fails as well, or it is not GC
  482. * requesting, report not able to retrieve a block and let the
  483. * caller handle further processing.
  484. */
  485. spin_lock(&rlun->lock);
  486. cur_rblk = &rlun->cur;
  487. rblk = rlun->cur;
  488. retry:
  489. paddr = rrpc_alloc_addr(rrpc, rblk);
  490. if (paddr != ADDR_EMPTY)
  491. goto done;
  492. if (!list_empty(&rlun->wblk_list)) {
  493. new_blk:
  494. rblk = list_first_entry(&rlun->wblk_list, struct rrpc_block,
  495. prio);
  496. rrpc_set_lun_cur(rlun, rblk, cur_rblk);
  497. list_del(&rblk->prio);
  498. goto retry;
  499. }
  500. spin_unlock(&rlun->lock);
  501. rblk = rrpc_get_blk(rrpc, rlun, gc_force);
  502. if (rblk) {
  503. spin_lock(&rlun->lock);
  504. list_add_tail(&rblk->prio, &rlun->wblk_list);
  505. /*
  506. * another thread might already have added a new block,
  507. * Therefore, make sure that one is used, instead of the
  508. * one just added.
  509. */
  510. goto new_blk;
  511. }
  512. if (unlikely(is_gc) && !gc_force) {
  513. /* retry from emergency gc block */
  514. cur_rblk = &rlun->gc_cur;
  515. rblk = rlun->gc_cur;
  516. gc_force = 1;
  517. spin_lock(&rlun->lock);
  518. goto retry;
  519. }
  520. pr_err("rrpc: failed to allocate new block\n");
  521. return NULL;
  522. done:
  523. spin_unlock(&rlun->lock);
  524. return rrpc_update_map(rrpc, laddr, rblk, paddr);
  525. }
  526. static void rrpc_run_gc(struct rrpc *rrpc, struct rrpc_block *rblk)
  527. {
  528. struct rrpc_block_gc *gcb;
  529. gcb = mempool_alloc(rrpc->gcb_pool, GFP_ATOMIC);
  530. if (!gcb) {
  531. pr_err("rrpc: unable to queue block for gc.");
  532. return;
  533. }
  534. gcb->rrpc = rrpc;
  535. gcb->rblk = rblk;
  536. INIT_WORK(&gcb->ws_gc, rrpc_gc_queue);
  537. queue_work(rrpc->kgc_wq, &gcb->ws_gc);
  538. }
  539. static void rrpc_end_io_write(struct rrpc *rrpc, struct rrpc_rq *rrqd,
  540. sector_t laddr, uint8_t npages)
  541. {
  542. struct rrpc_addr *p;
  543. struct rrpc_block *rblk;
  544. struct nvm_lun *lun;
  545. int cmnt_size, i;
  546. for (i = 0; i < npages; i++) {
  547. p = &rrpc->trans_map[laddr + i];
  548. rblk = p->rblk;
  549. lun = rblk->parent->lun;
  550. cmnt_size = atomic_inc_return(&rblk->data_cmnt_size);
  551. if (unlikely(cmnt_size == rrpc->dev->sec_per_blk))
  552. rrpc_run_gc(rrpc, rblk);
  553. }
  554. }
  555. static void rrpc_end_io(struct nvm_rq *rqd)
  556. {
  557. struct rrpc *rrpc = container_of(rqd->ins, struct rrpc, instance);
  558. struct rrpc_rq *rrqd = nvm_rq_to_pdu(rqd);
  559. uint8_t npages = rqd->nr_ppas;
  560. sector_t laddr = rrpc_get_laddr(rqd->bio) - npages;
  561. if (bio_data_dir(rqd->bio) == WRITE)
  562. rrpc_end_io_write(rrpc, rrqd, laddr, npages);
  563. bio_put(rqd->bio);
  564. if (rrqd->flags & NVM_IOTYPE_GC)
  565. return;
  566. rrpc_unlock_rq(rrpc, rqd);
  567. if (npages > 1)
  568. nvm_dev_dma_free(rrpc->dev, rqd->ppa_list, rqd->dma_ppa_list);
  569. mempool_free(rqd, rrpc->rq_pool);
  570. }
  571. static int rrpc_read_ppalist_rq(struct rrpc *rrpc, struct bio *bio,
  572. struct nvm_rq *rqd, unsigned long flags, int npages)
  573. {
  574. struct rrpc_inflight_rq *r = rrpc_get_inflight_rq(rqd);
  575. struct rrpc_addr *gp;
  576. sector_t laddr = rrpc_get_laddr(bio);
  577. int is_gc = flags & NVM_IOTYPE_GC;
  578. int i;
  579. if (!is_gc && rrpc_lock_rq(rrpc, bio, rqd)) {
  580. nvm_dev_dma_free(rrpc->dev, rqd->ppa_list, rqd->dma_ppa_list);
  581. return NVM_IO_REQUEUE;
  582. }
  583. for (i = 0; i < npages; i++) {
  584. /* We assume that mapping occurs at 4KB granularity */
  585. BUG_ON(!(laddr + i >= 0 && laddr + i < rrpc->nr_sects));
  586. gp = &rrpc->trans_map[laddr + i];
  587. if (gp->rblk) {
  588. rqd->ppa_list[i] = rrpc_ppa_to_gaddr(rrpc->dev,
  589. gp->addr);
  590. } else {
  591. BUG_ON(is_gc);
  592. rrpc_unlock_laddr(rrpc, r);
  593. nvm_dev_dma_free(rrpc->dev, rqd->ppa_list,
  594. rqd->dma_ppa_list);
  595. return NVM_IO_DONE;
  596. }
  597. }
  598. rqd->opcode = NVM_OP_HBREAD;
  599. return NVM_IO_OK;
  600. }
  601. static int rrpc_read_rq(struct rrpc *rrpc, struct bio *bio, struct nvm_rq *rqd,
  602. unsigned long flags)
  603. {
  604. struct rrpc_rq *rrqd = nvm_rq_to_pdu(rqd);
  605. int is_gc = flags & NVM_IOTYPE_GC;
  606. sector_t laddr = rrpc_get_laddr(bio);
  607. struct rrpc_addr *gp;
  608. if (!is_gc && rrpc_lock_rq(rrpc, bio, rqd))
  609. return NVM_IO_REQUEUE;
  610. BUG_ON(!(laddr >= 0 && laddr < rrpc->nr_sects));
  611. gp = &rrpc->trans_map[laddr];
  612. if (gp->rblk) {
  613. rqd->ppa_addr = rrpc_ppa_to_gaddr(rrpc->dev, gp->addr);
  614. } else {
  615. BUG_ON(is_gc);
  616. rrpc_unlock_rq(rrpc, rqd);
  617. return NVM_IO_DONE;
  618. }
  619. rqd->opcode = NVM_OP_HBREAD;
  620. rrqd->addr = gp;
  621. return NVM_IO_OK;
  622. }
  623. static int rrpc_write_ppalist_rq(struct rrpc *rrpc, struct bio *bio,
  624. struct nvm_rq *rqd, unsigned long flags, int npages)
  625. {
  626. struct rrpc_inflight_rq *r = rrpc_get_inflight_rq(rqd);
  627. struct rrpc_addr *p;
  628. sector_t laddr = rrpc_get_laddr(bio);
  629. int is_gc = flags & NVM_IOTYPE_GC;
  630. int i;
  631. if (!is_gc && rrpc_lock_rq(rrpc, bio, rqd)) {
  632. nvm_dev_dma_free(rrpc->dev, rqd->ppa_list, rqd->dma_ppa_list);
  633. return NVM_IO_REQUEUE;
  634. }
  635. for (i = 0; i < npages; i++) {
  636. /* We assume that mapping occurs at 4KB granularity */
  637. p = rrpc_map_page(rrpc, laddr + i, is_gc);
  638. if (!p) {
  639. BUG_ON(is_gc);
  640. rrpc_unlock_laddr(rrpc, r);
  641. nvm_dev_dma_free(rrpc->dev, rqd->ppa_list,
  642. rqd->dma_ppa_list);
  643. rrpc_gc_kick(rrpc);
  644. return NVM_IO_REQUEUE;
  645. }
  646. rqd->ppa_list[i] = rrpc_ppa_to_gaddr(rrpc->dev,
  647. p->addr);
  648. }
  649. rqd->opcode = NVM_OP_HBWRITE;
  650. return NVM_IO_OK;
  651. }
  652. static int rrpc_write_rq(struct rrpc *rrpc, struct bio *bio,
  653. struct nvm_rq *rqd, unsigned long flags)
  654. {
  655. struct rrpc_rq *rrqd = nvm_rq_to_pdu(rqd);
  656. struct rrpc_addr *p;
  657. int is_gc = flags & NVM_IOTYPE_GC;
  658. sector_t laddr = rrpc_get_laddr(bio);
  659. if (!is_gc && rrpc_lock_rq(rrpc, bio, rqd))
  660. return NVM_IO_REQUEUE;
  661. p = rrpc_map_page(rrpc, laddr, is_gc);
  662. if (!p) {
  663. BUG_ON(is_gc);
  664. rrpc_unlock_rq(rrpc, rqd);
  665. rrpc_gc_kick(rrpc);
  666. return NVM_IO_REQUEUE;
  667. }
  668. rqd->ppa_addr = rrpc_ppa_to_gaddr(rrpc->dev, p->addr);
  669. rqd->opcode = NVM_OP_HBWRITE;
  670. rrqd->addr = p;
  671. return NVM_IO_OK;
  672. }
  673. static int rrpc_setup_rq(struct rrpc *rrpc, struct bio *bio,
  674. struct nvm_rq *rqd, unsigned long flags, uint8_t npages)
  675. {
  676. if (npages > 1) {
  677. rqd->ppa_list = nvm_dev_dma_alloc(rrpc->dev, GFP_KERNEL,
  678. &rqd->dma_ppa_list);
  679. if (!rqd->ppa_list) {
  680. pr_err("rrpc: not able to allocate ppa list\n");
  681. return NVM_IO_ERR;
  682. }
  683. if (bio_op(bio) == REQ_OP_WRITE)
  684. return rrpc_write_ppalist_rq(rrpc, bio, rqd, flags,
  685. npages);
  686. return rrpc_read_ppalist_rq(rrpc, bio, rqd, flags, npages);
  687. }
  688. if (bio_op(bio) == REQ_OP_WRITE)
  689. return rrpc_write_rq(rrpc, bio, rqd, flags);
  690. return rrpc_read_rq(rrpc, bio, rqd, flags);
  691. }
  692. static int rrpc_submit_io(struct rrpc *rrpc, struct bio *bio,
  693. struct nvm_rq *rqd, unsigned long flags)
  694. {
  695. int err;
  696. struct rrpc_rq *rrq = nvm_rq_to_pdu(rqd);
  697. uint8_t nr_pages = rrpc_get_pages(bio);
  698. int bio_size = bio_sectors(bio) << 9;
  699. if (bio_size < rrpc->dev->sec_size)
  700. return NVM_IO_ERR;
  701. else if (bio_size > rrpc->dev->max_rq_size)
  702. return NVM_IO_ERR;
  703. err = rrpc_setup_rq(rrpc, bio, rqd, flags, nr_pages);
  704. if (err)
  705. return err;
  706. bio_get(bio);
  707. rqd->bio = bio;
  708. rqd->ins = &rrpc->instance;
  709. rqd->nr_ppas = nr_pages;
  710. rrq->flags = flags;
  711. err = nvm_submit_io(rrpc->dev, rqd);
  712. if (err) {
  713. pr_err("rrpc: I/O submission failed: %d\n", err);
  714. bio_put(bio);
  715. if (!(flags & NVM_IOTYPE_GC)) {
  716. rrpc_unlock_rq(rrpc, rqd);
  717. if (rqd->nr_ppas > 1)
  718. nvm_dev_dma_free(rrpc->dev,
  719. rqd->ppa_list, rqd->dma_ppa_list);
  720. }
  721. return NVM_IO_ERR;
  722. }
  723. return NVM_IO_OK;
  724. }
  725. static blk_qc_t rrpc_make_rq(struct request_queue *q, struct bio *bio)
  726. {
  727. struct rrpc *rrpc = q->queuedata;
  728. struct nvm_rq *rqd;
  729. int err;
  730. if (bio_op(bio) == REQ_OP_DISCARD) {
  731. rrpc_discard(rrpc, bio);
  732. return BLK_QC_T_NONE;
  733. }
  734. rqd = mempool_alloc(rrpc->rq_pool, GFP_KERNEL);
  735. if (!rqd) {
  736. pr_err_ratelimited("rrpc: not able to queue bio.");
  737. bio_io_error(bio);
  738. return BLK_QC_T_NONE;
  739. }
  740. memset(rqd, 0, sizeof(struct nvm_rq));
  741. err = rrpc_submit_io(rrpc, bio, rqd, NVM_IOTYPE_NONE);
  742. switch (err) {
  743. case NVM_IO_OK:
  744. return BLK_QC_T_NONE;
  745. case NVM_IO_ERR:
  746. bio_io_error(bio);
  747. break;
  748. case NVM_IO_DONE:
  749. bio_endio(bio);
  750. break;
  751. case NVM_IO_REQUEUE:
  752. spin_lock(&rrpc->bio_lock);
  753. bio_list_add(&rrpc->requeue_bios, bio);
  754. spin_unlock(&rrpc->bio_lock);
  755. queue_work(rrpc->kgc_wq, &rrpc->ws_requeue);
  756. break;
  757. }
  758. mempool_free(rqd, rrpc->rq_pool);
  759. return BLK_QC_T_NONE;
  760. }
  761. static void rrpc_requeue(struct work_struct *work)
  762. {
  763. struct rrpc *rrpc = container_of(work, struct rrpc, ws_requeue);
  764. struct bio_list bios;
  765. struct bio *bio;
  766. bio_list_init(&bios);
  767. spin_lock(&rrpc->bio_lock);
  768. bio_list_merge(&bios, &rrpc->requeue_bios);
  769. bio_list_init(&rrpc->requeue_bios);
  770. spin_unlock(&rrpc->bio_lock);
  771. while ((bio = bio_list_pop(&bios)))
  772. rrpc_make_rq(rrpc->disk->queue, bio);
  773. }
  774. static void rrpc_gc_free(struct rrpc *rrpc)
  775. {
  776. if (rrpc->krqd_wq)
  777. destroy_workqueue(rrpc->krqd_wq);
  778. if (rrpc->kgc_wq)
  779. destroy_workqueue(rrpc->kgc_wq);
  780. }
  781. static int rrpc_gc_init(struct rrpc *rrpc)
  782. {
  783. rrpc->krqd_wq = alloc_workqueue("rrpc-lun", WQ_MEM_RECLAIM|WQ_UNBOUND,
  784. rrpc->nr_luns);
  785. if (!rrpc->krqd_wq)
  786. return -ENOMEM;
  787. rrpc->kgc_wq = alloc_workqueue("rrpc-bg", WQ_MEM_RECLAIM, 1);
  788. if (!rrpc->kgc_wq)
  789. return -ENOMEM;
  790. setup_timer(&rrpc->gc_timer, rrpc_gc_timer, (unsigned long)rrpc);
  791. return 0;
  792. }
  793. static void rrpc_map_free(struct rrpc *rrpc)
  794. {
  795. vfree(rrpc->rev_trans_map);
  796. vfree(rrpc->trans_map);
  797. }
  798. static int rrpc_l2p_update(u64 slba, u32 nlb, __le64 *entries, void *private)
  799. {
  800. struct rrpc *rrpc = (struct rrpc *)private;
  801. struct nvm_dev *dev = rrpc->dev;
  802. struct rrpc_addr *addr = rrpc->trans_map + slba;
  803. struct rrpc_rev_addr *raddr = rrpc->rev_trans_map;
  804. u64 elba = slba + nlb;
  805. u64 i;
  806. if (unlikely(elba > dev->total_secs)) {
  807. pr_err("nvm: L2P data from device is out of bounds!\n");
  808. return -EINVAL;
  809. }
  810. for (i = 0; i < nlb; i++) {
  811. u64 pba = le64_to_cpu(entries[i]);
  812. unsigned int mod;
  813. /* LNVM treats address-spaces as silos, LBA and PBA are
  814. * equally large and zero-indexed.
  815. */
  816. if (unlikely(pba >= dev->total_secs && pba != U64_MAX)) {
  817. pr_err("nvm: L2P data entry is out of bounds!\n");
  818. return -EINVAL;
  819. }
  820. /* Address zero is a special one. The first page on a disk is
  821. * protected. As it often holds internal device boot
  822. * information.
  823. */
  824. if (!pba)
  825. continue;
  826. div_u64_rem(pba, rrpc->nr_sects, &mod);
  827. addr[i].addr = pba;
  828. raddr[mod].addr = slba + i;
  829. }
  830. return 0;
  831. }
  832. static int rrpc_map_init(struct rrpc *rrpc)
  833. {
  834. struct nvm_dev *dev = rrpc->dev;
  835. sector_t i;
  836. int ret;
  837. rrpc->trans_map = vzalloc(sizeof(struct rrpc_addr) * rrpc->nr_sects);
  838. if (!rrpc->trans_map)
  839. return -ENOMEM;
  840. rrpc->rev_trans_map = vmalloc(sizeof(struct rrpc_rev_addr)
  841. * rrpc->nr_sects);
  842. if (!rrpc->rev_trans_map)
  843. return -ENOMEM;
  844. for (i = 0; i < rrpc->nr_sects; i++) {
  845. struct rrpc_addr *p = &rrpc->trans_map[i];
  846. struct rrpc_rev_addr *r = &rrpc->rev_trans_map[i];
  847. p->addr = ADDR_EMPTY;
  848. r->addr = ADDR_EMPTY;
  849. }
  850. if (!dev->ops->get_l2p_tbl)
  851. return 0;
  852. /* Bring up the mapping table from device */
  853. ret = dev->ops->get_l2p_tbl(dev, rrpc->soffset, rrpc->nr_sects,
  854. rrpc_l2p_update, rrpc);
  855. if (ret) {
  856. pr_err("nvm: rrpc: could not read L2P table.\n");
  857. return -EINVAL;
  858. }
  859. return 0;
  860. }
  861. /* Minimum pages needed within a lun */
  862. #define PAGE_POOL_SIZE 16
  863. #define ADDR_POOL_SIZE 64
  864. static int rrpc_core_init(struct rrpc *rrpc)
  865. {
  866. down_write(&rrpc_lock);
  867. if (!rrpc_gcb_cache) {
  868. rrpc_gcb_cache = kmem_cache_create("rrpc_gcb",
  869. sizeof(struct rrpc_block_gc), 0, 0, NULL);
  870. if (!rrpc_gcb_cache) {
  871. up_write(&rrpc_lock);
  872. return -ENOMEM;
  873. }
  874. rrpc_rq_cache = kmem_cache_create("rrpc_rq",
  875. sizeof(struct nvm_rq) + sizeof(struct rrpc_rq),
  876. 0, 0, NULL);
  877. if (!rrpc_rq_cache) {
  878. kmem_cache_destroy(rrpc_gcb_cache);
  879. up_write(&rrpc_lock);
  880. return -ENOMEM;
  881. }
  882. }
  883. up_write(&rrpc_lock);
  884. rrpc->page_pool = mempool_create_page_pool(PAGE_POOL_SIZE, 0);
  885. if (!rrpc->page_pool)
  886. return -ENOMEM;
  887. rrpc->gcb_pool = mempool_create_slab_pool(rrpc->dev->nr_luns,
  888. rrpc_gcb_cache);
  889. if (!rrpc->gcb_pool)
  890. return -ENOMEM;
  891. rrpc->rq_pool = mempool_create_slab_pool(64, rrpc_rq_cache);
  892. if (!rrpc->rq_pool)
  893. return -ENOMEM;
  894. spin_lock_init(&rrpc->inflights.lock);
  895. INIT_LIST_HEAD(&rrpc->inflights.reqs);
  896. return 0;
  897. }
  898. static void rrpc_core_free(struct rrpc *rrpc)
  899. {
  900. mempool_destroy(rrpc->page_pool);
  901. mempool_destroy(rrpc->gcb_pool);
  902. mempool_destroy(rrpc->rq_pool);
  903. }
  904. static void rrpc_luns_free(struct rrpc *rrpc)
  905. {
  906. struct nvm_dev *dev = rrpc->dev;
  907. struct nvm_lun *lun;
  908. struct rrpc_lun *rlun;
  909. int i;
  910. if (!rrpc->luns)
  911. return;
  912. for (i = 0; i < rrpc->nr_luns; i++) {
  913. rlun = &rrpc->luns[i];
  914. lun = rlun->parent;
  915. if (!lun)
  916. break;
  917. dev->mt->release_lun(dev, lun->id);
  918. vfree(rlun->blocks);
  919. }
  920. kfree(rrpc->luns);
  921. }
  922. static int rrpc_luns_init(struct rrpc *rrpc, int lun_begin, int lun_end)
  923. {
  924. struct nvm_dev *dev = rrpc->dev;
  925. struct rrpc_lun *rlun;
  926. int i, j, ret = -EINVAL;
  927. if (dev->sec_per_blk > MAX_INVALID_PAGES_STORAGE * BITS_PER_LONG) {
  928. pr_err("rrpc: number of pages per block too high.");
  929. return -EINVAL;
  930. }
  931. spin_lock_init(&rrpc->rev_lock);
  932. rrpc->luns = kcalloc(rrpc->nr_luns, sizeof(struct rrpc_lun),
  933. GFP_KERNEL);
  934. if (!rrpc->luns)
  935. return -ENOMEM;
  936. /* 1:1 mapping */
  937. for (i = 0; i < rrpc->nr_luns; i++) {
  938. int lunid = lun_begin + i;
  939. struct nvm_lun *lun;
  940. if (dev->mt->reserve_lun(dev, lunid)) {
  941. pr_err("rrpc: lun %u is already allocated\n", lunid);
  942. goto err;
  943. }
  944. lun = dev->mt->get_lun(dev, lunid);
  945. if (!lun)
  946. goto err;
  947. rlun = &rrpc->luns[i];
  948. rlun->parent = lun;
  949. rlun->blocks = vzalloc(sizeof(struct rrpc_block) *
  950. rrpc->dev->blks_per_lun);
  951. if (!rlun->blocks) {
  952. ret = -ENOMEM;
  953. goto err;
  954. }
  955. for (j = 0; j < rrpc->dev->blks_per_lun; j++) {
  956. struct rrpc_block *rblk = &rlun->blocks[j];
  957. struct nvm_block *blk = &lun->blocks[j];
  958. rblk->parent = blk;
  959. rblk->rlun = rlun;
  960. INIT_LIST_HEAD(&rblk->prio);
  961. spin_lock_init(&rblk->lock);
  962. }
  963. rlun->rrpc = rrpc;
  964. INIT_LIST_HEAD(&rlun->prio_list);
  965. INIT_LIST_HEAD(&rlun->wblk_list);
  966. INIT_WORK(&rlun->ws_gc, rrpc_lun_gc);
  967. spin_lock_init(&rlun->lock);
  968. }
  969. return 0;
  970. err:
  971. return ret;
  972. }
  973. /* returns 0 on success and stores the beginning address in *begin */
  974. static int rrpc_area_init(struct rrpc *rrpc, sector_t *begin)
  975. {
  976. struct nvm_dev *dev = rrpc->dev;
  977. struct nvmm_type *mt = dev->mt;
  978. sector_t size = rrpc->nr_sects * dev->sec_size;
  979. int ret;
  980. size >>= 9;
  981. ret = mt->get_area(dev, begin, size);
  982. if (!ret)
  983. *begin >>= (ilog2(dev->sec_size) - 9);
  984. return ret;
  985. }
  986. static void rrpc_area_free(struct rrpc *rrpc)
  987. {
  988. struct nvm_dev *dev = rrpc->dev;
  989. struct nvmm_type *mt = dev->mt;
  990. sector_t begin = rrpc->soffset << (ilog2(dev->sec_size) - 9);
  991. mt->put_area(dev, begin);
  992. }
  993. static void rrpc_free(struct rrpc *rrpc)
  994. {
  995. rrpc_gc_free(rrpc);
  996. rrpc_map_free(rrpc);
  997. rrpc_core_free(rrpc);
  998. rrpc_luns_free(rrpc);
  999. rrpc_area_free(rrpc);
  1000. kfree(rrpc);
  1001. }
  1002. static void rrpc_exit(void *private)
  1003. {
  1004. struct rrpc *rrpc = private;
  1005. del_timer(&rrpc->gc_timer);
  1006. flush_workqueue(rrpc->krqd_wq);
  1007. flush_workqueue(rrpc->kgc_wq);
  1008. rrpc_free(rrpc);
  1009. }
  1010. static sector_t rrpc_capacity(void *private)
  1011. {
  1012. struct rrpc *rrpc = private;
  1013. struct nvm_dev *dev = rrpc->dev;
  1014. sector_t reserved, provisioned;
  1015. /* cur, gc, and two emergency blocks for each lun */
  1016. reserved = rrpc->nr_luns * dev->sec_per_blk * 4;
  1017. provisioned = rrpc->nr_sects - reserved;
  1018. if (reserved > rrpc->nr_sects) {
  1019. pr_err("rrpc: not enough space available to expose storage.\n");
  1020. return 0;
  1021. }
  1022. sector_div(provisioned, 10);
  1023. return provisioned * 9 * NR_PHY_IN_LOG;
  1024. }
  1025. /*
  1026. * Looks up the logical address from reverse trans map and check if its valid by
  1027. * comparing the logical to physical address with the physical address.
  1028. * Returns 0 on free, otherwise 1 if in use
  1029. */
  1030. static void rrpc_block_map_update(struct rrpc *rrpc, struct rrpc_block *rblk)
  1031. {
  1032. struct nvm_dev *dev = rrpc->dev;
  1033. int offset;
  1034. struct rrpc_addr *laddr;
  1035. u64 bpaddr, paddr, pladdr;
  1036. bpaddr = block_to_rel_addr(rrpc, rblk);
  1037. for (offset = 0; offset < dev->sec_per_blk; offset++) {
  1038. paddr = bpaddr + offset;
  1039. pladdr = rrpc->rev_trans_map[paddr].addr;
  1040. if (pladdr == ADDR_EMPTY)
  1041. continue;
  1042. laddr = &rrpc->trans_map[pladdr];
  1043. if (paddr == laddr->addr) {
  1044. laddr->rblk = rblk;
  1045. } else {
  1046. set_bit(offset, rblk->invalid_pages);
  1047. rblk->nr_invalid_pages++;
  1048. }
  1049. }
  1050. }
  1051. static int rrpc_blocks_init(struct rrpc *rrpc)
  1052. {
  1053. struct rrpc_lun *rlun;
  1054. struct rrpc_block *rblk;
  1055. int lun_iter, blk_iter;
  1056. for (lun_iter = 0; lun_iter < rrpc->nr_luns; lun_iter++) {
  1057. rlun = &rrpc->luns[lun_iter];
  1058. for (blk_iter = 0; blk_iter < rrpc->dev->blks_per_lun;
  1059. blk_iter++) {
  1060. rblk = &rlun->blocks[blk_iter];
  1061. rrpc_block_map_update(rrpc, rblk);
  1062. }
  1063. }
  1064. return 0;
  1065. }
  1066. static int rrpc_luns_configure(struct rrpc *rrpc)
  1067. {
  1068. struct rrpc_lun *rlun;
  1069. struct rrpc_block *rblk;
  1070. int i;
  1071. for (i = 0; i < rrpc->nr_luns; i++) {
  1072. rlun = &rrpc->luns[i];
  1073. rblk = rrpc_get_blk(rrpc, rlun, 0);
  1074. if (!rblk)
  1075. goto err;
  1076. rrpc_set_lun_cur(rlun, rblk, &rlun->cur);
  1077. /* Emergency gc block */
  1078. rblk = rrpc_get_blk(rrpc, rlun, 1);
  1079. if (!rblk)
  1080. goto err;
  1081. rrpc_set_lun_cur(rlun, rblk, &rlun->gc_cur);
  1082. }
  1083. return 0;
  1084. err:
  1085. rrpc_put_blks(rrpc);
  1086. return -EINVAL;
  1087. }
  1088. static struct nvm_tgt_type tt_rrpc;
  1089. static void *rrpc_init(struct nvm_dev *dev, struct gendisk *tdisk,
  1090. int lun_begin, int lun_end)
  1091. {
  1092. struct request_queue *bqueue = dev->q;
  1093. struct request_queue *tqueue = tdisk->queue;
  1094. struct rrpc *rrpc;
  1095. sector_t soffset;
  1096. int ret;
  1097. if (!(dev->identity.dom & NVM_RSP_L2P)) {
  1098. pr_err("nvm: rrpc: device does not support l2p (%x)\n",
  1099. dev->identity.dom);
  1100. return ERR_PTR(-EINVAL);
  1101. }
  1102. rrpc = kzalloc(sizeof(struct rrpc), GFP_KERNEL);
  1103. if (!rrpc)
  1104. return ERR_PTR(-ENOMEM);
  1105. rrpc->instance.tt = &tt_rrpc;
  1106. rrpc->dev = dev;
  1107. rrpc->disk = tdisk;
  1108. bio_list_init(&rrpc->requeue_bios);
  1109. spin_lock_init(&rrpc->bio_lock);
  1110. INIT_WORK(&rrpc->ws_requeue, rrpc_requeue);
  1111. rrpc->nr_luns = lun_end - lun_begin + 1;
  1112. rrpc->total_blocks = (unsigned long)dev->blks_per_lun * rrpc->nr_luns;
  1113. rrpc->nr_sects = (unsigned long long)dev->sec_per_lun * rrpc->nr_luns;
  1114. /* simple round-robin strategy */
  1115. atomic_set(&rrpc->next_lun, -1);
  1116. ret = rrpc_area_init(rrpc, &soffset);
  1117. if (ret < 0) {
  1118. pr_err("nvm: rrpc: could not initialize area\n");
  1119. return ERR_PTR(ret);
  1120. }
  1121. rrpc->soffset = soffset;
  1122. ret = rrpc_luns_init(rrpc, lun_begin, lun_end);
  1123. if (ret) {
  1124. pr_err("nvm: rrpc: could not initialize luns\n");
  1125. goto err;
  1126. }
  1127. rrpc->poffset = dev->sec_per_lun * lun_begin;
  1128. rrpc->lun_offset = lun_begin;
  1129. ret = rrpc_core_init(rrpc);
  1130. if (ret) {
  1131. pr_err("nvm: rrpc: could not initialize core\n");
  1132. goto err;
  1133. }
  1134. ret = rrpc_map_init(rrpc);
  1135. if (ret) {
  1136. pr_err("nvm: rrpc: could not initialize maps\n");
  1137. goto err;
  1138. }
  1139. ret = rrpc_blocks_init(rrpc);
  1140. if (ret) {
  1141. pr_err("nvm: rrpc: could not initialize state for blocks\n");
  1142. goto err;
  1143. }
  1144. ret = rrpc_luns_configure(rrpc);
  1145. if (ret) {
  1146. pr_err("nvm: rrpc: not enough blocks available in LUNs.\n");
  1147. goto err;
  1148. }
  1149. ret = rrpc_gc_init(rrpc);
  1150. if (ret) {
  1151. pr_err("nvm: rrpc: could not initialize gc\n");
  1152. goto err;
  1153. }
  1154. /* inherit the size from the underlying device */
  1155. blk_queue_logical_block_size(tqueue, queue_physical_block_size(bqueue));
  1156. blk_queue_max_hw_sectors(tqueue, queue_max_hw_sectors(bqueue));
  1157. pr_info("nvm: rrpc initialized with %u luns and %llu pages.\n",
  1158. rrpc->nr_luns, (unsigned long long)rrpc->nr_sects);
  1159. mod_timer(&rrpc->gc_timer, jiffies + msecs_to_jiffies(10));
  1160. return rrpc;
  1161. err:
  1162. rrpc_free(rrpc);
  1163. return ERR_PTR(ret);
  1164. }
  1165. /* round robin, page-based FTL, and cost-based GC */
  1166. static struct nvm_tgt_type tt_rrpc = {
  1167. .name = "rrpc",
  1168. .version = {1, 0, 0},
  1169. .make_rq = rrpc_make_rq,
  1170. .capacity = rrpc_capacity,
  1171. .end_io = rrpc_end_io,
  1172. .init = rrpc_init,
  1173. .exit = rrpc_exit,
  1174. };
  1175. static int __init rrpc_module_init(void)
  1176. {
  1177. return nvm_register_tgt_type(&tt_rrpc);
  1178. }
  1179. static void rrpc_module_exit(void)
  1180. {
  1181. nvm_unregister_tgt_type(&tt_rrpc);
  1182. }
  1183. module_init(rrpc_module_init);
  1184. module_exit(rrpc_module_exit);
  1185. MODULE_LICENSE("GPL v2");
  1186. MODULE_DESCRIPTION("Block-Device Target for Open-Channel SSDs");