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 /* NAND Access Modes */
54 NVM_IO_SUSPEND = 0x80,
55 NVM_IO_SLC_MODE = 0x100,
56 NVM_IO_SCRAMBLE_DISABLE = 0x200,
61 NVM_BLK_T_GRWN_BAD = 0x2,
88 struct nvm_addr_format {
109 struct nvm_addr_format ppaf;
110 struct nvm_id_group groups[4];
114 struct list_head list;
115 struct nvm_tgt_type *type;
116 struct gendisk *disk;
119 struct nvm_tgt_instance {
120 struct nvm_tgt_type *tt;
123 #define ADDR_EMPTY (~0ULL)
125 #define NVM_VERSION_MAJOR 1
126 #define NVM_VERSION_MINOR 0
127 #define NVM_VERSION_PATCH 0
129 #define NVM_BLK_BITS (16)
130 #define NVM_PG_BITS (16)
131 #define NVM_SEC_BITS (8)
132 #define NVM_PL_BITS (8)
133 #define NVM_LUN_BITS (8)
134 #define NVM_CH_BITS (8)
137 /* Generic structure for all addresses */
140 u64 blk : NVM_BLK_BITS;
141 u64 pg : NVM_PG_BITS;
142 u64 sec : NVM_SEC_BITS;
143 u64 pl : NVM_PL_BITS;
144 u64 lun : NVM_LUN_BITS;
145 u64 ch : NVM_CH_BITS;
153 typedef void (nvm_end_io_fn)(struct nvm_rq *);
156 struct nvm_tgt_instance *ins;
162 struct ppa_addr ppa_addr;
163 dma_addr_t dma_ppa_list;
166 struct ppa_addr *ppa_list;
169 dma_addr_t dma_metadata;
171 struct completion *wait;
172 nvm_end_io_fn *end_io;
181 static inline struct nvm_rq *nvm_rq_from_pdu(void *pdu)
183 return pdu - sizeof(struct nvm_rq);
186 static inline void *nvm_rq_to_pdu(struct nvm_rq *rqdata)
193 typedef int (nvm_l2p_update_fn)(u64, u32, __le64 *, void *);
194 typedef int (nvm_bb_update_fn)(struct ppa_addr, int, u8 *, void *);
195 typedef int (nvm_id_fn)(struct nvm_dev *, struct nvm_id *);
196 typedef int (nvm_get_l2p_tbl_fn)(struct nvm_dev *, u64, u32,
197 nvm_l2p_update_fn *, void *);
198 typedef int (nvm_op_bb_tbl_fn)(struct nvm_dev *, struct ppa_addr, int,
199 nvm_bb_update_fn *, void *);
200 typedef int (nvm_op_set_bb_fn)(struct nvm_dev *, struct nvm_rq *, int);
201 typedef int (nvm_submit_io_fn)(struct nvm_dev *, struct nvm_rq *);
202 typedef int (nvm_erase_blk_fn)(struct nvm_dev *, struct nvm_rq *);
203 typedef void *(nvm_create_dma_pool_fn)(struct nvm_dev *, char *);
204 typedef void (nvm_destroy_dma_pool_fn)(void *);
205 typedef void *(nvm_dev_dma_alloc_fn)(struct nvm_dev *, void *, gfp_t,
207 typedef void (nvm_dev_dma_free_fn)(void *, void*, dma_addr_t);
211 nvm_get_l2p_tbl_fn *get_l2p_tbl;
212 nvm_op_bb_tbl_fn *get_bb_tbl;
213 nvm_op_set_bb_fn *set_bb_tbl;
215 nvm_submit_io_fn *submit_io;
216 nvm_erase_blk_fn *erase_block;
218 nvm_create_dma_pool_fn *create_dma_pool;
219 nvm_destroy_dma_pool_fn *destroy_dma_pool;
220 nvm_dev_dma_alloc_fn *dev_dma_alloc;
221 nvm_dev_dma_free_fn *dev_dma_free;
223 unsigned int max_phys_sect;
232 /* It is up to the target to mark blocks as closed. If the target does
233 * not do it, all blocks are marked as open, and nr_open_blocks
234 * represents the number of blocks in use
236 unsigned int nr_open_blocks; /* Number of used, writable blocks */
237 unsigned int nr_closed_blocks; /* Number of used, read-only blocks */
238 unsigned int nr_free_blocks; /* Number of unused blocks */
239 unsigned int nr_bad_blocks; /* Number of bad blocks */
243 struct nvm_block *blocks;
247 NVM_BLK_ST_FREE = 0x1, /* Free block */
248 NVM_BLK_ST_OPEN = 0x2, /* Open block - read-write */
249 NVM_BLK_ST_CLOSED = 0x4, /* Closed block - read-only */
250 NVM_BLK_ST_BAD = 0x8, /* Bad block */
254 struct list_head list;
263 struct nvm_dev_ops *ops;
265 struct list_head devices;
266 struct list_head online_targets;
269 struct nvmm_type *mt;
272 /* Device information */
276 int sec_per_pg; /* only sectors for a single page */
281 struct nvm_addr_format ppaf;
283 /* Calculated/Cached values. These do not reflect the actual usable
284 * blocks at run-time.
287 int plane_mode; /* drive device in single, double or quad mode */
289 int sec_per_pl; /* all sectors across planes */
293 unsigned long total_pages;
294 unsigned long total_blocks;
296 unsigned max_pages_per_blk;
300 struct nvm_id identity;
303 struct request_queue *q;
304 char name[DISK_NAME_LEN];
307 static inline struct ppa_addr generic_to_dev_addr(struct nvm_dev *dev,
312 l.ppa = ((u64)r.g.blk) << dev->ppaf.blk_offset;
313 l.ppa |= ((u64)r.g.pg) << dev->ppaf.pg_offset;
314 l.ppa |= ((u64)r.g.sec) << dev->ppaf.sect_offset;
315 l.ppa |= ((u64)r.g.pl) << dev->ppaf.pln_offset;
316 l.ppa |= ((u64)r.g.lun) << dev->ppaf.lun_offset;
317 l.ppa |= ((u64)r.g.ch) << dev->ppaf.ch_offset;
322 static inline struct ppa_addr dev_to_generic_addr(struct nvm_dev *dev,
328 * (r.ppa << X offset) & X len bitmask. X eq. blk, pg, etc.
330 l.g.blk = (r.ppa >> dev->ppaf.blk_offset) &
331 (((1 << dev->ppaf.blk_len) - 1));
332 l.g.pg |= (r.ppa >> dev->ppaf.pg_offset) &
333 (((1 << dev->ppaf.pg_len) - 1));
334 l.g.sec |= (r.ppa >> dev->ppaf.sect_offset) &
335 (((1 << dev->ppaf.sect_len) - 1));
336 l.g.pl |= (r.ppa >> dev->ppaf.pln_offset) &
337 (((1 << dev->ppaf.pln_len) - 1));
338 l.g.lun |= (r.ppa >> dev->ppaf.lun_offset) &
339 (((1 << dev->ppaf.lun_len) - 1));
340 l.g.ch |= (r.ppa >> dev->ppaf.ch_offset) &
341 (((1 << dev->ppaf.ch_len) - 1));
346 static inline int ppa_empty(struct ppa_addr ppa_addr)
348 return (ppa_addr.ppa == ADDR_EMPTY);
351 static inline void ppa_set_empty(struct ppa_addr *ppa_addr)
353 ppa_addr->ppa = ADDR_EMPTY;
356 static inline struct ppa_addr block_to_ppa(struct nvm_dev *dev,
357 struct nvm_block *blk)
360 struct nvm_lun *lun = blk->lun;
363 ppa.g.blk = blk->id % dev->blks_per_lun;
364 ppa.g.lun = lun->lun_id;
365 ppa.g.ch = lun->chnl_id;
370 typedef blk_qc_t (nvm_tgt_make_rq_fn)(struct request_queue *, struct bio *);
371 typedef sector_t (nvm_tgt_capacity_fn)(void *);
372 typedef void *(nvm_tgt_init_fn)(struct nvm_dev *, struct gendisk *, int, int);
373 typedef void (nvm_tgt_exit_fn)(void *);
375 struct nvm_tgt_type {
377 unsigned int version[3];
379 /* target entry points */
380 nvm_tgt_make_rq_fn *make_rq;
381 nvm_tgt_capacity_fn *capacity;
382 nvm_end_io_fn *end_io;
384 /* module-specific init/teardown */
385 nvm_tgt_init_fn *init;
386 nvm_tgt_exit_fn *exit;
388 /* For internal use */
389 struct list_head list;
392 extern int nvm_register_target(struct nvm_tgt_type *);
393 extern void nvm_unregister_target(struct nvm_tgt_type *);
395 extern void *nvm_dev_dma_alloc(struct nvm_dev *, gfp_t, dma_addr_t *);
396 extern void nvm_dev_dma_free(struct nvm_dev *, void *, dma_addr_t);
398 typedef int (nvmm_register_fn)(struct nvm_dev *);
399 typedef void (nvmm_unregister_fn)(struct nvm_dev *);
400 typedef struct nvm_block *(nvmm_get_blk_fn)(struct nvm_dev *,
401 struct nvm_lun *, unsigned long);
402 typedef void (nvmm_put_blk_fn)(struct nvm_dev *, struct nvm_block *);
403 typedef int (nvmm_open_blk_fn)(struct nvm_dev *, struct nvm_block *);
404 typedef int (nvmm_close_blk_fn)(struct nvm_dev *, struct nvm_block *);
405 typedef void (nvmm_flush_blk_fn)(struct nvm_dev *, struct nvm_block *);
406 typedef int (nvmm_submit_io_fn)(struct nvm_dev *, struct nvm_rq *);
407 typedef int (nvmm_erase_blk_fn)(struct nvm_dev *, struct nvm_block *,
409 typedef struct nvm_lun *(nvmm_get_lun_fn)(struct nvm_dev *, int);
410 typedef void (nvmm_lun_info_print_fn)(struct nvm_dev *);
414 unsigned int version[3];
416 nvmm_register_fn *register_mgr;
417 nvmm_unregister_fn *unregister_mgr;
419 /* Block administration callbacks */
420 nvmm_get_blk_fn *get_blk_unlocked;
421 nvmm_put_blk_fn *put_blk_unlocked;
422 nvmm_get_blk_fn *get_blk;
423 nvmm_put_blk_fn *put_blk;
424 nvmm_open_blk_fn *open_blk;
425 nvmm_close_blk_fn *close_blk;
426 nvmm_flush_blk_fn *flush_blk;
428 nvmm_submit_io_fn *submit_io;
429 nvmm_erase_blk_fn *erase_blk;
431 /* Configuration management */
432 nvmm_get_lun_fn *get_lun;
435 nvmm_lun_info_print_fn *lun_info_print;
436 struct list_head list;
439 extern int nvm_register_mgr(struct nvmm_type *);
440 extern void nvm_unregister_mgr(struct nvmm_type *);
442 extern struct nvm_block *nvm_get_blk_unlocked(struct nvm_dev *,
443 struct nvm_lun *, unsigned long);
444 extern void nvm_put_blk_unlocked(struct nvm_dev *, struct nvm_block *);
446 extern struct nvm_block *nvm_get_blk(struct nvm_dev *, struct nvm_lun *,
448 extern void nvm_put_blk(struct nvm_dev *, struct nvm_block *);
450 extern int nvm_register(struct request_queue *, char *,
451 struct nvm_dev_ops *);
452 extern void nvm_unregister(char *);
454 extern int nvm_submit_io(struct nvm_dev *, struct nvm_rq *);
455 extern void nvm_generic_to_addr_mode(struct nvm_dev *, struct nvm_rq *);
456 extern void nvm_addr_to_generic_mode(struct nvm_dev *, struct nvm_rq *);
457 extern int nvm_set_rqd_ppalist(struct nvm_dev *, struct nvm_rq *,
458 struct ppa_addr *, int);
459 extern void nvm_free_rqd_ppalist(struct nvm_dev *, struct nvm_rq *);
460 extern int nvm_erase_ppa(struct nvm_dev *, struct ppa_addr *, int);
461 extern int nvm_erase_blk(struct nvm_dev *, struct nvm_block *);
462 extern void nvm_end_io(struct nvm_rq *, int);
463 extern int nvm_submit_ppa(struct nvm_dev *, struct ppa_addr *, int, int, int,
465 #else /* CONFIG_NVM */
468 static inline int nvm_register(struct request_queue *q, char *disk_name,
469 struct nvm_dev_ops *ops)
473 static inline void nvm_unregister(char *disk_name) {}
474 #endif /* CONFIG_NVM */
475 #endif /* LIGHTNVM.H */