1 /* SPDX-License-Identifier: BSD-3-Clause
14 #include <sys/types.h>
15 #include <sys/syscall.h>
17 #include <rte_byteorder.h>
18 #include <rte_common.h>
19 #include <rte_interrupts.h>
21 #include <rte_debug.h>
23 #include <rte_atomic.h>
24 #include <rte_branch_prediction.h>
25 #include <rte_memory.h>
26 #include <rte_tailq.h>
28 #include <rte_alarm.h>
29 #include <rte_ether.h>
30 #include <rte_ethdev.h>
31 #include <rte_malloc.h>
35 #include <rte_dpaa_bus.h>
36 #include <rte_dpaa_logs.h>
45 int dpaa_logtype_mempool;
48 struct rte_dpaa_bus rte_dpaa_bus;
49 struct netcfg_info *dpaa_netcfg;
51 /* define a variable to hold the portal_key, once created.*/
52 pthread_key_t dpaa_portal_key;
54 unsigned int dpaa_svr_family;
56 RTE_DEFINE_PER_LCORE(bool, _dpaa_io);
57 RTE_DEFINE_PER_LCORE(struct dpaa_portal_dqrr, held_bufs);
60 dpaa_add_to_device_list(struct rte_dpaa_device *dev)
62 TAILQ_INSERT_TAIL(&rte_dpaa_bus.device_list, dev, next);
66 dpaa_remove_from_device_list(struct rte_dpaa_device *dev)
68 TAILQ_INSERT_TAIL(&rte_dpaa_bus.device_list, dev, next);
72 * Reads the SEC device from DTS
73 * Returns -1 if SEC devices not available, 0 otherwise
76 dpaa_sec_available(void)
78 const struct device_node *caam_node;
80 for_each_compatible_node(caam_node, NULL, "fsl,sec-v4.0") {
87 static void dpaa_clean_device_list(void);
90 dpaa_create_device_list(void)
94 struct rte_dpaa_device *dev;
95 struct fm_eth_port_cfg *cfg;
96 struct fman_if *fman_intf;
98 /* Creating Ethernet Devices */
99 for (i = 0; i < dpaa_netcfg->num_ethports; i++) {
100 dev = calloc(1, sizeof(struct rte_dpaa_device));
102 DPAA_BUS_LOG(ERR, "Failed to allocate ETH devices");
107 cfg = &dpaa_netcfg->port_cfg[i];
108 fman_intf = cfg->fman_if;
110 /* Device identifiers */
111 dev->id.fman_id = fman_intf->fman_idx + 1;
112 dev->id.mac_id = fman_intf->mac_idx;
113 dev->device_type = FSL_DPAA_ETH;
116 /* Create device name */
117 memset(dev->name, 0, RTE_ETH_NAME_MAX_LEN);
118 sprintf(dev->name, "fm%d-mac%d", (fman_intf->fman_idx + 1),
120 DPAA_BUS_LOG(DEBUG, "Device added: %s", dev->name);
121 dev->device.name = dev->name;
123 dpaa_add_to_device_list(dev);
126 rte_dpaa_bus.device_count = i;
128 /* Unlike case of ETH, RTE_LIBRTE_DPAA_MAX_CRYPTODEV SEC devices are
129 * constantly created only if "sec" property is found in the device
130 * tree. Logically there is no limit for number of devices (QI
131 * interfaces) that can be created.
134 if (dpaa_sec_available()) {
135 DPAA_BUS_LOG(INFO, "DPAA SEC devices are not available");
139 /* Creating SEC Devices */
140 for (i = 0; i < RTE_LIBRTE_DPAA_MAX_CRYPTODEV; i++) {
141 dev = calloc(1, sizeof(struct rte_dpaa_device));
143 DPAA_BUS_LOG(ERR, "Failed to allocate SEC devices");
148 dev->device_type = FSL_DPAA_CRYPTO;
149 dev->id.dev_id = rte_dpaa_bus.device_count + i;
151 /* Even though RTE_CRYPTODEV_NAME_MAX_LEN is valid length of
152 * crypto PMD, using RTE_ETH_NAME_MAX_LEN as that is the size
153 * allocated for dev->name/
155 memset(dev->name, 0, RTE_ETH_NAME_MAX_LEN);
156 sprintf(dev->name, "dpaa-sec%d", i);
157 DPAA_BUS_LOG(DEBUG, "Device added: %s", dev->name);
159 dpaa_add_to_device_list(dev);
162 rte_dpaa_bus.device_count += i;
167 dpaa_clean_device_list();
172 dpaa_clean_device_list(void)
174 struct rte_dpaa_device *dev = NULL;
175 struct rte_dpaa_device *tdev = NULL;
177 TAILQ_FOREACH_SAFE(dev, &rte_dpaa_bus.device_list, next, tdev) {
178 TAILQ_REMOVE(&rte_dpaa_bus.device_list, dev, next);
184 /** XXX move this function into a separate file */
186 _dpaa_portal_init(void *arg)
190 uint32_t cpu = rte_lcore_id();
192 struct dpaa_portal *dpaa_io_portal;
194 BUS_INIT_FUNC_TRACE();
196 if ((uint64_t)arg == 1 || cpu == LCORE_ID_ANY)
197 cpu = rte_get_master_lcore();
198 /* if the core id is not supported */
200 if (cpu >= RTE_MAX_LCORE)
203 /* Set CPU affinity for this thread */
205 CPU_SET(cpu, &cpuset);
207 ret = pthread_setaffinity_np(id, sizeof(cpu_set_t), &cpuset);
209 DPAA_BUS_LOG(ERR, "pthread_setaffinity_np failed on "
210 "core :%d with ret: %d", cpu, ret);
214 /* Initialise bman thread portals */
215 ret = bman_thread_init();
217 DPAA_BUS_LOG(ERR, "bman_thread_init failed on "
218 "core %d with ret: %d", cpu, ret);
222 DPAA_BUS_LOG(DEBUG, "BMAN thread initialized");
224 /* Initialise qman thread portals */
225 ret = qman_thread_init();
227 DPAA_BUS_LOG(ERR, "bman_thread_init failed on "
228 "core %d with ret: %d", cpu, ret);
229 bman_thread_finish();
233 DPAA_BUS_LOG(DEBUG, "QMAN thread initialized");
235 dpaa_io_portal = rte_malloc(NULL, sizeof(struct dpaa_portal),
236 RTE_CACHE_LINE_SIZE);
237 if (!dpaa_io_portal) {
238 DPAA_BUS_LOG(ERR, "Unable to allocate memory");
239 bman_thread_finish();
240 qman_thread_finish();
244 dpaa_io_portal->qman_idx = qman_get_portal_index();
245 dpaa_io_portal->bman_idx = bman_get_portal_index();
246 dpaa_io_portal->tid = syscall(SYS_gettid);
248 ret = pthread_setspecific(dpaa_portal_key, (void *)dpaa_io_portal);
250 DPAA_BUS_LOG(ERR, "pthread_setspecific failed on "
251 "core %d with ret: %d", cpu, ret);
252 dpaa_portal_finish(NULL);
257 RTE_PER_LCORE(_dpaa_io) = true;
259 DPAA_BUS_LOG(DEBUG, "QMAN thread initialized");
265 * rte_dpaa_portal_init - Wrapper over _dpaa_portal_init with thread level check
268 int rte_dpaa_portal_init(void *arg)
270 if (unlikely(!RTE_PER_LCORE(_dpaa_io)))
271 return _dpaa_portal_init(arg);
277 rte_dpaa_portal_fq_init(void *arg, struct qman_fq *fq)
279 /* Affine above created portal with channel*/
281 struct qman_portal *qp;
283 if (unlikely(!RTE_PER_LCORE(_dpaa_io)))
284 _dpaa_portal_init(arg);
286 /* Initialise qman specific portals */
287 qp = fsl_qman_portal_create();
289 DPAA_BUS_LOG(ERR, "Unable to alloc fq portal");
293 sdqcr = QM_SDQCR_CHANNELS_POOL_CONV(fq->ch_id);
294 qman_static_dequeue_add(sdqcr, qp);
299 int rte_dpaa_portal_fq_close(struct qman_fq *fq)
301 return fsl_qman_portal_destroy(fq->qp);
305 dpaa_portal_finish(void *arg)
307 struct dpaa_portal *dpaa_io_portal = (struct dpaa_portal *)arg;
309 if (!dpaa_io_portal) {
310 DPAA_BUS_LOG(DEBUG, "Portal already cleaned");
314 bman_thread_finish();
315 qman_thread_finish();
317 pthread_setspecific(dpaa_portal_key, NULL);
319 rte_free(dpaa_io_portal);
320 dpaa_io_portal = NULL;
322 RTE_PER_LCORE(_dpaa_io) = false;
325 #define DPAA_DEV_PATH1 "/sys/devices/platform/soc/soc:fsl,dpaa"
326 #define DPAA_DEV_PATH2 "/sys/devices/platform/fsl,dpaa"
329 rte_dpaa_bus_scan(void)
333 BUS_INIT_FUNC_TRACE();
335 if ((access(DPAA_DEV_PATH1, F_OK) != 0) &&
336 (access(DPAA_DEV_PATH2, F_OK) != 0)) {
337 RTE_LOG(DEBUG, EAL, "DPAA Bus not present. Skipping.\n");
341 /* Load the device-tree driver */
344 DPAA_BUS_LOG(ERR, "of_init failed with ret: %d", ret);
348 /* Get the interface configurations from device-tree */
349 dpaa_netcfg = netcfg_acquire();
351 DPAA_BUS_LOG(ERR, "netcfg_acquire failed");
355 RTE_LOG(NOTICE, EAL, "DPAA Bus Detected\n");
357 if (!dpaa_netcfg->num_ethports) {
358 DPAA_BUS_LOG(INFO, "no network interfaces available");
359 /* This is not an error */
363 DPAA_BUS_LOG(DEBUG, "Bus: Address of netcfg=%p, Ethports=%d",
364 dpaa_netcfg, dpaa_netcfg->num_ethports);
366 #ifdef RTE_LIBRTE_DPAA_DEBUG_DRIVER
367 dump_netcfg(dpaa_netcfg);
370 DPAA_BUS_LOG(DEBUG, "Number of devices = %d\n",
371 dpaa_netcfg->num_ethports);
372 ret = dpaa_create_device_list();
374 DPAA_BUS_LOG(ERR, "Unable to create device list. (%d)", ret);
378 /* create the key, supplying a function that'll be invoked
379 * when a portal affined thread will be deleted.
381 ret = pthread_key_create(&dpaa_portal_key, dpaa_portal_finish);
383 DPAA_BUS_LOG(DEBUG, "Unable to create pthread key. (%d)", ret);
384 dpaa_clean_device_list();
388 DPAA_BUS_LOG(DEBUG, "dpaa_portal_key=%u, ret=%d\n",
389 (unsigned int)dpaa_portal_key, ret);
394 /* register a dpaa bus based dpaa driver */
396 rte_dpaa_driver_register(struct rte_dpaa_driver *driver)
400 BUS_INIT_FUNC_TRACE();
402 TAILQ_INSERT_TAIL(&rte_dpaa_bus.driver_list, driver, next);
403 /* Update Bus references */
404 driver->dpaa_bus = &rte_dpaa_bus;
407 /* un-register a dpaa bus based dpaa driver */
409 rte_dpaa_driver_unregister(struct rte_dpaa_driver *driver)
411 struct rte_dpaa_bus *dpaa_bus;
413 BUS_INIT_FUNC_TRACE();
415 dpaa_bus = driver->dpaa_bus;
417 TAILQ_REMOVE(&dpaa_bus->driver_list, driver, next);
418 /* Update Bus references */
419 driver->dpaa_bus = NULL;
423 rte_dpaa_device_match(struct rte_dpaa_driver *drv,
424 struct rte_dpaa_device *dev)
428 BUS_INIT_FUNC_TRACE();
431 DPAA_BUS_DEBUG("Invalid drv or dev received.");
435 if (drv->drv_type == dev->device_type) {
436 DPAA_BUS_INFO("Device: %s matches for driver: %s",
437 dev->name, drv->driver.name);
438 ret = 0; /* Found a match */
445 rte_dpaa_bus_probe(void)
448 struct rte_dpaa_device *dev;
449 struct rte_dpaa_driver *drv;
450 FILE *svr_file = NULL;
451 unsigned int svr_ver;
453 BUS_INIT_FUNC_TRACE();
455 /* For each registered driver, and device, call the driver->probe */
456 TAILQ_FOREACH(dev, &rte_dpaa_bus.device_list, next) {
457 TAILQ_FOREACH(drv, &rte_dpaa_bus.driver_list, next) {
458 ret = rte_dpaa_device_match(drv, dev);
465 ret = drv->probe(drv, dev);
467 DPAA_BUS_ERR("Unable to probe.\n");
472 svr_file = fopen(DPAA_SOC_ID_FILE, "r");
474 if (fscanf(svr_file, "svr:%x", &svr_ver) > 0)
475 dpaa_svr_family = svr_ver & SVR_MASK;
482 static struct rte_device *
483 rte_dpaa_find_device(const struct rte_device *start, rte_dev_cmp_t cmp,
486 struct rte_dpaa_device *dev;
488 TAILQ_FOREACH(dev, &rte_dpaa_bus.device_list, next) {
489 if (start && &dev->device == start) {
490 start = NULL; /* starting point found */
494 if (cmp(&dev->device, data) == 0)
502 * Get iommu class of DPAA2 devices on the bus.
504 static enum rte_iova_mode
505 rte_dpaa_get_iommu_class(void)
510 struct rte_dpaa_bus rte_dpaa_bus = {
512 .scan = rte_dpaa_bus_scan,
513 .probe = rte_dpaa_bus_probe,
514 .find_device = rte_dpaa_find_device,
515 .get_iommu_class = rte_dpaa_get_iommu_class,
517 .device_list = TAILQ_HEAD_INITIALIZER(rte_dpaa_bus.device_list),
518 .driver_list = TAILQ_HEAD_INITIALIZER(rte_dpaa_bus.driver_list),
522 RTE_REGISTER_BUS(FSL_DPAA_BUS_NAME, rte_dpaa_bus.bus);
524 RTE_INIT(dpaa_init_log);
528 dpaa_logtype_bus = rte_log_register("bus.dpaa");
529 if (dpaa_logtype_bus >= 0)
530 rte_log_set_level(dpaa_logtype_bus, RTE_LOG_NOTICE);
532 dpaa_logtype_mempool = rte_log_register("mempool.dpaa");
533 if (dpaa_logtype_mempool >= 0)
534 rte_log_set_level(dpaa_logtype_mempool, RTE_LOG_NOTICE);
536 dpaa_logtype_pmd = rte_log_register("pmd.dpaa");
537 if (dpaa_logtype_pmd >= 0)
538 rte_log_set_level(dpaa_logtype_pmd, RTE_LOG_NOTICE);