xref: /f-stack/dpdk/drivers/bus/ifpga/ifpga_bus.c (revision 2d9fd380)
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2010-2018 Intel Corporation
3  */
4 
5 #include <string.h>
6 #include <inttypes.h>
7 #include <stdint.h>
8 #include <stdlib.h>
9 #include <stdio.h>
10 #include <sys/queue.h>
11 #include <sys/mman.h>
12 #include <sys/types.h>
13 #include <unistd.h>
14 #include <fcntl.h>
15 
16 #include <rte_errno.h>
17 #include <rte_bus.h>
18 #include <rte_per_lcore.h>
19 #include <rte_memory.h>
20 #include <rte_memzone.h>
21 #include <rte_eal.h>
22 #include <rte_common.h>
23 #include <rte_devargs.h>
24 #include <rte_kvargs.h>
25 #include <rte_alarm.h>
26 #include <rte_string_fns.h>
27 #include <rte_debug.h>
28 
29 #include "rte_rawdev.h"
30 #include "rte_rawdev_pmd.h"
31 #include "rte_bus_ifpga.h"
32 #include "ifpga_logs.h"
33 #include "ifpga_common.h"
34 
35 /* Forward declaration to access Intel FPGA bus
36  * on which iFPGA devices are connected
37  */
38 static struct rte_bus rte_ifpga_bus;
39 
40 static struct ifpga_afu_dev_list ifpga_afu_dev_list =
41 	TAILQ_HEAD_INITIALIZER(ifpga_afu_dev_list);
42 static struct ifpga_afu_drv_list ifpga_afu_drv_list =
43 	TAILQ_HEAD_INITIALIZER(ifpga_afu_drv_list);
44 
45 
46 /* register a ifpga bus based driver */
rte_ifpga_driver_register(struct rte_afu_driver * driver)47 void rte_ifpga_driver_register(struct rte_afu_driver *driver)
48 {
49 	RTE_VERIFY(driver);
50 
51 	TAILQ_INSERT_TAIL(&ifpga_afu_drv_list, driver, next);
52 }
53 
54 /* un-register a fpga bus based driver */
rte_ifpga_driver_unregister(struct rte_afu_driver * driver)55 void rte_ifpga_driver_unregister(struct rte_afu_driver *driver)
56 {
57 	TAILQ_REMOVE(&ifpga_afu_drv_list, driver, next);
58 }
59 
60 static struct rte_afu_device *
ifpga_find_afu_dev(const struct rte_rawdev * rdev,const struct rte_afu_id * afu_id)61 ifpga_find_afu_dev(const struct rte_rawdev *rdev,
62 	const struct rte_afu_id *afu_id)
63 {
64 	struct rte_afu_device *afu_dev = NULL;
65 
66 	TAILQ_FOREACH(afu_dev, &ifpga_afu_dev_list, next) {
67 		if (afu_dev &&
68 			afu_dev->rawdev == rdev &&
69 			!ifpga_afu_id_cmp(&afu_dev->id, afu_id))
70 			return afu_dev;
71 	}
72 	return NULL;
73 }
74 
75 struct rte_afu_device *
rte_ifpga_find_afu_by_name(const char * name)76 rte_ifpga_find_afu_by_name(const char *name)
77 {
78 	struct rte_afu_device *afu_dev = NULL;
79 
80 	TAILQ_FOREACH(afu_dev, &ifpga_afu_dev_list, next) {
81 		if (afu_dev &&
82 			!strcmp(afu_dev->device.name, name))
83 			return afu_dev;
84 	}
85 	return NULL;
86 }
87 
88 static const char * const valid_args[] = {
89 #define IFPGA_ARG_NAME         "ifpga"
90 	IFPGA_ARG_NAME,
91 #define IFPGA_ARG_PORT         "port"
92 	IFPGA_ARG_PORT,
93 #define IFPGA_AFU_BTS          "afu_bts"
94 	IFPGA_AFU_BTS,
95 	NULL
96 };
97 
98 /*
99  * Scan the content of the FPGA bus, and the devices in the devices
100  * list
101  */
102 static struct rte_afu_device *
ifpga_scan_one(struct rte_rawdev * rawdev,struct rte_devargs * devargs)103 ifpga_scan_one(struct rte_rawdev *rawdev,
104 		struct rte_devargs *devargs)
105 {
106 	struct rte_kvargs *kvlist = NULL;
107 	struct rte_afu_device *afu_dev = NULL;
108 	struct rte_afu_pr_conf afu_pr_conf;
109 	int ret = 0;
110 	char *path = NULL;
111 
112 	memset(&afu_pr_conf, 0, sizeof(struct rte_afu_pr_conf));
113 
114 	kvlist = rte_kvargs_parse(devargs->args, valid_args);
115 	if (!kvlist) {
116 		IFPGA_BUS_ERR("error when parsing param");
117 		goto end;
118 	}
119 
120 	if (rte_kvargs_count(kvlist, IFPGA_ARG_PORT) == 1) {
121 		if (rte_kvargs_process(kvlist, IFPGA_ARG_PORT,
122 		&rte_ifpga_get_integer32_arg, &afu_pr_conf.afu_id.port) < 0) {
123 			IFPGA_BUS_ERR("error to parse %s",
124 				     IFPGA_ARG_PORT);
125 			goto end;
126 		}
127 	} else {
128 		IFPGA_BUS_ERR("arg %s is mandatory for ifpga bus",
129 			  IFPGA_ARG_PORT);
130 		goto end;
131 	}
132 
133 	if (rte_kvargs_count(kvlist, IFPGA_AFU_BTS) == 1) {
134 		if (rte_kvargs_process(kvlist, IFPGA_AFU_BTS,
135 				       &rte_ifpga_get_string_arg, &path) < 0) {
136 			IFPGA_BUS_ERR("Failed to parse %s",
137 				     IFPGA_AFU_BTS);
138 			goto end;
139 		}
140 		afu_pr_conf.pr_enable = 1;
141 	} else {
142 		afu_pr_conf.pr_enable = 0;
143 	}
144 
145 	afu_pr_conf.afu_id.uuid.uuid_low = 0;
146 	afu_pr_conf.afu_id.uuid.uuid_high = 0;
147 
148 	if (ifpga_find_afu_dev(rawdev, &afu_pr_conf.afu_id))
149 		goto end;
150 
151 	afu_dev = calloc(1, sizeof(*afu_dev));
152 	if (!afu_dev)
153 		goto end;
154 
155 	afu_dev->device.bus = &rte_ifpga_bus;
156 	afu_dev->device.devargs = devargs;
157 	afu_dev->device.numa_node = SOCKET_ID_ANY;
158 	afu_dev->device.name = devargs->name;
159 	afu_dev->rawdev = rawdev;
160 	afu_dev->id.uuid.uuid_low  = 0;
161 	afu_dev->id.uuid.uuid_high = 0;
162 	afu_dev->id.port      = afu_pr_conf.afu_id.port;
163 
164 	if (rawdev->dev_ops && rawdev->dev_ops->dev_info_get)
165 		rawdev->dev_ops->dev_info_get(rawdev, afu_dev, sizeof(*afu_dev));
166 
167 	if (rawdev->dev_ops &&
168 		rawdev->dev_ops->dev_start &&
169 		rawdev->dev_ops->dev_start(rawdev))
170 		goto end;
171 
172 	strlcpy(afu_pr_conf.bs_path, path, sizeof(afu_pr_conf.bs_path));
173 	if (rawdev->dev_ops &&
174 		rawdev->dev_ops->firmware_load &&
175 		rawdev->dev_ops->firmware_load(rawdev,
176 				&afu_pr_conf)){
177 		IFPGA_BUS_ERR("firmware load error %d\n", ret);
178 		goto end;
179 	}
180 	afu_dev->id.uuid.uuid_low  = afu_pr_conf.afu_id.uuid.uuid_low;
181 	afu_dev->id.uuid.uuid_high = afu_pr_conf.afu_id.uuid.uuid_high;
182 
183 	rte_kvargs_free(kvlist);
184 	free(path);
185 	return afu_dev;
186 
187 end:
188 	if (kvlist)
189 		rte_kvargs_free(kvlist);
190 	if (path)
191 		free(path);
192 	if (afu_dev)
193 		free(afu_dev);
194 
195 	return NULL;
196 }
197 
198 /*
199  * Scan the content of the FPGA bus, and the devices in the devices
200  * list
201  */
202 static int
ifpga_scan(void)203 ifpga_scan(void)
204 {
205 	struct rte_devargs *devargs;
206 	struct rte_kvargs *kvlist = NULL;
207 	struct rte_rawdev *rawdev = NULL;
208 	char *name = NULL;
209 	char name1[RTE_RAWDEV_NAME_MAX_LEN];
210 	struct rte_afu_device *afu_dev = NULL;
211 
212 	/* for FPGA devices we scan the devargs_list populated via cmdline */
213 	RTE_EAL_DEVARGS_FOREACH(IFPGA_ARG_NAME, devargs) {
214 		if (devargs->bus != &rte_ifpga_bus)
215 			continue;
216 
217 		kvlist = rte_kvargs_parse(devargs->args, valid_args);
218 		if (!kvlist) {
219 			IFPGA_BUS_ERR("error when parsing param");
220 			goto end;
221 		}
222 
223 		if (rte_kvargs_count(kvlist, IFPGA_ARG_NAME) == 1) {
224 			if (rte_kvargs_process(kvlist, IFPGA_ARG_NAME,
225 				       &rte_ifpga_get_string_arg, &name) < 0) {
226 				IFPGA_BUS_ERR("error to parse %s",
227 				     IFPGA_ARG_NAME);
228 				goto end;
229 			}
230 		} else {
231 			IFPGA_BUS_ERR("arg %s is mandatory for ifpga bus",
232 			  IFPGA_ARG_NAME);
233 			goto end;
234 		}
235 
236 		memset(name1, 0, sizeof(name1));
237 		snprintf(name1, RTE_RAWDEV_NAME_MAX_LEN, "IFPGA:%s", name);
238 
239 		rawdev = rte_rawdev_pmd_get_named_dev(name1);
240 		if (!rawdev)
241 			goto end;
242 
243 		afu_dev = ifpga_scan_one(rawdev, devargs);
244 		if (afu_dev != NULL)
245 			TAILQ_INSERT_TAIL(&ifpga_afu_dev_list, afu_dev, next);
246 	}
247 
248 end:
249 	if (kvlist)
250 		rte_kvargs_free(kvlist);
251 	if (name)
252 		free(name);
253 
254 	return 0;
255 }
256 
257 /*
258  * Match the AFU Driver and AFU Device using the ID Table
259  */
260 static int
rte_afu_match(const struct rte_afu_driver * afu_drv,const struct rte_afu_device * afu_dev)261 rte_afu_match(const struct rte_afu_driver *afu_drv,
262 	      const struct rte_afu_device *afu_dev)
263 {
264 	const struct rte_afu_uuid *id_table;
265 
266 	for (id_table = afu_drv->id_table;
267 		((id_table->uuid_low != 0) && (id_table->uuid_high != 0));
268 	     id_table++) {
269 		/* check if device's identifiers match the driver's ones */
270 		if ((id_table->uuid_low != afu_dev->id.uuid.uuid_low) ||
271 				id_table->uuid_high !=
272 				 afu_dev->id.uuid.uuid_high)
273 			continue;
274 
275 		return 1;
276 	}
277 
278 	return 0;
279 }
280 
281 static int
ifpga_probe_one_driver(struct rte_afu_driver * drv,struct rte_afu_device * afu_dev)282 ifpga_probe_one_driver(struct rte_afu_driver *drv,
283 			struct rte_afu_device *afu_dev)
284 {
285 	int ret;
286 
287 	if (!rte_afu_match(drv, afu_dev))
288 		/* Match of device and driver failed */
289 		return 1;
290 
291 	/* reference driver structure */
292 	afu_dev->driver = drv;
293 
294 	/* call the driver probe() function */
295 	ret = drv->probe(afu_dev);
296 	if (ret)
297 		afu_dev->driver = NULL;
298 	else
299 		afu_dev->device.driver = &drv->driver;
300 
301 	return ret;
302 }
303 
304 static int
ifpga_probe_all_drivers(struct rte_afu_device * afu_dev)305 ifpga_probe_all_drivers(struct rte_afu_device *afu_dev)
306 {
307 	struct rte_afu_driver *drv = NULL;
308 	int ret = 0;
309 
310 	if (afu_dev == NULL)
311 		return -1;
312 
313 	/* Check if a driver is already loaded */
314 	if (rte_dev_is_probed(&afu_dev->device)) {
315 		IFPGA_BUS_DEBUG("Device %s is already probed\n",
316 				rte_ifpga_device_name(afu_dev));
317 		return -EEXIST;
318 	}
319 
320 	TAILQ_FOREACH(drv, &ifpga_afu_drv_list, next) {
321 		ret = ifpga_probe_one_driver(drv, afu_dev);
322 		if (ret < 0)
323 			/* negative value is an error */
324 			return ret;
325 		if (ret > 0)
326 			/* positive value means driver doesn't support it */
327 			continue;
328 		return 0;
329 	}
330 	if ((ret > 0) && (afu_dev->driver == NULL))
331 		return 0;
332 	else
333 		return ret;
334 }
335 
336 /*
337  * Scan the content of the Intel FPGA bus, and call the probe() function for
338  * all registered drivers that have a matching entry in its id_table
339  * for discovered devices.
340  */
341 static int
ifpga_probe(void)342 ifpga_probe(void)
343 {
344 	struct rte_afu_device *afu_dev = NULL;
345 	int ret = 0;
346 
347 	TAILQ_FOREACH(afu_dev, &ifpga_afu_dev_list, next) {
348 		ret = ifpga_probe_all_drivers(afu_dev);
349 		if (ret == -EEXIST)
350 			continue;
351 		if (ret < 0)
352 			IFPGA_BUS_ERR("failed to initialize %s device\n",
353 				rte_ifpga_device_name(afu_dev));
354 	}
355 
356 	return ret;
357 }
358 
359 static int
ifpga_plug(struct rte_device * dev)360 ifpga_plug(struct rte_device *dev)
361 {
362 	return ifpga_probe_all_drivers(RTE_DEV_TO_AFU(dev));
363 }
364 
365 static int
ifpga_remove_driver(struct rte_afu_device * afu_dev)366 ifpga_remove_driver(struct rte_afu_device *afu_dev)
367 {
368 	const char *name;
369 
370 	name = rte_ifpga_device_name(afu_dev);
371 	if (afu_dev->driver == NULL) {
372 		IFPGA_BUS_DEBUG("no driver attach to device %s\n", name);
373 		return 1;
374 	}
375 
376 	return afu_dev->driver->remove(afu_dev);
377 }
378 
379 static int
ifpga_unplug(struct rte_device * dev)380 ifpga_unplug(struct rte_device *dev)
381 {
382 	struct rte_afu_device *afu_dev = NULL;
383 	int ret;
384 
385 	if (dev == NULL)
386 		return -EINVAL;
387 
388 	afu_dev = RTE_DEV_TO_AFU(dev);
389 	if (!afu_dev)
390 		return -ENOENT;
391 
392 	ret = ifpga_remove_driver(afu_dev);
393 	if (ret)
394 		return ret;
395 
396 	TAILQ_REMOVE(&ifpga_afu_dev_list, afu_dev, next);
397 
398 	rte_devargs_remove(dev->devargs);
399 	free(afu_dev);
400 	return 0;
401 
402 }
403 
404 static struct rte_device *
ifpga_find_device(const struct rte_device * start,rte_dev_cmp_t cmp,const void * data)405 ifpga_find_device(const struct rte_device *start,
406 	rte_dev_cmp_t cmp, const void *data)
407 {
408 	struct rte_afu_device *afu_dev;
409 
410 	TAILQ_FOREACH(afu_dev, &ifpga_afu_dev_list, next) {
411 		if (start && &afu_dev->device == start) {
412 			start = NULL;
413 			continue;
414 		}
415 		if (cmp(&afu_dev->device, data) == 0)
416 			return &afu_dev->device;
417 	}
418 
419 	return NULL;
420 }
421 static int
ifpga_parse(const char * name,void * addr)422 ifpga_parse(const char *name, void *addr)
423 {
424 	int *out = addr;
425 	struct rte_rawdev *rawdev = NULL;
426 	char rawdev_name[RTE_RAWDEV_NAME_MAX_LEN];
427 	char *c1 = NULL;
428 	char *c2 = NULL;
429 	int port = IFPGA_BUS_DEV_PORT_MAX;
430 	char str_port[8];
431 	int str_port_len = 0;
432 	int ret;
433 
434 	memset(str_port, 0, 8);
435 	c1 = strchr(name, '|');
436 	if (c1 != NULL) {
437 		str_port_len = c1 - name;
438 		c2 = c1 + 1;
439 	}
440 
441 	if (str_port_len < 8 &&
442 		str_port_len > 0) {
443 		memcpy(str_port, name, str_port_len);
444 		ret = sscanf(str_port, "%d", &port);
445 		if (ret == -1)
446 			return 0;
447 	}
448 
449 	memset(rawdev_name, 0, sizeof(rawdev_name));
450 	snprintf(rawdev_name, RTE_RAWDEV_NAME_MAX_LEN, "IFPGA:%s", c2);
451 	rawdev = rte_rawdev_pmd_get_named_dev(rawdev_name);
452 
453 	if ((port < IFPGA_BUS_DEV_PORT_MAX) &&
454 		rawdev &&
455 		(addr != NULL))
456 		*out = port;
457 
458 	if ((port < IFPGA_BUS_DEV_PORT_MAX) &&
459 		rawdev)
460 		return 0;
461 	else
462 		return 1;
463 }
464 
465 static struct rte_bus rte_ifpga_bus = {
466 	.scan        = ifpga_scan,
467 	.probe       = ifpga_probe,
468 	.find_device = ifpga_find_device,
469 	.plug        = ifpga_plug,
470 	.unplug      = ifpga_unplug,
471 	.parse       = ifpga_parse,
472 };
473 
474 RTE_REGISTER_BUS(IFPGA_BUS_NAME, rte_ifpga_bus);
475 RTE_LOG_REGISTER(ifpga_bus_logtype, bus.ifpga, NOTICE);
476