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 <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)
393 /* There are two ways of passing device name, with and without
394 * separator. "dpaa_bus:fm1-mac3" with separator, and "fm1-mac3"
395 * without separator. Both need to be handled.
396 * It is also possible that "name=fm1-mac3" is passed along.
398 DPAA_BUS_DEBUG("Parse device name (%s)", name);
401 if (strncmp(name, "dpaa_bus:", 9) == 0) {
403 } else if (strncmp(name, "name=", 5) == 0) {
407 if (sscanf(&name[delta], "fm%u-mac%u", &i, &j) != 2 ||
413 char *out_name = out;
414 const size_t max_name_len = sizeof("fm.-mac..") - 1;
416 /* Do not check for truncation, either name ends with
417 * '\0' or the device name is followed by parameters and there
418 * will be a ',' instead. Not copying past this comma is not an
421 strlcpy(out_name, &name[delta], max_name_len + 1);
423 /* Second digit of mac%u could instead be ','. */
424 if ((strlen(out_name) == max_name_len) &&
425 out_name[max_name_len] == ',')
426 out_name[max_name_len] = '\0';
432 #define DPAA_DEV_PATH1 "/sys/devices/platform/soc/soc:fsl,dpaa"
433 #define DPAA_DEV_PATH2 "/sys/devices/platform/fsl,dpaa"
436 rte_dpaa_bus_scan(void)
440 BUS_INIT_FUNC_TRACE();
442 if ((access(DPAA_DEV_PATH1, F_OK) != 0) &&
443 (access(DPAA_DEV_PATH2, F_OK) != 0)) {
444 RTE_LOG(DEBUG, EAL, "DPAA Bus not present. Skipping.\n");
448 if (rte_dpaa_bus.detected)
451 rte_dpaa_bus.detected = 1;
453 /* create the key, supplying a function that'll be invoked
454 * when a portal affined thread will be deleted.
456 ret = pthread_key_create(&dpaa_portal_key, dpaa_portal_finish);
458 DPAA_BUS_LOG(DEBUG, "Unable to create pthread key. (%d)", ret);
459 dpaa_clean_device_list();
466 /* register a dpaa bus based dpaa driver */
468 rte_dpaa_driver_register(struct rte_dpaa_driver *driver)
472 BUS_INIT_FUNC_TRACE();
474 TAILQ_INSERT_TAIL(&rte_dpaa_bus.driver_list, driver, next);
475 /* Update Bus references */
476 driver->dpaa_bus = &rte_dpaa_bus;
479 /* un-register a dpaa bus based dpaa driver */
481 rte_dpaa_driver_unregister(struct rte_dpaa_driver *driver)
483 struct rte_dpaa_bus *dpaa_bus;
485 BUS_INIT_FUNC_TRACE();
487 dpaa_bus = driver->dpaa_bus;
489 TAILQ_REMOVE(&dpaa_bus->driver_list, driver, next);
490 /* Update Bus references */
491 driver->dpaa_bus = NULL;
495 rte_dpaa_device_match(struct rte_dpaa_driver *drv,
496 struct rte_dpaa_device *dev)
499 DPAA_BUS_DEBUG("Invalid drv or dev received.");
503 if (drv->drv_type == dev->device_type)
510 rte_dpaa_bus_dev_build(void)
514 /* Load the device-tree driver */
517 DPAA_BUS_LOG(ERR, "of_init failed with ret: %d", ret);
521 /* Get the interface configurations from device-tree */
522 dpaa_netcfg = netcfg_acquire();
525 "netcfg failed: /dev/fsl_usdpaa device not available");
527 "Check if you are using USDPAA based device tree");
531 RTE_LOG(NOTICE, EAL, "DPAA Bus Detected\n");
533 if (!dpaa_netcfg->num_ethports) {
534 DPAA_BUS_LOG(INFO, "NO DPDK mapped net interfaces available");
535 /* This is not an error */
538 #ifdef RTE_LIBRTE_DPAA_DEBUG_DRIVER
539 dump_netcfg(dpaa_netcfg);
542 DPAA_BUS_LOG(DEBUG, "Number of ethernet devices = %d",
543 dpaa_netcfg->num_ethports);
544 ret = dpaa_create_device_list();
546 DPAA_BUS_LOG(ERR, "Unable to create device list. (%d)", ret);
552 static int rte_dpaa_setup_intr(struct rte_intr_handle *intr_handle)
556 fd = eventfd(0, EFD_NONBLOCK | EFD_CLOEXEC);
558 DPAA_BUS_ERR("Cannot set up eventfd, error %i (%s)",
559 errno, strerror(errno));
563 intr_handle->fd = fd;
564 intr_handle->type = RTE_INTR_HANDLE_EXT;
570 rte_dpaa_bus_probe(void)
573 struct rte_dpaa_device *dev;
574 struct rte_dpaa_driver *drv;
575 FILE *svr_file = NULL;
576 unsigned int svr_ver;
577 int probe_all = rte_dpaa_bus.bus.conf.scan_mode != RTE_BUS_SCAN_ALLOWLIST;
578 static int process_once;
580 /* If DPAA bus is not present nothing needs to be done */
581 if (!rte_dpaa_bus.detected)
584 /* Device list creation is only done once */
586 rte_dpaa_bus_dev_build();
587 /* One time load of Qman/Bman drivers */
588 ret = qman_global_init();
590 DPAA_BUS_ERR("QMAN initialization failed: %d",
594 ret = bman_global_init();
596 DPAA_BUS_ERR("BMAN initialization failed: %d",
603 /* If no device present on DPAA bus nothing needs to be done */
604 if (TAILQ_EMPTY(&rte_dpaa_bus.device_list))
607 svr_file = fopen(DPAA_SOC_ID_FILE, "r");
609 if (fscanf(svr_file, "svr:%x", &svr_ver) > 0)
610 dpaa_svr_family = svr_ver & SVR_MASK;
614 TAILQ_FOREACH(dev, &rte_dpaa_bus.device_list, next) {
615 if (dev->device_type == FSL_DPAA_ETH) {
616 ret = rte_dpaa_setup_intr(&dev->intr_handle);
618 DPAA_BUS_ERR("Error setting up interrupt.\n");
622 /* And initialize the PA->VA translation table */
623 dpaax_iova_table_populate();
625 /* For each registered driver, and device, call the driver->probe */
626 TAILQ_FOREACH(dev, &rte_dpaa_bus.device_list, next) {
627 TAILQ_FOREACH(drv, &rte_dpaa_bus.driver_list, next) {
628 ret = rte_dpaa_device_match(drv, dev);
632 if (rte_dev_is_probed(&dev->device))
636 (dev->device.devargs &&
637 dev->device.devargs->policy == RTE_DEV_BLOCKED))
641 (dev->device.devargs &&
642 dev->device.devargs->policy == RTE_DEV_ALLOWED)) {
643 ret = drv->probe(drv, dev);
645 DPAA_BUS_ERR("unable to probe:%s",
649 dev->device.driver = &drv->driver;
656 /* Register DPAA mempool ops only if any DPAA device has
659 rte_mbuf_set_platform_mempool_ops(DPAA_MEMPOOL_OPS_NAME);
664 static struct rte_device *
665 rte_dpaa_find_device(const struct rte_device *start, rte_dev_cmp_t cmp,
668 struct rte_dpaa_device *dev;
669 const struct rte_dpaa_device *dstart;
671 /* find_device is called with 'data' as an opaque object - just call
672 * cmp with this and each device object on bus.
676 dstart = RTE_DEV_TO_DPAA_CONST(start);
677 dev = TAILQ_NEXT(dstart, next);
679 dev = TAILQ_FIRST(&rte_dpaa_bus.device_list);
682 while (dev != NULL) {
683 if (cmp(&dev->device, data) == 0) {
684 DPAA_BUS_DEBUG("Found dev=(%s)\n", dev->device.name);
687 dev = TAILQ_NEXT(dev, next);
690 DPAA_BUS_DEBUG("Unable to find any device\n");
695 * Get iommu class of DPAA2 devices on the bus.
697 static enum rte_iova_mode
698 rte_dpaa_get_iommu_class(void)
700 if ((access(DPAA_DEV_PATH1, F_OK) != 0) &&
701 (access(DPAA_DEV_PATH2, F_OK) != 0)) {
708 dpaa_bus_plug(struct rte_device *dev __rte_unused)
710 /* No operation is performed while plugging the device */
715 dpaa_bus_unplug(struct rte_device *dev __rte_unused)
717 /* No operation is performed while unplugging the device */
722 dpaa_bus_dev_iterate(const void *start, const char *str,
723 const struct rte_dev_iterator *it __rte_unused)
725 const struct rte_dpaa_device *dstart;
726 struct rte_dpaa_device *dev;
727 char *dup, *dev_name = NULL;
730 DPAA_BUS_DEBUG("No device string");
734 /* Expectation is that device would be name=device_name */
735 if (strncmp(str, "name=", 5) != 0) {
736 DPAA_BUS_DEBUG("Invalid device string (%s)\n", str);
740 /* Now that name=device_name format is available, split */
742 dev_name = dup + strlen("name=");
745 dstart = RTE_DEV_TO_DPAA_CONST(start);
746 dev = TAILQ_NEXT(dstart, next);
748 dev = TAILQ_FIRST(&rte_dpaa_bus.device_list);
751 while (dev != NULL) {
752 if (strcmp(dev->device.name, dev_name) == 0) {
756 dev = TAILQ_NEXT(dev, next);
763 static struct rte_dpaa_bus rte_dpaa_bus = {
765 .scan = rte_dpaa_bus_scan,
766 .probe = rte_dpaa_bus_probe,
767 .parse = rte_dpaa_bus_parse,
768 .find_device = rte_dpaa_find_device,
769 .get_iommu_class = rte_dpaa_get_iommu_class,
770 .plug = dpaa_bus_plug,
771 .unplug = dpaa_bus_unplug,
772 .dev_iterate = dpaa_bus_dev_iterate,
774 .device_list = TAILQ_HEAD_INITIALIZER(rte_dpaa_bus.device_list),
775 .driver_list = TAILQ_HEAD_INITIALIZER(rte_dpaa_bus.driver_list),
779 RTE_REGISTER_BUS(FSL_DPAA_BUS_NAME, rte_dpaa_bus.bus);
780 RTE_LOG_REGISTER(dpaa_logtype_bus, bus.dpaa, NOTICE);