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;
168 dev->dev_type = DPAA2_UNKNOWN;
170 /* Update the device found into the device_count table */
171 rte_fslmc_bus.device_count[dev->dev_type]++;
173 t_ptr = strtok(NULL, ".");
175 DPAA2_BUS_ERR("Incorrect device string observed (%s)", t_ptr);
179 sscanf(t_ptr, "%hu", &dev->object_id);
180 dev->device.name = strdup(dev_name);
181 if (!dev->device.name) {
182 DPAA2_BUS_ERR("Unable to clone device name. Out of memory");
186 /* Add device in the fslmc device list */
187 insert_in_device_list(dev);
189 /* Don't need the duplicated device filesystem entry anymore */
206 int device_count = 0;
207 char fslmc_dirpath[PATH_MAX];
209 struct dirent *entry;
210 static int process_once;
214 DPAA2_BUS_DEBUG("Fslmc bus already scanned. Not rescanning");
219 ret = fslmc_get_container_group(&groupid);
223 /* Scan devices on the group */
224 sprintf(fslmc_dirpath, "%s/%d/devices", VFIO_IOMMU_GROUP_PATH,
226 dir = opendir(fslmc_dirpath);
228 DPAA2_BUS_ERR("Unable to open VFIO group directory");
232 while ((entry = readdir(dir)) != NULL) {
233 if (entry->d_name[0] == '.' || entry->d_type != DT_LNK)
236 ret = scan_one_fslmc_device(entry->d_name);
238 /* Error in parsing directory - exit gracefully */
239 goto scan_fail_cleanup;
246 DPAA2_BUS_INFO("FSLMC Bus scan completed");
247 /* If debugging is enabled, device list is dumped to log output */
255 /* Remove all devices in the list */
256 cleanup_fslmc_device_list();
258 DPAA2_BUS_INFO("FSLMC Bus Not Available. Skipping");
259 /* Irrespective of failure, scan only return success */
264 rte_fslmc_match(struct rte_dpaa2_driver *dpaa2_drv,
265 struct rte_dpaa2_device *dpaa2_dev)
267 if (dpaa2_drv->drv_type == dpaa2_dev->dev_type)
274 rte_fslmc_probe(void)
277 struct rte_dpaa2_device *dev;
278 struct rte_dpaa2_driver *drv;
280 if (TAILQ_EMPTY(&rte_fslmc_bus.device_list))
283 ret = fslmc_vfio_setup_group();
285 DPAA2_BUS_ERR("Unable to setup VFIO %d", ret);
289 ret = fslmc_vfio_process_group();
291 DPAA2_BUS_ERR("Unable to setup devices %d", ret);
295 TAILQ_FOREACH(dev, &rte_fslmc_bus.device_list, next) {
296 TAILQ_FOREACH(drv, &rte_fslmc_bus.driver_list, next) {
297 ret = rte_fslmc_match(drv, dev);
304 ret = drv->probe(drv, dev);
306 DPAA2_BUS_ERR("Unable to probe");
311 if (rte_eal_iova_mode() == RTE_IOVA_VA)
317 static struct rte_device *
318 rte_fslmc_find_device(const struct rte_device *start, rte_dev_cmp_t cmp,
321 struct rte_dpaa2_device *dev;
323 TAILQ_FOREACH(dev, &rte_fslmc_bus.device_list, next) {
325 if (&dev->device == start)
326 start = NULL; /* starting point found */
330 if (cmp(&dev->device, data) == 0)
337 /*register a fslmc bus based dpaa2 driver */
339 rte_fslmc_driver_register(struct rte_dpaa2_driver *driver)
343 TAILQ_INSERT_TAIL(&rte_fslmc_bus.driver_list, driver, next);
344 /* Update Bus references */
345 driver->fslmc_bus = &rte_fslmc_bus;
348 /*un-register a fslmc bus based dpaa2 driver */
350 rte_fslmc_driver_unregister(struct rte_dpaa2_driver *driver)
352 struct rte_fslmc_bus *fslmc_bus;
354 fslmc_bus = driver->fslmc_bus;
356 TAILQ_REMOVE(&fslmc_bus->driver_list, driver, next);
357 /* Update Bus references */
358 driver->fslmc_bus = NULL;
362 * All device has iova as va
365 fslmc_all_device_support_iova(void)
368 struct rte_dpaa2_device *dev;
369 struct rte_dpaa2_driver *drv;
371 TAILQ_FOREACH(dev, &rte_fslmc_bus.device_list, next) {
372 TAILQ_FOREACH(drv, &rte_fslmc_bus.driver_list, next) {
373 ret = rte_fslmc_match(drv, dev);
376 /* if the driver is not supporting IOVA */
377 if (!(drv->drv_flags & RTE_DPAA2_DRV_IOVA_AS_VA))
385 * Get iommu class of DPAA2 devices on the bus.
387 static enum rte_iova_mode
388 rte_dpaa2_get_iommu_class(void)
390 bool is_vfio_noiommu_enabled = 1;
393 if (TAILQ_EMPTY(&rte_fslmc_bus.device_list))
396 /* check if all devices on the bus support Virtual addressing or not */
397 has_iova_va = fslmc_all_device_support_iova();
400 is_vfio_noiommu_enabled = rte_vfio_noiommu_is_enabled() == true ?
404 if (has_iova_va && !is_vfio_noiommu_enabled)
410 struct rte_fslmc_bus rte_fslmc_bus = {
412 .scan = rte_fslmc_scan,
413 .probe = rte_fslmc_probe,
414 .find_device = rte_fslmc_find_device,
415 .get_iommu_class = rte_dpaa2_get_iommu_class,
417 .device_list = TAILQ_HEAD_INITIALIZER(rte_fslmc_bus.device_list),
418 .driver_list = TAILQ_HEAD_INITIALIZER(rte_fslmc_bus.driver_list),
422 RTE_REGISTER_BUS(fslmc, rte_fslmc_bus.bus);
424 RTE_INIT(fslmc_init_log);
429 dpaa2_logtype_bus = rte_log_register("bus.fslmc");
430 if (dpaa2_logtype_bus >= 0)
431 rte_log_set_level(dpaa2_logtype_bus, RTE_LOG_NOTICE);