New upstream version 18.08
[deb_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         /* Parse the device name and ID */
165         t_ptr = strtok(dup_dev_name, ".");
166         if (!t_ptr) {
167                 DPAA2_BUS_ERR("Incorrect device name observed");
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
187                 dev->dev_type = DPAA2_UNKNOWN;
188
189         /* Update the device found into the device_count table */
190         rte_fslmc_bus.device_count[dev->dev_type]++;
191
192         t_ptr = strtok(NULL, ".");
193         if (!t_ptr) {
194                 DPAA2_BUS_ERR("Incorrect device string observed (%s)", t_ptr);
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                 goto cleanup;
203         }
204         dev->device.devargs = fslmc_devargs_lookup(dev);
205
206         /* Add device in the fslmc device list */
207         insert_in_device_list(dev);
208
209         /* Don't need the duplicated device filesystem entry anymore */
210         if (dup_dev_name)
211                 free(dup_dev_name);
212
213         return 0;
214 cleanup:
215         if (dup_dev_name)
216                 free(dup_dev_name);
217         if (dev)
218                 free(dev);
219         return -1;
220 }
221
222 static int
223 rte_fslmc_parse(const char *name, void *addr)
224 {
225         uint16_t dev_id;
226         char *t_ptr;
227         char *sep = strchr(name, ':');
228
229         if (strncmp(name, RTE_STR(FSLMC_BUS_NAME),
230                 strlen(RTE_STR(FSLMC_BUS_NAME)))) {
231                 return -EINVAL;
232         }
233
234         if (!sep) {
235                 DPAA2_BUS_ERR("Incorrect device name observed");
236                 return -EINVAL;
237         }
238
239         t_ptr = (char *)(sep + 1);
240
241         if (strncmp("dpni", t_ptr, 4) &&
242             strncmp("dpseci", t_ptr, 6) &&
243             strncmp("dpcon", t_ptr, 5) &&
244             strncmp("dpbp", t_ptr, 4) &&
245             strncmp("dpio", t_ptr, 4) &&
246             strncmp("dpci", t_ptr, 4) &&
247             strncmp("dpmcp", t_ptr, 5) &&
248             strncmp("dpdmai", t_ptr, 6)) {
249                 DPAA2_BUS_ERR("Unknown or unsupported device");
250                 return -EINVAL;
251         }
252
253         t_ptr = strchr(name, '.');
254         if (!t_ptr) {
255                 DPAA2_BUS_ERR("Incorrect device string observed (%s)", t_ptr);
256                 return -EINVAL;
257         }
258
259         t_ptr = (char *)(t_ptr + 1);
260         if (sscanf(t_ptr, "%hu", &dev_id) <= 0) {
261                 DPAA2_BUS_ERR("Incorrect device string observed (%s)", t_ptr);
262                 return -EINVAL;
263         }
264
265         if (addr)
266                 strcpy(addr, (char *)(sep + 1));
267         return 0;
268 }
269
270 static int
271 rte_fslmc_scan(void)
272 {
273         int ret;
274         int device_count = 0;
275         char fslmc_dirpath[PATH_MAX];
276         DIR *dir;
277         struct dirent *entry;
278         static int process_once;
279         int groupid;
280
281         if (process_once) {
282                 DPAA2_BUS_DEBUG("Fslmc bus already scanned. Not rescanning");
283                 return 0;
284         }
285         process_once = 1;
286
287         ret = fslmc_get_container_group(&groupid);
288         if (ret != 0)
289                 goto scan_fail;
290
291         /* Scan devices on the group */
292         sprintf(fslmc_dirpath, "%s/%d/devices", VFIO_IOMMU_GROUP_PATH,
293                 groupid);
294         dir = opendir(fslmc_dirpath);
295         if (!dir) {
296                 DPAA2_BUS_ERR("Unable to open VFIO group directory");
297                 goto scan_fail;
298         }
299
300         while ((entry = readdir(dir)) != NULL) {
301                 if (entry->d_name[0] == '.' || entry->d_type != DT_LNK)
302                         continue;
303
304                 ret = scan_one_fslmc_device(entry->d_name);
305                 if (ret != 0) {
306                         /* Error in parsing directory - exit gracefully */
307                         goto scan_fail_cleanup;
308                 }
309                 device_count += 1;
310         }
311
312         closedir(dir);
313
314         DPAA2_BUS_INFO("FSLMC Bus scan completed");
315         /* If debugging is enabled, device list is dumped to log output */
316         dump_device_list();
317
318         return 0;
319
320 scan_fail_cleanup:
321         closedir(dir);
322
323         /* Remove all devices in the list */
324         cleanup_fslmc_device_list();
325 scan_fail:
326         DPAA2_BUS_DEBUG("FSLMC Bus Not Available. Skipping");
327         /* Irrespective of failure, scan only return success */
328         return 0;
329 }
330
331 static int
332 rte_fslmc_match(struct rte_dpaa2_driver *dpaa2_drv,
333                 struct rte_dpaa2_device *dpaa2_dev)
334 {
335         if (dpaa2_drv->drv_type == dpaa2_dev->dev_type)
336                 return 0;
337
338         return 1;
339 }
340
341 static int
342 rte_fslmc_probe(void)
343 {
344         int ret = 0;
345         int probe_all;
346
347         struct rte_dpaa2_device *dev;
348         struct rte_dpaa2_driver *drv;
349
350         if (TAILQ_EMPTY(&rte_fslmc_bus.device_list))
351                 return 0;
352
353         ret = fslmc_vfio_setup_group();
354         if (ret) {
355                 DPAA2_BUS_ERR("Unable to setup VFIO %d", ret);
356                 return 0;
357         }
358
359         /* Map existing segments as well as, in case of hotpluggable memory,
360          * install callback handler.
361          */
362         ret = rte_fslmc_vfio_dmamap();
363         if (ret) {
364                 DPAA2_BUS_ERR("Unable to DMA map existing VAs: (%d)", ret);
365                 /* Not continuing ahead */
366                 DPAA2_BUS_ERR("FSLMC VFIO Mapping failed");
367                 return 0;
368         }
369
370         ret = fslmc_vfio_process_group();
371         if (ret) {
372                 DPAA2_BUS_ERR("Unable to setup devices %d", ret);
373                 return 0;
374         }
375
376         probe_all = rte_fslmc_bus.bus.conf.scan_mode != RTE_BUS_SCAN_WHITELIST;
377
378         TAILQ_FOREACH(dev, &rte_fslmc_bus.device_list, next) {
379                 TAILQ_FOREACH(drv, &rte_fslmc_bus.driver_list, next) {
380                         ret = rte_fslmc_match(drv, dev);
381                         if (ret)
382                                 continue;
383
384                         if (!drv->probe)
385                                 continue;
386
387                         if (dev->device.devargs &&
388                           dev->device.devargs->policy == RTE_DEV_BLACKLISTED) {
389                                 DPAA2_BUS_LOG(DEBUG, "%s Blacklisted, skipping",
390                                               dev->device.name);
391                                 continue;
392                         }
393
394                         if (probe_all ||
395                            (dev->device.devargs &&
396                            dev->device.devargs->policy ==
397                            RTE_DEV_WHITELISTED)) {
398                                 ret = drv->probe(drv, dev);
399                                 if (ret)
400                                         DPAA2_BUS_ERR("Unable to probe");
401                         }
402                         break;
403                 }
404         }
405
406         if (rte_eal_iova_mode() == RTE_IOVA_VA)
407                 dpaa2_virt_mode = 1;
408
409         return 0;
410 }
411
412 static struct rte_device *
413 rte_fslmc_find_device(const struct rte_device *start, rte_dev_cmp_t cmp,
414                       const void *data)
415 {
416         const struct rte_dpaa2_device *dstart;
417         struct rte_dpaa2_device *dev;
418
419         if (start != NULL) {
420                 dstart = RTE_DEV_TO_FSLMC_CONST(start);
421                 dev = TAILQ_NEXT(dstart, next);
422         } else {
423                 dev = TAILQ_FIRST(&rte_fslmc_bus.device_list);
424         }
425         while (dev != NULL) {
426                 if (cmp(&dev->device, data) == 0)
427                         return &dev->device;
428                 dev = TAILQ_NEXT(dev, next);
429         }
430
431         return NULL;
432 }
433
434 /*register a fslmc bus based dpaa2 driver */
435 void
436 rte_fslmc_driver_register(struct rte_dpaa2_driver *driver)
437 {
438         RTE_VERIFY(driver);
439
440         TAILQ_INSERT_TAIL(&rte_fslmc_bus.driver_list, driver, next);
441         /* Update Bus references */
442         driver->fslmc_bus = &rte_fslmc_bus;
443 }
444
445 /*un-register a fslmc bus based dpaa2 driver */
446 void
447 rte_fslmc_driver_unregister(struct rte_dpaa2_driver *driver)
448 {
449         struct rte_fslmc_bus *fslmc_bus;
450
451         fslmc_bus = driver->fslmc_bus;
452
453         TAILQ_REMOVE(&fslmc_bus->driver_list, driver, next);
454         /* Update Bus references */
455         driver->fslmc_bus = NULL;
456 }
457
458 /*
459  * All device has iova as va
460  */
461 static inline int
462 fslmc_all_device_support_iova(void)
463 {
464         int ret = 0;
465         struct rte_dpaa2_device *dev;
466         struct rte_dpaa2_driver *drv;
467
468         TAILQ_FOREACH(dev, &rte_fslmc_bus.device_list, next) {
469                 TAILQ_FOREACH(drv, &rte_fslmc_bus.driver_list, next) {
470                         ret = rte_fslmc_match(drv, dev);
471                         if (ret)
472                                 continue;
473                         /* if the driver is not supporting IOVA */
474                         if (!(drv->drv_flags & RTE_DPAA2_DRV_IOVA_AS_VA))
475                                 return 0;
476                 }
477         }
478         return 1;
479 }
480
481 /*
482  * Get iommu class of DPAA2 devices on the bus.
483  */
484 static enum rte_iova_mode
485 rte_dpaa2_get_iommu_class(void)
486 {
487         bool is_vfio_noiommu_enabled = 1;
488         bool has_iova_va;
489
490         if (TAILQ_EMPTY(&rte_fslmc_bus.device_list))
491                 return RTE_IOVA_DC;
492
493         /* check if all devices on the bus support Virtual addressing or not */
494         has_iova_va = fslmc_all_device_support_iova();
495
496 #ifdef VFIO_PRESENT
497         is_vfio_noiommu_enabled = rte_vfio_noiommu_is_enabled() == true ?
498                                                 true : false;
499 #endif
500
501         if (has_iova_va && !is_vfio_noiommu_enabled)
502                 return RTE_IOVA_VA;
503
504         return RTE_IOVA_PA;
505 }
506
507 struct rte_fslmc_bus rte_fslmc_bus = {
508         .bus = {
509                 .scan = rte_fslmc_scan,
510                 .probe = rte_fslmc_probe,
511                 .parse = rte_fslmc_parse,
512                 .find_device = rte_fslmc_find_device,
513                 .get_iommu_class = rte_dpaa2_get_iommu_class,
514         },
515         .device_list = TAILQ_HEAD_INITIALIZER(rte_fslmc_bus.device_list),
516         .driver_list = TAILQ_HEAD_INITIALIZER(rte_fslmc_bus.driver_list),
517         .device_count = {0},
518 };
519
520 RTE_REGISTER_BUS(FSLMC_BUS_NAME, rte_fslmc_bus.bus);
521
522 RTE_INIT(fslmc_init_log)
523 {
524         /* Bus level logs */
525         dpaa2_logtype_bus = rte_log_register("bus.fslmc");
526         if (dpaa2_logtype_bus >= 0)
527                 rte_log_set_level(dpaa2_logtype_bus, RTE_LOG_NOTICE);
528 }