]> git.karo-electronics.de Git - karo-tx-linux.git/blob - include/linux/lightnvm.h
lightnvm: move rq->error to nvm_rq->error
[karo-tx-linux.git] / include / linux / lightnvm.h
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
53         /* NAND Access Modes */
54         NVM_IO_SUSPEND          = 0x80,
55         NVM_IO_SLC_MODE         = 0x100,
56         NVM_IO_SCRAMBLE_DISABLE = 0x200,
57
58         /* Block Types */
59         NVM_BLK_T_FREE          = 0x0,
60         NVM_BLK_T_BAD           = 0x1,
61         NVM_BLK_T_DEV           = 0x2,
62         NVM_BLK_T_HOST          = 0x4,
63 };
64
65 struct nvm_id_group {
66         u8      mtype;
67         u8      fmtype;
68         u8      num_ch;
69         u8      num_lun;
70         u8      num_pln;
71         u16     num_blk;
72         u16     num_pg;
73         u16     fpg_sz;
74         u16     csecs;
75         u16     sos;
76         u32     trdt;
77         u32     trdm;
78         u32     tprt;
79         u32     tprm;
80         u32     tbet;
81         u32     tbem;
82         u32     mpos;
83         u32     mccap;
84         u16     cpar;
85 };
86
87 struct nvm_addr_format {
88         u8      ch_offset;
89         u8      ch_len;
90         u8      lun_offset;
91         u8      lun_len;
92         u8      pln_offset;
93         u8      pln_len;
94         u8      blk_offset;
95         u8      blk_len;
96         u8      pg_offset;
97         u8      pg_len;
98         u8      sect_offset;
99         u8      sect_len;
100 };
101
102 struct nvm_id {
103         u8      ver_id;
104         u8      vmnt;
105         u8      cgrps;
106         u32     cap;
107         u32     dom;
108         struct nvm_addr_format ppaf;
109         struct nvm_id_group groups[4];
110 } __packed;
111
112 struct nvm_target {
113         struct list_head list;
114         struct nvm_tgt_type *type;
115         struct gendisk *disk;
116 };
117
118 struct nvm_tgt_instance {
119         struct nvm_tgt_type *tt;
120 };
121
122 #define ADDR_EMPTY (~0ULL)
123
124 #define NVM_VERSION_MAJOR 1
125 #define NVM_VERSION_MINOR 0
126 #define NVM_VERSION_PATCH 0
127
128 #define NVM_BLK_BITS (16)
129 #define NVM_PG_BITS  (16)
130 #define NVM_SEC_BITS (8)
131 #define NVM_PL_BITS  (8)
132 #define NVM_LUN_BITS (8)
133 #define NVM_CH_BITS  (8)
134
135 struct ppa_addr {
136         /* Generic structure for all addresses */
137         union {
138                 struct {
139                         u64 blk         : NVM_BLK_BITS;
140                         u64 pg          : NVM_PG_BITS;
141                         u64 sec         : NVM_SEC_BITS;
142                         u64 pl          : NVM_PL_BITS;
143                         u64 lun         : NVM_LUN_BITS;
144                         u64 ch          : NVM_CH_BITS;
145                 } g;
146
147                 u64 ppa;
148         };
149 };
150
151 struct nvm_rq;
152 typedef void (nvm_end_io_fn)(struct nvm_rq *);
153
154 struct nvm_rq {
155         struct nvm_tgt_instance *ins;
156         struct nvm_dev *dev;
157
158         struct bio *bio;
159
160         union {
161                 struct ppa_addr ppa_addr;
162                 dma_addr_t dma_ppa_list;
163         };
164
165         struct ppa_addr *ppa_list;
166
167         void *metadata;
168         dma_addr_t dma_metadata;
169
170         struct completion *wait;
171         nvm_end_io_fn *end_io;
172
173         uint8_t opcode;
174         uint16_t nr_pages;
175         uint16_t flags;
176
177         int error;
178 };
179
180 static inline struct nvm_rq *nvm_rq_from_pdu(void *pdu)
181 {
182         return pdu - sizeof(struct nvm_rq);
183 }
184
185 static inline void *nvm_rq_to_pdu(struct nvm_rq *rqdata)
186 {
187         return rqdata + 1;
188 }
189
190 struct nvm_block;
191
192 typedef int (nvm_l2p_update_fn)(u64, u32, __le64 *, void *);
193 typedef int (nvm_bb_update_fn)(struct ppa_addr, int, u8 *, void *);
194 typedef int (nvm_id_fn)(struct nvm_dev *, struct nvm_id *);
195 typedef int (nvm_get_l2p_tbl_fn)(struct nvm_dev *, u64, u32,
196                                 nvm_l2p_update_fn *, void *);
197 typedef int (nvm_op_bb_tbl_fn)(struct nvm_dev *, struct ppa_addr, int,
198                                 nvm_bb_update_fn *, void *);
199 typedef int (nvm_op_set_bb_fn)(struct nvm_dev *, struct nvm_rq *, int);
200 typedef int (nvm_submit_io_fn)(struct nvm_dev *, struct nvm_rq *);
201 typedef int (nvm_erase_blk_fn)(struct nvm_dev *, struct nvm_rq *);
202 typedef void *(nvm_create_dma_pool_fn)(struct nvm_dev *, char *);
203 typedef void (nvm_destroy_dma_pool_fn)(void *);
204 typedef void *(nvm_dev_dma_alloc_fn)(struct nvm_dev *, void *, gfp_t,
205                                                                 dma_addr_t *);
206 typedef void (nvm_dev_dma_free_fn)(void *, void*, dma_addr_t);
207
208 struct nvm_dev_ops {
209         nvm_id_fn               *identity;
210         nvm_get_l2p_tbl_fn      *get_l2p_tbl;
211         nvm_op_bb_tbl_fn        *get_bb_tbl;
212         nvm_op_set_bb_fn        *set_bb_tbl;
213
214         nvm_submit_io_fn        *submit_io;
215         nvm_erase_blk_fn        *erase_block;
216
217         nvm_create_dma_pool_fn  *create_dma_pool;
218         nvm_destroy_dma_pool_fn *destroy_dma_pool;
219         nvm_dev_dma_alloc_fn    *dev_dma_alloc;
220         nvm_dev_dma_free_fn     *dev_dma_free;
221
222         unsigned int            max_phys_sect;
223 };
224
225 struct nvm_lun {
226         int id;
227
228         int lun_id;
229         int chnl_id;
230
231         unsigned int nr_inuse_blocks;   /* Number of used blocks */
232         unsigned int nr_free_blocks;    /* Number of unused blocks */
233         unsigned int nr_bad_blocks;     /* Number of bad blocks */
234         struct nvm_block *blocks;
235
236         spinlock_t lock;
237 };
238
239 struct nvm_block {
240         struct list_head list;
241         struct nvm_lun *lun;
242         unsigned long id;
243
244         void *priv;
245         int type;
246 };
247
248 struct nvm_dev {
249         struct nvm_dev_ops *ops;
250
251         struct list_head devices;
252         struct list_head online_targets;
253
254         /* Media manager */
255         struct nvmm_type *mt;
256         void *mp;
257
258         /* Device information */
259         int nr_chnls;
260         int nr_planes;
261         int luns_per_chnl;
262         int sec_per_pg; /* only sectors for a single page */
263         int pgs_per_blk;
264         int blks_per_lun;
265         int sec_size;
266         int oob_size;
267         struct nvm_addr_format ppaf;
268
269         /* Calculated/Cached values. These do not reflect the actual usable
270          * blocks at run-time.
271          */
272         int max_rq_size;
273         int plane_mode; /* drive device in single, double or quad mode */
274
275         int sec_per_pl; /* all sectors across planes */
276         int sec_per_blk;
277         int sec_per_lun;
278
279         unsigned long total_pages;
280         unsigned long total_blocks;
281         int nr_luns;
282         unsigned max_pages_per_blk;
283
284         void *ppalist_pool;
285
286         struct nvm_id identity;
287
288         /* Backend device */
289         struct request_queue *q;
290         char name[DISK_NAME_LEN];
291 };
292
293 static inline struct ppa_addr generic_to_dev_addr(struct nvm_dev *dev,
294                                                 struct ppa_addr r)
295 {
296         struct ppa_addr l;
297
298         l.ppa = ((u64)r.g.blk) << dev->ppaf.blk_offset;
299         l.ppa |= ((u64)r.g.pg) << dev->ppaf.pg_offset;
300         l.ppa |= ((u64)r.g.sec) << dev->ppaf.sect_offset;
301         l.ppa |= ((u64)r.g.pl) << dev->ppaf.pln_offset;
302         l.ppa |= ((u64)r.g.lun) << dev->ppaf.lun_offset;
303         l.ppa |= ((u64)r.g.ch) << dev->ppaf.ch_offset;
304
305         return l;
306 }
307
308 static inline struct ppa_addr dev_to_generic_addr(struct nvm_dev *dev,
309                                                 struct ppa_addr r)
310 {
311         struct ppa_addr l;
312
313         /*
314          * (r.ppa << X offset) & X len bitmask. X eq. blk, pg, etc.
315          */
316         l.g.blk = (r.ppa >> dev->ppaf.blk_offset) &
317                                         (((1 << dev->ppaf.blk_len) - 1));
318         l.g.pg |= (r.ppa >> dev->ppaf.pg_offset) &
319                                         (((1 << dev->ppaf.pg_len) - 1));
320         l.g.sec |= (r.ppa >> dev->ppaf.sect_offset) &
321                                         (((1 << dev->ppaf.sect_len) - 1));
322         l.g.pl |= (r.ppa >> dev->ppaf.pln_offset) &
323                                         (((1 << dev->ppaf.pln_len) - 1));
324         l.g.lun |= (r.ppa >> dev->ppaf.lun_offset) &
325                                         (((1 << dev->ppaf.lun_len) - 1));
326         l.g.ch |= (r.ppa >> dev->ppaf.ch_offset) &
327                                         (((1 << dev->ppaf.ch_len) - 1));
328
329         return l;
330 }
331
332 static inline int ppa_empty(struct ppa_addr ppa_addr)
333 {
334         return (ppa_addr.ppa == ADDR_EMPTY);
335 }
336
337 static inline void ppa_set_empty(struct ppa_addr *ppa_addr)
338 {
339         ppa_addr->ppa = ADDR_EMPTY;
340 }
341
342 static inline struct ppa_addr block_to_ppa(struct nvm_dev *dev,
343                                                         struct nvm_block *blk)
344 {
345         struct ppa_addr ppa;
346         struct nvm_lun *lun = blk->lun;
347
348         ppa.ppa = 0;
349         ppa.g.blk = blk->id % dev->blks_per_lun;
350         ppa.g.lun = lun->lun_id;
351         ppa.g.ch = lun->chnl_id;
352
353         return ppa;
354 }
355
356 typedef blk_qc_t (nvm_tgt_make_rq_fn)(struct request_queue *, struct bio *);
357 typedef sector_t (nvm_tgt_capacity_fn)(void *);
358 typedef void *(nvm_tgt_init_fn)(struct nvm_dev *, struct gendisk *, int, int);
359 typedef void (nvm_tgt_exit_fn)(void *);
360
361 struct nvm_tgt_type {
362         const char *name;
363         unsigned int version[3];
364
365         /* target entry points */
366         nvm_tgt_make_rq_fn *make_rq;
367         nvm_tgt_capacity_fn *capacity;
368         nvm_end_io_fn *end_io;
369
370         /* module-specific init/teardown */
371         nvm_tgt_init_fn *init;
372         nvm_tgt_exit_fn *exit;
373
374         /* For internal use */
375         struct list_head list;
376 };
377
378 extern int nvm_register_target(struct nvm_tgt_type *);
379 extern void nvm_unregister_target(struct nvm_tgt_type *);
380
381 extern void *nvm_dev_dma_alloc(struct nvm_dev *, gfp_t, dma_addr_t *);
382 extern void nvm_dev_dma_free(struct nvm_dev *, void *, dma_addr_t);
383
384 typedef int (nvmm_register_fn)(struct nvm_dev *);
385 typedef void (nvmm_unregister_fn)(struct nvm_dev *);
386 typedef struct nvm_block *(nvmm_get_blk_fn)(struct nvm_dev *,
387                                               struct nvm_lun *, unsigned long);
388 typedef void (nvmm_put_blk_fn)(struct nvm_dev *, struct nvm_block *);
389 typedef int (nvmm_open_blk_fn)(struct nvm_dev *, struct nvm_block *);
390 typedef int (nvmm_close_blk_fn)(struct nvm_dev *, struct nvm_block *);
391 typedef void (nvmm_flush_blk_fn)(struct nvm_dev *, struct nvm_block *);
392 typedef int (nvmm_submit_io_fn)(struct nvm_dev *, struct nvm_rq *);
393 typedef int (nvmm_erase_blk_fn)(struct nvm_dev *, struct nvm_block *,
394                                                                 unsigned long);
395 typedef struct nvm_lun *(nvmm_get_lun_fn)(struct nvm_dev *, int);
396 typedef void (nvmm_lun_info_print_fn)(struct nvm_dev *);
397
398 struct nvmm_type {
399         const char *name;
400         unsigned int version[3];
401
402         nvmm_register_fn *register_mgr;
403         nvmm_unregister_fn *unregister_mgr;
404
405         /* Block administration callbacks */
406         nvmm_get_blk_fn *get_blk;
407         nvmm_put_blk_fn *put_blk;
408         nvmm_open_blk_fn *open_blk;
409         nvmm_close_blk_fn *close_blk;
410         nvmm_flush_blk_fn *flush_blk;
411
412         nvmm_submit_io_fn *submit_io;
413         nvmm_erase_blk_fn *erase_blk;
414
415         /* Configuration management */
416         nvmm_get_lun_fn *get_lun;
417
418         /* Statistics */
419         nvmm_lun_info_print_fn *lun_info_print;
420         struct list_head list;
421 };
422
423 extern int nvm_register_mgr(struct nvmm_type *);
424 extern void nvm_unregister_mgr(struct nvmm_type *);
425
426 extern struct nvm_block *nvm_get_blk(struct nvm_dev *, struct nvm_lun *,
427                                                                 unsigned long);
428 extern void nvm_put_blk(struct nvm_dev *, struct nvm_block *);
429
430 extern int nvm_register(struct request_queue *, char *,
431                                                 struct nvm_dev_ops *);
432 extern void nvm_unregister(char *);
433
434 extern int nvm_submit_io(struct nvm_dev *, struct nvm_rq *);
435 extern void nvm_generic_to_addr_mode(struct nvm_dev *, struct nvm_rq *);
436 extern void nvm_addr_to_generic_mode(struct nvm_dev *, struct nvm_rq *);
437 extern int nvm_set_rqd_ppalist(struct nvm_dev *, struct nvm_rq *,
438                                                         struct ppa_addr *, int);
439 extern void nvm_free_rqd_ppalist(struct nvm_dev *, struct nvm_rq *);
440 extern int nvm_erase_ppa(struct nvm_dev *, struct ppa_addr *, int);
441 extern int nvm_erase_blk(struct nvm_dev *, struct nvm_block *);
442 extern void nvm_end_io(struct nvm_rq *, int);
443 #else /* CONFIG_NVM */
444 struct nvm_dev_ops;
445
446 static inline int nvm_register(struct request_queue *q, char *disk_name,
447                                                         struct nvm_dev_ops *ops)
448 {
449         return -EINVAL;
450 }
451 static inline void nvm_unregister(char *disk_name) {}
452 #endif /* CONFIG_NVM */
453 #endif /* LIGHTNVM.H */