1 /* SPDX-License-Identifier: BSD-3-Clause
3 * Copyright 2016,2018-2021 NXP
13 #include <rte_malloc.h>
14 #include <rte_devargs.h>
15 #include <rte_memcpy.h>
16 #include <ethdev_driver.h>
17 #include <rte_mbuf_dyn.h>
19 #include <rte_fslmc.h>
20 #include <fslmc_vfio.h>
21 #include "fslmc_logs.h"
23 #include <dpaax_iova_table.h>
25 #define VFIO_IOMMU_GROUP_PATH "/sys/kernel/iommu_groups"
26 #define FSLMC_BUS_NAME fslmc
28 struct rte_fslmc_bus rte_fslmc_bus;
29 uint8_t dpaa2_virt_mode;
31 #define DPAA2_SEQN_DYNFIELD_NAME "dpaa2_seqn_dynfield"
32 int dpaa2_seqn_dynfield_offset = -1;
35 rte_fslmc_get_device_count(enum rte_dpaa2_dev_type device_type)
37 if (device_type >= DPAA2_DEVTYPE_MAX)
39 return rte_fslmc_bus.device_count[device_type];
43 cleanup_fslmc_device_list(void)
45 struct rte_dpaa2_device *dev;
46 struct rte_dpaa2_device *t_dev;
48 RTE_TAILQ_FOREACH_SAFE(dev, &rte_fslmc_bus.device_list, next, t_dev) {
49 TAILQ_REMOVE(&rte_fslmc_bus.device_list, dev, next);
50 rte_intr_instance_free(dev->intr_handle);
57 compare_dpaa2_devname(struct rte_dpaa2_device *dev1,
58 struct rte_dpaa2_device *dev2)
62 if (dev1->dev_type > dev2->dev_type) {
64 } else if (dev1->dev_type < dev2->dev_type) {
67 /* Check the ID as types match */
68 if (dev1->object_id > dev2->object_id)
70 else if (dev1->object_id < dev2->object_id)
73 comp = 0; /* Duplicate device name */
80 insert_in_device_list(struct rte_dpaa2_device *newdev)
82 int comp, inserted = 0;
83 struct rte_dpaa2_device *dev = NULL;
84 struct rte_dpaa2_device *tdev = NULL;
86 RTE_TAILQ_FOREACH_SAFE(dev, &rte_fslmc_bus.device_list, next, tdev) {
87 comp = compare_dpaa2_devname(newdev, dev);
89 TAILQ_INSERT_BEFORE(dev, newdev, next);
96 TAILQ_INSERT_TAIL(&rte_fslmc_bus.device_list, newdev, next);
99 static struct rte_devargs *
100 fslmc_devargs_lookup(struct rte_dpaa2_device *dev)
102 struct rte_devargs *devargs;
105 RTE_EAL_DEVARGS_FOREACH("fslmc", devargs) {
106 devargs->bus->parse(devargs->name, &dev_name);
107 if (strcmp(dev_name, dev->device.name) == 0) {
108 DPAA2_BUS_INFO("**Devargs matched %s", dev_name);
116 dump_device_list(void)
118 struct rte_dpaa2_device *dev;
120 /* Only if the log level has been set to Debugging, print list */
121 if (rte_log_can_log(dpaa2_logtype_bus, RTE_LOG_DEBUG)) {
122 DPAA2_BUS_LOG(DEBUG, "List of devices scanned on bus:");
123 TAILQ_FOREACH(dev, &rte_fslmc_bus.device_list, next) {
124 DPAA2_BUS_LOG(DEBUG, "\t\t%s", dev->device.name);
130 scan_one_fslmc_device(char *dev_name)
132 char *dup_dev_name, *t_ptr;
133 struct rte_dpaa2_device *dev = NULL;
139 /* Creating a temporary copy to perform cut-parse over string */
140 dup_dev_name = strdup(dev_name);
142 DPAA2_BUS_ERR("Unable to allocate device name memory");
146 /* For all other devices, we allocate rte_dpaa2_device.
147 * For those devices where there is no driver, probe would release
148 * the memory associated with the rte_dpaa2_device after necessary
151 dev = calloc(1, sizeof(struct rte_dpaa2_device));
153 DPAA2_BUS_ERR("Unable to allocate device object");
158 dev->device.bus = &rte_fslmc_bus.bus;
160 /* Allocate interrupt instance */
162 rte_intr_instance_alloc(RTE_INTR_INSTANCE_F_PRIVATE);
163 if (dev->intr_handle == NULL) {
164 DPAA2_BUS_ERR("Failed to allocate intr handle");
169 /* Parse the device name and ID */
170 t_ptr = strtok(dup_dev_name, ".");
172 DPAA2_BUS_ERR("Invalid device found: (%s)", dup_dev_name);
176 if (!strncmp("dpni", t_ptr, 4))
177 dev->dev_type = DPAA2_ETH;
178 else if (!strncmp("dpseci", t_ptr, 6))
179 dev->dev_type = DPAA2_CRYPTO;
180 else if (!strncmp("dpcon", t_ptr, 5))
181 dev->dev_type = DPAA2_CON;
182 else if (!strncmp("dpbp", t_ptr, 4))
183 dev->dev_type = DPAA2_BPOOL;
184 else if (!strncmp("dpio", t_ptr, 4))
185 dev->dev_type = DPAA2_IO;
186 else if (!strncmp("dpci", t_ptr, 4))
187 dev->dev_type = DPAA2_CI;
188 else if (!strncmp("dpmcp", t_ptr, 5))
189 dev->dev_type = DPAA2_MPORTAL;
190 else if (!strncmp("dpdmai", t_ptr, 6))
191 dev->dev_type = DPAA2_QDMA;
192 else if (!strncmp("dpdmux", t_ptr, 6))
193 dev->dev_type = DPAA2_MUX;
194 else if (!strncmp("dprtc", t_ptr, 5))
195 dev->dev_type = DPAA2_DPRTC;
196 else if (!strncmp("dprc", t_ptr, 4))
197 dev->dev_type = DPAA2_DPRC;
199 dev->dev_type = DPAA2_UNKNOWN;
201 t_ptr = strtok(NULL, ".");
203 DPAA2_BUS_ERR("Skipping invalid device (%s)", dup_dev_name);
208 sscanf(t_ptr, "%hu", &dev->object_id);
209 dev->device.name = strdup(dev_name);
210 if (!dev->device.name) {
211 DPAA2_BUS_ERR("Unable to clone device name. Out of memory");
215 dev->device.devargs = fslmc_devargs_lookup(dev);
217 /* Update the device found into the device_count table */
218 rte_fslmc_bus.device_count[dev->dev_type]++;
220 /* Add device in the fslmc device list */
221 insert_in_device_list(dev);
223 /* Don't need the duplicated device filesystem entry anymore */
232 rte_intr_instance_free(dev->intr_handle);
239 rte_fslmc_parse(const char *name, void *addr)
244 uint8_t sep_exists = 0;
247 DPAA2_BUS_DEBUG("Parsing dev=(%s)", name);
249 /* There are multiple ways this can be called, with bus:dev, name=dev
250 * or just dev. In all cases, the 'addr' is actually a string.
252 sep = strchr(name, ':');
255 sep = strchr(name, '=');
263 /* Check if starting part if either of 'fslmc:' or 'name=', separator
267 /* If either of "fslmc" or "name" are starting part */
268 if (!strncmp(name, RTE_STR(FSLMC_BUS_NAME),
269 strlen(RTE_STR(FSLMC_BUS_NAME))) ||
270 (!strncmp(name, "name", strlen("name")))) {
273 DPAA2_BUS_DEBUG("Invalid device for matching (%s).",
282 /* Validate device name */
283 if (strncmp("dpni", sep, 4) &&
284 strncmp("dpseci", sep, 6) &&
285 strncmp("dpcon", sep, 5) &&
286 strncmp("dpbp", sep, 4) &&
287 strncmp("dpio", sep, 4) &&
288 strncmp("dpci", sep, 4) &&
289 strncmp("dpmcp", sep, 5) &&
290 strncmp("dpdmai", sep, 6) &&
291 strncmp("dpdmux", sep, 6)) {
292 DPAA2_BUS_DEBUG("Unknown or unsupported device (%s)", sep);
297 t_ptr = strchr(sep, '.');
298 if (!t_ptr || sscanf(t_ptr + 1, "%hu", &dev_id) != 1) {
299 DPAA2_BUS_ERR("Missing device id in device name (%s)", sep);
316 char fslmc_dirpath[PATH_MAX];
318 struct dirent *entry;
319 static int process_once;
323 DPAA2_BUS_DEBUG("Fslmc bus already scanned. Not rescanning");
328 ret = fslmc_get_container_group(&groupid);
332 /* Scan devices on the group */
333 sprintf(fslmc_dirpath, "%s/%s", SYSFS_FSL_MC_DEVICES, fslmc_container);
334 dir = opendir(fslmc_dirpath);
336 DPAA2_BUS_ERR("Unable to open VFIO group directory");
340 /* Scan the DPRC container object */
341 ret = scan_one_fslmc_device(fslmc_container);
343 /* Error in parsing directory - exit gracefully */
344 goto scan_fail_cleanup;
347 while ((entry = readdir(dir)) != NULL) {
348 if (entry->d_name[0] == '.' || entry->d_type != DT_DIR)
351 ret = scan_one_fslmc_device(entry->d_name);
353 /* Error in parsing directory - exit gracefully */
354 goto scan_fail_cleanup;
360 DPAA2_BUS_INFO("FSLMC Bus scan completed");
361 /* If debugging is enabled, device list is dumped to log output */
369 /* Remove all devices in the list */
370 cleanup_fslmc_device_list();
372 DPAA2_BUS_DEBUG("FSLMC Bus Not Available. Skipping (%d)", ret);
373 /* Irrespective of failure, scan only return success */
378 rte_fslmc_match(struct rte_dpaa2_driver *dpaa2_drv,
379 struct rte_dpaa2_device *dpaa2_dev)
381 if (dpaa2_drv->drv_type == dpaa2_dev->dev_type)
388 rte_fslmc_probe(void)
393 struct rte_dpaa2_device *dev;
394 struct rte_dpaa2_driver *drv;
396 static const struct rte_mbuf_dynfield dpaa2_seqn_dynfield_desc = {
397 .name = DPAA2_SEQN_DYNFIELD_NAME,
398 .size = sizeof(dpaa2_seqn_t),
399 .align = __alignof__(dpaa2_seqn_t),
402 if (TAILQ_EMPTY(&rte_fslmc_bus.device_list))
405 dpaa2_seqn_dynfield_offset =
406 rte_mbuf_dynfield_register(&dpaa2_seqn_dynfield_desc);
407 if (dpaa2_seqn_dynfield_offset < 0) {
408 DPAA2_BUS_ERR("Failed to register mbuf field for dpaa sequence number");
412 ret = fslmc_vfio_setup_group();
414 DPAA2_BUS_ERR("Unable to setup VFIO %d", ret);
418 /* Map existing segments as well as, in case of hotpluggable memory,
419 * install callback handler.
421 if (rte_eal_process_type() == RTE_PROC_PRIMARY) {
422 ret = rte_fslmc_vfio_dmamap();
424 DPAA2_BUS_ERR("Unable to DMA map existing VAs: (%d)",
426 /* Not continuing ahead */
427 DPAA2_BUS_ERR("FSLMC VFIO Mapping failed");
432 ret = fslmc_vfio_process_group();
434 DPAA2_BUS_ERR("Unable to setup devices %d", ret);
438 probe_all = rte_fslmc_bus.bus.conf.scan_mode != RTE_BUS_SCAN_ALLOWLIST;
440 /* In case of PA, the FD addresses returned by qbman APIs are physical
441 * addresses, which need conversion into equivalent VA address for
442 * rte_mbuf. For that, a table (a serial array, in memory) is used to
443 * increase translation efficiency.
444 * This has to be done before probe as some device initialization
445 * (during) probe allocate memory (dpaa2_sec) which needs to be pinned
448 * Error is ignored as relevant logs are handled within dpaax and
449 * handling for unavailable dpaax table too is transparent to caller.
451 * And, the IOVA table is only applicable in case of PA mode.
453 if (rte_eal_iova_mode() == RTE_IOVA_PA)
454 dpaax_iova_table_populate();
456 TAILQ_FOREACH(dev, &rte_fslmc_bus.device_list, next) {
457 TAILQ_FOREACH(drv, &rte_fslmc_bus.driver_list, next) {
458 ret = rte_fslmc_match(drv, dev);
465 if (rte_dev_is_probed(&dev->device))
468 if (dev->device.devargs &&
469 dev->device.devargs->policy == RTE_DEV_BLOCKED) {
470 DPAA2_BUS_LOG(DEBUG, "%s Blocked, skipping",
476 (dev->device.devargs &&
477 dev->device.devargs->policy == RTE_DEV_ALLOWED)) {
478 ret = drv->probe(drv, dev);
480 DPAA2_BUS_ERR("Unable to probe");
483 dev->device.driver = &drv->driver;
490 if (rte_eal_iova_mode() == RTE_IOVA_VA)
496 static struct rte_device *
497 rte_fslmc_find_device(const struct rte_device *start, rte_dev_cmp_t cmp,
500 const struct rte_dpaa2_device *dstart;
501 struct rte_dpaa2_device *dev;
503 DPAA2_BUS_DEBUG("Finding a device named %s\n", (const char *)data);
505 /* find_device is always called with an opaque object which should be
506 * passed along to the 'cmp' function iterating over all device obj
511 dstart = RTE_DEV_TO_FSLMC_CONST(start);
512 dev = TAILQ_NEXT(dstart, next);
514 dev = TAILQ_FIRST(&rte_fslmc_bus.device_list);
516 while (dev != NULL) {
517 if (cmp(&dev->device, data) == 0) {
518 DPAA2_BUS_DEBUG("Found device (%s)\n",
522 dev = TAILQ_NEXT(dev, next);
528 /*register a fslmc bus based dpaa2 driver */
530 rte_fslmc_driver_register(struct rte_dpaa2_driver *driver)
534 TAILQ_INSERT_TAIL(&rte_fslmc_bus.driver_list, driver, next);
535 /* Update Bus references */
536 driver->fslmc_bus = &rte_fslmc_bus;
539 /*un-register a fslmc bus based dpaa2 driver */
541 rte_fslmc_driver_unregister(struct rte_dpaa2_driver *driver)
543 struct rte_fslmc_bus *fslmc_bus;
545 fslmc_bus = driver->fslmc_bus;
547 /* Cleanup the PA->VA Translation table; From wherever this function
550 if (rte_eal_iova_mode() == RTE_IOVA_PA)
551 dpaax_iova_table_depopulate();
553 TAILQ_REMOVE(&fslmc_bus->driver_list, driver, next);
554 /* Update Bus references */
555 driver->fslmc_bus = NULL;
559 * All device has iova as va
562 fslmc_all_device_support_iova(void)
565 struct rte_dpaa2_device *dev;
566 struct rte_dpaa2_driver *drv;
568 TAILQ_FOREACH(dev, &rte_fslmc_bus.device_list, next) {
569 TAILQ_FOREACH(drv, &rte_fslmc_bus.driver_list, next) {
570 ret = rte_fslmc_match(drv, dev);
573 /* if the driver is not supporting IOVA */
574 if (!(drv->drv_flags & RTE_DPAA2_DRV_IOVA_AS_VA))
582 * Get iommu class of DPAA2 devices on the bus.
584 static enum rte_iova_mode
585 rte_dpaa2_get_iommu_class(void)
587 bool is_vfio_noiommu_enabled = 1;
590 if (TAILQ_EMPTY(&rte_fslmc_bus.device_list))
593 #ifdef RTE_LIBRTE_DPAA2_USE_PHYS_IOVA
597 /* check if all devices on the bus support Virtual addressing or not */
598 has_iova_va = fslmc_all_device_support_iova();
601 is_vfio_noiommu_enabled = rte_vfio_noiommu_is_enabled() == true ?
605 if (has_iova_va && !is_vfio_noiommu_enabled)
612 fslmc_bus_plug(struct rte_device *dev __rte_unused)
614 /* No operation is performed while plugging the device */
619 fslmc_bus_unplug(struct rte_device *dev __rte_unused)
621 /* No operation is performed while unplugging the device */
626 fslmc_bus_dev_iterate(const void *start, const char *str,
627 const struct rte_dev_iterator *it __rte_unused)
629 const struct rte_dpaa2_device *dstart;
630 struct rte_dpaa2_device *dev;
631 char *dup, *dev_name = NULL;
634 DPAA2_BUS_DEBUG("No device string");
638 /* Expectation is that device would be name=device_name */
639 if (strncmp(str, "name=", 5) != 0) {
640 DPAA2_BUS_DEBUG("Invalid device string (%s)\n", str);
644 /* Now that name=device_name format is available, split */
646 dev_name = dup + strlen("name=");
649 dstart = RTE_DEV_TO_FSLMC_CONST(start);
650 dev = TAILQ_NEXT(dstart, next);
652 dev = TAILQ_FIRST(&rte_fslmc_bus.device_list);
655 while (dev != NULL) {
656 if (strcmp(dev->device.name, dev_name) == 0) {
660 dev = TAILQ_NEXT(dev, next);
667 struct rte_fslmc_bus rte_fslmc_bus = {
669 .scan = rte_fslmc_scan,
670 .probe = rte_fslmc_probe,
671 .parse = rte_fslmc_parse,
672 .find_device = rte_fslmc_find_device,
673 .get_iommu_class = rte_dpaa2_get_iommu_class,
674 .plug = fslmc_bus_plug,
675 .unplug = fslmc_bus_unplug,
676 .dev_iterate = fslmc_bus_dev_iterate,
678 .device_list = TAILQ_HEAD_INITIALIZER(rte_fslmc_bus.device_list),
679 .driver_list = TAILQ_HEAD_INITIALIZER(rte_fslmc_bus.driver_list),
683 RTE_REGISTER_BUS(FSLMC_BUS_NAME, rte_fslmc_bus.bus);
684 RTE_LOG_REGISTER_DEFAULT(dpaa2_logtype_bus, NOTICE);