bus/dpaa: support device blacklisting
[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 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(DEBUG, "Device added: %s", 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(DEBUG, "Device added: %s", 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         DPAA_BUS_LOG(DEBUG, "Bus: Address of netcfg=%p, Ethports=%d",
465                      dpaa_netcfg, dpaa_netcfg->num_ethports);
466
467 #ifdef RTE_LIBRTE_DPAA_DEBUG_DRIVER
468         dump_netcfg(dpaa_netcfg);
469 #endif
470
471         DPAA_BUS_LOG(DEBUG, "Number of devices = %d\n",
472                      dpaa_netcfg->num_ethports);
473         ret = dpaa_create_device_list();
474         if (ret) {
475                 DPAA_BUS_LOG(ERR, "Unable to create device list. (%d)", ret);
476                 return ret;
477         }
478
479         /* create the key, supplying a function that'll be invoked
480          * when a portal affined thread will be deleted.
481          */
482         ret = pthread_key_create(&dpaa_portal_key, dpaa_portal_finish);
483         if (ret) {
484                 DPAA_BUS_LOG(DEBUG, "Unable to create pthread key. (%d)", ret);
485                 dpaa_clean_device_list();
486                 return ret;
487         }
488
489         DPAA_BUS_LOG(DEBUG, "dpaa_portal_key=%u, ret=%d\n",
490                     (unsigned int)dpaa_portal_key, ret);
491
492         return 0;
493 }
494
495 /* register a dpaa bus based dpaa driver */
496 void
497 rte_dpaa_driver_register(struct rte_dpaa_driver *driver)
498 {
499         RTE_VERIFY(driver);
500
501         BUS_INIT_FUNC_TRACE();
502
503         TAILQ_INSERT_TAIL(&rte_dpaa_bus.driver_list, driver, next);
504         /* Update Bus references */
505         driver->dpaa_bus = &rte_dpaa_bus;
506 }
507
508 /* un-register a dpaa bus based dpaa driver */
509 void
510 rte_dpaa_driver_unregister(struct rte_dpaa_driver *driver)
511 {
512         struct rte_dpaa_bus *dpaa_bus;
513
514         BUS_INIT_FUNC_TRACE();
515
516         dpaa_bus = driver->dpaa_bus;
517
518         TAILQ_REMOVE(&dpaa_bus->driver_list, driver, next);
519         /* Update Bus references */
520         driver->dpaa_bus = NULL;
521 }
522
523 static int
524 rte_dpaa_device_match(struct rte_dpaa_driver *drv,
525                       struct rte_dpaa_device *dev)
526 {
527         if (!drv || !dev) {
528                 DPAA_BUS_DEBUG("Invalid drv or dev received.");
529                 return -1;
530         }
531
532         if (drv->drv_type == dev->device_type)
533                 return 0;
534
535         return -1;
536 }
537
538 static int
539 rte_dpaa_bus_probe(void)
540 {
541         int ret = -1;
542         struct rte_dpaa_device *dev;
543         struct rte_dpaa_driver *drv;
544         FILE *svr_file = NULL;
545         unsigned int svr_ver;
546         int probe_all = rte_dpaa_bus.bus.conf.scan_mode != RTE_BUS_SCAN_WHITELIST;
547
548         /* For each registered driver, and device, call the driver->probe */
549         TAILQ_FOREACH(dev, &rte_dpaa_bus.device_list, next) {
550                 TAILQ_FOREACH(drv, &rte_dpaa_bus.driver_list, next) {
551                         ret = rte_dpaa_device_match(drv, dev);
552                         if (ret)
553                                 continue;
554
555                         if (!drv->probe ||
556                             (dev->device.devargs &&
557                             dev->device.devargs->policy == RTE_DEV_BLACKLISTED))
558                                 continue;
559
560                         if (probe_all ||
561                             (dev->device.devargs &&
562                             dev->device.devargs->policy ==
563                             RTE_DEV_WHITELISTED)) {
564                                 ret = drv->probe(drv, dev);
565                                 if (ret)
566                                         DPAA_BUS_ERR("Unable to probe.\n");
567                         }
568                         break;
569                 }
570         }
571
572         /* Register DPAA mempool ops only if any DPAA device has
573          * been detected.
574          */
575         if (!TAILQ_EMPTY(&rte_dpaa_bus.device_list))
576                 rte_mbuf_set_platform_mempool_ops(DPAA_MEMPOOL_OPS_NAME);
577
578         svr_file = fopen(DPAA_SOC_ID_FILE, "r");
579         if (svr_file) {
580                 if (fscanf(svr_file, "svr:%x", &svr_ver) > 0)
581                         dpaa_svr_family = svr_ver & SVR_MASK;
582                 fclose(svr_file);
583         }
584
585         return 0;
586 }
587
588 static struct rte_device *
589 rte_dpaa_find_device(const struct rte_device *start, rte_dev_cmp_t cmp,
590                      const void *data)
591 {
592         struct rte_dpaa_device *dev;
593
594         TAILQ_FOREACH(dev, &rte_dpaa_bus.device_list, next) {
595                 if (start && &dev->device == start) {
596                         start = NULL;  /* starting point found */
597                         continue;
598                 }
599
600                 if (cmp(&dev->device, data) == 0)
601                         return &dev->device;
602         }
603
604         return NULL;
605 }
606
607 /*
608  * Get iommu class of DPAA2 devices on the bus.
609  */
610 static enum rte_iova_mode
611 rte_dpaa_get_iommu_class(void)
612 {
613         if ((access(DPAA_DEV_PATH1, F_OK) != 0) &&
614             (access(DPAA_DEV_PATH2, F_OK) != 0)) {
615                 return RTE_IOVA_DC;
616         }
617         return RTE_IOVA_PA;
618 }
619
620 struct rte_dpaa_bus rte_dpaa_bus = {
621         .bus = {
622                 .scan = rte_dpaa_bus_scan,
623                 .probe = rte_dpaa_bus_probe,
624                 .parse = rte_dpaa_bus_parse,
625                 .find_device = rte_dpaa_find_device,
626                 .get_iommu_class = rte_dpaa_get_iommu_class,
627         },
628         .device_list = TAILQ_HEAD_INITIALIZER(rte_dpaa_bus.device_list),
629         .driver_list = TAILQ_HEAD_INITIALIZER(rte_dpaa_bus.driver_list),
630         .device_count = 0,
631 };
632
633 RTE_REGISTER_BUS(FSL_DPAA_BUS_NAME, rte_dpaa_bus.bus);
634
635 RTE_INIT(dpaa_init_log);
636 static void
637 dpaa_init_log(void)
638 {
639         dpaa_logtype_bus = rte_log_register("bus.dpaa");
640         if (dpaa_logtype_bus >= 0)
641                 rte_log_set_level(dpaa_logtype_bus, RTE_LOG_NOTICE);
642
643         dpaa_logtype_mempool = rte_log_register("mempool.dpaa");
644         if (dpaa_logtype_mempool >= 0)
645                 rte_log_set_level(dpaa_logtype_mempool, RTE_LOG_NOTICE);
646
647         dpaa_logtype_pmd = rte_log_register("pmd.dpaa");
648         if (dpaa_logtype_pmd >= 0)
649                 rte_log_set_level(dpaa_logtype_pmd, RTE_LOG_NOTICE);
650
651         dpaa_logtype_eventdev = rte_log_register("eventdev.dpaa");
652         if (dpaa_logtype_eventdev >= 0)
653                 rte_log_set_level(dpaa_logtype_eventdev, RTE_LOG_NOTICE);
654 }