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