2 * YAFFS: Yet Another Flash File System. A NAND-flash specific file system.
4 * Copyright (C) 2002-2011 Aleph One Ltd.
5 * for Toby Churchill Ltd and Brightstar Engineering
7 * Created by Charles Manning <charles@aleph1.co.uk>
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.
14 /* mtd interface for YAFFS2 */
17 #include "yaffs_trace.h"
19 #include "yaffs_mtdif2.h"
21 #include "linux/mtd/mtd.h"
22 #include "linux/types.h"
23 #include "linux/time.h"
25 #include "yaffs_packedtags2.h"
27 #include "yaffs_linux.h"
29 #if (LINUX_VERSION_CODE < KERNEL_VERSION(3, 2, 0))
30 #define MTD_OPS_AUTO_OOB MTD_OOB_AUTO
35 /* NB For use with inband tags....
36 * We assume that the data buffer is of size total_bytes_per_chunk so
37 * that we can also use it to load the tags.
39 int nandmtd2_write_chunk_tags(struct yaffs_dev *dev, int nand_chunk,
41 const struct yaffs_ext_tags *tags)
43 struct mtd_info *mtd = yaffs_dev_to_mtd(dev);
44 #if (MTD_VERSION_CODE > MTD_VERSION(2, 6, 17))
45 struct mtd_oob_ops ops;
53 struct yaffs_packed_tags2 pt;
55 int packed_tags_size =
56 dev->param.no_tags_ecc ? sizeof(pt.t) : sizeof(pt);
57 void *packed_tags_ptr =
58 dev->param.no_tags_ecc ? (void *)&pt.t : (void *)&pt;
60 yaffs_trace(YAFFS_TRACE_MTD,
61 "nandmtd2_write_chunk_tags chunk %d data %p tags %p",
62 nand_chunk, data, tags);
64 addr = ((loff_t) nand_chunk) * dev->param.total_bytes_per_chunk;
66 /* For yaffs2 writing there must be both data and tags.
67 * If we're using inband tags, then the tags are stuffed into
68 * the end of the data buffer.
72 else if (dev->param.inband_tags) {
73 struct yaffs_packed_tags2_tags_only *pt2tp;
75 (struct yaffs_packed_tags2_tags_only *)(data +
77 data_bytes_per_chunk);
78 yaffs_pack_tags2_tags_only(pt2tp, tags);
80 yaffs_pack_tags2(&pt, tags, !dev->param.no_tags_ecc);
83 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
84 ops.mode = MTD_OPS_AUTO_OOB;
85 ops.ooblen = (dev->param.inband_tags) ? 0 : packed_tags_size;
86 ops.len = dev->param.total_bytes_per_chunk;
88 ops.datbuf = (u8 *) data;
89 ops.oobbuf = (dev->param.inband_tags) ? NULL : packed_tags_ptr;
90 retval = mtd->write_oob(mtd, addr, &ops);
93 if (!dev->param.inband_tags) {
95 mtd->write_ecc(mtd, addr, dev->data_bytes_per_chunk,
96 &dummy, data, (u8 *) packed_tags_ptr, NULL);
99 mtd->write(mtd, addr, dev->param.total_bytes_per_chunk,
110 int nandmtd2_read_chunk_tags(struct yaffs_dev *dev, int nand_chunk,
111 u8 *data, struct yaffs_ext_tags *tags)
113 struct mtd_info *mtd = yaffs_dev_to_mtd(dev);
114 #if (MTD_VERSION_CODE > MTD_VERSION(2, 6, 17))
115 struct mtd_oob_ops ops;
121 loff_t addr = ((loff_t) nand_chunk) * dev->param.total_bytes_per_chunk;
123 struct yaffs_packed_tags2 pt;
125 int packed_tags_size =
126 dev->param.no_tags_ecc ? sizeof(pt.t) : sizeof(pt);
127 void *packed_tags_ptr =
128 dev->param.no_tags_ecc ? (void *)&pt.t : (void *)&pt;
130 yaffs_trace(YAFFS_TRACE_MTD,
131 "nandmtd2_read_chunk_tags chunk %d data %p tags %p",
132 nand_chunk, data, tags);
134 if (dev->param.inband_tags) {
138 data = yaffs_get_temp_buffer(dev);
143 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
144 if (dev->param.inband_tags || (data && !tags))
145 retval = mtd->read(mtd, addr, dev->param.total_bytes_per_chunk,
148 ops.mode = MTD_OPS_AUTO_OOB;
149 ops.ooblen = packed_tags_size;
150 ops.len = data ? dev->data_bytes_per_chunk : packed_tags_size;
153 ops.oobbuf = yaffs_dev_to_lc(dev)->spare_buffer;
154 retval = mtd->read_oob(mtd, addr, &ops);
157 if (!dev->param.inband_tags && data && tags) {
159 retval = mtd->read_ecc(mtd, addr, dev->data_bytes_per_chunk,
160 &dummy, data, dev->spare_buffer, NULL);
164 mtd->read(mtd, addr, dev->data_bytes_per_chunk,
166 if (!dev->param.inband_tags && tags)
168 mtd->read_oob(mtd, addr, mtd->oobsize, &dummy,
173 if (dev->param.inband_tags) {
175 struct yaffs_packed_tags2_tags_only *pt2tp;
177 (struct yaffs_packed_tags2_tags_only *)
178 &data[dev->data_bytes_per_chunk];
179 yaffs_unpack_tags2_tags_only(tags, pt2tp);
183 memcpy(packed_tags_ptr,
184 yaffs_dev_to_lc(dev)->spare_buffer,
186 yaffs_unpack_tags2(tags, &pt, !dev->param.no_tags_ecc);
191 yaffs_release_temp_buffer(dev, data);
193 if (tags && retval == -EBADMSG
194 && tags->ecc_result == YAFFS_ECC_RESULT_NO_ERROR) {
195 tags->ecc_result = YAFFS_ECC_RESULT_UNFIXED;
196 dev->n_ecc_unfixed++;
198 if (tags && retval == -EUCLEAN
199 && tags->ecc_result == YAFFS_ECC_RESULT_NO_ERROR) {
200 tags->ecc_result = YAFFS_ECC_RESULT_FIXED;
209 int nandmtd2_mark_block_bad(struct yaffs_dev *dev, int block_no)
211 struct mtd_info *mtd = yaffs_dev_to_mtd(dev);
213 yaffs_trace(YAFFS_TRACE_MTD,
214 "nandmtd2_mark_block_bad %d",
218 mtd->block_markbad(mtd,
219 block_no * dev->param.chunks_per_block *
220 dev->param.total_bytes_per_chunk);
229 int nandmtd2_query_block(struct yaffs_dev *dev, int block_no,
230 enum yaffs_block_state *state, u32 *seq_number)
232 struct mtd_info *mtd = yaffs_dev_to_mtd(dev);
235 yaffs_trace(YAFFS_TRACE_MTD, "nandmtd2_query_block %d", block_no);
237 mtd->block_isbad(mtd,
238 block_no * dev->param.chunks_per_block *
239 dev->param.total_bytes_per_chunk);
242 yaffs_trace(YAFFS_TRACE_MTD, "block is bad");
244 *state = YAFFS_BLOCK_STATE_DEAD;
247 struct yaffs_ext_tags t;
248 nandmtd2_read_chunk_tags(dev, block_no *
249 dev->param.chunks_per_block, NULL, &t);
252 *seq_number = t.seq_number;
253 *state = YAFFS_BLOCK_STATE_NEEDS_SCAN;
256 *state = YAFFS_BLOCK_STATE_EMPTY;
259 yaffs_trace(YAFFS_TRACE_MTD,
260 "block is bad seq %d state %d",
261 *seq_number, *state);