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