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