xref: /spdk/test/unit/lib/ftl/common/utils.c (revision b90b7ce477926a509c99112331dff6bee7336282)
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 struct ftl_band *test_init_ftl_band(struct spdk_ftl_dev *dev, size_t id);
41 void test_free_ftl_dev(struct spdk_ftl_dev *dev);
42 void test_free_ftl_band(struct ftl_band *band);
43 uint64_t test_offset_from_addr(struct ftl_addr addr, struct ftl_band *band);
44 
45 struct spdk_ftl_dev *
46 test_init_ftl_dev(const struct spdk_ocssd_geometry_data *geo)
47 {
48 	struct spdk_ftl_dev *dev;
49 
50 	dev = calloc(1, sizeof(*dev));
51 	SPDK_CU_ASSERT_FATAL(dev != NULL);
52 
53 	dev->xfer_size = geo->ws_opt;
54 	dev->geo = *geo;
55 	dev->core_thread.thread = spdk_thread_create("unit_test_thread", NULL);
56 	spdk_set_thread(dev->core_thread.thread);
57 
58 	dev->bands = calloc(geo->num_chk, sizeof(*dev->bands));
59 	SPDK_CU_ASSERT_FATAL(dev->bands != NULL);
60 
61 	dev->lba_pool = spdk_mempool_create("ftl_ut", 2, 0x18000,
62 					    SPDK_MEMPOOL_DEFAULT_CACHE_SIZE,
63 					    SPDK_ENV_SOCKET_ID_ANY);
64 	SPDK_CU_ASSERT_FATAL(dev->lba_pool != NULL);
65 
66 	LIST_INIT(&dev->free_bands);
67 	LIST_INIT(&dev->shut_bands);
68 
69 	return dev;
70 }
71 
72 struct ftl_band *
73 test_init_ftl_band(struct spdk_ftl_dev *dev, size_t id)
74 {
75 	struct ftl_band *band;
76 	struct ftl_zone *zone;
77 
78 	SPDK_CU_ASSERT_FATAL(dev != NULL);
79 	SPDK_CU_ASSERT_FATAL(id < dev->geo.num_chk);
80 
81 	band = &dev->bands[id];
82 	band->dev = dev;
83 	band->id = id;
84 
85 	band->state = FTL_BAND_STATE_CLOSED;
86 	LIST_INSERT_HEAD(&dev->shut_bands, band, list_entry);
87 	CIRCLEQ_INIT(&band->zones);
88 
89 	band->lba_map.vld = spdk_bit_array_create(ftl_get_num_blocks_in_band(dev));
90 	SPDK_CU_ASSERT_FATAL(band->lba_map.vld != NULL);
91 
92 	band->zone_buf = calloc(ftl_get_num_punits(dev), sizeof(*band->zone_buf));
93 	SPDK_CU_ASSERT_FATAL(band->zone_buf != NULL);
94 
95 	band->reloc_bitmap = spdk_bit_array_create(ftl_get_num_bands(dev));
96 	SPDK_CU_ASSERT_FATAL(band->reloc_bitmap != NULL);
97 
98 	for (size_t i = 0; i < ftl_get_num_punits(dev); ++i) {
99 		zone = &band->zone_buf[i];
100 		zone->state = SPDK_BDEV_ZONE_STATE_CLOSED;
101 		zone->start_addr.pu = i;
102 		zone->start_addr.zone_id = band->id;
103 		CIRCLEQ_INSERT_TAIL(&band->zones, zone, circleq);
104 		band->num_zones++;
105 	}
106 
107 	pthread_spin_init(&band->lba_map.lock, PTHREAD_PROCESS_PRIVATE);
108 	return band;
109 }
110 
111 void
112 test_free_ftl_dev(struct spdk_ftl_dev *dev)
113 {
114 	SPDK_CU_ASSERT_FATAL(dev != NULL);
115 	spdk_set_thread(dev->core_thread.thread);
116 	spdk_thread_exit(dev->core_thread.thread);
117 	spdk_thread_destroy(dev->core_thread.thread);
118 	spdk_mempool_free(dev->lba_pool);
119 	free(dev->bands);
120 	free(dev);
121 }
122 
123 void
124 test_free_ftl_band(struct ftl_band *band)
125 {
126 	SPDK_CU_ASSERT_FATAL(band != NULL);
127 	spdk_bit_array_free(&band->lba_map.vld);
128 	spdk_bit_array_free(&band->reloc_bitmap);
129 	free(band->zone_buf);
130 	spdk_dma_free(band->lba_map.dma_buf);
131 }
132 
133 uint64_t
134 test_offset_from_addr(struct ftl_addr addr, struct ftl_band *band)
135 {
136 	struct spdk_ftl_dev *dev = band->dev;
137 
138 	CU_ASSERT_EQUAL(addr.zone_id, band->id);
139 
140 	return addr.pu * ftl_get_num_blocks_in_zone(dev) + addr.offset;
141 }
142