1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2018 Advanced Micro Devices, Inc. All rights reserved.
11 #include <rte_string_fns.h>
15 static const char * const uio_module_names[] = {
21 ccp_check_pci_uio_module(void)
27 fp = fopen(PROC_MODULES, "r");
31 while (uio_module_names[i] != NULL) {
32 while (fgets(buf, sizeof(buf), fp) != NULL) {
33 if (!strncmp(buf, uio_module_names[i],
34 strlen(uio_module_names[i]))) {
43 printf("Insert igb_uio or uio_pci_generic kernel module(s)");
44 return -1;/* uio not inserted */
48 * split up a pci address into its constituent parts.
51 ccp_parse_pci_addr_format(const char *buf, int bufsize, uint16_t *domain,
52 uint8_t *bus, uint8_t *devid, uint8_t *function)
54 /* first split on ':' */
62 char *str[PCI_FMT_NVAL];
63 /* last element-separator is "." not ":" */
66 char *buf_copy = strndup(buf, bufsize);
71 if (rte_strsplit(buf_copy, bufsize, splitaddr.str, PCI_FMT_NVAL, ':')
74 /* final split is on '.' between devid and function */
75 splitaddr.function = strchr(splitaddr.devid, '.');
76 if (splitaddr.function == NULL)
78 *splitaddr.function++ = '\0';
80 /* now convert to int values */
82 *domain = (uint8_t)strtoul(splitaddr.domain, NULL, 16);
83 *bus = (uint8_t)strtoul(splitaddr.bus, NULL, 16);
84 *devid = (uint8_t)strtoul(splitaddr.devid, NULL, 16);
85 *function = (uint8_t)strtoul(splitaddr.function, NULL, 10);
89 free(buf_copy); /* free the copy made with strdup */
97 ccp_pci_parse_sysfs_value(const char *filename, unsigned long *val)
103 f = fopen(filename, "r");
106 if (fgets(buf, sizeof(buf), f) == NULL) {
110 *val = strtoul(buf, &end, 0);
111 if ((buf[0] == '\0') || (end == NULL) || (*end != '\n')) {
119 /** IO resource type: */
120 #define IORESOURCE_IO 0x00000100
121 #define IORESOURCE_MEM 0x00000200
123 /* parse one line of the "resource" sysfs file (note that the 'line'
124 * string is modified)
127 ccp_pci_parse_one_sysfs_resource(char *line, size_t len, uint64_t *phys_addr,
128 uint64_t *end_addr, uint64_t *flags)
130 union pci_resource_info {
136 char *ptrs[PCI_RESOURCE_FMT_NVAL];
139 if (rte_strsplit(line, len, res_info.ptrs, 3, ' ') != 3)
142 *phys_addr = strtoull(res_info.phys_addr, NULL, 16);
143 *end_addr = strtoull(res_info.end_addr, NULL, 16);
144 *flags = strtoull(res_info.flags, NULL, 16);
151 /* parse the "resource" sysfs file */
153 ccp_pci_parse_sysfs_resource(const char *filename, struct rte_pci_device *dev)
158 uint64_t phys_addr, end_addr, flags;
160 fp = fopen(filename, "r");
164 for (i = 0; i < PCI_MAX_RESOURCE; i++) {
165 if (fgets(buf, sizeof(buf), fp) == NULL)
167 if (ccp_pci_parse_one_sysfs_resource(buf, sizeof(buf),
168 &phys_addr, &end_addr, &flags) < 0)
171 if (flags & IORESOURCE_MEM) {
172 dev->mem_resource[i].phys_addr = phys_addr;
173 dev->mem_resource[i].len = end_addr - phys_addr + 1;
174 /* not mapped for now */
175 dev->mem_resource[i].addr = NULL;
187 ccp_find_uio_devname(const char *dirname)
192 char dirname_uio[PATH_MAX];
193 unsigned int uio_num;
196 /* depending on kernel version, uio can be located in uio/uioX
199 snprintf(dirname_uio, sizeof(dirname_uio), "%s/uio", dirname);
200 dir = opendir(dirname_uio);
202 /* retry with the parent directory might be different kernel version*/
203 dir = opendir(dirname);
208 /* take the first file starting with "uio" */
209 while ((e = readdir(dir)) != NULL) {
210 /* format could be uio%d ...*/
211 int shortprefix_len = sizeof("uio") - 1;
212 /* ... or uio:uio%d */
213 int longprefix_len = sizeof("uio:uio") - 1;
216 if (strncmp(e->d_name, "uio", 3) != 0)
219 /* first try uio%d */
221 uio_num = strtoull(e->d_name + shortprefix_len, &endptr, 10);
222 if (errno == 0 && endptr != (e->d_name + shortprefix_len)) {
227 /* then try uio:uio%d */
229 uio_num = strtoull(e->d_name + longprefix_len, &endptr, 10);
230 if (errno == 0 && endptr != (e->d_name + longprefix_len)) {