1 /* SPDX-License-Identifier: BSD-3-Clause
14 #include <sys/types.h>
15 #include <sys/syscall.h>
17 #include <rte_byteorder.h>
18 #include <rte_common.h>
19 #include <rte_interrupts.h>
21 #include <rte_debug.h>
22 #include <rte_atomic.h>
23 #include <rte_branch_prediction.h>
24 #include <rte_memory.h>
25 #include <rte_tailq.h>
27 #include <rte_alarm.h>
28 #include <rte_ether.h>
29 #include <rte_ethdev_driver.h>
30 #include <rte_malloc.h>
33 #include <rte_mbuf_pool_ops.h>
35 #include <rte_dpaa_bus.h>
36 #include <rte_dpaa_logs.h>
45 int dpaa_logtype_mempool;
47 int dpaa_logtype_eventdev;
49 struct rte_dpaa_bus rte_dpaa_bus;
50 struct netcfg_info *dpaa_netcfg;
52 /* define a variable to hold the portal_key, once created.*/
53 static pthread_key_t dpaa_portal_key;
55 unsigned int dpaa_svr_family;
57 #define FSL_DPAA_BUS_NAME dpaa_bus
59 RTE_DEFINE_PER_LCORE(bool, dpaa_io);
60 RTE_DEFINE_PER_LCORE(struct dpaa_portal_dqrr, held_bufs);
63 compare_dpaa_devices(struct rte_dpaa_device *dev1,
64 struct rte_dpaa_device *dev2)
68 /* Segragating ETH from SEC devices */
69 if (dev1->device_type > dev2->device_type)
71 else if (dev1->device_type < dev2->device_type)
76 if ((comp != 0) || (dev1->device_type != FSL_DPAA_ETH))
79 if (dev1->id.fman_id > dev2->id.fman_id) {
81 } else if (dev1->id.fman_id < dev2->id.fman_id) {
84 /* FMAN ids match, check for mac_id */
85 if (dev1->id.mac_id > dev2->id.mac_id)
87 else if (dev1->id.mac_id < dev2->id.mac_id)
97 dpaa_add_to_device_list(struct rte_dpaa_device *newdev)
99 int comp, inserted = 0;
100 struct rte_dpaa_device *dev = NULL;
101 struct rte_dpaa_device *tdev = NULL;
103 TAILQ_FOREACH_SAFE(dev, &rte_dpaa_bus.device_list, next, tdev) {
104 comp = compare_dpaa_devices(newdev, dev);
106 TAILQ_INSERT_BEFORE(dev, newdev, next);
113 TAILQ_INSERT_TAIL(&rte_dpaa_bus.device_list, newdev, next);
117 * Reads the SEC device from DTS
118 * Returns -1 if SEC devices not available, 0 otherwise
121 dpaa_sec_available(void)
123 const struct device_node *caam_node;
125 for_each_compatible_node(caam_node, NULL, "fsl,sec-v4.0") {
132 static void dpaa_clean_device_list(void);
134 static struct rte_devargs *
135 dpaa_devargs_lookup(struct rte_dpaa_device *dev)
137 struct rte_devargs *devargs;
140 RTE_EAL_DEVARGS_FOREACH("dpaa_bus", devargs) {
141 devargs->bus->parse(devargs->name, &dev_name);
142 if (strcmp(dev_name, dev->device.name) == 0) {
143 DPAA_BUS_INFO("**Devargs matched %s", dev_name);
151 dpaa_create_device_list(void)
155 struct rte_dpaa_device *dev;
156 struct fm_eth_port_cfg *cfg;
157 struct fman_if *fman_intf;
159 /* Creating Ethernet Devices */
160 for (i = 0; i < dpaa_netcfg->num_ethports; i++) {
161 dev = calloc(1, sizeof(struct rte_dpaa_device));
163 DPAA_BUS_LOG(ERR, "Failed to allocate ETH devices");
168 cfg = &dpaa_netcfg->port_cfg[i];
169 fman_intf = cfg->fman_if;
171 /* Device identifiers */
172 dev->id.fman_id = fman_intf->fman_idx + 1;
173 dev->id.mac_id = fman_intf->mac_idx;
174 dev->device_type = FSL_DPAA_ETH;
177 /* Create device name */
178 memset(dev->name, 0, RTE_ETH_NAME_MAX_LEN);
179 sprintf(dev->name, "fm%d-mac%d", (fman_intf->fman_idx + 1),
181 DPAA_BUS_LOG(INFO, "%s netdev added", dev->name);
182 dev->device.name = dev->name;
183 dev->device.devargs = dpaa_devargs_lookup(dev);
185 dpaa_add_to_device_list(dev);
188 rte_dpaa_bus.device_count = i;
190 /* Unlike case of ETH, RTE_LIBRTE_DPAA_MAX_CRYPTODEV SEC devices are
191 * constantly created only if "sec" property is found in the device
192 * tree. Logically there is no limit for number of devices (QI
193 * interfaces) that can be created.
196 if (dpaa_sec_available()) {
197 DPAA_BUS_LOG(INFO, "DPAA SEC devices are not available");
201 /* Creating SEC Devices */
202 for (i = 0; i < RTE_LIBRTE_DPAA_MAX_CRYPTODEV; i++) {
203 dev = calloc(1, sizeof(struct rte_dpaa_device));
205 DPAA_BUS_LOG(ERR, "Failed to allocate SEC devices");
210 dev->device_type = FSL_DPAA_CRYPTO;
211 dev->id.dev_id = rte_dpaa_bus.device_count + i;
213 /* Even though RTE_CRYPTODEV_NAME_MAX_LEN is valid length of
214 * crypto PMD, using RTE_ETH_NAME_MAX_LEN as that is the size
215 * allocated for dev->name/
217 memset(dev->name, 0, RTE_ETH_NAME_MAX_LEN);
218 sprintf(dev->name, "dpaa-sec%d", i);
219 DPAA_BUS_LOG(INFO, "%s cryptodev added", dev->name);
220 dev->device.name = dev->name;
221 dev->device.devargs = dpaa_devargs_lookup(dev);
223 dpaa_add_to_device_list(dev);
226 rte_dpaa_bus.device_count += i;
231 dpaa_clean_device_list();
236 dpaa_clean_device_list(void)
238 struct rte_dpaa_device *dev = NULL;
239 struct rte_dpaa_device *tdev = NULL;
241 TAILQ_FOREACH_SAFE(dev, &rte_dpaa_bus.device_list, next, tdev) {
242 TAILQ_REMOVE(&rte_dpaa_bus.device_list, dev, next);
248 int rte_dpaa_portal_init(void *arg)
252 uint32_t cpu = rte_lcore_id();
254 struct dpaa_portal *dpaa_io_portal;
256 BUS_INIT_FUNC_TRACE();
258 if ((size_t)arg == 1 || cpu == LCORE_ID_ANY)
259 cpu = rte_get_master_lcore();
260 /* if the core id is not supported */
262 if (cpu >= RTE_MAX_LCORE)
265 /* Set CPU affinity for this thread */
267 CPU_SET(cpu, &cpuset);
269 ret = pthread_setaffinity_np(id, sizeof(cpu_set_t), &cpuset);
271 DPAA_BUS_LOG(ERR, "pthread_setaffinity_np failed on "
272 "core :%d with ret: %d", cpu, ret);
276 /* Initialise bman thread portals */
277 ret = bman_thread_init();
279 DPAA_BUS_LOG(ERR, "bman_thread_init failed on "
280 "core %d with ret: %d", cpu, ret);
284 DPAA_BUS_LOG(DEBUG, "BMAN thread initialized");
286 /* Initialise qman thread portals */
287 ret = qman_thread_init();
289 DPAA_BUS_LOG(ERR, "bman_thread_init failed on "
290 "core %d with ret: %d", cpu, ret);
291 bman_thread_finish();
295 DPAA_BUS_LOG(DEBUG, "QMAN thread initialized");
297 dpaa_io_portal = rte_malloc(NULL, sizeof(struct dpaa_portal),
298 RTE_CACHE_LINE_SIZE);
299 if (!dpaa_io_portal) {
300 DPAA_BUS_LOG(ERR, "Unable to allocate memory");
301 bman_thread_finish();
302 qman_thread_finish();
306 dpaa_io_portal->qman_idx = qman_get_portal_index();
307 dpaa_io_portal->bman_idx = bman_get_portal_index();
308 dpaa_io_portal->tid = syscall(SYS_gettid);
310 ret = pthread_setspecific(dpaa_portal_key, (void *)dpaa_io_portal);
312 DPAA_BUS_LOG(ERR, "pthread_setspecific failed on "
313 "core %d with ret: %d", cpu, ret);
314 dpaa_portal_finish(NULL);
319 RTE_PER_LCORE(dpaa_io) = true;
321 DPAA_BUS_LOG(DEBUG, "QMAN thread initialized");
327 rte_dpaa_portal_fq_init(void *arg, struct qman_fq *fq)
329 /* Affine above created portal with channel*/
331 struct qman_portal *qp;
334 if (unlikely(!RTE_PER_LCORE(dpaa_io))) {
335 ret = rte_dpaa_portal_init(arg);
337 DPAA_BUS_LOG(ERR, "portal initialization failure");
342 /* Initialise qman specific portals */
343 qp = fsl_qman_portal_create();
345 DPAA_BUS_LOG(ERR, "Unable to alloc fq portal");
349 sdqcr = QM_SDQCR_CHANNELS_POOL_CONV(fq->ch_id);
350 qman_static_dequeue_add(sdqcr, qp);
355 int rte_dpaa_portal_fq_close(struct qman_fq *fq)
357 return fsl_qman_portal_destroy(fq->qp);
361 dpaa_portal_finish(void *arg)
363 struct dpaa_portal *dpaa_io_portal = (struct dpaa_portal *)arg;
365 if (!dpaa_io_portal) {
366 DPAA_BUS_LOG(DEBUG, "Portal already cleaned");
370 bman_thread_finish();
371 qman_thread_finish();
373 pthread_setspecific(dpaa_portal_key, NULL);
375 rte_free(dpaa_io_portal);
376 dpaa_io_portal = NULL;
378 RTE_PER_LCORE(dpaa_io) = false;
382 rte_dpaa_bus_parse(const char *name, void *out_name)
385 int max_fman = 2, max_macs = 16;
386 char *sep = strchr(name, ':');
388 if (strncmp(name, RTE_STR(FSL_DPAA_BUS_NAME),
389 strlen(RTE_STR(FSL_DPAA_BUS_NAME)))) {
394 DPAA_BUS_ERR("Incorrect device name observed");
398 sep = (char *) (sep + 1);
400 for (i = 0; i < max_fman; i++) {
401 for (j = 0; j < max_macs; j++) {
403 snprintf(fm_name, 16, "fm%d-mac%d", i, j);
404 if (strcmp(fm_name, sep) == 0) {
406 strcpy(out_name, sep);
412 for (i = 0; i < RTE_LIBRTE_DPAA_MAX_CRYPTODEV; i++) {
415 snprintf(sec_name, 16, "dpaa-sec%d", i);
416 if (strcmp(sec_name, sep) == 0) {
418 strcpy(out_name, sep);
426 #define DPAA_DEV_PATH1 "/sys/devices/platform/soc/soc:fsl,dpaa"
427 #define DPAA_DEV_PATH2 "/sys/devices/platform/fsl,dpaa"
430 rte_dpaa_bus_scan(void)
434 BUS_INIT_FUNC_TRACE();
436 if ((access(DPAA_DEV_PATH1, F_OK) != 0) &&
437 (access(DPAA_DEV_PATH2, F_OK) != 0)) {
438 RTE_LOG(DEBUG, EAL, "DPAA Bus not present. Skipping.\n");
442 /* Load the device-tree driver */
445 DPAA_BUS_LOG(ERR, "of_init failed with ret: %d", ret);
449 /* Get the interface configurations from device-tree */
450 dpaa_netcfg = netcfg_acquire();
452 DPAA_BUS_LOG(ERR, "netcfg_acquire failed");
456 RTE_LOG(NOTICE, EAL, "DPAA Bus Detected\n");
458 if (!dpaa_netcfg->num_ethports) {
459 DPAA_BUS_LOG(INFO, "no network interfaces available");
460 /* This is not an error */
464 #ifdef RTE_LIBRTE_DPAA_DEBUG_DRIVER
465 dump_netcfg(dpaa_netcfg);
468 DPAA_BUS_LOG(DEBUG, "Number of ethernet devices = %d",
469 dpaa_netcfg->num_ethports);
470 ret = dpaa_create_device_list();
472 DPAA_BUS_LOG(ERR, "Unable to create device list. (%d)", ret);
476 /* create the key, supplying a function that'll be invoked
477 * when a portal affined thread will be deleted.
479 ret = pthread_key_create(&dpaa_portal_key, dpaa_portal_finish);
481 DPAA_BUS_LOG(DEBUG, "Unable to create pthread key. (%d)", ret);
482 dpaa_clean_device_list();
489 /* register a dpaa bus based dpaa driver */
491 rte_dpaa_driver_register(struct rte_dpaa_driver *driver)
495 BUS_INIT_FUNC_TRACE();
497 TAILQ_INSERT_TAIL(&rte_dpaa_bus.driver_list, driver, next);
498 /* Update Bus references */
499 driver->dpaa_bus = &rte_dpaa_bus;
502 /* un-register a dpaa bus based dpaa driver */
504 rte_dpaa_driver_unregister(struct rte_dpaa_driver *driver)
506 struct rte_dpaa_bus *dpaa_bus;
508 BUS_INIT_FUNC_TRACE();
510 dpaa_bus = driver->dpaa_bus;
512 TAILQ_REMOVE(&dpaa_bus->driver_list, driver, next);
513 /* Update Bus references */
514 driver->dpaa_bus = NULL;
518 rte_dpaa_device_match(struct rte_dpaa_driver *drv,
519 struct rte_dpaa_device *dev)
522 DPAA_BUS_DEBUG("Invalid drv or dev received.");
526 if (drv->drv_type == dev->device_type)
533 rte_dpaa_bus_probe(void)
536 struct rte_dpaa_device *dev;
537 struct rte_dpaa_driver *drv;
538 FILE *svr_file = NULL;
539 unsigned int svr_ver;
540 int probe_all = rte_dpaa_bus.bus.conf.scan_mode != RTE_BUS_SCAN_WHITELIST;
542 svr_file = fopen(DPAA_SOC_ID_FILE, "r");
544 if (fscanf(svr_file, "svr:%x", &svr_ver) > 0)
545 dpaa_svr_family = svr_ver & SVR_MASK;
549 /* For each registered driver, and device, call the driver->probe */
550 TAILQ_FOREACH(dev, &rte_dpaa_bus.device_list, next) {
551 TAILQ_FOREACH(drv, &rte_dpaa_bus.driver_list, next) {
552 ret = rte_dpaa_device_match(drv, dev);
557 (dev->device.devargs &&
558 dev->device.devargs->policy == RTE_DEV_BLACKLISTED))
562 (dev->device.devargs &&
563 dev->device.devargs->policy ==
564 RTE_DEV_WHITELISTED)) {
565 ret = drv->probe(drv, dev);
567 DPAA_BUS_ERR("Unable to probe.\n");
573 /* Register DPAA mempool ops only if any DPAA device has
576 if (!TAILQ_EMPTY(&rte_dpaa_bus.device_list))
577 rte_mbuf_set_platform_mempool_ops(DPAA_MEMPOOL_OPS_NAME);
582 static struct rte_device *
583 rte_dpaa_find_device(const struct rte_device *start, rte_dev_cmp_t cmp,
586 struct rte_dpaa_device *dev;
588 TAILQ_FOREACH(dev, &rte_dpaa_bus.device_list, next) {
589 if (start && &dev->device == start) {
590 start = NULL; /* starting point found */
594 if (cmp(&dev->device, data) == 0)
602 * Get iommu class of DPAA2 devices on the bus.
604 static enum rte_iova_mode
605 rte_dpaa_get_iommu_class(void)
607 if ((access(DPAA_DEV_PATH1, F_OK) != 0) &&
608 (access(DPAA_DEV_PATH2, F_OK) != 0)) {
614 struct rte_dpaa_bus rte_dpaa_bus = {
616 .scan = rte_dpaa_bus_scan,
617 .probe = rte_dpaa_bus_probe,
618 .parse = rte_dpaa_bus_parse,
619 .find_device = rte_dpaa_find_device,
620 .get_iommu_class = rte_dpaa_get_iommu_class,
622 .device_list = TAILQ_HEAD_INITIALIZER(rte_dpaa_bus.device_list),
623 .driver_list = TAILQ_HEAD_INITIALIZER(rte_dpaa_bus.driver_list),
627 RTE_REGISTER_BUS(FSL_DPAA_BUS_NAME, rte_dpaa_bus.bus);
629 RTE_INIT(dpaa_init_log)
631 dpaa_logtype_bus = rte_log_register("bus.dpaa");
632 if (dpaa_logtype_bus >= 0)
633 rte_log_set_level(dpaa_logtype_bus, RTE_LOG_NOTICE);
635 dpaa_logtype_mempool = rte_log_register("mempool.dpaa");
636 if (dpaa_logtype_mempool >= 0)
637 rte_log_set_level(dpaa_logtype_mempool, RTE_LOG_NOTICE);
639 dpaa_logtype_pmd = rte_log_register("pmd.net.dpaa");
640 if (dpaa_logtype_pmd >= 0)
641 rte_log_set_level(dpaa_logtype_pmd, RTE_LOG_NOTICE);
643 dpaa_logtype_eventdev = rte_log_register("pmd.event.dpaa");
644 if (dpaa_logtype_eventdev >= 0)
645 rte_log_set_level(dpaa_logtype_eventdev, RTE_LOG_NOTICE);