Sean Young | e27a996 | 2005-06-16 09:49:33 +0100 | [diff] [blame] | 1 | /* |
| 2 | * rfd_ftl.c -- resident flash disk (flash translation layer) |
| 3 | * |
| 4 | * Copyright (C) 2005 Sean Young <sean@mess.org> |
| 5 | * |
Thomas Gleixner | 97894cd | 2005-11-07 11:15:26 +0000 | [diff] [blame] | 6 | * $Id: rfd_ftl.c,v 1.5 2005/11/07 11:14:21 gleixner Exp $ |
Sean Young | e27a996 | 2005-06-16 09:49:33 +0100 | [diff] [blame] | 7 | * |
| 8 | * This type of flash translation layer (FTL) is used by the Embedded BIOS |
| 9 | * by General Software. It is known as the Resident Flash Disk (RFD), see: |
| 10 | * |
| 11 | * http://www.gensw.com/pages/prod/bios/rfd.htm |
| 12 | * |
| 13 | * based on ftl.c |
| 14 | */ |
| 15 | |
| 16 | #include <linux/hdreg.h> |
| 17 | #include <linux/init.h> |
| 18 | #include <linux/mtd/blktrans.h> |
| 19 | #include <linux/mtd/mtd.h> |
| 20 | #include <linux/vmalloc.h> |
Andrew Morton | b80b583 | 2005-11-08 21:34:27 -0800 | [diff] [blame] | 21 | #include <linux/jiffies.h> |
Sean Young | e27a996 | 2005-06-16 09:49:33 +0100 | [diff] [blame] | 22 | |
| 23 | #include <asm/types.h> |
| 24 | |
| 25 | #define const_cpu_to_le16 __constant_cpu_to_le16 |
| 26 | |
| 27 | static int block_size = 0; |
| 28 | module_param(block_size, int, 0); |
| 29 | MODULE_PARM_DESC(block_size, "Block size to use by RFD, defaults to erase unit size"); |
| 30 | |
| 31 | #define PREFIX "rfd_ftl: " |
| 32 | |
| 33 | /* Major device # for FTL device */ |
| 34 | |
| 35 | /* A request for this major has been sent to device@lanana.org */ |
| 36 | #ifndef RFD_FTL_MAJOR |
| 37 | #define RFD_FTL_MAJOR 95 |
| 38 | #endif |
| 39 | |
| 40 | /* Maximum number of partitions in an FTL region */ |
| 41 | #define PART_BITS 4 |
| 42 | |
| 43 | /* An erase unit should start with this value */ |
| 44 | #define RFD_MAGIC 0x9193 |
| 45 | |
| 46 | /* the second value is 0xffff or 0xffc8; function unknown */ |
| 47 | |
| 48 | /* the third value is always 0xffff, ignored */ |
| 49 | |
| 50 | /* next is an array of mapping for each corresponding sector */ |
| 51 | #define HEADER_MAP_OFFSET 3 |
| 52 | #define SECTOR_DELETED 0x0000 |
| 53 | #define SECTOR_ZERO 0xfffe |
| 54 | #define SECTOR_FREE 0xffff |
| 55 | |
| 56 | #define SECTOR_SIZE 512 |
| 57 | |
| 58 | #define SECTORS_PER_TRACK 63 |
| 59 | |
| 60 | struct block { |
| 61 | enum { |
| 62 | BLOCK_OK, |
| 63 | BLOCK_ERASING, |
| 64 | BLOCK_ERASED, |
| 65 | BLOCK_FAILED |
| 66 | } state; |
| 67 | int free_sectors; |
| 68 | int used_sectors; |
| 69 | int erases; |
| 70 | u_long offset; |
| 71 | }; |
| 72 | |
| 73 | struct partition { |
| 74 | struct mtd_blktrans_dev mbd; |
| 75 | |
| 76 | u_int block_size; /* size of erase unit */ |
| 77 | u_int total_blocks; /* number of erase units */ |
| 78 | u_int header_sectors_per_block; /* header sectors in erase unit */ |
| 79 | u_int data_sectors_per_block; /* data sectors in erase unit */ |
| 80 | u_int sector_count; /* sectors in translated disk */ |
| 81 | u_int header_size; /* bytes in header sector */ |
| 82 | int reserved_block; /* block next up for reclaim */ |
| 83 | int current_block; /* block to write to */ |
| 84 | u16 *header_cache; /* cached header */ |
| 85 | |
| 86 | int is_reclaiming; |
| 87 | int cylinders; |
| 88 | int errors; |
| 89 | u_long *sector_map; |
| 90 | struct block *blocks; |
| 91 | }; |
| 92 | |
| 93 | static int rfd_ftl_writesect(struct mtd_blktrans_dev *dev, u_long sector, char *buf); |
| 94 | |
| 95 | static int build_block_map(struct partition *part, int block_no) |
| 96 | { |
| 97 | struct block *block = &part->blocks[block_no]; |
| 98 | int i; |
Thomas Gleixner | 97894cd | 2005-11-07 11:15:26 +0000 | [diff] [blame] | 99 | |
Sean Young | e27a996 | 2005-06-16 09:49:33 +0100 | [diff] [blame] | 100 | block->offset = part->block_size * block_no; |
| 101 | |
| 102 | if (le16_to_cpu(part->header_cache[0]) != RFD_MAGIC) { |
| 103 | block->state = BLOCK_ERASED; /* assumption */ |
| 104 | block->free_sectors = part->data_sectors_per_block; |
| 105 | part->reserved_block = block_no; |
| 106 | return 1; |
| 107 | } |
| 108 | |
| 109 | block->state = BLOCK_OK; |
| 110 | |
| 111 | for (i=0; i<part->data_sectors_per_block; i++) { |
| 112 | u16 entry; |
Thomas Gleixner | 97894cd | 2005-11-07 11:15:26 +0000 | [diff] [blame] | 113 | |
Sean Young | e27a996 | 2005-06-16 09:49:33 +0100 | [diff] [blame] | 114 | entry = le16_to_cpu(part->header_cache[HEADER_MAP_OFFSET + i]); |
| 115 | |
| 116 | if (entry == SECTOR_DELETED) |
| 117 | continue; |
Thomas Gleixner | 97894cd | 2005-11-07 11:15:26 +0000 | [diff] [blame] | 118 | |
Sean Young | e27a996 | 2005-06-16 09:49:33 +0100 | [diff] [blame] | 119 | if (entry == SECTOR_FREE) { |
| 120 | block->free_sectors++; |
| 121 | continue; |
| 122 | } |
| 123 | |
| 124 | if (entry == SECTOR_ZERO) |
| 125 | entry = 0; |
Thomas Gleixner | 97894cd | 2005-11-07 11:15:26 +0000 | [diff] [blame] | 126 | |
Sean Young | e27a996 | 2005-06-16 09:49:33 +0100 | [diff] [blame] | 127 | if (entry >= part->sector_count) { |
Thomas Gleixner | 97894cd | 2005-11-07 11:15:26 +0000 | [diff] [blame] | 128 | printk(KERN_NOTICE PREFIX |
Sean Young | e27a996 | 2005-06-16 09:49:33 +0100 | [diff] [blame] | 129 | "'%s': unit #%d: entry %d corrupt, " |
| 130 | "sector %d out of range\n", |
| 131 | part->mbd.mtd->name, block_no, i, entry); |
| 132 | continue; |
| 133 | } |
| 134 | |
| 135 | if (part->sector_map[entry] != -1) { |
Thomas Gleixner | 97894cd | 2005-11-07 11:15:26 +0000 | [diff] [blame] | 136 | printk(KERN_NOTICE PREFIX |
Sean Young | e27a996 | 2005-06-16 09:49:33 +0100 | [diff] [blame] | 137 | "'%s': more than one entry for sector %d\n", |
| 138 | part->mbd.mtd->name, entry); |
| 139 | part->errors = 1; |
| 140 | continue; |
| 141 | } |
| 142 | |
Thomas Gleixner | 97894cd | 2005-11-07 11:15:26 +0000 | [diff] [blame] | 143 | part->sector_map[entry] = block->offset + |
Sean Young | e27a996 | 2005-06-16 09:49:33 +0100 | [diff] [blame] | 144 | (i + part->header_sectors_per_block) * SECTOR_SIZE; |
| 145 | |
| 146 | block->used_sectors++; |
| 147 | } |
| 148 | |
| 149 | if (block->free_sectors == part->data_sectors_per_block) |
| 150 | part->reserved_block = block_no; |
| 151 | |
| 152 | return 0; |
| 153 | } |
| 154 | |
| 155 | static int scan_header(struct partition *part) |
| 156 | { |
| 157 | int sectors_per_block; |
| 158 | int i, rc = -ENOMEM; |
| 159 | int blocks_found; |
| 160 | size_t retlen; |
| 161 | |
| 162 | sectors_per_block = part->block_size / SECTOR_SIZE; |
| 163 | part->total_blocks = part->mbd.mtd->size / part->block_size; |
| 164 | |
| 165 | if (part->total_blocks < 2) |
| 166 | return -ENOENT; |
| 167 | |
| 168 | /* each erase block has three bytes header, followed by the map */ |
Thomas Gleixner | 97894cd | 2005-11-07 11:15:26 +0000 | [diff] [blame] | 169 | part->header_sectors_per_block = |
| 170 | ((HEADER_MAP_OFFSET + sectors_per_block) * |
Sean Young | e27a996 | 2005-06-16 09:49:33 +0100 | [diff] [blame] | 171 | sizeof(u16) + SECTOR_SIZE - 1) / SECTOR_SIZE; |
| 172 | |
Thomas Gleixner | 97894cd | 2005-11-07 11:15:26 +0000 | [diff] [blame] | 173 | part->data_sectors_per_block = sectors_per_block - |
Sean Young | e27a996 | 2005-06-16 09:49:33 +0100 | [diff] [blame] | 174 | part->header_sectors_per_block; |
| 175 | |
Thomas Gleixner | 97894cd | 2005-11-07 11:15:26 +0000 | [diff] [blame] | 176 | part->header_size = (HEADER_MAP_OFFSET + |
Sean Young | e27a996 | 2005-06-16 09:49:33 +0100 | [diff] [blame] | 177 | part->data_sectors_per_block) * sizeof(u16); |
| 178 | |
| 179 | part->cylinders = (part->data_sectors_per_block * |
| 180 | (part->total_blocks - 1) - 1) / SECTORS_PER_TRACK; |
| 181 | |
| 182 | part->sector_count = part->cylinders * SECTORS_PER_TRACK; |
| 183 | |
| 184 | part->current_block = -1; |
| 185 | part->reserved_block = -1; |
| 186 | part->is_reclaiming = 0; |
| 187 | |
| 188 | part->header_cache = kmalloc(part->header_size, GFP_KERNEL); |
| 189 | if (!part->header_cache) |
| 190 | goto err; |
| 191 | |
Thomas Gleixner | 97894cd | 2005-11-07 11:15:26 +0000 | [diff] [blame] | 192 | part->blocks = kcalloc(part->total_blocks, sizeof(struct block), |
Sean Young | e27a996 | 2005-06-16 09:49:33 +0100 | [diff] [blame] | 193 | GFP_KERNEL); |
| 194 | if (!part->blocks) |
| 195 | goto err; |
| 196 | |
| 197 | part->sector_map = vmalloc(part->sector_count * sizeof(u_long)); |
| 198 | if (!part->sector_map) { |
| 199 | printk(KERN_ERR PREFIX "'%s': unable to allocate memory for " |
| 200 | "sector map", part->mbd.mtd->name); |
| 201 | goto err; |
| 202 | } |
| 203 | |
Thomas Gleixner | 97894cd | 2005-11-07 11:15:26 +0000 | [diff] [blame] | 204 | for (i=0; i<part->sector_count; i++) |
Sean Young | e27a996 | 2005-06-16 09:49:33 +0100 | [diff] [blame] | 205 | part->sector_map[i] = -1; |
| 206 | |
| 207 | for (i=0, blocks_found=0; i<part->total_blocks; i++) { |
Thomas Gleixner | 97894cd | 2005-11-07 11:15:26 +0000 | [diff] [blame] | 208 | rc = part->mbd.mtd->read(part->mbd.mtd, |
Sean Young | e27a996 | 2005-06-16 09:49:33 +0100 | [diff] [blame] | 209 | i * part->block_size, part->header_size, |
| 210 | &retlen, (u_char*)part->header_cache); |
| 211 | |
| 212 | if (!rc && retlen != part->header_size) |
| 213 | rc = -EIO; |
| 214 | |
Thomas Gleixner | 97894cd | 2005-11-07 11:15:26 +0000 | [diff] [blame] | 215 | if (rc) |
Sean Young | e27a996 | 2005-06-16 09:49:33 +0100 | [diff] [blame] | 216 | goto err; |
| 217 | |
| 218 | if (!build_block_map(part, i)) |
| 219 | blocks_found++; |
| 220 | } |
| 221 | |
| 222 | if (blocks_found == 0) { |
| 223 | printk(KERN_NOTICE PREFIX "no RFD magic found in '%s'\n", |
| 224 | part->mbd.mtd->name); |
| 225 | rc = -ENOENT; |
| 226 | goto err; |
| 227 | } |
| 228 | |
| 229 | if (part->reserved_block == -1) { |
Thomas Gleixner | 97894cd | 2005-11-07 11:15:26 +0000 | [diff] [blame] | 230 | printk(KERN_NOTICE PREFIX "'%s': no empty erase unit found\n", |
Sean Young | e27a996 | 2005-06-16 09:49:33 +0100 | [diff] [blame] | 231 | part->mbd.mtd->name); |
| 232 | |
| 233 | part->errors = 1; |
| 234 | } |
| 235 | |
| 236 | return 0; |
| 237 | |
| 238 | err: |
| 239 | vfree(part->sector_map); |
| 240 | kfree(part->header_cache); |
| 241 | kfree(part->blocks); |
| 242 | |
| 243 | return rc; |
| 244 | } |
| 245 | |
| 246 | static int rfd_ftl_readsect(struct mtd_blktrans_dev *dev, u_long sector, char *buf) |
| 247 | { |
| 248 | struct partition *part = (struct partition*)dev; |
| 249 | u_long addr; |
| 250 | size_t retlen; |
| 251 | int rc; |
Thomas Gleixner | 97894cd | 2005-11-07 11:15:26 +0000 | [diff] [blame] | 252 | |
Sean Young | e27a996 | 2005-06-16 09:49:33 +0100 | [diff] [blame] | 253 | if (sector >= part->sector_count) |
| 254 | return -EIO; |
| 255 | |
| 256 | addr = part->sector_map[sector]; |
| 257 | if (addr != -1) { |
| 258 | rc = part->mbd.mtd->read(part->mbd.mtd, addr, SECTOR_SIZE, |
| 259 | &retlen, (u_char*)buf); |
| 260 | if (!rc && retlen != SECTOR_SIZE) |
| 261 | rc = -EIO; |
| 262 | |
| 263 | if (rc) { |
| 264 | printk(KERN_WARNING PREFIX "error reading '%s' at " |
| 265 | "0x%lx\n", part->mbd.mtd->name, addr); |
| 266 | return rc; |
| 267 | } |
| 268 | } else |
| 269 | memset(buf, 0, SECTOR_SIZE); |
Thomas Gleixner | 97894cd | 2005-11-07 11:15:26 +0000 | [diff] [blame] | 270 | |
Sean Young | e27a996 | 2005-06-16 09:49:33 +0100 | [diff] [blame] | 271 | return 0; |
Thomas Gleixner | 97894cd | 2005-11-07 11:15:26 +0000 | [diff] [blame] | 272 | } |
Sean Young | e27a996 | 2005-06-16 09:49:33 +0100 | [diff] [blame] | 273 | |
| 274 | static void erase_callback(struct erase_info *erase) |
| 275 | { |
| 276 | struct partition *part; |
| 277 | u16 magic; |
| 278 | int i, rc; |
| 279 | size_t retlen; |
| 280 | |
| 281 | part = (struct partition*)erase->priv; |
| 282 | |
| 283 | i = erase->addr / part->block_size; |
| 284 | if (i >= part->total_blocks || part->blocks[i].offset != erase->addr) { |
| 285 | printk(KERN_ERR PREFIX "erase callback for unknown offset %x " |
| 286 | "on '%s'\n", erase->addr, part->mbd.mtd->name); |
| 287 | return; |
| 288 | } |
| 289 | |
| 290 | if (erase->state != MTD_ERASE_DONE) { |
| 291 | printk(KERN_WARNING PREFIX "erase failed at 0x%x on '%s', " |
Thomas Gleixner | 97894cd | 2005-11-07 11:15:26 +0000 | [diff] [blame] | 292 | "state %d\n", erase->addr, |
Sean Young | e27a996 | 2005-06-16 09:49:33 +0100 | [diff] [blame] | 293 | part->mbd.mtd->name, erase->state); |
| 294 | |
| 295 | part->blocks[i].state = BLOCK_FAILED; |
| 296 | part->blocks[i].free_sectors = 0; |
| 297 | part->blocks[i].used_sectors = 0; |
| 298 | |
| 299 | kfree(erase); |
| 300 | |
| 301 | return; |
| 302 | } |
| 303 | |
| 304 | magic = const_cpu_to_le16(RFD_MAGIC); |
| 305 | |
| 306 | part->blocks[i].state = BLOCK_ERASED; |
| 307 | part->blocks[i].free_sectors = part->data_sectors_per_block; |
| 308 | part->blocks[i].used_sectors = 0; |
| 309 | part->blocks[i].erases++; |
| 310 | |
Thomas Gleixner | 97894cd | 2005-11-07 11:15:26 +0000 | [diff] [blame] | 311 | rc = part->mbd.mtd->write(part->mbd.mtd, |
| 312 | part->blocks[i].offset, sizeof(magic), &retlen, |
Sean Young | e27a996 | 2005-06-16 09:49:33 +0100 | [diff] [blame] | 313 | (u_char*)&magic); |
Thomas Gleixner | 97894cd | 2005-11-07 11:15:26 +0000 | [diff] [blame] | 314 | |
Sean Young | e27a996 | 2005-06-16 09:49:33 +0100 | [diff] [blame] | 315 | if (!rc && retlen != sizeof(magic)) |
| 316 | rc = -EIO; |
| 317 | |
| 318 | if (rc) { |
| 319 | printk(KERN_NOTICE PREFIX "'%s': unable to write RFD " |
| 320 | "header at 0x%lx\n", |
Thomas Gleixner | 97894cd | 2005-11-07 11:15:26 +0000 | [diff] [blame] | 321 | part->mbd.mtd->name, |
Sean Young | e27a996 | 2005-06-16 09:49:33 +0100 | [diff] [blame] | 322 | part->blocks[i].offset); |
| 323 | part->blocks[i].state = BLOCK_FAILED; |
| 324 | } |
| 325 | else |
| 326 | part->blocks[i].state = BLOCK_OK; |
| 327 | |
| 328 | kfree(erase); |
| 329 | } |
| 330 | |
| 331 | static int erase_block(struct partition *part, int block) |
| 332 | { |
| 333 | struct erase_info *erase; |
| 334 | int rc = -ENOMEM; |
| 335 | |
| 336 | erase = kmalloc(sizeof(struct erase_info), GFP_KERNEL); |
| 337 | if (!erase) |
| 338 | goto err; |
| 339 | |
| 340 | erase->mtd = part->mbd.mtd; |
| 341 | erase->callback = erase_callback; |
| 342 | erase->addr = part->blocks[block].offset; |
| 343 | erase->len = part->block_size; |
| 344 | erase->priv = (u_long)part; |
| 345 | |
| 346 | part->blocks[block].state = BLOCK_ERASING; |
| 347 | part->blocks[block].free_sectors = 0; |
| 348 | |
| 349 | rc = part->mbd.mtd->erase(part->mbd.mtd, erase); |
| 350 | |
| 351 | if (rc) { |
| 352 | printk(KERN_WARNING PREFIX "erase of region %x,%x on '%s' " |
| 353 | "failed\n", erase->addr, erase->len, |
| 354 | part->mbd.mtd->name); |
| 355 | kfree(erase); |
| 356 | } |
| 357 | |
| 358 | err: |
| 359 | return rc; |
| 360 | } |
| 361 | |
| 362 | static int move_block_contents(struct partition *part, int block_no, u_long *old_sector) |
| 363 | { |
| 364 | void *sector_data; |
| 365 | u16 *map; |
| 366 | size_t retlen; |
| 367 | int i, rc = -ENOMEM; |
| 368 | |
| 369 | part->is_reclaiming = 1; |
| 370 | |
| 371 | sector_data = kmalloc(SECTOR_SIZE, GFP_KERNEL); |
| 372 | if (!sector_data) |
| 373 | goto err3; |
| 374 | |
| 375 | map = kmalloc(part->header_size, GFP_KERNEL); |
| 376 | if (!map) |
| 377 | goto err2; |
Thomas Gleixner | 97894cd | 2005-11-07 11:15:26 +0000 | [diff] [blame] | 378 | |
| 379 | rc = part->mbd.mtd->read(part->mbd.mtd, |
| 380 | part->blocks[block_no].offset, part->header_size, |
Sean Young | e27a996 | 2005-06-16 09:49:33 +0100 | [diff] [blame] | 381 | &retlen, (u_char*)map); |
Thomas Gleixner | 97894cd | 2005-11-07 11:15:26 +0000 | [diff] [blame] | 382 | |
Sean Young | e27a996 | 2005-06-16 09:49:33 +0100 | [diff] [blame] | 383 | if (!rc && retlen != part->header_size) |
| 384 | rc = -EIO; |
| 385 | |
| 386 | if (rc) { |
| 387 | printk(KERN_NOTICE PREFIX "error reading '%s' at " |
Thomas Gleixner | 97894cd | 2005-11-07 11:15:26 +0000 | [diff] [blame] | 388 | "0x%lx\n", part->mbd.mtd->name, |
Sean Young | e27a996 | 2005-06-16 09:49:33 +0100 | [diff] [blame] | 389 | part->blocks[block_no].offset); |
| 390 | |
| 391 | goto err; |
| 392 | } |
| 393 | |
| 394 | for (i=0; i<part->data_sectors_per_block; i++) { |
| 395 | u16 entry = le16_to_cpu(map[HEADER_MAP_OFFSET + i]); |
| 396 | u_long addr; |
| 397 | |
| 398 | |
| 399 | if (entry == SECTOR_FREE || entry == SECTOR_DELETED) |
| 400 | continue; |
| 401 | |
Thomas Gleixner | 97894cd | 2005-11-07 11:15:26 +0000 | [diff] [blame] | 402 | if (entry == SECTOR_ZERO) |
Sean Young | e27a996 | 2005-06-16 09:49:33 +0100 | [diff] [blame] | 403 | entry = 0; |
| 404 | |
| 405 | /* already warned about and ignored in build_block_map() */ |
Thomas Gleixner | 97894cd | 2005-11-07 11:15:26 +0000 | [diff] [blame] | 406 | if (entry >= part->sector_count) |
Sean Young | e27a996 | 2005-06-16 09:49:33 +0100 | [diff] [blame] | 407 | continue; |
| 408 | |
| 409 | addr = part->blocks[block_no].offset + |
| 410 | (i + part->header_sectors_per_block) * SECTOR_SIZE; |
| 411 | |
| 412 | if (*old_sector == addr) { |
| 413 | *old_sector = -1; |
| 414 | if (!part->blocks[block_no].used_sectors--) { |
| 415 | rc = erase_block(part, block_no); |
| 416 | break; |
| 417 | } |
| 418 | continue; |
| 419 | } |
| 420 | rc = part->mbd.mtd->read(part->mbd.mtd, addr, |
| 421 | SECTOR_SIZE, &retlen, sector_data); |
Thomas Gleixner | 97894cd | 2005-11-07 11:15:26 +0000 | [diff] [blame] | 422 | |
Sean Young | e27a996 | 2005-06-16 09:49:33 +0100 | [diff] [blame] | 423 | if (!rc && retlen != SECTOR_SIZE) |
| 424 | rc = -EIO; |
| 425 | |
| 426 | if (rc) { |
| 427 | printk(KERN_NOTICE PREFIX "'%s': Unable to " |
| 428 | "read sector for relocation\n", |
| 429 | part->mbd.mtd->name); |
| 430 | |
| 431 | goto err; |
| 432 | } |
Thomas Gleixner | 97894cd | 2005-11-07 11:15:26 +0000 | [diff] [blame] | 433 | |
Sean Young | e27a996 | 2005-06-16 09:49:33 +0100 | [diff] [blame] | 434 | rc = rfd_ftl_writesect((struct mtd_blktrans_dev*)part, |
| 435 | entry, sector_data); |
Thomas Gleixner | 97894cd | 2005-11-07 11:15:26 +0000 | [diff] [blame] | 436 | |
| 437 | if (rc) |
Sean Young | e27a996 | 2005-06-16 09:49:33 +0100 | [diff] [blame] | 438 | goto err; |
| 439 | } |
| 440 | |
| 441 | err: |
| 442 | kfree(map); |
| 443 | err2: |
| 444 | kfree(sector_data); |
| 445 | err3: |
| 446 | part->is_reclaiming = 0; |
| 447 | |
| 448 | return rc; |
| 449 | } |
| 450 | |
Thomas Gleixner | 97894cd | 2005-11-07 11:15:26 +0000 | [diff] [blame] | 451 | static int reclaim_block(struct partition *part, u_long *old_sector) |
Sean Young | e27a996 | 2005-06-16 09:49:33 +0100 | [diff] [blame] | 452 | { |
| 453 | int block, best_block, score, old_sector_block; |
| 454 | int rc; |
Thomas Gleixner | 97894cd | 2005-11-07 11:15:26 +0000 | [diff] [blame] | 455 | |
Sean Young | e27a996 | 2005-06-16 09:49:33 +0100 | [diff] [blame] | 456 | /* we have a race if sync doesn't exist */ |
| 457 | if (part->mbd.mtd->sync) |
| 458 | part->mbd.mtd->sync(part->mbd.mtd); |
| 459 | |
| 460 | score = 0x7fffffff; /* MAX_INT */ |
| 461 | best_block = -1; |
| 462 | if (*old_sector != -1) |
| 463 | old_sector_block = *old_sector / part->block_size; |
| 464 | else |
| 465 | old_sector_block = -1; |
| 466 | |
| 467 | for (block=0; block<part->total_blocks; block++) { |
| 468 | int this_score; |
| 469 | |
| 470 | if (block == part->reserved_block) |
| 471 | continue; |
| 472 | |
| 473 | /* |
| 474 | * Postpone reclaiming if there is a free sector as |
| 475 | * more removed sectors is more efficient (have to move |
| 476 | * less). |
| 477 | */ |
Thomas Gleixner | 97894cd | 2005-11-07 11:15:26 +0000 | [diff] [blame] | 478 | if (part->blocks[block].free_sectors) |
Sean Young | e27a996 | 2005-06-16 09:49:33 +0100 | [diff] [blame] | 479 | return 0; |
| 480 | |
| 481 | this_score = part->blocks[block].used_sectors; |
| 482 | |
Thomas Gleixner | 97894cd | 2005-11-07 11:15:26 +0000 | [diff] [blame] | 483 | if (block == old_sector_block) |
Sean Young | e27a996 | 2005-06-16 09:49:33 +0100 | [diff] [blame] | 484 | this_score--; |
| 485 | else { |
| 486 | /* no point in moving a full block */ |
Thomas Gleixner | 97894cd | 2005-11-07 11:15:26 +0000 | [diff] [blame] | 487 | if (part->blocks[block].used_sectors == |
Sean Young | e27a996 | 2005-06-16 09:49:33 +0100 | [diff] [blame] | 488 | part->data_sectors_per_block) |
| 489 | continue; |
| 490 | } |
| 491 | |
| 492 | this_score += part->blocks[block].erases; |
| 493 | |
| 494 | if (this_score < score) { |
| 495 | best_block = block; |
| 496 | score = this_score; |
| 497 | } |
| 498 | } |
| 499 | |
| 500 | if (best_block == -1) |
| 501 | return -ENOSPC; |
| 502 | |
| 503 | part->current_block = -1; |
| 504 | part->reserved_block = best_block; |
| 505 | |
| 506 | pr_debug("reclaim_block: reclaiming block #%d with %d used " |
| 507 | "%d free sectors\n", best_block, |
| 508 | part->blocks[best_block].used_sectors, |
| 509 | part->blocks[best_block].free_sectors); |
| 510 | |
| 511 | if (part->blocks[best_block].used_sectors) |
| 512 | rc = move_block_contents(part, best_block, old_sector); |
| 513 | else |
| 514 | rc = erase_block(part, best_block); |
| 515 | |
| 516 | return rc; |
| 517 | } |
| 518 | |
| 519 | /* |
| 520 | * IMPROVE: It would be best to choose the block with the most deleted sectors, |
| 521 | * because if we fill that one up first it'll have the most chance of having |
| 522 | * the least live sectors at reclaim. |
| 523 | */ |
| 524 | static int find_free_block(const struct partition *part) |
| 525 | { |
| 526 | int block, stop; |
| 527 | |
| 528 | block = part->current_block == -1 ? |
| 529 | jiffies % part->total_blocks : part->current_block; |
| 530 | stop = block; |
| 531 | |
| 532 | do { |
Thomas Gleixner | 97894cd | 2005-11-07 11:15:26 +0000 | [diff] [blame] | 533 | if (part->blocks[block].free_sectors && |
Sean Young | e27a996 | 2005-06-16 09:49:33 +0100 | [diff] [blame] | 534 | block != part->reserved_block) |
| 535 | return block; |
| 536 | |
| 537 | if (++block >= part->total_blocks) |
| 538 | block = 0; |
| 539 | |
| 540 | } while (block != stop); |
| 541 | |
| 542 | return -1; |
| 543 | } |
| 544 | |
| 545 | static int find_writeable_block(struct partition *part, u_long *old_sector) |
| 546 | { |
| 547 | int rc, block; |
| 548 | size_t retlen; |
| 549 | |
| 550 | block = find_free_block(part); |
| 551 | |
| 552 | if (block == -1) { |
| 553 | if (!part->is_reclaiming) { |
| 554 | rc = reclaim_block(part, old_sector); |
| 555 | if (rc) |
| 556 | goto err; |
| 557 | |
| 558 | block = find_free_block(part); |
| 559 | } |
| 560 | |
| 561 | if (block == -1) { |
| 562 | rc = -ENOSPC; |
| 563 | goto err; |
| 564 | } |
| 565 | } |
| 566 | |
Thomas Gleixner | 97894cd | 2005-11-07 11:15:26 +0000 | [diff] [blame] | 567 | rc = part->mbd.mtd->read(part->mbd.mtd, part->blocks[block].offset, |
Sean Young | e27a996 | 2005-06-16 09:49:33 +0100 | [diff] [blame] | 568 | part->header_size, &retlen, (u_char*)part->header_cache); |
| 569 | |
| 570 | if (!rc && retlen != part->header_size) |
| 571 | rc = -EIO; |
| 572 | |
| 573 | if (rc) { |
| 574 | printk(KERN_NOTICE PREFIX "'%s': unable to read header at " |
Thomas Gleixner | 97894cd | 2005-11-07 11:15:26 +0000 | [diff] [blame] | 575 | "0x%lx\n", part->mbd.mtd->name, |
Sean Young | e27a996 | 2005-06-16 09:49:33 +0100 | [diff] [blame] | 576 | part->blocks[block].offset); |
| 577 | goto err; |
| 578 | } |
| 579 | |
| 580 | part->current_block = block; |
| 581 | |
| 582 | err: |
| 583 | return rc; |
Thomas Gleixner | 97894cd | 2005-11-07 11:15:26 +0000 | [diff] [blame] | 584 | } |
Sean Young | e27a996 | 2005-06-16 09:49:33 +0100 | [diff] [blame] | 585 | |
| 586 | static int mark_sector_deleted(struct partition *part, u_long old_addr) |
| 587 | { |
| 588 | int block, offset, rc; |
| 589 | u_long addr; |
| 590 | size_t retlen; |
| 591 | u16 del = const_cpu_to_le16(SECTOR_DELETED); |
| 592 | |
| 593 | block = old_addr / part->block_size; |
Thomas Gleixner | 97894cd | 2005-11-07 11:15:26 +0000 | [diff] [blame] | 594 | offset = (old_addr % part->block_size) / SECTOR_SIZE - |
Sean Young | e27a996 | 2005-06-16 09:49:33 +0100 | [diff] [blame] | 595 | part->header_sectors_per_block; |
| 596 | |
| 597 | addr = part->blocks[block].offset + |
| 598 | (HEADER_MAP_OFFSET + offset) * sizeof(u16); |
| 599 | rc = part->mbd.mtd->write(part->mbd.mtd, addr, |
| 600 | sizeof(del), &retlen, (u_char*)&del); |
| 601 | |
| 602 | if (!rc && retlen != sizeof(del)) |
| 603 | rc = -EIO; |
| 604 | |
| 605 | if (rc) { |
| 606 | printk(KERN_WARNING PREFIX "error writing '%s' at " |
| 607 | "0x%lx\n", part->mbd.mtd->name, addr); |
Thomas Gleixner | 97894cd | 2005-11-07 11:15:26 +0000 | [diff] [blame] | 608 | if (rc) |
Sean Young | e27a996 | 2005-06-16 09:49:33 +0100 | [diff] [blame] | 609 | goto err; |
| 610 | } |
| 611 | if (block == part->current_block) |
| 612 | part->header_cache[offset + HEADER_MAP_OFFSET] = del; |
| 613 | |
| 614 | part->blocks[block].used_sectors--; |
| 615 | |
| 616 | if (!part->blocks[block].used_sectors && |
| 617 | !part->blocks[block].free_sectors) |
| 618 | rc = erase_block(part, block); |
| 619 | |
| 620 | err: |
| 621 | return rc; |
| 622 | } |
| 623 | |
| 624 | static int find_free_sector(const struct partition *part, const struct block *block) |
| 625 | { |
| 626 | int i, stop; |
| 627 | |
| 628 | i = stop = part->data_sectors_per_block - block->free_sectors; |
| 629 | |
| 630 | do { |
Thomas Gleixner | 97894cd | 2005-11-07 11:15:26 +0000 | [diff] [blame] | 631 | if (le16_to_cpu(part->header_cache[HEADER_MAP_OFFSET + i]) |
Sean Young | e27a996 | 2005-06-16 09:49:33 +0100 | [diff] [blame] | 632 | == SECTOR_FREE) |
| 633 | return i; |
| 634 | |
| 635 | if (++i == part->data_sectors_per_block) |
| 636 | i = 0; |
| 637 | } |
| 638 | while(i != stop); |
| 639 | |
| 640 | return -1; |
| 641 | } |
| 642 | |
| 643 | static int do_writesect(struct mtd_blktrans_dev *dev, u_long sector, char *buf, ulong *old_addr) |
| 644 | { |
| 645 | struct partition *part = (struct partition*)dev; |
| 646 | struct block *block; |
| 647 | u_long addr; |
| 648 | int i; |
| 649 | int rc; |
| 650 | size_t retlen; |
| 651 | u16 entry; |
| 652 | |
| 653 | if (part->current_block == -1 || |
| 654 | !part->blocks[part->current_block].free_sectors) { |
| 655 | |
| 656 | rc = find_writeable_block(part, old_addr); |
Thomas Gleixner | 97894cd | 2005-11-07 11:15:26 +0000 | [diff] [blame] | 657 | if (rc) |
Sean Young | e27a996 | 2005-06-16 09:49:33 +0100 | [diff] [blame] | 658 | goto err; |
| 659 | } |
| 660 | |
| 661 | block = &part->blocks[part->current_block]; |
| 662 | |
| 663 | i = find_free_sector(part, block); |
| 664 | |
| 665 | if (i < 0) { |
| 666 | rc = -ENOSPC; |
| 667 | goto err; |
| 668 | } |
Thomas Gleixner | 97894cd | 2005-11-07 11:15:26 +0000 | [diff] [blame] | 669 | |
| 670 | addr = (i + part->header_sectors_per_block) * SECTOR_SIZE + |
Sean Young | e27a996 | 2005-06-16 09:49:33 +0100 | [diff] [blame] | 671 | block->offset; |
Thomas Gleixner | 97894cd | 2005-11-07 11:15:26 +0000 | [diff] [blame] | 672 | rc = part->mbd.mtd->write(part->mbd.mtd, |
Sean Young | e27a996 | 2005-06-16 09:49:33 +0100 | [diff] [blame] | 673 | addr, SECTOR_SIZE, &retlen, (u_char*)buf); |
| 674 | |
| 675 | if (!rc && retlen != SECTOR_SIZE) |
| 676 | rc = -EIO; |
| 677 | |
| 678 | if (rc) { |
| 679 | printk(KERN_WARNING PREFIX "error writing '%s' at 0x%lx\n", |
| 680 | part->mbd.mtd->name, addr); |
Thomas Gleixner | 97894cd | 2005-11-07 11:15:26 +0000 | [diff] [blame] | 681 | if (rc) |
Sean Young | e27a996 | 2005-06-16 09:49:33 +0100 | [diff] [blame] | 682 | goto err; |
| 683 | } |
| 684 | |
| 685 | part->sector_map[sector] = addr; |
| 686 | |
| 687 | entry = cpu_to_le16(sector == 0 ? SECTOR_ZERO : sector); |
| 688 | |
| 689 | part->header_cache[i + HEADER_MAP_OFFSET] = entry; |
| 690 | |
| 691 | addr = block->offset + (HEADER_MAP_OFFSET + i) * sizeof(u16); |
| 692 | rc = part->mbd.mtd->write(part->mbd.mtd, addr, |
| 693 | sizeof(entry), &retlen, (u_char*)&entry); |
| 694 | |
| 695 | if (!rc && retlen != sizeof(entry)) |
| 696 | rc = -EIO; |
| 697 | |
| 698 | if (rc) { |
| 699 | printk(KERN_WARNING PREFIX "error writing '%s' at 0x%lx\n", |
| 700 | part->mbd.mtd->name, addr); |
Thomas Gleixner | 97894cd | 2005-11-07 11:15:26 +0000 | [diff] [blame] | 701 | if (rc) |
Sean Young | e27a996 | 2005-06-16 09:49:33 +0100 | [diff] [blame] | 702 | goto err; |
| 703 | } |
| 704 | block->used_sectors++; |
| 705 | block->free_sectors--; |
| 706 | |
| 707 | err: |
| 708 | return rc; |
| 709 | } |
| 710 | |
| 711 | static int rfd_ftl_writesect(struct mtd_blktrans_dev *dev, u_long sector, char *buf) |
| 712 | { |
| 713 | struct partition *part = (struct partition*)dev; |
| 714 | u_long old_addr; |
| 715 | int i; |
| 716 | int rc = 0; |
| 717 | |
| 718 | pr_debug("rfd_ftl_writesect(sector=0x%lx)\n", sector); |
| 719 | |
| 720 | if (part->reserved_block == -1) { |
| 721 | rc = -EACCES; |
| 722 | goto err; |
| 723 | } |
| 724 | |
| 725 | if (sector >= part->sector_count) { |
| 726 | rc = -EIO; |
| 727 | goto err; |
| 728 | } |
| 729 | |
| 730 | old_addr = part->sector_map[sector]; |
| 731 | |
| 732 | for (i=0; i<SECTOR_SIZE; i++) { |
| 733 | if (!buf[i]) |
| 734 | continue; |
| 735 | |
| 736 | rc = do_writesect(dev, sector, buf, &old_addr); |
| 737 | if (rc) |
| 738 | goto err; |
| 739 | break; |
| 740 | } |
| 741 | |
Thomas Gleixner | 97894cd | 2005-11-07 11:15:26 +0000 | [diff] [blame] | 742 | if (i == SECTOR_SIZE) |
Sean Young | e27a996 | 2005-06-16 09:49:33 +0100 | [diff] [blame] | 743 | part->sector_map[sector] = -1; |
| 744 | |
| 745 | if (old_addr != -1) |
| 746 | rc = mark_sector_deleted(part, old_addr); |
| 747 | |
| 748 | err: |
| 749 | return rc; |
| 750 | } |
| 751 | |
| 752 | static int rfd_ftl_getgeo(struct mtd_blktrans_dev *dev, struct hd_geometry *geo) |
| 753 | { |
| 754 | struct partition *part = (struct partition*)dev; |
| 755 | |
| 756 | geo->heads = 1; |
| 757 | geo->sectors = SECTORS_PER_TRACK; |
| 758 | geo->cylinders = part->cylinders; |
| 759 | |
| 760 | return 0; |
| 761 | } |
| 762 | |
| 763 | static void rfd_ftl_add_mtd(struct mtd_blktrans_ops *tr, struct mtd_info *mtd) |
| 764 | { |
| 765 | struct partition *part; |
| 766 | |
| 767 | if (mtd->type != MTD_NORFLASH) |
| 768 | return; |
| 769 | |
| 770 | part = kcalloc(1, sizeof(struct partition), GFP_KERNEL); |
| 771 | if (!part) |
| 772 | return; |
| 773 | |
| 774 | part->mbd.mtd = mtd; |
| 775 | |
| 776 | if (block_size) |
| 777 | part->block_size = block_size; |
| 778 | else { |
| 779 | if (!mtd->erasesize) { |
| 780 | printk(KERN_NOTICE PREFIX "please provide block_size"); |
| 781 | return; |
| 782 | } |
| 783 | else |
| 784 | part->block_size = mtd->erasesize; |
| 785 | } |
| 786 | |
| 787 | if (scan_header(part) == 0) { |
| 788 | part->mbd.size = part->sector_count; |
| 789 | part->mbd.blksize = SECTOR_SIZE; |
| 790 | part->mbd.tr = tr; |
| 791 | part->mbd.devnum = -1; |
| 792 | if (!(mtd->flags & MTD_WRITEABLE)) |
| 793 | part->mbd.readonly = 1; |
| 794 | else if (part->errors) { |
| 795 | printk(KERN_NOTICE PREFIX "'%s': errors found, " |
| 796 | "setting read-only", mtd->name); |
| 797 | part->mbd.readonly = 1; |
| 798 | } |
| 799 | |
| 800 | printk(KERN_INFO PREFIX "name: '%s' type: %d flags %x\n", |
| 801 | mtd->name, mtd->type, mtd->flags); |
| 802 | |
| 803 | if (!add_mtd_blktrans_dev((void*)part)) |
| 804 | return; |
Thomas Gleixner | 97894cd | 2005-11-07 11:15:26 +0000 | [diff] [blame] | 805 | } |
Sean Young | e27a996 | 2005-06-16 09:49:33 +0100 | [diff] [blame] | 806 | |
| 807 | kfree(part); |
| 808 | } |
| 809 | |
| 810 | static void rfd_ftl_remove_dev(struct mtd_blktrans_dev *dev) |
| 811 | { |
| 812 | struct partition *part = (struct partition*)dev; |
| 813 | int i; |
| 814 | |
| 815 | for (i=0; i<part->total_blocks; i++) { |
| 816 | pr_debug("rfd_ftl_remove_dev:'%s': erase unit #%02d: %d erases\n", |
| 817 | part->mbd.mtd->name, i, part->blocks[i].erases); |
| 818 | } |
| 819 | |
| 820 | del_mtd_blktrans_dev(dev); |
| 821 | vfree(part->sector_map); |
| 822 | kfree(part->header_cache); |
| 823 | kfree(part->blocks); |
| 824 | kfree(part); |
| 825 | } |
| 826 | |
| 827 | struct mtd_blktrans_ops rfd_ftl_tr = { |
| 828 | .name = "rfd", |
| 829 | .major = RFD_FTL_MAJOR, |
| 830 | .part_bits = PART_BITS, |
| 831 | .readsect = rfd_ftl_readsect, |
Thomas Gleixner | 97894cd | 2005-11-07 11:15:26 +0000 | [diff] [blame] | 832 | .writesect = rfd_ftl_writesect, |
Sean Young | e27a996 | 2005-06-16 09:49:33 +0100 | [diff] [blame] | 833 | .getgeo = rfd_ftl_getgeo, |
| 834 | .add_mtd = rfd_ftl_add_mtd, |
| 835 | .remove_dev = rfd_ftl_remove_dev, |
| 836 | .owner = THIS_MODULE, |
| 837 | }; |
| 838 | |
| 839 | static int __init init_rfd_ftl(void) |
| 840 | { |
| 841 | return register_mtd_blktrans(&rfd_ftl_tr); |
| 842 | } |
| 843 | |
| 844 | static void __exit cleanup_rfd_ftl(void) |
| 845 | { |
| 846 | deregister_mtd_blktrans(&rfd_ftl_tr); |
| 847 | } |
| 848 | |
| 849 | module_init(init_rfd_ftl); |
| 850 | module_exit(cleanup_rfd_ftl); |
| 851 | |
| 852 | MODULE_LICENSE("GPL"); |
| 853 | MODULE_AUTHOR("Sean Young <sean@mess.org>"); |
| 854 | MODULE_DESCRIPTION("Support code for RFD Flash Translation Layer, " |
| 855 | "used by General Software's Embedded BIOS"); |
| 856 | |