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