6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
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
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.
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.
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>
45 #include <rte_fslmc.h>
46 #include <fslmc_vfio.h>
48 #define FSLMC_BUS_LOG(level, fmt, args...) \
49 RTE_LOG(level, EAL, fmt "\n", ##args)
51 #define VFIO_IOMMU_GROUP_PATH "/sys/kernel/iommu_groups"
53 struct rte_fslmc_bus rte_fslmc_bus;
56 cleanup_fslmc_device_list(void)
58 struct rte_dpaa2_device *dev;
59 struct rte_dpaa2_device *t_dev;
61 TAILQ_FOREACH_SAFE(dev, &rte_fslmc_bus.device_list, next, t_dev) {
62 TAILQ_REMOVE(&rte_fslmc_bus.device_list, dev, next);
69 scan_one_fslmc_device(char *dev_name)
71 char *dup_dev_name, *t_ptr;
72 struct rte_dpaa2_device *dev;
77 /* Ignore the Container name itself */
78 if (!strncmp("dprc", dev_name, 4))
81 /* Creating a temporary copy to perform cut-parse over string */
82 dup_dev_name = strdup(dev_name);
84 FSLMC_BUS_LOG(ERR, "Out of memory.");
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
93 dev = calloc(1, sizeof(struct rte_dpaa2_device));
95 FSLMC_BUS_LOG(ERR, "Out of memory.");
100 /* Parse the device name and ID */
101 t_ptr = strtok(dup_dev_name, ".");
103 FSLMC_BUS_LOG(ERR, "Incorrect device string observed.");
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;
121 dev->dev_type = DPAA2_UNKNOWN;
123 t_ptr = strtok(NULL, ".");
125 FSLMC_BUS_LOG(ERR, "Incorrect device string observed (%s).",
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.");
137 /* Add device in the fslmc device list */
138 TAILQ_INSERT_TAIL(&rte_fslmc_bus.device_list, dev, next);
140 /* Don't need the duplicated device filesystem entry anymore */
157 int device_count = 0;
158 char fslmc_dirpath[PATH_MAX];
160 struct dirent *entry;
161 static int process_once;
166 "Fslmc bus already scanned. Not rescanning");
171 ret = fslmc_get_container_group(&groupid);
175 /* Scan devices on the group */
176 sprintf(fslmc_dirpath, "%s/%d/devices", VFIO_IOMMU_GROUP_PATH,
178 dir = opendir(fslmc_dirpath);
180 FSLMC_BUS_LOG(ERR, "Unable to open VFIO group dir.");
184 while ((entry = readdir(dir)) != NULL) {
185 if (entry->d_name[0] == '.' || entry->d_type != DT_LNK)
188 ret = scan_one_fslmc_device(entry->d_name);
190 /* Error in parsing directory - exit gracefully */
191 goto scan_fail_cleanup;
196 FSLMC_BUS_LOG(INFO, "fslmc: Bus scan completed");
204 /* Remove all devices in the list */
205 cleanup_fslmc_device_list();
207 FSLMC_BUS_LOG(DEBUG, "FSLMC Bus Not Available. Skipping.");
208 /* Irrespective of failure, scan only return success */
213 rte_fslmc_match(struct rte_dpaa2_driver *dpaa2_drv,
214 struct rte_dpaa2_device *dpaa2_dev)
216 if (dpaa2_drv->drv_type == dpaa2_dev->dev_type)
223 rte_fslmc_probe(void)
226 struct rte_dpaa2_device *dev;
227 struct rte_dpaa2_driver *drv;
229 if (TAILQ_EMPTY(&rte_fslmc_bus.device_list))
232 ret = fslmc_vfio_setup_group();
234 FSLMC_BUS_LOG(ERR, "Unable to setup VFIO %d", ret);
238 ret = fslmc_vfio_process_group();
240 FSLMC_BUS_LOG(ERR, "Unable to setup devices %d", ret);
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);
253 ret = drv->probe(drv, dev);
255 FSLMC_BUS_LOG(ERR, "Unable to probe.\n");
263 static struct rte_device *
264 rte_fslmc_find_device(const struct rte_device *start, rte_dev_cmp_t cmp,
267 struct rte_dpaa2_device *dev;
269 TAILQ_FOREACH(dev, &rte_fslmc_bus.device_list, next) {
270 if (start && &dev->device == start) {
271 start = NULL; /* starting point found */
275 if (cmp(&dev->device, data) == 0)
282 /*register a fslmc bus based dpaa2 driver */
284 rte_fslmc_driver_register(struct rte_dpaa2_driver *driver)
288 TAILQ_INSERT_TAIL(&rte_fslmc_bus.driver_list, driver, next);
289 /* Update Bus references */
290 driver->fslmc_bus = &rte_fslmc_bus;
293 /*un-register a fslmc bus based dpaa2 driver */
295 rte_fslmc_driver_unregister(struct rte_dpaa2_driver *driver)
297 struct rte_fslmc_bus *fslmc_bus;
299 fslmc_bus = driver->fslmc_bus;
301 TAILQ_REMOVE(&fslmc_bus->driver_list, driver, next);
302 /* Update Bus references */
303 driver->fslmc_bus = NULL;
306 struct rte_fslmc_bus rte_fslmc_bus = {
308 .scan = rte_fslmc_scan,
309 .probe = rte_fslmc_probe,
310 .find_device = rte_fslmc_find_device,
312 .device_list = TAILQ_HEAD_INITIALIZER(rte_fslmc_bus.device_list),
313 .driver_list = TAILQ_HEAD_INITIALIZER(rte_fslmc_bus.driver_list),
316 RTE_REGISTER_BUS(fslmc, rte_fslmc_bus.bus);