/* This version ported to the Linux-MTD system by dwmw2@infradead.org * * Fixes: Arnaldo Carvalho de Melo <acme@conectiva.com.br> * - fixes some leaks on failure in build_maps and ftl_notify_add, cleanups * * Based on: *//*====================================================================== A Flash Translation Layer memory card driver This driver implements a disk-like block device driver with an apparent block size of 512 bytes for flash memory cards. ftl_cs.c 1.62 2000/02/01 00:59:04 The contents of this file are subject to the Mozilla Public License Version 1.1 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.mozilla.org/MPL/ Software distributed under the License is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for the specific language governing rights and limitations under the License. The initial developer of the original code is David A. Hinds <dahinds@users.sourceforge.net>. Portions created by David A. Hinds are Copyright © 1999 David A. Hinds. All Rights Reserved. Alternatively, the contents of this file may be used under the terms of the GNU General Public License version 2 (the "GPL"), in which case the provisions of the GPL are applicable instead of the above. If you wish to allow the use of your version of this file only under the terms of the GPL and not to allow others to use your version of this file under the MPL, indicate your decision by deleting the provisions above and replace them with the notice and other provisions required by the GPL. If you do not delete the provisions above, a recipient may use your version of this file under either the MPL or the GPL. LEGAL NOTE: The FTL format is patented by M-Systems. They have granted a license for its use with PCMCIA devices: "M-Systems grants a royalty-free, non-exclusive license under any presently existing M-Systems intellectual property rights necessary for the design and development of FTL-compatible drivers, file systems and utilities using the data formats with PCMCIA PC Cards as described in the PCMCIA Flash Translation Layer (FTL) Specification." Use of the FTL format for non-PCMCIA applications may be an infringement of these patents. For additional information, contact M-Systems directly. M-Systems since acquired by Sandisk. ======================================================================*/#include <linux/mtd/blktrans.h>#include <linux/module.h>#include <linux/mtd/mtd.h>/*#define PSYCHO_DEBUG */ #include <linux/kernel.h>#include <linux/ptrace.h>#include <linux/slab.h>#include <linux/string.h>#include <linux/timer.h>#include <linux/major.h>#include <linux/fs.h>#include <linux/init.h>#include <linux/hdreg.h>#include <linux/vmalloc.h>#include <linux/blkpg.h>#include <linux/uaccess.h> #include <linux/mtd/ftl.h> /*====================================================================*/ /* Parameters that can be set with 'insmod' */static int shuffle_freq = 50;module_param(shuffle_freq, int, 0); /*====================================================================*/ /* Major device # for FTL device */#ifndef FTL_MAJOR#define FTL_MAJOR 44#endif /*====================================================================*/ /* Maximum number of separate memory devices we'll allow */#define MAX_DEV 4 /* Maximum number of regions per device */#define MAX_REGION 4 /* Maximum number of partitions in an FTL region */#define PART_BITS 4 /* Maximum number of outstanding erase requests per socket */#define MAX_ERASE 8 /* Sector size -- shouldn't need to change */#define SECTOR_SIZE 512 /* Each memory region corresponds to a minor device */typedef struct partition_t { struct mtd_blktrans_dev mbd; uint32_t state; uint32_t *VirtualBlockMap; uint32_t FreeTotal; struct eun_info_t { uint32_t Offset; uint32_t EraseCount; uint32_t Free; uint32_t Deleted; } *EUNInfo; struct xfer_info_t { uint32_t Offset; uint32_t EraseCount; uint16_t state; } *XferInfo; uint16_t bam_index; uint32_t *bam_cache; uint16_t DataUnits; uint32_t BlocksPerUnit; erase_unit_header_t header;} partition_t; /* Partition state flags */#define FTL_FORMATTED 0x01 /* Transfer unit states */#define XFER_UNKNOWN 0x00#define XFER_ERASING 0x01#define XFER_ERASED 0x02#define XFER_PREPARED 0x03#define XFER_FAILED 0x04 /*====================================================================== 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; size_t ret; int err; part->header.FormattedSize = 0; 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->mbd.mtd->erasesize ? : 0x2000) { err = mtd_read(part->mbd.mtd, offset, sizeof(header), &ret, (unsigned char *)&header); if (err) return err; if (strcmp(header.DataOrgTuple+3, "FTL100") == 0) break; } if (offset == max_offset) { printk(KERN_NOTICE "ftl_cs: FTL header not found.\n"); return -ENOENT; } 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->mbd.mtd->erasesize) { printk(KERN_NOTICE "ftl: FTL EraseUnitSize %x != MTD erasesize %x\n", 1 << header.EraseUnitSize,part->mbd.mtd->erasesize); return -1; } part->header = header; return 0;} static int build_maps(partition_t *part){ erase_unit_header_t header; uint16_t xvalid, xtrans, i; unsigned blocks, j; int hdr_ok, ret = -1; ssize_t retval; loff_t offset; /* Set up erase unit maps */ part->DataUnits = le16_to_cpu(part->header.NumEraseUnits) - part->header.NumTransferUnits; part->EUNInfo = kmalloc_objs(struct eun_info_t, part->DataUnits); if (!part->EUNInfo) goto out; for (i = 0; i < part->DataUnits; i++) part->EUNInfo[i].Offset = 0xffffffff; part->XferInfo = kmalloc_objs(struct xfer_info_t, part->header.NumTransferUnits); if (!part->XferInfo) goto out_EUNInfo; xvalid = xtrans = 0; for (i = 0; i < le16_to_cpu(part->header.NumEraseUnits); i++) { offset = ((i + le16_to_cpu(part->header.FirstPhysicalEUN)) << part->header.EraseUnitSize); ret = mtd_read(part->mbd.mtd, offset, sizeof(header), &retval, (unsigned char *)&header); if (ret) goto out_XferInfo; ret = -1; /* Is this a transfer partition? */ hdr_ok = (strcmp(header.DataOrgTuple+3, "FTL100") == 0); if (hdr_ok && (le16_to_cpu(header.LogicalEUN) < part->DataUnits) && (part->EUNInfo[le16_to_cpu(header.LogicalEUN)].Offset == 0xffffffff)) { part->EUNInfo[le16_to_cpu(header.LogicalEUN)].Offset = offset; part->EUNInfo[le16_to_cpu(header.LogicalEUN)].EraseCount = le32_to_cpu(header.EraseCount); xvalid++; } else { if (xtrans == part->header.NumTransferUnits) { printk(KERN_NOTICE "ftl_cs: format error: too many " "transfer units!\n"); goto out_XferInfo; } if (hdr_ok && (le16_to_cpu(header.LogicalEUN) == 0xffff)) { part->XferInfo[xtrans].state = XFER_PREPARED; part->XferInfo[xtrans].EraseCount = le32_to_cpu(header.EraseCount); } else { part->XferInfo[xtrans].state = XFER_UNKNOWN; /* Pick anything reasonable for the erase count */ part->XferInfo[xtrans].EraseCount = le32_to_cpu(part->header.EraseCount); } part->XferInfo[xtrans].Offset = offset; xtrans++; } } /* Check for format trouble */ header = part->header; if ((xtrans != header.NumTransferUnits) || (xvalid+xtrans != le16_to_cpu(header.NumEraseUnits))) { printk(KERN_NOTICE "ftl_cs: format error: erase units " "don't add up!\n"); goto out_XferInfo; } /* Set up virtual page map */ blocks = le32_to_cpu(header.FormattedSize) >> header.BlockSize; part->VirtualBlockMap = vmalloc_array(blocks, sizeof(uint32_t)); if (!part->VirtualBlockMap) goto out_XferInfo; memset(part->VirtualBlockMap, 0xff, blocks * sizeof(uint32_t)); part->BlocksPerUnit = (1 << header.EraseUnitSize) >> header.BlockSize; part->bam_cache = kmalloc_array(part->BlocksPerUnit, sizeof(uint32_t), GFP_KERNEL); if (!part->bam_cache) goto out_VirtualBlockMap; part->bam_index = 0xffff; part->FreeTotal = 0; for (i = 0; i < part->DataUnits; i++) { part->EUNInfo[i].Free = 0; part->EUNInfo[i].Deleted = 0; offset = part->EUNInfo[i].Offset + le32_to_cpu(header.BAMOffset); ret = mtd_read(part->mbd.mtd, offset, part->BlocksPerUnit * sizeof(uint32_t), &retval, (unsigned char *)part->bam_cache); if (ret) goto out_bam_cache; for (j = 0; j < part->BlocksPerUnit; j++) { if (BLOCK_FREE(le32_to_cpu(part->bam_cache[j]))) { part->EUNInfo[i].Free++; part->FreeTotal++; } else if ((BLOCK_TYPE(le32_to_cpu(part->bam_cache[j])) == BLOCK_DATA) && (BLOCK_NUMBER(le32_to_cpu(part->bam_cache[j])) < blocks)) part->VirtualBlockMap[BLOCK_NUMBER(le32_to_cpu(part->bam_cache[j]))] = (i << header.EraseUnitSize) + (j << header.BlockSize); else if (BLOCK_DELETED(le32_to_cpu(part->bam_cache[j]))) part->EUNInfo[i].Deleted++; } } ret = 0; goto out; out_bam_cache: kfree(part->bam_cache);out_VirtualBlockMap: vfree(part->VirtualBlockMap);out_XferInfo: kfree(part->XferInfo);out_EUNInfo: kfree(part->EUNInfo);out: return ret;} /* build_maps */ /*====================================================================== Erase_xfer() schedules an asynchronous erase operation for a transfer unit. ======================================================================*/ static int erase_xfer(partition_t *part, uint16_t xfernum){ int ret; struct xfer_info_t *xfer; struct erase_info *erase; xfer = &part->XferInfo[xfernum]; pr_debug("ftl_cs: erasing xfer unit at 0x%x\n", xfer->Offset); xfer->state = XFER_ERASING; /* Is there a free erase slot? Always in MTD. */ erase=kmalloc_obj(struct erase_info); if (!erase) return -ENOMEM; erase->addr = xfer->Offset; erase->len = 1ULL << part->header.EraseUnitSize; ret = mtd_erase(part->mbd.mtd, erase); if (!ret) { xfer->state = XFER_ERASED; xfer->EraseCount++; } else { xfer->state = XFER_FAILED; pr_notice("ftl_cs: erase failed: err = %d\n", ret); } kfree(erase); return ret;} /* erase_xfer */ /*====================================================================== Prepare_xfer() takes a freshly erased transfer unit and gives it an appropriate header. ======================================================================*/ static int prepare_xfer(partition_t *part, int i){ erase_unit_header_t header; struct xfer_info_t *xfer; int nbam, ret; uint32_t ctl; ssize_t retlen; loff_t offset; xfer = &part->XferInfo[i]; xfer->state = XFER_FAILED; pr_debug("ftl_cs: preparing xfer unit at 0x%x\n", xfer->Offset); /* Write the transfer unit header */ header = part->header; header.LogicalEUN = cpu_to_le16(0xffff); header.EraseCount = cpu_to_le32(xfer->EraseCount); ret = mtd_write(part->mbd.mtd, xfer->Offset, sizeof(header), &retlen, (u_char *)&header); if (ret) { return ret; } /* Write the BAM stub */ nbam = DIV_ROUND_UP(part->BlocksPerUnit * sizeof(uint32_t) + le32_to_cpu(part->header.BAMOffset), SECTOR_SIZE); offset = xfer->Offset + le32_to_cpu(part->header.BAMOffset); ctl = cpu_to_le32(BLOCK_CONTROL); for (i = 0; i < nbam; i++, offset += sizeof(uint32_t)) { ret = mtd_write(part->mbd.mtd, offset, sizeof(uint32_t), &retlen, (u_char *)&ctl); if (ret) return ret; } xfer->state = XFER_PREPARED; return 0; } /* prepare_xfer */ /*====================================================================== Copy_erase_unit() takes a full erase block and a transfer unit, copies everything to the transfer unit, then swaps the block pointers. 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, uint16_t srcunit, uint16_t xferunit){ u_char buf[SECTOR_SIZE]; struct eun_info_t *eun; struct xfer_info_t *xfer; uint32_t src, dest, free, i; uint16_t unit; int ret; ssize_t retlen; loff_t offset; uint16_t srcunitswap = cpu_to_le16(srcunit); eun = &part->EUNInfo[srcunit]; xfer = &part->XferInfo[xferunit]; pr_debug("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 = mtd_read(part->mbd.mtd, offset, part->BlocksPerUnit * sizeof(uint32_t), &retlen, (u_char *)(part->bam_cache)); /* mark the cache bad, in case we get an error later */ part->bam_index = 0xffff; if (ret) { 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 = mtd_write(part->mbd.mtd, offset, sizeof(uint16_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; free = 0; ret = 0; for (i = 0; i < part->BlocksPerUnit; i++) { switch (BLOCK_TYPE(le32_to_cpu(part->bam_cache[i]))) { case BLOCK_CONTROL: /* This gets updated later */ break; case BLOCK_DATA: case BLOCK_REPLACEMENT: ret = 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"); return ret; } ret = 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"); return ret; }Lines 1–500 of 1060. Load more
/* This version ported to the Linux-MTD system by dwmw2@infradead.org * * Fixes: Arnaldo Carvalho de Melo <acme@conectiva.com.br> * - fixes some leaks on failure in build_maps and ftl_notify_add, cleanups * * Based on: */ /*====================================================================== A Flash Translation Layer memory card driver This driver implements a disk-like block device driver with an apparent block size of 512 bytes for flash memory cards. ftl_cs.c 1.62 2000/02/01 00:59:04 The contents of this file are subject to the Mozilla Public License Version 1.1 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.mozilla.org/MPL/ Software distributed under the License is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for the specific language governing rights and limitations under the License. The initial developer of the original code is David A. Hinds <dahinds@users.sourceforge.net>. Portions created by David A. Hinds are Copyright © 1999 David A. Hinds. All Rights Reserved. Alternatively, the contents of this file may be used under the terms of the GNU General Public License version 2 (the "GPL"), in which case the provisions of the GPL are applicable instead of the above. If you wish to allow the use of your version of this file only under the terms of the GPL and not to allow others to use your version of this file under the MPL, indicate your decision by deleting the provisions above and replace them with the notice and other provisions required by the GPL. If you do not delete the provisions above, a recipient may use your version of this file under either the MPL or the GPL. LEGAL NOTE: The FTL format is patented by M-Systems. They have granted a license for its use with PCMCIA devices: "M-Systems grants a royalty-free, non-exclusive license under any presently existing M-Systems intellectual property rights necessary for the design and development of FTL-compatible drivers, file systems and utilities using the data formats with PCMCIA PC Cards as described in the PCMCIA Flash Translation Layer (FTL) Specification." Use of the FTL format for non-PCMCIA applications may be an infringement of these patents. For additional information, contact M-Systems directly. M-Systems since acquired by Sandisk. ======================================================================*/ #include <linux/mtd/blktrans.h> #include <linux/module.h> #include <linux/mtd/mtd.h> /*#define PSYCHO_DEBUG */ #include <linux/kernel.h> #include <linux/ptrace.h> #include <linux/slab.h> #include <linux/string.h> #include <linux/timer.h> #include <linux/major.h> #include <linux/fs.h> #include <linux/init.h> #include <linux/hdreg.h> #include <linux/vmalloc.h> #include <linux/blkpg.h> #include <linux/uaccess.h> #include <linux/mtd/ftl.h> /*====================================================================*/ /* Parameters that can be set with 'insmod' */ static int shuffle_freq = 50; module_param(shuffle_freq, int, 0); /*====================================================================*/ /* Major device # for FTL device */ #ifndef FTL_MAJOR #define FTL_MAJOR 44 #endif /*====================================================================*/ /* Maximum number of separate memory devices we'll allow */ #define MAX_DEV 4 /* Maximum number of regions per device */ #define MAX_REGION 4 /* Maximum number of partitions in an FTL region */ #define PART_BITS 4 /* Maximum number of outstanding erase requests per socket */ #define MAX_ERASE 8 /* Sector size -- shouldn't need to change */ #define SECTOR_SIZE 512 /* Each memory region corresponds to a minor device */ typedef struct partition_t { struct mtd_blktrans_dev mbd; uint32_t state; uint32_t *VirtualBlockMap; uint32_t FreeTotal; struct eun_info_t { uint32_t Offset; uint32_t EraseCount; uint32_t Free; uint32_t Deleted; } *EUNInfo; struct xfer_info_t { uint32_t Offset; uint32_t EraseCount; uint16_t state; } *XferInfo; uint16_t bam_index; uint32_t *bam_cache; uint16_t DataUnits; uint32_t BlocksPerUnit; erase_unit_header_t header; } partition_t; /* Partition state flags */ #define FTL_FORMATTED 0x01 /* Transfer unit states */ #define XFER_UNKNOWN 0x00 #define XFER_ERASING 0x01 #define XFER_ERASED 0x02 #define XFER_PREPARED 0x03 #define XFER_FAILED 0x04 /*====================================================================== 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; size_t ret; int err; part->header.FormattedSize = 0; 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->mbd.mtd->erasesize ? : 0x2000) { err = mtd_read(part->mbd.mtd, offset, sizeof(header), &ret, (unsigned char *)&header); if (err) return err; if (strcmp(header.DataOrgTuple+3, "FTL100") == 0) break; } if (offset == max_offset) { printk(KERN_NOTICE "ftl_cs: FTL header not found.\n"); return -ENOENT; } 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->mbd.mtd->erasesize) { printk(KERN_NOTICE "ftl: FTL EraseUnitSize %x != MTD erasesize %x\n", 1 << header.EraseUnitSize,part->mbd.mtd->erasesize); return -1; } part->header = header; return 0; } static int build_maps(partition_t *part) { erase_unit_header_t header; uint16_t xvalid, xtrans, i; unsigned blocks, j; int hdr_ok, ret = -1; ssize_t retval; loff_t offset; /* Set up erase unit maps */ part->DataUnits = le16_to_cpu(part->header.NumEraseUnits) - part->header.NumTransferUnits; part->EUNInfo = kmalloc_objs(struct eun_info_t, part->DataUnits); if (!part->EUNInfo) goto out; for (i = 0; i < part->DataUnits; i++) part->EUNInfo[i].Offset = 0xffffffff; part->XferInfo = kmalloc_objs(struct xfer_info_t, part->header.NumTransferUnits); if (!part->XferInfo) goto out_EUNInfo; xvalid = xtrans = 0; for (i = 0; i < le16_to_cpu(part->header.NumEraseUnits); i++) { offset = ((i + le16_to_cpu(part->header.FirstPhysicalEUN)) << part->header.EraseUnitSize); ret = mtd_read(part->mbd.mtd, offset, sizeof(header), &retval, (unsigned char *)&header); if (ret) goto out_XferInfo; ret = -1; /* Is this a transfer partition? */ hdr_ok = (strcmp(header.DataOrgTuple+3, "FTL100") == 0); if (hdr_ok && (le16_to_cpu(header.LogicalEUN) < part->DataUnits) && (part->EUNInfo[le16_to_cpu(header.LogicalEUN)].Offset == 0xffffffff)) { part->EUNInfo[le16_to_cpu(header.LogicalEUN)].Offset = offset; part->EUNInfo[le16_to_cpu(header.LogicalEUN)].EraseCount = le32_to_cpu(header.EraseCount); xvalid++; } else { if (xtrans == part->header.NumTransferUnits) { printk(KERN_NOTICE "ftl_cs: format error: too many " "transfer units!\n"); goto out_XferInfo; } if (hdr_ok && (le16_to_cpu(header.LogicalEUN) == 0xffff)) { part->XferInfo[xtrans].state = XFER_PREPARED; part->XferInfo[xtrans].EraseCount = le32_to_cpu(header.EraseCount); } else { part->XferInfo[xtrans].state = XFER_UNKNOWN; /* Pick anything reasonable for the erase count */ part->XferInfo[xtrans].EraseCount = le32_to_cpu(part->header.EraseCount); } part->XferInfo[xtrans].Offset = offset; xtrans++; } } /* Check for format trouble */ header = part->header; if ((xtrans != header.NumTransferUnits) || (xvalid+xtrans != le16_to_cpu(header.NumEraseUnits))) { printk(KERN_NOTICE "ftl_cs: format error: erase units " "don't add up!\n"); goto out_XferInfo; } /* Set up virtual page map */ blocks = le32_to_cpu(header.FormattedSize) >> header.BlockSize; part->VirtualBlockMap = vmalloc_array(blocks, sizeof(uint32_t)); if (!part->VirtualBlockMap) goto out_XferInfo; memset(part->VirtualBlockMap, 0xff, blocks * sizeof(uint32_t)); part->BlocksPerUnit = (1 << header.EraseUnitSize) >> header.BlockSize; part->bam_cache = kmalloc_array(part->BlocksPerUnit, sizeof(uint32_t), GFP_KERNEL); if (!part->bam_cache) goto out_VirtualBlockMap; part->bam_index = 0xffff; part->FreeTotal = 0; for (i = 0; i < part->DataUnits; i++) { part->EUNInfo[i].Free = 0; part->EUNInfo[i].Deleted = 0; offset = part->EUNInfo[i].Offset + le32_to_cpu(header.BAMOffset); ret = mtd_read(part->mbd.mtd, offset, part->BlocksPerUnit * sizeof(uint32_t), &retval, (unsigned char *)part->bam_cache); if (ret) goto out_bam_cache; for (j = 0; j < part->BlocksPerUnit; j++) { if (BLOCK_FREE(le32_to_cpu(part->bam_cache[j]))) { part->EUNInfo[i].Free++; part->FreeTotal++; } else if ((BLOCK_TYPE(le32_to_cpu(part->bam_cache[j])) == BLOCK_DATA) && (BLOCK_NUMBER(le32_to_cpu(part->bam_cache[j])) < blocks)) part->VirtualBlockMap[BLOCK_NUMBER(le32_to_cpu(part->bam_cache[j]))] = (i << header.EraseUnitSize) + (j << header.BlockSize); else if (BLOCK_DELETED(le32_to_cpu(part->bam_cache[j]))) part->EUNInfo[i].Deleted++; } } ret = 0; goto out; out_bam_cache: kfree(part->bam_cache); out_VirtualBlockMap: vfree(part->VirtualBlockMap); out_XferInfo: kfree(part->XferInfo); out_EUNInfo: kfree(part->EUNInfo); out: return ret; } /* build_maps */ /*====================================================================== Erase_xfer() schedules an asynchronous erase operation for a transfer unit. ======================================================================*/ static int erase_xfer(partition_t *part, uint16_t xfernum) { int ret; struct xfer_info_t *xfer; struct erase_info *erase; xfer = &part->XferInfo[xfernum]; pr_debug("ftl_cs: erasing xfer unit at 0x%x\n", xfer->Offset); xfer->state = XFER_ERASING; /* Is there a free erase slot? Always in MTD. */ erase=kmalloc_obj(struct erase_info); if (!erase) return -ENOMEM; erase->addr = xfer->Offset; erase->len = 1ULL << part->header.EraseUnitSize; ret = mtd_erase(part->mbd.mtd, erase); if (!ret) { xfer->state = XFER_ERASED; xfer->EraseCount++; } else { xfer->state = XFER_FAILED; pr_notice("ftl_cs: erase failed: err = %d\n", ret); } kfree(erase); return ret; } /* erase_xfer */ /*====================================================================== Prepare_xfer() takes a freshly erased transfer unit and gives it an appropriate header. ======================================================================*/ static int prepare_xfer(partition_t *part, int i) { erase_unit_header_t header; struct xfer_info_t *xfer; int nbam, ret; uint32_t ctl; ssize_t retlen; loff_t offset; xfer = &part->XferInfo[i]; xfer->state = XFER_FAILED; pr_debug("ftl_cs: preparing xfer unit at 0x%x\n", xfer->Offset); /* Write the transfer unit header */ header = part->header; header.LogicalEUN = cpu_to_le16(0xffff); header.EraseCount = cpu_to_le32(xfer->EraseCount); ret = mtd_write(part->mbd.mtd, xfer->Offset, sizeof(header), &retlen, (u_char *)&header); if (ret) { return ret; } /* Write the BAM stub */ nbam = DIV_ROUND_UP(part->BlocksPerUnit * sizeof(uint32_t) + le32_to_cpu(part->header.BAMOffset), SECTOR_SIZE); offset = xfer->Offset + le32_to_cpu(part->header.BAMOffset); ctl = cpu_to_le32(BLOCK_CONTROL); for (i = 0; i < nbam; i++, offset += sizeof(uint32_t)) { ret = mtd_write(part->mbd.mtd, offset, sizeof(uint32_t), &retlen, (u_char *)&ctl); if (ret) return ret; } xfer->state = XFER_PREPARED; return 0; } /* prepare_xfer */ /*====================================================================== Copy_erase_unit() takes a full erase block and a transfer unit, copies everything to the transfer unit, then swaps the block pointers. 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, uint16_t srcunit, uint16_t xferunit) { u_char buf[SECTOR_SIZE]; struct eun_info_t *eun; struct xfer_info_t *xfer; uint32_t src, dest, free, i; uint16_t unit; int ret; ssize_t retlen; loff_t offset; uint16_t srcunitswap = cpu_to_le16(srcunit); eun = &part->EUNInfo[srcunit]; xfer = &part->XferInfo[xferunit]; pr_debug("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 = mtd_read(part->mbd.mtd, offset, part->BlocksPerUnit * sizeof(uint32_t), &retlen, (u_char *)(part->bam_cache)); /* mark the cache bad, in case we get an error later */ part->bam_index = 0xffff; if (ret) { 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 = mtd_write(part->mbd.mtd, offset, sizeof(uint16_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; free = 0; ret = 0; for (i = 0; i < part->BlocksPerUnit; i++) { switch (BLOCK_TYPE(le32_to_cpu(part->bam_cache[i]))) { case BLOCK_CONTROL: /* This gets updated later */ break; case BLOCK_DATA: case BLOCK_REPLACEMENT: ret = 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"); return ret; } ret = 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"); return ret; }