ace4b28af49dea04c39df9c1dbc7dfbd36e32684
[yaffs2.git] / yaffs_tagscompat.c
1 /*
2  * YAFFS: Yet another FFS. A NAND-flash specific file system. 
3  * yaffs_tagscompat.h: Tags compatability layer to use YAFFS1 formatted NAND.
4  *
5  * Copyright (C) 2002 Aleph One Ltd.
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  * $Id: yaffs_tagscompat.c,v 1.5 2005-08-10 20:34:40 charles Exp $
14  */
15
16 #include "yaffs_guts.h"
17 #include "yaffs_tagscompat.h"
18 #include "yaffs_ecc.h"
19
20 static void yaffs_HandleReadDataError(yaffs_Device *dev,int chunkInNAND);
21 #ifdef NOTYET
22 static void yaffs_CheckWrittenBlock(yaffs_Device *dev,int chunkInNAND);
23 static void yaffs_HandleWriteChunkOk(yaffs_Device *dev,int chunkInNAND,const __u8 *data, const yaffs_Spare *spare);
24 static void yaffs_HandleUpdateChunk(yaffs_Device *dev,int chunkInNAND, const yaffs_Spare *spare);
25 static void yaffs_HandleWriteChunkError(yaffs_Device *dev,int chunkInNAND);
26 #endif
27
28
29 static const char yaffs_countBitsTable[256] =
30 {
31 0,1,1,2,1,2,2,3,1,2,2,3,2,3,3,4,
32 1,2,2,3,2,3,3,4,2,3,3,4,3,4,4,5,
33 1,2,2,3,2,3,3,4,2,3,3,4,3,4,4,5,
34 2,3,3,4,3,4,4,5,3,4,4,5,4,5,5,6,
35 1,2,2,3,2,3,3,4,2,3,3,4,3,4,4,5,
36 2,3,3,4,3,4,4,5,3,4,4,5,4,5,5,6,
37 2,3,3,4,3,4,4,5,3,4,4,5,4,5,5,6,
38 3,4,4,5,4,5,5,6,4,5,5,6,5,6,6,7,
39 1,2,2,3,2,3,3,4,2,3,3,4,3,4,4,5,
40 2,3,3,4,3,4,4,5,3,4,4,5,4,5,5,6,
41 2,3,3,4,3,4,4,5,3,4,4,5,4,5,5,6,
42 3,4,4,5,4,5,5,6,4,5,5,6,5,6,6,7,
43 2,3,3,4,3,4,4,5,3,4,4,5,4,5,5,6,
44 3,4,4,5,4,5,5,6,4,5,5,6,5,6,6,7,
45 3,4,4,5,4,5,5,6,4,5,5,6,5,6,6,7,
46 4,5,5,6,5,6,6,7,5,6,6,7,6,7,7,8
47 };
48
49 static int yaffs_CountBits(__u8 x)
50 {
51         int retVal;
52         retVal = yaffs_countBitsTable[x];
53         return retVal;
54 }
55
56
57 /////////////// Tags ECC calculations ///////////////////
58
59 void yaffs_CalcECC(const __u8 *data, yaffs_Spare *spare)
60 {
61         yaffs_ECCCalculate(data , spare->ecc1);
62         yaffs_ECCCalculate(&data[256] , spare->ecc2);
63 }
64
65 void yaffs_CalcTagsECC(yaffs_Tags *tags)
66 {
67         // Calculate an ecc
68
69         unsigned char *b = ((yaffs_TagsUnion *)tags)->asBytes;
70         unsigned  i,j;
71         unsigned  ecc = 0;
72         unsigned bit = 0;
73
74         tags->ecc = 0;
75
76         for(i = 0; i < 8; i++)
77         {
78                 for(j = 1; j &0xff; j<<=1)
79                 {
80                         bit++;
81                         if(b[i] & j)
82                         {
83                                 ecc ^= bit;
84                         }
85                 }
86         }
87
88         tags->ecc = ecc;
89
90
91 }
92
93 int  yaffs_CheckECCOnTags(yaffs_Tags *tags)
94 {
95         unsigned ecc = tags->ecc;
96
97         yaffs_CalcTagsECC(tags);
98
99         ecc ^= tags->ecc;
100         
101         if(ecc && ecc <= 64)
102         {
103                 // TODO: Handle the failure better. Retire?
104                 unsigned char *b = ((yaffs_TagsUnion *)tags)->asBytes;
105
106                 ecc--;
107
108                 b[ecc / 8] ^= (1 << (ecc & 7));
109
110                 // Now recvalc the ecc
111                 yaffs_CalcTagsECC(tags);
112
113                 return 1; // recovered error
114         }
115         else if(ecc)
116         {
117                 // Wierd ecc failure value
118                 // TODO Need to do somethiong here
119                 return -1; //unrecovered error
120         }
121
122         return 0;
123 }
124
125 //////////////////////////// Tags ///////////////////////////////////////
126
127 static void yaffs_LoadTagsIntoSpare(yaffs_Spare *sparePtr, yaffs_Tags *tagsPtr)
128 {
129         yaffs_TagsUnion *tu = (yaffs_TagsUnion *)tagsPtr;
130
131         yaffs_CalcTagsECC(tagsPtr);
132         
133         sparePtr->tagByte0 = tu->asBytes[0];
134         sparePtr->tagByte1 = tu->asBytes[1];
135         sparePtr->tagByte2 = tu->asBytes[2];
136         sparePtr->tagByte3 = tu->asBytes[3];
137         sparePtr->tagByte4 = tu->asBytes[4];
138         sparePtr->tagByte5 = tu->asBytes[5];
139         sparePtr->tagByte6 = tu->asBytes[6];
140         sparePtr->tagByte7 = tu->asBytes[7];
141 }
142
143 static void yaffs_GetTagsFromSpare(yaffs_Device *dev, yaffs_Spare *sparePtr,yaffs_Tags *tagsPtr)
144 {
145         yaffs_TagsUnion *tu = (yaffs_TagsUnion *)tagsPtr;
146         int result;
147
148         tu->asBytes[0]= sparePtr->tagByte0;
149         tu->asBytes[1]= sparePtr->tagByte1;
150         tu->asBytes[2]= sparePtr->tagByte2;
151         tu->asBytes[3]= sparePtr->tagByte3;
152         tu->asBytes[4]= sparePtr->tagByte4;
153         tu->asBytes[5]= sparePtr->tagByte5;
154         tu->asBytes[6]= sparePtr->tagByte6;
155         tu->asBytes[7]= sparePtr->tagByte7;
156         
157         result =  yaffs_CheckECCOnTags(tagsPtr);
158         if(result> 0)
159         {
160                 dev->tagsEccFixed++;
161         }
162         else if(result <0)
163         {
164                 dev->tagsEccUnfixed++;
165         }
166 }
167
168 static void yaffs_SpareInitialise(yaffs_Spare *spare)
169 {
170         memset(spare,0xFF,sizeof(yaffs_Spare));
171 }
172
173
174
175
176 static int yaffs_WriteChunkToNAND(struct yaffs_DeviceStruct *dev,int chunkInNAND, const __u8 *data, yaffs_Spare *spare)
177 {
178         if(chunkInNAND < dev->startBlock * dev->nChunksPerBlock)
179         {
180                 T(YAFFS_TRACE_ERROR,(TSTR("**>> yaffs chunk %d is not valid" TENDSTR),chunkInNAND));
181                 return YAFFS_FAIL;
182         }
183
184         dev->nPageWrites++;
185         return dev->writeChunkToNAND(dev,chunkInNAND,data,spare);
186 }
187
188
189
190 static int yaffs_ReadChunkFromNAND(struct yaffs_DeviceStruct *dev,
191                                                         int chunkInNAND, 
192                                                         __u8 *data, 
193                                                         yaffs_Spare *spare,
194                                                         yaffs_ECCResult *eccResult,
195                                                         int doErrorCorrection)
196 {
197         int retVal;
198         yaffs_Spare localSpare;
199
200         dev->nPageReads++;
201         
202         
203
204         
205         if(!spare && data)
206         {
207                 // If we don't have a real spare, then we use a local one.
208                 // Need this for the calculation of the ecc
209                 spare = &localSpare;
210         }
211         
212
213         if(!dev->useNANDECC)
214         {
215                 retVal  = dev->readChunkFromNAND(dev,chunkInNAND,data,spare);
216                 if(data && doErrorCorrection)
217                 {
218                         // Do ECC correction
219                         //Todo handle any errors
220                 int eccResult1,eccResult2;
221                 __u8 calcEcc[3];
222                 
223                         yaffs_ECCCalculate(data,calcEcc);
224                         eccResult1 = yaffs_ECCCorrect (data,spare->ecc1, calcEcc);
225                         yaffs_ECCCalculate(&data[256],calcEcc);
226                         eccResult2 = yaffs_ECCCorrect(&data[256],spare->ecc2, calcEcc);
227
228                         if(eccResult1>0)
229                         {
230                                 T(YAFFS_TRACE_ERROR, (TSTR("**>>ecc error fix performed on chunk %d:0" TENDSTR),chunkInNAND));
231                                 dev->eccFixed++;
232                         }
233                         else if(eccResult1<0)
234                         {
235                                 T(YAFFS_TRACE_ERROR,(TSTR("**>>ecc error unfixed on chunk %d:0" TENDSTR),chunkInNAND));
236                                 dev->eccUnfixed++;
237                         }
238
239                         if(eccResult2>0)
240                         {
241                                 T(YAFFS_TRACE_ERROR,(TSTR("**>>ecc error fix performed on chunk %d:1" TENDSTR),chunkInNAND));
242                                 dev->eccFixed++;
243                         }
244                         else if(eccResult2<0)
245                         {
246                                 T(YAFFS_TRACE_ERROR,(TSTR("**>>ecc error unfixed on chunk %d:1" TENDSTR),chunkInNAND));
247                                 dev->eccUnfixed++;
248                         }
249
250                         if(eccResult1 || eccResult2)
251                         {
252                                 // Hoosterman, we had a data problem on this page
253                                 yaffs_HandleReadDataError(dev,chunkInNAND);
254                         }
255                         
256                         if(eccResult1 < 0 || eccResult2 < 0) 
257                                 *eccResult = YAFFS_ECC_RESULT_UNFIXED;
258                         else if(eccResult1 > 0 || eccResult2 > 0)
259                                 *eccResult = YAFFS_ECC_RESULT_FIXED;
260                         else
261                                 *eccResult = YAFFS_ECC_RESULT_NO_ERROR;
262                 }
263         }
264         else
265         {
266         // Must allocate enough memory for spare+2*sizeof(int) for ecc results from device.
267         struct yaffs_NANDSpare nspare;
268                 retVal  = dev->readChunkFromNAND(dev,chunkInNAND,data,(yaffs_Spare*)&nspare);
269                 memcpy (spare, &nspare, sizeof(yaffs_Spare));
270                 if(data && doErrorCorrection)
271                 {
272                         if(nspare.eccres1>0)
273                         {
274                                 T(YAFFS_TRACE_ERROR,(TSTR("**>>ecc error fix performed on chunk %d:0" TENDSTR),chunkInNAND));
275                         }
276                         else if(nspare.eccres1<0)
277                         {
278                                 T(YAFFS_TRACE_ERROR,(TSTR("**>>ecc error unfixed on chunk %d:0" TENDSTR),chunkInNAND));
279                         }
280
281                         if(nspare.eccres2>0)
282                         {
283                                 T(YAFFS_TRACE_ERROR,(TSTR("**>>ecc error fix performed on chunk %d:1" TENDSTR),chunkInNAND));
284                         }
285                         else if(nspare.eccres2<0)
286                         {
287                                 T(YAFFS_TRACE_ERROR,(TSTR("**>>ecc error unfixed on chunk %d:1" TENDSTR),chunkInNAND));
288                         }
289
290                         if(nspare.eccres1 || nspare.eccres2)
291                         {
292                                 // Hoosterman, we had a data problem on this page
293                                 yaffs_HandleReadDataError(dev,chunkInNAND);
294                         }
295                         
296                         if(nspare.eccres1 < 0 || nspare.eccres2 < 0) 
297                                 *eccResult = YAFFS_ECC_RESULT_UNFIXED;
298                         else if(nspare.eccres1 > 0 || nspare.eccres2 > 0)
299                                 *eccResult = YAFFS_ECC_RESULT_FIXED;
300                         else
301                                 *eccResult = YAFFS_ECC_RESULT_NO_ERROR;
302
303
304                 }
305         }
306         return retVal;
307 }
308
309 #ifdef NOTYET
310 static int yaffs_CheckChunkErased(struct yaffs_DeviceStruct *dev,int chunkInNAND)
311 {
312
313         static int init = 0;
314         static __u8 cmpbuf[YAFFS_BYTES_PER_CHUNK];
315         static __u8 data[YAFFS_BYTES_PER_CHUNK];
316     // Might as well always allocate the larger size for dev->useNANDECC == true;
317         static __u8 spare[sizeof(struct yaffs_NANDSpare)];
318
319         dev->readChunkFromNAND(dev,chunkInNAND,data,(yaffs_Spare *)spare);
320
321         if(!init)
322         {
323                 memset(cmpbuf,0xff,YAFFS_BYTES_PER_CHUNK);
324                 init = 1;
325         }
326
327         if(memcmp(cmpbuf,data,YAFFS_BYTES_PER_CHUNK)) return  YAFFS_FAIL;
328         if(memcmp(cmpbuf,spare,16)) return YAFFS_FAIL;
329
330
331         return YAFFS_OK;
332
333 }
334 #endif
335
336
337
338 ///
339 // Functions for robustisizing
340 //
341 //
342
343 static void yaffs_HandleReadDataError(yaffs_Device *dev,int chunkInNAND)
344 {
345         int blockInNAND = chunkInNAND/dev->nChunksPerBlock;
346
347         // Mark the block for retirement
348         yaffs_GetBlockInfo(dev,blockInNAND)->needsRetiring = 1;
349         T(YAFFS_TRACE_ERROR | YAFFS_TRACE_BAD_BLOCKS,(TSTR("**>>Block %d marked for retirement" TENDSTR),blockInNAND));
350
351
352         //TODO
353         // Just do a garbage collection on the affected block then retire the block
354         // NB recursion
355 }
356
357
358 #ifdef NOTYET
359 static void yaffs_CheckWrittenBlock(yaffs_Device *dev,int chunkInNAND)
360 {
361 }
362
363 static void yaffs_HandleWriteChunkOk(yaffs_Device *dev,int chunkInNAND,const __u8 *data, const yaffs_Spare *spare)
364 {
365 }
366
367 static void yaffs_HandleUpdateChunk(yaffs_Device *dev,int chunkInNAND, const yaffs_Spare *spare)
368 {
369 }
370
371 static void yaffs_HandleWriteChunkError(yaffs_Device *dev,int chunkInNAND)
372 {
373         int blockInNAND = chunkInNAND/dev->nChunksPerBlock;
374
375         // Mark the block for retirement
376         yaffs_GetBlockInfo(dev,blockInNAND)->needsRetiring = 1;
377         // Delete the chunk
378         yaffs_DeleteChunk(dev,chunkInNAND,1,__LINE__);
379 }
380
381
382
383
384 static int yaffs_VerifyCompare(const __u8 *d0, const __u8 * d1, const yaffs_Spare *s0, const yaffs_Spare *s1)
385 {
386
387
388         if( memcmp(d0,d1,YAFFS_BYTES_PER_CHUNK) != 0 ||
389                 s0->tagByte0 != s1->tagByte0 ||
390                 s0->tagByte1 != s1->tagByte1 ||
391                 s0->tagByte2 != s1->tagByte2 ||
392                 s0->tagByte3 != s1->tagByte3 ||
393                 s0->tagByte4 != s1->tagByte4 ||
394                 s0->tagByte5 != s1->tagByte5 ||
395                 s0->tagByte6 != s1->tagByte6 ||
396                 s0->tagByte7 != s1->tagByte7 ||
397                 s0->ecc1[0]  != s1->ecc1[0]  ||
398                 s0->ecc1[1]  != s1->ecc1[1]  ||
399                 s0->ecc1[2]  != s1->ecc1[2]  ||
400                 s0->ecc2[0]  != s1->ecc2[0]  ||
401                 s0->ecc2[1]  != s1->ecc2[1]  ||
402                 s0->ecc2[2]  != s1->ecc2[2] )
403                 {
404                         return 0;
405                 }
406
407         return 1;
408 }
409 #endif /* NOTYET */
410
411
412
413 int yaffs_TagsCompatabilityWriteChunkWithTagsToNAND(yaffs_Device *dev,int chunkInNAND,const __u8 *data, const yaffs_ExtendedTags *eTags)
414 {
415         yaffs_Spare spare;
416         yaffs_Tags tags;        
417         
418         yaffs_SpareInitialise(&spare);
419         
420         if(eTags->chunkDeleted)
421         {
422                 spare.pageStatus = 0;
423         }
424         else
425         {
426                 tags.objectId = eTags->objectId;
427                 tags.chunkId = eTags->chunkId;
428                 tags.byteCount = eTags->byteCount;
429                 tags.serialNumber = eTags->serialNumber;
430                 
431 // NCB
432                 if (!dev->useNANDECC && data)
433                 {
434                     yaffs_CalcECC(data,&spare);
435                 }
436
437 // /NCB
438                  yaffs_LoadTagsIntoSpare(&spare,&tags);
439                 
440         }
441         
442         return yaffs_WriteChunkToNAND(dev,chunkInNAND,data,&spare);
443 }
444
445
446 int yaffs_TagsCompatabilityReadChunkWithTagsFromNAND(yaffs_Device *dev,int chunkInNAND, __u8 *data, yaffs_ExtendedTags *eTags)
447 {
448
449         yaffs_Spare spare;
450         yaffs_Tags tags;
451         yaffs_ECCResult eccResult;
452         
453 // NCB
454      static yaffs_Spare spareFF;
455      static int init;
456      
457      if(!init)
458      {
459              memset(&spareFF,0xFF,sizeof(spareFF));
460              init = 1;
461      }
462 // /NCB
463         if(yaffs_ReadChunkFromNAND(dev,chunkInNAND,data,&spare,&eccResult,1))
464         {
465 // added NCB - eTags may be NULL
466                 if (eTags) {
467
468                  int deleted = (yaffs_CountBits(spare.pageStatus) < 7) ? 1 : 0;
469                         
470                  yaffs_GetTagsFromSpare(dev,&spare,&tags);
471                  
472                  eTags->chunkDeleted = deleted;
473                  eTags->objectId = tags.objectId;
474                  eTags->chunkId = tags.chunkId;
475                  eTags->byteCount = tags.byteCount;
476                  eTags->serialNumber = tags.serialNumber;
477                  eTags->eccResult = eccResult;
478                  eTags->blockBad = 0; // We're reading it therefore it is not a bad block
479                  
480 // NCB added 18/2/2005
481                  eTags->chunkUsed = (memcmp(&spareFF,&spare,sizeof(spareFF)) != 0) ? 1:0;
482                 }
483                  
484                  return YAFFS_OK;
485         }
486         else
487         { 
488                 return YAFFS_FAIL;
489         }
490 }
491
492 int yaffs_TagsCompatabilityMarkNANDBlockBad(struct yaffs_DeviceStruct *dev, int blockInNAND)
493 {
494
495         yaffs_Spare spare;
496
497         memset(&spare, 0xff,sizeof(yaffs_Spare));
498
499         spare.blockStatus = 'Y';
500
501         yaffs_WriteChunkToNAND(dev, blockInNAND * dev->nChunksPerBlock, NULL , &spare);
502         yaffs_WriteChunkToNAND(dev, blockInNAND * dev->nChunksPerBlock + 1, NULL , &spare);
503         
504         return YAFFS_OK;
505         
506 }
507
508
509 int yaffs_TagsCompatabilityQueryNANDBlock(struct yaffs_DeviceStruct *dev, int blockNo, yaffs_BlockState *state, int *sequenceNumber)
510 {
511      
512      yaffs_Spare spare0,spare1;
513      static yaffs_Spare spareFF;
514      static int init;
515      yaffs_ECCResult dummy;
516      
517      if(!init)
518      {
519              memset(&spareFF,0xFF,sizeof(spareFF));
520              init = 1;
521      }
522      
523      *sequenceNumber = 0;
524      
525      yaffs_ReadChunkFromNAND(dev,blockNo * dev->nChunksPerBlock,NULL,&spare0,&dummy,1);
526      yaffs_ReadChunkFromNAND(dev,blockNo * dev->nChunksPerBlock + 1,NULL,&spare1,&dummy,1);
527      
528      if(yaffs_CountBits(spare0.blockStatus & spare1.blockStatus) < 7)
529         *state = YAFFS_BLOCK_STATE_DEAD;
530      else if(memcmp(&spareFF,&spare0,sizeof(spareFF)) == 0)
531          *state = YAFFS_BLOCK_STATE_EMPTY;
532      else
533          *state = YAFFS_BLOCK_STATE_NEEDS_SCANNING;  
534
535      return YAFFS_OK;
536 }
537