1 /* SPDX-License-Identifier: BSD-3-Clause
3 * Copyright 2017-2020 NXP
14 #include <sys/types.h>
15 #include <sys/syscall.h>
16 #include <sys/eventfd.h>
18 #include <rte_byteorder.h>
19 #include <rte_common.h>
20 #include <rte_interrupts.h>
22 #include <rte_debug.h>
23 #include <rte_atomic.h>
24 #include <rte_branch_prediction.h>
25 #include <rte_memory.h>
26 #include <rte_tailq.h>
28 #include <rte_alarm.h>
29 #include <rte_ether.h>
30 #include <rte_ethdev_driver.h>
31 #include <rte_malloc.h>
34 #include <rte_mbuf_pool_ops.h>
35 #include <rte_mbuf_dyn.h>
38 #include <rte_dpaa_bus.h>
39 #include <rte_dpaa_logs.h>
40 #include <dpaax_iova_table.h>
47 static struct rte_dpaa_bus rte_dpaa_bus;
48 struct netcfg_info *dpaa_netcfg;
50 /* define a variable to hold the portal_key, once created.*/
51 static pthread_key_t dpaa_portal_key;
53 unsigned int dpaa_svr_family;
55 #define FSL_DPAA_BUS_NAME dpaa_bus
57 RTE_DEFINE_PER_LCORE(struct dpaa_portal *, dpaa_io);
59 #define DPAA_SEQN_DYNFIELD_NAME "dpaa_seqn_dynfield"
60 int dpaa_seqn_dynfield_offset = -1;
62 struct fm_eth_port_cfg *
63 dpaa_get_eth_port_cfg(int dev_id)
65 return &dpaa_netcfg->port_cfg[dev_id];
69 compare_dpaa_devices(struct rte_dpaa_device *dev1,
70 struct rte_dpaa_device *dev2)
74 /* Segragating ETH from SEC devices */
75 if (dev1->device_type > dev2->device_type)
77 else if (dev1->device_type < dev2->device_type)
82 if ((comp != 0) || (dev1->device_type != FSL_DPAA_ETH))
85 if (dev1->id.fman_id > dev2->id.fman_id) {
87 } else if (dev1->id.fman_id < dev2->id.fman_id) {
90 /* FMAN ids match, check for mac_id */
91 if (dev1->id.mac_id > dev2->id.mac_id)
93 else if (dev1->id.mac_id < dev2->id.mac_id)
103 dpaa_add_to_device_list(struct rte_dpaa_device *newdev)
105 int comp, inserted = 0;
106 struct rte_dpaa_device *dev = NULL;
107 struct rte_dpaa_device *tdev = NULL;
109 TAILQ_FOREACH_SAFE(dev, &rte_dpaa_bus.device_list, next, tdev) {
110 comp = compare_dpaa_devices(newdev, dev);
112 TAILQ_INSERT_BEFORE(dev, newdev, next);
119 TAILQ_INSERT_TAIL(&rte_dpaa_bus.device_list, newdev, next);
123 * Reads the SEC device from DTS
124 * Returns -1 if SEC devices not available, 0 otherwise
127 dpaa_sec_available(void)
129 const struct device_node *caam_node;
131 for_each_compatible_node(caam_node, NULL, "fsl,sec-v4.0") {
138 static void dpaa_clean_device_list(void);
140 static struct rte_devargs *
141 dpaa_devargs_lookup(struct rte_dpaa_device *dev)
143 struct rte_devargs *devargs;
146 RTE_EAL_DEVARGS_FOREACH("dpaa_bus", devargs) {
147 devargs->bus->parse(devargs->name, &dev_name);
148 if (strcmp(dev_name, dev->device.name) == 0) {
149 DPAA_BUS_INFO("**Devargs matched %s", dev_name);
157 dpaa_create_device_list(void)
161 struct rte_dpaa_device *dev;
162 struct fm_eth_port_cfg *cfg;
163 struct fman_if *fman_intf;
165 /* Creating Ethernet Devices */
166 for (i = 0; i < dpaa_netcfg->num_ethports; i++) {
167 dev = calloc(1, sizeof(struct rte_dpaa_device));
169 DPAA_BUS_LOG(ERR, "Failed to allocate ETH devices");
174 dev->device.bus = &rte_dpaa_bus.bus;
176 cfg = &dpaa_netcfg->port_cfg[i];
177 fman_intf = cfg->fman_if;
179 /* Device identifiers */
180 dev->id.fman_id = fman_intf->fman_idx + 1;
181 dev->id.mac_id = fman_intf->mac_idx;
182 dev->device_type = FSL_DPAA_ETH;
185 /* Create device name */
186 memset(dev->name, 0, RTE_ETH_NAME_MAX_LEN);
187 sprintf(dev->name, "fm%d-mac%d", (fman_intf->fman_idx + 1),
189 DPAA_BUS_LOG(INFO, "%s netdev added", dev->name);
190 dev->device.name = dev->name;
191 dev->device.devargs = dpaa_devargs_lookup(dev);
193 dpaa_add_to_device_list(dev);
196 rte_dpaa_bus.device_count = i;
198 /* Unlike case of ETH, RTE_LIBRTE_DPAA_MAX_CRYPTODEV SEC devices are
199 * constantly created only if "sec" property is found in the device
200 * tree. Logically there is no limit for number of devices (QI
201 * interfaces) that can be created.
204 if (dpaa_sec_available()) {
205 DPAA_BUS_LOG(INFO, "DPAA SEC devices are not available");
209 /* Creating SEC Devices */
210 for (i = 0; i < RTE_LIBRTE_DPAA_MAX_CRYPTODEV; i++) {
211 dev = calloc(1, sizeof(struct rte_dpaa_device));
213 DPAA_BUS_LOG(ERR, "Failed to allocate SEC devices");
218 dev->device_type = FSL_DPAA_CRYPTO;
219 dev->id.dev_id = rte_dpaa_bus.device_count + i;
221 /* Even though RTE_CRYPTODEV_NAME_MAX_LEN is valid length of
222 * crypto PMD, using RTE_ETH_NAME_MAX_LEN as that is the size
223 * allocated for dev->name/
225 memset(dev->name, 0, RTE_ETH_NAME_MAX_LEN);
226 sprintf(dev->name, "dpaa_sec-%d", i+1);
227 DPAA_BUS_LOG(INFO, "%s cryptodev added", dev->name);
228 dev->device.name = dev->name;
229 dev->device.devargs = dpaa_devargs_lookup(dev);
231 dpaa_add_to_device_list(dev);
234 rte_dpaa_bus.device_count += i;
239 dpaa_clean_device_list();
244 dpaa_clean_device_list(void)
246 struct rte_dpaa_device *dev = NULL;
247 struct rte_dpaa_device *tdev = NULL;
249 TAILQ_FOREACH_SAFE(dev, &rte_dpaa_bus.device_list, next, tdev) {
250 TAILQ_REMOVE(&rte_dpaa_bus.device_list, dev, next);
256 int rte_dpaa_portal_init(void *arg)
258 static const struct rte_mbuf_dynfield dpaa_seqn_dynfield_desc = {
259 .name = DPAA_SEQN_DYNFIELD_NAME,
260 .size = sizeof(dpaa_seqn_t),
261 .align = __alignof__(dpaa_seqn_t),
263 unsigned int cpu, lcore = rte_lcore_id();
266 BUS_INIT_FUNC_TRACE();
268 if ((size_t)arg == 1 || lcore == LCORE_ID_ANY)
269 lcore = rte_get_main_lcore();
271 if (lcore >= RTE_MAX_LCORE)
274 cpu = rte_lcore_to_cpu_id(lcore);
276 dpaa_seqn_dynfield_offset =
277 rte_mbuf_dynfield_register(&dpaa_seqn_dynfield_desc);
278 if (dpaa_seqn_dynfield_offset < 0) {
279 DPAA_BUS_LOG(ERR, "Failed to register mbuf field for dpaa sequence number\n");
283 /* Initialise bman thread portals */
284 ret = bman_thread_init();
286 DPAA_BUS_LOG(ERR, "bman_thread_init failed on core %u"
287 " (lcore=%u) with ret: %d", cpu, lcore, ret);
291 DPAA_BUS_LOG(DEBUG, "BMAN thread initialized - CPU=%d lcore=%d",
294 /* Initialise qman thread portals */
295 ret = qman_thread_init();
297 DPAA_BUS_LOG(ERR, "qman_thread_init failed on core %u"
298 " (lcore=%u) with ret: %d", cpu, lcore, ret);
299 bman_thread_finish();
303 DPAA_BUS_LOG(DEBUG, "QMAN thread initialized - CPU=%d lcore=%d",
306 DPAA_PER_LCORE_PORTAL = rte_malloc(NULL, sizeof(struct dpaa_portal),
307 RTE_CACHE_LINE_SIZE);
308 if (!DPAA_PER_LCORE_PORTAL) {
309 DPAA_BUS_LOG(ERR, "Unable to allocate memory");
310 bman_thread_finish();
311 qman_thread_finish();
315 DPAA_PER_LCORE_PORTAL->qman_idx = qman_get_portal_index();
316 DPAA_PER_LCORE_PORTAL->bman_idx = bman_get_portal_index();
317 DPAA_PER_LCORE_PORTAL->tid = syscall(SYS_gettid);
319 ret = pthread_setspecific(dpaa_portal_key,
320 (void *)DPAA_PER_LCORE_PORTAL);
322 DPAA_BUS_LOG(ERR, "pthread_setspecific failed on core %u"
323 " (lcore=%u) with ret: %d", cpu, lcore, ret);
324 dpaa_portal_finish(NULL);
329 DPAA_BUS_LOG(DEBUG, "QMAN thread initialized");
335 rte_dpaa_portal_fq_init(void *arg, struct qman_fq *fq)
337 /* Affine above created portal with channel*/
341 if (unlikely(!DPAA_PER_LCORE_PORTAL)) {
342 ret = rte_dpaa_portal_init(arg);
344 DPAA_BUS_LOG(ERR, "portal initialization failure");
349 /* Initialise qman specific portals */
350 ret = fsl_qman_fq_portal_init(fq->qp);
352 DPAA_BUS_LOG(ERR, "Unable to init fq portal");
356 sdqcr = QM_SDQCR_CHANNELS_POOL_CONV(fq->ch_id);
357 qman_static_dequeue_add(sdqcr, fq->qp);
362 int rte_dpaa_portal_fq_close(struct qman_fq *fq)
364 return fsl_qman_fq_portal_destroy(fq->qp);
368 dpaa_portal_finish(void *arg)
370 struct dpaa_portal *dpaa_io_portal = (struct dpaa_portal *)arg;
372 if (!dpaa_io_portal) {
373 DPAA_BUS_LOG(DEBUG, "Portal already cleaned");
377 bman_thread_finish();
378 qman_thread_finish();
380 pthread_setspecific(dpaa_portal_key, NULL);
382 rte_free(dpaa_io_portal);
383 dpaa_io_portal = NULL;
384 DPAA_PER_LCORE_PORTAL = NULL;
388 rte_dpaa_bus_parse(const char *name, void *out_name)
391 int max_fman = 2, max_macs = 16;
395 /* There are two ways of passing device name, with and without
396 * separator. "dpaa_bus:fm1-mac3" with separator, and "fm1-mac3"
397 * without separator. Both need to be handled.
398 * It is also possible that "name=fm1-mac3" is passed along.
400 DPAA_BUS_DEBUG("Parse device name (%s)", name);
402 /* Check for dpaa_bus:fm1-mac3 style */
403 dup_name = strdup(name);
404 sep = strchr(dup_name, ':');
406 /* If not, check for name=fm1-mac3 style */
407 sep = strchr(dup_name, '=');
410 /* jump over the seprator */
411 sep = (char *) (sep + 1);
415 for (i = 0; i < max_fman; i++) {
416 for (j = 0; j < max_macs; j++) {
418 snprintf(fm_name, 16, "fm%d-mac%d", i, j);
419 if (strcmp(fm_name, sep) == 0) {
421 strcpy(out_name, sep);
428 for (i = 0; i < RTE_LIBRTE_DPAA_MAX_CRYPTODEV; i++) {
431 snprintf(sec_name, 16, "dpaa_sec-%d", i+1);
432 if (strcmp(sec_name, sep) == 0) {
434 strcpy(out_name, sep);
444 #define DPAA_DEV_PATH1 "/sys/devices/platform/soc/soc:fsl,dpaa"
445 #define DPAA_DEV_PATH2 "/sys/devices/platform/fsl,dpaa"
448 rte_dpaa_bus_scan(void)
452 BUS_INIT_FUNC_TRACE();
454 if ((access(DPAA_DEV_PATH1, F_OK) != 0) &&
455 (access(DPAA_DEV_PATH2, F_OK) != 0)) {
456 RTE_LOG(DEBUG, EAL, "DPAA Bus not present. Skipping.\n");
460 if (rte_dpaa_bus.detected)
463 rte_dpaa_bus.detected = 1;
465 /* create the key, supplying a function that'll be invoked
466 * when a portal affined thread will be deleted.
468 ret = pthread_key_create(&dpaa_portal_key, dpaa_portal_finish);
470 DPAA_BUS_LOG(DEBUG, "Unable to create pthread key. (%d)", ret);
471 dpaa_clean_device_list();
478 /* register a dpaa bus based dpaa driver */
480 rte_dpaa_driver_register(struct rte_dpaa_driver *driver)
484 BUS_INIT_FUNC_TRACE();
486 TAILQ_INSERT_TAIL(&rte_dpaa_bus.driver_list, driver, next);
487 /* Update Bus references */
488 driver->dpaa_bus = &rte_dpaa_bus;
491 /* un-register a dpaa bus based dpaa driver */
493 rte_dpaa_driver_unregister(struct rte_dpaa_driver *driver)
495 struct rte_dpaa_bus *dpaa_bus;
497 BUS_INIT_FUNC_TRACE();
499 dpaa_bus = driver->dpaa_bus;
501 TAILQ_REMOVE(&dpaa_bus->driver_list, driver, next);
502 /* Update Bus references */
503 driver->dpaa_bus = NULL;
507 rte_dpaa_device_match(struct rte_dpaa_driver *drv,
508 struct rte_dpaa_device *dev)
511 DPAA_BUS_DEBUG("Invalid drv or dev received.");
515 if (drv->drv_type == dev->device_type)
522 rte_dpaa_bus_dev_build(void)
526 /* Load the device-tree driver */
529 DPAA_BUS_LOG(ERR, "of_init failed with ret: %d", ret);
533 /* Get the interface configurations from device-tree */
534 dpaa_netcfg = netcfg_acquire();
536 DPAA_BUS_LOG(ERR, "netcfg_acquire failed");
540 RTE_LOG(NOTICE, EAL, "DPAA Bus Detected\n");
542 if (!dpaa_netcfg->num_ethports) {
543 DPAA_BUS_LOG(INFO, "no network interfaces available");
544 /* This is not an error */
548 #ifdef RTE_LIBRTE_DPAA_DEBUG_DRIVER
549 dump_netcfg(dpaa_netcfg);
552 DPAA_BUS_LOG(DEBUG, "Number of ethernet devices = %d",
553 dpaa_netcfg->num_ethports);
554 ret = dpaa_create_device_list();
556 DPAA_BUS_LOG(ERR, "Unable to create device list. (%d)", ret);
562 static int rte_dpaa_setup_intr(struct rte_intr_handle *intr_handle)
566 fd = eventfd(0, EFD_NONBLOCK | EFD_CLOEXEC);
568 DPAA_BUS_ERR("Cannot set up eventfd, error %i (%s)",
569 errno, strerror(errno));
573 intr_handle->fd = fd;
574 intr_handle->type = RTE_INTR_HANDLE_EXT;
580 rte_dpaa_bus_probe(void)
583 struct rte_dpaa_device *dev;
584 struct rte_dpaa_driver *drv;
585 FILE *svr_file = NULL;
586 unsigned int svr_ver;
587 int probe_all = rte_dpaa_bus.bus.conf.scan_mode != RTE_BUS_SCAN_WHITELIST;
588 static int process_once;
590 /* If DPAA bus is not present nothing needs to be done */
591 if (!rte_dpaa_bus.detected)
594 /* Device list creation is only done once */
596 rte_dpaa_bus_dev_build();
597 if (rte_eal_process_type() == RTE_PROC_PRIMARY) {
598 /* One time load of Qman/Bman drivers */
599 ret = qman_global_init();
601 DPAA_BUS_ERR("QMAN initialization failed: %d",
605 ret = bman_global_init();
607 DPAA_BUS_ERR("BMAN initialization failed: %d",
615 /* If no device present on DPAA bus nothing needs to be done */
616 if (TAILQ_EMPTY(&rte_dpaa_bus.device_list))
619 svr_file = fopen(DPAA_SOC_ID_FILE, "r");
621 if (fscanf(svr_file, "svr:%x", &svr_ver) > 0)
622 dpaa_svr_family = svr_ver & SVR_MASK;
626 TAILQ_FOREACH(dev, &rte_dpaa_bus.device_list, next) {
627 if (dev->device_type == FSL_DPAA_ETH) {
628 ret = rte_dpaa_setup_intr(&dev->intr_handle);
630 DPAA_BUS_ERR("Error setting up interrupt.\n");
634 /* And initialize the PA->VA translation table */
635 dpaax_iova_table_populate();
637 /* For each registered driver, and device, call the driver->probe */
638 TAILQ_FOREACH(dev, &rte_dpaa_bus.device_list, next) {
639 TAILQ_FOREACH(drv, &rte_dpaa_bus.driver_list, next) {
640 ret = rte_dpaa_device_match(drv, dev);
644 if (rte_dev_is_probed(&dev->device))
648 (dev->device.devargs &&
649 dev->device.devargs->policy == RTE_DEV_BLACKLISTED))
653 (dev->device.devargs &&
654 dev->device.devargs->policy ==
655 RTE_DEV_WHITELISTED)) {
656 ret = drv->probe(drv, dev);
658 DPAA_BUS_ERR("unable to probe:%s",
662 dev->device.driver = &drv->driver;
669 /* Register DPAA mempool ops only if any DPAA device has
672 rte_mbuf_set_platform_mempool_ops(DPAA_MEMPOOL_OPS_NAME);
677 static struct rte_device *
678 rte_dpaa_find_device(const struct rte_device *start, rte_dev_cmp_t cmp,
681 struct rte_dpaa_device *dev;
682 const struct rte_dpaa_device *dstart;
684 /* find_device is called with 'data' as an opaque object - just call
685 * cmp with this and each device object on bus.
689 dstart = RTE_DEV_TO_DPAA_CONST(start);
690 dev = TAILQ_NEXT(dstart, next);
692 dev = TAILQ_FIRST(&rte_dpaa_bus.device_list);
695 while (dev != NULL) {
696 if (cmp(&dev->device, data) == 0) {
697 DPAA_BUS_DEBUG("Found dev=(%s)\n", dev->device.name);
700 dev = TAILQ_NEXT(dev, next);
703 DPAA_BUS_DEBUG("Unable to find any device\n");
708 * Get iommu class of DPAA2 devices on the bus.
710 static enum rte_iova_mode
711 rte_dpaa_get_iommu_class(void)
713 if ((access(DPAA_DEV_PATH1, F_OK) != 0) &&
714 (access(DPAA_DEV_PATH2, F_OK) != 0)) {
721 dpaa_bus_plug(struct rte_device *dev __rte_unused)
723 /* No operation is performed while plugging the device */
728 dpaa_bus_unplug(struct rte_device *dev __rte_unused)
730 /* No operation is performed while unplugging the device */
735 dpaa_bus_dev_iterate(const void *start, const char *str,
736 const struct rte_dev_iterator *it __rte_unused)
738 const struct rte_dpaa_device *dstart;
739 struct rte_dpaa_device *dev;
740 char *dup, *dev_name = NULL;
743 DPAA_BUS_DEBUG("No device string");
747 /* Expectation is that device would be name=device_name */
748 if (strncmp(str, "name=", 5) != 0) {
749 DPAA_BUS_DEBUG("Invalid device string (%s)\n", str);
753 /* Now that name=device_name format is available, split */
755 dev_name = dup + strlen("name=");
758 dstart = RTE_DEV_TO_DPAA_CONST(start);
759 dev = TAILQ_NEXT(dstart, next);
761 dev = TAILQ_FIRST(&rte_dpaa_bus.device_list);
764 while (dev != NULL) {
765 if (strcmp(dev->device.name, dev_name) == 0) {
769 dev = TAILQ_NEXT(dev, next);
776 static struct rte_dpaa_bus rte_dpaa_bus = {
778 .scan = rte_dpaa_bus_scan,
779 .probe = rte_dpaa_bus_probe,
780 .parse = rte_dpaa_bus_parse,
781 .find_device = rte_dpaa_find_device,
782 .get_iommu_class = rte_dpaa_get_iommu_class,
783 .plug = dpaa_bus_plug,
784 .unplug = dpaa_bus_unplug,
785 .dev_iterate = dpaa_bus_dev_iterate,
787 .device_list = TAILQ_HEAD_INITIALIZER(rte_dpaa_bus.device_list),
788 .driver_list = TAILQ_HEAD_INITIALIZER(rte_dpaa_bus.driver_list),
792 RTE_REGISTER_BUS(FSL_DPAA_BUS_NAME, rte_dpaa_bus.bus);
793 RTE_LOG_REGISTER(dpaa_logtype_bus, bus.dpaa, NOTICE);