yaffs direct: Add ELOOP definition
[yaffs2.git] / yaffs_mtdif1_multi.c
1 /*
2  * YAFFS: Yet another FFS. A NAND-flash specific file system.
3  *
4  * Copyright (C) 2002-2010 Aleph One Ltd.
5  *   for Toby Churchill Ltd and Brightstar Engineering
6  *
7  * Created by Charles Manning <charles@aleph1.co.uk>
8  *
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.
12  */
13
14 /*
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.
19  *
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_tags compatability 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 (use_nand_ecc is true).
25  */
26
27 #include "yportenv.h"
28 #include "yaffs_trace.h"
29 #include "yaffs_guts.h"
30 #include "yaffs_packedtags1.h"
31 #include "yaffs_tagscompat.h"   /* for yaffs_calc_tags_ecc */
32 #include "yaffs_linux.h"
33
34 #include "linux/kernel.h"
35 #include "linux/version.h"
36 #include "linux/types.h"
37 #include "linux/mtd/mtd.h"
38
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))
41
42 #ifndef CONFIG_YAFFS_9BYTE_TAGS
43 # define YTAG1_SIZE 8
44 #else
45 # define YTAG1_SIZE 9
46 #endif
47
48 #if 0
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.
53  *
54  * This nand_ecclayout scatters/gathers to/from the old-yaffs layout with the
55  * page_status byte (at NAND spare offset 4) scattered/gathered from/to
56  * the 9th byte.
57  *
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 packed_tags1 plus page_status: 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 page_status
61  * byte and B is the small-page bad-block indicator byte.
62  */
63 static struct nand_ecclayout nand_oob_16 = {
64         .eccbytes = 6,
65         .eccpos = {8, 9, 10, 13, 14, 15},
66         .oobavail = 9,
67         .oobfree = {{0, 4}, {6, 2}, {11, 2}, {4, 1}}
68 };
69 #endif
70
71 /* Write a chunk (page) of data to NAND.
72  *
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.
76  *
77  *  - Pack ExtendedTags to packed_tags1 form
78  *  - Compute mini-ECC for packed_tags1
79  *  - Write data and packed tags to NAND.
80  *
81  * Note: Due to the use of the packed_tags1 meta-data which does not include
82  * a full sequence number (as found in the larger packed_tags2 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).
88  *
89  * Any underlying MTD error results in YAFFS_FAIL.
90  * Returns YAFFS_OK or YAFFS_FAIL.
91  */
92 int nandmtd1_write_chunk_tags(struct yaffs_dev *dev,
93                               int nand_chunk, const u8 * data,
94                               const struct yaffs_ext_tags *etags)
95 {
96         struct mtd_info *mtd = yaffs_dev_to_mtd(dev);
97         int chunk_bytes = dev->data_bytes_per_chunk;
98         loff_t addr = ((loff_t) nand_chunk) * chunk_bytes;
99         struct mtd_oob_ops ops;
100         struct yaffs_packed_tags1 pt1;
101         int retval;
102
103         /* we assume that packed_tags1 and struct yaffs_tags are compatible */
104         compile_time_assertion(sizeof(struct yaffs_packed_tags1) == 12);
105         compile_time_assertion(sizeof(struct yaffs_tags) == 8);
106
107         yaffs_pack_tags1(&pt1, etags);
108         yaffs_calc_tags_ecc((struct yaffs_tags *)&pt1);
109
110         /* When deleting a chunk, the upper layer provides only skeletal
111          * etags, one with is_deleted 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->is_deleted) {
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->is_deleted) {
125                 memset(&pt1, 0xff, 8);
126                 /* zero page_status 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) ? chunk_bytes : 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                 T(YAFFS_TRACE_MTD,
141                   (TSTR("write_oob failed, chunk %d, mtd error %d" TENDSTR),
142                    nand_chunk, retval));
143         }
144         return retval ? YAFFS_FAIL : YAFFS_OK;
145 }
146
147 /* Return with empty ExtendedTags but add ecc_result.
148  */
149 static int rettags(struct yaffs_ext_tags *etags, int ecc_result, int retval)
150 {
151         if (etags) {
152                 memset(etags, 0, sizeof(*etags));
153                 etags->ecc_result = ecc_result;
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 ecc_result and block_bad 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 packed_tags1 mini-ECC (correct if necessary/possible)
166  *  - Convert packed_tags1 to ExtendedTags
167  *  - Update ecc_result and block_bad members to refect state.
168  *
169  * Returns YAFFS_OK or YAFFS_FAIL.
170  */
171 int nandmtd1_read_chunk_tags(struct yaffs_dev *dev,
172                              int nand_chunk, u8 * data,
173                              struct yaffs_ext_tags *etags)
174 {
175         struct mtd_info *mtd = yaffs_dev_to_mtd(dev);
176         int chunk_bytes = dev->data_bytes_per_chunk;
177         loff_t addr = ((loff_t) nand_chunk) * chunk_bytes;
178         int eccres = YAFFS_ECC_RESULT_NO_ERROR;
179         struct mtd_oob_ops ops;
180         struct yaffs_packed_tags1 pt1;
181         int retval;
182         int deleted;
183
184         memset(&ops, 0, sizeof(ops));
185         ops.mode = MTD_OOB_AUTO;
186         ops.len = (data) ? chunk_bytes : 0;
187         ops.ooblen = YTAG1_SIZE;
188         ops.datbuf = data;
189         ops.oobbuf = (u8 *) & pt1;
190
191 #if (MTD_VERSION_CODE < MTD_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                 T(YAFFS_TRACE_MTD,
203                   (TSTR("read_oob failed, chunk %d, mtd error %d" TENDSTR),
204                    nand_chunk, 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->n_ecc_fixed++;
216                 break;
217
218         case -EBADMSG:
219                 /* MTD's ECC could not fix the data */
220                 dev->n_ecc_unfixed++;
221                 /* fall into... */
222         default:
223                 rettags(etags, YAFFS_ECC_RESULT_UNFIXED, 0);
224                 etags->block_bad = (mtd->block_isbad) (mtd, addr);
225                 return YAFFS_FAIL;
226         }
227
228         /* Check for a blank/erased chunk.
229          */
230         if (yaffs_check_ff((u8 *) & pt1, 8)) {
231                 /* when blank, upper layers want ecc_result to be <= NO_ERROR */
232                 return rettags(etags, YAFFS_ECC_RESULT_NO_ERROR, YAFFS_OK);
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_count_bits(((u8 *) & pt1)[8]) < 7);
243 #endif
244
245         /* Check the packed tags mini-ECC and correct if necessary/possible.
246          */
247         retval = yaffs_check_tags_ecc((struct 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->n_tags_ecc_fixed++;
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->n_tags_ecc_unfixed++;
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 should_be_ff just to keep yaffs_unpack_tags1 happy]
266          */
267         pt1.should_be_ff = 0xFFFFFFFF;
268         yaffs_unpack_tags1(etags, &pt1);
269         etags->ecc_result = eccres;
270
271         /* Set deleted state */
272         etags->is_deleted = 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_mark_block_bad(struct yaffs_dev *dev, int block_no)
284 {
285         struct mtd_info *mtd = yaffs_dev_to_mtd(dev);
286         int blocksize = dev->param.chunks_per_block * dev->data_bytes_per_chunk;
287         int retval;
288
289         T(YAFFS_TRACE_BAD_BLOCKS,
290           (TSTR("marking block %d bad" TENDSTR), block_no));
291
292         retval = mtd->block_markbad(mtd, (loff_t) blocksize * block_no);
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_test_prerequists(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                 T(YAFFS_TRACE_ERROR,
308                   (TSTR
309                    ("mtd device has only %d bytes for tags, need %d" TENDSTR),
310                    oobavail, YTAG1_SIZE));
311                 return YAFFS_FAIL;
312         }
313         return YAFFS_OK;
314 }
315
316 /* Query for the current state of a specific block.
317  *
318  * Examine the tags of the first chunk of the block and return the state:
319  *  - YAFFS_BLOCK_STATE_DEAD, the block is marked bad
320  *  - YAFFS_BLOCK_STATE_NEEDS_SCANNING, the block is in use
321  *  - YAFFS_BLOCK_STATE_EMPTY, the block is clean
322  *
323  * Always returns YAFFS_OK.
324  */
325 int nandmtd1_query_block(struct yaffs_dev *dev, int block_no,
326                          enum yaffs_block_state *state_ptr, u32 * seq_ptr)
327 {
328         struct mtd_info *mtd = yaffs_dev_to_mtd(dev);
329         int chunk_num = block_no * dev->param.chunks_per_block;
330         loff_t addr = (loff_t) chunk_num * dev->data_bytes_per_chunk;
331         struct yaffs_ext_tags etags;
332         int state = YAFFS_BLOCK_STATE_DEAD;
333         int seqnum = 0;
334         int retval;
335
336         /* We don't yet have a good place to test for MTD config prerequists.
337          * Do it here as we are called during the initial scan.
338          */
339         if (nandmtd1_test_prerequists(mtd) != YAFFS_OK)
340                 return YAFFS_FAIL;
341
342         retval = nandmtd1_read_chunk_tags(dev, chunk_num, NULL, &etags);
343         etags.block_bad = (mtd->block_isbad) (mtd, addr);
344         if (etags.block_bad) {
345                 T(YAFFS_TRACE_BAD_BLOCKS,
346                   (TSTR("block %d is marked bad" TENDSTR), block_no));
347                 state = YAFFS_BLOCK_STATE_DEAD;
348         } else if (etags.ecc_result != YAFFS_ECC_RESULT_NO_ERROR) {
349                 /* bad tags, need to look more closely */
350                 state = YAFFS_BLOCK_STATE_NEEDS_SCANNING;
351         } else if (etags.chunk_used) {
352                 state = YAFFS_BLOCK_STATE_NEEDS_SCANNING;
353                 seqnum = etags.seq_number;
354         } else {
355                 state = YAFFS_BLOCK_STATE_EMPTY;
356         }
357
358         *state_ptr = state;
359         *seq_ptr = seqnum;
360
361         /* query always succeeds */
362         return YAFFS_OK;
363 }
364
365 #endif /*MTD_VERSION */