sysblk.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746
  1. /*
  2. * Copyright (C) 2015 Matias Bjorling. All rights reserved.
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public License version
  6. * 2 as published by the Free Software Foundation.
  7. *
  8. * This program is distributed in the hope that it will be useful, but
  9. * WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. * General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU General Public License
  14. * along with this program; see the file COPYING. If not, write to
  15. * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139,
  16. * USA.
  17. *
  18. */
  19. #include <linux/lightnvm.h>
  20. #define MAX_SYSBLKS 3 /* remember to update mapping scheme on change */
  21. #define MAX_BLKS_PR_SYSBLK 2 /* 2 blks with 256 pages and 3000 erases
  22. * enables ~1.5M updates per sysblk unit
  23. */
  24. struct sysblk_scan {
  25. /* A row is a collection of flash blocks for a system block. */
  26. int nr_rows;
  27. int row;
  28. int act_blk[MAX_SYSBLKS];
  29. int nr_ppas;
  30. struct ppa_addr ppas[MAX_SYSBLKS * MAX_BLKS_PR_SYSBLK];/* all sysblks */
  31. };
  32. static inline int scan_ppa_idx(int row, int blkid)
  33. {
  34. return (row * MAX_BLKS_PR_SYSBLK) + blkid;
  35. }
  36. static void nvm_sysblk_to_cpu(struct nvm_sb_info *info,
  37. struct nvm_system_block *sb)
  38. {
  39. info->seqnr = be32_to_cpu(sb->seqnr);
  40. info->erase_cnt = be32_to_cpu(sb->erase_cnt);
  41. info->version = be16_to_cpu(sb->version);
  42. strncpy(info->mmtype, sb->mmtype, NVM_MMTYPE_LEN);
  43. info->fs_ppa.ppa = be64_to_cpu(sb->fs_ppa);
  44. }
  45. static void nvm_cpu_to_sysblk(struct nvm_system_block *sb,
  46. struct nvm_sb_info *info)
  47. {
  48. sb->magic = cpu_to_be32(NVM_SYSBLK_MAGIC);
  49. sb->seqnr = cpu_to_be32(info->seqnr);
  50. sb->erase_cnt = cpu_to_be32(info->erase_cnt);
  51. sb->version = cpu_to_be16(info->version);
  52. strncpy(sb->mmtype, info->mmtype, NVM_MMTYPE_LEN);
  53. sb->fs_ppa = cpu_to_be64(info->fs_ppa.ppa);
  54. }
  55. static int nvm_setup_sysblks(struct nvm_dev *dev, struct ppa_addr *sysblk_ppas)
  56. {
  57. int nr_rows = min_t(int, MAX_SYSBLKS, dev->nr_chnls);
  58. int i;
  59. for (i = 0; i < nr_rows; i++)
  60. sysblk_ppas[i].ppa = 0;
  61. /* if possible, place sysblk at first channel, middle channel and last
  62. * channel of the device. If not, create only one or two sys blocks
  63. */
  64. switch (dev->nr_chnls) {
  65. case 2:
  66. sysblk_ppas[1].g.ch = 1;
  67. /* fall-through */
  68. case 1:
  69. sysblk_ppas[0].g.ch = 0;
  70. break;
  71. default:
  72. sysblk_ppas[0].g.ch = 0;
  73. sysblk_ppas[1].g.ch = dev->nr_chnls / 2;
  74. sysblk_ppas[2].g.ch = dev->nr_chnls - 1;
  75. break;
  76. }
  77. return nr_rows;
  78. }
  79. static void nvm_setup_sysblk_scan(struct nvm_dev *dev, struct sysblk_scan *s,
  80. struct ppa_addr *sysblk_ppas)
  81. {
  82. memset(s, 0, sizeof(struct sysblk_scan));
  83. s->nr_rows = nvm_setup_sysblks(dev, sysblk_ppas);
  84. }
  85. static int sysblk_get_free_blks(struct nvm_dev *dev, struct ppa_addr ppa,
  86. u8 *blks, int nr_blks,
  87. struct sysblk_scan *s)
  88. {
  89. struct ppa_addr *sppa;
  90. int i, blkid = 0;
  91. nr_blks = nvm_bb_tbl_fold(dev, blks, nr_blks);
  92. if (nr_blks < 0)
  93. return nr_blks;
  94. for (i = 0; i < nr_blks; i++) {
  95. if (blks[i] == NVM_BLK_T_HOST)
  96. return -EEXIST;
  97. if (blks[i] != NVM_BLK_T_FREE)
  98. continue;
  99. sppa = &s->ppas[scan_ppa_idx(s->row, blkid)];
  100. sppa->g.ch = ppa.g.ch;
  101. sppa->g.lun = ppa.g.lun;
  102. sppa->g.blk = i;
  103. s->nr_ppas++;
  104. blkid++;
  105. pr_debug("nvm: use (%u %u %u) as sysblk\n",
  106. sppa->g.ch, sppa->g.lun, sppa->g.blk);
  107. if (blkid > MAX_BLKS_PR_SYSBLK - 1)
  108. return 0;
  109. }
  110. pr_err("nvm: sysblk failed get sysblk\n");
  111. return -EINVAL;
  112. }
  113. static int sysblk_get_host_blks(struct nvm_dev *dev, struct ppa_addr ppa,
  114. u8 *blks, int nr_blks,
  115. struct sysblk_scan *s)
  116. {
  117. int i, nr_sysblk = 0;
  118. nr_blks = nvm_bb_tbl_fold(dev, blks, nr_blks);
  119. if (nr_blks < 0)
  120. return nr_blks;
  121. for (i = 0; i < nr_blks; i++) {
  122. if (blks[i] != NVM_BLK_T_HOST)
  123. continue;
  124. if (s->nr_ppas == MAX_BLKS_PR_SYSBLK * MAX_SYSBLKS) {
  125. pr_err("nvm: too many host blks\n");
  126. return -EINVAL;
  127. }
  128. ppa.g.blk = i;
  129. s->ppas[scan_ppa_idx(s->row, nr_sysblk)] = ppa;
  130. s->nr_ppas++;
  131. nr_sysblk++;
  132. }
  133. return 0;
  134. }
  135. static int nvm_get_all_sysblks(struct nvm_dev *dev, struct sysblk_scan *s,
  136. struct ppa_addr *ppas, int get_free)
  137. {
  138. int i, nr_blks, ret = 0;
  139. u8 *blks;
  140. s->nr_ppas = 0;
  141. nr_blks = dev->blks_per_lun * dev->plane_mode;
  142. blks = kmalloc(nr_blks, GFP_KERNEL);
  143. if (!blks)
  144. return -ENOMEM;
  145. for (i = 0; i < s->nr_rows; i++) {
  146. s->row = i;
  147. ret = nvm_get_bb_tbl(dev, ppas[i], blks);
  148. if (ret) {
  149. pr_err("nvm: failed bb tbl for ppa (%u %u)\n",
  150. ppas[i].g.ch,
  151. ppas[i].g.blk);
  152. goto err_get;
  153. }
  154. if (get_free)
  155. ret = sysblk_get_free_blks(dev, ppas[i], blks, nr_blks,
  156. s);
  157. else
  158. ret = sysblk_get_host_blks(dev, ppas[i], blks, nr_blks,
  159. s);
  160. if (ret)
  161. goto err_get;
  162. }
  163. err_get:
  164. kfree(blks);
  165. return ret;
  166. }
  167. /*
  168. * scans a block for latest sysblk.
  169. * Returns:
  170. * 0 - newer sysblk not found. PPA is updated to latest page.
  171. * 1 - newer sysblk found and stored in *cur. PPA is updated to
  172. * next valid page.
  173. * <0- error.
  174. */
  175. static int nvm_scan_block(struct nvm_dev *dev, struct ppa_addr *ppa,
  176. struct nvm_system_block *sblk)
  177. {
  178. struct nvm_system_block *cur;
  179. int pg, ret, found = 0;
  180. /* the full buffer for a flash page is allocated. Only the first of it
  181. * contains the system block information
  182. */
  183. cur = kmalloc(dev->pfpg_size, GFP_KERNEL);
  184. if (!cur)
  185. return -ENOMEM;
  186. /* perform linear scan through the block */
  187. for (pg = 0; pg < dev->lps_per_blk; pg++) {
  188. ppa->g.pg = ppa_to_slc(dev, pg);
  189. ret = nvm_submit_ppa(dev, ppa, 1, NVM_OP_PREAD, NVM_IO_SLC_MODE,
  190. cur, dev->pfpg_size);
  191. if (ret) {
  192. if (ret == NVM_RSP_ERR_EMPTYPAGE) {
  193. pr_debug("nvm: sysblk scan empty ppa (%u %u %u %u)\n",
  194. ppa->g.ch,
  195. ppa->g.lun,
  196. ppa->g.blk,
  197. ppa->g.pg);
  198. break;
  199. }
  200. pr_err("nvm: read failed (%x) for ppa (%u %u %u %u)",
  201. ret,
  202. ppa->g.ch,
  203. ppa->g.lun,
  204. ppa->g.blk,
  205. ppa->g.pg);
  206. break; /* if we can't read a page, continue to the
  207. * next blk
  208. */
  209. }
  210. if (be32_to_cpu(cur->magic) != NVM_SYSBLK_MAGIC) {
  211. pr_debug("nvm: scan break for ppa (%u %u %u %u)\n",
  212. ppa->g.ch,
  213. ppa->g.lun,
  214. ppa->g.blk,
  215. ppa->g.pg);
  216. break; /* last valid page already found */
  217. }
  218. if (be32_to_cpu(cur->seqnr) < be32_to_cpu(sblk->seqnr))
  219. continue;
  220. memcpy(sblk, cur, sizeof(struct nvm_system_block));
  221. found = 1;
  222. }
  223. kfree(cur);
  224. return found;
  225. }
  226. static int nvm_set_bb_tbl(struct nvm_dev *dev, struct sysblk_scan *s, int type)
  227. {
  228. struct nvm_rq rqd;
  229. int ret;
  230. if (s->nr_ppas > dev->ops->max_phys_sect) {
  231. pr_err("nvm: unable to update all sysblocks atomically\n");
  232. return -EINVAL;
  233. }
  234. memset(&rqd, 0, sizeof(struct nvm_rq));
  235. nvm_set_rqd_ppalist(dev, &rqd, s->ppas, s->nr_ppas, 1);
  236. nvm_generic_to_addr_mode(dev, &rqd);
  237. ret = dev->ops->set_bb_tbl(dev, &rqd.ppa_addr, rqd.nr_ppas, type);
  238. nvm_free_rqd_ppalist(dev, &rqd);
  239. if (ret) {
  240. pr_err("nvm: sysblk failed bb mark\n");
  241. return -EINVAL;
  242. }
  243. return 0;
  244. }
  245. static int nvm_write_and_verify(struct nvm_dev *dev, struct nvm_sb_info *info,
  246. struct sysblk_scan *s)
  247. {
  248. struct nvm_system_block nvmsb;
  249. void *buf;
  250. int i, sect, ret = 0;
  251. struct ppa_addr *ppas;
  252. nvm_cpu_to_sysblk(&nvmsb, info);
  253. buf = kzalloc(dev->pfpg_size, GFP_KERNEL);
  254. if (!buf)
  255. return -ENOMEM;
  256. memcpy(buf, &nvmsb, sizeof(struct nvm_system_block));
  257. ppas = kcalloc(dev->sec_per_pg, sizeof(struct ppa_addr), GFP_KERNEL);
  258. if (!ppas) {
  259. ret = -ENOMEM;
  260. goto err;
  261. }
  262. /* Write and verify */
  263. for (i = 0; i < s->nr_rows; i++) {
  264. ppas[0] = s->ppas[scan_ppa_idx(i, s->act_blk[i])];
  265. pr_debug("nvm: writing sysblk to ppa (%u %u %u %u)\n",
  266. ppas[0].g.ch,
  267. ppas[0].g.lun,
  268. ppas[0].g.blk,
  269. ppas[0].g.pg);
  270. /* Expand to all sectors within a flash page */
  271. if (dev->sec_per_pg > 1) {
  272. for (sect = 1; sect < dev->sec_per_pg; sect++) {
  273. ppas[sect].ppa = ppas[0].ppa;
  274. ppas[sect].g.sec = sect;
  275. }
  276. }
  277. ret = nvm_submit_ppa(dev, ppas, dev->sec_per_pg, NVM_OP_PWRITE,
  278. NVM_IO_SLC_MODE, buf, dev->pfpg_size);
  279. if (ret) {
  280. pr_err("nvm: sysblk failed program (%u %u %u)\n",
  281. ppas[0].g.ch,
  282. ppas[0].g.lun,
  283. ppas[0].g.blk);
  284. break;
  285. }
  286. ret = nvm_submit_ppa(dev, ppas, dev->sec_per_pg, NVM_OP_PREAD,
  287. NVM_IO_SLC_MODE, buf, dev->pfpg_size);
  288. if (ret) {
  289. pr_err("nvm: sysblk failed read (%u %u %u)\n",
  290. ppas[0].g.ch,
  291. ppas[0].g.lun,
  292. ppas[0].g.blk);
  293. break;
  294. }
  295. if (memcmp(buf, &nvmsb, sizeof(struct nvm_system_block))) {
  296. pr_err("nvm: sysblk failed verify (%u %u %u)\n",
  297. ppas[0].g.ch,
  298. ppas[0].g.lun,
  299. ppas[0].g.blk);
  300. ret = -EINVAL;
  301. break;
  302. }
  303. }
  304. kfree(ppas);
  305. err:
  306. kfree(buf);
  307. return ret;
  308. }
  309. static int nvm_prepare_new_sysblks(struct nvm_dev *dev, struct sysblk_scan *s)
  310. {
  311. int i, ret;
  312. unsigned long nxt_blk;
  313. struct ppa_addr *ppa;
  314. for (i = 0; i < s->nr_rows; i++) {
  315. nxt_blk = (s->act_blk[i] + 1) % MAX_BLKS_PR_SYSBLK;
  316. ppa = &s->ppas[scan_ppa_idx(i, nxt_blk)];
  317. ppa->g.pg = ppa_to_slc(dev, 0);
  318. ret = nvm_erase_ppa(dev, ppa, 1);
  319. if (ret)
  320. return ret;
  321. s->act_blk[i] = nxt_blk;
  322. }
  323. return 0;
  324. }
  325. int nvm_get_sysblock(struct nvm_dev *dev, struct nvm_sb_info *info)
  326. {
  327. struct ppa_addr sysblk_ppas[MAX_SYSBLKS];
  328. struct sysblk_scan s;
  329. struct nvm_system_block *cur;
  330. int i, j, found = 0;
  331. int ret = -ENOMEM;
  332. /*
  333. * 1. setup sysblk locations
  334. * 2. get bad block list
  335. * 3. filter on host-specific (type 3)
  336. * 4. iterate through all and find the highest seq nr.
  337. * 5. return superblock information
  338. */
  339. if (!dev->ops->get_bb_tbl)
  340. return -EINVAL;
  341. nvm_setup_sysblk_scan(dev, &s, sysblk_ppas);
  342. mutex_lock(&dev->mlock);
  343. ret = nvm_get_all_sysblks(dev, &s, sysblk_ppas, 0);
  344. if (ret)
  345. goto err_sysblk;
  346. /* no sysblocks initialized */
  347. if (!s.nr_ppas)
  348. goto err_sysblk;
  349. cur = kzalloc(sizeof(struct nvm_system_block), GFP_KERNEL);
  350. if (!cur)
  351. goto err_sysblk;
  352. /* find the latest block across all sysblocks */
  353. for (i = 0; i < s.nr_rows; i++) {
  354. for (j = 0; j < MAX_BLKS_PR_SYSBLK; j++) {
  355. struct ppa_addr ppa = s.ppas[scan_ppa_idx(i, j)];
  356. ret = nvm_scan_block(dev, &ppa, cur);
  357. if (ret > 0)
  358. found = 1;
  359. else if (ret < 0)
  360. break;
  361. }
  362. }
  363. nvm_sysblk_to_cpu(info, cur);
  364. kfree(cur);
  365. err_sysblk:
  366. mutex_unlock(&dev->mlock);
  367. if (found)
  368. return 1;
  369. return ret;
  370. }
  371. int nvm_update_sysblock(struct nvm_dev *dev, struct nvm_sb_info *new)
  372. {
  373. /* 1. for each latest superblock
  374. * 2. if room
  375. * a. write new flash page entry with the updated information
  376. * 3. if no room
  377. * a. find next available block on lun (linear search)
  378. * if none, continue to next lun
  379. * if none at all, report error. also report that it wasn't
  380. * possible to write to all superblocks.
  381. * c. write data to block.
  382. */
  383. struct ppa_addr sysblk_ppas[MAX_SYSBLKS];
  384. struct sysblk_scan s;
  385. struct nvm_system_block *cur;
  386. int i, j, ppaidx, found = 0;
  387. int ret = -ENOMEM;
  388. if (!dev->ops->get_bb_tbl)
  389. return -EINVAL;
  390. nvm_setup_sysblk_scan(dev, &s, sysblk_ppas);
  391. mutex_lock(&dev->mlock);
  392. ret = nvm_get_all_sysblks(dev, &s, sysblk_ppas, 0);
  393. if (ret)
  394. goto err_sysblk;
  395. cur = kzalloc(sizeof(struct nvm_system_block), GFP_KERNEL);
  396. if (!cur)
  397. goto err_sysblk;
  398. /* Get the latest sysblk for each sysblk row */
  399. for (i = 0; i < s.nr_rows; i++) {
  400. found = 0;
  401. for (j = 0; j < MAX_BLKS_PR_SYSBLK; j++) {
  402. ppaidx = scan_ppa_idx(i, j);
  403. ret = nvm_scan_block(dev, &s.ppas[ppaidx], cur);
  404. if (ret > 0) {
  405. s.act_blk[i] = j;
  406. found = 1;
  407. } else if (ret < 0)
  408. break;
  409. }
  410. }
  411. if (!found) {
  412. pr_err("nvm: no valid sysblks found to update\n");
  413. ret = -EINVAL;
  414. goto err_cur;
  415. }
  416. /*
  417. * All sysblocks found. Check that they have same page id in their flash
  418. * blocks
  419. */
  420. for (i = 1; i < s.nr_rows; i++) {
  421. struct ppa_addr l = s.ppas[scan_ppa_idx(0, s.act_blk[0])];
  422. struct ppa_addr r = s.ppas[scan_ppa_idx(i, s.act_blk[i])];
  423. if (l.g.pg != r.g.pg) {
  424. pr_err("nvm: sysblks not on same page. Previous update failed.\n");
  425. ret = -EINVAL;
  426. goto err_cur;
  427. }
  428. }
  429. /*
  430. * Check that there haven't been another update to the seqnr since we
  431. * began
  432. */
  433. if ((new->seqnr - 1) != be32_to_cpu(cur->seqnr)) {
  434. pr_err("nvm: seq is not sequential\n");
  435. ret = -EINVAL;
  436. goto err_cur;
  437. }
  438. /*
  439. * When all pages in a block has been written, a new block is selected
  440. * and writing is performed on the new block.
  441. */
  442. if (s.ppas[scan_ppa_idx(0, s.act_blk[0])].g.pg ==
  443. dev->lps_per_blk - 1) {
  444. ret = nvm_prepare_new_sysblks(dev, &s);
  445. if (ret)
  446. goto err_cur;
  447. }
  448. ret = nvm_write_and_verify(dev, new, &s);
  449. err_cur:
  450. kfree(cur);
  451. err_sysblk:
  452. mutex_unlock(&dev->mlock);
  453. return ret;
  454. }
  455. int nvm_init_sysblock(struct nvm_dev *dev, struct nvm_sb_info *info)
  456. {
  457. struct ppa_addr sysblk_ppas[MAX_SYSBLKS];
  458. struct sysblk_scan s;
  459. int ret;
  460. /*
  461. * 1. select master blocks and select first available blks
  462. * 2. get bad block list
  463. * 3. mark MAX_SYSBLKS block as host-based device allocated.
  464. * 4. write and verify data to block
  465. */
  466. if (!dev->ops->get_bb_tbl || !dev->ops->set_bb_tbl)
  467. return -EINVAL;
  468. if (!(dev->mccap & NVM_ID_CAP_SLC) || !dev->lps_per_blk) {
  469. pr_err("nvm: memory does not support SLC access\n");
  470. return -EINVAL;
  471. }
  472. /* Index all sysblocks and mark them as host-driven */
  473. nvm_setup_sysblk_scan(dev, &s, sysblk_ppas);
  474. mutex_lock(&dev->mlock);
  475. ret = nvm_get_all_sysblks(dev, &s, sysblk_ppas, 1);
  476. if (ret)
  477. goto err_mark;
  478. ret = nvm_set_bb_tbl(dev, &s, NVM_BLK_T_HOST);
  479. if (ret)
  480. goto err_mark;
  481. /* Write to the first block of each row */
  482. ret = nvm_write_and_verify(dev, info, &s);
  483. err_mark:
  484. mutex_unlock(&dev->mlock);
  485. return ret;
  486. }
  487. static int factory_nblks(int nblks)
  488. {
  489. /* Round up to nearest BITS_PER_LONG */
  490. return (nblks + (BITS_PER_LONG - 1)) & ~(BITS_PER_LONG - 1);
  491. }
  492. static unsigned int factory_blk_offset(struct nvm_dev *dev, struct ppa_addr ppa)
  493. {
  494. int nblks = factory_nblks(dev->blks_per_lun);
  495. return ((ppa.g.ch * dev->luns_per_chnl * nblks) + (ppa.g.lun * nblks)) /
  496. BITS_PER_LONG;
  497. }
  498. static int nvm_factory_blks(struct nvm_dev *dev, struct ppa_addr ppa,
  499. u8 *blks, int nr_blks,
  500. unsigned long *blk_bitmap, int flags)
  501. {
  502. int i, lunoff;
  503. nr_blks = nvm_bb_tbl_fold(dev, blks, nr_blks);
  504. if (nr_blks < 0)
  505. return nr_blks;
  506. lunoff = factory_blk_offset(dev, ppa);
  507. /* non-set bits correspond to the block must be erased */
  508. for (i = 0; i < nr_blks; i++) {
  509. switch (blks[i]) {
  510. case NVM_BLK_T_FREE:
  511. if (flags & NVM_FACTORY_ERASE_ONLY_USER)
  512. set_bit(i, &blk_bitmap[lunoff]);
  513. break;
  514. case NVM_BLK_T_HOST:
  515. if (!(flags & NVM_FACTORY_RESET_HOST_BLKS))
  516. set_bit(i, &blk_bitmap[lunoff]);
  517. break;
  518. case NVM_BLK_T_GRWN_BAD:
  519. if (!(flags & NVM_FACTORY_RESET_GRWN_BBLKS))
  520. set_bit(i, &blk_bitmap[lunoff]);
  521. break;
  522. default:
  523. set_bit(i, &blk_bitmap[lunoff]);
  524. break;
  525. }
  526. }
  527. return 0;
  528. }
  529. static int nvm_fact_get_blks(struct nvm_dev *dev, struct ppa_addr *erase_list,
  530. int max_ppas, unsigned long *blk_bitmap)
  531. {
  532. struct ppa_addr ppa;
  533. int ch, lun, blkid, idx, done = 0, ppa_cnt = 0;
  534. unsigned long *offset;
  535. while (!done) {
  536. done = 1;
  537. nvm_for_each_lun_ppa(dev, ppa, ch, lun) {
  538. idx = factory_blk_offset(dev, ppa);
  539. offset = &blk_bitmap[idx];
  540. blkid = find_first_zero_bit(offset,
  541. dev->blks_per_lun);
  542. if (blkid >= dev->blks_per_lun)
  543. continue;
  544. set_bit(blkid, offset);
  545. ppa.g.blk = blkid;
  546. pr_debug("nvm: erase ppa (%u %u %u)\n",
  547. ppa.g.ch,
  548. ppa.g.lun,
  549. ppa.g.blk);
  550. erase_list[ppa_cnt] = ppa;
  551. ppa_cnt++;
  552. done = 0;
  553. if (ppa_cnt == max_ppas)
  554. return ppa_cnt;
  555. }
  556. }
  557. return ppa_cnt;
  558. }
  559. static int nvm_fact_select_blks(struct nvm_dev *dev, unsigned long *blk_bitmap,
  560. int flags)
  561. {
  562. struct ppa_addr ppa;
  563. int ch, lun, nr_blks, ret = 0;
  564. u8 *blks;
  565. nr_blks = dev->blks_per_lun * dev->plane_mode;
  566. blks = kmalloc(nr_blks, GFP_KERNEL);
  567. if (!blks)
  568. return -ENOMEM;
  569. nvm_for_each_lun_ppa(dev, ppa, ch, lun) {
  570. ret = nvm_get_bb_tbl(dev, ppa, blks);
  571. if (ret)
  572. pr_err("nvm: failed bb tbl for ch%u lun%u\n",
  573. ppa.g.ch, ppa.g.blk);
  574. ret = nvm_factory_blks(dev, ppa, blks, nr_blks, blk_bitmap,
  575. flags);
  576. if (ret)
  577. break;
  578. }
  579. kfree(blks);
  580. return ret;
  581. }
  582. int nvm_dev_factory(struct nvm_dev *dev, int flags)
  583. {
  584. struct ppa_addr *ppas;
  585. int ppa_cnt, ret = -ENOMEM;
  586. int max_ppas = dev->ops->max_phys_sect / dev->nr_planes;
  587. struct ppa_addr sysblk_ppas[MAX_SYSBLKS];
  588. struct sysblk_scan s;
  589. unsigned long *blk_bitmap;
  590. blk_bitmap = kzalloc(factory_nblks(dev->blks_per_lun) * dev->nr_luns,
  591. GFP_KERNEL);
  592. if (!blk_bitmap)
  593. return ret;
  594. ppas = kcalloc(max_ppas, sizeof(struct ppa_addr), GFP_KERNEL);
  595. if (!ppas)
  596. goto err_blks;
  597. /* create list of blks to be erased */
  598. ret = nvm_fact_select_blks(dev, blk_bitmap, flags);
  599. if (ret)
  600. goto err_ppas;
  601. /* continue to erase until list of blks until empty */
  602. while ((ppa_cnt =
  603. nvm_fact_get_blks(dev, ppas, max_ppas, blk_bitmap)) > 0)
  604. nvm_erase_ppa(dev, ppas, ppa_cnt);
  605. /* mark host reserved blocks free */
  606. if (flags & NVM_FACTORY_RESET_HOST_BLKS) {
  607. nvm_setup_sysblk_scan(dev, &s, sysblk_ppas);
  608. mutex_lock(&dev->mlock);
  609. ret = nvm_get_all_sysblks(dev, &s, sysblk_ppas, 0);
  610. if (!ret)
  611. ret = nvm_set_bb_tbl(dev, &s, NVM_BLK_T_FREE);
  612. mutex_unlock(&dev->mlock);
  613. }
  614. err_ppas:
  615. kfree(ppas);
  616. err_blks:
  617. kfree(blk_bitmap);
  618. return ret;
  619. }
  620. EXPORT_SYMBOL(nvm_dev_factory);