Imported Upstream version 16.11.2
[deb_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 #if defined(RTE_ARCH_X86)
55 #include <sys/types.h>
56 #include <machine/cpufunc.h>
57 #endif
58
59 #include <rte_interrupts.h>
60 #include <rte_log.h>
61 #include <rte_pci.h>
62 #include <rte_common.h>
63 #include <rte_launch.h>
64 #include <rte_memory.h>
65 #include <rte_memzone.h>
66 #include <rte_eal.h>
67 #include <rte_eal_memconfig.h>
68 #include <rte_per_lcore.h>
69 #include <rte_lcore.h>
70 #include <rte_malloc.h>
71 #include <rte_string_fns.h>
72 #include <rte_debug.h>
73 #include <rte_devargs.h>
74
75 #include "eal_filesystem.h"
76 #include "eal_private.h"
77
78 /**
79  * @file
80  * PCI probing under linux
81  *
82  * This code is used to simulate a PCI probe by parsing information in
83  * sysfs. Moreover, when a registered driver matches a device, the
84  * kernel driver currently using it is unloaded and replaced by
85  * igb_uio module, which is a very minimal userland driver for Intel
86  * network card, only providing access to PCI BAR to applications, and
87  * enabling bus master.
88  */
89
90 /* unbind kernel driver for this device */
91 int
92 pci_unbind_kernel_driver(struct rte_pci_device *dev __rte_unused)
93 {
94         RTE_LOG(ERR, EAL, "RTE_PCI_DRV_FORCE_UNBIND flag is not implemented "
95                 "for BSD\n");
96         return -ENOTSUP;
97 }
98
99 /* Map pci device */
100 int
101 rte_eal_pci_map_device(struct rte_pci_device *dev)
102 {
103         int ret = -1;
104
105         /* try mapping the NIC resources */
106         switch (dev->kdrv) {
107         case RTE_KDRV_NIC_UIO:
108                 /* map resources for devices that use uio */
109                 ret = pci_uio_map_resource(dev);
110                 break;
111         default:
112                 RTE_LOG(DEBUG, EAL,
113                         "  Not managed by a supported kernel driver, skipped\n");
114                 ret = 1;
115                 break;
116         }
117
118         return ret;
119 }
120
121 /* Unmap pci device */
122 void
123 rte_eal_pci_unmap_device(struct rte_pci_device *dev)
124 {
125         /* try unmapping the NIC resources */
126         switch (dev->kdrv) {
127         case RTE_KDRV_NIC_UIO:
128                 /* unmap resources for devices that use uio */
129                 pci_uio_unmap_resource(dev);
130                 break;
131         default:
132                 RTE_LOG(DEBUG, EAL,
133                         "  Not managed by a supported kernel driver, skipped\n");
134                 break;
135         }
136 }
137
138 void
139 pci_uio_free_resource(struct rte_pci_device *dev,
140                 struct mapped_pci_resource *uio_res)
141 {
142         rte_free(uio_res);
143
144         if (dev->intr_handle.fd) {
145                 close(dev->intr_handle.fd);
146                 dev->intr_handle.fd = -1;
147                 dev->intr_handle.type = RTE_INTR_HANDLE_UNKNOWN;
148         }
149 }
150
151 int
152 pci_uio_alloc_resource(struct rte_pci_device *dev,
153                 struct mapped_pci_resource **uio_res)
154 {
155         char devname[PATH_MAX]; /* contains the /dev/uioX */
156         struct rte_pci_addr *loc;
157
158         loc = &dev->addr;
159
160         snprintf(devname, sizeof(devname), "/dev/uio@pci:%u:%u:%u",
161                         dev->addr.bus, dev->addr.devid, dev->addr.function);
162
163         if (access(devname, O_RDWR) < 0) {
164                 RTE_LOG(WARNING, EAL, "  "PCI_PRI_FMT" not managed by UIO driver, "
165                                 "skipping\n", loc->domain, loc->bus, loc->devid, loc->function);
166                 return 1;
167         }
168
169         /* save fd if in primary process */
170         dev->intr_handle.fd = open(devname, O_RDWR);
171         if (dev->intr_handle.fd < 0) {
172                 RTE_LOG(ERR, EAL, "Cannot open %s: %s\n",
173                         devname, strerror(errno));
174                 goto error;
175         }
176         dev->intr_handle.type = RTE_INTR_HANDLE_UIO;
177
178         /* allocate the mapping details for secondary processes*/
179         *uio_res = rte_zmalloc("UIO_RES", sizeof(**uio_res), 0);
180         if (*uio_res == NULL) {
181                 RTE_LOG(ERR, EAL,
182                         "%s(): cannot store uio mmap details\n", __func__);
183                 goto error;
184         }
185
186         snprintf((*uio_res)->path, sizeof((*uio_res)->path), "%s", devname);
187         memcpy(&(*uio_res)->pci_addr, &dev->addr, sizeof((*uio_res)->pci_addr));
188
189         return 0;
190
191 error:
192         pci_uio_free_resource(dev, *uio_res);
193         return -1;
194 }
195
196 int
197 pci_uio_map_resource_by_index(struct rte_pci_device *dev, int res_idx,
198                 struct mapped_pci_resource *uio_res, int map_idx)
199 {
200         int fd;
201         char *devname;
202         void *mapaddr;
203         uint64_t offset;
204         uint64_t pagesz;
205         struct pci_map *maps;
206
207         maps = uio_res->maps;
208         devname = uio_res->path;
209         pagesz = sysconf(_SC_PAGESIZE);
210
211         /* allocate memory to keep path */
212         maps[map_idx].path = rte_malloc(NULL, strlen(devname) + 1, 0);
213         if (maps[map_idx].path == NULL) {
214                 RTE_LOG(ERR, EAL, "Cannot allocate memory for path: %s\n",
215                                 strerror(errno));
216                 return -1;
217         }
218
219         /*
220          * open resource file, to mmap it
221          */
222         fd = open(devname, O_RDWR);
223         if (fd < 0) {
224                 RTE_LOG(ERR, EAL, "Cannot open %s: %s\n",
225                                 devname, strerror(errno));
226                 goto error;
227         }
228
229         /* if matching map is found, then use it */
230         offset = res_idx * pagesz;
231         mapaddr = pci_map_resource(NULL, fd, (off_t)offset,
232                         (size_t)dev->mem_resource[res_idx].len, 0);
233         close(fd);
234         if (mapaddr == MAP_FAILED)
235                 goto error;
236
237         maps[map_idx].phaddr = dev->mem_resource[res_idx].phys_addr;
238         maps[map_idx].size = dev->mem_resource[res_idx].len;
239         maps[map_idx].addr = mapaddr;
240         maps[map_idx].offset = offset;
241         strcpy(maps[map_idx].path, devname);
242         dev->mem_resource[res_idx].addr = mapaddr;
243
244         return 0;
245
246 error:
247         rte_free(maps[map_idx].path);
248         return -1;
249 }
250
251 static int
252 pci_scan_one(int dev_pci_fd, struct pci_conf *conf)
253 {
254         struct rte_pci_device *dev;
255         struct pci_bar_io bar;
256         unsigned i, max;
257
258         dev = malloc(sizeof(*dev));
259         if (dev == NULL) {
260                 return -1;
261         }
262
263         memset(dev, 0, sizeof(*dev));
264         dev->addr.domain = conf->pc_sel.pc_domain;
265         dev->addr.bus = conf->pc_sel.pc_bus;
266         dev->addr.devid = conf->pc_sel.pc_dev;
267         dev->addr.function = conf->pc_sel.pc_func;
268
269         /* get vendor id */
270         dev->id.vendor_id = conf->pc_vendor;
271
272         /* get device id */
273         dev->id.device_id = conf->pc_device;
274
275         /* get subsystem_vendor id */
276         dev->id.subsystem_vendor_id = conf->pc_subvendor;
277
278         /* get subsystem_device id */
279         dev->id.subsystem_device_id = conf->pc_subdevice;
280
281         /* get class id */
282         dev->id.class_id = (conf->pc_class << 16) |
283                            (conf->pc_subclass << 8) |
284                            (conf->pc_progif);
285
286         /* TODO: get max_vfs */
287         dev->max_vfs = 0;
288
289         /* FreeBSD has no NUMA support (yet) */
290         dev->device.numa_node = 0;
291
292         /* FreeBSD has only one pass through driver */
293         dev->kdrv = RTE_KDRV_NIC_UIO;
294
295         /* parse resources */
296         switch (conf->pc_hdr & PCIM_HDRTYPE) {
297         case PCIM_HDRTYPE_NORMAL:
298                 max = PCIR_MAX_BAR_0;
299                 break;
300         case PCIM_HDRTYPE_BRIDGE:
301                 max = PCIR_MAX_BAR_1;
302                 break;
303         case PCIM_HDRTYPE_CARDBUS:
304                 max = PCIR_MAX_BAR_2;
305                 break;
306         default:
307                 goto skipdev;
308         }
309
310         for (i = 0; i <= max; i++) {
311                 bar.pbi_sel = conf->pc_sel;
312                 bar.pbi_reg = PCIR_BAR(i);
313                 if (ioctl(dev_pci_fd, PCIOCGETBAR, &bar) < 0)
314                         continue;
315
316                 dev->mem_resource[i].len = bar.pbi_length;
317                 if (PCI_BAR_IO(bar.pbi_base)) {
318                         dev->mem_resource[i].addr = (void *)(bar.pbi_base & ~((uint64_t)0xf));
319                         continue;
320                 }
321                 dev->mem_resource[i].phys_addr = bar.pbi_base & ~((uint64_t)0xf);
322         }
323
324         /* device is valid, add in list (sorted) */
325         if (TAILQ_EMPTY(&pci_device_list)) {
326                 rte_eal_device_insert(&dev->device);
327                 TAILQ_INSERT_TAIL(&pci_device_list, dev, next);
328         }
329         else {
330                 struct rte_pci_device *dev2 = NULL;
331                 int ret;
332
333                 TAILQ_FOREACH(dev2, &pci_device_list, next) {
334                         ret = rte_eal_compare_pci_addr(&dev->addr, &dev2->addr);
335                         if (ret > 0)
336                                 continue;
337                         else if (ret < 0) {
338                                 TAILQ_INSERT_BEFORE(dev2, dev, next);
339                                 rte_eal_device_insert(&dev->device);
340                         } else { /* already registered */
341                                 dev2->kdrv = dev->kdrv;
342                                 dev2->max_vfs = dev->max_vfs;
343                                 memmove(dev2->mem_resource,
344                                         dev->mem_resource,
345                                         sizeof(dev->mem_resource));
346                                 free(dev);
347                         }
348                         return 0;
349                 }
350                 rte_eal_device_insert(&dev->device);
351                 TAILQ_INSERT_TAIL(&pci_device_list, dev, next);
352         }
353
354         return 0;
355
356 skipdev:
357         free(dev);
358         return 0;
359 }
360
361 /*
362  * Scan the content of the PCI bus, and add the devices in the devices
363  * list. Call pci_scan_one() for each pci entry found.
364  */
365 int
366 rte_eal_pci_scan(void)
367 {
368         int fd;
369         unsigned dev_count = 0;
370         struct pci_conf matches[16];
371         struct pci_conf_io conf_io = {
372                         .pat_buf_len = 0,
373                         .num_patterns = 0,
374                         .patterns = NULL,
375                         .match_buf_len = sizeof(matches),
376                         .matches = &matches[0],
377         };
378
379         fd = open("/dev/pci", O_RDONLY);
380         if (fd < 0) {
381                 RTE_LOG(ERR, EAL, "%s(): error opening /dev/pci\n", __func__);
382                 goto error;
383         }
384
385         do {
386                 unsigned i;
387                 if (ioctl(fd, PCIOCGETCONF, &conf_io) < 0) {
388                         RTE_LOG(ERR, EAL, "%s(): error with ioctl on /dev/pci: %s\n",
389                                         __func__, strerror(errno));
390                         goto error;
391                 }
392
393                 for (i = 0; i < conf_io.num_matches; i++)
394                         if (pci_scan_one(fd, &matches[i]) < 0)
395                                 goto error;
396
397                 dev_count += conf_io.num_matches;
398         } while(conf_io.status == PCI_GETCONF_MORE_DEVS);
399
400         close(fd);
401
402         RTE_LOG(ERR, EAL, "PCI scan found %u devices\n", dev_count);
403         return 0;
404
405 error:
406         if (fd >= 0)
407                 close(fd);
408         return -1;
409 }
410
411 int
412 pci_update_device(const struct rte_pci_addr *addr)
413 {
414         int fd;
415         struct pci_conf matches[2];
416         struct pci_match_conf match = {
417                 .pc_sel = {
418                         .pc_domain = addr->domain,
419                         .pc_bus = addr->bus,
420                         .pc_dev = addr->devid,
421                         .pc_func = addr->function,
422                 },
423         };
424         struct pci_conf_io conf_io = {
425                 .pat_buf_len = 0,
426                 .num_patterns = 1,
427                 .patterns = &match,
428                 .match_buf_len = sizeof(matches),
429                 .matches = &matches[0],
430         };
431
432         fd = open("/dev/pci", O_RDONLY);
433         if (fd < 0) {
434                 RTE_LOG(ERR, EAL, "%s(): error opening /dev/pci\n", __func__);
435                 goto error;
436         }
437
438         if (ioctl(fd, PCIOCGETCONF, &conf_io) < 0) {
439                 RTE_LOG(ERR, EAL, "%s(): error with ioctl on /dev/pci: %s\n",
440                                 __func__, strerror(errno));
441                 goto error;
442         }
443
444         if (conf_io.num_matches != 1)
445                 goto error;
446
447         if (pci_scan_one(fd, &matches[0]) < 0)
448                 goto error;
449
450         close(fd);
451
452         return 0;
453
454 error:
455         if (fd >= 0)
456                 close(fd);
457         return -1;
458 }
459
460 /* Read PCI config space. */
461 int rte_eal_pci_read_config(const struct rte_pci_device *dev,
462                             void *buf, size_t len, off_t offset)
463 {
464         int fd = -1;
465         struct pci_io pi = {
466                 .pi_sel = {
467                         .pc_domain = dev->addr.domain,
468                         .pc_bus = dev->addr.bus,
469                         .pc_dev = dev->addr.devid,
470                         .pc_func = dev->addr.function,
471                 },
472                 .pi_reg = offset,
473                 .pi_width = len,
474         };
475
476         if (len == 3 || len > sizeof(pi.pi_data)) {
477                 RTE_LOG(ERR, EAL, "%s(): invalid pci read length\n", __func__);
478                 goto error;
479         }
480
481         fd = open("/dev/pci", O_RDWR);
482         if (fd < 0) {
483                 RTE_LOG(ERR, EAL, "%s(): error opening /dev/pci\n", __func__);
484                 goto error;
485         }
486
487         if (ioctl(fd, PCIOCREAD, &pi) < 0)
488                 goto error;
489         close(fd);
490
491         memcpy(buf, &pi.pi_data, len);
492         return 0;
493
494  error:
495         if (fd >= 0)
496                 close(fd);
497         return -1;
498 }
499
500 /* Write PCI config space. */
501 int rte_eal_pci_write_config(const struct rte_pci_device *dev,
502                              const void *buf, size_t len, off_t offset)
503 {
504         int fd = -1;
505
506         struct pci_io pi = {
507                 .pi_sel = {
508                         .pc_domain = dev->addr.domain,
509                         .pc_bus = dev->addr.bus,
510                         .pc_dev = dev->addr.devid,
511                         .pc_func = dev->addr.function,
512                 },
513                 .pi_reg = offset,
514                 .pi_data = *(const uint32_t *)buf,
515                 .pi_width = len,
516         };
517
518         if (len == 3 || len > sizeof(pi.pi_data)) {
519                 RTE_LOG(ERR, EAL, "%s(): invalid pci read length\n", __func__);
520                 goto error;
521         }
522
523         memcpy(&pi.pi_data, buf, len);
524
525         fd = open("/dev/pci", O_RDWR);
526         if (fd < 0) {
527                 RTE_LOG(ERR, EAL, "%s(): error opening /dev/pci\n", __func__);
528                 goto error;
529         }
530
531         if (ioctl(fd, PCIOCWRITE, &pi) < 0)
532                 goto error;
533
534         close(fd);
535         return 0;
536
537  error:
538         if (fd >= 0)
539                 close(fd);
540         return -1;
541 }
542
543 int
544 rte_eal_pci_ioport_map(struct rte_pci_device *dev, int bar,
545                        struct rte_pci_ioport *p)
546 {
547         int ret;
548
549         switch (dev->kdrv) {
550 #if defined(RTE_ARCH_X86)
551         case RTE_KDRV_NIC_UIO:
552                 if ((uintptr_t) dev->mem_resource[bar].addr <= UINT16_MAX) {
553                         p->base = (uintptr_t)dev->mem_resource[bar].addr;
554                         ret = 0;
555                 } else
556                         ret = -1;
557                 break;
558 #endif
559         default:
560                 ret = -1;
561                 break;
562         }
563
564         if (!ret)
565                 p->dev = dev;
566
567         return ret;
568 }
569
570 static void
571 pci_uio_ioport_read(struct rte_pci_ioport *p,
572                     void *data, size_t len, off_t offset)
573 {
574 #if defined(RTE_ARCH_X86)
575         uint8_t *d;
576         int size;
577         unsigned short reg = p->base + offset;
578
579         for (d = data; len > 0; d += size, reg += size, len -= size) {
580                 if (len >= 4) {
581                         size = 4;
582                         *(uint32_t *)d = inl(reg);
583                 } else if (len >= 2) {
584                         size = 2;
585                         *(uint16_t *)d = inw(reg);
586                 } else {
587                         size = 1;
588                         *d = inb(reg);
589                 }
590         }
591 #else
592         RTE_SET_USED(p);
593         RTE_SET_USED(data);
594         RTE_SET_USED(len);
595         RTE_SET_USED(offset);
596 #endif
597 }
598
599 void
600 rte_eal_pci_ioport_read(struct rte_pci_ioport *p,
601                         void *data, size_t len, off_t offset)
602 {
603         switch (p->dev->kdrv) {
604         case RTE_KDRV_NIC_UIO:
605                 pci_uio_ioport_read(p, data, len, offset);
606                 break;
607         default:
608                 break;
609         }
610 }
611
612 static void
613 pci_uio_ioport_write(struct rte_pci_ioport *p,
614                      const void *data, size_t len, off_t offset)
615 {
616 #if defined(RTE_ARCH_X86)
617         const uint8_t *s;
618         int size;
619         unsigned short reg = p->base + offset;
620
621         for (s = data; len > 0; s += size, reg += size, len -= size) {
622                 if (len >= 4) {
623                         size = 4;
624                         outl(*(const uint32_t *)s, reg);
625                 } else if (len >= 2) {
626                         size = 2;
627                         outw(*(const uint16_t *)s, reg);
628                 } else {
629                         size = 1;
630                         outb(*s, reg);
631                 }
632         }
633 #else
634         RTE_SET_USED(p);
635         RTE_SET_USED(data);
636         RTE_SET_USED(len);
637         RTE_SET_USED(offset);
638 #endif
639 }
640
641 void
642 rte_eal_pci_ioport_write(struct rte_pci_ioport *p,
643                          const void *data, size_t len, off_t offset)
644 {
645         switch (p->dev->kdrv) {
646         case RTE_KDRV_NIC_UIO:
647                 pci_uio_ioport_write(p, data, len, offset);
648                 break;
649         default:
650                 break;
651         }
652 }
653
654 int
655 rte_eal_pci_ioport_unmap(struct rte_pci_ioport *p)
656 {
657         int ret;
658
659         switch (p->dev->kdrv) {
660 #if defined(RTE_ARCH_X86)
661         case RTE_KDRV_NIC_UIO:
662                 ret = 0;
663                 break;
664 #endif
665         default:
666                 ret = -1;
667                 break;
668         }
669
670         return ret;
671 }
672
673 /* Init the PCI EAL subsystem */
674 int
675 rte_eal_pci_init(void)
676 {
677         /* for debug purposes, PCI can be disabled */
678         if (internal_config.no_pci)
679                 return 0;
680
681         if (rte_eal_pci_scan() < 0) {
682                 RTE_LOG(ERR, EAL, "%s(): Cannot scan PCI bus\n", __func__);
683                 return -1;
684         }
685         return 0;
686 }