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