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 if (rawdev->dev_ops && rawdev->dev_ops->dev_info_get)
165 rawdev->dev_ops->dev_info_get(rawdev, afu_dev, sizeof(*afu_dev));
167 if (rawdev->dev_ops &&
168 rawdev->dev_ops->dev_start &&
169 rawdev->dev_ops->dev_start(rawdev))
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,
177 IFPGA_BUS_ERR("firmware load error %d\n", ret);
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;
183 rte_kvargs_free(kvlist);
189 rte_kvargs_free(kvlist);
199 * Scan the content of the FPGA bus, and the devices in the devices
205 struct rte_devargs *devargs;
206 struct rte_kvargs *kvlist = NULL;
207 struct rte_rawdev *rawdev = NULL;
209 char name1[RTE_RAWDEV_NAME_MAX_LEN];
210 struct rte_afu_device *afu_dev = NULL;
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)
217 kvlist = rte_kvargs_parse(devargs->args, valid_args);
219 IFPGA_BUS_ERR("error when parsing param");
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",
231 IFPGA_BUS_ERR("arg %s is mandatory for ifpga bus",
236 memset(name1, 0, sizeof(name1));
237 snprintf(name1, RTE_RAWDEV_NAME_MAX_LEN, "IFPGA:%s", name);
239 rawdev = rte_rawdev_pmd_get_named_dev(name1);
243 afu_dev = ifpga_scan_one(rawdev, devargs);
245 TAILQ_INSERT_TAIL(&ifpga_afu_dev_list, afu_dev, next);
250 rte_kvargs_free(kvlist);
258 * Match the AFU Driver and AFU Device using the ID Table
261 rte_afu_match(const struct rte_afu_driver *afu_drv,
262 const struct rte_afu_device *afu_dev)
264 const struct rte_afu_uuid *id_table;
266 for (id_table = afu_drv->id_table;
267 ((id_table->uuid_low != 0) && (id_table->uuid_high != 0));
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)
282 ifpga_probe_one_driver(struct rte_afu_driver *drv,
283 struct rte_afu_device *afu_dev)
287 if (!rte_afu_match(drv, afu_dev))
288 /* Match of device and driver failed */
291 /* reference driver structure */
292 afu_dev->driver = drv;
294 /* call the driver probe() function */
295 ret = drv->probe(afu_dev);
297 afu_dev->driver = NULL;
299 afu_dev->device.driver = &drv->driver;
305 ifpga_probe_all_drivers(struct rte_afu_device *afu_dev)
307 struct rte_afu_driver *drv = NULL;
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));
320 TAILQ_FOREACH(drv, &ifpga_afu_drv_list, next) {
321 ret = ifpga_probe_one_driver(drv, afu_dev);
323 /* negative value is an error */
326 /* positive value means driver doesn't support it */
330 if ((ret > 0) && (afu_dev->driver == NULL))
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.
344 struct rte_afu_device *afu_dev = NULL;
347 TAILQ_FOREACH(afu_dev, &ifpga_afu_dev_list, next) {
348 ret = ifpga_probe_all_drivers(afu_dev);
352 IFPGA_BUS_ERR("failed to initialize %s device\n",
353 rte_ifpga_device_name(afu_dev));
360 ifpga_plug(struct rte_device *dev)
362 return ifpga_probe_all_drivers(RTE_DEV_TO_AFU(dev));
366 ifpga_remove_driver(struct rte_afu_device *afu_dev)
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);
376 return afu_dev->driver->remove(afu_dev);
380 ifpga_unplug(struct rte_device *dev)
382 struct rte_afu_device *afu_dev = NULL;
388 afu_dev = RTE_DEV_TO_AFU(dev);
392 ret = ifpga_remove_driver(afu_dev);
396 TAILQ_REMOVE(&ifpga_afu_dev_list, afu_dev, next);
398 rte_devargs_remove(dev->devargs);
404 static struct rte_device *
405 ifpga_find_device(const struct rte_device *start,
406 rte_dev_cmp_t cmp, const void *data)
408 struct rte_afu_device *afu_dev;
410 TAILQ_FOREACH(afu_dev, &ifpga_afu_dev_list, next) {
411 if (start && &afu_dev->device == start) {
415 if (cmp(&afu_dev->device, data) == 0)
416 return &afu_dev->device;
422 ifpga_parse(const char *name, void *addr)
425 struct rte_rawdev *rawdev = NULL;
426 char rawdev_name[RTE_RAWDEV_NAME_MAX_LEN];
429 int port = IFPGA_BUS_DEV_PORT_MAX;
431 int str_port_len = 0;
434 memset(str_port, 0, 8);
435 c1 = strchr(name, '|');
437 str_port_len = c1 - name;
441 if (str_port_len < 8 &&
443 memcpy(str_port, name, str_port_len);
444 ret = sscanf(str_port, "%d", &port);
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);
453 if ((port < IFPGA_BUS_DEV_PORT_MAX) &&
458 if ((port < IFPGA_BUS_DEV_PORT_MAX) &&
465 static struct rte_bus rte_ifpga_bus = {
467 .probe = ifpga_probe,
468 .find_device = ifpga_find_device,
470 .unplug = ifpga_unplug,
471 .parse = ifpga_parse,
474 RTE_REGISTER_BUS(IFPGA_BUS_NAME, rte_ifpga_bus);
475 RTE_LOG_REGISTER_DEFAULT(ifpga_bus_logtype, NOTICE);