get rid of $Id$ - it has never helped us and it has broken too many patches ;)
[openwrt.git] / target / linux / generic-2.6 / files / fs / yaffs2 / yaffs_mtdif1.c
1 /*
2  * YAFFS: Yet another FFS. A NAND-flash specific file system.
3  * yaffs_mtdif1.c  NAND mtd interface functions for small-page NAND.
4  *
5  * Copyright (C) 2002 Aleph One Ltd.
6  *   for Toby Churchill Ltd and Brightstar Engineering
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License version 2 as
10  * published by the Free Software Foundation.
11  */
12
13 /*
14  * This module provides the interface between yaffs_nand.c and the
15  * MTD API.  This version is used when the MTD interface supports the
16  * 'mtd_oob_ops' style calls to read_oob and write_oob, circa 2.6.17,
17  * and we have small-page NAND device.
18  *
19  * These functions are invoked via function pointers in yaffs_nand.c.
20  * This replaces functionality provided by functions in yaffs_mtdif.c
21  * and the yaffs_TagsCompatability functions in yaffs_tagscompat.c that are
22  * called in yaffs_mtdif.c when the function pointers are NULL.
23  * We assume the MTD layer is performing ECC (useNANDECC is true).
24  */
25
26 #include "yportenv.h"
27 #include "yaffs_guts.h"
28 #include "yaffs_packedtags1.h"
29 #include "yaffs_tagscompat.h"   // for yaffs_CalcTagsECC
30
31 #include "linux/kernel.h"
32 #include "linux/version.h"
33 #include "linux/types.h"
34 #include "linux/mtd/mtd.h"
35
36 /* Don't compile this module if we don't have MTD's mtd_oob_ops interface */
37 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,17))
38
39
40 #ifndef CONFIG_YAFFS_9BYTE_TAGS
41 # define YTAG1_SIZE 8
42 #else
43 # define YTAG1_SIZE 9
44 #endif
45
46 #if 0
47 /* Use the following nand_ecclayout with MTD when using
48  * CONFIG_YAFFS_9BYTE_TAGS and the older on-NAND tags layout.
49  * If you have existing Yaffs images and the byte order differs from this,
50  * adjust 'oobfree' to match your existing Yaffs data.
51  *
52  * This nand_ecclayout scatters/gathers to/from the old-yaffs layout with the
53  * pageStatus byte (at NAND spare offset 4) scattered/gathered from/to
54  * the 9th byte.
55  *
56  * Old-style on-NAND format: T0,T1,T2,T3,P,B,T4,T5,E0,E1,E2,T6,T7,E3,E4,E5
57  * We have/need PackedTags1 plus pageStatus: T0,T1,T2,T3,T4,T5,T6,T7,P
58  * where Tn are the tag bytes, En are MTD's ECC bytes, P is the pageStatus
59  * byte and B is the small-page bad-block indicator byte.
60  */
61 static struct nand_ecclayout nand_oob_16 = {
62         .eccbytes = 6,
63         .eccpos = { 8, 9, 10, 13, 14, 15 },
64         .oobavail = 9,
65         .oobfree = { { 0, 4 }, { 6, 2 }, { 11, 2 }, { 4, 1 } }
66 };
67 #endif
68
69 /* Write a chunk (page) of data to NAND.
70  *
71  * Caller always provides ExtendedTags data which are converted to a more
72  * compact (packed) form for storage in NAND.  A mini-ECC runs over the
73  * contents of the tags meta-data; used to valid the tags when read.
74  *
75  *  - Pack ExtendedTags to PackedTags1 form
76  *  - Compute mini-ECC for PackedTags1
77  *  - Write data and packed tags to NAND.
78  *
79  * Note: Due to the use of the PackedTags1 meta-data which does not include
80  * a full sequence number (as found in the larger PackedTags2 form) it is
81  * necessary for Yaffs to re-write a chunk/page (just once) to mark it as
82  * discarded and dirty.  This is not ideal: newer NAND parts are supposed
83  * to be written just once.  When Yaffs performs this operation, this
84  * function is called with a NULL data pointer -- calling MTD write_oob
85  * without data is valid usage (2.6.17).
86  *
87  * Any underlying MTD error results in YAFFS_FAIL.
88  * Returns YAFFS_OK or YAFFS_FAIL.
89  */
90 int nandmtd1_WriteChunkWithTagsToNAND(yaffs_Device *dev,
91         int chunkInNAND, const __u8 * data, const yaffs_ExtendedTags * etags)
92 {
93         struct mtd_info * mtd = dev->genericDevice;
94         int chunkBytes = dev->nDataBytesPerChunk;
95         loff_t addr = ((loff_t)chunkInNAND) * chunkBytes;
96         struct mtd_oob_ops ops;
97         yaffs_PackedTags1 pt1;
98         int retval;
99
100         /* we assume that PackedTags1 and yaffs_Tags are compatible */
101         compile_time_assertion(sizeof(yaffs_PackedTags1) == 12);
102         compile_time_assertion(sizeof(yaffs_Tags) == 8);
103
104         dev->nPageWrites++;
105
106         yaffs_PackTags1(&pt1, etags);
107         yaffs_CalcTagsECC((yaffs_Tags *)&pt1);
108
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.
114          */
115 #ifndef CONFIG_YAFFS_9BYTE_TAGS
116         if (etags->chunkDeleted) {
117                 memset(&pt1, 0xff, 8);
118                 /* clear delete status bit to indicate deleted */
119                 pt1.deleted = 0;
120         }
121 #else
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;
127         }
128 #endif
129
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;
136
137         retval = mtd->write_oob(mtd, addr, &ops);
138         if (retval) {
139                 yaffs_trace(YAFFS_TRACE_MTD,
140                         "write_oob failed, chunk %d, mtd error %d\n",
141                         chunkInNAND, retval);
142         }
143         return retval ? YAFFS_FAIL : YAFFS_OK;
144 }
145
146 /* Return with empty ExtendedTags but add eccResult.
147  */
148 static int rettags(yaffs_ExtendedTags * etags, int eccResult, int retval)
149 {
150         if (etags) {
151                 memset(etags, 0, sizeof(*etags));
152                 etags->eccResult = eccResult;
153         }
154         return retval;
155 }
156
157 /* Read a chunk (page) from NAND.
158  *
159  * Caller expects ExtendedTags data to be usable even on error; that is,
160  * all members except eccResult and blockBad are zeroed.
161  *
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.
167  *
168  * Returns YAFFS_OK or YAFFS_FAIL.
169  */
170 int nandmtd1_ReadChunkWithTagsFromNAND(yaffs_Device *dev,
171         int chunkInNAND, __u8 * data, yaffs_ExtendedTags * etags)
172 {
173         struct mtd_info * mtd = dev->genericDevice;
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;
179         int retval;
180         int deleted;
181
182         dev->nPageReads++;
183
184         memset(&ops, 0, sizeof(ops));
185         ops.mode = MTD_OOB_AUTO;
186         ops.len = (data) ? chunkBytes : 0;
187         ops.ooblen = YTAG1_SIZE;
188         ops.datbuf = data;
189         ops.oobbuf = (__u8 *)&pt1;
190
191 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,6,20))
192         /* In MTD 2.6.18 to 2.6.19 nand_base.c:nand_do_read_oob() has a bug;
193          * help it out with ops.len = ops.ooblen when ops.datbuf == NULL.
194          */
195         ops.len = (ops.datbuf) ? ops.len : ops.ooblen;
196 #endif
197         /* Read page and oob using MTD.
198          * Check status and determine ECC result.
199          */
200         retval = mtd->read_oob(mtd, addr, &ops);
201         if (retval) {
202                 yaffs_trace(YAFFS_TRACE_MTD,
203                         "read_oob failed, chunk %d, mtd error %d\n",
204                         chunkInNAND, retval);
205         }
206
207         switch (retval) {
208         case 0:
209                 /* no error */
210                 break;
211
212         case -EUCLEAN:
213                 /* MTD's ECC fixed the data */
214                 eccres = YAFFS_ECC_RESULT_FIXED;
215                 dev->eccFixed++;
216                 break;
217
218         case -EBADMSG:
219                 /* MTD's ECC could not fix the data */
220                 dev->eccUnfixed++;
221                 /* fall into... */
222         default:
223                 rettags(etags, YAFFS_ECC_RESULT_UNFIXED, 0);
224                 etags->blockBad = (mtd->block_isbad)(mtd, addr);
225                 return YAFFS_FAIL;
226         }
227
228         /* Check for a blank/erased chunk.
229          */
230         if (yaffs_CheckFF((__u8 *)&pt1, 8)) {
231                 /* when blank, upper layers want eccResult to be <= NO_ERROR */
232                 return rettags(etags, YAFFS_ECC_RESULT_NO_ERROR, YAFFS_OK);
233         }
234
235 #ifndef CONFIG_YAFFS_9BYTE_TAGS
236         /* Read deleted status (bit) then return it to it's non-deleted
237          * state before performing tags mini-ECC check. pt1.deleted is
238          * inverted.
239          */
240         deleted = !pt1.deleted;
241         pt1.deleted = 1;
242 #else
243         deleted = (yaffs_CountBits(((__u8 *)&pt1)[8]) < 7);
244 #endif
245
246         /* Check the packed tags mini-ECC and correct if necessary/possible.
247          */
248         retval = yaffs_CheckECCOnTags((yaffs_Tags *)&pt1);
249         switch (retval) {
250         case 0:
251                 /* no tags error, use MTD result */
252                 break;
253         case 1:
254                 /* recovered tags-ECC error */
255                 dev->tagsEccFixed++;
256                 if (eccres == YAFFS_ECC_RESULT_NO_ERROR)
257                         eccres = YAFFS_ECC_RESULT_FIXED;
258                 break;
259         default:
260                 /* unrecovered tags-ECC error */
261                 dev->tagsEccUnfixed++;
262                 return rettags(etags, YAFFS_ECC_RESULT_UNFIXED, YAFFS_FAIL);
263         }
264
265         /* Unpack the tags to extended form and set ECC result.
266          * [set shouldBeFF just to keep yaffs_UnpackTags1 happy]
267          */
268         pt1.shouldBeFF = 0xFFFFFFFF;
269         yaffs_UnpackTags1(etags, &pt1);
270         etags->eccResult = eccres;
271
272         /* Set deleted state */
273         etags->chunkDeleted = deleted;
274         return YAFFS_OK;
275 }
276
277 /* Mark a block bad.
278  *
279  * This is a persistant state.
280  * Use of this function should be rare.
281  *
282  * Returns YAFFS_OK or YAFFS_FAIL.
283  */
284 int nandmtd1_MarkNANDBlockBad(struct yaffs_DeviceStruct *dev, int blockNo)
285 {
286         struct mtd_info * mtd = dev->genericDevice;
287         int blocksize = dev->nChunksPerBlock * dev->nDataBytesPerChunk;
288         int retval;
289
290         yaffs_trace(YAFFS_TRACE_BAD_BLOCKS, "marking block %d bad", blockNo);
291
292         retval = mtd->block_markbad(mtd, (loff_t)blocksize * blockNo);
293         return (retval) ? YAFFS_FAIL : YAFFS_OK;
294 }
295
296 /* Check any MTD prerequists.
297  *
298  * Returns YAFFS_OK or YAFFS_FAIL.
299  */
300 static int nandmtd1_TestPrerequists(struct mtd_info * mtd)
301 {
302         /* 2.6.18 has mtd->ecclayout->oobavail */
303         /* 2.6.21 has mtd->ecclayout->oobavail and mtd->oobavail */
304         int oobavail = mtd->ecclayout->oobavail;
305
306         if (oobavail < YTAG1_SIZE) {
307                 yaffs_trace(YAFFS_TRACE_ERROR,
308                         "mtd device has only %d bytes for tags, need %d\n",
309                         oobavail, YTAG1_SIZE);
310                 return YAFFS_FAIL;
311         }
312         return YAFFS_OK;
313 }
314
315 /* Query for the current state of a specific block.
316  *
317  * Examine the tags of the first chunk of the block and return the state:
318  *  - YAFFS_BLOCK_STATE_DEAD, the block is marked bad
319  *  - YAFFS_BLOCK_STATE_NEEDS_SCANNING, the block is in use
320  *  - YAFFS_BLOCK_STATE_EMPTY, the block is clean
321  *
322  * Always returns YAFFS_OK.
323  */
324 int nandmtd1_QueryNANDBlock(struct yaffs_DeviceStruct *dev, int blockNo,
325         yaffs_BlockState * pState, int *pSequenceNumber)
326 {
327         struct mtd_info * mtd = dev->genericDevice;
328         int chunkNo = blockNo * dev->nChunksPerBlock;
329         yaffs_ExtendedTags etags;
330         int state = YAFFS_BLOCK_STATE_DEAD;
331         int seqnum = 0;
332         int retval;
333
334         /* We don't yet have a good place to test for MTD config prerequists.
335          * Do it here as we are called during the initial scan.
336          */
337         if (nandmtd1_TestPrerequists(mtd) != YAFFS_OK) {
338                 return YAFFS_FAIL;
339         }
340
341         retval = nandmtd1_ReadChunkWithTagsFromNAND(dev, chunkNo, NULL, &etags);
342         if (etags.blockBad) {
343                 yaffs_trace(YAFFS_TRACE_BAD_BLOCKS,
344                         "block %d is marked bad", blockNo);
345                 state = YAFFS_BLOCK_STATE_DEAD;
346         }
347         else if (etags.chunkUsed) {
348                 state = YAFFS_BLOCK_STATE_NEEDS_SCANNING;
349                 seqnum = etags.sequenceNumber;
350         }
351         else {
352                 state = YAFFS_BLOCK_STATE_EMPTY;
353         }
354
355         *pState = state;
356         *pSequenceNumber = seqnum;
357
358         /* query always succeeds */
359         return YAFFS_OK;
360 }
361
362 #endif /*KERNEL_VERSION*/