blob: c449babb74b9e1dbc13b42a3dc3d7744262bef73 [file] [log] [blame]
Arve Hjønnevåg3de69a72010-05-18 20:35:30 -07001/*
2 * YAFFS: Yet another FFS. 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/*
15 * This module provides the interface between yaffs_nand.c and the
16 * MTD API. This version is used when the MTD interface supports the
17 * 'mtd_oob_ops' style calls to read_oob and write_oob, circa 2.6.17,
18 * and we have small-page NAND device.
19 *
20 * These functions are invoked via function pointers in yaffs_nand.c.
21 * This replaces functionality provided by functions in yaffs_mtdif.c
22 * and the yaffs_tags compatability functions in yaffs_tagscompat.c that are
23 * called in yaffs_mtdif.c when the function pointers are NULL.
24 * We assume the MTD layer is performing ECC (use_nand_ecc is true).
25 */
26
27#include "yportenv.h"
28#include "yaffs_trace.h"
29#include "yaffs_guts.h"
30#include "yaffs_packedtags1.h"
31#include "yaffs_tagscompat.h" /* for yaffs_calc_tags_ecc */
32#include "yaffs_linux.h"
33
34#include "linux/kernel.h"
35#include "linux/version.h"
36#include "linux/types.h"
37#include "linux/mtd/mtd.h"
38
39#ifndef CONFIG_YAFFS_9BYTE_TAGS
40# define YTAG1_SIZE 8
41#else
42# define YTAG1_SIZE 9
43#endif
44
45/* Write a chunk (page) of data to NAND.
46 *
47 * Caller always provides ExtendedTags data which are converted to a more
48 * compact (packed) form for storage in NAND. A mini-ECC runs over the
49 * contents of the tags meta-data; used to valid the tags when read.
50 *
51 * - Pack ExtendedTags to packed_tags1 form
52 * - Compute mini-ECC for packed_tags1
53 * - Write data and packed tags to NAND.
54 *
55 * Note: Due to the use of the packed_tags1 meta-data which does not include
56 * a full sequence number (as found in the larger packed_tags2 form) it is
57 * necessary for Yaffs to re-write a chunk/page (just once) to mark it as
58 * discarded and dirty. This is not ideal: newer NAND parts are supposed
59 * to be written just once. When Yaffs performs this operation, this
60 * function is called with a NULL data pointer -- calling MTD write_oob
61 * without data is valid usage (2.6.17).
62 *
63 * Any underlying MTD error results in YAFFS_FAIL.
64 * Returns YAFFS_OK or YAFFS_FAIL.
65 */
66int nandmtd1_write_chunk_tags(struct yaffs_dev *dev,
67 int nand_chunk, const u8 * data,
68 const struct yaffs_ext_tags *etags)
69{
70 struct mtd_info *mtd = yaffs_dev_to_mtd(dev);
71 int chunk_bytes = dev->data_bytes_per_chunk;
72 loff_t addr = ((loff_t) nand_chunk) * chunk_bytes;
73 struct mtd_oob_ops ops;
74 struct yaffs_packed_tags1 pt1;
75 int retval;
76
77 /* we assume that packed_tags1 and struct yaffs_tags are compatible */
78 compile_time_assertion(sizeof(struct yaffs_packed_tags1) == 12);
79 compile_time_assertion(sizeof(struct yaffs_tags) == 8);
80
81 yaffs_pack_tags1(&pt1, etags);
82 yaffs_calc_tags_ecc((struct yaffs_tags *)&pt1);
83
84 /* When deleting a chunk, the upper layer provides only skeletal
85 * etags, one with is_deleted set. However, we need to update the
86 * tags, not erase them completely. So we use the NAND write property
87 * that only zeroed-bits stick and set tag bytes to all-ones and
88 * zero just the (not) deleted bit.
89 */
90#ifndef CONFIG_YAFFS_9BYTE_TAGS
91 if (etags->is_deleted) {
92 memset(&pt1, 0xff, 8);
93 /* clear delete status bit to indicate deleted */
94 pt1.deleted = 0;
95 }
96#else
97 ((u8 *) & pt1)[8] = 0xff;
98 if (etags->is_deleted) {
99 memset(&pt1, 0xff, 8);
100 /* zero page_status byte to indicate deleted */
101 ((u8 *) & pt1)[8] = 0;
102 }
103#endif
104
105 memset(&ops, 0, sizeof(ops));
Steve Mucklef132c6c2012-06-06 18:30:57 -0700106 ops.mode = MTD_OPS_AUTO_OOB;
Arve Hjønnevåg3de69a72010-05-18 20:35:30 -0700107 ops.len = (data) ? chunk_bytes : 0;
108 ops.ooblen = YTAG1_SIZE;
109 ops.datbuf = (u8 *) data;
110 ops.oobbuf = (u8 *) & pt1;
111
Steve Mucklef132c6c2012-06-06 18:30:57 -0700112 retval = mtd_write_oob(mtd, addr, &ops);
Arve Hjønnevåg3de69a72010-05-18 20:35:30 -0700113 if (retval) {
114 yaffs_trace(YAFFS_TRACE_MTD,
115 "write_oob failed, chunk %d, mtd error %d",
116 nand_chunk, retval);
117 }
118 return retval ? YAFFS_FAIL : YAFFS_OK;
119}
120
121/* Return with empty ExtendedTags but add ecc_result.
122 */
123static int rettags(struct yaffs_ext_tags *etags, int ecc_result, int retval)
124{
125 if (etags) {
126 memset(etags, 0, sizeof(*etags));
127 etags->ecc_result = ecc_result;
128 }
129 return retval;
130}
131
132/* Read a chunk (page) from NAND.
133 *
134 * Caller expects ExtendedTags data to be usable even on error; that is,
135 * all members except ecc_result and block_bad are zeroed.
136 *
137 * - Check ECC results for data (if applicable)
138 * - Check for blank/erased block (return empty ExtendedTags if blank)
139 * - Check the packed_tags1 mini-ECC (correct if necessary/possible)
140 * - Convert packed_tags1 to ExtendedTags
141 * - Update ecc_result and block_bad members to refect state.
142 *
143 * Returns YAFFS_OK or YAFFS_FAIL.
144 */
145int nandmtd1_read_chunk_tags(struct yaffs_dev *dev,
146 int nand_chunk, u8 * data,
147 struct yaffs_ext_tags *etags)
148{
149 struct mtd_info *mtd = yaffs_dev_to_mtd(dev);
150 int chunk_bytes = dev->data_bytes_per_chunk;
151 loff_t addr = ((loff_t) nand_chunk) * chunk_bytes;
152 int eccres = YAFFS_ECC_RESULT_NO_ERROR;
153 struct mtd_oob_ops ops;
154 struct yaffs_packed_tags1 pt1;
155 int retval;
156 int deleted;
157
158 memset(&ops, 0, sizeof(ops));
Steve Mucklef132c6c2012-06-06 18:30:57 -0700159 ops.mode = MTD_OPS_AUTO_OOB;
Arve Hjønnevåg3de69a72010-05-18 20:35:30 -0700160 ops.len = (data) ? chunk_bytes : 0;
161 ops.ooblen = YTAG1_SIZE;
162 ops.datbuf = data;
163 ops.oobbuf = (u8 *) & pt1;
164
165 /* Read page and oob using MTD.
166 * Check status and determine ECC result.
167 */
Steve Mucklef132c6c2012-06-06 18:30:57 -0700168 retval = mtd_read_oob(mtd, addr, &ops);
Arve Hjønnevåg3de69a72010-05-18 20:35:30 -0700169 if (retval) {
170 yaffs_trace(YAFFS_TRACE_MTD,
171 "read_oob failed, chunk %d, mtd error %d",
172 nand_chunk, retval);
173 }
174
175 switch (retval) {
176 case 0:
177 /* no error */
178 break;
179
180 case -EUCLEAN:
181 /* MTD's ECC fixed the data */
182 eccres = YAFFS_ECC_RESULT_FIXED;
183 dev->n_ecc_fixed++;
184 break;
185
186 case -EBADMSG:
187 /* MTD's ECC could not fix the data */
188 dev->n_ecc_unfixed++;
189 /* fall into... */
190 default:
191 rettags(etags, YAFFS_ECC_RESULT_UNFIXED, 0);
Steve Mucklef132c6c2012-06-06 18:30:57 -0700192 etags->block_bad = mtd_block_isbad(mtd, addr);
Arve Hjønnevåg3de69a72010-05-18 20:35:30 -0700193 return YAFFS_FAIL;
194 }
195
196 /* Check for a blank/erased chunk.
197 */
198 if (yaffs_check_ff((u8 *) & pt1, 8)) {
199 /* when blank, upper layers want ecc_result to be <= NO_ERROR */
200 return rettags(etags, YAFFS_ECC_RESULT_NO_ERROR, YAFFS_OK);
201 }
202#ifndef CONFIG_YAFFS_9BYTE_TAGS
203 /* Read deleted status (bit) then return it to it's non-deleted
204 * state before performing tags mini-ECC check. pt1.deleted is
205 * inverted.
206 */
207 deleted = !pt1.deleted;
208 pt1.deleted = 1;
209#else
210 deleted = (yaffs_count_bits(((u8 *) & pt1)[8]) < 7);
211#endif
212
213 /* Check the packed tags mini-ECC and correct if necessary/possible.
214 */
215 retval = yaffs_check_tags_ecc((struct yaffs_tags *)&pt1);
216 switch (retval) {
217 case 0:
218 /* no tags error, use MTD result */
219 break;
220 case 1:
221 /* recovered tags-ECC error */
222 dev->n_tags_ecc_fixed++;
223 if (eccres == YAFFS_ECC_RESULT_NO_ERROR)
224 eccres = YAFFS_ECC_RESULT_FIXED;
225 break;
226 default:
227 /* unrecovered tags-ECC error */
228 dev->n_tags_ecc_unfixed++;
229 return rettags(etags, YAFFS_ECC_RESULT_UNFIXED, YAFFS_FAIL);
230 }
231
232 /* Unpack the tags to extended form and set ECC result.
233 * [set should_be_ff just to keep yaffs_unpack_tags1 happy]
234 */
235 pt1.should_be_ff = 0xFFFFFFFF;
236 yaffs_unpack_tags1(etags, &pt1);
237 etags->ecc_result = eccres;
238
239 /* Set deleted state */
240 etags->is_deleted = deleted;
241 return YAFFS_OK;
242}
243
244/* Mark a block bad.
245 *
246 * This is a persistant state.
247 * Use of this function should be rare.
248 *
249 * Returns YAFFS_OK or YAFFS_FAIL.
250 */
251int nandmtd1_mark_block_bad(struct yaffs_dev *dev, int block_no)
252{
253 struct mtd_info *mtd = yaffs_dev_to_mtd(dev);
254 int blocksize = dev->param.chunks_per_block * dev->data_bytes_per_chunk;
255 int retval;
256
257 yaffs_trace(YAFFS_TRACE_BAD_BLOCKS,
258 "marking block %d bad", block_no);
259
Steve Mucklef132c6c2012-06-06 18:30:57 -0700260 retval = mtd_block_markbad(mtd, (loff_t) blocksize * block_no);
Arve Hjønnevåg3de69a72010-05-18 20:35:30 -0700261 return (retval) ? YAFFS_FAIL : YAFFS_OK;
262}
263
264/* Check any MTD prerequists.
265 *
266 * Returns YAFFS_OK or YAFFS_FAIL.
267 */
268static int nandmtd1_test_prerequists(struct mtd_info *mtd)
269{
270 /* 2.6.18 has mtd->ecclayout->oobavail */
271 /* 2.6.21 has mtd->ecclayout->oobavail and mtd->oobavail */
272 int oobavail = mtd->ecclayout->oobavail;
273
274 if (oobavail < YTAG1_SIZE) {
275 yaffs_trace(YAFFS_TRACE_ERROR,
276 "mtd device has only %d bytes for tags, need %d",
277 oobavail, YTAG1_SIZE);
278 return YAFFS_FAIL;
279 }
280 return YAFFS_OK;
281}
282
283/* Query for the current state of a specific block.
284 *
285 * Examine the tags of the first chunk of the block and return the state:
286 * - YAFFS_BLOCK_STATE_DEAD, the block is marked bad
287 * - YAFFS_BLOCK_STATE_NEEDS_SCANNING, the block is in use
288 * - YAFFS_BLOCK_STATE_EMPTY, the block is clean
289 *
290 * Always returns YAFFS_OK.
291 */
292int nandmtd1_query_block(struct yaffs_dev *dev, int block_no,
293 enum yaffs_block_state *state_ptr, u32 * seq_ptr)
294{
295 struct mtd_info *mtd = yaffs_dev_to_mtd(dev);
296 int chunk_num = block_no * dev->param.chunks_per_block;
297 loff_t addr = (loff_t) chunk_num * dev->data_bytes_per_chunk;
298 struct yaffs_ext_tags etags;
299 int state = YAFFS_BLOCK_STATE_DEAD;
300 int seqnum = 0;
301 int retval;
302
303 /* We don't yet have a good place to test for MTD config prerequists.
304 * Do it here as we are called during the initial scan.
305 */
306 if (nandmtd1_test_prerequists(mtd) != YAFFS_OK)
307 return YAFFS_FAIL;
308
309 retval = nandmtd1_read_chunk_tags(dev, chunk_num, NULL, &etags);
Steve Mucklef132c6c2012-06-06 18:30:57 -0700310 etags.block_bad = mtd_block_isbad(mtd, addr);
Arve Hjønnevåg3de69a72010-05-18 20:35:30 -0700311 if (etags.block_bad) {
312 yaffs_trace(YAFFS_TRACE_BAD_BLOCKS,
313 "block %d is marked bad", block_no);
314 state = YAFFS_BLOCK_STATE_DEAD;
315 } else if (etags.ecc_result != YAFFS_ECC_RESULT_NO_ERROR) {
316 /* bad tags, need to look more closely */
317 state = YAFFS_BLOCK_STATE_NEEDS_SCANNING;
318 } else if (etags.chunk_used) {
319 state = YAFFS_BLOCK_STATE_NEEDS_SCANNING;
320 seqnum = etags.seq_number;
321 } else {
322 state = YAFFS_BLOCK_STATE_EMPTY;
323 }
324
325 *state_ptr = state;
326 *seq_ptr = seqnum;
327
328 /* query always succeeds */
329 return YAFFS_OK;
330}