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"
26 #define FSLMC_BUS_NAME fslmc
28 struct rte_fslmc_bus rte_fslmc_bus;
29 uint8_t dpaa2_virt_mode;
32 rte_fslmc_get_device_count(enum rte_dpaa2_dev_type device_type)
34 if (device_type > DPAA2_DEVTYPE_MAX)
36 return rte_fslmc_bus.device_count[device_type];
39 RTE_DEFINE_PER_LCORE(struct dpaa2_portal_dqrr, dpaa2_held_bufs);
42 cleanup_fslmc_device_list(void)
44 struct rte_dpaa2_device *dev;
45 struct rte_dpaa2_device *t_dev;
47 TAILQ_FOREACH_SAFE(dev, &rte_fslmc_bus.device_list, next, t_dev) {
48 TAILQ_REMOVE(&rte_fslmc_bus.device_list, dev, next);
55 compare_dpaa2_devname(struct rte_dpaa2_device *dev1,
56 struct rte_dpaa2_device *dev2)
60 if (dev1->dev_type > dev2->dev_type) {
62 } else if (dev1->dev_type < dev2->dev_type) {
65 /* Check the ID as types match */
66 if (dev1->object_id > dev2->object_id)
68 else if (dev1->object_id < dev2->object_id)
71 comp = 0; /* Duplicate device name */
78 insert_in_device_list(struct rte_dpaa2_device *newdev)
80 int comp, inserted = 0;
81 struct rte_dpaa2_device *dev = NULL;
82 struct rte_dpaa2_device *tdev = NULL;
84 TAILQ_FOREACH_SAFE(dev, &rte_fslmc_bus.device_list, next, tdev) {
85 comp = compare_dpaa2_devname(newdev, dev);
87 TAILQ_INSERT_BEFORE(dev, newdev, next);
94 TAILQ_INSERT_TAIL(&rte_fslmc_bus.device_list, newdev, next);
97 static struct rte_devargs *
98 fslmc_devargs_lookup(struct rte_dpaa2_device *dev)
100 struct rte_devargs *devargs;
103 RTE_EAL_DEVARGS_FOREACH("fslmc", devargs) {
104 devargs->bus->parse(devargs->name, &dev_name);
105 if (strcmp(dev_name, dev->device.name) == 0) {
106 DPAA2_BUS_INFO("**Devargs matched %s", dev_name);
114 dump_device_list(void)
116 struct rte_dpaa2_device *dev;
117 uint32_t global_log_level;
120 /* Only if the log level has been set to Debugging, print list */
121 global_log_level = rte_log_get_global_level();
122 local_log_level = rte_log_get_level(dpaa2_logtype_bus);
123 if (global_log_level == RTE_LOG_DEBUG ||
124 local_log_level == RTE_LOG_DEBUG) {
125 DPAA2_BUS_LOG(DEBUG, "List of devices scanned on bus:");
126 TAILQ_FOREACH(dev, &rte_fslmc_bus.device_list, next) {
127 DPAA2_BUS_LOG(DEBUG, "\t\t%s", dev->device.name);
133 scan_one_fslmc_device(char *dev_name)
135 char *dup_dev_name, *t_ptr;
136 struct rte_dpaa2_device *dev;
141 /* Ignore the Container name itself */
142 if (!strncmp("dprc", dev_name, 4))
145 /* Creating a temporary copy to perform cut-parse over string */
146 dup_dev_name = strdup(dev_name);
148 DPAA2_BUS_ERR("Unable to allocate device name memory");
152 /* For all other devices, we allocate rte_dpaa2_device.
153 * For those devices where there is no driver, probe would release
154 * the memory associated with the rte_dpaa2_device after necessary
157 dev = calloc(1, sizeof(struct rte_dpaa2_device));
159 DPAA2_BUS_ERR("Unable to allocate device object");
164 /* Parse the device name and ID */
165 t_ptr = strtok(dup_dev_name, ".");
167 DPAA2_BUS_ERR("Incorrect device name observed");
170 if (!strncmp("dpni", t_ptr, 4))
171 dev->dev_type = DPAA2_ETH;
172 else if (!strncmp("dpseci", t_ptr, 6))
173 dev->dev_type = DPAA2_CRYPTO;
174 else if (!strncmp("dpcon", t_ptr, 5))
175 dev->dev_type = DPAA2_CON;
176 else if (!strncmp("dpbp", t_ptr, 4))
177 dev->dev_type = DPAA2_BPOOL;
178 else if (!strncmp("dpio", t_ptr, 4))
179 dev->dev_type = DPAA2_IO;
180 else if (!strncmp("dpci", t_ptr, 4))
181 dev->dev_type = DPAA2_CI;
182 else if (!strncmp("dpmcp", t_ptr, 5))
183 dev->dev_type = DPAA2_MPORTAL;
184 else if (!strncmp("dpdmai", t_ptr, 6))
185 dev->dev_type = DPAA2_QDMA;
187 dev->dev_type = DPAA2_UNKNOWN;
189 /* Update the device found into the device_count table */
190 rte_fslmc_bus.device_count[dev->dev_type]++;
192 t_ptr = strtok(NULL, ".");
194 DPAA2_BUS_ERR("Incorrect device string observed (%s)", t_ptr);
198 sscanf(t_ptr, "%hu", &dev->object_id);
199 dev->device.name = strdup(dev_name);
200 if (!dev->device.name) {
201 DPAA2_BUS_ERR("Unable to clone device name. Out of memory");
204 dev->device.devargs = fslmc_devargs_lookup(dev);
206 /* Add device in the fslmc device list */
207 insert_in_device_list(dev);
209 /* Don't need the duplicated device filesystem entry anymore */
223 rte_fslmc_parse(const char *name, void *addr)
227 char *sep = strchr(name, ':');
229 if (strncmp(name, RTE_STR(FSLMC_BUS_NAME),
230 strlen(RTE_STR(FSLMC_BUS_NAME)))) {
235 DPAA2_BUS_ERR("Incorrect device name observed");
239 t_ptr = (char *)(sep + 1);
241 if (strncmp("dpni", t_ptr, 4) &&
242 strncmp("dpseci", t_ptr, 6) &&
243 strncmp("dpcon", t_ptr, 5) &&
244 strncmp("dpbp", t_ptr, 4) &&
245 strncmp("dpio", t_ptr, 4) &&
246 strncmp("dpci", t_ptr, 4) &&
247 strncmp("dpmcp", t_ptr, 5) &&
248 strncmp("dpdmai", t_ptr, 6)) {
249 DPAA2_BUS_ERR("Unknown or unsupported device");
253 t_ptr = strchr(name, '.');
255 DPAA2_BUS_ERR("Incorrect device string observed (%s)", t_ptr);
259 t_ptr = (char *)(t_ptr + 1);
260 if (sscanf(t_ptr, "%hu", &dev_id) <= 0) {
261 DPAA2_BUS_ERR("Incorrect device string observed (%s)", t_ptr);
266 strcpy(addr, (char *)(sep + 1));
274 int device_count = 0;
275 char fslmc_dirpath[PATH_MAX];
277 struct dirent *entry;
278 static int process_once;
282 DPAA2_BUS_DEBUG("Fslmc bus already scanned. Not rescanning");
287 ret = fslmc_get_container_group(&groupid);
291 /* Scan devices on the group */
292 sprintf(fslmc_dirpath, "%s/%d/devices", VFIO_IOMMU_GROUP_PATH,
294 dir = opendir(fslmc_dirpath);
296 DPAA2_BUS_ERR("Unable to open VFIO group directory");
300 while ((entry = readdir(dir)) != NULL) {
301 if (entry->d_name[0] == '.' || entry->d_type != DT_LNK)
304 ret = scan_one_fslmc_device(entry->d_name);
306 /* Error in parsing directory - exit gracefully */
307 goto scan_fail_cleanup;
314 DPAA2_BUS_INFO("FSLMC Bus scan completed");
315 /* If debugging is enabled, device list is dumped to log output */
323 /* Remove all devices in the list */
324 cleanup_fslmc_device_list();
326 DPAA2_BUS_DEBUG("FSLMC Bus Not Available. Skipping");
327 /* Irrespective of failure, scan only return success */
332 rte_fslmc_match(struct rte_dpaa2_driver *dpaa2_drv,
333 struct rte_dpaa2_device *dpaa2_dev)
335 if (dpaa2_drv->drv_type == dpaa2_dev->dev_type)
342 rte_fslmc_probe(void)
347 struct rte_dpaa2_device *dev;
348 struct rte_dpaa2_driver *drv;
350 if (TAILQ_EMPTY(&rte_fslmc_bus.device_list))
353 ret = fslmc_vfio_setup_group();
355 DPAA2_BUS_ERR("Unable to setup VFIO %d", ret);
359 /* Map existing segments as well as, in case of hotpluggable memory,
360 * install callback handler.
362 ret = rte_fslmc_vfio_dmamap();
364 DPAA2_BUS_ERR("Unable to DMA map existing VAs: (%d)", ret);
365 /* Not continuing ahead */
366 DPAA2_BUS_ERR("FSLMC VFIO Mapping failed");
370 ret = fslmc_vfio_process_group();
372 DPAA2_BUS_ERR("Unable to setup devices %d", ret);
376 probe_all = rte_fslmc_bus.bus.conf.scan_mode != RTE_BUS_SCAN_WHITELIST;
378 TAILQ_FOREACH(dev, &rte_fslmc_bus.device_list, next) {
379 TAILQ_FOREACH(drv, &rte_fslmc_bus.driver_list, next) {
380 ret = rte_fslmc_match(drv, dev);
387 if (dev->device.devargs &&
388 dev->device.devargs->policy == RTE_DEV_BLACKLISTED) {
389 DPAA2_BUS_LOG(DEBUG, "%s Blacklisted, skipping",
395 (dev->device.devargs &&
396 dev->device.devargs->policy ==
397 RTE_DEV_WHITELISTED)) {
398 ret = drv->probe(drv, dev);
400 DPAA2_BUS_ERR("Unable to probe");
406 if (rte_eal_iova_mode() == RTE_IOVA_VA)
412 static struct rte_device *
413 rte_fslmc_find_device(const struct rte_device *start, rte_dev_cmp_t cmp,
416 const struct rte_dpaa2_device *dstart;
417 struct rte_dpaa2_device *dev;
420 dstart = RTE_DEV_TO_FSLMC_CONST(start);
421 dev = TAILQ_NEXT(dstart, next);
423 dev = TAILQ_FIRST(&rte_fslmc_bus.device_list);
425 while (dev != NULL) {
426 if (cmp(&dev->device, data) == 0)
428 dev = TAILQ_NEXT(dev, next);
434 /*register a fslmc bus based dpaa2 driver */
436 rte_fslmc_driver_register(struct rte_dpaa2_driver *driver)
440 TAILQ_INSERT_TAIL(&rte_fslmc_bus.driver_list, driver, next);
441 /* Update Bus references */
442 driver->fslmc_bus = &rte_fslmc_bus;
445 /*un-register a fslmc bus based dpaa2 driver */
447 rte_fslmc_driver_unregister(struct rte_dpaa2_driver *driver)
449 struct rte_fslmc_bus *fslmc_bus;
451 fslmc_bus = driver->fslmc_bus;
453 TAILQ_REMOVE(&fslmc_bus->driver_list, driver, next);
454 /* Update Bus references */
455 driver->fslmc_bus = NULL;
459 * All device has iova as va
462 fslmc_all_device_support_iova(void)
465 struct rte_dpaa2_device *dev;
466 struct rte_dpaa2_driver *drv;
468 TAILQ_FOREACH(dev, &rte_fslmc_bus.device_list, next) {
469 TAILQ_FOREACH(drv, &rte_fslmc_bus.driver_list, next) {
470 ret = rte_fslmc_match(drv, dev);
473 /* if the driver is not supporting IOVA */
474 if (!(drv->drv_flags & RTE_DPAA2_DRV_IOVA_AS_VA))
482 * Get iommu class of DPAA2 devices on the bus.
484 static enum rte_iova_mode
485 rte_dpaa2_get_iommu_class(void)
487 bool is_vfio_noiommu_enabled = 1;
490 if (TAILQ_EMPTY(&rte_fslmc_bus.device_list))
493 /* check if all devices on the bus support Virtual addressing or not */
494 has_iova_va = fslmc_all_device_support_iova();
497 is_vfio_noiommu_enabled = rte_vfio_noiommu_is_enabled() == true ?
501 if (has_iova_va && !is_vfio_noiommu_enabled)
507 struct rte_fslmc_bus rte_fslmc_bus = {
509 .scan = rte_fslmc_scan,
510 .probe = rte_fslmc_probe,
511 .parse = rte_fslmc_parse,
512 .find_device = rte_fslmc_find_device,
513 .get_iommu_class = rte_dpaa2_get_iommu_class,
515 .device_list = TAILQ_HEAD_INITIALIZER(rte_fslmc_bus.device_list),
516 .driver_list = TAILQ_HEAD_INITIALIZER(rte_fslmc_bus.driver_list),
520 RTE_REGISTER_BUS(FSLMC_BUS_NAME, rte_fslmc_bus.bus);
522 RTE_INIT(fslmc_init_log);
527 dpaa2_logtype_bus = rte_log_register("bus.fslmc");
528 if (dpaa2_logtype_bus >= 0)
529 rte_log_set_level(dpaa2_logtype_bus, RTE_LOG_NOTICE);