blob: 490b4742ce3a1948a8920302aa79c2f9284474a8 [file] [log] [blame]
Sean Younge27a9962005-06-16 09:49:33 +01001/*
2 * rfd_ftl.c -- resident flash disk (flash translation layer)
3 *
4 * Copyright (C) 2005 Sean Young <sean@mess.org>
5 *
Sean Younge27a9962005-06-16 09:49:33 +01006 * 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>
Tim Schmielaude259682006-01-08 01:02:05 -080019#include <linux/slab.h>
Andrew Mortonb80b5832005-11-08 21:34:27 -080020#include <linux/jiffies.h>
Sean Younge27a9962005-06-16 09:49:33 +010021
22#include <asm/types.h>
23
24#define const_cpu_to_le16 __constant_cpu_to_le16
25
26static int block_size = 0;
27module_param(block_size, int, 0);
28MODULE_PARM_DESC(block_size, "Block size to use by RFD, defaults to erase unit size");
29
30#define PREFIX "rfd_ftl: "
31
Sean Youngbc4117f2005-11-29 11:48:00 +000032/* This major has been assigned by device@lanana.org */
Sean Younge27a9962005-06-16 09:49:33 +010033#ifndef RFD_FTL_MAJOR
Sean Youngbc4117f2005-11-29 11:48:00 +000034#define RFD_FTL_MAJOR 256
Sean Younge27a9962005-06-16 09:49:33 +010035#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
57struct block {
58 enum {
59 BLOCK_OK,
60 BLOCK_ERASING,
61 BLOCK_ERASED,
Sean Young683b30c2006-05-17 12:45:34 +010062 BLOCK_UNUSED,
Sean Younge27a9962005-06-16 09:49:33 +010063 BLOCK_FAILED
64 } state;
65 int free_sectors;
66 int used_sectors;
67 int erases;
68 u_long offset;
69};
70
71struct 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
91static int rfd_ftl_writesect(struct mtd_blktrans_dev *dev, u_long sector, char *buf);
92
93static int build_block_map(struct partition *part, int block_no)
94{
95 struct block *block = &part->blocks[block_no];
96 int i;
Thomas Gleixner97894cd2005-11-07 11:15:26 +000097
Sean Younge27a9962005-06-16 09:49:33 +010098 block->offset = part->block_size * block_no;
99
100 if (le16_to_cpu(part->header_cache[0]) != RFD_MAGIC) {
Sean Young683b30c2006-05-17 12:45:34 +0100101 block->state = BLOCK_UNUSED;
102 return -ENOENT;
Sean Younge27a9962005-06-16 09:49:33 +0100103 }
104
105 block->state = BLOCK_OK;
106
107 for (i=0; i<part->data_sectors_per_block; i++) {
108 u16 entry;
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000109
Sean Younge27a9962005-06-16 09:49:33 +0100110 entry = le16_to_cpu(part->header_cache[HEADER_MAP_OFFSET + i]);
111
112 if (entry == SECTOR_DELETED)
113 continue;
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000114
Sean Younge27a9962005-06-16 09:49:33 +0100115 if (entry == SECTOR_FREE) {
116 block->free_sectors++;
117 continue;
118 }
119
120 if (entry == SECTOR_ZERO)
121 entry = 0;
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000122
Sean Younge27a9962005-06-16 09:49:33 +0100123 if (entry >= part->sector_count) {
Sean Young683b30c2006-05-17 12:45:34 +0100124 printk(KERN_WARNING PREFIX
Sean Younge27a9962005-06-16 09:49:33 +0100125 "'%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) {
Sean Young683b30c2006-05-17 12:45:34 +0100132 printk(KERN_WARNING PREFIX
Sean Younge27a9962005-06-16 09:49:33 +0100133 "'%s': more than one entry for sector %d\n",
134 part->mbd.mtd->name, entry);
135 part->errors = 1;
136 continue;
137 }
138
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000139 part->sector_map[entry] = block->offset +
Sean Younge27a9962005-06-16 09:49:33 +0100140 (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
151static 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;
Adrian Hunter69423d92008-12-10 13:37:21 +0000159 part->total_blocks = (u32)part->mbd.mtd->size / part->block_size;
Sean Younge27a9962005-06-16 09:49:33 +0100160
161 if (part->total_blocks < 2)
162 return -ENOENT;
163
164 /* each erase block has three bytes header, followed by the map */
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000165 part->header_sectors_per_block =
166 ((HEADER_MAP_OFFSET + sectors_per_block) *
Sean Young683b30c2006-05-17 12:45:34 +0100167 sizeof(u16) + SECTOR_SIZE - 1) / SECTOR_SIZE;
Sean Younge27a9962005-06-16 09:49:33 +0100168
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000169 part->data_sectors_per_block = sectors_per_block -
Sean Younge27a9962005-06-16 09:49:33 +0100170 part->header_sectors_per_block;
171
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000172 part->header_size = (HEADER_MAP_OFFSET +
Sean Younge27a9962005-06-16 09:49:33 +0100173 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
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000188 part->blocks = kcalloc(part->total_blocks, sizeof(struct block),
Sean Younge27a9962005-06-16 09:49:33 +0100189 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
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000200 for (i=0; i<part->sector_count; i++)
Sean Younge27a9962005-06-16 09:49:33 +0100201 part->sector_map[i] = -1;
202
203 for (i=0, blocks_found=0; i<part->total_blocks; i++) {
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000204 rc = part->mbd.mtd->read(part->mbd.mtd,
Sean Younge27a9962005-06-16 09:49:33 +0100205 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
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000211 if (rc)
Sean Younge27a9962005-06-16 09:49:33 +0100212 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) {
Sean Young683b30c2006-05-17 12:45:34 +0100226 printk(KERN_WARNING PREFIX "'%s': no empty erase unit found\n",
Sean Younge27a9962005-06-16 09:49:33 +0100227 part->mbd.mtd->name);
228
229 part->errors = 1;
230 }
231
232 return 0;
233
234err:
235 vfree(part->sector_map);
236 kfree(part->header_cache);
237 kfree(part->blocks);
238
239 return rc;
240}
241
242static 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;
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000248
Sean Younge27a9962005-06-16 09:49:33 +0100249 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);
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000266
Sean Younge27a9962005-06-16 09:49:33 +0100267 return 0;
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000268}
Sean Younge27a9962005-06-16 09:49:33 +0100269
270static 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
Adrian Hunter69423d92008-12-10 13:37:21 +0000279 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);
Sean Younge27a9962005-06-16 09:49:33 +0100284 return;
285 }
286
287 if (erase->state != MTD_ERASE_DONE) {
Adrian Hunter69423d92008-12-10 13:37:21 +0000288 printk(KERN_WARNING PREFIX "erase failed at 0x%llx on '%s', "
289 "state %d\n", (unsigned long long)erase->addr,
Sean Younge27a9962005-06-16 09:49:33 +0100290 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
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000308 rc = part->mbd.mtd->write(part->mbd.mtd,
309 part->blocks[i].offset, sizeof(magic), &retlen,
Sean Younge27a9962005-06-16 09:49:33 +0100310 (u_char*)&magic);
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000311
Sean Younge27a9962005-06-16 09:49:33 +0100312 if (!rc && retlen != sizeof(magic))
313 rc = -EIO;
314
315 if (rc) {
Sean Young683b30c2006-05-17 12:45:34 +0100316 printk(KERN_ERR PREFIX "'%s': unable to write RFD "
Sean Younge27a9962005-06-16 09:49:33 +0100317 "header at 0x%lx\n",
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000318 part->mbd.mtd->name,
Sean Younge27a9962005-06-16 09:49:33 +0100319 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
328static 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) {
Adrian Hunter69423d92008-12-10 13:37:21 +0000349 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);
Sean Younge27a9962005-06-16 09:49:33 +0100352 kfree(erase);
353 }
354
355err:
356 return rc;
357}
358
359static 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;
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000375
376 rc = part->mbd.mtd->read(part->mbd.mtd,
377 part->blocks[block_no].offset, part->header_size,
Sean Younge27a9962005-06-16 09:49:33 +0100378 &retlen, (u_char*)map);
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000379
Sean Younge27a9962005-06-16 09:49:33 +0100380 if (!rc && retlen != part->header_size)
381 rc = -EIO;
382
383 if (rc) {
Sean Young683b30c2006-05-17 12:45:34 +0100384 printk(KERN_ERR PREFIX "error reading '%s' at "
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000385 "0x%lx\n", part->mbd.mtd->name,
Sean Younge27a9962005-06-16 09:49:33 +0100386 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
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000399 if (entry == SECTOR_ZERO)
Sean Younge27a9962005-06-16 09:49:33 +0100400 entry = 0;
401
402 /* already warned about and ignored in build_block_map() */
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000403 if (entry >= part->sector_count)
Sean Younge27a9962005-06-16 09:49:33 +0100404 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);
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000419
Sean Younge27a9962005-06-16 09:49:33 +0100420 if (!rc && retlen != SECTOR_SIZE)
421 rc = -EIO;
422
423 if (rc) {
Sean Young683b30c2006-05-17 12:45:34 +0100424 printk(KERN_ERR PREFIX "'%s': Unable to "
Sean Younge27a9962005-06-16 09:49:33 +0100425 "read sector for relocation\n",
426 part->mbd.mtd->name);
427
428 goto err;
429 }
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000430
Sean Younge27a9962005-06-16 09:49:33 +0100431 rc = rfd_ftl_writesect((struct mtd_blktrans_dev*)part,
432 entry, sector_data);
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000433
434 if (rc)
Sean Younge27a9962005-06-16 09:49:33 +0100435 goto err;
436 }
437
438err:
439 kfree(map);
440err2:
441 kfree(sector_data);
442err3:
443 part->is_reclaiming = 0;
444
445 return rc;
446}
447
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000448static int reclaim_block(struct partition *part, u_long *old_sector)
Sean Younge27a9962005-06-16 09:49:33 +0100449{
450 int block, best_block, score, old_sector_block;
451 int rc;
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000452
Sean Younge27a9962005-06-16 09:49:33 +0100453 /* 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 */
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000475 if (part->blocks[block].free_sectors)
Sean Younge27a9962005-06-16 09:49:33 +0100476 return 0;
477
478 this_score = part->blocks[block].used_sectors;
479
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000480 if (block == old_sector_block)
Sean Younge27a9962005-06-16 09:49:33 +0100481 this_score--;
482 else {
483 /* no point in moving a full block */
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000484 if (part->blocks[block].used_sectors ==
Sean Younge27a9962005-06-16 09:49:33 +0100485 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 */
Sean Young683b30c2006-05-17 12:45:34 +0100521static int find_free_block(struct partition *part)
Sean Younge27a9962005-06-16 09:49:33 +0100522{
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 {
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000530 if (part->blocks[block].free_sectors &&
Sean Younge27a9962005-06-16 09:49:33 +0100531 block != part->reserved_block)
532 return block;
533
Sean Young683b30c2006-05-17 12:45:34 +0100534 if (part->blocks[block].state == BLOCK_UNUSED)
535 erase_block(part, block);
536
Sean Younge27a9962005-06-16 09:49:33 +0100537 if (++block >= part->total_blocks)
538 block = 0;
539
540 } while (block != stop);
541
542 return -1;
543}
544
Sean Young683b30c2006-05-17 12:45:34 +0100545static int find_writable_block(struct partition *part, u_long *old_sector)
Sean Younge27a9962005-06-16 09:49:33 +0100546{
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
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000567 rc = part->mbd.mtd->read(part->mbd.mtd, part->blocks[block].offset,
Sean Younge27a9962005-06-16 09:49:33 +0100568 part->header_size, &retlen, (u_char*)part->header_cache);
569
570 if (!rc && retlen != part->header_size)
571 rc = -EIO;
572
573 if (rc) {
Sean Young683b30c2006-05-17 12:45:34 +0100574 printk(KERN_ERR PREFIX "'%s': unable to read header at "
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000575 "0x%lx\n", part->mbd.mtd->name,
Sean Younge27a9962005-06-16 09:49:33 +0100576 part->blocks[block].offset);
577 goto err;
578 }
579
580 part->current_block = block;
581
582err:
583 return rc;
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000584}
Sean Younge27a9962005-06-16 09:49:33 +0100585
586static 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;
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000594 offset = (old_addr % part->block_size) / SECTOR_SIZE -
Sean Younge27a9962005-06-16 09:49:33 +0100595 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) {
Sean Young683b30c2006-05-17 12:45:34 +0100606 printk(KERN_ERR PREFIX "error writing '%s' at "
Sean Younge27a9962005-06-16 09:49:33 +0100607 "0x%lx\n", part->mbd.mtd->name, addr);
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000608 if (rc)
Sean Younge27a9962005-06-16 09:49:33 +0100609 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
620err:
621 return rc;
622}
623
624static 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 {
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000631 if (le16_to_cpu(part->header_cache[HEADER_MAP_OFFSET + i])
Sean Younge27a9962005-06-16 09:49:33 +0100632 == 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
643static 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
Sean Young683b30c2006-05-17 12:45:34 +0100656 rc = find_writable_block(part, old_addr);
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000657 if (rc)
Sean Younge27a9962005-06-16 09:49:33 +0100658 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 }
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000669
670 addr = (i + part->header_sectors_per_block) * SECTOR_SIZE +
Sean Younge27a9962005-06-16 09:49:33 +0100671 block->offset;
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000672 rc = part->mbd.mtd->write(part->mbd.mtd,
Sean Younge27a9962005-06-16 09:49:33 +0100673 addr, SECTOR_SIZE, &retlen, (u_char*)buf);
674
675 if (!rc && retlen != SECTOR_SIZE)
676 rc = -EIO;
677
678 if (rc) {
Sean Young683b30c2006-05-17 12:45:34 +0100679 printk(KERN_ERR PREFIX "error writing '%s' at 0x%lx\n",
Sean Younge27a9962005-06-16 09:49:33 +0100680 part->mbd.mtd->name, addr);
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000681 if (rc)
Sean Younge27a9962005-06-16 09:49:33 +0100682 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) {
Sean Young683b30c2006-05-17 12:45:34 +0100699 printk(KERN_ERR PREFIX "error writing '%s' at 0x%lx\n",
Sean Younge27a9962005-06-16 09:49:33 +0100700 part->mbd.mtd->name, addr);
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000701 if (rc)
Sean Younge27a9962005-06-16 09:49:33 +0100702 goto err;
703 }
704 block->used_sectors++;
705 block->free_sectors--;
706
707err:
708 return rc;
709}
710
711static 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
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000742 if (i == SECTOR_SIZE)
Sean Younge27a9962005-06-16 09:49:33 +0100743 part->sector_map[sector] = -1;
744
745 if (old_addr != -1)
746 rc = mark_sector_deleted(part, old_addr);
747
748err:
749 return rc;
750}
751
752static 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
763static void rfd_ftl_add_mtd(struct mtd_blktrans_ops *tr, struct mtd_info *mtd)
764{
765 struct partition *part;
766
Adrian Hunter69423d92008-12-10 13:37:21 +0000767 if (mtd->type != MTD_NORFLASH || mtd->size > UINT_MAX)
Sean Younge27a9962005-06-16 09:49:33 +0100768 return;
769
Robert P. J. Daycd861282006-12-13 00:34:52 -0800770 part = kzalloc(sizeof(struct partition), GFP_KERNEL);
Sean Younge27a9962005-06-16 09:49:33 +0100771 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) {
Sean Young683b30c2006-05-17 12:45:34 +0100780 printk(KERN_WARNING PREFIX "please provide block_size");
akpm@linux-foundation.org030f9e12007-07-20 11:56:19 -0700781 goto out;
782 } else
Sean Younge27a9962005-06-16 09:49:33 +0100783 part->block_size = mtd->erasesize;
784 }
785
786 if (scan_header(part) == 0) {
787 part->mbd.size = part->sector_count;
Sean Younge27a9962005-06-16 09:49:33 +0100788 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) {
Sean Young683b30c2006-05-17 12:45:34 +0100793 printk(KERN_WARNING PREFIX "'%s': errors found, "
794 "setting read-only\n", mtd->name);
Sean Younge27a9962005-06-16 09:49:33 +0100795 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;
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000803 }
akpm@linux-foundation.org030f9e12007-07-20 11:56:19 -0700804out:
Sean Younge27a9962005-06-16 09:49:33 +0100805 kfree(part);
806}
807
808static 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
Adrian Bunk7fe92962008-04-14 17:20:40 +0300825static struct mtd_blktrans_ops rfd_ftl_tr = {
Sean Younge27a9962005-06-16 09:49:33 +0100826 .name = "rfd",
827 .major = RFD_FTL_MAJOR,
828 .part_bits = PART_BITS,
Richard Purdie19187672006-10-27 09:09:33 +0100829 .blksize = SECTOR_SIZE,
830
Sean Younge27a9962005-06-16 09:49:33 +0100831 .readsect = rfd_ftl_readsect,
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000832 .writesect = rfd_ftl_writesect,
Sean Younge27a9962005-06-16 09:49:33 +0100833 .getgeo = rfd_ftl_getgeo,
834 .add_mtd = rfd_ftl_add_mtd,
835 .remove_dev = rfd_ftl_remove_dev,
836 .owner = THIS_MODULE,
837};
838
839static int __init init_rfd_ftl(void)
840{
841 return register_mtd_blktrans(&rfd_ftl_tr);
842}
843
844static void __exit cleanup_rfd_ftl(void)
845{
846 deregister_mtd_blktrans(&rfd_ftl_tr);
847}
848
849module_init(init_rfd_ftl);
850module_exit(cleanup_rfd_ftl);
851
852MODULE_LICENSE("GPL");
853MODULE_AUTHOR("Sean Young <sean@mess.org>");
854MODULE_DESCRIPTION("Support code for RFD Flash Translation Layer, "
855 "used by General Software's Embedded BIOS");
856