1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2010-2018 Intel Corporation
10 #include <sys/queue.h>
12 #include <sys/types.h>
16 #include <rte_errno.h>
18 #include <rte_per_lcore.h>
19 #include <rte_memory.h>
20 #include <rte_memzone.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>
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"
35 /* Forward declaration to access Intel FPGA bus
36 * on which iFPGA devices are connected
38 static struct rte_bus rte_ifpga_bus;
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);
46 /* register a ifpga bus based driver */
47 void rte_ifpga_driver_register(struct rte_afu_driver *driver)
51 TAILQ_INSERT_TAIL(&ifpga_afu_drv_list, driver, next);
54 /* un-register a fpga bus based driver */
55 void rte_ifpga_driver_unregister(struct rte_afu_driver *driver)
57 TAILQ_REMOVE(&ifpga_afu_drv_list, driver, next);
60 static struct rte_afu_device *
61 ifpga_find_afu_dev(const struct rte_rawdev *rdev,
62 const struct rte_afu_id *afu_id)
64 struct rte_afu_device *afu_dev = NULL;
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))
74 struct rte_afu_device *
75 rte_ifpga_find_afu_by_name(const char *name)
77 struct rte_afu_device *afu_dev = NULL;
79 TAILQ_FOREACH(afu_dev, &ifpga_afu_dev_list, next) {
80 if (!strcmp(afu_dev->device.name, name))
86 static const char * const valid_args[] = {
87 #define IFPGA_ARG_NAME "ifpga"
89 #define IFPGA_ARG_PORT "port"
91 #define IFPGA_AFU_BTS "afu_bts"
97 * Scan the content of the FPGA bus, and the devices in the devices
100 static struct rte_afu_device *
101 ifpga_scan_one(struct rte_rawdev *rawdev,
102 struct rte_devargs *devargs)
104 struct rte_kvargs *kvlist = NULL;
105 struct rte_afu_device *afu_dev = NULL;
106 struct rte_afu_pr_conf afu_pr_conf;
110 memset(&afu_pr_conf, 0, sizeof(struct rte_afu_pr_conf));
112 kvlist = rte_kvargs_parse(devargs->args, valid_args);
114 IFPGA_BUS_ERR("error when parsing param");
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",
126 IFPGA_BUS_ERR("arg %s is mandatory for ifpga bus",
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",
138 afu_pr_conf.pr_enable = 1;
140 afu_pr_conf.pr_enable = 0;
143 afu_pr_conf.afu_id.uuid.uuid_low = 0;
144 afu_pr_conf.afu_id.uuid.uuid_high = 0;
146 if (ifpga_find_afu_dev(rawdev, &afu_pr_conf.afu_id))
149 afu_dev = calloc(1, sizeof(*afu_dev));
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;
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");
170 if (rawdev->dev_ops && rawdev->dev_ops->dev_info_get)
171 rawdev->dev_ops->dev_info_get(rawdev, afu_dev, sizeof(*afu_dev));
173 if (rawdev->dev_ops &&
174 rawdev->dev_ops->dev_start &&
175 rawdev->dev_ops->dev_start(rawdev))
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,
183 IFPGA_BUS_ERR("firmware load error %d\n", ret);
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;
189 rte_kvargs_free(kvlist);
194 rte_kvargs_free(kvlist);
197 rte_intr_instance_free(afu_dev->intr_handle);
205 * Scan the content of the FPGA bus, and the devices in the devices
211 struct rte_devargs *devargs;
212 struct rte_kvargs *kvlist = NULL;
213 struct rte_rawdev *rawdev = NULL;
215 char name1[RTE_RAWDEV_NAME_MAX_LEN];
216 struct rte_afu_device *afu_dev = NULL;
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)
223 kvlist = rte_kvargs_parse(devargs->args, valid_args);
225 IFPGA_BUS_ERR("error when parsing param");
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",
237 IFPGA_BUS_ERR("arg %s is mandatory for ifpga bus",
242 memset(name1, 0, sizeof(name1));
243 snprintf(name1, RTE_RAWDEV_NAME_MAX_LEN, "IFPGA:%s", name);
245 rawdev = rte_rawdev_pmd_get_named_dev(name1);
249 afu_dev = ifpga_scan_one(rawdev, devargs);
251 TAILQ_INSERT_TAIL(&ifpga_afu_dev_list, afu_dev, next);
255 rte_kvargs_free(kvlist);
262 * Match the AFU Driver and AFU Device using the ID Table
265 rte_afu_match(const struct rte_afu_driver *afu_drv,
266 const struct rte_afu_device *afu_dev)
268 const struct rte_afu_uuid *id_table;
270 for (id_table = afu_drv->id_table;
271 ((id_table->uuid_low != 0) && (id_table->uuid_high != 0));
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)
286 ifpga_probe_one_driver(struct rte_afu_driver *drv,
287 struct rte_afu_device *afu_dev)
291 if (!rte_afu_match(drv, afu_dev))
292 /* Match of device and driver failed */
295 /* reference driver structure */
296 afu_dev->driver = drv;
298 /* call the driver probe() function */
299 ret = drv->probe(afu_dev);
301 afu_dev->driver = NULL;
303 afu_dev->device.driver = &drv->driver;
309 ifpga_probe_all_drivers(struct rte_afu_device *afu_dev)
311 struct rte_afu_driver *drv = NULL;
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));
324 TAILQ_FOREACH(drv, &ifpga_afu_drv_list, next) {
325 ret = ifpga_probe_one_driver(drv, afu_dev);
327 /* negative value is an error */
330 /* positive value means driver doesn't support it */
334 if ((ret > 0) && (afu_dev->driver == NULL))
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.
348 struct rte_afu_device *afu_dev = NULL;
351 TAILQ_FOREACH(afu_dev, &ifpga_afu_dev_list, next) {
352 ret = ifpga_probe_all_drivers(afu_dev);
356 IFPGA_BUS_ERR("failed to initialize %s device\n",
357 rte_ifpga_device_name(afu_dev));
364 ifpga_plug(struct rte_device *dev)
366 return ifpga_probe_all_drivers(RTE_DEV_TO_AFU(dev));
370 ifpga_remove_driver(struct rte_afu_device *afu_dev)
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);
380 return afu_dev->driver->remove(afu_dev);
384 ifpga_unplug(struct rte_device *dev)
386 struct rte_afu_device *afu_dev = NULL;
392 afu_dev = RTE_DEV_TO_AFU(dev);
396 ret = ifpga_remove_driver(afu_dev);
400 TAILQ_REMOVE(&ifpga_afu_dev_list, afu_dev, next);
402 rte_devargs_remove(dev->devargs);
403 rte_intr_instance_free(afu_dev->intr_handle);
409 static struct rte_device *
410 ifpga_find_device(const struct rte_device *start,
411 rte_dev_cmp_t cmp, const void *data)
413 struct rte_afu_device *afu_dev;
415 TAILQ_FOREACH(afu_dev, &ifpga_afu_dev_list, next) {
416 if (start && &afu_dev->device == start) {
420 if (cmp(&afu_dev->device, data) == 0)
421 return &afu_dev->device;
427 ifpga_parse(const char *name, void *addr)
430 struct rte_rawdev *rawdev = NULL;
431 char rawdev_name[RTE_RAWDEV_NAME_MAX_LEN];
434 int port = IFPGA_BUS_DEV_PORT_MAX;
436 int str_port_len = 0;
439 memset(str_port, 0, 8);
440 c1 = strchr(name, '|');
442 str_port_len = c1 - name;
446 if (str_port_len < 8 &&
448 memcpy(str_port, name, str_port_len);
449 ret = sscanf(str_port, "%d", &port);
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);
458 if ((port < IFPGA_BUS_DEV_PORT_MAX) &&
463 if ((port < IFPGA_BUS_DEV_PORT_MAX) &&
470 static struct rte_bus rte_ifpga_bus = {
472 .probe = ifpga_probe,
473 .find_device = ifpga_find_device,
475 .unplug = ifpga_unplug,
476 .parse = ifpga_parse,
479 RTE_REGISTER_BUS(IFPGA_BUS_NAME, rte_ifpga_bus);
480 RTE_LOG_REGISTER_DEFAULT(ifpga_bus_logtype, NOTICE);