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.devargs = devargs;
146 afu_dev->device.numa_node = SOCKET_ID_ANY;
147 afu_dev->device.name = devargs->name;
148 afu_dev->rawdev = rawdev;
149 afu_dev->id.uuid.uuid_low = 0;
150 afu_dev->id.uuid.uuid_high = 0;
151 afu_dev->id.port = afu_pr_conf.afu_id.port;
153 if (rawdev->dev_ops && rawdev->dev_ops->dev_info_get)
154 rawdev->dev_ops->dev_info_get(rawdev, afu_dev);
156 if (rawdev->dev_ops &&
157 rawdev->dev_ops->dev_start &&
158 rawdev->dev_ops->dev_start(rawdev))
161 strlcpy(afu_pr_conf.bs_path, path, sizeof(afu_pr_conf.bs_path));
162 if (rawdev->dev_ops &&
163 rawdev->dev_ops->firmware_load &&
164 rawdev->dev_ops->firmware_load(rawdev,
166 IFPGA_BUS_ERR("firmware load error %d\n", ret);
169 afu_dev->id.uuid.uuid_low = afu_pr_conf.afu_id.uuid.uuid_low;
170 afu_dev->id.uuid.uuid_high = afu_pr_conf.afu_id.uuid.uuid_high;
172 rte_kvargs_free(kvlist);
178 rte_kvargs_free(kvlist);
188 * Scan the content of the FPGA bus, and the devices in the devices
194 struct rte_devargs *devargs;
195 struct rte_kvargs *kvlist = NULL;
196 struct rte_rawdev *rawdev = NULL;
198 char name1[RTE_RAWDEV_NAME_MAX_LEN];
199 struct rte_afu_device *afu_dev = NULL;
201 /* for FPGA devices we scan the devargs_list populated via cmdline */
202 RTE_EAL_DEVARGS_FOREACH(IFPGA_ARG_NAME, devargs) {
203 if (devargs->bus != &rte_ifpga_bus)
206 kvlist = rte_kvargs_parse(devargs->args, valid_args);
208 IFPGA_BUS_ERR("error when parsing param");
212 if (rte_kvargs_count(kvlist, IFPGA_ARG_NAME) == 1) {
213 if (rte_kvargs_process(kvlist, IFPGA_ARG_NAME,
214 &rte_ifpga_get_string_arg, &name) < 0) {
215 IFPGA_BUS_ERR("error to parse %s",
220 IFPGA_BUS_ERR("arg %s is mandatory for ifpga bus",
225 memset(name1, 0, sizeof(name1));
226 snprintf(name1, RTE_RAWDEV_NAME_MAX_LEN, "IFPGA:%s", name);
228 rawdev = rte_rawdev_pmd_get_named_dev(name1);
232 afu_dev = ifpga_scan_one(rawdev, devargs);
234 TAILQ_INSERT_TAIL(&ifpga_afu_dev_list, afu_dev, next);
239 rte_kvargs_free(kvlist);
247 * Match the AFU Driver and AFU Device using the ID Table
250 rte_afu_match(const struct rte_afu_driver *afu_drv,
251 const struct rte_afu_device *afu_dev)
253 const struct rte_afu_uuid *id_table;
255 for (id_table = afu_drv->id_table;
256 ((id_table->uuid_low != 0) && (id_table->uuid_high != 0));
258 /* check if device's identifiers match the driver's ones */
259 if ((id_table->uuid_low != afu_dev->id.uuid.uuid_low) ||
260 id_table->uuid_high !=
261 afu_dev->id.uuid.uuid_high)
271 ifpga_probe_one_driver(struct rte_afu_driver *drv,
272 struct rte_afu_device *afu_dev)
276 if (!rte_afu_match(drv, afu_dev))
277 /* Match of device and driver failed */
280 /* reference driver structure */
281 afu_dev->driver = drv;
282 afu_dev->device.driver = &drv->driver;
284 /* call the driver probe() function */
285 ret = drv->probe(afu_dev);
287 afu_dev->driver = NULL;
288 afu_dev->device.driver = NULL;
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 (afu_dev->driver != NULL)
307 TAILQ_FOREACH(drv, &ifpga_afu_drv_list, next) {
308 if (ifpga_probe_one_driver(drv, afu_dev)) {
317 * Scan the content of the Intel FPGA bus, and call the probe() function for
318 * all registered drivers that have a matching entry in its id_table
319 * for discovered devices.
324 struct rte_afu_device *afu_dev = NULL;
327 TAILQ_FOREACH(afu_dev, &ifpga_afu_dev_list, next) {
328 if (afu_dev->device.driver)
331 ret = ifpga_probe_all_drivers(afu_dev);
333 IFPGA_BUS_ERR("failed to initialize %s device\n",
334 rte_ifpga_device_name(afu_dev));
341 ifpga_plug(struct rte_device *dev)
343 return ifpga_probe_all_drivers(RTE_DEV_TO_AFU(dev));
347 ifpga_remove_driver(struct rte_afu_device *afu_dev)
350 const struct rte_afu_driver *driver;
352 name = rte_ifpga_device_name(afu_dev);
353 if (!afu_dev->device.driver) {
354 IFPGA_BUS_DEBUG("no driver attach to device %s\n", name);
358 driver = RTE_DRV_TO_AFU_CONST(afu_dev->device.driver);
359 return driver->remove(afu_dev);
363 ifpga_unplug(struct rte_device *dev)
365 struct rte_afu_device *afu_dev = NULL;
366 struct rte_devargs *devargs = NULL;
372 afu_dev = RTE_DEV_TO_AFU(dev);
376 devargs = dev->devargs;
378 ret = ifpga_remove_driver(afu_dev);
382 TAILQ_REMOVE(&ifpga_afu_dev_list, afu_dev, next);
384 rte_devargs_remove(devargs->bus->name, devargs->name);
390 static struct rte_device *
391 ifpga_find_device(const struct rte_device *start,
392 rte_dev_cmp_t cmp, const void *data)
394 struct rte_afu_device *afu_dev;
396 TAILQ_FOREACH(afu_dev, &ifpga_afu_dev_list, next) {
397 if (start && &afu_dev->device == start) {
401 if (cmp(&afu_dev->device, data) == 0)
402 return &afu_dev->device;
408 ifpga_parse(const char *name, void *addr)
411 struct rte_rawdev *rawdev = NULL;
412 char rawdev_name[RTE_RAWDEV_NAME_MAX_LEN];
415 int port = IFPGA_BUS_DEV_PORT_MAX;
417 int str_port_len = 0;
420 memset(str_port, 0, 8);
421 c1 = strchr(name, '|');
423 str_port_len = c1 - name;
427 if (str_port_len < 8 &&
429 memcpy(str_port, name, str_port_len);
430 ret = sscanf(str_port, "%d", &port);
435 memset(rawdev_name, 0, sizeof(rawdev_name));
436 snprintf(rawdev_name, RTE_RAWDEV_NAME_MAX_LEN, "IFPGA:%s", c2);
437 rawdev = rte_rawdev_pmd_get_named_dev(rawdev_name);
439 if ((port < IFPGA_BUS_DEV_PORT_MAX) &&
444 if ((port < IFPGA_BUS_DEV_PORT_MAX) &&
451 static struct rte_bus rte_ifpga_bus = {
453 .probe = ifpga_probe,
454 .find_device = ifpga_find_device,
456 .unplug = ifpga_unplug,
457 .parse = ifpga_parse,
460 RTE_REGISTER_BUS(IFPGA_BUS_NAME, rte_ifpga_bus);
462 RTE_INIT(ifpga_init_log)
464 ifpga_bus_logtype = rte_log_register("bus.ifpga");
465 if (ifpga_bus_logtype >= 0)
466 rte_log_set_level(ifpga_bus_logtype, RTE_LOG_NOTICE);