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