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 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25 #include <linux/kernel.h>
26 #include <linux/module.h>
27 #include <linux/delay.h>
28 #include <linux/slab.h>
29 #include <linux/sched.h>
30 #include <linux/init.h>
31 #include <linux/kmod.h>
32 #include <linux/hdreg.h>
33 #include <linux/mtd/mtd.h>
34 #include <linux/mtd/nftl.h>
35 #include <linux/mtd/inftl.h>
36 #include <linux/mtd/nand.h>
37 #include <asm/uaccess.h>
38 #include <asm/errno.h>
42 * Maximum number of loops while examining next block, to have a
43 * chance to detect consistency problems (they should never happen
44 * because of the checks done in the mounting.
46 #define MAX_LOOPS 10000
48 static void inftl_add_mtd(struct mtd_blktrans_ops
*tr
, struct mtd_info
*mtd
)
50 struct INFTLrecord
*inftl
;
53 if (mtd
->type
!= MTD_NANDFLASH
|| mtd
->size
> UINT_MAX
)
55 /* OK, this is moderately ugly. But probably safe. Alternatives? */
56 if (memcmp(mtd
->name
, "DiskOnChip", 10))
59 if (!mtd
->block_isbad
) {
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");
66 DEBUG(MTD_DEBUG_LEVEL3
, "INFTL: add_mtd for %s\n", mtd
->name
);
68 inftl
= kzalloc(sizeof(*inftl
), GFP_KERNEL
);
71 printk(KERN_WARNING
"INFTL: Out of memory for data structures\n");
76 inftl
->mbd
.devnum
= -1;
80 if (INFTL_mount(inftl
) < 0) {
81 printk(KERN_WARNING
"INFTL: could not mount device\n");
86 /* OK, it's a new one. Set up all the data structures. */
88 /* Calculate geometry */
89 inftl
->cylinders
= 1024;
92 temp
= inftl
->cylinders
* inftl
->heads
;
93 inftl
->sectors
= inftl
->mbd
.size
/ temp
;
94 if (inftl
->mbd
.size
% temp
) {
96 temp
= inftl
->cylinders
* inftl
->sectors
;
97 inftl
->heads
= inftl
->mbd
.size
/ temp
;
99 if (inftl
->mbd
.size
% temp
) {
101 temp
= inftl
->heads
* inftl
->sectors
;
102 inftl
->cylinders
= inftl
->mbd
.size
/ temp
;
106 if (inftl
->mbd
.size
!= inftl
->heads
* inftl
->cylinders
* inftl
->sectors
) {
109 mbd.size == heads * cylinders * sectors
111 printk(KERN_WARNING
"INFTL: cannot calculate a geometry to "
112 "match size of 0x%lx.\n", inftl
->mbd
.size
);
113 printk(KERN_WARNING
"INFTL: using C:%d H:%d S:%d "
114 "(== 0x%lx sects)\n",
115 inftl
->cylinders
, inftl
->heads
, inftl
->sectors
,
116 (long)inftl
->cylinders
* (long)inftl
->heads
*
117 (long)inftl
->sectors
);
120 if (add_mtd_blktrans_dev(&inftl
->mbd
)) {
121 kfree(inftl
->PUtable
);
122 kfree(inftl
->VUtable
);
127 printk(KERN_INFO
"INFTL: Found new inftl%c\n", inftl
->mbd
.devnum
+ 'a');
132 static void inftl_remove_dev(struct mtd_blktrans_dev
*dev
)
134 struct INFTLrecord
*inftl
= (void *)dev
;
136 DEBUG(MTD_DEBUG_LEVEL3
, "INFTL: remove_dev (i=%d)\n", dev
->devnum
);
138 del_mtd_blktrans_dev(dev
);
140 kfree(inftl
->PUtable
);
141 kfree(inftl
->VUtable
);
146 * Actual INFTL access routines.
150 * Read oob data from flash
152 int inftl_read_oob(struct mtd_info
*mtd
, loff_t offs
, size_t len
,
153 size_t *retlen
, uint8_t *buf
)
155 struct mtd_oob_ops ops
;
158 ops
.mode
= MTD_OOB_PLACE
;
159 ops
.ooboffs
= offs
& (mtd
->writesize
- 1);
164 res
= mtd
->read_oob(mtd
, offs
& ~(mtd
->writesize
- 1), &ops
);
165 *retlen
= ops
.oobretlen
;
170 * Write oob data to flash
172 int inftl_write_oob(struct mtd_info
*mtd
, loff_t offs
, size_t len
,
173 size_t *retlen
, uint8_t *buf
)
175 struct mtd_oob_ops ops
;
178 ops
.mode
= MTD_OOB_PLACE
;
179 ops
.ooboffs
= offs
& (mtd
->writesize
- 1);
184 res
= mtd
->write_oob(mtd
, offs
& ~(mtd
->writesize
- 1), &ops
);
185 *retlen
= ops
.oobretlen
;
190 * Write data and oob to flash
192 static int inftl_write(struct mtd_info
*mtd
, loff_t offs
, size_t len
,
193 size_t *retlen
, uint8_t *buf
, uint8_t *oob
)
195 struct mtd_oob_ops ops
;
198 ops
.mode
= MTD_OOB_PLACE
;
200 ops
.ooblen
= mtd
->oobsize
;
205 res
= mtd
->write_oob(mtd
, offs
& ~(mtd
->writesize
- 1), &ops
);
206 *retlen
= ops
.retlen
;
211 * INFTL_findfreeblock: Find a free Erase Unit on the INFTL partition.
212 * This function is used when the give Virtual Unit Chain.
214 static u16
INFTL_findfreeblock(struct INFTLrecord
*inftl
, int desperate
)
216 u16 pot
= inftl
->LastFreeEUN
;
217 int silly
= inftl
->nb_blocks
;
219 DEBUG(MTD_DEBUG_LEVEL3
, "INFTL: INFTL_findfreeblock(inftl=%p,"
220 "desperate=%d)\n", inftl
, desperate
);
223 * Normally, we force a fold to happen before we run out of free
226 if (!desperate
&& inftl
->numfreeEUNs
< 2) {
227 DEBUG(MTD_DEBUG_LEVEL1
, "INFTL: there are too few free "
228 "EUNs (%d)\n", inftl
->numfreeEUNs
);
232 /* Scan for a free block */
234 if (inftl
->PUtable
[pot
] == BLOCK_FREE
) {
235 inftl
->LastFreeEUN
= pot
;
239 if (++pot
> inftl
->lastEUN
)
243 printk(KERN_WARNING
"INFTL: no free blocks found! "
244 "EUN range = %d - %d\n", 0, inftl
->LastFreeEUN
);
247 } while (pot
!= inftl
->LastFreeEUN
);
252 static u16
INFTL_foldchain(struct INFTLrecord
*inftl
, unsigned thisVUC
, unsigned pendingblock
)
254 u16 BlockMap
[MAX_SECTORS_PER_UNIT
];
255 unsigned char BlockDeleted
[MAX_SECTORS_PER_UNIT
];
256 unsigned int thisEUN
, prevEUN
, status
;
257 struct mtd_info
*mtd
= inftl
->mbd
.mtd
;
259 unsigned int targetEUN
;
260 struct inftl_oob oob
;
263 DEBUG(MTD_DEBUG_LEVEL3
, "INFTL: INFTL_foldchain(inftl=%p,thisVUC=%d,"
264 "pending=%d)\n", inftl
, thisVUC
, pendingblock
);
266 memset(BlockMap
, 0xff, sizeof(BlockMap
));
267 memset(BlockDeleted
, 0, sizeof(BlockDeleted
));
269 thisEUN
= targetEUN
= inftl
->VUtable
[thisVUC
];
271 if (thisEUN
== BLOCK_NIL
) {
272 printk(KERN_WARNING
"INFTL: trying to fold non-existent "
273 "Virtual Unit Chain %d!\n", thisVUC
);
278 * Scan to find the Erase Unit which holds the actual data for each
279 * 512-byte block within the Chain.
282 while (thisEUN
< inftl
->nb_blocks
) {
283 for (block
= 0; block
< inftl
->EraseSize
/SECTORSIZE
; block
++) {
284 if ((BlockMap
[block
] != BLOCK_NIL
) ||
288 if (inftl_read_oob(mtd
, (thisEUN
* inftl
->EraseSize
)
289 + (block
* SECTORSIZE
), 16, &retlen
,
291 status
= SECTOR_IGNORE
;
293 status
= oob
.b
.Status
| oob
.b
.Status1
;
300 BlockMap
[block
] = thisEUN
;
303 BlockDeleted
[block
] = 1;
306 printk(KERN_WARNING
"INFTL: unknown status "
307 "for block %d in EUN %d: %x\n",
308 block
, thisEUN
, status
);
314 printk(KERN_WARNING
"INFTL: infinite loop in Virtual "
315 "Unit Chain 0x%x\n", thisVUC
);
319 thisEUN
= inftl
->PUtable
[thisEUN
];
323 * OK. We now know the location of every block in the Virtual Unit
324 * Chain, and the Erase Unit into which we are supposed to be copying.
327 DEBUG(MTD_DEBUG_LEVEL1
, "INFTL: folding chain %d into unit %d\n",
330 for (block
= 0; block
< inftl
->EraseSize
/SECTORSIZE
; block
++) {
331 unsigned char movebuf
[SECTORSIZE
];
335 * If it's in the target EUN already, or if it's pending write,
338 if (BlockMap
[block
] == targetEUN
|| (pendingblock
==
339 (thisVUC
* (inftl
->EraseSize
/ SECTORSIZE
) + block
))) {
344 * Copy only in non free block (free blocks can only
345 * happen in case of media errors or deleted blocks).
347 if (BlockMap
[block
] == BLOCK_NIL
)
350 ret
= mtd
->read(mtd
, (inftl
->EraseSize
* BlockMap
[block
]) +
351 (block
* SECTORSIZE
), SECTORSIZE
, &retlen
,
353 if (ret
< 0 && ret
!= -EUCLEAN
) {
355 (inftl
->EraseSize
* BlockMap
[block
]) +
356 (block
* SECTORSIZE
), SECTORSIZE
,
359 DEBUG(MTD_DEBUG_LEVEL1
, "INFTL: error went "
362 memset(&oob
, 0xff, sizeof(struct inftl_oob
));
363 oob
.b
.Status
= oob
.b
.Status1
= SECTOR_USED
;
365 inftl_write(inftl
->mbd
.mtd
, (inftl
->EraseSize
* targetEUN
) +
366 (block
* SECTORSIZE
), SECTORSIZE
, &retlen
,
367 movebuf
, (char *)&oob
);
371 * Newest unit in chain now contains data from _all_ older units.
372 * So go through and erase each unit in chain, oldest first. (This
373 * is important, by doing oldest first if we crash/reboot then it
374 * it is relatively simple to clean up the mess).
376 DEBUG(MTD_DEBUG_LEVEL1
, "INFTL: want to erase virtual chain %d\n",
380 /* Find oldest unit in chain. */
381 thisEUN
= inftl
->VUtable
[thisVUC
];
383 while (inftl
->PUtable
[thisEUN
] != BLOCK_NIL
) {
385 thisEUN
= inftl
->PUtable
[thisEUN
];
388 /* Check if we are all done */
389 if (thisEUN
== targetEUN
)
392 /* Unlink the last block from the chain. */
393 inftl
->PUtable
[prevEUN
] = BLOCK_NIL
;
395 /* Now try to erase it. */
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
->numfreeEUNs
++;
411 static u16
INFTL_makefreeblock(struct INFTLrecord
*inftl
, unsigned pendingblock
)
414 * This is the part that needs some cleverness applied.
415 * For now, I'm doing the minimum applicable to actually
416 * get the thing to work.
417 * Wear-levelling and other clever stuff needs to be implemented
418 * and we also need to do some assessment of the results when
419 * the system loses power half-way through the routine.
421 u16 LongestChain
= 0;
422 u16 ChainLength
= 0, thislen
;
425 DEBUG(MTD_DEBUG_LEVEL3
, "INFTL: INFTL_makefreeblock(inftl=%p,"
426 "pending=%d)\n", inftl
, pendingblock
);
428 for (chain
= 0; chain
< inftl
->nb_blocks
; chain
++) {
429 EUN
= inftl
->VUtable
[chain
];
432 while (EUN
<= inftl
->lastEUN
) {
434 EUN
= inftl
->PUtable
[EUN
];
435 if (thislen
> 0xff00) {
436 printk(KERN_WARNING
"INFTL: endless loop in "
437 "Virtual Chain %d: Unit %x\n",
440 * Actually, don't return failure.
441 * Just ignore this chain and get on with it.
448 if (thislen
> ChainLength
) {
449 ChainLength
= thislen
;
450 LongestChain
= chain
;
454 if (ChainLength
< 2) {
455 printk(KERN_WARNING
"INFTL: no Virtual Unit Chains available "
456 "for folding. Failing request\n");
460 return INFTL_foldchain(inftl
, LongestChain
, pendingblock
);
463 static int nrbits(unsigned int val
, int bitcount
)
467 for (i
= 0; (i
< bitcount
); i
++)
468 total
+= (((0x1 << i
) & val
) ? 1 : 0);
473 * INFTL_findwriteunit: Return the unit number into which we can write
474 * for this block. Make it available if it isn't already.
476 static inline u16
INFTL_findwriteunit(struct INFTLrecord
*inftl
, unsigned block
)
478 unsigned int thisVUC
= block
/ (inftl
->EraseSize
/ SECTORSIZE
);
479 unsigned int thisEUN
, writeEUN
, prev_block
, status
;
480 unsigned long blockofs
= (block
* SECTORSIZE
) & (inftl
->EraseSize
-1);
481 struct mtd_info
*mtd
= inftl
->mbd
.mtd
;
482 struct inftl_oob oob
;
483 struct inftl_bci bci
;
484 unsigned char anac
, nacs
, parity
;
486 int silly
, silly2
= 3;
488 DEBUG(MTD_DEBUG_LEVEL3
, "INFTL: INFTL_findwriteunit(inftl=%p,"
489 "block=%d)\n", inftl
, block
);
493 * Scan the media to find a unit in the VUC which has
494 * a free space for the block in question.
496 writeEUN
= BLOCK_NIL
;
497 thisEUN
= inftl
->VUtable
[thisVUC
];
500 while (thisEUN
<= inftl
->lastEUN
) {
501 inftl_read_oob(mtd
, (thisEUN
* inftl
->EraseSize
) +
502 blockofs
, 8, &retlen
, (char *)&bci
);
504 status
= bci
.Status
| bci
.Status1
;
505 DEBUG(MTD_DEBUG_LEVEL3
, "INFTL: status of block %d in "
506 "EUN %d is %x\n", block
, writeEUN
, status
);
514 /* Can't go any further */
520 * Invalid block. Don't use it any more.
527 printk(KERN_WARNING
"INFTL: infinite loop in "
528 "Virtual Unit Chain 0x%x\n", thisVUC
);
532 /* Skip to next block in chain */
533 thisEUN
= inftl
->PUtable
[thisEUN
];
537 if (writeEUN
!= BLOCK_NIL
)
542 * OK. We didn't find one in the existing chain, or there
543 * is no existing chain. Allocate a new one.
545 writeEUN
= INFTL_findfreeblock(inftl
, 0);
547 if (writeEUN
== BLOCK_NIL
) {
549 * That didn't work - there were no free blocks just
550 * waiting to be picked up. We're going to have to fold
551 * a chain to make room.
553 thisEUN
= INFTL_makefreeblock(inftl
, BLOCK_NIL
);
556 * Hopefully we free something, lets try again.
557 * This time we are desperate...
559 DEBUG(MTD_DEBUG_LEVEL1
, "INFTL: using desperate==1 "
560 "to find free EUN to accommodate write to "
561 "VUC %d\n", thisVUC
);
562 writeEUN
= INFTL_findfreeblock(inftl
, 1);
563 if (writeEUN
== BLOCK_NIL
) {
565 * Ouch. This should never happen - we should
566 * always be able to make some room somehow.
567 * If we get here, we've allocated more storage
568 * space than actual media, or our makefreeblock
569 * routine is missing something.
571 printk(KERN_WARNING
"INFTL: cannot make free "
574 INFTL_dumptables(inftl
);
575 INFTL_dumpVUchains(inftl
);
582 * Insert new block into virtual chain. Firstly update the
583 * block headers in flash...
587 thisEUN
= inftl
->VUtable
[thisVUC
];
588 if (thisEUN
!= BLOCK_NIL
) {
589 inftl_read_oob(mtd
, thisEUN
* inftl
->EraseSize
590 + 8, 8, &retlen
, (char *)&oob
.u
);
591 anac
= oob
.u
.a
.ANAC
+ 1;
592 nacs
= oob
.u
.a
.NACs
+ 1;
595 prev_block
= inftl
->VUtable
[thisVUC
];
596 if (prev_block
< inftl
->nb_blocks
)
597 prev_block
-= inftl
->firstEUN
;
599 parity
= (nrbits(thisVUC
, 16) & 0x1) ? 0x1 : 0;
600 parity
|= (nrbits(prev_block
, 16) & 0x1) ? 0x2 : 0;
601 parity
|= (nrbits(anac
, 8) & 0x1) ? 0x4 : 0;
602 parity
|= (nrbits(nacs
, 8) & 0x1) ? 0x8 : 0;
604 oob
.u
.a
.virtualUnitNo
= cpu_to_le16(thisVUC
);
605 oob
.u
.a
.prevUnitNo
= cpu_to_le16(prev_block
);
608 oob
.u
.a
.parityPerField
= parity
;
609 oob
.u
.a
.discarded
= 0xaa;
611 inftl_write_oob(mtd
, writeEUN
* inftl
->EraseSize
+ 8, 8,
612 &retlen
, (char *)&oob
.u
);
614 /* Also back up header... */
615 oob
.u
.b
.virtualUnitNo
= cpu_to_le16(thisVUC
);
616 oob
.u
.b
.prevUnitNo
= cpu_to_le16(prev_block
);
619 oob
.u
.b
.parityPerField
= parity
;
620 oob
.u
.b
.discarded
= 0xaa;
622 inftl_write_oob(mtd
, writeEUN
* inftl
->EraseSize
+
623 SECTORSIZE
* 4 + 8, 8, &retlen
, (char *)&oob
.u
);
625 inftl
->PUtable
[writeEUN
] = inftl
->VUtable
[thisVUC
];
626 inftl
->VUtable
[thisVUC
] = writeEUN
;
628 inftl
->numfreeEUNs
--;
633 printk(KERN_WARNING
"INFTL: error folding to make room for Virtual "
634 "Unit Chain 0x%x\n", thisVUC
);
639 * Given a Virtual Unit Chain, see if it can be deleted, and if so do it.
641 static void INFTL_trydeletechain(struct INFTLrecord
*inftl
, unsigned thisVUC
)
643 struct mtd_info
*mtd
= inftl
->mbd
.mtd
;
644 unsigned char BlockUsed
[MAX_SECTORS_PER_UNIT
];
645 unsigned char BlockDeleted
[MAX_SECTORS_PER_UNIT
];
646 unsigned int thisEUN
, status
;
648 struct inftl_bci bci
;
651 DEBUG(MTD_DEBUG_LEVEL3
, "INFTL: INFTL_trydeletechain(inftl=%p,"
652 "thisVUC=%d)\n", inftl
, thisVUC
);
654 memset(BlockUsed
, 0, sizeof(BlockUsed
));
655 memset(BlockDeleted
, 0, sizeof(BlockDeleted
));
657 thisEUN
= inftl
->VUtable
[thisVUC
];
658 if (thisEUN
== BLOCK_NIL
) {
659 printk(KERN_WARNING
"INFTL: trying to delete non-existent "
660 "Virtual Unit Chain %d!\n", thisVUC
);
665 * Scan through the Erase Units to determine whether any data is in
666 * each of the 512-byte blocks within the Chain.
669 while (thisEUN
< inftl
->nb_blocks
) {
670 for (block
= 0; block
< inftl
->EraseSize
/SECTORSIZE
; block
++) {
671 if (BlockUsed
[block
] || BlockDeleted
[block
])
674 if (inftl_read_oob(mtd
, (thisEUN
* inftl
->EraseSize
)
675 + (block
* SECTORSIZE
), 8 , &retlen
,
677 status
= SECTOR_IGNORE
;
679 status
= bci
.Status
| bci
.Status1
;
686 BlockUsed
[block
] = 1;
689 BlockDeleted
[block
] = 1;
692 printk(KERN_WARNING
"INFTL: unknown status "
693 "for block %d in EUN %d: 0x%x\n",
694 block
, thisEUN
, status
);
699 printk(KERN_WARNING
"INFTL: infinite loop in Virtual "
700 "Unit Chain 0x%x\n", thisVUC
);
704 thisEUN
= inftl
->PUtable
[thisEUN
];
707 for (block
= 0; block
< inftl
->EraseSize
/SECTORSIZE
; block
++)
708 if (BlockUsed
[block
])
712 * For each block in the chain free it and make it available
713 * for future use. Erase from the oldest unit first.
715 DEBUG(MTD_DEBUG_LEVEL1
, "INFTL: deleting empty VUC %d\n", thisVUC
);
718 u16
*prevEUN
= &inftl
->VUtable
[thisVUC
];
721 /* If the chain is all gone already, we're done */
722 if (thisEUN
== BLOCK_NIL
) {
723 DEBUG(MTD_DEBUG_LEVEL2
, "INFTL: Empty VUC %d for deletion was already absent\n", thisEUN
);
727 /* Find oldest unit in chain. */
728 while (inftl
->PUtable
[thisEUN
] != BLOCK_NIL
) {
729 BUG_ON(thisEUN
>= inftl
->nb_blocks
);
731 prevEUN
= &inftl
->PUtable
[thisEUN
];
735 DEBUG(MTD_DEBUG_LEVEL3
, "Deleting EUN %d from VUC %d\n",
738 if (INFTL_formatblock(inftl
, thisEUN
) < 0) {
740 * Could not erase : mark block as reserved.
742 inftl
->PUtable
[thisEUN
] = BLOCK_RESERVED
;
744 /* Correctly erased : mark it as free */
745 inftl
->PUtable
[thisEUN
] = BLOCK_FREE
;
746 inftl
->numfreeEUNs
++;
749 /* Now sort out whatever was pointing to it... */
750 *prevEUN
= BLOCK_NIL
;
752 /* Ideally we'd actually be responsive to new
753 requests while we're doing this -- if there's
754 free space why should others be made to wait? */
758 inftl
->VUtable
[thisVUC
] = BLOCK_NIL
;
761 static int INFTL_deleteblock(struct INFTLrecord
*inftl
, unsigned block
)
763 unsigned int thisEUN
= inftl
->VUtable
[block
/ (inftl
->EraseSize
/ SECTORSIZE
)];
764 unsigned long blockofs
= (block
* SECTORSIZE
) & (inftl
->EraseSize
- 1);
765 struct mtd_info
*mtd
= inftl
->mbd
.mtd
;
767 int silly
= MAX_LOOPS
;
769 struct inftl_bci bci
;
771 DEBUG(MTD_DEBUG_LEVEL3
, "INFTL: INFTL_deleteblock(inftl=%p,"
772 "block=%d)\n", inftl
, block
);
774 while (thisEUN
< inftl
->nb_blocks
) {
775 if (inftl_read_oob(mtd
, (thisEUN
* inftl
->EraseSize
) +
776 blockofs
, 8, &retlen
, (char *)&bci
) < 0)
777 status
= SECTOR_IGNORE
;
779 status
= bci
.Status
| bci
.Status1
;
791 printk(KERN_WARNING
"INFTL: unknown status for "
792 "block %d in EUN %d: 0x%x\n",
793 block
, thisEUN
, status
);
798 printk(KERN_WARNING
"INFTL: infinite loop in Virtual "
800 block
/ (inftl
->EraseSize
/ SECTORSIZE
));
803 thisEUN
= inftl
->PUtable
[thisEUN
];
807 if (thisEUN
!= BLOCK_NIL
) {
808 loff_t ptr
= (thisEUN
* inftl
->EraseSize
) + blockofs
;
810 if (inftl_read_oob(mtd
, ptr
, 8, &retlen
, (char *)&bci
) < 0)
812 bci
.Status
= bci
.Status1
= SECTOR_DELETED
;
813 if (inftl_write_oob(mtd
, ptr
, 8, &retlen
, (char *)&bci
) < 0)
815 INFTL_trydeletechain(inftl
, block
/ (inftl
->EraseSize
/ SECTORSIZE
));
820 static int inftl_writeblock(struct mtd_blktrans_dev
*mbd
, unsigned long block
,
823 struct INFTLrecord
*inftl
= (void *)mbd
;
824 unsigned int writeEUN
;
825 unsigned long blockofs
= (block
* SECTORSIZE
) & (inftl
->EraseSize
- 1);
827 struct inftl_oob oob
;
830 DEBUG(MTD_DEBUG_LEVEL3
, "INFTL: inftl_writeblock(inftl=%p,block=%ld,"
831 "buffer=%p)\n", inftl
, block
, buffer
);
833 /* Is block all zero? */
834 pend
= buffer
+ SECTORSIZE
;
835 for (p
= buffer
; p
< pend
&& !*p
; p
++)
839 writeEUN
= INFTL_findwriteunit(inftl
, block
);
841 if (writeEUN
== BLOCK_NIL
) {
842 printk(KERN_WARNING
"inftl_writeblock(): cannot find "
843 "block to write to\n");
845 * If we _still_ haven't got a block to use,
851 memset(&oob
, 0xff, sizeof(struct inftl_oob
));
852 oob
.b
.Status
= oob
.b
.Status1
= SECTOR_USED
;
854 inftl_write(inftl
->mbd
.mtd
, (writeEUN
* inftl
->EraseSize
) +
855 blockofs
, SECTORSIZE
, &retlen
, (char *)buffer
,
858 * need to write SECTOR_USED flags since they are not written
862 INFTL_deleteblock(inftl
, block
);
868 static int inftl_readblock(struct mtd_blktrans_dev
*mbd
, unsigned long block
,
871 struct INFTLrecord
*inftl
= (void *)mbd
;
872 unsigned int thisEUN
= inftl
->VUtable
[block
/ (inftl
->EraseSize
/ SECTORSIZE
)];
873 unsigned long blockofs
= (block
* SECTORSIZE
) & (inftl
->EraseSize
- 1);
874 struct mtd_info
*mtd
= inftl
->mbd
.mtd
;
876 int silly
= MAX_LOOPS
;
877 struct inftl_bci bci
;
880 DEBUG(MTD_DEBUG_LEVEL3
, "INFTL: inftl_readblock(inftl=%p,block=%ld,"
881 "buffer=%p)\n", inftl
, block
, buffer
);
883 while (thisEUN
< inftl
->nb_blocks
) {
884 if (inftl_read_oob(mtd
, (thisEUN
* inftl
->EraseSize
) +
885 blockofs
, 8, &retlen
, (char *)&bci
) < 0)
886 status
= SECTOR_IGNORE
;
888 status
= bci
.Status
| bci
.Status1
;
900 printk(KERN_WARNING
"INFTL: unknown status for "
901 "block %ld in EUN %d: 0x%04x\n",
902 block
, thisEUN
, status
);
907 printk(KERN_WARNING
"INFTL: infinite loop in "
908 "Virtual Unit Chain 0x%lx\n",
909 block
/ (inftl
->EraseSize
/ SECTORSIZE
));
913 thisEUN
= inftl
->PUtable
[thisEUN
];
917 if (thisEUN
== BLOCK_NIL
) {
918 /* The requested block is not on the media, return all 0x00 */
919 memset(buffer
, 0, SECTORSIZE
);
922 loff_t ptr
= (thisEUN
* inftl
->EraseSize
) + blockofs
;
923 int ret
= mtd
->read(mtd
, ptr
, SECTORSIZE
, &retlen
, buffer
);
925 /* Handle corrected bit flips gracefully */
926 if (ret
< 0 && ret
!= -EUCLEAN
)
932 static int inftl_getgeo(struct mtd_blktrans_dev
*dev
, struct hd_geometry
*geo
)
934 struct INFTLrecord
*inftl
= (void *)dev
;
936 geo
->heads
= inftl
->heads
;
937 geo
->sectors
= inftl
->sectors
;
938 geo
->cylinders
= inftl
->cylinders
;
943 static struct mtd_blktrans_ops inftl_tr
= {
945 .major
= INFTL_MAJOR
,
946 .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 return register_mtd_blktrans(&inftl_tr
);
961 static void __exit
cleanup_inftl(void)
963 deregister_mtd_blktrans(&inftl_tr
);
966 module_init(init_inftl
);
967 module_exit(cleanup_inftl
);
969 MODULE_LICENSE("GPL");
970 MODULE_AUTHOR("Greg Ungerer <gerg@snapgear.com>, David Woodhouse <dwmw2@infradead.org>, Fabrice Bellard <fabrice.bellard@netgem.com> et al.");
971 MODULE_DESCRIPTION("Support code for Inverse Flash Translation Layer, used on M-Systems DiskOnChip 2000, Millennium and Millennium Plus");