Commit | Line | Data |
---|---|---|
cd9e9808 MB |
1 | #ifndef NVM_H |
2 | #define NVM_H | |
3 | ||
4 | enum { | |
5 | NVM_IO_OK = 0, | |
6 | NVM_IO_REQUEUE = 1, | |
7 | NVM_IO_DONE = 2, | |
8 | NVM_IO_ERR = 3, | |
9 | ||
10 | NVM_IOTYPE_NONE = 0, | |
11 | NVM_IOTYPE_GC = 1, | |
12 | }; | |
13 | ||
14 | #ifdef CONFIG_NVM | |
15 | ||
16 | #include <linux/blkdev.h> | |
17 | #include <linux/types.h> | |
18 | #include <linux/file.h> | |
19 | #include <linux/dmapool.h> | |
20 | ||
21 | enum { | |
22 | /* HW Responsibilities */ | |
23 | NVM_RSP_L2P = 1 << 0, | |
24 | NVM_RSP_ECC = 1 << 1, | |
25 | ||
26 | /* Physical Adressing Mode */ | |
27 | NVM_ADDRMODE_LINEAR = 0, | |
28 | NVM_ADDRMODE_CHANNEL = 1, | |
29 | ||
30 | /* Plane programming mode for LUN */ | |
31 | NVM_PLANE_SINGLE = 0, | |
32 | NVM_PLANE_DOUBLE = 1, | |
33 | NVM_PLANE_QUAD = 2, | |
34 | ||
35 | /* Status codes */ | |
36 | NVM_RSP_SUCCESS = 0x0, | |
37 | NVM_RSP_NOT_CHANGEABLE = 0x1, | |
38 | NVM_RSP_ERR_FAILWRITE = 0x40ff, | |
39 | NVM_RSP_ERR_EMPTYPAGE = 0x42ff, | |
40 | ||
41 | /* Device opcodes */ | |
42 | NVM_OP_HBREAD = 0x02, | |
43 | NVM_OP_HBWRITE = 0x81, | |
44 | NVM_OP_PWRITE = 0x91, | |
45 | NVM_OP_PREAD = 0x92, | |
46 | NVM_OP_ERASE = 0x90, | |
47 | ||
48 | /* PPA Command Flags */ | |
49 | NVM_IO_SNGL_ACCESS = 0x0, | |
50 | NVM_IO_DUAL_ACCESS = 0x1, | |
51 | NVM_IO_QUAD_ACCESS = 0x2, | |
52 | ||
57b4bd06 | 53 | /* NAND Access Modes */ |
cd9e9808 MB |
54 | NVM_IO_SUSPEND = 0x80, |
55 | NVM_IO_SLC_MODE = 0x100, | |
56 | NVM_IO_SCRAMBLE_DISABLE = 0x200, | |
57b4bd06 MB |
57 | |
58 | /* Block Types */ | |
59 | NVM_BLK_T_FREE = 0x0, | |
60 | NVM_BLK_T_BAD = 0x1, | |
b5d4acd4 MB |
61 | NVM_BLK_T_GRWN_BAD = 0x2, |
62 | NVM_BLK_T_DEV = 0x4, | |
63 | NVM_BLK_T_HOST = 0x8, | |
cd9e9808 MB |
64 | }; |
65 | ||
66 | struct nvm_id_group { | |
67 | u8 mtype; | |
68 | u8 fmtype; | |
cd9e9808 MB |
69 | u8 num_ch; |
70 | u8 num_lun; | |
71 | u8 num_pln; | |
72 | u16 num_blk; | |
73 | u16 num_pg; | |
74 | u16 fpg_sz; | |
75 | u16 csecs; | |
76 | u16 sos; | |
77 | u32 trdt; | |
78 | u32 trdm; | |
79 | u32 tprt; | |
80 | u32 tprm; | |
81 | u32 tbet; | |
82 | u32 tbem; | |
83 | u32 mpos; | |
12be5edf | 84 | u32 mccap; |
cd9e9808 | 85 | u16 cpar; |
73387e7b | 86 | }; |
cd9e9808 MB |
87 | |
88 | struct nvm_addr_format { | |
89 | u8 ch_offset; | |
90 | u8 ch_len; | |
91 | u8 lun_offset; | |
92 | u8 lun_len; | |
93 | u8 pln_offset; | |
94 | u8 pln_len; | |
95 | u8 blk_offset; | |
96 | u8 blk_len; | |
97 | u8 pg_offset; | |
98 | u8 pg_len; | |
99 | u8 sect_offset; | |
100 | u8 sect_len; | |
cd9e9808 MB |
101 | }; |
102 | ||
103 | struct nvm_id { | |
104 | u8 ver_id; | |
105 | u8 vmnt; | |
106 | u8 cgrps; | |
cd9e9808 MB |
107 | u32 cap; |
108 | u32 dom; | |
109 | struct nvm_addr_format ppaf; | |
cd9e9808 MB |
110 | struct nvm_id_group groups[4]; |
111 | } __packed; | |
112 | ||
113 | struct nvm_target { | |
114 | struct list_head list; | |
115 | struct nvm_tgt_type *type; | |
116 | struct gendisk *disk; | |
117 | }; | |
118 | ||
119 | struct nvm_tgt_instance { | |
120 | struct nvm_tgt_type *tt; | |
121 | }; | |
122 | ||
123 | #define ADDR_EMPTY (~0ULL) | |
124 | ||
125 | #define NVM_VERSION_MAJOR 1 | |
126 | #define NVM_VERSION_MINOR 0 | |
127 | #define NVM_VERSION_PATCH 0 | |
128 | ||
cd9e9808 | 129 | #define NVM_BLK_BITS (16) |
7386af27 MB |
130 | #define NVM_PG_BITS (16) |
131 | #define NVM_SEC_BITS (8) | |
132 | #define NVM_PL_BITS (8) | |
133 | #define NVM_LUN_BITS (8) | |
cd9e9808 MB |
134 | #define NVM_CH_BITS (8) |
135 | ||
136 | struct ppa_addr { | |
7386af27 | 137 | /* Generic structure for all addresses */ |
cd9e9808 | 138 | union { |
cd9e9808 | 139 | struct { |
7386af27 MB |
140 | u64 blk : NVM_BLK_BITS; |
141 | u64 pg : NVM_PG_BITS; | |
b7ceb7d5 MB |
142 | u64 sec : NVM_SEC_BITS; |
143 | u64 pl : NVM_PL_BITS; | |
b7ceb7d5 MB |
144 | u64 lun : NVM_LUN_BITS; |
145 | u64 ch : NVM_CH_BITS; | |
cd9e9808 MB |
146 | } g; |
147 | ||
b7ceb7d5 | 148 | u64 ppa; |
cd9e9808 | 149 | }; |
7386af27 | 150 | }; |
cd9e9808 | 151 | |
91276162 | 152 | struct nvm_rq; |
72d256ec | 153 | typedef void (nvm_end_io_fn)(struct nvm_rq *); |
91276162 | 154 | |
cd9e9808 MB |
155 | struct nvm_rq { |
156 | struct nvm_tgt_instance *ins; | |
157 | struct nvm_dev *dev; | |
158 | ||
159 | struct bio *bio; | |
160 | ||
161 | union { | |
162 | struct ppa_addr ppa_addr; | |
163 | dma_addr_t dma_ppa_list; | |
164 | }; | |
165 | ||
166 | struct ppa_addr *ppa_list; | |
167 | ||
168 | void *metadata; | |
169 | dma_addr_t dma_metadata; | |
170 | ||
91276162 MB |
171 | struct completion *wait; |
172 | nvm_end_io_fn *end_io; | |
173 | ||
cd9e9808 MB |
174 | uint8_t opcode; |
175 | uint16_t nr_pages; | |
176 | uint16_t flags; | |
72d256ec MB |
177 | |
178 | int error; | |
cd9e9808 MB |
179 | }; |
180 | ||
181 | static inline struct nvm_rq *nvm_rq_from_pdu(void *pdu) | |
182 | { | |
183 | return pdu - sizeof(struct nvm_rq); | |
184 | } | |
185 | ||
186 | static inline void *nvm_rq_to_pdu(struct nvm_rq *rqdata) | |
187 | { | |
188 | return rqdata + 1; | |
189 | } | |
190 | ||
191 | struct nvm_block; | |
192 | ||
193 | typedef int (nvm_l2p_update_fn)(u64, u32, __le64 *, void *); | |
11450469 | 194 | typedef int (nvm_bb_update_fn)(struct ppa_addr, int, u8 *, void *); |
16f26c3a MB |
195 | typedef int (nvm_id_fn)(struct nvm_dev *, struct nvm_id *); |
196 | typedef int (nvm_get_l2p_tbl_fn)(struct nvm_dev *, u64, u32, | |
cd9e9808 | 197 | nvm_l2p_update_fn *, void *); |
08236c6b | 198 | typedef int (nvm_op_bb_tbl_fn)(struct nvm_dev *, struct ppa_addr, int, |
cd9e9808 | 199 | nvm_bb_update_fn *, void *); |
16f26c3a MB |
200 | typedef int (nvm_op_set_bb_fn)(struct nvm_dev *, struct nvm_rq *, int); |
201 | typedef int (nvm_submit_io_fn)(struct nvm_dev *, struct nvm_rq *); | |
202 | typedef int (nvm_erase_blk_fn)(struct nvm_dev *, struct nvm_rq *); | |
203 | typedef void *(nvm_create_dma_pool_fn)(struct nvm_dev *, char *); | |
cd9e9808 | 204 | typedef void (nvm_destroy_dma_pool_fn)(void *); |
16f26c3a | 205 | typedef void *(nvm_dev_dma_alloc_fn)(struct nvm_dev *, void *, gfp_t, |
cd9e9808 MB |
206 | dma_addr_t *); |
207 | typedef void (nvm_dev_dma_free_fn)(void *, void*, dma_addr_t); | |
208 | ||
209 | struct nvm_dev_ops { | |
210 | nvm_id_fn *identity; | |
211 | nvm_get_l2p_tbl_fn *get_l2p_tbl; | |
212 | nvm_op_bb_tbl_fn *get_bb_tbl; | |
11450469 | 213 | nvm_op_set_bb_fn *set_bb_tbl; |
cd9e9808 MB |
214 | |
215 | nvm_submit_io_fn *submit_io; | |
216 | nvm_erase_blk_fn *erase_block; | |
217 | ||
218 | nvm_create_dma_pool_fn *create_dma_pool; | |
219 | nvm_destroy_dma_pool_fn *destroy_dma_pool; | |
220 | nvm_dev_dma_alloc_fn *dev_dma_alloc; | |
221 | nvm_dev_dma_free_fn *dev_dma_free; | |
222 | ||
aedf17f4 | 223 | unsigned int max_phys_sect; |
cd9e9808 MB |
224 | }; |
225 | ||
226 | struct nvm_lun { | |
227 | int id; | |
228 | ||
229 | int lun_id; | |
230 | int chnl_id; | |
231 | ||
ff0e498b JG |
232 | /* It is up to the target to mark blocks as closed. If the target does |
233 | * not do it, all blocks are marked as open, and nr_open_blocks | |
234 | * represents the number of blocks in use | |
235 | */ | |
236 | unsigned int nr_open_blocks; /* Number of used, writable blocks */ | |
237 | unsigned int nr_closed_blocks; /* Number of used, read-only blocks */ | |
cd9e9808 | 238 | unsigned int nr_free_blocks; /* Number of unused blocks */ |
0b59733b | 239 | unsigned int nr_bad_blocks; /* Number of bad blocks */ |
cd9e9808 MB |
240 | |
241 | spinlock_t lock; | |
ff0e498b JG |
242 | |
243 | struct nvm_block *blocks; | |
244 | }; | |
245 | ||
246 | enum { | |
247 | NVM_BLK_ST_FREE = 0x1, /* Free block */ | |
248 | NVM_BLK_ST_OPEN = 0x2, /* Open block - read-write */ | |
249 | NVM_BLK_ST_CLOSED = 0x4, /* Closed block - read-only */ | |
250 | NVM_BLK_ST_BAD = 0x8, /* Bad block */ | |
cd9e9808 MB |
251 | }; |
252 | ||
253 | struct nvm_block { | |
254 | struct list_head list; | |
255 | struct nvm_lun *lun; | |
256 | unsigned long id; | |
257 | ||
258 | void *priv; | |
ff0e498b | 259 | int state; |
cd9e9808 MB |
260 | }; |
261 | ||
262 | struct nvm_dev { | |
263 | struct nvm_dev_ops *ops; | |
264 | ||
265 | struct list_head devices; | |
266 | struct list_head online_targets; | |
267 | ||
268 | /* Media manager */ | |
269 | struct nvmm_type *mt; | |
270 | void *mp; | |
271 | ||
272 | /* Device information */ | |
273 | int nr_chnls; | |
274 | int nr_planes; | |
275 | int luns_per_chnl; | |
276 | int sec_per_pg; /* only sectors for a single page */ | |
277 | int pgs_per_blk; | |
278 | int blks_per_lun; | |
279 | int sec_size; | |
280 | int oob_size; | |
7386af27 | 281 | struct nvm_addr_format ppaf; |
cd9e9808 MB |
282 | |
283 | /* Calculated/Cached values. These do not reflect the actual usable | |
284 | * blocks at run-time. | |
285 | */ | |
286 | int max_rq_size; | |
287 | int plane_mode; /* drive device in single, double or quad mode */ | |
288 | ||
289 | int sec_per_pl; /* all sectors across planes */ | |
290 | int sec_per_blk; | |
291 | int sec_per_lun; | |
292 | ||
293 | unsigned long total_pages; | |
294 | unsigned long total_blocks; | |
295 | int nr_luns; | |
296 | unsigned max_pages_per_blk; | |
297 | ||
298 | void *ppalist_pool; | |
299 | ||
300 | struct nvm_id identity; | |
301 | ||
302 | /* Backend device */ | |
303 | struct request_queue *q; | |
304 | char name[DISK_NAME_LEN]; | |
305 | }; | |
306 | ||
7386af27 MB |
307 | static inline struct ppa_addr generic_to_dev_addr(struct nvm_dev *dev, |
308 | struct ppa_addr r) | |
cd9e9808 MB |
309 | { |
310 | struct ppa_addr l; | |
311 | ||
7386af27 MB |
312 | l.ppa = ((u64)r.g.blk) << dev->ppaf.blk_offset; |
313 | l.ppa |= ((u64)r.g.pg) << dev->ppaf.pg_offset; | |
314 | l.ppa |= ((u64)r.g.sec) << dev->ppaf.sect_offset; | |
315 | l.ppa |= ((u64)r.g.pl) << dev->ppaf.pln_offset; | |
316 | l.ppa |= ((u64)r.g.lun) << dev->ppaf.lun_offset; | |
317 | l.ppa |= ((u64)r.g.ch) << dev->ppaf.ch_offset; | |
cd9e9808 MB |
318 | |
319 | return l; | |
320 | } | |
321 | ||
7386af27 MB |
322 | static inline struct ppa_addr dev_to_generic_addr(struct nvm_dev *dev, |
323 | struct ppa_addr r) | |
cd9e9808 MB |
324 | { |
325 | struct ppa_addr l; | |
326 | ||
7386af27 MB |
327 | /* |
328 | * (r.ppa << X offset) & X len bitmask. X eq. blk, pg, etc. | |
329 | */ | |
330 | l.g.blk = (r.ppa >> dev->ppaf.blk_offset) & | |
331 | (((1 << dev->ppaf.blk_len) - 1)); | |
332 | l.g.pg |= (r.ppa >> dev->ppaf.pg_offset) & | |
333 | (((1 << dev->ppaf.pg_len) - 1)); | |
334 | l.g.sec |= (r.ppa >> dev->ppaf.sect_offset) & | |
335 | (((1 << dev->ppaf.sect_len) - 1)); | |
336 | l.g.pl |= (r.ppa >> dev->ppaf.pln_offset) & | |
337 | (((1 << dev->ppaf.pln_len) - 1)); | |
338 | l.g.lun |= (r.ppa >> dev->ppaf.lun_offset) & | |
339 | (((1 << dev->ppaf.lun_len) - 1)); | |
340 | l.g.ch |= (r.ppa >> dev->ppaf.ch_offset) & | |
341 | (((1 << dev->ppaf.ch_len) - 1)); | |
cd9e9808 MB |
342 | |
343 | return l; | |
344 | } | |
345 | ||
cd9e9808 MB |
346 | static inline int ppa_empty(struct ppa_addr ppa_addr) |
347 | { | |
348 | return (ppa_addr.ppa == ADDR_EMPTY); | |
349 | } | |
350 | ||
351 | static inline void ppa_set_empty(struct ppa_addr *ppa_addr) | |
352 | { | |
353 | ppa_addr->ppa = ADDR_EMPTY; | |
354 | } | |
355 | ||
356 | static inline struct ppa_addr block_to_ppa(struct nvm_dev *dev, | |
357 | struct nvm_block *blk) | |
358 | { | |
359 | struct ppa_addr ppa; | |
360 | struct nvm_lun *lun = blk->lun; | |
361 | ||
362 | ppa.ppa = 0; | |
363 | ppa.g.blk = blk->id % dev->blks_per_lun; | |
364 | ppa.g.lun = lun->lun_id; | |
365 | ppa.g.ch = lun->chnl_id; | |
366 | ||
367 | return ppa; | |
368 | } | |
369 | ||
dece1635 | 370 | typedef blk_qc_t (nvm_tgt_make_rq_fn)(struct request_queue *, struct bio *); |
cd9e9808 | 371 | typedef sector_t (nvm_tgt_capacity_fn)(void *); |
cd9e9808 MB |
372 | typedef void *(nvm_tgt_init_fn)(struct nvm_dev *, struct gendisk *, int, int); |
373 | typedef void (nvm_tgt_exit_fn)(void *); | |
374 | ||
375 | struct nvm_tgt_type { | |
376 | const char *name; | |
377 | unsigned int version[3]; | |
378 | ||
379 | /* target entry points */ | |
380 | nvm_tgt_make_rq_fn *make_rq; | |
381 | nvm_tgt_capacity_fn *capacity; | |
91276162 | 382 | nvm_end_io_fn *end_io; |
cd9e9808 MB |
383 | |
384 | /* module-specific init/teardown */ | |
385 | nvm_tgt_init_fn *init; | |
386 | nvm_tgt_exit_fn *exit; | |
387 | ||
388 | /* For internal use */ | |
389 | struct list_head list; | |
390 | }; | |
391 | ||
392 | extern int nvm_register_target(struct nvm_tgt_type *); | |
393 | extern void nvm_unregister_target(struct nvm_tgt_type *); | |
394 | ||
395 | extern void *nvm_dev_dma_alloc(struct nvm_dev *, gfp_t, dma_addr_t *); | |
396 | extern void nvm_dev_dma_free(struct nvm_dev *, void *, dma_addr_t); | |
397 | ||
398 | typedef int (nvmm_register_fn)(struct nvm_dev *); | |
399 | typedef void (nvmm_unregister_fn)(struct nvm_dev *); | |
400 | typedef struct nvm_block *(nvmm_get_blk_fn)(struct nvm_dev *, | |
401 | struct nvm_lun *, unsigned long); | |
402 | typedef void (nvmm_put_blk_fn)(struct nvm_dev *, struct nvm_block *); | |
403 | typedef int (nvmm_open_blk_fn)(struct nvm_dev *, struct nvm_block *); | |
404 | typedef int (nvmm_close_blk_fn)(struct nvm_dev *, struct nvm_block *); | |
405 | typedef void (nvmm_flush_blk_fn)(struct nvm_dev *, struct nvm_block *); | |
406 | typedef int (nvmm_submit_io_fn)(struct nvm_dev *, struct nvm_rq *); | |
cd9e9808 MB |
407 | typedef int (nvmm_erase_blk_fn)(struct nvm_dev *, struct nvm_block *, |
408 | unsigned long); | |
409 | typedef struct nvm_lun *(nvmm_get_lun_fn)(struct nvm_dev *, int); | |
2fde0e48 | 410 | typedef void (nvmm_lun_info_print_fn)(struct nvm_dev *); |
cd9e9808 MB |
411 | |
412 | struct nvmm_type { | |
413 | const char *name; | |
414 | unsigned int version[3]; | |
415 | ||
416 | nvmm_register_fn *register_mgr; | |
417 | nvmm_unregister_fn *unregister_mgr; | |
418 | ||
419 | /* Block administration callbacks */ | |
ff0e498b JG |
420 | nvmm_get_blk_fn *get_blk_unlocked; |
421 | nvmm_put_blk_fn *put_blk_unlocked; | |
cd9e9808 MB |
422 | nvmm_get_blk_fn *get_blk; |
423 | nvmm_put_blk_fn *put_blk; | |
424 | nvmm_open_blk_fn *open_blk; | |
425 | nvmm_close_blk_fn *close_blk; | |
426 | nvmm_flush_blk_fn *flush_blk; | |
427 | ||
428 | nvmm_submit_io_fn *submit_io; | |
cd9e9808 MB |
429 | nvmm_erase_blk_fn *erase_blk; |
430 | ||
431 | /* Configuration management */ | |
432 | nvmm_get_lun_fn *get_lun; | |
433 | ||
434 | /* Statistics */ | |
2fde0e48 | 435 | nvmm_lun_info_print_fn *lun_info_print; |
cd9e9808 MB |
436 | struct list_head list; |
437 | }; | |
438 | ||
439 | extern int nvm_register_mgr(struct nvmm_type *); | |
440 | extern void nvm_unregister_mgr(struct nvmm_type *); | |
441 | ||
ff0e498b JG |
442 | extern struct nvm_block *nvm_get_blk_unlocked(struct nvm_dev *, |
443 | struct nvm_lun *, unsigned long); | |
444 | extern void nvm_put_blk_unlocked(struct nvm_dev *, struct nvm_block *); | |
445 | ||
cd9e9808 MB |
446 | extern struct nvm_block *nvm_get_blk(struct nvm_dev *, struct nvm_lun *, |
447 | unsigned long); | |
448 | extern void nvm_put_blk(struct nvm_dev *, struct nvm_block *); | |
449 | ||
450 | extern int nvm_register(struct request_queue *, char *, | |
451 | struct nvm_dev_ops *); | |
452 | extern void nvm_unregister(char *); | |
453 | ||
454 | extern int nvm_submit_io(struct nvm_dev *, struct nvm_rq *); | |
069368e9 MB |
455 | extern void nvm_generic_to_addr_mode(struct nvm_dev *, struct nvm_rq *); |
456 | extern void nvm_addr_to_generic_mode(struct nvm_dev *, struct nvm_rq *); | |
abd805ec MB |
457 | extern int nvm_set_rqd_ppalist(struct nvm_dev *, struct nvm_rq *, |
458 | struct ppa_addr *, int); | |
459 | extern void nvm_free_rqd_ppalist(struct nvm_dev *, struct nvm_rq *); | |
81e681d3 | 460 | extern int nvm_erase_ppa(struct nvm_dev *, struct ppa_addr *, int); |
cd9e9808 | 461 | extern int nvm_erase_blk(struct nvm_dev *, struct nvm_block *); |
91276162 | 462 | extern void nvm_end_io(struct nvm_rq *, int); |
09719b62 MB |
463 | extern int nvm_submit_ppa(struct nvm_dev *, struct ppa_addr *, int, int, int, |
464 | void *, int); | |
cd9e9808 MB |
465 | #else /* CONFIG_NVM */ |
466 | struct nvm_dev_ops; | |
467 | ||
468 | static inline int nvm_register(struct request_queue *q, char *disk_name, | |
469 | struct nvm_dev_ops *ops) | |
470 | { | |
471 | return -EINVAL; | |
472 | } | |
473 | static inline void nvm_unregister(char *disk_name) {} | |
474 | #endif /* CONFIG_NVM */ | |
475 | #endif /* LIGHTNVM.H */ |