pci: merge mapping functions for linux and bsd
[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 #include <sys/mman.h>
71
72 #include <rte_interrupts.h>
73 #include <rte_log.h>
74 #include <rte_pci.h>
75 #include <rte_per_lcore.h>
76 #include <rte_memory.h>
77 #include <rte_memzone.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 /* map a particular resource from a file */
103 void *
104 pci_map_resource(void *requested_addr, int fd, off_t offset, size_t size,
105                  int additional_flags)
106 {
107         void *mapaddr;
108
109         /* Map the PCI memory resource of device */
110         mapaddr = mmap(requested_addr, size, PROT_READ | PROT_WRITE,
111                         MAP_SHARED | additional_flags, fd, offset);
112         if (mapaddr == MAP_FAILED) {
113                 RTE_LOG(ERR, EAL, "%s(): cannot mmap(%d, %p, 0x%lx, 0x%lx): %s (%p)\n",
114                         __func__, fd, requested_addr,
115                         (unsigned long)size, (unsigned long)offset,
116                         strerror(errno), mapaddr);
117         } else
118                 RTE_LOG(DEBUG, EAL, "  PCI memory mapped at %p\n", mapaddr);
119
120         return mapaddr;
121 }
122
123 /* unmap a particular resource */
124 void
125 pci_unmap_resource(void *requested_addr, size_t size)
126 {
127         if (requested_addr == NULL)
128                 return;
129
130         /* Unmap the PCI memory resource of device */
131         if (munmap(requested_addr, size)) {
132                 RTE_LOG(ERR, EAL, "%s(): cannot munmap(%p, 0x%lx): %s\n",
133                         __func__, requested_addr, (unsigned long)size,
134                         strerror(errno));
135         } else
136                 RTE_LOG(DEBUG, EAL, "  PCI memory unmapped at %p\n",
137                                 requested_addr);
138 }
139
140 /* Map pci device */
141 int
142 pci_map_device(struct rte_pci_device *dev)
143 {
144         int ret = -1;
145
146         /* try mapping the NIC resources using VFIO if it exists */
147         switch (dev->kdrv) {
148         case RTE_KDRV_VFIO:
149 #ifdef VFIO_PRESENT
150                 if (pci_vfio_is_enabled())
151                         ret = pci_vfio_map_resource(dev);
152 #endif
153                 break;
154         case RTE_KDRV_IGB_UIO:
155         case RTE_KDRV_UIO_GENERIC:
156         case RTE_KDRV_NIC_UIO:
157                 /* map resources for devices that use uio */
158                 ret = pci_uio_map_resource(dev);
159                 break;
160         default:
161                 RTE_LOG(DEBUG, EAL, "  Not managed by a supported kernel driver,"
162                         " skipped\n");
163                 ret = 1;
164                 break;
165         }
166
167         return ret;
168 }
169
170 #ifdef RTE_LIBRTE_EAL_HOTPLUG
171 /* Unmap pci device */
172 void
173 pci_unmap_device(struct rte_pci_device *dev)
174 {
175         if (dev == NULL)
176                 return;
177
178         /* try unmapping the NIC resources using VFIO if it exists */
179         switch (dev->kdrv) {
180         case RTE_KDRV_VFIO:
181                 RTE_LOG(ERR, EAL, "Hotplug doesn't support vfio yet\n");
182                 break;
183         case RTE_KDRV_IGB_UIO:
184         case RTE_KDRV_UIO_GENERIC:
185         case RTE_KDRV_NIC_UIO:
186                 /* unmap resources for devices that use uio */
187                 pci_uio_unmap_resource(dev);
188                 break;
189         default:
190                 RTE_LOG(DEBUG, EAL, "  Not managed by a supported kernel driver,"
191                         " skipped\n");
192                 break;
193         }
194 }
195 #endif /* RTE_LIBRTE_EAL_HOTPLUG */
196
197 /*
198  * If vendor/device ID match, call the devinit() function of all
199  * registered driver for the given device. Return -1 if initialization
200  * failed, return 1 if no driver is found for this device.
201  */
202 static int
203 pci_probe_all_drivers(struct rte_pci_device *dev)
204 {
205         struct rte_pci_driver *dr = NULL;
206         int rc = 0;
207
208         if (dev == NULL)
209                 return -1;
210
211         TAILQ_FOREACH(dr, &pci_driver_list, next) {
212                 rc = rte_eal_pci_probe_one_driver(dr, dev);
213                 if (rc < 0)
214                         /* negative value is an error */
215                         return -1;
216                 if (rc > 0)
217                         /* positive value means driver not found */
218                         continue;
219                 return 0;
220         }
221         return 1;
222 }
223
224 #ifdef RTE_LIBRTE_EAL_HOTPLUG
225 /*
226  * If vendor/device ID match, call the devuninit() function of all
227  * registered driver for the given device. Return -1 if initialization
228  * failed, return 1 if no driver is found for this device.
229  */
230 static int
231 pci_close_all_drivers(struct rte_pci_device *dev)
232 {
233         struct rte_pci_driver *dr = NULL;
234         int rc = 0;
235
236         if (dev == NULL)
237                 return -1;
238
239         TAILQ_FOREACH(dr, &pci_driver_list, next) {
240                 rc = rte_eal_pci_close_one_driver(dr, dev);
241                 if (rc < 0)
242                         /* negative value is an error */
243                         return -1;
244                 if (rc > 0)
245                         /* positive value means driver not found */
246                         continue;
247                 return 0;
248         }
249         return 1;
250 }
251
252 /*
253  * Find the pci device specified by pci address, then invoke probe function of
254  * the driver of the devive.
255  */
256 int
257 rte_eal_pci_probe_one(const struct rte_pci_addr *addr)
258 {
259         struct rte_pci_device *dev = NULL;
260         int ret = 0;
261
262         if (addr == NULL)
263                 return -1;
264
265         TAILQ_FOREACH(dev, &pci_device_list, next) {
266                 if (rte_eal_compare_pci_addr(&dev->addr, addr))
267                         continue;
268
269                 ret = pci_probe_all_drivers(dev);
270                 if (ret < 0)
271                         goto err_return;
272                 return 0;
273         }
274         return -1;
275
276 err_return:
277         RTE_LOG(WARNING, EAL, "Requested device " PCI_PRI_FMT
278                         " cannot be used\n", dev->addr.domain, dev->addr.bus,
279                         dev->addr.devid, dev->addr.function);
280         return -1;
281 }
282
283 /*
284  * Find the pci device specified by pci address, then invoke close function of
285  * the driver of the devive.
286  */
287 int
288 rte_eal_pci_close_one(const struct rte_pci_addr *addr)
289 {
290         struct rte_pci_device *dev = NULL;
291         int ret = 0;
292
293         if (addr == NULL)
294                 return -1;
295
296         TAILQ_FOREACH(dev, &pci_device_list, next) {
297                 if (rte_eal_compare_pci_addr(&dev->addr, addr))
298                         continue;
299
300                 ret = pci_close_all_drivers(dev);
301                 if (ret < 0)
302                         goto err_return;
303
304                 TAILQ_REMOVE(&pci_device_list, dev, next);
305                 return 0;
306         }
307         return -1;
308
309 err_return:
310         RTE_LOG(WARNING, EAL, "Requested device " PCI_PRI_FMT
311                         " cannot be used\n", dev->addr.domain, dev->addr.bus,
312                         dev->addr.devid, dev->addr.function);
313         return -1;
314 }
315 #endif /* RTE_LIBRTE_EAL_HOTPLUG */
316
317 /*
318  * Scan the content of the PCI bus, and call the devinit() function for
319  * all registered drivers that have a matching entry in its id_table
320  * for discovered devices.
321  */
322 int
323 rte_eal_pci_probe(void)
324 {
325         struct rte_pci_device *dev = NULL;
326         struct rte_devargs *devargs;
327         int probe_all = 0;
328         int ret = 0;
329
330         if (rte_eal_devargs_type_count(RTE_DEVTYPE_WHITELISTED_PCI) == 0)
331                 probe_all = 1;
332
333         TAILQ_FOREACH(dev, &pci_device_list, next) {
334
335                 /* set devargs in PCI structure */
336                 devargs = pci_devargs_lookup(dev);
337                 if (devargs != NULL)
338                         dev->devargs = devargs;
339
340                 /* probe all or only whitelisted devices */
341                 if (probe_all)
342                         ret = pci_probe_all_drivers(dev);
343                 else if (devargs != NULL &&
344                         devargs->type == RTE_DEVTYPE_WHITELISTED_PCI)
345                         ret = pci_probe_all_drivers(dev);
346                 if (ret < 0)
347                         rte_exit(EXIT_FAILURE, "Requested device " PCI_PRI_FMT
348                                  " cannot be used\n", dev->addr.domain, dev->addr.bus,
349                                  dev->addr.devid, dev->addr.function);
350         }
351
352         return 0;
353 }
354
355 /* dump one device */
356 static int
357 pci_dump_one_device(FILE *f, struct rte_pci_device *dev)
358 {
359         int i;
360
361         fprintf(f, PCI_PRI_FMT, dev->addr.domain, dev->addr.bus,
362                dev->addr.devid, dev->addr.function);
363         fprintf(f, " - vendor:%x device:%x\n", dev->id.vendor_id,
364                dev->id.device_id);
365
366         for (i = 0; i != sizeof(dev->mem_resource) /
367                 sizeof(dev->mem_resource[0]); i++) {
368                 fprintf(f, "   %16.16"PRIx64" %16.16"PRIx64"\n",
369                         dev->mem_resource[i].phys_addr,
370                         dev->mem_resource[i].len);
371         }
372         return 0;
373 }
374
375 /* dump devices on the bus */
376 void
377 rte_eal_pci_dump(FILE *f)
378 {
379         struct rte_pci_device *dev = NULL;
380
381         TAILQ_FOREACH(dev, &pci_device_list, next) {
382                 pci_dump_one_device(f, dev);
383         }
384 }
385
386 /* register a driver */
387 void
388 rte_eal_pci_register(struct rte_pci_driver *driver)
389 {
390         TAILQ_INSERT_TAIL(&pci_driver_list, driver, next);
391 }
392
393 /* unregister a driver */
394 void
395 rte_eal_pci_unregister(struct rte_pci_driver *driver)
396 {
397         TAILQ_REMOVE(&pci_driver_list, driver, next);
398 }