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 dev->device.bus = &rte_fslmc_bus.bus;
166 /* Parse the device name and ID */
167 t_ptr = strtok(dup_dev_name, ".");
169 DPAA2_BUS_ERR("Incorrect device name observed");
172 if (!strncmp("dpni", t_ptr, 4))
173 dev->dev_type = DPAA2_ETH;
174 else if (!strncmp("dpseci", t_ptr, 6))
175 dev->dev_type = DPAA2_CRYPTO;
176 else if (!strncmp("dpcon", t_ptr, 5))
177 dev->dev_type = DPAA2_CON;
178 else if (!strncmp("dpbp", t_ptr, 4))
179 dev->dev_type = DPAA2_BPOOL;
180 else if (!strncmp("dpio", t_ptr, 4))
181 dev->dev_type = DPAA2_IO;
182 else if (!strncmp("dpci", t_ptr, 4))
183 dev->dev_type = DPAA2_CI;
184 else if (!strncmp("dpmcp", t_ptr, 5))
185 dev->dev_type = DPAA2_MPORTAL;
186 else if (!strncmp("dpdmai", t_ptr, 6))
187 dev->dev_type = DPAA2_QDMA;
189 dev->dev_type = DPAA2_UNKNOWN;
191 /* Update the device found into the device_count table */
192 rte_fslmc_bus.device_count[dev->dev_type]++;
194 t_ptr = strtok(NULL, ".");
196 DPAA2_BUS_ERR("Incorrect device string observed (%s)", t_ptr);
200 sscanf(t_ptr, "%hu", &dev->object_id);
201 dev->device.name = strdup(dev_name);
202 if (!dev->device.name) {
203 DPAA2_BUS_ERR("Unable to clone device name. Out of memory");
206 dev->device.devargs = fslmc_devargs_lookup(dev);
208 /* Add device in the fslmc device list */
209 insert_in_device_list(dev);
211 /* Don't need the duplicated device filesystem entry anymore */
225 rte_fslmc_parse(const char *name, void *addr)
229 char *sep = strchr(name, ':');
231 if (strncmp(name, RTE_STR(FSLMC_BUS_NAME),
232 strlen(RTE_STR(FSLMC_BUS_NAME)))) {
237 DPAA2_BUS_ERR("Incorrect device name observed");
241 t_ptr = (char *)(sep + 1);
243 if (strncmp("dpni", t_ptr, 4) &&
244 strncmp("dpseci", t_ptr, 6) &&
245 strncmp("dpcon", t_ptr, 5) &&
246 strncmp("dpbp", t_ptr, 4) &&
247 strncmp("dpio", t_ptr, 4) &&
248 strncmp("dpci", t_ptr, 4) &&
249 strncmp("dpmcp", t_ptr, 5) &&
250 strncmp("dpdmai", t_ptr, 6)) {
251 DPAA2_BUS_ERR("Unknown or unsupported device");
255 t_ptr = strchr(name, '.');
257 DPAA2_BUS_ERR("Incorrect device string observed (%s)", t_ptr);
261 t_ptr = (char *)(t_ptr + 1);
262 if (sscanf(t_ptr, "%hu", &dev_id) <= 0) {
263 DPAA2_BUS_ERR("Incorrect device string observed (%s)", t_ptr);
268 strcpy(addr, (char *)(sep + 1));
276 int device_count = 0;
277 char fslmc_dirpath[PATH_MAX];
279 struct dirent *entry;
280 static int process_once;
284 DPAA2_BUS_DEBUG("Fslmc bus already scanned. Not rescanning");
289 ret = fslmc_get_container_group(&groupid);
293 /* Scan devices on the group */
294 sprintf(fslmc_dirpath, "%s/%d/devices", VFIO_IOMMU_GROUP_PATH,
296 dir = opendir(fslmc_dirpath);
298 DPAA2_BUS_ERR("Unable to open VFIO group directory");
302 while ((entry = readdir(dir)) != NULL) {
303 if (entry->d_name[0] == '.' || entry->d_type != DT_LNK)
306 ret = scan_one_fslmc_device(entry->d_name);
308 /* Error in parsing directory - exit gracefully */
309 goto scan_fail_cleanup;
316 DPAA2_BUS_INFO("FSLMC Bus scan completed");
317 /* If debugging is enabled, device list is dumped to log output */
325 /* Remove all devices in the list */
326 cleanup_fslmc_device_list();
328 DPAA2_BUS_DEBUG("FSLMC Bus Not Available. Skipping");
329 /* Irrespective of failure, scan only return success */
334 rte_fslmc_match(struct rte_dpaa2_driver *dpaa2_drv,
335 struct rte_dpaa2_device *dpaa2_dev)
337 if (dpaa2_drv->drv_type == dpaa2_dev->dev_type)
344 rte_fslmc_probe(void)
349 struct rte_dpaa2_device *dev;
350 struct rte_dpaa2_driver *drv;
352 if (TAILQ_EMPTY(&rte_fslmc_bus.device_list))
355 ret = fslmc_vfio_setup_group();
357 DPAA2_BUS_ERR("Unable to setup VFIO %d", ret);
361 /* Map existing segments as well as, in case of hotpluggable memory,
362 * install callback handler.
364 ret = rte_fslmc_vfio_dmamap();
366 DPAA2_BUS_ERR("Unable to DMA map existing VAs: (%d)", ret);
367 /* Not continuing ahead */
368 DPAA2_BUS_ERR("FSLMC VFIO Mapping failed");
372 ret = fslmc_vfio_process_group();
374 DPAA2_BUS_ERR("Unable to setup devices %d", ret);
378 probe_all = rte_fslmc_bus.bus.conf.scan_mode != RTE_BUS_SCAN_WHITELIST;
380 TAILQ_FOREACH(dev, &rte_fslmc_bus.device_list, next) {
381 TAILQ_FOREACH(drv, &rte_fslmc_bus.driver_list, next) {
382 ret = rte_fslmc_match(drv, dev);
389 if (dev->device.devargs &&
390 dev->device.devargs->policy == RTE_DEV_BLACKLISTED) {
391 DPAA2_BUS_LOG(DEBUG, "%s Blacklisted, skipping",
397 (dev->device.devargs &&
398 dev->device.devargs->policy ==
399 RTE_DEV_WHITELISTED)) {
400 ret = drv->probe(drv, dev);
402 DPAA2_BUS_ERR("Unable to probe");
405 dev->device.driver = &drv->driver;
412 if (rte_eal_iova_mode() == RTE_IOVA_VA)
418 static struct rte_device *
419 rte_fslmc_find_device(const struct rte_device *start, rte_dev_cmp_t cmp,
422 const struct rte_dpaa2_device *dstart;
423 struct rte_dpaa2_device *dev;
426 dstart = RTE_DEV_TO_FSLMC_CONST(start);
427 dev = TAILQ_NEXT(dstart, next);
429 dev = TAILQ_FIRST(&rte_fslmc_bus.device_list);
431 while (dev != NULL) {
432 if (cmp(&dev->device, data) == 0)
434 dev = TAILQ_NEXT(dev, next);
440 /*register a fslmc bus based dpaa2 driver */
442 rte_fslmc_driver_register(struct rte_dpaa2_driver *driver)
446 TAILQ_INSERT_TAIL(&rte_fslmc_bus.driver_list, driver, next);
447 /* Update Bus references */
448 driver->fslmc_bus = &rte_fslmc_bus;
451 /*un-register a fslmc bus based dpaa2 driver */
453 rte_fslmc_driver_unregister(struct rte_dpaa2_driver *driver)
455 struct rte_fslmc_bus *fslmc_bus;
457 fslmc_bus = driver->fslmc_bus;
459 TAILQ_REMOVE(&fslmc_bus->driver_list, driver, next);
460 /* Update Bus references */
461 driver->fslmc_bus = NULL;
465 * All device has iova as va
468 fslmc_all_device_support_iova(void)
471 struct rte_dpaa2_device *dev;
472 struct rte_dpaa2_driver *drv;
474 TAILQ_FOREACH(dev, &rte_fslmc_bus.device_list, next) {
475 TAILQ_FOREACH(drv, &rte_fslmc_bus.driver_list, next) {
476 ret = rte_fslmc_match(drv, dev);
479 /* if the driver is not supporting IOVA */
480 if (!(drv->drv_flags & RTE_DPAA2_DRV_IOVA_AS_VA))
488 * Get iommu class of DPAA2 devices on the bus.
490 static enum rte_iova_mode
491 rte_dpaa2_get_iommu_class(void)
493 bool is_vfio_noiommu_enabled = 1;
496 if (TAILQ_EMPTY(&rte_fslmc_bus.device_list))
499 #ifdef RTE_LIBRTE_DPAA2_USE_PHYS_IOVA
503 /* check if all devices on the bus support Virtual addressing or not */
504 has_iova_va = fslmc_all_device_support_iova();
507 is_vfio_noiommu_enabled = rte_vfio_noiommu_is_enabled() == true ?
511 if (has_iova_va && !is_vfio_noiommu_enabled)
517 struct rte_fslmc_bus rte_fslmc_bus = {
519 .scan = rte_fslmc_scan,
520 .probe = rte_fslmc_probe,
521 .parse = rte_fslmc_parse,
522 .find_device = rte_fslmc_find_device,
523 .get_iommu_class = rte_dpaa2_get_iommu_class,
525 .device_list = TAILQ_HEAD_INITIALIZER(rte_fslmc_bus.device_list),
526 .driver_list = TAILQ_HEAD_INITIALIZER(rte_fslmc_bus.driver_list),
530 RTE_REGISTER_BUS(FSLMC_BUS_NAME, rte_fslmc_bus.bus);
532 RTE_INIT(fslmc_init_log)
535 dpaa2_logtype_bus = rte_log_register("bus.fslmc");
536 if (dpaa2_logtype_bus >= 0)
537 rte_log_set_level(dpaa2_logtype_bus, RTE_LOG_NOTICE);