/* 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 (C) 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 (http://www.m-sys.com) directly.
======================================================================*/
#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 <asm/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 *VirtualPageMap;
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
/*====================================================================*/
static void ftl_erase_callback(struct erase_info *done);
/*======================================================================
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 = part->mbd.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