]>
Commit | Line | Data |
---|---|---|
b2441318 | 1 | /* SPDX-License-Identifier: GPL-2.0 */ |
cd9e9808 MB |
2 | #ifndef NVM_H |
3 | #define NVM_H | |
4 | ||
b76eb20b | 5 | #include <linux/blkdev.h> |
a7fd9a4f | 6 | #include <linux/types.h> |
b76eb20b | 7 | #include <uapi/linux/lightnvm.h> |
a7fd9a4f | 8 | |
cd9e9808 MB |
9 | enum { |
10 | NVM_IO_OK = 0, | |
11 | NVM_IO_REQUEUE = 1, | |
12 | NVM_IO_DONE = 2, | |
13 | NVM_IO_ERR = 3, | |
14 | ||
15 | NVM_IOTYPE_NONE = 0, | |
16 | NVM_IOTYPE_GC = 1, | |
17 | }; | |
18 | ||
a7fd9a4f JA |
19 | #define NVM_BLK_BITS (16) |
20 | #define NVM_PG_BITS (16) | |
21 | #define NVM_SEC_BITS (8) | |
22 | #define NVM_PL_BITS (8) | |
23 | #define NVM_LUN_BITS (8) | |
df414b33 | 24 | #define NVM_CH_BITS (7) |
a7fd9a4f JA |
25 | |
26 | struct ppa_addr { | |
27 | /* Generic structure for all addresses */ | |
28 | union { | |
29 | struct { | |
30 | u64 blk : NVM_BLK_BITS; | |
31 | u64 pg : NVM_PG_BITS; | |
32 | u64 sec : NVM_SEC_BITS; | |
33 | u64 pl : NVM_PL_BITS; | |
34 | u64 lun : NVM_LUN_BITS; | |
35 | u64 ch : NVM_CH_BITS; | |
df414b33 | 36 | u64 reserved : 1; |
a7fd9a4f JA |
37 | } g; |
38 | ||
df414b33 MB |
39 | struct { |
40 | u64 line : 63; | |
41 | u64 is_cached : 1; | |
42 | } c; | |
43 | ||
a7fd9a4f JA |
44 | u64 ppa; |
45 | }; | |
46 | }; | |
47 | ||
48 | struct nvm_rq; | |
49 | struct nvm_id; | |
50 | struct nvm_dev; | |
8e53624d | 51 | struct nvm_tgt_dev; |
a7fd9a4f | 52 | |
a7fd9a4f | 53 | typedef int (nvm_id_fn)(struct nvm_dev *, struct nvm_id *); |
e11903f5 | 54 | typedef int (nvm_op_bb_tbl_fn)(struct nvm_dev *, struct ppa_addr, u8 *); |
00ee6cc3 | 55 | typedef int (nvm_op_set_bb_fn)(struct nvm_dev *, struct ppa_addr *, int, int); |
a7fd9a4f | 56 | typedef int (nvm_submit_io_fn)(struct nvm_dev *, struct nvm_rq *); |
1a94b2d4 | 57 | typedef int (nvm_submit_io_sync_fn)(struct nvm_dev *, struct nvm_rq *); |
a7fd9a4f JA |
58 | typedef void *(nvm_create_dma_pool_fn)(struct nvm_dev *, char *); |
59 | typedef void (nvm_destroy_dma_pool_fn)(void *); | |
60 | typedef void *(nvm_dev_dma_alloc_fn)(struct nvm_dev *, void *, gfp_t, | |
61 | dma_addr_t *); | |
62 | typedef void (nvm_dev_dma_free_fn)(void *, void*, dma_addr_t); | |
63 | ||
64 | struct nvm_dev_ops { | |
65 | nvm_id_fn *identity; | |
a7fd9a4f JA |
66 | nvm_op_bb_tbl_fn *get_bb_tbl; |
67 | nvm_op_set_bb_fn *set_bb_tbl; | |
68 | ||
69 | nvm_submit_io_fn *submit_io; | |
1a94b2d4 | 70 | nvm_submit_io_sync_fn *submit_io_sync; |
a7fd9a4f JA |
71 | |
72 | nvm_create_dma_pool_fn *create_dma_pool; | |
73 | nvm_destroy_dma_pool_fn *destroy_dma_pool; | |
74 | nvm_dev_dma_alloc_fn *dev_dma_alloc; | |
75 | nvm_dev_dma_free_fn *dev_dma_free; | |
a7fd9a4f JA |
76 | }; |
77 | ||
cd9e9808 MB |
78 | #ifdef CONFIG_NVM |
79 | ||
80 | #include <linux/blkdev.h> | |
cd9e9808 MB |
81 | #include <linux/file.h> |
82 | #include <linux/dmapool.h> | |
e3eb3799 | 83 | #include <uapi/linux/lightnvm.h> |
cd9e9808 MB |
84 | |
85 | enum { | |
86 | /* HW Responsibilities */ | |
87 | NVM_RSP_L2P = 1 << 0, | |
88 | NVM_RSP_ECC = 1 << 1, | |
89 | ||
90 | /* Physical Adressing Mode */ | |
91 | NVM_ADDRMODE_LINEAR = 0, | |
92 | NVM_ADDRMODE_CHANNEL = 1, | |
93 | ||
94 | /* Plane programming mode for LUN */ | |
d5bdec8d MB |
95 | NVM_PLANE_SINGLE = 1, |
96 | NVM_PLANE_DOUBLE = 2, | |
97 | NVM_PLANE_QUAD = 4, | |
cd9e9808 MB |
98 | |
99 | /* Status codes */ | |
100 | NVM_RSP_SUCCESS = 0x0, | |
101 | NVM_RSP_NOT_CHANGEABLE = 0x1, | |
102 | NVM_RSP_ERR_FAILWRITE = 0x40ff, | |
103 | NVM_RSP_ERR_EMPTYPAGE = 0x42ff, | |
402ab9a8 | 104 | NVM_RSP_ERR_FAILECC = 0x4281, |
38ea2f76 | 105 | NVM_RSP_ERR_FAILCRC = 0x4004, |
402ab9a8 | 106 | NVM_RSP_WARN_HIGHECC = 0x4700, |
cd9e9808 MB |
107 | |
108 | /* Device opcodes */ | |
cd9e9808 MB |
109 | NVM_OP_PWRITE = 0x91, |
110 | NVM_OP_PREAD = 0x92, | |
111 | NVM_OP_ERASE = 0x90, | |
112 | ||
113 | /* PPA Command Flags */ | |
114 | NVM_IO_SNGL_ACCESS = 0x0, | |
115 | NVM_IO_DUAL_ACCESS = 0x1, | |
116 | NVM_IO_QUAD_ACCESS = 0x2, | |
117 | ||
57b4bd06 | 118 | /* NAND Access Modes */ |
cd9e9808 MB |
119 | NVM_IO_SUSPEND = 0x80, |
120 | NVM_IO_SLC_MODE = 0x100, | |
a7737f39 | 121 | NVM_IO_SCRAMBLE_ENABLE = 0x200, |
57b4bd06 MB |
122 | |
123 | /* Block Types */ | |
124 | NVM_BLK_T_FREE = 0x0, | |
125 | NVM_BLK_T_BAD = 0x1, | |
b5d4acd4 MB |
126 | NVM_BLK_T_GRWN_BAD = 0x2, |
127 | NVM_BLK_T_DEV = 0x4, | |
128 | NVM_BLK_T_HOST = 0x8, | |
f9a99950 MB |
129 | |
130 | /* Memory capabilities */ | |
131 | NVM_ID_CAP_SLC = 0x1, | |
132 | NVM_ID_CAP_CMD_SUSPEND = 0x2, | |
133 | NVM_ID_CAP_SCRAMBLE = 0x4, | |
134 | NVM_ID_CAP_ENCRYPT = 0x8, | |
ca5927e7 MB |
135 | |
136 | /* Memory types */ | |
137 | NVM_ID_FMTYPE_SLC = 0, | |
138 | NVM_ID_FMTYPE_MLC = 1, | |
bf643185 MB |
139 | |
140 | /* Device capabilities */ | |
141 | NVM_ID_DCAP_BBLKMGMT = 0x1, | |
142 | NVM_UD_DCAP_ECC = 0x2, | |
ca5927e7 MB |
143 | }; |
144 | ||
145 | struct nvm_id_lp_mlc { | |
146 | u16 num_pairs; | |
147 | u8 pairs[886]; | |
148 | }; | |
149 | ||
150 | struct nvm_id_lp_tbl { | |
151 | __u8 id[8]; | |
152 | struct nvm_id_lp_mlc mlc; | |
cd9e9808 MB |
153 | }; |
154 | ||
c6ac3f35 MB |
155 | struct nvm_addr_format { |
156 | u8 ch_offset; | |
157 | u8 ch_len; | |
158 | u8 lun_offset; | |
159 | u8 lun_len; | |
160 | u8 pln_offset; | |
161 | u8 pln_len; | |
162 | u8 blk_offset; | |
163 | u8 blk_len; | |
164 | u8 pg_offset; | |
165 | u8 pg_len; | |
166 | u8 sect_offset; | |
167 | u8 sect_len; | |
168 | }; | |
169 | ||
170 | struct nvm_id { | |
171 | u8 ver_id; | |
172 | u8 vmnt; | |
173 | u32 cap; | |
174 | u32 dom; | |
175 | ||
176 | struct nvm_addr_format ppaf; | |
177 | ||
cd9e9808 MB |
178 | u8 num_ch; |
179 | u8 num_lun; | |
fae7fae4 MB |
180 | u16 num_chk; |
181 | u16 clba; | |
cd9e9808 MB |
182 | u16 csecs; |
183 | u16 sos; | |
fae7fae4 | 184 | |
62771fe0 MB |
185 | u32 ws_min; |
186 | u32 ws_opt; | |
187 | u32 mw_cunits; | |
fae7fae4 | 188 | |
cd9e9808 MB |
189 | u32 trdt; |
190 | u32 trdm; | |
191 | u32 tprt; | |
192 | u32 tprm; | |
193 | u32 tbet; | |
194 | u32 tbem; | |
195 | u32 mpos; | |
12be5edf | 196 | u32 mccap; |
cd9e9808 | 197 | u16 cpar; |
fae7fae4 | 198 | |
62771fe0 MB |
199 | /* calculated values */ |
200 | u16 ws_seq; | |
201 | u16 ws_per_chk; | |
202 | ||
fae7fae4 | 203 | /* 1.2 compatibility */ |
c6ac3f35 MB |
204 | u8 mtype; |
205 | u8 fmtype; | |
206 | ||
fae7fae4 MB |
207 | u8 num_pln; |
208 | u16 num_pg; | |
209 | u16 fpg_sz; | |
cd9e9808 MB |
210 | } __packed; |
211 | ||
212 | struct nvm_target { | |
213 | struct list_head list; | |
8e79b5cb | 214 | struct nvm_tgt_dev *dev; |
cd9e9808 MB |
215 | struct nvm_tgt_type *type; |
216 | struct gendisk *disk; | |
217 | }; | |
218 | ||
cd9e9808 MB |
219 | #define ADDR_EMPTY (~0ULL) |
220 | ||
e5392739 JG |
221 | #define NVM_TARGET_DEFAULT_OP (101) |
222 | #define NVM_TARGET_MIN_OP (3) | |
223 | #define NVM_TARGET_MAX_OP (80) | |
224 | ||
cd9e9808 MB |
225 | #define NVM_VERSION_MAJOR 1 |
226 | #define NVM_VERSION_MINOR 0 | |
227 | #define NVM_VERSION_PATCH 0 | |
228 | ||
89a09c56 MB |
229 | #define NVM_MAX_VLBA (64) /* max logical blocks in a vector command */ |
230 | ||
91276162 | 231 | struct nvm_rq; |
72d256ec | 232 | typedef void (nvm_end_io_fn)(struct nvm_rq *); |
91276162 | 233 | |
cd9e9808 | 234 | struct nvm_rq { |
8e53624d | 235 | struct nvm_tgt_dev *dev; |
cd9e9808 MB |
236 | |
237 | struct bio *bio; | |
238 | ||
239 | union { | |
240 | struct ppa_addr ppa_addr; | |
241 | dma_addr_t dma_ppa_list; | |
242 | }; | |
243 | ||
244 | struct ppa_addr *ppa_list; | |
245 | ||
003fad37 JG |
246 | void *meta_list; |
247 | dma_addr_t dma_meta_list; | |
cd9e9808 | 248 | |
91276162 MB |
249 | nvm_end_io_fn *end_io; |
250 | ||
cd9e9808 | 251 | uint8_t opcode; |
6d5be959 | 252 | uint16_t nr_ppas; |
cd9e9808 | 253 | uint16_t flags; |
72d256ec | 254 | |
9f867268 | 255 | u64 ppa_status; /* ppa media status */ |
72d256ec | 256 | int error; |
06894efe MB |
257 | |
258 | void *private; | |
cd9e9808 MB |
259 | }; |
260 | ||
261 | static inline struct nvm_rq *nvm_rq_from_pdu(void *pdu) | |
262 | { | |
263 | return pdu - sizeof(struct nvm_rq); | |
264 | } | |
265 | ||
266 | static inline void *nvm_rq_to_pdu(struct nvm_rq *rqdata) | |
267 | { | |
268 | return rqdata + 1; | |
269 | } | |
270 | ||
ff0e498b JG |
271 | enum { |
272 | NVM_BLK_ST_FREE = 0x1, /* Free block */ | |
077d2389 | 273 | NVM_BLK_ST_TGT = 0x2, /* Block in use by target */ |
ff0e498b | 274 | NVM_BLK_ST_BAD = 0x8, /* Bad block */ |
cd9e9808 MB |
275 | }; |
276 | ||
fae7fae4 | 277 | |
8e79b5cb JG |
278 | /* Device generic information */ |
279 | struct nvm_geo { | |
fae7fae4 | 280 | /* generic geometry */ |
cd9e9808 | 281 | int nr_chnls; |
fae7fae4 MB |
282 | int all_luns; /* across channels */ |
283 | int nr_luns; /* per channel */ | |
284 | int nr_chks; /* per lun */ | |
285 | ||
cd9e9808 MB |
286 | int sec_size; |
287 | int oob_size; | |
f9a99950 | 288 | int mccap; |
cd9e9808 | 289 | |
fae7fae4 MB |
290 | int sec_per_chk; |
291 | int sec_per_lun; | |
292 | ||
293 | int ws_min; | |
294 | int ws_opt; | |
295 | int ws_seq; | |
296 | int ws_per_chk; | |
297 | ||
e5392739 JG |
298 | int op; |
299 | ||
fae7fae4 MB |
300 | struct nvm_addr_format ppaf; |
301 | ||
302 | /* Legacy 1.2 specific geometry */ | |
303 | int plane_mode; /* drive device in single, double or quad mode */ | |
304 | int nr_planes; | |
305 | int sec_per_pg; /* only sectors for a single page */ | |
cd9e9808 | 306 | int sec_per_pl; /* all sectors across planes */ |
8e79b5cb JG |
307 | }; |
308 | ||
ade69e24 | 309 | /* sub-device structure */ |
8e79b5cb JG |
310 | struct nvm_tgt_dev { |
311 | /* Device information */ | |
312 | struct nvm_geo geo; | |
313 | ||
8e53624d JG |
314 | /* Base ppas for target LUNs */ |
315 | struct ppa_addr *luns; | |
316 | ||
8e79b5cb JG |
317 | sector_t total_secs; |
318 | ||
319 | struct nvm_id identity; | |
320 | struct request_queue *q; | |
321 | ||
959e911b | 322 | struct nvm_dev *parent; |
8e53624d | 323 | void *map; |
8e79b5cb JG |
324 | }; |
325 | ||
326 | struct nvm_dev { | |
327 | struct nvm_dev_ops *ops; | |
328 | ||
329 | struct list_head devices; | |
330 | ||
8e79b5cb JG |
331 | /* Device information */ |
332 | struct nvm_geo geo; | |
cd9e9808 | 333 | |
4ece44af | 334 | unsigned long total_secs; |
cd9e9808 | 335 | |
da1e2849 | 336 | unsigned long *lun_map; |
75b85649 | 337 | void *dma_pool; |
cd9e9808 MB |
338 | |
339 | struct nvm_id identity; | |
340 | ||
341 | /* Backend device */ | |
342 | struct request_queue *q; | |
343 | char name[DISK_NAME_LEN]; | |
40267efd | 344 | void *private_data; |
e3eb3799 | 345 | |
8e53624d JG |
346 | void *rmap; |
347 | ||
e3eb3799 | 348 | struct mutex mlock; |
4c9dacb8 | 349 | spinlock_t lock; |
ade69e24 MB |
350 | |
351 | /* target management */ | |
352 | struct list_head area_list; | |
353 | struct list_head targets; | |
cd9e9808 MB |
354 | }; |
355 | ||
dab8ee9e MB |
356 | static inline struct ppa_addr generic_to_dev_addr(struct nvm_tgt_dev *tgt_dev, |
357 | struct ppa_addr r) | |
cd9e9808 | 358 | { |
dab8ee9e | 359 | struct nvm_geo *geo = &tgt_dev->geo; |
cd9e9808 MB |
360 | struct ppa_addr l; |
361 | ||
8e79b5cb JG |
362 | l.ppa = ((u64)r.g.blk) << geo->ppaf.blk_offset; |
363 | l.ppa |= ((u64)r.g.pg) << geo->ppaf.pg_offset; | |
364 | l.ppa |= ((u64)r.g.sec) << geo->ppaf.sect_offset; | |
365 | l.ppa |= ((u64)r.g.pl) << geo->ppaf.pln_offset; | |
366 | l.ppa |= ((u64)r.g.lun) << geo->ppaf.lun_offset; | |
367 | l.ppa |= ((u64)r.g.ch) << geo->ppaf.ch_offset; | |
cd9e9808 MB |
368 | |
369 | return l; | |
370 | } | |
371 | ||
dab8ee9e MB |
372 | static inline struct ppa_addr dev_to_generic_addr(struct nvm_tgt_dev *tgt_dev, |
373 | struct ppa_addr r) | |
cd9e9808 | 374 | { |
dab8ee9e | 375 | struct nvm_geo *geo = &tgt_dev->geo; |
cd9e9808 MB |
376 | struct ppa_addr l; |
377 | ||
5389a1df | 378 | l.ppa = 0; |
7386af27 MB |
379 | /* |
380 | * (r.ppa << X offset) & X len bitmask. X eq. blk, pg, etc. | |
381 | */ | |
8e79b5cb JG |
382 | l.g.blk = (r.ppa >> geo->ppaf.blk_offset) & |
383 | (((1 << geo->ppaf.blk_len) - 1)); | |
384 | l.g.pg |= (r.ppa >> geo->ppaf.pg_offset) & | |
385 | (((1 << geo->ppaf.pg_len) - 1)); | |
386 | l.g.sec |= (r.ppa >> geo->ppaf.sect_offset) & | |
387 | (((1 << geo->ppaf.sect_len) - 1)); | |
388 | l.g.pl |= (r.ppa >> geo->ppaf.pln_offset) & | |
389 | (((1 << geo->ppaf.pln_len) - 1)); | |
390 | l.g.lun |= (r.ppa >> geo->ppaf.lun_offset) & | |
391 | (((1 << geo->ppaf.lun_len) - 1)); | |
392 | l.g.ch |= (r.ppa >> geo->ppaf.ch_offset) & | |
393 | (((1 << geo->ppaf.ch_len) - 1)); | |
cd9e9808 MB |
394 | |
395 | return l; | |
396 | } | |
397 | ||
dece1635 | 398 | typedef blk_qc_t (nvm_tgt_make_rq_fn)(struct request_queue *, struct bio *); |
cd9e9808 | 399 | typedef sector_t (nvm_tgt_capacity_fn)(void *); |
4af3f75d JG |
400 | typedef void *(nvm_tgt_init_fn)(struct nvm_tgt_dev *, struct gendisk *, |
401 | int flags); | |
cd9e9808 | 402 | typedef void (nvm_tgt_exit_fn)(void *); |
9a69b0ed JG |
403 | typedef int (nvm_tgt_sysfs_init_fn)(struct gendisk *); |
404 | typedef void (nvm_tgt_sysfs_exit_fn)(struct gendisk *); | |
cd9e9808 MB |
405 | |
406 | struct nvm_tgt_type { | |
407 | const char *name; | |
408 | unsigned int version[3]; | |
409 | ||
410 | /* target entry points */ | |
411 | nvm_tgt_make_rq_fn *make_rq; | |
412 | nvm_tgt_capacity_fn *capacity; | |
cd9e9808 MB |
413 | |
414 | /* module-specific init/teardown */ | |
415 | nvm_tgt_init_fn *init; | |
416 | nvm_tgt_exit_fn *exit; | |
417 | ||
9a69b0ed JG |
418 | /* sysfs */ |
419 | nvm_tgt_sysfs_init_fn *sysfs_init; | |
420 | nvm_tgt_sysfs_exit_fn *sysfs_exit; | |
421 | ||
cd9e9808 MB |
422 | /* For internal use */ |
423 | struct list_head list; | |
90014829 | 424 | struct module *owner; |
cd9e9808 MB |
425 | }; |
426 | ||
6063fe39 SL |
427 | extern int nvm_register_tgt_type(struct nvm_tgt_type *); |
428 | extern void nvm_unregister_tgt_type(struct nvm_tgt_type *); | |
cd9e9808 MB |
429 | |
430 | extern void *nvm_dev_dma_alloc(struct nvm_dev *, gfp_t, dma_addr_t *); | |
431 | extern void nvm_dev_dma_free(struct nvm_dev *, void *, dma_addr_t); | |
432 | ||
b0b4e09c MB |
433 | extern struct nvm_dev *nvm_alloc_dev(int); |
434 | extern int nvm_register(struct nvm_dev *); | |
435 | extern void nvm_unregister(struct nvm_dev *); | |
cd9e9808 | 436 | |
333ba053 JG |
437 | extern int nvm_set_tgt_bb_tbl(struct nvm_tgt_dev *, struct ppa_addr *, |
438 | int, int); | |
8e53624d | 439 | extern int nvm_submit_io(struct nvm_tgt_dev *, struct nvm_rq *); |
1a94b2d4 | 440 | extern int nvm_submit_io_sync(struct nvm_tgt_dev *, struct nvm_rq *); |
06894efe | 441 | extern void nvm_end_io(struct nvm_rq *); |
22e8c976 | 442 | extern int nvm_bb_tbl_fold(struct nvm_dev *, u8 *, int); |
333ba053 | 443 | extern int nvm_get_tgt_bb_tbl(struct nvm_tgt_dev *, struct ppa_addr, u8 *); |
e3eb3799 | 444 | |
cd9e9808 MB |
445 | #else /* CONFIG_NVM */ |
446 | struct nvm_dev_ops; | |
447 | ||
b0b4e09c MB |
448 | static inline struct nvm_dev *nvm_alloc_dev(int node) |
449 | { | |
450 | return ERR_PTR(-EINVAL); | |
451 | } | |
452 | static inline int nvm_register(struct nvm_dev *dev) | |
cd9e9808 MB |
453 | { |
454 | return -EINVAL; | |
455 | } | |
b0b4e09c | 456 | static inline void nvm_unregister(struct nvm_dev *dev) {} |
cd9e9808 MB |
457 | #endif /* CONFIG_NVM */ |
458 | #endif /* LIGHTNVM.H */ |