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>
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];
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 scan_one_fslmc_device(char *dev_name)
99 char *dup_dev_name, *t_ptr;
100 struct rte_dpaa2_device *dev;
105 /* Ignore the Container name itself */
106 if (!strncmp("dprc", dev_name, 4))
109 /* Creating a temporary copy to perform cut-parse over string */
110 dup_dev_name = strdup(dev_name);
112 FSLMC_BUS_LOG(ERR, "Out of memory.");
116 /* For all other devices, we allocate rte_dpaa2_device.
117 * For those devices where there is no driver, probe would release
118 * the memory associated with the rte_dpaa2_device after necessary
121 dev = calloc(1, sizeof(struct rte_dpaa2_device));
123 FSLMC_BUS_LOG(ERR, "Out of memory.");
128 /* Parse the device name and ID */
129 t_ptr = strtok(dup_dev_name, ".");
131 FSLMC_BUS_LOG(ERR, "Incorrect device string observed.");
134 if (!strncmp("dpni", t_ptr, 4))
135 dev->dev_type = DPAA2_ETH;
136 else if (!strncmp("dpseci", t_ptr, 6))
137 dev->dev_type = DPAA2_CRYPTO;
138 else if (!strncmp("dpcon", t_ptr, 5))
139 dev->dev_type = DPAA2_CON;
140 else if (!strncmp("dpbp", t_ptr, 4))
141 dev->dev_type = DPAA2_BPOOL;
142 else if (!strncmp("dpio", t_ptr, 4))
143 dev->dev_type = DPAA2_IO;
144 else if (!strncmp("dpci", t_ptr, 5))
145 dev->dev_type = DPAA2_CI;
146 else if (!strncmp("dpmcp", t_ptr, 5))
147 dev->dev_type = DPAA2_MPORTAL;
149 dev->dev_type = DPAA2_UNKNOWN;
151 /* Update the device found into the device_count table */
152 rte_fslmc_bus.device_count[dev->dev_type]++;
154 t_ptr = strtok(NULL, ".");
156 FSLMC_BUS_LOG(ERR, "Incorrect device string observed (%s).",
161 sscanf(t_ptr, "%hu", &dev->object_id);
162 dev->device.name = strdup(dev_name);
163 if (!dev->device.name) {
164 FSLMC_BUS_LOG(ERR, "Out of memory.");
168 /* Add device in the fslmc device list */
169 insert_in_device_list(dev);
171 /* Don't need the duplicated device filesystem entry anymore */
188 int device_count = 0;
189 char fslmc_dirpath[PATH_MAX];
191 struct dirent *entry;
192 static int process_once;
197 "Fslmc bus already scanned. Not rescanning");
202 ret = fslmc_get_container_group(&groupid);
206 /* Scan devices on the group */
207 sprintf(fslmc_dirpath, "%s/%d/devices", VFIO_IOMMU_GROUP_PATH,
209 dir = opendir(fslmc_dirpath);
211 FSLMC_BUS_LOG(ERR, "Unable to open VFIO group dir.");
215 while ((entry = readdir(dir)) != NULL) {
216 if (entry->d_name[0] == '.' || entry->d_type != DT_LNK)
219 ret = scan_one_fslmc_device(entry->d_name);
221 /* Error in parsing directory - exit gracefully */
222 goto scan_fail_cleanup;
227 FSLMC_BUS_LOG(INFO, "fslmc: Bus scan completed");
235 /* Remove all devices in the list */
236 cleanup_fslmc_device_list();
238 FSLMC_BUS_LOG(DEBUG, "FSLMC Bus Not Available. Skipping.");
239 /* Irrespective of failure, scan only return success */
244 rte_fslmc_match(struct rte_dpaa2_driver *dpaa2_drv,
245 struct rte_dpaa2_device *dpaa2_dev)
247 if (dpaa2_drv->drv_type == dpaa2_dev->dev_type)
254 rte_fslmc_probe(void)
257 struct rte_dpaa2_device *dev;
258 struct rte_dpaa2_driver *drv;
260 if (TAILQ_EMPTY(&rte_fslmc_bus.device_list))
263 ret = fslmc_vfio_setup_group();
265 FSLMC_BUS_LOG(ERR, "Unable to setup VFIO %d", ret);
269 ret = fslmc_vfio_process_group();
271 FSLMC_BUS_LOG(ERR, "Unable to setup devices %d", ret);
275 TAILQ_FOREACH(dev, &rte_fslmc_bus.device_list, next) {
276 TAILQ_FOREACH(drv, &rte_fslmc_bus.driver_list, next) {
277 ret = rte_fslmc_match(drv, dev);
284 ret = drv->probe(drv, dev);
286 FSLMC_BUS_LOG(ERR, "Unable to probe.\n");
291 if (rte_eal_iova_mode() == RTE_IOVA_VA)
297 static struct rte_device *
298 rte_fslmc_find_device(const struct rte_device *start, rte_dev_cmp_t cmp,
301 struct rte_dpaa2_device *dev;
303 TAILQ_FOREACH(dev, &rte_fslmc_bus.device_list, next) {
304 if (start && &dev->device == start) {
305 start = NULL; /* starting point found */
309 if (cmp(&dev->device, data) == 0)
316 /*register a fslmc bus based dpaa2 driver */
318 rte_fslmc_driver_register(struct rte_dpaa2_driver *driver)
322 TAILQ_INSERT_TAIL(&rte_fslmc_bus.driver_list, driver, next);
323 /* Update Bus references */
324 driver->fslmc_bus = &rte_fslmc_bus;
327 /*un-register a fslmc bus based dpaa2 driver */
329 rte_fslmc_driver_unregister(struct rte_dpaa2_driver *driver)
331 struct rte_fslmc_bus *fslmc_bus;
333 fslmc_bus = driver->fslmc_bus;
335 TAILQ_REMOVE(&fslmc_bus->driver_list, driver, next);
336 /* Update Bus references */
337 driver->fslmc_bus = NULL;
341 * All device has iova as va
344 fslmc_all_device_support_iova(void)
347 struct rte_dpaa2_device *dev;
348 struct rte_dpaa2_driver *drv;
350 TAILQ_FOREACH(dev, &rte_fslmc_bus.device_list, next) {
351 TAILQ_FOREACH(drv, &rte_fslmc_bus.driver_list, next) {
352 ret = rte_fslmc_match(drv, dev);
355 /* if the driver is not supporting IOVA */
356 if (!(drv->drv_flags & RTE_DPAA2_DRV_IOVA_AS_VA))
364 * Get iommu class of DPAA2 devices on the bus.
366 static enum rte_iova_mode
367 rte_dpaa2_get_iommu_class(void)
369 bool is_vfio_noiommu_enabled = 1;
372 if (TAILQ_EMPTY(&rte_fslmc_bus.device_list))
375 /* check if all devices on the bus support Virtual addressing or not */
376 has_iova_va = fslmc_all_device_support_iova();
379 is_vfio_noiommu_enabled = rte_vfio_noiommu_is_enabled() == true ?
383 if (has_iova_va && !is_vfio_noiommu_enabled)
389 struct rte_fslmc_bus rte_fslmc_bus = {
391 .scan = rte_fslmc_scan,
392 .probe = rte_fslmc_probe,
393 .find_device = rte_fslmc_find_device,
394 .get_iommu_class = rte_dpaa2_get_iommu_class,
396 .device_list = TAILQ_HEAD_INITIALIZER(rte_fslmc_bus.device_list),
397 .driver_list = TAILQ_HEAD_INITIALIZER(rte_fslmc_bus.driver_list),
401 RTE_REGISTER_BUS(fslmc, rte_fslmc_bus.bus);