summaryrefslogtreecommitdiffstats
path: root/linux-2.4.x/drivers/mtd/ftl.c
diff options
context:
space:
mode:
Diffstat (limited to 'linux-2.4.x/drivers/mtd/ftl.c')
-rw-r--r--linux-2.4.x/drivers/mtd/ftl.c636
1 files changed, 141 insertions, 495 deletions
diff --git a/linux-2.4.x/drivers/mtd/ftl.c b/linux-2.4.x/drivers/mtd/ftl.c
index de429d8..8a878b3 100644
--- a/linux-2.4.x/drivers/mtd/ftl.c
+++ b/linux-2.4.x/drivers/mtd/ftl.c
@@ -1,5 +1,5 @@
/* This version ported to the Linux-MTD system by dwmw2@infradead.org
- * $Id: ftl.c,v 1.43 2002/02/13 15:31:37 dwmw2 Exp $
+ * $Id: ftl.c,v 1.59 2005/11/29 14:48:31 gleixner Exp $
*
* Fixes: Arnaldo Carvalho de Melo <acme@conectiva.com.br>
* - fixes some leaks on failure in build_maps and ftl_notify_add, cleanups
@@ -53,10 +53,10 @@
Use of the FTL format for non-PCMCIA applications may be an
infringement of these patents. For additional information,
contact M-Systems (http://www.m-sys.com) directly.
-
+
======================================================================*/
+#include <linux/mtd/blktrans.h>
#include <linux/module.h>
-#include <linux/mtd/compatmac.h>
#include <linux/mtd/mtd.h>
/*#define PSYCHO_DEBUG */
@@ -68,50 +68,19 @@
#include <linux/timer.h>
#include <linux/major.h>
#include <linux/fs.h>
-#include <linux/ioctl.h>
+#include <linux/init.h>
#include <linux/hdreg.h>
-#include <stdarg.h>
-
-#if (LINUX_VERSION_CODE >= 0x20100)
#include <linux/vmalloc.h>
-#endif
-#if (LINUX_VERSION_CODE >= 0x20303)
#include <linux/blkpg.h>
-#endif
+#include <asm/uaccess.h>
#include <linux/mtd/ftl.h>
-/*====================================================================*/
-/* Stuff which really ought to be in compatmac.h */
-
-#if (LINUX_VERSION_CODE < 0x20328)
-#define register_disk(dev, drive, minors, ops, size) \
- do { (dev)->part[(drive)*(minors)].nr_sects = size; \
- if (size == 0) (dev)->part[(drive)*(minors)].start_sect = -1; \
- resetup_one_dev(dev, drive); } while (0)
-#endif
-
-#if (LINUX_VERSION_CODE < 0x20320)
-#define BLK_DEFAULT_QUEUE(n) blk_dev[n].request_fn
-#define blk_init_queue(q, req) q = (req)
-#define blk_cleanup_queue(q) q = NULL
-#define request_arg_t void
-#else
-#define request_arg_t request_queue_t *q
-#endif
-
-#if LINUX_VERSION_CODE < KERNEL_VERSION(2,4,14)
-#define BLK_INC_USE_COUNT MOD_INC_USE_COUNT
-#define BLK_DEC_USE_COUNT MOD_DEC_USE_COUNT
-#else
-#define BLK_INC_USE_COUNT do {} while(0)
-#define BLK_DEC_USE_COUNT do {} while(0)
-#endif
/*====================================================================*/
/* Parameters that can be set with 'insmod' */
static int shuffle_freq = 50;
-MODULE_PARM(shuffle_freq, "i");
+module_param(shuffle_freq, int, 0);
/*====================================================================*/
@@ -120,19 +89,6 @@ MODULE_PARM(shuffle_freq, "i");
#define FTL_MAJOR 44
#endif
-/* Funky stuff for setting up a block device */
-#define MAJOR_NR FTL_MAJOR
-#define DEVICE_NAME "ftl"
-#define DEVICE_REQUEST do_ftl_request
-#define DEVICE_ON(device)
-#define DEVICE_OFF(device)
-
-#define DEVICE_NR(minor) ((minor)>>5)
-#define REGION_NR(minor) (((minor)>>3)&3)
-#define PART_NR(minor) ((minor)&7)
-#define MINOR_NR(dev,reg,part) (((dev)<<5)+((reg)<<3)+(part))
-
-#include <linux/blk.h>
/*====================================================================*/
@@ -143,8 +99,7 @@ MODULE_PARM(shuffle_freq, "i");
#define MAX_REGION 4
/* Maximum number of partitions in an FTL region */
-#define PART_BITS 3
-#define MAX_PART 8
+#define PART_BITS 4
/* Maximum number of outstanding erase requests per socket */
#define MAX_ERASE 8
@@ -155,7 +110,7 @@ MODULE_PARM(shuffle_freq, "i");
/* Each memory region corresponds to a minor device */
typedef struct partition_t {
- struct mtd_info *mtd;
+ struct mtd_blktrans_dev mbd;
u_int32_t state;
u_int32_t *VirtualBlockMap;
u_int32_t *VirtualPageMap;
@@ -180,21 +135,10 @@ typedef struct partition_t {
region_info_t region;
memory_handle_t handle;
#endif
- atomic_t open;
} partition_t;
-partition_t *myparts[MAX_MTD_DEVICES];
-
-static void ftl_notify_add(struct mtd_info *mtd);
-static void ftl_notify_remove(struct mtd_info *mtd);
-
void ftl_freepart(partition_t *part);
-static struct mtd_notifier ftl_notifier = {
- add: ftl_notify_add,
- remove: ftl_notify_remove,
-};
-
/* Partition state flags */
#define FTL_FORMATTED 0x01
@@ -205,77 +149,38 @@ static struct mtd_notifier ftl_notifier = {
#define XFER_PREPARED 0x03
#define XFER_FAILED 0x04
-static struct hd_struct ftl_hd[MINOR_NR(MAX_DEV, 0, 0)];
-static int ftl_sizes[MINOR_NR(MAX_DEV, 0, 0)];
-static int ftl_blocksizes[MINOR_NR(MAX_DEV, 0, 0)];
-
-static struct gendisk ftl_gendisk = {
- major: FTL_MAJOR,
- major_name: "ftl",
- minor_shift: PART_BITS,
- max_p: MAX_PART,
-#if (LINUX_VERSION_CODE < 0x20328)
- max_nr: MAX_DEV*MAX_PART,
-#endif
- part: ftl_hd,
- sizes: ftl_sizes,
-};
-
/*====================================================================*/
-static int ftl_ioctl(struct inode *inode, struct file *file,
- u_int cmd, u_long arg);
-static int ftl_open(struct inode *inode, struct file *file);
-static release_t ftl_close(struct inode *inode, struct file *file);
-static int ftl_reread_partitions(int minor);
static void ftl_erase_callback(struct erase_info *done);
-#if LINUX_VERSION_CODE < 0x20326
-static struct file_operations ftl_blk_fops = {
- open: ftl_open,
- release: ftl_close,
- ioctl: ftl_ioctl,
- read: block_read,
- write: block_write,
- fsync: block_fsync
-};
-#else
-static struct block_device_operations ftl_blk_fops = {
-#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,14)
- owner: THIS_MODULE,
-#endif
- open: ftl_open,
- release: ftl_close,
- ioctl: ftl_ioctl,
-};
-#endif
/*======================================================================
Scan_header() checks to see if a memory region contains an FTL
partition. build_maps() reads all the erase unit headers, builds
the erase unit map, and then builds the virtual page map.
-
+
======================================================================*/
static int scan_header(partition_t *part)
{
erase_unit_header_t header;
loff_t offset, max_offset;
- int ret;
+ size_t ret;
+ int err;
part->header.FormattedSize = 0;
- max_offset = (0x100000<part->mtd->size)?0x100000:part->mtd->size;
+ max_offset = (0x100000<part->mbd.mtd->size)?0x100000:part->mbd.mtd->size;
/* Search first megabyte for a valid FTL header */
for (offset = 0;
(offset + sizeof(header)) < max_offset;
- offset += part->mtd->erasesize ? : 0x2000) {
+ offset += part->mbd.mtd->erasesize ? : 0x2000) {
- ret = part->mtd->read(part->mtd, offset, sizeof(header), &ret,
+ err = part->mbd.mtd->read(part->mbd.mtd, offset, sizeof(header), &ret,
(unsigned char *)&header);
-
- if (ret)
- return ret;
+
+ if (err)
+ return err;
if (strcmp(header.DataOrgTuple+3, "FTL100") == 0) break;
}
@@ -284,15 +189,15 @@ static int scan_header(partition_t *part)
printk(KERN_NOTICE "ftl_cs: FTL header not found.\n");
return -ENOENT;
}
- if ((le16_to_cpu(header.NumEraseUnits) > 65536) || header.BlockSize != 9 ||
+ if (header.BlockSize != 9 ||
(header.EraseUnitSize < 10) || (header.EraseUnitSize > 31) ||
(header.NumTransferUnits >= le16_to_cpu(header.NumEraseUnits))) {
printk(KERN_NOTICE "ftl_cs: FTL header corrupt!\n");
return -1;
}
- if ((1 << header.EraseUnitSize) != part->mtd->erasesize) {
+ if ((1 << header.EraseUnitSize) != part->mbd.mtd->erasesize) {
printk(KERN_NOTICE "ftl: FTL EraseUnitSize %x != MTD erasesize %x\n",
- 1 << header.EraseUnitSize,part->mtd->erasesize);
+ 1 << header.EraseUnitSize,part->mbd.mtd->erasesize);
return -1;
}
part->header = header;
@@ -327,10 +232,10 @@ static int build_maps(partition_t *part)
for (i = 0; i < le16_to_cpu(part->header.NumEraseUnits); i++) {
offset = ((i + le16_to_cpu(part->header.FirstPhysicalEUN))
<< part->header.EraseUnitSize);
- ret = part->mtd->read(part->mtd, offset, sizeof(header), &retval,
+ ret = part->mbd.mtd->read(part->mbd.mtd, offset, sizeof(header), &retval,
(unsigned char *)&header);
-
- if (ret)
+
+ if (ret)
goto out_XferInfo;
ret = -1;
@@ -369,7 +274,7 @@ static int build_maps(partition_t *part)
"don't add up!\n");
goto out_XferInfo;
}
-
+
/* Set up virtual page map */
blocks = le32_to_cpu(header.FormattedSize) >> header.BlockSize;
part->VirtualBlockMap = vmalloc(blocks * sizeof(u_int32_t));
@@ -391,12 +296,12 @@ static int build_maps(partition_t *part)
part->EUNInfo[i].Free = 0;
part->EUNInfo[i].Deleted = 0;
offset = part->EUNInfo[i].Offset + le32_to_cpu(header.BAMOffset);
-
- ret = part->mtd->read(part->mtd, offset,
- part->BlocksPerUnit * sizeof(u_int32_t), &retval,
+
+ ret = part->mbd.mtd->read(part->mbd.mtd, offset,
+ part->BlocksPerUnit * sizeof(u_int32_t), &retval,
(unsigned char *)part->bam_cache);
-
- if (ret)
+
+ if (ret)
goto out_bam_cache;
for (j = 0; j < part->BlocksPerUnit; j++) {
@@ -411,7 +316,7 @@ static int build_maps(partition_t *part)
part->EUNInfo[i].Deleted++;
}
}
-
+
ret = 0;
goto out;
@@ -431,7 +336,7 @@ out:
Erase_xfer() schedules an asynchronous erase operation for a
transfer unit.
-
+
======================================================================*/
static int erase_xfer(partition_t *part,
@@ -446,18 +351,19 @@ static int erase_xfer(partition_t *part,
xfer->state = XFER_ERASING;
/* Is there a free erase slot? Always in MTD. */
-
-
+
+
erase=kmalloc(sizeof(struct erase_info), GFP_KERNEL);
- if (!erase)
+ if (!erase)
return -ENOMEM;
+ erase->mtd = part->mbd.mtd;
erase->callback = ftl_erase_callback;
erase->addr = xfer->Offset;
erase->len = 1 << part->header.EraseUnitSize;
erase->priv = (u_long)part;
-
- ret = part->mtd->erase(part->mtd, erase);
+
+ ret = part->mbd.mtd->erase(part->mbd.mtd, erase);
if (!ret)
xfer->EraseCount++;
@@ -471,7 +377,7 @@ static int erase_xfer(partition_t *part,
Prepare_xfer() takes a freshly erased transfer unit and gives
it an appropriate header.
-
+
======================================================================*/
static void ftl_erase_callback(struct erase_info *erase)
@@ -479,7 +385,7 @@ static void ftl_erase_callback(struct erase_info *erase)
partition_t *part;
struct xfer_info_t *xfer;
int i;
-
+
/* Look up the transfer unit */
part = (partition_t *)(erase->priv);
@@ -516,7 +422,7 @@ static int prepare_xfer(partition_t *part, int i)
xfer = &part->XferInfo[i];
xfer->state = XFER_FAILED;
-
+
DEBUG(1, "ftl_cs: preparing xfer unit at 0x%x\n", xfer->Offset);
/* Write the transfer unit header */
@@ -524,7 +430,7 @@ static int prepare_xfer(partition_t *part, int i)
header.LogicalEUN = cpu_to_le16(0xffff);
header.EraseCount = cpu_to_le32(xfer->EraseCount);
- ret = part->mtd->write(part->mtd, xfer->Offset, sizeof(header),
+ ret = part->mbd.mtd->write(part->mbd.mtd, xfer->Offset, sizeof(header),
&retlen, (u_char *)&header);
if (ret) {
@@ -540,7 +446,7 @@ static int prepare_xfer(partition_t *part, int i)
for (i = 0; i < nbam; i++, offset += sizeof(u_int32_t)) {
- ret = part->mtd->write(part->mtd, offset, sizeof(u_int32_t),
+ ret = part->mbd.mtd->write(part->mbd.mtd, offset, sizeof(u_int32_t),
&retlen, (u_char *)&ctl);
if (ret)
@@ -548,7 +454,7 @@ static int prepare_xfer(partition_t *part, int i)
}
xfer->state = XFER_PREPARED;
return 0;
-
+
} /* prepare_xfer */
/*======================================================================
@@ -560,7 +466,7 @@ static int prepare_xfer(partition_t *part, int i)
All data blocks are copied to the corresponding blocks in the
target unit, so the virtual block map does not need to be
updated.
-
+
======================================================================*/
static int copy_erase_unit(partition_t *part, u_int16_t srcunit,
@@ -580,14 +486,14 @@ static int copy_erase_unit(partition_t *part, u_int16_t srcunit,
xfer = &part->XferInfo[xferunit];
DEBUG(2, "ftl_cs: copying block 0x%x to 0x%x\n",
eun->Offset, xfer->Offset);
-
-
+
+
/* Read current BAM */
if (part->bam_index != srcunit) {
offset = eun->Offset + le32_to_cpu(part->header.BAMOffset);
- ret = part->mtd->read(part->mtd, offset,
+ ret = part->mbd.mtd->read(part->mbd.mtd, offset,
part->BlocksPerUnit * sizeof(u_int32_t),
&retlen, (u_char *) (part->bam_cache));
@@ -595,24 +501,24 @@ static int copy_erase_unit(partition_t *part, u_int16_t srcunit,
part->bam_index = 0xffff;
if (ret) {
- printk( KERN_WARNING "ftl: Failed to read BAM cache in copy_erase_unit()!\n");
+ printk( KERN_WARNING "ftl: Failed to read BAM cache in copy_erase_unit()!\n");
return ret;
}
}
-
+
/* Write the LogicalEUN for the transfer unit */
xfer->state = XFER_UNKNOWN;
offset = xfer->Offset + 20; /* Bad! */
unit = cpu_to_le16(0x7fff);
- ret = part->mtd->write(part->mtd, offset, sizeof(u_int16_t),
+ ret = part->mbd.mtd->write(part->mbd.mtd, offset, sizeof(u_int16_t),
&retlen, (u_char *) &unit);
-
+
if (ret) {
printk( KERN_WARNING "ftl: Failed to write back to BAM cache in copy_erase_unit()!\n");
return ret;
}
-
+
/* Copy all data blocks from source unit to transfer unit */
src = eun->Offset; dest = xfer->Offset;
@@ -625,7 +531,7 @@ static int copy_erase_unit(partition_t *part, u_int16_t srcunit,
break;
case BLOCK_DATA:
case BLOCK_REPLACEMENT:
- ret = part->mtd->read(part->mtd, src, SECTOR_SIZE,
+ ret = part->mbd.mtd->read(part->mbd.mtd, src, SECTOR_SIZE,
&retlen, (u_char *) buf);
if (ret) {
printk(KERN_WARNING "ftl: Error reading old xfer unit in copy_erase_unit\n");
@@ -633,7 +539,7 @@ static int copy_erase_unit(partition_t *part, u_int16_t srcunit,
}
- ret = part->mtd->write(part->mtd, dest, SECTOR_SIZE,
+ ret = part->mbd.mtd->write(part->mbd.mtd, dest, SECTOR_SIZE,
&retlen, (u_char *) buf);
if (ret) {
printk(KERN_WARNING "ftl: Error writing new xfer unit in copy_erase_unit\n");
@@ -652,25 +558,25 @@ static int copy_erase_unit(partition_t *part, u_int16_t srcunit,
}
/* Write the BAM to the transfer unit */
- ret = part->mtd->write(part->mtd, xfer->Offset + le32_to_cpu(part->header.BAMOffset),
- part->BlocksPerUnit * sizeof(int32_t), &retlen,
+ ret = part->mbd.mtd->write(part->mbd.mtd, xfer->Offset + le32_to_cpu(part->header.BAMOffset),
+ part->BlocksPerUnit * sizeof(int32_t), &retlen,
(u_char *)part->bam_cache);
if (ret) {
printk( KERN_WARNING "ftl: Error writing BAM in copy_erase_unit\n");
return ret;
}
-
+
/* All clear? Then update the LogicalEUN again */
- ret = part->mtd->write(part->mtd, xfer->Offset + 20, sizeof(u_int16_t),
+ ret = part->mbd.mtd->write(part->mbd.mtd, xfer->Offset + 20, sizeof(u_int16_t),
&retlen, (u_char *)&srcunitswap);
if (ret) {
printk(KERN_WARNING "ftl: Error writing new LogicalEUN in copy_erase_unit\n");
return ret;
- }
-
-
+ }
+
+
/* Update the maps and usage stats*/
i = xfer->EraseCount;
xfer->EraseCount = eun->EraseCount;
@@ -682,10 +588,10 @@ static int copy_erase_unit(partition_t *part, u_int16_t srcunit,
part->FreeTotal += free;
eun->Free = free;
eun->Deleted = 0;
-
+
/* Now, the cache should be valid for the new block */
part->bam_index = srcunit;
-
+
return 0;
} /* copy_erase_unit */
@@ -702,7 +608,7 @@ static int copy_erase_unit(partition_t *part, u_int16_t srcunit,
oldest data unit instead. This means that we generally postpone
the next reclaimation as long as possible, but shuffle static
stuff around a bit for wear leveling.
-
+
======================================================================*/
static int reclaim_block(partition_t *part)
@@ -750,8 +656,8 @@ static int reclaim_block(partition_t *part)
if (queued) {
DEBUG(1, "ftl_cs: waiting for transfer "
"unit to be prepared...\n");
- if (part->mtd->sync)
- part->mtd->sync(part->mtd);
+ if (part->mbd.mtd->sync)
+ part->mbd.mtd->sync(part->mbd.mtd);
} else {
static int ne = 0;
if (++ne < 5)
@@ -760,7 +666,7 @@ static int reclaim_block(partition_t *part)
else
DEBUG(1, "ftl_cs: reclaim failed: no "
"suitable transfer units!\n");
-
+
return -EIO;
}
}
@@ -809,7 +715,7 @@ static int reclaim_block(partition_t *part)
returns the block index -- the erase unit is just the currently
cached unit. If there are no free blocks, it returns 0 -- this
is never a valid data block because it contains the header.
-
+
======================================================================*/
#ifdef PSYCHO_DEBUG
@@ -831,7 +737,7 @@ static u_int32_t find_free(partition_t *part)
u_int32_t blk;
size_t retlen;
int ret;
-
+
/* Find an erase unit with some free space */
stop = (part->bam_index == 0xffff) ? 0 : part->bam_index;
eun = stop;
@@ -843,17 +749,17 @@ static u_int32_t find_free(partition_t *part)
if (part->EUNInfo[eun].Free == 0)
return 0;
-
+
/* Is this unit's BAM cached? */
if (eun != part->bam_index) {
/* Invalidate cache */
part->bam_index = 0xffff;
- ret = part->mtd->read(part->mtd,
+ ret = part->mbd.mtd->read(part->mbd.mtd,
part->EUNInfo[eun].Offset + le32_to_cpu(part->header.BAMOffset),
part->BlocksPerUnit * sizeof(u_int32_t),
&retlen, (u_char *) (part->bam_cache));
-
+
if (ret) {
printk(KERN_WARNING"ftl: Error reading BAM in find_free\n");
return 0;
@@ -875,86 +781,14 @@ static u_int32_t find_free(partition_t *part)
}
DEBUG(2, "ftl_cs: found free block at %d in %d\n", blk, eun);
return blk;
-
-} /* find_free */
-
-/*======================================================================
-
- This gets a memory handle for the region corresponding to the
- minor device number.
-
-======================================================================*/
-
-static int ftl_open(struct inode *inode, struct file *file)
-{
- int minor = MINOR(inode->i_rdev);
- partition_t *partition;
-
- if (minor>>4 >= MAX_MTD_DEVICES)
- return -ENODEV;
-
- partition = myparts[minor>>4];
-
- if (!partition)
- return -ENODEV;
-
- if (partition->state != FTL_FORMATTED)
- return -ENXIO;
-
- if (ftl_gendisk.part[minor].nr_sects == 0)
- return -ENXIO;
-
- BLK_INC_USE_COUNT;
-
- if (!get_mtd_device(partition->mtd, -1)) {
- BLK_DEC_USE_COUNT;
- return -ENXIO;
- }
-
- if ((file->f_mode & 2) && !(partition->mtd->flags & MTD_CLEAR_BITS) ) {
- put_mtd_device(partition->mtd);
- BLK_DEC_USE_COUNT;
- return -EROFS;
- }
-
- DEBUG(0, "ftl_cs: ftl_open(%d)\n", minor);
-
- atomic_inc(&partition->open);
-
- return 0;
-}
-
-/*====================================================================*/
-
-static release_t ftl_close(struct inode *inode, struct file *file)
-{
- int minor = MINOR(inode->i_rdev);
- partition_t *part = myparts[minor >> 4];
- int i;
-
- DEBUG(0, "ftl_cs: ftl_close(%d)\n", minor);
-
- /* Wait for any pending erase operations to complete */
- if (part->mtd->sync)
- part->mtd->sync(part->mtd);
-
- for (i = 0; i < part->header.NumTransferUnits; i++) {
- if (part->XferInfo[i].state == XFER_ERASED)
- prepare_xfer(part, i);
- }
-
- atomic_dec(&part->open);
- put_mtd_device(part->mtd);
- BLK_DEC_USE_COUNT;
- release_return(0);
-} /* ftl_close */
+} /* find_free */
/*======================================================================
Read a series of sectors from an FTL partition.
-
+
======================================================================*/
static int ftl_read(partition_t *part, caddr_t buffer,
@@ -964,7 +798,7 @@ static int ftl_read(partition_t *part, caddr_t buffer,
u_long i;
int ret;
size_t offset, retlen;
-
+
DEBUG(2, "ftl_cs: ftl_read(0x%p, 0x%lx, %ld)\n",
part, sector, nblocks);
if (!(part->state & FTL_FORMATTED)) {
@@ -984,7 +818,7 @@ static int ftl_read(partition_t *part, caddr_t buffer,
else {
offset = (part->EUNInfo[log_addr / bsize].Offset
+ (log_addr % bsize));
- ret = part->mtd->read(part->mtd, offset, SECTOR_SIZE,
+ ret = part->mbd.mtd->read(part->mbd.mtd, offset, SECTOR_SIZE,
&retlen, (u_char *) buffer);
if (ret) {
@@ -1000,7 +834,7 @@ static int ftl_read(partition_t *part, caddr_t buffer,
/*======================================================================
Write a series of sectors to an FTL partition
-
+
======================================================================*/
static int set_bam_entry(partition_t *part, u_int32_t log_addr,
@@ -1021,9 +855,9 @@ static int set_bam_entry(partition_t *part, u_int32_t log_addr,
blk = (log_addr % bsize) / SECTOR_SIZE;
offset = (part->EUNInfo[eun].Offset + blk * sizeof(u_int32_t) +
le32_to_cpu(part->header.BAMOffset));
-
+
#ifdef PSYCHO_DEBUG
- ret = part->mtd->read(part->mtd, offset, sizeof(u_int32_t),
+ ret = part->mbd.mtd->read(part->mbd.mtd, offset, sizeof(u_int32_t),
&retlen, (u_char *)&old_addr);
if (ret) {
printk(KERN_WARNING"ftl: Error reading old_addr in set_bam_entry: %d\n",ret);
@@ -1060,7 +894,7 @@ static int set_bam_entry(partition_t *part, u_int32_t log_addr,
#endif
part->bam_cache[blk] = le_virt_addr;
}
- ret = part->mtd->write(part->mtd, offset, sizeof(u_int32_t),
+ ret = part->mbd.mtd->write(part->mbd.mtd, offset, sizeof(u_int32_t),
&retlen, (u_char *)&le_virt_addr);
if (ret) {
@@ -1091,7 +925,7 @@ static int ftl_write(partition_t *part, caddr_t buffer,
if (ret)
return ret;
}
-
+
bsize = 1 << part->header.EraseUnitSize;
virt_addr = sector * SECTOR_SIZE | BLOCK_DATA;
@@ -1115,22 +949,22 @@ static int ftl_write(partition_t *part, caddr_t buffer,
log_addr = part->bam_index * bsize + blk * SECTOR_SIZE;
part->EUNInfo[part->bam_index].Free--;
part->FreeTotal--;
- if (set_bam_entry(part, log_addr, 0xfffffffe))
+ if (set_bam_entry(part, log_addr, 0xfffffffe))
return -EIO;
part->EUNInfo[part->bam_index].Deleted++;
offset = (part->EUNInfo[part->bam_index].Offset +
blk * SECTOR_SIZE);
- ret = part->mtd->write(part->mtd, offset, SECTOR_SIZE, &retlen,
+ ret = part->mbd.mtd->write(part->mbd.mtd, offset, SECTOR_SIZE, &retlen,
buffer);
if (ret) {
printk(KERN_NOTICE "ftl_cs: block write failed!\n");
printk(KERN_NOTICE "ftl_cs: log_addr = 0x%x, virt_addr"
- " = 0x%x, Offset = 0x%x\n", log_addr, virt_addr,
+ " = 0x%x, Offset = 0x%zx\n", log_addr, virt_addr,
offset);
return -EIO;
}
-
+
/* Only delete the old entry when the new entry is ready */
old_addr = part->VirtualBlockMap[sector+i];
if (old_addr != 0xffffffff) {
@@ -1145,309 +979,121 @@ static int ftl_write(partition_t *part, caddr_t buffer,
return -EIO;
part->VirtualBlockMap[sector+i] = log_addr;
part->EUNInfo[part->bam_index].Deleted--;
-
+
buffer += SECTOR_SIZE;
virt_addr += SECTOR_SIZE;
}
return 0;
} /* ftl_write */
-/*======================================================================
-
- IOCTL calls for getting device parameters.
-
-======================================================================*/
-
-static int ftl_ioctl(struct inode *inode, struct file *file,
- u_int cmd, u_long arg)
+static int ftl_getgeo(struct mtd_blktrans_dev *dev, struct hd_geometry *geo)
{
- struct hd_geometry *geo = (struct hd_geometry *)arg;
- int ret = 0, minor = MINOR(inode->i_rdev);
- partition_t *part= myparts[minor >> 4];
- u_long sect;
-
- if (!part)
- return -ENODEV; /* How? */
-
- switch (cmd) {
- case HDIO_GETGEO:
- ret = verify_area(VERIFY_WRITE, (long *)arg, sizeof(*geo));
- if (ret) return ret;
- /* Sort of arbitrary: round size down to 4K boundary */
- sect = le32_to_cpu(part->header.FormattedSize)/SECTOR_SIZE;
- put_user(1, (char *)&geo->heads);
- put_user(8, (char *)&geo->sectors);
- put_user((sect>>3), (short *)&geo->cylinders);
- put_user(ftl_hd[minor].start_sect, (u_long *)&geo->start);
- break;
- case BLKGETSIZE:
- ret = put_user(ftl_hd[minor].nr_sects, (unsigned long *)arg);
- break;
-#ifdef BLKGETSIZE64
- case BLKGETSIZE64:
- ret = put_user((u64)ftl_hd[minor].nr_sects << 9, (u64 *)arg);
- break;
-#endif
- case BLKRRPART:
- ret = ftl_reread_partitions(minor);
- break;
-#if (LINUX_VERSION_CODE < 0x20303)
- case BLKFLSBUF:
-#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,2,0)
- if (!capable(CAP_SYS_ADMIN)) return -EACCES;
-#endif
- fsync_dev(inode->i_rdev);
- invalidate_buffers(inode->i_rdev);
- break;
- RO_IOCTLS(inode->i_rdev, arg);
-#else
- case BLKROSET:
- case BLKROGET:
- case BLKFLSBUF:
- ret = blk_ioctl(inode->i_rdev, cmd, arg);
- break;
-#endif
- default:
- ret = -EINVAL;
- }
-
- return ret;
-} /* ftl_ioctl */
+ partition_t *part = (void *)dev;
+ u_long sect;
-/*======================================================================
+ /* Sort of arbitrary: round size down to 4KiB boundary */
+ sect = le32_to_cpu(part->header.FormattedSize)/SECTOR_SIZE;
- Handler for block device requests
+ geo->heads = 1;
+ geo->sectors = 8;
+ geo->cylinders = sect >> 3;
-======================================================================*/
+ return 0;
+}
-static int ftl_reread_partitions(int minor)
+static int ftl_readsect(struct mtd_blktrans_dev *dev,
+ unsigned long block, char *buf)
{
- partition_t *part = myparts[minor >> 4];
- int i, whole;
-
- DEBUG(0, "ftl_cs: ftl_reread_partition(%d)\n", minor);
- if ((atomic_read(&part->open) > 1)) {
- return -EBUSY;
- }
- whole = minor & ~(MAX_PART-1);
-
- i = MAX_PART - 1;
- while (i-- > 0) {
- if (ftl_hd[whole+i].nr_sects > 0) {
- kdev_t rdev = MKDEV(FTL_MAJOR, whole+i);
-
- invalidate_device(rdev, 1);
- }
- ftl_hd[whole+i].start_sect = 0;
- ftl_hd[whole+i].nr_sects = 0;
- }
-
- scan_header(part);
-
- register_disk(&ftl_gendisk, whole >> PART_BITS, MAX_PART,
- &ftl_blk_fops, le32_to_cpu(part->header.FormattedSize)/SECTOR_SIZE);
-
-#ifdef PCMCIA_DEBUG
- for (i = 0; i < MAX_PART; i++) {
- if (ftl_hd[whole+i].nr_sects > 0)
- printk(KERN_INFO " %d: start %ld size %ld\n", i,
- ftl_hd[whole+i].start_sect,
- ftl_hd[whole+i].nr_sects);
- }
-#endif
- return 0;
+ return ftl_read((void *)dev, buf, block, 1);
}
-/*======================================================================
-
- Handler for block device requests
-
-======================================================================*/
-
-static void do_ftl_request(request_arg_t)
+static int ftl_writesect(struct mtd_blktrans_dev *dev,
+ unsigned long block, char *buf)
{
- int ret, minor;
- partition_t *part;
-
- do {
- // sti();
- INIT_REQUEST;
-
- minor = MINOR(CURRENT->rq_dev);
-
- part = myparts[minor >> 4];
- if (part) {
- ret = 0;
-
- switch (CURRENT->cmd) {
- case READ:
- ret = ftl_read(part, CURRENT->buffer,
- CURRENT->sector+ftl_hd[minor].start_sect,
- CURRENT->current_nr_sectors);
- if (ret) printk("ftl_read returned %d\n", ret);
- break;
-
- case WRITE:
- ret = ftl_write(part, CURRENT->buffer,
- CURRENT->sector+ftl_hd[minor].start_sect,
- CURRENT->current_nr_sectors);
- if (ret) printk("ftl_write returned %d\n", ret);
- break;
-
- default:
- panic("ftl_cs: unknown block command!\n");
-
- }
- } else {
- ret = 1;
- printk("NULL part in ftl_request\n");
- }
-
- if (!ret) {
- CURRENT->sector += CURRENT->current_nr_sectors;
- }
-
- end_request((ret == 0) ? 1 : 0);
- } while (1);
-} /* do_ftl_request */
+ return ftl_write((void *)dev, buf, block, 1);
+}
/*====================================================================*/
void ftl_freepart(partition_t *part)
{
- if (part->VirtualBlockMap) {
vfree(part->VirtualBlockMap);
part->VirtualBlockMap = NULL;
- }
- if (part->VirtualPageMap) {
kfree(part->VirtualPageMap);
part->VirtualPageMap = NULL;
- }
- if (part->EUNInfo) {
kfree(part->EUNInfo);
part->EUNInfo = NULL;
- }
- if (part->XferInfo) {
kfree(part->XferInfo);
part->XferInfo = NULL;
- }
- if (part->bam_cache) {
kfree(part->bam_cache);
part->bam_cache = NULL;
- }
-
} /* ftl_freepart */
-static void ftl_notify_add(struct mtd_info *mtd)
+static void ftl_add_mtd(struct mtd_blktrans_ops *tr, struct mtd_info *mtd)
{
partition_t *partition;
- int device;
-
- for (device=0; device < MAX_MTD_DEVICES && myparts[device]; device++)
- ;
-
- if (device == MAX_MTD_DEVICES) {
- printk(KERN_NOTICE "Maximum number of FTL partitions reached\n"
- "Not scanning <%s>\n", mtd->name);
- return;
- }
partition = kmalloc(sizeof(partition_t), GFP_KERNEL);
-
+
if (!partition) {
printk(KERN_WARNING "No memory to scan for FTL on %s\n",
mtd->name);
return;
- }
+ }
memset(partition, 0, sizeof(partition_t));
- partition->mtd = mtd;
+ partition->mbd.mtd = mtd;
- if ((scan_header(partition) == 0) &&
+ if ((scan_header(partition) == 0) &&
(build_maps(partition) == 0)) {
-
+
partition->state = FTL_FORMATTED;
- atomic_set(&partition->open, 0);
- myparts[device] = partition;
- ftl_reread_partitions(device << 4);
#ifdef PCMCIA_DEBUG
- printk(KERN_INFO "ftl_cs: opening %d kb FTL partition\n",
+ printk(KERN_INFO "ftl_cs: opening %d KiB FTL partition\n",
le32_to_cpu(partition->header.FormattedSize) >> 10);
#endif
- } else
- kfree(partition);
+ partition->mbd.size = le32_to_cpu(partition->header.FormattedSize) >> 9;
+ partition->mbd.blksize = SECTOR_SIZE;
+ partition->mbd.tr = tr;
+ partition->mbd.devnum = -1;
+ if (!add_mtd_blktrans_dev((void *)partition))
+ return;
+ }
+
+ ftl_freepart(partition);
+ kfree(partition);
}
-static void ftl_notify_remove(struct mtd_info *mtd)
+static void ftl_remove_dev(struct mtd_blktrans_dev *dev)
{
- int i,j;
-
- /* Q: What happens if you try to remove a device which has
- * a currently-open FTL partition on it?
- *
- * A: You don't. The ftl_open routine is responsible for
- * increasing the use count of the driver module which
- * it uses.
- */
-
- /* That's the theory, anyway :) */
-
- for (i=0; i< MAX_MTD_DEVICES; i++)
- if (myparts[i] && myparts[i]->mtd == mtd) {
-
- if (myparts[i]->state == FTL_FORMATTED)
- ftl_freepart(myparts[i]);
-
- myparts[i]->state = 0;
- for (j=0; j<16; j++) {
- ftl_gendisk.part[j].nr_sects=0;
- ftl_gendisk.part[j].start_sect=0;
- }
- kfree(myparts[i]);
- myparts[i] = NULL;
- }
+ del_mtd_blktrans_dev(dev);
+ ftl_freepart((partition_t *)dev);
+ kfree(dev);
}
-int init_ftl(void)
+struct mtd_blktrans_ops ftl_tr = {
+ .name = "ftl",
+ .major = FTL_MAJOR,
+ .part_bits = PART_BITS,
+ .readsect = ftl_readsect,
+ .writesect = ftl_writesect,
+ .getgeo = ftl_getgeo,
+ .add_mtd = ftl_add_mtd,
+ .remove_dev = ftl_remove_dev,
+ .owner = THIS_MODULE,
+};
+
+static int init_ftl(void)
{
- int i;
+ DEBUG(0, "$Id: ftl.c,v 1.59 2005/11/29 14:48:31 gleixner Exp $\n");
- memset(myparts, 0, sizeof(myparts));
-
- DEBUG(0, "$Id: ftl.c,v 1.43 2002/02/13 15:31:37 dwmw2 Exp $\n");
-
- if (register_blkdev(FTL_MAJOR, "ftl", &ftl_blk_fops)) {
- printk(KERN_NOTICE "ftl_cs: unable to grab major "
- "device number!\n");
- return -EAGAIN;
- }
-
- for (i = 0; i < MINOR_NR(MAX_DEV, 0, 0); i++)
- ftl_blocksizes[i] = 1024;
- for (i = 0; i < MAX_DEV*MAX_PART; i++) {
- ftl_hd[i].nr_sects = 0;
- ftl_hd[i].start_sect = 0;
- }
- blksize_size[FTL_MAJOR] = ftl_blocksizes;
- ftl_gendisk.major = FTL_MAJOR;
- blk_init_queue(BLK_DEFAULT_QUEUE(FTL_MAJOR), &do_ftl_request);
- add_gendisk(&ftl_gendisk);
-
- register_mtd_user(&ftl_notifier);
-
- return 0;
+ return register_mtd_blktrans(&ftl_tr);
}
static void __exit cleanup_ftl(void)
{
- unregister_mtd_user(&ftl_notifier);
-
- unregister_blkdev(FTL_MAJOR, "ftl");
- blk_cleanup_queue(BLK_DEFAULT_QUEUE(FTL_MAJOR));
- blksize_size[FTL_MAJOR] = NULL;
-
- del_gendisk(&ftl_gendisk);
+ deregister_mtd_blktrans(&ftl_tr);
}
module_init(init_ftl);
@@ -1456,4 +1102,4 @@ module_exit(cleanup_ftl);
MODULE_LICENSE("Dual MPL/GPL");
MODULE_AUTHOR("David Hinds <dahinds@users.sourceforge.net>");
-MODULE_DESCRIPTION("Support code for Flash Translation Layer, used on PCMCIA devices and M-Systems DiskOnChip 1000");
+MODULE_DESCRIPTION("Support code for Flash Translation Layer, used on PCMCIA devices");