2 * YAFFS: Yet Another Flash File System. A NAND-flash specific file system.
4 * Copyright (C) 2002-2010 Aleph One Ltd.
5 * for Toby Churchill Ltd and Brightstar Engineering
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.
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"
20 static void yaffs_handle_rd_data_error(struct yaffs_dev *dev, int nand_chunk);
22 static const char yaffs_count_bits_table[256] = {
23 0, 1, 1, 2, 1, 2, 2, 3, 1, 2, 2, 3, 2, 3, 3, 4,
24 1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5,
25 1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5,
26 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
27 1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5,
28 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
29 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
30 3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7,
31 1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5,
32 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
33 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
34 3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7,
35 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
36 3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7,
37 3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7,
38 4, 5, 5, 6, 5, 6, 6, 7, 5, 6, 6, 7, 6, 7, 7, 8
41 int yaffs_count_bits(u8 x)
44 ret_val = yaffs_count_bits_table[x];
48 /********** Tags ECC calculations *********/
50 void yaffs_calc_ecc(const u8 * data, struct yaffs_spare *spare)
52 yaffs_ecc_cacl(data, spare->ecc1);
53 yaffs_ecc_cacl(&data[256], spare->ecc2);
56 void yaffs_calc_tags_ecc(struct yaffs_tags *tags)
58 /* Calculate an ecc */
60 unsigned char *b = ((union yaffs_tags_union *)tags)->as_bytes;
67 for (i = 0; i < 8; i++) {
68 for (j = 1; j & 0xff; j <<= 1) {
79 int yaffs_check_tags_ecc(struct yaffs_tags *tags)
81 unsigned ecc = tags->ecc;
83 yaffs_calc_tags_ecc(tags);
87 if (ecc && ecc <= 64) {
88 /* TODO: Handle the failure better. Retire? */
89 unsigned char *b = ((union yaffs_tags_union *)tags)->as_bytes;
93 b[ecc / 8] ^= (1 << (ecc & 7));
95 /* Now recvalc the ecc */
96 yaffs_calc_tags_ecc(tags);
98 return 1; /* recovered error */
100 /* Wierd ecc failure value */
101 /* TODO Need to do somethiong here */
102 return -1; /* unrecovered error */
108 /********** Tags **********/
110 static void yaffs_load_tags_to_spare(struct yaffs_spare *spare_ptr,
111 struct yaffs_tags *tags_ptr)
113 union yaffs_tags_union *tu = (union yaffs_tags_union *)tags_ptr;
115 yaffs_calc_tags_ecc(tags_ptr);
117 spare_ptr->tb0 = tu->as_bytes[0];
118 spare_ptr->tb1 = tu->as_bytes[1];
119 spare_ptr->tb2 = tu->as_bytes[2];
120 spare_ptr->tb3 = tu->as_bytes[3];
121 spare_ptr->tb4 = tu->as_bytes[4];
122 spare_ptr->tb5 = tu->as_bytes[5];
123 spare_ptr->tb6 = tu->as_bytes[6];
124 spare_ptr->tb7 = tu->as_bytes[7];
127 static void yaffs_get_tags_from_spare(struct yaffs_dev *dev,
128 struct yaffs_spare *spare_ptr,
129 struct yaffs_tags *tags_ptr)
131 union yaffs_tags_union *tu = (union yaffs_tags_union *)tags_ptr;
134 tu->as_bytes[0] = spare_ptr->tb0;
135 tu->as_bytes[1] = spare_ptr->tb1;
136 tu->as_bytes[2] = spare_ptr->tb2;
137 tu->as_bytes[3] = spare_ptr->tb3;
138 tu->as_bytes[4] = spare_ptr->tb4;
139 tu->as_bytes[5] = spare_ptr->tb5;
140 tu->as_bytes[6] = spare_ptr->tb6;
141 tu->as_bytes[7] = spare_ptr->tb7;
143 result = yaffs_check_tags_ecc(tags_ptr);
145 dev->n_tags_ecc_fixed++;
147 dev->n_tags_ecc_unfixed++;
150 static void yaffs_spare_init(struct yaffs_spare *spare)
152 memset(spare, 0xFF, sizeof(struct yaffs_spare));
155 static int yaffs_wr_nand(struct yaffs_dev *dev,
156 int nand_chunk, const u8 * data,
157 struct yaffs_spare *spare)
159 if (nand_chunk < dev->param.start_block * dev->param.chunks_per_block) {
161 (TSTR("**>> yaffs chunk %d is not valid" TENDSTR),
166 return dev->param.write_chunk_fn(dev, nand_chunk, data, spare);
169 static int yaffs_rd_chunk_nand(struct yaffs_dev *dev,
172 struct yaffs_spare *spare,
173 enum yaffs_ecc_result *ecc_result,
177 struct yaffs_spare local_spare;
179 if (!spare && data) {
180 /* If we don't have a real spare, then we use a local one. */
181 /* Need this for the calculation of the ecc */
182 spare = &local_spare;
185 if (!dev->param.use_nand_ecc) {
187 dev->param.read_chunk_fn(dev, nand_chunk, data, spare);
188 if (data && correct_errors) {
189 /* Do ECC correction */
190 /* Todo handle any errors */
191 int ecc_result1, ecc_result2;
194 yaffs_ecc_cacl(data, calc_ecc);
196 yaffs_ecc_correct(data, spare->ecc1, calc_ecc);
197 yaffs_ecc_cacl(&data[256], calc_ecc);
199 yaffs_ecc_correct(&data[256], spare->ecc2,
202 if (ecc_result1 > 0) {
205 ("**>>yaffs ecc error fix performed on chunk %d:0"
206 TENDSTR), nand_chunk));
208 } else if (ecc_result1 < 0) {
211 ("**>>yaffs ecc error unfixed on chunk %d:0"
212 TENDSTR), nand_chunk));
213 dev->n_ecc_unfixed++;
216 if (ecc_result2 > 0) {
219 ("**>>yaffs ecc error fix performed on chunk %d:1"
220 TENDSTR), nand_chunk));
222 } else if (ecc_result2 < 0) {
225 ("**>>yaffs ecc error unfixed on chunk %d:1"
226 TENDSTR), nand_chunk));
227 dev->n_ecc_unfixed++;
230 if (ecc_result1 || ecc_result2) {
231 /* We had a data problem on this page */
232 yaffs_handle_rd_data_error(dev, nand_chunk);
235 if (ecc_result1 < 0 || ecc_result2 < 0)
236 *ecc_result = YAFFS_ECC_RESULT_UNFIXED;
237 else if (ecc_result1 > 0 || ecc_result2 > 0)
238 *ecc_result = YAFFS_ECC_RESULT_FIXED;
240 *ecc_result = YAFFS_ECC_RESULT_NO_ERROR;
243 /* Must allocate enough memory for spare+2*sizeof(int) */
244 /* for ecc results from device. */
245 struct yaffs_nand_spare nspare;
247 memset(&nspare, 0, sizeof(nspare));
249 ret_val = dev->param.read_chunk_fn(dev, nand_chunk, data,
250 (struct yaffs_spare *)
252 memcpy(spare, &nspare, sizeof(struct yaffs_spare));
253 if (data && correct_errors) {
254 if (nspare.eccres1 > 0) {
257 ("**>>mtd ecc error fix performed on chunk %d:0"
258 TENDSTR), nand_chunk));
259 } else if (nspare.eccres1 < 0) {
262 ("**>>mtd ecc error unfixed on chunk %d:0"
263 TENDSTR), nand_chunk));
266 if (nspare.eccres2 > 0) {
269 ("**>>mtd ecc error fix performed on chunk %d:1"
270 TENDSTR), nand_chunk));
271 } else if (nspare.eccres2 < 0) {
274 ("**>>mtd ecc error unfixed on chunk %d:1"
275 TENDSTR), nand_chunk));
278 if (nspare.eccres1 || nspare.eccres2) {
279 /* We had a data problem on this page */
280 yaffs_handle_rd_data_error(dev, nand_chunk);
283 if (nspare.eccres1 < 0 || nspare.eccres2 < 0)
284 *ecc_result = YAFFS_ECC_RESULT_UNFIXED;
285 else if (nspare.eccres1 > 0 || nspare.eccres2 > 0)
286 *ecc_result = YAFFS_ECC_RESULT_FIXED;
288 *ecc_result = YAFFS_ECC_RESULT_NO_ERROR;
296 * Functions for robustisizing
299 static void yaffs_handle_rd_data_error(struct yaffs_dev *dev, int nand_chunk)
301 int flash_block = nand_chunk / dev->param.chunks_per_block;
303 /* Mark the block for retirement */
304 yaffs_get_block_info(dev,
305 flash_block + dev->block_offset)->needs_retiring =
307 T(YAFFS_TRACE_ERROR | YAFFS_TRACE_BAD_BLOCKS,
308 (TSTR("**>>Block %d marked for retirement" TENDSTR), flash_block));
311 * Just do a garbage collection on the affected block
312 * then retire the block
317 int yaffs_tags_compat_wr(struct yaffs_dev *dev,
319 const u8 * data, const struct yaffs_ext_tags *ext_tags)
321 struct yaffs_spare spare;
322 struct yaffs_tags tags;
324 yaffs_spare_init(&spare);
326 if (ext_tags->is_deleted)
327 spare.page_status = 0;
329 tags.obj_id = ext_tags->obj_id;
330 tags.chunk_id = ext_tags->chunk_id;
332 tags.n_bytes_lsb = ext_tags->n_bytes & 0x3ff;
334 if (dev->data_bytes_per_chunk >= 1024)
335 tags.n_bytes_msb = (ext_tags->n_bytes >> 10) & 3;
337 tags.n_bytes_msb = 3;
339 tags.serial_number = ext_tags->serial_number;
341 if (!dev->param.use_nand_ecc && data)
342 yaffs_calc_ecc(data, &spare);
344 yaffs_load_tags_to_spare(&spare, &tags);
348 return yaffs_wr_nand(dev, nand_chunk, data, &spare);
351 int yaffs_tags_compat_rd(struct yaffs_dev *dev,
353 u8 * data, struct yaffs_ext_tags *ext_tags)
356 struct yaffs_spare spare;
357 struct yaffs_tags tags;
358 enum yaffs_ecc_result ecc_result = YAFFS_ECC_RESULT_UNKNOWN;
360 static struct yaffs_spare spare_ff;
364 memset(&spare_ff, 0xFF, sizeof(spare_ff));
368 if (yaffs_rd_chunk_nand(dev, nand_chunk, data, &spare, &ecc_result, 1)) {
369 /* ext_tags may be NULL */
373 (yaffs_count_bits(spare.page_status) < 7) ? 1 : 0;
375 ext_tags->is_deleted = deleted;
376 ext_tags->ecc_result = ecc_result;
377 ext_tags->block_bad = 0; /* We're reading it */
378 /* therefore it is not a bad block */
379 ext_tags->chunk_used =
380 (memcmp(&spare_ff, &spare, sizeof(spare_ff)) !=
383 if (ext_tags->chunk_used) {
384 yaffs_get_tags_from_spare(dev, &spare, &tags);
386 ext_tags->obj_id = tags.obj_id;
387 ext_tags->chunk_id = tags.chunk_id;
388 ext_tags->n_bytes = tags.n_bytes_lsb;
390 if (dev->data_bytes_per_chunk >= 1024)
395 ext_tags->serial_number = tags.serial_number;
405 int yaffs_tags_compat_mark_bad(struct yaffs_dev *dev, int flash_block)
408 struct yaffs_spare spare;
410 memset(&spare, 0xff, sizeof(struct yaffs_spare));
412 spare.block_status = 'Y';
414 yaffs_wr_nand(dev, flash_block * dev->param.chunks_per_block, NULL,
416 yaffs_wr_nand(dev, flash_block * dev->param.chunks_per_block + 1,
423 int yaffs_tags_compat_query_block(struct yaffs_dev *dev,
425 enum yaffs_block_state *state,
429 struct yaffs_spare spare0, spare1;
430 static struct yaffs_spare spare_ff;
432 enum yaffs_ecc_result dummy;
435 memset(&spare_ff, 0xFF, sizeof(spare_ff));
441 yaffs_rd_chunk_nand(dev, block_no * dev->param.chunks_per_block, NULL,
443 yaffs_rd_chunk_nand(dev, block_no * dev->param.chunks_per_block + 1,
444 NULL, &spare1, &dummy, 1);
446 if (yaffs_count_bits(spare0.block_status & spare1.block_status) < 7)
447 *state = YAFFS_BLOCK_STATE_DEAD;
448 else if (memcmp(&spare_ff, &spare0, sizeof(spare_ff)) == 0)
449 *state = YAFFS_BLOCK_STATE_EMPTY;
451 *state = YAFFS_BLOCK_STATE_NEEDS_SCANNING;