Merge remote-tracking branch 'aosp/android-3.0' into develop-3.0-jb
[firefly-linux-kernel-4.4.55.git] / fs / yaffs2 / yaffs_mtdif1.c
index bcac47025ece1362ad5ec0157ad9aaf0a10f49e9..51083695eb3347eea20734317ac27d2b276338e8 100644 (file)
@@ -1,10 +1,11 @@
 /*
  * YAFFS: Yet another FFS. A NAND-flash specific file system.
- * yaffs_mtdif1.c  NAND mtd interface functions for small-page NAND.
  *
- * Copyright (C) 2002 Aleph One Ltd.
+ * Copyright (C) 2002-2010 Aleph One Ltd.
  *   for Toby Churchill Ltd and Brightstar Engineering
  *
+ * Created by Charles Manning <charles@aleph1.co.uk>
+ *
  * This program is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License version 2 as
  * published by the Free Software Foundation.
  *
  * These functions are invoked via function pointers in yaffs_nand.c.
  * This replaces functionality provided by functions in yaffs_mtdif.c
- * and the yaffs_TagsCompatability functions in yaffs_tagscompat.c that are
+ * and the yaffs_tags compatability functions in yaffs_tagscompat.c that are
  * called in yaffs_mtdif.c when the function pointers are NULL.
- * We assume the MTD layer is performing ECC (useNANDECC is true).
+ * We assume the MTD layer is performing ECC (use_nand_ecc is true).
  */
 
 #include "yportenv.h"
+#include "yaffs_trace.h"
 #include "yaffs_guts.h"
 #include "yaffs_packedtags1.h"
-#include "yaffs_tagscompat.h"  /* for yaffs_CalcTagsECC */
+#include "yaffs_tagscompat.h"  /* for yaffs_calc_tags_ecc */
+#include "yaffs_linux.h"
 
 #include "linux/kernel.h"
 #include "linux/version.h"
 #include "linux/types.h"
 #include "linux/mtd/mtd.h"
 
-/* Don't compile this module if we don't have MTD's mtd_oob_ops interface */
-#if (MTD_VERSION_CODE > MTD_VERSION(2, 6, 17))
-
-const char *yaffs_mtdif1_c_version = "$Id$";
-
 #ifndef CONFIG_YAFFS_9BYTE_TAGS
 # define YTAG1_SIZE 8
 #else
 # define YTAG1_SIZE 9
 #endif
 
-#if 0
-/* Use the following nand_ecclayout with MTD when using
- * CONFIG_YAFFS_9BYTE_TAGS and the older on-NAND tags layout.
- * If you have existing Yaffs images and the byte order differs from this,
- * adjust 'oobfree' to match your existing Yaffs data.
- *
- * This nand_ecclayout scatters/gathers to/from the old-yaffs layout with the
- * pageStatus byte (at NAND spare offset 4) scattered/gathered from/to
- * the 9th byte.
- *
- * Old-style on-NAND format: T0,T1,T2,T3,P,B,T4,T5,E0,E1,E2,T6,T7,E3,E4,E5
- * We have/need PackedTags1 plus pageStatus: T0,T1,T2,T3,T4,T5,T6,T7,P
- * where Tn are the tag bytes, En are MTD's ECC bytes, P is the pageStatus
- * byte and B is the small-page bad-block indicator byte.
- */
-static struct nand_ecclayout nand_oob_16 = {
-       .eccbytes = 6,
-       .eccpos = { 8, 9, 10, 13, 14, 15 },
-       .oobavail = 9,
-       .oobfree = { { 0, 4 }, { 6, 2 }, { 11, 2 }, { 4, 1 } }
-};
-#endif
-
 /* Write a chunk (page) of data to NAND.
  *
  * Caller always provides ExtendedTags data which are converted to a more
  * compact (packed) form for storage in NAND.  A mini-ECC runs over the
  * contents of the tags meta-data; used to valid the tags when read.
  *
- *  - Pack ExtendedTags to PackedTags1 form
- *  - Compute mini-ECC for PackedTags1
+ *  - Pack ExtendedTags to packed_tags1 form
+ *  - Compute mini-ECC for packed_tags1
  *  - Write data and packed tags to NAND.
  *
- * Note: Due to the use of the PackedTags1 meta-data which does not include
- * a full sequence number (as found in the larger PackedTags2 form) it is
+ * Note: Due to the use of the packed_tags1 meta-data which does not include
+ * a full sequence number (as found in the larger packed_tags2 form) it is
  * necessary for Yaffs to re-write a chunk/page (just once) to mark it as
  * discarded and dirty.  This is not ideal: newer NAND parts are supposed
  * to be written just once.  When Yaffs performs this operation, this
@@ -88,67 +63,68 @@ static struct nand_ecclayout nand_oob_16 = {
  * Any underlying MTD error results in YAFFS_FAIL.
  * Returns YAFFS_OK or YAFFS_FAIL.
  */
-int nandmtd1_WriteChunkWithTagsToNAND(yaffs_Device *dev,
-       int chunkInNAND, const __u8 *data, const yaffs_ExtendedTags *etags)
+int nandmtd1_write_chunk_tags(struct yaffs_dev *dev,
+                             int nand_chunk, const u8 * data,
+                             const struct yaffs_ext_tags *etags)
 {
-       struct mtd_info *mtd = dev->genericDevice;
-       int chunkBytes = dev->nDataBytesPerChunk;
-       loff_t addr = ((loff_t)chunkInNAND) * chunkBytes;
+       struct mtd_info *mtd = yaffs_dev_to_mtd(dev);
+       int chunk_bytes = dev->data_bytes_per_chunk;
+       loff_t addr = ((loff_t) nand_chunk) * chunk_bytes;
        struct mtd_oob_ops ops;
-       yaffs_PackedTags1 pt1;
+       struct yaffs_packed_tags1 pt1;
        int retval;
 
-       /* we assume that PackedTags1 and yaffs_Tags are compatible */
-       compile_time_assertion(sizeof(yaffs_PackedTags1) == 12);
-       compile_time_assertion(sizeof(yaffs_Tags) == 8);
+       /* we assume that packed_tags1 and struct yaffs_tags are compatible */
+       compile_time_assertion(sizeof(struct yaffs_packed_tags1) == 12);
+       compile_time_assertion(sizeof(struct yaffs_tags) == 8);
 
-       yaffs_PackTags1(&pt1, etags);
-       yaffs_CalcTagsECC((yaffs_Tags *)&pt1);
+       yaffs_pack_tags1(&pt1, etags);
+       yaffs_calc_tags_ecc((struct yaffs_tags *)&pt1);
 
        /* When deleting a chunk, the upper layer provides only skeletal
-        * etags, one with chunkDeleted set.  However, we need to update the
+        * etags, one with is_deleted set.  However, we need to update the
         * tags, not erase them completely.  So we use the NAND write property
         * that only zeroed-bits stick and set tag bytes to all-ones and
         * zero just the (not) deleted bit.
         */
 #ifndef CONFIG_YAFFS_9BYTE_TAGS
-       if (etags->chunkDeleted) {
+       if (etags->is_deleted) {
                memset(&pt1, 0xff, 8);
                /* clear delete status bit to indicate deleted */
                pt1.deleted = 0;
        }
 #else
-       ((__u8 *)&pt1)[8] = 0xff;
-       if (etags->chunkDeleted) {
+       ((u8 *) & pt1)[8] = 0xff;
+       if (etags->is_deleted) {
                memset(&pt1, 0xff, 8);
-               /* zero pageStatus byte to indicate deleted */
-               ((__u8 *)&pt1)[8] = 0;
+               /* zero page_status byte to indicate deleted */
+               ((u8 *) & pt1)[8] = 0;
        }
 #endif
 
        memset(&ops, 0, sizeof(ops));
        ops.mode = MTD_OOB_AUTO;
-       ops.len = (data) ? chunkBytes : 0;
+       ops.len = (data) ? chunk_bytes : 0;
        ops.ooblen = YTAG1_SIZE;
-       ops.datbuf = (__u8 *)data;
-       ops.oobbuf = (__u8 *)&pt1;
+       ops.datbuf = (u8 *) data;
+       ops.oobbuf = (u8 *) & pt1;
 
        retval = mtd->write_oob(mtd, addr, &ops);
        if (retval) {
                yaffs_trace(YAFFS_TRACE_MTD,
-                       "write_oob failed, chunk %d, mtd error %d\n",
-                       chunkInNAND, retval);
+                       "write_oob failed, chunk %d, mtd error %d",
+                       nand_chunk, retval);
        }
        return retval ? YAFFS_FAIL : YAFFS_OK;
 }
 
-/* Return with empty ExtendedTags but add eccResult.
+/* Return with empty ExtendedTags but add ecc_result.
  */
-static int rettags(yaffs_ExtendedTags *etags, int eccResult, int retval)
+static int rettags(struct yaffs_ext_tags *etags, int ecc_result, int retval)
 {
        if (etags) {
                memset(etags, 0, sizeof(*etags));
-               etags->eccResult = eccResult;
+               etags->ecc_result = ecc_result;
        }
        return retval;
 }
@@ -156,49 +132,44 @@ static int rettags(yaffs_ExtendedTags *etags, int eccResult, int retval)
 /* Read a chunk (page) from NAND.
  *
  * Caller expects ExtendedTags data to be usable even on error; that is,
- * all members except eccResult and blockBad are zeroed.
+ * all members except ecc_result and block_bad are zeroed.
  *
  *  - Check ECC results for data (if applicable)
  *  - Check for blank/erased block (return empty ExtendedTags if blank)
- *  - Check the PackedTags1 mini-ECC (correct if necessary/possible)
- *  - Convert PackedTags1 to ExtendedTags
- *  - Update eccResult and blockBad members to refect state.
+ *  - Check the packed_tags1 mini-ECC (correct if necessary/possible)
+ *  - Convert packed_tags1 to ExtendedTags
+ *  - Update ecc_result and block_bad members to refect state.
  *
  * Returns YAFFS_OK or YAFFS_FAIL.
  */
-int nandmtd1_ReadChunkWithTagsFromNAND(yaffs_Device *dev,
-       int chunkInNAND, __u8 *data, yaffs_ExtendedTags *etags)
+int nandmtd1_read_chunk_tags(struct yaffs_dev *dev,
+                            int nand_chunk, u8 * data,
+                            struct yaffs_ext_tags *etags)
 {
-       struct mtd_info *mtd = dev->genericDevice;
-       int chunkBytes = dev->nDataBytesPerChunk;
-       loff_t addr = ((loff_t)chunkInNAND) * chunkBytes;
+       struct mtd_info *mtd = yaffs_dev_to_mtd(dev);
+       int chunk_bytes = dev->data_bytes_per_chunk;
+       loff_t addr = ((loff_t) nand_chunk) * chunk_bytes;
        int eccres = YAFFS_ECC_RESULT_NO_ERROR;
        struct mtd_oob_ops ops;
-       yaffs_PackedTags1 pt1;
+       struct yaffs_packed_tags1 pt1;
        int retval;
        int deleted;
 
        memset(&ops, 0, sizeof(ops));
        ops.mode = MTD_OOB_AUTO;
-       ops.len = (data) ? chunkBytes : 0;
+       ops.len = (data) ? chunk_bytes : 0;
        ops.ooblen = YTAG1_SIZE;
        ops.datbuf = data;
-       ops.oobbuf = (__u8 *)&pt1;
+       ops.oobbuf = (u8 *) & pt1;
 
-#if (MTD_VERSION_CODE < MTD_VERSION(2, 6, 20))
-       /* In MTD 2.6.18 to 2.6.19 nand_base.c:nand_do_read_oob() has a bug;
-        * help it out with ops.len = ops.ooblen when ops.datbuf == NULL.
-        */
-       ops.len = (ops.datbuf) ? ops.len : ops.ooblen;
-#endif
        /* Read page and oob using MTD.
         * Check status and determine ECC result.
         */
        retval = mtd->read_oob(mtd, addr, &ops);
        if (retval) {
                yaffs_trace(YAFFS_TRACE_MTD,
-                       "read_oob failed, chunk %d, mtd error %d\n",
-                       chunkInNAND, retval);
+                       "read_oob failed, chunk %d, mtd error %d",
+                       nand_chunk, retval);
        }
 
        switch (retval) {
@@ -209,26 +180,25 @@ int nandmtd1_ReadChunkWithTagsFromNAND(yaffs_Device *dev,
        case -EUCLEAN:
                /* MTD's ECC fixed the data */
                eccres = YAFFS_ECC_RESULT_FIXED;
-               dev->eccFixed++;
+               dev->n_ecc_fixed++;
                break;
 
        case -EBADMSG:
                /* MTD's ECC could not fix the data */
-               dev->eccUnfixed++;
+               dev->n_ecc_unfixed++;
                /* fall into... */
        default:
                rettags(etags, YAFFS_ECC_RESULT_UNFIXED, 0);
-               etags->blockBad = (mtd->block_isbad)(mtd, addr);
+               etags->block_bad = (mtd->block_isbad) (mtd, addr);
                return YAFFS_FAIL;
        }
 
        /* Check for a blank/erased chunk.
         */
-       if (yaffs_CheckFF((__u8 *)&pt1, 8)) {
-               /* when blank, upper layers want eccResult to be <= NO_ERROR */
+       if (yaffs_check_ff((u8 *) & pt1, 8)) {
+               /* when blank, upper layers want ecc_result to be <= NO_ERROR */
                return rettags(etags, YAFFS_ECC_RESULT_NO_ERROR, YAFFS_OK);
        }
-
 #ifndef CONFIG_YAFFS_9BYTE_TAGS
        /* Read deleted status (bit) then return it to it's non-deleted
         * state before performing tags mini-ECC check. pt1.deleted is
@@ -237,37 +207,37 @@ int nandmtd1_ReadChunkWithTagsFromNAND(yaffs_Device *dev,
        deleted = !pt1.deleted;
        pt1.deleted = 1;
 #else
-       deleted = (yaffs_CountBits(((__u8 *)&pt1)[8]) < 7);
+       deleted = (yaffs_count_bits(((u8 *) & pt1)[8]) < 7);
 #endif
 
        /* Check the packed tags mini-ECC and correct if necessary/possible.
         */
-       retval = yaffs_CheckECCOnTags((yaffs_Tags *)&pt1);
+       retval = yaffs_check_tags_ecc((struct yaffs_tags *)&pt1);
        switch (retval) {
        case 0:
                /* no tags error, use MTD result */
                break;
        case 1:
                /* recovered tags-ECC error */
-               dev->tagsEccFixed++;
+               dev->n_tags_ecc_fixed++;
                if (eccres == YAFFS_ECC_RESULT_NO_ERROR)
                        eccres = YAFFS_ECC_RESULT_FIXED;
                break;
        default:
                /* unrecovered tags-ECC error */
-               dev->tagsEccUnfixed++;
+               dev->n_tags_ecc_unfixed++;
                return rettags(etags, YAFFS_ECC_RESULT_UNFIXED, YAFFS_FAIL);
        }
 
        /* Unpack the tags to extended form and set ECC result.
-        * [set shouldBeFF just to keep yaffs_UnpackTags1 happy]
+        * [set should_be_ff just to keep yaffs_unpack_tags1 happy]
         */
-       pt1.shouldBeFF = 0xFFFFFFFF;
-       yaffs_UnpackTags1(etags, &pt1);
-       etags->eccResult = eccres;
+       pt1.should_be_ff = 0xFFFFFFFF;
+       yaffs_unpack_tags1(etags, &pt1);
+       etags->ecc_result = eccres;
 
        /* Set deleted state */
-       etags->chunkDeleted = deleted;
+       etags->is_deleted = deleted;
        return YAFFS_OK;
 }
 
@@ -278,15 +248,16 @@ int nandmtd1_ReadChunkWithTagsFromNAND(yaffs_Device *dev,
  *
  * Returns YAFFS_OK or YAFFS_FAIL.
  */
-int nandmtd1_MarkNANDBlockBad(struct yaffs_DeviceStruct *dev, int blockNo)
+int nandmtd1_mark_block_bad(struct yaffs_dev *dev, int block_no)
 {
-       struct mtd_info *mtd = dev->genericDevice;
-       int blocksize = dev->nChunksPerBlock * dev->nDataBytesPerChunk;
+       struct mtd_info *mtd = yaffs_dev_to_mtd(dev);
+       int blocksize = dev->param.chunks_per_block * dev->data_bytes_per_chunk;
        int retval;
 
-       yaffs_trace(YAFFS_TRACE_BAD_BLOCKS, "marking block %d bad\n", blockNo);
+       yaffs_trace(YAFFS_TRACE_BAD_BLOCKS,
+               "marking block %d bad", block_no);
 
-       retval = mtd->block_markbad(mtd, (loff_t)blocksize * blockNo);
+       retval = mtd->block_markbad(mtd, (loff_t) blocksize * block_no);
        return (retval) ? YAFFS_FAIL : YAFFS_OK;
 }
 
@@ -294,7 +265,7 @@ int nandmtd1_MarkNANDBlockBad(struct yaffs_DeviceStruct *dev, int blockNo)
  *
  * Returns YAFFS_OK or YAFFS_FAIL.
  */
-static int nandmtd1_TestPrerequists(struct mtd_info *mtd)
+static int nandmtd1_test_prerequists(struct mtd_info *mtd)
 {
        /* 2.6.18 has mtd->ecclayout->oobavail */
        /* 2.6.21 has mtd->ecclayout->oobavail and mtd->oobavail */
@@ -302,7 +273,7 @@ static int nandmtd1_TestPrerequists(struct mtd_info *mtd)
 
        if (oobavail < YTAG1_SIZE) {
                yaffs_trace(YAFFS_TRACE_ERROR,
-                       "mtd device has only %d bytes for tags, need %d\n",
+                       "mtd device has only %d bytes for tags, need %d",
                        oobavail, YTAG1_SIZE);
                return YAFFS_FAIL;
        }
@@ -318,13 +289,13 @@ static int nandmtd1_TestPrerequists(struct mtd_info *mtd)
  *
  * Always returns YAFFS_OK.
  */
-int nandmtd1_QueryNANDBlock(struct yaffs_DeviceStruct *dev, int blockNo,
-       yaffs_BlockState *pState, __u32 *pSequenceNumber)
+int nandmtd1_query_block(struct yaffs_dev *dev, int block_no,
+                        enum yaffs_block_state *state_ptr, u32 * seq_ptr)
 {
-       struct mtd_info *mtd = dev->genericDevice;
-       int chunkNo = blockNo * dev->nChunksPerBlock;
-       loff_t addr = (loff_t)chunkNo * dev->nDataBytesPerChunk;
-       yaffs_ExtendedTags etags;
+       struct mtd_info *mtd = yaffs_dev_to_mtd(dev);
+       int chunk_num = block_no * dev->param.chunks_per_block;
+       loff_t addr = (loff_t) chunk_num * dev->data_bytes_per_chunk;
+       struct yaffs_ext_tags etags;
        int state = YAFFS_BLOCK_STATE_DEAD;
        int seqnum = 0;
        int retval;
@@ -332,30 +303,28 @@ int nandmtd1_QueryNANDBlock(struct yaffs_DeviceStruct *dev, int blockNo,
        /* We don't yet have a good place to test for MTD config prerequists.
         * Do it here as we are called during the initial scan.
         */
-       if (nandmtd1_TestPrerequists(mtd) != YAFFS_OK)
+       if (nandmtd1_test_prerequists(mtd) != YAFFS_OK)
                return YAFFS_FAIL;
 
-       retval = nandmtd1_ReadChunkWithTagsFromNAND(dev, chunkNo, NULL, &etags);
-       etags.blockBad = (mtd->block_isbad)(mtd, addr);
-       if (etags.blockBad) {
+       retval = nandmtd1_read_chunk_tags(dev, chunk_num, NULL, &etags);
+       etags.block_bad = (mtd->block_isbad) (mtd, addr);
+       if (etags.block_bad) {
                yaffs_trace(YAFFS_TRACE_BAD_BLOCKS,
-                       "block %d is marked bad\n", blockNo);
+                       "block %d is marked bad", block_no);
                state = YAFFS_BLOCK_STATE_DEAD;
-       } else if (etags.eccResult != YAFFS_ECC_RESULT_NO_ERROR) {
+       } else if (etags.ecc_result != YAFFS_ECC_RESULT_NO_ERROR) {
                /* bad tags, need to look more closely */
                state = YAFFS_BLOCK_STATE_NEEDS_SCANNING;
-       } else if (etags.chunkUsed) {
+       } else if (etags.chunk_used) {
                state = YAFFS_BLOCK_STATE_NEEDS_SCANNING;
-               seqnum = etags.sequenceNumber;
+               seqnum = etags.seq_number;
        } else {
                state = YAFFS_BLOCK_STATE_EMPTY;
        }
 
-       *pState = state;
-       *pSequenceNumber = seqnum;
+       *state_ptr = state;
+       *seq_ptr = seqnum;
 
        /* query always succeeds */
        return YAFFS_OK;
 }
-
-#endif /*MTD_VERSION*/