This source file includes following definitions.
- bbt_get_entry
- bbt_mark_entry
- check_pattern_no_oob
- check_pattern
- check_short_pattern
- add_marker_len
- read_bbt
- read_abs_bbt
- scan_read_data
- scan_read_oob
- scan_read
- scan_write_bbt
- bbt_get_ver_offs
- read_abs_bbts
- scan_block_fast
- create_bbt
- search_bbt
- search_read_bbts
- get_bbt_block
- mark_bbt_block_bad
- write_bbt
- nand_memory_bbt
- check_create
- nand_update_bbt
- mark_bbt_region
- verify_bbt_descr
- nand_scan_bbt
- nand_create_badblock_pattern
- nand_create_bbt
- nand_isreserved_bbt
- nand_isbad_bbt
- nand_markbad_bbt
   1 
   2 
   3 
   4 
   5 
   6 
   7 
   8 
   9 
  10 
  11 
  12 
  13 
  14 
  15 
  16 
  17 
  18 
  19 
  20 
  21 
  22 
  23 
  24 
  25 
  26 
  27 
  28 
  29 
  30 
  31 
  32 
  33 
  34 
  35 
  36 
  37 
  38 
  39 
  40 
  41 
  42 
  43 
  44 
  45 
  46 
  47 
  48 
  49 
  50 
  51 
  52 
  53 
  54 
  55 
  56 #include <linux/slab.h>
  57 #include <linux/types.h>
  58 #include <linux/mtd/mtd.h>
  59 #include <linux/mtd/bbm.h>
  60 #include <linux/bitops.h>
  61 #include <linux/delay.h>
  62 #include <linux/vmalloc.h>
  63 #include <linux/export.h>
  64 #include <linux/string.h>
  65 
  66 #include "internals.h"
  67 
  68 #define BBT_BLOCK_GOOD          0x00
  69 #define BBT_BLOCK_WORN          0x01
  70 #define BBT_BLOCK_RESERVED      0x02
  71 #define BBT_BLOCK_FACTORY_BAD   0x03
  72 
  73 #define BBT_ENTRY_MASK          0x03
  74 #define BBT_ENTRY_SHIFT         2
  75 
  76 static inline uint8_t bbt_get_entry(struct nand_chip *chip, int block)
  77 {
  78         uint8_t entry = chip->bbt[block >> BBT_ENTRY_SHIFT];
  79         entry >>= (block & BBT_ENTRY_MASK) * 2;
  80         return entry & BBT_ENTRY_MASK;
  81 }
  82 
  83 static inline void bbt_mark_entry(struct nand_chip *chip, int block,
  84                 uint8_t mark)
  85 {
  86         uint8_t msk = (mark & BBT_ENTRY_MASK) << ((block & BBT_ENTRY_MASK) * 2);
  87         chip->bbt[block >> BBT_ENTRY_SHIFT] |= msk;
  88 }
  89 
  90 static int check_pattern_no_oob(uint8_t *buf, struct nand_bbt_descr *td)
  91 {
  92         if (memcmp(buf, td->pattern, td->len))
  93                 return -1;
  94         return 0;
  95 }
  96 
  97 
  98 
  99 
 100 
 101 
 102 
 103 
 104 
 105 
 106 
 107 static int check_pattern(uint8_t *buf, int len, int paglen, struct nand_bbt_descr *td)
 108 {
 109         if (td->options & NAND_BBT_NO_OOB)
 110                 return check_pattern_no_oob(buf, td);
 111 
 112         
 113         if (memcmp(buf + paglen + td->offs, td->pattern, td->len))
 114                 return -1;
 115 
 116         return 0;
 117 }
 118 
 119 
 120 
 121 
 122 
 123 
 124 
 125 
 126 
 127 
 128 static int check_short_pattern(uint8_t *buf, struct nand_bbt_descr *td)
 129 {
 130         
 131         if (memcmp(buf + td->offs, td->pattern, td->len))
 132                 return -1;
 133         return 0;
 134 }
 135 
 136 
 137 
 138 
 139 
 140 
 141 
 142 static u32 add_marker_len(struct nand_bbt_descr *td)
 143 {
 144         u32 len;
 145 
 146         if (!(td->options & NAND_BBT_NO_OOB))
 147                 return 0;
 148 
 149         len = td->len;
 150         if (td->options & NAND_BBT_VERSION)
 151                 len++;
 152         return len;
 153 }
 154 
 155 
 156 
 157 
 158 
 159 
 160 
 161 
 162 
 163 
 164 
 165 
 166 static int read_bbt(struct nand_chip *this, uint8_t *buf, int page, int num,
 167                     struct nand_bbt_descr *td, int offs)
 168 {
 169         struct mtd_info *mtd = nand_to_mtd(this);
 170         int res, ret = 0, i, j, act = 0;
 171         size_t retlen, len, totlen;
 172         loff_t from;
 173         int bits = td->options & NAND_BBT_NRBITS_MSK;
 174         uint8_t msk = (uint8_t)((1 << bits) - 1);
 175         u32 marker_len;
 176         int reserved_block_code = td->reserved_block_code;
 177 
 178         totlen = (num * bits) >> 3;
 179         marker_len = add_marker_len(td);
 180         from = ((loff_t)page) << this->page_shift;
 181 
 182         while (totlen) {
 183                 len = min(totlen, (size_t)(1 << this->bbt_erase_shift));
 184                 if (marker_len) {
 185                         
 186 
 187 
 188 
 189                         len -= marker_len;
 190                         from += marker_len;
 191                         marker_len = 0;
 192                 }
 193                 res = mtd_read(mtd, from, len, &retlen, buf);
 194                 if (res < 0) {
 195                         if (mtd_is_eccerr(res)) {
 196                                 pr_info("nand_bbt: ECC error in BBT at 0x%012llx\n",
 197                                         from & ~mtd->writesize);
 198                                 return res;
 199                         } else if (mtd_is_bitflip(res)) {
 200                                 pr_info("nand_bbt: corrected error in BBT at 0x%012llx\n",
 201                                         from & ~mtd->writesize);
 202                                 ret = res;
 203                         } else {
 204                                 pr_info("nand_bbt: error reading BBT\n");
 205                                 return res;
 206                         }
 207                 }
 208 
 209                 
 210                 for (i = 0; i < len; i++) {
 211                         uint8_t dat = buf[i];
 212                         for (j = 0; j < 8; j += bits, act++) {
 213                                 uint8_t tmp = (dat >> j) & msk;
 214                                 if (tmp == msk)
 215                                         continue;
 216                                 if (reserved_block_code && (tmp == reserved_block_code)) {
 217                                         pr_info("nand_read_bbt: reserved block at 0x%012llx\n",
 218                                                  (loff_t)(offs + act) <<
 219                                                  this->bbt_erase_shift);
 220                                         bbt_mark_entry(this, offs + act,
 221                                                         BBT_BLOCK_RESERVED);
 222                                         mtd->ecc_stats.bbtblocks++;
 223                                         continue;
 224                                 }
 225                                 
 226 
 227 
 228 
 229                                 pr_info("nand_read_bbt: bad block at 0x%012llx\n",
 230                                          (loff_t)(offs + act) <<
 231                                          this->bbt_erase_shift);
 232                                 
 233                                 if (tmp == 0)
 234                                         bbt_mark_entry(this, offs + act,
 235                                                         BBT_BLOCK_FACTORY_BAD);
 236                                 else
 237                                         bbt_mark_entry(this, offs + act,
 238                                                         BBT_BLOCK_WORN);
 239                                 mtd->ecc_stats.badblocks++;
 240                         }
 241                 }
 242                 totlen -= len;
 243                 from += len;
 244         }
 245         return ret;
 246 }
 247 
 248 
 249 
 250 
 251 
 252 
 253 
 254 
 255 
 256 
 257 
 258 
 259 static int read_abs_bbt(struct nand_chip *this, uint8_t *buf,
 260                         struct nand_bbt_descr *td, int chip)
 261 {
 262         struct mtd_info *mtd = nand_to_mtd(this);
 263         u64 targetsize = nanddev_target_size(&this->base);
 264         int res = 0, i;
 265 
 266         if (td->options & NAND_BBT_PERCHIP) {
 267                 int offs = 0;
 268                 for (i = 0; i < nanddev_ntargets(&this->base); i++) {
 269                         if (chip == -1 || chip == i)
 270                                 res = read_bbt(this, buf, td->pages[i],
 271                                         targetsize >> this->bbt_erase_shift,
 272                                         td, offs);
 273                         if (res)
 274                                 return res;
 275                         offs += targetsize >> this->bbt_erase_shift;
 276                 }
 277         } else {
 278                 res = read_bbt(this, buf, td->pages[0],
 279                                 mtd->size >> this->bbt_erase_shift, td, 0);
 280                 if (res)
 281                         return res;
 282         }
 283         return 0;
 284 }
 285 
 286 
 287 static int scan_read_data(struct nand_chip *this, uint8_t *buf, loff_t offs,
 288                           struct nand_bbt_descr *td)
 289 {
 290         struct mtd_info *mtd = nand_to_mtd(this);
 291         size_t retlen;
 292         size_t len;
 293 
 294         len = td->len;
 295         if (td->options & NAND_BBT_VERSION)
 296                 len++;
 297 
 298         return mtd_read(mtd, offs, len, &retlen, buf);
 299 }
 300 
 301 
 302 
 303 
 304 
 305 
 306 
 307 
 308 
 309 
 310 
 311 
 312 static int scan_read_oob(struct nand_chip *this, uint8_t *buf, loff_t offs,
 313                          size_t len)
 314 {
 315         struct mtd_info *mtd = nand_to_mtd(this);
 316         struct mtd_oob_ops ops;
 317         int res, ret = 0;
 318 
 319         ops.mode = MTD_OPS_PLACE_OOB;
 320         ops.ooboffs = 0;
 321         ops.ooblen = mtd->oobsize;
 322 
 323         while (len > 0) {
 324                 ops.datbuf = buf;
 325                 ops.len = min(len, (size_t)mtd->writesize);
 326                 ops.oobbuf = buf + ops.len;
 327 
 328                 res = mtd_read_oob(mtd, offs, &ops);
 329                 if (res) {
 330                         if (!mtd_is_bitflip_or_eccerr(res))
 331                                 return res;
 332                         else if (mtd_is_eccerr(res) || !ret)
 333                                 ret = res;
 334                 }
 335 
 336                 buf += mtd->oobsize + mtd->writesize;
 337                 len -= mtd->writesize;
 338                 offs += mtd->writesize;
 339         }
 340         return ret;
 341 }
 342 
 343 static int scan_read(struct nand_chip *this, uint8_t *buf, loff_t offs,
 344                      size_t len, struct nand_bbt_descr *td)
 345 {
 346         if (td->options & NAND_BBT_NO_OOB)
 347                 return scan_read_data(this, buf, offs, td);
 348         else
 349                 return scan_read_oob(this, buf, offs, len);
 350 }
 351 
 352 
 353 static int scan_write_bbt(struct nand_chip *this, loff_t offs, size_t len,
 354                           uint8_t *buf, uint8_t *oob)
 355 {
 356         struct mtd_info *mtd = nand_to_mtd(this);
 357         struct mtd_oob_ops ops;
 358 
 359         ops.mode = MTD_OPS_PLACE_OOB;
 360         ops.ooboffs = 0;
 361         ops.ooblen = mtd->oobsize;
 362         ops.datbuf = buf;
 363         ops.oobbuf = oob;
 364         ops.len = len;
 365 
 366         return mtd_write_oob(mtd, offs, &ops);
 367 }
 368 
 369 static u32 bbt_get_ver_offs(struct nand_chip *this, struct nand_bbt_descr *td)
 370 {
 371         struct mtd_info *mtd = nand_to_mtd(this);
 372         u32 ver_offs = td->veroffs;
 373 
 374         if (!(td->options & NAND_BBT_NO_OOB))
 375                 ver_offs += mtd->writesize;
 376         return ver_offs;
 377 }
 378 
 379 
 380 
 381 
 382 
 383 
 384 
 385 
 386 
 387 
 388 
 389 static void read_abs_bbts(struct nand_chip *this, uint8_t *buf,
 390                           struct nand_bbt_descr *td, struct nand_bbt_descr *md)
 391 {
 392         struct mtd_info *mtd = nand_to_mtd(this);
 393 
 394         
 395         if (td->options & NAND_BBT_VERSION) {
 396                 scan_read(this, buf, (loff_t)td->pages[0] << this->page_shift,
 397                           mtd->writesize, td);
 398                 td->version[0] = buf[bbt_get_ver_offs(this, td)];
 399                 pr_info("Bad block table at page %d, version 0x%02X\n",
 400                          td->pages[0], td->version[0]);
 401         }
 402 
 403         
 404         if (md && (md->options & NAND_BBT_VERSION)) {
 405                 scan_read(this, buf, (loff_t)md->pages[0] << this->page_shift,
 406                           mtd->writesize, md);
 407                 md->version[0] = buf[bbt_get_ver_offs(this, md)];
 408                 pr_info("Bad block table at page %d, version 0x%02X\n",
 409                          md->pages[0], md->version[0]);
 410         }
 411 }
 412 
 413 
 414 static int scan_block_fast(struct nand_chip *this, struct nand_bbt_descr *bd,
 415                            loff_t offs, uint8_t *buf)
 416 {
 417         struct mtd_info *mtd = nand_to_mtd(this);
 418 
 419         struct mtd_oob_ops ops;
 420         int ret, page_offset;
 421 
 422         ops.ooblen = mtd->oobsize;
 423         ops.oobbuf = buf;
 424         ops.ooboffs = 0;
 425         ops.datbuf = NULL;
 426         ops.mode = MTD_OPS_PLACE_OOB;
 427 
 428         page_offset = nand_bbm_get_next_page(this, 0);
 429 
 430         while (page_offset >= 0) {
 431                 
 432 
 433 
 434 
 435                 ret = mtd_read_oob(mtd, offs + (page_offset * mtd->writesize),
 436                                    &ops);
 437                 
 438                 if (ret && !mtd_is_bitflip_or_eccerr(ret))
 439                         return ret;
 440 
 441                 if (check_short_pattern(buf, bd))
 442                         return 1;
 443 
 444                 page_offset = nand_bbm_get_next_page(this, page_offset + 1);
 445         }
 446 
 447         return 0;
 448 }
 449 
 450 
 451 
 452 
 453 
 454 
 455 
 456 
 457 
 458 
 459 
 460 
 461 static int create_bbt(struct nand_chip *this, uint8_t *buf,
 462                       struct nand_bbt_descr *bd, int chip)
 463 {
 464         u64 targetsize = nanddev_target_size(&this->base);
 465         struct mtd_info *mtd = nand_to_mtd(this);
 466         int i, numblocks, startblock;
 467         loff_t from;
 468 
 469         pr_info("Scanning device for bad blocks\n");
 470 
 471         if (chip == -1) {
 472                 numblocks = mtd->size >> this->bbt_erase_shift;
 473                 startblock = 0;
 474                 from = 0;
 475         } else {
 476                 if (chip >= nanddev_ntargets(&this->base)) {
 477                         pr_warn("create_bbt(): chipnr (%d) > available chips (%d)\n",
 478                                 chip + 1, nanddev_ntargets(&this->base));
 479                         return -EINVAL;
 480                 }
 481                 numblocks = targetsize >> this->bbt_erase_shift;
 482                 startblock = chip * numblocks;
 483                 numblocks += startblock;
 484                 from = (loff_t)startblock << this->bbt_erase_shift;
 485         }
 486 
 487         for (i = startblock; i < numblocks; i++) {
 488                 int ret;
 489 
 490                 BUG_ON(bd->options & NAND_BBT_NO_OOB);
 491 
 492                 ret = scan_block_fast(this, bd, from, buf);
 493                 if (ret < 0)
 494                         return ret;
 495 
 496                 if (ret) {
 497                         bbt_mark_entry(this, i, BBT_BLOCK_FACTORY_BAD);
 498                         pr_warn("Bad eraseblock %d at 0x%012llx\n",
 499                                 i, (unsigned long long)from);
 500                         mtd->ecc_stats.badblocks++;
 501                 }
 502 
 503                 from += (1 << this->bbt_erase_shift);
 504         }
 505         return 0;
 506 }
 507 
 508 
 509 
 510 
 511 
 512 
 513 
 514 
 515 
 516 
 517 
 518 
 519 
 520 
 521 
 522 
 523 static int search_bbt(struct nand_chip *this, uint8_t *buf,
 524                       struct nand_bbt_descr *td)
 525 {
 526         u64 targetsize = nanddev_target_size(&this->base);
 527         struct mtd_info *mtd = nand_to_mtd(this);
 528         int i, chips;
 529         int startblock, block, dir;
 530         int scanlen = mtd->writesize + mtd->oobsize;
 531         int bbtblocks;
 532         int blocktopage = this->bbt_erase_shift - this->page_shift;
 533 
 534         
 535         if (td->options & NAND_BBT_LASTBLOCK) {
 536                 startblock = (mtd->size >> this->bbt_erase_shift) - 1;
 537                 dir = -1;
 538         } else {
 539                 startblock = 0;
 540                 dir = 1;
 541         }
 542 
 543         
 544         if (td->options & NAND_BBT_PERCHIP) {
 545                 chips = nanddev_ntargets(&this->base);
 546                 bbtblocks = targetsize >> this->bbt_erase_shift;
 547                 startblock &= bbtblocks - 1;
 548         } else {
 549                 chips = 1;
 550                 bbtblocks = mtd->size >> this->bbt_erase_shift;
 551         }
 552 
 553         for (i = 0; i < chips; i++) {
 554                 
 555                 td->version[i] = 0;
 556                 td->pages[i] = -1;
 557                 
 558                 for (block = 0; block < td->maxblocks; block++) {
 559 
 560                         int actblock = startblock + dir * block;
 561                         loff_t offs = (loff_t)actblock << this->bbt_erase_shift;
 562 
 563                         
 564                         scan_read(this, buf, offs, mtd->writesize, td);
 565                         if (!check_pattern(buf, scanlen, mtd->writesize, td)) {
 566                                 td->pages[i] = actblock << blocktopage;
 567                                 if (td->options & NAND_BBT_VERSION) {
 568                                         offs = bbt_get_ver_offs(this, td);
 569                                         td->version[i] = buf[offs];
 570                                 }
 571                                 break;
 572                         }
 573                 }
 574                 startblock += targetsize >> this->bbt_erase_shift;
 575         }
 576         
 577         for (i = 0; i < chips; i++) {
 578                 if (td->pages[i] == -1)
 579                         pr_warn("Bad block table not found for chip %d\n", i);
 580                 else
 581                         pr_info("Bad block table found at page %d, version 0x%02X\n",
 582                                 td->pages[i], td->version[i]);
 583         }
 584         return 0;
 585 }
 586 
 587 
 588 
 589 
 590 
 591 
 592 
 593 
 594 
 595 
 596 static void search_read_bbts(struct nand_chip *this, uint8_t *buf,
 597                              struct nand_bbt_descr *td,
 598                              struct nand_bbt_descr *md)
 599 {
 600         
 601         search_bbt(this, buf, td);
 602 
 603         
 604         if (md)
 605                 search_bbt(this, buf, md);
 606 }
 607 
 608 
 609 
 610 
 611 
 612 
 613 
 614 
 615 
 616 
 617 
 618 
 619 
 620 
 621 static int get_bbt_block(struct nand_chip *this, struct nand_bbt_descr *td,
 622                          struct nand_bbt_descr *md, int chip)
 623 {
 624         u64 targetsize = nanddev_target_size(&this->base);
 625         int startblock, dir, page, numblocks, i;
 626 
 627         
 628 
 629 
 630 
 631 
 632         if (td->pages[chip] != -1)
 633                 return td->pages[chip] >>
 634                                 (this->bbt_erase_shift - this->page_shift);
 635 
 636         numblocks = (int)(targetsize >> this->bbt_erase_shift);
 637         if (!(td->options & NAND_BBT_PERCHIP))
 638                 numblocks *= nanddev_ntargets(&this->base);
 639 
 640         
 641 
 642 
 643 
 644         if (td->options & NAND_BBT_LASTBLOCK) {
 645                 startblock = numblocks * (chip + 1) - 1;
 646                 dir = -1;
 647         } else {
 648                 startblock = chip * numblocks;
 649                 dir = 1;
 650         }
 651 
 652         for (i = 0; i < td->maxblocks; i++) {
 653                 int block = startblock + dir * i;
 654 
 655                 
 656                 switch (bbt_get_entry(this, block)) {
 657                 case BBT_BLOCK_WORN:
 658                 case BBT_BLOCK_FACTORY_BAD:
 659                         continue;
 660                 }
 661 
 662                 page = block << (this->bbt_erase_shift - this->page_shift);
 663 
 664                 
 665                 if (!md || md->pages[chip] != page)
 666                         return block;
 667         }
 668 
 669         return -ENOSPC;
 670 }
 671 
 672 
 673 
 674 
 675 
 676 
 677 
 678 
 679 
 680 
 681 
 682 
 683 
 684 static void mark_bbt_block_bad(struct nand_chip *this,
 685                                struct nand_bbt_descr *td,
 686                                int chip, int block)
 687 {
 688         loff_t to;
 689         int res;
 690 
 691         bbt_mark_entry(this, block, BBT_BLOCK_WORN);
 692 
 693         to = (loff_t)block << this->bbt_erase_shift;
 694         res = nand_markbad_bbm(this, to);
 695         if (res)
 696                 pr_warn("nand_bbt: error %d while marking block %d bad\n",
 697                         res, block);
 698 
 699         td->pages[chip] = -1;
 700 }
 701 
 702 
 703 
 704 
 705 
 706 
 707 
 708 
 709 
 710 
 711 
 712 static int write_bbt(struct nand_chip *this, uint8_t *buf,
 713                      struct nand_bbt_descr *td, struct nand_bbt_descr *md,
 714                      int chipsel)
 715 {
 716         u64 targetsize = nanddev_target_size(&this->base);
 717         struct mtd_info *mtd = nand_to_mtd(this);
 718         struct erase_info einfo;
 719         int i, res, chip = 0;
 720         int bits, page, offs, numblocks, sft, sftmsk;
 721         int nrchips, pageoffs, ooboffs;
 722         uint8_t msk[4];
 723         uint8_t rcode = td->reserved_block_code;
 724         size_t retlen, len = 0;
 725         loff_t to;
 726         struct mtd_oob_ops ops;
 727 
 728         ops.ooblen = mtd->oobsize;
 729         ops.ooboffs = 0;
 730         ops.datbuf = NULL;
 731         ops.mode = MTD_OPS_PLACE_OOB;
 732 
 733         if (!rcode)
 734                 rcode = 0xff;
 735         
 736         if (td->options & NAND_BBT_PERCHIP) {
 737                 numblocks = (int)(targetsize >> this->bbt_erase_shift);
 738                 
 739                 if (chipsel == -1) {
 740                         nrchips = nanddev_ntargets(&this->base);
 741                 } else {
 742                         nrchips = chipsel + 1;
 743                         chip = chipsel;
 744                 }
 745         } else {
 746                 numblocks = (int)(mtd->size >> this->bbt_erase_shift);
 747                 nrchips = 1;
 748         }
 749 
 750         
 751         while (chip < nrchips) {
 752                 int block;
 753 
 754                 block = get_bbt_block(this, td, md, chip);
 755                 if (block < 0) {
 756                         pr_err("No space left to write bad block table\n");
 757                         res = block;
 758                         goto outerr;
 759                 }
 760 
 761                 
 762 
 763 
 764 
 765                 page = block << (this->bbt_erase_shift - this->page_shift);
 766 
 767                 
 768                 bits = td->options & NAND_BBT_NRBITS_MSK;
 769                 msk[2] = ~rcode;
 770                 switch (bits) {
 771                 case 1: sft = 3; sftmsk = 0x07; msk[0] = 0x00; msk[1] = 0x01;
 772                         msk[3] = 0x01;
 773                         break;
 774                 case 2: sft = 2; sftmsk = 0x06; msk[0] = 0x00; msk[1] = 0x01;
 775                         msk[3] = 0x03;
 776                         break;
 777                 case 4: sft = 1; sftmsk = 0x04; msk[0] = 0x00; msk[1] = 0x0C;
 778                         msk[3] = 0x0f;
 779                         break;
 780                 case 8: sft = 0; sftmsk = 0x00; msk[0] = 0x00; msk[1] = 0x0F;
 781                         msk[3] = 0xff;
 782                         break;
 783                 default: return -EINVAL;
 784                 }
 785 
 786                 to = ((loff_t)page) << this->page_shift;
 787 
 788                 
 789                 if (td->options & NAND_BBT_SAVECONTENT) {
 790                         
 791                         to &= ~(((loff_t)1 << this->bbt_erase_shift) - 1);
 792                         len = 1 << this->bbt_erase_shift;
 793                         res = mtd_read(mtd, to, len, &retlen, buf);
 794                         if (res < 0) {
 795                                 if (retlen != len) {
 796                                         pr_info("nand_bbt: error reading block for writing the bad block table\n");
 797                                         return res;
 798                                 }
 799                                 pr_warn("nand_bbt: ECC error while reading block for writing bad block table\n");
 800                         }
 801                         
 802                         ops.ooblen = (len >> this->page_shift) * mtd->oobsize;
 803                         ops.oobbuf = &buf[len];
 804                         res = mtd_read_oob(mtd, to + mtd->writesize, &ops);
 805                         if (res < 0 || ops.oobretlen != ops.ooblen)
 806                                 goto outerr;
 807 
 808                         
 809                         pageoffs = page - (int)(to >> this->page_shift);
 810                         offs = pageoffs << this->page_shift;
 811                         
 812                         memset(&buf[offs], 0xff, (size_t)(numblocks >> sft));
 813                         ooboffs = len + (pageoffs * mtd->oobsize);
 814 
 815                 } else if (td->options & NAND_BBT_NO_OOB) {
 816                         ooboffs = 0;
 817                         offs = td->len;
 818                         
 819                         if (td->options & NAND_BBT_VERSION)
 820                                 offs++;
 821                         
 822                         len = (size_t)(numblocks >> sft);
 823                         len += offs;
 824                         
 825                         len = ALIGN(len, mtd->writesize);
 826                         
 827                         memset(buf, 0xff, len);
 828                         
 829                         memcpy(buf, td->pattern, td->len);
 830                 } else {
 831                         
 832                         len = (size_t)(numblocks >> sft);
 833                         
 834                         len = ALIGN(len, mtd->writesize);
 835                         
 836                         memset(buf, 0xff, len +
 837                                (len >> this->page_shift)* mtd->oobsize);
 838                         offs = 0;
 839                         ooboffs = len;
 840                         
 841                         memcpy(&buf[ooboffs + td->offs], td->pattern, td->len);
 842                 }
 843 
 844                 if (td->options & NAND_BBT_VERSION)
 845                         buf[ooboffs + td->veroffs] = td->version[chip];
 846 
 847                 
 848                 for (i = 0; i < numblocks; i++) {
 849                         uint8_t dat;
 850                         int sftcnt = (i << (3 - sft)) & sftmsk;
 851                         dat = bbt_get_entry(this, chip * numblocks + i);
 852                         
 853                         buf[offs + (i >> sft)] &= ~(msk[dat] << sftcnt);
 854                 }
 855 
 856                 memset(&einfo, 0, sizeof(einfo));
 857                 einfo.addr = to;
 858                 einfo.len = 1 << this->bbt_erase_shift;
 859                 res = nand_erase_nand(this, &einfo, 1);
 860                 if (res < 0) {
 861                         pr_warn("nand_bbt: error while erasing BBT block %d\n",
 862                                 res);
 863                         mark_bbt_block_bad(this, td, chip, block);
 864                         continue;
 865                 }
 866 
 867                 res = scan_write_bbt(this, to, len, buf,
 868                                      td->options & NAND_BBT_NO_OOB ?
 869                                      NULL : &buf[len]);
 870                 if (res < 0) {
 871                         pr_warn("nand_bbt: error while writing BBT block %d\n",
 872                                 res);
 873                         mark_bbt_block_bad(this, td, chip, block);
 874                         continue;
 875                 }
 876 
 877                 pr_info("Bad block table written to 0x%012llx, version 0x%02X\n",
 878                          (unsigned long long)to, td->version[chip]);
 879 
 880                 
 881                 td->pages[chip++] = page;
 882         }
 883         return 0;
 884 
 885  outerr:
 886         pr_warn("nand_bbt: error while writing bad block table %d\n", res);
 887         return res;
 888 }
 889 
 890 
 891 
 892 
 893 
 894 
 895 
 896 
 897 
 898 static inline int nand_memory_bbt(struct nand_chip *this,
 899                                   struct nand_bbt_descr *bd)
 900 {
 901         u8 *pagebuf = nand_get_data_buf(this);
 902 
 903         return create_bbt(this, pagebuf, bd, -1);
 904 }
 905 
 906 
 907 
 908 
 909 
 910 
 911 
 912 
 913 
 914 
 915 
 916 
 917 static int check_create(struct nand_chip *this, uint8_t *buf,
 918                         struct nand_bbt_descr *bd)
 919 {
 920         int i, chips, writeops, create, chipsel, res, res2;
 921         struct nand_bbt_descr *td = this->bbt_td;
 922         struct nand_bbt_descr *md = this->bbt_md;
 923         struct nand_bbt_descr *rd, *rd2;
 924 
 925         
 926         if (td->options & NAND_BBT_PERCHIP)
 927                 chips = nanddev_ntargets(&this->base);
 928         else
 929                 chips = 1;
 930 
 931         for (i = 0; i < chips; i++) {
 932                 writeops = 0;
 933                 create = 0;
 934                 rd = NULL;
 935                 rd2 = NULL;
 936                 res = res2 = 0;
 937                 
 938                 chipsel = (td->options & NAND_BBT_PERCHIP) ? i : -1;
 939                 
 940                 if (md) {
 941                         if (td->pages[i] == -1 && md->pages[i] == -1) {
 942                                 create = 1;
 943                                 writeops = 0x03;
 944                         } else if (td->pages[i] == -1) {
 945                                 rd = md;
 946                                 writeops = 0x01;
 947                         } else if (md->pages[i] == -1) {
 948                                 rd = td;
 949                                 writeops = 0x02;
 950                         } else if (td->version[i] == md->version[i]) {
 951                                 rd = td;
 952                                 if (!(td->options & NAND_BBT_VERSION))
 953                                         rd2 = md;
 954                         } else if (((int8_t)(td->version[i] - md->version[i])) > 0) {
 955                                 rd = td;
 956                                 writeops = 0x02;
 957                         } else {
 958                                 rd = md;
 959                                 writeops = 0x01;
 960                         }
 961                 } else {
 962                         if (td->pages[i] == -1) {
 963                                 create = 1;
 964                                 writeops = 0x01;
 965                         } else {
 966                                 rd = td;
 967                         }
 968                 }
 969 
 970                 if (create) {
 971                         
 972                         if (!(td->options & NAND_BBT_CREATE))
 973                                 continue;
 974 
 975                         
 976                         if (!(this->bbt_options & NAND_BBT_CREATE_EMPTY))
 977                                 create_bbt(this, buf, bd, chipsel);
 978 
 979                         td->version[i] = 1;
 980                         if (md)
 981                                 md->version[i] = 1;
 982                 }
 983 
 984                 
 985                 if (rd) {
 986                         res = read_abs_bbt(this, buf, rd, chipsel);
 987                         if (mtd_is_eccerr(res)) {
 988                                 
 989                                 rd->pages[i] = -1;
 990                                 rd->version[i] = 0;
 991                                 i--;
 992                                 continue;
 993                         }
 994                 }
 995                 
 996                 if (rd2) {
 997                         res2 = read_abs_bbt(this, buf, rd2, chipsel);
 998                         if (mtd_is_eccerr(res2)) {
 999                                 
1000                                 rd2->pages[i] = -1;
1001                                 rd2->version[i] = 0;
1002                                 i--;
1003                                 continue;
1004                         }
1005                 }
1006 
1007                 
1008                 if (mtd_is_bitflip(res) || mtd_is_bitflip(res2))
1009                         writeops = 0x03;
1010 
1011                 
1012                 if (md) {
1013                         td->version[i] = max(td->version[i], md->version[i]);
1014                         md->version[i] = td->version[i];
1015                 }
1016 
1017                 
1018                 if ((writeops & 0x01) && (td->options & NAND_BBT_WRITE)) {
1019                         res = write_bbt(this, buf, td, md, chipsel);
1020                         if (res < 0)
1021                                 return res;
1022                 }
1023 
1024                 
1025                 if ((writeops & 0x02) && md && (md->options & NAND_BBT_WRITE)) {
1026                         res = write_bbt(this, buf, md, td, chipsel);
1027                         if (res < 0)
1028                                 return res;
1029                 }
1030         }
1031         return 0;
1032 }
1033 
1034 
1035 
1036 
1037 
1038 
1039 
1040 
1041 static int nand_update_bbt(struct nand_chip *this, loff_t offs)
1042 {
1043         struct mtd_info *mtd = nand_to_mtd(this);
1044         int len, res = 0;
1045         int chip, chipsel;
1046         uint8_t *buf;
1047         struct nand_bbt_descr *td = this->bbt_td;
1048         struct nand_bbt_descr *md = this->bbt_md;
1049 
1050         if (!this->bbt || !td)
1051                 return -EINVAL;
1052 
1053         
1054         len = (1 << this->bbt_erase_shift);
1055         len += (len >> this->page_shift) * mtd->oobsize;
1056         buf = kmalloc(len, GFP_KERNEL);
1057         if (!buf)
1058                 return -ENOMEM;
1059 
1060         
1061         if (td->options & NAND_BBT_PERCHIP) {
1062                 chip = (int)(offs >> this->chip_shift);
1063                 chipsel = chip;
1064         } else {
1065                 chip = 0;
1066                 chipsel = -1;
1067         }
1068 
1069         td->version[chip]++;
1070         if (md)
1071                 md->version[chip]++;
1072 
1073         
1074         if (td->options & NAND_BBT_WRITE) {
1075                 res = write_bbt(this, buf, td, md, chipsel);
1076                 if (res < 0)
1077                         goto out;
1078         }
1079         
1080         if (md && (md->options & NAND_BBT_WRITE)) {
1081                 res = write_bbt(this, buf, md, td, chipsel);
1082         }
1083 
1084  out:
1085         kfree(buf);
1086         return res;
1087 }
1088 
1089 
1090 
1091 
1092 
1093 
1094 
1095 
1096 
1097 static void mark_bbt_region(struct nand_chip *this, struct nand_bbt_descr *td)
1098 {
1099         u64 targetsize = nanddev_target_size(&this->base);
1100         struct mtd_info *mtd = nand_to_mtd(this);
1101         int i, j, chips, block, nrblocks, update;
1102         uint8_t oldval;
1103 
1104         
1105         if (td->options & NAND_BBT_PERCHIP) {
1106                 chips = nanddev_ntargets(&this->base);
1107                 nrblocks = (int)(targetsize >> this->bbt_erase_shift);
1108         } else {
1109                 chips = 1;
1110                 nrblocks = (int)(mtd->size >> this->bbt_erase_shift);
1111         }
1112 
1113         for (i = 0; i < chips; i++) {
1114                 if ((td->options & NAND_BBT_ABSPAGE) ||
1115                     !(td->options & NAND_BBT_WRITE)) {
1116                         if (td->pages[i] == -1)
1117                                 continue;
1118                         block = td->pages[i] >> (this->bbt_erase_shift - this->page_shift);
1119                         oldval = bbt_get_entry(this, block);
1120                         bbt_mark_entry(this, block, BBT_BLOCK_RESERVED);
1121                         if ((oldval != BBT_BLOCK_RESERVED) &&
1122                                         td->reserved_block_code)
1123                                 nand_update_bbt(this, (loff_t)block <<
1124                                                 this->bbt_erase_shift);
1125                         continue;
1126                 }
1127                 update = 0;
1128                 if (td->options & NAND_BBT_LASTBLOCK)
1129                         block = ((i + 1) * nrblocks) - td->maxblocks;
1130                 else
1131                         block = i * nrblocks;
1132                 for (j = 0; j < td->maxblocks; j++) {
1133                         oldval = bbt_get_entry(this, block);
1134                         bbt_mark_entry(this, block, BBT_BLOCK_RESERVED);
1135                         if (oldval != BBT_BLOCK_RESERVED)
1136                                 update = 1;
1137                         block++;
1138                 }
1139                 
1140 
1141 
1142 
1143 
1144                 if (update && td->reserved_block_code)
1145                         nand_update_bbt(this, (loff_t)(block - 1) <<
1146                                         this->bbt_erase_shift);
1147         }
1148 }
1149 
1150 
1151 
1152 
1153 
1154 
1155 
1156 
1157 
1158 static void verify_bbt_descr(struct nand_chip *this, struct nand_bbt_descr *bd)
1159 {
1160         u64 targetsize = nanddev_target_size(&this->base);
1161         struct mtd_info *mtd = nand_to_mtd(this);
1162         u32 pattern_len;
1163         u32 bits;
1164         u32 table_size;
1165 
1166         if (!bd)
1167                 return;
1168 
1169         pattern_len = bd->len;
1170         bits = bd->options & NAND_BBT_NRBITS_MSK;
1171 
1172         BUG_ON((this->bbt_options & NAND_BBT_NO_OOB) &&
1173                         !(this->bbt_options & NAND_BBT_USE_FLASH));
1174         BUG_ON(!bits);
1175 
1176         if (bd->options & NAND_BBT_VERSION)
1177                 pattern_len++;
1178 
1179         if (bd->options & NAND_BBT_NO_OOB) {
1180                 BUG_ON(!(this->bbt_options & NAND_BBT_USE_FLASH));
1181                 BUG_ON(!(this->bbt_options & NAND_BBT_NO_OOB));
1182                 BUG_ON(bd->offs);
1183                 if (bd->options & NAND_BBT_VERSION)
1184                         BUG_ON(bd->veroffs != bd->len);
1185                 BUG_ON(bd->options & NAND_BBT_SAVECONTENT);
1186         }
1187 
1188         if (bd->options & NAND_BBT_PERCHIP)
1189                 table_size = targetsize >> this->bbt_erase_shift;
1190         else
1191                 table_size = mtd->size >> this->bbt_erase_shift;
1192         table_size >>= 3;
1193         table_size *= bits;
1194         if (bd->options & NAND_BBT_NO_OOB)
1195                 table_size += pattern_len;
1196         BUG_ON(table_size > (1 << this->bbt_erase_shift));
1197 }
1198 
1199 
1200 
1201 
1202 
1203 
1204 
1205 
1206 
1207 
1208 
1209 
1210 
1211 static int nand_scan_bbt(struct nand_chip *this, struct nand_bbt_descr *bd)
1212 {
1213         struct mtd_info *mtd = nand_to_mtd(this);
1214         int len, res;
1215         uint8_t *buf;
1216         struct nand_bbt_descr *td = this->bbt_td;
1217         struct nand_bbt_descr *md = this->bbt_md;
1218 
1219         len = (mtd->size >> (this->bbt_erase_shift + 2)) ? : 1;
1220         
1221 
1222 
1223 
1224         this->bbt = kzalloc(len, GFP_KERNEL);
1225         if (!this->bbt)
1226                 return -ENOMEM;
1227 
1228         
1229 
1230 
1231 
1232         if (!td) {
1233                 if ((res = nand_memory_bbt(this, bd))) {
1234                         pr_err("nand_bbt: can't scan flash and build the RAM-based BBT\n");
1235                         goto err_free_bbt;
1236                 }
1237                 return 0;
1238         }
1239         verify_bbt_descr(this, td);
1240         verify_bbt_descr(this, md);
1241 
1242         
1243         len = (1 << this->bbt_erase_shift);
1244         len += (len >> this->page_shift) * mtd->oobsize;
1245         buf = vmalloc(len);
1246         if (!buf) {
1247                 res = -ENOMEM;
1248                 goto err_free_bbt;
1249         }
1250 
1251         
1252         if (td->options & NAND_BBT_ABSPAGE) {
1253                 read_abs_bbts(this, buf, td, md);
1254         } else {
1255                 
1256                 search_read_bbts(this, buf, td, md);
1257         }
1258 
1259         res = check_create(this, buf, bd);
1260         if (res)
1261                 goto err_free_buf;
1262 
1263         
1264         mark_bbt_region(this, td);
1265         if (md)
1266                 mark_bbt_region(this, md);
1267 
1268         vfree(buf);
1269         return 0;
1270 
1271 err_free_buf:
1272         vfree(buf);
1273 err_free_bbt:
1274         kfree(this->bbt);
1275         this->bbt = NULL;
1276         return res;
1277 }
1278 
1279 
1280 
1281 
1282 
1283 static uint8_t scan_ff_pattern[] = { 0xff, 0xff };
1284 
1285 
1286 static uint8_t bbt_pattern[] = {'B', 'b', 't', '0' };
1287 static uint8_t mirror_pattern[] = {'1', 't', 'b', 'B' };
1288 
1289 static struct nand_bbt_descr bbt_main_descr = {
1290         .options = NAND_BBT_LASTBLOCK | NAND_BBT_CREATE | NAND_BBT_WRITE
1291                 | NAND_BBT_2BIT | NAND_BBT_VERSION | NAND_BBT_PERCHIP,
1292         .offs = 8,
1293         .len = 4,
1294         .veroffs = 12,
1295         .maxblocks = NAND_BBT_SCAN_MAXBLOCKS,
1296         .pattern = bbt_pattern
1297 };
1298 
1299 static struct nand_bbt_descr bbt_mirror_descr = {
1300         .options = NAND_BBT_LASTBLOCK | NAND_BBT_CREATE | NAND_BBT_WRITE
1301                 | NAND_BBT_2BIT | NAND_BBT_VERSION | NAND_BBT_PERCHIP,
1302         .offs = 8,
1303         .len = 4,
1304         .veroffs = 12,
1305         .maxblocks = NAND_BBT_SCAN_MAXBLOCKS,
1306         .pattern = mirror_pattern
1307 };
1308 
1309 static struct nand_bbt_descr bbt_main_no_oob_descr = {
1310         .options = NAND_BBT_LASTBLOCK | NAND_BBT_CREATE | NAND_BBT_WRITE
1311                 | NAND_BBT_2BIT | NAND_BBT_VERSION | NAND_BBT_PERCHIP
1312                 | NAND_BBT_NO_OOB,
1313         .len = 4,
1314         .veroffs = 4,
1315         .maxblocks = NAND_BBT_SCAN_MAXBLOCKS,
1316         .pattern = bbt_pattern
1317 };
1318 
1319 static struct nand_bbt_descr bbt_mirror_no_oob_descr = {
1320         .options = NAND_BBT_LASTBLOCK | NAND_BBT_CREATE | NAND_BBT_WRITE
1321                 | NAND_BBT_2BIT | NAND_BBT_VERSION | NAND_BBT_PERCHIP
1322                 | NAND_BBT_NO_OOB,
1323         .len = 4,
1324         .veroffs = 4,
1325         .maxblocks = NAND_BBT_SCAN_MAXBLOCKS,
1326         .pattern = mirror_pattern
1327 };
1328 
1329 #define BADBLOCK_SCAN_MASK (~NAND_BBT_NO_OOB)
1330 
1331 
1332 
1333 
1334 
1335 
1336 
1337 
1338 
1339 static int nand_create_badblock_pattern(struct nand_chip *this)
1340 {
1341         struct nand_bbt_descr *bd;
1342         if (this->badblock_pattern) {
1343                 pr_warn("Bad block pattern already allocated; not replacing\n");
1344                 return -EINVAL;
1345         }
1346         bd = kzalloc(sizeof(*bd), GFP_KERNEL);
1347         if (!bd)
1348                 return -ENOMEM;
1349         bd->options = this->bbt_options & BADBLOCK_SCAN_MASK;
1350         bd->offs = this->badblockpos;
1351         bd->len = (this->options & NAND_BUSWIDTH_16) ? 2 : 1;
1352         bd->pattern = scan_ff_pattern;
1353         bd->options |= NAND_BBT_DYNAMICSTRUCT;
1354         this->badblock_pattern = bd;
1355         return 0;
1356 }
1357 
1358 
1359 
1360 
1361 
1362 
1363 
1364 
1365 int nand_create_bbt(struct nand_chip *this)
1366 {
1367         int ret;
1368 
1369         
1370         if (this->bbt_options & NAND_BBT_USE_FLASH) {
1371                 
1372                 if (!this->bbt_td) {
1373                         if (this->bbt_options & NAND_BBT_NO_OOB) {
1374                                 this->bbt_td = &bbt_main_no_oob_descr;
1375                                 this->bbt_md = &bbt_mirror_no_oob_descr;
1376                         } else {
1377                                 this->bbt_td = &bbt_main_descr;
1378                                 this->bbt_md = &bbt_mirror_descr;
1379                         }
1380                 }
1381         } else {
1382                 this->bbt_td = NULL;
1383                 this->bbt_md = NULL;
1384         }
1385 
1386         if (!this->badblock_pattern) {
1387                 ret = nand_create_badblock_pattern(this);
1388                 if (ret)
1389                         return ret;
1390         }
1391 
1392         return nand_scan_bbt(this, this->badblock_pattern);
1393 }
1394 EXPORT_SYMBOL(nand_create_bbt);
1395 
1396 
1397 
1398 
1399 
1400 
1401 int nand_isreserved_bbt(struct nand_chip *this, loff_t offs)
1402 {
1403         int block;
1404 
1405         block = (int)(offs >> this->bbt_erase_shift);
1406         return bbt_get_entry(this, block) == BBT_BLOCK_RESERVED;
1407 }
1408 
1409 
1410 
1411 
1412 
1413 
1414 
1415 int nand_isbad_bbt(struct nand_chip *this, loff_t offs, int allowbbt)
1416 {
1417         int block, res;
1418 
1419         block = (int)(offs >> this->bbt_erase_shift);
1420         res = bbt_get_entry(this, block);
1421 
1422         pr_debug("nand_isbad_bbt(): bbt info for offs 0x%08x: (block %d) 0x%02x\n",
1423                  (unsigned int)offs, block, res);
1424 
1425         switch (res) {
1426         case BBT_BLOCK_GOOD:
1427                 return 0;
1428         case BBT_BLOCK_WORN:
1429                 return 1;
1430         case BBT_BLOCK_RESERVED:
1431                 return allowbbt ? 0 : 1;
1432         }
1433         return 1;
1434 }
1435 
1436 
1437 
1438 
1439 
1440 
1441 int nand_markbad_bbt(struct nand_chip *this, loff_t offs)
1442 {
1443         int block, ret = 0;
1444 
1445         block = (int)(offs >> this->bbt_erase_shift);
1446 
1447         
1448         bbt_mark_entry(this, block, BBT_BLOCK_WORN);
1449 
1450         
1451         if (this->bbt_options & NAND_BBT_USE_FLASH)
1452                 ret = nand_update_bbt(this, offs);
1453 
1454         return ret;
1455 }