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_driver.h>
31 #include <rte_malloc.h>
34 #include <rte_mbuf_pool_ops.h>
36 #include <rte_dpaa_bus.h>
37 #include <rte_dpaa_logs.h>
46 int dpaa_logtype_mempool;
48 int dpaa_logtype_eventdev;
50 struct rte_dpaa_bus rte_dpaa_bus;
51 struct netcfg_info *dpaa_netcfg;
53 /* define a variable to hold the portal_key, once created.*/
54 pthread_key_t dpaa_portal_key;
56 unsigned int dpaa_svr_family;
58 RTE_DEFINE_PER_LCORE(bool, _dpaa_io);
59 RTE_DEFINE_PER_LCORE(struct dpaa_portal_dqrr, held_bufs);
62 compare_dpaa_devices(struct rte_dpaa_device *dev1,
63 struct rte_dpaa_device *dev2)
67 /* Segragating ETH from SEC devices */
68 if (dev1->device_type > dev2->device_type)
70 else if (dev1->device_type < dev2->device_type)
75 if ((comp != 0) || (dev1->device_type != FSL_DPAA_ETH))
78 if (dev1->id.fman_id > dev2->id.fman_id) {
80 } else if (dev1->id.fman_id < dev2->id.fman_id) {
83 /* FMAN ids match, check for mac_id */
84 if (dev1->id.mac_id > dev2->id.mac_id)
86 else if (dev1->id.mac_id < dev2->id.mac_id)
96 dpaa_add_to_device_list(struct rte_dpaa_device *newdev)
98 int comp, inserted = 0;
99 struct rte_dpaa_device *dev = NULL;
100 struct rte_dpaa_device *tdev = NULL;
102 TAILQ_FOREACH_SAFE(dev, &rte_dpaa_bus.device_list, next, tdev) {
103 comp = compare_dpaa_devices(newdev, dev);
105 TAILQ_INSERT_BEFORE(dev, newdev, next);
112 TAILQ_INSERT_TAIL(&rte_dpaa_bus.device_list, newdev, next);
116 * Reads the SEC device from DTS
117 * Returns -1 if SEC devices not available, 0 otherwise
120 dpaa_sec_available(void)
122 const struct device_node *caam_node;
124 for_each_compatible_node(caam_node, NULL, "fsl,sec-v4.0") {
131 static void dpaa_clean_device_list(void);
134 dpaa_create_device_list(void)
138 struct rte_dpaa_device *dev;
139 struct fm_eth_port_cfg *cfg;
140 struct fman_if *fman_intf;
142 /* Creating Ethernet Devices */
143 for (i = 0; i < dpaa_netcfg->num_ethports; i++) {
144 dev = calloc(1, sizeof(struct rte_dpaa_device));
146 DPAA_BUS_LOG(ERR, "Failed to allocate ETH devices");
151 cfg = &dpaa_netcfg->port_cfg[i];
152 fman_intf = cfg->fman_if;
154 /* Device identifiers */
155 dev->id.fman_id = fman_intf->fman_idx + 1;
156 dev->id.mac_id = fman_intf->mac_idx;
157 dev->device_type = FSL_DPAA_ETH;
160 /* Create device name */
161 memset(dev->name, 0, RTE_ETH_NAME_MAX_LEN);
162 sprintf(dev->name, "fm%d-mac%d", (fman_intf->fman_idx + 1),
164 DPAA_BUS_LOG(DEBUG, "Device added: %s", dev->name);
165 dev->device.name = dev->name;
167 dpaa_add_to_device_list(dev);
170 rte_dpaa_bus.device_count = i;
172 /* Unlike case of ETH, RTE_LIBRTE_DPAA_MAX_CRYPTODEV SEC devices are
173 * constantly created only if "sec" property is found in the device
174 * tree. Logically there is no limit for number of devices (QI
175 * interfaces) that can be created.
178 if (dpaa_sec_available()) {
179 DPAA_BUS_LOG(INFO, "DPAA SEC devices are not available");
183 /* Creating SEC Devices */
184 for (i = 0; i < RTE_LIBRTE_DPAA_MAX_CRYPTODEV; i++) {
185 dev = calloc(1, sizeof(struct rte_dpaa_device));
187 DPAA_BUS_LOG(ERR, "Failed to allocate SEC devices");
192 dev->device_type = FSL_DPAA_CRYPTO;
193 dev->id.dev_id = rte_dpaa_bus.device_count + i;
195 /* Even though RTE_CRYPTODEV_NAME_MAX_LEN is valid length of
196 * crypto PMD, using RTE_ETH_NAME_MAX_LEN as that is the size
197 * allocated for dev->name/
199 memset(dev->name, 0, RTE_ETH_NAME_MAX_LEN);
200 sprintf(dev->name, "dpaa-sec%d", i);
201 DPAA_BUS_LOG(DEBUG, "Device added: %s", dev->name);
203 dpaa_add_to_device_list(dev);
206 rte_dpaa_bus.device_count += i;
211 dpaa_clean_device_list();
216 dpaa_clean_device_list(void)
218 struct rte_dpaa_device *dev = NULL;
219 struct rte_dpaa_device *tdev = NULL;
221 TAILQ_FOREACH_SAFE(dev, &rte_dpaa_bus.device_list, next, tdev) {
222 TAILQ_REMOVE(&rte_dpaa_bus.device_list, dev, next);
228 /** XXX move this function into a separate file */
230 _dpaa_portal_init(void *arg)
234 uint32_t cpu = rte_lcore_id();
236 struct dpaa_portal *dpaa_io_portal;
238 BUS_INIT_FUNC_TRACE();
240 if ((uint64_t)arg == 1 || cpu == LCORE_ID_ANY)
241 cpu = rte_get_master_lcore();
242 /* if the core id is not supported */
244 if (cpu >= RTE_MAX_LCORE)
247 /* Set CPU affinity for this thread */
249 CPU_SET(cpu, &cpuset);
251 ret = pthread_setaffinity_np(id, sizeof(cpu_set_t), &cpuset);
253 DPAA_BUS_LOG(ERR, "pthread_setaffinity_np failed on "
254 "core :%d with ret: %d", cpu, ret);
258 /* Initialise bman thread portals */
259 ret = bman_thread_init();
261 DPAA_BUS_LOG(ERR, "bman_thread_init failed on "
262 "core %d with ret: %d", cpu, ret);
266 DPAA_BUS_LOG(DEBUG, "BMAN thread initialized");
268 /* Initialise qman thread portals */
269 ret = qman_thread_init();
271 DPAA_BUS_LOG(ERR, "bman_thread_init failed on "
272 "core %d with ret: %d", cpu, ret);
273 bman_thread_finish();
277 DPAA_BUS_LOG(DEBUG, "QMAN thread initialized");
279 dpaa_io_portal = rte_malloc(NULL, sizeof(struct dpaa_portal),
280 RTE_CACHE_LINE_SIZE);
281 if (!dpaa_io_portal) {
282 DPAA_BUS_LOG(ERR, "Unable to allocate memory");
283 bman_thread_finish();
284 qman_thread_finish();
288 dpaa_io_portal->qman_idx = qman_get_portal_index();
289 dpaa_io_portal->bman_idx = bman_get_portal_index();
290 dpaa_io_portal->tid = syscall(SYS_gettid);
292 ret = pthread_setspecific(dpaa_portal_key, (void *)dpaa_io_portal);
294 DPAA_BUS_LOG(ERR, "pthread_setspecific failed on "
295 "core %d with ret: %d", cpu, ret);
296 dpaa_portal_finish(NULL);
301 RTE_PER_LCORE(_dpaa_io) = true;
303 DPAA_BUS_LOG(DEBUG, "QMAN thread initialized");
309 * rte_dpaa_portal_init - Wrapper over _dpaa_portal_init with thread level check
312 int rte_dpaa_portal_init(void *arg)
314 if (unlikely(!RTE_PER_LCORE(_dpaa_io)))
315 return _dpaa_portal_init(arg);
321 rte_dpaa_portal_fq_init(void *arg, struct qman_fq *fq)
323 /* Affine above created portal with channel*/
325 struct qman_portal *qp;
327 if (unlikely(!RTE_PER_LCORE(_dpaa_io)))
328 _dpaa_portal_init(arg);
330 /* Initialise qman specific portals */
331 qp = fsl_qman_portal_create();
333 DPAA_BUS_LOG(ERR, "Unable to alloc fq portal");
337 sdqcr = QM_SDQCR_CHANNELS_POOL_CONV(fq->ch_id);
338 qman_static_dequeue_add(sdqcr, qp);
343 int rte_dpaa_portal_fq_close(struct qman_fq *fq)
345 return fsl_qman_portal_destroy(fq->qp);
349 dpaa_portal_finish(void *arg)
351 struct dpaa_portal *dpaa_io_portal = (struct dpaa_portal *)arg;
353 if (!dpaa_io_portal) {
354 DPAA_BUS_LOG(DEBUG, "Portal already cleaned");
358 bman_thread_finish();
359 qman_thread_finish();
361 pthread_setspecific(dpaa_portal_key, NULL);
363 rte_free(dpaa_io_portal);
364 dpaa_io_portal = NULL;
366 RTE_PER_LCORE(_dpaa_io) = false;
369 #define DPAA_DEV_PATH1 "/sys/devices/platform/soc/soc:fsl,dpaa"
370 #define DPAA_DEV_PATH2 "/sys/devices/platform/fsl,dpaa"
373 rte_dpaa_bus_scan(void)
377 BUS_INIT_FUNC_TRACE();
379 if ((access(DPAA_DEV_PATH1, F_OK) != 0) &&
380 (access(DPAA_DEV_PATH2, F_OK) != 0)) {
381 RTE_LOG(DEBUG, EAL, "DPAA Bus not present. Skipping.\n");
385 /* Load the device-tree driver */
388 DPAA_BUS_LOG(ERR, "of_init failed with ret: %d", ret);
392 /* Get the interface configurations from device-tree */
393 dpaa_netcfg = netcfg_acquire();
395 DPAA_BUS_LOG(ERR, "netcfg_acquire failed");
399 RTE_LOG(NOTICE, EAL, "DPAA Bus Detected\n");
401 if (!dpaa_netcfg->num_ethports) {
402 DPAA_BUS_LOG(INFO, "no network interfaces available");
403 /* This is not an error */
407 DPAA_BUS_LOG(DEBUG, "Bus: Address of netcfg=%p, Ethports=%d",
408 dpaa_netcfg, dpaa_netcfg->num_ethports);
410 #ifdef RTE_LIBRTE_DPAA_DEBUG_DRIVER
411 dump_netcfg(dpaa_netcfg);
414 DPAA_BUS_LOG(DEBUG, "Number of devices = %d\n",
415 dpaa_netcfg->num_ethports);
416 ret = dpaa_create_device_list();
418 DPAA_BUS_LOG(ERR, "Unable to create device list. (%d)", ret);
422 /* create the key, supplying a function that'll be invoked
423 * when a portal affined thread will be deleted.
425 ret = pthread_key_create(&dpaa_portal_key, dpaa_portal_finish);
427 DPAA_BUS_LOG(DEBUG, "Unable to create pthread key. (%d)", ret);
428 dpaa_clean_device_list();
432 DPAA_BUS_LOG(DEBUG, "dpaa_portal_key=%u, ret=%d\n",
433 (unsigned int)dpaa_portal_key, ret);
438 /* register a dpaa bus based dpaa driver */
440 rte_dpaa_driver_register(struct rte_dpaa_driver *driver)
444 BUS_INIT_FUNC_TRACE();
446 TAILQ_INSERT_TAIL(&rte_dpaa_bus.driver_list, driver, next);
447 /* Update Bus references */
448 driver->dpaa_bus = &rte_dpaa_bus;
451 /* un-register a dpaa bus based dpaa driver */
453 rte_dpaa_driver_unregister(struct rte_dpaa_driver *driver)
455 struct rte_dpaa_bus *dpaa_bus;
457 BUS_INIT_FUNC_TRACE();
459 dpaa_bus = driver->dpaa_bus;
461 TAILQ_REMOVE(&dpaa_bus->driver_list, driver, next);
462 /* Update Bus references */
463 driver->dpaa_bus = NULL;
467 rte_dpaa_device_match(struct rte_dpaa_driver *drv,
468 struct rte_dpaa_device *dev)
472 BUS_INIT_FUNC_TRACE();
475 DPAA_BUS_DEBUG("Invalid drv or dev received.");
479 if (drv->drv_type == dev->device_type) {
480 DPAA_BUS_INFO("Device: %s matches for driver: %s",
481 dev->name, drv->driver.name);
482 ret = 0; /* Found a match */
489 rte_dpaa_bus_probe(void)
492 struct rte_dpaa_device *dev;
493 struct rte_dpaa_driver *drv;
494 FILE *svr_file = NULL;
495 unsigned int svr_ver;
497 BUS_INIT_FUNC_TRACE();
499 /* For each registered driver, and device, call the driver->probe */
500 TAILQ_FOREACH(dev, &rte_dpaa_bus.device_list, next) {
501 TAILQ_FOREACH(drv, &rte_dpaa_bus.driver_list, next) {
502 ret = rte_dpaa_device_match(drv, dev);
509 ret = drv->probe(drv, dev);
511 DPAA_BUS_ERR("Unable to probe.\n");
515 rte_mbuf_set_platform_mempool_ops(DPAA_MEMPOOL_OPS_NAME);
517 svr_file = fopen(DPAA_SOC_ID_FILE, "r");
519 if (fscanf(svr_file, "svr:%x", &svr_ver) > 0)
520 dpaa_svr_family = svr_ver & SVR_MASK;
527 static struct rte_device *
528 rte_dpaa_find_device(const struct rte_device *start, rte_dev_cmp_t cmp,
531 struct rte_dpaa_device *dev;
533 TAILQ_FOREACH(dev, &rte_dpaa_bus.device_list, next) {
534 if (start && &dev->device == start) {
535 start = NULL; /* starting point found */
539 if (cmp(&dev->device, data) == 0)
547 * Get iommu class of DPAA2 devices on the bus.
549 static enum rte_iova_mode
550 rte_dpaa_get_iommu_class(void)
555 struct rte_dpaa_bus rte_dpaa_bus = {
557 .scan = rte_dpaa_bus_scan,
558 .probe = rte_dpaa_bus_probe,
559 .find_device = rte_dpaa_find_device,
560 .get_iommu_class = rte_dpaa_get_iommu_class,
562 .device_list = TAILQ_HEAD_INITIALIZER(rte_dpaa_bus.device_list),
563 .driver_list = TAILQ_HEAD_INITIALIZER(rte_dpaa_bus.driver_list),
567 RTE_REGISTER_BUS(FSL_DPAA_BUS_NAME, rte_dpaa_bus.bus);
569 RTE_INIT(dpaa_init_log);
573 dpaa_logtype_bus = rte_log_register("bus.dpaa");
574 if (dpaa_logtype_bus >= 0)
575 rte_log_set_level(dpaa_logtype_bus, RTE_LOG_NOTICE);
577 dpaa_logtype_mempool = rte_log_register("mempool.dpaa");
578 if (dpaa_logtype_mempool >= 0)
579 rte_log_set_level(dpaa_logtype_mempool, RTE_LOG_NOTICE);
581 dpaa_logtype_pmd = rte_log_register("pmd.dpaa");
582 if (dpaa_logtype_pmd >= 0)
583 rte_log_set_level(dpaa_logtype_pmd, RTE_LOG_NOTICE);
585 dpaa_logtype_eventdev = rte_log_register("eventdev.dpaa");
586 if (dpaa_logtype_eventdev >= 0)
587 rte_log_set_level(dpaa_logtype_eventdev, RTE_LOG_NOTICE);