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