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