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 static int block_size
= 0;
25 module_param(block_size
, int, 0);
26 MODULE_PARM_DESC(block_size
, "Block size to use by RFD, defaults to erase unit size");
28 #define PREFIX "rfd_ftl: "
30 /* This major has been assigned by device@lanana.org */
32 #define RFD_FTL_MAJOR 256
35 /* Maximum number of partitions in an FTL region */
38 /* An erase unit should start with this value */
39 #define RFD_MAGIC 0x9193
41 /* the second value is 0xffff or 0xffc8; function unknown */
43 /* the third value is always 0xffff, ignored */
45 /* next is an array of mapping for each corresponding sector */
46 #define HEADER_MAP_OFFSET 3
47 #define SECTOR_DELETED 0x0000
48 #define SECTOR_ZERO 0xfffe
49 #define SECTOR_FREE 0xffff
51 #define SECTOR_SIZE 512
53 #define SECTORS_PER_TRACK 63
70 struct mtd_blktrans_dev mbd
;
72 u_int block_size
; /* size of erase unit */
73 u_int total_blocks
; /* number of erase units */
74 u_int header_sectors_per_block
; /* header sectors in erase unit */
75 u_int data_sectors_per_block
; /* data sectors in erase unit */
76 u_int sector_count
; /* sectors in translated disk */
77 u_int header_size
; /* bytes in header sector */
78 int reserved_block
; /* block next up for reclaim */
79 int current_block
; /* block to write to */
80 u16
*header_cache
; /* cached header */
89 static int rfd_ftl_writesect(struct mtd_blktrans_dev
*dev
, u_long sector
, char *buf
);
91 static int build_block_map(struct partition
*part
, int block_no
)
93 struct block
*block
= &part
->blocks
[block_no
];
96 block
->offset
= part
->block_size
* block_no
;
98 if (le16_to_cpu(part
->header_cache
[0]) != RFD_MAGIC
) {
99 block
->state
= BLOCK_UNUSED
;
103 block
->state
= BLOCK_OK
;
105 for (i
=0; i
<part
->data_sectors_per_block
; i
++) {
108 entry
= le16_to_cpu(part
->header_cache
[HEADER_MAP_OFFSET
+ i
]);
110 if (entry
== SECTOR_DELETED
)
113 if (entry
== SECTOR_FREE
) {
114 block
->free_sectors
++;
118 if (entry
== SECTOR_ZERO
)
121 if (entry
>= part
->sector_count
) {
122 printk(KERN_WARNING PREFIX
123 "'%s': unit #%d: entry %d corrupt, "
124 "sector %d out of range\n",
125 part
->mbd
.mtd
->name
, block_no
, i
, entry
);
129 if (part
->sector_map
[entry
] != -1) {
130 printk(KERN_WARNING PREFIX
131 "'%s': more than one entry for sector %d\n",
132 part
->mbd
.mtd
->name
, entry
);
137 part
->sector_map
[entry
] = block
->offset
+
138 (i
+ part
->header_sectors_per_block
) * SECTOR_SIZE
;
140 block
->used_sectors
++;
143 if (block
->free_sectors
== part
->data_sectors_per_block
)
144 part
->reserved_block
= block_no
;
149 static int scan_header(struct partition
*part
)
151 int sectors_per_block
;
156 sectors_per_block
= part
->block_size
/ SECTOR_SIZE
;
157 part
->total_blocks
= (u32
)part
->mbd
.mtd
->size
/ part
->block_size
;
159 if (part
->total_blocks
< 2)
162 /* each erase block has three bytes header, followed by the map */
163 part
->header_sectors_per_block
=
164 ((HEADER_MAP_OFFSET
+ sectors_per_block
) *
165 sizeof(u16
) + SECTOR_SIZE
- 1) / SECTOR_SIZE
;
167 part
->data_sectors_per_block
= sectors_per_block
-
168 part
->header_sectors_per_block
;
170 part
->header_size
= (HEADER_MAP_OFFSET
+
171 part
->data_sectors_per_block
) * sizeof(u16
);
173 part
->cylinders
= (part
->data_sectors_per_block
*
174 (part
->total_blocks
- 1) - 1) / SECTORS_PER_TRACK
;
176 part
->sector_count
= part
->cylinders
* SECTORS_PER_TRACK
;
178 part
->current_block
= -1;
179 part
->reserved_block
= -1;
180 part
->is_reclaiming
= 0;
182 part
->header_cache
= kmalloc(part
->header_size
, GFP_KERNEL
);
183 if (!part
->header_cache
)
186 part
->blocks
= kcalloc(part
->total_blocks
, sizeof(struct block
),
191 part
->sector_map
= vmalloc(part
->sector_count
* sizeof(u_long
));
192 if (!part
->sector_map
) {
193 printk(KERN_ERR PREFIX
"'%s': unable to allocate memory for "
194 "sector map", part
->mbd
.mtd
->name
);
198 for (i
=0; i
<part
->sector_count
; i
++)
199 part
->sector_map
[i
] = -1;
201 for (i
=0, blocks_found
=0; i
<part
->total_blocks
; i
++) {
202 rc
= part
->mbd
.mtd
->read(part
->mbd
.mtd
,
203 i
* part
->block_size
, part
->header_size
,
204 &retlen
, (u_char
*)part
->header_cache
);
206 if (!rc
&& retlen
!= part
->header_size
)
212 if (!build_block_map(part
, i
))
216 if (blocks_found
== 0) {
217 printk(KERN_NOTICE PREFIX
"no RFD magic found in '%s'\n",
218 part
->mbd
.mtd
->name
);
223 if (part
->reserved_block
== -1) {
224 printk(KERN_WARNING PREFIX
"'%s': no empty erase unit found\n",
225 part
->mbd
.mtd
->name
);
233 vfree(part
->sector_map
);
234 kfree(part
->header_cache
);
240 static int rfd_ftl_readsect(struct mtd_blktrans_dev
*dev
, u_long sector
, char *buf
)
242 struct partition
*part
= (struct partition
*)dev
;
247 if (sector
>= part
->sector_count
)
250 addr
= part
->sector_map
[sector
];
252 rc
= part
->mbd
.mtd
->read(part
->mbd
.mtd
, addr
, SECTOR_SIZE
,
253 &retlen
, (u_char
*)buf
);
254 if (!rc
&& retlen
!= SECTOR_SIZE
)
258 printk(KERN_WARNING PREFIX
"error reading '%s' at "
259 "0x%lx\n", part
->mbd
.mtd
->name
, addr
);
263 memset(buf
, 0, SECTOR_SIZE
);
268 static void erase_callback(struct erase_info
*erase
)
270 struct partition
*part
;
275 part
= (struct partition
*)erase
->priv
;
277 i
= (u32
)erase
->addr
/ part
->block_size
;
278 if (i
>= part
->total_blocks
|| part
->blocks
[i
].offset
!= erase
->addr
||
279 erase
->addr
> UINT_MAX
) {
280 printk(KERN_ERR PREFIX
"erase callback for unknown offset %llx "
281 "on '%s'\n", (unsigned long long)erase
->addr
, part
->mbd
.mtd
->name
);
285 if (erase
->state
!= MTD_ERASE_DONE
) {
286 printk(KERN_WARNING PREFIX
"erase failed at 0x%llx on '%s', "
287 "state %d\n", (unsigned long long)erase
->addr
,
288 part
->mbd
.mtd
->name
, erase
->state
);
290 part
->blocks
[i
].state
= BLOCK_FAILED
;
291 part
->blocks
[i
].free_sectors
= 0;
292 part
->blocks
[i
].used_sectors
= 0;
299 magic
= cpu_to_le16(RFD_MAGIC
);
301 part
->blocks
[i
].state
= BLOCK_ERASED
;
302 part
->blocks
[i
].free_sectors
= part
->data_sectors_per_block
;
303 part
->blocks
[i
].used_sectors
= 0;
304 part
->blocks
[i
].erases
++;
306 rc
= part
->mbd
.mtd
->write(part
->mbd
.mtd
,
307 part
->blocks
[i
].offset
, sizeof(magic
), &retlen
,
310 if (!rc
&& retlen
!= sizeof(magic
))
314 printk(KERN_ERR PREFIX
"'%s': unable to write RFD "
317 part
->blocks
[i
].offset
);
318 part
->blocks
[i
].state
= BLOCK_FAILED
;
321 part
->blocks
[i
].state
= BLOCK_OK
;
326 static int erase_block(struct partition
*part
, int block
)
328 struct erase_info
*erase
;
331 erase
= kmalloc(sizeof(struct erase_info
), GFP_KERNEL
);
335 erase
->mtd
= part
->mbd
.mtd
;
336 erase
->callback
= erase_callback
;
337 erase
->addr
= part
->blocks
[block
].offset
;
338 erase
->len
= part
->block_size
;
339 erase
->priv
= (u_long
)part
;
341 part
->blocks
[block
].state
= BLOCK_ERASING
;
342 part
->blocks
[block
].free_sectors
= 0;
344 rc
= part
->mbd
.mtd
->erase(part
->mbd
.mtd
, erase
);
347 printk(KERN_ERR PREFIX
"erase of region %llx,%llx on '%s' "
348 "failed\n", (unsigned long long)erase
->addr
,
349 (unsigned long long)erase
->len
, part
->mbd
.mtd
->name
);
357 static int move_block_contents(struct partition
*part
, int block_no
, u_long
*old_sector
)
364 part
->is_reclaiming
= 1;
366 sector_data
= kmalloc(SECTOR_SIZE
, GFP_KERNEL
);
370 map
= kmalloc(part
->header_size
, GFP_KERNEL
);
374 rc
= part
->mbd
.mtd
->read(part
->mbd
.mtd
,
375 part
->blocks
[block_no
].offset
, part
->header_size
,
376 &retlen
, (u_char
*)map
);
378 if (!rc
&& retlen
!= part
->header_size
)
382 printk(KERN_ERR PREFIX
"error reading '%s' at "
383 "0x%lx\n", part
->mbd
.mtd
->name
,
384 part
->blocks
[block_no
].offset
);
389 for (i
=0; i
<part
->data_sectors_per_block
; i
++) {
390 u16 entry
= le16_to_cpu(map
[HEADER_MAP_OFFSET
+ i
]);
394 if (entry
== SECTOR_FREE
|| entry
== SECTOR_DELETED
)
397 if (entry
== SECTOR_ZERO
)
400 /* already warned about and ignored in build_block_map() */
401 if (entry
>= part
->sector_count
)
404 addr
= part
->blocks
[block_no
].offset
+
405 (i
+ part
->header_sectors_per_block
) * SECTOR_SIZE
;
407 if (*old_sector
== addr
) {
409 if (!part
->blocks
[block_no
].used_sectors
--) {
410 rc
= erase_block(part
, block_no
);
415 rc
= part
->mbd
.mtd
->read(part
->mbd
.mtd
, addr
,
416 SECTOR_SIZE
, &retlen
, sector_data
);
418 if (!rc
&& retlen
!= SECTOR_SIZE
)
422 printk(KERN_ERR PREFIX
"'%s': Unable to "
423 "read sector for relocation\n",
424 part
->mbd
.mtd
->name
);
429 rc
= rfd_ftl_writesect((struct mtd_blktrans_dev
*)part
,
441 part
->is_reclaiming
= 0;
446 static int reclaim_block(struct partition
*part
, u_long
*old_sector
)
448 int block
, best_block
, score
, old_sector_block
;
451 /* we have a race if sync doesn't exist */
452 if (part
->mbd
.mtd
->sync
)
453 part
->mbd
.mtd
->sync(part
->mbd
.mtd
);
455 score
= 0x7fffffff; /* MAX_INT */
457 if (*old_sector
!= -1)
458 old_sector_block
= *old_sector
/ part
->block_size
;
460 old_sector_block
= -1;
462 for (block
=0; block
<part
->total_blocks
; block
++) {
465 if (block
== part
->reserved_block
)
469 * Postpone reclaiming if there is a free sector as
470 * more removed sectors is more efficient (have to move
473 if (part
->blocks
[block
].free_sectors
)
476 this_score
= part
->blocks
[block
].used_sectors
;
478 if (block
== old_sector_block
)
481 /* no point in moving a full block */
482 if (part
->blocks
[block
].used_sectors
==
483 part
->data_sectors_per_block
)
487 this_score
+= part
->blocks
[block
].erases
;
489 if (this_score
< score
) {
495 if (best_block
== -1)
498 part
->current_block
= -1;
499 part
->reserved_block
= best_block
;
501 pr_debug("reclaim_block: reclaiming block #%d with %d used "
502 "%d free sectors\n", best_block
,
503 part
->blocks
[best_block
].used_sectors
,
504 part
->blocks
[best_block
].free_sectors
);
506 if (part
->blocks
[best_block
].used_sectors
)
507 rc
= move_block_contents(part
, best_block
, old_sector
);
509 rc
= erase_block(part
, best_block
);
515 * IMPROVE: It would be best to choose the block with the most deleted sectors,
516 * because if we fill that one up first it'll have the most chance of having
517 * the least live sectors at reclaim.
519 static int find_free_block(struct partition
*part
)
523 block
= part
->current_block
== -1 ?
524 jiffies
% part
->total_blocks
: part
->current_block
;
528 if (part
->blocks
[block
].free_sectors
&&
529 block
!= part
->reserved_block
)
532 if (part
->blocks
[block
].state
== BLOCK_UNUSED
)
533 erase_block(part
, block
);
535 if (++block
>= part
->total_blocks
)
538 } while (block
!= stop
);
543 static int find_writable_block(struct partition
*part
, u_long
*old_sector
)
548 block
= find_free_block(part
);
551 if (!part
->is_reclaiming
) {
552 rc
= reclaim_block(part
, old_sector
);
556 block
= find_free_block(part
);
565 rc
= part
->mbd
.mtd
->read(part
->mbd
.mtd
, part
->blocks
[block
].offset
,
566 part
->header_size
, &retlen
, (u_char
*)part
->header_cache
);
568 if (!rc
&& retlen
!= part
->header_size
)
572 printk(KERN_ERR PREFIX
"'%s': unable to read header at "
573 "0x%lx\n", part
->mbd
.mtd
->name
,
574 part
->blocks
[block
].offset
);
578 part
->current_block
= block
;
584 static int mark_sector_deleted(struct partition
*part
, u_long old_addr
)
586 int block
, offset
, rc
;
589 u16 del
= cpu_to_le16(SECTOR_DELETED
);
591 block
= old_addr
/ part
->block_size
;
592 offset
= (old_addr
% part
->block_size
) / SECTOR_SIZE
-
593 part
->header_sectors_per_block
;
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
);
600 if (!rc
&& retlen
!= sizeof(del
))
604 printk(KERN_ERR PREFIX
"error writing '%s' at "
605 "0x%lx\n", part
->mbd
.mtd
->name
, addr
);
609 if (block
== part
->current_block
)
610 part
->header_cache
[offset
+ HEADER_MAP_OFFSET
] = del
;
612 part
->blocks
[block
].used_sectors
--;
614 if (!part
->blocks
[block
].used_sectors
&&
615 !part
->blocks
[block
].free_sectors
)
616 rc
= erase_block(part
, block
);
622 static int find_free_sector(const struct partition
*part
, const struct block
*block
)
626 i
= stop
= part
->data_sectors_per_block
- block
->free_sectors
;
629 if (le16_to_cpu(part
->header_cache
[HEADER_MAP_OFFSET
+ i
])
633 if (++i
== part
->data_sectors_per_block
)
641 static int do_writesect(struct mtd_blktrans_dev
*dev
, u_long sector
, char *buf
, ulong
*old_addr
)
643 struct partition
*part
= (struct partition
*)dev
;
651 if (part
->current_block
== -1 ||
652 !part
->blocks
[part
->current_block
].free_sectors
) {
654 rc
= find_writable_block(part
, old_addr
);
659 block
= &part
->blocks
[part
->current_block
];
661 i
= find_free_sector(part
, block
);
668 addr
= (i
+ part
->header_sectors_per_block
) * SECTOR_SIZE
+
670 rc
= part
->mbd
.mtd
->write(part
->mbd
.mtd
,
671 addr
, SECTOR_SIZE
, &retlen
, (u_char
*)buf
);
673 if (!rc
&& retlen
!= SECTOR_SIZE
)
677 printk(KERN_ERR PREFIX
"error writing '%s' at 0x%lx\n",
678 part
->mbd
.mtd
->name
, addr
);
683 part
->sector_map
[sector
] = addr
;
685 entry
= cpu_to_le16(sector
== 0 ? SECTOR_ZERO
: sector
);
687 part
->header_cache
[i
+ HEADER_MAP_OFFSET
] = entry
;
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
);
693 if (!rc
&& retlen
!= sizeof(entry
))
697 printk(KERN_ERR PREFIX
"error writing '%s' at 0x%lx\n",
698 part
->mbd
.mtd
->name
, addr
);
702 block
->used_sectors
++;
703 block
->free_sectors
--;
709 static int rfd_ftl_writesect(struct mtd_blktrans_dev
*dev
, u_long sector
, char *buf
)
711 struct partition
*part
= (struct partition
*)dev
;
716 pr_debug("rfd_ftl_writesect(sector=0x%lx)\n", sector
);
718 if (part
->reserved_block
== -1) {
723 if (sector
>= part
->sector_count
) {
728 old_addr
= part
->sector_map
[sector
];
730 for (i
=0; i
<SECTOR_SIZE
; i
++) {
734 rc
= do_writesect(dev
, sector
, buf
, &old_addr
);
740 if (i
== SECTOR_SIZE
)
741 part
->sector_map
[sector
] = -1;
744 rc
= mark_sector_deleted(part
, old_addr
);
750 static int rfd_ftl_getgeo(struct mtd_blktrans_dev
*dev
, struct hd_geometry
*geo
)
752 struct partition
*part
= (struct partition
*)dev
;
755 geo
->sectors
= SECTORS_PER_TRACK
;
756 geo
->cylinders
= part
->cylinders
;
761 static void rfd_ftl_add_mtd(struct mtd_blktrans_ops
*tr
, struct mtd_info
*mtd
)
763 struct partition
*part
;
765 if (mtd
->type
!= MTD_NORFLASH
|| mtd
->size
> UINT_MAX
)
768 part
= kzalloc(sizeof(struct partition
), GFP_KERNEL
);
775 part
->block_size
= block_size
;
777 if (!mtd
->erasesize
) {
778 printk(KERN_WARNING PREFIX
"please provide block_size");
781 part
->block_size
= mtd
->erasesize
;
784 if (scan_header(part
) == 0) {
785 part
->mbd
.size
= part
->sector_count
;
787 part
->mbd
.devnum
= -1;
788 if (!(mtd
->flags
& MTD_WRITEABLE
))
789 part
->mbd
.readonly
= 1;
790 else if (part
->errors
) {
791 printk(KERN_WARNING PREFIX
"'%s': errors found, "
792 "setting read-only\n", mtd
->name
);
793 part
->mbd
.readonly
= 1;
796 printk(KERN_INFO PREFIX
"name: '%s' type: %d flags %x\n",
797 mtd
->name
, mtd
->type
, mtd
->flags
);
799 if (!add_mtd_blktrans_dev((void*)part
))
806 static void rfd_ftl_remove_dev(struct mtd_blktrans_dev
*dev
)
808 struct partition
*part
= (struct partition
*)dev
;
811 for (i
=0; i
<part
->total_blocks
; i
++) {
812 pr_debug("rfd_ftl_remove_dev:'%s': erase unit #%02d: %d erases\n",
813 part
->mbd
.mtd
->name
, i
, part
->blocks
[i
].erases
);
816 del_mtd_blktrans_dev(dev
);
817 vfree(part
->sector_map
);
818 kfree(part
->header_cache
);
822 static struct mtd_blktrans_ops rfd_ftl_tr
= {
824 .major
= RFD_FTL_MAJOR
,
825 .part_bits
= PART_BITS
,
826 .blksize
= SECTOR_SIZE
,
828 .readsect
= rfd_ftl_readsect
,
829 .writesect
= rfd_ftl_writesect
,
830 .getgeo
= rfd_ftl_getgeo
,
831 .add_mtd
= rfd_ftl_add_mtd
,
832 .remove_dev
= rfd_ftl_remove_dev
,
833 .owner
= THIS_MODULE
,
836 static int __init
init_rfd_ftl(void)
838 return register_mtd_blktrans(&rfd_ftl_tr
);
841 static void __exit
cleanup_rfd_ftl(void)
843 deregister_mtd_blktrans(&rfd_ftl_tr
);
846 module_init(init_rfd_ftl
);
847 module_exit(cleanup_rfd_ftl
);
849 MODULE_LICENSE("GPL");
850 MODULE_AUTHOR("Sean Young <sean@mess.org>");
851 MODULE_DESCRIPTION("Support code for RFD Flash Translation Layer, "
852 "used by General Software's Embedded BIOS");