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;
30 cleanup_fslmc_device_list(void)
32 struct rte_dpaa2_device *dev;
33 struct rte_dpaa2_device *t_dev;
35 TAILQ_FOREACH_SAFE(dev, &rte_fslmc_bus.device_list, next, t_dev) {
36 TAILQ_REMOVE(&rte_fslmc_bus.device_list, dev, next);
43 compare_dpaa2_devname(struct rte_dpaa2_device *dev1,
44 struct rte_dpaa2_device *dev2)
48 if (dev1->dev_type > dev2->dev_type) {
50 } else if (dev1->dev_type < dev2->dev_type) {
53 /* Check the ID as types match */
54 if (dev1->object_id > dev2->object_id)
56 else if (dev1->object_id < dev2->object_id)
59 comp = 0; /* Duplicate device name */
66 insert_in_device_list(struct rte_dpaa2_device *newdev)
68 int comp, inserted = 0;
69 struct rte_dpaa2_device *dev = NULL;
70 struct rte_dpaa2_device *tdev = NULL;
72 TAILQ_FOREACH_SAFE(dev, &rte_fslmc_bus.device_list, next, tdev) {
73 comp = compare_dpaa2_devname(newdev, dev);
75 TAILQ_INSERT_BEFORE(dev, newdev, next);
82 TAILQ_INSERT_TAIL(&rte_fslmc_bus.device_list, newdev, next);
86 scan_one_fslmc_device(char *dev_name)
88 char *dup_dev_name, *t_ptr;
89 struct rte_dpaa2_device *dev;
94 /* Ignore the Container name itself */
95 if (!strncmp("dprc", dev_name, 4))
98 /* Creating a temporary copy to perform cut-parse over string */
99 dup_dev_name = strdup(dev_name);
101 FSLMC_BUS_LOG(ERR, "Out of memory.");
105 /* For all other devices, we allocate rte_dpaa2_device.
106 * For those devices where there is no driver, probe would release
107 * the memory associated with the rte_dpaa2_device after necessary
110 dev = calloc(1, sizeof(struct rte_dpaa2_device));
112 FSLMC_BUS_LOG(ERR, "Out of memory.");
117 /* Parse the device name and ID */
118 t_ptr = strtok(dup_dev_name, ".");
120 FSLMC_BUS_LOG(ERR, "Incorrect device string observed.");
123 if (!strncmp("dpni", t_ptr, 4))
124 dev->dev_type = DPAA2_ETH;
125 else if (!strncmp("dpseci", t_ptr, 6))
126 dev->dev_type = DPAA2_CRYPTO;
127 else if (!strncmp("dpcon", t_ptr, 5))
128 dev->dev_type = DPAA2_CON;
129 else if (!strncmp("dpbp", t_ptr, 4))
130 dev->dev_type = DPAA2_BPOOL;
131 else if (!strncmp("dpio", t_ptr, 4))
132 dev->dev_type = DPAA2_IO;
133 else if (!strncmp("dpci", t_ptr, 5))
134 dev->dev_type = DPAA2_CI;
135 else if (!strncmp("dpmcp", t_ptr, 5))
136 dev->dev_type = DPAA2_MPORTAL;
138 dev->dev_type = DPAA2_UNKNOWN;
140 t_ptr = strtok(NULL, ".");
142 FSLMC_BUS_LOG(ERR, "Incorrect device string observed (%s).",
147 sscanf(t_ptr, "%hu", &dev->object_id);
148 dev->device.name = strdup(dev_name);
149 if (!dev->device.name) {
150 FSLMC_BUS_LOG(ERR, "Out of memory.");
154 /* Add device in the fslmc device list */
155 insert_in_device_list(dev);
157 /* Don't need the duplicated device filesystem entry anymore */
174 int device_count = 0;
175 char fslmc_dirpath[PATH_MAX];
177 struct dirent *entry;
178 static int process_once;
183 "Fslmc bus already scanned. Not rescanning");
188 ret = fslmc_get_container_group(&groupid);
192 /* Scan devices on the group */
193 sprintf(fslmc_dirpath, "%s/%d/devices", VFIO_IOMMU_GROUP_PATH,
195 dir = opendir(fslmc_dirpath);
197 FSLMC_BUS_LOG(ERR, "Unable to open VFIO group dir.");
201 while ((entry = readdir(dir)) != NULL) {
202 if (entry->d_name[0] == '.' || entry->d_type != DT_LNK)
205 ret = scan_one_fslmc_device(entry->d_name);
207 /* Error in parsing directory - exit gracefully */
208 goto scan_fail_cleanup;
213 FSLMC_BUS_LOG(INFO, "fslmc: Bus scan completed");
221 /* Remove all devices in the list */
222 cleanup_fslmc_device_list();
224 FSLMC_BUS_LOG(DEBUG, "FSLMC Bus Not Available. Skipping.");
225 /* Irrespective of failure, scan only return success */
230 rte_fslmc_match(struct rte_dpaa2_driver *dpaa2_drv,
231 struct rte_dpaa2_device *dpaa2_dev)
233 if (dpaa2_drv->drv_type == dpaa2_dev->dev_type)
240 rte_fslmc_probe(void)
243 struct rte_dpaa2_device *dev;
244 struct rte_dpaa2_driver *drv;
246 if (TAILQ_EMPTY(&rte_fslmc_bus.device_list))
249 ret = fslmc_vfio_setup_group();
251 FSLMC_BUS_LOG(ERR, "Unable to setup VFIO %d", ret);
255 ret = fslmc_vfio_process_group();
257 FSLMC_BUS_LOG(ERR, "Unable to setup devices %d", ret);
261 TAILQ_FOREACH(dev, &rte_fslmc_bus.device_list, next) {
262 TAILQ_FOREACH(drv, &rte_fslmc_bus.driver_list, next) {
263 ret = rte_fslmc_match(drv, dev);
270 ret = drv->probe(drv, dev);
272 FSLMC_BUS_LOG(ERR, "Unable to probe.\n");
280 static struct rte_device *
281 rte_fslmc_find_device(const struct rte_device *start, rte_dev_cmp_t cmp,
284 struct rte_dpaa2_device *dev;
286 TAILQ_FOREACH(dev, &rte_fslmc_bus.device_list, next) {
287 if (start && &dev->device == start) {
288 start = NULL; /* starting point found */
292 if (cmp(&dev->device, data) == 0)
299 /*register a fslmc bus based dpaa2 driver */
301 rte_fslmc_driver_register(struct rte_dpaa2_driver *driver)
305 TAILQ_INSERT_TAIL(&rte_fslmc_bus.driver_list, driver, next);
306 /* Update Bus references */
307 driver->fslmc_bus = &rte_fslmc_bus;
310 /*un-register a fslmc bus based dpaa2 driver */
312 rte_fslmc_driver_unregister(struct rte_dpaa2_driver *driver)
314 struct rte_fslmc_bus *fslmc_bus;
316 fslmc_bus = driver->fslmc_bus;
318 TAILQ_REMOVE(&fslmc_bus->driver_list, driver, next);
319 /* Update Bus references */
320 driver->fslmc_bus = NULL;
324 * Get iommu class of DPAA2 devices on the bus.
326 static enum rte_iova_mode
327 rte_dpaa2_get_iommu_class(void)
332 struct rte_fslmc_bus rte_fslmc_bus = {
334 .scan = rte_fslmc_scan,
335 .probe = rte_fslmc_probe,
336 .find_device = rte_fslmc_find_device,
337 .get_iommu_class = rte_dpaa2_get_iommu_class,
339 .device_list = TAILQ_HEAD_INITIALIZER(rte_fslmc_bus.device_list),
340 .driver_list = TAILQ_HEAD_INITIALIZER(rte_fslmc_bus.driver_list),
343 RTE_REGISTER_BUS(fslmc, rte_fslmc_bus.bus);