remove version in all files
[dpdk.git] / app / test / test_pci.c
1 /*-
2  *   BSD LICENSE
3  * 
4  *   Copyright(c) 2010-2012 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
35 #include <stdio.h>
36 #include <string.h>
37 #include <stdint.h>
38 #include <sys/queue.h>
39
40 #include <cmdline_parse.h>
41
42 #include <rte_interrupts.h>
43 #include <rte_pci.h>
44
45 #include "test.h"
46
47 #define RTE_PCI_DEV_ID_DECL(vend, dev) {RTE_PCI_DEVICE(vend, dev)},
48
49 #define TEST_BLACKLIST_NUM      0x100
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 static unsigned driver_registered = 0;
65 static struct rte_pci_addr blacklist[TEST_BLACKLIST_NUM];
66
67 static int my_driver_init(struct rte_pci_driver *dr,
68                           struct rte_pci_device *dev);
69
70 /*
71  * To test cases where RTE_PCI_DRV_NEED_IGB_UIO is set, and isn't set, two
72  * drivers are created (one with IGB devices, the other with IXGBE devices).
73  */
74
75 /* IXGBE NICS + e1000 used for Qemu */
76 #define RTE_LIBRTE_IXGBE_PMD 1
77 #undef RTE_LIBRTE_IGB_PMD
78 struct rte_pci_id my_driver_id[] = {
79
80 #include <rte_pci_dev_ids.h>
81
82 /* this device is the e1000 of qemu for testing */
83 {RTE_PCI_DEVICE(PCI_VENDOR_ID_INTEL, 0x100E)},
84
85 { .vendor_id = 0, /* sentinel */ },
86 };
87
88 struct rte_pci_id my_driver_id2[] = {
89
90 /* IGB NICS */
91 #undef RTE_LIBRTE_IXGBE_PMD
92 #define RTE_LIBRTE_IGB_PMD 1
93 #define RTE_PCI_DEV_USE_82575EB_COPPER
94 #include <rte_pci_dev_ids.h>
95
96 { .vendor_id = 0, /* sentinel */ },
97 };
98
99 struct rte_pci_driver my_driver = {
100         .name = "test_driver",
101         .devinit = my_driver_init,
102         .id_table = my_driver_id,
103         .drv_flags = RTE_PCI_DRV_NEED_IGB_UIO,
104 };
105
106 struct rte_pci_driver my_driver2 = {
107         .name = "test_driver2",
108         .devinit = my_driver_init,
109         .id_table = my_driver_id2,
110         .drv_flags = 0,
111 };
112
113 static int
114 my_driver_init(__attribute__((unused)) struct rte_pci_driver *dr,
115                struct rte_pci_device *dev)
116 {
117         printf("My driver init called in %s\n", dr->name);
118         printf("%x:%x:%x.%d", dev->addr.domain, dev->addr.bus,
119                dev->addr.devid, dev->addr.function);
120         printf(" - vendor:%x device:%x\n", dev->id.vendor_id, dev->id.device_id);
121
122         pci_dev_count ++;
123         return 0;
124 }
125
126 static void
127 blacklist_clear(void)
128 {
129         rte_eal_pci_set_blacklist(NULL, 0);
130 }
131
132
133
134 static void
135 blacklist_all_devices(void)
136 {
137         struct rte_pci_device *dev = NULL;
138         unsigned idx = 0;
139
140         memset(blacklist, 0, sizeof (blacklist));
141
142         TAILQ_FOREACH(dev, &device_list, next) {
143                 if (idx >= sizeof (blacklist) / sizeof (blacklist[0])) {
144                         printf("Error: too many devices to blacklist");
145                         break;
146                 }
147                 blacklist[idx] = dev->addr;
148                 ++idx;
149         }
150
151         rte_eal_pci_set_blacklist(blacklist, idx);
152         printf("%u devices blacklisted\n", idx);
153 }
154
155 int
156 test_pci(void)
157 {
158
159         printf("Dump all devices\n");
160         rte_eal_pci_dump();
161         if (driver_registered == 0) {
162                 rte_eal_pci_register(&my_driver);
163                 rte_eal_pci_register(&my_driver2);
164                 driver_registered = 1;
165         }
166
167         pci_dev_count = 0;
168         printf("Scan bus\n");
169         rte_eal_pci_probe();
170
171         if (pci_dev_count == 0) {
172                 printf("no device detected\n");
173                 return -1;
174         }
175
176         blacklist_all_devices();
177
178         pci_dev_count = 0;
179         printf("Scan bus with all devices blacklisted\n");
180         rte_eal_pci_probe();
181
182         blacklist_clear();
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         return 0;
191 }