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