1 /* SPDX-License-Identifier: BSD-3-Clause
3 * Copyright 2017-2020 NXP
14 #include <sys/types.h>
15 #include <sys/eventfd.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 <ethdev_driver.h>
30 #include <rte_malloc.h>
33 #include <rte_mbuf_pool_ops.h>
34 #include <rte_mbuf_dyn.h>
37 #include <rte_dpaa_bus.h>
38 #include <rte_dpaa_logs.h>
39 #include <dpaax_iova_table.h>
46 static struct rte_dpaa_bus rte_dpaa_bus;
47 struct netcfg_info *dpaa_netcfg;
49 /* define a variable to hold the portal_key, once created.*/
50 static pthread_key_t dpaa_portal_key;
52 unsigned int dpaa_svr_family;
54 #define FSL_DPAA_BUS_NAME dpaa_bus
56 RTE_DEFINE_PER_LCORE(struct dpaa_portal *, dpaa_io);
58 #define DPAA_SEQN_DYNFIELD_NAME "dpaa_seqn_dynfield"
59 int dpaa_seqn_dynfield_offset = -1;
61 struct fm_eth_port_cfg *
62 dpaa_get_eth_port_cfg(int dev_id)
64 return &dpaa_netcfg->port_cfg[dev_id];
68 compare_dpaa_devices(struct rte_dpaa_device *dev1,
69 struct rte_dpaa_device *dev2)
73 /* Segragating ETH from SEC devices */
74 if (dev1->device_type > dev2->device_type)
76 else if (dev1->device_type < dev2->device_type)
81 if ((comp != 0) || (dev1->device_type != FSL_DPAA_ETH))
84 if (dev1->id.fman_id > dev2->id.fman_id) {
86 } else if (dev1->id.fman_id < dev2->id.fman_id) {
89 /* FMAN ids match, check for mac_id */
90 if (dev1->id.mac_id > dev2->id.mac_id)
92 else if (dev1->id.mac_id < dev2->id.mac_id)
102 dpaa_add_to_device_list(struct rte_dpaa_device *newdev)
104 int comp, inserted = 0;
105 struct rte_dpaa_device *dev = NULL;
106 struct rte_dpaa_device *tdev = NULL;
108 RTE_TAILQ_FOREACH_SAFE(dev, &rte_dpaa_bus.device_list, next, tdev) {
109 comp = compare_dpaa_devices(newdev, dev);
111 TAILQ_INSERT_BEFORE(dev, newdev, next);
118 TAILQ_INSERT_TAIL(&rte_dpaa_bus.device_list, newdev, next);
122 * Reads the SEC device from DTS
123 * Returns -1 if SEC devices not available, 0 otherwise
126 dpaa_sec_available(void)
128 const struct device_node *caam_node;
130 for_each_compatible_node(caam_node, NULL, "fsl,sec-v4.0") {
137 static void dpaa_clean_device_list(void);
139 static struct rte_devargs *
140 dpaa_devargs_lookup(struct rte_dpaa_device *dev)
142 struct rte_devargs *devargs;
145 RTE_EAL_DEVARGS_FOREACH("dpaa_bus", devargs) {
146 devargs->bus->parse(devargs->name, &dev_name);
147 if (strcmp(dev_name, dev->device.name) == 0) {
148 DPAA_BUS_INFO("**Devargs matched %s", dev_name);
156 dpaa_create_device_list(void)
160 struct rte_dpaa_device *dev;
161 struct fm_eth_port_cfg *cfg;
162 struct fman_if *fman_intf;
164 /* Creating Ethernet Devices */
165 for (i = 0; i < dpaa_netcfg->num_ethports; i++) {
166 dev = calloc(1, sizeof(struct rte_dpaa_device));
168 DPAA_BUS_LOG(ERR, "Failed to allocate ETH devices");
173 dev->device.bus = &rte_dpaa_bus.bus;
175 /* Allocate interrupt handle instance */
177 rte_intr_instance_alloc(RTE_INTR_INSTANCE_F_PRIVATE);
178 if (dev->intr_handle == NULL) {
179 DPAA_BUS_LOG(ERR, "Failed to allocate intr handle");
184 cfg = &dpaa_netcfg->port_cfg[i];
185 fman_intf = cfg->fman_if;
187 /* Device identifiers */
188 dev->id.fman_id = fman_intf->fman_idx + 1;
189 dev->id.mac_id = fman_intf->mac_idx;
190 dev->device_type = FSL_DPAA_ETH;
193 /* Create device name */
194 memset(dev->name, 0, RTE_ETH_NAME_MAX_LEN);
195 sprintf(dev->name, "fm%d-mac%d", (fman_intf->fman_idx + 1),
197 DPAA_BUS_LOG(INFO, "%s netdev added", dev->name);
198 dev->device.name = dev->name;
199 dev->device.devargs = dpaa_devargs_lookup(dev);
201 dpaa_add_to_device_list(dev);
204 rte_dpaa_bus.device_count = i;
206 /* Unlike case of ETH, RTE_LIBRTE_DPAA_MAX_CRYPTODEV SEC devices are
207 * constantly created only if "sec" property is found in the device
208 * tree. Logically there is no limit for number of devices (QI
209 * interfaces) that can be created.
212 if (dpaa_sec_available()) {
213 DPAA_BUS_LOG(INFO, "DPAA SEC devices are not available");
217 /* Creating SEC Devices */
218 for (i = 0; i < RTE_LIBRTE_DPAA_MAX_CRYPTODEV; i++) {
219 dev = calloc(1, sizeof(struct rte_dpaa_device));
221 DPAA_BUS_LOG(ERR, "Failed to allocate SEC devices");
226 /* Allocate interrupt handle instance */
228 rte_intr_instance_alloc(RTE_INTR_INSTANCE_F_PRIVATE);
229 if (dev->intr_handle == NULL) {
230 DPAA_BUS_LOG(ERR, "Failed to allocate intr handle");
235 dev->device_type = FSL_DPAA_CRYPTO;
236 dev->id.dev_id = rte_dpaa_bus.device_count + i;
238 /* Even though RTE_CRYPTODEV_NAME_MAX_LEN is valid length of
239 * crypto PMD, using RTE_ETH_NAME_MAX_LEN as that is the size
240 * allocated for dev->name/
242 memset(dev->name, 0, RTE_ETH_NAME_MAX_LEN);
243 sprintf(dev->name, "dpaa_sec-%d", i+1);
244 DPAA_BUS_LOG(INFO, "%s cryptodev added", dev->name);
245 dev->device.name = dev->name;
246 dev->device.devargs = dpaa_devargs_lookup(dev);
248 dpaa_add_to_device_list(dev);
251 rte_dpaa_bus.device_count += i;
256 dpaa_clean_device_list();
261 dpaa_clean_device_list(void)
263 struct rte_dpaa_device *dev = NULL;
264 struct rte_dpaa_device *tdev = NULL;
266 RTE_TAILQ_FOREACH_SAFE(dev, &rte_dpaa_bus.device_list, next, tdev) {
267 TAILQ_REMOVE(&rte_dpaa_bus.device_list, dev, next);
268 rte_intr_instance_free(dev->intr_handle);
274 int rte_dpaa_portal_init(void *arg)
276 static const struct rte_mbuf_dynfield dpaa_seqn_dynfield_desc = {
277 .name = DPAA_SEQN_DYNFIELD_NAME,
278 .size = sizeof(dpaa_seqn_t),
279 .align = __alignof__(dpaa_seqn_t),
281 unsigned int cpu, lcore = rte_lcore_id();
284 BUS_INIT_FUNC_TRACE();
286 if ((size_t)arg == 1 || lcore == LCORE_ID_ANY)
287 lcore = rte_get_main_lcore();
289 if (lcore >= RTE_MAX_LCORE)
292 cpu = rte_lcore_to_cpu_id(lcore);
294 dpaa_seqn_dynfield_offset =
295 rte_mbuf_dynfield_register(&dpaa_seqn_dynfield_desc);
296 if (dpaa_seqn_dynfield_offset < 0) {
297 DPAA_BUS_LOG(ERR, "Failed to register mbuf field for dpaa sequence number\n");
301 /* Initialise bman thread portals */
302 ret = bman_thread_init();
304 DPAA_BUS_LOG(ERR, "bman_thread_init failed on core %u"
305 " (lcore=%u) with ret: %d", cpu, lcore, ret);
309 DPAA_BUS_LOG(DEBUG, "BMAN thread initialized - CPU=%d lcore=%d",
312 /* Initialise qman thread portals */
313 ret = qman_thread_init();
315 DPAA_BUS_LOG(ERR, "qman_thread_init failed on core %u"
316 " (lcore=%u) with ret: %d", cpu, lcore, ret);
317 bman_thread_finish();
321 DPAA_BUS_LOG(DEBUG, "QMAN thread initialized - CPU=%d lcore=%d",
324 DPAA_PER_LCORE_PORTAL = rte_malloc(NULL, sizeof(struct dpaa_portal),
325 RTE_CACHE_LINE_SIZE);
326 if (!DPAA_PER_LCORE_PORTAL) {
327 DPAA_BUS_LOG(ERR, "Unable to allocate memory");
328 bman_thread_finish();
329 qman_thread_finish();
333 DPAA_PER_LCORE_PORTAL->qman_idx = qman_get_portal_index();
334 DPAA_PER_LCORE_PORTAL->bman_idx = bman_get_portal_index();
335 DPAA_PER_LCORE_PORTAL->tid = rte_gettid();
337 ret = pthread_setspecific(dpaa_portal_key,
338 (void *)DPAA_PER_LCORE_PORTAL);
340 DPAA_BUS_LOG(ERR, "pthread_setspecific failed on core %u"
341 " (lcore=%u) with ret: %d", cpu, lcore, ret);
342 dpaa_portal_finish(NULL);
347 DPAA_BUS_LOG(DEBUG, "QMAN thread initialized");
353 rte_dpaa_portal_fq_init(void *arg, struct qman_fq *fq)
355 /* Affine above created portal with channel*/
359 if (unlikely(!DPAA_PER_LCORE_PORTAL)) {
360 ret = rte_dpaa_portal_init(arg);
362 DPAA_BUS_LOG(ERR, "portal initialization failure");
367 /* Initialise qman specific portals */
368 ret = fsl_qman_fq_portal_init(fq->qp);
370 DPAA_BUS_LOG(ERR, "Unable to init fq portal");
374 sdqcr = QM_SDQCR_CHANNELS_POOL_CONV(fq->ch_id);
375 qman_static_dequeue_add(sdqcr, fq->qp);
380 int rte_dpaa_portal_fq_close(struct qman_fq *fq)
382 return fsl_qman_fq_portal_destroy(fq->qp);
386 dpaa_portal_finish(void *arg)
388 struct dpaa_portal *dpaa_io_portal = (struct dpaa_portal *)arg;
390 if (!dpaa_io_portal) {
391 DPAA_BUS_LOG(DEBUG, "Portal already cleaned");
395 bman_thread_finish();
396 qman_thread_finish();
398 pthread_setspecific(dpaa_portal_key, NULL);
400 rte_free(dpaa_io_portal);
401 dpaa_io_portal = NULL;
402 DPAA_PER_LCORE_PORTAL = NULL;
406 rte_dpaa_bus_parse(const char *name, void *out)
411 /* There are two ways of passing device name, with and without
412 * separator. "dpaa_bus:fm1-mac3" with separator, and "fm1-mac3"
413 * without separator. Both need to be handled.
414 * It is also possible that "name=fm1-mac3" is passed along.
416 DPAA_BUS_DEBUG("Parse device name (%s)", name);
419 if (strncmp(name, "dpaa_bus:", 9) == 0) {
421 } else if (strncmp(name, "name=", 5) == 0) {
425 if (sscanf(&name[delta], "fm%u-mac%u", &i, &j) != 2 ||
431 char *out_name = out;
432 const size_t max_name_len = sizeof("fm.-mac..") - 1;
434 /* Do not check for truncation, either name ends with
435 * '\0' or the device name is followed by parameters and there
436 * will be a ',' instead. Not copying past this comma is not an
439 strlcpy(out_name, &name[delta], max_name_len + 1);
441 /* Second digit of mac%u could instead be ','. */
442 if ((strlen(out_name) == max_name_len) &&
443 out_name[max_name_len] == ',')
444 out_name[max_name_len] = '\0';
450 #define DPAA_DEV_PATH1 "/sys/devices/platform/soc/soc:fsl,dpaa"
451 #define DPAA_DEV_PATH2 "/sys/devices/platform/fsl,dpaa"
454 rte_dpaa_bus_scan(void)
458 BUS_INIT_FUNC_TRACE();
460 if ((access(DPAA_DEV_PATH1, F_OK) != 0) &&
461 (access(DPAA_DEV_PATH2, F_OK) != 0)) {
462 RTE_LOG(DEBUG, EAL, "DPAA Bus not present. Skipping.\n");
466 if (rte_dpaa_bus.detected)
469 rte_dpaa_bus.detected = 1;
471 /* create the key, supplying a function that'll be invoked
472 * when a portal affined thread will be deleted.
474 ret = pthread_key_create(&dpaa_portal_key, dpaa_portal_finish);
476 DPAA_BUS_LOG(DEBUG, "Unable to create pthread key. (%d)", ret);
477 dpaa_clean_device_list();
484 /* register a dpaa bus based dpaa driver */
486 rte_dpaa_driver_register(struct rte_dpaa_driver *driver)
490 BUS_INIT_FUNC_TRACE();
492 TAILQ_INSERT_TAIL(&rte_dpaa_bus.driver_list, driver, next);
493 /* Update Bus references */
494 driver->dpaa_bus = &rte_dpaa_bus;
497 /* un-register a dpaa bus based dpaa driver */
499 rte_dpaa_driver_unregister(struct rte_dpaa_driver *driver)
501 struct rte_dpaa_bus *dpaa_bus;
503 BUS_INIT_FUNC_TRACE();
505 dpaa_bus = driver->dpaa_bus;
507 TAILQ_REMOVE(&dpaa_bus->driver_list, driver, next);
508 /* Update Bus references */
509 driver->dpaa_bus = NULL;
513 rte_dpaa_device_match(struct rte_dpaa_driver *drv,
514 struct rte_dpaa_device *dev)
517 DPAA_BUS_DEBUG("Invalid drv or dev received.");
521 if (drv->drv_type == dev->device_type)
528 rte_dpaa_bus_dev_build(void)
532 /* Load the device-tree driver */
535 DPAA_BUS_LOG(ERR, "of_init failed with ret: %d", ret);
539 /* Get the interface configurations from device-tree */
540 dpaa_netcfg = netcfg_acquire();
543 "netcfg failed: /dev/fsl_usdpaa device not available");
545 "Check if you are using USDPAA based device tree");
549 RTE_LOG(NOTICE, EAL, "DPAA Bus Detected\n");
551 if (!dpaa_netcfg->num_ethports) {
552 DPAA_BUS_LOG(INFO, "NO DPDK mapped net interfaces available");
553 /* This is not an error */
556 #ifdef RTE_LIBRTE_DPAA_DEBUG_DRIVER
557 dump_netcfg(dpaa_netcfg);
560 DPAA_BUS_LOG(DEBUG, "Number of ethernet devices = %d",
561 dpaa_netcfg->num_ethports);
562 ret = dpaa_create_device_list();
564 DPAA_BUS_LOG(ERR, "Unable to create device list. (%d)", ret);
570 static int rte_dpaa_setup_intr(struct rte_intr_handle *intr_handle)
574 fd = eventfd(0, EFD_NONBLOCK | EFD_CLOEXEC);
576 DPAA_BUS_ERR("Cannot set up eventfd, error %i (%s)",
577 errno, strerror(errno));
581 if (rte_intr_fd_set(intr_handle, fd))
584 if (rte_intr_type_set(intr_handle, RTE_INTR_HANDLE_EXT))
591 rte_dpaa_bus_probe(void)
594 struct rte_dpaa_device *dev;
595 struct rte_dpaa_driver *drv;
596 FILE *svr_file = NULL;
597 unsigned int svr_ver;
598 int probe_all = rte_dpaa_bus.bus.conf.scan_mode != RTE_BUS_SCAN_ALLOWLIST;
599 static int process_once;
601 /* If DPAA bus is not present nothing needs to be done */
602 if (!rte_dpaa_bus.detected)
605 /* Device list creation is only done once */
607 rte_dpaa_bus_dev_build();
608 /* One time load of Qman/Bman drivers */
609 ret = qman_global_init();
611 DPAA_BUS_ERR("QMAN initialization failed: %d",
615 ret = bman_global_init();
617 DPAA_BUS_ERR("BMAN initialization failed: %d",
624 /* If no device present on DPAA bus nothing needs to be done */
625 if (TAILQ_EMPTY(&rte_dpaa_bus.device_list))
628 svr_file = fopen(DPAA_SOC_ID_FILE, "r");
630 if (fscanf(svr_file, "svr:%x", &svr_ver) > 0)
631 dpaa_svr_family = svr_ver & SVR_MASK;
635 TAILQ_FOREACH(dev, &rte_dpaa_bus.device_list, next) {
636 if (dev->device_type == FSL_DPAA_ETH) {
637 ret = rte_dpaa_setup_intr(dev->intr_handle);
639 DPAA_BUS_ERR("Error setting up interrupt.\n");
643 /* And initialize the PA->VA translation table */
644 dpaax_iova_table_populate();
646 /* For each registered driver, and device, call the driver->probe */
647 TAILQ_FOREACH(dev, &rte_dpaa_bus.device_list, next) {
648 TAILQ_FOREACH(drv, &rte_dpaa_bus.driver_list, next) {
649 ret = rte_dpaa_device_match(drv, dev);
653 if (rte_dev_is_probed(&dev->device))
657 (dev->device.devargs &&
658 dev->device.devargs->policy == RTE_DEV_BLOCKED))
662 (dev->device.devargs &&
663 dev->device.devargs->policy == RTE_DEV_ALLOWED)) {
664 ret = drv->probe(drv, dev);
666 DPAA_BUS_ERR("unable to probe:%s",
670 dev->device.driver = &drv->driver;
677 /* Register DPAA mempool ops only if any DPAA device has
680 rte_mbuf_set_platform_mempool_ops(DPAA_MEMPOOL_OPS_NAME);
685 static struct rte_device *
686 rte_dpaa_find_device(const struct rte_device *start, rte_dev_cmp_t cmp,
689 struct rte_dpaa_device *dev;
690 const struct rte_dpaa_device *dstart;
692 /* find_device is called with 'data' as an opaque object - just call
693 * cmp with this and each device object on bus.
697 dstart = RTE_DEV_TO_DPAA_CONST(start);
698 dev = TAILQ_NEXT(dstart, next);
700 dev = TAILQ_FIRST(&rte_dpaa_bus.device_list);
703 while (dev != NULL) {
704 if (cmp(&dev->device, data) == 0) {
705 DPAA_BUS_DEBUG("Found dev=(%s)\n", dev->device.name);
708 dev = TAILQ_NEXT(dev, next);
711 DPAA_BUS_DEBUG("Unable to find any device\n");
716 * Get iommu class of DPAA2 devices on the bus.
718 static enum rte_iova_mode
719 rte_dpaa_get_iommu_class(void)
721 if ((access(DPAA_DEV_PATH1, F_OK) != 0) &&
722 (access(DPAA_DEV_PATH2, F_OK) != 0)) {
729 dpaa_bus_plug(struct rte_device *dev __rte_unused)
731 /* No operation is performed while plugging the device */
736 dpaa_bus_unplug(struct rte_device *dev __rte_unused)
738 /* No operation is performed while unplugging the device */
743 dpaa_bus_dev_iterate(const void *start, const char *str,
744 const struct rte_dev_iterator *it __rte_unused)
746 const struct rte_dpaa_device *dstart;
747 struct rte_dpaa_device *dev;
748 char *dup, *dev_name = NULL;
751 DPAA_BUS_DEBUG("No device string");
755 /* Expectation is that device would be name=device_name */
756 if (strncmp(str, "name=", 5) != 0) {
757 DPAA_BUS_DEBUG("Invalid device string (%s)\n", str);
761 /* Now that name=device_name format is available, split */
763 dev_name = dup + strlen("name=");
766 dstart = RTE_DEV_TO_DPAA_CONST(start);
767 dev = TAILQ_NEXT(dstart, next);
769 dev = TAILQ_FIRST(&rte_dpaa_bus.device_list);
772 while (dev != NULL) {
773 if (strcmp(dev->device.name, dev_name) == 0) {
777 dev = TAILQ_NEXT(dev, next);
784 static struct rte_dpaa_bus rte_dpaa_bus = {
786 .scan = rte_dpaa_bus_scan,
787 .probe = rte_dpaa_bus_probe,
788 .parse = rte_dpaa_bus_parse,
789 .find_device = rte_dpaa_find_device,
790 .get_iommu_class = rte_dpaa_get_iommu_class,
791 .plug = dpaa_bus_plug,
792 .unplug = dpaa_bus_unplug,
793 .dev_iterate = dpaa_bus_dev_iterate,
795 .device_list = TAILQ_HEAD_INITIALIZER(rte_dpaa_bus.device_list),
796 .driver_list = TAILQ_HEAD_INITIALIZER(rte_dpaa_bus.driver_list),
800 RTE_REGISTER_BUS(FSL_DPAA_BUS_NAME, rte_dpaa_bus.bus);
801 RTE_LOG_REGISTER_DEFAULT(dpaa_logtype_bus, NOTICE);