Thomas Gleixner | 97894cd | 2005-11-07 11:15:26 +0000 | [diff] [blame] | 1 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2 | * inftlcore.c -- Linux driver for Inverse Flash Translation Layer (INFTL) |
| 3 | * |
| 4 | * (C) Copyright 2002, Greg Ungerer (gerg@snapgear.com) |
| 5 | * |
| 6 | * Based heavily on the nftlcore.c code which is: |
| 7 | * (c) 1999 Machine Vision Holdings, Inc. |
| 8 | * Author: David Woodhouse <dwmw2@infradead.org> |
| 9 | * |
Thomas Gleixner | 97894cd | 2005-11-07 11:15:26 +0000 | [diff] [blame] | 10 | * $Id: inftlcore.c,v 1.19 2005/11/07 11:14:20 gleixner Exp $ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 11 | * |
| 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. |
| 16 | * |
| 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. |
| 21 | * |
| 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 |
| 25 | */ |
| 26 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 27 | #include <linux/kernel.h> |
| 28 | #include <linux/module.h> |
| 29 | #include <linux/delay.h> |
| 30 | #include <linux/slab.h> |
| 31 | #include <linux/sched.h> |
| 32 | #include <linux/init.h> |
| 33 | #include <linux/kmod.h> |
| 34 | #include <linux/hdreg.h> |
| 35 | #include <linux/mtd/mtd.h> |
| 36 | #include <linux/mtd/nftl.h> |
| 37 | #include <linux/mtd/inftl.h> |
Thomas Gleixner | 9223a45 | 2006-05-23 17:21:03 +0200 | [diff] [blame] | 38 | #include <linux/mtd/nand.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 39 | #include <asm/uaccess.h> |
| 40 | #include <asm/errno.h> |
| 41 | #include <asm/io.h> |
| 42 | |
| 43 | /* |
| 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. |
| 47 | */ |
| 48 | #define MAX_LOOPS 10000 |
| 49 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 50 | static void inftl_add_mtd(struct mtd_blktrans_ops *tr, struct mtd_info *mtd) |
| 51 | { |
| 52 | struct INFTLrecord *inftl; |
| 53 | unsigned long temp; |
| 54 | |
| 55 | if (mtd->type != MTD_NANDFLASH) |
| 56 | return; |
| 57 | /* OK, this is moderately ugly. But probably safe. Alternatives? */ |
| 58 | if (memcmp(mtd->name, "DiskOnChip", 10)) |
| 59 | return; |
| 60 | |
| 61 | if (!mtd->block_isbad) { |
| 62 | printk(KERN_ERR |
| 63 | "INFTL no longer supports the old DiskOnChip drivers loaded via docprobe.\n" |
| 64 | "Please use the new diskonchip driver under the NAND subsystem.\n"); |
| 65 | return; |
| 66 | } |
| 67 | |
| 68 | DEBUG(MTD_DEBUG_LEVEL3, "INFTL: add_mtd for %s\n", mtd->name); |
| 69 | |
| 70 | inftl = kmalloc(sizeof(*inftl), GFP_KERNEL); |
| 71 | |
| 72 | if (!inftl) { |
| 73 | printk(KERN_WARNING "INFTL: Out of memory for data structures\n"); |
| 74 | return; |
| 75 | } |
| 76 | memset(inftl, 0, sizeof(*inftl)); |
| 77 | |
| 78 | inftl->mbd.mtd = mtd; |
| 79 | inftl->mbd.devnum = -1; |
Richard Purdie | 1918767 | 2006-10-27 09:09:33 +0100 | [diff] [blame] | 80 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 81 | inftl->mbd.tr = tr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 82 | |
Thomas Gleixner | 9223a45 | 2006-05-23 17:21:03 +0200 | [diff] [blame] | 83 | if (INFTL_mount(inftl) < 0) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 84 | printk(KERN_WARNING "INFTL: could not mount device\n"); |
| 85 | kfree(inftl); |
| 86 | return; |
Thomas Gleixner | 9223a45 | 2006-05-23 17:21:03 +0200 | [diff] [blame] | 87 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 88 | |
| 89 | /* OK, it's a new one. Set up all the data structures. */ |
| 90 | |
| 91 | /* Calculate geometry */ |
| 92 | inftl->cylinders = 1024; |
| 93 | inftl->heads = 16; |
| 94 | |
| 95 | temp = inftl->cylinders * inftl->heads; |
| 96 | inftl->sectors = inftl->mbd.size / temp; |
| 97 | if (inftl->mbd.size % temp) { |
| 98 | inftl->sectors++; |
| 99 | temp = inftl->cylinders * inftl->sectors; |
| 100 | inftl->heads = inftl->mbd.size / temp; |
| 101 | |
| 102 | if (inftl->mbd.size % temp) { |
| 103 | inftl->heads++; |
| 104 | temp = inftl->heads * inftl->sectors; |
| 105 | inftl->cylinders = inftl->mbd.size / temp; |
| 106 | } |
| 107 | } |
| 108 | |
| 109 | if (inftl->mbd.size != inftl->heads * inftl->cylinders * inftl->sectors) { |
| 110 | /* |
Thomas Gleixner | 97894cd | 2005-11-07 11:15:26 +0000 | [diff] [blame] | 111 | Oh no we don't have |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 112 | mbd.size == heads * cylinders * sectors |
| 113 | */ |
| 114 | printk(KERN_WARNING "INFTL: cannot calculate a geometry to " |
| 115 | "match size of 0x%lx.\n", inftl->mbd.size); |
| 116 | printk(KERN_WARNING "INFTL: using C:%d H:%d S:%d " |
| 117 | "(== 0x%lx sects)\n", |
Thomas Gleixner | 97894cd | 2005-11-07 11:15:26 +0000 | [diff] [blame] | 118 | inftl->cylinders, inftl->heads , inftl->sectors, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 119 | (long)inftl->cylinders * (long)inftl->heads * |
| 120 | (long)inftl->sectors ); |
| 121 | } |
| 122 | |
| 123 | if (add_mtd_blktrans_dev(&inftl->mbd)) { |
Jesper Juhl | fa67164 | 2005-11-07 01:01:27 -0800 | [diff] [blame] | 124 | kfree(inftl->PUtable); |
| 125 | kfree(inftl->VUtable); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 126 | kfree(inftl); |
| 127 | return; |
| 128 | } |
| 129 | #ifdef PSYCHO_DEBUG |
Eric Sesterhenn / snakebyte | 8b68a12 | 2006-03-31 02:29:47 -0800 | [diff] [blame] | 130 | printk(KERN_INFO "INFTL: Found new inftl%c\n", inftl->mbd.devnum + 'a'); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 131 | #endif |
| 132 | return; |
| 133 | } |
| 134 | |
| 135 | static void inftl_remove_dev(struct mtd_blktrans_dev *dev) |
| 136 | { |
| 137 | struct INFTLrecord *inftl = (void *)dev; |
| 138 | |
| 139 | DEBUG(MTD_DEBUG_LEVEL3, "INFTL: remove_dev (i=%d)\n", dev->devnum); |
| 140 | |
| 141 | del_mtd_blktrans_dev(dev); |
| 142 | |
Jesper Juhl | fa67164 | 2005-11-07 01:01:27 -0800 | [diff] [blame] | 143 | kfree(inftl->PUtable); |
| 144 | kfree(inftl->VUtable); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 145 | kfree(inftl); |
| 146 | } |
| 147 | |
| 148 | /* |
| 149 | * Actual INFTL access routines. |
| 150 | */ |
| 151 | |
| 152 | /* |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 153 | * Read oob data from flash |
| 154 | */ |
| 155 | int inftl_read_oob(struct mtd_info *mtd, loff_t offs, size_t len, |
| 156 | size_t *retlen, uint8_t *buf) |
| 157 | { |
| 158 | struct mtd_oob_ops ops; |
| 159 | int res; |
| 160 | |
| 161 | ops.mode = MTD_OOB_PLACE; |
| 162 | ops.ooboffs = offs & (mtd->writesize - 1); |
| 163 | ops.ooblen = len; |
| 164 | ops.oobbuf = buf; |
| 165 | ops.datbuf = NULL; |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 166 | |
| 167 | res = mtd->read_oob(mtd, offs & ~(mtd->writesize - 1), &ops); |
Vitaly Wool | 7014568 | 2006-11-03 18:20:38 +0300 | [diff] [blame^] | 168 | *retlen = ops.oobretlen; |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 169 | return res; |
| 170 | } |
| 171 | |
| 172 | /* |
| 173 | * Write oob data to flash |
| 174 | */ |
| 175 | int inftl_write_oob(struct mtd_info *mtd, loff_t offs, size_t len, |
| 176 | size_t *retlen, uint8_t *buf) |
| 177 | { |
| 178 | struct mtd_oob_ops ops; |
| 179 | int res; |
| 180 | |
| 181 | ops.mode = MTD_OOB_PLACE; |
| 182 | ops.ooboffs = offs & (mtd->writesize - 1); |
| 183 | ops.ooblen = len; |
| 184 | ops.oobbuf = buf; |
| 185 | ops.datbuf = NULL; |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 186 | |
| 187 | res = mtd->write_oob(mtd, offs & ~(mtd->writesize - 1), &ops); |
Vitaly Wool | 7014568 | 2006-11-03 18:20:38 +0300 | [diff] [blame^] | 188 | *retlen = ops.oobretlen; |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 189 | return res; |
| 190 | } |
| 191 | |
| 192 | /* |
| 193 | * Write data and oob to flash |
| 194 | */ |
| 195 | static int inftl_write(struct mtd_info *mtd, loff_t offs, size_t len, |
| 196 | size_t *retlen, uint8_t *buf, uint8_t *oob) |
| 197 | { |
| 198 | struct mtd_oob_ops ops; |
| 199 | int res; |
| 200 | |
| 201 | ops.mode = MTD_OOB_PLACE; |
| 202 | ops.ooboffs = offs; |
| 203 | ops.ooblen = mtd->oobsize; |
| 204 | ops.oobbuf = oob; |
| 205 | ops.datbuf = buf; |
| 206 | ops.len = len; |
| 207 | |
| 208 | res = mtd->write_oob(mtd, offs & ~(mtd->writesize - 1), &ops); |
| 209 | *retlen = ops.retlen; |
| 210 | return res; |
| 211 | } |
| 212 | |
| 213 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 214 | * INFTL_findfreeblock: Find a free Erase Unit on the INFTL partition. |
| 215 | * This function is used when the give Virtual Unit Chain. |
| 216 | */ |
| 217 | static u16 INFTL_findfreeblock(struct INFTLrecord *inftl, int desperate) |
| 218 | { |
| 219 | u16 pot = inftl->LastFreeEUN; |
| 220 | int silly = inftl->nb_blocks; |
| 221 | |
| 222 | DEBUG(MTD_DEBUG_LEVEL3, "INFTL: INFTL_findfreeblock(inftl=%p," |
| 223 | "desperate=%d)\n", inftl, desperate); |
| 224 | |
| 225 | /* |
| 226 | * Normally, we force a fold to happen before we run out of free |
| 227 | * blocks completely. |
| 228 | */ |
| 229 | if (!desperate && inftl->numfreeEUNs < 2) { |
| 230 | DEBUG(MTD_DEBUG_LEVEL1, "INFTL: there are too few free " |
| 231 | "EUNs (%d)\n", inftl->numfreeEUNs); |
| 232 | return 0xffff; |
| 233 | } |
| 234 | |
| 235 | /* Scan for a free block */ |
| 236 | do { |
| 237 | if (inftl->PUtable[pot] == BLOCK_FREE) { |
| 238 | inftl->LastFreeEUN = pot; |
| 239 | return pot; |
| 240 | } |
| 241 | |
| 242 | if (++pot > inftl->lastEUN) |
| 243 | pot = 0; |
| 244 | |
| 245 | if (!silly--) { |
| 246 | printk(KERN_WARNING "INFTL: no free blocks found! " |
| 247 | "EUN range = %d - %d\n", 0, inftl->LastFreeEUN); |
| 248 | return BLOCK_NIL; |
| 249 | } |
| 250 | } while (pot != inftl->LastFreeEUN); |
| 251 | |
| 252 | return BLOCK_NIL; |
| 253 | } |
| 254 | |
| 255 | static u16 INFTL_foldchain(struct INFTLrecord *inftl, unsigned thisVUC, unsigned pendingblock) |
| 256 | { |
| 257 | u16 BlockMap[MAX_SECTORS_PER_UNIT]; |
| 258 | unsigned char BlockDeleted[MAX_SECTORS_PER_UNIT]; |
| 259 | unsigned int thisEUN, prevEUN, status; |
Thomas Gleixner | f4a43cf | 2006-05-28 11:01:53 +0200 | [diff] [blame] | 260 | struct mtd_info *mtd = inftl->mbd.mtd; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 261 | int block, silly; |
| 262 | unsigned int targetEUN; |
| 263 | struct inftl_oob oob; |
Thomas Gleixner | f4a43cf | 2006-05-28 11:01:53 +0200 | [diff] [blame] | 264 | size_t retlen; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 265 | |
| 266 | DEBUG(MTD_DEBUG_LEVEL3, "INFTL: INFTL_foldchain(inftl=%p,thisVUC=%d," |
| 267 | "pending=%d)\n", inftl, thisVUC, pendingblock); |
| 268 | |
| 269 | memset(BlockMap, 0xff, sizeof(BlockMap)); |
| 270 | memset(BlockDeleted, 0, sizeof(BlockDeleted)); |
| 271 | |
| 272 | thisEUN = targetEUN = inftl->VUtable[thisVUC]; |
| 273 | |
| 274 | if (thisEUN == BLOCK_NIL) { |
| 275 | printk(KERN_WARNING "INFTL: trying to fold non-existent " |
| 276 | "Virtual Unit Chain %d!\n", thisVUC); |
| 277 | return BLOCK_NIL; |
| 278 | } |
Thomas Gleixner | 97894cd | 2005-11-07 11:15:26 +0000 | [diff] [blame] | 279 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 280 | /* |
| 281 | * Scan to find the Erase Unit which holds the actual data for each |
| 282 | * 512-byte block within the Chain. |
| 283 | */ |
Thomas Gleixner | 9223a45 | 2006-05-23 17:21:03 +0200 | [diff] [blame] | 284 | silly = MAX_LOOPS; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 285 | while (thisEUN < inftl->nb_blocks) { |
| 286 | for (block = 0; block < inftl->EraseSize/SECTORSIZE; block ++) { |
| 287 | if ((BlockMap[block] != 0xffff) || BlockDeleted[block]) |
| 288 | continue; |
| 289 | |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 290 | if (inftl_read_oob(mtd, (thisEUN * inftl->EraseSize) |
| 291 | + (block * SECTORSIZE), 16, &retlen, |
| 292 | (char *)&oob) < 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 293 | status = SECTOR_IGNORE; |
| 294 | else |
Thomas Gleixner | 9223a45 | 2006-05-23 17:21:03 +0200 | [diff] [blame] | 295 | status = oob.b.Status | oob.b.Status1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 296 | |
| 297 | switch(status) { |
| 298 | case SECTOR_FREE: |
| 299 | case SECTOR_IGNORE: |
| 300 | break; |
| 301 | case SECTOR_USED: |
| 302 | BlockMap[block] = thisEUN; |
| 303 | continue; |
| 304 | case SECTOR_DELETED: |
| 305 | BlockDeleted[block] = 1; |
| 306 | continue; |
| 307 | default: |
| 308 | printk(KERN_WARNING "INFTL: unknown status " |
| 309 | "for block %d in EUN %d: %x\n", |
| 310 | block, thisEUN, status); |
| 311 | break; |
| 312 | } |
| 313 | } |
| 314 | |
| 315 | if (!silly--) { |
| 316 | printk(KERN_WARNING "INFTL: infinite loop in Virtual " |
| 317 | "Unit Chain 0x%x\n", thisVUC); |
| 318 | return BLOCK_NIL; |
| 319 | } |
Thomas Gleixner | 97894cd | 2005-11-07 11:15:26 +0000 | [diff] [blame] | 320 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 321 | thisEUN = inftl->PUtable[thisEUN]; |
| 322 | } |
| 323 | |
| 324 | /* |
| 325 | * OK. We now know the location of every block in the Virtual Unit |
| 326 | * Chain, and the Erase Unit into which we are supposed to be copying. |
| 327 | * Go for it. |
| 328 | */ |
| 329 | DEBUG(MTD_DEBUG_LEVEL1, "INFTL: folding chain %d into unit %d\n", |
| 330 | thisVUC, targetEUN); |
| 331 | |
| 332 | for (block = 0; block < inftl->EraseSize/SECTORSIZE ; block++) { |
| 333 | unsigned char movebuf[SECTORSIZE]; |
| 334 | int ret; |
| 335 | |
| 336 | /* |
| 337 | * If it's in the target EUN already, or if it's pending write, |
| 338 | * do nothing. |
| 339 | */ |
| 340 | if (BlockMap[block] == targetEUN || (pendingblock == |
| 341 | (thisVUC * (inftl->EraseSize / SECTORSIZE) + block))) { |
| 342 | continue; |
| 343 | } |
| 344 | |
Thomas Gleixner | 9223a45 | 2006-05-23 17:21:03 +0200 | [diff] [blame] | 345 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 346 | * Copy only in non free block (free blocks can only |
| 347 | * happen in case of media errors or deleted blocks). |
| 348 | */ |
Thomas Gleixner | 9223a45 | 2006-05-23 17:21:03 +0200 | [diff] [blame] | 349 | if (BlockMap[block] == BLOCK_NIL) |
| 350 | continue; |
Thomas Gleixner | 97894cd | 2005-11-07 11:15:26 +0000 | [diff] [blame] | 351 | |
Thomas Gleixner | f4a43cf | 2006-05-28 11:01:53 +0200 | [diff] [blame] | 352 | ret = mtd->read(mtd, (inftl->EraseSize * BlockMap[block]) + |
| 353 | (block * SECTORSIZE), SECTORSIZE, &retlen, |
| 354 | movebuf); |
Thomas Gleixner | 9a1fcdf | 2006-05-29 14:56:39 +0200 | [diff] [blame] | 355 | if (ret < 0 && ret != -EUCLEAN) { |
Thomas Gleixner | f4a43cf | 2006-05-28 11:01:53 +0200 | [diff] [blame] | 356 | ret = mtd->read(mtd, |
| 357 | (inftl->EraseSize * BlockMap[block]) + |
| 358 | (block * SECTORSIZE), SECTORSIZE, |
| 359 | &retlen, movebuf); |
Thomas Gleixner | 97894cd | 2005-11-07 11:15:26 +0000 | [diff] [blame] | 360 | if (ret != -EIO) |
Thomas Gleixner | 9223a45 | 2006-05-23 17:21:03 +0200 | [diff] [blame] | 361 | DEBUG(MTD_DEBUG_LEVEL1, "INFTL: error went " |
| 362 | "away on retry?\n"); |
| 363 | } |
| 364 | memset(&oob, 0xff, sizeof(struct inftl_oob)); |
| 365 | oob.b.Status = oob.b.Status1 = SECTOR_USED; |
| 366 | |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 367 | inftl_write(inftl->mbd.mtd, (inftl->EraseSize * targetEUN) + |
| 368 | (block * SECTORSIZE), SECTORSIZE, &retlen, |
| 369 | movebuf, (char *)&oob); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 370 | } |
| 371 | |
| 372 | /* |
| 373 | * Newest unit in chain now contains data from _all_ older units. |
| 374 | * So go through and erase each unit in chain, oldest first. (This |
| 375 | * is important, by doing oldest first if we crash/reboot then it |
| 376 | * it is relatively simple to clean up the mess). |
| 377 | */ |
| 378 | DEBUG(MTD_DEBUG_LEVEL1, "INFTL: want to erase virtual chain %d\n", |
| 379 | thisVUC); |
| 380 | |
| 381 | for (;;) { |
| 382 | /* Find oldest unit in chain. */ |
| 383 | thisEUN = inftl->VUtable[thisVUC]; |
| 384 | prevEUN = BLOCK_NIL; |
| 385 | while (inftl->PUtable[thisEUN] != BLOCK_NIL) { |
| 386 | prevEUN = thisEUN; |
| 387 | thisEUN = inftl->PUtable[thisEUN]; |
| 388 | } |
| 389 | |
| 390 | /* Check if we are all done */ |
| 391 | if (thisEUN == targetEUN) |
| 392 | break; |
| 393 | |
Thomas Gleixner | 9223a45 | 2006-05-23 17:21:03 +0200 | [diff] [blame] | 394 | if (INFTL_formatblock(inftl, thisEUN) < 0) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 395 | /* |
| 396 | * Could not erase : mark block as reserved. |
| 397 | */ |
| 398 | inftl->PUtable[thisEUN] = BLOCK_RESERVED; |
Thomas Gleixner | 9223a45 | 2006-05-23 17:21:03 +0200 | [diff] [blame] | 399 | } else { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 400 | /* Correctly erased : mark it as free */ |
| 401 | inftl->PUtable[thisEUN] = BLOCK_FREE; |
| 402 | inftl->PUtable[prevEUN] = BLOCK_NIL; |
| 403 | inftl->numfreeEUNs++; |
Thomas Gleixner | 9223a45 | 2006-05-23 17:21:03 +0200 | [diff] [blame] | 404 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 405 | } |
| 406 | |
| 407 | return targetEUN; |
| 408 | } |
| 409 | |
| 410 | static u16 INFTL_makefreeblock(struct INFTLrecord *inftl, unsigned pendingblock) |
| 411 | { |
| 412 | /* |
Thomas Gleixner | 97894cd | 2005-11-07 11:15:26 +0000 | [diff] [blame] | 413 | * This is the part that needs some cleverness applied. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 414 | * For now, I'm doing the minimum applicable to actually |
| 415 | * get the thing to work. |
| 416 | * Wear-levelling and other clever stuff needs to be implemented |
| 417 | * and we also need to do some assessment of the results when |
| 418 | * the system loses power half-way through the routine. |
| 419 | */ |
| 420 | u16 LongestChain = 0; |
| 421 | u16 ChainLength = 0, thislen; |
| 422 | u16 chain, EUN; |
| 423 | |
| 424 | DEBUG(MTD_DEBUG_LEVEL3, "INFTL: INFTL_makefreeblock(inftl=%p," |
| 425 | "pending=%d)\n", inftl, pendingblock); |
| 426 | |
| 427 | for (chain = 0; chain < inftl->nb_blocks; chain++) { |
| 428 | EUN = inftl->VUtable[chain]; |
| 429 | thislen = 0; |
| 430 | |
| 431 | while (EUN <= inftl->lastEUN) { |
| 432 | thislen++; |
| 433 | EUN = inftl->PUtable[EUN]; |
| 434 | if (thislen > 0xff00) { |
| 435 | printk(KERN_WARNING "INFTL: endless loop in " |
| 436 | "Virtual Chain %d: Unit %x\n", |
| 437 | chain, EUN); |
| 438 | /* |
| 439 | * Actually, don't return failure. |
| 440 | * Just ignore this chain and get on with it. |
| 441 | */ |
| 442 | thislen = 0; |
| 443 | break; |
| 444 | } |
| 445 | } |
| 446 | |
| 447 | if (thislen > ChainLength) { |
| 448 | ChainLength = thislen; |
| 449 | LongestChain = chain; |
| 450 | } |
| 451 | } |
| 452 | |
| 453 | if (ChainLength < 2) { |
| 454 | printk(KERN_WARNING "INFTL: no Virtual Unit Chains available " |
| 455 | "for folding. Failing request\n"); |
| 456 | return BLOCK_NIL; |
| 457 | } |
| 458 | |
| 459 | return INFTL_foldchain(inftl, LongestChain, pendingblock); |
| 460 | } |
| 461 | |
| 462 | static int nrbits(unsigned int val, int bitcount) |
| 463 | { |
| 464 | int i, total = 0; |
| 465 | |
| 466 | for (i = 0; (i < bitcount); i++) |
| 467 | total += (((0x1 << i) & val) ? 1 : 0); |
| 468 | return total; |
| 469 | } |
| 470 | |
| 471 | /* |
Thomas Gleixner | 97894cd | 2005-11-07 11:15:26 +0000 | [diff] [blame] | 472 | * INFTL_findwriteunit: Return the unit number into which we can write |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 473 | * for this block. Make it available if it isn't already. |
| 474 | */ |
| 475 | static inline u16 INFTL_findwriteunit(struct INFTLrecord *inftl, unsigned block) |
| 476 | { |
| 477 | unsigned int thisVUC = block / (inftl->EraseSize / SECTORSIZE); |
| 478 | unsigned int thisEUN, writeEUN, prev_block, status; |
| 479 | unsigned long blockofs = (block * SECTORSIZE) & (inftl->EraseSize -1); |
Thomas Gleixner | f4a43cf | 2006-05-28 11:01:53 +0200 | [diff] [blame] | 480 | struct mtd_info *mtd = inftl->mbd.mtd; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 481 | struct inftl_oob oob; |
| 482 | struct inftl_bci bci; |
| 483 | unsigned char anac, nacs, parity; |
| 484 | size_t retlen; |
| 485 | int silly, silly2 = 3; |
| 486 | |
| 487 | DEBUG(MTD_DEBUG_LEVEL3, "INFTL: INFTL_findwriteunit(inftl=%p," |
| 488 | "block=%d)\n", inftl, block); |
| 489 | |
| 490 | do { |
| 491 | /* |
| 492 | * Scan the media to find a unit in the VUC which has |
| 493 | * a free space for the block in question. |
| 494 | */ |
| 495 | writeEUN = BLOCK_NIL; |
| 496 | thisEUN = inftl->VUtable[thisVUC]; |
| 497 | silly = MAX_LOOPS; |
| 498 | |
| 499 | while (thisEUN <= inftl->lastEUN) { |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 500 | inftl_read_oob(mtd, (thisEUN * inftl->EraseSize) + |
| 501 | blockofs, 8, &retlen, (char *)&bci); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 502 | |
Thomas Gleixner | 9223a45 | 2006-05-23 17:21:03 +0200 | [diff] [blame] | 503 | status = bci.Status | bci.Status1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 504 | DEBUG(MTD_DEBUG_LEVEL3, "INFTL: status of block %d in " |
| 505 | "EUN %d is %x\n", block , writeEUN, status); |
| 506 | |
| 507 | switch(status) { |
| 508 | case SECTOR_FREE: |
| 509 | writeEUN = thisEUN; |
| 510 | break; |
| 511 | case SECTOR_DELETED: |
| 512 | case SECTOR_USED: |
| 513 | /* Can't go any further */ |
| 514 | goto hitused; |
| 515 | case SECTOR_IGNORE: |
| 516 | break; |
| 517 | default: |
| 518 | /* |
| 519 | * Invalid block. Don't use it any more. |
| 520 | * Must implement. |
| 521 | */ |
Thomas Gleixner | 97894cd | 2005-11-07 11:15:26 +0000 | [diff] [blame] | 522 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 523 | } |
Thomas Gleixner | 97894cd | 2005-11-07 11:15:26 +0000 | [diff] [blame] | 524 | |
| 525 | if (!silly--) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 526 | printk(KERN_WARNING "INFTL: infinite loop in " |
| 527 | "Virtual Unit Chain 0x%x\n", thisVUC); |
| 528 | return 0xffff; |
| 529 | } |
| 530 | |
| 531 | /* Skip to next block in chain */ |
| 532 | thisEUN = inftl->PUtable[thisEUN]; |
| 533 | } |
| 534 | |
| 535 | hitused: |
| 536 | if (writeEUN != BLOCK_NIL) |
| 537 | return writeEUN; |
| 538 | |
| 539 | |
| 540 | /* |
Thomas Gleixner | 97894cd | 2005-11-07 11:15:26 +0000 | [diff] [blame] | 541 | * OK. We didn't find one in the existing chain, or there |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 542 | * is no existing chain. Allocate a new one. |
| 543 | */ |
| 544 | writeEUN = INFTL_findfreeblock(inftl, 0); |
| 545 | |
| 546 | if (writeEUN == BLOCK_NIL) { |
| 547 | /* |
| 548 | * That didn't work - there were no free blocks just |
| 549 | * waiting to be picked up. We're going to have to fold |
| 550 | * a chain to make room. |
| 551 | */ |
| 552 | thisEUN = INFTL_makefreeblock(inftl, 0xffff); |
| 553 | |
| 554 | /* |
| 555 | * Hopefully we free something, lets try again. |
| 556 | * This time we are desperate... |
| 557 | */ |
| 558 | DEBUG(MTD_DEBUG_LEVEL1, "INFTL: using desperate==1 " |
| 559 | "to find free EUN to accommodate write to " |
| 560 | "VUC %d\n", thisVUC); |
| 561 | writeEUN = INFTL_findfreeblock(inftl, 1); |
| 562 | if (writeEUN == BLOCK_NIL) { |
| 563 | /* |
| 564 | * Ouch. This should never happen - we should |
Thomas Gleixner | 97894cd | 2005-11-07 11:15:26 +0000 | [diff] [blame] | 565 | * always be able to make some room somehow. |
| 566 | * If we get here, we've allocated more storage |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 567 | * space than actual media, or our makefreeblock |
| 568 | * routine is missing something. |
| 569 | */ |
| 570 | printk(KERN_WARNING "INFTL: cannot make free " |
| 571 | "space.\n"); |
| 572 | #ifdef DEBUG |
| 573 | INFTL_dumptables(inftl); |
| 574 | INFTL_dumpVUchains(inftl); |
| 575 | #endif |
| 576 | return BLOCK_NIL; |
Thomas Gleixner | 97894cd | 2005-11-07 11:15:26 +0000 | [diff] [blame] | 577 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 578 | } |
| 579 | |
| 580 | /* |
| 581 | * Insert new block into virtual chain. Firstly update the |
| 582 | * block headers in flash... |
| 583 | */ |
| 584 | anac = 0; |
| 585 | nacs = 0; |
| 586 | thisEUN = inftl->VUtable[thisVUC]; |
| 587 | if (thisEUN != BLOCK_NIL) { |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 588 | inftl_read_oob(mtd, thisEUN * inftl->EraseSize |
| 589 | + 8, 8, &retlen, (char *)&oob.u); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 590 | anac = oob.u.a.ANAC + 1; |
| 591 | nacs = oob.u.a.NACs + 1; |
| 592 | } |
| 593 | |
| 594 | prev_block = inftl->VUtable[thisVUC]; |
| 595 | if (prev_block < inftl->nb_blocks) |
| 596 | prev_block -= inftl->firstEUN; |
| 597 | |
| 598 | parity = (nrbits(thisVUC, 16) & 0x1) ? 0x1 : 0; |
| 599 | parity |= (nrbits(prev_block, 16) & 0x1) ? 0x2 : 0; |
| 600 | parity |= (nrbits(anac, 8) & 0x1) ? 0x4 : 0; |
| 601 | parity |= (nrbits(nacs, 8) & 0x1) ? 0x8 : 0; |
Thomas Gleixner | 97894cd | 2005-11-07 11:15:26 +0000 | [diff] [blame] | 602 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 603 | oob.u.a.virtualUnitNo = cpu_to_le16(thisVUC); |
| 604 | oob.u.a.prevUnitNo = cpu_to_le16(prev_block); |
| 605 | oob.u.a.ANAC = anac; |
| 606 | oob.u.a.NACs = nacs; |
| 607 | oob.u.a.parityPerField = parity; |
| 608 | oob.u.a.discarded = 0xaa; |
| 609 | |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 610 | inftl_write_oob(mtd, writeEUN * inftl->EraseSize + 8, 8, |
| 611 | &retlen, (char *)&oob.u); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 612 | |
| 613 | /* Also back up header... */ |
| 614 | oob.u.b.virtualUnitNo = cpu_to_le16(thisVUC); |
| 615 | oob.u.b.prevUnitNo = cpu_to_le16(prev_block); |
| 616 | oob.u.b.ANAC = anac; |
| 617 | oob.u.b.NACs = nacs; |
| 618 | oob.u.b.parityPerField = parity; |
| 619 | oob.u.b.discarded = 0xaa; |
| 620 | |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 621 | inftl_write_oob(mtd, writeEUN * inftl->EraseSize + |
| 622 | SECTORSIZE * 4 + 8, 8, &retlen, (char *)&oob.u); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 623 | |
| 624 | inftl->PUtable[writeEUN] = inftl->VUtable[thisVUC]; |
| 625 | inftl->VUtable[thisVUC] = writeEUN; |
| 626 | |
| 627 | inftl->numfreeEUNs--; |
| 628 | return writeEUN; |
| 629 | |
| 630 | } while (silly2--); |
| 631 | |
| 632 | printk(KERN_WARNING "INFTL: error folding to make room for Virtual " |
| 633 | "Unit Chain 0x%x\n", thisVUC); |
| 634 | return 0xffff; |
| 635 | } |
| 636 | |
| 637 | /* |
| 638 | * Given a Virtual Unit Chain, see if it can be deleted, and if so do it. |
| 639 | */ |
| 640 | static void INFTL_trydeletechain(struct INFTLrecord *inftl, unsigned thisVUC) |
| 641 | { |
Thomas Gleixner | f4a43cf | 2006-05-28 11:01:53 +0200 | [diff] [blame] | 642 | struct mtd_info *mtd = inftl->mbd.mtd; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 643 | unsigned char BlockUsed[MAX_SECTORS_PER_UNIT]; |
| 644 | unsigned char BlockDeleted[MAX_SECTORS_PER_UNIT]; |
| 645 | unsigned int thisEUN, status; |
| 646 | int block, silly; |
| 647 | struct inftl_bci bci; |
| 648 | size_t retlen; |
| 649 | |
| 650 | DEBUG(MTD_DEBUG_LEVEL3, "INFTL: INFTL_trydeletechain(inftl=%p," |
| 651 | "thisVUC=%d)\n", inftl, thisVUC); |
| 652 | |
| 653 | memset(BlockUsed, 0, sizeof(BlockUsed)); |
| 654 | memset(BlockDeleted, 0, sizeof(BlockDeleted)); |
| 655 | |
| 656 | thisEUN = inftl->VUtable[thisVUC]; |
| 657 | if (thisEUN == BLOCK_NIL) { |
| 658 | printk(KERN_WARNING "INFTL: trying to delete non-existent " |
| 659 | "Virtual Unit Chain %d!\n", thisVUC); |
| 660 | return; |
| 661 | } |
Thomas Gleixner | 97894cd | 2005-11-07 11:15:26 +0000 | [diff] [blame] | 662 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 663 | /* |
| 664 | * Scan through the Erase Units to determine whether any data is in |
| 665 | * each of the 512-byte blocks within the Chain. |
| 666 | */ |
| 667 | silly = MAX_LOOPS; |
| 668 | while (thisEUN < inftl->nb_blocks) { |
| 669 | for (block = 0; block < inftl->EraseSize/SECTORSIZE; block++) { |
| 670 | if (BlockUsed[block] || BlockDeleted[block]) |
| 671 | continue; |
| 672 | |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 673 | if (inftl_read_oob(mtd, (thisEUN * inftl->EraseSize) |
| 674 | + (block * SECTORSIZE), 8 , &retlen, |
Thomas Gleixner | f4a43cf | 2006-05-28 11:01:53 +0200 | [diff] [blame] | 675 | (char *)&bci) < 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 676 | status = SECTOR_IGNORE; |
| 677 | else |
| 678 | status = bci.Status | bci.Status1; |
| 679 | |
| 680 | switch(status) { |
| 681 | case SECTOR_FREE: |
| 682 | case SECTOR_IGNORE: |
| 683 | break; |
| 684 | case SECTOR_USED: |
| 685 | BlockUsed[block] = 1; |
| 686 | continue; |
| 687 | case SECTOR_DELETED: |
| 688 | BlockDeleted[block] = 1; |
| 689 | continue; |
| 690 | default: |
| 691 | printk(KERN_WARNING "INFTL: unknown status " |
| 692 | "for block %d in EUN %d: 0x%x\n", |
| 693 | block, thisEUN, status); |
| 694 | } |
| 695 | } |
| 696 | |
| 697 | if (!silly--) { |
| 698 | printk(KERN_WARNING "INFTL: infinite loop in Virtual " |
| 699 | "Unit Chain 0x%x\n", thisVUC); |
| 700 | return; |
| 701 | } |
Thomas Gleixner | 97894cd | 2005-11-07 11:15:26 +0000 | [diff] [blame] | 702 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 703 | thisEUN = inftl->PUtable[thisEUN]; |
| 704 | } |
| 705 | |
| 706 | for (block = 0; block < inftl->EraseSize/SECTORSIZE; block++) |
| 707 | if (BlockUsed[block]) |
| 708 | return; |
| 709 | |
| 710 | /* |
| 711 | * For each block in the chain free it and make it available |
| 712 | * for future use. Erase from the oldest unit first. |
| 713 | */ |
| 714 | DEBUG(MTD_DEBUG_LEVEL1, "INFTL: deleting empty VUC %d\n", thisVUC); |
| 715 | |
| 716 | for (;;) { |
| 717 | u16 *prevEUN = &inftl->VUtable[thisVUC]; |
| 718 | thisEUN = *prevEUN; |
| 719 | |
| 720 | /* If the chain is all gone already, we're done */ |
| 721 | if (thisEUN == BLOCK_NIL) { |
| 722 | DEBUG(MTD_DEBUG_LEVEL2, "INFTL: Empty VUC %d for deletion was already absent\n", thisEUN); |
| 723 | return; |
| 724 | } |
| 725 | |
| 726 | /* Find oldest unit in chain. */ |
| 727 | while (inftl->PUtable[thisEUN] != BLOCK_NIL) { |
| 728 | BUG_ON(thisEUN >= inftl->nb_blocks); |
| 729 | |
| 730 | prevEUN = &inftl->PUtable[thisEUN]; |
| 731 | thisEUN = *prevEUN; |
| 732 | } |
| 733 | |
| 734 | DEBUG(MTD_DEBUG_LEVEL3, "Deleting EUN %d from VUC %d\n", |
| 735 | thisEUN, thisVUC); |
| 736 | |
Thomas Gleixner | 9223a45 | 2006-05-23 17:21:03 +0200 | [diff] [blame] | 737 | if (INFTL_formatblock(inftl, thisEUN) < 0) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 738 | /* |
| 739 | * Could not erase : mark block as reserved. |
| 740 | */ |
| 741 | inftl->PUtable[thisEUN] = BLOCK_RESERVED; |
Thomas Gleixner | 9223a45 | 2006-05-23 17:21:03 +0200 | [diff] [blame] | 742 | } else { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 743 | /* Correctly erased : mark it as free */ |
| 744 | inftl->PUtable[thisEUN] = BLOCK_FREE; |
| 745 | inftl->numfreeEUNs++; |
| 746 | } |
| 747 | |
| 748 | /* Now sort out whatever was pointing to it... */ |
| 749 | *prevEUN = BLOCK_NIL; |
| 750 | |
| 751 | /* Ideally we'd actually be responsive to new |
| 752 | requests while we're doing this -- if there's |
| 753 | free space why should others be made to wait? */ |
| 754 | cond_resched(); |
| 755 | } |
| 756 | |
| 757 | inftl->VUtable[thisVUC] = BLOCK_NIL; |
| 758 | } |
| 759 | |
| 760 | static int INFTL_deleteblock(struct INFTLrecord *inftl, unsigned block) |
| 761 | { |
| 762 | unsigned int thisEUN = inftl->VUtable[block / (inftl->EraseSize / SECTORSIZE)]; |
| 763 | unsigned long blockofs = (block * SECTORSIZE) & (inftl->EraseSize - 1); |
Thomas Gleixner | f4a43cf | 2006-05-28 11:01:53 +0200 | [diff] [blame] | 764 | struct mtd_info *mtd = inftl->mbd.mtd; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 765 | unsigned int status; |
| 766 | int silly = MAX_LOOPS; |
| 767 | size_t retlen; |
| 768 | struct inftl_bci bci; |
| 769 | |
| 770 | DEBUG(MTD_DEBUG_LEVEL3, "INFTL: INFTL_deleteblock(inftl=%p," |
| 771 | "block=%d)\n", inftl, block); |
| 772 | |
| 773 | while (thisEUN < inftl->nb_blocks) { |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 774 | if (inftl_read_oob(mtd, (thisEUN * inftl->EraseSize) + |
| 775 | blockofs, 8, &retlen, (char *)&bci) < 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 776 | status = SECTOR_IGNORE; |
| 777 | else |
| 778 | status = bci.Status | bci.Status1; |
| 779 | |
| 780 | switch (status) { |
| 781 | case SECTOR_FREE: |
| 782 | case SECTOR_IGNORE: |
| 783 | break; |
| 784 | case SECTOR_DELETED: |
| 785 | thisEUN = BLOCK_NIL; |
| 786 | goto foundit; |
| 787 | case SECTOR_USED: |
| 788 | goto foundit; |
| 789 | default: |
| 790 | printk(KERN_WARNING "INFTL: unknown status for " |
| 791 | "block %d in EUN %d: 0x%x\n", |
| 792 | block, thisEUN, status); |
| 793 | break; |
| 794 | } |
| 795 | |
| 796 | if (!silly--) { |
| 797 | printk(KERN_WARNING "INFTL: infinite loop in Virtual " |
| 798 | "Unit Chain 0x%x\n", |
| 799 | block / (inftl->EraseSize / SECTORSIZE)); |
| 800 | return 1; |
| 801 | } |
| 802 | thisEUN = inftl->PUtable[thisEUN]; |
| 803 | } |
| 804 | |
| 805 | foundit: |
| 806 | if (thisEUN != BLOCK_NIL) { |
| 807 | loff_t ptr = (thisEUN * inftl->EraseSize) + blockofs; |
| 808 | |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 809 | if (inftl_read_oob(mtd, ptr, 8, &retlen, (char *)&bci) < 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 810 | return -EIO; |
| 811 | bci.Status = bci.Status1 = SECTOR_DELETED; |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 812 | if (inftl_write_oob(mtd, ptr, 8, &retlen, (char *)&bci) < 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 813 | return -EIO; |
| 814 | INFTL_trydeletechain(inftl, block / (inftl->EraseSize / SECTORSIZE)); |
| 815 | } |
| 816 | return 0; |
| 817 | } |
| 818 | |
Thomas Gleixner | 97894cd | 2005-11-07 11:15:26 +0000 | [diff] [blame] | 819 | static int inftl_writeblock(struct mtd_blktrans_dev *mbd, unsigned long block, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 820 | char *buffer) |
| 821 | { |
| 822 | struct INFTLrecord *inftl = (void *)mbd; |
| 823 | unsigned int writeEUN; |
| 824 | unsigned long blockofs = (block * SECTORSIZE) & (inftl->EraseSize - 1); |
| 825 | size_t retlen; |
| 826 | struct inftl_oob oob; |
| 827 | char *p, *pend; |
| 828 | |
| 829 | DEBUG(MTD_DEBUG_LEVEL3, "INFTL: inftl_writeblock(inftl=%p,block=%ld," |
| 830 | "buffer=%p)\n", inftl, block, buffer); |
| 831 | |
| 832 | /* Is block all zero? */ |
| 833 | pend = buffer + SECTORSIZE; |
| 834 | for (p = buffer; p < pend && !*p; p++) |
| 835 | ; |
| 836 | |
| 837 | if (p < pend) { |
| 838 | writeEUN = INFTL_findwriteunit(inftl, block); |
| 839 | |
| 840 | if (writeEUN == BLOCK_NIL) { |
| 841 | printk(KERN_WARNING "inftl_writeblock(): cannot find " |
| 842 | "block to write to\n"); |
| 843 | /* |
| 844 | * If we _still_ haven't got a block to use, |
| 845 | * we're screwed. |
| 846 | */ |
| 847 | return 1; |
| 848 | } |
| 849 | |
| 850 | memset(&oob, 0xff, sizeof(struct inftl_oob)); |
| 851 | oob.b.Status = oob.b.Status1 = SECTOR_USED; |
Thomas Gleixner | 9223a45 | 2006-05-23 17:21:03 +0200 | [diff] [blame] | 852 | |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 853 | inftl_write(inftl->mbd.mtd, (writeEUN * inftl->EraseSize) + |
| 854 | blockofs, SECTORSIZE, &retlen, (char *)buffer, |
| 855 | (char *)&oob); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 856 | /* |
| 857 | * need to write SECTOR_USED flags since they are not written |
| 858 | * in mtd_writeecc |
| 859 | */ |
| 860 | } else { |
| 861 | INFTL_deleteblock(inftl, block); |
| 862 | } |
| 863 | |
| 864 | return 0; |
| 865 | } |
| 866 | |
| 867 | static int inftl_readblock(struct mtd_blktrans_dev *mbd, unsigned long block, |
| 868 | char *buffer) |
| 869 | { |
| 870 | struct INFTLrecord *inftl = (void *)mbd; |
| 871 | unsigned int thisEUN = inftl->VUtable[block / (inftl->EraseSize / SECTORSIZE)]; |
| 872 | unsigned long blockofs = (block * SECTORSIZE) & (inftl->EraseSize - 1); |
Thomas Gleixner | f4a43cf | 2006-05-28 11:01:53 +0200 | [diff] [blame] | 873 | struct mtd_info *mtd = inftl->mbd.mtd; |
Thomas Gleixner | 9223a45 | 2006-05-23 17:21:03 +0200 | [diff] [blame] | 874 | unsigned int status; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 875 | int silly = MAX_LOOPS; |
Thomas Gleixner | 9223a45 | 2006-05-23 17:21:03 +0200 | [diff] [blame] | 876 | struct inftl_bci bci; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 877 | size_t retlen; |
| 878 | |
| 879 | DEBUG(MTD_DEBUG_LEVEL3, "INFTL: inftl_readblock(inftl=%p,block=%ld," |
| 880 | "buffer=%p)\n", inftl, block, buffer); |
| 881 | |
| 882 | while (thisEUN < inftl->nb_blocks) { |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 883 | if (inftl_read_oob(mtd, (thisEUN * inftl->EraseSize) + |
Thomas Gleixner | f4a43cf | 2006-05-28 11:01:53 +0200 | [diff] [blame] | 884 | blockofs, 8, &retlen, (char *)&bci) < 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 885 | status = SECTOR_IGNORE; |
| 886 | else |
| 887 | status = bci.Status | bci.Status1; |
| 888 | |
| 889 | switch (status) { |
| 890 | case SECTOR_DELETED: |
| 891 | thisEUN = BLOCK_NIL; |
| 892 | goto foundit; |
| 893 | case SECTOR_USED: |
| 894 | goto foundit; |
| 895 | case SECTOR_FREE: |
| 896 | case SECTOR_IGNORE: |
| 897 | break; |
| 898 | default: |
| 899 | printk(KERN_WARNING "INFTL: unknown status for " |
| 900 | "block %ld in EUN %d: 0x%04x\n", |
| 901 | block, thisEUN, status); |
| 902 | break; |
| 903 | } |
| 904 | |
| 905 | if (!silly--) { |
| 906 | printk(KERN_WARNING "INFTL: infinite loop in " |
| 907 | "Virtual Unit Chain 0x%lx\n", |
| 908 | block / (inftl->EraseSize / SECTORSIZE)); |
| 909 | return 1; |
| 910 | } |
| 911 | |
| 912 | thisEUN = inftl->PUtable[thisEUN]; |
| 913 | } |
| 914 | |
| 915 | foundit: |
| 916 | if (thisEUN == BLOCK_NIL) { |
| 917 | /* The requested block is not on the media, return all 0x00 */ |
| 918 | memset(buffer, 0, SECTORSIZE); |
| 919 | } else { |
Thomas Gleixner | 9223a45 | 2006-05-23 17:21:03 +0200 | [diff] [blame] | 920 | size_t retlen; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 921 | loff_t ptr = (thisEUN * inftl->EraseSize) + blockofs; |
Thomas Gleixner | 9a1fcdf | 2006-05-29 14:56:39 +0200 | [diff] [blame] | 922 | int ret = mtd->read(mtd, ptr, SECTORSIZE, &retlen, buffer); |
| 923 | |
| 924 | /* Handle corrected bit flips gracefully */ |
| 925 | if (ret < 0 && ret != -EUCLEAN) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 926 | return -EIO; |
| 927 | } |
| 928 | return 0; |
| 929 | } |
| 930 | |
| 931 | static int inftl_getgeo(struct mtd_blktrans_dev *dev, struct hd_geometry *geo) |
| 932 | { |
| 933 | struct INFTLrecord *inftl = (void *)dev; |
| 934 | |
| 935 | geo->heads = inftl->heads; |
| 936 | geo->sectors = inftl->sectors; |
| 937 | geo->cylinders = inftl->cylinders; |
| 938 | |
| 939 | return 0; |
| 940 | } |
| 941 | |
| 942 | static struct mtd_blktrans_ops inftl_tr = { |
| 943 | .name = "inftl", |
| 944 | .major = INFTL_MAJOR, |
| 945 | .part_bits = INFTL_PARTN_BITS, |
Richard Purdie | 1918767 | 2006-10-27 09:09:33 +0100 | [diff] [blame] | 946 | .blksize = 512, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 947 | .getgeo = inftl_getgeo, |
| 948 | .readsect = inftl_readblock, |
| 949 | .writesect = inftl_writeblock, |
| 950 | .add_mtd = inftl_add_mtd, |
| 951 | .remove_dev = inftl_remove_dev, |
| 952 | .owner = THIS_MODULE, |
| 953 | }; |
| 954 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 955 | static int __init init_inftl(void) |
| 956 | { |
Thomas Gleixner | 97894cd | 2005-11-07 11:15:26 +0000 | [diff] [blame] | 957 | printk(KERN_INFO "INFTL: inftlcore.c $Revision: 1.19 $, " |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 958 | "inftlmount.c %s\n", inftlmountrev); |
| 959 | |
| 960 | return register_mtd_blktrans(&inftl_tr); |
| 961 | } |
| 962 | |
| 963 | static void __exit cleanup_inftl(void) |
| 964 | { |
| 965 | deregister_mtd_blktrans(&inftl_tr); |
| 966 | } |
| 967 | |
| 968 | module_init(init_inftl); |
| 969 | module_exit(cleanup_inftl); |
| 970 | |
| 971 | MODULE_LICENSE("GPL"); |
| 972 | MODULE_AUTHOR("Greg Ungerer <gerg@snapgear.com>, David Woodhouse <dwmw2@infradead.org>, Fabrice Bellard <fabrice.bellard@netgem.com> et al."); |
| 973 | MODULE_DESCRIPTION("Support code for Inverse Flash Translation Layer, used on M-Systems DiskOnChip 2000, Millennium and Millennium Plus"); |