15566a3e3SBruce Richardson /* SPDX-License-Identifier: BSD-3-Clause
25566a3e3SBruce Richardson * Copyright(c) 2010-2014 Intel Corporation
3c752998bSGaetan Rivet */
4c752998bSGaetan Rivet
5c752998bSGaetan Rivet #include <ctype.h>
6c752998bSGaetan Rivet #include <stdio.h>
7c752998bSGaetan Rivet #include <stdlib.h>
8c752998bSGaetan Rivet #include <string.h>
9c752998bSGaetan Rivet #include <stdarg.h>
10c752998bSGaetan Rivet #include <unistd.h>
11c752998bSGaetan Rivet #include <inttypes.h>
12c752998bSGaetan Rivet #include <sys/types.h>
13c752998bSGaetan Rivet #include <sys/stat.h>
14c752998bSGaetan Rivet #include <fcntl.h>
15c752998bSGaetan Rivet #include <errno.h>
16c752998bSGaetan Rivet #include <dirent.h>
17c752998bSGaetan Rivet #include <limits.h>
18c752998bSGaetan Rivet #include <sys/queue.h>
19c752998bSGaetan Rivet #include <sys/mman.h>
20c752998bSGaetan Rivet #include <sys/ioctl.h>
21c752998bSGaetan Rivet #include <sys/pciio.h>
22c752998bSGaetan Rivet #include <dev/pci/pcireg.h>
23c752998bSGaetan Rivet
24c752998bSGaetan Rivet #if defined(RTE_ARCH_X86)
25c752998bSGaetan Rivet #include <machine/cpufunc.h>
26c752998bSGaetan Rivet #endif
27c752998bSGaetan Rivet
28c752998bSGaetan Rivet #include <rte_interrupts.h>
29c752998bSGaetan Rivet #include <rte_log.h>
30c752998bSGaetan Rivet #include <rte_pci.h>
31c752998bSGaetan Rivet #include <rte_common.h>
32c752998bSGaetan Rivet #include <rte_launch.h>
33c752998bSGaetan Rivet #include <rte_memory.h>
34c752998bSGaetan Rivet #include <rte_eal.h>
35c752998bSGaetan Rivet #include <rte_per_lcore.h>
36c752998bSGaetan Rivet #include <rte_lcore.h>
37c752998bSGaetan Rivet #include <rte_malloc.h>
38c752998bSGaetan Rivet #include <rte_string_fns.h>
39c752998bSGaetan Rivet #include <rte_debug.h>
40c752998bSGaetan Rivet #include <rte_devargs.h>
41c752998bSGaetan Rivet
42c752998bSGaetan Rivet #include "eal_filesystem.h"
43c752998bSGaetan Rivet #include "private.h"
44c752998bSGaetan Rivet
45c752998bSGaetan Rivet /**
46c752998bSGaetan Rivet * @file
47aa777f00SThomas Monjalon * PCI probing under BSD.
48c752998bSGaetan Rivet */
49c752998bSGaetan Rivet
50c752998bSGaetan Rivet /* Map pci device */
51c752998bSGaetan Rivet int
rte_pci_map_device(struct rte_pci_device * dev)52c752998bSGaetan Rivet rte_pci_map_device(struct rte_pci_device *dev)
53c752998bSGaetan Rivet {
54c752998bSGaetan Rivet int ret = -1;
55c752998bSGaetan Rivet
56c752998bSGaetan Rivet /* try mapping the NIC resources */
57c752998bSGaetan Rivet switch (dev->kdrv) {
587c0d798aSDavid Marchand case RTE_PCI_KDRV_NIC_UIO:
59c752998bSGaetan Rivet /* map resources for devices that use uio */
60c752998bSGaetan Rivet ret = pci_uio_map_resource(dev);
61c752998bSGaetan Rivet break;
62c752998bSGaetan Rivet default:
63*849f773bSDavid Marchand PCI_LOG(DEBUG, " Not managed by a supported kernel driver, skipped");
64c752998bSGaetan Rivet ret = 1;
65c752998bSGaetan Rivet break;
66c752998bSGaetan Rivet }
67c752998bSGaetan Rivet
68c752998bSGaetan Rivet return ret;
69c752998bSGaetan Rivet }
70c752998bSGaetan Rivet
71c752998bSGaetan Rivet /* Unmap pci device */
72c752998bSGaetan Rivet void
rte_pci_unmap_device(struct rte_pci_device * dev)73c752998bSGaetan Rivet rte_pci_unmap_device(struct rte_pci_device *dev)
74c752998bSGaetan Rivet {
75c752998bSGaetan Rivet /* try unmapping the NIC resources */
76c752998bSGaetan Rivet switch (dev->kdrv) {
777c0d798aSDavid Marchand case RTE_PCI_KDRV_NIC_UIO:
78c752998bSGaetan Rivet /* unmap resources for devices that use uio */
79c752998bSGaetan Rivet pci_uio_unmap_resource(dev);
80c752998bSGaetan Rivet break;
81c752998bSGaetan Rivet default:
82*849f773bSDavid Marchand PCI_LOG(DEBUG, " Not managed by a supported kernel driver, skipped");
83c752998bSGaetan Rivet break;
84c752998bSGaetan Rivet }
85c752998bSGaetan Rivet }
86c752998bSGaetan Rivet
87c752998bSGaetan Rivet void
pci_uio_free_resource(struct rte_pci_device * dev,struct mapped_pci_resource * uio_res)88c752998bSGaetan Rivet pci_uio_free_resource(struct rte_pci_device *dev,
89c752998bSGaetan Rivet struct mapped_pci_resource *uio_res)
90c752998bSGaetan Rivet {
91c752998bSGaetan Rivet rte_free(uio_res);
92c752998bSGaetan Rivet
93d61138d4SHarman Kalra if (rte_intr_fd_get(dev->intr_handle)) {
94d61138d4SHarman Kalra close(rte_intr_fd_get(dev->intr_handle));
95d61138d4SHarman Kalra rte_intr_fd_set(dev->intr_handle, -1);
96d61138d4SHarman Kalra rte_intr_type_set(dev->intr_handle, RTE_INTR_HANDLE_UNKNOWN);
97c752998bSGaetan Rivet }
98c752998bSGaetan Rivet }
99c752998bSGaetan Rivet
100c752998bSGaetan Rivet int
pci_uio_alloc_resource(struct rte_pci_device * dev,struct mapped_pci_resource ** uio_res)101c752998bSGaetan Rivet pci_uio_alloc_resource(struct rte_pci_device *dev,
102c752998bSGaetan Rivet struct mapped_pci_resource **uio_res)
103c752998bSGaetan Rivet {
104c752998bSGaetan Rivet char devname[PATH_MAX]; /* contains the /dev/uioX */
105c752998bSGaetan Rivet struct rte_pci_addr *loc;
106c752998bSGaetan Rivet
107c752998bSGaetan Rivet loc = &dev->addr;
108c752998bSGaetan Rivet
109c752998bSGaetan Rivet snprintf(devname, sizeof(devname), "/dev/uio@pci:%u:%u:%u",
110c752998bSGaetan Rivet dev->addr.bus, dev->addr.devid, dev->addr.function);
111c752998bSGaetan Rivet
112c752998bSGaetan Rivet if (access(devname, O_RDWR) < 0) {
113*849f773bSDavid Marchand PCI_LOG(WARNING, " "PCI_PRI_FMT" not managed by UIO driver, skipping",
114*849f773bSDavid Marchand loc->domain, loc->bus, loc->devid, loc->function);
115c752998bSGaetan Rivet return 1;
116c752998bSGaetan Rivet }
117c752998bSGaetan Rivet
118c752998bSGaetan Rivet /* save fd if in primary process */
119d61138d4SHarman Kalra if (rte_intr_fd_set(dev->intr_handle, open(devname, O_RDWR))) {
120*849f773bSDavid Marchand PCI_LOG(WARNING, "Failed to save fd");
121d61138d4SHarman Kalra goto error;
122d61138d4SHarman Kalra }
123d61138d4SHarman Kalra
124d61138d4SHarman Kalra if (rte_intr_fd_get(dev->intr_handle) < 0) {
125*849f773bSDavid Marchand PCI_LOG(ERR, "Cannot open %s: %s", devname, strerror(errno));
126c752998bSGaetan Rivet goto error;
127c752998bSGaetan Rivet }
128d61138d4SHarman Kalra
129d61138d4SHarman Kalra if (rte_intr_type_set(dev->intr_handle, RTE_INTR_HANDLE_UIO))
130d61138d4SHarman Kalra goto error;
131c752998bSGaetan Rivet
132c752998bSGaetan Rivet /* allocate the mapping details for secondary processes*/
133c752998bSGaetan Rivet *uio_res = rte_zmalloc("UIO_RES", sizeof(**uio_res), 0);
134c752998bSGaetan Rivet if (*uio_res == NULL) {
135*849f773bSDavid Marchand PCI_LOG(ERR, "%s(): cannot store uio mmap details", __func__);
136c752998bSGaetan Rivet goto error;
137c752998bSGaetan Rivet }
138c752998bSGaetan Rivet
139f9acaf84SBruce Richardson strlcpy((*uio_res)->path, devname, sizeof((*uio_res)->path));
140c752998bSGaetan Rivet memcpy(&(*uio_res)->pci_addr, &dev->addr, sizeof((*uio_res)->pci_addr));
141c752998bSGaetan Rivet
142c752998bSGaetan Rivet return 0;
143c752998bSGaetan Rivet
144c752998bSGaetan Rivet error:
145c752998bSGaetan Rivet pci_uio_free_resource(dev, *uio_res);
146c752998bSGaetan Rivet return -1;
147c752998bSGaetan Rivet }
148c752998bSGaetan Rivet
149c752998bSGaetan Rivet int
pci_uio_map_resource_by_index(struct rte_pci_device * dev,int res_idx,struct mapped_pci_resource * uio_res,int map_idx)150c752998bSGaetan Rivet pci_uio_map_resource_by_index(struct rte_pci_device *dev, int res_idx,
151c752998bSGaetan Rivet struct mapped_pci_resource *uio_res, int map_idx)
152c752998bSGaetan Rivet {
153c752998bSGaetan Rivet int fd;
154c752998bSGaetan Rivet char *devname;
155c752998bSGaetan Rivet void *mapaddr;
156c752998bSGaetan Rivet uint64_t offset;
157c752998bSGaetan Rivet uint64_t pagesz;
158c752998bSGaetan Rivet struct pci_map *maps;
159c752998bSGaetan Rivet
160c752998bSGaetan Rivet maps = uio_res->maps;
161c752998bSGaetan Rivet devname = uio_res->path;
162c752998bSGaetan Rivet pagesz = sysconf(_SC_PAGESIZE);
163c752998bSGaetan Rivet
164c752998bSGaetan Rivet /* allocate memory to keep path */
165c752998bSGaetan Rivet maps[map_idx].path = rte_malloc(NULL, strlen(devname) + 1, 0);
166c752998bSGaetan Rivet if (maps[map_idx].path == NULL) {
167*849f773bSDavid Marchand PCI_LOG(ERR, "Cannot allocate memory for path: %s", strerror(errno));
168c752998bSGaetan Rivet return -1;
169c752998bSGaetan Rivet }
170c752998bSGaetan Rivet
171c752998bSGaetan Rivet /*
172c752998bSGaetan Rivet * open resource file, to mmap it
173c752998bSGaetan Rivet */
174c752998bSGaetan Rivet fd = open(devname, O_RDWR);
175c752998bSGaetan Rivet if (fd < 0) {
176*849f773bSDavid Marchand PCI_LOG(ERR, "Cannot open %s: %s", devname, strerror(errno));
177c752998bSGaetan Rivet goto error;
178c752998bSGaetan Rivet }
179c752998bSGaetan Rivet
180c752998bSGaetan Rivet /* if matching map is found, then use it */
181c752998bSGaetan Rivet offset = res_idx * pagesz;
182c752998bSGaetan Rivet mapaddr = pci_map_resource(NULL, fd, (off_t)offset,
183c752998bSGaetan Rivet (size_t)dev->mem_resource[res_idx].len, 0);
184c752998bSGaetan Rivet close(fd);
185e200535cSDavid Marchand if (mapaddr == NULL)
186c752998bSGaetan Rivet goto error;
187c752998bSGaetan Rivet
188c752998bSGaetan Rivet maps[map_idx].phaddr = dev->mem_resource[res_idx].phys_addr;
189c752998bSGaetan Rivet maps[map_idx].size = dev->mem_resource[res_idx].len;
190c752998bSGaetan Rivet maps[map_idx].addr = mapaddr;
191c752998bSGaetan Rivet maps[map_idx].offset = offset;
192c752998bSGaetan Rivet strcpy(maps[map_idx].path, devname);
193c752998bSGaetan Rivet dev->mem_resource[res_idx].addr = mapaddr;
194c752998bSGaetan Rivet
195c752998bSGaetan Rivet return 0;
196c752998bSGaetan Rivet
197c752998bSGaetan Rivet error:
198c752998bSGaetan Rivet rte_free(maps[map_idx].path);
199c752998bSGaetan Rivet return -1;
200c752998bSGaetan Rivet }
201c752998bSGaetan Rivet
202c752998bSGaetan Rivet static int
pci_scan_one(int dev_pci_fd,struct pci_conf * conf)203c752998bSGaetan Rivet pci_scan_one(int dev_pci_fd, struct pci_conf *conf)
204c752998bSGaetan Rivet {
20587a02023SChenbo Xia struct rte_pci_device_internal *pdev;
206c752998bSGaetan Rivet struct rte_pci_device *dev;
207c752998bSGaetan Rivet struct pci_bar_io bar;
208c752998bSGaetan Rivet unsigned i, max;
209c752998bSGaetan Rivet
21087a02023SChenbo Xia pdev = malloc(sizeof(*pdev));
21187a02023SChenbo Xia if (pdev == NULL) {
212*849f773bSDavid Marchand PCI_LOG(ERR, "Cannot allocate memory for internal pci device");
213c752998bSGaetan Rivet return -1;
214c752998bSGaetan Rivet }
215c752998bSGaetan Rivet
21687a02023SChenbo Xia memset(pdev, 0, sizeof(*pdev));
21787a02023SChenbo Xia dev = &pdev->device;
2186844d146SThomas Monjalon dev->device.bus = &rte_pci_bus.bus;
2196844d146SThomas Monjalon
220c752998bSGaetan Rivet dev->addr.domain = conf->pc_sel.pc_domain;
221c752998bSGaetan Rivet dev->addr.bus = conf->pc_sel.pc_bus;
222c752998bSGaetan Rivet dev->addr.devid = conf->pc_sel.pc_dev;
223c752998bSGaetan Rivet dev->addr.function = conf->pc_sel.pc_func;
224c752998bSGaetan Rivet
225c752998bSGaetan Rivet /* get vendor id */
226c752998bSGaetan Rivet dev->id.vendor_id = conf->pc_vendor;
227c752998bSGaetan Rivet
228c752998bSGaetan Rivet /* get device id */
229c752998bSGaetan Rivet dev->id.device_id = conf->pc_device;
230c752998bSGaetan Rivet
231c752998bSGaetan Rivet /* get subsystem_vendor id */
232c752998bSGaetan Rivet dev->id.subsystem_vendor_id = conf->pc_subvendor;
233c752998bSGaetan Rivet
234c752998bSGaetan Rivet /* get subsystem_device id */
235c752998bSGaetan Rivet dev->id.subsystem_device_id = conf->pc_subdevice;
236c752998bSGaetan Rivet
237c752998bSGaetan Rivet /* get class id */
238c752998bSGaetan Rivet dev->id.class_id = (conf->pc_class << 16) |
239c752998bSGaetan Rivet (conf->pc_subclass << 8) |
240c752998bSGaetan Rivet (conf->pc_progif);
241c752998bSGaetan Rivet
242c752998bSGaetan Rivet /* TODO: get max_vfs */
243c752998bSGaetan Rivet dev->max_vfs = 0;
244c752998bSGaetan Rivet
245c752998bSGaetan Rivet /* FreeBSD has no NUMA support (yet) */
2467dcd73e3SOlivier Matz dev->device.numa_node = SOCKET_ID_ANY;
247c752998bSGaetan Rivet
2488f4de2dbSDavid Marchand pci_common_set(dev);
249c752998bSGaetan Rivet
250c752998bSGaetan Rivet /* FreeBSD has only one pass through driver */
2517c0d798aSDavid Marchand dev->kdrv = RTE_PCI_KDRV_NIC_UIO;
252c752998bSGaetan Rivet
253c752998bSGaetan Rivet /* parse resources */
254c752998bSGaetan Rivet switch (conf->pc_hdr & PCIM_HDRTYPE) {
255c752998bSGaetan Rivet case PCIM_HDRTYPE_NORMAL:
256c752998bSGaetan Rivet max = PCIR_MAX_BAR_0;
257c752998bSGaetan Rivet break;
258c752998bSGaetan Rivet case PCIM_HDRTYPE_BRIDGE:
259c752998bSGaetan Rivet max = PCIR_MAX_BAR_1;
260c752998bSGaetan Rivet break;
261c752998bSGaetan Rivet case PCIM_HDRTYPE_CARDBUS:
262c752998bSGaetan Rivet max = PCIR_MAX_BAR_2;
263c752998bSGaetan Rivet break;
264c752998bSGaetan Rivet default:
265c752998bSGaetan Rivet goto skipdev;
266c752998bSGaetan Rivet }
267c752998bSGaetan Rivet
268c752998bSGaetan Rivet for (i = 0; i <= max; i++) {
269c752998bSGaetan Rivet bar.pbi_sel = conf->pc_sel;
270c752998bSGaetan Rivet bar.pbi_reg = PCIR_BAR(i);
271c752998bSGaetan Rivet if (ioctl(dev_pci_fd, PCIOCGETBAR, &bar) < 0)
272c752998bSGaetan Rivet continue;
273c752998bSGaetan Rivet
274c752998bSGaetan Rivet dev->mem_resource[i].len = bar.pbi_length;
275c752998bSGaetan Rivet if (PCI_BAR_IO(bar.pbi_base)) {
276c752998bSGaetan Rivet dev->mem_resource[i].addr = (void *)(bar.pbi_base & ~((uint64_t)0xf));
277c752998bSGaetan Rivet continue;
278c752998bSGaetan Rivet }
279c752998bSGaetan Rivet dev->mem_resource[i].phys_addr = bar.pbi_base & ~((uint64_t)0xf);
280c752998bSGaetan Rivet }
281c752998bSGaetan Rivet
282c752998bSGaetan Rivet /* device is valid, add in list (sorted) */
283c752998bSGaetan Rivet if (TAILQ_EMPTY(&rte_pci_bus.device_list)) {
284c752998bSGaetan Rivet rte_pci_add_device(dev);
285c752998bSGaetan Rivet }
286c752998bSGaetan Rivet else {
287c752998bSGaetan Rivet struct rte_pci_device *dev2 = NULL;
288c752998bSGaetan Rivet int ret;
289c752998bSGaetan Rivet
290c752998bSGaetan Rivet TAILQ_FOREACH(dev2, &rte_pci_bus.device_list, next) {
2910e3ef055SGaetan Rivet ret = rte_pci_addr_cmp(&dev->addr, &dev2->addr);
292c752998bSGaetan Rivet if (ret > 0)
293c752998bSGaetan Rivet continue;
294c752998bSGaetan Rivet else if (ret < 0) {
295c752998bSGaetan Rivet rte_pci_insert_device(dev2, dev);
296c752998bSGaetan Rivet } else { /* already registered */
297c752998bSGaetan Rivet dev2->kdrv = dev->kdrv;
298c752998bSGaetan Rivet dev2->max_vfs = dev->max_vfs;
2998f4de2dbSDavid Marchand pci_common_set(dev2);
300c752998bSGaetan Rivet memmove(dev2->mem_resource,
301c752998bSGaetan Rivet dev->mem_resource,
302c752998bSGaetan Rivet sizeof(dev->mem_resource));
30387a02023SChenbo Xia pci_free(pdev);
304c752998bSGaetan Rivet }
305c752998bSGaetan Rivet return 0;
306c752998bSGaetan Rivet }
307c752998bSGaetan Rivet rte_pci_add_device(dev);
308c752998bSGaetan Rivet }
309c752998bSGaetan Rivet
310c752998bSGaetan Rivet return 0;
311c752998bSGaetan Rivet
312c752998bSGaetan Rivet skipdev:
31387a02023SChenbo Xia pci_free(pdev);
314c752998bSGaetan Rivet return 0;
315c752998bSGaetan Rivet }
316c752998bSGaetan Rivet
317c752998bSGaetan Rivet /*
318c752998bSGaetan Rivet * Scan the content of the PCI bus, and add the devices in the devices
319c752998bSGaetan Rivet * list. Call pci_scan_one() for each pci entry found.
320c752998bSGaetan Rivet */
321c752998bSGaetan Rivet int
rte_pci_scan(void)322c752998bSGaetan Rivet rte_pci_scan(void)
323c752998bSGaetan Rivet {
324c752998bSGaetan Rivet int fd;
325c752998bSGaetan Rivet unsigned dev_count = 0;
326c752998bSGaetan Rivet struct pci_conf matches[16];
327c752998bSGaetan Rivet struct pci_conf_io conf_io = {
328c752998bSGaetan Rivet .pat_buf_len = 0,
329c752998bSGaetan Rivet .num_patterns = 0,
330c752998bSGaetan Rivet .patterns = NULL,
331c752998bSGaetan Rivet .match_buf_len = sizeof(matches),
332c752998bSGaetan Rivet .matches = &matches[0],
333c752998bSGaetan Rivet };
334463a5245SSunil Kumar Kori struct rte_pci_addr pci_addr;
335c752998bSGaetan Rivet
336c752998bSGaetan Rivet /* for debug purposes, PCI can be disabled */
337c752998bSGaetan Rivet if (!rte_eal_has_pci())
338c752998bSGaetan Rivet return 0;
339c752998bSGaetan Rivet
340c752998bSGaetan Rivet fd = open("/dev/pci", O_RDONLY);
341c752998bSGaetan Rivet if (fd < 0) {
342*849f773bSDavid Marchand PCI_LOG(ERR, "%s(): error opening /dev/pci", __func__);
343c752998bSGaetan Rivet goto error;
344c752998bSGaetan Rivet }
345c752998bSGaetan Rivet
346c752998bSGaetan Rivet do {
347c752998bSGaetan Rivet unsigned i;
348c752998bSGaetan Rivet if (ioctl(fd, PCIOCGETCONF, &conf_io) < 0) {
349*849f773bSDavid Marchand PCI_LOG(ERR, "%s(): error with ioctl on /dev/pci: %s",
350c752998bSGaetan Rivet __func__, strerror(errno));
351c752998bSGaetan Rivet goto error;
352c752998bSGaetan Rivet }
353c752998bSGaetan Rivet
354463a5245SSunil Kumar Kori for (i = 0; i < conf_io.num_matches; i++) {
355463a5245SSunil Kumar Kori pci_addr.domain = matches[i].pc_sel.pc_domain;
356463a5245SSunil Kumar Kori pci_addr.bus = matches[i].pc_sel.pc_bus;
357463a5245SSunil Kumar Kori pci_addr.devid = matches[i].pc_sel.pc_dev;
358463a5245SSunil Kumar Kori pci_addr.function = matches[i].pc_sel.pc_func;
359463a5245SSunil Kumar Kori
360463a5245SSunil Kumar Kori if (rte_pci_ignore_device(&pci_addr))
361463a5245SSunil Kumar Kori continue;
362463a5245SSunil Kumar Kori
363c752998bSGaetan Rivet if (pci_scan_one(fd, &matches[i]) < 0)
364c752998bSGaetan Rivet goto error;
365463a5245SSunil Kumar Kori }
366c752998bSGaetan Rivet
367c752998bSGaetan Rivet dev_count += conf_io.num_matches;
368c752998bSGaetan Rivet } while(conf_io.status == PCI_GETCONF_MORE_DEVS);
369c752998bSGaetan Rivet
370c752998bSGaetan Rivet close(fd);
371c752998bSGaetan Rivet
372*849f773bSDavid Marchand PCI_LOG(DEBUG, "PCI scan found %u devices", dev_count);
373c752998bSGaetan Rivet return 0;
374c752998bSGaetan Rivet
375c752998bSGaetan Rivet error:
376c752998bSGaetan Rivet if (fd >= 0)
377c752998bSGaetan Rivet close(fd);
378c752998bSGaetan Rivet return -1;
379c752998bSGaetan Rivet }
380c752998bSGaetan Rivet
38166d3724bSDavid Marchand bool
pci_device_iommu_support_va(__rte_unused const struct rte_pci_device * dev)38266d3724bSDavid Marchand pci_device_iommu_support_va(__rte_unused const struct rte_pci_device *dev)
38366d3724bSDavid Marchand {
38466d3724bSDavid Marchand return false;
38566d3724bSDavid Marchand }
38666d3724bSDavid Marchand
387c752998bSGaetan Rivet enum rte_iova_mode
pci_device_iova_mode(const struct rte_pci_driver * pdrv __rte_unused,const struct rte_pci_device * pdev)388703458e1SBen Walker pci_device_iova_mode(const struct rte_pci_driver *pdrv __rte_unused,
389703458e1SBen Walker const struct rte_pci_device *pdev)
390c752998bSGaetan Rivet {
3917c0d798aSDavid Marchand if (pdev->kdrv != RTE_PCI_KDRV_NIC_UIO)
392*849f773bSDavid Marchand PCI_LOG(DEBUG, "Unsupported kernel driver? Defaulting to IOVA as 'PA'");
393703458e1SBen Walker
394c752998bSGaetan Rivet return RTE_IOVA_PA;
395c752998bSGaetan Rivet }
396c752998bSGaetan Rivet
397c752998bSGaetan Rivet /* Read PCI config space. */
rte_pci_read_config(const struct rte_pci_device * dev,void * buf,size_t len,off_t offset)398c752998bSGaetan Rivet int rte_pci_read_config(const struct rte_pci_device *dev,
399c752998bSGaetan Rivet void *buf, size_t len, off_t offset)
400c752998bSGaetan Rivet {
401c752998bSGaetan Rivet int fd = -1;
402c752998bSGaetan Rivet int size;
403e8d435f1SLuca Boccassi /* Copy Linux implementation's behaviour */
404e8d435f1SLuca Boccassi const int return_len = len;
405c752998bSGaetan Rivet struct pci_io pi = {
406c752998bSGaetan Rivet .pi_sel = {
407c752998bSGaetan Rivet .pc_domain = dev->addr.domain,
408c752998bSGaetan Rivet .pc_bus = dev->addr.bus,
409c752998bSGaetan Rivet .pc_dev = dev->addr.devid,
410c752998bSGaetan Rivet .pc_func = dev->addr.function,
411c752998bSGaetan Rivet },
412c752998bSGaetan Rivet .pi_reg = offset,
413c752998bSGaetan Rivet };
414c752998bSGaetan Rivet
415c752998bSGaetan Rivet fd = open("/dev/pci", O_RDWR);
416c752998bSGaetan Rivet if (fd < 0) {
417*849f773bSDavid Marchand PCI_LOG(ERR, "%s(): error opening /dev/pci", __func__);
418c752998bSGaetan Rivet goto error;
419c752998bSGaetan Rivet }
420c752998bSGaetan Rivet
421c752998bSGaetan Rivet while (len > 0) {
422c752998bSGaetan Rivet size = (len >= 4) ? 4 : ((len >= 2) ? 2 : 1);
423c752998bSGaetan Rivet pi.pi_width = size;
424c752998bSGaetan Rivet
425c752998bSGaetan Rivet if (ioctl(fd, PCIOCREAD, &pi) < 0)
426c752998bSGaetan Rivet goto error;
427c752998bSGaetan Rivet memcpy(buf, &pi.pi_data, size);
428c752998bSGaetan Rivet
429c752998bSGaetan Rivet buf = (char *)buf + size;
430c752998bSGaetan Rivet pi.pi_reg += size;
431c752998bSGaetan Rivet len -= size;
432c752998bSGaetan Rivet }
433c752998bSGaetan Rivet close(fd);
434c752998bSGaetan Rivet
435e8d435f1SLuca Boccassi return return_len;
436c752998bSGaetan Rivet
437c752998bSGaetan Rivet error:
438c752998bSGaetan Rivet if (fd >= 0)
439c752998bSGaetan Rivet close(fd);
440c752998bSGaetan Rivet return -1;
441c752998bSGaetan Rivet }
442c752998bSGaetan Rivet
443c752998bSGaetan Rivet /* Write PCI config space. */
rte_pci_write_config(const struct rte_pci_device * dev,const void * buf,size_t len,off_t offset)444c752998bSGaetan Rivet int rte_pci_write_config(const struct rte_pci_device *dev,
445c752998bSGaetan Rivet const void *buf, size_t len, off_t offset)
446c752998bSGaetan Rivet {
447c752998bSGaetan Rivet int fd = -1;
448c752998bSGaetan Rivet
449c752998bSGaetan Rivet struct pci_io pi = {
450c752998bSGaetan Rivet .pi_sel = {
451c752998bSGaetan Rivet .pc_domain = dev->addr.domain,
452c752998bSGaetan Rivet .pc_bus = dev->addr.bus,
453c752998bSGaetan Rivet .pc_dev = dev->addr.devid,
454c752998bSGaetan Rivet .pc_func = dev->addr.function,
455c752998bSGaetan Rivet },
456c752998bSGaetan Rivet .pi_reg = offset,
457c752998bSGaetan Rivet .pi_data = *(const uint32_t *)buf,
458c752998bSGaetan Rivet .pi_width = len,
459c752998bSGaetan Rivet };
460c752998bSGaetan Rivet
461c752998bSGaetan Rivet if (len == 3 || len > sizeof(pi.pi_data)) {
462*849f773bSDavid Marchand PCI_LOG(ERR, "%s(): invalid pci read length", __func__);
463c752998bSGaetan Rivet goto error;
464c752998bSGaetan Rivet }
465c752998bSGaetan Rivet
466c752998bSGaetan Rivet memcpy(&pi.pi_data, buf, len);
467c752998bSGaetan Rivet
468c752998bSGaetan Rivet fd = open("/dev/pci", O_RDWR);
469c752998bSGaetan Rivet if (fd < 0) {
470*849f773bSDavid Marchand PCI_LOG(ERR, "%s(): error opening /dev/pci", __func__);
471c752998bSGaetan Rivet goto error;
472c752998bSGaetan Rivet }
473c752998bSGaetan Rivet
474c752998bSGaetan Rivet if (ioctl(fd, PCIOCWRITE, &pi) < 0)
475c752998bSGaetan Rivet goto error;
476c752998bSGaetan Rivet
477c752998bSGaetan Rivet close(fd);
478c752998bSGaetan Rivet return 0;
479c752998bSGaetan Rivet
480c752998bSGaetan Rivet error:
481c752998bSGaetan Rivet if (fd >= 0)
482c752998bSGaetan Rivet close(fd);
483c752998bSGaetan Rivet return -1;
484c752998bSGaetan Rivet }
485c752998bSGaetan Rivet
486095cf6e6SChenbo Xia /* Read PCI MMIO space. */
rte_pci_mmio_read(const struct rte_pci_device * dev,int bar,void * buf,size_t len,off_t offset)487095cf6e6SChenbo Xia int rte_pci_mmio_read(const struct rte_pci_device *dev, int bar,
488095cf6e6SChenbo Xia void *buf, size_t len, off_t offset)
489095cf6e6SChenbo Xia {
490095cf6e6SChenbo Xia if (bar >= PCI_MAX_RESOURCE || dev->mem_resource[bar].addr == NULL ||
491095cf6e6SChenbo Xia (uint64_t)offset + len > dev->mem_resource[bar].len)
492095cf6e6SChenbo Xia return -1;
493095cf6e6SChenbo Xia memcpy(buf, (uint8_t *)dev->mem_resource[bar].addr + offset, len);
494095cf6e6SChenbo Xia return len;
495095cf6e6SChenbo Xia }
496095cf6e6SChenbo Xia
497095cf6e6SChenbo Xia /* Write PCI MMIO space. */
rte_pci_mmio_write(const struct rte_pci_device * dev,int bar,const void * buf,size_t len,off_t offset)498095cf6e6SChenbo Xia int rte_pci_mmio_write(const struct rte_pci_device *dev, int bar,
499095cf6e6SChenbo Xia const void *buf, size_t len, off_t offset)
500095cf6e6SChenbo Xia {
501095cf6e6SChenbo Xia if (bar >= PCI_MAX_RESOURCE || dev->mem_resource[bar].addr == NULL ||
502095cf6e6SChenbo Xia (uint64_t)offset + len > dev->mem_resource[bar].len)
503095cf6e6SChenbo Xia return -1;
504095cf6e6SChenbo Xia memcpy((uint8_t *)dev->mem_resource[bar].addr + offset, buf, len);
505095cf6e6SChenbo Xia return len;
506095cf6e6SChenbo Xia }
507095cf6e6SChenbo Xia
508c752998bSGaetan Rivet int
rte_pci_ioport_map(struct rte_pci_device * dev,int bar,struct rte_pci_ioport * p)509c752998bSGaetan Rivet rte_pci_ioport_map(struct rte_pci_device *dev, int bar,
510c752998bSGaetan Rivet struct rte_pci_ioport *p)
511c752998bSGaetan Rivet {
512c752998bSGaetan Rivet int ret;
513c752998bSGaetan Rivet
514c752998bSGaetan Rivet switch (dev->kdrv) {
515c752998bSGaetan Rivet #if defined(RTE_ARCH_X86)
5167c0d798aSDavid Marchand case RTE_PCI_KDRV_NIC_UIO:
517e02b661bSDavid Marchand if (rte_eal_iopl_init() != 0) {
518*849f773bSDavid Marchand PCI_LOG(ERR, "%s(): insufficient ioport permissions for PCI device %s",
519e02b661bSDavid Marchand __func__, dev->name);
520e02b661bSDavid Marchand return -1;
521e02b661bSDavid Marchand }
522c752998bSGaetan Rivet if ((uintptr_t) dev->mem_resource[bar].addr <= UINT16_MAX) {
523c752998bSGaetan Rivet p->base = (uintptr_t)dev->mem_resource[bar].addr;
524c752998bSGaetan Rivet ret = 0;
525c752998bSGaetan Rivet } else
526c752998bSGaetan Rivet ret = -1;
527c752998bSGaetan Rivet break;
528c752998bSGaetan Rivet #endif
529c752998bSGaetan Rivet default:
530c752998bSGaetan Rivet ret = -1;
531c752998bSGaetan Rivet break;
532c752998bSGaetan Rivet }
533c752998bSGaetan Rivet
534c752998bSGaetan Rivet if (!ret)
535c752998bSGaetan Rivet p->dev = dev;
536c752998bSGaetan Rivet
537c752998bSGaetan Rivet return ret;
538c752998bSGaetan Rivet }
539c752998bSGaetan Rivet
540c752998bSGaetan Rivet static void
pci_uio_ioport_read(struct rte_pci_ioport * p,void * data,size_t len,off_t offset)541c752998bSGaetan Rivet pci_uio_ioport_read(struct rte_pci_ioport *p,
542c752998bSGaetan Rivet void *data, size_t len, off_t offset)
543c752998bSGaetan Rivet {
544c752998bSGaetan Rivet #if defined(RTE_ARCH_X86)
545c752998bSGaetan Rivet uint8_t *d;
546c752998bSGaetan Rivet int size;
547c752998bSGaetan Rivet unsigned short reg = p->base + offset;
548c752998bSGaetan Rivet
549c752998bSGaetan Rivet for (d = data; len > 0; d += size, reg += size, len -= size) {
550c752998bSGaetan Rivet if (len >= 4) {
551c752998bSGaetan Rivet size = 4;
552c752998bSGaetan Rivet *(uint32_t *)d = inl(reg);
553c752998bSGaetan Rivet } else if (len >= 2) {
554c752998bSGaetan Rivet size = 2;
555c752998bSGaetan Rivet *(uint16_t *)d = inw(reg);
556c752998bSGaetan Rivet } else {
557c752998bSGaetan Rivet size = 1;
558c752998bSGaetan Rivet *d = inb(reg);
559c752998bSGaetan Rivet }
560c752998bSGaetan Rivet }
561c752998bSGaetan Rivet #else
562c752998bSGaetan Rivet RTE_SET_USED(p);
563c752998bSGaetan Rivet RTE_SET_USED(data);
564c752998bSGaetan Rivet RTE_SET_USED(len);
565c752998bSGaetan Rivet RTE_SET_USED(offset);
566c752998bSGaetan Rivet #endif
567c752998bSGaetan Rivet }
568c752998bSGaetan Rivet
569c752998bSGaetan Rivet void
rte_pci_ioport_read(struct rte_pci_ioport * p,void * data,size_t len,off_t offset)570c752998bSGaetan Rivet rte_pci_ioport_read(struct rte_pci_ioport *p,
571c752998bSGaetan Rivet void *data, size_t len, off_t offset)
572c752998bSGaetan Rivet {
573c752998bSGaetan Rivet switch (p->dev->kdrv) {
5747c0d798aSDavid Marchand case RTE_PCI_KDRV_NIC_UIO:
575c752998bSGaetan Rivet pci_uio_ioport_read(p, data, len, offset);
576c752998bSGaetan Rivet break;
577c752998bSGaetan Rivet default:
578c752998bSGaetan Rivet break;
579c752998bSGaetan Rivet }
580c752998bSGaetan Rivet }
581c752998bSGaetan Rivet
582c752998bSGaetan Rivet static void
pci_uio_ioport_write(struct rte_pci_ioport * p,const void * data,size_t len,off_t offset)583c752998bSGaetan Rivet pci_uio_ioport_write(struct rte_pci_ioport *p,
584c752998bSGaetan Rivet const void *data, size_t len, off_t offset)
585c752998bSGaetan Rivet {
586c752998bSGaetan Rivet #if defined(RTE_ARCH_X86)
587c752998bSGaetan Rivet const uint8_t *s;
588c752998bSGaetan Rivet int size;
589c752998bSGaetan Rivet unsigned short reg = p->base + offset;
590c752998bSGaetan Rivet
591c752998bSGaetan Rivet for (s = data; len > 0; s += size, reg += size, len -= size) {
592c752998bSGaetan Rivet if (len >= 4) {
593c752998bSGaetan Rivet size = 4;
594c752998bSGaetan Rivet outl(reg, *(const uint32_t *)s);
595c752998bSGaetan Rivet } else if (len >= 2) {
596c752998bSGaetan Rivet size = 2;
597c752998bSGaetan Rivet outw(reg, *(const uint16_t *)s);
598c752998bSGaetan Rivet } else {
599c752998bSGaetan Rivet size = 1;
600c752998bSGaetan Rivet outb(reg, *s);
601c752998bSGaetan Rivet }
602c752998bSGaetan Rivet }
603c752998bSGaetan Rivet #else
604c752998bSGaetan Rivet RTE_SET_USED(p);
605c752998bSGaetan Rivet RTE_SET_USED(data);
606c752998bSGaetan Rivet RTE_SET_USED(len);
607c752998bSGaetan Rivet RTE_SET_USED(offset);
608c752998bSGaetan Rivet #endif
609c752998bSGaetan Rivet }
610c752998bSGaetan Rivet
611c752998bSGaetan Rivet void
rte_pci_ioport_write(struct rte_pci_ioport * p,const void * data,size_t len,off_t offset)612c752998bSGaetan Rivet rte_pci_ioport_write(struct rte_pci_ioport *p,
613c752998bSGaetan Rivet const void *data, size_t len, off_t offset)
614c752998bSGaetan Rivet {
615c752998bSGaetan Rivet switch (p->dev->kdrv) {
6167c0d798aSDavid Marchand case RTE_PCI_KDRV_NIC_UIO:
617c752998bSGaetan Rivet pci_uio_ioport_write(p, data, len, offset);
618c752998bSGaetan Rivet break;
619c752998bSGaetan Rivet default:
620c752998bSGaetan Rivet break;
621c752998bSGaetan Rivet }
622c752998bSGaetan Rivet }
623c752998bSGaetan Rivet
624c752998bSGaetan Rivet int
rte_pci_ioport_unmap(struct rte_pci_ioport * p)625c752998bSGaetan Rivet rte_pci_ioport_unmap(struct rte_pci_ioport *p)
626c752998bSGaetan Rivet {
627c752998bSGaetan Rivet int ret;
628c752998bSGaetan Rivet
629c752998bSGaetan Rivet switch (p->dev->kdrv) {
630c752998bSGaetan Rivet #if defined(RTE_ARCH_X86)
6317c0d798aSDavid Marchand case RTE_PCI_KDRV_NIC_UIO:
632c752998bSGaetan Rivet ret = 0;
633c752998bSGaetan Rivet break;
634c752998bSGaetan Rivet #endif
635c752998bSGaetan Rivet default:
636c752998bSGaetan Rivet ret = -1;
637c752998bSGaetan Rivet break;
638c752998bSGaetan Rivet }
639c752998bSGaetan Rivet
640c752998bSGaetan Rivet return ret;
641c752998bSGaetan Rivet }
642