Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /*
0003  *  Overview:
0004  *   Bad block table support for the NAND driver
0005  *
0006  *  Copyright © 2004 Thomas Gleixner (tglx@linutronix.de)
0007  *
0008  * Description:
0009  *
0010  * When nand_scan_bbt is called, then it tries to find the bad block table
0011  * depending on the options in the BBT descriptor(s). If no flash based BBT
0012  * (NAND_BBT_USE_FLASH) is specified then the device is scanned for factory
0013  * marked good / bad blocks. This information is used to create a memory BBT.
0014  * Once a new bad block is discovered then the "factory" information is updated
0015  * on the device.
0016  * If a flash based BBT is specified then the function first tries to find the
0017  * BBT on flash. If a BBT is found then the contents are read and the memory
0018  * based BBT is created. If a mirrored BBT is selected then the mirror is
0019  * searched too and the versions are compared. If the mirror has a greater
0020  * version number, then the mirror BBT is used to build the memory based BBT.
0021  * If the tables are not versioned, then we "or" the bad block information.
0022  * If one of the BBTs is out of date or does not exist it is (re)created.
0023  * If no BBT exists at all then the device is scanned for factory marked
0024  * good / bad blocks and the bad block tables are created.
0025  *
0026  * For manufacturer created BBTs like the one found on M-SYS DOC devices
0027  * the BBT is searched and read but never created
0028  *
0029  * The auto generated bad block table is located in the last good blocks
0030  * of the device. The table is mirrored, so it can be updated eventually.
0031  * The table is marked in the OOB area with an ident pattern and a version
0032  * number which indicates which of both tables is more up to date. If the NAND
0033  * controller needs the complete OOB area for the ECC information then the
0034  * option NAND_BBT_NO_OOB should be used (along with NAND_BBT_USE_FLASH, of
0035  * course): it moves the ident pattern and the version byte into the data area
0036  * and the OOB area will remain untouched.
0037  *
0038  * The table uses 2 bits per block
0039  * 11b:     block is good
0040  * 00b:     block is factory marked bad
0041  * 01b, 10b:    block is marked bad due to wear
0042  *
0043  * The memory bad block table uses the following scheme:
0044  * 00b:     block is good
0045  * 01b:     block is marked bad due to wear
0046  * 10b:     block is reserved (to protect the bbt area)
0047  * 11b:     block is factory marked bad
0048  *
0049  * Multichip devices like DOC store the bad block info per floor.
0050  *
0051  * Following assumptions are made:
0052  * - bbts start at a page boundary, if autolocated on a block boundary
0053  * - the space necessary for a bbt in FLASH does not exceed a block boundary
0054  */
0055 
0056 #include <linux/slab.h>
0057 #include <linux/types.h>
0058 #include <linux/mtd/mtd.h>
0059 #include <linux/mtd/bbm.h>
0060 #include <linux/bitops.h>
0061 #include <linux/delay.h>
0062 #include <linux/vmalloc.h>
0063 #include <linux/export.h>
0064 #include <linux/string.h>
0065 
0066 #include "internals.h"
0067 
0068 #define BBT_BLOCK_GOOD      0x00
0069 #define BBT_BLOCK_WORN      0x01
0070 #define BBT_BLOCK_RESERVED  0x02
0071 #define BBT_BLOCK_FACTORY_BAD   0x03
0072 
0073 #define BBT_ENTRY_MASK      0x03
0074 #define BBT_ENTRY_SHIFT     2
0075 
0076 static inline uint8_t bbt_get_entry(struct nand_chip *chip, int block)
0077 {
0078     uint8_t entry = chip->bbt[block >> BBT_ENTRY_SHIFT];
0079     entry >>= (block & BBT_ENTRY_MASK) * 2;
0080     return entry & BBT_ENTRY_MASK;
0081 }
0082 
0083 static inline void bbt_mark_entry(struct nand_chip *chip, int block,
0084         uint8_t mark)
0085 {
0086     uint8_t msk = (mark & BBT_ENTRY_MASK) << ((block & BBT_ENTRY_MASK) * 2);
0087     chip->bbt[block >> BBT_ENTRY_SHIFT] |= msk;
0088 }
0089 
0090 static int check_pattern_no_oob(uint8_t *buf, struct nand_bbt_descr *td)
0091 {
0092     if (memcmp(buf, td->pattern, td->len))
0093         return -1;
0094     return 0;
0095 }
0096 
0097 /**
0098  * check_pattern - [GENERIC] check if a pattern is in the buffer
0099  * @buf: the buffer to search
0100  * @len: the length of buffer to search
0101  * @paglen: the pagelength
0102  * @td: search pattern descriptor
0103  *
0104  * Check for a pattern at the given place. Used to search bad block tables and
0105  * good / bad block identifiers.
0106  */
0107 static int check_pattern(uint8_t *buf, int len, int paglen, struct nand_bbt_descr *td)
0108 {
0109     if (td->options & NAND_BBT_NO_OOB)
0110         return check_pattern_no_oob(buf, td);
0111 
0112     /* Compare the pattern */
0113     if (memcmp(buf + paglen + td->offs, td->pattern, td->len))
0114         return -1;
0115 
0116     return 0;
0117 }
0118 
0119 /**
0120  * check_short_pattern - [GENERIC] check if a pattern is in the buffer
0121  * @buf: the buffer to search
0122  * @td: search pattern descriptor
0123  *
0124  * Check for a pattern at the given place. Used to search bad block tables and
0125  * good / bad block identifiers. Same as check_pattern, but no optional empty
0126  * check.
0127  */
0128 static int check_short_pattern(uint8_t *buf, struct nand_bbt_descr *td)
0129 {
0130     /* Compare the pattern */
0131     if (memcmp(buf + td->offs, td->pattern, td->len))
0132         return -1;
0133     return 0;
0134 }
0135 
0136 /**
0137  * add_marker_len - compute the length of the marker in data area
0138  * @td: BBT descriptor used for computation
0139  *
0140  * The length will be 0 if the marker is located in OOB area.
0141  */
0142 static u32 add_marker_len(struct nand_bbt_descr *td)
0143 {
0144     u32 len;
0145 
0146     if (!(td->options & NAND_BBT_NO_OOB))
0147         return 0;
0148 
0149     len = td->len;
0150     if (td->options & NAND_BBT_VERSION)
0151         len++;
0152     return len;
0153 }
0154 
0155 /**
0156  * read_bbt - [GENERIC] Read the bad block table starting from page
0157  * @this: NAND chip object
0158  * @buf: temporary buffer
0159  * @page: the starting page
0160  * @num: the number of bbt descriptors to read
0161  * @td: the bbt describtion table
0162  * @offs: block number offset in the table
0163  *
0164  * Read the bad block table starting from page.
0165  */
0166 static int read_bbt(struct nand_chip *this, uint8_t *buf, int page, int num,
0167             struct nand_bbt_descr *td, int offs)
0168 {
0169     struct mtd_info *mtd = nand_to_mtd(this);
0170     int res, ret = 0, i, j, act = 0;
0171     size_t retlen, len, totlen;
0172     loff_t from;
0173     int bits = td->options & NAND_BBT_NRBITS_MSK;
0174     uint8_t msk = (uint8_t)((1 << bits) - 1);
0175     u32 marker_len;
0176     int reserved_block_code = td->reserved_block_code;
0177 
0178     totlen = (num * bits) >> 3;
0179     marker_len = add_marker_len(td);
0180     from = ((loff_t)page) << this->page_shift;
0181 
0182     while (totlen) {
0183         len = min(totlen, (size_t)(1 << this->bbt_erase_shift));
0184         if (marker_len) {
0185             /*
0186              * In case the BBT marker is not in the OOB area it
0187              * will be just in the first page.
0188              */
0189             len -= marker_len;
0190             from += marker_len;
0191             marker_len = 0;
0192         }
0193         res = mtd_read(mtd, from, len, &retlen, buf);
0194         if (res < 0) {
0195             if (mtd_is_eccerr(res)) {
0196                 pr_info("nand_bbt: ECC error in BBT at 0x%012llx\n",
0197                     from & ~mtd->writesize);
0198                 return res;
0199             } else if (mtd_is_bitflip(res)) {
0200                 pr_info("nand_bbt: corrected error in BBT at 0x%012llx\n",
0201                     from & ~mtd->writesize);
0202                 ret = res;
0203             } else {
0204                 pr_info("nand_bbt: error reading BBT\n");
0205                 return res;
0206             }
0207         }
0208 
0209         /* Analyse data */
0210         for (i = 0; i < len; i++) {
0211             uint8_t dat = buf[i];
0212             for (j = 0; j < 8; j += bits, act++) {
0213                 uint8_t tmp = (dat >> j) & msk;
0214                 if (tmp == msk)
0215                     continue;
0216                 if (reserved_block_code && (tmp == reserved_block_code)) {
0217                     pr_info("nand_read_bbt: reserved block at 0x%012llx\n",
0218                          (loff_t)(offs + act) <<
0219                          this->bbt_erase_shift);
0220                     bbt_mark_entry(this, offs + act,
0221                             BBT_BLOCK_RESERVED);
0222                     mtd->ecc_stats.bbtblocks++;
0223                     continue;
0224                 }
0225                 /*
0226                  * Leave it for now, if it's matured we can
0227                  * move this message to pr_debug.
0228                  */
0229                 pr_info("nand_read_bbt: bad block at 0x%012llx\n",
0230                      (loff_t)(offs + act) <<
0231                      this->bbt_erase_shift);
0232                 /* Factory marked bad or worn out? */
0233                 if (tmp == 0)
0234                     bbt_mark_entry(this, offs + act,
0235                             BBT_BLOCK_FACTORY_BAD);
0236                 else
0237                     bbt_mark_entry(this, offs + act,
0238                             BBT_BLOCK_WORN);
0239                 mtd->ecc_stats.badblocks++;
0240             }
0241         }
0242         totlen -= len;
0243         from += len;
0244     }
0245     return ret;
0246 }
0247 
0248 /**
0249  * read_abs_bbt - [GENERIC] Read the bad block table starting at a given page
0250  * @this: NAND chip object
0251  * @buf: temporary buffer
0252  * @td: descriptor for the bad block table
0253  * @chip: read the table for a specific chip, -1 read all chips; applies only if
0254  *        NAND_BBT_PERCHIP option is set
0255  *
0256  * Read the bad block table for all chips starting at a given page. We assume
0257  * that the bbt bits are in consecutive order.
0258  */
0259 static int read_abs_bbt(struct nand_chip *this, uint8_t *buf,
0260             struct nand_bbt_descr *td, int chip)
0261 {
0262     struct mtd_info *mtd = nand_to_mtd(this);
0263     u64 targetsize = nanddev_target_size(&this->base);
0264     int res = 0, i;
0265 
0266     if (td->options & NAND_BBT_PERCHIP) {
0267         int offs = 0;
0268         for (i = 0; i < nanddev_ntargets(&this->base); i++) {
0269             if (chip == -1 || chip == i)
0270                 res = read_bbt(this, buf, td->pages[i],
0271                     targetsize >> this->bbt_erase_shift,
0272                     td, offs);
0273             if (res)
0274                 return res;
0275             offs += targetsize >> this->bbt_erase_shift;
0276         }
0277     } else {
0278         res = read_bbt(this, buf, td->pages[0],
0279                 mtd->size >> this->bbt_erase_shift, td, 0);
0280         if (res)
0281             return res;
0282     }
0283     return 0;
0284 }
0285 
0286 /* BBT marker is in the first page, no OOB */
0287 static int scan_read_data(struct nand_chip *this, uint8_t *buf, loff_t offs,
0288               struct nand_bbt_descr *td)
0289 {
0290     struct mtd_info *mtd = nand_to_mtd(this);
0291     size_t retlen;
0292     size_t len;
0293 
0294     len = td->len;
0295     if (td->options & NAND_BBT_VERSION)
0296         len++;
0297 
0298     return mtd_read(mtd, offs, len, &retlen, buf);
0299 }
0300 
0301 /**
0302  * scan_read_oob - [GENERIC] Scan data+OOB region to buffer
0303  * @this: NAND chip object
0304  * @buf: temporary buffer
0305  * @offs: offset at which to scan
0306  * @len: length of data region to read
0307  *
0308  * Scan read data from data+OOB. May traverse multiple pages, interleaving
0309  * page,OOB,page,OOB,... in buf. Completes transfer and returns the "strongest"
0310  * ECC condition (error or bitflip). May quit on the first (non-ECC) error.
0311  */
0312 static int scan_read_oob(struct nand_chip *this, uint8_t *buf, loff_t offs,
0313              size_t len)
0314 {
0315     struct mtd_info *mtd = nand_to_mtd(this);
0316     struct mtd_oob_ops ops;
0317     int res, ret = 0;
0318 
0319     ops.mode = MTD_OPS_PLACE_OOB;
0320     ops.ooboffs = 0;
0321     ops.ooblen = mtd->oobsize;
0322 
0323     while (len > 0) {
0324         ops.datbuf = buf;
0325         ops.len = min(len, (size_t)mtd->writesize);
0326         ops.oobbuf = buf + ops.len;
0327 
0328         res = mtd_read_oob(mtd, offs, &ops);
0329         if (res) {
0330             if (!mtd_is_bitflip_or_eccerr(res))
0331                 return res;
0332             else if (mtd_is_eccerr(res) || !ret)
0333                 ret = res;
0334         }
0335 
0336         buf += mtd->oobsize + mtd->writesize;
0337         len -= mtd->writesize;
0338         offs += mtd->writesize;
0339     }
0340     return ret;
0341 }
0342 
0343 static int scan_read(struct nand_chip *this, uint8_t *buf, loff_t offs,
0344              size_t len, struct nand_bbt_descr *td)
0345 {
0346     if (td->options & NAND_BBT_NO_OOB)
0347         return scan_read_data(this, buf, offs, td);
0348     else
0349         return scan_read_oob(this, buf, offs, len);
0350 }
0351 
0352 /* Scan write data with oob to flash */
0353 static int scan_write_bbt(struct nand_chip *this, loff_t offs, size_t len,
0354               uint8_t *buf, uint8_t *oob)
0355 {
0356     struct mtd_info *mtd = nand_to_mtd(this);
0357     struct mtd_oob_ops ops;
0358 
0359     ops.mode = MTD_OPS_PLACE_OOB;
0360     ops.ooboffs = 0;
0361     ops.ooblen = mtd->oobsize;
0362     ops.datbuf = buf;
0363     ops.oobbuf = oob;
0364     ops.len = len;
0365 
0366     return mtd_write_oob(mtd, offs, &ops);
0367 }
0368 
0369 static u32 bbt_get_ver_offs(struct nand_chip *this, struct nand_bbt_descr *td)
0370 {
0371     struct mtd_info *mtd = nand_to_mtd(this);
0372     u32 ver_offs = td->veroffs;
0373 
0374     if (!(td->options & NAND_BBT_NO_OOB))
0375         ver_offs += mtd->writesize;
0376     return ver_offs;
0377 }
0378 
0379 /**
0380  * read_abs_bbts - [GENERIC] Read the bad block table(s) for all chips starting at a given page
0381  * @this: NAND chip object
0382  * @buf: temporary buffer
0383  * @td: descriptor for the bad block table
0384  * @md: descriptor for the bad block table mirror
0385  *
0386  * Read the bad block table(s) for all chips starting at a given page. We
0387  * assume that the bbt bits are in consecutive order.
0388  */
0389 static void read_abs_bbts(struct nand_chip *this, uint8_t *buf,
0390               struct nand_bbt_descr *td, struct nand_bbt_descr *md)
0391 {
0392     struct mtd_info *mtd = nand_to_mtd(this);
0393 
0394     /* Read the primary version, if available */
0395     if (td->options & NAND_BBT_VERSION) {
0396         scan_read(this, buf, (loff_t)td->pages[0] << this->page_shift,
0397               mtd->writesize, td);
0398         td->version[0] = buf[bbt_get_ver_offs(this, td)];
0399         pr_info("Bad block table at page %d, version 0x%02X\n",
0400              td->pages[0], td->version[0]);
0401     }
0402 
0403     /* Read the mirror version, if available */
0404     if (md && (md->options & NAND_BBT_VERSION)) {
0405         scan_read(this, buf, (loff_t)md->pages[0] << this->page_shift,
0406               mtd->writesize, md);
0407         md->version[0] = buf[bbt_get_ver_offs(this, md)];
0408         pr_info("Bad block table at page %d, version 0x%02X\n",
0409              md->pages[0], md->version[0]);
0410     }
0411 }
0412 
0413 /* Scan a given block partially */
0414 static int scan_block_fast(struct nand_chip *this, struct nand_bbt_descr *bd,
0415                loff_t offs, uint8_t *buf)
0416 {
0417     struct mtd_info *mtd = nand_to_mtd(this);
0418 
0419     struct mtd_oob_ops ops;
0420     int ret, page_offset;
0421 
0422     ops.ooblen = mtd->oobsize;
0423     ops.oobbuf = buf;
0424     ops.ooboffs = 0;
0425     ops.datbuf = NULL;
0426     ops.mode = MTD_OPS_PLACE_OOB;
0427 
0428     page_offset = nand_bbm_get_next_page(this, 0);
0429 
0430     while (page_offset >= 0) {
0431         /*
0432          * Read the full oob until read_oob is fixed to handle single
0433          * byte reads for 16 bit buswidth.
0434          */
0435         ret = mtd_read_oob(mtd, offs + (page_offset * mtd->writesize),
0436                    &ops);
0437         /* Ignore ECC errors when checking for BBM */
0438         if (ret && !mtd_is_bitflip_or_eccerr(ret))
0439             return ret;
0440 
0441         if (check_short_pattern(buf, bd))
0442             return 1;
0443 
0444         page_offset = nand_bbm_get_next_page(this, page_offset + 1);
0445     }
0446 
0447     return 0;
0448 }
0449 
0450 /* Check if a potential BBT block is marked as bad */
0451 static int bbt_block_checkbad(struct nand_chip *this, struct nand_bbt_descr *td,
0452                   loff_t offs, uint8_t *buf)
0453 {
0454     struct nand_bbt_descr *bd = this->badblock_pattern;
0455 
0456     /*
0457      * No need to check for a bad BBT block if the BBM area overlaps with
0458      * the bad block table marker area in OOB since writing a BBM here
0459      * invalidates the bad block table marker anyway.
0460      */
0461     if (!(td->options & NAND_BBT_NO_OOB) &&
0462         td->offs >= bd->offs && td->offs < bd->offs + bd->len)
0463         return 0;
0464 
0465     /*
0466      * There is no point in checking for a bad block marker if writing
0467      * such marker is not supported
0468      */
0469     if (this->bbt_options & NAND_BBT_NO_OOB_BBM ||
0470         this->options & NAND_NO_BBM_QUIRK)
0471         return 0;
0472 
0473     if (scan_block_fast(this, bd, offs, buf) > 0)
0474         return 1;
0475 
0476     return 0;
0477 }
0478 
0479 /**
0480  * create_bbt - [GENERIC] Create a bad block table by scanning the device
0481  * @this: NAND chip object
0482  * @buf: temporary buffer
0483  * @bd: descriptor for the good/bad block search pattern
0484  * @chip: create the table for a specific chip, -1 read all chips; applies only
0485  *        if NAND_BBT_PERCHIP option is set
0486  *
0487  * Create a bad block table by scanning the device for the given good/bad block
0488  * identify pattern.
0489  */
0490 static int create_bbt(struct nand_chip *this, uint8_t *buf,
0491               struct nand_bbt_descr *bd, int chip)
0492 {
0493     u64 targetsize = nanddev_target_size(&this->base);
0494     struct mtd_info *mtd = nand_to_mtd(this);
0495     int i, numblocks, startblock;
0496     loff_t from;
0497 
0498     pr_info("Scanning device for bad blocks\n");
0499 
0500     if (chip == -1) {
0501         numblocks = mtd->size >> this->bbt_erase_shift;
0502         startblock = 0;
0503         from = 0;
0504     } else {
0505         if (chip >= nanddev_ntargets(&this->base)) {
0506             pr_warn("create_bbt(): chipnr (%d) > available chips (%d)\n",
0507                     chip + 1, nanddev_ntargets(&this->base));
0508             return -EINVAL;
0509         }
0510         numblocks = targetsize >> this->bbt_erase_shift;
0511         startblock = chip * numblocks;
0512         numblocks += startblock;
0513         from = (loff_t)startblock << this->bbt_erase_shift;
0514     }
0515 
0516     for (i = startblock; i < numblocks; i++) {
0517         int ret;
0518 
0519         BUG_ON(bd->options & NAND_BBT_NO_OOB);
0520 
0521         ret = scan_block_fast(this, bd, from, buf);
0522         if (ret < 0)
0523             return ret;
0524 
0525         if (ret) {
0526             bbt_mark_entry(this, i, BBT_BLOCK_FACTORY_BAD);
0527             pr_warn("Bad eraseblock %d at 0x%012llx\n",
0528                 i, (unsigned long long)from);
0529             mtd->ecc_stats.badblocks++;
0530         }
0531 
0532         from += (1 << this->bbt_erase_shift);
0533     }
0534     return 0;
0535 }
0536 
0537 /**
0538  * search_bbt - [GENERIC] scan the device for a specific bad block table
0539  * @this: NAND chip object
0540  * @buf: temporary buffer
0541  * @td: descriptor for the bad block table
0542  *
0543  * Read the bad block table by searching for a given ident pattern. Search is
0544  * preformed either from the beginning up or from the end of the device
0545  * downwards. The search starts always at the start of a block. If the option
0546  * NAND_BBT_PERCHIP is given, each chip is searched for a bbt, which contains
0547  * the bad block information of this chip. This is necessary to provide support
0548  * for certain DOC devices.
0549  *
0550  * The bbt ident pattern resides in the oob area of the first page in a block.
0551  */
0552 static int search_bbt(struct nand_chip *this, uint8_t *buf,
0553               struct nand_bbt_descr *td)
0554 {
0555     u64 targetsize = nanddev_target_size(&this->base);
0556     struct mtd_info *mtd = nand_to_mtd(this);
0557     int i, chips;
0558     int startblock, block, dir;
0559     int scanlen = mtd->writesize + mtd->oobsize;
0560     int bbtblocks;
0561     int blocktopage = this->bbt_erase_shift - this->page_shift;
0562 
0563     /* Search direction top -> down? */
0564     if (td->options & NAND_BBT_LASTBLOCK) {
0565         startblock = (mtd->size >> this->bbt_erase_shift) - 1;
0566         dir = -1;
0567     } else {
0568         startblock = 0;
0569         dir = 1;
0570     }
0571 
0572     /* Do we have a bbt per chip? */
0573     if (td->options & NAND_BBT_PERCHIP) {
0574         chips = nanddev_ntargets(&this->base);
0575         bbtblocks = targetsize >> this->bbt_erase_shift;
0576         startblock &= bbtblocks - 1;
0577     } else {
0578         chips = 1;
0579         bbtblocks = mtd->size >> this->bbt_erase_shift;
0580     }
0581 
0582     for (i = 0; i < chips; i++) {
0583         /* Reset version information */
0584         td->version[i] = 0;
0585         td->pages[i] = -1;
0586         /* Scan the maximum number of blocks */
0587         for (block = 0; block < td->maxblocks; block++) {
0588 
0589             int actblock = startblock + dir * block;
0590             loff_t offs = (loff_t)actblock << this->bbt_erase_shift;
0591 
0592             /* Check if block is marked bad */
0593             if (bbt_block_checkbad(this, td, offs, buf))
0594                 continue;
0595 
0596             /* Read first page */
0597             scan_read(this, buf, offs, mtd->writesize, td);
0598             if (!check_pattern(buf, scanlen, mtd->writesize, td)) {
0599                 td->pages[i] = actblock << blocktopage;
0600                 if (td->options & NAND_BBT_VERSION) {
0601                     offs = bbt_get_ver_offs(this, td);
0602                     td->version[i] = buf[offs];
0603                 }
0604                 break;
0605             }
0606         }
0607         startblock += targetsize >> this->bbt_erase_shift;
0608     }
0609     /* Check, if we found a bbt for each requested chip */
0610     for (i = 0; i < chips; i++) {
0611         if (td->pages[i] == -1)
0612             pr_warn("Bad block table not found for chip %d\n", i);
0613         else
0614             pr_info("Bad block table found at page %d, version 0x%02X\n",
0615                 td->pages[i], td->version[i]);
0616     }
0617     return 0;
0618 }
0619 
0620 /**
0621  * search_read_bbts - [GENERIC] scan the device for bad block table(s)
0622  * @this: NAND chip object
0623  * @buf: temporary buffer
0624  * @td: descriptor for the bad block table
0625  * @md: descriptor for the bad block table mirror
0626  *
0627  * Search and read the bad block table(s).
0628  */
0629 static void search_read_bbts(struct nand_chip *this, uint8_t *buf,
0630                  struct nand_bbt_descr *td,
0631                  struct nand_bbt_descr *md)
0632 {
0633     /* Search the primary table */
0634     search_bbt(this, buf, td);
0635 
0636     /* Search the mirror table */
0637     if (md)
0638         search_bbt(this, buf, md);
0639 }
0640 
0641 /**
0642  * get_bbt_block - Get the first valid eraseblock suitable to store a BBT
0643  * @this: the NAND device
0644  * @td: the BBT description
0645  * @md: the mirror BBT descriptor
0646  * @chip: the CHIP selector
0647  *
0648  * This functions returns a positive block number pointing a valid eraseblock
0649  * suitable to store a BBT (i.e. in the range reserved for BBT), or -ENOSPC if
0650  * all blocks are already used of marked bad. If td->pages[chip] was already
0651  * pointing to a valid block we re-use it, otherwise we search for the next
0652  * valid one.
0653  */
0654 static int get_bbt_block(struct nand_chip *this, struct nand_bbt_descr *td,
0655              struct nand_bbt_descr *md, int chip)
0656 {
0657     u64 targetsize = nanddev_target_size(&this->base);
0658     int startblock, dir, page, numblocks, i;
0659 
0660     /*
0661      * There was already a version of the table, reuse the page. This
0662      * applies for absolute placement too, as we have the page number in
0663      * td->pages.
0664      */
0665     if (td->pages[chip] != -1)
0666         return td->pages[chip] >>
0667                 (this->bbt_erase_shift - this->page_shift);
0668 
0669     numblocks = (int)(targetsize >> this->bbt_erase_shift);
0670     if (!(td->options & NAND_BBT_PERCHIP))
0671         numblocks *= nanddev_ntargets(&this->base);
0672 
0673     /*
0674      * Automatic placement of the bad block table. Search direction
0675      * top -> down?
0676      */
0677     if (td->options & NAND_BBT_LASTBLOCK) {
0678         startblock = numblocks * (chip + 1) - 1;
0679         dir = -1;
0680     } else {
0681         startblock = chip * numblocks;
0682         dir = 1;
0683     }
0684 
0685     for (i = 0; i < td->maxblocks; i++) {
0686         int block = startblock + dir * i;
0687 
0688         /* Check, if the block is bad */
0689         switch (bbt_get_entry(this, block)) {
0690         case BBT_BLOCK_WORN:
0691         case BBT_BLOCK_FACTORY_BAD:
0692             continue;
0693         }
0694 
0695         page = block << (this->bbt_erase_shift - this->page_shift);
0696 
0697         /* Check, if the block is used by the mirror table */
0698         if (!md || md->pages[chip] != page)
0699             return block;
0700     }
0701 
0702     return -ENOSPC;
0703 }
0704 
0705 /**
0706  * mark_bbt_block_bad - Mark one of the block reserved for BBT bad
0707  * @this: the NAND device
0708  * @td: the BBT description
0709  * @chip: the CHIP selector
0710  * @block: the BBT block to mark
0711  *
0712  * Blocks reserved for BBT can become bad. This functions is an helper to mark
0713  * such blocks as bad. It takes care of updating the in-memory BBT, marking the
0714  * block as bad using a bad block marker and invalidating the associated
0715  * td->pages[] entry.
0716  */
0717 static void mark_bbt_block_bad(struct nand_chip *this,
0718                    struct nand_bbt_descr *td,
0719                    int chip, int block)
0720 {
0721     loff_t to;
0722     int res;
0723 
0724     bbt_mark_entry(this, block, BBT_BLOCK_WORN);
0725 
0726     to = (loff_t)block << this->bbt_erase_shift;
0727     res = nand_markbad_bbm(this, to);
0728     if (res)
0729         pr_warn("nand_bbt: error %d while marking block %d bad\n",
0730             res, block);
0731 
0732     td->pages[chip] = -1;
0733 }
0734 
0735 /**
0736  * write_bbt - [GENERIC] (Re)write the bad block table
0737  * @this: NAND chip object
0738  * @buf: temporary buffer
0739  * @td: descriptor for the bad block table
0740  * @md: descriptor for the bad block table mirror
0741  * @chipsel: selector for a specific chip, -1 for all
0742  *
0743  * (Re)write the bad block table.
0744  */
0745 static int write_bbt(struct nand_chip *this, uint8_t *buf,
0746              struct nand_bbt_descr *td, struct nand_bbt_descr *md,
0747              int chipsel)
0748 {
0749     u64 targetsize = nanddev_target_size(&this->base);
0750     struct mtd_info *mtd = nand_to_mtd(this);
0751     struct erase_info einfo;
0752     int i, res, chip = 0;
0753     int bits, page, offs, numblocks, sft, sftmsk;
0754     int nrchips, pageoffs, ooboffs;
0755     uint8_t msk[4];
0756     uint8_t rcode = td->reserved_block_code;
0757     size_t retlen, len = 0;
0758     loff_t to;
0759     struct mtd_oob_ops ops;
0760 
0761     ops.ooblen = mtd->oobsize;
0762     ops.ooboffs = 0;
0763     ops.datbuf = NULL;
0764     ops.mode = MTD_OPS_PLACE_OOB;
0765 
0766     if (!rcode)
0767         rcode = 0xff;
0768     /* Write bad block table per chip rather than per device? */
0769     if (td->options & NAND_BBT_PERCHIP) {
0770         numblocks = (int)(targetsize >> this->bbt_erase_shift);
0771         /* Full device write or specific chip? */
0772         if (chipsel == -1) {
0773             nrchips = nanddev_ntargets(&this->base);
0774         } else {
0775             nrchips = chipsel + 1;
0776             chip = chipsel;
0777         }
0778     } else {
0779         numblocks = (int)(mtd->size >> this->bbt_erase_shift);
0780         nrchips = 1;
0781     }
0782 
0783     /* Loop through the chips */
0784     while (chip < nrchips) {
0785         int block;
0786 
0787         block = get_bbt_block(this, td, md, chip);
0788         if (block < 0) {
0789             pr_err("No space left to write bad block table\n");
0790             res = block;
0791             goto outerr;
0792         }
0793 
0794         /*
0795          * get_bbt_block() returns a block number, shift the value to
0796          * get a page number.
0797          */
0798         page = block << (this->bbt_erase_shift - this->page_shift);
0799 
0800         /* Set up shift count and masks for the flash table */
0801         bits = td->options & NAND_BBT_NRBITS_MSK;
0802         msk[2] = ~rcode;
0803         switch (bits) {
0804         case 1: sft = 3; sftmsk = 0x07; msk[0] = 0x00; msk[1] = 0x01;
0805             msk[3] = 0x01;
0806             break;
0807         case 2: sft = 2; sftmsk = 0x06; msk[0] = 0x00; msk[1] = 0x01;
0808             msk[3] = 0x03;
0809             break;
0810         case 4: sft = 1; sftmsk = 0x04; msk[0] = 0x00; msk[1] = 0x0C;
0811             msk[3] = 0x0f;
0812             break;
0813         case 8: sft = 0; sftmsk = 0x00; msk[0] = 0x00; msk[1] = 0x0F;
0814             msk[3] = 0xff;
0815             break;
0816         default: return -EINVAL;
0817         }
0818 
0819         to = ((loff_t)page) << this->page_shift;
0820 
0821         /* Must we save the block contents? */
0822         if (td->options & NAND_BBT_SAVECONTENT) {
0823             /* Make it block aligned */
0824             to &= ~(((loff_t)1 << this->bbt_erase_shift) - 1);
0825             len = 1 << this->bbt_erase_shift;
0826             res = mtd_read(mtd, to, len, &retlen, buf);
0827             if (res < 0) {
0828                 if (retlen != len) {
0829                     pr_info("nand_bbt: error reading block for writing the bad block table\n");
0830                     return res;
0831                 }
0832                 pr_warn("nand_bbt: ECC error while reading block for writing bad block table\n");
0833             }
0834             /* Read oob data */
0835             ops.ooblen = (len >> this->page_shift) * mtd->oobsize;
0836             ops.oobbuf = &buf[len];
0837             res = mtd_read_oob(mtd, to + mtd->writesize, &ops);
0838             if (res < 0 || ops.oobretlen != ops.ooblen)
0839                 goto outerr;
0840 
0841             /* Calc the byte offset in the buffer */
0842             pageoffs = page - (int)(to >> this->page_shift);
0843             offs = pageoffs << this->page_shift;
0844             /* Preset the bbt area with 0xff */
0845             memset(&buf[offs], 0xff, (size_t)(numblocks >> sft));
0846             ooboffs = len + (pageoffs * mtd->oobsize);
0847 
0848         } else if (td->options & NAND_BBT_NO_OOB) {
0849             ooboffs = 0;
0850             offs = td->len;
0851             /* The version byte */
0852             if (td->options & NAND_BBT_VERSION)
0853                 offs++;
0854             /* Calc length */
0855             len = (size_t)(numblocks >> sft);
0856             len += offs;
0857             /* Make it page aligned! */
0858             len = ALIGN(len, mtd->writesize);
0859             /* Preset the buffer with 0xff */
0860             memset(buf, 0xff, len);
0861             /* Pattern is located at the begin of first page */
0862             memcpy(buf, td->pattern, td->len);
0863         } else {
0864             /* Calc length */
0865             len = (size_t)(numblocks >> sft);
0866             /* Make it page aligned! */
0867             len = ALIGN(len, mtd->writesize);
0868             /* Preset the buffer with 0xff */
0869             memset(buf, 0xff, len +
0870                    (len >> this->page_shift)* mtd->oobsize);
0871             offs = 0;
0872             ooboffs = len;
0873             /* Pattern is located in oob area of first page */
0874             memcpy(&buf[ooboffs + td->offs], td->pattern, td->len);
0875         }
0876 
0877         if (td->options & NAND_BBT_VERSION)
0878             buf[ooboffs + td->veroffs] = td->version[chip];
0879 
0880         /* Walk through the memory table */
0881         for (i = 0; i < numblocks; i++) {
0882             uint8_t dat;
0883             int sftcnt = (i << (3 - sft)) & sftmsk;
0884             dat = bbt_get_entry(this, chip * numblocks + i);
0885             /* Do not store the reserved bbt blocks! */
0886             buf[offs + (i >> sft)] &= ~(msk[dat] << sftcnt);
0887         }
0888 
0889         memset(&einfo, 0, sizeof(einfo));
0890         einfo.addr = to;
0891         einfo.len = 1 << this->bbt_erase_shift;
0892         res = nand_erase_nand(this, &einfo, 1);
0893         if (res < 0) {
0894             pr_warn("nand_bbt: error while erasing BBT block %d\n",
0895                 res);
0896             mark_bbt_block_bad(this, td, chip, block);
0897             continue;
0898         }
0899 
0900         res = scan_write_bbt(this, to, len, buf,
0901                      td->options & NAND_BBT_NO_OOB ?
0902                      NULL : &buf[len]);
0903         if (res < 0) {
0904             pr_warn("nand_bbt: error while writing BBT block %d\n",
0905                 res);
0906             mark_bbt_block_bad(this, td, chip, block);
0907             continue;
0908         }
0909 
0910         pr_info("Bad block table written to 0x%012llx, version 0x%02X\n",
0911              (unsigned long long)to, td->version[chip]);
0912 
0913         /* Mark it as used */
0914         td->pages[chip++] = page;
0915     }
0916     return 0;
0917 
0918  outerr:
0919     pr_warn("nand_bbt: error while writing bad block table %d\n", res);
0920     return res;
0921 }
0922 
0923 /**
0924  * nand_memory_bbt - [GENERIC] create a memory based bad block table
0925  * @this: NAND chip object
0926  * @bd: descriptor for the good/bad block search pattern
0927  *
0928  * The function creates a memory based bbt by scanning the device for
0929  * manufacturer / software marked good / bad blocks.
0930  */
0931 static inline int nand_memory_bbt(struct nand_chip *this,
0932                   struct nand_bbt_descr *bd)
0933 {
0934     u8 *pagebuf = nand_get_data_buf(this);
0935 
0936     return create_bbt(this, pagebuf, bd, -1);
0937 }
0938 
0939 /**
0940  * check_create - [GENERIC] create and write bbt(s) if necessary
0941  * @this: the NAND device
0942  * @buf: temporary buffer
0943  * @bd: descriptor for the good/bad block search pattern
0944  *
0945  * The function checks the results of the previous call to read_bbt and creates
0946  * / updates the bbt(s) if necessary. Creation is necessary if no bbt was found
0947  * for the chip/device. Update is necessary if one of the tables is missing or
0948  * the version nr. of one table is less than the other.
0949  */
0950 static int check_create(struct nand_chip *this, uint8_t *buf,
0951             struct nand_bbt_descr *bd)
0952 {
0953     int i, chips, writeops, create, chipsel, res, res2;
0954     struct nand_bbt_descr *td = this->bbt_td;
0955     struct nand_bbt_descr *md = this->bbt_md;
0956     struct nand_bbt_descr *rd, *rd2;
0957 
0958     /* Do we have a bbt per chip? */
0959     if (td->options & NAND_BBT_PERCHIP)
0960         chips = nanddev_ntargets(&this->base);
0961     else
0962         chips = 1;
0963 
0964     for (i = 0; i < chips; i++) {
0965         writeops = 0;
0966         create = 0;
0967         rd = NULL;
0968         rd2 = NULL;
0969         res = res2 = 0;
0970         /* Per chip or per device? */
0971         chipsel = (td->options & NAND_BBT_PERCHIP) ? i : -1;
0972         /* Mirrored table available? */
0973         if (md) {
0974             if (td->pages[i] == -1 && md->pages[i] == -1) {
0975                 create = 1;
0976                 writeops = 0x03;
0977             } else if (td->pages[i] == -1) {
0978                 rd = md;
0979                 writeops = 0x01;
0980             } else if (md->pages[i] == -1) {
0981                 rd = td;
0982                 writeops = 0x02;
0983             } else if (td->version[i] == md->version[i]) {
0984                 rd = td;
0985                 if (!(td->options & NAND_BBT_VERSION))
0986                     rd2 = md;
0987             } else if (((int8_t)(td->version[i] - md->version[i])) > 0) {
0988                 rd = td;
0989                 writeops = 0x02;
0990             } else {
0991                 rd = md;
0992                 writeops = 0x01;
0993             }
0994         } else {
0995             if (td->pages[i] == -1) {
0996                 create = 1;
0997                 writeops = 0x01;
0998             } else {
0999                 rd = td;
1000             }
1001         }
1002 
1003         if (create) {
1004             /* Create the bad block table by scanning the device? */
1005             if (!(td->options & NAND_BBT_CREATE))
1006                 continue;
1007 
1008             /* Create the table in memory by scanning the chip(s) */
1009             if (!(this->bbt_options & NAND_BBT_CREATE_EMPTY))
1010                 create_bbt(this, buf, bd, chipsel);
1011 
1012             td->version[i] = 1;
1013             if (md)
1014                 md->version[i] = 1;
1015         }
1016 
1017         /* Read back first? */
1018         if (rd) {
1019             res = read_abs_bbt(this, buf, rd, chipsel);
1020             if (mtd_is_eccerr(res)) {
1021                 /* Mark table as invalid */
1022                 rd->pages[i] = -1;
1023                 rd->version[i] = 0;
1024                 i--;
1025                 continue;
1026             }
1027         }
1028         /* If they weren't versioned, read both */
1029         if (rd2) {
1030             res2 = read_abs_bbt(this, buf, rd2, chipsel);
1031             if (mtd_is_eccerr(res2)) {
1032                 /* Mark table as invalid */
1033                 rd2->pages[i] = -1;
1034                 rd2->version[i] = 0;
1035                 i--;
1036                 continue;
1037             }
1038         }
1039 
1040         /* Scrub the flash table(s)? */
1041         if (mtd_is_bitflip(res) || mtd_is_bitflip(res2))
1042             writeops = 0x03;
1043 
1044         /* Update version numbers before writing */
1045         if (md) {
1046             td->version[i] = max(td->version[i], md->version[i]);
1047             md->version[i] = td->version[i];
1048         }
1049 
1050         /* Write the bad block table to the device? */
1051         if ((writeops & 0x01) && (td->options & NAND_BBT_WRITE)) {
1052             res = write_bbt(this, buf, td, md, chipsel);
1053             if (res < 0)
1054                 return res;
1055         }
1056 
1057         /* Write the mirror bad block table to the device? */
1058         if ((writeops & 0x02) && md && (md->options & NAND_BBT_WRITE)) {
1059             res = write_bbt(this, buf, md, td, chipsel);
1060             if (res < 0)
1061                 return res;
1062         }
1063     }
1064     return 0;
1065 }
1066 
1067 /**
1068  * nand_update_bbt - update bad block table(s)
1069  * @this: the NAND device
1070  * @offs: the offset of the newly marked block
1071  *
1072  * The function updates the bad block table(s).
1073  */
1074 static int nand_update_bbt(struct nand_chip *this, loff_t offs)
1075 {
1076     struct mtd_info *mtd = nand_to_mtd(this);
1077     int len, res = 0;
1078     int chip, chipsel;
1079     uint8_t *buf;
1080     struct nand_bbt_descr *td = this->bbt_td;
1081     struct nand_bbt_descr *md = this->bbt_md;
1082 
1083     if (!this->bbt || !td)
1084         return -EINVAL;
1085 
1086     /* Allocate a temporary buffer for one eraseblock incl. oob */
1087     len = (1 << this->bbt_erase_shift);
1088     len += (len >> this->page_shift) * mtd->oobsize;
1089     buf = kmalloc(len, GFP_KERNEL);
1090     if (!buf)
1091         return -ENOMEM;
1092 
1093     /* Do we have a bbt per chip? */
1094     if (td->options & NAND_BBT_PERCHIP) {
1095         chip = (int)(offs >> this->chip_shift);
1096         chipsel = chip;
1097     } else {
1098         chip = 0;
1099         chipsel = -1;
1100     }
1101 
1102     td->version[chip]++;
1103     if (md)
1104         md->version[chip]++;
1105 
1106     /* Write the bad block table to the device? */
1107     if (td->options & NAND_BBT_WRITE) {
1108         res = write_bbt(this, buf, td, md, chipsel);
1109         if (res < 0)
1110             goto out;
1111     }
1112     /* Write the mirror bad block table to the device? */
1113     if (md && (md->options & NAND_BBT_WRITE)) {
1114         res = write_bbt(this, buf, md, td, chipsel);
1115     }
1116 
1117  out:
1118     kfree(buf);
1119     return res;
1120 }
1121 
1122 /**
1123  * mark_bbt_region - [GENERIC] mark the bad block table regions
1124  * @this: the NAND device
1125  * @td: bad block table descriptor
1126  *
1127  * The bad block table regions are marked as "bad" to prevent accidental
1128  * erasures / writes. The regions are identified by the mark 0x02.
1129  */
1130 static void mark_bbt_region(struct nand_chip *this, struct nand_bbt_descr *td)
1131 {
1132     u64 targetsize = nanddev_target_size(&this->base);
1133     struct mtd_info *mtd = nand_to_mtd(this);
1134     int i, j, chips, block, nrblocks, update;
1135     uint8_t oldval;
1136 
1137     /* Do we have a bbt per chip? */
1138     if (td->options & NAND_BBT_PERCHIP) {
1139         chips = nanddev_ntargets(&this->base);
1140         nrblocks = (int)(targetsize >> this->bbt_erase_shift);
1141     } else {
1142         chips = 1;
1143         nrblocks = (int)(mtd->size >> this->bbt_erase_shift);
1144     }
1145 
1146     for (i = 0; i < chips; i++) {
1147         if ((td->options & NAND_BBT_ABSPAGE) ||
1148             !(td->options & NAND_BBT_WRITE)) {
1149             if (td->pages[i] == -1)
1150                 continue;
1151             block = td->pages[i] >> (this->bbt_erase_shift - this->page_shift);
1152             oldval = bbt_get_entry(this, block);
1153             bbt_mark_entry(this, block, BBT_BLOCK_RESERVED);
1154             if ((oldval != BBT_BLOCK_RESERVED) &&
1155                     td->reserved_block_code)
1156                 nand_update_bbt(this, (loff_t)block <<
1157                         this->bbt_erase_shift);
1158             continue;
1159         }
1160         update = 0;
1161         if (td->options & NAND_BBT_LASTBLOCK)
1162             block = ((i + 1) * nrblocks) - td->maxblocks;
1163         else
1164             block = i * nrblocks;
1165         for (j = 0; j < td->maxblocks; j++) {
1166             oldval = bbt_get_entry(this, block);
1167             bbt_mark_entry(this, block, BBT_BLOCK_RESERVED);
1168             if (oldval != BBT_BLOCK_RESERVED)
1169                 update = 1;
1170             block++;
1171         }
1172         /*
1173          * If we want reserved blocks to be recorded to flash, and some
1174          * new ones have been marked, then we need to update the stored
1175          * bbts.  This should only happen once.
1176          */
1177         if (update && td->reserved_block_code)
1178             nand_update_bbt(this, (loff_t)(block - 1) <<
1179                     this->bbt_erase_shift);
1180     }
1181 }
1182 
1183 /**
1184  * verify_bbt_descr - verify the bad block description
1185  * @this: the NAND device
1186  * @bd: the table to verify
1187  *
1188  * This functions performs a few sanity checks on the bad block description
1189  * table.
1190  */
1191 static void verify_bbt_descr(struct nand_chip *this, struct nand_bbt_descr *bd)
1192 {
1193     u64 targetsize = nanddev_target_size(&this->base);
1194     struct mtd_info *mtd = nand_to_mtd(this);
1195     u32 pattern_len;
1196     u32 bits;
1197     u32 table_size;
1198 
1199     if (!bd)
1200         return;
1201 
1202     pattern_len = bd->len;
1203     bits = bd->options & NAND_BBT_NRBITS_MSK;
1204 
1205     BUG_ON((this->bbt_options & NAND_BBT_NO_OOB) &&
1206             !(this->bbt_options & NAND_BBT_USE_FLASH));
1207     BUG_ON(!bits);
1208 
1209     if (bd->options & NAND_BBT_VERSION)
1210         pattern_len++;
1211 
1212     if (bd->options & NAND_BBT_NO_OOB) {
1213         BUG_ON(!(this->bbt_options & NAND_BBT_USE_FLASH));
1214         BUG_ON(!(this->bbt_options & NAND_BBT_NO_OOB));
1215         BUG_ON(bd->offs);
1216         if (bd->options & NAND_BBT_VERSION)
1217             BUG_ON(bd->veroffs != bd->len);
1218         BUG_ON(bd->options & NAND_BBT_SAVECONTENT);
1219     }
1220 
1221     if (bd->options & NAND_BBT_PERCHIP)
1222         table_size = targetsize >> this->bbt_erase_shift;
1223     else
1224         table_size = mtd->size >> this->bbt_erase_shift;
1225     table_size >>= 3;
1226     table_size *= bits;
1227     if (bd->options & NAND_BBT_NO_OOB)
1228         table_size += pattern_len;
1229     BUG_ON(table_size > (1 << this->bbt_erase_shift));
1230 }
1231 
1232 /**
1233  * nand_scan_bbt - [NAND Interface] scan, find, read and maybe create bad block table(s)
1234  * @this: the NAND device
1235  * @bd: descriptor for the good/bad block search pattern
1236  *
1237  * The function checks, if a bad block table(s) is/are already available. If
1238  * not it scans the device for manufacturer marked good / bad blocks and writes
1239  * the bad block table(s) to the selected place.
1240  *
1241  * The bad block table memory is allocated here. It must be freed by calling
1242  * the nand_free_bbt function.
1243  */
1244 static int nand_scan_bbt(struct nand_chip *this, struct nand_bbt_descr *bd)
1245 {
1246     struct mtd_info *mtd = nand_to_mtd(this);
1247     int len, res;
1248     uint8_t *buf;
1249     struct nand_bbt_descr *td = this->bbt_td;
1250     struct nand_bbt_descr *md = this->bbt_md;
1251 
1252     len = (mtd->size >> (this->bbt_erase_shift + 2)) ? : 1;
1253     /*
1254      * Allocate memory (2bit per block) and clear the memory bad block
1255      * table.
1256      */
1257     this->bbt = kzalloc(len, GFP_KERNEL);
1258     if (!this->bbt)
1259         return -ENOMEM;
1260 
1261     /*
1262      * If no primary table descriptor is given, scan the device to build a
1263      * memory based bad block table.
1264      */
1265     if (!td) {
1266         if ((res = nand_memory_bbt(this, bd))) {
1267             pr_err("nand_bbt: can't scan flash and build the RAM-based BBT\n");
1268             goto err_free_bbt;
1269         }
1270         return 0;
1271     }
1272     verify_bbt_descr(this, td);
1273     verify_bbt_descr(this, md);
1274 
1275     /* Allocate a temporary buffer for one eraseblock incl. oob */
1276     len = (1 << this->bbt_erase_shift);
1277     len += (len >> this->page_shift) * mtd->oobsize;
1278     buf = vmalloc(len);
1279     if (!buf) {
1280         res = -ENOMEM;
1281         goto err_free_bbt;
1282     }
1283 
1284     /* Is the bbt at a given page? */
1285     if (td->options & NAND_BBT_ABSPAGE) {
1286         read_abs_bbts(this, buf, td, md);
1287     } else {
1288         /* Search the bad block table using a pattern in oob */
1289         search_read_bbts(this, buf, td, md);
1290     }
1291 
1292     res = check_create(this, buf, bd);
1293     if (res)
1294         goto err_free_buf;
1295 
1296     /* Prevent the bbt regions from erasing / writing */
1297     mark_bbt_region(this, td);
1298     if (md)
1299         mark_bbt_region(this, md);
1300 
1301     vfree(buf);
1302     return 0;
1303 
1304 err_free_buf:
1305     vfree(buf);
1306 err_free_bbt:
1307     kfree(this->bbt);
1308     this->bbt = NULL;
1309     return res;
1310 }
1311 
1312 /*
1313  * Define some generic bad / good block scan pattern which are used
1314  * while scanning a device for factory marked good / bad blocks.
1315  */
1316 static uint8_t scan_ff_pattern[] = { 0xff, 0xff };
1317 
1318 /* Generic flash bbt descriptors */
1319 static uint8_t bbt_pattern[] = {'B', 'b', 't', '0' };
1320 static uint8_t mirror_pattern[] = {'1', 't', 'b', 'B' };
1321 
1322 static struct nand_bbt_descr bbt_main_descr = {
1323     .options = NAND_BBT_LASTBLOCK | NAND_BBT_CREATE | NAND_BBT_WRITE
1324         | NAND_BBT_2BIT | NAND_BBT_VERSION | NAND_BBT_PERCHIP,
1325     .offs = 8,
1326     .len = 4,
1327     .veroffs = 12,
1328     .maxblocks = NAND_BBT_SCAN_MAXBLOCKS,
1329     .pattern = bbt_pattern
1330 };
1331 
1332 static struct nand_bbt_descr bbt_mirror_descr = {
1333     .options = NAND_BBT_LASTBLOCK | NAND_BBT_CREATE | NAND_BBT_WRITE
1334         | NAND_BBT_2BIT | NAND_BBT_VERSION | NAND_BBT_PERCHIP,
1335     .offs = 8,
1336     .len = 4,
1337     .veroffs = 12,
1338     .maxblocks = NAND_BBT_SCAN_MAXBLOCKS,
1339     .pattern = mirror_pattern
1340 };
1341 
1342 static struct nand_bbt_descr bbt_main_no_oob_descr = {
1343     .options = NAND_BBT_LASTBLOCK | NAND_BBT_CREATE | NAND_BBT_WRITE
1344         | NAND_BBT_2BIT | NAND_BBT_VERSION | NAND_BBT_PERCHIP
1345         | NAND_BBT_NO_OOB,
1346     .len = 4,
1347     .veroffs = 4,
1348     .maxblocks = NAND_BBT_SCAN_MAXBLOCKS,
1349     .pattern = bbt_pattern
1350 };
1351 
1352 static struct nand_bbt_descr bbt_mirror_no_oob_descr = {
1353     .options = NAND_BBT_LASTBLOCK | NAND_BBT_CREATE | NAND_BBT_WRITE
1354         | NAND_BBT_2BIT | NAND_BBT_VERSION | NAND_BBT_PERCHIP
1355         | NAND_BBT_NO_OOB,
1356     .len = 4,
1357     .veroffs = 4,
1358     .maxblocks = NAND_BBT_SCAN_MAXBLOCKS,
1359     .pattern = mirror_pattern
1360 };
1361 
1362 #define BADBLOCK_SCAN_MASK (~NAND_BBT_NO_OOB)
1363 /**
1364  * nand_create_badblock_pattern - [INTERN] Creates a BBT descriptor structure
1365  * @this: NAND chip to create descriptor for
1366  *
1367  * This function allocates and initializes a nand_bbt_descr for BBM detection
1368  * based on the properties of @this. The new descriptor is stored in
1369  * this->badblock_pattern. Thus, this->badblock_pattern should be NULL when
1370  * passed to this function.
1371  */
1372 static int nand_create_badblock_pattern(struct nand_chip *this)
1373 {
1374     struct nand_bbt_descr *bd;
1375     if (this->badblock_pattern) {
1376         pr_warn("Bad block pattern already allocated; not replacing\n");
1377         return -EINVAL;
1378     }
1379     bd = kzalloc(sizeof(*bd), GFP_KERNEL);
1380     if (!bd)
1381         return -ENOMEM;
1382     bd->options = this->bbt_options & BADBLOCK_SCAN_MASK;
1383     bd->offs = this->badblockpos;
1384     bd->len = (this->options & NAND_BUSWIDTH_16) ? 2 : 1;
1385     bd->pattern = scan_ff_pattern;
1386     bd->options |= NAND_BBT_DYNAMICSTRUCT;
1387     this->badblock_pattern = bd;
1388     return 0;
1389 }
1390 
1391 /**
1392  * nand_create_bbt - [NAND Interface] Select a default bad block table for the device
1393  * @this: NAND chip object
1394  *
1395  * This function selects the default bad block table support for the device and
1396  * calls the nand_scan_bbt function.
1397  */
1398 int nand_create_bbt(struct nand_chip *this)
1399 {
1400     int ret;
1401 
1402     /* Is a flash based bad block table requested? */
1403     if (this->bbt_options & NAND_BBT_USE_FLASH) {
1404         /* Use the default pattern descriptors */
1405         if (!this->bbt_td) {
1406             if (this->bbt_options & NAND_BBT_NO_OOB) {
1407                 this->bbt_td = &bbt_main_no_oob_descr;
1408                 this->bbt_md = &bbt_mirror_no_oob_descr;
1409             } else {
1410                 this->bbt_td = &bbt_main_descr;
1411                 this->bbt_md = &bbt_mirror_descr;
1412             }
1413         }
1414     } else {
1415         this->bbt_td = NULL;
1416         this->bbt_md = NULL;
1417     }
1418 
1419     if (!this->badblock_pattern) {
1420         ret = nand_create_badblock_pattern(this);
1421         if (ret)
1422             return ret;
1423     }
1424 
1425     return nand_scan_bbt(this, this->badblock_pattern);
1426 }
1427 EXPORT_SYMBOL(nand_create_bbt);
1428 
1429 /**
1430  * nand_isreserved_bbt - [NAND Interface] Check if a block is reserved
1431  * @this: NAND chip object
1432  * @offs: offset in the device
1433  */
1434 int nand_isreserved_bbt(struct nand_chip *this, loff_t offs)
1435 {
1436     int block;
1437 
1438     block = (int)(offs >> this->bbt_erase_shift);
1439     return bbt_get_entry(this, block) == BBT_BLOCK_RESERVED;
1440 }
1441 
1442 /**
1443  * nand_isbad_bbt - [NAND Interface] Check if a block is bad
1444  * @this: NAND chip object
1445  * @offs: offset in the device
1446  * @allowbbt: allow access to bad block table region
1447  */
1448 int nand_isbad_bbt(struct nand_chip *this, loff_t offs, int allowbbt)
1449 {
1450     int block, res;
1451 
1452     block = (int)(offs >> this->bbt_erase_shift);
1453     res = bbt_get_entry(this, block);
1454 
1455     pr_debug("nand_isbad_bbt(): bbt info for offs 0x%08x: (block %d) 0x%02x\n",
1456          (unsigned int)offs, block, res);
1457 
1458     if (mtd_check_expert_analysis_mode())
1459         return 0;
1460 
1461     switch (res) {
1462     case BBT_BLOCK_GOOD:
1463         return 0;
1464     case BBT_BLOCK_WORN:
1465         return 1;
1466     case BBT_BLOCK_RESERVED:
1467         return allowbbt ? 0 : 1;
1468     }
1469     return 1;
1470 }
1471 
1472 /**
1473  * nand_markbad_bbt - [NAND Interface] Mark a block bad in the BBT
1474  * @this: NAND chip object
1475  * @offs: offset of the bad block
1476  */
1477 int nand_markbad_bbt(struct nand_chip *this, loff_t offs)
1478 {
1479     int block, ret = 0;
1480 
1481     block = (int)(offs >> this->bbt_erase_shift);
1482 
1483     /* Mark bad block in memory */
1484     bbt_mark_entry(this, block, BBT_BLOCK_WORN);
1485 
1486     /* Update flash-based bad block table */
1487     if (this->bbt_options & NAND_BBT_USE_FLASH)
1488         ret = nand_update_bbt(this, offs);
1489 
1490     return ret;
1491 }