2 * YAFFS: Yet another FFS. A NAND-flash specific file system.
4 * Copyright (C) 2002-2010 Aleph One Ltd.
5 * for Toby Churchill Ltd and Brightstar Engineering
7 * Created by Charles Manning <charles@aleph1.co.uk>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License version 2 as
11 * published by the Free Software Foundation.
15 * This module provides the interface between yaffs_nand.c and the
16 * MTD API. This version is used when the MTD interface supports the
17 * 'mtd_oob_ops' style calls to read_oob and write_oob, circa 2.6.17,
18 * and we have small-page NAND device.
20 * These functions are invoked via function pointers in yaffs_nand.c.
21 * This replaces functionality provided by functions in yaffs_mtdif.c
22 * and the yaffs_TagsCompatability functions in yaffs_tagscompat.c that are
23 * called in yaffs_mtdif.c when the function pointers are NULL.
24 * We assume the MTD layer is performing ECC (useNANDECC is true).
28 #include "yaffs_trace.h"
29 #include "yaffs_guts.h"
30 #include "yaffs_packedtags1.h"
31 #include "yaffs_tagscompat.h" /* for yaffs_CalcTagsECC */
32 #include "yaffs_linux.h"
34 #include "linux/kernel.h"
35 #include "linux/version.h"
36 #include "linux/types.h"
37 #include "linux/mtd/mtd.h"
39 /* Don't compile this module if we don't have MTD's mtd_oob_ops interface */
40 #if (MTD_VERSION_CODE > MTD_VERSION(2, 6, 17))
42 #ifndef CONFIG_YAFFS_9BYTE_TAGS
49 /* Use the following nand_ecclayout with MTD when using
50 * CONFIG_YAFFS_9BYTE_TAGS and the older on-NAND tags layout.
51 * If you have existing Yaffs images and the byte order differs from this,
52 * adjust 'oobfree' to match your existing Yaffs data.
54 * This nand_ecclayout scatters/gathers to/from the old-yaffs layout with the
55 * pageStatus byte (at NAND spare offset 4) scattered/gathered from/to
58 * Old-style on-NAND format: T0,T1,T2,T3,P,B,T4,T5,E0,E1,E2,T6,T7,E3,E4,E5
59 * We have/need PackedTags1 plus pageStatus: T0,T1,T2,T3,T4,T5,T6,T7,P
60 * where Tn are the tag bytes, En are MTD's ECC bytes, P is the pageStatus
61 * byte and B is the small-page bad-block indicator byte.
63 static struct nand_ecclayout nand_oob_16 = {
65 .eccpos = { 8, 9, 10, 13, 14, 15 },
67 .oobfree = { { 0, 4 }, { 6, 2 }, { 11, 2 }, { 4, 1 } }
71 /* Write a chunk (page) of data to NAND.
73 * Caller always provides ExtendedTags data which are converted to a more
74 * compact (packed) form for storage in NAND. A mini-ECC runs over the
75 * contents of the tags meta-data; used to valid the tags when read.
77 * - Pack ExtendedTags to PackedTags1 form
78 * - Compute mini-ECC for PackedTags1
79 * - Write data and packed tags to NAND.
81 * Note: Due to the use of the PackedTags1 meta-data which does not include
82 * a full sequence number (as found in the larger PackedTags2 form) it is
83 * necessary for Yaffs to re-write a chunk/page (just once) to mark it as
84 * discarded and dirty. This is not ideal: newer NAND parts are supposed
85 * to be written just once. When Yaffs performs this operation, this
86 * function is called with a NULL data pointer -- calling MTD write_oob
87 * without data is valid usage (2.6.17).
89 * Any underlying MTD error results in YAFFS_FAIL.
90 * Returns YAFFS_OK or YAFFS_FAIL.
92 int nandmtd1_WriteChunkWithTagsToNAND(yaffs_Device *dev,
93 int chunkInNAND, const __u8 *data, const yaffs_ExtendedTags *etags)
95 struct mtd_info *mtd = yaffs_DeviceToMtd(dev);
96 int chunkBytes = dev->nDataBytesPerChunk;
97 loff_t addr = ((loff_t)chunkInNAND) * chunkBytes;
98 struct mtd_oob_ops ops;
99 yaffs_PackedTags1 pt1;
102 /* we assume that PackedTags1 and yaffs_Tags are compatible */
103 compile_time_assertion(sizeof(yaffs_PackedTags1) == 12);
104 compile_time_assertion(sizeof(yaffs_Tags) == 8);
106 yaffs_PackTags1(&pt1, etags);
107 yaffs_CalcTagsECC((yaffs_Tags *)&pt1);
109 /* When deleting a chunk, the upper layer provides only skeletal
110 * etags, one with chunkDeleted set. However, we need to update the
111 * tags, not erase them completely. So we use the NAND write property
112 * that only zeroed-bits stick and set tag bytes to all-ones and
113 * zero just the (not) deleted bit.
115 #ifndef CONFIG_YAFFS_9BYTE_TAGS
116 if (etags->chunkDeleted) {
117 memset(&pt1, 0xff, 8);
118 /* clear delete status bit to indicate deleted */
122 ((__u8 *)&pt1)[8] = 0xff;
123 if (etags->chunkDeleted) {
124 memset(&pt1, 0xff, 8);
125 /* zero pageStatus byte to indicate deleted */
126 ((__u8 *)&pt1)[8] = 0;
130 memset(&ops, 0, sizeof(ops));
131 ops.mode = MTD_OOB_AUTO;
132 ops.len = (data) ? chunkBytes : 0;
133 ops.ooblen = YTAG1_SIZE;
134 ops.datbuf = (__u8 *)data;
135 ops.oobbuf = (__u8 *)&pt1;
137 retval = mtd->write_oob(mtd, addr, &ops);
140 (TSTR("write_oob failed, chunk %d, mtd error %d"TENDSTR),
141 chunkInNAND, retval));
143 return retval ? YAFFS_FAIL : YAFFS_OK;
146 /* Return with empty ExtendedTags but add eccResult.
148 static int rettags(yaffs_ExtendedTags *etags, int eccResult, int retval)
151 memset(etags, 0, sizeof(*etags));
152 etags->eccResult = eccResult;
157 /* Read a chunk (page) from NAND.
159 * Caller expects ExtendedTags data to be usable even on error; that is,
160 * all members except eccResult and blockBad are zeroed.
162 * - Check ECC results for data (if applicable)
163 * - Check for blank/erased block (return empty ExtendedTags if blank)
164 * - Check the PackedTags1 mini-ECC (correct if necessary/possible)
165 * - Convert PackedTags1 to ExtendedTags
166 * - Update eccResult and blockBad members to refect state.
168 * Returns YAFFS_OK or YAFFS_FAIL.
170 int nandmtd1_ReadChunkWithTagsFromNAND(yaffs_Device *dev,
171 int chunkInNAND, __u8 *data, yaffs_ExtendedTags *etags)
173 struct mtd_info *mtd = yaffs_DeviceToMtd(dev);
174 int chunkBytes = dev->nDataBytesPerChunk;
175 loff_t addr = ((loff_t)chunkInNAND) * chunkBytes;
176 int eccres = YAFFS_ECC_RESULT_NO_ERROR;
177 struct mtd_oob_ops ops;
178 yaffs_PackedTags1 pt1;
182 memset(&ops, 0, sizeof(ops));
183 ops.mode = MTD_OOB_AUTO;
184 ops.len = (data) ? chunkBytes : 0;
185 ops.ooblen = YTAG1_SIZE;
187 ops.oobbuf = (__u8 *)&pt1;
189 #if (MTD_VERSION_CODE < MTD_VERSION(2, 6, 20))
190 /* In MTD 2.6.18 to 2.6.19 nand_base.c:nand_do_read_oob() has a bug;
191 * help it out with ops.len = ops.ooblen when ops.datbuf == NULL.
193 ops.len = (ops.datbuf) ? ops.len : ops.ooblen;
195 /* Read page and oob using MTD.
196 * Check status and determine ECC result.
198 retval = mtd->read_oob(mtd, addr, &ops);
201 (TSTR("read_oob failed, chunk %d, mtd error %d"TENDSTR),
202 chunkInNAND, retval));
211 /* MTD's ECC fixed the data */
212 eccres = YAFFS_ECC_RESULT_FIXED;
217 /* MTD's ECC could not fix the data */
221 rettags(etags, YAFFS_ECC_RESULT_UNFIXED, 0);
222 etags->blockBad = (mtd->block_isbad)(mtd, addr);
226 /* Check for a blank/erased chunk.
228 if (yaffs_CheckFF((__u8 *)&pt1, 8)) {
229 /* when blank, upper layers want eccResult to be <= NO_ERROR */
230 return rettags(etags, YAFFS_ECC_RESULT_NO_ERROR, YAFFS_OK);
233 #ifndef CONFIG_YAFFS_9BYTE_TAGS
234 /* Read deleted status (bit) then return it to it's non-deleted
235 * state before performing tags mini-ECC check. pt1.deleted is
238 deleted = !pt1.deleted;
241 deleted = (yaffs_CountBits(((__u8 *)&pt1)[8]) < 7);
244 /* Check the packed tags mini-ECC and correct if necessary/possible.
246 retval = yaffs_CheckECCOnTags((yaffs_Tags *)&pt1);
249 /* no tags error, use MTD result */
252 /* recovered tags-ECC error */
254 if (eccres == YAFFS_ECC_RESULT_NO_ERROR)
255 eccres = YAFFS_ECC_RESULT_FIXED;
258 /* unrecovered tags-ECC error */
259 dev->tagsEccUnfixed++;
260 return rettags(etags, YAFFS_ECC_RESULT_UNFIXED, YAFFS_FAIL);
263 /* Unpack the tags to extended form and set ECC result.
264 * [set shouldBeFF just to keep yaffs_UnpackTags1 happy]
266 pt1.shouldBeFF = 0xFFFFFFFF;
267 yaffs_UnpackTags1(etags, &pt1);
268 etags->eccResult = eccres;
270 /* Set deleted state */
271 etags->chunkDeleted = deleted;
277 * This is a persistant state.
278 * Use of this function should be rare.
280 * Returns YAFFS_OK or YAFFS_FAIL.
282 int nandmtd1_MarkNANDBlockBad(struct yaffs_DeviceStruct *dev, int blockNo)
284 struct mtd_info *mtd = yaffs_DeviceToMtd(dev);
285 int blocksize = dev->param.nChunksPerBlock * dev->nDataBytesPerChunk;
288 T(YAFFS_TRACE_BAD_BLOCKS,(TSTR("marking block %d bad"TENDSTR), blockNo));
290 retval = mtd->block_markbad(mtd, (loff_t)blocksize * blockNo);
291 return (retval) ? YAFFS_FAIL : YAFFS_OK;
294 /* Check any MTD prerequists.
296 * Returns YAFFS_OK or YAFFS_FAIL.
298 static int nandmtd1_TestPrerequists(struct mtd_info *mtd)
300 /* 2.6.18 has mtd->ecclayout->oobavail */
301 /* 2.6.21 has mtd->ecclayout->oobavail and mtd->oobavail */
302 int oobavail = mtd->ecclayout->oobavail;
304 if (oobavail < YTAG1_SIZE) {
306 (TSTR("mtd device has only %d bytes for tags, need %d"TENDSTR),
307 oobavail, YTAG1_SIZE));
313 /* Query for the current state of a specific block.
315 * Examine the tags of the first chunk of the block and return the state:
316 * - YAFFS_BLOCK_STATE_DEAD, the block is marked bad
317 * - YAFFS_BLOCK_STATE_NEEDS_SCANNING, the block is in use
318 * - YAFFS_BLOCK_STATE_EMPTY, the block is clean
320 * Always returns YAFFS_OK.
322 int nandmtd1_QueryNANDBlock(struct yaffs_DeviceStruct *dev, int blockNo,
323 yaffs_BlockState *pState, __u32 *pSequenceNumber)
325 struct mtd_info *mtd = yaffs_DeviceToMtd(dev);
326 int chunkNo = blockNo * dev->param.nChunksPerBlock;
327 loff_t addr = (loff_t)chunkNo * dev->nDataBytesPerChunk;
328 yaffs_ExtendedTags etags;
329 int state = YAFFS_BLOCK_STATE_DEAD;
333 /* We don't yet have a good place to test for MTD config prerequists.
334 * Do it here as we are called during the initial scan.
336 if (nandmtd1_TestPrerequists(mtd) != YAFFS_OK)
339 retval = nandmtd1_ReadChunkWithTagsFromNAND(dev, chunkNo, NULL, &etags);
340 etags.blockBad = (mtd->block_isbad)(mtd, addr);
341 if (etags.blockBad) {
342 T(YAFFS_TRACE_BAD_BLOCKS,
343 (TSTR("block %d is marked bad"TENDSTR), blockNo));
344 state = YAFFS_BLOCK_STATE_DEAD;
345 } else if (etags.eccResult != YAFFS_ECC_RESULT_NO_ERROR) {
346 /* bad tags, need to look more closely */
347 state = YAFFS_BLOCK_STATE_NEEDS_SCANNING;
348 } else if (etags.chunkUsed) {
349 state = YAFFS_BLOCK_STATE_NEEDS_SCANNING;
350 seqnum = etags.sequenceNumber;
352 state = YAFFS_BLOCK_STATE_EMPTY;
356 *pSequenceNumber = seqnum;
358 /* query always succeeds */
362 #endif /*MTD_VERSION*/