pci: fix probing blacklisted device with RTE_PCI_DRV_MULTIPLE flag
[dpdk.git] / lib / librte_eal / common / eal_common_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 /*   BSD LICENSE
35  *
36  *   Copyright(c) 2013 6WIND.
37  *
38  *   Redistribution and use in source and binary forms, with or without
39  *   modification, are permitted provided that the following conditions
40  *   are met:
41  *
42  *     * Redistributions of source code must retain the above copyright
43  *       notice, this list of conditions and the following disclaimer.
44  *     * Redistributions in binary form must reproduce the above copyright
45  *       notice, this list of conditions and the following disclaimer in
46  *       the documentation and/or other materials provided with the
47  *       distribution.
48  *     * Neither the name of 6WIND S.A. nor the names of its
49  *       contributors may be used to endorse or promote products derived
50  *       from this software without specific prior written permission.
51  *
52  *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
53  *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
54  *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
55  *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
56  *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
57  *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
58  *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
59  *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
60  *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
61  *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
62  *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
63  */
64
65 #include <inttypes.h>
66 #include <stdint.h>
67 #include <stdlib.h>
68 #include <stdio.h>
69 #include <sys/queue.h>
70
71 #include <rte_interrupts.h>
72 #include <rte_log.h>
73 #include <rte_pci.h>
74 #include <rte_per_lcore.h>
75 #include <rte_memory.h>
76 #include <rte_memzone.h>
77 #include <rte_tailq.h>
78 #include <rte_eal.h>
79
80 #include "eal_private.h"
81
82 struct pci_driver_list driver_list;
83 struct pci_device_list device_list;
84
85 static struct rte_pci_addr *dev_blacklist = NULL;
86 static unsigned dev_blacklist_size = 0;
87
88 static int is_blacklisted(struct rte_pci_device *dev)
89 {
90         struct rte_pci_addr *loc = &dev->addr;
91         unsigned i;
92
93         for (i = 0; i < dev_blacklist_size; i++) {
94                 if ((loc->domain == dev_blacklist[i].domain) &&
95                                 (loc->bus == dev_blacklist[i].bus) &&
96                                 (loc->devid == dev_blacklist[i].devid) &&
97                                 (loc->function == dev_blacklist[i].function)) {
98                         return 1;
99                 }
100         }
101
102         return 0;           /* not in blacklist */
103 }
104
105 /*
106  * If vendor/device ID match, call the devinit() function of all
107  * registered driver for the given device. Return -1 if no driver is
108  * found for this device.
109  * For drivers with the RTE_PCI_DRV_MULTIPLE flag enabled, register
110  * the same device multiple times until failure to do so.
111  * It is required for non-Intel NIC drivers provided by third-parties such
112  * as 6WIND.
113  */
114 static int
115 pci_probe_all_drivers(struct rte_pci_device *dev)
116 {
117         struct rte_pci_driver *dr = NULL;
118
119         dev->blacklisted = !!is_blacklisted(dev);
120         TAILQ_FOREACH(dr, &driver_list, next) {
121                 if (rte_eal_pci_probe_one_driver(dr, dev))
122                         continue;
123                 /* initialize subsequent driver instances for this device */
124                 if ((dr->drv_flags & RTE_PCI_DRV_MULTIPLE) &&
125                                 (!dev->blacklisted))
126                         while (rte_eal_pci_probe_one_driver(dr, dev) == 0)
127                                 ;
128                 return 0;
129         }
130         return -1;
131 }
132
133 /*
134  * Scan the content of the PCI bus, and call the devinit() function for
135  * all registered drivers that have a matching entry in its id_table
136  * for discovered devices.
137  */
138 int
139 rte_eal_pci_probe(void)
140 {
141         struct rte_pci_device *dev = NULL;
142
143         TAILQ_FOREACH(dev, &device_list, next)
144                 pci_probe_all_drivers(dev);
145         return 0;
146 }
147
148 /* dump one device */
149 static int
150 pci_dump_one_device(struct rte_pci_device *dev)
151 {
152         int i;
153
154         printf(PCI_PRI_FMT, dev->addr.domain, dev->addr.bus,
155                dev->addr.devid, dev->addr.function);
156         printf(" - vendor:%x device:%x\n", dev->id.vendor_id,
157                dev->id.device_id);
158
159         for (i = 0; i != sizeof(dev->mem_resource) /
160                 sizeof(dev->mem_resource[0]); i++) {
161                 printf("   %16.16"PRIx64" %16.16"PRIx64"\n",
162                         dev->mem_resource[i].phys_addr, 
163                         dev->mem_resource[i].len);
164         }
165         return 0;
166 }
167
168 /* dump devices on the bus */
169 void
170 rte_eal_pci_dump(void)
171 {
172         struct rte_pci_device *dev = NULL;
173
174         TAILQ_FOREACH(dev, &device_list, next) {
175                 pci_dump_one_device(dev);
176         }
177 }
178
179 /* register a driver */
180 void
181 rte_eal_pci_register(struct rte_pci_driver *driver)
182 {
183         TAILQ_INSERT_TAIL(&driver_list, driver, next);
184 }
185
186 /* unregister a driver */
187 void
188 rte_eal_pci_unregister(struct rte_pci_driver *driver)
189 {
190         TAILQ_REMOVE(&driver_list, driver, next);
191 }
192
193 void
194 rte_eal_pci_set_blacklist(struct rte_pci_addr *blacklist, unsigned size)
195 {
196         dev_blacklist = blacklist;
197         dev_blacklist_size = size;
198 }