6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
10 * * Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * * Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in
14 * the documentation and/or other materials provided with the
16 * * Neither the name of NXP nor the names of its
17 * contributors may be used to endorse or promote products derived
18 * from this software without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
40 #include <sys/types.h>
41 #include <sys/syscall.h>
43 #include <rte_byteorder.h>
44 #include <rte_common.h>
45 #include <rte_interrupts.h>
47 #include <rte_debug.h>
49 #include <rte_atomic.h>
50 #include <rte_branch_prediction.h>
51 #include <rte_memory.h>
52 #include <rte_memzone.h>
53 #include <rte_tailq.h>
55 #include <rte_alarm.h>
56 #include <rte_ether.h>
57 #include <rte_ethdev.h>
58 #include <rte_malloc.h>
62 #include <rte_dpaa_bus.h>
63 #include <rte_dpaa_logs.h>
72 int dpaa_logtype_mempool;
75 struct rte_dpaa_bus rte_dpaa_bus;
76 struct netcfg_info *dpaa_netcfg;
78 /* define a variable to hold the portal_key, once created.*/
79 pthread_key_t dpaa_portal_key;
81 RTE_DEFINE_PER_LCORE(bool, _dpaa_io);
84 dpaa_add_to_device_list(struct rte_dpaa_device *dev)
86 TAILQ_INSERT_TAIL(&rte_dpaa_bus.device_list, dev, next);
90 dpaa_remove_from_device_list(struct rte_dpaa_device *dev)
92 TAILQ_INSERT_TAIL(&rte_dpaa_bus.device_list, dev, next);
96 * Reads the SEC device from DTS
97 * Returns -1 if SEC devices not available, 0 otherwise
100 dpaa_sec_available(void)
102 const struct device_node *caam_node;
104 for_each_compatible_node(caam_node, NULL, "fsl,sec-v4.0") {
111 static void dpaa_clean_device_list(void);
114 dpaa_create_device_list(void)
118 struct rte_dpaa_device *dev;
119 struct fm_eth_port_cfg *cfg;
120 struct fman_if *fman_intf;
122 /* Creating Ethernet Devices */
123 for (i = 0; i < dpaa_netcfg->num_ethports; i++) {
124 dev = calloc(1, sizeof(struct rte_dpaa_device));
126 DPAA_BUS_LOG(ERR, "Failed to allocate ETH devices");
131 cfg = &dpaa_netcfg->port_cfg[i];
132 fman_intf = cfg->fman_if;
134 /* Device identifiers */
135 dev->id.fman_id = fman_intf->fman_idx + 1;
136 dev->id.mac_id = fman_intf->mac_idx;
137 dev->device_type = FSL_DPAA_ETH;
140 /* Create device name */
141 memset(dev->name, 0, RTE_ETH_NAME_MAX_LEN);
142 sprintf(dev->name, "fm%d-mac%d", (fman_intf->fman_idx + 1),
144 DPAA_BUS_LOG(DEBUG, "Device added: %s", dev->name);
145 dev->device.name = dev->name;
147 dpaa_add_to_device_list(dev);
150 rte_dpaa_bus.device_count = i;
152 /* Unlike case of ETH, RTE_LIBRTE_DPAA_MAX_CRYPTODEV SEC devices are
153 * constantly created only if "sec" property is found in the device
154 * tree. Logically there is no limit for number of devices (QI
155 * interfaces) that can be created.
158 if (dpaa_sec_available()) {
159 DPAA_BUS_LOG(INFO, "DPAA SEC devices are not available");
163 /* Creating SEC Devices */
164 for (i = 0; i < RTE_LIBRTE_DPAA_MAX_CRYPTODEV; i++) {
165 dev = calloc(1, sizeof(struct rte_dpaa_device));
167 DPAA_BUS_LOG(ERR, "Failed to allocate SEC devices");
172 dev->device_type = FSL_DPAA_CRYPTO;
173 dev->id.dev_id = rte_dpaa_bus.device_count + i;
175 /* Even though RTE_CRYPTODEV_NAME_MAX_LEN is valid length of
176 * crypto PMD, using RTE_ETH_NAME_MAX_LEN as that is the size
177 * allocated for dev->name/
179 memset(dev->name, 0, RTE_ETH_NAME_MAX_LEN);
180 sprintf(dev->name, "dpaa-sec%d", i);
181 DPAA_BUS_LOG(DEBUG, "Device added: %s", dev->name);
183 dpaa_add_to_device_list(dev);
186 rte_dpaa_bus.device_count += i;
191 dpaa_clean_device_list();
196 dpaa_clean_device_list(void)
198 struct rte_dpaa_device *dev = NULL;
199 struct rte_dpaa_device *tdev = NULL;
201 TAILQ_FOREACH_SAFE(dev, &rte_dpaa_bus.device_list, next, tdev) {
202 TAILQ_REMOVE(&rte_dpaa_bus.device_list, dev, next);
208 /** XXX move this function into a separate file */
210 _dpaa_portal_init(void *arg)
214 uint32_t cpu = rte_lcore_id();
216 struct dpaa_portal *dpaa_io_portal;
218 BUS_INIT_FUNC_TRACE();
220 if ((uint64_t)arg == 1 || cpu == LCORE_ID_ANY)
221 cpu = rte_get_master_lcore();
222 /* if the core id is not supported */
224 if (cpu >= RTE_MAX_LCORE)
227 /* Set CPU affinity for this thread */
229 CPU_SET(cpu, &cpuset);
231 ret = pthread_setaffinity_np(id, sizeof(cpu_set_t), &cpuset);
233 DPAA_BUS_LOG(ERR, "pthread_setaffinity_np failed on "
234 "core :%d with ret: %d", cpu, ret);
238 /* Initialise bman thread portals */
239 ret = bman_thread_init();
241 DPAA_BUS_LOG(ERR, "bman_thread_init failed on "
242 "core %d with ret: %d", cpu, ret);
246 DPAA_BUS_LOG(DEBUG, "BMAN thread initialized");
248 /* Initialise qman thread portals */
249 ret = qman_thread_init();
251 DPAA_BUS_LOG(ERR, "bman_thread_init failed on "
252 "core %d with ret: %d", cpu, ret);
253 bman_thread_finish();
257 DPAA_BUS_LOG(DEBUG, "QMAN thread initialized");
259 dpaa_io_portal = rte_malloc(NULL, sizeof(struct dpaa_portal),
260 RTE_CACHE_LINE_SIZE);
261 if (!dpaa_io_portal) {
262 DPAA_BUS_LOG(ERR, "Unable to allocate memory");
263 bman_thread_finish();
264 qman_thread_finish();
268 dpaa_io_portal->qman_idx = qman_get_portal_index();
269 dpaa_io_portal->bman_idx = bman_get_portal_index();
270 dpaa_io_portal->tid = syscall(SYS_gettid);
272 ret = pthread_setspecific(dpaa_portal_key, (void *)dpaa_io_portal);
274 DPAA_BUS_LOG(ERR, "pthread_setspecific failed on "
275 "core %d with ret: %d", cpu, ret);
276 dpaa_portal_finish(NULL);
281 RTE_PER_LCORE(_dpaa_io) = true;
283 DPAA_BUS_LOG(DEBUG, "QMAN thread initialized");
289 * rte_dpaa_portal_init - Wrapper over _dpaa_portal_init with thread level check
293 rte_dpaa_portal_init(void *arg)
295 if (unlikely(!RTE_PER_LCORE(_dpaa_io)))
296 return _dpaa_portal_init(arg);
302 dpaa_portal_finish(void *arg)
304 struct dpaa_portal *dpaa_io_portal = (struct dpaa_portal *)arg;
306 if (!dpaa_io_portal) {
307 DPAA_BUS_LOG(DEBUG, "Portal already cleaned");
311 bman_thread_finish();
312 qman_thread_finish();
314 pthread_setspecific(dpaa_portal_key, NULL);
316 rte_free(dpaa_io_portal);
317 dpaa_io_portal = NULL;
319 RTE_PER_LCORE(_dpaa_io) = false;
322 #define DPAA_DEV_PATH1 "/sys/devices/platform/soc/soc:fsl,dpaa"
323 #define DPAA_DEV_PATH2 "/sys/devices/platform/fsl,dpaa"
326 rte_dpaa_bus_scan(void)
330 BUS_INIT_FUNC_TRACE();
332 if ((access(DPAA_DEV_PATH1, F_OK) != 0) &&
333 (access(DPAA_DEV_PATH2, F_OK) != 0)) {
334 RTE_LOG(DEBUG, EAL, "DPAA Bus not present. Skipping.\n");
338 /* Load the device-tree driver */
341 DPAA_BUS_LOG(ERR, "of_init failed with ret: %d", ret);
345 /* Get the interface configurations from device-tree */
346 dpaa_netcfg = netcfg_acquire();
348 DPAA_BUS_LOG(ERR, "netcfg_acquire failed");
352 RTE_LOG(NOTICE, EAL, "DPAA Bus Detected\n");
354 if (!dpaa_netcfg->num_ethports) {
355 DPAA_BUS_LOG(INFO, "no network interfaces available");
356 /* This is not an error */
360 DPAA_BUS_LOG(DEBUG, "Bus: Address of netcfg=%p, Ethports=%d",
361 dpaa_netcfg, dpaa_netcfg->num_ethports);
363 #ifdef RTE_LIBRTE_DPAA_DEBUG_DRIVER
364 dump_netcfg(dpaa_netcfg);
367 DPAA_BUS_LOG(DEBUG, "Number of devices = %d\n",
368 dpaa_netcfg->num_ethports);
369 ret = dpaa_create_device_list();
371 DPAA_BUS_LOG(ERR, "Unable to create device list. (%d)", ret);
375 /* create the key, supplying a function that'll be invoked
376 * when a portal affined thread will be deleted.
378 ret = pthread_key_create(&dpaa_portal_key, dpaa_portal_finish);
380 DPAA_BUS_LOG(DEBUG, "Unable to create pthread key. (%d)", ret);
381 dpaa_clean_device_list();
385 DPAA_BUS_LOG(DEBUG, "dpaa_portal_key=%u, ret=%d\n",
386 (unsigned int)dpaa_portal_key, ret);
391 /* register a dpaa bus based dpaa driver */
393 rte_dpaa_driver_register(struct rte_dpaa_driver *driver)
397 BUS_INIT_FUNC_TRACE();
399 TAILQ_INSERT_TAIL(&rte_dpaa_bus.driver_list, driver, next);
400 /* Update Bus references */
401 driver->dpaa_bus = &rte_dpaa_bus;
404 /* un-register a dpaa bus based dpaa driver */
406 rte_dpaa_driver_unregister(struct rte_dpaa_driver *driver)
408 struct rte_dpaa_bus *dpaa_bus;
410 BUS_INIT_FUNC_TRACE();
412 dpaa_bus = driver->dpaa_bus;
414 TAILQ_REMOVE(&dpaa_bus->driver_list, driver, next);
415 /* Update Bus references */
416 driver->dpaa_bus = NULL;
420 rte_dpaa_device_match(struct rte_dpaa_driver *drv,
421 struct rte_dpaa_device *dev)
425 BUS_INIT_FUNC_TRACE();
428 DPAA_BUS_DEBUG("Invalid drv or dev received.");
432 if (drv->drv_type == dev->device_type) {
433 DPAA_BUS_INFO("Device: %s matches for driver: %s",
434 dev->name, drv->driver.name);
435 ret = 0; /* Found a match */
442 rte_dpaa_bus_probe(void)
445 struct rte_dpaa_device *dev;
446 struct rte_dpaa_driver *drv;
448 BUS_INIT_FUNC_TRACE();
450 /* For each registered driver, and device, call the driver->probe */
451 TAILQ_FOREACH(dev, &rte_dpaa_bus.device_list, next) {
452 TAILQ_FOREACH(drv, &rte_dpaa_bus.driver_list, next) {
453 ret = rte_dpaa_device_match(drv, dev);
460 ret = drv->probe(drv, dev);
462 DPAA_BUS_ERR("Unable to probe.\n");
469 static struct rte_device *
470 rte_dpaa_find_device(const struct rte_device *start, rte_dev_cmp_t cmp,
473 struct rte_dpaa_device *dev;
475 TAILQ_FOREACH(dev, &rte_dpaa_bus.device_list, next) {
476 if (start && &dev->device == start) {
477 start = NULL; /* starting point found */
481 if (cmp(&dev->device, data) == 0)
488 struct rte_dpaa_bus rte_dpaa_bus = {
490 .scan = rte_dpaa_bus_scan,
491 .probe = rte_dpaa_bus_probe,
492 .find_device = rte_dpaa_find_device,
494 .device_list = TAILQ_HEAD_INITIALIZER(rte_dpaa_bus.device_list),
495 .driver_list = TAILQ_HEAD_INITIALIZER(rte_dpaa_bus.driver_list),
499 RTE_REGISTER_BUS(FSL_DPAA_BUS_NAME, rte_dpaa_bus.bus);
501 RTE_INIT(dpaa_init_log);
505 dpaa_logtype_bus = rte_log_register("bus.dpaa");
506 if (dpaa_logtype_bus >= 0)
507 rte_log_set_level(dpaa_logtype_bus, RTE_LOG_NOTICE);
509 dpaa_logtype_mempool = rte_log_register("mempool.dpaa");
510 if (dpaa_logtype_mempool >= 0)
511 rte_log_set_level(dpaa_logtype_mempool, RTE_LOG_NOTICE);
513 dpaa_logtype_pmd = rte_log_register("pmd.dpaa");
514 if (dpaa_logtype_pmd >= 0)
515 rte_log_set_level(dpaa_logtype_pmd, RTE_LOG_NOTICE);