app/test: fix build without librte_cmdline
[dpdk.git] / app / test / test_pci.c
1 /*-
2  *   BSD LICENSE
3  * 
4  *   Copyright(c) 2010-2014 Intel Corporation. All rights reserved.
5  *   All rights reserved.
6  * 
7  *   Redistribution and use in source and binary forms, with or without
8  *   modification, are permitted provided that the following conditions
9  *   are met:
10  * 
11  *     * Redistributions of source code must retain the above copyright
12  *       notice, this list of conditions and the following disclaimer.
13  *     * Redistributions in binary form must reproduce the above copyright
14  *       notice, this list of conditions and the following disclaimer in
15  *       the documentation and/or other materials provided with the
16  *       distribution.
17  *     * Neither the name of Intel Corporation nor the names of its
18  *       contributors may be used to endorse or promote products derived
19  *       from this software without specific prior written permission.
20  * 
21  *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24  *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25  *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26  *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27  *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28  *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29  *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30  *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31  *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32  */
33
34 #include <stdio.h>
35 #include <string.h>
36 #include <stdint.h>
37 #include <sys/queue.h>
38
39 #include <rte_interrupts.h>
40 #include <rte_pci.h>
41
42 #include "test.h"
43
44
45 #define TEST_BLACKLIST_NUM      0x100
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 static struct rte_pci_addr blacklist[TEST_BLACKLIST_NUM];
62
63 static int my_driver_init(struct rte_pci_driver *dr,
64                           struct rte_pci_device *dev);
65
66 /*
67  * To test cases where RTE_PCI_DRV_NEED_IGB_UIO is set, and isn't set, two
68  * drivers are created (one with IGB devices, the other with IXGBE devices).
69  */
70
71 /* IXGBE NICS */
72 struct rte_pci_id my_driver_id[] = {
73
74 #define RTE_PCI_DEV_ID_DECL_IXGBE(vend, dev) {RTE_PCI_DEVICE(vend, dev)},
75 #include <rte_pci_dev_ids.h>
76
77 { .vendor_id = 0, /* sentinel */ },
78 };
79
80 struct rte_pci_id my_driver_id2[] = {
81
82 /* IGB & EM NICS */
83 #define RTE_PCI_DEV_ID_DECL_EM(vend, dev) {RTE_PCI_DEVICE(vend, dev)},
84 #define RTE_PCI_DEV_ID_DECL_IGB(vend, dev) {RTE_PCI_DEVICE(vend, dev)},
85 #define RTE_PCI_DEV_USE_82575EB_COPPER
86 #include <rte_pci_dev_ids.h>
87
88 { .vendor_id = 0, /* sentinel */ },
89 };
90
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_IGB_UIO,
96 };
97
98 struct rte_pci_driver my_driver2 = {
99         .name = "test_driver2",
100         .devinit = my_driver_init,
101         .id_table = my_driver_id2,
102         .drv_flags = 0,
103 };
104
105 static int
106 my_driver_init(__attribute__((unused)) struct rte_pci_driver *dr,
107                struct rte_pci_device *dev)
108 {
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);
113
114         pci_dev_count ++;
115         return 0;
116 }
117
118 static void
119 blacklist_clear(void)
120 {
121         rte_eal_pci_set_blacklist(NULL, 0);
122 }
123
124
125
126 static void
127 blacklist_all_devices(void)
128 {
129         struct rte_pci_device *dev = NULL;
130         unsigned idx = 0;
131
132         memset(blacklist, 0, sizeof (blacklist));
133
134         TAILQ_FOREACH(dev, &device_list, next) {
135                 if (idx >= sizeof (blacklist) / sizeof (blacklist[0])) {
136                         printf("Error: too many devices to blacklist");
137                         break;
138                 }
139                 blacklist[idx] = dev->addr;
140                 ++idx;
141         }
142
143         rte_eal_pci_set_blacklist(blacklist, idx);
144         printf("%u devices blacklisted\n", idx);
145 }
146
147 int
148 test_pci(void)
149 {
150
151         printf("Dump all devices\n");
152         rte_eal_pci_dump();
153         if (driver_registered == 0) {
154                 rte_eal_pci_register(&my_driver);
155                 rte_eal_pci_register(&my_driver2);
156                 driver_registered = 1;
157         }
158
159         pci_dev_count = 0;
160         printf("Scan bus\n");
161         rte_eal_pci_probe();
162
163         if (pci_dev_count == 0) {
164                 printf("no device detected\n");
165                 return -1;
166         }
167
168         blacklist_all_devices();
169
170         pci_dev_count = 0;
171         printf("Scan bus with all devices blacklisted\n");
172         rte_eal_pci_probe();
173
174         blacklist_clear();
175
176         if (pci_dev_count != 0) {
177                 printf("not all devices are blacklisted\n");
178                 return -1;
179         }
180
181         test_pci_run = 1;
182         if (driver_registered == 1) {
183                 rte_eal_pci_unregister(&my_driver);
184                 rte_eal_pci_unregister(&my_driver2);
185                 driver_registered = 0;
186         }
187
188         return 0;
189 }