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