lightnvm.h 14.3 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 50 51 52 53 54
		u64 ppa;
	};
};

struct nvm_rq;
struct nvm_id;
struct nvm_dev;

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 *);
55
typedef int (nvm_op_bb_tbl_fn)(struct nvm_dev *, struct ppa_addr, u8 *);
56
typedef int (nvm_op_set_bb_fn)(struct nvm_dev *, struct ppa_addr *, int, int);
57 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 83
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;
};



84 85 86 87 88
#ifdef CONFIG_NVM

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

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 */
101 102 103
	NVM_PLANE_SINGLE	= 1,
	NVM_PLANE_DOUBLE	= 2,
	NVM_PLANE_QUAD		= 4,
104 105 106 107 108 109

	/* Status codes */
	NVM_RSP_SUCCESS		= 0x0,
	NVM_RSP_NOT_CHANGEABLE	= 0x1,
	NVM_RSP_ERR_FAILWRITE	= 0x40ff,
	NVM_RSP_ERR_EMPTYPAGE	= 0x42ff,
110 111
	NVM_RSP_ERR_FAILECC	= 0x4281,
	NVM_RSP_WARN_HIGHECC	= 0x4700,
112 113 114 115 116 117 118 119 120 121 122 123 124

	/* 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,

125
	/* NAND Access Modes */
126 127 128
	NVM_IO_SUSPEND		= 0x80,
	NVM_IO_SLC_MODE		= 0x100,
	NVM_IO_SCRAMBLE_DISABLE	= 0x200,
129 130 131 132

	/* Block Types */
	NVM_BLK_T_FREE		= 0x0,
	NVM_BLK_T_BAD		= 0x1,
133 134 135
	NVM_BLK_T_GRWN_BAD	= 0x2,
	NVM_BLK_T_DEV		= 0x4,
	NVM_BLK_T_HOST		= 0x8,
136 137 138 139 140 141

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

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

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

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;
160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179
};

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;
180
	u32	mccap;
181
	u16	cpar;
182 183

	struct nvm_id_lp_tbl lptbl;
184
};
185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212

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;
	u8	cgrps;
	u32	cap;
	u32	dom;
	struct nvm_addr_format ppaf;
	struct nvm_id_group groups[4];
} __packed;

struct nvm_target {
	struct list_head list;
213
	struct nvm_dev *dev;
214 215 216 217 218 219 220 221 222 223 224 225 226 227
	struct nvm_tgt_type *type;
	struct gendisk *disk;
};

struct nvm_tgt_instance {
	struct nvm_tgt_type *tt;
};

#define ADDR_EMPTY (~0ULL)

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

228
struct nvm_rq;
229
typedef void (nvm_end_io_fn)(struct nvm_rq *);
230

231 232 233 234 235 236 237 238 239 240 241 242 243
struct nvm_rq {
	struct nvm_tgt_instance *ins;
	struct nvm_dev *dev;

	struct bio *bio;

	union {
		struct ppa_addr ppa_addr;
		dma_addr_t dma_ppa_list;
	};

	struct ppa_addr *ppa_list;

244 245
	void *meta_list;
	dma_addr_t dma_meta_list;
246

247 248 249
	struct completion *wait;
	nvm_end_io_fn *end_io;

250
	uint8_t opcode;
251
	uint16_t nr_ppas;
252
	uint16_t flags;
253

254
	u64 ppa_status; /* ppa media status */
255
	int error;
256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276
};

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;
}

struct nvm_block;

struct nvm_lun {
	int id;

	int lun_id;
	int chnl_id;

	spinlock_t lock;
277

278
	unsigned int nr_free_blocks;	/* Number of unused blocks */
279 280 281 282 283
	struct nvm_block *blocks;
};

enum {
	NVM_BLK_ST_FREE =	0x1,	/* Free block */
284
	NVM_BLK_ST_TGT =	0x2,	/* Block in use by target */
285
	NVM_BLK_ST_BAD =	0x8,	/* Bad block */
286 287 288 289 290 291 292 293
};

struct nvm_block {
	struct list_head list;
	struct nvm_lun *lun;
	unsigned long id;

	void *priv;
294
	int state;
295 296
};

297 298 299 300 301 302 303 304 305
/* system block cpu representation */
struct nvm_sb_info {
	unsigned long		seqnr;
	unsigned long		erase_cnt;
	unsigned int		version;
	char			mmtype[NVM_MMTYPE_LEN];
	struct ppa_addr		fs_ppa;
};

306 307 308 309 310 311 312 313 314
struct nvm_dev {
	struct nvm_dev_ops *ops;

	struct list_head devices;

	/* Media manager */
	struct nvmm_type *mt;
	void *mp;

315 316 317
	/* System blocks */
	struct nvm_sb_info sb;

318 319 320 321 322 323 324
	/* Device information */
	int nr_chnls;
	int nr_planes;
	int luns_per_chnl;
	int sec_per_pg; /* only sectors for a single page */
	int pgs_per_blk;
	int blks_per_lun;
325 326
	int fpg_size;
	int pfpg_size; /* size of buffer if all pages are to be read */
327 328
	int sec_size;
	int oob_size;
329
	int mccap;
330
	struct nvm_addr_format ppaf;
331 332 333 334 335 336 337 338 339 340 341

	/* 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;

342 343 344 345
	/* lower page table */
	int lps_per_blk;
	int *lptbl;

346
	unsigned long total_blocks;
347
	unsigned long total_secs;
348 349
	int nr_luns;

350
	unsigned long *lun_map;
351
	void *dma_pool;
352 353 354 355 356 357

	struct nvm_id identity;

	/* Backend device */
	struct request_queue *q;
	char name[DISK_NAME_LEN];
358
	void *private_data;
359 360

	struct mutex mlock;
361
	spinlock_t lock;
362 363
};

364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393
static inline struct ppa_addr linear_to_generic_addr(struct nvm_dev *dev,
							struct ppa_addr r)
{
	struct ppa_addr l;
	int secs, pgs, blks, luns;
	sector_t ppa = r.ppa;

	l.ppa = 0;

	div_u64_rem(ppa, dev->sec_per_pg, &secs);
	l.g.sec = secs;

	sector_div(ppa, dev->sec_per_pg);
	div_u64_rem(ppa, dev->pgs_per_blk, &pgs);
	l.g.pg = pgs;

	sector_div(ppa, dev->pgs_per_blk);
	div_u64_rem(ppa, dev->blks_per_lun, &blks);
	l.g.blk = blks;

	sector_div(ppa, dev->blks_per_lun);
	div_u64_rem(ppa, dev->luns_per_chnl, &luns);
	l.g.lun = luns;

	sector_div(ppa, dev->luns_per_chnl);
	l.g.ch = ppa;

	return l;
}

394 395
static inline struct ppa_addr generic_to_dev_addr(struct nvm_dev *dev,
						struct ppa_addr r)
396 397 398
{
	struct ppa_addr l;

399 400 401 402 403 404
	l.ppa = ((u64)r.g.blk) << dev->ppaf.blk_offset;
	l.ppa |= ((u64)r.g.pg) << dev->ppaf.pg_offset;
	l.ppa |= ((u64)r.g.sec) << dev->ppaf.sect_offset;
	l.ppa |= ((u64)r.g.pl) << dev->ppaf.pln_offset;
	l.ppa |= ((u64)r.g.lun) << dev->ppaf.lun_offset;
	l.ppa |= ((u64)r.g.ch) << dev->ppaf.ch_offset;
405 406 407 408

	return l;
}

409 410
static inline struct ppa_addr dev_to_generic_addr(struct nvm_dev *dev,
						struct ppa_addr r)
411 412 413
{
	struct ppa_addr l;

414
	l.ppa = 0;
415 416 417 418 419 420 421 422 423 424 425 426 427 428 429
	/*
	 * (r.ppa << X offset) & X len bitmask. X eq. blk, pg, etc.
	 */
	l.g.blk = (r.ppa >> dev->ppaf.blk_offset) &
					(((1 << dev->ppaf.blk_len) - 1));
	l.g.pg |= (r.ppa >> dev->ppaf.pg_offset) &
					(((1 << dev->ppaf.pg_len) - 1));
	l.g.sec |= (r.ppa >> dev->ppaf.sect_offset) &
					(((1 << dev->ppaf.sect_len) - 1));
	l.g.pl |= (r.ppa >> dev->ppaf.pln_offset) &
					(((1 << dev->ppaf.pln_len) - 1));
	l.g.lun |= (r.ppa >> dev->ppaf.lun_offset) &
					(((1 << dev->ppaf.lun_len) - 1));
	l.g.ch |= (r.ppa >> dev->ppaf.ch_offset) &
					(((1 << dev->ppaf.ch_len) - 1));
430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457

	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;
}

static inline struct ppa_addr block_to_ppa(struct nvm_dev *dev,
							struct nvm_block *blk)
{
	struct ppa_addr ppa;
	struct nvm_lun *lun = blk->lun;

	ppa.ppa = 0;
	ppa.g.blk = blk->id % dev->blks_per_lun;
	ppa.g.lun = lun->lun_id;
	ppa.g.ch = lun->chnl_id;

	return ppa;
}

458 459 460 461 462 463 464 465 466
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));
}

467 468 469 470 471
static inline int ppa_to_slc(struct nvm_dev *dev, int slc_pg)
{
	return dev->lptbl[slc_pg];
}

472
typedef blk_qc_t (nvm_tgt_make_rq_fn)(struct request_queue *, struct bio *);
473 474 475 476 477 478 479 480 481 482 483
typedef sector_t (nvm_tgt_capacity_fn)(void *);
typedef void *(nvm_tgt_init_fn)(struct nvm_dev *, struct gendisk *, int, int);
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;
484
	nvm_end_io_fn *end_io;
485 486 487 488 489 490 491 492 493

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

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

494 495
extern struct nvm_tgt_type *nvm_find_target_type(const char *, int);

496 497
extern int nvm_register_tgt_type(struct nvm_tgt_type *);
extern void nvm_unregister_tgt_type(struct nvm_tgt_type *);
498 499 500 501 502 503

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);

typedef int (nvmm_register_fn)(struct nvm_dev *);
typedef void (nvmm_unregister_fn)(struct nvm_dev *);
504 505 506

typedef int (nvmm_create_tgt_fn)(struct nvm_dev *, struct nvm_ioctl_create *);
typedef int (nvmm_remove_tgt_fn)(struct nvm_dev *, struct nvm_ioctl_remove *);
507 508 509 510
typedef struct nvm_block *(nvmm_get_blk_fn)(struct nvm_dev *,
					      struct nvm_lun *, unsigned long);
typedef void (nvmm_put_blk_fn)(struct nvm_dev *, struct nvm_block *);
typedef int (nvmm_submit_io_fn)(struct nvm_dev *, struct nvm_rq *);
511
typedef int (nvmm_erase_blk_fn)(struct nvm_dev *, struct nvm_block *, int);
512
typedef void (nvmm_mark_blk_fn)(struct nvm_dev *, struct ppa_addr, int);
513
typedef struct nvm_lun *(nvmm_get_lun_fn)(struct nvm_dev *, int);
514 515
typedef int (nvmm_reserve_lun)(struct nvm_dev *, int);
typedef void (nvmm_release_lun)(struct nvm_dev *, int);
516
typedef void (nvmm_lun_info_print_fn)(struct nvm_dev *);
517

518 519 520
typedef int (nvmm_get_area_fn)(struct nvm_dev *, sector_t *, sector_t);
typedef void (nvmm_put_area_fn)(struct nvm_dev *, sector_t);

521 522 523 524 525 526 527
struct nvmm_type {
	const char *name;
	unsigned int version[3];

	nvmm_register_fn *register_mgr;
	nvmm_unregister_fn *unregister_mgr;

528 529 530
	nvmm_create_tgt_fn *create_tgt;
	nvmm_remove_tgt_fn *remove_tgt;

531 532 533 534 535 536 537
	/* Block administration callbacks */
	nvmm_get_blk_fn *get_blk;
	nvmm_put_blk_fn *put_blk;

	nvmm_submit_io_fn *submit_io;
	nvmm_erase_blk_fn *erase_blk;

538 539 540
	/* Bad block mgmt */
	nvmm_mark_blk_fn *mark_blk;

541 542
	/* Configuration management */
	nvmm_get_lun_fn *get_lun;
543 544
	nvmm_reserve_lun *reserve_lun;
	nvmm_release_lun *release_lun;
545 546

	/* Statistics */
547
	nvmm_lun_info_print_fn *lun_info_print;
548 549 550 551

	nvmm_get_area_fn *get_area;
	nvmm_put_area_fn *put_area;

552 553 554 555 556 557 558 559 560 561
	struct list_head list;
};

extern int nvm_register_mgr(struct nvmm_type *);
extern void nvm_unregister_mgr(struct nvmm_type *);

extern struct nvm_block *nvm_get_blk(struct nvm_dev *, struct nvm_lun *,
								unsigned long);
extern void nvm_put_blk(struct nvm_dev *, struct nvm_block *);

562 563 564
extern struct nvm_dev *nvm_alloc_dev(int);
extern int nvm_register(struct nvm_dev *);
extern void nvm_unregister(struct nvm_dev *);
565

566 567 568
extern void nvm_mark_blk(struct nvm_dev *dev, struct ppa_addr ppa, int type);
extern int nvm_set_bb_tbl(struct nvm_dev *dev, struct ppa_addr *ppas,
							int nr_ppas, int type);
569

570
extern int nvm_submit_io(struct nvm_dev *, struct nvm_rq *);
571 572
extern void nvm_generic_to_addr_mode(struct nvm_dev *, struct nvm_rq *);
extern void nvm_addr_to_generic_mode(struct nvm_dev *, struct nvm_rq *);
573
extern int nvm_set_rqd_ppalist(struct nvm_dev *, struct nvm_rq *,
574
					const struct ppa_addr *, int, int);
575
extern void nvm_free_rqd_ppalist(struct nvm_dev *, struct nvm_rq *);
576 577
extern int nvm_erase_ppa(struct nvm_dev *, struct ppa_addr *, int, int);
extern int nvm_erase_blk(struct nvm_dev *, struct nvm_block *, int);
578
extern void nvm_end_io(struct nvm_rq *, int);
579 580
extern int nvm_submit_ppa(struct nvm_dev *, struct ppa_addr *, int, int, int,
								void *, int);
581 582
extern int nvm_submit_ppa_list(struct nvm_dev *, struct ppa_addr *, int, int,
							int, void *, int);
583
extern int nvm_bb_tbl_fold(struct nvm_dev *, u8 *, int);
584
extern int nvm_get_bb_tbl(struct nvm_dev *, struct ppa_addr, u8 *);
585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602

/* sysblk.c */
#define NVM_SYSBLK_MAGIC 0x4E564D53 /* "NVMS" */

/* system block on disk representation */
struct nvm_system_block {
	__be32			magic;		/* magic signature */
	__be32			seqnr;		/* sequence number */
	__be32			erase_cnt;	/* erase count */
	__be16			version;	/* version number */
	u8			mmtype[NVM_MMTYPE_LEN]; /* media manager name */
	__be64			fs_ppa;		/* PPA for media manager
						 * superblock */
};

extern int nvm_get_sysblock(struct nvm_dev *, struct nvm_sb_info *);
extern int nvm_update_sysblock(struct nvm_dev *, struct nvm_sb_info *);
extern int nvm_init_sysblock(struct nvm_dev *, struct nvm_sb_info *);
603 604

extern int nvm_dev_factory(struct nvm_dev *, int flags);
605 606 607 608 609 610 611

#define nvm_for_each_lun_ppa(dev, ppa, chid, lunid)			\
	for ((chid) = 0, (ppa).ppa = 0; (chid) < (dev)->nr_chnls;	\
					(chid)++, (ppa).g.ch = (chid))	\
		for ((lunid) = 0; (lunid) < (dev)->luns_per_chnl;	\
					(lunid)++, (ppa).g.lun = (lunid))

612 613 614
#else /* CONFIG_NVM */
struct nvm_dev_ops;

615 616 617 618 619
static inline struct nvm_dev *nvm_alloc_dev(int node)
{
	return ERR_PTR(-EINVAL);
}
static inline int nvm_register(struct nvm_dev *dev)
620 621 622
{
	return -EINVAL;
}
623
static inline void nvm_unregister(struct nvm_dev *dev) {}
624 625
#endif /* CONFIG_NVM */
#endif /* LIGHTNVM.H */
反馈
建议
客服 返回
顶部