2 * YAFFS: Yet Another Flash File System. A NAND-flash specific file system.
4 * Copyright (C) 2002-2018 Aleph One Ltd.
6 * Created by Charles Manning <charles@aleph1.co.uk>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 as
10 * published by the Free Software Foundation.
13 /* mtd interface for YAFFS2 */
17 #include "asm/errno.h"
20 #include "yaffs_trace.h"
22 #include "yaffs_mtdif2.h"
24 #include "linux/mtd/mtd.h"
25 #include "linux/types.h"
26 #include "linux/time.h"
28 #include "yaffs_trace.h"
29 #include "yaffs_packedtags2.h"
32 #define yaffs_dev_to_mtd(dev) ((struct mtd_info *)((dev)->driver_context))
33 #define yaffs_dev_to_lc(dev) ((struct yaffs_linux_context *)((dev)->os_context))
36 /* NB For use with inband tags....
37 * We assume that the data buffer is of size total_bytes_per_chunk so
38 * that we can also use it to load the tags.
40 int nandmtd2_write_chunk_tags(struct yaffs_dev *dev, int nand_chunk,
42 const struct yaffs_ext_tags *tags)
44 struct mtd_info *mtd = yaffs_dev_to_mtd(dev);
45 struct mtd_oob_ops ops;
52 struct yaffs_packed_tags2 pt;
54 int packed_tags_size =
55 dev->param.no_tags_ecc ? sizeof(pt.t) : sizeof(pt);
56 void *packed_tags_ptr =
57 dev->param.no_tags_ecc ? (void *)&pt.t : (void *)&pt;
59 yaffs_trace(YAFFS_TRACE_MTD,
60 "nandmtd2_write_chunk_tags chunk %d data %p tags %p",
61 nand_chunk, data, tags);
63 addr = ((loff_t) nand_chunk) * dev->param.total_bytes_per_chunk;
65 /* For yaffs2 writing there must be both data and tags.
66 * If we're using inband tags, then the tags are stuffed into
67 * the end of the data buffer.
71 else if (dev->param.inband_tags) {
72 struct yaffs_packed_tags2_tags_only *pt2tp;
74 (struct yaffs_packed_tags2_tags_only *)(data +
76 data_bytes_per_chunk);
77 yaffs_pack_tags2_tags_only(pt2tp, tags);
79 yaffs_pack_tags2(&pt, tags, !dev->param.no_tags_ecc);
82 ops.mode = MTD_OOB_AUTO;
83 ops.ooblen = (dev->param.inband_tags) ? 0 : packed_tags_size;
84 ops.len = dev->param.total_bytes_per_chunk;
86 ops.datbuf = (u8 *) data;
87 ops.oobbuf = (dev->param.inband_tags) ? NULL : packed_tags_ptr;
88 retval = mtd->write_oob(mtd, addr, &ops);
96 int nandmtd2_read_chunk_tags(struct yaffs_dev *dev, int nand_chunk,
97 u8 *data, struct yaffs_ext_tags *tags)
99 struct mtd_info *mtd = yaffs_dev_to_mtd(dev);
101 struct mtd_oob_ops ops;
105 struct yaffs_packed_tags2 pt;
106 loff_t addr = ((loff_t) nand_chunk) * dev->param.total_bytes_per_chunk;
107 int packed_tags_size =
108 dev->param.no_tags_ecc ? sizeof(pt.t) : sizeof(pt);
109 void *packed_tags_ptr =
110 dev->param.no_tags_ecc ? (void *)&pt.t : (void *)&pt;
112 yaffs_trace(YAFFS_TRACE_MTD,
113 "nandmtd2_read_chunk_tags chunk %d data %p tags %p",
114 nand_chunk, data, tags);
116 if (dev->param.inband_tags) {
120 data = yaffs_get_temp_buffer(dev);
125 if (dev->param.inband_tags || (data && !tags))
126 retval = mtd->read(mtd, addr, dev->param.total_bytes_per_chunk,
129 ops.mode = MTD_OOB_AUTO;
130 ops.ooblen = packed_tags_size;
131 ops.len = data ? dev->data_bytes_per_chunk : packed_tags_size;
134 ops.oobbuf = local_spare;
135 retval = mtd->read_oob(mtd, addr, &ops);
138 if (dev->param.inband_tags) {
140 struct yaffs_packed_tags2_tags_only *pt2tp;
142 (struct yaffs_packed_tags2_tags_only *)
143 &data[dev->data_bytes_per_chunk];
144 yaffs_unpack_tags2_tags_only(tags, pt2tp);
148 memcpy(packed_tags_ptr,
151 yaffs_unpack_tags2(tags, &pt, !dev->param.no_tags_ecc);
156 yaffs_release_temp_buffer(dev, data);
158 if (tags && retval == -EBADMSG
159 && tags->ecc_result == YAFFS_ECC_RESULT_NO_ERROR) {
160 tags->ecc_result = YAFFS_ECC_RESULT_UNFIXED;
161 dev->n_ecc_unfixed++;
163 if (tags && retval == -EUCLEAN
164 && tags->ecc_result == YAFFS_ECC_RESULT_NO_ERROR) {
165 tags->ecc_result = YAFFS_ECC_RESULT_FIXED;
175 int nandmtd2_MarkNANDBlockBad(struct yaffs_dev *dev, int blockNo)
177 struct mtd_info *mtd = (struct mtd_info *)(dev->driver_context);
180 yaffs_trace(YAFFS_TRACE_MTD,
181 "nandmtd2_MarkNANDBlockBad %d", blockNo);
184 mtd->block_markbad(mtd,
185 blockNo * dev->param.chunks_per_block *
186 dev->data_bytes_per_chunk);
195 int nandmtd2_QueryNANDBlock(struct yaffs_dev *dev, int blockNo,
196 enum yaffs_block_state *state, u32 *sequenceNumber)
198 struct mtd_info *mtd = (struct mtd_info *)(dev->driver_context);
201 yaffs_trace(YAFFS_TRACE_MTD, "nandmtd2_QueryNANDBlock %d", blockNo);
203 mtd->block_isbad(mtd,
204 blockNo * dev->param.chunks_per_block *
205 dev->data_bytes_per_chunk);
208 yaffs_trace(YAFFS_TRACE_MTD, "block is bad");
210 *state = YAFFS_BLOCK_STATE_DEAD;
213 struct yaffs_ext_tags t;
214 nandmtd2_read_chunk_tags(dev,
216 dev->param.chunks_per_block, NULL,
220 *sequenceNumber = t.seq_number;
221 *state = YAFFS_BLOCK_STATE_NEEDS_SCAN;
224 *state = YAFFS_BLOCK_STATE_EMPTY;
227 yaffs_trace(YAFFS_TRACE_MTD, "block is bad seq %d state %d",
228 *sequenceNumber, *state);