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