yaffs Have revised and checked each test in quick tests.
[yaffs2.git] / yaffs_mtdif2_multi.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 #if (MTD_VERSION_CODE > MTD_VERSION(2, 6, 17))
39         struct mtd_oob_ops ops;
40 #else
41         size_t dummy;
42 #endif
43         int retval = 0;
44
45         loff_t addr;
46
47         struct yaffs_packed_tags2 pt;
48
49         int packed_tags_size =
50             dev->param.no_tags_ecc ? sizeof(pt.t) : sizeof(pt);
51         void *packed_tags_ptr =
52             dev->param.no_tags_ecc ? (void *)&pt.t : (void *)&pt;
53
54         T(YAFFS_TRACE_MTD,
55           (TSTR
56            ("nandmtd2_write_chunk_tags chunk %d data %p tags %p"
57             TENDSTR), nand_chunk, data, tags));
58
59         addr = ((loff_t) nand_chunk) * dev->param.total_bytes_per_chunk;
60
61         /* For yaffs2 writing there must be both data and tags.
62          * If we're using inband tags, then the tags are stuffed into
63          * the end of the data buffer.
64          */
65         if (!data || !tags)
66                 BUG();
67         else if (dev->param.inband_tags) {
68                 struct yaffs_packed_tags2_tags_only *pt2tp;
69                 pt2tp =
70                     (struct yaffs_packed_tags2_tags_only *)(data +
71                                                             dev->
72                                                             data_bytes_per_chunk);
73                 yaffs_pack_tags2_tags_only(pt2tp, tags);
74         } else {
75                 yaffs_pack_tags2(&pt, tags, !dev->param.no_tags_ecc);
76         }
77
78 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
79         ops.mode = MTD_OOB_AUTO;
80         ops.ooblen = (dev->param.inband_tags) ? 0 : packed_tags_size;
81         ops.len = dev->param.total_bytes_per_chunk;
82         ops.ooboffs = 0;
83         ops.datbuf = (u8 *) data;
84         ops.oobbuf = (dev->param.inband_tags) ? NULL : packed_tags_ptr;
85         retval = mtd->write_oob(mtd, addr, &ops);
86
87 #else
88         if (!dev->param.inband_tags) {
89                 retval =
90                     mtd->write_ecc(mtd, addr, dev->data_bytes_per_chunk,
91                                    &dummy, data, (u8 *) packed_tags_ptr, NULL);
92         } else {
93                 retval =
94                     mtd->write(mtd, addr, dev->param.total_bytes_per_chunk,
95                                &dummy, data);
96         }
97 #endif
98
99         if (retval == 0)
100                 return YAFFS_OK;
101         else
102                 return YAFFS_FAIL;
103 }
104
105 int nandmtd2_read_chunk_tags(struct yaffs_dev *dev, int nand_chunk,
106                              u8 * data, struct yaffs_ext_tags *tags)
107 {
108         struct mtd_info *mtd = yaffs_dev_to_mtd(dev);
109 #if (MTD_VERSION_CODE > MTD_VERSION(2, 6, 17))
110         struct mtd_oob_ops ops;
111 #endif
112         size_t dummy;
113         int retval = 0;
114         int local_data = 0;
115
116         loff_t addr = ((loff_t) nand_chunk) * dev->param.total_bytes_per_chunk;
117
118         struct yaffs_packed_tags2 pt;
119
120         int packed_tags_size =
121             dev->param.no_tags_ecc ? sizeof(pt.t) : sizeof(pt);
122         void *packed_tags_ptr =
123             dev->param.no_tags_ecc ? (void *)&pt.t : (void *)&pt;
124
125         T(YAFFS_TRACE_MTD,
126           (TSTR
127            ("nandmtd2_read_chunk_tags chunk %d data %p tags %p"
128             TENDSTR), nand_chunk, data, tags));
129
130         if (dev->param.inband_tags) {
131
132                 if (!data) {
133                         local_data = 1;
134                         data = yaffs_get_temp_buffer(dev, __LINE__);
135                 }
136
137         }
138
139 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
140         if (dev->param.inband_tags || (data && !tags))
141                 retval = mtd->read(mtd, addr, dev->param.total_bytes_per_chunk,
142                                    &dummy, data);
143         else if (tags) {
144                 ops.mode = MTD_OOB_AUTO;
145                 ops.ooblen = packed_tags_size;
146                 ops.len = data ? dev->data_bytes_per_chunk : packed_tags_size;
147                 ops.ooboffs = 0;
148                 ops.datbuf = data;
149                 ops.oobbuf = yaffs_dev_to_lc(dev)->spare_buffer;
150                 retval = mtd->read_oob(mtd, addr, &ops);
151         }
152 #else
153         if (!dev->param.inband_tags && data && tags) {
154
155                 retval = mtd->read_ecc(mtd, addr, dev->data_bytes_per_chunk,
156                                        &dummy, data, dev->spare_buffer, NULL);
157         } else {
158                 if (data)
159                         retval =
160                             mtd->read(mtd, addr, dev->data_bytes_per_chunk,
161                                       &dummy, data);
162                 if (!dev->param.inband_tags && tags)
163                         retval =
164                             mtd->read_oob(mtd, addr, mtd->oobsize, &dummy,
165                                           dev->spare_buffer);
166         }
167 #endif
168
169         if (dev->param.inband_tags) {
170                 if (tags) {
171                         struct yaffs_packed_tags2_tags_only *pt2tp;
172                         pt2tp =
173                             (struct yaffs_packed_tags2_tags_only *)&data[dev->
174                                                                          data_bytes_per_chunk];
175                         yaffs_unpack_tags2_tags_only(tags, pt2tp);
176                 }
177         } else {
178                 if (tags) {
179                         memcpy(packed_tags_ptr,
180                                yaffs_dev_to_lc(dev)->spare_buffer,
181                                packed_tags_size);
182                         yaffs_unpack_tags2(tags, &pt, !dev->param.no_tags_ecc);
183                 }
184         }
185
186         if (local_data)
187                 yaffs_release_temp_buffer(dev, data, __LINE__);
188
189         if (tags && retval == -EBADMSG
190             && tags->ecc_result == YAFFS_ECC_RESULT_NO_ERROR) {
191                 tags->ecc_result = YAFFS_ECC_RESULT_UNFIXED;
192                 dev->n_ecc_unfixed++;
193         }
194         if (tags && retval == -EUCLEAN
195             && tags->ecc_result == YAFFS_ECC_RESULT_NO_ERROR) {
196                 tags->ecc_result = YAFFS_ECC_RESULT_FIXED;
197                 dev->n_ecc_fixed++;
198         }
199         if (retval == 0)
200                 return YAFFS_OK;
201         else
202                 return YAFFS_FAIL;
203 }
204
205 int nandmtd2_mark_block_bad(struct yaffs_dev *dev, int block_no)
206 {
207         struct mtd_info *mtd = yaffs_dev_to_mtd(dev);
208         int retval;
209         T(YAFFS_TRACE_MTD,
210           (TSTR("nandmtd2_mark_block_bad %d" TENDSTR), block_no));
211
212         retval =
213             mtd->block_markbad(mtd,
214                                block_no * dev->param.chunks_per_block *
215                                dev->param.total_bytes_per_chunk);
216
217         if (retval == 0)
218                 return YAFFS_OK;
219         else
220                 return YAFFS_FAIL;
221
222 }
223
224 int nandmtd2_query_block(struct yaffs_dev *dev, int block_no,
225                          enum yaffs_block_state *state, u32 * seq_number)
226 {
227         struct mtd_info *mtd = yaffs_dev_to_mtd(dev);
228         int retval;
229
230         T(YAFFS_TRACE_MTD, (TSTR("nandmtd2_query_block %d" TENDSTR), block_no));
231         retval =
232             mtd->block_isbad(mtd,
233                              block_no * dev->param.chunks_per_block *
234                              dev->param.total_bytes_per_chunk);
235
236         if (retval) {
237                 T(YAFFS_TRACE_MTD, (TSTR("block is bad" TENDSTR)));
238
239                 *state = YAFFS_BLOCK_STATE_DEAD;
240                 *seq_number = 0;
241         } else {
242                 struct yaffs_ext_tags t;
243                 nandmtd2_read_chunk_tags(dev, block_no *
244                                          dev->param.chunks_per_block, NULL, &t);
245
246                 if (t.chunk_used) {
247                         *seq_number = t.seq_number;
248                         *state = YAFFS_BLOCK_STATE_NEEDS_SCANNING;
249                 } else {
250                         *seq_number = 0;
251                         *state = YAFFS_BLOCK_STATE_EMPTY;
252                 }
253         }
254         T(YAFFS_TRACE_MTD,
255           (TSTR("block is bad seq %d state %d" TENDSTR), *seq_number, *state));
256
257         if (retval == 0)
258                 return YAFFS_OK;
259         else
260                 return YAFFS_FAIL;
261 }