xref: /dpdk/drivers/net/bnxt/tf_core/tf_if_tbl.c (revision 97435d7906d7706e39e5c3dfefa5e09d7de7f733)
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2019-2023 Broadcom
3  * All rights reserved.
4  */
5 
6 #include <rte_common.h>
7 
8 #include "tf_if_tbl.h"
9 #include "tf_common.h"
10 #include "tf_rm.h"
11 #include "tf_util.h"
12 #include "tf_msg.h"
13 #include "tfp.h"
14 
15 struct tf;
16 
17 /**
18  * IF Table database
19  */
20 struct tf_if_tbl_db {
21 	struct tf_if_tbl_cfg *if_tbl_cfg_db[TF_DIR_MAX];
22 };
23 
24 /**
25  * Convert if_tbl_type to hwrm type.
26  *
27  * [in] if_tbl_type
28  *   Interface table type
29  *
30  * [out] hwrm_type
31  *   HWRM device data type
32  *
33  * Returns:
34  *    0          - Success
35  *   -EOPNOTSUPP - Type not supported
36  */
37 static int
tf_if_tbl_get_hcapi_type(struct tf_if_tbl_get_hcapi_parms * parms)38 tf_if_tbl_get_hcapi_type(struct tf_if_tbl_get_hcapi_parms *parms)
39 {
40 	struct tf_if_tbl_cfg *tbl_cfg;
41 	enum tf_if_tbl_cfg_type cfg_type;
42 
43 	tbl_cfg = (struct tf_if_tbl_cfg *)parms->tbl_db;
44 	cfg_type = tbl_cfg[parms->db_index].cfg_type;
45 
46 	if (cfg_type != TF_IF_TBL_CFG)
47 		return -ENOTSUP;
48 
49 	*parms->hcapi_type = tbl_cfg[parms->db_index].hcapi_type;
50 
51 	return 0;
52 }
53 
54 int
tf_if_tbl_bind(struct tf * tfp,struct tf_if_tbl_cfg_parms * parms)55 tf_if_tbl_bind(struct tf *tfp,
56 	       struct tf_if_tbl_cfg_parms *parms)
57 {
58 	struct tfp_calloc_parms cparms;
59 	struct tf_if_tbl_db *if_tbl_db;
60 
61 	TF_CHECK_PARMS2(tfp, parms);
62 
63 	cparms.nitems = 1;
64 	cparms.size = sizeof(struct tf_if_tbl_db);
65 	cparms.alignment = 0;
66 	if (tfp_calloc(&cparms) != 0) {
67 		TFP_DRV_LOG(ERR, "if_tbl_rm_db alloc error %s\n",
68 			    strerror(ENOMEM));
69 		return -ENOMEM;
70 	}
71 
72 	if_tbl_db = cparms.mem_va;
73 	if_tbl_db->if_tbl_cfg_db[TF_DIR_RX] = parms->cfg;
74 	if_tbl_db->if_tbl_cfg_db[TF_DIR_TX] = parms->cfg;
75 	tf_session_set_if_tbl_db(tfp, (void *)if_tbl_db);
76 
77 	TFP_DRV_LOG(INFO,
78 		    "Table Type - initialized\n");
79 
80 	return 0;
81 }
82 
83 int
tf_if_tbl_unbind(struct tf * tfp)84 tf_if_tbl_unbind(struct tf *tfp)
85 {
86 	int rc;
87 	struct tf_if_tbl_db *if_tbl_db_ptr = NULL;
88 
89 	TF_CHECK_PARMS1(tfp);
90 
91 	rc = tf_session_get_if_tbl_db(tfp, (void **)&if_tbl_db_ptr);
92 	if (rc) {
93 		TFP_DRV_LOG(INFO, "No IF Table DBs initialized\n");
94 		return 0;
95 	}
96 	/* Bail if nothing has been initialized */
97 	if (!if_tbl_db_ptr) {
98 		TFP_DRV_LOG(INFO,
99 			    "No Table DBs created\n");
100 		return 0;
101 	}
102 
103 	tfp_free((void *)if_tbl_db_ptr);
104 	tf_session_set_if_tbl_db(tfp, NULL);
105 
106 	return 0;
107 }
108 
109 int
tf_if_tbl_set(struct tf * tfp,struct tf_if_tbl_set_parms * parms)110 tf_if_tbl_set(struct tf *tfp,
111 	      struct tf_if_tbl_set_parms *parms)
112 {
113 	int rc;
114 	struct tf_if_tbl_db *if_tbl_db_ptr = NULL;
115 	struct tf_if_tbl_get_hcapi_parms hparms;
116 
117 	TF_CHECK_PARMS3(tfp, parms, parms->data);
118 
119 	rc = tf_session_get_if_tbl_db(tfp, (void **)&if_tbl_db_ptr);
120 	if (rc) {
121 		TFP_DRV_LOG(INFO, "No IF Table DBs initialized\n");
122 		return 0;
123 	}
124 
125 	if (!if_tbl_db_ptr) {
126 		TFP_DRV_LOG(ERR,
127 			    "%s: No Table DBs created\n",
128 			    tf_dir_2_str(parms->dir));
129 		return -EINVAL;
130 	}
131 
132 	/* Convert TF type to HCAPI type */
133 	hparms.tbl_db = if_tbl_db_ptr->if_tbl_cfg_db[parms->dir];
134 	hparms.db_index = parms->type;
135 	hparms.hcapi_type = &parms->hcapi_type;
136 	rc = tf_if_tbl_get_hcapi_type(&hparms);
137 	if (rc)
138 		return rc;
139 
140 	rc = tf_msg_set_if_tbl_entry(tfp, parms);
141 	if (rc) {
142 		TFP_DRV_LOG(ERR,
143 			    "%s, If Tbl set failed, type:%d, rc:%s\n",
144 			    tf_dir_2_str(parms->dir),
145 			    parms->type,
146 			    strerror(-rc));
147 	}
148 
149 	return 0;
150 }
151 
152 int
tf_if_tbl_get(struct tf * tfp,struct tf_if_tbl_get_parms * parms)153 tf_if_tbl_get(struct tf *tfp,
154 	      struct tf_if_tbl_get_parms *parms)
155 {
156 	int rc = 0;
157 	struct tf_if_tbl_db *if_tbl_db_ptr = NULL;
158 	struct tf_if_tbl_get_hcapi_parms hparms;
159 
160 	TF_CHECK_PARMS3(tfp, parms, parms->data);
161 
162 	rc = tf_session_get_if_tbl_db(tfp, (void **)&if_tbl_db_ptr);
163 	if (rc) {
164 		TFP_DRV_LOG(INFO, "No IF Table DBs initialized\n");
165 		return 0;
166 	}
167 
168 	if (!if_tbl_db_ptr) {
169 		TFP_DRV_LOG(ERR,
170 			    "%s: No Table DBs created\n",
171 			    tf_dir_2_str(parms->dir));
172 		return -EINVAL;
173 	}
174 
175 	/* Convert TF type to HCAPI type */
176 	hparms.tbl_db = if_tbl_db_ptr->if_tbl_cfg_db[parms->dir];
177 	hparms.db_index = parms->type;
178 	hparms.hcapi_type = &parms->hcapi_type;
179 	rc = tf_if_tbl_get_hcapi_type(&hparms);
180 	if (rc)
181 		return rc;
182 
183 	/* Get the entry */
184 	rc = tf_msg_get_if_tbl_entry(tfp, parms);
185 	if (rc) {
186 		TFP_DRV_LOG(ERR,
187 			    "%s, If Tbl get failed, type:%d, rc:%s\n",
188 			    tf_dir_2_str(parms->dir),
189 			    parms->type,
190 			    strerror(-rc));
191 	}
192 
193 	return 0;
194 }
195