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