1 /* SPDX-License-Identifier: BSD-3-Clause
3 * Copyright 2016,2018-2019 NXP
13 #include <rte_malloc.h>
14 #include <rte_devargs.h>
15 #include <rte_memcpy.h>
16 #include <rte_ethdev_driver.h>
18 #include <rte_fslmc.h>
19 #include <fslmc_vfio.h>
20 #include "fslmc_logs.h"
22 #include <dpaax_iova_table.h>
24 #define VFIO_IOMMU_GROUP_PATH "/sys/kernel/iommu_groups"
25 #define FSLMC_BUS_NAME fslmc
27 struct rte_fslmc_bus rte_fslmc_bus;
28 uint8_t dpaa2_virt_mode;
31 rte_fslmc_get_device_count(enum rte_dpaa2_dev_type device_type)
33 if (device_type >= DPAA2_DEVTYPE_MAX)
35 return rte_fslmc_bus.device_count[device_type];
39 cleanup_fslmc_device_list(void)
41 struct rte_dpaa2_device *dev;
42 struct rte_dpaa2_device *t_dev;
44 TAILQ_FOREACH_SAFE(dev, &rte_fslmc_bus.device_list, next, t_dev) {
45 TAILQ_REMOVE(&rte_fslmc_bus.device_list, dev, next);
52 compare_dpaa2_devname(struct rte_dpaa2_device *dev1,
53 struct rte_dpaa2_device *dev2)
57 if (dev1->dev_type > dev2->dev_type) {
59 } else if (dev1->dev_type < dev2->dev_type) {
62 /* Check the ID as types match */
63 if (dev1->object_id > dev2->object_id)
65 else if (dev1->object_id < dev2->object_id)
68 comp = 0; /* Duplicate device name */
75 insert_in_device_list(struct rte_dpaa2_device *newdev)
77 int comp, inserted = 0;
78 struct rte_dpaa2_device *dev = NULL;
79 struct rte_dpaa2_device *tdev = NULL;
81 TAILQ_FOREACH_SAFE(dev, &rte_fslmc_bus.device_list, next, tdev) {
82 comp = compare_dpaa2_devname(newdev, dev);
84 TAILQ_INSERT_BEFORE(dev, newdev, next);
91 TAILQ_INSERT_TAIL(&rte_fslmc_bus.device_list, newdev, next);
94 static struct rte_devargs *
95 fslmc_devargs_lookup(struct rte_dpaa2_device *dev)
97 struct rte_devargs *devargs;
100 RTE_EAL_DEVARGS_FOREACH("fslmc", devargs) {
101 devargs->bus->parse(devargs->name, &dev_name);
102 if (strcmp(dev_name, dev->device.name) == 0) {
103 DPAA2_BUS_INFO("**Devargs matched %s", dev_name);
111 dump_device_list(void)
113 struct rte_dpaa2_device *dev;
115 /* Only if the log level has been set to Debugging, print list */
116 if (rte_log_can_log(dpaa2_logtype_bus, RTE_LOG_DEBUG)) {
117 DPAA2_BUS_LOG(DEBUG, "List of devices scanned on bus:");
118 TAILQ_FOREACH(dev, &rte_fslmc_bus.device_list, next) {
119 DPAA2_BUS_LOG(DEBUG, "\t\t%s", dev->device.name);
125 scan_one_fslmc_device(char *dev_name)
127 char *dup_dev_name, *t_ptr;
128 struct rte_dpaa2_device *dev = NULL;
134 /* Ignore the Container name itself */
135 if (!strncmp("dprc", dev_name, 4))
138 /* Creating a temporary copy to perform cut-parse over string */
139 dup_dev_name = strdup(dev_name);
141 DPAA2_BUS_ERR("Unable to allocate device name memory");
145 /* For all other devices, we allocate rte_dpaa2_device.
146 * For those devices where there is no driver, probe would release
147 * the memory associated with the rte_dpaa2_device after necessary
150 dev = calloc(1, sizeof(struct rte_dpaa2_device));
152 DPAA2_BUS_ERR("Unable to allocate device object");
157 dev->device.bus = &rte_fslmc_bus.bus;
159 /* Parse the device name and ID */
160 t_ptr = strtok(dup_dev_name, ".");
162 DPAA2_BUS_ERR("Invalid device found: (%s)", dup_dev_name);
166 if (!strncmp("dpni", t_ptr, 4))
167 dev->dev_type = DPAA2_ETH;
168 else if (!strncmp("dpseci", t_ptr, 6))
169 dev->dev_type = DPAA2_CRYPTO;
170 else if (!strncmp("dpcon", t_ptr, 5))
171 dev->dev_type = DPAA2_CON;
172 else if (!strncmp("dpbp", t_ptr, 4))
173 dev->dev_type = DPAA2_BPOOL;
174 else if (!strncmp("dpio", t_ptr, 4))
175 dev->dev_type = DPAA2_IO;
176 else if (!strncmp("dpci", t_ptr, 4))
177 dev->dev_type = DPAA2_CI;
178 else if (!strncmp("dpmcp", t_ptr, 5))
179 dev->dev_type = DPAA2_MPORTAL;
180 else if (!strncmp("dpdmai", t_ptr, 6))
181 dev->dev_type = DPAA2_QDMA;
182 else if (!strncmp("dpdmux", t_ptr, 6))
183 dev->dev_type = DPAA2_MUX;
184 else if (!strncmp("dprtc", t_ptr, 5))
185 dev->dev_type = DPAA2_DPRTC;
187 dev->dev_type = DPAA2_UNKNOWN;
189 t_ptr = strtok(NULL, ".");
191 DPAA2_BUS_ERR("Skipping invalid device (%s)", dup_dev_name);
196 sscanf(t_ptr, "%hu", &dev->object_id);
197 dev->device.name = strdup(dev_name);
198 if (!dev->device.name) {
199 DPAA2_BUS_ERR("Unable to clone device name. Out of memory");
203 dev->device.devargs = fslmc_devargs_lookup(dev);
205 /* Update the device found into the device_count table */
206 rte_fslmc_bus.device_count[dev->dev_type]++;
208 /* Add device in the fslmc device list */
209 insert_in_device_list(dev);
211 /* Don't need the duplicated device filesystem entry anymore */
225 rte_fslmc_parse(const char *name, void *addr)
230 uint8_t sep_exists = 0;
233 DPAA2_BUS_DEBUG("Parsing dev=(%s)", name);
235 /* There are multiple ways this can be called, with bus:dev, name=dev
236 * or just dev. In all cases, the 'addr' is actually a string.
238 sep = strchr(name, ':');
241 sep = strchr(name, '=');
249 /* Check if starting part if either of 'fslmc:' or 'name=', separator
253 /* If either of "fslmc" or "name" are starting part */
254 if (!strncmp(name, RTE_STR(FSLMC_BUS_NAME),
255 strlen(RTE_STR(FSLMC_BUS_NAME))) ||
256 (!strncmp(name, "name", strlen("name")))) {
259 DPAA2_BUS_DEBUG("Invalid device for matching (%s).",
268 /* Validate device name */
269 if (strncmp("dpni", sep, 4) &&
270 strncmp("dpseci", sep, 6) &&
271 strncmp("dpcon", sep, 5) &&
272 strncmp("dpbp", sep, 4) &&
273 strncmp("dpio", sep, 4) &&
274 strncmp("dpci", sep, 4) &&
275 strncmp("dpmcp", sep, 5) &&
276 strncmp("dpdmai", sep, 6) &&
277 strncmp("dpdmux", sep, 6)) {
278 DPAA2_BUS_DEBUG("Unknown or unsupported device (%s)", sep);
283 t_ptr = strchr(sep, '.');
284 if (!t_ptr || sscanf(t_ptr + 1, "%hu", &dev_id) != 1) {
285 DPAA2_BUS_ERR("Missing device id in device name (%s)", sep);
302 int device_count = 0;
303 char fslmc_dirpath[PATH_MAX];
305 struct dirent *entry;
306 static int process_once;
310 DPAA2_BUS_DEBUG("Fslmc bus already scanned. Not rescanning");
315 ret = fslmc_get_container_group(&groupid);
319 /* Scan devices on the group */
320 sprintf(fslmc_dirpath, "%s/%s", SYSFS_FSL_MC_DEVICES, fslmc_container);
321 dir = opendir(fslmc_dirpath);
323 DPAA2_BUS_ERR("Unable to open VFIO group directory");
327 while ((entry = readdir(dir)) != NULL) {
328 if (entry->d_name[0] == '.' || entry->d_type != DT_DIR)
331 ret = scan_one_fslmc_device(entry->d_name);
333 /* Error in parsing directory - exit gracefully */
334 goto scan_fail_cleanup;
341 DPAA2_BUS_INFO("FSLMC Bus scan completed");
342 /* If debugging is enabled, device list is dumped to log output */
350 /* Remove all devices in the list */
351 cleanup_fslmc_device_list();
353 DPAA2_BUS_DEBUG("FSLMC Bus Not Available. Skipping (%d)", ret);
354 /* Irrespective of failure, scan only return success */
359 rte_fslmc_match(struct rte_dpaa2_driver *dpaa2_drv,
360 struct rte_dpaa2_device *dpaa2_dev)
362 if (dpaa2_drv->drv_type == dpaa2_dev->dev_type)
369 rte_fslmc_probe(void)
374 struct rte_dpaa2_device *dev;
375 struct rte_dpaa2_driver *drv;
377 if (TAILQ_EMPTY(&rte_fslmc_bus.device_list))
380 ret = fslmc_vfio_setup_group();
382 DPAA2_BUS_ERR("Unable to setup VFIO %d", ret);
386 /* Map existing segments as well as, in case of hotpluggable memory,
387 * install callback handler.
389 if (rte_eal_process_type() == RTE_PROC_PRIMARY) {
390 ret = rte_fslmc_vfio_dmamap();
392 DPAA2_BUS_ERR("Unable to DMA map existing VAs: (%d)",
394 /* Not continuing ahead */
395 DPAA2_BUS_ERR("FSLMC VFIO Mapping failed");
400 ret = fslmc_vfio_process_group();
402 DPAA2_BUS_ERR("Unable to setup devices %d", ret);
406 probe_all = rte_fslmc_bus.bus.conf.scan_mode != RTE_BUS_SCAN_WHITELIST;
408 /* In case of PA, the FD addresses returned by qbman APIs are physical
409 * addresses, which need conversion into equivalent VA address for
410 * rte_mbuf. For that, a table (a serial array, in memory) is used to
411 * increase translation efficiency.
412 * This has to be done before probe as some device initialization
413 * (during) probe allocate memory (dpaa2_sec) which needs to be pinned
416 * Error is ignored as relevant logs are handled within dpaax and
417 * handling for unavailable dpaax table too is transparent to caller.
419 * And, the IOVA table is only applicable in case of PA mode.
421 if (rte_eal_iova_mode() == RTE_IOVA_PA)
422 dpaax_iova_table_populate();
424 TAILQ_FOREACH(dev, &rte_fslmc_bus.device_list, next) {
425 TAILQ_FOREACH(drv, &rte_fslmc_bus.driver_list, next) {
426 ret = rte_fslmc_match(drv, dev);
433 if (rte_dev_is_probed(&dev->device))
436 if (dev->device.devargs &&
437 dev->device.devargs->policy == RTE_DEV_BLACKLISTED) {
438 DPAA2_BUS_LOG(DEBUG, "%s Blacklisted, skipping",
444 (dev->device.devargs &&
445 dev->device.devargs->policy ==
446 RTE_DEV_WHITELISTED)) {
447 ret = drv->probe(drv, dev);
449 DPAA2_BUS_ERR("Unable to probe");
452 dev->device.driver = &drv->driver;
459 if (rte_eal_iova_mode() == RTE_IOVA_VA)
465 static struct rte_device *
466 rte_fslmc_find_device(const struct rte_device *start, rte_dev_cmp_t cmp,
469 const struct rte_dpaa2_device *dstart;
470 struct rte_dpaa2_device *dev;
472 DPAA2_BUS_DEBUG("Finding a device named %s\n", (const char *)data);
474 /* find_device is always called with an opaque object which should be
475 * passed along to the 'cmp' function iterating over all device obj
480 dstart = RTE_DEV_TO_FSLMC_CONST(start);
481 dev = TAILQ_NEXT(dstart, next);
483 dev = TAILQ_FIRST(&rte_fslmc_bus.device_list);
485 while (dev != NULL) {
486 if (cmp(&dev->device, data) == 0) {
487 DPAA2_BUS_DEBUG("Found device (%s)\n",
491 dev = TAILQ_NEXT(dev, next);
497 /*register a fslmc bus based dpaa2 driver */
499 rte_fslmc_driver_register(struct rte_dpaa2_driver *driver)
503 TAILQ_INSERT_TAIL(&rte_fslmc_bus.driver_list, driver, next);
504 /* Update Bus references */
505 driver->fslmc_bus = &rte_fslmc_bus;
508 /*un-register a fslmc bus based dpaa2 driver */
510 rte_fslmc_driver_unregister(struct rte_dpaa2_driver *driver)
512 struct rte_fslmc_bus *fslmc_bus;
514 fslmc_bus = driver->fslmc_bus;
516 /* Cleanup the PA->VA Translation table; From whereever this function
519 if (rte_eal_iova_mode() == RTE_IOVA_PA)
520 dpaax_iova_table_depopulate();
522 TAILQ_REMOVE(&fslmc_bus->driver_list, driver, next);
523 /* Update Bus references */
524 driver->fslmc_bus = NULL;
528 * All device has iova as va
531 fslmc_all_device_support_iova(void)
534 struct rte_dpaa2_device *dev;
535 struct rte_dpaa2_driver *drv;
537 TAILQ_FOREACH(dev, &rte_fslmc_bus.device_list, next) {
538 TAILQ_FOREACH(drv, &rte_fslmc_bus.driver_list, next) {
539 ret = rte_fslmc_match(drv, dev);
542 /* if the driver is not supporting IOVA */
543 if (!(drv->drv_flags & RTE_DPAA2_DRV_IOVA_AS_VA))
551 * Get iommu class of DPAA2 devices on the bus.
553 static enum rte_iova_mode
554 rte_dpaa2_get_iommu_class(void)
556 bool is_vfio_noiommu_enabled = 1;
559 if (TAILQ_EMPTY(&rte_fslmc_bus.device_list))
562 #ifdef RTE_LIBRTE_DPAA2_USE_PHYS_IOVA
566 /* check if all devices on the bus support Virtual addressing or not */
567 has_iova_va = fslmc_all_device_support_iova();
570 is_vfio_noiommu_enabled = rte_vfio_noiommu_is_enabled() == true ?
574 if (has_iova_va && !is_vfio_noiommu_enabled)
581 fslmc_bus_plug(struct rte_device *dev __rte_unused)
583 /* No operation is performed while plugging the device */
588 fslmc_bus_unplug(struct rte_device *dev __rte_unused)
590 /* No operation is performed while unplugging the device */
595 fslmc_bus_dev_iterate(const void *start, const char *str,
596 const struct rte_dev_iterator *it __rte_unused)
598 const struct rte_dpaa2_device *dstart;
599 struct rte_dpaa2_device *dev;
600 char *dup, *dev_name = NULL;
603 DPAA2_BUS_DEBUG("No device string");
607 /* Expectation is that device would be name=device_name */
608 if (strncmp(str, "name=", 5) != 0) {
609 DPAA2_BUS_DEBUG("Invalid device string (%s)\n", str);
613 /* Now that name=device_name format is available, split */
615 dev_name = dup + strlen("name=");
618 dstart = RTE_DEV_TO_FSLMC_CONST(start);
619 dev = TAILQ_NEXT(dstart, next);
621 dev = TAILQ_FIRST(&rte_fslmc_bus.device_list);
624 while (dev != NULL) {
625 if (strcmp(dev->device.name, dev_name) == 0) {
629 dev = TAILQ_NEXT(dev, next);
636 struct rte_fslmc_bus rte_fslmc_bus = {
638 .scan = rte_fslmc_scan,
639 .probe = rte_fslmc_probe,
640 .parse = rte_fslmc_parse,
641 .find_device = rte_fslmc_find_device,
642 .get_iommu_class = rte_dpaa2_get_iommu_class,
643 .plug = fslmc_bus_plug,
644 .unplug = fslmc_bus_unplug,
645 .dev_iterate = fslmc_bus_dev_iterate,
647 .device_list = TAILQ_HEAD_INITIALIZER(rte_fslmc_bus.device_list),
648 .driver_list = TAILQ_HEAD_INITIALIZER(rte_fslmc_bus.driver_list),
652 RTE_REGISTER_BUS(FSLMC_BUS_NAME, rte_fslmc_bus.bus);
653 RTE_LOG_REGISTER(dpaa2_logtype_bus, bus.fslmc, NOTICE);