1
2#ifndef NVM_H
3#define NVM_H
4
5#include <linux/blkdev.h>
6#include <linux/types.h>
7#include <uapi/linux/lightnvm.h>
8
9enum {
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
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)
24#define NVM_CH_BITS (7)
25
26struct ppa_addr {
27
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;
36 u64 reserved : 1;
37 } g;
38
39 struct {
40 u64 line : 63;
41 u64 is_cached : 1;
42 } c;
43
44 u64 ppa;
45 };
46};
47
48struct nvm_rq;
49struct nvm_id;
50struct nvm_dev;
51struct nvm_tgt_dev;
52
53typedef int (nvm_l2p_update_fn)(u64, u32, __le64 *, void *);
54typedef int (nvm_id_fn)(struct nvm_dev *, struct nvm_id *);
55typedef int (nvm_get_l2p_tbl_fn)(struct nvm_dev *, u64, u32,
56 nvm_l2p_update_fn *, void *);
57typedef int (nvm_op_bb_tbl_fn)(struct nvm_dev *, struct ppa_addr, u8 *);
58typedef int (nvm_op_set_bb_fn)(struct nvm_dev *, struct ppa_addr *, int, int);
59typedef int (nvm_submit_io_fn)(struct nvm_dev *, struct nvm_rq *);
60typedef void *(nvm_create_dma_pool_fn)(struct nvm_dev *, char *);
61typedef void (nvm_destroy_dma_pool_fn)(void *);
62typedef void *(nvm_dev_dma_alloc_fn)(struct nvm_dev *, void *, gfp_t,
63 dma_addr_t *);
64typedef void (nvm_dev_dma_free_fn)(void *, void*, dma_addr_t);
65
66struct nvm_dev_ops {
67 nvm_id_fn *identity;
68 nvm_get_l2p_tbl_fn *get_l2p_tbl;
69 nvm_op_bb_tbl_fn *get_bb_tbl;
70 nvm_op_set_bb_fn *set_bb_tbl;
71
72 nvm_submit_io_fn *submit_io;
73
74 nvm_create_dma_pool_fn *create_dma_pool;
75 nvm_destroy_dma_pool_fn *destroy_dma_pool;
76 nvm_dev_dma_alloc_fn *dev_dma_alloc;
77 nvm_dev_dma_free_fn *dev_dma_free;
78
79 unsigned int max_phys_sect;
80};
81
82#ifdef CONFIG_NVM
83
84#include <linux/blkdev.h>
85#include <linux/file.h>
86#include <linux/dmapool.h>
87#include <uapi/linux/lightnvm.h>
88
89enum {
90
91 NVM_RSP_L2P = 1 << 0,
92 NVM_RSP_ECC = 1 << 1,
93
94
95 NVM_ADDRMODE_LINEAR = 0,
96 NVM_ADDRMODE_CHANNEL = 1,
97
98
99 NVM_PLANE_SINGLE = 1,
100 NVM_PLANE_DOUBLE = 2,
101 NVM_PLANE_QUAD = 4,
102
103
104 NVM_RSP_SUCCESS = 0x0,
105 NVM_RSP_NOT_CHANGEABLE = 0x1,
106 NVM_RSP_ERR_FAILWRITE = 0x40ff,
107 NVM_RSP_ERR_EMPTYPAGE = 0x42ff,
108 NVM_RSP_ERR_FAILECC = 0x4281,
109 NVM_RSP_ERR_FAILCRC = 0x4004,
110 NVM_RSP_WARN_HIGHECC = 0x4700,
111
112
113 NVM_OP_HBREAD = 0x02,
114 NVM_OP_HBWRITE = 0x81,
115 NVM_OP_PWRITE = 0x91,
116 NVM_OP_PREAD = 0x92,
117 NVM_OP_ERASE = 0x90,
118
119
120 NVM_IO_SNGL_ACCESS = 0x0,
121 NVM_IO_DUAL_ACCESS = 0x1,
122 NVM_IO_QUAD_ACCESS = 0x2,
123
124
125 NVM_IO_SUSPEND = 0x80,
126 NVM_IO_SLC_MODE = 0x100,
127 NVM_IO_SCRAMBLE_ENABLE = 0x200,
128
129
130 NVM_BLK_T_FREE = 0x0,
131 NVM_BLK_T_BAD = 0x1,
132 NVM_BLK_T_GRWN_BAD = 0x2,
133 NVM_BLK_T_DEV = 0x4,
134 NVM_BLK_T_HOST = 0x8,
135
136
137 NVM_ID_CAP_SLC = 0x1,
138 NVM_ID_CAP_CMD_SUSPEND = 0x2,
139 NVM_ID_CAP_SCRAMBLE = 0x4,
140 NVM_ID_CAP_ENCRYPT = 0x8,
141
142
143 NVM_ID_FMTYPE_SLC = 0,
144 NVM_ID_FMTYPE_MLC = 1,
145
146
147 NVM_ID_DCAP_BBLKMGMT = 0x1,
148 NVM_UD_DCAP_ECC = 0x2,
149};
150
151struct nvm_id_lp_mlc {
152 u16 num_pairs;
153 u8 pairs[886];
154};
155
156struct nvm_id_lp_tbl {
157 __u8 id[8];
158 struct nvm_id_lp_mlc mlc;
159};
160
161struct nvm_id_group {
162 u8 mtype;
163 u8 fmtype;
164 u8 num_ch;
165 u8 num_lun;
166 u8 num_pln;
167 u16 num_blk;
168 u16 num_pg;
169 u16 fpg_sz;
170 u16 csecs;
171 u16 sos;
172 u32 trdt;
173 u32 trdm;
174 u32 tprt;
175 u32 tprm;
176 u32 tbet;
177 u32 tbem;
178 u32 mpos;
179 u32 mccap;
180 u16 cpar;
181
182 struct nvm_id_lp_tbl lptbl;
183};
184
185struct nvm_addr_format {
186 u8 ch_offset;
187 u8 ch_len;
188 u8 lun_offset;
189 u8 lun_len;
190 u8 pln_offset;
191 u8 pln_len;
192 u8 blk_offset;
193 u8 blk_len;
194 u8 pg_offset;
195 u8 pg_len;
196 u8 sect_offset;
197 u8 sect_len;
198};
199
200struct nvm_id {
201 u8 ver_id;
202 u8 vmnt;
203 u32 cap;
204 u32 dom;
205 struct nvm_addr_format ppaf;
206 struct nvm_id_group grp;
207} __packed;
208
209struct nvm_target {
210 struct list_head list;
211 struct nvm_tgt_dev *dev;
212 struct nvm_tgt_type *type;
213 struct gendisk *disk;
214};
215
216#define ADDR_EMPTY (~0ULL)
217
218#define NVM_VERSION_MAJOR 1
219#define NVM_VERSION_MINOR 0
220#define NVM_VERSION_PATCH 0
221
222struct nvm_rq;
223typedef void (nvm_end_io_fn)(struct nvm_rq *);
224
225struct nvm_rq {
226 struct nvm_tgt_dev *dev;
227
228 struct bio *bio;
229
230 union {
231 struct ppa_addr ppa_addr;
232 dma_addr_t dma_ppa_list;
233 };
234
235 struct ppa_addr *ppa_list;
236
237 void *meta_list;
238 dma_addr_t dma_meta_list;
239
240 struct completion *wait;
241 nvm_end_io_fn *end_io;
242
243 uint8_t opcode;
244 uint16_t nr_ppas;
245 uint16_t flags;
246
247 u64 ppa_status;
248 int error;
249
250 void *private;
251};
252
253static inline struct nvm_rq *nvm_rq_from_pdu(void *pdu)
254{
255 return pdu - sizeof(struct nvm_rq);
256}
257
258static inline void *nvm_rq_to_pdu(struct nvm_rq *rqdata)
259{
260 return rqdata + 1;
261}
262
263enum {
264 NVM_BLK_ST_FREE = 0x1,
265 NVM_BLK_ST_TGT = 0x2,
266 NVM_BLK_ST_BAD = 0x8,
267};
268
269
270struct nvm_geo {
271 int nr_chnls;
272 int nr_luns;
273 int luns_per_chnl;
274 int nr_planes;
275 int sec_per_pg;
276 int pgs_per_blk;
277 int blks_per_lun;
278 int fpg_size;
279 int pfpg_size;
280 int sec_size;
281 int oob_size;
282 int mccap;
283 struct nvm_addr_format ppaf;
284
285
286
287
288 int max_rq_size;
289 int plane_mode;
290
291 int sec_per_pl;
292 int sec_per_blk;
293 int sec_per_lun;
294};
295
296
297struct nvm_tgt_dev {
298
299 struct nvm_geo geo;
300
301
302 struct ppa_addr *luns;
303
304 sector_t total_secs;
305
306 struct nvm_id identity;
307 struct request_queue *q;
308
309 struct nvm_dev *parent;
310 void *map;
311};
312
313struct nvm_dev {
314 struct nvm_dev_ops *ops;
315
316 struct list_head devices;
317
318
319 struct nvm_geo geo;
320
321
322 int lps_per_blk;
323 int *lptbl;
324
325 unsigned long total_secs;
326
327 unsigned long *lun_map;
328 void *dma_pool;
329
330 struct nvm_id identity;
331
332
333 struct request_queue *q;
334 char name[DISK_NAME_LEN];
335 void *private_data;
336
337 void *rmap;
338
339 struct mutex mlock;
340 spinlock_t lock;
341
342
343 struct list_head area_list;
344 struct list_head targets;
345};
346
347static inline struct ppa_addr linear_to_generic_addr(struct nvm_geo *geo,
348 u64 pba)
349{
350 struct ppa_addr l;
351 int secs, pgs, blks, luns;
352 sector_t ppa = pba;
353
354 l.ppa = 0;
355
356 div_u64_rem(ppa, geo->sec_per_pg, &secs);
357 l.g.sec = secs;
358
359 sector_div(ppa, geo->sec_per_pg);
360 div_u64_rem(ppa, geo->pgs_per_blk, &pgs);
361 l.g.pg = pgs;
362
363 sector_div(ppa, geo->pgs_per_blk);
364 div_u64_rem(ppa, geo->blks_per_lun, &blks);
365 l.g.blk = blks;
366
367 sector_div(ppa, geo->blks_per_lun);
368 div_u64_rem(ppa, geo->luns_per_chnl, &luns);
369 l.g.lun = luns;
370
371 sector_div(ppa, geo->luns_per_chnl);
372 l.g.ch = ppa;
373
374 return l;
375}
376
377static inline struct ppa_addr generic_to_dev_addr(struct nvm_tgt_dev *tgt_dev,
378 struct ppa_addr r)
379{
380 struct nvm_geo *geo = &tgt_dev->geo;
381 struct ppa_addr l;
382
383 l.ppa = ((u64)r.g.blk) << geo->ppaf.blk_offset;
384 l.ppa |= ((u64)r.g.pg) << geo->ppaf.pg_offset;
385 l.ppa |= ((u64)r.g.sec) << geo->ppaf.sect_offset;
386 l.ppa |= ((u64)r.g.pl) << geo->ppaf.pln_offset;
387 l.ppa |= ((u64)r.g.lun) << geo->ppaf.lun_offset;
388 l.ppa |= ((u64)r.g.ch) << geo->ppaf.ch_offset;
389
390 return l;
391}
392
393static inline struct ppa_addr dev_to_generic_addr(struct nvm_tgt_dev *tgt_dev,
394 struct ppa_addr r)
395{
396 struct nvm_geo *geo = &tgt_dev->geo;
397 struct ppa_addr l;
398
399 l.ppa = 0;
400
401
402
403 l.g.blk = (r.ppa >> geo->ppaf.blk_offset) &
404 (((1 << geo->ppaf.blk_len) - 1));
405 l.g.pg |= (r.ppa >> geo->ppaf.pg_offset) &
406 (((1 << geo->ppaf.pg_len) - 1));
407 l.g.sec |= (r.ppa >> geo->ppaf.sect_offset) &
408 (((1 << geo->ppaf.sect_len) - 1));
409 l.g.pl |= (r.ppa >> geo->ppaf.pln_offset) &
410 (((1 << geo->ppaf.pln_len) - 1));
411 l.g.lun |= (r.ppa >> geo->ppaf.lun_offset) &
412 (((1 << geo->ppaf.lun_len) - 1));
413 l.g.ch |= (r.ppa >> geo->ppaf.ch_offset) &
414 (((1 << geo->ppaf.ch_len) - 1));
415
416 return l;
417}
418
419static inline int ppa_empty(struct ppa_addr ppa_addr)
420{
421 return (ppa_addr.ppa == ADDR_EMPTY);
422}
423
424static inline void ppa_set_empty(struct ppa_addr *ppa_addr)
425{
426 ppa_addr->ppa = ADDR_EMPTY;
427}
428
429static inline int ppa_cmp_blk(struct ppa_addr ppa1, struct ppa_addr ppa2)
430{
431 if (ppa_empty(ppa1) || ppa_empty(ppa2))
432 return 0;
433
434 return ((ppa1.g.ch == ppa2.g.ch) && (ppa1.g.lun == ppa2.g.lun) &&
435 (ppa1.g.blk == ppa2.g.blk));
436}
437
438typedef blk_qc_t (nvm_tgt_make_rq_fn)(struct request_queue *, struct bio *);
439typedef sector_t (nvm_tgt_capacity_fn)(void *);
440typedef void *(nvm_tgt_init_fn)(struct nvm_tgt_dev *, struct gendisk *,
441 int flags);
442typedef void (nvm_tgt_exit_fn)(void *);
443typedef int (nvm_tgt_sysfs_init_fn)(struct gendisk *);
444typedef void (nvm_tgt_sysfs_exit_fn)(struct gendisk *);
445
446struct nvm_tgt_type {
447 const char *name;
448 unsigned int version[3];
449
450
451 nvm_tgt_make_rq_fn *make_rq;
452 nvm_tgt_capacity_fn *capacity;
453
454
455 nvm_tgt_init_fn *init;
456 nvm_tgt_exit_fn *exit;
457
458
459 nvm_tgt_sysfs_init_fn *sysfs_init;
460 nvm_tgt_sysfs_exit_fn *sysfs_exit;
461
462
463 struct list_head list;
464};
465
466extern struct nvm_tgt_type *nvm_find_target_type(const char *, int);
467
468extern int nvm_register_tgt_type(struct nvm_tgt_type *);
469extern void nvm_unregister_tgt_type(struct nvm_tgt_type *);
470
471extern void *nvm_dev_dma_alloc(struct nvm_dev *, gfp_t, dma_addr_t *);
472extern void nvm_dev_dma_free(struct nvm_dev *, void *, dma_addr_t);
473
474extern struct nvm_dev *nvm_alloc_dev(int);
475extern int nvm_register(struct nvm_dev *);
476extern void nvm_unregister(struct nvm_dev *);
477
478extern int nvm_set_tgt_bb_tbl(struct nvm_tgt_dev *, struct ppa_addr *,
479 int, int);
480extern int nvm_max_phys_sects(struct nvm_tgt_dev *);
481extern int nvm_submit_io(struct nvm_tgt_dev *, struct nvm_rq *);
482extern int nvm_erase_sync(struct nvm_tgt_dev *, struct ppa_addr *, int);
483extern int nvm_set_rqd_ppalist(struct nvm_tgt_dev *, struct nvm_rq *,
484 const struct ppa_addr *, int, int);
485extern void nvm_free_rqd_ppalist(struct nvm_tgt_dev *, struct nvm_rq *);
486extern int nvm_get_l2p_tbl(struct nvm_tgt_dev *, u64, u32, nvm_l2p_update_fn *,
487 void *);
488extern int nvm_get_area(struct nvm_tgt_dev *, sector_t *, sector_t);
489extern void nvm_put_area(struct nvm_tgt_dev *, sector_t);
490extern void nvm_end_io(struct nvm_rq *);
491extern int nvm_bb_tbl_fold(struct nvm_dev *, u8 *, int);
492extern int nvm_get_tgt_bb_tbl(struct nvm_tgt_dev *, struct ppa_addr, u8 *);
493
494extern int nvm_dev_factory(struct nvm_dev *, int flags);
495
496extern void nvm_part_to_tgt(struct nvm_dev *, sector_t *, int);
497
498#else
499struct nvm_dev_ops;
500
501static inline struct nvm_dev *nvm_alloc_dev(int node)
502{
503 return ERR_PTR(-EINVAL);
504}
505static inline int nvm_register(struct nvm_dev *dev)
506{
507 return -EINVAL;
508}
509static inline void nvm_unregister(struct nvm_dev *dev) {}
510#endif
511#endif
512