4 * Copyright(c) 2010-2014 Intel Corporation. All rights reserved.
5 * Copyright(c) 2014 6WIND S.A.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
12 * * Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * * Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in
16 * the documentation and/or other materials provided with the
18 * * Neither the name of Intel Corporation nor the names of its
19 * contributors may be used to endorse or promote products derived
20 * from this software without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
25 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
26 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
27 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
28 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
29 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
30 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
31 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
32 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
38 #include <sys/queue.h>
40 #include <rte_interrupts.h>
42 #include <rte_ethdev.h>
43 #include <rte_devargs.h>
48 /* Generic maximum number of drivers to have room to allocate all drivers */
49 #define NUM_MAX_DRIVERS 256
55 * - Register a driver with a ``devinit()`` function.
57 * - Dump all PCI devices.
59 * - Check that the ``devinit()`` function is called at least once.
62 int test_pci_run = 0; /* value checked by the multiprocess test */
63 static unsigned pci_dev_count;
65 static int my_driver_init(struct rte_pci_driver *dr,
66 struct rte_pci_device *dev);
69 struct rte_pci_id my_driver_id[] = {
70 {RTE_PCI_DEVICE(0x0001, 0x1234)},
71 { .vendor_id = 0, /* sentinel */ },
74 struct rte_pci_id my_driver_id2[] = {
75 {RTE_PCI_DEVICE(0x0001, 0x4444)},
76 {RTE_PCI_DEVICE(0x0002, 0xabcd)},
77 { .vendor_id = 0, /* sentinel */ },
80 struct rte_pci_driver my_driver = {
81 .name = "test_driver",
82 .devinit = my_driver_init,
83 .id_table = my_driver_id,
87 struct rte_pci_driver my_driver2 = {
88 .name = "test_driver2",
89 .devinit = my_driver_init,
90 .id_table = my_driver_id2,
95 my_driver_init(__attribute__((unused)) struct rte_pci_driver *dr,
96 struct rte_pci_device *dev)
98 printf("My driver init called in %s\n", dr->name);
99 printf("%x:%x:%x.%d", dev->addr.domain, dev->addr.bus,
100 dev->addr.devid, dev->addr.function);
101 printf(" - vendor:%x device:%x\n", dev->id.vendor_id, dev->id.device_id);
108 blacklist_all_devices(void)
110 struct rte_pci_device *dev = NULL;
112 char pci_addr_str[16];
114 TAILQ_FOREACH(dev, &pci_device_list, next) {
115 snprintf(pci_addr_str, sizeof(pci_addr_str), PCI_PRI_FMT,
116 dev->addr.domain, dev->addr.bus, dev->addr.devid,
118 if (rte_eal_devargs_add(RTE_DEVTYPE_BLACKLISTED_PCI,
120 printf("Error: cannot blacklist <%s>", pci_addr_str);
125 printf("%u devices blacklisted\n", i);
128 /* clear devargs list that was modified by the test */
129 static void free_devargs_list(void)
131 struct rte_devargs *devargs;
133 while (!TAILQ_EMPTY(&devargs_list)) {
134 devargs = TAILQ_FIRST(&devargs_list);
135 TAILQ_REMOVE(&devargs_list, devargs, next);
141 /* backup real devices & drivers (not used for testing) */
142 struct pci_driver_list real_pci_driver_list =
143 TAILQ_HEAD_INITIALIZER(real_pci_driver_list);
144 struct pci_device_list real_pci_device_list =
145 TAILQ_HEAD_INITIALIZER(real_pci_device_list);
147 REGISTER_LINKED_RESOURCE(test_pci_sysfs);
152 struct rte_pci_device *dev;
153 struct rte_pci_driver *dr;
154 const struct resource *r;
157 r = resource_find("test_pci_sysfs");
158 TEST_ASSERT_NOT_NULL(r, "missing resource test_pci_sysfs");
160 ret = resource_untar(r);
161 TEST_ASSERT_SUCCESS(ret, "failed to untar %s", r->name);
163 ret = setenv("SYSFS_PCI_DEVICES", "test_pci_sysfs/bus/pci/devices", 1);
164 TEST_ASSERT_SUCCESS(ret, "failed to setenv");
166 /* Unregister original devices & drivers lists */
167 while (!TAILQ_EMPTY(&pci_driver_list)) {
168 dr = TAILQ_FIRST(&pci_driver_list);
169 rte_eal_pci_unregister(dr);
170 TAILQ_INSERT_TAIL(&real_pci_driver_list, dr, next);
173 while (!TAILQ_EMPTY(&pci_device_list)) {
174 dev = TAILQ_FIRST(&pci_device_list);
175 TAILQ_REMOVE(&pci_device_list, dev, next);
176 TAILQ_INSERT_TAIL(&real_pci_device_list, dev, next);
179 ret = rte_eal_pci_scan();
180 TEST_ASSERT_SUCCESS(ret, "failed to scan PCI bus");
181 rte_eal_pci_dump(stdout);
187 test_pci_cleanup(void)
189 struct rte_pci_device *dev;
190 struct rte_pci_driver *dr;
191 const struct resource *r;
194 unsetenv("SYSFS_PCI_DEVICES");
196 r = resource_find("test_pci_sysfs");
197 TEST_ASSERT_NOT_NULL(r, "missing resource test_pci_sysfs");
199 ret = resource_rm_by_tar(r);
200 TEST_ASSERT_SUCCESS(ret, "Failed to delete resource %s", r->name);
203 * FIXME: there is no API in DPDK to free a rte_pci_device so we
204 * cannot free the devices in the right way. Let's assume that we
205 * don't care for tests.
207 while (!TAILQ_EMPTY(&pci_device_list)) {
208 dev = TAILQ_FIRST(&pci_device_list);
209 TAILQ_REMOVE(&pci_device_list, dev, next);
212 /* Restore original devices & drivers lists */
213 while (!TAILQ_EMPTY(&real_pci_driver_list)) {
214 dr = TAILQ_FIRST(&real_pci_driver_list);
215 TAILQ_REMOVE(&real_pci_driver_list, dr, next);
216 rte_eal_pci_register(dr);
219 while (!TAILQ_EMPTY(&real_pci_device_list)) {
220 dev = TAILQ_FIRST(&real_pci_device_list);
221 TAILQ_REMOVE(&real_pci_device_list, dev, next);
222 TAILQ_INSERT_TAIL(&pci_device_list, dev, next);
229 test_pci_blacklist(void)
231 struct rte_devargs_list save_devargs_list;
233 printf("Dump all devices\n");
234 TEST_ASSERT(TAILQ_EMPTY(&pci_driver_list),
235 "pci_driver_list not empty");
237 rte_eal_pci_register(&my_driver);
238 rte_eal_pci_register(&my_driver2);
241 printf("Scan bus\n");
244 if (pci_dev_count == 0) {
245 printf("no device detected\n");
249 /* save the real devargs_list */
250 save_devargs_list = devargs_list;
251 TAILQ_INIT(&devargs_list);
253 blacklist_all_devices();
256 printf("Scan bus with all devices blacklisted\n");
260 devargs_list = save_devargs_list;
262 if (pci_dev_count != 0) {
263 printf("not all devices are blacklisted\n");
269 rte_eal_pci_unregister(&my_driver);
270 rte_eal_pci_unregister(&my_driver2);
275 static int test_pci_sysfs(void)
281 orig = pci_get_sysfs_path();
282 ret = setenv("SYSFS_PCI_DEVICES", "My Documents", 1);
283 TEST_ASSERT_SUCCESS(ret, "Failed setenv to My Documents");
285 newpath = pci_get_sysfs_path();
286 TEST_ASSERT(!strcmp(newpath, "My Documents"),
287 "pci_get_sysfs_path() should return 'My Documents' "
288 "but gives %s", newpath);
290 ret = setenv("SYSFS_PCI_DEVICES", orig, 1);
291 TEST_ASSERT_SUCCESS(ret, "Failed setenv back to '%s'", orig);
293 newpath = pci_get_sysfs_path();
294 TEST_ASSERT(!strcmp(orig, newpath),
295 "pci_get_sysfs_path returned unexpected path: "
296 "%s (expected: %s)", newpath, orig);
303 if (test_pci_sysfs())
306 if (test_pci_setup())
309 if (test_pci_blacklist())
312 if (test_pci_cleanup())
318 static struct test_command pci_cmd = {
319 .command = "pci_autotest",
320 .callback = test_pci,
322 REGISTER_TEST_COMMAND(pci_cmd);