[PATCH] pcmcia: make pcmcia_release_{io,irq} static
[linux-2.6/mini2440.git] / drivers / mtd / nftlcore.c
blobd7cd5fa16ba445e8d854760de615e7ec15821949
1 /* Linux driver for NAND Flash Translation Layer */
2 /* (c) 1999 Machine Vision Holdings, Inc. */
3 /* Author: David Woodhouse <dwmw2@infradead.org> */
4 /* $Id: nftlcore.c,v 1.98 2005/11/07 11:14:21 gleixner Exp $ */
6 /*
7 The contents of this file are distributed under the GNU General
8 Public License version 2. The author places no additional
9 restrictions of any kind on it.
12 #define PRERELEASE
14 #include <linux/config.h>
15 #include <linux/kernel.h>
16 #include <linux/module.h>
17 #include <asm/errno.h>
18 #include <asm/io.h>
19 #include <asm/uaccess.h>
20 #include <linux/miscdevice.h>
21 #include <linux/pci.h>
22 #include <linux/delay.h>
23 #include <linux/slab.h>
24 #include <linux/sched.h>
25 #include <linux/init.h>
26 #include <linux/hdreg.h>
28 #include <linux/kmod.h>
29 #include <linux/mtd/mtd.h>
30 #include <linux/mtd/nand.h>
31 #include <linux/mtd/nftl.h>
32 #include <linux/mtd/blktrans.h>
34 /* maximum number of loops while examining next block, to have a
35 chance to detect consistency problems (they should never happen
36 because of the checks done in the mounting */
38 #define MAX_LOOPS 10000
41 static void nftl_add_mtd(struct mtd_blktrans_ops *tr, struct mtd_info *mtd)
43 struct NFTLrecord *nftl;
44 unsigned long temp;
46 if (mtd->type != MTD_NANDFLASH)
47 return;
48 /* OK, this is moderately ugly. But probably safe. Alternatives? */
49 if (memcmp(mtd->name, "DiskOnChip", 10))
50 return;
52 if (!mtd->block_isbad) {
53 printk(KERN_ERR
54 "NFTL no longer supports the old DiskOnChip drivers loaded via docprobe.\n"
55 "Please use the new diskonchip driver under the NAND subsystem.\n");
56 return;
59 DEBUG(MTD_DEBUG_LEVEL1, "NFTL: add_mtd for %s\n", mtd->name);
61 nftl = kmalloc(sizeof(struct NFTLrecord), GFP_KERNEL);
63 if (!nftl) {
64 printk(KERN_WARNING "NFTL: out of memory for data structures\n");
65 return;
67 memset(nftl, 0, sizeof(*nftl));
69 nftl->mbd.mtd = mtd;
70 nftl->mbd.devnum = -1;
71 nftl->mbd.blksize = 512;
72 nftl->mbd.tr = tr;
73 memcpy(&nftl->oobinfo, &mtd->oobinfo, sizeof(struct nand_oobinfo));
74 nftl->oobinfo.useecc = MTD_NANDECC_PLACEONLY;
76 if (NFTL_mount(nftl) < 0) {
77 printk(KERN_WARNING "NFTL: could not mount device\n");
78 kfree(nftl);
79 return;
82 /* OK, it's a new one. Set up all the data structures. */
84 /* Calculate geometry */
85 nftl->cylinders = 1024;
86 nftl->heads = 16;
88 temp = nftl->cylinders * nftl->heads;
89 nftl->sectors = nftl->mbd.size / temp;
90 if (nftl->mbd.size % temp) {
91 nftl->sectors++;
92 temp = nftl->cylinders * nftl->sectors;
93 nftl->heads = nftl->mbd.size / temp;
95 if (nftl->mbd.size % temp) {
96 nftl->heads++;
97 temp = nftl->heads * nftl->sectors;
98 nftl->cylinders = nftl->mbd.size / temp;
102 if (nftl->mbd.size != nftl->heads * nftl->cylinders * nftl->sectors) {
104 Oh no we don't have
105 mbd.size == heads * cylinders * sectors
107 printk(KERN_WARNING "NFTL: cannot calculate a geometry to "
108 "match size of 0x%lx.\n", nftl->mbd.size);
109 printk(KERN_WARNING "NFTL: using C:%d H:%d S:%d "
110 "(== 0x%lx sects)\n",
111 nftl->cylinders, nftl->heads , nftl->sectors,
112 (long)nftl->cylinders * (long)nftl->heads *
113 (long)nftl->sectors );
116 if (add_mtd_blktrans_dev(&nftl->mbd)) {
117 kfree(nftl->ReplUnitTable);
118 kfree(nftl->EUNtable);
119 kfree(nftl);
120 return;
122 #ifdef PSYCHO_DEBUG
123 printk(KERN_INFO "NFTL: Found new nftl%c\n", nftl->mbd.devnum + 'a');
124 #endif
127 static void nftl_remove_dev(struct mtd_blktrans_dev *dev)
129 struct NFTLrecord *nftl = (void *)dev;
131 DEBUG(MTD_DEBUG_LEVEL1, "NFTL: remove_dev (i=%d)\n", dev->devnum);
133 del_mtd_blktrans_dev(dev);
134 kfree(nftl->ReplUnitTable);
135 kfree(nftl->EUNtable);
136 kfree(nftl);
139 #ifdef CONFIG_NFTL_RW
141 /* Actual NFTL access routines */
142 /* NFTL_findfreeblock: Find a free Erase Unit on the NFTL partition. This function is used
143 * when the give Virtual Unit Chain
145 static u16 NFTL_findfreeblock(struct NFTLrecord *nftl, int desperate )
147 /* For a given Virtual Unit Chain: find or create a free block and
148 add it to the chain */
149 /* We're passed the number of the last EUN in the chain, to save us from
150 having to look it up again */
151 u16 pot = nftl->LastFreeEUN;
152 int silly = nftl->nb_blocks;
154 /* Normally, we force a fold to happen before we run out of free blocks completely */
155 if (!desperate && nftl->numfreeEUNs < 2) {
156 DEBUG(MTD_DEBUG_LEVEL1, "NFTL_findfreeblock: there are too few free EUNs\n");
157 return 0xffff;
160 /* Scan for a free block */
161 do {
162 if (nftl->ReplUnitTable[pot] == BLOCK_FREE) {
163 nftl->LastFreeEUN = pot;
164 nftl->numfreeEUNs--;
165 return pot;
168 /* This will probably point to the MediaHdr unit itself,
169 right at the beginning of the partition. But that unit
170 (and the backup unit too) should have the UCI set
171 up so that it's not selected for overwriting */
172 if (++pot > nftl->lastEUN)
173 pot = le16_to_cpu(nftl->MediaHdr.FirstPhysicalEUN);
175 if (!silly--) {
176 printk("Argh! No free blocks found! LastFreeEUN = %d, "
177 "FirstEUN = %d\n", nftl->LastFreeEUN,
178 le16_to_cpu(nftl->MediaHdr.FirstPhysicalEUN));
179 return 0xffff;
181 } while (pot != nftl->LastFreeEUN);
183 return 0xffff;
186 static u16 NFTL_foldchain (struct NFTLrecord *nftl, unsigned thisVUC, unsigned pendingblock )
188 u16 BlockMap[MAX_SECTORS_PER_UNIT];
189 unsigned char BlockLastState[MAX_SECTORS_PER_UNIT];
190 unsigned char BlockFreeFound[MAX_SECTORS_PER_UNIT];
191 unsigned int thisEUN;
192 int block;
193 int silly;
194 unsigned int targetEUN;
195 struct nftl_oob oob;
196 int inplace = 1;
197 size_t retlen;
199 memset(BlockMap, 0xff, sizeof(BlockMap));
200 memset(BlockFreeFound, 0, sizeof(BlockFreeFound));
202 thisEUN = nftl->EUNtable[thisVUC];
204 if (thisEUN == BLOCK_NIL) {
205 printk(KERN_WARNING "Trying to fold non-existent "
206 "Virtual Unit Chain %d!\n", thisVUC);
207 return BLOCK_NIL;
210 /* Scan to find the Erase Unit which holds the actual data for each
211 512-byte block within the Chain.
213 silly = MAX_LOOPS;
214 targetEUN = BLOCK_NIL;
215 while (thisEUN <= nftl->lastEUN ) {
216 unsigned int status, foldmark;
218 targetEUN = thisEUN;
219 for (block = 0; block < nftl->EraseSize / 512; block ++) {
220 MTD_READOOB(nftl->mbd.mtd,
221 (thisEUN * nftl->EraseSize) + (block * 512),
222 16 , &retlen, (char *)&oob);
223 if (block == 2) {
224 foldmark = oob.u.c.FoldMark | oob.u.c.FoldMark1;
225 if (foldmark == FOLD_MARK_IN_PROGRESS) {
226 DEBUG(MTD_DEBUG_LEVEL1,
227 "Write Inhibited on EUN %d\n", thisEUN);
228 inplace = 0;
229 } else {
230 /* There's no other reason not to do inplace,
231 except ones that come later. So we don't need
232 to preserve inplace */
233 inplace = 1;
236 status = oob.b.Status | oob.b.Status1;
237 BlockLastState[block] = status;
239 switch(status) {
240 case SECTOR_FREE:
241 BlockFreeFound[block] = 1;
242 break;
244 case SECTOR_USED:
245 if (!BlockFreeFound[block])
246 BlockMap[block] = thisEUN;
247 else
248 printk(KERN_WARNING
249 "SECTOR_USED found after SECTOR_FREE "
250 "in Virtual Unit Chain %d for block %d\n",
251 thisVUC, block);
252 break;
253 case SECTOR_DELETED:
254 if (!BlockFreeFound[block])
255 BlockMap[block] = BLOCK_NIL;
256 else
257 printk(KERN_WARNING
258 "SECTOR_DELETED found after SECTOR_FREE "
259 "in Virtual Unit Chain %d for block %d\n",
260 thisVUC, block);
261 break;
263 case SECTOR_IGNORE:
264 break;
265 default:
266 printk("Unknown status for block %d in EUN %d: %x\n",
267 block, thisEUN, status);
271 if (!silly--) {
272 printk(KERN_WARNING "Infinite loop in Virtual Unit Chain 0x%x\n",
273 thisVUC);
274 return BLOCK_NIL;
277 thisEUN = nftl->ReplUnitTable[thisEUN];
280 if (inplace) {
281 /* We're being asked to be a fold-in-place. Check
282 that all blocks which actually have data associated
283 with them (i.e. BlockMap[block] != BLOCK_NIL) are
284 either already present or SECTOR_FREE in the target
285 block. If not, we're going to have to fold out-of-place
286 anyway.
288 for (block = 0; block < nftl->EraseSize / 512 ; block++) {
289 if (BlockLastState[block] != SECTOR_FREE &&
290 BlockMap[block] != BLOCK_NIL &&
291 BlockMap[block] != targetEUN) {
292 DEBUG(MTD_DEBUG_LEVEL1, "Setting inplace to 0. VUC %d, "
293 "block %d was %x lastEUN, "
294 "and is in EUN %d (%s) %d\n",
295 thisVUC, block, BlockLastState[block],
296 BlockMap[block],
297 BlockMap[block]== targetEUN ? "==" : "!=",
298 targetEUN);
299 inplace = 0;
300 break;
304 if (pendingblock >= (thisVUC * (nftl->EraseSize / 512)) &&
305 pendingblock < ((thisVUC + 1)* (nftl->EraseSize / 512)) &&
306 BlockLastState[pendingblock - (thisVUC * (nftl->EraseSize / 512))] !=
307 SECTOR_FREE) {
308 DEBUG(MTD_DEBUG_LEVEL1, "Pending write not free in EUN %d. "
309 "Folding out of place.\n", targetEUN);
310 inplace = 0;
314 if (!inplace) {
315 DEBUG(MTD_DEBUG_LEVEL1, "Cannot fold Virtual Unit Chain %d in place. "
316 "Trying out-of-place\n", thisVUC);
317 /* We need to find a targetEUN to fold into. */
318 targetEUN = NFTL_findfreeblock(nftl, 1);
319 if (targetEUN == BLOCK_NIL) {
320 /* Ouch. Now we're screwed. We need to do a
321 fold-in-place of another chain to make room
322 for this one. We need a better way of selecting
323 which chain to fold, because makefreeblock will
324 only ask us to fold the same one again.
326 printk(KERN_WARNING
327 "NFTL_findfreeblock(desperate) returns 0xffff.\n");
328 return BLOCK_NIL;
330 } else {
331 /* We put a fold mark in the chain we are folding only if
332 we fold in place to help the mount check code. If we do
333 not fold in place, it is possible to find the valid
334 chain by selecting the longer one */
335 oob.u.c.FoldMark = oob.u.c.FoldMark1 = cpu_to_le16(FOLD_MARK_IN_PROGRESS);
336 oob.u.c.unused = 0xffffffff;
337 MTD_WRITEOOB(nftl->mbd.mtd, (nftl->EraseSize * targetEUN) + 2 * 512 + 8,
338 8, &retlen, (char *)&oob.u);
341 /* OK. We now know the location of every block in the Virtual Unit Chain,
342 and the Erase Unit into which we are supposed to be copying.
343 Go for it.
345 DEBUG(MTD_DEBUG_LEVEL1,"Folding chain %d into unit %d\n", thisVUC, targetEUN);
346 for (block = 0; block < nftl->EraseSize / 512 ; block++) {
347 unsigned char movebuf[512];
348 int ret;
350 /* If it's in the target EUN already, or if it's pending write, do nothing */
351 if (BlockMap[block] == targetEUN ||
352 (pendingblock == (thisVUC * (nftl->EraseSize / 512) + block))) {
353 continue;
356 /* copy only in non free block (free blocks can only
357 happen in case of media errors or deleted blocks) */
358 if (BlockMap[block] == BLOCK_NIL)
359 continue;
361 ret = MTD_READ(nftl->mbd.mtd, (nftl->EraseSize * BlockMap[block]) + (block * 512),
362 512, &retlen, movebuf);
363 if (ret < 0) {
364 ret = MTD_READ(nftl->mbd.mtd, (nftl->EraseSize * BlockMap[block])
365 + (block * 512), 512, &retlen,
366 movebuf);
367 if (ret != -EIO)
368 printk("Error went away on retry.\n");
370 memset(&oob, 0xff, sizeof(struct nftl_oob));
371 oob.b.Status = oob.b.Status1 = SECTOR_USED;
372 MTD_WRITEECC(nftl->mbd.mtd, (nftl->EraseSize * targetEUN) + (block * 512),
373 512, &retlen, movebuf, (char *)&oob, &nftl->oobinfo);
376 /* add the header so that it is now a valid chain */
377 oob.u.a.VirtUnitNum = oob.u.a.SpareVirtUnitNum
378 = cpu_to_le16(thisVUC);
379 oob.u.a.ReplUnitNum = oob.u.a.SpareReplUnitNum = 0xffff;
381 MTD_WRITEOOB(nftl->mbd.mtd, (nftl->EraseSize * targetEUN) + 8,
382 8, &retlen, (char *)&oob.u);
384 /* OK. We've moved the whole lot into the new block. Now we have to free the original blocks. */
386 /* At this point, we have two different chains for this Virtual Unit, and no way to tell
387 them apart. If we crash now, we get confused. However, both contain the same data, so we
388 shouldn't actually lose data in this case. It's just that when we load up on a medium which
389 has duplicate chains, we need to free one of the chains because it's not necessary any more.
391 thisEUN = nftl->EUNtable[thisVUC];
392 DEBUG(MTD_DEBUG_LEVEL1,"Want to erase\n");
394 /* For each block in the old chain (except the targetEUN of course),
395 free it and make it available for future use */
396 while (thisEUN <= nftl->lastEUN && thisEUN != targetEUN) {
397 unsigned int EUNtmp;
399 EUNtmp = nftl->ReplUnitTable[thisEUN];
401 if (NFTL_formatblock(nftl, thisEUN) < 0) {
402 /* could not erase : mark block as reserved
404 nftl->ReplUnitTable[thisEUN] = BLOCK_RESERVED;
405 } else {
406 /* correctly erased : mark it as free */
407 nftl->ReplUnitTable[thisEUN] = BLOCK_FREE;
408 nftl->numfreeEUNs++;
410 thisEUN = EUNtmp;
413 /* Make this the new start of chain for thisVUC */
414 nftl->ReplUnitTable[targetEUN] = BLOCK_NIL;
415 nftl->EUNtable[thisVUC] = targetEUN;
417 return targetEUN;
420 static u16 NFTL_makefreeblock( struct NFTLrecord *nftl , unsigned pendingblock)
422 /* This is the part that needs some cleverness applied.
423 For now, I'm doing the minimum applicable to actually
424 get the thing to work.
425 Wear-levelling and other clever stuff needs to be implemented
426 and we also need to do some assessment of the results when
427 the system loses power half-way through the routine.
429 u16 LongestChain = 0;
430 u16 ChainLength = 0, thislen;
431 u16 chain, EUN;
433 for (chain = 0; chain < le32_to_cpu(nftl->MediaHdr.FormattedSize) / nftl->EraseSize; chain++) {
434 EUN = nftl->EUNtable[chain];
435 thislen = 0;
437 while (EUN <= nftl->lastEUN) {
438 thislen++;
439 //printk("VUC %d reaches len %d with EUN %d\n", chain, thislen, EUN);
440 EUN = nftl->ReplUnitTable[EUN] & 0x7fff;
441 if (thislen > 0xff00) {
442 printk("Endless loop in Virtual Chain %d: Unit %x\n",
443 chain, EUN);
445 if (thislen > 0xff10) {
446 /* Actually, don't return failure. Just ignore this chain and
447 get on with it. */
448 thislen = 0;
449 break;
453 if (thislen > ChainLength) {
454 //printk("New longest chain is %d with length %d\n", chain, thislen);
455 ChainLength = thislen;
456 LongestChain = chain;
460 if (ChainLength < 2) {
461 printk(KERN_WARNING "No Virtual Unit Chains available for folding. "
462 "Failing request\n");
463 return 0xffff;
466 return NFTL_foldchain (nftl, LongestChain, pendingblock);
469 /* NFTL_findwriteunit: Return the unit number into which we can write
470 for this block. Make it available if it isn't already
472 static inline u16 NFTL_findwriteunit(struct NFTLrecord *nftl, unsigned block)
474 u16 lastEUN;
475 u16 thisVUC = block / (nftl->EraseSize / 512);
476 unsigned int writeEUN;
477 unsigned long blockofs = (block * 512) & (nftl->EraseSize -1);
478 size_t retlen;
479 int silly, silly2 = 3;
480 struct nftl_oob oob;
482 do {
483 /* Scan the media to find a unit in the VUC which has
484 a free space for the block in question.
487 /* This condition catches the 0x[7f]fff cases, as well as
488 being a sanity check for past-end-of-media access
490 lastEUN = BLOCK_NIL;
491 writeEUN = nftl->EUNtable[thisVUC];
492 silly = MAX_LOOPS;
493 while (writeEUN <= nftl->lastEUN) {
494 struct nftl_bci bci;
495 size_t retlen;
496 unsigned int status;
498 lastEUN = writeEUN;
500 MTD_READOOB(nftl->mbd.mtd, (writeEUN * nftl->EraseSize) + blockofs,
501 8, &retlen, (char *)&bci);
503 DEBUG(MTD_DEBUG_LEVEL2, "Status of block %d in EUN %d is %x\n",
504 block , writeEUN, le16_to_cpu(bci.Status));
506 status = bci.Status | bci.Status1;
507 switch(status) {
508 case SECTOR_FREE:
509 return writeEUN;
511 case SECTOR_DELETED:
512 case SECTOR_USED:
513 case SECTOR_IGNORE:
514 break;
515 default:
516 // Invalid block. Don't use it any more. Must implement.
517 break;
520 if (!silly--) {
521 printk(KERN_WARNING
522 "Infinite loop in Virtual Unit Chain 0x%x\n",
523 thisVUC);
524 return 0xffff;
527 /* Skip to next block in chain */
528 writeEUN = nftl->ReplUnitTable[writeEUN];
531 /* OK. We didn't find one in the existing chain, or there
532 is no existing chain. */
534 /* Try to find an already-free block */
535 writeEUN = NFTL_findfreeblock(nftl, 0);
537 if (writeEUN == BLOCK_NIL) {
538 /* That didn't work - there were no free blocks just
539 waiting to be picked up. We're going to have to fold
540 a chain to make room.
543 /* First remember the start of this chain */
544 //u16 startEUN = nftl->EUNtable[thisVUC];
546 //printk("Write to VirtualUnitChain %d, calling makefreeblock()\n", thisVUC);
547 writeEUN = NFTL_makefreeblock(nftl, 0xffff);
549 if (writeEUN == BLOCK_NIL) {
550 /* OK, we accept that the above comment is
551 lying - there may have been free blocks
552 last time we called NFTL_findfreeblock(),
553 but they are reserved for when we're
554 desperate. Well, now we're desperate.
556 DEBUG(MTD_DEBUG_LEVEL1, "Using desperate==1 to find free EUN to accommodate write to VUC %d\n", thisVUC);
557 writeEUN = NFTL_findfreeblock(nftl, 1);
559 if (writeEUN == BLOCK_NIL) {
560 /* Ouch. This should never happen - we should
561 always be able to make some room somehow.
562 If we get here, we've allocated more storage
563 space than actual media, or our makefreeblock
564 routine is missing something.
566 printk(KERN_WARNING "Cannot make free space.\n");
567 return BLOCK_NIL;
569 //printk("Restarting scan\n");
570 lastEUN = BLOCK_NIL;
571 continue;
574 /* We've found a free block. Insert it into the chain. */
576 if (lastEUN != BLOCK_NIL) {
577 thisVUC |= 0x8000; /* It's a replacement block */
578 } else {
579 /* The first block in a new chain */
580 nftl->EUNtable[thisVUC] = writeEUN;
583 /* set up the actual EUN we're writing into */
584 /* Both in our cache... */
585 nftl->ReplUnitTable[writeEUN] = BLOCK_NIL;
587 /* ... and on the flash itself */
588 MTD_READOOB(nftl->mbd.mtd, writeEUN * nftl->EraseSize + 8, 8,
589 &retlen, (char *)&oob.u);
591 oob.u.a.VirtUnitNum = oob.u.a.SpareVirtUnitNum = cpu_to_le16(thisVUC);
593 MTD_WRITEOOB(nftl->mbd.mtd, writeEUN * nftl->EraseSize + 8, 8,
594 &retlen, (char *)&oob.u);
596 /* we link the new block to the chain only after the
597 block is ready. It avoids the case where the chain
598 could point to a free block */
599 if (lastEUN != BLOCK_NIL) {
600 /* Both in our cache... */
601 nftl->ReplUnitTable[lastEUN] = writeEUN;
602 /* ... and on the flash itself */
603 MTD_READOOB(nftl->mbd.mtd, (lastEUN * nftl->EraseSize) + 8,
604 8, &retlen, (char *)&oob.u);
606 oob.u.a.ReplUnitNum = oob.u.a.SpareReplUnitNum
607 = cpu_to_le16(writeEUN);
609 MTD_WRITEOOB(nftl->mbd.mtd, (lastEUN * nftl->EraseSize) + 8,
610 8, &retlen, (char *)&oob.u);
613 return writeEUN;
615 } while (silly2--);
617 printk(KERN_WARNING "Error folding to make room for Virtual Unit Chain 0x%x\n",
618 thisVUC);
619 return 0xffff;
622 static int nftl_writeblock(struct mtd_blktrans_dev *mbd, unsigned long block,
623 char *buffer)
625 struct NFTLrecord *nftl = (void *)mbd;
626 u16 writeEUN;
627 unsigned long blockofs = (block * 512) & (nftl->EraseSize - 1);
628 size_t retlen;
629 struct nftl_oob oob;
631 writeEUN = NFTL_findwriteunit(nftl, block);
633 if (writeEUN == BLOCK_NIL) {
634 printk(KERN_WARNING
635 "NFTL_writeblock(): Cannot find block to write to\n");
636 /* If we _still_ haven't got a block to use, we're screwed */
637 return 1;
640 memset(&oob, 0xff, sizeof(struct nftl_oob));
641 oob.b.Status = oob.b.Status1 = SECTOR_USED;
642 MTD_WRITEECC(nftl->mbd.mtd, (writeEUN * nftl->EraseSize) + blockofs,
643 512, &retlen, (char *)buffer, (char *)&oob, &nftl->oobinfo);
644 /* need to write SECTOR_USED flags since they are not written in mtd_writeecc */
646 return 0;
648 #endif /* CONFIG_NFTL_RW */
650 static int nftl_readblock(struct mtd_blktrans_dev *mbd, unsigned long block,
651 char *buffer)
653 struct NFTLrecord *nftl = (void *)mbd;
654 u16 lastgoodEUN;
655 u16 thisEUN = nftl->EUNtable[block / (nftl->EraseSize / 512)];
656 unsigned long blockofs = (block * 512) & (nftl->EraseSize - 1);
657 unsigned int status;
658 int silly = MAX_LOOPS;
659 size_t retlen;
660 struct nftl_bci bci;
662 lastgoodEUN = BLOCK_NIL;
664 if (thisEUN != BLOCK_NIL) {
665 while (thisEUN < nftl->nb_blocks) {
666 if (MTD_READOOB(nftl->mbd.mtd, (thisEUN * nftl->EraseSize) + blockofs,
667 8, &retlen, (char *)&bci) < 0)
668 status = SECTOR_IGNORE;
669 else
670 status = bci.Status | bci.Status1;
672 switch (status) {
673 case SECTOR_FREE:
674 /* no modification of a sector should follow a free sector */
675 goto the_end;
676 case SECTOR_DELETED:
677 lastgoodEUN = BLOCK_NIL;
678 break;
679 case SECTOR_USED:
680 lastgoodEUN = thisEUN;
681 break;
682 case SECTOR_IGNORE:
683 break;
684 default:
685 printk("Unknown status for block %ld in EUN %d: %x\n",
686 block, thisEUN, status);
687 break;
690 if (!silly--) {
691 printk(KERN_WARNING "Infinite loop in Virtual Unit Chain 0x%lx\n",
692 block / (nftl->EraseSize / 512));
693 return 1;
695 thisEUN = nftl->ReplUnitTable[thisEUN];
699 the_end:
700 if (lastgoodEUN == BLOCK_NIL) {
701 /* the requested block is not on the media, return all 0x00 */
702 memset(buffer, 0, 512);
703 } else {
704 loff_t ptr = (lastgoodEUN * nftl->EraseSize) + blockofs;
705 size_t retlen;
706 if (MTD_READ(nftl->mbd.mtd, ptr, 512, &retlen, buffer))
707 return -EIO;
709 return 0;
712 static int nftl_getgeo(struct mtd_blktrans_dev *dev, struct hd_geometry *geo)
714 struct NFTLrecord *nftl = (void *)dev;
716 geo->heads = nftl->heads;
717 geo->sectors = nftl->sectors;
718 geo->cylinders = nftl->cylinders;
720 return 0;
723 /****************************************************************************
725 * Module stuff
727 ****************************************************************************/
730 static struct mtd_blktrans_ops nftl_tr = {
731 .name = "nftl",
732 .major = NFTL_MAJOR,
733 .part_bits = NFTL_PARTN_BITS,
734 .getgeo = nftl_getgeo,
735 .readsect = nftl_readblock,
736 #ifdef CONFIG_NFTL_RW
737 .writesect = nftl_writeblock,
738 #endif
739 .add_mtd = nftl_add_mtd,
740 .remove_dev = nftl_remove_dev,
741 .owner = THIS_MODULE,
744 extern char nftlmountrev[];
746 static int __init init_nftl(void)
748 printk(KERN_INFO "NFTL driver: nftlcore.c $Revision: 1.98 $, nftlmount.c %s\n", nftlmountrev);
750 return register_mtd_blktrans(&nftl_tr);
753 static void __exit cleanup_nftl(void)
755 deregister_mtd_blktrans(&nftl_tr);
758 module_init(init_nftl);
759 module_exit(cleanup_nftl);
761 MODULE_LICENSE("GPL");
762 MODULE_AUTHOR("David Woodhouse <dwmw2@infradead.org>, Fabrice Bellard <fabrice.bellard@netgem.com> et al.");
763 MODULE_DESCRIPTION("Support code for NAND Flash Translation Layer, used on M-Systems DiskOnChip 2000 and Millennium");