Merge tag 'block-5.11-2021-01-10' of git://git.kernel.dk/linux-block
[linux/fpc-iii.git] / drivers / mtd / rfd_ftl.c
blob3d1df82fa105f13900750aa3db0a4bfe493c4142
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * rfd_ftl.c -- resident flash disk (flash translation layer)
5 * Copyright © 2005 Sean Young <sean@mess.org>
7 * This type of flash translation layer (FTL) is used by the Embedded BIOS
8 * by General Software. It is known as the Resident Flash Disk (RFD), see:
10 * http://www.gensw.com/pages/prod/bios/rfd.htm
12 * based on ftl.c
15 #include <linux/hdreg.h>
16 #include <linux/init.h>
17 #include <linux/mtd/blktrans.h>
18 #include <linux/mtd/mtd.h>
19 #include <linux/vmalloc.h>
20 #include <linux/slab.h>
21 #include <linux/jiffies.h>
22 #include <linux/module.h>
24 #include <asm/types.h>
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 */
33 #ifndef RFD_FTL_MAJOR
34 #define RFD_FTL_MAJOR 256
35 #endif
37 /* Maximum number of partitions in an FTL region */
38 #define PART_BITS 4
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
57 struct block {
58 enum {
59 BLOCK_OK,
60 BLOCK_ERASING,
61 BLOCK_ERASED,
62 BLOCK_UNUSED,
63 BLOCK_FAILED
64 } state;
65 int free_sectors;
66 int used_sectors;
67 int erases;
68 u_long offset;
71 struct partition {
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 */
84 int is_reclaiming;
85 int cylinders;
86 int errors;
87 u_long *sector_map;
88 struct block *blocks;
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];
96 int i;
98 block->offset = part->block_size * block_no;
100 if (le16_to_cpu(part->header_cache[0]) != RFD_MAGIC) {
101 block->state = BLOCK_UNUSED;
102 return -ENOENT;
105 block->state = BLOCK_OK;
107 for (i=0; i<part->data_sectors_per_block; i++) {
108 u16 entry;
110 entry = le16_to_cpu(part->header_cache[HEADER_MAP_OFFSET + i]);
112 if (entry == SECTOR_DELETED)
113 continue;
115 if (entry == SECTOR_FREE) {
116 block->free_sectors++;
117 continue;
120 if (entry == SECTOR_ZERO)
121 entry = 0;
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);
128 continue;
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);
135 part->errors = 1;
136 continue;
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;
148 return 0;
151 static int scan_header(struct partition *part)
153 int sectors_per_block;
154 int i, rc = -ENOMEM;
155 int blocks_found;
156 size_t retlen;
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)
162 return -ENOENT;
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)
186 goto err;
188 part->blocks = kcalloc(part->total_blocks, sizeof(struct block),
189 GFP_KERNEL);
190 if (!part->blocks)
191 goto err;
193 part->sector_map = vmalloc(array_size(sizeof(u_long),
194 part->sector_count));
195 if (!part->sector_map) {
196 printk(KERN_ERR PREFIX "'%s': unable to allocate memory for "
197 "sector map", part->mbd.mtd->name);
198 goto err;
201 for (i=0; i<part->sector_count; i++)
202 part->sector_map[i] = -1;
204 for (i=0, blocks_found=0; i<part->total_blocks; i++) {
205 rc = mtd_read(part->mbd.mtd, i * part->block_size,
206 part->header_size, &retlen,
207 (u_char *)part->header_cache);
209 if (!rc && retlen != part->header_size)
210 rc = -EIO;
212 if (rc)
213 goto err;
215 if (!build_block_map(part, i))
216 blocks_found++;
219 if (blocks_found == 0) {
220 printk(KERN_NOTICE PREFIX "no RFD magic found in '%s'\n",
221 part->mbd.mtd->name);
222 rc = -ENOENT;
223 goto err;
226 if (part->reserved_block == -1) {
227 printk(KERN_WARNING PREFIX "'%s': no empty erase unit found\n",
228 part->mbd.mtd->name);
230 part->errors = 1;
233 return 0;
235 err:
236 vfree(part->sector_map);
237 kfree(part->header_cache);
238 kfree(part->blocks);
240 return rc;
243 static int rfd_ftl_readsect(struct mtd_blktrans_dev *dev, u_long sector, char *buf)
245 struct partition *part = (struct partition*)dev;
246 u_long addr;
247 size_t retlen;
248 int rc;
250 if (sector >= part->sector_count)
251 return -EIO;
253 addr = part->sector_map[sector];
254 if (addr != -1) {
255 rc = mtd_read(part->mbd.mtd, addr, SECTOR_SIZE, &retlen,
256 (u_char *)buf);
257 if (!rc && retlen != SECTOR_SIZE)
258 rc = -EIO;
260 if (rc) {
261 printk(KERN_WARNING PREFIX "error reading '%s' at "
262 "0x%lx\n", part->mbd.mtd->name, addr);
263 return rc;
265 } else
266 memset(buf, 0, SECTOR_SIZE);
268 return 0;
271 static int erase_block(struct partition *part, int block)
273 struct erase_info *erase;
274 int rc;
276 erase = kmalloc(sizeof(struct erase_info), GFP_KERNEL);
277 if (!erase)
278 return -ENOMEM;
280 erase->addr = part->blocks[block].offset;
281 erase->len = part->block_size;
283 part->blocks[block].state = BLOCK_ERASING;
284 part->blocks[block].free_sectors = 0;
286 rc = mtd_erase(part->mbd.mtd, erase);
287 if (rc) {
288 printk(KERN_ERR PREFIX "erase of region %llx,%llx on '%s' "
289 "failed\n", (unsigned long long)erase->addr,
290 (unsigned long long)erase->len, part->mbd.mtd->name);
291 part->blocks[block].state = BLOCK_FAILED;
292 part->blocks[block].free_sectors = 0;
293 part->blocks[block].used_sectors = 0;
294 } else {
295 u16 magic = cpu_to_le16(RFD_MAGIC);
296 size_t retlen;
298 part->blocks[block].state = BLOCK_ERASED;
299 part->blocks[block].free_sectors = part->data_sectors_per_block;
300 part->blocks[block].used_sectors = 0;
301 part->blocks[block].erases++;
303 rc = mtd_write(part->mbd.mtd, part->blocks[block].offset,
304 sizeof(magic), &retlen, (u_char *)&magic);
305 if (!rc && retlen != sizeof(magic))
306 rc = -EIO;
308 if (rc) {
309 pr_err(PREFIX "'%s': unable to write RFD header at 0x%lx\n",
310 part->mbd.mtd->name, part->blocks[block].offset);
311 part->blocks[block].state = BLOCK_FAILED;
312 } else {
313 part->blocks[block].state = BLOCK_OK;
317 kfree(erase);
319 return rc;
322 static int move_block_contents(struct partition *part, int block_no, u_long *old_sector)
324 void *sector_data;
325 u16 *map;
326 size_t retlen;
327 int i, rc = -ENOMEM;
329 part->is_reclaiming = 1;
331 sector_data = kmalloc(SECTOR_SIZE, GFP_KERNEL);
332 if (!sector_data)
333 goto err3;
335 map = kmalloc(part->header_size, GFP_KERNEL);
336 if (!map)
337 goto err2;
339 rc = mtd_read(part->mbd.mtd, part->blocks[block_no].offset,
340 part->header_size, &retlen, (u_char *)map);
342 if (!rc && retlen != part->header_size)
343 rc = -EIO;
345 if (rc) {
346 printk(KERN_ERR PREFIX "error reading '%s' at "
347 "0x%lx\n", part->mbd.mtd->name,
348 part->blocks[block_no].offset);
350 goto err;
353 for (i=0; i<part->data_sectors_per_block; i++) {
354 u16 entry = le16_to_cpu(map[HEADER_MAP_OFFSET + i]);
355 u_long addr;
358 if (entry == SECTOR_FREE || entry == SECTOR_DELETED)
359 continue;
361 if (entry == SECTOR_ZERO)
362 entry = 0;
364 /* already warned about and ignored in build_block_map() */
365 if (entry >= part->sector_count)
366 continue;
368 addr = part->blocks[block_no].offset +
369 (i + part->header_sectors_per_block) * SECTOR_SIZE;
371 if (*old_sector == addr) {
372 *old_sector = -1;
373 if (!part->blocks[block_no].used_sectors--) {
374 rc = erase_block(part, block_no);
375 break;
377 continue;
379 rc = mtd_read(part->mbd.mtd, addr, SECTOR_SIZE, &retlen,
380 sector_data);
382 if (!rc && retlen != SECTOR_SIZE)
383 rc = -EIO;
385 if (rc) {
386 printk(KERN_ERR PREFIX "'%s': Unable to "
387 "read sector for relocation\n",
388 part->mbd.mtd->name);
390 goto err;
393 rc = rfd_ftl_writesect((struct mtd_blktrans_dev*)part,
394 entry, sector_data);
396 if (rc)
397 goto err;
400 err:
401 kfree(map);
402 err2:
403 kfree(sector_data);
404 err3:
405 part->is_reclaiming = 0;
407 return rc;
410 static int reclaim_block(struct partition *part, u_long *old_sector)
412 int block, best_block, score, old_sector_block;
413 int rc;
415 /* we have a race if sync doesn't exist */
416 mtd_sync(part->mbd.mtd);
418 score = 0x7fffffff; /* MAX_INT */
419 best_block = -1;
420 if (*old_sector != -1)
421 old_sector_block = *old_sector / part->block_size;
422 else
423 old_sector_block = -1;
425 for (block=0; block<part->total_blocks; block++) {
426 int this_score;
428 if (block == part->reserved_block)
429 continue;
432 * Postpone reclaiming if there is a free sector as
433 * more removed sectors is more efficient (have to move
434 * less).
436 if (part->blocks[block].free_sectors)
437 return 0;
439 this_score = part->blocks[block].used_sectors;
441 if (block == old_sector_block)
442 this_score--;
443 else {
444 /* no point in moving a full block */
445 if (part->blocks[block].used_sectors ==
446 part->data_sectors_per_block)
447 continue;
450 this_score += part->blocks[block].erases;
452 if (this_score < score) {
453 best_block = block;
454 score = this_score;
458 if (best_block == -1)
459 return -ENOSPC;
461 part->current_block = -1;
462 part->reserved_block = best_block;
464 pr_debug("reclaim_block: reclaiming block #%d with %d used "
465 "%d free sectors\n", best_block,
466 part->blocks[best_block].used_sectors,
467 part->blocks[best_block].free_sectors);
469 if (part->blocks[best_block].used_sectors)
470 rc = move_block_contents(part, best_block, old_sector);
471 else
472 rc = erase_block(part, best_block);
474 return rc;
478 * IMPROVE: It would be best to choose the block with the most deleted sectors,
479 * because if we fill that one up first it'll have the most chance of having
480 * the least live sectors at reclaim.
482 static int find_free_block(struct partition *part)
484 int block, stop;
486 block = part->current_block == -1 ?
487 jiffies % part->total_blocks : part->current_block;
488 stop = block;
490 do {
491 if (part->blocks[block].free_sectors &&
492 block != part->reserved_block)
493 return block;
495 if (part->blocks[block].state == BLOCK_UNUSED)
496 erase_block(part, block);
498 if (++block >= part->total_blocks)
499 block = 0;
501 } while (block != stop);
503 return -1;
506 static int find_writable_block(struct partition *part, u_long *old_sector)
508 int rc, block;
509 size_t retlen;
511 block = find_free_block(part);
513 if (block == -1) {
514 if (!part->is_reclaiming) {
515 rc = reclaim_block(part, old_sector);
516 if (rc)
517 goto err;
519 block = find_free_block(part);
522 if (block == -1) {
523 rc = -ENOSPC;
524 goto err;
528 rc = mtd_read(part->mbd.mtd, part->blocks[block].offset,
529 part->header_size, &retlen,
530 (u_char *)part->header_cache);
532 if (!rc && retlen != part->header_size)
533 rc = -EIO;
535 if (rc) {
536 printk(KERN_ERR PREFIX "'%s': unable to read header at "
537 "0x%lx\n", part->mbd.mtd->name,
538 part->blocks[block].offset);
539 goto err;
542 part->current_block = block;
544 err:
545 return rc;
548 static int mark_sector_deleted(struct partition *part, u_long old_addr)
550 int block, offset, rc;
551 u_long addr;
552 size_t retlen;
553 u16 del = cpu_to_le16(SECTOR_DELETED);
555 block = old_addr / part->block_size;
556 offset = (old_addr % part->block_size) / SECTOR_SIZE -
557 part->header_sectors_per_block;
559 addr = part->blocks[block].offset +
560 (HEADER_MAP_OFFSET + offset) * sizeof(u16);
561 rc = mtd_write(part->mbd.mtd, addr, sizeof(del), &retlen,
562 (u_char *)&del);
564 if (!rc && retlen != sizeof(del))
565 rc = -EIO;
567 if (rc) {
568 printk(KERN_ERR PREFIX "error writing '%s' at "
569 "0x%lx\n", part->mbd.mtd->name, addr);
570 goto err;
572 if (block == part->current_block)
573 part->header_cache[offset + HEADER_MAP_OFFSET] = del;
575 part->blocks[block].used_sectors--;
577 if (!part->blocks[block].used_sectors &&
578 !part->blocks[block].free_sectors)
579 rc = erase_block(part, block);
581 err:
582 return rc;
585 static int find_free_sector(const struct partition *part, const struct block *block)
587 int i, stop;
589 i = stop = part->data_sectors_per_block - block->free_sectors;
591 do {
592 if (le16_to_cpu(part->header_cache[HEADER_MAP_OFFSET + i])
593 == SECTOR_FREE)
594 return i;
596 if (++i == part->data_sectors_per_block)
597 i = 0;
599 while(i != stop);
601 return -1;
604 static int do_writesect(struct mtd_blktrans_dev *dev, u_long sector, char *buf, ulong *old_addr)
606 struct partition *part = (struct partition*)dev;
607 struct block *block;
608 u_long addr;
609 int i;
610 int rc;
611 size_t retlen;
612 u16 entry;
614 if (part->current_block == -1 ||
615 !part->blocks[part->current_block].free_sectors) {
617 rc = find_writable_block(part, old_addr);
618 if (rc)
619 goto err;
622 block = &part->blocks[part->current_block];
624 i = find_free_sector(part, block);
626 if (i < 0) {
627 rc = -ENOSPC;
628 goto err;
631 addr = (i + part->header_sectors_per_block) * SECTOR_SIZE +
632 block->offset;
633 rc = mtd_write(part->mbd.mtd, addr, SECTOR_SIZE, &retlen,
634 (u_char *)buf);
636 if (!rc && retlen != SECTOR_SIZE)
637 rc = -EIO;
639 if (rc) {
640 printk(KERN_ERR PREFIX "error writing '%s' at 0x%lx\n",
641 part->mbd.mtd->name, addr);
642 goto err;
645 part->sector_map[sector] = addr;
647 entry = cpu_to_le16(sector == 0 ? SECTOR_ZERO : sector);
649 part->header_cache[i + HEADER_MAP_OFFSET] = entry;
651 addr = block->offset + (HEADER_MAP_OFFSET + i) * sizeof(u16);
652 rc = mtd_write(part->mbd.mtd, addr, sizeof(entry), &retlen,
653 (u_char *)&entry);
655 if (!rc && retlen != sizeof(entry))
656 rc = -EIO;
658 if (rc) {
659 printk(KERN_ERR PREFIX "error writing '%s' at 0x%lx\n",
660 part->mbd.mtd->name, addr);
661 goto err;
663 block->used_sectors++;
664 block->free_sectors--;
666 err:
667 return rc;
670 static int rfd_ftl_writesect(struct mtd_blktrans_dev *dev, u_long sector, char *buf)
672 struct partition *part = (struct partition*)dev;
673 u_long old_addr;
674 int i;
675 int rc = 0;
677 pr_debug("rfd_ftl_writesect(sector=0x%lx)\n", sector);
679 if (part->reserved_block == -1) {
680 rc = -EACCES;
681 goto err;
684 if (sector >= part->sector_count) {
685 rc = -EIO;
686 goto err;
689 old_addr = part->sector_map[sector];
691 for (i=0; i<SECTOR_SIZE; i++) {
692 if (!buf[i])
693 continue;
695 rc = do_writesect(dev, sector, buf, &old_addr);
696 if (rc)
697 goto err;
698 break;
701 if (i == SECTOR_SIZE)
702 part->sector_map[sector] = -1;
704 if (old_addr != -1)
705 rc = mark_sector_deleted(part, old_addr);
707 err:
708 return rc;
711 static int rfd_ftl_getgeo(struct mtd_blktrans_dev *dev, struct hd_geometry *geo)
713 struct partition *part = (struct partition*)dev;
715 geo->heads = 1;
716 geo->sectors = SECTORS_PER_TRACK;
717 geo->cylinders = part->cylinders;
719 return 0;
722 static void rfd_ftl_add_mtd(struct mtd_blktrans_ops *tr, struct mtd_info *mtd)
724 struct partition *part;
726 if (mtd->type != MTD_NORFLASH || mtd->size > UINT_MAX)
727 return;
729 part = kzalloc(sizeof(struct partition), GFP_KERNEL);
730 if (!part)
731 return;
733 part->mbd.mtd = mtd;
735 if (block_size)
736 part->block_size = block_size;
737 else {
738 if (!mtd->erasesize) {
739 printk(KERN_WARNING PREFIX "please provide block_size");
740 goto out;
741 } else
742 part->block_size = mtd->erasesize;
745 if (scan_header(part) == 0) {
746 part->mbd.size = part->sector_count;
747 part->mbd.tr = tr;
748 part->mbd.devnum = -1;
749 if (!(mtd->flags & MTD_WRITEABLE))
750 part->mbd.readonly = 1;
751 else if (part->errors) {
752 printk(KERN_WARNING PREFIX "'%s': errors found, "
753 "setting read-only\n", mtd->name);
754 part->mbd.readonly = 1;
757 printk(KERN_INFO PREFIX "name: '%s' type: %d flags %x\n",
758 mtd->name, mtd->type, mtd->flags);
760 if (!add_mtd_blktrans_dev((void*)part))
761 return;
763 out:
764 kfree(part);
767 static void rfd_ftl_remove_dev(struct mtd_blktrans_dev *dev)
769 struct partition *part = (struct partition*)dev;
770 int i;
772 for (i=0; i<part->total_blocks; i++) {
773 pr_debug("rfd_ftl_remove_dev:'%s': erase unit #%02d: %d erases\n",
774 part->mbd.mtd->name, i, part->blocks[i].erases);
777 del_mtd_blktrans_dev(dev);
778 vfree(part->sector_map);
779 kfree(part->header_cache);
780 kfree(part->blocks);
783 static struct mtd_blktrans_ops rfd_ftl_tr = {
784 .name = "rfd",
785 .major = RFD_FTL_MAJOR,
786 .part_bits = PART_BITS,
787 .blksize = SECTOR_SIZE,
789 .readsect = rfd_ftl_readsect,
790 .writesect = rfd_ftl_writesect,
791 .getgeo = rfd_ftl_getgeo,
792 .add_mtd = rfd_ftl_add_mtd,
793 .remove_dev = rfd_ftl_remove_dev,
794 .owner = THIS_MODULE,
797 static int __init init_rfd_ftl(void)
799 return register_mtd_blktrans(&rfd_ftl_tr);
802 static void __exit cleanup_rfd_ftl(void)
804 deregister_mtd_blktrans(&rfd_ftl_tr);
807 module_init(init_rfd_ftl);
808 module_exit(cleanup_rfd_ftl);
810 MODULE_LICENSE("GPL");
811 MODULE_AUTHOR("Sean Young <sean@mess.org>");
812 MODULE_DESCRIPTION("Support code for RFD Flash Translation Layer, "
813 "used by General Software's Embedded BIOS");