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