event/dpaa2: replace static with dynamic logging
[dpdk.git] / drivers / bus / dpaa / dpaa_bus.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  *
3  *   Copyright 2017 NXP
4  *
5  */
6 /* System headers */
7 #include <stdio.h>
8 #include <inttypes.h>
9 #include <unistd.h>
10 #include <limits.h>
11 #include <sched.h>
12 #include <signal.h>
13 #include <pthread.h>
14 #include <sys/types.h>
15 #include <sys/syscall.h>
16
17 #include <rte_byteorder.h>
18 #include <rte_common.h>
19 #include <rte_interrupts.h>
20 #include <rte_log.h>
21 #include <rte_debug.h>
22 #include <rte_pci.h>
23 #include <rte_atomic.h>
24 #include <rte_branch_prediction.h>
25 #include <rte_memory.h>
26 #include <rte_tailq.h>
27 #include <rte_eal.h>
28 #include <rte_alarm.h>
29 #include <rte_ether.h>
30 #include <rte_ethdev.h>
31 #include <rte_malloc.h>
32 #include <rte_ring.h>
33 #include <rte_bus.h>
34
35 #include <rte_dpaa_bus.h>
36 #include <rte_dpaa_logs.h>
37
38 #include <fsl_usd.h>
39 #include <fsl_qman.h>
40 #include <fsl_bman.h>
41 #include <of.h>
42 #include <netcfg.h>
43
44 int dpaa_logtype_bus;
45 int dpaa_logtype_mempool;
46 int dpaa_logtype_pmd;
47
48 struct rte_dpaa_bus rte_dpaa_bus;
49 struct netcfg_info *dpaa_netcfg;
50
51 /* define a variable to hold the portal_key, once created.*/
52 pthread_key_t dpaa_portal_key;
53
54 unsigned int dpaa_svr_family;
55
56 RTE_DEFINE_PER_LCORE(bool, _dpaa_io);
57
58 static inline void
59 dpaa_add_to_device_list(struct rte_dpaa_device *dev)
60 {
61         TAILQ_INSERT_TAIL(&rte_dpaa_bus.device_list, dev, next);
62 }
63
64 static inline void
65 dpaa_remove_from_device_list(struct rte_dpaa_device *dev)
66 {
67         TAILQ_INSERT_TAIL(&rte_dpaa_bus.device_list, dev, next);
68 }
69
70 /*
71  * Reads the SEC device from DTS
72  * Returns -1 if SEC devices not available, 0 otherwise
73  */
74 static inline int
75 dpaa_sec_available(void)
76 {
77         const struct device_node *caam_node;
78
79         for_each_compatible_node(caam_node, NULL, "fsl,sec-v4.0") {
80                 return 0;
81         }
82
83         return -1;
84 }
85
86 static void dpaa_clean_device_list(void);
87
88 static int
89 dpaa_create_device_list(void)
90 {
91         int i;
92         int ret;
93         struct rte_dpaa_device *dev;
94         struct fm_eth_port_cfg *cfg;
95         struct fman_if *fman_intf;
96
97         /* Creating Ethernet Devices */
98         for (i = 0; i < dpaa_netcfg->num_ethports; i++) {
99                 dev = calloc(1, sizeof(struct rte_dpaa_device));
100                 if (!dev) {
101                         DPAA_BUS_LOG(ERR, "Failed to allocate ETH devices");
102                         ret = -ENOMEM;
103                         goto cleanup;
104                 }
105
106                 cfg = &dpaa_netcfg->port_cfg[i];
107                 fman_intf = cfg->fman_if;
108
109                 /* Device identifiers */
110                 dev->id.fman_id = fman_intf->fman_idx + 1;
111                 dev->id.mac_id = fman_intf->mac_idx;
112                 dev->device_type = FSL_DPAA_ETH;
113                 dev->id.dev_id = i;
114
115                 /* Create device name */
116                 memset(dev->name, 0, RTE_ETH_NAME_MAX_LEN);
117                 sprintf(dev->name, "fm%d-mac%d", (fman_intf->fman_idx + 1),
118                         fman_intf->mac_idx);
119                 DPAA_BUS_LOG(DEBUG, "Device added: %s", dev->name);
120                 dev->device.name = dev->name;
121
122                 dpaa_add_to_device_list(dev);
123         }
124
125         rte_dpaa_bus.device_count = i;
126
127         /* Unlike case of ETH, RTE_LIBRTE_DPAA_MAX_CRYPTODEV SEC devices are
128          * constantly created only if "sec" property is found in the device
129          * tree. Logically there is no limit for number of devices (QI
130          * interfaces) that can be created.
131          */
132
133         if (dpaa_sec_available()) {
134                 DPAA_BUS_LOG(INFO, "DPAA SEC devices are not available");
135                 return 0;
136         }
137
138         /* Creating SEC Devices */
139         for (i = 0; i < RTE_LIBRTE_DPAA_MAX_CRYPTODEV; i++) {
140                 dev = calloc(1, sizeof(struct rte_dpaa_device));
141                 if (!dev) {
142                         DPAA_BUS_LOG(ERR, "Failed to allocate SEC devices");
143                         ret = -1;
144                         goto cleanup;
145                 }
146
147                 dev->device_type = FSL_DPAA_CRYPTO;
148                 dev->id.dev_id = rte_dpaa_bus.device_count + i;
149
150                 /* Even though RTE_CRYPTODEV_NAME_MAX_LEN is valid length of
151                  * crypto PMD, using RTE_ETH_NAME_MAX_LEN as that is the size
152                  * allocated for dev->name/
153                  */
154                 memset(dev->name, 0, RTE_ETH_NAME_MAX_LEN);
155                 sprintf(dev->name, "dpaa-sec%d", i);
156                 DPAA_BUS_LOG(DEBUG, "Device added: %s", dev->name);
157
158                 dpaa_add_to_device_list(dev);
159         }
160
161         rte_dpaa_bus.device_count += i;
162
163         return 0;
164
165 cleanup:
166         dpaa_clean_device_list();
167         return ret;
168 }
169
170 static void
171 dpaa_clean_device_list(void)
172 {
173         struct rte_dpaa_device *dev = NULL;
174         struct rte_dpaa_device *tdev = NULL;
175
176         TAILQ_FOREACH_SAFE(dev, &rte_dpaa_bus.device_list, next, tdev) {
177                 TAILQ_REMOVE(&rte_dpaa_bus.device_list, dev, next);
178                 free(dev);
179                 dev = NULL;
180         }
181 }
182
183 /** XXX move this function into a separate file */
184 static int
185 _dpaa_portal_init(void *arg)
186 {
187         cpu_set_t cpuset;
188         pthread_t id;
189         uint32_t cpu = rte_lcore_id();
190         int ret;
191         struct dpaa_portal *dpaa_io_portal;
192
193         BUS_INIT_FUNC_TRACE();
194
195         if ((uint64_t)arg == 1 || cpu == LCORE_ID_ANY)
196                 cpu = rte_get_master_lcore();
197         /* if the core id is not supported */
198         else
199                 if (cpu >= RTE_MAX_LCORE)
200                         return -1;
201
202         /* Set CPU affinity for this thread */
203         CPU_ZERO(&cpuset);
204         CPU_SET(cpu, &cpuset);
205         id = pthread_self();
206         ret = pthread_setaffinity_np(id, sizeof(cpu_set_t), &cpuset);
207         if (ret) {
208                 DPAA_BUS_LOG(ERR, "pthread_setaffinity_np failed on "
209                         "core :%d with ret: %d", cpu, ret);
210                 return ret;
211         }
212
213         /* Initialise bman thread portals */
214         ret = bman_thread_init();
215         if (ret) {
216                 DPAA_BUS_LOG(ERR, "bman_thread_init failed on "
217                         "core %d with ret: %d", cpu, ret);
218                 return ret;
219         }
220
221         DPAA_BUS_LOG(DEBUG, "BMAN thread initialized");
222
223         /* Initialise qman thread portals */
224         ret = qman_thread_init();
225         if (ret) {
226                 DPAA_BUS_LOG(ERR, "bman_thread_init failed on "
227                         "core %d with ret: %d", cpu, ret);
228                 bman_thread_finish();
229                 return ret;
230         }
231
232         DPAA_BUS_LOG(DEBUG, "QMAN thread initialized");
233
234         dpaa_io_portal = rte_malloc(NULL, sizeof(struct dpaa_portal),
235                                     RTE_CACHE_LINE_SIZE);
236         if (!dpaa_io_portal) {
237                 DPAA_BUS_LOG(ERR, "Unable to allocate memory");
238                 bman_thread_finish();
239                 qman_thread_finish();
240                 return -ENOMEM;
241         }
242
243         dpaa_io_portal->qman_idx = qman_get_portal_index();
244         dpaa_io_portal->bman_idx = bman_get_portal_index();
245         dpaa_io_portal->tid = syscall(SYS_gettid);
246
247         ret = pthread_setspecific(dpaa_portal_key, (void *)dpaa_io_portal);
248         if (ret) {
249                 DPAA_BUS_LOG(ERR, "pthread_setspecific failed on "
250                             "core %d with ret: %d", cpu, ret);
251                 dpaa_portal_finish(NULL);
252
253                 return ret;
254         }
255
256         RTE_PER_LCORE(_dpaa_io) = true;
257
258         DPAA_BUS_LOG(DEBUG, "QMAN thread initialized");
259
260         return 0;
261 }
262
263 /*
264  * rte_dpaa_portal_init - Wrapper over _dpaa_portal_init with thread level check
265  * XXX Complete this
266  */
267 int rte_dpaa_portal_init(void *arg)
268 {
269         if (unlikely(!RTE_PER_LCORE(_dpaa_io)))
270                 return _dpaa_portal_init(arg);
271
272         return 0;
273 }
274
275 int
276 rte_dpaa_portal_fq_init(void *arg, struct qman_fq *fq)
277 {
278         /* Affine above created portal with channel*/
279         u32 sdqcr;
280         struct qman_portal *qp;
281
282         if (unlikely(!RTE_PER_LCORE(_dpaa_io)))
283                 _dpaa_portal_init(arg);
284
285         /* Initialise qman specific portals */
286         qp = fsl_qman_portal_create();
287         if (!qp) {
288                 DPAA_BUS_LOG(ERR, "Unable to alloc fq portal");
289                 return -1;
290         }
291         fq->qp = qp;
292         sdqcr = QM_SDQCR_CHANNELS_POOL_CONV(fq->ch_id);
293         qman_static_dequeue_add(sdqcr, qp);
294
295         return 0;
296 }
297
298 int rte_dpaa_portal_fq_close(struct qman_fq *fq)
299 {
300         return fsl_qman_portal_destroy(fq->qp);
301 }
302
303 void
304 dpaa_portal_finish(void *arg)
305 {
306         struct dpaa_portal *dpaa_io_portal = (struct dpaa_portal *)arg;
307
308         if (!dpaa_io_portal) {
309                 DPAA_BUS_LOG(DEBUG, "Portal already cleaned");
310                 return;
311         }
312
313         bman_thread_finish();
314         qman_thread_finish();
315
316         pthread_setspecific(dpaa_portal_key, NULL);
317
318         rte_free(dpaa_io_portal);
319         dpaa_io_portal = NULL;
320
321         RTE_PER_LCORE(_dpaa_io) = false;
322 }
323
324 #define DPAA_DEV_PATH1 "/sys/devices/platform/soc/soc:fsl,dpaa"
325 #define DPAA_DEV_PATH2 "/sys/devices/platform/fsl,dpaa"
326
327 static int
328 rte_dpaa_bus_scan(void)
329 {
330         int ret;
331
332         BUS_INIT_FUNC_TRACE();
333
334         if ((access(DPAA_DEV_PATH1, F_OK) != 0) &&
335             (access(DPAA_DEV_PATH2, F_OK) != 0)) {
336                 RTE_LOG(DEBUG, EAL, "DPAA Bus not present. Skipping.\n");
337                 return 0;
338         }
339
340         /* Load the device-tree driver */
341         ret = of_init();
342         if (ret) {
343                 DPAA_BUS_LOG(ERR, "of_init failed with ret: %d", ret);
344                 return -1;
345         }
346
347         /* Get the interface configurations from device-tree */
348         dpaa_netcfg = netcfg_acquire();
349         if (!dpaa_netcfg) {
350                 DPAA_BUS_LOG(ERR, "netcfg_acquire failed");
351                 return -EINVAL;
352         }
353
354         RTE_LOG(NOTICE, EAL, "DPAA Bus Detected\n");
355
356         if (!dpaa_netcfg->num_ethports) {
357                 DPAA_BUS_LOG(INFO, "no network interfaces available");
358                 /* This is not an error */
359                 return 0;
360         }
361
362         DPAA_BUS_LOG(DEBUG, "Bus: Address of netcfg=%p, Ethports=%d",
363                      dpaa_netcfg, dpaa_netcfg->num_ethports);
364
365 #ifdef RTE_LIBRTE_DPAA_DEBUG_DRIVER
366         dump_netcfg(dpaa_netcfg);
367 #endif
368
369         DPAA_BUS_LOG(DEBUG, "Number of devices = %d\n",
370                      dpaa_netcfg->num_ethports);
371         ret = dpaa_create_device_list();
372         if (ret) {
373                 DPAA_BUS_LOG(ERR, "Unable to create device list. (%d)", ret);
374                 return ret;
375         }
376
377         /* create the key, supplying a function that'll be invoked
378          * when a portal affined thread will be deleted.
379          */
380         ret = pthread_key_create(&dpaa_portal_key, dpaa_portal_finish);
381         if (ret) {
382                 DPAA_BUS_LOG(DEBUG, "Unable to create pthread key. (%d)", ret);
383                 dpaa_clean_device_list();
384                 return ret;
385         }
386
387         DPAA_BUS_LOG(DEBUG, "dpaa_portal_key=%u, ret=%d\n",
388                     (unsigned int)dpaa_portal_key, ret);
389
390         return 0;
391 }
392
393 /* register a dpaa bus based dpaa driver */
394 void
395 rte_dpaa_driver_register(struct rte_dpaa_driver *driver)
396 {
397         RTE_VERIFY(driver);
398
399         BUS_INIT_FUNC_TRACE();
400
401         TAILQ_INSERT_TAIL(&rte_dpaa_bus.driver_list, driver, next);
402         /* Update Bus references */
403         driver->dpaa_bus = &rte_dpaa_bus;
404 }
405
406 /* un-register a dpaa bus based dpaa driver */
407 void
408 rte_dpaa_driver_unregister(struct rte_dpaa_driver *driver)
409 {
410         struct rte_dpaa_bus *dpaa_bus;
411
412         BUS_INIT_FUNC_TRACE();
413
414         dpaa_bus = driver->dpaa_bus;
415
416         TAILQ_REMOVE(&dpaa_bus->driver_list, driver, next);
417         /* Update Bus references */
418         driver->dpaa_bus = NULL;
419 }
420
421 static int
422 rte_dpaa_device_match(struct rte_dpaa_driver *drv,
423                       struct rte_dpaa_device *dev)
424 {
425         int ret = -1;
426
427         BUS_INIT_FUNC_TRACE();
428
429         if (!drv || !dev) {
430                 DPAA_BUS_DEBUG("Invalid drv or dev received.");
431                 return ret;
432         }
433
434         if (drv->drv_type == dev->device_type) {
435                 DPAA_BUS_INFO("Device: %s matches for driver: %s",
436                               dev->name, drv->driver.name);
437                 ret = 0; /* Found a match */
438         }
439
440         return ret;
441 }
442
443 static int
444 rte_dpaa_bus_probe(void)
445 {
446         int ret = -1;
447         struct rte_dpaa_device *dev;
448         struct rte_dpaa_driver *drv;
449         FILE *svr_file = NULL;
450         unsigned int svr_ver;
451
452         BUS_INIT_FUNC_TRACE();
453
454         /* For each registered driver, and device, call the driver->probe */
455         TAILQ_FOREACH(dev, &rte_dpaa_bus.device_list, next) {
456                 TAILQ_FOREACH(drv, &rte_dpaa_bus.driver_list, next) {
457                         ret = rte_dpaa_device_match(drv, dev);
458                         if (ret)
459                                 continue;
460
461                         if (!drv->probe)
462                                 continue;
463
464                         ret = drv->probe(drv, dev);
465                         if (ret)
466                                 DPAA_BUS_ERR("Unable to probe.\n");
467                         break;
468                 }
469         }
470
471         svr_file = fopen(DPAA_SOC_ID_FILE, "r");
472         if (svr_file) {
473                 if (fscanf(svr_file, "svr:%x", &svr_ver) > 0)
474                         dpaa_svr_family = svr_ver & SVR_MASK;
475                 fclose(svr_file);
476         }
477
478         return 0;
479 }
480
481 static struct rte_device *
482 rte_dpaa_find_device(const struct rte_device *start, rte_dev_cmp_t cmp,
483                      const void *data)
484 {
485         struct rte_dpaa_device *dev;
486
487         TAILQ_FOREACH(dev, &rte_dpaa_bus.device_list, next) {
488                 if (start && &dev->device == start) {
489                         start = NULL;  /* starting point found */
490                         continue;
491                 }
492
493                 if (cmp(&dev->device, data) == 0)
494                         return &dev->device;
495         }
496
497         return NULL;
498 }
499
500 /*
501  * Get iommu class of DPAA2 devices on the bus.
502  */
503 static enum rte_iova_mode
504 rte_dpaa_get_iommu_class(void)
505 {
506         return RTE_IOVA_PA;
507 }
508
509 struct rte_dpaa_bus rte_dpaa_bus = {
510         .bus = {
511                 .scan = rte_dpaa_bus_scan,
512                 .probe = rte_dpaa_bus_probe,
513                 .find_device = rte_dpaa_find_device,
514                 .get_iommu_class = rte_dpaa_get_iommu_class,
515         },
516         .device_list = TAILQ_HEAD_INITIALIZER(rte_dpaa_bus.device_list),
517         .driver_list = TAILQ_HEAD_INITIALIZER(rte_dpaa_bus.driver_list),
518         .device_count = 0,
519 };
520
521 RTE_REGISTER_BUS(FSL_DPAA_BUS_NAME, rte_dpaa_bus.bus);
522
523 RTE_INIT(dpaa_init_log);
524 static void
525 dpaa_init_log(void)
526 {
527         dpaa_logtype_bus = rte_log_register("bus.dpaa");
528         if (dpaa_logtype_bus >= 0)
529                 rte_log_set_level(dpaa_logtype_bus, RTE_LOG_NOTICE);
530
531         dpaa_logtype_mempool = rte_log_register("mempool.dpaa");
532         if (dpaa_logtype_mempool >= 0)
533                 rte_log_set_level(dpaa_logtype_mempool, RTE_LOG_NOTICE);
534
535         dpaa_logtype_pmd = rte_log_register("pmd.dpaa");
536         if (dpaa_logtype_pmd >= 0)
537                 rte_log_set_level(dpaa_logtype_pmd, RTE_LOG_NOTICE);
538 }