update Intel copyright years to 2014
[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 <cmdline_parse.h>
40
41 #include <rte_interrupts.h>
42 #include <rte_pci.h>
43
44 #include "test.h"
45
46
47 #define TEST_BLACKLIST_NUM      0x100
48
49 /*
50  * PCI test
51  * ========
52  *
53  * - Register a driver with a ``devinit()`` function.
54  *
55  * - Dump all PCI devices.
56  *
57  * - Check that the ``devinit()`` function is called at least once.
58  */
59
60 int test_pci_run = 0; /* value checked by the multiprocess test */
61 static unsigned pci_dev_count;
62 static unsigned driver_registered = 0;
63 static struct rte_pci_addr blacklist[TEST_BLACKLIST_NUM];
64
65 static int my_driver_init(struct rte_pci_driver *dr,
66                           struct rte_pci_device *dev);
67
68 /*
69  * To test cases where RTE_PCI_DRV_NEED_IGB_UIO is set, and isn't set, two
70  * drivers are created (one with IGB devices, the other with IXGBE devices).
71  */
72
73 /* IXGBE NICS */
74 struct rte_pci_id my_driver_id[] = {
75
76 #define RTE_PCI_DEV_ID_DECL_IXGBE(vend, dev) {RTE_PCI_DEVICE(vend, dev)},
77 #include <rte_pci_dev_ids.h>
78
79 { .vendor_id = 0, /* sentinel */ },
80 };
81
82 struct rte_pci_id my_driver_id2[] = {
83
84 /* IGB & EM NICS */
85 #define RTE_PCI_DEV_ID_DECL_EM(vend, dev) {RTE_PCI_DEVICE(vend, dev)},
86 #define RTE_PCI_DEV_ID_DECL_IGB(vend, dev) {RTE_PCI_DEVICE(vend, dev)},
87 #define RTE_PCI_DEV_USE_82575EB_COPPER
88 #include <rte_pci_dev_ids.h>
89
90 { .vendor_id = 0, /* sentinel */ },
91 };
92
93 struct rte_pci_driver my_driver = {
94         .name = "test_driver",
95         .devinit = my_driver_init,
96         .id_table = my_driver_id,
97 #ifdef RTE_EAL_UNBIND_PORTS
98         .drv_flags = RTE_PCI_DRV_NEED_IGB_UIO,
99 #endif
100 };
101
102 struct rte_pci_driver my_driver2 = {
103         .name = "test_driver2",
104         .devinit = my_driver_init,
105         .id_table = my_driver_id2,
106         .drv_flags = 0,
107 };
108
109 static int
110 my_driver_init(__attribute__((unused)) struct rte_pci_driver *dr,
111                struct rte_pci_device *dev)
112 {
113         printf("My driver init called in %s\n", dr->name);
114         printf("%x:%x:%x.%d", dev->addr.domain, dev->addr.bus,
115                dev->addr.devid, dev->addr.function);
116         printf(" - vendor:%x device:%x\n", dev->id.vendor_id, dev->id.device_id);
117
118         pci_dev_count ++;
119         return 0;
120 }
121
122 static void
123 blacklist_clear(void)
124 {
125         rte_eal_pci_set_blacklist(NULL, 0);
126 }
127
128
129
130 static void
131 blacklist_all_devices(void)
132 {
133         struct rte_pci_device *dev = NULL;
134         unsigned idx = 0;
135
136         memset(blacklist, 0, sizeof (blacklist));
137
138         TAILQ_FOREACH(dev, &device_list, next) {
139                 if (idx >= sizeof (blacklist) / sizeof (blacklist[0])) {
140                         printf("Error: too many devices to blacklist");
141                         break;
142                 }
143                 blacklist[idx] = dev->addr;
144                 ++idx;
145         }
146
147         rte_eal_pci_set_blacklist(blacklist, idx);
148         printf("%u devices blacklisted\n", idx);
149 }
150
151 int
152 test_pci(void)
153 {
154
155         printf("Dump all devices\n");
156         rte_eal_pci_dump();
157         if (driver_registered == 0) {
158                 rte_eal_pci_register(&my_driver);
159                 rte_eal_pci_register(&my_driver2);
160                 driver_registered = 1;
161         }
162
163         pci_dev_count = 0;
164         printf("Scan bus\n");
165         rte_eal_pci_probe();
166
167         if (pci_dev_count == 0) {
168                 printf("no device detected\n");
169                 return -1;
170         }
171
172         blacklist_all_devices();
173
174         pci_dev_count = 0;
175         printf("Scan bus with all devices blacklisted\n");
176         rte_eal_pci_probe();
177
178         blacklist_clear();
179
180         if (pci_dev_count != 0) {
181                 printf("not all devices are blacklisted\n");
182                 return -1;
183         }
184
185         test_pci_run = 1;
186         if (driver_registered == 1) {
187                 rte_eal_pci_unregister(&my_driver);
188                 rte_eal_pci_unregister(&my_driver2);
189                 driver_registered = 0;
190         }
191
192         return 0;
193 }