18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * inftlcore.c -- Linux driver for Inverse Flash Translation Layer (INFTL)
48c2ecf20Sopenharmony_ci *
58c2ecf20Sopenharmony_ci * Copyright © 2002, Greg Ungerer (gerg@snapgear.com)
68c2ecf20Sopenharmony_ci *
78c2ecf20Sopenharmony_ci * Based heavily on the nftlcore.c code which is:
88c2ecf20Sopenharmony_ci * Copyright © 1999 Machine Vision Holdings, Inc.
98c2ecf20Sopenharmony_ci * Copyright © 1999 David Woodhouse <dwmw2@infradead.org>
108c2ecf20Sopenharmony_ci */
118c2ecf20Sopenharmony_ci
128c2ecf20Sopenharmony_ci#include <linux/kernel.h>
138c2ecf20Sopenharmony_ci#include <linux/module.h>
148c2ecf20Sopenharmony_ci#include <linux/delay.h>
158c2ecf20Sopenharmony_ci#include <linux/slab.h>
168c2ecf20Sopenharmony_ci#include <linux/sched.h>
178c2ecf20Sopenharmony_ci#include <linux/init.h>
188c2ecf20Sopenharmony_ci#include <linux/kmod.h>
198c2ecf20Sopenharmony_ci#include <linux/hdreg.h>
208c2ecf20Sopenharmony_ci#include <linux/mtd/mtd.h>
218c2ecf20Sopenharmony_ci#include <linux/mtd/nftl.h>
228c2ecf20Sopenharmony_ci#include <linux/mtd/inftl.h>
238c2ecf20Sopenharmony_ci#include <linux/mtd/rawnand.h>
248c2ecf20Sopenharmony_ci#include <linux/uaccess.h>
258c2ecf20Sopenharmony_ci#include <asm/errno.h>
268c2ecf20Sopenharmony_ci#include <asm/io.h>
278c2ecf20Sopenharmony_ci
288c2ecf20Sopenharmony_ci/*
298c2ecf20Sopenharmony_ci * Maximum number of loops while examining next block, to have a
308c2ecf20Sopenharmony_ci * chance to detect consistency problems (they should never happen
318c2ecf20Sopenharmony_ci * because of the checks done in the mounting.
328c2ecf20Sopenharmony_ci */
338c2ecf20Sopenharmony_ci#define MAX_LOOPS 10000
348c2ecf20Sopenharmony_ci
358c2ecf20Sopenharmony_cistatic void inftl_add_mtd(struct mtd_blktrans_ops *tr, struct mtd_info *mtd)
368c2ecf20Sopenharmony_ci{
378c2ecf20Sopenharmony_ci	struct INFTLrecord *inftl;
388c2ecf20Sopenharmony_ci	unsigned long temp;
398c2ecf20Sopenharmony_ci
408c2ecf20Sopenharmony_ci	if (!mtd_type_is_nand(mtd) || mtd->size > UINT_MAX)
418c2ecf20Sopenharmony_ci		return;
428c2ecf20Sopenharmony_ci	/* OK, this is moderately ugly.  But probably safe.  Alternatives? */
438c2ecf20Sopenharmony_ci	if (memcmp(mtd->name, "DiskOnChip", 10))
448c2ecf20Sopenharmony_ci		return;
458c2ecf20Sopenharmony_ci
468c2ecf20Sopenharmony_ci	if (!mtd->_block_isbad) {
478c2ecf20Sopenharmony_ci		printk(KERN_ERR
488c2ecf20Sopenharmony_ci"INFTL no longer supports the old DiskOnChip drivers loaded via docprobe.\n"
498c2ecf20Sopenharmony_ci"Please use the new diskonchip driver under the NAND subsystem.\n");
508c2ecf20Sopenharmony_ci		return;
518c2ecf20Sopenharmony_ci	}
528c2ecf20Sopenharmony_ci
538c2ecf20Sopenharmony_ci	pr_debug("INFTL: add_mtd for %s\n", mtd->name);
548c2ecf20Sopenharmony_ci
558c2ecf20Sopenharmony_ci	inftl = kzalloc(sizeof(*inftl), GFP_KERNEL);
568c2ecf20Sopenharmony_ci
578c2ecf20Sopenharmony_ci	if (!inftl)
588c2ecf20Sopenharmony_ci		return;
598c2ecf20Sopenharmony_ci
608c2ecf20Sopenharmony_ci	inftl->mbd.mtd = mtd;
618c2ecf20Sopenharmony_ci	inftl->mbd.devnum = -1;
628c2ecf20Sopenharmony_ci
638c2ecf20Sopenharmony_ci	inftl->mbd.tr = tr;
648c2ecf20Sopenharmony_ci
658c2ecf20Sopenharmony_ci	if (INFTL_mount(inftl) < 0) {
668c2ecf20Sopenharmony_ci		printk(KERN_WARNING "INFTL: could not mount device\n");
678c2ecf20Sopenharmony_ci		kfree(inftl);
688c2ecf20Sopenharmony_ci		return;
698c2ecf20Sopenharmony_ci	}
708c2ecf20Sopenharmony_ci
718c2ecf20Sopenharmony_ci	/* OK, it's a new one. Set up all the data structures. */
728c2ecf20Sopenharmony_ci
738c2ecf20Sopenharmony_ci	/* Calculate geometry */
748c2ecf20Sopenharmony_ci	inftl->cylinders = 1024;
758c2ecf20Sopenharmony_ci	inftl->heads = 16;
768c2ecf20Sopenharmony_ci
778c2ecf20Sopenharmony_ci	temp = inftl->cylinders * inftl->heads;
788c2ecf20Sopenharmony_ci	inftl->sectors = inftl->mbd.size / temp;
798c2ecf20Sopenharmony_ci	if (inftl->mbd.size % temp) {
808c2ecf20Sopenharmony_ci		inftl->sectors++;
818c2ecf20Sopenharmony_ci		temp = inftl->cylinders * inftl->sectors;
828c2ecf20Sopenharmony_ci		inftl->heads = inftl->mbd.size / temp;
838c2ecf20Sopenharmony_ci
848c2ecf20Sopenharmony_ci		if (inftl->mbd.size % temp) {
858c2ecf20Sopenharmony_ci			inftl->heads++;
868c2ecf20Sopenharmony_ci			temp = inftl->heads * inftl->sectors;
878c2ecf20Sopenharmony_ci			inftl->cylinders = inftl->mbd.size / temp;
888c2ecf20Sopenharmony_ci		}
898c2ecf20Sopenharmony_ci	}
908c2ecf20Sopenharmony_ci
918c2ecf20Sopenharmony_ci	if (inftl->mbd.size != inftl->heads * inftl->cylinders * inftl->sectors) {
928c2ecf20Sopenharmony_ci		/*
938c2ecf20Sopenharmony_ci		  Oh no we don't have
948c2ecf20Sopenharmony_ci		   mbd.size == heads * cylinders * sectors
958c2ecf20Sopenharmony_ci		*/
968c2ecf20Sopenharmony_ci		printk(KERN_WARNING "INFTL: cannot calculate a geometry to "
978c2ecf20Sopenharmony_ci		       "match size of 0x%lx.\n", inftl->mbd.size);
988c2ecf20Sopenharmony_ci		printk(KERN_WARNING "INFTL: using C:%d H:%d S:%d "
998c2ecf20Sopenharmony_ci			"(== 0x%lx sects)\n",
1008c2ecf20Sopenharmony_ci			inftl->cylinders, inftl->heads , inftl->sectors,
1018c2ecf20Sopenharmony_ci			(long)inftl->cylinders * (long)inftl->heads *
1028c2ecf20Sopenharmony_ci			(long)inftl->sectors );
1038c2ecf20Sopenharmony_ci	}
1048c2ecf20Sopenharmony_ci
1058c2ecf20Sopenharmony_ci	if (add_mtd_blktrans_dev(&inftl->mbd)) {
1068c2ecf20Sopenharmony_ci		kfree(inftl->PUtable);
1078c2ecf20Sopenharmony_ci		kfree(inftl->VUtable);
1088c2ecf20Sopenharmony_ci		kfree(inftl);
1098c2ecf20Sopenharmony_ci		return;
1108c2ecf20Sopenharmony_ci	}
1118c2ecf20Sopenharmony_ci#ifdef PSYCHO_DEBUG
1128c2ecf20Sopenharmony_ci	printk(KERN_INFO "INFTL: Found new inftl%c\n", inftl->mbd.devnum + 'a');
1138c2ecf20Sopenharmony_ci#endif
1148c2ecf20Sopenharmony_ci	return;
1158c2ecf20Sopenharmony_ci}
1168c2ecf20Sopenharmony_ci
1178c2ecf20Sopenharmony_cistatic void inftl_remove_dev(struct mtd_blktrans_dev *dev)
1188c2ecf20Sopenharmony_ci{
1198c2ecf20Sopenharmony_ci	struct INFTLrecord *inftl = (void *)dev;
1208c2ecf20Sopenharmony_ci
1218c2ecf20Sopenharmony_ci	pr_debug("INFTL: remove_dev (i=%d)\n", dev->devnum);
1228c2ecf20Sopenharmony_ci
1238c2ecf20Sopenharmony_ci	del_mtd_blktrans_dev(dev);
1248c2ecf20Sopenharmony_ci
1258c2ecf20Sopenharmony_ci	kfree(inftl->PUtable);
1268c2ecf20Sopenharmony_ci	kfree(inftl->VUtable);
1278c2ecf20Sopenharmony_ci}
1288c2ecf20Sopenharmony_ci
1298c2ecf20Sopenharmony_ci/*
1308c2ecf20Sopenharmony_ci * Actual INFTL access routines.
1318c2ecf20Sopenharmony_ci */
1328c2ecf20Sopenharmony_ci
1338c2ecf20Sopenharmony_ci/*
1348c2ecf20Sopenharmony_ci * Read oob data from flash
1358c2ecf20Sopenharmony_ci */
1368c2ecf20Sopenharmony_ciint inftl_read_oob(struct mtd_info *mtd, loff_t offs, size_t len,
1378c2ecf20Sopenharmony_ci		   size_t *retlen, uint8_t *buf)
1388c2ecf20Sopenharmony_ci{
1398c2ecf20Sopenharmony_ci	struct mtd_oob_ops ops;
1408c2ecf20Sopenharmony_ci	int res;
1418c2ecf20Sopenharmony_ci
1428c2ecf20Sopenharmony_ci	ops.mode = MTD_OPS_PLACE_OOB;
1438c2ecf20Sopenharmony_ci	ops.ooboffs = offs & (mtd->writesize - 1);
1448c2ecf20Sopenharmony_ci	ops.ooblen = len;
1458c2ecf20Sopenharmony_ci	ops.oobbuf = buf;
1468c2ecf20Sopenharmony_ci	ops.datbuf = NULL;
1478c2ecf20Sopenharmony_ci
1488c2ecf20Sopenharmony_ci	res = mtd_read_oob(mtd, offs & ~(mtd->writesize - 1), &ops);
1498c2ecf20Sopenharmony_ci	*retlen = ops.oobretlen;
1508c2ecf20Sopenharmony_ci	return res;
1518c2ecf20Sopenharmony_ci}
1528c2ecf20Sopenharmony_ci
1538c2ecf20Sopenharmony_ci/*
1548c2ecf20Sopenharmony_ci * Write oob data to flash
1558c2ecf20Sopenharmony_ci */
1568c2ecf20Sopenharmony_ciint inftl_write_oob(struct mtd_info *mtd, loff_t offs, size_t len,
1578c2ecf20Sopenharmony_ci		    size_t *retlen, uint8_t *buf)
1588c2ecf20Sopenharmony_ci{
1598c2ecf20Sopenharmony_ci	struct mtd_oob_ops ops;
1608c2ecf20Sopenharmony_ci	int res;
1618c2ecf20Sopenharmony_ci
1628c2ecf20Sopenharmony_ci	ops.mode = MTD_OPS_PLACE_OOB;
1638c2ecf20Sopenharmony_ci	ops.ooboffs = offs & (mtd->writesize - 1);
1648c2ecf20Sopenharmony_ci	ops.ooblen = len;
1658c2ecf20Sopenharmony_ci	ops.oobbuf = buf;
1668c2ecf20Sopenharmony_ci	ops.datbuf = NULL;
1678c2ecf20Sopenharmony_ci
1688c2ecf20Sopenharmony_ci	res = mtd_write_oob(mtd, offs & ~(mtd->writesize - 1), &ops);
1698c2ecf20Sopenharmony_ci	*retlen = ops.oobretlen;
1708c2ecf20Sopenharmony_ci	return res;
1718c2ecf20Sopenharmony_ci}
1728c2ecf20Sopenharmony_ci
1738c2ecf20Sopenharmony_ci/*
1748c2ecf20Sopenharmony_ci * Write data and oob to flash
1758c2ecf20Sopenharmony_ci */
1768c2ecf20Sopenharmony_cistatic int inftl_write(struct mtd_info *mtd, loff_t offs, size_t len,
1778c2ecf20Sopenharmony_ci		       size_t *retlen, uint8_t *buf, uint8_t *oob)
1788c2ecf20Sopenharmony_ci{
1798c2ecf20Sopenharmony_ci	struct mtd_oob_ops ops;
1808c2ecf20Sopenharmony_ci	int res;
1818c2ecf20Sopenharmony_ci
1828c2ecf20Sopenharmony_ci	ops.mode = MTD_OPS_PLACE_OOB;
1838c2ecf20Sopenharmony_ci	ops.ooboffs = offs;
1848c2ecf20Sopenharmony_ci	ops.ooblen = mtd->oobsize;
1858c2ecf20Sopenharmony_ci	ops.oobbuf = oob;
1868c2ecf20Sopenharmony_ci	ops.datbuf = buf;
1878c2ecf20Sopenharmony_ci	ops.len = len;
1888c2ecf20Sopenharmony_ci
1898c2ecf20Sopenharmony_ci	res = mtd_write_oob(mtd, offs & ~(mtd->writesize - 1), &ops);
1908c2ecf20Sopenharmony_ci	*retlen = ops.retlen;
1918c2ecf20Sopenharmony_ci	return res;
1928c2ecf20Sopenharmony_ci}
1938c2ecf20Sopenharmony_ci
1948c2ecf20Sopenharmony_ci/*
1958c2ecf20Sopenharmony_ci * INFTL_findfreeblock: Find a free Erase Unit on the INFTL partition.
1968c2ecf20Sopenharmony_ci *	This function is used when the give Virtual Unit Chain.
1978c2ecf20Sopenharmony_ci */
1988c2ecf20Sopenharmony_cistatic u16 INFTL_findfreeblock(struct INFTLrecord *inftl, int desperate)
1998c2ecf20Sopenharmony_ci{
2008c2ecf20Sopenharmony_ci	u16 pot = inftl->LastFreeEUN;
2018c2ecf20Sopenharmony_ci	int silly = inftl->nb_blocks;
2028c2ecf20Sopenharmony_ci
2038c2ecf20Sopenharmony_ci	pr_debug("INFTL: INFTL_findfreeblock(inftl=%p,desperate=%d)\n",
2048c2ecf20Sopenharmony_ci			inftl, desperate);
2058c2ecf20Sopenharmony_ci
2068c2ecf20Sopenharmony_ci	/*
2078c2ecf20Sopenharmony_ci	 * Normally, we force a fold to happen before we run out of free
2088c2ecf20Sopenharmony_ci	 * blocks completely.
2098c2ecf20Sopenharmony_ci	 */
2108c2ecf20Sopenharmony_ci	if (!desperate && inftl->numfreeEUNs < 2) {
2118c2ecf20Sopenharmony_ci		pr_debug("INFTL: there are too few free EUNs (%d)\n",
2128c2ecf20Sopenharmony_ci				inftl->numfreeEUNs);
2138c2ecf20Sopenharmony_ci		return BLOCK_NIL;
2148c2ecf20Sopenharmony_ci	}
2158c2ecf20Sopenharmony_ci
2168c2ecf20Sopenharmony_ci	/* Scan for a free block */
2178c2ecf20Sopenharmony_ci	do {
2188c2ecf20Sopenharmony_ci		if (inftl->PUtable[pot] == BLOCK_FREE) {
2198c2ecf20Sopenharmony_ci			inftl->LastFreeEUN = pot;
2208c2ecf20Sopenharmony_ci			return pot;
2218c2ecf20Sopenharmony_ci		}
2228c2ecf20Sopenharmony_ci
2238c2ecf20Sopenharmony_ci		if (++pot > inftl->lastEUN)
2248c2ecf20Sopenharmony_ci			pot = 0;
2258c2ecf20Sopenharmony_ci
2268c2ecf20Sopenharmony_ci		if (!silly--) {
2278c2ecf20Sopenharmony_ci			printk(KERN_WARNING "INFTL: no free blocks found!  "
2288c2ecf20Sopenharmony_ci				"EUN range = %d - %d\n", 0, inftl->LastFreeEUN);
2298c2ecf20Sopenharmony_ci			return BLOCK_NIL;
2308c2ecf20Sopenharmony_ci		}
2318c2ecf20Sopenharmony_ci	} while (pot != inftl->LastFreeEUN);
2328c2ecf20Sopenharmony_ci
2338c2ecf20Sopenharmony_ci	return BLOCK_NIL;
2348c2ecf20Sopenharmony_ci}
2358c2ecf20Sopenharmony_ci
2368c2ecf20Sopenharmony_cistatic u16 INFTL_foldchain(struct INFTLrecord *inftl, unsigned thisVUC, unsigned pendingblock)
2378c2ecf20Sopenharmony_ci{
2388c2ecf20Sopenharmony_ci	u16 BlockMap[MAX_SECTORS_PER_UNIT];
2398c2ecf20Sopenharmony_ci	unsigned char BlockDeleted[MAX_SECTORS_PER_UNIT];
2408c2ecf20Sopenharmony_ci	unsigned int thisEUN, prevEUN, status;
2418c2ecf20Sopenharmony_ci	struct mtd_info *mtd = inftl->mbd.mtd;
2428c2ecf20Sopenharmony_ci	int block, silly;
2438c2ecf20Sopenharmony_ci	unsigned int targetEUN;
2448c2ecf20Sopenharmony_ci	struct inftl_oob oob;
2458c2ecf20Sopenharmony_ci	size_t retlen;
2468c2ecf20Sopenharmony_ci
2478c2ecf20Sopenharmony_ci	pr_debug("INFTL: INFTL_foldchain(inftl=%p,thisVUC=%d,pending=%d)\n",
2488c2ecf20Sopenharmony_ci			inftl, thisVUC, pendingblock);
2498c2ecf20Sopenharmony_ci
2508c2ecf20Sopenharmony_ci	memset(BlockMap, 0xff, sizeof(BlockMap));
2518c2ecf20Sopenharmony_ci	memset(BlockDeleted, 0, sizeof(BlockDeleted));
2528c2ecf20Sopenharmony_ci
2538c2ecf20Sopenharmony_ci	thisEUN = targetEUN = inftl->VUtable[thisVUC];
2548c2ecf20Sopenharmony_ci
2558c2ecf20Sopenharmony_ci	if (thisEUN == BLOCK_NIL) {
2568c2ecf20Sopenharmony_ci		printk(KERN_WARNING "INFTL: trying to fold non-existent "
2578c2ecf20Sopenharmony_ci		       "Virtual Unit Chain %d!\n", thisVUC);
2588c2ecf20Sopenharmony_ci		return BLOCK_NIL;
2598c2ecf20Sopenharmony_ci	}
2608c2ecf20Sopenharmony_ci
2618c2ecf20Sopenharmony_ci	/*
2628c2ecf20Sopenharmony_ci	 * Scan to find the Erase Unit which holds the actual data for each
2638c2ecf20Sopenharmony_ci	 * 512-byte block within the Chain.
2648c2ecf20Sopenharmony_ci	 */
2658c2ecf20Sopenharmony_ci	silly = MAX_LOOPS;
2668c2ecf20Sopenharmony_ci	while (thisEUN < inftl->nb_blocks) {
2678c2ecf20Sopenharmony_ci		for (block = 0; block < inftl->EraseSize/SECTORSIZE; block ++) {
2688c2ecf20Sopenharmony_ci			if ((BlockMap[block] != BLOCK_NIL) ||
2698c2ecf20Sopenharmony_ci			    BlockDeleted[block])
2708c2ecf20Sopenharmony_ci				continue;
2718c2ecf20Sopenharmony_ci
2728c2ecf20Sopenharmony_ci			if (inftl_read_oob(mtd, (thisEUN * inftl->EraseSize)
2738c2ecf20Sopenharmony_ci					   + (block * SECTORSIZE), 16, &retlen,
2748c2ecf20Sopenharmony_ci					   (char *)&oob) < 0)
2758c2ecf20Sopenharmony_ci				status = SECTOR_IGNORE;
2768c2ecf20Sopenharmony_ci			else
2778c2ecf20Sopenharmony_ci				status = oob.b.Status | oob.b.Status1;
2788c2ecf20Sopenharmony_ci
2798c2ecf20Sopenharmony_ci			switch(status) {
2808c2ecf20Sopenharmony_ci			case SECTOR_FREE:
2818c2ecf20Sopenharmony_ci			case SECTOR_IGNORE:
2828c2ecf20Sopenharmony_ci				break;
2838c2ecf20Sopenharmony_ci			case SECTOR_USED:
2848c2ecf20Sopenharmony_ci				BlockMap[block] = thisEUN;
2858c2ecf20Sopenharmony_ci				continue;
2868c2ecf20Sopenharmony_ci			case SECTOR_DELETED:
2878c2ecf20Sopenharmony_ci				BlockDeleted[block] = 1;
2888c2ecf20Sopenharmony_ci				continue;
2898c2ecf20Sopenharmony_ci			default:
2908c2ecf20Sopenharmony_ci				printk(KERN_WARNING "INFTL: unknown status "
2918c2ecf20Sopenharmony_ci					"for block %d in EUN %d: %x\n",
2928c2ecf20Sopenharmony_ci					block, thisEUN, status);
2938c2ecf20Sopenharmony_ci				break;
2948c2ecf20Sopenharmony_ci			}
2958c2ecf20Sopenharmony_ci		}
2968c2ecf20Sopenharmony_ci
2978c2ecf20Sopenharmony_ci		if (!silly--) {
2988c2ecf20Sopenharmony_ci			printk(KERN_WARNING "INFTL: infinite loop in Virtual "
2998c2ecf20Sopenharmony_ci				"Unit Chain 0x%x\n", thisVUC);
3008c2ecf20Sopenharmony_ci			return BLOCK_NIL;
3018c2ecf20Sopenharmony_ci		}
3028c2ecf20Sopenharmony_ci
3038c2ecf20Sopenharmony_ci		thisEUN = inftl->PUtable[thisEUN];
3048c2ecf20Sopenharmony_ci	}
3058c2ecf20Sopenharmony_ci
3068c2ecf20Sopenharmony_ci	/*
3078c2ecf20Sopenharmony_ci	 * OK. We now know the location of every block in the Virtual Unit
3088c2ecf20Sopenharmony_ci	 * Chain, and the Erase Unit into which we are supposed to be copying.
3098c2ecf20Sopenharmony_ci	 * Go for it.
3108c2ecf20Sopenharmony_ci	 */
3118c2ecf20Sopenharmony_ci	pr_debug("INFTL: folding chain %d into unit %d\n", thisVUC, targetEUN);
3128c2ecf20Sopenharmony_ci
3138c2ecf20Sopenharmony_ci	for (block = 0; block < inftl->EraseSize/SECTORSIZE ; block++) {
3148c2ecf20Sopenharmony_ci		unsigned char movebuf[SECTORSIZE];
3158c2ecf20Sopenharmony_ci		int ret;
3168c2ecf20Sopenharmony_ci
3178c2ecf20Sopenharmony_ci		/*
3188c2ecf20Sopenharmony_ci		 * If it's in the target EUN already, or if it's pending write,
3198c2ecf20Sopenharmony_ci		 * do nothing.
3208c2ecf20Sopenharmony_ci		 */
3218c2ecf20Sopenharmony_ci		if (BlockMap[block] == targetEUN || (pendingblock ==
3228c2ecf20Sopenharmony_ci		    (thisVUC * (inftl->EraseSize / SECTORSIZE) + block))) {
3238c2ecf20Sopenharmony_ci			continue;
3248c2ecf20Sopenharmony_ci		}
3258c2ecf20Sopenharmony_ci
3268c2ecf20Sopenharmony_ci		/*
3278c2ecf20Sopenharmony_ci		 * Copy only in non free block (free blocks can only
3288c2ecf20Sopenharmony_ci                 * happen in case of media errors or deleted blocks).
3298c2ecf20Sopenharmony_ci		 */
3308c2ecf20Sopenharmony_ci		if (BlockMap[block] == BLOCK_NIL)
3318c2ecf20Sopenharmony_ci			continue;
3328c2ecf20Sopenharmony_ci
3338c2ecf20Sopenharmony_ci		ret = mtd_read(mtd,
3348c2ecf20Sopenharmony_ci			       (inftl->EraseSize * BlockMap[block]) + (block * SECTORSIZE),
3358c2ecf20Sopenharmony_ci			       SECTORSIZE,
3368c2ecf20Sopenharmony_ci			       &retlen,
3378c2ecf20Sopenharmony_ci			       movebuf);
3388c2ecf20Sopenharmony_ci		if (ret < 0 && !mtd_is_bitflip(ret)) {
3398c2ecf20Sopenharmony_ci			ret = mtd_read(mtd,
3408c2ecf20Sopenharmony_ci				       (inftl->EraseSize * BlockMap[block]) + (block * SECTORSIZE),
3418c2ecf20Sopenharmony_ci				       SECTORSIZE,
3428c2ecf20Sopenharmony_ci				       &retlen,
3438c2ecf20Sopenharmony_ci				       movebuf);
3448c2ecf20Sopenharmony_ci			if (ret != -EIO)
3458c2ecf20Sopenharmony_ci				pr_debug("INFTL: error went away on retry?\n");
3468c2ecf20Sopenharmony_ci		}
3478c2ecf20Sopenharmony_ci		memset(&oob, 0xff, sizeof(struct inftl_oob));
3488c2ecf20Sopenharmony_ci		oob.b.Status = oob.b.Status1 = SECTOR_USED;
3498c2ecf20Sopenharmony_ci
3508c2ecf20Sopenharmony_ci		inftl_write(inftl->mbd.mtd, (inftl->EraseSize * targetEUN) +
3518c2ecf20Sopenharmony_ci			    (block * SECTORSIZE), SECTORSIZE, &retlen,
3528c2ecf20Sopenharmony_ci			    movebuf, (char *)&oob);
3538c2ecf20Sopenharmony_ci	}
3548c2ecf20Sopenharmony_ci
3558c2ecf20Sopenharmony_ci	/*
3568c2ecf20Sopenharmony_ci	 * Newest unit in chain now contains data from _all_ older units.
3578c2ecf20Sopenharmony_ci	 * So go through and erase each unit in chain, oldest first. (This
3588c2ecf20Sopenharmony_ci	 * is important, by doing oldest first if we crash/reboot then it
3598c2ecf20Sopenharmony_ci	 * it is relatively simple to clean up the mess).
3608c2ecf20Sopenharmony_ci	 */
3618c2ecf20Sopenharmony_ci	pr_debug("INFTL: want to erase virtual chain %d\n", thisVUC);
3628c2ecf20Sopenharmony_ci
3638c2ecf20Sopenharmony_ci	for (;;) {
3648c2ecf20Sopenharmony_ci		/* Find oldest unit in chain. */
3658c2ecf20Sopenharmony_ci		thisEUN = inftl->VUtable[thisVUC];
3668c2ecf20Sopenharmony_ci		prevEUN = BLOCK_NIL;
3678c2ecf20Sopenharmony_ci		while (inftl->PUtable[thisEUN] != BLOCK_NIL) {
3688c2ecf20Sopenharmony_ci			prevEUN = thisEUN;
3698c2ecf20Sopenharmony_ci			thisEUN = inftl->PUtable[thisEUN];
3708c2ecf20Sopenharmony_ci		}
3718c2ecf20Sopenharmony_ci
3728c2ecf20Sopenharmony_ci		/* Check if we are all done */
3738c2ecf20Sopenharmony_ci		if (thisEUN == targetEUN)
3748c2ecf20Sopenharmony_ci			break;
3758c2ecf20Sopenharmony_ci
3768c2ecf20Sopenharmony_ci		/* Unlink the last block from the chain. */
3778c2ecf20Sopenharmony_ci		inftl->PUtable[prevEUN] = BLOCK_NIL;
3788c2ecf20Sopenharmony_ci
3798c2ecf20Sopenharmony_ci		/* Now try to erase it. */
3808c2ecf20Sopenharmony_ci		if (INFTL_formatblock(inftl, thisEUN) < 0) {
3818c2ecf20Sopenharmony_ci			/*
3828c2ecf20Sopenharmony_ci			 * Could not erase : mark block as reserved.
3838c2ecf20Sopenharmony_ci			 */
3848c2ecf20Sopenharmony_ci			inftl->PUtable[thisEUN] = BLOCK_RESERVED;
3858c2ecf20Sopenharmony_ci		} else {
3868c2ecf20Sopenharmony_ci			/* Correctly erased : mark it as free */
3878c2ecf20Sopenharmony_ci			inftl->PUtable[thisEUN] = BLOCK_FREE;
3888c2ecf20Sopenharmony_ci			inftl->numfreeEUNs++;
3898c2ecf20Sopenharmony_ci		}
3908c2ecf20Sopenharmony_ci	}
3918c2ecf20Sopenharmony_ci
3928c2ecf20Sopenharmony_ci	return targetEUN;
3938c2ecf20Sopenharmony_ci}
3948c2ecf20Sopenharmony_ci
3958c2ecf20Sopenharmony_cistatic u16 INFTL_makefreeblock(struct INFTLrecord *inftl, unsigned pendingblock)
3968c2ecf20Sopenharmony_ci{
3978c2ecf20Sopenharmony_ci	/*
3988c2ecf20Sopenharmony_ci	 * This is the part that needs some cleverness applied.
3998c2ecf20Sopenharmony_ci	 * For now, I'm doing the minimum applicable to actually
4008c2ecf20Sopenharmony_ci	 * get the thing to work.
4018c2ecf20Sopenharmony_ci	 * Wear-levelling and other clever stuff needs to be implemented
4028c2ecf20Sopenharmony_ci	 * and we also need to do some assessment of the results when
4038c2ecf20Sopenharmony_ci	 * the system loses power half-way through the routine.
4048c2ecf20Sopenharmony_ci	 */
4058c2ecf20Sopenharmony_ci	u16 LongestChain = 0;
4068c2ecf20Sopenharmony_ci	u16 ChainLength = 0, thislen;
4078c2ecf20Sopenharmony_ci	u16 chain, EUN;
4088c2ecf20Sopenharmony_ci
4098c2ecf20Sopenharmony_ci	pr_debug("INFTL: INFTL_makefreeblock(inftl=%p,"
4108c2ecf20Sopenharmony_ci		"pending=%d)\n", inftl, pendingblock);
4118c2ecf20Sopenharmony_ci
4128c2ecf20Sopenharmony_ci	for (chain = 0; chain < inftl->nb_blocks; chain++) {
4138c2ecf20Sopenharmony_ci		EUN = inftl->VUtable[chain];
4148c2ecf20Sopenharmony_ci		thislen = 0;
4158c2ecf20Sopenharmony_ci
4168c2ecf20Sopenharmony_ci		while (EUN <= inftl->lastEUN) {
4178c2ecf20Sopenharmony_ci			thislen++;
4188c2ecf20Sopenharmony_ci			EUN = inftl->PUtable[EUN];
4198c2ecf20Sopenharmony_ci			if (thislen > 0xff00) {
4208c2ecf20Sopenharmony_ci				printk(KERN_WARNING "INFTL: endless loop in "
4218c2ecf20Sopenharmony_ci					"Virtual Chain %d: Unit %x\n",
4228c2ecf20Sopenharmony_ci					chain, EUN);
4238c2ecf20Sopenharmony_ci				/*
4248c2ecf20Sopenharmony_ci				 * Actually, don't return failure.
4258c2ecf20Sopenharmony_ci				 * Just ignore this chain and get on with it.
4268c2ecf20Sopenharmony_ci				 */
4278c2ecf20Sopenharmony_ci				thislen = 0;
4288c2ecf20Sopenharmony_ci				break;
4298c2ecf20Sopenharmony_ci			}
4308c2ecf20Sopenharmony_ci		}
4318c2ecf20Sopenharmony_ci
4328c2ecf20Sopenharmony_ci		if (thislen > ChainLength) {
4338c2ecf20Sopenharmony_ci			ChainLength = thislen;
4348c2ecf20Sopenharmony_ci			LongestChain = chain;
4358c2ecf20Sopenharmony_ci		}
4368c2ecf20Sopenharmony_ci	}
4378c2ecf20Sopenharmony_ci
4388c2ecf20Sopenharmony_ci	if (ChainLength < 2) {
4398c2ecf20Sopenharmony_ci		printk(KERN_WARNING "INFTL: no Virtual Unit Chains available "
4408c2ecf20Sopenharmony_ci			"for folding. Failing request\n");
4418c2ecf20Sopenharmony_ci		return BLOCK_NIL;
4428c2ecf20Sopenharmony_ci	}
4438c2ecf20Sopenharmony_ci
4448c2ecf20Sopenharmony_ci	return INFTL_foldchain(inftl, LongestChain, pendingblock);
4458c2ecf20Sopenharmony_ci}
4468c2ecf20Sopenharmony_ci
4478c2ecf20Sopenharmony_cistatic int nrbits(unsigned int val, int bitcount)
4488c2ecf20Sopenharmony_ci{
4498c2ecf20Sopenharmony_ci	int i, total = 0;
4508c2ecf20Sopenharmony_ci
4518c2ecf20Sopenharmony_ci	for (i = 0; (i < bitcount); i++)
4528c2ecf20Sopenharmony_ci		total += (((0x1 << i) & val) ? 1 : 0);
4538c2ecf20Sopenharmony_ci	return total;
4548c2ecf20Sopenharmony_ci}
4558c2ecf20Sopenharmony_ci
4568c2ecf20Sopenharmony_ci/*
4578c2ecf20Sopenharmony_ci * INFTL_findwriteunit: Return the unit number into which we can write
4588c2ecf20Sopenharmony_ci *                      for this block. Make it available if it isn't already.
4598c2ecf20Sopenharmony_ci */
4608c2ecf20Sopenharmony_cistatic inline u16 INFTL_findwriteunit(struct INFTLrecord *inftl, unsigned block)
4618c2ecf20Sopenharmony_ci{
4628c2ecf20Sopenharmony_ci	unsigned int thisVUC = block / (inftl->EraseSize / SECTORSIZE);
4638c2ecf20Sopenharmony_ci	unsigned int thisEUN, writeEUN, prev_block, status;
4648c2ecf20Sopenharmony_ci	unsigned long blockofs = (block * SECTORSIZE) & (inftl->EraseSize -1);
4658c2ecf20Sopenharmony_ci	struct mtd_info *mtd = inftl->mbd.mtd;
4668c2ecf20Sopenharmony_ci	struct inftl_oob oob;
4678c2ecf20Sopenharmony_ci	struct inftl_bci bci;
4688c2ecf20Sopenharmony_ci	unsigned char anac, nacs, parity;
4698c2ecf20Sopenharmony_ci	size_t retlen;
4708c2ecf20Sopenharmony_ci	int silly, silly2 = 3;
4718c2ecf20Sopenharmony_ci
4728c2ecf20Sopenharmony_ci	pr_debug("INFTL: INFTL_findwriteunit(inftl=%p,block=%d)\n",
4738c2ecf20Sopenharmony_ci			inftl, block);
4748c2ecf20Sopenharmony_ci
4758c2ecf20Sopenharmony_ci	do {
4768c2ecf20Sopenharmony_ci		/*
4778c2ecf20Sopenharmony_ci		 * Scan the media to find a unit in the VUC which has
4788c2ecf20Sopenharmony_ci		 * a free space for the block in question.
4798c2ecf20Sopenharmony_ci		 */
4808c2ecf20Sopenharmony_ci		writeEUN = BLOCK_NIL;
4818c2ecf20Sopenharmony_ci		thisEUN = inftl->VUtable[thisVUC];
4828c2ecf20Sopenharmony_ci		silly = MAX_LOOPS;
4838c2ecf20Sopenharmony_ci
4848c2ecf20Sopenharmony_ci		while (thisEUN <= inftl->lastEUN) {
4858c2ecf20Sopenharmony_ci			inftl_read_oob(mtd, (thisEUN * inftl->EraseSize) +
4868c2ecf20Sopenharmony_ci				       blockofs, 8, &retlen, (char *)&bci);
4878c2ecf20Sopenharmony_ci
4888c2ecf20Sopenharmony_ci			status = bci.Status | bci.Status1;
4898c2ecf20Sopenharmony_ci			pr_debug("INFTL: status of block %d in EUN %d is %x\n",
4908c2ecf20Sopenharmony_ci					block , writeEUN, status);
4918c2ecf20Sopenharmony_ci
4928c2ecf20Sopenharmony_ci			switch(status) {
4938c2ecf20Sopenharmony_ci			case SECTOR_FREE:
4948c2ecf20Sopenharmony_ci				writeEUN = thisEUN;
4958c2ecf20Sopenharmony_ci				break;
4968c2ecf20Sopenharmony_ci			case SECTOR_DELETED:
4978c2ecf20Sopenharmony_ci			case SECTOR_USED:
4988c2ecf20Sopenharmony_ci				/* Can't go any further */
4998c2ecf20Sopenharmony_ci				goto hitused;
5008c2ecf20Sopenharmony_ci			case SECTOR_IGNORE:
5018c2ecf20Sopenharmony_ci				break;
5028c2ecf20Sopenharmony_ci			default:
5038c2ecf20Sopenharmony_ci				/*
5048c2ecf20Sopenharmony_ci				 * Invalid block. Don't use it any more.
5058c2ecf20Sopenharmony_ci				 * Must implement.
5068c2ecf20Sopenharmony_ci				 */
5078c2ecf20Sopenharmony_ci				break;
5088c2ecf20Sopenharmony_ci			}
5098c2ecf20Sopenharmony_ci
5108c2ecf20Sopenharmony_ci			if (!silly--) {
5118c2ecf20Sopenharmony_ci				printk(KERN_WARNING "INFTL: infinite loop in "
5128c2ecf20Sopenharmony_ci					"Virtual Unit Chain 0x%x\n", thisVUC);
5138c2ecf20Sopenharmony_ci				return BLOCK_NIL;
5148c2ecf20Sopenharmony_ci			}
5158c2ecf20Sopenharmony_ci
5168c2ecf20Sopenharmony_ci			/* Skip to next block in chain */
5178c2ecf20Sopenharmony_ci			thisEUN = inftl->PUtable[thisEUN];
5188c2ecf20Sopenharmony_ci		}
5198c2ecf20Sopenharmony_ci
5208c2ecf20Sopenharmony_cihitused:
5218c2ecf20Sopenharmony_ci		if (writeEUN != BLOCK_NIL)
5228c2ecf20Sopenharmony_ci			return writeEUN;
5238c2ecf20Sopenharmony_ci
5248c2ecf20Sopenharmony_ci
5258c2ecf20Sopenharmony_ci		/*
5268c2ecf20Sopenharmony_ci		 * OK. We didn't find one in the existing chain, or there
5278c2ecf20Sopenharmony_ci		 * is no existing chain. Allocate a new one.
5288c2ecf20Sopenharmony_ci		 */
5298c2ecf20Sopenharmony_ci		writeEUN = INFTL_findfreeblock(inftl, 0);
5308c2ecf20Sopenharmony_ci
5318c2ecf20Sopenharmony_ci		if (writeEUN == BLOCK_NIL) {
5328c2ecf20Sopenharmony_ci			/*
5338c2ecf20Sopenharmony_ci			 * That didn't work - there were no free blocks just
5348c2ecf20Sopenharmony_ci			 * waiting to be picked up. We're going to have to fold
5358c2ecf20Sopenharmony_ci			 * a chain to make room.
5368c2ecf20Sopenharmony_ci			 */
5378c2ecf20Sopenharmony_ci			thisEUN = INFTL_makefreeblock(inftl, block);
5388c2ecf20Sopenharmony_ci
5398c2ecf20Sopenharmony_ci			/*
5408c2ecf20Sopenharmony_ci			 * Hopefully we free something, lets try again.
5418c2ecf20Sopenharmony_ci			 * This time we are desperate...
5428c2ecf20Sopenharmony_ci			 */
5438c2ecf20Sopenharmony_ci			pr_debug("INFTL: using desperate==1 to find free EUN "
5448c2ecf20Sopenharmony_ci					"to accommodate write to VUC %d\n",
5458c2ecf20Sopenharmony_ci					thisVUC);
5468c2ecf20Sopenharmony_ci			writeEUN = INFTL_findfreeblock(inftl, 1);
5478c2ecf20Sopenharmony_ci			if (writeEUN == BLOCK_NIL) {
5488c2ecf20Sopenharmony_ci				/*
5498c2ecf20Sopenharmony_ci				 * Ouch. This should never happen - we should
5508c2ecf20Sopenharmony_ci				 * always be able to make some room somehow.
5518c2ecf20Sopenharmony_ci				 * If we get here, we've allocated more storage
5528c2ecf20Sopenharmony_ci				 * space than actual media, or our makefreeblock
5538c2ecf20Sopenharmony_ci				 * routine is missing something.
5548c2ecf20Sopenharmony_ci				 */
5558c2ecf20Sopenharmony_ci				printk(KERN_WARNING "INFTL: cannot make free "
5568c2ecf20Sopenharmony_ci					"space.\n");
5578c2ecf20Sopenharmony_ci#ifdef DEBUG
5588c2ecf20Sopenharmony_ci				INFTL_dumptables(inftl);
5598c2ecf20Sopenharmony_ci				INFTL_dumpVUchains(inftl);
5608c2ecf20Sopenharmony_ci#endif
5618c2ecf20Sopenharmony_ci				return BLOCK_NIL;
5628c2ecf20Sopenharmony_ci			}
5638c2ecf20Sopenharmony_ci		}
5648c2ecf20Sopenharmony_ci
5658c2ecf20Sopenharmony_ci		/*
5668c2ecf20Sopenharmony_ci		 * Insert new block into virtual chain. Firstly update the
5678c2ecf20Sopenharmony_ci		 * block headers in flash...
5688c2ecf20Sopenharmony_ci		 */
5698c2ecf20Sopenharmony_ci		anac = 0;
5708c2ecf20Sopenharmony_ci		nacs = 0;
5718c2ecf20Sopenharmony_ci		thisEUN = inftl->VUtable[thisVUC];
5728c2ecf20Sopenharmony_ci		if (thisEUN != BLOCK_NIL) {
5738c2ecf20Sopenharmony_ci			inftl_read_oob(mtd, thisEUN * inftl->EraseSize
5748c2ecf20Sopenharmony_ci				       + 8, 8, &retlen, (char *)&oob.u);
5758c2ecf20Sopenharmony_ci			anac = oob.u.a.ANAC + 1;
5768c2ecf20Sopenharmony_ci			nacs = oob.u.a.NACs + 1;
5778c2ecf20Sopenharmony_ci		}
5788c2ecf20Sopenharmony_ci
5798c2ecf20Sopenharmony_ci		prev_block = inftl->VUtable[thisVUC];
5808c2ecf20Sopenharmony_ci		if (prev_block < inftl->nb_blocks)
5818c2ecf20Sopenharmony_ci			prev_block -= inftl->firstEUN;
5828c2ecf20Sopenharmony_ci
5838c2ecf20Sopenharmony_ci		parity = (nrbits(thisVUC, 16) & 0x1) ? 0x1 : 0;
5848c2ecf20Sopenharmony_ci		parity |= (nrbits(prev_block, 16) & 0x1) ? 0x2 : 0;
5858c2ecf20Sopenharmony_ci		parity |= (nrbits(anac, 8) & 0x1) ? 0x4 : 0;
5868c2ecf20Sopenharmony_ci		parity |= (nrbits(nacs, 8) & 0x1) ? 0x8 : 0;
5878c2ecf20Sopenharmony_ci
5888c2ecf20Sopenharmony_ci		oob.u.a.virtualUnitNo = cpu_to_le16(thisVUC);
5898c2ecf20Sopenharmony_ci		oob.u.a.prevUnitNo = cpu_to_le16(prev_block);
5908c2ecf20Sopenharmony_ci		oob.u.a.ANAC = anac;
5918c2ecf20Sopenharmony_ci		oob.u.a.NACs = nacs;
5928c2ecf20Sopenharmony_ci		oob.u.a.parityPerField = parity;
5938c2ecf20Sopenharmony_ci		oob.u.a.discarded = 0xaa;
5948c2ecf20Sopenharmony_ci
5958c2ecf20Sopenharmony_ci		inftl_write_oob(mtd, writeEUN * inftl->EraseSize + 8, 8,
5968c2ecf20Sopenharmony_ci				&retlen, (char *)&oob.u);
5978c2ecf20Sopenharmony_ci
5988c2ecf20Sopenharmony_ci		/* Also back up header... */
5998c2ecf20Sopenharmony_ci		oob.u.b.virtualUnitNo = cpu_to_le16(thisVUC);
6008c2ecf20Sopenharmony_ci		oob.u.b.prevUnitNo = cpu_to_le16(prev_block);
6018c2ecf20Sopenharmony_ci		oob.u.b.ANAC = anac;
6028c2ecf20Sopenharmony_ci		oob.u.b.NACs = nacs;
6038c2ecf20Sopenharmony_ci		oob.u.b.parityPerField = parity;
6048c2ecf20Sopenharmony_ci		oob.u.b.discarded = 0xaa;
6058c2ecf20Sopenharmony_ci
6068c2ecf20Sopenharmony_ci		inftl_write_oob(mtd, writeEUN * inftl->EraseSize +
6078c2ecf20Sopenharmony_ci				SECTORSIZE * 4 + 8, 8, &retlen, (char *)&oob.u);
6088c2ecf20Sopenharmony_ci
6098c2ecf20Sopenharmony_ci		inftl->PUtable[writeEUN] = inftl->VUtable[thisVUC];
6108c2ecf20Sopenharmony_ci		inftl->VUtable[thisVUC] = writeEUN;
6118c2ecf20Sopenharmony_ci
6128c2ecf20Sopenharmony_ci		inftl->numfreeEUNs--;
6138c2ecf20Sopenharmony_ci		return writeEUN;
6148c2ecf20Sopenharmony_ci
6158c2ecf20Sopenharmony_ci	} while (silly2--);
6168c2ecf20Sopenharmony_ci
6178c2ecf20Sopenharmony_ci	printk(KERN_WARNING "INFTL: error folding to make room for Virtual "
6188c2ecf20Sopenharmony_ci		"Unit Chain 0x%x\n", thisVUC);
6198c2ecf20Sopenharmony_ci	return BLOCK_NIL;
6208c2ecf20Sopenharmony_ci}
6218c2ecf20Sopenharmony_ci
6228c2ecf20Sopenharmony_ci/*
6238c2ecf20Sopenharmony_ci * Given a Virtual Unit Chain, see if it can be deleted, and if so do it.
6248c2ecf20Sopenharmony_ci */
6258c2ecf20Sopenharmony_cistatic void INFTL_trydeletechain(struct INFTLrecord *inftl, unsigned thisVUC)
6268c2ecf20Sopenharmony_ci{
6278c2ecf20Sopenharmony_ci	struct mtd_info *mtd = inftl->mbd.mtd;
6288c2ecf20Sopenharmony_ci	unsigned char BlockUsed[MAX_SECTORS_PER_UNIT];
6298c2ecf20Sopenharmony_ci	unsigned char BlockDeleted[MAX_SECTORS_PER_UNIT];
6308c2ecf20Sopenharmony_ci	unsigned int thisEUN, status;
6318c2ecf20Sopenharmony_ci	int block, silly;
6328c2ecf20Sopenharmony_ci	struct inftl_bci bci;
6338c2ecf20Sopenharmony_ci	size_t retlen;
6348c2ecf20Sopenharmony_ci
6358c2ecf20Sopenharmony_ci	pr_debug("INFTL: INFTL_trydeletechain(inftl=%p,"
6368c2ecf20Sopenharmony_ci		"thisVUC=%d)\n", inftl, thisVUC);
6378c2ecf20Sopenharmony_ci
6388c2ecf20Sopenharmony_ci	memset(BlockUsed, 0, sizeof(BlockUsed));
6398c2ecf20Sopenharmony_ci	memset(BlockDeleted, 0, sizeof(BlockDeleted));
6408c2ecf20Sopenharmony_ci
6418c2ecf20Sopenharmony_ci	thisEUN = inftl->VUtable[thisVUC];
6428c2ecf20Sopenharmony_ci	if (thisEUN == BLOCK_NIL) {
6438c2ecf20Sopenharmony_ci		printk(KERN_WARNING "INFTL: trying to delete non-existent "
6448c2ecf20Sopenharmony_ci		       "Virtual Unit Chain %d!\n", thisVUC);
6458c2ecf20Sopenharmony_ci		return;
6468c2ecf20Sopenharmony_ci	}
6478c2ecf20Sopenharmony_ci
6488c2ecf20Sopenharmony_ci	/*
6498c2ecf20Sopenharmony_ci	 * Scan through the Erase Units to determine whether any data is in
6508c2ecf20Sopenharmony_ci	 * each of the 512-byte blocks within the Chain.
6518c2ecf20Sopenharmony_ci	 */
6528c2ecf20Sopenharmony_ci	silly = MAX_LOOPS;
6538c2ecf20Sopenharmony_ci	while (thisEUN < inftl->nb_blocks) {
6548c2ecf20Sopenharmony_ci		for (block = 0; block < inftl->EraseSize/SECTORSIZE; block++) {
6558c2ecf20Sopenharmony_ci			if (BlockUsed[block] || BlockDeleted[block])
6568c2ecf20Sopenharmony_ci				continue;
6578c2ecf20Sopenharmony_ci
6588c2ecf20Sopenharmony_ci			if (inftl_read_oob(mtd, (thisEUN * inftl->EraseSize)
6598c2ecf20Sopenharmony_ci					   + (block * SECTORSIZE), 8 , &retlen,
6608c2ecf20Sopenharmony_ci					  (char *)&bci) < 0)
6618c2ecf20Sopenharmony_ci				status = SECTOR_IGNORE;
6628c2ecf20Sopenharmony_ci			else
6638c2ecf20Sopenharmony_ci				status = bci.Status | bci.Status1;
6648c2ecf20Sopenharmony_ci
6658c2ecf20Sopenharmony_ci			switch(status) {
6668c2ecf20Sopenharmony_ci			case SECTOR_FREE:
6678c2ecf20Sopenharmony_ci			case SECTOR_IGNORE:
6688c2ecf20Sopenharmony_ci				break;
6698c2ecf20Sopenharmony_ci			case SECTOR_USED:
6708c2ecf20Sopenharmony_ci				BlockUsed[block] = 1;
6718c2ecf20Sopenharmony_ci				continue;
6728c2ecf20Sopenharmony_ci			case SECTOR_DELETED:
6738c2ecf20Sopenharmony_ci				BlockDeleted[block] = 1;
6748c2ecf20Sopenharmony_ci				continue;
6758c2ecf20Sopenharmony_ci			default:
6768c2ecf20Sopenharmony_ci				printk(KERN_WARNING "INFTL: unknown status "
6778c2ecf20Sopenharmony_ci					"for block %d in EUN %d: 0x%x\n",
6788c2ecf20Sopenharmony_ci					block, thisEUN, status);
6798c2ecf20Sopenharmony_ci			}
6808c2ecf20Sopenharmony_ci		}
6818c2ecf20Sopenharmony_ci
6828c2ecf20Sopenharmony_ci		if (!silly--) {
6838c2ecf20Sopenharmony_ci			printk(KERN_WARNING "INFTL: infinite loop in Virtual "
6848c2ecf20Sopenharmony_ci				"Unit Chain 0x%x\n", thisVUC);
6858c2ecf20Sopenharmony_ci			return;
6868c2ecf20Sopenharmony_ci		}
6878c2ecf20Sopenharmony_ci
6888c2ecf20Sopenharmony_ci		thisEUN = inftl->PUtable[thisEUN];
6898c2ecf20Sopenharmony_ci	}
6908c2ecf20Sopenharmony_ci
6918c2ecf20Sopenharmony_ci	for (block = 0; block < inftl->EraseSize/SECTORSIZE; block++)
6928c2ecf20Sopenharmony_ci		if (BlockUsed[block])
6938c2ecf20Sopenharmony_ci			return;
6948c2ecf20Sopenharmony_ci
6958c2ecf20Sopenharmony_ci	/*
6968c2ecf20Sopenharmony_ci	 * For each block in the chain free it and make it available
6978c2ecf20Sopenharmony_ci	 * for future use. Erase from the oldest unit first.
6988c2ecf20Sopenharmony_ci	 */
6998c2ecf20Sopenharmony_ci	pr_debug("INFTL: deleting empty VUC %d\n", thisVUC);
7008c2ecf20Sopenharmony_ci
7018c2ecf20Sopenharmony_ci	for (;;) {
7028c2ecf20Sopenharmony_ci		u16 *prevEUN = &inftl->VUtable[thisVUC];
7038c2ecf20Sopenharmony_ci		thisEUN = *prevEUN;
7048c2ecf20Sopenharmony_ci
7058c2ecf20Sopenharmony_ci		/* If the chain is all gone already, we're done */
7068c2ecf20Sopenharmony_ci		if (thisEUN == BLOCK_NIL) {
7078c2ecf20Sopenharmony_ci			pr_debug("INFTL: Empty VUC %d for deletion was already absent\n", thisEUN);
7088c2ecf20Sopenharmony_ci			return;
7098c2ecf20Sopenharmony_ci		}
7108c2ecf20Sopenharmony_ci
7118c2ecf20Sopenharmony_ci		/* Find oldest unit in chain. */
7128c2ecf20Sopenharmony_ci		while (inftl->PUtable[thisEUN] != BLOCK_NIL) {
7138c2ecf20Sopenharmony_ci			BUG_ON(thisEUN >= inftl->nb_blocks);
7148c2ecf20Sopenharmony_ci
7158c2ecf20Sopenharmony_ci			prevEUN = &inftl->PUtable[thisEUN];
7168c2ecf20Sopenharmony_ci			thisEUN = *prevEUN;
7178c2ecf20Sopenharmony_ci		}
7188c2ecf20Sopenharmony_ci
7198c2ecf20Sopenharmony_ci		pr_debug("Deleting EUN %d from VUC %d\n",
7208c2ecf20Sopenharmony_ci		      thisEUN, thisVUC);
7218c2ecf20Sopenharmony_ci
7228c2ecf20Sopenharmony_ci		if (INFTL_formatblock(inftl, thisEUN) < 0) {
7238c2ecf20Sopenharmony_ci			/*
7248c2ecf20Sopenharmony_ci			 * Could not erase : mark block as reserved.
7258c2ecf20Sopenharmony_ci			 */
7268c2ecf20Sopenharmony_ci			inftl->PUtable[thisEUN] = BLOCK_RESERVED;
7278c2ecf20Sopenharmony_ci		} else {
7288c2ecf20Sopenharmony_ci			/* Correctly erased : mark it as free */
7298c2ecf20Sopenharmony_ci			inftl->PUtable[thisEUN] = BLOCK_FREE;
7308c2ecf20Sopenharmony_ci			inftl->numfreeEUNs++;
7318c2ecf20Sopenharmony_ci		}
7328c2ecf20Sopenharmony_ci
7338c2ecf20Sopenharmony_ci		/* Now sort out whatever was pointing to it... */
7348c2ecf20Sopenharmony_ci		*prevEUN = BLOCK_NIL;
7358c2ecf20Sopenharmony_ci
7368c2ecf20Sopenharmony_ci		/* Ideally we'd actually be responsive to new
7378c2ecf20Sopenharmony_ci		   requests while we're doing this -- if there's
7388c2ecf20Sopenharmony_ci		   free space why should others be made to wait? */
7398c2ecf20Sopenharmony_ci		cond_resched();
7408c2ecf20Sopenharmony_ci	}
7418c2ecf20Sopenharmony_ci
7428c2ecf20Sopenharmony_ci	inftl->VUtable[thisVUC] = BLOCK_NIL;
7438c2ecf20Sopenharmony_ci}
7448c2ecf20Sopenharmony_ci
7458c2ecf20Sopenharmony_cistatic int INFTL_deleteblock(struct INFTLrecord *inftl, unsigned block)
7468c2ecf20Sopenharmony_ci{
7478c2ecf20Sopenharmony_ci	unsigned int thisEUN = inftl->VUtable[block / (inftl->EraseSize / SECTORSIZE)];
7488c2ecf20Sopenharmony_ci	unsigned long blockofs = (block * SECTORSIZE) & (inftl->EraseSize - 1);
7498c2ecf20Sopenharmony_ci	struct mtd_info *mtd = inftl->mbd.mtd;
7508c2ecf20Sopenharmony_ci	unsigned int status;
7518c2ecf20Sopenharmony_ci	int silly = MAX_LOOPS;
7528c2ecf20Sopenharmony_ci	size_t retlen;
7538c2ecf20Sopenharmony_ci	struct inftl_bci bci;
7548c2ecf20Sopenharmony_ci
7558c2ecf20Sopenharmony_ci	pr_debug("INFTL: INFTL_deleteblock(inftl=%p,"
7568c2ecf20Sopenharmony_ci		"block=%d)\n", inftl, block);
7578c2ecf20Sopenharmony_ci
7588c2ecf20Sopenharmony_ci	while (thisEUN < inftl->nb_blocks) {
7598c2ecf20Sopenharmony_ci		if (inftl_read_oob(mtd, (thisEUN * inftl->EraseSize) +
7608c2ecf20Sopenharmony_ci				   blockofs, 8, &retlen, (char *)&bci) < 0)
7618c2ecf20Sopenharmony_ci			status = SECTOR_IGNORE;
7628c2ecf20Sopenharmony_ci		else
7638c2ecf20Sopenharmony_ci			status = bci.Status | bci.Status1;
7648c2ecf20Sopenharmony_ci
7658c2ecf20Sopenharmony_ci		switch (status) {
7668c2ecf20Sopenharmony_ci		case SECTOR_FREE:
7678c2ecf20Sopenharmony_ci		case SECTOR_IGNORE:
7688c2ecf20Sopenharmony_ci			break;
7698c2ecf20Sopenharmony_ci		case SECTOR_DELETED:
7708c2ecf20Sopenharmony_ci			thisEUN = BLOCK_NIL;
7718c2ecf20Sopenharmony_ci			goto foundit;
7728c2ecf20Sopenharmony_ci		case SECTOR_USED:
7738c2ecf20Sopenharmony_ci			goto foundit;
7748c2ecf20Sopenharmony_ci		default:
7758c2ecf20Sopenharmony_ci			printk(KERN_WARNING "INFTL: unknown status for "
7768c2ecf20Sopenharmony_ci				"block %d in EUN %d: 0x%x\n",
7778c2ecf20Sopenharmony_ci				block, thisEUN, status);
7788c2ecf20Sopenharmony_ci			break;
7798c2ecf20Sopenharmony_ci		}
7808c2ecf20Sopenharmony_ci
7818c2ecf20Sopenharmony_ci		if (!silly--) {
7828c2ecf20Sopenharmony_ci			printk(KERN_WARNING "INFTL: infinite loop in Virtual "
7838c2ecf20Sopenharmony_ci				"Unit Chain 0x%x\n",
7848c2ecf20Sopenharmony_ci				block / (inftl->EraseSize / SECTORSIZE));
7858c2ecf20Sopenharmony_ci			return 1;
7868c2ecf20Sopenharmony_ci		}
7878c2ecf20Sopenharmony_ci		thisEUN = inftl->PUtable[thisEUN];
7888c2ecf20Sopenharmony_ci	}
7898c2ecf20Sopenharmony_ci
7908c2ecf20Sopenharmony_cifoundit:
7918c2ecf20Sopenharmony_ci	if (thisEUN != BLOCK_NIL) {
7928c2ecf20Sopenharmony_ci		loff_t ptr = (thisEUN * inftl->EraseSize) + blockofs;
7938c2ecf20Sopenharmony_ci
7948c2ecf20Sopenharmony_ci		if (inftl_read_oob(mtd, ptr, 8, &retlen, (char *)&bci) < 0)
7958c2ecf20Sopenharmony_ci			return -EIO;
7968c2ecf20Sopenharmony_ci		bci.Status = bci.Status1 = SECTOR_DELETED;
7978c2ecf20Sopenharmony_ci		if (inftl_write_oob(mtd, ptr, 8, &retlen, (char *)&bci) < 0)
7988c2ecf20Sopenharmony_ci			return -EIO;
7998c2ecf20Sopenharmony_ci		INFTL_trydeletechain(inftl, block / (inftl->EraseSize / SECTORSIZE));
8008c2ecf20Sopenharmony_ci	}
8018c2ecf20Sopenharmony_ci	return 0;
8028c2ecf20Sopenharmony_ci}
8038c2ecf20Sopenharmony_ci
8048c2ecf20Sopenharmony_cistatic int inftl_writeblock(struct mtd_blktrans_dev *mbd, unsigned long block,
8058c2ecf20Sopenharmony_ci			    char *buffer)
8068c2ecf20Sopenharmony_ci{
8078c2ecf20Sopenharmony_ci	struct INFTLrecord *inftl = (void *)mbd;
8088c2ecf20Sopenharmony_ci	unsigned int writeEUN;
8098c2ecf20Sopenharmony_ci	unsigned long blockofs = (block * SECTORSIZE) & (inftl->EraseSize - 1);
8108c2ecf20Sopenharmony_ci	size_t retlen;
8118c2ecf20Sopenharmony_ci	struct inftl_oob oob;
8128c2ecf20Sopenharmony_ci	char *p, *pend;
8138c2ecf20Sopenharmony_ci
8148c2ecf20Sopenharmony_ci	pr_debug("INFTL: inftl_writeblock(inftl=%p,block=%ld,"
8158c2ecf20Sopenharmony_ci		"buffer=%p)\n", inftl, block, buffer);
8168c2ecf20Sopenharmony_ci
8178c2ecf20Sopenharmony_ci	/* Is block all zero? */
8188c2ecf20Sopenharmony_ci	pend = buffer + SECTORSIZE;
8198c2ecf20Sopenharmony_ci	for (p = buffer; p < pend && !*p; p++)
8208c2ecf20Sopenharmony_ci		;
8218c2ecf20Sopenharmony_ci
8228c2ecf20Sopenharmony_ci	if (p < pend) {
8238c2ecf20Sopenharmony_ci		writeEUN = INFTL_findwriteunit(inftl, block);
8248c2ecf20Sopenharmony_ci
8258c2ecf20Sopenharmony_ci		if (writeEUN == BLOCK_NIL) {
8268c2ecf20Sopenharmony_ci			printk(KERN_WARNING "inftl_writeblock(): cannot find "
8278c2ecf20Sopenharmony_ci				"block to write to\n");
8288c2ecf20Sopenharmony_ci			/*
8298c2ecf20Sopenharmony_ci			 * If we _still_ haven't got a block to use,
8308c2ecf20Sopenharmony_ci			 * we're screwed.
8318c2ecf20Sopenharmony_ci			 */
8328c2ecf20Sopenharmony_ci			return 1;
8338c2ecf20Sopenharmony_ci		}
8348c2ecf20Sopenharmony_ci
8358c2ecf20Sopenharmony_ci		memset(&oob, 0xff, sizeof(struct inftl_oob));
8368c2ecf20Sopenharmony_ci		oob.b.Status = oob.b.Status1 = SECTOR_USED;
8378c2ecf20Sopenharmony_ci
8388c2ecf20Sopenharmony_ci		inftl_write(inftl->mbd.mtd, (writeEUN * inftl->EraseSize) +
8398c2ecf20Sopenharmony_ci			    blockofs, SECTORSIZE, &retlen, (char *)buffer,
8408c2ecf20Sopenharmony_ci			    (char *)&oob);
8418c2ecf20Sopenharmony_ci		/*
8428c2ecf20Sopenharmony_ci		 * need to write SECTOR_USED flags since they are not written
8438c2ecf20Sopenharmony_ci		 * in mtd_writeecc
8448c2ecf20Sopenharmony_ci		 */
8458c2ecf20Sopenharmony_ci	} else {
8468c2ecf20Sopenharmony_ci		INFTL_deleteblock(inftl, block);
8478c2ecf20Sopenharmony_ci	}
8488c2ecf20Sopenharmony_ci
8498c2ecf20Sopenharmony_ci	return 0;
8508c2ecf20Sopenharmony_ci}
8518c2ecf20Sopenharmony_ci
8528c2ecf20Sopenharmony_cistatic int inftl_readblock(struct mtd_blktrans_dev *mbd, unsigned long block,
8538c2ecf20Sopenharmony_ci			   char *buffer)
8548c2ecf20Sopenharmony_ci{
8558c2ecf20Sopenharmony_ci	struct INFTLrecord *inftl = (void *)mbd;
8568c2ecf20Sopenharmony_ci	unsigned int thisEUN = inftl->VUtable[block / (inftl->EraseSize / SECTORSIZE)];
8578c2ecf20Sopenharmony_ci	unsigned long blockofs = (block * SECTORSIZE) & (inftl->EraseSize - 1);
8588c2ecf20Sopenharmony_ci	struct mtd_info *mtd = inftl->mbd.mtd;
8598c2ecf20Sopenharmony_ci	unsigned int status;
8608c2ecf20Sopenharmony_ci	int silly = MAX_LOOPS;
8618c2ecf20Sopenharmony_ci	struct inftl_bci bci;
8628c2ecf20Sopenharmony_ci	size_t retlen;
8638c2ecf20Sopenharmony_ci
8648c2ecf20Sopenharmony_ci	pr_debug("INFTL: inftl_readblock(inftl=%p,block=%ld,"
8658c2ecf20Sopenharmony_ci		"buffer=%p)\n", inftl, block, buffer);
8668c2ecf20Sopenharmony_ci
8678c2ecf20Sopenharmony_ci	while (thisEUN < inftl->nb_blocks) {
8688c2ecf20Sopenharmony_ci		if (inftl_read_oob(mtd, (thisEUN * inftl->EraseSize) +
8698c2ecf20Sopenharmony_ci				  blockofs, 8, &retlen, (char *)&bci) < 0)
8708c2ecf20Sopenharmony_ci			status = SECTOR_IGNORE;
8718c2ecf20Sopenharmony_ci		else
8728c2ecf20Sopenharmony_ci			status = bci.Status | bci.Status1;
8738c2ecf20Sopenharmony_ci
8748c2ecf20Sopenharmony_ci		switch (status) {
8758c2ecf20Sopenharmony_ci		case SECTOR_DELETED:
8768c2ecf20Sopenharmony_ci			thisEUN = BLOCK_NIL;
8778c2ecf20Sopenharmony_ci			goto foundit;
8788c2ecf20Sopenharmony_ci		case SECTOR_USED:
8798c2ecf20Sopenharmony_ci			goto foundit;
8808c2ecf20Sopenharmony_ci		case SECTOR_FREE:
8818c2ecf20Sopenharmony_ci		case SECTOR_IGNORE:
8828c2ecf20Sopenharmony_ci			break;
8838c2ecf20Sopenharmony_ci		default:
8848c2ecf20Sopenharmony_ci			printk(KERN_WARNING "INFTL: unknown status for "
8858c2ecf20Sopenharmony_ci				"block %ld in EUN %d: 0x%04x\n",
8868c2ecf20Sopenharmony_ci				block, thisEUN, status);
8878c2ecf20Sopenharmony_ci			break;
8888c2ecf20Sopenharmony_ci		}
8898c2ecf20Sopenharmony_ci
8908c2ecf20Sopenharmony_ci		if (!silly--) {
8918c2ecf20Sopenharmony_ci			printk(KERN_WARNING "INFTL: infinite loop in "
8928c2ecf20Sopenharmony_ci				"Virtual Unit Chain 0x%lx\n",
8938c2ecf20Sopenharmony_ci				block / (inftl->EraseSize / SECTORSIZE));
8948c2ecf20Sopenharmony_ci			return 1;
8958c2ecf20Sopenharmony_ci		}
8968c2ecf20Sopenharmony_ci
8978c2ecf20Sopenharmony_ci		thisEUN = inftl->PUtable[thisEUN];
8988c2ecf20Sopenharmony_ci	}
8998c2ecf20Sopenharmony_ci
9008c2ecf20Sopenharmony_cifoundit:
9018c2ecf20Sopenharmony_ci	if (thisEUN == BLOCK_NIL) {
9028c2ecf20Sopenharmony_ci		/* The requested block is not on the media, return all 0x00 */
9038c2ecf20Sopenharmony_ci		memset(buffer, 0, SECTORSIZE);
9048c2ecf20Sopenharmony_ci	} else {
9058c2ecf20Sopenharmony_ci		size_t retlen;
9068c2ecf20Sopenharmony_ci		loff_t ptr = (thisEUN * inftl->EraseSize) + blockofs;
9078c2ecf20Sopenharmony_ci		int ret = mtd_read(mtd, ptr, SECTORSIZE, &retlen, buffer);
9088c2ecf20Sopenharmony_ci
9098c2ecf20Sopenharmony_ci		/* Handle corrected bit flips gracefully */
9108c2ecf20Sopenharmony_ci		if (ret < 0 && !mtd_is_bitflip(ret))
9118c2ecf20Sopenharmony_ci			return -EIO;
9128c2ecf20Sopenharmony_ci	}
9138c2ecf20Sopenharmony_ci	return 0;
9148c2ecf20Sopenharmony_ci}
9158c2ecf20Sopenharmony_ci
9168c2ecf20Sopenharmony_cistatic int inftl_getgeo(struct mtd_blktrans_dev *dev, struct hd_geometry *geo)
9178c2ecf20Sopenharmony_ci{
9188c2ecf20Sopenharmony_ci	struct INFTLrecord *inftl = (void *)dev;
9198c2ecf20Sopenharmony_ci
9208c2ecf20Sopenharmony_ci	geo->heads = inftl->heads;
9218c2ecf20Sopenharmony_ci	geo->sectors = inftl->sectors;
9228c2ecf20Sopenharmony_ci	geo->cylinders = inftl->cylinders;
9238c2ecf20Sopenharmony_ci
9248c2ecf20Sopenharmony_ci	return 0;
9258c2ecf20Sopenharmony_ci}
9268c2ecf20Sopenharmony_ci
9278c2ecf20Sopenharmony_cistatic struct mtd_blktrans_ops inftl_tr = {
9288c2ecf20Sopenharmony_ci	.name		= "inftl",
9298c2ecf20Sopenharmony_ci	.major		= INFTL_MAJOR,
9308c2ecf20Sopenharmony_ci	.part_bits	= INFTL_PARTN_BITS,
9318c2ecf20Sopenharmony_ci	.blksize 	= 512,
9328c2ecf20Sopenharmony_ci	.getgeo		= inftl_getgeo,
9338c2ecf20Sopenharmony_ci	.readsect	= inftl_readblock,
9348c2ecf20Sopenharmony_ci	.writesect	= inftl_writeblock,
9358c2ecf20Sopenharmony_ci	.add_mtd	= inftl_add_mtd,
9368c2ecf20Sopenharmony_ci	.remove_dev	= inftl_remove_dev,
9378c2ecf20Sopenharmony_ci	.owner		= THIS_MODULE,
9388c2ecf20Sopenharmony_ci};
9398c2ecf20Sopenharmony_ci
9408c2ecf20Sopenharmony_cistatic int __init init_inftl(void)
9418c2ecf20Sopenharmony_ci{
9428c2ecf20Sopenharmony_ci	return register_mtd_blktrans(&inftl_tr);
9438c2ecf20Sopenharmony_ci}
9448c2ecf20Sopenharmony_ci
9458c2ecf20Sopenharmony_cistatic void __exit cleanup_inftl(void)
9468c2ecf20Sopenharmony_ci{
9478c2ecf20Sopenharmony_ci	deregister_mtd_blktrans(&inftl_tr);
9488c2ecf20Sopenharmony_ci}
9498c2ecf20Sopenharmony_ci
9508c2ecf20Sopenharmony_cimodule_init(init_inftl);
9518c2ecf20Sopenharmony_cimodule_exit(cleanup_inftl);
9528c2ecf20Sopenharmony_ci
9538c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL");
9548c2ecf20Sopenharmony_ciMODULE_AUTHOR("Greg Ungerer <gerg@snapgear.com>, David Woodhouse <dwmw2@infradead.org>, Fabrice Bellard <fabrice.bellard@netgem.com> et al.");
9558c2ecf20Sopenharmony_ciMODULE_DESCRIPTION("Support code for Inverse Flash Translation Layer, used on M-Systems DiskOnChip 2000, Millennium and Millennium Plus");
956