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
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/slab.h>
22 #include <linux/jiffies.h>
24 #include <asm/types.h>
26 #define const_cpu_to_le16 __constant_cpu_to_le16
28 static int block_size
= 0;
29 module_param(block_size
, int, 0);
30 MODULE_PARM_DESC(block_size
, "Block size to use by RFD, defaults to erase unit size");
32 #define PREFIX "rfd_ftl: "
34 /* This major has been assigned by device@lanana.org */
36 #define RFD_FTL_MAJOR 256
39 /* Maximum number of partitions in an FTL region */
42 /* An erase unit should start with this value */
43 #define RFD_MAGIC 0x9193
45 /* the second value is 0xffff or 0xffc8; function unknown */
47 /* the third value is always 0xffff, ignored */
49 /* next is an array of mapping for each corresponding sector */
50 #define HEADER_MAP_OFFSET 3
51 #define SECTOR_DELETED 0x0000
52 #define SECTOR_ZERO 0xfffe
53 #define SECTOR_FREE 0xffff
55 #define SECTOR_SIZE 512
57 #define SECTORS_PER_TRACK 63
73 struct mtd_blktrans_dev mbd
;
75 u_int block_size
; /* size of erase unit */
76 u_int total_blocks
; /* number of erase units */
77 u_int header_sectors_per_block
; /* header sectors in erase unit */
78 u_int data_sectors_per_block
; /* data sectors in erase unit */
79 u_int sector_count
; /* sectors in translated disk */
80 u_int header_size
; /* bytes in header sector */
81 int reserved_block
; /* block next up for reclaim */
82 int current_block
; /* block to write to */
83 u16
*header_cache
; /* cached header */
92 static int rfd_ftl_writesect(struct mtd_blktrans_dev
*dev
, u_long sector
, char *buf
);
94 static int build_block_map(struct partition
*part
, int block_no
)
96 struct block
*block
= &part
->blocks
[block_no
];
99 block
->offset
= part
->block_size
* block_no
;
101 if (le16_to_cpu(part
->header_cache
[0]) != RFD_MAGIC
) {
102 block
->state
= BLOCK_ERASED
; /* assumption */
103 block
->free_sectors
= part
->data_sectors_per_block
;
104 part
->reserved_block
= block_no
;
108 block
->state
= BLOCK_OK
;
110 for (i
=0; i
<part
->data_sectors_per_block
; i
++) {
113 entry
= le16_to_cpu(part
->header_cache
[HEADER_MAP_OFFSET
+ i
]);
115 if (entry
== SECTOR_DELETED
)
118 if (entry
== SECTOR_FREE
) {
119 block
->free_sectors
++;
123 if (entry
== SECTOR_ZERO
)
126 if (entry
>= part
->sector_count
) {
127 printk(KERN_NOTICE PREFIX
128 "'%s': unit #%d: entry %d corrupt, "
129 "sector %d out of range\n",
130 part
->mbd
.mtd
->name
, block_no
, i
, entry
);
134 if (part
->sector_map
[entry
] != -1) {
135 printk(KERN_NOTICE PREFIX
136 "'%s': more than one entry for sector %d\n",
137 part
->mbd
.mtd
->name
, entry
);
142 part
->sector_map
[entry
] = block
->offset
+
143 (i
+ part
->header_sectors_per_block
) * SECTOR_SIZE
;
145 block
->used_sectors
++;
148 if (block
->free_sectors
== part
->data_sectors_per_block
)
149 part
->reserved_block
= block_no
;
154 static int scan_header(struct partition
*part
)
156 int sectors_per_block
;
161 sectors_per_block
= part
->block_size
/ SECTOR_SIZE
;
162 part
->total_blocks
= part
->mbd
.mtd
->size
/ part
->block_size
;
164 if (part
->total_blocks
< 2)
167 /* each erase block has three bytes header, followed by the map */
168 part
->header_sectors_per_block
=
169 ((HEADER_MAP_OFFSET
+ sectors_per_block
) *
170 sizeof(u16
) + SECTOR_SIZE
- 1) / SECTOR_SIZE
;
172 part
->data_sectors_per_block
= sectors_per_block
-
173 part
->header_sectors_per_block
;
175 part
->header_size
= (HEADER_MAP_OFFSET
+
176 part
->data_sectors_per_block
) * sizeof(u16
);
178 part
->cylinders
= (part
->data_sectors_per_block
*
179 (part
->total_blocks
- 1) - 1) / SECTORS_PER_TRACK
;
181 part
->sector_count
= part
->cylinders
* SECTORS_PER_TRACK
;
183 part
->current_block
= -1;
184 part
->reserved_block
= -1;
185 part
->is_reclaiming
= 0;
187 part
->header_cache
= kmalloc(part
->header_size
, GFP_KERNEL
);
188 if (!part
->header_cache
)
191 part
->blocks
= kcalloc(part
->total_blocks
, sizeof(struct block
),
196 part
->sector_map
= vmalloc(part
->sector_count
* sizeof(u_long
));
197 if (!part
->sector_map
) {
198 printk(KERN_ERR PREFIX
"'%s': unable to allocate memory for "
199 "sector map", part
->mbd
.mtd
->name
);
203 for (i
=0; i
<part
->sector_count
; i
++)
204 part
->sector_map
[i
] = -1;
206 for (i
=0, blocks_found
=0; i
<part
->total_blocks
; i
++) {
207 rc
= part
->mbd
.mtd
->read(part
->mbd
.mtd
,
208 i
* part
->block_size
, part
->header_size
,
209 &retlen
, (u_char
*)part
->header_cache
);
211 if (!rc
&& retlen
!= part
->header_size
)
217 if (!build_block_map(part
, i
))
221 if (blocks_found
== 0) {
222 printk(KERN_NOTICE PREFIX
"no RFD magic found in '%s'\n",
223 part
->mbd
.mtd
->name
);
228 if (part
->reserved_block
== -1) {
229 printk(KERN_NOTICE PREFIX
"'%s': no empty erase unit found\n",
230 part
->mbd
.mtd
->name
);
238 vfree(part
->sector_map
);
239 kfree(part
->header_cache
);
245 static int rfd_ftl_readsect(struct mtd_blktrans_dev
*dev
, u_long sector
, char *buf
)
247 struct partition
*part
= (struct partition
*)dev
;
252 if (sector
>= part
->sector_count
)
255 addr
= part
->sector_map
[sector
];
257 rc
= part
->mbd
.mtd
->read(part
->mbd
.mtd
, addr
, SECTOR_SIZE
,
258 &retlen
, (u_char
*)buf
);
259 if (!rc
&& retlen
!= SECTOR_SIZE
)
263 printk(KERN_WARNING PREFIX
"error reading '%s' at "
264 "0x%lx\n", part
->mbd
.mtd
->name
, addr
);
268 memset(buf
, 0, SECTOR_SIZE
);
273 static void erase_callback(struct erase_info
*erase
)
275 struct partition
*part
;
280 part
= (struct partition
*)erase
->priv
;
282 i
= erase
->addr
/ part
->block_size
;
283 if (i
>= part
->total_blocks
|| part
->blocks
[i
].offset
!= erase
->addr
) {
284 printk(KERN_ERR PREFIX
"erase callback for unknown offset %x "
285 "on '%s'\n", erase
->addr
, part
->mbd
.mtd
->name
);
289 if (erase
->state
!= MTD_ERASE_DONE
) {
290 printk(KERN_WARNING PREFIX
"erase failed at 0x%x on '%s', "
291 "state %d\n", erase
->addr
,
292 part
->mbd
.mtd
->name
, erase
->state
);
294 part
->blocks
[i
].state
= BLOCK_FAILED
;
295 part
->blocks
[i
].free_sectors
= 0;
296 part
->blocks
[i
].used_sectors
= 0;
303 magic
= const_cpu_to_le16(RFD_MAGIC
);
305 part
->blocks
[i
].state
= BLOCK_ERASED
;
306 part
->blocks
[i
].free_sectors
= part
->data_sectors_per_block
;
307 part
->blocks
[i
].used_sectors
= 0;
308 part
->blocks
[i
].erases
++;
310 rc
= part
->mbd
.mtd
->write(part
->mbd
.mtd
,
311 part
->blocks
[i
].offset
, sizeof(magic
), &retlen
,
314 if (!rc
&& retlen
!= sizeof(magic
))
318 printk(KERN_NOTICE PREFIX
"'%s': unable to write RFD "
321 part
->blocks
[i
].offset
);
322 part
->blocks
[i
].state
= BLOCK_FAILED
;
325 part
->blocks
[i
].state
= BLOCK_OK
;
330 static int erase_block(struct partition
*part
, int block
)
332 struct erase_info
*erase
;
335 erase
= kmalloc(sizeof(struct erase_info
), GFP_KERNEL
);
339 erase
->mtd
= part
->mbd
.mtd
;
340 erase
->callback
= erase_callback
;
341 erase
->addr
= part
->blocks
[block
].offset
;
342 erase
->len
= part
->block_size
;
343 erase
->priv
= (u_long
)part
;
345 part
->blocks
[block
].state
= BLOCK_ERASING
;
346 part
->blocks
[block
].free_sectors
= 0;
348 rc
= part
->mbd
.mtd
->erase(part
->mbd
.mtd
, erase
);
351 printk(KERN_WARNING PREFIX
"erase of region %x,%x on '%s' "
352 "failed\n", erase
->addr
, erase
->len
,
353 part
->mbd
.mtd
->name
);
361 static int move_block_contents(struct partition
*part
, int block_no
, u_long
*old_sector
)
368 part
->is_reclaiming
= 1;
370 sector_data
= kmalloc(SECTOR_SIZE
, GFP_KERNEL
);
374 map
= kmalloc(part
->header_size
, GFP_KERNEL
);
378 rc
= part
->mbd
.mtd
->read(part
->mbd
.mtd
,
379 part
->blocks
[block_no
].offset
, part
->header_size
,
380 &retlen
, (u_char
*)map
);
382 if (!rc
&& retlen
!= part
->header_size
)
386 printk(KERN_NOTICE PREFIX
"error reading '%s' at "
387 "0x%lx\n", part
->mbd
.mtd
->name
,
388 part
->blocks
[block_no
].offset
);
393 for (i
=0; i
<part
->data_sectors_per_block
; i
++) {
394 u16 entry
= le16_to_cpu(map
[HEADER_MAP_OFFSET
+ i
]);
398 if (entry
== SECTOR_FREE
|| entry
== SECTOR_DELETED
)
401 if (entry
== SECTOR_ZERO
)
404 /* already warned about and ignored in build_block_map() */
405 if (entry
>= part
->sector_count
)
408 addr
= part
->blocks
[block_no
].offset
+
409 (i
+ part
->header_sectors_per_block
) * SECTOR_SIZE
;
411 if (*old_sector
== addr
) {
413 if (!part
->blocks
[block_no
].used_sectors
--) {
414 rc
= erase_block(part
, block_no
);
419 rc
= part
->mbd
.mtd
->read(part
->mbd
.mtd
, addr
,
420 SECTOR_SIZE
, &retlen
, sector_data
);
422 if (!rc
&& retlen
!= SECTOR_SIZE
)
426 printk(KERN_NOTICE PREFIX
"'%s': Unable to "
427 "read sector for relocation\n",
428 part
->mbd
.mtd
->name
);
433 rc
= rfd_ftl_writesect((struct mtd_blktrans_dev
*)part
,
445 part
->is_reclaiming
= 0;
450 static int reclaim_block(struct partition
*part
, u_long
*old_sector
)
452 int block
, best_block
, score
, old_sector_block
;
455 /* we have a race if sync doesn't exist */
456 if (part
->mbd
.mtd
->sync
)
457 part
->mbd
.mtd
->sync(part
->mbd
.mtd
);
459 score
= 0x7fffffff; /* MAX_INT */
461 if (*old_sector
!= -1)
462 old_sector_block
= *old_sector
/ part
->block_size
;
464 old_sector_block
= -1;
466 for (block
=0; block
<part
->total_blocks
; block
++) {
469 if (block
== part
->reserved_block
)
473 * Postpone reclaiming if there is a free sector as
474 * more removed sectors is more efficient (have to move
477 if (part
->blocks
[block
].free_sectors
)
480 this_score
= part
->blocks
[block
].used_sectors
;
482 if (block
== old_sector_block
)
485 /* no point in moving a full block */
486 if (part
->blocks
[block
].used_sectors
==
487 part
->data_sectors_per_block
)
491 this_score
+= part
->blocks
[block
].erases
;
493 if (this_score
< score
) {
499 if (best_block
== -1)
502 part
->current_block
= -1;
503 part
->reserved_block
= best_block
;
505 pr_debug("reclaim_block: reclaiming block #%d with %d used "
506 "%d free sectors\n", best_block
,
507 part
->blocks
[best_block
].used_sectors
,
508 part
->blocks
[best_block
].free_sectors
);
510 if (part
->blocks
[best_block
].used_sectors
)
511 rc
= move_block_contents(part
, best_block
, old_sector
);
513 rc
= erase_block(part
, best_block
);
519 * IMPROVE: It would be best to choose the block with the most deleted sectors,
520 * because if we fill that one up first it'll have the most chance of having
521 * the least live sectors at reclaim.
523 static int find_free_block(const struct partition
*part
)
527 block
= part
->current_block
== -1 ?
528 jiffies
% part
->total_blocks
: part
->current_block
;
532 if (part
->blocks
[block
].free_sectors
&&
533 block
!= part
->reserved_block
)
536 if (++block
>= part
->total_blocks
)
539 } while (block
!= stop
);
544 static int find_writeable_block(struct partition
*part
, u_long
*old_sector
)
549 block
= find_free_block(part
);
552 if (!part
->is_reclaiming
) {
553 rc
= reclaim_block(part
, old_sector
);
557 block
= find_free_block(part
);
566 rc
= part
->mbd
.mtd
->read(part
->mbd
.mtd
, part
->blocks
[block
].offset
,
567 part
->header_size
, &retlen
, (u_char
*)part
->header_cache
);
569 if (!rc
&& retlen
!= part
->header_size
)
573 printk(KERN_NOTICE PREFIX
"'%s': unable to read header at "
574 "0x%lx\n", part
->mbd
.mtd
->name
,
575 part
->blocks
[block
].offset
);
579 part
->current_block
= block
;
585 static int mark_sector_deleted(struct partition
*part
, u_long old_addr
)
587 int block
, offset
, rc
;
590 u16 del
= const_cpu_to_le16(SECTOR_DELETED
);
592 block
= old_addr
/ part
->block_size
;
593 offset
= (old_addr
% part
->block_size
) / SECTOR_SIZE
-
594 part
->header_sectors_per_block
;
596 addr
= part
->blocks
[block
].offset
+
597 (HEADER_MAP_OFFSET
+ offset
) * sizeof(u16
);
598 rc
= part
->mbd
.mtd
->write(part
->mbd
.mtd
, addr
,
599 sizeof(del
), &retlen
, (u_char
*)&del
);
601 if (!rc
&& retlen
!= sizeof(del
))
605 printk(KERN_WARNING PREFIX
"error writing '%s' at "
606 "0x%lx\n", part
->mbd
.mtd
->name
, addr
);
610 if (block
== part
->current_block
)
611 part
->header_cache
[offset
+ HEADER_MAP_OFFSET
] = del
;
613 part
->blocks
[block
].used_sectors
--;
615 if (!part
->blocks
[block
].used_sectors
&&
616 !part
->blocks
[block
].free_sectors
)
617 rc
= erase_block(part
, block
);
623 static int find_free_sector(const struct partition
*part
, const struct block
*block
)
627 i
= stop
= part
->data_sectors_per_block
- block
->free_sectors
;
630 if (le16_to_cpu(part
->header_cache
[HEADER_MAP_OFFSET
+ i
])
634 if (++i
== part
->data_sectors_per_block
)
642 static int do_writesect(struct mtd_blktrans_dev
*dev
, u_long sector
, char *buf
, ulong
*old_addr
)
644 struct partition
*part
= (struct partition
*)dev
;
652 if (part
->current_block
== -1 ||
653 !part
->blocks
[part
->current_block
].free_sectors
) {
655 rc
= find_writeable_block(part
, old_addr
);
660 block
= &part
->blocks
[part
->current_block
];
662 i
= find_free_sector(part
, block
);
669 addr
= (i
+ part
->header_sectors_per_block
) * SECTOR_SIZE
+
671 rc
= part
->mbd
.mtd
->write(part
->mbd
.mtd
,
672 addr
, SECTOR_SIZE
, &retlen
, (u_char
*)buf
);
674 if (!rc
&& retlen
!= SECTOR_SIZE
)
678 printk(KERN_WARNING PREFIX
"error writing '%s' at 0x%lx\n",
679 part
->mbd
.mtd
->name
, addr
);
684 part
->sector_map
[sector
] = addr
;
686 entry
= cpu_to_le16(sector
== 0 ? SECTOR_ZERO
: sector
);
688 part
->header_cache
[i
+ HEADER_MAP_OFFSET
] = entry
;
690 addr
= block
->offset
+ (HEADER_MAP_OFFSET
+ i
) * sizeof(u16
);
691 rc
= part
->mbd
.mtd
->write(part
->mbd
.mtd
, addr
,
692 sizeof(entry
), &retlen
, (u_char
*)&entry
);
694 if (!rc
&& retlen
!= sizeof(entry
))
698 printk(KERN_WARNING PREFIX
"error writing '%s' at 0x%lx\n",
699 part
->mbd
.mtd
->name
, addr
);
703 block
->used_sectors
++;
704 block
->free_sectors
--;
710 static int rfd_ftl_writesect(struct mtd_blktrans_dev
*dev
, u_long sector
, char *buf
)
712 struct partition
*part
= (struct partition
*)dev
;
717 pr_debug("rfd_ftl_writesect(sector=0x%lx)\n", sector
);
719 if (part
->reserved_block
== -1) {
724 if (sector
>= part
->sector_count
) {
729 old_addr
= part
->sector_map
[sector
];
731 for (i
=0; i
<SECTOR_SIZE
; i
++) {
735 rc
= do_writesect(dev
, sector
, buf
, &old_addr
);
741 if (i
== SECTOR_SIZE
)
742 part
->sector_map
[sector
] = -1;
745 rc
= mark_sector_deleted(part
, old_addr
);
751 static int rfd_ftl_getgeo(struct mtd_blktrans_dev
*dev
, struct hd_geometry
*geo
)
753 struct partition
*part
= (struct partition
*)dev
;
756 geo
->sectors
= SECTORS_PER_TRACK
;
757 geo
->cylinders
= part
->cylinders
;
762 static void rfd_ftl_add_mtd(struct mtd_blktrans_ops
*tr
, struct mtd_info
*mtd
)
764 struct partition
*part
;
766 if (mtd
->type
!= MTD_NORFLASH
)
769 part
= kcalloc(1, sizeof(struct partition
), GFP_KERNEL
);
776 part
->block_size
= block_size
;
778 if (!mtd
->erasesize
) {
779 printk(KERN_NOTICE PREFIX
"please provide block_size");
783 part
->block_size
= mtd
->erasesize
;
786 if (scan_header(part
) == 0) {
787 part
->mbd
.size
= part
->sector_count
;
788 part
->mbd
.blksize
= SECTOR_SIZE
;
790 part
->mbd
.devnum
= -1;
791 if (!(mtd
->flags
& MTD_WRITEABLE
))
792 part
->mbd
.readonly
= 1;
793 else if (part
->errors
) {
794 printk(KERN_NOTICE PREFIX
"'%s': errors found, "
795 "setting read-only", mtd
->name
);
796 part
->mbd
.readonly
= 1;
799 printk(KERN_INFO PREFIX
"name: '%s' type: %d flags %x\n",
800 mtd
->name
, mtd
->type
, mtd
->flags
);
802 if (!add_mtd_blktrans_dev((void*)part
))
809 static void rfd_ftl_remove_dev(struct mtd_blktrans_dev
*dev
)
811 struct partition
*part
= (struct partition
*)dev
;
814 for (i
=0; i
<part
->total_blocks
; i
++) {
815 pr_debug("rfd_ftl_remove_dev:'%s': erase unit #%02d: %d erases\n",
816 part
->mbd
.mtd
->name
, i
, part
->blocks
[i
].erases
);
819 del_mtd_blktrans_dev(dev
);
820 vfree(part
->sector_map
);
821 kfree(part
->header_cache
);
826 struct mtd_blktrans_ops rfd_ftl_tr
= {
828 .major
= RFD_FTL_MAJOR
,
829 .part_bits
= PART_BITS
,
830 .readsect
= rfd_ftl_readsect
,
831 .writesect
= rfd_ftl_writesect
,
832 .getgeo
= rfd_ftl_getgeo
,
833 .add_mtd
= rfd_ftl_add_mtd
,
834 .remove_dev
= rfd_ftl_remove_dev
,
835 .owner
= THIS_MODULE
,
838 static int __init
init_rfd_ftl(void)
840 return register_mtd_blktrans(&rfd_ftl_tr
);
843 static void __exit
cleanup_rfd_ftl(void)
845 deregister_mtd_blktrans(&rfd_ftl_tr
);
848 module_init(init_rfd_ftl
);
849 module_exit(cleanup_rfd_ftl
);
851 MODULE_LICENSE("GPL");
852 MODULE_AUTHOR("Sean Young <sean@mess.org>");
853 MODULE_DESCRIPTION("Support code for RFD Flash Translation Layer, "
854 "used by General Software's Embedded BIOS");