bus/fslmc: fix find device start condition
[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         ret = fslmc_vfio_process_group();
290         if (ret) {
291                 DPAA2_BUS_ERR("Unable to setup devices %d", ret);
292                 return 0;
293         }
294
295         TAILQ_FOREACH(dev, &rte_fslmc_bus.device_list, next) {
296                 TAILQ_FOREACH(drv, &rte_fslmc_bus.driver_list, next) {
297                         ret = rte_fslmc_match(drv, dev);
298                         if (ret)
299                                 continue;
300
301                         if (!drv->probe)
302                                 continue;
303
304                         ret = drv->probe(drv, dev);
305                         if (ret)
306                                 DPAA2_BUS_ERR("Unable to probe");
307                         break;
308                 }
309         }
310
311         if (rte_eal_iova_mode() == RTE_IOVA_VA)
312                 dpaa2_virt_mode = 1;
313
314         return 0;
315 }
316
317 static struct rte_device *
318 rte_fslmc_find_device(const struct rte_device *start, rte_dev_cmp_t cmp,
319                       const void *data)
320 {
321         struct rte_dpaa2_device *dev;
322
323         TAILQ_FOREACH(dev, &rte_fslmc_bus.device_list, next) {
324                 if (start != NULL) {
325                         if (&dev->device == start)
326                                 start = NULL;  /* starting point found */
327                         continue;
328                 }
329
330                 if (cmp(&dev->device, data) == 0)
331                         return &dev->device;
332         }
333
334         return NULL;
335 }
336
337 /*register a fslmc bus based dpaa2 driver */
338 void
339 rte_fslmc_driver_register(struct rte_dpaa2_driver *driver)
340 {
341         RTE_VERIFY(driver);
342
343         TAILQ_INSERT_TAIL(&rte_fslmc_bus.driver_list, driver, next);
344         /* Update Bus references */
345         driver->fslmc_bus = &rte_fslmc_bus;
346 }
347
348 /*un-register a fslmc bus based dpaa2 driver */
349 void
350 rte_fslmc_driver_unregister(struct rte_dpaa2_driver *driver)
351 {
352         struct rte_fslmc_bus *fslmc_bus;
353
354         fslmc_bus = driver->fslmc_bus;
355
356         TAILQ_REMOVE(&fslmc_bus->driver_list, driver, next);
357         /* Update Bus references */
358         driver->fslmc_bus = NULL;
359 }
360
361 /*
362  * All device has iova as va
363  */
364 static inline int
365 fslmc_all_device_support_iova(void)
366 {
367         int ret = 0;
368         struct rte_dpaa2_device *dev;
369         struct rte_dpaa2_driver *drv;
370
371         TAILQ_FOREACH(dev, &rte_fslmc_bus.device_list, next) {
372                 TAILQ_FOREACH(drv, &rte_fslmc_bus.driver_list, next) {
373                         ret = rte_fslmc_match(drv, dev);
374                         if (ret)
375                                 continue;
376                         /* if the driver is not supporting IOVA */
377                         if (!(drv->drv_flags & RTE_DPAA2_DRV_IOVA_AS_VA))
378                                 return 0;
379                 }
380         }
381         return 1;
382 }
383
384 /*
385  * Get iommu class of DPAA2 devices on the bus.
386  */
387 static enum rte_iova_mode
388 rte_dpaa2_get_iommu_class(void)
389 {
390         bool is_vfio_noiommu_enabled = 1;
391         bool has_iova_va;
392
393         if (TAILQ_EMPTY(&rte_fslmc_bus.device_list))
394                 return RTE_IOVA_DC;
395
396         /* check if all devices on the bus support Virtual addressing or not */
397         has_iova_va = fslmc_all_device_support_iova();
398
399 #ifdef VFIO_PRESENT
400         is_vfio_noiommu_enabled = rte_vfio_noiommu_is_enabled() == true ?
401                                                 true : false;
402 #endif
403
404         if (has_iova_va && !is_vfio_noiommu_enabled)
405                 return RTE_IOVA_VA;
406
407         return RTE_IOVA_PA;
408 }
409
410 struct rte_fslmc_bus rte_fslmc_bus = {
411         .bus = {
412                 .scan = rte_fslmc_scan,
413                 .probe = rte_fslmc_probe,
414                 .find_device = rte_fslmc_find_device,
415                 .get_iommu_class = rte_dpaa2_get_iommu_class,
416         },
417         .device_list = TAILQ_HEAD_INITIALIZER(rte_fslmc_bus.device_list),
418         .driver_list = TAILQ_HEAD_INITIALIZER(rte_fslmc_bus.driver_list),
419         .device_count = {0},
420 };
421
422 RTE_REGISTER_BUS(fslmc, rte_fslmc_bus.bus);
423
424 RTE_INIT(fslmc_init_log);
425 static void
426 fslmc_init_log(void)
427 {
428         /* Bus level logs */
429         dpaa2_logtype_bus = rte_log_register("bus.fslmc");
430         if (dpaa2_logtype_bus >= 0)
431                 rte_log_set_level(dpaa2_logtype_bus, RTE_LOG_NOTICE);
432 }