pblk-core.c 52 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  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. * pblk-core.c - pblk's core functionality
  17. *
  18. */
  19. #define CREATE_TRACE_POINTS
  20. #include "pblk.h"
  21. #include "pblk-trace.h"
  22. static void pblk_line_mark_bb(struct work_struct *work)
  23. {
  24. struct pblk_line_ws *line_ws = container_of(work, struct pblk_line_ws,
  25. ws);
  26. struct pblk *pblk = line_ws->pblk;
  27. struct nvm_tgt_dev *dev = pblk->dev;
  28. struct ppa_addr *ppa = line_ws->priv;
  29. int ret;
  30. ret = nvm_set_chunk_meta(dev, ppa, 1, NVM_BLK_T_GRWN_BAD);
  31. if (ret) {
  32. struct pblk_line *line;
  33. int pos;
  34. line = pblk_ppa_to_line(pblk, *ppa);
  35. pos = pblk_ppa_to_pos(&dev->geo, *ppa);
  36. pblk_err(pblk, "failed to mark bb, line:%d, pos:%d\n",
  37. line->id, pos);
  38. }
  39. kfree(ppa);
  40. mempool_free(line_ws, &pblk->gen_ws_pool);
  41. }
  42. static void pblk_mark_bb(struct pblk *pblk, struct pblk_line *line,
  43. struct ppa_addr ppa_addr)
  44. {
  45. struct nvm_tgt_dev *dev = pblk->dev;
  46. struct nvm_geo *geo = &dev->geo;
  47. struct ppa_addr *ppa;
  48. int pos = pblk_ppa_to_pos(geo, ppa_addr);
  49. pblk_debug(pblk, "erase failed: line:%d, pos:%d\n", line->id, pos);
  50. atomic_long_inc(&pblk->erase_failed);
  51. atomic_dec(&line->blk_in_line);
  52. if (test_and_set_bit(pos, line->blk_bitmap))
  53. pblk_err(pblk, "attempted to erase bb: line:%d, pos:%d\n",
  54. line->id, pos);
  55. /* Not necessary to mark bad blocks on 2.0 spec. */
  56. if (geo->version == NVM_OCSSD_SPEC_20)
  57. return;
  58. ppa = kmalloc(sizeof(struct ppa_addr), GFP_ATOMIC);
  59. if (!ppa)
  60. return;
  61. *ppa = ppa_addr;
  62. pblk_gen_run_ws(pblk, NULL, ppa, pblk_line_mark_bb,
  63. GFP_ATOMIC, pblk->bb_wq);
  64. }
  65. static void __pblk_end_io_erase(struct pblk *pblk, struct nvm_rq *rqd)
  66. {
  67. struct nvm_tgt_dev *dev = pblk->dev;
  68. struct nvm_geo *geo = &dev->geo;
  69. struct nvm_chk_meta *chunk;
  70. struct pblk_line *line;
  71. int pos;
  72. line = pblk_ppa_to_line(pblk, rqd->ppa_addr);
  73. pos = pblk_ppa_to_pos(geo, rqd->ppa_addr);
  74. chunk = &line->chks[pos];
  75. atomic_dec(&line->left_seblks);
  76. if (rqd->error) {
  77. trace_pblk_chunk_reset(pblk_disk_name(pblk),
  78. &rqd->ppa_addr, PBLK_CHUNK_RESET_FAILED);
  79. chunk->state = NVM_CHK_ST_OFFLINE;
  80. pblk_mark_bb(pblk, line, rqd->ppa_addr);
  81. } else {
  82. trace_pblk_chunk_reset(pblk_disk_name(pblk),
  83. &rqd->ppa_addr, PBLK_CHUNK_RESET_DONE);
  84. chunk->state = NVM_CHK_ST_FREE;
  85. }
  86. trace_pblk_chunk_state(pblk_disk_name(pblk), &rqd->ppa_addr,
  87. chunk->state);
  88. atomic_dec(&pblk->inflight_io);
  89. }
  90. /* Erase completion assumes that only one block is erased at the time */
  91. static void pblk_end_io_erase(struct nvm_rq *rqd)
  92. {
  93. struct pblk *pblk = rqd->private;
  94. __pblk_end_io_erase(pblk, rqd);
  95. mempool_free(rqd, &pblk->e_rq_pool);
  96. }
  97. /*
  98. * Get information for all chunks from the device.
  99. *
  100. * The caller is responsible for freeing (vmalloc) the returned structure
  101. */
  102. struct nvm_chk_meta *pblk_get_chunk_meta(struct pblk *pblk)
  103. {
  104. struct nvm_tgt_dev *dev = pblk->dev;
  105. struct nvm_geo *geo = &dev->geo;
  106. struct nvm_chk_meta *meta;
  107. struct ppa_addr ppa;
  108. unsigned long len;
  109. int ret;
  110. ppa.ppa = 0;
  111. len = geo->all_chunks * sizeof(*meta);
  112. meta = vzalloc(len);
  113. if (!meta)
  114. return ERR_PTR(-ENOMEM);
  115. ret = nvm_get_chunk_meta(dev, ppa, geo->all_chunks, meta);
  116. if (ret) {
  117. vfree(meta);
  118. return ERR_PTR(-EIO);
  119. }
  120. return meta;
  121. }
  122. struct nvm_chk_meta *pblk_chunk_get_off(struct pblk *pblk,
  123. struct nvm_chk_meta *meta,
  124. struct ppa_addr ppa)
  125. {
  126. struct nvm_tgt_dev *dev = pblk->dev;
  127. struct nvm_geo *geo = &dev->geo;
  128. int ch_off = ppa.m.grp * geo->num_chk * geo->num_lun;
  129. int lun_off = ppa.m.pu * geo->num_chk;
  130. int chk_off = ppa.m.chk;
  131. return meta + ch_off + lun_off + chk_off;
  132. }
  133. void __pblk_map_invalidate(struct pblk *pblk, struct pblk_line *line,
  134. u64 paddr)
  135. {
  136. struct pblk_line_mgmt *l_mg = &pblk->l_mg;
  137. struct list_head *move_list = NULL;
  138. /* Lines being reclaimed (GC'ed) cannot be invalidated. Before the L2P
  139. * table is modified with reclaimed sectors, a check is done to endure
  140. * that newer updates are not overwritten.
  141. */
  142. spin_lock(&line->lock);
  143. WARN_ON(line->state == PBLK_LINESTATE_FREE);
  144. if (test_and_set_bit(paddr, line->invalid_bitmap)) {
  145. WARN_ONCE(1, "pblk: double invalidate\n");
  146. spin_unlock(&line->lock);
  147. return;
  148. }
  149. le32_add_cpu(line->vsc, -1);
  150. if (line->state == PBLK_LINESTATE_CLOSED)
  151. move_list = pblk_line_gc_list(pblk, line);
  152. spin_unlock(&line->lock);
  153. if (move_list) {
  154. spin_lock(&l_mg->gc_lock);
  155. spin_lock(&line->lock);
  156. /* Prevent moving a line that has just been chosen for GC */
  157. if (line->state == PBLK_LINESTATE_GC) {
  158. spin_unlock(&line->lock);
  159. spin_unlock(&l_mg->gc_lock);
  160. return;
  161. }
  162. spin_unlock(&line->lock);
  163. list_move_tail(&line->list, move_list);
  164. spin_unlock(&l_mg->gc_lock);
  165. }
  166. }
  167. void pblk_map_invalidate(struct pblk *pblk, struct ppa_addr ppa)
  168. {
  169. struct pblk_line *line;
  170. u64 paddr;
  171. #ifdef CONFIG_NVM_PBLK_DEBUG
  172. /* Callers must ensure that the ppa points to a device address */
  173. BUG_ON(pblk_addr_in_cache(ppa));
  174. BUG_ON(pblk_ppa_empty(ppa));
  175. #endif
  176. line = pblk_ppa_to_line(pblk, ppa);
  177. paddr = pblk_dev_ppa_to_line_addr(pblk, ppa);
  178. __pblk_map_invalidate(pblk, line, paddr);
  179. }
  180. static void pblk_invalidate_range(struct pblk *pblk, sector_t slba,
  181. unsigned int nr_secs)
  182. {
  183. sector_t lba;
  184. spin_lock(&pblk->trans_lock);
  185. for (lba = slba; lba < slba + nr_secs; lba++) {
  186. struct ppa_addr ppa;
  187. ppa = pblk_trans_map_get(pblk, lba);
  188. if (!pblk_addr_in_cache(ppa) && !pblk_ppa_empty(ppa))
  189. pblk_map_invalidate(pblk, ppa);
  190. pblk_ppa_set_empty(&ppa);
  191. pblk_trans_map_set(pblk, lba, ppa);
  192. }
  193. spin_unlock(&pblk->trans_lock);
  194. }
  195. int pblk_alloc_rqd_meta(struct pblk *pblk, struct nvm_rq *rqd)
  196. {
  197. struct nvm_tgt_dev *dev = pblk->dev;
  198. rqd->meta_list = nvm_dev_dma_alloc(dev->parent, GFP_KERNEL,
  199. &rqd->dma_meta_list);
  200. if (!rqd->meta_list)
  201. return -ENOMEM;
  202. if (rqd->nr_ppas == 1)
  203. return 0;
  204. rqd->ppa_list = rqd->meta_list + pblk_dma_meta_size(pblk);
  205. rqd->dma_ppa_list = rqd->dma_meta_list + pblk_dma_meta_size(pblk);
  206. return 0;
  207. }
  208. void pblk_free_rqd_meta(struct pblk *pblk, struct nvm_rq *rqd)
  209. {
  210. struct nvm_tgt_dev *dev = pblk->dev;
  211. if (rqd->meta_list)
  212. nvm_dev_dma_free(dev->parent, rqd->meta_list,
  213. rqd->dma_meta_list);
  214. }
  215. /* Caller must guarantee that the request is a valid type */
  216. struct nvm_rq *pblk_alloc_rqd(struct pblk *pblk, int type)
  217. {
  218. mempool_t *pool;
  219. struct nvm_rq *rqd;
  220. int rq_size;
  221. switch (type) {
  222. case PBLK_WRITE:
  223. case PBLK_WRITE_INT:
  224. pool = &pblk->w_rq_pool;
  225. rq_size = pblk_w_rq_size;
  226. break;
  227. case PBLK_READ:
  228. pool = &pblk->r_rq_pool;
  229. rq_size = pblk_g_rq_size;
  230. break;
  231. default:
  232. pool = &pblk->e_rq_pool;
  233. rq_size = pblk_g_rq_size;
  234. }
  235. rqd = mempool_alloc(pool, GFP_KERNEL);
  236. memset(rqd, 0, rq_size);
  237. return rqd;
  238. }
  239. /* Typically used on completion path. Cannot guarantee request consistency */
  240. void pblk_free_rqd(struct pblk *pblk, struct nvm_rq *rqd, int type)
  241. {
  242. mempool_t *pool;
  243. switch (type) {
  244. case PBLK_WRITE:
  245. kfree(((struct pblk_c_ctx *)nvm_rq_to_pdu(rqd))->lun_bitmap);
  246. /* fall through */
  247. case PBLK_WRITE_INT:
  248. pool = &pblk->w_rq_pool;
  249. break;
  250. case PBLK_READ:
  251. pool = &pblk->r_rq_pool;
  252. break;
  253. case PBLK_ERASE:
  254. pool = &pblk->e_rq_pool;
  255. break;
  256. default:
  257. pblk_err(pblk, "trying to free unknown rqd type\n");
  258. return;
  259. }
  260. pblk_free_rqd_meta(pblk, rqd);
  261. mempool_free(rqd, pool);
  262. }
  263. void pblk_bio_free_pages(struct pblk *pblk, struct bio *bio, int off,
  264. int nr_pages)
  265. {
  266. struct bio_vec *bv;
  267. struct page *page;
  268. int i, e, nbv = 0;
  269. for (i = 0; i < bio->bi_vcnt; i++) {
  270. bv = &bio->bi_io_vec[i];
  271. page = bv->bv_page;
  272. for (e = 0; e < bv->bv_len; e += PBLK_EXPOSED_PAGE_SIZE, nbv++)
  273. if (nbv >= off)
  274. mempool_free(page++, &pblk->page_bio_pool);
  275. }
  276. }
  277. int pblk_bio_add_pages(struct pblk *pblk, struct bio *bio, gfp_t flags,
  278. int nr_pages)
  279. {
  280. struct request_queue *q = pblk->dev->q;
  281. struct page *page;
  282. int i, ret;
  283. for (i = 0; i < nr_pages; i++) {
  284. page = mempool_alloc(&pblk->page_bio_pool, flags);
  285. ret = bio_add_pc_page(q, bio, page, PBLK_EXPOSED_PAGE_SIZE, 0);
  286. if (ret != PBLK_EXPOSED_PAGE_SIZE) {
  287. pblk_err(pblk, "could not add page to bio\n");
  288. mempool_free(page, &pblk->page_bio_pool);
  289. goto err;
  290. }
  291. }
  292. return 0;
  293. err:
  294. pblk_bio_free_pages(pblk, bio, (bio->bi_vcnt - i), i);
  295. return -1;
  296. }
  297. void pblk_write_kick(struct pblk *pblk)
  298. {
  299. wake_up_process(pblk->writer_ts);
  300. mod_timer(&pblk->wtimer, jiffies + msecs_to_jiffies(1000));
  301. }
  302. void pblk_write_timer_fn(struct timer_list *t)
  303. {
  304. struct pblk *pblk = from_timer(pblk, t, wtimer);
  305. /* kick the write thread every tick to flush outstanding data */
  306. pblk_write_kick(pblk);
  307. }
  308. void pblk_write_should_kick(struct pblk *pblk)
  309. {
  310. unsigned int secs_avail = pblk_rb_read_count(&pblk->rwb);
  311. if (secs_avail >= pblk->min_write_pgs_data)
  312. pblk_write_kick(pblk);
  313. }
  314. static void pblk_wait_for_meta(struct pblk *pblk)
  315. {
  316. do {
  317. if (!atomic_read(&pblk->inflight_io))
  318. break;
  319. schedule();
  320. } while (1);
  321. }
  322. static void pblk_flush_writer(struct pblk *pblk)
  323. {
  324. pblk_rb_flush(&pblk->rwb);
  325. do {
  326. if (!pblk_rb_sync_count(&pblk->rwb))
  327. break;
  328. pblk_write_kick(pblk);
  329. schedule();
  330. } while (1);
  331. }
  332. struct list_head *pblk_line_gc_list(struct pblk *pblk, struct pblk_line *line)
  333. {
  334. struct pblk_line_meta *lm = &pblk->lm;
  335. struct pblk_line_mgmt *l_mg = &pblk->l_mg;
  336. struct list_head *move_list = NULL;
  337. int packed_meta = (le32_to_cpu(*line->vsc) / pblk->min_write_pgs_data)
  338. * (pblk->min_write_pgs - pblk->min_write_pgs_data);
  339. int vsc = le32_to_cpu(*line->vsc) + packed_meta;
  340. lockdep_assert_held(&line->lock);
  341. if (line->w_err_gc->has_write_err) {
  342. if (line->gc_group != PBLK_LINEGC_WERR) {
  343. line->gc_group = PBLK_LINEGC_WERR;
  344. move_list = &l_mg->gc_werr_list;
  345. pblk_rl_werr_line_in(&pblk->rl);
  346. }
  347. } else if (!vsc) {
  348. if (line->gc_group != PBLK_LINEGC_FULL) {
  349. line->gc_group = PBLK_LINEGC_FULL;
  350. move_list = &l_mg->gc_full_list;
  351. }
  352. } else if (vsc < lm->high_thrs) {
  353. if (line->gc_group != PBLK_LINEGC_HIGH) {
  354. line->gc_group = PBLK_LINEGC_HIGH;
  355. move_list = &l_mg->gc_high_list;
  356. }
  357. } else if (vsc < lm->mid_thrs) {
  358. if (line->gc_group != PBLK_LINEGC_MID) {
  359. line->gc_group = PBLK_LINEGC_MID;
  360. move_list = &l_mg->gc_mid_list;
  361. }
  362. } else if (vsc < line->sec_in_line) {
  363. if (line->gc_group != PBLK_LINEGC_LOW) {
  364. line->gc_group = PBLK_LINEGC_LOW;
  365. move_list = &l_mg->gc_low_list;
  366. }
  367. } else if (vsc == line->sec_in_line) {
  368. if (line->gc_group != PBLK_LINEGC_EMPTY) {
  369. line->gc_group = PBLK_LINEGC_EMPTY;
  370. move_list = &l_mg->gc_empty_list;
  371. }
  372. } else {
  373. line->state = PBLK_LINESTATE_CORRUPT;
  374. trace_pblk_line_state(pblk_disk_name(pblk), line->id,
  375. line->state);
  376. line->gc_group = PBLK_LINEGC_NONE;
  377. move_list = &l_mg->corrupt_list;
  378. pblk_err(pblk, "corrupted vsc for line %d, vsc:%d (%d/%d/%d)\n",
  379. line->id, vsc,
  380. line->sec_in_line,
  381. lm->high_thrs, lm->mid_thrs);
  382. }
  383. return move_list;
  384. }
  385. void pblk_discard(struct pblk *pblk, struct bio *bio)
  386. {
  387. sector_t slba = pblk_get_lba(bio);
  388. sector_t nr_secs = pblk_get_secs(bio);
  389. pblk_invalidate_range(pblk, slba, nr_secs);
  390. }
  391. void pblk_log_write_err(struct pblk *pblk, struct nvm_rq *rqd)
  392. {
  393. atomic_long_inc(&pblk->write_failed);
  394. #ifdef CONFIG_NVM_PBLK_DEBUG
  395. pblk_print_failed_rqd(pblk, rqd, rqd->error);
  396. #endif
  397. }
  398. void pblk_log_read_err(struct pblk *pblk, struct nvm_rq *rqd)
  399. {
  400. /* Empty page read is not necessarily an error (e.g., L2P recovery) */
  401. if (rqd->error == NVM_RSP_ERR_EMPTYPAGE) {
  402. atomic_long_inc(&pblk->read_empty);
  403. return;
  404. }
  405. switch (rqd->error) {
  406. case NVM_RSP_WARN_HIGHECC:
  407. atomic_long_inc(&pblk->read_high_ecc);
  408. break;
  409. case NVM_RSP_ERR_FAILECC:
  410. case NVM_RSP_ERR_FAILCRC:
  411. atomic_long_inc(&pblk->read_failed);
  412. break;
  413. default:
  414. pblk_err(pblk, "unknown read error:%d\n", rqd->error);
  415. }
  416. #ifdef CONFIG_NVM_PBLK_DEBUG
  417. pblk_print_failed_rqd(pblk, rqd, rqd->error);
  418. #endif
  419. }
  420. void pblk_set_sec_per_write(struct pblk *pblk, int sec_per_write)
  421. {
  422. pblk->sec_per_write = sec_per_write;
  423. }
  424. int pblk_submit_io(struct pblk *pblk, struct nvm_rq *rqd, void *buf)
  425. {
  426. struct nvm_tgt_dev *dev = pblk->dev;
  427. atomic_inc(&pblk->inflight_io);
  428. #ifdef CONFIG_NVM_PBLK_DEBUG
  429. if (pblk_check_io(pblk, rqd))
  430. return NVM_IO_ERR;
  431. #endif
  432. return nvm_submit_io(dev, rqd, buf);
  433. }
  434. void pblk_check_chunk_state_update(struct pblk *pblk, struct nvm_rq *rqd)
  435. {
  436. struct ppa_addr *ppa_list = nvm_rq_to_ppa_list(rqd);
  437. int i;
  438. for (i = 0; i < rqd->nr_ppas; i++) {
  439. struct ppa_addr *ppa = &ppa_list[i];
  440. struct nvm_chk_meta *chunk = pblk_dev_ppa_to_chunk(pblk, *ppa);
  441. u64 caddr = pblk_dev_ppa_to_chunk_addr(pblk, *ppa);
  442. if (caddr == 0)
  443. trace_pblk_chunk_state(pblk_disk_name(pblk),
  444. ppa, NVM_CHK_ST_OPEN);
  445. else if (caddr == (chunk->cnlb - 1))
  446. trace_pblk_chunk_state(pblk_disk_name(pblk),
  447. ppa, NVM_CHK_ST_CLOSED);
  448. }
  449. }
  450. int pblk_submit_io_sync(struct pblk *pblk, struct nvm_rq *rqd, void *buf)
  451. {
  452. struct nvm_tgt_dev *dev = pblk->dev;
  453. int ret;
  454. atomic_inc(&pblk->inflight_io);
  455. #ifdef CONFIG_NVM_PBLK_DEBUG
  456. if (pblk_check_io(pblk, rqd))
  457. return NVM_IO_ERR;
  458. #endif
  459. ret = nvm_submit_io_sync(dev, rqd, buf);
  460. if (trace_pblk_chunk_state_enabled() && !ret &&
  461. rqd->opcode == NVM_OP_PWRITE)
  462. pblk_check_chunk_state_update(pblk, rqd);
  463. return ret;
  464. }
  465. static int pblk_submit_io_sync_sem(struct pblk *pblk, struct nvm_rq *rqd,
  466. void *buf)
  467. {
  468. struct ppa_addr *ppa_list = nvm_rq_to_ppa_list(rqd);
  469. int ret;
  470. pblk_down_chunk(pblk, ppa_list[0]);
  471. ret = pblk_submit_io_sync(pblk, rqd, buf);
  472. pblk_up_chunk(pblk, ppa_list[0]);
  473. return ret;
  474. }
  475. int pblk_calc_secs(struct pblk *pblk, unsigned long secs_avail,
  476. unsigned long secs_to_flush, bool skip_meta)
  477. {
  478. int max = pblk->sec_per_write;
  479. int min = pblk->min_write_pgs;
  480. int secs_to_sync = 0;
  481. if (skip_meta && pblk->min_write_pgs_data != pblk->min_write_pgs)
  482. min = max = pblk->min_write_pgs_data;
  483. if (secs_avail >= max)
  484. secs_to_sync = max;
  485. else if (secs_avail >= min)
  486. secs_to_sync = min * (secs_avail / min);
  487. else if (secs_to_flush)
  488. secs_to_sync = min;
  489. return secs_to_sync;
  490. }
  491. void pblk_dealloc_page(struct pblk *pblk, struct pblk_line *line, int nr_secs)
  492. {
  493. u64 addr;
  494. int i;
  495. spin_lock(&line->lock);
  496. addr = find_next_zero_bit(line->map_bitmap,
  497. pblk->lm.sec_per_line, line->cur_sec);
  498. line->cur_sec = addr - nr_secs;
  499. for (i = 0; i < nr_secs; i++, line->cur_sec--)
  500. WARN_ON(!test_and_clear_bit(line->cur_sec, line->map_bitmap));
  501. spin_unlock(&line->lock);
  502. }
  503. u64 __pblk_alloc_page(struct pblk *pblk, struct pblk_line *line, int nr_secs)
  504. {
  505. u64 addr;
  506. int i;
  507. lockdep_assert_held(&line->lock);
  508. /* logic error: ppa out-of-bounds. Prevent generating bad address */
  509. if (line->cur_sec + nr_secs > pblk->lm.sec_per_line) {
  510. WARN(1, "pblk: page allocation out of bounds\n");
  511. nr_secs = pblk->lm.sec_per_line - line->cur_sec;
  512. }
  513. line->cur_sec = addr = find_next_zero_bit(line->map_bitmap,
  514. pblk->lm.sec_per_line, line->cur_sec);
  515. for (i = 0; i < nr_secs; i++, line->cur_sec++)
  516. WARN_ON(test_and_set_bit(line->cur_sec, line->map_bitmap));
  517. return addr;
  518. }
  519. u64 pblk_alloc_page(struct pblk *pblk, struct pblk_line *line, int nr_secs)
  520. {
  521. u64 addr;
  522. /* Lock needed in case a write fails and a recovery needs to remap
  523. * failed write buffer entries
  524. */
  525. spin_lock(&line->lock);
  526. addr = __pblk_alloc_page(pblk, line, nr_secs);
  527. line->left_msecs -= nr_secs;
  528. WARN(line->left_msecs < 0, "pblk: page allocation out of bounds\n");
  529. spin_unlock(&line->lock);
  530. return addr;
  531. }
  532. u64 pblk_lookup_page(struct pblk *pblk, struct pblk_line *line)
  533. {
  534. u64 paddr;
  535. spin_lock(&line->lock);
  536. paddr = find_next_zero_bit(line->map_bitmap,
  537. pblk->lm.sec_per_line, line->cur_sec);
  538. spin_unlock(&line->lock);
  539. return paddr;
  540. }
  541. u64 pblk_line_smeta_start(struct pblk *pblk, struct pblk_line *line)
  542. {
  543. struct nvm_tgt_dev *dev = pblk->dev;
  544. struct nvm_geo *geo = &dev->geo;
  545. struct pblk_line_meta *lm = &pblk->lm;
  546. int bit;
  547. /* This usually only happens on bad lines */
  548. bit = find_first_zero_bit(line->blk_bitmap, lm->blk_per_line);
  549. if (bit >= lm->blk_per_line)
  550. return -1;
  551. return bit * geo->ws_opt;
  552. }
  553. int pblk_line_smeta_read(struct pblk *pblk, struct pblk_line *line)
  554. {
  555. struct pblk_line_meta *lm = &pblk->lm;
  556. struct ppa_addr *ppa_list;
  557. struct nvm_rq rqd;
  558. u64 paddr = pblk_line_smeta_start(pblk, line);
  559. int i, ret;
  560. memset(&rqd, 0, sizeof(struct nvm_rq));
  561. ret = pblk_alloc_rqd_meta(pblk, &rqd);
  562. if (ret)
  563. return ret;
  564. rqd.opcode = NVM_OP_PREAD;
  565. rqd.nr_ppas = lm->smeta_sec;
  566. rqd.is_seq = 1;
  567. ppa_list = nvm_rq_to_ppa_list(&rqd);
  568. for (i = 0; i < lm->smeta_sec; i++, paddr++)
  569. ppa_list[i] = addr_to_gen_ppa(pblk, paddr, line->id);
  570. ret = pblk_submit_io_sync(pblk, &rqd, line->smeta);
  571. if (ret) {
  572. pblk_err(pblk, "smeta I/O submission failed: %d\n", ret);
  573. goto clear_rqd;
  574. }
  575. atomic_dec(&pblk->inflight_io);
  576. if (rqd.error && rqd.error != NVM_RSP_WARN_HIGHECC) {
  577. pblk_log_read_err(pblk, &rqd);
  578. ret = -EIO;
  579. }
  580. clear_rqd:
  581. pblk_free_rqd_meta(pblk, &rqd);
  582. return ret;
  583. }
  584. static int pblk_line_smeta_write(struct pblk *pblk, struct pblk_line *line,
  585. u64 paddr)
  586. {
  587. struct pblk_line_meta *lm = &pblk->lm;
  588. struct ppa_addr *ppa_list;
  589. struct nvm_rq rqd;
  590. __le64 *lba_list = emeta_to_lbas(pblk, line->emeta->buf);
  591. __le64 addr_empty = cpu_to_le64(ADDR_EMPTY);
  592. int i, ret;
  593. memset(&rqd, 0, sizeof(struct nvm_rq));
  594. ret = pblk_alloc_rqd_meta(pblk, &rqd);
  595. if (ret)
  596. return ret;
  597. rqd.opcode = NVM_OP_PWRITE;
  598. rqd.nr_ppas = lm->smeta_sec;
  599. rqd.is_seq = 1;
  600. ppa_list = nvm_rq_to_ppa_list(&rqd);
  601. for (i = 0; i < lm->smeta_sec; i++, paddr++) {
  602. struct pblk_sec_meta *meta = pblk_get_meta(pblk,
  603. rqd.meta_list, i);
  604. ppa_list[i] = addr_to_gen_ppa(pblk, paddr, line->id);
  605. meta->lba = lba_list[paddr] = addr_empty;
  606. }
  607. ret = pblk_submit_io_sync_sem(pblk, &rqd, line->smeta);
  608. if (ret) {
  609. pblk_err(pblk, "smeta I/O submission failed: %d\n", ret);
  610. goto clear_rqd;
  611. }
  612. atomic_dec(&pblk->inflight_io);
  613. if (rqd.error) {
  614. pblk_log_write_err(pblk, &rqd);
  615. ret = -EIO;
  616. }
  617. clear_rqd:
  618. pblk_free_rqd_meta(pblk, &rqd);
  619. return ret;
  620. }
  621. int pblk_line_emeta_read(struct pblk *pblk, struct pblk_line *line,
  622. void *emeta_buf)
  623. {
  624. struct nvm_tgt_dev *dev = pblk->dev;
  625. struct nvm_geo *geo = &dev->geo;
  626. struct pblk_line_meta *lm = &pblk->lm;
  627. void *ppa_list_buf, *meta_list;
  628. struct ppa_addr *ppa_list;
  629. struct nvm_rq rqd;
  630. u64 paddr = line->emeta_ssec;
  631. dma_addr_t dma_ppa_list, dma_meta_list;
  632. int min = pblk->min_write_pgs;
  633. int left_ppas = lm->emeta_sec[0];
  634. int line_id = line->id;
  635. int rq_ppas, rq_len;
  636. int i, j;
  637. int ret;
  638. meta_list = nvm_dev_dma_alloc(dev->parent, GFP_KERNEL,
  639. &dma_meta_list);
  640. if (!meta_list)
  641. return -ENOMEM;
  642. ppa_list_buf = meta_list + pblk_dma_meta_size(pblk);
  643. dma_ppa_list = dma_meta_list + pblk_dma_meta_size(pblk);
  644. next_rq:
  645. memset(&rqd, 0, sizeof(struct nvm_rq));
  646. rq_ppas = pblk_calc_secs(pblk, left_ppas, 0, false);
  647. rq_len = rq_ppas * geo->csecs;
  648. rqd.meta_list = meta_list;
  649. rqd.ppa_list = ppa_list_buf;
  650. rqd.dma_meta_list = dma_meta_list;
  651. rqd.dma_ppa_list = dma_ppa_list;
  652. rqd.opcode = NVM_OP_PREAD;
  653. rqd.nr_ppas = rq_ppas;
  654. ppa_list = nvm_rq_to_ppa_list(&rqd);
  655. for (i = 0; i < rqd.nr_ppas; ) {
  656. struct ppa_addr ppa = addr_to_gen_ppa(pblk, paddr, line_id);
  657. int pos = pblk_ppa_to_pos(geo, ppa);
  658. if (pblk_io_aligned(pblk, rq_ppas))
  659. rqd.is_seq = 1;
  660. while (test_bit(pos, line->blk_bitmap)) {
  661. paddr += min;
  662. if (pblk_boundary_paddr_checks(pblk, paddr)) {
  663. ret = -EINTR;
  664. goto free_rqd_dma;
  665. }
  666. ppa = addr_to_gen_ppa(pblk, paddr, line_id);
  667. pos = pblk_ppa_to_pos(geo, ppa);
  668. }
  669. if (pblk_boundary_paddr_checks(pblk, paddr + min)) {
  670. ret = -EINTR;
  671. goto free_rqd_dma;
  672. }
  673. for (j = 0; j < min; j++, i++, paddr++)
  674. ppa_list[i] = addr_to_gen_ppa(pblk, paddr, line_id);
  675. }
  676. ret = pblk_submit_io_sync(pblk, &rqd, emeta_buf);
  677. if (ret) {
  678. pblk_err(pblk, "emeta I/O submission failed: %d\n", ret);
  679. goto free_rqd_dma;
  680. }
  681. atomic_dec(&pblk->inflight_io);
  682. if (rqd.error && rqd.error != NVM_RSP_WARN_HIGHECC) {
  683. pblk_log_read_err(pblk, &rqd);
  684. ret = -EIO;
  685. goto free_rqd_dma;
  686. }
  687. emeta_buf += rq_len;
  688. left_ppas -= rq_ppas;
  689. if (left_ppas)
  690. goto next_rq;
  691. free_rqd_dma:
  692. nvm_dev_dma_free(dev->parent, rqd.meta_list, rqd.dma_meta_list);
  693. return ret;
  694. }
  695. static void pblk_setup_e_rq(struct pblk *pblk, struct nvm_rq *rqd,
  696. struct ppa_addr ppa)
  697. {
  698. rqd->opcode = NVM_OP_ERASE;
  699. rqd->ppa_addr = ppa;
  700. rqd->nr_ppas = 1;
  701. rqd->is_seq = 1;
  702. rqd->bio = NULL;
  703. }
  704. static int pblk_blk_erase_sync(struct pblk *pblk, struct ppa_addr ppa)
  705. {
  706. struct nvm_rq rqd = {NULL};
  707. int ret;
  708. trace_pblk_chunk_reset(pblk_disk_name(pblk), &ppa,
  709. PBLK_CHUNK_RESET_START);
  710. pblk_setup_e_rq(pblk, &rqd, ppa);
  711. /* The write thread schedules erases so that it minimizes disturbances
  712. * with writes. Thus, there is no need to take the LUN semaphore.
  713. */
  714. ret = pblk_submit_io_sync(pblk, &rqd, NULL);
  715. rqd.private = pblk;
  716. __pblk_end_io_erase(pblk, &rqd);
  717. return ret;
  718. }
  719. int pblk_line_erase(struct pblk *pblk, struct pblk_line *line)
  720. {
  721. struct pblk_line_meta *lm = &pblk->lm;
  722. struct ppa_addr ppa;
  723. int ret, bit = -1;
  724. /* Erase only good blocks, one at a time */
  725. do {
  726. spin_lock(&line->lock);
  727. bit = find_next_zero_bit(line->erase_bitmap, lm->blk_per_line,
  728. bit + 1);
  729. if (bit >= lm->blk_per_line) {
  730. spin_unlock(&line->lock);
  731. break;
  732. }
  733. ppa = pblk->luns[bit].bppa; /* set ch and lun */
  734. ppa.a.blk = line->id;
  735. atomic_dec(&line->left_eblks);
  736. WARN_ON(test_and_set_bit(bit, line->erase_bitmap));
  737. spin_unlock(&line->lock);
  738. ret = pblk_blk_erase_sync(pblk, ppa);
  739. if (ret) {
  740. pblk_err(pblk, "failed to erase line %d\n", line->id);
  741. return ret;
  742. }
  743. } while (1);
  744. return 0;
  745. }
  746. static void pblk_line_setup_metadata(struct pblk_line *line,
  747. struct pblk_line_mgmt *l_mg,
  748. struct pblk_line_meta *lm)
  749. {
  750. int meta_line;
  751. lockdep_assert_held(&l_mg->free_lock);
  752. retry_meta:
  753. meta_line = find_first_zero_bit(&l_mg->meta_bitmap, PBLK_DATA_LINES);
  754. if (meta_line == PBLK_DATA_LINES) {
  755. spin_unlock(&l_mg->free_lock);
  756. io_schedule();
  757. spin_lock(&l_mg->free_lock);
  758. goto retry_meta;
  759. }
  760. set_bit(meta_line, &l_mg->meta_bitmap);
  761. line->meta_line = meta_line;
  762. line->smeta = l_mg->sline_meta[meta_line];
  763. line->emeta = l_mg->eline_meta[meta_line];
  764. memset(line->smeta, 0, lm->smeta_len);
  765. memset(line->emeta->buf, 0, lm->emeta_len[0]);
  766. line->emeta->mem = 0;
  767. atomic_set(&line->emeta->sync, 0);
  768. }
  769. /* For now lines are always assumed full lines. Thus, smeta former and current
  770. * lun bitmaps are omitted.
  771. */
  772. static int pblk_line_init_metadata(struct pblk *pblk, struct pblk_line *line,
  773. struct pblk_line *cur)
  774. {
  775. struct nvm_tgt_dev *dev = pblk->dev;
  776. struct nvm_geo *geo = &dev->geo;
  777. struct pblk_line_meta *lm = &pblk->lm;
  778. struct pblk_line_mgmt *l_mg = &pblk->l_mg;
  779. struct pblk_emeta *emeta = line->emeta;
  780. struct line_emeta *emeta_buf = emeta->buf;
  781. struct line_smeta *smeta_buf = (struct line_smeta *)line->smeta;
  782. int nr_blk_line;
  783. /* After erasing the line, new bad blocks might appear and we risk
  784. * having an invalid line
  785. */
  786. nr_blk_line = lm->blk_per_line -
  787. bitmap_weight(line->blk_bitmap, lm->blk_per_line);
  788. if (nr_blk_line < lm->min_blk_line) {
  789. spin_lock(&l_mg->free_lock);
  790. spin_lock(&line->lock);
  791. line->state = PBLK_LINESTATE_BAD;
  792. trace_pblk_line_state(pblk_disk_name(pblk), line->id,
  793. line->state);
  794. spin_unlock(&line->lock);
  795. list_add_tail(&line->list, &l_mg->bad_list);
  796. spin_unlock(&l_mg->free_lock);
  797. pblk_debug(pblk, "line %d is bad\n", line->id);
  798. return 0;
  799. }
  800. /* Run-time metadata */
  801. line->lun_bitmap = ((void *)(smeta_buf)) + sizeof(struct line_smeta);
  802. /* Mark LUNs allocated in this line (all for now) */
  803. bitmap_set(line->lun_bitmap, 0, lm->lun_bitmap_len);
  804. smeta_buf->header.identifier = cpu_to_le32(PBLK_MAGIC);
  805. guid_copy((guid_t *)&smeta_buf->header.uuid, &pblk->instance_uuid);
  806. smeta_buf->header.id = cpu_to_le32(line->id);
  807. smeta_buf->header.type = cpu_to_le16(line->type);
  808. smeta_buf->header.version_major = SMETA_VERSION_MAJOR;
  809. smeta_buf->header.version_minor = SMETA_VERSION_MINOR;
  810. /* Start metadata */
  811. smeta_buf->seq_nr = cpu_to_le64(line->seq_nr);
  812. smeta_buf->window_wr_lun = cpu_to_le32(geo->all_luns);
  813. /* Fill metadata among lines */
  814. if (cur) {
  815. memcpy(line->lun_bitmap, cur->lun_bitmap, lm->lun_bitmap_len);
  816. smeta_buf->prev_id = cpu_to_le32(cur->id);
  817. cur->emeta->buf->next_id = cpu_to_le32(line->id);
  818. } else {
  819. smeta_buf->prev_id = cpu_to_le32(PBLK_LINE_EMPTY);
  820. }
  821. /* All smeta must be set at this point */
  822. smeta_buf->header.crc = cpu_to_le32(
  823. pblk_calc_meta_header_crc(pblk, &smeta_buf->header));
  824. smeta_buf->crc = cpu_to_le32(pblk_calc_smeta_crc(pblk, smeta_buf));
  825. /* End metadata */
  826. memcpy(&emeta_buf->header, &smeta_buf->header,
  827. sizeof(struct line_header));
  828. emeta_buf->header.version_major = EMETA_VERSION_MAJOR;
  829. emeta_buf->header.version_minor = EMETA_VERSION_MINOR;
  830. emeta_buf->header.crc = cpu_to_le32(
  831. pblk_calc_meta_header_crc(pblk, &emeta_buf->header));
  832. emeta_buf->seq_nr = cpu_to_le64(line->seq_nr);
  833. emeta_buf->nr_lbas = cpu_to_le64(line->sec_in_line);
  834. emeta_buf->nr_valid_lbas = cpu_to_le64(0);
  835. emeta_buf->next_id = cpu_to_le32(PBLK_LINE_EMPTY);
  836. emeta_buf->crc = cpu_to_le32(0);
  837. emeta_buf->prev_id = smeta_buf->prev_id;
  838. return 1;
  839. }
  840. static int pblk_line_alloc_bitmaps(struct pblk *pblk, struct pblk_line *line)
  841. {
  842. struct pblk_line_meta *lm = &pblk->lm;
  843. struct pblk_line_mgmt *l_mg = &pblk->l_mg;
  844. line->map_bitmap = mempool_alloc(l_mg->bitmap_pool, GFP_KERNEL);
  845. if (!line->map_bitmap)
  846. return -ENOMEM;
  847. memset(line->map_bitmap, 0, lm->sec_bitmap_len);
  848. /* will be initialized using bb info from map_bitmap */
  849. line->invalid_bitmap = mempool_alloc(l_mg->bitmap_pool, GFP_KERNEL);
  850. if (!line->invalid_bitmap) {
  851. mempool_free(line->map_bitmap, l_mg->bitmap_pool);
  852. line->map_bitmap = NULL;
  853. return -ENOMEM;
  854. }
  855. return 0;
  856. }
  857. /* For now lines are always assumed full lines. Thus, smeta former and current
  858. * lun bitmaps are omitted.
  859. */
  860. static int pblk_line_init_bb(struct pblk *pblk, struct pblk_line *line,
  861. int init)
  862. {
  863. struct nvm_tgt_dev *dev = pblk->dev;
  864. struct nvm_geo *geo = &dev->geo;
  865. struct pblk_line_meta *lm = &pblk->lm;
  866. struct pblk_line_mgmt *l_mg = &pblk->l_mg;
  867. u64 off;
  868. int bit = -1;
  869. int emeta_secs;
  870. line->sec_in_line = lm->sec_per_line;
  871. /* Capture bad block information on line mapping bitmaps */
  872. while ((bit = find_next_bit(line->blk_bitmap, lm->blk_per_line,
  873. bit + 1)) < lm->blk_per_line) {
  874. off = bit * geo->ws_opt;
  875. bitmap_shift_left(l_mg->bb_aux, l_mg->bb_template, off,
  876. lm->sec_per_line);
  877. bitmap_or(line->map_bitmap, line->map_bitmap, l_mg->bb_aux,
  878. lm->sec_per_line);
  879. line->sec_in_line -= geo->clba;
  880. }
  881. /* Mark smeta metadata sectors as bad sectors */
  882. bit = find_first_zero_bit(line->blk_bitmap, lm->blk_per_line);
  883. off = bit * geo->ws_opt;
  884. bitmap_set(line->map_bitmap, off, lm->smeta_sec);
  885. line->sec_in_line -= lm->smeta_sec;
  886. line->cur_sec = off + lm->smeta_sec;
  887. if (init && pblk_line_smeta_write(pblk, line, off)) {
  888. pblk_debug(pblk, "line smeta I/O failed. Retry\n");
  889. return 0;
  890. }
  891. bitmap_copy(line->invalid_bitmap, line->map_bitmap, lm->sec_per_line);
  892. /* Mark emeta metadata sectors as bad sectors. We need to consider bad
  893. * blocks to make sure that there are enough sectors to store emeta
  894. */
  895. emeta_secs = lm->emeta_sec[0];
  896. off = lm->sec_per_line;
  897. while (emeta_secs) {
  898. off -= geo->ws_opt;
  899. if (!test_bit(off, line->invalid_bitmap)) {
  900. bitmap_set(line->invalid_bitmap, off, geo->ws_opt);
  901. emeta_secs -= geo->ws_opt;
  902. }
  903. }
  904. line->emeta_ssec = off;
  905. line->sec_in_line -= lm->emeta_sec[0];
  906. line->nr_valid_lbas = 0;
  907. line->left_msecs = line->sec_in_line;
  908. *line->vsc = cpu_to_le32(line->sec_in_line);
  909. if (lm->sec_per_line - line->sec_in_line !=
  910. bitmap_weight(line->invalid_bitmap, lm->sec_per_line)) {
  911. spin_lock(&line->lock);
  912. line->state = PBLK_LINESTATE_BAD;
  913. trace_pblk_line_state(pblk_disk_name(pblk), line->id,
  914. line->state);
  915. spin_unlock(&line->lock);
  916. list_add_tail(&line->list, &l_mg->bad_list);
  917. pblk_err(pblk, "unexpected line %d is bad\n", line->id);
  918. return 0;
  919. }
  920. return 1;
  921. }
  922. static int pblk_prepare_new_line(struct pblk *pblk, struct pblk_line *line)
  923. {
  924. struct pblk_line_meta *lm = &pblk->lm;
  925. struct nvm_tgt_dev *dev = pblk->dev;
  926. struct nvm_geo *geo = &dev->geo;
  927. int blk_to_erase = atomic_read(&line->blk_in_line);
  928. int i;
  929. for (i = 0; i < lm->blk_per_line; i++) {
  930. struct pblk_lun *rlun = &pblk->luns[i];
  931. int pos = pblk_ppa_to_pos(geo, rlun->bppa);
  932. int state = line->chks[pos].state;
  933. /* Free chunks should not be erased */
  934. if (state & NVM_CHK_ST_FREE) {
  935. set_bit(pblk_ppa_to_pos(geo, rlun->bppa),
  936. line->erase_bitmap);
  937. blk_to_erase--;
  938. }
  939. }
  940. return blk_to_erase;
  941. }
  942. static int pblk_line_prepare(struct pblk *pblk, struct pblk_line *line)
  943. {
  944. struct pblk_line_meta *lm = &pblk->lm;
  945. int blk_in_line = atomic_read(&line->blk_in_line);
  946. int blk_to_erase;
  947. /* Bad blocks do not need to be erased */
  948. bitmap_copy(line->erase_bitmap, line->blk_bitmap, lm->blk_per_line);
  949. spin_lock(&line->lock);
  950. /* If we have not written to this line, we need to mark up free chunks
  951. * as already erased
  952. */
  953. if (line->state == PBLK_LINESTATE_NEW) {
  954. blk_to_erase = pblk_prepare_new_line(pblk, line);
  955. line->state = PBLK_LINESTATE_FREE;
  956. trace_pblk_line_state(pblk_disk_name(pblk), line->id,
  957. line->state);
  958. } else {
  959. blk_to_erase = blk_in_line;
  960. }
  961. if (blk_in_line < lm->min_blk_line) {
  962. spin_unlock(&line->lock);
  963. return -EAGAIN;
  964. }
  965. if (line->state != PBLK_LINESTATE_FREE) {
  966. WARN(1, "pblk: corrupted line %d, state %d\n",
  967. line->id, line->state);
  968. spin_unlock(&line->lock);
  969. return -EINTR;
  970. }
  971. line->state = PBLK_LINESTATE_OPEN;
  972. trace_pblk_line_state(pblk_disk_name(pblk), line->id,
  973. line->state);
  974. atomic_set(&line->left_eblks, blk_to_erase);
  975. atomic_set(&line->left_seblks, blk_to_erase);
  976. line->meta_distance = lm->meta_distance;
  977. spin_unlock(&line->lock);
  978. kref_init(&line->ref);
  979. atomic_set(&line->sec_to_update, 0);
  980. return 0;
  981. }
  982. /* Line allocations in the recovery path are always single threaded */
  983. int pblk_line_recov_alloc(struct pblk *pblk, struct pblk_line *line)
  984. {
  985. struct pblk_line_mgmt *l_mg = &pblk->l_mg;
  986. int ret;
  987. spin_lock(&l_mg->free_lock);
  988. l_mg->data_line = line;
  989. list_del(&line->list);
  990. ret = pblk_line_prepare(pblk, line);
  991. if (ret) {
  992. list_add(&line->list, &l_mg->free_list);
  993. spin_unlock(&l_mg->free_lock);
  994. return ret;
  995. }
  996. spin_unlock(&l_mg->free_lock);
  997. ret = pblk_line_alloc_bitmaps(pblk, line);
  998. if (ret)
  999. goto fail;
  1000. if (!pblk_line_init_bb(pblk, line, 0)) {
  1001. ret = -EINTR;
  1002. goto fail;
  1003. }
  1004. pblk_rl_free_lines_dec(&pblk->rl, line, true);
  1005. return 0;
  1006. fail:
  1007. spin_lock(&l_mg->free_lock);
  1008. list_add(&line->list, &l_mg->free_list);
  1009. spin_unlock(&l_mg->free_lock);
  1010. return ret;
  1011. }
  1012. void pblk_line_recov_close(struct pblk *pblk, struct pblk_line *line)
  1013. {
  1014. struct pblk_line_mgmt *l_mg = &pblk->l_mg;
  1015. mempool_free(line->map_bitmap, l_mg->bitmap_pool);
  1016. line->map_bitmap = NULL;
  1017. line->smeta = NULL;
  1018. line->emeta = NULL;
  1019. }
  1020. static void pblk_line_reinit(struct pblk_line *line)
  1021. {
  1022. *line->vsc = cpu_to_le32(EMPTY_ENTRY);
  1023. line->map_bitmap = NULL;
  1024. line->invalid_bitmap = NULL;
  1025. line->smeta = NULL;
  1026. line->emeta = NULL;
  1027. }
  1028. void pblk_line_free(struct pblk_line *line)
  1029. {
  1030. struct pblk *pblk = line->pblk;
  1031. struct pblk_line_mgmt *l_mg = &pblk->l_mg;
  1032. mempool_free(line->map_bitmap, l_mg->bitmap_pool);
  1033. mempool_free(line->invalid_bitmap, l_mg->bitmap_pool);
  1034. pblk_line_reinit(line);
  1035. }
  1036. struct pblk_line *pblk_line_get(struct pblk *pblk)
  1037. {
  1038. struct pblk_line_mgmt *l_mg = &pblk->l_mg;
  1039. struct pblk_line_meta *lm = &pblk->lm;
  1040. struct pblk_line *line;
  1041. int ret, bit;
  1042. lockdep_assert_held(&l_mg->free_lock);
  1043. retry:
  1044. if (list_empty(&l_mg->free_list)) {
  1045. pblk_err(pblk, "no free lines\n");
  1046. return NULL;
  1047. }
  1048. line = list_first_entry(&l_mg->free_list, struct pblk_line, list);
  1049. list_del(&line->list);
  1050. l_mg->nr_free_lines--;
  1051. bit = find_first_zero_bit(line->blk_bitmap, lm->blk_per_line);
  1052. if (unlikely(bit >= lm->blk_per_line)) {
  1053. spin_lock(&line->lock);
  1054. line->state = PBLK_LINESTATE_BAD;
  1055. trace_pblk_line_state(pblk_disk_name(pblk), line->id,
  1056. line->state);
  1057. spin_unlock(&line->lock);
  1058. list_add_tail(&line->list, &l_mg->bad_list);
  1059. pblk_debug(pblk, "line %d is bad\n", line->id);
  1060. goto retry;
  1061. }
  1062. ret = pblk_line_prepare(pblk, line);
  1063. if (ret) {
  1064. switch (ret) {
  1065. case -EAGAIN:
  1066. list_add(&line->list, &l_mg->bad_list);
  1067. goto retry;
  1068. case -EINTR:
  1069. list_add(&line->list, &l_mg->corrupt_list);
  1070. goto retry;
  1071. default:
  1072. pblk_err(pblk, "failed to prepare line %d\n", line->id);
  1073. list_add(&line->list, &l_mg->free_list);
  1074. l_mg->nr_free_lines++;
  1075. return NULL;
  1076. }
  1077. }
  1078. return line;
  1079. }
  1080. static struct pblk_line *pblk_line_retry(struct pblk *pblk,
  1081. struct pblk_line *line)
  1082. {
  1083. struct pblk_line_mgmt *l_mg = &pblk->l_mg;
  1084. struct pblk_line *retry_line;
  1085. retry:
  1086. spin_lock(&l_mg->free_lock);
  1087. retry_line = pblk_line_get(pblk);
  1088. if (!retry_line) {
  1089. l_mg->data_line = NULL;
  1090. spin_unlock(&l_mg->free_lock);
  1091. return NULL;
  1092. }
  1093. retry_line->map_bitmap = line->map_bitmap;
  1094. retry_line->invalid_bitmap = line->invalid_bitmap;
  1095. retry_line->smeta = line->smeta;
  1096. retry_line->emeta = line->emeta;
  1097. retry_line->meta_line = line->meta_line;
  1098. pblk_line_reinit(line);
  1099. l_mg->data_line = retry_line;
  1100. spin_unlock(&l_mg->free_lock);
  1101. pblk_rl_free_lines_dec(&pblk->rl, line, false);
  1102. if (pblk_line_erase(pblk, retry_line))
  1103. goto retry;
  1104. return retry_line;
  1105. }
  1106. static void pblk_set_space_limit(struct pblk *pblk)
  1107. {
  1108. struct pblk_rl *rl = &pblk->rl;
  1109. atomic_set(&rl->rb_space, 0);
  1110. }
  1111. struct pblk_line *pblk_line_get_first_data(struct pblk *pblk)
  1112. {
  1113. struct pblk_line_mgmt *l_mg = &pblk->l_mg;
  1114. struct pblk_line *line;
  1115. spin_lock(&l_mg->free_lock);
  1116. line = pblk_line_get(pblk);
  1117. if (!line) {
  1118. spin_unlock(&l_mg->free_lock);
  1119. return NULL;
  1120. }
  1121. line->seq_nr = l_mg->d_seq_nr++;
  1122. line->type = PBLK_LINETYPE_DATA;
  1123. l_mg->data_line = line;
  1124. pblk_line_setup_metadata(line, l_mg, &pblk->lm);
  1125. /* Allocate next line for preparation */
  1126. l_mg->data_next = pblk_line_get(pblk);
  1127. if (!l_mg->data_next) {
  1128. /* If we cannot get a new line, we need to stop the pipeline.
  1129. * Only allow as many writes in as we can store safely and then
  1130. * fail gracefully
  1131. */
  1132. pblk_set_space_limit(pblk);
  1133. l_mg->data_next = NULL;
  1134. } else {
  1135. l_mg->data_next->seq_nr = l_mg->d_seq_nr++;
  1136. l_mg->data_next->type = PBLK_LINETYPE_DATA;
  1137. }
  1138. spin_unlock(&l_mg->free_lock);
  1139. if (pblk_line_alloc_bitmaps(pblk, line))
  1140. return NULL;
  1141. if (pblk_line_erase(pblk, line)) {
  1142. line = pblk_line_retry(pblk, line);
  1143. if (!line)
  1144. return NULL;
  1145. }
  1146. retry_setup:
  1147. if (!pblk_line_init_metadata(pblk, line, NULL)) {
  1148. line = pblk_line_retry(pblk, line);
  1149. if (!line)
  1150. return NULL;
  1151. goto retry_setup;
  1152. }
  1153. if (!pblk_line_init_bb(pblk, line, 1)) {
  1154. line = pblk_line_retry(pblk, line);
  1155. if (!line)
  1156. return NULL;
  1157. goto retry_setup;
  1158. }
  1159. pblk_rl_free_lines_dec(&pblk->rl, line, true);
  1160. return line;
  1161. }
  1162. void pblk_ppa_to_line_put(struct pblk *pblk, struct ppa_addr ppa)
  1163. {
  1164. struct pblk_line *line;
  1165. line = pblk_ppa_to_line(pblk, ppa);
  1166. kref_put(&line->ref, pblk_line_put_wq);
  1167. }
  1168. void pblk_rq_to_line_put(struct pblk *pblk, struct nvm_rq *rqd)
  1169. {
  1170. struct ppa_addr *ppa_list = nvm_rq_to_ppa_list(rqd);
  1171. int i;
  1172. for (i = 0; i < rqd->nr_ppas; i++)
  1173. pblk_ppa_to_line_put(pblk, ppa_list[i]);
  1174. }
  1175. static void pblk_stop_writes(struct pblk *pblk, struct pblk_line *line)
  1176. {
  1177. lockdep_assert_held(&pblk->l_mg.free_lock);
  1178. pblk_set_space_limit(pblk);
  1179. pblk->state = PBLK_STATE_STOPPING;
  1180. trace_pblk_state(pblk_disk_name(pblk), pblk->state);
  1181. }
  1182. static void pblk_line_close_meta_sync(struct pblk *pblk)
  1183. {
  1184. struct pblk_line_mgmt *l_mg = &pblk->l_mg;
  1185. struct pblk_line_meta *lm = &pblk->lm;
  1186. struct pblk_line *line, *tline;
  1187. LIST_HEAD(list);
  1188. spin_lock(&l_mg->close_lock);
  1189. if (list_empty(&l_mg->emeta_list)) {
  1190. spin_unlock(&l_mg->close_lock);
  1191. return;
  1192. }
  1193. list_cut_position(&list, &l_mg->emeta_list, l_mg->emeta_list.prev);
  1194. spin_unlock(&l_mg->close_lock);
  1195. list_for_each_entry_safe(line, tline, &list, list) {
  1196. struct pblk_emeta *emeta = line->emeta;
  1197. while (emeta->mem < lm->emeta_len[0]) {
  1198. int ret;
  1199. ret = pblk_submit_meta_io(pblk, line);
  1200. if (ret) {
  1201. pblk_err(pblk, "sync meta line %d failed (%d)\n",
  1202. line->id, ret);
  1203. return;
  1204. }
  1205. }
  1206. }
  1207. pblk_wait_for_meta(pblk);
  1208. flush_workqueue(pblk->close_wq);
  1209. }
  1210. void __pblk_pipeline_flush(struct pblk *pblk)
  1211. {
  1212. struct pblk_line_mgmt *l_mg = &pblk->l_mg;
  1213. int ret;
  1214. spin_lock(&l_mg->free_lock);
  1215. if (pblk->state == PBLK_STATE_RECOVERING ||
  1216. pblk->state == PBLK_STATE_STOPPED) {
  1217. spin_unlock(&l_mg->free_lock);
  1218. return;
  1219. }
  1220. pblk->state = PBLK_STATE_RECOVERING;
  1221. trace_pblk_state(pblk_disk_name(pblk), pblk->state);
  1222. spin_unlock(&l_mg->free_lock);
  1223. pblk_flush_writer(pblk);
  1224. pblk_wait_for_meta(pblk);
  1225. ret = pblk_recov_pad(pblk);
  1226. if (ret) {
  1227. pblk_err(pblk, "could not close data on teardown(%d)\n", ret);
  1228. return;
  1229. }
  1230. flush_workqueue(pblk->bb_wq);
  1231. pblk_line_close_meta_sync(pblk);
  1232. }
  1233. void __pblk_pipeline_stop(struct pblk *pblk)
  1234. {
  1235. struct pblk_line_mgmt *l_mg = &pblk->l_mg;
  1236. spin_lock(&l_mg->free_lock);
  1237. pblk->state = PBLK_STATE_STOPPED;
  1238. trace_pblk_state(pblk_disk_name(pblk), pblk->state);
  1239. l_mg->data_line = NULL;
  1240. l_mg->data_next = NULL;
  1241. spin_unlock(&l_mg->free_lock);
  1242. }
  1243. void pblk_pipeline_stop(struct pblk *pblk)
  1244. {
  1245. __pblk_pipeline_flush(pblk);
  1246. __pblk_pipeline_stop(pblk);
  1247. }
  1248. struct pblk_line *pblk_line_replace_data(struct pblk *pblk)
  1249. {
  1250. struct pblk_line_mgmt *l_mg = &pblk->l_mg;
  1251. struct pblk_line *cur, *new = NULL;
  1252. unsigned int left_seblks;
  1253. new = l_mg->data_next;
  1254. if (!new)
  1255. goto out;
  1256. spin_lock(&l_mg->free_lock);
  1257. cur = l_mg->data_line;
  1258. l_mg->data_line = new;
  1259. pblk_line_setup_metadata(new, l_mg, &pblk->lm);
  1260. spin_unlock(&l_mg->free_lock);
  1261. retry_erase:
  1262. left_seblks = atomic_read(&new->left_seblks);
  1263. if (left_seblks) {
  1264. /* If line is not fully erased, erase it */
  1265. if (atomic_read(&new->left_eblks)) {
  1266. if (pblk_line_erase(pblk, new))
  1267. goto out;
  1268. } else {
  1269. io_schedule();
  1270. }
  1271. goto retry_erase;
  1272. }
  1273. if (pblk_line_alloc_bitmaps(pblk, new))
  1274. return NULL;
  1275. retry_setup:
  1276. if (!pblk_line_init_metadata(pblk, new, cur)) {
  1277. new = pblk_line_retry(pblk, new);
  1278. if (!new)
  1279. goto out;
  1280. goto retry_setup;
  1281. }
  1282. if (!pblk_line_init_bb(pblk, new, 1)) {
  1283. new = pblk_line_retry(pblk, new);
  1284. if (!new)
  1285. goto out;
  1286. goto retry_setup;
  1287. }
  1288. pblk_rl_free_lines_dec(&pblk->rl, new, true);
  1289. /* Allocate next line for preparation */
  1290. spin_lock(&l_mg->free_lock);
  1291. l_mg->data_next = pblk_line_get(pblk);
  1292. if (!l_mg->data_next) {
  1293. /* If we cannot get a new line, we need to stop the pipeline.
  1294. * Only allow as many writes in as we can store safely and then
  1295. * fail gracefully
  1296. */
  1297. pblk_stop_writes(pblk, new);
  1298. l_mg->data_next = NULL;
  1299. } else {
  1300. l_mg->data_next->seq_nr = l_mg->d_seq_nr++;
  1301. l_mg->data_next->type = PBLK_LINETYPE_DATA;
  1302. }
  1303. spin_unlock(&l_mg->free_lock);
  1304. out:
  1305. return new;
  1306. }
  1307. static void __pblk_line_put(struct pblk *pblk, struct pblk_line *line)
  1308. {
  1309. struct pblk_line_mgmt *l_mg = &pblk->l_mg;
  1310. struct pblk_gc *gc = &pblk->gc;
  1311. spin_lock(&line->lock);
  1312. WARN_ON(line->state != PBLK_LINESTATE_GC);
  1313. if (line->w_err_gc->has_gc_err) {
  1314. spin_unlock(&line->lock);
  1315. pblk_err(pblk, "line %d had errors during GC\n", line->id);
  1316. pblk_put_line_back(pblk, line);
  1317. line->w_err_gc->has_gc_err = 0;
  1318. return;
  1319. }
  1320. line->state = PBLK_LINESTATE_FREE;
  1321. trace_pblk_line_state(pblk_disk_name(pblk), line->id,
  1322. line->state);
  1323. line->gc_group = PBLK_LINEGC_NONE;
  1324. pblk_line_free(line);
  1325. if (line->w_err_gc->has_write_err) {
  1326. pblk_rl_werr_line_out(&pblk->rl);
  1327. line->w_err_gc->has_write_err = 0;
  1328. }
  1329. spin_unlock(&line->lock);
  1330. atomic_dec(&gc->pipeline_gc);
  1331. spin_lock(&l_mg->free_lock);
  1332. list_add_tail(&line->list, &l_mg->free_list);
  1333. l_mg->nr_free_lines++;
  1334. spin_unlock(&l_mg->free_lock);
  1335. pblk_rl_free_lines_inc(&pblk->rl, line);
  1336. }
  1337. static void pblk_line_put_ws(struct work_struct *work)
  1338. {
  1339. struct pblk_line_ws *line_put_ws = container_of(work,
  1340. struct pblk_line_ws, ws);
  1341. struct pblk *pblk = line_put_ws->pblk;
  1342. struct pblk_line *line = line_put_ws->line;
  1343. __pblk_line_put(pblk, line);
  1344. mempool_free(line_put_ws, &pblk->gen_ws_pool);
  1345. }
  1346. void pblk_line_put(struct kref *ref)
  1347. {
  1348. struct pblk_line *line = container_of(ref, struct pblk_line, ref);
  1349. struct pblk *pblk = line->pblk;
  1350. __pblk_line_put(pblk, line);
  1351. }
  1352. void pblk_line_put_wq(struct kref *ref)
  1353. {
  1354. struct pblk_line *line = container_of(ref, struct pblk_line, ref);
  1355. struct pblk *pblk = line->pblk;
  1356. struct pblk_line_ws *line_put_ws;
  1357. line_put_ws = mempool_alloc(&pblk->gen_ws_pool, GFP_ATOMIC);
  1358. if (!line_put_ws)
  1359. return;
  1360. line_put_ws->pblk = pblk;
  1361. line_put_ws->line = line;
  1362. line_put_ws->priv = NULL;
  1363. INIT_WORK(&line_put_ws->ws, pblk_line_put_ws);
  1364. queue_work(pblk->r_end_wq, &line_put_ws->ws);
  1365. }
  1366. int pblk_blk_erase_async(struct pblk *pblk, struct ppa_addr ppa)
  1367. {
  1368. struct nvm_rq *rqd;
  1369. int err;
  1370. rqd = pblk_alloc_rqd(pblk, PBLK_ERASE);
  1371. pblk_setup_e_rq(pblk, rqd, ppa);
  1372. rqd->end_io = pblk_end_io_erase;
  1373. rqd->private = pblk;
  1374. trace_pblk_chunk_reset(pblk_disk_name(pblk),
  1375. &ppa, PBLK_CHUNK_RESET_START);
  1376. /* The write thread schedules erases so that it minimizes disturbances
  1377. * with writes. Thus, there is no need to take the LUN semaphore.
  1378. */
  1379. err = pblk_submit_io(pblk, rqd, NULL);
  1380. if (err) {
  1381. struct nvm_tgt_dev *dev = pblk->dev;
  1382. struct nvm_geo *geo = &dev->geo;
  1383. pblk_err(pblk, "could not async erase line:%d,blk:%d\n",
  1384. pblk_ppa_to_line_id(ppa),
  1385. pblk_ppa_to_pos(geo, ppa));
  1386. }
  1387. return err;
  1388. }
  1389. struct pblk_line *pblk_line_get_data(struct pblk *pblk)
  1390. {
  1391. return pblk->l_mg.data_line;
  1392. }
  1393. /* For now, always erase next line */
  1394. struct pblk_line *pblk_line_get_erase(struct pblk *pblk)
  1395. {
  1396. return pblk->l_mg.data_next;
  1397. }
  1398. int pblk_line_is_full(struct pblk_line *line)
  1399. {
  1400. return (line->left_msecs == 0);
  1401. }
  1402. static void pblk_line_should_sync_meta(struct pblk *pblk)
  1403. {
  1404. if (pblk_rl_is_limit(&pblk->rl))
  1405. pblk_line_close_meta_sync(pblk);
  1406. }
  1407. void pblk_line_close(struct pblk *pblk, struct pblk_line *line)
  1408. {
  1409. struct nvm_tgt_dev *dev = pblk->dev;
  1410. struct nvm_geo *geo = &dev->geo;
  1411. struct pblk_line_meta *lm = &pblk->lm;
  1412. struct pblk_line_mgmt *l_mg = &pblk->l_mg;
  1413. struct list_head *move_list;
  1414. int i;
  1415. #ifdef CONFIG_NVM_PBLK_DEBUG
  1416. WARN(!bitmap_full(line->map_bitmap, lm->sec_per_line),
  1417. "pblk: corrupt closed line %d\n", line->id);
  1418. #endif
  1419. spin_lock(&l_mg->free_lock);
  1420. WARN_ON(!test_and_clear_bit(line->meta_line, &l_mg->meta_bitmap));
  1421. spin_unlock(&l_mg->free_lock);
  1422. spin_lock(&l_mg->gc_lock);
  1423. spin_lock(&line->lock);
  1424. WARN_ON(line->state != PBLK_LINESTATE_OPEN);
  1425. line->state = PBLK_LINESTATE_CLOSED;
  1426. move_list = pblk_line_gc_list(pblk, line);
  1427. list_add_tail(&line->list, move_list);
  1428. mempool_free(line->map_bitmap, l_mg->bitmap_pool);
  1429. line->map_bitmap = NULL;
  1430. line->smeta = NULL;
  1431. line->emeta = NULL;
  1432. for (i = 0; i < lm->blk_per_line; i++) {
  1433. struct pblk_lun *rlun = &pblk->luns[i];
  1434. int pos = pblk_ppa_to_pos(geo, rlun->bppa);
  1435. int state = line->chks[pos].state;
  1436. if (!(state & NVM_CHK_ST_OFFLINE))
  1437. state = NVM_CHK_ST_CLOSED;
  1438. }
  1439. spin_unlock(&line->lock);
  1440. spin_unlock(&l_mg->gc_lock);
  1441. trace_pblk_line_state(pblk_disk_name(pblk), line->id,
  1442. line->state);
  1443. }
  1444. void pblk_line_close_meta(struct pblk *pblk, struct pblk_line *line)
  1445. {
  1446. struct pblk_line_mgmt *l_mg = &pblk->l_mg;
  1447. struct pblk_line_meta *lm = &pblk->lm;
  1448. struct pblk_emeta *emeta = line->emeta;
  1449. struct line_emeta *emeta_buf = emeta->buf;
  1450. struct wa_counters *wa = emeta_to_wa(lm, emeta_buf);
  1451. /* No need for exact vsc value; avoid a big line lock and take aprox. */
  1452. memcpy(emeta_to_vsc(pblk, emeta_buf), l_mg->vsc_list, lm->vsc_list_len);
  1453. memcpy(emeta_to_bb(emeta_buf), line->blk_bitmap, lm->blk_bitmap_len);
  1454. wa->user = cpu_to_le64(atomic64_read(&pblk->user_wa));
  1455. wa->pad = cpu_to_le64(atomic64_read(&pblk->pad_wa));
  1456. wa->gc = cpu_to_le64(atomic64_read(&pblk->gc_wa));
  1457. if (le32_to_cpu(emeta_buf->header.identifier) != PBLK_MAGIC) {
  1458. emeta_buf->header.identifier = cpu_to_le32(PBLK_MAGIC);
  1459. guid_copy((guid_t *)&emeta_buf->header.uuid,
  1460. &pblk->instance_uuid);
  1461. emeta_buf->header.id = cpu_to_le32(line->id);
  1462. emeta_buf->header.type = cpu_to_le16(line->type);
  1463. emeta_buf->header.version_major = EMETA_VERSION_MAJOR;
  1464. emeta_buf->header.version_minor = EMETA_VERSION_MINOR;
  1465. emeta_buf->header.crc = cpu_to_le32(
  1466. pblk_calc_meta_header_crc(pblk, &emeta_buf->header));
  1467. }
  1468. emeta_buf->nr_valid_lbas = cpu_to_le64(line->nr_valid_lbas);
  1469. emeta_buf->crc = cpu_to_le32(pblk_calc_emeta_crc(pblk, emeta_buf));
  1470. spin_lock(&l_mg->close_lock);
  1471. spin_lock(&line->lock);
  1472. /* Update the in-memory start address for emeta, in case it has
  1473. * shifted due to write errors
  1474. */
  1475. if (line->emeta_ssec != line->cur_sec)
  1476. line->emeta_ssec = line->cur_sec;
  1477. list_add_tail(&line->list, &l_mg->emeta_list);
  1478. spin_unlock(&line->lock);
  1479. spin_unlock(&l_mg->close_lock);
  1480. pblk_line_should_sync_meta(pblk);
  1481. }
  1482. static void pblk_save_lba_list(struct pblk *pblk, struct pblk_line *line)
  1483. {
  1484. struct pblk_line_meta *lm = &pblk->lm;
  1485. unsigned int lba_list_size = lm->emeta_len[2];
  1486. struct pblk_w_err_gc *w_err_gc = line->w_err_gc;
  1487. struct pblk_emeta *emeta = line->emeta;
  1488. w_err_gc->lba_list = kvmalloc(lba_list_size, GFP_KERNEL);
  1489. memcpy(w_err_gc->lba_list, emeta_to_lbas(pblk, emeta->buf),
  1490. lba_list_size);
  1491. }
  1492. void pblk_line_close_ws(struct work_struct *work)
  1493. {
  1494. struct pblk_line_ws *line_ws = container_of(work, struct pblk_line_ws,
  1495. ws);
  1496. struct pblk *pblk = line_ws->pblk;
  1497. struct pblk_line *line = line_ws->line;
  1498. struct pblk_w_err_gc *w_err_gc = line->w_err_gc;
  1499. /* Write errors makes the emeta start address stored in smeta invalid,
  1500. * so keep a copy of the lba list until we've gc'd the line
  1501. */
  1502. if (w_err_gc->has_write_err)
  1503. pblk_save_lba_list(pblk, line);
  1504. pblk_line_close(pblk, line);
  1505. mempool_free(line_ws, &pblk->gen_ws_pool);
  1506. }
  1507. void pblk_gen_run_ws(struct pblk *pblk, struct pblk_line *line, void *priv,
  1508. void (*work)(struct work_struct *), gfp_t gfp_mask,
  1509. struct workqueue_struct *wq)
  1510. {
  1511. struct pblk_line_ws *line_ws;
  1512. line_ws = mempool_alloc(&pblk->gen_ws_pool, gfp_mask);
  1513. line_ws->pblk = pblk;
  1514. line_ws->line = line;
  1515. line_ws->priv = priv;
  1516. INIT_WORK(&line_ws->ws, work);
  1517. queue_work(wq, &line_ws->ws);
  1518. }
  1519. static void __pblk_down_chunk(struct pblk *pblk, int pos)
  1520. {
  1521. struct pblk_lun *rlun = &pblk->luns[pos];
  1522. int ret;
  1523. /*
  1524. * Only send one inflight I/O per LUN. Since we map at a page
  1525. * granurality, all ppas in the I/O will map to the same LUN
  1526. */
  1527. ret = down_timeout(&rlun->wr_sem, msecs_to_jiffies(30000));
  1528. if (ret == -ETIME || ret == -EINTR)
  1529. pblk_err(pblk, "taking lun semaphore timed out: err %d\n",
  1530. -ret);
  1531. }
  1532. void pblk_down_chunk(struct pblk *pblk, struct ppa_addr ppa)
  1533. {
  1534. struct nvm_tgt_dev *dev = pblk->dev;
  1535. struct nvm_geo *geo = &dev->geo;
  1536. int pos = pblk_ppa_to_pos(geo, ppa);
  1537. __pblk_down_chunk(pblk, pos);
  1538. }
  1539. void pblk_down_rq(struct pblk *pblk, struct ppa_addr ppa,
  1540. unsigned long *lun_bitmap)
  1541. {
  1542. struct nvm_tgt_dev *dev = pblk->dev;
  1543. struct nvm_geo *geo = &dev->geo;
  1544. int pos = pblk_ppa_to_pos(geo, ppa);
  1545. /* If the LUN has been locked for this same request, do no attempt to
  1546. * lock it again
  1547. */
  1548. if (test_and_set_bit(pos, lun_bitmap))
  1549. return;
  1550. __pblk_down_chunk(pblk, pos);
  1551. }
  1552. void pblk_up_chunk(struct pblk *pblk, struct ppa_addr ppa)
  1553. {
  1554. struct nvm_tgt_dev *dev = pblk->dev;
  1555. struct nvm_geo *geo = &dev->geo;
  1556. struct pblk_lun *rlun;
  1557. int pos = pblk_ppa_to_pos(geo, ppa);
  1558. rlun = &pblk->luns[pos];
  1559. up(&rlun->wr_sem);
  1560. }
  1561. void pblk_up_rq(struct pblk *pblk, unsigned long *lun_bitmap)
  1562. {
  1563. struct nvm_tgt_dev *dev = pblk->dev;
  1564. struct nvm_geo *geo = &dev->geo;
  1565. struct pblk_lun *rlun;
  1566. int num_lun = geo->all_luns;
  1567. int bit = -1;
  1568. while ((bit = find_next_bit(lun_bitmap, num_lun, bit + 1)) < num_lun) {
  1569. rlun = &pblk->luns[bit];
  1570. up(&rlun->wr_sem);
  1571. }
  1572. }
  1573. void pblk_update_map(struct pblk *pblk, sector_t lba, struct ppa_addr ppa)
  1574. {
  1575. struct ppa_addr ppa_l2p;
  1576. /* logic error: lba out-of-bounds. Ignore update */
  1577. if (!(lba < pblk->capacity)) {
  1578. WARN(1, "pblk: corrupted L2P map request\n");
  1579. return;
  1580. }
  1581. spin_lock(&pblk->trans_lock);
  1582. ppa_l2p = pblk_trans_map_get(pblk, lba);
  1583. if (!pblk_addr_in_cache(ppa_l2p) && !pblk_ppa_empty(ppa_l2p))
  1584. pblk_map_invalidate(pblk, ppa_l2p);
  1585. pblk_trans_map_set(pblk, lba, ppa);
  1586. spin_unlock(&pblk->trans_lock);
  1587. }
  1588. void pblk_update_map_cache(struct pblk *pblk, sector_t lba, struct ppa_addr ppa)
  1589. {
  1590. #ifdef CONFIG_NVM_PBLK_DEBUG
  1591. /* Callers must ensure that the ppa points to a cache address */
  1592. BUG_ON(!pblk_addr_in_cache(ppa));
  1593. BUG_ON(pblk_rb_pos_oob(&pblk->rwb, pblk_addr_to_cacheline(ppa)));
  1594. #endif
  1595. pblk_update_map(pblk, lba, ppa);
  1596. }
  1597. int pblk_update_map_gc(struct pblk *pblk, sector_t lba, struct ppa_addr ppa_new,
  1598. struct pblk_line *gc_line, u64 paddr_gc)
  1599. {
  1600. struct ppa_addr ppa_l2p, ppa_gc;
  1601. int ret = 1;
  1602. #ifdef CONFIG_NVM_PBLK_DEBUG
  1603. /* Callers must ensure that the ppa points to a cache address */
  1604. BUG_ON(!pblk_addr_in_cache(ppa_new));
  1605. BUG_ON(pblk_rb_pos_oob(&pblk->rwb, pblk_addr_to_cacheline(ppa_new)));
  1606. #endif
  1607. /* logic error: lba out-of-bounds. Ignore update */
  1608. if (!(lba < pblk->capacity)) {
  1609. WARN(1, "pblk: corrupted L2P map request\n");
  1610. return 0;
  1611. }
  1612. spin_lock(&pblk->trans_lock);
  1613. ppa_l2p = pblk_trans_map_get(pblk, lba);
  1614. ppa_gc = addr_to_gen_ppa(pblk, paddr_gc, gc_line->id);
  1615. if (!pblk_ppa_comp(ppa_l2p, ppa_gc)) {
  1616. spin_lock(&gc_line->lock);
  1617. WARN(!test_bit(paddr_gc, gc_line->invalid_bitmap),
  1618. "pblk: corrupted GC update");
  1619. spin_unlock(&gc_line->lock);
  1620. ret = 0;
  1621. goto out;
  1622. }
  1623. pblk_trans_map_set(pblk, lba, ppa_new);
  1624. out:
  1625. spin_unlock(&pblk->trans_lock);
  1626. return ret;
  1627. }
  1628. void pblk_update_map_dev(struct pblk *pblk, sector_t lba,
  1629. struct ppa_addr ppa_mapped, struct ppa_addr ppa_cache)
  1630. {
  1631. struct ppa_addr ppa_l2p;
  1632. #ifdef CONFIG_NVM_PBLK_DEBUG
  1633. /* Callers must ensure that the ppa points to a device address */
  1634. BUG_ON(pblk_addr_in_cache(ppa_mapped));
  1635. #endif
  1636. /* Invalidate and discard padded entries */
  1637. if (lba == ADDR_EMPTY) {
  1638. atomic64_inc(&pblk->pad_wa);
  1639. #ifdef CONFIG_NVM_PBLK_DEBUG
  1640. atomic_long_inc(&pblk->padded_wb);
  1641. #endif
  1642. if (!pblk_ppa_empty(ppa_mapped))
  1643. pblk_map_invalidate(pblk, ppa_mapped);
  1644. return;
  1645. }
  1646. /* logic error: lba out-of-bounds. Ignore update */
  1647. if (!(lba < pblk->capacity)) {
  1648. WARN(1, "pblk: corrupted L2P map request\n");
  1649. return;
  1650. }
  1651. spin_lock(&pblk->trans_lock);
  1652. ppa_l2p = pblk_trans_map_get(pblk, lba);
  1653. /* Do not update L2P if the cacheline has been updated. In this case,
  1654. * the mapped ppa must be invalidated
  1655. */
  1656. if (!pblk_ppa_comp(ppa_l2p, ppa_cache)) {
  1657. if (!pblk_ppa_empty(ppa_mapped))
  1658. pblk_map_invalidate(pblk, ppa_mapped);
  1659. goto out;
  1660. }
  1661. #ifdef CONFIG_NVM_PBLK_DEBUG
  1662. WARN_ON(!pblk_addr_in_cache(ppa_l2p) && !pblk_ppa_empty(ppa_l2p));
  1663. #endif
  1664. pblk_trans_map_set(pblk, lba, ppa_mapped);
  1665. out:
  1666. spin_unlock(&pblk->trans_lock);
  1667. }
  1668. int pblk_lookup_l2p_seq(struct pblk *pblk, struct ppa_addr *ppas,
  1669. sector_t blba, int nr_secs, bool *from_cache)
  1670. {
  1671. int i;
  1672. spin_lock(&pblk->trans_lock);
  1673. for (i = 0; i < nr_secs; i++) {
  1674. struct ppa_addr ppa;
  1675. ppa = ppas[i] = pblk_trans_map_get(pblk, blba + i);
  1676. /* If the L2P entry maps to a line, the reference is valid */
  1677. if (!pblk_ppa_empty(ppa) && !pblk_addr_in_cache(ppa)) {
  1678. struct pblk_line *line = pblk_ppa_to_line(pblk, ppa);
  1679. if (i > 0 && *from_cache)
  1680. break;
  1681. *from_cache = false;
  1682. kref_get(&line->ref);
  1683. } else {
  1684. if (i > 0 && !*from_cache)
  1685. break;
  1686. *from_cache = true;
  1687. }
  1688. }
  1689. spin_unlock(&pblk->trans_lock);
  1690. return i;
  1691. }
  1692. void pblk_lookup_l2p_rand(struct pblk *pblk, struct ppa_addr *ppas,
  1693. u64 *lba_list, int nr_secs)
  1694. {
  1695. u64 lba;
  1696. int i;
  1697. spin_lock(&pblk->trans_lock);
  1698. for (i = 0; i < nr_secs; i++) {
  1699. lba = lba_list[i];
  1700. if (lba != ADDR_EMPTY) {
  1701. /* logic error: lba out-of-bounds. Ignore update */
  1702. if (!(lba < pblk->capacity)) {
  1703. WARN(1, "pblk: corrupted L2P map request\n");
  1704. continue;
  1705. }
  1706. ppas[i] = pblk_trans_map_get(pblk, lba);
  1707. }
  1708. }
  1709. spin_unlock(&pblk->trans_lock);
  1710. }
  1711. void *pblk_get_meta_for_writes(struct pblk *pblk, struct nvm_rq *rqd)
  1712. {
  1713. void *buffer;
  1714. if (pblk_is_oob_meta_supported(pblk)) {
  1715. /* Just use OOB metadata buffer as always */
  1716. buffer = rqd->meta_list;
  1717. } else {
  1718. /* We need to reuse last page of request (packed metadata)
  1719. * in similar way as traditional oob metadata
  1720. */
  1721. buffer = page_to_virt(
  1722. rqd->bio->bi_io_vec[rqd->bio->bi_vcnt - 1].bv_page);
  1723. }
  1724. return buffer;
  1725. }
  1726. void pblk_get_packed_meta(struct pblk *pblk, struct nvm_rq *rqd)
  1727. {
  1728. void *meta_list = rqd->meta_list;
  1729. void *page;
  1730. int i = 0;
  1731. if (pblk_is_oob_meta_supported(pblk))
  1732. return;
  1733. page = page_to_virt(rqd->bio->bi_io_vec[rqd->bio->bi_vcnt - 1].bv_page);
  1734. /* We need to fill oob meta buffer with data from packed metadata */
  1735. for (; i < rqd->nr_ppas; i++)
  1736. memcpy(pblk_get_meta(pblk, meta_list, i),
  1737. page + (i * sizeof(struct pblk_sec_meta)),
  1738. sizeof(struct pblk_sec_meta));
  1739. }