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