2007-10-10 18:05:48 +00:00
|
|
|
/*
|
|
|
|
* YAFFS: Yet Another Flash File System. A NAND-flash specific file system.
|
|
|
|
*
|
2012-10-30 14:58:17 +00:00
|
|
|
* Copyright (C) 2002-2010 Aleph One Ltd.
|
2007-10-10 18:05:48 +00:00
|
|
|
* for Toby Churchill Ltd and Brightstar Engineering
|
|
|
|
*
|
|
|
|
* Created by Charles Manning <charles@aleph1.co.uk>
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License version 2 as
|
|
|
|
* published by the Free Software Foundation.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* mtd interface for YAFFS2 */
|
|
|
|
|
|
|
|
#include "yportenv.h"
|
2012-10-30 14:58:17 +00:00
|
|
|
#include "yaffs_trace.h"
|
2007-10-10 18:05:48 +00:00
|
|
|
|
|
|
|
#include "yaffs_mtdif2.h"
|
|
|
|
|
|
|
|
#include "linux/mtd/mtd.h"
|
|
|
|
#include "linux/types.h"
|
|
|
|
#include "linux/time.h"
|
|
|
|
|
|
|
|
#include "yaffs_packedtags2.h"
|
|
|
|
|
2012-10-30 14:58:17 +00:00
|
|
|
#include "yaffs_linux.h"
|
|
|
|
|
2012-10-30 14:58:13 +00:00
|
|
|
/* NB For use with inband tags....
|
|
|
|
* We assume that the data buffer is of size totalBytersPerChunk so that we can also
|
|
|
|
* use it to load the tags.
|
|
|
|
*/
|
2012-10-30 14:58:17 +00:00
|
|
|
int nandmtd2_WriteChunkWithTagsToNAND(yaffs_dev_t *dev, int nand_chunk,
|
2012-10-30 14:58:13 +00:00
|
|
|
const __u8 *data,
|
2012-10-30 14:58:17 +00:00
|
|
|
const yaffs_ext_tags *tags)
|
2007-10-10 18:05:48 +00:00
|
|
|
{
|
2012-10-30 14:58:17 +00:00
|
|
|
struct mtd_info *mtd = yaffs_dev_to_mtd(dev);
|
2012-10-30 14:58:13 +00:00
|
|
|
#if (MTD_VERSION_CODE > MTD_VERSION(2, 6, 17))
|
2007-10-10 18:05:48 +00:00
|
|
|
struct mtd_oob_ops ops;
|
|
|
|
#else
|
|
|
|
size_t dummy;
|
|
|
|
#endif
|
|
|
|
int retval = 0;
|
|
|
|
|
2012-10-30 14:58:13 +00:00
|
|
|
loff_t addr;
|
2007-10-10 18:05:48 +00:00
|
|
|
|
|
|
|
yaffs_PackedTags2 pt;
|
|
|
|
|
2012-10-30 14:58:17 +00:00
|
|
|
int packed_tags_size = dev->param.no_tags_ecc ? sizeof(pt.t) : sizeof(pt);
|
|
|
|
void * packed_tags_ptr = dev->param.no_tags_ecc ? (void *) &pt.t : (void *)&pt;
|
|
|
|
|
2007-10-10 18:05:48 +00:00
|
|
|
T(YAFFS_TRACE_MTD,
|
|
|
|
(TSTR
|
|
|
|
("nandmtd2_WriteChunkWithTagsToNAND chunk %d data %p tags %p"
|
2012-10-30 14:58:17 +00:00
|
|
|
TENDSTR), nand_chunk, data, tags));
|
2008-06-10 08:20:19 +00:00
|
|
|
|
2007-10-10 18:05:48 +00:00
|
|
|
|
2012-10-30 14:58:17 +00:00
|
|
|
addr = ((loff_t) nand_chunk) * dev->param.total_bytes_per_chunk;
|
2012-10-30 14:58:13 +00:00
|
|
|
|
|
|
|
/* For yaffs2 writing there must be both data and tags.
|
|
|
|
* If we're using inband tags, then the tags are stuffed into
|
|
|
|
* the end of the data buffer.
|
|
|
|
*/
|
|
|
|
if (!data || !tags)
|
|
|
|
BUG();
|
2012-10-30 14:58:17 +00:00
|
|
|
else if (dev->param.inband_tags) {
|
2012-10-30 14:58:13 +00:00
|
|
|
yaffs_PackedTags2TagsPart *pt2tp;
|
2012-10-30 14:58:17 +00:00
|
|
|
pt2tp = (yaffs_PackedTags2TagsPart *)(data + dev->data_bytes_per_chunk);
|
2012-10-30 14:58:13 +00:00
|
|
|
yaffs_PackTags2TagsPart(pt2tp, tags);
|
2008-06-10 08:20:19 +00:00
|
|
|
} else
|
2012-10-30 14:58:17 +00:00
|
|
|
yaffs_PackTags2(&pt, tags, !dev->param.no_tags_ecc);
|
2008-06-10 08:20:19 +00:00
|
|
|
|
2012-10-30 14:58:13 +00:00
|
|
|
#if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
|
|
|
|
ops.mode = MTD_OOB_AUTO;
|
2012-10-30 14:58:17 +00:00
|
|
|
ops.ooblen = (dev->param.inband_tags) ? 0 : packed_tags_size;
|
|
|
|
ops.len = dev->param.total_bytes_per_chunk;
|
2012-10-30 14:58:13 +00:00
|
|
|
ops.ooboffs = 0;
|
|
|
|
ops.datbuf = (__u8 *)data;
|
2012-10-30 14:58:17 +00:00
|
|
|
ops.oobbuf = (dev->param.inband_tags) ? NULL : packed_tags_ptr;
|
2012-10-30 14:58:13 +00:00
|
|
|
retval = mtd->write_oob(mtd, addr, &ops);
|
2008-06-10 08:20:19 +00:00
|
|
|
|
2012-10-30 14:58:13 +00:00
|
|
|
#else
|
2012-10-30 14:58:17 +00:00
|
|
|
if (!dev->param.inband_tags) {
|
2012-10-30 14:58:13 +00:00
|
|
|
retval =
|
2012-10-30 14:58:17 +00:00
|
|
|
mtd->write_ecc(mtd, addr, dev->data_bytes_per_chunk,
|
|
|
|
&dummy, data, (__u8 *) packed_tags_ptr, NULL);
|
2012-10-30 14:58:13 +00:00
|
|
|
} else {
|
|
|
|
retval =
|
2012-10-30 14:58:17 +00:00
|
|
|
mtd->write(mtd, addr, dev->param.total_bytes_per_chunk, &dummy,
|
2012-10-30 14:58:13 +00:00
|
|
|
data);
|
2007-10-10 18:05:48 +00:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
if (retval == 0)
|
|
|
|
return YAFFS_OK;
|
|
|
|
else
|
|
|
|
return YAFFS_FAIL;
|
|
|
|
}
|
|
|
|
|
2012-10-30 14:58:17 +00:00
|
|
|
int nandmtd2_ReadChunkWithTagsFromNAND(yaffs_dev_t *dev, int nand_chunk,
|
|
|
|
__u8 *data, yaffs_ext_tags *tags)
|
2007-10-10 18:05:48 +00:00
|
|
|
{
|
2012-10-30 14:58:17 +00:00
|
|
|
struct mtd_info *mtd = yaffs_dev_to_mtd(dev);
|
2012-10-30 14:58:13 +00:00
|
|
|
#if (MTD_VERSION_CODE > MTD_VERSION(2, 6, 17))
|
2007-10-10 18:05:48 +00:00
|
|
|
struct mtd_oob_ops ops;
|
|
|
|
#endif
|
|
|
|
size_t dummy;
|
|
|
|
int retval = 0;
|
2012-10-30 14:58:13 +00:00
|
|
|
int localData = 0;
|
2007-10-10 18:05:48 +00:00
|
|
|
|
2012-10-30 14:58:17 +00:00
|
|
|
loff_t addr = ((loff_t) nand_chunk) * dev->param.total_bytes_per_chunk;
|
2007-10-10 18:05:48 +00:00
|
|
|
|
|
|
|
yaffs_PackedTags2 pt;
|
|
|
|
|
2012-10-30 14:58:17 +00:00
|
|
|
int packed_tags_size = dev->param.no_tags_ecc ? sizeof(pt.t) : sizeof(pt);
|
|
|
|
void * packed_tags_ptr = dev->param.no_tags_ecc ? (void *) &pt.t: (void *)&pt;
|
|
|
|
|
2007-10-10 18:05:48 +00:00
|
|
|
T(YAFFS_TRACE_MTD,
|
|
|
|
(TSTR
|
|
|
|
("nandmtd2_ReadChunkWithTagsFromNAND chunk %d data %p tags %p"
|
2012-10-30 14:58:17 +00:00
|
|
|
TENDSTR), nand_chunk, data, tags));
|
2007-10-10 18:05:48 +00:00
|
|
|
|
2012-10-30 14:58:17 +00:00
|
|
|
if (dev->param.inband_tags) {
|
2012-10-30 14:58:13 +00:00
|
|
|
|
|
|
|
if (!data) {
|
|
|
|
localData = 1;
|
2012-10-30 14:58:17 +00:00
|
|
|
data = yaffs_get_temp_buffer(dev, __LINE__);
|
2012-10-30 14:58:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
|
2012-10-30 14:58:17 +00:00
|
|
|
if (dev->param.inband_tags || (data && !tags))
|
|
|
|
retval = mtd->read(mtd, addr, dev->param.total_bytes_per_chunk,
|
2007-10-10 18:05:48 +00:00
|
|
|
&dummy, data);
|
|
|
|
else if (tags) {
|
|
|
|
ops.mode = MTD_OOB_AUTO;
|
2012-10-30 14:58:17 +00:00
|
|
|
ops.ooblen = packed_tags_size;
|
|
|
|
ops.len = data ? dev->data_bytes_per_chunk : packed_tags_size;
|
2007-10-10 18:05:48 +00:00
|
|
|
ops.ooboffs = 0;
|
|
|
|
ops.datbuf = data;
|
2012-10-30 14:58:17 +00:00
|
|
|
ops.oobbuf = yaffs_dev_to_lc(dev)->spareBuffer;
|
2007-10-10 18:05:48 +00:00
|
|
|
retval = mtd->read_oob(mtd, addr, &ops);
|
|
|
|
}
|
|
|
|
#else
|
2012-10-30 14:58:17 +00:00
|
|
|
if (!dev->param.inband_tags && data && tags) {
|
2012-10-30 14:58:13 +00:00
|
|
|
|
2012-10-30 14:58:17 +00:00
|
|
|
retval = mtd->read_ecc(mtd, addr, dev->data_bytes_per_chunk,
|
2008-06-10 08:20:19 +00:00
|
|
|
&dummy, data, dev->spareBuffer,
|
|
|
|
NULL);
|
2007-10-10 18:05:48 +00:00
|
|
|
} else {
|
|
|
|
if (data)
|
|
|
|
retval =
|
2012-10-30 14:58:17 +00:00
|
|
|
mtd->read(mtd, addr, dev->data_bytes_per_chunk, &dummy,
|
2007-10-10 18:05:48 +00:00
|
|
|
data);
|
2012-10-30 14:58:17 +00:00
|
|
|
if (!dev->param.inband_tags && tags)
|
2007-10-10 18:05:48 +00:00
|
|
|
retval =
|
|
|
|
mtd->read_oob(mtd, addr, mtd->oobsize, &dummy,
|
|
|
|
dev->spareBuffer);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
2012-10-30 14:58:17 +00:00
|
|
|
if (dev->param.inband_tags) {
|
2012-10-30 14:58:13 +00:00
|
|
|
if (tags) {
|
|
|
|
yaffs_PackedTags2TagsPart *pt2tp;
|
2012-10-30 14:58:17 +00:00
|
|
|
pt2tp = (yaffs_PackedTags2TagsPart *)&data[dev->data_bytes_per_chunk];
|
|
|
|
yaffs_unpack_tags2tags_part(tags, pt2tp);
|
2012-10-30 14:58:13 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (tags) {
|
2012-10-30 14:58:17 +00:00
|
|
|
memcpy(packed_tags_ptr, yaffs_dev_to_lc(dev)->spareBuffer, packed_tags_size);
|
|
|
|
yaffs_unpack_tags2(tags, &pt, !dev->param.no_tags_ecc);
|
2012-10-30 14:58:13 +00:00
|
|
|
}
|
|
|
|
}
|
2008-05-28 07:31:26 +00:00
|
|
|
|
2012-10-30 14:58:13 +00:00
|
|
|
if (localData)
|
2012-10-30 14:58:17 +00:00
|
|
|
yaffs_release_temp_buffer(dev, data, __LINE__);
|
2008-06-10 08:20:19 +00:00
|
|
|
|
2012-10-30 14:58:17 +00:00
|
|
|
if (tags && retval == -EBADMSG && tags->ecc_result == YAFFS_ECC_RESULT_NO_ERROR) {
|
|
|
|
tags->ecc_result = YAFFS_ECC_RESULT_UNFIXED;
|
|
|
|
dev->n_ecc_unfixed++;
|
|
|
|
}
|
|
|
|
if(tags && retval == -EUCLEAN && tags->ecc_result == YAFFS_ECC_RESULT_NO_ERROR) {
|
|
|
|
tags->ecc_result = YAFFS_ECC_RESULT_FIXED;
|
|
|
|
dev->n_ecc_fixed++;
|
|
|
|
}
|
2007-10-10 18:05:48 +00:00
|
|
|
if (retval == 0)
|
|
|
|
return YAFFS_OK;
|
|
|
|
else
|
|
|
|
return YAFFS_FAIL;
|
|
|
|
}
|
|
|
|
|
2012-10-30 14:58:17 +00:00
|
|
|
int nandmtd2_MarkNANDBlockBad(struct yaffs_dev_s *dev, int block_no)
|
2007-10-10 18:05:48 +00:00
|
|
|
{
|
2012-10-30 14:58:17 +00:00
|
|
|
struct mtd_info *mtd = yaffs_dev_to_mtd(dev);
|
2007-10-10 18:05:48 +00:00
|
|
|
int retval;
|
|
|
|
T(YAFFS_TRACE_MTD,
|
2012-10-30 14:58:17 +00:00
|
|
|
(TSTR("nandmtd2_MarkNANDBlockBad %d" TENDSTR), block_no));
|
2007-10-10 18:05:48 +00:00
|
|
|
|
|
|
|
retval =
|
|
|
|
mtd->block_markbad(mtd,
|
2012-10-30 14:58:17 +00:00
|
|
|
block_no * dev->param.chunks_per_block *
|
|
|
|
dev->param.total_bytes_per_chunk);
|
2007-10-10 18:05:48 +00:00
|
|
|
|
|
|
|
if (retval == 0)
|
|
|
|
return YAFFS_OK;
|
|
|
|
else
|
|
|
|
return YAFFS_FAIL;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2012-10-30 14:58:17 +00:00
|
|
|
int nandmtd2_QueryNANDBlock(struct yaffs_dev_s *dev, int block_no,
|
|
|
|
yaffs_block_state_t *state, __u32 *seq_number)
|
2007-10-10 18:05:48 +00:00
|
|
|
{
|
2012-10-30 14:58:17 +00:00
|
|
|
struct mtd_info *mtd = yaffs_dev_to_mtd(dev);
|
2007-10-10 18:05:48 +00:00
|
|
|
int retval;
|
|
|
|
|
|
|
|
T(YAFFS_TRACE_MTD,
|
2012-10-30 14:58:17 +00:00
|
|
|
(TSTR("nandmtd2_QueryNANDBlock %d" TENDSTR), block_no));
|
2007-10-10 18:05:48 +00:00
|
|
|
retval =
|
|
|
|
mtd->block_isbad(mtd,
|
2012-10-30 14:58:17 +00:00
|
|
|
block_no * dev->param.chunks_per_block *
|
|
|
|
dev->param.total_bytes_per_chunk);
|
2007-10-10 18:05:48 +00:00
|
|
|
|
|
|
|
if (retval) {
|
|
|
|
T(YAFFS_TRACE_MTD, (TSTR("block is bad" TENDSTR)));
|
|
|
|
|
|
|
|
*state = YAFFS_BLOCK_STATE_DEAD;
|
2012-10-30 14:58:17 +00:00
|
|
|
*seq_number = 0;
|
2007-10-10 18:05:48 +00:00
|
|
|
} else {
|
2012-10-30 14:58:17 +00:00
|
|
|
yaffs_ext_tags t;
|
2007-10-10 18:05:48 +00:00
|
|
|
nandmtd2_ReadChunkWithTagsFromNAND(dev,
|
2012-10-30 14:58:17 +00:00
|
|
|
block_no *
|
|
|
|
dev->param.chunks_per_block, NULL,
|
2007-10-10 18:05:48 +00:00
|
|
|
&t);
|
|
|
|
|
2012-10-30 14:58:17 +00:00
|
|
|
if (t.chunk_used) {
|
|
|
|
*seq_number = t.seq_number;
|
2007-10-10 18:05:48 +00:00
|
|
|
*state = YAFFS_BLOCK_STATE_NEEDS_SCANNING;
|
|
|
|
} else {
|
2012-10-30 14:58:17 +00:00
|
|
|
*seq_number = 0;
|
2007-10-10 18:05:48 +00:00
|
|
|
*state = YAFFS_BLOCK_STATE_EMPTY;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
T(YAFFS_TRACE_MTD,
|
2012-10-30 14:58:17 +00:00
|
|
|
(TSTR("block is bad seq %d state %d" TENDSTR), *seq_number,
|
2007-10-10 18:05:48 +00:00
|
|
|
*state));
|
|
|
|
|
|
|
|
if (retval == 0)
|
|
|
|
return YAFFS_OK;
|
|
|
|
else
|
|
|
|
return YAFFS_FAIL;
|
|
|
|
}
|
|
|
|
|