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" 22*5212e11fSVishal Verma #define MAP_ENT_SIZE 4 23*5212e11fSVishal Verma #define MAP_TRIM_SHIFT 31 24*5212e11fSVishal Verma #define MAP_TRIM_MASK (1 << MAP_TRIM_SHIFT) 25*5212e11fSVishal Verma #define MAP_ERR_SHIFT 30 26*5212e11fSVishal Verma #define MAP_ERR_MASK (1 << MAP_ERR_SHIFT) 27*5212e11fSVishal Verma #define MAP_LBA_MASK (~((1 << MAP_TRIM_SHIFT) | (1 << MAP_ERR_SHIFT))) 28*5212e11fSVishal Verma #define MAP_ENT_NORMAL 0xC0000000 29*5212e11fSVishal Verma #define LOG_ENT_SIZE sizeof(struct log_entry) 30*5212e11fSVishal Verma #define ARENA_MIN_SIZE (1UL << 24) /* 16 MB */ 31*5212e11fSVishal Verma #define ARENA_MAX_SIZE (1ULL << 39) /* 512 GB */ 32*5212e11fSVishal Verma #define RTT_VALID (1UL << 31) 33*5212e11fSVishal Verma #define RTT_INVALID 0 34*5212e11fSVishal Verma #define INT_LBASIZE_ALIGNMENT 256 35*5212e11fSVishal Verma #define BTT_PG_SIZE 4096 36*5212e11fSVishal Verma #define BTT_DEFAULT_NFREE ND_MAX_LANES 37*5212e11fSVishal Verma #define LOG_SEQ_INIT 1 38*5212e11fSVishal Verma 39*5212e11fSVishal Verma #define IB_FLAG_ERROR 0x00000001 40*5212e11fSVishal Verma #define IB_FLAG_ERROR_MASK 0x00000001 41*5212e11fSVishal Verma 42*5212e11fSVishal Verma enum btt_init_state { 43*5212e11fSVishal Verma INIT_UNCHECKED = 0, 44*5212e11fSVishal Verma INIT_NOTFOUND, 45*5212e11fSVishal Verma INIT_READY 46*5212e11fSVishal Verma }; 47*5212e11fSVishal Verma 48*5212e11fSVishal Verma struct log_entry { 49*5212e11fSVishal Verma __le32 lba; 50*5212e11fSVishal Verma __le32 old_map; 51*5212e11fSVishal Verma __le32 new_map; 52*5212e11fSVishal Verma __le32 seq; 53*5212e11fSVishal Verma __le64 padding[2]; 54*5212e11fSVishal Verma }; 558c2f7e86SDan Williams 568c2f7e86SDan Williams struct btt_sb { 578c2f7e86SDan Williams u8 signature[BTT_SIG_LEN]; 588c2f7e86SDan Williams u8 uuid[16]; 598c2f7e86SDan Williams u8 parent_uuid[16]; 608c2f7e86SDan Williams __le32 flags; 618c2f7e86SDan Williams __le16 version_major; 628c2f7e86SDan Williams __le16 version_minor; 638c2f7e86SDan Williams __le32 external_lbasize; 648c2f7e86SDan Williams __le32 external_nlba; 658c2f7e86SDan Williams __le32 internal_lbasize; 668c2f7e86SDan Williams __le32 internal_nlba; 678c2f7e86SDan Williams __le32 nfree; 688c2f7e86SDan Williams __le32 infosize; 698c2f7e86SDan Williams __le64 nextoff; 708c2f7e86SDan Williams __le64 dataoff; 718c2f7e86SDan Williams __le64 mapoff; 728c2f7e86SDan Williams __le64 logoff; 738c2f7e86SDan Williams __le64 info2off; 748c2f7e86SDan Williams u8 padding[3968]; 758c2f7e86SDan Williams __le64 checksum; 768c2f7e86SDan Williams }; 778c2f7e86SDan Williams 78*5212e11fSVishal Verma struct free_entry { 79*5212e11fSVishal Verma u32 block; 80*5212e11fSVishal Verma u8 sub; 81*5212e11fSVishal Verma u8 seq; 82*5212e11fSVishal Verma }; 83*5212e11fSVishal Verma 84*5212e11fSVishal Verma struct aligned_lock { 85*5212e11fSVishal Verma union { 86*5212e11fSVishal Verma spinlock_t lock; 87*5212e11fSVishal Verma u8 cacheline_padding[L1_CACHE_BYTES]; 88*5212e11fSVishal Verma }; 89*5212e11fSVishal Verma }; 90*5212e11fSVishal Verma 91*5212e11fSVishal Verma /** 92*5212e11fSVishal Verma * struct arena_info - handle for an arena 93*5212e11fSVishal Verma * @size: Size in bytes this arena occupies on the raw device. 94*5212e11fSVishal Verma * This includes arena metadata. 95*5212e11fSVishal Verma * @external_lba_start: The first external LBA in this arena. 96*5212e11fSVishal Verma * @internal_nlba: Number of internal blocks available in the arena 97*5212e11fSVishal Verma * including nfree reserved blocks 98*5212e11fSVishal Verma * @internal_lbasize: Internal and external lba sizes may be different as 99*5212e11fSVishal Verma * we can round up 'odd' external lbasizes such as 520B 100*5212e11fSVishal Verma * to be aligned. 101*5212e11fSVishal Verma * @external_nlba: Number of blocks contributed by the arena to the number 102*5212e11fSVishal Verma * reported to upper layers. (internal_nlba - nfree) 103*5212e11fSVishal Verma * @external_lbasize: LBA size as exposed to upper layers. 104*5212e11fSVishal Verma * @nfree: A reserve number of 'free' blocks that is used to 105*5212e11fSVishal Verma * handle incoming writes. 106*5212e11fSVishal Verma * @version_major: Metadata layout version major. 107*5212e11fSVishal Verma * @version_minor: Metadata layout version minor. 108*5212e11fSVishal Verma * @nextoff: Offset in bytes to the start of the next arena. 109*5212e11fSVishal Verma * @infooff: Offset in bytes to the info block of this arena. 110*5212e11fSVishal Verma * @dataoff: Offset in bytes to the data area of this arena. 111*5212e11fSVishal Verma * @mapoff: Offset in bytes to the map area of this arena. 112*5212e11fSVishal Verma * @logoff: Offset in bytes to the log area of this arena. 113*5212e11fSVishal Verma * @info2off: Offset in bytes to the backup info block of this arena. 114*5212e11fSVishal Verma * @freelist: Pointer to in-memory list of free blocks 115*5212e11fSVishal Verma * @rtt: Pointer to in-memory "Read Tracking Table" 116*5212e11fSVishal Verma * @map_locks: Spinlocks protecting concurrent map writes 117*5212e11fSVishal Verma * @nd_btt: Pointer to parent nd_btt structure. 118*5212e11fSVishal Verma * @list: List head for list of arenas 119*5212e11fSVishal Verma * @debugfs_dir: Debugfs dentry 120*5212e11fSVishal Verma * @flags: Arena flags - may signify error states. 121*5212e11fSVishal Verma * 122*5212e11fSVishal Verma * arena_info is a per-arena handle. Once an arena is narrowed down for an 123*5212e11fSVishal Verma * IO, this struct is passed around for the duration of the IO. 124*5212e11fSVishal Verma */ 125*5212e11fSVishal Verma struct arena_info { 126*5212e11fSVishal Verma u64 size; /* Total bytes for this arena */ 127*5212e11fSVishal Verma u64 external_lba_start; 128*5212e11fSVishal Verma u32 internal_nlba; 129*5212e11fSVishal Verma u32 internal_lbasize; 130*5212e11fSVishal Verma u32 external_nlba; 131*5212e11fSVishal Verma u32 external_lbasize; 132*5212e11fSVishal Verma u32 nfree; 133*5212e11fSVishal Verma u16 version_major; 134*5212e11fSVishal Verma u16 version_minor; 135*5212e11fSVishal Verma /* Byte offsets to the different on-media structures */ 136*5212e11fSVishal Verma u64 nextoff; 137*5212e11fSVishal Verma u64 infooff; 138*5212e11fSVishal Verma u64 dataoff; 139*5212e11fSVishal Verma u64 mapoff; 140*5212e11fSVishal Verma u64 logoff; 141*5212e11fSVishal Verma u64 info2off; 142*5212e11fSVishal Verma /* Pointers to other in-memory structures for this arena */ 143*5212e11fSVishal Verma struct free_entry *freelist; 144*5212e11fSVishal Verma u32 *rtt; 145*5212e11fSVishal Verma struct aligned_lock *map_locks; 146*5212e11fSVishal Verma struct nd_btt *nd_btt; 147*5212e11fSVishal Verma struct list_head list; 148*5212e11fSVishal Verma struct dentry *debugfs_dir; 149*5212e11fSVishal Verma /* Arena flags */ 150*5212e11fSVishal Verma u32 flags; 151*5212e11fSVishal Verma }; 152*5212e11fSVishal Verma 153*5212e11fSVishal Verma /** 154*5212e11fSVishal Verma * struct btt - handle for a BTT instance 155*5212e11fSVishal Verma * @btt_disk: Pointer to the gendisk for BTT device 156*5212e11fSVishal Verma * @btt_queue: Pointer to the request queue for the BTT device 157*5212e11fSVishal Verma * @arena_list: Head of the list of arenas 158*5212e11fSVishal Verma * @debugfs_dir: Debugfs dentry 159*5212e11fSVishal Verma * @nd_btt: Parent nd_btt struct 160*5212e11fSVishal Verma * @nlba: Number of logical blocks exposed to the upper layers 161*5212e11fSVishal Verma * after removing the amount of space needed by metadata 162*5212e11fSVishal Verma * @rawsize: Total size in bytes of the available backing device 163*5212e11fSVishal Verma * @lbasize: LBA size as requested and presented to upper layers. 164*5212e11fSVishal Verma * This is sector_size + size of any metadata. 165*5212e11fSVishal Verma * @sector_size: The Linux sector size - 512 or 4096 166*5212e11fSVishal Verma * @lanes: Per-lane spinlocks 167*5212e11fSVishal Verma * @init_lock: Mutex used for the BTT initialization 168*5212e11fSVishal Verma * @init_state: Flag describing the initialization state for the BTT 169*5212e11fSVishal Verma * @num_arenas: Number of arenas in the BTT instance 170*5212e11fSVishal Verma */ 171*5212e11fSVishal Verma struct btt { 172*5212e11fSVishal Verma struct gendisk *btt_disk; 173*5212e11fSVishal Verma struct request_queue *btt_queue; 174*5212e11fSVishal Verma struct list_head arena_list; 175*5212e11fSVishal Verma struct dentry *debugfs_dir; 176*5212e11fSVishal Verma struct nd_btt *nd_btt; 177*5212e11fSVishal Verma u64 nlba; 178*5212e11fSVishal Verma unsigned long long rawsize; 179*5212e11fSVishal Verma u32 lbasize; 180*5212e11fSVishal Verma u32 sector_size; 181*5212e11fSVishal Verma struct nd_region *nd_region; 182*5212e11fSVishal Verma struct mutex init_lock; 183*5212e11fSVishal Verma int init_state; 184*5212e11fSVishal Verma int num_arenas; 185*5212e11fSVishal Verma }; 1868c2f7e86SDan Williams #endif 187