app/test: do not dump devices in PCI blacklist test
[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_ethdev.h>
43 #include <rte_devargs.h>
44
45 #include "test.h"
46 #include "resource.h"
47
48 /* Generic maximum number of drivers to have room to allocate all drivers */
49 #define NUM_MAX_DRIVERS 256
50
51 /*
52  * PCI test
53  * ========
54  *
55  * - Register a driver with a ``devinit()`` function.
56  *
57  * - Dump all PCI devices.
58  *
59  * - Check that the ``devinit()`` function is called at least once.
60  */
61
62 int test_pci_run = 0; /* value checked by the multiprocess test */
63 static unsigned pci_dev_count;
64
65 static int my_driver_init(struct rte_pci_driver *dr,
66                           struct rte_pci_device *dev);
67
68 /* IXGBE NICS */
69 struct rte_pci_id my_driver_id[] = {
70
71 #define RTE_PCI_DEV_ID_DECL_IXGBE(vend, dev) {RTE_PCI_DEVICE(vend, dev)},
72 #include <rte_pci_dev_ids.h>
73
74 { .vendor_id = 0, /* sentinel */ },
75 };
76
77 struct rte_pci_id my_driver_id2[] = {
78
79 /* IGB & EM NICS */
80 #define RTE_PCI_DEV_ID_DECL_EM(vend, dev) {RTE_PCI_DEVICE(vend, dev)},
81 #define RTE_PCI_DEV_ID_DECL_IGB(vend, dev) {RTE_PCI_DEVICE(vend, dev)},
82 #include <rte_pci_dev_ids.h>
83
84 { .vendor_id = 0, /* sentinel */ },
85 };
86
87 struct rte_pci_driver my_driver = {
88         .name = "test_driver",
89         .devinit = my_driver_init,
90         .id_table = my_driver_id,
91         .drv_flags = 0,
92 };
93
94 struct rte_pci_driver my_driver2 = {
95         .name = "test_driver2",
96         .devinit = my_driver_init,
97         .id_table = my_driver_id2,
98         .drv_flags = 0,
99 };
100
101 static int
102 my_driver_init(__attribute__((unused)) struct rte_pci_driver *dr,
103                struct rte_pci_device *dev)
104 {
105         printf("My driver init called in %s\n", dr->name);
106         printf("%x:%x:%x.%d", dev->addr.domain, dev->addr.bus,
107                dev->addr.devid, dev->addr.function);
108         printf(" - vendor:%x device:%x\n", dev->id.vendor_id, dev->id.device_id);
109
110         pci_dev_count ++;
111         return 0;
112 }
113
114 static void
115 blacklist_all_devices(void)
116 {
117         struct rte_pci_device *dev = NULL;
118         unsigned i = 0;
119         char pci_addr_str[16];
120
121         TAILQ_FOREACH(dev, &pci_device_list, next) {
122                 snprintf(pci_addr_str, sizeof(pci_addr_str), PCI_PRI_FMT,
123                         dev->addr.domain, dev->addr.bus, dev->addr.devid,
124                         dev->addr.function);
125                 if (rte_eal_devargs_add(RTE_DEVTYPE_BLACKLISTED_PCI,
126                                 pci_addr_str) < 0) {
127                         printf("Error: cannot blacklist <%s>", pci_addr_str);
128                         break;
129                 }
130                 i++;
131         }
132         printf("%u devices blacklisted\n", i);
133 }
134
135 /* clear devargs list that was modified by the test */
136 static void free_devargs_list(void)
137 {
138         struct rte_devargs *devargs;
139
140         while (!TAILQ_EMPTY(&devargs_list)) {
141                 devargs = TAILQ_FIRST(&devargs_list);
142                 TAILQ_REMOVE(&devargs_list, devargs, next);
143                 free(devargs->args);
144                 free(devargs);
145         }
146 }
147
148 /* backup real devices & drivers (not used for testing) */
149 struct pci_driver_list real_pci_driver_list =
150         TAILQ_HEAD_INITIALIZER(real_pci_driver_list);
151 struct pci_device_list real_pci_device_list =
152         TAILQ_HEAD_INITIALIZER(real_pci_device_list);
153
154 REGISTER_LINKED_RESOURCE(test_pci_sysfs);
155
156 static int
157 test_pci_setup(void)
158 {
159         struct rte_pci_device *dev;
160         struct rte_pci_driver *dr;
161         const struct resource *r;
162         int ret;
163
164         r = resource_find("test_pci_sysfs");
165         TEST_ASSERT_NOT_NULL(r, "missing resource test_pci_sysfs");
166
167         ret = resource_untar(r);
168         TEST_ASSERT_SUCCESS(ret, "failed to untar %s", r->name);
169
170         ret = setenv("SYSFS_PCI_DEVICES", "test_pci_sysfs/bus/pci/devices", 1);
171         TEST_ASSERT_SUCCESS(ret, "failed to setenv");
172
173         /* Unregister original devices & drivers lists */
174         while (!TAILQ_EMPTY(&pci_driver_list)) {
175                 dr = TAILQ_FIRST(&pci_driver_list);
176                 rte_eal_pci_unregister(dr);
177                 TAILQ_INSERT_TAIL(&real_pci_driver_list, dr, next);
178         }
179
180         while (!TAILQ_EMPTY(&pci_device_list)) {
181                 dev = TAILQ_FIRST(&pci_device_list);
182                 TAILQ_REMOVE(&pci_device_list, dev, next);
183                 TAILQ_INSERT_TAIL(&real_pci_device_list, dev, next);
184         }
185
186         ret = rte_eal_pci_scan();
187         TEST_ASSERT_SUCCESS(ret, "failed to scan PCI bus");
188         rte_eal_pci_dump(stdout);
189
190         return 0;
191 }
192
193 static int
194 test_pci_cleanup(void)
195 {
196         struct rte_pci_device *dev;
197         struct rte_pci_driver *dr;
198         const struct resource *r;
199         int ret;
200
201         unsetenv("SYSFS_PCI_DEVICES");
202
203         r = resource_find("test_pci_sysfs");
204         TEST_ASSERT_NOT_NULL(r, "missing resource test_pci_sysfs");
205
206         ret = resource_rm_by_tar(r);
207         TEST_ASSERT_SUCCESS(ret, "Failed to delete resource %s", r->name);
208
209         /*
210          * FIXME: there is no API in DPDK to free a rte_pci_device so we
211          * cannot free the devices in the right way. Let's assume that we
212          * don't care for tests.
213          */
214         while (!TAILQ_EMPTY(&pci_device_list)) {
215                 dev = TAILQ_FIRST(&pci_device_list);
216                 TAILQ_REMOVE(&pci_device_list, dev, next);
217         }
218
219         /* Restore original devices & drivers lists */
220         while (!TAILQ_EMPTY(&real_pci_driver_list)) {
221                 dr = TAILQ_FIRST(&real_pci_driver_list);
222                 TAILQ_REMOVE(&real_pci_driver_list, dr, next);
223                 rte_eal_pci_register(dr);
224         }
225
226         while (!TAILQ_EMPTY(&real_pci_device_list)) {
227                 dev = TAILQ_FIRST(&real_pci_device_list);
228                 TAILQ_REMOVE(&real_pci_device_list, dev, next);
229                 TAILQ_INSERT_TAIL(&pci_device_list, dev, next);
230         }
231
232         return 0;
233 }
234
235 static int
236 test_pci_blacklist(void)
237 {
238         struct rte_devargs_list save_devargs_list;
239
240         printf("Dump all devices\n");
241         TEST_ASSERT(TAILQ_EMPTY(&pci_driver_list),
242                         "pci_driver_list not empty");
243
244         rte_eal_pci_register(&my_driver);
245         rte_eal_pci_register(&my_driver2);
246
247         pci_dev_count = 0;
248         printf("Scan bus\n");
249         rte_eal_pci_probe();
250
251         if (pci_dev_count == 0) {
252                 printf("no device detected\n");
253                 return -1;
254         }
255
256         /* save the real devargs_list */
257         save_devargs_list = devargs_list;
258         TAILQ_INIT(&devargs_list);
259
260         blacklist_all_devices();
261
262         pci_dev_count = 0;
263         printf("Scan bus with all devices blacklisted\n");
264         rte_eal_pci_probe();
265
266         free_devargs_list();
267         devargs_list = save_devargs_list;
268
269         if (pci_dev_count != 0) {
270                 printf("not all devices are blacklisted\n");
271                 return -1;
272         }
273
274         test_pci_run = 1;
275
276         rte_eal_pci_unregister(&my_driver);
277         rte_eal_pci_unregister(&my_driver2);
278
279         return 0;
280 }
281
282 static int test_pci_sysfs(void)
283 {
284         const char *orig;
285         const char *newpath;
286         int ret;
287
288         orig = pci_get_sysfs_path();
289         ret = setenv("SYSFS_PCI_DEVICES", "My Documents", 1);
290         TEST_ASSERT_SUCCESS(ret, "Failed setenv to My Documents");
291
292         newpath = pci_get_sysfs_path();
293         TEST_ASSERT(!strcmp(newpath, "My Documents"),
294                         "pci_get_sysfs_path() should return 'My Documents' "
295                         "but gives %s", newpath);
296
297         ret = setenv("SYSFS_PCI_DEVICES", orig, 1);
298         TEST_ASSERT_SUCCESS(ret, "Failed setenv back to '%s'", orig);
299
300         newpath = pci_get_sysfs_path();
301         TEST_ASSERT(!strcmp(orig, newpath),
302                         "pci_get_sysfs_path returned unexpected path: "
303                         "%s (expected: %s)", newpath, orig);
304         return 0;
305 }
306
307 int
308 test_pci(void)
309 {
310         if (test_pci_sysfs())
311                 return -1;
312
313         if (test_pci_setup())
314                 return -1;
315
316         if (test_pci_blacklist())
317                 return -1;
318
319         if (test_pci_cleanup())
320                 return -1;
321
322         return 0;
323 }
324
325 static struct test_command pci_cmd = {
326         .command = "pci_autotest",
327         .callback = test_pci,
328 };
329 REGISTER_TEST_COMMAND(pci_cmd);