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