]> bbs.cooldavid.org Git - net-next-2.6.git/blob - drivers/mtd/rfd_ftl.c
490b4742ce3a1948a8920302aa79c2f9284474a8
[net-next-2.6.git] / drivers / mtd / rfd_ftl.c
1 /*
2  * rfd_ftl.c -- resident flash disk (flash translation layer)
3  *
4  * Copyright (C) 2005  Sean Young <sean@mess.org>
5  *
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:
8  *
9  *      http://www.gensw.com/pages/prod/bios/rfd.htm
10  *
11  * based on ftl.c
12  */
13
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>
21
22 #include <asm/types.h>
23
24 #define const_cpu_to_le16       __constant_cpu_to_le16
25
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");
29
30 #define PREFIX "rfd_ftl: "
31
32 /* This major has been assigned by device@lanana.org */
33 #ifndef RFD_FTL_MAJOR
34 #define RFD_FTL_MAJOR           256
35 #endif
36
37 /* Maximum number of partitions in an FTL region */
38 #define PART_BITS               4
39
40 /* An erase unit should start with this value */
41 #define RFD_MAGIC               0x9193
42
43 /* the second value is 0xffff or 0xffc8; function unknown */
44
45 /* the third value is always 0xffff, ignored */
46
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
52
53 #define SECTOR_SIZE             512
54
55 #define SECTORS_PER_TRACK       63
56
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;
69 };
70
71 struct partition {
72         struct mtd_blktrans_dev mbd;
73
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 */
83
84         int is_reclaiming;
85         int cylinders;
86         int errors;
87         u_long *sector_map;
88         struct block *blocks;
89 };
90
91 static int rfd_ftl_writesect(struct mtd_blktrans_dev *dev, u_long sector, char *buf);
92
93 static int build_block_map(struct partition *part, int block_no)
94 {
95         struct block *block = &part->blocks[block_no];
96         int i;
97
98         block->offset = part->block_size * block_no;
99
100         if (le16_to_cpu(part->header_cache[0]) != RFD_MAGIC) {
101                 block->state = BLOCK_UNUSED;
102                 return -ENOENT;
103         }
104
105         block->state = BLOCK_OK;
106
107         for (i=0; i<part->data_sectors_per_block; i++) {
108                 u16 entry;
109
110                 entry = le16_to_cpu(part->header_cache[HEADER_MAP_OFFSET + i]);
111
112                 if (entry == SECTOR_DELETED)
113                         continue;
114
115                 if (entry == SECTOR_FREE) {
116                         block->free_sectors++;
117                         continue;
118                 }
119
120                 if (entry == SECTOR_ZERO)
121                         entry = 0;
122
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;
129                 }
130
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;
137                 }
138
139                 part->sector_map[entry] = block->offset +
140                         (i + part->header_sectors_per_block) * SECTOR_SIZE;
141
142                 block->used_sectors++;
143         }
144
145         if (block->free_sectors == part->data_sectors_per_block)
146                 part->reserved_block = block_no;
147
148         return 0;
149 }
150
151 static int scan_header(struct partition *part)
152 {
153         int sectors_per_block;
154         int i, rc = -ENOMEM;
155         int blocks_found;
156         size_t retlen;
157
158         sectors_per_block = part->block_size / SECTOR_SIZE;
159         part->total_blocks = (u32)part->mbd.mtd->size / part->block_size;
160
161         if (part->total_blocks < 2)
162                 return -ENOENT;
163
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;
168
169         part->data_sectors_per_block = sectors_per_block -
170                         part->header_sectors_per_block;
171
172         part->header_size = (HEADER_MAP_OFFSET +
173                         part->data_sectors_per_block) * sizeof(u16);
174
175         part->cylinders = (part->data_sectors_per_block *
176                         (part->total_blocks - 1) - 1) / SECTORS_PER_TRACK;
177
178         part->sector_count = part->cylinders * SECTORS_PER_TRACK;
179
180         part->current_block = -1;
181         part->reserved_block = -1;
182         part->is_reclaiming = 0;
183
184         part->header_cache = kmalloc(part->header_size, GFP_KERNEL);
185         if (!part->header_cache)
186                 goto err;
187
188         part->blocks = kcalloc(part->total_blocks, sizeof(struct block),
189                         GFP_KERNEL);
190         if (!part->blocks)
191                 goto err;
192
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);
197                 goto err;
198         }
199
200         for (i=0; i<part->sector_count; i++)
201                 part->sector_map[i] = -1;
202
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);
207
208                 if (!rc && retlen != part->header_size)
209                         rc = -EIO;
210
211                 if (rc)
212                         goto err;
213
214                 if (!build_block_map(part, i))
215                         blocks_found++;
216         }
217
218         if (blocks_found == 0) {
219                 printk(KERN_NOTICE PREFIX "no RFD magic found in '%s'\n",
220                                 part->mbd.mtd->name);
221                 rc = -ENOENT;
222                 goto err;
223         }
224
225         if (part->reserved_block == -1) {
226                 printk(KERN_WARNING PREFIX "'%s': no empty erase unit found\n",
227                                 part->mbd.mtd->name);
228
229                 part->errors = 1;
230         }
231
232         return 0;
233
234 err:
235         vfree(part->sector_map);
236         kfree(part->header_cache);
237         kfree(part->blocks);
238
239         return rc;
240 }
241
242 static int rfd_ftl_readsect(struct mtd_blktrans_dev *dev, u_long sector, char *buf)
243 {
244         struct partition *part = (struct partition*)dev;
245         u_long addr;
246         size_t retlen;
247         int rc;
248
249         if (sector >= part->sector_count)
250                 return -EIO;
251
252         addr = part->sector_map[sector];
253         if (addr != -1) {
254                 rc = part->mbd.mtd->read(part->mbd.mtd, addr, SECTOR_SIZE,
255                                                 &retlen, (u_char*)buf);
256                 if (!rc && retlen != SECTOR_SIZE)
257                         rc = -EIO;
258
259                 if (rc) {
260                         printk(KERN_WARNING PREFIX "error reading '%s' at "
261                                 "0x%lx\n", part->mbd.mtd->name, addr);
262                         return rc;
263                 }
264         } else
265                 memset(buf, 0, SECTOR_SIZE);
266
267         return 0;
268 }
269
270 static void erase_callback(struct erase_info *erase)
271 {
272         struct partition *part;
273         u16 magic;
274         int i, rc;
275         size_t retlen;
276
277         part = (struct partition*)erase->priv;
278
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);
284                 return;
285         }
286
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);
291
292                 part->blocks[i].state = BLOCK_FAILED;
293                 part->blocks[i].free_sectors = 0;
294                 part->blocks[i].used_sectors = 0;
295
296                 kfree(erase);
297
298                 return;
299         }
300
301         magic = const_cpu_to_le16(RFD_MAGIC);
302
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++;
307
308         rc = part->mbd.mtd->write(part->mbd.mtd,
309                 part->blocks[i].offset, sizeof(magic), &retlen,
310                 (u_char*)&magic);
311
312         if (!rc && retlen != sizeof(magic))
313                 rc = -EIO;
314
315         if (rc) {
316                 printk(KERN_ERR PREFIX "'%s': unable to write RFD "
317                                 "header at 0x%lx\n",
318                                 part->mbd.mtd->name,
319                                 part->blocks[i].offset);
320                 part->blocks[i].state = BLOCK_FAILED;
321         }
322         else
323                 part->blocks[i].state = BLOCK_OK;
324
325         kfree(erase);
326 }
327
328 static int erase_block(struct partition *part, int block)
329 {
330         struct erase_info *erase;
331         int rc = -ENOMEM;
332
333         erase = kmalloc(sizeof(struct erase_info), GFP_KERNEL);
334         if (!erase)
335                 goto err;
336
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;
342
343         part->blocks[block].state = BLOCK_ERASING;
344         part->blocks[block].free_sectors = 0;
345
346         rc = part->mbd.mtd->erase(part->mbd.mtd, erase);
347
348         if (rc) {
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);
352                 kfree(erase);
353         }
354
355 err:
356         return rc;
357 }
358
359 static int move_block_contents(struct partition *part, int block_no, u_long *old_sector)
360 {
361         void *sector_data;
362         u16 *map;
363         size_t retlen;
364         int i, rc = -ENOMEM;
365
366         part->is_reclaiming = 1;
367
368         sector_data = kmalloc(SECTOR_SIZE, GFP_KERNEL);
369         if (!sector_data)
370                 goto err3;
371
372         map = kmalloc(part->header_size, GFP_KERNEL);
373         if (!map)
374                 goto err2;
375
376         rc = part->mbd.mtd->read(part->mbd.mtd,
377                 part->blocks[block_no].offset, part->header_size,
378                 &retlen, (u_char*)map);
379
380         if (!rc && retlen != part->header_size)
381                 rc = -EIO;
382
383         if (rc) {
384                 printk(KERN_ERR PREFIX "error reading '%s' at "
385                         "0x%lx\n", part->mbd.mtd->name,
386                         part->blocks[block_no].offset);
387
388                 goto err;
389         }
390
391         for (i=0; i<part->data_sectors_per_block; i++) {
392                 u16 entry = le16_to_cpu(map[HEADER_MAP_OFFSET + i]);
393                 u_long addr;
394
395
396                 if (entry == SECTOR_FREE || entry == SECTOR_DELETED)
397                         continue;
398
399                 if (entry == SECTOR_ZERO)
400                         entry = 0;
401
402                 /* already warned about and ignored in build_block_map() */
403                 if (entry >= part->sector_count)
404                         continue;
405
406                 addr = part->blocks[block_no].offset +
407                         (i + part->header_sectors_per_block) * SECTOR_SIZE;
408
409                 if (*old_sector == addr) {
410                         *old_sector = -1;
411                         if (!part->blocks[block_no].used_sectors--) {
412                                 rc = erase_block(part, block_no);
413                                 break;
414                         }
415                         continue;
416                 }
417                 rc = part->mbd.mtd->read(part->mbd.mtd, addr,
418                         SECTOR_SIZE, &retlen, sector_data);
419
420                 if (!rc && retlen != SECTOR_SIZE)
421                         rc = -EIO;
422
423                 if (rc) {
424                         printk(KERN_ERR PREFIX "'%s': Unable to "
425                                 "read sector for relocation\n",
426                                 part->mbd.mtd->name);
427
428                         goto err;
429                 }
430
431                 rc = rfd_ftl_writesect((struct mtd_blktrans_dev*)part,
432                                 entry, sector_data);
433
434                 if (rc)
435                         goto err;
436         }
437
438 err:
439         kfree(map);
440 err2:
441         kfree(sector_data);
442 err3:
443         part->is_reclaiming = 0;
444
445         return rc;
446 }
447
448 static int reclaim_block(struct partition *part, u_long *old_sector)
449 {
450         int block, best_block, score, old_sector_block;
451         int rc;
452
453         /* we have a race if sync doesn't exist */
454         if (part->mbd.mtd->sync)
455                 part->mbd.mtd->sync(part->mbd.mtd);
456
457         score = 0x7fffffff; /* MAX_INT */
458         best_block = -1;
459         if (*old_sector != -1)
460                 old_sector_block = *old_sector / part->block_size;
461         else
462                 old_sector_block = -1;
463
464         for (block=0; block<part->total_blocks; block++) {
465                 int this_score;
466
467                 if (block == part->reserved_block)
468                         continue;
469
470                 /*
471                  * Postpone reclaiming if there is a free sector as
472                  * more removed sectors is more efficient (have to move
473                  * less).
474                  */
475                 if (part->blocks[block].free_sectors)
476                         return 0;
477
478                 this_score = part->blocks[block].used_sectors;
479
480                 if (block == old_sector_block)
481                         this_score--;
482                 else {
483                         /* no point in moving a full block */
484                         if (part->blocks[block].used_sectors ==
485                                         part->data_sectors_per_block)
486                                 continue;
487                 }
488
489                 this_score += part->blocks[block].erases;
490
491                 if (this_score < score) {
492                         best_block = block;
493                         score = this_score;
494                 }
495         }
496
497         if (best_block == -1)
498                 return -ENOSPC;
499
500         part->current_block = -1;
501         part->reserved_block = best_block;
502
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);
507
508         if (part->blocks[best_block].used_sectors)
509                 rc = move_block_contents(part, best_block, old_sector);
510         else
511                 rc = erase_block(part, best_block);
512
513         return rc;
514 }
515
516 /*
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.
520  */
521 static int find_free_block(struct partition *part)
522 {
523         int block, stop;
524
525         block = part->current_block == -1 ?
526                         jiffies % part->total_blocks : part->current_block;
527         stop = block;
528
529         do {
530                 if (part->blocks[block].free_sectors &&
531                                 block != part->reserved_block)
532                         return block;
533
534                 if (part->blocks[block].state == BLOCK_UNUSED)
535                         erase_block(part, block);
536
537                 if (++block >= part->total_blocks)
538                         block = 0;
539
540         } while (block != stop);
541
542         return -1;
543 }
544
545 static int find_writable_block(struct partition *part, u_long *old_sector)
546 {
547         int rc, block;
548         size_t retlen;
549
550         block = find_free_block(part);
551
552         if (block == -1) {
553                 if (!part->is_reclaiming) {
554                         rc = reclaim_block(part, old_sector);
555                         if (rc)
556                                 goto err;
557
558                         block = find_free_block(part);
559                 }
560
561                 if (block == -1) {
562                         rc = -ENOSPC;
563                         goto err;
564                 }
565         }
566
567         rc = part->mbd.mtd->read(part->mbd.mtd, part->blocks[block].offset,
568                 part->header_size, &retlen, (u_char*)part->header_cache);
569
570         if (!rc && retlen != part->header_size)
571                 rc = -EIO;
572
573         if (rc) {
574                 printk(KERN_ERR PREFIX "'%s': unable to read header at "
575                                 "0x%lx\n", part->mbd.mtd->name,
576                                 part->blocks[block].offset);
577                 goto err;
578         }
579
580         part->current_block = block;
581
582 err:
583         return rc;
584 }
585
586 static int mark_sector_deleted(struct partition *part, u_long old_addr)
587 {
588         int block, offset, rc;
589         u_long addr;
590         size_t retlen;
591         u16 del = const_cpu_to_le16(SECTOR_DELETED);
592
593         block = old_addr / part->block_size;
594         offset = (old_addr % part->block_size) / SECTOR_SIZE -
595                 part->header_sectors_per_block;
596
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);
601
602         if (!rc && retlen != sizeof(del))
603                 rc = -EIO;
604
605         if (rc) {
606                 printk(KERN_ERR PREFIX "error writing '%s' at "
607                         "0x%lx\n", part->mbd.mtd->name, addr);
608                 if (rc)
609                         goto err;
610         }
611         if (block == part->current_block)
612                 part->header_cache[offset + HEADER_MAP_OFFSET] = del;
613
614         part->blocks[block].used_sectors--;
615
616         if (!part->blocks[block].used_sectors &&
617             !part->blocks[block].free_sectors)
618                 rc = erase_block(part, block);
619
620 err:
621         return rc;
622 }
623
624 static int find_free_sector(const struct partition *part, const struct block *block)
625 {
626         int i, stop;
627
628         i = stop = part->data_sectors_per_block - block->free_sectors;
629
630         do {
631                 if (le16_to_cpu(part->header_cache[HEADER_MAP_OFFSET + i])
632                                 == SECTOR_FREE)
633                         return i;
634
635                 if (++i == part->data_sectors_per_block)
636                         i = 0;
637         }
638         while(i != stop);
639
640         return -1;
641 }
642
643 static int do_writesect(struct mtd_blktrans_dev *dev, u_long sector, char *buf, ulong *old_addr)
644 {
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;
652
653         if (part->current_block == -1 ||
654                 !part->blocks[part->current_block].free_sectors) {
655
656                 rc = find_writable_block(part, old_addr);
657                 if (rc)
658                         goto err;
659         }
660
661         block = &part->blocks[part->current_block];
662
663         i = find_free_sector(part, block);
664
665         if (i < 0) {
666                 rc = -ENOSPC;
667                 goto err;
668         }
669
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);
674
675         if (!rc && retlen != SECTOR_SIZE)
676                 rc = -EIO;
677
678         if (rc) {
679                 printk(KERN_ERR PREFIX "error writing '%s' at 0x%lx\n",
680                                 part->mbd.mtd->name, addr);
681                 if (rc)
682                         goto err;
683         }
684
685         part->sector_map[sector] = addr;
686
687         entry = cpu_to_le16(sector == 0 ? SECTOR_ZERO : sector);
688
689         part->header_cache[i + HEADER_MAP_OFFSET] = entry;
690
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);
694
695         if (!rc && retlen != sizeof(entry))
696                 rc = -EIO;
697
698         if (rc) {
699                 printk(KERN_ERR PREFIX "error writing '%s' at 0x%lx\n",
700                                 part->mbd.mtd->name, addr);
701                 if (rc)
702                         goto err;
703         }
704         block->used_sectors++;
705         block->free_sectors--;
706
707 err:
708         return rc;
709 }
710
711 static int rfd_ftl_writesect(struct mtd_blktrans_dev *dev, u_long sector, char *buf)
712 {
713         struct partition *part = (struct partition*)dev;
714         u_long old_addr;
715         int i;
716         int rc = 0;
717
718         pr_debug("rfd_ftl_writesect(sector=0x%lx)\n", sector);
719
720         if (part->reserved_block == -1) {
721                 rc = -EACCES;
722                 goto err;
723         }
724
725         if (sector >= part->sector_count) {
726                 rc = -EIO;
727                 goto err;
728         }
729
730         old_addr = part->sector_map[sector];
731
732         for (i=0; i<SECTOR_SIZE; i++) {
733                 if (!buf[i])
734                         continue;
735
736                 rc = do_writesect(dev, sector, buf, &old_addr);
737                 if (rc)
738                         goto err;
739                 break;
740         }
741
742         if (i == SECTOR_SIZE)
743                 part->sector_map[sector] = -1;
744
745         if (old_addr != -1)
746                 rc = mark_sector_deleted(part, old_addr);
747
748 err:
749         return rc;
750 }
751
752 static int rfd_ftl_getgeo(struct mtd_blktrans_dev *dev, struct hd_geometry *geo)
753 {
754         struct partition *part = (struct partition*)dev;
755
756         geo->heads = 1;
757         geo->sectors = SECTORS_PER_TRACK;
758         geo->cylinders = part->cylinders;
759
760         return 0;
761 }
762
763 static void rfd_ftl_add_mtd(struct mtd_blktrans_ops *tr, struct mtd_info *mtd)
764 {
765         struct partition *part;
766
767         if (mtd->type != MTD_NORFLASH || mtd->size > UINT_MAX)
768                 return;
769
770         part = kzalloc(sizeof(struct partition), GFP_KERNEL);
771         if (!part)
772                 return;
773
774         part->mbd.mtd = mtd;
775
776         if (block_size)
777                 part->block_size = block_size;
778         else {
779                 if (!mtd->erasesize) {
780                         printk(KERN_WARNING PREFIX "please provide block_size");
781                         goto out;
782                 } else
783                         part->block_size = mtd->erasesize;
784         }
785
786         if (scan_header(part) == 0) {
787                 part->mbd.size = part->sector_count;
788                 part->mbd.tr = tr;
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;
796                 }
797
798                 printk(KERN_INFO PREFIX "name: '%s' type: %d flags %x\n",
799                                 mtd->name, mtd->type, mtd->flags);
800
801                 if (!add_mtd_blktrans_dev((void*)part))
802                         return;
803         }
804 out:
805         kfree(part);
806 }
807
808 static void rfd_ftl_remove_dev(struct mtd_blktrans_dev *dev)
809 {
810         struct partition *part = (struct partition*)dev;
811         int i;
812
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);
816         }
817
818         del_mtd_blktrans_dev(dev);
819         vfree(part->sector_map);
820         kfree(part->header_cache);
821         kfree(part->blocks);
822         kfree(part);
823 }
824
825 static struct mtd_blktrans_ops rfd_ftl_tr = {
826         .name           = "rfd",
827         .major          = RFD_FTL_MAJOR,
828         .part_bits      = PART_BITS,
829         .blksize        = SECTOR_SIZE,
830
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,
837 };
838
839 static int __init init_rfd_ftl(void)
840 {
841         return register_mtd_blktrans(&rfd_ftl_tr);
842 }
843
844 static void __exit cleanup_rfd_ftl(void)
845 {
846         deregister_mtd_blktrans(&rfd_ftl_tr);
847 }
848
849 module_init(init_rfd_ftl);
850 module_exit(cleanup_rfd_ftl);
851
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");
856