]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - drivers/mtd/rfd_ftl.c
mtd: harmonize mtd_writev usage
[mirror_ubuntu-bionic-kernel.git] / drivers / mtd / rfd_ftl.c
CommitLineData
e27a9960
SY
1/*
2 * rfd_ftl.c -- resident flash disk (flash translation layer)
3 *
a1452a37 4 * Copyright © 2005 Sean Young <sean@mess.org>
e27a9960 5 *
e27a9960
SY
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>
de25968c 19#include <linux/slab.h>
b80b5832 20#include <linux/jiffies.h>
a0e5cc58 21#include <linux/module.h>
e27a9960
SY
22
23#include <asm/types.h>
24
e27a9960
SY
25static int block_size = 0;
26module_param(block_size, int, 0);
27MODULE_PARM_DESC(block_size, "Block size to use by RFD, defaults to erase unit size");
28
29#define PREFIX "rfd_ftl: "
30
bc4117f8 31/* This major has been assigned by device@lanana.org */
e27a9960 32#ifndef RFD_FTL_MAJOR
bc4117f8 33#define RFD_FTL_MAJOR 256
e27a9960
SY
34#endif
35
36/* Maximum number of partitions in an FTL region */
37#define PART_BITS 4
38
39/* An erase unit should start with this value */
40#define RFD_MAGIC 0x9193
41
42/* the second value is 0xffff or 0xffc8; function unknown */
43
44/* the third value is always 0xffff, ignored */
45
46/* next is an array of mapping for each corresponding sector */
47#define HEADER_MAP_OFFSET 3
48#define SECTOR_DELETED 0x0000
49#define SECTOR_ZERO 0xfffe
50#define SECTOR_FREE 0xffff
51
52#define SECTOR_SIZE 512
53
54#define SECTORS_PER_TRACK 63
55
56struct block {
57 enum {
58 BLOCK_OK,
59 BLOCK_ERASING,
60 BLOCK_ERASED,
683b30c8 61 BLOCK_UNUSED,
e27a9960
SY
62 BLOCK_FAILED
63 } state;
64 int free_sectors;
65 int used_sectors;
66 int erases;
67 u_long offset;
68};
69
70struct partition {
71 struct mtd_blktrans_dev mbd;
72
73 u_int block_size; /* size of erase unit */
74 u_int total_blocks; /* number of erase units */
75 u_int header_sectors_per_block; /* header sectors in erase unit */
76 u_int data_sectors_per_block; /* data sectors in erase unit */
77 u_int sector_count; /* sectors in translated disk */
78 u_int header_size; /* bytes in header sector */
79 int reserved_block; /* block next up for reclaim */
80 int current_block; /* block to write to */
81 u16 *header_cache; /* cached header */
82
83 int is_reclaiming;
84 int cylinders;
85 int errors;
86 u_long *sector_map;
87 struct block *blocks;
88};
89
90static int rfd_ftl_writesect(struct mtd_blktrans_dev *dev, u_long sector, char *buf);
91
92static int build_block_map(struct partition *part, int block_no)
93{
94 struct block *block = &part->blocks[block_no];
95 int i;
97894cda 96
e27a9960
SY
97 block->offset = part->block_size * block_no;
98
99 if (le16_to_cpu(part->header_cache[0]) != RFD_MAGIC) {
683b30c8
SY
100 block->state = BLOCK_UNUSED;
101 return -ENOENT;
e27a9960
SY
102 }
103
104 block->state = BLOCK_OK;
105
106 for (i=0; i<part->data_sectors_per_block; i++) {
107 u16 entry;
97894cda 108
e27a9960
SY
109 entry = le16_to_cpu(part->header_cache[HEADER_MAP_OFFSET + i]);
110
111 if (entry == SECTOR_DELETED)
112 continue;
97894cda 113
e27a9960
SY
114 if (entry == SECTOR_FREE) {
115 block->free_sectors++;
116 continue;
117 }
118
119 if (entry == SECTOR_ZERO)
120 entry = 0;
97894cda 121
e27a9960 122 if (entry >= part->sector_count) {
683b30c8 123 printk(KERN_WARNING PREFIX
e27a9960
SY
124 "'%s': unit #%d: entry %d corrupt, "
125 "sector %d out of range\n",
126 part->mbd.mtd->name, block_no, i, entry);
127 continue;
128 }
129
130 if (part->sector_map[entry] != -1) {
683b30c8 131 printk(KERN_WARNING PREFIX
e27a9960
SY
132 "'%s': more than one entry for sector %d\n",
133 part->mbd.mtd->name, entry);
134 part->errors = 1;
135 continue;
136 }
137
97894cda 138 part->sector_map[entry] = block->offset +
e27a9960
SY
139 (i + part->header_sectors_per_block) * SECTOR_SIZE;
140
141 block->used_sectors++;
142 }
143
144 if (block->free_sectors == part->data_sectors_per_block)
145 part->reserved_block = block_no;
146
147 return 0;
148}
149
150static int scan_header(struct partition *part)
151{
152 int sectors_per_block;
153 int i, rc = -ENOMEM;
154 int blocks_found;
155 size_t retlen;
156
157 sectors_per_block = part->block_size / SECTOR_SIZE;
69423d99 158 part->total_blocks = (u32)part->mbd.mtd->size / part->block_size;
e27a9960
SY
159
160 if (part->total_blocks < 2)
161 return -ENOENT;
162
163 /* each erase block has three bytes header, followed by the map */
97894cda
TG
164 part->header_sectors_per_block =
165 ((HEADER_MAP_OFFSET + sectors_per_block) *
683b30c8 166 sizeof(u16) + SECTOR_SIZE - 1) / SECTOR_SIZE;
e27a9960 167
97894cda 168 part->data_sectors_per_block = sectors_per_block -
e27a9960
SY
169 part->header_sectors_per_block;
170
97894cda 171 part->header_size = (HEADER_MAP_OFFSET +
e27a9960
SY
172 part->data_sectors_per_block) * sizeof(u16);
173
174 part->cylinders = (part->data_sectors_per_block *
175 (part->total_blocks - 1) - 1) / SECTORS_PER_TRACK;
176
177 part->sector_count = part->cylinders * SECTORS_PER_TRACK;
178
179 part->current_block = -1;
180 part->reserved_block = -1;
181 part->is_reclaiming = 0;
182
183 part->header_cache = kmalloc(part->header_size, GFP_KERNEL);
184 if (!part->header_cache)
185 goto err;
186
97894cda 187 part->blocks = kcalloc(part->total_blocks, sizeof(struct block),
e27a9960
SY
188 GFP_KERNEL);
189 if (!part->blocks)
190 goto err;
191
192 part->sector_map = vmalloc(part->sector_count * sizeof(u_long));
193 if (!part->sector_map) {
194 printk(KERN_ERR PREFIX "'%s': unable to allocate memory for "
195 "sector map", part->mbd.mtd->name);
196 goto err;
197 }
198
97894cda 199 for (i=0; i<part->sector_count; i++)
e27a9960
SY
200 part->sector_map[i] = -1;
201
202 for (i=0, blocks_found=0; i<part->total_blocks; i++) {
329ad399
AB
203 rc = mtd_read(part->mbd.mtd, i * part->block_size,
204 part->header_size, &retlen,
205 (u_char *)part->header_cache);
e27a9960
SY
206
207 if (!rc && retlen != part->header_size)
208 rc = -EIO;
209
97894cda 210 if (rc)
e27a9960
SY
211 goto err;
212
213 if (!build_block_map(part, i))
214 blocks_found++;
215 }
216
217 if (blocks_found == 0) {
218 printk(KERN_NOTICE PREFIX "no RFD magic found in '%s'\n",
219 part->mbd.mtd->name);
220 rc = -ENOENT;
221 goto err;
222 }
223
224 if (part->reserved_block == -1) {
683b30c8 225 printk(KERN_WARNING PREFIX "'%s': no empty erase unit found\n",
e27a9960
SY
226 part->mbd.mtd->name);
227
228 part->errors = 1;
229 }
230
231 return 0;
232
233err:
234 vfree(part->sector_map);
235 kfree(part->header_cache);
236 kfree(part->blocks);
237
238 return rc;
239}
240
241static int rfd_ftl_readsect(struct mtd_blktrans_dev *dev, u_long sector, char *buf)
242{
243 struct partition *part = (struct partition*)dev;
244 u_long addr;
245 size_t retlen;
246 int rc;
97894cda 247
e27a9960
SY
248 if (sector >= part->sector_count)
249 return -EIO;
250
251 addr = part->sector_map[sector];
252 if (addr != -1) {
329ad399
AB
253 rc = mtd_read(part->mbd.mtd, addr, SECTOR_SIZE, &retlen,
254 (u_char *)buf);
e27a9960
SY
255 if (!rc && retlen != SECTOR_SIZE)
256 rc = -EIO;
257
258 if (rc) {
259 printk(KERN_WARNING PREFIX "error reading '%s' at "
260 "0x%lx\n", part->mbd.mtd->name, addr);
261 return rc;
262 }
263 } else
264 memset(buf, 0, SECTOR_SIZE);
97894cda 265
e27a9960 266 return 0;
97894cda 267}
e27a9960
SY
268
269static void erase_callback(struct erase_info *erase)
270{
271 struct partition *part;
272 u16 magic;
273 int i, rc;
274 size_t retlen;
275
276 part = (struct partition*)erase->priv;
277
69423d99
AH
278 i = (u32)erase->addr / part->block_size;
279 if (i >= part->total_blocks || part->blocks[i].offset != erase->addr ||
280 erase->addr > UINT_MAX) {
281 printk(KERN_ERR PREFIX "erase callback for unknown offset %llx "
282 "on '%s'\n", (unsigned long long)erase->addr, part->mbd.mtd->name);
e27a9960
SY
283 return;
284 }
285
286 if (erase->state != MTD_ERASE_DONE) {
69423d99
AH
287 printk(KERN_WARNING PREFIX "erase failed at 0x%llx on '%s', "
288 "state %d\n", (unsigned long long)erase->addr,
e27a9960
SY
289 part->mbd.mtd->name, erase->state);
290
291 part->blocks[i].state = BLOCK_FAILED;
292 part->blocks[i].free_sectors = 0;
293 part->blocks[i].used_sectors = 0;
294
295 kfree(erase);
296
297 return;
298 }
299
c80a7b26 300 magic = cpu_to_le16(RFD_MAGIC);
e27a9960
SY
301
302 part->blocks[i].state = BLOCK_ERASED;
303 part->blocks[i].free_sectors = part->data_sectors_per_block;
304 part->blocks[i].used_sectors = 0;
305 part->blocks[i].erases++;
306
eda95cbf
AB
307 rc = mtd_write(part->mbd.mtd, part->blocks[i].offset, sizeof(magic),
308 &retlen, (u_char *)&magic);
97894cda 309
e27a9960
SY
310 if (!rc && retlen != sizeof(magic))
311 rc = -EIO;
312
313 if (rc) {
683b30c8 314 printk(KERN_ERR PREFIX "'%s': unable to write RFD "
e27a9960 315 "header at 0x%lx\n",
97894cda 316 part->mbd.mtd->name,
e27a9960
SY
317 part->blocks[i].offset);
318 part->blocks[i].state = BLOCK_FAILED;
319 }
320 else
321 part->blocks[i].state = BLOCK_OK;
322
323 kfree(erase);
324}
325
326static int erase_block(struct partition *part, int block)
327{
328 struct erase_info *erase;
329 int rc = -ENOMEM;
330
331 erase = kmalloc(sizeof(struct erase_info), GFP_KERNEL);
332 if (!erase)
333 goto err;
334
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;
340
341 part->blocks[block].state = BLOCK_ERASING;
342 part->blocks[block].free_sectors = 0;
343
7e1f0dc0 344 rc = mtd_erase(part->mbd.mtd, erase);
e27a9960
SY
345
346 if (rc) {
69423d99
AH
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);
e27a9960
SY
350 kfree(erase);
351 }
352
353err:
354 return rc;
355}
356
357static int move_block_contents(struct partition *part, int block_no, u_long *old_sector)
358{
359 void *sector_data;
360 u16 *map;
361 size_t retlen;
362 int i, rc = -ENOMEM;
363
364 part->is_reclaiming = 1;
365
366 sector_data = kmalloc(SECTOR_SIZE, GFP_KERNEL);
367 if (!sector_data)
368 goto err3;
369
370 map = kmalloc(part->header_size, GFP_KERNEL);
371 if (!map)
372 goto err2;
97894cda 373
329ad399
AB
374 rc = mtd_read(part->mbd.mtd, part->blocks[block_no].offset,
375 part->header_size, &retlen, (u_char *)map);
97894cda 376
e27a9960
SY
377 if (!rc && retlen != part->header_size)
378 rc = -EIO;
379
380 if (rc) {
683b30c8 381 printk(KERN_ERR PREFIX "error reading '%s' at "
97894cda 382 "0x%lx\n", part->mbd.mtd->name,
e27a9960
SY
383 part->blocks[block_no].offset);
384
385 goto err;
386 }
387
388 for (i=0; i<part->data_sectors_per_block; i++) {
389 u16 entry = le16_to_cpu(map[HEADER_MAP_OFFSET + i]);
390 u_long addr;
391
392
393 if (entry == SECTOR_FREE || entry == SECTOR_DELETED)
394 continue;
395
97894cda 396 if (entry == SECTOR_ZERO)
e27a9960
SY
397 entry = 0;
398
399 /* already warned about and ignored in build_block_map() */
97894cda 400 if (entry >= part->sector_count)
e27a9960
SY
401 continue;
402
403 addr = part->blocks[block_no].offset +
404 (i + part->header_sectors_per_block) * SECTOR_SIZE;
405
406 if (*old_sector == addr) {
407 *old_sector = -1;
408 if (!part->blocks[block_no].used_sectors--) {
409 rc = erase_block(part, block_no);
410 break;
411 }
412 continue;
413 }
329ad399
AB
414 rc = mtd_read(part->mbd.mtd, addr, SECTOR_SIZE, &retlen,
415 sector_data);
97894cda 416
e27a9960
SY
417 if (!rc && retlen != SECTOR_SIZE)
418 rc = -EIO;
419
420 if (rc) {
683b30c8 421 printk(KERN_ERR PREFIX "'%s': Unable to "
e27a9960
SY
422 "read sector for relocation\n",
423 part->mbd.mtd->name);
424
425 goto err;
426 }
97894cda 427
e27a9960
SY
428 rc = rfd_ftl_writesect((struct mtd_blktrans_dev*)part,
429 entry, sector_data);
97894cda
TG
430
431 if (rc)
e27a9960
SY
432 goto err;
433 }
434
435err:
436 kfree(map);
437err2:
438 kfree(sector_data);
439err3:
440 part->is_reclaiming = 0;
441
442 return rc;
443}
444
97894cda 445static int reclaim_block(struct partition *part, u_long *old_sector)
e27a9960
SY
446{
447 int block, best_block, score, old_sector_block;
448 int rc;
97894cda 449
e27a9960
SY
450 /* we have a race if sync doesn't exist */
451 if (part->mbd.mtd->sync)
85f2f2a8 452 mtd_sync(part->mbd.mtd);
e27a9960
SY
453
454 score = 0x7fffffff; /* MAX_INT */
455 best_block = -1;
456 if (*old_sector != -1)
457 old_sector_block = *old_sector / part->block_size;
458 else
459 old_sector_block = -1;
460
461 for (block=0; block<part->total_blocks; block++) {
462 int this_score;
463
464 if (block == part->reserved_block)
465 continue;
466
467 /*
468 * Postpone reclaiming if there is a free sector as
469 * more removed sectors is more efficient (have to move
470 * less).
471 */
97894cda 472 if (part->blocks[block].free_sectors)
e27a9960
SY
473 return 0;
474
475 this_score = part->blocks[block].used_sectors;
476
97894cda 477 if (block == old_sector_block)
e27a9960
SY
478 this_score--;
479 else {
480 /* no point in moving a full block */
97894cda 481 if (part->blocks[block].used_sectors ==
e27a9960
SY
482 part->data_sectors_per_block)
483 continue;
484 }
485
486 this_score += part->blocks[block].erases;
487
488 if (this_score < score) {
489 best_block = block;
490 score = this_score;
491 }
492 }
493
494 if (best_block == -1)
495 return -ENOSPC;
496
497 part->current_block = -1;
498 part->reserved_block = best_block;
499
500 pr_debug("reclaim_block: reclaiming block #%d with %d used "
501 "%d free sectors\n", best_block,
502 part->blocks[best_block].used_sectors,
503 part->blocks[best_block].free_sectors);
504
505 if (part->blocks[best_block].used_sectors)
506 rc = move_block_contents(part, best_block, old_sector);
507 else
508 rc = erase_block(part, best_block);
509
510 return rc;
511}
512
513/*
514 * IMPROVE: It would be best to choose the block with the most deleted sectors,
515 * because if we fill that one up first it'll have the most chance of having
516 * the least live sectors at reclaim.
517 */
683b30c8 518static int find_free_block(struct partition *part)
e27a9960
SY
519{
520 int block, stop;
521
522 block = part->current_block == -1 ?
523 jiffies % part->total_blocks : part->current_block;
524 stop = block;
525
526 do {
97894cda 527 if (part->blocks[block].free_sectors &&
e27a9960
SY
528 block != part->reserved_block)
529 return block;
530
683b30c8
SY
531 if (part->blocks[block].state == BLOCK_UNUSED)
532 erase_block(part, block);
533
e27a9960
SY
534 if (++block >= part->total_blocks)
535 block = 0;
536
537 } while (block != stop);
538
539 return -1;
540}
541
683b30c8 542static int find_writable_block(struct partition *part, u_long *old_sector)
e27a9960
SY
543{
544 int rc, block;
545 size_t retlen;
546
547 block = find_free_block(part);
548
549 if (block == -1) {
550 if (!part->is_reclaiming) {
551 rc = reclaim_block(part, old_sector);
552 if (rc)
553 goto err;
554
555 block = find_free_block(part);
556 }
557
558 if (block == -1) {
559 rc = -ENOSPC;
560 goto err;
561 }
562 }
563
329ad399
AB
564 rc = mtd_read(part->mbd.mtd, part->blocks[block].offset,
565 part->header_size, &retlen,
566 (u_char *)part->header_cache);
e27a9960
SY
567
568 if (!rc && retlen != part->header_size)
569 rc = -EIO;
570
571 if (rc) {
683b30c8 572 printk(KERN_ERR PREFIX "'%s': unable to read header at "
97894cda 573 "0x%lx\n", part->mbd.mtd->name,
e27a9960
SY
574 part->blocks[block].offset);
575 goto err;
576 }
577
578 part->current_block = block;
579
580err:
581 return rc;
97894cda 582}
e27a9960
SY
583
584static int mark_sector_deleted(struct partition *part, u_long old_addr)
585{
586 int block, offset, rc;
587 u_long addr;
588 size_t retlen;
c80a7b26 589 u16 del = cpu_to_le16(SECTOR_DELETED);
e27a9960
SY
590
591 block = old_addr / part->block_size;
97894cda 592 offset = (old_addr % part->block_size) / SECTOR_SIZE -
e27a9960
SY
593 part->header_sectors_per_block;
594
595 addr = part->blocks[block].offset +
596 (HEADER_MAP_OFFSET + offset) * sizeof(u16);
eda95cbf
AB
597 rc = mtd_write(part->mbd.mtd, addr, sizeof(del), &retlen,
598 (u_char *)&del);
e27a9960
SY
599
600 if (!rc && retlen != sizeof(del))
601 rc = -EIO;
602
603 if (rc) {
683b30c8 604 printk(KERN_ERR PREFIX "error writing '%s' at "
e27a9960 605 "0x%lx\n", part->mbd.mtd->name, addr);
97894cda 606 if (rc)
e27a9960
SY
607 goto err;
608 }
609 if (block == part->current_block)
610 part->header_cache[offset + HEADER_MAP_OFFSET] = del;
611
612 part->blocks[block].used_sectors--;
613
614 if (!part->blocks[block].used_sectors &&
615 !part->blocks[block].free_sectors)
616 rc = erase_block(part, block);
617
618err:
619 return rc;
620}
621
622static int find_free_sector(const struct partition *part, const struct block *block)
623{
624 int i, stop;
625
626 i = stop = part->data_sectors_per_block - block->free_sectors;
627
628 do {
97894cda 629 if (le16_to_cpu(part->header_cache[HEADER_MAP_OFFSET + i])
e27a9960
SY
630 == SECTOR_FREE)
631 return i;
632
633 if (++i == part->data_sectors_per_block)
634 i = 0;
635 }
636 while(i != stop);
637
638 return -1;
639}
640
641static int do_writesect(struct mtd_blktrans_dev *dev, u_long sector, char *buf, ulong *old_addr)
642{
643 struct partition *part = (struct partition*)dev;
644 struct block *block;
645 u_long addr;
646 int i;
647 int rc;
648 size_t retlen;
649 u16 entry;
650
651 if (part->current_block == -1 ||
652 !part->blocks[part->current_block].free_sectors) {
653
683b30c8 654 rc = find_writable_block(part, old_addr);
97894cda 655 if (rc)
e27a9960
SY
656 goto err;
657 }
658
659 block = &part->blocks[part->current_block];
660
661 i = find_free_sector(part, block);
662
663 if (i < 0) {
664 rc = -ENOSPC;
665 goto err;
666 }
97894cda
TG
667
668 addr = (i + part->header_sectors_per_block) * SECTOR_SIZE +
e27a9960 669 block->offset;
eda95cbf
AB
670 rc = mtd_write(part->mbd.mtd, addr, SECTOR_SIZE, &retlen,
671 (u_char *)buf);
e27a9960
SY
672
673 if (!rc && retlen != SECTOR_SIZE)
674 rc = -EIO;
675
676 if (rc) {
683b30c8 677 printk(KERN_ERR PREFIX "error writing '%s' at 0x%lx\n",
e27a9960 678 part->mbd.mtd->name, addr);
97894cda 679 if (rc)
e27a9960
SY
680 goto err;
681 }
682
683 part->sector_map[sector] = addr;
684
685 entry = cpu_to_le16(sector == 0 ? SECTOR_ZERO : sector);
686
687 part->header_cache[i + HEADER_MAP_OFFSET] = entry;
688
689 addr = block->offset + (HEADER_MAP_OFFSET + i) * sizeof(u16);
eda95cbf
AB
690 rc = mtd_write(part->mbd.mtd, addr, sizeof(entry), &retlen,
691 (u_char *)&entry);
e27a9960
SY
692
693 if (!rc && retlen != sizeof(entry))
694 rc = -EIO;
695
696 if (rc) {
683b30c8 697 printk(KERN_ERR PREFIX "error writing '%s' at 0x%lx\n",
e27a9960 698 part->mbd.mtd->name, addr);
97894cda 699 if (rc)
e27a9960
SY
700 goto err;
701 }
702 block->used_sectors++;
703 block->free_sectors--;
704
705err:
706 return rc;
707}
708
709static int rfd_ftl_writesect(struct mtd_blktrans_dev *dev, u_long sector, char *buf)
710{
711 struct partition *part = (struct partition*)dev;
712 u_long old_addr;
713 int i;
714 int rc = 0;
715
716 pr_debug("rfd_ftl_writesect(sector=0x%lx)\n", sector);
717
718 if (part->reserved_block == -1) {
719 rc = -EACCES;
720 goto err;
721 }
722
723 if (sector >= part->sector_count) {
724 rc = -EIO;
725 goto err;
726 }
727
728 old_addr = part->sector_map[sector];
729
730 for (i=0; i<SECTOR_SIZE; i++) {
731 if (!buf[i])
732 continue;
733
734 rc = do_writesect(dev, sector, buf, &old_addr);
735 if (rc)
736 goto err;
737 break;
738 }
739
97894cda 740 if (i == SECTOR_SIZE)
e27a9960
SY
741 part->sector_map[sector] = -1;
742
743 if (old_addr != -1)
744 rc = mark_sector_deleted(part, old_addr);
745
746err:
747 return rc;
748}
749
750static int rfd_ftl_getgeo(struct mtd_blktrans_dev *dev, struct hd_geometry *geo)
751{
752 struct partition *part = (struct partition*)dev;
753
754 geo->heads = 1;
755 geo->sectors = SECTORS_PER_TRACK;
756 geo->cylinders = part->cylinders;
757
758 return 0;
759}
760
761static void rfd_ftl_add_mtd(struct mtd_blktrans_ops *tr, struct mtd_info *mtd)
762{
763 struct partition *part;
764
69423d99 765 if (mtd->type != MTD_NORFLASH || mtd->size > UINT_MAX)
e27a9960
SY
766 return;
767
cd861280 768 part = kzalloc(sizeof(struct partition), GFP_KERNEL);
e27a9960
SY
769 if (!part)
770 return;
771
772 part->mbd.mtd = mtd;
773
774 if (block_size)
775 part->block_size = block_size;
776 else {
777 if (!mtd->erasesize) {
683b30c8 778 printk(KERN_WARNING PREFIX "please provide block_size");
030f9e13
AM
779 goto out;
780 } else
e27a9960
SY
781 part->block_size = mtd->erasesize;
782 }
783
784 if (scan_header(part) == 0) {
785 part->mbd.size = part->sector_count;
e27a9960
SY
786 part->mbd.tr = tr;
787 part->mbd.devnum = -1;
788 if (!(mtd->flags & MTD_WRITEABLE))
789 part->mbd.readonly = 1;
790 else if (part->errors) {
683b30c8
SY
791 printk(KERN_WARNING PREFIX "'%s': errors found, "
792 "setting read-only\n", mtd->name);
e27a9960
SY
793 part->mbd.readonly = 1;
794 }
795
796 printk(KERN_INFO PREFIX "name: '%s' type: %d flags %x\n",
797 mtd->name, mtd->type, mtd->flags);
798
799 if (!add_mtd_blktrans_dev((void*)part))
800 return;
97894cda 801 }
030f9e13 802out:
e27a9960
SY
803 kfree(part);
804}
805
806static void rfd_ftl_remove_dev(struct mtd_blktrans_dev *dev)
807{
808 struct partition *part = (struct partition*)dev;
809 int i;
810
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);
814 }
815
816 del_mtd_blktrans_dev(dev);
817 vfree(part->sector_map);
818 kfree(part->header_cache);
819 kfree(part->blocks);
e27a9960
SY
820}
821
7fe9296c 822static struct mtd_blktrans_ops rfd_ftl_tr = {
e27a9960
SY
823 .name = "rfd",
824 .major = RFD_FTL_MAJOR,
825 .part_bits = PART_BITS,
19187672
RP
826 .blksize = SECTOR_SIZE,
827
e27a9960 828 .readsect = rfd_ftl_readsect,
97894cda 829 .writesect = rfd_ftl_writesect,
e27a9960
SY
830 .getgeo = rfd_ftl_getgeo,
831 .add_mtd = rfd_ftl_add_mtd,
832 .remove_dev = rfd_ftl_remove_dev,
833 .owner = THIS_MODULE,
834};
835
836static int __init init_rfd_ftl(void)
837{
838 return register_mtd_blktrans(&rfd_ftl_tr);
839}
840
841static void __exit cleanup_rfd_ftl(void)
842{
843 deregister_mtd_blktrans(&rfd_ftl_tr);
844}
845
846module_init(init_rfd_ftl);
847module_exit(cleanup_rfd_ftl);
848
849MODULE_LICENSE("GPL");
850MODULE_AUTHOR("Sean Young <sean@mess.org>");
851MODULE_DESCRIPTION("Support code for RFD Flash Translation Layer, "
852 "used by General Software's Embedded BIOS");
853