IPoIB: fix error handling in ipoib_open
[linux-2.6/mini2440.git] / drivers / mtd / rfd_ftl.c
blob0ab8d29caeea07ecaf4e0434cfa7b2fd512496e7
1 /*
2 * rfd_ftl.c -- resident flash disk (flash translation layer)
4 * Copyright (C) 2005 Sean Young <sean@mess.org>
6 * $Id: rfd_ftl.c,v 1.5 2005/11/07 11:14:21 gleixner Exp $
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:
11 * http://www.gensw.com/pages/prod/bios/rfd.htm
13 * based on ftl.c
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>
21 #include <linux/jiffies.h>
23 #include <asm/types.h>
25 #define const_cpu_to_le16 __constant_cpu_to_le16
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");
31 #define PREFIX "rfd_ftl: "
33 /* Major device # for FTL device */
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
40 /* Maximum number of partitions in an FTL region */
41 #define PART_BITS 4
43 /* An erase unit should start with this value */
44 #define RFD_MAGIC 0x9193
46 /* the second value is 0xffff or 0xffc8; function unknown */
48 /* the third value is always 0xffff, ignored */
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
56 #define SECTOR_SIZE 512
58 #define SECTORS_PER_TRACK 63
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;
73 struct partition {
74 struct mtd_blktrans_dev mbd;
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 */
86 int is_reclaiming;
87 int cylinders;
88 int errors;
89 u_long *sector_map;
90 struct block *blocks;
93 static int rfd_ftl_writesect(struct mtd_blktrans_dev *dev, u_long sector, char *buf);
95 static int build_block_map(struct partition *part, int block_no)
97 struct block *block = &part->blocks[block_no];
98 int i;
100 block->offset = part->block_size * block_no;
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;
109 block->state = BLOCK_OK;
111 for (i=0; i<part->data_sectors_per_block; i++) {
112 u16 entry;
114 entry = le16_to_cpu(part->header_cache[HEADER_MAP_OFFSET + i]);
116 if (entry == SECTOR_DELETED)
117 continue;
119 if (entry == SECTOR_FREE) {
120 block->free_sectors++;
121 continue;
124 if (entry == SECTOR_ZERO)
125 entry = 0;
127 if (entry >= part->sector_count) {
128 printk(KERN_NOTICE PREFIX
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;
135 if (part->sector_map[entry] != -1) {
136 printk(KERN_NOTICE PREFIX
137 "'%s': more than one entry for sector %d\n",
138 part->mbd.mtd->name, entry);
139 part->errors = 1;
140 continue;
143 part->sector_map[entry] = block->offset +
144 (i + part->header_sectors_per_block) * SECTOR_SIZE;
146 block->used_sectors++;
149 if (block->free_sectors == part->data_sectors_per_block)
150 part->reserved_block = block_no;
152 return 0;
155 static int scan_header(struct partition *part)
157 int sectors_per_block;
158 int i, rc = -ENOMEM;
159 int blocks_found;
160 size_t retlen;
162 sectors_per_block = part->block_size / SECTOR_SIZE;
163 part->total_blocks = part->mbd.mtd->size / part->block_size;
165 if (part->total_blocks < 2)
166 return -ENOENT;
168 /* each erase block has three bytes header, followed by the map */
169 part->header_sectors_per_block =
170 ((HEADER_MAP_OFFSET + sectors_per_block) *
171 sizeof(u16) + SECTOR_SIZE - 1) / SECTOR_SIZE;
173 part->data_sectors_per_block = sectors_per_block -
174 part->header_sectors_per_block;
176 part->header_size = (HEADER_MAP_OFFSET +
177 part->data_sectors_per_block) * sizeof(u16);
179 part->cylinders = (part->data_sectors_per_block *
180 (part->total_blocks - 1) - 1) / SECTORS_PER_TRACK;
182 part->sector_count = part->cylinders * SECTORS_PER_TRACK;
184 part->current_block = -1;
185 part->reserved_block = -1;
186 part->is_reclaiming = 0;
188 part->header_cache = kmalloc(part->header_size, GFP_KERNEL);
189 if (!part->header_cache)
190 goto err;
192 part->blocks = kcalloc(part->total_blocks, sizeof(struct block),
193 GFP_KERNEL);
194 if (!part->blocks)
195 goto err;
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;
204 for (i=0; i<part->sector_count; i++)
205 part->sector_map[i] = -1;
207 for (i=0, blocks_found=0; i<part->total_blocks; i++) {
208 rc = part->mbd.mtd->read(part->mbd.mtd,
209 i * part->block_size, part->header_size,
210 &retlen, (u_char*)part->header_cache);
212 if (!rc && retlen != part->header_size)
213 rc = -EIO;
215 if (rc)
216 goto err;
218 if (!build_block_map(part, i))
219 blocks_found++;
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;
229 if (part->reserved_block == -1) {
230 printk(KERN_NOTICE PREFIX "'%s': no empty erase unit found\n",
231 part->mbd.mtd->name);
233 part->errors = 1;
236 return 0;
238 err:
239 vfree(part->sector_map);
240 kfree(part->header_cache);
241 kfree(part->blocks);
243 return rc;
246 static int rfd_ftl_readsect(struct mtd_blktrans_dev *dev, u_long sector, char *buf)
248 struct partition *part = (struct partition*)dev;
249 u_long addr;
250 size_t retlen;
251 int rc;
253 if (sector >= part->sector_count)
254 return -EIO;
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;
263 if (rc) {
264 printk(KERN_WARNING PREFIX "error reading '%s' at "
265 "0x%lx\n", part->mbd.mtd->name, addr);
266 return rc;
268 } else
269 memset(buf, 0, SECTOR_SIZE);
271 return 0;
274 static void erase_callback(struct erase_info *erase)
276 struct partition *part;
277 u16 magic;
278 int i, rc;
279 size_t retlen;
281 part = (struct partition*)erase->priv;
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;
290 if (erase->state != MTD_ERASE_DONE) {
291 printk(KERN_WARNING PREFIX "erase failed at 0x%x on '%s', "
292 "state %d\n", erase->addr,
293 part->mbd.mtd->name, erase->state);
295 part->blocks[i].state = BLOCK_FAILED;
296 part->blocks[i].free_sectors = 0;
297 part->blocks[i].used_sectors = 0;
299 kfree(erase);
301 return;
304 magic = const_cpu_to_le16(RFD_MAGIC);
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++;
311 rc = part->mbd.mtd->write(part->mbd.mtd,
312 part->blocks[i].offset, sizeof(magic), &retlen,
313 (u_char*)&magic);
315 if (!rc && retlen != sizeof(magic))
316 rc = -EIO;
318 if (rc) {
319 printk(KERN_NOTICE PREFIX "'%s': unable to write RFD "
320 "header at 0x%lx\n",
321 part->mbd.mtd->name,
322 part->blocks[i].offset);
323 part->blocks[i].state = BLOCK_FAILED;
325 else
326 part->blocks[i].state = BLOCK_OK;
328 kfree(erase);
331 static int erase_block(struct partition *part, int block)
333 struct erase_info *erase;
334 int rc = -ENOMEM;
336 erase = kmalloc(sizeof(struct erase_info), GFP_KERNEL);
337 if (!erase)
338 goto err;
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;
346 part->blocks[block].state = BLOCK_ERASING;
347 part->blocks[block].free_sectors = 0;
349 rc = part->mbd.mtd->erase(part->mbd.mtd, erase);
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);
358 err:
359 return rc;
362 static int move_block_contents(struct partition *part, int block_no, u_long *old_sector)
364 void *sector_data;
365 u16 *map;
366 size_t retlen;
367 int i, rc = -ENOMEM;
369 part->is_reclaiming = 1;
371 sector_data = kmalloc(SECTOR_SIZE, GFP_KERNEL);
372 if (!sector_data)
373 goto err3;
375 map = kmalloc(part->header_size, GFP_KERNEL);
376 if (!map)
377 goto err2;
379 rc = part->mbd.mtd->read(part->mbd.mtd,
380 part->blocks[block_no].offset, part->header_size,
381 &retlen, (u_char*)map);
383 if (!rc && retlen != part->header_size)
384 rc = -EIO;
386 if (rc) {
387 printk(KERN_NOTICE PREFIX "error reading '%s' at "
388 "0x%lx\n", part->mbd.mtd->name,
389 part->blocks[block_no].offset);
391 goto err;
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;
399 if (entry == SECTOR_FREE || entry == SECTOR_DELETED)
400 continue;
402 if (entry == SECTOR_ZERO)
403 entry = 0;
405 /* already warned about and ignored in build_block_map() */
406 if (entry >= part->sector_count)
407 continue;
409 addr = part->blocks[block_no].offset +
410 (i + part->header_sectors_per_block) * SECTOR_SIZE;
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;
418 continue;
420 rc = part->mbd.mtd->read(part->mbd.mtd, addr,
421 SECTOR_SIZE, &retlen, sector_data);
423 if (!rc && retlen != SECTOR_SIZE)
424 rc = -EIO;
426 if (rc) {
427 printk(KERN_NOTICE PREFIX "'%s': Unable to "
428 "read sector for relocation\n",
429 part->mbd.mtd->name);
431 goto err;
434 rc = rfd_ftl_writesect((struct mtd_blktrans_dev*)part,
435 entry, sector_data);
437 if (rc)
438 goto err;
441 err:
442 kfree(map);
443 err2:
444 kfree(sector_data);
445 err3:
446 part->is_reclaiming = 0;
448 return rc;
451 static int reclaim_block(struct partition *part, u_long *old_sector)
453 int block, best_block, score, old_sector_block;
454 int rc;
456 /* we have a race if sync doesn't exist */
457 if (part->mbd.mtd->sync)
458 part->mbd.mtd->sync(part->mbd.mtd);
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;
467 for (block=0; block<part->total_blocks; block++) {
468 int this_score;
470 if (block == part->reserved_block)
471 continue;
474 * Postpone reclaiming if there is a free sector as
475 * more removed sectors is more efficient (have to move
476 * less).
478 if (part->blocks[block].free_sectors)
479 return 0;
481 this_score = part->blocks[block].used_sectors;
483 if (block == old_sector_block)
484 this_score--;
485 else {
486 /* no point in moving a full block */
487 if (part->blocks[block].used_sectors ==
488 part->data_sectors_per_block)
489 continue;
492 this_score += part->blocks[block].erases;
494 if (this_score < score) {
495 best_block = block;
496 score = this_score;
500 if (best_block == -1)
501 return -ENOSPC;
503 part->current_block = -1;
504 part->reserved_block = best_block;
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);
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);
516 return rc;
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.
524 static int find_free_block(const struct partition *part)
526 int block, stop;
528 block = part->current_block == -1 ?
529 jiffies % part->total_blocks : part->current_block;
530 stop = block;
532 do {
533 if (part->blocks[block].free_sectors &&
534 block != part->reserved_block)
535 return block;
537 if (++block >= part->total_blocks)
538 block = 0;
540 } while (block != stop);
542 return -1;
545 static int find_writeable_block(struct partition *part, u_long *old_sector)
547 int rc, block;
548 size_t retlen;
550 block = find_free_block(part);
552 if (block == -1) {
553 if (!part->is_reclaiming) {
554 rc = reclaim_block(part, old_sector);
555 if (rc)
556 goto err;
558 block = find_free_block(part);
561 if (block == -1) {
562 rc = -ENOSPC;
563 goto err;
567 rc = part->mbd.mtd->read(part->mbd.mtd, part->blocks[block].offset,
568 part->header_size, &retlen, (u_char*)part->header_cache);
570 if (!rc && retlen != part->header_size)
571 rc = -EIO;
573 if (rc) {
574 printk(KERN_NOTICE PREFIX "'%s': unable to read header at "
575 "0x%lx\n", part->mbd.mtd->name,
576 part->blocks[block].offset);
577 goto err;
580 part->current_block = block;
582 err:
583 return rc;
586 static int mark_sector_deleted(struct partition *part, u_long old_addr)
588 int block, offset, rc;
589 u_long addr;
590 size_t retlen;
591 u16 del = const_cpu_to_le16(SECTOR_DELETED);
593 block = old_addr / part->block_size;
594 offset = (old_addr % part->block_size) / SECTOR_SIZE -
595 part->header_sectors_per_block;
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);
602 if (!rc && retlen != sizeof(del))
603 rc = -EIO;
605 if (rc) {
606 printk(KERN_WARNING PREFIX "error writing '%s' at "
607 "0x%lx\n", part->mbd.mtd->name, addr);
608 if (rc)
609 goto err;
611 if (block == part->current_block)
612 part->header_cache[offset + HEADER_MAP_OFFSET] = del;
614 part->blocks[block].used_sectors--;
616 if (!part->blocks[block].used_sectors &&
617 !part->blocks[block].free_sectors)
618 rc = erase_block(part, block);
620 err:
621 return rc;
624 static int find_free_sector(const struct partition *part, const struct block *block)
626 int i, stop;
628 i = stop = part->data_sectors_per_block - block->free_sectors;
630 do {
631 if (le16_to_cpu(part->header_cache[HEADER_MAP_OFFSET + i])
632 == SECTOR_FREE)
633 return i;
635 if (++i == part->data_sectors_per_block)
636 i = 0;
638 while(i != stop);
640 return -1;
643 static int do_writesect(struct mtd_blktrans_dev *dev, u_long sector, char *buf, ulong *old_addr)
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;
653 if (part->current_block == -1 ||
654 !part->blocks[part->current_block].free_sectors) {
656 rc = find_writeable_block(part, old_addr);
657 if (rc)
658 goto err;
661 block = &part->blocks[part->current_block];
663 i = find_free_sector(part, block);
665 if (i < 0) {
666 rc = -ENOSPC;
667 goto err;
670 addr = (i + part->header_sectors_per_block) * SECTOR_SIZE +
671 block->offset;
672 rc = part->mbd.mtd->write(part->mbd.mtd,
673 addr, SECTOR_SIZE, &retlen, (u_char*)buf);
675 if (!rc && retlen != SECTOR_SIZE)
676 rc = -EIO;
678 if (rc) {
679 printk(KERN_WARNING PREFIX "error writing '%s' at 0x%lx\n",
680 part->mbd.mtd->name, addr);
681 if (rc)
682 goto err;
685 part->sector_map[sector] = addr;
687 entry = cpu_to_le16(sector == 0 ? SECTOR_ZERO : sector);
689 part->header_cache[i + HEADER_MAP_OFFSET] = entry;
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);
695 if (!rc && retlen != sizeof(entry))
696 rc = -EIO;
698 if (rc) {
699 printk(KERN_WARNING PREFIX "error writing '%s' at 0x%lx\n",
700 part->mbd.mtd->name, addr);
701 if (rc)
702 goto err;
704 block->used_sectors++;
705 block->free_sectors--;
707 err:
708 return rc;
711 static int rfd_ftl_writesect(struct mtd_blktrans_dev *dev, u_long sector, char *buf)
713 struct partition *part = (struct partition*)dev;
714 u_long old_addr;
715 int i;
716 int rc = 0;
718 pr_debug("rfd_ftl_writesect(sector=0x%lx)\n", sector);
720 if (part->reserved_block == -1) {
721 rc = -EACCES;
722 goto err;
725 if (sector >= part->sector_count) {
726 rc = -EIO;
727 goto err;
730 old_addr = part->sector_map[sector];
732 for (i=0; i<SECTOR_SIZE; i++) {
733 if (!buf[i])
734 continue;
736 rc = do_writesect(dev, sector, buf, &old_addr);
737 if (rc)
738 goto err;
739 break;
742 if (i == SECTOR_SIZE)
743 part->sector_map[sector] = -1;
745 if (old_addr != -1)
746 rc = mark_sector_deleted(part, old_addr);
748 err:
749 return rc;
752 static int rfd_ftl_getgeo(struct mtd_blktrans_dev *dev, struct hd_geometry *geo)
754 struct partition *part = (struct partition*)dev;
756 geo->heads = 1;
757 geo->sectors = SECTORS_PER_TRACK;
758 geo->cylinders = part->cylinders;
760 return 0;
763 static void rfd_ftl_add_mtd(struct mtd_blktrans_ops *tr, struct mtd_info *mtd)
765 struct partition *part;
767 if (mtd->type != MTD_NORFLASH)
768 return;
770 part = kcalloc(1, sizeof(struct partition), GFP_KERNEL);
771 if (!part)
772 return;
774 part->mbd.mtd = mtd;
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;
783 else
784 part->block_size = mtd->erasesize;
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;
800 printk(KERN_INFO PREFIX "name: '%s' type: %d flags %x\n",
801 mtd->name, mtd->type, mtd->flags);
803 if (!add_mtd_blktrans_dev((void*)part))
804 return;
807 kfree(part);
810 static void rfd_ftl_remove_dev(struct mtd_blktrans_dev *dev)
812 struct partition *part = (struct partition*)dev;
813 int i;
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);
820 del_mtd_blktrans_dev(dev);
821 vfree(part->sector_map);
822 kfree(part->header_cache);
823 kfree(part->blocks);
824 kfree(part);
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,
832 .writesect = rfd_ftl_writesect,
833 .getgeo = rfd_ftl_getgeo,
834 .add_mtd = rfd_ftl_add_mtd,
835 .remove_dev = rfd_ftl_remove_dev,
836 .owner = THIS_MODULE,
839 static int __init init_rfd_ftl(void)
841 return register_mtd_blktrans(&rfd_ftl_tr);
844 static void __exit cleanup_rfd_ftl(void)
846 deregister_mtd_blktrans(&rfd_ftl_tr);
849 module_init(init_rfd_ftl);
850 module_exit(cleanup_rfd_ftl);
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");