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