xref: /spdk/test/unit/lib/ftl/common/utils.c (revision 8bb0ded3e55c182cea67af1f6790f8de5f38c05f)
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright (c) Intel Corporation.
5  *   All rights reserved.
6  *
7  *   Redistribution and use in source and binary forms, with or without
8  *   modification, are permitted provided that the following conditions
9  *   are met:
10  *
11  *     * Redistributions of source code must retain the above copyright
12  *       notice, this list of conditions and the following disclaimer.
13  *     * Redistributions in binary form must reproduce the above copyright
14  *       notice, this list of conditions and the following disclaimer in
15  *       the documentation and/or other materials provided with the
16  *       distribution.
17  *     * Neither the name of Intel Corporation nor the names of its
18  *       contributors may be used to endorse or promote products derived
19  *       from this software without specific prior written permission.
20  *
21  *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24  *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25  *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26  *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27  *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28  *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29  *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30  *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31  *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32  */
33 
34 #include "spdk/ftl.h"
35 #include "ftl/ftl_core.h"
36 
37 struct base_bdev_geometry {
38 	size_t write_unit_size;
39 	size_t zone_size;
40 	size_t optimal_open_zones;
41 	size_t blockcnt;
42 };
43 
44 extern struct base_bdev_geometry g_geo;
45 
46 struct spdk_ftl_dev *test_init_ftl_dev(const struct base_bdev_geometry *geo);
47 struct ftl_band *test_init_ftl_band(struct spdk_ftl_dev *dev, size_t id, size_t zone_size);
48 void test_free_ftl_dev(struct spdk_ftl_dev *dev);
49 void test_free_ftl_band(struct ftl_band *band);
50 uint64_t test_offset_from_addr(struct ftl_addr addr, struct ftl_band *band);
51 
52 DEFINE_STUB(spdk_bdev_desc_get_bdev, struct spdk_bdev *, (struct spdk_bdev_desc *desc), NULL);
53 
54 uint64_t
55 spdk_bdev_get_zone_size(const struct spdk_bdev *bdev)
56 {
57 	return g_geo.zone_size;
58 }
59 
60 uint32_t
61 spdk_bdev_get_optimal_open_zones(const struct spdk_bdev *bdev)
62 {
63 	return g_geo.optimal_open_zones;
64 }
65 
66 struct spdk_ftl_dev *
67 test_init_ftl_dev(const struct base_bdev_geometry *geo)
68 {
69 	struct spdk_ftl_dev *dev;
70 
71 	dev = calloc(1, sizeof(*dev));
72 	SPDK_CU_ASSERT_FATAL(dev != NULL);
73 
74 	dev->xfer_size = geo->write_unit_size;
75 	dev->core_thread = spdk_thread_create("unit_test_thread", NULL);
76 	spdk_set_thread(dev->core_thread);
77 	dev->ioch = calloc(1, sizeof(*dev->ioch)
78 			   + sizeof(struct ftl_io_channel *));
79 	dev->num_bands = geo->blockcnt / (geo->zone_size * geo->optimal_open_zones);
80 	dev->bands = calloc(dev->num_bands, sizeof(*dev->bands));
81 	SPDK_CU_ASSERT_FATAL(dev->bands != NULL);
82 
83 	dev->lba_pool = spdk_mempool_create("ftl_ut", 2, 0x18000,
84 					    SPDK_MEMPOOL_DEFAULT_CACHE_SIZE,
85 					    SPDK_ENV_SOCKET_ID_ANY);
86 	SPDK_CU_ASSERT_FATAL(dev->lba_pool != NULL);
87 
88 	LIST_INIT(&dev->free_bands);
89 	LIST_INIT(&dev->shut_bands);
90 
91 	return dev;
92 }
93 
94 struct ftl_band *
95 test_init_ftl_band(struct spdk_ftl_dev *dev, size_t id, size_t zone_size)
96 {
97 	struct ftl_band *band;
98 	struct ftl_zone *zone;
99 
100 	SPDK_CU_ASSERT_FATAL(dev != NULL);
101 	SPDK_CU_ASSERT_FATAL(id < dev->num_bands);
102 
103 	band = &dev->bands[id];
104 	band->dev = dev;
105 	band->id = id;
106 
107 	band->state = FTL_BAND_STATE_CLOSED;
108 	LIST_INSERT_HEAD(&dev->shut_bands, band, list_entry);
109 	CIRCLEQ_INIT(&band->zones);
110 
111 	band->lba_map.vld = spdk_bit_array_create(ftl_get_num_blocks_in_band(dev));
112 	SPDK_CU_ASSERT_FATAL(band->lba_map.vld != NULL);
113 
114 	band->zone_buf = calloc(ftl_get_num_punits(dev), sizeof(*band->zone_buf));
115 	SPDK_CU_ASSERT_FATAL(band->zone_buf != NULL);
116 
117 	band->reloc_bitmap = spdk_bit_array_create(ftl_get_num_bands(dev));
118 	SPDK_CU_ASSERT_FATAL(band->reloc_bitmap != NULL);
119 
120 	for (size_t i = 0; i < ftl_get_num_punits(dev); ++i) {
121 		zone = &band->zone_buf[i];
122 		zone->info.state = SPDK_BDEV_ZONE_STATE_FULL;
123 		zone->info.zone_id = zone_size * (id * ftl_get_num_punits(dev) + i);
124 		CIRCLEQ_INSERT_TAIL(&band->zones, zone, circleq);
125 		band->num_zones++;
126 	}
127 
128 	pthread_spin_init(&band->lba_map.lock, PTHREAD_PROCESS_PRIVATE);
129 	return band;
130 }
131 
132 void
133 test_free_ftl_dev(struct spdk_ftl_dev *dev)
134 {
135 	struct spdk_thread *thread;
136 
137 	SPDK_CU_ASSERT_FATAL(dev != NULL);
138 	free(dev->ioch);
139 
140 	thread = dev->core_thread;
141 
142 	spdk_set_thread(thread);
143 	spdk_thread_exit(thread);
144 	while (!spdk_thread_is_exited(thread)) {
145 		spdk_thread_poll(thread, 0, 0);
146 	}
147 	spdk_thread_destroy(thread);
148 	spdk_mempool_free(dev->lba_pool);
149 	free(dev->bands);
150 	free(dev);
151 }
152 
153 void
154 test_free_ftl_band(struct ftl_band *band)
155 {
156 	SPDK_CU_ASSERT_FATAL(band != NULL);
157 	spdk_bit_array_free(&band->lba_map.vld);
158 	spdk_bit_array_free(&band->reloc_bitmap);
159 	free(band->zone_buf);
160 	spdk_dma_free(band->lba_map.dma_buf);
161 }
162 
163 uint64_t
164 test_offset_from_addr(struct ftl_addr addr, struct ftl_band *band)
165 {
166 	struct spdk_ftl_dev *dev = band->dev;
167 
168 	CU_ASSERT_EQUAL(ftl_addr_get_band(dev, addr), band->id);
169 
170 	return addr.offset - band->id * ftl_get_num_blocks_in_band(dev);
171 }
172