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 struct rte_afu_device
*
77 rte_ifpga_find_afu_by_name(const char *name
)
79 struct rte_afu_device
*afu_dev
= NULL
;
81 TAILQ_FOREACH(afu_dev
, &ifpga_afu_dev_list
, next
) {
83 !strcmp(afu_dev
->device
.name
, name
))
89 static const char * const valid_args
[] = {
90 #define IFPGA_ARG_NAME "ifpga"
92 #define IFPGA_ARG_PORT "port"
94 #define IFPGA_AFU_BTS "afu_bts"
100 * Scan the content of the FPGA bus, and the devices in the devices
103 static struct rte_afu_device
*
104 ifpga_scan_one(struct rte_rawdev
*rawdev
,
105 struct rte_devargs
*devargs
)
107 struct rte_kvargs
*kvlist
= NULL
;
108 struct rte_afu_device
*afu_dev
= NULL
;
109 struct rte_afu_pr_conf afu_pr_conf
;
113 memset(&afu_pr_conf
, 0, sizeof(struct rte_afu_pr_conf
));
115 kvlist
= rte_kvargs_parse(devargs
->args
, valid_args
);
117 IFPGA_BUS_ERR("error when parsing param");
121 if (rte_kvargs_count(kvlist
, IFPGA_ARG_PORT
) == 1) {
122 if (rte_kvargs_process(kvlist
, IFPGA_ARG_PORT
,
123 &rte_ifpga_get_integer32_arg
, &afu_pr_conf
.afu_id
.port
) < 0) {
124 IFPGA_BUS_ERR("error to parse %s",
129 IFPGA_BUS_ERR("arg %s is mandatory for ifpga bus",
134 if (rte_kvargs_count(kvlist
, IFPGA_AFU_BTS
) == 1) {
135 if (rte_kvargs_process(kvlist
, IFPGA_AFU_BTS
,
136 &rte_ifpga_get_string_arg
, &path
) < 0) {
137 IFPGA_BUS_ERR("Failed to parse %s",
141 afu_pr_conf
.pr_enable
= 1;
143 afu_pr_conf
.pr_enable
= 0;
146 afu_pr_conf
.afu_id
.uuid
.uuid_low
= 0;
147 afu_pr_conf
.afu_id
.uuid
.uuid_high
= 0;
149 if (ifpga_find_afu_dev(rawdev
, &afu_pr_conf
.afu_id
))
152 afu_dev
= calloc(1, sizeof(*afu_dev
));
156 afu_dev
->device
.bus
= &rte_ifpga_bus
;
157 afu_dev
->device
.devargs
= devargs
;
158 afu_dev
->device
.numa_node
= SOCKET_ID_ANY
;
159 afu_dev
->device
.name
= devargs
->name
;
160 afu_dev
->rawdev
= rawdev
;
161 afu_dev
->id
.uuid
.uuid_low
= 0;
162 afu_dev
->id
.uuid
.uuid_high
= 0;
163 afu_dev
->id
.port
= afu_pr_conf
.afu_id
.port
;
165 if (rawdev
->dev_ops
&& rawdev
->dev_ops
->dev_info_get
)
166 rawdev
->dev_ops
->dev_info_get(rawdev
, afu_dev
);
168 if (rawdev
->dev_ops
&&
169 rawdev
->dev_ops
->dev_start
&&
170 rawdev
->dev_ops
->dev_start(rawdev
))
173 strlcpy(afu_pr_conf
.bs_path
, path
, sizeof(afu_pr_conf
.bs_path
));
174 if (rawdev
->dev_ops
&&
175 rawdev
->dev_ops
->firmware_load
&&
176 rawdev
->dev_ops
->firmware_load(rawdev
,
178 IFPGA_BUS_ERR("firmware load error %d\n", ret
);
181 afu_dev
->id
.uuid
.uuid_low
= afu_pr_conf
.afu_id
.uuid
.uuid_low
;
182 afu_dev
->id
.uuid
.uuid_high
= afu_pr_conf
.afu_id
.uuid
.uuid_high
;
184 rte_kvargs_free(kvlist
);
190 rte_kvargs_free(kvlist
);
200 * Scan the content of the FPGA bus, and the devices in the devices
206 struct rte_devargs
*devargs
;
207 struct rte_kvargs
*kvlist
= NULL
;
208 struct rte_rawdev
*rawdev
= NULL
;
210 char name1
[RTE_RAWDEV_NAME_MAX_LEN
];
211 struct rte_afu_device
*afu_dev
= NULL
;
213 /* for FPGA devices we scan the devargs_list populated via cmdline */
214 RTE_EAL_DEVARGS_FOREACH(IFPGA_ARG_NAME
, devargs
) {
215 if (devargs
->bus
!= &rte_ifpga_bus
)
218 kvlist
= rte_kvargs_parse(devargs
->args
, valid_args
);
220 IFPGA_BUS_ERR("error when parsing param");
224 if (rte_kvargs_count(kvlist
, IFPGA_ARG_NAME
) == 1) {
225 if (rte_kvargs_process(kvlist
, IFPGA_ARG_NAME
,
226 &rte_ifpga_get_string_arg
, &name
) < 0) {
227 IFPGA_BUS_ERR("error to parse %s",
232 IFPGA_BUS_ERR("arg %s is mandatory for ifpga bus",
237 memset(name1
, 0, sizeof(name1
));
238 snprintf(name1
, RTE_RAWDEV_NAME_MAX_LEN
, "IFPGA:%s", name
);
240 rawdev
= rte_rawdev_pmd_get_named_dev(name1
);
244 afu_dev
= ifpga_scan_one(rawdev
, devargs
);
246 TAILQ_INSERT_TAIL(&ifpga_afu_dev_list
, afu_dev
, next
);
251 rte_kvargs_free(kvlist
);
259 * Match the AFU Driver and AFU Device using the ID Table
262 rte_afu_match(const struct rte_afu_driver
*afu_drv
,
263 const struct rte_afu_device
*afu_dev
)
265 const struct rte_afu_uuid
*id_table
;
267 for (id_table
= afu_drv
->id_table
;
268 ((id_table
->uuid_low
!= 0) && (id_table
->uuid_high
!= 0));
270 /* check if device's identifiers match the driver's ones */
271 if ((id_table
->uuid_low
!= afu_dev
->id
.uuid
.uuid_low
) ||
272 id_table
->uuid_high
!=
273 afu_dev
->id
.uuid
.uuid_high
)
283 ifpga_probe_one_driver(struct rte_afu_driver
*drv
,
284 struct rte_afu_device
*afu_dev
)
288 if (!rte_afu_match(drv
, afu_dev
))
289 /* Match of device and driver failed */
292 /* reference driver structure */
293 afu_dev
->driver
= drv
;
295 /* call the driver probe() function */
296 ret
= drv
->probe(afu_dev
);
298 afu_dev
->driver
= NULL
;
300 afu_dev
->device
.driver
= &drv
->driver
;
306 ifpga_probe_all_drivers(struct rte_afu_device
*afu_dev
)
308 struct rte_afu_driver
*drv
= NULL
;
314 /* Check if a driver is already loaded */
315 if (rte_dev_is_probed(&afu_dev
->device
)) {
316 IFPGA_BUS_DEBUG("Device %s is already probed\n",
317 rte_ifpga_device_name(afu_dev
));
321 TAILQ_FOREACH(drv
, &ifpga_afu_drv_list
, next
) {
322 ret
= ifpga_probe_one_driver(drv
, afu_dev
);
324 /* negative value is an error */
327 /* positive value means driver doesn't support it */
331 if ((ret
> 0) && (afu_dev
->driver
== NULL
))
338 * Scan the content of the Intel FPGA bus, and call the probe() function for
339 * all registered drivers that have a matching entry in its id_table
340 * for discovered devices.
345 struct rte_afu_device
*afu_dev
= NULL
;
348 TAILQ_FOREACH(afu_dev
, &ifpga_afu_dev_list
, next
) {
349 ret
= ifpga_probe_all_drivers(afu_dev
);
353 IFPGA_BUS_ERR("failed to initialize %s device\n",
354 rte_ifpga_device_name(afu_dev
));
361 ifpga_plug(struct rte_device
*dev
)
363 return ifpga_probe_all_drivers(RTE_DEV_TO_AFU(dev
));
367 ifpga_remove_driver(struct rte_afu_device
*afu_dev
)
371 name
= rte_ifpga_device_name(afu_dev
);
372 if (afu_dev
->driver
== NULL
) {
373 IFPGA_BUS_DEBUG("no driver attach to device %s\n", name
);
377 return afu_dev
->driver
->remove(afu_dev
);
381 ifpga_unplug(struct rte_device
*dev
)
383 struct rte_afu_device
*afu_dev
= NULL
;
389 afu_dev
= RTE_DEV_TO_AFU(dev
);
393 ret
= ifpga_remove_driver(afu_dev
);
397 TAILQ_REMOVE(&ifpga_afu_dev_list
, afu_dev
, next
);
399 rte_devargs_remove(dev
->devargs
);
405 static struct rte_device
*
406 ifpga_find_device(const struct rte_device
*start
,
407 rte_dev_cmp_t cmp
, const void *data
)
409 struct rte_afu_device
*afu_dev
;
411 TAILQ_FOREACH(afu_dev
, &ifpga_afu_dev_list
, next
) {
412 if (start
&& &afu_dev
->device
== start
) {
416 if (cmp(&afu_dev
->device
, data
) == 0)
417 return &afu_dev
->device
;
423 ifpga_parse(const char *name
, void *addr
)
426 struct rte_rawdev
*rawdev
= NULL
;
427 char rawdev_name
[RTE_RAWDEV_NAME_MAX_LEN
];
430 int port
= IFPGA_BUS_DEV_PORT_MAX
;
432 int str_port_len
= 0;
435 memset(str_port
, 0, 8);
436 c1
= strchr(name
, '|');
438 str_port_len
= c1
- name
;
442 if (str_port_len
< 8 &&
444 memcpy(str_port
, name
, str_port_len
);
445 ret
= sscanf(str_port
, "%d", &port
);
450 memset(rawdev_name
, 0, sizeof(rawdev_name
));
451 snprintf(rawdev_name
, RTE_RAWDEV_NAME_MAX_LEN
, "IFPGA:%s", c2
);
452 rawdev
= rte_rawdev_pmd_get_named_dev(rawdev_name
);
454 if ((port
< IFPGA_BUS_DEV_PORT_MAX
) &&
459 if ((port
< IFPGA_BUS_DEV_PORT_MAX
) &&
466 static struct rte_bus rte_ifpga_bus
= {
468 .probe
= ifpga_probe
,
469 .find_device
= ifpga_find_device
,
471 .unplug
= ifpga_unplug
,
472 .parse
= ifpga_parse
,
475 RTE_REGISTER_BUS(IFPGA_BUS_NAME
, rte_ifpga_bus
);
477 RTE_INIT(ifpga_init_log
)
479 ifpga_bus_logtype
= rte_log_register("bus.ifpga");
480 if (ifpga_bus_logtype
>= 0)
481 rte_log_set_level(ifpga_bus_logtype
, RTE_LOG_NOTICE
);