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>
47 /* Generic maximum number of drivers to have room to allocate all drivers */
48 #define NUM_MAX_DRIVERS 256
54 * - Register a driver with a ``devinit()`` function.
56 * - Dump all PCI devices.
58 * - Check that the ``devinit()`` function is called at least once.
61 int test_pci_run = 0; /* value checked by the multiprocess test */
62 static unsigned pci_dev_count;
64 static int my_driver_init(struct rte_pci_driver *dr,
65 struct rte_pci_device *dev);
68 * To test cases where RTE_PCI_DRV_NEED_MAPPING is set, and isn't set, two
69 * drivers are created (one with IGB devices, the other with IXGBE devices).
73 struct rte_pci_id my_driver_id[] = {
75 #define RTE_PCI_DEV_ID_DECL_IXGBE(vend, dev) {RTE_PCI_DEVICE(vend, dev)},
76 #include <rte_pci_dev_ids.h>
78 { .vendor_id = 0, /* sentinel */ },
81 struct rte_pci_id my_driver_id2[] = {
84 #define RTE_PCI_DEV_ID_DECL_EM(vend, dev) {RTE_PCI_DEVICE(vend, dev)},
85 #define RTE_PCI_DEV_ID_DECL_IGB(vend, dev) {RTE_PCI_DEVICE(vend, dev)},
86 #include <rte_pci_dev_ids.h>
88 { .vendor_id = 0, /* sentinel */ },
91 struct rte_pci_driver my_driver = {
92 .name = "test_driver",
93 .devinit = my_driver_init,
94 .id_table = my_driver_id,
95 .drv_flags = RTE_PCI_DRV_NEED_MAPPING,
98 struct rte_pci_driver my_driver2 = {
99 .name = "test_driver2",
100 .devinit = my_driver_init,
101 .id_table = my_driver_id2,
106 my_driver_init(__attribute__((unused)) struct rte_pci_driver *dr,
107 struct rte_pci_device *dev)
109 printf("My driver init called in %s\n", dr->name);
110 printf("%x:%x:%x.%d", dev->addr.domain, dev->addr.bus,
111 dev->addr.devid, dev->addr.function);
112 printf(" - vendor:%x device:%x\n", dev->id.vendor_id, dev->id.device_id);
119 blacklist_all_devices(void)
121 struct rte_pci_device *dev = NULL;
123 char pci_addr_str[16];
125 TAILQ_FOREACH(dev, &pci_device_list, next) {
126 snprintf(pci_addr_str, sizeof(pci_addr_str), PCI_PRI_FMT,
127 dev->addr.domain, dev->addr.bus, dev->addr.devid,
129 if (rte_eal_devargs_add(RTE_DEVTYPE_BLACKLISTED_PCI,
131 printf("Error: cannot blacklist <%s>", pci_addr_str);
136 printf("%u devices blacklisted\n", i);
139 /* clear devargs list that was modified by the test */
140 static void free_devargs_list(void)
142 struct rte_devargs *devargs;
144 while (!TAILQ_EMPTY(&devargs_list)) {
145 devargs = TAILQ_FIRST(&devargs_list);
146 TAILQ_REMOVE(&devargs_list, devargs, next);
154 struct rte_devargs_list save_devargs_list;
155 struct rte_pci_driver *dr = NULL;
156 struct rte_pci_driver *save_pci_driver_list[NUM_MAX_DRIVERS];
157 unsigned i, num_drivers = 0;
159 printf("Dump all devices\n");
160 rte_eal_pci_dump(stdout);
162 /* Unregister all previous drivers */
163 TAILQ_FOREACH(dr, &pci_driver_list, next) {
164 rte_eal_pci_unregister(dr);
165 save_pci_driver_list[num_drivers++] = dr;
168 rte_eal_pci_register(&my_driver);
169 rte_eal_pci_register(&my_driver2);
172 printf("Scan bus\n");
175 if (pci_dev_count == 0) {
176 printf("no device detected\n");
180 /* save the real devargs_list */
181 save_devargs_list = devargs_list;
182 TAILQ_INIT(&devargs_list);
184 blacklist_all_devices();
187 printf("Scan bus with all devices blacklisted\n");
191 devargs_list = save_devargs_list;
193 if (pci_dev_count != 0) {
194 printf("not all devices are blacklisted\n");
200 rte_eal_pci_unregister(&my_driver);
201 rte_eal_pci_unregister(&my_driver2);
203 /* Restore original driver list */
204 for (i = 0; i < num_drivers; i++)
205 rte_eal_pci_register(save_pci_driver_list[i]);
210 static struct test_command pci_cmd = {
211 .command = "pci_autotest",
212 .callback = test_pci,
214 REGISTER_TEST_COMMAND(pci_cmd);