yaffs Pulled the bugs section out of the README.txt and put it into current_bugs.txt
[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 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
78         ops.mode = MTD_OOB_AUTO;
79         ops.ooblen = (dev->param.inband_tags) ? 0 : packed_tags_size;
80         ops.len = dev->param.total_bytes_per_chunk;
81         ops.ooboffs = 0;
82         ops.datbuf = (u8 *) data;
83         ops.oobbuf = (dev->param.inband_tags) ? NULL : packed_tags_ptr;
84         retval = mtd->write_oob(mtd, addr, &ops);
85
86 #else
87         if (!dev->param.inband_tags) {
88                 retval =
89                     mtd->write_ecc(mtd, addr, dev->data_bytes_per_chunk,
90                                    &dummy, data, (u8 *) packed_tags_ptr, NULL);
91         } else {
92                 retval =
93                     mtd->write(mtd, addr, dev->param.total_bytes_per_chunk,
94                                &dummy, data);
95         }
96 #endif
97
98         if (retval == 0)
99                 return YAFFS_OK;
100         else
101                 return YAFFS_FAIL;
102 }
103
104 int nandmtd2_read_chunk_tags(struct yaffs_dev *dev, int nand_chunk,
105                              u8 * data, struct yaffs_ext_tags *tags)
106 {
107         struct mtd_info *mtd = yaffs_dev_to_mtd(dev);
108 #if (MTD_VERSION_CODE > MTD_VERSION(2, 6, 17))
109         struct mtd_oob_ops ops;
110 #endif
111         size_t dummy;
112         int retval = 0;
113         int local_data = 0;
114
115         loff_t addr = ((loff_t) nand_chunk) * dev->param.total_bytes_per_chunk;
116
117         struct yaffs_packed_tags2 pt;
118
119         int packed_tags_size =
120             dev->param.no_tags_ecc ? sizeof(pt.t) : sizeof(pt);
121         void *packed_tags_ptr =
122             dev->param.no_tags_ecc ? (void *)&pt.t : (void *)&pt;
123
124         T(YAFFS_TRACE_MTD,
125           (TSTR
126            ("nandmtd2_read_chunk_tags chunk %d data %p tags %p"
127             TENDSTR), nand_chunk, data, tags));
128
129         if (dev->param.inband_tags) {
130
131                 if (!data) {
132                         local_data = 1;
133                         data = yaffs_get_temp_buffer(dev, __LINE__);
134                 }
135
136         }
137
138 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
139         if (dev->param.inband_tags || (data && !tags))
140                 retval = mtd->read(mtd, addr, dev->param.total_bytes_per_chunk,
141                                    &dummy, data);
142         else if (tags) {
143                 ops.mode = MTD_OOB_AUTO;
144                 ops.ooblen = packed_tags_size;
145                 ops.len = data ? dev->data_bytes_per_chunk : packed_tags_size;
146                 ops.ooboffs = 0;
147                 ops.datbuf = data;
148                 ops.oobbuf = yaffs_dev_to_lc(dev)->spare_buffer;
149                 retval = mtd->read_oob(mtd, addr, &ops);
150         }
151 #else
152         if (!dev->param.inband_tags && data && tags) {
153
154                 retval = mtd->read_ecc(mtd, addr, dev->data_bytes_per_chunk,
155                                        &dummy, data, dev->spare_buffer, NULL);
156         } else {
157                 if (data)
158                         retval =
159                             mtd->read(mtd, addr, dev->data_bytes_per_chunk,
160                                       &dummy, data);
161                 if (!dev->param.inband_tags && tags)
162                         retval =
163                             mtd->read_oob(mtd, addr, mtd->oobsize, &dummy,
164                                           dev->spare_buffer);
165         }
166 #endif
167
168         if (dev->param.inband_tags) {
169                 if (tags) {
170                         struct yaffs_packed_tags2_tags_only *pt2tp;
171                         pt2tp =
172                             (struct yaffs_packed_tags2_tags_only *)&data[dev->
173                                                                          data_bytes_per_chunk];
174                         yaffs_unpack_tags2_tags_only(tags, pt2tp);
175                 }
176         } else {
177                 if (tags) {
178                         memcpy(packed_tags_ptr,
179                                yaffs_dev_to_lc(dev)->spare_buffer,
180                                packed_tags_size);
181                         yaffs_unpack_tags2(tags, &pt, !dev->param.no_tags_ecc);
182                 }
183         }
184
185         if (local_data)
186                 yaffs_release_temp_buffer(dev, data, __LINE__);
187
188         if (tags && retval == -EBADMSG
189             && tags->ecc_result == YAFFS_ECC_RESULT_NO_ERROR) {
190                 tags->ecc_result = YAFFS_ECC_RESULT_UNFIXED;
191                 dev->n_ecc_unfixed++;
192         }
193         if (tags && retval == -EUCLEAN
194             && tags->ecc_result == YAFFS_ECC_RESULT_NO_ERROR) {
195                 tags->ecc_result = YAFFS_ECC_RESULT_FIXED;
196                 dev->n_ecc_fixed++;
197         }
198         if (retval == 0)
199                 return YAFFS_OK;
200         else
201                 return YAFFS_FAIL;
202 }
203
204 int nandmtd2_mark_block_bad(struct yaffs_dev *dev, int block_no)
205 {
206         struct mtd_info *mtd = yaffs_dev_to_mtd(dev);
207         int retval;
208         T(YAFFS_TRACE_MTD,
209           (TSTR("nandmtd2_mark_block_bad %d" TENDSTR), block_no));
210
211         retval =
212             mtd->block_markbad(mtd,
213                                block_no * dev->param.chunks_per_block *
214                                dev->param.total_bytes_per_chunk);
215
216         if (retval == 0)
217                 return YAFFS_OK;
218         else
219                 return YAFFS_FAIL;
220
221 }
222
223 int nandmtd2_query_block(struct yaffs_dev *dev, int block_no,
224                          enum yaffs_block_state *state, u32 * seq_number)
225 {
226         struct mtd_info *mtd = yaffs_dev_to_mtd(dev);
227         int retval;
228
229         T(YAFFS_TRACE_MTD, (TSTR("nandmtd2_query_block %d" TENDSTR), block_no));
230         retval =
231             mtd->block_isbad(mtd,
232                              block_no * dev->param.chunks_per_block *
233                              dev->param.total_bytes_per_chunk);
234
235         if (retval) {
236                 T(YAFFS_TRACE_MTD, (TSTR("block is bad" TENDSTR)));
237
238                 *state = YAFFS_BLOCK_STATE_DEAD;
239                 *seq_number = 0;
240         } else {
241                 struct yaffs_ext_tags t;
242                 nandmtd2_read_chunk_tags(dev, block_no *
243                                          dev->param.chunks_per_block, NULL, &t);
244
245                 if (t.chunk_used) {
246                         *seq_number = t.seq_number;
247                         *state = YAFFS_BLOCK_STATE_NEEDS_SCANNING;
248                 } else {
249                         *seq_number = 0;
250                         *state = YAFFS_BLOCK_STATE_EMPTY;
251                 }
252         }
253         T(YAFFS_TRACE_MTD,
254           (TSTR("block is bad seq %d state %d" TENDSTR), *seq_number, *state));
255
256         if (retval == 0)
257                 return YAFFS_OK;
258         else
259                 return YAFFS_FAIL;
260 }