xref: /spdk/test/unit/lib/ftl/common/utils.c (revision 56e12b00711b40d1e2ff45d4147193f45fdd9af0)
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 static struct spdk_ftl_dev *
38 test_init_ftl_dev(const struct spdk_ocssd_geometry_data *geo,
39 		  const struct spdk_ftl_punit_range *range)
40 {
41 	struct spdk_ftl_dev *dev;
42 	unsigned int punit;
43 
44 	dev = calloc(1, sizeof(*dev));
45 	SPDK_CU_ASSERT_FATAL(dev != NULL);
46 
47 	dev->xfer_size = geo->ws_opt;
48 	dev->geo = *geo;
49 	dev->range = *range;
50 
51 	dev->bands = calloc(geo->num_chk, sizeof(*dev->bands));
52 	SPDK_CU_ASSERT_FATAL(dev->bands != NULL);
53 
54 	dev->punits = calloc(ftl_dev_num_punits(dev), sizeof(*dev->punits));
55 	SPDK_CU_ASSERT_FATAL(dev->punits != NULL);
56 
57 	for (size_t i = 0; i < ftl_dev_num_punits(dev); ++i) {
58 		punit = range->begin + i;
59 		dev->punits[i].dev = dev;
60 		dev->punits[i].start_ppa.grp = punit % geo->num_grp;
61 		dev->punits[i].start_ppa.pu = punit / geo->num_grp;
62 	}
63 
64 	return dev;
65 }
66 
67 static struct ftl_band *
68 test_init_ftl_band(struct spdk_ftl_dev *dev, size_t id)
69 {
70 	struct ftl_band *band;
71 	struct ftl_chunk *chunk;
72 
73 	SPDK_CU_ASSERT_FATAL(dev != NULL);
74 	SPDK_CU_ASSERT_FATAL(id < dev->geo.num_chk);
75 
76 	band = &dev->bands[id];
77 	band->dev = dev;
78 	band->id = id;
79 	CIRCLEQ_INIT(&band->chunks);
80 
81 	band->md.vld_map = spdk_bit_array_create(ftl_num_band_lbks(dev));
82 	SPDK_CU_ASSERT_FATAL(band->md.vld_map != NULL);
83 
84 	band->chunk_buf = calloc(ftl_dev_num_punits(dev), sizeof(*band->chunk_buf));
85 	SPDK_CU_ASSERT_FATAL(band->chunk_buf != NULL);
86 
87 	for (size_t i = 0; i < ftl_dev_num_punits(dev); ++i) {
88 		chunk = &band->chunk_buf[i];
89 		chunk->pos = i;
90 		chunk->state = FTL_CHUNK_STATE_CLOSED;
91 		chunk->punit = &dev->punits[i];
92 		chunk->start_ppa = dev->punits[i].start_ppa;
93 		chunk->start_ppa.chk = band->id;
94 		CIRCLEQ_INSERT_TAIL(&band->chunks, chunk, circleq);
95 		band->num_chunks++;
96 	}
97 
98 	pthread_spin_init(&band->md.lock, PTHREAD_PROCESS_PRIVATE);
99 	return band;
100 }
101 
102 static void
103 test_free_ftl_dev(struct spdk_ftl_dev *dev)
104 {
105 	SPDK_CU_ASSERT_FATAL(dev != NULL);
106 	free(dev->punits);
107 	free(dev->bands);
108 	free(dev);
109 }
110 
111 static void
112 test_free_ftl_band(struct ftl_band *band)
113 {
114 	SPDK_CU_ASSERT_FATAL(band != NULL);
115 	spdk_bit_array_free(&band->md.vld_map);
116 	free(band->chunk_buf);
117 	free(band->md.lba_map);
118 }
119 
120 static uint64_t
121 test_offset_from_ppa(struct ftl_ppa ppa, struct ftl_band *band)
122 {
123 	struct spdk_ftl_dev *dev = band->dev;
124 	unsigned int punit;
125 
126 	/* TODO: ftl_ppa_flatten_punit should return uint32_t */
127 	punit = ftl_ppa_flatten_punit(dev, ppa);
128 	CU_ASSERT_EQUAL(ppa.chk, band->id);
129 
130 	return punit * ftl_dev_lbks_in_chunk(dev) + ppa.lbk;
131 }
132