1 /* SPDX-License-Identifier: BSD-3-Clause
3 * Copyright 2016,2018 NXP
13 #include <rte_eal_memconfig.h>
14 #include <rte_malloc.h>
15 #include <rte_devargs.h>
16 #include <rte_memcpy.h>
17 #include <rte_ethdev_driver.h>
19 #include <rte_fslmc.h>
20 #include <fslmc_vfio.h>
21 #include "fslmc_logs.h"
23 #include <dpaax_iova_table.h>
25 int dpaa2_logtype_bus;
27 #define VFIO_IOMMU_GROUP_PATH "/sys/kernel/iommu_groups"
28 #define FSLMC_BUS_NAME fslmc
30 struct rte_fslmc_bus rte_fslmc_bus;
31 uint8_t dpaa2_virt_mode;
34 rte_fslmc_get_device_count(enum rte_dpaa2_dev_type device_type)
36 if (device_type > DPAA2_DEVTYPE_MAX)
38 return rte_fslmc_bus.device_count[device_type];
41 RTE_DEFINE_PER_LCORE(struct dpaa2_portal_dqrr, dpaa2_held_bufs);
44 cleanup_fslmc_device_list(void)
46 struct rte_dpaa2_device *dev;
47 struct rte_dpaa2_device *t_dev;
49 TAILQ_FOREACH_SAFE(dev, &rte_fslmc_bus.device_list, next, t_dev) {
50 TAILQ_REMOVE(&rte_fslmc_bus.device_list, dev, next);
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 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;
119 uint32_t global_log_level;
122 /* Only if the log level has been set to Debugging, print list */
123 global_log_level = rte_log_get_global_level();
124 local_log_level = rte_log_get_level(dpaa2_logtype_bus);
125 if (global_log_level == RTE_LOG_DEBUG ||
126 local_log_level == RTE_LOG_DEBUG) {
127 DPAA2_BUS_LOG(DEBUG, "List of devices scanned on bus:");
128 TAILQ_FOREACH(dev, &rte_fslmc_bus.device_list, next) {
129 DPAA2_BUS_LOG(DEBUG, "\t\t%s", dev->device.name);
135 scan_one_fslmc_device(char *dev_name)
137 char *dup_dev_name, *t_ptr;
138 struct rte_dpaa2_device *dev;
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("Incorrect device name observed");
174 if (!strncmp("dpni", t_ptr, 4))
175 dev->dev_type = DPAA2_ETH;
176 else if (!strncmp("dpseci", t_ptr, 6))
177 dev->dev_type = DPAA2_CRYPTO;
178 else if (!strncmp("dpcon", t_ptr, 5))
179 dev->dev_type = DPAA2_CON;
180 else if (!strncmp("dpbp", t_ptr, 4))
181 dev->dev_type = DPAA2_BPOOL;
182 else if (!strncmp("dpio", t_ptr, 4))
183 dev->dev_type = DPAA2_IO;
184 else if (!strncmp("dpci", t_ptr, 4))
185 dev->dev_type = DPAA2_CI;
186 else if (!strncmp("dpmcp", t_ptr, 5))
187 dev->dev_type = DPAA2_MPORTAL;
188 else if (!strncmp("dpdmai", t_ptr, 6))
189 dev->dev_type = DPAA2_QDMA;
190 else if (!strncmp("dpdmux", t_ptr, 6))
191 dev->dev_type = DPAA2_MUX;
193 dev->dev_type = DPAA2_UNKNOWN;
195 /* Update the device found into the device_count table */
196 rte_fslmc_bus.device_count[dev->dev_type]++;
198 t_ptr = strtok(NULL, ".");
200 DPAA2_BUS_ERR("Incorrect device string observed (%s)", t_ptr);
204 sscanf(t_ptr, "%hu", &dev->object_id);
205 dev->device.name = strdup(dev_name);
206 if (!dev->device.name) {
207 DPAA2_BUS_ERR("Unable to clone device name. Out of memory");
210 dev->device.devargs = fslmc_devargs_lookup(dev);
212 /* Add device in the fslmc device list */
213 insert_in_device_list(dev);
215 /* Don't need the duplicated device filesystem entry anymore */
229 rte_fslmc_parse(const char *name, void *addr)
232 char *t_ptr = NULL, *dname = NULL;
234 /* 'name' is expected to contain name of device, for example, dpio.1,
238 dname = strdup(name);
243 if (strncmp("dpni", t_ptr, 4) &&
244 strncmp("dpseci", t_ptr, 6) &&
245 strncmp("dpcon", t_ptr, 5) &&
246 strncmp("dpbp", t_ptr, 4) &&
247 strncmp("dpio", t_ptr, 4) &&
248 strncmp("dpci", t_ptr, 4) &&
249 strncmp("dpmcp", t_ptr, 5) &&
250 strncmp("dpdmai", t_ptr, 6) &&
251 strncmp("dpdmux", t_ptr, 6)) {
252 DPAA2_BUS_ERR("Unknown or unsupported device");
256 t_ptr = strchr(name, '.');
258 DPAA2_BUS_ERR("Incorrect device string observed (%s)", t_ptr);
262 t_ptr = (char *)(t_ptr + 1);
263 if (sscanf(t_ptr, "%hu", &dev_id) <= 0) {
264 DPAA2_BUS_ERR("Incorrect device string observed (%s)", t_ptr);
282 int device_count = 0;
283 char fslmc_dirpath[PATH_MAX];
285 struct dirent *entry;
286 static int process_once;
290 DPAA2_BUS_DEBUG("Fslmc bus already scanned. Not rescanning");
295 ret = fslmc_get_container_group(&groupid);
299 /* Scan devices on the group */
300 sprintf(fslmc_dirpath, "%s/%d/devices", VFIO_IOMMU_GROUP_PATH,
302 dir = opendir(fslmc_dirpath);
304 DPAA2_BUS_ERR("Unable to open VFIO group directory");
308 while ((entry = readdir(dir)) != NULL) {
309 if (entry->d_name[0] == '.' || entry->d_type != DT_LNK)
312 ret = scan_one_fslmc_device(entry->d_name);
314 /* Error in parsing directory - exit gracefully */
315 goto scan_fail_cleanup;
322 DPAA2_BUS_INFO("FSLMC Bus scan completed");
323 /* If debugging is enabled, device list is dumped to log output */
331 /* Remove all devices in the list */
332 cleanup_fslmc_device_list();
334 DPAA2_BUS_DEBUG("FSLMC Bus Not Available. Skipping");
335 /* Irrespective of failure, scan only return success */
340 rte_fslmc_match(struct rte_dpaa2_driver *dpaa2_drv,
341 struct rte_dpaa2_device *dpaa2_dev)
343 if (dpaa2_drv->drv_type == dpaa2_dev->dev_type)
350 rte_fslmc_probe(void)
355 struct rte_dpaa2_device *dev;
356 struct rte_dpaa2_driver *drv;
358 if (TAILQ_EMPTY(&rte_fslmc_bus.device_list))
361 ret = fslmc_vfio_setup_group();
363 DPAA2_BUS_ERR("Unable to setup VFIO %d", ret);
367 /* Map existing segments as well as, in case of hotpluggable memory,
368 * install callback handler.
370 ret = rte_fslmc_vfio_dmamap();
372 DPAA2_BUS_ERR("Unable to DMA map existing VAs: (%d)", ret);
373 /* Not continuing ahead */
374 DPAA2_BUS_ERR("FSLMC VFIO Mapping failed");
378 ret = fslmc_vfio_process_group();
380 DPAA2_BUS_ERR("Unable to setup devices %d", ret);
384 probe_all = rte_fslmc_bus.bus.conf.scan_mode != RTE_BUS_SCAN_WHITELIST;
386 /* In case of PA, the FD addresses returned by qbman APIs are physical
387 * addresses, which need conversion into equivalent VA address for
388 * rte_mbuf. For that, a table (a serial array, in memory) is used to
389 * increase translation efficiency.
390 * This has to be done before probe as some device initialization
391 * (during) probe allocate memory (dpaa2_sec) which needs to be pinned
394 * Error is ignored as relevant logs are handled within dpaax and
395 * handling for unavailable dpaax table too is transparent to caller.
397 dpaax_iova_table_populate();
399 TAILQ_FOREACH(dev, &rte_fslmc_bus.device_list, next) {
400 TAILQ_FOREACH(drv, &rte_fslmc_bus.driver_list, next) {
401 ret = rte_fslmc_match(drv, dev);
408 if (rte_dev_is_probed(&dev->device))
411 if (dev->device.devargs &&
412 dev->device.devargs->policy == RTE_DEV_BLACKLISTED) {
413 DPAA2_BUS_LOG(DEBUG, "%s Blacklisted, skipping",
419 (dev->device.devargs &&
420 dev->device.devargs->policy ==
421 RTE_DEV_WHITELISTED)) {
422 ret = drv->probe(drv, dev);
424 DPAA2_BUS_ERR("Unable to probe");
427 dev->device.driver = &drv->driver;
434 if (rte_eal_iova_mode() == RTE_IOVA_VA)
440 static struct rte_device *
441 rte_fslmc_find_device(const struct rte_device *start, rte_dev_cmp_t cmp,
444 const struct rte_dpaa2_device *dstart;
445 struct rte_dpaa2_device *dev;
448 dstart = RTE_DEV_TO_FSLMC_CONST(start);
449 dev = TAILQ_NEXT(dstart, next);
451 dev = TAILQ_FIRST(&rte_fslmc_bus.device_list);
453 while (dev != NULL) {
454 if (cmp(&dev->device, data) == 0)
456 dev = TAILQ_NEXT(dev, next);
462 /*register a fslmc bus based dpaa2 driver */
464 rte_fslmc_driver_register(struct rte_dpaa2_driver *driver)
468 TAILQ_INSERT_TAIL(&rte_fslmc_bus.driver_list, driver, next);
469 /* Update Bus references */
470 driver->fslmc_bus = &rte_fslmc_bus;
473 /*un-register a fslmc bus based dpaa2 driver */
475 rte_fslmc_driver_unregister(struct rte_dpaa2_driver *driver)
477 struct rte_fslmc_bus *fslmc_bus;
479 fslmc_bus = driver->fslmc_bus;
481 /* Cleanup the PA->VA Translation table; From whereever this function
484 dpaax_iova_table_depopulate();
486 TAILQ_REMOVE(&fslmc_bus->driver_list, driver, next);
487 /* Update Bus references */
488 driver->fslmc_bus = NULL;
492 * All device has iova as va
495 fslmc_all_device_support_iova(void)
498 struct rte_dpaa2_device *dev;
499 struct rte_dpaa2_driver *drv;
501 TAILQ_FOREACH(dev, &rte_fslmc_bus.device_list, next) {
502 TAILQ_FOREACH(drv, &rte_fslmc_bus.driver_list, next) {
503 ret = rte_fslmc_match(drv, dev);
506 /* if the driver is not supporting IOVA */
507 if (!(drv->drv_flags & RTE_DPAA2_DRV_IOVA_AS_VA))
515 * Get iommu class of DPAA2 devices on the bus.
517 static enum rte_iova_mode
518 rte_dpaa2_get_iommu_class(void)
520 bool is_vfio_noiommu_enabled = 1;
523 if (TAILQ_EMPTY(&rte_fslmc_bus.device_list))
526 #ifdef RTE_LIBRTE_DPAA2_USE_PHYS_IOVA
530 /* check if all devices on the bus support Virtual addressing or not */
531 has_iova_va = fslmc_all_device_support_iova();
534 is_vfio_noiommu_enabled = rte_vfio_noiommu_is_enabled() == true ?
538 if (has_iova_va && !is_vfio_noiommu_enabled)
544 struct rte_fslmc_bus rte_fslmc_bus = {
546 .scan = rte_fslmc_scan,
547 .probe = rte_fslmc_probe,
548 .parse = rte_fslmc_parse,
549 .find_device = rte_fslmc_find_device,
550 .get_iommu_class = rte_dpaa2_get_iommu_class,
552 .device_list = TAILQ_HEAD_INITIALIZER(rte_fslmc_bus.device_list),
553 .driver_list = TAILQ_HEAD_INITIALIZER(rte_fslmc_bus.driver_list),
557 RTE_REGISTER_BUS(FSLMC_BUS_NAME, rte_fslmc_bus.bus);
559 RTE_INIT(fslmc_init_log)
562 dpaa2_logtype_bus = rte_log_register("bus.fslmc");
563 if (dpaa2_logtype_bus >= 0)
564 rte_log_set_level(dpaa2_logtype_bus, RTE_LOG_NOTICE);