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