Branch data Line data Source code
1 : : /* SPDX-License-Identifier: BSD-3-Clause
2 : : * Copyright 2018-2024 Advanced Micro Devices, Inc.
3 : : */
4 : :
5 : : #include <stdio.h>
6 : : #include <stdint.h>
7 : : #include <string.h>
8 : : #include <unistd.h>
9 : : #include <stdarg.h>
10 : : #include <inttypes.h>
11 : : #include <sys/mman.h>
12 : : #include <sys/stat.h>
13 : : #include <dirent.h>
14 : : #include <stdlib.h>
15 : : #include <fcntl.h>
16 : : #include <stdbool.h>
17 : :
18 : : #include <eal_export.h>
19 : : #include <rte_common.h>
20 : : #include <rte_eal.h>
21 : : #include <rte_string_fns.h>
22 : :
23 : : #include "ionic_common.h"
24 : :
25 : : #define IONIC_MDEV_UNK "mdev_unknown"
26 : : #define IONIC_MNIC "cpu_mnic"
27 : : #define IONIC_MCRYPT "cpu_mcrypt"
28 : :
29 : : #define IONIC_MAX_NAME_LEN 20
30 : : #define IONIC_MAX_MNETS 5
31 : : #define IONIC_MAX_MCPTS 1
32 : : #define IONIC_MAX_DEVICES (IONIC_MAX_MNETS + IONIC_MAX_MCPTS)
33 : : #define IONIC_MAX_U16_IDX 0xFFFF
34 : : #define IONIC_UIO_MAX_TRIES 32
35 : :
36 : : /*
37 : : * Note: the driver can assign any idx number
38 : : * in the range [0-IONIC_MAX_MDEV_SCAN)
39 : : */
40 : : #define IONIC_MAX_MDEV_SCAN 32
41 : :
42 : : struct ionic_map_tbl {
43 : : char dev_name[IONIC_MAX_NAME_LEN];
44 : : uint16_t dev_idx;
45 : : uint16_t uio_idx;
46 : : char mdev_name[IONIC_MAX_NAME_LEN];
47 : : };
48 : :
49 : : struct ionic_map_tbl ionic_mdev_map[IONIC_MAX_DEVICES] = {
50 : : { "net_ionic0", 0, IONIC_MAX_U16_IDX, IONIC_MDEV_UNK },
51 : : { "net_ionic1", 1, IONIC_MAX_U16_IDX, IONIC_MDEV_UNK },
52 : : { "net_ionic2", 2, IONIC_MAX_U16_IDX, IONIC_MDEV_UNK },
53 : : { "net_ionic3", 3, IONIC_MAX_U16_IDX, IONIC_MDEV_UNK },
54 : : { "net_ionic4", 4, IONIC_MAX_U16_IDX, IONIC_MDEV_UNK },
55 : : { "crypto_ionic0", 5, IONIC_MAX_U16_IDX, IONIC_MDEV_UNK },
56 : : };
57 : :
58 : : struct uio_name {
59 : : uint16_t idx;
60 : : char name[IONIC_MAX_NAME_LEN];
61 : : };
62 : :
63 : : static void
64 : 0 : uio_fill_name_cache(struct uio_name *name_cache, const char *pfx)
65 : : {
66 : : char file[64];
67 : : FILE *fp;
68 : : char *ret;
69 : : int name_idx = 0;
70 : : int i;
71 : :
72 : 0 : for (i = 0; i < IONIC_UIO_MAX_TRIES &&
73 [ # # ]: 0 : name_idx < IONIC_MAX_DEVICES; i++) {
74 : : sprintf(file, "/sys/class/uio/uio%d/name", i);
75 : :
76 : 0 : fp = fopen(file, "r");
77 [ # # ]: 0 : if (fp == NULL)
78 : 0 : continue;
79 : :
80 [ # # ]: 0 : ret = fgets(name_cache[name_idx].name, IONIC_MAX_NAME_LEN, fp);
81 [ # # ]: 0 : if (ret == NULL) {
82 : 0 : fclose(fp);
83 : 0 : continue;
84 : : }
85 : :
86 : 0 : name_cache[name_idx].idx = i;
87 : :
88 : 0 : fclose(fp);
89 : :
90 [ # # ]: 0 : if (strncmp(name_cache[name_idx].name, pfx, strlen(pfx)) == 0)
91 : 0 : name_idx++;
92 : : }
93 : 0 : }
94 : :
95 : : static int
96 : 0 : uio_get_idx_for_devname(struct uio_name *name_cache, char *devname)
97 : : {
98 : : int i;
99 : :
100 [ # # ]: 0 : for (i = 0; i < IONIC_MAX_DEVICES; i++)
101 [ # # ]: 0 : if (strncmp(name_cache[i].name, devname, strlen(devname)) == 0)
102 : 0 : return name_cache[i].idx;
103 : :
104 : : return -1;
105 : : }
106 : :
107 : : RTE_EXPORT_INTERNAL_SYMBOL(ionic_uio_scan_mnet_devices)
108 : : void
109 : 0 : ionic_uio_scan_mnet_devices(void)
110 : : {
111 : : struct ionic_map_tbl *map;
112 : : char devname[IONIC_MAX_NAME_LEN];
113 : : struct uio_name name_cache[IONIC_MAX_DEVICES];
114 : : bool done;
115 : : int mdev_idx = 0;
116 : : int uio_idx;
117 : : int i;
118 : : static bool scan_done;
119 : :
120 [ # # ]: 0 : if (scan_done)
121 : 0 : return;
122 : :
123 : 0 : scan_done = true;
124 : :
125 : 0 : uio_fill_name_cache(name_cache, IONIC_MNIC);
126 : :
127 [ # # ]: 0 : for (i = 0; i < IONIC_MAX_MNETS; i++) {
128 : : done = false;
129 : :
130 [ # # ]: 0 : while (!done) {
131 [ # # ]: 0 : if (mdev_idx > IONIC_MAX_MDEV_SCAN)
132 : : break;
133 : :
134 : : /* Look for a matching mnic */
135 : : snprintf(devname, IONIC_MAX_NAME_LEN,
136 : : IONIC_MNIC "%d", mdev_idx);
137 : 0 : uio_idx = uio_get_idx_for_devname(name_cache, devname);
138 [ # # ]: 0 : if (uio_idx >= 0) {
139 : : map = &ionic_mdev_map[i];
140 : 0 : map->uio_idx = (uint16_t)uio_idx;
141 : 0 : strlcpy(map->mdev_name, devname,
142 : : IONIC_MAX_NAME_LEN);
143 : : done = true;
144 : : }
145 : :
146 : 0 : mdev_idx++;
147 : : }
148 : : }
149 : : }
150 : :
151 : : RTE_EXPORT_INTERNAL_SYMBOL(ionic_uio_scan_mcrypt_devices)
152 : : void
153 : 0 : ionic_uio_scan_mcrypt_devices(void)
154 : : {
155 : : struct ionic_map_tbl *map;
156 : : char devname[IONIC_MAX_NAME_LEN];
157 : : struct uio_name name_cache[IONIC_MAX_DEVICES];
158 : : bool done;
159 : : int mdev_idx = 0;
160 : : int uio_idx;
161 : : int i;
162 : : static bool scan_done;
163 : :
164 [ # # ]: 0 : if (scan_done)
165 : 0 : return;
166 : :
167 : 0 : scan_done = true;
168 : :
169 : 0 : uio_fill_name_cache(name_cache, IONIC_MCRYPT);
170 : :
171 [ # # ]: 0 : for (i = IONIC_MAX_MNETS; i < IONIC_MAX_DEVICES; i++) {
172 : : done = false;
173 : :
174 [ # # ]: 0 : while (!done) {
175 [ # # ]: 0 : if (mdev_idx > IONIC_MAX_MDEV_SCAN)
176 : : break;
177 : :
178 : : /* Look for a matching mcrypt */
179 : : snprintf(devname, IONIC_MAX_NAME_LEN,
180 : : IONIC_MCRYPT "%d", mdev_idx);
181 : 0 : uio_idx = uio_get_idx_for_devname(name_cache, devname);
182 [ # # ]: 0 : if (uio_idx >= 0) {
183 : : map = &ionic_mdev_map[i];
184 : 0 : map->uio_idx = (uint16_t)uio_idx;
185 : : strlcpy(map->mdev_name, devname,
186 : : IONIC_MAX_NAME_LEN);
187 : : done = true;
188 : : }
189 : :
190 : 0 : mdev_idx++;
191 : : }
192 : : }
193 : : }
194 : :
195 : : static int
196 : 0 : uio_get_multi_dev_uionum(const char *name)
197 : : {
198 : : struct ionic_map_tbl *map;
199 : : int i;
200 : :
201 [ # # ]: 0 : for (i = 0; i < IONIC_MAX_DEVICES; i++) {
202 : : map = &ionic_mdev_map[i];
203 [ # # ]: 0 : if (strncmp(map->dev_name, name, IONIC_MAX_NAME_LEN) == 0) {
204 [ # # ]: 0 : if (map->uio_idx == IONIC_MAX_U16_IDX)
205 : : return -1;
206 : : else
207 : 0 : return map->uio_idx;
208 : : }
209 : : }
210 : :
211 : : return -1;
212 : : }
213 : :
214 : : static unsigned long
215 : 0 : uio_get_res_size(int uio_idx, int res_idx)
216 : : {
217 : : unsigned long size;
218 : : char file[64];
219 : : FILE *fp;
220 : :
221 : : sprintf(file, "/sys/class/uio/uio%d/maps/map%d/size",
222 : : uio_idx, res_idx);
223 : :
224 : 0 : fp = fopen(file, "r");
225 [ # # ]: 0 : if (fp == NULL)
226 : : return 0;
227 : :
228 [ # # ]: 0 : if (fscanf(fp, "0x%lx", &size) != 1)
229 : 0 : size = 0;
230 : :
231 : 0 : fclose(fp);
232 : :
233 : 0 : return size;
234 : : }
235 : :
236 : : static unsigned long
237 : 0 : uio_get_res_phy_addr_offs(int uio_idx, int res_idx)
238 : : {
239 : : unsigned long offset;
240 : : char file[64];
241 : : FILE *fp;
242 : :
243 : : sprintf(file, "/sys/class/uio/uio%d/maps/map%d/offset",
244 : : uio_idx, res_idx);
245 : :
246 : 0 : fp = fopen(file, "r");
247 [ # # ]: 0 : if (fp == NULL)
248 : : return 0;
249 : :
250 [ # # ]: 0 : if (fscanf(fp, "0x%lx", &offset) != 1)
251 : 0 : offset = 0;
252 : :
253 : 0 : fclose(fp);
254 : :
255 : 0 : return offset;
256 : : }
257 : :
258 : : static unsigned long
259 : 0 : uio_get_res_phy_addr(int uio_idx, int res_idx)
260 : : {
261 : : unsigned long addr;
262 : : char file[64];
263 : : FILE *fp;
264 : :
265 : : sprintf(file, "/sys/class/uio/uio%d/maps/map%d/addr",
266 : : uio_idx, res_idx);
267 : :
268 : 0 : fp = fopen(file, "r");
269 [ # # ]: 0 : if (fp == NULL)
270 : : return 0;
271 : :
272 [ # # ]: 0 : if (fscanf(fp, "0x%lx", &addr) != 1)
273 : 0 : addr = 0;
274 : :
275 : 0 : fclose(fp);
276 : :
277 : 0 : return addr;
278 : : }
279 : :
280 : : static void *
281 : 0 : uio_get_map_res_addr(int uio_idx, int size, int res_idx)
282 : : {
283 : : char name[64];
284 : : int fd;
285 : : void *addr;
286 : :
287 [ # # ]: 0 : if (size == 0)
288 : : return NULL;
289 : :
290 : : sprintf(name, "/dev/uio%d", uio_idx);
291 : :
292 : : fd = open(name, O_RDWR);
293 [ # # ]: 0 : if (fd < 0)
294 : : return NULL;
295 : :
296 : 0 : if (size < getpagesize())
297 : : size = getpagesize();
298 : :
299 : 0 : addr = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED,
300 : 0 : fd, res_idx * getpagesize());
301 : :
302 : 0 : close(fd);
303 : :
304 : 0 : return addr;
305 : : }
306 : :
307 : : RTE_EXPORT_INTERNAL_SYMBOL(ionic_uio_get_rsrc)
308 : : void
309 : 0 : ionic_uio_get_rsrc(const char *name, int idx, struct ionic_dev_bar *bar)
310 : : {
311 : : int num;
312 : : int offs;
313 : :
314 : 0 : num = uio_get_multi_dev_uionum(name);
315 [ # # ]: 0 : if (num < 0)
316 : : return;
317 : :
318 : 0 : bar->len = uio_get_res_size(num, idx);
319 : 0 : offs = uio_get_res_phy_addr_offs(num, idx);
320 : 0 : bar->bus_addr = uio_get_res_phy_addr(num, idx);
321 : 0 : bar->bus_addr += offs;
322 : 0 : bar->vaddr = uio_get_map_res_addr(num, bar->len, idx);
323 : 0 : bar->vaddr = ((char *)bar->vaddr) + offs;
324 : : }
325 : :
326 : : RTE_EXPORT_INTERNAL_SYMBOL(ionic_uio_rel_rsrc)
327 : : void
328 : 0 : ionic_uio_rel_rsrc(const char *name, int idx, struct ionic_dev_bar *bar)
329 : : {
330 : : int num, offs;
331 : :
332 : 0 : num = uio_get_multi_dev_uionum(name);
333 [ # # ]: 0 : if (num < 0)
334 : : return;
335 [ # # ]: 0 : if (bar->vaddr == NULL)
336 : : return;
337 : :
338 : 0 : offs = uio_get_res_phy_addr_offs(num, idx);
339 : 0 : munmap(((char *)bar->vaddr) - offs, bar->len);
340 : : }
|