app/test: convert all tests to register system
[dpdk.git] / app / test / test_pci.c
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright(c) 2010-2014 Intel Corporation. All rights reserved.
5  *   Copyright(c) 2014 6WIND S.A.
6  *   All rights reserved.
7  *
8  *   Redistribution and use in source and binary forms, with or without
9  *   modification, are permitted provided that the following conditions
10  *   are met:
11  *
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
17  *       distribution.
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.
21  *
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.
33  */
34
35 #include <stdio.h>
36 #include <string.h>
37 #include <stdint.h>
38 #include <sys/queue.h>
39
40 #include <rte_interrupts.h>
41 #include <rte_pci.h>
42 #include <rte_devargs.h>
43
44 #include "test.h"
45
46
47 /*
48  * PCI test
49  * ========
50  *
51  * - Register a driver with a ``devinit()`` function.
52  *
53  * - Dump all PCI devices.
54  *
55  * - Check that the ``devinit()`` function is called at least once.
56  */
57
58 int test_pci_run = 0; /* value checked by the multiprocess test */
59 static unsigned pci_dev_count;
60 static unsigned driver_registered = 0;
61
62 static int my_driver_init(struct rte_pci_driver *dr,
63                           struct rte_pci_device *dev);
64
65 /*
66  * To test cases where RTE_PCI_DRV_NEED_MAPPING is set, and isn't set, two
67  * drivers are created (one with IGB devices, the other with IXGBE devices).
68  */
69
70 /* IXGBE NICS */
71 struct rte_pci_id my_driver_id[] = {
72
73 #define RTE_PCI_DEV_ID_DECL_IXGBE(vend, dev) {RTE_PCI_DEVICE(vend, dev)},
74 #include <rte_pci_dev_ids.h>
75
76 { .vendor_id = 0, /* sentinel */ },
77 };
78
79 struct rte_pci_id my_driver_id2[] = {
80
81 /* IGB & EM NICS */
82 #define RTE_PCI_DEV_ID_DECL_EM(vend, dev) {RTE_PCI_DEVICE(vend, dev)},
83 #define RTE_PCI_DEV_ID_DECL_IGB(vend, dev) {RTE_PCI_DEVICE(vend, dev)},
84 #include <rte_pci_dev_ids.h>
85
86 { .vendor_id = 0, /* sentinel */ },
87 };
88
89 struct rte_pci_driver my_driver = {
90         .name = "test_driver",
91         .devinit = my_driver_init,
92         .id_table = my_driver_id,
93         .drv_flags = RTE_PCI_DRV_NEED_MAPPING,
94 };
95
96 struct rte_pci_driver my_driver2 = {
97         .name = "test_driver2",
98         .devinit = my_driver_init,
99         .id_table = my_driver_id2,
100         .drv_flags = 0,
101 };
102
103 static int
104 my_driver_init(__attribute__((unused)) struct rte_pci_driver *dr,
105                struct rte_pci_device *dev)
106 {
107         printf("My driver init called in %s\n", dr->name);
108         printf("%x:%x:%x.%d", dev->addr.domain, dev->addr.bus,
109                dev->addr.devid, dev->addr.function);
110         printf(" - vendor:%x device:%x\n", dev->id.vendor_id, dev->id.device_id);
111
112         pci_dev_count ++;
113         return 0;
114 }
115
116 static void
117 blacklist_all_devices(void)
118 {
119         struct rte_pci_device *dev = NULL;
120         unsigned i = 0;
121         char pci_addr_str[16];
122
123         TAILQ_FOREACH(dev, &pci_device_list, next) {
124                 snprintf(pci_addr_str, sizeof(pci_addr_str), PCI_PRI_FMT,
125                         dev->addr.domain, dev->addr.bus, dev->addr.devid,
126                         dev->addr.function);
127                 if (rte_eal_devargs_add(RTE_DEVTYPE_BLACKLISTED_PCI,
128                                 pci_addr_str) < 0) {
129                         printf("Error: cannot blacklist <%s>", pci_addr_str);
130                         break;
131                 }
132                 i++;
133         }
134         printf("%u devices blacklisted\n", i);
135 }
136
137 /* clear devargs list that was modified by the test */
138 static void free_devargs_list(void)
139 {
140         struct rte_devargs *devargs;
141
142         while (!TAILQ_EMPTY(&devargs_list)) {
143                 devargs = TAILQ_FIRST(&devargs_list);
144                 TAILQ_REMOVE(&devargs_list, devargs, next);
145                 free(devargs);
146         }
147 }
148
149 int
150 test_pci(void)
151 {
152         struct rte_devargs_list save_devargs_list;
153
154         printf("Dump all devices\n");
155         rte_eal_pci_dump(stdout);
156         if (driver_registered == 0) {
157                 rte_eal_pci_register(&my_driver);
158                 rte_eal_pci_register(&my_driver2);
159                 driver_registered = 1;
160         }
161
162         pci_dev_count = 0;
163         printf("Scan bus\n");
164         rte_eal_pci_probe();
165
166         if (pci_dev_count == 0) {
167                 printf("no device detected\n");
168                 return -1;
169         }
170
171         /* save the real devargs_list */
172         save_devargs_list = devargs_list;
173         TAILQ_INIT(&devargs_list);
174
175         blacklist_all_devices();
176
177         pci_dev_count = 0;
178         printf("Scan bus with all devices blacklisted\n");
179         rte_eal_pci_probe();
180
181         free_devargs_list();
182         devargs_list = save_devargs_list;
183
184         if (pci_dev_count != 0) {
185                 printf("not all devices are blacklisted\n");
186                 return -1;
187         }
188
189         test_pci_run = 1;
190         if (driver_registered == 1) {
191                 rte_eal_pci_unregister(&my_driver);
192                 rte_eal_pci_unregister(&my_driver2);
193                 driver_registered = 0;
194         }
195
196         return 0;
197 }
198
199 static struct test_command pci_cmd = {
200         .command = "pci_autotest",
201         .callback = test_pci,
202 };
203 REGISTER_TEST_COMMAND(pci_cmd);