xref: /openbmc/linux/drivers/nvdimm/btt.h (revision f220d3eb)
1 /*
2  * Block Translation Table library
3  * Copyright (c) 2014-2015, Intel Corporation.
4  *
5  * This program is free software; you can redistribute it and/or modify it
6  * under the terms and conditions of the GNU General Public License,
7  * version 2, as published by the Free Software Foundation.
8  *
9  * This program is distributed in the hope it will be useful, but WITHOUT
10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
12  * more details.
13  */
14 
15 #ifndef _LINUX_BTT_H
16 #define _LINUX_BTT_H
17 
18 #include <linux/badblocks.h>
19 #include <linux/types.h>
20 
21 #define BTT_SIG_LEN 16
22 #define BTT_SIG "BTT_ARENA_INFO\0"
23 #define MAP_ENT_SIZE 4
24 #define MAP_TRIM_SHIFT 31
25 #define MAP_TRIM_MASK (1 << MAP_TRIM_SHIFT)
26 #define MAP_ERR_SHIFT 30
27 #define MAP_ERR_MASK (1 << MAP_ERR_SHIFT)
28 #define MAP_LBA_MASK (~((1 << MAP_TRIM_SHIFT) | (1 << MAP_ERR_SHIFT)))
29 #define MAP_ENT_NORMAL 0xC0000000
30 #define LOG_GRP_SIZE sizeof(struct log_group)
31 #define LOG_ENT_SIZE sizeof(struct log_entry)
32 #define ARENA_MIN_SIZE (1UL << 24)	/* 16 MB */
33 #define ARENA_MAX_SIZE (1ULL << 39)	/* 512 GB */
34 #define RTT_VALID (1UL << 31)
35 #define RTT_INVALID 0
36 #define BTT_PG_SIZE 4096
37 #define BTT_DEFAULT_NFREE ND_MAX_LANES
38 #define LOG_SEQ_INIT 1
39 
40 #define IB_FLAG_ERROR 0x00000001
41 #define IB_FLAG_ERROR_MASK 0x00000001
42 
43 #define ent_lba(ent) (ent & MAP_LBA_MASK)
44 #define ent_e_flag(ent) (!!(ent & MAP_ERR_MASK))
45 #define ent_z_flag(ent) (!!(ent & MAP_TRIM_MASK))
46 #define set_e_flag(ent) (ent |= MAP_ERR_MASK)
47 
48 enum btt_init_state {
49 	INIT_UNCHECKED = 0,
50 	INIT_NOTFOUND,
51 	INIT_READY
52 };
53 
54 /*
55  * A log group represents one log 'lane', and consists of four log entries.
56  * Two of the four entries are valid entries, and the remaining two are
57  * padding. Due to an old bug in the padding location, we need to perform a
58  * test to determine the padding scheme being used, and use that scheme
59  * thereafter.
60  *
61  * In kernels prior to 4.15, 'log group' would have actual log entries at
62  * indices (0, 2) and padding at indices (1, 3), where as the correct/updated
63  * format has log entries at indices (0, 1) and padding at indices (2, 3).
64  *
65  * Old (pre 4.15) format:
66  * +-----------------+-----------------+
67  * |      ent[0]     |      ent[1]     |
68  * |       16B       |       16B       |
69  * | lba/old/new/seq |       pad       |
70  * +-----------------------------------+
71  * |      ent[2]     |      ent[3]     |
72  * |       16B       |       16B       |
73  * | lba/old/new/seq |       pad       |
74  * +-----------------+-----------------+
75  *
76  * New format:
77  * +-----------------+-----------------+
78  * |      ent[0]     |      ent[1]     |
79  * |       16B       |       16B       |
80  * | lba/old/new/seq | lba/old/new/seq |
81  * +-----------------------------------+
82  * |      ent[2]     |      ent[3]     |
83  * |       16B       |       16B       |
84  * |       pad       |       pad       |
85  * +-----------------+-----------------+
86  *
87  * We detect during start-up which format is in use, and set
88  * arena->log_index[(0, 1)] with the detected format.
89  */
90 
91 struct log_entry {
92 	__le32 lba;
93 	__le32 old_map;
94 	__le32 new_map;
95 	__le32 seq;
96 };
97 
98 struct log_group {
99 	struct log_entry ent[4];
100 };
101 
102 struct btt_sb {
103 	u8 signature[BTT_SIG_LEN];
104 	u8 uuid[16];
105 	u8 parent_uuid[16];
106 	__le32 flags;
107 	__le16 version_major;
108 	__le16 version_minor;
109 	__le32 external_lbasize;
110 	__le32 external_nlba;
111 	__le32 internal_lbasize;
112 	__le32 internal_nlba;
113 	__le32 nfree;
114 	__le32 infosize;
115 	__le64 nextoff;
116 	__le64 dataoff;
117 	__le64 mapoff;
118 	__le64 logoff;
119 	__le64 info2off;
120 	u8 padding[3968];
121 	__le64 checksum;
122 };
123 
124 struct free_entry {
125 	u32 block;
126 	u8 sub;
127 	u8 seq;
128 	u8 has_err;
129 };
130 
131 struct aligned_lock {
132 	union {
133 		spinlock_t lock;
134 		u8 cacheline_padding[L1_CACHE_BYTES];
135 	};
136 };
137 
138 /**
139  * struct arena_info - handle for an arena
140  * @size:		Size in bytes this arena occupies on the raw device.
141  *			This includes arena metadata.
142  * @external_lba_start:	The first external LBA in this arena.
143  * @internal_nlba:	Number of internal blocks available in the arena
144  *			including nfree reserved blocks
145  * @internal_lbasize:	Internal and external lba sizes may be different as
146  *			we can round up 'odd' external lbasizes such as 520B
147  *			to be aligned.
148  * @external_nlba:	Number of blocks contributed by the arena to the number
149  *			reported to upper layers. (internal_nlba - nfree)
150  * @external_lbasize:	LBA size as exposed to upper layers.
151  * @nfree:		A reserve number of 'free' blocks that is used to
152  *			handle incoming writes.
153  * @version_major:	Metadata layout version major.
154  * @version_minor:	Metadata layout version minor.
155  * @sector_size:	The Linux sector size - 512 or 4096
156  * @nextoff:		Offset in bytes to the start of the next arena.
157  * @infooff:		Offset in bytes to the info block of this arena.
158  * @dataoff:		Offset in bytes to the data area of this arena.
159  * @mapoff:		Offset in bytes to the map area of this arena.
160  * @logoff:		Offset in bytes to the log area of this arena.
161  * @info2off:		Offset in bytes to the backup info block of this arena.
162  * @freelist:		Pointer to in-memory list of free blocks
163  * @rtt:		Pointer to in-memory "Read Tracking Table"
164  * @map_locks:		Spinlocks protecting concurrent map writes
165  * @nd_btt:		Pointer to parent nd_btt structure.
166  * @list:		List head for list of arenas
167  * @debugfs_dir:	Debugfs dentry
168  * @flags:		Arena flags - may signify error states.
169  * @err_lock:		Mutex for synchronizing error clearing.
170  * @log_index:		Indices of the valid log entries in a log_group
171  *
172  * arena_info is a per-arena handle. Once an arena is narrowed down for an
173  * IO, this struct is passed around for the duration of the IO.
174  */
175 struct arena_info {
176 	u64 size;			/* Total bytes for this arena */
177 	u64 external_lba_start;
178 	u32 internal_nlba;
179 	u32 internal_lbasize;
180 	u32 external_nlba;
181 	u32 external_lbasize;
182 	u32 nfree;
183 	u16 version_major;
184 	u16 version_minor;
185 	u32 sector_size;
186 	/* Byte offsets to the different on-media structures */
187 	u64 nextoff;
188 	u64 infooff;
189 	u64 dataoff;
190 	u64 mapoff;
191 	u64 logoff;
192 	u64 info2off;
193 	/* Pointers to other in-memory structures for this arena */
194 	struct free_entry *freelist;
195 	u32 *rtt;
196 	struct aligned_lock *map_locks;
197 	struct nd_btt *nd_btt;
198 	struct list_head list;
199 	struct dentry *debugfs_dir;
200 	/* Arena flags */
201 	u32 flags;
202 	struct mutex err_lock;
203 	int log_index[2];
204 };
205 
206 /**
207  * struct btt - handle for a BTT instance
208  * @btt_disk:		Pointer to the gendisk for BTT device
209  * @btt_queue:		Pointer to the request queue for the BTT device
210  * @arena_list:		Head of the list of arenas
211  * @debugfs_dir:	Debugfs dentry
212  * @nd_btt:		Parent nd_btt struct
213  * @nlba:		Number of logical blocks exposed to the	upper layers
214  *			after removing the amount of space needed by metadata
215  * @rawsize:		Total size in bytes of the available backing device
216  * @lbasize:		LBA size as requested and presented to upper layers.
217  *			This is sector_size + size of any metadata.
218  * @sector_size:	The Linux sector size - 512 or 4096
219  * @lanes:		Per-lane spinlocks
220  * @init_lock:		Mutex used for the BTT initialization
221  * @init_state:		Flag describing the initialization state for the BTT
222  * @num_arenas:		Number of arenas in the BTT instance
223  * @phys_bb:		Pointer to the namespace's badblocks structure
224  */
225 struct btt {
226 	struct gendisk *btt_disk;
227 	struct request_queue *btt_queue;
228 	struct list_head arena_list;
229 	struct dentry *debugfs_dir;
230 	struct nd_btt *nd_btt;
231 	u64 nlba;
232 	unsigned long long rawsize;
233 	u32 lbasize;
234 	u32 sector_size;
235 	struct nd_region *nd_region;
236 	struct mutex init_lock;
237 	int init_state;
238 	int num_arenas;
239 	struct badblocks *phys_bb;
240 };
241 
242 bool nd_btt_arena_is_valid(struct nd_btt *nd_btt, struct btt_sb *super);
243 int nd_btt_version(struct nd_btt *nd_btt, struct nd_namespace_common *ndns,
244 		struct btt_sb *btt_sb);
245 
246 #endif
247