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