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 request_queue *, 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_free_blocks; /* Number of unused blocks */
217 struct nvm_block *blocks;
223 struct list_head list;
232 struct nvm_dev_ops *ops;
234 struct list_head devices;
235 struct list_head online_targets;
238 struct nvmm_type *mt;
241 /* Device information */
245 int sec_per_pg; /* only sectors for a single page */
250 struct nvm_addr_format ppaf;
252 /* Calculated/Cached values. These do not reflect the actual usable
253 * blocks at run-time.
256 int plane_mode; /* drive device in single, double or quad mode */
258 int sec_per_pl; /* all sectors across planes */
262 unsigned long total_pages;
263 unsigned long total_blocks;
265 unsigned max_pages_per_blk;
269 struct nvm_id identity;
272 struct request_queue *q;
273 char name[DISK_NAME_LEN];
276 static inline struct ppa_addr generic_to_dev_addr(struct nvm_dev *dev,
281 l.ppa = ((u64)r.g.blk) << dev->ppaf.blk_offset;
282 l.ppa |= ((u64)r.g.pg) << dev->ppaf.pg_offset;
283 l.ppa |= ((u64)r.g.sec) << dev->ppaf.sect_offset;
284 l.ppa |= ((u64)r.g.pl) << dev->ppaf.pln_offset;
285 l.ppa |= ((u64)r.g.lun) << dev->ppaf.lun_offset;
286 l.ppa |= ((u64)r.g.ch) << dev->ppaf.ch_offset;
291 static inline struct ppa_addr dev_to_generic_addr(struct nvm_dev *dev,
297 * (r.ppa << X offset) & X len bitmask. X eq. blk, pg, etc.
299 l.g.blk = (r.ppa >> dev->ppaf.blk_offset) &
300 (((1 << dev->ppaf.blk_len) - 1));
301 l.g.pg |= (r.ppa >> dev->ppaf.pg_offset) &
302 (((1 << dev->ppaf.pg_len) - 1));
303 l.g.sec |= (r.ppa >> dev->ppaf.sect_offset) &
304 (((1 << dev->ppaf.sect_len) - 1));
305 l.g.pl |= (r.ppa >> dev->ppaf.pln_offset) &
306 (((1 << dev->ppaf.pln_len) - 1));
307 l.g.lun |= (r.ppa >> dev->ppaf.lun_offset) &
308 (((1 << dev->ppaf.lun_len) - 1));
309 l.g.ch |= (r.ppa >> dev->ppaf.ch_offset) &
310 (((1 << dev->ppaf.ch_len) - 1));
315 static inline int ppa_empty(struct ppa_addr ppa_addr)
317 return (ppa_addr.ppa == ADDR_EMPTY);
320 static inline void ppa_set_empty(struct ppa_addr *ppa_addr)
322 ppa_addr->ppa = ADDR_EMPTY;
325 static inline struct ppa_addr block_to_ppa(struct nvm_dev *dev,
326 struct nvm_block *blk)
329 struct nvm_lun *lun = blk->lun;
332 ppa.g.blk = blk->id % dev->blks_per_lun;
333 ppa.g.lun = lun->lun_id;
334 ppa.g.ch = lun->chnl_id;
339 typedef blk_qc_t (nvm_tgt_make_rq_fn)(struct request_queue *, struct bio *);
340 typedef sector_t (nvm_tgt_capacity_fn)(void *);
341 typedef int (nvm_tgt_end_io_fn)(struct nvm_rq *, int);
342 typedef void *(nvm_tgt_init_fn)(struct nvm_dev *, struct gendisk *, int, int);
343 typedef void (nvm_tgt_exit_fn)(void *);
345 struct nvm_tgt_type {
347 unsigned int version[3];
349 /* target entry points */
350 nvm_tgt_make_rq_fn *make_rq;
351 nvm_tgt_capacity_fn *capacity;
352 nvm_tgt_end_io_fn *end_io;
354 /* module-specific init/teardown */
355 nvm_tgt_init_fn *init;
356 nvm_tgt_exit_fn *exit;
358 /* For internal use */
359 struct list_head list;
362 extern int nvm_register_target(struct nvm_tgt_type *);
363 extern void nvm_unregister_target(struct nvm_tgt_type *);
365 extern void *nvm_dev_dma_alloc(struct nvm_dev *, gfp_t, dma_addr_t *);
366 extern void nvm_dev_dma_free(struct nvm_dev *, void *, dma_addr_t);
368 typedef int (nvmm_register_fn)(struct nvm_dev *);
369 typedef void (nvmm_unregister_fn)(struct nvm_dev *);
370 typedef struct nvm_block *(nvmm_get_blk_fn)(struct nvm_dev *,
371 struct nvm_lun *, unsigned long);
372 typedef void (nvmm_put_blk_fn)(struct nvm_dev *, struct nvm_block *);
373 typedef int (nvmm_open_blk_fn)(struct nvm_dev *, struct nvm_block *);
374 typedef int (nvmm_close_blk_fn)(struct nvm_dev *, struct nvm_block *);
375 typedef void (nvmm_flush_blk_fn)(struct nvm_dev *, struct nvm_block *);
376 typedef int (nvmm_submit_io_fn)(struct nvm_dev *, struct nvm_rq *);
377 typedef int (nvmm_end_io_fn)(struct nvm_rq *, int);
378 typedef int (nvmm_erase_blk_fn)(struct nvm_dev *, struct nvm_block *,
380 typedef struct nvm_lun *(nvmm_get_lun_fn)(struct nvm_dev *, int);
381 typedef void (nvmm_free_blocks_print_fn)(struct nvm_dev *);
385 unsigned int version[3];
387 nvmm_register_fn *register_mgr;
388 nvmm_unregister_fn *unregister_mgr;
390 /* Block administration callbacks */
391 nvmm_get_blk_fn *get_blk;
392 nvmm_put_blk_fn *put_blk;
393 nvmm_open_blk_fn *open_blk;
394 nvmm_close_blk_fn *close_blk;
395 nvmm_flush_blk_fn *flush_blk;
397 nvmm_submit_io_fn *submit_io;
398 nvmm_end_io_fn *end_io;
399 nvmm_erase_blk_fn *erase_blk;
401 /* Configuration management */
402 nvmm_get_lun_fn *get_lun;
405 nvmm_free_blocks_print_fn *free_blocks_print;
406 struct list_head list;
409 extern int nvm_register_mgr(struct nvmm_type *);
410 extern void nvm_unregister_mgr(struct nvmm_type *);
412 extern struct nvm_block *nvm_get_blk(struct nvm_dev *, struct nvm_lun *,
414 extern void nvm_put_blk(struct nvm_dev *, struct nvm_block *);
416 extern int nvm_register(struct request_queue *, char *,
417 struct nvm_dev_ops *);
418 extern void nvm_unregister(char *);
420 extern int nvm_submit_io(struct nvm_dev *, struct nvm_rq *);
421 extern int nvm_erase_blk(struct nvm_dev *, struct nvm_block *);
422 #else /* CONFIG_NVM */
425 static inline int nvm_register(struct request_queue *q, char *disk_name,
426 struct nvm_dev_ops *ops)
430 static inline void nvm_unregister(char *disk_name) {}
431 #endif /* CONFIG_NVM */
432 #endif /* LIGHTNVM.H */