8bc5f5a6258741797516bd3e44b9f89b66d3b385
[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 (!memcmp(&dev->addr, &devargs->pci.addr, sizeof(dev->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 no driver is
105  * found for this device.
106  * For drivers with the RTE_PCI_DRV_MULTIPLE flag enabled, register
107  * the same device multiple times until failure to do so.
108  * It is required for non-Intel NIC drivers provided by third-parties such
109  * as 6WIND.
110  */
111 static int
112 pci_probe_all_drivers(struct rte_pci_device *dev)
113 {
114         struct rte_pci_driver *dr = NULL;
115         int rc;
116
117         TAILQ_FOREACH(dr, &pci_driver_list, next) {
118                 rc = rte_eal_pci_probe_one_driver(dr, dev);
119                 if (rc < 0)
120                         /* negative value is an error */
121                         break;
122                 if (rc > 0)
123                         /* positive value means driver not found */
124                         continue;
125                 /* initialize subsequent driver instances for this device */
126                 if ((dr->drv_flags & RTE_PCI_DRV_MULTIPLE) &&
127                         (dev->devargs == NULL ||
128                                 dev->devargs->type != RTE_DEVTYPE_BLACKLISTED_PCI))
129                         while (rte_eal_pci_probe_one_driver(dr, dev) == 0)
130                                 ;
131                 return 0;
132         }
133         return -1;
134 }
135
136 /*
137  * Scan the content of the PCI bus, and call the devinit() function for
138  * all registered drivers that have a matching entry in its id_table
139  * for discovered devices.
140  */
141 int
142 rte_eal_pci_probe(void)
143 {
144         struct rte_pci_device *dev = NULL;
145         struct rte_devargs *devargs;
146         int probe_all = 0;
147
148         if (rte_eal_devargs_type_count(RTE_DEVTYPE_WHITELISTED_PCI) == 0)
149                 probe_all = 1;
150
151         TAILQ_FOREACH(dev, &pci_device_list, next) {
152
153                 /* set devargs in PCI structure */
154                 devargs = pci_devargs_lookup(dev);
155                 if (devargs != NULL)
156                         dev->devargs = devargs;
157
158                 /* probe all or only whitelisted devices */
159                 if (probe_all)
160                         pci_probe_all_drivers(dev);
161                 else if (devargs != NULL &&
162                         devargs->type == RTE_DEVTYPE_WHITELISTED_PCI &&
163                         pci_probe_all_drivers(dev) < 0)
164                         rte_exit(EXIT_FAILURE, "Requested device " PCI_PRI_FMT
165                                  " cannot be used\n", dev->addr.domain, dev->addr.bus,
166                                  dev->addr.devid, dev->addr.function);
167         }
168
169         return 0;
170 }
171
172 /* dump one device */
173 static int
174 pci_dump_one_device(struct rte_pci_device *dev)
175 {
176         int i;
177
178         printf(PCI_PRI_FMT, dev->addr.domain, dev->addr.bus,
179                dev->addr.devid, dev->addr.function);
180         printf(" - vendor:%x device:%x\n", dev->id.vendor_id,
181                dev->id.device_id);
182
183         for (i = 0; i != sizeof(dev->mem_resource) /
184                 sizeof(dev->mem_resource[0]); i++) {
185                 printf("   %16.16"PRIx64" %16.16"PRIx64"\n",
186                         dev->mem_resource[i].phys_addr, 
187                         dev->mem_resource[i].len);
188         }
189         return 0;
190 }
191
192 /* dump devices on the bus */
193 void
194 rte_eal_pci_dump(void)
195 {
196         struct rte_pci_device *dev = NULL;
197
198         TAILQ_FOREACH(dev, &pci_device_list, next) {
199                 pci_dump_one_device(dev);
200         }
201 }
202
203 /* register a driver */
204 void
205 rte_eal_pci_register(struct rte_pci_driver *driver)
206 {
207         TAILQ_INSERT_TAIL(&pci_driver_list, driver, next);
208 }
209
210 /* unregister a driver */
211 void
212 rte_eal_pci_unregister(struct rte_pci_driver *driver)
213 {
214         TAILQ_REMOVE(&pci_driver_list, driver, next);
215 }