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