4 #include <linux/types.h>
16 #define NVM_BLK_BITS (16)
17 #define NVM_PG_BITS (16)
18 #define NVM_SEC_BITS (8)
19 #define NVM_PL_BITS (8)
20 #define NVM_LUN_BITS (8)
21 #define NVM_CH_BITS (8)
24 /* Generic structure for all addresses */
27 u64 blk : NVM_BLK_BITS;
29 u64 sec : NVM_SEC_BITS;
31 u64 lun : NVM_LUN_BITS;
43 typedef int (nvm_l2p_update_fn)(u64, u32, __le64 *, void *);
44 typedef int (nvm_id_fn)(struct nvm_dev *, struct nvm_id *);
45 typedef int (nvm_get_l2p_tbl_fn)(struct nvm_dev *, u64, u32,
46 nvm_l2p_update_fn *, void *);
47 typedef int (nvm_op_bb_tbl_fn)(struct nvm_dev *, struct ppa_addr, u8 *);
48 typedef int (nvm_op_set_bb_fn)(struct nvm_dev *, struct ppa_addr *, int, int);
49 typedef int (nvm_submit_io_fn)(struct nvm_dev *, struct nvm_rq *);
50 typedef int (nvm_erase_blk_fn)(struct nvm_dev *, struct nvm_rq *);
51 typedef void *(nvm_create_dma_pool_fn)(struct nvm_dev *, char *);
52 typedef void (nvm_destroy_dma_pool_fn)(void *);
53 typedef void *(nvm_dev_dma_alloc_fn)(struct nvm_dev *, void *, gfp_t,
55 typedef void (nvm_dev_dma_free_fn)(void *, void*, dma_addr_t);
59 nvm_get_l2p_tbl_fn *get_l2p_tbl;
60 nvm_op_bb_tbl_fn *get_bb_tbl;
61 nvm_op_set_bb_fn *set_bb_tbl;
63 nvm_submit_io_fn *submit_io;
64 nvm_erase_blk_fn *erase_block;
66 nvm_create_dma_pool_fn *create_dma_pool;
67 nvm_destroy_dma_pool_fn *destroy_dma_pool;
68 nvm_dev_dma_alloc_fn *dev_dma_alloc;
69 nvm_dev_dma_free_fn *dev_dma_free;
71 unsigned int max_phys_sect;
78 #include <linux/blkdev.h>
79 #include <linux/file.h>
80 #include <linux/dmapool.h>
81 #include <uapi/linux/lightnvm.h>
84 /* HW Responsibilities */
88 /* Physical Adressing Mode */
89 NVM_ADDRMODE_LINEAR = 0,
90 NVM_ADDRMODE_CHANNEL = 1,
92 /* Plane programming mode for LUN */
98 NVM_RSP_SUCCESS = 0x0,
99 NVM_RSP_NOT_CHANGEABLE = 0x1,
100 NVM_RSP_ERR_FAILWRITE = 0x40ff,
101 NVM_RSP_ERR_EMPTYPAGE = 0x42ff,
104 NVM_OP_HBREAD = 0x02,
105 NVM_OP_HBWRITE = 0x81,
106 NVM_OP_PWRITE = 0x91,
110 /* PPA Command Flags */
111 NVM_IO_SNGL_ACCESS = 0x0,
112 NVM_IO_DUAL_ACCESS = 0x1,
113 NVM_IO_QUAD_ACCESS = 0x2,
115 /* NAND Access Modes */
116 NVM_IO_SUSPEND = 0x80,
117 NVM_IO_SLC_MODE = 0x100,
118 NVM_IO_SCRAMBLE_DISABLE = 0x200,
121 NVM_BLK_T_FREE = 0x0,
123 NVM_BLK_T_GRWN_BAD = 0x2,
125 NVM_BLK_T_HOST = 0x8,
127 /* Memory capabilities */
128 NVM_ID_CAP_SLC = 0x1,
129 NVM_ID_CAP_CMD_SUSPEND = 0x2,
130 NVM_ID_CAP_SCRAMBLE = 0x4,
131 NVM_ID_CAP_ENCRYPT = 0x8,
134 NVM_ID_FMTYPE_SLC = 0,
135 NVM_ID_FMTYPE_MLC = 1,
137 /* Device capabilities */
138 NVM_ID_DCAP_BBLKMGMT = 0x1,
139 NVM_UD_DCAP_ECC = 0x2,
142 struct nvm_id_lp_mlc {
147 struct nvm_id_lp_tbl {
149 struct nvm_id_lp_mlc mlc;
152 struct nvm_id_group {
173 struct nvm_id_lp_tbl lptbl;
176 struct nvm_addr_format {
197 struct nvm_addr_format ppaf;
198 struct nvm_id_group groups[4];
202 struct list_head list;
203 struct nvm_tgt_type *type;
204 struct gendisk *disk;
207 struct nvm_tgt_instance {
208 struct nvm_tgt_type *tt;
211 #define ADDR_EMPTY (~0ULL)
213 #define NVM_VERSION_MAJOR 1
214 #define NVM_VERSION_MINOR 0
215 #define NVM_VERSION_PATCH 0
218 typedef void (nvm_end_io_fn)(struct nvm_rq *);
221 struct nvm_tgt_instance *ins;
227 struct ppa_addr ppa_addr;
228 dma_addr_t dma_ppa_list;
231 struct ppa_addr *ppa_list;
234 dma_addr_t dma_meta_list;
236 struct completion *wait;
237 nvm_end_io_fn *end_io;
243 u64 ppa_status; /* ppa media status */
247 static inline struct nvm_rq *nvm_rq_from_pdu(void *pdu)
249 return pdu - sizeof(struct nvm_rq);
252 static inline void *nvm_rq_to_pdu(struct nvm_rq *rqdata)
265 /* It is up to the target to mark blocks as closed. If the target does
266 * not do it, all blocks are marked as open, and nr_open_blocks
267 * represents the number of blocks in use
269 unsigned int nr_open_blocks; /* Number of used, writable blocks */
270 unsigned int nr_closed_blocks; /* Number of used, read-only blocks */
271 unsigned int nr_free_blocks; /* Number of unused blocks */
272 unsigned int nr_bad_blocks; /* Number of bad blocks */
276 struct nvm_block *blocks;
280 NVM_BLK_ST_FREE = 0x1, /* Free block */
281 NVM_BLK_ST_OPEN = 0x2, /* Open block - read-write */
282 NVM_BLK_ST_CLOSED = 0x4, /* Closed block - read-only */
283 NVM_BLK_ST_BAD = 0x8, /* Bad block */
287 struct list_head list;
295 /* system block cpu representation */
298 unsigned long erase_cnt;
299 unsigned int version;
300 char mmtype[NVM_MMTYPE_LEN];
301 struct ppa_addr fs_ppa;
305 struct nvm_dev_ops *ops;
307 struct list_head devices;
310 struct nvmm_type *mt;
314 struct nvm_sb_info sb;
316 /* Device information */
320 int sec_per_pg; /* only sectors for a single page */
324 int pfpg_size; /* size of buffer if all pages are to be read */
328 struct nvm_addr_format ppaf;
330 /* Calculated/Cached values. These do not reflect the actual usable
331 * blocks at run-time.
334 int plane_mode; /* drive device in single, double or quad mode */
336 int sec_per_pl; /* all sectors across planes */
340 /* lower page table */
344 unsigned long total_blocks;
345 unsigned long total_secs;
347 unsigned max_pages_per_blk;
349 unsigned long *lun_map;
352 struct nvm_id identity;
355 struct request_queue *q;
356 char name[DISK_NAME_LEN];
362 static inline struct ppa_addr generic_to_dev_addr(struct nvm_dev *dev,
367 l.ppa = ((u64)r.g.blk) << dev->ppaf.blk_offset;
368 l.ppa |= ((u64)r.g.pg) << dev->ppaf.pg_offset;
369 l.ppa |= ((u64)r.g.sec) << dev->ppaf.sect_offset;
370 l.ppa |= ((u64)r.g.pl) << dev->ppaf.pln_offset;
371 l.ppa |= ((u64)r.g.lun) << dev->ppaf.lun_offset;
372 l.ppa |= ((u64)r.g.ch) << dev->ppaf.ch_offset;
377 static inline struct ppa_addr dev_to_generic_addr(struct nvm_dev *dev,
383 * (r.ppa << X offset) & X len bitmask. X eq. blk, pg, etc.
385 l.g.blk = (r.ppa >> dev->ppaf.blk_offset) &
386 (((1 << dev->ppaf.blk_len) - 1));
387 l.g.pg |= (r.ppa >> dev->ppaf.pg_offset) &
388 (((1 << dev->ppaf.pg_len) - 1));
389 l.g.sec |= (r.ppa >> dev->ppaf.sect_offset) &
390 (((1 << dev->ppaf.sect_len) - 1));
391 l.g.pl |= (r.ppa >> dev->ppaf.pln_offset) &
392 (((1 << dev->ppaf.pln_len) - 1));
393 l.g.lun |= (r.ppa >> dev->ppaf.lun_offset) &
394 (((1 << dev->ppaf.lun_len) - 1));
395 l.g.ch |= (r.ppa >> dev->ppaf.ch_offset) &
396 (((1 << dev->ppaf.ch_len) - 1));
401 static inline int ppa_empty(struct ppa_addr ppa_addr)
403 return (ppa_addr.ppa == ADDR_EMPTY);
406 static inline void ppa_set_empty(struct ppa_addr *ppa_addr)
408 ppa_addr->ppa = ADDR_EMPTY;
411 static inline struct ppa_addr block_to_ppa(struct nvm_dev *dev,
412 struct nvm_block *blk)
415 struct nvm_lun *lun = blk->lun;
418 ppa.g.blk = blk->id % dev->blks_per_lun;
419 ppa.g.lun = lun->lun_id;
420 ppa.g.ch = lun->chnl_id;
425 static inline int ppa_to_slc(struct nvm_dev *dev, int slc_pg)
427 return dev->lptbl[slc_pg];
430 typedef blk_qc_t (nvm_tgt_make_rq_fn)(struct request_queue *, struct bio *);
431 typedef sector_t (nvm_tgt_capacity_fn)(void *);
432 typedef void *(nvm_tgt_init_fn)(struct nvm_dev *, struct gendisk *, int, int);
433 typedef void (nvm_tgt_exit_fn)(void *);
435 struct nvm_tgt_type {
437 unsigned int version[3];
439 /* target entry points */
440 nvm_tgt_make_rq_fn *make_rq;
441 nvm_tgt_capacity_fn *capacity;
442 nvm_end_io_fn *end_io;
444 /* module-specific init/teardown */
445 nvm_tgt_init_fn *init;
446 nvm_tgt_exit_fn *exit;
448 /* For internal use */
449 struct list_head list;
452 extern int nvm_register_tgt_type(struct nvm_tgt_type *);
453 extern void nvm_unregister_tgt_type(struct nvm_tgt_type *);
455 extern void *nvm_dev_dma_alloc(struct nvm_dev *, gfp_t, dma_addr_t *);
456 extern void nvm_dev_dma_free(struct nvm_dev *, void *, dma_addr_t);
458 typedef int (nvmm_register_fn)(struct nvm_dev *);
459 typedef void (nvmm_unregister_fn)(struct nvm_dev *);
460 typedef struct nvm_block *(nvmm_get_blk_fn)(struct nvm_dev *,
461 struct nvm_lun *, unsigned long);
462 typedef void (nvmm_put_blk_fn)(struct nvm_dev *, struct nvm_block *);
463 typedef int (nvmm_open_blk_fn)(struct nvm_dev *, struct nvm_block *);
464 typedef int (nvmm_close_blk_fn)(struct nvm_dev *, struct nvm_block *);
465 typedef void (nvmm_flush_blk_fn)(struct nvm_dev *, struct nvm_block *);
466 typedef int (nvmm_submit_io_fn)(struct nvm_dev *, struct nvm_rq *);
467 typedef int (nvmm_erase_blk_fn)(struct nvm_dev *, struct nvm_block *,
469 typedef struct nvm_lun *(nvmm_get_lun_fn)(struct nvm_dev *, int);
470 typedef int (nvmm_reserve_lun)(struct nvm_dev *, int);
471 typedef void (nvmm_release_lun)(struct nvm_dev *, int);
472 typedef void (nvmm_lun_info_print_fn)(struct nvm_dev *);
474 typedef int (nvmm_get_area_fn)(struct nvm_dev *, sector_t *, sector_t);
475 typedef void (nvmm_put_area_fn)(struct nvm_dev *, sector_t);
479 unsigned int version[3];
481 nvmm_register_fn *register_mgr;
482 nvmm_unregister_fn *unregister_mgr;
484 /* Block administration callbacks */
485 nvmm_get_blk_fn *get_blk_unlocked;
486 nvmm_put_blk_fn *put_blk_unlocked;
487 nvmm_get_blk_fn *get_blk;
488 nvmm_put_blk_fn *put_blk;
489 nvmm_open_blk_fn *open_blk;
490 nvmm_close_blk_fn *close_blk;
491 nvmm_flush_blk_fn *flush_blk;
493 nvmm_submit_io_fn *submit_io;
494 nvmm_erase_blk_fn *erase_blk;
496 /* Configuration management */
497 nvmm_get_lun_fn *get_lun;
498 nvmm_reserve_lun *reserve_lun;
499 nvmm_release_lun *release_lun;
502 nvmm_lun_info_print_fn *lun_info_print;
504 nvmm_get_area_fn *get_area;
505 nvmm_put_area_fn *put_area;
507 struct list_head list;
510 extern int nvm_register_mgr(struct nvmm_type *);
511 extern void nvm_unregister_mgr(struct nvmm_type *);
513 extern struct nvm_block *nvm_get_blk_unlocked(struct nvm_dev *,
514 struct nvm_lun *, unsigned long);
515 extern void nvm_put_blk_unlocked(struct nvm_dev *, struct nvm_block *);
517 extern struct nvm_block *nvm_get_blk(struct nvm_dev *, struct nvm_lun *,
519 extern void nvm_put_blk(struct nvm_dev *, struct nvm_block *);
521 extern int nvm_register(struct request_queue *, char *,
522 struct nvm_dev_ops *);
523 extern void nvm_unregister(char *);
525 extern int nvm_submit_io(struct nvm_dev *, struct nvm_rq *);
526 extern void nvm_generic_to_addr_mode(struct nvm_dev *, struct nvm_rq *);
527 extern void nvm_addr_to_generic_mode(struct nvm_dev *, struct nvm_rq *);
528 extern int nvm_set_rqd_ppalist(struct nvm_dev *, struct nvm_rq *,
529 struct ppa_addr *, int, int);
530 extern void nvm_free_rqd_ppalist(struct nvm_dev *, struct nvm_rq *);
531 extern int nvm_erase_ppa(struct nvm_dev *, struct ppa_addr *, int);
532 extern int nvm_erase_blk(struct nvm_dev *, struct nvm_block *);
533 extern void nvm_end_io(struct nvm_rq *, int);
534 extern int nvm_submit_ppa(struct nvm_dev *, struct ppa_addr *, int, int, int,
536 extern int nvm_submit_ppa_list(struct nvm_dev *, struct ppa_addr *, int, int,
538 extern int nvm_bb_tbl_fold(struct nvm_dev *, u8 *, int);
539 extern int nvm_get_bb_tbl(struct nvm_dev *, struct ppa_addr, u8 *);
542 #define NVM_SYSBLK_MAGIC 0x4E564D53 /* "NVMS" */
544 /* system block on disk representation */
545 struct nvm_system_block {
546 __be32 magic; /* magic signature */
547 __be32 seqnr; /* sequence number */
548 __be32 erase_cnt; /* erase count */
549 __be16 version; /* version number */
550 u8 mmtype[NVM_MMTYPE_LEN]; /* media manager name */
551 __be64 fs_ppa; /* PPA for media manager
555 extern int nvm_get_sysblock(struct nvm_dev *, struct nvm_sb_info *);
556 extern int nvm_update_sysblock(struct nvm_dev *, struct nvm_sb_info *);
557 extern int nvm_init_sysblock(struct nvm_dev *, struct nvm_sb_info *);
559 extern int nvm_dev_factory(struct nvm_dev *, int flags);
561 #define nvm_for_each_lun_ppa(dev, ppa, chid, lunid) \
562 for ((chid) = 0, (ppa).ppa = 0; (chid) < (dev)->nr_chnls; \
563 (chid)++, (ppa).g.ch = (chid)) \
564 for ((lunid) = 0; (lunid) < (dev)->luns_per_chnl; \
565 (lunid)++, (ppa).g.lun = (lunid))
567 #else /* CONFIG_NVM */
570 static inline int nvm_register(struct request_queue *q, char *disk_name,
571 struct nvm_dev_ops *ops)
575 static inline void nvm_unregister(char *disk_name) {}
576 #endif /* CONFIG_NVM */
577 #endif /* LIGHTNVM.H */