1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Linux driver for NAND Flash Translation Layer
5 * Copyright © 1999 Machine Vision Holdings, Inc.
6 * Copyright © 1999-2010 David Woodhouse <dwmw2@infradead.org>
11 #include <linux/kernel.h>
12 #include <linux/module.h>
13 #include <asm/errno.h>
15 #include <linux/uaccess.h>
16 #include <linux/delay.h>
17 #include <linux/slab.h>
18 #include <linux/init.h>
19 #include <linux/hdreg.h>
20 #include <linux/blkdev.h>
22 #include <linux/kmod.h>
23 #include <linux/mtd/mtd.h>
24 #include <linux/mtd/rawnand.h>
25 #include <linux/mtd/nftl.h>
26 #include <linux/mtd/blktrans.h>
28 /* maximum number of loops while examining next block, to have a
29 chance to detect consistency problems (they should never happen
30 because of the checks done in the mounting */
32 #define MAX_LOOPS 10000
35 static void nftl_add_mtd(struct mtd_blktrans_ops *tr, struct mtd_info *mtd)
37 struct NFTLrecord *nftl;
40 if (!mtd_type_is_nand(mtd) || mtd->size > UINT_MAX)
42 /* OK, this is moderately ugly. But probably safe. Alternatives? */
43 if (memcmp(mtd->name, "DiskOnChip", 10))
46 pr_debug("NFTL: add_mtd for %s\n", mtd->name);
48 nftl = kzalloc(sizeof(struct NFTLrecord), GFP_KERNEL);
54 nftl->mbd.devnum = -1;
58 if (NFTL_mount(nftl) < 0) {
59 printk(KERN_WARNING "NFTL: could not mount device\n");
64 /* OK, it's a new one. Set up all the data structures. */
66 /* Calculate geometry */
67 nftl->cylinders = 1024;
70 temp = nftl->cylinders * nftl->heads;
71 nftl->sectors = nftl->mbd.size / temp;
72 if (nftl->mbd.size % temp) {
74 temp = nftl->cylinders * nftl->sectors;
75 nftl->heads = nftl->mbd.size / temp;
77 if (nftl->mbd.size % temp) {
79 temp = nftl->heads * nftl->sectors;
80 nftl->cylinders = nftl->mbd.size / temp;
84 if (nftl->mbd.size != nftl->heads * nftl->cylinders * nftl->sectors) {
87 mbd.size == heads * cylinders * sectors
89 printk(KERN_WARNING "NFTL: cannot calculate a geometry to "
90 "match size of 0x%lx.\n", nftl->mbd.size);
91 printk(KERN_WARNING "NFTL: using C:%d H:%d S:%d "
93 nftl->cylinders, nftl->heads , nftl->sectors,
94 (long)nftl->cylinders * (long)nftl->heads *
95 (long)nftl->sectors );
98 if (add_mtd_blktrans_dev(&nftl->mbd)) {
99 kfree(nftl->ReplUnitTable);
100 kfree(nftl->EUNtable);
105 printk(KERN_INFO "NFTL: Found new nftl%c\n", nftl->mbd.devnum + 'a');
109 static void nftl_remove_dev(struct mtd_blktrans_dev *dev)
111 struct NFTLrecord *nftl = (void *)dev;
113 pr_debug("NFTL: remove_dev (i=%d)\n", dev->devnum);
115 del_mtd_blktrans_dev(dev);
116 kfree(nftl->ReplUnitTable);
117 kfree(nftl->EUNtable);
121 * Read oob data from flash
123 int nftl_read_oob(struct mtd_info *mtd, loff_t offs, size_t len,
124 size_t *retlen, uint8_t *buf)
126 loff_t mask = mtd->writesize - 1;
127 struct mtd_oob_ops ops;
130 ops.mode = MTD_OPS_PLACE_OOB;
131 ops.ooboffs = offs & mask;
136 res = mtd_read_oob(mtd, offs & ~mask, &ops);
137 *retlen = ops.oobretlen;
142 * Write oob data to flash
144 int nftl_write_oob(struct mtd_info *mtd, loff_t offs, size_t len,
145 size_t *retlen, uint8_t *buf)
147 loff_t mask = mtd->writesize - 1;
148 struct mtd_oob_ops ops;
151 ops.mode = MTD_OPS_PLACE_OOB;
152 ops.ooboffs = offs & mask;
157 res = mtd_write_oob(mtd, offs & ~mask, &ops);
158 *retlen = ops.oobretlen;
162 #ifdef CONFIG_NFTL_RW
165 * Write data and oob to flash
167 static int nftl_write(struct mtd_info *mtd, loff_t offs, size_t len,
168 size_t *retlen, uint8_t *buf, uint8_t *oob)
170 loff_t mask = mtd->writesize - 1;
171 struct mtd_oob_ops ops;
174 ops.mode = MTD_OPS_PLACE_OOB;
175 ops.ooboffs = offs & mask;
176 ops.ooblen = mtd->oobsize;
181 res = mtd_write_oob(mtd, offs & ~mask, &ops);
182 *retlen = ops.retlen;
186 /* Actual NFTL access routines */
187 /* NFTL_findfreeblock: Find a free Erase Unit on the NFTL partition. This function is used
188 * when the give Virtual Unit Chain
190 static u16 NFTL_findfreeblock(struct NFTLrecord *nftl, int desperate )
192 /* For a given Virtual Unit Chain: find or create a free block and
193 add it to the chain */
194 /* We're passed the number of the last EUN in the chain, to save us from
195 having to look it up again */
196 u16 pot = nftl->LastFreeEUN;
197 int silly = nftl->nb_blocks;
199 /* Normally, we force a fold to happen before we run out of free blocks completely */
200 if (!desperate && nftl->numfreeEUNs < 2) {
201 pr_debug("NFTL_findfreeblock: there are too few free EUNs\n");
205 /* Scan for a free block */
207 if (nftl->ReplUnitTable[pot] == BLOCK_FREE) {
208 nftl->LastFreeEUN = pot;
213 /* This will probably point to the MediaHdr unit itself,
214 right at the beginning of the partition. But that unit
215 (and the backup unit too) should have the UCI set
216 up so that it's not selected for overwriting */
217 if (++pot > nftl->lastEUN)
218 pot = le16_to_cpu(nftl->MediaHdr.FirstPhysicalEUN);
221 printk("Argh! No free blocks found! LastFreeEUN = %d, "
222 "FirstEUN = %d\n", nftl->LastFreeEUN,
223 le16_to_cpu(nftl->MediaHdr.FirstPhysicalEUN));
226 } while (pot != nftl->LastFreeEUN);
231 static u16 NFTL_foldchain (struct NFTLrecord *nftl, unsigned thisVUC, unsigned pendingblock )
233 struct mtd_info *mtd = nftl->mbd.mtd;
234 u16 BlockMap[MAX_SECTORS_PER_UNIT];
235 unsigned char BlockLastState[MAX_SECTORS_PER_UNIT];
236 unsigned char BlockFreeFound[MAX_SECTORS_PER_UNIT];
237 unsigned int thisEUN;
240 unsigned int targetEUN;
245 memset(BlockMap, 0xff, sizeof(BlockMap));
246 memset(BlockFreeFound, 0, sizeof(BlockFreeFound));
248 thisEUN = nftl->EUNtable[thisVUC];
250 if (thisEUN == BLOCK_NIL) {
251 printk(KERN_WARNING "Trying to fold non-existent "
252 "Virtual Unit Chain %d!\n", thisVUC);
256 /* Scan to find the Erase Unit which holds the actual data for each
257 512-byte block within the Chain.
260 targetEUN = BLOCK_NIL;
261 while (thisEUN <= nftl->lastEUN ) {
262 unsigned int status, foldmark;
265 for (block = 0; block < nftl->EraseSize / 512; block ++) {
266 nftl_read_oob(mtd, (thisEUN * nftl->EraseSize) +
267 (block * 512), 16 , &retlen,
270 foldmark = oob.u.c.FoldMark | oob.u.c.FoldMark1;
271 if (foldmark == FOLD_MARK_IN_PROGRESS) {
272 pr_debug("Write Inhibited on EUN %d\n", thisEUN);
275 /* There's no other reason not to do inplace,
276 except ones that come later. So we don't need
277 to preserve inplace */
281 status = oob.b.Status | oob.b.Status1;
282 BlockLastState[block] = status;
286 BlockFreeFound[block] = 1;
290 if (!BlockFreeFound[block])
291 BlockMap[block] = thisEUN;
294 "SECTOR_USED found after SECTOR_FREE "
295 "in Virtual Unit Chain %d for block %d\n",
299 if (!BlockFreeFound[block])
300 BlockMap[block] = BLOCK_NIL;
303 "SECTOR_DELETED found after SECTOR_FREE "
304 "in Virtual Unit Chain %d for block %d\n",
311 printk("Unknown status for block %d in EUN %d: %x\n",
312 block, thisEUN, status);
317 printk(KERN_WARNING "Infinite loop in Virtual Unit Chain 0x%x\n",
322 thisEUN = nftl->ReplUnitTable[thisEUN];
326 /* We're being asked to be a fold-in-place. Check
327 that all blocks which actually have data associated
328 with them (i.e. BlockMap[block] != BLOCK_NIL) are
329 either already present or SECTOR_FREE in the target
330 block. If not, we're going to have to fold out-of-place
333 for (block = 0; block < nftl->EraseSize / 512 ; block++) {
334 if (BlockLastState[block] != SECTOR_FREE &&
335 BlockMap[block] != BLOCK_NIL &&
336 BlockMap[block] != targetEUN) {
337 pr_debug("Setting inplace to 0. VUC %d, "
338 "block %d was %x lastEUN, "
339 "and is in EUN %d (%s) %d\n",
340 thisVUC, block, BlockLastState[block],
342 BlockMap[block]== targetEUN ? "==" : "!=",
349 if (pendingblock >= (thisVUC * (nftl->EraseSize / 512)) &&
350 pendingblock < ((thisVUC + 1)* (nftl->EraseSize / 512)) &&
351 BlockLastState[pendingblock - (thisVUC * (nftl->EraseSize / 512))] !=
353 pr_debug("Pending write not free in EUN %d. "
354 "Folding out of place.\n", targetEUN);
360 pr_debug("Cannot fold Virtual Unit Chain %d in place. "
361 "Trying out-of-place\n", thisVUC);
362 /* We need to find a targetEUN to fold into. */
363 targetEUN = NFTL_findfreeblock(nftl, 1);
364 if (targetEUN == BLOCK_NIL) {
365 /* Ouch. Now we're screwed. We need to do a
366 fold-in-place of another chain to make room
367 for this one. We need a better way of selecting
368 which chain to fold, because makefreeblock will
369 only ask us to fold the same one again.
372 "NFTL_findfreeblock(desperate) returns 0xffff.\n");
376 /* We put a fold mark in the chain we are folding only if we
377 fold in place to help the mount check code. If we do not fold in
378 place, it is possible to find the valid chain by selecting the
380 oob.u.c.FoldMark = oob.u.c.FoldMark1 = cpu_to_le16(FOLD_MARK_IN_PROGRESS);
381 oob.u.c.unused = 0xffffffff;
382 nftl_write_oob(mtd, (nftl->EraseSize * targetEUN) + 2 * 512 + 8,
383 8, &retlen, (char *)&oob.u);
386 /* OK. We now know the location of every block in the Virtual Unit Chain,
387 and the Erase Unit into which we are supposed to be copying.
390 pr_debug("Folding chain %d into unit %d\n", thisVUC, targetEUN);
391 for (block = 0; block < nftl->EraseSize / 512 ; block++) {
392 unsigned char movebuf[512];
395 /* If it's in the target EUN already, or if it's pending write, do nothing */
396 if (BlockMap[block] == targetEUN ||
397 (pendingblock == (thisVUC * (nftl->EraseSize / 512) + block))) {
401 /* copy only in non free block (free blocks can only
402 happen in case of media errors or deleted blocks) */
403 if (BlockMap[block] == BLOCK_NIL)
407 (nftl->EraseSize * BlockMap[block]) + (block * 512),
411 if (ret < 0 && !mtd_is_bitflip(ret)) {
413 (nftl->EraseSize * BlockMap[block]) + (block * 512),
418 printk("Error went away on retry.\n");
420 memset(&oob, 0xff, sizeof(struct nftl_oob));
421 oob.b.Status = oob.b.Status1 = SECTOR_USED;
423 nftl_write(nftl->mbd.mtd, (nftl->EraseSize * targetEUN) +
424 (block * 512), 512, &retlen, movebuf, (char *)&oob);
427 /* add the header so that it is now a valid chain */
428 oob.u.a.VirtUnitNum = oob.u.a.SpareVirtUnitNum = cpu_to_le16(thisVUC);
429 oob.u.a.ReplUnitNum = oob.u.a.SpareReplUnitNum = BLOCK_NIL;
431 nftl_write_oob(mtd, (nftl->EraseSize * targetEUN) + 8,
432 8, &retlen, (char *)&oob.u);
434 /* OK. We've moved the whole lot into the new block. Now we have to free the original blocks. */
436 /* At this point, we have two different chains for this Virtual Unit, and no way to tell
437 them apart. If we crash now, we get confused. However, both contain the same data, so we
438 shouldn't actually lose data in this case. It's just that when we load up on a medium which
439 has duplicate chains, we need to free one of the chains because it's not necessary any more.
441 thisEUN = nftl->EUNtable[thisVUC];
442 pr_debug("Want to erase\n");
444 /* For each block in the old chain (except the targetEUN of course),
445 free it and make it available for future use */
446 while (thisEUN <= nftl->lastEUN && thisEUN != targetEUN) {
449 EUNtmp = nftl->ReplUnitTable[thisEUN];
451 if (NFTL_formatblock(nftl, thisEUN) < 0) {
452 /* could not erase : mark block as reserved
454 nftl->ReplUnitTable[thisEUN] = BLOCK_RESERVED;
456 /* correctly erased : mark it as free */
457 nftl->ReplUnitTable[thisEUN] = BLOCK_FREE;
463 /* Make this the new start of chain for thisVUC */
464 nftl->ReplUnitTable[targetEUN] = BLOCK_NIL;
465 nftl->EUNtable[thisVUC] = targetEUN;
470 static u16 NFTL_makefreeblock( struct NFTLrecord *nftl , unsigned pendingblock)
472 /* This is the part that needs some cleverness applied.
473 For now, I'm doing the minimum applicable to actually
474 get the thing to work.
475 Wear-levelling and other clever stuff needs to be implemented
476 and we also need to do some assessment of the results when
477 the system loses power half-way through the routine.
479 u16 LongestChain = 0;
480 u16 ChainLength = 0, thislen;
483 for (chain = 0; chain < le32_to_cpu(nftl->MediaHdr.FormattedSize) / nftl->EraseSize; chain++) {
484 EUN = nftl->EUNtable[chain];
487 while (EUN <= nftl->lastEUN) {
489 //printk("VUC %d reaches len %d with EUN %d\n", chain, thislen, EUN);
490 EUN = nftl->ReplUnitTable[EUN] & 0x7fff;
491 if (thislen > 0xff00) {
492 printk("Endless loop in Virtual Chain %d: Unit %x\n",
495 if (thislen > 0xff10) {
496 /* Actually, don't return failure. Just ignore this chain and
503 if (thislen > ChainLength) {
504 //printk("New longest chain is %d with length %d\n", chain, thislen);
505 ChainLength = thislen;
506 LongestChain = chain;
510 if (ChainLength < 2) {
511 printk(KERN_WARNING "No Virtual Unit Chains available for folding. "
512 "Failing request\n");
516 return NFTL_foldchain (nftl, LongestChain, pendingblock);
519 /* NFTL_findwriteunit: Return the unit number into which we can write
520 for this block. Make it available if it isn't already
522 static inline u16 NFTL_findwriteunit(struct NFTLrecord *nftl, unsigned block)
525 u16 thisVUC = block / (nftl->EraseSize / 512);
526 struct mtd_info *mtd = nftl->mbd.mtd;
527 unsigned int writeEUN;
528 unsigned long blockofs = (block * 512) & (nftl->EraseSize -1);
530 int silly, silly2 = 3;
534 /* Scan the media to find a unit in the VUC which has
535 a free space for the block in question.
538 /* This condition catches the 0x[7f]fff cases, as well as
539 being a sanity check for past-end-of-media access
542 writeEUN = nftl->EUNtable[thisVUC];
544 while (writeEUN <= nftl->lastEUN) {
552 (writeEUN * nftl->EraseSize) + blockofs,
553 8, &retlen, (char *)&bci);
555 pr_debug("Status of block %d in EUN %d is %x\n",
556 block , writeEUN, le16_to_cpu(bci.Status));
558 status = bci.Status | bci.Status1;
568 // Invalid block. Don't use it any more. Must implement.
574 "Infinite loop in Virtual Unit Chain 0x%x\n",
579 /* Skip to next block in chain */
580 writeEUN = nftl->ReplUnitTable[writeEUN];
583 /* OK. We didn't find one in the existing chain, or there
584 is no existing chain. */
586 /* Try to find an already-free block */
587 writeEUN = NFTL_findfreeblock(nftl, 0);
589 if (writeEUN == BLOCK_NIL) {
590 /* That didn't work - there were no free blocks just
591 waiting to be picked up. We're going to have to fold
592 a chain to make room.
595 /* First remember the start of this chain */
596 //u16 startEUN = nftl->EUNtable[thisVUC];
598 //printk("Write to VirtualUnitChain %d, calling makefreeblock()\n", thisVUC);
599 writeEUN = NFTL_makefreeblock(nftl, BLOCK_NIL);
601 if (writeEUN == BLOCK_NIL) {
602 /* OK, we accept that the above comment is
603 lying - there may have been free blocks
604 last time we called NFTL_findfreeblock(),
605 but they are reserved for when we're
606 desperate. Well, now we're desperate.
608 pr_debug("Using desperate==1 to find free EUN to accommodate write to VUC %d\n", thisVUC);
609 writeEUN = NFTL_findfreeblock(nftl, 1);
611 if (writeEUN == BLOCK_NIL) {
612 /* Ouch. This should never happen - we should
613 always be able to make some room somehow.
614 If we get here, we've allocated more storage
615 space than actual media, or our makefreeblock
616 routine is missing something.
618 printk(KERN_WARNING "Cannot make free space.\n");
621 //printk("Restarting scan\n");
626 /* We've found a free block. Insert it into the chain. */
628 if (lastEUN != BLOCK_NIL) {
629 thisVUC |= 0x8000; /* It's a replacement block */
631 /* The first block in a new chain */
632 nftl->EUNtable[thisVUC] = writeEUN;
635 /* set up the actual EUN we're writing into */
636 /* Both in our cache... */
637 nftl->ReplUnitTable[writeEUN] = BLOCK_NIL;
639 /* ... and on the flash itself */
640 nftl_read_oob(mtd, writeEUN * nftl->EraseSize + 8, 8,
641 &retlen, (char *)&oob.u);
643 oob.u.a.VirtUnitNum = oob.u.a.SpareVirtUnitNum = cpu_to_le16(thisVUC);
645 nftl_write_oob(mtd, writeEUN * nftl->EraseSize + 8, 8,
646 &retlen, (char *)&oob.u);
648 /* we link the new block to the chain only after the
649 block is ready. It avoids the case where the chain
650 could point to a free block */
651 if (lastEUN != BLOCK_NIL) {
652 /* Both in our cache... */
653 nftl->ReplUnitTable[lastEUN] = writeEUN;
654 /* ... and on the flash itself */
655 nftl_read_oob(mtd, (lastEUN * nftl->EraseSize) + 8,
656 8, &retlen, (char *)&oob.u);
658 oob.u.a.ReplUnitNum = oob.u.a.SpareReplUnitNum
659 = cpu_to_le16(writeEUN);
661 nftl_write_oob(mtd, (lastEUN * nftl->EraseSize) + 8,
662 8, &retlen, (char *)&oob.u);
669 printk(KERN_WARNING "Error folding to make room for Virtual Unit Chain 0x%x\n",
674 static int nftl_writeblock(struct mtd_blktrans_dev *mbd, unsigned long block,
677 struct NFTLrecord *nftl = (void *)mbd;
679 unsigned long blockofs = (block * 512) & (nftl->EraseSize - 1);
683 writeEUN = NFTL_findwriteunit(nftl, block);
685 if (writeEUN == BLOCK_NIL) {
687 "NFTL_writeblock(): Cannot find block to write to\n");
688 /* If we _still_ haven't got a block to use, we're screwed */
692 memset(&oob, 0xff, sizeof(struct nftl_oob));
693 oob.b.Status = oob.b.Status1 = SECTOR_USED;
695 nftl_write(nftl->mbd.mtd, (writeEUN * nftl->EraseSize) + blockofs,
696 512, &retlen, (char *)buffer, (char *)&oob);
699 #endif /* CONFIG_NFTL_RW */
701 static int nftl_readblock(struct mtd_blktrans_dev *mbd, unsigned long block,
704 struct NFTLrecord *nftl = (void *)mbd;
705 struct mtd_info *mtd = nftl->mbd.mtd;
707 u16 thisEUN = nftl->EUNtable[block / (nftl->EraseSize / 512)];
708 unsigned long blockofs = (block * 512) & (nftl->EraseSize - 1);
710 int silly = MAX_LOOPS;
714 lastgoodEUN = BLOCK_NIL;
716 if (thisEUN != BLOCK_NIL) {
717 while (thisEUN < nftl->nb_blocks) {
718 if (nftl_read_oob(mtd, (thisEUN * nftl->EraseSize) +
719 blockofs, 8, &retlen,
721 status = SECTOR_IGNORE;
723 status = bci.Status | bci.Status1;
727 /* no modification of a sector should follow a free sector */
730 lastgoodEUN = BLOCK_NIL;
733 lastgoodEUN = thisEUN;
738 printk("Unknown status for block %ld in EUN %d: %x\n",
739 block, thisEUN, status);
744 printk(KERN_WARNING "Infinite loop in Virtual Unit Chain 0x%lx\n",
745 block / (nftl->EraseSize / 512));
748 thisEUN = nftl->ReplUnitTable[thisEUN];
753 if (lastgoodEUN == BLOCK_NIL) {
754 /* the requested block is not on the media, return all 0x00 */
755 memset(buffer, 0, 512);
757 loff_t ptr = (lastgoodEUN * nftl->EraseSize) + blockofs;
759 int res = mtd_read(mtd, ptr, 512, &retlen, buffer);
761 if (res < 0 && !mtd_is_bitflip(res))
767 static int nftl_getgeo(struct mtd_blktrans_dev *dev, struct hd_geometry *geo)
769 struct NFTLrecord *nftl = (void *)dev;
771 geo->heads = nftl->heads;
772 geo->sectors = nftl->sectors;
773 geo->cylinders = nftl->cylinders;
778 /****************************************************************************
782 ****************************************************************************/
785 static struct mtd_blktrans_ops nftl_tr = {
788 .part_bits = NFTL_PARTN_BITS,
790 .getgeo = nftl_getgeo,
791 .readsect = nftl_readblock,
792 #ifdef CONFIG_NFTL_RW
793 .writesect = nftl_writeblock,
795 .add_mtd = nftl_add_mtd,
796 .remove_dev = nftl_remove_dev,
797 .owner = THIS_MODULE,
800 module_mtd_blktrans(nftl_tr);
802 MODULE_LICENSE("GPL");
803 MODULE_AUTHOR("David Woodhouse <dwmw2@infradead.org>, Fabrice Bellard <fabrice.bellard@netgem.com> et al.");
804 MODULE_DESCRIPTION("Support code for NAND Flash Translation Layer, used on M-Systems DiskOnChip 2000 and Millennium");
805 MODULE_ALIAS_BLOCKDEV_MAJOR(NFTL_MAJOR);