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 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];
39 cleanup_fslmc_device_list(void)
41 struct rte_dpaa2_device *dev;
42 struct rte_dpaa2_device *t_dev;
44 TAILQ_FOREACH_SAFE(dev, &rte_fslmc_bus.device_list, next, t_dev) {
45 TAILQ_REMOVE(&rte_fslmc_bus.device_list, dev, next);
52 compare_dpaa2_devname(struct rte_dpaa2_device *dev1,
53 struct rte_dpaa2_device *dev2)
57 if (dev1->dev_type > dev2->dev_type) {
59 } else if (dev1->dev_type < dev2->dev_type) {
62 /* Check the ID as types match */
63 if (dev1->object_id > dev2->object_id)
65 else if (dev1->object_id < dev2->object_id)
68 comp = 0; /* Duplicate device name */
75 insert_in_device_list(struct rte_dpaa2_device *newdev)
77 int comp, inserted = 0;
78 struct rte_dpaa2_device *dev = NULL;
79 struct rte_dpaa2_device *tdev = NULL;
81 TAILQ_FOREACH_SAFE(dev, &rte_fslmc_bus.device_list, next, tdev) {
82 comp = compare_dpaa2_devname(newdev, dev);
84 TAILQ_INSERT_BEFORE(dev, newdev, next);
91 TAILQ_INSERT_TAIL(&rte_fslmc_bus.device_list, newdev, next);
95 scan_one_fslmc_device(char *dev_name)
97 char *dup_dev_name, *t_ptr;
98 struct rte_dpaa2_device *dev;
103 /* Ignore the Container name itself */
104 if (!strncmp("dprc", dev_name, 4))
107 /* Creating a temporary copy to perform cut-parse over string */
108 dup_dev_name = strdup(dev_name);
110 FSLMC_BUS_LOG(ERR, "Out of memory.");
114 /* For all other devices, we allocate rte_dpaa2_device.
115 * For those devices where there is no driver, probe would release
116 * the memory associated with the rte_dpaa2_device after necessary
119 dev = calloc(1, sizeof(struct rte_dpaa2_device));
121 FSLMC_BUS_LOG(ERR, "Out of memory.");
126 /* Parse the device name and ID */
127 t_ptr = strtok(dup_dev_name, ".");
129 FSLMC_BUS_LOG(ERR, "Incorrect device string observed.");
132 if (!strncmp("dpni", t_ptr, 4))
133 dev->dev_type = DPAA2_ETH;
134 else if (!strncmp("dpseci", t_ptr, 6))
135 dev->dev_type = DPAA2_CRYPTO;
136 else if (!strncmp("dpcon", t_ptr, 5))
137 dev->dev_type = DPAA2_CON;
138 else if (!strncmp("dpbp", t_ptr, 4))
139 dev->dev_type = DPAA2_BPOOL;
140 else if (!strncmp("dpio", t_ptr, 4))
141 dev->dev_type = DPAA2_IO;
142 else if (!strncmp("dpci", t_ptr, 5))
143 dev->dev_type = DPAA2_CI;
144 else if (!strncmp("dpmcp", t_ptr, 5))
145 dev->dev_type = DPAA2_MPORTAL;
147 dev->dev_type = DPAA2_UNKNOWN;
149 /* Update the device found into the device_count table */
150 rte_fslmc_bus.device_count[dev->dev_type]++;
152 t_ptr = strtok(NULL, ".");
154 FSLMC_BUS_LOG(ERR, "Incorrect device string observed (%s).",
159 sscanf(t_ptr, "%hu", &dev->object_id);
160 dev->device.name = strdup(dev_name);
161 if (!dev->device.name) {
162 FSLMC_BUS_LOG(ERR, "Out of memory.");
166 /* Add device in the fslmc device list */
167 insert_in_device_list(dev);
169 /* Don't need the duplicated device filesystem entry anymore */
186 int device_count = 0;
187 char fslmc_dirpath[PATH_MAX];
189 struct dirent *entry;
190 static int process_once;
195 "Fslmc bus already scanned. Not rescanning");
200 ret = fslmc_get_container_group(&groupid);
204 /* Scan devices on the group */
205 sprintf(fslmc_dirpath, "%s/%d/devices", VFIO_IOMMU_GROUP_PATH,
207 dir = opendir(fslmc_dirpath);
209 FSLMC_BUS_LOG(ERR, "Unable to open VFIO group dir.");
213 while ((entry = readdir(dir)) != NULL) {
214 if (entry->d_name[0] == '.' || entry->d_type != DT_LNK)
217 ret = scan_one_fslmc_device(entry->d_name);
219 /* Error in parsing directory - exit gracefully */
220 goto scan_fail_cleanup;
225 FSLMC_BUS_LOG(INFO, "fslmc: Bus scan completed");
233 /* Remove all devices in the list */
234 cleanup_fslmc_device_list();
236 FSLMC_BUS_LOG(DEBUG, "FSLMC Bus Not Available. Skipping.");
237 /* Irrespective of failure, scan only return success */
242 rte_fslmc_match(struct rte_dpaa2_driver *dpaa2_drv,
243 struct rte_dpaa2_device *dpaa2_dev)
245 if (dpaa2_drv->drv_type == dpaa2_dev->dev_type)
252 rte_fslmc_probe(void)
255 struct rte_dpaa2_device *dev;
256 struct rte_dpaa2_driver *drv;
258 if (TAILQ_EMPTY(&rte_fslmc_bus.device_list))
261 ret = fslmc_vfio_setup_group();
263 FSLMC_BUS_LOG(ERR, "Unable to setup VFIO %d", ret);
267 ret = fslmc_vfio_process_group();
269 FSLMC_BUS_LOG(ERR, "Unable to setup devices %d", ret);
273 TAILQ_FOREACH(dev, &rte_fslmc_bus.device_list, next) {
274 TAILQ_FOREACH(drv, &rte_fslmc_bus.driver_list, next) {
275 ret = rte_fslmc_match(drv, dev);
282 ret = drv->probe(drv, dev);
284 FSLMC_BUS_LOG(ERR, "Unable to probe.\n");
289 if (rte_eal_iova_mode() == RTE_IOVA_VA)
295 static struct rte_device *
296 rte_fslmc_find_device(const struct rte_device *start, rte_dev_cmp_t cmp,
299 struct rte_dpaa2_device *dev;
301 TAILQ_FOREACH(dev, &rte_fslmc_bus.device_list, next) {
302 if (start && &dev->device == start) {
303 start = NULL; /* starting point found */
307 if (cmp(&dev->device, data) == 0)
314 /*register a fslmc bus based dpaa2 driver */
316 rte_fslmc_driver_register(struct rte_dpaa2_driver *driver)
320 TAILQ_INSERT_TAIL(&rte_fslmc_bus.driver_list, driver, next);
321 /* Update Bus references */
322 driver->fslmc_bus = &rte_fslmc_bus;
325 /*un-register a fslmc bus based dpaa2 driver */
327 rte_fslmc_driver_unregister(struct rte_dpaa2_driver *driver)
329 struct rte_fslmc_bus *fslmc_bus;
331 fslmc_bus = driver->fslmc_bus;
333 TAILQ_REMOVE(&fslmc_bus->driver_list, driver, next);
334 /* Update Bus references */
335 driver->fslmc_bus = NULL;
339 * All device has iova as va
342 fslmc_all_device_support_iova(void)
345 struct rte_dpaa2_device *dev;
346 struct rte_dpaa2_driver *drv;
348 TAILQ_FOREACH(dev, &rte_fslmc_bus.device_list, next) {
349 TAILQ_FOREACH(drv, &rte_fslmc_bus.driver_list, next) {
350 ret = rte_fslmc_match(drv, dev);
353 /* if the driver is not supporting IOVA */
354 if (!(drv->drv_flags & RTE_DPAA2_DRV_IOVA_AS_VA))
362 * Get iommu class of DPAA2 devices on the bus.
364 static enum rte_iova_mode
365 rte_dpaa2_get_iommu_class(void)
367 bool is_vfio_noiommu_enabled = 1;
370 if (TAILQ_EMPTY(&rte_fslmc_bus.device_list))
373 /* check if all devices on the bus support Virtual addressing or not */
374 has_iova_va = fslmc_all_device_support_iova();
377 is_vfio_noiommu_enabled = rte_vfio_noiommu_is_enabled() == true ?
381 if (has_iova_va && !is_vfio_noiommu_enabled)
387 struct rte_fslmc_bus rte_fslmc_bus = {
389 .scan = rte_fslmc_scan,
390 .probe = rte_fslmc_probe,
391 .find_device = rte_fslmc_find_device,
392 .get_iommu_class = rte_dpaa2_get_iommu_class,
394 .device_list = TAILQ_HEAD_INITIALIZER(rte_fslmc_bus.device_list),
395 .driver_list = TAILQ_HEAD_INITIALIZER(rte_fslmc_bus.driver_list),
399 RTE_REGISTER_BUS(fslmc, rte_fslmc_bus.bus);