25 #include <linux/kernel.h>
26 #include <linux/module.h>
28 #include <linux/slab.h>
29 #include <linux/sched.h>
37 #include <asm/uaccess.h>
38 #include <asm/errno.h>
46 #define MAX_LOOPS 10000
50 struct INFTLrecord *inftl;
61 "INFTL no longer supports the old DiskOnChip drivers loaded via docprobe.\n"
62 "Please use the new diskonchip driver under the NAND subsystem.\n");
74 inftl->mbd.devnum = -1;
87 inftl->cylinders = 1024;
90 temp = inftl->cylinders * inftl->heads;
91 inftl->sectors = inftl->mbd.size /
temp;
92 if (inftl->mbd.size % temp) {
94 temp = inftl->cylinders * inftl->sectors;
95 inftl->heads = inftl->mbd.size /
temp;
97 if (inftl->mbd.size % temp) {
99 temp = inftl->heads * inftl->sectors;
100 inftl->cylinders = inftl->mbd.size /
temp;
104 if (inftl->mbd.size != inftl->heads * inftl->cylinders * inftl->sectors) {
110 "match size of 0x%lx.\n", inftl->mbd.size);
112 "(== 0x%lx sects)\n",
113 inftl->cylinders, inftl->heads , inftl->sectors,
114 (
long)inftl->cylinders * (
long)inftl->heads *
115 (
long)inftl->sectors );
119 kfree(inftl->PUtable);
120 kfree(inftl->VUtable);
125 printk(
KERN_INFO "INFTL: Found new inftl%c\n", inftl->mbd.devnum +
'a');
132 struct INFTLrecord *inftl = (
void *)dev;
138 kfree(inftl->PUtable);
139 kfree(inftl->VUtable);
181 res = mtd_write_oob(mtd, offs & ~(mtd->
writesize - 1), &ops);
202 res = mtd_write_oob(mtd, offs & ~(mtd->
writesize - 1), &ops);
203 *retlen =
ops.retlen;
211 static u16 INFTL_findfreeblock(
struct INFTLrecord *inftl,
int desperate)
213 u16 pot = inftl->LastFreeEUN;
214 int silly = inftl->nb_blocks;
216 pr_debug(
"INFTL: INFTL_findfreeblock(inftl=%p,desperate=%d)\n",
223 if (!desperate && inftl->numfreeEUNs < 2) {
224 pr_debug(
"INFTL: there are too few free EUNs (%d)\n",
232 inftl->LastFreeEUN = pot;
236 if (++pot > inftl->lastEUN)
241 "EUN range = %d - %d\n", 0, inftl->LastFreeEUN);
244 }
while (pot != inftl->LastFreeEUN);
249 static u16 INFTL_foldchain(
struct INFTLrecord *inftl,
unsigned thisVUC,
unsigned pendingblock)
253 unsigned int thisEUN, prevEUN,
status;
254 struct mtd_info *mtd = inftl->mbd.mtd;
256 unsigned int targetEUN;
260 pr_debug(
"INFTL: INFTL_foldchain(inftl=%p,thisVUC=%d,pending=%d)\n",
261 inftl, thisVUC, pendingblock);
263 memset(BlockMap, 0xff,
sizeof(BlockMap));
264 memset(BlockDeleted, 0,
sizeof(BlockDeleted));
266 thisEUN = targetEUN = inftl->VUtable[thisVUC];
270 "Virtual Unit Chain %d!\n", thisVUC);
279 while (thisEUN < inftl->nb_blocks) {
280 for (block = 0; block < inftl->EraseSize/
SECTORSIZE; block ++) {
290 status = oob.b.Status | oob.b.Status1;
297 BlockMap[
block] = thisEUN;
300 BlockDeleted[
block] = 1;
304 "for block %d in EUN %d: %x\n",
305 block, thisEUN, status);
312 "Unit Chain 0x%x\n", thisVUC);
316 thisEUN = inftl->PUtable[thisEUN];
324 pr_debug(
"INFTL: folding chain %d into unit %d\n", thisVUC, targetEUN);
326 for (block = 0; block < inftl->EraseSize/
SECTORSIZE ; block++) {
334 if (BlockMap[block] == targetEUN || (pendingblock ==
335 (thisVUC * (inftl->EraseSize / SECTORSIZE) + block))) {
347 (inftl->EraseSize * BlockMap[block]) + (block * SECTORSIZE),
351 if (ret < 0 && !mtd_is_bitflip(ret)) {
353 (inftl->EraseSize * BlockMap[block]) + (block * SECTORSIZE),
358 pr_debug(
"INFTL: error went away on retry?\n");
363 inftl_write(inftl->mbd.mtd, (inftl->EraseSize * targetEUN) +
364 (block * SECTORSIZE), SECTORSIZE, &retlen,
365 movebuf, (
char *)&oob);
374 pr_debug(
"INFTL: want to erase virtual chain %d\n", thisVUC);
378 thisEUN = inftl->VUtable[thisVUC];
380 while (inftl->PUtable[thisEUN] !=
BLOCK_NIL) {
382 thisEUN = inftl->PUtable[thisEUN];
386 if (thisEUN == targetEUN)
401 inftl->numfreeEUNs++;
408 static u16 INFTL_makefreeblock(
struct INFTLrecord *inftl,
unsigned pendingblock)
418 u16 LongestChain = 0;
419 u16 ChainLength = 0, thislen;
422 pr_debug(
"INFTL: INFTL_makefreeblock(inftl=%p,"
423 "pending=%d)\n", inftl, pendingblock);
425 for (chain = 0; chain < inftl->nb_blocks; chain++) {
426 EUN = inftl->VUtable[
chain];
429 while (EUN <= inftl->lastEUN) {
431 EUN = inftl->PUtable[EUN];
432 if (thislen > 0xff00) {
434 "Virtual Chain %d: Unit %x\n",
445 if (thislen > ChainLength) {
446 ChainLength = thislen;
447 LongestChain =
chain;
451 if (ChainLength < 2) {
453 "for folding. Failing request\n");
457 return INFTL_foldchain(inftl, LongestChain, pendingblock);
460 static int nrbits(
unsigned int val,
int bitcount)
464 for (i = 0; (i < bitcount); i++)
465 total += (((0x1 << i) & val) ? 1 : 0);
473 static inline u16 INFTL_findwriteunit(
struct INFTLrecord *inftl,
unsigned block)
475 unsigned int thisVUC = block / (inftl->EraseSize /
SECTORSIZE);
476 unsigned int thisEUN, writeEUN, prev_block,
status;
477 unsigned long blockofs = (block *
SECTORSIZE) & (inftl->EraseSize -1);
478 struct mtd_info *mtd = inftl->mbd.mtd;
481 unsigned char anac, nacs,
parity;
483 int silly, silly2 = 3;
485 pr_debug(
"INFTL: INFTL_findwriteunit(inftl=%p,block=%d)\n",
494 thisEUN = inftl->VUtable[thisVUC];
497 while (thisEUN <= inftl->lastEUN) {
499 blockofs, 8, &retlen, (
char *)&bci);
501 status = bci.Status | bci.Status1;
502 pr_debug(
"INFTL: status of block %d in EUN %d is %x\n",
503 block , writeEUN, status);
525 "Virtual Unit Chain 0x%x\n", thisVUC);
530 thisEUN = inftl->PUtable[thisEUN];
542 writeEUN = INFTL_findfreeblock(inftl, 0);
550 thisEUN = INFTL_makefreeblock(inftl, block);
556 pr_debug(
"INFTL: using desperate==1 to find free EUN "
557 "to accommodate write to VUC %d\n",
559 writeEUN = INFTL_findfreeblock(inftl, 1);
584 thisEUN = inftl->VUtable[thisVUC];
587 + 8, 8, &retlen, (
char *)&oob.u);
588 anac = oob.u.a.ANAC + 1;
589 nacs = oob.u.a.NACs + 1;
592 prev_block = inftl->VUtable[thisVUC];
593 if (prev_block < inftl->nb_blocks)
594 prev_block -= inftl->firstEUN;
596 parity = (nrbits(thisVUC, 16) & 0x1) ? 0x1 : 0;
597 parity |= (nrbits(prev_block, 16) & 0x1) ? 0x2 : 0;
598 parity |= (nrbits(anac, 8) & 0x1) ? 0x4 : 0;
599 parity |= (nrbits(nacs, 8) & 0x1) ? 0x8 : 0;
605 oob.u.a.parityPerField = parity;
606 oob.u.a.discarded = 0xaa;
609 &retlen, (
char *)&oob.u);
616 oob.u.b.parityPerField = parity;
617 oob.u.b.discarded = 0xaa;
620 SECTORSIZE * 4 + 8, 8, &retlen, (
char *)&oob.u);
622 inftl->PUtable[writeEUN] = inftl->VUtable[thisVUC];
623 inftl->VUtable[thisVUC] = writeEUN;
625 inftl->numfreeEUNs--;
631 "Unit Chain 0x%x\n", thisVUC);
638 static void INFTL_trydeletechain(
struct INFTLrecord *inftl,
unsigned thisVUC)
640 struct mtd_info *mtd = inftl->mbd.mtd;
643 unsigned int thisEUN,
status;
648 pr_debug(
"INFTL: INFTL_trydeletechain(inftl=%p,"
649 "thisVUC=%d)\n", inftl, thisVUC);
651 memset(BlockUsed, 0,
sizeof(BlockUsed));
652 memset(BlockDeleted, 0,
sizeof(BlockDeleted));
654 thisEUN = inftl->VUtable[thisVUC];
657 "Virtual Unit Chain %d!\n", thisVUC);
666 while (thisEUN < inftl->nb_blocks) {
667 for (block = 0; block < inftl->EraseSize/
SECTORSIZE; block++) {
668 if (BlockUsed[block] || BlockDeleted[block])
672 + (block * SECTORSIZE), 8 , &retlen,
676 status = bci.Status | bci.Status1;
683 BlockUsed[
block] = 1;
686 BlockDeleted[
block] = 1;
690 "for block %d in EUN %d: 0x%x\n",
691 block, thisEUN, status);
697 "Unit Chain 0x%x\n", thisVUC);
701 thisEUN = inftl->PUtable[thisEUN];
704 for (block = 0; block < inftl->EraseSize/
SECTORSIZE; block++)
705 if (BlockUsed[block])
712 pr_debug(
"INFTL: deleting empty VUC %d\n", thisVUC);
715 u16 *prevEUN = &inftl->VUtable[thisVUC];
720 pr_debug(
"INFTL: Empty VUC %d for deletion was already absent\n", thisEUN);
725 while (inftl->PUtable[thisEUN] !=
BLOCK_NIL) {
726 BUG_ON(thisEUN >= inftl->nb_blocks);
728 prevEUN = &inftl->PUtable[thisEUN];
732 pr_debug(
"Deleting EUN %d from VUC %d\n",
743 inftl->numfreeEUNs++;
758 static int INFTL_deleteblock(
struct INFTLrecord *inftl,
unsigned block)
760 unsigned int thisEUN = inftl->VUtable[block / (inftl->EraseSize /
SECTORSIZE)];
761 unsigned long blockofs = (block *
SECTORSIZE) & (inftl->EraseSize - 1);
762 struct mtd_info *mtd = inftl->mbd.mtd;
768 pr_debug(
"INFTL: INFTL_deleteblock(inftl=%p,"
769 "block=%d)\n", inftl, block);
771 while (thisEUN < inftl->nb_blocks) {
773 blockofs, 8, &retlen, (
char *)&bci) < 0)
776 status = bci.Status | bci.Status1;
789 "block %d in EUN %d: 0x%x\n",
790 block, thisEUN, status);
797 block / (inftl->EraseSize / SECTORSIZE));
800 thisEUN = inftl->PUtable[thisEUN];
805 loff_t
ptr = (thisEUN * inftl->EraseSize) + blockofs;
812 INFTL_trydeletechain(inftl, block / (inftl->EraseSize / SECTORSIZE));
817 static int inftl_writeblock(
struct mtd_blktrans_dev *mbd,
unsigned long block,
820 struct INFTLrecord *inftl = (
void *)mbd;
821 unsigned int writeEUN;
822 unsigned long blockofs = (block *
SECTORSIZE) & (inftl->EraseSize - 1);
827 pr_debug(
"INFTL: inftl_writeblock(inftl=%p,block=%ld,"
828 "buffer=%p)\n", inftl, block, buffer);
832 for (p = buffer; p < pend && !*
p; p++)
836 writeEUN = INFTL_findwriteunit(inftl, block);
840 "block to write to\n");
851 inftl_write(inftl->mbd.mtd, (writeEUN * inftl->EraseSize) +
852 blockofs, SECTORSIZE, &retlen, (
char *)buffer,
859 INFTL_deleteblock(inftl, block);
865 static int inftl_readblock(
struct mtd_blktrans_dev *mbd,
unsigned long block,
868 struct INFTLrecord *inftl = (
void *)mbd;
869 unsigned int thisEUN = inftl->VUtable[block / (inftl->EraseSize /
SECTORSIZE)];
870 unsigned long blockofs = (block *
SECTORSIZE) & (inftl->EraseSize - 1);
871 struct mtd_info *mtd = inftl->mbd.mtd;
877 pr_debug(
"INFTL: inftl_readblock(inftl=%p,block=%ld,"
878 "buffer=%p)\n", inftl, block, buffer);
880 while (thisEUN < inftl->nb_blocks) {
882 blockofs, 8, &retlen, (
char *)&bci) < 0)
885 status = bci.Status | bci.Status1;
898 "block %ld in EUN %d: 0x%04x\n",
899 block, thisEUN, status);
905 "Virtual Unit Chain 0x%lx\n",
906 block / (inftl->EraseSize / SECTORSIZE));
910 thisEUN = inftl->PUtable[thisEUN];
916 memset(buffer, 0, SECTORSIZE);
919 loff_t ptr = (thisEUN * inftl->EraseSize) + blockofs;
920 int ret =
mtd_read(mtd, ptr, SECTORSIZE, &retlen, buffer);
923 if (ret < 0 && !mtd_is_bitflip(ret))
931 struct INFTLrecord *inftl = (
void *)dev;
933 geo->
heads = inftl->heads;
945 .getgeo = inftl_getgeo,
946 .readsect = inftl_readblock,
947 .writesect = inftl_writeblock,
948 .add_mtd = inftl_add_mtd,
949 .remove_dev = inftl_remove_dev,
953 static int __init init_inftl(
void)
958 static void __exit cleanup_inftl(
void)
968 MODULE_DESCRIPTION(
"Support code for Inverse Flash Translation Layer, used on M-Systems DiskOnChip 2000, Millennium and Millennium Plus");