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 base_bdev_geometry { 40 size_t write_unit_size; 41 size_t zone_size; 42 size_t optimal_open_zones; 43 size_t blockcnt; 44 }; 45 46 extern struct base_bdev_geometry g_geo; 47 48 struct spdk_ftl_dev *test_init_ftl_dev(const struct base_bdev_geometry *geo); 49 struct ftl_band *test_init_ftl_band(struct spdk_ftl_dev *dev, size_t id, size_t zone_size); 50 void test_free_ftl_dev(struct spdk_ftl_dev *dev); 51 void test_free_ftl_band(struct ftl_band *band); 52 uint64_t test_offset_from_addr(struct ftl_addr addr, struct ftl_band *band); 53 54 DEFINE_STUB(spdk_bdev_desc_get_bdev, struct spdk_bdev *, (struct spdk_bdev_desc *desc), NULL); 55 56 uint64_t 57 spdk_bdev_get_zone_size(const struct spdk_bdev *bdev) 58 { 59 return g_geo.zone_size; 60 } 61 62 uint32_t 63 spdk_bdev_get_optimal_open_zones(const struct spdk_bdev *bdev) 64 { 65 return g_geo.optimal_open_zones; 66 } 67 68 struct spdk_ftl_dev * 69 test_init_ftl_dev(const struct base_bdev_geometry *geo) 70 { 71 struct spdk_ftl_dev *dev; 72 73 dev = calloc(1, sizeof(*dev)); 74 SPDK_CU_ASSERT_FATAL(dev != NULL); 75 76 dev->xfer_size = geo->write_unit_size; 77 dev->core_thread = spdk_thread_create("unit_test_thread", NULL); 78 spdk_set_thread(dev->core_thread); 79 dev->ioch = calloc(1, sizeof(*dev->ioch) 80 + sizeof(struct ftl_io_channel *)); 81 dev->num_bands = geo->blockcnt / (geo->zone_size * geo->optimal_open_zones); 82 dev->bands = calloc(dev->num_bands, sizeof(*dev->bands)); 83 SPDK_CU_ASSERT_FATAL(dev->bands != NULL); 84 85 dev->lba_pool = spdk_mempool_create("ftl_ut", 2, 0x18000, 86 SPDK_MEMPOOL_DEFAULT_CACHE_SIZE, 87 SPDK_ENV_SOCKET_ID_ANY); 88 SPDK_CU_ASSERT_FATAL(dev->lba_pool != NULL); 89 90 LIST_INIT(&dev->free_bands); 91 LIST_INIT(&dev->shut_bands); 92 93 return dev; 94 } 95 96 struct ftl_band * 97 test_init_ftl_band(struct spdk_ftl_dev *dev, size_t id, size_t zone_size) 98 { 99 struct ftl_band *band; 100 struct ftl_zone *zone; 101 102 SPDK_CU_ASSERT_FATAL(dev != NULL); 103 SPDK_CU_ASSERT_FATAL(id < dev->num_bands); 104 105 band = &dev->bands[id]; 106 band->dev = dev; 107 band->id = id; 108 109 band->state = FTL_BAND_STATE_CLOSED; 110 LIST_INSERT_HEAD(&dev->shut_bands, band, list_entry); 111 CIRCLEQ_INIT(&band->zones); 112 113 band->lba_map.vld = spdk_bit_array_create(ftl_get_num_blocks_in_band(dev)); 114 SPDK_CU_ASSERT_FATAL(band->lba_map.vld != NULL); 115 116 band->zone_buf = calloc(ftl_get_num_punits(dev), sizeof(*band->zone_buf)); 117 SPDK_CU_ASSERT_FATAL(band->zone_buf != NULL); 118 119 band->reloc_bitmap = spdk_bit_array_create(ftl_get_num_bands(dev)); 120 SPDK_CU_ASSERT_FATAL(band->reloc_bitmap != NULL); 121 122 for (size_t i = 0; i < ftl_get_num_punits(dev); ++i) { 123 zone = &band->zone_buf[i]; 124 zone->info.state = SPDK_BDEV_ZONE_STATE_FULL; 125 zone->info.zone_id = zone_size * (id * ftl_get_num_punits(dev) + i); 126 CIRCLEQ_INSERT_TAIL(&band->zones, zone, circleq); 127 band->num_zones++; 128 } 129 130 pthread_spin_init(&band->lba_map.lock, PTHREAD_PROCESS_PRIVATE); 131 return band; 132 } 133 134 void 135 test_free_ftl_dev(struct spdk_ftl_dev *dev) 136 { 137 struct spdk_thread *thread; 138 139 SPDK_CU_ASSERT_FATAL(dev != NULL); 140 free(dev->ioch); 141 142 thread = dev->core_thread; 143 144 spdk_set_thread(thread); 145 spdk_thread_exit(thread); 146 while (!spdk_thread_is_exited(thread)) { 147 spdk_thread_poll(thread, 0, 0); 148 } 149 spdk_thread_destroy(thread); 150 spdk_mempool_free(dev->lba_pool); 151 free(dev->bands); 152 free(dev); 153 } 154 155 void 156 test_free_ftl_band(struct ftl_band *band) 157 { 158 SPDK_CU_ASSERT_FATAL(band != NULL); 159 spdk_bit_array_free(&band->lba_map.vld); 160 spdk_bit_array_free(&band->reloc_bitmap); 161 free(band->zone_buf); 162 spdk_dma_free(band->lba_map.dma_buf); 163 } 164 165 uint64_t 166 test_offset_from_addr(struct ftl_addr addr, struct ftl_band *band) 167 { 168 struct spdk_ftl_dev *dev = band->dev; 169 170 CU_ASSERT_EQUAL(ftl_addr_get_band(dev, addr), band->id); 171 172 return addr.offset - band->id * ftl_get_num_blocks_in_band(dev); 173 } 174