1 /* SPDX-License-Identifier: BSD-3-Clause
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 int dpaa2_logtype_bus;
25 #define VFIO_IOMMU_GROUP_PATH "/sys/kernel/iommu_groups"
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);
97 dump_device_list(void)
99 struct rte_dpaa2_device *dev;
100 uint32_t global_log_level;
103 /* Only if the log level has been set to Debugging, print list */
104 global_log_level = rte_log_get_global_level();
105 local_log_level = rte_log_get_level(dpaa2_logtype_bus);
106 if (global_log_level == RTE_LOG_DEBUG ||
107 local_log_level == RTE_LOG_DEBUG) {
108 DPAA2_BUS_DEBUG("List of devices scanned on bus:");
109 TAILQ_FOREACH(dev, &rte_fslmc_bus.device_list, next) {
110 DPAA2_BUS_DEBUG("%s", dev->device.name);
116 scan_one_fslmc_device(char *dev_name)
118 char *dup_dev_name, *t_ptr;
119 struct rte_dpaa2_device *dev;
124 /* Ignore the Container name itself */
125 if (!strncmp("dprc", dev_name, 4))
128 /* Creating a temporary copy to perform cut-parse over string */
129 dup_dev_name = strdup(dev_name);
131 DPAA2_BUS_ERR("Unable to allocate device name memory");
135 /* For all other devices, we allocate rte_dpaa2_device.
136 * For those devices where there is no driver, probe would release
137 * the memory associated with the rte_dpaa2_device after necessary
140 dev = calloc(1, sizeof(struct rte_dpaa2_device));
142 DPAA2_BUS_ERR("Unable to allocate device object");
147 /* Parse the device name and ID */
148 t_ptr = strtok(dup_dev_name, ".");
150 DPAA2_BUS_ERR("Incorrect device name observed");
153 if (!strncmp("dpni", t_ptr, 4))
154 dev->dev_type = DPAA2_ETH;
155 else if (!strncmp("dpseci", t_ptr, 6))
156 dev->dev_type = DPAA2_CRYPTO;
157 else if (!strncmp("dpcon", t_ptr, 5))
158 dev->dev_type = DPAA2_CON;
159 else if (!strncmp("dpbp", t_ptr, 4))
160 dev->dev_type = DPAA2_BPOOL;
161 else if (!strncmp("dpio", t_ptr, 4))
162 dev->dev_type = DPAA2_IO;
163 else if (!strncmp("dpci", t_ptr, 4))
164 dev->dev_type = DPAA2_CI;
165 else if (!strncmp("dpmcp", t_ptr, 5))
166 dev->dev_type = DPAA2_MPORTAL;
167 else if (!strncmp("dpdmai", t_ptr, 6))
168 dev->dev_type = DPAA2_QDMA;
170 dev->dev_type = DPAA2_UNKNOWN;
172 /* Update the device found into the device_count table */
173 rte_fslmc_bus.device_count[dev->dev_type]++;
175 t_ptr = strtok(NULL, ".");
177 DPAA2_BUS_ERR("Incorrect device string observed (%s)", t_ptr);
181 sscanf(t_ptr, "%hu", &dev->object_id);
182 dev->device.name = strdup(dev_name);
183 if (!dev->device.name) {
184 DPAA2_BUS_ERR("Unable to clone device name. Out of memory");
188 /* Add device in the fslmc device list */
189 insert_in_device_list(dev);
191 /* Don't need the duplicated device filesystem entry anymore */
208 int device_count = 0;
209 char fslmc_dirpath[PATH_MAX];
211 struct dirent *entry;
212 static int process_once;
216 DPAA2_BUS_DEBUG("Fslmc bus already scanned. Not rescanning");
221 ret = fslmc_get_container_group(&groupid);
225 /* Scan devices on the group */
226 sprintf(fslmc_dirpath, "%s/%d/devices", VFIO_IOMMU_GROUP_PATH,
228 dir = opendir(fslmc_dirpath);
230 DPAA2_BUS_ERR("Unable to open VFIO group directory");
234 while ((entry = readdir(dir)) != NULL) {
235 if (entry->d_name[0] == '.' || entry->d_type != DT_LNK)
238 ret = scan_one_fslmc_device(entry->d_name);
240 /* Error in parsing directory - exit gracefully */
241 goto scan_fail_cleanup;
248 DPAA2_BUS_INFO("FSLMC Bus scan completed");
249 /* If debugging is enabled, device list is dumped to log output */
257 /* Remove all devices in the list */
258 cleanup_fslmc_device_list();
260 DPAA2_BUS_DEBUG("FSLMC Bus Not Available. Skipping");
261 /* Irrespective of failure, scan only return success */
266 rte_fslmc_match(struct rte_dpaa2_driver *dpaa2_drv,
267 struct rte_dpaa2_device *dpaa2_dev)
269 if (dpaa2_drv->drv_type == dpaa2_dev->dev_type)
276 rte_fslmc_probe(void)
279 struct rte_dpaa2_device *dev;
280 struct rte_dpaa2_driver *drv;
282 if (TAILQ_EMPTY(&rte_fslmc_bus.device_list))
285 ret = fslmc_vfio_setup_group();
287 DPAA2_BUS_ERR("Unable to setup VFIO %d", ret);
291 /* Map existing segments as well as, in case of hotpluggable memory,
292 * install callback handler.
294 ret = rte_fslmc_vfio_dmamap();
296 DPAA2_BUS_ERR("Unable to DMA map existing VAs: (%d)", ret);
297 /* Not continuing ahead */
298 DPAA2_BUS_ERR("FSLMC VFIO Mapping failed");
302 ret = fslmc_vfio_process_group();
304 DPAA2_BUS_ERR("Unable to setup devices %d", ret);
308 TAILQ_FOREACH(dev, &rte_fslmc_bus.device_list, next) {
309 TAILQ_FOREACH(drv, &rte_fslmc_bus.driver_list, next) {
310 ret = rte_fslmc_match(drv, dev);
317 ret = drv->probe(drv, dev);
319 DPAA2_BUS_ERR("Unable to probe");
324 if (rte_eal_iova_mode() == RTE_IOVA_VA)
330 static struct rte_device *
331 rte_fslmc_find_device(const struct rte_device *start, rte_dev_cmp_t cmp,
334 struct rte_dpaa2_device *dev;
336 TAILQ_FOREACH(dev, &rte_fslmc_bus.device_list, next) {
338 if (&dev->device == start)
339 start = NULL; /* starting point found */
343 if (cmp(&dev->device, data) == 0)
350 /*register a fslmc bus based dpaa2 driver */
352 rte_fslmc_driver_register(struct rte_dpaa2_driver *driver)
356 TAILQ_INSERT_TAIL(&rte_fslmc_bus.driver_list, driver, next);
357 /* Update Bus references */
358 driver->fslmc_bus = &rte_fslmc_bus;
361 /*un-register a fslmc bus based dpaa2 driver */
363 rte_fslmc_driver_unregister(struct rte_dpaa2_driver *driver)
365 struct rte_fslmc_bus *fslmc_bus;
367 fslmc_bus = driver->fslmc_bus;
369 TAILQ_REMOVE(&fslmc_bus->driver_list, driver, next);
370 /* Update Bus references */
371 driver->fslmc_bus = NULL;
375 * All device has iova as va
378 fslmc_all_device_support_iova(void)
381 struct rte_dpaa2_device *dev;
382 struct rte_dpaa2_driver *drv;
384 TAILQ_FOREACH(dev, &rte_fslmc_bus.device_list, next) {
385 TAILQ_FOREACH(drv, &rte_fslmc_bus.driver_list, next) {
386 ret = rte_fslmc_match(drv, dev);
389 /* if the driver is not supporting IOVA */
390 if (!(drv->drv_flags & RTE_DPAA2_DRV_IOVA_AS_VA))
398 * Get iommu class of DPAA2 devices on the bus.
400 static enum rte_iova_mode
401 rte_dpaa2_get_iommu_class(void)
403 bool is_vfio_noiommu_enabled = 1;
406 if (TAILQ_EMPTY(&rte_fslmc_bus.device_list))
409 /* check if all devices on the bus support Virtual addressing or not */
410 has_iova_va = fslmc_all_device_support_iova();
413 is_vfio_noiommu_enabled = rte_vfio_noiommu_is_enabled() == true ?
417 if (has_iova_va && !is_vfio_noiommu_enabled)
423 struct rte_fslmc_bus rte_fslmc_bus = {
425 .scan = rte_fslmc_scan,
426 .probe = rte_fslmc_probe,
427 .find_device = rte_fslmc_find_device,
428 .get_iommu_class = rte_dpaa2_get_iommu_class,
430 .device_list = TAILQ_HEAD_INITIALIZER(rte_fslmc_bus.device_list),
431 .driver_list = TAILQ_HEAD_INITIALIZER(rte_fslmc_bus.driver_list),
435 RTE_REGISTER_BUS(fslmc, rte_fslmc_bus.bus);
437 RTE_INIT(fslmc_init_log);
442 dpaa2_logtype_bus = rte_log_register("bus.fslmc");
443 if (dpaa2_logtype_bus >= 0)
444 rte_log_set_level(dpaa2_logtype_bus, RTE_LOG_NOTICE);