Branch data Line data Source code
1 : : /* SPDX-License-Identifier: BSD-3-Clause
2 : : * Copyright 2021,2024-2025 NXP
3 : : */
4 : :
5 : : #include <stdbool.h>
6 : : #include <stdint.h>
7 : : #include <stdio.h>
8 : : #include <unistd.h>
9 : : #include <stdlib.h>
10 : : #include <dirent.h>
11 : : #include <string.h>
12 : : #include <sys/mman.h>
13 : : #include <errno.h>
14 : : #include <fcntl.h>
15 : :
16 : : #include <rte_common.h>
17 : : #include <rte_malloc.h>
18 : : #include "enet_pmd_logs.h"
19 : : #include "enet_uio.h"
20 : :
21 : : static struct uio_job enetfec_uio_job;
22 : : static int enetfec_count;
23 : :
24 : : /** @brief Checks if a file name contains a certain substring.
25 : : * This function assumes a filename format of: [text][number].
26 : : * @param [in] filename File name
27 : : * @param [in] match String to match in file name
28 : : *
29 : : * @retval true if file name matches the criteria
30 : : * @retval false if file name does not match the criteria
31 : : */
32 : : static bool
33 : : file_name_match_extract(const char filename[], const char match[])
34 : : {
35 : 0 : return strstr(filename, match) != NULL;
36 : : }
37 : :
38 : : /*
39 : : * @brief Reads first line from a file.
40 : : * Composes file name as: root/subdir/filename
41 : : *
42 : : * @param [in] root Root path
43 : : * @param [in] subdir Subdirectory name
44 : : * @param [in] filename File name
45 : : * @param [out] line The first line read from file.
46 : : *
47 : : * @retval 0 for success
48 : : * @retval other value for error
49 : : */
50 : : static int
51 : 0 : file_read_first_line(const char root[], const char subdir[],
52 : : const char filename[], char *line)
53 : : {
54 : : char absolute_file_name[FEC_UIO_MAX_ATTR_FILE_NAME];
55 : : int fd = 0, ret = 0;
56 : :
57 : : /*compose the file name: root/subdir/filename */
58 : : memset(absolute_file_name, 0, sizeof(absolute_file_name));
59 : : snprintf(absolute_file_name, FEC_UIO_MAX_ATTR_FILE_NAME,
60 : : "%s/%s/%s", root, subdir, filename);
61 : :
62 : : fd = open(absolute_file_name, O_RDONLY);
63 [ # # ]: 0 : if (fd < 0) {
64 : 0 : ENETFEC_PMD_ERR("Error opening file %s", absolute_file_name);
65 : 0 : return fd;
66 : : }
67 : :
68 : : /* read UIO device name from first line in file */
69 : 0 : ret = read(fd, line, FEC_UIO_MAX_DEVICE_FILE_NAME_LENGTH);
70 [ # # ]: 0 : if (ret <= 0) {
71 : 0 : ENETFEC_PMD_ERR("Error reading file %s", absolute_file_name);
72 : 0 : close(fd);
73 : 0 : return ret;
74 : : }
75 : 0 : close(fd);
76 : :
77 : : /* NULL-ify string */
78 : 0 : line[ret] = '\0';
79 : :
80 : 0 : return 0;
81 : : }
82 : :
83 : : /*
84 : : * @brief Maps rx-tx bd range assigned for a bd ring.
85 : : *
86 : : * @param [in] uio_device_fd UIO device file descriptor
87 : : * @param [in] uio_device_id UIO device id
88 : : * @param [in] uio_map_id UIO allows maximum 5 different mapping for
89 : : each device. Maps start with id 0.
90 : : * @param [out] map_size Map size.
91 : : * @param [out] map_addr Map physical address
92 : : *
93 : : * @retval NULL if failed to map registers
94 : : * @retval Virtual address for mapped register address range
95 : : */
96 : : static void *
97 : 0 : uio_map_mem(int uio_device_fd, int uio_device_id,
98 : : int uio_map_id, int *map_size, uint64_t *map_addr)
99 : : {
100 : : void *mapped_address = NULL;
101 : : unsigned int uio_map_size = 0;
102 : : unsigned int uio_map_p_addr = 0;
103 : : char uio_sys_root[FEC_UIO_MAX_ATTR_FILE_NAME];
104 : : char uio_sys_map_subdir[FEC_UIO_MAX_ATTR_FILE_NAME];
105 : : char uio_map_size_str[FEC_UIO_MAX_DEVICE_FILE_NAME_LENGTH + 1];
106 : : char uio_map_p_addr_str[32];
107 : : int ret = 0;
108 : :
109 : : /* compose the file name: root/subdir/filename */
110 : : memset(uio_sys_root, 0, sizeof(uio_sys_root));
111 : : memset(uio_sys_map_subdir, 0, sizeof(uio_sys_map_subdir));
112 : : memset(uio_map_size_str, 0, sizeof(uio_map_size_str));
113 : : memset(uio_map_p_addr_str, 0, sizeof(uio_map_p_addr_str));
114 : :
115 : : /* Compose string: /sys/class/uio/uioX */
116 : : snprintf(uio_sys_root, sizeof(uio_sys_root), "%s/%s%d",
117 : : FEC_UIO_DEVICE_SYS_ATTR_PATH, "uio", uio_device_id);
118 : : /* Compose string: maps/mapY */
119 : : snprintf(uio_sys_map_subdir, sizeof(uio_sys_map_subdir), "%s%d",
120 : : FEC_UIO_DEVICE_SYS_MAP_ATTR, uio_map_id);
121 : :
122 : : /* Read first (and only) line from file
123 : : * /sys/class/uio/uioX/maps/mapY/size
124 : : */
125 : 0 : ret = file_read_first_line(uio_sys_root, uio_sys_map_subdir,
126 : : "size", uio_map_size_str);
127 [ # # ]: 0 : if (ret < 0) {
128 : 0 : ENETFEC_PMD_ERR("file_read_first_line() failed");
129 : 0 : return NULL;
130 : : }
131 : 0 : ret = file_read_first_line(uio_sys_root, uio_sys_map_subdir,
132 : : "addr", uio_map_p_addr_str);
133 [ # # ]: 0 : if (ret < 0) {
134 : 0 : ENETFEC_PMD_ERR("file_read_first_line() failed");
135 : 0 : return NULL;
136 : : }
137 : : /* Read mapping size and physical address expressed in hexa(base 16) */
138 : 0 : uio_map_size = strtol(uio_map_size_str, NULL, 16);
139 [ # # ]: 0 : if (uio_map_size <= 0 || uio_map_size > INT_MAX) {
140 : 0 : ENETFEC_PMD_ERR("Invalid mapping size: %u.", uio_map_size);
141 : 0 : return NULL;
142 : : }
143 : 0 : uio_map_p_addr = strtol(uio_map_p_addr_str, NULL, 16);
144 : :
145 [ # # ]: 0 : if (uio_map_id == 0) {
146 : : /* Map the register address in user space when map_id is 0 */
147 : 0 : mapped_address = mmap(0 /*dynamically choose virtual address */,
148 : : uio_map_size, PROT_READ | PROT_WRITE,
149 : : MAP_SHARED, uio_device_fd, 0);
150 : : } else {
151 : : /* Map the BD memory in user space */
152 : 0 : mapped_address = mmap(NULL, uio_map_size,
153 : : PROT_READ | PROT_WRITE,
154 : : MAP_SHARED, uio_device_fd, (1 * MAP_PAGE_SIZE));
155 : : }
156 : :
157 [ # # ]: 0 : if (mapped_address == MAP_FAILED) {
158 : 0 : ENETFEC_PMD_ERR("Failed to map! errno = %d uio job fd = %d,"
159 : : "uio device id = %d, uio map id = %d", errno,
160 : : uio_device_fd, uio_device_id, uio_map_id);
161 : 0 : return NULL;
162 : : }
163 : :
164 : : /* Save the map size to use it later on for munmap-ing */
165 : 0 : *map_size = uio_map_size;
166 : 0 : *map_addr = uio_map_p_addr;
167 : 0 : ENETFEC_PMD_INFO("UIO dev[%d] mapped region [id =%d] size 0x%x at %p",
168 : : uio_device_id, uio_map_id, uio_map_size, mapped_address);
169 : :
170 : 0 : return mapped_address;
171 : : }
172 : :
173 : : int
174 : 0 : config_enetfec_uio(struct enetfec_private *fep)
175 : : {
176 : : char uio_device_file_name[32];
177 : : struct uio_job *uio_job = NULL;
178 : :
179 : : /* Mapping is done only one time */
180 [ # # ]: 0 : if (enetfec_count > 0) {
181 : 0 : ENETFEC_PMD_INFO("Mapped!");
182 : 0 : return 0;
183 : : }
184 : :
185 : : uio_job = &enetfec_uio_job;
186 : :
187 : : /* Find UIO device created by ENETFEC-UIO kernel driver */
188 : : memset(uio_device_file_name, 0, sizeof(uio_device_file_name));
189 : 0 : snprintf(uio_device_file_name, sizeof(uio_device_file_name), "%s%d",
190 : : FEC_UIO_DEVICE_FILE_NAME, uio_job->uio_minor_number);
191 : :
192 : : /* Open device file */
193 : 0 : uio_job->uio_fd = open(uio_device_file_name, O_RDWR);
194 [ # # ]: 0 : if (uio_job->uio_fd < 0) {
195 : 0 : ENETFEC_PMD_WARN("Unable to open ENETFEC_UIO file");
196 : 0 : return -1;
197 : : }
198 : :
199 : 0 : ENETFEC_PMD_INFO("US_UIO: Open device(%s) file with uio_fd = %d",
200 : : uio_device_file_name, uio_job->uio_fd);
201 : :
202 : 0 : fep->hw_baseaddr_v = uio_map_mem(uio_job->uio_fd,
203 : : uio_job->uio_minor_number, FEC_UIO_REG_MAP_ID,
204 : : &uio_job->map_size, &uio_job->map_addr);
205 [ # # ]: 0 : if (fep->hw_baseaddr_v == NULL)
206 : : return -ENOMEM;
207 : 0 : fep->hw_baseaddr_p = uio_job->map_addr;
208 : 0 : fep->reg_size = uio_job->map_size;
209 : :
210 : 0 : fep->bd_addr_v = uio_map_mem(uio_job->uio_fd,
211 : : uio_job->uio_minor_number, FEC_UIO_BD_MAP_ID,
212 : : &uio_job->map_size, &uio_job->map_addr);
213 [ # # ]: 0 : if (fep->hw_baseaddr_v == NULL)
214 : : return -ENOMEM;
215 : 0 : fep->bd_addr_p = (uint32_t)uio_job->map_addr;
216 : 0 : fep->bd_size = uio_job->map_size;
217 : :
218 : 0 : enetfec_count++;
219 : :
220 : 0 : return 0;
221 : : }
222 : :
223 : : int
224 : 0 : enetfec_configure(void)
225 : : {
226 : : char uio_name[32];
227 : 0 : int uio_minor_number = -1;
228 : : int ret;
229 : : DIR *d = NULL;
230 : : struct dirent *dir;
231 : :
232 : 0 : d = opendir(FEC_UIO_DEVICE_SYS_ATTR_PATH);
233 [ # # ]: 0 : if (d == NULL) {
234 : 0 : ENETFEC_PMD_ERR("Error opening directory '%s': %s",
235 : : FEC_UIO_DEVICE_SYS_ATTR_PATH, strerror(errno));
236 : 0 : return -1;
237 : : }
238 : :
239 : : /* Iterate through all subdirs */
240 [ # # ]: 0 : while ((dir = readdir(d)) != NULL) {
241 [ # # ]: 0 : if (!strncmp(dir->d_name, ".", 1) ||
242 [ # # ]: 0 : !strncmp(dir->d_name, "..", 2))
243 : 0 : continue;
244 : :
245 [ # # ]: 0 : if (file_name_match_extract(dir->d_name, "uio")) {
246 : : /*
247 : : * As substring <uio> was found in <d_name>
248 : : * read number following <uio> substring in <d_name>
249 : : */
250 : 0 : ret = sscanf(dir->d_name + strlen("uio"), "%d",
251 : : &uio_minor_number);
252 [ # # ]: 0 : if (ret < 0)
253 : 0 : ENETFEC_PMD_ERR("Error: not find minor number");
254 : : /*
255 : : * Open file uioX/name and read first line which
256 : : * contains the name for the device. Based on the
257 : : * name check if this UIO device is for enetfec.
258 : : */
259 : : memset(uio_name, 0, sizeof(uio_name));
260 : 0 : ret = file_read_first_line(FEC_UIO_DEVICE_SYS_ATTR_PATH,
261 : : dir->d_name, "name", uio_name);
262 [ # # ]: 0 : if (ret != 0) {
263 : 0 : ENETFEC_PMD_INFO("file_read_first_line failed");
264 : 0 : closedir(d);
265 : 0 : return -1;
266 : : }
267 : :
268 [ # # ]: 0 : if (file_name_match_extract(uio_name,
269 : : FEC_UIO_DEVICE_NAME)) {
270 : 0 : enetfec_uio_job.uio_minor_number =
271 : : uio_minor_number;
272 : 0 : ENETFEC_PMD_INFO("enetfec device uio name: %s",
273 : : uio_name);
274 : : }
275 : : }
276 : : }
277 : 0 : closedir(d);
278 : 0 : return 0;
279 : : }
280 : :
281 : : void
282 : 0 : enetfec_cleanup(struct enetfec_private *fep)
283 : : {
284 : 0 : munmap(fep->hw_baseaddr_v, fep->cbus_size);
285 : 0 : }
|