static int yaffs_mtd_write(struct yaffs_dev *dev, int nand_chunk, const u8 *data, int data_len, const u8 *oob, int oob_len) { struct mtd_info *mtd = yaffs_dev_to_mtd(dev); loff_t addr; struct mtd_oob_ops ops; int retval; addr = ((loff_t) nand_chunk) * dev->param.total_bytes_per_chunk; memset(&ops, 0, sizeof(ops)); ops.mode = MTD_OPS_AUTO_OOB; ops.len = (data) ? data_len : 0; ops.ooblen = oob_len; ops.datbuf = (u8 *)data; ops.oobbuf = (u8 *)oob; #if (LINUX_VERSION_CODE >= KERNEL_VERSION(3, 3, 0)) retval = mtd_write_oob(mtd, addr, &ops); #else retval = mtd->write_oob(mtd, addr, &ops); #endif if (retval) { yaffs_trace(YAFFS_TRACE_MTD, "write_oob failed, chunk %d, mtd error %d", nand_chunk, retval); } return retval ? YAFFS_FAIL : YAFFS_OK; }
static int yaffs_mtd_erase(struct yaffs_dev *dev, int block_no) { struct mtd_info *mtd = yaffs_dev_to_mtd(dev); loff_t addr; struct erase_info ei; int retval = 0; u32 block_size; block_size = dev->param.total_bytes_per_chunk * dev->param.chunks_per_block; addr = ((loff_t) block_no) * block_size; ei.mtd = mtd; ei.addr = addr; ei.len = block_size; ei.time = 1000; ei.retries = 2; ei.callback = NULL; ei.priv = (u_long) dev; #if (LINUX_VERSION_CODE >= KERNEL_VERSION(3, 3, 0)) retval = mtd_erase(mtd, &ei); #else retval = mtd->erase(mtd, &ei); #endif if (retval == 0) return YAFFS_OK; return YAFFS_FAIL; }
int nandmtd_erase_block(struct yaffs_dev *dev, int block_no) { struct mtd_info *mtd = yaffs_dev_to_mtd(dev); u32 addr = ((loff_t) block_no) * dev->param.total_bytes_per_chunk * dev->param.chunks_per_block; struct erase_info ei; int retval = 0; ei.mtd = mtd; ei.addr = addr; ei.len = dev->param.total_bytes_per_chunk * dev->param.chunks_per_block; ei.time = 1000; ei.retries = 2; ei.callback = NULL; ei.priv = (u_long) dev; retval = mtd_erase(mtd, &ei); if (retval == 0) return YAFFS_OK; else return YAFFS_FAIL; }
static int yaffs_mtd_read(struct yaffs_dev *dev, int nand_chunk, u8 *data, int data_len, u8 *oob, int oob_len, enum yaffs_ecc_result *ecc_result) { struct mtd_info *mtd = yaffs_dev_to_mtd(dev); loff_t addr; struct mtd_oob_ops ops; int retval; addr = ((loff_t) nand_chunk) * dev->param.total_bytes_per_chunk; memset(&ops, 0, sizeof(ops)); ops.mode = MTD_OPS_AUTO_OOB; ops.len = (data) ? data_len : 0; ops.ooblen = oob_len; ops.datbuf = data; ops.oobbuf = oob; #if (MTD_VERSION_CODE < MTD_VERSION(2, 6, 20)) /* In MTD 2.6.18 to 2.6.19 nand_base.c:nand_do_read_oob() has a bug; * help it out with ops.len = ops.ooblen when ops.datbuf == NULL. */ ops.len = (ops.datbuf) ? ops.len : ops.ooblen; #endif /* Read page and oob using MTD. * Check status and determine ECC result. */ retval = mtd_read_oob(mtd, addr, &ops); if (retval) yaffs_trace(YAFFS_TRACE_MTD, "read_oob failed, chunk %d, mtd error %d", nand_chunk, retval); switch (retval) { case 0: /* no error */ if(ecc_result) *ecc_result = YAFFS_ECC_RESULT_NO_ERROR; break; case -EUCLEAN: /* MTD's ECC fixed the data */ if(ecc_result) *ecc_result = YAFFS_ECC_RESULT_FIXED; dev->n_ecc_fixed++; break; case -EBADMSG: default: /* MTD's ECC could not fix the data */ dev->n_ecc_unfixed++; if(ecc_result) *ecc_result = YAFFS_ECC_RESULT_UNFIXED; return YAFFS_FAIL; } return YAFFS_OK; }
/* Write a chunk (page) of data to NAND. * * Caller always provides ExtendedTags data which are converted to a more * compact (packed) form for storage in NAND. A mini-ECC runs over the * contents of the tags meta-data; used to valid the tags when read. * * - Pack ExtendedTags to packed_tags1 form * - Compute mini-ECC for packed_tags1 * - Write data and packed tags to NAND. * * Note: Due to the use of the packed_tags1 meta-data which does not include * a full sequence number (as found in the larger packed_tags2 form) it is * necessary for Yaffs to re-write a chunk/page (just once) to mark it as * discarded and dirty. This is not ideal: newer NAND parts are supposed * to be written just once. When Yaffs performs this operation, this * function is called with a NULL data pointer -- calling MTD write_oob * without data is valid usage (2.6.17). * * Any underlying MTD error results in YAFFS_FAIL. * Returns YAFFS_OK or YAFFS_FAIL. */ int nandmtd1_write_chunk_tags(struct yaffs_dev *dev, int nand_chunk, const u8 *data, const struct yaffs_ext_tags *etags) { struct mtd_info *mtd = yaffs_dev_to_mtd(dev); int chunk_bytes = dev->data_bytes_per_chunk; loff_t addr = ((loff_t) nand_chunk) * chunk_bytes; struct mtd_oob_ops ops; struct yaffs_packed_tags1 pt1; int retval; /* we assume that packed_tags1 and struct yaffs_tags are compatible */ compile_time_assertion(sizeof(struct yaffs_packed_tags1) == 12); compile_time_assertion(sizeof(struct yaffs_tags) == 8); yaffs_pack_tags1(&pt1, etags); yaffs_calc_tags_ecc((struct yaffs_tags *)&pt1); /* When deleting a chunk, the upper layer provides only skeletal * etags, one with is_deleted set. However, we need to update the * tags, not erase them completely. So we use the NAND write property * that only zeroed-bits stick and set tag bytes to all-ones and * zero just the (not) deleted bit. */ if(dev->param.tags_9bytes) { ((u8 *) &pt1)[8] = 0xff; if (etags->is_deleted) { memset(&pt1, 0xff, 8); /* zero page_status byte to indicate deleted */ ((u8 *) &pt1)[8] = 0; } } else { if (etags->is_deleted) { memset(&pt1, 0xff, 8); /* clear delete status bit to indicate deleted */ pt1.deleted = 0; } } memset(&ops, 0, sizeof(ops)); ops.mode = MTD_OPS_AUTO_OOB; ops.len = (data) ? chunk_bytes : 0; ops.ooblen = dev->param.tags_9bytes ? 9 : 8; ops.datbuf = (u8 *) data; ops.oobbuf = (u8 *) &pt1; retval = mtd->write_oob(mtd, addr, &ops); if (retval) { yaffs_trace(YAFFS_TRACE_MTD, "write_oob failed, chunk %d, mtd error %d", nand_chunk, retval); } return retval ? YAFFS_FAIL : YAFFS_OK; }
static int yaffs_mtd_check_bad(struct yaffs_dev *dev, int block_no) { struct mtd_info *mtd = yaffs_dev_to_mtd(dev); int blocksize = dev->param.chunks_per_block * dev->param.total_bytes_per_chunk; int retval; yaffs_trace(YAFFS_TRACE_BAD_BLOCKS, "checking block %d bad", block_no); retval = mtd_block_isbad(mtd, (loff_t) blocksize * block_no); return (retval) ? YAFFS_FAIL : YAFFS_OK; }
/* Mark a block bad. * * This is a persistant state. * Use of this function should be rare. * * Returns YAFFS_OK or YAFFS_FAIL. */ int nandmtd1_MarkNANDBlockBad(struct yaffs_dev_s *dev, int block_no) { struct mtd_info *mtd = yaffs_dev_to_mtd(dev); int blocksize = dev->param.chunks_per_block * dev->data_bytes_per_chunk; int retval; T(YAFFS_TRACE_BAD_BLOCKS,(TSTR("marking block %d bad"TENDSTR), block_no)); retval = mtd->block_markbad(mtd, (loff_t)blocksize * block_no); return (retval) ? YAFFS_FAIL : YAFFS_OK; }
/* Mark a block bad. * * This is a persistant state. * Use of this function should be rare. * * Returns YAFFS_OK or YAFFS_FAIL. */ int nandmtd1_mark_block_bad(struct yaffs_dev *dev, int block_no) { struct mtd_info *mtd = yaffs_dev_to_mtd(dev); int blocksize = dev->param.chunks_per_block * dev->data_bytes_per_chunk; int retval; yaffs_trace(YAFFS_TRACE_BAD_BLOCKS, "marking block %d bad", block_no); retval = mtd->block_markbad(mtd, (loff_t) blocksize * block_no); return (retval) ? YAFFS_FAIL : YAFFS_OK; }
/* Write a chunk (page) of data to NAND. * * Caller always provides ExtendedTags data which are converted to a more * compact (packed) form for storage in NAND. A mini-ECC runs over the * contents of the tags meta-data; used to valid the tags when read. * * - Pack ExtendedTags to PackedTags1 form * - Compute mini-ECC for PackedTags1 * - Write data and packed tags to NAND. * * Note: Due to the use of the PackedTags1 meta-data which does not include * a full sequence number (as found in the larger PackedTags2 form) it is * necessary for Yaffs to re-write a chunk/page (just once) to mark it as * discarded and dirty. This is not ideal: newer NAND parts are supposed * to be written just once. When Yaffs performs this operation, this * function is called with a NULL data pointer -- calling MTD write_oob * without data is valid usage (2.6.17). * * Any underlying MTD error results in YAFFS_FAIL. * Returns YAFFS_OK or YAFFS_FAIL. */ int nandmtd1_WriteChunkWithTagsToNAND(yaffs_dev_t *dev, int nand_chunk, const __u8 *data, const yaffs_ext_tags *etags) { struct mtd_info *mtd = yaffs_dev_to_mtd(dev); int chunkBytes = dev->data_bytes_per_chunk; loff_t addr = ((loff_t)nand_chunk) * chunkBytes; struct mtd_oob_ops ops; yaffs_PackedTags1 pt1; int retval; /* we assume that PackedTags1 and yaffs_tags_t are compatible */ compile_time_assertion(sizeof(yaffs_PackedTags1) == 12); compile_time_assertion(sizeof(yaffs_tags_t) == 8); yaffs_PackTags1(&pt1, etags); yaffs_calc_tags_ecc((yaffs_tags_t *)&pt1); /* When deleting a chunk, the upper layer provides only skeletal * etags, one with is_deleted set. However, we need to update the * tags, not erase them completely. So we use the NAND write property * that only zeroed-bits stick and set tag bytes to all-ones and * zero just the (not) deleted bit. */ #ifndef CONFIG_YAFFS_9BYTE_TAGS if (etags->is_deleted) { memset(&pt1, 0xff, 8); /* clear delete status bit to indicate deleted */ pt1.deleted = 0; } #else ((__u8 *)&pt1)[8] = 0xff; if (etags->is_deleted) { memset(&pt1, 0xff, 8); /* zero page_status byte to indicate deleted */ ((__u8 *)&pt1)[8] = 0; } #endif memset(&ops, 0, sizeof(ops)); ops.mode = MTD_OOB_AUTO; ops.len = (data) ? chunkBytes : 0; ops.ooblen = YTAG1_SIZE; ops.datbuf = (__u8 *)data; ops.oobbuf = (__u8 *)&pt1; retval = mtd->write_oob(mtd, addr, &ops); if (retval) { T(YAFFS_TRACE_MTD, (TSTR("write_oob failed, chunk %d, mtd error %d"TENDSTR), nand_chunk, retval)); } return retval ? YAFFS_FAIL : YAFFS_OK; }
/* NB For use with inband tags.... * We assume that the data buffer is of size total_bytes_per_chunk so that we can also * use it to load the tags. */ int nandmtd2_write_chunk_tags(struct yaffs_dev *dev, int nand_chunk, const u8 * data, const struct yaffs_ext_tags *tags) { struct mtd_info *mtd = yaffs_dev_to_mtd(dev); struct mtd_oob_ops ops; int retval = 0; loff_t addr; struct yaffs_packed_tags2 pt; 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; yaffs_trace(YAFFS_TRACE_MTD, "nandmtd2_write_chunk_tags chunk %d data %p tags %p", nand_chunk, data, tags); addr = ((loff_t) nand_chunk) * dev->param.total_bytes_per_chunk; /* 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(); else if (dev->param.inband_tags) { struct yaffs_packed_tags2_tags_only *pt2tp; pt2tp = (struct yaffs_packed_tags2_tags_only *)(data + dev-> data_bytes_per_chunk); yaffs_pack_tags2_tags_only(pt2tp, tags); } else { yaffs_pack_tags2(&pt, tags, !dev->param.no_tags_ecc); } ops.mode = MTD_OOB_AUTO; ops.ooblen = (dev->param.inband_tags) ? 0 : packed_tags_size; ops.len = dev->param.total_bytes_per_chunk; ops.ooboffs = 0; ops.datbuf = (u8 *) data; ops.oobbuf = (dev->param.inband_tags) ? NULL : packed_tags_ptr; retval = mtd->write_oob(mtd, addr, &ops); if (retval == 0) return YAFFS_OK; else return YAFFS_FAIL; }
static int yaffs_mtd_check_bad(struct yaffs_dev *dev, int block_no) { struct mtd_info *mtd = yaffs_dev_to_mtd(dev); int blocksize = dev->param.chunks_per_block * dev->data_bytes_per_chunk; int retval; yaffs_trace(YAFFS_TRACE_SCAN, "checking block %d bad", block_no); #if (LINUX_VERSION_CODE >= KERNEL_VERSION(3, 3, 0)) retval = mtd_block_isbad(mtd, (loff_t) blocksize * block_no); #else retval = mtd->block_isbad(mtd, (loff_t) blocksize * block_no); #endif return (retval) ? YAFFS_FAIL : YAFFS_OK; }
int nandmtd2_query_block(struct yaffs_dev *dev, int block_no, enum yaffs_block_state *state, u32 * seq_number) { struct mtd_info *mtd = yaffs_dev_to_mtd(dev); int retval; yaffs_trace(YAFFS_TRACE_MTD, "nandmtd2_query_block %d", block_no); retval = mtd_block_isbad(mtd, block_no * dev->param.chunks_per_block * dev->param.total_bytes_per_chunk); if (retval) { yaffs_trace(YAFFS_TRACE_MTD, "block is bad"); *state = YAFFS_BLOCK_STATE_DEAD; *seq_number = 0; } else { struct yaffs_ext_tags t; nandmtd2_read_chunk_tags(dev, block_no * dev->param.chunks_per_block, NULL, &t); if (t.chunk_used) { *seq_number = t.seq_number; *state = YAFFS_BLOCK_STATE_NEEDS_SCANNING; } else { *seq_number = 0; *state = YAFFS_BLOCK_STATE_EMPTY; } /*if ecc unfixed, set the block empty. This block will be erased when allocate.*/ if(t.ecc_result == YAFFS_ECC_RESULT_UNFIXED) { printk("yaffsdebug : unfixed ecc error when scan block, seqnum:0x%x\n",t.seq_number); *seq_number = 0; *state = YAFFS_BLOCK_STATE_EMPTY; } } yaffs_trace(YAFFS_TRACE_MTD, "block is bad seq %d state %d", *seq_number, *state); if (retval == 0) return YAFFS_OK; else return YAFFS_FAIL; }
int nandmtd2_MarkNANDBlockBad(struct yaffs_dev_s *dev, int block_no) { struct mtd_info *mtd = yaffs_dev_to_mtd(dev); int retval; T(YAFFS_TRACE_MTD, (TSTR("nandmtd2_MarkNANDBlockBad %d" TENDSTR), block_no)); retval = mtd->block_markbad(mtd, block_no * dev->param.chunks_per_block * dev->param.total_bytes_per_chunk); if (retval == 0) return YAFFS_OK; else return YAFFS_FAIL; }
int nandmtd2_mark_block_bad(struct yaffs_dev *dev, int block_no) { struct mtd_info *mtd = yaffs_dev_to_mtd(dev); int retval; yaffs_trace(YAFFS_TRACE_MTD, "nandmtd2_mark_block_bad %d", block_no); retval = mtd_block_markbad(mtd, block_no * dev->param.chunks_per_block * dev->param.total_bytes_per_chunk); if (retval == 0) return YAFFS_OK; else return YAFFS_FAIL; }
int nandmtd2_QueryNANDBlock(struct yaffs_dev_s *dev, int block_no, yaffs_block_state_t *state, __u32 *seq_number) { struct mtd_info *mtd = yaffs_dev_to_mtd(dev); int retval; T(YAFFS_TRACE_MTD, (TSTR("nandmtd2_QueryNANDBlock %d" TENDSTR), block_no)); retval = mtd->block_isbad(mtd, block_no * dev->param.chunks_per_block * dev->param.total_bytes_per_chunk); if (retval) { T(YAFFS_TRACE_MTD, (TSTR("block is bad" TENDSTR))); *state = YAFFS_BLOCK_STATE_DEAD; *seq_number = 0; } else { yaffs_ext_tags t; nandmtd2_ReadChunkWithTagsFromNAND(dev, block_no * dev->param.chunks_per_block, NULL, &t); if (t.chunk_used) { *seq_number = t.seq_number; *state = YAFFS_BLOCK_STATE_NEEDS_SCANNING; } else { *seq_number = 0; *state = YAFFS_BLOCK_STATE_EMPTY; } } T(YAFFS_TRACE_MTD, (TSTR("block is bad seq %d state %d" TENDSTR), *seq_number, *state)); if (retval == 0) return YAFFS_OK; else return YAFFS_FAIL; }
/* Query for the current state of a specific block. * * Examine the tags of the first chunk of the block and return the state: * - YAFFS_BLOCK_STATE_DEAD, the block is marked bad * - YAFFS_BLOCK_STATE_NEEDS_SCANNING, the block is in use * - YAFFS_BLOCK_STATE_EMPTY, the block is clean * * Always returns YAFFS_OK. */ int nandmtd1_query_block(struct yaffs_dev *dev, int block_no, enum yaffs_block_state *state_ptr, u32 * seq_ptr) { struct mtd_info *mtd = yaffs_dev_to_mtd(dev); int chunk_num = block_no * dev->param.chunks_per_block; loff_t addr = (loff_t) chunk_num * dev->data_bytes_per_chunk; struct yaffs_ext_tags etags; int state = YAFFS_BLOCK_STATE_DEAD; int seqnum = 0; int retval; /* We don't yet have a good place to test for MTD config prerequists. * Do it here as we are called during the initial scan. */ if (nandmtd1_test_prerequists(mtd) != YAFFS_OK) return YAFFS_FAIL; retval = nandmtd1_read_chunk_tags(dev, chunk_num, NULL, &etags); etags.block_bad = (mtd->block_isbad) (mtd, addr); if (etags.block_bad) { yaffs_trace(YAFFS_TRACE_BAD_BLOCKS, "block %d is marked bad", block_no); state = YAFFS_BLOCK_STATE_DEAD; } else if (etags.ecc_result != YAFFS_ECC_RESULT_NO_ERROR) { /* bad tags, need to look more closely */ state = YAFFS_BLOCK_STATE_NEEDS_SCANNING; } else if (etags.chunk_used) { state = YAFFS_BLOCK_STATE_NEEDS_SCANNING; seqnum = etags.seq_number; } else { state = YAFFS_BLOCK_STATE_EMPTY; } *state_ptr = state; *seq_ptr = seqnum; /* query always succeeds */ return YAFFS_OK; }
int nandmtd2_query_block(struct yaffs_dev *dev, int block_no, enum yaffs_block_state *state, u32 *seq_number) { struct mtd_info *mtd = yaffs_dev_to_mtd(dev); int retval; yaffs_trace(YAFFS_TRACE_MTD, "nandmtd2_query_block %d", block_no); retval = mtd->block_isbad(mtd, ((loff_t)block_no) * dev->param.chunks_per_block * dev->param.total_bytes_per_chunk); if (retval) { yaffs_trace(YAFFS_TRACE_MTD, "block is bad"); *state = YAFFS_BLOCK_STATE_DEAD; *seq_number = 0; } else { struct yaffs_ext_tags t; nandmtd2_read_chunk_tags(dev, block_no * dev->param.chunks_per_block, NULL, &t); if (t.chunk_used) { *seq_number = t.seq_number; *state = YAFFS_BLOCK_STATE_NEEDS_SCAN; } else { *seq_number = 0; *state = YAFFS_BLOCK_STATE_EMPTY; } } yaffs_trace(YAFFS_TRACE_MTD, "block is bad seq %d state %d", *seq_number, *state); if (retval == 0) return YAFFS_OK; else return YAFFS_FAIL; }
static int yaffs_mtd_write(struct yaffs_dev *dev, int nand_chunk, const u8 *data, int data_len, const u8 *oob, int oob_len) { struct mtd_info *mtd = yaffs_dev_to_mtd(dev); loff_t addr; struct mtd_oob_ops ops; int retval; yaffs_trace(YAFFS_TRACE_MTD, "yaffs_mtd_write(%p, %d, %p, %d, %p, %d)\n", dev, nand_chunk, data, data_len, oob, oob_len); if (!data || !data_len) { data = NULL; data_len = 0; } if (!oob || !oob_len) { oob = NULL; oob_len = 0; } addr = ((loff_t) nand_chunk) * dev->param.total_bytes_per_chunk; memset(&ops, 0, sizeof(ops)); ops.mode = MTD_OPS_AUTO_OOB; ops.len = (data) ? data_len : 0; ops.ooblen = oob_len; ops.datbuf = (u8 *)data; ops.oobbuf = (u8 *)oob; retval = mtd_write_oob(mtd, addr, &ops); if (retval) { yaffs_trace(YAFFS_TRACE_MTD, "write_oob failed, chunk %d, mtd error %d", nand_chunk, retval); } return retval ? YAFFS_FAIL : YAFFS_OK; }
/* Query for the current state of a specific block. * * Examine the tags of the first chunk of the block and return the state: * - YAFFS_BLOCK_STATE_DEAD, the block is marked bad * - YAFFS_BLOCK_STATE_NEEDS_SCANNING, the block is in use * - YAFFS_BLOCK_STATE_EMPTY, the block is clean * * Always returns YAFFS_OK. */ int nandmtd1_QueryNANDBlock(struct yaffs_dev_s *dev, int block_no, yaffs_block_state_t *pState, __u32 *pSequenceNumber) { struct mtd_info *mtd = yaffs_dev_to_mtd(dev); int chunkNo = block_no * dev->param.chunks_per_block; loff_t addr = (loff_t)chunkNo * dev->data_bytes_per_chunk; yaffs_ext_tags etags; int state = YAFFS_BLOCK_STATE_DEAD; int seqnum = 0; int retval; /* We don't yet have a good place to test for MTD config prerequists. * Do it here as we are called during the initial scan. */ if (nandmtd1_TestPrerequists(mtd) != YAFFS_OK) return YAFFS_FAIL; retval = nandmtd1_ReadChunkWithTagsFromNAND(dev, chunkNo, NULL, &etags); etags.block_bad = (mtd->block_isbad)(mtd, addr); if (etags.block_bad) { T(YAFFS_TRACE_BAD_BLOCKS, (TSTR("block %d is marked bad"TENDSTR), block_no)); state = YAFFS_BLOCK_STATE_DEAD; } else if (etags.ecc_result != YAFFS_ECC_RESULT_NO_ERROR) { /* bad tags, need to look more closely */ state = YAFFS_BLOCK_STATE_NEEDS_SCANNING; } else if (etags.chunk_used) { state = YAFFS_BLOCK_STATE_NEEDS_SCANNING; seqnum = etags.seq_number; } else { state = YAFFS_BLOCK_STATE_EMPTY; } *pState = state; *pSequenceNumber = seqnum; /* query always succeeds */ return YAFFS_OK; }
int nandmtd2_read_chunk_tags(struct yaffs_dev *dev, int nand_chunk, u8 *data, struct yaffs_ext_tags *tags) { struct mtd_info *mtd = yaffs_dev_to_mtd(dev); #if (MTD_VERSION_CODE > MTD_VERSION(2, 6, 17)) struct mtd_oob_ops ops; #endif size_t dummy; int retval = 0; int local_data = 0; loff_t addr = ((loff_t) nand_chunk) * dev->param.total_bytes_per_chunk; struct yaffs_packed_tags2 pt; 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; yaffs_trace(YAFFS_TRACE_MTD, "nandmtd2_read_chunk_tags chunk %d data %p tags %p", nand_chunk, data, tags); if (dev->param.inband_tags) { if (!data) { local_data = 1; data = yaffs_get_temp_buffer(dev); } } #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17)) if (dev->param.inband_tags || (data && !tags)) retval = mtd->read(mtd, addr, dev->param.total_bytes_per_chunk, &dummy, data); else if (tags) { ops.mode = MTD_OPS_AUTO_OOB; ops.ooblen = packed_tags_size; ops.len = data ? dev->data_bytes_per_chunk : packed_tags_size; ops.ooboffs = 0; ops.datbuf = data; ops.oobbuf = yaffs_dev_to_lc(dev)->spare_buffer; retval = mtd->read_oob(mtd, addr, &ops); } #else if (!dev->param.inband_tags && data && tags) { retval = mtd->read_ecc(mtd, addr, dev->data_bytes_per_chunk, &dummy, data, dev->spare_buffer, NULL); } else { if (data) retval = mtd->read(mtd, addr, dev->data_bytes_per_chunk, &dummy, data); if (!dev->param.inband_tags && tags) retval = mtd->read_oob(mtd, addr, mtd->oobsize, &dummy, dev->spare_buffer); } #endif if (dev->param.inband_tags) { if (tags) { struct yaffs_packed_tags2_tags_only *pt2tp; pt2tp = (struct yaffs_packed_tags2_tags_only *) &data[dev->data_bytes_per_chunk]; yaffs_unpack_tags2_tags_only(tags, pt2tp); } } else { if (tags) { memcpy(packed_tags_ptr, yaffs_dev_to_lc(dev)->spare_buffer, packed_tags_size); yaffs_unpack_tags2(tags, &pt, !dev->param.no_tags_ecc); } } if (local_data) yaffs_release_temp_buffer(dev, data); 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++; } if (retval == 0) return YAFFS_OK; else return YAFFS_FAIL; }
/* 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. */ int nandmtd2_WriteChunkWithTagsToNAND(yaffs_dev_t *dev, int nand_chunk, const __u8 *data, const yaffs_ext_tags *tags) { struct mtd_info *mtd = yaffs_dev_to_mtd(dev); #if (MTD_VERSION_CODE > MTD_VERSION(2, 6, 17)) struct mtd_oob_ops ops; #else size_t dummy; #endif int retval = 0; loff_t addr; yaffs_PackedTags2 pt; 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; T(YAFFS_TRACE_MTD, (TSTR ("nandmtd2_WriteChunkWithTagsToNAND chunk %d data %p tags %p" TENDSTR), nand_chunk, data, tags)); addr = ((loff_t) nand_chunk) * dev->param.total_bytes_per_chunk; /* 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(); else if (dev->param.inband_tags) { yaffs_PackedTags2TagsPart *pt2tp; pt2tp = (yaffs_PackedTags2TagsPart *)(data + dev->data_bytes_per_chunk); yaffs_PackTags2TagsPart(pt2tp, tags); } else yaffs_PackTags2(&pt, tags, !dev->param.no_tags_ecc); #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17)) ops.mode = MTD_OOB_AUTO; ops.ooblen = (dev->param.inband_tags) ? 0 : packed_tags_size; ops.len = dev->param.total_bytes_per_chunk; ops.ooboffs = 0; ops.datbuf = (__u8 *)data; ops.oobbuf = (dev->param.inband_tags) ? NULL : packed_tags_ptr; retval = mtd->write_oob(mtd, addr, &ops); #else if (!dev->param.inband_tags) { retval = mtd->write_ecc(mtd, addr, dev->data_bytes_per_chunk, &dummy, data, (__u8 *) packed_tags_ptr, NULL); } else { retval = mtd->write(mtd, addr, dev->param.total_bytes_per_chunk, &dummy, data); } #endif if (retval == 0) return YAFFS_OK; else return YAFFS_FAIL; }
/* Read a chunk (page) from NAND. * * Caller expects ExtendedTags data to be usable even on error; that is, * all members except ecc_result and block_bad are zeroed. * * - Check ECC results for data (if applicable) * - Check for blank/erased block (return empty ExtendedTags if blank) * - Check the packed_tags1 mini-ECC (correct if necessary/possible) * - Convert packed_tags1 to ExtendedTags * - Update ecc_result and block_bad members to refect state. * * Returns YAFFS_OK or YAFFS_FAIL. */ int nandmtd1_read_chunk_tags(struct yaffs_dev *dev, int nand_chunk, u8 *data, struct yaffs_ext_tags *etags) { struct mtd_info *mtd = yaffs_dev_to_mtd(dev); int chunk_bytes = dev->data_bytes_per_chunk; loff_t addr = ((loff_t) nand_chunk) * chunk_bytes; int eccres = YAFFS_ECC_RESULT_NO_ERROR; struct mtd_oob_ops ops; struct yaffs_packed_tags1 pt1; int retval; int deleted; memset(&ops, 0, sizeof(ops)); ops.mode = MTD_OOB_AUTO; ops.len = (data) ? chunk_bytes : 0; ops.ooblen = YTAG1_SIZE; ops.datbuf = data; ops.oobbuf = (u8 *) &pt1; #if (MTD_VERSION_CODE < MTD_VERSION(2, 6, 20)) /* In MTD 2.6.18 to 2.6.19 nand_base.c:nand_do_read_oob() has a bug; * help it out with ops.len = ops.ooblen when ops.datbuf == NULL. */ ops.len = (ops.datbuf) ? ops.len : ops.ooblen; #endif /* Read page and oob using MTD. * Check status and determine ECC result. */ retval = mtd->read_oob(mtd, addr, &ops); if (retval) yaffs_trace(YAFFS_TRACE_MTD, "read_oob failed, chunk %d, mtd error %d", nand_chunk, retval); switch (retval) { case 0: /* no error */ break; case -EUCLEAN: /* MTD's ECC fixed the data */ eccres = YAFFS_ECC_RESULT_FIXED; dev->n_ecc_fixed++; break; case -EBADMSG: /* MTD's ECC could not fix the data */ dev->n_ecc_unfixed++; /* fall into... */ default: rettags(etags, YAFFS_ECC_RESULT_UNFIXED, 0); etags->block_bad = (mtd->block_isbad) (mtd, addr); return YAFFS_FAIL; } /* Check for a blank/erased chunk. */ if (yaffs_check_ff((u8 *) &pt1, 8)) { /* when blank, upper layers want ecc_result to be <= NO_ERROR */ return rettags(etags, YAFFS_ECC_RESULT_NO_ERROR, YAFFS_OK); } #ifndef CONFIG_YAFFS_9BYTE_TAGS /* Read deleted status (bit) then return it to it's non-deleted * state before performing tags mini-ECC check. pt1.deleted is * inverted. */ deleted = !pt1.deleted; pt1.deleted = 1; #else deleted = (hweight8(((u8 *) &pt1)[8]) < 7); #endif /* Check the packed tags mini-ECC and correct if necessary/possible. */ retval = yaffs_check_tags_ecc((struct yaffs_tags *)&pt1); switch (retval) { case 0: /* no tags error, use MTD result */ break; case 1: /* recovered tags-ECC error */ dev->n_tags_ecc_fixed++; if (eccres == YAFFS_ECC_RESULT_NO_ERROR) eccres = YAFFS_ECC_RESULT_FIXED; break; default: /* unrecovered tags-ECC error */ dev->n_tags_ecc_unfixed++; return rettags(etags, YAFFS_ECC_RESULT_UNFIXED, YAFFS_FAIL); } /* Unpack the tags to extended form and set ECC result. * [set should_be_ff just to keep yaffs_unpack_tags1 happy] */ pt1.should_be_ff = 0xFFFFFFFF; yaffs_unpack_tags1(etags, &pt1); etags->ecc_result = eccres; /* Set deleted state */ etags->is_deleted = deleted; return YAFFS_OK; }
/* NB For use with inband tags.... * We assume that the data buffer is of size total_bytes_per_chunk so that we can also * use it to load the tags. */ int nandmtd2_write_chunk_tags(struct yaffs_dev *dev, int nand_chunk, const u8 * data, const struct yaffs_ext_tags *tags) { struct mtd_info *mtd = yaffs_dev_to_mtd(dev); #if (MTD_VERSION_CODE > MTD_VERSION(2, 6, 17)) struct mtd_oob_ops ops; #else size_t dummy; #endif int retval = 0; loff_t addr; struct yaffs_packed_tags2 pt; u8 *encrypted_data = NULL; 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; yaffs_trace(YAFFS_TRACE_MTD, "nandmtd2_write_chunk_tags chunk %d data %p tags %p", nand_chunk, data, tags); addr = ((loff_t) nand_chunk) * dev->param.total_bytes_per_chunk; /* 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(); else if (dev->param.inband_tags) { struct yaffs_packed_tags2_tags_only *pt2tp; pt2tp = (struct yaffs_packed_tags2_tags_only *)(data + dev-> data_bytes_per_chunk); yaffs_pack_tags2_tags_only(pt2tp, tags); } else { yaffs_pack_tags2(&pt, tags, !dev->param.no_tags_ecc); } #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17)) if (dev->is_encrypted_fs) { if (dev->param.inband_tags) BUG(); encrypted_data = yaffs_get_temp_buffer(dev, __LINE__); memcpy(encrypted_data, data, dev->param.total_bytes_per_chunk); AES_xts_encrypt(dev->cipher, encrypted_data, encrypted_data, nand_chunk * 2, dev->param.total_bytes_per_chunk, packed_tags_ptr+SEQUENCE_OFFSET, packed_tags_ptr+SEQUENCE_OFFSET, (nand_chunk * 2) + 1, packed_tags_size-SEQUENCE_OFFSET); } ops.mode = MTD_OOB_AUTO; ops.ooblen = (dev->param.inband_tags) ? 0 : packed_tags_size; ops.len = dev->param.total_bytes_per_chunk; ops.ooboffs = 0; ops.datbuf = dev->is_encrypted_fs ? encrypted_data : (u8 *) data; ops.oobbuf = (dev->param.inband_tags) ? NULL : packed_tags_ptr; retval = mtd->write_oob(mtd, addr, &ops); if (dev->is_encrypted_fs && encrypted_data) yaffs_release_temp_buffer(dev, encrypted_data, __LINE__); #else if (!dev->param.inband_tags) { retval = mtd->write_ecc(mtd, addr, dev->data_bytes_per_chunk, &dummy, data, (u8 *) packed_tags_ptr, NULL); } else { retval = mtd->write(mtd, addr, dev->param.total_bytes_per_chunk, &dummy, data); } #endif if (retval == 0) return YAFFS_OK; else return YAFFS_FAIL; }
int nandmtd2_read_chunk_tags(struct yaffs_dev *dev, int nand_chunk, u8 * data, struct yaffs_ext_tags *tags) { struct mtd_info *mtd = yaffs_dev_to_mtd(dev); struct mtd_oob_ops ops; size_t dummy; int retval = 0; int local_data = 0; loff_t addr = ((loff_t) nand_chunk) * dev->param.total_bytes_per_chunk; struct yaffs_packed_tags2 pt; 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; yaffs_trace(YAFFS_TRACE_MTD, "nandmtd2_read_chunk_tags chunk %d data %p tags %p", nand_chunk, data, tags); if (dev->param.inband_tags) { if (!data) { local_data = 1; data = yaffs_get_temp_buffer(dev, __LINE__); } } if (dev->param.inband_tags || (data && !tags)) retval = mtd_read(mtd, addr, dev->param.total_bytes_per_chunk, &dummy, data); else if (tags) { #if 1 if(!data ) { local_data = 1; data = yaffs_get_temp_buffer(dev, __LINE__); } ops.mode = MTD_OPS_AUTO_OOB; ops.ooblen = packed_tags_size; ops.ooboffs = 0; ops.len = dev->data_bytes_per_chunk; ops.datbuf = data; ops.oobbuf = yaffs_dev_to_lc(dev)->spare_buffer; retval = mtd_read_oob(mtd, addr, &ops); if(retval) { printk(KERN_ERR"yaffsdebug mtdiferror %s retval:%d unfixed:%d fixed:%d chunk:%d addr:0x%llx\n",\ (retval == -EUCLEAN)?"fix ecc":"unfix ecc",retval,dev->n_ecc_unfixed,dev->n_ecc_fixed,nand_chunk,addr); #ifdef YAFFS_MVG_TEST_DEBUG_LOG if(retval != -EUCLEAN) { printk(KERN_ERR"dump checksum_BCH chunk:%d addr:0x%x\n",nand_chunk,addr); mtk_dump_byte(ops.datbuf,ops.len,0); printk(KERN_ERR"dump BCH oob\n"); mtk_dump_byte(ops.oobbuf,ops.ooblen,0); } #endif } #else ops.mode = MTD_OOB_AUTO; ops.ooblen = packed_tags_size; ops.len = data ? dev->data_bytes_per_chunk : packed_tags_size; ops.ooboffs = 0; ops.datbuf = data; ops.oobbuf = yaffs_dev_to_lc(dev)->spare_buffer; retval = mtd->read_oob(mtd, addr, &ops); #endif } if (dev->param.inband_tags) { if (tags) { struct yaffs_packed_tags2_tags_only *pt2tp; pt2tp = (struct yaffs_packed_tags2_tags_only *)&data[dev-> data_bytes_per_chunk]; yaffs_unpack_tags2_tags_only(tags, pt2tp); } } else { if (tags) { memcpy(packed_tags_ptr, yaffs_dev_to_lc(dev)->spare_buffer, packed_tags_size); yaffs_unpack_tags2(tags, &pt, !dev->param.no_tags_ecc); } } if (local_data) yaffs_release_temp_buffer(dev, data, __LINE__); 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++; } if (retval == 0) return YAFFS_OK; else return YAFFS_FAIL; }