[PATCH v2 15/46] mtd: nandsim: Introduce backend operations

2016-09-21 Thread Daniel Walter
From: Richard Weinberger 

...to untangle the ram based and cachefile based
code.
It will help us later supporting different backends.

Signed-off-by: Richard Weinberger 
---
 drivers/mtd/nand/nandsim.c | 400 ++---
 1 file changed, 228 insertions(+), 172 deletions(-)

diff --git a/drivers/mtd/nand/nandsim.c b/drivers/mtd/nand/nandsim.c
index 2e02089..633872a 100644
--- a/drivers/mtd/nand/nandsim.c
+++ b/drivers/mtd/nand/nandsim.c
@@ -314,6 +314,7 @@ struct nandsim_params {
unsigned int bbt;
unsigned int bch;
unsigned char *id_bytes;
+   struct ns_backend_ops *bops;
 };
 
 struct nandsim_debug_info {
@@ -329,6 +330,22 @@ union ns_mem {
uint16_t *word;  /* for 16-bit word access */
 };
 
+struct ns_ram_data {
+   /* The simulated NAND flash pages array */
+   union ns_mem *pages;
+
+   /* Slab allocator for nand pages */
+   struct kmem_cache *nand_pages_slab;
+};
+
+struct ns_cachefile_data {
+   struct file *cfile; /* Open file */
+   unsigned long *pages_written; /* Which pages have been written */
+   void *file_buf;
+   struct page *held_pages[NS_MAX_HELD_PAGES];
+   int held_cnt;
+};
+
 /*
  * The structure which describes all the internal simulator data.
  */
@@ -348,12 +365,6 @@ struct nandsim {
uint16_t npstates;  /* number of previous states saved */
uint16_t stateidx;  /* current state index */
 
-   /* The simulated NAND flash pages array */
-   union ns_mem *pages;
-
-   /* Slab allocator for nand pages */
-   struct kmem_cache *nand_pages_slab;
-
/* Internal buffer of page + OOB size bytes */
union ns_mem buf;
 
@@ -394,12 +405,8 @@ struct nandsim {
 int wp;  /* write Protect */
 } lines;
 
-   /* Fields needed when using a cache file */
-   struct file *cfile; /* Open file */
-   unsigned long *pages_written; /* Which pages have been written */
-   void *file_buf;
-   struct page *held_pages[NS_MAX_HELD_PAGES];
-   int held_cnt;
+   struct ns_backend_ops *bops;
+   void *backend_data;
 
struct list_head weak_blocks;
struct list_head weak_pages;
@@ -420,6 +427,17 @@ struct nandsim {
struct nandsim_debug_info dbg;
 };
 
+struct ns_backend_ops {
+   void (*erase_sector)(struct nandsim *ns);
+   int (*prog_page)(struct nandsim *ns, int num);
+   void (*read_page)(struct nandsim *ns, int num);
+   int (*init)(struct nandsim *ns, struct nandsim_params *nsparam);
+   void (*destroy)(struct nandsim *ns);
+};
+
+static struct ns_backend_ops ns_ram_bops;
+static struct ns_backend_ops ns_cachefile_bops;
+
 /*
  * Operations array. To perform any operation the simulator must pass
  * through the correspondent states chain.
@@ -641,95 +659,105 @@ static void nandsim_debugfs_remove(struct nandsim *ns)
debugfs_remove_recursive(ns->dbg.dfs_root);
 }
 
-/*
- * Allocate array of page pointers, create slab allocation for an array
- * and initialize the array by NULL pointers.
- *
- * RETURNS: 0 if success, -ENOMEM if memory alloc fails.
- */
-static int alloc_device(struct nandsim *ns, struct nandsim_params *nsparam)
+static int ns_ram_init(struct nandsim *ns, struct nandsim_params *nsparam)
 {
-   struct file *cfile;
-   int i, err;
-
-   if (nsparam->cache_file) {
-   cfile = filp_open(nsparam->cache_file, O_CREAT | O_RDWR | 
O_LARGEFILE, 0600);
-   if (IS_ERR(cfile))
-   return PTR_ERR(cfile);
-   if (!(cfile->f_mode & FMODE_CAN_READ)) {
-   NS_ERR("alloc_device: cache file not readable\n");
-   err = -EINVAL;
-   goto err_close;
-   }
-   if (!(cfile->f_mode & FMODE_CAN_WRITE)) {
-   NS_ERR("alloc_device: cache file not writeable\n");
-   err = -EINVAL;
-   goto err_close;
-   }
-   ns->pages_written = vzalloc(BITS_TO_LONGS(ns->geom.pgnum) *
-   sizeof(unsigned long));
-   if (!ns->pages_written) {
-   NS_ERR("alloc_device: unable to allocate pages written 
array\n");
-   err = -ENOMEM;
-   goto err_close;
-   }
-   ns->file_buf = kmalloc(ns->geom.pgszoob, GFP_KERNEL);
-   if (!ns->file_buf) {
-   NS_ERR("alloc_device: unable to allocate file buf\n");
-   err = -ENOMEM;
-   goto err_free;
-   }
-   ns->cfile = cfile;
-   return 0;
-   }
+   int i;
+   struct ns_ram_data *data = kzalloc(sizeof(*data), GFP_KERNEL);
 
-   ns->pages = vmalloc(ns->geom.pgnum * sizeof(union ns_mem));
-   if (!ns->pages) {
+   

[PATCH v2 15/46] mtd: nandsim: Introduce backend operations

2016-09-21 Thread Daniel Walter
From: Richard Weinberger 

...to untangle the ram based and cachefile based
code.
It will help us later supporting different backends.

Signed-off-by: Richard Weinberger 
---
 drivers/mtd/nand/nandsim.c | 400 ++---
 1 file changed, 228 insertions(+), 172 deletions(-)

diff --git a/drivers/mtd/nand/nandsim.c b/drivers/mtd/nand/nandsim.c
index 2e02089..633872a 100644
--- a/drivers/mtd/nand/nandsim.c
+++ b/drivers/mtd/nand/nandsim.c
@@ -314,6 +314,7 @@ struct nandsim_params {
unsigned int bbt;
unsigned int bch;
unsigned char *id_bytes;
+   struct ns_backend_ops *bops;
 };
 
 struct nandsim_debug_info {
@@ -329,6 +330,22 @@ union ns_mem {
uint16_t *word;  /* for 16-bit word access */
 };
 
+struct ns_ram_data {
+   /* The simulated NAND flash pages array */
+   union ns_mem *pages;
+
+   /* Slab allocator for nand pages */
+   struct kmem_cache *nand_pages_slab;
+};
+
+struct ns_cachefile_data {
+   struct file *cfile; /* Open file */
+   unsigned long *pages_written; /* Which pages have been written */
+   void *file_buf;
+   struct page *held_pages[NS_MAX_HELD_PAGES];
+   int held_cnt;
+};
+
 /*
  * The structure which describes all the internal simulator data.
  */
@@ -348,12 +365,6 @@ struct nandsim {
uint16_t npstates;  /* number of previous states saved */
uint16_t stateidx;  /* current state index */
 
-   /* The simulated NAND flash pages array */
-   union ns_mem *pages;
-
-   /* Slab allocator for nand pages */
-   struct kmem_cache *nand_pages_slab;
-
/* Internal buffer of page + OOB size bytes */
union ns_mem buf;
 
@@ -394,12 +405,8 @@ struct nandsim {
 int wp;  /* write Protect */
 } lines;
 
-   /* Fields needed when using a cache file */
-   struct file *cfile; /* Open file */
-   unsigned long *pages_written; /* Which pages have been written */
-   void *file_buf;
-   struct page *held_pages[NS_MAX_HELD_PAGES];
-   int held_cnt;
+   struct ns_backend_ops *bops;
+   void *backend_data;
 
struct list_head weak_blocks;
struct list_head weak_pages;
@@ -420,6 +427,17 @@ struct nandsim {
struct nandsim_debug_info dbg;
 };
 
+struct ns_backend_ops {
+   void (*erase_sector)(struct nandsim *ns);
+   int (*prog_page)(struct nandsim *ns, int num);
+   void (*read_page)(struct nandsim *ns, int num);
+   int (*init)(struct nandsim *ns, struct nandsim_params *nsparam);
+   void (*destroy)(struct nandsim *ns);
+};
+
+static struct ns_backend_ops ns_ram_bops;
+static struct ns_backend_ops ns_cachefile_bops;
+
 /*
  * Operations array. To perform any operation the simulator must pass
  * through the correspondent states chain.
@@ -641,95 +659,105 @@ static void nandsim_debugfs_remove(struct nandsim *ns)
debugfs_remove_recursive(ns->dbg.dfs_root);
 }
 
-/*
- * Allocate array of page pointers, create slab allocation for an array
- * and initialize the array by NULL pointers.
- *
- * RETURNS: 0 if success, -ENOMEM if memory alloc fails.
- */
-static int alloc_device(struct nandsim *ns, struct nandsim_params *nsparam)
+static int ns_ram_init(struct nandsim *ns, struct nandsim_params *nsparam)
 {
-   struct file *cfile;
-   int i, err;
-
-   if (nsparam->cache_file) {
-   cfile = filp_open(nsparam->cache_file, O_CREAT | O_RDWR | 
O_LARGEFILE, 0600);
-   if (IS_ERR(cfile))
-   return PTR_ERR(cfile);
-   if (!(cfile->f_mode & FMODE_CAN_READ)) {
-   NS_ERR("alloc_device: cache file not readable\n");
-   err = -EINVAL;
-   goto err_close;
-   }
-   if (!(cfile->f_mode & FMODE_CAN_WRITE)) {
-   NS_ERR("alloc_device: cache file not writeable\n");
-   err = -EINVAL;
-   goto err_close;
-   }
-   ns->pages_written = vzalloc(BITS_TO_LONGS(ns->geom.pgnum) *
-   sizeof(unsigned long));
-   if (!ns->pages_written) {
-   NS_ERR("alloc_device: unable to allocate pages written 
array\n");
-   err = -ENOMEM;
-   goto err_close;
-   }
-   ns->file_buf = kmalloc(ns->geom.pgszoob, GFP_KERNEL);
-   if (!ns->file_buf) {
-   NS_ERR("alloc_device: unable to allocate file buf\n");
-   err = -ENOMEM;
-   goto err_free;
-   }
-   ns->cfile = cfile;
-   return 0;
-   }
+   int i;
+   struct ns_ram_data *data = kzalloc(sizeof(*data), GFP_KERNEL);
 
-   ns->pages = vmalloc(ns->geom.pgnum * sizeof(union ns_mem));
-   if (!ns->pages) {
+   if (!data)
+