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