xref: /linux/drivers/nvdimm/btt.h (revision 13b7954c0b8dd2d6382b4ddb5053f09e389d5c6e)
18c2f7e86SDan Williams /*
28c2f7e86SDan Williams  * Block Translation Table library
38c2f7e86SDan Williams  * Copyright (c) 2014-2015, Intel Corporation.
48c2f7e86SDan Williams  *
58c2f7e86SDan Williams  * This program is free software; you can redistribute it and/or modify it
68c2f7e86SDan Williams  * under the terms and conditions of the GNU General Public License,
78c2f7e86SDan Williams  * version 2, as published by the Free Software Foundation.
88c2f7e86SDan Williams  *
98c2f7e86SDan Williams  * This program is distributed in the hope it will be useful, but WITHOUT
108c2f7e86SDan Williams  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
118c2f7e86SDan Williams  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
128c2f7e86SDan Williams  * more details.
138c2f7e86SDan Williams  */
148c2f7e86SDan Williams 
158c2f7e86SDan Williams #ifndef _LINUX_BTT_H
168c2f7e86SDan Williams #define _LINUX_BTT_H
178c2f7e86SDan Williams 
18d9b83c75SVishal Verma #include <linux/badblocks.h>
198c2f7e86SDan Williams #include <linux/types.h>
208c2f7e86SDan Williams 
218c2f7e86SDan Williams #define BTT_SIG_LEN 16
228c2f7e86SDan Williams #define BTT_SIG "BTT_ARENA_INFO\0"
235212e11fSVishal Verma #define MAP_ENT_SIZE 4
245212e11fSVishal Verma #define MAP_TRIM_SHIFT 31
255212e11fSVishal Verma #define MAP_TRIM_MASK (1 << MAP_TRIM_SHIFT)
265212e11fSVishal Verma #define MAP_ERR_SHIFT 30
275212e11fSVishal Verma #define MAP_ERR_MASK (1 << MAP_ERR_SHIFT)
285212e11fSVishal Verma #define MAP_LBA_MASK (~((1 << MAP_TRIM_SHIFT) | (1 << MAP_ERR_SHIFT)))
295212e11fSVishal Verma #define MAP_ENT_NORMAL 0xC0000000
305212e11fSVishal Verma #define LOG_ENT_SIZE sizeof(struct log_entry)
315212e11fSVishal Verma #define ARENA_MIN_SIZE (1UL << 24)	/* 16 MB */
325212e11fSVishal Verma #define ARENA_MAX_SIZE (1ULL << 39)	/* 512 GB */
335212e11fSVishal Verma #define RTT_VALID (1UL << 31)
345212e11fSVishal Verma #define RTT_INVALID 0
355212e11fSVishal Verma #define BTT_PG_SIZE 4096
365212e11fSVishal Verma #define BTT_DEFAULT_NFREE ND_MAX_LANES
375212e11fSVishal Verma #define LOG_SEQ_INIT 1
385212e11fSVishal Verma 
395212e11fSVishal Verma #define IB_FLAG_ERROR 0x00000001
405212e11fSVishal Verma #define IB_FLAG_ERROR_MASK 0x00000001
415212e11fSVishal Verma 
420595d539SVishal Verma #define ent_lba(ent) (ent & MAP_LBA_MASK)
430595d539SVishal Verma #define ent_e_flag(ent) (!!(ent & MAP_ERR_MASK))
440595d539SVishal Verma #define ent_z_flag(ent) (!!(ent & MAP_TRIM_MASK))
45d9b83c75SVishal Verma #define set_e_flag(ent) (ent |= MAP_ERR_MASK)
460595d539SVishal Verma 
475212e11fSVishal Verma enum btt_init_state {
485212e11fSVishal Verma 	INIT_UNCHECKED = 0,
495212e11fSVishal Verma 	INIT_NOTFOUND,
505212e11fSVishal Verma 	INIT_READY
515212e11fSVishal Verma };
525212e11fSVishal Verma 
535212e11fSVishal Verma struct log_entry {
545212e11fSVishal Verma 	__le32 lba;
555212e11fSVishal Verma 	__le32 old_map;
565212e11fSVishal Verma 	__le32 new_map;
575212e11fSVishal Verma 	__le32 seq;
585212e11fSVishal Verma 	__le64 padding[2];
595212e11fSVishal Verma };
608c2f7e86SDan Williams 
618c2f7e86SDan Williams struct btt_sb {
628c2f7e86SDan Williams 	u8 signature[BTT_SIG_LEN];
638c2f7e86SDan Williams 	u8 uuid[16];
648c2f7e86SDan Williams 	u8 parent_uuid[16];
658c2f7e86SDan Williams 	__le32 flags;
668c2f7e86SDan Williams 	__le16 version_major;
678c2f7e86SDan Williams 	__le16 version_minor;
688c2f7e86SDan Williams 	__le32 external_lbasize;
698c2f7e86SDan Williams 	__le32 external_nlba;
708c2f7e86SDan Williams 	__le32 internal_lbasize;
718c2f7e86SDan Williams 	__le32 internal_nlba;
728c2f7e86SDan Williams 	__le32 nfree;
738c2f7e86SDan Williams 	__le32 infosize;
748c2f7e86SDan Williams 	__le64 nextoff;
758c2f7e86SDan Williams 	__le64 dataoff;
768c2f7e86SDan Williams 	__le64 mapoff;
778c2f7e86SDan Williams 	__le64 logoff;
788c2f7e86SDan Williams 	__le64 info2off;
798c2f7e86SDan Williams 	u8 padding[3968];
808c2f7e86SDan Williams 	__le64 checksum;
818c2f7e86SDan Williams };
828c2f7e86SDan Williams 
835212e11fSVishal Verma struct free_entry {
845212e11fSVishal Verma 	u32 block;
855212e11fSVishal Verma 	u8 sub;
865212e11fSVishal Verma 	u8 seq;
87d9b83c75SVishal Verma 	u8 has_err;
885212e11fSVishal Verma };
895212e11fSVishal Verma 
905212e11fSVishal Verma struct aligned_lock {
915212e11fSVishal Verma 	union {
925212e11fSVishal Verma 		spinlock_t lock;
935212e11fSVishal Verma 		u8 cacheline_padding[L1_CACHE_BYTES];
945212e11fSVishal Verma 	};
955212e11fSVishal Verma };
965212e11fSVishal Verma 
975212e11fSVishal Verma /**
985212e11fSVishal Verma  * struct arena_info - handle for an arena
995212e11fSVishal Verma  * @size:		Size in bytes this arena occupies on the raw device.
1005212e11fSVishal Verma  *			This includes arena metadata.
1015212e11fSVishal Verma  * @external_lba_start:	The first external LBA in this arena.
1025212e11fSVishal Verma  * @internal_nlba:	Number of internal blocks available in the arena
1035212e11fSVishal Verma  *			including nfree reserved blocks
1045212e11fSVishal Verma  * @internal_lbasize:	Internal and external lba sizes may be different as
1055212e11fSVishal Verma  *			we can round up 'odd' external lbasizes such as 520B
1065212e11fSVishal Verma  *			to be aligned.
1075212e11fSVishal Verma  * @external_nlba:	Number of blocks contributed by the arena to the number
1085212e11fSVishal Verma  *			reported to upper layers. (internal_nlba - nfree)
1095212e11fSVishal Verma  * @external_lbasize:	LBA size as exposed to upper layers.
1105212e11fSVishal Verma  * @nfree:		A reserve number of 'free' blocks that is used to
1115212e11fSVishal Verma  *			handle incoming writes.
1125212e11fSVishal Verma  * @version_major:	Metadata layout version major.
1135212e11fSVishal Verma  * @version_minor:	Metadata layout version minor.
11475892004SVishal Verma  * @sector_size:	The Linux sector size - 512 or 4096
1155212e11fSVishal Verma  * @nextoff:		Offset in bytes to the start of the next arena.
1165212e11fSVishal Verma  * @infooff:		Offset in bytes to the info block of this arena.
1175212e11fSVishal Verma  * @dataoff:		Offset in bytes to the data area of this arena.
1185212e11fSVishal Verma  * @mapoff:		Offset in bytes to the map area of this arena.
1195212e11fSVishal Verma  * @logoff:		Offset in bytes to the log area of this arena.
1205212e11fSVishal Verma  * @info2off:		Offset in bytes to the backup info block of this arena.
1215212e11fSVishal Verma  * @freelist:		Pointer to in-memory list of free blocks
1225212e11fSVishal Verma  * @rtt:		Pointer to in-memory "Read Tracking Table"
1235212e11fSVishal Verma  * @map_locks:		Spinlocks protecting concurrent map writes
1245212e11fSVishal Verma  * @nd_btt:		Pointer to parent nd_btt structure.
1255212e11fSVishal Verma  * @list:		List head for list of arenas
1265212e11fSVishal Verma  * @debugfs_dir:	Debugfs dentry
1275212e11fSVishal Verma  * @flags:		Arena flags - may signify error states.
128*13b7954cSVishal Verma  * @err_lock:		Mutex for synchronizing error clearing.
1295212e11fSVishal Verma  *
1305212e11fSVishal Verma  * arena_info is a per-arena handle. Once an arena is narrowed down for an
1315212e11fSVishal Verma  * IO, this struct is passed around for the duration of the IO.
1325212e11fSVishal Verma  */
1335212e11fSVishal Verma struct arena_info {
1345212e11fSVishal Verma 	u64 size;			/* Total bytes for this arena */
1355212e11fSVishal Verma 	u64 external_lba_start;
1365212e11fSVishal Verma 	u32 internal_nlba;
1375212e11fSVishal Verma 	u32 internal_lbasize;
1385212e11fSVishal Verma 	u32 external_nlba;
1395212e11fSVishal Verma 	u32 external_lbasize;
1405212e11fSVishal Verma 	u32 nfree;
1415212e11fSVishal Verma 	u16 version_major;
1425212e11fSVishal Verma 	u16 version_minor;
14375892004SVishal Verma 	u32 sector_size;
1445212e11fSVishal Verma 	/* Byte offsets to the different on-media structures */
1455212e11fSVishal Verma 	u64 nextoff;
1465212e11fSVishal Verma 	u64 infooff;
1475212e11fSVishal Verma 	u64 dataoff;
1485212e11fSVishal Verma 	u64 mapoff;
1495212e11fSVishal Verma 	u64 logoff;
1505212e11fSVishal Verma 	u64 info2off;
1515212e11fSVishal Verma 	/* Pointers to other in-memory structures for this arena */
1525212e11fSVishal Verma 	struct free_entry *freelist;
1535212e11fSVishal Verma 	u32 *rtt;
1545212e11fSVishal Verma 	struct aligned_lock *map_locks;
1555212e11fSVishal Verma 	struct nd_btt *nd_btt;
1565212e11fSVishal Verma 	struct list_head list;
1575212e11fSVishal Verma 	struct dentry *debugfs_dir;
1585212e11fSVishal Verma 	/* Arena flags */
1595212e11fSVishal Verma 	u32 flags;
160d9b83c75SVishal Verma 	struct mutex err_lock;
1615212e11fSVishal Verma };
1625212e11fSVishal Verma 
1635212e11fSVishal Verma /**
1645212e11fSVishal Verma  * struct btt - handle for a BTT instance
1655212e11fSVishal Verma  * @btt_disk:		Pointer to the gendisk for BTT device
1665212e11fSVishal Verma  * @btt_queue:		Pointer to the request queue for the BTT device
1675212e11fSVishal Verma  * @arena_list:		Head of the list of arenas
1685212e11fSVishal Verma  * @debugfs_dir:	Debugfs dentry
1695212e11fSVishal Verma  * @nd_btt:		Parent nd_btt struct
1705212e11fSVishal Verma  * @nlba:		Number of logical blocks exposed to the	upper layers
1715212e11fSVishal Verma  *			after removing the amount of space needed by metadata
1725212e11fSVishal Verma  * @rawsize:		Total size in bytes of the available backing device
1735212e11fSVishal Verma  * @lbasize:		LBA size as requested and presented to upper layers.
1745212e11fSVishal Verma  *			This is sector_size + size of any metadata.
1755212e11fSVishal Verma  * @sector_size:	The Linux sector size - 512 or 4096
1765212e11fSVishal Verma  * @lanes:		Per-lane spinlocks
1775212e11fSVishal Verma  * @init_lock:		Mutex used for the BTT initialization
1785212e11fSVishal Verma  * @init_state:		Flag describing the initialization state for the BTT
1795212e11fSVishal Verma  * @num_arenas:		Number of arenas in the BTT instance
180*13b7954cSVishal Verma  * @phys_bb:		Pointer to the namespace's badblocks structure
1815212e11fSVishal Verma  */
1825212e11fSVishal Verma struct btt {
1835212e11fSVishal Verma 	struct gendisk *btt_disk;
1845212e11fSVishal Verma 	struct request_queue *btt_queue;
1855212e11fSVishal Verma 	struct list_head arena_list;
1865212e11fSVishal Verma 	struct dentry *debugfs_dir;
1875212e11fSVishal Verma 	struct nd_btt *nd_btt;
1885212e11fSVishal Verma 	u64 nlba;
1895212e11fSVishal Verma 	unsigned long long rawsize;
1905212e11fSVishal Verma 	u32 lbasize;
1915212e11fSVishal Verma 	u32 sector_size;
1925212e11fSVishal Verma 	struct nd_region *nd_region;
1935212e11fSVishal Verma 	struct mutex init_lock;
1945212e11fSVishal Verma 	int init_state;
1955212e11fSVishal Verma 	int num_arenas;
196d9b83c75SVishal Verma 	struct badblocks *phys_bb;
1975212e11fSVishal Verma };
198ab45e763SVishal Verma 
199ab45e763SVishal Verma bool nd_btt_arena_is_valid(struct nd_btt *nd_btt, struct btt_sb *super);
20014e49454SVishal Verma int nd_btt_version(struct nd_btt *nd_btt, struct nd_namespace_common *ndns,
20114e49454SVishal Verma 		struct btt_sb *btt_sb);
202ab45e763SVishal Verma 
2038c2f7e86SDan Williams #endif
204