Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* Linux driver for NAND Flash Translation Layer */ |
| 2 | /* (c) 1999 Machine Vision Holdings, Inc. */ |
| 3 | /* Author: David Woodhouse <dwmw2@infradead.org> */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4 | |
| 5 | /* |
| 6 | The contents of this file are distributed under the GNU General |
| 7 | Public License version 2. The author places no additional |
| 8 | restrictions of any kind on it. |
| 9 | */ |
| 10 | |
| 11 | #define PRERELEASE |
| 12 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 13 | #include <linux/kernel.h> |
| 14 | #include <linux/module.h> |
| 15 | #include <asm/errno.h> |
| 16 | #include <asm/io.h> |
| 17 | #include <asm/uaccess.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 18 | #include <linux/delay.h> |
| 19 | #include <linux/slab.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 20 | #include <linux/init.h> |
| 21 | #include <linux/hdreg.h> |
Scott James Remnant | e7f5216 | 2009-03-02 17:43:04 +0000 | [diff] [blame] | 22 | #include <linux/blkdev.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 23 | |
| 24 | #include <linux/kmod.h> |
| 25 | #include <linux/mtd/mtd.h> |
| 26 | #include <linux/mtd/nand.h> |
| 27 | #include <linux/mtd/nftl.h> |
| 28 | #include <linux/mtd/blktrans.h> |
| 29 | |
| 30 | /* maximum number of loops while examining next block, to have a |
| 31 | chance to detect consistency problems (they should never happen |
| 32 | because of the checks done in the mounting */ |
| 33 | |
| 34 | #define MAX_LOOPS 10000 |
| 35 | |
| 36 | |
| 37 | static void nftl_add_mtd(struct mtd_blktrans_ops *tr, struct mtd_info *mtd) |
| 38 | { |
| 39 | struct NFTLrecord *nftl; |
| 40 | unsigned long temp; |
| 41 | |
Adrian Hunter | 69423d9 | 2008-12-10 13:37:21 +0000 | [diff] [blame] | 42 | if (mtd->type != MTD_NANDFLASH || mtd->size > UINT_MAX) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 43 | return; |
| 44 | /* OK, this is moderately ugly. But probably safe. Alternatives? */ |
| 45 | if (memcmp(mtd->name, "DiskOnChip", 10)) |
| 46 | return; |
| 47 | |
| 48 | if (!mtd->block_isbad) { |
| 49 | printk(KERN_ERR |
| 50 | "NFTL no longer supports the old DiskOnChip drivers loaded via docprobe.\n" |
| 51 | "Please use the new diskonchip driver under the NAND subsystem.\n"); |
| 52 | return; |
| 53 | } |
| 54 | |
| 55 | DEBUG(MTD_DEBUG_LEVEL1, "NFTL: add_mtd for %s\n", mtd->name); |
| 56 | |
Burman Yan | 95b93a0 | 2006-11-15 21:10:29 +0200 | [diff] [blame] | 57 | nftl = kzalloc(sizeof(struct NFTLrecord), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 58 | |
| 59 | if (!nftl) { |
| 60 | printk(KERN_WARNING "NFTL: out of memory for data structures\n"); |
| 61 | return; |
| 62 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 63 | |
| 64 | nftl->mbd.mtd = mtd; |
| 65 | nftl->mbd.devnum = -1; |
Richard Purdie | 1918767 | 2006-10-27 09:09:33 +0100 | [diff] [blame] | 66 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 67 | nftl->mbd.tr = tr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 68 | |
| 69 | if (NFTL_mount(nftl) < 0) { |
| 70 | printk(KERN_WARNING "NFTL: could not mount device\n"); |
| 71 | kfree(nftl); |
| 72 | return; |
| 73 | } |
| 74 | |
| 75 | /* OK, it's a new one. Set up all the data structures. */ |
| 76 | |
| 77 | /* Calculate geometry */ |
| 78 | nftl->cylinders = 1024; |
| 79 | nftl->heads = 16; |
| 80 | |
| 81 | temp = nftl->cylinders * nftl->heads; |
| 82 | nftl->sectors = nftl->mbd.size / temp; |
| 83 | if (nftl->mbd.size % temp) { |
| 84 | nftl->sectors++; |
| 85 | temp = nftl->cylinders * nftl->sectors; |
| 86 | nftl->heads = nftl->mbd.size / temp; |
| 87 | |
| 88 | if (nftl->mbd.size % temp) { |
| 89 | nftl->heads++; |
| 90 | temp = nftl->heads * nftl->sectors; |
| 91 | nftl->cylinders = nftl->mbd.size / temp; |
| 92 | } |
| 93 | } |
| 94 | |
| 95 | if (nftl->mbd.size != nftl->heads * nftl->cylinders * nftl->sectors) { |
| 96 | /* |
Thomas Gleixner | 97894cd | 2005-11-07 11:15:26 +0000 | [diff] [blame] | 97 | Oh no we don't have |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 98 | mbd.size == heads * cylinders * sectors |
| 99 | */ |
| 100 | printk(KERN_WARNING "NFTL: cannot calculate a geometry to " |
| 101 | "match size of 0x%lx.\n", nftl->mbd.size); |
| 102 | printk(KERN_WARNING "NFTL: using C:%d H:%d S:%d " |
| 103 | "(== 0x%lx sects)\n", |
Thomas Gleixner | 97894cd | 2005-11-07 11:15:26 +0000 | [diff] [blame] | 104 | nftl->cylinders, nftl->heads , nftl->sectors, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 105 | (long)nftl->cylinders * (long)nftl->heads * |
| 106 | (long)nftl->sectors ); |
| 107 | } |
| 108 | |
| 109 | if (add_mtd_blktrans_dev(&nftl->mbd)) { |
Jesper Juhl | fa67164 | 2005-11-07 01:01:27 -0800 | [diff] [blame] | 110 | kfree(nftl->ReplUnitTable); |
| 111 | kfree(nftl->EUNtable); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 112 | kfree(nftl); |
| 113 | return; |
| 114 | } |
| 115 | #ifdef PSYCHO_DEBUG |
| 116 | printk(KERN_INFO "NFTL: Found new nftl%c\n", nftl->mbd.devnum + 'a'); |
| 117 | #endif |
| 118 | } |
| 119 | |
| 120 | static void nftl_remove_dev(struct mtd_blktrans_dev *dev) |
| 121 | { |
| 122 | struct NFTLrecord *nftl = (void *)dev; |
| 123 | |
| 124 | DEBUG(MTD_DEBUG_LEVEL1, "NFTL: remove_dev (i=%d)\n", dev->devnum); |
| 125 | |
| 126 | del_mtd_blktrans_dev(dev); |
Jesper Juhl | fa67164 | 2005-11-07 01:01:27 -0800 | [diff] [blame] | 127 | kfree(nftl->ReplUnitTable); |
| 128 | kfree(nftl->EUNtable); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 129 | } |
| 130 | |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 131 | /* |
| 132 | * Read oob data from flash |
| 133 | */ |
| 134 | int nftl_read_oob(struct mtd_info *mtd, loff_t offs, size_t len, |
| 135 | size_t *retlen, uint8_t *buf) |
| 136 | { |
Dimitri Gorokhovik | 16f05c2 | 2009-09-03 14:04:22 +0100 | [diff] [blame] | 137 | loff_t mask = mtd->writesize - 1; |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 138 | struct mtd_oob_ops ops; |
| 139 | int res; |
| 140 | |
| 141 | ops.mode = MTD_OOB_PLACE; |
Dimitri Gorokhovik | 16f05c2 | 2009-09-03 14:04:22 +0100 | [diff] [blame] | 142 | ops.ooboffs = offs & mask; |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 143 | ops.ooblen = len; |
| 144 | ops.oobbuf = buf; |
| 145 | ops.datbuf = NULL; |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 146 | |
Dimitri Gorokhovik | 16f05c2 | 2009-09-03 14:04:22 +0100 | [diff] [blame] | 147 | res = mtd->read_oob(mtd, offs & ~mask, &ops); |
Vitaly Wool | 7014568 | 2006-11-03 18:20:38 +0300 | [diff] [blame] | 148 | *retlen = ops.oobretlen; |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 149 | return res; |
| 150 | } |
| 151 | |
| 152 | /* |
| 153 | * Write oob data to flash |
| 154 | */ |
| 155 | int nftl_write_oob(struct mtd_info *mtd, loff_t offs, size_t len, |
| 156 | size_t *retlen, uint8_t *buf) |
| 157 | { |
Dimitri Gorokhovik | 16f05c2 | 2009-09-03 14:04:22 +0100 | [diff] [blame] | 158 | loff_t mask = mtd->writesize - 1; |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 159 | struct mtd_oob_ops ops; |
| 160 | int res; |
| 161 | |
| 162 | ops.mode = MTD_OOB_PLACE; |
Dimitri Gorokhovik | 16f05c2 | 2009-09-03 14:04:22 +0100 | [diff] [blame] | 163 | ops.ooboffs = offs & mask; |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 164 | ops.ooblen = len; |
| 165 | ops.oobbuf = buf; |
| 166 | ops.datbuf = NULL; |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 167 | |
Dimitri Gorokhovik | 16f05c2 | 2009-09-03 14:04:22 +0100 | [diff] [blame] | 168 | res = mtd->write_oob(mtd, offs & ~mask, &ops); |
Vitaly Wool | 7014568 | 2006-11-03 18:20:38 +0300 | [diff] [blame] | 169 | *retlen = ops.oobretlen; |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 170 | return res; |
| 171 | } |
| 172 | |
Frederik Deweerdt | 553a801 | 2006-10-02 09:42:25 +0100 | [diff] [blame] | 173 | #ifdef CONFIG_NFTL_RW |
| 174 | |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 175 | /* |
| 176 | * Write data and oob to flash |
| 177 | */ |
| 178 | static int nftl_write(struct mtd_info *mtd, loff_t offs, size_t len, |
| 179 | size_t *retlen, uint8_t *buf, uint8_t *oob) |
| 180 | { |
Dimitri Gorokhovik | 16f05c2 | 2009-09-03 14:04:22 +0100 | [diff] [blame] | 181 | loff_t mask = mtd->writesize - 1; |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 182 | struct mtd_oob_ops ops; |
| 183 | int res; |
| 184 | |
| 185 | ops.mode = MTD_OOB_PLACE; |
Dimitri Gorokhovik | 16f05c2 | 2009-09-03 14:04:22 +0100 | [diff] [blame] | 186 | ops.ooboffs = offs & mask; |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 187 | ops.ooblen = mtd->oobsize; |
| 188 | ops.oobbuf = oob; |
| 189 | ops.datbuf = buf; |
| 190 | ops.len = len; |
| 191 | |
Dimitri Gorokhovik | 16f05c2 | 2009-09-03 14:04:22 +0100 | [diff] [blame] | 192 | res = mtd->write_oob(mtd, offs & ~mask, &ops); |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 193 | *retlen = ops.retlen; |
| 194 | return res; |
| 195 | } |
| 196 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 197 | /* Actual NFTL access routines */ |
| 198 | /* NFTL_findfreeblock: Find a free Erase Unit on the NFTL partition. This function is used |
| 199 | * when the give Virtual Unit Chain |
| 200 | */ |
| 201 | static u16 NFTL_findfreeblock(struct NFTLrecord *nftl, int desperate ) |
| 202 | { |
| 203 | /* For a given Virtual Unit Chain: find or create a free block and |
| 204 | add it to the chain */ |
| 205 | /* We're passed the number of the last EUN in the chain, to save us from |
| 206 | having to look it up again */ |
| 207 | u16 pot = nftl->LastFreeEUN; |
| 208 | int silly = nftl->nb_blocks; |
| 209 | |
| 210 | /* Normally, we force a fold to happen before we run out of free blocks completely */ |
| 211 | if (!desperate && nftl->numfreeEUNs < 2) { |
| 212 | DEBUG(MTD_DEBUG_LEVEL1, "NFTL_findfreeblock: there are too few free EUNs\n"); |
Julia Lawall | 70ec3bb | 2009-06-27 09:55:32 +0200 | [diff] [blame] | 213 | return BLOCK_NIL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 214 | } |
| 215 | |
| 216 | /* Scan for a free block */ |
| 217 | do { |
| 218 | if (nftl->ReplUnitTable[pot] == BLOCK_FREE) { |
| 219 | nftl->LastFreeEUN = pot; |
| 220 | nftl->numfreeEUNs--; |
| 221 | return pot; |
| 222 | } |
| 223 | |
| 224 | /* This will probably point to the MediaHdr unit itself, |
| 225 | right at the beginning of the partition. But that unit |
| 226 | (and the backup unit too) should have the UCI set |
| 227 | up so that it's not selected for overwriting */ |
| 228 | if (++pot > nftl->lastEUN) |
| 229 | pot = le16_to_cpu(nftl->MediaHdr.FirstPhysicalEUN); |
| 230 | |
| 231 | if (!silly--) { |
| 232 | printk("Argh! No free blocks found! LastFreeEUN = %d, " |
Thomas Gleixner | 97894cd | 2005-11-07 11:15:26 +0000 | [diff] [blame] | 233 | "FirstEUN = %d\n", nftl->LastFreeEUN, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 234 | le16_to_cpu(nftl->MediaHdr.FirstPhysicalEUN)); |
Julia Lawall | 70ec3bb | 2009-06-27 09:55:32 +0200 | [diff] [blame] | 235 | return BLOCK_NIL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 236 | } |
| 237 | } while (pot != nftl->LastFreeEUN); |
| 238 | |
Julia Lawall | 70ec3bb | 2009-06-27 09:55:32 +0200 | [diff] [blame] | 239 | return BLOCK_NIL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 240 | } |
| 241 | |
| 242 | static u16 NFTL_foldchain (struct NFTLrecord *nftl, unsigned thisVUC, unsigned pendingblock ) |
| 243 | { |
Thomas Gleixner | f4a43cf | 2006-05-28 11:01:53 +0200 | [diff] [blame] | 244 | struct mtd_info *mtd = nftl->mbd.mtd; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 245 | u16 BlockMap[MAX_SECTORS_PER_UNIT]; |
| 246 | unsigned char BlockLastState[MAX_SECTORS_PER_UNIT]; |
| 247 | unsigned char BlockFreeFound[MAX_SECTORS_PER_UNIT]; |
| 248 | unsigned int thisEUN; |
| 249 | int block; |
| 250 | int silly; |
| 251 | unsigned int targetEUN; |
| 252 | struct nftl_oob oob; |
| 253 | int inplace = 1; |
Thomas Gleixner | f4a43cf | 2006-05-28 11:01:53 +0200 | [diff] [blame] | 254 | size_t retlen; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 255 | |
| 256 | memset(BlockMap, 0xff, sizeof(BlockMap)); |
| 257 | memset(BlockFreeFound, 0, sizeof(BlockFreeFound)); |
| 258 | |
| 259 | thisEUN = nftl->EUNtable[thisVUC]; |
| 260 | |
| 261 | if (thisEUN == BLOCK_NIL) { |
| 262 | printk(KERN_WARNING "Trying to fold non-existent " |
| 263 | "Virtual Unit Chain %d!\n", thisVUC); |
| 264 | return BLOCK_NIL; |
| 265 | } |
Thomas Gleixner | 97894cd | 2005-11-07 11:15:26 +0000 | [diff] [blame] | 266 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 267 | /* Scan to find the Erase Unit which holds the actual data for each |
| 268 | 512-byte block within the Chain. |
| 269 | */ |
Thomas Gleixner | f4a43cf | 2006-05-28 11:01:53 +0200 | [diff] [blame] | 270 | silly = MAX_LOOPS; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 271 | targetEUN = BLOCK_NIL; |
| 272 | while (thisEUN <= nftl->lastEUN ) { |
Thomas Gleixner | f4a43cf | 2006-05-28 11:01:53 +0200 | [diff] [blame] | 273 | unsigned int status, foldmark; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 274 | |
| 275 | targetEUN = thisEUN; |
| 276 | for (block = 0; block < nftl->EraseSize / 512; block ++) { |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 277 | nftl_read_oob(mtd, (thisEUN * nftl->EraseSize) + |
Thomas Gleixner | f4a43cf | 2006-05-28 11:01:53 +0200 | [diff] [blame] | 278 | (block * 512), 16 , &retlen, |
| 279 | (char *)&oob); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 280 | if (block == 2) { |
Thomas Gleixner | f4a43cf | 2006-05-28 11:01:53 +0200 | [diff] [blame] | 281 | foldmark = oob.u.c.FoldMark | oob.u.c.FoldMark1; |
| 282 | if (foldmark == FOLD_MARK_IN_PROGRESS) { |
| 283 | DEBUG(MTD_DEBUG_LEVEL1, |
| 284 | "Write Inhibited on EUN %d\n", thisEUN); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 285 | inplace = 0; |
| 286 | } else { |
| 287 | /* There's no other reason not to do inplace, |
| 288 | except ones that come later. So we don't need |
| 289 | to preserve inplace */ |
| 290 | inplace = 1; |
| 291 | } |
| 292 | } |
Thomas Gleixner | f4a43cf | 2006-05-28 11:01:53 +0200 | [diff] [blame] | 293 | status = oob.b.Status | oob.b.Status1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 294 | BlockLastState[block] = status; |
| 295 | |
| 296 | switch(status) { |
| 297 | case SECTOR_FREE: |
| 298 | BlockFreeFound[block] = 1; |
| 299 | break; |
| 300 | |
| 301 | case SECTOR_USED: |
| 302 | if (!BlockFreeFound[block]) |
| 303 | BlockMap[block] = thisEUN; |
| 304 | else |
Thomas Gleixner | 97894cd | 2005-11-07 11:15:26 +0000 | [diff] [blame] | 305 | printk(KERN_WARNING |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 306 | "SECTOR_USED found after SECTOR_FREE " |
| 307 | "in Virtual Unit Chain %d for block %d\n", |
| 308 | thisVUC, block); |
| 309 | break; |
| 310 | case SECTOR_DELETED: |
| 311 | if (!BlockFreeFound[block]) |
| 312 | BlockMap[block] = BLOCK_NIL; |
| 313 | else |
Thomas Gleixner | 97894cd | 2005-11-07 11:15:26 +0000 | [diff] [blame] | 314 | printk(KERN_WARNING |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 315 | "SECTOR_DELETED found after SECTOR_FREE " |
| 316 | "in Virtual Unit Chain %d for block %d\n", |
| 317 | thisVUC, block); |
| 318 | break; |
| 319 | |
| 320 | case SECTOR_IGNORE: |
| 321 | break; |
| 322 | default: |
| 323 | printk("Unknown status for block %d in EUN %d: %x\n", |
| 324 | block, thisEUN, status); |
| 325 | } |
| 326 | } |
| 327 | |
| 328 | if (!silly--) { |
| 329 | printk(KERN_WARNING "Infinite loop in Virtual Unit Chain 0x%x\n", |
| 330 | thisVUC); |
| 331 | return BLOCK_NIL; |
| 332 | } |
Thomas Gleixner | 97894cd | 2005-11-07 11:15:26 +0000 | [diff] [blame] | 333 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 334 | thisEUN = nftl->ReplUnitTable[thisEUN]; |
| 335 | } |
| 336 | |
| 337 | if (inplace) { |
| 338 | /* We're being asked to be a fold-in-place. Check |
| 339 | that all blocks which actually have data associated |
Thomas Gleixner | 97894cd | 2005-11-07 11:15:26 +0000 | [diff] [blame] | 340 | with them (i.e. BlockMap[block] != BLOCK_NIL) are |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 341 | either already present or SECTOR_FREE in the target |
| 342 | block. If not, we're going to have to fold out-of-place |
| 343 | anyway. |
| 344 | */ |
| 345 | for (block = 0; block < nftl->EraseSize / 512 ; block++) { |
| 346 | if (BlockLastState[block] != SECTOR_FREE && |
| 347 | BlockMap[block] != BLOCK_NIL && |
| 348 | BlockMap[block] != targetEUN) { |
| 349 | DEBUG(MTD_DEBUG_LEVEL1, "Setting inplace to 0. VUC %d, " |
| 350 | "block %d was %x lastEUN, " |
| 351 | "and is in EUN %d (%s) %d\n", |
| 352 | thisVUC, block, BlockLastState[block], |
Thomas Gleixner | 97894cd | 2005-11-07 11:15:26 +0000 | [diff] [blame] | 353 | BlockMap[block], |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 354 | BlockMap[block]== targetEUN ? "==" : "!=", |
| 355 | targetEUN); |
| 356 | inplace = 0; |
| 357 | break; |
| 358 | } |
| 359 | } |
| 360 | |
| 361 | if (pendingblock >= (thisVUC * (nftl->EraseSize / 512)) && |
| 362 | pendingblock < ((thisVUC + 1)* (nftl->EraseSize / 512)) && |
| 363 | BlockLastState[pendingblock - (thisVUC * (nftl->EraseSize / 512))] != |
| 364 | SECTOR_FREE) { |
| 365 | DEBUG(MTD_DEBUG_LEVEL1, "Pending write not free in EUN %d. " |
| 366 | "Folding out of place.\n", targetEUN); |
| 367 | inplace = 0; |
| 368 | } |
| 369 | } |
Thomas Gleixner | 97894cd | 2005-11-07 11:15:26 +0000 | [diff] [blame] | 370 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 371 | if (!inplace) { |
| 372 | DEBUG(MTD_DEBUG_LEVEL1, "Cannot fold Virtual Unit Chain %d in place. " |
| 373 | "Trying out-of-place\n", thisVUC); |
| 374 | /* We need to find a targetEUN to fold into. */ |
| 375 | targetEUN = NFTL_findfreeblock(nftl, 1); |
| 376 | if (targetEUN == BLOCK_NIL) { |
Thomas Gleixner | 97894cd | 2005-11-07 11:15:26 +0000 | [diff] [blame] | 377 | /* Ouch. Now we're screwed. We need to do a |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 378 | fold-in-place of another chain to make room |
| 379 | for this one. We need a better way of selecting |
Thomas Gleixner | 97894cd | 2005-11-07 11:15:26 +0000 | [diff] [blame] | 380 | which chain to fold, because makefreeblock will |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 381 | only ask us to fold the same one again. |
| 382 | */ |
| 383 | printk(KERN_WARNING |
| 384 | "NFTL_findfreeblock(desperate) returns 0xffff.\n"); |
| 385 | return BLOCK_NIL; |
| 386 | } |
| 387 | } else { |
Thomas Gleixner | f4a43cf | 2006-05-28 11:01:53 +0200 | [diff] [blame] | 388 | /* We put a fold mark in the chain we are folding only if we |
| 389 | fold in place to help the mount check code. If we do not fold in |
| 390 | place, it is possible to find the valid chain by selecting the |
| 391 | longer one */ |
| 392 | oob.u.c.FoldMark = oob.u.c.FoldMark1 = cpu_to_le16(FOLD_MARK_IN_PROGRESS); |
| 393 | oob.u.c.unused = 0xffffffff; |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 394 | nftl_write_oob(mtd, (nftl->EraseSize * targetEUN) + 2 * 512 + 8, |
Thomas Gleixner | f4a43cf | 2006-05-28 11:01:53 +0200 | [diff] [blame] | 395 | 8, &retlen, (char *)&oob.u); |
| 396 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 397 | |
| 398 | /* OK. We now know the location of every block in the Virtual Unit Chain, |
| 399 | and the Erase Unit into which we are supposed to be copying. |
| 400 | Go for it. |
| 401 | */ |
| 402 | DEBUG(MTD_DEBUG_LEVEL1,"Folding chain %d into unit %d\n", thisVUC, targetEUN); |
| 403 | for (block = 0; block < nftl->EraseSize / 512 ; block++) { |
| 404 | unsigned char movebuf[512]; |
| 405 | int ret; |
| 406 | |
| 407 | /* If it's in the target EUN already, or if it's pending write, do nothing */ |
| 408 | if (BlockMap[block] == targetEUN || |
| 409 | (pendingblock == (thisVUC * (nftl->EraseSize / 512) + block))) { |
| 410 | continue; |
| 411 | } |
| 412 | |
Thomas Gleixner | f4a43cf | 2006-05-28 11:01:53 +0200 | [diff] [blame] | 413 | /* copy only in non free block (free blocks can only |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 414 | happen in case of media errors or deleted blocks) */ |
Thomas Gleixner | f4a43cf | 2006-05-28 11:01:53 +0200 | [diff] [blame] | 415 | if (BlockMap[block] == BLOCK_NIL) |
| 416 | continue; |
Thomas Gleixner | 97894cd | 2005-11-07 11:15:26 +0000 | [diff] [blame] | 417 | |
Thomas Gleixner | f4a43cf | 2006-05-28 11:01:53 +0200 | [diff] [blame] | 418 | ret = mtd->read(mtd, (nftl->EraseSize * BlockMap[block]) + (block * 512), |
| 419 | 512, &retlen, movebuf); |
Thomas Gleixner | 9a1fcdf | 2006-05-29 14:56:39 +0200 | [diff] [blame] | 420 | if (ret < 0 && ret != -EUCLEAN) { |
Thomas Gleixner | f4a43cf | 2006-05-28 11:01:53 +0200 | [diff] [blame] | 421 | ret = mtd->read(mtd, (nftl->EraseSize * BlockMap[block]) |
| 422 | + (block * 512), 512, &retlen, |
| 423 | movebuf); |
| 424 | if (ret != -EIO) |
| 425 | printk("Error went away on retry.\n"); |
| 426 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 427 | memset(&oob, 0xff, sizeof(struct nftl_oob)); |
| 428 | oob.b.Status = oob.b.Status1 = SECTOR_USED; |
Thomas Gleixner | 9223a45 | 2006-05-23 17:21:03 +0200 | [diff] [blame] | 429 | |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 430 | nftl_write(nftl->mbd.mtd, (nftl->EraseSize * targetEUN) + |
| 431 | (block * 512), 512, &retlen, movebuf, (char *)&oob); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 432 | } |
Thomas Gleixner | 97894cd | 2005-11-07 11:15:26 +0000 | [diff] [blame] | 433 | |
Thomas Gleixner | f4a43cf | 2006-05-28 11:01:53 +0200 | [diff] [blame] | 434 | /* add the header so that it is now a valid chain */ |
| 435 | oob.u.a.VirtUnitNum = oob.u.a.SpareVirtUnitNum = cpu_to_le16(thisVUC); |
Julia Lawall | 70ec3bb | 2009-06-27 09:55:32 +0200 | [diff] [blame] | 436 | oob.u.a.ReplUnitNum = oob.u.a.SpareReplUnitNum = BLOCK_NIL; |
Thomas Gleixner | 97894cd | 2005-11-07 11:15:26 +0000 | [diff] [blame] | 437 | |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 438 | nftl_write_oob(mtd, (nftl->EraseSize * targetEUN) + 8, |
Thomas Gleixner | f4a43cf | 2006-05-28 11:01:53 +0200 | [diff] [blame] | 439 | 8, &retlen, (char *)&oob.u); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 440 | |
| 441 | /* OK. We've moved the whole lot into the new block. Now we have to free the original blocks. */ |
| 442 | |
Thomas Gleixner | 97894cd | 2005-11-07 11:15:26 +0000 | [diff] [blame] | 443 | /* At this point, we have two different chains for this Virtual Unit, and no way to tell |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 444 | them apart. If we crash now, we get confused. However, both contain the same data, so we |
| 445 | shouldn't actually lose data in this case. It's just that when we load up on a medium which |
| 446 | has duplicate chains, we need to free one of the chains because it's not necessary any more. |
| 447 | */ |
| 448 | thisEUN = nftl->EUNtable[thisVUC]; |
| 449 | DEBUG(MTD_DEBUG_LEVEL1,"Want to erase\n"); |
| 450 | |
Thomas Gleixner | 97894cd | 2005-11-07 11:15:26 +0000 | [diff] [blame] | 451 | /* For each block in the old chain (except the targetEUN of course), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 452 | free it and make it available for future use */ |
| 453 | while (thisEUN <= nftl->lastEUN && thisEUN != targetEUN) { |
| 454 | unsigned int EUNtmp; |
| 455 | |
Thomas Gleixner | f4a43cf | 2006-05-28 11:01:53 +0200 | [diff] [blame] | 456 | EUNtmp = nftl->ReplUnitTable[thisEUN]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 457 | |
Thomas Gleixner | f4a43cf | 2006-05-28 11:01:53 +0200 | [diff] [blame] | 458 | if (NFTL_formatblock(nftl, thisEUN) < 0) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 459 | /* could not erase : mark block as reserved |
| 460 | */ |
| 461 | nftl->ReplUnitTable[thisEUN] = BLOCK_RESERVED; |
Thomas Gleixner | f4a43cf | 2006-05-28 11:01:53 +0200 | [diff] [blame] | 462 | } else { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 463 | /* correctly erased : mark it as free */ |
| 464 | nftl->ReplUnitTable[thisEUN] = BLOCK_FREE; |
| 465 | nftl->numfreeEUNs++; |
Thomas Gleixner | f4a43cf | 2006-05-28 11:01:53 +0200 | [diff] [blame] | 466 | } |
| 467 | thisEUN = EUNtmp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 468 | } |
Thomas Gleixner | 97894cd | 2005-11-07 11:15:26 +0000 | [diff] [blame] | 469 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 470 | /* Make this the new start of chain for thisVUC */ |
| 471 | nftl->ReplUnitTable[targetEUN] = BLOCK_NIL; |
| 472 | nftl->EUNtable[thisVUC] = targetEUN; |
| 473 | |
| 474 | return targetEUN; |
| 475 | } |
| 476 | |
| 477 | static u16 NFTL_makefreeblock( struct NFTLrecord *nftl , unsigned pendingblock) |
| 478 | { |
Thomas Gleixner | 97894cd | 2005-11-07 11:15:26 +0000 | [diff] [blame] | 479 | /* This is the part that needs some cleverness applied. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 480 | For now, I'm doing the minimum applicable to actually |
| 481 | get the thing to work. |
| 482 | Wear-levelling and other clever stuff needs to be implemented |
| 483 | and we also need to do some assessment of the results when |
| 484 | the system loses power half-way through the routine. |
| 485 | */ |
| 486 | u16 LongestChain = 0; |
| 487 | u16 ChainLength = 0, thislen; |
| 488 | u16 chain, EUN; |
| 489 | |
| 490 | for (chain = 0; chain < le32_to_cpu(nftl->MediaHdr.FormattedSize) / nftl->EraseSize; chain++) { |
| 491 | EUN = nftl->EUNtable[chain]; |
| 492 | thislen = 0; |
| 493 | |
| 494 | while (EUN <= nftl->lastEUN) { |
| 495 | thislen++; |
| 496 | //printk("VUC %d reaches len %d with EUN %d\n", chain, thislen, EUN); |
| 497 | EUN = nftl->ReplUnitTable[EUN] & 0x7fff; |
| 498 | if (thislen > 0xff00) { |
| 499 | printk("Endless loop in Virtual Chain %d: Unit %x\n", |
| 500 | chain, EUN); |
| 501 | } |
| 502 | if (thislen > 0xff10) { |
| 503 | /* Actually, don't return failure. Just ignore this chain and |
| 504 | get on with it. */ |
| 505 | thislen = 0; |
| 506 | break; |
| 507 | } |
| 508 | } |
| 509 | |
| 510 | if (thislen > ChainLength) { |
| 511 | //printk("New longest chain is %d with length %d\n", chain, thislen); |
| 512 | ChainLength = thislen; |
| 513 | LongestChain = chain; |
| 514 | } |
| 515 | } |
| 516 | |
| 517 | if (ChainLength < 2) { |
| 518 | printk(KERN_WARNING "No Virtual Unit Chains available for folding. " |
| 519 | "Failing request\n"); |
Julia Lawall | 70ec3bb | 2009-06-27 09:55:32 +0200 | [diff] [blame] | 520 | return BLOCK_NIL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 521 | } |
| 522 | |
| 523 | return NFTL_foldchain (nftl, LongestChain, pendingblock); |
| 524 | } |
| 525 | |
Thomas Gleixner | 97894cd | 2005-11-07 11:15:26 +0000 | [diff] [blame] | 526 | /* NFTL_findwriteunit: Return the unit number into which we can write |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 527 | for this block. Make it available if it isn't already |
| 528 | */ |
| 529 | static inline u16 NFTL_findwriteunit(struct NFTLrecord *nftl, unsigned block) |
| 530 | { |
| 531 | u16 lastEUN; |
| 532 | u16 thisVUC = block / (nftl->EraseSize / 512); |
Thomas Gleixner | f4a43cf | 2006-05-28 11:01:53 +0200 | [diff] [blame] | 533 | struct mtd_info *mtd = nftl->mbd.mtd; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 534 | unsigned int writeEUN; |
| 535 | unsigned long blockofs = (block * 512) & (nftl->EraseSize -1); |
| 536 | size_t retlen; |
| 537 | int silly, silly2 = 3; |
| 538 | struct nftl_oob oob; |
| 539 | |
| 540 | do { |
| 541 | /* Scan the media to find a unit in the VUC which has |
| 542 | a free space for the block in question. |
| 543 | */ |
| 544 | |
Thomas Gleixner | 97894cd | 2005-11-07 11:15:26 +0000 | [diff] [blame] | 545 | /* This condition catches the 0x[7f]fff cases, as well as |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 546 | being a sanity check for past-end-of-media access |
| 547 | */ |
| 548 | lastEUN = BLOCK_NIL; |
| 549 | writeEUN = nftl->EUNtable[thisVUC]; |
Thomas Gleixner | f4a43cf | 2006-05-28 11:01:53 +0200 | [diff] [blame] | 550 | silly = MAX_LOOPS; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 551 | while (writeEUN <= nftl->lastEUN) { |
| 552 | struct nftl_bci bci; |
| 553 | size_t retlen; |
Thomas Gleixner | f4a43cf | 2006-05-28 11:01:53 +0200 | [diff] [blame] | 554 | unsigned int status; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 555 | |
| 556 | lastEUN = writeEUN; |
| 557 | |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 558 | nftl_read_oob(mtd, |
Thomas Gleixner | f4a43cf | 2006-05-28 11:01:53 +0200 | [diff] [blame] | 559 | (writeEUN * nftl->EraseSize) + blockofs, |
| 560 | 8, &retlen, (char *)&bci); |
Thomas Gleixner | 97894cd | 2005-11-07 11:15:26 +0000 | [diff] [blame] | 561 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 562 | DEBUG(MTD_DEBUG_LEVEL2, "Status of block %d in EUN %d is %x\n", |
| 563 | block , writeEUN, le16_to_cpu(bci.Status)); |
| 564 | |
Thomas Gleixner | f4a43cf | 2006-05-28 11:01:53 +0200 | [diff] [blame] | 565 | status = bci.Status | bci.Status1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 566 | switch(status) { |
| 567 | case SECTOR_FREE: |
| 568 | return writeEUN; |
| 569 | |
| 570 | case SECTOR_DELETED: |
| 571 | case SECTOR_USED: |
| 572 | case SECTOR_IGNORE: |
| 573 | break; |
| 574 | default: |
| 575 | // Invalid block. Don't use it any more. Must implement. |
Thomas Gleixner | 97894cd | 2005-11-07 11:15:26 +0000 | [diff] [blame] | 576 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 577 | } |
Thomas Gleixner | 97894cd | 2005-11-07 11:15:26 +0000 | [diff] [blame] | 578 | |
| 579 | if (!silly--) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 580 | printk(KERN_WARNING |
| 581 | "Infinite loop in Virtual Unit Chain 0x%x\n", |
| 582 | thisVUC); |
Julia Lawall | 70ec3bb | 2009-06-27 09:55:32 +0200 | [diff] [blame] | 583 | return BLOCK_NIL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 584 | } |
| 585 | |
| 586 | /* Skip to next block in chain */ |
| 587 | writeEUN = nftl->ReplUnitTable[writeEUN]; |
| 588 | } |
| 589 | |
Thomas Gleixner | 97894cd | 2005-11-07 11:15:26 +0000 | [diff] [blame] | 590 | /* OK. We didn't find one in the existing chain, or there |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 591 | is no existing chain. */ |
| 592 | |
| 593 | /* Try to find an already-free block */ |
| 594 | writeEUN = NFTL_findfreeblock(nftl, 0); |
| 595 | |
| 596 | if (writeEUN == BLOCK_NIL) { |
| 597 | /* That didn't work - there were no free blocks just |
| 598 | waiting to be picked up. We're going to have to fold |
| 599 | a chain to make room. |
| 600 | */ |
| 601 | |
| 602 | /* First remember the start of this chain */ |
| 603 | //u16 startEUN = nftl->EUNtable[thisVUC]; |
Thomas Gleixner | 97894cd | 2005-11-07 11:15:26 +0000 | [diff] [blame] | 604 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 605 | //printk("Write to VirtualUnitChain %d, calling makefreeblock()\n", thisVUC); |
Julia Lawall | 70ec3bb | 2009-06-27 09:55:32 +0200 | [diff] [blame] | 606 | writeEUN = NFTL_makefreeblock(nftl, BLOCK_NIL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 607 | |
| 608 | if (writeEUN == BLOCK_NIL) { |
Thomas Gleixner | 97894cd | 2005-11-07 11:15:26 +0000 | [diff] [blame] | 609 | /* OK, we accept that the above comment is |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 610 | lying - there may have been free blocks |
| 611 | last time we called NFTL_findfreeblock(), |
| 612 | but they are reserved for when we're |
| 613 | desperate. Well, now we're desperate. |
| 614 | */ |
| 615 | DEBUG(MTD_DEBUG_LEVEL1, "Using desperate==1 to find free EUN to accommodate write to VUC %d\n", thisVUC); |
| 616 | writeEUN = NFTL_findfreeblock(nftl, 1); |
| 617 | } |
| 618 | if (writeEUN == BLOCK_NIL) { |
| 619 | /* Ouch. This should never happen - we should |
Thomas Gleixner | 97894cd | 2005-11-07 11:15:26 +0000 | [diff] [blame] | 620 | always be able to make some room somehow. |
| 621 | If we get here, we've allocated more storage |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 622 | space than actual media, or our makefreeblock |
| 623 | routine is missing something. |
| 624 | */ |
| 625 | printk(KERN_WARNING "Cannot make free space.\n"); |
| 626 | return BLOCK_NIL; |
Thomas Gleixner | 97894cd | 2005-11-07 11:15:26 +0000 | [diff] [blame] | 627 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 628 | //printk("Restarting scan\n"); |
| 629 | lastEUN = BLOCK_NIL; |
| 630 | continue; |
| 631 | } |
| 632 | |
| 633 | /* We've found a free block. Insert it into the chain. */ |
Thomas Gleixner | 97894cd | 2005-11-07 11:15:26 +0000 | [diff] [blame] | 634 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 635 | if (lastEUN != BLOCK_NIL) { |
Thomas Gleixner | f4a43cf | 2006-05-28 11:01:53 +0200 | [diff] [blame] | 636 | thisVUC |= 0x8000; /* It's a replacement block */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 637 | } else { |
Thomas Gleixner | f4a43cf | 2006-05-28 11:01:53 +0200 | [diff] [blame] | 638 | /* The first block in a new chain */ |
| 639 | nftl->EUNtable[thisVUC] = writeEUN; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 640 | } |
| 641 | |
| 642 | /* set up the actual EUN we're writing into */ |
| 643 | /* Both in our cache... */ |
| 644 | nftl->ReplUnitTable[writeEUN] = BLOCK_NIL; |
| 645 | |
| 646 | /* ... and on the flash itself */ |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 647 | nftl_read_oob(mtd, writeEUN * nftl->EraseSize + 8, 8, |
Thomas Gleixner | f4a43cf | 2006-05-28 11:01:53 +0200 | [diff] [blame] | 648 | &retlen, (char *)&oob.u); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 649 | |
| 650 | oob.u.a.VirtUnitNum = oob.u.a.SpareVirtUnitNum = cpu_to_le16(thisVUC); |
| 651 | |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 652 | nftl_write_oob(mtd, writeEUN * nftl->EraseSize + 8, 8, |
Thomas Gleixner | f4a43cf | 2006-05-28 11:01:53 +0200 | [diff] [blame] | 653 | &retlen, (char *)&oob.u); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 654 | |
Thomas Gleixner | f4a43cf | 2006-05-28 11:01:53 +0200 | [diff] [blame] | 655 | /* we link the new block to the chain only after the |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 656 | block is ready. It avoids the case where the chain |
| 657 | could point to a free block */ |
Thomas Gleixner | f4a43cf | 2006-05-28 11:01:53 +0200 | [diff] [blame] | 658 | if (lastEUN != BLOCK_NIL) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 659 | /* Both in our cache... */ |
| 660 | nftl->ReplUnitTable[lastEUN] = writeEUN; |
| 661 | /* ... and on the flash itself */ |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 662 | nftl_read_oob(mtd, (lastEUN * nftl->EraseSize) + 8, |
Thomas Gleixner | f4a43cf | 2006-05-28 11:01:53 +0200 | [diff] [blame] | 663 | 8, &retlen, (char *)&oob.u); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 664 | |
| 665 | oob.u.a.ReplUnitNum = oob.u.a.SpareReplUnitNum |
| 666 | = cpu_to_le16(writeEUN); |
| 667 | |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 668 | nftl_write_oob(mtd, (lastEUN * nftl->EraseSize) + 8, |
Thomas Gleixner | f4a43cf | 2006-05-28 11:01:53 +0200 | [diff] [blame] | 669 | 8, &retlen, (char *)&oob.u); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 670 | } |
| 671 | |
| 672 | return writeEUN; |
| 673 | |
| 674 | } while (silly2--); |
| 675 | |
| 676 | printk(KERN_WARNING "Error folding to make room for Virtual Unit Chain 0x%x\n", |
| 677 | thisVUC); |
Julia Lawall | 70ec3bb | 2009-06-27 09:55:32 +0200 | [diff] [blame] | 678 | return BLOCK_NIL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 679 | } |
| 680 | |
| 681 | static int nftl_writeblock(struct mtd_blktrans_dev *mbd, unsigned long block, |
| 682 | char *buffer) |
| 683 | { |
| 684 | struct NFTLrecord *nftl = (void *)mbd; |
| 685 | u16 writeEUN; |
| 686 | unsigned long blockofs = (block * 512) & (nftl->EraseSize - 1); |
| 687 | size_t retlen; |
| 688 | struct nftl_oob oob; |
| 689 | |
| 690 | writeEUN = NFTL_findwriteunit(nftl, block); |
| 691 | |
| 692 | if (writeEUN == BLOCK_NIL) { |
| 693 | printk(KERN_WARNING |
| 694 | "NFTL_writeblock(): Cannot find block to write to\n"); |
| 695 | /* If we _still_ haven't got a block to use, we're screwed */ |
| 696 | return 1; |
| 697 | } |
| 698 | |
| 699 | memset(&oob, 0xff, sizeof(struct nftl_oob)); |
| 700 | oob.b.Status = oob.b.Status1 = SECTOR_USED; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 701 | |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 702 | nftl_write(nftl->mbd.mtd, (writeEUN * nftl->EraseSize) + blockofs, |
| 703 | 512, &retlen, (char *)buffer, (char *)&oob); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 704 | return 0; |
| 705 | } |
| 706 | #endif /* CONFIG_NFTL_RW */ |
| 707 | |
| 708 | static int nftl_readblock(struct mtd_blktrans_dev *mbd, unsigned long block, |
| 709 | char *buffer) |
| 710 | { |
| 711 | struct NFTLrecord *nftl = (void *)mbd; |
Thomas Gleixner | f4a43cf | 2006-05-28 11:01:53 +0200 | [diff] [blame] | 712 | struct mtd_info *mtd = nftl->mbd.mtd; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 713 | u16 lastgoodEUN; |
| 714 | u16 thisEUN = nftl->EUNtable[block / (nftl->EraseSize / 512)]; |
| 715 | unsigned long blockofs = (block * 512) & (nftl->EraseSize - 1); |
Thomas Gleixner | f4a43cf | 2006-05-28 11:01:53 +0200 | [diff] [blame] | 716 | unsigned int status; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 717 | int silly = MAX_LOOPS; |
Thomas Gleixner | f4a43cf | 2006-05-28 11:01:53 +0200 | [diff] [blame] | 718 | size_t retlen; |
| 719 | struct nftl_bci bci; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 720 | |
| 721 | lastgoodEUN = BLOCK_NIL; |
| 722 | |
Thomas Gleixner | f4a43cf | 2006-05-28 11:01:53 +0200 | [diff] [blame] | 723 | if (thisEUN != BLOCK_NIL) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 724 | while (thisEUN < nftl->nb_blocks) { |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 725 | if (nftl_read_oob(mtd, (thisEUN * nftl->EraseSize) + |
Thomas Gleixner | f4a43cf | 2006-05-28 11:01:53 +0200 | [diff] [blame] | 726 | blockofs, 8, &retlen, |
| 727 | (char *)&bci) < 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 728 | status = SECTOR_IGNORE; |
| 729 | else |
| 730 | status = bci.Status | bci.Status1; |
| 731 | |
| 732 | switch (status) { |
| 733 | case SECTOR_FREE: |
| 734 | /* no modification of a sector should follow a free sector */ |
| 735 | goto the_end; |
| 736 | case SECTOR_DELETED: |
| 737 | lastgoodEUN = BLOCK_NIL; |
| 738 | break; |
| 739 | case SECTOR_USED: |
| 740 | lastgoodEUN = thisEUN; |
| 741 | break; |
| 742 | case SECTOR_IGNORE: |
| 743 | break; |
| 744 | default: |
| 745 | printk("Unknown status for block %ld in EUN %d: %x\n", |
| 746 | block, thisEUN, status); |
| 747 | break; |
| 748 | } |
| 749 | |
| 750 | if (!silly--) { |
| 751 | printk(KERN_WARNING "Infinite loop in Virtual Unit Chain 0x%lx\n", |
| 752 | block / (nftl->EraseSize / 512)); |
| 753 | return 1; |
| 754 | } |
| 755 | thisEUN = nftl->ReplUnitTable[thisEUN]; |
| 756 | } |
Thomas Gleixner | f4a43cf | 2006-05-28 11:01:53 +0200 | [diff] [blame] | 757 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 758 | |
| 759 | the_end: |
| 760 | if (lastgoodEUN == BLOCK_NIL) { |
| 761 | /* the requested block is not on the media, return all 0x00 */ |
| 762 | memset(buffer, 0, 512); |
| 763 | } else { |
| 764 | loff_t ptr = (lastgoodEUN * nftl->EraseSize) + blockofs; |
| 765 | size_t retlen; |
Thomas Gleixner | 9a1fcdf | 2006-05-29 14:56:39 +0200 | [diff] [blame] | 766 | int res = mtd->read(mtd, ptr, 512, &retlen, buffer); |
| 767 | |
| 768 | if (res < 0 && res != -EUCLEAN) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 769 | return -EIO; |
| 770 | } |
| 771 | return 0; |
| 772 | } |
| 773 | |
| 774 | static int nftl_getgeo(struct mtd_blktrans_dev *dev, struct hd_geometry *geo) |
| 775 | { |
| 776 | struct NFTLrecord *nftl = (void *)dev; |
| 777 | |
| 778 | geo->heads = nftl->heads; |
| 779 | geo->sectors = nftl->sectors; |
| 780 | geo->cylinders = nftl->cylinders; |
| 781 | |
| 782 | return 0; |
| 783 | } |
| 784 | |
| 785 | /**************************************************************************** |
| 786 | * |
| 787 | * Module stuff |
| 788 | * |
| 789 | ****************************************************************************/ |
| 790 | |
| 791 | |
| 792 | static struct mtd_blktrans_ops nftl_tr = { |
| 793 | .name = "nftl", |
| 794 | .major = NFTL_MAJOR, |
| 795 | .part_bits = NFTL_PARTN_BITS, |
Richard Purdie | 1918767 | 2006-10-27 09:09:33 +0100 | [diff] [blame] | 796 | .blksize = 512, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 797 | .getgeo = nftl_getgeo, |
| 798 | .readsect = nftl_readblock, |
| 799 | #ifdef CONFIG_NFTL_RW |
| 800 | .writesect = nftl_writeblock, |
| 801 | #endif |
| 802 | .add_mtd = nftl_add_mtd, |
| 803 | .remove_dev = nftl_remove_dev, |
| 804 | .owner = THIS_MODULE, |
| 805 | }; |
| 806 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 807 | static int __init init_nftl(void) |
| 808 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 809 | return register_mtd_blktrans(&nftl_tr); |
| 810 | } |
| 811 | |
| 812 | static void __exit cleanup_nftl(void) |
| 813 | { |
| 814 | deregister_mtd_blktrans(&nftl_tr); |
| 815 | } |
| 816 | |
| 817 | module_init(init_nftl); |
| 818 | module_exit(cleanup_nftl); |
| 819 | |
| 820 | MODULE_LICENSE("GPL"); |
| 821 | MODULE_AUTHOR("David Woodhouse <dwmw2@infradead.org>, Fabrice Bellard <fabrice.bellard@netgem.com> et al."); |
| 822 | MODULE_DESCRIPTION("Support code for NAND Flash Translation Layer, used on M-Systems DiskOnChip 2000 and Millennium"); |
Scott James Remnant | e7f5216 | 2009-03-02 17:43:04 +0000 | [diff] [blame] | 823 | MODULE_ALIAS_BLOCKDEV_MAJOR(NFTL_MAJOR); |