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"
37 #include "spdk_internal/mock.h"
39 #include "env_dpdk/pci.c"
42 pci_claim_test(struct spdk_pci_device
*dev
)
48 rc
= spdk_pci_device_claim(dev
);
52 CU_ASSERT(childPid
>= 0);
54 ret
= spdk_pci_device_claim(dev
);
58 waitpid(childPid
, &status
, 0);
62 static struct spdk_pci_driver ut_pci_driver
;
65 struct spdk_pci_device pci
;
72 ut_map_bar(struct spdk_pci_device
*dev
, uint32_t bar
,
73 void **mapped_addr
, uint64_t *phys_addr
, uint64_t *size
)
75 struct ut_pci_dev
*ut_dev
= (struct ut_pci_dev
*)dev
;
82 *mapped_addr
= ut_dev
->bar
;
84 *size
= sizeof(ut_dev
->bar
);
89 ut_unmap_bar(struct spdk_pci_device
*device
, uint32_t bar
, void *addr
)
95 ut_cfg_read(struct spdk_pci_device
*dev
, void *value
, uint32_t len
, uint32_t offset
)
97 struct ut_pci_dev
*ut_dev
= (struct ut_pci_dev
*)dev
;
99 if (len
+ offset
>= sizeof(ut_dev
->config
)) {
103 memcpy(value
, (void *)((uintptr_t)ut_dev
->config
+ offset
), len
);
107 static int ut_cfg_write(struct spdk_pci_device
*dev
, void *value
, uint32_t len
, uint32_t offset
)
109 struct ut_pci_dev
*ut_dev
= (struct ut_pci_dev
*)dev
;
111 if (len
+ offset
>= sizeof(ut_dev
->config
)) {
115 memcpy((void *)((uintptr_t)ut_dev
->config
+ offset
), value
, len
);
121 ut_enum_cb(void *ctx
, struct spdk_pci_device
*dev
)
123 struct ut_pci_dev
*ut_dev
= (struct ut_pci_dev
*)dev
;
125 ut_dev
->attached
= true;
132 struct ut_pci_dev ut_dev
= {};
135 uint64_t bar0_paddr
, bar0_size
;
138 ut_dev
.pci
.type
= "custom";
139 ut_dev
.pci
.id
.vendor_id
= 0x4;
140 ut_dev
.pci
.id
.device_id
= 0x8;
142 /* Use add parse for initilization */
143 spdk_pci_addr_parse(&ut_dev
.pci
.addr
, "10000:00:01.0");
144 CU_ASSERT(ut_dev
.pci
.addr
.domain
== 0x10000);
145 CU_ASSERT(ut_dev
.pci
.addr
.bus
== 0x0);
146 CU_ASSERT(ut_dev
.pci
.addr
.dev
== 0x1);
147 CU_ASSERT(ut_dev
.pci
.addr
.func
== 0x0);
149 ut_dev
.pci
.map_bar
= ut_map_bar
;
150 ut_dev
.pci
.unmap_bar
= ut_unmap_bar
;
151 ut_dev
.pci
.cfg_read
= ut_cfg_read
;
152 ut_dev
.pci
.cfg_write
= ut_cfg_write
;
154 /* hook the device into the PCI layer */
155 spdk_pci_hook_device(&ut_pci_driver
, &ut_dev
.pci
);
157 /* try to attach a device with the matching driver and bdf */
158 rc
= spdk_pci_device_attach(&ut_pci_driver
, ut_enum_cb
, NULL
, &ut_dev
.pci
.addr
);
160 CU_ASSERT(ut_dev
.pci
.internal
.attached
);
161 CU_ASSERT(ut_dev
.attached
);
163 /* check PCI config writes and reads */
164 value_32
= 0xDEADBEEF;
165 rc
= spdk_pci_device_cfg_write32(&ut_dev
.pci
, value_32
, 0);
168 value_32
= 0x0BADF00D;
169 rc
= spdk_pci_device_cfg_write32(&ut_dev
.pci
, value_32
, 4);
172 rc
= spdk_pci_device_cfg_read32(&ut_dev
.pci
, &value_32
, 0);
174 CU_ASSERT(value_32
== 0xDEADBEEF);
175 CU_ASSERT(memcmp(&value_32
, &ut_dev
.config
[0], 4) == 0);
177 rc
= spdk_pci_device_cfg_read32(&ut_dev
.pci
, &value_32
, 4);
179 CU_ASSERT(value_32
== 0x0BADF00D);
180 CU_ASSERT(memcmp(&value_32
, &ut_dev
.config
[4], 4) == 0);
182 /* out-of-bounds write */
183 rc
= spdk_pci_device_cfg_read32(&ut_dev
.pci
, &value_32
, sizeof(ut_dev
.config
));
187 rc
= spdk_pci_device_map_bar(&ut_dev
.pci
, 0, &bar0_vaddr
, &bar0_paddr
, &bar0_size
);
189 CU_ASSERT(bar0_vaddr
== ut_dev
.bar
);
190 CU_ASSERT(bar0_size
== sizeof(ut_dev
.bar
));
191 spdk_pci_device_unmap_bar(&ut_dev
.pci
, 0, bar0_vaddr
);
193 /* map an inaccessible bar */
194 rc
= spdk_pci_device_map_bar(&ut_dev
.pci
, 1, &bar0_vaddr
, &bar0_paddr
, &bar0_size
);
197 /* test spdk_pci_device_claim() */
198 pci_claim_test(&ut_dev
.pci
);
200 spdk_pci_device_detach(&ut_dev
.pci
);
201 CU_ASSERT(!ut_dev
.pci
.internal
.attached
);
203 /* unhook the device */
204 spdk_pci_unhook_device(&ut_dev
.pci
);
206 /* try to attach the same device again */
207 rc
= spdk_pci_device_attach(&ut_pci_driver
, ut_enum_cb
, NULL
, &ut_dev
.pci
.addr
);
211 int main(int argc
, char **argv
)
213 CU_pSuite suite
= NULL
;
214 unsigned int num_failures
;
216 if (CU_initialize_registry() != CUE_SUCCESS
) {
217 return CU_get_error();
220 suite
= CU_add_suite("pci", NULL
, NULL
);
222 CU_cleanup_registry();
223 return CU_get_error();
227 CU_add_test(suite
, "pci_hook", pci_hook_test
) == NULL
229 CU_cleanup_registry();
230 return CU_get_error();
233 CU_basic_set_mode(CU_BRM_VERBOSE
);
234 CU_basic_run_tests();
235 num_failures
= CU_get_number_of_failures();
236 CU_cleanup_registry();