16 #include <linux/blkdev.h>
17 #include <linux/types.h>
18 #include <linux/file.h>
19 #include <linux/dmapool.h>
22 /* HW Responsibilities */
26 /* Physical Adressing Mode */
27 NVM_ADDRMODE_LINEAR = 0,
28 NVM_ADDRMODE_CHANNEL = 1,
30 /* Plane programming mode for LUN */
36 NVM_RSP_SUCCESS = 0x0,
37 NVM_RSP_NOT_CHANGEABLE = 0x1,
38 NVM_RSP_ERR_FAILWRITE = 0x40ff,
39 NVM_RSP_ERR_EMPTYPAGE = 0x42ff,
43 NVM_OP_HBWRITE = 0x81,
48 /* PPA Command Flags */
49 NVM_IO_SNGL_ACCESS = 0x0,
50 NVM_IO_DUAL_ACCESS = 0x1,
51 NVM_IO_QUAD_ACCESS = 0x2,
53 NVM_IO_SUSPEND = 0x80,
54 NVM_IO_SLC_MODE = 0x100,
55 NVM_IO_SCRAMBLE_DISABLE = 0x200,
80 struct nvm_addr_format {
101 struct nvm_addr_format ppaf;
102 struct nvm_id_group groups[4];
106 struct list_head list;
107 struct nvm_tgt_type *type;
108 struct gendisk *disk;
111 struct nvm_tgt_instance {
112 struct nvm_tgt_type *tt;
115 #define ADDR_EMPTY (~0ULL)
117 #define NVM_VERSION_MAJOR 1
118 #define NVM_VERSION_MINOR 0
119 #define NVM_VERSION_PATCH 0
121 #define NVM_BLK_BITS (16)
122 #define NVM_PG_BITS (16)
123 #define NVM_SEC_BITS (8)
124 #define NVM_PL_BITS (8)
125 #define NVM_LUN_BITS (8)
126 #define NVM_CH_BITS (8)
129 /* Generic structure for all addresses */
132 u64 blk : NVM_BLK_BITS;
133 u64 pg : NVM_PG_BITS;
134 u64 sec : NVM_SEC_BITS;
135 u64 pl : NVM_PL_BITS;
136 u64 lun : NVM_LUN_BITS;
137 u64 ch : NVM_CH_BITS;
145 struct nvm_tgt_instance *ins;
151 struct ppa_addr ppa_addr;
152 dma_addr_t dma_ppa_list;
155 struct ppa_addr *ppa_list;
158 dma_addr_t dma_metadata;
165 static inline struct nvm_rq *nvm_rq_from_pdu(void *pdu)
167 return pdu - sizeof(struct nvm_rq);
170 static inline void *nvm_rq_to_pdu(struct nvm_rq *rqdata)
177 typedef int (nvm_l2p_update_fn)(u64, u32, __le64 *, void *);
178 typedef int (nvm_bb_update_fn)(struct ppa_addr, int, u8 *, void *);
179 typedef int (nvm_id_fn)(struct request_queue *, struct nvm_id *);
180 typedef int (nvm_get_l2p_tbl_fn)(struct request_queue *, u64, u32,
181 nvm_l2p_update_fn *, void *);
182 typedef int (nvm_op_bb_tbl_fn)(struct nvm_dev *, struct ppa_addr, int,
183 nvm_bb_update_fn *, void *);
184 typedef int (nvm_op_set_bb_fn)(struct request_queue *, struct nvm_rq *, int);
185 typedef int (nvm_submit_io_fn)(struct request_queue *, struct nvm_rq *);
186 typedef int (nvm_erase_blk_fn)(struct request_queue *, struct nvm_rq *);
187 typedef void *(nvm_create_dma_pool_fn)(struct request_queue *, char *);
188 typedef void (nvm_destroy_dma_pool_fn)(void *);
189 typedef void *(nvm_dev_dma_alloc_fn)(struct request_queue *, void *, gfp_t,
191 typedef void (nvm_dev_dma_free_fn)(void *, void*, dma_addr_t);
195 nvm_get_l2p_tbl_fn *get_l2p_tbl;
196 nvm_op_bb_tbl_fn *get_bb_tbl;
197 nvm_op_set_bb_fn *set_bb_tbl;
199 nvm_submit_io_fn *submit_io;
200 nvm_erase_blk_fn *erase_block;
202 nvm_create_dma_pool_fn *create_dma_pool;
203 nvm_destroy_dma_pool_fn *destroy_dma_pool;
204 nvm_dev_dma_alloc_fn *dev_dma_alloc;
205 nvm_dev_dma_free_fn *dev_dma_free;
207 unsigned int max_phys_sect;
216 unsigned int nr_inuse_blocks; /* Number of used blocks */
217 unsigned int nr_free_blocks; /* Number of unused blocks */
218 unsigned int nr_bad_blocks; /* Number of bad blocks */
219 struct nvm_block *blocks;
225 struct list_head list;
234 struct nvm_dev_ops *ops;
236 struct list_head devices;
237 struct list_head online_targets;
240 struct nvmm_type *mt;
243 /* Device information */
247 int sec_per_pg; /* only sectors for a single page */
252 struct nvm_addr_format ppaf;
254 /* Calculated/Cached values. These do not reflect the actual usable
255 * blocks at run-time.
258 int plane_mode; /* drive device in single, double or quad mode */
260 int sec_per_pl; /* all sectors across planes */
264 unsigned long total_pages;
265 unsigned long total_blocks;
267 unsigned max_pages_per_blk;
271 struct nvm_id identity;
274 struct request_queue *q;
275 char name[DISK_NAME_LEN];
278 static inline struct ppa_addr generic_to_dev_addr(struct nvm_dev *dev,
283 l.ppa = ((u64)r.g.blk) << dev->ppaf.blk_offset;
284 l.ppa |= ((u64)r.g.pg) << dev->ppaf.pg_offset;
285 l.ppa |= ((u64)r.g.sec) << dev->ppaf.sect_offset;
286 l.ppa |= ((u64)r.g.pl) << dev->ppaf.pln_offset;
287 l.ppa |= ((u64)r.g.lun) << dev->ppaf.lun_offset;
288 l.ppa |= ((u64)r.g.ch) << dev->ppaf.ch_offset;
293 static inline struct ppa_addr dev_to_generic_addr(struct nvm_dev *dev,
299 * (r.ppa << X offset) & X len bitmask. X eq. blk, pg, etc.
301 l.g.blk = (r.ppa >> dev->ppaf.blk_offset) &
302 (((1 << dev->ppaf.blk_len) - 1));
303 l.g.pg |= (r.ppa >> dev->ppaf.pg_offset) &
304 (((1 << dev->ppaf.pg_len) - 1));
305 l.g.sec |= (r.ppa >> dev->ppaf.sect_offset) &
306 (((1 << dev->ppaf.sect_len) - 1));
307 l.g.pl |= (r.ppa >> dev->ppaf.pln_offset) &
308 (((1 << dev->ppaf.pln_len) - 1));
309 l.g.lun |= (r.ppa >> dev->ppaf.lun_offset) &
310 (((1 << dev->ppaf.lun_len) - 1));
311 l.g.ch |= (r.ppa >> dev->ppaf.ch_offset) &
312 (((1 << dev->ppaf.ch_len) - 1));
317 static inline int ppa_empty(struct ppa_addr ppa_addr)
319 return (ppa_addr.ppa == ADDR_EMPTY);
322 static inline void ppa_set_empty(struct ppa_addr *ppa_addr)
324 ppa_addr->ppa = ADDR_EMPTY;
327 static inline struct ppa_addr block_to_ppa(struct nvm_dev *dev,
328 struct nvm_block *blk)
331 struct nvm_lun *lun = blk->lun;
334 ppa.g.blk = blk->id % dev->blks_per_lun;
335 ppa.g.lun = lun->lun_id;
336 ppa.g.ch = lun->chnl_id;
341 typedef blk_qc_t (nvm_tgt_make_rq_fn)(struct request_queue *, struct bio *);
342 typedef sector_t (nvm_tgt_capacity_fn)(void *);
343 typedef int (nvm_tgt_end_io_fn)(struct nvm_rq *, int);
344 typedef void *(nvm_tgt_init_fn)(struct nvm_dev *, struct gendisk *, int, int);
345 typedef void (nvm_tgt_exit_fn)(void *);
347 struct nvm_tgt_type {
349 unsigned int version[3];
351 /* target entry points */
352 nvm_tgt_make_rq_fn *make_rq;
353 nvm_tgt_capacity_fn *capacity;
354 nvm_tgt_end_io_fn *end_io;
356 /* module-specific init/teardown */
357 nvm_tgt_init_fn *init;
358 nvm_tgt_exit_fn *exit;
360 /* For internal use */
361 struct list_head list;
364 extern int nvm_register_target(struct nvm_tgt_type *);
365 extern void nvm_unregister_target(struct nvm_tgt_type *);
367 extern void *nvm_dev_dma_alloc(struct nvm_dev *, gfp_t, dma_addr_t *);
368 extern void nvm_dev_dma_free(struct nvm_dev *, void *, dma_addr_t);
370 typedef int (nvmm_register_fn)(struct nvm_dev *);
371 typedef void (nvmm_unregister_fn)(struct nvm_dev *);
372 typedef struct nvm_block *(nvmm_get_blk_fn)(struct nvm_dev *,
373 struct nvm_lun *, unsigned long);
374 typedef void (nvmm_put_blk_fn)(struct nvm_dev *, struct nvm_block *);
375 typedef int (nvmm_open_blk_fn)(struct nvm_dev *, struct nvm_block *);
376 typedef int (nvmm_close_blk_fn)(struct nvm_dev *, struct nvm_block *);
377 typedef void (nvmm_flush_blk_fn)(struct nvm_dev *, struct nvm_block *);
378 typedef int (nvmm_submit_io_fn)(struct nvm_dev *, struct nvm_rq *);
379 typedef int (nvmm_end_io_fn)(struct nvm_rq *, int);
380 typedef int (nvmm_erase_blk_fn)(struct nvm_dev *, struct nvm_block *,
382 typedef struct nvm_lun *(nvmm_get_lun_fn)(struct nvm_dev *, int);
383 typedef void (nvmm_lun_info_print_fn)(struct nvm_dev *);
387 unsigned int version[3];
389 nvmm_register_fn *register_mgr;
390 nvmm_unregister_fn *unregister_mgr;
392 /* Block administration callbacks */
393 nvmm_get_blk_fn *get_blk;
394 nvmm_put_blk_fn *put_blk;
395 nvmm_open_blk_fn *open_blk;
396 nvmm_close_blk_fn *close_blk;
397 nvmm_flush_blk_fn *flush_blk;
399 nvmm_submit_io_fn *submit_io;
400 nvmm_end_io_fn *end_io;
401 nvmm_erase_blk_fn *erase_blk;
403 /* Configuration management */
404 nvmm_get_lun_fn *get_lun;
407 nvmm_lun_info_print_fn *lun_info_print;
408 struct list_head list;
411 extern int nvm_register_mgr(struct nvmm_type *);
412 extern void nvm_unregister_mgr(struct nvmm_type *);
414 extern struct nvm_block *nvm_get_blk(struct nvm_dev *, struct nvm_lun *,
416 extern void nvm_put_blk(struct nvm_dev *, struct nvm_block *);
418 extern int nvm_register(struct request_queue *, char *,
419 struct nvm_dev_ops *);
420 extern void nvm_unregister(char *);
422 extern int nvm_submit_io(struct nvm_dev *, struct nvm_rq *);
423 extern int nvm_erase_blk(struct nvm_dev *, struct nvm_block *);
424 #else /* CONFIG_NVM */
427 static inline int nvm_register(struct request_queue *q, char *disk_name,
428 struct nvm_dev_ops *ops)
432 static inline void nvm_unregister(char *disk_name) {}
433 #endif /* CONFIG_NVM */
434 #endif /* LIGHTNVM.H */