d50c303ba7a3872473cfc6a04bba60b7161e5fec
[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 scan_one_fslmc_device(char *dev_name)
70 {
71         char *dup_dev_name, *t_ptr;
72         struct rte_dpaa2_device *dev;
73
74         if (!dev_name)
75                 return -1;
76
77         /* Ignore the Container name itself */
78         if (!strncmp("dprc", dev_name, 4))
79                 return 0;
80
81         /* Creating a temporary copy to perform cut-parse over string */
82         dup_dev_name = strdup(dev_name);
83         if (!dup_dev_name) {
84                 FSLMC_BUS_LOG(ERR, "Out of memory.");
85                 return -ENOMEM;
86         }
87
88         /* For all other devices, we allocate rte_dpaa2_device.
89          * For those devices where there is no driver, probe would release
90          * the memory associated with the rte_dpaa2_device after necessary
91          * initialization.
92          */
93         dev = calloc(1, sizeof(struct rte_dpaa2_device));
94         if (!dev) {
95                 FSLMC_BUS_LOG(ERR, "Out of memory.");
96                 free(dup_dev_name);
97                 return -ENOMEM;
98         }
99
100         /* Parse the device name and ID */
101         t_ptr = strtok(dup_dev_name, ".");
102         if (!t_ptr) {
103                 FSLMC_BUS_LOG(ERR, "Incorrect device string observed.");
104                 goto cleanup;
105         }
106         if (!strncmp("dpni", t_ptr, 4))
107                 dev->dev_type = DPAA2_ETH;
108         else if (!strncmp("dpseci", t_ptr, 6))
109                 dev->dev_type = DPAA2_CRYPTO;
110         else if (!strncmp("dpcon", t_ptr, 5))
111                 dev->dev_type = DPAA2_CON;
112         else if (!strncmp("dpbp", t_ptr, 4))
113                 dev->dev_type = DPAA2_BPOOL;
114         else if (!strncmp("dpio", t_ptr, 4))
115                 dev->dev_type = DPAA2_IO;
116         else if (!strncmp("dpci", t_ptr, 5))
117                 dev->dev_type = DPAA2_CI;
118         else if (!strncmp("dpmcp", t_ptr, 5))
119                 dev->dev_type = DPAA2_MPORTAL;
120         else
121                 dev->dev_type = DPAA2_UNKNOWN;
122
123         t_ptr = strtok(NULL, ".");
124         if (!t_ptr) {
125                 FSLMC_BUS_LOG(ERR, "Incorrect device string observed (%s).",
126                               t_ptr);
127                 goto cleanup;
128         }
129
130         sscanf(t_ptr, "%hu", &dev->object_id);
131         dev->device.name = strdup(dev_name);
132         if (!dev->device.name) {
133                 FSLMC_BUS_LOG(ERR, "Out of memory.");
134                 goto cleanup;
135         }
136
137         /* Add device in the fslmc device list */
138         TAILQ_INSERT_TAIL(&rte_fslmc_bus.device_list, dev, next);
139
140         /* Don't need the duplicated device filesystem entry anymore */
141         if (dup_dev_name)
142                 free(dup_dev_name);
143
144         return 0;
145 cleanup:
146         if (dup_dev_name)
147                 free(dup_dev_name);
148         if (dev)
149                 free(dev);
150         return -1;
151 }
152
153 static int
154 rte_fslmc_scan(void)
155 {
156         int ret;
157         int device_count = 0;
158         char fslmc_dirpath[PATH_MAX];
159         DIR *dir;
160         struct dirent *entry;
161         static int process_once;
162         int groupid;
163
164         if (process_once) {
165                 FSLMC_BUS_LOG(DEBUG,
166                               "Fslmc bus already scanned. Not rescanning");
167                 return 0;
168         }
169         process_once = 1;
170
171         ret = fslmc_get_container_group(&groupid);
172         if (ret != 0)
173                 goto scan_fail;
174
175         /* Scan devices on the group */
176         sprintf(fslmc_dirpath, "%s/%d/devices", VFIO_IOMMU_GROUP_PATH,
177                 groupid);
178         dir = opendir(fslmc_dirpath);
179         if (!dir) {
180                 FSLMC_BUS_LOG(ERR, "Unable to open VFIO group dir.");
181                 goto scan_fail;
182         }
183
184         while ((entry = readdir(dir)) != NULL) {
185                 if (entry->d_name[0] == '.' || entry->d_type != DT_LNK)
186                         continue;
187
188                 ret = scan_one_fslmc_device(entry->d_name);
189                 if (ret != 0) {
190                         /* Error in parsing directory - exit gracefully */
191                         goto scan_fail_cleanup;
192                 }
193                 device_count += 1;
194         }
195
196         FSLMC_BUS_LOG(INFO, "fslmc: Bus scan completed");
197
198         closedir(dir);
199         return 0;
200
201 scan_fail_cleanup:
202         closedir(dir);
203
204         /* Remove all devices in the list */
205         cleanup_fslmc_device_list();
206 scan_fail:
207         FSLMC_BUS_LOG(DEBUG, "FSLMC Bus Not Available. Skipping.");
208         /* Irrespective of failure, scan only return success */
209         return 0;
210 }
211
212 static int
213 rte_fslmc_match(struct rte_dpaa2_driver *dpaa2_drv,
214                 struct rte_dpaa2_device *dpaa2_dev)
215 {
216         if (dpaa2_drv->drv_type == dpaa2_dev->dev_type)
217                 return 0;
218
219         return 1;
220 }
221
222 static int
223 rte_fslmc_probe(void)
224 {
225         int ret = 0;
226         struct rte_dpaa2_device *dev;
227         struct rte_dpaa2_driver *drv;
228
229         if (TAILQ_EMPTY(&rte_fslmc_bus.device_list))
230                 return 0;
231
232         ret = fslmc_vfio_setup_group();
233         if (ret) {
234                 FSLMC_BUS_LOG(ERR, "Unable to setup VFIO %d", ret);
235                 return 0;
236         }
237
238         ret = fslmc_vfio_process_group();
239         if (ret) {
240                 FSLMC_BUS_LOG(ERR, "Unable to setup devices %d", ret);
241                 return 0;
242         }
243
244         TAILQ_FOREACH(dev, &rte_fslmc_bus.device_list, next) {
245                 TAILQ_FOREACH(drv, &rte_fslmc_bus.driver_list, next) {
246                         ret = rte_fslmc_match(drv, dev);
247                         if (ret)
248                                 continue;
249
250                         if (!drv->probe)
251                                 continue;
252
253                         ret = drv->probe(drv, dev);
254                         if (ret)
255                                 FSLMC_BUS_LOG(ERR, "Unable to probe.\n");
256                         break;
257                 }
258         }
259
260         return 0;
261 }
262
263 static struct rte_device *
264 rte_fslmc_find_device(const struct rte_device *start, rte_dev_cmp_t cmp,
265                       const void *data)
266 {
267         struct rte_dpaa2_device *dev;
268
269         TAILQ_FOREACH(dev, &rte_fslmc_bus.device_list, next) {
270                 if (start && &dev->device == start) {
271                         start = NULL;  /* starting point found */
272                         continue;
273                 }
274
275                 if (cmp(&dev->device, data) == 0)
276                         return &dev->device;
277         }
278
279         return NULL;
280 }
281
282 /*register a fslmc bus based dpaa2 driver */
283 void
284 rte_fslmc_driver_register(struct rte_dpaa2_driver *driver)
285 {
286         RTE_VERIFY(driver);
287
288         TAILQ_INSERT_TAIL(&rte_fslmc_bus.driver_list, driver, next);
289         /* Update Bus references */
290         driver->fslmc_bus = &rte_fslmc_bus;
291 }
292
293 /*un-register a fslmc bus based dpaa2 driver */
294 void
295 rte_fslmc_driver_unregister(struct rte_dpaa2_driver *driver)
296 {
297         struct rte_fslmc_bus *fslmc_bus;
298
299         fslmc_bus = driver->fslmc_bus;
300
301         TAILQ_REMOVE(&fslmc_bus->driver_list, driver, next);
302         /* Update Bus references */
303         driver->fslmc_bus = NULL;
304 }
305
306 struct rte_fslmc_bus rte_fslmc_bus = {
307         .bus = {
308                 .scan = rte_fslmc_scan,
309                 .probe = rte_fslmc_probe,
310                 .find_device = rte_fslmc_find_device,
311         },
312         .device_list = TAILQ_HEAD_INITIALIZER(rte_fslmc_bus.device_list),
313         .driver_list = TAILQ_HEAD_INITIALIZER(rte_fslmc_bus.driver_list),
314 };
315
316 RTE_REGISTER_BUS(fslmc, rte_fslmc_bus.bus);