1 /* SPDX-License-Identifier: BSD-3-Clause
3 * Copyright 2017-2019 NXP
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>
36 #include <rte_dpaa_bus.h>
37 #include <rte_dpaa_logs.h>
38 #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(bool, dpaa_io);
58 RTE_DEFINE_PER_LCORE(struct dpaa_portal_dqrr, held_bufs);
60 struct fm_eth_port_cfg *
61 dpaa_get_eth_port_cfg(int dev_id)
63 return &dpaa_netcfg->port_cfg[dev_id];
67 compare_dpaa_devices(struct rte_dpaa_device *dev1,
68 struct rte_dpaa_device *dev2)
72 /* Segragating ETH from SEC devices */
73 if (dev1->device_type > dev2->device_type)
75 else if (dev1->device_type < dev2->device_type)
80 if ((comp != 0) || (dev1->device_type != FSL_DPAA_ETH))
83 if (dev1->id.fman_id > dev2->id.fman_id) {
85 } else if (dev1->id.fman_id < dev2->id.fman_id) {
88 /* FMAN ids match, check for mac_id */
89 if (dev1->id.mac_id > dev2->id.mac_id)
91 else if (dev1->id.mac_id < dev2->id.mac_id)
101 dpaa_add_to_device_list(struct rte_dpaa_device *newdev)
103 int comp, inserted = 0;
104 struct rte_dpaa_device *dev = NULL;
105 struct rte_dpaa_device *tdev = NULL;
107 TAILQ_FOREACH_SAFE(dev, &rte_dpaa_bus.device_list, next, tdev) {
108 comp = compare_dpaa_devices(newdev, dev);
110 TAILQ_INSERT_BEFORE(dev, newdev, next);
117 TAILQ_INSERT_TAIL(&rte_dpaa_bus.device_list, newdev, next);
121 * Reads the SEC device from DTS
122 * Returns -1 if SEC devices not available, 0 otherwise
125 dpaa_sec_available(void)
127 const struct device_node *caam_node;
129 for_each_compatible_node(caam_node, NULL, "fsl,sec-v4.0") {
136 static void dpaa_clean_device_list(void);
138 static struct rte_devargs *
139 dpaa_devargs_lookup(struct rte_dpaa_device *dev)
141 struct rte_devargs *devargs;
144 RTE_EAL_DEVARGS_FOREACH("dpaa_bus", devargs) {
145 devargs->bus->parse(devargs->name, &dev_name);
146 if (strcmp(dev_name, dev->device.name) == 0) {
147 DPAA_BUS_INFO("**Devargs matched %s", dev_name);
155 dpaa_create_device_list(void)
159 struct rte_dpaa_device *dev;
160 struct fm_eth_port_cfg *cfg;
161 struct fman_if *fman_intf;
163 /* Creating Ethernet Devices */
164 for (i = 0; i < dpaa_netcfg->num_ethports; i++) {
165 dev = calloc(1, sizeof(struct rte_dpaa_device));
167 DPAA_BUS_LOG(ERR, "Failed to allocate ETH devices");
172 dev->device.bus = &rte_dpaa_bus.bus;
174 cfg = &dpaa_netcfg->port_cfg[i];
175 fman_intf = cfg->fman_if;
177 /* Device identifiers */
178 dev->id.fman_id = fman_intf->fman_idx + 1;
179 dev->id.mac_id = fman_intf->mac_idx;
180 dev->device_type = FSL_DPAA_ETH;
183 /* Create device name */
184 memset(dev->name, 0, RTE_ETH_NAME_MAX_LEN);
185 sprintf(dev->name, "fm%d-mac%d", (fman_intf->fman_idx + 1),
187 DPAA_BUS_LOG(INFO, "%s netdev added", dev->name);
188 dev->device.name = dev->name;
189 dev->device.devargs = dpaa_devargs_lookup(dev);
191 dpaa_add_to_device_list(dev);
194 rte_dpaa_bus.device_count = i;
196 /* Unlike case of ETH, RTE_LIBRTE_DPAA_MAX_CRYPTODEV SEC devices are
197 * constantly created only if "sec" property is found in the device
198 * tree. Logically there is no limit for number of devices (QI
199 * interfaces) that can be created.
202 if (dpaa_sec_available()) {
203 DPAA_BUS_LOG(INFO, "DPAA SEC devices are not available");
207 /* Creating SEC Devices */
208 for (i = 0; i < RTE_LIBRTE_DPAA_MAX_CRYPTODEV; i++) {
209 dev = calloc(1, sizeof(struct rte_dpaa_device));
211 DPAA_BUS_LOG(ERR, "Failed to allocate SEC devices");
216 dev->device_type = FSL_DPAA_CRYPTO;
217 dev->id.dev_id = rte_dpaa_bus.device_count + i;
219 /* Even though RTE_CRYPTODEV_NAME_MAX_LEN is valid length of
220 * crypto PMD, using RTE_ETH_NAME_MAX_LEN as that is the size
221 * allocated for dev->name/
223 memset(dev->name, 0, RTE_ETH_NAME_MAX_LEN);
224 sprintf(dev->name, "dpaa_sec-%d", i+1);
225 DPAA_BUS_LOG(INFO, "%s cryptodev added", dev->name);
226 dev->device.name = dev->name;
227 dev->device.devargs = dpaa_devargs_lookup(dev);
229 dpaa_add_to_device_list(dev);
232 rte_dpaa_bus.device_count += i;
237 dpaa_clean_device_list();
242 dpaa_clean_device_list(void)
244 struct rte_dpaa_device *dev = NULL;
245 struct rte_dpaa_device *tdev = NULL;
247 TAILQ_FOREACH_SAFE(dev, &rte_dpaa_bus.device_list, next, tdev) {
248 TAILQ_REMOVE(&rte_dpaa_bus.device_list, dev, next);
254 int rte_dpaa_portal_init(void *arg)
256 unsigned int cpu, lcore = rte_lcore_id();
258 struct dpaa_portal *dpaa_io_portal;
260 BUS_INIT_FUNC_TRACE();
262 if ((size_t)arg == 1 || lcore == LCORE_ID_ANY)
263 lcore = rte_get_master_lcore();
265 if (lcore >= RTE_MAX_LCORE)
268 cpu = rte_lcore_to_cpu_id(lcore);
270 /* Initialise bman thread portals */
271 ret = bman_thread_init();
273 DPAA_BUS_LOG(ERR, "bman_thread_init failed on core %u"
274 " (lcore=%u) with ret: %d", cpu, lcore, ret);
278 DPAA_BUS_LOG(DEBUG, "BMAN thread initialized - CPU=%d lcore=%d",
281 /* Initialise qman thread portals */
282 ret = qman_thread_init();
284 DPAA_BUS_LOG(ERR, "qman_thread_init failed on core %u"
285 " (lcore=%u) with ret: %d", cpu, lcore, ret);
286 bman_thread_finish();
290 DPAA_BUS_LOG(DEBUG, "QMAN thread initialized - CPU=%d lcore=%d",
293 dpaa_io_portal = rte_malloc(NULL, sizeof(struct dpaa_portal),
294 RTE_CACHE_LINE_SIZE);
295 if (!dpaa_io_portal) {
296 DPAA_BUS_LOG(ERR, "Unable to allocate memory");
297 bman_thread_finish();
298 qman_thread_finish();
302 dpaa_io_portal->qman_idx = qman_get_portal_index();
303 dpaa_io_portal->bman_idx = bman_get_portal_index();
304 dpaa_io_portal->tid = syscall(SYS_gettid);
306 ret = pthread_setspecific(dpaa_portal_key, (void *)dpaa_io_portal);
308 DPAA_BUS_LOG(ERR, "pthread_setspecific failed on core %u"
309 " (lcore=%u) with ret: %d", cpu, lcore, ret);
310 dpaa_portal_finish(NULL);
315 RTE_PER_LCORE(dpaa_io) = true;
317 DPAA_BUS_LOG(DEBUG, "QMAN thread initialized");
323 rte_dpaa_portal_fq_init(void *arg, struct qman_fq *fq)
325 /* Affine above created portal with channel*/
329 if (unlikely(!RTE_PER_LCORE(dpaa_io))) {
330 ret = rte_dpaa_portal_init(arg);
332 DPAA_BUS_LOG(ERR, "portal initialization failure");
337 /* Initialise qman specific portals */
338 ret = fsl_qman_fq_portal_init(fq->qp);
340 DPAA_BUS_LOG(ERR, "Unable to init fq portal");
344 sdqcr = QM_SDQCR_CHANNELS_POOL_CONV(fq->ch_id);
345 qman_static_dequeue_add(sdqcr, fq->qp);
350 int rte_dpaa_portal_fq_close(struct qman_fq *fq)
352 return fsl_qman_fq_portal_destroy(fq->qp);
356 dpaa_portal_finish(void *arg)
358 struct dpaa_portal *dpaa_io_portal = (struct dpaa_portal *)arg;
360 if (!dpaa_io_portal) {
361 DPAA_BUS_LOG(DEBUG, "Portal already cleaned");
365 bman_thread_finish();
366 qman_thread_finish();
368 pthread_setspecific(dpaa_portal_key, NULL);
370 rte_free(dpaa_io_portal);
371 dpaa_io_portal = NULL;
373 RTE_PER_LCORE(dpaa_io) = false;
377 rte_dpaa_bus_parse(const char *name, void *out_name)
380 int max_fman = 2, max_macs = 16;
384 /* There are two ways of passing device name, with and without
385 * separator. "dpaa_bus:fm1-mac3" with separator, and "fm1-mac3"
386 * without separator. Both need to be handled.
387 * It is also possible that "name=fm1-mac3" is passed along.
389 DPAA_BUS_DEBUG("Parse device name (%s)", name);
391 /* Check for dpaa_bus:fm1-mac3 style */
392 dup_name = strdup(name);
393 sep = strchr(dup_name, ':');
395 /* If not, check for name=fm1-mac3 style */
396 sep = strchr(dup_name, '=');
399 /* jump over the seprator */
400 sep = (char *) (sep + 1);
404 for (i = 0; i < max_fman; i++) {
405 for (j = 0; j < max_macs; j++) {
407 snprintf(fm_name, 16, "fm%d-mac%d", i, j);
408 if (strcmp(fm_name, sep) == 0) {
410 strcpy(out_name, sep);
417 for (i = 0; i < RTE_LIBRTE_DPAA_MAX_CRYPTODEV; i++) {
420 snprintf(sec_name, 16, "dpaa_sec-%d", i+1);
421 if (strcmp(sec_name, sep) == 0) {
423 strcpy(out_name, sep);
433 #define DPAA_DEV_PATH1 "/sys/devices/platform/soc/soc:fsl,dpaa"
434 #define DPAA_DEV_PATH2 "/sys/devices/platform/fsl,dpaa"
437 rte_dpaa_bus_scan(void)
441 BUS_INIT_FUNC_TRACE();
443 if ((access(DPAA_DEV_PATH1, F_OK) != 0) &&
444 (access(DPAA_DEV_PATH2, F_OK) != 0)) {
445 RTE_LOG(DEBUG, EAL, "DPAA Bus not present. Skipping.\n");
449 if (rte_dpaa_bus.detected)
452 rte_dpaa_bus.detected = 1;
454 /* create the key, supplying a function that'll be invoked
455 * when a portal affined thread will be deleted.
457 ret = pthread_key_create(&dpaa_portal_key, dpaa_portal_finish);
459 DPAA_BUS_LOG(DEBUG, "Unable to create pthread key. (%d)", ret);
460 dpaa_clean_device_list();
467 /* register a dpaa bus based dpaa driver */
469 rte_dpaa_driver_register(struct rte_dpaa_driver *driver)
473 BUS_INIT_FUNC_TRACE();
475 TAILQ_INSERT_TAIL(&rte_dpaa_bus.driver_list, driver, next);
476 /* Update Bus references */
477 driver->dpaa_bus = &rte_dpaa_bus;
480 /* un-register a dpaa bus based dpaa driver */
482 rte_dpaa_driver_unregister(struct rte_dpaa_driver *driver)
484 struct rte_dpaa_bus *dpaa_bus;
486 BUS_INIT_FUNC_TRACE();
488 dpaa_bus = driver->dpaa_bus;
490 TAILQ_REMOVE(&dpaa_bus->driver_list, driver, next);
491 /* Update Bus references */
492 driver->dpaa_bus = NULL;
496 rte_dpaa_device_match(struct rte_dpaa_driver *drv,
497 struct rte_dpaa_device *dev)
500 DPAA_BUS_DEBUG("Invalid drv or dev received.");
504 if (drv->drv_type == dev->device_type)
511 rte_dpaa_bus_dev_build(void)
515 /* Load the device-tree driver */
518 DPAA_BUS_LOG(ERR, "of_init failed with ret: %d", ret);
522 /* Get the interface configurations from device-tree */
523 dpaa_netcfg = netcfg_acquire();
525 DPAA_BUS_LOG(ERR, "netcfg_acquire failed");
529 RTE_LOG(NOTICE, EAL, "DPAA Bus Detected\n");
531 if (!dpaa_netcfg->num_ethports) {
532 DPAA_BUS_LOG(INFO, "no network interfaces available");
533 /* This is not an error */
537 #ifdef RTE_LIBRTE_DPAA_DEBUG_DRIVER
538 dump_netcfg(dpaa_netcfg);
541 DPAA_BUS_LOG(DEBUG, "Number of ethernet devices = %d",
542 dpaa_netcfg->num_ethports);
543 ret = dpaa_create_device_list();
545 DPAA_BUS_LOG(ERR, "Unable to create device list. (%d)", ret);
552 rte_dpaa_bus_probe(void)
555 struct rte_dpaa_device *dev;
556 struct rte_dpaa_driver *drv;
557 FILE *svr_file = NULL;
558 unsigned int svr_ver;
559 int probe_all = rte_dpaa_bus.bus.conf.scan_mode != RTE_BUS_SCAN_WHITELIST;
560 static int process_once;
562 /* If DPAA bus is not present nothing needs to be done */
563 if (!rte_dpaa_bus.detected)
566 /* Device list creation is only done once */
568 rte_dpaa_bus_dev_build();
569 if (rte_eal_process_type() == RTE_PROC_PRIMARY) {
570 /* One time load of Qman/Bman drivers */
571 ret = qman_global_init();
573 DPAA_BUS_ERR("QMAN initialization failed: %d",
577 ret = bman_global_init();
579 DPAA_BUS_ERR("BMAN initialization failed: %d",
587 /* If no device present on DPAA bus nothing needs to be done */
588 if (TAILQ_EMPTY(&rte_dpaa_bus.device_list))
591 svr_file = fopen(DPAA_SOC_ID_FILE, "r");
593 if (fscanf(svr_file, "svr:%x", &svr_ver) > 0)
594 dpaa_svr_family = svr_ver & SVR_MASK;
598 /* And initialize the PA->VA translation table */
599 dpaax_iova_table_populate();
601 /* For each registered driver, and device, call the driver->probe */
602 TAILQ_FOREACH(dev, &rte_dpaa_bus.device_list, next) {
603 TAILQ_FOREACH(drv, &rte_dpaa_bus.driver_list, next) {
604 ret = rte_dpaa_device_match(drv, dev);
608 if (rte_dev_is_probed(&dev->device))
612 (dev->device.devargs &&
613 dev->device.devargs->policy == RTE_DEV_BLACKLISTED))
617 (dev->device.devargs &&
618 dev->device.devargs->policy ==
619 RTE_DEV_WHITELISTED)) {
620 ret = drv->probe(drv, dev);
622 DPAA_BUS_ERR("unable to probe:%s",
626 dev->device.driver = &drv->driver;
633 /* Register DPAA mempool ops only if any DPAA device has
636 rte_mbuf_set_platform_mempool_ops(DPAA_MEMPOOL_OPS_NAME);
641 static struct rte_device *
642 rte_dpaa_find_device(const struct rte_device *start, rte_dev_cmp_t cmp,
645 struct rte_dpaa_device *dev;
646 const struct rte_dpaa_device *dstart;
648 /* find_device is called with 'data' as an opaque object - just call
649 * cmp with this and each device object on bus.
653 dstart = RTE_DEV_TO_DPAA_CONST(start);
654 dev = TAILQ_NEXT(dstart, next);
656 dev = TAILQ_FIRST(&rte_dpaa_bus.device_list);
659 while (dev != NULL) {
660 if (cmp(&dev->device, data) == 0) {
661 DPAA_BUS_DEBUG("Found dev=(%s)\n", dev->device.name);
664 dev = TAILQ_NEXT(dev, next);
667 DPAA_BUS_DEBUG("Unable to find any device\n");
672 * Get iommu class of DPAA2 devices on the bus.
674 static enum rte_iova_mode
675 rte_dpaa_get_iommu_class(void)
677 if ((access(DPAA_DEV_PATH1, F_OK) != 0) &&
678 (access(DPAA_DEV_PATH2, F_OK) != 0)) {
685 dpaa_bus_plug(struct rte_device *dev __rte_unused)
687 /* No operation is performed while plugging the device */
692 dpaa_bus_unplug(struct rte_device *dev __rte_unused)
694 /* No operation is performed while unplugging the device */
699 dpaa_bus_dev_iterate(const void *start, const char *str,
700 const struct rte_dev_iterator *it __rte_unused)
702 const struct rte_dpaa_device *dstart;
703 struct rte_dpaa_device *dev;
704 char *dup, *dev_name = NULL;
707 DPAA_BUS_DEBUG("No device string");
711 /* Expectation is that device would be name=device_name */
712 if (strncmp(str, "name=", 5) != 0) {
713 DPAA_BUS_DEBUG("Invalid device string (%s)\n", str);
717 /* Now that name=device_name format is available, split */
719 dev_name = dup + strlen("name=");
722 dstart = RTE_DEV_TO_DPAA_CONST(start);
723 dev = TAILQ_NEXT(dstart, next);
725 dev = TAILQ_FIRST(&rte_dpaa_bus.device_list);
728 while (dev != NULL) {
729 if (strcmp(dev->device.name, dev_name) == 0) {
733 dev = TAILQ_NEXT(dev, next);
740 static struct rte_dpaa_bus rte_dpaa_bus = {
742 .scan = rte_dpaa_bus_scan,
743 .probe = rte_dpaa_bus_probe,
744 .parse = rte_dpaa_bus_parse,
745 .find_device = rte_dpaa_find_device,
746 .get_iommu_class = rte_dpaa_get_iommu_class,
747 .plug = dpaa_bus_plug,
748 .unplug = dpaa_bus_unplug,
749 .dev_iterate = dpaa_bus_dev_iterate,
751 .device_list = TAILQ_HEAD_INITIALIZER(rte_dpaa_bus.device_list),
752 .driver_list = TAILQ_HEAD_INITIALIZER(rte_dpaa_bus.driver_list),
756 RTE_REGISTER_BUS(FSL_DPAA_BUS_NAME, rte_dpaa_bus.bus);
758 RTE_INIT(dpaa_init_log)
760 dpaa_logtype_bus = rte_log_register("bus.dpaa");
761 if (dpaa_logtype_bus >= 0)
762 rte_log_set_level(dpaa_logtype_bus, RTE_LOG_NOTICE);