1 /* SPDX-License-Identifier: GPL-2.0 */
5 #include <linux/blkdev.h>
6 #include <linux/types.h>
7 #include <uapi/linux/lightnvm.h>
20 #define NVM_GEN_CH_BITS (8)
21 #define NVM_GEN_LUN_BITS (8)
22 #define NVM_GEN_BLK_BITS (16)
23 #define NVM_GEN_RESERVED (32)
26 #define NVM_12_PG_BITS (16)
27 #define NVM_12_PL_BITS (4)
28 #define NVM_12_SEC_BITS (4)
29 #define NVM_12_RESERVED (8)
32 #define NVM_20_SEC_BITS (24)
33 #define NVM_20_RESERVED (8)
36 NVM_OCSSD_SPEC_12 = 12,
37 NVM_OCSSD_SPEC_20 = 20,
41 /* Generic structure for all addresses */
43 /* generic device format */
45 u64 ch : NVM_GEN_CH_BITS;
46 u64 lun : NVM_GEN_LUN_BITS;
47 u64 blk : NVM_GEN_BLK_BITS;
48 u64 reserved : NVM_GEN_RESERVED;
51 /* 1.2 device format */
53 u64 ch : NVM_GEN_CH_BITS;
54 u64 lun : NVM_GEN_LUN_BITS;
55 u64 blk : NVM_GEN_BLK_BITS;
56 u64 pg : NVM_12_PG_BITS;
57 u64 pl : NVM_12_PL_BITS;
58 u64 sec : NVM_12_SEC_BITS;
59 u64 reserved : NVM_12_RESERVED;
62 /* 2.0 device format */
64 u64 grp : NVM_GEN_CH_BITS;
65 u64 pu : NVM_GEN_LUN_BITS;
66 u64 chk : NVM_GEN_BLK_BITS;
67 u64 sec : NVM_20_SEC_BITS;
68 u64 reserved : NVM_20_RESERVED;
86 typedef int (nvm_id_fn)(struct nvm_dev *);
87 typedef int (nvm_op_bb_tbl_fn)(struct nvm_dev *, struct ppa_addr, u8 *);
88 typedef int (nvm_op_set_bb_fn)(struct nvm_dev *, struct ppa_addr *, int, int);
89 typedef int (nvm_get_chk_meta_fn)(struct nvm_dev *, struct nvm_chk_meta *,
91 typedef int (nvm_submit_io_fn)(struct nvm_dev *, struct nvm_rq *);
92 typedef int (nvm_submit_io_sync_fn)(struct nvm_dev *, struct nvm_rq *);
93 typedef void *(nvm_create_dma_pool_fn)(struct nvm_dev *, char *);
94 typedef void (nvm_destroy_dma_pool_fn)(void *);
95 typedef void *(nvm_dev_dma_alloc_fn)(struct nvm_dev *, void *, gfp_t,
97 typedef void (nvm_dev_dma_free_fn)(void *, void*, dma_addr_t);
101 nvm_op_bb_tbl_fn *get_bb_tbl;
102 nvm_op_set_bb_fn *set_bb_tbl;
104 nvm_get_chk_meta_fn *get_chk_meta;
106 nvm_submit_io_fn *submit_io;
107 nvm_submit_io_sync_fn *submit_io_sync;
109 nvm_create_dma_pool_fn *create_dma_pool;
110 nvm_destroy_dma_pool_fn *destroy_dma_pool;
111 nvm_dev_dma_alloc_fn *dev_dma_alloc;
112 nvm_dev_dma_free_fn *dev_dma_free;
117 #include <linux/blkdev.h>
118 #include <linux/file.h>
119 #include <linux/dmapool.h>
120 #include <uapi/linux/lightnvm.h>
123 /* HW Responsibilities */
124 NVM_RSP_L2P = 1 << 0,
125 NVM_RSP_ECC = 1 << 1,
127 /* Physical Adressing Mode */
128 NVM_ADDRMODE_LINEAR = 0,
129 NVM_ADDRMODE_CHANNEL = 1,
131 /* Plane programming mode for LUN */
132 NVM_PLANE_SINGLE = 1,
133 NVM_PLANE_DOUBLE = 2,
137 NVM_RSP_SUCCESS = 0x0,
138 NVM_RSP_NOT_CHANGEABLE = 0x1,
139 NVM_RSP_ERR_FAILWRITE = 0x40ff,
140 NVM_RSP_ERR_EMPTYPAGE = 0x42ff,
141 NVM_RSP_ERR_FAILECC = 0x4281,
142 NVM_RSP_ERR_FAILCRC = 0x4004,
143 NVM_RSP_WARN_HIGHECC = 0x4700,
146 NVM_OP_PWRITE = 0x91,
150 /* PPA Command Flags */
151 NVM_IO_SNGL_ACCESS = 0x0,
152 NVM_IO_DUAL_ACCESS = 0x1,
153 NVM_IO_QUAD_ACCESS = 0x2,
155 /* NAND Access Modes */
156 NVM_IO_SUSPEND = 0x80,
157 NVM_IO_SLC_MODE = 0x100,
158 NVM_IO_SCRAMBLE_ENABLE = 0x200,
161 NVM_BLK_T_FREE = 0x0,
163 NVM_BLK_T_GRWN_BAD = 0x2,
165 NVM_BLK_T_HOST = 0x8,
167 /* Memory capabilities */
168 NVM_ID_CAP_SLC = 0x1,
169 NVM_ID_CAP_CMD_SUSPEND = 0x2,
170 NVM_ID_CAP_SCRAMBLE = 0x4,
171 NVM_ID_CAP_ENCRYPT = 0x8,
174 NVM_ID_FMTYPE_SLC = 0,
175 NVM_ID_FMTYPE_MLC = 1,
177 /* Device capabilities */
178 NVM_ID_DCAP_BBLKMGMT = 0x1,
179 NVM_UD_DCAP_ECC = 0x2,
182 struct nvm_id_lp_mlc {
187 struct nvm_id_lp_tbl {
189 struct nvm_id_lp_mlc mlc;
192 struct nvm_addrf_12 {
237 NVM_CHK_ST_FREE = 1 << 0,
238 NVM_CHK_ST_CLOSED = 1 << 1,
239 NVM_CHK_ST_OPEN = 1 << 2,
240 NVM_CHK_ST_OFFLINE = 1 << 3,
243 NVM_CHK_TP_W_SEQ = 1 << 0,
244 NVM_CHK_TP_W_RAN = 1 << 1,
245 NVM_CHK_TP_SZ_SPEC = 1 << 4,
249 * Note: The structure size is linked to nvme_nvm_chk_meta such that the same
250 * buffer can be used when converting from little endian to cpu addressing.
252 struct nvm_chk_meta {
263 struct list_head list;
264 struct nvm_tgt_dev *dev;
265 struct nvm_tgt_type *type;
266 struct gendisk *disk;
269 #define ADDR_EMPTY (~0ULL)
271 #define NVM_TARGET_DEFAULT_OP (101)
272 #define NVM_TARGET_MIN_OP (3)
273 #define NVM_TARGET_MAX_OP (80)
275 #define NVM_VERSION_MAJOR 1
276 #define NVM_VERSION_MINOR 0
277 #define NVM_VERSION_PATCH 0
279 #define NVM_MAX_VLBA (64) /* max logical blocks in a vector command */
282 typedef void (nvm_end_io_fn)(struct nvm_rq *);
285 struct nvm_tgt_dev *dev;
290 struct ppa_addr ppa_addr;
291 dma_addr_t dma_ppa_list;
294 struct ppa_addr *ppa_list;
297 dma_addr_t dma_meta_list;
299 nvm_end_io_fn *end_io;
305 u64 ppa_status; /* ppa media status */
311 static inline struct nvm_rq *nvm_rq_from_pdu(void *pdu)
313 return pdu - sizeof(struct nvm_rq);
316 static inline void *nvm_rq_to_pdu(struct nvm_rq *rqdata)
322 NVM_BLK_ST_FREE = 0x1, /* Free block */
323 NVM_BLK_ST_TGT = 0x2, /* Block in use by target */
324 NVM_BLK_ST_BAD = 0x8, /* Bad block */
327 /* Instance geometry */
329 /* device reported version */
333 /* kernel short version */
336 /* instance specific geometry */
338 int num_lun; /* per channel */
340 /* calculated values */
341 int all_luns; /* across channels */
342 int all_chunks; /* across channels */
344 int op; /* over-provision in instance */
346 sector_t total_secs; /* across channels */
349 u32 num_chk; /* chunks per lun */
350 u32 clba; /* sectors per chunk */
351 u16 csecs; /* sector size */
352 u16 sos; /* out-of-band area size */
354 /* device write constrains */
355 u32 ws_min; /* minimum write size */
356 u32 ws_opt; /* optimal write size */
357 u32 mw_cunits; /* distance required for successful read */
358 u32 maxoc; /* maximum open chunks */
359 u32 maxocpu; /* maximum open chunks per parallel unit */
361 /* device capabilities */
365 u32 trdt; /* Avg. Tread (ns) */
366 u32 trdm; /* Max Tread (ns) */
367 u32 tprt; /* Avg. Tprog (ns) */
368 u32 tprm; /* Max Tprog (ns) */
369 u32 tbet; /* Avg. Terase (ns) */
370 u32 tbem; /* Max Terase (ns) */
372 /* generic address format */
373 struct nvm_addrf addrf;
375 /* 1.2 compatibility */
392 /* sub-device structure */
394 /* Device information */
397 /* Base ppas for target LUNs */
398 struct ppa_addr *luns;
400 struct request_queue *q;
402 struct nvm_dev *parent;
407 struct nvm_dev_ops *ops;
409 struct list_head devices;
411 /* Device information */
414 unsigned long *lun_map;
418 struct request_queue *q;
419 char name[DISK_NAME_LEN];
427 /* target management */
428 struct list_head area_list;
429 struct list_head targets;
432 static inline struct ppa_addr generic_to_dev_addr(struct nvm_dev *dev,
435 struct nvm_geo *geo = &dev->geo;
438 if (geo->version == NVM_OCSSD_SPEC_12) {
439 struct nvm_addrf_12 *ppaf = (struct nvm_addrf_12 *)&geo->addrf;
441 l.ppa = ((u64)r.g.ch) << ppaf->ch_offset;
442 l.ppa |= ((u64)r.g.lun) << ppaf->lun_offset;
443 l.ppa |= ((u64)r.g.blk) << ppaf->blk_offset;
444 l.ppa |= ((u64)r.g.pg) << ppaf->pg_offset;
445 l.ppa |= ((u64)r.g.pl) << ppaf->pln_offset;
446 l.ppa |= ((u64)r.g.sec) << ppaf->sec_offset;
448 struct nvm_addrf *lbaf = &geo->addrf;
450 l.ppa = ((u64)r.m.grp) << lbaf->ch_offset;
451 l.ppa |= ((u64)r.m.pu) << lbaf->lun_offset;
452 l.ppa |= ((u64)r.m.chk) << lbaf->chk_offset;
453 l.ppa |= ((u64)r.m.sec) << lbaf->sec_offset;
459 static inline struct ppa_addr dev_to_generic_addr(struct nvm_dev *dev,
462 struct nvm_geo *geo = &dev->geo;
467 if (geo->version == NVM_OCSSD_SPEC_12) {
468 struct nvm_addrf_12 *ppaf = (struct nvm_addrf_12 *)&geo->addrf;
470 l.g.ch = (r.ppa & ppaf->ch_mask) >> ppaf->ch_offset;
471 l.g.lun = (r.ppa & ppaf->lun_mask) >> ppaf->lun_offset;
472 l.g.blk = (r.ppa & ppaf->blk_mask) >> ppaf->blk_offset;
473 l.g.pg = (r.ppa & ppaf->pg_mask) >> ppaf->pg_offset;
474 l.g.pl = (r.ppa & ppaf->pln_mask) >> ppaf->pln_offset;
475 l.g.sec = (r.ppa & ppaf->sec_mask) >> ppaf->sec_offset;
477 struct nvm_addrf *lbaf = &geo->addrf;
479 l.m.grp = (r.ppa & lbaf->ch_mask) >> lbaf->ch_offset;
480 l.m.pu = (r.ppa & lbaf->lun_mask) >> lbaf->lun_offset;
481 l.m.chk = (r.ppa & lbaf->chk_mask) >> lbaf->chk_offset;
482 l.m.sec = (r.ppa & lbaf->sec_mask) >> lbaf->sec_offset;
488 typedef blk_qc_t (nvm_tgt_make_rq_fn)(struct request_queue *, struct bio *);
489 typedef sector_t (nvm_tgt_capacity_fn)(void *);
490 typedef void *(nvm_tgt_init_fn)(struct nvm_tgt_dev *, struct gendisk *,
492 typedef void (nvm_tgt_exit_fn)(void *);
493 typedef int (nvm_tgt_sysfs_init_fn)(struct gendisk *);
494 typedef void (nvm_tgt_sysfs_exit_fn)(struct gendisk *);
496 struct nvm_tgt_type {
498 unsigned int version[3];
500 /* target entry points */
501 nvm_tgt_make_rq_fn *make_rq;
502 nvm_tgt_capacity_fn *capacity;
504 /* module-specific init/teardown */
505 nvm_tgt_init_fn *init;
506 nvm_tgt_exit_fn *exit;
509 nvm_tgt_sysfs_init_fn *sysfs_init;
510 nvm_tgt_sysfs_exit_fn *sysfs_exit;
512 /* For internal use */
513 struct list_head list;
514 struct module *owner;
517 extern int nvm_register_tgt_type(struct nvm_tgt_type *);
518 extern void nvm_unregister_tgt_type(struct nvm_tgt_type *);
520 extern void *nvm_dev_dma_alloc(struct nvm_dev *, gfp_t, dma_addr_t *);
521 extern void nvm_dev_dma_free(struct nvm_dev *, void *, dma_addr_t);
523 extern struct nvm_dev *nvm_alloc_dev(int);
524 extern int nvm_register(struct nvm_dev *);
525 extern void nvm_unregister(struct nvm_dev *);
528 extern int nvm_get_chunk_meta(struct nvm_tgt_dev *tgt_dev,
529 struct nvm_chk_meta *meta, struct ppa_addr ppa,
532 extern int nvm_set_tgt_bb_tbl(struct nvm_tgt_dev *, struct ppa_addr *,
534 extern int nvm_submit_io(struct nvm_tgt_dev *, struct nvm_rq *);
535 extern int nvm_submit_io_sync(struct nvm_tgt_dev *, struct nvm_rq *);
536 extern void nvm_end_io(struct nvm_rq *);
537 extern int nvm_bb_tbl_fold(struct nvm_dev *, u8 *, int);
538 extern int nvm_get_tgt_bb_tbl(struct nvm_tgt_dev *, struct ppa_addr, u8 *);
540 #else /* CONFIG_NVM */
543 static inline struct nvm_dev *nvm_alloc_dev(int node)
545 return ERR_PTR(-EINVAL);
547 static inline int nvm_register(struct nvm_dev *dev)
551 static inline void nvm_unregister(struct nvm_dev *dev) {}
552 #endif /* CONFIG_NVM */
553 #endif /* LIGHTNVM.H */