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 int ifpga_bus_logtype;
37 /* Forward declaration to access Intel FPGA bus
38 * on which iFPGA devices are connected
40 static struct rte_bus rte_ifpga_bus;
42 static struct ifpga_afu_dev_list ifpga_afu_dev_list =
43 TAILQ_HEAD_INITIALIZER(ifpga_afu_dev_list);
44 static struct ifpga_afu_drv_list ifpga_afu_drv_list =
45 TAILQ_HEAD_INITIALIZER(ifpga_afu_drv_list);
48 /* register a ifpga bus based driver */
49 void rte_ifpga_driver_register(struct rte_afu_driver *driver)
53 TAILQ_INSERT_TAIL(&ifpga_afu_drv_list, driver, next);
56 /* un-register a fpga bus based driver */
57 void rte_ifpga_driver_unregister(struct rte_afu_driver *driver)
59 TAILQ_REMOVE(&ifpga_afu_drv_list, driver, next);
62 static struct rte_afu_device *
63 ifpga_find_afu_dev(const struct rte_rawdev *rdev,
64 const struct rte_afu_id *afu_id)
66 struct rte_afu_device *afu_dev = NULL;
68 TAILQ_FOREACH(afu_dev, &ifpga_afu_dev_list, next) {
70 afu_dev->rawdev == rdev &&
71 !ifpga_afu_id_cmp(&afu_dev->id, afu_id))
77 struct rte_afu_device *
78 rte_ifpga_find_afu_by_name(const char *name)
80 struct rte_afu_device *afu_dev = NULL;
82 TAILQ_FOREACH(afu_dev, &ifpga_afu_dev_list, next) {
84 !strcmp(afu_dev->device.name, name))
90 static const char * const valid_args[] = {
91 #define IFPGA_ARG_NAME "ifpga"
93 #define IFPGA_ARG_PORT "port"
95 #define IFPGA_AFU_BTS "afu_bts"
101 * Scan the content of the FPGA bus, and the devices in the devices
104 static struct rte_afu_device *
105 ifpga_scan_one(struct rte_rawdev *rawdev,
106 struct rte_devargs *devargs)
108 struct rte_kvargs *kvlist = NULL;
109 struct rte_afu_device *afu_dev = NULL;
110 struct rte_afu_pr_conf afu_pr_conf;
114 memset(&afu_pr_conf, 0, sizeof(struct rte_afu_pr_conf));
116 kvlist = rte_kvargs_parse(devargs->args, valid_args);
118 IFPGA_BUS_ERR("error when parsing param");
122 if (rte_kvargs_count(kvlist, IFPGA_ARG_PORT) == 1) {
123 if (rte_kvargs_process(kvlist, IFPGA_ARG_PORT,
124 &rte_ifpga_get_integer32_arg, &afu_pr_conf.afu_id.port) < 0) {
125 IFPGA_BUS_ERR("error to parse %s",
130 IFPGA_BUS_ERR("arg %s is mandatory for ifpga bus",
135 if (rte_kvargs_count(kvlist, IFPGA_AFU_BTS) == 1) {
136 if (rte_kvargs_process(kvlist, IFPGA_AFU_BTS,
137 &rte_ifpga_get_string_arg, &path) < 0) {
138 IFPGA_BUS_ERR("Failed to parse %s",
142 afu_pr_conf.pr_enable = 1;
144 afu_pr_conf.pr_enable = 0;
147 afu_pr_conf.afu_id.uuid.uuid_low = 0;
148 afu_pr_conf.afu_id.uuid.uuid_high = 0;
150 if (ifpga_find_afu_dev(rawdev, &afu_pr_conf.afu_id))
153 afu_dev = calloc(1, sizeof(*afu_dev));
157 afu_dev->device.bus = &rte_ifpga_bus;
158 afu_dev->device.devargs = devargs;
159 afu_dev->device.numa_node = SOCKET_ID_ANY;
160 afu_dev->device.name = devargs->name;
161 afu_dev->rawdev = rawdev;
162 afu_dev->id.uuid.uuid_low = 0;
163 afu_dev->id.uuid.uuid_high = 0;
164 afu_dev->id.port = afu_pr_conf.afu_id.port;
166 if (rawdev->dev_ops && rawdev->dev_ops->dev_info_get)
167 rawdev->dev_ops->dev_info_get(rawdev, afu_dev);
169 if (rawdev->dev_ops &&
170 rawdev->dev_ops->dev_start &&
171 rawdev->dev_ops->dev_start(rawdev))
174 strlcpy(afu_pr_conf.bs_path, path, sizeof(afu_pr_conf.bs_path));
175 if (rawdev->dev_ops &&
176 rawdev->dev_ops->firmware_load &&
177 rawdev->dev_ops->firmware_load(rawdev,
179 IFPGA_BUS_ERR("firmware load error %d\n", ret);
182 afu_dev->id.uuid.uuid_low = afu_pr_conf.afu_id.uuid.uuid_low;
183 afu_dev->id.uuid.uuid_high = afu_pr_conf.afu_id.uuid.uuid_high;
185 rte_kvargs_free(kvlist);
191 rte_kvargs_free(kvlist);
201 * Scan the content of the FPGA bus, and the devices in the devices
207 struct rte_devargs *devargs;
208 struct rte_kvargs *kvlist = NULL;
209 struct rte_rawdev *rawdev = NULL;
211 char name1[RTE_RAWDEV_NAME_MAX_LEN];
212 struct rte_afu_device *afu_dev = NULL;
214 /* for FPGA devices we scan the devargs_list populated via cmdline */
215 RTE_EAL_DEVARGS_FOREACH(IFPGA_ARG_NAME, devargs) {
216 if (devargs->bus != &rte_ifpga_bus)
219 kvlist = rte_kvargs_parse(devargs->args, valid_args);
221 IFPGA_BUS_ERR("error when parsing param");
225 if (rte_kvargs_count(kvlist, IFPGA_ARG_NAME) == 1) {
226 if (rte_kvargs_process(kvlist, IFPGA_ARG_NAME,
227 &rte_ifpga_get_string_arg, &name) < 0) {
228 IFPGA_BUS_ERR("error to parse %s",
233 IFPGA_BUS_ERR("arg %s is mandatory for ifpga bus",
238 memset(name1, 0, sizeof(name1));
239 snprintf(name1, RTE_RAWDEV_NAME_MAX_LEN, "IFPGA:%s", name);
241 rawdev = rte_rawdev_pmd_get_named_dev(name1);
245 afu_dev = ifpga_scan_one(rawdev, devargs);
247 TAILQ_INSERT_TAIL(&ifpga_afu_dev_list, afu_dev, next);
252 rte_kvargs_free(kvlist);
260 * Match the AFU Driver and AFU Device using the ID Table
263 rte_afu_match(const struct rte_afu_driver *afu_drv,
264 const struct rte_afu_device *afu_dev)
266 const struct rte_afu_uuid *id_table;
268 for (id_table = afu_drv->id_table;
269 ((id_table->uuid_low != 0) && (id_table->uuid_high != 0));
271 /* check if device's identifiers match the driver's ones */
272 if ((id_table->uuid_low != afu_dev->id.uuid.uuid_low) ||
273 id_table->uuid_high !=
274 afu_dev->id.uuid.uuid_high)
284 ifpga_probe_one_driver(struct rte_afu_driver *drv,
285 struct rte_afu_device *afu_dev)
289 if (!rte_afu_match(drv, afu_dev))
290 /* Match of device and driver failed */
293 /* reference driver structure */
294 afu_dev->driver = drv;
296 /* call the driver probe() function */
297 ret = drv->probe(afu_dev);
299 afu_dev->driver = NULL;
301 afu_dev->device.driver = &drv->driver;
307 ifpga_probe_all_drivers(struct rte_afu_device *afu_dev)
309 struct rte_afu_driver *drv = NULL;
315 /* Check if a driver is already loaded */
316 if (rte_dev_is_probed(&afu_dev->device)) {
317 IFPGA_BUS_DEBUG("Device %s is already probed\n",
318 rte_ifpga_device_name(afu_dev));
322 TAILQ_FOREACH(drv, &ifpga_afu_drv_list, next) {
323 ret = ifpga_probe_one_driver(drv, afu_dev);
325 /* negative value is an error */
328 /* positive value means driver doesn't support it */
332 if ((ret > 0) && (afu_dev->driver == NULL))
339 * Scan the content of the Intel FPGA bus, and call the probe() function for
340 * all registered drivers that have a matching entry in its id_table
341 * for discovered devices.
346 struct rte_afu_device *afu_dev = NULL;
349 TAILQ_FOREACH(afu_dev, &ifpga_afu_dev_list, next) {
350 ret = ifpga_probe_all_drivers(afu_dev);
354 IFPGA_BUS_ERR("failed to initialize %s device\n",
355 rte_ifpga_device_name(afu_dev));
362 ifpga_plug(struct rte_device *dev)
364 return ifpga_probe_all_drivers(RTE_DEV_TO_AFU(dev));
368 ifpga_remove_driver(struct rte_afu_device *afu_dev)
372 name = rte_ifpga_device_name(afu_dev);
373 if (afu_dev->driver == NULL) {
374 IFPGA_BUS_DEBUG("no driver attach to device %s\n", name);
378 return afu_dev->driver->remove(afu_dev);
382 ifpga_unplug(struct rte_device *dev)
384 struct rte_afu_device *afu_dev = NULL;
390 afu_dev = RTE_DEV_TO_AFU(dev);
394 ret = ifpga_remove_driver(afu_dev);
398 TAILQ_REMOVE(&ifpga_afu_dev_list, afu_dev, next);
400 rte_devargs_remove(dev->devargs);
406 static struct rte_device *
407 ifpga_find_device(const struct rte_device *start,
408 rte_dev_cmp_t cmp, const void *data)
410 struct rte_afu_device *afu_dev;
412 TAILQ_FOREACH(afu_dev, &ifpga_afu_dev_list, next) {
413 if (start && &afu_dev->device == start) {
417 if (cmp(&afu_dev->device, data) == 0)
418 return &afu_dev->device;
424 ifpga_parse(const char *name, void *addr)
427 struct rte_rawdev *rawdev = NULL;
428 char rawdev_name[RTE_RAWDEV_NAME_MAX_LEN];
431 int port = IFPGA_BUS_DEV_PORT_MAX;
433 int str_port_len = 0;
436 memset(str_port, 0, 8);
437 c1 = strchr(name, '|');
439 str_port_len = c1 - name;
443 if (str_port_len < 8 &&
445 memcpy(str_port, name, str_port_len);
446 ret = sscanf(str_port, "%d", &port);
451 memset(rawdev_name, 0, sizeof(rawdev_name));
452 snprintf(rawdev_name, RTE_RAWDEV_NAME_MAX_LEN, "IFPGA:%s", c2);
453 rawdev = rte_rawdev_pmd_get_named_dev(rawdev_name);
455 if ((port < IFPGA_BUS_DEV_PORT_MAX) &&
460 if ((port < IFPGA_BUS_DEV_PORT_MAX) &&
467 static struct rte_bus rte_ifpga_bus = {
469 .probe = ifpga_probe,
470 .find_device = ifpga_find_device,
472 .unplug = ifpga_unplug,
473 .parse = ifpga_parse,
476 RTE_REGISTER_BUS(IFPGA_BUS_NAME, rte_ifpga_bus);
478 RTE_INIT(ifpga_init_log)
480 ifpga_bus_logtype = rte_log_register("bus.ifpga");
481 if (ifpga_bus_logtype >= 0)
482 rte_log_set_level(ifpga_bus_logtype, RTE_LOG_NOTICE);