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