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