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 */
33int nandmtd2_WriteChunkWithTagsToNAND(yaffs_dev_t *dev, int nand_chunk,
34				      const __u8 *data,
35				      const yaffs_ext_tags *tags)
36{
37	struct mtd_info *mtd = yaffs_dev_to_mtd(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.no_tags_ecc ? sizeof(pt.t) : sizeof(pt);
50	void * packed_tags_ptr = dev->param.no_tags_ecc ? (void *) &pt.t : (void *)&pt;
51
52	T(YAFFS_TRACE_MTD,
53	  (TSTR
54	   ("nandmtd2_WriteChunkWithTagsToNAND chunk %d data %p tags %p"
55	    TENDSTR), nand_chunk, data, tags));
56
57
58	addr  = ((loff_t) nand_chunk) * dev->param.total_bytes_per_chunk;
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.inband_tags) {
67		yaffs_PackedTags2TagsPart *pt2tp;
68		pt2tp = (yaffs_PackedTags2TagsPart *)(data + dev->data_bytes_per_chunk);
69		yaffs_PackTags2TagsPart(pt2tp, tags);
70	} else
71		yaffs_PackTags2(&pt, tags, !dev->param.no_tags_ecc);
72
73#if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
74	ops.mode = MTD_OOB_AUTO;
75	ops.ooblen = (dev->param.inband_tags) ? 0 : packed_tags_size;
76	ops.len = dev->param.total_bytes_per_chunk;
77	ops.ooboffs = 0;
78	ops.datbuf = (__u8 *)data;
79	ops.oobbuf = (dev->param.inband_tags) ? NULL : packed_tags_ptr;
80	retval = mtd->write_oob(mtd, addr, &ops);
81
82#else
83	if (!dev->param.inband_tags) {
84		retval =
85		    mtd->write_ecc(mtd, addr, dev->data_bytes_per_chunk,
86				   &dummy, data, (__u8 *) packed_tags_ptr, NULL);
87	} else {
88		retval =
89		    mtd->write(mtd, addr, dev->param.total_bytes_per_chunk, &dummy,
90			       data);
91	}
92#endif
93
94	if (retval == 0)
95		return YAFFS_OK;
96	else
97		return YAFFS_FAIL;
98}
99
100int nandmtd2_ReadChunkWithTagsFromNAND(yaffs_dev_t *dev, int nand_chunk,
101				       __u8 *data, yaffs_ext_tags *tags)
102{
103	struct mtd_info *mtd = yaffs_dev_to_mtd(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) nand_chunk) * dev->param.total_bytes_per_chunk;
112
113	yaffs_PackedTags2 pt;
114
115	int packed_tags_size = dev->param.no_tags_ecc ? sizeof(pt.t) : sizeof(pt);
116	void * packed_tags_ptr = dev->param.no_tags_ecc ? (void *) &pt.t: (void *)&pt;
117
118	T(YAFFS_TRACE_MTD,
119	  (TSTR
120	   ("nandmtd2_ReadChunkWithTagsFromNAND chunk %d data %p tags %p"
121	    TENDSTR), nand_chunk, data, tags));
122
123	if (dev->param.inband_tags) {
124
125		if (!data) {
126			localData = 1;
127			data = yaffs_get_temp_buffer(dev, __LINE__);
128		}
129
130
131	}
132
133
134#if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
135	if (dev->param.inband_tags || (data && !tags))
136		retval = mtd->read(mtd, addr, dev->param.total_bytes_per_chunk,
137				&dummy, data);
138	else if (tags) {
139		ops.mode = MTD_OOB_AUTO;
140		ops.ooblen = packed_tags_size;
141		ops.len = data ? dev->data_bytes_per_chunk : packed_tags_size;
142		ops.ooboffs = 0;
143		ops.datbuf = data;
144		ops.oobbuf = yaffs_dev_to_lc(dev)->spareBuffer;
145		retval = mtd->read_oob(mtd, addr, &ops);
146	}
147#else
148	if (!dev->param.inband_tags && data && tags) {
149
150		retval = mtd->read_ecc(mtd, addr, dev->data_bytes_per_chunk,
151					  &dummy, data, dev->spareBuffer,
152					  NULL);
153	} else {
154		if (data)
155			retval =
156			    mtd->read(mtd, addr, dev->data_bytes_per_chunk, &dummy,
157				      data);
158		if (!dev->param.inband_tags && tags)
159			retval =
160			    mtd->read_oob(mtd, addr, mtd->oobsize, &dummy,
161					  dev->spareBuffer);
162	}
163#endif
164
165
166	if (dev->param.inband_tags) {
167		if (tags) {
168			yaffs_PackedTags2TagsPart *pt2tp;
169			pt2tp = (yaffs_PackedTags2TagsPart *)&data[dev->data_bytes_per_chunk];
170			yaffs_unpack_tags2tags_part(tags, pt2tp);
171		}
172	} else {
173		if (tags) {
174			memcpy(packed_tags_ptr, yaffs_dev_to_lc(dev)->spareBuffer, packed_tags_size);
175			yaffs_unpack_tags2(tags, &pt, !dev->param.no_tags_ecc);
176		}
177	}
178
179	if (localData)
180		yaffs_release_temp_buffer(dev, data, __LINE__);
181
182	if (tags && retval == -EBADMSG && tags->ecc_result == YAFFS_ECC_RESULT_NO_ERROR) {
183		tags->ecc_result = YAFFS_ECC_RESULT_UNFIXED;
184		dev->n_ecc_unfixed++;
185	}
186	if(tags && retval == -EUCLEAN && tags->ecc_result == YAFFS_ECC_RESULT_NO_ERROR) {
187		tags->ecc_result = YAFFS_ECC_RESULT_FIXED;
188		dev->n_ecc_fixed++;
189	}
190	if (retval == 0)
191		return YAFFS_OK;
192	else
193		return YAFFS_FAIL;
194}
195
196int nandmtd2_MarkNANDBlockBad(struct yaffs_dev_s *dev, int block_no)
197{
198	struct mtd_info *mtd = yaffs_dev_to_mtd(dev);
199	int retval;
200	T(YAFFS_TRACE_MTD,
201	  (TSTR("nandmtd2_MarkNANDBlockBad %d" TENDSTR), block_no));
202
203	retval =
204	    mtd->block_markbad(mtd,
205			       block_no * dev->param.chunks_per_block *
206			       dev->param.total_bytes_per_chunk);
207
208	if (retval == 0)
209		return YAFFS_OK;
210	else
211		return YAFFS_FAIL;
212
213}
214
215int nandmtd2_QueryNANDBlock(struct yaffs_dev_s *dev, int block_no,
216			    yaffs_block_state_t *state, __u32 *seq_number)
217{
218	struct mtd_info *mtd = yaffs_dev_to_mtd(dev);
219	int retval;
220
221	T(YAFFS_TRACE_MTD,
222	  (TSTR("nandmtd2_QueryNANDBlock %d" TENDSTR), block_no));
223	retval =
224	    mtd->block_isbad(mtd,
225			     block_no * dev->param.chunks_per_block *
226			     dev->param.total_bytes_per_chunk);
227
228	if (retval) {
229		T(YAFFS_TRACE_MTD, (TSTR("block is bad" TENDSTR)));
230
231		*state = YAFFS_BLOCK_STATE_DEAD;
232		*seq_number = 0;
233	} else {
234		yaffs_ext_tags t;
235		nandmtd2_ReadChunkWithTagsFromNAND(dev,
236						   block_no *
237						   dev->param.chunks_per_block, NULL,
238						   &t);
239
240		if (t.chunk_used) {
241			*seq_number = t.seq_number;
242			*state = YAFFS_BLOCK_STATE_NEEDS_SCANNING;
243		} else {
244			*seq_number = 0;
245			*state = YAFFS_BLOCK_STATE_EMPTY;
246		}
247	}
248	T(YAFFS_TRACE_MTD,
249	  (TSTR("block is bad seq %d state %d" TENDSTR), *seq_number,
250	   *state));
251
252	if (retval == 0)
253		return YAFFS_OK;
254	else
255		return YAFFS_FAIL;
256}
257
258