4 * Copyright (c) Intel Corporation.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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
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.
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.
34 #include "spdk/stdinc.h"
36 #include "CUnit/Basic.h"
38 #include "env_dpdk/pci.c"
46 struct spdk_pci_addr pci_addr
;
48 pci_addr
.domain
= 0x0;
53 rc
= spdk_pci_device_claim(&pci_addr
);
57 CU_ASSERT(childPid
>= 0);
59 ret
= spdk_pci_device_claim(&pci_addr
);
63 waitpid(childPid
, &status
, 0);
67 static struct spdk_pci_driver ut_pci_driver
= {
68 .is_registered
= true,
72 struct spdk_pci_device pci
;
79 ut_map_bar(struct spdk_pci_device
*dev
, uint32_t bar
,
80 void **mapped_addr
, uint64_t *phys_addr
, uint64_t *size
)
82 struct ut_pci_dev
*ut_dev
= (struct ut_pci_dev
*)dev
;
89 *mapped_addr
= ut_dev
->bar
;
91 *size
= sizeof(ut_dev
->bar
);
96 ut_unmap_bar(struct spdk_pci_device
*device
, uint32_t bar
, void *addr
)
102 ut_cfg_read(struct spdk_pci_device
*dev
, void *value
, uint32_t len
, uint32_t offset
)
104 struct ut_pci_dev
*ut_dev
= (struct ut_pci_dev
*)dev
;
106 if (len
+ offset
>= sizeof(ut_dev
->config
)) {
110 memcpy(value
, (void *)((uintptr_t)ut_dev
->config
+ offset
), len
);
114 static int ut_cfg_write(struct spdk_pci_device
*dev
, void *value
, uint32_t len
, uint32_t offset
)
116 struct ut_pci_dev
*ut_dev
= (struct ut_pci_dev
*)dev
;
118 if (len
+ offset
>= sizeof(ut_dev
->config
)) {
122 memcpy((void *)((uintptr_t)ut_dev
->config
+ offset
), value
, len
);
128 ut_enum_cb(void *ctx
, struct spdk_pci_device
*dev
)
130 struct ut_pci_dev
*ut_dev
= (struct ut_pci_dev
*)dev
;
132 ut_dev
->attached
= true;
137 ut_detach(struct spdk_pci_device
*dev
)
139 struct ut_pci_dev
*ut_dev
= (struct ut_pci_dev
*)dev
;
141 ut_dev
->attached
= false;
147 struct ut_pci_dev ut_dev
= {};
150 uint64_t bar0_paddr
, bar0_size
;
153 ut_dev
.pci
.addr
.domain
= 0x10000;
154 ut_dev
.pci
.addr
.bus
= 0x0;
155 ut_dev
.pci
.addr
.dev
= 0x1;
156 ut_dev
.pci
.addr
.func
= 0x0;
157 ut_dev
.pci
.id
.vendor_id
= 0x4;
158 ut_dev
.pci
.id
.device_id
= 0x8;
160 ut_dev
.pci
.map_bar
= ut_map_bar
;
161 ut_dev
.pci
.unmap_bar
= ut_unmap_bar
;
162 ut_dev
.pci
.cfg_read
= ut_cfg_read
;
163 ut_dev
.pci
.cfg_write
= ut_cfg_write
;
164 ut_dev
.pci
.detach
= ut_detach
;
166 /* hook the device into the PCI layer */
167 spdk_pci_hook_device(&ut_pci_driver
, &ut_dev
.pci
);
169 /* try to attach a device with the matching driver and bdf */
170 rc
= spdk_pci_device_attach(&ut_pci_driver
, ut_enum_cb
, NULL
, &ut_dev
.pci
.addr
);
172 CU_ASSERT(ut_dev
.pci
.internal
.attached
);
173 CU_ASSERT(ut_dev
.attached
);
175 /* check PCI config writes and reads */
176 value_32
= 0xDEADBEEF;
177 rc
= spdk_pci_device_cfg_write32(&ut_dev
.pci
, value_32
, 0);
180 value_32
= 0x0BADF00D;
181 rc
= spdk_pci_device_cfg_write32(&ut_dev
.pci
, value_32
, 4);
184 rc
= spdk_pci_device_cfg_read32(&ut_dev
.pci
, &value_32
, 0);
186 CU_ASSERT(value_32
== 0xDEADBEEF);
187 CU_ASSERT(memcmp(&value_32
, &ut_dev
.config
[0], 4) == 0);
189 rc
= spdk_pci_device_cfg_read32(&ut_dev
.pci
, &value_32
, 4);
191 CU_ASSERT(value_32
== 0x0BADF00D);
192 CU_ASSERT(memcmp(&value_32
, &ut_dev
.config
[4], 4) == 0);
194 /* out-of-bounds write */
195 rc
= spdk_pci_device_cfg_read32(&ut_dev
.pci
, &value_32
, sizeof(ut_dev
.config
));
199 rc
= spdk_pci_device_map_bar(&ut_dev
.pci
, 0, &bar0_vaddr
, &bar0_paddr
, &bar0_size
);
201 CU_ASSERT(bar0_vaddr
== ut_dev
.bar
);
202 CU_ASSERT(bar0_size
== sizeof(ut_dev
.bar
));
203 spdk_pci_device_unmap_bar(&ut_dev
.pci
, 0, bar0_vaddr
);
205 /* map an inaccessible bar */
206 rc
= spdk_pci_device_map_bar(&ut_dev
.pci
, 1, &bar0_vaddr
, &bar0_paddr
, &bar0_size
);
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
);
214 /* unhook the device */
215 spdk_pci_unhook_device(&ut_dev
.pci
);
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
);
222 int main(int argc
, char **argv
)
224 CU_pSuite suite
= NULL
;
225 unsigned int num_failures
;
227 if (CU_initialize_registry() != CUE_SUCCESS
) {
228 return CU_get_error();
231 suite
= CU_add_suite("pci", NULL
, NULL
);
233 CU_cleanup_registry();
234 return CU_get_error();
238 CU_add_test(suite
, "pci_claim", pci_claim_test
) == NULL
||
239 CU_add_test(suite
, "pci_hook", pci_hook_test
) == NULL
241 CU_cleanup_registry();
242 return CU_get_error();
245 CU_basic_set_mode(CU_BRM_VERBOSE
);
246 CU_basic_run_tests();
247 num_failures
= CU_get_number_of_failures();
248 CU_cleanup_registry();