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