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.18 2004/11/16 18:28:59 dwmw2 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/config.h>
28 #include <linux/kernel.h>
29 #include <linux/module.h>
30 #include <linux/delay.h>
31 #include <linux/slab.h>
32 #include <linux/sched.h>
33 #include <linux/init.h>
34 #include <linux/kmod.h>
35 #include <linux/hdreg.h>
36 #include <linux/mtd/mtd.h>
37 #include <linux/mtd/nftl.h>
38 #include <linux/mtd/inftl.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 extern void INFTL_dumptables(struct INFTLrecord
*inftl
);
51 extern void INFTL_dumpVUchains(struct INFTLrecord
*inftl
);
53 static void inftl_add_mtd(struct mtd_blktrans_ops
*tr
, struct mtd_info
*mtd
)
55 struct INFTLrecord
*inftl
;
58 if (mtd
->type
!= MTD_NANDFLASH
)
60 /* OK, this is moderately ugly. But probably safe. Alternatives? */
61 if (memcmp(mtd
->name
, "DiskOnChip", 10))
64 if (!mtd
->block_isbad
) {
66 "INFTL no longer supports the old DiskOnChip drivers loaded via docprobe.\n"
67 "Please use the new diskonchip driver under the NAND subsystem.\n");
71 DEBUG(MTD_DEBUG_LEVEL3
, "INFTL: add_mtd for %s\n", mtd
->name
);
73 inftl
= kmalloc(sizeof(*inftl
), GFP_KERNEL
);
76 printk(KERN_WARNING
"INFTL: Out of memory for data structures\n");
79 memset(inftl
, 0, sizeof(*inftl
));
82 inftl
->mbd
.devnum
= -1;
83 inftl
->mbd
.blksize
= 512;
85 memcpy(&inftl
->oobinfo
, &mtd
->oobinfo
, sizeof(struct nand_oobinfo
));
86 inftl
->oobinfo
.useecc
= MTD_NANDECC_PLACEONLY
;
88 if (INFTL_mount(inftl
) < 0) {
89 printk(KERN_WARNING
"INFTL: could not mount device\n");
94 /* OK, it's a new one. Set up all the data structures. */
96 /* Calculate geometry */
97 inftl
->cylinders
= 1024;
100 temp
= inftl
->cylinders
* inftl
->heads
;
101 inftl
->sectors
= inftl
->mbd
.size
/ temp
;
102 if (inftl
->mbd
.size
% temp
) {
104 temp
= inftl
->cylinders
* inftl
->sectors
;
105 inftl
->heads
= inftl
->mbd
.size
/ temp
;
107 if (inftl
->mbd
.size
% temp
) {
109 temp
= inftl
->heads
* inftl
->sectors
;
110 inftl
->cylinders
= inftl
->mbd
.size
/ temp
;
114 if (inftl
->mbd
.size
!= inftl
->heads
* inftl
->cylinders
* inftl
->sectors
) {
117 mbd.size == heads * cylinders * sectors
119 printk(KERN_WARNING
"INFTL: cannot calculate a geometry to "
120 "match size of 0x%lx.\n", inftl
->mbd
.size
);
121 printk(KERN_WARNING
"INFTL: using C:%d H:%d S:%d "
122 "(== 0x%lx sects)\n",
123 inftl
->cylinders
, inftl
->heads
, inftl
->sectors
,
124 (long)inftl
->cylinders
* (long)inftl
->heads
*
125 (long)inftl
->sectors
);
128 if (add_mtd_blktrans_dev(&inftl
->mbd
)) {
130 kfree(inftl
->PUtable
);
132 kfree(inftl
->VUtable
);
137 printk(KERN_INFO
"INFTL: Found new nftl%c\n", nftl
->mbd
.devnum
+ 'a');
142 static void inftl_remove_dev(struct mtd_blktrans_dev
*dev
)
144 struct INFTLrecord
*inftl
= (void *)dev
;
146 DEBUG(MTD_DEBUG_LEVEL3
, "INFTL: remove_dev (i=%d)\n", dev
->devnum
);
148 del_mtd_blktrans_dev(dev
);
151 kfree(inftl
->PUtable
);
153 kfree(inftl
->VUtable
);
158 * Actual INFTL access routines.
162 * INFTL_findfreeblock: Find a free Erase Unit on the INFTL partition.
163 * This function is used when the give Virtual Unit Chain.
165 static u16
INFTL_findfreeblock(struct INFTLrecord
*inftl
, int desperate
)
167 u16 pot
= inftl
->LastFreeEUN
;
168 int silly
= inftl
->nb_blocks
;
170 DEBUG(MTD_DEBUG_LEVEL3
, "INFTL: INFTL_findfreeblock(inftl=%p,"
171 "desperate=%d)\n", inftl
, desperate
);
174 * Normally, we force a fold to happen before we run out of free
177 if (!desperate
&& inftl
->numfreeEUNs
< 2) {
178 DEBUG(MTD_DEBUG_LEVEL1
, "INFTL: there are too few free "
179 "EUNs (%d)\n", inftl
->numfreeEUNs
);
183 /* Scan for a free block */
185 if (inftl
->PUtable
[pot
] == BLOCK_FREE
) {
186 inftl
->LastFreeEUN
= pot
;
190 if (++pot
> inftl
->lastEUN
)
194 printk(KERN_WARNING
"INFTL: no free blocks found! "
195 "EUN range = %d - %d\n", 0, inftl
->LastFreeEUN
);
198 } while (pot
!= inftl
->LastFreeEUN
);
203 static u16
INFTL_foldchain(struct INFTLrecord
*inftl
, unsigned thisVUC
, unsigned pendingblock
)
205 u16 BlockMap
[MAX_SECTORS_PER_UNIT
];
206 unsigned char BlockDeleted
[MAX_SECTORS_PER_UNIT
];
207 unsigned int thisEUN
, prevEUN
, status
;
209 unsigned int targetEUN
;
210 struct inftl_oob oob
;
213 DEBUG(MTD_DEBUG_LEVEL3
, "INFTL: INFTL_foldchain(inftl=%p,thisVUC=%d,"
214 "pending=%d)\n", inftl
, thisVUC
, pendingblock
);
216 memset(BlockMap
, 0xff, sizeof(BlockMap
));
217 memset(BlockDeleted
, 0, sizeof(BlockDeleted
));
219 thisEUN
= targetEUN
= inftl
->VUtable
[thisVUC
];
221 if (thisEUN
== BLOCK_NIL
) {
222 printk(KERN_WARNING
"INFTL: trying to fold non-existent "
223 "Virtual Unit Chain %d!\n", thisVUC
);
228 * Scan to find the Erase Unit which holds the actual data for each
229 * 512-byte block within the Chain.
232 while (thisEUN
< inftl
->nb_blocks
) {
233 for (block
= 0; block
< inftl
->EraseSize
/SECTORSIZE
; block
++) {
234 if ((BlockMap
[block
] != 0xffff) || BlockDeleted
[block
])
237 if (MTD_READOOB(inftl
->mbd
.mtd
, (thisEUN
* inftl
->EraseSize
)
238 + (block
* SECTORSIZE
), 16 , &retlen
,
240 status
= SECTOR_IGNORE
;
242 status
= oob
.b
.Status
| oob
.b
.Status1
;
249 BlockMap
[block
] = thisEUN
;
252 BlockDeleted
[block
] = 1;
255 printk(KERN_WARNING
"INFTL: unknown status "
256 "for block %d in EUN %d: %x\n",
257 block
, thisEUN
, status
);
263 printk(KERN_WARNING
"INFTL: infinite loop in Virtual "
264 "Unit Chain 0x%x\n", thisVUC
);
268 thisEUN
= inftl
->PUtable
[thisEUN
];
272 * OK. We now know the location of every block in the Virtual Unit
273 * Chain, and the Erase Unit into which we are supposed to be copying.
276 DEBUG(MTD_DEBUG_LEVEL1
, "INFTL: folding chain %d into unit %d\n",
279 for (block
= 0; block
< inftl
->EraseSize
/SECTORSIZE
; block
++) {
280 unsigned char movebuf
[SECTORSIZE
];
284 * If it's in the target EUN already, or if it's pending write,
287 if (BlockMap
[block
] == targetEUN
|| (pendingblock
==
288 (thisVUC
* (inftl
->EraseSize
/ SECTORSIZE
) + block
))) {
293 * Copy only in non free block (free blocks can only
294 * happen in case of media errors or deleted blocks).
296 if (BlockMap
[block
] == BLOCK_NIL
)
299 ret
= MTD_READ(inftl
->mbd
.mtd
, (inftl
->EraseSize
*
300 BlockMap
[block
]) + (block
* SECTORSIZE
), SECTORSIZE
,
303 ret
= MTD_READ(inftl
->mbd
.mtd
, (inftl
->EraseSize
*
304 BlockMap
[block
]) + (block
* SECTORSIZE
),
305 SECTORSIZE
, &retlen
, movebuf
);
307 DEBUG(MTD_DEBUG_LEVEL1
, "INFTL: error went "
310 memset(&oob
, 0xff, sizeof(struct inftl_oob
));
311 oob
.b
.Status
= oob
.b
.Status1
= SECTOR_USED
;
312 MTD_WRITEECC(inftl
->mbd
.mtd
, (inftl
->EraseSize
* targetEUN
) +
313 (block
* SECTORSIZE
), SECTORSIZE
, &retlen
,
314 movebuf
, (char *)&oob
, &inftl
->oobinfo
);
318 * Newest unit in chain now contains data from _all_ older units.
319 * So go through and erase each unit in chain, oldest first. (This
320 * is important, by doing oldest first if we crash/reboot then it
321 * it is relatively simple to clean up the mess).
323 DEBUG(MTD_DEBUG_LEVEL1
, "INFTL: want to erase virtual chain %d\n",
327 /* Find oldest unit in chain. */
328 thisEUN
= inftl
->VUtable
[thisVUC
];
330 while (inftl
->PUtable
[thisEUN
] != BLOCK_NIL
) {
332 thisEUN
= inftl
->PUtable
[thisEUN
];
335 /* Check if we are all done */
336 if (thisEUN
== targetEUN
)
339 if (INFTL_formatblock(inftl
, thisEUN
) < 0) {
341 * Could not erase : mark block as reserved.
343 inftl
->PUtable
[thisEUN
] = BLOCK_RESERVED
;
345 /* Correctly erased : mark it as free */
346 inftl
->PUtable
[thisEUN
] = BLOCK_FREE
;
347 inftl
->PUtable
[prevEUN
] = BLOCK_NIL
;
348 inftl
->numfreeEUNs
++;
355 static u16
INFTL_makefreeblock(struct INFTLrecord
*inftl
, unsigned pendingblock
)
358 * This is the part that needs some cleverness applied.
359 * For now, I'm doing the minimum applicable to actually
360 * get the thing to work.
361 * Wear-levelling and other clever stuff needs to be implemented
362 * and we also need to do some assessment of the results when
363 * the system loses power half-way through the routine.
365 u16 LongestChain
= 0;
366 u16 ChainLength
= 0, thislen
;
369 DEBUG(MTD_DEBUG_LEVEL3
, "INFTL: INFTL_makefreeblock(inftl=%p,"
370 "pending=%d)\n", inftl
, pendingblock
);
372 for (chain
= 0; chain
< inftl
->nb_blocks
; chain
++) {
373 EUN
= inftl
->VUtable
[chain
];
376 while (EUN
<= inftl
->lastEUN
) {
378 EUN
= inftl
->PUtable
[EUN
];
379 if (thislen
> 0xff00) {
380 printk(KERN_WARNING
"INFTL: endless loop in "
381 "Virtual Chain %d: Unit %x\n",
384 * Actually, don't return failure.
385 * Just ignore this chain and get on with it.
392 if (thislen
> ChainLength
) {
393 ChainLength
= thislen
;
394 LongestChain
= chain
;
398 if (ChainLength
< 2) {
399 printk(KERN_WARNING
"INFTL: no Virtual Unit Chains available "
400 "for folding. Failing request\n");
404 return INFTL_foldchain(inftl
, LongestChain
, pendingblock
);
407 static int nrbits(unsigned int val
, int bitcount
)
411 for (i
= 0; (i
< bitcount
); i
++)
412 total
+= (((0x1 << i
) & val
) ? 1 : 0);
417 * INFTL_findwriteunit: Return the unit number into which we can write
418 * for this block. Make it available if it isn't already.
420 static inline u16
INFTL_findwriteunit(struct INFTLrecord
*inftl
, unsigned block
)
422 unsigned int thisVUC
= block
/ (inftl
->EraseSize
/ SECTORSIZE
);
423 unsigned int thisEUN
, writeEUN
, prev_block
, status
;
424 unsigned long blockofs
= (block
* SECTORSIZE
) & (inftl
->EraseSize
-1);
425 struct inftl_oob oob
;
426 struct inftl_bci bci
;
427 unsigned char anac
, nacs
, parity
;
429 int silly
, silly2
= 3;
431 DEBUG(MTD_DEBUG_LEVEL3
, "INFTL: INFTL_findwriteunit(inftl=%p,"
432 "block=%d)\n", inftl
, block
);
436 * Scan the media to find a unit in the VUC which has
437 * a free space for the block in question.
439 writeEUN
= BLOCK_NIL
;
440 thisEUN
= inftl
->VUtable
[thisVUC
];
443 while (thisEUN
<= inftl
->lastEUN
) {
444 MTD_READOOB(inftl
->mbd
.mtd
, (thisEUN
* inftl
->EraseSize
) +
445 blockofs
, 8, &retlen
, (char *)&bci
);
447 status
= bci
.Status
| bci
.Status1
;
448 DEBUG(MTD_DEBUG_LEVEL3
, "INFTL: status of block %d in "
449 "EUN %d is %x\n", block
, writeEUN
, status
);
457 /* Can't go any further */
463 * Invalid block. Don't use it any more.
470 printk(KERN_WARNING
"INFTL: infinite loop in "
471 "Virtual Unit Chain 0x%x\n", thisVUC
);
475 /* Skip to next block in chain */
476 thisEUN
= inftl
->PUtable
[thisEUN
];
480 if (writeEUN
!= BLOCK_NIL
)
485 * OK. We didn't find one in the existing chain, or there
486 * is no existing chain. Allocate a new one.
488 writeEUN
= INFTL_findfreeblock(inftl
, 0);
490 if (writeEUN
== BLOCK_NIL
) {
492 * That didn't work - there were no free blocks just
493 * waiting to be picked up. We're going to have to fold
494 * a chain to make room.
496 thisEUN
= INFTL_makefreeblock(inftl
, 0xffff);
499 * Hopefully we free something, lets try again.
500 * This time we are desperate...
502 DEBUG(MTD_DEBUG_LEVEL1
, "INFTL: using desperate==1 "
503 "to find free EUN to accommodate write to "
504 "VUC %d\n", thisVUC
);
505 writeEUN
= INFTL_findfreeblock(inftl
, 1);
506 if (writeEUN
== BLOCK_NIL
) {
508 * Ouch. This should never happen - we should
509 * always be able to make some room somehow.
510 * If we get here, we've allocated more storage
511 * space than actual media, or our makefreeblock
512 * routine is missing something.
514 printk(KERN_WARNING
"INFTL: cannot make free "
517 INFTL_dumptables(inftl
);
518 INFTL_dumpVUchains(inftl
);
525 * Insert new block into virtual chain. Firstly update the
526 * block headers in flash...
530 thisEUN
= inftl
->VUtable
[thisVUC
];
531 if (thisEUN
!= BLOCK_NIL
) {
532 MTD_READOOB(inftl
->mbd
.mtd
, thisEUN
* inftl
->EraseSize
533 + 8, 8, &retlen
, (char *)&oob
.u
);
534 anac
= oob
.u
.a
.ANAC
+ 1;
535 nacs
= oob
.u
.a
.NACs
+ 1;
538 prev_block
= inftl
->VUtable
[thisVUC
];
539 if (prev_block
< inftl
->nb_blocks
)
540 prev_block
-= inftl
->firstEUN
;
542 parity
= (nrbits(thisVUC
, 16) & 0x1) ? 0x1 : 0;
543 parity
|= (nrbits(prev_block
, 16) & 0x1) ? 0x2 : 0;
544 parity
|= (nrbits(anac
, 8) & 0x1) ? 0x4 : 0;
545 parity
|= (nrbits(nacs
, 8) & 0x1) ? 0x8 : 0;
547 oob
.u
.a
.virtualUnitNo
= cpu_to_le16(thisVUC
);
548 oob
.u
.a
.prevUnitNo
= cpu_to_le16(prev_block
);
551 oob
.u
.a
.parityPerField
= parity
;
552 oob
.u
.a
.discarded
= 0xaa;
554 MTD_WRITEOOB(inftl
->mbd
.mtd
, writeEUN
* inftl
->EraseSize
+ 8, 8,
555 &retlen
, (char *)&oob
.u
);
557 /* Also back up header... */
558 oob
.u
.b
.virtualUnitNo
= cpu_to_le16(thisVUC
);
559 oob
.u
.b
.prevUnitNo
= cpu_to_le16(prev_block
);
562 oob
.u
.b
.parityPerField
= parity
;
563 oob
.u
.b
.discarded
= 0xaa;
565 MTD_WRITEOOB(inftl
->mbd
.mtd
, writeEUN
* inftl
->EraseSize
+
566 SECTORSIZE
* 4 + 8, 8, &retlen
, (char *)&oob
.u
);
568 inftl
->PUtable
[writeEUN
] = inftl
->VUtable
[thisVUC
];
569 inftl
->VUtable
[thisVUC
] = writeEUN
;
571 inftl
->numfreeEUNs
--;
576 printk(KERN_WARNING
"INFTL: error folding to make room for Virtual "
577 "Unit Chain 0x%x\n", thisVUC
);
582 * Given a Virtual Unit Chain, see if it can be deleted, and if so do it.
584 static void INFTL_trydeletechain(struct INFTLrecord
*inftl
, unsigned thisVUC
)
586 unsigned char BlockUsed
[MAX_SECTORS_PER_UNIT
];
587 unsigned char BlockDeleted
[MAX_SECTORS_PER_UNIT
];
588 unsigned int thisEUN
, status
;
590 struct inftl_bci bci
;
593 DEBUG(MTD_DEBUG_LEVEL3
, "INFTL: INFTL_trydeletechain(inftl=%p,"
594 "thisVUC=%d)\n", inftl
, thisVUC
);
596 memset(BlockUsed
, 0, sizeof(BlockUsed
));
597 memset(BlockDeleted
, 0, sizeof(BlockDeleted
));
599 thisEUN
= inftl
->VUtable
[thisVUC
];
600 if (thisEUN
== BLOCK_NIL
) {
601 printk(KERN_WARNING
"INFTL: trying to delete non-existent "
602 "Virtual Unit Chain %d!\n", thisVUC
);
607 * Scan through the Erase Units to determine whether any data is in
608 * each of the 512-byte blocks within the Chain.
611 while (thisEUN
< inftl
->nb_blocks
) {
612 for (block
= 0; block
< inftl
->EraseSize
/SECTORSIZE
; block
++) {
613 if (BlockUsed
[block
] || BlockDeleted
[block
])
616 if (MTD_READOOB(inftl
->mbd
.mtd
, (thisEUN
* inftl
->EraseSize
)
617 + (block
* SECTORSIZE
), 8 , &retlen
,
619 status
= SECTOR_IGNORE
;
621 status
= bci
.Status
| bci
.Status1
;
628 BlockUsed
[block
] = 1;
631 BlockDeleted
[block
] = 1;
634 printk(KERN_WARNING
"INFTL: unknown status "
635 "for block %d in EUN %d: 0x%x\n",
636 block
, thisEUN
, status
);
641 printk(KERN_WARNING
"INFTL: infinite loop in Virtual "
642 "Unit Chain 0x%x\n", thisVUC
);
646 thisEUN
= inftl
->PUtable
[thisEUN
];
649 for (block
= 0; block
< inftl
->EraseSize
/SECTORSIZE
; block
++)
650 if (BlockUsed
[block
])
654 * For each block in the chain free it and make it available
655 * for future use. Erase from the oldest unit first.
657 DEBUG(MTD_DEBUG_LEVEL1
, "INFTL: deleting empty VUC %d\n", thisVUC
);
660 u16
*prevEUN
= &inftl
->VUtable
[thisVUC
];
663 /* If the chain is all gone already, we're done */
664 if (thisEUN
== BLOCK_NIL
) {
665 DEBUG(MTD_DEBUG_LEVEL2
, "INFTL: Empty VUC %d for deletion was already absent\n", thisEUN
);
669 /* Find oldest unit in chain. */
670 while (inftl
->PUtable
[thisEUN
] != BLOCK_NIL
) {
671 BUG_ON(thisEUN
>= inftl
->nb_blocks
);
673 prevEUN
= &inftl
->PUtable
[thisEUN
];
677 DEBUG(MTD_DEBUG_LEVEL3
, "Deleting EUN %d from VUC %d\n",
680 if (INFTL_formatblock(inftl
, thisEUN
) < 0) {
682 * Could not erase : mark block as reserved.
684 inftl
->PUtable
[thisEUN
] = BLOCK_RESERVED
;
686 /* Correctly erased : mark it as free */
687 inftl
->PUtable
[thisEUN
] = BLOCK_FREE
;
688 inftl
->numfreeEUNs
++;
691 /* Now sort out whatever was pointing to it... */
692 *prevEUN
= BLOCK_NIL
;
694 /* Ideally we'd actually be responsive to new
695 requests while we're doing this -- if there's
696 free space why should others be made to wait? */
700 inftl
->VUtable
[thisVUC
] = BLOCK_NIL
;
703 static int INFTL_deleteblock(struct INFTLrecord
*inftl
, unsigned block
)
705 unsigned int thisEUN
= inftl
->VUtable
[block
/ (inftl
->EraseSize
/ SECTORSIZE
)];
706 unsigned long blockofs
= (block
* SECTORSIZE
) & (inftl
->EraseSize
- 1);
708 int silly
= MAX_LOOPS
;
710 struct inftl_bci bci
;
712 DEBUG(MTD_DEBUG_LEVEL3
, "INFTL: INFTL_deleteblock(inftl=%p,"
713 "block=%d)\n", inftl
, block
);
715 while (thisEUN
< inftl
->nb_blocks
) {
716 if (MTD_READOOB(inftl
->mbd
.mtd
, (thisEUN
* inftl
->EraseSize
) +
717 blockofs
, 8, &retlen
, (char *)&bci
) < 0)
718 status
= SECTOR_IGNORE
;
720 status
= bci
.Status
| bci
.Status1
;
732 printk(KERN_WARNING
"INFTL: unknown status for "
733 "block %d in EUN %d: 0x%x\n",
734 block
, thisEUN
, status
);
739 printk(KERN_WARNING
"INFTL: infinite loop in Virtual "
741 block
/ (inftl
->EraseSize
/ SECTORSIZE
));
744 thisEUN
= inftl
->PUtable
[thisEUN
];
748 if (thisEUN
!= BLOCK_NIL
) {
749 loff_t ptr
= (thisEUN
* inftl
->EraseSize
) + blockofs
;
751 if (MTD_READOOB(inftl
->mbd
.mtd
, ptr
, 8, &retlen
, (char *)&bci
) < 0)
753 bci
.Status
= bci
.Status1
= SECTOR_DELETED
;
754 if (MTD_WRITEOOB(inftl
->mbd
.mtd
, ptr
, 8, &retlen
, (char *)&bci
) < 0)
756 INFTL_trydeletechain(inftl
, block
/ (inftl
->EraseSize
/ SECTORSIZE
));
761 static int inftl_writeblock(struct mtd_blktrans_dev
*mbd
, unsigned long block
,
764 struct INFTLrecord
*inftl
= (void *)mbd
;
765 unsigned int writeEUN
;
766 unsigned long blockofs
= (block
* SECTORSIZE
) & (inftl
->EraseSize
- 1);
768 struct inftl_oob oob
;
771 DEBUG(MTD_DEBUG_LEVEL3
, "INFTL: inftl_writeblock(inftl=%p,block=%ld,"
772 "buffer=%p)\n", inftl
, block
, buffer
);
774 /* Is block all zero? */
775 pend
= buffer
+ SECTORSIZE
;
776 for (p
= buffer
; p
< pend
&& !*p
; p
++)
780 writeEUN
= INFTL_findwriteunit(inftl
, block
);
782 if (writeEUN
== BLOCK_NIL
) {
783 printk(KERN_WARNING
"inftl_writeblock(): cannot find "
784 "block to write to\n");
786 * If we _still_ haven't got a block to use,
792 memset(&oob
, 0xff, sizeof(struct inftl_oob
));
793 oob
.b
.Status
= oob
.b
.Status1
= SECTOR_USED
;
794 MTD_WRITEECC(inftl
->mbd
.mtd
, (writeEUN
* inftl
->EraseSize
) +
795 blockofs
, SECTORSIZE
, &retlen
, (char *)buffer
,
796 (char *)&oob
, &inftl
->oobinfo
);
798 * need to write SECTOR_USED flags since they are not written
802 INFTL_deleteblock(inftl
, block
);
808 static int inftl_readblock(struct mtd_blktrans_dev
*mbd
, unsigned long block
,
811 struct INFTLrecord
*inftl
= (void *)mbd
;
812 unsigned int thisEUN
= inftl
->VUtable
[block
/ (inftl
->EraseSize
/ SECTORSIZE
)];
813 unsigned long blockofs
= (block
* SECTORSIZE
) & (inftl
->EraseSize
- 1);
815 int silly
= MAX_LOOPS
;
816 struct inftl_bci bci
;
819 DEBUG(MTD_DEBUG_LEVEL3
, "INFTL: inftl_readblock(inftl=%p,block=%ld,"
820 "buffer=%p)\n", inftl
, block
, buffer
);
822 while (thisEUN
< inftl
->nb_blocks
) {
823 if (MTD_READOOB(inftl
->mbd
.mtd
, (thisEUN
* inftl
->EraseSize
) +
824 blockofs
, 8, &retlen
, (char *)&bci
) < 0)
825 status
= SECTOR_IGNORE
;
827 status
= bci
.Status
| bci
.Status1
;
839 printk(KERN_WARNING
"INFTL: unknown status for "
840 "block %ld in EUN %d: 0x%04x\n",
841 block
, thisEUN
, status
);
846 printk(KERN_WARNING
"INFTL: infinite loop in "
847 "Virtual Unit Chain 0x%lx\n",
848 block
/ (inftl
->EraseSize
/ SECTORSIZE
));
852 thisEUN
= inftl
->PUtable
[thisEUN
];
856 if (thisEUN
== BLOCK_NIL
) {
857 /* The requested block is not on the media, return all 0x00 */
858 memset(buffer
, 0, SECTORSIZE
);
861 loff_t ptr
= (thisEUN
* inftl
->EraseSize
) + blockofs
;
862 if (MTD_READ(inftl
->mbd
.mtd
, ptr
, SECTORSIZE
, &retlen
,
869 static int inftl_getgeo(struct mtd_blktrans_dev
*dev
, struct hd_geometry
*geo
)
871 struct INFTLrecord
*inftl
= (void *)dev
;
873 geo
->heads
= inftl
->heads
;
874 geo
->sectors
= inftl
->sectors
;
875 geo
->cylinders
= inftl
->cylinders
;
880 static struct mtd_blktrans_ops inftl_tr
= {
882 .major
= INFTL_MAJOR
,
883 .part_bits
= INFTL_PARTN_BITS
,
884 .getgeo
= inftl_getgeo
,
885 .readsect
= inftl_readblock
,
886 .writesect
= inftl_writeblock
,
887 .add_mtd
= inftl_add_mtd
,
888 .remove_dev
= inftl_remove_dev
,
889 .owner
= THIS_MODULE
,
892 extern char inftlmountrev
[];
894 static int __init
init_inftl(void)
896 printk(KERN_INFO
"INFTL: inftlcore.c $Revision: 1.18 $, "
897 "inftlmount.c %s\n", inftlmountrev
);
899 return register_mtd_blktrans(&inftl_tr
);
902 static void __exit
cleanup_inftl(void)
904 deregister_mtd_blktrans(&inftl_tr
);
907 module_init(init_inftl
);
908 module_exit(cleanup_inftl
);
910 MODULE_LICENSE("GPL");
911 MODULE_AUTHOR("Greg Ungerer <gerg@snapgear.com>, David Woodhouse <dwmw2@infradead.org>, Fabrice Bellard <fabrice.bellard@netgem.com> et al.");
912 MODULE_DESCRIPTION("Support code for Inverse Flash Translation Layer, used on M-Systems DiskOnChip 2000, Millennium and Millennium Plus");