pci: make it possible to keep devices bound to uio
[dpdk.git] / lib / librte_eal / common / eal_common_pci.c
1 /*-
2  *   BSD LICENSE
3  * 
4  *   Copyright(c) 2010-2012 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
35 #include <inttypes.h>
36 #include <stdint.h>
37 #include <stdlib.h>
38 #include <stdio.h>
39 #include <sys/queue.h>
40
41 #include <rte_interrupts.h>
42 #include <rte_log.h>
43 #include <rte_pci.h>
44 #include <rte_per_lcore.h>
45 #include <rte_memory.h>
46 #include <rte_memzone.h>
47 #include <rte_tailq.h>
48 #include <rte_eal.h>
49
50 #include "eal_private.h"
51
52 struct pci_driver_list driver_list;
53 struct pci_device_list device_list;
54
55 static struct rte_pci_addr *dev_blacklist = NULL;
56 static unsigned dev_blacklist_size = 0;
57
58 static int is_blacklisted(struct rte_pci_device *dev)
59 {
60         struct rte_pci_addr *loc = &dev->addr;
61         unsigned i;
62
63         for (i = 0; i < dev_blacklist_size; i++) {
64                 if ((loc->domain == dev_blacklist[i].domain) &&
65                                 (loc->bus == dev_blacklist[i].bus) &&
66                                 (loc->devid == dev_blacklist[i].devid) &&
67                                 (loc->function == dev_blacklist[i].function)) {
68                         return 1;
69                 }
70         }
71
72         return 0;           /* not in blacklist */
73 }
74
75 /*
76  * If vendor/device ID match, call the devinit() function of all
77  * registered driver for the given device. Return -1 if no driver is
78  * found for this device.
79  */
80 static int
81 pci_probe_all_drivers(struct rte_pci_device *dev)
82 {
83         struct rte_pci_driver *dr = NULL;
84
85         dev->blacklisted = !!is_blacklisted(dev);
86         TAILQ_FOREACH(dr, &driver_list, next) {
87                 if (rte_eal_pci_probe_one_driver(dr, dev) == 0)
88                         return 0;
89         }
90         return -1;
91 }
92
93 /*
94  * Scan the content of the PCI bus, and call the devinit() function for
95  * all registered drivers that have a matching entry in its id_table
96  * for discovered devices.
97  */
98 int
99 rte_eal_pci_probe(void)
100 {
101         struct rte_pci_device *dev = NULL;
102
103         TAILQ_FOREACH(dev, &device_list, next)
104                 pci_probe_all_drivers(dev);
105         #ifdef RTE_EAL_UNBIND_PORTS
106                 if (atexit(rte_eal_pci_exit) != 0)
107                         RTE_LOG(ERR, EAL, "atexit failure\n");
108         #endif
109         return 0;
110 }
111
112 /* dump one device */
113 static int
114 pci_dump_one_device(struct rte_pci_device *dev)
115 {
116         printf(PCI_PRI_FMT, dev->addr.domain, dev->addr.bus,
117                dev->addr.devid, dev->addr.function);
118         printf(" - vendor:%x device:%x\n", dev->id.vendor_id,
119                dev->id.device_id);
120         printf("   %16.16"PRIx64" %16.16"PRIx64"\n",
121                dev->mem_resource.phys_addr, dev->mem_resource.len);
122         return 0;
123 }
124
125 /* dump devices on the bus */
126 void
127 rte_eal_pci_dump(void)
128 {
129         struct rte_pci_device *dev = NULL;
130
131         TAILQ_FOREACH(dev, &device_list, next) {
132                 pci_dump_one_device(dev);
133         }
134 }
135
136 /* register a driver */
137 void
138 rte_eal_pci_register(struct rte_pci_driver *driver)
139 {
140         TAILQ_INSERT_TAIL(&driver_list, driver, next);
141 }
142
143 void
144 rte_eal_pci_set_blacklist(struct rte_pci_addr *blacklist, unsigned size)
145 {
146         dev_blacklist = blacklist;
147         dev_blacklist_size = size;
148 }