doc: add a supported NIC in mlx5 vDPA guide
[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 #define VFIO_IOMMU_GROUP_PATH "/sys/kernel/iommu_groups"
25 #define FSLMC_BUS_NAME  fslmc
26
27 struct rte_fslmc_bus rte_fslmc_bus;
28 uint8_t dpaa2_virt_mode;
29
30 uint32_t
31 rte_fslmc_get_device_count(enum rte_dpaa2_dev_type device_type)
32 {
33         if (device_type >= DPAA2_DEVTYPE_MAX)
34                 return 0;
35         return rte_fslmc_bus.device_count[device_type];
36 }
37
38 static void
39 cleanup_fslmc_device_list(void)
40 {
41         struct rte_dpaa2_device *dev;
42         struct rte_dpaa2_device *t_dev;
43
44         TAILQ_FOREACH_SAFE(dev, &rte_fslmc_bus.device_list, next, t_dev) {
45                 TAILQ_REMOVE(&rte_fslmc_bus.device_list, dev, next);
46                 free(dev);
47                 dev = NULL;
48         }
49 }
50
51 static int
52 compare_dpaa2_devname(struct rte_dpaa2_device *dev1,
53                       struct rte_dpaa2_device *dev2)
54 {
55         int comp;
56
57         if (dev1->dev_type > dev2->dev_type) {
58                 comp = 1;
59         } else if (dev1->dev_type < dev2->dev_type) {
60                 comp = -1;
61         } else {
62                 /* Check the ID as types match */
63                 if (dev1->object_id > dev2->object_id)
64                         comp = 1;
65                 else if (dev1->object_id < dev2->object_id)
66                         comp = -1;
67                 else
68                         comp = 0; /* Duplicate device name */
69         }
70
71         return comp;
72 }
73
74 static void
75 insert_in_device_list(struct rte_dpaa2_device *newdev)
76 {
77         int comp, inserted = 0;
78         struct rte_dpaa2_device *dev = NULL;
79         struct rte_dpaa2_device *tdev = NULL;
80
81         TAILQ_FOREACH_SAFE(dev, &rte_fslmc_bus.device_list, next, tdev) {
82                 comp = compare_dpaa2_devname(newdev, dev);
83                 if (comp < 0) {
84                         TAILQ_INSERT_BEFORE(dev, newdev, next);
85                         inserted = 1;
86                         break;
87                 }
88         }
89
90         if (!inserted)
91                 TAILQ_INSERT_TAIL(&rte_fslmc_bus.device_list, newdev, next);
92 }
93
94 static struct rte_devargs *
95 fslmc_devargs_lookup(struct rte_dpaa2_device *dev)
96 {
97         struct rte_devargs *devargs;
98         char dev_name[32];
99
100         RTE_EAL_DEVARGS_FOREACH("fslmc", devargs) {
101                 devargs->bus->parse(devargs->name, &dev_name);
102                 if (strcmp(dev_name, dev->device.name) == 0) {
103                         DPAA2_BUS_INFO("**Devargs matched %s", dev_name);
104                         return devargs;
105                 }
106         }
107         return NULL;
108 }
109
110 static void
111 dump_device_list(void)
112 {
113         struct rte_dpaa2_device *dev;
114
115         /* Only if the log level has been set to Debugging, print list */
116         if (rte_log_can_log(dpaa2_logtype_bus, RTE_LOG_DEBUG)) {
117                 DPAA2_BUS_LOG(DEBUG, "List of devices scanned on bus:");
118                 TAILQ_FOREACH(dev, &rte_fslmc_bus.device_list, next) {
119                         DPAA2_BUS_LOG(DEBUG, "\t\t%s", dev->device.name);
120                 }
121         }
122 }
123
124 static int
125 scan_one_fslmc_device(char *dev_name)
126 {
127         char *dup_dev_name, *t_ptr;
128         struct rte_dpaa2_device *dev = NULL;
129         int ret = -1;
130
131         if (!dev_name)
132                 return ret;
133
134         /* Ignore the Container name itself */
135         if (!strncmp("dprc", dev_name, 4))
136                 return 0;
137
138         /* Creating a temporary copy to perform cut-parse over string */
139         dup_dev_name = strdup(dev_name);
140         if (!dup_dev_name) {
141                 DPAA2_BUS_ERR("Unable to allocate device name memory");
142                 return -ENOMEM;
143         }
144
145         /* For all other devices, we allocate rte_dpaa2_device.
146          * For those devices where there is no driver, probe would release
147          * the memory associated with the rte_dpaa2_device after necessary
148          * initialization.
149          */
150         dev = calloc(1, sizeof(struct rte_dpaa2_device));
151         if (!dev) {
152                 DPAA2_BUS_ERR("Unable to allocate device object");
153                 free(dup_dev_name);
154                 return -ENOMEM;
155         }
156
157         dev->device.bus = &rte_fslmc_bus.bus;
158
159         /* Parse the device name and ID */
160         t_ptr = strtok(dup_dev_name, ".");
161         if (!t_ptr) {
162                 DPAA2_BUS_ERR("Invalid device found: (%s)", dup_dev_name);
163                 ret = -EINVAL;
164                 goto cleanup;
165         }
166         if (!strncmp("dpni", t_ptr, 4))
167                 dev->dev_type = DPAA2_ETH;
168         else if (!strncmp("dpseci", t_ptr, 6))
169                 dev->dev_type = DPAA2_CRYPTO;
170         else if (!strncmp("dpcon", t_ptr, 5))
171                 dev->dev_type = DPAA2_CON;
172         else if (!strncmp("dpbp", t_ptr, 4))
173                 dev->dev_type = DPAA2_BPOOL;
174         else if (!strncmp("dpio", t_ptr, 4))
175                 dev->dev_type = DPAA2_IO;
176         else if (!strncmp("dpci", t_ptr, 4))
177                 dev->dev_type = DPAA2_CI;
178         else if (!strncmp("dpmcp", t_ptr, 5))
179                 dev->dev_type = DPAA2_MPORTAL;
180         else if (!strncmp("dpdmai", t_ptr, 6))
181                 dev->dev_type = DPAA2_QDMA;
182         else if (!strncmp("dpdmux", t_ptr, 6))
183                 dev->dev_type = DPAA2_MUX;
184         else if (!strncmp("dprtc", t_ptr, 5))
185                 dev->dev_type = DPAA2_DPRTC;
186         else
187                 dev->dev_type = DPAA2_UNKNOWN;
188
189         t_ptr = strtok(NULL, ".");
190         if (!t_ptr) {
191                 DPAA2_BUS_ERR("Skipping invalid device (%s)", dup_dev_name);
192                 ret = 0;
193                 goto cleanup;
194         }
195
196         sscanf(t_ptr, "%hu", &dev->object_id);
197         dev->device.name = strdup(dev_name);
198         if (!dev->device.name) {
199                 DPAA2_BUS_ERR("Unable to clone device name. Out of memory");
200                 ret = -ENOMEM;
201                 goto cleanup;
202         }
203         dev->device.devargs = fslmc_devargs_lookup(dev);
204
205         /* Update the device found into the device_count table */
206         rte_fslmc_bus.device_count[dev->dev_type]++;
207
208         /* Add device in the fslmc device list */
209         insert_in_device_list(dev);
210
211         /* Don't need the duplicated device filesystem entry anymore */
212         if (dup_dev_name)
213                 free(dup_dev_name);
214
215         return 0;
216 cleanup:
217         if (dup_dev_name)
218                 free(dup_dev_name);
219         if (dev)
220                 free(dev);
221         return ret;
222 }
223
224 static int
225 rte_fslmc_parse(const char *name, void *addr)
226 {
227         uint16_t dev_id;
228         char *t_ptr;
229         const char *sep;
230         uint8_t sep_exists = 0;
231         int ret = -1;
232
233         DPAA2_BUS_DEBUG("Parsing dev=(%s)", name);
234
235         /* There are multiple ways this can be called, with bus:dev, name=dev
236          * or just dev. In all cases, the 'addr' is actually a string.
237          */
238         sep = strchr(name, ':');
239         if (!sep) {
240                 /* check for '=' */
241                 sep = strchr(name, '=');
242                 if (!sep)
243                         sep_exists = 0;
244                 else
245                         sep_exists = 1;
246         } else
247                 sep_exists = 1;
248
249         /* Check if starting part if either of 'fslmc:' or 'name=', separator
250          * exists.
251          */
252         if (sep_exists) {
253                 /* If either of "fslmc" or "name" are starting part */
254                 if (!strncmp(name, RTE_STR(FSLMC_BUS_NAME),
255                              strlen(RTE_STR(FSLMC_BUS_NAME))) ||
256                    (!strncmp(name, "name", strlen("name")))) {
257                         goto jump_out;
258                 } else {
259                         DPAA2_BUS_DEBUG("Invalid device for matching (%s).",
260                                         name);
261                         ret = -EINVAL;
262                         goto err_out;
263                 }
264         } else
265                 sep = name;
266
267 jump_out:
268         /* Validate device name */
269         if (strncmp("dpni", sep, 4) &&
270             strncmp("dpseci", sep, 6) &&
271             strncmp("dpcon", sep, 5) &&
272             strncmp("dpbp", sep, 4) &&
273             strncmp("dpio", sep, 4) &&
274             strncmp("dpci", sep, 4) &&
275             strncmp("dpmcp", sep, 5) &&
276             strncmp("dpdmai", sep, 6) &&
277             strncmp("dpdmux", sep, 6)) {
278                 DPAA2_BUS_DEBUG("Unknown or unsupported device (%s)", sep);
279                 ret = -EINVAL;
280                 goto err_out;
281         }
282
283         t_ptr = strchr(sep, '.');
284         if (!t_ptr || sscanf(t_ptr + 1, "%hu", &dev_id) != 1) {
285                 DPAA2_BUS_ERR("Missing device id in device name (%s)", sep);
286                 ret = -EINVAL;
287                 goto err_out;
288         }
289
290         if (addr)
291                 strcpy(addr, sep);
292
293         ret = 0;
294 err_out:
295         return ret;
296 }
297
298 static int
299 rte_fslmc_scan(void)
300 {
301         int ret;
302         int device_count = 0;
303         char fslmc_dirpath[PATH_MAX];
304         DIR *dir;
305         struct dirent *entry;
306         static int process_once;
307         int groupid;
308
309         if (process_once) {
310                 DPAA2_BUS_DEBUG("Fslmc bus already scanned. Not rescanning");
311                 return 0;
312         }
313         process_once = 1;
314
315         ret = fslmc_get_container_group(&groupid);
316         if (ret != 0)
317                 goto scan_fail;
318
319         /* Scan devices on the group */
320         sprintf(fslmc_dirpath, "%s/%s", SYSFS_FSL_MC_DEVICES, fslmc_container);
321         dir = opendir(fslmc_dirpath);
322         if (!dir) {
323                 DPAA2_BUS_ERR("Unable to open VFIO group directory");
324                 goto scan_fail;
325         }
326
327         while ((entry = readdir(dir)) != NULL) {
328                 if (entry->d_name[0] == '.' || entry->d_type != DT_DIR)
329                         continue;
330
331                 ret = scan_one_fslmc_device(entry->d_name);
332                 if (ret != 0) {
333                         /* Error in parsing directory - exit gracefully */
334                         goto scan_fail_cleanup;
335                 }
336                 device_count += 1;
337         }
338
339         closedir(dir);
340
341         DPAA2_BUS_INFO("FSLMC Bus scan completed");
342         /* If debugging is enabled, device list is dumped to log output */
343         dump_device_list();
344
345         return 0;
346
347 scan_fail_cleanup:
348         closedir(dir);
349
350         /* Remove all devices in the list */
351         cleanup_fslmc_device_list();
352 scan_fail:
353         DPAA2_BUS_DEBUG("FSLMC Bus Not Available. Skipping (%d)", ret);
354         /* Irrespective of failure, scan only return success */
355         return 0;
356 }
357
358 static int
359 rte_fslmc_match(struct rte_dpaa2_driver *dpaa2_drv,
360                 struct rte_dpaa2_device *dpaa2_dev)
361 {
362         if (dpaa2_drv->drv_type == dpaa2_dev->dev_type)
363                 return 0;
364
365         return 1;
366 }
367
368 static int
369 rte_fslmc_probe(void)
370 {
371         int ret = 0;
372         int probe_all;
373
374         struct rte_dpaa2_device *dev;
375         struct rte_dpaa2_driver *drv;
376
377         if (TAILQ_EMPTY(&rte_fslmc_bus.device_list))
378                 return 0;
379
380         ret = fslmc_vfio_setup_group();
381         if (ret) {
382                 DPAA2_BUS_ERR("Unable to setup VFIO %d", ret);
383                 return 0;
384         }
385
386         /* Map existing segments as well as, in case of hotpluggable memory,
387          * install callback handler.
388          */
389         if (rte_eal_process_type() == RTE_PROC_PRIMARY) {
390                 ret = rte_fslmc_vfio_dmamap();
391                 if (ret) {
392                         DPAA2_BUS_ERR("Unable to DMA map existing VAs: (%d)",
393                                       ret);
394                         /* Not continuing ahead */
395                         DPAA2_BUS_ERR("FSLMC VFIO Mapping failed");
396                         return 0;
397                 }
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          * And, the IOVA table is only applicable in case of PA mode.
420          */
421         if (rte_eal_iova_mode() == RTE_IOVA_PA)
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         if (rte_eal_iova_mode() == RTE_IOVA_PA)
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         if (str == NULL) {
603                 DPAA2_BUS_DEBUG("No device string");
604                 return NULL;
605         }
606
607         /* Expectation is that device would be name=device_name */
608         if (strncmp(str, "name=", 5) != 0) {
609                 DPAA2_BUS_DEBUG("Invalid device string (%s)\n", str);
610                 return NULL;
611         }
612
613         /* Now that name=device_name format is available, split */
614         dup = strdup(str);
615         dev_name = dup + strlen("name=");
616
617         if (start != NULL) {
618                 dstart = RTE_DEV_TO_FSLMC_CONST(start);
619                 dev = TAILQ_NEXT(dstart, next);
620         } else {
621                 dev = TAILQ_FIRST(&rte_fslmc_bus.device_list);
622         }
623
624         while (dev != NULL) {
625                 if (strcmp(dev->device.name, dev_name) == 0) {
626                         free(dup);
627                         return &dev->device;
628                 }
629                 dev = TAILQ_NEXT(dev, next);
630         }
631
632         free(dup);
633         return NULL;
634 }
635
636 struct rte_fslmc_bus rte_fslmc_bus = {
637         .bus = {
638                 .scan = rte_fslmc_scan,
639                 .probe = rte_fslmc_probe,
640                 .parse = rte_fslmc_parse,
641                 .find_device = rte_fslmc_find_device,
642                 .get_iommu_class = rte_dpaa2_get_iommu_class,
643                 .plug = fslmc_bus_plug,
644                 .unplug = fslmc_bus_unplug,
645                 .dev_iterate = fslmc_bus_dev_iterate,
646         },
647         .device_list = TAILQ_HEAD_INITIALIZER(rte_fslmc_bus.device_list),
648         .driver_list = TAILQ_HEAD_INITIALIZER(rte_fslmc_bus.driver_list),
649         .device_count = {0},
650 };
651
652 RTE_REGISTER_BUS(FSLMC_BUS_NAME, rte_fslmc_bus.bus);
653 RTE_LOG_REGISTER(dpaa2_logtype_bus, bus.fslmc, NOTICE);