New upstream version 17.11-rc3
[deb_dpdk.git] / drivers / bus / fslmc / fslmc_bus.c
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright 2016 NXP.
5  *
6  *   Redistribution and use in source and binary forms, with or without
7  *   modification, are permitted provided that the following conditions
8  *   are met:
9  *
10  *     * Redistributions of source code must retain the above copyright
11  *       notice, this list of conditions and the following disclaimer.
12  *     * Redistributions in binary form must reproduce the above copyright
13  *       notice, this list of conditions and the following disclaimer in
14  *       the documentation and/or other materials provided with the
15  *       distribution.
16  *     * Neither the name of NXP nor the names of its
17  *       contributors may be used to endorse or promote products derived
18  *       from this software without specific prior written permission.
19  *
20  *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21  *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22  *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23  *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24  *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25  *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26  *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27  *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28  *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29  *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30  *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  */
32
33 #include <string.h>
34 #include <dirent.h>
35 #include <stdbool.h>
36
37 #include <rte_log.h>
38 #include <rte_bus.h>
39 #include <rte_eal_memconfig.h>
40 #include <rte_malloc.h>
41 #include <rte_devargs.h>
42 #include <rte_memcpy.h>
43 #include <rte_ethdev.h>
44
45 #include <rte_fslmc.h>
46 #include <fslmc_vfio.h>
47
48 #define FSLMC_BUS_LOG(level, fmt, args...) \
49         RTE_LOG(level, EAL, fmt "\n", ##args)
50
51 #define VFIO_IOMMU_GROUP_PATH "/sys/kernel/iommu_groups"
52
53 struct rte_fslmc_bus rte_fslmc_bus;
54
55 static void
56 cleanup_fslmc_device_list(void)
57 {
58         struct rte_dpaa2_device *dev;
59         struct rte_dpaa2_device *t_dev;
60
61         TAILQ_FOREACH_SAFE(dev, &rte_fslmc_bus.device_list, next, t_dev) {
62                 TAILQ_REMOVE(&rte_fslmc_bus.device_list, dev, next);
63                 free(dev);
64                 dev = NULL;
65         }
66 }
67
68 static int
69 compare_dpaa2_devname(struct rte_dpaa2_device *dev1,
70                       struct rte_dpaa2_device *dev2)
71 {
72         int comp;
73
74         if (dev1->dev_type > dev2->dev_type) {
75                 comp = 1;
76         } else if (dev1->dev_type < dev2->dev_type) {
77                 comp = -1;
78         } else {
79                 /* Check the ID as types match */
80                 if (dev1->object_id > dev2->object_id)
81                         comp = 1;
82                 else if (dev1->object_id < dev2->object_id)
83                         comp = -1;
84                 else
85                         comp = 0; /* Duplicate device name */
86         }
87
88         return comp;
89 }
90
91 static void
92 insert_in_device_list(struct rte_dpaa2_device *newdev)
93 {
94         int comp, inserted = 0;
95         struct rte_dpaa2_device *dev = NULL;
96         struct rte_dpaa2_device *tdev = NULL;
97
98         TAILQ_FOREACH_SAFE(dev, &rte_fslmc_bus.device_list, next, tdev) {
99                 comp = compare_dpaa2_devname(newdev, dev);
100                 if (comp < 0) {
101                         TAILQ_INSERT_BEFORE(dev, newdev, next);
102                         inserted = 1;
103                         break;
104                 }
105         }
106
107         if (!inserted)
108                 TAILQ_INSERT_TAIL(&rte_fslmc_bus.device_list, newdev, next);
109 }
110
111 static int
112 scan_one_fslmc_device(char *dev_name)
113 {
114         char *dup_dev_name, *t_ptr;
115         struct rte_dpaa2_device *dev;
116
117         if (!dev_name)
118                 return -1;
119
120         /* Ignore the Container name itself */
121         if (!strncmp("dprc", dev_name, 4))
122                 return 0;
123
124         /* Creating a temporary copy to perform cut-parse over string */
125         dup_dev_name = strdup(dev_name);
126         if (!dup_dev_name) {
127                 FSLMC_BUS_LOG(ERR, "Out of memory.");
128                 return -ENOMEM;
129         }
130
131         /* For all other devices, we allocate rte_dpaa2_device.
132          * For those devices where there is no driver, probe would release
133          * the memory associated with the rte_dpaa2_device after necessary
134          * initialization.
135          */
136         dev = calloc(1, sizeof(struct rte_dpaa2_device));
137         if (!dev) {
138                 FSLMC_BUS_LOG(ERR, "Out of memory.");
139                 free(dup_dev_name);
140                 return -ENOMEM;
141         }
142
143         /* Parse the device name and ID */
144         t_ptr = strtok(dup_dev_name, ".");
145         if (!t_ptr) {
146                 FSLMC_BUS_LOG(ERR, "Incorrect device string observed.");
147                 goto cleanup;
148         }
149         if (!strncmp("dpni", t_ptr, 4))
150                 dev->dev_type = DPAA2_ETH;
151         else if (!strncmp("dpseci", t_ptr, 6))
152                 dev->dev_type = DPAA2_CRYPTO;
153         else if (!strncmp("dpcon", t_ptr, 5))
154                 dev->dev_type = DPAA2_CON;
155         else if (!strncmp("dpbp", t_ptr, 4))
156                 dev->dev_type = DPAA2_BPOOL;
157         else if (!strncmp("dpio", t_ptr, 4))
158                 dev->dev_type = DPAA2_IO;
159         else if (!strncmp("dpci", t_ptr, 5))
160                 dev->dev_type = DPAA2_CI;
161         else if (!strncmp("dpmcp", t_ptr, 5))
162                 dev->dev_type = DPAA2_MPORTAL;
163         else
164                 dev->dev_type = DPAA2_UNKNOWN;
165
166         t_ptr = strtok(NULL, ".");
167         if (!t_ptr) {
168                 FSLMC_BUS_LOG(ERR, "Incorrect device string observed (%s).",
169                               t_ptr);
170                 goto cleanup;
171         }
172
173         sscanf(t_ptr, "%hu", &dev->object_id);
174         dev->device.name = strdup(dev_name);
175         if (!dev->device.name) {
176                 FSLMC_BUS_LOG(ERR, "Out of memory.");
177                 goto cleanup;
178         }
179
180         /* Add device in the fslmc device list */
181         insert_in_device_list(dev);
182
183         /* Don't need the duplicated device filesystem entry anymore */
184         if (dup_dev_name)
185                 free(dup_dev_name);
186
187         return 0;
188 cleanup:
189         if (dup_dev_name)
190                 free(dup_dev_name);
191         if (dev)
192                 free(dev);
193         return -1;
194 }
195
196 static int
197 rte_fslmc_scan(void)
198 {
199         int ret;
200         int device_count = 0;
201         char fslmc_dirpath[PATH_MAX];
202         DIR *dir;
203         struct dirent *entry;
204         static int process_once;
205         int groupid;
206
207         if (process_once) {
208                 FSLMC_BUS_LOG(DEBUG,
209                               "Fslmc bus already scanned. Not rescanning");
210                 return 0;
211         }
212         process_once = 1;
213
214         ret = fslmc_get_container_group(&groupid);
215         if (ret != 0)
216                 goto scan_fail;
217
218         /* Scan devices on the group */
219         sprintf(fslmc_dirpath, "%s/%d/devices", VFIO_IOMMU_GROUP_PATH,
220                 groupid);
221         dir = opendir(fslmc_dirpath);
222         if (!dir) {
223                 FSLMC_BUS_LOG(ERR, "Unable to open VFIO group dir.");
224                 goto scan_fail;
225         }
226
227         while ((entry = readdir(dir)) != NULL) {
228                 if (entry->d_name[0] == '.' || entry->d_type != DT_LNK)
229                         continue;
230
231                 ret = scan_one_fslmc_device(entry->d_name);
232                 if (ret != 0) {
233                         /* Error in parsing directory - exit gracefully */
234                         goto scan_fail_cleanup;
235                 }
236                 device_count += 1;
237         }
238
239         FSLMC_BUS_LOG(INFO, "fslmc: Bus scan completed");
240
241         closedir(dir);
242         return 0;
243
244 scan_fail_cleanup:
245         closedir(dir);
246
247         /* Remove all devices in the list */
248         cleanup_fslmc_device_list();
249 scan_fail:
250         FSLMC_BUS_LOG(DEBUG, "FSLMC Bus Not Available. Skipping.");
251         /* Irrespective of failure, scan only return success */
252         return 0;
253 }
254
255 static int
256 rte_fslmc_match(struct rte_dpaa2_driver *dpaa2_drv,
257                 struct rte_dpaa2_device *dpaa2_dev)
258 {
259         if (dpaa2_drv->drv_type == dpaa2_dev->dev_type)
260                 return 0;
261
262         return 1;
263 }
264
265 static int
266 rte_fslmc_probe(void)
267 {
268         int ret = 0;
269         struct rte_dpaa2_device *dev;
270         struct rte_dpaa2_driver *drv;
271
272         if (TAILQ_EMPTY(&rte_fslmc_bus.device_list))
273                 return 0;
274
275         ret = fslmc_vfio_setup_group();
276         if (ret) {
277                 FSLMC_BUS_LOG(ERR, "Unable to setup VFIO %d", ret);
278                 return 0;
279         }
280
281         ret = fslmc_vfio_process_group();
282         if (ret) {
283                 FSLMC_BUS_LOG(ERR, "Unable to setup devices %d", ret);
284                 return 0;
285         }
286
287         TAILQ_FOREACH(dev, &rte_fslmc_bus.device_list, next) {
288                 TAILQ_FOREACH(drv, &rte_fslmc_bus.driver_list, next) {
289                         ret = rte_fslmc_match(drv, dev);
290                         if (ret)
291                                 continue;
292
293                         if (!drv->probe)
294                                 continue;
295
296                         ret = drv->probe(drv, dev);
297                         if (ret)
298                                 FSLMC_BUS_LOG(ERR, "Unable to probe.\n");
299                         break;
300                 }
301         }
302
303         return 0;
304 }
305
306 static struct rte_device *
307 rte_fslmc_find_device(const struct rte_device *start, rte_dev_cmp_t cmp,
308                       const void *data)
309 {
310         struct rte_dpaa2_device *dev;
311
312         TAILQ_FOREACH(dev, &rte_fslmc_bus.device_list, next) {
313                 if (start && &dev->device == start) {
314                         start = NULL;  /* starting point found */
315                         continue;
316                 }
317
318                 if (cmp(&dev->device, data) == 0)
319                         return &dev->device;
320         }
321
322         return NULL;
323 }
324
325 /*register a fslmc bus based dpaa2 driver */
326 void
327 rte_fslmc_driver_register(struct rte_dpaa2_driver *driver)
328 {
329         RTE_VERIFY(driver);
330
331         TAILQ_INSERT_TAIL(&rte_fslmc_bus.driver_list, driver, next);
332         /* Update Bus references */
333         driver->fslmc_bus = &rte_fslmc_bus;
334 }
335
336 /*un-register a fslmc bus based dpaa2 driver */
337 void
338 rte_fslmc_driver_unregister(struct rte_dpaa2_driver *driver)
339 {
340         struct rte_fslmc_bus *fslmc_bus;
341
342         fslmc_bus = driver->fslmc_bus;
343
344         TAILQ_REMOVE(&fslmc_bus->driver_list, driver, next);
345         /* Update Bus references */
346         driver->fslmc_bus = NULL;
347 }
348
349 /*
350  * Get iommu class of DPAA2 devices on the bus.
351  */
352 static enum rte_iova_mode
353 rte_dpaa2_get_iommu_class(void)
354 {
355         return RTE_IOVA_PA;
356 }
357
358 struct rte_fslmc_bus rte_fslmc_bus = {
359         .bus = {
360                 .scan = rte_fslmc_scan,
361                 .probe = rte_fslmc_probe,
362                 .find_device = rte_fslmc_find_device,
363                 .get_iommu_class = rte_dpaa2_get_iommu_class,
364         },
365         .device_list = TAILQ_HEAD_INITIALIZER(rte_fslmc_bus.device_list),
366         .driver_list = TAILQ_HEAD_INITIALIZER(rte_fslmc_bus.driver_list),
367 };
368
369 RTE_REGISTER_BUS(fslmc, rte_fslmc_bus.bus);