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.h>
19 #include <rte_fslmc.h>
20 #include <fslmc_vfio.h>
22 #define FSLMC_BUS_LOG(level, fmt, args...) \
23 RTE_LOG(level, EAL, fmt "\n", ##args)
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 cleanup_fslmc_device_list(void)
33 struct rte_dpaa2_device *dev;
34 struct rte_dpaa2_device *t_dev;
36 TAILQ_FOREACH_SAFE(dev, &rte_fslmc_bus.device_list, next, t_dev) {
37 TAILQ_REMOVE(&rte_fslmc_bus.device_list, dev, next);
44 compare_dpaa2_devname(struct rte_dpaa2_device *dev1,
45 struct rte_dpaa2_device *dev2)
49 if (dev1->dev_type > dev2->dev_type) {
51 } else if (dev1->dev_type < dev2->dev_type) {
54 /* Check the ID as types match */
55 if (dev1->object_id > dev2->object_id)
57 else if (dev1->object_id < dev2->object_id)
60 comp = 0; /* Duplicate device name */
67 insert_in_device_list(struct rte_dpaa2_device *newdev)
69 int comp, inserted = 0;
70 struct rte_dpaa2_device *dev = NULL;
71 struct rte_dpaa2_device *tdev = NULL;
73 TAILQ_FOREACH_SAFE(dev, &rte_fslmc_bus.device_list, next, tdev) {
74 comp = compare_dpaa2_devname(newdev, dev);
76 TAILQ_INSERT_BEFORE(dev, newdev, next);
83 TAILQ_INSERT_TAIL(&rte_fslmc_bus.device_list, newdev, next);
87 scan_one_fslmc_device(char *dev_name)
89 char *dup_dev_name, *t_ptr;
90 struct rte_dpaa2_device *dev;
95 /* Ignore the Container name itself */
96 if (!strncmp("dprc", dev_name, 4))
99 /* Creating a temporary copy to perform cut-parse over string */
100 dup_dev_name = strdup(dev_name);
102 FSLMC_BUS_LOG(ERR, "Out of memory.");
106 /* For all other devices, we allocate rte_dpaa2_device.
107 * For those devices where there is no driver, probe would release
108 * the memory associated with the rte_dpaa2_device after necessary
111 dev = calloc(1, sizeof(struct rte_dpaa2_device));
113 FSLMC_BUS_LOG(ERR, "Out of memory.");
118 /* Parse the device name and ID */
119 t_ptr = strtok(dup_dev_name, ".");
121 FSLMC_BUS_LOG(ERR, "Incorrect device string observed.");
124 if (!strncmp("dpni", t_ptr, 4))
125 dev->dev_type = DPAA2_ETH;
126 else if (!strncmp("dpseci", t_ptr, 6))
127 dev->dev_type = DPAA2_CRYPTO;
128 else if (!strncmp("dpcon", t_ptr, 5))
129 dev->dev_type = DPAA2_CON;
130 else if (!strncmp("dpbp", t_ptr, 4))
131 dev->dev_type = DPAA2_BPOOL;
132 else if (!strncmp("dpio", t_ptr, 4))
133 dev->dev_type = DPAA2_IO;
134 else if (!strncmp("dpci", t_ptr, 5))
135 dev->dev_type = DPAA2_CI;
136 else if (!strncmp("dpmcp", t_ptr, 5))
137 dev->dev_type = DPAA2_MPORTAL;
139 dev->dev_type = DPAA2_UNKNOWN;
141 t_ptr = strtok(NULL, ".");
143 FSLMC_BUS_LOG(ERR, "Incorrect device string observed (%s).",
148 sscanf(t_ptr, "%hu", &dev->object_id);
149 dev->device.name = strdup(dev_name);
150 if (!dev->device.name) {
151 FSLMC_BUS_LOG(ERR, "Out of memory.");
155 /* Add device in the fslmc device list */
156 insert_in_device_list(dev);
158 /* Don't need the duplicated device filesystem entry anymore */
175 int device_count = 0;
176 char fslmc_dirpath[PATH_MAX];
178 struct dirent *entry;
179 static int process_once;
184 "Fslmc bus already scanned. Not rescanning");
189 ret = fslmc_get_container_group(&groupid);
193 /* Scan devices on the group */
194 sprintf(fslmc_dirpath, "%s/%d/devices", VFIO_IOMMU_GROUP_PATH,
196 dir = opendir(fslmc_dirpath);
198 FSLMC_BUS_LOG(ERR, "Unable to open VFIO group dir.");
202 while ((entry = readdir(dir)) != NULL) {
203 if (entry->d_name[0] == '.' || entry->d_type != DT_LNK)
206 ret = scan_one_fslmc_device(entry->d_name);
208 /* Error in parsing directory - exit gracefully */
209 goto scan_fail_cleanup;
214 FSLMC_BUS_LOG(INFO, "fslmc: Bus scan completed");
222 /* Remove all devices in the list */
223 cleanup_fslmc_device_list();
225 FSLMC_BUS_LOG(DEBUG, "FSLMC Bus Not Available. Skipping.");
226 /* Irrespective of failure, scan only return success */
231 rte_fslmc_match(struct rte_dpaa2_driver *dpaa2_drv,
232 struct rte_dpaa2_device *dpaa2_dev)
234 if (dpaa2_drv->drv_type == dpaa2_dev->dev_type)
241 rte_fslmc_probe(void)
244 struct rte_dpaa2_device *dev;
245 struct rte_dpaa2_driver *drv;
247 if (TAILQ_EMPTY(&rte_fslmc_bus.device_list))
250 ret = fslmc_vfio_setup_group();
252 FSLMC_BUS_LOG(ERR, "Unable to setup VFIO %d", ret);
256 ret = fslmc_vfio_process_group();
258 FSLMC_BUS_LOG(ERR, "Unable to setup devices %d", ret);
262 TAILQ_FOREACH(dev, &rte_fslmc_bus.device_list, next) {
263 TAILQ_FOREACH(drv, &rte_fslmc_bus.driver_list, next) {
264 ret = rte_fslmc_match(drv, dev);
271 ret = drv->probe(drv, dev);
273 FSLMC_BUS_LOG(ERR, "Unable to probe.\n");
278 if (rte_eal_iova_mode() == RTE_IOVA_VA)
284 static struct rte_device *
285 rte_fslmc_find_device(const struct rte_device *start, rte_dev_cmp_t cmp,
288 struct rte_dpaa2_device *dev;
290 TAILQ_FOREACH(dev, &rte_fslmc_bus.device_list, next) {
291 if (start && &dev->device == start) {
292 start = NULL; /* starting point found */
296 if (cmp(&dev->device, data) == 0)
303 /*register a fslmc bus based dpaa2 driver */
305 rte_fslmc_driver_register(struct rte_dpaa2_driver *driver)
309 TAILQ_INSERT_TAIL(&rte_fslmc_bus.driver_list, driver, next);
310 /* Update Bus references */
311 driver->fslmc_bus = &rte_fslmc_bus;
314 /*un-register a fslmc bus based dpaa2 driver */
316 rte_fslmc_driver_unregister(struct rte_dpaa2_driver *driver)
318 struct rte_fslmc_bus *fslmc_bus;
320 fslmc_bus = driver->fslmc_bus;
322 TAILQ_REMOVE(&fslmc_bus->driver_list, driver, next);
323 /* Update Bus references */
324 driver->fslmc_bus = NULL;
328 * All device has iova as va
331 fslmc_all_device_support_iova(void)
334 struct rte_dpaa2_device *dev;
335 struct rte_dpaa2_driver *drv;
337 TAILQ_FOREACH(dev, &rte_fslmc_bus.device_list, next) {
338 TAILQ_FOREACH(drv, &rte_fslmc_bus.driver_list, next) {
339 ret = rte_fslmc_match(drv, dev);
342 /* if the driver is not supporting IOVA */
343 if (!(drv->drv_flags & RTE_DPAA2_DRV_IOVA_AS_VA))
351 * Get iommu class of DPAA2 devices on the bus.
353 static enum rte_iova_mode
354 rte_dpaa2_get_iommu_class(void)
356 bool is_vfio_noiommu_enabled = 1;
359 if (TAILQ_EMPTY(&rte_fslmc_bus.device_list))
362 /* check if all devices on the bus support Virtual addressing or not */
363 has_iova_va = fslmc_all_device_support_iova();
366 is_vfio_noiommu_enabled = rte_vfio_noiommu_is_enabled() == true ?
370 if (has_iova_va && !is_vfio_noiommu_enabled)
376 struct rte_fslmc_bus rte_fslmc_bus = {
378 .scan = rte_fslmc_scan,
379 .probe = rte_fslmc_probe,
380 .find_device = rte_fslmc_find_device,
381 .get_iommu_class = rte_dpaa2_get_iommu_class,
383 .device_list = TAILQ_HEAD_INITIALIZER(rte_fslmc_bus.device_list),
384 .driver_list = TAILQ_HEAD_INITIALIZER(rte_fslmc_bus.driver_list),
387 RTE_REGISTER_BUS(fslmc, rte_fslmc_bus.bus);