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