app/test: fix devargs tests
[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
47 /* Generic maximum number of drivers to have room to allocate all drivers */
48 #define NUM_MAX_DRIVERS 256
49
50 /*
51  * PCI test
52  * ========
53  *
54  * - Register a driver with a ``devinit()`` function.
55  *
56  * - Dump all PCI devices.
57  *
58  * - Check that the ``devinit()`` function is called at least once.
59  */
60
61 int test_pci_run = 0; /* value checked by the multiprocess test */
62 static unsigned pci_dev_count;
63
64 static int my_driver_init(struct rte_pci_driver *dr,
65                           struct rte_pci_device *dev);
66
67 /* IXGBE NICS */
68 struct rte_pci_id my_driver_id[] = {
69
70 #define RTE_PCI_DEV_ID_DECL_IXGBE(vend, dev) {RTE_PCI_DEVICE(vend, dev)},
71 #include <rte_pci_dev_ids.h>
72
73 { .vendor_id = 0, /* sentinel */ },
74 };
75
76 struct rte_pci_id my_driver_id2[] = {
77
78 /* IGB & EM NICS */
79 #define RTE_PCI_DEV_ID_DECL_EM(vend, dev) {RTE_PCI_DEVICE(vend, dev)},
80 #define RTE_PCI_DEV_ID_DECL_IGB(vend, dev) {RTE_PCI_DEVICE(vend, dev)},
81 #include <rte_pci_dev_ids.h>
82
83 { .vendor_id = 0, /* sentinel */ },
84 };
85
86 struct rte_pci_driver my_driver = {
87         .name = "test_driver",
88         .devinit = my_driver_init,
89         .id_table = my_driver_id,
90         .drv_flags = 0,
91 };
92
93 struct rte_pci_driver my_driver2 = {
94         .name = "test_driver2",
95         .devinit = my_driver_init,
96         .id_table = my_driver_id2,
97         .drv_flags = 0,
98 };
99
100 static int
101 my_driver_init(__attribute__((unused)) struct rte_pci_driver *dr,
102                struct rte_pci_device *dev)
103 {
104         printf("My driver init called in %s\n", dr->name);
105         printf("%x:%x:%x.%d", dev->addr.domain, dev->addr.bus,
106                dev->addr.devid, dev->addr.function);
107         printf(" - vendor:%x device:%x\n", dev->id.vendor_id, dev->id.device_id);
108
109         pci_dev_count ++;
110         return 0;
111 }
112
113 static void
114 blacklist_all_devices(void)
115 {
116         struct rte_pci_device *dev = NULL;
117         unsigned i = 0;
118         char pci_addr_str[16];
119
120         TAILQ_FOREACH(dev, &pci_device_list, next) {
121                 snprintf(pci_addr_str, sizeof(pci_addr_str), PCI_PRI_FMT,
122                         dev->addr.domain, dev->addr.bus, dev->addr.devid,
123                         dev->addr.function);
124                 if (rte_eal_devargs_add(RTE_DEVTYPE_BLACKLISTED_PCI,
125                                 pci_addr_str) < 0) {
126                         printf("Error: cannot blacklist <%s>", pci_addr_str);
127                         break;
128                 }
129                 i++;
130         }
131         printf("%u devices blacklisted\n", i);
132 }
133
134 /* clear devargs list that was modified by the test */
135 static void free_devargs_list(void)
136 {
137         struct rte_devargs *devargs;
138
139         while (!TAILQ_EMPTY(&devargs_list)) {
140                 devargs = TAILQ_FIRST(&devargs_list);
141                 TAILQ_REMOVE(&devargs_list, devargs, next);
142                 if (devargs->args)
143                         free(devargs->args);
144                 free(devargs);
145         }
146 }
147
148 int
149 test_pci(void)
150 {
151         struct rte_devargs_list save_devargs_list;
152         struct rte_pci_driver *dr = NULL;
153         struct rte_pci_driver *save_pci_driver_list[NUM_MAX_DRIVERS];
154         unsigned i, num_drivers = 0;
155
156         printf("Dump all devices\n");
157         rte_eal_pci_dump(stdout);
158
159         /* Unregister all previous drivers */
160         TAILQ_FOREACH(dr, &pci_driver_list, next) {
161                 rte_eal_pci_unregister(dr);
162                 save_pci_driver_list[num_drivers++] = dr;
163         }
164
165         rte_eal_pci_register(&my_driver);
166         rte_eal_pci_register(&my_driver2);
167
168         pci_dev_count = 0;
169         printf("Scan bus\n");
170         rte_eal_pci_probe();
171
172         if (pci_dev_count == 0) {
173                 printf("no device detected\n");
174                 return -1;
175         }
176
177         /* save the real devargs_list */
178         save_devargs_list = devargs_list;
179         TAILQ_INIT(&devargs_list);
180
181         blacklist_all_devices();
182
183         pci_dev_count = 0;
184         printf("Scan bus with all devices blacklisted\n");
185         rte_eal_pci_probe();
186
187         free_devargs_list();
188         devargs_list = save_devargs_list;
189
190         if (pci_dev_count != 0) {
191                 printf("not all devices are blacklisted\n");
192                 return -1;
193         }
194
195         test_pci_run = 1;
196
197         rte_eal_pci_unregister(&my_driver);
198         rte_eal_pci_unregister(&my_driver2);
199
200         /* Restore original driver list */
201         for (i = 0; i < num_drivers; i++)
202                 rte_eal_pci_register(save_pci_driver_list[i]);
203
204         return 0;
205 }
206
207 static struct test_command pci_cmd = {
208         .command = "pci_autotest",
209         .callback = test_pci,
210 };
211 REGISTER_TEST_COMMAND(pci_cmd);