1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright (C) 2022 Intel Corporation.
3 * All rights reserved.
4 */
5
6 #include "spdk/stdinc.h"
7
8 #include "spdk_internal/cunit.h"
9 #include "common/lib/test_env.c"
10
11 #include "ftl/ftl_core.h"
12
13 #define L2P_TABLE_SIZE 1024
14
15 static struct spdk_ftl_dev *g_dev;
16
17 static struct spdk_ftl_dev *
test_alloc_dev(size_t size)18 test_alloc_dev(size_t size)
19 {
20 struct spdk_ftl_dev *dev;
21
22 dev = calloc(1, sizeof(*dev));
23
24 dev->num_lbas = L2P_TABLE_SIZE;
25 dev->l2p = calloc(L2P_TABLE_SIZE, size);
26 dev->layout.l2p.addr_size = size;
27
28 if (size > sizeof(uint32_t)) {
29 dev->layout.base.total_blocks = ~(~0ULL << 33);
30 } else {
31 dev->layout.base.total_blocks = 1024;
32 }
33
34 return dev;
35 }
36
37 static int
setup_l2p_64bit(void)38 setup_l2p_64bit(void)
39 {
40 g_dev = test_alloc_dev(sizeof(uint64_t));
41 return 0;
42 }
43
44 static void
clean_l2p(void)45 clean_l2p(void)
46 {
47 size_t l2p_elem_size;
48
49 if (ftl_addr_packed(g_dev)) {
50 l2p_elem_size = sizeof(uint32_t);
51 } else {
52 l2p_elem_size = sizeof(uint64_t);
53 }
54 memset(g_dev->l2p, 0, g_dev->num_lbas * l2p_elem_size);
55 }
56
57 static int
cleanup(void)58 cleanup(void)
59 {
60 free(g_dev->l2p);
61 free(g_dev);
62 g_dev = NULL;
63 return 0;
64 }
65
66 void
ftl_l2p_set(struct spdk_ftl_dev * dev,uint64_t lba,ftl_addr addr)67 ftl_l2p_set(struct spdk_ftl_dev *dev, uint64_t lba, ftl_addr addr)
68 {
69 ((uint64_t *)dev->l2p)[lba] = addr;
70 }
71
72 ftl_addr
ftl_l2p_get(struct spdk_ftl_dev * dev,uint64_t lba)73 ftl_l2p_get(struct spdk_ftl_dev *dev, uint64_t lba)
74 {
75 return ((uint64_t *)dev->l2p)[lba];
76 }
77
78 static void
test_addr_cached(void)79 test_addr_cached(void)
80 {
81 ftl_addr addr;
82 size_t i;
83
84 /* Set every other LBA is cached */
85 for (i = 0; i < L2P_TABLE_SIZE; i += 2) {
86 addr = ftl_addr_from_nvc_offset(g_dev, i);
87 ftl_l2p_set(g_dev, i, addr);
88 }
89
90 /* Check every even LBA is cached while others are not */
91 for (i = 0; i < L2P_TABLE_SIZE; ++i) {
92 addr = ftl_l2p_get(g_dev, i);
93
94 if (i % 2 == 0) {
95 CU_ASSERT_TRUE(ftl_addr_in_nvc(g_dev, addr));
96 CU_ASSERT_EQUAL(ftl_addr_to_nvc_offset(g_dev, addr), i);
97 } else {
98 CU_ASSERT_FALSE(ftl_addr_in_nvc(g_dev, addr));
99 }
100 }
101 clean_l2p();
102 }
103
104 int
main(int argc,char ** argv)105 main(int argc, char **argv)
106 {
107 CU_pSuite suite64 = NULL;
108 unsigned int num_failures;
109
110 CU_initialize_registry();
111
112 suite64 = CU_add_suite("ftl_addr64_suite", setup_l2p_64bit, cleanup);
113
114 CU_ADD_TEST(suite64, test_addr_cached);
115
116 num_failures = spdk_ut_run_tests(argc, argv, NULL);
117 CU_cleanup_registry();
118
119 return num_failures;
120 }
121