2 * rfd_ftl.c -- resident flash disk (flash translation layer)
4 * Copyright (C) 2005 Sean Young <sean@mess.org>
6 * This type of flash translation layer (FTL) is used by the Embedded BIOS
7 * by General Software. It is known as the Resident Flash Disk (RFD), see:
9 * http://www.gensw.com/pages/prod/bios/rfd.htm
14 #include <linux/hdreg.h>
15 #include <linux/init.h>
16 #include <linux/mtd/blktrans.h>
17 #include <linux/mtd/mtd.h>
18 #include <linux/vmalloc.h>
19 #include <linux/slab.h>
20 #include <linux/jiffies.h>
22 #include <asm/types.h>
24 #define const_cpu_to_le16 __constant_cpu_to_le16
26 static int block_size = 0;
27 module_param(block_size, int, 0);
28 MODULE_PARM_DESC(block_size, "Block size to use by RFD, defaults to erase unit size");
30 #define PREFIX "rfd_ftl: "
32 /* This major has been assigned by device@lanana.org */
34 #define RFD_FTL_MAJOR 256
37 /* Maximum number of partitions in an FTL region */
40 /* An erase unit should start with this value */
41 #define RFD_MAGIC 0x9193
43 /* the second value is 0xffff or 0xffc8; function unknown */
45 /* the third value is always 0xffff, ignored */
47 /* next is an array of mapping for each corresponding sector */
48 #define HEADER_MAP_OFFSET 3
49 #define SECTOR_DELETED 0x0000
50 #define SECTOR_ZERO 0xfffe
51 #define SECTOR_FREE 0xffff
53 #define SECTOR_SIZE 512
55 #define SECTORS_PER_TRACK 63
72 struct mtd_blktrans_dev mbd;
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 */
91 static int rfd_ftl_writesect(struct mtd_blktrans_dev *dev, u_long sector, char *buf);
93 static int build_block_map(struct partition *part, int block_no)
95 struct block *block = &part->blocks[block_no];
98 block->offset = part->block_size * block_no;
100 if (le16_to_cpu(part->header_cache[0]) != RFD_MAGIC) {
101 block->state = BLOCK_UNUSED;
105 block->state = BLOCK_OK;
107 for (i=0; i<part->data_sectors_per_block; i++) {
110 entry = le16_to_cpu(part->header_cache[HEADER_MAP_OFFSET + i]);
112 if (entry == SECTOR_DELETED)
115 if (entry == SECTOR_FREE) {
116 block->free_sectors++;
120 if (entry == SECTOR_ZERO)
123 if (entry >= part->sector_count) {
124 printk(KERN_WARNING PREFIX
125 "'%s': unit #%d: entry %d corrupt, "
126 "sector %d out of range\n",
127 part->mbd.mtd->name, block_no, i, entry);
131 if (part->sector_map[entry] != -1) {
132 printk(KERN_WARNING PREFIX
133 "'%s': more than one entry for sector %d\n",
134 part->mbd.mtd->name, entry);
139 part->sector_map[entry] = block->offset +
140 (i + part->header_sectors_per_block) * SECTOR_SIZE;
142 block->used_sectors++;
145 if (block->free_sectors == part->data_sectors_per_block)
146 part->reserved_block = block_no;
151 static int scan_header(struct partition *part)
153 int sectors_per_block;
158 sectors_per_block = part->block_size / SECTOR_SIZE;
159 part->total_blocks = (u32)part->mbd.mtd->size / part->block_size;
161 if (part->total_blocks < 2)
164 /* each erase block has three bytes header, followed by the map */
165 part->header_sectors_per_block =
166 ((HEADER_MAP_OFFSET + sectors_per_block) *
167 sizeof(u16) + SECTOR_SIZE - 1) / SECTOR_SIZE;
169 part->data_sectors_per_block = sectors_per_block -
170 part->header_sectors_per_block;
172 part->header_size = (HEADER_MAP_OFFSET +
173 part->data_sectors_per_block) * sizeof(u16);
175 part->cylinders = (part->data_sectors_per_block *
176 (part->total_blocks - 1) - 1) / SECTORS_PER_TRACK;
178 part->sector_count = part->cylinders * SECTORS_PER_TRACK;
180 part->current_block = -1;
181 part->reserved_block = -1;
182 part->is_reclaiming = 0;
184 part->header_cache = kmalloc(part->header_size, GFP_KERNEL);
185 if (!part->header_cache)
188 part->blocks = kcalloc(part->total_blocks, sizeof(struct block),
193 part->sector_map = vmalloc(part->sector_count * sizeof(u_long));
194 if (!part->sector_map) {
195 printk(KERN_ERR PREFIX "'%s': unable to allocate memory for "
196 "sector map", part->mbd.mtd->name);
200 for (i=0; i<part->sector_count; i++)
201 part->sector_map[i] = -1;
203 for (i=0, blocks_found=0; i<part->total_blocks; i++) {
204 rc = part->mbd.mtd->read(part->mbd.mtd,
205 i * part->block_size, part->header_size,
206 &retlen, (u_char*)part->header_cache);
208 if (!rc && retlen != part->header_size)
214 if (!build_block_map(part, i))
218 if (blocks_found == 0) {
219 printk(KERN_NOTICE PREFIX "no RFD magic found in '%s'\n",
220 part->mbd.mtd->name);
225 if (part->reserved_block == -1) {
226 printk(KERN_WARNING PREFIX "'%s': no empty erase unit found\n",
227 part->mbd.mtd->name);
235 vfree(part->sector_map);
236 kfree(part->header_cache);
242 static int rfd_ftl_readsect(struct mtd_blktrans_dev *dev, u_long sector, char *buf)
244 struct partition *part = (struct partition*)dev;
249 if (sector >= part->sector_count)
252 addr = part->sector_map[sector];
254 rc = part->mbd.mtd->read(part->mbd.mtd, addr, SECTOR_SIZE,
255 &retlen, (u_char*)buf);
256 if (!rc && retlen != SECTOR_SIZE)
260 printk(KERN_WARNING PREFIX "error reading '%s' at "
261 "0x%lx\n", part->mbd.mtd->name, addr);
265 memset(buf, 0, SECTOR_SIZE);
270 static void erase_callback(struct erase_info *erase)
272 struct partition *part;
277 part = (struct partition*)erase->priv;
279 i = (u32)erase->addr / part->block_size;
280 if (i >= part->total_blocks || part->blocks[i].offset != erase->addr ||
281 erase->addr > UINT_MAX) {
282 printk(KERN_ERR PREFIX "erase callback for unknown offset %llx "
283 "on '%s'\n", (unsigned long long)erase->addr, part->mbd.mtd->name);
287 if (erase->state != MTD_ERASE_DONE) {
288 printk(KERN_WARNING PREFIX "erase failed at 0x%llx on '%s', "
289 "state %d\n", (unsigned long long)erase->addr,
290 part->mbd.mtd->name, erase->state);
292 part->blocks[i].state = BLOCK_FAILED;
293 part->blocks[i].free_sectors = 0;
294 part->blocks[i].used_sectors = 0;
301 magic = const_cpu_to_le16(RFD_MAGIC);
303 part->blocks[i].state = BLOCK_ERASED;
304 part->blocks[i].free_sectors = part->data_sectors_per_block;
305 part->blocks[i].used_sectors = 0;
306 part->blocks[i].erases++;
308 rc = part->mbd.mtd->write(part->mbd.mtd,
309 part->blocks[i].offset, sizeof(magic), &retlen,
312 if (!rc && retlen != sizeof(magic))
316 printk(KERN_ERR PREFIX "'%s': unable to write RFD "
319 part->blocks[i].offset);
320 part->blocks[i].state = BLOCK_FAILED;
323 part->blocks[i].state = BLOCK_OK;
328 static int erase_block(struct partition *part, int block)
330 struct erase_info *erase;
333 erase = kmalloc(sizeof(struct erase_info), GFP_KERNEL);
337 erase->mtd = part->mbd.mtd;
338 erase->callback = erase_callback;
339 erase->addr = part->blocks[block].offset;
340 erase->len = part->block_size;
341 erase->priv = (u_long)part;
343 part->blocks[block].state = BLOCK_ERASING;
344 part->blocks[block].free_sectors = 0;
346 rc = part->mbd.mtd->erase(part->mbd.mtd, erase);
349 printk(KERN_ERR PREFIX "erase of region %llx,%llx on '%s' "
350 "failed\n", (unsigned long long)erase->addr,
351 (unsigned long long)erase->len, part->mbd.mtd->name);
359 static int move_block_contents(struct partition *part, int block_no, u_long *old_sector)
366 part->is_reclaiming = 1;
368 sector_data = kmalloc(SECTOR_SIZE, GFP_KERNEL);
372 map = kmalloc(part->header_size, GFP_KERNEL);
376 rc = part->mbd.mtd->read(part->mbd.mtd,
377 part->blocks[block_no].offset, part->header_size,
378 &retlen, (u_char*)map);
380 if (!rc && retlen != part->header_size)
384 printk(KERN_ERR PREFIX "error reading '%s' at "
385 "0x%lx\n", part->mbd.mtd->name,
386 part->blocks[block_no].offset);
391 for (i=0; i<part->data_sectors_per_block; i++) {
392 u16 entry = le16_to_cpu(map[HEADER_MAP_OFFSET + i]);
396 if (entry == SECTOR_FREE || entry == SECTOR_DELETED)
399 if (entry == SECTOR_ZERO)
402 /* already warned about and ignored in build_block_map() */
403 if (entry >= part->sector_count)
406 addr = part->blocks[block_no].offset +
407 (i + part->header_sectors_per_block) * SECTOR_SIZE;
409 if (*old_sector == addr) {
411 if (!part->blocks[block_no].used_sectors--) {
412 rc = erase_block(part, block_no);
417 rc = part->mbd.mtd->read(part->mbd.mtd, addr,
418 SECTOR_SIZE, &retlen, sector_data);
420 if (!rc && retlen != SECTOR_SIZE)
424 printk(KERN_ERR PREFIX "'%s': Unable to "
425 "read sector for relocation\n",
426 part->mbd.mtd->name);
431 rc = rfd_ftl_writesect((struct mtd_blktrans_dev*)part,
443 part->is_reclaiming = 0;
448 static int reclaim_block(struct partition *part, u_long *old_sector)
450 int block, best_block, score, old_sector_block;
453 /* we have a race if sync doesn't exist */
454 if (part->mbd.mtd->sync)
455 part->mbd.mtd->sync(part->mbd.mtd);
457 score = 0x7fffffff; /* MAX_INT */
459 if (*old_sector != -1)
460 old_sector_block = *old_sector / part->block_size;
462 old_sector_block = -1;
464 for (block=0; block<part->total_blocks; block++) {
467 if (block == part->reserved_block)
471 * Postpone reclaiming if there is a free sector as
472 * more removed sectors is more efficient (have to move
475 if (part->blocks[block].free_sectors)
478 this_score = part->blocks[block].used_sectors;
480 if (block == old_sector_block)
483 /* no point in moving a full block */
484 if (part->blocks[block].used_sectors ==
485 part->data_sectors_per_block)
489 this_score += part->blocks[block].erases;
491 if (this_score < score) {
497 if (best_block == -1)
500 part->current_block = -1;
501 part->reserved_block = best_block;
503 pr_debug("reclaim_block: reclaiming block #%d with %d used "
504 "%d free sectors\n", best_block,
505 part->blocks[best_block].used_sectors,
506 part->blocks[best_block].free_sectors);
508 if (part->blocks[best_block].used_sectors)
509 rc = move_block_contents(part, best_block, old_sector);
511 rc = erase_block(part, best_block);
517 * IMPROVE: It would be best to choose the block with the most deleted sectors,
518 * because if we fill that one up first it'll have the most chance of having
519 * the least live sectors at reclaim.
521 static int find_free_block(struct partition *part)
525 block = part->current_block == -1 ?
526 jiffies % part->total_blocks : part->current_block;
530 if (part->blocks[block].free_sectors &&
531 block != part->reserved_block)
534 if (part->blocks[block].state == BLOCK_UNUSED)
535 erase_block(part, block);
537 if (++block >= part->total_blocks)
540 } while (block != stop);
545 static int find_writable_block(struct partition *part, u_long *old_sector)
550 block = find_free_block(part);
553 if (!part->is_reclaiming) {
554 rc = reclaim_block(part, old_sector);
558 block = find_free_block(part);
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)
574 printk(KERN_ERR PREFIX "'%s': unable to read header at "
575 "0x%lx\n", part->mbd.mtd->name,
576 part->blocks[block].offset);
580 part->current_block = block;
586 static int mark_sector_deleted(struct partition *part, u_long old_addr)
588 int block, offset, rc;
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))
606 printk(KERN_ERR PREFIX "error writing '%s' at "
607 "0x%lx\n", part->mbd.mtd->name, addr);
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);
624 static int find_free_sector(const struct partition *part, const struct block *block)
628 i = stop = part->data_sectors_per_block - block->free_sectors;
631 if (le16_to_cpu(part->header_cache[HEADER_MAP_OFFSET + i])
635 if (++i == part->data_sectors_per_block)
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;
653 if (part->current_block == -1 ||
654 !part->blocks[part->current_block].free_sectors) {
656 rc = find_writable_block(part, old_addr);
661 block = &part->blocks[part->current_block];
663 i = find_free_sector(part, block);
670 addr = (i + part->header_sectors_per_block) * SECTOR_SIZE +
672 rc = part->mbd.mtd->write(part->mbd.mtd,
673 addr, SECTOR_SIZE, &retlen, (u_char*)buf);
675 if (!rc && retlen != SECTOR_SIZE)
679 printk(KERN_ERR PREFIX "error writing '%s' at 0x%lx\n",
680 part->mbd.mtd->name, addr);
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))
699 printk(KERN_ERR PREFIX "error writing '%s' at 0x%lx\n",
700 part->mbd.mtd->name, addr);
704 block->used_sectors++;
705 block->free_sectors--;
711 static int rfd_ftl_writesect(struct mtd_blktrans_dev *dev, u_long sector, char *buf)
713 struct partition *part = (struct partition*)dev;
718 pr_debug("rfd_ftl_writesect(sector=0x%lx)\n", sector);
720 if (part->reserved_block == -1) {
725 if (sector >= part->sector_count) {
730 old_addr = part->sector_map[sector];
732 for (i=0; i<SECTOR_SIZE; i++) {
736 rc = do_writesect(dev, sector, buf, &old_addr);
742 if (i == SECTOR_SIZE)
743 part->sector_map[sector] = -1;
746 rc = mark_sector_deleted(part, old_addr);
752 static int rfd_ftl_getgeo(struct mtd_blktrans_dev *dev, struct hd_geometry *geo)
754 struct partition *part = (struct partition*)dev;
757 geo->sectors = SECTORS_PER_TRACK;
758 geo->cylinders = part->cylinders;
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 || mtd->size > UINT_MAX)
770 part = kzalloc(sizeof(struct partition), GFP_KERNEL);
777 part->block_size = block_size;
779 if (!mtd->erasesize) {
780 printk(KERN_WARNING PREFIX "please provide block_size");
783 part->block_size = mtd->erasesize;
786 if (scan_header(part) == 0) {
787 part->mbd.size = part->sector_count;
789 part->mbd.devnum = -1;
790 if (!(mtd->flags & MTD_WRITEABLE))
791 part->mbd.readonly = 1;
792 else if (part->errors) {
793 printk(KERN_WARNING PREFIX "'%s': errors found, "
794 "setting read-only\n", mtd->name);
795 part->mbd.readonly = 1;
798 printk(KERN_INFO PREFIX "name: '%s' type: %d flags %x\n",
799 mtd->name, mtd->type, mtd->flags);
801 if (!add_mtd_blktrans_dev((void*)part))
808 static void rfd_ftl_remove_dev(struct mtd_blktrans_dev *dev)
810 struct partition *part = (struct partition*)dev;
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);
818 del_mtd_blktrans_dev(dev);
819 vfree(part->sector_map);
820 kfree(part->header_cache);
825 static struct mtd_blktrans_ops rfd_ftl_tr = {
827 .major = RFD_FTL_MAJOR,
828 .part_bits = PART_BITS,
829 .blksize = SECTOR_SIZE,
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");