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 /********** Tags ECC calculations *********/
25 void yaffs_calc_ecc(const u8 * data, struct yaffs_spare *spare)
27 yaffs_ecc_cacl(data, spare->ecc1);
28 yaffs_ecc_cacl(&data[256], spare->ecc2);
31 void yaffs_calc_tags_ecc(struct yaffs_tags *tags)
33 /* Calculate an ecc */
35 unsigned char *b = ((union yaffs_tags_union *)tags)->as_bytes;
42 for (i = 0; i < 8; i++) {
43 for (j = 1; j & 0xff; j <<= 1) {
54 int yaffs_check_tags_ecc(struct yaffs_tags *tags)
56 unsigned ecc = tags->ecc;
58 yaffs_calc_tags_ecc(tags);
62 if (ecc && ecc <= 64) {
63 /* TODO: Handle the failure better. Retire? */
64 unsigned char *b = ((union yaffs_tags_union *)tags)->as_bytes;
68 b[ecc / 8] ^= (1 << (ecc & 7));
70 /* Now recvalc the ecc */
71 yaffs_calc_tags_ecc(tags);
73 return 1; /* recovered error */
75 /* Wierd ecc failure value */
76 /* TODO Need to do somethiong here */
77 return -1; /* unrecovered error */
83 /********** Tags **********/
85 static void yaffs_load_tags_to_spare(struct yaffs_spare *spare_ptr,
86 struct yaffs_tags *tags_ptr)
88 union yaffs_tags_union *tu = (union yaffs_tags_union *)tags_ptr;
90 yaffs_calc_tags_ecc(tags_ptr);
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];
102 static void yaffs_get_tags_from_spare(struct yaffs_dev *dev,
103 struct yaffs_spare *spare_ptr,
104 struct yaffs_tags *tags_ptr)
106 union yaffs_tags_union *tu = (union yaffs_tags_union *)tags_ptr;
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;
118 result = yaffs_check_tags_ecc(tags_ptr);
120 dev->n_tags_ecc_fixed++;
122 dev->n_tags_ecc_unfixed++;
125 static void yaffs_spare_init(struct yaffs_spare *spare)
127 memset(spare, 0xFF, sizeof(struct yaffs_spare));
130 static int yaffs_wr_nand(struct yaffs_dev *dev,
131 int nand_chunk, const u8 * data,
132 struct yaffs_spare *spare)
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",
141 return dev->param.write_chunk_fn(dev, nand_chunk, data, spare);
144 static int yaffs_rd_chunk_nand(struct yaffs_dev *dev,
147 struct yaffs_spare *spare,
148 enum yaffs_ecc_result *ecc_result,
152 struct yaffs_spare local_spare;
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;
160 if (!dev->param.use_nand_ecc) {
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;
169 yaffs_ecc_cacl(data, calc_ecc);
171 yaffs_ecc_correct(data, spare->ecc1, calc_ecc);
172 yaffs_ecc_cacl(&data[256], calc_ecc);
174 yaffs_ecc_correct(&data[256], spare->ecc2,
177 if (ecc_result1 > 0) {
178 yaffs_trace(YAFFS_TRACE_ERROR,
179 "**>>yaffs ecc error fix performed on chunk %d:0",
182 } else if (ecc_result1 < 0) {
183 yaffs_trace(YAFFS_TRACE_ERROR,
184 "**>>yaffs ecc error unfixed on chunk %d:0",
186 dev->n_ecc_unfixed++;
189 if (ecc_result2 > 0) {
190 yaffs_trace(YAFFS_TRACE_ERROR,
191 "**>>yaffs ecc error fix performed on chunk %d:1",
194 } else if (ecc_result2 < 0) {
195 yaffs_trace(YAFFS_TRACE_ERROR,
196 "**>>yaffs ecc error unfixed on chunk %d:1",
198 dev->n_ecc_unfixed++;
201 if (ecc_result1 || ecc_result2) {
202 /* We had a data problem on this page */
203 yaffs_handle_rd_data_error(dev, nand_chunk);
206 if (ecc_result1 < 0 || ecc_result2 < 0)
207 *ecc_result = YAFFS_ECC_RESULT_UNFIXED;
208 else if (ecc_result1 > 0 || ecc_result2 > 0)
209 *ecc_result = YAFFS_ECC_RESULT_FIXED;
211 *ecc_result = YAFFS_ECC_RESULT_NO_ERROR;
214 /* Must allocate enough memory for spare+2*sizeof(int) */
215 /* for ecc results from device. */
216 struct yaffs_nand_spare nspare;
218 memset(&nspare, 0, sizeof(nspare));
220 ret_val = dev->param.read_chunk_fn(dev, nand_chunk, data,
221 (struct yaffs_spare *)
223 memcpy(spare, &nspare, sizeof(struct yaffs_spare));
224 if (data && correct_errors) {
225 if (nspare.eccres1 > 0) {
226 yaffs_trace(YAFFS_TRACE_ERROR,
227 "**>>mtd ecc error fix performed on chunk %d:0",
229 } else if (nspare.eccres1 < 0) {
230 yaffs_trace(YAFFS_TRACE_ERROR,
231 "**>>mtd ecc error unfixed on chunk %d:0",
235 if (nspare.eccres2 > 0) {
236 yaffs_trace(YAFFS_TRACE_ERROR,
237 "**>>mtd ecc error fix performed on chunk %d:1",
239 } else if (nspare.eccres2 < 0) {
240 yaffs_trace(YAFFS_TRACE_ERROR,
241 "**>>mtd ecc error unfixed on chunk %d:1",
245 if (nspare.eccres1 || nspare.eccres2) {
246 /* We had a data problem on this page */
247 yaffs_handle_rd_data_error(dev, nand_chunk);
250 if (nspare.eccres1 < 0 || nspare.eccres2 < 0)
251 *ecc_result = YAFFS_ECC_RESULT_UNFIXED;
252 else if (nspare.eccres1 > 0 || nspare.eccres2 > 0)
253 *ecc_result = YAFFS_ECC_RESULT_FIXED;
255 *ecc_result = YAFFS_ECC_RESULT_NO_ERROR;
263 * Functions for robustisizing
266 static void yaffs_handle_rd_data_error(struct yaffs_dev *dev, int nand_chunk)
268 int flash_block = nand_chunk / dev->param.chunks_per_block;
270 /* Mark the block for retirement */
271 yaffs_get_block_info(dev,
272 flash_block + dev->block_offset)->needs_retiring =
274 yaffs_trace(YAFFS_TRACE_ERROR | YAFFS_TRACE_BAD_BLOCKS,
275 "**>>Block %d marked for retirement",
279 * Just do a garbage collection on the affected block
280 * then retire the block
285 int yaffs_tags_compat_wr(struct yaffs_dev *dev,
287 const u8 * data, const struct yaffs_ext_tags *ext_tags)
289 struct yaffs_spare spare;
290 struct yaffs_tags tags;
292 yaffs_spare_init(&spare);
294 if (ext_tags->is_deleted)
295 spare.page_status = 0;
297 tags.obj_id = ext_tags->obj_id;
298 tags.chunk_id = ext_tags->chunk_id;
300 tags.n_bytes_lsb = ext_tags->n_bytes & 0x3ff;
302 if (dev->data_bytes_per_chunk >= 1024)
303 tags.n_bytes_msb = (ext_tags->n_bytes >> 10) & 3;
305 tags.n_bytes_msb = 3;
307 tags.serial_number = ext_tags->serial_number;
309 if (!dev->param.use_nand_ecc && data)
310 yaffs_calc_ecc(data, &spare);
312 yaffs_load_tags_to_spare(&spare, &tags);
316 return yaffs_wr_nand(dev, nand_chunk, data, &spare);
319 int yaffs_tags_compat_rd(struct yaffs_dev *dev,
321 u8 * data, struct yaffs_ext_tags *ext_tags)
324 struct yaffs_spare spare;
325 struct yaffs_tags tags;
326 enum yaffs_ecc_result ecc_result = YAFFS_ECC_RESULT_UNKNOWN;
328 static struct yaffs_spare spare_ff;
332 memset(&spare_ff, 0xFF, sizeof(spare_ff));
336 if (yaffs_rd_chunk_nand(dev, nand_chunk, data, &spare, &ecc_result, 1)) {
337 /* ext_tags may be NULL */
341 (hweight8(spare.page_status) < 7) ? 1 : 0;
343 ext_tags->is_deleted = deleted;
344 ext_tags->ecc_result = ecc_result;
345 ext_tags->block_bad = 0; /* We're reading it */
346 /* therefore it is not a bad block */
347 ext_tags->chunk_used =
348 (memcmp(&spare_ff, &spare, sizeof(spare_ff)) !=
351 if (ext_tags->chunk_used) {
352 yaffs_get_tags_from_spare(dev, &spare, &tags);
354 ext_tags->obj_id = tags.obj_id;
355 ext_tags->chunk_id = tags.chunk_id;
356 ext_tags->n_bytes = tags.n_bytes_lsb;
358 if (dev->data_bytes_per_chunk >= 1024)
363 ext_tags->serial_number = tags.serial_number;
373 int yaffs_tags_compat_mark_bad(struct yaffs_dev *dev, int flash_block)
376 struct yaffs_spare spare;
378 memset(&spare, 0xff, sizeof(struct yaffs_spare));
380 spare.block_status = 'Y';
382 yaffs_wr_nand(dev, flash_block * dev->param.chunks_per_block, NULL,
384 yaffs_wr_nand(dev, flash_block * dev->param.chunks_per_block + 1,
391 int yaffs_tags_compat_query_block(struct yaffs_dev *dev,
393 enum yaffs_block_state *state,
397 struct yaffs_spare spare0, spare1;
398 static struct yaffs_spare spare_ff;
400 enum yaffs_ecc_result dummy;
403 memset(&spare_ff, 0xFF, sizeof(spare_ff));
409 yaffs_rd_chunk_nand(dev, block_no * dev->param.chunks_per_block, NULL,
411 yaffs_rd_chunk_nand(dev, block_no * dev->param.chunks_per_block + 1,
412 NULL, &spare1, &dummy, 1);
414 if (hweight8(spare0.block_status & spare1.block_status) < 7)
415 *state = YAFFS_BLOCK_STATE_DEAD;
416 else if (memcmp(&spare_ff, &spare0, sizeof(spare_ff)) == 0)
417 *state = YAFFS_BLOCK_STATE_EMPTY;
419 *state = YAFFS_BLOCK_STATE_NEEDS_SCANNING;