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