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