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