Flash Translation Layer  1.0
device.h
Go to the documentation of this file.
1 
8 #ifndef DEVICE_H
9 #define DEVICE_H
10 
11 #include <glib.h>
12 #include <pthread.h>
13 #include <stdint.h>
14 #include <stdlib.h>
15 
16 #include "include/log.h"
18 #define PADDR_EMPTY ((uint32_t)UINT32_MAX)
19 
20 struct device_request;
21 struct device_operations;
22 
23 #define DEVICE_PAGE_SIZE (8192)
24 
29 };
30 
34 enum { DEVICE_WRITE = 0 ,
37 };
38 
42 enum { RAMDISK_MODULE = 0 ,
45 };
46 
55 #ifndef DEVICE_NR_BUS_BITS
56 #define DEVICE_NR_BUS_BITS (3)
57 #endif
58 
59 #ifndef DEVICE_NR_CHIPS_BITS
60 #define DEVICE_NR_CHIPS_BITS (3)
61 #endif
62 
63 #ifndef DEVICE_NR_PAGES_BITS
64 #define DEVICE_NR_PAGES_BITS (7)
65 #endif
66 
67 #ifndef DEVICE_NR_BLOCKS_BITS
68 #define DEVICE_NR_BLOCKS_BITS (19)
69 #endif
70 
71 #if 0
72 #pragma message("DEVICE_NR_BUS_BITS = " TOSTRING(DEVICE_NR_BUS_BITS))
73 #pragma message("DEVICE_NR_CHIPS_BITS = " TOSTRING(DEVICE_NR_CHIPS_BITS))
74 #pragma message("DEVICE_NR_PAGES_BITS = " TOSTRING(DEVICE_NR_PAGES_BITS))
75 #pragma message("DEVICE_NR_BLOCKS_BITS = " TOSTRING(DEVICE_NR_BLOCKS_BITS))
76 #endif
77 
87 typedef void (*device_end_req_fn)(struct device_request *);
88 
96  union {
97  struct {
98  uint32_t bus : DEVICE_NR_BUS_BITS;
102  } format;
103  uint32_t lpn;
104  };
105 };
106 
111  unsigned int flag;
113  size_t data_len;
114  size_t sector;
115  struct device_address paddr;
117  void *data;
120  gint is_finish;
121 
122  pthread_mutex_t mutex;
123  pthread_cond_t cond;
124 
125  void *rq_private;
126 };
127 
131 struct device_page {
132  size_t size;
133 };
134 
138 struct device_block {
140  size_t nr_pages;
141 };
142 
148  size_t nr_blocks;
149 };
150 
154 struct device_info {
155  struct device_package package;
156  size_t nr_bus;
157  size_t nr_chips;
158 };
159 
163 struct device {
164  pthread_mutex_t mutex;
165  const struct device_operations *d_op;
166  struct device_info info;
167  uint64_t *badseg_bitmap;
168  void *d_private;
169  int (*d_submodule_exit)(struct device *);
170 };
171 
176  int (*open)(struct device *, const char *name, int flags);
177  ssize_t (*write)(struct device *, struct device_request *);
178  ssize_t (*read)(struct device *, struct device_request *);
179  int (*erase)(struct device *, struct device_request *);
180  int (*close)(struct device *);
181 };
182 
183 struct device_request *device_alloc_request(uint64_t flags);
184 void device_free_request(struct device_request *);
185 
186 int device_module_init(const uint64_t modnum, struct device **, uint64_t flags);
187 int device_module_exit(struct device *);
188 
196 static inline size_t device_get_nr_segments(struct device *dev)
197 {
198  struct device_info *info = &dev->info;
199  struct device_package *package = &info->package;
200  return package->nr_blocks;
201 }
202 
203 static inline size_t device_get_blocks_per_segment(struct device *dev)
204 {
205  struct device_info *info = &dev->info;
206  return (info->nr_bus * info->nr_chips);
207 }
208 
216 static inline size_t device_get_pages_per_segment(struct device *dev)
217 {
218  struct device_info *info = &dev->info;
219  struct device_package *package = &info->package;
220  struct device_block *block = &package->block;
221 
222  return device_get_blocks_per_segment(dev) * block->nr_pages;
223 }
224 
232 static inline size_t device_get_page_size(struct device *dev)
233 {
234  struct device_info *info = &dev->info;
235  struct device_package *package = &info->package;
236  struct device_block *block = &package->block;
237  struct device_page *page = &block->page;
238  return page->size;
239 }
240 
248 static inline size_t device_get_total_size(struct device *dev)
249 {
250  size_t nr_segments = device_get_nr_segments(dev);
251  size_t nr_pages_per_segment = device_get_pages_per_segment(dev);
252  size_t page_size = device_get_page_size(dev);
253 
254  return nr_segments * nr_pages_per_segment * page_size;
255 }
256 
264 static inline size_t device_get_total_pages(struct device *dev)
265 {
266  return device_get_total_size(dev) / device_get_page_size(dev);
267 }
268 
269 #endif
flash board&#39;s page information
Definition: device.h:131
void * d_private
Definition: device.h:168
struct device_address::@3::@5 format
static size_t device_get_blocks_per_segment(struct device *dev)
Definition: device.h:203
Definition: device.h:44
device_end_req_fn end_rq
Definition: device.h:118
size_t sector
Definition: device.h:114
Definition: device.h:34
Definition: device.h:43
generic device address format
Definition: device.h:95
Definition: device.h:35
uint32_t page
Definition: device.h:100
int device_module_init(const uint64_t modnum, struct device **, uint64_t flags)
initialize the device module
Definition: device.c:104
struct device_page page
Definition: device.h:139
#define DEVICE_NR_CHIPS_BITS
Definition: device.h:60
size_t nr_chips
Definition: device.h:157
metadata of the device
Definition: device.h:163
uint32_t block
Definition: device.h:101
pthread_cond_t cond
Definition: device.h:123
flash board&#39;s block information
Definition: device.h:138
Definition: device.h:28
struct device_request * device_alloc_request(uint64_t flags)
dynamic allocate the device request
Definition: device.c:51
flash board&#39;s package(nand chip) information
Definition: device.h:146
size_t size
Definition: device.h:132
static size_t device_get_total_pages(struct device *dev)
get total the number of pages in a flash board
Definition: device.h:264
#define DEVICE_NR_BUS_BITS
device address information
Definition: device.h:56
#define DEVICE_NR_PAGES_BITS
Definition: device.h:64
size_t nr_pages
Definition: device.h:140
uint32_t bus
Definition: device.h:98
flash board&#39;s architecture information
Definition: device.h:154
uint32_t lpn
Definition: device.h:103
static size_t device_get_nr_segments(struct device *dev)
get the number of segments in a flash board
Definition: device.h:196
pthread_mutex_t mutex
Definition: device.h:122
operations for device
Definition: device.h:175
uint64_t * badseg_bitmap
Definition: device.h:167
const struct device_operations * d_op
Definition: device.h:165
size_t nr_bus
Definition: device.h:156
unsigned int flag
Definition: device.h:111
void device_free_request(struct device_request *)
free pre-allocated device_request resource
Definition: device.c:88
gint is_finish
Definition: device.h:120
size_t data_len
Definition: device.h:113
static size_t device_get_total_size(struct device *dev)
total size of a flash board
Definition: device.h:248
void * data
Definition: device.h:117
uint32_t chip
Definition: device.h:99
int device_module_exit(struct device *)
deallocate the device module
Definition: device.c:140
struct device * dev
Definition: ramdisk-test.c:10
void * rq_private
Definition: device.h:125
void(* device_end_req_fn)(struct device_request *)
I/O end request function.
Definition: device.h:87
size_t nr_blocks
Definition: device.h:148
struct device_info info
Definition: device.h:166
Definition: device.h:36
static size_t device_get_pages_per_segment(struct device *dev)
get the number of pages in a segment
Definition: device.h:216
Definition: device.h:42
static size_t device_get_page_size(struct device *dev)
get flash board&#39;s NAND page size
Definition: device.h:232
#define DEVICE_NR_BLOCKS_BITS
Definition: device.h:68
pthread_mutex_t mutex
Definition: device.h:164
request for device
Definition: device.h:110