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->firmware_load &&
163 rawdev->dev_ops->firmware_load(rawdev,
165 IFPGA_BUS_ERR("firmware load error %d\n", ret);
168 afu_dev->id.uuid.uuid_low = afu_pr_conf.afu_id.uuid.uuid_low;
169 afu_dev->id.uuid.uuid_high = afu_pr_conf.afu_id.uuid.uuid_high;
171 rte_kvargs_free(kvlist);
177 rte_kvargs_free(kvlist);
187 * Scan the content of the FPGA bus, and the devices in the devices
193 struct rte_devargs *devargs;
194 struct rte_kvargs *kvlist = NULL;
195 struct rte_rawdev *rawdev = NULL;
197 char name1[RTE_RAWDEV_NAME_MAX_LEN];
198 struct rte_afu_device *afu_dev = NULL;
200 /* for FPGA devices we scan the devargs_list populated via cmdline */
201 RTE_EAL_DEVARGS_FOREACH(IFPGA_ARG_NAME, devargs) {
202 if (devargs->bus != &rte_ifpga_bus)
205 kvlist = rte_kvargs_parse(devargs->args, valid_args);
207 IFPGA_BUS_ERR("error when parsing param");
211 if (rte_kvargs_count(kvlist, IFPGA_ARG_NAME) == 1) {
212 if (rte_kvargs_process(kvlist, IFPGA_ARG_NAME,
213 &rte_ifpga_get_string_arg, &name) < 0) {
214 IFPGA_BUS_ERR("error to parse %s",
219 IFPGA_BUS_ERR("arg %s is mandatory for ifpga bus",
224 memset(name1, 0, sizeof(name1));
225 snprintf(name1, RTE_RAWDEV_NAME_MAX_LEN, "IFPGA:%s", name);
227 rawdev = rte_rawdev_pmd_get_named_dev(name1);
231 afu_dev = ifpga_scan_one(rawdev, devargs);
233 TAILQ_INSERT_TAIL(&ifpga_afu_dev_list, afu_dev, next);
238 rte_kvargs_free(kvlist);
246 * Match the AFU Driver and AFU Device using the ID Table
249 rte_afu_match(const struct rte_afu_driver *afu_drv,
250 const struct rte_afu_device *afu_dev)
252 const struct rte_afu_uuid *id_table;
254 for (id_table = afu_drv->id_table;
255 ((id_table->uuid_low != 0) && (id_table->uuid_high != 0));
257 /* check if device's identifiers match the driver's ones */
258 if ((id_table->uuid_low != afu_dev->id.uuid.uuid_low) ||
259 id_table->uuid_high !=
260 afu_dev->id.uuid.uuid_high)
270 ifpga_probe_one_driver(struct rte_afu_driver *drv,
271 struct rte_afu_device *afu_dev)
275 if (!rte_afu_match(drv, afu_dev))
276 /* Match of device and driver failed */
279 /* reference driver structure */
280 afu_dev->driver = drv;
281 afu_dev->device.driver = &drv->driver;
283 /* call the driver probe() function */
284 ret = drv->probe(afu_dev);
286 afu_dev->driver = NULL;
287 afu_dev->device.driver = NULL;
294 ifpga_probe_all_drivers(struct rte_afu_device *afu_dev)
296 struct rte_afu_driver *drv = NULL;
302 /* Check if a driver is already loaded */
303 if (afu_dev->driver != NULL)
306 TAILQ_FOREACH(drv, &ifpga_afu_drv_list, next) {
307 if (ifpga_probe_one_driver(drv, afu_dev)) {
316 * Scan the content of the Intel FPGA bus, and call the probe() function for
317 * all registered drivers that have a matching entry in its id_table
318 * for discovered devices.
323 struct rte_afu_device *afu_dev = NULL;
326 TAILQ_FOREACH(afu_dev, &ifpga_afu_dev_list, next) {
327 if (afu_dev->device.driver)
330 ret = ifpga_probe_all_drivers(afu_dev);
332 IFPGA_BUS_ERR("failed to initialize %s device\n",
333 rte_ifpga_device_name(afu_dev));
340 ifpga_plug(struct rte_device *dev)
342 return ifpga_probe_all_drivers(RTE_DEV_TO_AFU(dev));
346 ifpga_remove_driver(struct rte_afu_device *afu_dev)
349 const struct rte_afu_driver *driver;
351 name = rte_ifpga_device_name(afu_dev);
352 if (!afu_dev->device.driver) {
353 IFPGA_BUS_DEBUG("no driver attach to device %s\n", name);
357 driver = RTE_DRV_TO_AFU_CONST(afu_dev->device.driver);
358 return driver->remove(afu_dev);
362 ifpga_unplug(struct rte_device *dev)
364 struct rte_afu_device *afu_dev = NULL;
365 struct rte_devargs *devargs = NULL;
371 afu_dev = RTE_DEV_TO_AFU(dev);
375 devargs = dev->devargs;
377 ret = ifpga_remove_driver(afu_dev);
381 TAILQ_REMOVE(&ifpga_afu_dev_list, afu_dev, next);
383 rte_devargs_remove(devargs->bus->name, devargs->name);
389 static struct rte_device *
390 ifpga_find_device(const struct rte_device *start,
391 rte_dev_cmp_t cmp, const void *data)
393 struct rte_afu_device *afu_dev;
395 TAILQ_FOREACH(afu_dev, &ifpga_afu_dev_list, next) {
396 if (start && &afu_dev->device == start) {
400 if (cmp(&afu_dev->device, data) == 0)
401 return &afu_dev->device;
407 ifpga_parse(const char *name, void *addr)
410 struct rte_rawdev *rawdev = NULL;
411 char rawdev_name[RTE_RAWDEV_NAME_MAX_LEN];
414 int port = IFPGA_BUS_DEV_PORT_MAX;
416 int str_port_len = 0;
419 memset(str_port, 0, 8);
420 c1 = strchr(name, '|');
422 str_port_len = c1 - name;
426 if (str_port_len < 8 &&
428 memcpy(str_port, name, str_port_len);
429 ret = sscanf(str_port, "%d", &port);
434 memset(rawdev_name, 0, sizeof(rawdev_name));
435 snprintf(rawdev_name, RTE_RAWDEV_NAME_MAX_LEN, "IFPGA:%s", c2);
436 rawdev = rte_rawdev_pmd_get_named_dev(rawdev_name);
438 if ((port < IFPGA_BUS_DEV_PORT_MAX) &&
443 if ((port < IFPGA_BUS_DEV_PORT_MAX) &&
450 static struct rte_bus rte_ifpga_bus = {
452 .probe = ifpga_probe,
453 .find_device = ifpga_find_device,
455 .unplug = ifpga_unplug,
456 .parse = ifpga_parse,
459 RTE_REGISTER_BUS(IFPGA_BUS_NAME, rte_ifpga_bus);
461 RTE_INIT(ifpga_init_log)
463 ifpga_bus_logtype = rte_log_register("bus.ifpga");
464 if (ifpga_bus_logtype >= 0)
465 rte_log_set_level(ifpga_bus_logtype, RTE_LOG_NOTICE);