lightnvm.h 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450
  1. #ifndef NVM_H
  2. #define NVM_H
  3. #include <linux/types.h>
  4. enum {
  5. NVM_IO_OK = 0,
  6. NVM_IO_REQUEUE = 1,
  7. NVM_IO_DONE = 2,
  8. NVM_IO_ERR = 3,
  9. NVM_IOTYPE_NONE = 0,
  10. NVM_IOTYPE_GC = 1,
  11. };
  12. #define NVM_BLK_BITS (16)
  13. #define NVM_PG_BITS (16)
  14. #define NVM_SEC_BITS (8)
  15. #define NVM_PL_BITS (8)
  16. #define NVM_LUN_BITS (8)
  17. #define NVM_CH_BITS (8)
  18. struct ppa_addr {
  19. /* Generic structure for all addresses */
  20. union {
  21. struct {
  22. u64 blk : NVM_BLK_BITS;
  23. u64 pg : NVM_PG_BITS;
  24. u64 sec : NVM_SEC_BITS;
  25. u64 pl : NVM_PL_BITS;
  26. u64 lun : NVM_LUN_BITS;
  27. u64 ch : NVM_CH_BITS;
  28. } g;
  29. u64 ppa;
  30. };
  31. };
  32. struct nvm_rq;
  33. struct nvm_id;
  34. struct nvm_dev;
  35. typedef int (nvm_l2p_update_fn)(u64, u32, __le64 *, void *);
  36. typedef int (nvm_bb_update_fn)(struct ppa_addr, int, u8 *, void *);
  37. typedef int (nvm_id_fn)(struct nvm_dev *, struct nvm_id *);
  38. typedef int (nvm_get_l2p_tbl_fn)(struct nvm_dev *, u64, u32,
  39. nvm_l2p_update_fn *, void *);
  40. typedef int (nvm_op_bb_tbl_fn)(struct nvm_dev *, struct ppa_addr, int,
  41. nvm_bb_update_fn *, void *);
  42. typedef int (nvm_op_set_bb_fn)(struct nvm_dev *, struct nvm_rq *, int);
  43. typedef int (nvm_submit_io_fn)(struct nvm_dev *, struct nvm_rq *);
  44. typedef int (nvm_erase_blk_fn)(struct nvm_dev *, struct nvm_rq *);
  45. typedef void *(nvm_create_dma_pool_fn)(struct nvm_dev *, char *);
  46. typedef void (nvm_destroy_dma_pool_fn)(void *);
  47. typedef void *(nvm_dev_dma_alloc_fn)(struct nvm_dev *, void *, gfp_t,
  48. dma_addr_t *);
  49. typedef void (nvm_dev_dma_free_fn)(void *, void*, dma_addr_t);
  50. struct nvm_dev_ops {
  51. nvm_id_fn *identity;
  52. nvm_get_l2p_tbl_fn *get_l2p_tbl;
  53. nvm_op_bb_tbl_fn *get_bb_tbl;
  54. nvm_op_set_bb_fn *set_bb_tbl;
  55. nvm_submit_io_fn *submit_io;
  56. nvm_erase_blk_fn *erase_block;
  57. nvm_create_dma_pool_fn *create_dma_pool;
  58. nvm_destroy_dma_pool_fn *destroy_dma_pool;
  59. nvm_dev_dma_alloc_fn *dev_dma_alloc;
  60. nvm_dev_dma_free_fn *dev_dma_free;
  61. unsigned int max_phys_sect;
  62. };
  63. #ifdef CONFIG_NVM
  64. #include <linux/blkdev.h>
  65. #include <linux/file.h>
  66. #include <linux/dmapool.h>
  67. enum {
  68. /* HW Responsibilities */
  69. NVM_RSP_L2P = 1 << 0,
  70. NVM_RSP_ECC = 1 << 1,
  71. /* Physical Adressing Mode */
  72. NVM_ADDRMODE_LINEAR = 0,
  73. NVM_ADDRMODE_CHANNEL = 1,
  74. /* Plane programming mode for LUN */
  75. NVM_PLANE_SINGLE = 0,
  76. NVM_PLANE_DOUBLE = 1,
  77. NVM_PLANE_QUAD = 2,
  78. /* Status codes */
  79. NVM_RSP_SUCCESS = 0x0,
  80. NVM_RSP_NOT_CHANGEABLE = 0x1,
  81. NVM_RSP_ERR_FAILWRITE = 0x40ff,
  82. NVM_RSP_ERR_EMPTYPAGE = 0x42ff,
  83. /* Device opcodes */
  84. NVM_OP_HBREAD = 0x02,
  85. NVM_OP_HBWRITE = 0x81,
  86. NVM_OP_PWRITE = 0x91,
  87. NVM_OP_PREAD = 0x92,
  88. NVM_OP_ERASE = 0x90,
  89. /* PPA Command Flags */
  90. NVM_IO_SNGL_ACCESS = 0x0,
  91. NVM_IO_DUAL_ACCESS = 0x1,
  92. NVM_IO_QUAD_ACCESS = 0x2,
  93. /* NAND Access Modes */
  94. NVM_IO_SUSPEND = 0x80,
  95. NVM_IO_SLC_MODE = 0x100,
  96. NVM_IO_SCRAMBLE_DISABLE = 0x200,
  97. /* Block Types */
  98. NVM_BLK_T_FREE = 0x0,
  99. NVM_BLK_T_BAD = 0x1,
  100. NVM_BLK_T_GRWN_BAD = 0x2,
  101. NVM_BLK_T_DEV = 0x4,
  102. NVM_BLK_T_HOST = 0x8,
  103. };
  104. struct nvm_id_group {
  105. u8 mtype;
  106. u8 fmtype;
  107. u8 num_ch;
  108. u8 num_lun;
  109. u8 num_pln;
  110. u16 num_blk;
  111. u16 num_pg;
  112. u16 fpg_sz;
  113. u16 csecs;
  114. u16 sos;
  115. u32 trdt;
  116. u32 trdm;
  117. u32 tprt;
  118. u32 tprm;
  119. u32 tbet;
  120. u32 tbem;
  121. u32 mpos;
  122. u32 mccap;
  123. u16 cpar;
  124. };
  125. struct nvm_addr_format {
  126. u8 ch_offset;
  127. u8 ch_len;
  128. u8 lun_offset;
  129. u8 lun_len;
  130. u8 pln_offset;
  131. u8 pln_len;
  132. u8 blk_offset;
  133. u8 blk_len;
  134. u8 pg_offset;
  135. u8 pg_len;
  136. u8 sect_offset;
  137. u8 sect_len;
  138. };
  139. struct nvm_id {
  140. u8 ver_id;
  141. u8 vmnt;
  142. u8 cgrps;
  143. u32 cap;
  144. u32 dom;
  145. struct nvm_addr_format ppaf;
  146. struct nvm_id_group groups[4];
  147. } __packed;
  148. struct nvm_target {
  149. struct list_head list;
  150. struct nvm_tgt_type *type;
  151. struct gendisk *disk;
  152. };
  153. struct nvm_tgt_instance {
  154. struct nvm_tgt_type *tt;
  155. };
  156. #define ADDR_EMPTY (~0ULL)
  157. #define NVM_VERSION_MAJOR 1
  158. #define NVM_VERSION_MINOR 0
  159. #define NVM_VERSION_PATCH 0
  160. struct nvm_rq {
  161. struct nvm_tgt_instance *ins;
  162. struct nvm_dev *dev;
  163. struct bio *bio;
  164. union {
  165. struct ppa_addr ppa_addr;
  166. dma_addr_t dma_ppa_list;
  167. };
  168. struct ppa_addr *ppa_list;
  169. void *metadata;
  170. dma_addr_t dma_metadata;
  171. uint8_t opcode;
  172. uint16_t nr_pages;
  173. uint16_t flags;
  174. };
  175. static inline struct nvm_rq *nvm_rq_from_pdu(void *pdu)
  176. {
  177. return pdu - sizeof(struct nvm_rq);
  178. }
  179. static inline void *nvm_rq_to_pdu(struct nvm_rq *rqdata)
  180. {
  181. return rqdata + 1;
  182. }
  183. struct nvm_block;
  184. struct nvm_lun {
  185. int id;
  186. int lun_id;
  187. int chnl_id;
  188. unsigned int nr_inuse_blocks; /* Number of used blocks */
  189. unsigned int nr_free_blocks; /* Number of unused blocks */
  190. unsigned int nr_bad_blocks; /* Number of bad blocks */
  191. struct nvm_block *blocks;
  192. spinlock_t lock;
  193. };
  194. struct nvm_block {
  195. struct list_head list;
  196. struct nvm_lun *lun;
  197. unsigned long id;
  198. void *priv;
  199. int type;
  200. };
  201. struct nvm_dev {
  202. struct nvm_dev_ops *ops;
  203. struct list_head devices;
  204. struct list_head online_targets;
  205. /* Media manager */
  206. struct nvmm_type *mt;
  207. void *mp;
  208. /* Device information */
  209. int nr_chnls;
  210. int nr_planes;
  211. int luns_per_chnl;
  212. int sec_per_pg; /* only sectors for a single page */
  213. int pgs_per_blk;
  214. int blks_per_lun;
  215. int sec_size;
  216. int oob_size;
  217. struct nvm_addr_format ppaf;
  218. /* Calculated/Cached values. These do not reflect the actual usable
  219. * blocks at run-time.
  220. */
  221. int max_rq_size;
  222. int plane_mode; /* drive device in single, double or quad mode */
  223. int sec_per_pl; /* all sectors across planes */
  224. int sec_per_blk;
  225. int sec_per_lun;
  226. unsigned long total_pages;
  227. unsigned long total_blocks;
  228. int nr_luns;
  229. unsigned max_pages_per_blk;
  230. void *ppalist_pool;
  231. struct nvm_id identity;
  232. /* Backend device */
  233. struct request_queue *q;
  234. char name[DISK_NAME_LEN];
  235. };
  236. static inline struct ppa_addr generic_to_dev_addr(struct nvm_dev *dev,
  237. struct ppa_addr r)
  238. {
  239. struct ppa_addr l;
  240. l.ppa = ((u64)r.g.blk) << dev->ppaf.blk_offset;
  241. l.ppa |= ((u64)r.g.pg) << dev->ppaf.pg_offset;
  242. l.ppa |= ((u64)r.g.sec) << dev->ppaf.sect_offset;
  243. l.ppa |= ((u64)r.g.pl) << dev->ppaf.pln_offset;
  244. l.ppa |= ((u64)r.g.lun) << dev->ppaf.lun_offset;
  245. l.ppa |= ((u64)r.g.ch) << dev->ppaf.ch_offset;
  246. return l;
  247. }
  248. static inline struct ppa_addr dev_to_generic_addr(struct nvm_dev *dev,
  249. struct ppa_addr r)
  250. {
  251. struct ppa_addr l;
  252. l.ppa = 0;
  253. /*
  254. * (r.ppa << X offset) & X len bitmask. X eq. blk, pg, etc.
  255. */
  256. l.g.blk = (r.ppa >> dev->ppaf.blk_offset) &
  257. (((1 << dev->ppaf.blk_len) - 1));
  258. l.g.pg |= (r.ppa >> dev->ppaf.pg_offset) &
  259. (((1 << dev->ppaf.pg_len) - 1));
  260. l.g.sec |= (r.ppa >> dev->ppaf.sect_offset) &
  261. (((1 << dev->ppaf.sect_len) - 1));
  262. l.g.pl |= (r.ppa >> dev->ppaf.pln_offset) &
  263. (((1 << dev->ppaf.pln_len) - 1));
  264. l.g.lun |= (r.ppa >> dev->ppaf.lun_offset) &
  265. (((1 << dev->ppaf.lun_len) - 1));
  266. l.g.ch |= (r.ppa >> dev->ppaf.ch_offset) &
  267. (((1 << dev->ppaf.ch_len) - 1));
  268. return l;
  269. }
  270. static inline int ppa_empty(struct ppa_addr ppa_addr)
  271. {
  272. return (ppa_addr.ppa == ADDR_EMPTY);
  273. }
  274. static inline void ppa_set_empty(struct ppa_addr *ppa_addr)
  275. {
  276. ppa_addr->ppa = ADDR_EMPTY;
  277. }
  278. static inline struct ppa_addr block_to_ppa(struct nvm_dev *dev,
  279. struct nvm_block *blk)
  280. {
  281. struct ppa_addr ppa;
  282. struct nvm_lun *lun = blk->lun;
  283. ppa.ppa = 0;
  284. ppa.g.blk = blk->id % dev->blks_per_lun;
  285. ppa.g.lun = lun->lun_id;
  286. ppa.g.ch = lun->chnl_id;
  287. return ppa;
  288. }
  289. typedef blk_qc_t (nvm_tgt_make_rq_fn)(struct request_queue *, struct bio *);
  290. typedef sector_t (nvm_tgt_capacity_fn)(void *);
  291. typedef int (nvm_tgt_end_io_fn)(struct nvm_rq *, int);
  292. typedef void *(nvm_tgt_init_fn)(struct nvm_dev *, struct gendisk *, int, int);
  293. typedef void (nvm_tgt_exit_fn)(void *);
  294. struct nvm_tgt_type {
  295. const char *name;
  296. unsigned int version[3];
  297. /* target entry points */
  298. nvm_tgt_make_rq_fn *make_rq;
  299. nvm_tgt_capacity_fn *capacity;
  300. nvm_tgt_end_io_fn *end_io;
  301. /* module-specific init/teardown */
  302. nvm_tgt_init_fn *init;
  303. nvm_tgt_exit_fn *exit;
  304. /* For internal use */
  305. struct list_head list;
  306. };
  307. extern int nvm_register_target(struct nvm_tgt_type *);
  308. extern void nvm_unregister_target(struct nvm_tgt_type *);
  309. extern void *nvm_dev_dma_alloc(struct nvm_dev *, gfp_t, dma_addr_t *);
  310. extern void nvm_dev_dma_free(struct nvm_dev *, void *, dma_addr_t);
  311. typedef int (nvmm_register_fn)(struct nvm_dev *);
  312. typedef void (nvmm_unregister_fn)(struct nvm_dev *);
  313. typedef struct nvm_block *(nvmm_get_blk_fn)(struct nvm_dev *,
  314. struct nvm_lun *, unsigned long);
  315. typedef void (nvmm_put_blk_fn)(struct nvm_dev *, struct nvm_block *);
  316. typedef int (nvmm_open_blk_fn)(struct nvm_dev *, struct nvm_block *);
  317. typedef int (nvmm_close_blk_fn)(struct nvm_dev *, struct nvm_block *);
  318. typedef void (nvmm_flush_blk_fn)(struct nvm_dev *, struct nvm_block *);
  319. typedef int (nvmm_submit_io_fn)(struct nvm_dev *, struct nvm_rq *);
  320. typedef int (nvmm_end_io_fn)(struct nvm_rq *, int);
  321. typedef int (nvmm_erase_blk_fn)(struct nvm_dev *, struct nvm_block *,
  322. unsigned long);
  323. typedef struct nvm_lun *(nvmm_get_lun_fn)(struct nvm_dev *, int);
  324. typedef void (nvmm_lun_info_print_fn)(struct nvm_dev *);
  325. struct nvmm_type {
  326. const char *name;
  327. unsigned int version[3];
  328. nvmm_register_fn *register_mgr;
  329. nvmm_unregister_fn *unregister_mgr;
  330. /* Block administration callbacks */
  331. nvmm_get_blk_fn *get_blk;
  332. nvmm_put_blk_fn *put_blk;
  333. nvmm_open_blk_fn *open_blk;
  334. nvmm_close_blk_fn *close_blk;
  335. nvmm_flush_blk_fn *flush_blk;
  336. nvmm_submit_io_fn *submit_io;
  337. nvmm_end_io_fn *end_io;
  338. nvmm_erase_blk_fn *erase_blk;
  339. /* Configuration management */
  340. nvmm_get_lun_fn *get_lun;
  341. /* Statistics */
  342. nvmm_lun_info_print_fn *lun_info_print;
  343. struct list_head list;
  344. };
  345. extern int nvm_register_mgr(struct nvmm_type *);
  346. extern void nvm_unregister_mgr(struct nvmm_type *);
  347. extern struct nvm_block *nvm_get_blk(struct nvm_dev *, struct nvm_lun *,
  348. unsigned long);
  349. extern void nvm_put_blk(struct nvm_dev *, struct nvm_block *);
  350. extern int nvm_register(struct request_queue *, char *,
  351. struct nvm_dev_ops *);
  352. extern void nvm_unregister(char *);
  353. extern int nvm_submit_io(struct nvm_dev *, struct nvm_rq *);
  354. extern int nvm_erase_blk(struct nvm_dev *, struct nvm_block *);
  355. #else /* CONFIG_NVM */
  356. struct nvm_dev_ops;
  357. static inline int nvm_register(struct request_queue *q, char *disk_name,
  358. struct nvm_dev_ops *ops)
  359. {
  360. return -EINVAL;
  361. }
  362. static inline void nvm_unregister(char *disk_name) {}
  363. #endif /* CONFIG_NVM */
  364. #endif /* LIGHTNVM.H */