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 #include "bcmfs_vfio.h"
17 #include "bcmfs_sym_pmd.h"
19 struct bcmfs_device_attr {
20 const char name[BCMFS_MAX_PATH_LEN];
21 const char suffix[BCMFS_DEV_NAME_LEN];
22 const enum bcmfs_device_type type;
23 const uint32_t offset;
24 const uint32_t version;
27 /* BCMFS supported devices */
28 static struct bcmfs_device_attr dev_table[] = {
31 .suffix = "crypto_mbox",
32 .type = BCMFS_SYM_FS4,
34 .version = BCMFS_SYM_FS4_VERSION
39 .type = BCMFS_SYM_FS5,
41 .version = BCMFS_SYM_FS5_VERSION
48 struct bcmfs_hw_queue_pair_ops_table bcmfs_hw_queue_pair_ops_table = {
49 .tl = RTE_SPINLOCK_INITIALIZER,
53 int bcmfs_hw_queue_pair_register_ops(const struct bcmfs_hw_queue_pair_ops *h)
55 struct bcmfs_hw_queue_pair_ops *ops;
58 rte_spinlock_lock(&bcmfs_hw_queue_pair_ops_table.tl);
60 if (h->enq_one_req == NULL || h->dequeue == NULL ||
61 h->ring_db == NULL || h->startq == NULL || h->stopq == NULL) {
62 rte_spinlock_unlock(&bcmfs_hw_queue_pair_ops_table.tl);
64 "Missing callback while registering device ops");
68 if (strlen(h->name) >= sizeof(ops->name) - 1) {
69 rte_spinlock_unlock(&bcmfs_hw_queue_pair_ops_table.tl);
70 BCMFS_LOG(ERR, "%s(): fs device_ops <%s>: name too long",
75 ops_index = bcmfs_hw_queue_pair_ops_table.num_ops++;
76 ops = &bcmfs_hw_queue_pair_ops_table.qp_ops[ops_index];
77 strlcpy(ops->name, h->name, sizeof(ops->name));
78 ops->enq_one_req = h->enq_one_req;
79 ops->dequeue = h->dequeue;
80 ops->ring_db = h->ring_db;
81 ops->startq = h->startq;
82 ops->stopq = h->stopq;
84 rte_spinlock_unlock(&bcmfs_hw_queue_pair_ops_table.tl);
89 TAILQ_HEAD(fsdev_list, bcmfs_device);
90 static struct fsdev_list fsdev_list = TAILQ_HEAD_INITIALIZER(fsdev_list);
92 static struct bcmfs_device *
93 fsdev_allocate_one_dev(struct rte_vdev_device *vdev,
96 enum bcmfs_device_type dev_type __rte_unused)
98 struct bcmfs_device *fsdev;
101 fsdev = rte_calloc(__func__, 1, sizeof(*fsdev), 0);
105 if (strlen(dirpath) > sizeof(fsdev->dirname)) {
106 BCMFS_LOG(ERR, "dir path name is too long");
110 if (strlen(devname) > sizeof(fsdev->name)) {
111 BCMFS_LOG(ERR, "devname is too long");
115 /* check if registered ops name is present in directory path */
116 for (i = 0; i < bcmfs_hw_queue_pair_ops_table.num_ops; i++)
118 bcmfs_hw_queue_pair_ops_table.qp_ops[i].name))
119 fsdev->sym_hw_qp_ops =
120 &bcmfs_hw_queue_pair_ops_table.qp_ops[i];
121 if (!fsdev->sym_hw_qp_ops)
124 strcpy(fsdev->dirname, dirpath);
125 strcpy(fsdev->name, devname);
130 if (bcmfs_attach_vfio(fsdev))
133 /* Maximum number of QPs supported */
134 fsdev->max_hw_qps = fsdev->mmap_size / BCMFS_HW_QUEUE_IO_ADDR_LEN;
136 TAILQ_INSERT_TAIL(&fsdev_list, fsdev, next);
146 static struct bcmfs_device *
147 find_fsdev(struct rte_vdev_device *vdev)
149 struct bcmfs_device *fsdev;
151 TAILQ_FOREACH(fsdev, &fsdev_list, next)
152 if (fsdev->vdev == vdev)
159 fsdev_release(struct bcmfs_device *fsdev)
164 TAILQ_REMOVE(&fsdev_list, fsdev, next);
169 cmprator(const void *a, const void *b)
171 return (*(const unsigned int *)a - *(const unsigned int *)b);
175 fsdev_find_all_devs(const char *path, const char *search,
179 struct dirent *entry;
181 char addr[BCMFS_MAX_NODES][BCMFS_MAX_PATH_LEN];
186 BCMFS_LOG(ERR, "Unable to open directory");
190 while ((entry = readdir(dir)) != NULL) {
191 if (strstr(entry->d_name, search)) {
192 strlcpy(addr[count], entry->d_name,
200 for (i = 0 ; i < count; i++)
201 devs[i] = (uint32_t)strtoul(addr[i], NULL, 16);
202 /* sort the devices based on IO addresses */
203 qsort(devs, count, sizeof(uint32_t), cmprator);
209 fsdev_find_sub_dir(char *path, const char *search, char *output)
212 struct dirent *entry;
216 BCMFS_LOG(ERR, "Unable to open directory");
220 while ((entry = readdir(dir)) != NULL) {
221 if (!strcmp(entry->d_name, search)) {
222 strlcpy(output, entry->d_name, BCMFS_MAX_PATH_LEN);
235 bcmfs_vdev_probe(struct rte_vdev_device *vdev)
237 struct bcmfs_device *fsdev;
238 char top_dirpath[BCMFS_MAX_PATH_LEN];
239 char sub_dirpath[BCMFS_MAX_PATH_LEN];
240 char out_dirpath[BCMFS_MAX_PATH_LEN];
241 char out_dirname[BCMFS_MAX_PATH_LEN];
242 uint32_t fsdev_dev[BCMFS_MAX_NODES];
243 enum bcmfs_device_type dtype;
250 sprintf(top_dirpath, "%s", SYSFS_BCM_PLTFORM_DEVICES);
251 while (strlen(dev_table[i].name)) {
252 found = fsdev_find_sub_dir(top_dirpath,
260 BCMFS_LOG(ERR, "No supported bcmfs dev found");
265 dtype = dev_table[i].type;
267 snprintf(out_dirpath, sizeof(out_dirpath), "%s/%s",
268 top_dirpath, sub_dirpath);
269 count = fsdev_find_all_devs(out_dirpath,
270 dev_table[dev_idx].suffix,
273 BCMFS_LOG(ERR, "No supported bcmfs dev found");
279 /* format the device name present in the patch */
280 snprintf(out_dirname, sizeof(out_dirname), "%x.%s",
281 fsdev_dev[i], dev_table[dev_idx].suffix);
282 fsdev = fsdev_allocate_one_dev(vdev, out_dirpath,
292 BCMFS_LOG(ERR, "All supported devs busy");
296 err = bcmfs_sym_dev_create(fsdev);
299 "Failed to create BCMFS SYM PMD for device %s",
301 goto pmd_create_fail;
307 fsdev_release(fsdev);
313 bcmfs_vdev_remove(struct rte_vdev_device *vdev)
315 struct bcmfs_device *fsdev;
317 fsdev = find_fsdev(vdev);
321 fsdev_release(fsdev);
325 /* Register with vdev */
326 static struct rte_vdev_driver rte_bcmfs_pmd = {
327 .probe = bcmfs_vdev_probe,
328 .remove = bcmfs_vdev_remove
331 RTE_PMD_REGISTER_VDEV(bcmfs_pmd,