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>
45 static struct rte_dpaa_bus rte_dpaa_bus;
46 struct netcfg_info *dpaa_netcfg;
48 /* define a variable to hold the portal_key, once created.*/
49 static pthread_key_t dpaa_portal_key;
51 unsigned int dpaa_svr_family;
53 #define FSL_DPAA_BUS_NAME dpaa_bus
55 RTE_DEFINE_PER_LCORE(bool, dpaa_io);
56 RTE_DEFINE_PER_LCORE(struct dpaa_portal_dqrr, held_bufs);
58 struct fm_eth_port_cfg *
59 dpaa_get_eth_port_cfg(int dev_id)
61 return &dpaa_netcfg->port_cfg[dev_id];
65 compare_dpaa_devices(struct rte_dpaa_device *dev1,
66 struct rte_dpaa_device *dev2)
70 /* Segragating ETH from SEC devices */
71 if (dev1->device_type > dev2->device_type)
73 else if (dev1->device_type < dev2->device_type)
78 if ((comp != 0) || (dev1->device_type != FSL_DPAA_ETH))
81 if (dev1->id.fman_id > dev2->id.fman_id) {
83 } else if (dev1->id.fman_id < dev2->id.fman_id) {
86 /* FMAN ids match, check for mac_id */
87 if (dev1->id.mac_id > dev2->id.mac_id)
89 else if (dev1->id.mac_id < dev2->id.mac_id)
99 dpaa_add_to_device_list(struct rte_dpaa_device *newdev)
101 int comp, inserted = 0;
102 struct rte_dpaa_device *dev = NULL;
103 struct rte_dpaa_device *tdev = NULL;
105 TAILQ_FOREACH_SAFE(dev, &rte_dpaa_bus.device_list, next, tdev) {
106 comp = compare_dpaa_devices(newdev, dev);
108 TAILQ_INSERT_BEFORE(dev, newdev, next);
115 TAILQ_INSERT_TAIL(&rte_dpaa_bus.device_list, newdev, next);
119 * Reads the SEC device from DTS
120 * Returns -1 if SEC devices not available, 0 otherwise
123 dpaa_sec_available(void)
125 const struct device_node *caam_node;
127 for_each_compatible_node(caam_node, NULL, "fsl,sec-v4.0") {
134 static void dpaa_clean_device_list(void);
136 static struct rte_devargs *
137 dpaa_devargs_lookup(struct rte_dpaa_device *dev)
139 struct rte_devargs *devargs;
142 RTE_EAL_DEVARGS_FOREACH("dpaa_bus", devargs) {
143 devargs->bus->parse(devargs->name, &dev_name);
144 if (strcmp(dev_name, dev->device.name) == 0) {
145 DPAA_BUS_INFO("**Devargs matched %s", dev_name);
153 dpaa_create_device_list(void)
157 struct rte_dpaa_device *dev;
158 struct fm_eth_port_cfg *cfg;
159 struct fman_if *fman_intf;
161 /* Creating Ethernet Devices */
162 for (i = 0; i < dpaa_netcfg->num_ethports; i++) {
163 dev = calloc(1, sizeof(struct rte_dpaa_device));
165 DPAA_BUS_LOG(ERR, "Failed to allocate ETH devices");
170 dev->device.bus = &rte_dpaa_bus.bus;
172 cfg = &dpaa_netcfg->port_cfg[i];
173 fman_intf = cfg->fman_if;
175 /* Device identifiers */
176 dev->id.fman_id = fman_intf->fman_idx + 1;
177 dev->id.mac_id = fman_intf->mac_idx;
178 dev->device_type = FSL_DPAA_ETH;
181 /* Create device name */
182 memset(dev->name, 0, RTE_ETH_NAME_MAX_LEN);
183 sprintf(dev->name, "fm%d-mac%d", (fman_intf->fman_idx + 1),
185 DPAA_BUS_LOG(INFO, "%s netdev added", dev->name);
186 dev->device.name = dev->name;
187 dev->device.devargs = dpaa_devargs_lookup(dev);
189 dpaa_add_to_device_list(dev);
192 rte_dpaa_bus.device_count = i;
194 /* Unlike case of ETH, RTE_LIBRTE_DPAA_MAX_CRYPTODEV SEC devices are
195 * constantly created only if "sec" property is found in the device
196 * tree. Logically there is no limit for number of devices (QI
197 * interfaces) that can be created.
200 if (dpaa_sec_available()) {
201 DPAA_BUS_LOG(INFO, "DPAA SEC devices are not available");
205 /* Creating SEC Devices */
206 for (i = 0; i < RTE_LIBRTE_DPAA_MAX_CRYPTODEV; i++) {
207 dev = calloc(1, sizeof(struct rte_dpaa_device));
209 DPAA_BUS_LOG(ERR, "Failed to allocate SEC devices");
214 dev->device_type = FSL_DPAA_CRYPTO;
215 dev->id.dev_id = rte_dpaa_bus.device_count + i;
217 /* Even though RTE_CRYPTODEV_NAME_MAX_LEN is valid length of
218 * crypto PMD, using RTE_ETH_NAME_MAX_LEN as that is the size
219 * allocated for dev->name/
221 memset(dev->name, 0, RTE_ETH_NAME_MAX_LEN);
222 sprintf(dev->name, "dpaa_sec-%d", i+1);
223 DPAA_BUS_LOG(INFO, "%s cryptodev added", dev->name);
224 dev->device.name = dev->name;
225 dev->device.devargs = dpaa_devargs_lookup(dev);
227 dpaa_add_to_device_list(dev);
230 rte_dpaa_bus.device_count += i;
235 dpaa_clean_device_list();
240 dpaa_clean_device_list(void)
242 struct rte_dpaa_device *dev = NULL;
243 struct rte_dpaa_device *tdev = NULL;
245 TAILQ_FOREACH_SAFE(dev, &rte_dpaa_bus.device_list, next, tdev) {
246 TAILQ_REMOVE(&rte_dpaa_bus.device_list, dev, next);
252 int rte_dpaa_portal_init(void *arg)
254 unsigned int cpu, lcore = rte_lcore_id();
256 struct dpaa_portal *dpaa_io_portal;
258 BUS_INIT_FUNC_TRACE();
260 if ((size_t)arg == 1 || lcore == LCORE_ID_ANY)
261 lcore = rte_get_master_lcore();
263 if (lcore >= RTE_MAX_LCORE)
266 cpu = rte_lcore_to_cpu_id(lcore);
268 /* Initialise bman thread portals */
269 ret = bman_thread_init();
271 DPAA_BUS_LOG(ERR, "bman_thread_init failed on core %u"
272 " (lcore=%u) with ret: %d", cpu, lcore, ret);
276 DPAA_BUS_LOG(DEBUG, "BMAN thread initialized - CPU=%d lcore=%d",
279 /* Initialise qman thread portals */
280 ret = qman_thread_init();
282 DPAA_BUS_LOG(ERR, "qman_thread_init failed on core %u"
283 " (lcore=%u) with ret: %d", cpu, lcore, ret);
284 bman_thread_finish();
288 DPAA_BUS_LOG(DEBUG, "QMAN thread initialized - CPU=%d lcore=%d",
291 dpaa_io_portal = rte_malloc(NULL, sizeof(struct dpaa_portal),
292 RTE_CACHE_LINE_SIZE);
293 if (!dpaa_io_portal) {
294 DPAA_BUS_LOG(ERR, "Unable to allocate memory");
295 bman_thread_finish();
296 qman_thread_finish();
300 dpaa_io_portal->qman_idx = qman_get_portal_index();
301 dpaa_io_portal->bman_idx = bman_get_portal_index();
302 dpaa_io_portal->tid = syscall(SYS_gettid);
304 ret = pthread_setspecific(dpaa_portal_key, (void *)dpaa_io_portal);
306 DPAA_BUS_LOG(ERR, "pthread_setspecific failed on core %u"
307 " (lcore=%u) with ret: %d", cpu, lcore, ret);
308 dpaa_portal_finish(NULL);
313 RTE_PER_LCORE(dpaa_io) = true;
315 DPAA_BUS_LOG(DEBUG, "QMAN thread initialized");
321 rte_dpaa_portal_fq_init(void *arg, struct qman_fq *fq)
323 /* Affine above created portal with channel*/
327 if (unlikely(!RTE_PER_LCORE(dpaa_io))) {
328 ret = rte_dpaa_portal_init(arg);
330 DPAA_BUS_LOG(ERR, "portal initialization failure");
335 /* Initialise qman specific portals */
336 ret = fsl_qman_fq_portal_init(fq->qp);
338 DPAA_BUS_LOG(ERR, "Unable to init fq portal");
342 sdqcr = QM_SDQCR_CHANNELS_POOL_CONV(fq->ch_id);
343 qman_static_dequeue_add(sdqcr, fq->qp);
348 int rte_dpaa_portal_fq_close(struct qman_fq *fq)
350 return fsl_qman_fq_portal_destroy(fq->qp);
354 dpaa_portal_finish(void *arg)
356 struct dpaa_portal *dpaa_io_portal = (struct dpaa_portal *)arg;
358 if (!dpaa_io_portal) {
359 DPAA_BUS_LOG(DEBUG, "Portal already cleaned");
363 bman_thread_finish();
364 qman_thread_finish();
366 pthread_setspecific(dpaa_portal_key, NULL);
368 rte_free(dpaa_io_portal);
369 dpaa_io_portal = NULL;
371 RTE_PER_LCORE(dpaa_io) = false;
375 rte_dpaa_bus_parse(const char *name, void *out_name)
378 int max_fman = 2, max_macs = 16;
382 /* There are two ways of passing device name, with and without
383 * separator. "dpaa_bus:fm1-mac3" with separator, and "fm1-mac3"
384 * without separator. Both need to be handled.
385 * It is also possible that "name=fm1-mac3" is passed along.
387 DPAA_BUS_DEBUG("Parse device name (%s)", name);
389 /* Check for dpaa_bus:fm1-mac3 style */
390 dup_name = strdup(name);
391 sep = strchr(dup_name, ':');
393 /* If not, check for name=fm1-mac3 style */
394 sep = strchr(dup_name, '=');
397 /* jump over the seprator */
398 sep = (char *) (sep + 1);
402 for (i = 0; i < max_fman; i++) {
403 for (j = 0; j < max_macs; j++) {
405 snprintf(fm_name, 16, "fm%d-mac%d", i, j);
406 if (strcmp(fm_name, sep) == 0) {
408 strcpy(out_name, sep);
415 for (i = 0; i < RTE_LIBRTE_DPAA_MAX_CRYPTODEV; i++) {
418 snprintf(sec_name, 16, "dpaa_sec-%d", i+1);
419 if (strcmp(sec_name, sep) == 0) {
421 strcpy(out_name, sep);
431 #define DPAA_DEV_PATH1 "/sys/devices/platform/soc/soc:fsl,dpaa"
432 #define DPAA_DEV_PATH2 "/sys/devices/platform/fsl,dpaa"
435 rte_dpaa_bus_scan(void)
439 BUS_INIT_FUNC_TRACE();
441 if ((access(DPAA_DEV_PATH1, F_OK) != 0) &&
442 (access(DPAA_DEV_PATH2, F_OK) != 0)) {
443 RTE_LOG(DEBUG, EAL, "DPAA Bus not present. Skipping.\n");
447 if (rte_dpaa_bus.detected)
450 rte_dpaa_bus.detected = 1;
452 /* create the key, supplying a function that'll be invoked
453 * when a portal affined thread will be deleted.
455 ret = pthread_key_create(&dpaa_portal_key, dpaa_portal_finish);
457 DPAA_BUS_LOG(DEBUG, "Unable to create pthread key. (%d)", ret);
458 dpaa_clean_device_list();
465 /* register a dpaa bus based dpaa driver */
467 rte_dpaa_driver_register(struct rte_dpaa_driver *driver)
471 BUS_INIT_FUNC_TRACE();
473 TAILQ_INSERT_TAIL(&rte_dpaa_bus.driver_list, driver, next);
474 /* Update Bus references */
475 driver->dpaa_bus = &rte_dpaa_bus;
478 /* un-register a dpaa bus based dpaa driver */
480 rte_dpaa_driver_unregister(struct rte_dpaa_driver *driver)
482 struct rte_dpaa_bus *dpaa_bus;
484 BUS_INIT_FUNC_TRACE();
486 dpaa_bus = driver->dpaa_bus;
488 TAILQ_REMOVE(&dpaa_bus->driver_list, driver, next);
489 /* Update Bus references */
490 driver->dpaa_bus = NULL;
494 rte_dpaa_device_match(struct rte_dpaa_driver *drv,
495 struct rte_dpaa_device *dev)
498 DPAA_BUS_DEBUG("Invalid drv or dev received.");
502 if (drv->drv_type == dev->device_type)
509 rte_dpaa_bus_dev_build(void)
513 /* Load the device-tree driver */
516 DPAA_BUS_LOG(ERR, "of_init failed with ret: %d", ret);
520 /* Get the interface configurations from device-tree */
521 dpaa_netcfg = netcfg_acquire();
523 DPAA_BUS_LOG(ERR, "netcfg_acquire failed");
527 RTE_LOG(NOTICE, EAL, "DPAA Bus Detected\n");
529 if (!dpaa_netcfg->num_ethports) {
530 DPAA_BUS_LOG(INFO, "no network interfaces available");
531 /* This is not an error */
535 #ifdef RTE_LIBRTE_DPAA_DEBUG_DRIVER
536 dump_netcfg(dpaa_netcfg);
539 DPAA_BUS_LOG(DEBUG, "Number of ethernet devices = %d",
540 dpaa_netcfg->num_ethports);
541 ret = dpaa_create_device_list();
543 DPAA_BUS_LOG(ERR, "Unable to create device list. (%d)", ret);
550 rte_dpaa_bus_probe(void)
553 struct rte_dpaa_device *dev;
554 struct rte_dpaa_driver *drv;
555 FILE *svr_file = NULL;
556 unsigned int svr_ver;
557 int probe_all = rte_dpaa_bus.bus.conf.scan_mode != RTE_BUS_SCAN_WHITELIST;
558 static int process_once;
560 /* If DPAA bus is not present nothing needs to be done */
561 if (!rte_dpaa_bus.detected)
564 /* Device list creation is only done once */
566 rte_dpaa_bus_dev_build();
567 if (rte_eal_process_type() == RTE_PROC_PRIMARY) {
568 /* One time load of Qman/Bman drivers */
569 ret = qman_global_init();
571 DPAA_BUS_ERR("QMAN initialization failed: %d",
575 ret = bman_global_init();
577 DPAA_BUS_ERR("BMAN initialization failed: %d",
585 /* If no device present on DPAA bus nothing needs to be done */
586 if (TAILQ_EMPTY(&rte_dpaa_bus.device_list))
589 svr_file = fopen(DPAA_SOC_ID_FILE, "r");
591 if (fscanf(svr_file, "svr:%x", &svr_ver) > 0)
592 dpaa_svr_family = svr_ver & SVR_MASK;
596 /* And initialize the PA->VA translation table */
597 dpaax_iova_table_populate();
599 /* For each registered driver, and device, call the driver->probe */
600 TAILQ_FOREACH(dev, &rte_dpaa_bus.device_list, next) {
601 TAILQ_FOREACH(drv, &rte_dpaa_bus.driver_list, next) {
602 ret = rte_dpaa_device_match(drv, dev);
606 if (rte_dev_is_probed(&dev->device))
610 (dev->device.devargs &&
611 dev->device.devargs->policy == RTE_DEV_BLACKLISTED))
615 (dev->device.devargs &&
616 dev->device.devargs->policy ==
617 RTE_DEV_WHITELISTED)) {
618 ret = drv->probe(drv, dev);
620 DPAA_BUS_ERR("unable to probe:%s",
624 dev->device.driver = &drv->driver;
631 /* Register DPAA mempool ops only if any DPAA device has
634 rte_mbuf_set_platform_mempool_ops(DPAA_MEMPOOL_OPS_NAME);
639 static struct rte_device *
640 rte_dpaa_find_device(const struct rte_device *start, rte_dev_cmp_t cmp,
643 struct rte_dpaa_device *dev;
644 const struct rte_dpaa_device *dstart;
646 /* find_device is called with 'data' as an opaque object - just call
647 * cmp with this and each device object on bus.
651 dstart = RTE_DEV_TO_DPAA_CONST(start);
652 dev = TAILQ_NEXT(dstart, next);
654 dev = TAILQ_FIRST(&rte_dpaa_bus.device_list);
657 while (dev != NULL) {
658 if (cmp(&dev->device, data) == 0) {
659 DPAA_BUS_DEBUG("Found dev=(%s)\n", dev->device.name);
662 dev = TAILQ_NEXT(dev, next);
665 DPAA_BUS_DEBUG("Unable to find any device\n");
670 * Get iommu class of DPAA2 devices on the bus.
672 static enum rte_iova_mode
673 rte_dpaa_get_iommu_class(void)
675 if ((access(DPAA_DEV_PATH1, F_OK) != 0) &&
676 (access(DPAA_DEV_PATH2, F_OK) != 0)) {
683 dpaa_bus_plug(struct rte_device *dev __rte_unused)
685 /* No operation is performed while plugging the device */
690 dpaa_bus_unplug(struct rte_device *dev __rte_unused)
692 /* No operation is performed while unplugging the device */
697 dpaa_bus_dev_iterate(const void *start, const char *str,
698 const struct rte_dev_iterator *it __rte_unused)
700 const struct rte_dpaa_device *dstart;
701 struct rte_dpaa_device *dev;
702 char *dup, *dev_name = NULL;
705 DPAA_BUS_DEBUG("No device string");
709 /* Expectation is that device would be name=device_name */
710 if (strncmp(str, "name=", 5) != 0) {
711 DPAA_BUS_DEBUG("Invalid device string (%s)\n", str);
715 /* Now that name=device_name format is available, split */
717 dev_name = dup + strlen("name=");
720 dstart = RTE_DEV_TO_DPAA_CONST(start);
721 dev = TAILQ_NEXT(dstart, next);
723 dev = TAILQ_FIRST(&rte_dpaa_bus.device_list);
726 while (dev != NULL) {
727 if (strcmp(dev->device.name, dev_name) == 0) {
731 dev = TAILQ_NEXT(dev, next);
738 static struct rte_dpaa_bus rte_dpaa_bus = {
740 .scan = rte_dpaa_bus_scan,
741 .probe = rte_dpaa_bus_probe,
742 .parse = rte_dpaa_bus_parse,
743 .find_device = rte_dpaa_find_device,
744 .get_iommu_class = rte_dpaa_get_iommu_class,
745 .plug = dpaa_bus_plug,
746 .unplug = dpaa_bus_unplug,
747 .dev_iterate = dpaa_bus_dev_iterate,
749 .device_list = TAILQ_HEAD_INITIALIZER(rte_dpaa_bus.device_list),
750 .driver_list = TAILQ_HEAD_INITIALIZER(rte_dpaa_bus.driver_list),
754 RTE_REGISTER_BUS(FSL_DPAA_BUS_NAME, rte_dpaa_bus.bus);
755 RTE_LOG_REGISTER(dpaa_logtype_bus, bus.dpaa, NOTICE);