bus/dpaa: scan for DPAA Crypto devices
[dpdk.git] / drivers / bus / dpaa / dpaa_bus.c
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright 2017 NXP.
5  *
6  *   Redistribution and use in source and binary forms, with or without
7  *   modification, are permitted provided that the following conditions
8  *   are met:
9  *
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
15  *       distribution.
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.
19  *
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.
31  */
32 /* System headers */
33 #include <stdio.h>
34 #include <inttypes.h>
35 #include <unistd.h>
36 #include <limits.h>
37 #include <sched.h>
38 #include <signal.h>
39 #include <pthread.h>
40 #include <sys/types.h>
41 #include <sys/syscall.h>
42
43 #include <rte_config.h>
44 #include <rte_byteorder.h>
45 #include <rte_common.h>
46 #include <rte_interrupts.h>
47 #include <rte_log.h>
48 #include <rte_debug.h>
49 #include <rte_pci.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>
55 #include <rte_eal.h>
56 #include <rte_alarm.h>
57 #include <rte_ether.h>
58 #include <rte_ethdev.h>
59 #include <rte_malloc.h>
60 #include <rte_ring.h>
61 #include <rte_bus.h>
62
63 #include <rte_dpaa_bus.h>
64 #include <rte_dpaa_logs.h>
65
66 #include <fsl_usd.h>
67 #include <fsl_qman.h>
68 #include <fsl_bman.h>
69 #include <of.h>
70 #include <netcfg.h>
71
72 int dpaa_logtype_bus;
73 int dpaa_logtype_mempool;
74 int dpaa_logtype_pmd;
75
76 struct rte_dpaa_bus rte_dpaa_bus;
77 struct netcfg_info *dpaa_netcfg;
78
79 /* define a variable to hold the portal_key, once created.*/
80 pthread_key_t dpaa_portal_key;
81
82 RTE_DEFINE_PER_LCORE(bool, _dpaa_io);
83
84 static inline void
85 dpaa_add_to_device_list(struct rte_dpaa_device *dev)
86 {
87         TAILQ_INSERT_TAIL(&rte_dpaa_bus.device_list, dev, next);
88 }
89
90 static inline void
91 dpaa_remove_from_device_list(struct rte_dpaa_device *dev)
92 {
93         TAILQ_INSERT_TAIL(&rte_dpaa_bus.device_list, dev, next);
94 }
95
96 /*
97  * Reads the SEC device from DTS
98  * Returns -1 if SEC devices not available, 0 otherwise
99  */
100 static inline int
101 dpaa_sec_available(void)
102 {
103         const struct device_node *caam_node;
104
105         for_each_compatible_node(caam_node, NULL, "fsl,sec-v4.0") {
106                 return 0;
107         }
108
109         return -1;
110 }
111
112 static void dpaa_clean_device_list(void);
113
114 static int
115 dpaa_create_device_list(void)
116 {
117         int i;
118         int ret;
119         struct rte_dpaa_device *dev;
120         struct fm_eth_port_cfg *cfg;
121         struct fman_if *fman_intf;
122
123         /* Creating Ethernet Devices */
124         for (i = 0; i < dpaa_netcfg->num_ethports; i++) {
125                 dev = calloc(1, sizeof(struct rte_dpaa_device));
126                 if (!dev) {
127                         DPAA_BUS_LOG(ERR, "Failed to allocate ETH devices");
128                         ret = -ENOMEM;
129                         goto cleanup;
130                 }
131
132                 cfg = &dpaa_netcfg->port_cfg[i];
133                 fman_intf = cfg->fman_if;
134
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;
139                 dev->id.dev_id = i;
140
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),
144                         fman_intf->mac_idx);
145                 DPAA_BUS_LOG(DEBUG, "Device added: %s", dev->name);
146                 dev->device.name = dev->name;
147
148                 dpaa_add_to_device_list(dev);
149         }
150
151         rte_dpaa_bus.device_count = i;
152
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.
157          */
158
159         if (dpaa_sec_available()) {
160                 DPAA_BUS_LOG(INFO, "DPAA SEC devices are not available");
161                 return 0;
162         }
163
164         /* Creating SEC Devices */
165         for (i = 0; i < RTE_LIBRTE_DPAA_MAX_CRYPTODEV; i++) {
166                 dev = calloc(1, sizeof(struct rte_dpaa_device));
167                 if (!dev) {
168                         DPAA_BUS_LOG(ERR, "Failed to allocate SEC devices");
169                         ret = -1;
170                         goto cleanup;
171                 }
172
173                 dev->device_type = FSL_DPAA_CRYPTO;
174                 dev->id.dev_id = rte_dpaa_bus.device_count + i;
175
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/
179                  */
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);
183
184                 dpaa_add_to_device_list(dev);
185         }
186
187         rte_dpaa_bus.device_count += i;
188
189         return 0;
190
191 cleanup:
192         dpaa_clean_device_list();
193         return ret;
194 }
195
196 static void
197 dpaa_clean_device_list(void)
198 {
199         struct rte_dpaa_device *dev = NULL;
200         struct rte_dpaa_device *tdev = NULL;
201
202         TAILQ_FOREACH_SAFE(dev, &rte_dpaa_bus.device_list, next, tdev) {
203                 TAILQ_REMOVE(&rte_dpaa_bus.device_list, dev, next);
204                 free(dev);
205                 dev = NULL;
206         }
207 }
208
209 /** XXX move this function into a separate file */
210 static int
211 _dpaa_portal_init(void *arg)
212 {
213         cpu_set_t cpuset;
214         pthread_t id;
215         uint32_t cpu = rte_lcore_id();
216         int ret;
217         struct dpaa_portal *dpaa_io_portal;
218
219         BUS_INIT_FUNC_TRACE();
220
221         if ((uint64_t)arg == 1 || cpu == LCORE_ID_ANY)
222                 cpu = rte_get_master_lcore();
223         /* if the core id is not supported */
224         else
225                 if (cpu >= RTE_MAX_LCORE)
226                         return -1;
227
228         /* Set CPU affinity for this thread */
229         CPU_ZERO(&cpuset);
230         CPU_SET(cpu, &cpuset);
231         id = pthread_self();
232         ret = pthread_setaffinity_np(id, sizeof(cpu_set_t), &cpuset);
233         if (ret) {
234                 DPAA_BUS_LOG(ERR, "pthread_setaffinity_np failed on "
235                         "core :%d with ret: %d", cpu, ret);
236                 return ret;
237         }
238
239         /* Initialise bman thread portals */
240         ret = bman_thread_init();
241         if (ret) {
242                 DPAA_BUS_LOG(ERR, "bman_thread_init failed on "
243                         "core %d with ret: %d", cpu, ret);
244                 return ret;
245         }
246
247         DPAA_BUS_LOG(DEBUG, "BMAN thread initialized");
248
249         /* Initialise qman thread portals */
250         ret = qman_thread_init();
251         if (ret) {
252                 DPAA_BUS_LOG(ERR, "bman_thread_init failed on "
253                         "core %d with ret: %d", cpu, ret);
254                 bman_thread_finish();
255                 return ret;
256         }
257
258         DPAA_BUS_LOG(DEBUG, "QMAN thread initialized");
259
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();
266                 return -ENOMEM;
267         }
268
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);
272
273         ret = pthread_setspecific(dpaa_portal_key, (void *)dpaa_io_portal);
274         if (ret) {
275                 DPAA_BUS_LOG(ERR, "pthread_setspecific failed on "
276                             "core %d with ret: %d", cpu, ret);
277                 dpaa_portal_finish(NULL);
278
279                 return ret;
280         }
281
282         RTE_PER_LCORE(_dpaa_io) = true;
283
284         DPAA_BUS_LOG(DEBUG, "QMAN thread initialized");
285
286         return 0;
287 }
288
289 /*
290  * rte_dpaa_portal_init - Wrapper over _dpaa_portal_init with thread level check
291  * XXX Complete this
292  */
293 int
294 rte_dpaa_portal_init(void *arg)
295 {
296         if (unlikely(!RTE_PER_LCORE(_dpaa_io)))
297                 return _dpaa_portal_init(arg);
298
299         return 0;
300 }
301
302 void
303 dpaa_portal_finish(void *arg)
304 {
305         struct dpaa_portal *dpaa_io_portal = (struct dpaa_portal *)arg;
306
307         if (!dpaa_io_portal) {
308                 DPAA_BUS_LOG(DEBUG, "Portal already cleaned");
309                 return;
310         }
311
312         bman_thread_finish();
313         qman_thread_finish();
314
315         pthread_setspecific(dpaa_portal_key, NULL);
316
317         rte_free(dpaa_io_portal);
318         dpaa_io_portal = NULL;
319
320         RTE_PER_LCORE(_dpaa_io) = false;
321 }
322
323 #define DPAA_DEV_PATH1 "/sys/devices/platform/soc/soc:fsl,dpaa"
324 #define DPAA_DEV_PATH2 "/sys/devices/platform/fsl,dpaa"
325
326 static int
327 rte_dpaa_bus_scan(void)
328 {
329         int ret;
330
331         BUS_INIT_FUNC_TRACE();
332
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");
336                 return 0;
337         }
338
339         /* Load the device-tree driver */
340         ret = of_init();
341         if (ret) {
342                 DPAA_BUS_LOG(ERR, "of_init failed with ret: %d", ret);
343                 return -1;
344         }
345
346         /* Get the interface configurations from device-tree */
347         dpaa_netcfg = netcfg_acquire();
348         if (!dpaa_netcfg) {
349                 DPAA_BUS_LOG(ERR, "netcfg_acquire failed");
350                 return -EINVAL;
351         }
352
353         RTE_LOG(NOTICE, EAL, "DPAA Bus Detected\n");
354
355         if (!dpaa_netcfg->num_ethports) {
356                 DPAA_BUS_LOG(INFO, "no network interfaces available");
357                 /* This is not an error */
358                 return 0;
359         }
360
361         DPAA_BUS_LOG(DEBUG, "Bus: Address of netcfg=%p, Ethports=%d",
362                      dpaa_netcfg, dpaa_netcfg->num_ethports);
363
364 #ifdef RTE_LIBRTE_DPAA_DEBUG_DRIVER
365         dump_netcfg(dpaa_netcfg);
366 #endif
367
368         DPAA_BUS_LOG(DEBUG, "Number of devices = %d\n",
369                      dpaa_netcfg->num_ethports);
370         ret = dpaa_create_device_list();
371         if (ret) {
372                 DPAA_BUS_LOG(ERR, "Unable to create device list. (%d)", ret);
373                 return ret;
374         }
375
376         /* create the key, supplying a function that'll be invoked
377          * when a portal affined thread will be deleted.
378          */
379         ret = pthread_key_create(&dpaa_portal_key, dpaa_portal_finish);
380         if (ret) {
381                 DPAA_BUS_LOG(DEBUG, "Unable to create pthread key. (%d)", ret);
382                 dpaa_clean_device_list();
383                 return ret;
384         }
385
386         DPAA_BUS_LOG(DEBUG, "dpaa_portal_key=%u, ret=%d\n",
387                     (unsigned int)dpaa_portal_key, ret);
388
389         return 0;
390 }
391
392 /* register a dpaa bus based dpaa driver */
393 void
394 rte_dpaa_driver_register(struct rte_dpaa_driver *driver)
395 {
396         RTE_VERIFY(driver);
397
398         BUS_INIT_FUNC_TRACE();
399
400         TAILQ_INSERT_TAIL(&rte_dpaa_bus.driver_list, driver, next);
401         /* Update Bus references */
402         driver->dpaa_bus = &rte_dpaa_bus;
403 }
404
405 /* un-register a dpaa bus based dpaa driver */
406 void
407 rte_dpaa_driver_unregister(struct rte_dpaa_driver *driver)
408 {
409         struct rte_dpaa_bus *dpaa_bus;
410
411         BUS_INIT_FUNC_TRACE();
412
413         dpaa_bus = driver->dpaa_bus;
414
415         TAILQ_REMOVE(&dpaa_bus->driver_list, driver, next);
416         /* Update Bus references */
417         driver->dpaa_bus = NULL;
418 }
419
420 static int
421 rte_dpaa_device_match(struct rte_dpaa_driver *drv,
422                       struct rte_dpaa_device *dev)
423 {
424         int ret = -1;
425
426         BUS_INIT_FUNC_TRACE();
427
428         if (!drv || !dev) {
429                 DPAA_BUS_DEBUG("Invalid drv or dev received.");
430                 return ret;
431         }
432
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 */
437         }
438
439         return ret;
440 }
441
442 static int
443 rte_dpaa_bus_probe(void)
444 {
445         int ret = -1;
446         struct rte_dpaa_device *dev;
447         struct rte_dpaa_driver *drv;
448
449         BUS_INIT_FUNC_TRACE();
450
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);
455                         if (ret)
456                                 continue;
457
458                         if (!drv->probe)
459                                 continue;
460
461                         ret = drv->probe(drv, dev);
462                         if (ret)
463                                 DPAA_BUS_ERR("Unable to probe.\n");
464                         break;
465                 }
466         }
467         return 0;
468 }
469
470 static struct rte_device *
471 rte_dpaa_find_device(const struct rte_device *start, rte_dev_cmp_t cmp,
472                      const void *data)
473 {
474         struct rte_dpaa_device *dev;
475
476         TAILQ_FOREACH(dev, &rte_dpaa_bus.device_list, next) {
477                 if (start && &dev->device == start) {
478                         start = NULL;  /* starting point found */
479                         continue;
480                 }
481
482                 if (cmp(&dev->device, data) == 0)
483                         return &dev->device;
484         }
485
486         return NULL;
487 }
488
489 struct rte_dpaa_bus rte_dpaa_bus = {
490         .bus = {
491                 .scan = rte_dpaa_bus_scan,
492                 .probe = rte_dpaa_bus_probe,
493                 .find_device = rte_dpaa_find_device,
494         },
495         .device_list = TAILQ_HEAD_INITIALIZER(rte_dpaa_bus.device_list),
496         .driver_list = TAILQ_HEAD_INITIALIZER(rte_dpaa_bus.driver_list),
497         .device_count = 0,
498 };
499
500 RTE_REGISTER_BUS(FSL_DPAA_BUS_NAME, rte_dpaa_bus.bus);
501
502 RTE_INIT(dpaa_init_log);
503 static void
504 dpaa_init_log(void)
505 {
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);
509
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);
513
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);
517 }