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