2009-12-06 01:19:50 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* drivers/mtd/ftl.c
|
|
|
|
*
|
2020-03-07 20:56:32 +01:00
|
|
|
* Licensed to the Apache Software Foundation (ASF) under one or more
|
|
|
|
* contributor license agreements. See the NOTICE file distributed with
|
|
|
|
* this work for additional information regarding copyright ownership. The
|
|
|
|
* ASF licenses this file to you under the Apache License, Version 2.0 (the
|
|
|
|
* "License"); you may not use this file except in compliance with the
|
|
|
|
* License. You may obtain a copy of the License at
|
2009-12-06 01:19:50 +01:00
|
|
|
*
|
2020-03-07 20:56:32 +01:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2009-12-06 01:19:50 +01:00
|
|
|
*
|
2020-03-07 20:56:32 +01:00
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
|
|
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
|
|
|
* License for the specific language governing permissions and limitations
|
|
|
|
* under the License.
|
2009-12-06 01:19:50 +01:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Included Files
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include <nuttx/config.h>
|
|
|
|
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/ioctl.h>
|
2009-12-15 15:25:14 +01:00
|
|
|
#include <stdint.h>
|
|
|
|
#include <stdbool.h>
|
2009-12-06 01:19:50 +01:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
2018-08-28 16:19:15 +02:00
|
|
|
#include <limits.h>
|
2009-12-06 01:19:50 +01:00
|
|
|
#include <string.h>
|
|
|
|
#include <debug.h>
|
|
|
|
#include <errno.h>
|
|
|
|
|
2011-04-06 18:40:47 +02:00
|
|
|
#include <nuttx/kmalloc.h>
|
2012-03-21 19:01:07 +01:00
|
|
|
#include <nuttx/fs/fs.h>
|
|
|
|
#include <nuttx/fs/ioctl.h>
|
2013-11-15 18:22:23 +01:00
|
|
|
#include <nuttx/mtd/mtd.h>
|
2016-07-20 21:54:38 +02:00
|
|
|
#include <nuttx/drivers/rwbuffer.h>
|
2009-12-06 01:19:50 +01:00
|
|
|
|
|
|
|
/****************************************************************************
|
2014-07-11 19:20:11 +02:00
|
|
|
* Pre-processor Definitions
|
2009-12-06 01:19:50 +01:00
|
|
|
****************************************************************************/
|
|
|
|
|
2018-08-28 16:19:15 +02:00
|
|
|
/* Check if read/write buffer support is needed */
|
|
|
|
|
2014-07-12 00:19:17 +02:00
|
|
|
#if defined(CONFIG_FTL_READAHEAD) || defined(CONFIG_FTL_WRITEBUFFER)
|
|
|
|
# define FTL_HAVE_RWBUFFER 1
|
2009-12-06 01:19:50 +01:00
|
|
|
#endif
|
|
|
|
|
2018-08-28 16:19:15 +02:00
|
|
|
/* The maximum length of the device name paths is the maximum length of a
|
|
|
|
* name plus 5 for the the length of "/dev/" and a NUL terminator.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#define DEV_NAME_MAX (NAME_MAX + 5)
|
|
|
|
|
2009-12-06 01:19:50 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* Private Types
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
struct ftl_struct_s
|
|
|
|
{
|
2020-03-07 20:56:32 +01:00
|
|
|
FAR struct mtd_dev_s *mtd; /* Contained MTD interface */
|
|
|
|
struct mtd_geometry_s geo; /* Device geometry */
|
2014-07-12 00:19:17 +02:00
|
|
|
#ifdef FTL_HAVE_RWBUFFER
|
2020-03-07 20:56:32 +01:00
|
|
|
struct rwbuffer_s rwb; /* Read-ahead/write buffer support */
|
2009-12-06 01:19:50 +01:00
|
|
|
#endif
|
2020-03-07 20:56:32 +01:00
|
|
|
uint16_t blkper; /* R/W blocks per erase block */
|
|
|
|
uint16_t refs; /* Number of references */
|
|
|
|
bool unlinked; /* The driver has been unlinked */
|
|
|
|
FAR uint8_t *eblock; /* One, in-memory erase block */
|
2009-12-06 01:19:50 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Function Prototypes
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static int ftl_open(FAR struct inode *inode);
|
|
|
|
static int ftl_close(FAR struct inode *inode);
|
2009-12-15 15:25:14 +01:00
|
|
|
static ssize_t ftl_reload(FAR void *priv, FAR uint8_t *buffer,
|
2009-12-06 01:19:50 +01:00
|
|
|
off_t startblock, size_t nblocks);
|
2020-03-07 20:56:32 +01:00
|
|
|
static ssize_t ftl_read(FAR struct inode *inode, FAR unsigned char *buffer,
|
2009-12-06 01:19:50 +01:00
|
|
|
size_t start_sector, unsigned int nsectors);
|
2009-12-15 15:25:14 +01:00
|
|
|
static ssize_t ftl_flush(FAR void *priv, FAR const uint8_t *buffer,
|
2009-12-06 01:19:50 +01:00
|
|
|
off_t startblock, size_t nblocks);
|
2020-03-07 20:56:32 +01:00
|
|
|
static ssize_t ftl_write(FAR struct inode *inode,
|
|
|
|
FAR const unsigned char *buffer, size_t start_sector,
|
|
|
|
unsigned int nsectors);
|
|
|
|
static int ftl_geometry(FAR struct inode *inode,
|
|
|
|
FAR struct geometry *geometry);
|
|
|
|
static int ftl_ioctl(FAR struct inode *inode, int cmd,
|
|
|
|
unsigned long arg);
|
2018-11-09 15:22:51 +01:00
|
|
|
#ifndef CONFIG_DISABLE_PSEUDOFS_OPERATIONS
|
|
|
|
static int ftl_unlink(FAR struct inode *inode);
|
|
|
|
#endif
|
2009-12-06 01:19:50 +01:00
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Data
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static const struct block_operations g_bops =
|
|
|
|
{
|
|
|
|
ftl_open, /* open */
|
|
|
|
ftl_close, /* close */
|
|
|
|
ftl_read, /* read */
|
|
|
|
ftl_write, /* write */
|
|
|
|
ftl_geometry, /* geometry */
|
|
|
|
ftl_ioctl /* ioctl */
|
2016-04-15 15:03:28 +02:00
|
|
|
#ifndef CONFIG_DISABLE_PSEUDOFS_OPERATIONS
|
2018-11-09 15:22:51 +01:00
|
|
|
, ftl_unlink /* unlink */
|
2016-04-15 15:03:28 +02:00
|
|
|
#endif
|
2009-12-06 01:19:50 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: ftl_open
|
|
|
|
*
|
|
|
|
* Description: Open the block device
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static int ftl_open(FAR struct inode *inode)
|
|
|
|
{
|
2018-11-09 15:22:51 +01:00
|
|
|
FAR struct ftl_struct_s *dev;
|
|
|
|
|
|
|
|
DEBUGASSERT(inode && inode->i_private);
|
|
|
|
dev = (FAR struct ftl_struct_s *)inode->i_private;
|
|
|
|
|
|
|
|
dev->refs++;
|
2009-12-06 01:19:50 +01:00
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
2011-04-24 19:35:16 +02:00
|
|
|
* Name: ftl_close
|
2009-12-06 01:19:50 +01:00
|
|
|
*
|
|
|
|
* Description: close the block device
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static int ftl_close(FAR struct inode *inode)
|
|
|
|
{
|
2018-11-09 15:22:51 +01:00
|
|
|
FAR struct ftl_struct_s *dev;
|
|
|
|
|
|
|
|
DEBUGASSERT(inode && inode->i_private);
|
|
|
|
dev = (FAR struct ftl_struct_s *)inode->i_private;
|
|
|
|
|
2018-11-09 15:12:52 +01:00
|
|
|
#ifdef CONFIG_FTL_WRITEBUFFER
|
|
|
|
rwb_flush(&dev->rwb);
|
|
|
|
#endif
|
|
|
|
|
2018-11-09 15:22:51 +01:00
|
|
|
if (--dev->refs == 0 && dev->unlinked)
|
|
|
|
{
|
|
|
|
#ifdef FTL_HAVE_RWBUFFER
|
|
|
|
rwb_uninitialize(&dev->rwb);
|
|
|
|
#endif
|
|
|
|
if (dev->eblock)
|
|
|
|
{
|
|
|
|
kmm_free(dev->eblock);
|
|
|
|
}
|
2020-03-07 20:56:32 +01:00
|
|
|
|
2018-11-09 15:22:51 +01:00
|
|
|
kmm_free(dev);
|
|
|
|
}
|
|
|
|
|
2009-12-06 01:19:50 +01:00
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: ftl_reload
|
|
|
|
*
|
2020-02-23 09:50:23 +01:00
|
|
|
* Description: Read the specified number of sectors
|
2009-12-06 01:19:50 +01:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2009-12-15 15:25:14 +01:00
|
|
|
static ssize_t ftl_reload(FAR void *priv, FAR uint8_t *buffer,
|
2009-12-06 01:19:50 +01:00
|
|
|
off_t startblock, size_t nblocks)
|
|
|
|
{
|
|
|
|
struct ftl_struct_s *dev = (struct ftl_struct_s *)priv;
|
|
|
|
ssize_t nread;
|
|
|
|
|
|
|
|
/* Read the full erase block into the buffer */
|
|
|
|
|
|
|
|
nread = MTD_BREAD(dev->mtd, startblock, nblocks, buffer);
|
|
|
|
if (nread != nblocks)
|
|
|
|
{
|
2016-06-12 16:31:22 +02:00
|
|
|
ferr("ERROR: Read %d blocks starting at block %d failed: %d\n",
|
2009-12-06 01:19:50 +01:00
|
|
|
nblocks, startblock, nread);
|
|
|
|
}
|
2014-09-25 21:35:10 +02:00
|
|
|
|
2009-12-06 01:19:50 +01:00
|
|
|
return nread;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: ftl_read
|
|
|
|
*
|
2020-02-23 09:50:23 +01:00
|
|
|
* Description: Read the specified number of sectors
|
2009-12-06 01:19:50 +01:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static ssize_t ftl_read(FAR struct inode *inode, unsigned char *buffer,
|
|
|
|
size_t start_sector, unsigned int nsectors)
|
|
|
|
{
|
2014-07-11 19:20:11 +02:00
|
|
|
FAR struct ftl_struct_s *dev;
|
2009-12-06 01:19:50 +01:00
|
|
|
|
2016-06-11 19:59:51 +02:00
|
|
|
finfo("sector: %d nsectors: %d\n", start_sector, nsectors);
|
2009-12-06 01:19:50 +01:00
|
|
|
|
|
|
|
DEBUGASSERT(inode && inode->i_private);
|
2014-07-11 19:20:11 +02:00
|
|
|
|
|
|
|
dev = (FAR struct ftl_struct_s *)inode->i_private;
|
2018-11-09 15:18:46 +01:00
|
|
|
#ifdef FTL_HAVE_RWBUFFER
|
2009-12-06 01:19:50 +01:00
|
|
|
return rwb_read(&dev->rwb, start_sector, nsectors, buffer);
|
|
|
|
#else
|
|
|
|
return ftl_reload(dev, buffer, start_sector, nsectors);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
2012-07-25 00:56:36 +02:00
|
|
|
* Name: ftl_flush
|
2009-12-06 01:19:50 +01:00
|
|
|
*
|
|
|
|
* Description: Write the specified number of sectors
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2018-11-09 15:21:10 +01:00
|
|
|
static int ftl_alloc_eblock(FAR struct ftl_struct_s *dev)
|
|
|
|
{
|
|
|
|
if (dev->eblock == NULL)
|
|
|
|
{
|
2020-03-07 20:56:32 +01:00
|
|
|
/* Allocate one, in-memory erase block buffer */
|
2018-11-09 15:21:10 +01:00
|
|
|
|
2020-03-07 20:56:32 +01:00
|
|
|
dev->eblock = (FAR uint8_t *)kmm_malloc(dev->geo.erasesize);
|
2018-11-09 15:21:10 +01:00
|
|
|
}
|
|
|
|
|
2020-03-07 20:56:32 +01:00
|
|
|
return dev->eblock != NULL ? OK : -ENOMEM;
|
2018-11-09 15:21:10 +01:00
|
|
|
}
|
|
|
|
|
2009-12-15 15:25:14 +01:00
|
|
|
static ssize_t ftl_flush(FAR void *priv, FAR const uint8_t *buffer,
|
2009-12-06 01:19:50 +01:00
|
|
|
off_t startblock, size_t nblocks)
|
|
|
|
{
|
|
|
|
struct ftl_struct_s *dev = (struct ftl_struct_s *)priv;
|
|
|
|
off_t alignedblock;
|
|
|
|
off_t mask;
|
|
|
|
off_t rwblock;
|
|
|
|
off_t eraseblock;
|
|
|
|
off_t offset;
|
|
|
|
size_t remaining;
|
|
|
|
size_t nxfrd;
|
|
|
|
int nbytes;
|
|
|
|
int ret;
|
2014-04-13 22:32:20 +02:00
|
|
|
|
2009-12-06 01:19:50 +01:00
|
|
|
/* Get the aligned block. Here is is assumed: (1) The number of R/W blocks
|
|
|
|
* per erase block is a power of 2, and (2) the erase begins with that same
|
|
|
|
* alignment.
|
|
|
|
*/
|
|
|
|
|
2013-05-02 16:07:42 +02:00
|
|
|
mask = dev->blkper - 1;
|
|
|
|
alignedblock = (startblock + mask) & ~mask;
|
2009-12-06 01:19:50 +01:00
|
|
|
|
|
|
|
/* Handle partial erase blocks before the first unaligned block */
|
|
|
|
|
|
|
|
remaining = nblocks;
|
|
|
|
if (alignedblock > startblock)
|
|
|
|
{
|
2012-12-07 17:00:56 +01:00
|
|
|
/* Check if the write is shorter than to the end of the erase block */
|
|
|
|
|
|
|
|
bool short_write = (remaining < (alignedblock - startblock));
|
2014-04-13 22:32:20 +02:00
|
|
|
|
2018-11-09 15:21:10 +01:00
|
|
|
ret = ftl_alloc_eblock(dev);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
ferr("ERROR: Failed to allocate an erase block buffer\n");
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2009-12-06 01:19:50 +01:00
|
|
|
/* Read the full erase block into the buffer */
|
|
|
|
|
|
|
|
rwblock = startblock & ~mask;
|
|
|
|
nxfrd = MTD_BREAD(dev->mtd, rwblock, dev->blkper, dev->eblock);
|
|
|
|
if (nxfrd != dev->blkper)
|
|
|
|
{
|
2016-06-12 16:31:22 +02:00
|
|
|
ferr("ERROR: Read erase block %d failed: %d\n", rwblock, nxfrd);
|
2009-12-06 01:19:50 +01:00
|
|
|
return -EIO;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Then erase the erase block */
|
|
|
|
|
|
|
|
eraseblock = rwblock / dev->blkper;
|
|
|
|
ret = MTD_ERASE(dev->mtd, eraseblock, 1);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
2016-06-12 16:31:22 +02:00
|
|
|
ferr("ERROR: Erase block=%d failed: %d\n", eraseblock, ret);
|
2009-12-06 01:19:50 +01:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Copy the user data at the end of the buffered erase block */
|
|
|
|
|
|
|
|
offset = (startblock & mask) * dev->geo.blocksize;
|
2014-04-13 22:32:20 +02:00
|
|
|
|
2012-12-07 17:00:56 +01:00
|
|
|
if (short_write)
|
|
|
|
{
|
|
|
|
nbytes = remaining * dev->geo.blocksize;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
nbytes = dev->geo.erasesize - offset;
|
|
|
|
}
|
2014-04-13 22:32:20 +02:00
|
|
|
|
2016-06-11 19:59:51 +02:00
|
|
|
finfo("Copy %d bytes into erase block=%d at offset=%d\n",
|
2009-12-06 01:19:50 +01:00
|
|
|
nbytes, eraseblock, offset);
|
2012-12-07 17:00:56 +01:00
|
|
|
|
2009-12-06 01:19:50 +01:00
|
|
|
memcpy(dev->eblock + offset, buffer, nbytes);
|
|
|
|
|
2013-05-02 16:07:42 +02:00
|
|
|
/* And write the erase block back to flash */
|
2009-12-06 01:19:50 +01:00
|
|
|
|
|
|
|
nxfrd = MTD_BWRITE(dev->mtd, rwblock, dev->blkper, dev->eblock);
|
|
|
|
if (nxfrd != dev->blkper)
|
|
|
|
{
|
2016-06-12 16:31:22 +02:00
|
|
|
ferr("ERROR: Write erase block %d failed: %d\n", rwblock, nxfrd);
|
2009-12-06 01:19:50 +01:00
|
|
|
return -EIO;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Then update for amount written */
|
|
|
|
|
2012-12-07 17:00:56 +01:00
|
|
|
if (short_write)
|
|
|
|
{
|
|
|
|
remaining = 0;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
remaining -= dev->blkper - (startblock & mask);
|
|
|
|
}
|
2014-04-13 22:32:20 +02:00
|
|
|
|
2012-12-07 17:00:56 +01:00
|
|
|
buffer += nbytes;
|
2009-12-06 01:19:50 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* How handle full erase pages in the middle */
|
|
|
|
|
|
|
|
while (remaining >= dev->blkper)
|
|
|
|
{
|
|
|
|
/* Erase the erase block */
|
|
|
|
|
|
|
|
eraseblock = alignedblock / dev->blkper;
|
|
|
|
ret = MTD_ERASE(dev->mtd, eraseblock, 1);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
2016-06-12 16:31:22 +02:00
|
|
|
ferr("ERROR: Erase block=%d failed: %d\n", eraseblock, ret);
|
2009-12-06 01:19:50 +01:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Write a full erase back to flash */
|
|
|
|
|
2016-06-11 19:59:51 +02:00
|
|
|
finfo("Write %d bytes into erase block=%d at offset=0\n",
|
2009-12-06 01:19:50 +01:00
|
|
|
dev->geo.erasesize, alignedblock);
|
2012-12-07 17:00:56 +01:00
|
|
|
|
2009-12-06 01:19:50 +01:00
|
|
|
nxfrd = MTD_BWRITE(dev->mtd, alignedblock, dev->blkper, buffer);
|
|
|
|
if (nxfrd != dev->blkper)
|
|
|
|
{
|
2020-03-07 20:56:32 +01:00
|
|
|
ferr("ERROR: Write erase block %d failed: %d\n",
|
|
|
|
alignedblock, nxfrd);
|
2009-12-06 01:19:50 +01:00
|
|
|
return -EIO;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Then update for amount written */
|
|
|
|
|
|
|
|
alignedblock += dev->blkper;
|
|
|
|
remaining -= dev->blkper;
|
|
|
|
buffer += dev->geo.erasesize;
|
|
|
|
}
|
|
|
|
|
2015-11-13 18:32:34 +01:00
|
|
|
/* Finally, handle any partial blocks after the last full erase block */
|
2009-12-06 01:19:50 +01:00
|
|
|
|
|
|
|
if (remaining > 0)
|
|
|
|
{
|
2018-11-09 15:21:10 +01:00
|
|
|
ret = ftl_alloc_eblock(dev);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
ferr("ERROR: Failed to allocate an erase block buffer\n");
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2009-12-06 01:19:50 +01:00
|
|
|
/* Read the full erase block into the buffer */
|
|
|
|
|
2018-11-09 15:21:10 +01:00
|
|
|
nxfrd = MTD_BREAD(dev->mtd, alignedblock, dev->blkper, dev->eblock);
|
2009-12-06 01:19:50 +01:00
|
|
|
if (nxfrd != dev->blkper)
|
|
|
|
{
|
2020-03-07 20:56:32 +01:00
|
|
|
ferr("ERROR: Read erase block %d failed: %d\n",
|
|
|
|
alignedblock, nxfrd);
|
2009-12-06 01:19:50 +01:00
|
|
|
return -EIO;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Then erase the erase block */
|
|
|
|
|
|
|
|
eraseblock = alignedblock / dev->blkper;
|
|
|
|
ret = MTD_ERASE(dev->mtd, eraseblock, 1);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
2016-06-12 16:31:22 +02:00
|
|
|
ferr("ERROR: Erase block=%d failed: %d\n", eraseblock, ret);
|
2009-12-06 01:19:50 +01:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Copy the user data at the beginning the buffered erase block */
|
|
|
|
|
|
|
|
nbytes = remaining * dev->geo.blocksize;
|
2016-06-11 19:59:51 +02:00
|
|
|
finfo("Copy %d bytes into erase block=%d at offset=0\n",
|
2009-12-06 01:19:50 +01:00
|
|
|
nbytes, alignedblock);
|
|
|
|
memcpy(dev->eblock, buffer, nbytes);
|
|
|
|
|
|
|
|
/* And write the erase back to flash */
|
|
|
|
|
|
|
|
nxfrd = MTD_BWRITE(dev->mtd, alignedblock, dev->blkper, dev->eblock);
|
|
|
|
if (nxfrd != dev->blkper)
|
|
|
|
{
|
2020-03-07 20:56:32 +01:00
|
|
|
ferr("ERROR: Write erase block %d failed: %d\n",
|
|
|
|
alignedblock, nxfrd);
|
2009-12-06 01:19:50 +01:00
|
|
|
return -EIO;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nblocks;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: ftl_write
|
|
|
|
*
|
|
|
|
* Description: Write (or buffer) the specified number of sectors
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2020-03-07 20:56:32 +01:00
|
|
|
static ssize_t ftl_write(FAR struct inode *inode,
|
|
|
|
FAR const unsigned char *buffer,
|
|
|
|
size_t start_sector, unsigned int nsectors)
|
2009-12-06 01:19:50 +01:00
|
|
|
{
|
|
|
|
struct ftl_struct_s *dev;
|
|
|
|
|
2016-06-11 19:59:51 +02:00
|
|
|
finfo("sector: %d nsectors: %d\n", start_sector, nsectors);
|
2009-12-06 01:19:50 +01:00
|
|
|
|
|
|
|
DEBUGASSERT(inode && inode->i_private);
|
|
|
|
dev = (struct ftl_struct_s *)inode->i_private;
|
2018-11-09 15:18:46 +01:00
|
|
|
#ifdef FTL_HAVE_RWBUFFER
|
2009-12-06 01:19:50 +01:00
|
|
|
return rwb_write(&dev->rwb, start_sector, nsectors, buffer);
|
|
|
|
#else
|
|
|
|
return ftl_flush(dev, buffer, start_sector, nsectors);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: ftl_geometry
|
|
|
|
*
|
|
|
|
* Description: Return device geometry
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2020-03-07 20:56:32 +01:00
|
|
|
static int ftl_geometry(FAR struct inode *inode,
|
|
|
|
FAR struct geometry *geometry)
|
2009-12-06 01:19:50 +01:00
|
|
|
{
|
2020-03-07 20:56:32 +01:00
|
|
|
FAR struct ftl_struct_s *dev;
|
2009-12-06 01:19:50 +01:00
|
|
|
|
2016-06-11 19:59:51 +02:00
|
|
|
finfo("Entry\n");
|
2009-12-06 01:19:50 +01:00
|
|
|
|
|
|
|
DEBUGASSERT(inode);
|
|
|
|
if (geometry)
|
|
|
|
{
|
|
|
|
dev = (struct ftl_struct_s *)inode->i_private;
|
2009-12-15 15:25:14 +01:00
|
|
|
geometry->geo_available = true;
|
|
|
|
geometry->geo_mediachanged = false;
|
|
|
|
geometry->geo_writeenabled = true;
|
2009-12-06 01:19:50 +01:00
|
|
|
geometry->geo_nsectors = dev->geo.neraseblocks * dev->blkper;
|
|
|
|
geometry->geo_sectorsize = dev->geo.blocksize;
|
|
|
|
|
2016-06-11 19:59:51 +02:00
|
|
|
finfo("available: true mediachanged: false writeenabled: %s\n",
|
2009-12-15 15:25:14 +01:00
|
|
|
geometry->geo_writeenabled ? "true" : "false");
|
2016-06-11 19:59:51 +02:00
|
|
|
finfo("nsectors: %d sectorsize: %d\n",
|
2009-12-06 01:19:50 +01:00
|
|
|
geometry->geo_nsectors, geometry->geo_sectorsize);
|
2014-04-13 22:32:20 +02:00
|
|
|
|
2009-12-06 01:19:50 +01:00
|
|
|
return OK;
|
|
|
|
}
|
2014-09-25 21:35:10 +02:00
|
|
|
|
2009-12-06 01:19:50 +01:00
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: ftl_ioctl
|
|
|
|
*
|
|
|
|
* Description: Return device geometry
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static int ftl_ioctl(FAR struct inode *inode, int cmd, unsigned long arg)
|
|
|
|
{
|
|
|
|
struct ftl_struct_s *dev ;
|
2011-08-27 18:31:41 +02:00
|
|
|
int ret;
|
2009-12-06 01:19:50 +01:00
|
|
|
|
2016-06-11 19:59:51 +02:00
|
|
|
finfo("Entry\n");
|
2011-08-27 18:31:41 +02:00
|
|
|
DEBUGASSERT(inode && inode->i_private);
|
2009-12-06 01:19:50 +01:00
|
|
|
|
2018-11-09 15:12:52 +01:00
|
|
|
dev = (struct ftl_struct_s *)inode->i_private;
|
|
|
|
|
2011-08-27 18:31:41 +02:00
|
|
|
/* Only one block driver ioctl command is supported by this driver (and
|
|
|
|
* that command is just passed on to the MTD driver in a slightly
|
|
|
|
* different form).
|
|
|
|
*/
|
2009-12-06 01:19:50 +01:00
|
|
|
|
2011-08-27 18:31:41 +02:00
|
|
|
if (cmd == BIOC_XIPBASE)
|
2009-12-06 01:19:50 +01:00
|
|
|
{
|
2011-08-27 18:31:41 +02:00
|
|
|
/* The argument accompanying the BIOC_XIPBASE should be non-NULL. If
|
|
|
|
* DEBUG is enabled, we will catch it here instead of in the MTD
|
|
|
|
* driver.
|
|
|
|
*/
|
|
|
|
|
2016-06-11 22:14:08 +02:00
|
|
|
#ifdef CONFIG_DEBUG_FEATURES
|
2011-08-27 18:31:41 +02:00
|
|
|
if (arg == 0)
|
2009-12-06 01:19:50 +01:00
|
|
|
{
|
2016-06-11 23:50:49 +02:00
|
|
|
ferr("ERROR: BIOC_XIPBASE argument is NULL\n");
|
2011-08-27 18:31:41 +02:00
|
|
|
return -EINVAL;
|
2009-12-06 01:19:50 +01:00
|
|
|
}
|
2011-08-27 18:31:41 +02:00
|
|
|
#endif
|
|
|
|
|
|
|
|
/* Just change the BIOC_XIPBASE command to the MTDIOC_XIPBASE command. */
|
|
|
|
|
|
|
|
cmd = MTDIOC_XIPBASE;
|
|
|
|
}
|
2018-11-09 15:12:52 +01:00
|
|
|
#ifdef CONFIG_FTL_WRITEBUFFER
|
|
|
|
else if (cmd == BIOC_FLUSH)
|
|
|
|
{
|
|
|
|
return rwb_flush(&dev->rwb);
|
|
|
|
}
|
|
|
|
#endif
|
2011-08-27 18:31:41 +02:00
|
|
|
|
2020-02-23 09:50:23 +01:00
|
|
|
/* No other block driver ioctl commands are not recognized by this
|
2011-08-27 18:31:41 +02:00
|
|
|
* driver. Other possible MTD driver ioctl commands are passed through
|
|
|
|
* to the MTD driver (unchanged).
|
|
|
|
*/
|
|
|
|
|
|
|
|
ret = MTD_IOCTL(dev->mtd, cmd, arg);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
2016-06-11 23:50:49 +02:00
|
|
|
ferr("ERROR: MTD ioctl(%04x) failed: %d\n", cmd, ret);
|
2009-12-06 01:19:50 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2018-11-09 15:22:51 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: ftl_unlink
|
|
|
|
*
|
|
|
|
* Description: Unlink the device
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#ifndef CONFIG_DISABLE_PSEUDOFS_OPERATIONS
|
|
|
|
static int ftl_unlink(FAR struct inode *inode)
|
|
|
|
{
|
|
|
|
FAR struct ftl_struct_s *dev;
|
|
|
|
|
|
|
|
DEBUGASSERT(inode && inode->i_private);
|
|
|
|
dev = (FAR struct ftl_struct_s *)inode->i_private;
|
|
|
|
|
|
|
|
dev->unlinked = true;
|
|
|
|
if (dev->refs == 0)
|
|
|
|
{
|
|
|
|
#ifdef FTL_HAVE_RWBUFFER
|
|
|
|
rwb_uninitialize(&dev->rwb);
|
|
|
|
#endif
|
|
|
|
if (dev->eblock)
|
|
|
|
{
|
|
|
|
kmm_free(dev->eblock);
|
|
|
|
}
|
2020-03-07 20:56:32 +01:00
|
|
|
|
2018-11-09 15:22:51 +01:00
|
|
|
kmm_free(dev);
|
|
|
|
}
|
|
|
|
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2009-12-06 01:19:50 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* Public Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
2018-11-09 15:15:57 +01:00
|
|
|
* Name: ftl_initialize_by_path
|
2009-12-06 01:19:50 +01:00
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Initialize to provide a block driver wrapper around an MTD interface
|
|
|
|
*
|
2011-04-24 19:35:16 +02:00
|
|
|
* Input Parameters:
|
2018-11-09 15:15:57 +01:00
|
|
|
* path - The block device path.
|
2018-08-28 15:15:57 +02:00
|
|
|
* mtd - The MTD device that supports the FLASH interface.
|
2011-04-24 19:35:16 +02:00
|
|
|
*
|
2009-12-06 01:19:50 +01:00
|
|
|
****************************************************************************/
|
|
|
|
|
2018-11-09 15:15:57 +01:00
|
|
|
int ftl_initialize_by_path(FAR const char *path, FAR struct mtd_dev_s *mtd)
|
2009-12-06 01:19:50 +01:00
|
|
|
{
|
|
|
|
struct ftl_struct_s *dev;
|
|
|
|
int ret = -ENOMEM;
|
|
|
|
|
|
|
|
/* Sanity check */
|
|
|
|
|
2018-11-09 15:15:57 +01:00
|
|
|
if (path == NULL || mtd == NULL)
|
2009-12-06 01:19:50 +01:00
|
|
|
{
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
2020-03-07 20:56:32 +01:00
|
|
|
finfo("path=\"%s\"\n", path);
|
|
|
|
|
2011-04-24 19:35:16 +02:00
|
|
|
/* Allocate a FTL device structure */
|
2009-12-06 01:19:50 +01:00
|
|
|
|
2020-03-07 20:56:32 +01:00
|
|
|
dev = (FAR struct ftl_struct_s *)kmm_zalloc(sizeof(struct ftl_struct_s));
|
2009-12-06 01:19:50 +01:00
|
|
|
if (dev)
|
|
|
|
{
|
2011-04-24 19:35:16 +02:00
|
|
|
/* Initialize the FTL device structure */
|
2009-12-06 01:19:50 +01:00
|
|
|
|
|
|
|
dev->mtd = mtd;
|
|
|
|
|
2009-12-17 00:23:46 +01:00
|
|
|
/* Get the device geometry. (casting to uintptr_t first eliminates
|
2009-12-13 16:13:37 +01:00
|
|
|
* complaints on some architectures where the sizeof long is different
|
|
|
|
* from the size of a pointer).
|
|
|
|
*/
|
2009-12-06 01:19:50 +01:00
|
|
|
|
2020-03-07 20:56:32 +01:00
|
|
|
ret = MTD_IOCTL(mtd, MTDIOC_GEOMETRY,
|
|
|
|
(unsigned long)((uintptr_t)&dev->geo));
|
2009-12-06 01:19:50 +01:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
2016-06-12 16:31:22 +02:00
|
|
|
ferr("ERROR: MTD ioctl(MTDIOC_GEOMETRY) failed: %d\n", ret);
|
2014-09-01 01:04:02 +02:00
|
|
|
kmm_free(dev);
|
2009-12-06 01:19:50 +01:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Get the number of R/W blocks per erase block */
|
|
|
|
|
|
|
|
dev->blkper = dev->geo.erasesize / dev->geo.blocksize;
|
2009-12-17 00:23:46 +01:00
|
|
|
DEBUGASSERT(dev->blkper * dev->geo.blocksize == dev->geo.erasesize);
|
2009-12-06 01:19:50 +01:00
|
|
|
|
|
|
|
/* Configure read-ahead/write buffering */
|
|
|
|
|
2014-07-12 00:19:17 +02:00
|
|
|
#ifdef FTL_HAVE_RWBUFFER
|
2009-12-06 01:19:50 +01:00
|
|
|
dev->rwb.blocksize = dev->geo.blocksize;
|
|
|
|
dev->rwb.nblocks = dev->geo.neraseblocks * dev->blkper;
|
|
|
|
dev->rwb.dev = (FAR void *)dev;
|
2018-11-09 15:18:46 +01:00
|
|
|
dev->rwb.wrflush = ftl_flush;
|
|
|
|
dev->rwb.rhreload = ftl_reload;
|
2009-12-06 01:19:50 +01:00
|
|
|
|
2020-03-21 17:36:26 +01:00
|
|
|
#if defined(CONFIG_FTL_WRITEBUFFER)
|
2009-12-06 01:19:50 +01:00
|
|
|
dev->rwb.wrmaxblocks = dev->blkper;
|
|
|
|
#endif
|
|
|
|
|
2014-07-12 00:19:17 +02:00
|
|
|
#ifdef CONFIG_FTL_READAHEAD
|
2009-12-06 01:19:50 +01:00
|
|
|
dev->rwb.rhmaxblocks = dev->blkper;
|
|
|
|
#endif
|
2014-07-11 19:20:11 +02:00
|
|
|
|
2009-12-06 01:19:50 +01:00
|
|
|
ret = rwb_initialize(&dev->rwb);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
2016-06-12 16:31:22 +02:00
|
|
|
ferr("ERROR: rwb_initialize failed: %d\n", ret);
|
2014-09-01 01:04:02 +02:00
|
|
|
kmm_free(dev);
|
2009-12-06 01:19:50 +01:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2011-04-24 19:35:16 +02:00
|
|
|
/* Inode private data is a reference to the FTL device structure */
|
2009-12-06 01:19:50 +01:00
|
|
|
|
2018-11-09 15:15:57 +01:00
|
|
|
ret = register_blockdriver(path, &g_bops, 0, dev);
|
2009-12-06 01:19:50 +01:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
2016-06-12 16:31:22 +02:00
|
|
|
ferr("ERROR: register_blockdriver failed: %d\n", -ret);
|
2018-11-09 15:22:51 +01:00
|
|
|
#ifdef FTL_HAVE_RWBUFFER
|
|
|
|
rwb_uninitialize(&dev->rwb);
|
|
|
|
#endif
|
2014-09-01 01:04:02 +02:00
|
|
|
kmm_free(dev);
|
2009-12-06 01:19:50 +01:00
|
|
|
}
|
|
|
|
}
|
2014-09-25 21:35:10 +02:00
|
|
|
|
2009-12-06 01:19:50 +01:00
|
|
|
return ret;
|
|
|
|
}
|
2018-08-28 15:15:57 +02:00
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: ftl_initialize
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Initialize to provide a block driver wrapper around an MTD interface
|
|
|
|
*
|
|
|
|
* Input Parameters:
|
|
|
|
* minor - The minor device number. The MTD block device will be
|
|
|
|
* registered as as /dev/mtdblockN where N is the minor number.
|
|
|
|
* mtd - The MTD device that supports the FLASH interface.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
int ftl_initialize(int minor, FAR struct mtd_dev_s *mtd)
|
|
|
|
{
|
2018-11-09 15:15:57 +01:00
|
|
|
char path[DEV_NAME_MAX];
|
2018-08-28 15:15:57 +02:00
|
|
|
|
|
|
|
#ifdef CONFIG_DEBUG_FEATURES
|
|
|
|
/* Sanity check */
|
|
|
|
|
|
|
|
if (minor < 0 || minor > 255)
|
|
|
|
{
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2018-11-09 15:15:57 +01:00
|
|
|
/* Do the real work by ftl_initialize_by_path */
|
2018-08-28 15:15:57 +02:00
|
|
|
|
2018-11-09 15:15:57 +01:00
|
|
|
snprintf(path, DEV_NAME_MAX, "/dev/mtdblock%d", minor);
|
|
|
|
return ftl_initialize_by_path(path, mtd);
|
2018-08-28 15:15:57 +02:00
|
|
|
}
|