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