fedora core 6 1.2949 + vserver 2.2.0
[linux-2.6.git] / drivers / mtd / inftlmount.c
index 64cc97c..8f6006f 100644 (file)
@@ -1,14 +1,14 @@
-/* 
+/*
  * inftlmount.c -- INFTL mount code with extensive checks.
  *
  * Author: Greg Ungerer (gerg@snapgear.com)
  * (C) Copyright 2002-2003, Greg Ungerer (gerg@snapgear.com)
  *
  * Based heavily on the nftlmount.c code which is:
- * Author: Fabrice Bellard (fabrice.bellard@netgem.com) 
+ * Author: Fabrice Bellard (fabrice.bellard@netgem.com)
  * Copyright (C) 2000 Netgem S.A.
  *
- * $Id: inftlmount.c,v 1.14 2004/08/09 13:57:42 dwmw2 Exp $
+ * $Id: inftlmount.c,v 1.18 2005/11/07 11:14:20 gleixner Exp $
  *
  * This program is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
 #include <linux/mtd/inftl.h>
 #include <linux/mtd/compatmac.h>
 
-char inftlmountrev[]="$Revision: 1.14 $";
+char inftlmountrev[]="$Revision: 1.18 $";
+
+extern int inftl_read_oob(struct mtd_info *mtd, loff_t offs, size_t len,
+                         size_t *retlen, uint8_t *buf);
+extern int inftl_write_oob(struct mtd_info *mtd, loff_t offs, size_t len,
+                          size_t *retlen, uint8_t *buf);
 
 /*
  * find_boot_record: Find the INFTL Media Header and its Spare copy which
@@ -57,6 +62,7 @@ static int find_boot_record(struct INFTLrecord *inftl)
        unsigned int i, block;
        u8 buf[SECTORSIZE];
        struct INFTLMediaHeader *mh = &inftl->MediaHdr;
+       struct mtd_info *mtd = inftl->mbd.mtd;
        struct INFTLPartition *ip;
        size_t retlen;
 
@@ -80,8 +86,8 @@ static int find_boot_record(struct INFTLrecord *inftl)
                 * Check for BNAND header first. Then whinge if it's found
                 * but later checks fail.
                 */
-               ret = MTD_READ(inftl->mbd.mtd, block * inftl->EraseSize,
-                   SECTORSIZE, &retlen, buf);
+               ret = mtd->read(mtd, block * inftl->EraseSize,
+                               SECTORSIZE, &retlen, buf);
                /* We ignore ret in case the ECC of the MediaHeader is invalid
                   (which is apparently acceptable) */
                if (retlen != SECTORSIZE) {
@@ -106,8 +112,9 @@ static int find_boot_record(struct INFTLrecord *inftl)
                }
 
                /* To be safer with BIOS, also use erase mark as discriminant */
-               if ((ret = MTD_READOOB(inftl->mbd.mtd, block * inftl->EraseSize +
-                   SECTORSIZE + 8, 8, &retlen, (char *)&h1) < 0)) {
+               if ((ret = inftl_read_oob(mtd, block * inftl->EraseSize +
+                                         SECTORSIZE + 8, 8, &retlen,
+                                         (char *)&h1) < 0)) {
                        printk(KERN_WARNING "INFTL: ANAND header found at "
                                "0x%x in mtd%d, but OOB data read failed "
                                "(err %d)\n", block * inftl->EraseSize,
@@ -123,8 +130,8 @@ static int find_boot_record(struct INFTLrecord *inftl)
                memcpy(mh, buf, sizeof(struct INFTLMediaHeader));
 
                /* Read the spare media header at offset 4096 */
-               MTD_READ(inftl->mbd.mtd, block * inftl->EraseSize + 4096,
-                   SECTORSIZE, &retlen, buf);
+               mtd->read(mtd, block * inftl->EraseSize + 4096,
+                         SECTORSIZE, &retlen, buf);
                if (retlen != SECTORSIZE) {
                        printk(KERN_WARNING "INFTL: Unable to read spare "
                               "Media Header\n");
@@ -222,6 +229,8 @@ static int find_boot_record(struct INFTLrecord *inftl)
                        if (ip->Reserved0 != ip->firstUnit) {
                                struct erase_info *instr = &inftl->instr;
 
+                               instr->mtd = inftl->mbd.mtd;
+
                                /*
                                 *      Most likely this is using the
                                 *      undocumented qiuck mount feature.
@@ -231,7 +240,7 @@ static int find_boot_record(struct INFTLrecord *inftl)
                                 */
                                instr->addr = ip->Reserved0 * inftl->EraseSize;
                                instr->len = inftl->EraseSize;
-                               MTD_ERASE(inftl->mbd.mtd, instr);
+                               mtd->erase(mtd, instr);
                        }
                        if ((ip->lastUnit - ip->firstUnit + 1) < ip->virtualUnits) {
                                printk(KERN_WARNING "INFTL: Media Header "
@@ -271,7 +280,7 @@ static int find_boot_record(struct INFTLrecord *inftl)
                                inftl->nb_boot_blocks);
                        return -1;
                }
-               
+
                inftl->mbd.size  = inftl->numvunits *
                        (inftl->EraseSize / SECTORSIZE);
 
@@ -300,7 +309,7 @@ static int find_boot_record(struct INFTLrecord *inftl)
                                inftl->nb_blocks * sizeof(u16));
                        return -ENOMEM;
                }
-               
+
                /* Mark the blocks before INFTL MediaHeader as reserved */
                for (i = 0; i < inftl->nb_boot_blocks; i++)
                        inftl->PUtable[i] = BLOCK_RESERVED;
@@ -348,21 +357,21 @@ static int check_free_sectors(struct INFTLrecord *inftl, unsigned int address,
        int len, int check_oob)
 {
        u8 buf[SECTORSIZE + inftl->mbd.mtd->oobsize];
+       struct mtd_info *mtd = inftl->mbd.mtd;
        size_t retlen;
        int i;
 
-       DEBUG(MTD_DEBUG_LEVEL3, "INFTL: check_free_sectors(inftl=%p,"
-               "address=0x%x,len=%d,check_oob=%d)\n", inftl,
-               address, len, check_oob);
-
        for (i = 0; i < len; i += SECTORSIZE) {
-               if (MTD_READECC(inftl->mbd.mtd, address, SECTORSIZE, &retlen, buf, &buf[SECTORSIZE], &inftl->oobinfo) < 0)
+               if (mtd->read(mtd, address, SECTORSIZE, &retlen, buf))
                        return -1;
                if (memcmpb(buf, 0xff, SECTORSIZE) != 0)
                        return -1;
 
                if (check_oob) {
-                       if (memcmpb(buf + SECTORSIZE, 0xff, inftl->mbd.mtd->oobsize) != 0)
+                       if(inftl_read_oob(mtd, address, mtd->oobsize,
+                                         &retlen, &buf[SECTORSIZE]) < 0)
+                               return -1;
+                       if (memcmpb(buf + SECTORSIZE, 0xff, mtd->oobsize) != 0)
                                return -1;
                }
                address += SECTORSIZE;
@@ -378,13 +387,14 @@ static int check_free_sectors(struct INFTLrecord *inftl, unsigned int address,
  *
  * Return: 0 when succeed, -1 on error.
  *
- * ToDo: 1. Is it neceressary to check_free_sector after erasing ?? 
+ * ToDo: 1. Is it neceressary to check_free_sector after erasing ??
  */
 int INFTL_formatblock(struct INFTLrecord *inftl, int block)
 {
        size_t retlen;
        struct inftl_unittail uci;
        struct erase_info *instr = &inftl->instr;
+       struct mtd_info *mtd = inftl->mbd.mtd;
        int physblock;
 
        DEBUG(MTD_DEBUG_LEVEL3, "INFTL: INFTL_formatblock(inftl=%p,"
@@ -396,13 +406,15 @@ int INFTL_formatblock(struct INFTLrecord *inftl, int block)
           _first_? */
 
        /* Use async erase interface, test return code */
+       instr->mtd = inftl->mbd.mtd;
        instr->addr = block * inftl->EraseSize;
        instr->len = inftl->mbd.mtd->erasesize;
        /* Erase one physical eraseblock at a time, even though the NAND api
           allows us to group them.  This way we if we have a failure, we can
           mark only the failed block in the bbt. */
-       for (physblock = 0; physblock < inftl->EraseSize; physblock += instr->len, instr->addr += instr->len) {
-               MTD_ERASE(inftl->mbd.mtd, instr);
+       for (physblock = 0; physblock < inftl->EraseSize;
+            physblock += instr->len, instr->addr += instr->len) {
+               mtd->erase(inftl->mbd.mtd, instr);
 
                if (instr->state == MTD_ERASE_FAILED) {
                        printk(KERN_WARNING "INFTL: error while formatting block %d\n",
@@ -411,10 +423,10 @@ int INFTL_formatblock(struct INFTLrecord *inftl, int block)
                }
 
                /*
-               * Check the "freeness" of Erase Unit before updating metadata.
-               * FixMe: is this check really necessary? Since we have check the
-               *        return code after the erase operation.
-               */
+                * Check the "freeness" of Erase Unit before updating metadata.
+                * FixMe: is this check really necessary? Since we have check
+                * the return code after the erase operation.
+                */
                if (check_free_sectors(inftl, instr->addr, instr->len, 1) != 0)
                        goto fail;
        }
@@ -426,8 +438,7 @@ int INFTL_formatblock(struct INFTLrecord *inftl, int block)
        uci.Reserved[2] = 0;
        uci.Reserved[3] = 0;
        instr->addr = block * inftl->EraseSize + SECTORSIZE * 2;
-       if (MTD_WRITEOOB(inftl->mbd.mtd, instr->addr +
-           8, 8, &retlen, (char *)&uci) < 0)
+       if (inftl_write_oob(mtd, instr->addr + 8, 8, &retlen, (char *)&uci) < 0)
                goto fail;
        return 0;
 fail:
@@ -546,6 +557,7 @@ void INFTL_dumpVUchains(struct INFTLrecord *s)
 
 int INFTL_mount(struct INFTLrecord *s)
 {
+       struct mtd_info *mtd = s->mbd.mtd;
        unsigned int block, first_block, prev_block, last_block;
        unsigned int first_logical_block, logical_block, erase_mark;
        int chain_length, do_format_chain;
@@ -560,7 +572,7 @@ int INFTL_mount(struct INFTLrecord *s)
        /* Search for INFTL MediaHeader and Spare INFTL Media Header */
        if (find_boot_record(s) < 0) {
                printk(KERN_WARNING "INFTL: could not find valid boot record?\n");
-               return -1;
+               return -ENXIO;
        }
 
        /* Init the logical to physical table */
@@ -571,6 +583,12 @@ int INFTL_mount(struct INFTLrecord *s)
 
        /* Temporary buffer to store ANAC numbers. */
        ANACtable = kmalloc(s->nb_blocks * sizeof(u8), GFP_KERNEL);
+       if (!ANACtable) {
+               printk(KERN_WARNING "INFTL: allocation of ANACtable "
+                               "failed (%zd bytes)\n",
+                               s->nb_blocks * sizeof(u8));
+               return -ENOMEM;
+       }
        memset(ANACtable, 0, s->nb_blocks);
 
        /*
@@ -592,16 +610,17 @@ int INFTL_mount(struct INFTLrecord *s)
 
                for (chain_length = 0; ; chain_length++) {
 
-                       if ((chain_length == 0) && 
+                       if ((chain_length == 0) &&
                            (s->PUtable[block] != BLOCK_NOTEXPLORED)) {
                                /* Nothing to do here, onto next block */
                                break;
                        }
 
-                       if (MTD_READOOB(s->mbd.mtd, block * s->EraseSize + 8,
-                           8, &retlen, (char *)&h0) < 0 ||
-                           MTD_READOOB(s->mbd.mtd, block * s->EraseSize +
-                           2 * SECTORSIZE + 8, 8, &retlen, (char *)&h1) < 0) {
+                       if (inftl_read_oob(mtd, block * s->EraseSize + 8,
+                                          8, &retlen, (char *)&h0) < 0 ||
+                           inftl_read_oob(mtd, block * s->EraseSize +
+                                          2 * SECTORSIZE + 8, 8, &retlen,
+                                          (char *)&h1) < 0) {
                                /* Should never happen? */
                                do_format_chain++;
                                break;
@@ -739,7 +758,7 @@ int INFTL_mount(struct INFTLrecord *s)
                                        "in virtual chain %d\n",
                                        s->PUtable[block], logical_block);
                                s->PUtable[block] = BLOCK_NIL;
-                                       
+
                        }
                        if (ANACtable[block] != ANAC) {
                                /*