bus/fslmc: move VFIO DMA map into bus probe
[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
168                 dev->dev_type = DPAA2_UNKNOWN;
169
170         /* Update the device found into the device_count table */
171         rte_fslmc_bus.device_count[dev->dev_type]++;
172
173         t_ptr = strtok(NULL, ".");
174         if (!t_ptr) {
175                 DPAA2_BUS_ERR("Incorrect device string observed (%s)", t_ptr);
176                 goto cleanup;
177         }
178
179         sscanf(t_ptr, "%hu", &dev->object_id);
180         dev->device.name = strdup(dev_name);
181         if (!dev->device.name) {
182                 DPAA2_BUS_ERR("Unable to clone device name. Out of memory");
183                 goto cleanup;
184         }
185
186         /* Add device in the fslmc device list */
187         insert_in_device_list(dev);
188
189         /* Don't need the duplicated device filesystem entry anymore */
190         if (dup_dev_name)
191                 free(dup_dev_name);
192
193         return 0;
194 cleanup:
195         if (dup_dev_name)
196                 free(dup_dev_name);
197         if (dev)
198                 free(dev);
199         return -1;
200 }
201
202 static int
203 rte_fslmc_scan(void)
204 {
205         int ret;
206         int device_count = 0;
207         char fslmc_dirpath[PATH_MAX];
208         DIR *dir;
209         struct dirent *entry;
210         static int process_once;
211         int groupid;
212
213         if (process_once) {
214                 DPAA2_BUS_DEBUG("Fslmc bus already scanned. Not rescanning");
215                 return 0;
216         }
217         process_once = 1;
218
219         ret = fslmc_get_container_group(&groupid);
220         if (ret != 0)
221                 goto scan_fail;
222
223         /* Scan devices on the group */
224         sprintf(fslmc_dirpath, "%s/%d/devices", VFIO_IOMMU_GROUP_PATH,
225                 groupid);
226         dir = opendir(fslmc_dirpath);
227         if (!dir) {
228                 DPAA2_BUS_ERR("Unable to open VFIO group directory");
229                 goto scan_fail;
230         }
231
232         while ((entry = readdir(dir)) != NULL) {
233                 if (entry->d_name[0] == '.' || entry->d_type != DT_LNK)
234                         continue;
235
236                 ret = scan_one_fslmc_device(entry->d_name);
237                 if (ret != 0) {
238                         /* Error in parsing directory - exit gracefully */
239                         goto scan_fail_cleanup;
240                 }
241                 device_count += 1;
242         }
243
244         closedir(dir);
245
246         DPAA2_BUS_INFO("FSLMC Bus scan completed");
247         /* If debugging is enabled, device list is dumped to log output */
248         dump_device_list();
249
250         return 0;
251
252 scan_fail_cleanup:
253         closedir(dir);
254
255         /* Remove all devices in the list */
256         cleanup_fslmc_device_list();
257 scan_fail:
258         DPAA2_BUS_INFO("FSLMC Bus Not Available. Skipping");
259         /* Irrespective of failure, scan only return success */
260         return 0;
261 }
262
263 static int
264 rte_fslmc_match(struct rte_dpaa2_driver *dpaa2_drv,
265                 struct rte_dpaa2_device *dpaa2_dev)
266 {
267         if (dpaa2_drv->drv_type == dpaa2_dev->dev_type)
268                 return 0;
269
270         return 1;
271 }
272
273 static int
274 rte_fslmc_probe(void)
275 {
276         int ret = 0;
277         struct rte_dpaa2_device *dev;
278         struct rte_dpaa2_driver *drv;
279
280         if (TAILQ_EMPTY(&rte_fslmc_bus.device_list))
281                 return 0;
282
283         ret = fslmc_vfio_setup_group();
284         if (ret) {
285                 DPAA2_BUS_ERR("Unable to setup VFIO %d", ret);
286                 return 0;
287         }
288
289         /* Map existing segments as well as, in case of hotpluggable memory,
290          * install callback handler.
291          */
292         ret = rte_fslmc_vfio_dmamap();
293         if (ret) {
294                 DPAA2_BUS_ERR("Unable to DMA map existing VAs: (%d)", ret);
295                 /* Not continuing ahead */
296                 DPAA2_BUS_ERR("FSLMC VFIO Mapping failed");
297                 return 0;
298         }
299
300         ret = fslmc_vfio_process_group();
301         if (ret) {
302                 DPAA2_BUS_ERR("Unable to setup devices %d", ret);
303                 return 0;
304         }
305
306         TAILQ_FOREACH(dev, &rte_fslmc_bus.device_list, next) {
307                 TAILQ_FOREACH(drv, &rte_fslmc_bus.driver_list, next) {
308                         ret = rte_fslmc_match(drv, dev);
309                         if (ret)
310                                 continue;
311
312                         if (!drv->probe)
313                                 continue;
314
315                         ret = drv->probe(drv, dev);
316                         if (ret)
317                                 DPAA2_BUS_ERR("Unable to probe");
318                         break;
319                 }
320         }
321
322         if (rte_eal_iova_mode() == RTE_IOVA_VA)
323                 dpaa2_virt_mode = 1;
324
325         return 0;
326 }
327
328 static struct rte_device *
329 rte_fslmc_find_device(const struct rte_device *start, rte_dev_cmp_t cmp,
330                       const void *data)
331 {
332         struct rte_dpaa2_device *dev;
333
334         TAILQ_FOREACH(dev, &rte_fslmc_bus.device_list, next) {
335                 if (start != NULL) {
336                         if (&dev->device == start)
337                                 start = NULL;  /* starting point found */
338                         continue;
339                 }
340
341                 if (cmp(&dev->device, data) == 0)
342                         return &dev->device;
343         }
344
345         return NULL;
346 }
347
348 /*register a fslmc bus based dpaa2 driver */
349 void
350 rte_fslmc_driver_register(struct rte_dpaa2_driver *driver)
351 {
352         RTE_VERIFY(driver);
353
354         TAILQ_INSERT_TAIL(&rte_fslmc_bus.driver_list, driver, next);
355         /* Update Bus references */
356         driver->fslmc_bus = &rte_fslmc_bus;
357 }
358
359 /*un-register a fslmc bus based dpaa2 driver */
360 void
361 rte_fslmc_driver_unregister(struct rte_dpaa2_driver *driver)
362 {
363         struct rte_fslmc_bus *fslmc_bus;
364
365         fslmc_bus = driver->fslmc_bus;
366
367         TAILQ_REMOVE(&fslmc_bus->driver_list, driver, next);
368         /* Update Bus references */
369         driver->fslmc_bus = NULL;
370 }
371
372 /*
373  * All device has iova as va
374  */
375 static inline int
376 fslmc_all_device_support_iova(void)
377 {
378         int ret = 0;
379         struct rte_dpaa2_device *dev;
380         struct rte_dpaa2_driver *drv;
381
382         TAILQ_FOREACH(dev, &rte_fslmc_bus.device_list, next) {
383                 TAILQ_FOREACH(drv, &rte_fslmc_bus.driver_list, next) {
384                         ret = rte_fslmc_match(drv, dev);
385                         if (ret)
386                                 continue;
387                         /* if the driver is not supporting IOVA */
388                         if (!(drv->drv_flags & RTE_DPAA2_DRV_IOVA_AS_VA))
389                                 return 0;
390                 }
391         }
392         return 1;
393 }
394
395 /*
396  * Get iommu class of DPAA2 devices on the bus.
397  */
398 static enum rte_iova_mode
399 rte_dpaa2_get_iommu_class(void)
400 {
401         bool is_vfio_noiommu_enabled = 1;
402         bool has_iova_va;
403
404         if (TAILQ_EMPTY(&rte_fslmc_bus.device_list))
405                 return RTE_IOVA_DC;
406
407         /* check if all devices on the bus support Virtual addressing or not */
408         has_iova_va = fslmc_all_device_support_iova();
409
410 #ifdef VFIO_PRESENT
411         is_vfio_noiommu_enabled = rte_vfio_noiommu_is_enabled() == true ?
412                                                 true : false;
413 #endif
414
415         if (has_iova_va && !is_vfio_noiommu_enabled)
416                 return RTE_IOVA_VA;
417
418         return RTE_IOVA_PA;
419 }
420
421 struct rte_fslmc_bus rte_fslmc_bus = {
422         .bus = {
423                 .scan = rte_fslmc_scan,
424                 .probe = rte_fslmc_probe,
425                 .find_device = rte_fslmc_find_device,
426                 .get_iommu_class = rte_dpaa2_get_iommu_class,
427         },
428         .device_list = TAILQ_HEAD_INITIALIZER(rte_fslmc_bus.device_list),
429         .driver_list = TAILQ_HEAD_INITIALIZER(rte_fslmc_bus.driver_list),
430         .device_count = {0},
431 };
432
433 RTE_REGISTER_BUS(fslmc, rte_fslmc_bus.bus);
434
435 RTE_INIT(fslmc_init_log);
436 static void
437 fslmc_init_log(void)
438 {
439         /* Bus level logs */
440         dpaa2_logtype_bus = rte_log_register("bus.fslmc");
441         if (dpaa2_logtype_bus >= 0)
442                 rte_log_set_level(dpaa2_logtype_bus, RTE_LOG_NOTICE);
443 }