2 * inftlcore.c -- Linux driver for Inverse Flash Translation Layer (INFTL)
4 * (C) Copyright 2002, Greg Ungerer (gerg@snapgear.com)
6 * Based heavily on the nftlcore.c code which is:
7 * (c) 1999 Machine Vision Holdings, Inc.
8 * Author: David Woodhouse <dwmw2@infradead.org>
10 * $Id: inftlcore.c,v 1.19 2005/11/07 11:14:20 gleixner Exp $
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, write to the Free Software
24 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
27 #include <linux/kernel.h>
28 #include <linux/module.h>
29 #include <linux/delay.h>
30 #include <linux/slab.h>
31 #include <linux/sched.h>
32 #include <linux/init.h>
33 #include <linux/kmod.h>
34 #include <linux/hdreg.h>
35 #include <linux/mtd/mtd.h>
36 #include <linux/mtd/nftl.h>
37 #include <linux/mtd/inftl.h>
38 #include <linux/mtd/nand.h>
39 #include <asm/uaccess.h>
40 #include <asm/errno.h>
44 * Maximum number of loops while examining next block, to have a
45 * chance to detect consistency problems (they should never happen
46 * because of the checks done in the mounting.
48 #define MAX_LOOPS 10000
50 static void inftl_add_mtd(struct mtd_blktrans_ops
*tr
, struct mtd_info
*mtd
)
52 struct INFTLrecord
*inftl
;
55 if (mtd
->type
!= MTD_NANDFLASH
)
57 /* OK, this is moderately ugly. But probably safe. Alternatives? */
58 if (memcmp(mtd
->name
, "DiskOnChip", 10))
61 if (!mtd
->block_isbad
) {
63 "INFTL no longer supports the old DiskOnChip drivers loaded via docprobe.\n"
64 "Please use the new diskonchip driver under the NAND subsystem.\n");
68 DEBUG(MTD_DEBUG_LEVEL3
, "INFTL: add_mtd for %s\n", mtd
->name
);
70 inftl
= kmalloc(sizeof(*inftl
), GFP_KERNEL
);
73 printk(KERN_WARNING
"INFTL: Out of memory for data structures\n");
76 memset(inftl
, 0, sizeof(*inftl
));
79 inftl
->mbd
.devnum
= -1;
80 inftl
->mbd
.blksize
= 512;
83 if (INFTL_mount(inftl
) < 0) {
84 printk(KERN_WARNING
"INFTL: could not mount device\n");
89 /* OK, it's a new one. Set up all the data structures. */
91 /* Calculate geometry */
92 inftl
->cylinders
= 1024;
95 temp
= inftl
->cylinders
* inftl
->heads
;
96 inftl
->sectors
= inftl
->mbd
.size
/ temp
;
97 if (inftl
->mbd
.size
% temp
) {
99 temp
= inftl
->cylinders
* inftl
->sectors
;
100 inftl
->heads
= inftl
->mbd
.size
/ temp
;
102 if (inftl
->mbd
.size
% temp
) {
104 temp
= inftl
->heads
* inftl
->sectors
;
105 inftl
->cylinders
= inftl
->mbd
.size
/ temp
;
109 if (inftl
->mbd
.size
!= inftl
->heads
* inftl
->cylinders
* inftl
->sectors
) {
112 mbd.size == heads * cylinders * sectors
114 printk(KERN_WARNING
"INFTL: cannot calculate a geometry to "
115 "match size of 0x%lx.\n", inftl
->mbd
.size
);
116 printk(KERN_WARNING
"INFTL: using C:%d H:%d S:%d "
117 "(== 0x%lx sects)\n",
118 inftl
->cylinders
, inftl
->heads
, inftl
->sectors
,
119 (long)inftl
->cylinders
* (long)inftl
->heads
*
120 (long)inftl
->sectors
);
123 if (add_mtd_blktrans_dev(&inftl
->mbd
)) {
124 kfree(inftl
->PUtable
);
125 kfree(inftl
->VUtable
);
130 printk(KERN_INFO
"INFTL: Found new inftl%c\n", inftl
->mbd
.devnum
+ 'a');
135 static void inftl_remove_dev(struct mtd_blktrans_dev
*dev
)
137 struct INFTLrecord
*inftl
= (void *)dev
;
139 DEBUG(MTD_DEBUG_LEVEL3
, "INFTL: remove_dev (i=%d)\n", dev
->devnum
);
141 del_mtd_blktrans_dev(dev
);
143 kfree(inftl
->PUtable
);
144 kfree(inftl
->VUtable
);
149 * Actual INFTL access routines.
153 * Read oob data from flash
155 int inftl_read_oob(struct mtd_info
*mtd
, loff_t offs
, size_t len
,
156 size_t *retlen
, uint8_t *buf
)
158 struct mtd_oob_ops ops
;
161 ops
.mode
= MTD_OOB_PLACE
;
162 ops
.ooboffs
= offs
& (mtd
->writesize
- 1);
168 res
= mtd
->read_oob(mtd
, offs
& ~(mtd
->writesize
- 1), &ops
);
169 *retlen
= ops
.retlen
;
174 * Write oob data to flash
176 int inftl_write_oob(struct mtd_info
*mtd
, loff_t offs
, size_t len
,
177 size_t *retlen
, uint8_t *buf
)
179 struct mtd_oob_ops ops
;
182 ops
.mode
= MTD_OOB_PLACE
;
183 ops
.ooboffs
= offs
& (mtd
->writesize
- 1);
189 res
= mtd
->write_oob(mtd
, offs
& ~(mtd
->writesize
- 1), &ops
);
190 *retlen
= ops
.retlen
;
195 * Write data and oob to flash
197 static int inftl_write(struct mtd_info
*mtd
, loff_t offs
, size_t len
,
198 size_t *retlen
, uint8_t *buf
, uint8_t *oob
)
200 struct mtd_oob_ops ops
;
203 ops
.mode
= MTD_OOB_PLACE
;
205 ops
.ooblen
= mtd
->oobsize
;
210 res
= mtd
->write_oob(mtd
, offs
& ~(mtd
->writesize
- 1), &ops
);
211 *retlen
= ops
.retlen
;
216 * INFTL_findfreeblock: Find a free Erase Unit on the INFTL partition.
217 * This function is used when the give Virtual Unit Chain.
219 static u16
INFTL_findfreeblock(struct INFTLrecord
*inftl
, int desperate
)
221 u16 pot
= inftl
->LastFreeEUN
;
222 int silly
= inftl
->nb_blocks
;
224 DEBUG(MTD_DEBUG_LEVEL3
, "INFTL: INFTL_findfreeblock(inftl=%p,"
225 "desperate=%d)\n", inftl
, desperate
);
228 * Normally, we force a fold to happen before we run out of free
231 if (!desperate
&& inftl
->numfreeEUNs
< 2) {
232 DEBUG(MTD_DEBUG_LEVEL1
, "INFTL: there are too few free "
233 "EUNs (%d)\n", inftl
->numfreeEUNs
);
237 /* Scan for a free block */
239 if (inftl
->PUtable
[pot
] == BLOCK_FREE
) {
240 inftl
->LastFreeEUN
= pot
;
244 if (++pot
> inftl
->lastEUN
)
248 printk(KERN_WARNING
"INFTL: no free blocks found! "
249 "EUN range = %d - %d\n", 0, inftl
->LastFreeEUN
);
252 } while (pot
!= inftl
->LastFreeEUN
);
257 static u16
INFTL_foldchain(struct INFTLrecord
*inftl
, unsigned thisVUC
, unsigned pendingblock
)
259 u16 BlockMap
[MAX_SECTORS_PER_UNIT
];
260 unsigned char BlockDeleted
[MAX_SECTORS_PER_UNIT
];
261 unsigned int thisEUN
, prevEUN
, status
;
262 struct mtd_info
*mtd
= inftl
->mbd
.mtd
;
264 unsigned int targetEUN
;
265 struct inftl_oob oob
;
268 DEBUG(MTD_DEBUG_LEVEL3
, "INFTL: INFTL_foldchain(inftl=%p,thisVUC=%d,"
269 "pending=%d)\n", inftl
, thisVUC
, pendingblock
);
271 memset(BlockMap
, 0xff, sizeof(BlockMap
));
272 memset(BlockDeleted
, 0, sizeof(BlockDeleted
));
274 thisEUN
= targetEUN
= inftl
->VUtable
[thisVUC
];
276 if (thisEUN
== BLOCK_NIL
) {
277 printk(KERN_WARNING
"INFTL: trying to fold non-existent "
278 "Virtual Unit Chain %d!\n", thisVUC
);
283 * Scan to find the Erase Unit which holds the actual data for each
284 * 512-byte block within the Chain.
287 while (thisEUN
< inftl
->nb_blocks
) {
288 for (block
= 0; block
< inftl
->EraseSize
/SECTORSIZE
; block
++) {
289 if ((BlockMap
[block
] != 0xffff) || BlockDeleted
[block
])
292 if (inftl_read_oob(mtd
, (thisEUN
* inftl
->EraseSize
)
293 + (block
* SECTORSIZE
), 16, &retlen
,
295 status
= SECTOR_IGNORE
;
297 status
= oob
.b
.Status
| oob
.b
.Status1
;
304 BlockMap
[block
] = thisEUN
;
307 BlockDeleted
[block
] = 1;
310 printk(KERN_WARNING
"INFTL: unknown status "
311 "for block %d in EUN %d: %x\n",
312 block
, thisEUN
, status
);
318 printk(KERN_WARNING
"INFTL: infinite loop in Virtual "
319 "Unit Chain 0x%x\n", thisVUC
);
323 thisEUN
= inftl
->PUtable
[thisEUN
];
327 * OK. We now know the location of every block in the Virtual Unit
328 * Chain, and the Erase Unit into which we are supposed to be copying.
331 DEBUG(MTD_DEBUG_LEVEL1
, "INFTL: folding chain %d into unit %d\n",
334 for (block
= 0; block
< inftl
->EraseSize
/SECTORSIZE
; block
++) {
335 unsigned char movebuf
[SECTORSIZE
];
339 * If it's in the target EUN already, or if it's pending write,
342 if (BlockMap
[block
] == targetEUN
|| (pendingblock
==
343 (thisVUC
* (inftl
->EraseSize
/ SECTORSIZE
) + block
))) {
348 * Copy only in non free block (free blocks can only
349 * happen in case of media errors or deleted blocks).
351 if (BlockMap
[block
] == BLOCK_NIL
)
354 ret
= mtd
->read(mtd
, (inftl
->EraseSize
* BlockMap
[block
]) +
355 (block
* SECTORSIZE
), SECTORSIZE
, &retlen
,
357 if (ret
< 0 && ret
!= -EUCLEAN
) {
359 (inftl
->EraseSize
* BlockMap
[block
]) +
360 (block
* SECTORSIZE
), SECTORSIZE
,
363 DEBUG(MTD_DEBUG_LEVEL1
, "INFTL: error went "
366 memset(&oob
, 0xff, sizeof(struct inftl_oob
));
367 oob
.b
.Status
= oob
.b
.Status1
= SECTOR_USED
;
369 inftl_write(inftl
->mbd
.mtd
, (inftl
->EraseSize
* targetEUN
) +
370 (block
* SECTORSIZE
), SECTORSIZE
, &retlen
,
371 movebuf
, (char *)&oob
);
375 * Newest unit in chain now contains data from _all_ older units.
376 * So go through and erase each unit in chain, oldest first. (This
377 * is important, by doing oldest first if we crash/reboot then it
378 * it is relatively simple to clean up the mess).
380 DEBUG(MTD_DEBUG_LEVEL1
, "INFTL: want to erase virtual chain %d\n",
384 /* Find oldest unit in chain. */
385 thisEUN
= inftl
->VUtable
[thisVUC
];
387 while (inftl
->PUtable
[thisEUN
] != BLOCK_NIL
) {
389 thisEUN
= inftl
->PUtable
[thisEUN
];
392 /* Check if we are all done */
393 if (thisEUN
== targetEUN
)
396 if (INFTL_formatblock(inftl
, thisEUN
) < 0) {
398 * Could not erase : mark block as reserved.
400 inftl
->PUtable
[thisEUN
] = BLOCK_RESERVED
;
402 /* Correctly erased : mark it as free */
403 inftl
->PUtable
[thisEUN
] = BLOCK_FREE
;
404 inftl
->PUtable
[prevEUN
] = BLOCK_NIL
;
405 inftl
->numfreeEUNs
++;
412 static u16
INFTL_makefreeblock(struct INFTLrecord
*inftl
, unsigned pendingblock
)
415 * This is the part that needs some cleverness applied.
416 * For now, I'm doing the minimum applicable to actually
417 * get the thing to work.
418 * Wear-levelling and other clever stuff needs to be implemented
419 * and we also need to do some assessment of the results when
420 * the system loses power half-way through the routine.
422 u16 LongestChain
= 0;
423 u16 ChainLength
= 0, thislen
;
426 DEBUG(MTD_DEBUG_LEVEL3
, "INFTL: INFTL_makefreeblock(inftl=%p,"
427 "pending=%d)\n", inftl
, pendingblock
);
429 for (chain
= 0; chain
< inftl
->nb_blocks
; chain
++) {
430 EUN
= inftl
->VUtable
[chain
];
433 while (EUN
<= inftl
->lastEUN
) {
435 EUN
= inftl
->PUtable
[EUN
];
436 if (thislen
> 0xff00) {
437 printk(KERN_WARNING
"INFTL: endless loop in "
438 "Virtual Chain %d: Unit %x\n",
441 * Actually, don't return failure.
442 * Just ignore this chain and get on with it.
449 if (thislen
> ChainLength
) {
450 ChainLength
= thislen
;
451 LongestChain
= chain
;
455 if (ChainLength
< 2) {
456 printk(KERN_WARNING
"INFTL: no Virtual Unit Chains available "
457 "for folding. Failing request\n");
461 return INFTL_foldchain(inftl
, LongestChain
, pendingblock
);
464 static int nrbits(unsigned int val
, int bitcount
)
468 for (i
= 0; (i
< bitcount
); i
++)
469 total
+= (((0x1 << i
) & val
) ? 1 : 0);
474 * INFTL_findwriteunit: Return the unit number into which we can write
475 * for this block. Make it available if it isn't already.
477 static inline u16
INFTL_findwriteunit(struct INFTLrecord
*inftl
, unsigned block
)
479 unsigned int thisVUC
= block
/ (inftl
->EraseSize
/ SECTORSIZE
);
480 unsigned int thisEUN
, writeEUN
, prev_block
, status
;
481 unsigned long blockofs
= (block
* SECTORSIZE
) & (inftl
->EraseSize
-1);
482 struct mtd_info
*mtd
= inftl
->mbd
.mtd
;
483 struct inftl_oob oob
;
484 struct inftl_bci bci
;
485 unsigned char anac
, nacs
, parity
;
487 int silly
, silly2
= 3;
489 DEBUG(MTD_DEBUG_LEVEL3
, "INFTL: INFTL_findwriteunit(inftl=%p,"
490 "block=%d)\n", inftl
, block
);
494 * Scan the media to find a unit in the VUC which has
495 * a free space for the block in question.
497 writeEUN
= BLOCK_NIL
;
498 thisEUN
= inftl
->VUtable
[thisVUC
];
501 while (thisEUN
<= inftl
->lastEUN
) {
502 inftl_read_oob(mtd
, (thisEUN
* inftl
->EraseSize
) +
503 blockofs
, 8, &retlen
, (char *)&bci
);
505 status
= bci
.Status
| bci
.Status1
;
506 DEBUG(MTD_DEBUG_LEVEL3
, "INFTL: status of block %d in "
507 "EUN %d is %x\n", block
, writeEUN
, status
);
515 /* Can't go any further */
521 * Invalid block. Don't use it any more.
528 printk(KERN_WARNING
"INFTL: infinite loop in "
529 "Virtual Unit Chain 0x%x\n", thisVUC
);
533 /* Skip to next block in chain */
534 thisEUN
= inftl
->PUtable
[thisEUN
];
538 if (writeEUN
!= BLOCK_NIL
)
543 * OK. We didn't find one in the existing chain, or there
544 * is no existing chain. Allocate a new one.
546 writeEUN
= INFTL_findfreeblock(inftl
, 0);
548 if (writeEUN
== BLOCK_NIL
) {
550 * That didn't work - there were no free blocks just
551 * waiting to be picked up. We're going to have to fold
552 * a chain to make room.
554 thisEUN
= INFTL_makefreeblock(inftl
, 0xffff);
557 * Hopefully we free something, lets try again.
558 * This time we are desperate...
560 DEBUG(MTD_DEBUG_LEVEL1
, "INFTL: using desperate==1 "
561 "to find free EUN to accommodate write to "
562 "VUC %d\n", thisVUC
);
563 writeEUN
= INFTL_findfreeblock(inftl
, 1);
564 if (writeEUN
== BLOCK_NIL
) {
566 * Ouch. This should never happen - we should
567 * always be able to make some room somehow.
568 * If we get here, we've allocated more storage
569 * space than actual media, or our makefreeblock
570 * routine is missing something.
572 printk(KERN_WARNING
"INFTL: cannot make free "
575 INFTL_dumptables(inftl
);
576 INFTL_dumpVUchains(inftl
);
583 * Insert new block into virtual chain. Firstly update the
584 * block headers in flash...
588 thisEUN
= inftl
->VUtable
[thisVUC
];
589 if (thisEUN
!= BLOCK_NIL
) {
590 inftl_read_oob(mtd
, thisEUN
* inftl
->EraseSize
591 + 8, 8, &retlen
, (char *)&oob
.u
);
592 anac
= oob
.u
.a
.ANAC
+ 1;
593 nacs
= oob
.u
.a
.NACs
+ 1;
596 prev_block
= inftl
->VUtable
[thisVUC
];
597 if (prev_block
< inftl
->nb_blocks
)
598 prev_block
-= inftl
->firstEUN
;
600 parity
= (nrbits(thisVUC
, 16) & 0x1) ? 0x1 : 0;
601 parity
|= (nrbits(prev_block
, 16) & 0x1) ? 0x2 : 0;
602 parity
|= (nrbits(anac
, 8) & 0x1) ? 0x4 : 0;
603 parity
|= (nrbits(nacs
, 8) & 0x1) ? 0x8 : 0;
605 oob
.u
.a
.virtualUnitNo
= cpu_to_le16(thisVUC
);
606 oob
.u
.a
.prevUnitNo
= cpu_to_le16(prev_block
);
609 oob
.u
.a
.parityPerField
= parity
;
610 oob
.u
.a
.discarded
= 0xaa;
612 inftl_write_oob(mtd
, writeEUN
* inftl
->EraseSize
+ 8, 8,
613 &retlen
, (char *)&oob
.u
);
615 /* Also back up header... */
616 oob
.u
.b
.virtualUnitNo
= cpu_to_le16(thisVUC
);
617 oob
.u
.b
.prevUnitNo
= cpu_to_le16(prev_block
);
620 oob
.u
.b
.parityPerField
= parity
;
621 oob
.u
.b
.discarded
= 0xaa;
623 inftl_write_oob(mtd
, writeEUN
* inftl
->EraseSize
+
624 SECTORSIZE
* 4 + 8, 8, &retlen
, (char *)&oob
.u
);
626 inftl
->PUtable
[writeEUN
] = inftl
->VUtable
[thisVUC
];
627 inftl
->VUtable
[thisVUC
] = writeEUN
;
629 inftl
->numfreeEUNs
--;
634 printk(KERN_WARNING
"INFTL: error folding to make room for Virtual "
635 "Unit Chain 0x%x\n", thisVUC
);
640 * Given a Virtual Unit Chain, see if it can be deleted, and if so do it.
642 static void INFTL_trydeletechain(struct INFTLrecord
*inftl
, unsigned thisVUC
)
644 struct mtd_info
*mtd
= inftl
->mbd
.mtd
;
645 unsigned char BlockUsed
[MAX_SECTORS_PER_UNIT
];
646 unsigned char BlockDeleted
[MAX_SECTORS_PER_UNIT
];
647 unsigned int thisEUN
, status
;
649 struct inftl_bci bci
;
652 DEBUG(MTD_DEBUG_LEVEL3
, "INFTL: INFTL_trydeletechain(inftl=%p,"
653 "thisVUC=%d)\n", inftl
, thisVUC
);
655 memset(BlockUsed
, 0, sizeof(BlockUsed
));
656 memset(BlockDeleted
, 0, sizeof(BlockDeleted
));
658 thisEUN
= inftl
->VUtable
[thisVUC
];
659 if (thisEUN
== BLOCK_NIL
) {
660 printk(KERN_WARNING
"INFTL: trying to delete non-existent "
661 "Virtual Unit Chain %d!\n", thisVUC
);
666 * Scan through the Erase Units to determine whether any data is in
667 * each of the 512-byte blocks within the Chain.
670 while (thisEUN
< inftl
->nb_blocks
) {
671 for (block
= 0; block
< inftl
->EraseSize
/SECTORSIZE
; block
++) {
672 if (BlockUsed
[block
] || BlockDeleted
[block
])
675 if (inftl_read_oob(mtd
, (thisEUN
* inftl
->EraseSize
)
676 + (block
* SECTORSIZE
), 8 , &retlen
,
678 status
= SECTOR_IGNORE
;
680 status
= bci
.Status
| bci
.Status1
;
687 BlockUsed
[block
] = 1;
690 BlockDeleted
[block
] = 1;
693 printk(KERN_WARNING
"INFTL: unknown status "
694 "for block %d in EUN %d: 0x%x\n",
695 block
, thisEUN
, status
);
700 printk(KERN_WARNING
"INFTL: infinite loop in Virtual "
701 "Unit Chain 0x%x\n", thisVUC
);
705 thisEUN
= inftl
->PUtable
[thisEUN
];
708 for (block
= 0; block
< inftl
->EraseSize
/SECTORSIZE
; block
++)
709 if (BlockUsed
[block
])
713 * For each block in the chain free it and make it available
714 * for future use. Erase from the oldest unit first.
716 DEBUG(MTD_DEBUG_LEVEL1
, "INFTL: deleting empty VUC %d\n", thisVUC
);
719 u16
*prevEUN
= &inftl
->VUtable
[thisVUC
];
722 /* If the chain is all gone already, we're done */
723 if (thisEUN
== BLOCK_NIL
) {
724 DEBUG(MTD_DEBUG_LEVEL2
, "INFTL: Empty VUC %d for deletion was already absent\n", thisEUN
);
728 /* Find oldest unit in chain. */
729 while (inftl
->PUtable
[thisEUN
] != BLOCK_NIL
) {
730 BUG_ON(thisEUN
>= inftl
->nb_blocks
);
732 prevEUN
= &inftl
->PUtable
[thisEUN
];
736 DEBUG(MTD_DEBUG_LEVEL3
, "Deleting EUN %d from VUC %d\n",
739 if (INFTL_formatblock(inftl
, thisEUN
) < 0) {
741 * Could not erase : mark block as reserved.
743 inftl
->PUtable
[thisEUN
] = BLOCK_RESERVED
;
745 /* Correctly erased : mark it as free */
746 inftl
->PUtable
[thisEUN
] = BLOCK_FREE
;
747 inftl
->numfreeEUNs
++;
750 /* Now sort out whatever was pointing to it... */
751 *prevEUN
= BLOCK_NIL
;
753 /* Ideally we'd actually be responsive to new
754 requests while we're doing this -- if there's
755 free space why should others be made to wait? */
759 inftl
->VUtable
[thisVUC
] = BLOCK_NIL
;
762 static int INFTL_deleteblock(struct INFTLrecord
*inftl
, unsigned block
)
764 unsigned int thisEUN
= inftl
->VUtable
[block
/ (inftl
->EraseSize
/ SECTORSIZE
)];
765 unsigned long blockofs
= (block
* SECTORSIZE
) & (inftl
->EraseSize
- 1);
766 struct mtd_info
*mtd
= inftl
->mbd
.mtd
;
768 int silly
= MAX_LOOPS
;
770 struct inftl_bci bci
;
772 DEBUG(MTD_DEBUG_LEVEL3
, "INFTL: INFTL_deleteblock(inftl=%p,"
773 "block=%d)\n", inftl
, block
);
775 while (thisEUN
< inftl
->nb_blocks
) {
776 if (inftl_read_oob(mtd
, (thisEUN
* inftl
->EraseSize
) +
777 blockofs
, 8, &retlen
, (char *)&bci
) < 0)
778 status
= SECTOR_IGNORE
;
780 status
= bci
.Status
| bci
.Status1
;
792 printk(KERN_WARNING
"INFTL: unknown status for "
793 "block %d in EUN %d: 0x%x\n",
794 block
, thisEUN
, status
);
799 printk(KERN_WARNING
"INFTL: infinite loop in Virtual "
801 block
/ (inftl
->EraseSize
/ SECTORSIZE
));
804 thisEUN
= inftl
->PUtable
[thisEUN
];
808 if (thisEUN
!= BLOCK_NIL
) {
809 loff_t ptr
= (thisEUN
* inftl
->EraseSize
) + blockofs
;
811 if (inftl_read_oob(mtd
, ptr
, 8, &retlen
, (char *)&bci
) < 0)
813 bci
.Status
= bci
.Status1
= SECTOR_DELETED
;
814 if (inftl_write_oob(mtd
, ptr
, 8, &retlen
, (char *)&bci
) < 0)
816 INFTL_trydeletechain(inftl
, block
/ (inftl
->EraseSize
/ SECTORSIZE
));
821 static int inftl_writeblock(struct mtd_blktrans_dev
*mbd
, unsigned long block
,
824 struct INFTLrecord
*inftl
= (void *)mbd
;
825 unsigned int writeEUN
;
826 unsigned long blockofs
= (block
* SECTORSIZE
) & (inftl
->EraseSize
- 1);
828 struct inftl_oob oob
;
831 DEBUG(MTD_DEBUG_LEVEL3
, "INFTL: inftl_writeblock(inftl=%p,block=%ld,"
832 "buffer=%p)\n", inftl
, block
, buffer
);
834 /* Is block all zero? */
835 pend
= buffer
+ SECTORSIZE
;
836 for (p
= buffer
; p
< pend
&& !*p
; p
++)
840 writeEUN
= INFTL_findwriteunit(inftl
, block
);
842 if (writeEUN
== BLOCK_NIL
) {
843 printk(KERN_WARNING
"inftl_writeblock(): cannot find "
844 "block to write to\n");
846 * If we _still_ haven't got a block to use,
852 memset(&oob
, 0xff, sizeof(struct inftl_oob
));
853 oob
.b
.Status
= oob
.b
.Status1
= SECTOR_USED
;
855 inftl_write(inftl
->mbd
.mtd
, (writeEUN
* inftl
->EraseSize
) +
856 blockofs
, SECTORSIZE
, &retlen
, (char *)buffer
,
859 * need to write SECTOR_USED flags since they are not written
863 INFTL_deleteblock(inftl
, block
);
869 static int inftl_readblock(struct mtd_blktrans_dev
*mbd
, unsigned long block
,
872 struct INFTLrecord
*inftl
= (void *)mbd
;
873 unsigned int thisEUN
= inftl
->VUtable
[block
/ (inftl
->EraseSize
/ SECTORSIZE
)];
874 unsigned long blockofs
= (block
* SECTORSIZE
) & (inftl
->EraseSize
- 1);
875 struct mtd_info
*mtd
= inftl
->mbd
.mtd
;
877 int silly
= MAX_LOOPS
;
878 struct inftl_bci bci
;
881 DEBUG(MTD_DEBUG_LEVEL3
, "INFTL: inftl_readblock(inftl=%p,block=%ld,"
882 "buffer=%p)\n", inftl
, block
, buffer
);
884 while (thisEUN
< inftl
->nb_blocks
) {
885 if (inftl_read_oob(mtd
, (thisEUN
* inftl
->EraseSize
) +
886 blockofs
, 8, &retlen
, (char *)&bci
) < 0)
887 status
= SECTOR_IGNORE
;
889 status
= bci
.Status
| bci
.Status1
;
901 printk(KERN_WARNING
"INFTL: unknown status for "
902 "block %ld in EUN %d: 0x%04x\n",
903 block
, thisEUN
, status
);
908 printk(KERN_WARNING
"INFTL: infinite loop in "
909 "Virtual Unit Chain 0x%lx\n",
910 block
/ (inftl
->EraseSize
/ SECTORSIZE
));
914 thisEUN
= inftl
->PUtable
[thisEUN
];
918 if (thisEUN
== BLOCK_NIL
) {
919 /* The requested block is not on the media, return all 0x00 */
920 memset(buffer
, 0, SECTORSIZE
);
923 loff_t ptr
= (thisEUN
* inftl
->EraseSize
) + blockofs
;
924 int ret
= mtd
->read(mtd
, ptr
, SECTORSIZE
, &retlen
, buffer
);
926 /* Handle corrected bit flips gracefully */
927 if (ret
< 0 && ret
!= -EUCLEAN
)
933 static int inftl_getgeo(struct mtd_blktrans_dev
*dev
, struct hd_geometry
*geo
)
935 struct INFTLrecord
*inftl
= (void *)dev
;
937 geo
->heads
= inftl
->heads
;
938 geo
->sectors
= inftl
->sectors
;
939 geo
->cylinders
= inftl
->cylinders
;
944 static struct mtd_blktrans_ops inftl_tr
= {
946 .major
= INFTL_MAJOR
,
947 .part_bits
= INFTL_PARTN_BITS
,
948 .getgeo
= inftl_getgeo
,
949 .readsect
= inftl_readblock
,
950 .writesect
= inftl_writeblock
,
951 .add_mtd
= inftl_add_mtd
,
952 .remove_dev
= inftl_remove_dev
,
953 .owner
= THIS_MODULE
,
956 static int __init
init_inftl(void)
958 printk(KERN_INFO
"INFTL: inftlcore.c $Revision: 1.19 $, "
959 "inftlmount.c %s\n", inftlmountrev
);
961 return register_mtd_blktrans(&inftl_tr
);
964 static void __exit
cleanup_inftl(void)
966 deregister_mtd_blktrans(&inftl_tr
);
969 module_init(init_inftl
);
970 module_exit(cleanup_inftl
);
972 MODULE_LICENSE("GPL");
973 MODULE_AUTHOR("Greg Ungerer <gerg@snapgear.com>, David Woodhouse <dwmw2@infradead.org>, Fabrice Bellard <fabrice.bellard@netgem.com> et al.");
974 MODULE_DESCRIPTION("Support code for Inverse Flash Translation Layer, used on M-Systems DiskOnChip 2000, Millennium and Millennium Plus");