xref: /OK3568_Linux_fs/kernel/drivers/mtd/nftlcore.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0-or-later
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun  * Linux driver for NAND Flash Translation Layer
4*4882a593Smuzhiyun  *
5*4882a593Smuzhiyun  * Copyright © 1999 Machine Vision Holdings, Inc.
6*4882a593Smuzhiyun  * Copyright © 1999-2010 David Woodhouse <dwmw2@infradead.org>
7*4882a593Smuzhiyun  */
8*4882a593Smuzhiyun 
9*4882a593Smuzhiyun #define PRERELEASE
10*4882a593Smuzhiyun 
11*4882a593Smuzhiyun #include <linux/kernel.h>
12*4882a593Smuzhiyun #include <linux/module.h>
13*4882a593Smuzhiyun #include <asm/errno.h>
14*4882a593Smuzhiyun #include <asm/io.h>
15*4882a593Smuzhiyun #include <linux/uaccess.h>
16*4882a593Smuzhiyun #include <linux/delay.h>
17*4882a593Smuzhiyun #include <linux/slab.h>
18*4882a593Smuzhiyun #include <linux/init.h>
19*4882a593Smuzhiyun #include <linux/hdreg.h>
20*4882a593Smuzhiyun #include <linux/blkdev.h>
21*4882a593Smuzhiyun 
22*4882a593Smuzhiyun #include <linux/kmod.h>
23*4882a593Smuzhiyun #include <linux/mtd/mtd.h>
24*4882a593Smuzhiyun #include <linux/mtd/rawnand.h>
25*4882a593Smuzhiyun #include <linux/mtd/nftl.h>
26*4882a593Smuzhiyun #include <linux/mtd/blktrans.h>
27*4882a593Smuzhiyun 
28*4882a593Smuzhiyun /* maximum number of loops while examining next block, to have a
29*4882a593Smuzhiyun    chance to detect consistency problems (they should never happen
30*4882a593Smuzhiyun    because of the checks done in the mounting */
31*4882a593Smuzhiyun 
32*4882a593Smuzhiyun #define MAX_LOOPS 10000
33*4882a593Smuzhiyun 
34*4882a593Smuzhiyun 
nftl_add_mtd(struct mtd_blktrans_ops * tr,struct mtd_info * mtd)35*4882a593Smuzhiyun static void nftl_add_mtd(struct mtd_blktrans_ops *tr, struct mtd_info *mtd)
36*4882a593Smuzhiyun {
37*4882a593Smuzhiyun 	struct NFTLrecord *nftl;
38*4882a593Smuzhiyun 	unsigned long temp;
39*4882a593Smuzhiyun 
40*4882a593Smuzhiyun 	if (!mtd_type_is_nand(mtd) || mtd->size > UINT_MAX)
41*4882a593Smuzhiyun 		return;
42*4882a593Smuzhiyun 	/* OK, this is moderately ugly.  But probably safe.  Alternatives? */
43*4882a593Smuzhiyun 	if (memcmp(mtd->name, "DiskOnChip", 10))
44*4882a593Smuzhiyun 		return;
45*4882a593Smuzhiyun 
46*4882a593Smuzhiyun 	pr_debug("NFTL: add_mtd for %s\n", mtd->name);
47*4882a593Smuzhiyun 
48*4882a593Smuzhiyun 	nftl = kzalloc(sizeof(struct NFTLrecord), GFP_KERNEL);
49*4882a593Smuzhiyun 
50*4882a593Smuzhiyun 	if (!nftl)
51*4882a593Smuzhiyun 		return;
52*4882a593Smuzhiyun 
53*4882a593Smuzhiyun 	nftl->mbd.mtd = mtd;
54*4882a593Smuzhiyun 	nftl->mbd.devnum = -1;
55*4882a593Smuzhiyun 
56*4882a593Smuzhiyun 	nftl->mbd.tr = tr;
57*4882a593Smuzhiyun 
58*4882a593Smuzhiyun         if (NFTL_mount(nftl) < 0) {
59*4882a593Smuzhiyun 		printk(KERN_WARNING "NFTL: could not mount device\n");
60*4882a593Smuzhiyun 		kfree(nftl);
61*4882a593Smuzhiyun 		return;
62*4882a593Smuzhiyun         }
63*4882a593Smuzhiyun 
64*4882a593Smuzhiyun 	/* OK, it's a new one. Set up all the data structures. */
65*4882a593Smuzhiyun 
66*4882a593Smuzhiyun 	/* Calculate geometry */
67*4882a593Smuzhiyun 	nftl->cylinders = 1024;
68*4882a593Smuzhiyun 	nftl->heads = 16;
69*4882a593Smuzhiyun 
70*4882a593Smuzhiyun 	temp = nftl->cylinders * nftl->heads;
71*4882a593Smuzhiyun 	nftl->sectors = nftl->mbd.size / temp;
72*4882a593Smuzhiyun 	if (nftl->mbd.size % temp) {
73*4882a593Smuzhiyun 		nftl->sectors++;
74*4882a593Smuzhiyun 		temp = nftl->cylinders * nftl->sectors;
75*4882a593Smuzhiyun 		nftl->heads = nftl->mbd.size / temp;
76*4882a593Smuzhiyun 
77*4882a593Smuzhiyun 		if (nftl->mbd.size % temp) {
78*4882a593Smuzhiyun 			nftl->heads++;
79*4882a593Smuzhiyun 			temp = nftl->heads * nftl->sectors;
80*4882a593Smuzhiyun 			nftl->cylinders = nftl->mbd.size / temp;
81*4882a593Smuzhiyun 		}
82*4882a593Smuzhiyun 	}
83*4882a593Smuzhiyun 
84*4882a593Smuzhiyun 	if (nftl->mbd.size != nftl->heads * nftl->cylinders * nftl->sectors) {
85*4882a593Smuzhiyun 		/*
86*4882a593Smuzhiyun 		  Oh no we don't have
87*4882a593Smuzhiyun 		   mbd.size == heads * cylinders * sectors
88*4882a593Smuzhiyun 		*/
89*4882a593Smuzhiyun 		printk(KERN_WARNING "NFTL: cannot calculate a geometry to "
90*4882a593Smuzhiyun 		       "match size of 0x%lx.\n", nftl->mbd.size);
91*4882a593Smuzhiyun 		printk(KERN_WARNING "NFTL: using C:%d H:%d S:%d "
92*4882a593Smuzhiyun 			"(== 0x%lx sects)\n",
93*4882a593Smuzhiyun 			nftl->cylinders, nftl->heads , nftl->sectors,
94*4882a593Smuzhiyun 			(long)nftl->cylinders * (long)nftl->heads *
95*4882a593Smuzhiyun 			(long)nftl->sectors );
96*4882a593Smuzhiyun 	}
97*4882a593Smuzhiyun 
98*4882a593Smuzhiyun 	if (add_mtd_blktrans_dev(&nftl->mbd)) {
99*4882a593Smuzhiyun 		kfree(nftl->ReplUnitTable);
100*4882a593Smuzhiyun 		kfree(nftl->EUNtable);
101*4882a593Smuzhiyun 		kfree(nftl);
102*4882a593Smuzhiyun 		return;
103*4882a593Smuzhiyun 	}
104*4882a593Smuzhiyun #ifdef PSYCHO_DEBUG
105*4882a593Smuzhiyun 	printk(KERN_INFO "NFTL: Found new nftl%c\n", nftl->mbd.devnum + 'a');
106*4882a593Smuzhiyun #endif
107*4882a593Smuzhiyun }
108*4882a593Smuzhiyun 
nftl_remove_dev(struct mtd_blktrans_dev * dev)109*4882a593Smuzhiyun static void nftl_remove_dev(struct mtd_blktrans_dev *dev)
110*4882a593Smuzhiyun {
111*4882a593Smuzhiyun 	struct NFTLrecord *nftl = (void *)dev;
112*4882a593Smuzhiyun 
113*4882a593Smuzhiyun 	pr_debug("NFTL: remove_dev (i=%d)\n", dev->devnum);
114*4882a593Smuzhiyun 
115*4882a593Smuzhiyun 	del_mtd_blktrans_dev(dev);
116*4882a593Smuzhiyun 	kfree(nftl->ReplUnitTable);
117*4882a593Smuzhiyun 	kfree(nftl->EUNtable);
118*4882a593Smuzhiyun }
119*4882a593Smuzhiyun 
120*4882a593Smuzhiyun /*
121*4882a593Smuzhiyun  * Read oob data from flash
122*4882a593Smuzhiyun  */
nftl_read_oob(struct mtd_info * mtd,loff_t offs,size_t len,size_t * retlen,uint8_t * buf)123*4882a593Smuzhiyun int nftl_read_oob(struct mtd_info *mtd, loff_t offs, size_t len,
124*4882a593Smuzhiyun 		  size_t *retlen, uint8_t *buf)
125*4882a593Smuzhiyun {
126*4882a593Smuzhiyun 	loff_t mask = mtd->writesize - 1;
127*4882a593Smuzhiyun 	struct mtd_oob_ops ops;
128*4882a593Smuzhiyun 	int res;
129*4882a593Smuzhiyun 
130*4882a593Smuzhiyun 	ops.mode = MTD_OPS_PLACE_OOB;
131*4882a593Smuzhiyun 	ops.ooboffs = offs & mask;
132*4882a593Smuzhiyun 	ops.ooblen = len;
133*4882a593Smuzhiyun 	ops.oobbuf = buf;
134*4882a593Smuzhiyun 	ops.datbuf = NULL;
135*4882a593Smuzhiyun 
136*4882a593Smuzhiyun 	res = mtd_read_oob(mtd, offs & ~mask, &ops);
137*4882a593Smuzhiyun 	*retlen = ops.oobretlen;
138*4882a593Smuzhiyun 	return res;
139*4882a593Smuzhiyun }
140*4882a593Smuzhiyun 
141*4882a593Smuzhiyun /*
142*4882a593Smuzhiyun  * Write oob data to flash
143*4882a593Smuzhiyun  */
nftl_write_oob(struct mtd_info * mtd,loff_t offs,size_t len,size_t * retlen,uint8_t * buf)144*4882a593Smuzhiyun int nftl_write_oob(struct mtd_info *mtd, loff_t offs, size_t len,
145*4882a593Smuzhiyun 		   size_t *retlen, uint8_t *buf)
146*4882a593Smuzhiyun {
147*4882a593Smuzhiyun 	loff_t mask = mtd->writesize - 1;
148*4882a593Smuzhiyun 	struct mtd_oob_ops ops;
149*4882a593Smuzhiyun 	int res;
150*4882a593Smuzhiyun 
151*4882a593Smuzhiyun 	ops.mode = MTD_OPS_PLACE_OOB;
152*4882a593Smuzhiyun 	ops.ooboffs = offs & mask;
153*4882a593Smuzhiyun 	ops.ooblen = len;
154*4882a593Smuzhiyun 	ops.oobbuf = buf;
155*4882a593Smuzhiyun 	ops.datbuf = NULL;
156*4882a593Smuzhiyun 
157*4882a593Smuzhiyun 	res = mtd_write_oob(mtd, offs & ~mask, &ops);
158*4882a593Smuzhiyun 	*retlen = ops.oobretlen;
159*4882a593Smuzhiyun 	return res;
160*4882a593Smuzhiyun }
161*4882a593Smuzhiyun 
162*4882a593Smuzhiyun #ifdef CONFIG_NFTL_RW
163*4882a593Smuzhiyun 
164*4882a593Smuzhiyun /*
165*4882a593Smuzhiyun  * Write data and oob to flash
166*4882a593Smuzhiyun  */
nftl_write(struct mtd_info * mtd,loff_t offs,size_t len,size_t * retlen,uint8_t * buf,uint8_t * oob)167*4882a593Smuzhiyun static int nftl_write(struct mtd_info *mtd, loff_t offs, size_t len,
168*4882a593Smuzhiyun 		      size_t *retlen, uint8_t *buf, uint8_t *oob)
169*4882a593Smuzhiyun {
170*4882a593Smuzhiyun 	loff_t mask = mtd->writesize - 1;
171*4882a593Smuzhiyun 	struct mtd_oob_ops ops;
172*4882a593Smuzhiyun 	int res;
173*4882a593Smuzhiyun 
174*4882a593Smuzhiyun 	ops.mode = MTD_OPS_PLACE_OOB;
175*4882a593Smuzhiyun 	ops.ooboffs = offs & mask;
176*4882a593Smuzhiyun 	ops.ooblen = mtd->oobsize;
177*4882a593Smuzhiyun 	ops.oobbuf = oob;
178*4882a593Smuzhiyun 	ops.datbuf = buf;
179*4882a593Smuzhiyun 	ops.len = len;
180*4882a593Smuzhiyun 
181*4882a593Smuzhiyun 	res = mtd_write_oob(mtd, offs & ~mask, &ops);
182*4882a593Smuzhiyun 	*retlen = ops.retlen;
183*4882a593Smuzhiyun 	return res;
184*4882a593Smuzhiyun }
185*4882a593Smuzhiyun 
186*4882a593Smuzhiyun /* Actual NFTL access routines */
187*4882a593Smuzhiyun /* NFTL_findfreeblock: Find a free Erase Unit on the NFTL partition. This function is used
188*4882a593Smuzhiyun  *	when the give Virtual Unit Chain
189*4882a593Smuzhiyun  */
NFTL_findfreeblock(struct NFTLrecord * nftl,int desperate)190*4882a593Smuzhiyun static u16 NFTL_findfreeblock(struct NFTLrecord *nftl, int desperate )
191*4882a593Smuzhiyun {
192*4882a593Smuzhiyun 	/* For a given Virtual Unit Chain: find or create a free block and
193*4882a593Smuzhiyun 	   add it to the chain */
194*4882a593Smuzhiyun 	/* We're passed the number of the last EUN in the chain, to save us from
195*4882a593Smuzhiyun 	   having to look it up again */
196*4882a593Smuzhiyun 	u16 pot = nftl->LastFreeEUN;
197*4882a593Smuzhiyun 	int silly = nftl->nb_blocks;
198*4882a593Smuzhiyun 
199*4882a593Smuzhiyun 	/* Normally, we force a fold to happen before we run out of free blocks completely */
200*4882a593Smuzhiyun 	if (!desperate && nftl->numfreeEUNs < 2) {
201*4882a593Smuzhiyun 		pr_debug("NFTL_findfreeblock: there are too few free EUNs\n");
202*4882a593Smuzhiyun 		return BLOCK_NIL;
203*4882a593Smuzhiyun 	}
204*4882a593Smuzhiyun 
205*4882a593Smuzhiyun 	/* Scan for a free block */
206*4882a593Smuzhiyun 	do {
207*4882a593Smuzhiyun 		if (nftl->ReplUnitTable[pot] == BLOCK_FREE) {
208*4882a593Smuzhiyun 			nftl->LastFreeEUN = pot;
209*4882a593Smuzhiyun 			nftl->numfreeEUNs--;
210*4882a593Smuzhiyun 			return pot;
211*4882a593Smuzhiyun 		}
212*4882a593Smuzhiyun 
213*4882a593Smuzhiyun 		/* This will probably point to the MediaHdr unit itself,
214*4882a593Smuzhiyun 		   right at the beginning of the partition. But that unit
215*4882a593Smuzhiyun 		   (and the backup unit too) should have the UCI set
216*4882a593Smuzhiyun 		   up so that it's not selected for overwriting */
217*4882a593Smuzhiyun 		if (++pot > nftl->lastEUN)
218*4882a593Smuzhiyun 			pot = le16_to_cpu(nftl->MediaHdr.FirstPhysicalEUN);
219*4882a593Smuzhiyun 
220*4882a593Smuzhiyun 		if (!silly--) {
221*4882a593Smuzhiyun 			printk("Argh! No free blocks found! LastFreeEUN = %d, "
222*4882a593Smuzhiyun 			       "FirstEUN = %d\n", nftl->LastFreeEUN,
223*4882a593Smuzhiyun 			       le16_to_cpu(nftl->MediaHdr.FirstPhysicalEUN));
224*4882a593Smuzhiyun 			return BLOCK_NIL;
225*4882a593Smuzhiyun 		}
226*4882a593Smuzhiyun 	} while (pot != nftl->LastFreeEUN);
227*4882a593Smuzhiyun 
228*4882a593Smuzhiyun 	return BLOCK_NIL;
229*4882a593Smuzhiyun }
230*4882a593Smuzhiyun 
NFTL_foldchain(struct NFTLrecord * nftl,unsigned thisVUC,unsigned pendingblock)231*4882a593Smuzhiyun static u16 NFTL_foldchain (struct NFTLrecord *nftl, unsigned thisVUC, unsigned pendingblock )
232*4882a593Smuzhiyun {
233*4882a593Smuzhiyun 	struct mtd_info *mtd = nftl->mbd.mtd;
234*4882a593Smuzhiyun 	u16 BlockMap[MAX_SECTORS_PER_UNIT];
235*4882a593Smuzhiyun 	unsigned char BlockLastState[MAX_SECTORS_PER_UNIT];
236*4882a593Smuzhiyun 	unsigned char BlockFreeFound[MAX_SECTORS_PER_UNIT];
237*4882a593Smuzhiyun 	unsigned int thisEUN;
238*4882a593Smuzhiyun 	int block;
239*4882a593Smuzhiyun 	int silly;
240*4882a593Smuzhiyun 	unsigned int targetEUN;
241*4882a593Smuzhiyun 	struct nftl_oob oob;
242*4882a593Smuzhiyun 	int inplace = 1;
243*4882a593Smuzhiyun 	size_t retlen;
244*4882a593Smuzhiyun 
245*4882a593Smuzhiyun 	memset(BlockMap, 0xff, sizeof(BlockMap));
246*4882a593Smuzhiyun 	memset(BlockFreeFound, 0, sizeof(BlockFreeFound));
247*4882a593Smuzhiyun 
248*4882a593Smuzhiyun 	thisEUN = nftl->EUNtable[thisVUC];
249*4882a593Smuzhiyun 
250*4882a593Smuzhiyun 	if (thisEUN == BLOCK_NIL) {
251*4882a593Smuzhiyun 		printk(KERN_WARNING "Trying to fold non-existent "
252*4882a593Smuzhiyun 		       "Virtual Unit Chain %d!\n", thisVUC);
253*4882a593Smuzhiyun 		return BLOCK_NIL;
254*4882a593Smuzhiyun 	}
255*4882a593Smuzhiyun 
256*4882a593Smuzhiyun 	/* Scan to find the Erase Unit which holds the actual data for each
257*4882a593Smuzhiyun 	   512-byte block within the Chain.
258*4882a593Smuzhiyun 	*/
259*4882a593Smuzhiyun 	silly = MAX_LOOPS;
260*4882a593Smuzhiyun 	targetEUN = BLOCK_NIL;
261*4882a593Smuzhiyun 	while (thisEUN <= nftl->lastEUN ) {
262*4882a593Smuzhiyun 		unsigned int status, foldmark;
263*4882a593Smuzhiyun 
264*4882a593Smuzhiyun 		targetEUN = thisEUN;
265*4882a593Smuzhiyun 		for (block = 0; block < nftl->EraseSize / 512; block ++) {
266*4882a593Smuzhiyun 			nftl_read_oob(mtd, (thisEUN * nftl->EraseSize) +
267*4882a593Smuzhiyun 				      (block * 512), 16 , &retlen,
268*4882a593Smuzhiyun 				      (char *)&oob);
269*4882a593Smuzhiyun 			if (block == 2) {
270*4882a593Smuzhiyun 				foldmark = oob.u.c.FoldMark | oob.u.c.FoldMark1;
271*4882a593Smuzhiyun 				if (foldmark == FOLD_MARK_IN_PROGRESS) {
272*4882a593Smuzhiyun 					pr_debug("Write Inhibited on EUN %d\n", thisEUN);
273*4882a593Smuzhiyun 					inplace = 0;
274*4882a593Smuzhiyun 				} else {
275*4882a593Smuzhiyun 					/* There's no other reason not to do inplace,
276*4882a593Smuzhiyun 					   except ones that come later. So we don't need
277*4882a593Smuzhiyun 					   to preserve inplace */
278*4882a593Smuzhiyun 					inplace = 1;
279*4882a593Smuzhiyun 				}
280*4882a593Smuzhiyun 			}
281*4882a593Smuzhiyun 			status = oob.b.Status | oob.b.Status1;
282*4882a593Smuzhiyun 			BlockLastState[block] = status;
283*4882a593Smuzhiyun 
284*4882a593Smuzhiyun 			switch(status) {
285*4882a593Smuzhiyun 			case SECTOR_FREE:
286*4882a593Smuzhiyun 				BlockFreeFound[block] = 1;
287*4882a593Smuzhiyun 				break;
288*4882a593Smuzhiyun 
289*4882a593Smuzhiyun 			case SECTOR_USED:
290*4882a593Smuzhiyun 				if (!BlockFreeFound[block])
291*4882a593Smuzhiyun 					BlockMap[block] = thisEUN;
292*4882a593Smuzhiyun 				else
293*4882a593Smuzhiyun 					printk(KERN_WARNING
294*4882a593Smuzhiyun 					       "SECTOR_USED found after SECTOR_FREE "
295*4882a593Smuzhiyun 					       "in Virtual Unit Chain %d for block %d\n",
296*4882a593Smuzhiyun 					       thisVUC, block);
297*4882a593Smuzhiyun 				break;
298*4882a593Smuzhiyun 			case SECTOR_DELETED:
299*4882a593Smuzhiyun 				if (!BlockFreeFound[block])
300*4882a593Smuzhiyun 					BlockMap[block] = BLOCK_NIL;
301*4882a593Smuzhiyun 				else
302*4882a593Smuzhiyun 					printk(KERN_WARNING
303*4882a593Smuzhiyun 					       "SECTOR_DELETED found after SECTOR_FREE "
304*4882a593Smuzhiyun 					       "in Virtual Unit Chain %d for block %d\n",
305*4882a593Smuzhiyun 					       thisVUC, block);
306*4882a593Smuzhiyun 				break;
307*4882a593Smuzhiyun 
308*4882a593Smuzhiyun 			case SECTOR_IGNORE:
309*4882a593Smuzhiyun 				break;
310*4882a593Smuzhiyun 			default:
311*4882a593Smuzhiyun 				printk("Unknown status for block %d in EUN %d: %x\n",
312*4882a593Smuzhiyun 				       block, thisEUN, status);
313*4882a593Smuzhiyun 			}
314*4882a593Smuzhiyun 		}
315*4882a593Smuzhiyun 
316*4882a593Smuzhiyun 		if (!silly--) {
317*4882a593Smuzhiyun 			printk(KERN_WARNING "Infinite loop in Virtual Unit Chain 0x%x\n",
318*4882a593Smuzhiyun 			       thisVUC);
319*4882a593Smuzhiyun 			return BLOCK_NIL;
320*4882a593Smuzhiyun 		}
321*4882a593Smuzhiyun 
322*4882a593Smuzhiyun 		thisEUN = nftl->ReplUnitTable[thisEUN];
323*4882a593Smuzhiyun 	}
324*4882a593Smuzhiyun 
325*4882a593Smuzhiyun 	if (inplace) {
326*4882a593Smuzhiyun 		/* We're being asked to be a fold-in-place. Check
327*4882a593Smuzhiyun 		   that all blocks which actually have data associated
328*4882a593Smuzhiyun 		   with them (i.e. BlockMap[block] != BLOCK_NIL) are
329*4882a593Smuzhiyun 		   either already present or SECTOR_FREE in the target
330*4882a593Smuzhiyun 		   block. If not, we're going to have to fold out-of-place
331*4882a593Smuzhiyun 		   anyway.
332*4882a593Smuzhiyun 		*/
333*4882a593Smuzhiyun 		for (block = 0; block < nftl->EraseSize / 512 ; block++) {
334*4882a593Smuzhiyun 			if (BlockLastState[block] != SECTOR_FREE &&
335*4882a593Smuzhiyun 			    BlockMap[block] != BLOCK_NIL &&
336*4882a593Smuzhiyun 			    BlockMap[block] != targetEUN) {
337*4882a593Smuzhiyun 				pr_debug("Setting inplace to 0. VUC %d, "
338*4882a593Smuzhiyun 				      "block %d was %x lastEUN, "
339*4882a593Smuzhiyun 				      "and is in EUN %d (%s) %d\n",
340*4882a593Smuzhiyun 				      thisVUC, block, BlockLastState[block],
341*4882a593Smuzhiyun 				      BlockMap[block],
342*4882a593Smuzhiyun 				      BlockMap[block]== targetEUN ? "==" : "!=",
343*4882a593Smuzhiyun 				      targetEUN);
344*4882a593Smuzhiyun 				inplace = 0;
345*4882a593Smuzhiyun 				break;
346*4882a593Smuzhiyun 			}
347*4882a593Smuzhiyun 		}
348*4882a593Smuzhiyun 
349*4882a593Smuzhiyun 		if (pendingblock >= (thisVUC * (nftl->EraseSize / 512)) &&
350*4882a593Smuzhiyun 		    pendingblock < ((thisVUC + 1)* (nftl->EraseSize / 512)) &&
351*4882a593Smuzhiyun 		    BlockLastState[pendingblock - (thisVUC * (nftl->EraseSize / 512))] !=
352*4882a593Smuzhiyun 		    SECTOR_FREE) {
353*4882a593Smuzhiyun 			pr_debug("Pending write not free in EUN %d. "
354*4882a593Smuzhiyun 			      "Folding out of place.\n", targetEUN);
355*4882a593Smuzhiyun 			inplace = 0;
356*4882a593Smuzhiyun 		}
357*4882a593Smuzhiyun 	}
358*4882a593Smuzhiyun 
359*4882a593Smuzhiyun 	if (!inplace) {
360*4882a593Smuzhiyun 		pr_debug("Cannot fold Virtual Unit Chain %d in place. "
361*4882a593Smuzhiyun 		      "Trying out-of-place\n", thisVUC);
362*4882a593Smuzhiyun 		/* We need to find a targetEUN to fold into. */
363*4882a593Smuzhiyun 		targetEUN = NFTL_findfreeblock(nftl, 1);
364*4882a593Smuzhiyun 		if (targetEUN == BLOCK_NIL) {
365*4882a593Smuzhiyun 			/* Ouch. Now we're screwed. We need to do a
366*4882a593Smuzhiyun 			   fold-in-place of another chain to make room
367*4882a593Smuzhiyun 			   for this one. We need a better way of selecting
368*4882a593Smuzhiyun 			   which chain to fold, because makefreeblock will
369*4882a593Smuzhiyun 			   only ask us to fold the same one again.
370*4882a593Smuzhiyun 			*/
371*4882a593Smuzhiyun 			printk(KERN_WARNING
372*4882a593Smuzhiyun 			       "NFTL_findfreeblock(desperate) returns 0xffff.\n");
373*4882a593Smuzhiyun 			return BLOCK_NIL;
374*4882a593Smuzhiyun 		}
375*4882a593Smuzhiyun 	} else {
376*4882a593Smuzhiyun 		/* We put a fold mark in the chain we are folding only if we
377*4882a593Smuzhiyun                fold in place to help the mount check code. If we do not fold in
378*4882a593Smuzhiyun                place, it is possible to find the valid chain by selecting the
379*4882a593Smuzhiyun                longer one */
380*4882a593Smuzhiyun 		oob.u.c.FoldMark = oob.u.c.FoldMark1 = cpu_to_le16(FOLD_MARK_IN_PROGRESS);
381*4882a593Smuzhiyun 		oob.u.c.unused = 0xffffffff;
382*4882a593Smuzhiyun 		nftl_write_oob(mtd, (nftl->EraseSize * targetEUN) + 2 * 512 + 8,
383*4882a593Smuzhiyun 			       8, &retlen, (char *)&oob.u);
384*4882a593Smuzhiyun 	}
385*4882a593Smuzhiyun 
386*4882a593Smuzhiyun 	/* OK. We now know the location of every block in the Virtual Unit Chain,
387*4882a593Smuzhiyun 	   and the Erase Unit into which we are supposed to be copying.
388*4882a593Smuzhiyun 	   Go for it.
389*4882a593Smuzhiyun 	*/
390*4882a593Smuzhiyun 	pr_debug("Folding chain %d into unit %d\n", thisVUC, targetEUN);
391*4882a593Smuzhiyun 	for (block = 0; block < nftl->EraseSize / 512 ; block++) {
392*4882a593Smuzhiyun 		unsigned char movebuf[512];
393*4882a593Smuzhiyun 		int ret;
394*4882a593Smuzhiyun 
395*4882a593Smuzhiyun 		/* If it's in the target EUN already, or if it's pending write, do nothing */
396*4882a593Smuzhiyun 		if (BlockMap[block] == targetEUN ||
397*4882a593Smuzhiyun 		    (pendingblock == (thisVUC * (nftl->EraseSize / 512) + block))) {
398*4882a593Smuzhiyun 			continue;
399*4882a593Smuzhiyun 		}
400*4882a593Smuzhiyun 
401*4882a593Smuzhiyun 		/* copy only in non free block (free blocks can only
402*4882a593Smuzhiyun                    happen in case of media errors or deleted blocks) */
403*4882a593Smuzhiyun 		if (BlockMap[block] == BLOCK_NIL)
404*4882a593Smuzhiyun 			continue;
405*4882a593Smuzhiyun 
406*4882a593Smuzhiyun 		ret = mtd_read(mtd,
407*4882a593Smuzhiyun 			       (nftl->EraseSize * BlockMap[block]) + (block * 512),
408*4882a593Smuzhiyun 			       512,
409*4882a593Smuzhiyun 			       &retlen,
410*4882a593Smuzhiyun 			       movebuf);
411*4882a593Smuzhiyun 		if (ret < 0 && !mtd_is_bitflip(ret)) {
412*4882a593Smuzhiyun 			ret = mtd_read(mtd,
413*4882a593Smuzhiyun 				       (nftl->EraseSize * BlockMap[block]) + (block * 512),
414*4882a593Smuzhiyun 				       512,
415*4882a593Smuzhiyun 				       &retlen,
416*4882a593Smuzhiyun 				       movebuf);
417*4882a593Smuzhiyun 			if (ret != -EIO)
418*4882a593Smuzhiyun 				printk("Error went away on retry.\n");
419*4882a593Smuzhiyun 		}
420*4882a593Smuzhiyun 		memset(&oob, 0xff, sizeof(struct nftl_oob));
421*4882a593Smuzhiyun 		oob.b.Status = oob.b.Status1 = SECTOR_USED;
422*4882a593Smuzhiyun 
423*4882a593Smuzhiyun 		nftl_write(nftl->mbd.mtd, (nftl->EraseSize * targetEUN) +
424*4882a593Smuzhiyun 			   (block * 512), 512, &retlen, movebuf, (char *)&oob);
425*4882a593Smuzhiyun 	}
426*4882a593Smuzhiyun 
427*4882a593Smuzhiyun 	/* add the header so that it is now a valid chain */
428*4882a593Smuzhiyun 	oob.u.a.VirtUnitNum = oob.u.a.SpareVirtUnitNum = cpu_to_le16(thisVUC);
429*4882a593Smuzhiyun 	oob.u.a.ReplUnitNum = oob.u.a.SpareReplUnitNum = BLOCK_NIL;
430*4882a593Smuzhiyun 
431*4882a593Smuzhiyun 	nftl_write_oob(mtd, (nftl->EraseSize * targetEUN) + 8,
432*4882a593Smuzhiyun 		       8, &retlen, (char *)&oob.u);
433*4882a593Smuzhiyun 
434*4882a593Smuzhiyun 	/* OK. We've moved the whole lot into the new block. Now we have to free the original blocks. */
435*4882a593Smuzhiyun 
436*4882a593Smuzhiyun 	/* At this point, we have two different chains for this Virtual Unit, and no way to tell
437*4882a593Smuzhiyun 	   them apart. If we crash now, we get confused. However, both contain the same data, so we
438*4882a593Smuzhiyun 	   shouldn't actually lose data in this case. It's just that when we load up on a medium which
439*4882a593Smuzhiyun 	   has duplicate chains, we need to free one of the chains because it's not necessary any more.
440*4882a593Smuzhiyun 	*/
441*4882a593Smuzhiyun 	thisEUN = nftl->EUNtable[thisVUC];
442*4882a593Smuzhiyun 	pr_debug("Want to erase\n");
443*4882a593Smuzhiyun 
444*4882a593Smuzhiyun 	/* For each block in the old chain (except the targetEUN of course),
445*4882a593Smuzhiyun 	   free it and make it available for future use */
446*4882a593Smuzhiyun 	while (thisEUN <= nftl->lastEUN && thisEUN != targetEUN) {
447*4882a593Smuzhiyun 		unsigned int EUNtmp;
448*4882a593Smuzhiyun 
449*4882a593Smuzhiyun 		EUNtmp = nftl->ReplUnitTable[thisEUN];
450*4882a593Smuzhiyun 
451*4882a593Smuzhiyun 		if (NFTL_formatblock(nftl, thisEUN) < 0) {
452*4882a593Smuzhiyun 			/* could not erase : mark block as reserved
453*4882a593Smuzhiyun 			 */
454*4882a593Smuzhiyun 			nftl->ReplUnitTable[thisEUN] = BLOCK_RESERVED;
455*4882a593Smuzhiyun 		} else {
456*4882a593Smuzhiyun 			/* correctly erased : mark it as free */
457*4882a593Smuzhiyun 			nftl->ReplUnitTable[thisEUN] = BLOCK_FREE;
458*4882a593Smuzhiyun 			nftl->numfreeEUNs++;
459*4882a593Smuzhiyun 		}
460*4882a593Smuzhiyun 		thisEUN = EUNtmp;
461*4882a593Smuzhiyun 	}
462*4882a593Smuzhiyun 
463*4882a593Smuzhiyun 	/* Make this the new start of chain for thisVUC */
464*4882a593Smuzhiyun 	nftl->ReplUnitTable[targetEUN] = BLOCK_NIL;
465*4882a593Smuzhiyun 	nftl->EUNtable[thisVUC] = targetEUN;
466*4882a593Smuzhiyun 
467*4882a593Smuzhiyun 	return targetEUN;
468*4882a593Smuzhiyun }
469*4882a593Smuzhiyun 
NFTL_makefreeblock(struct NFTLrecord * nftl,unsigned pendingblock)470*4882a593Smuzhiyun static u16 NFTL_makefreeblock( struct NFTLrecord *nftl , unsigned pendingblock)
471*4882a593Smuzhiyun {
472*4882a593Smuzhiyun 	/* This is the part that needs some cleverness applied.
473*4882a593Smuzhiyun 	   For now, I'm doing the minimum applicable to actually
474*4882a593Smuzhiyun 	   get the thing to work.
475*4882a593Smuzhiyun 	   Wear-levelling and other clever stuff needs to be implemented
476*4882a593Smuzhiyun 	   and we also need to do some assessment of the results when
477*4882a593Smuzhiyun 	   the system loses power half-way through the routine.
478*4882a593Smuzhiyun 	*/
479*4882a593Smuzhiyun 	u16 LongestChain = 0;
480*4882a593Smuzhiyun 	u16 ChainLength = 0, thislen;
481*4882a593Smuzhiyun 	u16 chain, EUN;
482*4882a593Smuzhiyun 
483*4882a593Smuzhiyun 	for (chain = 0; chain < le32_to_cpu(nftl->MediaHdr.FormattedSize) / nftl->EraseSize; chain++) {
484*4882a593Smuzhiyun 		EUN = nftl->EUNtable[chain];
485*4882a593Smuzhiyun 		thislen = 0;
486*4882a593Smuzhiyun 
487*4882a593Smuzhiyun 		while (EUN <= nftl->lastEUN) {
488*4882a593Smuzhiyun 			thislen++;
489*4882a593Smuzhiyun 			//printk("VUC %d reaches len %d with EUN %d\n", chain, thislen, EUN);
490*4882a593Smuzhiyun 			EUN = nftl->ReplUnitTable[EUN] & 0x7fff;
491*4882a593Smuzhiyun 			if (thislen > 0xff00) {
492*4882a593Smuzhiyun 				printk("Endless loop in Virtual Chain %d: Unit %x\n",
493*4882a593Smuzhiyun 				       chain, EUN);
494*4882a593Smuzhiyun 			}
495*4882a593Smuzhiyun 			if (thislen > 0xff10) {
496*4882a593Smuzhiyun 				/* Actually, don't return failure. Just ignore this chain and
497*4882a593Smuzhiyun 				   get on with it. */
498*4882a593Smuzhiyun 				thislen = 0;
499*4882a593Smuzhiyun 				break;
500*4882a593Smuzhiyun 			}
501*4882a593Smuzhiyun 		}
502*4882a593Smuzhiyun 
503*4882a593Smuzhiyun 		if (thislen > ChainLength) {
504*4882a593Smuzhiyun 			//printk("New longest chain is %d with length %d\n", chain, thislen);
505*4882a593Smuzhiyun 			ChainLength = thislen;
506*4882a593Smuzhiyun 			LongestChain = chain;
507*4882a593Smuzhiyun 		}
508*4882a593Smuzhiyun 	}
509*4882a593Smuzhiyun 
510*4882a593Smuzhiyun 	if (ChainLength < 2) {
511*4882a593Smuzhiyun 		printk(KERN_WARNING "No Virtual Unit Chains available for folding. "
512*4882a593Smuzhiyun 		       "Failing request\n");
513*4882a593Smuzhiyun 		return BLOCK_NIL;
514*4882a593Smuzhiyun 	}
515*4882a593Smuzhiyun 
516*4882a593Smuzhiyun 	return NFTL_foldchain (nftl, LongestChain, pendingblock);
517*4882a593Smuzhiyun }
518*4882a593Smuzhiyun 
519*4882a593Smuzhiyun /* NFTL_findwriteunit: Return the unit number into which we can write
520*4882a593Smuzhiyun                        for this block. Make it available if it isn't already
521*4882a593Smuzhiyun */
NFTL_findwriteunit(struct NFTLrecord * nftl,unsigned block)522*4882a593Smuzhiyun static inline u16 NFTL_findwriteunit(struct NFTLrecord *nftl, unsigned block)
523*4882a593Smuzhiyun {
524*4882a593Smuzhiyun 	u16 lastEUN;
525*4882a593Smuzhiyun 	u16 thisVUC = block / (nftl->EraseSize / 512);
526*4882a593Smuzhiyun 	struct mtd_info *mtd = nftl->mbd.mtd;
527*4882a593Smuzhiyun 	unsigned int writeEUN;
528*4882a593Smuzhiyun 	unsigned long blockofs = (block * 512) & (nftl->EraseSize -1);
529*4882a593Smuzhiyun 	size_t retlen;
530*4882a593Smuzhiyun 	int silly, silly2 = 3;
531*4882a593Smuzhiyun 	struct nftl_oob oob;
532*4882a593Smuzhiyun 
533*4882a593Smuzhiyun 	do {
534*4882a593Smuzhiyun 		/* Scan the media to find a unit in the VUC which has
535*4882a593Smuzhiyun 		   a free space for the block in question.
536*4882a593Smuzhiyun 		*/
537*4882a593Smuzhiyun 
538*4882a593Smuzhiyun 		/* This condition catches the 0x[7f]fff cases, as well as
539*4882a593Smuzhiyun 		   being a sanity check for past-end-of-media access
540*4882a593Smuzhiyun 		*/
541*4882a593Smuzhiyun 		lastEUN = BLOCK_NIL;
542*4882a593Smuzhiyun 		writeEUN = nftl->EUNtable[thisVUC];
543*4882a593Smuzhiyun 		silly = MAX_LOOPS;
544*4882a593Smuzhiyun 		while (writeEUN <= nftl->lastEUN) {
545*4882a593Smuzhiyun 			struct nftl_bci bci;
546*4882a593Smuzhiyun 			size_t retlen;
547*4882a593Smuzhiyun 			unsigned int status;
548*4882a593Smuzhiyun 
549*4882a593Smuzhiyun 			lastEUN = writeEUN;
550*4882a593Smuzhiyun 
551*4882a593Smuzhiyun 			nftl_read_oob(mtd,
552*4882a593Smuzhiyun 				      (writeEUN * nftl->EraseSize) + blockofs,
553*4882a593Smuzhiyun 				      8, &retlen, (char *)&bci);
554*4882a593Smuzhiyun 
555*4882a593Smuzhiyun 			pr_debug("Status of block %d in EUN %d is %x\n",
556*4882a593Smuzhiyun 			      block , writeEUN, le16_to_cpu(bci.Status));
557*4882a593Smuzhiyun 
558*4882a593Smuzhiyun 			status = bci.Status | bci.Status1;
559*4882a593Smuzhiyun 			switch(status) {
560*4882a593Smuzhiyun 			case SECTOR_FREE:
561*4882a593Smuzhiyun 				return writeEUN;
562*4882a593Smuzhiyun 
563*4882a593Smuzhiyun 			case SECTOR_DELETED:
564*4882a593Smuzhiyun 			case SECTOR_USED:
565*4882a593Smuzhiyun 			case SECTOR_IGNORE:
566*4882a593Smuzhiyun 				break;
567*4882a593Smuzhiyun 			default:
568*4882a593Smuzhiyun 				// Invalid block. Don't use it any more. Must implement.
569*4882a593Smuzhiyun 				break;
570*4882a593Smuzhiyun 			}
571*4882a593Smuzhiyun 
572*4882a593Smuzhiyun 			if (!silly--) {
573*4882a593Smuzhiyun 				printk(KERN_WARNING
574*4882a593Smuzhiyun 				       "Infinite loop in Virtual Unit Chain 0x%x\n",
575*4882a593Smuzhiyun 				       thisVUC);
576*4882a593Smuzhiyun 				return BLOCK_NIL;
577*4882a593Smuzhiyun 			}
578*4882a593Smuzhiyun 
579*4882a593Smuzhiyun 			/* Skip to next block in chain */
580*4882a593Smuzhiyun 			writeEUN = nftl->ReplUnitTable[writeEUN];
581*4882a593Smuzhiyun 		}
582*4882a593Smuzhiyun 
583*4882a593Smuzhiyun 		/* OK. We didn't find one in the existing chain, or there
584*4882a593Smuzhiyun 		   is no existing chain. */
585*4882a593Smuzhiyun 
586*4882a593Smuzhiyun 		/* Try to find an already-free block */
587*4882a593Smuzhiyun 		writeEUN = NFTL_findfreeblock(nftl, 0);
588*4882a593Smuzhiyun 
589*4882a593Smuzhiyun 		if (writeEUN == BLOCK_NIL) {
590*4882a593Smuzhiyun 			/* That didn't work - there were no free blocks just
591*4882a593Smuzhiyun 			   waiting to be picked up. We're going to have to fold
592*4882a593Smuzhiyun 			   a chain to make room.
593*4882a593Smuzhiyun 			*/
594*4882a593Smuzhiyun 
595*4882a593Smuzhiyun 			/* First remember the start of this chain */
596*4882a593Smuzhiyun 			//u16 startEUN = nftl->EUNtable[thisVUC];
597*4882a593Smuzhiyun 
598*4882a593Smuzhiyun 			//printk("Write to VirtualUnitChain %d, calling makefreeblock()\n", thisVUC);
599*4882a593Smuzhiyun 			writeEUN = NFTL_makefreeblock(nftl, BLOCK_NIL);
600*4882a593Smuzhiyun 
601*4882a593Smuzhiyun 			if (writeEUN == BLOCK_NIL) {
602*4882a593Smuzhiyun 				/* OK, we accept that the above comment is
603*4882a593Smuzhiyun 				   lying - there may have been free blocks
604*4882a593Smuzhiyun 				   last time we called NFTL_findfreeblock(),
605*4882a593Smuzhiyun 				   but they are reserved for when we're
606*4882a593Smuzhiyun 				   desperate. Well, now we're desperate.
607*4882a593Smuzhiyun 				*/
608*4882a593Smuzhiyun 				pr_debug("Using desperate==1 to find free EUN to accommodate write to VUC %d\n", thisVUC);
609*4882a593Smuzhiyun 				writeEUN = NFTL_findfreeblock(nftl, 1);
610*4882a593Smuzhiyun 			}
611*4882a593Smuzhiyun 			if (writeEUN == BLOCK_NIL) {
612*4882a593Smuzhiyun 				/* Ouch. This should never happen - we should
613*4882a593Smuzhiyun 				   always be able to make some room somehow.
614*4882a593Smuzhiyun 				   If we get here, we've allocated more storage
615*4882a593Smuzhiyun 				   space than actual media, or our makefreeblock
616*4882a593Smuzhiyun 				   routine is missing something.
617*4882a593Smuzhiyun 				*/
618*4882a593Smuzhiyun 				printk(KERN_WARNING "Cannot make free space.\n");
619*4882a593Smuzhiyun 				return BLOCK_NIL;
620*4882a593Smuzhiyun 			}
621*4882a593Smuzhiyun 			//printk("Restarting scan\n");
622*4882a593Smuzhiyun 			lastEUN = BLOCK_NIL;
623*4882a593Smuzhiyun 			continue;
624*4882a593Smuzhiyun 		}
625*4882a593Smuzhiyun 
626*4882a593Smuzhiyun 		/* We've found a free block. Insert it into the chain. */
627*4882a593Smuzhiyun 
628*4882a593Smuzhiyun 		if (lastEUN != BLOCK_NIL) {
629*4882a593Smuzhiyun 			thisVUC |= 0x8000; /* It's a replacement block */
630*4882a593Smuzhiyun 		} else {
631*4882a593Smuzhiyun 			/* The first block in a new chain */
632*4882a593Smuzhiyun 			nftl->EUNtable[thisVUC] = writeEUN;
633*4882a593Smuzhiyun 		}
634*4882a593Smuzhiyun 
635*4882a593Smuzhiyun 		/* set up the actual EUN we're writing into */
636*4882a593Smuzhiyun 		/* Both in our cache... */
637*4882a593Smuzhiyun 		nftl->ReplUnitTable[writeEUN] = BLOCK_NIL;
638*4882a593Smuzhiyun 
639*4882a593Smuzhiyun 		/* ... and on the flash itself */
640*4882a593Smuzhiyun 		nftl_read_oob(mtd, writeEUN * nftl->EraseSize + 8, 8,
641*4882a593Smuzhiyun 			      &retlen, (char *)&oob.u);
642*4882a593Smuzhiyun 
643*4882a593Smuzhiyun 		oob.u.a.VirtUnitNum = oob.u.a.SpareVirtUnitNum = cpu_to_le16(thisVUC);
644*4882a593Smuzhiyun 
645*4882a593Smuzhiyun 		nftl_write_oob(mtd, writeEUN * nftl->EraseSize + 8, 8,
646*4882a593Smuzhiyun 			       &retlen, (char *)&oob.u);
647*4882a593Smuzhiyun 
648*4882a593Smuzhiyun 		/* we link the new block to the chain only after the
649*4882a593Smuzhiyun                    block is ready. It avoids the case where the chain
650*4882a593Smuzhiyun                    could point to a free block */
651*4882a593Smuzhiyun 		if (lastEUN != BLOCK_NIL) {
652*4882a593Smuzhiyun 			/* Both in our cache... */
653*4882a593Smuzhiyun 			nftl->ReplUnitTable[lastEUN] = writeEUN;
654*4882a593Smuzhiyun 			/* ... and on the flash itself */
655*4882a593Smuzhiyun 			nftl_read_oob(mtd, (lastEUN * nftl->EraseSize) + 8,
656*4882a593Smuzhiyun 				      8, &retlen, (char *)&oob.u);
657*4882a593Smuzhiyun 
658*4882a593Smuzhiyun 			oob.u.a.ReplUnitNum = oob.u.a.SpareReplUnitNum
659*4882a593Smuzhiyun 				= cpu_to_le16(writeEUN);
660*4882a593Smuzhiyun 
661*4882a593Smuzhiyun 			nftl_write_oob(mtd, (lastEUN * nftl->EraseSize) + 8,
662*4882a593Smuzhiyun 				       8, &retlen, (char *)&oob.u);
663*4882a593Smuzhiyun 		}
664*4882a593Smuzhiyun 
665*4882a593Smuzhiyun 		return writeEUN;
666*4882a593Smuzhiyun 
667*4882a593Smuzhiyun 	} while (silly2--);
668*4882a593Smuzhiyun 
669*4882a593Smuzhiyun 	printk(KERN_WARNING "Error folding to make room for Virtual Unit Chain 0x%x\n",
670*4882a593Smuzhiyun 	       thisVUC);
671*4882a593Smuzhiyun 	return BLOCK_NIL;
672*4882a593Smuzhiyun }
673*4882a593Smuzhiyun 
nftl_writeblock(struct mtd_blktrans_dev * mbd,unsigned long block,char * buffer)674*4882a593Smuzhiyun static int nftl_writeblock(struct mtd_blktrans_dev *mbd, unsigned long block,
675*4882a593Smuzhiyun 			   char *buffer)
676*4882a593Smuzhiyun {
677*4882a593Smuzhiyun 	struct NFTLrecord *nftl = (void *)mbd;
678*4882a593Smuzhiyun 	u16 writeEUN;
679*4882a593Smuzhiyun 	unsigned long blockofs = (block * 512) & (nftl->EraseSize - 1);
680*4882a593Smuzhiyun 	size_t retlen;
681*4882a593Smuzhiyun 	struct nftl_oob oob;
682*4882a593Smuzhiyun 
683*4882a593Smuzhiyun 	writeEUN = NFTL_findwriteunit(nftl, block);
684*4882a593Smuzhiyun 
685*4882a593Smuzhiyun 	if (writeEUN == BLOCK_NIL) {
686*4882a593Smuzhiyun 		printk(KERN_WARNING
687*4882a593Smuzhiyun 		       "NFTL_writeblock(): Cannot find block to write to\n");
688*4882a593Smuzhiyun 		/* If we _still_ haven't got a block to use, we're screwed */
689*4882a593Smuzhiyun 		return 1;
690*4882a593Smuzhiyun 	}
691*4882a593Smuzhiyun 
692*4882a593Smuzhiyun 	memset(&oob, 0xff, sizeof(struct nftl_oob));
693*4882a593Smuzhiyun 	oob.b.Status = oob.b.Status1 = SECTOR_USED;
694*4882a593Smuzhiyun 
695*4882a593Smuzhiyun 	nftl_write(nftl->mbd.mtd, (writeEUN * nftl->EraseSize) + blockofs,
696*4882a593Smuzhiyun 		   512, &retlen, (char *)buffer, (char *)&oob);
697*4882a593Smuzhiyun 	return 0;
698*4882a593Smuzhiyun }
699*4882a593Smuzhiyun #endif /* CONFIG_NFTL_RW */
700*4882a593Smuzhiyun 
nftl_readblock(struct mtd_blktrans_dev * mbd,unsigned long block,char * buffer)701*4882a593Smuzhiyun static int nftl_readblock(struct mtd_blktrans_dev *mbd, unsigned long block,
702*4882a593Smuzhiyun 			  char *buffer)
703*4882a593Smuzhiyun {
704*4882a593Smuzhiyun 	struct NFTLrecord *nftl = (void *)mbd;
705*4882a593Smuzhiyun 	struct mtd_info *mtd = nftl->mbd.mtd;
706*4882a593Smuzhiyun 	u16 lastgoodEUN;
707*4882a593Smuzhiyun 	u16 thisEUN = nftl->EUNtable[block / (nftl->EraseSize / 512)];
708*4882a593Smuzhiyun 	unsigned long blockofs = (block * 512) & (nftl->EraseSize - 1);
709*4882a593Smuzhiyun 	unsigned int status;
710*4882a593Smuzhiyun 	int silly = MAX_LOOPS;
711*4882a593Smuzhiyun 	size_t retlen;
712*4882a593Smuzhiyun 	struct nftl_bci bci;
713*4882a593Smuzhiyun 
714*4882a593Smuzhiyun 	lastgoodEUN = BLOCK_NIL;
715*4882a593Smuzhiyun 
716*4882a593Smuzhiyun 	if (thisEUN != BLOCK_NIL) {
717*4882a593Smuzhiyun 		while (thisEUN < nftl->nb_blocks) {
718*4882a593Smuzhiyun 			if (nftl_read_oob(mtd, (thisEUN * nftl->EraseSize) +
719*4882a593Smuzhiyun 					  blockofs, 8, &retlen,
720*4882a593Smuzhiyun 					  (char *)&bci) < 0)
721*4882a593Smuzhiyun 				status = SECTOR_IGNORE;
722*4882a593Smuzhiyun 			else
723*4882a593Smuzhiyun 				status = bci.Status | bci.Status1;
724*4882a593Smuzhiyun 
725*4882a593Smuzhiyun 			switch (status) {
726*4882a593Smuzhiyun 			case SECTOR_FREE:
727*4882a593Smuzhiyun 				/* no modification of a sector should follow a free sector */
728*4882a593Smuzhiyun 				goto the_end;
729*4882a593Smuzhiyun 			case SECTOR_DELETED:
730*4882a593Smuzhiyun 				lastgoodEUN = BLOCK_NIL;
731*4882a593Smuzhiyun 				break;
732*4882a593Smuzhiyun 			case SECTOR_USED:
733*4882a593Smuzhiyun 				lastgoodEUN = thisEUN;
734*4882a593Smuzhiyun 				break;
735*4882a593Smuzhiyun 			case SECTOR_IGNORE:
736*4882a593Smuzhiyun 				break;
737*4882a593Smuzhiyun 			default:
738*4882a593Smuzhiyun 				printk("Unknown status for block %ld in EUN %d: %x\n",
739*4882a593Smuzhiyun 				       block, thisEUN, status);
740*4882a593Smuzhiyun 				break;
741*4882a593Smuzhiyun 			}
742*4882a593Smuzhiyun 
743*4882a593Smuzhiyun 			if (!silly--) {
744*4882a593Smuzhiyun 				printk(KERN_WARNING "Infinite loop in Virtual Unit Chain 0x%lx\n",
745*4882a593Smuzhiyun 				       block / (nftl->EraseSize / 512));
746*4882a593Smuzhiyun 				return 1;
747*4882a593Smuzhiyun 			}
748*4882a593Smuzhiyun 			thisEUN = nftl->ReplUnitTable[thisEUN];
749*4882a593Smuzhiyun 		}
750*4882a593Smuzhiyun 	}
751*4882a593Smuzhiyun 
752*4882a593Smuzhiyun  the_end:
753*4882a593Smuzhiyun 	if (lastgoodEUN == BLOCK_NIL) {
754*4882a593Smuzhiyun 		/* the requested block is not on the media, return all 0x00 */
755*4882a593Smuzhiyun 		memset(buffer, 0, 512);
756*4882a593Smuzhiyun 	} else {
757*4882a593Smuzhiyun 		loff_t ptr = (lastgoodEUN * nftl->EraseSize) + blockofs;
758*4882a593Smuzhiyun 		size_t retlen;
759*4882a593Smuzhiyun 		int res = mtd_read(mtd, ptr, 512, &retlen, buffer);
760*4882a593Smuzhiyun 
761*4882a593Smuzhiyun 		if (res < 0 && !mtd_is_bitflip(res))
762*4882a593Smuzhiyun 			return -EIO;
763*4882a593Smuzhiyun 	}
764*4882a593Smuzhiyun 	return 0;
765*4882a593Smuzhiyun }
766*4882a593Smuzhiyun 
nftl_getgeo(struct mtd_blktrans_dev * dev,struct hd_geometry * geo)767*4882a593Smuzhiyun static int nftl_getgeo(struct mtd_blktrans_dev *dev,  struct hd_geometry *geo)
768*4882a593Smuzhiyun {
769*4882a593Smuzhiyun 	struct NFTLrecord *nftl = (void *)dev;
770*4882a593Smuzhiyun 
771*4882a593Smuzhiyun 	geo->heads = nftl->heads;
772*4882a593Smuzhiyun 	geo->sectors = nftl->sectors;
773*4882a593Smuzhiyun 	geo->cylinders = nftl->cylinders;
774*4882a593Smuzhiyun 
775*4882a593Smuzhiyun 	return 0;
776*4882a593Smuzhiyun }
777*4882a593Smuzhiyun 
778*4882a593Smuzhiyun /****************************************************************************
779*4882a593Smuzhiyun  *
780*4882a593Smuzhiyun  * Module stuff
781*4882a593Smuzhiyun  *
782*4882a593Smuzhiyun  ****************************************************************************/
783*4882a593Smuzhiyun 
784*4882a593Smuzhiyun 
785*4882a593Smuzhiyun static struct mtd_blktrans_ops nftl_tr = {
786*4882a593Smuzhiyun 	.name		= "nftl",
787*4882a593Smuzhiyun 	.major		= NFTL_MAJOR,
788*4882a593Smuzhiyun 	.part_bits	= NFTL_PARTN_BITS,
789*4882a593Smuzhiyun 	.blksize 	= 512,
790*4882a593Smuzhiyun 	.getgeo		= nftl_getgeo,
791*4882a593Smuzhiyun 	.readsect	= nftl_readblock,
792*4882a593Smuzhiyun #ifdef CONFIG_NFTL_RW
793*4882a593Smuzhiyun 	.writesect	= nftl_writeblock,
794*4882a593Smuzhiyun #endif
795*4882a593Smuzhiyun 	.add_mtd	= nftl_add_mtd,
796*4882a593Smuzhiyun 	.remove_dev	= nftl_remove_dev,
797*4882a593Smuzhiyun 	.owner		= THIS_MODULE,
798*4882a593Smuzhiyun };
799*4882a593Smuzhiyun 
init_nftl(void)800*4882a593Smuzhiyun static int __init init_nftl(void)
801*4882a593Smuzhiyun {
802*4882a593Smuzhiyun 	return register_mtd_blktrans(&nftl_tr);
803*4882a593Smuzhiyun }
804*4882a593Smuzhiyun 
cleanup_nftl(void)805*4882a593Smuzhiyun static void __exit cleanup_nftl(void)
806*4882a593Smuzhiyun {
807*4882a593Smuzhiyun 	deregister_mtd_blktrans(&nftl_tr);
808*4882a593Smuzhiyun }
809*4882a593Smuzhiyun 
810*4882a593Smuzhiyun module_init(init_nftl);
811*4882a593Smuzhiyun module_exit(cleanup_nftl);
812*4882a593Smuzhiyun 
813*4882a593Smuzhiyun MODULE_LICENSE("GPL");
814*4882a593Smuzhiyun MODULE_AUTHOR("David Woodhouse <dwmw2@infradead.org>, Fabrice Bellard <fabrice.bellard@netgem.com> et al.");
815*4882a593Smuzhiyun MODULE_DESCRIPTION("Support code for NAND Flash Translation Layer, used on M-Systems DiskOnChip 2000 and Millennium");
816*4882a593Smuzhiyun MODULE_ALIAS_BLOCKDEV_MAJOR(NFTL_MAJOR);
817