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