2 * inftlcore.c -- Linux driver for Inverse Flash Translation Layer (INFTL)
4 * Copyright © 2002, Greg Ungerer (gerg@snapgear.com)
6 * Based heavily on the nftlcore.c code which is:
7 * Copyright © 1999 Machine Vision Holdings, Inc.
8 * Copyright © 1999 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
);
145 * Actual INFTL access routines.
149 * Read oob data from flash
151 int inftl_read_oob(struct mtd_info
*mtd
, loff_t offs
, size_t len
,
152 size_t *retlen
, uint8_t *buf
)
154 struct mtd_oob_ops ops
;
157 ops
.mode
= MTD_OOB_PLACE
;
158 ops
.ooboffs
= offs
& (mtd
->writesize
- 1);
163 res
= mtd
->read_oob(mtd
, offs
& ~(mtd
->writesize
- 1), &ops
);
164 *retlen
= ops
.oobretlen
;
169 * Write oob data to flash
171 int inftl_write_oob(struct mtd_info
*mtd
, loff_t offs
, size_t len
,
172 size_t *retlen
, uint8_t *buf
)
174 struct mtd_oob_ops ops
;
177 ops
.mode
= MTD_OOB_PLACE
;
178 ops
.ooboffs
= offs
& (mtd
->writesize
- 1);
183 res
= mtd
->write_oob(mtd
, offs
& ~(mtd
->writesize
- 1), &ops
);
184 *retlen
= ops
.oobretlen
;
189 * Write data and oob to flash
191 static int inftl_write(struct mtd_info
*mtd
, loff_t offs
, size_t len
,
192 size_t *retlen
, uint8_t *buf
, uint8_t *oob
)
194 struct mtd_oob_ops ops
;
197 ops
.mode
= MTD_OOB_PLACE
;
199 ops
.ooblen
= mtd
->oobsize
;
204 res
= mtd
->write_oob(mtd
, offs
& ~(mtd
->writesize
- 1), &ops
);
205 *retlen
= ops
.retlen
;
210 * INFTL_findfreeblock: Find a free Erase Unit on the INFTL partition.
211 * This function is used when the give Virtual Unit Chain.
213 static u16
INFTL_findfreeblock(struct INFTLrecord
*inftl
, int desperate
)
215 u16 pot
= inftl
->LastFreeEUN
;
216 int silly
= inftl
->nb_blocks
;
218 DEBUG(MTD_DEBUG_LEVEL3
, "INFTL: INFTL_findfreeblock(inftl=%p,"
219 "desperate=%d)\n", inftl
, desperate
);
222 * Normally, we force a fold to happen before we run out of free
225 if (!desperate
&& inftl
->numfreeEUNs
< 2) {
226 DEBUG(MTD_DEBUG_LEVEL1
, "INFTL: there are too few free "
227 "EUNs (%d)\n", inftl
->numfreeEUNs
);
231 /* Scan for a free block */
233 if (inftl
->PUtable
[pot
] == BLOCK_FREE
) {
234 inftl
->LastFreeEUN
= pot
;
238 if (++pot
> inftl
->lastEUN
)
242 printk(KERN_WARNING
"INFTL: no free blocks found! "
243 "EUN range = %d - %d\n", 0, inftl
->LastFreeEUN
);
246 } while (pot
!= inftl
->LastFreeEUN
);
251 static u16
INFTL_foldchain(struct INFTLrecord
*inftl
, unsigned thisVUC
, unsigned pendingblock
)
253 u16 BlockMap
[MAX_SECTORS_PER_UNIT
];
254 unsigned char BlockDeleted
[MAX_SECTORS_PER_UNIT
];
255 unsigned int thisEUN
, prevEUN
, status
;
256 struct mtd_info
*mtd
= inftl
->mbd
.mtd
;
258 unsigned int targetEUN
;
259 struct inftl_oob oob
;
262 DEBUG(MTD_DEBUG_LEVEL3
, "INFTL: INFTL_foldchain(inftl=%p,thisVUC=%d,"
263 "pending=%d)\n", inftl
, thisVUC
, pendingblock
);
265 memset(BlockMap
, 0xff, sizeof(BlockMap
));
266 memset(BlockDeleted
, 0, sizeof(BlockDeleted
));
268 thisEUN
= targetEUN
= inftl
->VUtable
[thisVUC
];
270 if (thisEUN
== BLOCK_NIL
) {
271 printk(KERN_WARNING
"INFTL: trying to fold non-existent "
272 "Virtual Unit Chain %d!\n", thisVUC
);
277 * Scan to find the Erase Unit which holds the actual data for each
278 * 512-byte block within the Chain.
281 while (thisEUN
< inftl
->nb_blocks
) {
282 for (block
= 0; block
< inftl
->EraseSize
/SECTORSIZE
; block
++) {
283 if ((BlockMap
[block
] != BLOCK_NIL
) ||
287 if (inftl_read_oob(mtd
, (thisEUN
* inftl
->EraseSize
)
288 + (block
* SECTORSIZE
), 16, &retlen
,
290 status
= SECTOR_IGNORE
;
292 status
= oob
.b
.Status
| oob
.b
.Status1
;
299 BlockMap
[block
] = thisEUN
;
302 BlockDeleted
[block
] = 1;
305 printk(KERN_WARNING
"INFTL: unknown status "
306 "for block %d in EUN %d: %x\n",
307 block
, thisEUN
, status
);
313 printk(KERN_WARNING
"INFTL: infinite loop in Virtual "
314 "Unit Chain 0x%x\n", thisVUC
);
318 thisEUN
= inftl
->PUtable
[thisEUN
];
322 * OK. We now know the location of every block in the Virtual Unit
323 * Chain, and the Erase Unit into which we are supposed to be copying.
326 DEBUG(MTD_DEBUG_LEVEL1
, "INFTL: folding chain %d into unit %d\n",
329 for (block
= 0; block
< inftl
->EraseSize
/SECTORSIZE
; block
++) {
330 unsigned char movebuf
[SECTORSIZE
];
334 * If it's in the target EUN already, or if it's pending write,
337 if (BlockMap
[block
] == targetEUN
|| (pendingblock
==
338 (thisVUC
* (inftl
->EraseSize
/ SECTORSIZE
) + block
))) {
343 * Copy only in non free block (free blocks can only
344 * happen in case of media errors or deleted blocks).
346 if (BlockMap
[block
] == BLOCK_NIL
)
349 ret
= mtd
->read(mtd
, (inftl
->EraseSize
* BlockMap
[block
]) +
350 (block
* SECTORSIZE
), SECTORSIZE
, &retlen
,
352 if (ret
< 0 && ret
!= -EUCLEAN
) {
354 (inftl
->EraseSize
* BlockMap
[block
]) +
355 (block
* SECTORSIZE
), SECTORSIZE
,
358 DEBUG(MTD_DEBUG_LEVEL1
, "INFTL: error went "
361 memset(&oob
, 0xff, sizeof(struct inftl_oob
));
362 oob
.b
.Status
= oob
.b
.Status1
= SECTOR_USED
;
364 inftl_write(inftl
->mbd
.mtd
, (inftl
->EraseSize
* targetEUN
) +
365 (block
* SECTORSIZE
), SECTORSIZE
, &retlen
,
366 movebuf
, (char *)&oob
);
370 * Newest unit in chain now contains data from _all_ older units.
371 * So go through and erase each unit in chain, oldest first. (This
372 * is important, by doing oldest first if we crash/reboot then it
373 * it is relatively simple to clean up the mess).
375 DEBUG(MTD_DEBUG_LEVEL1
, "INFTL: want to erase virtual chain %d\n",
379 /* Find oldest unit in chain. */
380 thisEUN
= inftl
->VUtable
[thisVUC
];
382 while (inftl
->PUtable
[thisEUN
] != BLOCK_NIL
) {
384 thisEUN
= inftl
->PUtable
[thisEUN
];
387 /* Check if we are all done */
388 if (thisEUN
== targetEUN
)
391 /* Unlink the last block from the chain. */
392 inftl
->PUtable
[prevEUN
] = BLOCK_NIL
;
394 /* Now try to erase it. */
395 if (INFTL_formatblock(inftl
, thisEUN
) < 0) {
397 * Could not erase : mark block as reserved.
399 inftl
->PUtable
[thisEUN
] = BLOCK_RESERVED
;
401 /* Correctly erased : mark it as free */
402 inftl
->PUtable
[thisEUN
] = BLOCK_FREE
;
403 inftl
->numfreeEUNs
++;
410 static u16
INFTL_makefreeblock(struct INFTLrecord
*inftl
, unsigned pendingblock
)
413 * This is the part that needs some cleverness applied.
414 * For now, I'm doing the minimum applicable to actually
415 * get the thing to work.
416 * Wear-levelling and other clever stuff needs to be implemented
417 * and we also need to do some assessment of the results when
418 * the system loses power half-way through the routine.
420 u16 LongestChain
= 0;
421 u16 ChainLength
= 0, thislen
;
424 DEBUG(MTD_DEBUG_LEVEL3
, "INFTL: INFTL_makefreeblock(inftl=%p,"
425 "pending=%d)\n", inftl
, pendingblock
);
427 for (chain
= 0; chain
< inftl
->nb_blocks
; chain
++) {
428 EUN
= inftl
->VUtable
[chain
];
431 while (EUN
<= inftl
->lastEUN
) {
433 EUN
= inftl
->PUtable
[EUN
];
434 if (thislen
> 0xff00) {
435 printk(KERN_WARNING
"INFTL: endless loop in "
436 "Virtual Chain %d: Unit %x\n",
439 * Actually, don't return failure.
440 * Just ignore this chain and get on with it.
447 if (thislen
> ChainLength
) {
448 ChainLength
= thislen
;
449 LongestChain
= chain
;
453 if (ChainLength
< 2) {
454 printk(KERN_WARNING
"INFTL: no Virtual Unit Chains available "
455 "for folding. Failing request\n");
459 return INFTL_foldchain(inftl
, LongestChain
, pendingblock
);
462 static int nrbits(unsigned int val
, int bitcount
)
466 for (i
= 0; (i
< bitcount
); i
++)
467 total
+= (((0x1 << i
) & val
) ? 1 : 0);
472 * INFTL_findwriteunit: Return the unit number into which we can write
473 * for this block. Make it available if it isn't already.
475 static inline u16
INFTL_findwriteunit(struct INFTLrecord
*inftl
, unsigned block
)
477 unsigned int thisVUC
= block
/ (inftl
->EraseSize
/ SECTORSIZE
);
478 unsigned int thisEUN
, writeEUN
, prev_block
, status
;
479 unsigned long blockofs
= (block
* SECTORSIZE
) & (inftl
->EraseSize
-1);
480 struct mtd_info
*mtd
= inftl
->mbd
.mtd
;
481 struct inftl_oob oob
;
482 struct inftl_bci bci
;
483 unsigned char anac
, nacs
, parity
;
485 int silly
, silly2
= 3;
487 DEBUG(MTD_DEBUG_LEVEL3
, "INFTL: INFTL_findwriteunit(inftl=%p,"
488 "block=%d)\n", inftl
, block
);
492 * Scan the media to find a unit in the VUC which has
493 * a free space for the block in question.
495 writeEUN
= BLOCK_NIL
;
496 thisEUN
= inftl
->VUtable
[thisVUC
];
499 while (thisEUN
<= inftl
->lastEUN
) {
500 inftl_read_oob(mtd
, (thisEUN
* inftl
->EraseSize
) +
501 blockofs
, 8, &retlen
, (char *)&bci
);
503 status
= bci
.Status
| bci
.Status1
;
504 DEBUG(MTD_DEBUG_LEVEL3
, "INFTL: status of block %d in "
505 "EUN %d is %x\n", block
, writeEUN
, status
);
513 /* Can't go any further */
519 * Invalid block. Don't use it any more.
526 printk(KERN_WARNING
"INFTL: infinite loop in "
527 "Virtual Unit Chain 0x%x\n", thisVUC
);
531 /* Skip to next block in chain */
532 thisEUN
= inftl
->PUtable
[thisEUN
];
536 if (writeEUN
!= BLOCK_NIL
)
541 * OK. We didn't find one in the existing chain, or there
542 * is no existing chain. Allocate a new one.
544 writeEUN
= INFTL_findfreeblock(inftl
, 0);
546 if (writeEUN
== BLOCK_NIL
) {
548 * That didn't work - there were no free blocks just
549 * waiting to be picked up. We're going to have to fold
550 * a chain to make room.
552 thisEUN
= INFTL_makefreeblock(inftl
, block
);
555 * Hopefully we free something, lets try again.
556 * This time we are desperate...
558 DEBUG(MTD_DEBUG_LEVEL1
, "INFTL: using desperate==1 "
559 "to find free EUN to accommodate write to "
560 "VUC %d\n", thisVUC
);
561 writeEUN
= INFTL_findfreeblock(inftl
, 1);
562 if (writeEUN
== BLOCK_NIL
) {
564 * Ouch. This should never happen - we should
565 * always be able to make some room somehow.
566 * If we get here, we've allocated more storage
567 * space than actual media, or our makefreeblock
568 * routine is missing something.
570 printk(KERN_WARNING
"INFTL: cannot make free "
573 INFTL_dumptables(inftl
);
574 INFTL_dumpVUchains(inftl
);
581 * Insert new block into virtual chain. Firstly update the
582 * block headers in flash...
586 thisEUN
= inftl
->VUtable
[thisVUC
];
587 if (thisEUN
!= BLOCK_NIL
) {
588 inftl_read_oob(mtd
, thisEUN
* inftl
->EraseSize
589 + 8, 8, &retlen
, (char *)&oob
.u
);
590 anac
= oob
.u
.a
.ANAC
+ 1;
591 nacs
= oob
.u
.a
.NACs
+ 1;
594 prev_block
= inftl
->VUtable
[thisVUC
];
595 if (prev_block
< inftl
->nb_blocks
)
596 prev_block
-= inftl
->firstEUN
;
598 parity
= (nrbits(thisVUC
, 16) & 0x1) ? 0x1 : 0;
599 parity
|= (nrbits(prev_block
, 16) & 0x1) ? 0x2 : 0;
600 parity
|= (nrbits(anac
, 8) & 0x1) ? 0x4 : 0;
601 parity
|= (nrbits(nacs
, 8) & 0x1) ? 0x8 : 0;
603 oob
.u
.a
.virtualUnitNo
= cpu_to_le16(thisVUC
);
604 oob
.u
.a
.prevUnitNo
= cpu_to_le16(prev_block
);
607 oob
.u
.a
.parityPerField
= parity
;
608 oob
.u
.a
.discarded
= 0xaa;
610 inftl_write_oob(mtd
, writeEUN
* inftl
->EraseSize
+ 8, 8,
611 &retlen
, (char *)&oob
.u
);
613 /* Also back up header... */
614 oob
.u
.b
.virtualUnitNo
= cpu_to_le16(thisVUC
);
615 oob
.u
.b
.prevUnitNo
= cpu_to_le16(prev_block
);
618 oob
.u
.b
.parityPerField
= parity
;
619 oob
.u
.b
.discarded
= 0xaa;
621 inftl_write_oob(mtd
, writeEUN
* inftl
->EraseSize
+
622 SECTORSIZE
* 4 + 8, 8, &retlen
, (char *)&oob
.u
);
624 inftl
->PUtable
[writeEUN
] = inftl
->VUtable
[thisVUC
];
625 inftl
->VUtable
[thisVUC
] = writeEUN
;
627 inftl
->numfreeEUNs
--;
632 printk(KERN_WARNING
"INFTL: error folding to make room for Virtual "
633 "Unit Chain 0x%x\n", thisVUC
);
638 * Given a Virtual Unit Chain, see if it can be deleted, and if so do it.
640 static void INFTL_trydeletechain(struct INFTLrecord
*inftl
, unsigned thisVUC
)
642 struct mtd_info
*mtd
= inftl
->mbd
.mtd
;
643 unsigned char BlockUsed
[MAX_SECTORS_PER_UNIT
];
644 unsigned char BlockDeleted
[MAX_SECTORS_PER_UNIT
];
645 unsigned int thisEUN
, status
;
647 struct inftl_bci bci
;
650 DEBUG(MTD_DEBUG_LEVEL3
, "INFTL: INFTL_trydeletechain(inftl=%p,"
651 "thisVUC=%d)\n", inftl
, thisVUC
);
653 memset(BlockUsed
, 0, sizeof(BlockUsed
));
654 memset(BlockDeleted
, 0, sizeof(BlockDeleted
));
656 thisEUN
= inftl
->VUtable
[thisVUC
];
657 if (thisEUN
== BLOCK_NIL
) {
658 printk(KERN_WARNING
"INFTL: trying to delete non-existent "
659 "Virtual Unit Chain %d!\n", thisVUC
);
664 * Scan through the Erase Units to determine whether any data is in
665 * each of the 512-byte blocks within the Chain.
668 while (thisEUN
< inftl
->nb_blocks
) {
669 for (block
= 0; block
< inftl
->EraseSize
/SECTORSIZE
; block
++) {
670 if (BlockUsed
[block
] || BlockDeleted
[block
])
673 if (inftl_read_oob(mtd
, (thisEUN
* inftl
->EraseSize
)
674 + (block
* SECTORSIZE
), 8 , &retlen
,
676 status
= SECTOR_IGNORE
;
678 status
= bci
.Status
| bci
.Status1
;
685 BlockUsed
[block
] = 1;
688 BlockDeleted
[block
] = 1;
691 printk(KERN_WARNING
"INFTL: unknown status "
692 "for block %d in EUN %d: 0x%x\n",
693 block
, thisEUN
, status
);
698 printk(KERN_WARNING
"INFTL: infinite loop in Virtual "
699 "Unit Chain 0x%x\n", thisVUC
);
703 thisEUN
= inftl
->PUtable
[thisEUN
];
706 for (block
= 0; block
< inftl
->EraseSize
/SECTORSIZE
; block
++)
707 if (BlockUsed
[block
])
711 * For each block in the chain free it and make it available
712 * for future use. Erase from the oldest unit first.
714 DEBUG(MTD_DEBUG_LEVEL1
, "INFTL: deleting empty VUC %d\n", thisVUC
);
717 u16
*prevEUN
= &inftl
->VUtable
[thisVUC
];
720 /* If the chain is all gone already, we're done */
721 if (thisEUN
== BLOCK_NIL
) {
722 DEBUG(MTD_DEBUG_LEVEL2
, "INFTL: Empty VUC %d for deletion was already absent\n", thisEUN
);
726 /* Find oldest unit in chain. */
727 while (inftl
->PUtable
[thisEUN
] != BLOCK_NIL
) {
728 BUG_ON(thisEUN
>= inftl
->nb_blocks
);
730 prevEUN
= &inftl
->PUtable
[thisEUN
];
734 DEBUG(MTD_DEBUG_LEVEL3
, "Deleting EUN %d from VUC %d\n",
737 if (INFTL_formatblock(inftl
, thisEUN
) < 0) {
739 * Could not erase : mark block as reserved.
741 inftl
->PUtable
[thisEUN
] = BLOCK_RESERVED
;
743 /* Correctly erased : mark it as free */
744 inftl
->PUtable
[thisEUN
] = BLOCK_FREE
;
745 inftl
->numfreeEUNs
++;
748 /* Now sort out whatever was pointing to it... */
749 *prevEUN
= BLOCK_NIL
;
751 /* Ideally we'd actually be responsive to new
752 requests while we're doing this -- if there's
753 free space why should others be made to wait? */
757 inftl
->VUtable
[thisVUC
] = BLOCK_NIL
;
760 static int INFTL_deleteblock(struct INFTLrecord
*inftl
, unsigned block
)
762 unsigned int thisEUN
= inftl
->VUtable
[block
/ (inftl
->EraseSize
/ SECTORSIZE
)];
763 unsigned long blockofs
= (block
* SECTORSIZE
) & (inftl
->EraseSize
- 1);
764 struct mtd_info
*mtd
= inftl
->mbd
.mtd
;
766 int silly
= MAX_LOOPS
;
768 struct inftl_bci bci
;
770 DEBUG(MTD_DEBUG_LEVEL3
, "INFTL: INFTL_deleteblock(inftl=%p,"
771 "block=%d)\n", inftl
, block
);
773 while (thisEUN
< inftl
->nb_blocks
) {
774 if (inftl_read_oob(mtd
, (thisEUN
* inftl
->EraseSize
) +
775 blockofs
, 8, &retlen
, (char *)&bci
) < 0)
776 status
= SECTOR_IGNORE
;
778 status
= bci
.Status
| bci
.Status1
;
790 printk(KERN_WARNING
"INFTL: unknown status for "
791 "block %d in EUN %d: 0x%x\n",
792 block
, thisEUN
, status
);
797 printk(KERN_WARNING
"INFTL: infinite loop in Virtual "
799 block
/ (inftl
->EraseSize
/ SECTORSIZE
));
802 thisEUN
= inftl
->PUtable
[thisEUN
];
806 if (thisEUN
!= BLOCK_NIL
) {
807 loff_t ptr
= (thisEUN
* inftl
->EraseSize
) + blockofs
;
809 if (inftl_read_oob(mtd
, ptr
, 8, &retlen
, (char *)&bci
) < 0)
811 bci
.Status
= bci
.Status1
= SECTOR_DELETED
;
812 if (inftl_write_oob(mtd
, ptr
, 8, &retlen
, (char *)&bci
) < 0)
814 INFTL_trydeletechain(inftl
, block
/ (inftl
->EraseSize
/ SECTORSIZE
));
819 static int inftl_writeblock(struct mtd_blktrans_dev
*mbd
, unsigned long block
,
822 struct INFTLrecord
*inftl
= (void *)mbd
;
823 unsigned int writeEUN
;
824 unsigned long blockofs
= (block
* SECTORSIZE
) & (inftl
->EraseSize
- 1);
826 struct inftl_oob oob
;
829 DEBUG(MTD_DEBUG_LEVEL3
, "INFTL: inftl_writeblock(inftl=%p,block=%ld,"
830 "buffer=%p)\n", inftl
, block
, buffer
);
832 /* Is block all zero? */
833 pend
= buffer
+ SECTORSIZE
;
834 for (p
= buffer
; p
< pend
&& !*p
; p
++)
838 writeEUN
= INFTL_findwriteunit(inftl
, block
);
840 if (writeEUN
== BLOCK_NIL
) {
841 printk(KERN_WARNING
"inftl_writeblock(): cannot find "
842 "block to write to\n");
844 * If we _still_ haven't got a block to use,
850 memset(&oob
, 0xff, sizeof(struct inftl_oob
));
851 oob
.b
.Status
= oob
.b
.Status1
= SECTOR_USED
;
853 inftl_write(inftl
->mbd
.mtd
, (writeEUN
* inftl
->EraseSize
) +
854 blockofs
, SECTORSIZE
, &retlen
, (char *)buffer
,
857 * need to write SECTOR_USED flags since they are not written
861 INFTL_deleteblock(inftl
, block
);
867 static int inftl_readblock(struct mtd_blktrans_dev
*mbd
, unsigned long block
,
870 struct INFTLrecord
*inftl
= (void *)mbd
;
871 unsigned int thisEUN
= inftl
->VUtable
[block
/ (inftl
->EraseSize
/ SECTORSIZE
)];
872 unsigned long blockofs
= (block
* SECTORSIZE
) & (inftl
->EraseSize
- 1);
873 struct mtd_info
*mtd
= inftl
->mbd
.mtd
;
875 int silly
= MAX_LOOPS
;
876 struct inftl_bci bci
;
879 DEBUG(MTD_DEBUG_LEVEL3
, "INFTL: inftl_readblock(inftl=%p,block=%ld,"
880 "buffer=%p)\n", inftl
, block
, buffer
);
882 while (thisEUN
< inftl
->nb_blocks
) {
883 if (inftl_read_oob(mtd
, (thisEUN
* inftl
->EraseSize
) +
884 blockofs
, 8, &retlen
, (char *)&bci
) < 0)
885 status
= SECTOR_IGNORE
;
887 status
= bci
.Status
| bci
.Status1
;
899 printk(KERN_WARNING
"INFTL: unknown status for "
900 "block %ld in EUN %d: 0x%04x\n",
901 block
, thisEUN
, status
);
906 printk(KERN_WARNING
"INFTL: infinite loop in "
907 "Virtual Unit Chain 0x%lx\n",
908 block
/ (inftl
->EraseSize
/ SECTORSIZE
));
912 thisEUN
= inftl
->PUtable
[thisEUN
];
916 if (thisEUN
== BLOCK_NIL
) {
917 /* The requested block is not on the media, return all 0x00 */
918 memset(buffer
, 0, SECTORSIZE
);
921 loff_t ptr
= (thisEUN
* inftl
->EraseSize
) + blockofs
;
922 int ret
= mtd
->read(mtd
, ptr
, SECTORSIZE
, &retlen
, buffer
);
924 /* Handle corrected bit flips gracefully */
925 if (ret
< 0 && ret
!= -EUCLEAN
)
931 static int inftl_getgeo(struct mtd_blktrans_dev
*dev
, struct hd_geometry
*geo
)
933 struct INFTLrecord
*inftl
= (void *)dev
;
935 geo
->heads
= inftl
->heads
;
936 geo
->sectors
= inftl
->sectors
;
937 geo
->cylinders
= inftl
->cylinders
;
942 static struct mtd_blktrans_ops inftl_tr
= {
944 .major
= INFTL_MAJOR
,
945 .part_bits
= INFTL_PARTN_BITS
,
947 .getgeo
= inftl_getgeo
,
948 .readsect
= inftl_readblock
,
949 .writesect
= inftl_writeblock
,
950 .add_mtd
= inftl_add_mtd
,
951 .remove_dev
= inftl_remove_dev
,
952 .owner
= THIS_MODULE
,
955 static int __init
init_inftl(void)
957 return register_mtd_blktrans(&inftl_tr
);
960 static void __exit
cleanup_inftl(void)
962 deregister_mtd_blktrans(&inftl_tr
);
965 module_init(init_inftl
);
966 module_exit(cleanup_inftl
);
968 MODULE_LICENSE("GPL");
969 MODULE_AUTHOR("Greg Ungerer <gerg@snapgear.com>, David Woodhouse <dwmw2@infradead.org>, Fabrice Bellard <fabrice.bellard@netgem.com> et al.");
970 MODULE_DESCRIPTION("Support code for Inverse Flash Translation Layer, used on M-Systems DiskOnChip 2000, Millennium and Millennium Plus");