blob: 2222853ef969636e9f0d02b9406704713be7727f [file] [log] [blame]
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001#ifndef NVM_H
2#define NVM_H
3
Matias Bjørlingb76eb20b2016-07-07 09:54:16 +02004#include <linux/blkdev.h>
Jens Axboea7fd9a42016-01-13 13:04:11 -07005#include <linux/types.h>
Matias Bjørlingb76eb20b2016-07-07 09:54:16 +02006#include <uapi/linux/lightnvm.h>
Jens Axboea7fd9a42016-01-13 13:04:11 -07007
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01008enum {
9 NVM_IO_OK = 0,
10 NVM_IO_REQUEUE = 1,
11 NVM_IO_DONE = 2,
12 NVM_IO_ERR = 3,
13
14 NVM_IOTYPE_NONE = 0,
15 NVM_IOTYPE_GC = 1,
16};
17
Jens Axboea7fd9a42016-01-13 13:04:11 -070018#define NVM_BLK_BITS (16)
19#define NVM_PG_BITS (16)
20#define NVM_SEC_BITS (8)
21#define NVM_PL_BITS (8)
22#define NVM_LUN_BITS (8)
Matias Bjørlingdf414b32016-05-06 20:03:19 +020023#define NVM_CH_BITS (7)
Jens Axboea7fd9a42016-01-13 13:04:11 -070024
25struct ppa_addr {
26 /* Generic structure for all addresses */
27 union {
28 struct {
29 u64 blk : NVM_BLK_BITS;
30 u64 pg : NVM_PG_BITS;
31 u64 sec : NVM_SEC_BITS;
32 u64 pl : NVM_PL_BITS;
33 u64 lun : NVM_LUN_BITS;
34 u64 ch : NVM_CH_BITS;
Matias Bjørlingdf414b32016-05-06 20:03:19 +020035 u64 reserved : 1;
Jens Axboea7fd9a42016-01-13 13:04:11 -070036 } g;
37
Matias Bjørlingdf414b32016-05-06 20:03:19 +020038 struct {
39 u64 line : 63;
40 u64 is_cached : 1;
41 } c;
42
Jens Axboea7fd9a42016-01-13 13:04:11 -070043 u64 ppa;
44 };
45};
46
47struct nvm_rq;
48struct nvm_id;
49struct nvm_dev;
Javier González8e536242016-11-28 22:39:10 +010050struct nvm_tgt_dev;
Jens Axboea7fd9a42016-01-13 13:04:11 -070051
52typedef int (nvm_l2p_update_fn)(u64, u32, __le64 *, void *);
Jens Axboea7fd9a42016-01-13 13:04:11 -070053typedef int (nvm_id_fn)(struct nvm_dev *, struct nvm_id *);
54typedef int (nvm_get_l2p_tbl_fn)(struct nvm_dev *, u64, u32,
55 nvm_l2p_update_fn *, void *);
Matias Bjørlinge11903f2016-05-06 20:03:05 +020056typedef int (nvm_op_bb_tbl_fn)(struct nvm_dev *, struct ppa_addr, u8 *);
Matias Bjørling00ee6cc2016-05-06 20:03:09 +020057typedef int (nvm_op_set_bb_fn)(struct nvm_dev *, struct ppa_addr *, int, int);
Jens Axboea7fd9a42016-01-13 13:04:11 -070058typedef int (nvm_submit_io_fn)(struct nvm_dev *, struct nvm_rq *);
59typedef int (nvm_erase_blk_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 nvm_erase_blk_fn *erase_block;
74
75 nvm_create_dma_pool_fn *create_dma_pool;
76 nvm_destroy_dma_pool_fn *destroy_dma_pool;
77 nvm_dev_dma_alloc_fn *dev_dma_alloc;
78 nvm_dev_dma_free_fn *dev_dma_free;
79
80 unsigned int max_phys_sect;
81};
82
83
84
Matias Bjørlingcd9e9802015-10-28 19:54:55 +010085#ifdef CONFIG_NVM
86
87#include <linux/blkdev.h>
Matias Bjørlingcd9e9802015-10-28 19:54:55 +010088#include <linux/file.h>
89#include <linux/dmapool.h>
Matias Bjørlinge3eb3792016-01-12 07:49:36 +010090#include <uapi/linux/lightnvm.h>
Matias Bjørlingcd9e9802015-10-28 19:54:55 +010091
92enum {
93 /* HW Responsibilities */
94 NVM_RSP_L2P = 1 << 0,
95 NVM_RSP_ECC = 1 << 1,
96
97 /* Physical Adressing Mode */
98 NVM_ADDRMODE_LINEAR = 0,
99 NVM_ADDRMODE_CHANNEL = 1,
100
101 /* Plane programming mode for LUN */
Matias Bjørlingd5bdec82016-02-19 13:56:58 +0100102 NVM_PLANE_SINGLE = 1,
103 NVM_PLANE_DOUBLE = 2,
104 NVM_PLANE_QUAD = 4,
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100105
106 /* Status codes */
107 NVM_RSP_SUCCESS = 0x0,
108 NVM_RSP_NOT_CHANGEABLE = 0x1,
109 NVM_RSP_ERR_FAILWRITE = 0x40ff,
110 NVM_RSP_ERR_EMPTYPAGE = 0x42ff,
Javier González402ab9a2016-11-28 22:38:57 +0100111 NVM_RSP_ERR_FAILECC = 0x4281,
112 NVM_RSP_WARN_HIGHECC = 0x4700,
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100113
114 /* Device opcodes */
115 NVM_OP_HBREAD = 0x02,
116 NVM_OP_HBWRITE = 0x81,
117 NVM_OP_PWRITE = 0x91,
118 NVM_OP_PREAD = 0x92,
119 NVM_OP_ERASE = 0x90,
120
121 /* PPA Command Flags */
122 NVM_IO_SNGL_ACCESS = 0x0,
123 NVM_IO_DUAL_ACCESS = 0x1,
124 NVM_IO_QUAD_ACCESS = 0x2,
125
Matias Bjørling57b4bd02015-12-06 11:25:47 +0100126 /* NAND Access Modes */
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100127 NVM_IO_SUSPEND = 0x80,
128 NVM_IO_SLC_MODE = 0x100,
129 NVM_IO_SCRAMBLE_DISABLE = 0x200,
Matias Bjørling57b4bd02015-12-06 11:25:47 +0100130
131 /* Block Types */
132 NVM_BLK_T_FREE = 0x0,
133 NVM_BLK_T_BAD = 0x1,
Matias Bjørlingb5d4acd2016-01-12 07:49:32 +0100134 NVM_BLK_T_GRWN_BAD = 0x2,
135 NVM_BLK_T_DEV = 0x4,
136 NVM_BLK_T_HOST = 0x8,
Matias Bjørlingf9a99952016-01-12 07:49:34 +0100137
138 /* Memory capabilities */
139 NVM_ID_CAP_SLC = 0x1,
140 NVM_ID_CAP_CMD_SUSPEND = 0x2,
141 NVM_ID_CAP_SCRAMBLE = 0x4,
142 NVM_ID_CAP_ENCRYPT = 0x8,
Matias Bjørlingca5927e2016-01-12 07:49:35 +0100143
144 /* Memory types */
145 NVM_ID_FMTYPE_SLC = 0,
146 NVM_ID_FMTYPE_MLC = 1,
Matias Bjørlingbf643182016-02-04 15:13:27 +0100147
148 /* Device capabilities */
149 NVM_ID_DCAP_BBLKMGMT = 0x1,
150 NVM_UD_DCAP_ECC = 0x2,
Matias Bjørlingca5927e2016-01-12 07:49:35 +0100151};
152
153struct nvm_id_lp_mlc {
154 u16 num_pairs;
155 u8 pairs[886];
156};
157
158struct nvm_id_lp_tbl {
159 __u8 id[8];
160 struct nvm_id_lp_mlc mlc;
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100161};
162
163struct nvm_id_group {
164 u8 mtype;
165 u8 fmtype;
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100166 u8 num_ch;
167 u8 num_lun;
168 u8 num_pln;
169 u16 num_blk;
170 u16 num_pg;
171 u16 fpg_sz;
172 u16 csecs;
173 u16 sos;
174 u32 trdt;
175 u32 trdm;
176 u32 tprt;
177 u32 tprm;
178 u32 tbet;
179 u32 tbem;
180 u32 mpos;
Matias Bjørling12be5ed2015-11-16 15:34:39 +0100181 u32 mccap;
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100182 u16 cpar;
Matias Bjørlingca5927e2016-01-12 07:49:35 +0100183
184 struct nvm_id_lp_tbl lptbl;
Matias Bjørling73387e72015-11-16 15:34:40 +0100185};
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100186
187struct nvm_addr_format {
188 u8 ch_offset;
189 u8 ch_len;
190 u8 lun_offset;
191 u8 lun_len;
192 u8 pln_offset;
193 u8 pln_len;
194 u8 blk_offset;
195 u8 blk_len;
196 u8 pg_offset;
197 u8 pg_len;
198 u8 sect_offset;
199 u8 sect_len;
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100200};
201
202struct nvm_id {
203 u8 ver_id;
204 u8 vmnt;
205 u8 cgrps;
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100206 u32 cap;
207 u32 dom;
208 struct nvm_addr_format ppaf;
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100209 struct nvm_id_group groups[4];
210} __packed;
211
212struct nvm_target {
213 struct list_head list;
Javier González8e79b5c2016-11-28 22:39:06 +0100214 struct nvm_tgt_dev *dev;
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100215 struct nvm_tgt_type *type;
216 struct gendisk *disk;
217};
218
219struct nvm_tgt_instance {
220 struct nvm_tgt_type *tt;
221};
222
223#define ADDR_EMPTY (~0ULL)
224
225#define NVM_VERSION_MAJOR 1
226#define NVM_VERSION_MINOR 0
227#define NVM_VERSION_PATCH 0
228
Matias Bjørling912761622016-01-12 07:49:21 +0100229struct nvm_rq;
Matias Bjørling72d256e2016-01-12 07:49:29 +0100230typedef void (nvm_end_io_fn)(struct nvm_rq *);
Matias Bjørling912761622016-01-12 07:49:21 +0100231
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100232struct nvm_rq {
233 struct nvm_tgt_instance *ins;
Javier González8e536242016-11-28 22:39:10 +0100234 struct nvm_tgt_dev *dev;
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100235
236 struct bio *bio;
237
238 union {
239 struct ppa_addr ppa_addr;
240 dma_addr_t dma_ppa_list;
241 };
242
243 struct ppa_addr *ppa_list;
244
Javier González003fad32016-05-06 20:03:12 +0200245 void *meta_list;
246 dma_addr_t dma_meta_list;
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100247
Matias Bjørling912761622016-01-12 07:49:21 +0100248 struct completion *wait;
249 nvm_end_io_fn *end_io;
250
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100251 uint8_t opcode;
Javier González6d5be952016-05-06 20:03:20 +0200252 uint16_t nr_ppas;
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100253 uint16_t flags;
Matias Bjørling72d256e2016-01-12 07:49:29 +0100254
Matias Bjorling9f867262016-03-03 15:06:39 +0100255 u64 ppa_status; /* ppa media status */
Matias Bjørling72d256e2016-01-12 07:49:29 +0100256 int error;
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100257};
258
259static inline struct nvm_rq *nvm_rq_from_pdu(void *pdu)
260{
261 return pdu - sizeof(struct nvm_rq);
262}
263
264static inline void *nvm_rq_to_pdu(struct nvm_rq *rqdata)
265{
266 return rqdata + 1;
267}
268
Javier Gonzálezff0e4982016-01-12 07:49:33 +0100269enum {
270 NVM_BLK_ST_FREE = 0x1, /* Free block */
Matias Bjørling077d2382016-07-07 09:54:14 +0200271 NVM_BLK_ST_TGT = 0x2, /* Block in use by target */
Javier Gonzálezff0e4982016-01-12 07:49:33 +0100272 NVM_BLK_ST_BAD = 0x8, /* Bad block */
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100273};
274
Matias Bjørlinge3eb3792016-01-12 07:49:36 +0100275/* system block cpu representation */
276struct nvm_sb_info {
277 unsigned long seqnr;
278 unsigned long erase_cnt;
279 unsigned int version;
280 char mmtype[NVM_MMTYPE_LEN];
281 struct ppa_addr fs_ppa;
282};
283
Javier González8e79b5c2016-11-28 22:39:06 +0100284/* Device generic information */
285struct nvm_geo {
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100286 int nr_chnls;
Javier González8e79b5c2016-11-28 22:39:06 +0100287 int nr_luns;
288 int luns_per_chnl; /* -1 if channels are not symmetric */
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100289 int nr_planes;
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100290 int sec_per_pg; /* only sectors for a single page */
291 int pgs_per_blk;
292 int blks_per_lun;
Matias Bjørling4891d122016-05-06 20:02:57 +0200293 int fpg_size;
294 int pfpg_size; /* size of buffer if all pages are to be read */
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100295 int sec_size;
296 int oob_size;
Matias Bjørlingf9a99952016-01-12 07:49:34 +0100297 int mccap;
Matias Bjørling7386af22015-11-16 15:34:44 +0100298 struct nvm_addr_format ppaf;
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100299
300 /* Calculated/Cached values. These do not reflect the actual usable
301 * blocks at run-time.
302 */
303 int max_rq_size;
304 int plane_mode; /* drive device in single, double or quad mode */
305
306 int sec_per_pl; /* all sectors across planes */
307 int sec_per_blk;
308 int sec_per_lun;
Javier González8e79b5c2016-11-28 22:39:06 +0100309};
310
311struct nvm_tgt_dev {
312 /* Device information */
313 struct nvm_geo geo;
314
Javier González8e536242016-11-28 22:39:10 +0100315 /* Base ppas for target LUNs */
316 struct ppa_addr *luns;
317
Javier González8e79b5c2016-11-28 22:39:06 +0100318 sector_t total_secs;
319
320 struct nvm_id identity;
321 struct request_queue *q;
322
323 struct nvmm_type *mt;
324 struct nvm_dev_ops *ops;
325
326 void *parent;
Javier González8e536242016-11-28 22:39:10 +0100327 void *map;
Javier González8e79b5c2016-11-28 22:39:06 +0100328};
329
330struct nvm_dev {
331 struct nvm_dev_ops *ops;
332
333 struct list_head devices;
334
335 /* Media manager */
336 struct nvmm_type *mt;
337 void *mp;
338
339 /* System blocks */
340 struct nvm_sb_info sb;
341
342 /* Device information */
343 struct nvm_geo geo;
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100344
Matias Bjørlingca5927e2016-01-12 07:49:35 +0100345 /* lower page table */
346 int lps_per_blk;
347 int *lptbl;
348
Matias Bjørling4ece44a2016-02-20 08:52:41 +0100349 unsigned long total_secs;
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100350
Wenwei Taoda1e2842016-03-03 15:06:38 +0100351 unsigned long *lun_map;
Javier González75b85642016-05-06 20:03:13 +0200352 void *dma_pool;
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100353
354 struct nvm_id identity;
355
356 /* Backend device */
357 struct request_queue *q;
358 char name[DISK_NAME_LEN];
Simon A. F. Lund40267ef2016-09-16 14:25:08 +0200359 void *private_data;
Matias Bjørlinge3eb3792016-01-12 07:49:36 +0100360
Javier González8e536242016-11-28 22:39:10 +0100361 void *rmap;
362
Matias Bjørlinge3eb3792016-01-12 07:49:36 +0100363 struct mutex mlock;
Wenwei Tao4c9dacb2016-03-03 15:06:37 +0100364 spinlock_t lock;
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100365};
366
Javier González8e79b5c2016-11-28 22:39:06 +0100367static inline struct ppa_addr linear_to_generic_addr(struct nvm_geo *geo,
Javier González8e536242016-11-28 22:39:10 +0100368 u64 pba)
Javier González0e5c3242016-11-28 22:39:01 +0100369{
370 struct ppa_addr l;
371 int secs, pgs, blks, luns;
Javier González8e536242016-11-28 22:39:10 +0100372 sector_t ppa = pba;
Javier González0e5c3242016-11-28 22:39:01 +0100373
374 l.ppa = 0;
375
Javier González8e79b5c2016-11-28 22:39:06 +0100376 div_u64_rem(ppa, geo->sec_per_pg, &secs);
Javier González0e5c3242016-11-28 22:39:01 +0100377 l.g.sec = secs;
378
Javier González8e79b5c2016-11-28 22:39:06 +0100379 sector_div(ppa, geo->sec_per_pg);
380 div_u64_rem(ppa, geo->pgs_per_blk, &pgs);
Javier González0e5c3242016-11-28 22:39:01 +0100381 l.g.pg = pgs;
382
Javier González8e79b5c2016-11-28 22:39:06 +0100383 sector_div(ppa, geo->pgs_per_blk);
384 div_u64_rem(ppa, geo->blks_per_lun, &blks);
Javier González0e5c3242016-11-28 22:39:01 +0100385 l.g.blk = blks;
386
Javier González8e79b5c2016-11-28 22:39:06 +0100387 sector_div(ppa, geo->blks_per_lun);
388 div_u64_rem(ppa, geo->luns_per_chnl, &luns);
Javier González0e5c3242016-11-28 22:39:01 +0100389 l.g.lun = luns;
390
Javier González8e79b5c2016-11-28 22:39:06 +0100391 sector_div(ppa, geo->luns_per_chnl);
Javier González0e5c3242016-11-28 22:39:01 +0100392 l.g.ch = ppa;
393
394 return l;
395}
396
Matias Bjørling7386af22015-11-16 15:34:44 +0100397static inline struct ppa_addr generic_to_dev_addr(struct nvm_dev *dev,
398 struct ppa_addr r)
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100399{
Javier González8e79b5c2016-11-28 22:39:06 +0100400 struct nvm_geo *geo = &dev->geo;
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100401 struct ppa_addr l;
402
Javier González8e79b5c2016-11-28 22:39:06 +0100403 l.ppa = ((u64)r.g.blk) << geo->ppaf.blk_offset;
404 l.ppa |= ((u64)r.g.pg) << geo->ppaf.pg_offset;
405 l.ppa |= ((u64)r.g.sec) << geo->ppaf.sect_offset;
406 l.ppa |= ((u64)r.g.pl) << geo->ppaf.pln_offset;
407 l.ppa |= ((u64)r.g.lun) << geo->ppaf.lun_offset;
408 l.ppa |= ((u64)r.g.ch) << geo->ppaf.ch_offset;
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100409
410 return l;
411}
412
Matias Bjørling7386af22015-11-16 15:34:44 +0100413static inline struct ppa_addr dev_to_generic_addr(struct nvm_dev *dev,
414 struct ppa_addr r)
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100415{
Javier González8e79b5c2016-11-28 22:39:06 +0100416 struct nvm_geo *geo = &dev->geo;
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100417 struct ppa_addr l;
418
Javier González5389a1d2016-07-07 09:54:09 +0200419 l.ppa = 0;
Matias Bjørling7386af22015-11-16 15:34:44 +0100420 /*
421 * (r.ppa << X offset) & X len bitmask. X eq. blk, pg, etc.
422 */
Javier González8e79b5c2016-11-28 22:39:06 +0100423 l.g.blk = (r.ppa >> geo->ppaf.blk_offset) &
424 (((1 << geo->ppaf.blk_len) - 1));
425 l.g.pg |= (r.ppa >> geo->ppaf.pg_offset) &
426 (((1 << geo->ppaf.pg_len) - 1));
427 l.g.sec |= (r.ppa >> geo->ppaf.sect_offset) &
428 (((1 << geo->ppaf.sect_len) - 1));
429 l.g.pl |= (r.ppa >> geo->ppaf.pln_offset) &
430 (((1 << geo->ppaf.pln_len) - 1));
431 l.g.lun |= (r.ppa >> geo->ppaf.lun_offset) &
432 (((1 << geo->ppaf.lun_len) - 1));
433 l.g.ch |= (r.ppa >> geo->ppaf.ch_offset) &
434 (((1 << geo->ppaf.ch_len) - 1));
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100435
436 return l;
437}
438
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100439static inline int ppa_empty(struct ppa_addr ppa_addr)
440{
441 return (ppa_addr.ppa == ADDR_EMPTY);
442}
443
444static inline void ppa_set_empty(struct ppa_addr *ppa_addr)
445{
446 ppa_addr->ppa = ADDR_EMPTY;
447}
448
Javier Gonzáleza24ba462016-11-28 22:38:56 +0100449static inline int ppa_cmp_blk(struct ppa_addr ppa1, struct ppa_addr ppa2)
450{
451 if (ppa_empty(ppa1) || ppa_empty(ppa2))
452 return 0;
453
454 return ((ppa1.g.ch == ppa2.g.ch) && (ppa1.g.lun == ppa2.g.lun) &&
455 (ppa1.g.blk == ppa2.g.blk));
456}
457
Matias Bjørlinge3eb3792016-01-12 07:49:36 +0100458static inline int ppa_to_slc(struct nvm_dev *dev, int slc_pg)
459{
460 return dev->lptbl[slc_pg];
461}
462
Jens Axboedece1632015-11-05 10:41:16 -0700463typedef blk_qc_t (nvm_tgt_make_rq_fn)(struct request_queue *, struct bio *);
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100464typedef sector_t (nvm_tgt_capacity_fn)(void *);
Javier González8e536242016-11-28 22:39:10 +0100465typedef void *(nvm_tgt_init_fn)(struct nvm_tgt_dev *, struct gendisk *);
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100466typedef void (nvm_tgt_exit_fn)(void *);
467
468struct nvm_tgt_type {
469 const char *name;
470 unsigned int version[3];
471
472 /* target entry points */
473 nvm_tgt_make_rq_fn *make_rq;
474 nvm_tgt_capacity_fn *capacity;
Matias Bjørling912761622016-01-12 07:49:21 +0100475 nvm_end_io_fn *end_io;
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100476
477 /* module-specific init/teardown */
478 nvm_tgt_init_fn *init;
479 nvm_tgt_exit_fn *exit;
480
481 /* For internal use */
482 struct list_head list;
483};
484
Matias Bjørlingb76eb20b2016-07-07 09:54:16 +0200485extern struct nvm_tgt_type *nvm_find_target_type(const char *, int);
486
Simon A. F. Lund6063fe32016-05-06 20:03:02 +0200487extern int nvm_register_tgt_type(struct nvm_tgt_type *);
488extern void nvm_unregister_tgt_type(struct nvm_tgt_type *);
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100489
490extern void *nvm_dev_dma_alloc(struct nvm_dev *, gfp_t, dma_addr_t *);
491extern void nvm_dev_dma_free(struct nvm_dev *, void *, dma_addr_t);
492
493typedef int (nvmm_register_fn)(struct nvm_dev *);
494typedef void (nvmm_unregister_fn)(struct nvm_dev *);
Matias Bjørlingb76eb20b2016-07-07 09:54:16 +0200495
496typedef int (nvmm_create_tgt_fn)(struct nvm_dev *, struct nvm_ioctl_create *);
497typedef int (nvmm_remove_tgt_fn)(struct nvm_dev *, struct nvm_ioctl_remove *);
Javier González8e536242016-11-28 22:39:10 +0100498typedef int (nvmm_submit_io_fn)(struct nvm_tgt_dev *, struct nvm_rq *);
499typedef int (nvmm_erase_blk_fn)(struct nvm_tgt_dev *, struct ppa_addr *, int);
Wenwei Tao4c9dacb2016-03-03 15:06:37 +0100500typedef int (nvmm_get_area_fn)(struct nvm_dev *, sector_t *, sector_t);
501typedef void (nvmm_put_area_fn)(struct nvm_dev *, sector_t);
Javier González8e536242016-11-28 22:39:10 +0100502typedef void (nvmm_part_to_tgt_fn)(struct nvm_dev *, sector_t*, int);
Wenwei Tao4c9dacb2016-03-03 15:06:37 +0100503
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100504struct nvmm_type {
505 const char *name;
506 unsigned int version[3];
507
508 nvmm_register_fn *register_mgr;
509 nvmm_unregister_fn *unregister_mgr;
510
Matias Bjørlingb76eb20b2016-07-07 09:54:16 +0200511 nvmm_create_tgt_fn *create_tgt;
512 nvmm_remove_tgt_fn *remove_tgt;
513
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100514 nvmm_submit_io_fn *submit_io;
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100515 nvmm_erase_blk_fn *erase_blk;
516
Wenwei Tao4c9dacb2016-03-03 15:06:37 +0100517 nvmm_get_area_fn *get_area;
518 nvmm_put_area_fn *put_area;
519
Javier González8e536242016-11-28 22:39:10 +0100520 nvmm_part_to_tgt_fn *part_to_tgt;
521
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100522 struct list_head list;
523};
524
525extern int nvm_register_mgr(struct nvmm_type *);
526extern void nvm_unregister_mgr(struct nvmm_type *);
527
Matias Bjørlingb0b4e092016-09-16 14:25:07 +0200528extern struct nvm_dev *nvm_alloc_dev(int);
529extern int nvm_register(struct nvm_dev *);
530extern void nvm_unregister(struct nvm_dev *);
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100531
Javier Gonzáleza24ba462016-11-28 22:38:56 +0100532extern int nvm_set_bb_tbl(struct nvm_dev *dev, struct ppa_addr *ppas,
533 int nr_ppas, int type);
Javier González529435e812016-07-07 09:54:08 +0200534
Javier González8e536242016-11-28 22:39:10 +0100535extern int nvm_submit_io(struct nvm_tgt_dev *, struct nvm_rq *);
Matias Bjørling069368e2016-01-12 07:49:19 +0100536extern void nvm_generic_to_addr_mode(struct nvm_dev *, struct nvm_rq *);
537extern void nvm_addr_to_generic_mode(struct nvm_dev *, struct nvm_rq *);
Matias Bjørlingabd805e2016-01-12 07:49:20 +0100538extern int nvm_set_rqd_ppalist(struct nvm_dev *, struct nvm_rq *,
Matias Bjørling8680f162016-07-07 09:54:22 +0200539 const struct ppa_addr *, int, int);
Matias Bjørlingabd805e2016-01-12 07:49:20 +0100540extern void nvm_free_rqd_ppalist(struct nvm_dev *, struct nvm_rq *);
Javier Gonzálezbb314972016-11-28 22:38:54 +0100541extern int nvm_erase_ppa(struct nvm_dev *, struct ppa_addr *, int, int);
Javier González8e536242016-11-28 22:39:10 +0100542extern int nvm_erase_blk(struct nvm_tgt_dev *, struct ppa_addr *, int);
543extern int nvm_get_l2p_tbl(struct nvm_dev *, u64, u32, nvm_l2p_update_fn *,
544 void *);
545extern int nvm_get_area(struct nvm_dev *, sector_t *, sector_t);
546extern void nvm_put_area(struct nvm_dev *, sector_t);
Matias Bjørling912761622016-01-12 07:49:21 +0100547extern void nvm_end_io(struct nvm_rq *, int);
Matias Bjørling09719b62016-01-12 07:49:30 +0100548extern int nvm_submit_ppa(struct nvm_dev *, struct ppa_addr *, int, int, int,
549 void *, int);
Matias Bjørling1145e632016-05-06 20:02:56 +0200550extern int nvm_submit_ppa_list(struct nvm_dev *, struct ppa_addr *, int, int,
551 int, void *, int);
Matias Bjørling22e8c972016-05-06 20:02:58 +0200552extern int nvm_bb_tbl_fold(struct nvm_dev *, u8 *, int);
Matias Bjørlinge11903f2016-05-06 20:03:05 +0200553extern int nvm_get_bb_tbl(struct nvm_dev *, struct ppa_addr, u8 *);
Matias Bjørlinge3eb3792016-01-12 07:49:36 +0100554
555/* sysblk.c */
556#define NVM_SYSBLK_MAGIC 0x4E564D53 /* "NVMS" */
557
558/* system block on disk representation */
559struct nvm_system_block {
560 __be32 magic; /* magic signature */
561 __be32 seqnr; /* sequence number */
562 __be32 erase_cnt; /* erase count */
563 __be16 version; /* version number */
564 u8 mmtype[NVM_MMTYPE_LEN]; /* media manager name */
565 __be64 fs_ppa; /* PPA for media manager
566 * superblock */
567};
568
569extern int nvm_get_sysblock(struct nvm_dev *, struct nvm_sb_info *);
570extern int nvm_update_sysblock(struct nvm_dev *, struct nvm_sb_info *);
571extern int nvm_init_sysblock(struct nvm_dev *, struct nvm_sb_info *);
Matias Bjørling8b4970c2016-01-12 07:49:39 +0100572
573extern int nvm_dev_factory(struct nvm_dev *, int flags);
Matias Bjørling51360612016-05-06 20:03:04 +0200574
Javier González8e79b5c2016-11-28 22:39:06 +0100575#define nvm_for_each_lun_ppa(geo, ppa, chid, lunid) \
576 for ((chid) = 0, (ppa).ppa = 0; (chid) < (geo)->nr_chnls; \
Matias Bjørling51360612016-05-06 20:03:04 +0200577 (chid)++, (ppa).g.ch = (chid)) \
Javier González8e79b5c2016-11-28 22:39:06 +0100578 for ((lunid) = 0; (lunid) < (geo)->luns_per_chnl; \
Matias Bjørling51360612016-05-06 20:03:04 +0200579 (lunid)++, (ppa).g.lun = (lunid))
580
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100581#else /* CONFIG_NVM */
582struct nvm_dev_ops;
583
Matias Bjørlingb0b4e092016-09-16 14:25:07 +0200584static inline struct nvm_dev *nvm_alloc_dev(int node)
585{
586 return ERR_PTR(-EINVAL);
587}
588static inline int nvm_register(struct nvm_dev *dev)
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100589{
590 return -EINVAL;
591}
Matias Bjørlingb0b4e092016-09-16 14:25:07 +0200592static inline void nvm_unregister(struct nvm_dev *dev) {}
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100593#endif /* CONFIG_NVM */
594#endif /* LIGHTNVM.H */