2 * YAFFS: Yet another FFS. A NAND-flash specific file system.
3 * yaffs_mtdif1.c NAND mtd interface functions for small-page NAND.
5 * Copyright (C) 2002-2010 Aleph One Ltd.
6 * for Toby Churchill Ltd and Brightstar Engineering
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.
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.
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_tags_tCompatability 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 (use_nand_ecc is true).
27 #include "yaffs_trace.h"
28 #include "yaffs_guts.h"
29 #include "yaffs_packedtags1.h"
30 #include "yaffs_tagscompat.h" /* for yaffs_calc_tags_ecc */
31 #include "yaffs_linux.h"
33 #include "linux/kernel.h"
34 #include "linux/version.h"
35 #include "linux/types.h"
36 #include "linux/mtd/mtd.h"
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))
41 #ifndef CONFIG_YAFFS_9BYTE_TAGS
48 /* Use the following nand_ecclayout with MTD when using
49 * CONFIG_YAFFS_9BYTE_TAGS and the older on-NAND tags layout.
50 * If you have existing Yaffs images and the byte order differs from this,
51 * adjust 'oobfree' to match your existing Yaffs data.
53 * This nand_ecclayout scatters/gathers to/from the old-yaffs layout with the
54 * page_status byte (at NAND spare offset 4) scattered/gathered from/to
57 * Old-style on-NAND format: T0,T1,T2,T3,P,B,T4,T5,E0,E1,E2,T6,T7,E3,E4,E5
58 * We have/need PackedTags1 plus page_status: T0,T1,T2,T3,T4,T5,T6,T7,P
59 * where Tn are the tag bytes, En are MTD's ECC bytes, P is the page_status
60 * byte and B is the small-page bad-block indicator byte.
62 static struct nand_ecclayout nand_oob_16
= {
64 .eccpos
= { 8, 9, 10, 13, 14, 15 },
66 .oobfree
= { { 0, 4 }, { 6, 2 }, { 11, 2 }, { 4, 1 } }
70 /* Write a chunk (page) of data to NAND.
72 * Caller always provides ExtendedTags data which are converted to a more
73 * compact (packed) form for storage in NAND. A mini-ECC runs over the
74 * contents of the tags meta-data; used to valid the tags when read.
76 * - Pack ExtendedTags to PackedTags1 form
77 * - Compute mini-ECC for PackedTags1
78 * - Write data and packed tags to NAND.
80 * Note: Due to the use of the PackedTags1 meta-data which does not include
81 * a full sequence number (as found in the larger PackedTags2 form) it is
82 * necessary for Yaffs to re-write a chunk/page (just once) to mark it as
83 * discarded and dirty. This is not ideal: newer NAND parts are supposed
84 * to be written just once. When Yaffs performs this operation, this
85 * function is called with a NULL data pointer -- calling MTD write_oob
86 * without data is valid usage (2.6.17).
88 * Any underlying MTD error results in YAFFS_FAIL.
89 * Returns YAFFS_OK or YAFFS_FAIL.
91 int nandmtd1_WriteChunkWithTagsToNAND(yaffs_dev_t
*dev
,
92 int nand_chunk
, const __u8
*data
, const yaffs_ext_tags
*etags
)
94 struct mtd_info
*mtd
= yaffs_dev_to_mtd(dev
);
95 int chunkBytes
= dev
->data_bytes_per_chunk
;
96 loff_t addr
= ((loff_t
)nand_chunk
) * chunkBytes
;
97 struct mtd_oob_ops ops
;
98 yaffs_PackedTags1 pt1
;
101 /* we assume that PackedTags1 and yaffs_tags_t are compatible */
102 compile_time_assertion(sizeof(yaffs_PackedTags1
) == 12);
103 compile_time_assertion(sizeof(yaffs_tags_t
) == 8);
105 yaffs_PackTags1(&pt1
, etags
);
106 yaffs_calc_tags_ecc((yaffs_tags_t
*)&pt1
);
108 /* When deleting a chunk, the upper layer provides only skeletal
109 * etags, one with is_deleted set. However, we need to update the
110 * tags, not erase them completely. So we use the NAND write property
111 * that only zeroed-bits stick and set tag bytes to all-ones and
112 * zero just the (not) deleted bit.
114 #ifndef CONFIG_YAFFS_9BYTE_TAGS
115 if (etags
->is_deleted
) {
116 memset(&pt1
, 0xff, 8);
117 /* clear delete status bit to indicate deleted */
121 ((__u8
*)&pt1
)[8] = 0xff;
122 if (etags
->is_deleted
) {
123 memset(&pt1
, 0xff, 8);
124 /* zero page_status byte to indicate deleted */
125 ((__u8
*)&pt1
)[8] = 0;
129 memset(&ops
, 0, sizeof(ops
));
130 ops
.mode
= MTD_OOB_AUTO
;
131 ops
.len
= (data
) ? chunkBytes
: 0;
132 ops
.ooblen
= YTAG1_SIZE
;
133 ops
.datbuf
= (__u8
*)data
;
134 ops
.oobbuf
= (__u8
*)&pt1
;
136 retval
= mtd
->write_oob(mtd
, addr
, &ops
);
139 (TSTR("write_oob failed, chunk %d, mtd error %d"TENDSTR
),
140 nand_chunk
, retval
));
142 return retval
? YAFFS_FAIL
: YAFFS_OK
;
145 /* Return with empty ExtendedTags but add ecc_result.
147 static int rettags(yaffs_ext_tags
*etags
, int ecc_result
, int retval
)
150 memset(etags
, 0, sizeof(*etags
));
151 etags
->ecc_result
= ecc_result
;
156 /* Read a chunk (page) from NAND.
158 * Caller expects ExtendedTags data to be usable even on error; that is,
159 * all members except ecc_result and block_bad are zeroed.
161 * - Check ECC results for data (if applicable)
162 * - Check for blank/erased block (return empty ExtendedTags if blank)
163 * - Check the PackedTags1 mini-ECC (correct if necessary/possible)
164 * - Convert PackedTags1 to ExtendedTags
165 * - Update ecc_result and block_bad members to refect state.
167 * Returns YAFFS_OK or YAFFS_FAIL.
169 int nandmtd1_ReadChunkWithTagsFromNAND(yaffs_dev_t
*dev
,
170 int nand_chunk
, __u8
*data
, yaffs_ext_tags
*etags
)
172 struct mtd_info
*mtd
= yaffs_dev_to_mtd(dev
);
173 int chunkBytes
= dev
->data_bytes_per_chunk
;
174 loff_t addr
= ((loff_t
)nand_chunk
) * chunkBytes
;
175 int eccres
= YAFFS_ECC_RESULT_NO_ERROR
;
176 struct mtd_oob_ops ops
;
177 yaffs_PackedTags1 pt1
;
181 memset(&ops
, 0, sizeof(ops
));
182 ops
.mode
= MTD_OOB_AUTO
;
183 ops
.len
= (data
) ? chunkBytes
: 0;
184 ops
.ooblen
= YTAG1_SIZE
;
186 ops
.oobbuf
= (__u8
*)&pt1
;
188 #if (MTD_VERSION_CODE < MTD_VERSION(2, 6, 20))
189 /* In MTD 2.6.18 to 2.6.19 nand_base.c:nand_do_read_oob() has a bug;
190 * help it out with ops.len = ops.ooblen when ops.datbuf == NULL.
192 ops
.len
= (ops
.datbuf
) ? ops
.len
: ops
.ooblen
;
194 /* Read page and oob using MTD.
195 * Check status and determine ECC result.
197 retval
= mtd
->read_oob(mtd
, addr
, &ops
);
200 (TSTR("read_oob failed, chunk %d, mtd error %d"TENDSTR
),
201 nand_chunk
, retval
));
210 /* MTD's ECC fixed the data */
211 eccres
= YAFFS_ECC_RESULT_FIXED
;
216 /* MTD's ECC could not fix the data */
217 dev
->n_ecc_unfixed
++;
220 rettags(etags
, YAFFS_ECC_RESULT_UNFIXED
, 0);
221 etags
->block_bad
= (mtd
->block_isbad
)(mtd
, addr
);
225 /* Check for a blank/erased chunk.
227 if (yaffs_check_ff((__u8
*)&pt1
, 8)) {
228 /* when blank, upper layers want ecc_result to be <= NO_ERROR */
229 return rettags(etags
, YAFFS_ECC_RESULT_NO_ERROR
, YAFFS_OK
);
232 #ifndef CONFIG_YAFFS_9BYTE_TAGS
233 /* Read deleted status (bit) then return it to it's non-deleted
234 * state before performing tags mini-ECC check. pt1.deleted is
237 deleted
= !pt1
.deleted
;
240 deleted
= (yaffs_count_bits(((__u8
*)&pt1
)[8]) < 7);
243 /* Check the packed tags mini-ECC and correct if necessary/possible.
245 retval
= yaffs_check_tags_ecc((yaffs_tags_t
*)&pt1
);
248 /* no tags error, use MTD result */
251 /* recovered tags-ECC error */
252 dev
->n_tags_ecc_fixed
++;
253 if (eccres
== YAFFS_ECC_RESULT_NO_ERROR
)
254 eccres
= YAFFS_ECC_RESULT_FIXED
;
257 /* unrecovered tags-ECC error */
258 dev
->n_tags_ecc_unfixed
++;
259 return rettags(etags
, YAFFS_ECC_RESULT_UNFIXED
, YAFFS_FAIL
);
262 /* Unpack the tags to extended form and set ECC result.
263 * [set shouldBeFF just to keep yaffs_unpack_tags1 happy]
265 pt1
.shouldBeFF
= 0xFFFFFFFF;
266 yaffs_unpack_tags1(etags
, &pt1
);
267 etags
->ecc_result
= eccres
;
269 /* Set deleted state */
270 etags
->is_deleted
= deleted
;
276 * This is a persistant state.
277 * Use of this function should be rare.
279 * Returns YAFFS_OK or YAFFS_FAIL.
281 int nandmtd1_MarkNANDBlockBad(struct yaffs_dev_s
*dev
, int block_no
)
283 struct mtd_info
*mtd
= yaffs_dev_to_mtd(dev
);
284 int blocksize
= dev
->param
.chunks_per_block
* dev
->data_bytes_per_chunk
;
287 T(YAFFS_TRACE_BAD_BLOCKS
,(TSTR("marking block %d bad"TENDSTR
), block_no
));
289 retval
= mtd
->block_markbad(mtd
, (loff_t
)blocksize
* block_no
);
290 return (retval
) ? YAFFS_FAIL
: YAFFS_OK
;
293 /* Check any MTD prerequists.
295 * Returns YAFFS_OK or YAFFS_FAIL.
297 static int nandmtd1_TestPrerequists(struct mtd_info
*mtd
)
299 /* 2.6.18 has mtd->ecclayout->oobavail */
300 /* 2.6.21 has mtd->ecclayout->oobavail and mtd->oobavail */
301 int oobavail
= mtd
->ecclayout
->oobavail
;
303 if (oobavail
< YTAG1_SIZE
) {
305 (TSTR("mtd device has only %d bytes for tags, need %d"TENDSTR
),
306 oobavail
, YTAG1_SIZE
));
312 /* Query for the current state of a specific block.
314 * Examine the tags of the first chunk of the block and return the state:
315 * - YAFFS_BLOCK_STATE_DEAD, the block is marked bad
316 * - YAFFS_BLOCK_STATE_NEEDS_SCANNING, the block is in use
317 * - YAFFS_BLOCK_STATE_EMPTY, the block is clean
319 * Always returns YAFFS_OK.
321 int nandmtd1_QueryNANDBlock(struct yaffs_dev_s
*dev
, int block_no
,
322 yaffs_block_state_t
*pState
, __u32
*pSequenceNumber
)
324 struct mtd_info
*mtd
= yaffs_dev_to_mtd(dev
);
325 int chunkNo
= block_no
* dev
->param
.chunks_per_block
;
326 loff_t addr
= (loff_t
)chunkNo
* dev
->data_bytes_per_chunk
;
327 yaffs_ext_tags etags
;
328 int state
= YAFFS_BLOCK_STATE_DEAD
;
332 /* We don't yet have a good place to test for MTD config prerequists.
333 * Do it here as we are called during the initial scan.
335 if (nandmtd1_TestPrerequists(mtd
) != YAFFS_OK
)
338 retval
= nandmtd1_ReadChunkWithTagsFromNAND(dev
, chunkNo
, NULL
, &etags
);
339 etags
.block_bad
= (mtd
->block_isbad
)(mtd
, addr
);
340 if (etags
.block_bad
) {
341 T(YAFFS_TRACE_BAD_BLOCKS
,
342 (TSTR("block %d is marked bad"TENDSTR
), block_no
));
343 state
= YAFFS_BLOCK_STATE_DEAD
;
344 } else if (etags
.ecc_result
!= YAFFS_ECC_RESULT_NO_ERROR
) {
345 /* bad tags, need to look more closely */
346 state
= YAFFS_BLOCK_STATE_NEEDS_SCANNING
;
347 } else if (etags
.chunk_used
) {
348 state
= YAFFS_BLOCK_STATE_NEEDS_SCANNING
;
349 seqnum
= etags
.seq_number
;
351 state
= YAFFS_BLOCK_STATE_EMPTY
;
355 *pSequenceNumber
= seqnum
;
357 /* query always succeeds */
361 #endif /*MTD_VERSION*/