pblk-init.c 27 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120
  1. /*
  2. * Copyright (C) 2015 IT University of Copenhagen (rrpc.c)
  3. * Copyright (C) 2016 CNEX Labs
  4. * Initial release: Javier Gonzalez <javier@cnexlabs.com>
  5. * Matias Bjorling <matias@cnexlabs.com>
  6. *
  7. * This program is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU General Public License version
  9. * 2 as published by the Free Software Foundation.
  10. *
  11. * This program is distributed in the hope that it will be useful, but
  12. * WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * General Public License for more details.
  15. *
  16. * Implementation of a physical block-device target for Open-channel SSDs.
  17. *
  18. * pblk-init.c - pblk's initialization.
  19. */
  20. #include "pblk.h"
  21. static struct kmem_cache *pblk_ws_cache, *pblk_rec_cache, *pblk_g_rq_cache,
  22. *pblk_w_rq_cache;
  23. static DECLARE_RWSEM(pblk_lock);
  24. struct bio_set *pblk_bio_set;
  25. static int pblk_rw_io(struct request_queue *q, struct pblk *pblk,
  26. struct bio *bio)
  27. {
  28. int ret;
  29. /* Read requests must be <= 256kb due to NVMe's 64 bit completion bitmap
  30. * constraint. Writes can be of arbitrary size.
  31. */
  32. if (bio_data_dir(bio) == READ) {
  33. blk_queue_split(q, &bio);
  34. ret = pblk_submit_read(pblk, bio);
  35. if (ret == NVM_IO_DONE && bio_flagged(bio, BIO_CLONED))
  36. bio_put(bio);
  37. return ret;
  38. }
  39. /* Prevent deadlock in the case of a modest LUN configuration and large
  40. * user I/Os. Unless stalled, the rate limiter leaves at least 256KB
  41. * available for user I/O.
  42. */
  43. if (pblk_get_secs(bio) > pblk_rl_max_io(&pblk->rl))
  44. blk_queue_split(q, &bio);
  45. return pblk_write_to_cache(pblk, bio, PBLK_IOTYPE_USER);
  46. }
  47. static blk_qc_t pblk_make_rq(struct request_queue *q, struct bio *bio)
  48. {
  49. struct pblk *pblk = q->queuedata;
  50. if (bio_op(bio) == REQ_OP_DISCARD) {
  51. pblk_discard(pblk, bio);
  52. if (!(bio->bi_opf & REQ_PREFLUSH)) {
  53. bio_endio(bio);
  54. return BLK_QC_T_NONE;
  55. }
  56. }
  57. switch (pblk_rw_io(q, pblk, bio)) {
  58. case NVM_IO_ERR:
  59. bio_io_error(bio);
  60. break;
  61. case NVM_IO_DONE:
  62. bio_endio(bio);
  63. break;
  64. }
  65. return BLK_QC_T_NONE;
  66. }
  67. static size_t pblk_trans_map_size(struct pblk *pblk)
  68. {
  69. int entry_size = 8;
  70. if (pblk->ppaf_bitsize < 32)
  71. entry_size = 4;
  72. return entry_size * pblk->rl.nr_secs;
  73. }
  74. #ifdef CONFIG_NVM_DEBUG
  75. static u32 pblk_l2p_crc(struct pblk *pblk)
  76. {
  77. size_t map_size;
  78. u32 crc = ~(u32)0;
  79. map_size = pblk_trans_map_size(pblk);
  80. crc = crc32_le(crc, pblk->trans_map, map_size);
  81. return crc;
  82. }
  83. #endif
  84. static void pblk_l2p_free(struct pblk *pblk)
  85. {
  86. vfree(pblk->trans_map);
  87. }
  88. static int pblk_l2p_init(struct pblk *pblk)
  89. {
  90. sector_t i;
  91. struct ppa_addr ppa;
  92. size_t map_size;
  93. map_size = pblk_trans_map_size(pblk);
  94. pblk->trans_map = vmalloc(map_size);
  95. if (!pblk->trans_map)
  96. return -ENOMEM;
  97. pblk_ppa_set_empty(&ppa);
  98. for (i = 0; i < pblk->rl.nr_secs; i++)
  99. pblk_trans_map_set(pblk, i, ppa);
  100. return 0;
  101. }
  102. static void pblk_rwb_free(struct pblk *pblk)
  103. {
  104. if (pblk_rb_tear_down_check(&pblk->rwb))
  105. pr_err("pblk: write buffer error on tear down\n");
  106. pblk_rb_data_free(&pblk->rwb);
  107. vfree(pblk_rb_entries_ref(&pblk->rwb));
  108. }
  109. static int pblk_rwb_init(struct pblk *pblk)
  110. {
  111. struct nvm_tgt_dev *dev = pblk->dev;
  112. struct nvm_geo *geo = &dev->geo;
  113. struct pblk_rb_entry *entries;
  114. unsigned long nr_entries;
  115. unsigned int power_size, power_seg_sz;
  116. nr_entries = pblk_rb_calculate_size(pblk->pgs_in_buffer);
  117. entries = vzalloc(nr_entries * sizeof(struct pblk_rb_entry));
  118. if (!entries)
  119. return -ENOMEM;
  120. power_size = get_count_order(nr_entries);
  121. power_seg_sz = get_count_order(geo->sec_size);
  122. return pblk_rb_init(&pblk->rwb, entries, power_size, power_seg_sz);
  123. }
  124. /* Minimum pages needed within a lun */
  125. #define ADDR_POOL_SIZE 64
  126. static int pblk_set_ppaf(struct pblk *pblk)
  127. {
  128. struct nvm_tgt_dev *dev = pblk->dev;
  129. struct nvm_geo *geo = &dev->geo;
  130. struct nvm_addr_format ppaf = geo->ppaf;
  131. int power_len;
  132. /* Re-calculate channel and lun format to adapt to configuration */
  133. power_len = get_count_order(geo->nr_chnls);
  134. if (1 << power_len != geo->nr_chnls) {
  135. pr_err("pblk: supports only power-of-two channel config.\n");
  136. return -EINVAL;
  137. }
  138. ppaf.ch_len = power_len;
  139. power_len = get_count_order(geo->nr_luns);
  140. if (1 << power_len != geo->nr_luns) {
  141. pr_err("pblk: supports only power-of-two LUN config.\n");
  142. return -EINVAL;
  143. }
  144. ppaf.lun_len = power_len;
  145. pblk->ppaf.sec_offset = 0;
  146. pblk->ppaf.pln_offset = ppaf.sect_len;
  147. pblk->ppaf.ch_offset = pblk->ppaf.pln_offset + ppaf.pln_len;
  148. pblk->ppaf.lun_offset = pblk->ppaf.ch_offset + ppaf.ch_len;
  149. pblk->ppaf.pg_offset = pblk->ppaf.lun_offset + ppaf.lun_len;
  150. pblk->ppaf.blk_offset = pblk->ppaf.pg_offset + ppaf.pg_len;
  151. pblk->ppaf.sec_mask = (1ULL << ppaf.sect_len) - 1;
  152. pblk->ppaf.pln_mask = ((1ULL << ppaf.pln_len) - 1) <<
  153. pblk->ppaf.pln_offset;
  154. pblk->ppaf.ch_mask = ((1ULL << ppaf.ch_len) - 1) <<
  155. pblk->ppaf.ch_offset;
  156. pblk->ppaf.lun_mask = ((1ULL << ppaf.lun_len) - 1) <<
  157. pblk->ppaf.lun_offset;
  158. pblk->ppaf.pg_mask = ((1ULL << ppaf.pg_len) - 1) <<
  159. pblk->ppaf.pg_offset;
  160. pblk->ppaf.blk_mask = ((1ULL << ppaf.blk_len) - 1) <<
  161. pblk->ppaf.blk_offset;
  162. pblk->ppaf_bitsize = pblk->ppaf.blk_offset + ppaf.blk_len;
  163. return 0;
  164. }
  165. static int pblk_init_global_caches(struct pblk *pblk)
  166. {
  167. down_write(&pblk_lock);
  168. pblk_ws_cache = kmem_cache_create("pblk_blk_ws",
  169. sizeof(struct pblk_line_ws), 0, 0, NULL);
  170. if (!pblk_ws_cache) {
  171. up_write(&pblk_lock);
  172. return -ENOMEM;
  173. }
  174. pblk_rec_cache = kmem_cache_create("pblk_rec",
  175. sizeof(struct pblk_rec_ctx), 0, 0, NULL);
  176. if (!pblk_rec_cache) {
  177. kmem_cache_destroy(pblk_ws_cache);
  178. up_write(&pblk_lock);
  179. return -ENOMEM;
  180. }
  181. pblk_g_rq_cache = kmem_cache_create("pblk_g_rq", pblk_g_rq_size,
  182. 0, 0, NULL);
  183. if (!pblk_g_rq_cache) {
  184. kmem_cache_destroy(pblk_ws_cache);
  185. kmem_cache_destroy(pblk_rec_cache);
  186. up_write(&pblk_lock);
  187. return -ENOMEM;
  188. }
  189. pblk_w_rq_cache = kmem_cache_create("pblk_w_rq", pblk_w_rq_size,
  190. 0, 0, NULL);
  191. if (!pblk_w_rq_cache) {
  192. kmem_cache_destroy(pblk_ws_cache);
  193. kmem_cache_destroy(pblk_rec_cache);
  194. kmem_cache_destroy(pblk_g_rq_cache);
  195. up_write(&pblk_lock);
  196. return -ENOMEM;
  197. }
  198. up_write(&pblk_lock);
  199. return 0;
  200. }
  201. static void pblk_free_global_caches(struct pblk *pblk)
  202. {
  203. kmem_cache_destroy(pblk_ws_cache);
  204. kmem_cache_destroy(pblk_rec_cache);
  205. kmem_cache_destroy(pblk_g_rq_cache);
  206. kmem_cache_destroy(pblk_w_rq_cache);
  207. }
  208. static int pblk_core_init(struct pblk *pblk)
  209. {
  210. struct nvm_tgt_dev *dev = pblk->dev;
  211. struct nvm_geo *geo = &dev->geo;
  212. pblk->pgs_in_buffer = NVM_MEM_PAGE_WRITE * geo->sec_per_pg *
  213. geo->nr_planes * geo->all_luns;
  214. if (pblk_init_global_caches(pblk))
  215. return -ENOMEM;
  216. /* Internal bios can be at most the sectors signaled by the device. */
  217. pblk->page_bio_pool = mempool_create_page_pool(nvm_max_phys_sects(dev),
  218. 0);
  219. if (!pblk->page_bio_pool)
  220. goto free_global_caches;
  221. pblk->gen_ws_pool = mempool_create_slab_pool(PBLK_GEN_WS_POOL_SIZE,
  222. pblk_ws_cache);
  223. if (!pblk->gen_ws_pool)
  224. goto free_page_bio_pool;
  225. pblk->rec_pool = mempool_create_slab_pool(geo->all_luns,
  226. pblk_rec_cache);
  227. if (!pblk->rec_pool)
  228. goto free_gen_ws_pool;
  229. pblk->r_rq_pool = mempool_create_slab_pool(geo->all_luns,
  230. pblk_g_rq_cache);
  231. if (!pblk->r_rq_pool)
  232. goto free_rec_pool;
  233. pblk->e_rq_pool = mempool_create_slab_pool(geo->all_luns,
  234. pblk_g_rq_cache);
  235. if (!pblk->e_rq_pool)
  236. goto free_r_rq_pool;
  237. pblk->w_rq_pool = mempool_create_slab_pool(geo->all_luns,
  238. pblk_w_rq_cache);
  239. if (!pblk->w_rq_pool)
  240. goto free_e_rq_pool;
  241. pblk->close_wq = alloc_workqueue("pblk-close-wq",
  242. WQ_MEM_RECLAIM | WQ_UNBOUND, PBLK_NR_CLOSE_JOBS);
  243. if (!pblk->close_wq)
  244. goto free_w_rq_pool;
  245. pblk->bb_wq = alloc_workqueue("pblk-bb-wq",
  246. WQ_MEM_RECLAIM | WQ_UNBOUND, 0);
  247. if (!pblk->bb_wq)
  248. goto free_close_wq;
  249. pblk->r_end_wq = alloc_workqueue("pblk-read-end-wq",
  250. WQ_MEM_RECLAIM | WQ_UNBOUND, 0);
  251. if (!pblk->r_end_wq)
  252. goto free_bb_wq;
  253. if (pblk_set_ppaf(pblk))
  254. goto free_r_end_wq;
  255. if (pblk_rwb_init(pblk))
  256. goto free_r_end_wq;
  257. INIT_LIST_HEAD(&pblk->compl_list);
  258. return 0;
  259. free_r_end_wq:
  260. destroy_workqueue(pblk->r_end_wq);
  261. free_bb_wq:
  262. destroy_workqueue(pblk->bb_wq);
  263. free_close_wq:
  264. destroy_workqueue(pblk->close_wq);
  265. free_w_rq_pool:
  266. mempool_destroy(pblk->w_rq_pool);
  267. free_e_rq_pool:
  268. mempool_destroy(pblk->e_rq_pool);
  269. free_r_rq_pool:
  270. mempool_destroy(pblk->r_rq_pool);
  271. free_rec_pool:
  272. mempool_destroy(pblk->rec_pool);
  273. free_gen_ws_pool:
  274. mempool_destroy(pblk->gen_ws_pool);
  275. free_page_bio_pool:
  276. mempool_destroy(pblk->page_bio_pool);
  277. free_global_caches:
  278. pblk_free_global_caches(pblk);
  279. return -ENOMEM;
  280. }
  281. static void pblk_core_free(struct pblk *pblk)
  282. {
  283. if (pblk->close_wq)
  284. destroy_workqueue(pblk->close_wq);
  285. if (pblk->r_end_wq)
  286. destroy_workqueue(pblk->r_end_wq);
  287. if (pblk->bb_wq)
  288. destroy_workqueue(pblk->bb_wq);
  289. mempool_destroy(pblk->page_bio_pool);
  290. mempool_destroy(pblk->gen_ws_pool);
  291. mempool_destroy(pblk->rec_pool);
  292. mempool_destroy(pblk->r_rq_pool);
  293. mempool_destroy(pblk->e_rq_pool);
  294. mempool_destroy(pblk->w_rq_pool);
  295. pblk_free_global_caches(pblk);
  296. }
  297. static void pblk_luns_free(struct pblk *pblk)
  298. {
  299. kfree(pblk->luns);
  300. }
  301. static void pblk_free_line_bitmaps(struct pblk_line *line)
  302. {
  303. kfree(line->blk_bitmap);
  304. kfree(line->erase_bitmap);
  305. }
  306. static void pblk_lines_free(struct pblk *pblk)
  307. {
  308. struct pblk_line_mgmt *l_mg = &pblk->l_mg;
  309. struct pblk_line *line;
  310. int i;
  311. spin_lock(&l_mg->free_lock);
  312. for (i = 0; i < l_mg->nr_lines; i++) {
  313. line = &pblk->lines[i];
  314. pblk_line_free(pblk, line);
  315. pblk_free_line_bitmaps(line);
  316. }
  317. spin_unlock(&l_mg->free_lock);
  318. }
  319. static void pblk_line_meta_free(struct pblk *pblk)
  320. {
  321. struct pblk_line_mgmt *l_mg = &pblk->l_mg;
  322. int i;
  323. kfree(l_mg->bb_template);
  324. kfree(l_mg->bb_aux);
  325. kfree(l_mg->vsc_list);
  326. for (i = 0; i < PBLK_DATA_LINES; i++) {
  327. kfree(l_mg->sline_meta[i]);
  328. pblk_mfree(l_mg->eline_meta[i]->buf, l_mg->emeta_alloc_type);
  329. kfree(l_mg->eline_meta[i]);
  330. }
  331. kfree(pblk->lines);
  332. }
  333. static int pblk_bb_discovery(struct nvm_tgt_dev *dev, struct pblk_lun *rlun)
  334. {
  335. struct nvm_geo *geo = &dev->geo;
  336. struct ppa_addr ppa;
  337. u8 *blks;
  338. int nr_blks, ret;
  339. nr_blks = geo->nr_chks * geo->plane_mode;
  340. blks = kmalloc(nr_blks, GFP_KERNEL);
  341. if (!blks)
  342. return -ENOMEM;
  343. ppa.ppa = 0;
  344. ppa.g.ch = rlun->bppa.g.ch;
  345. ppa.g.lun = rlun->bppa.g.lun;
  346. ret = nvm_get_tgt_bb_tbl(dev, ppa, blks);
  347. if (ret)
  348. goto out;
  349. nr_blks = nvm_bb_tbl_fold(dev->parent, blks, nr_blks);
  350. if (nr_blks < 0) {
  351. ret = nr_blks;
  352. goto out;
  353. }
  354. rlun->bb_list = blks;
  355. return 0;
  356. out:
  357. kfree(blks);
  358. return ret;
  359. }
  360. static int pblk_bb_line(struct pblk *pblk, struct pblk_line *line,
  361. int blk_per_line)
  362. {
  363. struct nvm_tgt_dev *dev = pblk->dev;
  364. struct nvm_geo *geo = &dev->geo;
  365. struct pblk_lun *rlun;
  366. int bb_cnt = 0;
  367. int i;
  368. for (i = 0; i < blk_per_line; i++) {
  369. rlun = &pblk->luns[i];
  370. if (rlun->bb_list[line->id] == NVM_BLK_T_FREE)
  371. continue;
  372. set_bit(pblk_ppa_to_pos(geo, rlun->bppa), line->blk_bitmap);
  373. bb_cnt++;
  374. }
  375. return bb_cnt;
  376. }
  377. static int pblk_alloc_line_bitmaps(struct pblk *pblk, struct pblk_line *line)
  378. {
  379. struct pblk_line_meta *lm = &pblk->lm;
  380. line->blk_bitmap = kzalloc(lm->blk_bitmap_len, GFP_KERNEL);
  381. if (!line->blk_bitmap)
  382. return -ENOMEM;
  383. line->erase_bitmap = kzalloc(lm->blk_bitmap_len, GFP_KERNEL);
  384. if (!line->erase_bitmap) {
  385. kfree(line->blk_bitmap);
  386. return -ENOMEM;
  387. }
  388. return 0;
  389. }
  390. static int pblk_luns_init(struct pblk *pblk, struct ppa_addr *luns)
  391. {
  392. struct nvm_tgt_dev *dev = pblk->dev;
  393. struct nvm_geo *geo = &dev->geo;
  394. struct pblk_lun *rlun;
  395. int i, ret;
  396. /* TODO: Implement unbalanced LUN support */
  397. if (geo->nr_luns < 0) {
  398. pr_err("pblk: unbalanced LUN config.\n");
  399. return -EINVAL;
  400. }
  401. pblk->luns = kcalloc(geo->all_luns, sizeof(struct pblk_lun),
  402. GFP_KERNEL);
  403. if (!pblk->luns)
  404. return -ENOMEM;
  405. for (i = 0; i < geo->all_luns; i++) {
  406. /* Stripe across channels */
  407. int ch = i % geo->nr_chnls;
  408. int lun_raw = i / geo->nr_chnls;
  409. int lunid = lun_raw + ch * geo->nr_luns;
  410. rlun = &pblk->luns[i];
  411. rlun->bppa = luns[lunid];
  412. sema_init(&rlun->wr_sem, 1);
  413. ret = pblk_bb_discovery(dev, rlun);
  414. if (ret) {
  415. while (--i >= 0)
  416. kfree(pblk->luns[i].bb_list);
  417. return ret;
  418. }
  419. }
  420. return 0;
  421. }
  422. static int pblk_lines_configure(struct pblk *pblk, int flags)
  423. {
  424. struct pblk_line *line = NULL;
  425. int ret = 0;
  426. if (!(flags & NVM_TARGET_FACTORY)) {
  427. line = pblk_recov_l2p(pblk);
  428. if (IS_ERR(line)) {
  429. pr_err("pblk: could not recover l2p table\n");
  430. ret = -EFAULT;
  431. }
  432. }
  433. #ifdef CONFIG_NVM_DEBUG
  434. pr_info("pblk init: L2P CRC: %x\n", pblk_l2p_crc(pblk));
  435. #endif
  436. /* Free full lines directly as GC has not been started yet */
  437. pblk_gc_free_full_lines(pblk);
  438. if (!line) {
  439. /* Configure next line for user data */
  440. line = pblk_line_get_first_data(pblk);
  441. if (!line) {
  442. pr_err("pblk: line list corrupted\n");
  443. ret = -EFAULT;
  444. }
  445. }
  446. return ret;
  447. }
  448. /* See comment over struct line_emeta definition */
  449. static unsigned int calc_emeta_len(struct pblk *pblk)
  450. {
  451. struct pblk_line_meta *lm = &pblk->lm;
  452. struct pblk_line_mgmt *l_mg = &pblk->l_mg;
  453. struct nvm_tgt_dev *dev = pblk->dev;
  454. struct nvm_geo *geo = &dev->geo;
  455. /* Round to sector size so that lba_list starts on its own sector */
  456. lm->emeta_sec[1] = DIV_ROUND_UP(
  457. sizeof(struct line_emeta) + lm->blk_bitmap_len,
  458. geo->sec_size);
  459. lm->emeta_len[1] = lm->emeta_sec[1] * geo->sec_size;
  460. /* Round to sector size so that vsc_list starts on its own sector */
  461. lm->dsec_per_line = lm->sec_per_line - lm->emeta_sec[0];
  462. lm->emeta_sec[2] = DIV_ROUND_UP(lm->dsec_per_line * sizeof(u64),
  463. geo->sec_size);
  464. lm->emeta_len[2] = lm->emeta_sec[2] * geo->sec_size;
  465. lm->emeta_sec[3] = DIV_ROUND_UP(l_mg->nr_lines * sizeof(u32),
  466. geo->sec_size);
  467. lm->emeta_len[3] = lm->emeta_sec[3] * geo->sec_size;
  468. lm->vsc_list_len = l_mg->nr_lines * sizeof(u32);
  469. return (lm->emeta_len[1] + lm->emeta_len[2] + lm->emeta_len[3]);
  470. }
  471. static void pblk_set_provision(struct pblk *pblk, long nr_free_blks)
  472. {
  473. struct nvm_tgt_dev *dev = pblk->dev;
  474. struct nvm_geo *geo = &dev->geo;
  475. sector_t provisioned;
  476. pblk->over_pct = 20;
  477. provisioned = nr_free_blks;
  478. provisioned *= (100 - pblk->over_pct);
  479. sector_div(provisioned, 100);
  480. /* Internally pblk manages all free blocks, but all calculations based
  481. * on user capacity consider only provisioned blocks
  482. */
  483. pblk->rl.total_blocks = nr_free_blks;
  484. pblk->rl.nr_secs = nr_free_blks * geo->sec_per_chk;
  485. pblk->capacity = provisioned * geo->sec_per_chk;
  486. atomic_set(&pblk->rl.free_blocks, nr_free_blks);
  487. }
  488. static int pblk_lines_alloc_metadata(struct pblk *pblk)
  489. {
  490. struct pblk_line_mgmt *l_mg = &pblk->l_mg;
  491. struct pblk_line_meta *lm = &pblk->lm;
  492. int i;
  493. /* smeta is always small enough to fit on a kmalloc memory allocation,
  494. * emeta depends on the number of LUNs allocated to the pblk instance
  495. */
  496. for (i = 0; i < PBLK_DATA_LINES; i++) {
  497. l_mg->sline_meta[i] = kmalloc(lm->smeta_len, GFP_KERNEL);
  498. if (!l_mg->sline_meta[i])
  499. goto fail_free_smeta;
  500. }
  501. /* emeta allocates three different buffers for managing metadata with
  502. * in-memory and in-media layouts
  503. */
  504. for (i = 0; i < PBLK_DATA_LINES; i++) {
  505. struct pblk_emeta *emeta;
  506. emeta = kmalloc(sizeof(struct pblk_emeta), GFP_KERNEL);
  507. if (!emeta)
  508. goto fail_free_emeta;
  509. if (lm->emeta_len[0] > KMALLOC_MAX_CACHE_SIZE) {
  510. l_mg->emeta_alloc_type = PBLK_VMALLOC_META;
  511. emeta->buf = vmalloc(lm->emeta_len[0]);
  512. if (!emeta->buf) {
  513. kfree(emeta);
  514. goto fail_free_emeta;
  515. }
  516. emeta->nr_entries = lm->emeta_sec[0];
  517. l_mg->eline_meta[i] = emeta;
  518. } else {
  519. l_mg->emeta_alloc_type = PBLK_KMALLOC_META;
  520. emeta->buf = kmalloc(lm->emeta_len[0], GFP_KERNEL);
  521. if (!emeta->buf) {
  522. kfree(emeta);
  523. goto fail_free_emeta;
  524. }
  525. emeta->nr_entries = lm->emeta_sec[0];
  526. l_mg->eline_meta[i] = emeta;
  527. }
  528. }
  529. l_mg->vsc_list = kcalloc(l_mg->nr_lines, sizeof(__le32), GFP_KERNEL);
  530. if (!l_mg->vsc_list)
  531. goto fail_free_emeta;
  532. for (i = 0; i < l_mg->nr_lines; i++)
  533. l_mg->vsc_list[i] = cpu_to_le32(EMPTY_ENTRY);
  534. return 0;
  535. fail_free_emeta:
  536. while (--i >= 0) {
  537. if (l_mg->emeta_alloc_type == PBLK_VMALLOC_META)
  538. vfree(l_mg->eline_meta[i]->buf);
  539. else
  540. kfree(l_mg->eline_meta[i]->buf);
  541. kfree(l_mg->eline_meta[i]);
  542. }
  543. fail_free_smeta:
  544. for (i = 0; i < PBLK_DATA_LINES; i++)
  545. kfree(l_mg->sline_meta[i]);
  546. return -ENOMEM;
  547. }
  548. static int pblk_lines_init(struct pblk *pblk)
  549. {
  550. struct nvm_tgt_dev *dev = pblk->dev;
  551. struct nvm_geo *geo = &dev->geo;
  552. struct pblk_line_mgmt *l_mg = &pblk->l_mg;
  553. struct pblk_line_meta *lm = &pblk->lm;
  554. struct pblk_line *line;
  555. unsigned int smeta_len, emeta_len;
  556. long nr_bad_blks, nr_free_blks;
  557. int bb_distance, max_write_ppas, mod;
  558. int i, ret;
  559. pblk->min_write_pgs = geo->sec_per_pl * (geo->sec_size / PAGE_SIZE);
  560. max_write_ppas = pblk->min_write_pgs * geo->all_luns;
  561. pblk->max_write_pgs = (max_write_ppas < nvm_max_phys_sects(dev)) ?
  562. max_write_ppas : nvm_max_phys_sects(dev);
  563. pblk_set_sec_per_write(pblk, pblk->min_write_pgs);
  564. if (pblk->max_write_pgs > PBLK_MAX_REQ_ADDRS) {
  565. pr_err("pblk: cannot support device max_phys_sect\n");
  566. return -EINVAL;
  567. }
  568. div_u64_rem(geo->sec_per_chk, pblk->min_write_pgs, &mod);
  569. if (mod) {
  570. pr_err("pblk: bad configuration of sectors/pages\n");
  571. return -EINVAL;
  572. }
  573. l_mg->nr_lines = geo->nr_chks;
  574. l_mg->log_line = l_mg->data_line = NULL;
  575. l_mg->l_seq_nr = l_mg->d_seq_nr = 0;
  576. l_mg->nr_free_lines = 0;
  577. bitmap_zero(&l_mg->meta_bitmap, PBLK_DATA_LINES);
  578. lm->sec_per_line = geo->sec_per_chk * geo->all_luns;
  579. lm->blk_per_line = geo->all_luns;
  580. lm->blk_bitmap_len = BITS_TO_LONGS(geo->all_luns) * sizeof(long);
  581. lm->sec_bitmap_len = BITS_TO_LONGS(lm->sec_per_line) * sizeof(long);
  582. lm->lun_bitmap_len = BITS_TO_LONGS(geo->all_luns) * sizeof(long);
  583. lm->mid_thrs = lm->sec_per_line / 2;
  584. lm->high_thrs = lm->sec_per_line / 4;
  585. lm->meta_distance = (geo->all_luns / 2) * pblk->min_write_pgs;
  586. /* Calculate necessary pages for smeta. See comment over struct
  587. * line_smeta definition
  588. */
  589. i = 1;
  590. add_smeta_page:
  591. lm->smeta_sec = i * geo->sec_per_pl;
  592. lm->smeta_len = lm->smeta_sec * geo->sec_size;
  593. smeta_len = sizeof(struct line_smeta) + lm->lun_bitmap_len;
  594. if (smeta_len > lm->smeta_len) {
  595. i++;
  596. goto add_smeta_page;
  597. }
  598. /* Calculate necessary pages for emeta. See comment over struct
  599. * line_emeta definition
  600. */
  601. i = 1;
  602. add_emeta_page:
  603. lm->emeta_sec[0] = i * geo->sec_per_pl;
  604. lm->emeta_len[0] = lm->emeta_sec[0] * geo->sec_size;
  605. emeta_len = calc_emeta_len(pblk);
  606. if (emeta_len > lm->emeta_len[0]) {
  607. i++;
  608. goto add_emeta_page;
  609. }
  610. lm->emeta_bb = geo->all_luns > i ? geo->all_luns - i : 0;
  611. lm->min_blk_line = 1;
  612. if (geo->all_luns > 1)
  613. lm->min_blk_line += DIV_ROUND_UP(lm->smeta_sec +
  614. lm->emeta_sec[0], geo->sec_per_chk);
  615. if (lm->min_blk_line > lm->blk_per_line) {
  616. pr_err("pblk: config. not supported. Min. LUN in line:%d\n",
  617. lm->blk_per_line);
  618. ret = -EINVAL;
  619. goto fail;
  620. }
  621. ret = pblk_lines_alloc_metadata(pblk);
  622. if (ret)
  623. goto fail;
  624. l_mg->bb_template = kzalloc(lm->sec_bitmap_len, GFP_KERNEL);
  625. if (!l_mg->bb_template) {
  626. ret = -ENOMEM;
  627. goto fail_free_meta;
  628. }
  629. l_mg->bb_aux = kzalloc(lm->sec_bitmap_len, GFP_KERNEL);
  630. if (!l_mg->bb_aux) {
  631. ret = -ENOMEM;
  632. goto fail_free_bb_template;
  633. }
  634. bb_distance = (geo->all_luns) * geo->sec_per_pl;
  635. for (i = 0; i < lm->sec_per_line; i += bb_distance)
  636. bitmap_set(l_mg->bb_template, i, geo->sec_per_pl);
  637. INIT_LIST_HEAD(&l_mg->free_list);
  638. INIT_LIST_HEAD(&l_mg->corrupt_list);
  639. INIT_LIST_HEAD(&l_mg->bad_list);
  640. INIT_LIST_HEAD(&l_mg->gc_full_list);
  641. INIT_LIST_HEAD(&l_mg->gc_high_list);
  642. INIT_LIST_HEAD(&l_mg->gc_mid_list);
  643. INIT_LIST_HEAD(&l_mg->gc_low_list);
  644. INIT_LIST_HEAD(&l_mg->gc_empty_list);
  645. INIT_LIST_HEAD(&l_mg->emeta_list);
  646. l_mg->gc_lists[0] = &l_mg->gc_high_list;
  647. l_mg->gc_lists[1] = &l_mg->gc_mid_list;
  648. l_mg->gc_lists[2] = &l_mg->gc_low_list;
  649. spin_lock_init(&l_mg->free_lock);
  650. spin_lock_init(&l_mg->close_lock);
  651. spin_lock_init(&l_mg->gc_lock);
  652. pblk->lines = kcalloc(l_mg->nr_lines, sizeof(struct pblk_line),
  653. GFP_KERNEL);
  654. if (!pblk->lines) {
  655. ret = -ENOMEM;
  656. goto fail_free_bb_aux;
  657. }
  658. nr_free_blks = 0;
  659. for (i = 0; i < l_mg->nr_lines; i++) {
  660. int blk_in_line;
  661. line = &pblk->lines[i];
  662. line->pblk = pblk;
  663. line->id = i;
  664. line->type = PBLK_LINETYPE_FREE;
  665. line->state = PBLK_LINESTATE_FREE;
  666. line->gc_group = PBLK_LINEGC_NONE;
  667. line->vsc = &l_mg->vsc_list[i];
  668. spin_lock_init(&line->lock);
  669. ret = pblk_alloc_line_bitmaps(pblk, line);
  670. if (ret)
  671. goto fail_free_lines;
  672. nr_bad_blks = pblk_bb_line(pblk, line, lm->blk_per_line);
  673. if (nr_bad_blks < 0 || nr_bad_blks > lm->blk_per_line) {
  674. pblk_free_line_bitmaps(line);
  675. ret = -EINVAL;
  676. goto fail_free_lines;
  677. }
  678. blk_in_line = lm->blk_per_line - nr_bad_blks;
  679. if (blk_in_line < lm->min_blk_line) {
  680. line->state = PBLK_LINESTATE_BAD;
  681. list_add_tail(&line->list, &l_mg->bad_list);
  682. continue;
  683. }
  684. nr_free_blks += blk_in_line;
  685. atomic_set(&line->blk_in_line, blk_in_line);
  686. l_mg->nr_free_lines++;
  687. list_add_tail(&line->list, &l_mg->free_list);
  688. }
  689. pblk_set_provision(pblk, nr_free_blks);
  690. /* Cleanup per-LUN bad block lists - managed within lines on run-time */
  691. for (i = 0; i < geo->all_luns; i++)
  692. kfree(pblk->luns[i].bb_list);
  693. return 0;
  694. fail_free_lines:
  695. while (--i >= 0)
  696. pblk_free_line_bitmaps(&pblk->lines[i]);
  697. fail_free_bb_aux:
  698. kfree(l_mg->bb_aux);
  699. fail_free_bb_template:
  700. kfree(l_mg->bb_template);
  701. fail_free_meta:
  702. pblk_line_meta_free(pblk);
  703. fail:
  704. for (i = 0; i < geo->all_luns; i++)
  705. kfree(pblk->luns[i].bb_list);
  706. return ret;
  707. }
  708. static int pblk_writer_init(struct pblk *pblk)
  709. {
  710. timer_setup(&pblk->wtimer, pblk_write_timer_fn, 0);
  711. mod_timer(&pblk->wtimer, jiffies + msecs_to_jiffies(100));
  712. pblk->writer_ts = kthread_create(pblk_write_ts, pblk, "pblk-writer-t");
  713. if (IS_ERR(pblk->writer_ts)) {
  714. pr_err("pblk: could not allocate writer kthread\n");
  715. return PTR_ERR(pblk->writer_ts);
  716. }
  717. return 0;
  718. }
  719. static void pblk_writer_stop(struct pblk *pblk)
  720. {
  721. /* The pipeline must be stopped and the write buffer emptied before the
  722. * write thread is stopped
  723. */
  724. WARN(pblk_rb_read_count(&pblk->rwb),
  725. "Stopping not fully persisted write buffer\n");
  726. WARN(pblk_rb_sync_count(&pblk->rwb),
  727. "Stopping not fully synced write buffer\n");
  728. if (pblk->writer_ts)
  729. kthread_stop(pblk->writer_ts);
  730. del_timer(&pblk->wtimer);
  731. }
  732. static void pblk_free(struct pblk *pblk)
  733. {
  734. pblk_luns_free(pblk);
  735. pblk_lines_free(pblk);
  736. pblk_line_meta_free(pblk);
  737. pblk_core_free(pblk);
  738. pblk_l2p_free(pblk);
  739. kfree(pblk);
  740. }
  741. static void pblk_tear_down(struct pblk *pblk)
  742. {
  743. pblk_pipeline_stop(pblk);
  744. pblk_writer_stop(pblk);
  745. pblk_rb_sync_l2p(&pblk->rwb);
  746. pblk_rwb_free(pblk);
  747. pblk_rl_free(&pblk->rl);
  748. pr_debug("pblk: consistent tear down\n");
  749. }
  750. static void pblk_exit(void *private)
  751. {
  752. struct pblk *pblk = private;
  753. down_write(&pblk_lock);
  754. pblk_gc_exit(pblk);
  755. pblk_tear_down(pblk);
  756. #ifdef CONFIG_NVM_DEBUG
  757. pr_info("pblk exit: L2P CRC: %x\n", pblk_l2p_crc(pblk));
  758. #endif
  759. pblk_free(pblk);
  760. up_write(&pblk_lock);
  761. }
  762. static sector_t pblk_capacity(void *private)
  763. {
  764. struct pblk *pblk = private;
  765. return pblk->capacity * NR_PHY_IN_LOG;
  766. }
  767. static void *pblk_init(struct nvm_tgt_dev *dev, struct gendisk *tdisk,
  768. int flags)
  769. {
  770. struct nvm_geo *geo = &dev->geo;
  771. struct request_queue *bqueue = dev->q;
  772. struct request_queue *tqueue = tdisk->queue;
  773. struct pblk *pblk;
  774. int ret;
  775. if (dev->identity.dom & NVM_RSP_L2P) {
  776. pr_err("pblk: host-side L2P table not supported. (%x)\n",
  777. dev->identity.dom);
  778. return ERR_PTR(-EINVAL);
  779. }
  780. pblk = kzalloc(sizeof(struct pblk), GFP_KERNEL);
  781. if (!pblk)
  782. return ERR_PTR(-ENOMEM);
  783. pblk->dev = dev;
  784. pblk->disk = tdisk;
  785. pblk->state = PBLK_STATE_RUNNING;
  786. pblk->gc.gc_enabled = 0;
  787. spin_lock_init(&pblk->trans_lock);
  788. spin_lock_init(&pblk->lock);
  789. if (flags & NVM_TARGET_FACTORY)
  790. pblk_setup_uuid(pblk);
  791. #ifdef CONFIG_NVM_DEBUG
  792. atomic_long_set(&pblk->inflight_writes, 0);
  793. atomic_long_set(&pblk->padded_writes, 0);
  794. atomic_long_set(&pblk->padded_wb, 0);
  795. atomic_long_set(&pblk->nr_flush, 0);
  796. atomic_long_set(&pblk->req_writes, 0);
  797. atomic_long_set(&pblk->sub_writes, 0);
  798. atomic_long_set(&pblk->sync_writes, 0);
  799. atomic_long_set(&pblk->inflight_reads, 0);
  800. atomic_long_set(&pblk->cache_reads, 0);
  801. atomic_long_set(&pblk->sync_reads, 0);
  802. atomic_long_set(&pblk->recov_writes, 0);
  803. atomic_long_set(&pblk->recov_writes, 0);
  804. atomic_long_set(&pblk->recov_gc_writes, 0);
  805. atomic_long_set(&pblk->recov_gc_reads, 0);
  806. #endif
  807. atomic_long_set(&pblk->read_failed, 0);
  808. atomic_long_set(&pblk->read_empty, 0);
  809. atomic_long_set(&pblk->read_high_ecc, 0);
  810. atomic_long_set(&pblk->read_failed_gc, 0);
  811. atomic_long_set(&pblk->write_failed, 0);
  812. atomic_long_set(&pblk->erase_failed, 0);
  813. ret = pblk_luns_init(pblk, dev->luns);
  814. if (ret) {
  815. pr_err("pblk: could not initialize luns\n");
  816. goto fail;
  817. }
  818. ret = pblk_lines_init(pblk);
  819. if (ret) {
  820. pr_err("pblk: could not initialize lines\n");
  821. goto fail_free_luns;
  822. }
  823. ret = pblk_core_init(pblk);
  824. if (ret) {
  825. pr_err("pblk: could not initialize core\n");
  826. goto fail_free_line_meta;
  827. }
  828. ret = pblk_l2p_init(pblk);
  829. if (ret) {
  830. pr_err("pblk: could not initialize maps\n");
  831. goto fail_free_core;
  832. }
  833. ret = pblk_lines_configure(pblk, flags);
  834. if (ret) {
  835. pr_err("pblk: could not configure lines\n");
  836. goto fail_free_l2p;
  837. }
  838. ret = pblk_writer_init(pblk);
  839. if (ret) {
  840. pr_err("pblk: could not initialize write thread\n");
  841. goto fail_free_lines;
  842. }
  843. ret = pblk_gc_init(pblk);
  844. if (ret) {
  845. pr_err("pblk: could not initialize gc\n");
  846. goto fail_stop_writer;
  847. }
  848. /* inherit the size from the underlying device */
  849. blk_queue_logical_block_size(tqueue, queue_physical_block_size(bqueue));
  850. blk_queue_max_hw_sectors(tqueue, queue_max_hw_sectors(bqueue));
  851. blk_queue_write_cache(tqueue, true, false);
  852. tqueue->limits.discard_granularity = geo->sec_per_chk * geo->sec_size;
  853. tqueue->limits.discard_alignment = 0;
  854. blk_queue_max_discard_sectors(tqueue, UINT_MAX >> 9);
  855. queue_flag_set_unlocked(QUEUE_FLAG_DISCARD, tqueue);
  856. pr_info("pblk init: luns:%u, lines:%d, secs:%llu, buf entries:%u\n",
  857. geo->all_luns, pblk->l_mg.nr_lines,
  858. (unsigned long long)pblk->rl.nr_secs,
  859. pblk->rwb.nr_entries);
  860. wake_up_process(pblk->writer_ts);
  861. /* Check if we need to start GC */
  862. pblk_gc_should_kick(pblk);
  863. return pblk;
  864. fail_stop_writer:
  865. pblk_writer_stop(pblk);
  866. fail_free_lines:
  867. pblk_lines_free(pblk);
  868. fail_free_l2p:
  869. pblk_l2p_free(pblk);
  870. fail_free_core:
  871. pblk_core_free(pblk);
  872. fail_free_line_meta:
  873. pblk_line_meta_free(pblk);
  874. fail_free_luns:
  875. pblk_luns_free(pblk);
  876. fail:
  877. kfree(pblk);
  878. return ERR_PTR(ret);
  879. }
  880. /* physical block device target */
  881. static struct nvm_tgt_type tt_pblk = {
  882. .name = "pblk",
  883. .version = {1, 0, 0},
  884. .make_rq = pblk_make_rq,
  885. .capacity = pblk_capacity,
  886. .init = pblk_init,
  887. .exit = pblk_exit,
  888. .sysfs_init = pblk_sysfs_init,
  889. .sysfs_exit = pblk_sysfs_exit,
  890. .owner = THIS_MODULE,
  891. };
  892. static int __init pblk_module_init(void)
  893. {
  894. int ret;
  895. pblk_bio_set = bioset_create(BIO_POOL_SIZE, 0, 0);
  896. if (!pblk_bio_set)
  897. return -ENOMEM;
  898. ret = nvm_register_tgt_type(&tt_pblk);
  899. if (ret)
  900. bioset_free(pblk_bio_set);
  901. return ret;
  902. }
  903. static void pblk_module_exit(void)
  904. {
  905. bioset_free(pblk_bio_set);
  906. nvm_unregister_tgt_type(&tt_pblk);
  907. }
  908. module_init(pblk_module_init);
  909. module_exit(pblk_module_exit);
  910. MODULE_AUTHOR("Javier Gonzalez <javier@cnexlabs.com>");
  911. MODULE_AUTHOR("Matias Bjorling <matias@cnexlabs.com>");
  912. MODULE_LICENSE("GPL v2");
  913. MODULE_DESCRIPTION("Physical Block-Device for Open-Channel SSDs");