1 /* SPDX-License-Identifier: BSD-3-Clause
3 * Copyright 2016,2018 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 int dpaa2_logtype_bus;
26 #define VFIO_IOMMU_GROUP_PATH "/sys/kernel/iommu_groups"
27 #define FSLMC_BUS_NAME fslmc
29 struct rte_fslmc_bus rte_fslmc_bus;
30 uint8_t dpaa2_virt_mode;
33 rte_fslmc_get_device_count(enum rte_dpaa2_dev_type device_type)
35 if (device_type >= DPAA2_DEVTYPE_MAX)
37 return rte_fslmc_bus.device_count[device_type];
40 RTE_DEFINE_PER_LCORE(struct dpaa2_portal_dqrr, dpaa2_held_bufs);
43 cleanup_fslmc_device_list(void)
45 struct rte_dpaa2_device *dev;
46 struct rte_dpaa2_device *t_dev;
48 TAILQ_FOREACH_SAFE(dev, &rte_fslmc_bus.device_list, next, t_dev) {
49 TAILQ_REMOVE(&rte_fslmc_bus.device_list, dev, next);
56 compare_dpaa2_devname(struct rte_dpaa2_device *dev1,
57 struct rte_dpaa2_device *dev2)
61 if (dev1->dev_type > dev2->dev_type) {
63 } else if (dev1->dev_type < dev2->dev_type) {
66 /* Check the ID as types match */
67 if (dev1->object_id > dev2->object_id)
69 else if (dev1->object_id < dev2->object_id)
72 comp = 0; /* Duplicate device name */
79 insert_in_device_list(struct rte_dpaa2_device *newdev)
81 int comp, inserted = 0;
82 struct rte_dpaa2_device *dev = NULL;
83 struct rte_dpaa2_device *tdev = NULL;
85 TAILQ_FOREACH_SAFE(dev, &rte_fslmc_bus.device_list, next, tdev) {
86 comp = compare_dpaa2_devname(newdev, dev);
88 TAILQ_INSERT_BEFORE(dev, newdev, next);
95 TAILQ_INSERT_TAIL(&rte_fslmc_bus.device_list, newdev, next);
98 static struct rte_devargs *
99 fslmc_devargs_lookup(struct rte_dpaa2_device *dev)
101 struct rte_devargs *devargs;
104 RTE_EAL_DEVARGS_FOREACH("fslmc", devargs) {
105 devargs->bus->parse(devargs->name, &dev_name);
106 if (strcmp(dev_name, dev->device.name) == 0) {
107 DPAA2_BUS_INFO("**Devargs matched %s", dev_name);
115 dump_device_list(void)
117 struct rte_dpaa2_device *dev;
118 uint32_t global_log_level;
121 /* Only if the log level has been set to Debugging, print list */
122 global_log_level = rte_log_get_global_level();
123 local_log_level = rte_log_get_level(dpaa2_logtype_bus);
124 if (global_log_level == RTE_LOG_DEBUG ||
125 local_log_level == RTE_LOG_DEBUG) {
126 DPAA2_BUS_LOG(DEBUG, "List of devices scanned on bus:");
127 TAILQ_FOREACH(dev, &rte_fslmc_bus.device_list, next) {
128 DPAA2_BUS_LOG(DEBUG, "\t\t%s", dev->device.name);
134 scan_one_fslmc_device(char *dev_name)
136 char *dup_dev_name, *t_ptr;
137 struct rte_dpaa2_device *dev = NULL;
143 /* Ignore the Container name itself */
144 if (!strncmp("dprc", dev_name, 4))
147 /* Creating a temporary copy to perform cut-parse over string */
148 dup_dev_name = strdup(dev_name);
150 DPAA2_BUS_ERR("Unable to allocate device name memory");
154 /* For all other devices, we allocate rte_dpaa2_device.
155 * For those devices where there is no driver, probe would release
156 * the memory associated with the rte_dpaa2_device after necessary
159 dev = calloc(1, sizeof(struct rte_dpaa2_device));
161 DPAA2_BUS_ERR("Unable to allocate device object");
166 dev->device.bus = &rte_fslmc_bus.bus;
168 /* Parse the device name and ID */
169 t_ptr = strtok(dup_dev_name, ".");
171 DPAA2_BUS_ERR("Invalid device found: (%s)", dup_dev_name);
175 if (!strncmp("dpni", t_ptr, 4))
176 dev->dev_type = DPAA2_ETH;
177 else if (!strncmp("dpseci", t_ptr, 6))
178 dev->dev_type = DPAA2_CRYPTO;
179 else if (!strncmp("dpcon", t_ptr, 5))
180 dev->dev_type = DPAA2_CON;
181 else if (!strncmp("dpbp", t_ptr, 4))
182 dev->dev_type = DPAA2_BPOOL;
183 else if (!strncmp("dpio", t_ptr, 4))
184 dev->dev_type = DPAA2_IO;
185 else if (!strncmp("dpci", t_ptr, 4))
186 dev->dev_type = DPAA2_CI;
187 else if (!strncmp("dpmcp", t_ptr, 5))
188 dev->dev_type = DPAA2_MPORTAL;
189 else if (!strncmp("dpdmai", t_ptr, 6))
190 dev->dev_type = DPAA2_QDMA;
191 else if (!strncmp("dpdmux", t_ptr, 6))
192 dev->dev_type = DPAA2_MUX;
193 else if (!strncmp("dprtc", t_ptr, 5))
194 dev->dev_type = DPAA2_DPRTC;
196 dev->dev_type = DPAA2_UNKNOWN;
198 t_ptr = strtok(NULL, ".");
200 DPAA2_BUS_ERR("Skipping invalid device (%s)", dup_dev_name);
205 sscanf(t_ptr, "%hu", &dev->object_id);
206 dev->device.name = strdup(dev_name);
207 if (!dev->device.name) {
208 DPAA2_BUS_ERR("Unable to clone device name. Out of memory");
212 dev->device.devargs = fslmc_devargs_lookup(dev);
214 /* Update the device found into the device_count table */
215 rte_fslmc_bus.device_count[dev->dev_type]++;
217 /* Add device in the fslmc device list */
218 insert_in_device_list(dev);
220 /* Don't need the duplicated device filesystem entry anymore */
234 rte_fslmc_parse(const char *name, void *addr)
239 uint8_t sep_exists = 0;
241 DPAA2_BUS_DEBUG("Parsing dev=(%s)", name);
243 /* There are multiple ways this can be called, with bus:dev, name=dev
244 * or just dev. In all cases, the 'addr' is actually a string.
246 sep = strchr(name, ':');
249 sep = strchr(name, '=');
257 /* Check if starting part if either of 'fslmc:' or 'name=', separator
261 /* If either of "fslmc" or "name" are starting part */
262 if (!strncmp(name, RTE_STR(FSLMC_BUS_NAME),
263 strlen(RTE_STR(FSLMC_BUS_NAME))) ||
264 (!strncmp(name, "name", strlen("name")))) {
267 DPAA2_BUS_DEBUG("Invalid device for matching (%s).",
275 /* Validate device name */
276 if (strncmp("dpni", sep, 4) &&
277 strncmp("dpseci", sep, 6) &&
278 strncmp("dpcon", sep, 5) &&
279 strncmp("dpbp", sep, 4) &&
280 strncmp("dpio", sep, 4) &&
281 strncmp("dpci", sep, 4) &&
282 strncmp("dpmcp", sep, 5) &&
283 strncmp("dpdmai", sep, 6) &&
284 strncmp("dpdmux", sep, 6)) {
285 DPAA2_BUS_DEBUG("Unknown or unsupported device (%s)", sep);
289 t_ptr = strchr(sep, '.');
290 if (!t_ptr || sscanf(t_ptr + 1, "%hu", &dev_id) != 1) {
291 DPAA2_BUS_ERR("Missing device id in device name (%s)", sep);
300 if (!sep_exists && sep)
309 int device_count = 0;
310 char fslmc_dirpath[PATH_MAX];
312 struct dirent *entry;
313 static int process_once;
317 DPAA2_BUS_DEBUG("Fslmc bus already scanned. Not rescanning");
322 ret = fslmc_get_container_group(&groupid);
326 /* Scan devices on the group */
327 snprintf(fslmc_dirpath, sizeof(fslmc_dirpath), "%s/%d/devices",
328 VFIO_IOMMU_GROUP_PATH, groupid);
329 dir = opendir(fslmc_dirpath);
331 DPAA2_BUS_ERR("Unable to open VFIO group directory");
335 while ((entry = readdir(dir)) != NULL) {
336 if (entry->d_name[0] == '.' || entry->d_type != DT_LNK)
339 ret = scan_one_fslmc_device(entry->d_name);
341 /* Error in parsing directory - exit gracefully */
342 goto scan_fail_cleanup;
349 DPAA2_BUS_INFO("FSLMC Bus scan completed");
350 /* If debugging is enabled, device list is dumped to log output */
358 /* Remove all devices in the list */
359 cleanup_fslmc_device_list();
361 DPAA2_BUS_DEBUG("FSLMC Bus Not Available. Skipping (%d)", ret);
362 /* Irrespective of failure, scan only return success */
367 rte_fslmc_match(struct rte_dpaa2_driver *dpaa2_drv,
368 struct rte_dpaa2_device *dpaa2_dev)
370 if (dpaa2_drv->drv_type == dpaa2_dev->dev_type)
377 rte_fslmc_probe(void)
382 struct rte_dpaa2_device *dev;
383 struct rte_dpaa2_driver *drv;
385 if (TAILQ_EMPTY(&rte_fslmc_bus.device_list))
388 ret = fslmc_vfio_setup_group();
390 DPAA2_BUS_ERR("Unable to setup VFIO %d", ret);
394 /* Map existing segments as well as, in case of hotpluggable memory,
395 * install callback handler.
397 if (rte_eal_process_type() == RTE_PROC_PRIMARY) {
398 ret = rte_fslmc_vfio_dmamap();
400 DPAA2_BUS_ERR("Unable to DMA map existing VAs: (%d)",
402 /* Not continuing ahead */
403 DPAA2_BUS_ERR("FSLMC VFIO Mapping failed");
408 ret = fslmc_vfio_process_group();
410 DPAA2_BUS_ERR("Unable to setup devices %d", ret);
414 probe_all = rte_fslmc_bus.bus.conf.scan_mode != RTE_BUS_SCAN_WHITELIST;
416 /* In case of PA, the FD addresses returned by qbman APIs are physical
417 * addresses, which need conversion into equivalent VA address for
418 * rte_mbuf. For that, a table (a serial array, in memory) is used to
419 * increase translation efficiency.
420 * This has to be done before probe as some device initialization
421 * (during) probe allocate memory (dpaa2_sec) which needs to be pinned
424 * Error is ignored as relevant logs are handled within dpaax and
425 * handling for unavailable dpaax table too is transparent to caller.
427 * And, the IOVA table is only applicable in case of PA mode.
429 if (rte_eal_iova_mode() == RTE_IOVA_PA)
430 dpaax_iova_table_populate();
432 TAILQ_FOREACH(dev, &rte_fslmc_bus.device_list, next) {
433 TAILQ_FOREACH(drv, &rte_fslmc_bus.driver_list, next) {
434 ret = rte_fslmc_match(drv, dev);
441 if (rte_dev_is_probed(&dev->device))
444 if (dev->device.devargs &&
445 dev->device.devargs->policy == RTE_DEV_BLACKLISTED) {
446 DPAA2_BUS_LOG(DEBUG, "%s Blacklisted, skipping",
452 (dev->device.devargs &&
453 dev->device.devargs->policy ==
454 RTE_DEV_WHITELISTED)) {
455 ret = drv->probe(drv, dev);
457 DPAA2_BUS_ERR("Unable to probe");
460 dev->device.driver = &drv->driver;
467 if (rte_eal_iova_mode() == RTE_IOVA_VA)
473 static struct rte_device *
474 rte_fslmc_find_device(const struct rte_device *start, rte_dev_cmp_t cmp,
477 const struct rte_dpaa2_device *dstart;
478 struct rte_dpaa2_device *dev;
480 DPAA2_BUS_DEBUG("Finding a device named %s\n", (const char *)data);
482 /* find_device is always called with an opaque object which should be
483 * passed along to the 'cmp' function iterating over all device obj
488 dstart = RTE_DEV_TO_FSLMC_CONST(start);
489 dev = TAILQ_NEXT(dstart, next);
491 dev = TAILQ_FIRST(&rte_fslmc_bus.device_list);
493 while (dev != NULL) {
494 if (cmp(&dev->device, data) == 0) {
495 DPAA2_BUS_DEBUG("Found device (%s)\n",
499 dev = TAILQ_NEXT(dev, next);
505 /*register a fslmc bus based dpaa2 driver */
507 rte_fslmc_driver_register(struct rte_dpaa2_driver *driver)
511 TAILQ_INSERT_TAIL(&rte_fslmc_bus.driver_list, driver, next);
512 /* Update Bus references */
513 driver->fslmc_bus = &rte_fslmc_bus;
516 /*un-register a fslmc bus based dpaa2 driver */
518 rte_fslmc_driver_unregister(struct rte_dpaa2_driver *driver)
520 struct rte_fslmc_bus *fslmc_bus;
522 fslmc_bus = driver->fslmc_bus;
524 /* Cleanup the PA->VA Translation table; From whereever this function
527 if (rte_eal_iova_mode() == RTE_IOVA_PA)
528 dpaax_iova_table_depopulate();
530 TAILQ_REMOVE(&fslmc_bus->driver_list, driver, next);
531 /* Update Bus references */
532 driver->fslmc_bus = NULL;
536 * All device has iova as va
539 fslmc_all_device_support_iova(void)
542 struct rte_dpaa2_device *dev;
543 struct rte_dpaa2_driver *drv;
545 TAILQ_FOREACH(dev, &rte_fslmc_bus.device_list, next) {
546 TAILQ_FOREACH(drv, &rte_fslmc_bus.driver_list, next) {
547 ret = rte_fslmc_match(drv, dev);
550 /* if the driver is not supporting IOVA */
551 if (!(drv->drv_flags & RTE_DPAA2_DRV_IOVA_AS_VA))
559 * Get iommu class of DPAA2 devices on the bus.
561 static enum rte_iova_mode
562 rte_dpaa2_get_iommu_class(void)
564 bool is_vfio_noiommu_enabled = 1;
567 if (TAILQ_EMPTY(&rte_fslmc_bus.device_list))
570 #ifdef RTE_LIBRTE_DPAA2_USE_PHYS_IOVA
574 /* check if all devices on the bus support Virtual addressing or not */
575 has_iova_va = fslmc_all_device_support_iova();
578 is_vfio_noiommu_enabled = rte_vfio_noiommu_is_enabled() == true ?
582 if (has_iova_va && !is_vfio_noiommu_enabled)
589 fslmc_bus_plug(struct rte_device *dev __rte_unused)
591 /* No operation is performed while plugging the device */
596 fslmc_bus_unplug(struct rte_device *dev __rte_unused)
598 /* No operation is performed while unplugging the device */
603 fslmc_bus_dev_iterate(const void *start, const char *str,
604 const struct rte_dev_iterator *it __rte_unused)
606 const struct rte_dpaa2_device *dstart;
607 struct rte_dpaa2_device *dev;
608 char *dup, *dev_name = NULL;
610 /* Expectation is that device would be name=device_name */
611 if (strncmp(str, "name=", 5) != 0) {
612 DPAA2_BUS_DEBUG("Invalid device string (%s)\n", str);
616 /* Now that name=device_name format is available, split */
618 dev_name = dup + strlen("name=");
621 dstart = RTE_DEV_TO_FSLMC_CONST(start);
622 dev = TAILQ_NEXT(dstart, next);
624 dev = TAILQ_FIRST(&rte_fslmc_bus.device_list);
627 while (dev != NULL) {
628 if (strcmp(dev->device.name, dev_name) == 0) {
632 dev = TAILQ_NEXT(dev, next);
639 struct rte_fslmc_bus rte_fslmc_bus = {
641 .scan = rte_fslmc_scan,
642 .probe = rte_fslmc_probe,
643 .parse = rte_fslmc_parse,
644 .find_device = rte_fslmc_find_device,
645 .get_iommu_class = rte_dpaa2_get_iommu_class,
646 .plug = fslmc_bus_plug,
647 .unplug = fslmc_bus_unplug,
648 .dev_iterate = fslmc_bus_dev_iterate,
650 .device_list = TAILQ_HEAD_INITIALIZER(rte_fslmc_bus.device_list),
651 .driver_list = TAILQ_HEAD_INITIALIZER(rte_fslmc_bus.driver_list),
655 RTE_REGISTER_BUS(FSLMC_BUS_NAME, rte_fslmc_bus.bus);
657 RTE_INIT(fslmc_init_log)
660 dpaa2_logtype_bus = rte_log_register("bus.fslmc");
661 if (dpaa2_logtype_bus >= 0)
662 rte_log_set_level(dpaa2_logtype_bus, RTE_LOG_NOTICE);