bus/fslmc: fix physical addressing check
[dpdk.git] / drivers / bus / fslmc / fslmc_bus.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  *
3  *   Copyright 2016 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 int dpaa2_logtype_bus;
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 uint32_t
32 rte_fslmc_get_device_count(enum rte_dpaa2_dev_type device_type)
33 {
34         if (device_type > DPAA2_DEVTYPE_MAX)
35                 return 0;
36         return rte_fslmc_bus.device_count[device_type];
37 }
38
39 RTE_DEFINE_PER_LCORE(struct dpaa2_portal_dqrr, dpaa2_held_bufs);
40
41 static void
42 cleanup_fslmc_device_list(void)
43 {
44         struct rte_dpaa2_device *dev;
45         struct rte_dpaa2_device *t_dev;
46
47         TAILQ_FOREACH_SAFE(dev, &rte_fslmc_bus.device_list, next, t_dev) {
48                 TAILQ_REMOVE(&rte_fslmc_bus.device_list, dev, next);
49                 free(dev);
50                 dev = NULL;
51         }
52 }
53
54 static int
55 compare_dpaa2_devname(struct rte_dpaa2_device *dev1,
56                       struct rte_dpaa2_device *dev2)
57 {
58         int comp;
59
60         if (dev1->dev_type > dev2->dev_type) {
61                 comp = 1;
62         } else if (dev1->dev_type < dev2->dev_type) {
63                 comp = -1;
64         } else {
65                 /* Check the ID as types match */
66                 if (dev1->object_id > dev2->object_id)
67                         comp = 1;
68                 else if (dev1->object_id < dev2->object_id)
69                         comp = -1;
70                 else
71                         comp = 0; /* Duplicate device name */
72         }
73
74         return comp;
75 }
76
77 static void
78 insert_in_device_list(struct rte_dpaa2_device *newdev)
79 {
80         int comp, inserted = 0;
81         struct rte_dpaa2_device *dev = NULL;
82         struct rte_dpaa2_device *tdev = NULL;
83
84         TAILQ_FOREACH_SAFE(dev, &rte_fslmc_bus.device_list, next, tdev) {
85                 comp = compare_dpaa2_devname(newdev, dev);
86                 if (comp < 0) {
87                         TAILQ_INSERT_BEFORE(dev, newdev, next);
88                         inserted = 1;
89                         break;
90                 }
91         }
92
93         if (!inserted)
94                 TAILQ_INSERT_TAIL(&rte_fslmc_bus.device_list, newdev, next);
95 }
96
97 static struct rte_devargs *
98 fslmc_devargs_lookup(struct rte_dpaa2_device *dev)
99 {
100         struct rte_devargs *devargs;
101         char dev_name[32];
102
103         RTE_EAL_DEVARGS_FOREACH("fslmc", devargs) {
104                 devargs->bus->parse(devargs->name, &dev_name);
105                 if (strcmp(dev_name, dev->device.name) == 0) {
106                         DPAA2_BUS_INFO("**Devargs matched %s", dev_name);
107                         return devargs;
108                 }
109         }
110         return NULL;
111 }
112
113 static void
114 dump_device_list(void)
115 {
116         struct rte_dpaa2_device *dev;
117         uint32_t global_log_level;
118         int local_log_level;
119
120         /* Only if the log level has been set to Debugging, print list */
121         global_log_level = rte_log_get_global_level();
122         local_log_level = rte_log_get_level(dpaa2_logtype_bus);
123         if (global_log_level == RTE_LOG_DEBUG ||
124             local_log_level == RTE_LOG_DEBUG) {
125                 DPAA2_BUS_LOG(DEBUG, "List of devices scanned on bus:");
126                 TAILQ_FOREACH(dev, &rte_fslmc_bus.device_list, next) {
127                         DPAA2_BUS_LOG(DEBUG, "\t\t%s", dev->device.name);
128                 }
129         }
130 }
131
132 static int
133 scan_one_fslmc_device(char *dev_name)
134 {
135         char *dup_dev_name, *t_ptr;
136         struct rte_dpaa2_device *dev;
137
138         if (!dev_name)
139                 return -1;
140
141         /* Ignore the Container name itself */
142         if (!strncmp("dprc", dev_name, 4))
143                 return 0;
144
145         /* Creating a temporary copy to perform cut-parse over string */
146         dup_dev_name = strdup(dev_name);
147         if (!dup_dev_name) {
148                 DPAA2_BUS_ERR("Unable to allocate device name memory");
149                 return -ENOMEM;
150         }
151
152         /* For all other devices, we allocate rte_dpaa2_device.
153          * For those devices where there is no driver, probe would release
154          * the memory associated with the rte_dpaa2_device after necessary
155          * initialization.
156          */
157         dev = calloc(1, sizeof(struct rte_dpaa2_device));
158         if (!dev) {
159                 DPAA2_BUS_ERR("Unable to allocate device object");
160                 free(dup_dev_name);
161                 return -ENOMEM;
162         }
163
164         dev->device.bus = &rte_fslmc_bus.bus;
165
166         /* Parse the device name and ID */
167         t_ptr = strtok(dup_dev_name, ".");
168         if (!t_ptr) {
169                 DPAA2_BUS_ERR("Incorrect device name observed");
170                 goto cleanup;
171         }
172         if (!strncmp("dpni", t_ptr, 4))
173                 dev->dev_type = DPAA2_ETH;
174         else if (!strncmp("dpseci", t_ptr, 6))
175                 dev->dev_type = DPAA2_CRYPTO;
176         else if (!strncmp("dpcon", t_ptr, 5))
177                 dev->dev_type = DPAA2_CON;
178         else if (!strncmp("dpbp", t_ptr, 4))
179                 dev->dev_type = DPAA2_BPOOL;
180         else if (!strncmp("dpio", t_ptr, 4))
181                 dev->dev_type = DPAA2_IO;
182         else if (!strncmp("dpci", t_ptr, 4))
183                 dev->dev_type = DPAA2_CI;
184         else if (!strncmp("dpmcp", t_ptr, 5))
185                 dev->dev_type = DPAA2_MPORTAL;
186         else if (!strncmp("dpdmai", t_ptr, 6))
187                 dev->dev_type = DPAA2_QDMA;
188         else
189                 dev->dev_type = DPAA2_UNKNOWN;
190
191         /* Update the device found into the device_count table */
192         rte_fslmc_bus.device_count[dev->dev_type]++;
193
194         t_ptr = strtok(NULL, ".");
195         if (!t_ptr) {
196                 DPAA2_BUS_ERR("Incorrect device string observed (%s)", t_ptr);
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                 goto cleanup;
205         }
206         dev->device.devargs = fslmc_devargs_lookup(dev);
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 -1;
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         char *sep = strchr(name, ':');
230
231         if (strncmp(name, RTE_STR(FSLMC_BUS_NAME),
232                 strlen(RTE_STR(FSLMC_BUS_NAME)))) {
233                 return -EINVAL;
234         }
235
236         if (!sep) {
237                 DPAA2_BUS_ERR("Incorrect device name observed");
238                 return -EINVAL;
239         }
240
241         t_ptr = (char *)(sep + 1);
242
243         if (strncmp("dpni", t_ptr, 4) &&
244             strncmp("dpseci", t_ptr, 6) &&
245             strncmp("dpcon", t_ptr, 5) &&
246             strncmp("dpbp", t_ptr, 4) &&
247             strncmp("dpio", t_ptr, 4) &&
248             strncmp("dpci", t_ptr, 4) &&
249             strncmp("dpmcp", t_ptr, 5) &&
250             strncmp("dpdmai", t_ptr, 6)) {
251                 DPAA2_BUS_ERR("Unknown or unsupported device");
252                 return -EINVAL;
253         }
254
255         t_ptr = strchr(name, '.');
256         if (!t_ptr) {
257                 DPAA2_BUS_ERR("Incorrect device string observed (%s)", t_ptr);
258                 return -EINVAL;
259         }
260
261         t_ptr = (char *)(t_ptr + 1);
262         if (sscanf(t_ptr, "%hu", &dev_id) <= 0) {
263                 DPAA2_BUS_ERR("Incorrect device string observed (%s)", t_ptr);
264                 return -EINVAL;
265         }
266
267         if (addr)
268                 strcpy(addr, (char *)(sep + 1));
269         return 0;
270 }
271
272 static int
273 rte_fslmc_scan(void)
274 {
275         int ret;
276         int device_count = 0;
277         char fslmc_dirpath[PATH_MAX];
278         DIR *dir;
279         struct dirent *entry;
280         static int process_once;
281         int groupid;
282
283         if (process_once) {
284                 DPAA2_BUS_DEBUG("Fslmc bus already scanned. Not rescanning");
285                 return 0;
286         }
287         process_once = 1;
288
289         ret = fslmc_get_container_group(&groupid);
290         if (ret != 0)
291                 goto scan_fail;
292
293         /* Scan devices on the group */
294         sprintf(fslmc_dirpath, "%s/%d/devices", VFIO_IOMMU_GROUP_PATH,
295                 groupid);
296         dir = opendir(fslmc_dirpath);
297         if (!dir) {
298                 DPAA2_BUS_ERR("Unable to open VFIO group directory");
299                 goto scan_fail;
300         }
301
302         while ((entry = readdir(dir)) != NULL) {
303                 if (entry->d_name[0] == '.' || entry->d_type != DT_LNK)
304                         continue;
305
306                 ret = scan_one_fslmc_device(entry->d_name);
307                 if (ret != 0) {
308                         /* Error in parsing directory - exit gracefully */
309                         goto scan_fail_cleanup;
310                 }
311                 device_count += 1;
312         }
313
314         closedir(dir);
315
316         DPAA2_BUS_INFO("FSLMC Bus scan completed");
317         /* If debugging is enabled, device list is dumped to log output */
318         dump_device_list();
319
320         return 0;
321
322 scan_fail_cleanup:
323         closedir(dir);
324
325         /* Remove all devices in the list */
326         cleanup_fslmc_device_list();
327 scan_fail:
328         DPAA2_BUS_DEBUG("FSLMC Bus Not Available. Skipping");
329         /* Irrespective of failure, scan only return success */
330         return 0;
331 }
332
333 static int
334 rte_fslmc_match(struct rte_dpaa2_driver *dpaa2_drv,
335                 struct rte_dpaa2_device *dpaa2_dev)
336 {
337         if (dpaa2_drv->drv_type == dpaa2_dev->dev_type)
338                 return 0;
339
340         return 1;
341 }
342
343 static int
344 rte_fslmc_probe(void)
345 {
346         int ret = 0;
347         int probe_all;
348
349         struct rte_dpaa2_device *dev;
350         struct rte_dpaa2_driver *drv;
351
352         if (TAILQ_EMPTY(&rte_fslmc_bus.device_list))
353                 return 0;
354
355         ret = fslmc_vfio_setup_group();
356         if (ret) {
357                 DPAA2_BUS_ERR("Unable to setup VFIO %d", ret);
358                 return 0;
359         }
360
361         /* Map existing segments as well as, in case of hotpluggable memory,
362          * install callback handler.
363          */
364         ret = rte_fslmc_vfio_dmamap();
365         if (ret) {
366                 DPAA2_BUS_ERR("Unable to DMA map existing VAs: (%d)", ret);
367                 /* Not continuing ahead */
368                 DPAA2_BUS_ERR("FSLMC VFIO Mapping failed");
369                 return 0;
370         }
371
372         ret = fslmc_vfio_process_group();
373         if (ret) {
374                 DPAA2_BUS_ERR("Unable to setup devices %d", ret);
375                 return 0;
376         }
377
378         probe_all = rte_fslmc_bus.bus.conf.scan_mode != RTE_BUS_SCAN_WHITELIST;
379
380         TAILQ_FOREACH(dev, &rte_fslmc_bus.device_list, next) {
381                 TAILQ_FOREACH(drv, &rte_fslmc_bus.driver_list, next) {
382                         ret = rte_fslmc_match(drv, dev);
383                         if (ret)
384                                 continue;
385
386                         if (!drv->probe)
387                                 continue;
388
389                         if (dev->device.devargs &&
390                           dev->device.devargs->policy == RTE_DEV_BLACKLISTED) {
391                                 DPAA2_BUS_LOG(DEBUG, "%s Blacklisted, skipping",
392                                               dev->device.name);
393                                 continue;
394                         }
395
396                         if (probe_all ||
397                            (dev->device.devargs &&
398                            dev->device.devargs->policy ==
399                            RTE_DEV_WHITELISTED)) {
400                                 ret = drv->probe(drv, dev);
401                                 if (ret) {
402                                         DPAA2_BUS_ERR("Unable to probe");
403                                 } else {
404                                         dev->driver = drv;
405                                         dev->device.driver = &drv->driver;
406                                 }
407                         }
408                         break;
409                 }
410         }
411
412         if (rte_eal_iova_mode() == RTE_IOVA_VA)
413                 dpaa2_virt_mode = 1;
414
415         return 0;
416 }
417
418 static struct rte_device *
419 rte_fslmc_find_device(const struct rte_device *start, rte_dev_cmp_t cmp,
420                       const void *data)
421 {
422         const struct rte_dpaa2_device *dstart;
423         struct rte_dpaa2_device *dev;
424
425         if (start != NULL) {
426                 dstart = RTE_DEV_TO_FSLMC_CONST(start);
427                 dev = TAILQ_NEXT(dstart, next);
428         } else {
429                 dev = TAILQ_FIRST(&rte_fslmc_bus.device_list);
430         }
431         while (dev != NULL) {
432                 if (cmp(&dev->device, data) == 0)
433                         return &dev->device;
434                 dev = TAILQ_NEXT(dev, next);
435         }
436
437         return NULL;
438 }
439
440 /*register a fslmc bus based dpaa2 driver */
441 void
442 rte_fslmc_driver_register(struct rte_dpaa2_driver *driver)
443 {
444         RTE_VERIFY(driver);
445
446         TAILQ_INSERT_TAIL(&rte_fslmc_bus.driver_list, driver, next);
447         /* Update Bus references */
448         driver->fslmc_bus = &rte_fslmc_bus;
449 }
450
451 /*un-register a fslmc bus based dpaa2 driver */
452 void
453 rte_fslmc_driver_unregister(struct rte_dpaa2_driver *driver)
454 {
455         struct rte_fslmc_bus *fslmc_bus;
456
457         fslmc_bus = driver->fslmc_bus;
458
459         TAILQ_REMOVE(&fslmc_bus->driver_list, driver, next);
460         /* Update Bus references */
461         driver->fslmc_bus = NULL;
462 }
463
464 /*
465  * All device has iova as va
466  */
467 static inline int
468 fslmc_all_device_support_iova(void)
469 {
470         int ret = 0;
471         struct rte_dpaa2_device *dev;
472         struct rte_dpaa2_driver *drv;
473
474         TAILQ_FOREACH(dev, &rte_fslmc_bus.device_list, next) {
475                 TAILQ_FOREACH(drv, &rte_fslmc_bus.driver_list, next) {
476                         ret = rte_fslmc_match(drv, dev);
477                         if (ret)
478                                 continue;
479                         /* if the driver is not supporting IOVA */
480                         if (!(drv->drv_flags & RTE_DPAA2_DRV_IOVA_AS_VA))
481                                 return 0;
482                 }
483         }
484         return 1;
485 }
486
487 /*
488  * Get iommu class of DPAA2 devices on the bus.
489  */
490 static enum rte_iova_mode
491 rte_dpaa2_get_iommu_class(void)
492 {
493         bool is_vfio_noiommu_enabled = 1;
494         bool has_iova_va;
495
496         if (TAILQ_EMPTY(&rte_fslmc_bus.device_list))
497                 return RTE_IOVA_DC;
498
499 #ifdef RTE_LIBRTE_DPAA2_USE_PHYS_IOVA
500         return RTE_IOVA_PA;
501 #endif
502
503         /* check if all devices on the bus support Virtual addressing or not */
504         has_iova_va = fslmc_all_device_support_iova();
505
506 #ifdef VFIO_PRESENT
507         is_vfio_noiommu_enabled = rte_vfio_noiommu_is_enabled() == true ?
508                                                 true : false;
509 #endif
510
511         if (has_iova_va && !is_vfio_noiommu_enabled)
512                 return RTE_IOVA_VA;
513
514         return RTE_IOVA_PA;
515 }
516
517 struct rte_fslmc_bus rte_fslmc_bus = {
518         .bus = {
519                 .scan = rte_fslmc_scan,
520                 .probe = rte_fslmc_probe,
521                 .parse = rte_fslmc_parse,
522                 .find_device = rte_fslmc_find_device,
523                 .get_iommu_class = rte_dpaa2_get_iommu_class,
524         },
525         .device_list = TAILQ_HEAD_INITIALIZER(rte_fslmc_bus.device_list),
526         .driver_list = TAILQ_HEAD_INITIALIZER(rte_fslmc_bus.driver_list),
527         .device_count = {0},
528 };
529
530 RTE_REGISTER_BUS(FSLMC_BUS_NAME, rte_fslmc_bus.bus);
531
532 RTE_INIT(fslmc_init_log)
533 {
534         /* Bus level logs */
535         dpaa2_logtype_bus = rte_log_register("bus.fslmc");
536         if (dpaa2_logtype_bus >= 0)
537                 rte_log_set_level(dpaa2_logtype_bus, RTE_LOG_NOTICE);
538 }