78781e638346f37d7138d7b87f366e5009ed95e0
[yaffs2.git] / yaffs_tagscompat.c
1 /*
2  * YAFFS: Yet Another Flash File System. A NAND-flash specific file system.
3  *
4  * Copyright (C) 2002-2011 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 #include "yaffs_guts.h"
15 #include "yaffs_tagscompat.h"
16 #include "yaffs_ecc.h"
17 #include "yaffs_getblockinfo.h"
18 #include "yaffs_trace.h"
19
20 static void yaffs_handle_rd_data_error(struct yaffs_dev *dev, int nand_chunk);
21
22
23 /********** Tags ECC calculations  *********/
24
25 static void yaffs_calc_ecc(const u8 *data, struct yaffs_spare *spare)
26 {
27         yaffs_ecc_calc(data, spare->ecc1);
28         yaffs_ecc_calc(&data[256], spare->ecc2);
29 }
30
31 void yaffs_calc_tags_ecc(struct yaffs_tags *tags)
32 {
33         /* Calculate an ecc */
34         unsigned char *b = ((union yaffs_tags_union *)tags)->as_bytes;
35         unsigned i, j;
36         unsigned ecc = 0;
37         unsigned bit = 0;
38
39         tags->ecc = 0;
40
41         for (i = 0; i < 8; i++) {
42                 for (j = 1; j & 0xff; j <<= 1) {
43                         bit++;
44                         if (b[i] & j)
45                                 ecc ^= bit;
46                 }
47         }
48         tags->ecc = ecc;
49 }
50
51 int yaffs_check_tags_ecc(struct yaffs_tags *tags)
52 {
53         unsigned ecc = tags->ecc;
54
55         yaffs_calc_tags_ecc(tags);
56
57         ecc ^= tags->ecc;
58
59         if (ecc && ecc <= 64) {
60                 /* TODO: Handle the failure better. Retire? */
61                 unsigned char *b = ((union yaffs_tags_union *)tags)->as_bytes;
62
63                 ecc--;
64
65                 b[ecc / 8] ^= (1 << (ecc & 7));
66
67                 /* Now recvalc the ecc */
68                 yaffs_calc_tags_ecc(tags);
69
70                 return 1;       /* recovered error */
71         } else if (ecc) {
72                 /* Wierd ecc failure value */
73                 /* TODO Need to do somethiong here */
74                 return -1;      /* unrecovered error */
75         }
76         return 0;
77 }
78
79 /********** Tags **********/
80
81 static void yaffs_load_tags_to_spare(struct yaffs_spare *spare_ptr,
82                                      struct yaffs_tags *tags_ptr)
83 {
84         union yaffs_tags_union *tu = (union yaffs_tags_union *)tags_ptr;
85
86         yaffs_calc_tags_ecc(tags_ptr);
87
88         spare_ptr->tb0 = tu->as_bytes[0];
89         spare_ptr->tb1 = tu->as_bytes[1];
90         spare_ptr->tb2 = tu->as_bytes[2];
91         spare_ptr->tb3 = tu->as_bytes[3];
92         spare_ptr->tb4 = tu->as_bytes[4];
93         spare_ptr->tb5 = tu->as_bytes[5];
94         spare_ptr->tb6 = tu->as_bytes[6];
95         spare_ptr->tb7 = tu->as_bytes[7];
96 }
97
98 static void yaffs_get_tags_from_spare(struct yaffs_dev *dev,
99                                       struct yaffs_spare *spare_ptr,
100                                       struct yaffs_tags *tags_ptr)
101 {
102         union yaffs_tags_union *tu = (union yaffs_tags_union *)tags_ptr;
103         int result;
104
105         tu->as_bytes[0] = spare_ptr->tb0;
106         tu->as_bytes[1] = spare_ptr->tb1;
107         tu->as_bytes[2] = spare_ptr->tb2;
108         tu->as_bytes[3] = spare_ptr->tb3;
109         tu->as_bytes[4] = spare_ptr->tb4;
110         tu->as_bytes[5] = spare_ptr->tb5;
111         tu->as_bytes[6] = spare_ptr->tb6;
112         tu->as_bytes[7] = spare_ptr->tb7;
113
114         result = yaffs_check_tags_ecc(tags_ptr);
115         if (result > 0)
116                 dev->n_tags_ecc_fixed++;
117         else if (result < 0)
118                 dev->n_tags_ecc_unfixed++;
119 }
120
121 static void yaffs_spare_init(struct yaffs_spare *spare)
122 {
123         memset(spare, 0xff, sizeof(struct yaffs_spare));
124 }
125
126 static int yaffs_wr_nand(struct yaffs_dev *dev,
127                          int nand_chunk, const u8 *data,
128                          struct yaffs_spare *spare)
129 {
130         int data_size;
131         int spare_size;
132         u8 spare_buffer[100];
133
134         if (nand_chunk < dev->param.start_block * dev->param.chunks_per_block) {
135                 yaffs_trace(YAFFS_TRACE_ERROR,
136                         "**>> yaffs chunk %d is not valid",
137                         nand_chunk);
138                 return YAFFS_FAIL;
139         }
140
141         /* Format up the spare */
142
143         return dev->param.drv_write_chunk_fn(dev, nand_chunk,
144                                 data, data_size,
145                                 spare_buffer, spare_size);
146 }
147
148 static int yaffs_rd_chunk_nand(struct yaffs_dev *dev,
149                                int nand_chunk,
150                                u8 *data,
151                                struct yaffs_spare *spare,
152                                enum yaffs_ecc_result *ecc_result,
153                                int correct_errors)
154 {
155         int ret_val;
156         struct yaffs_spare local_spare;
157         int data_size;
158         int spare_size;
159
160         if (!spare) {
161                 /* If we don't have a real spare, then we use a local one. */
162                 /* Need this for the calculation of the ecc */
163                 spare = &local_spare;
164         }
165
166         if (!dev->param.use_nand_ecc) {
167                 ret_val =
168                     dev->param.drv_read_chunk_fn(dev, nand_chunk,
169                                                  data, data_size,
170                                                  (u8 *)spare, spare_size,
171                                         ecc_result);
172                 if (data && correct_errors) {
173                         /* Do ECC correction */
174                         /* Todo handle any errors */
175                         int ecc_result1, ecc_result2;
176                         u8 calc_ecc[3];
177
178                         yaffs_ecc_calc(data, calc_ecc);
179                         ecc_result1 =
180                             yaffs_ecc_correct(data, spare->ecc1, calc_ecc);
181                         yaffs_ecc_calc(&data[256], calc_ecc);
182                         ecc_result2 =
183                             yaffs_ecc_correct(&data[256], spare->ecc2,
184                                               calc_ecc);
185
186                         if (ecc_result1 > 0) {
187                                 yaffs_trace(YAFFS_TRACE_ERROR,
188                                         "**>>yaffs ecc error fix performed on chunk %d:0",
189                                         nand_chunk);
190                                 dev->n_ecc_fixed++;
191                         } else if (ecc_result1 < 0) {
192                                 yaffs_trace(YAFFS_TRACE_ERROR,
193                                         "**>>yaffs ecc error unfixed on chunk %d:0",
194                                         nand_chunk);
195                                 dev->n_ecc_unfixed++;
196                         }
197
198                         if (ecc_result2 > 0) {
199                                 yaffs_trace(YAFFS_TRACE_ERROR,
200                                         "**>>yaffs ecc error fix performed on chunk %d:1",
201                                         nand_chunk);
202                                 dev->n_ecc_fixed++;
203                         } else if (ecc_result2 < 0) {
204                                 yaffs_trace(YAFFS_TRACE_ERROR,
205                                         "**>>yaffs ecc error unfixed on chunk %d:1",
206                                         nand_chunk);
207                                 dev->n_ecc_unfixed++;
208                         }
209
210                         if (ecc_result1 || ecc_result2) {
211                                 /* We had a data problem on this page */
212                                 yaffs_handle_rd_data_error(dev, nand_chunk);
213                         }
214
215                         if (ecc_result1 < 0 || ecc_result2 < 0)
216                                 *ecc_result = YAFFS_ECC_RESULT_UNFIXED;
217                         else if (ecc_result1 > 0 || ecc_result2 > 0)
218                                 *ecc_result = YAFFS_ECC_RESULT_FIXED;
219                         else
220                                 *ecc_result = YAFFS_ECC_RESULT_NO_ERROR;
221                 }
222         } else {
223                 /* Must allocate enough memory for spare+2*sizeof(int) */
224                 /* for ecc results from device. */
225                 struct yaffs_nand_spare nspare;
226
227                 memset(&nspare, 0, sizeof(nspare));
228
229                 ret_val = dev->param.drv_read_chunk_fn(dev, nand_chunk,
230                                                 data, data_size,
231                                                 (u8 *) &nspare, sizeof(nspare),
232                                                 NULL);
233                 memcpy(spare, &nspare, sizeof(struct yaffs_spare));
234                 if (data && correct_errors) {
235                         if (nspare.eccres1 > 0) {
236                                 yaffs_trace(YAFFS_TRACE_ERROR,
237                                         "**>>mtd ecc error fix performed on chunk %d:0",
238                                         nand_chunk);
239                         } else if (nspare.eccres1 < 0) {
240                                 yaffs_trace(YAFFS_TRACE_ERROR,
241                                         "**>>mtd ecc error unfixed on chunk %d:0",
242                                         nand_chunk);
243                         }
244
245                         if (nspare.eccres2 > 0) {
246                                 yaffs_trace(YAFFS_TRACE_ERROR,
247                                         "**>>mtd ecc error fix performed on chunk %d:1",
248                                         nand_chunk);
249                         } else if (nspare.eccres2 < 0) {
250                                 yaffs_trace(YAFFS_TRACE_ERROR,
251                                         "**>>mtd ecc error unfixed on chunk %d:1",
252                                         nand_chunk);
253                         }
254
255                         if (nspare.eccres1 || nspare.eccres2) {
256                                 /* We had a data problem on this page */
257                                 yaffs_handle_rd_data_error(dev, nand_chunk);
258                         }
259
260                         if (nspare.eccres1 < 0 || nspare.eccres2 < 0)
261                                 *ecc_result = YAFFS_ECC_RESULT_UNFIXED;
262                         else if (nspare.eccres1 > 0 || nspare.eccres2 > 0)
263                                 *ecc_result = YAFFS_ECC_RESULT_FIXED;
264                         else
265                                 *ecc_result = YAFFS_ECC_RESULT_NO_ERROR;
266
267                 }
268         }
269         return ret_val;
270 }
271
272 /*
273  * Functions for robustisizing
274  */
275
276 static void yaffs_handle_rd_data_error(struct yaffs_dev *dev, int nand_chunk)
277 {
278         int flash_block = nand_chunk / dev->param.chunks_per_block;
279
280         /* Mark the block for retirement */
281         yaffs_get_block_info(dev, flash_block + dev->block_offset)->
282                 needs_retiring = 1;
283         yaffs_trace(YAFFS_TRACE_ERROR | YAFFS_TRACE_BAD_BLOCKS,
284                 "**>>Block %d marked for retirement",
285                 flash_block);
286
287         /* TODO:
288          * Just do a garbage collection on the affected block
289          * then retire the block
290          * NB recursion
291          */
292 }
293
294 static int yaffs_tags_compat_wr(struct yaffs_dev *dev,
295                          int nand_chunk,
296                          const u8 *data, const struct yaffs_ext_tags *ext_tags)
297 {
298         struct yaffs_spare spare;
299         struct yaffs_tags tags;
300
301         yaffs_spare_init(&spare);
302
303         if (ext_tags->is_deleted)
304                 spare.page_status = 0;
305         else {
306                 tags.obj_id = ext_tags->obj_id;
307                 tags.chunk_id = ext_tags->chunk_id;
308
309                 tags.n_bytes_lsb = ext_tags->n_bytes & (1024 - 1);
310
311                 if (dev->data_bytes_per_chunk >= 1024)
312                         tags.n_bytes_msb = (ext_tags->n_bytes >> 10) & 3;
313                 else
314                         tags.n_bytes_msb = 3;
315
316                 tags.serial_number = ext_tags->serial_number;
317
318                 if (!dev->param.use_nand_ecc && data)
319                         yaffs_calc_ecc(data, &spare);
320
321                 yaffs_load_tags_to_spare(&spare, &tags);
322         }
323         return yaffs_wr_nand(dev, nand_chunk, data, &spare);
324 }
325
326 static int yaffs_tags_compat_rd(struct yaffs_dev *dev,
327                          int nand_chunk,
328                          u8 *data, struct yaffs_ext_tags *ext_tags)
329 {
330         struct yaffs_spare spare;
331         struct yaffs_tags tags;
332         enum yaffs_ecc_result ecc_result = YAFFS_ECC_RESULT_UNKNOWN;
333         static struct yaffs_spare spare_ff;
334         static int init;
335         int deleted;
336
337         if (!init) {
338                 memset(&spare_ff, 0xff, sizeof(spare_ff));
339                 init = 1;
340         }
341
342         if (!yaffs_rd_chunk_nand(dev, nand_chunk,
343                                         data, &spare, &ecc_result, 1))
344                 return YAFFS_FAIL;
345
346         /* ext_tags may be NULL */
347         if (!ext_tags)
348                 return YAFFS_OK;
349
350         deleted = (hweight8(spare.page_status) < 7) ? 1 : 0;
351
352         ext_tags->is_deleted = deleted;
353         ext_tags->ecc_result = ecc_result;
354         ext_tags->block_bad = 0;        /* We're reading it */
355         /* therefore it is not a bad block */
356         ext_tags->chunk_used =
357                 memcmp(&spare_ff, &spare, sizeof(spare_ff)) ? 1 : 0;
358
359         if (ext_tags->chunk_used) {
360                 yaffs_get_tags_from_spare(dev, &spare, &tags);
361                 ext_tags->obj_id = tags.obj_id;
362                 ext_tags->chunk_id = tags.chunk_id;
363                 ext_tags->n_bytes = tags.n_bytes_lsb;
364
365                 if (dev->data_bytes_per_chunk >= 1024)
366                         ext_tags->n_bytes |=
367                                 (((unsigned)tags.n_bytes_msb) << 10);
368
369                 ext_tags->serial_number = tags.serial_number;
370         }
371
372         return YAFFS_OK;
373 }
374
375 static int yaffs_tags_compat_mark_bad(struct yaffs_dev *dev, int flash_block)
376 {
377         struct yaffs_spare spare;
378
379         memset(&spare, 0xff, sizeof(struct yaffs_spare));
380
381         spare.block_status = 'Y';
382
383         yaffs_wr_nand(dev, flash_block * dev->param.chunks_per_block, NULL,
384                       &spare);
385         yaffs_wr_nand(dev, flash_block * dev->param.chunks_per_block + 1,
386                       NULL, &spare);
387
388         return YAFFS_OK;
389 }
390
391 static int yaffs_tags_compat_query_block(struct yaffs_dev *dev,
392                                   int block_no,
393                                   enum yaffs_block_state *state,
394                                   u32 *seq_number)
395 {
396         struct yaffs_spare spare0, spare1;
397         static struct yaffs_spare spare_ff;
398         static int init;
399         enum yaffs_ecc_result dummy;
400
401         if (!init) {
402                 memset(&spare_ff, 0xff, sizeof(spare_ff));
403                 init = 1;
404         }
405
406         *seq_number = 0;
407
408         yaffs_rd_chunk_nand(dev, block_no * dev->param.chunks_per_block, NULL,
409                             &spare0, &dummy, 1);
410         yaffs_rd_chunk_nand(dev, block_no * dev->param.chunks_per_block + 1,
411                             NULL, &spare1, &dummy, 1);
412
413         if (hweight8(spare0.block_status & spare1.block_status) < 7)
414                 *state = YAFFS_BLOCK_STATE_DEAD;
415         else if (memcmp(&spare_ff, &spare0, sizeof(spare_ff)) == 0)
416                 *state = YAFFS_BLOCK_STATE_EMPTY;
417         else
418                 *state = YAFFS_BLOCK_STATE_NEEDS_SCAN;
419
420         return YAFFS_OK;
421 }
422
423 void yaffs_tags_compat_install(struct yaffs_dev *dev)
424 {
425         if(dev->param.is_yaffs2)
426                 return;
427         if(!dev->param.write_chunk_tags_fn)
428                 dev->param.write_chunk_tags_fn = yaffs_tags_compat_wr;
429         if(!dev->param.read_chunk_tags_fn)
430                 dev->param.read_chunk_tags_fn = yaffs_tags_compat_rd;
431         if(!dev->param.read_chunk_tags_fn)
432                 dev->param.query_block_fn = yaffs_tags_compat_query_block;
433         if(!dev->param.mark_bad_fn)
434                 dev->param.mark_bad_fn = yaffs_tags_compat_mark_bad;
435 }