pblk-init.c 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320
  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->addrf_len < 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_recover(struct pblk *pblk, bool factory_init)
  89. {
  90. struct pblk_line *line = NULL;
  91. if (factory_init) {
  92. pblk_setup_uuid(pblk);
  93. } else {
  94. line = pblk_recov_l2p(pblk);
  95. if (IS_ERR(line)) {
  96. pr_err("pblk: could not recover l2p table\n");
  97. return -EFAULT;
  98. }
  99. }
  100. #ifdef CONFIG_NVM_DEBUG
  101. pr_info("pblk init: L2P CRC: %x\n", pblk_l2p_crc(pblk));
  102. #endif
  103. /* Free full lines directly as GC has not been started yet */
  104. pblk_gc_free_full_lines(pblk);
  105. if (!line) {
  106. /* Configure next line for user data */
  107. line = pblk_line_get_first_data(pblk);
  108. if (!line) {
  109. pr_err("pblk: line list corrupted\n");
  110. return -EFAULT;
  111. }
  112. }
  113. return 0;
  114. }
  115. static int pblk_l2p_init(struct pblk *pblk, bool factory_init)
  116. {
  117. sector_t i;
  118. struct ppa_addr ppa;
  119. size_t map_size;
  120. map_size = pblk_trans_map_size(pblk);
  121. pblk->trans_map = vmalloc(map_size);
  122. if (!pblk->trans_map)
  123. return -ENOMEM;
  124. pblk_ppa_set_empty(&ppa);
  125. for (i = 0; i < pblk->rl.nr_secs; i++)
  126. pblk_trans_map_set(pblk, i, ppa);
  127. return pblk_l2p_recover(pblk, factory_init);
  128. }
  129. static void pblk_rwb_free(struct pblk *pblk)
  130. {
  131. if (pblk_rb_tear_down_check(&pblk->rwb))
  132. pr_err("pblk: write buffer error on tear down\n");
  133. pblk_rb_data_free(&pblk->rwb);
  134. vfree(pblk_rb_entries_ref(&pblk->rwb));
  135. }
  136. static int pblk_rwb_init(struct pblk *pblk)
  137. {
  138. struct nvm_tgt_dev *dev = pblk->dev;
  139. struct nvm_geo *geo = &dev->geo;
  140. struct pblk_rb_entry *entries;
  141. unsigned long nr_entries;
  142. unsigned int power_size, power_seg_sz;
  143. nr_entries = pblk_rb_calculate_size(pblk->pgs_in_buffer);
  144. entries = vzalloc(nr_entries * sizeof(struct pblk_rb_entry));
  145. if (!entries)
  146. return -ENOMEM;
  147. power_size = get_count_order(nr_entries);
  148. power_seg_sz = get_count_order(geo->csecs);
  149. return pblk_rb_init(&pblk->rwb, entries, power_size, power_seg_sz);
  150. }
  151. /* Minimum pages needed within a lun */
  152. #define ADDR_POOL_SIZE 64
  153. static int pblk_set_addrf_12(struct nvm_geo *geo, struct nvm_addrf_12 *dst)
  154. {
  155. struct nvm_addrf_12 *src = (struct nvm_addrf_12 *)&geo->addrf;
  156. int power_len;
  157. /* Re-calculate channel and lun format to adapt to configuration */
  158. power_len = get_count_order(geo->num_ch);
  159. if (1 << power_len != geo->num_ch) {
  160. pr_err("pblk: supports only power-of-two channel config.\n");
  161. return -EINVAL;
  162. }
  163. dst->ch_len = power_len;
  164. power_len = get_count_order(geo->num_lun);
  165. if (1 << power_len != geo->num_lun) {
  166. pr_err("pblk: supports only power-of-two LUN config.\n");
  167. return -EINVAL;
  168. }
  169. dst->lun_len = power_len;
  170. dst->blk_len = src->blk_len;
  171. dst->pg_len = src->pg_len;
  172. dst->pln_len = src->pln_len;
  173. dst->sec_len = src->sec_len;
  174. dst->sec_offset = 0;
  175. dst->pln_offset = dst->sec_len;
  176. dst->ch_offset = dst->pln_offset + dst->pln_len;
  177. dst->lun_offset = dst->ch_offset + dst->ch_len;
  178. dst->pg_offset = dst->lun_offset + dst->lun_len;
  179. dst->blk_offset = dst->pg_offset + dst->pg_len;
  180. dst->sec_mask = ((1ULL << dst->sec_len) - 1) << dst->sec_offset;
  181. dst->pln_mask = ((1ULL << dst->pln_len) - 1) << dst->pln_offset;
  182. dst->ch_mask = ((1ULL << dst->ch_len) - 1) << dst->ch_offset;
  183. dst->lun_mask = ((1ULL << dst->lun_len) - 1) << dst->lun_offset;
  184. dst->pg_mask = ((1ULL << dst->pg_len) - 1) << dst->pg_offset;
  185. dst->blk_mask = ((1ULL << dst->blk_len) - 1) << dst->blk_offset;
  186. return dst->blk_offset + src->blk_len;
  187. }
  188. static int pblk_set_addrf_20(struct nvm_geo *geo, struct nvm_addrf *adst,
  189. struct pblk_addrf *udst)
  190. {
  191. struct nvm_addrf *src = &geo->addrf;
  192. adst->ch_len = get_count_order(geo->num_ch);
  193. adst->lun_len = get_count_order(geo->num_lun);
  194. adst->chk_len = src->chk_len;
  195. adst->sec_len = src->sec_len;
  196. adst->sec_offset = 0;
  197. adst->ch_offset = adst->sec_len;
  198. adst->lun_offset = adst->ch_offset + adst->ch_len;
  199. adst->chk_offset = adst->lun_offset + adst->lun_len;
  200. adst->sec_mask = ((1ULL << adst->sec_len) - 1) << adst->sec_offset;
  201. adst->chk_mask = ((1ULL << adst->chk_len) - 1) << adst->chk_offset;
  202. adst->lun_mask = ((1ULL << adst->lun_len) - 1) << adst->lun_offset;
  203. adst->ch_mask = ((1ULL << adst->ch_len) - 1) << adst->ch_offset;
  204. udst->sec_stripe = geo->ws_opt;
  205. udst->ch_stripe = geo->num_ch;
  206. udst->lun_stripe = geo->num_lun;
  207. udst->sec_lun_stripe = udst->sec_stripe * udst->ch_stripe;
  208. udst->sec_ws_stripe = udst->sec_lun_stripe * udst->lun_stripe;
  209. return adst->chk_offset + adst->chk_len;
  210. }
  211. static int pblk_set_addrf(struct pblk *pblk)
  212. {
  213. struct nvm_tgt_dev *dev = pblk->dev;
  214. struct nvm_geo *geo = &dev->geo;
  215. int mod;
  216. switch (geo->version) {
  217. case NVM_OCSSD_SPEC_12:
  218. div_u64_rem(geo->clba, pblk->min_write_pgs, &mod);
  219. if (mod) {
  220. pr_err("pblk: bad configuration of sectors/pages\n");
  221. return -EINVAL;
  222. }
  223. pblk->addrf_len = pblk_set_addrf_12(geo, (void *)&pblk->addrf);
  224. break;
  225. case NVM_OCSSD_SPEC_20:
  226. pblk->addrf_len = pblk_set_addrf_20(geo, (void *)&pblk->addrf,
  227. &pblk->uaddrf);
  228. break;
  229. default:
  230. pr_err("pblk: OCSSD revision not supported (%d)\n",
  231. geo->version);
  232. return -EINVAL;
  233. }
  234. return 0;
  235. }
  236. static int pblk_init_global_caches(struct pblk *pblk)
  237. {
  238. down_write(&pblk_lock);
  239. pblk_ws_cache = kmem_cache_create("pblk_blk_ws",
  240. sizeof(struct pblk_line_ws), 0, 0, NULL);
  241. if (!pblk_ws_cache) {
  242. up_write(&pblk_lock);
  243. return -ENOMEM;
  244. }
  245. pblk_rec_cache = kmem_cache_create("pblk_rec",
  246. sizeof(struct pblk_rec_ctx), 0, 0, NULL);
  247. if (!pblk_rec_cache) {
  248. kmem_cache_destroy(pblk_ws_cache);
  249. up_write(&pblk_lock);
  250. return -ENOMEM;
  251. }
  252. pblk_g_rq_cache = kmem_cache_create("pblk_g_rq", pblk_g_rq_size,
  253. 0, 0, NULL);
  254. if (!pblk_g_rq_cache) {
  255. kmem_cache_destroy(pblk_ws_cache);
  256. kmem_cache_destroy(pblk_rec_cache);
  257. up_write(&pblk_lock);
  258. return -ENOMEM;
  259. }
  260. pblk_w_rq_cache = kmem_cache_create("pblk_w_rq", pblk_w_rq_size,
  261. 0, 0, NULL);
  262. if (!pblk_w_rq_cache) {
  263. kmem_cache_destroy(pblk_ws_cache);
  264. kmem_cache_destroy(pblk_rec_cache);
  265. kmem_cache_destroy(pblk_g_rq_cache);
  266. up_write(&pblk_lock);
  267. return -ENOMEM;
  268. }
  269. up_write(&pblk_lock);
  270. return 0;
  271. }
  272. static void pblk_free_global_caches(struct pblk *pblk)
  273. {
  274. kmem_cache_destroy(pblk_ws_cache);
  275. kmem_cache_destroy(pblk_rec_cache);
  276. kmem_cache_destroy(pblk_g_rq_cache);
  277. kmem_cache_destroy(pblk_w_rq_cache);
  278. }
  279. static int pblk_core_init(struct pblk *pblk)
  280. {
  281. struct nvm_tgt_dev *dev = pblk->dev;
  282. struct nvm_geo *geo = &dev->geo;
  283. int max_write_ppas;
  284. atomic64_set(&pblk->user_wa, 0);
  285. atomic64_set(&pblk->pad_wa, 0);
  286. atomic64_set(&pblk->gc_wa, 0);
  287. pblk->user_rst_wa = 0;
  288. pblk->pad_rst_wa = 0;
  289. pblk->gc_rst_wa = 0;
  290. atomic64_set(&pblk->nr_flush, 0);
  291. pblk->nr_flush_rst = 0;
  292. pblk->pgs_in_buffer = geo->mw_cunits * geo->all_luns;
  293. pblk->min_write_pgs = geo->ws_opt * (geo->csecs / PAGE_SIZE);
  294. max_write_ppas = pblk->min_write_pgs * geo->all_luns;
  295. pblk->max_write_pgs = min_t(int, max_write_ppas, NVM_MAX_VLBA);
  296. pblk_set_sec_per_write(pblk, pblk->min_write_pgs);
  297. if (pblk->max_write_pgs > PBLK_MAX_REQ_ADDRS) {
  298. pr_err("pblk: vector list too big(%u > %u)\n",
  299. pblk->max_write_pgs, PBLK_MAX_REQ_ADDRS);
  300. return -EINVAL;
  301. }
  302. pblk->pad_dist = kzalloc((pblk->min_write_pgs - 1) * sizeof(atomic64_t),
  303. GFP_KERNEL);
  304. if (!pblk->pad_dist)
  305. return -ENOMEM;
  306. if (pblk_init_global_caches(pblk))
  307. goto fail_free_pad_dist;
  308. /* Internal bios can be at most the sectors signaled by the device. */
  309. pblk->page_bio_pool = mempool_create_page_pool(NVM_MAX_VLBA, 0);
  310. if (!pblk->page_bio_pool)
  311. goto free_global_caches;
  312. pblk->gen_ws_pool = mempool_create_slab_pool(PBLK_GEN_WS_POOL_SIZE,
  313. pblk_ws_cache);
  314. if (!pblk->gen_ws_pool)
  315. goto free_page_bio_pool;
  316. pblk->rec_pool = mempool_create_slab_pool(geo->all_luns,
  317. pblk_rec_cache);
  318. if (!pblk->rec_pool)
  319. goto free_gen_ws_pool;
  320. pblk->r_rq_pool = mempool_create_slab_pool(geo->all_luns,
  321. pblk_g_rq_cache);
  322. if (!pblk->r_rq_pool)
  323. goto free_rec_pool;
  324. pblk->e_rq_pool = mempool_create_slab_pool(geo->all_luns,
  325. pblk_g_rq_cache);
  326. if (!pblk->e_rq_pool)
  327. goto free_r_rq_pool;
  328. pblk->w_rq_pool = mempool_create_slab_pool(geo->all_luns,
  329. pblk_w_rq_cache);
  330. if (!pblk->w_rq_pool)
  331. goto free_e_rq_pool;
  332. pblk->close_wq = alloc_workqueue("pblk-close-wq",
  333. WQ_MEM_RECLAIM | WQ_UNBOUND, PBLK_NR_CLOSE_JOBS);
  334. if (!pblk->close_wq)
  335. goto free_w_rq_pool;
  336. pblk->bb_wq = alloc_workqueue("pblk-bb-wq",
  337. WQ_MEM_RECLAIM | WQ_UNBOUND, 0);
  338. if (!pblk->bb_wq)
  339. goto free_close_wq;
  340. pblk->r_end_wq = alloc_workqueue("pblk-read-end-wq",
  341. WQ_MEM_RECLAIM | WQ_UNBOUND, 0);
  342. if (!pblk->r_end_wq)
  343. goto free_bb_wq;
  344. if (pblk_set_addrf(pblk))
  345. goto free_r_end_wq;
  346. INIT_LIST_HEAD(&pblk->compl_list);
  347. return 0;
  348. free_r_end_wq:
  349. destroy_workqueue(pblk->r_end_wq);
  350. free_bb_wq:
  351. destroy_workqueue(pblk->bb_wq);
  352. free_close_wq:
  353. destroy_workqueue(pblk->close_wq);
  354. free_w_rq_pool:
  355. mempool_destroy(pblk->w_rq_pool);
  356. free_e_rq_pool:
  357. mempool_destroy(pblk->e_rq_pool);
  358. free_r_rq_pool:
  359. mempool_destroy(pblk->r_rq_pool);
  360. free_rec_pool:
  361. mempool_destroy(pblk->rec_pool);
  362. free_gen_ws_pool:
  363. mempool_destroy(pblk->gen_ws_pool);
  364. free_page_bio_pool:
  365. mempool_destroy(pblk->page_bio_pool);
  366. free_global_caches:
  367. pblk_free_global_caches(pblk);
  368. fail_free_pad_dist:
  369. kfree(pblk->pad_dist);
  370. return -ENOMEM;
  371. }
  372. static void pblk_core_free(struct pblk *pblk)
  373. {
  374. if (pblk->close_wq)
  375. destroy_workqueue(pblk->close_wq);
  376. if (pblk->r_end_wq)
  377. destroy_workqueue(pblk->r_end_wq);
  378. if (pblk->bb_wq)
  379. destroy_workqueue(pblk->bb_wq);
  380. mempool_destroy(pblk->page_bio_pool);
  381. mempool_destroy(pblk->gen_ws_pool);
  382. mempool_destroy(pblk->rec_pool);
  383. mempool_destroy(pblk->r_rq_pool);
  384. mempool_destroy(pblk->e_rq_pool);
  385. mempool_destroy(pblk->w_rq_pool);
  386. pblk_free_global_caches(pblk);
  387. kfree(pblk->pad_dist);
  388. }
  389. static void pblk_line_mg_free(struct pblk *pblk)
  390. {
  391. struct pblk_line_mgmt *l_mg = &pblk->l_mg;
  392. int i;
  393. kfree(l_mg->bb_template);
  394. kfree(l_mg->bb_aux);
  395. kfree(l_mg->vsc_list);
  396. for (i = 0; i < PBLK_DATA_LINES; i++) {
  397. kfree(l_mg->sline_meta[i]);
  398. pblk_mfree(l_mg->eline_meta[i]->buf, l_mg->emeta_alloc_type);
  399. kfree(l_mg->eline_meta[i]);
  400. }
  401. }
  402. static void pblk_line_meta_free(struct pblk_line *line)
  403. {
  404. kfree(line->blk_bitmap);
  405. kfree(line->erase_bitmap);
  406. kfree(line->chks);
  407. }
  408. static void pblk_lines_free(struct pblk *pblk)
  409. {
  410. struct pblk_line_mgmt *l_mg = &pblk->l_mg;
  411. struct pblk_line *line;
  412. int i;
  413. spin_lock(&l_mg->free_lock);
  414. for (i = 0; i < l_mg->nr_lines; i++) {
  415. line = &pblk->lines[i];
  416. pblk_line_free(pblk, line);
  417. pblk_line_meta_free(line);
  418. }
  419. spin_unlock(&l_mg->free_lock);
  420. pblk_line_mg_free(pblk);
  421. kfree(pblk->luns);
  422. kfree(pblk->lines);
  423. }
  424. static int pblk_bb_get_tbl(struct nvm_tgt_dev *dev, struct pblk_lun *rlun,
  425. u8 *blks, int nr_blks)
  426. {
  427. struct ppa_addr ppa;
  428. int ret;
  429. ppa.ppa = 0;
  430. ppa.g.ch = rlun->bppa.g.ch;
  431. ppa.g.lun = rlun->bppa.g.lun;
  432. ret = nvm_get_tgt_bb_tbl(dev, ppa, blks);
  433. if (ret)
  434. return ret;
  435. nr_blks = nvm_bb_tbl_fold(dev->parent, blks, nr_blks);
  436. if (nr_blks < 0)
  437. return -EIO;
  438. return 0;
  439. }
  440. static void *pblk_bb_get_meta(struct pblk *pblk)
  441. {
  442. struct nvm_tgt_dev *dev = pblk->dev;
  443. struct nvm_geo *geo = &dev->geo;
  444. u8 *meta;
  445. int i, nr_blks, blk_per_lun;
  446. int ret;
  447. blk_per_lun = geo->num_chk * geo->pln_mode;
  448. nr_blks = blk_per_lun * geo->all_luns;
  449. meta = kmalloc(nr_blks, GFP_KERNEL);
  450. if (!meta)
  451. return ERR_PTR(-ENOMEM);
  452. for (i = 0; i < geo->all_luns; i++) {
  453. struct pblk_lun *rlun = &pblk->luns[i];
  454. u8 *meta_pos = meta + i * blk_per_lun;
  455. ret = pblk_bb_get_tbl(dev, rlun, meta_pos, blk_per_lun);
  456. if (ret) {
  457. kfree(meta);
  458. return ERR_PTR(-EIO);
  459. }
  460. }
  461. return meta;
  462. }
  463. static void *pblk_chunk_get_meta(struct pblk *pblk)
  464. {
  465. struct nvm_tgt_dev *dev = pblk->dev;
  466. struct nvm_geo *geo = &dev->geo;
  467. if (geo->version == NVM_OCSSD_SPEC_12)
  468. return pblk_bb_get_meta(pblk);
  469. else
  470. return pblk_chunk_get_info(pblk);
  471. }
  472. static int pblk_luns_init(struct pblk *pblk)
  473. {
  474. struct nvm_tgt_dev *dev = pblk->dev;
  475. struct nvm_geo *geo = &dev->geo;
  476. struct pblk_lun *rlun;
  477. int i;
  478. /* TODO: Implement unbalanced LUN support */
  479. if (geo->num_lun < 0) {
  480. pr_err("pblk: unbalanced LUN config.\n");
  481. return -EINVAL;
  482. }
  483. pblk->luns = kcalloc(geo->all_luns, sizeof(struct pblk_lun),
  484. GFP_KERNEL);
  485. if (!pblk->luns)
  486. return -ENOMEM;
  487. for (i = 0; i < geo->all_luns; i++) {
  488. /* Stripe across channels */
  489. int ch = i % geo->num_ch;
  490. int lun_raw = i / geo->num_ch;
  491. int lunid = lun_raw + ch * geo->num_lun;
  492. rlun = &pblk->luns[i];
  493. rlun->bppa = dev->luns[lunid];
  494. sema_init(&rlun->wr_sem, 1);
  495. }
  496. return 0;
  497. }
  498. /* See comment over struct line_emeta definition */
  499. static unsigned int calc_emeta_len(struct pblk *pblk)
  500. {
  501. struct pblk_line_meta *lm = &pblk->lm;
  502. struct pblk_line_mgmt *l_mg = &pblk->l_mg;
  503. struct nvm_tgt_dev *dev = pblk->dev;
  504. struct nvm_geo *geo = &dev->geo;
  505. /* Round to sector size so that lba_list starts on its own sector */
  506. lm->emeta_sec[1] = DIV_ROUND_UP(
  507. sizeof(struct line_emeta) + lm->blk_bitmap_len +
  508. sizeof(struct wa_counters), geo->csecs);
  509. lm->emeta_len[1] = lm->emeta_sec[1] * geo->csecs;
  510. /* Round to sector size so that vsc_list starts on its own sector */
  511. lm->dsec_per_line = lm->sec_per_line - lm->emeta_sec[0];
  512. lm->emeta_sec[2] = DIV_ROUND_UP(lm->dsec_per_line * sizeof(u64),
  513. geo->csecs);
  514. lm->emeta_len[2] = lm->emeta_sec[2] * geo->csecs;
  515. lm->emeta_sec[3] = DIV_ROUND_UP(l_mg->nr_lines * sizeof(u32),
  516. geo->csecs);
  517. lm->emeta_len[3] = lm->emeta_sec[3] * geo->csecs;
  518. lm->vsc_list_len = l_mg->nr_lines * sizeof(u32);
  519. return (lm->emeta_len[1] + lm->emeta_len[2] + lm->emeta_len[3]);
  520. }
  521. static void pblk_set_provision(struct pblk *pblk, long nr_free_blks)
  522. {
  523. struct nvm_tgt_dev *dev = pblk->dev;
  524. struct pblk_line_mgmt *l_mg = &pblk->l_mg;
  525. struct pblk_line_meta *lm = &pblk->lm;
  526. struct nvm_geo *geo = &dev->geo;
  527. sector_t provisioned;
  528. int sec_meta, blk_meta;
  529. if (geo->op == NVM_TARGET_DEFAULT_OP)
  530. pblk->op = PBLK_DEFAULT_OP;
  531. else
  532. pblk->op = geo->op;
  533. provisioned = nr_free_blks;
  534. provisioned *= (100 - pblk->op);
  535. sector_div(provisioned, 100);
  536. pblk->op_blks = nr_free_blks - provisioned;
  537. /* Internally pblk manages all free blocks, but all calculations based
  538. * on user capacity consider only provisioned blocks
  539. */
  540. pblk->rl.total_blocks = nr_free_blks;
  541. pblk->rl.nr_secs = nr_free_blks * geo->clba;
  542. /* Consider sectors used for metadata */
  543. sec_meta = (lm->smeta_sec + lm->emeta_sec[0]) * l_mg->nr_free_lines;
  544. blk_meta = DIV_ROUND_UP(sec_meta, geo->clba);
  545. pblk->capacity = (provisioned - blk_meta) * geo->clba;
  546. atomic_set(&pblk->rl.free_blocks, nr_free_blks);
  547. atomic_set(&pblk->rl.free_user_blocks, nr_free_blks);
  548. }
  549. static int pblk_setup_line_meta_12(struct pblk *pblk, struct pblk_line *line,
  550. void *chunk_meta)
  551. {
  552. struct nvm_tgt_dev *dev = pblk->dev;
  553. struct nvm_geo *geo = &dev->geo;
  554. struct pblk_line_meta *lm = &pblk->lm;
  555. int i, chk_per_lun, nr_bad_chks = 0;
  556. chk_per_lun = geo->num_chk * geo->pln_mode;
  557. for (i = 0; i < lm->blk_per_line; i++) {
  558. struct pblk_lun *rlun = &pblk->luns[i];
  559. struct nvm_chk_meta *chunk;
  560. int pos = pblk_ppa_to_pos(geo, rlun->bppa);
  561. u8 *lun_bb_meta = chunk_meta + pos * chk_per_lun;
  562. chunk = &line->chks[pos];
  563. /*
  564. * In 1.2 spec. chunk state is not persisted by the device. Thus
  565. * some of the values are reset each time pblk is instantiated.
  566. */
  567. if (lun_bb_meta[line->id] == NVM_BLK_T_FREE)
  568. chunk->state = NVM_CHK_ST_FREE;
  569. else
  570. chunk->state = NVM_CHK_ST_OFFLINE;
  571. chunk->type = NVM_CHK_TP_W_SEQ;
  572. chunk->wi = 0;
  573. chunk->slba = -1;
  574. chunk->cnlb = geo->clba;
  575. chunk->wp = 0;
  576. if (!(chunk->state & NVM_CHK_ST_OFFLINE))
  577. continue;
  578. set_bit(pos, line->blk_bitmap);
  579. nr_bad_chks++;
  580. }
  581. return nr_bad_chks;
  582. }
  583. static int pblk_setup_line_meta_20(struct pblk *pblk, struct pblk_line *line,
  584. struct nvm_chk_meta *meta)
  585. {
  586. struct nvm_tgt_dev *dev = pblk->dev;
  587. struct nvm_geo *geo = &dev->geo;
  588. struct pblk_line_meta *lm = &pblk->lm;
  589. int i, nr_bad_chks = 0;
  590. for (i = 0; i < lm->blk_per_line; i++) {
  591. struct pblk_lun *rlun = &pblk->luns[i];
  592. struct nvm_chk_meta *chunk;
  593. struct nvm_chk_meta *chunk_meta;
  594. struct ppa_addr ppa;
  595. int pos;
  596. ppa = rlun->bppa;
  597. pos = pblk_ppa_to_pos(geo, ppa);
  598. chunk = &line->chks[pos];
  599. ppa.m.chk = line->id;
  600. chunk_meta = pblk_chunk_get_off(pblk, meta, ppa);
  601. chunk->state = chunk_meta->state;
  602. chunk->type = chunk_meta->type;
  603. chunk->wi = chunk_meta->wi;
  604. chunk->slba = chunk_meta->slba;
  605. chunk->cnlb = chunk_meta->cnlb;
  606. chunk->wp = chunk_meta->wp;
  607. if (!(chunk->state & NVM_CHK_ST_OFFLINE))
  608. continue;
  609. if (chunk->type & NVM_CHK_TP_SZ_SPEC) {
  610. WARN_ONCE(1, "pblk: custom-sized chunks unsupported\n");
  611. continue;
  612. }
  613. set_bit(pos, line->blk_bitmap);
  614. nr_bad_chks++;
  615. }
  616. return nr_bad_chks;
  617. }
  618. static long pblk_setup_line_meta(struct pblk *pblk, struct pblk_line *line,
  619. void *chunk_meta, int line_id)
  620. {
  621. struct nvm_tgt_dev *dev = pblk->dev;
  622. struct nvm_geo *geo = &dev->geo;
  623. struct pblk_line_mgmt *l_mg = &pblk->l_mg;
  624. struct pblk_line_meta *lm = &pblk->lm;
  625. long nr_bad_chks, chk_in_line;
  626. line->pblk = pblk;
  627. line->id = line_id;
  628. line->type = PBLK_LINETYPE_FREE;
  629. line->state = PBLK_LINESTATE_NEW;
  630. line->gc_group = PBLK_LINEGC_NONE;
  631. line->vsc = &l_mg->vsc_list[line_id];
  632. spin_lock_init(&line->lock);
  633. if (geo->version == NVM_OCSSD_SPEC_12)
  634. nr_bad_chks = pblk_setup_line_meta_12(pblk, line, chunk_meta);
  635. else
  636. nr_bad_chks = pblk_setup_line_meta_20(pblk, line, chunk_meta);
  637. chk_in_line = lm->blk_per_line - nr_bad_chks;
  638. if (nr_bad_chks < 0 || nr_bad_chks > lm->blk_per_line ||
  639. chk_in_line < lm->min_blk_line) {
  640. line->state = PBLK_LINESTATE_BAD;
  641. list_add_tail(&line->list, &l_mg->bad_list);
  642. return 0;
  643. }
  644. atomic_set(&line->blk_in_line, chk_in_line);
  645. list_add_tail(&line->list, &l_mg->free_list);
  646. l_mg->nr_free_lines++;
  647. return chk_in_line;
  648. }
  649. static int pblk_alloc_line_meta(struct pblk *pblk, struct pblk_line *line)
  650. {
  651. struct pblk_line_meta *lm = &pblk->lm;
  652. line->blk_bitmap = kzalloc(lm->blk_bitmap_len, GFP_KERNEL);
  653. if (!line->blk_bitmap)
  654. return -ENOMEM;
  655. line->erase_bitmap = kzalloc(lm->blk_bitmap_len, GFP_KERNEL);
  656. if (!line->erase_bitmap) {
  657. kfree(line->blk_bitmap);
  658. return -ENOMEM;
  659. }
  660. line->chks = kmalloc(lm->blk_per_line * sizeof(struct nvm_chk_meta),
  661. GFP_KERNEL);
  662. if (!line->chks) {
  663. kfree(line->erase_bitmap);
  664. kfree(line->blk_bitmap);
  665. return -ENOMEM;
  666. }
  667. return 0;
  668. }
  669. static int pblk_line_mg_init(struct pblk *pblk)
  670. {
  671. struct nvm_tgt_dev *dev = pblk->dev;
  672. struct nvm_geo *geo = &dev->geo;
  673. struct pblk_line_mgmt *l_mg = &pblk->l_mg;
  674. struct pblk_line_meta *lm = &pblk->lm;
  675. int i, bb_distance;
  676. l_mg->nr_lines = geo->num_chk;
  677. l_mg->log_line = l_mg->data_line = NULL;
  678. l_mg->l_seq_nr = l_mg->d_seq_nr = 0;
  679. l_mg->nr_free_lines = 0;
  680. bitmap_zero(&l_mg->meta_bitmap, PBLK_DATA_LINES);
  681. INIT_LIST_HEAD(&l_mg->free_list);
  682. INIT_LIST_HEAD(&l_mg->corrupt_list);
  683. INIT_LIST_HEAD(&l_mg->bad_list);
  684. INIT_LIST_HEAD(&l_mg->gc_full_list);
  685. INIT_LIST_HEAD(&l_mg->gc_high_list);
  686. INIT_LIST_HEAD(&l_mg->gc_mid_list);
  687. INIT_LIST_HEAD(&l_mg->gc_low_list);
  688. INIT_LIST_HEAD(&l_mg->gc_empty_list);
  689. INIT_LIST_HEAD(&l_mg->emeta_list);
  690. l_mg->gc_lists[0] = &l_mg->gc_high_list;
  691. l_mg->gc_lists[1] = &l_mg->gc_mid_list;
  692. l_mg->gc_lists[2] = &l_mg->gc_low_list;
  693. spin_lock_init(&l_mg->free_lock);
  694. spin_lock_init(&l_mg->close_lock);
  695. spin_lock_init(&l_mg->gc_lock);
  696. l_mg->vsc_list = kcalloc(l_mg->nr_lines, sizeof(__le32), GFP_KERNEL);
  697. if (!l_mg->vsc_list)
  698. goto fail;
  699. l_mg->bb_template = kzalloc(lm->sec_bitmap_len, GFP_KERNEL);
  700. if (!l_mg->bb_template)
  701. goto fail_free_vsc_list;
  702. l_mg->bb_aux = kzalloc(lm->sec_bitmap_len, GFP_KERNEL);
  703. if (!l_mg->bb_aux)
  704. goto fail_free_bb_template;
  705. /* smeta is always small enough to fit on a kmalloc memory allocation,
  706. * emeta depends on the number of LUNs allocated to the pblk instance
  707. */
  708. for (i = 0; i < PBLK_DATA_LINES; i++) {
  709. l_mg->sline_meta[i] = kmalloc(lm->smeta_len, GFP_KERNEL);
  710. if (!l_mg->sline_meta[i])
  711. goto fail_free_smeta;
  712. }
  713. /* emeta allocates three different buffers for managing metadata with
  714. * in-memory and in-media layouts
  715. */
  716. for (i = 0; i < PBLK_DATA_LINES; i++) {
  717. struct pblk_emeta *emeta;
  718. emeta = kmalloc(sizeof(struct pblk_emeta), GFP_KERNEL);
  719. if (!emeta)
  720. goto fail_free_emeta;
  721. if (lm->emeta_len[0] > KMALLOC_MAX_CACHE_SIZE) {
  722. l_mg->emeta_alloc_type = PBLK_VMALLOC_META;
  723. emeta->buf = vmalloc(lm->emeta_len[0]);
  724. if (!emeta->buf) {
  725. kfree(emeta);
  726. goto fail_free_emeta;
  727. }
  728. emeta->nr_entries = lm->emeta_sec[0];
  729. l_mg->eline_meta[i] = emeta;
  730. } else {
  731. l_mg->emeta_alloc_type = PBLK_KMALLOC_META;
  732. emeta->buf = kmalloc(lm->emeta_len[0], GFP_KERNEL);
  733. if (!emeta->buf) {
  734. kfree(emeta);
  735. goto fail_free_emeta;
  736. }
  737. emeta->nr_entries = lm->emeta_sec[0];
  738. l_mg->eline_meta[i] = emeta;
  739. }
  740. }
  741. for (i = 0; i < l_mg->nr_lines; i++)
  742. l_mg->vsc_list[i] = cpu_to_le32(EMPTY_ENTRY);
  743. bb_distance = (geo->all_luns) * geo->ws_opt;
  744. for (i = 0; i < lm->sec_per_line; i += bb_distance)
  745. bitmap_set(l_mg->bb_template, i, geo->ws_opt);
  746. return 0;
  747. fail_free_emeta:
  748. while (--i >= 0) {
  749. if (l_mg->emeta_alloc_type == PBLK_VMALLOC_META)
  750. vfree(l_mg->eline_meta[i]->buf);
  751. else
  752. kfree(l_mg->eline_meta[i]->buf);
  753. kfree(l_mg->eline_meta[i]);
  754. }
  755. fail_free_smeta:
  756. for (i = 0; i < PBLK_DATA_LINES; i++)
  757. kfree(l_mg->sline_meta[i]);
  758. kfree(l_mg->bb_aux);
  759. fail_free_bb_template:
  760. kfree(l_mg->bb_template);
  761. fail_free_vsc_list:
  762. kfree(l_mg->vsc_list);
  763. fail:
  764. return -ENOMEM;
  765. }
  766. static int pblk_line_meta_init(struct pblk *pblk)
  767. {
  768. struct nvm_tgt_dev *dev = pblk->dev;
  769. struct nvm_geo *geo = &dev->geo;
  770. struct pblk_line_meta *lm = &pblk->lm;
  771. unsigned int smeta_len, emeta_len;
  772. int i;
  773. lm->sec_per_line = geo->clba * geo->all_luns;
  774. lm->blk_per_line = geo->all_luns;
  775. lm->blk_bitmap_len = BITS_TO_LONGS(geo->all_luns) * sizeof(long);
  776. lm->sec_bitmap_len = BITS_TO_LONGS(lm->sec_per_line) * sizeof(long);
  777. lm->lun_bitmap_len = BITS_TO_LONGS(geo->all_luns) * sizeof(long);
  778. lm->mid_thrs = lm->sec_per_line / 2;
  779. lm->high_thrs = lm->sec_per_line / 4;
  780. lm->meta_distance = (geo->all_luns / 2) * pblk->min_write_pgs;
  781. /* Calculate necessary pages for smeta. See comment over struct
  782. * line_smeta definition
  783. */
  784. i = 1;
  785. add_smeta_page:
  786. lm->smeta_sec = i * geo->ws_opt;
  787. lm->smeta_len = lm->smeta_sec * geo->csecs;
  788. smeta_len = sizeof(struct line_smeta) + lm->lun_bitmap_len;
  789. if (smeta_len > lm->smeta_len) {
  790. i++;
  791. goto add_smeta_page;
  792. }
  793. /* Calculate necessary pages for emeta. See comment over struct
  794. * line_emeta definition
  795. */
  796. i = 1;
  797. add_emeta_page:
  798. lm->emeta_sec[0] = i * geo->ws_opt;
  799. lm->emeta_len[0] = lm->emeta_sec[0] * geo->csecs;
  800. emeta_len = calc_emeta_len(pblk);
  801. if (emeta_len > lm->emeta_len[0]) {
  802. i++;
  803. goto add_emeta_page;
  804. }
  805. lm->emeta_bb = geo->all_luns > i ? geo->all_luns - i : 0;
  806. lm->min_blk_line = 1;
  807. if (geo->all_luns > 1)
  808. lm->min_blk_line += DIV_ROUND_UP(lm->smeta_sec +
  809. lm->emeta_sec[0], geo->clba);
  810. if (lm->min_blk_line > lm->blk_per_line) {
  811. pr_err("pblk: config. not supported. Min. LUN in line:%d\n",
  812. lm->blk_per_line);
  813. return -EINVAL;
  814. }
  815. return 0;
  816. }
  817. static int pblk_lines_init(struct pblk *pblk)
  818. {
  819. struct pblk_line_mgmt *l_mg = &pblk->l_mg;
  820. struct pblk_line *line;
  821. void *chunk_meta;
  822. long nr_free_chks = 0;
  823. int i, ret;
  824. ret = pblk_line_meta_init(pblk);
  825. if (ret)
  826. return ret;
  827. ret = pblk_line_mg_init(pblk);
  828. if (ret)
  829. return ret;
  830. ret = pblk_luns_init(pblk);
  831. if (ret)
  832. goto fail_free_meta;
  833. chunk_meta = pblk_chunk_get_meta(pblk);
  834. if (IS_ERR(chunk_meta)) {
  835. ret = PTR_ERR(chunk_meta);
  836. goto fail_free_luns;
  837. }
  838. pblk->lines = kcalloc(l_mg->nr_lines, sizeof(struct pblk_line),
  839. GFP_KERNEL);
  840. if (!pblk->lines) {
  841. ret = -ENOMEM;
  842. goto fail_free_chunk_meta;
  843. }
  844. for (i = 0; i < l_mg->nr_lines; i++) {
  845. line = &pblk->lines[i];
  846. ret = pblk_alloc_line_meta(pblk, line);
  847. if (ret)
  848. goto fail_free_lines;
  849. nr_free_chks += pblk_setup_line_meta(pblk, line, chunk_meta, i);
  850. }
  851. pblk_set_provision(pblk, nr_free_chks);
  852. kfree(chunk_meta);
  853. return 0;
  854. fail_free_lines:
  855. while (--i >= 0)
  856. pblk_line_meta_free(&pblk->lines[i]);
  857. kfree(pblk->lines);
  858. fail_free_chunk_meta:
  859. kfree(chunk_meta);
  860. fail_free_luns:
  861. kfree(pblk->luns);
  862. fail_free_meta:
  863. pblk_line_mg_free(pblk);
  864. return ret;
  865. }
  866. static int pblk_writer_init(struct pblk *pblk)
  867. {
  868. pblk->writer_ts = kthread_create(pblk_write_ts, pblk, "pblk-writer-t");
  869. if (IS_ERR(pblk->writer_ts)) {
  870. int err = PTR_ERR(pblk->writer_ts);
  871. if (err != -EINTR)
  872. pr_err("pblk: could not allocate writer kthread (%d)\n",
  873. err);
  874. return err;
  875. }
  876. timer_setup(&pblk->wtimer, pblk_write_timer_fn, 0);
  877. mod_timer(&pblk->wtimer, jiffies + msecs_to_jiffies(100));
  878. return 0;
  879. }
  880. static void pblk_writer_stop(struct pblk *pblk)
  881. {
  882. /* The pipeline must be stopped and the write buffer emptied before the
  883. * write thread is stopped
  884. */
  885. WARN(pblk_rb_read_count(&pblk->rwb),
  886. "Stopping not fully persisted write buffer\n");
  887. WARN(pblk_rb_sync_count(&pblk->rwb),
  888. "Stopping not fully synced write buffer\n");
  889. del_timer_sync(&pblk->wtimer);
  890. if (pblk->writer_ts)
  891. kthread_stop(pblk->writer_ts);
  892. }
  893. static void pblk_free(struct pblk *pblk)
  894. {
  895. pblk_lines_free(pblk);
  896. pblk_l2p_free(pblk);
  897. pblk_rwb_free(pblk);
  898. pblk_core_free(pblk);
  899. kfree(pblk);
  900. }
  901. static void pblk_tear_down(struct pblk *pblk)
  902. {
  903. pblk_pipeline_stop(pblk);
  904. pblk_writer_stop(pblk);
  905. pblk_rb_sync_l2p(&pblk->rwb);
  906. pblk_rl_free(&pblk->rl);
  907. pr_debug("pblk: consistent tear down\n");
  908. }
  909. static void pblk_exit(void *private)
  910. {
  911. struct pblk *pblk = private;
  912. down_write(&pblk_lock);
  913. pblk_gc_exit(pblk);
  914. pblk_tear_down(pblk);
  915. #ifdef CONFIG_NVM_DEBUG
  916. pr_info("pblk exit: L2P CRC: %x\n", pblk_l2p_crc(pblk));
  917. #endif
  918. pblk_free(pblk);
  919. up_write(&pblk_lock);
  920. }
  921. static sector_t pblk_capacity(void *private)
  922. {
  923. struct pblk *pblk = private;
  924. return pblk->capacity * NR_PHY_IN_LOG;
  925. }
  926. static void *pblk_init(struct nvm_tgt_dev *dev, struct gendisk *tdisk,
  927. int flags)
  928. {
  929. struct nvm_geo *geo = &dev->geo;
  930. struct request_queue *bqueue = dev->q;
  931. struct request_queue *tqueue = tdisk->queue;
  932. struct pblk *pblk;
  933. int ret;
  934. /* pblk supports 1.2 and 2.0 versions */
  935. if (!(geo->version == NVM_OCSSD_SPEC_12 ||
  936. geo->version == NVM_OCSSD_SPEC_20)) {
  937. pr_err("pblk: OCSSD version not supported (%u)\n",
  938. geo->version);
  939. return ERR_PTR(-EINVAL);
  940. }
  941. if (geo->version == NVM_OCSSD_SPEC_12 && geo->dom & NVM_RSP_L2P) {
  942. pr_err("pblk: host-side L2P table not supported. (%x)\n",
  943. geo->dom);
  944. return ERR_PTR(-EINVAL);
  945. }
  946. pblk = kzalloc(sizeof(struct pblk), GFP_KERNEL);
  947. if (!pblk)
  948. return ERR_PTR(-ENOMEM);
  949. pblk->dev = dev;
  950. pblk->disk = tdisk;
  951. pblk->state = PBLK_STATE_RUNNING;
  952. pblk->gc.gc_enabled = 0;
  953. spin_lock_init(&pblk->trans_lock);
  954. spin_lock_init(&pblk->lock);
  955. #ifdef CONFIG_NVM_DEBUG
  956. atomic_long_set(&pblk->inflight_writes, 0);
  957. atomic_long_set(&pblk->padded_writes, 0);
  958. atomic_long_set(&pblk->padded_wb, 0);
  959. atomic_long_set(&pblk->req_writes, 0);
  960. atomic_long_set(&pblk->sub_writes, 0);
  961. atomic_long_set(&pblk->sync_writes, 0);
  962. atomic_long_set(&pblk->inflight_reads, 0);
  963. atomic_long_set(&pblk->cache_reads, 0);
  964. atomic_long_set(&pblk->sync_reads, 0);
  965. atomic_long_set(&pblk->recov_writes, 0);
  966. atomic_long_set(&pblk->recov_writes, 0);
  967. atomic_long_set(&pblk->recov_gc_writes, 0);
  968. atomic_long_set(&pblk->recov_gc_reads, 0);
  969. #endif
  970. atomic_long_set(&pblk->read_failed, 0);
  971. atomic_long_set(&pblk->read_empty, 0);
  972. atomic_long_set(&pblk->read_high_ecc, 0);
  973. atomic_long_set(&pblk->read_failed_gc, 0);
  974. atomic_long_set(&pblk->write_failed, 0);
  975. atomic_long_set(&pblk->erase_failed, 0);
  976. ret = pblk_core_init(pblk);
  977. if (ret) {
  978. pr_err("pblk: could not initialize core\n");
  979. goto fail;
  980. }
  981. ret = pblk_lines_init(pblk);
  982. if (ret) {
  983. pr_err("pblk: could not initialize lines\n");
  984. goto fail_free_core;
  985. }
  986. ret = pblk_rwb_init(pblk);
  987. if (ret) {
  988. pr_err("pblk: could not initialize write buffer\n");
  989. goto fail_free_lines;
  990. }
  991. ret = pblk_l2p_init(pblk, flags & NVM_TARGET_FACTORY);
  992. if (ret) {
  993. pr_err("pblk: could not initialize maps\n");
  994. goto fail_free_rwb;
  995. }
  996. ret = pblk_writer_init(pblk);
  997. if (ret) {
  998. if (ret != -EINTR)
  999. pr_err("pblk: could not initialize write thread\n");
  1000. goto fail_free_l2p;
  1001. }
  1002. ret = pblk_gc_init(pblk);
  1003. if (ret) {
  1004. pr_err("pblk: could not initialize gc\n");
  1005. goto fail_stop_writer;
  1006. }
  1007. /* inherit the size from the underlying device */
  1008. blk_queue_logical_block_size(tqueue, queue_physical_block_size(bqueue));
  1009. blk_queue_max_hw_sectors(tqueue, queue_max_hw_sectors(bqueue));
  1010. blk_queue_write_cache(tqueue, true, false);
  1011. tqueue->limits.discard_granularity = geo->clba * geo->csecs;
  1012. tqueue->limits.discard_alignment = 0;
  1013. blk_queue_max_discard_sectors(tqueue, UINT_MAX >> 9);
  1014. blk_queue_flag_set(QUEUE_FLAG_DISCARD, tqueue);
  1015. pr_info("pblk(%s): luns:%u, lines:%d, secs:%llu, buf entries:%u\n",
  1016. tdisk->disk_name,
  1017. geo->all_luns, pblk->l_mg.nr_lines,
  1018. (unsigned long long)pblk->rl.nr_secs,
  1019. pblk->rwb.nr_entries);
  1020. wake_up_process(pblk->writer_ts);
  1021. /* Check if we need to start GC */
  1022. pblk_gc_should_kick(pblk);
  1023. return pblk;
  1024. fail_stop_writer:
  1025. pblk_writer_stop(pblk);
  1026. fail_free_l2p:
  1027. pblk_l2p_free(pblk);
  1028. fail_free_rwb:
  1029. pblk_rwb_free(pblk);
  1030. fail_free_lines:
  1031. pblk_lines_free(pblk);
  1032. fail_free_core:
  1033. pblk_core_free(pblk);
  1034. fail:
  1035. kfree(pblk);
  1036. return ERR_PTR(ret);
  1037. }
  1038. /* physical block device target */
  1039. static struct nvm_tgt_type tt_pblk = {
  1040. .name = "pblk",
  1041. .version = {1, 0, 0},
  1042. .make_rq = pblk_make_rq,
  1043. .capacity = pblk_capacity,
  1044. .init = pblk_init,
  1045. .exit = pblk_exit,
  1046. .sysfs_init = pblk_sysfs_init,
  1047. .sysfs_exit = pblk_sysfs_exit,
  1048. .owner = THIS_MODULE,
  1049. };
  1050. static int __init pblk_module_init(void)
  1051. {
  1052. int ret;
  1053. pblk_bio_set = bioset_create(BIO_POOL_SIZE, 0, 0);
  1054. if (!pblk_bio_set)
  1055. return -ENOMEM;
  1056. ret = nvm_register_tgt_type(&tt_pblk);
  1057. if (ret)
  1058. bioset_free(pblk_bio_set);
  1059. return ret;
  1060. }
  1061. static void pblk_module_exit(void)
  1062. {
  1063. bioset_free(pblk_bio_set);
  1064. nvm_unregister_tgt_type(&tt_pblk);
  1065. }
  1066. module_init(pblk_module_init);
  1067. module_exit(pblk_module_exit);
  1068. MODULE_AUTHOR("Javier Gonzalez <javier@cnexlabs.com>");
  1069. MODULE_AUTHOR("Matias Bjorling <matias@cnexlabs.com>");
  1070. MODULE_LICENSE("GPL v2");
  1071. MODULE_DESCRIPTION("Physical Block-Device for Open-Channel SSDs");