2 * YAFFS: Yet Another Flash File System. A NAND-flash specific file system.
4 * Copyright (C) 2002-2011 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.
13 * This file handles yaffs1-style tags to allow compatibility with Yaffs1 style
17 #include "yaffs_guts.h"
18 #include "yaffs_tagscompat.h"
19 #include "yaffs_ecc.h"
20 #include "yaffs_getblockinfo.h"
21 #include "yaffs_trace.h"
22 #include "yaffs_endian.h"
24 static void yaffs_handle_rd_data_error(struct yaffs_dev *dev, int nand_chunk);
27 /********** Tags ECC calculations *********/
29 void yaffs_calc_tags_ecc(struct yaffs_tags *tags)
31 /* Calculate an ecc */
32 unsigned char *b = ((union yaffs_tags_union *)tags)->as_bytes;
39 for (i = 0; i < 8; i++) {
40 for (j = 1; j & 0xff; j <<= 1) {
49 int yaffs_check_tags_ecc(struct yaffs_tags *tags)
51 unsigned ecc = tags->ecc;
53 yaffs_calc_tags_ecc(tags);
57 if (ecc && ecc <= 64) {
58 /* TODO: Handle the failure better. Retire? */
59 unsigned char *b = ((union yaffs_tags_union *)tags)->as_bytes;
63 b[ecc / 8] ^= (1 << (ecc & 7));
65 /* Now recvalc the ecc */
66 yaffs_calc_tags_ecc(tags);
68 return 1; /* recovered error */
70 /* Wierd ecc failure value */
71 /* TODO Need to do somethiong here */
72 return -1; /* unrecovered error */
77 /********** Tags **********/
80 * During tags storing/retireval we use a copy of the tags so that
81 * we can modify the endian etc without damaging the previous structure.
83 static void yaffs_load_tags_to_spare(struct yaffs_dev *dev,
84 struct yaffs_spare *spare_ptr,
85 struct yaffs_tags *tags_ptr)
87 union yaffs_tags_union *tu_ptr = (union yaffs_tags_union *)tags_ptr;
88 union yaffs_tags_union tags_stored = *tu_ptr;
90 yaffs_calc_tags_ecc(&tags_stored.as_tags);
92 yaffs_do_endian_u32(dev, &tags_stored.as_u32[0]);
93 yaffs_do_endian_u32(dev, &tags_stored.as_u32[1]);
95 spare_ptr->tb0 = tags_stored.as_bytes[0];
96 spare_ptr->tb1 = tags_stored.as_bytes[1];
97 spare_ptr->tb2 = tags_stored.as_bytes[2];
98 spare_ptr->tb3 = tags_stored.as_bytes[3];
99 spare_ptr->tb4 = tags_stored.as_bytes[4];
100 spare_ptr->tb5 = tags_stored.as_bytes[5];
101 spare_ptr->tb6 = tags_stored.as_bytes[6];
102 spare_ptr->tb7 = tags_stored.as_bytes[7];
105 static void yaffs_get_tags_from_spare(struct yaffs_dev *dev,
106 struct yaffs_spare *spare_ptr,
107 struct yaffs_tags *tags_ptr)
109 union yaffs_tags_union *tu = (union yaffs_tags_union *)tags_ptr;
110 union yaffs_tags_union tags_stored;
113 tags_stored.as_bytes[0] = spare_ptr->tb0;
114 tags_stored.as_bytes[1] = spare_ptr->tb1;
115 tags_stored.as_bytes[2] = spare_ptr->tb2;
116 tags_stored.as_bytes[3] = spare_ptr->tb3;
117 tags_stored.as_bytes[4] = spare_ptr->tb4;
118 tags_stored.as_bytes[5] = spare_ptr->tb5;
119 tags_stored.as_bytes[6] = spare_ptr->tb6;
120 tags_stored.as_bytes[7] = spare_ptr->tb7;
122 yaffs_do_endian_u32(dev, &tags_stored.as_u32[0]);
123 yaffs_do_endian_u32(dev, &tags_stored.as_u32[1]);
127 result = yaffs_check_tags_ecc(tags_ptr);
129 dev->n_tags_ecc_fixed++;
131 dev->n_tags_ecc_unfixed++;
134 static void yaffs_spare_init(struct yaffs_spare *spare)
136 memset(spare, 0xff, sizeof(struct yaffs_spare));
139 static int yaffs_wr_nand(struct yaffs_dev *dev,
140 int nand_chunk, const u8 *data,
141 struct yaffs_spare *spare)
143 int data_size = dev->data_bytes_per_chunk;
145 return dev->drv.drv_write_chunk_fn(dev, nand_chunk,
147 (u8 *) spare, sizeof(*spare));
150 static int yaffs_rd_chunk_nand(struct yaffs_dev *dev,
153 struct yaffs_spare *spare,
154 enum yaffs_ecc_result *ecc_result,
158 struct yaffs_spare local_spare;
161 int ecc_result1, ecc_result2;
165 /* If we don't have a real spare, then we use a local one. */
166 /* Need this for the calculation of the ecc */
167 spare = &local_spare;
169 data_size = dev->data_bytes_per_chunk;
170 spare_size = sizeof(struct yaffs_spare);
172 if (dev->param.use_nand_ecc)
173 return dev->drv.drv_read_chunk_fn(dev, nand_chunk,
175 (u8 *) spare, spare_size,
179 /* Handle the ECC at this level. */
181 ret_val = dev->drv.drv_read_chunk_fn(dev, nand_chunk,
183 (u8 *)spare, spare_size,
185 if (!data || !correct_errors)
188 /* Do ECC correction if needed. */
189 yaffs_ecc_calc(data, calc_ecc);
190 ecc_result1 = yaffs_ecc_correct(data, spare->ecc1, calc_ecc);
191 yaffs_ecc_calc(&data[256], calc_ecc);
192 ecc_result2 = yaffs_ecc_correct(&data[256], spare->ecc2, calc_ecc);
194 if (ecc_result1 > 0) {
195 yaffs_trace(YAFFS_TRACE_ERROR,
196 "**>>yaffs ecc error fix performed on chunk %d:0",
199 } else if (ecc_result1 < 0) {
200 yaffs_trace(YAFFS_TRACE_ERROR,
201 "**>>yaffs ecc error unfixed on chunk %d:0",
203 dev->n_ecc_unfixed++;
206 if (ecc_result2 > 0) {
207 yaffs_trace(YAFFS_TRACE_ERROR,
208 "**>>yaffs ecc error fix performed on chunk %d:1",
211 } else if (ecc_result2 < 0) {
212 yaffs_trace(YAFFS_TRACE_ERROR,
213 "**>>yaffs ecc error unfixed on chunk %d:1",
215 dev->n_ecc_unfixed++;
218 if (ecc_result1 || ecc_result2) {
219 /* We had a data problem on this page */
220 yaffs_handle_rd_data_error(dev, nand_chunk);
223 if (ecc_result1 < 0 || ecc_result2 < 0)
224 *ecc_result = YAFFS_ECC_RESULT_UNFIXED;
225 else if (ecc_result1 > 0 || ecc_result2 > 0)
226 *ecc_result = YAFFS_ECC_RESULT_FIXED;
228 *ecc_result = YAFFS_ECC_RESULT_NO_ERROR;
234 * Functions for robustisizing
237 static void yaffs_handle_rd_data_error(struct yaffs_dev *dev, int nand_chunk)
239 int flash_block = nand_chunk / dev->param.chunks_per_block;
241 /* Mark the block for retirement */
242 yaffs_get_block_info(dev, flash_block + dev->block_offset)->
244 yaffs_trace(YAFFS_TRACE_ERROR | YAFFS_TRACE_BAD_BLOCKS,
245 "**>>Block %d marked for retirement",
249 * Just do a garbage collection on the affected block
250 * then retire the block
255 static int yaffs_tags_compat_wr(struct yaffs_dev *dev,
257 const u8 *data, const struct yaffs_ext_tags *ext_tags)
259 struct yaffs_spare spare;
260 struct yaffs_tags tags;
262 yaffs_spare_init(&spare);
264 if (ext_tags->is_deleted)
265 spare.page_status = 0;
267 tags.obj_id = ext_tags->obj_id;
268 tags.chunk_id = ext_tags->chunk_id;
270 tags.n_bytes_lsb = ext_tags->n_bytes & (1024 - 1);
272 if (dev->data_bytes_per_chunk >= 1024)
273 tags.n_bytes_msb = (ext_tags->n_bytes >> 10) & 3;
275 tags.n_bytes_msb = 3;
277 tags.serial_number = ext_tags->serial_number;
279 if (!dev->param.use_nand_ecc && data) {
280 yaffs_ecc_calc(data, spare.ecc1);
281 yaffs_ecc_calc(&data[256], spare.ecc2);
284 yaffs_load_tags_to_spare(dev, &spare, &tags);
286 return yaffs_wr_nand(dev, nand_chunk, data, &spare);
289 static int yaffs_tags_compat_rd(struct yaffs_dev *dev,
291 u8 *data, struct yaffs_ext_tags *ext_tags)
293 struct yaffs_spare spare;
294 struct yaffs_tags tags;
295 enum yaffs_ecc_result ecc_result = YAFFS_ECC_RESULT_UNKNOWN;
296 static struct yaffs_spare spare_ff;
301 memset(&spare_ff, 0xff, sizeof(spare_ff));
305 if (!yaffs_rd_chunk_nand(dev, nand_chunk,
306 data, &spare, &ecc_result, 1))
309 /* ext_tags may be NULL */
313 deleted = (hweight8(spare.page_status) < 7) ? 1 : 0;
315 ext_tags->is_deleted = deleted;
316 ext_tags->ecc_result = ecc_result;
317 ext_tags->block_bad = 0; /* We're reading it */
318 /* therefore it is not a bad block */
319 ext_tags->chunk_used =
320 memcmp(&spare_ff, &spare, sizeof(spare_ff)) ? 1 : 0;
322 if (ext_tags->chunk_used) {
323 yaffs_get_tags_from_spare(dev, &spare, &tags);
325 ext_tags->obj_id = tags.obj_id;
326 ext_tags->chunk_id = tags.chunk_id;
327 ext_tags->n_bytes = tags.n_bytes_lsb;
329 if (dev->data_bytes_per_chunk >= 1024)
331 (((unsigned)tags.n_bytes_msb) << 10);
333 ext_tags->serial_number = tags.serial_number;
339 static int yaffs_tags_compat_mark_bad(struct yaffs_dev *dev, int flash_block)
341 struct yaffs_spare spare;
343 memset(&spare, 0xff, sizeof(struct yaffs_spare));
345 spare.block_status = 'Y';
347 yaffs_wr_nand(dev, flash_block * dev->param.chunks_per_block, NULL,
349 yaffs_wr_nand(dev, flash_block * dev->param.chunks_per_block + 1,
355 static int yaffs_tags_compat_query_block(struct yaffs_dev *dev,
357 enum yaffs_block_state *state,
360 struct yaffs_spare spare0, spare1;
361 static struct yaffs_spare spare_ff;
363 enum yaffs_ecc_result dummy;
366 memset(&spare_ff, 0xff, sizeof(spare_ff));
372 /* Look for bad block markers in the first two chunks */
373 yaffs_rd_chunk_nand(dev, block_no * dev->param.chunks_per_block,
374 NULL, &spare0, &dummy, 0);
375 yaffs_rd_chunk_nand(dev, block_no * dev->param.chunks_per_block + 1,
376 NULL, &spare1, &dummy, 0);
378 if (hweight8(spare0.block_status & spare1.block_status) < 7)
379 *state = YAFFS_BLOCK_STATE_DEAD;
380 else if (memcmp(&spare_ff, &spare0, sizeof(spare_ff)) == 0)
381 *state = YAFFS_BLOCK_STATE_EMPTY;
383 *state = YAFFS_BLOCK_STATE_NEEDS_SCAN;
388 void yaffs_tags_compat_install(struct yaffs_dev *dev)
390 if(dev->param.is_yaffs2)
392 if(!dev->tagger.write_chunk_tags_fn)
393 dev->tagger.write_chunk_tags_fn = yaffs_tags_compat_wr;
394 if(!dev->tagger.read_chunk_tags_fn)
395 dev->tagger.read_chunk_tags_fn = yaffs_tags_compat_rd;
396 if(!dev->tagger.query_block_fn)
397 dev->tagger.query_block_fn = yaffs_tags_compat_query_block;
398 if(!dev->tagger.mark_bad_fn)
399 dev->tagger.mark_bad_fn = yaffs_tags_compat_mark_bad;