pci: merge resource mapping functions for linux and bsd
[dpdk.git] / lib / librte_eal / bsdapp / eal / eal_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
34 #include <ctype.h>
35 #include <stdio.h>
36 #include <stdlib.h>
37 #include <string.h>
38 #include <stdarg.h>
39 #include <unistd.h>
40 #include <inttypes.h>
41 #include <sys/types.h>
42 #include <sys/stat.h>
43 #include <fcntl.h>
44 #include <stdarg.h>
45 #include <errno.h>
46 #include <dirent.h>
47 #include <limits.h>
48 #include <sys/queue.h>
49 #include <sys/mman.h>
50 #include <sys/ioctl.h>
51 #include <sys/pciio.h>
52 #include <dev/pci/pcireg.h>
53
54 #include <rte_interrupts.h>
55 #include <rte_log.h>
56 #include <rte_pci.h>
57 #include <rte_common.h>
58 #include <rte_launch.h>
59 #include <rte_memory.h>
60 #include <rte_memzone.h>
61 #include <rte_eal.h>
62 #include <rte_eal_memconfig.h>
63 #include <rte_per_lcore.h>
64 #include <rte_lcore.h>
65 #include <rte_malloc.h>
66 #include <rte_string_fns.h>
67 #include <rte_debug.h>
68 #include <rte_devargs.h>
69
70 #include "rte_pci_dev_ids.h"
71 #include "eal_filesystem.h"
72 #include "eal_private.h"
73
74 /**
75  * @file
76  * PCI probing under linux
77  *
78  * This code is used to simulate a PCI probe by parsing information in
79  * sysfs. Moreover, when a registered driver matches a device, the
80  * kernel driver currently using it is unloaded and replaced by
81  * igb_uio module, which is a very minimal userland driver for Intel
82  * network card, only providing access to PCI BAR to applications, and
83  * enabling bus master.
84  */
85 static struct rte_tailq_elem rte_uio_tailq = {
86         .name = "UIO_RESOURCE_LIST",
87 };
88 EAL_REGISTER_TAILQ(rte_uio_tailq)
89
90 /* unbind kernel driver for this device */
91 static int
92 pci_unbind_kernel_driver(struct rte_pci_device *dev __rte_unused)
93 {
94         RTE_LOG(ERR, EAL, "RTE_PCI_DRV_FORCE_UNBIND flag is not implemented "
95                 "for BSD\n");
96         return -ENOTSUP;
97 }
98
99 static int
100 pci_uio_map_secondary(struct rte_pci_device *dev)
101 {
102         int i, fd;
103         struct mapped_pci_resource *uio_res;
104         struct mapped_pci_res_list *uio_res_list =
105                         RTE_TAILQ_CAST(rte_uio_tailq.head, mapped_pci_res_list);
106
107         TAILQ_FOREACH(uio_res, uio_res_list, next) {
108
109                 /* skip this element if it doesn't match our PCI address */
110                 if (rte_eal_compare_pci_addr(&uio_res->pci_addr, &dev->addr))
111                         continue;
112
113                 for (i = 0; i != uio_res->nb_maps; i++) {
114                         /*
115                          * open devname, to mmap it
116                          */
117                         fd = open(uio_res->maps[i].path, O_RDWR);
118                         if (fd < 0) {
119                                 RTE_LOG(ERR, EAL, "Cannot open %s: %s\n",
120                                         uio_res->maps[i].path, strerror(errno));
121                                 return -1;
122                         }
123
124                         void *mapaddr = pci_map_resource(uio_res->maps[i].addr,
125                                         fd, (off_t)uio_res->maps[i].offset,
126                                         (size_t)uio_res->maps[i].size, 0);
127                         /* fd is not needed in slave process, close it */
128                         close(fd);
129                         if (mapaddr != uio_res->maps[i].addr) {
130                                 RTE_LOG(ERR, EAL,
131                                         "Cannot mmap device resource file %s to address: %p\n",
132                                         uio_res->maps[i].path,
133                                         uio_res->maps[i].addr);
134                                 return -1;
135                         }
136                 }
137                 return 0;
138         }
139
140         RTE_LOG(ERR, EAL, "Cannot find resource for device\n");
141         return 1;
142 }
143
144 static void
145 pci_uio_free_resource(struct rte_pci_device *dev,
146                 struct mapped_pci_resource *uio_res)
147 {
148         rte_free(uio_res);
149
150         if (dev->intr_handle.fd) {
151                 close(dev->intr_handle.fd);
152                 dev->intr_handle.fd = -1;
153                 dev->intr_handle.type = RTE_INTR_HANDLE_UNKNOWN;
154         }
155 }
156
157 static int
158 pci_uio_alloc_resource(struct rte_pci_device *dev,
159                 struct mapped_pci_resource **uio_res)
160 {
161         char devname[PATH_MAX]; /* contains the /dev/uioX */
162         struct rte_pci_addr *loc;
163
164         loc = &dev->addr;
165
166         snprintf(devname, sizeof(devname), "/dev/uio@pci:%u:%u:%u",
167                         dev->addr.bus, dev->addr.devid, dev->addr.function);
168
169         if (access(devname, O_RDWR) < 0) {
170                 RTE_LOG(WARNING, EAL, "  "PCI_PRI_FMT" not managed by UIO driver, "
171                                 "skipping\n", loc->domain, loc->bus, loc->devid, loc->function);
172                 return 1;
173         }
174
175         /* save fd if in primary process */
176         dev->intr_handle.fd = open(devname, O_RDWR);
177         if (dev->intr_handle.fd < 0) {
178                 RTE_LOG(ERR, EAL, "Cannot open %s: %s\n",
179                         devname, strerror(errno));
180                 goto error;
181         }
182         dev->intr_handle.type = RTE_INTR_HANDLE_UIO;
183
184         /* allocate the mapping details for secondary processes*/
185         *uio_res = rte_zmalloc("UIO_RES", sizeof(**uio_res), 0);
186         if (*uio_res == NULL) {
187                 RTE_LOG(ERR, EAL,
188                         "%s(): cannot store uio mmap details\n", __func__);
189                 goto error;
190         }
191
192         snprintf((*uio_res)->path, sizeof((*uio_res)->path), "%s", devname);
193         memcpy(&(*uio_res)->pci_addr, &dev->addr, sizeof((*uio_res)->pci_addr));
194
195         return 0;
196
197 error:
198         pci_uio_free_resource(dev, *uio_res);
199         return -1;
200 }
201
202 static int
203 pci_uio_map_resource_by_index(struct rte_pci_device *dev, int res_idx,
204                 struct mapped_pci_resource *uio_res, int map_idx)
205 {
206         int fd;
207         char *devname;
208         void *mapaddr;
209         uint64_t offset;
210         uint64_t pagesz;
211         struct pci_map *maps;
212
213         maps = uio_res->maps;
214         devname = uio_res->path;
215         pagesz = sysconf(_SC_PAGESIZE);
216
217         /* allocate memory to keep path */
218         maps[map_idx].path = rte_malloc(NULL, strlen(devname) + 1, 0);
219         if (maps[map_idx].path == NULL) {
220                 RTE_LOG(ERR, EAL, "Cannot allocate memory for path: %s\n",
221                                 strerror(errno));
222                 return -1;
223         }
224
225         /*
226          * open resource file, to mmap it
227          */
228         fd = open(devname, O_RDWR);
229         if (fd < 0) {
230                 RTE_LOG(ERR, EAL, "Cannot open %s: %s\n",
231                                 devname, strerror(errno));
232                 goto error;
233         }
234
235         /* if matching map is found, then use it */
236         offset = res_idx * pagesz;
237         mapaddr = pci_map_resource(NULL, fd, (off_t)offset,
238                         (size_t)dev->mem_resource[res_idx].len, 0);
239         close(fd);
240         if (mapaddr == MAP_FAILED)
241                 goto error;
242
243         maps[map_idx].phaddr = dev->mem_resource[res_idx].phys_addr;
244         maps[map_idx].size = dev->mem_resource[res_idx].len;
245         maps[map_idx].addr = mapaddr;
246         maps[map_idx].offset = offset;
247         strcpy(maps[map_idx].path, devname);
248         dev->mem_resource[res_idx].addr = mapaddr;
249
250         return 0;
251
252 error:
253         rte_free(maps[map_idx].path);
254         return -1;
255 }
256
257 /* map the PCI resource of a PCI device in virtual memory */
258 static int
259 pci_uio_map_resource(struct rte_pci_device *dev)
260 {
261         int i, map_idx = 0, ret;
262         uint64_t phaddr;
263         struct mapped_pci_resource *uio_res = NULL;
264         struct mapped_pci_res_list *uio_res_list =
265                 RTE_TAILQ_CAST(rte_uio_tailq.head, mapped_pci_res_list);
266
267         dev->intr_handle.fd = -1;
268         dev->intr_handle.type = RTE_INTR_HANDLE_UNKNOWN;
269
270         /* secondary processes - use already recorded details */
271         if (rte_eal_process_type() != RTE_PROC_PRIMARY)
272                 return pci_uio_map_secondary(dev);
273
274         /* allocate uio resource */
275         ret = pci_uio_alloc_resource(dev, &uio_res);
276         if (ret)
277                 return ret;
278
279         /* Map all BARs */
280         for (i = 0; i != PCI_MAX_RESOURCE; i++) {
281                 /* skip empty BAR */
282                 if ((phaddr = dev->mem_resource[i].phys_addr) == 0)
283                         continue;
284
285                 ret = pci_uio_map_resource_by_index(dev, i,
286                                 uio_res, map_idx);
287                 if (ret)
288                         goto error;
289
290                 map_idx++;
291         }
292
293         uio_res->nb_maps = map_idx;
294
295         TAILQ_INSERT_TAIL(uio_res_list, uio_res, next);
296
297         return 0;
298 error:
299         for (i = 0; i < map_idx; i++)
300                 rte_free(uio_res->maps[i].path);
301         pci_uio_free_resource(dev, uio_res);
302         return -1;
303 }
304
305 /* Scan one pci sysfs entry, and fill the devices list from it. */
306 static int
307 pci_scan_one(int dev_pci_fd, struct pci_conf *conf)
308 {
309         struct rte_pci_device *dev;
310         struct pci_bar_io bar;
311         unsigned i, max;
312
313         dev = malloc(sizeof(*dev));
314         if (dev == NULL) {
315                 return -1;
316         }
317
318         memset(dev, 0, sizeof(*dev));
319         dev->addr.domain = conf->pc_sel.pc_domain;
320         dev->addr.bus = conf->pc_sel.pc_bus;
321         dev->addr.devid = conf->pc_sel.pc_dev;
322         dev->addr.function = conf->pc_sel.pc_func;
323
324         /* get vendor id */
325         dev->id.vendor_id = conf->pc_vendor;
326
327         /* get device id */
328         dev->id.device_id = conf->pc_device;
329
330         /* get subsystem_vendor id */
331         dev->id.subsystem_vendor_id = conf->pc_subvendor;
332
333         /* get subsystem_device id */
334         dev->id.subsystem_device_id = conf->pc_subdevice;
335
336         /* TODO: get max_vfs */
337         dev->max_vfs = 0;
338
339         /* FreeBSD has no NUMA support (yet) */
340         dev->numa_node = 0;
341
342         /* parse resources */
343         switch (conf->pc_hdr & PCIM_HDRTYPE) {
344         case PCIM_HDRTYPE_NORMAL:
345                 max = PCIR_MAX_BAR_0;
346                 break;
347         case PCIM_HDRTYPE_BRIDGE:
348                 max = PCIR_MAX_BAR_1;
349                 break;
350         case PCIM_HDRTYPE_CARDBUS:
351                 max = PCIR_MAX_BAR_2;
352                 break;
353         default:
354                 goto skipdev;
355         }
356
357         for (i = 0; i <= max; i++) {
358                 bar.pbi_sel = conf->pc_sel;
359                 bar.pbi_reg = PCIR_BAR(i);
360                 if (ioctl(dev_pci_fd, PCIOCGETBAR, &bar) < 0)
361                         continue;
362
363                 dev->mem_resource[i].len = bar.pbi_length;
364                 if (PCI_BAR_IO(bar.pbi_base)) {
365                         dev->mem_resource[i].addr = (void *)(bar.pbi_base & ~((uint64_t)0xf));
366                         continue;
367                 }
368                 dev->mem_resource[i].phys_addr = bar.pbi_base & ~((uint64_t)0xf);
369         }
370
371         /* device is valid, add in list (sorted) */
372         if (TAILQ_EMPTY(&pci_device_list)) {
373                 TAILQ_INSERT_TAIL(&pci_device_list, dev, next);
374         }
375         else {
376                 struct rte_pci_device *dev2 = NULL;
377                 int ret;
378
379                 TAILQ_FOREACH(dev2, &pci_device_list, next) {
380                         ret = rte_eal_compare_pci_addr(&dev->addr, &dev2->addr);
381                         if (ret > 0)
382                                 continue;
383                         else if (ret < 0) {
384                                 TAILQ_INSERT_BEFORE(dev2, dev, next);
385                                 return 0;
386                         } else { /* already registered */
387                                 dev2->kdrv = dev->kdrv;
388                                 dev2->max_vfs = dev->max_vfs;
389                                 memmove(dev2->mem_resource,
390                                         dev->mem_resource,
391                                         sizeof(dev->mem_resource));
392                                 free(dev);
393                                 return 0;
394                         }
395                 }
396                 TAILQ_INSERT_TAIL(&pci_device_list, dev, next);
397         }
398
399         return 0;
400
401 skipdev:
402         free(dev);
403         return 0;
404 }
405
406 /*
407  * Scan the content of the PCI bus, and add the devices in the devices
408  * list. Call pci_scan_one() for each pci entry found.
409  */
410 static int
411 pci_scan(void)
412 {
413         int fd;
414         unsigned dev_count = 0;
415         struct pci_conf matches[16];
416         struct pci_conf_io conf_io = {
417                         .pat_buf_len = 0,
418                         .num_patterns = 0,
419                         .patterns = NULL,
420                         .match_buf_len = sizeof(matches),
421                         .matches = &matches[0],
422         };
423
424         fd = open("/dev/pci", O_RDONLY);
425         if (fd < 0) {
426                 RTE_LOG(ERR, EAL, "%s(): error opening /dev/pci\n", __func__);
427                 goto error;
428         }
429
430         do {
431                 unsigned i;
432                 if (ioctl(fd, PCIOCGETCONF, &conf_io) < 0) {
433                         RTE_LOG(ERR, EAL, "%s(): error with ioctl on /dev/pci: %s\n",
434                                         __func__, strerror(errno));
435                         goto error;
436                 }
437
438                 for (i = 0; i < conf_io.num_matches; i++)
439                         if (pci_scan_one(fd, &matches[i]) < 0)
440                                 goto error;
441
442                 dev_count += conf_io.num_matches;
443         } while(conf_io.status == PCI_GETCONF_MORE_DEVS);
444
445         close(fd);
446
447         RTE_LOG(ERR, EAL, "PCI scan found %u devices\n", dev_count);
448         return 0;
449
450 error:
451         if (fd >= 0)
452                 close(fd);
453         return -1;
454 }
455
456 /*
457  * If vendor/device ID match, call the devinit() function of the
458  * driver.
459  */
460 int
461 rte_eal_pci_probe_one_driver(struct rte_pci_driver *dr, struct rte_pci_device *dev)
462 {
463         const struct rte_pci_id *id_table;
464         int ret;
465
466         for (id_table = dr->id_table ; id_table->vendor_id != 0; id_table++) {
467
468                 /* check if device's identifiers match the driver's ones */
469                 if (id_table->vendor_id != dev->id.vendor_id &&
470                                 id_table->vendor_id != PCI_ANY_ID)
471                         continue;
472                 if (id_table->device_id != dev->id.device_id &&
473                                 id_table->device_id != PCI_ANY_ID)
474                         continue;
475                 if (id_table->subsystem_vendor_id != dev->id.subsystem_vendor_id &&
476                                 id_table->subsystem_vendor_id != PCI_ANY_ID)
477                         continue;
478                 if (id_table->subsystem_device_id != dev->id.subsystem_device_id &&
479                                 id_table->subsystem_device_id != PCI_ANY_ID)
480                         continue;
481
482                 struct rte_pci_addr *loc = &dev->addr;
483
484                 RTE_LOG(DEBUG, EAL, "PCI device "PCI_PRI_FMT" on NUMA socket %i\n",
485                                 loc->domain, loc->bus, loc->devid, loc->function,
486                                 dev->numa_node);
487
488                 RTE_LOG(DEBUG, EAL, "  probe driver: %x:%x %s\n", dev->id.vendor_id,
489                                 dev->id.device_id, dr->name);
490
491                 /* no initialization when blacklisted, return without error */
492                 if (dev->devargs != NULL &&
493                         dev->devargs->type == RTE_DEVTYPE_BLACKLISTED_PCI) {
494
495                         RTE_LOG(DEBUG, EAL, "  Device is blacklisted, not initializing\n");
496                         return 0;
497                 }
498
499                 if (dr->drv_flags & RTE_PCI_DRV_NEED_MAPPING) {
500                         /* map resources for devices that use igb_uio */
501                         ret = pci_uio_map_resource(dev);
502                         if (ret != 0)
503                                 return ret;
504                 } else if (dr->drv_flags & RTE_PCI_DRV_FORCE_UNBIND &&
505                            rte_eal_process_type() == RTE_PROC_PRIMARY) {
506                         /* unbind current driver */
507                         if (pci_unbind_kernel_driver(dev) < 0)
508                                 return -1;
509                 }
510
511                 /* reference driver structure */
512                 dev->driver = dr;
513
514                 /* call the driver devinit() function */
515                 return dr->devinit(dr, dev);
516         }
517         /* return positive value if driver is not found */
518         return 1;
519 }
520
521 /* Init the PCI EAL subsystem */
522 int
523 rte_eal_pci_init(void)
524 {
525         TAILQ_INIT(&pci_driver_list);
526         TAILQ_INIT(&pci_device_list);
527
528         /* for debug purposes, PCI can be disabled */
529         if (internal_config.no_pci)
530                 return 0;
531
532         if (pci_scan() < 0) {
533                 RTE_LOG(ERR, EAL, "%s(): Cannot scan PCI bus\n", __func__);
534                 return -1;
535         }
536         return 0;
537 }