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