xref: /spdk/test/env/pci/pci_ut.c (revision 1fa071d332db21bf893d581a8e93b425ba788a24)
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/stdinc.h"
35 
36 #include "CUnit/Basic.h"
37 
38 #include "env_dpdk/pci.c"
39 
40 static void
41 pci_claim_test(struct spdk_pci_device *dev)
42 {
43 	int rc = 0;
44 	pid_t childPid;
45 	int status, ret;
46 
47 	rc = spdk_pci_device_claim(dev);
48 	CU_ASSERT(rc >= 0);
49 
50 	childPid = fork();
51 	CU_ASSERT(childPid >= 0);
52 	if (childPid == 0) {
53 		ret = spdk_pci_device_claim(dev);
54 		CU_ASSERT(ret == -1);
55 		exit(0);
56 	} else {
57 		waitpid(childPid, &status, 0);
58 	}
59 }
60 
61 static struct spdk_pci_driver ut_pci_driver = {
62 	.is_registered = true,
63 };
64 
65 struct ut_pci_dev {
66 	struct spdk_pci_device pci;
67 	char config[16];
68 	char bar[16];
69 	bool attached;
70 };
71 
72 static int
73 ut_map_bar(struct spdk_pci_device *dev, uint32_t bar,
74 	   void **mapped_addr, uint64_t *phys_addr, uint64_t *size)
75 {
76 	struct ut_pci_dev *ut_dev = (struct ut_pci_dev *)dev;
77 
78 	/* just one bar */
79 	if (bar > 0) {
80 		return -1;
81 	}
82 
83 	*mapped_addr = ut_dev->bar;
84 	*phys_addr = 0;
85 	*size = sizeof(ut_dev->bar);
86 	return 0;
87 }
88 
89 static int
90 ut_unmap_bar(struct spdk_pci_device *device, uint32_t bar, void *addr)
91 {
92 	return 0;
93 }
94 
95 static int
96 ut_cfg_read(struct spdk_pci_device *dev, void *value, uint32_t len, uint32_t offset)
97 {
98 	struct ut_pci_dev *ut_dev = (struct ut_pci_dev *)dev;
99 
100 	if (len + offset >= sizeof(ut_dev->config)) {
101 		return -1;
102 	}
103 
104 	memcpy(value, (void *)((uintptr_t)ut_dev->config + offset), len);
105 	return 0;
106 }
107 
108 static int ut_cfg_write(struct spdk_pci_device *dev, void *value, uint32_t len, uint32_t offset)
109 {
110 	struct ut_pci_dev *ut_dev = (struct ut_pci_dev *)dev;
111 
112 	if (len + offset >= sizeof(ut_dev->config)) {
113 		return -1;
114 	}
115 
116 	memcpy((void *)((uintptr_t)ut_dev->config + offset), value, len);
117 	return 0;
118 }
119 
120 
121 static int
122 ut_enum_cb(void *ctx, struct spdk_pci_device *dev)
123 {
124 	struct ut_pci_dev *ut_dev = (struct ut_pci_dev *)dev;
125 
126 	ut_dev->attached = true;
127 	return 0;
128 }
129 
130 static void
131 ut_detach(struct spdk_pci_device *dev)
132 {
133 	struct ut_pci_dev *ut_dev = (struct ut_pci_dev *)dev;
134 
135 	ut_dev->attached = false;
136 }
137 
138 static void
139 pci_hook_test(void)
140 {
141 	struct ut_pci_dev ut_dev = {};
142 	uint32_t value_32;
143 	void *bar0_vaddr;
144 	uint64_t bar0_paddr, bar0_size;
145 	int rc;
146 
147 	ut_dev.pci.id.vendor_id = 0x4;
148 	ut_dev.pci.id.device_id = 0x8;
149 
150 	/* Use add parse for initilization */
151 	spdk_pci_addr_parse(&ut_dev.pci.addr, "10000:00:01.0");
152 	CU_ASSERT(ut_dev.pci.addr.domain == 0x10000);
153 	CU_ASSERT(ut_dev.pci.addr.bus == 0x0);
154 	CU_ASSERT(ut_dev.pci.addr.dev == 0x1);
155 	CU_ASSERT(ut_dev.pci.addr.func == 0x0);
156 
157 	ut_dev.pci.map_bar = ut_map_bar;
158 	ut_dev.pci.unmap_bar = ut_unmap_bar;
159 	ut_dev.pci.cfg_read = ut_cfg_read;
160 	ut_dev.pci.cfg_write = ut_cfg_write;
161 	ut_dev.pci.detach = ut_detach;
162 
163 	/* hook the device into the PCI layer */
164 	spdk_pci_hook_device(&ut_pci_driver, &ut_dev.pci);
165 
166 	/* try to attach a device with the matching driver and bdf */
167 	rc = spdk_pci_device_attach(&ut_pci_driver, ut_enum_cb, NULL, &ut_dev.pci.addr);
168 	CU_ASSERT(rc == 0);
169 	CU_ASSERT(ut_dev.pci.internal.attached);
170 	CU_ASSERT(ut_dev.attached);
171 
172 	/* check PCI config writes and reads */
173 	value_32 = 0xDEADBEEF;
174 	rc = spdk_pci_device_cfg_write32(&ut_dev.pci, value_32, 0);
175 	CU_ASSERT(rc == 0);
176 
177 	value_32 = 0x0BADF00D;
178 	rc = spdk_pci_device_cfg_write32(&ut_dev.pci, value_32, 4);
179 	CU_ASSERT(rc == 0);
180 
181 	rc = spdk_pci_device_cfg_read32(&ut_dev.pci, &value_32, 0);
182 	CU_ASSERT(rc == 0);
183 	CU_ASSERT(value_32 == 0xDEADBEEF);
184 	CU_ASSERT(memcmp(&value_32, &ut_dev.config[0], 4) == 0);
185 
186 	rc = spdk_pci_device_cfg_read32(&ut_dev.pci, &value_32, 4);
187 	CU_ASSERT(rc == 0);
188 	CU_ASSERT(value_32 == 0x0BADF00D);
189 	CU_ASSERT(memcmp(&value_32, &ut_dev.config[4], 4) == 0);
190 
191 	/* out-of-bounds write */
192 	rc = spdk_pci_device_cfg_read32(&ut_dev.pci, &value_32, sizeof(ut_dev.config));
193 	CU_ASSERT(rc != 0);
194 
195 	/* map a bar */
196 	rc = spdk_pci_device_map_bar(&ut_dev.pci, 0, &bar0_vaddr, &bar0_paddr, &bar0_size);
197 	CU_ASSERT(rc == 0);
198 	CU_ASSERT(bar0_vaddr == ut_dev.bar);
199 	CU_ASSERT(bar0_size == sizeof(ut_dev.bar));
200 	spdk_pci_device_unmap_bar(&ut_dev.pci, 0, bar0_vaddr);
201 
202 	/* map an inaccessible bar */
203 	rc = spdk_pci_device_map_bar(&ut_dev.pci, 1, &bar0_vaddr, &bar0_paddr, &bar0_size);
204 	CU_ASSERT(rc != 0);
205 
206 	/* test spdk_pci_device_claim() */
207 	pci_claim_test(&ut_dev.pci);
208 
209 	/* detach and verify our callback was called */
210 	spdk_pci_device_detach(&ut_dev.pci);
211 	CU_ASSERT(!ut_dev.attached);
212 	CU_ASSERT(!ut_dev.pci.internal.attached);
213 
214 	/* unhook the device */
215 	spdk_pci_unhook_device(&ut_dev.pci);
216 
217 	/* try to attach the same device again */
218 	rc = spdk_pci_device_attach(&ut_pci_driver, ut_enum_cb, NULL, &ut_dev.pci.addr);
219 	CU_ASSERT(rc != 0);
220 }
221 
222 int main(int argc, char **argv)
223 {
224 	CU_pSuite	suite = NULL;
225 	unsigned int	num_failures;
226 
227 	if (CU_initialize_registry() != CUE_SUCCESS) {
228 		return CU_get_error();
229 	}
230 
231 	suite = CU_add_suite("pci", NULL, NULL);
232 	if (suite == NULL) {
233 		CU_cleanup_registry();
234 		return CU_get_error();
235 	}
236 
237 	if (
238 		CU_add_test(suite, "pci_hook", pci_hook_test) == NULL
239 	) {
240 		CU_cleanup_registry();
241 		return CU_get_error();
242 	}
243 
244 	CU_basic_set_mode(CU_BRM_VERBOSE);
245 	CU_basic_run_tests();
246 	num_failures = CU_get_number_of_failures();
247 	CU_cleanup_registry();
248 	return num_failures;
249 }
250