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