1 /*
2 * YAFFS: Yet Another Flash File System. A NAND-flash specific file system.
3 *
4 * Copyright (C) 2002-2007 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 const char *yaffs_mtdif2_c_version =
17 "$Id$";
18
19 #include "yportenv.h"
20
21
22 #include "yaffs_mtdif2.h"
23
24 #include "linux/mtd/mtd.h"
25 #include "linux/types.h"
26 #include "linux/time.h"
27
28 #include "yaffs_packedtags2.h"
29
30 /* NB For use with inband tags....
31 * We assume that the data buffer is of size totalBytersPerChunk so that we can also
32 * use it to load the tags.
33 */
nandmtd2_WriteChunkWithTagsToNAND(yaffs_Device * dev,int chunkInNAND,const __u8 * data,const yaffs_ExtendedTags * tags)34 int nandmtd2_WriteChunkWithTagsToNAND(yaffs_Device *dev, int chunkInNAND,
35 const __u8 *data,
36 const yaffs_ExtendedTags *tags)
37 {
38 struct mtd_info *mtd = (struct mtd_info *)(dev->genericDevice);
39 #if (MTD_VERSION_CODE > MTD_VERSION(2, 6, 17))
40 struct mtd_oob_ops ops;
41 #else
42 size_t dummy;
43 #endif
44 int retval = 0;
45
46 loff_t addr;
47
48 yaffs_PackedTags2 pt;
49
50 T(YAFFS_TRACE_MTD,
51 (TSTR
52 ("nandmtd2_WriteChunkWithTagsToNAND chunk %d data %p tags %p"
53 TENDSTR), chunkInNAND, data, tags));
54
55 addr = ((loff_t) chunkInNAND) * dev->totalBytesPerChunk;
56
57 /* For yaffs2 writing there must be both data and tags.
58 * If we're using inband tags, then the tags are stuffed into
59 * the end of the data buffer.
60 */
61 if (!data || !tags)
62 BUG();
63 else if (dev->inbandTags) {
64 yaffs_PackedTags2TagsPart *pt2tp;
65 pt2tp = (yaffs_PackedTags2TagsPart *)(data + dev->nDataBytesPerChunk);
66 yaffs_PackTags2TagsPart(pt2tp, tags);
67 } else
68 yaffs_PackTags2(&pt, tags);
69
70 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
71 ops.mode = MTD_OOB_AUTO;
72 ops.ooblen = (dev->inbandTags) ? 0 : sizeof(pt);
73 ops.len = dev->totalBytesPerChunk;
74 ops.ooboffs = 0;
75 ops.datbuf = (__u8 *)data;
76 ops.oobbuf = (dev->inbandTags) ? NULL : (void *)&pt;
77 retval = mtd->write_oob(mtd, addr, &ops);
78
79 #else
80 if (!dev->inbandTags) {
81 retval =
82 mtd->write_ecc(mtd, addr, dev->nDataBytesPerChunk,
83 &dummy, data, (__u8 *) &pt, NULL);
84 } else {
85 retval =
86 mtd->write(mtd, addr, dev->totalBytesPerChunk, &dummy,
87 data);
88 }
89 #endif
90
91 if (retval == 0)
92 return YAFFS_OK;
93 else
94 return YAFFS_FAIL;
95 }
96
nandmtd2_ReadChunkWithTagsFromNAND(yaffs_Device * dev,int chunkInNAND,__u8 * data,yaffs_ExtendedTags * tags)97 int nandmtd2_ReadChunkWithTagsFromNAND(yaffs_Device *dev, int chunkInNAND,
98 __u8 *data, yaffs_ExtendedTags *tags)
99 {
100 struct mtd_info *mtd = (struct mtd_info *)(dev->genericDevice);
101 #if (MTD_VERSION_CODE > MTD_VERSION(2, 6, 17))
102 struct mtd_oob_ops ops;
103 #endif
104 size_t dummy;
105 int retval = 0;
106 int localData = 0;
107
108 loff_t addr = ((loff_t) chunkInNAND) * dev->totalBytesPerChunk;
109
110 yaffs_PackedTags2 pt;
111
112 T(YAFFS_TRACE_MTD,
113 (TSTR
114 ("nandmtd2_ReadChunkWithTagsFromNAND chunk %d data %p tags %p"
115 TENDSTR), chunkInNAND, data, tags));
116
117 if (dev->inbandTags) {
118
119 if (!data) {
120 localData = 1;
121 data = yaffs_GetTempBuffer(dev, __LINE__);
122 }
123
124
125 }
126
127
128 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
129 if (dev->inbandTags || (data && !tags))
130 retval = mtd->read(mtd, addr, dev->totalBytesPerChunk,
131 &dummy, data);
132 else if (tags) {
133 ops.mode = MTD_OOB_AUTO;
134 ops.ooblen = sizeof(pt);
135 ops.len = data ? dev->nDataBytesPerChunk : sizeof(pt);
136 ops.ooboffs = 0;
137 ops.datbuf = data;
138 ops.oobbuf = dev->spareBuffer;
139 retval = mtd->read_oob(mtd, addr, &ops);
140 }
141 #else
142 if (!dev->inbandTags && data && tags) {
143
144 retval = mtd->read_ecc(mtd, addr, dev->nDataBytesPerChunk,
145 &dummy, data, dev->spareBuffer,
146 NULL);
147 } else {
148 if (data)
149 retval =
150 mtd->read(mtd, addr, dev->nDataBytesPerChunk, &dummy,
151 data);
152 if (!dev->inbandTags && tags)
153 retval =
154 mtd->read_oob(mtd, addr, mtd->oobsize, &dummy,
155 dev->spareBuffer);
156 }
157 #endif
158
159
160 if (dev->inbandTags) {
161 if (tags) {
162 yaffs_PackedTags2TagsPart *pt2tp;
163 pt2tp = (yaffs_PackedTags2TagsPart *)&data[dev->nDataBytesPerChunk];
164 yaffs_UnpackTags2TagsPart(tags, pt2tp);
165 }
166 } else {
167 if (tags) {
168 memcpy(&pt, dev->spareBuffer, sizeof(pt));
169 yaffs_UnpackTags2(tags, &pt);
170 }
171 }
172
173 if (localData)
174 yaffs_ReleaseTempBuffer(dev, data, __LINE__);
175
176 if (tags && retval == -EBADMSG && tags->eccResult == YAFFS_ECC_RESULT_NO_ERROR) {
177 tags->eccResult = YAFFS_ECC_RESULT_UNFIXED;
178 dev->eccUnfixed++;
179 }
180 if (tags && retval == -EUCLEAN && tags->eccResult == YAFFS_ECC_RESULT_NO_ERROR) {
181 tags->eccResult = YAFFS_ECC_RESULT_FIXED;
182 dev->eccFixed++;
183 }
184 if (retval == 0)
185 return YAFFS_OK;
186 else
187 return YAFFS_FAIL;
188 }
189
nandmtd2_MarkNANDBlockBad(struct yaffs_DeviceStruct * dev,int blockNo)190 int nandmtd2_MarkNANDBlockBad(struct yaffs_DeviceStruct *dev, int blockNo)
191 {
192 struct mtd_info *mtd = (struct mtd_info *)(dev->genericDevice);
193 int retval;
194 T(YAFFS_TRACE_MTD,
195 (TSTR("nandmtd2_MarkNANDBlockBad %d" TENDSTR), blockNo));
196
197 retval =
198 mtd->block_markbad(mtd,
199 blockNo * dev->nChunksPerBlock *
200 dev->totalBytesPerChunk);
201
202 if (retval == 0)
203 return YAFFS_OK;
204 else
205 return YAFFS_FAIL;
206
207 }
208
nandmtd2_QueryNANDBlock(struct yaffs_DeviceStruct * dev,int blockNo,yaffs_BlockState * state,__u32 * sequenceNumber)209 int nandmtd2_QueryNANDBlock(struct yaffs_DeviceStruct *dev, int blockNo,
210 yaffs_BlockState *state, __u32 *sequenceNumber)
211 {
212 struct mtd_info *mtd = (struct mtd_info *)(dev->genericDevice);
213 int retval;
214
215 T(YAFFS_TRACE_MTD,
216 (TSTR("nandmtd2_QueryNANDBlock %d" TENDSTR), blockNo));
217 retval =
218 mtd->block_isbad(mtd,
219 blockNo * dev->nChunksPerBlock *
220 dev->totalBytesPerChunk);
221
222 if (retval) {
223 T(YAFFS_TRACE_MTD, (TSTR("block is bad" TENDSTR)));
224
225 *state = YAFFS_BLOCK_STATE_DEAD;
226 *sequenceNumber = 0;
227 } else {
228 yaffs_ExtendedTags t;
229 nandmtd2_ReadChunkWithTagsFromNAND(dev,
230 blockNo *
231 dev->nChunksPerBlock, NULL,
232 &t);
233
234 if (t.chunkUsed) {
235 *sequenceNumber = t.sequenceNumber;
236 *state = YAFFS_BLOCK_STATE_NEEDS_SCANNING;
237 } else {
238 *sequenceNumber = 0;
239 *state = YAFFS_BLOCK_STATE_EMPTY;
240 }
241 }
242 T(YAFFS_TRACE_MTD,
243 (TSTR("block is bad seq %d state %d" TENDSTR), *sequenceNumber,
244 *state));
245
246 if (retval == 0)
247 return YAFFS_OK;
248 else
249 return YAFFS_FAIL;
250 }
251
252