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