/* This version ported to the Linux-MTD system by [email protected] * * Fixes: Arnaldo Carvalho de Melo <[email protected]> * - 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 <[email protected]>. 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 = …; module_param(shuffle_freq, int, 0); /*====================================================================*/ /* Major device # for FTL device */ #ifndef FTL_MAJOR #define FTL_MAJOR … #endif /*====================================================================*/ /* Maximum number of separate memory devices we'll allow */ #define MAX_DEV … /* Maximum number of regions per device */ #define MAX_REGION … /* Maximum number of partitions in an FTL region */ #define PART_BITS … /* Maximum number of outstanding erase requests per socket */ #define MAX_ERASE … /* Sector size -- shouldn't need to change */ #define SECTOR_SIZE … /* Each memory region corresponds to a minor device */ partition_t; /* Partition state flags */ #define FTL_FORMATTED … /* Transfer unit states */ #define XFER_UNKNOWN … #define XFER_ERASING … #define XFER_ERASED … #define XFER_PREPARED … #define XFER_FAILED … /*====================================================================== 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) { … } static int build_maps(partition_t *part) { … } /* build_maps */ /*====================================================================== Erase_xfer() schedules an asynchronous erase operation for a transfer unit. ======================================================================*/ static int erase_xfer(partition_t *part, uint16_t xfernum) { … } /* 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) { … } /* 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) { … } /* copy_erase_unit */ /*====================================================================== reclaim_block() picks a full erase unit and a transfer unit and then calls copy_erase_unit() to copy one to the other. Then, it schedules an erase on the expired block. What's a good way to decide which transfer unit and which erase unit to use? Beats me. My way is to always pick the transfer unit with the fewest erases, and usually pick the data unit with the most deleted blocks. But with a small probability, pick the oldest data unit instead. This means that we generally postpone the next reclamation as long as possible, but shuffle static stuff around a bit for wear leveling. ======================================================================*/ static int reclaim_block(partition_t *part) { … } /* reclaim_block */ /*====================================================================== Find_free() searches for a free block. If necessary, it updates the BAM cache for the erase unit containing the free block. It 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 static void dump_lists(partition_t *part) { int i; printk(KERN_DEBUG "ftl_cs: Free total = %d\n", part->FreeTotal); for (i = 0; i < part->DataUnits; i++) printk(KERN_DEBUG "ftl_cs: unit %d: %d phys, %d free, " "%d deleted\n", i, part->EUNInfo[i].Offset >> part->header.EraseUnitSize, part->EUNInfo[i].Free, part->EUNInfo[i].Deleted); } #endif static uint32_t find_free(partition_t *part) { … } /* find_free */ /*====================================================================== Read a series of sectors from an FTL partition. ======================================================================*/ static int ftl_read(partition_t *part, caddr_t buffer, u_long sector, u_long nblocks) { … } /* ftl_read */ /*====================================================================== Write a series of sectors to an FTL partition ======================================================================*/ static int set_bam_entry(partition_t *part, uint32_t log_addr, uint32_t virt_addr) { … } /* set_bam_entry */ static int ftl_write(partition_t *part, caddr_t buffer, u_long sector, u_long nblocks) { … } /* ftl_write */ static int ftl_getgeo(struct mtd_blktrans_dev *dev, struct hd_geometry *geo) { … } static int ftl_readsect(struct mtd_blktrans_dev *dev, unsigned long block, char *buf) { … } static int ftl_writesect(struct mtd_blktrans_dev *dev, unsigned long block, char *buf) { … } static int ftl_discardsect(struct mtd_blktrans_dev *dev, unsigned long sector, unsigned nr_sects) { … } /*====================================================================*/ static void ftl_freepart(partition_t *part) { … } /* ftl_freepart */ static void ftl_add_mtd(struct mtd_blktrans_ops *tr, struct mtd_info *mtd) { … } static void ftl_remove_dev(struct mtd_blktrans_dev *dev) { … } static struct mtd_blktrans_ops ftl_tr = …; module_mtd_blktrans(…); MODULE_LICENSE(…) …; MODULE_AUTHOR(…) …; MODULE_DESCRIPTION(…) …;