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_bb_update_fn
)(struct ppa_addr
, int, u8
*, void *);
45 typedef int (nvm_id_fn
)(struct nvm_dev
*, struct nvm_id
*);
46 typedef int (nvm_get_l2p_tbl_fn
)(struct nvm_dev
*, u64
, u32
,
47 nvm_l2p_update_fn
*, void *);
48 typedef int (nvm_op_bb_tbl_fn
)(struct nvm_dev
*, struct ppa_addr
, int,
49 nvm_bb_update_fn
*, void *);
50 typedef int (nvm_op_set_bb_fn
)(struct nvm_dev
*, struct nvm_rq
*, int);
51 typedef int (nvm_submit_io_fn
)(struct nvm_dev
*, struct nvm_rq
*);
52 typedef int (nvm_erase_blk_fn
)(struct nvm_dev
*, struct nvm_rq
*);
53 typedef void *(nvm_create_dma_pool_fn
)(struct nvm_dev
*, char *);
54 typedef void (nvm_destroy_dma_pool_fn
)(void *);
55 typedef void *(nvm_dev_dma_alloc_fn
)(struct nvm_dev
*, void *, gfp_t
,
57 typedef void (nvm_dev_dma_free_fn
)(void *, void*, dma_addr_t
);
61 nvm_get_l2p_tbl_fn
*get_l2p_tbl
;
62 nvm_op_bb_tbl_fn
*get_bb_tbl
;
63 nvm_op_set_bb_fn
*set_bb_tbl
;
65 nvm_submit_io_fn
*submit_io
;
66 nvm_erase_blk_fn
*erase_block
;
68 nvm_create_dma_pool_fn
*create_dma_pool
;
69 nvm_destroy_dma_pool_fn
*destroy_dma_pool
;
70 nvm_dev_dma_alloc_fn
*dev_dma_alloc
;
71 nvm_dev_dma_free_fn
*dev_dma_free
;
73 unsigned int max_phys_sect
;
80 #include <linux/blkdev.h>
81 #include <linux/file.h>
82 #include <linux/dmapool.h>
83 #include <uapi/linux/lightnvm.h>
86 /* HW Responsibilities */
90 /* Physical Adressing Mode */
91 NVM_ADDRMODE_LINEAR
= 0,
92 NVM_ADDRMODE_CHANNEL
= 1,
94 /* Plane programming mode for LUN */
100 NVM_RSP_SUCCESS
= 0x0,
101 NVM_RSP_NOT_CHANGEABLE
= 0x1,
102 NVM_RSP_ERR_FAILWRITE
= 0x40ff,
103 NVM_RSP_ERR_EMPTYPAGE
= 0x42ff,
106 NVM_OP_HBREAD
= 0x02,
107 NVM_OP_HBWRITE
= 0x81,
108 NVM_OP_PWRITE
= 0x91,
112 /* PPA Command Flags */
113 NVM_IO_SNGL_ACCESS
= 0x0,
114 NVM_IO_DUAL_ACCESS
= 0x1,
115 NVM_IO_QUAD_ACCESS
= 0x2,
117 /* NAND Access Modes */
118 NVM_IO_SUSPEND
= 0x80,
119 NVM_IO_SLC_MODE
= 0x100,
120 NVM_IO_SCRAMBLE_DISABLE
= 0x200,
123 NVM_BLK_T_FREE
= 0x0,
125 NVM_BLK_T_GRWN_BAD
= 0x2,
127 NVM_BLK_T_HOST
= 0x8,
129 /* Memory capabilities */
130 NVM_ID_CAP_SLC
= 0x1,
131 NVM_ID_CAP_CMD_SUSPEND
= 0x2,
132 NVM_ID_CAP_SCRAMBLE
= 0x4,
133 NVM_ID_CAP_ENCRYPT
= 0x8,
136 NVM_ID_FMTYPE_SLC
= 0,
137 NVM_ID_FMTYPE_MLC
= 1,
139 /* Device capabilities */
140 NVM_ID_DCAP_BBLKMGMT
= 0x1,
141 NVM_UD_DCAP_ECC
= 0x2,
144 struct nvm_id_lp_mlc
{
149 struct nvm_id_lp_tbl
{
151 struct nvm_id_lp_mlc mlc
;
154 struct nvm_id_group
{
175 struct nvm_id_lp_tbl lptbl
;
178 struct nvm_addr_format
{
199 struct nvm_addr_format ppaf
;
200 struct nvm_id_group groups
[4];
204 struct list_head list
;
205 struct nvm_tgt_type
*type
;
206 struct gendisk
*disk
;
209 struct nvm_tgt_instance
{
210 struct nvm_tgt_type
*tt
;
213 #define ADDR_EMPTY (~0ULL)
215 #define NVM_VERSION_MAJOR 1
216 #define NVM_VERSION_MINOR 0
217 #define NVM_VERSION_PATCH 0
220 typedef void (nvm_end_io_fn
)(struct nvm_rq
*);
223 struct nvm_tgt_instance
*ins
;
229 struct ppa_addr ppa_addr
;
230 dma_addr_t dma_ppa_list
;
233 struct ppa_addr
*ppa_list
;
236 dma_addr_t dma_metadata
;
238 struct completion
*wait
;
239 nvm_end_io_fn
*end_io
;
245 u64 ppa_status
; /* ppa media status */
249 static inline struct nvm_rq
*nvm_rq_from_pdu(void *pdu
)
251 return pdu
- sizeof(struct nvm_rq
);
254 static inline void *nvm_rq_to_pdu(struct nvm_rq
*rqdata
)
267 /* It is up to the target to mark blocks as closed. If the target does
268 * not do it, all blocks are marked as open, and nr_open_blocks
269 * represents the number of blocks in use
271 unsigned int nr_open_blocks
; /* Number of used, writable blocks */
272 unsigned int nr_closed_blocks
; /* Number of used, read-only blocks */
273 unsigned int nr_free_blocks
; /* Number of unused blocks */
274 unsigned int nr_bad_blocks
; /* Number of bad blocks */
278 struct nvm_block
*blocks
;
282 NVM_BLK_ST_FREE
= 0x1, /* Free block */
283 NVM_BLK_ST_OPEN
= 0x2, /* Open block - read-write */
284 NVM_BLK_ST_CLOSED
= 0x4, /* Closed block - read-only */
285 NVM_BLK_ST_BAD
= 0x8, /* Bad block */
289 struct list_head list
;
297 /* system block cpu representation */
300 unsigned long erase_cnt
;
301 unsigned int version
;
302 char mmtype
[NVM_MMTYPE_LEN
];
303 struct ppa_addr fs_ppa
;
307 struct nvm_dev_ops
*ops
;
309 struct list_head devices
;
310 struct list_head online_targets
;
313 struct nvmm_type
*mt
;
317 struct nvm_sb_info sb
;
319 /* Device information */
323 int sec_per_pg
; /* only sectors for a single page */
329 struct nvm_addr_format ppaf
;
331 /* Calculated/Cached values. These do not reflect the actual usable
332 * blocks at run-time.
335 int plane_mode
; /* drive device in single, double or quad mode */
337 int sec_per_pl
; /* all sectors across planes */
341 /* lower page table */
345 unsigned long total_blocks
;
346 unsigned long total_secs
;
348 unsigned max_pages_per_blk
;
350 unsigned long *lun_map
;
353 struct nvm_id identity
;
356 struct request_queue
*q
;
357 char name
[DISK_NAME_LEN
];
363 static inline struct ppa_addr
generic_to_dev_addr(struct nvm_dev
*dev
,
368 l
.ppa
= ((u64
)r
.g
.blk
) << dev
->ppaf
.blk_offset
;
369 l
.ppa
|= ((u64
)r
.g
.pg
) << dev
->ppaf
.pg_offset
;
370 l
.ppa
|= ((u64
)r
.g
.sec
) << dev
->ppaf
.sect_offset
;
371 l
.ppa
|= ((u64
)r
.g
.pl
) << dev
->ppaf
.pln_offset
;
372 l
.ppa
|= ((u64
)r
.g
.lun
) << dev
->ppaf
.lun_offset
;
373 l
.ppa
|= ((u64
)r
.g
.ch
) << dev
->ppaf
.ch_offset
;
378 static inline struct ppa_addr
dev_to_generic_addr(struct nvm_dev
*dev
,
384 * (r.ppa << X offset) & X len bitmask. X eq. blk, pg, etc.
386 l
.g
.blk
= (r
.ppa
>> dev
->ppaf
.blk_offset
) &
387 (((1 << dev
->ppaf
.blk_len
) - 1));
388 l
.g
.pg
|= (r
.ppa
>> dev
->ppaf
.pg_offset
) &
389 (((1 << dev
->ppaf
.pg_len
) - 1));
390 l
.g
.sec
|= (r
.ppa
>> dev
->ppaf
.sect_offset
) &
391 (((1 << dev
->ppaf
.sect_len
) - 1));
392 l
.g
.pl
|= (r
.ppa
>> dev
->ppaf
.pln_offset
) &
393 (((1 << dev
->ppaf
.pln_len
) - 1));
394 l
.g
.lun
|= (r
.ppa
>> dev
->ppaf
.lun_offset
) &
395 (((1 << dev
->ppaf
.lun_len
) - 1));
396 l
.g
.ch
|= (r
.ppa
>> dev
->ppaf
.ch_offset
) &
397 (((1 << dev
->ppaf
.ch_len
) - 1));
402 static inline int ppa_empty(struct ppa_addr ppa_addr
)
404 return (ppa_addr
.ppa
== ADDR_EMPTY
);
407 static inline void ppa_set_empty(struct ppa_addr
*ppa_addr
)
409 ppa_addr
->ppa
= ADDR_EMPTY
;
412 static inline struct ppa_addr
block_to_ppa(struct nvm_dev
*dev
,
413 struct nvm_block
*blk
)
416 struct nvm_lun
*lun
= blk
->lun
;
419 ppa
.g
.blk
= blk
->id
% dev
->blks_per_lun
;
420 ppa
.g
.lun
= lun
->lun_id
;
421 ppa
.g
.ch
= lun
->chnl_id
;
426 static inline int ppa_to_slc(struct nvm_dev
*dev
, int slc_pg
)
428 return dev
->lptbl
[slc_pg
];
431 typedef blk_qc_t (nvm_tgt_make_rq_fn
)(struct request_queue
*, struct bio
*);
432 typedef sector_t (nvm_tgt_capacity_fn
)(void *);
433 typedef void *(nvm_tgt_init_fn
)(struct nvm_dev
*, struct gendisk
*, int, int);
434 typedef void (nvm_tgt_exit_fn
)(void *);
436 struct nvm_tgt_type
{
438 unsigned int version
[3];
440 /* target entry points */
441 nvm_tgt_make_rq_fn
*make_rq
;
442 nvm_tgt_capacity_fn
*capacity
;
443 nvm_end_io_fn
*end_io
;
445 /* module-specific init/teardown */
446 nvm_tgt_init_fn
*init
;
447 nvm_tgt_exit_fn
*exit
;
449 /* For internal use */
450 struct list_head list
;
453 extern int nvm_register_target(struct nvm_tgt_type
*);
454 extern void nvm_unregister_target(struct nvm_tgt_type
*);
456 extern void *nvm_dev_dma_alloc(struct nvm_dev
*, gfp_t
, dma_addr_t
*);
457 extern void nvm_dev_dma_free(struct nvm_dev
*, void *, dma_addr_t
);
459 typedef int (nvmm_register_fn
)(struct nvm_dev
*);
460 typedef void (nvmm_unregister_fn
)(struct nvm_dev
*);
461 typedef struct nvm_block
*(nvmm_get_blk_fn
)(struct nvm_dev
*,
462 struct nvm_lun
*, unsigned long);
463 typedef void (nvmm_put_blk_fn
)(struct nvm_dev
*, struct nvm_block
*);
464 typedef int (nvmm_open_blk_fn
)(struct nvm_dev
*, struct nvm_block
*);
465 typedef int (nvmm_close_blk_fn
)(struct nvm_dev
*, struct nvm_block
*);
466 typedef void (nvmm_flush_blk_fn
)(struct nvm_dev
*, struct nvm_block
*);
467 typedef int (nvmm_submit_io_fn
)(struct nvm_dev
*, struct nvm_rq
*);
468 typedef int (nvmm_erase_blk_fn
)(struct nvm_dev
*, struct nvm_block
*,
470 typedef struct nvm_lun
*(nvmm_get_lun_fn
)(struct nvm_dev
*, int);
471 typedef int (nvmm_reserve_lun
)(struct nvm_dev
*, int);
472 typedef void (nvmm_release_lun
)(struct nvm_dev
*, int);
473 typedef void (nvmm_lun_info_print_fn
)(struct nvm_dev
*);
475 typedef int (nvmm_get_area_fn
)(struct nvm_dev
*, sector_t
*, sector_t
);
476 typedef void (nvmm_put_area_fn
)(struct nvm_dev
*, sector_t
);
480 unsigned int version
[3];
482 nvmm_register_fn
*register_mgr
;
483 nvmm_unregister_fn
*unregister_mgr
;
485 /* Block administration callbacks */
486 nvmm_get_blk_fn
*get_blk_unlocked
;
487 nvmm_put_blk_fn
*put_blk_unlocked
;
488 nvmm_get_blk_fn
*get_blk
;
489 nvmm_put_blk_fn
*put_blk
;
490 nvmm_open_blk_fn
*open_blk
;
491 nvmm_close_blk_fn
*close_blk
;
492 nvmm_flush_blk_fn
*flush_blk
;
494 nvmm_submit_io_fn
*submit_io
;
495 nvmm_erase_blk_fn
*erase_blk
;
497 /* Configuration management */
498 nvmm_get_lun_fn
*get_lun
;
499 nvmm_reserve_lun
*reserve_lun
;
500 nvmm_release_lun
*release_lun
;
503 nvmm_lun_info_print_fn
*lun_info_print
;
505 nvmm_get_area_fn
*get_area
;
506 nvmm_put_area_fn
*put_area
;
508 struct list_head list
;
511 extern int nvm_register_mgr(struct nvmm_type
*);
512 extern void nvm_unregister_mgr(struct nvmm_type
*);
514 extern struct nvm_block
*nvm_get_blk_unlocked(struct nvm_dev
*,
515 struct nvm_lun
*, unsigned long);
516 extern void nvm_put_blk_unlocked(struct nvm_dev
*, struct nvm_block
*);
518 extern struct nvm_block
*nvm_get_blk(struct nvm_dev
*, struct nvm_lun
*,
520 extern void nvm_put_blk(struct nvm_dev
*, struct nvm_block
*);
522 extern int nvm_register(struct request_queue
*, char *,
523 struct nvm_dev_ops
*);
524 extern void nvm_unregister(char *);
526 extern int nvm_submit_io(struct nvm_dev
*, struct nvm_rq
*);
527 extern void nvm_generic_to_addr_mode(struct nvm_dev
*, struct nvm_rq
*);
528 extern void nvm_addr_to_generic_mode(struct nvm_dev
*, struct nvm_rq
*);
529 extern int nvm_set_rqd_ppalist(struct nvm_dev
*, struct nvm_rq
*,
530 struct ppa_addr
*, int);
531 extern void nvm_free_rqd_ppalist(struct nvm_dev
*, struct nvm_rq
*);
532 extern int nvm_erase_ppa(struct nvm_dev
*, struct ppa_addr
*, int);
533 extern int nvm_erase_blk(struct nvm_dev
*, struct nvm_block
*);
534 extern void nvm_end_io(struct nvm_rq
*, int);
535 extern int nvm_submit_ppa(struct nvm_dev
*, struct ppa_addr
*, int, int, int,
539 #define NVM_SYSBLK_MAGIC 0x4E564D53 /* "NVMS" */
541 /* system block on disk representation */
542 struct nvm_system_block
{
543 __be32 magic
; /* magic signature */
544 __be32 seqnr
; /* sequence number */
545 __be32 erase_cnt
; /* erase count */
546 __be16 version
; /* version number */
547 u8 mmtype
[NVM_MMTYPE_LEN
]; /* media manager name */
548 __be64 fs_ppa
; /* PPA for media manager
552 extern int nvm_get_sysblock(struct nvm_dev
*, struct nvm_sb_info
*);
553 extern int nvm_update_sysblock(struct nvm_dev
*, struct nvm_sb_info
*);
554 extern int nvm_init_sysblock(struct nvm_dev
*, struct nvm_sb_info
*);
556 extern int nvm_dev_factory(struct nvm_dev
*, int flags
);
557 #else /* CONFIG_NVM */
560 static inline int nvm_register(struct request_queue
*q
, char *disk_name
,
561 struct nvm_dev_ops
*ops
)
565 static inline void nvm_unregister(char *disk_name
) {}
566 #endif /* CONFIG_NVM */
567 #endif /* LIGHTNVM.H */