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 compare_dpaa2_devname(struct rte_dpaa2_device *dev1,
70 struct rte_dpaa2_device *dev2)
74 if (dev1->dev_type > dev2->dev_type) {
76 } else if (dev1->dev_type < dev2->dev_type) {
79 /* Check the ID as types match */
80 if (dev1->object_id > dev2->object_id)
82 else if (dev1->object_id < dev2->object_id)
85 comp = 0; /* Duplicate device name */
92 insert_in_device_list(struct rte_dpaa2_device *newdev)
94 int comp, inserted = 0;
95 struct rte_dpaa2_device *dev = NULL;
96 struct rte_dpaa2_device *tdev = NULL;
98 TAILQ_FOREACH_SAFE(dev, &rte_fslmc_bus.device_list, next, tdev) {
99 comp = compare_dpaa2_devname(newdev, dev);
101 TAILQ_INSERT_BEFORE(dev, newdev, next);
108 TAILQ_INSERT_TAIL(&rte_fslmc_bus.device_list, newdev, next);
112 scan_one_fslmc_device(char *dev_name)
114 char *dup_dev_name, *t_ptr;
115 struct rte_dpaa2_device *dev;
120 /* Ignore the Container name itself */
121 if (!strncmp("dprc", dev_name, 4))
124 /* Creating a temporary copy to perform cut-parse over string */
125 dup_dev_name = strdup(dev_name);
127 FSLMC_BUS_LOG(ERR, "Out of memory.");
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
136 dev = calloc(1, sizeof(struct rte_dpaa2_device));
138 FSLMC_BUS_LOG(ERR, "Out of memory.");
143 /* Parse the device name and ID */
144 t_ptr = strtok(dup_dev_name, ".");
146 FSLMC_BUS_LOG(ERR, "Incorrect device string observed.");
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;
164 dev->dev_type = DPAA2_UNKNOWN;
166 t_ptr = strtok(NULL, ".");
168 FSLMC_BUS_LOG(ERR, "Incorrect device string observed (%s).",
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.");
180 /* Add device in the fslmc device list */
181 insert_in_device_list(dev);
183 /* Don't need the duplicated device filesystem entry anymore */
200 int device_count = 0;
201 char fslmc_dirpath[PATH_MAX];
203 struct dirent *entry;
204 static int process_once;
209 "Fslmc bus already scanned. Not rescanning");
214 ret = fslmc_get_container_group(&groupid);
218 /* Scan devices on the group */
219 sprintf(fslmc_dirpath, "%s/%d/devices", VFIO_IOMMU_GROUP_PATH,
221 dir = opendir(fslmc_dirpath);
223 FSLMC_BUS_LOG(ERR, "Unable to open VFIO group dir.");
227 while ((entry = readdir(dir)) != NULL) {
228 if (entry->d_name[0] == '.' || entry->d_type != DT_LNK)
231 ret = scan_one_fslmc_device(entry->d_name);
233 /* Error in parsing directory - exit gracefully */
234 goto scan_fail_cleanup;
239 FSLMC_BUS_LOG(INFO, "fslmc: Bus scan completed");
247 /* Remove all devices in the list */
248 cleanup_fslmc_device_list();
250 FSLMC_BUS_LOG(DEBUG, "FSLMC Bus Not Available. Skipping.");
251 /* Irrespective of failure, scan only return success */
256 rte_fslmc_match(struct rte_dpaa2_driver *dpaa2_drv,
257 struct rte_dpaa2_device *dpaa2_dev)
259 if (dpaa2_drv->drv_type == dpaa2_dev->dev_type)
266 rte_fslmc_probe(void)
269 struct rte_dpaa2_device *dev;
270 struct rte_dpaa2_driver *drv;
272 if (TAILQ_EMPTY(&rte_fslmc_bus.device_list))
275 ret = fslmc_vfio_setup_group();
277 FSLMC_BUS_LOG(ERR, "Unable to setup VFIO %d", ret);
281 ret = fslmc_vfio_process_group();
283 FSLMC_BUS_LOG(ERR, "Unable to setup devices %d", ret);
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);
296 ret = drv->probe(drv, dev);
298 FSLMC_BUS_LOG(ERR, "Unable to probe.\n");
306 static struct rte_device *
307 rte_fslmc_find_device(const struct rte_device *start, rte_dev_cmp_t cmp,
310 struct rte_dpaa2_device *dev;
312 TAILQ_FOREACH(dev, &rte_fslmc_bus.device_list, next) {
313 if (start && &dev->device == start) {
314 start = NULL; /* starting point found */
318 if (cmp(&dev->device, data) == 0)
325 /*register a fslmc bus based dpaa2 driver */
327 rte_fslmc_driver_register(struct rte_dpaa2_driver *driver)
331 TAILQ_INSERT_TAIL(&rte_fslmc_bus.driver_list, driver, next);
332 /* Update Bus references */
333 driver->fslmc_bus = &rte_fslmc_bus;
336 /*un-register a fslmc bus based dpaa2 driver */
338 rte_fslmc_driver_unregister(struct rte_dpaa2_driver *driver)
340 struct rte_fslmc_bus *fslmc_bus;
342 fslmc_bus = driver->fslmc_bus;
344 TAILQ_REMOVE(&fslmc_bus->driver_list, driver, next);
345 /* Update Bus references */
346 driver->fslmc_bus = NULL;
349 struct rte_fslmc_bus rte_fslmc_bus = {
351 .scan = rte_fslmc_scan,
352 .probe = rte_fslmc_probe,
353 .find_device = rte_fslmc_find_device,
355 .device_list = TAILQ_HEAD_INITIALIZER(rte_fslmc_bus.device_list),
356 .driver_list = TAILQ_HEAD_INITIALIZER(rte_fslmc_bus.driver_list),
359 RTE_REGISTER_BUS(fslmc, rte_fslmc_bus.bus);