xref: /spdk/test/unit/lib/ftl/common/utils.c (revision 30afc27748e69257ca50f7e3a4b4ca6466ffc26b)
1 /*   SPDX-License-Identifier: BSD-3-Clause
2  *   Copyright (C) 2018 Intel Corporation.
3  *   All rights reserved.
4  */
5 
6 #include "spdk/ftl.h"
7 #include "ftl/ftl_core.h"
8 
9 #include "ftl/mngt/ftl_mngt_bdev.c"
10 
11 struct base_bdev_geometry {
12 	size_t write_unit_size;
13 	size_t zone_size;
14 	size_t optimal_open_zones;
15 	size_t blockcnt;
16 };
17 
18 extern struct base_bdev_geometry g_geo;
19 
20 struct spdk_ftl_dev *test_init_ftl_dev(const struct base_bdev_geometry *geo);
21 struct ftl_band *test_init_ftl_band(struct spdk_ftl_dev *dev, size_t id, size_t zone_size);
22 void test_free_ftl_dev(struct spdk_ftl_dev *dev);
23 void test_free_ftl_band(struct ftl_band *band);
24 uint64_t test_offset_from_addr(ftl_addr addr, struct ftl_band *band);
25 
26 DEFINE_STUB(spdk_bdev_desc_get_bdev, struct spdk_bdev *, (struct spdk_bdev_desc *desc), NULL);
27 
28 DEFINE_STUB(ftl_nv_cache_device_get_type_by_bdev, const struct ftl_nv_cache_device_type *,
29 	    (struct spdk_ftl_dev *dev, struct spdk_bdev *bdev), NULL);
30 
31 uint64_t
32 spdk_bdev_get_zone_size(const struct spdk_bdev *bdev)
33 {
34 	return g_geo.zone_size;
35 }
36 
37 uint32_t
38 spdk_bdev_get_optimal_open_zones(const struct spdk_bdev *bdev)
39 {
40 	return g_geo.optimal_open_zones;
41 }
42 
43 DEFINE_RETURN_MOCK(ftl_mempool_get, void *);
44 void *
45 ftl_mempool_get(struct ftl_mempool *mpool)
46 {
47 	return spdk_mempool_get((struct spdk_mempool *)mpool);
48 }
49 
50 void
51 ftl_mempool_put(struct ftl_mempool *mpool, void *element)
52 {
53 	spdk_mempool_put((struct spdk_mempool *)mpool, element);
54 }
55 
56 ftl_df_obj_id
57 ftl_mempool_get_df_obj_id(struct ftl_mempool *mpool, void *df_obj_ptr)
58 {
59 	return (ftl_df_obj_id)df_obj_ptr;
60 }
61 
62 struct spdk_ftl_dev *
63 test_init_ftl_dev(const struct base_bdev_geometry *geo)
64 {
65 	struct spdk_ftl_dev *dev;
66 
67 	dev = calloc(1, sizeof(*dev));
68 	SPDK_CU_ASSERT_FATAL(dev != NULL);
69 
70 	dev->xfer_size = geo->write_unit_size;
71 	dev->core_thread = spdk_thread_create("unit_test_thread", NULL);
72 	spdk_set_thread(dev->core_thread);
73 	dev->ioch = calloc(1, SPDK_IO_CHANNEL_STRUCT_SIZE
74 			   + sizeof(struct ftl_io_channel *));
75 	dev->num_bands = geo->blockcnt / (geo->zone_size * geo->optimal_open_zones);
76 	dev->bands = calloc(dev->num_bands, sizeof(*dev->bands));
77 	SPDK_CU_ASSERT_FATAL(dev->bands != NULL);
78 
79 	dev->layout.base.total_blocks = UINT64_MAX;
80 
81 	for (size_t i = 0; i < dev->num_bands; i++) {
82 		struct ftl_band_md *md;
83 		int ret;
84 
85 		ret = posix_memalign((void **)&md, FTL_BLOCK_SIZE, sizeof(*md));
86 		SPDK_CU_ASSERT_FATAL(ret == 0);
87 		memset(md, 0, sizeof(*md));
88 		dev->bands[i].md = md;
89 	}
90 
91 	for (int i = 0; i < FTL_LAYOUT_REGION_TYPE_MAX; i++) {
92 		dev->layout.region[i].type = i;
93 	}
94 
95 	dev->p2l_pool = (struct ftl_mempool *)spdk_mempool_create("ftl_ut", 2, 0x210200,
96 			SPDK_MEMPOOL_DEFAULT_CACHE_SIZE,
97 			SPDK_ENV_NUMA_ID_ANY);
98 	SPDK_CU_ASSERT_FATAL(dev->p2l_pool != NULL);
99 
100 	TAILQ_INIT(&dev->free_bands);
101 	TAILQ_INIT(&dev->shut_bands);
102 
103 	/* Cache frequently used values */
104 	dev->num_blocks_in_band = ftl_calculate_num_blocks_in_band(dev->base_bdev_desc);
105 	dev->is_zoned = spdk_bdev_is_zoned(spdk_bdev_desc_get_bdev(dev->base_bdev_desc));
106 
107 	return dev;
108 }
109 
110 struct ftl_band *
111 test_init_ftl_band(struct spdk_ftl_dev *dev, size_t id, size_t zone_size)
112 {
113 	struct ftl_band *band;
114 
115 	SPDK_CU_ASSERT_FATAL(dev != NULL);
116 	SPDK_CU_ASSERT_FATAL(id < dev->num_bands);
117 
118 	band = &dev->bands[id];
119 	band->dev = dev;
120 	band->id = id;
121 
122 	band->md->state = FTL_BAND_STATE_CLOSED;
123 	band->md->df_p2l_map = FTL_DF_OBJ_ID_INVALID;
124 	TAILQ_INSERT_HEAD(&dev->shut_bands, band, queue_entry);
125 
126 	band->p2l_map.valid = (struct ftl_bitmap *)spdk_bit_array_create(ftl_get_num_blocks_in_band(dev));
127 	SPDK_CU_ASSERT_FATAL(band->p2l_map.valid != NULL);
128 
129 	band->start_addr = zone_size * id;
130 
131 	return band;
132 }
133 
134 void
135 test_free_ftl_dev(struct spdk_ftl_dev *dev)
136 {
137 	struct spdk_thread *thread;
138 
139 	SPDK_CU_ASSERT_FATAL(dev != NULL);
140 	free(dev->ioch);
141 
142 	thread = dev->core_thread;
143 
144 	spdk_set_thread(thread);
145 	spdk_thread_exit(thread);
146 	while (!spdk_thread_is_exited(thread)) {
147 		spdk_thread_poll(thread, 0, 0);
148 	}
149 	spdk_thread_destroy(thread);
150 	spdk_mempool_free((struct spdk_mempool *)dev->p2l_pool);
151 	for (size_t i = 0; i < dev->num_bands; i++) {
152 		free(dev->bands[i].md);
153 	}
154 	free(dev->bands);
155 	free(dev);
156 }
157 
158 void
159 test_free_ftl_band(struct ftl_band *band)
160 {
161 	SPDK_CU_ASSERT_FATAL(band != NULL);
162 	spdk_bit_array_free((struct spdk_bit_array **)&band->p2l_map.valid);
163 	spdk_dma_free(band->p2l_map.band_dma_md);
164 
165 	band->p2l_map.band_dma_md = NULL;
166 }
167 
168 uint64_t
169 test_offset_from_addr(ftl_addr addr, struct ftl_band *band)
170 {
171 	struct spdk_ftl_dev *dev = band->dev;
172 
173 	CU_ASSERT_EQUAL(ftl_addr_get_band(dev, addr), band->id);
174 
175 	return addr - band->id * ftl_get_num_blocks_in_band(dev);
176 }
177