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) {
68 afu_dev->rawdev == rdev &&
69 !ifpga_afu_id_cmp(&afu_dev->id, afu_id))
75 struct rte_afu_device *
76 rte_ifpga_find_afu_by_name(const char *name)
78 struct rte_afu_device *afu_dev = NULL;
80 TAILQ_FOREACH(afu_dev, &ifpga_afu_dev_list, next) {
82 !strcmp(afu_dev->device.name, name))
88 static const char * const valid_args[] = {
89 #define IFPGA_ARG_NAME "ifpga"
91 #define IFPGA_ARG_PORT "port"
93 #define IFPGA_AFU_BTS "afu_bts"
99 * Scan the content of the FPGA bus, and the devices in the devices
102 static struct rte_afu_device *
103 ifpga_scan_one(struct rte_rawdev *rawdev,
104 struct rte_devargs *devargs)
106 struct rte_kvargs *kvlist = NULL;
107 struct rte_afu_device *afu_dev = NULL;
108 struct rte_afu_pr_conf afu_pr_conf;
112 memset(&afu_pr_conf, 0, sizeof(struct rte_afu_pr_conf));
114 kvlist = rte_kvargs_parse(devargs->args, valid_args);
116 IFPGA_BUS_ERR("error when parsing param");
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",
128 IFPGA_BUS_ERR("arg %s is mandatory for ifpga bus",
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",
140 afu_pr_conf.pr_enable = 1;
142 afu_pr_conf.pr_enable = 0;
145 afu_pr_conf.afu_id.uuid.uuid_low = 0;
146 afu_pr_conf.afu_id.uuid.uuid_high = 0;
148 if (ifpga_find_afu_dev(rawdev, &afu_pr_conf.afu_id))
151 afu_dev = calloc(1, sizeof(*afu_dev));
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;
164 /* Allocate interrupt instance */
165 afu_dev->intr_handle =
166 rte_intr_instance_alloc(RTE_INTR_INSTANCE_F_PRIVATE);
167 if (afu_dev->intr_handle == NULL) {
168 IFPGA_BUS_ERR("Failed to allocate intr handle");
172 if (rawdev->dev_ops && rawdev->dev_ops->dev_info_get)
173 rawdev->dev_ops->dev_info_get(rawdev, afu_dev, sizeof(*afu_dev));
175 if (rawdev->dev_ops &&
176 rawdev->dev_ops->dev_start &&
177 rawdev->dev_ops->dev_start(rawdev))
180 strlcpy(afu_pr_conf.bs_path, path, sizeof(afu_pr_conf.bs_path));
181 if (rawdev->dev_ops &&
182 rawdev->dev_ops->firmware_load &&
183 rawdev->dev_ops->firmware_load(rawdev,
185 IFPGA_BUS_ERR("firmware load error %d\n", ret);
188 afu_dev->id.uuid.uuid_low = afu_pr_conf.afu_id.uuid.uuid_low;
189 afu_dev->id.uuid.uuid_high = afu_pr_conf.afu_id.uuid.uuid_high;
191 rte_kvargs_free(kvlist);
197 rte_kvargs_free(kvlist);
201 rte_intr_instance_free(afu_dev->intr_handle);
209 * Scan the content of the FPGA bus, and the devices in the devices
215 struct rte_devargs *devargs;
216 struct rte_kvargs *kvlist = NULL;
217 struct rte_rawdev *rawdev = NULL;
219 char name1[RTE_RAWDEV_NAME_MAX_LEN];
220 struct rte_afu_device *afu_dev = NULL;
222 /* for FPGA devices we scan the devargs_list populated via cmdline */
223 RTE_EAL_DEVARGS_FOREACH(IFPGA_ARG_NAME, devargs) {
224 if (devargs->bus != &rte_ifpga_bus)
227 kvlist = rte_kvargs_parse(devargs->args, valid_args);
229 IFPGA_BUS_ERR("error when parsing param");
233 if (rte_kvargs_count(kvlist, IFPGA_ARG_NAME) == 1) {
234 if (rte_kvargs_process(kvlist, IFPGA_ARG_NAME,
235 &rte_ifpga_get_string_arg, &name) < 0) {
236 IFPGA_BUS_ERR("error to parse %s",
241 IFPGA_BUS_ERR("arg %s is mandatory for ifpga bus",
246 memset(name1, 0, sizeof(name1));
247 snprintf(name1, RTE_RAWDEV_NAME_MAX_LEN, "IFPGA:%s", name);
249 rawdev = rte_rawdev_pmd_get_named_dev(name1);
253 afu_dev = ifpga_scan_one(rawdev, devargs);
255 TAILQ_INSERT_TAIL(&ifpga_afu_dev_list, afu_dev, next);
260 rte_kvargs_free(kvlist);
268 * Match the AFU Driver and AFU Device using the ID Table
271 rte_afu_match(const struct rte_afu_driver *afu_drv,
272 const struct rte_afu_device *afu_dev)
274 const struct rte_afu_uuid *id_table;
276 for (id_table = afu_drv->id_table;
277 ((id_table->uuid_low != 0) && (id_table->uuid_high != 0));
279 /* check if device's identifiers match the driver's ones */
280 if ((id_table->uuid_low != afu_dev->id.uuid.uuid_low) ||
281 id_table->uuid_high !=
282 afu_dev->id.uuid.uuid_high)
292 ifpga_probe_one_driver(struct rte_afu_driver *drv,
293 struct rte_afu_device *afu_dev)
297 if (!rte_afu_match(drv, afu_dev))
298 /* Match of device and driver failed */
301 /* reference driver structure */
302 afu_dev->driver = drv;
304 /* call the driver probe() function */
305 ret = drv->probe(afu_dev);
307 afu_dev->driver = NULL;
309 afu_dev->device.driver = &drv->driver;
315 ifpga_probe_all_drivers(struct rte_afu_device *afu_dev)
317 struct rte_afu_driver *drv = NULL;
323 /* Check if a driver is already loaded */
324 if (rte_dev_is_probed(&afu_dev->device)) {
325 IFPGA_BUS_DEBUG("Device %s is already probed\n",
326 rte_ifpga_device_name(afu_dev));
330 TAILQ_FOREACH(drv, &ifpga_afu_drv_list, next) {
331 ret = ifpga_probe_one_driver(drv, afu_dev);
333 /* negative value is an error */
336 /* positive value means driver doesn't support it */
340 if ((ret > 0) && (afu_dev->driver == NULL))
347 * Scan the content of the Intel FPGA bus, and call the probe() function for
348 * all registered drivers that have a matching entry in its id_table
349 * for discovered devices.
354 struct rte_afu_device *afu_dev = NULL;
357 TAILQ_FOREACH(afu_dev, &ifpga_afu_dev_list, next) {
358 ret = ifpga_probe_all_drivers(afu_dev);
362 IFPGA_BUS_ERR("failed to initialize %s device\n",
363 rte_ifpga_device_name(afu_dev));
370 ifpga_plug(struct rte_device *dev)
372 return ifpga_probe_all_drivers(RTE_DEV_TO_AFU(dev));
376 ifpga_remove_driver(struct rte_afu_device *afu_dev)
380 name = rte_ifpga_device_name(afu_dev);
381 if (afu_dev->driver == NULL) {
382 IFPGA_BUS_DEBUG("no driver attach to device %s\n", name);
386 return afu_dev->driver->remove(afu_dev);
390 ifpga_unplug(struct rte_device *dev)
392 struct rte_afu_device *afu_dev = NULL;
398 afu_dev = RTE_DEV_TO_AFU(dev);
402 ret = ifpga_remove_driver(afu_dev);
406 TAILQ_REMOVE(&ifpga_afu_dev_list, afu_dev, next);
408 rte_devargs_remove(dev->devargs);
409 rte_intr_instance_free(afu_dev->intr_handle);
415 static struct rte_device *
416 ifpga_find_device(const struct rte_device *start,
417 rte_dev_cmp_t cmp, const void *data)
419 struct rte_afu_device *afu_dev;
421 TAILQ_FOREACH(afu_dev, &ifpga_afu_dev_list, next) {
422 if (start && &afu_dev->device == start) {
426 if (cmp(&afu_dev->device, data) == 0)
427 return &afu_dev->device;
433 ifpga_parse(const char *name, void *addr)
436 struct rte_rawdev *rawdev = NULL;
437 char rawdev_name[RTE_RAWDEV_NAME_MAX_LEN];
440 int port = IFPGA_BUS_DEV_PORT_MAX;
442 int str_port_len = 0;
445 memset(str_port, 0, 8);
446 c1 = strchr(name, '|');
448 str_port_len = c1 - name;
452 if (str_port_len < 8 &&
454 memcpy(str_port, name, str_port_len);
455 ret = sscanf(str_port, "%d", &port);
460 memset(rawdev_name, 0, sizeof(rawdev_name));
461 snprintf(rawdev_name, RTE_RAWDEV_NAME_MAX_LEN, "IFPGA:%s", c2);
462 rawdev = rte_rawdev_pmd_get_named_dev(rawdev_name);
464 if ((port < IFPGA_BUS_DEV_PORT_MAX) &&
469 if ((port < IFPGA_BUS_DEV_PORT_MAX) &&
476 static struct rte_bus rte_ifpga_bus = {
478 .probe = ifpga_probe,
479 .find_device = ifpga_find_device,
481 .unplug = ifpga_unplug,
482 .parse = ifpga_parse,
485 RTE_REGISTER_BUS(IFPGA_BUS_NAME, rte_ifpga_bus);
486 RTE_LOG_REGISTER_DEFAULT(ifpga_bus_logtype, NOTICE);