int nandmtd_EraseBlockInNAND(yaffs_Device *dev, int blockNumber) { struct mtd_info *mtd = yaffs_DeviceToMtd(dev); __u32 addr = ((loff_t) blockNumber) * dev->param.totalBytesPerChunk * dev->param.nChunksPerBlock; struct erase_info ei; int retval = 0; ei.mtd = mtd; ei.addr = addr; ei.len = dev->param.totalBytesPerChunk * dev->param.nChunksPerBlock; 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; }
/* 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_DeviceStruct *dev, int blockNo) { struct mtd_info *mtd = yaffs_DeviceToMtd(dev); int blocksize = dev->param.nChunksPerBlock * dev->nDataBytesPerChunk; int retval; T(YAFFS_TRACE_BAD_BLOCKS,(TSTR("marking block %d bad"TENDSTR), blockNo)); retval = mtd->block_markbad(mtd, (loff_t)blocksize * blockNo); 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_Device *dev, int chunkInNAND, const __u8 *data, const yaffs_ExtendedTags *etags) { struct mtd_info *mtd = yaffs_DeviceToMtd(dev); int chunkBytes = dev->nDataBytesPerChunk; loff_t addr = ((loff_t)chunkInNAND) * chunkBytes; struct mtd_oob_ops ops; yaffs_PackedTags1 pt1; int retval; /* we assume that PackedTags1 and yaffs_Tags are compatible */ compile_time_assertion(sizeof(yaffs_PackedTags1) == 12); compile_time_assertion(sizeof(yaffs_Tags) == 8); yaffs_PackTags1(&pt1, etags); yaffs_CalcTagsECC((yaffs_Tags *)&pt1); /* When deleting a chunk, the upper layer provides only skeletal * etags, one with chunkDeleted 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->chunkDeleted) { memset(&pt1, 0xff, 8); /* clear delete status bit to indicate deleted */ pt1.deleted = 0; } #else ((__u8 *)&pt1)[8] = 0xff; if (etags->chunkDeleted) { memset(&pt1, 0xff, 8); /* zero pageStatus 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), chunkInNAND, retval)); } return retval ? YAFFS_FAIL : YAFFS_OK; }
int nandmtd2_MarkNANDBlockBad(struct yaffs_DeviceStruct *dev, int blockNo) { struct mtd_info *mtd = yaffs_DeviceToMtd(dev); int retval; T(YAFFS_TRACE_MTD, (TSTR("nandmtd2_MarkNANDBlockBad %d" TENDSTR), blockNo)); retval = mtd->block_markbad(mtd, blockNo * dev->param.nChunksPerBlock * dev->param.totalBytesPerChunk); if (retval == 0) return YAFFS_OK; else return YAFFS_FAIL; }
int nandmtd2_QueryNANDBlock(struct yaffs_DeviceStruct *dev, int blockNo, yaffs_BlockState *state, __u32 *sequenceNumber) { struct mtd_info *mtd = yaffs_DeviceToMtd(dev); int retval; T(YAFFS_TRACE_MTD, (TSTR("nandmtd2_QueryNANDBlock %d" TENDSTR), blockNo)); retval = mtd->block_isbad(mtd, blockNo * dev->param.nChunksPerBlock * dev->param.totalBytesPerChunk); if (retval) { T(YAFFS_TRACE_MTD, (TSTR("block is bad" TENDSTR))); *state = YAFFS_BLOCK_STATE_DEAD; *sequenceNumber = 0; } else { yaffs_ExtendedTags t; nandmtd2_ReadChunkWithTagsFromNAND(dev, blockNo * dev->param.nChunksPerBlock, NULL, &t); if (t.chunkUsed) { *sequenceNumber = t.sequenceNumber; *state = YAFFS_BLOCK_STATE_NEEDS_SCANNING; } else { *sequenceNumber = 0; *state = YAFFS_BLOCK_STATE_EMPTY; } } T(YAFFS_TRACE_MTD, (TSTR("block is bad seq %d state %d" TENDSTR), *sequenceNumber, *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_QueryNANDBlock(struct yaffs_DeviceStruct *dev, int blockNo, yaffs_BlockState *pState, __u32 *pSequenceNumber) { struct mtd_info *mtd = yaffs_DeviceToMtd(dev); int chunkNo = blockNo * dev->param.nChunksPerBlock; loff_t addr = (loff_t)chunkNo * dev->nDataBytesPerChunk; yaffs_ExtendedTags 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.blockBad = (mtd->block_isbad)(mtd, addr); if (etags.blockBad) { T(YAFFS_TRACE_BAD_BLOCKS, (TSTR("block %d is marked bad"TENDSTR), blockNo)); state = YAFFS_BLOCK_STATE_DEAD; } else if (etags.eccResult != YAFFS_ECC_RESULT_NO_ERROR) { /* bad tags, need to look more closely */ state = YAFFS_BLOCK_STATE_NEEDS_SCANNING; } else if (etags.chunkUsed) { state = YAFFS_BLOCK_STATE_NEEDS_SCANNING; seqnum = etags.sequenceNumber; } else { state = YAFFS_BLOCK_STATE_EMPTY; } *pState = state; *pSequenceNumber = seqnum; /* query always succeeds */ return YAFFS_OK; }
/* Read a chunk (page) from NAND. * * Caller expects ExtendedTags data to be usable even on error; that is, * all members except eccResult and blockBad are zeroed. * * - Check ECC results for data (if applicable) * - Check for blank/erased block (return empty ExtendedTags if blank) * - Check the PackedTags1 mini-ECC (correct if necessary/possible) * - Convert PackedTags1 to ExtendedTags * - Update eccResult and blockBad members to refect state. * * Returns YAFFS_OK or YAFFS_FAIL. */ int nandmtd1_ReadChunkWithTagsFromNAND(yaffs_Device *dev, int chunkInNAND, __u8 *data, yaffs_ExtendedTags *etags) { struct mtd_info *mtd = yaffs_DeviceToMtd(dev); int chunkBytes = dev->nDataBytesPerChunk; loff_t addr = ((loff_t)chunkInNAND) * chunkBytes; int eccres = YAFFS_ECC_RESULT_NO_ERROR; struct mtd_oob_ops ops; yaffs_PackedTags1 pt1; int retval; int deleted; memset(&ops, 0, sizeof(ops)); ops.mode = MTD_OOB_AUTO; ops.len = (data) ? chunkBytes : 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) { T(YAFFS_TRACE_MTD, (TSTR("read_oob failed, chunk %d, mtd error %d"TENDSTR), chunkInNAND, retval)); } switch (retval) { case 0: /* no error */ break; case -EUCLEAN: /* MTD's ECC fixed the data */ eccres = YAFFS_ECC_RESULT_FIXED; dev->eccFixed++; break; case -EBADMSG: /* MTD's ECC could not fix the data */ dev->eccUnfixed++; /* fall into... */ default: rettags(etags, YAFFS_ECC_RESULT_UNFIXED, 0); etags->blockBad = (mtd->block_isbad)(mtd, addr); return YAFFS_FAIL; } /* Check for a blank/erased chunk. */ if (yaffs_CheckFF((__u8 *)&pt1, 8)) { /* when blank, upper layers want eccResult 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 = (yaffs_CountBits(((__u8 *)&pt1)[8]) < 7); #endif /* Check the packed tags mini-ECC and correct if necessary/possible. */ retval = yaffs_CheckECCOnTags((yaffs_Tags *)&pt1); switch (retval) { case 0: /* no tags error, use MTD result */ break; case 1: /* recovered tags-ECC error */ dev->tagsEccFixed++; if (eccres == YAFFS_ECC_RESULT_NO_ERROR) eccres = YAFFS_ECC_RESULT_FIXED; break; default: /* unrecovered tags-ECC error */ dev->tagsEccUnfixed++; return rettags(etags, YAFFS_ECC_RESULT_UNFIXED, YAFFS_FAIL); } /* Unpack the tags to extended form and set ECC result. * [set shouldBeFF just to keep yaffs_UnpackTags1 happy] */ pt1.shouldBeFF = 0xFFFFFFFF; yaffs_UnpackTags1(etags, &pt1); etags->eccResult = eccres; /* Set deleted state */ etags->chunkDeleted = deleted; return YAFFS_OK; }
int nandmtd2_ReadChunkWithTagsFromNAND(yaffs_Device *dev, int chunkInNAND, __u8 *data, yaffs_ExtendedTags *tags) { struct mtd_info *mtd = yaffs_DeviceToMtd(dev); #if (MTD_VERSION_CODE > MTD_VERSION(2, 6, 17)) struct mtd_oob_ops ops; #endif size_t dummy; int retval = 0; int localData = 0; loff_t addr = ((loff_t) chunkInNAND) * dev->param.totalBytesPerChunk; yaffs_PackedTags2 pt; int packed_tags_size = dev->param.noTagsECC ? sizeof(pt.t) : sizeof(pt); void * packed_tags_ptr = dev->param.noTagsECC ? (void *) &pt.t: (void *)&pt; T(YAFFS_TRACE_MTD, (TSTR ("nandmtd2_ReadChunkWithTagsFromNAND chunk %d data %p tags %p" TENDSTR), chunkInNAND, data, tags)); if (dev->param.inbandTags) { if (!data) { localData = 1; data = yaffs_GetTempBuffer(dev, __LINE__); } } #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17)) if (dev->param.inbandTags || (data && !tags)) retval = mtd->read(mtd, addr, dev->param.totalBytesPerChunk, &dummy, data); else if (tags) { ops.mode = MTD_OOB_AUTO; ops.ooblen = packed_tags_size; ops.len = data ? dev->nDataBytesPerChunk : packed_tags_size; ops.ooboffs = 0; ops.datbuf = data; ops.oobbuf = yaffs_DeviceToLC(dev)->spareBuffer; retval = mtd->read_oob(mtd, addr, &ops); } #else if (!dev->param.inbandTags && data && tags) { retval = mtd->read_ecc(mtd, addr, dev->nDataBytesPerChunk, &dummy, data, dev->spareBuffer, NULL); } else { if (data) retval = mtd->read(mtd, addr, dev->nDataBytesPerChunk, &dummy, data); if (!dev->param.inbandTags && tags) retval = mtd->read_oob(mtd, addr, mtd->oobsize, &dummy, dev->spareBuffer); } #endif if (dev->param.inbandTags) { if (tags) { yaffs_PackedTags2TagsPart *pt2tp; pt2tp = (yaffs_PackedTags2TagsPart *)&data[dev->nDataBytesPerChunk]; yaffs_UnpackTags2TagsPart(tags, pt2tp); } } else { if (tags) { memcpy(packed_tags_ptr, yaffs_DeviceToLC(dev)->spareBuffer, packed_tags_size); yaffs_UnpackTags2(tags, &pt, !dev->param.noTagsECC); } } if (localData) yaffs_ReleaseTempBuffer(dev, data, __LINE__); if (tags && retval == -EBADMSG && tags->eccResult == YAFFS_ECC_RESULT_NO_ERROR) { tags->eccResult = YAFFS_ECC_RESULT_UNFIXED; dev->eccUnfixed++; } if(tags && retval == -EUCLEAN && tags->eccResult == YAFFS_ECC_RESULT_NO_ERROR) { tags->eccResult = YAFFS_ECC_RESULT_FIXED; dev->eccFixed++; } 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_Device *dev, int chunkInNAND, const __u8 *data, const yaffs_ExtendedTags *tags) { struct mtd_info *mtd = yaffs_DeviceToMtd(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.noTagsECC ? sizeof(pt.t) : sizeof(pt); void * packed_tags_ptr = dev->param.noTagsECC ? (void *) &pt.t : (void *)&pt; T(YAFFS_TRACE_MTD, (TSTR ("nandmtd2_WriteChunkWithTagsToNAND chunk %d data %p tags %p" TENDSTR), chunkInNAND, data, tags)); addr = ((loff_t) chunkInNAND) * dev->param.totalBytesPerChunk; /* 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.inbandTags) { yaffs_PackedTags2TagsPart *pt2tp; pt2tp = (yaffs_PackedTags2TagsPart *)(data + dev->nDataBytesPerChunk); yaffs_PackTags2TagsPart(pt2tp, tags); } else yaffs_PackTags2(&pt, tags, !dev->param.noTagsECC); #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17)) ops.mode = MTD_OOB_AUTO; ops.ooblen = (dev->param.inbandTags) ? 0 : packed_tags_size; ops.len = dev->param.totalBytesPerChunk; ops.ooboffs = 0; ops.datbuf = (__u8 *)data; ops.oobbuf = (dev->param.inbandTags) ? NULL : packed_tags_ptr; retval = mtd->write_oob(mtd, addr, &ops); #else if (!dev->param.inbandTags) { retval = mtd->write_ecc(mtd, addr, dev->nDataBytesPerChunk, &dummy, data, (__u8 *) packed_tags_ptr, NULL); } else { retval = mtd->write(mtd, addr, dev->param.totalBytesPerChunk, &dummy, data); } #endif if (retval == 0) return YAFFS_OK; else return YAFFS_FAIL; }