Merge branch 'devel' of git://git.kernel.org/pub/scm/linux/kernel/git/rostedt/linux...
[linux-2.6] / drivers / mtd / inftlmount.c
1 /*
2  * inftlmount.c -- INFTL mount code with extensive checks.
3  *
4  * Author: Greg Ungerer (gerg@snapgear.com)
5  * (C) Copyright 2002-2003, Greg Ungerer (gerg@snapgear.com)
6  *
7  * Based heavily on the nftlmount.c code which is:
8  * Author: Fabrice Bellard (fabrice.bellard@netgem.com)
9  * Copyright (C) 2000 Netgem S.A.
10  *
11  * This program is free software; you can redistribute it and/or modify
12  * it under the terms of the GNU General Public License as published by
13  * the Free Software Foundation; either version 2 of the License, or
14  * (at your option) any later version.
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  * GNU General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program; if not, write to the Free Software
23  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
24  */
25
26 #include <linux/kernel.h>
27 #include <linux/module.h>
28 #include <asm/errno.h>
29 #include <asm/io.h>
30 #include <asm/uaccess.h>
31 #include <linux/miscdevice.h>
32 #include <linux/delay.h>
33 #include <linux/slab.h>
34 #include <linux/init.h>
35 #include <linux/mtd/mtd.h>
36 #include <linux/mtd/nftl.h>
37 #include <linux/mtd/inftl.h>
38 #include <linux/mtd/compatmac.h>
39
40 /*
41  * find_boot_record: Find the INFTL Media Header and its Spare copy which
42  *      contains the various device information of the INFTL partition and
43  *      Bad Unit Table. Update the PUtable[] table according to the Bad
44  *      Unit Table. PUtable[] is used for management of Erase Unit in
45  *      other routines in inftlcore.c and inftlmount.c.
46  */
47 static int find_boot_record(struct INFTLrecord *inftl)
48 {
49         struct inftl_unittail h1;
50         //struct inftl_oob oob;
51         unsigned int i, block;
52         u8 buf[SECTORSIZE];
53         struct INFTLMediaHeader *mh = &inftl->MediaHdr;
54         struct mtd_info *mtd = inftl->mbd.mtd;
55         struct INFTLPartition *ip;
56         size_t retlen;
57
58         DEBUG(MTD_DEBUG_LEVEL3, "INFTL: find_boot_record(inftl=%p)\n", inftl);
59
60         /*
61          * Assume logical EraseSize == physical erasesize for starting the
62          * scan. We'll sort it out later if we find a MediaHeader which says
63          * otherwise.
64          */
65         inftl->EraseSize = inftl->mbd.mtd->erasesize;
66         inftl->nb_blocks = inftl->mbd.mtd->size / inftl->EraseSize;
67
68         inftl->MediaUnit = BLOCK_NIL;
69
70         /* Search for a valid boot record */
71         for (block = 0; block < inftl->nb_blocks; block++) {
72                 int ret;
73
74                 /*
75                  * Check for BNAND header first. Then whinge if it's found
76                  * but later checks fail.
77                  */
78                 ret = mtd->read(mtd, block * inftl->EraseSize,
79                                 SECTORSIZE, &retlen, buf);
80                 /* We ignore ret in case the ECC of the MediaHeader is invalid
81                    (which is apparently acceptable) */
82                 if (retlen != SECTORSIZE) {
83                         static int warncount = 5;
84
85                         if (warncount) {
86                                 printk(KERN_WARNING "INFTL: block read at 0x%x "
87                                         "of mtd%d failed: %d\n",
88                                         block * inftl->EraseSize,
89                                         inftl->mbd.mtd->index, ret);
90                                 if (!--warncount)
91                                         printk(KERN_WARNING "INFTL: further "
92                                                 "failures for this block will "
93                                                 "not be printed\n");
94                         }
95                         continue;
96                 }
97
98                 if (retlen < 6 || memcmp(buf, "BNAND", 6)) {
99                         /* BNAND\0 not found. Continue */
100                         continue;
101                 }
102
103                 /* To be safer with BIOS, also use erase mark as discriminant */
104                 if ((ret = inftl_read_oob(mtd, block * inftl->EraseSize +
105                                           SECTORSIZE + 8, 8, &retlen,
106                                           (char *)&h1) < 0)) {
107                         printk(KERN_WARNING "INFTL: ANAND header found at "
108                                 "0x%x in mtd%d, but OOB data read failed "
109                                 "(err %d)\n", block * inftl->EraseSize,
110                                 inftl->mbd.mtd->index, ret);
111                         continue;
112                 }
113
114
115                 /*
116                  * This is the first we've seen.
117                  * Copy the media header structure into place.
118                  */
119                 memcpy(mh, buf, sizeof(struct INFTLMediaHeader));
120
121                 /* Read the spare media header at offset 4096 */
122                 mtd->read(mtd, block * inftl->EraseSize + 4096,
123                           SECTORSIZE, &retlen, buf);
124                 if (retlen != SECTORSIZE) {
125                         printk(KERN_WARNING "INFTL: Unable to read spare "
126                                "Media Header\n");
127                         return -1;
128                 }
129                 /* Check if this one is the same as the first one we found. */
130                 if (memcmp(mh, buf, sizeof(struct INFTLMediaHeader))) {
131                         printk(KERN_WARNING "INFTL: Primary and spare Media "
132                                "Headers disagree.\n");
133                         return -1;
134                 }
135
136                 mh->NoOfBootImageBlocks = le32_to_cpu(mh->NoOfBootImageBlocks);
137                 mh->NoOfBinaryPartitions = le32_to_cpu(mh->NoOfBinaryPartitions);
138                 mh->NoOfBDTLPartitions = le32_to_cpu(mh->NoOfBDTLPartitions);
139                 mh->BlockMultiplierBits = le32_to_cpu(mh->BlockMultiplierBits);
140                 mh->FormatFlags = le32_to_cpu(mh->FormatFlags);
141                 mh->PercentUsed = le32_to_cpu(mh->PercentUsed);
142
143 #ifdef CONFIG_MTD_DEBUG_VERBOSE
144                 if (CONFIG_MTD_DEBUG_VERBOSE >= 2) {
145                         printk("INFTL: Media Header ->\n"
146                                 "    bootRecordID          = %s\n"
147                                 "    NoOfBootImageBlocks   = %d\n"
148                                 "    NoOfBinaryPartitions  = %d\n"
149                                 "    NoOfBDTLPartitions    = %d\n"
150                                 "    BlockMultiplerBits    = %d\n"
151                                 "    FormatFlgs            = %d\n"
152                                 "    OsakVersion           = 0x%x\n"
153                                 "    PercentUsed           = %d\n",
154                                 mh->bootRecordID, mh->NoOfBootImageBlocks,
155                                 mh->NoOfBinaryPartitions,
156                                 mh->NoOfBDTLPartitions,
157                                 mh->BlockMultiplierBits, mh->FormatFlags,
158                                 mh->OsakVersion, mh->PercentUsed);
159                 }
160 #endif
161
162                 if (mh->NoOfBDTLPartitions == 0) {
163                         printk(KERN_WARNING "INFTL: Media Header sanity check "
164                                 "failed: NoOfBDTLPartitions (%d) == 0, "
165                                 "must be at least 1\n", mh->NoOfBDTLPartitions);
166                         return -1;
167                 }
168
169                 if ((mh->NoOfBDTLPartitions + mh->NoOfBinaryPartitions) > 4) {
170                         printk(KERN_WARNING "INFTL: Media Header sanity check "
171                                 "failed: Total Partitions (%d) > 4, "
172                                 "BDTL=%d Binary=%d\n", mh->NoOfBDTLPartitions +
173                                 mh->NoOfBinaryPartitions,
174                                 mh->NoOfBDTLPartitions,
175                                 mh->NoOfBinaryPartitions);
176                         return -1;
177                 }
178
179                 if (mh->BlockMultiplierBits > 1) {
180                         printk(KERN_WARNING "INFTL: sorry, we don't support "
181                                 "UnitSizeFactor 0x%02x\n",
182                                 mh->BlockMultiplierBits);
183                         return -1;
184                 } else if (mh->BlockMultiplierBits == 1) {
185                         printk(KERN_WARNING "INFTL: support for INFTL with "
186                                 "UnitSizeFactor 0x%02x is experimental\n",
187                                 mh->BlockMultiplierBits);
188                         inftl->EraseSize = inftl->mbd.mtd->erasesize <<
189                                 mh->BlockMultiplierBits;
190                         inftl->nb_blocks = inftl->mbd.mtd->size / inftl->EraseSize;
191                         block >>= mh->BlockMultiplierBits;
192                 }
193
194                 /* Scan the partitions */
195                 for (i = 0; (i < 4); i++) {
196                         ip = &mh->Partitions[i];
197                         ip->virtualUnits = le32_to_cpu(ip->virtualUnits);
198                         ip->firstUnit = le32_to_cpu(ip->firstUnit);
199                         ip->lastUnit = le32_to_cpu(ip->lastUnit);
200                         ip->flags = le32_to_cpu(ip->flags);
201                         ip->spareUnits = le32_to_cpu(ip->spareUnits);
202                         ip->Reserved0 = le32_to_cpu(ip->Reserved0);
203
204 #ifdef CONFIG_MTD_DEBUG_VERBOSE
205                         if (CONFIG_MTD_DEBUG_VERBOSE >= 2) {
206                                 printk("    PARTITION[%d] ->\n"
207                                         "        virtualUnits    = %d\n"
208                                         "        firstUnit       = %d\n"
209                                         "        lastUnit        = %d\n"
210                                         "        flags           = 0x%x\n"
211                                         "        spareUnits      = %d\n",
212                                         i, ip->virtualUnits, ip->firstUnit,
213                                         ip->lastUnit, ip->flags,
214                                         ip->spareUnits);
215                         }
216 #endif
217
218                         if (ip->Reserved0 != ip->firstUnit) {
219                                 struct erase_info *instr = &inftl->instr;
220
221                                 instr->mtd = inftl->mbd.mtd;
222
223                                 /*
224                                  *      Most likely this is using the
225                                  *      undocumented qiuck mount feature.
226                                  *      We don't support that, we will need
227                                  *      to erase the hidden block for full
228                                  *      compatibility.
229                                  */
230                                 instr->addr = ip->Reserved0 * inftl->EraseSize;
231                                 instr->len = inftl->EraseSize;
232                                 mtd->erase(mtd, instr);
233                         }
234                         if ((ip->lastUnit - ip->firstUnit + 1) < ip->virtualUnits) {
235                                 printk(KERN_WARNING "INFTL: Media Header "
236                                         "Partition %d sanity check failed\n"
237                                         "    firstUnit %d : lastUnit %d  >  "
238                                         "virtualUnits %d\n", i, ip->lastUnit,
239                                         ip->firstUnit, ip->Reserved0);
240                                 return -1;
241                         }
242                         if (ip->Reserved1 != 0) {
243                                 printk(KERN_WARNING "INFTL: Media Header "
244                                         "Partition %d sanity check failed: "
245                                         "Reserved1 %d != 0\n",
246                                         i, ip->Reserved1);
247                                 return -1;
248                         }
249
250                         if (ip->flags & INFTL_BDTL)
251                                 break;
252                 }
253
254                 if (i >= 4) {
255                         printk(KERN_WARNING "INFTL: Media Header Partition "
256                                 "sanity check failed:\n       No partition "
257                                 "marked as Disk Partition\n");
258                         return -1;
259                 }
260
261                 inftl->nb_boot_blocks = ip->firstUnit;
262                 inftl->numvunits = ip->virtualUnits;
263                 if (inftl->numvunits > (inftl->nb_blocks -
264                     inftl->nb_boot_blocks - 2)) {
265                         printk(KERN_WARNING "INFTL: Media Header sanity check "
266                                 "failed:\n        numvunits (%d) > nb_blocks "
267                                 "(%d) - nb_boot_blocks(%d) - 2\n",
268                                 inftl->numvunits, inftl->nb_blocks,
269                                 inftl->nb_boot_blocks);
270                         return -1;
271                 }
272
273                 inftl->mbd.size  = inftl->numvunits *
274                         (inftl->EraseSize / SECTORSIZE);
275
276                 /*
277                  * Block count is set to last used EUN (we won't need to keep
278                  * any meta-data past that point).
279                  */
280                 inftl->firstEUN = ip->firstUnit;
281                 inftl->lastEUN = ip->lastUnit;
282                 inftl->nb_blocks = ip->lastUnit + 1;
283
284                 /* Memory alloc */
285                 inftl->PUtable = kmalloc(inftl->nb_blocks * sizeof(u16), GFP_KERNEL);
286                 if (!inftl->PUtable) {
287                         printk(KERN_WARNING "INFTL: allocation of PUtable "
288                                 "failed (%zd bytes)\n",
289                                 inftl->nb_blocks * sizeof(u16));
290                         return -ENOMEM;
291                 }
292
293                 inftl->VUtable = kmalloc(inftl->nb_blocks * sizeof(u16), GFP_KERNEL);
294                 if (!inftl->VUtable) {
295                         kfree(inftl->PUtable);
296                         printk(KERN_WARNING "INFTL: allocation of VUtable "
297                                 "failed (%zd bytes)\n",
298                                 inftl->nb_blocks * sizeof(u16));
299                         return -ENOMEM;
300                 }
301
302                 /* Mark the blocks before INFTL MediaHeader as reserved */
303                 for (i = 0; i < inftl->nb_boot_blocks; i++)
304                         inftl->PUtable[i] = BLOCK_RESERVED;
305                 /* Mark all remaining blocks as potentially containing data */
306                 for (; i < inftl->nb_blocks; i++)
307                         inftl->PUtable[i] = BLOCK_NOTEXPLORED;
308
309                 /* Mark this boot record (NFTL MediaHeader) block as reserved */
310                 inftl->PUtable[block] = BLOCK_RESERVED;
311
312                 /* Read Bad Erase Unit Table and modify PUtable[] accordingly */
313                 for (i = 0; i < inftl->nb_blocks; i++) {
314                         int physblock;
315                         /* If any of the physical eraseblocks are bad, don't
316                            use the unit. */
317                         for (physblock = 0; physblock < inftl->EraseSize; physblock += inftl->mbd.mtd->erasesize) {
318                                 if (inftl->mbd.mtd->block_isbad(inftl->mbd.mtd, i * inftl->EraseSize + physblock))
319                                         inftl->PUtable[i] = BLOCK_RESERVED;
320                         }
321                 }
322
323                 inftl->MediaUnit = block;
324                 return 0;
325         }
326
327         /* Not found. */
328         return -1;
329 }
330
331 static int memcmpb(void *a, int c, int n)
332 {
333         int i;
334         for (i = 0; i < n; i++) {
335                 if (c != ((unsigned char *)a)[i])
336                         return 1;
337         }
338         return 0;
339 }
340
341 /*
342  * check_free_sector: check if a free sector is actually FREE,
343  *      i.e. All 0xff in data and oob area.
344  */
345 static int check_free_sectors(struct INFTLrecord *inftl, unsigned int address,
346         int len, int check_oob)
347 {
348         u8 buf[SECTORSIZE + inftl->mbd.mtd->oobsize];
349         struct mtd_info *mtd = inftl->mbd.mtd;
350         size_t retlen;
351         int i;
352
353         for (i = 0; i < len; i += SECTORSIZE) {
354                 if (mtd->read(mtd, address, SECTORSIZE, &retlen, buf))
355                         return -1;
356                 if (memcmpb(buf, 0xff, SECTORSIZE) != 0)
357                         return -1;
358
359                 if (check_oob) {
360                         if(inftl_read_oob(mtd, address, mtd->oobsize,
361                                           &retlen, &buf[SECTORSIZE]) < 0)
362                                 return -1;
363                         if (memcmpb(buf + SECTORSIZE, 0xff, mtd->oobsize) != 0)
364                                 return -1;
365                 }
366                 address += SECTORSIZE;
367         }
368
369         return 0;
370 }
371
372 /*
373  * INFTL_format: format a Erase Unit by erasing ALL Erase Zones in the Erase
374  *               Unit and Update INFTL metadata. Each erase operation is
375  *               checked with check_free_sectors.
376  *
377  * Return: 0 when succeed, -1 on error.
378  *
379  * ToDo: 1. Is it neceressary to check_free_sector after erasing ??
380  */
381 int INFTL_formatblock(struct INFTLrecord *inftl, int block)
382 {
383         size_t retlen;
384         struct inftl_unittail uci;
385         struct erase_info *instr = &inftl->instr;
386         struct mtd_info *mtd = inftl->mbd.mtd;
387         int physblock;
388
389         DEBUG(MTD_DEBUG_LEVEL3, "INFTL: INFTL_formatblock(inftl=%p,"
390                 "block=%d)\n", inftl, block);
391
392         memset(instr, 0, sizeof(struct erase_info));
393
394         /* FIXME: Shouldn't we be setting the 'discarded' flag to zero
395            _first_? */
396
397         /* Use async erase interface, test return code */
398         instr->mtd = inftl->mbd.mtd;
399         instr->addr = block * inftl->EraseSize;
400         instr->len = inftl->mbd.mtd->erasesize;
401         /* Erase one physical eraseblock at a time, even though the NAND api
402            allows us to group them.  This way we if we have a failure, we can
403            mark only the failed block in the bbt. */
404         for (physblock = 0; physblock < inftl->EraseSize;
405              physblock += instr->len, instr->addr += instr->len) {
406                 mtd->erase(inftl->mbd.mtd, instr);
407
408                 if (instr->state == MTD_ERASE_FAILED) {
409                         printk(KERN_WARNING "INFTL: error while formatting block %d\n",
410                                 block);
411                         goto fail;
412                 }
413
414                 /*
415                  * Check the "freeness" of Erase Unit before updating metadata.
416                  * FixMe: is this check really necessary? Since we have check
417                  * the return code after the erase operation.
418                  */
419                 if (check_free_sectors(inftl, instr->addr, instr->len, 1) != 0)
420                         goto fail;
421         }
422
423         uci.EraseMark = cpu_to_le16(ERASE_MARK);
424         uci.EraseMark1 = cpu_to_le16(ERASE_MARK);
425         uci.Reserved[0] = 0;
426         uci.Reserved[1] = 0;
427         uci.Reserved[2] = 0;
428         uci.Reserved[3] = 0;
429         instr->addr = block * inftl->EraseSize + SECTORSIZE * 2;
430         if (inftl_write_oob(mtd, instr->addr + 8, 8, &retlen, (char *)&uci) < 0)
431                 goto fail;
432         return 0;
433 fail:
434         /* could not format, update the bad block table (caller is responsible
435            for setting the PUtable to BLOCK_RESERVED on failure) */
436         inftl->mbd.mtd->block_markbad(inftl->mbd.mtd, instr->addr);
437         return -1;
438 }
439
440 /*
441  * format_chain: Format an invalid Virtual Unit chain. It frees all the Erase
442  *      Units in a Virtual Unit Chain, i.e. all the units are disconnected.
443  *
444  *      Since the chain is invalid then we will have to erase it from its
445  *      head (normally for INFTL we go from the oldest). But if it has a
446  *      loop then there is no oldest...
447  */
448 static void format_chain(struct INFTLrecord *inftl, unsigned int first_block)
449 {
450         unsigned int block = first_block, block1;
451
452         printk(KERN_WARNING "INFTL: formatting chain at block %d\n",
453                 first_block);
454
455         for (;;) {
456                 block1 = inftl->PUtable[block];
457
458                 printk(KERN_WARNING "INFTL: formatting block %d\n", block);
459                 if (INFTL_formatblock(inftl, block) < 0) {
460                         /*
461                          * Cannot format !!!! Mark it as Bad Unit,
462                          */
463                         inftl->PUtable[block] = BLOCK_RESERVED;
464                 } else {
465                         inftl->PUtable[block] = BLOCK_FREE;
466                 }
467
468                 /* Goto next block on the chain */
469                 block = block1;
470
471                 if (block == BLOCK_NIL || block >= inftl->lastEUN)
472                         break;
473         }
474 }
475
476 void INFTL_dumptables(struct INFTLrecord *s)
477 {
478         int i;
479
480         printk("-------------------------------------------"
481                 "----------------------------------\n");
482
483         printk("VUtable[%d] ->", s->nb_blocks);
484         for (i = 0; i < s->nb_blocks; i++) {
485                 if ((i % 8) == 0)
486                         printk("\n%04x: ", i);
487                 printk("%04x ", s->VUtable[i]);
488         }
489
490         printk("\n-------------------------------------------"
491                 "----------------------------------\n");
492
493         printk("PUtable[%d-%d=%d] ->", s->firstEUN, s->lastEUN, s->nb_blocks);
494         for (i = 0; i <= s->lastEUN; i++) {
495                 if ((i % 8) == 0)
496                         printk("\n%04x: ", i);
497                 printk("%04x ", s->PUtable[i]);
498         }
499
500         printk("\n-------------------------------------------"
501                 "----------------------------------\n");
502
503         printk("INFTL ->\n"
504                 "  EraseSize       = %d\n"
505                 "  h/s/c           = %d/%d/%d\n"
506                 "  numvunits       = %d\n"
507                 "  firstEUN        = %d\n"
508                 "  lastEUN         = %d\n"
509                 "  numfreeEUNs     = %d\n"
510                 "  LastFreeEUN     = %d\n"
511                 "  nb_blocks       = %d\n"
512                 "  nb_boot_blocks  = %d",
513                 s->EraseSize, s->heads, s->sectors, s->cylinders,
514                 s->numvunits, s->firstEUN, s->lastEUN, s->numfreeEUNs,
515                 s->LastFreeEUN, s->nb_blocks, s->nb_boot_blocks);
516
517         printk("\n-------------------------------------------"
518                 "----------------------------------\n");
519 }
520
521 void INFTL_dumpVUchains(struct INFTLrecord *s)
522 {
523         int logical, block, i;
524
525         printk("-------------------------------------------"
526                 "----------------------------------\n");
527
528         printk("INFTL Virtual Unit Chains:\n");
529         for (logical = 0; logical < s->nb_blocks; logical++) {
530                 block = s->VUtable[logical];
531                 if (block > s->nb_blocks)
532                         continue;
533                 printk("  LOGICAL %d --> %d ", logical, block);
534                 for (i = 0; i < s->nb_blocks; i++) {
535                         if (s->PUtable[block] == BLOCK_NIL)
536                                 break;
537                         block = s->PUtable[block];
538                         printk("%d ", block);
539                 }
540                 printk("\n");
541         }
542
543         printk("-------------------------------------------"
544                 "----------------------------------\n");
545 }
546
547 int INFTL_mount(struct INFTLrecord *s)
548 {
549         struct mtd_info *mtd = s->mbd.mtd;
550         unsigned int block, first_block, prev_block, last_block;
551         unsigned int first_logical_block, logical_block, erase_mark;
552         int chain_length, do_format_chain;
553         struct inftl_unithead1 h0;
554         struct inftl_unittail h1;
555         size_t retlen;
556         int i;
557         u8 *ANACtable, ANAC;
558
559         DEBUG(MTD_DEBUG_LEVEL3, "INFTL: INFTL_mount(inftl=%p)\n", s);
560
561         /* Search for INFTL MediaHeader and Spare INFTL Media Header */
562         if (find_boot_record(s) < 0) {
563                 printk(KERN_WARNING "INFTL: could not find valid boot record?\n");
564                 return -ENXIO;
565         }
566
567         /* Init the logical to physical table */
568         for (i = 0; i < s->nb_blocks; i++)
569                 s->VUtable[i] = BLOCK_NIL;
570
571         logical_block = block = BLOCK_NIL;
572
573         /* Temporary buffer to store ANAC numbers. */
574         ANACtable = kcalloc(s->nb_blocks, sizeof(u8), GFP_KERNEL);
575         if (!ANACtable) {
576                 printk(KERN_WARNING "INFTL: allocation of ANACtable "
577                                 "failed (%zd bytes)\n",
578                                 s->nb_blocks * sizeof(u8));
579                 return -ENOMEM;
580         }
581
582         /*
583          * First pass is to explore each physical unit, and construct the
584          * virtual chains that exist (newest physical unit goes into VUtable).
585          * Any block that is in any way invalid will be left in the
586          * NOTEXPLORED state. Then at the end we will try to format it and
587          * mark it as free.
588          */
589         DEBUG(MTD_DEBUG_LEVEL3, "INFTL: pass 1, explore each unit\n");
590         for (first_block = s->firstEUN; first_block <= s->lastEUN; first_block++) {
591                 if (s->PUtable[first_block] != BLOCK_NOTEXPLORED)
592                         continue;
593
594                 do_format_chain = 0;
595                 first_logical_block = BLOCK_NIL;
596                 last_block = BLOCK_NIL;
597                 block = first_block;
598
599                 for (chain_length = 0; ; chain_length++) {
600
601                         if ((chain_length == 0) &&
602                             (s->PUtable[block] != BLOCK_NOTEXPLORED)) {
603                                 /* Nothing to do here, onto next block */
604                                 break;
605                         }
606
607                         if (inftl_read_oob(mtd, block * s->EraseSize + 8,
608                                            8, &retlen, (char *)&h0) < 0 ||
609                             inftl_read_oob(mtd, block * s->EraseSize +
610                                            2 * SECTORSIZE + 8, 8, &retlen,
611                                            (char *)&h1) < 0) {
612                                 /* Should never happen? */
613                                 do_format_chain++;
614                                 break;
615                         }
616
617                         logical_block = le16_to_cpu(h0.virtualUnitNo);
618                         prev_block = le16_to_cpu(h0.prevUnitNo);
619                         erase_mark = le16_to_cpu((h1.EraseMark | h1.EraseMark1));
620                         ANACtable[block] = h0.ANAC;
621
622                         /* Previous block is relative to start of Partition */
623                         if (prev_block < s->nb_blocks)
624                                 prev_block += s->firstEUN;
625
626                         /* Already explored partial chain? */
627                         if (s->PUtable[block] != BLOCK_NOTEXPLORED) {
628                                 /* Check if chain for this logical */
629                                 if (logical_block == first_logical_block) {
630                                         if (last_block != BLOCK_NIL)
631                                                 s->PUtable[last_block] = block;
632                                 }
633                                 break;
634                         }
635
636                         /* Check for invalid block */
637                         if (erase_mark != ERASE_MARK) {
638                                 printk(KERN_WARNING "INFTL: corrupt block %d "
639                                         "in chain %d, chain length %d, erase "
640                                         "mark 0x%x?\n", block, first_block,
641                                         chain_length, erase_mark);
642                                 /*
643                                  * Assume end of chain, probably incomplete
644                                  * fold/erase...
645                                  */
646                                 if (chain_length == 0)
647                                         do_format_chain++;
648                                 break;
649                         }
650
651                         /* Check for it being free already then... */
652                         if ((logical_block == BLOCK_FREE) ||
653                             (logical_block == BLOCK_NIL)) {
654                                 s->PUtable[block] = BLOCK_FREE;
655                                 break;
656                         }
657
658                         /* Sanity checks on block numbers */
659                         if ((logical_block >= s->nb_blocks) ||
660                             ((prev_block >= s->nb_blocks) &&
661                              (prev_block != BLOCK_NIL))) {
662                                 if (chain_length > 0) {
663                                         printk(KERN_WARNING "INFTL: corrupt "
664                                                 "block %d in chain %d?\n",
665                                                 block, first_block);
666                                         do_format_chain++;
667                                 }
668                                 break;
669                         }
670
671                         if (first_logical_block == BLOCK_NIL) {
672                                 first_logical_block = logical_block;
673                         } else {
674                                 if (first_logical_block != logical_block) {
675                                         /* Normal for folded chain... */
676                                         break;
677                                 }
678                         }
679
680                         /*
681                          * Current block is valid, so if we followed a virtual
682                          * chain to get here then we can set the previous
683                          * block pointer in our PUtable now. Then move onto
684                          * the previous block in the chain.
685                          */
686                         s->PUtable[block] = BLOCK_NIL;
687                         if (last_block != BLOCK_NIL)
688                                 s->PUtable[last_block] = block;
689                         last_block = block;
690                         block = prev_block;
691
692                         /* Check for end of chain */
693                         if (block == BLOCK_NIL)
694                                 break;
695
696                         /* Validate next block before following it... */
697                         if (block > s->lastEUN) {
698                                 printk(KERN_WARNING "INFTL: invalid previous "
699                                         "block %d in chain %d?\n", block,
700                                         first_block);
701                                 do_format_chain++;
702                                 break;
703                         }
704                 }
705
706                 if (do_format_chain) {
707                         format_chain(s, first_block);
708                         continue;
709                 }
710
711                 /*
712                  * Looks like a valid chain then. It may not really be the
713                  * newest block in the chain, but it is the newest we have
714                  * found so far. We might update it in later iterations of
715                  * this loop if we find something newer.
716                  */
717                 s->VUtable[first_logical_block] = first_block;
718                 logical_block = BLOCK_NIL;
719         }
720
721 #ifdef CONFIG_MTD_DEBUG_VERBOSE
722         if (CONFIG_MTD_DEBUG_VERBOSE >= 2)
723                 INFTL_dumptables(s);
724 #endif
725
726         /*
727          * Second pass, check for infinite loops in chains. These are
728          * possible because we don't update the previous pointers when
729          * we fold chains. No big deal, just fix them up in PUtable.
730          */
731         DEBUG(MTD_DEBUG_LEVEL3, "INFTL: pass 2, validate virtual chains\n");
732         for (logical_block = 0; logical_block < s->numvunits; logical_block++) {
733                 block = s->VUtable[logical_block];
734                 last_block = BLOCK_NIL;
735
736                 /* Check for free/reserved/nil */
737                 if (block >= BLOCK_RESERVED)
738                         continue;
739
740                 ANAC = ANACtable[block];
741                 for (i = 0; i < s->numvunits; i++) {
742                         if (s->PUtable[block] == BLOCK_NIL)
743                                 break;
744                         if (s->PUtable[block] > s->lastEUN) {
745                                 printk(KERN_WARNING "INFTL: invalid prev %d, "
746                                         "in virtual chain %d\n",
747                                         s->PUtable[block], logical_block);
748                                 s->PUtable[block] = BLOCK_NIL;
749
750                         }
751                         if (ANACtable[block] != ANAC) {
752                                 /*
753                                  * Chain must point back to itself. This is ok,
754                                  * but we will need adjust the tables with this
755                                  * newest block and oldest block.
756                                  */
757                                 s->VUtable[logical_block] = block;
758                                 s->PUtable[last_block] = BLOCK_NIL;
759                                 break;
760                         }
761
762                         ANAC--;
763                         last_block = block;
764                         block = s->PUtable[block];
765                 }
766
767                 if (i >= s->nb_blocks) {
768                         /*
769                          * Uhoo, infinite chain with valid ANACS!
770                          * Format whole chain...
771                          */
772                         format_chain(s, first_block);
773                 }
774         }
775
776 #ifdef CONFIG_MTD_DEBUG_VERBOSE
777         if (CONFIG_MTD_DEBUG_VERBOSE >= 2)
778                 INFTL_dumptables(s);
779         if (CONFIG_MTD_DEBUG_VERBOSE >= 2)
780                 INFTL_dumpVUchains(s);
781 #endif
782
783         /*
784          * Third pass, format unreferenced blocks and init free block count.
785          */
786         s->numfreeEUNs = 0;
787         s->LastFreeEUN = BLOCK_NIL;
788
789         DEBUG(MTD_DEBUG_LEVEL3, "INFTL: pass 3, format unused blocks\n");
790         for (block = s->firstEUN; block <= s->lastEUN; block++) {
791                 if (s->PUtable[block] == BLOCK_NOTEXPLORED) {
792                         printk("INFTL: unreferenced block %d, formatting it\n",
793                                 block);
794                         if (INFTL_formatblock(s, block) < 0)
795                                 s->PUtable[block] = BLOCK_RESERVED;
796                         else
797                                 s->PUtable[block] = BLOCK_FREE;
798                 }
799                 if (s->PUtable[block] == BLOCK_FREE) {
800                         s->numfreeEUNs++;
801                         if (s->LastFreeEUN == BLOCK_NIL)
802                                 s->LastFreeEUN = block;
803                 }
804         }
805
806         kfree(ANACtable);
807         return 0;
808 }