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