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
= kzalloc(sizeof(*inftl
), GFP_KERNEL
);
73 printk(KERN_WARNING
"INFTL: Out of memory for data structures\n");
78 inftl
->mbd
.devnum
= -1;
82 if (INFTL_mount(inftl
) < 0) {
83 printk(KERN_WARNING
"INFTL: could not mount device\n");
88 /* OK, it's a new one. Set up all the data structures. */
90 /* Calculate geometry */
91 inftl
->cylinders
= 1024;
94 temp
= inftl
->cylinders
* inftl
->heads
;
95 inftl
->sectors
= inftl
->mbd
.size
/ temp
;
96 if (inftl
->mbd
.size
% temp
) {
98 temp
= inftl
->cylinders
* inftl
->sectors
;
99 inftl
->heads
= inftl
->mbd
.size
/ temp
;
101 if (inftl
->mbd
.size
% temp
) {
103 temp
= inftl
->heads
* inftl
->sectors
;
104 inftl
->cylinders
= inftl
->mbd
.size
/ temp
;
108 if (inftl
->mbd
.size
!= inftl
->heads
* inftl
->cylinders
* inftl
->sectors
) {
111 mbd.size == heads * cylinders * sectors
113 printk(KERN_WARNING
"INFTL: cannot calculate a geometry to "
114 "match size of 0x%lx.\n", inftl
->mbd
.size
);
115 printk(KERN_WARNING
"INFTL: using C:%d H:%d S:%d "
116 "(== 0x%lx sects)\n",
117 inftl
->cylinders
, inftl
->heads
, inftl
->sectors
,
118 (long)inftl
->cylinders
* (long)inftl
->heads
*
119 (long)inftl
->sectors
);
122 if (add_mtd_blktrans_dev(&inftl
->mbd
)) {
123 kfree(inftl
->PUtable
);
124 kfree(inftl
->VUtable
);
129 printk(KERN_INFO
"INFTL: Found new inftl%c\n", inftl
->mbd
.devnum
+ 'a');
134 static void inftl_remove_dev(struct mtd_blktrans_dev
*dev
)
136 struct INFTLrecord
*inftl
= (void *)dev
;
138 DEBUG(MTD_DEBUG_LEVEL3
, "INFTL: remove_dev (i=%d)\n", dev
->devnum
);
140 del_mtd_blktrans_dev(dev
);
142 kfree(inftl
->PUtable
);
143 kfree(inftl
->VUtable
);
148 * Actual INFTL access routines.
152 * Read oob data from flash
154 int inftl_read_oob(struct mtd_info
*mtd
, loff_t offs
, size_t len
,
155 size_t *retlen
, uint8_t *buf
)
157 struct mtd_oob_ops ops
;
160 ops
.mode
= MTD_OOB_PLACE
;
161 ops
.ooboffs
= offs
& (mtd
->writesize
- 1);
166 res
= mtd
->read_oob(mtd
, offs
& ~(mtd
->writesize
- 1), &ops
);
167 *retlen
= ops
.oobretlen
;
172 * Write oob data to flash
174 int inftl_write_oob(struct mtd_info
*mtd
, loff_t offs
, size_t len
,
175 size_t *retlen
, uint8_t *buf
)
177 struct mtd_oob_ops ops
;
180 ops
.mode
= MTD_OOB_PLACE
;
181 ops
.ooboffs
= offs
& (mtd
->writesize
- 1);
186 res
= mtd
->write_oob(mtd
, offs
& ~(mtd
->writesize
- 1), &ops
);
187 *retlen
= ops
.oobretlen
;
192 * Write data and oob to flash
194 static int inftl_write(struct mtd_info
*mtd
, loff_t offs
, size_t len
,
195 size_t *retlen
, uint8_t *buf
, uint8_t *oob
)
197 struct mtd_oob_ops ops
;
200 ops
.mode
= MTD_OOB_PLACE
;
202 ops
.ooblen
= mtd
->oobsize
;
207 res
= mtd
->write_oob(mtd
, offs
& ~(mtd
->writesize
- 1), &ops
);
208 *retlen
= ops
.retlen
;
213 * INFTL_findfreeblock: Find a free Erase Unit on the INFTL partition.
214 * This function is used when the give Virtual Unit Chain.
216 static u16
INFTL_findfreeblock(struct INFTLrecord
*inftl
, int desperate
)
218 u16 pot
= inftl
->LastFreeEUN
;
219 int silly
= inftl
->nb_blocks
;
221 DEBUG(MTD_DEBUG_LEVEL3
, "INFTL: INFTL_findfreeblock(inftl=%p,"
222 "desperate=%d)\n", inftl
, desperate
);
225 * Normally, we force a fold to happen before we run out of free
228 if (!desperate
&& inftl
->numfreeEUNs
< 2) {
229 DEBUG(MTD_DEBUG_LEVEL1
, "INFTL: there are too few free "
230 "EUNs (%d)\n", inftl
->numfreeEUNs
);
234 /* Scan for a free block */
236 if (inftl
->PUtable
[pot
] == BLOCK_FREE
) {
237 inftl
->LastFreeEUN
= pot
;
241 if (++pot
> inftl
->lastEUN
)
245 printk(KERN_WARNING
"INFTL: no free blocks found! "
246 "EUN range = %d - %d\n", 0, inftl
->LastFreeEUN
);
249 } while (pot
!= inftl
->LastFreeEUN
);
254 static u16
INFTL_foldchain(struct INFTLrecord
*inftl
, unsigned thisVUC
, unsigned pendingblock
)
256 u16 BlockMap
[MAX_SECTORS_PER_UNIT
];
257 unsigned char BlockDeleted
[MAX_SECTORS_PER_UNIT
];
258 unsigned int thisEUN
, prevEUN
, status
;
259 struct mtd_info
*mtd
= inftl
->mbd
.mtd
;
261 unsigned int targetEUN
;
262 struct inftl_oob oob
;
265 DEBUG(MTD_DEBUG_LEVEL3
, "INFTL: INFTL_foldchain(inftl=%p,thisVUC=%d,"
266 "pending=%d)\n", inftl
, thisVUC
, pendingblock
);
268 memset(BlockMap
, 0xff, sizeof(BlockMap
));
269 memset(BlockDeleted
, 0, sizeof(BlockDeleted
));
271 thisEUN
= targetEUN
= inftl
->VUtable
[thisVUC
];
273 if (thisEUN
== BLOCK_NIL
) {
274 printk(KERN_WARNING
"INFTL: trying to fold non-existent "
275 "Virtual Unit Chain %d!\n", thisVUC
);
280 * Scan to find the Erase Unit which holds the actual data for each
281 * 512-byte block within the Chain.
284 while (thisEUN
< inftl
->nb_blocks
) {
285 for (block
= 0; block
< inftl
->EraseSize
/SECTORSIZE
; block
++) {
286 if ((BlockMap
[block
] != 0xffff) || BlockDeleted
[block
])
289 if (inftl_read_oob(mtd
, (thisEUN
* inftl
->EraseSize
)
290 + (block
* SECTORSIZE
), 16, &retlen
,
292 status
= SECTOR_IGNORE
;
294 status
= oob
.b
.Status
| oob
.b
.Status1
;
301 BlockMap
[block
] = thisEUN
;
304 BlockDeleted
[block
] = 1;
307 printk(KERN_WARNING
"INFTL: unknown status "
308 "for block %d in EUN %d: %x\n",
309 block
, thisEUN
, status
);
315 printk(KERN_WARNING
"INFTL: infinite loop in Virtual "
316 "Unit Chain 0x%x\n", thisVUC
);
320 thisEUN
= inftl
->PUtable
[thisEUN
];
324 * OK. We now know the location of every block in the Virtual Unit
325 * Chain, and the Erase Unit into which we are supposed to be copying.
328 DEBUG(MTD_DEBUG_LEVEL1
, "INFTL: folding chain %d into unit %d\n",
331 for (block
= 0; block
< inftl
->EraseSize
/SECTORSIZE
; block
++) {
332 unsigned char movebuf
[SECTORSIZE
];
336 * If it's in the target EUN already, or if it's pending write,
339 if (BlockMap
[block
] == targetEUN
|| (pendingblock
==
340 (thisVUC
* (inftl
->EraseSize
/ SECTORSIZE
) + block
))) {
345 * Copy only in non free block (free blocks can only
346 * happen in case of media errors or deleted blocks).
348 if (BlockMap
[block
] == BLOCK_NIL
)
351 ret
= mtd
->read(mtd
, (inftl
->EraseSize
* BlockMap
[block
]) +
352 (block
* SECTORSIZE
), SECTORSIZE
, &retlen
,
354 if (ret
< 0 && ret
!= -EUCLEAN
) {
356 (inftl
->EraseSize
* BlockMap
[block
]) +
357 (block
* SECTORSIZE
), SECTORSIZE
,
360 DEBUG(MTD_DEBUG_LEVEL1
, "INFTL: error went "
363 memset(&oob
, 0xff, sizeof(struct inftl_oob
));
364 oob
.b
.Status
= oob
.b
.Status1
= SECTOR_USED
;
366 inftl_write(inftl
->mbd
.mtd
, (inftl
->EraseSize
* targetEUN
) +
367 (block
* SECTORSIZE
), SECTORSIZE
, &retlen
,
368 movebuf
, (char *)&oob
);
372 * Newest unit in chain now contains data from _all_ older units.
373 * So go through and erase each unit in chain, oldest first. (This
374 * is important, by doing oldest first if we crash/reboot then it
375 * it is relatively simple to clean up the mess).
377 DEBUG(MTD_DEBUG_LEVEL1
, "INFTL: want to erase virtual chain %d\n",
381 /* Find oldest unit in chain. */
382 thisEUN
= inftl
->VUtable
[thisVUC
];
384 while (inftl
->PUtable
[thisEUN
] != BLOCK_NIL
) {
386 thisEUN
= inftl
->PUtable
[thisEUN
];
389 /* Check if we are all done */
390 if (thisEUN
== targetEUN
)
393 if (INFTL_formatblock(inftl
, thisEUN
) < 0) {
395 * Could not erase : mark block as reserved.
397 inftl
->PUtable
[thisEUN
] = BLOCK_RESERVED
;
399 /* Correctly erased : mark it as free */
400 inftl
->PUtable
[thisEUN
] = BLOCK_FREE
;
401 inftl
->PUtable
[prevEUN
] = BLOCK_NIL
;
402 inftl
->numfreeEUNs
++;
409 static u16
INFTL_makefreeblock(struct INFTLrecord
*inftl
, unsigned pendingblock
)
412 * This is the part that needs some cleverness applied.
413 * For now, I'm doing the minimum applicable to actually
414 * get the thing to work.
415 * Wear-levelling and other clever stuff needs to be implemented
416 * and we also need to do some assessment of the results when
417 * the system loses power half-way through the routine.
419 u16 LongestChain
= 0;
420 u16 ChainLength
= 0, thislen
;
423 DEBUG(MTD_DEBUG_LEVEL3
, "INFTL: INFTL_makefreeblock(inftl=%p,"
424 "pending=%d)\n", inftl
, pendingblock
);
426 for (chain
= 0; chain
< inftl
->nb_blocks
; chain
++) {
427 EUN
= inftl
->VUtable
[chain
];
430 while (EUN
<= inftl
->lastEUN
) {
432 EUN
= inftl
->PUtable
[EUN
];
433 if (thislen
> 0xff00) {
434 printk(KERN_WARNING
"INFTL: endless loop in "
435 "Virtual Chain %d: Unit %x\n",
438 * Actually, don't return failure.
439 * Just ignore this chain and get on with it.
446 if (thislen
> ChainLength
) {
447 ChainLength
= thislen
;
448 LongestChain
= chain
;
452 if (ChainLength
< 2) {
453 printk(KERN_WARNING
"INFTL: no Virtual Unit Chains available "
454 "for folding. Failing request\n");
458 return INFTL_foldchain(inftl
, LongestChain
, pendingblock
);
461 static int nrbits(unsigned int val
, int bitcount
)
465 for (i
= 0; (i
< bitcount
); i
++)
466 total
+= (((0x1 << i
) & val
) ? 1 : 0);
471 * INFTL_findwriteunit: Return the unit number into which we can write
472 * for this block. Make it available if it isn't already.
474 static inline u16
INFTL_findwriteunit(struct INFTLrecord
*inftl
, unsigned block
)
476 unsigned int thisVUC
= block
/ (inftl
->EraseSize
/ SECTORSIZE
);
477 unsigned int thisEUN
, writeEUN
, prev_block
, status
;
478 unsigned long blockofs
= (block
* SECTORSIZE
) & (inftl
->EraseSize
-1);
479 struct mtd_info
*mtd
= inftl
->mbd
.mtd
;
480 struct inftl_oob oob
;
481 struct inftl_bci bci
;
482 unsigned char anac
, nacs
, parity
;
484 int silly
, silly2
= 3;
486 DEBUG(MTD_DEBUG_LEVEL3
, "INFTL: INFTL_findwriteunit(inftl=%p,"
487 "block=%d)\n", inftl
, block
);
491 * Scan the media to find a unit in the VUC which has
492 * a free space for the block in question.
494 writeEUN
= BLOCK_NIL
;
495 thisEUN
= inftl
->VUtable
[thisVUC
];
498 while (thisEUN
<= inftl
->lastEUN
) {
499 inftl_read_oob(mtd
, (thisEUN
* inftl
->EraseSize
) +
500 blockofs
, 8, &retlen
, (char *)&bci
);
502 status
= bci
.Status
| bci
.Status1
;
503 DEBUG(MTD_DEBUG_LEVEL3
, "INFTL: status of block %d in "
504 "EUN %d is %x\n", block
, writeEUN
, status
);
512 /* Can't go any further */
518 * Invalid block. Don't use it any more.
525 printk(KERN_WARNING
"INFTL: infinite loop in "
526 "Virtual Unit Chain 0x%x\n", thisVUC
);
530 /* Skip to next block in chain */
531 thisEUN
= inftl
->PUtable
[thisEUN
];
535 if (writeEUN
!= BLOCK_NIL
)
540 * OK. We didn't find one in the existing chain, or there
541 * is no existing chain. Allocate a new one.
543 writeEUN
= INFTL_findfreeblock(inftl
, 0);
545 if (writeEUN
== BLOCK_NIL
) {
547 * That didn't work - there were no free blocks just
548 * waiting to be picked up. We're going to have to fold
549 * a chain to make room.
551 thisEUN
= INFTL_makefreeblock(inftl
, 0xffff);
554 * Hopefully we free something, lets try again.
555 * This time we are desperate...
557 DEBUG(MTD_DEBUG_LEVEL1
, "INFTL: using desperate==1 "
558 "to find free EUN to accommodate write to "
559 "VUC %d\n", thisVUC
);
560 writeEUN
= INFTL_findfreeblock(inftl
, 1);
561 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
"INFTL: cannot make free "
572 INFTL_dumptables(inftl
);
573 INFTL_dumpVUchains(inftl
);
580 * Insert new block into virtual chain. Firstly update the
581 * block headers in flash...
585 thisEUN
= inftl
->VUtable
[thisVUC
];
586 if (thisEUN
!= BLOCK_NIL
) {
587 inftl_read_oob(mtd
, thisEUN
* inftl
->EraseSize
588 + 8, 8, &retlen
, (char *)&oob
.u
);
589 anac
= oob
.u
.a
.ANAC
+ 1;
590 nacs
= oob
.u
.a
.NACs
+ 1;
593 prev_block
= inftl
->VUtable
[thisVUC
];
594 if (prev_block
< inftl
->nb_blocks
)
595 prev_block
-= inftl
->firstEUN
;
597 parity
= (nrbits(thisVUC
, 16) & 0x1) ? 0x1 : 0;
598 parity
|= (nrbits(prev_block
, 16) & 0x1) ? 0x2 : 0;
599 parity
|= (nrbits(anac
, 8) & 0x1) ? 0x4 : 0;
600 parity
|= (nrbits(nacs
, 8) & 0x1) ? 0x8 : 0;
602 oob
.u
.a
.virtualUnitNo
= cpu_to_le16(thisVUC
);
603 oob
.u
.a
.prevUnitNo
= cpu_to_le16(prev_block
);
606 oob
.u
.a
.parityPerField
= parity
;
607 oob
.u
.a
.discarded
= 0xaa;
609 inftl_write_oob(mtd
, writeEUN
* inftl
->EraseSize
+ 8, 8,
610 &retlen
, (char *)&oob
.u
);
612 /* Also back up header... */
613 oob
.u
.b
.virtualUnitNo
= cpu_to_le16(thisVUC
);
614 oob
.u
.b
.prevUnitNo
= cpu_to_le16(prev_block
);
617 oob
.u
.b
.parityPerField
= parity
;
618 oob
.u
.b
.discarded
= 0xaa;
620 inftl_write_oob(mtd
, writeEUN
* inftl
->EraseSize
+
621 SECTORSIZE
* 4 + 8, 8, &retlen
, (char *)&oob
.u
);
623 inftl
->PUtable
[writeEUN
] = inftl
->VUtable
[thisVUC
];
624 inftl
->VUtable
[thisVUC
] = writeEUN
;
626 inftl
->numfreeEUNs
--;
631 printk(KERN_WARNING
"INFTL: error folding to make room for Virtual "
632 "Unit Chain 0x%x\n", thisVUC
);
637 * Given a Virtual Unit Chain, see if it can be deleted, and if so do it.
639 static void INFTL_trydeletechain(struct INFTLrecord
*inftl
, unsigned thisVUC
)
641 struct mtd_info
*mtd
= inftl
->mbd
.mtd
;
642 unsigned char BlockUsed
[MAX_SECTORS_PER_UNIT
];
643 unsigned char BlockDeleted
[MAX_SECTORS_PER_UNIT
];
644 unsigned int thisEUN
, status
;
646 struct inftl_bci bci
;
649 DEBUG(MTD_DEBUG_LEVEL3
, "INFTL: INFTL_trydeletechain(inftl=%p,"
650 "thisVUC=%d)\n", inftl
, thisVUC
);
652 memset(BlockUsed
, 0, sizeof(BlockUsed
));
653 memset(BlockDeleted
, 0, sizeof(BlockDeleted
));
655 thisEUN
= inftl
->VUtable
[thisVUC
];
656 if (thisEUN
== BLOCK_NIL
) {
657 printk(KERN_WARNING
"INFTL: trying to delete non-existent "
658 "Virtual Unit Chain %d!\n", thisVUC
);
663 * Scan through the Erase Units to determine whether any data is in
664 * each of the 512-byte blocks within the Chain.
667 while (thisEUN
< inftl
->nb_blocks
) {
668 for (block
= 0; block
< inftl
->EraseSize
/SECTORSIZE
; block
++) {
669 if (BlockUsed
[block
] || BlockDeleted
[block
])
672 if (inftl_read_oob(mtd
, (thisEUN
* inftl
->EraseSize
)
673 + (block
* SECTORSIZE
), 8 , &retlen
,
675 status
= SECTOR_IGNORE
;
677 status
= bci
.Status
| bci
.Status1
;
684 BlockUsed
[block
] = 1;
687 BlockDeleted
[block
] = 1;
690 printk(KERN_WARNING
"INFTL: unknown status "
691 "for block %d in EUN %d: 0x%x\n",
692 block
, thisEUN
, status
);
697 printk(KERN_WARNING
"INFTL: infinite loop in Virtual "
698 "Unit Chain 0x%x\n", thisVUC
);
702 thisEUN
= inftl
->PUtable
[thisEUN
];
705 for (block
= 0; block
< inftl
->EraseSize
/SECTORSIZE
; block
++)
706 if (BlockUsed
[block
])
710 * For each block in the chain free it and make it available
711 * for future use. Erase from the oldest unit first.
713 DEBUG(MTD_DEBUG_LEVEL1
, "INFTL: deleting empty VUC %d\n", thisVUC
);
716 u16
*prevEUN
= &inftl
->VUtable
[thisVUC
];
719 /* If the chain is all gone already, we're done */
720 if (thisEUN
== BLOCK_NIL
) {
721 DEBUG(MTD_DEBUG_LEVEL2
, "INFTL: Empty VUC %d for deletion was already absent\n", thisEUN
);
725 /* Find oldest unit in chain. */
726 while (inftl
->PUtable
[thisEUN
] != BLOCK_NIL
) {
727 BUG_ON(thisEUN
>= inftl
->nb_blocks
);
729 prevEUN
= &inftl
->PUtable
[thisEUN
];
733 DEBUG(MTD_DEBUG_LEVEL3
, "Deleting EUN %d from VUC %d\n",
736 if (INFTL_formatblock(inftl
, thisEUN
) < 0) {
738 * Could not erase : mark block as reserved.
740 inftl
->PUtable
[thisEUN
] = BLOCK_RESERVED
;
742 /* Correctly erased : mark it as free */
743 inftl
->PUtable
[thisEUN
] = BLOCK_FREE
;
744 inftl
->numfreeEUNs
++;
747 /* Now sort out whatever was pointing to it... */
748 *prevEUN
= BLOCK_NIL
;
750 /* Ideally we'd actually be responsive to new
751 requests while we're doing this -- if there's
752 free space why should others be made to wait? */
756 inftl
->VUtable
[thisVUC
] = BLOCK_NIL
;
759 static int INFTL_deleteblock(struct INFTLrecord
*inftl
, unsigned block
)
761 unsigned int thisEUN
= inftl
->VUtable
[block
/ (inftl
->EraseSize
/ SECTORSIZE
)];
762 unsigned long blockofs
= (block
* SECTORSIZE
) & (inftl
->EraseSize
- 1);
763 struct mtd_info
*mtd
= inftl
->mbd
.mtd
;
765 int silly
= MAX_LOOPS
;
767 struct inftl_bci bci
;
769 DEBUG(MTD_DEBUG_LEVEL3
, "INFTL: INFTL_deleteblock(inftl=%p,"
770 "block=%d)\n", inftl
, block
);
772 while (thisEUN
< inftl
->nb_blocks
) {
773 if (inftl_read_oob(mtd
, (thisEUN
* inftl
->EraseSize
) +
774 blockofs
, 8, &retlen
, (char *)&bci
) < 0)
775 status
= SECTOR_IGNORE
;
777 status
= bci
.Status
| bci
.Status1
;
789 printk(KERN_WARNING
"INFTL: unknown status for "
790 "block %d in EUN %d: 0x%x\n",
791 block
, thisEUN
, status
);
796 printk(KERN_WARNING
"INFTL: infinite loop in Virtual "
798 block
/ (inftl
->EraseSize
/ SECTORSIZE
));
801 thisEUN
= inftl
->PUtable
[thisEUN
];
805 if (thisEUN
!= BLOCK_NIL
) {
806 loff_t ptr
= (thisEUN
* inftl
->EraseSize
) + blockofs
;
808 if (inftl_read_oob(mtd
, ptr
, 8, &retlen
, (char *)&bci
) < 0)
810 bci
.Status
= bci
.Status1
= SECTOR_DELETED
;
811 if (inftl_write_oob(mtd
, ptr
, 8, &retlen
, (char *)&bci
) < 0)
813 INFTL_trydeletechain(inftl
, block
/ (inftl
->EraseSize
/ SECTORSIZE
));
818 static int inftl_writeblock(struct mtd_blktrans_dev
*mbd
, unsigned long block
,
821 struct INFTLrecord
*inftl
= (void *)mbd
;
822 unsigned int writeEUN
;
823 unsigned long blockofs
= (block
* SECTORSIZE
) & (inftl
->EraseSize
- 1);
825 struct inftl_oob oob
;
828 DEBUG(MTD_DEBUG_LEVEL3
, "INFTL: inftl_writeblock(inftl=%p,block=%ld,"
829 "buffer=%p)\n", inftl
, block
, buffer
);
831 /* Is block all zero? */
832 pend
= buffer
+ SECTORSIZE
;
833 for (p
= buffer
; p
< pend
&& !*p
; p
++)
837 writeEUN
= INFTL_findwriteunit(inftl
, block
);
839 if (writeEUN
== BLOCK_NIL
) {
840 printk(KERN_WARNING
"inftl_writeblock(): cannot find "
841 "block to write to\n");
843 * If we _still_ haven't got a block to use,
849 memset(&oob
, 0xff, sizeof(struct inftl_oob
));
850 oob
.b
.Status
= oob
.b
.Status1
= SECTOR_USED
;
852 inftl_write(inftl
->mbd
.mtd
, (writeEUN
* inftl
->EraseSize
) +
853 blockofs
, SECTORSIZE
, &retlen
, (char *)buffer
,
856 * need to write SECTOR_USED flags since they are not written
860 INFTL_deleteblock(inftl
, block
);
866 static int inftl_readblock(struct mtd_blktrans_dev
*mbd
, unsigned long block
,
869 struct INFTLrecord
*inftl
= (void *)mbd
;
870 unsigned int thisEUN
= inftl
->VUtable
[block
/ (inftl
->EraseSize
/ SECTORSIZE
)];
871 unsigned long blockofs
= (block
* SECTORSIZE
) & (inftl
->EraseSize
- 1);
872 struct mtd_info
*mtd
= inftl
->mbd
.mtd
;
874 int silly
= MAX_LOOPS
;
875 struct inftl_bci bci
;
878 DEBUG(MTD_DEBUG_LEVEL3
, "INFTL: inftl_readblock(inftl=%p,block=%ld,"
879 "buffer=%p)\n", inftl
, block
, buffer
);
881 while (thisEUN
< inftl
->nb_blocks
) {
882 if (inftl_read_oob(mtd
, (thisEUN
* inftl
->EraseSize
) +
883 blockofs
, 8, &retlen
, (char *)&bci
) < 0)
884 status
= SECTOR_IGNORE
;
886 status
= bci
.Status
| bci
.Status1
;
898 printk(KERN_WARNING
"INFTL: unknown status for "
899 "block %ld in EUN %d: 0x%04x\n",
900 block
, thisEUN
, status
);
905 printk(KERN_WARNING
"INFTL: infinite loop in "
906 "Virtual Unit Chain 0x%lx\n",
907 block
/ (inftl
->EraseSize
/ SECTORSIZE
));
911 thisEUN
= inftl
->PUtable
[thisEUN
];
915 if (thisEUN
== BLOCK_NIL
) {
916 /* The requested block is not on the media, return all 0x00 */
917 memset(buffer
, 0, SECTORSIZE
);
920 loff_t ptr
= (thisEUN
* inftl
->EraseSize
) + blockofs
;
921 int ret
= mtd
->read(mtd
, ptr
, SECTORSIZE
, &retlen
, buffer
);
923 /* Handle corrected bit flips gracefully */
924 if (ret
< 0 && ret
!= -EUCLEAN
)
930 static int inftl_getgeo(struct mtd_blktrans_dev
*dev
, struct hd_geometry
*geo
)
932 struct INFTLrecord
*inftl
= (void *)dev
;
934 geo
->heads
= inftl
->heads
;
935 geo
->sectors
= inftl
->sectors
;
936 geo
->cylinders
= inftl
->cylinders
;
941 static struct mtd_blktrans_ops inftl_tr
= {
943 .major
= INFTL_MAJOR
,
944 .part_bits
= INFTL_PARTN_BITS
,
946 .getgeo
= inftl_getgeo
,
947 .readsect
= inftl_readblock
,
948 .writesect
= inftl_writeblock
,
949 .add_mtd
= inftl_add_mtd
,
950 .remove_dev
= inftl_remove_dev
,
951 .owner
= THIS_MODULE
,
954 static int __init
init_inftl(void)
956 printk(KERN_INFO
"INFTL: inftlcore.c $Revision: 1.19 $, "
957 "inftlmount.c %s\n", inftlmountrev
);
959 return register_mtd_blktrans(&inftl_tr
);
962 static void __exit
cleanup_inftl(void)
964 deregister_mtd_blktrans(&inftl_tr
);
967 module_init(init_inftl
);
968 module_exit(cleanup_inftl
);
970 MODULE_LICENSE("GPL");
971 MODULE_AUTHOR("Greg Ungerer <gerg@snapgear.com>, David Woodhouse <dwmw2@infradead.org>, Fabrice Bellard <fabrice.bellard@netgem.com> et al.");
972 MODULE_DESCRIPTION("Support code for Inverse Flash Translation Layer, used on M-Systems DiskOnChip 2000, Millennium and Millennium Plus");