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);
195 rte_kvargs_free(kvlist);
199 rte_intr_instance_free(afu_dev->intr_handle);
207 * Scan the content of the FPGA bus, and the devices in the devices
213 struct rte_devargs *devargs;
214 struct rte_kvargs *kvlist = NULL;
215 struct rte_rawdev *rawdev = NULL;
217 char name1[RTE_RAWDEV_NAME_MAX_LEN];
218 struct rte_afu_device *afu_dev = NULL;
220 /* for FPGA devices we scan the devargs_list populated via cmdline */
221 RTE_EAL_DEVARGS_FOREACH(IFPGA_ARG_NAME, devargs) {
222 if (devargs->bus != &rte_ifpga_bus)
225 kvlist = rte_kvargs_parse(devargs->args, valid_args);
227 IFPGA_BUS_ERR("error when parsing param");
231 if (rte_kvargs_count(kvlist, IFPGA_ARG_NAME) == 1) {
232 if (rte_kvargs_process(kvlist, IFPGA_ARG_NAME,
233 &rte_ifpga_get_string_arg, &name) < 0) {
234 IFPGA_BUS_ERR("error to parse %s",
239 IFPGA_BUS_ERR("arg %s is mandatory for ifpga bus",
244 memset(name1, 0, sizeof(name1));
245 snprintf(name1, RTE_RAWDEV_NAME_MAX_LEN, "IFPGA:%s", name);
247 rawdev = rte_rawdev_pmd_get_named_dev(name1);
251 afu_dev = ifpga_scan_one(rawdev, devargs);
253 TAILQ_INSERT_TAIL(&ifpga_afu_dev_list, afu_dev, next);
258 rte_kvargs_free(kvlist);
266 * Match the AFU Driver and AFU Device using the ID Table
269 rte_afu_match(const struct rte_afu_driver *afu_drv,
270 const struct rte_afu_device *afu_dev)
272 const struct rte_afu_uuid *id_table;
274 for (id_table = afu_drv->id_table;
275 ((id_table->uuid_low != 0) && (id_table->uuid_high != 0));
277 /* check if device's identifiers match the driver's ones */
278 if ((id_table->uuid_low != afu_dev->id.uuid.uuid_low) ||
279 id_table->uuid_high !=
280 afu_dev->id.uuid.uuid_high)
290 ifpga_probe_one_driver(struct rte_afu_driver *drv,
291 struct rte_afu_device *afu_dev)
295 if (!rte_afu_match(drv, afu_dev))
296 /* Match of device and driver failed */
299 /* reference driver structure */
300 afu_dev->driver = drv;
302 /* call the driver probe() function */
303 ret = drv->probe(afu_dev);
305 afu_dev->driver = NULL;
307 afu_dev->device.driver = &drv->driver;
313 ifpga_probe_all_drivers(struct rte_afu_device *afu_dev)
315 struct rte_afu_driver *drv = NULL;
321 /* Check if a driver is already loaded */
322 if (rte_dev_is_probed(&afu_dev->device)) {
323 IFPGA_BUS_DEBUG("Device %s is already probed\n",
324 rte_ifpga_device_name(afu_dev));
328 TAILQ_FOREACH(drv, &ifpga_afu_drv_list, next) {
329 ret = ifpga_probe_one_driver(drv, afu_dev);
331 /* negative value is an error */
334 /* positive value means driver doesn't support it */
338 if ((ret > 0) && (afu_dev->driver == NULL))
345 * Scan the content of the Intel FPGA bus, and call the probe() function for
346 * all registered drivers that have a matching entry in its id_table
347 * for discovered devices.
352 struct rte_afu_device *afu_dev = NULL;
355 TAILQ_FOREACH(afu_dev, &ifpga_afu_dev_list, next) {
356 ret = ifpga_probe_all_drivers(afu_dev);
360 IFPGA_BUS_ERR("failed to initialize %s device\n",
361 rte_ifpga_device_name(afu_dev));
368 ifpga_plug(struct rte_device *dev)
370 return ifpga_probe_all_drivers(RTE_DEV_TO_AFU(dev));
374 ifpga_remove_driver(struct rte_afu_device *afu_dev)
378 name = rte_ifpga_device_name(afu_dev);
379 if (afu_dev->driver == NULL) {
380 IFPGA_BUS_DEBUG("no driver attach to device %s\n", name);
384 return afu_dev->driver->remove(afu_dev);
388 ifpga_unplug(struct rte_device *dev)
390 struct rte_afu_device *afu_dev = NULL;
396 afu_dev = RTE_DEV_TO_AFU(dev);
400 ret = ifpga_remove_driver(afu_dev);
404 TAILQ_REMOVE(&ifpga_afu_dev_list, afu_dev, next);
406 rte_devargs_remove(dev->devargs);
407 rte_intr_instance_free(afu_dev->intr_handle);
413 static struct rte_device *
414 ifpga_find_device(const struct rte_device *start,
415 rte_dev_cmp_t cmp, const void *data)
417 struct rte_afu_device *afu_dev;
419 TAILQ_FOREACH(afu_dev, &ifpga_afu_dev_list, next) {
420 if (start && &afu_dev->device == start) {
424 if (cmp(&afu_dev->device, data) == 0)
425 return &afu_dev->device;
431 ifpga_parse(const char *name, void *addr)
434 struct rte_rawdev *rawdev = NULL;
435 char rawdev_name[RTE_RAWDEV_NAME_MAX_LEN];
438 int port = IFPGA_BUS_DEV_PORT_MAX;
440 int str_port_len = 0;
443 memset(str_port, 0, 8);
444 c1 = strchr(name, '|');
446 str_port_len = c1 - name;
450 if (str_port_len < 8 &&
452 memcpy(str_port, name, str_port_len);
453 ret = sscanf(str_port, "%d", &port);
458 memset(rawdev_name, 0, sizeof(rawdev_name));
459 snprintf(rawdev_name, RTE_RAWDEV_NAME_MAX_LEN, "IFPGA:%s", c2);
460 rawdev = rte_rawdev_pmd_get_named_dev(rawdev_name);
462 if ((port < IFPGA_BUS_DEV_PORT_MAX) &&
467 if ((port < IFPGA_BUS_DEV_PORT_MAX) &&
474 static struct rte_bus rte_ifpga_bus = {
476 .probe = ifpga_probe,
477 .find_device = ifpga_find_device,
479 .unplug = ifpga_unplug,
480 .parse = ifpga_parse,
483 RTE_REGISTER_BUS(IFPGA_BUS_NAME, rte_ifpga_bus);
484 RTE_LOG_REGISTER_DEFAULT(ifpga_bus_logtype, NOTICE);