1 /* Linux driver for NAND Flash Translation Layer */
2 /* (c) 1999 Machine Vision Holdings, Inc. */
3 /* Author: David Woodhouse <dwmw2@infradead.org> */
4 /* $Id: nftlcore.c,v 1.98 2005/11/07 11:14:21 gleixner Exp $ */
7 The contents of this file are distributed under the GNU General
8 Public License version 2. The author places no additional
9 restrictions of any kind on it.
14 #include <linux/config.h>
15 #include <linux/kernel.h>
16 #include <linux/module.h>
17 #include <asm/errno.h>
19 #include <asm/uaccess.h>
20 #include <linux/miscdevice.h>
21 #include <linux/pci.h>
22 #include <linux/delay.h>
23 #include <linux/slab.h>
24 #include <linux/sched.h>
25 #include <linux/init.h>
26 #include <linux/hdreg.h>
28 #include <linux/kmod.h>
29 #include <linux/mtd/mtd.h>
30 #include <linux/mtd/nand.h>
31 #include <linux/mtd/nftl.h>
32 #include <linux/mtd/blktrans.h>
34 /* maximum number of loops while examining next block, to have a
35 chance to detect consistency problems (they should never happen
36 because of the checks done in the mounting */
38 #define MAX_LOOPS 10000
41 static void nftl_add_mtd(struct mtd_blktrans_ops *tr, struct mtd_info *mtd)
43 struct NFTLrecord *nftl;
46 if (mtd->type != MTD_NANDFLASH)
48 /* OK, this is moderately ugly. But probably safe. Alternatives? */
49 if (memcmp(mtd->name, "DiskOnChip", 10))
52 if (!mtd->block_isbad) {
54 "NFTL no longer supports the old DiskOnChip drivers loaded via docprobe.\n"
55 "Please use the new diskonchip driver under the NAND subsystem.\n");
59 DEBUG(MTD_DEBUG_LEVEL1, "NFTL: add_mtd for %s\n", mtd->name);
61 nftl = kmalloc(sizeof(struct NFTLrecord), GFP_KERNEL);
64 printk(KERN_WARNING "NFTL: out of memory for data structures\n");
67 memset(nftl, 0, sizeof(*nftl));
70 nftl->mbd.devnum = -1;
71 nftl->mbd.blksize = 512;
74 if (NFTL_mount(nftl) < 0) {
75 printk(KERN_WARNING "NFTL: could not mount device\n");
80 /* OK, it's a new one. Set up all the data structures. */
82 /* Calculate geometry */
83 nftl->cylinders = 1024;
86 temp = nftl->cylinders * nftl->heads;
87 nftl->sectors = nftl->mbd.size / temp;
88 if (nftl->mbd.size % temp) {
90 temp = nftl->cylinders * nftl->sectors;
91 nftl->heads = nftl->mbd.size / temp;
93 if (nftl->mbd.size % temp) {
95 temp = nftl->heads * nftl->sectors;
96 nftl->cylinders = nftl->mbd.size / temp;
100 if (nftl->mbd.size != nftl->heads * nftl->cylinders * nftl->sectors) {
103 mbd.size == heads * cylinders * sectors
105 printk(KERN_WARNING "NFTL: cannot calculate a geometry to "
106 "match size of 0x%lx.\n", nftl->mbd.size);
107 printk(KERN_WARNING "NFTL: using C:%d H:%d S:%d "
108 "(== 0x%lx sects)\n",
109 nftl->cylinders, nftl->heads , nftl->sectors,
110 (long)nftl->cylinders * (long)nftl->heads *
111 (long)nftl->sectors );
114 if (add_mtd_blktrans_dev(&nftl->mbd)) {
115 kfree(nftl->ReplUnitTable);
116 kfree(nftl->EUNtable);
121 printk(KERN_INFO "NFTL: Found new nftl%c\n", nftl->mbd.devnum + 'a');
125 static void nftl_remove_dev(struct mtd_blktrans_dev *dev)
127 struct NFTLrecord *nftl = (void *)dev;
129 DEBUG(MTD_DEBUG_LEVEL1, "NFTL: remove_dev (i=%d)\n", dev->devnum);
131 del_mtd_blktrans_dev(dev);
132 kfree(nftl->ReplUnitTable);
133 kfree(nftl->EUNtable);
137 #ifdef CONFIG_NFTL_RW
139 /* Actual NFTL access routines */
140 /* NFTL_findfreeblock: Find a free Erase Unit on the NFTL partition. This function is used
141 * when the give Virtual Unit Chain
143 static u16 NFTL_findfreeblock(struct NFTLrecord *nftl, int desperate )
145 /* For a given Virtual Unit Chain: find or create a free block and
146 add it to the chain */
147 /* We're passed the number of the last EUN in the chain, to save us from
148 having to look it up again */
149 u16 pot = nftl->LastFreeEUN;
150 int silly = nftl->nb_blocks;
152 /* Normally, we force a fold to happen before we run out of free blocks completely */
153 if (!desperate && nftl->numfreeEUNs < 2) {
154 DEBUG(MTD_DEBUG_LEVEL1, "NFTL_findfreeblock: there are too few free EUNs\n");
158 /* Scan for a free block */
160 if (nftl->ReplUnitTable[pot] == BLOCK_FREE) {
161 nftl->LastFreeEUN = pot;
166 /* This will probably point to the MediaHdr unit itself,
167 right at the beginning of the partition. But that unit
168 (and the backup unit too) should have the UCI set
169 up so that it's not selected for overwriting */
170 if (++pot > nftl->lastEUN)
171 pot = le16_to_cpu(nftl->MediaHdr.FirstPhysicalEUN);
174 printk("Argh! No free blocks found! LastFreeEUN = %d, "
175 "FirstEUN = %d\n", nftl->LastFreeEUN,
176 le16_to_cpu(nftl->MediaHdr.FirstPhysicalEUN));
179 } while (pot != nftl->LastFreeEUN);
184 static u16 NFTL_foldchain (struct NFTLrecord *nftl, unsigned thisVUC, unsigned pendingblock )
186 struct mtd_info *mtd = nftl->mbd.mtd;
187 u16 BlockMap[MAX_SECTORS_PER_UNIT];
188 unsigned char BlockLastState[MAX_SECTORS_PER_UNIT];
189 unsigned char BlockFreeFound[MAX_SECTORS_PER_UNIT];
190 unsigned int thisEUN;
193 unsigned int targetEUN;
198 memset(BlockMap, 0xff, sizeof(BlockMap));
199 memset(BlockFreeFound, 0, sizeof(BlockFreeFound));
201 thisEUN = nftl->EUNtable[thisVUC];
203 if (thisEUN == BLOCK_NIL) {
204 printk(KERN_WARNING "Trying to fold non-existent "
205 "Virtual Unit Chain %d!\n", thisVUC);
209 /* Scan to find the Erase Unit which holds the actual data for each
210 512-byte block within the Chain.
213 targetEUN = BLOCK_NIL;
214 while (thisEUN <= nftl->lastEUN ) {
215 unsigned int status, foldmark;
218 for (block = 0; block < nftl->EraseSize / 512; block ++) {
219 mtd->read_oob(mtd, (thisEUN * nftl->EraseSize) +
220 (block * 512), 16 , &retlen,
223 foldmark = oob.u.c.FoldMark | oob.u.c.FoldMark1;
224 if (foldmark == FOLD_MARK_IN_PROGRESS) {
225 DEBUG(MTD_DEBUG_LEVEL1,
226 "Write Inhibited on EUN %d\n", thisEUN);
229 /* There's no other reason not to do inplace,
230 except ones that come later. So we don't need
231 to preserve inplace */
235 status = oob.b.Status | oob.b.Status1;
236 BlockLastState[block] = status;
240 BlockFreeFound[block] = 1;
244 if (!BlockFreeFound[block])
245 BlockMap[block] = thisEUN;
248 "SECTOR_USED found after SECTOR_FREE "
249 "in Virtual Unit Chain %d for block %d\n",
253 if (!BlockFreeFound[block])
254 BlockMap[block] = BLOCK_NIL;
257 "SECTOR_DELETED found after SECTOR_FREE "
258 "in Virtual Unit Chain %d for block %d\n",
265 printk("Unknown status for block %d in EUN %d: %x\n",
266 block, thisEUN, status);
271 printk(KERN_WARNING "Infinite loop in Virtual Unit Chain 0x%x\n",
276 thisEUN = nftl->ReplUnitTable[thisEUN];
280 /* We're being asked to be a fold-in-place. Check
281 that all blocks which actually have data associated
282 with them (i.e. BlockMap[block] != BLOCK_NIL) are
283 either already present or SECTOR_FREE in the target
284 block. If not, we're going to have to fold out-of-place
287 for (block = 0; block < nftl->EraseSize / 512 ; block++) {
288 if (BlockLastState[block] != SECTOR_FREE &&
289 BlockMap[block] != BLOCK_NIL &&
290 BlockMap[block] != targetEUN) {
291 DEBUG(MTD_DEBUG_LEVEL1, "Setting inplace to 0. VUC %d, "
292 "block %d was %x lastEUN, "
293 "and is in EUN %d (%s) %d\n",
294 thisVUC, block, BlockLastState[block],
296 BlockMap[block]== targetEUN ? "==" : "!=",
303 if (pendingblock >= (thisVUC * (nftl->EraseSize / 512)) &&
304 pendingblock < ((thisVUC + 1)* (nftl->EraseSize / 512)) &&
305 BlockLastState[pendingblock - (thisVUC * (nftl->EraseSize / 512))] !=
307 DEBUG(MTD_DEBUG_LEVEL1, "Pending write not free in EUN %d. "
308 "Folding out of place.\n", targetEUN);
314 DEBUG(MTD_DEBUG_LEVEL1, "Cannot fold Virtual Unit Chain %d in place. "
315 "Trying out-of-place\n", thisVUC);
316 /* We need to find a targetEUN to fold into. */
317 targetEUN = NFTL_findfreeblock(nftl, 1);
318 if (targetEUN == BLOCK_NIL) {
319 /* Ouch. Now we're screwed. We need to do a
320 fold-in-place of another chain to make room
321 for this one. We need a better way of selecting
322 which chain to fold, because makefreeblock will
323 only ask us to fold the same one again.
326 "NFTL_findfreeblock(desperate) returns 0xffff.\n");
330 /* We put a fold mark in the chain we are folding only if we
331 fold in place to help the mount check code. If we do not fold in
332 place, it is possible to find the valid chain by selecting the
334 oob.u.c.FoldMark = oob.u.c.FoldMark1 = cpu_to_le16(FOLD_MARK_IN_PROGRESS);
335 oob.u.c.unused = 0xffffffff;
336 mtd->write_oob(mtd, (nftl->EraseSize * targetEUN) + 2 * 512 + 8,
337 8, &retlen, (char *)&oob.u);
340 /* OK. We now know the location of every block in the Virtual Unit Chain,
341 and the Erase Unit into which we are supposed to be copying.
344 DEBUG(MTD_DEBUG_LEVEL1,"Folding chain %d into unit %d\n", thisVUC, targetEUN);
345 for (block = 0; block < nftl->EraseSize / 512 ; block++) {
346 unsigned char movebuf[512];
349 /* If it's in the target EUN already, or if it's pending write, do nothing */
350 if (BlockMap[block] == targetEUN ||
351 (pendingblock == (thisVUC * (nftl->EraseSize / 512) + block))) {
355 /* copy only in non free block (free blocks can only
356 happen in case of media errors or deleted blocks) */
357 if (BlockMap[block] == BLOCK_NIL)
360 ret = mtd->read(mtd, (nftl->EraseSize * BlockMap[block]) + (block * 512),
361 512, &retlen, movebuf);
363 ret = mtd->read(mtd, (nftl->EraseSize * BlockMap[block])
364 + (block * 512), 512, &retlen,
367 printk("Error went away on retry.\n");
369 memset(&oob, 0xff, sizeof(struct nftl_oob));
370 oob.b.Status = oob.b.Status1 = SECTOR_USED;
372 nand_write_raw(nftl->mbd.mtd, (nftl->EraseSize * targetEUN) +
373 (block * 512), 512, &retlen, movebuf,
378 /* add the header so that it is now a valid chain */
379 oob.u.a.VirtUnitNum = oob.u.a.SpareVirtUnitNum = cpu_to_le16(thisVUC);
380 oob.u.a.ReplUnitNum = oob.u.a.SpareReplUnitNum = 0xffff;
382 mtd->write_oob(mtd, (nftl->EraseSize * targetEUN) + 8,
383 8, &retlen, (char *)&oob.u);
385 /* OK. We've moved the whole lot into the new block. Now we have to free the original blocks. */
387 /* At this point, we have two different chains for this Virtual Unit, and no way to tell
388 them apart. If we crash now, we get confused. However, both contain the same data, so we
389 shouldn't actually lose data in this case. It's just that when we load up on a medium which
390 has duplicate chains, we need to free one of the chains because it's not necessary any more.
392 thisEUN = nftl->EUNtable[thisVUC];
393 DEBUG(MTD_DEBUG_LEVEL1,"Want to erase\n");
395 /* For each block in the old chain (except the targetEUN of course),
396 free it and make it available for future use */
397 while (thisEUN <= nftl->lastEUN && thisEUN != targetEUN) {
400 EUNtmp = nftl->ReplUnitTable[thisEUN];
402 if (NFTL_formatblock(nftl, thisEUN) < 0) {
403 /* could not erase : mark block as reserved
405 nftl->ReplUnitTable[thisEUN] = BLOCK_RESERVED;
407 /* correctly erased : mark it as free */
408 nftl->ReplUnitTable[thisEUN] = BLOCK_FREE;
414 /* Make this the new start of chain for thisVUC */
415 nftl->ReplUnitTable[targetEUN] = BLOCK_NIL;
416 nftl->EUNtable[thisVUC] = targetEUN;
421 static u16 NFTL_makefreeblock( struct NFTLrecord *nftl , unsigned pendingblock)
423 /* This is the part that needs some cleverness applied.
424 For now, I'm doing the minimum applicable to actually
425 get the thing to work.
426 Wear-levelling and other clever stuff needs to be implemented
427 and we also need to do some assessment of the results when
428 the system loses power half-way through the routine.
430 u16 LongestChain = 0;
431 u16 ChainLength = 0, thislen;
434 for (chain = 0; chain < le32_to_cpu(nftl->MediaHdr.FormattedSize) / nftl->EraseSize; chain++) {
435 EUN = nftl->EUNtable[chain];
438 while (EUN <= nftl->lastEUN) {
440 //printk("VUC %d reaches len %d with EUN %d\n", chain, thislen, EUN);
441 EUN = nftl->ReplUnitTable[EUN] & 0x7fff;
442 if (thislen > 0xff00) {
443 printk("Endless loop in Virtual Chain %d: Unit %x\n",
446 if (thislen > 0xff10) {
447 /* Actually, don't return failure. Just ignore this chain and
454 if (thislen > ChainLength) {
455 //printk("New longest chain is %d with length %d\n", chain, thislen);
456 ChainLength = thislen;
457 LongestChain = chain;
461 if (ChainLength < 2) {
462 printk(KERN_WARNING "No Virtual Unit Chains available for folding. "
463 "Failing request\n");
467 return NFTL_foldchain (nftl, LongestChain, pendingblock);
470 /* NFTL_findwriteunit: Return the unit number into which we can write
471 for this block. Make it available if it isn't already
473 static inline u16 NFTL_findwriteunit(struct NFTLrecord *nftl, unsigned block)
476 u16 thisVUC = block / (nftl->EraseSize / 512);
477 struct mtd_info *mtd = nftl->mbd.mtd;
478 unsigned int writeEUN;
479 unsigned long blockofs = (block * 512) & (nftl->EraseSize -1);
481 int silly, silly2 = 3;
485 /* Scan the media to find a unit in the VUC which has
486 a free space for the block in question.
489 /* This condition catches the 0x[7f]fff cases, as well as
490 being a sanity check for past-end-of-media access
493 writeEUN = nftl->EUNtable[thisVUC];
495 while (writeEUN <= nftl->lastEUN) {
503 (writeEUN * nftl->EraseSize) + blockofs,
504 8, &retlen, (char *)&bci);
506 DEBUG(MTD_DEBUG_LEVEL2, "Status of block %d in EUN %d is %x\n",
507 block , writeEUN, le16_to_cpu(bci.Status));
509 status = bci.Status | bci.Status1;
519 // Invalid block. Don't use it any more. Must implement.
525 "Infinite loop in Virtual Unit Chain 0x%x\n",
530 /* Skip to next block in chain */
531 writeEUN = nftl->ReplUnitTable[writeEUN];
534 /* OK. We didn't find one in the existing chain, or there
535 is no existing chain. */
537 /* Try to find an already-free block */
538 writeEUN = NFTL_findfreeblock(nftl, 0);
540 if (writeEUN == BLOCK_NIL) {
541 /* That didn't work - there were no free blocks just
542 waiting to be picked up. We're going to have to fold
543 a chain to make room.
546 /* First remember the start of this chain */
547 //u16 startEUN = nftl->EUNtable[thisVUC];
549 //printk("Write to VirtualUnitChain %d, calling makefreeblock()\n", thisVUC);
550 writeEUN = NFTL_makefreeblock(nftl, 0xffff);
552 if (writeEUN == BLOCK_NIL) {
553 /* OK, we accept that the above comment is
554 lying - there may have been free blocks
555 last time we called NFTL_findfreeblock(),
556 but they are reserved for when we're
557 desperate. Well, now we're desperate.
559 DEBUG(MTD_DEBUG_LEVEL1, "Using desperate==1 to find free EUN to accommodate write to VUC %d\n", thisVUC);
560 writeEUN = NFTL_findfreeblock(nftl, 1);
562 if (writeEUN == BLOCK_NIL) {
563 /* Ouch. This should never happen - we should
564 always be able to make some room somehow.
565 If we get here, we've allocated more storage
566 space than actual media, or our makefreeblock
567 routine is missing something.
569 printk(KERN_WARNING "Cannot make free space.\n");
572 //printk("Restarting scan\n");
577 /* We've found a free block. Insert it into the chain. */
579 if (lastEUN != BLOCK_NIL) {
580 thisVUC |= 0x8000; /* It's a replacement block */
582 /* The first block in a new chain */
583 nftl->EUNtable[thisVUC] = writeEUN;
586 /* set up the actual EUN we're writing into */
587 /* Both in our cache... */
588 nftl->ReplUnitTable[writeEUN] = BLOCK_NIL;
590 /* ... and on the flash itself */
591 mtd->read_oob(mtd, writeEUN * nftl->EraseSize + 8, 8,
592 &retlen, (char *)&oob.u);
594 oob.u.a.VirtUnitNum = oob.u.a.SpareVirtUnitNum = cpu_to_le16(thisVUC);
596 mtd->write_oob(mtd, writeEUN * nftl->EraseSize + 8, 8,
597 &retlen, (char *)&oob.u);
599 /* we link the new block to the chain only after the
600 block is ready. It avoids the case where the chain
601 could point to a free block */
602 if (lastEUN != BLOCK_NIL) {
603 /* Both in our cache... */
604 nftl->ReplUnitTable[lastEUN] = writeEUN;
605 /* ... and on the flash itself */
606 mtd->read_oob(mtd, (lastEUN * nftl->EraseSize) + 8,
607 8, &retlen, (char *)&oob.u);
609 oob.u.a.ReplUnitNum = oob.u.a.SpareReplUnitNum
610 = cpu_to_le16(writeEUN);
612 mtd->write_oob(mtd, (lastEUN * nftl->EraseSize) + 8,
613 8, &retlen, (char *)&oob.u);
620 printk(KERN_WARNING "Error folding to make room for Virtual Unit Chain 0x%x\n",
625 static int nftl_writeblock(struct mtd_blktrans_dev *mbd, unsigned long block,
628 struct NFTLrecord *nftl = (void *)mbd;
630 unsigned long blockofs = (block * 512) & (nftl->EraseSize - 1);
634 writeEUN = NFTL_findwriteunit(nftl, block);
636 if (writeEUN == BLOCK_NIL) {
638 "NFTL_writeblock(): Cannot find block to write to\n");
639 /* If we _still_ haven't got a block to use, we're screwed */
643 memset(&oob, 0xff, sizeof(struct nftl_oob));
644 oob.b.Status = oob.b.Status1 = SECTOR_USED;
646 nand_write_raw(nftl->mbd.mtd, (writeEUN * nftl->EraseSize) +
647 blockofs, 512, &retlen, (char *)buffer,
651 #endif /* CONFIG_NFTL_RW */
653 static int nftl_readblock(struct mtd_blktrans_dev *mbd, unsigned long block,
656 struct NFTLrecord *nftl = (void *)mbd;
657 struct mtd_info *mtd = nftl->mbd.mtd;
659 u16 thisEUN = nftl->EUNtable[block / (nftl->EraseSize / 512)];
660 unsigned long blockofs = (block * 512) & (nftl->EraseSize - 1);
662 int silly = MAX_LOOPS;
666 lastgoodEUN = BLOCK_NIL;
668 if (thisEUN != BLOCK_NIL) {
669 while (thisEUN < nftl->nb_blocks) {
670 if (mtd->read_oob(mtd, (thisEUN * nftl->EraseSize) +
671 blockofs, 8, &retlen,
673 status = SECTOR_IGNORE;
675 status = bci.Status | bci.Status1;
679 /* no modification of a sector should follow a free sector */
682 lastgoodEUN = BLOCK_NIL;
685 lastgoodEUN = thisEUN;
690 printk("Unknown status for block %ld in EUN %d: %x\n",
691 block, thisEUN, status);
696 printk(KERN_WARNING "Infinite loop in Virtual Unit Chain 0x%lx\n",
697 block / (nftl->EraseSize / 512));
700 thisEUN = nftl->ReplUnitTable[thisEUN];
705 if (lastgoodEUN == BLOCK_NIL) {
706 /* the requested block is not on the media, return all 0x00 */
707 memset(buffer, 0, 512);
709 loff_t ptr = (lastgoodEUN * nftl->EraseSize) + blockofs;
711 if (mtd->read(mtd, ptr, 512, &retlen, buffer))
717 static int nftl_getgeo(struct mtd_blktrans_dev *dev, struct hd_geometry *geo)
719 struct NFTLrecord *nftl = (void *)dev;
721 geo->heads = nftl->heads;
722 geo->sectors = nftl->sectors;
723 geo->cylinders = nftl->cylinders;
728 /****************************************************************************
732 ****************************************************************************/
735 static struct mtd_blktrans_ops nftl_tr = {
738 .part_bits = NFTL_PARTN_BITS,
739 .getgeo = nftl_getgeo,
740 .readsect = nftl_readblock,
741 #ifdef CONFIG_NFTL_RW
742 .writesect = nftl_writeblock,
744 .add_mtd = nftl_add_mtd,
745 .remove_dev = nftl_remove_dev,
746 .owner = THIS_MODULE,
749 extern char nftlmountrev[];
751 static int __init init_nftl(void)
753 printk(KERN_INFO "NFTL driver: nftlcore.c $Revision: 1.98 $, nftlmount.c %s\n", nftlmountrev);
755 return register_mtd_blktrans(&nftl_tr);
758 static void __exit cleanup_nftl(void)
760 deregister_mtd_blktrans(&nftl_tr);
763 module_init(init_nftl);
764 module_exit(cleanup_nftl);
766 MODULE_LICENSE("GPL");
767 MODULE_AUTHOR("David Woodhouse <dwmw2@infradead.org>, Fabrice Bellard <fabrice.bellard@netgem.com> et al.");
768 MODULE_DESCRIPTION("Support code for NAND Flash Translation Layer, used on M-Systems DiskOnChip 2000 and Millennium");