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);
23 static const char yaffs_count_bits_table[256] = {
24 0, 1, 1, 2, 1, 2, 2, 3, 1, 2, 2, 3, 2, 3, 3, 4,
25 1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5,
26 1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5,
27 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
28 1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5,
29 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
30 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
31 3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7,
32 1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5,
33 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
34 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
35 3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7,
36 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
37 3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7,
38 3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7,
39 4, 5, 5, 6, 5, 6, 6, 7, 5, 6, 6, 7, 6, 7, 7, 8
42 int yaffs_count_bits(u8 x)
45 ret_val = yaffs_count_bits_table[x];
49 /********** Tags ECC calculations *********/
51 void yaffs_calc_ecc(const u8 *data, yaffs_spare *spare)
53 yaffs_ecc_cacl(data, spare->ecc1);
54 yaffs_ecc_cacl(&data[256], spare->ecc2);
57 void yaffs_calc_tags_ecc(struct yaffs_tags *tags)
59 /* Calculate an ecc */
61 unsigned char *b = ((yaffs_tags_union_t *) tags)->as_bytes;
68 for (i = 0; i < 8; i++) {
69 for (j = 1; j & 0xff; j <<= 1) {
80 int yaffs_check_tags_ecc(struct yaffs_tags *tags)
82 unsigned ecc = tags->ecc;
84 yaffs_calc_tags_ecc(tags);
88 if (ecc && ecc <= 64) {
89 /* TODO: Handle the failure better. Retire? */
90 unsigned char *b = ((yaffs_tags_union_t *) tags)->as_bytes;
94 b[ecc / 8] ^= (1 << (ecc & 7));
96 /* Now recvalc the ecc */
97 yaffs_calc_tags_ecc(tags);
99 return 1; /* recovered error */
101 /* Wierd ecc failure value */
102 /* TODO Need to do somethiong here */
103 return -1; /* unrecovered error */
109 /********** Tags **********/
111 static void yaffs_load_tags_to_spare(yaffs_spare *spare_ptr,
112 struct yaffs_tags *tags_ptr)
114 yaffs_tags_union_t *tu = (yaffs_tags_union_t *) tags_ptr;
116 yaffs_calc_tags_ecc(tags_ptr);
118 spare_ptr->tb0 = tu->as_bytes[0];
119 spare_ptr->tb1 = tu->as_bytes[1];
120 spare_ptr->tb2 = tu->as_bytes[2];
121 spare_ptr->tb3 = tu->as_bytes[3];
122 spare_ptr->tb4 = tu->as_bytes[4];
123 spare_ptr->tb5 = tu->as_bytes[5];
124 spare_ptr->tb6 = tu->as_bytes[6];
125 spare_ptr->tb7 = tu->as_bytes[7];
128 static void yaffs_get_tags_from_spare(struct yaffs_dev *dev, yaffs_spare *spare_ptr,
129 struct yaffs_tags *tags_ptr)
131 yaffs_tags_union_t *tu = (yaffs_tags_union_t *) 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(yaffs_spare *spare)
152 memset(spare, 0xFF, sizeof(yaffs_spare));
155 static int yaffs_wr_nand(struct yaffs_dev *dev,
156 int nand_chunk, const u8 *data,
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,
173 yaffs_ecc_result *ecc_result,
177 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) {
186 ret_val = dev->param.read_chunk_fn(dev, nand_chunk, data, spare);
187 if (data && correct_errors) {
188 /* Do ECC correction */
189 /* Todo handle any errors */
190 int ecc_result1, ecc_result2;
193 yaffs_ecc_cacl(data, calc_ecc);
195 yaffs_ecc_correct(data, spare->ecc1, calc_ecc);
196 yaffs_ecc_cacl(&data[256], calc_ecc);
198 yaffs_ecc_correct(&data[256], spare->ecc2, calc_ecc);
200 if (ecc_result1 > 0) {
203 ("**>>yaffs ecc error fix performed on chunk %d:0"
204 TENDSTR), nand_chunk));
206 } else if (ecc_result1 < 0) {
209 ("**>>yaffs ecc error unfixed on chunk %d:0"
210 TENDSTR), nand_chunk));
211 dev->n_ecc_unfixed++;
214 if (ecc_result2 > 0) {
217 ("**>>yaffs ecc error fix performed on chunk %d:1"
218 TENDSTR), nand_chunk));
220 } else if (ecc_result2 < 0) {
223 ("**>>yaffs ecc error unfixed on chunk %d:1"
224 TENDSTR), nand_chunk));
225 dev->n_ecc_unfixed++;
228 if (ecc_result1 || ecc_result2) {
229 /* We had a data problem on this page */
230 yaffs_handle_rd_data_error(dev, nand_chunk);
233 if (ecc_result1 < 0 || ecc_result2 < 0)
234 *ecc_result = YAFFS_ECC_RESULT_UNFIXED;
235 else if (ecc_result1 > 0 || ecc_result2 > 0)
236 *ecc_result = YAFFS_ECC_RESULT_FIXED;
238 *ecc_result = YAFFS_ECC_RESULT_NO_ERROR;
241 /* Must allocate enough memory for spare+2*sizeof(int) */
242 /* for ecc results from device. */
243 struct yaffs_nand_spare nspare;
245 memset(&nspare, 0, sizeof(nspare));
247 ret_val = dev->param.read_chunk_fn(dev, nand_chunk, data,
248 (yaffs_spare *) &nspare);
249 memcpy(spare, &nspare, sizeof(yaffs_spare));
250 if (data && correct_errors) {
251 if (nspare.eccres1 > 0) {
254 ("**>>mtd ecc error fix performed on chunk %d:0"
255 TENDSTR), nand_chunk));
256 } else if (nspare.eccres1 < 0) {
259 ("**>>mtd ecc error unfixed on chunk %d:0"
260 TENDSTR), nand_chunk));
263 if (nspare.eccres2 > 0) {
266 ("**>>mtd ecc error fix performed on chunk %d:1"
267 TENDSTR), nand_chunk));
268 } else if (nspare.eccres2 < 0) {
271 ("**>>mtd ecc error unfixed on chunk %d:1"
272 TENDSTR), nand_chunk));
275 if (nspare.eccres1 || nspare.eccres2) {
276 /* We had a data problem on this page */
277 yaffs_handle_rd_data_error(dev, nand_chunk);
280 if (nspare.eccres1 < 0 || nspare.eccres2 < 0)
281 *ecc_result = YAFFS_ECC_RESULT_UNFIXED;
282 else if (nspare.eccres1 > 0 || nspare.eccres2 > 0)
283 *ecc_result = YAFFS_ECC_RESULT_FIXED;
285 *ecc_result = YAFFS_ECC_RESULT_NO_ERROR;
294 * Functions for robustisizing
297 static void yaffs_handle_rd_data_error(struct yaffs_dev *dev, int nand_chunk)
299 int flash_block = nand_chunk / dev->param.chunks_per_block;
301 /* Mark the block for retirement */
302 yaffs_get_block_info(dev, flash_block + dev->block_offset)->needs_retiring = 1;
303 T(YAFFS_TRACE_ERROR | YAFFS_TRACE_BAD_BLOCKS,
304 (TSTR("**>>Block %d marked for retirement" TENDSTR), flash_block));
307 * Just do a garbage collection on the affected block
308 * then retire the block
314 int yaffs_tags_compat_wr(struct yaffs_dev *dev,
317 const struct yaffs_ext_tags *ext_tags)
320 struct yaffs_tags tags;
322 yaffs_spare_init(&spare);
324 if (ext_tags->is_deleted)
325 spare.page_status = 0;
327 tags.obj_id = ext_tags->obj_id;
328 tags.chunk_id = ext_tags->chunk_id;
330 tags.n_bytes_lsb = ext_tags->n_bytes & 0x3ff;
332 if (dev->data_bytes_per_chunk >= 1024)
333 tags.n_bytes_msb = (ext_tags->n_bytes >> 10) & 3;
335 tags.n_bytes_msb = 3;
338 tags.serial_number = ext_tags->serial_number;
340 if (!dev->param.use_nand_ecc && data)
341 yaffs_calc_ecc(data, &spare);
343 yaffs_load_tags_to_spare(&spare, &tags);
347 return yaffs_wr_nand(dev, nand_chunk, data, &spare);
350 int yaffs_tags_compat_rd(struct yaffs_dev *dev,
353 struct yaffs_ext_tags *ext_tags)
357 struct yaffs_tags tags;
358 yaffs_ecc_result ecc_result = YAFFS_ECC_RESULT_UNKNOWN;
360 static yaffs_spare spare_ff;
364 memset(&spare_ff, 0xFF, sizeof(spare_ff));
368 if (yaffs_rd_chunk_nand
369 (dev, nand_chunk, data, &spare, &ecc_result, 1)) {
370 /* ext_tags may be NULL */
374 (yaffs_count_bits(spare.page_status) < 7) ? 1 : 0;
376 ext_tags->is_deleted = deleted;
377 ext_tags->ecc_result = ecc_result;
378 ext_tags->block_bad = 0; /* We're reading it */
379 /* therefore it is not a bad block */
380 ext_tags->chunk_used =
381 (memcmp(&spare_ff, &spare, sizeof(spare_ff)) !=
384 if (ext_tags->chunk_used) {
385 yaffs_get_tags_from_spare(dev, &spare, &tags);
387 ext_tags->obj_id = tags.obj_id;
388 ext_tags->chunk_id = tags.chunk_id;
389 ext_tags->n_bytes = tags.n_bytes_lsb;
391 if (dev->data_bytes_per_chunk >= 1024)
392 ext_tags->n_bytes |= (((unsigned) tags.n_bytes_msb) << 10);
394 ext_tags->serial_number = tags.serial_number;
404 int yaffs_tags_compat_mark_bad(struct yaffs_dev *dev,
410 memset(&spare, 0xff, sizeof(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 yaffs_block_state_t *state,
429 yaffs_spare spare0, spare1;
430 static yaffs_spare spare_ff;
432 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, NULL,
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;