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