fde5f19f5912bb94a2948c673d9116f397949e2a
[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
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 void
97 dump_device_list(void)
98 {
99         struct rte_dpaa2_device *dev;
100         uint32_t global_log_level;
101         int local_log_level;
102
103         /* Only if the log level has been set to Debugging, print list */
104         global_log_level = rte_log_get_global_level();
105         local_log_level = rte_log_get_level(dpaa2_logtype_bus);
106         if (global_log_level == RTE_LOG_DEBUG ||
107             local_log_level == RTE_LOG_DEBUG) {
108                 DPAA2_BUS_DEBUG("List of devices scanned on bus:");
109                 TAILQ_FOREACH(dev, &rte_fslmc_bus.device_list, next) {
110                         DPAA2_BUS_DEBUG("%s", dev->device.name);
111                 }
112         }
113 }
114
115 static int
116 scan_one_fslmc_device(char *dev_name)
117 {
118         char *dup_dev_name, *t_ptr;
119         struct rte_dpaa2_device *dev;
120
121         if (!dev_name)
122                 return -1;
123
124         /* Ignore the Container name itself */
125         if (!strncmp("dprc", dev_name, 4))
126                 return 0;
127
128         /* Creating a temporary copy to perform cut-parse over string */
129         dup_dev_name = strdup(dev_name);
130         if (!dup_dev_name) {
131                 DPAA2_BUS_ERR("Unable to allocate device name memory");
132                 return -ENOMEM;
133         }
134
135         /* For all other devices, we allocate rte_dpaa2_device.
136          * For those devices where there is no driver, probe would release
137          * the memory associated with the rte_dpaa2_device after necessary
138          * initialization.
139          */
140         dev = calloc(1, sizeof(struct rte_dpaa2_device));
141         if (!dev) {
142                 DPAA2_BUS_ERR("Unable to allocate device object");
143                 free(dup_dev_name);
144                 return -ENOMEM;
145         }
146
147         /* Parse the device name and ID */
148         t_ptr = strtok(dup_dev_name, ".");
149         if (!t_ptr) {
150                 DPAA2_BUS_ERR("Incorrect device name observed");
151                 goto cleanup;
152         }
153         if (!strncmp("dpni", t_ptr, 4))
154                 dev->dev_type = DPAA2_ETH;
155         else if (!strncmp("dpseci", t_ptr, 6))
156                 dev->dev_type = DPAA2_CRYPTO;
157         else if (!strncmp("dpcon", t_ptr, 5))
158                 dev->dev_type = DPAA2_CON;
159         else if (!strncmp("dpbp", t_ptr, 4))
160                 dev->dev_type = DPAA2_BPOOL;
161         else if (!strncmp("dpio", t_ptr, 4))
162                 dev->dev_type = DPAA2_IO;
163         else if (!strncmp("dpci", t_ptr, 4))
164                 dev->dev_type = DPAA2_CI;
165         else if (!strncmp("dpmcp", t_ptr, 5))
166                 dev->dev_type = DPAA2_MPORTAL;
167         else if (!strncmp("dpdmai", t_ptr, 6))
168                 dev->dev_type = DPAA2_QDMA;
169         else
170                 dev->dev_type = DPAA2_UNKNOWN;
171
172         /* Update the device found into the device_count table */
173         rte_fslmc_bus.device_count[dev->dev_type]++;
174
175         t_ptr = strtok(NULL, ".");
176         if (!t_ptr) {
177                 DPAA2_BUS_ERR("Incorrect device string observed (%s)", t_ptr);
178                 goto cleanup;
179         }
180
181         sscanf(t_ptr, "%hu", &dev->object_id);
182         dev->device.name = strdup(dev_name);
183         if (!dev->device.name) {
184                 DPAA2_BUS_ERR("Unable to clone device name. Out of memory");
185                 goto cleanup;
186         }
187
188         /* Add device in the fslmc device list */
189         insert_in_device_list(dev);
190
191         /* Don't need the duplicated device filesystem entry anymore */
192         if (dup_dev_name)
193                 free(dup_dev_name);
194
195         return 0;
196 cleanup:
197         if (dup_dev_name)
198                 free(dup_dev_name);
199         if (dev)
200                 free(dev);
201         return -1;
202 }
203
204 static int
205 rte_fslmc_scan(void)
206 {
207         int ret;
208         int device_count = 0;
209         char fslmc_dirpath[PATH_MAX];
210         DIR *dir;
211         struct dirent *entry;
212         static int process_once;
213         int groupid;
214
215         if (process_once) {
216                 DPAA2_BUS_DEBUG("Fslmc bus already scanned. Not rescanning");
217                 return 0;
218         }
219         process_once = 1;
220
221         ret = fslmc_get_container_group(&groupid);
222         if (ret != 0)
223                 goto scan_fail;
224
225         /* Scan devices on the group */
226         sprintf(fslmc_dirpath, "%s/%d/devices", VFIO_IOMMU_GROUP_PATH,
227                 groupid);
228         dir = opendir(fslmc_dirpath);
229         if (!dir) {
230                 DPAA2_BUS_ERR("Unable to open VFIO group directory");
231                 goto scan_fail;
232         }
233
234         while ((entry = readdir(dir)) != NULL) {
235                 if (entry->d_name[0] == '.' || entry->d_type != DT_LNK)
236                         continue;
237
238                 ret = scan_one_fslmc_device(entry->d_name);
239                 if (ret != 0) {
240                         /* Error in parsing directory - exit gracefully */
241                         goto scan_fail_cleanup;
242                 }
243                 device_count += 1;
244         }
245
246         closedir(dir);
247
248         DPAA2_BUS_INFO("FSLMC Bus scan completed");
249         /* If debugging is enabled, device list is dumped to log output */
250         dump_device_list();
251
252         return 0;
253
254 scan_fail_cleanup:
255         closedir(dir);
256
257         /* Remove all devices in the list */
258         cleanup_fslmc_device_list();
259 scan_fail:
260         DPAA2_BUS_DEBUG("FSLMC Bus Not Available. Skipping");
261         /* Irrespective of failure, scan only return success */
262         return 0;
263 }
264
265 static int
266 rte_fslmc_match(struct rte_dpaa2_driver *dpaa2_drv,
267                 struct rte_dpaa2_device *dpaa2_dev)
268 {
269         if (dpaa2_drv->drv_type == dpaa2_dev->dev_type)
270                 return 0;
271
272         return 1;
273 }
274
275 static int
276 rte_fslmc_probe(void)
277 {
278         int ret = 0;
279         struct rte_dpaa2_device *dev;
280         struct rte_dpaa2_driver *drv;
281
282         if (TAILQ_EMPTY(&rte_fslmc_bus.device_list))
283                 return 0;
284
285         ret = fslmc_vfio_setup_group();
286         if (ret) {
287                 DPAA2_BUS_ERR("Unable to setup VFIO %d", ret);
288                 return 0;
289         }
290
291         /* Map existing segments as well as, in case of hotpluggable memory,
292          * install callback handler.
293          */
294         ret = rte_fslmc_vfio_dmamap();
295         if (ret) {
296                 DPAA2_BUS_ERR("Unable to DMA map existing VAs: (%d)", ret);
297                 /* Not continuing ahead */
298                 DPAA2_BUS_ERR("FSLMC VFIO Mapping failed");
299                 return 0;
300         }
301
302         ret = fslmc_vfio_process_group();
303         if (ret) {
304                 DPAA2_BUS_ERR("Unable to setup devices %d", ret);
305                 return 0;
306         }
307
308         TAILQ_FOREACH(dev, &rte_fslmc_bus.device_list, next) {
309                 TAILQ_FOREACH(drv, &rte_fslmc_bus.driver_list, next) {
310                         ret = rte_fslmc_match(drv, dev);
311                         if (ret)
312                                 continue;
313
314                         if (!drv->probe)
315                                 continue;
316
317                         ret = drv->probe(drv, dev);
318                         if (ret)
319                                 DPAA2_BUS_ERR("Unable to probe");
320                         break;
321                 }
322         }
323
324         if (rte_eal_iova_mode() == RTE_IOVA_VA)
325                 dpaa2_virt_mode = 1;
326
327         return 0;
328 }
329
330 static struct rte_device *
331 rte_fslmc_find_device(const struct rte_device *start, rte_dev_cmp_t cmp,
332                       const void *data)
333 {
334         struct rte_dpaa2_device *dev;
335
336         TAILQ_FOREACH(dev, &rte_fslmc_bus.device_list, next) {
337                 if (start != NULL) {
338                         if (&dev->device == start)
339                                 start = NULL;  /* starting point found */
340                         continue;
341                 }
342
343                 if (cmp(&dev->device, data) == 0)
344                         return &dev->device;
345         }
346
347         return NULL;
348 }
349
350 /*register a fslmc bus based dpaa2 driver */
351 void
352 rte_fslmc_driver_register(struct rte_dpaa2_driver *driver)
353 {
354         RTE_VERIFY(driver);
355
356         TAILQ_INSERT_TAIL(&rte_fslmc_bus.driver_list, driver, next);
357         /* Update Bus references */
358         driver->fslmc_bus = &rte_fslmc_bus;
359 }
360
361 /*un-register a fslmc bus based dpaa2 driver */
362 void
363 rte_fslmc_driver_unregister(struct rte_dpaa2_driver *driver)
364 {
365         struct rte_fslmc_bus *fslmc_bus;
366
367         fslmc_bus = driver->fslmc_bus;
368
369         TAILQ_REMOVE(&fslmc_bus->driver_list, driver, next);
370         /* Update Bus references */
371         driver->fslmc_bus = NULL;
372 }
373
374 /*
375  * All device has iova as va
376  */
377 static inline int
378 fslmc_all_device_support_iova(void)
379 {
380         int ret = 0;
381         struct rte_dpaa2_device *dev;
382         struct rte_dpaa2_driver *drv;
383
384         TAILQ_FOREACH(dev, &rte_fslmc_bus.device_list, next) {
385                 TAILQ_FOREACH(drv, &rte_fslmc_bus.driver_list, next) {
386                         ret = rte_fslmc_match(drv, dev);
387                         if (ret)
388                                 continue;
389                         /* if the driver is not supporting IOVA */
390                         if (!(drv->drv_flags & RTE_DPAA2_DRV_IOVA_AS_VA))
391                                 return 0;
392                 }
393         }
394         return 1;
395 }
396
397 /*
398  * Get iommu class of DPAA2 devices on the bus.
399  */
400 static enum rte_iova_mode
401 rte_dpaa2_get_iommu_class(void)
402 {
403         bool is_vfio_noiommu_enabled = 1;
404         bool has_iova_va;
405
406         if (TAILQ_EMPTY(&rte_fslmc_bus.device_list))
407                 return RTE_IOVA_DC;
408
409         /* check if all devices on the bus support Virtual addressing or not */
410         has_iova_va = fslmc_all_device_support_iova();
411
412 #ifdef VFIO_PRESENT
413         is_vfio_noiommu_enabled = rte_vfio_noiommu_is_enabled() == true ?
414                                                 true : false;
415 #endif
416
417         if (has_iova_va && !is_vfio_noiommu_enabled)
418                 return RTE_IOVA_VA;
419
420         return RTE_IOVA_PA;
421 }
422
423 struct rte_fslmc_bus rte_fslmc_bus = {
424         .bus = {
425                 .scan = rte_fslmc_scan,
426                 .probe = rte_fslmc_probe,
427                 .find_device = rte_fslmc_find_device,
428                 .get_iommu_class = rte_dpaa2_get_iommu_class,
429         },
430         .device_list = TAILQ_HEAD_INITIALIZER(rte_fslmc_bus.device_list),
431         .driver_list = TAILQ_HEAD_INITIALIZER(rte_fslmc_bus.driver_list),
432         .device_count = {0},
433 };
434
435 RTE_REGISTER_BUS(fslmc, rte_fslmc_bus.bus);
436
437 RTE_INIT(fslmc_init_log);
438 static void
439 fslmc_init_log(void)
440 {
441         /* Bus level logs */
442         dpaa2_logtype_bus = rte_log_register("bus.fslmc");
443         if (dpaa2_logtype_bus >= 0)
444                 rte_log_set_level(dpaa2_logtype_bus, RTE_LOG_NOTICE);
445 }