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