xref: /linux/drivers/nvdimm/btt.h (revision 14e494542636b7a685c5bf27e695e3bb9ec3fe7d)
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 
188c2f7e86SDan Williams #include <linux/types.h>
198c2f7e86SDan Williams 
208c2f7e86SDan Williams #define BTT_SIG_LEN 16
218c2f7e86SDan Williams #define BTT_SIG "BTT_ARENA_INFO\0"
225212e11fSVishal Verma #define MAP_ENT_SIZE 4
235212e11fSVishal Verma #define MAP_TRIM_SHIFT 31
245212e11fSVishal Verma #define MAP_TRIM_MASK (1 << MAP_TRIM_SHIFT)
255212e11fSVishal Verma #define MAP_ERR_SHIFT 30
265212e11fSVishal Verma #define MAP_ERR_MASK (1 << MAP_ERR_SHIFT)
275212e11fSVishal Verma #define MAP_LBA_MASK (~((1 << MAP_TRIM_SHIFT) | (1 << MAP_ERR_SHIFT)))
285212e11fSVishal Verma #define MAP_ENT_NORMAL 0xC0000000
295212e11fSVishal Verma #define LOG_ENT_SIZE sizeof(struct log_entry)
305212e11fSVishal Verma #define ARENA_MIN_SIZE (1UL << 24)	/* 16 MB */
315212e11fSVishal Verma #define ARENA_MAX_SIZE (1ULL << 39)	/* 512 GB */
325212e11fSVishal Verma #define RTT_VALID (1UL << 31)
335212e11fSVishal Verma #define RTT_INVALID 0
345212e11fSVishal Verma #define BTT_PG_SIZE 4096
355212e11fSVishal Verma #define BTT_DEFAULT_NFREE ND_MAX_LANES
365212e11fSVishal Verma #define LOG_SEQ_INIT 1
375212e11fSVishal Verma 
385212e11fSVishal Verma #define IB_FLAG_ERROR 0x00000001
395212e11fSVishal Verma #define IB_FLAG_ERROR_MASK 0x00000001
405212e11fSVishal Verma 
415212e11fSVishal Verma enum btt_init_state {
425212e11fSVishal Verma 	INIT_UNCHECKED = 0,
435212e11fSVishal Verma 	INIT_NOTFOUND,
445212e11fSVishal Verma 	INIT_READY
455212e11fSVishal Verma };
465212e11fSVishal Verma 
475212e11fSVishal Verma struct log_entry {
485212e11fSVishal Verma 	__le32 lba;
495212e11fSVishal Verma 	__le32 old_map;
505212e11fSVishal Verma 	__le32 new_map;
515212e11fSVishal Verma 	__le32 seq;
525212e11fSVishal Verma 	__le64 padding[2];
535212e11fSVishal Verma };
548c2f7e86SDan Williams 
558c2f7e86SDan Williams struct btt_sb {
568c2f7e86SDan Williams 	u8 signature[BTT_SIG_LEN];
578c2f7e86SDan Williams 	u8 uuid[16];
588c2f7e86SDan Williams 	u8 parent_uuid[16];
598c2f7e86SDan Williams 	__le32 flags;
608c2f7e86SDan Williams 	__le16 version_major;
618c2f7e86SDan Williams 	__le16 version_minor;
628c2f7e86SDan Williams 	__le32 external_lbasize;
638c2f7e86SDan Williams 	__le32 external_nlba;
648c2f7e86SDan Williams 	__le32 internal_lbasize;
658c2f7e86SDan Williams 	__le32 internal_nlba;
668c2f7e86SDan Williams 	__le32 nfree;
678c2f7e86SDan Williams 	__le32 infosize;
688c2f7e86SDan Williams 	__le64 nextoff;
698c2f7e86SDan Williams 	__le64 dataoff;
708c2f7e86SDan Williams 	__le64 mapoff;
718c2f7e86SDan Williams 	__le64 logoff;
728c2f7e86SDan Williams 	__le64 info2off;
738c2f7e86SDan Williams 	u8 padding[3968];
748c2f7e86SDan Williams 	__le64 checksum;
758c2f7e86SDan Williams };
768c2f7e86SDan Williams 
775212e11fSVishal Verma struct free_entry {
785212e11fSVishal Verma 	u32 block;
795212e11fSVishal Verma 	u8 sub;
805212e11fSVishal Verma 	u8 seq;
815212e11fSVishal Verma };
825212e11fSVishal Verma 
835212e11fSVishal Verma struct aligned_lock {
845212e11fSVishal Verma 	union {
855212e11fSVishal Verma 		spinlock_t lock;
865212e11fSVishal Verma 		u8 cacheline_padding[L1_CACHE_BYTES];
875212e11fSVishal Verma 	};
885212e11fSVishal Verma };
895212e11fSVishal Verma 
905212e11fSVishal Verma /**
915212e11fSVishal Verma  * struct arena_info - handle for an arena
925212e11fSVishal Verma  * @size:		Size in bytes this arena occupies on the raw device.
935212e11fSVishal Verma  *			This includes arena metadata.
945212e11fSVishal Verma  * @external_lba_start:	The first external LBA in this arena.
955212e11fSVishal Verma  * @internal_nlba:	Number of internal blocks available in the arena
965212e11fSVishal Verma  *			including nfree reserved blocks
975212e11fSVishal Verma  * @internal_lbasize:	Internal and external lba sizes may be different as
985212e11fSVishal Verma  *			we can round up 'odd' external lbasizes such as 520B
995212e11fSVishal Verma  *			to be aligned.
1005212e11fSVishal Verma  * @external_nlba:	Number of blocks contributed by the arena to the number
1015212e11fSVishal Verma  *			reported to upper layers. (internal_nlba - nfree)
1025212e11fSVishal Verma  * @external_lbasize:	LBA size as exposed to upper layers.
1035212e11fSVishal Verma  * @nfree:		A reserve number of 'free' blocks that is used to
1045212e11fSVishal Verma  *			handle incoming writes.
1055212e11fSVishal Verma  * @version_major:	Metadata layout version major.
1065212e11fSVishal Verma  * @version_minor:	Metadata layout version minor.
1075212e11fSVishal Verma  * @nextoff:		Offset in bytes to the start of the next arena.
1085212e11fSVishal Verma  * @infooff:		Offset in bytes to the info block of this arena.
1095212e11fSVishal Verma  * @dataoff:		Offset in bytes to the data area of this arena.
1105212e11fSVishal Verma  * @mapoff:		Offset in bytes to the map area of this arena.
1115212e11fSVishal Verma  * @logoff:		Offset in bytes to the log area of this arena.
1125212e11fSVishal Verma  * @info2off:		Offset in bytes to the backup info block of this arena.
1135212e11fSVishal Verma  * @freelist:		Pointer to in-memory list of free blocks
1145212e11fSVishal Verma  * @rtt:		Pointer to in-memory "Read Tracking Table"
1155212e11fSVishal Verma  * @map_locks:		Spinlocks protecting concurrent map writes
1165212e11fSVishal Verma  * @nd_btt:		Pointer to parent nd_btt structure.
1175212e11fSVishal Verma  * @list:		List head for list of arenas
1185212e11fSVishal Verma  * @debugfs_dir:	Debugfs dentry
1195212e11fSVishal Verma  * @flags:		Arena flags - may signify error states.
1205212e11fSVishal Verma  *
1215212e11fSVishal Verma  * arena_info is a per-arena handle. Once an arena is narrowed down for an
1225212e11fSVishal Verma  * IO, this struct is passed around for the duration of the IO.
1235212e11fSVishal Verma  */
1245212e11fSVishal Verma struct arena_info {
1255212e11fSVishal Verma 	u64 size;			/* Total bytes for this arena */
1265212e11fSVishal Verma 	u64 external_lba_start;
1275212e11fSVishal Verma 	u32 internal_nlba;
1285212e11fSVishal Verma 	u32 internal_lbasize;
1295212e11fSVishal Verma 	u32 external_nlba;
1305212e11fSVishal Verma 	u32 external_lbasize;
1315212e11fSVishal Verma 	u32 nfree;
1325212e11fSVishal Verma 	u16 version_major;
1335212e11fSVishal Verma 	u16 version_minor;
1345212e11fSVishal Verma 	/* Byte offsets to the different on-media structures */
1355212e11fSVishal Verma 	u64 nextoff;
1365212e11fSVishal Verma 	u64 infooff;
1375212e11fSVishal Verma 	u64 dataoff;
1385212e11fSVishal Verma 	u64 mapoff;
1395212e11fSVishal Verma 	u64 logoff;
1405212e11fSVishal Verma 	u64 info2off;
1415212e11fSVishal Verma 	/* Pointers to other in-memory structures for this arena */
1425212e11fSVishal Verma 	struct free_entry *freelist;
1435212e11fSVishal Verma 	u32 *rtt;
1445212e11fSVishal Verma 	struct aligned_lock *map_locks;
1455212e11fSVishal Verma 	struct nd_btt *nd_btt;
1465212e11fSVishal Verma 	struct list_head list;
1475212e11fSVishal Verma 	struct dentry *debugfs_dir;
1485212e11fSVishal Verma 	/* Arena flags */
1495212e11fSVishal Verma 	u32 flags;
1505212e11fSVishal Verma };
1515212e11fSVishal Verma 
1525212e11fSVishal Verma /**
1535212e11fSVishal Verma  * struct btt - handle for a BTT instance
1545212e11fSVishal Verma  * @btt_disk:		Pointer to the gendisk for BTT device
1555212e11fSVishal Verma  * @btt_queue:		Pointer to the request queue for the BTT device
1565212e11fSVishal Verma  * @arena_list:		Head of the list of arenas
1575212e11fSVishal Verma  * @debugfs_dir:	Debugfs dentry
1585212e11fSVishal Verma  * @nd_btt:		Parent nd_btt struct
1595212e11fSVishal Verma  * @nlba:		Number of logical blocks exposed to the	upper layers
1605212e11fSVishal Verma  *			after removing the amount of space needed by metadata
1615212e11fSVishal Verma  * @rawsize:		Total size in bytes of the available backing device
1625212e11fSVishal Verma  * @lbasize:		LBA size as requested and presented to upper layers.
1635212e11fSVishal Verma  *			This is sector_size + size of any metadata.
1645212e11fSVishal Verma  * @sector_size:	The Linux sector size - 512 or 4096
1655212e11fSVishal Verma  * @lanes:		Per-lane spinlocks
1665212e11fSVishal Verma  * @init_lock:		Mutex used for the BTT initialization
1675212e11fSVishal Verma  * @init_state:		Flag describing the initialization state for the BTT
1685212e11fSVishal Verma  * @num_arenas:		Number of arenas in the BTT instance
1695212e11fSVishal Verma  */
1705212e11fSVishal Verma struct btt {
1715212e11fSVishal Verma 	struct gendisk *btt_disk;
1725212e11fSVishal Verma 	struct request_queue *btt_queue;
1735212e11fSVishal Verma 	struct list_head arena_list;
1745212e11fSVishal Verma 	struct dentry *debugfs_dir;
1755212e11fSVishal Verma 	struct nd_btt *nd_btt;
1765212e11fSVishal Verma 	u64 nlba;
1775212e11fSVishal Verma 	unsigned long long rawsize;
1785212e11fSVishal Verma 	u32 lbasize;
1795212e11fSVishal Verma 	u32 sector_size;
1805212e11fSVishal Verma 	struct nd_region *nd_region;
1815212e11fSVishal Verma 	struct mutex init_lock;
1825212e11fSVishal Verma 	int init_state;
1835212e11fSVishal Verma 	int num_arenas;
1845212e11fSVishal Verma };
185ab45e763SVishal Verma 
186ab45e763SVishal Verma bool nd_btt_arena_is_valid(struct nd_btt *nd_btt, struct btt_sb *super);
187*14e49454SVishal Verma int nd_btt_version(struct nd_btt *nd_btt, struct nd_namespace_common *ndns,
188*14e49454SVishal Verma 		struct btt_sb *btt_sb);
189ab45e763SVishal Verma 
1908c2f7e86SDan Williams #endif
191