xref: /dpdk/drivers/net/bnxt/tf_ulp/ulp_def_rules.c (revision e0a43946)
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2019-2020 Broadcom
3  * All rights reserved.
4  */
5 
6 #include "bnxt_tf_common.h"
7 #include "ulp_template_struct.h"
8 #include "ulp_template_db_enum.h"
9 #include "ulp_template_db_field.h"
10 #include "ulp_utils.h"
11 #include "ulp_port_db.h"
12 #include "ulp_flow_db.h"
13 #include "ulp_mapper.h"
14 
15 struct bnxt_ulp_def_param_handler {
16 	int32_t (*vfr_func)(struct bnxt_ulp_context *ulp_ctx,
17 			    struct ulp_tlv_param *param,
18 			    struct bnxt_ulp_mapper_create_parms *mapper_params);
19 };
20 
21 static int32_t
22 ulp_set_svif_in_comp_fld(struct bnxt_ulp_context *ulp_ctx,
23 			 uint32_t  ifindex, uint8_t svif_type,
24 			 struct bnxt_ulp_mapper_create_parms *mapper_params)
25 {
26 	uint16_t svif;
27 	uint8_t idx;
28 	int rc;
29 
30 	rc = ulp_port_db_svif_get(ulp_ctx, ifindex, svif_type, &svif);
31 	if (rc)
32 		return rc;
33 
34 	if (svif_type == BNXT_ULP_PHY_PORT_SVIF)
35 		idx = BNXT_ULP_CF_IDX_PHY_PORT_SVIF;
36 	else if (svif_type == BNXT_ULP_DRV_FUNC_SVIF)
37 		idx = BNXT_ULP_CF_IDX_DRV_FUNC_SVIF;
38 	else
39 		idx = BNXT_ULP_CF_IDX_VF_FUNC_SVIF;
40 
41 	ULP_COMP_FLD_IDX_WR(mapper_params, idx, svif);
42 
43 	return 0;
44 }
45 
46 static int32_t
47 ulp_set_spif_in_comp_fld(struct bnxt_ulp_context *ulp_ctx,
48 			 uint32_t  ifindex, uint8_t spif_type,
49 			 struct bnxt_ulp_mapper_create_parms *mapper_params)
50 {
51 	uint16_t spif;
52 	uint8_t idx;
53 	int rc;
54 
55 	rc = ulp_port_db_spif_get(ulp_ctx, ifindex, spif_type, &spif);
56 	if (rc)
57 		return rc;
58 
59 	if (spif_type == BNXT_ULP_PHY_PORT_SPIF)
60 		idx = BNXT_ULP_CF_IDX_PHY_PORT_SPIF;
61 	else if (spif_type == BNXT_ULP_DRV_FUNC_SPIF)
62 		idx = BNXT_ULP_CF_IDX_DRV_FUNC_SPIF;
63 	else
64 		idx = BNXT_ULP_CF_IDX_VF_FUNC_SPIF;
65 
66 	ULP_COMP_FLD_IDX_WR(mapper_params, idx, spif);
67 
68 	return 0;
69 }
70 
71 static int32_t
72 ulp_set_parif_in_comp_fld(struct bnxt_ulp_context *ulp_ctx,
73 			  uint32_t  ifindex, uint8_t parif_type,
74 			  struct bnxt_ulp_mapper_create_parms *mapper_params)
75 {
76 	uint16_t parif;
77 	uint8_t idx;
78 	int rc;
79 
80 	rc = ulp_port_db_parif_get(ulp_ctx, ifindex, parif_type, &parif);
81 	if (rc)
82 		return rc;
83 
84 	if (parif_type == BNXT_ULP_PHY_PORT_PARIF)
85 		idx = BNXT_ULP_CF_IDX_PHY_PORT_PARIF;
86 	else if (parif_type == BNXT_ULP_DRV_FUNC_PARIF)
87 		idx = BNXT_ULP_CF_IDX_DRV_FUNC_PARIF;
88 	else
89 		idx = BNXT_ULP_CF_IDX_VF_FUNC_PARIF;
90 
91 	ULP_COMP_FLD_IDX_WR(mapper_params, idx, parif);
92 
93 	return 0;
94 }
95 
96 static int32_t
97 ulp_set_vport_in_comp_fld(struct bnxt_ulp_context *ulp_ctx, uint32_t ifindex,
98 			  struct bnxt_ulp_mapper_create_parms *mapper_params)
99 {
100 	uint16_t vport;
101 	int rc;
102 
103 	rc = ulp_port_db_vport_get(ulp_ctx, ifindex, &vport);
104 	if (rc)
105 		return rc;
106 
107 	ULP_COMP_FLD_IDX_WR(mapper_params, BNXT_ULP_CF_IDX_PHY_PORT_VPORT,
108 			    vport);
109 	return 0;
110 }
111 
112 static int32_t
113 ulp_set_vnic_in_comp_fld(struct bnxt_ulp_context *ulp_ctx,
114 			 uint32_t  ifindex, uint8_t vnic_type,
115 			 struct bnxt_ulp_mapper_create_parms *mapper_params)
116 {
117 	uint16_t vnic;
118 	uint8_t idx;
119 	int rc;
120 
121 	rc = ulp_port_db_default_vnic_get(ulp_ctx, ifindex, vnic_type, &vnic);
122 	if (rc)
123 		return rc;
124 
125 	if (vnic_type == BNXT_ULP_DRV_FUNC_VNIC)
126 		idx = BNXT_ULP_CF_IDX_DRV_FUNC_VNIC;
127 	else
128 		idx = BNXT_ULP_CF_IDX_VF_FUNC_VNIC;
129 
130 	ULP_COMP_FLD_IDX_WR(mapper_params, idx, vnic);
131 
132 	return 0;
133 }
134 
135 static int32_t
136 ulp_set_vlan_in_act_prop(uint16_t port_id,
137 			 struct bnxt_ulp_mapper_create_parms *mapper_params)
138 {
139 	struct ulp_rte_act_prop *act_prop = mapper_params->act_prop;
140 
141 	if (ULP_BITMAP_ISSET(mapper_params->act->bits,
142 			     BNXT_ULP_ACTION_BIT_SET_VLAN_VID)) {
143 		BNXT_TF_DBG(ERR,
144 			    "VLAN already set, multiple VLANs unsupported\n");
145 		return BNXT_TF_RC_ERROR;
146 	}
147 
148 	port_id = rte_cpu_to_be_16(port_id);
149 
150 	ULP_BITMAP_SET(mapper_params->act->bits,
151 		       BNXT_ULP_ACTION_BIT_SET_VLAN_VID);
152 
153 	memcpy(&act_prop->act_details[BNXT_ULP_ACT_PROP_IDX_ENCAP_VTAG],
154 	       &port_id, sizeof(port_id));
155 
156 	return 0;
157 }
158 
159 static int32_t
160 ulp_set_mark_in_act_prop(uint16_t port_id,
161 			 struct bnxt_ulp_mapper_create_parms *mapper_params)
162 {
163 	if (ULP_BITMAP_ISSET(mapper_params->act->bits,
164 			     BNXT_ULP_ACTION_BIT_MARK)) {
165 		BNXT_TF_DBG(ERR,
166 			    "MARK already set, multiple MARKs unsupported\n");
167 		return BNXT_TF_RC_ERROR;
168 	}
169 
170 	ULP_COMP_FLD_IDX_WR(mapper_params, BNXT_ULP_CF_IDX_DEV_PORT_ID,
171 			    port_id);
172 
173 	return 0;
174 }
175 
176 static int32_t
177 ulp_df_dev_port_handler(struct bnxt_ulp_context *ulp_ctx,
178 			struct ulp_tlv_param *param,
179 			struct bnxt_ulp_mapper_create_parms *mapper_params)
180 {
181 	uint16_t port_id;
182 	uint32_t ifindex;
183 	int rc;
184 
185 	port_id = param->value[0] | param->value[1];
186 
187 	rc = ulp_port_db_dev_port_to_ulp_index(ulp_ctx, port_id, &ifindex);
188 	if (rc) {
189 		BNXT_TF_DBG(ERR,
190 				"Invalid port id\n");
191 		return BNXT_TF_RC_ERROR;
192 	}
193 
194 	/* Set port SVIF */
195 	rc = ulp_set_svif_in_comp_fld(ulp_ctx, ifindex, BNXT_ULP_PHY_PORT_SVIF,
196 				      mapper_params);
197 	if (rc)
198 		return rc;
199 
200 	/* Set DRV Func SVIF */
201 	rc = ulp_set_svif_in_comp_fld(ulp_ctx, ifindex, BNXT_ULP_DRV_FUNC_SVIF,
202 				      mapper_params);
203 	if (rc)
204 		return rc;
205 
206 	/* Set VF Func SVIF */
207 	rc = ulp_set_svif_in_comp_fld(ulp_ctx, ifindex, BNXT_ULP_VF_FUNC_SVIF,
208 				      mapper_params);
209 	if (rc)
210 		return rc;
211 
212 	/* Set port SPIF */
213 	rc = ulp_set_spif_in_comp_fld(ulp_ctx, ifindex, BNXT_ULP_PHY_PORT_SPIF,
214 				      mapper_params);
215 	if (rc)
216 		return rc;
217 
218 	/* Set DRV Func SPIF */
219 	rc = ulp_set_spif_in_comp_fld(ulp_ctx, ifindex, BNXT_ULP_DRV_FUNC_SPIF,
220 				      mapper_params);
221 	if (rc)
222 		return rc;
223 
224 	/* Set VF Func SPIF */
225 	rc = ulp_set_spif_in_comp_fld(ulp_ctx, ifindex, BNXT_ULP_DRV_FUNC_SPIF,
226 				      mapper_params);
227 	if (rc)
228 		return rc;
229 
230 	/* Set port PARIF */
231 	rc = ulp_set_parif_in_comp_fld(ulp_ctx, ifindex,
232 				       BNXT_ULP_PHY_PORT_PARIF, mapper_params);
233 	if (rc)
234 		return rc;
235 
236 	/* Set DRV Func PARIF */
237 	rc = ulp_set_parif_in_comp_fld(ulp_ctx, ifindex,
238 				       BNXT_ULP_DRV_FUNC_PARIF, mapper_params);
239 	if (rc)
240 		return rc;
241 
242 	/* Set VF Func PARIF */
243 	rc = ulp_set_parif_in_comp_fld(ulp_ctx, ifindex, BNXT_ULP_VF_FUNC_PARIF,
244 				       mapper_params);
245 	if (rc)
246 		return rc;
247 
248 	/* Set uplink VNIC */
249 	rc = ulp_set_vnic_in_comp_fld(ulp_ctx, ifindex, true, mapper_params);
250 	if (rc)
251 		return rc;
252 
253 	/* Set VF VNIC */
254 	rc = ulp_set_vnic_in_comp_fld(ulp_ctx, ifindex, false, mapper_params);
255 	if (rc)
256 		return rc;
257 
258 	/* Set VPORT */
259 	rc = ulp_set_vport_in_comp_fld(ulp_ctx, ifindex, mapper_params);
260 	if (rc)
261 		return rc;
262 
263 	/* Set VLAN */
264 	rc = ulp_set_vlan_in_act_prop(port_id, mapper_params);
265 	if (rc)
266 		return rc;
267 
268 	/* Set MARK */
269 	rc = ulp_set_mark_in_act_prop(port_id, mapper_params);
270 	if (rc)
271 		return rc;
272 
273 	return 0;
274 }
275 
276 struct bnxt_ulp_def_param_handler ulp_def_handler_tbl[] = {
277 	[BNXT_ULP_DF_PARAM_TYPE_DEV_PORT_ID] = {
278 			.vfr_func = ulp_df_dev_port_handler }
279 };
280 
281 /*
282  * Function to create default rules for the following paths
283  * 1) Device PORT to DPDK App
284  * 2) DPDK App to Device PORT
285  * 3) VF Representor to VF
286  * 4) VF to VF Representor
287  *
288  * eth_dev [in] Ptr to rte eth device.
289  * param_list [in] Ptr to a list of parameters (Currently, only DPDK port_id).
290  * ulp_class_tid [in] Class template ID number.
291  * flow_id [out] Ptr to flow identifier.
292  *
293  * Returns 0 on success or negative number on failure.
294  */
295 int32_t
296 ulp_default_flow_create(struct rte_eth_dev *eth_dev,
297 			struct ulp_tlv_param *param_list,
298 			uint32_t ulp_class_tid,
299 			uint32_t *flow_id)
300 {
301 	struct ulp_rte_hdr_field	hdr_field[BNXT_ULP_PROTO_HDR_MAX];
302 	uint32_t			comp_fld[BNXT_ULP_CF_IDX_LAST];
303 	struct bnxt_ulp_mapper_create_parms mapper_params = { 0 };
304 	struct ulp_rte_act_prop		act_prop;
305 	struct ulp_rte_act_bitmap	act = { 0 };
306 	struct bnxt_ulp_context		*ulp_ctx;
307 	uint32_t type;
308 	int rc;
309 
310 	memset(&mapper_params, 0, sizeof(mapper_params));
311 	memset(hdr_field, 0, sizeof(hdr_field));
312 	memset(comp_fld, 0, sizeof(comp_fld));
313 	memset(&act_prop, 0, sizeof(act_prop));
314 
315 	mapper_params.hdr_field = hdr_field;
316 	mapper_params.act = &act;
317 	mapper_params.act_prop = &act_prop;
318 	mapper_params.comp_fld = comp_fld;
319 
320 	ulp_ctx = bnxt_ulp_eth_dev_ptr2_cntxt_get(eth_dev);
321 	if (!ulp_ctx) {
322 		BNXT_TF_DBG(ERR, "ULP context is not initialized. "
323 				 "Failed to create default flow.\n");
324 		return -EINVAL;
325 	}
326 
327 	type = param_list->type;
328 	while (type != BNXT_ULP_DF_PARAM_TYPE_LAST) {
329 		if (ulp_def_handler_tbl[type].vfr_func) {
330 			rc = ulp_def_handler_tbl[type].vfr_func(ulp_ctx,
331 								param_list,
332 								&mapper_params);
333 			if (rc) {
334 				BNXT_TF_DBG(ERR,
335 					    "Failed to create default flow.\n");
336 				return rc;
337 			}
338 		}
339 
340 		param_list++;
341 		type = param_list->type;
342 	}
343 
344 	mapper_params.class_tid = ulp_class_tid;
345 
346 	rc = ulp_mapper_flow_create(ulp_ctx, &mapper_params, flow_id);
347 	if (rc) {
348 		BNXT_TF_DBG(ERR, "Failed to create default flow.\n");
349 		return rc;
350 	}
351 
352 	return 0;
353 }
354 
355 /*
356  * Function to destroy default rules for the following paths
357  * 1) Device PORT to DPDK App
358  * 2) DPDK App to Device PORT
359  * 3) VF Representor to VF
360  * 4) VF to VF Representor
361  *
362  * eth_dev [in] Ptr to rte eth device.
363  * flow_id [in] Flow identifier.
364  *
365  * Returns 0 on success or negative number on failure.
366  */
367 int32_t
368 ulp_default_flow_destroy(struct rte_eth_dev *eth_dev, uint32_t flow_id)
369 {
370 	struct bnxt_ulp_context *ulp_ctx;
371 	int rc;
372 
373 	ulp_ctx = bnxt_ulp_eth_dev_ptr2_cntxt_get(eth_dev);
374 	if (!ulp_ctx) {
375 		BNXT_TF_DBG(ERR, "ULP context is not initialized\n");
376 		return -EINVAL;
377 	}
378 
379 	rc = ulp_mapper_flow_destroy(ulp_ctx, flow_id,
380 				     BNXT_ULP_DEFAULT_FLOW_TABLE);
381 	if (rc)
382 		BNXT_TF_DBG(ERR, "Failed to destroy flow.\n");
383 
384 	return rc;
385 }
386