1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2010-2014 Intel Corporation
12 #include <sys/types.h>
18 #include <sys/queue.h>
20 #include <sys/ioctl.h>
21 #include <sys/pciio.h>
22 #include <dev/pci/pcireg.h>
24 #if defined(RTE_ARCH_X86)
25 #include <machine/cpufunc.h>
28 #include <rte_interrupts.h>
31 #include <rte_bus_pci.h>
32 #include <rte_common.h>
33 #include <rte_launch.h>
34 #include <rte_memory.h>
36 #include <rte_per_lcore.h>
37 #include <rte_lcore.h>
38 #include <rte_malloc.h>
39 #include <rte_string_fns.h>
40 #include <rte_debug.h>
41 #include <rte_devargs.h>
43 #include "eal_filesystem.h"
48 * PCI probing under BSD.
51 extern struct rte_pci_bus rte_pci_bus;
55 rte_pci_map_device(struct rte_pci_device *dev)
59 /* try mapping the NIC resources */
61 case RTE_PCI_KDRV_NIC_UIO:
62 /* map resources for devices that use uio */
63 ret = pci_uio_map_resource(dev);
67 " Not managed by a supported kernel driver, skipped\n");
75 /* Unmap pci device */
77 rte_pci_unmap_device(struct rte_pci_device *dev)
79 /* try unmapping the NIC resources */
81 case RTE_PCI_KDRV_NIC_UIO:
82 /* unmap resources for devices that use uio */
83 pci_uio_unmap_resource(dev);
87 " Not managed by a supported kernel driver, skipped\n");
93 pci_uio_free_resource(struct rte_pci_device *dev,
94 struct mapped_pci_resource *uio_res)
98 if (dev->intr_handle.fd) {
99 close(dev->intr_handle.fd);
100 dev->intr_handle.fd = -1;
101 dev->intr_handle.type = RTE_INTR_HANDLE_UNKNOWN;
106 pci_uio_alloc_resource(struct rte_pci_device *dev,
107 struct mapped_pci_resource **uio_res)
109 char devname[PATH_MAX]; /* contains the /dev/uioX */
110 struct rte_pci_addr *loc;
114 snprintf(devname, sizeof(devname), "/dev/uio@pci:%u:%u:%u",
115 dev->addr.bus, dev->addr.devid, dev->addr.function);
117 if (access(devname, O_RDWR) < 0) {
118 RTE_LOG(WARNING, EAL, " "PCI_PRI_FMT" not managed by UIO driver, "
119 "skipping\n", loc->domain, loc->bus, loc->devid, loc->function);
123 /* save fd if in primary process */
124 dev->intr_handle.fd = open(devname, O_RDWR);
125 if (dev->intr_handle.fd < 0) {
126 RTE_LOG(ERR, EAL, "Cannot open %s: %s\n",
127 devname, strerror(errno));
130 dev->intr_handle.type = RTE_INTR_HANDLE_UIO;
132 /* allocate the mapping details for secondary processes*/
133 *uio_res = rte_zmalloc("UIO_RES", sizeof(**uio_res), 0);
134 if (*uio_res == NULL) {
136 "%s(): cannot store uio mmap details\n", __func__);
140 strlcpy((*uio_res)->path, devname, sizeof((*uio_res)->path));
141 memcpy(&(*uio_res)->pci_addr, &dev->addr, sizeof((*uio_res)->pci_addr));
146 pci_uio_free_resource(dev, *uio_res);
151 pci_uio_map_resource_by_index(struct rte_pci_device *dev, int res_idx,
152 struct mapped_pci_resource *uio_res, int map_idx)
159 struct pci_map *maps;
161 maps = uio_res->maps;
162 devname = uio_res->path;
163 pagesz = sysconf(_SC_PAGESIZE);
165 /* allocate memory to keep path */
166 maps[map_idx].path = rte_malloc(NULL, strlen(devname) + 1, 0);
167 if (maps[map_idx].path == NULL) {
168 RTE_LOG(ERR, EAL, "Cannot allocate memory for path: %s\n",
174 * open resource file, to mmap it
176 fd = open(devname, O_RDWR);
178 RTE_LOG(ERR, EAL, "Cannot open %s: %s\n",
179 devname, strerror(errno));
183 /* if matching map is found, then use it */
184 offset = res_idx * pagesz;
185 mapaddr = pci_map_resource(NULL, fd, (off_t)offset,
186 (size_t)dev->mem_resource[res_idx].len, 0);
191 maps[map_idx].phaddr = dev->mem_resource[res_idx].phys_addr;
192 maps[map_idx].size = dev->mem_resource[res_idx].len;
193 maps[map_idx].addr = mapaddr;
194 maps[map_idx].offset = offset;
195 strcpy(maps[map_idx].path, devname);
196 dev->mem_resource[res_idx].addr = mapaddr;
201 rte_free(maps[map_idx].path);
206 pci_scan_one(int dev_pci_fd, struct pci_conf *conf)
208 struct rte_pci_device *dev;
209 struct pci_bar_io bar;
212 dev = malloc(sizeof(*dev));
217 memset(dev, 0, sizeof(*dev));
218 dev->device.bus = &rte_pci_bus.bus;
220 dev->addr.domain = conf->pc_sel.pc_domain;
221 dev->addr.bus = conf->pc_sel.pc_bus;
222 dev->addr.devid = conf->pc_sel.pc_dev;
223 dev->addr.function = conf->pc_sel.pc_func;
226 dev->id.vendor_id = conf->pc_vendor;
229 dev->id.device_id = conf->pc_device;
231 /* get subsystem_vendor id */
232 dev->id.subsystem_vendor_id = conf->pc_subvendor;
234 /* get subsystem_device id */
235 dev->id.subsystem_device_id = conf->pc_subdevice;
238 dev->id.class_id = (conf->pc_class << 16) |
239 (conf->pc_subclass << 8) |
242 /* TODO: get max_vfs */
245 /* FreeBSD has no NUMA support (yet) */
246 dev->device.numa_node = 0;
250 /* FreeBSD has only one pass through driver */
251 dev->kdrv = RTE_PCI_KDRV_NIC_UIO;
253 /* parse resources */
254 switch (conf->pc_hdr & PCIM_HDRTYPE) {
255 case PCIM_HDRTYPE_NORMAL:
256 max = PCIR_MAX_BAR_0;
258 case PCIM_HDRTYPE_BRIDGE:
259 max = PCIR_MAX_BAR_1;
261 case PCIM_HDRTYPE_CARDBUS:
262 max = PCIR_MAX_BAR_2;
268 for (i = 0; i <= max; i++) {
269 bar.pbi_sel = conf->pc_sel;
270 bar.pbi_reg = PCIR_BAR(i);
271 if (ioctl(dev_pci_fd, PCIOCGETBAR, &bar) < 0)
274 dev->mem_resource[i].len = bar.pbi_length;
275 if (PCI_BAR_IO(bar.pbi_base)) {
276 dev->mem_resource[i].addr = (void *)(bar.pbi_base & ~((uint64_t)0xf));
279 dev->mem_resource[i].phys_addr = bar.pbi_base & ~((uint64_t)0xf);
282 /* device is valid, add in list (sorted) */
283 if (TAILQ_EMPTY(&rte_pci_bus.device_list)) {
284 rte_pci_add_device(dev);
287 struct rte_pci_device *dev2 = NULL;
290 TAILQ_FOREACH(dev2, &rte_pci_bus.device_list, next) {
291 ret = rte_pci_addr_cmp(&dev->addr, &dev2->addr);
295 rte_pci_insert_device(dev2, dev);
296 } else { /* already registered */
297 dev2->kdrv = dev->kdrv;
298 dev2->max_vfs = dev->max_vfs;
300 memmove(dev2->mem_resource,
302 sizeof(dev->mem_resource));
307 rte_pci_add_device(dev);
318 * Scan the content of the PCI bus, and add the devices in the devices
319 * list. Call pci_scan_one() for each pci entry found.
325 unsigned dev_count = 0;
326 struct pci_conf matches[16];
327 struct pci_conf_io conf_io = {
331 .match_buf_len = sizeof(matches),
332 .matches = &matches[0],
334 struct rte_pci_addr pci_addr;
336 /* for debug purposes, PCI can be disabled */
337 if (!rte_eal_has_pci())
340 fd = open("/dev/pci", O_RDONLY);
342 RTE_LOG(ERR, EAL, "%s(): error opening /dev/pci\n", __func__);
348 if (ioctl(fd, PCIOCGETCONF, &conf_io) < 0) {
349 RTE_LOG(ERR, EAL, "%s(): error with ioctl on /dev/pci: %s\n",
350 __func__, strerror(errno));
354 for (i = 0; i < conf_io.num_matches; i++) {
355 pci_addr.domain = matches[i].pc_sel.pc_domain;
356 pci_addr.bus = matches[i].pc_sel.pc_bus;
357 pci_addr.devid = matches[i].pc_sel.pc_dev;
358 pci_addr.function = matches[i].pc_sel.pc_func;
360 if (rte_pci_ignore_device(&pci_addr))
363 if (pci_scan_one(fd, &matches[i]) < 0)
367 dev_count += conf_io.num_matches;
368 } while(conf_io.status == PCI_GETCONF_MORE_DEVS);
372 RTE_LOG(DEBUG, EAL, "PCI scan found %u devices\n", dev_count);
382 pci_device_iommu_support_va(__rte_unused const struct rte_pci_device *dev)
388 pci_device_iova_mode(const struct rte_pci_driver *pdrv __rte_unused,
389 const struct rte_pci_device *pdev)
391 if (pdev->kdrv != RTE_PCI_KDRV_NIC_UIO)
392 RTE_LOG(DEBUG, EAL, "Unsupported kernel driver? Defaulting to IOVA as 'PA'\n");
397 /* Read PCI config space. */
398 int rte_pci_read_config(const struct rte_pci_device *dev,
399 void *buf, size_t len, off_t offset)
403 /* Copy Linux implementation's behaviour */
404 const int return_len = len;
407 .pc_domain = dev->addr.domain,
408 .pc_bus = dev->addr.bus,
409 .pc_dev = dev->addr.devid,
410 .pc_func = dev->addr.function,
415 fd = open("/dev/pci", O_RDWR);
417 RTE_LOG(ERR, EAL, "%s(): error opening /dev/pci\n", __func__);
422 size = (len >= 4) ? 4 : ((len >= 2) ? 2 : 1);
425 if (ioctl(fd, PCIOCREAD, &pi) < 0)
427 memcpy(buf, &pi.pi_data, size);
429 buf = (char *)buf + size;
443 /* Write PCI config space. */
444 int rte_pci_write_config(const struct rte_pci_device *dev,
445 const void *buf, size_t len, off_t offset)
451 .pc_domain = dev->addr.domain,
452 .pc_bus = dev->addr.bus,
453 .pc_dev = dev->addr.devid,
454 .pc_func = dev->addr.function,
457 .pi_data = *(const uint32_t *)buf,
461 if (len == 3 || len > sizeof(pi.pi_data)) {
462 RTE_LOG(ERR, EAL, "%s(): invalid pci read length\n", __func__);
466 memcpy(&pi.pi_data, buf, len);
468 fd = open("/dev/pci", O_RDWR);
470 RTE_LOG(ERR, EAL, "%s(): error opening /dev/pci\n", __func__);
474 if (ioctl(fd, PCIOCWRITE, &pi) < 0)
487 rte_pci_ioport_map(struct rte_pci_device *dev, int bar,
488 struct rte_pci_ioport *p)
493 #if defined(RTE_ARCH_X86)
494 case RTE_PCI_KDRV_NIC_UIO:
495 if (rte_eal_iopl_init() != 0) {
496 RTE_LOG(ERR, EAL, "%s(): insufficient ioport permissions for PCI device %s\n",
497 __func__, dev->name);
500 if ((uintptr_t) dev->mem_resource[bar].addr <= UINT16_MAX) {
501 p->base = (uintptr_t)dev->mem_resource[bar].addr;
519 pci_uio_ioport_read(struct rte_pci_ioport *p,
520 void *data, size_t len, off_t offset)
522 #if defined(RTE_ARCH_X86)
525 unsigned short reg = p->base + offset;
527 for (d = data; len > 0; d += size, reg += size, len -= size) {
530 *(uint32_t *)d = inl(reg);
531 } else if (len >= 2) {
533 *(uint16_t *)d = inw(reg);
543 RTE_SET_USED(offset);
548 rte_pci_ioport_read(struct rte_pci_ioport *p,
549 void *data, size_t len, off_t offset)
551 switch (p->dev->kdrv) {
552 case RTE_PCI_KDRV_NIC_UIO:
553 pci_uio_ioport_read(p, data, len, offset);
561 pci_uio_ioport_write(struct rte_pci_ioport *p,
562 const void *data, size_t len, off_t offset)
564 #if defined(RTE_ARCH_X86)
567 unsigned short reg = p->base + offset;
569 for (s = data; len > 0; s += size, reg += size, len -= size) {
572 outl(reg, *(const uint32_t *)s);
573 } else if (len >= 2) {
575 outw(reg, *(const uint16_t *)s);
585 RTE_SET_USED(offset);
590 rte_pci_ioport_write(struct rte_pci_ioport *p,
591 const void *data, size_t len, off_t offset)
593 switch (p->dev->kdrv) {
594 case RTE_PCI_KDRV_NIC_UIO:
595 pci_uio_ioport_write(p, data, len, offset);
603 rte_pci_ioport_unmap(struct rte_pci_ioport *p)
607 switch (p->dev->kdrv) {
608 #if defined(RTE_ARCH_X86)
609 case RTE_PCI_KDRV_NIC_UIO: