blob: 7c273bbc5351d528357d223dfb1427eb269b14c7 [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
Javier González959e9112016-11-28 22:39:11 +0100323 struct nvm_dev *parent;
Javier González8e536242016-11-28 22:39:10 +0100324 void *map;
Javier González8e79b5c2016-11-28 22:39:06 +0100325};
326
327struct nvm_dev {
328 struct nvm_dev_ops *ops;
329
330 struct list_head devices;
331
332 /* Media manager */
333 struct nvmm_type *mt;
334 void *mp;
335
336 /* System blocks */
337 struct nvm_sb_info sb;
338
339 /* Device information */
340 struct nvm_geo geo;
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100341
Matias Bjørlingca5927e2016-01-12 07:49:35 +0100342 /* lower page table */
343 int lps_per_blk;
344 int *lptbl;
345
Matias Bjørling4ece44a2016-02-20 08:52:41 +0100346 unsigned long total_secs;
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100347
Wenwei Taoda1e2842016-03-03 15:06:38 +0100348 unsigned long *lun_map;
Javier González75b85642016-05-06 20:03:13 +0200349 void *dma_pool;
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100350
351 struct nvm_id identity;
352
353 /* Backend device */
354 struct request_queue *q;
355 char name[DISK_NAME_LEN];
Simon A. F. Lund40267ef2016-09-16 14:25:08 +0200356 void *private_data;
Matias Bjørlinge3eb3792016-01-12 07:49:36 +0100357
Javier González8e536242016-11-28 22:39:10 +0100358 void *rmap;
359
Matias Bjørlinge3eb3792016-01-12 07:49:36 +0100360 struct mutex mlock;
Wenwei Tao4c9dacb2016-03-03 15:06:37 +0100361 spinlock_t lock;
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100362};
363
Javier González8e79b5c2016-11-28 22:39:06 +0100364static inline struct ppa_addr linear_to_generic_addr(struct nvm_geo *geo,
Javier González8e536242016-11-28 22:39:10 +0100365 u64 pba)
Javier González0e5c3242016-11-28 22:39:01 +0100366{
367 struct ppa_addr l;
368 int secs, pgs, blks, luns;
Javier González8e536242016-11-28 22:39:10 +0100369 sector_t ppa = pba;
Javier González0e5c3242016-11-28 22:39:01 +0100370
371 l.ppa = 0;
372
Javier González8e79b5c2016-11-28 22:39:06 +0100373 div_u64_rem(ppa, geo->sec_per_pg, &secs);
Javier González0e5c3242016-11-28 22:39:01 +0100374 l.g.sec = secs;
375
Javier González8e79b5c2016-11-28 22:39:06 +0100376 sector_div(ppa, geo->sec_per_pg);
377 div_u64_rem(ppa, geo->pgs_per_blk, &pgs);
Javier González0e5c3242016-11-28 22:39:01 +0100378 l.g.pg = pgs;
379
Javier González8e79b5c2016-11-28 22:39:06 +0100380 sector_div(ppa, geo->pgs_per_blk);
381 div_u64_rem(ppa, geo->blks_per_lun, &blks);
Javier González0e5c3242016-11-28 22:39:01 +0100382 l.g.blk = blks;
383
Javier González8e79b5c2016-11-28 22:39:06 +0100384 sector_div(ppa, geo->blks_per_lun);
385 div_u64_rem(ppa, geo->luns_per_chnl, &luns);
Javier González0e5c3242016-11-28 22:39:01 +0100386 l.g.lun = luns;
387
Javier González8e79b5c2016-11-28 22:39:06 +0100388 sector_div(ppa, geo->luns_per_chnl);
Javier González0e5c3242016-11-28 22:39:01 +0100389 l.g.ch = ppa;
390
391 return l;
392}
393
Matias Bjørling7386af22015-11-16 15:34:44 +0100394static inline struct ppa_addr generic_to_dev_addr(struct nvm_dev *dev,
395 struct ppa_addr r)
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100396{
Javier González8e79b5c2016-11-28 22:39:06 +0100397 struct nvm_geo *geo = &dev->geo;
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100398 struct ppa_addr l;
399
Javier González8e79b5c2016-11-28 22:39:06 +0100400 l.ppa = ((u64)r.g.blk) << geo->ppaf.blk_offset;
401 l.ppa |= ((u64)r.g.pg) << geo->ppaf.pg_offset;
402 l.ppa |= ((u64)r.g.sec) << geo->ppaf.sect_offset;
403 l.ppa |= ((u64)r.g.pl) << geo->ppaf.pln_offset;
404 l.ppa |= ((u64)r.g.lun) << geo->ppaf.lun_offset;
405 l.ppa |= ((u64)r.g.ch) << geo->ppaf.ch_offset;
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100406
407 return l;
408}
409
Matias Bjørling7386af22015-11-16 15:34:44 +0100410static inline struct ppa_addr dev_to_generic_addr(struct nvm_dev *dev,
411 struct ppa_addr r)
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100412{
Javier González8e79b5c2016-11-28 22:39:06 +0100413 struct nvm_geo *geo = &dev->geo;
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100414 struct ppa_addr l;
415
Javier González5389a1d2016-07-07 09:54:09 +0200416 l.ppa = 0;
Matias Bjørling7386af22015-11-16 15:34:44 +0100417 /*
418 * (r.ppa << X offset) & X len bitmask. X eq. blk, pg, etc.
419 */
Javier González8e79b5c2016-11-28 22:39:06 +0100420 l.g.blk = (r.ppa >> geo->ppaf.blk_offset) &
421 (((1 << geo->ppaf.blk_len) - 1));
422 l.g.pg |= (r.ppa >> geo->ppaf.pg_offset) &
423 (((1 << geo->ppaf.pg_len) - 1));
424 l.g.sec |= (r.ppa >> geo->ppaf.sect_offset) &
425 (((1 << geo->ppaf.sect_len) - 1));
426 l.g.pl |= (r.ppa >> geo->ppaf.pln_offset) &
427 (((1 << geo->ppaf.pln_len) - 1));
428 l.g.lun |= (r.ppa >> geo->ppaf.lun_offset) &
429 (((1 << geo->ppaf.lun_len) - 1));
430 l.g.ch |= (r.ppa >> geo->ppaf.ch_offset) &
431 (((1 << geo->ppaf.ch_len) - 1));
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100432
433 return l;
434}
435
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100436static inline int ppa_empty(struct ppa_addr ppa_addr)
437{
438 return (ppa_addr.ppa == ADDR_EMPTY);
439}
440
441static inline void ppa_set_empty(struct ppa_addr *ppa_addr)
442{
443 ppa_addr->ppa = ADDR_EMPTY;
444}
445
Javier Gonzáleza24ba462016-11-28 22:38:56 +0100446static inline int ppa_cmp_blk(struct ppa_addr ppa1, struct ppa_addr ppa2)
447{
448 if (ppa_empty(ppa1) || ppa_empty(ppa2))
449 return 0;
450
451 return ((ppa1.g.ch == ppa2.g.ch) && (ppa1.g.lun == ppa2.g.lun) &&
452 (ppa1.g.blk == ppa2.g.blk));
453}
454
Matias Bjørlinge3eb3792016-01-12 07:49:36 +0100455static inline int ppa_to_slc(struct nvm_dev *dev, int slc_pg)
456{
457 return dev->lptbl[slc_pg];
458}
459
Jens Axboedece1632015-11-05 10:41:16 -0700460typedef blk_qc_t (nvm_tgt_make_rq_fn)(struct request_queue *, struct bio *);
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100461typedef sector_t (nvm_tgt_capacity_fn)(void *);
Javier González8e536242016-11-28 22:39:10 +0100462typedef void *(nvm_tgt_init_fn)(struct nvm_tgt_dev *, struct gendisk *);
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100463typedef void (nvm_tgt_exit_fn)(void *);
464
465struct nvm_tgt_type {
466 const char *name;
467 unsigned int version[3];
468
469 /* target entry points */
470 nvm_tgt_make_rq_fn *make_rq;
471 nvm_tgt_capacity_fn *capacity;
Matias Bjørling912761622016-01-12 07:49:21 +0100472 nvm_end_io_fn *end_io;
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100473
474 /* module-specific init/teardown */
475 nvm_tgt_init_fn *init;
476 nvm_tgt_exit_fn *exit;
477
478 /* For internal use */
479 struct list_head list;
480};
481
Matias Bjørlingb76eb20b2016-07-07 09:54:16 +0200482extern struct nvm_tgt_type *nvm_find_target_type(const char *, int);
483
Simon A. F. Lund6063fe32016-05-06 20:03:02 +0200484extern int nvm_register_tgt_type(struct nvm_tgt_type *);
485extern void nvm_unregister_tgt_type(struct nvm_tgt_type *);
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100486
487extern void *nvm_dev_dma_alloc(struct nvm_dev *, gfp_t, dma_addr_t *);
488extern void nvm_dev_dma_free(struct nvm_dev *, void *, dma_addr_t);
489
490typedef int (nvmm_register_fn)(struct nvm_dev *);
491typedef void (nvmm_unregister_fn)(struct nvm_dev *);
Matias Bjørlingb76eb20b2016-07-07 09:54:16 +0200492
493typedef int (nvmm_create_tgt_fn)(struct nvm_dev *, struct nvm_ioctl_create *);
494typedef int (nvmm_remove_tgt_fn)(struct nvm_dev *, struct nvm_ioctl_remove *);
Javier González8e536242016-11-28 22:39:10 +0100495typedef int (nvmm_submit_io_fn)(struct nvm_tgt_dev *, struct nvm_rq *);
496typedef int (nvmm_erase_blk_fn)(struct nvm_tgt_dev *, struct ppa_addr *, int);
Wenwei Tao4c9dacb2016-03-03 15:06:37 +0100497typedef int (nvmm_get_area_fn)(struct nvm_dev *, sector_t *, sector_t);
498typedef void (nvmm_put_area_fn)(struct nvm_dev *, sector_t);
Javier González333ba052016-11-28 22:39:14 +0100499typedef struct ppa_addr (nvmm_trans_ppa_fn)(struct nvm_tgt_dev *,
500 struct ppa_addr, int);
Javier González8e536242016-11-28 22:39:10 +0100501typedef void (nvmm_part_to_tgt_fn)(struct nvm_dev *, sector_t*, int);
Wenwei Tao4c9dacb2016-03-03 15:06:37 +0100502
Javier González333ba052016-11-28 22:39:14 +0100503enum {
504 TRANS_TGT_TO_DEV = 0x0,
505 TRANS_DEV_TO_TGT = 0x1,
506};
507
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100508struct nvmm_type {
509 const char *name;
510 unsigned int version[3];
511
512 nvmm_register_fn *register_mgr;
513 nvmm_unregister_fn *unregister_mgr;
514
Matias Bjørlingb76eb20b2016-07-07 09:54:16 +0200515 nvmm_create_tgt_fn *create_tgt;
516 nvmm_remove_tgt_fn *remove_tgt;
517
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100518 nvmm_submit_io_fn *submit_io;
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100519 nvmm_erase_blk_fn *erase_blk;
520
Wenwei Tao4c9dacb2016-03-03 15:06:37 +0100521 nvmm_get_area_fn *get_area;
522 nvmm_put_area_fn *put_area;
523
Javier González333ba052016-11-28 22:39:14 +0100524 nvmm_trans_ppa_fn *trans_ppa;
Javier González8e536242016-11-28 22:39:10 +0100525 nvmm_part_to_tgt_fn *part_to_tgt;
526
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100527 struct list_head list;
528};
529
530extern int nvm_register_mgr(struct nvmm_type *);
531extern void nvm_unregister_mgr(struct nvmm_type *);
532
Matias Bjørlingb0b4e092016-09-16 14:25:07 +0200533extern struct nvm_dev *nvm_alloc_dev(int);
534extern int nvm_register(struct nvm_dev *);
535extern void nvm_unregister(struct nvm_dev *);
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100536
Javier González333ba052016-11-28 22:39:14 +0100537extern int nvm_set_bb_tbl(struct nvm_dev *, struct ppa_addr *, int, int);
538extern int nvm_set_tgt_bb_tbl(struct nvm_tgt_dev *, struct ppa_addr *,
539 int, int);
Javier Gonzáleza2790062016-11-28 22:39:12 +0100540extern int nvm_max_phys_sects(struct nvm_tgt_dev *);
Javier González8e536242016-11-28 22:39:10 +0100541extern int nvm_submit_io(struct nvm_tgt_dev *, struct nvm_rq *);
Matias Bjørling069368e2016-01-12 07:49:19 +0100542extern void nvm_generic_to_addr_mode(struct nvm_dev *, struct nvm_rq *);
543extern void nvm_addr_to_generic_mode(struct nvm_dev *, struct nvm_rq *);
Matias Bjørlingabd805e2016-01-12 07:49:20 +0100544extern int nvm_set_rqd_ppalist(struct nvm_dev *, struct nvm_rq *,
Matias Bjørling8680f162016-07-07 09:54:22 +0200545 const struct ppa_addr *, int, int);
Matias Bjørlingabd805e2016-01-12 07:49:20 +0100546extern void nvm_free_rqd_ppalist(struct nvm_dev *, struct nvm_rq *);
Javier Gonzálezbb314972016-11-28 22:38:54 +0100547extern int nvm_erase_ppa(struct nvm_dev *, struct ppa_addr *, int, int);
Javier González8e536242016-11-28 22:39:10 +0100548extern int nvm_erase_blk(struct nvm_tgt_dev *, struct ppa_addr *, int);
Javier Gonzálezda2d7cb2016-11-28 22:39:13 +0100549extern int nvm_get_l2p_tbl(struct nvm_tgt_dev *, u64, u32, nvm_l2p_update_fn *,
Javier González8e536242016-11-28 22:39:10 +0100550 void *);
Javier Gonzálezda2d7cb2016-11-28 22:39:13 +0100551extern int nvm_get_area(struct nvm_tgt_dev *, sector_t *, sector_t);
552extern void nvm_put_area(struct nvm_tgt_dev *, sector_t);
Matias Bjørling912761622016-01-12 07:49:21 +0100553extern void nvm_end_io(struct nvm_rq *, int);
Matias Bjørling09719b62016-01-12 07:49:30 +0100554extern int nvm_submit_ppa(struct nvm_dev *, struct ppa_addr *, int, int, int,
555 void *, int);
Matias Bjørling1145e632016-05-06 20:02:56 +0200556extern int nvm_submit_ppa_list(struct nvm_dev *, struct ppa_addr *, int, int,
557 int, void *, int);
Matias Bjørling22e8c972016-05-06 20:02:58 +0200558extern int nvm_bb_tbl_fold(struct nvm_dev *, u8 *, int);
Matias Bjørlinge11903f2016-05-06 20:03:05 +0200559extern int nvm_get_bb_tbl(struct nvm_dev *, struct ppa_addr, u8 *);
Javier González333ba052016-11-28 22:39:14 +0100560extern int nvm_get_tgt_bb_tbl(struct nvm_tgt_dev *, struct ppa_addr, u8 *);
Matias Bjørlinge3eb3792016-01-12 07:49:36 +0100561
562/* sysblk.c */
563#define NVM_SYSBLK_MAGIC 0x4E564D53 /* "NVMS" */
564
565/* system block on disk representation */
566struct nvm_system_block {
567 __be32 magic; /* magic signature */
568 __be32 seqnr; /* sequence number */
569 __be32 erase_cnt; /* erase count */
570 __be16 version; /* version number */
571 u8 mmtype[NVM_MMTYPE_LEN]; /* media manager name */
572 __be64 fs_ppa; /* PPA for media manager
573 * superblock */
574};
575
576extern int nvm_get_sysblock(struct nvm_dev *, struct nvm_sb_info *);
577extern int nvm_update_sysblock(struct nvm_dev *, struct nvm_sb_info *);
578extern int nvm_init_sysblock(struct nvm_dev *, struct nvm_sb_info *);
Matias Bjørling8b4970c2016-01-12 07:49:39 +0100579
580extern int nvm_dev_factory(struct nvm_dev *, int flags);
Matias Bjørling51360612016-05-06 20:03:04 +0200581
Javier González8e79b5c2016-11-28 22:39:06 +0100582#define nvm_for_each_lun_ppa(geo, ppa, chid, lunid) \
583 for ((chid) = 0, (ppa).ppa = 0; (chid) < (geo)->nr_chnls; \
Matias Bjørling51360612016-05-06 20:03:04 +0200584 (chid)++, (ppa).g.ch = (chid)) \
Javier González8e79b5c2016-11-28 22:39:06 +0100585 for ((lunid) = 0; (lunid) < (geo)->luns_per_chnl; \
Matias Bjørling51360612016-05-06 20:03:04 +0200586 (lunid)++, (ppa).g.lun = (lunid))
587
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100588#else /* CONFIG_NVM */
589struct nvm_dev_ops;
590
Matias Bjørlingb0b4e092016-09-16 14:25:07 +0200591static inline struct nvm_dev *nvm_alloc_dev(int node)
592{
593 return ERR_PTR(-EINVAL);
594}
595static inline int nvm_register(struct nvm_dev *dev)
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100596{
597 return -EINVAL;
598}
Matias Bjørlingb0b4e092016-09-16 14:25:07 +0200599static inline void nvm_unregister(struct nvm_dev *dev) {}
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100600#endif /* CONFIG_NVM */
601#endif /* LIGHTNVM.H */