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