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];
38 RTE_DEFINE_PER_LCORE(struct dpaa2_portal_dqrr, dpaa2_held_bufs);
41 cleanup_fslmc_device_list(void)
43 struct rte_dpaa2_device *dev;
44 struct rte_dpaa2_device *t_dev;
46 TAILQ_FOREACH_SAFE(dev, &rte_fslmc_bus.device_list, next, t_dev) {
47 TAILQ_REMOVE(&rte_fslmc_bus.device_list, dev, next);
54 compare_dpaa2_devname(struct rte_dpaa2_device *dev1,
55 struct rte_dpaa2_device *dev2)
59 if (dev1->dev_type > dev2->dev_type) {
61 } else if (dev1->dev_type < dev2->dev_type) {
64 /* Check the ID as types match */
65 if (dev1->object_id > dev2->object_id)
67 else if (dev1->object_id < dev2->object_id)
70 comp = 0; /* Duplicate device name */
77 insert_in_device_list(struct rte_dpaa2_device *newdev)
79 int comp, inserted = 0;
80 struct rte_dpaa2_device *dev = NULL;
81 struct rte_dpaa2_device *tdev = NULL;
83 TAILQ_FOREACH_SAFE(dev, &rte_fslmc_bus.device_list, next, tdev) {
84 comp = compare_dpaa2_devname(newdev, dev);
86 TAILQ_INSERT_BEFORE(dev, newdev, next);
93 TAILQ_INSERT_TAIL(&rte_fslmc_bus.device_list, newdev, next);
96 static struct rte_devargs *
97 fslmc_devargs_lookup(struct rte_dpaa2_device *dev)
99 struct rte_devargs *devargs;
102 RTE_EAL_DEVARGS_FOREACH("fslmc", devargs) {
103 devargs->bus->parse(devargs->name, &dev_name);
104 if (strcmp(dev_name, dev->device.name) == 0) {
105 DPAA2_BUS_INFO("**Devargs matched %s", dev_name);
113 dump_device_list(void)
115 struct rte_dpaa2_device *dev;
117 /* Only if the log level has been set to Debugging, print list */
118 if (rte_log_can_log(dpaa2_logtype_bus, RTE_LOG_DEBUG)) {
119 DPAA2_BUS_LOG(DEBUG, "List of devices scanned on bus:");
120 TAILQ_FOREACH(dev, &rte_fslmc_bus.device_list, next) {
121 DPAA2_BUS_LOG(DEBUG, "\t\t%s", dev->device.name);
127 scan_one_fslmc_device(char *dev_name)
129 char *dup_dev_name, *t_ptr;
130 struct rte_dpaa2_device *dev = NULL;
136 /* Ignore the Container name itself */
137 if (!strncmp("dprc", dev_name, 4))
140 /* Creating a temporary copy to perform cut-parse over string */
141 dup_dev_name = strdup(dev_name);
143 DPAA2_BUS_ERR("Unable to allocate device name memory");
147 /* For all other devices, we allocate rte_dpaa2_device.
148 * For those devices where there is no driver, probe would release
149 * the memory associated with the rte_dpaa2_device after necessary
152 dev = calloc(1, sizeof(struct rte_dpaa2_device));
154 DPAA2_BUS_ERR("Unable to allocate device object");
159 dev->device.bus = &rte_fslmc_bus.bus;
161 /* Parse the device name and ID */
162 t_ptr = strtok(dup_dev_name, ".");
164 DPAA2_BUS_ERR("Invalid device found: (%s)", dup_dev_name);
168 if (!strncmp("dpni", t_ptr, 4))
169 dev->dev_type = DPAA2_ETH;
170 else if (!strncmp("dpseci", t_ptr, 6))
171 dev->dev_type = DPAA2_CRYPTO;
172 else if (!strncmp("dpcon", t_ptr, 5))
173 dev->dev_type = DPAA2_CON;
174 else if (!strncmp("dpbp", t_ptr, 4))
175 dev->dev_type = DPAA2_BPOOL;
176 else if (!strncmp("dpio", t_ptr, 4))
177 dev->dev_type = DPAA2_IO;
178 else if (!strncmp("dpci", t_ptr, 4))
179 dev->dev_type = DPAA2_CI;
180 else if (!strncmp("dpmcp", t_ptr, 5))
181 dev->dev_type = DPAA2_MPORTAL;
182 else if (!strncmp("dpdmai", t_ptr, 6))
183 dev->dev_type = DPAA2_QDMA;
184 else if (!strncmp("dpdmux", t_ptr, 6))
185 dev->dev_type = DPAA2_MUX;
186 else if (!strncmp("dprtc", t_ptr, 5))
187 dev->dev_type = DPAA2_DPRTC;
189 dev->dev_type = DPAA2_UNKNOWN;
191 t_ptr = strtok(NULL, ".");
193 DPAA2_BUS_ERR("Skipping invalid device (%s)", dup_dev_name);
198 sscanf(t_ptr, "%hu", &dev->object_id);
199 dev->device.name = strdup(dev_name);
200 if (!dev->device.name) {
201 DPAA2_BUS_ERR("Unable to clone device name. Out of memory");
205 dev->device.devargs = fslmc_devargs_lookup(dev);
207 /* Update the device found into the device_count table */
208 rte_fslmc_bus.device_count[dev->dev_type]++;
210 /* Add device in the fslmc device list */
211 insert_in_device_list(dev);
213 /* Don't need the duplicated device filesystem entry anymore */
227 rte_fslmc_parse(const char *name, void *addr)
232 uint8_t sep_exists = 0;
235 DPAA2_BUS_DEBUG("Parsing dev=(%s)", name);
237 /* There are multiple ways this can be called, with bus:dev, name=dev
238 * or just dev. In all cases, the 'addr' is actually a string.
240 sep = strchr(name, ':');
243 sep = strchr(name, '=');
251 /* Check if starting part if either of 'fslmc:' or 'name=', separator
255 /* If either of "fslmc" or "name" are starting part */
256 if (!strncmp(name, RTE_STR(FSLMC_BUS_NAME),
257 strlen(RTE_STR(FSLMC_BUS_NAME))) ||
258 (!strncmp(name, "name", strlen("name")))) {
261 DPAA2_BUS_DEBUG("Invalid device for matching (%s).",
270 /* Validate device name */
271 if (strncmp("dpni", sep, 4) &&
272 strncmp("dpseci", sep, 6) &&
273 strncmp("dpcon", sep, 5) &&
274 strncmp("dpbp", sep, 4) &&
275 strncmp("dpio", sep, 4) &&
276 strncmp("dpci", sep, 4) &&
277 strncmp("dpmcp", sep, 5) &&
278 strncmp("dpdmai", sep, 6) &&
279 strncmp("dpdmux", sep, 6)) {
280 DPAA2_BUS_DEBUG("Unknown or unsupported device (%s)", sep);
285 t_ptr = strchr(sep, '.');
286 if (!t_ptr || sscanf(t_ptr + 1, "%hu", &dev_id) != 1) {
287 DPAA2_BUS_ERR("Missing device id in device name (%s)", sep);
304 int device_count = 0;
305 char fslmc_dirpath[PATH_MAX];
307 struct dirent *entry;
308 static int process_once;
312 DPAA2_BUS_DEBUG("Fslmc bus already scanned. Not rescanning");
317 ret = fslmc_get_container_group(&groupid);
321 /* Scan devices on the group */
322 sprintf(fslmc_dirpath, "%s/%s", SYSFS_FSL_MC_DEVICES, fslmc_container);
323 dir = opendir(fslmc_dirpath);
325 DPAA2_BUS_ERR("Unable to open VFIO group directory");
329 while ((entry = readdir(dir)) != NULL) {
330 if (entry->d_name[0] == '.' || entry->d_type != DT_DIR)
333 ret = scan_one_fslmc_device(entry->d_name);
335 /* Error in parsing directory - exit gracefully */
336 goto scan_fail_cleanup;
343 DPAA2_BUS_INFO("FSLMC Bus scan completed");
344 /* If debugging is enabled, device list is dumped to log output */
352 /* Remove all devices in the list */
353 cleanup_fslmc_device_list();
355 DPAA2_BUS_DEBUG("FSLMC Bus Not Available. Skipping (%d)", ret);
356 /* Irrespective of failure, scan only return success */
361 rte_fslmc_match(struct rte_dpaa2_driver *dpaa2_drv,
362 struct rte_dpaa2_device *dpaa2_dev)
364 if (dpaa2_drv->drv_type == dpaa2_dev->dev_type)
371 rte_fslmc_probe(void)
376 struct rte_dpaa2_device *dev;
377 struct rte_dpaa2_driver *drv;
379 if (TAILQ_EMPTY(&rte_fslmc_bus.device_list))
382 ret = fslmc_vfio_setup_group();
384 DPAA2_BUS_ERR("Unable to setup VFIO %d", ret);
388 /* Map existing segments as well as, in case of hotpluggable memory,
389 * install callback handler.
391 if (rte_eal_process_type() == RTE_PROC_PRIMARY) {
392 ret = rte_fslmc_vfio_dmamap();
394 DPAA2_BUS_ERR("Unable to DMA map existing VAs: (%d)",
396 /* Not continuing ahead */
397 DPAA2_BUS_ERR("FSLMC VFIO Mapping failed");
402 ret = fslmc_vfio_process_group();
404 DPAA2_BUS_ERR("Unable to setup devices %d", ret);
408 probe_all = rte_fslmc_bus.bus.conf.scan_mode != RTE_BUS_SCAN_WHITELIST;
410 /* In case of PA, the FD addresses returned by qbman APIs are physical
411 * addresses, which need conversion into equivalent VA address for
412 * rte_mbuf. For that, a table (a serial array, in memory) is used to
413 * increase translation efficiency.
414 * This has to be done before probe as some device initialization
415 * (during) probe allocate memory (dpaa2_sec) which needs to be pinned
418 * Error is ignored as relevant logs are handled within dpaax and
419 * handling for unavailable dpaax table too is transparent to caller.
421 * And, the IOVA table is only applicable in case of PA mode.
423 if (rte_eal_iova_mode() == RTE_IOVA_PA)
424 dpaax_iova_table_populate();
426 TAILQ_FOREACH(dev, &rte_fslmc_bus.device_list, next) {
427 TAILQ_FOREACH(drv, &rte_fslmc_bus.driver_list, next) {
428 ret = rte_fslmc_match(drv, dev);
435 if (rte_dev_is_probed(&dev->device))
438 if (dev->device.devargs &&
439 dev->device.devargs->policy == RTE_DEV_BLACKLISTED) {
440 DPAA2_BUS_LOG(DEBUG, "%s Blacklisted, skipping",
446 (dev->device.devargs &&
447 dev->device.devargs->policy ==
448 RTE_DEV_WHITELISTED)) {
449 ret = drv->probe(drv, dev);
451 DPAA2_BUS_ERR("Unable to probe");
454 dev->device.driver = &drv->driver;
461 if (rte_eal_iova_mode() == RTE_IOVA_VA)
467 static struct rte_device *
468 rte_fslmc_find_device(const struct rte_device *start, rte_dev_cmp_t cmp,
471 const struct rte_dpaa2_device *dstart;
472 struct rte_dpaa2_device *dev;
474 DPAA2_BUS_DEBUG("Finding a device named %s\n", (const char *)data);
476 /* find_device is always called with an opaque object which should be
477 * passed along to the 'cmp' function iterating over all device obj
482 dstart = RTE_DEV_TO_FSLMC_CONST(start);
483 dev = TAILQ_NEXT(dstart, next);
485 dev = TAILQ_FIRST(&rte_fslmc_bus.device_list);
487 while (dev != NULL) {
488 if (cmp(&dev->device, data) == 0) {
489 DPAA2_BUS_DEBUG("Found device (%s)\n",
493 dev = TAILQ_NEXT(dev, next);
499 /*register a fslmc bus based dpaa2 driver */
501 rte_fslmc_driver_register(struct rte_dpaa2_driver *driver)
505 TAILQ_INSERT_TAIL(&rte_fslmc_bus.driver_list, driver, next);
506 /* Update Bus references */
507 driver->fslmc_bus = &rte_fslmc_bus;
510 /*un-register a fslmc bus based dpaa2 driver */
512 rte_fslmc_driver_unregister(struct rte_dpaa2_driver *driver)
514 struct rte_fslmc_bus *fslmc_bus;
516 fslmc_bus = driver->fslmc_bus;
518 /* Cleanup the PA->VA Translation table; From whereever this function
521 if (rte_eal_iova_mode() == RTE_IOVA_PA)
522 dpaax_iova_table_depopulate();
524 TAILQ_REMOVE(&fslmc_bus->driver_list, driver, next);
525 /* Update Bus references */
526 driver->fslmc_bus = NULL;
530 * All device has iova as va
533 fslmc_all_device_support_iova(void)
536 struct rte_dpaa2_device *dev;
537 struct rte_dpaa2_driver *drv;
539 TAILQ_FOREACH(dev, &rte_fslmc_bus.device_list, next) {
540 TAILQ_FOREACH(drv, &rte_fslmc_bus.driver_list, next) {
541 ret = rte_fslmc_match(drv, dev);
544 /* if the driver is not supporting IOVA */
545 if (!(drv->drv_flags & RTE_DPAA2_DRV_IOVA_AS_VA))
553 * Get iommu class of DPAA2 devices on the bus.
555 static enum rte_iova_mode
556 rte_dpaa2_get_iommu_class(void)
558 bool is_vfio_noiommu_enabled = 1;
561 if (TAILQ_EMPTY(&rte_fslmc_bus.device_list))
564 #ifdef RTE_LIBRTE_DPAA2_USE_PHYS_IOVA
568 /* check if all devices on the bus support Virtual addressing or not */
569 has_iova_va = fslmc_all_device_support_iova();
572 is_vfio_noiommu_enabled = rte_vfio_noiommu_is_enabled() == true ?
576 if (has_iova_va && !is_vfio_noiommu_enabled)
583 fslmc_bus_plug(struct rte_device *dev __rte_unused)
585 /* No operation is performed while plugging the device */
590 fslmc_bus_unplug(struct rte_device *dev __rte_unused)
592 /* No operation is performed while unplugging the device */
597 fslmc_bus_dev_iterate(const void *start, const char *str,
598 const struct rte_dev_iterator *it __rte_unused)
600 const struct rte_dpaa2_device *dstart;
601 struct rte_dpaa2_device *dev;
602 char *dup, *dev_name = NULL;
605 DPAA2_BUS_DEBUG("No device string");
609 /* Expectation is that device would be name=device_name */
610 if (strncmp(str, "name=", 5) != 0) {
611 DPAA2_BUS_DEBUG("Invalid device string (%s)\n", str);
615 /* Now that name=device_name format is available, split */
617 dev_name = dup + strlen("name=");
620 dstart = RTE_DEV_TO_FSLMC_CONST(start);
621 dev = TAILQ_NEXT(dstart, next);
623 dev = TAILQ_FIRST(&rte_fslmc_bus.device_list);
626 while (dev != NULL) {
627 if (strcmp(dev->device.name, dev_name) == 0) {
631 dev = TAILQ_NEXT(dev, next);
638 struct rte_fslmc_bus rte_fslmc_bus = {
640 .scan = rte_fslmc_scan,
641 .probe = rte_fslmc_probe,
642 .parse = rte_fslmc_parse,
643 .find_device = rte_fslmc_find_device,
644 .get_iommu_class = rte_dpaa2_get_iommu_class,
645 .plug = fslmc_bus_plug,
646 .unplug = fslmc_bus_unplug,
647 .dev_iterate = fslmc_bus_dev_iterate,
649 .device_list = TAILQ_HEAD_INITIALIZER(rte_fslmc_bus.device_list),
650 .driver_list = TAILQ_HEAD_INITIALIZER(rte_fslmc_bus.driver_list),
654 RTE_REGISTER_BUS(FSLMC_BUS_NAME, rte_fslmc_bus.bus);
655 RTE_LOG_REGISTER(dpaa2_logtype_bus, bus.fslmc, NOTICE);