1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(C) 2020 Broadcom.
10 #include <rte_malloc.h>
11 #include <rte_string_fns.h>
13 #include "bcmfs_device.h"
14 #include "bcmfs_logs.h"
16 struct bcmfs_device_attr {
17 const char name[BCMFS_MAX_PATH_LEN];
18 const char suffix[BCMFS_DEV_NAME_LEN];
19 const enum bcmfs_device_type type;
20 const uint32_t offset;
21 const uint32_t version;
24 /* BCMFS supported devices */
25 static struct bcmfs_device_attr dev_table[] = {
28 .suffix = "crypto_mbox",
29 .type = BCMFS_SYM_FS4,
31 .version = BCMFS_SYM_FS4_VERSION
36 .type = BCMFS_SYM_FS5,
38 .version = BCMFS_SYM_FS5_VERSION
45 TAILQ_HEAD(fsdev_list, bcmfs_device);
46 static struct fsdev_list fsdev_list = TAILQ_HEAD_INITIALIZER(fsdev_list);
48 static struct bcmfs_device *
49 fsdev_allocate_one_dev(struct rte_vdev_device *vdev,
52 enum bcmfs_device_type dev_type __rte_unused)
54 struct bcmfs_device *fsdev;
56 fsdev = rte_calloc(__func__, 1, sizeof(*fsdev), 0);
60 if (strlen(dirpath) > sizeof(fsdev->dirname)) {
61 BCMFS_LOG(ERR, "dir path name is too long");
65 if (strlen(devname) > sizeof(fsdev->name)) {
66 BCMFS_LOG(ERR, "devname is too long");
70 strcpy(fsdev->dirname, dirpath);
71 strcpy(fsdev->name, devname);
75 TAILQ_INSERT_TAIL(&fsdev_list, fsdev, next);
85 static struct bcmfs_device *
86 find_fsdev(struct rte_vdev_device *vdev)
88 struct bcmfs_device *fsdev;
90 TAILQ_FOREACH(fsdev, &fsdev_list, next)
91 if (fsdev->vdev == vdev)
98 fsdev_release(struct bcmfs_device *fsdev)
103 TAILQ_REMOVE(&fsdev_list, fsdev, next);
108 cmprator(const void *a, const void *b)
110 return (*(const unsigned int *)a - *(const unsigned int *)b);
114 fsdev_find_all_devs(const char *path, const char *search,
118 struct dirent *entry;
120 char addr[BCMFS_MAX_NODES][BCMFS_MAX_PATH_LEN];
125 BCMFS_LOG(ERR, "Unable to open directory");
129 while ((entry = readdir(dir)) != NULL) {
130 if (strstr(entry->d_name, search)) {
131 strlcpy(addr[count], entry->d_name,
139 for (i = 0 ; i < count; i++)
140 devs[i] = (uint32_t)strtoul(addr[i], NULL, 16);
141 /* sort the devices based on IO addresses */
142 qsort(devs, count, sizeof(uint32_t), cmprator);
148 fsdev_find_sub_dir(char *path, const char *search, char *output)
151 struct dirent *entry;
155 BCMFS_LOG(ERR, "Unable to open directory");
159 while ((entry = readdir(dir)) != NULL) {
160 if (!strcmp(entry->d_name, search)) {
161 strlcpy(output, entry->d_name, BCMFS_MAX_PATH_LEN);
174 bcmfs_vdev_probe(struct rte_vdev_device *vdev)
176 struct bcmfs_device *fsdev;
177 char top_dirpath[BCMFS_MAX_PATH_LEN];
178 char sub_dirpath[BCMFS_MAX_PATH_LEN];
179 char out_dirpath[BCMFS_MAX_PATH_LEN];
180 char out_dirname[BCMFS_MAX_PATH_LEN];
181 uint32_t fsdev_dev[BCMFS_MAX_NODES];
182 enum bcmfs_device_type dtype;
188 sprintf(top_dirpath, "%s", SYSFS_BCM_PLTFORM_DEVICES);
189 while (strlen(dev_table[i].name)) {
190 found = fsdev_find_sub_dir(top_dirpath,
198 BCMFS_LOG(ERR, "No supported bcmfs dev found");
203 dtype = dev_table[i].type;
205 snprintf(out_dirpath, sizeof(out_dirpath), "%s/%s",
206 top_dirpath, sub_dirpath);
207 count = fsdev_find_all_devs(out_dirpath,
208 dev_table[dev_idx].suffix,
211 BCMFS_LOG(ERR, "No supported bcmfs dev found");
217 /* format the device name present in the patch */
218 snprintf(out_dirname, sizeof(out_dirname), "%x.%s",
219 fsdev_dev[i], dev_table[dev_idx].suffix);
220 fsdev = fsdev_allocate_one_dev(vdev, out_dirpath,
230 BCMFS_LOG(ERR, "All supported devs busy");
238 bcmfs_vdev_remove(struct rte_vdev_device *vdev)
240 struct bcmfs_device *fsdev;
242 fsdev = find_fsdev(vdev);
246 fsdev_release(fsdev);
250 /* Register with vdev */
251 static struct rte_vdev_driver rte_bcmfs_pmd = {
252 .probe = bcmfs_vdev_probe,
253 .remove = bcmfs_vdev_remove
256 RTE_PMD_REGISTER_VDEV(bcmfs_pmd,