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