bus/fslmc: fix resource leak
[dpdk.git] / drivers / bus / fslmc / fslmc_bus.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  *
3  *   Copyright 2016,2018-2019 NXP
4  *
5  */
6
7 #include <string.h>
8 #include <dirent.h>
9 #include <stdbool.h>
10
11 #include <rte_log.h>
12 #include <rte_bus.h>
13 #include <rte_malloc.h>
14 #include <rte_devargs.h>
15 #include <rte_memcpy.h>
16 #include <rte_ethdev_driver.h>
17
18 #include <rte_fslmc.h>
19 #include <fslmc_vfio.h>
20 #include "fslmc_logs.h"
21
22 #include <dpaax_iova_table.h>
23
24 int dpaa2_logtype_bus;
25
26 #define VFIO_IOMMU_GROUP_PATH "/sys/kernel/iommu_groups"
27 #define FSLMC_BUS_NAME  fslmc
28
29 struct rte_fslmc_bus rte_fslmc_bus;
30 uint8_t dpaa2_virt_mode;
31
32 uint32_t
33 rte_fslmc_get_device_count(enum rte_dpaa2_dev_type device_type)
34 {
35         if (device_type >= DPAA2_DEVTYPE_MAX)
36                 return 0;
37         return rte_fslmc_bus.device_count[device_type];
38 }
39
40 RTE_DEFINE_PER_LCORE(struct dpaa2_portal_dqrr, dpaa2_held_bufs);
41
42 static void
43 cleanup_fslmc_device_list(void)
44 {
45         struct rte_dpaa2_device *dev;
46         struct rte_dpaa2_device *t_dev;
47
48         TAILQ_FOREACH_SAFE(dev, &rte_fslmc_bus.device_list, next, t_dev) {
49                 TAILQ_REMOVE(&rte_fslmc_bus.device_list, dev, next);
50                 free(dev);
51                 dev = NULL;
52         }
53 }
54
55 static int
56 compare_dpaa2_devname(struct rte_dpaa2_device *dev1,
57                       struct rte_dpaa2_device *dev2)
58 {
59         int comp;
60
61         if (dev1->dev_type > dev2->dev_type) {
62                 comp = 1;
63         } else if (dev1->dev_type < dev2->dev_type) {
64                 comp = -1;
65         } else {
66                 /* Check the ID as types match */
67                 if (dev1->object_id > dev2->object_id)
68                         comp = 1;
69                 else if (dev1->object_id < dev2->object_id)
70                         comp = -1;
71                 else
72                         comp = 0; /* Duplicate device name */
73         }
74
75         return comp;
76 }
77
78 static void
79 insert_in_device_list(struct rte_dpaa2_device *newdev)
80 {
81         int comp, inserted = 0;
82         struct rte_dpaa2_device *dev = NULL;
83         struct rte_dpaa2_device *tdev = NULL;
84
85         TAILQ_FOREACH_SAFE(dev, &rte_fslmc_bus.device_list, next, tdev) {
86                 comp = compare_dpaa2_devname(newdev, dev);
87                 if (comp < 0) {
88                         TAILQ_INSERT_BEFORE(dev, newdev, next);
89                         inserted = 1;
90                         break;
91                 }
92         }
93
94         if (!inserted)
95                 TAILQ_INSERT_TAIL(&rte_fslmc_bus.device_list, newdev, next);
96 }
97
98 static struct rte_devargs *
99 fslmc_devargs_lookup(struct rte_dpaa2_device *dev)
100 {
101         struct rte_devargs *devargs;
102         char dev_name[32];
103
104         RTE_EAL_DEVARGS_FOREACH("fslmc", devargs) {
105                 devargs->bus->parse(devargs->name, &dev_name);
106                 if (strcmp(dev_name, dev->device.name) == 0) {
107                         DPAA2_BUS_INFO("**Devargs matched %s", dev_name);
108                         return devargs;
109                 }
110         }
111         return NULL;
112 }
113
114 static void
115 dump_device_list(void)
116 {
117         struct rte_dpaa2_device *dev;
118         uint32_t global_log_level;
119         int local_log_level;
120
121         /* Only if the log level has been set to Debugging, print list */
122         global_log_level = rte_log_get_global_level();
123         local_log_level = rte_log_get_level(dpaa2_logtype_bus);
124         if (global_log_level == RTE_LOG_DEBUG ||
125             local_log_level == RTE_LOG_DEBUG) {
126                 DPAA2_BUS_LOG(DEBUG, "List of devices scanned on bus:");
127                 TAILQ_FOREACH(dev, &rte_fslmc_bus.device_list, next) {
128                         DPAA2_BUS_LOG(DEBUG, "\t\t%s", dev->device.name);
129                 }
130         }
131 }
132
133 static int
134 scan_one_fslmc_device(char *dev_name)
135 {
136         char *dup_dev_name, *t_ptr;
137         struct rte_dpaa2_device *dev = NULL;
138         int ret = -1;
139
140         if (!dev_name)
141                 return ret;
142
143         /* Ignore the Container name itself */
144         if (!strncmp("dprc", dev_name, 4))
145                 return 0;
146
147         /* Creating a temporary copy to perform cut-parse over string */
148         dup_dev_name = strdup(dev_name);
149         if (!dup_dev_name) {
150                 DPAA2_BUS_ERR("Unable to allocate device name memory");
151                 return -ENOMEM;
152         }
153
154         /* For all other devices, we allocate rte_dpaa2_device.
155          * For those devices where there is no driver, probe would release
156          * the memory associated with the rte_dpaa2_device after necessary
157          * initialization.
158          */
159         dev = calloc(1, sizeof(struct rte_dpaa2_device));
160         if (!dev) {
161                 DPAA2_BUS_ERR("Unable to allocate device object");
162                 free(dup_dev_name);
163                 return -ENOMEM;
164         }
165
166         dev->device.bus = &rte_fslmc_bus.bus;
167
168         /* Parse the device name and ID */
169         t_ptr = strtok(dup_dev_name, ".");
170         if (!t_ptr) {
171                 DPAA2_BUS_ERR("Invalid device found: (%s)", dup_dev_name);
172                 ret = -EINVAL;
173                 goto cleanup;
174         }
175         if (!strncmp("dpni", t_ptr, 4))
176                 dev->dev_type = DPAA2_ETH;
177         else if (!strncmp("dpseci", t_ptr, 6))
178                 dev->dev_type = DPAA2_CRYPTO;
179         else if (!strncmp("dpcon", t_ptr, 5))
180                 dev->dev_type = DPAA2_CON;
181         else if (!strncmp("dpbp", t_ptr, 4))
182                 dev->dev_type = DPAA2_BPOOL;
183         else if (!strncmp("dpio", t_ptr, 4))
184                 dev->dev_type = DPAA2_IO;
185         else if (!strncmp("dpci", t_ptr, 4))
186                 dev->dev_type = DPAA2_CI;
187         else if (!strncmp("dpmcp", t_ptr, 5))
188                 dev->dev_type = DPAA2_MPORTAL;
189         else if (!strncmp("dpdmai", t_ptr, 6))
190                 dev->dev_type = DPAA2_QDMA;
191         else if (!strncmp("dpdmux", t_ptr, 6))
192                 dev->dev_type = DPAA2_MUX;
193         else if (!strncmp("dprtc", t_ptr, 5))
194                 dev->dev_type = DPAA2_DPRTC;
195         else
196                 dev->dev_type = DPAA2_UNKNOWN;
197
198         t_ptr = strtok(NULL, ".");
199         if (!t_ptr) {
200                 DPAA2_BUS_ERR("Skipping invalid device (%s)", dup_dev_name);
201                 ret = 0;
202                 goto cleanup;
203         }
204
205         sscanf(t_ptr, "%hu", &dev->object_id);
206         dev->device.name = strdup(dev_name);
207         if (!dev->device.name) {
208                 DPAA2_BUS_ERR("Unable to clone device name. Out of memory");
209                 ret = -ENOMEM;
210                 goto cleanup;
211         }
212         dev->device.devargs = fslmc_devargs_lookup(dev);
213
214         /* Update the device found into the device_count table */
215         rte_fslmc_bus.device_count[dev->dev_type]++;
216
217         /* Add device in the fslmc device list */
218         insert_in_device_list(dev);
219
220         /* Don't need the duplicated device filesystem entry anymore */
221         if (dup_dev_name)
222                 free(dup_dev_name);
223
224         return 0;
225 cleanup:
226         if (dup_dev_name)
227                 free(dup_dev_name);
228         if (dev)
229                 free(dev);
230         return ret;
231 }
232
233 static int
234 rte_fslmc_parse(const char *name, void *addr)
235 {
236         uint16_t dev_id;
237         char *t_ptr;
238         char *sep = NULL;
239         uint8_t sep_exists = 0;
240
241         DPAA2_BUS_DEBUG("Parsing dev=(%s)", name);
242
243         /* There are multiple ways this can be called, with bus:dev, name=dev
244          * or just dev. In all cases, the 'addr' is actually a string.
245          */
246         sep = strchr(name, ':');
247         if (!sep) {
248                 /* check for '=' */
249                 sep = strchr(name, '=');
250                 if (!sep)
251                         sep_exists = 0;
252                 else
253                         sep_exists = 1;
254         } else
255                 sep_exists = 1;
256
257         /* Check if starting part if either of 'fslmc:' or 'name=', separator
258          * exists.
259          */
260         if (sep_exists) {
261                 /* If either of "fslmc" or "name" are starting part */
262                 if (!strncmp(name, RTE_STR(FSLMC_BUS_NAME),
263                              strlen(RTE_STR(FSLMC_BUS_NAME))) ||
264                    (!strncmp(name, "name", strlen("name")))) {
265                         goto jump_out;
266                 } else {
267                         DPAA2_BUS_DEBUG("Invalid device for matching (%s).",
268                                         name);
269                         goto err_out;
270                 }
271         } else
272                 sep = strdup(name);
273
274 jump_out:
275         /* Validate device name */
276         if (strncmp("dpni", sep, 4) &&
277             strncmp("dpseci", sep, 6) &&
278             strncmp("dpcon", sep, 5) &&
279             strncmp("dpbp", sep, 4) &&
280             strncmp("dpio", sep, 4) &&
281             strncmp("dpci", sep, 4) &&
282             strncmp("dpmcp", sep, 5) &&
283             strncmp("dpdmai", sep, 6) &&
284             strncmp("dpdmux", sep, 6)) {
285                 DPAA2_BUS_DEBUG("Unknown or unsupported device (%s)", sep);
286                 goto err_out;
287         }
288
289         t_ptr = strchr(sep, '.');
290         if (!t_ptr || sscanf(t_ptr + 1, "%hu", &dev_id) != 1) {
291                 DPAA2_BUS_ERR("Missing device id in device name (%s)", sep);
292                 goto err_out;
293         }
294
295         if (addr) {
296                 strcpy(addr, sep);
297                 if (!sep_exists && sep)
298                         free(sep);
299                 return 0;
300         }
301
302 err_out:
303         if (!sep_exists && sep)
304                 free(sep);
305         return -EINVAL;
306 }
307
308 static int
309 rte_fslmc_scan(void)
310 {
311         int ret;
312         int device_count = 0;
313         char fslmc_dirpath[PATH_MAX];
314         DIR *dir;
315         struct dirent *entry;
316         static int process_once;
317         int groupid;
318
319         if (process_once) {
320                 DPAA2_BUS_DEBUG("Fslmc bus already scanned. Not rescanning");
321                 return 0;
322         }
323         process_once = 1;
324
325         ret = fslmc_get_container_group(&groupid);
326         if (ret != 0)
327                 goto scan_fail;
328
329         /* Scan devices on the group */
330         sprintf(fslmc_dirpath, "%s/%s", SYSFS_FSL_MC_DEVICES, fslmc_container);
331         dir = opendir(fslmc_dirpath);
332         if (!dir) {
333                 DPAA2_BUS_ERR("Unable to open VFIO group directory");
334                 goto scan_fail;
335         }
336
337         while ((entry = readdir(dir)) != NULL) {
338                 if (entry->d_name[0] == '.' || entry->d_type != DT_DIR)
339                         continue;
340
341                 ret = scan_one_fslmc_device(entry->d_name);
342                 if (ret != 0) {
343                         /* Error in parsing directory - exit gracefully */
344                         goto scan_fail_cleanup;
345                 }
346                 device_count += 1;
347         }
348
349         closedir(dir);
350
351         DPAA2_BUS_INFO("FSLMC Bus scan completed");
352         /* If debugging is enabled, device list is dumped to log output */
353         dump_device_list();
354
355         return 0;
356
357 scan_fail_cleanup:
358         closedir(dir);
359
360         /* Remove all devices in the list */
361         cleanup_fslmc_device_list();
362 scan_fail:
363         DPAA2_BUS_DEBUG("FSLMC Bus Not Available. Skipping (%d)", ret);
364         /* Irrespective of failure, scan only return success */
365         return 0;
366 }
367
368 static int
369 rte_fslmc_match(struct rte_dpaa2_driver *dpaa2_drv,
370                 struct rte_dpaa2_device *dpaa2_dev)
371 {
372         if (dpaa2_drv->drv_type == dpaa2_dev->dev_type)
373                 return 0;
374
375         return 1;
376 }
377
378 static int
379 rte_fslmc_probe(void)
380 {
381         int ret = 0;
382         int probe_all;
383
384         struct rte_dpaa2_device *dev;
385         struct rte_dpaa2_driver *drv;
386
387         if (TAILQ_EMPTY(&rte_fslmc_bus.device_list))
388                 return 0;
389
390         ret = fslmc_vfio_setup_group();
391         if (ret) {
392                 DPAA2_BUS_ERR("Unable to setup VFIO %d", ret);
393                 return 0;
394         }
395
396         /* Map existing segments as well as, in case of hotpluggable memory,
397          * install callback handler.
398          */
399         if (rte_eal_process_type() == RTE_PROC_PRIMARY) {
400                 ret = rte_fslmc_vfio_dmamap();
401                 if (ret) {
402                         DPAA2_BUS_ERR("Unable to DMA map existing VAs: (%d)",
403                                       ret);
404                         /* Not continuing ahead */
405                         DPAA2_BUS_ERR("FSLMC VFIO Mapping failed");
406                         return 0;
407                 }
408         }
409
410         ret = fslmc_vfio_process_group();
411         if (ret) {
412                 DPAA2_BUS_ERR("Unable to setup devices %d", ret);
413                 return 0;
414         }
415
416         probe_all = rte_fslmc_bus.bus.conf.scan_mode != RTE_BUS_SCAN_WHITELIST;
417
418         /* In case of PA, the FD addresses returned by qbman APIs are physical
419          * addresses, which need conversion into equivalent VA address for
420          * rte_mbuf. For that, a table (a serial array, in memory) is used to
421          * increase translation efficiency.
422          * This has to be done before probe as some device initialization
423          * (during) probe allocate memory (dpaa2_sec) which needs to be pinned
424          * to this table.
425          *
426          * Error is ignored as relevant logs are handled within dpaax and
427          * handling for unavailable dpaax table too is transparent to caller.
428          *
429          * And, the IOVA table is only applicable in case of PA mode.
430          */
431         if (rte_eal_iova_mode() == RTE_IOVA_PA)
432                 dpaax_iova_table_populate();
433
434         TAILQ_FOREACH(dev, &rte_fslmc_bus.device_list, next) {
435                 TAILQ_FOREACH(drv, &rte_fslmc_bus.driver_list, next) {
436                         ret = rte_fslmc_match(drv, dev);
437                         if (ret)
438                                 continue;
439
440                         if (!drv->probe)
441                                 continue;
442
443                         if (rte_dev_is_probed(&dev->device))
444                                 continue;
445
446                         if (dev->device.devargs &&
447                           dev->device.devargs->policy == RTE_DEV_BLACKLISTED) {
448                                 DPAA2_BUS_LOG(DEBUG, "%s Blacklisted, skipping",
449                                               dev->device.name);
450                                 continue;
451                         }
452
453                         if (probe_all ||
454                            (dev->device.devargs &&
455                            dev->device.devargs->policy ==
456                            RTE_DEV_WHITELISTED)) {
457                                 ret = drv->probe(drv, dev);
458                                 if (ret) {
459                                         DPAA2_BUS_ERR("Unable to probe");
460                                 } else {
461                                         dev->driver = drv;
462                                         dev->device.driver = &drv->driver;
463                                 }
464                         }
465                         break;
466                 }
467         }
468
469         if (rte_eal_iova_mode() == RTE_IOVA_VA)
470                 dpaa2_virt_mode = 1;
471
472         return 0;
473 }
474
475 static struct rte_device *
476 rte_fslmc_find_device(const struct rte_device *start, rte_dev_cmp_t cmp,
477                       const void *data)
478 {
479         const struct rte_dpaa2_device *dstart;
480         struct rte_dpaa2_device *dev;
481
482         DPAA2_BUS_DEBUG("Finding a device named %s\n", (const char *)data);
483
484         /* find_device is always called with an opaque object which should be
485          * passed along to the 'cmp' function iterating over all device obj
486          * on the bus.
487          */
488
489         if (start != NULL) {
490                 dstart = RTE_DEV_TO_FSLMC_CONST(start);
491                 dev = TAILQ_NEXT(dstart, next);
492         } else {
493                 dev = TAILQ_FIRST(&rte_fslmc_bus.device_list);
494         }
495         while (dev != NULL) {
496                 if (cmp(&dev->device, data) == 0) {
497                         DPAA2_BUS_DEBUG("Found device (%s)\n",
498                                         dev->device.name);
499                         return &dev->device;
500                 }
501                 dev = TAILQ_NEXT(dev, next);
502         }
503
504         return NULL;
505 }
506
507 /*register a fslmc bus based dpaa2 driver */
508 void
509 rte_fslmc_driver_register(struct rte_dpaa2_driver *driver)
510 {
511         RTE_VERIFY(driver);
512
513         TAILQ_INSERT_TAIL(&rte_fslmc_bus.driver_list, driver, next);
514         /* Update Bus references */
515         driver->fslmc_bus = &rte_fslmc_bus;
516 }
517
518 /*un-register a fslmc bus based dpaa2 driver */
519 void
520 rte_fslmc_driver_unregister(struct rte_dpaa2_driver *driver)
521 {
522         struct rte_fslmc_bus *fslmc_bus;
523
524         fslmc_bus = driver->fslmc_bus;
525
526         /* Cleanup the PA->VA Translation table; From whereever this function
527          * is called from.
528          */
529         if (rte_eal_iova_mode() == RTE_IOVA_PA)
530                 dpaax_iova_table_depopulate();
531
532         TAILQ_REMOVE(&fslmc_bus->driver_list, driver, next);
533         /* Update Bus references */
534         driver->fslmc_bus = NULL;
535 }
536
537 /*
538  * All device has iova as va
539  */
540 static inline int
541 fslmc_all_device_support_iova(void)
542 {
543         int ret = 0;
544         struct rte_dpaa2_device *dev;
545         struct rte_dpaa2_driver *drv;
546
547         TAILQ_FOREACH(dev, &rte_fslmc_bus.device_list, next) {
548                 TAILQ_FOREACH(drv, &rte_fslmc_bus.driver_list, next) {
549                         ret = rte_fslmc_match(drv, dev);
550                         if (ret)
551                                 continue;
552                         /* if the driver is not supporting IOVA */
553                         if (!(drv->drv_flags & RTE_DPAA2_DRV_IOVA_AS_VA))
554                                 return 0;
555                 }
556         }
557         return 1;
558 }
559
560 /*
561  * Get iommu class of DPAA2 devices on the bus.
562  */
563 static enum rte_iova_mode
564 rte_dpaa2_get_iommu_class(void)
565 {
566         bool is_vfio_noiommu_enabled = 1;
567         bool has_iova_va;
568
569         if (TAILQ_EMPTY(&rte_fslmc_bus.device_list))
570                 return RTE_IOVA_DC;
571
572 #ifdef RTE_LIBRTE_DPAA2_USE_PHYS_IOVA
573         return RTE_IOVA_PA;
574 #endif
575
576         /* check if all devices on the bus support Virtual addressing or not */
577         has_iova_va = fslmc_all_device_support_iova();
578
579 #ifdef VFIO_PRESENT
580         is_vfio_noiommu_enabled = rte_vfio_noiommu_is_enabled() == true ?
581                                                 true : false;
582 #endif
583
584         if (has_iova_va && !is_vfio_noiommu_enabled)
585                 return RTE_IOVA_VA;
586
587         return RTE_IOVA_PA;
588 }
589
590 static int
591 fslmc_bus_plug(struct rte_device *dev __rte_unused)
592 {
593         /* No operation is performed while plugging the device */
594         return 0;
595 }
596
597 static int
598 fslmc_bus_unplug(struct rte_device *dev __rte_unused)
599 {
600         /* No operation is performed while unplugging the device */
601         return 0;
602 }
603
604 static void *
605 fslmc_bus_dev_iterate(const void *start, const char *str,
606                       const struct rte_dev_iterator *it __rte_unused)
607 {
608         const struct rte_dpaa2_device *dstart;
609         struct rte_dpaa2_device *dev;
610         char *dup, *dev_name = NULL;
611
612         /* Expectation is that device would be name=device_name */
613         if (strncmp(str, "name=", 5) != 0) {
614                 DPAA2_BUS_DEBUG("Invalid device string (%s)\n", str);
615                 return NULL;
616         }
617
618         /* Now that name=device_name format is available, split */
619         dup = strdup(str);
620         dev_name = dup + strlen("name=");
621
622         if (start != NULL) {
623                 dstart = RTE_DEV_TO_FSLMC_CONST(start);
624                 dev = TAILQ_NEXT(dstart, next);
625         } else {
626                 dev = TAILQ_FIRST(&rte_fslmc_bus.device_list);
627         }
628
629         while (dev != NULL) {
630                 if (strcmp(dev->device.name, dev_name) == 0) {
631                         free(dup);
632                         return &dev->device;
633                 }
634                 dev = TAILQ_NEXT(dev, next);
635         }
636
637         free(dup);
638         return NULL;
639 }
640
641 struct rte_fslmc_bus rte_fslmc_bus = {
642         .bus = {
643                 .scan = rte_fslmc_scan,
644                 .probe = rte_fslmc_probe,
645                 .parse = rte_fslmc_parse,
646                 .find_device = rte_fslmc_find_device,
647                 .get_iommu_class = rte_dpaa2_get_iommu_class,
648                 .plug = fslmc_bus_plug,
649                 .unplug = fslmc_bus_unplug,
650                 .dev_iterate = fslmc_bus_dev_iterate,
651         },
652         .device_list = TAILQ_HEAD_INITIALIZER(rte_fslmc_bus.device_list),
653         .driver_list = TAILQ_HEAD_INITIALIZER(rte_fslmc_bus.driver_list),
654         .device_count = {0},
655 };
656
657 RTE_REGISTER_BUS(FSLMC_BUS_NAME, rte_fslmc_bus.bus);
658
659 RTE_INIT(fslmc_init_log)
660 {
661         /* Bus level logs */
662         dpaa2_logtype_bus = rte_log_register("bus.fslmc");
663         if (dpaa2_logtype_bus >= 0)
664                 rte_log_set_level(dpaa2_logtype_bus, RTE_LOG_NOTICE);
665 }