xref: /linux/drivers/md/dm-pcache/cache_dev.h (revision 4f38da1f027ea2c9f01bb71daa7a299c191b6940)
1*1d57628fSDongsheng Yang /* SPDX-License-Identifier: GPL-2.0-or-later */
2*1d57628fSDongsheng Yang #ifndef _PCACHE_CACHE_DEV_H
3*1d57628fSDongsheng Yang #define _PCACHE_CACHE_DEV_H
4*1d57628fSDongsheng Yang 
5*1d57628fSDongsheng Yang #include <linux/device.h>
6*1d57628fSDongsheng Yang #include <linux/device-mapper.h>
7*1d57628fSDongsheng Yang 
8*1d57628fSDongsheng Yang #include "pcache_internal.h"
9*1d57628fSDongsheng Yang 
10*1d57628fSDongsheng Yang #define PCACHE_MAGIC				0x65B05EFA96C596EFULL
11*1d57628fSDongsheng Yang 
12*1d57628fSDongsheng Yang #define PCACHE_SB_OFF				(4 * PCACHE_KB)
13*1d57628fSDongsheng Yang #define PCACHE_SB_SIZE				(4 * PCACHE_KB)
14*1d57628fSDongsheng Yang 
15*1d57628fSDongsheng Yang #define PCACHE_CACHE_INFO_OFF			(PCACHE_SB_OFF + PCACHE_SB_SIZE)
16*1d57628fSDongsheng Yang #define PCACHE_CACHE_INFO_SIZE			(4 * PCACHE_KB)
17*1d57628fSDongsheng Yang 
18*1d57628fSDongsheng Yang #define PCACHE_CACHE_CTRL_OFF			(PCACHE_CACHE_INFO_OFF + (PCACHE_CACHE_INFO_SIZE * PCACHE_META_INDEX_MAX))
19*1d57628fSDongsheng Yang #define PCACHE_CACHE_CTRL_SIZE			(4 * PCACHE_KB)
20*1d57628fSDongsheng Yang 
21*1d57628fSDongsheng Yang #define PCACHE_SEGMENTS_OFF			(PCACHE_CACHE_CTRL_OFF + PCACHE_CACHE_CTRL_SIZE)
22*1d57628fSDongsheng Yang #define PCACHE_SEG_INFO_SIZE			(4 * PCACHE_KB)
23*1d57628fSDongsheng Yang 
24*1d57628fSDongsheng Yang #define PCACHE_CACHE_DEV_SIZE_MIN		(512 * PCACHE_MB)	/* 512 MB */
25*1d57628fSDongsheng Yang #define PCACHE_SEG_SIZE				(16 * PCACHE_MB)	/* Size of each PCACHE segment (16 MB) */
26*1d57628fSDongsheng Yang 
27*1d57628fSDongsheng Yang #define CACHE_DEV_SB(cache_dev)			((struct pcache_sb *)(cache_dev->mapping + PCACHE_SB_OFF))
28*1d57628fSDongsheng Yang #define CACHE_DEV_CACHE_INFO(cache_dev)		((void *)cache_dev->mapping + PCACHE_CACHE_INFO_OFF)
29*1d57628fSDongsheng Yang #define CACHE_DEV_CACHE_CTRL(cache_dev)		((void *)cache_dev->mapping + PCACHE_CACHE_CTRL_OFF)
30*1d57628fSDongsheng Yang #define CACHE_DEV_SEGMENTS(cache_dev)		((void *)cache_dev->mapping + PCACHE_SEGMENTS_OFF)
31*1d57628fSDongsheng Yang #define CACHE_DEV_SEGMENT(cache_dev, id)	((void *)CACHE_DEV_SEGMENTS(cache_dev) + (u64)id * PCACHE_SEG_SIZE)
32*1d57628fSDongsheng Yang 
33*1d57628fSDongsheng Yang /*
34*1d57628fSDongsheng Yang  * PCACHE SB flags configured during formatting
35*1d57628fSDongsheng Yang  *
36*1d57628fSDongsheng Yang  * The PCACHE_SB_F_xxx flags define registration requirements based on cache_dev
37*1d57628fSDongsheng Yang  * formatting. For a machine to register a cache_dev:
38*1d57628fSDongsheng Yang  * - PCACHE_SB_F_BIGENDIAN: Requires a big-endian machine.
39*1d57628fSDongsheng Yang  */
40*1d57628fSDongsheng Yang #define PCACHE_SB_F_BIGENDIAN			BIT(0)
41*1d57628fSDongsheng Yang 
42*1d57628fSDongsheng Yang struct pcache_sb {
43*1d57628fSDongsheng Yang 	__le32 crc;
44*1d57628fSDongsheng Yang 	__le32 flags;
45*1d57628fSDongsheng Yang 	__le64 magic;
46*1d57628fSDongsheng Yang 
47*1d57628fSDongsheng Yang 	__le32 seg_num;
48*1d57628fSDongsheng Yang };
49*1d57628fSDongsheng Yang 
50*1d57628fSDongsheng Yang struct pcache_cache_dev {
51*1d57628fSDongsheng Yang 	u32				sb_flags;
52*1d57628fSDongsheng Yang 	u32				seg_num;
53*1d57628fSDongsheng Yang 	void				*mapping;
54*1d57628fSDongsheng Yang 	bool				use_vmap;
55*1d57628fSDongsheng Yang 
56*1d57628fSDongsheng Yang 	struct dm_dev			*dm_dev;
57*1d57628fSDongsheng Yang 
58*1d57628fSDongsheng Yang 	struct mutex			seg_lock;
59*1d57628fSDongsheng Yang 	unsigned long			*seg_bitmap;
60*1d57628fSDongsheng Yang };
61*1d57628fSDongsheng Yang 
62*1d57628fSDongsheng Yang struct dm_pcache;
63*1d57628fSDongsheng Yang int cache_dev_start(struct dm_pcache *pcache);
64*1d57628fSDongsheng Yang void cache_dev_stop(struct dm_pcache *pcache);
65*1d57628fSDongsheng Yang 
66*1d57628fSDongsheng Yang void cache_dev_zero_range(struct pcache_cache_dev *cache_dev, void *pos, u32 size);
67*1d57628fSDongsheng Yang 
68*1d57628fSDongsheng Yang int cache_dev_get_empty_segment_id(struct pcache_cache_dev *cache_dev, u32 *seg_id);
69*1d57628fSDongsheng Yang 
70*1d57628fSDongsheng Yang #endif /* _PCACHE_CACHE_DEV_H */
71