xref: /spdk/test/unit/lib/ftl/common/utils.c (revision ae7b5890ef728af40bd233a5011b924c482603bf)
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_internal/thread.h"
35 
36 #include "spdk/ftl.h"
37 #include "ftl/ftl_core.h"
38 
39 struct spdk_ftl_dev *test_init_ftl_dev(const struct spdk_ocssd_geometry_data *geo,
40 				       const struct spdk_ftl_punit_range *range);
41 struct ftl_band *test_init_ftl_band(struct spdk_ftl_dev *dev, size_t id);
42 void test_free_ftl_dev(struct spdk_ftl_dev *dev);
43 void test_free_ftl_band(struct ftl_band *band);
44 uint64_t test_offset_from_ppa(struct ftl_ppa ppa, struct ftl_band *band);
45 
46 struct spdk_ftl_dev *
47 test_init_ftl_dev(const struct spdk_ocssd_geometry_data *geo,
48 		  const struct spdk_ftl_punit_range *range)
49 {
50 	struct spdk_ftl_dev *dev;
51 	unsigned int punit;
52 
53 	dev = calloc(1, sizeof(*dev));
54 	SPDK_CU_ASSERT_FATAL(dev != NULL);
55 
56 	dev->xfer_size = geo->ws_opt;
57 	dev->geo = *geo;
58 	dev->range = *range;
59 	dev->core_thread.thread = spdk_thread_create("unit_test_thread", NULL);
60 	spdk_set_thread(dev->core_thread.thread);
61 
62 	dev->bands = calloc(geo->num_chk, sizeof(*dev->bands));
63 	SPDK_CU_ASSERT_FATAL(dev->bands != NULL);
64 
65 	dev->punits = calloc(ftl_dev_num_punits(dev), sizeof(*dev->punits));
66 	SPDK_CU_ASSERT_FATAL(dev->punits != NULL);
67 
68 	dev->lba_pool = spdk_mempool_create("ftl_ut", 2, 0x18000,
69 					    SPDK_MEMPOOL_DEFAULT_CACHE_SIZE,
70 					    SPDK_ENV_SOCKET_ID_ANY);
71 	SPDK_CU_ASSERT_FATAL(dev->lba_pool != NULL);
72 
73 	for (size_t i = 0; i < ftl_dev_num_punits(dev); ++i) {
74 		punit = range->begin + i;
75 		dev->punits[i].dev = dev;
76 		dev->punits[i].start_ppa.grp = punit % geo->num_grp;
77 		dev->punits[i].start_ppa.pu = punit / geo->num_grp;
78 	}
79 
80 	LIST_INIT(&dev->free_bands);
81 	LIST_INIT(&dev->shut_bands);
82 
83 	return dev;
84 }
85 
86 struct ftl_band *
87 test_init_ftl_band(struct spdk_ftl_dev *dev, size_t id)
88 {
89 	struct ftl_band *band;
90 	struct ftl_chunk *chunk;
91 
92 	SPDK_CU_ASSERT_FATAL(dev != NULL);
93 	SPDK_CU_ASSERT_FATAL(id < dev->geo.num_chk);
94 
95 	band = &dev->bands[id];
96 	band->dev = dev;
97 	band->id = id;
98 
99 	band->state = FTL_BAND_STATE_CLOSED;
100 	LIST_INSERT_HEAD(&dev->shut_bands, band, list_entry);
101 	CIRCLEQ_INIT(&band->chunks);
102 
103 	band->lba_map.vld = spdk_bit_array_create(ftl_num_band_lbks(dev));
104 	SPDK_CU_ASSERT_FATAL(band->lba_map.vld != NULL);
105 
106 	band->chunk_buf = calloc(ftl_dev_num_punits(dev), sizeof(*band->chunk_buf));
107 	SPDK_CU_ASSERT_FATAL(band->chunk_buf != NULL);
108 
109 	band->reloc_bitmap = spdk_bit_array_create(ftl_dev_num_bands(dev));
110 	SPDK_CU_ASSERT_FATAL(band->reloc_bitmap != NULL);
111 
112 	for (size_t i = 0; i < ftl_dev_num_punits(dev); ++i) {
113 		chunk = &band->chunk_buf[i];
114 		chunk->pos = i;
115 		chunk->state = FTL_CHUNK_STATE_CLOSED;
116 		chunk->punit = &dev->punits[i];
117 		chunk->start_ppa = dev->punits[i].start_ppa;
118 		chunk->start_ppa.chk = band->id;
119 		CIRCLEQ_INSERT_TAIL(&band->chunks, chunk, circleq);
120 		band->num_chunks++;
121 	}
122 
123 	pthread_spin_init(&band->lba_map.lock, PTHREAD_PROCESS_PRIVATE);
124 	return band;
125 }
126 
127 void
128 test_free_ftl_dev(struct spdk_ftl_dev *dev)
129 {
130 	SPDK_CU_ASSERT_FATAL(dev != NULL);
131 	spdk_set_thread(dev->core_thread.thread);
132 	spdk_thread_exit(dev->core_thread.thread);
133 	spdk_thread_destroy(dev->core_thread.thread);
134 	spdk_mempool_free(dev->lba_pool);
135 	free(dev->punits);
136 	free(dev->bands);
137 	free(dev);
138 }
139 
140 void
141 test_free_ftl_band(struct ftl_band *band)
142 {
143 	SPDK_CU_ASSERT_FATAL(band != NULL);
144 	spdk_bit_array_free(&band->lba_map.vld);
145 	spdk_bit_array_free(&band->reloc_bitmap);
146 	free(band->chunk_buf);
147 	spdk_dma_free(band->lba_map.dma_buf);
148 }
149 
150 uint64_t
151 test_offset_from_ppa(struct ftl_ppa ppa, struct ftl_band *band)
152 {
153 	struct spdk_ftl_dev *dev = band->dev;
154 	unsigned int punit;
155 
156 	/* TODO: ftl_ppa_flatten_punit should return uint32_t */
157 	punit = ftl_ppa_flatten_punit(dev, ppa);
158 	CU_ASSERT_EQUAL(ppa.chk, band->id);
159 
160 	return punit * ftl_dev_lbks_in_chunk(dev) + ppa.lbk;
161 }
162