|
179
|
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 const char *yaffs_mtdif1_c_version = "$Id: yaffs_mtdif1.c,v 1.1 2007-07-18 19:40:38 charles Exp $"; |
|
|
40 |
|
|
41 #ifndef CONFIG_YAFFS_9BYTE_TAGS |
|
|
42 # define YTAG1_SIZE 8 |
|
|
43 #else |
|
|
44 # define YTAG1_SIZE 9 |
|
|
45 #endif |
|
|
46 |
|
|
47 #if 0 |
|
|
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. |
|
|
52 * |
|
|
53 * This nand_ecclayout scatters/gathers to/from the old-yaffs layout with the |
|
|
54 * pageStatus byte (at NAND spare offset 4) scattered/gathered from/to |
|
|
55 * the 9th byte. |
|
|
56 * |
|
|
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 pageStatus: 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 pageStatus |
|
|
60 * byte and B is the small-page bad-block indicator byte. |
|
|
61 */ |
|
|
62 static struct nand_ecclayout nand_oob_16 = { |
|
|
63 .eccbytes = 6, |
|
|
64 .eccpos = { 8, 9, 10, 13, 14, 15 }, |
|
|
65 .oobavail = 9, |
|
|
66 .oobfree = { { 0, 4 }, { 6, 2 }, { 11, 2 }, { 4, 1 } } |
|
|
67 }; |
|
|
68 #endif |
|
|
69 |
|
|
70 /* Write a chunk (page) of data to NAND. |
|
|
71 * |
|
|
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. |
|
|
75 * |
|
|
76 * - Pack ExtendedTags to PackedTags1 form |
|
|
77 * - Compute mini-ECC for PackedTags1 |
|
|
78 * - Write data and packed tags to NAND. |
|
|
79 * |
|
|
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). |
|
|
87 * |
|
|
88 * Any underlying MTD error results in YAFFS_FAIL. |
|
|
89 * Returns YAFFS_OK or YAFFS_FAIL. |
|
|
90 */ |
|
|
91 int nandmtd1_WriteChunkWithTagsToNAND(yaffs_Device *dev, |
|
|
92 int chunkInNAND, const __u8 * data, const yaffs_ExtendedTags * etags) |
|
|
93 { |
|
|
94 struct mtd_info * mtd = dev->genericDevice; |
|
|
95 int chunkBytes = dev->nDataBytesPerChunk; |
|
|
96 loff_t addr = ((loff_t)chunkInNAND) * chunkBytes; |
|
|
97 struct mtd_oob_ops ops; |
|
|
98 yaffs_PackedTags1 pt1; |
|
|
99 int retval; |
|
|
100 |
|
|
101 /* we assume that PackedTags1 and yaffs_Tags are compatible */ |
|
|
102 compile_time_assertion(sizeof(yaffs_PackedTags1) == 12); |
|
|
103 compile_time_assertion(sizeof(yaffs_Tags) == 8); |
|
|
104 |
|
|
105 yaffs_PackTags1(&pt1, etags); |
|
|
106 yaffs_CalcTagsECC((yaffs_Tags *)&pt1); |
|
|
107 |
|
|
108 /* When deleting a chunk, the upper layer provides only skeletal |
|
|
109 * etags, one with chunkDeleted 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. |
|
|
113 */ |
|
|
114 #ifndef CONFIG_YAFFS_9BYTE_TAGS |
|
|
115 if (etags->chunkDeleted) { |
|
|
116 memset(&pt1, 0xff, 8); |
|
|
117 /* clear delete status bit to indicate deleted */ |
|
|
118 pt1.deleted = 0; |
|
|
119 } |
|
|
120 #else |
|
|
121 ((__u8 *)&pt1)[8] = 0xff; |
|
|
122 if (etags->chunkDeleted) { |
|
|
123 memset(&pt1, 0xff, 8); |
|
|
124 /* zero pageStatus byte to indicate deleted */ |
|
|
125 ((__u8 *)&pt1)[8] = 0; |
|
|
126 } |
|
|
127 #endif |
|
|
128 |
|
|
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; |
|
|
135 |
|
|
136 retval = mtd->write_oob(mtd, addr, &ops); |
|
|
137 if (retval) { |
|
|
138 yaffs_trace(YAFFS_TRACE_MTD, |
|
|
139 "write_oob failed, chunk %d, mtd error %d\n", |
|
|
140 chunkInNAND, retval); |
|
|
141 } |
|
|
142 return retval ? YAFFS_FAIL : YAFFS_OK; |
|
|
143 } |
|
|
144 |
|
|
145 /* Return with empty ExtendedTags but add eccResult. |
|
|
146 */ |
|
|
147 static int rettags(yaffs_ExtendedTags * etags, int eccResult, int retval) |
|
|
148 { |
|
|
149 if (etags) { |
|
|
150 memset(etags, 0, sizeof(*etags)); |
|
|
151 etags->eccResult = eccResult; |
|
|
152 } |
|
|
153 return retval; |
|
|
154 } |
|
|
155 |
|
|
156 /* Read a chunk (page) from NAND. |
|
|
157 * |
|
|
158 * Caller expects ExtendedTags data to be usable even on error; that is, |
|
|
159 * all members except eccResult and blockBad are zeroed. |
|
|
160 * |
|
|
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 eccResult and blockBad members to refect state. |
|
|
166 * |
|
|
167 * Returns YAFFS_OK or YAFFS_FAIL. |
|
|
168 */ |
|
|
169 int nandmtd1_ReadChunkWithTagsFromNAND(yaffs_Device *dev, |
|
|
170 int chunkInNAND, __u8 * data, yaffs_ExtendedTags * etags) |
|
|
171 { |
|
|
172 struct mtd_info * mtd = dev->genericDevice; |
|
|
173 int chunkBytes = dev->nDataBytesPerChunk; |
|
|
174 loff_t addr = ((loff_t)chunkInNAND) * chunkBytes; |
|
|
175 int eccres = YAFFS_ECC_RESULT_NO_ERROR; |
|
|
176 struct mtd_oob_ops ops; |
|
|
177 yaffs_PackedTags1 pt1; |
|
|
178 int retval; |
|
|
179 int deleted; |
|
|
180 |
|
|
181 memset(&ops, 0, sizeof(ops)); |
|
|
182 ops.mode = MTD_OOB_AUTO; |
|
|
183 ops.len = (data) ? chunkBytes : 0; |
|
|
184 ops.ooblen = YTAG1_SIZE; |
|
|
185 ops.datbuf = data; |
|
|
186 ops.oobbuf = (__u8 *)&pt1; |
|
|
187 |
|
|
188 #if (LINUX_VERSION_CODE < KERNEL_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. |
|
|
191 */ |
|
|
192 ops.len = (ops.datbuf) ? ops.len : ops.ooblen; |
|
|
193 #endif |
|
|
194 /* Read page and oob using MTD. |
|
|
195 * Check status and determine ECC result. |
|
|
196 */ |
|
|
197 retval = mtd->read_oob(mtd, addr, &ops); |
|
|
198 if (retval) { |
|
|
199 yaffs_trace(YAFFS_TRACE_MTD, |
|
|
200 "read_oob failed, chunk %d, mtd error %d\n", |
|
|
201 chunkInNAND, retval); |
|
|
202 } |
|
|
203 |
|
|
204 switch (retval) { |
|
|
205 case 0: |
|
|
206 /* no error */ |
|
|
207 break; |
|
|
208 |
|
|
209 case -EUCLEAN: |
|
|
210 /* MTD's ECC fixed the data */ |
|
|
211 eccres = YAFFS_ECC_RESULT_FIXED; |
|
|
212 dev->eccFixed++; |
|
|
213 break; |
|
|
214 |
|
|
215 case -EBADMSG: |
|
|
216 /* MTD's ECC could not fix the data */ |
|
|
217 dev->eccUnfixed++; |
|
|
218 /* fall into... */ |
|
|
219 default: |
|
|
220 rettags(etags, YAFFS_ECC_RESULT_UNFIXED, 0); |
|
|
221 etags->blockBad = (mtd->block_isbad)(mtd, addr); |
|
|
222 return YAFFS_FAIL; |
|
|
223 } |
|
|
224 |
|
|
225 /* Check for a blank/erased chunk. |
|
|
226 */ |
|
|
227 if (yaffs_CheckFF((__u8 *)&pt1, 8)) { |
|
|
228 /* when blank, upper layers want eccResult to be <= NO_ERROR */ |
|
|
229 return rettags(etags, YAFFS_ECC_RESULT_NO_ERROR, YAFFS_OK); |
|
|
230 } |
|
|
231 |
|
|
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 |
|
|
235 * inverted. |
|
|
236 */ |
|
|
237 deleted = !pt1.deleted; |
|
|
238 pt1.deleted = 1; |
|
|
239 #else |
|
|
240 (void) deleted; /* not used */ |
|
|
241 #endif |
|
|
242 |
|
|
243 /* Check the packed tags mini-ECC and correct if necessary/possible. |
|
|
244 */ |
|
|
245 retval = yaffs_CheckECCOnTags((yaffs_Tags *)&pt1); |
|
|
246 switch (retval) { |
|
|
247 case 0: |
|
|
248 /* no tags error, use MTD result */ |
|
|
249 break; |
|
|
250 case 1: |
|
|
251 /* recovered tags-ECC error */ |
|
|
252 dev->tagsEccFixed++; |
|
|
253 eccres = YAFFS_ECC_RESULT_FIXED; |
|
|
254 break; |
|
|
255 default: |
|
|
256 /* unrecovered tags-ECC error */ |
|
|
257 dev->tagsEccUnfixed++; |
|
|
258 return rettags(etags, YAFFS_ECC_RESULT_UNFIXED, YAFFS_FAIL); |
|
|
259 } |
|
|
260 |
|
|
261 /* Unpack the tags to extended form and set ECC result. |
|
|
262 * [set shouldBeFF just to keep yaffs_UnpackTags1 happy] |
|
|
263 */ |
|
|
264 pt1.shouldBeFF = 0xFFFFFFFF; |
|
|
265 yaffs_UnpackTags1(etags, &pt1); |
|
|
266 etags->eccResult = eccres; |
|
|
267 |
|
|
268 /* Set deleted state. |
|
|
269 */ |
|
|
270 #ifndef CONFIG_YAFFS_9BYTE_TAGS |
|
|
271 etags->chunkDeleted = deleted; |
|
|
272 #else |
|
|
273 etags->chunkDeleted = (yaffs_CountBits(((__u8 *)&pt1)[8]) < 7); |
|
|
274 #endif |
|
|
275 return YAFFS_OK; |
|
|
276 } |
|
|
277 |
|
|
278 /* Mark a block bad. |
|
|
279 * |
|
|
280 * This is a persistant state. |
|
|
281 * Use of this function should be rare. |
|
|
282 * |
|
|
283 * Returns YAFFS_OK or YAFFS_FAIL. |
|
|
284 */ |
|
|
285 int nandmtd1_MarkNANDBlockBad(struct yaffs_DeviceStruct *dev, int blockNo) |
|
|
286 { |
|
|
287 struct mtd_info * mtd = dev->genericDevice; |
|
|
288 int blocksize = dev->nChunksPerBlock * dev->nDataBytesPerChunk; |
|
|
289 int retval; |
|
|
290 |
|
|
291 yaffs_trace(YAFFS_TRACE_BAD_BLOCKS, "marking block %d bad", blockNo); |
|
|
292 |
|
|
293 retval = mtd->block_markbad(mtd, (loff_t)blocksize * blockNo); |
|
|
294 return (retval) ? YAFFS_FAIL : YAFFS_OK; |
|
|
295 } |
|
|
296 |
|
|
297 /* Check any MTD prerequists. |
|
|
298 * |
|
|
299 * Returns YAFFS_OK or YAFFS_FAIL. |
|
|
300 */ |
|
|
301 static int nandmtd1_TestPrerequists(struct mtd_info * mtd) |
|
|
302 { |
|
|
303 /* 2.6.18 has mtd->ecclayout->oobavail */ |
|
|
304 /* 2.6.21 has mtd->ecclayout->oobavail and mtd->oobavail */ |
|
|
305 int oobavail = mtd->ecclayout->oobavail; |
|
|
306 |
|
|
307 if (oobavail < YTAG1_SIZE) { |
|
|
308 yaffs_trace(YAFFS_TRACE_ERROR, |
|
|
309 "mtd device has only %d bytes for tags, need %d", |
|
|
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_QueryNANDBlock(struct yaffs_DeviceStruct *dev, int blockNo, |
|
|
326 yaffs_BlockState * pState, int *pSequenceNumber) |
|
|
327 { |
|
|
328 struct mtd_info * mtd = dev->genericDevice; |
|
|
329 int chunkNo = blockNo * dev->nChunksPerBlock; |
|
|
330 yaffs_ExtendedTags etags; |
|
|
331 int state = YAFFS_BLOCK_STATE_DEAD; |
|
|
332 int seqnum = 0; |
|
|
333 int retval; |
|
|
334 |
|
|
335 /* We don't yet have a good place to test for MTD config prerequists. |
|
|
336 * Do it here as we are called during the initial scan. |
|
|
337 */ |
|
|
338 if (nandmtd1_TestPrerequists(mtd) != YAFFS_OK) { |
|
|
339 return YAFFS_FAIL; |
|
|
340 } |
|
|
341 |
|
|
342 retval = nandmtd1_ReadChunkWithTagsFromNAND(dev, chunkNo, NULL, &etags); |
|
|
343 if (etags.blockBad) { |
|
|
344 yaffs_trace(YAFFS_TRACE_BAD_BLOCKS, |
|
|
345 "block %d is marked bad", blockNo); |
|
|
346 state = YAFFS_BLOCK_STATE_DEAD; |
|
|
347 } |
|
|
348 else if (etags.chunkUsed) { |
|
|
349 state = YAFFS_BLOCK_STATE_NEEDS_SCANNING; |
|
|
350 seqnum = etags.sequenceNumber; |
|
|
351 } |
|
|
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 /*KERNEL_VERSION*/ |