ed312229a649e506a6ed50b753fa56e04ad15270
[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 /* unbind kernel driver for this device */
87 int
88 pci_unbind_kernel_driver(struct rte_pci_device *dev __rte_unused)
89 {
90         RTE_LOG(ERR, EAL, "RTE_PCI_DRV_FORCE_UNBIND flag is not implemented "
91                 "for BSD\n");
92         return -ENOTSUP;
93 }
94
95 /* Map pci device */
96 int
97 pci_map_device(struct rte_pci_device *dev)
98 {
99         int ret = -1;
100
101         /* try mapping the NIC resources */
102         switch (dev->kdrv) {
103         case RTE_KDRV_NIC_UIO:
104                 /* map resources for devices that use uio */
105                 ret = pci_uio_map_resource(dev);
106                 break;
107         default:
108                 RTE_LOG(DEBUG, EAL,
109                         "  Not managed by a supported kernel driver, skipped\n");
110                 ret = 1;
111                 break;
112         }
113
114         return ret;
115 }
116
117 /* Unmap pci device */
118 void
119 pci_unmap_device(struct rte_pci_device *dev)
120 {
121         /* try unmapping the NIC resources */
122         switch (dev->kdrv) {
123         case RTE_KDRV_NIC_UIO:
124                 /* unmap resources for devices that use uio */
125                 pci_uio_unmap_resource(dev);
126                 break;
127         default:
128                 RTE_LOG(DEBUG, EAL,
129                         "  Not managed by a supported kernel driver, skipped\n");
130                 break;
131         }
132 }
133
134 void
135 pci_uio_free_resource(struct rte_pci_device *dev,
136                 struct mapped_pci_resource *uio_res)
137 {
138         rte_free(uio_res);
139
140         if (dev->intr_handle.fd) {
141                 close(dev->intr_handle.fd);
142                 dev->intr_handle.fd = -1;
143                 dev->intr_handle.type = RTE_INTR_HANDLE_UNKNOWN;
144         }
145 }
146
147 int
148 pci_uio_alloc_resource(struct rte_pci_device *dev,
149                 struct mapped_pci_resource **uio_res)
150 {
151         char devname[PATH_MAX]; /* contains the /dev/uioX */
152         struct rte_pci_addr *loc;
153
154         loc = &dev->addr;
155
156         snprintf(devname, sizeof(devname), "/dev/uio@pci:%u:%u:%u",
157                         dev->addr.bus, dev->addr.devid, dev->addr.function);
158
159         if (access(devname, O_RDWR) < 0) {
160                 RTE_LOG(WARNING, EAL, "  "PCI_PRI_FMT" not managed by UIO driver, "
161                                 "skipping\n", loc->domain, loc->bus, loc->devid, loc->function);
162                 return 1;
163         }
164
165         /* save fd if in primary process */
166         dev->intr_handle.fd = open(devname, O_RDWR);
167         if (dev->intr_handle.fd < 0) {
168                 RTE_LOG(ERR, EAL, "Cannot open %s: %s\n",
169                         devname, strerror(errno));
170                 goto error;
171         }
172         dev->intr_handle.type = RTE_INTR_HANDLE_UIO;
173
174         /* allocate the mapping details for secondary processes*/
175         *uio_res = rte_zmalloc("UIO_RES", sizeof(**uio_res), 0);
176         if (*uio_res == NULL) {
177                 RTE_LOG(ERR, EAL,
178                         "%s(): cannot store uio mmap details\n", __func__);
179                 goto error;
180         }
181
182         snprintf((*uio_res)->path, sizeof((*uio_res)->path), "%s", devname);
183         memcpy(&(*uio_res)->pci_addr, &dev->addr, sizeof((*uio_res)->pci_addr));
184
185         return 0;
186
187 error:
188         pci_uio_free_resource(dev, *uio_res);
189         return -1;
190 }
191
192 int
193 pci_uio_map_resource_by_index(struct rte_pci_device *dev, int res_idx,
194                 struct mapped_pci_resource *uio_res, int map_idx)
195 {
196         int fd;
197         char *devname;
198         void *mapaddr;
199         uint64_t offset;
200         uint64_t pagesz;
201         struct pci_map *maps;
202
203         maps = uio_res->maps;
204         devname = uio_res->path;
205         pagesz = sysconf(_SC_PAGESIZE);
206
207         /* allocate memory to keep path */
208         maps[map_idx].path = rte_malloc(NULL, strlen(devname) + 1, 0);
209         if (maps[map_idx].path == NULL) {
210                 RTE_LOG(ERR, EAL, "Cannot allocate memory for path: %s\n",
211                                 strerror(errno));
212                 return -1;
213         }
214
215         /*
216          * open resource file, to mmap it
217          */
218         fd = open(devname, O_RDWR);
219         if (fd < 0) {
220                 RTE_LOG(ERR, EAL, "Cannot open %s: %s\n",
221                                 devname, strerror(errno));
222                 goto error;
223         }
224
225         /* if matching map is found, then use it */
226         offset = res_idx * pagesz;
227         mapaddr = pci_map_resource(NULL, fd, (off_t)offset,
228                         (size_t)dev->mem_resource[res_idx].len, 0);
229         close(fd);
230         if (mapaddr == MAP_FAILED)
231                 goto error;
232
233         maps[map_idx].phaddr = dev->mem_resource[res_idx].phys_addr;
234         maps[map_idx].size = dev->mem_resource[res_idx].len;
235         maps[map_idx].addr = mapaddr;
236         maps[map_idx].offset = offset;
237         strcpy(maps[map_idx].path, devname);
238         dev->mem_resource[res_idx].addr = mapaddr;
239
240         return 0;
241
242 error:
243         rte_free(maps[map_idx].path);
244         return -1;
245 }
246
247 static int
248 pci_scan_one(int dev_pci_fd, struct pci_conf *conf)
249 {
250         struct rte_pci_device *dev;
251         struct pci_bar_io bar;
252         unsigned i, max;
253
254         dev = malloc(sizeof(*dev));
255         if (dev == NULL) {
256                 return -1;
257         }
258
259         memset(dev, 0, sizeof(*dev));
260         dev->addr.domain = conf->pc_sel.pc_domain;
261         dev->addr.bus = conf->pc_sel.pc_bus;
262         dev->addr.devid = conf->pc_sel.pc_dev;
263         dev->addr.function = conf->pc_sel.pc_func;
264
265         /* get vendor id */
266         dev->id.vendor_id = conf->pc_vendor;
267
268         /* get device id */
269         dev->id.device_id = conf->pc_device;
270
271         /* get subsystem_vendor id */
272         dev->id.subsystem_vendor_id = conf->pc_subvendor;
273
274         /* get subsystem_device id */
275         dev->id.subsystem_device_id = conf->pc_subdevice;
276
277         /* TODO: get max_vfs */
278         dev->max_vfs = 0;
279
280         /* FreeBSD has no NUMA support (yet) */
281         dev->numa_node = 0;
282
283         /* FreeBSD has only one pass through driver */
284         dev->kdrv = RTE_KDRV_NIC_UIO;
285
286         /* parse resources */
287         switch (conf->pc_hdr & PCIM_HDRTYPE) {
288         case PCIM_HDRTYPE_NORMAL:
289                 max = PCIR_MAX_BAR_0;
290                 break;
291         case PCIM_HDRTYPE_BRIDGE:
292                 max = PCIR_MAX_BAR_1;
293                 break;
294         case PCIM_HDRTYPE_CARDBUS:
295                 max = PCIR_MAX_BAR_2;
296                 break;
297         default:
298                 goto skipdev;
299         }
300
301         for (i = 0; i <= max; i++) {
302                 bar.pbi_sel = conf->pc_sel;
303                 bar.pbi_reg = PCIR_BAR(i);
304                 if (ioctl(dev_pci_fd, PCIOCGETBAR, &bar) < 0)
305                         continue;
306
307                 dev->mem_resource[i].len = bar.pbi_length;
308                 if (PCI_BAR_IO(bar.pbi_base)) {
309                         dev->mem_resource[i].addr = (void *)(bar.pbi_base & ~((uint64_t)0xf));
310                         continue;
311                 }
312                 dev->mem_resource[i].phys_addr = bar.pbi_base & ~((uint64_t)0xf);
313         }
314
315         /* device is valid, add in list (sorted) */
316         if (TAILQ_EMPTY(&pci_device_list)) {
317                 TAILQ_INSERT_TAIL(&pci_device_list, dev, next);
318         }
319         else {
320                 struct rte_pci_device *dev2 = NULL;
321                 int ret;
322
323                 TAILQ_FOREACH(dev2, &pci_device_list, next) {
324                         ret = rte_eal_compare_pci_addr(&dev->addr, &dev2->addr);
325                         if (ret > 0)
326                                 continue;
327                         else if (ret < 0) {
328                                 TAILQ_INSERT_BEFORE(dev2, dev, next);
329                                 return 0;
330                         } else { /* already registered */
331                                 dev2->kdrv = dev->kdrv;
332                                 dev2->max_vfs = dev->max_vfs;
333                                 memmove(dev2->mem_resource,
334                                         dev->mem_resource,
335                                         sizeof(dev->mem_resource));
336                                 free(dev);
337                                 return 0;
338                         }
339                 }
340                 TAILQ_INSERT_TAIL(&pci_device_list, dev, next);
341         }
342
343         return 0;
344
345 skipdev:
346         free(dev);
347         return 0;
348 }
349
350 /*
351  * Scan the content of the PCI bus, and add the devices in the devices
352  * list. Call pci_scan_one() for each pci entry found.
353  */
354 int
355 rte_eal_pci_scan(void)
356 {
357         int fd;
358         unsigned dev_count = 0;
359         struct pci_conf matches[16];
360         struct pci_conf_io conf_io = {
361                         .pat_buf_len = 0,
362                         .num_patterns = 0,
363                         .patterns = NULL,
364                         .match_buf_len = sizeof(matches),
365                         .matches = &matches[0],
366         };
367
368         fd = open("/dev/pci", O_RDONLY);
369         if (fd < 0) {
370                 RTE_LOG(ERR, EAL, "%s(): error opening /dev/pci\n", __func__);
371                 goto error;
372         }
373
374         do {
375                 unsigned i;
376                 if (ioctl(fd, PCIOCGETCONF, &conf_io) < 0) {
377                         RTE_LOG(ERR, EAL, "%s(): error with ioctl on /dev/pci: %s\n",
378                                         __func__, strerror(errno));
379                         goto error;
380                 }
381
382                 for (i = 0; i < conf_io.num_matches; i++)
383                         if (pci_scan_one(fd, &matches[i]) < 0)
384                                 goto error;
385
386                 dev_count += conf_io.num_matches;
387         } while(conf_io.status == PCI_GETCONF_MORE_DEVS);
388
389         close(fd);
390
391         RTE_LOG(ERR, EAL, "PCI scan found %u devices\n", dev_count);
392         return 0;
393
394 error:
395         if (fd >= 0)
396                 close(fd);
397         return -1;
398 }
399
400 /* Init the PCI EAL subsystem */
401 int
402 rte_eal_pci_init(void)
403 {
404         TAILQ_INIT(&pci_driver_list);
405         TAILQ_INIT(&pci_device_list);
406
407         /* for debug purposes, PCI can be disabled */
408         if (internal_config.no_pci)
409                 return 0;
410
411         if (rte_eal_pci_scan() < 0) {
412                 RTE_LOG(ERR, EAL, "%s(): Cannot scan PCI bus\n", __func__);
413                 return -1;
414         }
415         return 0;
416 }