2 * YAFFS: Yet another FFS. A NAND-flash specific file system.
3 * yaffs_tagscompat.h: Tags compatability layer to use YAFFS1 formatted NAND.
5 * Copyright (C) 2002 Aleph One Ltd.
7 * Created by Charles Manning <charles@aleph1.co.uk>
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.
13 * $Id: yaffs_tagscompat.c,v 1.6 2005-08-11 02:33:03 marty Exp $
16 #include "yaffs_guts.h"
17 #include "yaffs_tagscompat.h"
18 #include "yaffs_ecc.h"
20 static void yaffs_HandleReadDataError(yaffs_Device * dev, int chunkInNAND);
22 static void yaffs_CheckWrittenBlock(yaffs_Device * dev, int chunkInNAND);
23 static void yaffs_HandleWriteChunkOk(yaffs_Device * dev, int chunkInNAND,
25 const yaffs_Spare * spare);
26 static void yaffs_HandleUpdateChunk(yaffs_Device * dev, int chunkInNAND,
27 const yaffs_Spare * spare);
28 static void yaffs_HandleWriteChunkError(yaffs_Device * dev, int chunkInNAND);
31 static const char yaffs_countBitsTable[256] = {
32 0, 1, 1, 2, 1, 2, 2, 3, 1, 2, 2, 3, 2, 3, 3, 4,
33 1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5,
34 1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5,
35 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
36 1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5,
37 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
38 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
39 3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7,
40 1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5,
41 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
42 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
43 3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7,
44 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
45 3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7,
46 3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7,
47 4, 5, 5, 6, 5, 6, 6, 7, 5, 6, 6, 7, 6, 7, 7, 8
50 static int yaffs_CountBits(__u8 x)
53 retVal = yaffs_countBitsTable[x];
57 /********** Tags ECC calculations *********/
59 void yaffs_CalcECC(const __u8 * data, yaffs_Spare * spare)
61 yaffs_ECCCalculate(data, spare->ecc1);
62 yaffs_ECCCalculate(&data[256], spare->ecc2);
65 void yaffs_CalcTagsECC(yaffs_Tags * tags)
67 /* Calculate an ecc */
69 unsigned char *b = ((yaffs_TagsUnion *) tags)->asBytes;
76 for (i = 0; i < 8; i++) {
77 for (j = 1; j & 0xff; j <<= 1) {
89 int yaffs_CheckECCOnTags(yaffs_Tags * tags)
91 unsigned ecc = tags->ecc;
93 yaffs_CalcTagsECC(tags);
97 if (ecc && ecc <= 64) {
98 /* TODO: Handle the failure better. Retire? */
99 unsigned char *b = ((yaffs_TagsUnion *) tags)->asBytes;
103 b[ecc / 8] ^= (1 << (ecc & 7));
105 /* Now recvalc the ecc */
106 yaffs_CalcTagsECC(tags);
108 return 1; /* recovered error */
110 /* Wierd ecc failure value */
111 /* TODO Need to do somethiong here */
112 return -1; /* unrecovered error */
118 /********** Tags **********/
120 static void yaffs_LoadTagsIntoSpare(yaffs_Spare * sparePtr,
121 yaffs_Tags * tagsPtr)
123 yaffs_TagsUnion *tu = (yaffs_TagsUnion *) tagsPtr;
125 yaffs_CalcTagsECC(tagsPtr);
127 sparePtr->tagByte0 = tu->asBytes[0];
128 sparePtr->tagByte1 = tu->asBytes[1];
129 sparePtr->tagByte2 = tu->asBytes[2];
130 sparePtr->tagByte3 = tu->asBytes[3];
131 sparePtr->tagByte4 = tu->asBytes[4];
132 sparePtr->tagByte5 = tu->asBytes[5];
133 sparePtr->tagByte6 = tu->asBytes[6];
134 sparePtr->tagByte7 = tu->asBytes[7];
137 static void yaffs_GetTagsFromSpare(yaffs_Device * dev, yaffs_Spare * sparePtr,
138 yaffs_Tags * tagsPtr)
140 yaffs_TagsUnion *tu = (yaffs_TagsUnion *) tagsPtr;
143 tu->asBytes[0] = sparePtr->tagByte0;
144 tu->asBytes[1] = sparePtr->tagByte1;
145 tu->asBytes[2] = sparePtr->tagByte2;
146 tu->asBytes[3] = sparePtr->tagByte3;
147 tu->asBytes[4] = sparePtr->tagByte4;
148 tu->asBytes[5] = sparePtr->tagByte5;
149 tu->asBytes[6] = sparePtr->tagByte6;
150 tu->asBytes[7] = sparePtr->tagByte7;
152 result = yaffs_CheckECCOnTags(tagsPtr);
155 } else if (result < 0) {
156 dev->tagsEccUnfixed++;
160 static void yaffs_SpareInitialise(yaffs_Spare * spare)
162 memset(spare, 0xFF, sizeof(yaffs_Spare));
165 static int yaffs_WriteChunkToNAND(struct yaffs_DeviceStruct *dev,
166 int chunkInNAND, const __u8 * data,
169 if (chunkInNAND < dev->startBlock * dev->nChunksPerBlock) {
171 (TSTR("**>> yaffs chunk %d is not valid" TENDSTR),
177 return dev->writeChunkToNAND(dev, chunkInNAND, data, spare);
180 static int yaffs_ReadChunkFromNAND(struct yaffs_DeviceStruct *dev,
184 yaffs_ECCResult * eccResult,
185 int doErrorCorrection)
188 yaffs_Spare localSpare;
192 if (!spare && data) {
193 /* If we don't have a real spare, then we use a local one. */
194 /* Need this for the calculation of the ecc */
198 if (!dev->useNANDECC) {
199 retVal = dev->readChunkFromNAND(dev, chunkInNAND, data, spare);
200 if (data && doErrorCorrection) {
201 /* Do ECC correction */
202 /* Todo handle any errors */
203 int eccResult1, eccResult2;
206 yaffs_ECCCalculate(data, calcEcc);
208 yaffs_ECCCorrect(data, spare->ecc1, calcEcc);
209 yaffs_ECCCalculate(&data[256], calcEcc);
211 yaffs_ECCCorrect(&data[256], spare->ecc2, calcEcc);
213 if (eccResult1 > 0) {
216 ("**>>ecc error fix performed on chunk %d:0"
217 TENDSTR), chunkInNAND));
219 } else if (eccResult1 < 0) {
222 ("**>>ecc error unfixed on chunk %d:0"
223 TENDSTR), chunkInNAND));
227 if (eccResult2 > 0) {
230 ("**>>ecc error fix performed on chunk %d:1"
231 TENDSTR), chunkInNAND));
233 } else if (eccResult2 < 0) {
236 ("**>>ecc error unfixed on chunk %d:1"
237 TENDSTR), chunkInNAND));
241 if (eccResult1 || eccResult2) {
242 /* We had a data problem on this page */
243 yaffs_HandleReadDataError(dev, chunkInNAND);
246 if (eccResult1 < 0 || eccResult2 < 0)
247 *eccResult = YAFFS_ECC_RESULT_UNFIXED;
248 else if (eccResult1 > 0 || eccResult2 > 0)
249 *eccResult = YAFFS_ECC_RESULT_FIXED;
251 *eccResult = YAFFS_ECC_RESULT_NO_ERROR;
254 /* Must allocate enough memory for spare+2*sizeof(int) */
255 /* for ecc results from device. */
256 struct yaffs_NANDSpare nspare;
258 dev->readChunkFromNAND(dev, chunkInNAND, data,
259 (yaffs_Spare *) & nspare);
260 memcpy(spare, &nspare, sizeof(yaffs_Spare));
261 if (data && doErrorCorrection) {
262 if (nspare.eccres1 > 0) {
265 ("**>>ecc error fix performed on chunk %d:0"
266 TENDSTR), chunkInNAND));
267 } else if (nspare.eccres1 < 0) {
270 ("**>>ecc error unfixed on chunk %d:0"
271 TENDSTR), chunkInNAND));
274 if (nspare.eccres2 > 0) {
277 ("**>>ecc error fix performed on chunk %d:1"
278 TENDSTR), chunkInNAND));
279 } else if (nspare.eccres2 < 0) {
282 ("**>>ecc error unfixed on chunk %d:1"
283 TENDSTR), chunkInNAND));
286 if (nspare.eccres1 || nspare.eccres2) {
287 /* We had a data problem on this page */
288 yaffs_HandleReadDataError(dev, chunkInNAND);
291 if (nspare.eccres1 < 0 || nspare.eccres2 < 0)
292 *eccResult = YAFFS_ECC_RESULT_UNFIXED;
293 else if (nspare.eccres1 > 0 || nspare.eccres2 > 0)
294 *eccResult = YAFFS_ECC_RESULT_FIXED;
296 *eccResult = YAFFS_ECC_RESULT_NO_ERROR;
304 static int yaffs_CheckChunkErased(struct yaffs_DeviceStruct *dev,
309 static __u8 cmpbuf[YAFFS_BYTES_PER_CHUNK];
310 static __u8 data[YAFFS_BYTES_PER_CHUNK];
311 /* Might as well always allocate the larger size for */
312 /* dev->useNANDECC == true; */
313 static __u8 spare[sizeof(struct yaffs_NANDSpare)];
315 dev->readChunkFromNAND(dev, chunkInNAND, data, (yaffs_Spare *) spare);
318 memset(cmpbuf, 0xff, YAFFS_BYTES_PER_CHUNK);
322 if (memcmp(cmpbuf, data, YAFFS_BYTES_PER_CHUNK))
324 if (memcmp(cmpbuf, spare, 16))
333 * Functions for robustisizing
336 static void yaffs_HandleReadDataError(yaffs_Device * dev, int chunkInNAND)
338 int blockInNAND = chunkInNAND / dev->nChunksPerBlock;
340 /* Mark the block for retirement */
341 yaffs_GetBlockInfo(dev, blockInNAND)->needsRetiring = 1;
342 T(YAFFS_TRACE_ERROR | YAFFS_TRACE_BAD_BLOCKS,
343 (TSTR("**>>Block %d marked for retirement" TENDSTR), blockInNAND));
346 * Just do a garbage collection on the affected block
347 * then retire the block
353 static void yaffs_CheckWrittenBlock(yaffs_Device * dev, int chunkInNAND)
357 static void yaffs_HandleWriteChunkOk(yaffs_Device * dev, int chunkInNAND,
359 const yaffs_Spare * spare)
363 static void yaffs_HandleUpdateChunk(yaffs_Device * dev, int chunkInNAND,
364 const yaffs_Spare * spare)
368 static void yaffs_HandleWriteChunkError(yaffs_Device * dev, int chunkInNAND)
370 int blockInNAND = chunkInNAND / dev->nChunksPerBlock;
372 /* Mark the block for retirement */
373 yaffs_GetBlockInfo(dev, blockInNAND)->needsRetiring = 1;
374 /* Delete the chunk */
375 yaffs_DeleteChunk(dev, chunkInNAND, 1, __LINE__);
378 static int yaffs_VerifyCompare(const __u8 * d0, const __u8 * d1,
379 const yaffs_Spare * s0, const yaffs_Spare * s1)
382 if (memcmp(d0, d1, YAFFS_BYTES_PER_CHUNK) != 0 ||
383 s0->tagByte0 != s1->tagByte0 ||
384 s0->tagByte1 != s1->tagByte1 ||
385 s0->tagByte2 != s1->tagByte2 ||
386 s0->tagByte3 != s1->tagByte3 ||
387 s0->tagByte4 != s1->tagByte4 ||
388 s0->tagByte5 != s1->tagByte5 ||
389 s0->tagByte6 != s1->tagByte6 ||
390 s0->tagByte7 != s1->tagByte7 ||
391 s0->ecc1[0] != s1->ecc1[0] ||
392 s0->ecc1[1] != s1->ecc1[1] ||
393 s0->ecc1[2] != s1->ecc1[2] ||
394 s0->ecc2[0] != s1->ecc2[0] ||
395 s0->ecc2[1] != s1->ecc2[1] || s0->ecc2[2] != s1->ecc2[2]) {
403 int yaffs_TagsCompatabilityWriteChunkWithTagsToNAND(yaffs_Device * dev,
406 const yaffs_ExtendedTags *
412 yaffs_SpareInitialise(&spare);
414 if (eTags->chunkDeleted) {
415 spare.pageStatus = 0;
417 tags.objectId = eTags->objectId;
418 tags.chunkId = eTags->chunkId;
419 tags.byteCount = eTags->byteCount;
420 tags.serialNumber = eTags->serialNumber;
422 if (!dev->useNANDECC && data) {
423 yaffs_CalcECC(data, &spare);
425 yaffs_LoadTagsIntoSpare(&spare, &tags);
429 return yaffs_WriteChunkToNAND(dev, chunkInNAND, data, &spare);
432 int yaffs_TagsCompatabilityReadChunkWithTagsFromNAND(yaffs_Device * dev,
435 yaffs_ExtendedTags * eTags)
440 yaffs_ECCResult eccResult;
442 static yaffs_Spare spareFF;
446 memset(&spareFF, 0xFF, sizeof(spareFF));
450 if (yaffs_ReadChunkFromNAND
451 (dev, chunkInNAND, data, &spare, &eccResult, 1)) {
452 /* eTags may be NULL */
456 (yaffs_CountBits(spare.pageStatus) < 7) ? 1 : 0;
458 yaffs_GetTagsFromSpare(dev, &spare, &tags);
460 eTags->chunkDeleted = deleted;
461 eTags->objectId = tags.objectId;
462 eTags->chunkId = tags.chunkId;
463 eTags->byteCount = tags.byteCount;
464 eTags->serialNumber = tags.serialNumber;
465 eTags->eccResult = eccResult;
466 eTags->blockBad = 0; /* We're reading it */
467 /* therefore it is not a bad block */
470 (memcmp(&spareFF, &spare, sizeof(spareFF)) !=
480 int yaffs_TagsCompatabilityMarkNANDBlockBad(struct yaffs_DeviceStruct *dev,
486 memset(&spare, 0xff, sizeof(yaffs_Spare));
488 spare.blockStatus = 'Y';
490 yaffs_WriteChunkToNAND(dev, blockInNAND * dev->nChunksPerBlock, NULL,
492 yaffs_WriteChunkToNAND(dev, blockInNAND * dev->nChunksPerBlock + 1,
499 int yaffs_TagsCompatabilityQueryNANDBlock(struct yaffs_DeviceStruct *dev,
500 int blockNo, yaffs_BlockState *
505 yaffs_Spare spare0, spare1;
506 static yaffs_Spare spareFF;
508 yaffs_ECCResult dummy;
511 memset(&spareFF, 0xFF, sizeof(spareFF));
517 yaffs_ReadChunkFromNAND(dev, blockNo * dev->nChunksPerBlock, NULL,
519 yaffs_ReadChunkFromNAND(dev, blockNo * dev->nChunksPerBlock + 1, NULL,
522 if (yaffs_CountBits(spare0.blockStatus & spare1.blockStatus) < 7)
523 *state = YAFFS_BLOCK_STATE_DEAD;
524 else if (memcmp(&spareFF, &spare0, sizeof(spareFF)) == 0)
525 *state = YAFFS_BLOCK_STATE_EMPTY;
527 *state = YAFFS_BLOCK_STATE_NEEDS_SCANNING;