95c32c18562b9af23c345e61090943bbcb18aa00
[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 "eal_filesystem.h"
71 #include "eal_private.h"
72
73 /**
74  * @file
75  * PCI probing under linux
76  *
77  * This code is used to simulate a PCI probe by parsing information in
78  * sysfs. Moreover, when a registered driver matches a device, the
79  * kernel driver currently using it is unloaded and replaced by
80  * igb_uio module, which is a very minimal userland driver for Intel
81  * network card, only providing access to PCI BAR to applications, and
82  * enabling bus master.
83  */
84
85 /* unbind kernel driver for this device */
86 int
87 pci_unbind_kernel_driver(struct rte_pci_device *dev __rte_unused)
88 {
89         RTE_LOG(ERR, EAL, "RTE_PCI_DRV_FORCE_UNBIND flag is not implemented "
90                 "for BSD\n");
91         return -ENOTSUP;
92 }
93
94 /* Map pci device */
95 int
96 rte_eal_pci_map_device(struct rte_pci_device *dev)
97 {
98         int ret = -1;
99
100         /* try mapping the NIC resources */
101         switch (dev->kdrv) {
102         case RTE_KDRV_NIC_UIO:
103                 /* map resources for devices that use uio */
104                 ret = pci_uio_map_resource(dev);
105                 break;
106         default:
107                 RTE_LOG(DEBUG, EAL,
108                         "  Not managed by a supported kernel driver, skipped\n");
109                 ret = 1;
110                 break;
111         }
112
113         return ret;
114 }
115
116 /* Unmap pci device */
117 void
118 rte_eal_pci_unmap_device(struct rte_pci_device *dev)
119 {
120         /* try unmapping the NIC resources */
121         switch (dev->kdrv) {
122         case RTE_KDRV_NIC_UIO:
123                 /* unmap resources for devices that use uio */
124                 pci_uio_unmap_resource(dev);
125                 break;
126         default:
127                 RTE_LOG(DEBUG, EAL,
128                         "  Not managed by a supported kernel driver, skipped\n");
129                 break;
130         }
131 }
132
133 void
134 pci_uio_free_resource(struct rte_pci_device *dev,
135                 struct mapped_pci_resource *uio_res)
136 {
137         rte_free(uio_res);
138
139         if (dev->intr_handle.fd) {
140                 close(dev->intr_handle.fd);
141                 dev->intr_handle.fd = -1;
142                 dev->intr_handle.type = RTE_INTR_HANDLE_UNKNOWN;
143         }
144 }
145
146 int
147 pci_uio_alloc_resource(struct rte_pci_device *dev,
148                 struct mapped_pci_resource **uio_res)
149 {
150         char devname[PATH_MAX]; /* contains the /dev/uioX */
151         struct rte_pci_addr *loc;
152
153         loc = &dev->addr;
154
155         snprintf(devname, sizeof(devname), "/dev/uio@pci:%u:%u:%u",
156                         dev->addr.bus, dev->addr.devid, dev->addr.function);
157
158         if (access(devname, O_RDWR) < 0) {
159                 RTE_LOG(WARNING, EAL, "  "PCI_PRI_FMT" not managed by UIO driver, "
160                                 "skipping\n", loc->domain, loc->bus, loc->devid, loc->function);
161                 return 1;
162         }
163
164         /* save fd if in primary process */
165         dev->intr_handle.fd = open(devname, O_RDWR);
166         if (dev->intr_handle.fd < 0) {
167                 RTE_LOG(ERR, EAL, "Cannot open %s: %s\n",
168                         devname, strerror(errno));
169                 goto error;
170         }
171         dev->intr_handle.type = RTE_INTR_HANDLE_UIO;
172
173         /* allocate the mapping details for secondary processes*/
174         *uio_res = rte_zmalloc("UIO_RES", sizeof(**uio_res), 0);
175         if (*uio_res == NULL) {
176                 RTE_LOG(ERR, EAL,
177                         "%s(): cannot store uio mmap details\n", __func__);
178                 goto error;
179         }
180
181         snprintf((*uio_res)->path, sizeof((*uio_res)->path), "%s", devname);
182         memcpy(&(*uio_res)->pci_addr, &dev->addr, sizeof((*uio_res)->pci_addr));
183
184         return 0;
185
186 error:
187         pci_uio_free_resource(dev, *uio_res);
188         return -1;
189 }
190
191 int
192 pci_uio_map_resource_by_index(struct rte_pci_device *dev, int res_idx,
193                 struct mapped_pci_resource *uio_res, int map_idx)
194 {
195         int fd;
196         char *devname;
197         void *mapaddr;
198         uint64_t offset;
199         uint64_t pagesz;
200         struct pci_map *maps;
201
202         maps = uio_res->maps;
203         devname = uio_res->path;
204         pagesz = sysconf(_SC_PAGESIZE);
205
206         /* allocate memory to keep path */
207         maps[map_idx].path = rte_malloc(NULL, strlen(devname) + 1, 0);
208         if (maps[map_idx].path == NULL) {
209                 RTE_LOG(ERR, EAL, "Cannot allocate memory for path: %s\n",
210                                 strerror(errno));
211                 return -1;
212         }
213
214         /*
215          * open resource file, to mmap it
216          */
217         fd = open(devname, O_RDWR);
218         if (fd < 0) {
219                 RTE_LOG(ERR, EAL, "Cannot open %s: %s\n",
220                                 devname, strerror(errno));
221                 goto error;
222         }
223
224         /* if matching map is found, then use it */
225         offset = res_idx * pagesz;
226         mapaddr = pci_map_resource(NULL, fd, (off_t)offset,
227                         (size_t)dev->mem_resource[res_idx].len, 0);
228         close(fd);
229         if (mapaddr == MAP_FAILED)
230                 goto error;
231
232         maps[map_idx].phaddr = dev->mem_resource[res_idx].phys_addr;
233         maps[map_idx].size = dev->mem_resource[res_idx].len;
234         maps[map_idx].addr = mapaddr;
235         maps[map_idx].offset = offset;
236         strcpy(maps[map_idx].path, devname);
237         dev->mem_resource[res_idx].addr = mapaddr;
238
239         return 0;
240
241 error:
242         rte_free(maps[map_idx].path);
243         return -1;
244 }
245
246 static int
247 pci_scan_one(int dev_pci_fd, struct pci_conf *conf)
248 {
249         struct rte_pci_device *dev;
250         struct pci_bar_io bar;
251         unsigned i, max;
252
253         dev = malloc(sizeof(*dev));
254         if (dev == NULL) {
255                 return -1;
256         }
257
258         memset(dev, 0, sizeof(*dev));
259         dev->addr.domain = conf->pc_sel.pc_domain;
260         dev->addr.bus = conf->pc_sel.pc_bus;
261         dev->addr.devid = conf->pc_sel.pc_dev;
262         dev->addr.function = conf->pc_sel.pc_func;
263
264         /* get vendor id */
265         dev->id.vendor_id = conf->pc_vendor;
266
267         /* get device id */
268         dev->id.device_id = conf->pc_device;
269
270         /* get subsystem_vendor id */
271         dev->id.subsystem_vendor_id = conf->pc_subvendor;
272
273         /* get subsystem_device id */
274         dev->id.subsystem_device_id = conf->pc_subdevice;
275
276         /* TODO: get max_vfs */
277         dev->max_vfs = 0;
278
279         /* FreeBSD has no NUMA support (yet) */
280         dev->numa_node = 0;
281
282         /* FreeBSD has only one pass through driver */
283         dev->kdrv = RTE_KDRV_NIC_UIO;
284
285         /* parse resources */
286         switch (conf->pc_hdr & PCIM_HDRTYPE) {
287         case PCIM_HDRTYPE_NORMAL:
288                 max = PCIR_MAX_BAR_0;
289                 break;
290         case PCIM_HDRTYPE_BRIDGE:
291                 max = PCIR_MAX_BAR_1;
292                 break;
293         case PCIM_HDRTYPE_CARDBUS:
294                 max = PCIR_MAX_BAR_2;
295                 break;
296         default:
297                 goto skipdev;
298         }
299
300         for (i = 0; i <= max; i++) {
301                 bar.pbi_sel = conf->pc_sel;
302                 bar.pbi_reg = PCIR_BAR(i);
303                 if (ioctl(dev_pci_fd, PCIOCGETBAR, &bar) < 0)
304                         continue;
305
306                 dev->mem_resource[i].len = bar.pbi_length;
307                 if (PCI_BAR_IO(bar.pbi_base)) {
308                         dev->mem_resource[i].addr = (void *)(bar.pbi_base & ~((uint64_t)0xf));
309                         continue;
310                 }
311                 dev->mem_resource[i].phys_addr = bar.pbi_base & ~((uint64_t)0xf);
312         }
313
314         /* device is valid, add in list (sorted) */
315         if (TAILQ_EMPTY(&pci_device_list)) {
316                 TAILQ_INSERT_TAIL(&pci_device_list, dev, next);
317         }
318         else {
319                 struct rte_pci_device *dev2 = NULL;
320                 int ret;
321
322                 TAILQ_FOREACH(dev2, &pci_device_list, next) {
323                         ret = rte_eal_compare_pci_addr(&dev->addr, &dev2->addr);
324                         if (ret > 0)
325                                 continue;
326                         else if (ret < 0) {
327                                 TAILQ_INSERT_BEFORE(dev2, dev, next);
328                                 return 0;
329                         } else { /* already registered */
330                                 dev2->kdrv = dev->kdrv;
331                                 dev2->max_vfs = dev->max_vfs;
332                                 memmove(dev2->mem_resource,
333                                         dev->mem_resource,
334                                         sizeof(dev->mem_resource));
335                                 free(dev);
336                                 return 0;
337                         }
338                 }
339                 TAILQ_INSERT_TAIL(&pci_device_list, dev, next);
340         }
341
342         return 0;
343
344 skipdev:
345         free(dev);
346         return 0;
347 }
348
349 /*
350  * Scan the content of the PCI bus, and add the devices in the devices
351  * list. Call pci_scan_one() for each pci entry found.
352  */
353 int
354 rte_eal_pci_scan(void)
355 {
356         int fd;
357         unsigned dev_count = 0;
358         struct pci_conf matches[16];
359         struct pci_conf_io conf_io = {
360                         .pat_buf_len = 0,
361                         .num_patterns = 0,
362                         .patterns = NULL,
363                         .match_buf_len = sizeof(matches),
364                         .matches = &matches[0],
365         };
366
367         fd = open("/dev/pci", O_RDONLY);
368         if (fd < 0) {
369                 RTE_LOG(ERR, EAL, "%s(): error opening /dev/pci\n", __func__);
370                 goto error;
371         }
372
373         do {
374                 unsigned i;
375                 if (ioctl(fd, PCIOCGETCONF, &conf_io) < 0) {
376                         RTE_LOG(ERR, EAL, "%s(): error with ioctl on /dev/pci: %s\n",
377                                         __func__, strerror(errno));
378                         goto error;
379                 }
380
381                 for (i = 0; i < conf_io.num_matches; i++)
382                         if (pci_scan_one(fd, &matches[i]) < 0)
383                                 goto error;
384
385                 dev_count += conf_io.num_matches;
386         } while(conf_io.status == PCI_GETCONF_MORE_DEVS);
387
388         close(fd);
389
390         RTE_LOG(ERR, EAL, "PCI scan found %u devices\n", dev_count);
391         return 0;
392
393 error:
394         if (fd >= 0)
395                 close(fd);
396         return -1;
397 }
398
399 /* Read PCI config space. */
400 int rte_eal_pci_read_config(const struct rte_pci_device *dev,
401                             void *buf, size_t len, off_t offset)
402 {
403         int fd = -1;
404         struct pci_io pi = {
405                 .pi_sel = {
406                         .pc_domain = dev->addr.domain,
407                         .pc_bus = dev->addr.bus,
408                         .pc_dev = dev->addr.devid,
409                         .pc_func = dev->addr.function,
410                 },
411                 .pi_reg = offset,
412                 .pi_width = len,
413         };
414
415         if (len == 3 || len > sizeof(pi.pi_data)) {
416                 RTE_LOG(ERR, EAL, "%s(): invalid pci read length\n", __func__);
417                 goto error;
418         }
419
420         fd = open("/dev/pci", O_RDONLY);
421         if (fd < 0) {
422                 RTE_LOG(ERR, EAL, "%s(): error opening /dev/pci\n", __func__);
423                 goto error;
424         }
425
426         if (ioctl(fd, PCIOCREAD, &pi) < 0)
427                 goto error;
428         close(fd);
429
430         memcpy(buf, &pi.pi_data, len);
431         return 0;
432
433  error:
434         if (fd >= 0)
435                 close(fd);
436         return -1;
437 }
438
439 /* Write PCI config space. */
440 int rte_eal_pci_write_config(const struct rte_pci_device *dev,
441                              const void *buf, size_t len, off_t offset)
442 {
443         int fd = -1;
444
445         struct pci_io pi = {
446                 .pi_sel = {
447                         .pc_domain = dev->addr.domain,
448                         .pc_bus = dev->addr.bus,
449                         .pc_dev = dev->addr.devid,
450                         .pc_func = dev->addr.function,
451                 },
452                 .pi_reg = offset,
453                 .pi_data = *(const uint32_t *)buf,
454                 .pi_width = len,
455         };
456
457         if (len == 3 || len > sizeof(pi.pi_data)) {
458                 RTE_LOG(ERR, EAL, "%s(): invalid pci read length\n", __func__);
459                 goto error;
460         }
461
462         memcpy(&pi.pi_data, buf, len);
463
464         fd = open("/dev/pci", O_RDONLY);
465         if (fd < 0) {
466                 RTE_LOG(ERR, EAL, "%s(): error opening /dev/pci\n", __func__);
467                 goto error;
468         }
469
470         if (ioctl(fd, PCIOCWRITE, &pi) < 0)
471                 goto error;
472
473         close(fd);
474         return 0;
475
476  error:
477         if (fd >= 0)
478                 close(fd);
479         return -1;
480 }
481
482 /* Init the PCI EAL subsystem */
483 int
484 rte_eal_pci_init(void)
485 {
486         TAILQ_INIT(&pci_driver_list);
487         TAILQ_INIT(&pci_device_list);
488
489         /* for debug purposes, PCI can be disabled */
490         if (internal_config.no_pci)
491                 return 0;
492
493         if (rte_eal_pci_scan() < 0) {
494                 RTE_LOG(ERR, EAL, "%s(): Cannot scan PCI bus\n", __func__);
495                 return -1;
496         }
497         return 0;
498 }