lightnvm.h 10.9 KB
Newer Older
1 2 3
#ifndef NVM_H
#define NVM_H

4
#include <linux/blkdev.h>
5
#include <linux/types.h>
6
#include <uapi/linux/lightnvm.h>
7

8 9 10 11 12 13 14 15 16 17
enum {
	NVM_IO_OK = 0,
	NVM_IO_REQUEUE = 1,
	NVM_IO_DONE = 2,
	NVM_IO_ERR = 3,

	NVM_IOTYPE_NONE = 0,
	NVM_IOTYPE_GC = 1,
};

18 19 20 21 22
#define NVM_BLK_BITS (16)
#define NVM_PG_BITS  (16)
#define NVM_SEC_BITS (8)
#define NVM_PL_BITS  (8)
#define NVM_LUN_BITS (8)
23
#define NVM_CH_BITS  (7)
24 25 26 27 28 29 30 31 32 33 34

struct ppa_addr {
	/* Generic structure for all addresses */
	union {
		struct {
			u64 blk		: NVM_BLK_BITS;
			u64 pg		: NVM_PG_BITS;
			u64 sec		: NVM_SEC_BITS;
			u64 pl		: NVM_PL_BITS;
			u64 lun		: NVM_LUN_BITS;
			u64 ch		: NVM_CH_BITS;
35
			u64 reserved	: 1;
36 37
		} g;

38 39 40 41 42
		struct {
			u64 line	: 63;
			u64 is_cached	: 1;
		} c;

43 44 45 46 47 48 49
		u64 ppa;
	};
};

struct nvm_rq;
struct nvm_id;
struct nvm_dev;
50
struct nvm_tgt_dev;
51 52 53 54 55

typedef int (nvm_l2p_update_fn)(u64, u32, __le64 *, void *);
typedef int (nvm_id_fn)(struct nvm_dev *, struct nvm_id *);
typedef int (nvm_get_l2p_tbl_fn)(struct nvm_dev *, u64, u32,
				nvm_l2p_update_fn *, void *);
56
typedef int (nvm_op_bb_tbl_fn)(struct nvm_dev *, struct ppa_addr, u8 *);
57
typedef int (nvm_op_set_bb_fn)(struct nvm_dev *, struct ppa_addr *, int, int);
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
typedef int (nvm_submit_io_fn)(struct nvm_dev *, struct nvm_rq *);
typedef int (nvm_erase_blk_fn)(struct nvm_dev *, struct nvm_rq *);
typedef void *(nvm_create_dma_pool_fn)(struct nvm_dev *, char *);
typedef void (nvm_destroy_dma_pool_fn)(void *);
typedef void *(nvm_dev_dma_alloc_fn)(struct nvm_dev *, void *, gfp_t,
								dma_addr_t *);
typedef void (nvm_dev_dma_free_fn)(void *, void*, dma_addr_t);

struct nvm_dev_ops {
	nvm_id_fn		*identity;
	nvm_get_l2p_tbl_fn	*get_l2p_tbl;
	nvm_op_bb_tbl_fn	*get_bb_tbl;
	nvm_op_set_bb_fn	*set_bb_tbl;

	nvm_submit_io_fn	*submit_io;
	nvm_erase_blk_fn	*erase_block;

	nvm_create_dma_pool_fn	*create_dma_pool;
	nvm_destroy_dma_pool_fn	*destroy_dma_pool;
	nvm_dev_dma_alloc_fn	*dev_dma_alloc;
	nvm_dev_dma_free_fn	*dev_dma_free;

	unsigned int		max_phys_sect;
};

83 84 85 86 87
#ifdef CONFIG_NVM

#include <linux/blkdev.h>
#include <linux/file.h>
#include <linux/dmapool.h>
88
#include <uapi/linux/lightnvm.h>
89 90 91 92 93 94 95 96 97 98 99

enum {
	/* HW Responsibilities */
	NVM_RSP_L2P	= 1 << 0,
	NVM_RSP_ECC	= 1 << 1,

	/* Physical Adressing Mode */
	NVM_ADDRMODE_LINEAR	= 0,
	NVM_ADDRMODE_CHANNEL	= 1,

	/* Plane programming mode for LUN */
100 101 102
	NVM_PLANE_SINGLE	= 1,
	NVM_PLANE_DOUBLE	= 2,
	NVM_PLANE_QUAD		= 4,
103 104 105 106 107 108

	/* Status codes */
	NVM_RSP_SUCCESS		= 0x0,
	NVM_RSP_NOT_CHANGEABLE	= 0x1,
	NVM_RSP_ERR_FAILWRITE	= 0x40ff,
	NVM_RSP_ERR_EMPTYPAGE	= 0x42ff,
J
Javier González 已提交
109 110
	NVM_RSP_ERR_FAILECC	= 0x4281,
	NVM_RSP_WARN_HIGHECC	= 0x4700,
111 112 113 114 115 116 117 118 119 120 121 122 123

	/* Device opcodes */
	NVM_OP_HBREAD		= 0x02,
	NVM_OP_HBWRITE		= 0x81,
	NVM_OP_PWRITE		= 0x91,
	NVM_OP_PREAD		= 0x92,
	NVM_OP_ERASE		= 0x90,

	/* PPA Command Flags */
	NVM_IO_SNGL_ACCESS	= 0x0,
	NVM_IO_DUAL_ACCESS	= 0x1,
	NVM_IO_QUAD_ACCESS	= 0x2,

M
Matias Bjørling 已提交
124
	/* NAND Access Modes */
125 126 127
	NVM_IO_SUSPEND		= 0x80,
	NVM_IO_SLC_MODE		= 0x100,
	NVM_IO_SCRAMBLE_DISABLE	= 0x200,
M
Matias Bjørling 已提交
128 129 130 131

	/* Block Types */
	NVM_BLK_T_FREE		= 0x0,
	NVM_BLK_T_BAD		= 0x1,
132 133 134
	NVM_BLK_T_GRWN_BAD	= 0x2,
	NVM_BLK_T_DEV		= 0x4,
	NVM_BLK_T_HOST		= 0x8,
M
Matias Bjørling 已提交
135 136 137 138 139 140

	/* Memory capabilities */
	NVM_ID_CAP_SLC		= 0x1,
	NVM_ID_CAP_CMD_SUSPEND	= 0x2,
	NVM_ID_CAP_SCRAMBLE	= 0x4,
	NVM_ID_CAP_ENCRYPT	= 0x8,
141 142 143 144

	/* Memory types */
	NVM_ID_FMTYPE_SLC	= 0,
	NVM_ID_FMTYPE_MLC	= 1,
145 146 147 148

	/* Device capabilities */
	NVM_ID_DCAP_BBLKMGMT	= 0x1,
	NVM_UD_DCAP_ECC		= 0x2,
149 150 151 152 153 154 155 156 157 158
};

struct nvm_id_lp_mlc {
	u16	num_pairs;
	u8	pairs[886];
};

struct nvm_id_lp_tbl {
	__u8	id[8];
	struct nvm_id_lp_mlc mlc;
159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178
};

struct nvm_id_group {
	u8	mtype;
	u8	fmtype;
	u8	num_ch;
	u8	num_lun;
	u8	num_pln;
	u16	num_blk;
	u16	num_pg;
	u16	fpg_sz;
	u16	csecs;
	u16	sos;
	u32	trdt;
	u32	trdm;
	u32	tprt;
	u32	tprm;
	u32	tbet;
	u32	tbem;
	u32	mpos;
179
	u32	mccap;
180
	u16	cpar;
181 182

	struct nvm_id_lp_tbl lptbl;
183
};
184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205

struct nvm_addr_format {
	u8	ch_offset;
	u8	ch_len;
	u8	lun_offset;
	u8	lun_len;
	u8	pln_offset;
	u8	pln_len;
	u8	blk_offset;
	u8	blk_len;
	u8	pg_offset;
	u8	pg_len;
	u8	sect_offset;
	u8	sect_len;
};

struct nvm_id {
	u8	ver_id;
	u8	vmnt;
	u32	cap;
	u32	dom;
	struct nvm_addr_format ppaf;
206
	struct nvm_id_group grp;
207 208 209 210
} __packed;

struct nvm_target {
	struct list_head list;
211
	struct nvm_tgt_dev *dev;
212 213 214 215 216 217 218 219 220 221
	struct nvm_tgt_type *type;
	struct gendisk *disk;
};

#define ADDR_EMPTY (~0ULL)

#define NVM_VERSION_MAJOR 1
#define NVM_VERSION_MINOR 0
#define NVM_VERSION_PATCH 0

222
struct nvm_rq;
223
typedef void (nvm_end_io_fn)(struct nvm_rq *);
224

225
struct nvm_rq {
226
	struct nvm_tgt_dev *dev;
227 228 229 230 231 232 233 234 235 236

	struct bio *bio;

	union {
		struct ppa_addr ppa_addr;
		dma_addr_t dma_ppa_list;
	};

	struct ppa_addr *ppa_list;

237 238
	void *meta_list;
	dma_addr_t dma_meta_list;
239

240 241 242
	struct completion *wait;
	nvm_end_io_fn *end_io;

243
	uint8_t opcode;
244
	uint16_t nr_ppas;
245
	uint16_t flags;
246

247
	u64 ppa_status; /* ppa media status */
248
	int error;
249 250

	void *private;
251 252 253 254 255 256 257 258 259 260 261 262
};

static inline struct nvm_rq *nvm_rq_from_pdu(void *pdu)
{
	return pdu - sizeof(struct nvm_rq);
}

static inline void *nvm_rq_to_pdu(struct nvm_rq *rqdata)
{
	return rqdata + 1;
}

263 264
enum {
	NVM_BLK_ST_FREE =	0x1,	/* Free block */
265
	NVM_BLK_ST_TGT =	0x2,	/* Block in use by target */
266
	NVM_BLK_ST_BAD =	0x8,	/* Bad block */
267 268
};

269 270
/* Device generic information */
struct nvm_geo {
271
	int nr_chnls;
272 273
	int nr_luns;
	int luns_per_chnl; /* -1 if channels are not symmetric */
274 275 276 277
	int nr_planes;
	int sec_per_pg; /* only sectors for a single page */
	int pgs_per_blk;
	int blks_per_lun;
278 279
	int fpg_size;
	int pfpg_size; /* size of buffer if all pages are to be read */
280 281
	int sec_size;
	int oob_size;
M
Matias Bjørling 已提交
282
	int mccap;
283
	struct nvm_addr_format ppaf;
284 285 286 287 288 289 290 291 292 293

	/* Calculated/Cached values. These do not reflect the actual usable
	 * blocks at run-time.
	 */
	int max_rq_size;
	int plane_mode; /* drive device in single, double or quad mode */

	int sec_per_pl; /* all sectors across planes */
	int sec_per_blk;
	int sec_per_lun;
294 295
};

296
/* sub-device structure */
297 298 299 300
struct nvm_tgt_dev {
	/* Device information */
	struct nvm_geo geo;

301 302 303
	/* Base ppas for target LUNs */
	struct ppa_addr *luns;

304 305 306 307 308
	sector_t total_secs;

	struct nvm_id identity;
	struct request_queue *q;

309
	struct nvm_dev *parent;
310
	void *map;
311 312 313 314 315 316 317 318 319
};

struct nvm_dev {
	struct nvm_dev_ops *ops;

	struct list_head devices;

	/* Device information */
	struct nvm_geo geo;
320

321
	  /* lower page table */
322 323 324
	int lps_per_blk;
	int *lptbl;

325
	unsigned long total_secs;
326

W
Wenwei Tao 已提交
327
	unsigned long *lun_map;
328
	void *dma_pool;
329 330 331 332 333 334

	struct nvm_id identity;

	/* Backend device */
	struct request_queue *q;
	char name[DISK_NAME_LEN];
335
	void *private_data;
336

337 338
	void *rmap;

339
	struct mutex mlock;
340
	spinlock_t lock;
341 342 343 344

	/* target management */
	struct list_head area_list;
	struct list_head targets;
345 346
};

347
static inline struct ppa_addr linear_to_generic_addr(struct nvm_geo *geo,
348
						     u64 pba)
349 350 351
{
	struct ppa_addr l;
	int secs, pgs, blks, luns;
352
	sector_t ppa = pba;
353 354 355

	l.ppa = 0;

356
	div_u64_rem(ppa, geo->sec_per_pg, &secs);
357 358
	l.g.sec = secs;

359 360
	sector_div(ppa, geo->sec_per_pg);
	div_u64_rem(ppa, geo->pgs_per_blk, &pgs);
361 362
	l.g.pg = pgs;

363 364
	sector_div(ppa, geo->pgs_per_blk);
	div_u64_rem(ppa, geo->blks_per_lun, &blks);
365 366
	l.g.blk = blks;

367 368
	sector_div(ppa, geo->blks_per_lun);
	div_u64_rem(ppa, geo->luns_per_chnl, &luns);
369 370
	l.g.lun = luns;

371
	sector_div(ppa, geo->luns_per_chnl);
372 373 374 375 376
	l.g.ch = ppa;

	return l;
}

377 378
static inline struct ppa_addr generic_to_dev_addr(struct nvm_tgt_dev *tgt_dev,
						  struct ppa_addr r)
379
{
380
	struct nvm_geo *geo = &tgt_dev->geo;
381 382
	struct ppa_addr l;

383 384 385 386 387 388
	l.ppa = ((u64)r.g.blk) << geo->ppaf.blk_offset;
	l.ppa |= ((u64)r.g.pg) << geo->ppaf.pg_offset;
	l.ppa |= ((u64)r.g.sec) << geo->ppaf.sect_offset;
	l.ppa |= ((u64)r.g.pl) << geo->ppaf.pln_offset;
	l.ppa |= ((u64)r.g.lun) << geo->ppaf.lun_offset;
	l.ppa |= ((u64)r.g.ch) << geo->ppaf.ch_offset;
389 390 391 392

	return l;
}

393 394
static inline struct ppa_addr dev_to_generic_addr(struct nvm_tgt_dev *tgt_dev,
						  struct ppa_addr r)
395
{
396
	struct nvm_geo *geo = &tgt_dev->geo;
397 398
	struct ppa_addr l;

399
	l.ppa = 0;
400 401 402
	/*
	 * (r.ppa << X offset) & X len bitmask. X eq. blk, pg, etc.
	 */
403 404 405 406 407 408 409 410 411 412 413 414
	l.g.blk = (r.ppa >> geo->ppaf.blk_offset) &
					(((1 << geo->ppaf.blk_len) - 1));
	l.g.pg |= (r.ppa >> geo->ppaf.pg_offset) &
					(((1 << geo->ppaf.pg_len) - 1));
	l.g.sec |= (r.ppa >> geo->ppaf.sect_offset) &
					(((1 << geo->ppaf.sect_len) - 1));
	l.g.pl |= (r.ppa >> geo->ppaf.pln_offset) &
					(((1 << geo->ppaf.pln_len) - 1));
	l.g.lun |= (r.ppa >> geo->ppaf.lun_offset) &
					(((1 << geo->ppaf.lun_len) - 1));
	l.g.ch |= (r.ppa >> geo->ppaf.ch_offset) &
					(((1 << geo->ppaf.ch_len) - 1));
415 416 417 418 419 420 421 422 423 424 425 426 427 428

	return l;
}

static inline int ppa_empty(struct ppa_addr ppa_addr)
{
	return (ppa_addr.ppa == ADDR_EMPTY);
}

static inline void ppa_set_empty(struct ppa_addr *ppa_addr)
{
	ppa_addr->ppa = ADDR_EMPTY;
}

429 430 431 432 433 434 435 436 437
static inline int ppa_cmp_blk(struct ppa_addr ppa1, struct ppa_addr ppa2)
{
	if (ppa_empty(ppa1) || ppa_empty(ppa2))
		return 0;

	return ((ppa1.g.ch == ppa2.g.ch) && (ppa1.g.lun == ppa2.g.lun) &&
					(ppa1.g.blk == ppa2.g.blk));
}

438
typedef blk_qc_t (nvm_tgt_make_rq_fn)(struct request_queue *, struct bio *);
439
typedef sector_t (nvm_tgt_capacity_fn)(void *);
440
typedef void *(nvm_tgt_init_fn)(struct nvm_tgt_dev *, struct gendisk *);
441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458
typedef void (nvm_tgt_exit_fn)(void *);

struct nvm_tgt_type {
	const char *name;
	unsigned int version[3];

	/* target entry points */
	nvm_tgt_make_rq_fn *make_rq;
	nvm_tgt_capacity_fn *capacity;

	/* module-specific init/teardown */
	nvm_tgt_init_fn *init;
	nvm_tgt_exit_fn *exit;

	/* For internal use */
	struct list_head list;
};

459 460
extern struct nvm_tgt_type *nvm_find_target_type(const char *, int);

461 462
extern int nvm_register_tgt_type(struct nvm_tgt_type *);
extern void nvm_unregister_tgt_type(struct nvm_tgt_type *);
463 464 465 466

extern void *nvm_dev_dma_alloc(struct nvm_dev *, gfp_t, dma_addr_t *);
extern void nvm_dev_dma_free(struct nvm_dev *, void *, dma_addr_t);

467 468 469
extern struct nvm_dev *nvm_alloc_dev(int);
extern int nvm_register(struct nvm_dev *);
extern void nvm_unregister(struct nvm_dev *);
470

471 472
extern int nvm_set_tgt_bb_tbl(struct nvm_tgt_dev *, struct ppa_addr *,
			      int, int);
473
extern int nvm_max_phys_sects(struct nvm_tgt_dev *);
474
extern int nvm_submit_io(struct nvm_tgt_dev *, struct nvm_rq *);
475
extern int nvm_set_rqd_ppalist(struct nvm_dev *, struct nvm_rq *,
476
					const struct ppa_addr *, int, int);
477
extern void nvm_free_rqd_ppalist(struct nvm_dev *, struct nvm_rq *);
478
extern int nvm_erase_blk(struct nvm_tgt_dev *, struct ppa_addr *, int);
479
extern int nvm_get_l2p_tbl(struct nvm_tgt_dev *, u64, u32, nvm_l2p_update_fn *,
480
			   void *);
481 482
extern int nvm_get_area(struct nvm_tgt_dev *, sector_t *, sector_t);
extern void nvm_put_area(struct nvm_tgt_dev *, sector_t);
483
extern void nvm_end_io(struct nvm_rq *);
484
extern int nvm_bb_tbl_fold(struct nvm_dev *, u8 *, int);
485
extern int nvm_get_tgt_bb_tbl(struct nvm_tgt_dev *, struct ppa_addr, u8 *);
486

487
extern int nvm_dev_factory(struct nvm_dev *, int flags);
488

489
extern void nvm_part_to_tgt(struct nvm_dev *, sector_t *, int);
490

491 492 493
#else /* CONFIG_NVM */
struct nvm_dev_ops;

494 495 496 497 498
static inline struct nvm_dev *nvm_alloc_dev(int node)
{
	return ERR_PTR(-EINVAL);
}
static inline int nvm_register(struct nvm_dev *dev)
499 500 501
{
	return -EINVAL;
}
502
static inline void nvm_unregister(struct nvm_dev *dev) {}
503 504
#endif /* CONFIG_NVM */
#endif /* LIGHTNVM.H */