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 */
230 rte_intr_instance_free(dev->intr_handle);
237 rte_fslmc_parse(const char *name, void *addr)
242 uint8_t sep_exists = 0;
245 DPAA2_BUS_DEBUG("Parsing dev=(%s)", name);
247 /* There are multiple ways this can be called, with bus:dev, name=dev
248 * or just dev. In all cases, the 'addr' is actually a string.
250 sep = strchr(name, ':');
253 sep = strchr(name, '=');
261 /* Check if starting part if either of 'fslmc:' or 'name=', separator
265 /* If either of "fslmc" or "name" are starting part */
266 if (!strncmp(name, RTE_STR(FSLMC_BUS_NAME),
267 strlen(RTE_STR(FSLMC_BUS_NAME))) ||
268 (!strncmp(name, "name", strlen("name")))) {
271 DPAA2_BUS_DEBUG("Invalid device for matching (%s).",
280 /* Validate device name */
281 if (strncmp("dpni", sep, 4) &&
282 strncmp("dpseci", sep, 6) &&
283 strncmp("dpcon", sep, 5) &&
284 strncmp("dpbp", sep, 4) &&
285 strncmp("dpio", sep, 4) &&
286 strncmp("dpci", sep, 4) &&
287 strncmp("dpmcp", sep, 5) &&
288 strncmp("dpdmai", sep, 6) &&
289 strncmp("dpdmux", sep, 6)) {
290 DPAA2_BUS_DEBUG("Unknown or unsupported device (%s)", sep);
295 t_ptr = strchr(sep, '.');
296 if (!t_ptr || sscanf(t_ptr + 1, "%hu", &dev_id) != 1) {
297 DPAA2_BUS_ERR("Missing device id in device name (%s)", sep);
314 char fslmc_dirpath[PATH_MAX];
316 struct dirent *entry;
317 static int process_once;
321 DPAA2_BUS_DEBUG("Fslmc bus already scanned. Not rescanning");
326 ret = fslmc_get_container_group(&groupid);
330 /* Scan devices on the group */
331 sprintf(fslmc_dirpath, "%s/%s", SYSFS_FSL_MC_DEVICES, fslmc_container);
332 dir = opendir(fslmc_dirpath);
334 DPAA2_BUS_ERR("Unable to open VFIO group directory");
338 /* Scan the DPRC container object */
339 ret = scan_one_fslmc_device(fslmc_container);
341 /* Error in parsing directory - exit gracefully */
342 goto scan_fail_cleanup;
345 while ((entry = readdir(dir)) != NULL) {
346 if (entry->d_name[0] == '.' || entry->d_type != DT_DIR)
349 ret = scan_one_fslmc_device(entry->d_name);
351 /* Error in parsing directory - exit gracefully */
352 goto scan_fail_cleanup;
358 DPAA2_BUS_INFO("FSLMC Bus scan completed");
359 /* If debugging is enabled, device list is dumped to log output */
367 /* Remove all devices in the list */
368 cleanup_fslmc_device_list();
370 DPAA2_BUS_DEBUG("FSLMC Bus Not Available. Skipping (%d)", ret);
371 /* Irrespective of failure, scan only return success */
376 rte_fslmc_match(struct rte_dpaa2_driver *dpaa2_drv,
377 struct rte_dpaa2_device *dpaa2_dev)
379 if (dpaa2_drv->drv_type == dpaa2_dev->dev_type)
386 rte_fslmc_probe(void)
391 struct rte_dpaa2_device *dev;
392 struct rte_dpaa2_driver *drv;
394 static const struct rte_mbuf_dynfield dpaa2_seqn_dynfield_desc = {
395 .name = DPAA2_SEQN_DYNFIELD_NAME,
396 .size = sizeof(dpaa2_seqn_t),
397 .align = __alignof__(dpaa2_seqn_t),
400 if (TAILQ_EMPTY(&rte_fslmc_bus.device_list))
403 dpaa2_seqn_dynfield_offset =
404 rte_mbuf_dynfield_register(&dpaa2_seqn_dynfield_desc);
405 if (dpaa2_seqn_dynfield_offset < 0) {
406 DPAA2_BUS_ERR("Failed to register mbuf field for dpaa sequence number");
410 ret = fslmc_vfio_setup_group();
412 DPAA2_BUS_ERR("Unable to setup VFIO %d", ret);
416 /* Map existing segments as well as, in case of hotpluggable memory,
417 * install callback handler.
419 if (rte_eal_process_type() == RTE_PROC_PRIMARY) {
420 ret = rte_fslmc_vfio_dmamap();
422 DPAA2_BUS_ERR("Unable to DMA map existing VAs: (%d)",
424 /* Not continuing ahead */
425 DPAA2_BUS_ERR("FSLMC VFIO Mapping failed");
430 ret = fslmc_vfio_process_group();
432 DPAA2_BUS_ERR("Unable to setup devices %d", ret);
436 probe_all = rte_fslmc_bus.bus.conf.scan_mode != RTE_BUS_SCAN_ALLOWLIST;
438 /* In case of PA, the FD addresses returned by qbman APIs are physical
439 * addresses, which need conversion into equivalent VA address for
440 * rte_mbuf. For that, a table (a serial array, in memory) is used to
441 * increase translation efficiency.
442 * This has to be done before probe as some device initialization
443 * (during) probe allocate memory (dpaa2_sec) which needs to be pinned
446 * Error is ignored as relevant logs are handled within dpaax and
447 * handling for unavailable dpaax table too is transparent to caller.
449 * And, the IOVA table is only applicable in case of PA mode.
451 if (rte_eal_iova_mode() == RTE_IOVA_PA)
452 dpaax_iova_table_populate();
454 TAILQ_FOREACH(dev, &rte_fslmc_bus.device_list, next) {
455 TAILQ_FOREACH(drv, &rte_fslmc_bus.driver_list, next) {
456 ret = rte_fslmc_match(drv, dev);
463 if (rte_dev_is_probed(&dev->device))
466 if (dev->device.devargs &&
467 dev->device.devargs->policy == RTE_DEV_BLOCKED) {
468 DPAA2_BUS_LOG(DEBUG, "%s Blocked, skipping",
474 (dev->device.devargs &&
475 dev->device.devargs->policy == RTE_DEV_ALLOWED)) {
476 ret = drv->probe(drv, dev);
478 DPAA2_BUS_ERR("Unable to probe");
481 dev->device.driver = &drv->driver;
488 if (rte_eal_iova_mode() == RTE_IOVA_VA)
494 static struct rte_device *
495 rte_fslmc_find_device(const struct rte_device *start, rte_dev_cmp_t cmp,
498 const struct rte_dpaa2_device *dstart;
499 struct rte_dpaa2_device *dev;
501 DPAA2_BUS_DEBUG("Finding a device named %s\n", (const char *)data);
503 /* find_device is always called with an opaque object which should be
504 * passed along to the 'cmp' function iterating over all device obj
509 dstart = RTE_DEV_TO_FSLMC_CONST(start);
510 dev = TAILQ_NEXT(dstart, next);
512 dev = TAILQ_FIRST(&rte_fslmc_bus.device_list);
514 while (dev != NULL) {
515 if (cmp(&dev->device, data) == 0) {
516 DPAA2_BUS_DEBUG("Found device (%s)\n",
520 dev = TAILQ_NEXT(dev, next);
526 /*register a fslmc bus based dpaa2 driver */
528 rte_fslmc_driver_register(struct rte_dpaa2_driver *driver)
532 TAILQ_INSERT_TAIL(&rte_fslmc_bus.driver_list, driver, next);
533 /* Update Bus references */
534 driver->fslmc_bus = &rte_fslmc_bus;
537 /*un-register a fslmc bus based dpaa2 driver */
539 rte_fslmc_driver_unregister(struct rte_dpaa2_driver *driver)
541 struct rte_fslmc_bus *fslmc_bus;
543 fslmc_bus = driver->fslmc_bus;
545 /* Cleanup the PA->VA Translation table; From wherever this function
548 if (rte_eal_iova_mode() == RTE_IOVA_PA)
549 dpaax_iova_table_depopulate();
551 TAILQ_REMOVE(&fslmc_bus->driver_list, driver, next);
552 /* Update Bus references */
553 driver->fslmc_bus = NULL;
557 * All device has iova as va
560 fslmc_all_device_support_iova(void)
563 struct rte_dpaa2_device *dev;
564 struct rte_dpaa2_driver *drv;
566 TAILQ_FOREACH(dev, &rte_fslmc_bus.device_list, next) {
567 TAILQ_FOREACH(drv, &rte_fslmc_bus.driver_list, next) {
568 ret = rte_fslmc_match(drv, dev);
571 /* if the driver is not supporting IOVA */
572 if (!(drv->drv_flags & RTE_DPAA2_DRV_IOVA_AS_VA))
580 * Get iommu class of DPAA2 devices on the bus.
582 static enum rte_iova_mode
583 rte_dpaa2_get_iommu_class(void)
585 bool is_vfio_noiommu_enabled = 1;
588 if (TAILQ_EMPTY(&rte_fslmc_bus.device_list))
591 #ifdef RTE_LIBRTE_DPAA2_USE_PHYS_IOVA
595 /* check if all devices on the bus support Virtual addressing or not */
596 has_iova_va = fslmc_all_device_support_iova();
599 is_vfio_noiommu_enabled = rte_vfio_noiommu_is_enabled() == true ?
603 if (has_iova_va && !is_vfio_noiommu_enabled)
610 fslmc_bus_plug(struct rte_device *dev __rte_unused)
612 /* No operation is performed while plugging the device */
617 fslmc_bus_unplug(struct rte_device *dev __rte_unused)
619 /* No operation is performed while unplugging the device */
624 fslmc_bus_dev_iterate(const void *start, const char *str,
625 const struct rte_dev_iterator *it __rte_unused)
627 const struct rte_dpaa2_device *dstart;
628 struct rte_dpaa2_device *dev;
629 char *dup, *dev_name = NULL;
632 DPAA2_BUS_DEBUG("No device string");
636 /* Expectation is that device would be name=device_name */
637 if (strncmp(str, "name=", 5) != 0) {
638 DPAA2_BUS_DEBUG("Invalid device string (%s)\n", str);
642 /* Now that name=device_name format is available, split */
644 dev_name = dup + strlen("name=");
647 dstart = RTE_DEV_TO_FSLMC_CONST(start);
648 dev = TAILQ_NEXT(dstart, next);
650 dev = TAILQ_FIRST(&rte_fslmc_bus.device_list);
653 while (dev != NULL) {
654 if (strcmp(dev->device.name, dev_name) == 0) {
658 dev = TAILQ_NEXT(dev, next);
665 struct rte_fslmc_bus rte_fslmc_bus = {
667 .scan = rte_fslmc_scan,
668 .probe = rte_fslmc_probe,
669 .parse = rte_fslmc_parse,
670 .find_device = rte_fslmc_find_device,
671 .get_iommu_class = rte_dpaa2_get_iommu_class,
672 .plug = fslmc_bus_plug,
673 .unplug = fslmc_bus_unplug,
674 .dev_iterate = fslmc_bus_dev_iterate,
676 .device_list = TAILQ_HEAD_INITIALIZER(rte_fslmc_bus.device_list),
677 .driver_list = TAILQ_HEAD_INITIALIZER(rte_fslmc_bus.driver_list),
681 RTE_REGISTER_BUS(FSLMC_BUS_NAME, rte_fslmc_bus.bus);
682 RTE_LOG_REGISTER_DEFAULT(dpaa2_logtype_bus, NOTICE);