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