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>
28 #include "rte_rawdev.h"
29 #include "rte_rawdev_pmd.h"
30 #include "rte_bus_ifpga.h"
31 #include "ifpga_logs.h"
32 #include "ifpga_common.h"
34 int ifpga_bus_logtype;
36 /* Forward declaration to access Intel FPGA bus
37 * on which iFPGA devices are connected
39 static struct rte_bus rte_ifpga_bus;
41 static struct ifpga_afu_dev_list ifpga_afu_dev_list =
42 TAILQ_HEAD_INITIALIZER(ifpga_afu_dev_list);
43 static struct ifpga_afu_drv_list ifpga_afu_drv_list =
44 TAILQ_HEAD_INITIALIZER(ifpga_afu_drv_list);
47 /* register a ifpga bus based driver */
48 void rte_ifpga_driver_register(struct rte_afu_driver *driver)
52 TAILQ_INSERT_TAIL(&ifpga_afu_drv_list, driver, next);
55 /* un-register a fpga bus based driver */
56 void rte_ifpga_driver_unregister(struct rte_afu_driver *driver)
58 TAILQ_REMOVE(&ifpga_afu_drv_list, driver, next);
61 static struct rte_afu_device *
62 ifpga_find_afu_dev(const struct rte_rawdev *rdev,
63 const struct rte_afu_id *afu_id)
65 struct rte_afu_device *afu_dev = NULL;
67 TAILQ_FOREACH(afu_dev, &ifpga_afu_dev_list, next) {
69 afu_dev->rawdev == rdev &&
70 !ifpga_afu_id_cmp(&afu_dev->id, afu_id))
76 static const char * const valid_args[] = {
77 #define IFPGA_ARG_NAME "ifpga"
79 #define IFPGA_ARG_PORT "port"
81 #define IFPGA_AFU_BTS "afu_bts"
87 * Scan the content of the FPGA bus, and the devices in the devices
90 static struct rte_afu_device *
91 ifpga_scan_one(struct rte_rawdev *rawdev,
92 struct rte_devargs *devargs)
94 struct rte_kvargs *kvlist = NULL;
95 struct rte_afu_device *afu_dev = NULL;
96 struct rte_afu_pr_conf afu_pr_conf;
100 memset(&afu_pr_conf, 0, sizeof(struct rte_afu_pr_conf));
102 kvlist = rte_kvargs_parse(devargs->args, valid_args);
104 IFPGA_BUS_ERR("error when parsing param");
108 if (rte_kvargs_count(kvlist, IFPGA_ARG_PORT) == 1) {
109 if (rte_kvargs_process(kvlist, IFPGA_ARG_PORT,
110 &rte_ifpga_get_integer32_arg, &afu_pr_conf.afu_id.port) < 0) {
111 IFPGA_BUS_ERR("error to parse %s",
116 IFPGA_BUS_ERR("arg %s is mandatory for ifpga bus",
121 if (rte_kvargs_count(kvlist, IFPGA_AFU_BTS) == 1) {
122 if (rte_kvargs_process(kvlist, IFPGA_AFU_BTS,
123 &rte_ifpga_get_string_arg, &path) < 0) {
124 IFPGA_BUS_ERR("Failed to parse %s",
129 IFPGA_BUS_ERR("arg %s is mandatory for ifpga bus",
134 afu_pr_conf.afu_id.uuid.uuid_low = 0;
135 afu_pr_conf.afu_id.uuid.uuid_high = 0;
136 afu_pr_conf.pr_enable = path?1:0;
138 if (ifpga_find_afu_dev(rawdev, &afu_pr_conf.afu_id))
141 afu_dev = calloc(1, sizeof(*afu_dev));
145 afu_dev->device.bus = &rte_ifpga_bus;
146 afu_dev->device.devargs = devargs;
147 afu_dev->device.numa_node = SOCKET_ID_ANY;
148 afu_dev->device.name = devargs->name;
149 afu_dev->rawdev = rawdev;
150 afu_dev->id.uuid.uuid_low = 0;
151 afu_dev->id.uuid.uuid_high = 0;
152 afu_dev->id.port = afu_pr_conf.afu_id.port;
154 if (rawdev->dev_ops && rawdev->dev_ops->dev_info_get)
155 rawdev->dev_ops->dev_info_get(rawdev, afu_dev);
157 if (rawdev->dev_ops &&
158 rawdev->dev_ops->dev_start &&
159 rawdev->dev_ops->dev_start(rawdev))
162 strlcpy(afu_pr_conf.bs_path, path, sizeof(afu_pr_conf.bs_path));
163 if (rawdev->dev_ops &&
164 rawdev->dev_ops->firmware_load &&
165 rawdev->dev_ops->firmware_load(rawdev,
167 IFPGA_BUS_ERR("firmware load error %d\n", ret);
170 afu_dev->id.uuid.uuid_low = afu_pr_conf.afu_id.uuid.uuid_low;
171 afu_dev->id.uuid.uuid_high = afu_pr_conf.afu_id.uuid.uuid_high;
173 rte_kvargs_free(kvlist);
179 rte_kvargs_free(kvlist);
189 * Scan the content of the FPGA bus, and the devices in the devices
195 struct rte_devargs *devargs;
196 struct rte_kvargs *kvlist = NULL;
197 struct rte_rawdev *rawdev = NULL;
199 char name1[RTE_RAWDEV_NAME_MAX_LEN];
200 struct rte_afu_device *afu_dev = NULL;
202 /* for FPGA devices we scan the devargs_list populated via cmdline */
203 RTE_EAL_DEVARGS_FOREACH(IFPGA_ARG_NAME, devargs) {
204 if (devargs->bus != &rte_ifpga_bus)
207 kvlist = rte_kvargs_parse(devargs->args, valid_args);
209 IFPGA_BUS_ERR("error when parsing param");
213 if (rte_kvargs_count(kvlist, IFPGA_ARG_NAME) == 1) {
214 if (rte_kvargs_process(kvlist, IFPGA_ARG_NAME,
215 &rte_ifpga_get_string_arg, &name) < 0) {
216 IFPGA_BUS_ERR("error to parse %s",
221 IFPGA_BUS_ERR("arg %s is mandatory for ifpga bus",
226 memset(name1, 0, sizeof(name1));
227 snprintf(name1, RTE_RAWDEV_NAME_MAX_LEN, "IFPGA:%s", name);
229 rawdev = rte_rawdev_pmd_get_named_dev(name1);
233 afu_dev = ifpga_scan_one(rawdev, devargs);
235 TAILQ_INSERT_TAIL(&ifpga_afu_dev_list, afu_dev, next);
240 rte_kvargs_free(kvlist);
248 * Match the AFU Driver and AFU Device using the ID Table
251 rte_afu_match(const struct rte_afu_driver *afu_drv,
252 const struct rte_afu_device *afu_dev)
254 const struct rte_afu_uuid *id_table;
256 for (id_table = afu_drv->id_table;
257 ((id_table->uuid_low != 0) && (id_table->uuid_high != 0));
259 /* check if device's identifiers match the driver's ones */
260 if ((id_table->uuid_low != afu_dev->id.uuid.uuid_low) ||
261 id_table->uuid_high !=
262 afu_dev->id.uuid.uuid_high)
272 ifpga_probe_one_driver(struct rte_afu_driver *drv,
273 struct rte_afu_device *afu_dev)
277 if (!rte_afu_match(drv, afu_dev))
278 /* Match of device and driver failed */
281 /* reference driver structure */
282 afu_dev->driver = drv;
284 /* call the driver probe() function */
285 ret = drv->probe(afu_dev);
287 afu_dev->driver = NULL;
289 afu_dev->device.driver = &drv->driver;
295 ifpga_probe_all_drivers(struct rte_afu_device *afu_dev)
297 struct rte_afu_driver *drv = NULL;
303 /* Check if a driver is already loaded */
304 if (rte_dev_is_probed(&afu_dev->device)) {
305 IFPGA_BUS_DEBUG("Device %s is already probed\n",
306 rte_ifpga_device_name(afu_dev));
310 TAILQ_FOREACH(drv, &ifpga_afu_drv_list, next) {
311 if (ifpga_probe_one_driver(drv, afu_dev)) {
320 * Scan the content of the Intel FPGA bus, and call the probe() function for
321 * all registered drivers that have a matching entry in its id_table
322 * for discovered devices.
327 struct rte_afu_device *afu_dev = NULL;
330 TAILQ_FOREACH(afu_dev, &ifpga_afu_dev_list, next) {
331 ret = ifpga_probe_all_drivers(afu_dev);
335 IFPGA_BUS_ERR("failed to initialize %s device\n",
336 rte_ifpga_device_name(afu_dev));
343 ifpga_plug(struct rte_device *dev)
345 return ifpga_probe_all_drivers(RTE_DEV_TO_AFU(dev));
349 ifpga_remove_driver(struct rte_afu_device *afu_dev)
353 name = rte_ifpga_device_name(afu_dev);
354 if (afu_dev->driver == NULL) {
355 IFPGA_BUS_DEBUG("no driver attach to device %s\n", name);
359 return afu_dev->driver->remove(afu_dev);
363 ifpga_unplug(struct rte_device *dev)
365 struct rte_afu_device *afu_dev = NULL;
371 afu_dev = RTE_DEV_TO_AFU(dev);
375 ret = ifpga_remove_driver(afu_dev);
379 TAILQ_REMOVE(&ifpga_afu_dev_list, afu_dev, next);
381 rte_devargs_remove(dev->devargs);
387 static struct rte_device *
388 ifpga_find_device(const struct rte_device *start,
389 rte_dev_cmp_t cmp, const void *data)
391 struct rte_afu_device *afu_dev;
393 TAILQ_FOREACH(afu_dev, &ifpga_afu_dev_list, next) {
394 if (start && &afu_dev->device == start) {
398 if (cmp(&afu_dev->device, data) == 0)
399 return &afu_dev->device;
405 ifpga_parse(const char *name, void *addr)
408 struct rte_rawdev *rawdev = NULL;
409 char rawdev_name[RTE_RAWDEV_NAME_MAX_LEN];
412 int port = IFPGA_BUS_DEV_PORT_MAX;
414 int str_port_len = 0;
417 memset(str_port, 0, 8);
418 c1 = strchr(name, '|');
420 str_port_len = c1 - name;
424 if (str_port_len < 8 &&
426 memcpy(str_port, name, str_port_len);
427 ret = sscanf(str_port, "%d", &port);
432 memset(rawdev_name, 0, sizeof(rawdev_name));
433 snprintf(rawdev_name, RTE_RAWDEV_NAME_MAX_LEN, "IFPGA:%s", c2);
434 rawdev = rte_rawdev_pmd_get_named_dev(rawdev_name);
436 if ((port < IFPGA_BUS_DEV_PORT_MAX) &&
441 if ((port < IFPGA_BUS_DEV_PORT_MAX) &&
448 static struct rte_bus rte_ifpga_bus = {
450 .probe = ifpga_probe,
451 .find_device = ifpga_find_device,
453 .unplug = ifpga_unplug,
454 .parse = ifpga_parse,
457 RTE_REGISTER_BUS(IFPGA_BUS_NAME, rte_ifpga_bus);
459 RTE_INIT(ifpga_init_log)
461 ifpga_bus_logtype = rte_log_register("bus.ifpga");
462 if (ifpga_bus_logtype >= 0)
463 rte_log_set_level(ifpga_bus_logtype, RTE_LOG_NOTICE);