tailq: remove unneeded inclusions
[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 2013-2014 6WIND S.A.
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 <string.h>
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_eal.h>
78 #include <rte_string_fns.h>
79 #include <rte_common.h>
80 #include <rte_devargs.h>
81
82 #include "eal_private.h"
83
84 struct pci_driver_list pci_driver_list;
85 struct pci_device_list pci_device_list;
86
87 static struct rte_devargs *pci_devargs_lookup(struct rte_pci_device *dev)
88 {
89         struct rte_devargs *devargs;
90
91         TAILQ_FOREACH(devargs, &devargs_list, next) {
92                 if (devargs->type != RTE_DEVTYPE_BLACKLISTED_PCI &&
93                         devargs->type != RTE_DEVTYPE_WHITELISTED_PCI)
94                         continue;
95                 if (!rte_eal_compare_pci_addr(&dev->addr, &devargs->pci.addr))
96                         return devargs;
97         }
98         return NULL;
99 }
100
101 /*
102  * If vendor/device ID match, call the devinit() function of all
103  * registered driver for the given device. Return -1 if initialization
104  * failed, return 1 if no driver is found for this device.
105  */
106 static int
107 pci_probe_all_drivers(struct rte_pci_device *dev)
108 {
109         struct rte_pci_driver *dr = NULL;
110         int rc = 0;
111
112         if (dev == NULL)
113                 return -1;
114
115         TAILQ_FOREACH(dr, &pci_driver_list, next) {
116                 rc = rte_eal_pci_probe_one_driver(dr, dev);
117                 if (rc < 0)
118                         /* negative value is an error */
119                         return -1;
120                 if (rc > 0)
121                         /* positive value means driver not found */
122                         continue;
123                 return 0;
124         }
125         return 1;
126 }
127
128 #ifdef RTE_LIBRTE_EAL_HOTPLUG
129 /*
130  * If vendor/device ID match, call the devuninit() function of all
131  * registered driver for the given device. Return -1 if initialization
132  * failed, return 1 if no driver is found for this device.
133  */
134 static int
135 pci_close_all_drivers(struct rte_pci_device *dev)
136 {
137         struct rte_pci_driver *dr = NULL;
138         int rc = 0;
139
140         if (dev == NULL)
141                 return -1;
142
143         TAILQ_FOREACH(dr, &pci_driver_list, next) {
144                 rc = rte_eal_pci_close_one_driver(dr, dev);
145                 if (rc < 0)
146                         /* negative value is an error */
147                         return -1;
148                 if (rc > 0)
149                         /* positive value means driver not found */
150                         continue;
151                 return 0;
152         }
153         return 1;
154 }
155
156 /*
157  * Find the pci device specified by pci address, then invoke probe function of
158  * the driver of the devive.
159  */
160 int
161 rte_eal_pci_probe_one(struct rte_pci_addr *addr)
162 {
163         struct rte_pci_device *dev = NULL;
164         int ret = 0;
165
166         if (addr == NULL)
167                 return -1;
168
169         TAILQ_FOREACH(dev, &pci_device_list, next) {
170                 if (rte_eal_compare_pci_addr(&dev->addr, addr))
171                         continue;
172
173                 ret = pci_probe_all_drivers(dev);
174                 if (ret < 0)
175                         goto err_return;
176                 return 0;
177         }
178         return -1;
179
180 err_return:
181         RTE_LOG(WARNING, EAL, "Requested device " PCI_PRI_FMT
182                         " cannot be used\n", dev->addr.domain, dev->addr.bus,
183                         dev->addr.devid, dev->addr.function);
184         return -1;
185 }
186
187 /*
188  * Find the pci device specified by pci address, then invoke close function of
189  * the driver of the devive.
190  */
191 int
192 rte_eal_pci_close_one(struct rte_pci_addr *addr)
193 {
194         struct rte_pci_device *dev = NULL;
195         int ret = 0;
196
197         if (addr == NULL)
198                 return -1;
199
200         TAILQ_FOREACH(dev, &pci_device_list, next) {
201                 if (rte_eal_compare_pci_addr(&dev->addr, addr))
202                         continue;
203
204                 ret = pci_close_all_drivers(dev);
205                 if (ret < 0)
206                         goto err_return;
207
208                 TAILQ_REMOVE(&pci_device_list, dev, next);
209                 return 0;
210         }
211         return -1;
212
213 err_return:
214         RTE_LOG(WARNING, EAL, "Requested device " PCI_PRI_FMT
215                         " cannot be used\n", dev->addr.domain, dev->addr.bus,
216                         dev->addr.devid, dev->addr.function);
217         return -1;
218 }
219 #endif /* RTE_LIBRTE_EAL_HOTPLUG */
220
221 /*
222  * Scan the content of the PCI bus, and call the devinit() function for
223  * all registered drivers that have a matching entry in its id_table
224  * for discovered devices.
225  */
226 int
227 rte_eal_pci_probe(void)
228 {
229         struct rte_pci_device *dev = NULL;
230         struct rte_devargs *devargs;
231         int probe_all = 0;
232         int ret = 0;
233
234         if (rte_eal_devargs_type_count(RTE_DEVTYPE_WHITELISTED_PCI) == 0)
235                 probe_all = 1;
236
237         TAILQ_FOREACH(dev, &pci_device_list, next) {
238
239                 /* set devargs in PCI structure */
240                 devargs = pci_devargs_lookup(dev);
241                 if (devargs != NULL)
242                         dev->devargs = devargs;
243
244                 /* probe all or only whitelisted devices */
245                 if (probe_all)
246                         ret = pci_probe_all_drivers(dev);
247                 else if (devargs != NULL &&
248                         devargs->type == RTE_DEVTYPE_WHITELISTED_PCI)
249                         ret = pci_probe_all_drivers(dev);
250                 if (ret < 0)
251                         rte_exit(EXIT_FAILURE, "Requested device " PCI_PRI_FMT
252                                  " cannot be used\n", dev->addr.domain, dev->addr.bus,
253                                  dev->addr.devid, dev->addr.function);
254         }
255
256         return 0;
257 }
258
259 /* dump one device */
260 static int
261 pci_dump_one_device(FILE *f, struct rte_pci_device *dev)
262 {
263         int i;
264
265         fprintf(f, PCI_PRI_FMT, dev->addr.domain, dev->addr.bus,
266                dev->addr.devid, dev->addr.function);
267         fprintf(f, " - vendor:%x device:%x\n", dev->id.vendor_id,
268                dev->id.device_id);
269
270         for (i = 0; i != sizeof(dev->mem_resource) /
271                 sizeof(dev->mem_resource[0]); i++) {
272                 fprintf(f, "   %16.16"PRIx64" %16.16"PRIx64"\n",
273                         dev->mem_resource[i].phys_addr,
274                         dev->mem_resource[i].len);
275         }
276         return 0;
277 }
278
279 /* dump devices on the bus */
280 void
281 rte_eal_pci_dump(FILE *f)
282 {
283         struct rte_pci_device *dev = NULL;
284
285         TAILQ_FOREACH(dev, &pci_device_list, next) {
286                 pci_dump_one_device(f, dev);
287         }
288 }
289
290 /* register a driver */
291 void
292 rte_eal_pci_register(struct rte_pci_driver *driver)
293 {
294         TAILQ_INSERT_TAIL(&pci_driver_list, driver, next);
295 }
296
297 /* unregister a driver */
298 void
299 rte_eal_pci_unregister(struct rte_pci_driver *driver)
300 {
301         TAILQ_REMOVE(&pci_driver_list, driver, next);
302 }