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_config.h>
44 #include <rte_byteorder.h>
45 #include <rte_common.h>
46 #include <rte_interrupts.h>
48 #include <rte_debug.h>
50 #include <rte_atomic.h>
51 #include <rte_branch_prediction.h>
52 #include <rte_memory.h>
53 #include <rte_memzone.h>
54 #include <rte_tailq.h>
56 #include <rte_alarm.h>
57 #include <rte_ether.h>
58 #include <rte_ethdev.h>
59 #include <rte_malloc.h>
63 #include <rte_dpaa_bus.h>
64 #include <rte_dpaa_logs.h>
73 int dpaa_logtype_mempool;
76 struct rte_dpaa_bus rte_dpaa_bus;
77 struct netcfg_info *dpaa_netcfg;
79 /* define a variable to hold the portal_key, once created.*/
80 pthread_key_t dpaa_portal_key;
82 RTE_DEFINE_PER_LCORE(bool, _dpaa_io);
85 dpaa_add_to_device_list(struct rte_dpaa_device *dev)
87 TAILQ_INSERT_TAIL(&rte_dpaa_bus.device_list, dev, next);
91 dpaa_remove_from_device_list(struct rte_dpaa_device *dev)
93 TAILQ_INSERT_TAIL(&rte_dpaa_bus.device_list, dev, next);
97 * Reads the SEC device from DTS
98 * Returns -1 if SEC devices not available, 0 otherwise
101 dpaa_sec_available(void)
103 const struct device_node *caam_node;
105 for_each_compatible_node(caam_node, NULL, "fsl,sec-v4.0") {
112 static void dpaa_clean_device_list(void);
115 dpaa_create_device_list(void)
119 struct rte_dpaa_device *dev;
120 struct fm_eth_port_cfg *cfg;
121 struct fman_if *fman_intf;
123 /* Creating Ethernet Devices */
124 for (i = 0; i < dpaa_netcfg->num_ethports; i++) {
125 dev = calloc(1, sizeof(struct rte_dpaa_device));
127 DPAA_BUS_LOG(ERR, "Failed to allocate ETH devices");
132 cfg = &dpaa_netcfg->port_cfg[i];
133 fman_intf = cfg->fman_if;
135 /* Device identifiers */
136 dev->id.fman_id = fman_intf->fman_idx + 1;
137 dev->id.mac_id = fman_intf->mac_idx;
138 dev->device_type = FSL_DPAA_ETH;
141 /* Create device name */
142 memset(dev->name, 0, RTE_ETH_NAME_MAX_LEN);
143 sprintf(dev->name, "fm%d-mac%d", (fman_intf->fman_idx + 1),
145 DPAA_BUS_LOG(DEBUG, "Device added: %s", dev->name);
146 dev->device.name = dev->name;
148 dpaa_add_to_device_list(dev);
151 rte_dpaa_bus.device_count = i;
153 /* Unlike case of ETH, RTE_LIBRTE_DPAA_MAX_CRYPTODEV SEC devices are
154 * constantly created only if "sec" property is found in the device
155 * tree. Logically there is no limit for number of devices (QI
156 * interfaces) that can be created.
159 if (dpaa_sec_available()) {
160 DPAA_BUS_LOG(INFO, "DPAA SEC devices are not available");
164 /* Creating SEC Devices */
165 for (i = 0; i < RTE_LIBRTE_DPAA_MAX_CRYPTODEV; i++) {
166 dev = calloc(1, sizeof(struct rte_dpaa_device));
168 DPAA_BUS_LOG(ERR, "Failed to allocate SEC devices");
173 dev->device_type = FSL_DPAA_CRYPTO;
174 dev->id.dev_id = rte_dpaa_bus.device_count + i;
176 /* Even though RTE_CRYPTODEV_NAME_MAX_LEN is valid length of
177 * crypto PMD, using RTE_ETH_NAME_MAX_LEN as that is the size
178 * allocated for dev->name/
180 memset(dev->name, 0, RTE_ETH_NAME_MAX_LEN);
181 sprintf(dev->name, "dpaa-sec%d", i);
182 DPAA_BUS_LOG(DEBUG, "Device added: %s", dev->name);
184 dpaa_add_to_device_list(dev);
187 rte_dpaa_bus.device_count += i;
192 dpaa_clean_device_list();
197 dpaa_clean_device_list(void)
199 struct rte_dpaa_device *dev = NULL;
200 struct rte_dpaa_device *tdev = NULL;
202 TAILQ_FOREACH_SAFE(dev, &rte_dpaa_bus.device_list, next, tdev) {
203 TAILQ_REMOVE(&rte_dpaa_bus.device_list, dev, next);
209 /** XXX move this function into a separate file */
211 _dpaa_portal_init(void *arg)
215 uint32_t cpu = rte_lcore_id();
217 struct dpaa_portal *dpaa_io_portal;
219 BUS_INIT_FUNC_TRACE();
221 if ((uint64_t)arg == 1 || cpu == LCORE_ID_ANY)
222 cpu = rte_get_master_lcore();
223 /* if the core id is not supported */
225 if (cpu >= RTE_MAX_LCORE)
228 /* Set CPU affinity for this thread */
230 CPU_SET(cpu, &cpuset);
232 ret = pthread_setaffinity_np(id, sizeof(cpu_set_t), &cpuset);
234 DPAA_BUS_LOG(ERR, "pthread_setaffinity_np failed on "
235 "core :%d with ret: %d", cpu, ret);
239 /* Initialise bman thread portals */
240 ret = bman_thread_init();
242 DPAA_BUS_LOG(ERR, "bman_thread_init failed on "
243 "core %d with ret: %d", cpu, ret);
247 DPAA_BUS_LOG(DEBUG, "BMAN thread initialized");
249 /* Initialise qman thread portals */
250 ret = qman_thread_init();
252 DPAA_BUS_LOG(ERR, "bman_thread_init failed on "
253 "core %d with ret: %d", cpu, ret);
254 bman_thread_finish();
258 DPAA_BUS_LOG(DEBUG, "QMAN thread initialized");
260 dpaa_io_portal = rte_malloc(NULL, sizeof(struct dpaa_portal),
261 RTE_CACHE_LINE_SIZE);
262 if (!dpaa_io_portal) {
263 DPAA_BUS_LOG(ERR, "Unable to allocate memory");
264 bman_thread_finish();
265 qman_thread_finish();
269 dpaa_io_portal->qman_idx = qman_get_portal_index();
270 dpaa_io_portal->bman_idx = bman_get_portal_index();
271 dpaa_io_portal->tid = syscall(SYS_gettid);
273 ret = pthread_setspecific(dpaa_portal_key, (void *)dpaa_io_portal);
275 DPAA_BUS_LOG(ERR, "pthread_setspecific failed on "
276 "core %d with ret: %d", cpu, ret);
277 dpaa_portal_finish(NULL);
282 RTE_PER_LCORE(_dpaa_io) = true;
284 DPAA_BUS_LOG(DEBUG, "QMAN thread initialized");
290 * rte_dpaa_portal_init - Wrapper over _dpaa_portal_init with thread level check
294 rte_dpaa_portal_init(void *arg)
296 if (unlikely(!RTE_PER_LCORE(_dpaa_io)))
297 return _dpaa_portal_init(arg);
303 dpaa_portal_finish(void *arg)
305 struct dpaa_portal *dpaa_io_portal = (struct dpaa_portal *)arg;
307 if (!dpaa_io_portal) {
308 DPAA_BUS_LOG(DEBUG, "Portal already cleaned");
312 bman_thread_finish();
313 qman_thread_finish();
315 pthread_setspecific(dpaa_portal_key, NULL);
317 rte_free(dpaa_io_portal);
318 dpaa_io_portal = NULL;
320 RTE_PER_LCORE(_dpaa_io) = false;
323 #define DPAA_DEV_PATH1 "/sys/devices/platform/soc/soc:fsl,dpaa"
324 #define DPAA_DEV_PATH2 "/sys/devices/platform/fsl,dpaa"
327 rte_dpaa_bus_scan(void)
331 BUS_INIT_FUNC_TRACE();
333 if ((access(DPAA_DEV_PATH1, F_OK) != 0) &&
334 (access(DPAA_DEV_PATH2, F_OK) != 0)) {
335 RTE_LOG(DEBUG, EAL, "DPAA Bus not present. Skipping.\n");
339 /* Load the device-tree driver */
342 DPAA_BUS_LOG(ERR, "of_init failed with ret: %d", ret);
346 /* Get the interface configurations from device-tree */
347 dpaa_netcfg = netcfg_acquire();
349 DPAA_BUS_LOG(ERR, "netcfg_acquire failed");
353 RTE_LOG(NOTICE, EAL, "DPAA Bus Detected\n");
355 if (!dpaa_netcfg->num_ethports) {
356 DPAA_BUS_LOG(INFO, "no network interfaces available");
357 /* This is not an error */
361 DPAA_BUS_LOG(DEBUG, "Bus: Address of netcfg=%p, Ethports=%d",
362 dpaa_netcfg, dpaa_netcfg->num_ethports);
364 #ifdef RTE_LIBRTE_DPAA_DEBUG_DRIVER
365 dump_netcfg(dpaa_netcfg);
368 DPAA_BUS_LOG(DEBUG, "Number of devices = %d\n",
369 dpaa_netcfg->num_ethports);
370 ret = dpaa_create_device_list();
372 DPAA_BUS_LOG(ERR, "Unable to create device list. (%d)", ret);
376 /* create the key, supplying a function that'll be invoked
377 * when a portal affined thread will be deleted.
379 ret = pthread_key_create(&dpaa_portal_key, dpaa_portal_finish);
381 DPAA_BUS_LOG(DEBUG, "Unable to create pthread key. (%d)", ret);
382 dpaa_clean_device_list();
386 DPAA_BUS_LOG(DEBUG, "dpaa_portal_key=%u, ret=%d\n",
387 (unsigned int)dpaa_portal_key, ret);
392 /* register a dpaa bus based dpaa driver */
394 rte_dpaa_driver_register(struct rte_dpaa_driver *driver)
398 BUS_INIT_FUNC_TRACE();
400 TAILQ_INSERT_TAIL(&rte_dpaa_bus.driver_list, driver, next);
401 /* Update Bus references */
402 driver->dpaa_bus = &rte_dpaa_bus;
405 /* un-register a dpaa bus based dpaa driver */
407 rte_dpaa_driver_unregister(struct rte_dpaa_driver *driver)
409 struct rte_dpaa_bus *dpaa_bus;
411 BUS_INIT_FUNC_TRACE();
413 dpaa_bus = driver->dpaa_bus;
415 TAILQ_REMOVE(&dpaa_bus->driver_list, driver, next);
416 /* Update Bus references */
417 driver->dpaa_bus = NULL;
421 rte_dpaa_device_match(struct rte_dpaa_driver *drv,
422 struct rte_dpaa_device *dev)
426 BUS_INIT_FUNC_TRACE();
429 DPAA_BUS_DEBUG("Invalid drv or dev received.");
433 if (drv->drv_type == dev->device_type) {
434 DPAA_BUS_INFO("Device: %s matches for driver: %s",
435 dev->name, drv->driver.name);
436 ret = 0; /* Found a match */
443 rte_dpaa_bus_probe(void)
446 struct rte_dpaa_device *dev;
447 struct rte_dpaa_driver *drv;
449 BUS_INIT_FUNC_TRACE();
451 /* For each registered driver, and device, call the driver->probe */
452 TAILQ_FOREACH(dev, &rte_dpaa_bus.device_list, next) {
453 TAILQ_FOREACH(drv, &rte_dpaa_bus.driver_list, next) {
454 ret = rte_dpaa_device_match(drv, dev);
461 ret = drv->probe(drv, dev);
463 DPAA_BUS_ERR("Unable to probe.\n");
470 static struct rte_device *
471 rte_dpaa_find_device(const struct rte_device *start, rte_dev_cmp_t cmp,
474 struct rte_dpaa_device *dev;
476 TAILQ_FOREACH(dev, &rte_dpaa_bus.device_list, next) {
477 if (start && &dev->device == start) {
478 start = NULL; /* starting point found */
482 if (cmp(&dev->device, data) == 0)
489 struct rte_dpaa_bus rte_dpaa_bus = {
491 .scan = rte_dpaa_bus_scan,
492 .probe = rte_dpaa_bus_probe,
493 .find_device = rte_dpaa_find_device,
495 .device_list = TAILQ_HEAD_INITIALIZER(rte_dpaa_bus.device_list),
496 .driver_list = TAILQ_HEAD_INITIALIZER(rte_dpaa_bus.driver_list),
500 RTE_REGISTER_BUS(FSL_DPAA_BUS_NAME, rte_dpaa_bus.bus);
502 RTE_INIT(dpaa_init_log);
506 dpaa_logtype_bus = rte_log_register("bus.dpaa");
507 if (dpaa_logtype_bus >= 0)
508 rte_log_set_level(dpaa_logtype_bus, RTE_LOG_NOTICE);
510 dpaa_logtype_mempool = rte_log_register("mempool.dpaa");
511 if (dpaa_logtype_mempool >= 0)
512 rte_log_set_level(dpaa_logtype_mempool, RTE_LOG_NOTICE);
514 dpaa_logtype_pmd = rte_log_register("pmd.dpaa");
515 if (dpaa_logtype_pmd >= 0)
516 rte_log_set_level(dpaa_logtype_pmd, RTE_LOG_NOTICE);