2 * Copyright (C) 2015 Matias Bjorling. All rights reserved.
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.
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.
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,
20 #include <linux/lightnvm.h>
22 #define MAX_SYSBLKS 3 /* remember to update mapping scheme on change */
23 #define MAX_BLKS_PR_SYSBLK 2 /* 2 blks with 256 pages and 3000 erases
24 * enables ~1.5M updates per sysblk unit
28 /* A row is a collection of flash blocks for a system block. */
31 int act_blk[MAX_SYSBLKS];
34 struct ppa_addr ppas[MAX_SYSBLKS * MAX_BLKS_PR_SYSBLK];/* all sysblks */
37 static inline int scan_ppa_idx(int row, int blkid)
39 return (row * MAX_BLKS_PR_SYSBLK) + blkid;
42 static void nvm_sysblk_to_cpu(struct nvm_sb_info *info,
43 struct nvm_system_block *sb)
45 info->seqnr = be32_to_cpu(sb->seqnr);
46 info->erase_cnt = be32_to_cpu(sb->erase_cnt);
47 info->version = be16_to_cpu(sb->version);
48 strncpy(info->mmtype, sb->mmtype, NVM_MMTYPE_LEN);
49 info->fs_ppa.ppa = be64_to_cpu(sb->fs_ppa);
52 static void nvm_cpu_to_sysblk(struct nvm_system_block *sb,
53 struct nvm_sb_info *info)
55 sb->magic = cpu_to_be32(NVM_SYSBLK_MAGIC);
56 sb->seqnr = cpu_to_be32(info->seqnr);
57 sb->erase_cnt = cpu_to_be32(info->erase_cnt);
58 sb->version = cpu_to_be16(info->version);
59 strncpy(sb->mmtype, info->mmtype, NVM_MMTYPE_LEN);
60 sb->fs_ppa = cpu_to_be64(info->fs_ppa.ppa);
63 static int nvm_setup_sysblks(struct nvm_dev *dev, struct ppa_addr *sysblk_ppas)
65 int nr_rows = min_t(int, MAX_SYSBLKS, dev->nr_chnls);
68 for (i = 0; i < nr_rows; i++)
69 sysblk_ppas[i].ppa = 0;
71 /* if possible, place sysblk at first channel, middle channel and last
72 * channel of the device. If not, create only one or two sys blocks
74 switch (dev->nr_chnls) {
76 sysblk_ppas[1].g.ch = 1;
79 sysblk_ppas[0].g.ch = 0;
82 sysblk_ppas[0].g.ch = 0;
83 sysblk_ppas[1].g.ch = dev->nr_chnls / 2;
84 sysblk_ppas[2].g.ch = dev->nr_chnls - 1;
91 static void nvm_setup_sysblk_scan(struct nvm_dev *dev, struct sysblk_scan *s,
92 struct ppa_addr *sysblk_ppas)
94 memset(s, 0, sizeof(struct sysblk_scan));
95 s->nr_rows = nvm_setup_sysblks(dev, sysblk_ppas);
98 static int sysblk_get_free_blks(struct nvm_dev *dev, struct ppa_addr ppa,
99 u8 *blks, int nr_blks,
100 struct sysblk_scan *s)
102 struct ppa_addr *sppa;
105 nr_blks = nvm_bb_tbl_fold(dev, blks, nr_blks);
109 for (i = 0; i < nr_blks; i++) {
110 if (blks[i] == NVM_BLK_T_HOST)
113 if (blks[i] != NVM_BLK_T_FREE)
116 sppa = &s->ppas[scan_ppa_idx(s->row, blkid)];
117 sppa->g.ch = ppa.g.ch;
118 sppa->g.lun = ppa.g.lun;
123 pr_debug("nvm: use (%u %u %u) as sysblk\n",
124 sppa->g.ch, sppa->g.lun, sppa->g.blk);
125 if (blkid > MAX_BLKS_PR_SYSBLK - 1)
129 pr_err("nvm: sysblk failed get sysblk\n");
133 static int sysblk_get_host_blks(struct nvm_dev *dev, struct ppa_addr ppa,
134 u8 *blks, int nr_blks,
135 struct sysblk_scan *s)
137 int i, nr_sysblk = 0;
139 nr_blks = nvm_bb_tbl_fold(dev, blks, nr_blks);
143 for (i = 0; i < nr_blks; i++) {
144 if (blks[i] != NVM_BLK_T_HOST)
147 if (s->nr_ppas == MAX_BLKS_PR_SYSBLK * MAX_SYSBLKS) {
148 pr_err("nvm: too many host blks\n");
154 s->ppas[scan_ppa_idx(s->row, nr_sysblk)] = ppa;
162 static int nvm_get_all_sysblks(struct nvm_dev *dev, struct sysblk_scan *s,
163 struct ppa_addr *ppas, int get_free)
165 int i, nr_blks, ret = 0;
169 nr_blks = dev->blks_per_lun * dev->plane_mode;
171 blks = kmalloc(nr_blks, GFP_KERNEL);
175 for (i = 0; i < s->nr_rows; i++) {
178 ret = nvm_get_bb_tbl(dev, ppas[i], blks);
180 pr_err("nvm: failed bb tbl for ppa (%u %u)\n",
187 ret = sysblk_get_free_blks(dev, ppas[i], blks, nr_blks,
190 ret = sysblk_get_host_blks(dev, ppas[i], blks, nr_blks,
203 * scans a block for latest sysblk.
205 * 0 - newer sysblk not found. PPA is updated to latest page.
206 * 1 - newer sysblk found and stored in *cur. PPA is updated to
210 static int nvm_scan_block(struct nvm_dev *dev, struct ppa_addr *ppa,
211 struct nvm_system_block *sblk)
213 struct nvm_system_block *cur;
214 int pg, ret, found = 0;
216 /* the full buffer for a flash page is allocated. Only the first of it
217 * contains the system block information
219 cur = kmalloc(dev->pfpg_size, GFP_KERNEL);
223 /* perform linear scan through the block */
224 for (pg = 0; pg < dev->lps_per_blk; pg++) {
225 ppa->g.pg = ppa_to_slc(dev, pg);
227 ret = nvm_submit_ppa(dev, ppa, 1, NVM_OP_PREAD, NVM_IO_SLC_MODE,
228 cur, dev->pfpg_size);
230 if (ret == NVM_RSP_ERR_EMPTYPAGE) {
231 pr_debug("nvm: sysblk scan empty ppa (%u %u %u %u)\n",
238 pr_err("nvm: read failed (%x) for ppa (%u %u %u %u)",
244 break; /* if we can't read a page, continue to the
249 if (be32_to_cpu(cur->magic) != NVM_SYSBLK_MAGIC) {
250 pr_debug("nvm: scan break for ppa (%u %u %u %u)\n",
255 break; /* last valid page already found */
258 if (be32_to_cpu(cur->seqnr) < be32_to_cpu(sblk->seqnr))
261 memcpy(sblk, cur, sizeof(struct nvm_system_block));
270 static int nvm_sysblk_set_bb_tbl(struct nvm_dev *dev, struct sysblk_scan *s,
273 return nvm_set_bb_tbl(dev, s->ppas, s->nr_ppas, type);
276 static int nvm_write_and_verify(struct nvm_dev *dev, struct nvm_sb_info *info,
277 struct sysblk_scan *s)
279 struct nvm_system_block nvmsb;
281 int i, sect, ret = 0;
282 struct ppa_addr *ppas;
284 nvm_cpu_to_sysblk(&nvmsb, info);
286 buf = kzalloc(dev->pfpg_size, GFP_KERNEL);
289 memcpy(buf, &nvmsb, sizeof(struct nvm_system_block));
291 ppas = kcalloc(dev->sec_per_pg, sizeof(struct ppa_addr), GFP_KERNEL);
297 /* Write and verify */
298 for (i = 0; i < s->nr_rows; i++) {
299 ppas[0] = s->ppas[scan_ppa_idx(i, s->act_blk[i])];
301 pr_debug("nvm: writing sysblk to ppa (%u %u %u %u)\n",
307 /* Expand to all sectors within a flash page */
308 if (dev->sec_per_pg > 1) {
309 for (sect = 1; sect < dev->sec_per_pg; sect++) {
310 ppas[sect].ppa = ppas[0].ppa;
311 ppas[sect].g.sec = sect;
315 ret = nvm_submit_ppa(dev, ppas, dev->sec_per_pg, NVM_OP_PWRITE,
316 NVM_IO_SLC_MODE, buf, dev->pfpg_size);
318 pr_err("nvm: sysblk failed program (%u %u %u)\n",
325 ret = nvm_submit_ppa(dev, ppas, dev->sec_per_pg, NVM_OP_PREAD,
326 NVM_IO_SLC_MODE, buf, dev->pfpg_size);
328 pr_err("nvm: sysblk failed read (%u %u %u)\n",
335 if (memcmp(buf, &nvmsb, sizeof(struct nvm_system_block))) {
336 pr_err("nvm: sysblk failed verify (%u %u %u)\n",
352 static int nvm_prepare_new_sysblks(struct nvm_dev *dev, struct sysblk_scan *s)
355 unsigned long nxt_blk;
356 struct ppa_addr *ppa;
358 for (i = 0; i < s->nr_rows; i++) {
359 nxt_blk = (s->act_blk[i] + 1) % MAX_BLKS_PR_SYSBLK;
360 ppa = &s->ppas[scan_ppa_idx(i, nxt_blk)];
361 ppa->g.pg = ppa_to_slc(dev, 0);
363 ret = nvm_erase_ppa(dev, ppa, 1, 0);
367 s->act_blk[i] = nxt_blk;
373 int nvm_get_sysblock(struct nvm_dev *dev, struct nvm_sb_info *info)
375 struct ppa_addr sysblk_ppas[MAX_SYSBLKS];
376 struct sysblk_scan s;
377 struct nvm_system_block *cur;
382 * 1. setup sysblk locations
383 * 2. get bad block list
384 * 3. filter on host-specific (type 3)
385 * 4. iterate through all and find the highest seq nr.
386 * 5. return superblock information
389 if (!dev->ops->get_bb_tbl)
392 nvm_setup_sysblk_scan(dev, &s, sysblk_ppas);
394 mutex_lock(&dev->mlock);
395 ret = nvm_get_all_sysblks(dev, &s, sysblk_ppas, 0);
399 /* no sysblocks initialized */
403 cur = kzalloc(sizeof(struct nvm_system_block), GFP_KERNEL);
407 /* find the latest block across all sysblocks */
408 for (i = 0; i < s.nr_rows; i++) {
409 for (j = 0; j < MAX_BLKS_PR_SYSBLK; j++) {
410 struct ppa_addr ppa = s.ppas[scan_ppa_idx(i, j)];
412 ret = nvm_scan_block(dev, &ppa, cur);
420 nvm_sysblk_to_cpu(info, cur);
424 mutex_unlock(&dev->mlock);
431 int nvm_update_sysblock(struct nvm_dev *dev, struct nvm_sb_info *new)
433 /* 1. for each latest superblock
435 * a. write new flash page entry with the updated information
437 * a. find next available block on lun (linear search)
438 * if none, continue to next lun
439 * if none at all, report error. also report that it wasn't
440 * possible to write to all superblocks.
441 * c. write data to block.
443 struct ppa_addr sysblk_ppas[MAX_SYSBLKS];
444 struct sysblk_scan s;
445 struct nvm_system_block *cur;
446 int i, j, ppaidx, found = 0;
449 if (!dev->ops->get_bb_tbl)
452 nvm_setup_sysblk_scan(dev, &s, sysblk_ppas);
454 mutex_lock(&dev->mlock);
455 ret = nvm_get_all_sysblks(dev, &s, sysblk_ppas, 0);
459 cur = kzalloc(sizeof(struct nvm_system_block), GFP_KERNEL);
463 /* Get the latest sysblk for each sysblk row */
464 for (i = 0; i < s.nr_rows; i++) {
466 for (j = 0; j < MAX_BLKS_PR_SYSBLK; j++) {
467 ppaidx = scan_ppa_idx(i, j);
468 ret = nvm_scan_block(dev, &s.ppas[ppaidx], cur);
478 pr_err("nvm: no valid sysblks found to update\n");
484 * All sysblocks found. Check that they have same page id in their flash
487 for (i = 1; i < s.nr_rows; i++) {
488 struct ppa_addr l = s.ppas[scan_ppa_idx(0, s.act_blk[0])];
489 struct ppa_addr r = s.ppas[scan_ppa_idx(i, s.act_blk[i])];
491 if (l.g.pg != r.g.pg) {
492 pr_err("nvm: sysblks not on same page. Previous update failed.\n");
499 * Check that there haven't been another update to the seqnr since we
502 if ((new->seqnr - 1) != be32_to_cpu(cur->seqnr)) {
503 pr_err("nvm: seq is not sequential\n");
509 * When all pages in a block has been written, a new block is selected
510 * and writing is performed on the new block.
512 if (s.ppas[scan_ppa_idx(0, s.act_blk[0])].g.pg ==
513 dev->lps_per_blk - 1) {
514 ret = nvm_prepare_new_sysblks(dev, &s);
519 ret = nvm_write_and_verify(dev, new, &s);
523 mutex_unlock(&dev->mlock);
528 int nvm_init_sysblock(struct nvm_dev *dev, struct nvm_sb_info *info)
530 struct ppa_addr sysblk_ppas[MAX_SYSBLKS];
531 struct sysblk_scan s;
535 * 1. select master blocks and select first available blks
536 * 2. get bad block list
537 * 3. mark MAX_SYSBLKS block as host-based device allocated.
538 * 4. write and verify data to block
541 if (!dev->ops->get_bb_tbl || !dev->ops->set_bb_tbl)
544 if (!(dev->mccap & NVM_ID_CAP_SLC) || !dev->lps_per_blk) {
545 pr_err("nvm: memory does not support SLC access\n");
549 /* Index all sysblocks and mark them as host-driven */
550 nvm_setup_sysblk_scan(dev, &s, sysblk_ppas);
552 mutex_lock(&dev->mlock);
553 ret = nvm_get_all_sysblks(dev, &s, sysblk_ppas, 1);
557 ret = nvm_sysblk_set_bb_tbl(dev, &s, NVM_BLK_T_HOST);
561 /* Write to the first block of each row */
562 ret = nvm_write_and_verify(dev, info, &s);
564 mutex_unlock(&dev->mlock);
568 static int factory_nblks(int nblks)
570 /* Round up to nearest BITS_PER_LONG */
571 return (nblks + (BITS_PER_LONG - 1)) & ~(BITS_PER_LONG - 1);
574 static unsigned int factory_blk_offset(struct nvm_dev *dev, struct ppa_addr ppa)
576 int nblks = factory_nblks(dev->blks_per_lun);
578 return ((ppa.g.ch * dev->luns_per_chnl * nblks) + (ppa.g.lun * nblks)) /
582 static int nvm_factory_blks(struct nvm_dev *dev, struct ppa_addr ppa,
583 u8 *blks, int nr_blks,
584 unsigned long *blk_bitmap, int flags)
588 nr_blks = nvm_bb_tbl_fold(dev, blks, nr_blks);
592 lunoff = factory_blk_offset(dev, ppa);
594 /* non-set bits correspond to the block must be erased */
595 for (i = 0; i < nr_blks; i++) {
598 if (flags & NVM_FACTORY_ERASE_ONLY_USER)
599 set_bit(i, &blk_bitmap[lunoff]);
602 if (!(flags & NVM_FACTORY_RESET_HOST_BLKS))
603 set_bit(i, &blk_bitmap[lunoff]);
605 case NVM_BLK_T_GRWN_BAD:
606 if (!(flags & NVM_FACTORY_RESET_GRWN_BBLKS))
607 set_bit(i, &blk_bitmap[lunoff]);
610 set_bit(i, &blk_bitmap[lunoff]);
618 static int nvm_fact_get_blks(struct nvm_dev *dev, struct ppa_addr *erase_list,
619 int max_ppas, unsigned long *blk_bitmap)
622 int ch, lun, blkid, idx, done = 0, ppa_cnt = 0;
623 unsigned long *offset;
627 nvm_for_each_lun_ppa(dev, ppa, ch, lun) {
628 idx = factory_blk_offset(dev, ppa);
629 offset = &blk_bitmap[idx];
631 blkid = find_first_zero_bit(offset,
633 if (blkid >= dev->blks_per_lun)
635 set_bit(blkid, offset);
638 pr_debug("nvm: erase ppa (%u %u %u)\n",
643 erase_list[ppa_cnt] = ppa;
647 if (ppa_cnt == max_ppas)
655 static int nvm_fact_select_blks(struct nvm_dev *dev, unsigned long *blk_bitmap,
659 int ch, lun, nr_blks, ret = 0;
662 nr_blks = dev->blks_per_lun * dev->plane_mode;
663 blks = kmalloc(nr_blks, GFP_KERNEL);
667 nvm_for_each_lun_ppa(dev, ppa, ch, lun) {
668 ret = nvm_get_bb_tbl(dev, ppa, blks);
670 pr_err("nvm: failed bb tbl for ch%u lun%u\n",
671 ppa.g.ch, ppa.g.blk);
673 ret = nvm_factory_blks(dev, ppa, blks, nr_blks, blk_bitmap,
683 int nvm_dev_factory(struct nvm_dev *dev, int flags)
685 struct ppa_addr *ppas;
686 int ppa_cnt, ret = -ENOMEM;
687 int max_ppas = dev->ops->max_phys_sect / dev->nr_planes;
688 struct ppa_addr sysblk_ppas[MAX_SYSBLKS];
689 struct sysblk_scan s;
690 unsigned long *blk_bitmap;
692 blk_bitmap = kzalloc(factory_nblks(dev->blks_per_lun) * dev->nr_luns,
697 ppas = kcalloc(max_ppas, sizeof(struct ppa_addr), GFP_KERNEL);
701 /* create list of blks to be erased */
702 ret = nvm_fact_select_blks(dev, blk_bitmap, flags);
706 /* continue to erase until list of blks until empty */
708 nvm_fact_get_blks(dev, ppas, max_ppas, blk_bitmap)) > 0)
709 nvm_erase_ppa(dev, ppas, ppa_cnt, 0);
711 /* mark host reserved blocks free */
712 if (flags & NVM_FACTORY_RESET_HOST_BLKS) {
713 nvm_setup_sysblk_scan(dev, &s, sysblk_ppas);
714 mutex_lock(&dev->mlock);
715 ret = nvm_get_all_sysblks(dev, &s, sysblk_ppas, 0);
717 ret = nvm_sysblk_set_bb_tbl(dev, &s, NVM_BLK_T_FREE);
718 mutex_unlock(&dev->mlock);
726 EXPORT_SYMBOL(nvm_dev_factory);