support make_ext4fs
[firefly-linux-kernel-4.4.55.git] / fs / yaffs2 / 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$";
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         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
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 #ifdef CONFIG_MTD_NAND_RK29
177     //dxj 20101221@ if return -EBADMSG then i think the page is badchunk so just set the eccResult=YAFFS_ECC_RESULT_NO_ERROR
178     if (tags && retval == -EBADMSG /*&& tags->eccResult == YAFFS_ECC_RESULT_NO_ERROR*/) {
179 #else    
180         if (tags && retval == -EBADMSG && tags->eccResult == YAFFS_ECC_RESULT_NO_ERROR) {
181 #endif  
182                 tags->eccResult = YAFFS_ECC_RESULT_UNFIXED;
183                 dev->eccUnfixed++;
184         }
185         if (tags && retval == -EUCLEAN && tags->eccResult == YAFFS_ECC_RESULT_NO_ERROR) {
186                 tags->eccResult = YAFFS_ECC_RESULT_FIXED;
187                 dev->eccFixed++;
188         }
189         if (retval == 0)
190                 return YAFFS_OK;
191         else
192                 return YAFFS_FAIL;
193 }
194
195 int nandmtd2_MarkNANDBlockBad(struct yaffs_DeviceStruct *dev, int blockNo)
196 {
197         struct mtd_info *mtd = (struct mtd_info *)(dev->genericDevice);
198         int retval;
199         T(YAFFS_TRACE_MTD,
200           (TSTR("nandmtd2_MarkNANDBlockBad %d" TENDSTR), blockNo));
201
202         retval =
203             mtd->block_markbad(mtd,
204                                blockNo * dev->nChunksPerBlock *
205                                dev->totalBytesPerChunk);
206
207         if (retval == 0)
208                 return YAFFS_OK;
209         else
210                 return YAFFS_FAIL;
211
212 }
213
214 int nandmtd2_QueryNANDBlock(struct yaffs_DeviceStruct *dev, int blockNo,
215                             yaffs_BlockState *state, __u32 *sequenceNumber)
216 {
217         struct mtd_info *mtd = (struct mtd_info *)(dev->genericDevice);
218         int retval;
219
220         T(YAFFS_TRACE_MTD,
221           (TSTR("nandmtd2_QueryNANDBlock %d" TENDSTR), blockNo));
222         retval =
223             mtd->block_isbad(mtd,
224                              blockNo * dev->nChunksPerBlock *
225                              dev->totalBytesPerChunk);
226
227         if (retval) {
228                 T(YAFFS_TRACE_MTD, (TSTR("block is bad" TENDSTR)));
229
230                 *state = YAFFS_BLOCK_STATE_DEAD;
231                 *sequenceNumber = 0;
232         } else {
233                 yaffs_ExtendedTags t;
234                 nandmtd2_ReadChunkWithTagsFromNAND(dev,
235                                                    blockNo *
236                                                    dev->nChunksPerBlock, NULL,
237                                                    &t);
238
239                 if (t.chunkUsed) {
240                         *sequenceNumber = t.sequenceNumber;
241                         *state = YAFFS_BLOCK_STATE_NEEDS_SCANNING;
242                 } else {
243                         *sequenceNumber = 0;
244                         *state = YAFFS_BLOCK_STATE_EMPTY;
245                 }
246         }
247         T(YAFFS_TRACE_MTD,
248           (TSTR("block is bad seq %d state %d" TENDSTR), *sequenceNumber,
249            *state));
250
251         if (retval == 0)
252                 return YAFFS_OK;
253         else
254                 return YAFFS_FAIL;
255 }
256