yaffs Have revised and checked each test in quick tests.
[yaffs2.git] / yaffs_mtdif2_single.c
1 /*
2  * YAFFS: Yet Another Flash File System. 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 /* mtd interface for YAFFS2 */
15
16 #include "yportenv.h"
17 #include "yaffs_trace.h"
18
19 #include "yaffs_mtdif2.h"
20
21 #include "linux/mtd/mtd.h"
22 #include "linux/types.h"
23 #include "linux/time.h"
24
25 #include "yaffs_packedtags2.h"
26
27 #include "yaffs_linux.h"
28
29 /* NB For use with inband tags....
30  * We assume that the data buffer is of size total_bytes_per_chunk so that we can also
31  * use it to load the tags.
32  */
33 int nandmtd2_write_chunk_tags(struct yaffs_dev *dev, int nand_chunk,
34                               const u8 * data,
35                               const struct yaffs_ext_tags *tags)
36 {
37         struct mtd_info *mtd = yaffs_dev_to_mtd(dev);
38         struct mtd_oob_ops ops;
39         int retval = 0;
40
41         loff_t addr;
42
43         struct yaffs_packed_tags2 pt;
44
45         int packed_tags_size =
46             dev->param.no_tags_ecc ? sizeof(pt.t) : sizeof(pt);
47         void *packed_tags_ptr =
48             dev->param.no_tags_ecc ? (void *)&pt.t : (void *)&pt;
49
50         T(YAFFS_TRACE_MTD,
51           (TSTR
52            ("nandmtd2_write_chunk_tags chunk %d data %p tags %p"
53             TENDSTR), nand_chunk, data, tags));
54
55         addr = ((loff_t) nand_chunk) * dev->param.total_bytes_per_chunk;
56
57         /* For yaffs2 writing there must be both data and tags.
58          * If we're using inband tags, then the tags are stuffed into
59          * the end of the data buffer.
60          */
61         if (!data || !tags)
62                 BUG();
63         else if (dev->param.inband_tags) {
64                 struct yaffs_packed_tags2_tags_only *pt2tp;
65                 pt2tp =
66                     (struct yaffs_packed_tags2_tags_only *)(data +
67                                                             dev->
68                                                             data_bytes_per_chunk);
69                 yaffs_pack_tags2_tags_only(pt2tp, tags);
70         } else {
71                 yaffs_pack_tags2(&pt, tags, !dev->param.no_tags_ecc);
72         }
73
74         ops.mode = MTD_OOB_AUTO;
75         ops.ooblen = (dev->param.inband_tags) ? 0 : packed_tags_size;
76         ops.len = dev->param.total_bytes_per_chunk;
77         ops.ooboffs = 0;
78         ops.datbuf = (u8 *) data;
79         ops.oobbuf = (dev->param.inband_tags) ? NULL : packed_tags_ptr;
80         retval = mtd->write_oob(mtd, addr, &ops);
81
82         if (retval == 0)
83                 return YAFFS_OK;
84         else
85                 return YAFFS_FAIL;
86 }
87
88 int nandmtd2_read_chunk_tags(struct yaffs_dev *dev, int nand_chunk,
89                              u8 * data, struct yaffs_ext_tags *tags)
90 {
91         struct mtd_info *mtd = yaffs_dev_to_mtd(dev);
92         struct mtd_oob_ops ops;
93
94         size_t dummy;
95         int retval = 0;
96         int local_data = 0;
97
98         loff_t addr = ((loff_t) nand_chunk) * dev->param.total_bytes_per_chunk;
99
100         struct yaffs_packed_tags2 pt;
101
102         int packed_tags_size =
103             dev->param.no_tags_ecc ? sizeof(pt.t) : sizeof(pt);
104         void *packed_tags_ptr =
105             dev->param.no_tags_ecc ? (void *)&pt.t : (void *)&pt;
106
107         T(YAFFS_TRACE_MTD,
108           (TSTR
109            ("nandmtd2_read_chunk_tags chunk %d data %p tags %p"
110             TENDSTR), nand_chunk, data, tags));
111
112         if (dev->param.inband_tags) {
113
114                 if (!data) {
115                         local_data = 1;
116                         data = yaffs_get_temp_buffer(dev, __LINE__);
117                 }
118
119         }
120
121         if (dev->param.inband_tags || (data && !tags))
122                 retval = mtd->read(mtd, addr, dev->param.total_bytes_per_chunk,
123                                    &dummy, data);
124         else if (tags) {
125                 ops.mode = MTD_OOB_AUTO;
126                 ops.ooblen = packed_tags_size;
127                 ops.len = data ? dev->data_bytes_per_chunk : packed_tags_size;
128                 ops.ooboffs = 0;
129                 ops.datbuf = data;
130                 ops.oobbuf = yaffs_dev_to_lc(dev)->spare_buffer;
131                 retval = mtd->read_oob(mtd, addr, &ops);
132         }
133
134         if (dev->param.inband_tags) {
135                 if (tags) {
136                         struct yaffs_packed_tags2_tags_only *pt2tp;
137                         pt2tp =
138                             (struct yaffs_packed_tags2_tags_only *)&data[dev->
139                                                                          data_bytes_per_chunk];
140                         yaffs_unpack_tags2_tags_only(tags, pt2tp);
141                 }
142         } else {
143                 if (tags) {
144                         memcpy(packed_tags_ptr,
145                                yaffs_dev_to_lc(dev)->spare_buffer,
146                                packed_tags_size);
147                         yaffs_unpack_tags2(tags, &pt, !dev->param.no_tags_ecc);
148                 }
149         }
150
151         if (local_data)
152                 yaffs_release_temp_buffer(dev, data, __LINE__);
153
154         if (tags && retval == -EBADMSG
155             && tags->ecc_result == YAFFS_ECC_RESULT_NO_ERROR) {
156                 tags->ecc_result = YAFFS_ECC_RESULT_UNFIXED;
157                 dev->n_ecc_unfixed++;
158         }
159         if (tags && retval == -EUCLEAN
160             && tags->ecc_result == YAFFS_ECC_RESULT_NO_ERROR) {
161                 tags->ecc_result = YAFFS_ECC_RESULT_FIXED;
162                 dev->n_ecc_fixed++;
163         }
164         if (retval == 0)
165                 return YAFFS_OK;
166         else
167                 return YAFFS_FAIL;
168 }
169
170 int nandmtd2_mark_block_bad(struct yaffs_dev *dev, int block_no)
171 {
172         struct mtd_info *mtd = yaffs_dev_to_mtd(dev);
173         int retval;
174         T(YAFFS_TRACE_MTD,
175           (TSTR("nandmtd2_mark_block_bad %d" TENDSTR), block_no));
176
177         retval =
178             mtd->block_markbad(mtd,
179                                block_no * dev->param.chunks_per_block *
180                                dev->param.total_bytes_per_chunk);
181
182         if (retval == 0)
183                 return YAFFS_OK;
184         else
185                 return YAFFS_FAIL;
186
187 }
188
189 int nandmtd2_query_block(struct yaffs_dev *dev, int block_no,
190                          enum yaffs_block_state *state, u32 * seq_number)
191 {
192         struct mtd_info *mtd = yaffs_dev_to_mtd(dev);
193         int retval;
194
195         T(YAFFS_TRACE_MTD, (TSTR("nandmtd2_query_block %d" TENDSTR), block_no));
196         retval =
197             mtd->block_isbad(mtd,
198                              block_no * dev->param.chunks_per_block *
199                              dev->param.total_bytes_per_chunk);
200
201         if (retval) {
202                 T(YAFFS_TRACE_MTD, (TSTR("block is bad" TENDSTR)));
203
204                 *state = YAFFS_BLOCK_STATE_DEAD;
205                 *seq_number = 0;
206         } else {
207                 struct yaffs_ext_tags t;
208                 nandmtd2_read_chunk_tags(dev, block_no *
209                                          dev->param.chunks_per_block, NULL, &t);
210
211                 if (t.chunk_used) {
212                         *seq_number = t.seq_number;
213                         *state = YAFFS_BLOCK_STATE_NEEDS_SCANNING;
214                 } else {
215                         *seq_number = 0;
216                         *state = YAFFS_BLOCK_STATE_EMPTY;
217                 }
218         }
219         T(YAFFS_TRACE_MTD,
220           (TSTR("block is bad seq %d state %d" TENDSTR), *seq_number, *state));
221
222         if (retval == 0)
223                 return YAFFS_OK;
224         else
225                 return YAFFS_FAIL;
226 }