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