New upstream version 18.08
[deb_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_atomic.h>
23 #include <rte_branch_prediction.h>
24 #include <rte_memory.h>
25 #include <rte_tailq.h>
26 #include <rte_eal.h>
27 #include <rte_alarm.h>
28 #include <rte_ether.h>
29 #include <rte_ethdev_driver.h>
30 #include <rte_malloc.h>
31 #include <rte_ring.h>
32 #include <rte_bus.h>
33 #include <rte_mbuf_pool_ops.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 int dpaa_logtype_eventdev;
48
49 struct rte_dpaa_bus rte_dpaa_bus;
50 struct netcfg_info *dpaa_netcfg;
51
52 /* define a variable to hold the portal_key, once created.*/
53 static pthread_key_t dpaa_portal_key;
54
55 unsigned int dpaa_svr_family;
56
57 #define FSL_DPAA_BUS_NAME       dpaa_bus
58
59 RTE_DEFINE_PER_LCORE(bool, dpaa_io);
60 RTE_DEFINE_PER_LCORE(struct dpaa_portal_dqrr, held_bufs);
61
62 static int
63 compare_dpaa_devices(struct rte_dpaa_device *dev1,
64                      struct rte_dpaa_device *dev2)
65 {
66         int comp = 0;
67
68         /* Segragating ETH from SEC devices */
69         if (dev1->device_type > dev2->device_type)
70                 comp = 1;
71         else if (dev1->device_type < dev2->device_type)
72                 comp = -1;
73         else
74                 comp = 0;
75
76         if ((comp != 0) || (dev1->device_type != FSL_DPAA_ETH))
77                 return comp;
78
79         if (dev1->id.fman_id > dev2->id.fman_id) {
80                 comp = 1;
81         } else if (dev1->id.fman_id < dev2->id.fman_id) {
82                 comp = -1;
83         } else {
84                 /* FMAN ids match, check for mac_id */
85                 if (dev1->id.mac_id > dev2->id.mac_id)
86                         comp = 1;
87                 else if (dev1->id.mac_id < dev2->id.mac_id)
88                         comp = -1;
89                 else
90                         comp = 0;
91         }
92
93         return comp;
94 }
95
96 static inline void
97 dpaa_add_to_device_list(struct rte_dpaa_device *newdev)
98 {
99         int comp, inserted = 0;
100         struct rte_dpaa_device *dev = NULL;
101         struct rte_dpaa_device *tdev = NULL;
102
103         TAILQ_FOREACH_SAFE(dev, &rte_dpaa_bus.device_list, next, tdev) {
104                 comp = compare_dpaa_devices(newdev, dev);
105                 if (comp < 0) {
106                         TAILQ_INSERT_BEFORE(dev, newdev, next);
107                         inserted = 1;
108                         break;
109                 }
110         }
111
112         if (!inserted)
113                 TAILQ_INSERT_TAIL(&rte_dpaa_bus.device_list, newdev, next);
114 }
115
116 /*
117  * Reads the SEC device from DTS
118  * Returns -1 if SEC devices not available, 0 otherwise
119  */
120 static inline int
121 dpaa_sec_available(void)
122 {
123         const struct device_node *caam_node;
124
125         for_each_compatible_node(caam_node, NULL, "fsl,sec-v4.0") {
126                 return 0;
127         }
128
129         return -1;
130 }
131
132 static void dpaa_clean_device_list(void);
133
134 static struct rte_devargs *
135 dpaa_devargs_lookup(struct rte_dpaa_device *dev)
136 {
137         struct rte_devargs *devargs;
138         char dev_name[32];
139
140         RTE_EAL_DEVARGS_FOREACH("dpaa_bus", devargs) {
141                 devargs->bus->parse(devargs->name, &dev_name);
142                 if (strcmp(dev_name, dev->device.name) == 0) {
143                         DPAA_BUS_INFO("**Devargs matched %s", dev_name);
144                         return devargs;
145                 }
146         }
147         return NULL;
148 }
149
150 static int
151 dpaa_create_device_list(void)
152 {
153         int i;
154         int ret;
155         struct rte_dpaa_device *dev;
156         struct fm_eth_port_cfg *cfg;
157         struct fman_if *fman_intf;
158
159         /* Creating Ethernet Devices */
160         for (i = 0; i < dpaa_netcfg->num_ethports; i++) {
161                 dev = calloc(1, sizeof(struct rte_dpaa_device));
162                 if (!dev) {
163                         DPAA_BUS_LOG(ERR, "Failed to allocate ETH devices");
164                         ret = -ENOMEM;
165                         goto cleanup;
166                 }
167
168                 cfg = &dpaa_netcfg->port_cfg[i];
169                 fman_intf = cfg->fman_if;
170
171                 /* Device identifiers */
172                 dev->id.fman_id = fman_intf->fman_idx + 1;
173                 dev->id.mac_id = fman_intf->mac_idx;
174                 dev->device_type = FSL_DPAA_ETH;
175                 dev->id.dev_id = i;
176
177                 /* Create device name */
178                 memset(dev->name, 0, RTE_ETH_NAME_MAX_LEN);
179                 sprintf(dev->name, "fm%d-mac%d", (fman_intf->fman_idx + 1),
180                         fman_intf->mac_idx);
181                 DPAA_BUS_LOG(INFO, "%s netdev added", dev->name);
182                 dev->device.name = dev->name;
183                 dev->device.devargs = dpaa_devargs_lookup(dev);
184
185                 dpaa_add_to_device_list(dev);
186         }
187
188         rte_dpaa_bus.device_count = i;
189
190         /* Unlike case of ETH, RTE_LIBRTE_DPAA_MAX_CRYPTODEV SEC devices are
191          * constantly created only if "sec" property is found in the device
192          * tree. Logically there is no limit for number of devices (QI
193          * interfaces) that can be created.
194          */
195
196         if (dpaa_sec_available()) {
197                 DPAA_BUS_LOG(INFO, "DPAA SEC devices are not available");
198                 return 0;
199         }
200
201         /* Creating SEC Devices */
202         for (i = 0; i < RTE_LIBRTE_DPAA_MAX_CRYPTODEV; i++) {
203                 dev = calloc(1, sizeof(struct rte_dpaa_device));
204                 if (!dev) {
205                         DPAA_BUS_LOG(ERR, "Failed to allocate SEC devices");
206                         ret = -1;
207                         goto cleanup;
208                 }
209
210                 dev->device_type = FSL_DPAA_CRYPTO;
211                 dev->id.dev_id = rte_dpaa_bus.device_count + i;
212
213                 /* Even though RTE_CRYPTODEV_NAME_MAX_LEN is valid length of
214                  * crypto PMD, using RTE_ETH_NAME_MAX_LEN as that is the size
215                  * allocated for dev->name/
216                  */
217                 memset(dev->name, 0, RTE_ETH_NAME_MAX_LEN);
218                 sprintf(dev->name, "dpaa-sec%d", i);
219                 DPAA_BUS_LOG(INFO, "%s cryptodev added", dev->name);
220                 dev->device.name = dev->name;
221                 dev->device.devargs = dpaa_devargs_lookup(dev);
222
223                 dpaa_add_to_device_list(dev);
224         }
225
226         rte_dpaa_bus.device_count += i;
227
228         return 0;
229
230 cleanup:
231         dpaa_clean_device_list();
232         return ret;
233 }
234
235 static void
236 dpaa_clean_device_list(void)
237 {
238         struct rte_dpaa_device *dev = NULL;
239         struct rte_dpaa_device *tdev = NULL;
240
241         TAILQ_FOREACH_SAFE(dev, &rte_dpaa_bus.device_list, next, tdev) {
242                 TAILQ_REMOVE(&rte_dpaa_bus.device_list, dev, next);
243                 free(dev);
244                 dev = NULL;
245         }
246 }
247
248 int rte_dpaa_portal_init(void *arg)
249 {
250         cpu_set_t cpuset;
251         pthread_t id;
252         uint32_t cpu = rte_lcore_id();
253         int ret;
254         struct dpaa_portal *dpaa_io_portal;
255
256         BUS_INIT_FUNC_TRACE();
257
258         if ((size_t)arg == 1 || cpu == LCORE_ID_ANY)
259                 cpu = rte_get_master_lcore();
260         /* if the core id is not supported */
261         else
262                 if (cpu >= RTE_MAX_LCORE)
263                         return -1;
264
265         /* Set CPU affinity for this thread */
266         CPU_ZERO(&cpuset);
267         CPU_SET(cpu, &cpuset);
268         id = pthread_self();
269         ret = pthread_setaffinity_np(id, sizeof(cpu_set_t), &cpuset);
270         if (ret) {
271                 DPAA_BUS_LOG(ERR, "pthread_setaffinity_np failed on "
272                         "core :%d with ret: %d", cpu, ret);
273                 return ret;
274         }
275
276         /* Initialise bman thread portals */
277         ret = bman_thread_init();
278         if (ret) {
279                 DPAA_BUS_LOG(ERR, "bman_thread_init failed on "
280                         "core %d with ret: %d", cpu, ret);
281                 return ret;
282         }
283
284         DPAA_BUS_LOG(DEBUG, "BMAN thread initialized");
285
286         /* Initialise qman thread portals */
287         ret = qman_thread_init();
288         if (ret) {
289                 DPAA_BUS_LOG(ERR, "bman_thread_init failed on "
290                         "core %d with ret: %d", cpu, ret);
291                 bman_thread_finish();
292                 return ret;
293         }
294
295         DPAA_BUS_LOG(DEBUG, "QMAN thread initialized");
296
297         dpaa_io_portal = rte_malloc(NULL, sizeof(struct dpaa_portal),
298                                     RTE_CACHE_LINE_SIZE);
299         if (!dpaa_io_portal) {
300                 DPAA_BUS_LOG(ERR, "Unable to allocate memory");
301                 bman_thread_finish();
302                 qman_thread_finish();
303                 return -ENOMEM;
304         }
305
306         dpaa_io_portal->qman_idx = qman_get_portal_index();
307         dpaa_io_portal->bman_idx = bman_get_portal_index();
308         dpaa_io_portal->tid = syscall(SYS_gettid);
309
310         ret = pthread_setspecific(dpaa_portal_key, (void *)dpaa_io_portal);
311         if (ret) {
312                 DPAA_BUS_LOG(ERR, "pthread_setspecific failed on "
313                             "core %d with ret: %d", cpu, ret);
314                 dpaa_portal_finish(NULL);
315
316                 return ret;
317         }
318
319         RTE_PER_LCORE(dpaa_io) = true;
320
321         DPAA_BUS_LOG(DEBUG, "QMAN thread initialized");
322
323         return 0;
324 }
325
326 int
327 rte_dpaa_portal_fq_init(void *arg, struct qman_fq *fq)
328 {
329         /* Affine above created portal with channel*/
330         u32 sdqcr;
331         struct qman_portal *qp;
332         int ret;
333
334         if (unlikely(!RTE_PER_LCORE(dpaa_io))) {
335                 ret = rte_dpaa_portal_init(arg);
336                 if (ret < 0) {
337                         DPAA_BUS_LOG(ERR, "portal initialization failure");
338                         return ret;
339                 }
340         }
341
342         /* Initialise qman specific portals */
343         qp = fsl_qman_portal_create();
344         if (!qp) {
345                 DPAA_BUS_LOG(ERR, "Unable to alloc fq portal");
346                 return -1;
347         }
348         fq->qp = qp;
349         sdqcr = QM_SDQCR_CHANNELS_POOL_CONV(fq->ch_id);
350         qman_static_dequeue_add(sdqcr, qp);
351
352         return 0;
353 }
354
355 int rte_dpaa_portal_fq_close(struct qman_fq *fq)
356 {
357         return fsl_qman_portal_destroy(fq->qp);
358 }
359
360 void
361 dpaa_portal_finish(void *arg)
362 {
363         struct dpaa_portal *dpaa_io_portal = (struct dpaa_portal *)arg;
364
365         if (!dpaa_io_portal) {
366                 DPAA_BUS_LOG(DEBUG, "Portal already cleaned");
367                 return;
368         }
369
370         bman_thread_finish();
371         qman_thread_finish();
372
373         pthread_setspecific(dpaa_portal_key, NULL);
374
375         rte_free(dpaa_io_portal);
376         dpaa_io_portal = NULL;
377
378         RTE_PER_LCORE(dpaa_io) = false;
379 }
380
381 static int
382 rte_dpaa_bus_parse(const char *name, void *out_name)
383 {
384         int i, j;
385         int max_fman = 2, max_macs = 16;
386         char *sep = strchr(name, ':');
387
388         if (strncmp(name, RTE_STR(FSL_DPAA_BUS_NAME),
389                 strlen(RTE_STR(FSL_DPAA_BUS_NAME)))) {
390                 return -EINVAL;
391         }
392
393         if (!sep) {
394                 DPAA_BUS_ERR("Incorrect device name observed");
395                 return -EINVAL;
396         }
397
398         sep = (char *) (sep + 1);
399
400         for (i = 0; i < max_fman; i++) {
401                 for (j = 0; j < max_macs; j++) {
402                         char fm_name[16];
403                         snprintf(fm_name, 16, "fm%d-mac%d", i, j);
404                         if (strcmp(fm_name, sep) == 0) {
405                                 if (out_name)
406                                         strcpy(out_name, sep);
407                                 return 0;
408                         }
409                 }
410         }
411
412         for (i = 0; i < RTE_LIBRTE_DPAA_MAX_CRYPTODEV; i++) {
413                 char sec_name[16];
414
415                 snprintf(sec_name, 16, "dpaa-sec%d", i);
416                 if (strcmp(sec_name, sep) == 0) {
417                         if (out_name)
418                                 strcpy(out_name, sep);
419                         return 0;
420                 }
421         }
422
423         return -EINVAL;
424 }
425
426 #define DPAA_DEV_PATH1 "/sys/devices/platform/soc/soc:fsl,dpaa"
427 #define DPAA_DEV_PATH2 "/sys/devices/platform/fsl,dpaa"
428
429 static int
430 rte_dpaa_bus_scan(void)
431 {
432         int ret;
433
434         BUS_INIT_FUNC_TRACE();
435
436         if ((access(DPAA_DEV_PATH1, F_OK) != 0) &&
437             (access(DPAA_DEV_PATH2, F_OK) != 0)) {
438                 RTE_LOG(DEBUG, EAL, "DPAA Bus not present. Skipping.\n");
439                 return 0;
440         }
441
442         /* Load the device-tree driver */
443         ret = of_init();
444         if (ret) {
445                 DPAA_BUS_LOG(ERR, "of_init failed with ret: %d", ret);
446                 return -1;
447         }
448
449         /* Get the interface configurations from device-tree */
450         dpaa_netcfg = netcfg_acquire();
451         if (!dpaa_netcfg) {
452                 DPAA_BUS_LOG(ERR, "netcfg_acquire failed");
453                 return -EINVAL;
454         }
455
456         RTE_LOG(NOTICE, EAL, "DPAA Bus Detected\n");
457
458         if (!dpaa_netcfg->num_ethports) {
459                 DPAA_BUS_LOG(INFO, "no network interfaces available");
460                 /* This is not an error */
461                 return 0;
462         }
463
464 #ifdef RTE_LIBRTE_DPAA_DEBUG_DRIVER
465         dump_netcfg(dpaa_netcfg);
466 #endif
467
468         DPAA_BUS_LOG(DEBUG, "Number of ethernet devices = %d",
469                      dpaa_netcfg->num_ethports);
470         ret = dpaa_create_device_list();
471         if (ret) {
472                 DPAA_BUS_LOG(ERR, "Unable to create device list. (%d)", ret);
473                 return ret;
474         }
475
476         /* create the key, supplying a function that'll be invoked
477          * when a portal affined thread will be deleted.
478          */
479         ret = pthread_key_create(&dpaa_portal_key, dpaa_portal_finish);
480         if (ret) {
481                 DPAA_BUS_LOG(DEBUG, "Unable to create pthread key. (%d)", ret);
482                 dpaa_clean_device_list();
483                 return ret;
484         }
485
486         return 0;
487 }
488
489 /* register a dpaa bus based dpaa driver */
490 void
491 rte_dpaa_driver_register(struct rte_dpaa_driver *driver)
492 {
493         RTE_VERIFY(driver);
494
495         BUS_INIT_FUNC_TRACE();
496
497         TAILQ_INSERT_TAIL(&rte_dpaa_bus.driver_list, driver, next);
498         /* Update Bus references */
499         driver->dpaa_bus = &rte_dpaa_bus;
500 }
501
502 /* un-register a dpaa bus based dpaa driver */
503 void
504 rte_dpaa_driver_unregister(struct rte_dpaa_driver *driver)
505 {
506         struct rte_dpaa_bus *dpaa_bus;
507
508         BUS_INIT_FUNC_TRACE();
509
510         dpaa_bus = driver->dpaa_bus;
511
512         TAILQ_REMOVE(&dpaa_bus->driver_list, driver, next);
513         /* Update Bus references */
514         driver->dpaa_bus = NULL;
515 }
516
517 static int
518 rte_dpaa_device_match(struct rte_dpaa_driver *drv,
519                       struct rte_dpaa_device *dev)
520 {
521         if (!drv || !dev) {
522                 DPAA_BUS_DEBUG("Invalid drv or dev received.");
523                 return -1;
524         }
525
526         if (drv->drv_type == dev->device_type)
527                 return 0;
528
529         return -1;
530 }
531
532 static int
533 rte_dpaa_bus_probe(void)
534 {
535         int ret = -1;
536         struct rte_dpaa_device *dev;
537         struct rte_dpaa_driver *drv;
538         FILE *svr_file = NULL;
539         unsigned int svr_ver;
540         int probe_all = rte_dpaa_bus.bus.conf.scan_mode != RTE_BUS_SCAN_WHITELIST;
541
542         svr_file = fopen(DPAA_SOC_ID_FILE, "r");
543         if (svr_file) {
544                 if (fscanf(svr_file, "svr:%x", &svr_ver) > 0)
545                         dpaa_svr_family = svr_ver & SVR_MASK;
546                 fclose(svr_file);
547         }
548
549         /* For each registered driver, and device, call the driver->probe */
550         TAILQ_FOREACH(dev, &rte_dpaa_bus.device_list, next) {
551                 TAILQ_FOREACH(drv, &rte_dpaa_bus.driver_list, next) {
552                         ret = rte_dpaa_device_match(drv, dev);
553                         if (ret)
554                                 continue;
555
556                         if (!drv->probe ||
557                             (dev->device.devargs &&
558                             dev->device.devargs->policy == RTE_DEV_BLACKLISTED))
559                                 continue;
560
561                         if (probe_all ||
562                             (dev->device.devargs &&
563                             dev->device.devargs->policy ==
564                             RTE_DEV_WHITELISTED)) {
565                                 ret = drv->probe(drv, dev);
566                                 if (ret)
567                                         DPAA_BUS_ERR("Unable to probe.\n");
568                         }
569                         break;
570                 }
571         }
572
573         /* Register DPAA mempool ops only if any DPAA device has
574          * been detected.
575          */
576         if (!TAILQ_EMPTY(&rte_dpaa_bus.device_list))
577                 rte_mbuf_set_platform_mempool_ops(DPAA_MEMPOOL_OPS_NAME);
578
579         return 0;
580 }
581
582 static struct rte_device *
583 rte_dpaa_find_device(const struct rte_device *start, rte_dev_cmp_t cmp,
584                      const void *data)
585 {
586         struct rte_dpaa_device *dev;
587
588         TAILQ_FOREACH(dev, &rte_dpaa_bus.device_list, next) {
589                 if (start && &dev->device == start) {
590                         start = NULL;  /* starting point found */
591                         continue;
592                 }
593
594                 if (cmp(&dev->device, data) == 0)
595                         return &dev->device;
596         }
597
598         return NULL;
599 }
600
601 /*
602  * Get iommu class of DPAA2 devices on the bus.
603  */
604 static enum rte_iova_mode
605 rte_dpaa_get_iommu_class(void)
606 {
607         if ((access(DPAA_DEV_PATH1, F_OK) != 0) &&
608             (access(DPAA_DEV_PATH2, F_OK) != 0)) {
609                 return RTE_IOVA_DC;
610         }
611         return RTE_IOVA_PA;
612 }
613
614 struct rte_dpaa_bus rte_dpaa_bus = {
615         .bus = {
616                 .scan = rte_dpaa_bus_scan,
617                 .probe = rte_dpaa_bus_probe,
618                 .parse = rte_dpaa_bus_parse,
619                 .find_device = rte_dpaa_find_device,
620                 .get_iommu_class = rte_dpaa_get_iommu_class,
621         },
622         .device_list = TAILQ_HEAD_INITIALIZER(rte_dpaa_bus.device_list),
623         .driver_list = TAILQ_HEAD_INITIALIZER(rte_dpaa_bus.driver_list),
624         .device_count = 0,
625 };
626
627 RTE_REGISTER_BUS(FSL_DPAA_BUS_NAME, rte_dpaa_bus.bus);
628
629 RTE_INIT(dpaa_init_log)
630 {
631         dpaa_logtype_bus = rte_log_register("bus.dpaa");
632         if (dpaa_logtype_bus >= 0)
633                 rte_log_set_level(dpaa_logtype_bus, RTE_LOG_NOTICE);
634
635         dpaa_logtype_mempool = rte_log_register("mempool.dpaa");
636         if (dpaa_logtype_mempool >= 0)
637                 rte_log_set_level(dpaa_logtype_mempool, RTE_LOG_NOTICE);
638
639         dpaa_logtype_pmd = rte_log_register("pmd.net.dpaa");
640         if (dpaa_logtype_pmd >= 0)
641                 rte_log_set_level(dpaa_logtype_pmd, RTE_LOG_NOTICE);
642
643         dpaa_logtype_eventdev = rte_log_register("pmd.event.dpaa");
644         if (dpaa_logtype_eventdev >= 0)
645                 rte_log_set_level(dpaa_logtype_eventdev, RTE_LOG_NOTICE);
646 }