xref: /linux/drivers/net/ethernet/mellanox/mlx5/core/steering/hws/mlx5hws_action.c (revision 9410645520e9b820069761f3450ef6661418e279)
1 // SPDX-License-Identifier: GPL-2.0 OR Linux-OpenIB
2 /* Copyright (c) 2024 NVIDIA Corporation & Affiliates */
3 
4 #include "mlx5hws_internal.h"
5 
6 #define MLX5HWS_ACTION_METER_INIT_COLOR_OFFSET 1
7 
8 /* Header removal size limited to 128B (64 words) */
9 #define MLX5HWS_ACTION_REMOVE_HEADER_MAX_SIZE 128
10 
11 /* This is the longest supported action sequence for FDB table:
12  * DECAP, POP_VLAN, MODIFY, CTR, ASO, PUSH_VLAN, MODIFY, ENCAP, Term.
13  */
14 static const u32 action_order_arr[MLX5HWS_TABLE_TYPE_MAX][MLX5HWS_ACTION_TYP_MAX] = {
15 	[MLX5HWS_TABLE_TYPE_FDB] = {
16 		BIT(MLX5HWS_ACTION_TYP_REMOVE_HEADER) |
17 		BIT(MLX5HWS_ACTION_TYP_REFORMAT_TNL_L2_TO_L2) |
18 		BIT(MLX5HWS_ACTION_TYP_REFORMAT_TNL_L3_TO_L2),
19 		BIT(MLX5HWS_ACTION_TYP_POP_VLAN),
20 		BIT(MLX5HWS_ACTION_TYP_POP_VLAN),
21 		BIT(MLX5HWS_ACTION_TYP_MODIFY_HDR),
22 		BIT(MLX5HWS_ACTION_TYP_PUSH_VLAN),
23 		BIT(MLX5HWS_ACTION_TYP_PUSH_VLAN),
24 		BIT(MLX5HWS_ACTION_TYP_INSERT_HEADER) |
25 		BIT(MLX5HWS_ACTION_TYP_REFORMAT_L2_TO_TNL_L2) |
26 		BIT(MLX5HWS_ACTION_TYP_REFORMAT_L2_TO_TNL_L3),
27 		BIT(MLX5HWS_ACTION_TYP_CTR),
28 		BIT(MLX5HWS_ACTION_TYP_TAG),
29 		BIT(MLX5HWS_ACTION_TYP_ASO_METER),
30 		BIT(MLX5HWS_ACTION_TYP_MODIFY_HDR),
31 		BIT(MLX5HWS_ACTION_TYP_TBL) |
32 		BIT(MLX5HWS_ACTION_TYP_VPORT) |
33 		BIT(MLX5HWS_ACTION_TYP_DROP) |
34 		BIT(MLX5HWS_ACTION_TYP_SAMPLER) |
35 		BIT(MLX5HWS_ACTION_TYP_RANGE) |
36 		BIT(MLX5HWS_ACTION_TYP_DEST_ARRAY),
37 		BIT(MLX5HWS_ACTION_TYP_LAST),
38 	},
39 };
40 
41 static const char * const mlx5hws_action_type_str[] = {
42 	[MLX5HWS_ACTION_TYP_LAST] = "LAST",
43 	[MLX5HWS_ACTION_TYP_REFORMAT_TNL_L2_TO_L2] = "TNL_L2_TO_L2",
44 	[MLX5HWS_ACTION_TYP_REFORMAT_L2_TO_TNL_L2] = "L2_TO_TNL_L2",
45 	[MLX5HWS_ACTION_TYP_REFORMAT_TNL_L3_TO_L2] = "TNL_L3_TO_L2",
46 	[MLX5HWS_ACTION_TYP_REFORMAT_L2_TO_TNL_L3] = "L2_TO_TNL_L3",
47 	[MLX5HWS_ACTION_TYP_DROP] = "DROP",
48 	[MLX5HWS_ACTION_TYP_TBL] = "TBL",
49 	[MLX5HWS_ACTION_TYP_CTR] = "CTR",
50 	[MLX5HWS_ACTION_TYP_TAG] = "TAG",
51 	[MLX5HWS_ACTION_TYP_MODIFY_HDR] = "MODIFY_HDR",
52 	[MLX5HWS_ACTION_TYP_VPORT] = "VPORT",
53 	[MLX5HWS_ACTION_TYP_MISS] = "DEFAULT_MISS",
54 	[MLX5HWS_ACTION_TYP_POP_VLAN] = "POP_VLAN",
55 	[MLX5HWS_ACTION_TYP_PUSH_VLAN] = "PUSH_VLAN",
56 	[MLX5HWS_ACTION_TYP_ASO_METER] = "ASO_METER",
57 	[MLX5HWS_ACTION_TYP_DEST_ARRAY] = "DEST_ARRAY",
58 	[MLX5HWS_ACTION_TYP_INSERT_HEADER] = "INSERT_HEADER",
59 	[MLX5HWS_ACTION_TYP_REMOVE_HEADER] = "REMOVE_HEADER",
60 	[MLX5HWS_ACTION_TYP_SAMPLER] = "SAMPLER",
61 	[MLX5HWS_ACTION_TYP_RANGE] = "RANGE",
62 };
63 
64 static_assert(ARRAY_SIZE(mlx5hws_action_type_str) == MLX5HWS_ACTION_TYP_MAX,
65 	      "Missing mlx5hws_action_type_str");
66 
mlx5hws_action_type_to_str(enum mlx5hws_action_type action_type)67 const char *mlx5hws_action_type_to_str(enum mlx5hws_action_type action_type)
68 {
69 	return mlx5hws_action_type_str[action_type];
70 }
71 
mlx5hws_action_get_type(struct mlx5hws_action * action)72 enum mlx5hws_action_type mlx5hws_action_get_type(struct mlx5hws_action *action)
73 {
74 	return action->type;
75 }
76 
hws_action_get_shared_stc_nic(struct mlx5hws_context * ctx,enum mlx5hws_context_shared_stc_type stc_type,u8 tbl_type)77 static int hws_action_get_shared_stc_nic(struct mlx5hws_context *ctx,
78 					 enum mlx5hws_context_shared_stc_type stc_type,
79 					 u8 tbl_type)
80 {
81 	struct mlx5hws_cmd_stc_modify_attr stc_attr = {0};
82 	struct mlx5hws_action_shared_stc *shared_stc;
83 	int ret;
84 
85 	mutex_lock(&ctx->ctrl_lock);
86 	if (ctx->common_res[tbl_type].shared_stc[stc_type]) {
87 		ctx->common_res[tbl_type].shared_stc[stc_type]->refcount++;
88 		mutex_unlock(&ctx->ctrl_lock);
89 		return 0;
90 	}
91 
92 	shared_stc = kzalloc(sizeof(*shared_stc), GFP_KERNEL);
93 	if (!shared_stc) {
94 		ret = -ENOMEM;
95 		goto unlock_and_out;
96 	}
97 	switch (stc_type) {
98 	case MLX5HWS_CONTEXT_SHARED_STC_DECAP_L3:
99 		stc_attr.action_type = MLX5_IFC_STC_ACTION_TYPE_HEADER_REMOVE;
100 		stc_attr.action_offset = MLX5HWS_ACTION_OFFSET_DW5;
101 		stc_attr.reparse_mode = MLX5_IFC_STC_REPARSE_IGNORE;
102 		stc_attr.remove_header.decap = 0;
103 		stc_attr.remove_header.start_anchor = MLX5_HEADER_ANCHOR_PACKET_START;
104 		stc_attr.remove_header.end_anchor = MLX5_HEADER_ANCHOR_IPV6_IPV4;
105 		break;
106 	case MLX5HWS_CONTEXT_SHARED_STC_DOUBLE_POP:
107 		stc_attr.action_type = MLX5_IFC_STC_ACTION_TYPE_REMOVE_WORDS;
108 		stc_attr.action_offset = MLX5HWS_ACTION_OFFSET_DW5;
109 		stc_attr.reparse_mode = MLX5_IFC_STC_REPARSE_ALWAYS;
110 		stc_attr.remove_words.start_anchor = MLX5_HEADER_ANCHOR_FIRST_VLAN_START;
111 		stc_attr.remove_words.num_of_words = MLX5HWS_ACTION_HDR_LEN_L2_VLAN;
112 		break;
113 	default:
114 		mlx5hws_err(ctx, "No such stc_type: %d\n", stc_type);
115 		pr_warn("HWS: Invalid stc_type: %d\n", stc_type);
116 		ret = -EINVAL;
117 		goto unlock_and_out;
118 	}
119 
120 	ret = mlx5hws_action_alloc_single_stc(ctx, &stc_attr, tbl_type,
121 					      &shared_stc->stc_chunk);
122 	if (ret) {
123 		mlx5hws_err(ctx, "Failed to allocate shared decap l2 STC\n");
124 		goto free_shared_stc;
125 	}
126 
127 	ctx->common_res[tbl_type].shared_stc[stc_type] = shared_stc;
128 	ctx->common_res[tbl_type].shared_stc[stc_type]->refcount = 1;
129 
130 	mutex_unlock(&ctx->ctrl_lock);
131 
132 	return 0;
133 
134 free_shared_stc:
135 	kfree(shared_stc);
136 unlock_and_out:
137 	mutex_unlock(&ctx->ctrl_lock);
138 	return ret;
139 }
140 
hws_action_get_shared_stc(struct mlx5hws_action * action,enum mlx5hws_context_shared_stc_type stc_type)141 static int hws_action_get_shared_stc(struct mlx5hws_action *action,
142 				     enum mlx5hws_context_shared_stc_type stc_type)
143 {
144 	struct mlx5hws_context *ctx = action->ctx;
145 	int ret;
146 
147 	if (stc_type >= MLX5HWS_CONTEXT_SHARED_STC_MAX) {
148 		pr_warn("HWS: Invalid shared stc_type: %d\n", stc_type);
149 		return -EINVAL;
150 	}
151 
152 	if (unlikely(!(action->flags & MLX5HWS_ACTION_FLAG_HWS_FDB))) {
153 		pr_warn("HWS: Invalid action->flags: %d\n", action->flags);
154 		return -EINVAL;
155 	}
156 
157 	ret = hws_action_get_shared_stc_nic(ctx, stc_type, MLX5HWS_TABLE_TYPE_FDB);
158 	if (ret) {
159 		mlx5hws_err(ctx,
160 			    "Failed to allocate memory for FDB shared STCs (type: %d)\n",
161 			    stc_type);
162 		return ret;
163 	}
164 
165 	return 0;
166 }
167 
hws_action_put_shared_stc(struct mlx5hws_action * action,enum mlx5hws_context_shared_stc_type stc_type)168 static void hws_action_put_shared_stc(struct mlx5hws_action *action,
169 				      enum mlx5hws_context_shared_stc_type stc_type)
170 {
171 	enum mlx5hws_table_type tbl_type = MLX5HWS_TABLE_TYPE_FDB;
172 	struct mlx5hws_action_shared_stc *shared_stc;
173 	struct mlx5hws_context *ctx = action->ctx;
174 
175 	if (stc_type >= MLX5HWS_CONTEXT_SHARED_STC_MAX) {
176 		pr_warn("HWS: Invalid shared stc_type: %d\n", stc_type);
177 		return;
178 	}
179 
180 	mutex_lock(&ctx->ctrl_lock);
181 	if (--ctx->common_res[tbl_type].shared_stc[stc_type]->refcount) {
182 		mutex_unlock(&ctx->ctrl_lock);
183 		return;
184 	}
185 
186 	shared_stc = ctx->common_res[tbl_type].shared_stc[stc_type];
187 
188 	mlx5hws_action_free_single_stc(ctx, tbl_type, &shared_stc->stc_chunk);
189 	kfree(shared_stc);
190 	ctx->common_res[tbl_type].shared_stc[stc_type] = NULL;
191 	mutex_unlock(&ctx->ctrl_lock);
192 }
193 
hws_action_print_combo(struct mlx5hws_context * ctx,enum mlx5hws_action_type * user_actions)194 static void hws_action_print_combo(struct mlx5hws_context *ctx,
195 				   enum mlx5hws_action_type *user_actions)
196 {
197 	mlx5hws_err(ctx, "Invalid action_type sequence");
198 	while (*user_actions != MLX5HWS_ACTION_TYP_LAST) {
199 		mlx5hws_err(ctx, " %s", mlx5hws_action_type_to_str(*user_actions));
200 		user_actions++;
201 	}
202 	mlx5hws_err(ctx, "\n");
203 }
204 
mlx5hws_action_check_combo(struct mlx5hws_context * ctx,enum mlx5hws_action_type * user_actions,enum mlx5hws_table_type table_type)205 bool mlx5hws_action_check_combo(struct mlx5hws_context *ctx,
206 				enum mlx5hws_action_type *user_actions,
207 				enum mlx5hws_table_type table_type)
208 {
209 	const u32 *order_arr = action_order_arr[table_type];
210 	u8 order_idx = 0;
211 	u8 user_idx = 0;
212 	bool valid_combo;
213 
214 	if (table_type >= MLX5HWS_TABLE_TYPE_MAX) {
215 		mlx5hws_err(ctx, "Invalid table_type %d", table_type);
216 		return false;
217 	}
218 
219 	while (order_arr[order_idx] != BIT(MLX5HWS_ACTION_TYP_LAST)) {
220 		/* User action order validated move to next user action */
221 		if (BIT(user_actions[user_idx]) & order_arr[order_idx])
222 			user_idx++;
223 
224 		/* Iterate to the next supported action in the order */
225 		order_idx++;
226 	}
227 
228 	/* Combination is valid if all user action were processed */
229 	valid_combo = user_actions[user_idx] == MLX5HWS_ACTION_TYP_LAST;
230 	if (!valid_combo)
231 		hws_action_print_combo(ctx, user_actions);
232 
233 	return valid_combo;
234 }
235 
236 static bool
hws_action_fixup_stc_attr(struct mlx5hws_context * ctx,struct mlx5hws_cmd_stc_modify_attr * stc_attr,struct mlx5hws_cmd_stc_modify_attr * fixup_stc_attr,enum mlx5hws_table_type table_type,bool is_mirror)237 hws_action_fixup_stc_attr(struct mlx5hws_context *ctx,
238 			  struct mlx5hws_cmd_stc_modify_attr *stc_attr,
239 			  struct mlx5hws_cmd_stc_modify_attr *fixup_stc_attr,
240 			  enum mlx5hws_table_type table_type,
241 			  bool is_mirror)
242 {
243 	bool use_fixup = false;
244 	u32 fw_tbl_type;
245 	u32 base_id;
246 
247 	fw_tbl_type = mlx5hws_table_get_res_fw_ft_type(table_type, is_mirror);
248 
249 	switch (stc_attr->action_type) {
250 	case MLX5_IFC_STC_ACTION_TYPE_JUMP_TO_STE_TABLE:
251 		if (is_mirror && stc_attr->ste_table.ignore_tx) {
252 			fixup_stc_attr->action_type = MLX5_IFC_STC_ACTION_TYPE_DROP;
253 			fixup_stc_attr->action_offset = MLX5HWS_ACTION_OFFSET_HIT;
254 			fixup_stc_attr->stc_offset = stc_attr->stc_offset;
255 			use_fixup = true;
256 			break;
257 		}
258 		if (!is_mirror)
259 			base_id = mlx5hws_pool_chunk_get_base_id(stc_attr->ste_table.ste_pool,
260 								 &stc_attr->ste_table.ste);
261 		else
262 			base_id =
263 				mlx5hws_pool_chunk_get_base_mirror_id(stc_attr->ste_table.ste_pool,
264 								      &stc_attr->ste_table.ste);
265 
266 		*fixup_stc_attr = *stc_attr;
267 		fixup_stc_attr->ste_table.ste_obj_id = base_id;
268 		use_fixup = true;
269 		break;
270 
271 	case MLX5_IFC_STC_ACTION_TYPE_TAG:
272 		if (fw_tbl_type == FS_FT_FDB_TX) {
273 			fixup_stc_attr->action_type = MLX5_IFC_STC_ACTION_TYPE_NOP;
274 			fixup_stc_attr->action_offset = MLX5HWS_ACTION_OFFSET_DW5;
275 			fixup_stc_attr->stc_offset = stc_attr->stc_offset;
276 			use_fixup = true;
277 		}
278 		break;
279 
280 	case MLX5_IFC_STC_ACTION_TYPE_ALLOW:
281 		if (fw_tbl_type == FS_FT_FDB_TX || fw_tbl_type == FS_FT_FDB_RX) {
282 			fixup_stc_attr->action_type = MLX5_IFC_STC_ACTION_TYPE_JUMP_TO_VPORT;
283 			fixup_stc_attr->action_offset = stc_attr->action_offset;
284 			fixup_stc_attr->stc_offset = stc_attr->stc_offset;
285 			fixup_stc_attr->vport.esw_owner_vhca_id = ctx->caps->vhca_id;
286 			fixup_stc_attr->vport.vport_num = ctx->caps->eswitch_manager_vport_number;
287 			fixup_stc_attr->vport.eswitch_owner_vhca_id_valid =
288 				ctx->caps->merged_eswitch;
289 			use_fixup = true;
290 		}
291 		break;
292 
293 	case MLX5_IFC_STC_ACTION_TYPE_JUMP_TO_VPORT:
294 		if (stc_attr->vport.vport_num != MLX5_VPORT_UPLINK)
295 			break;
296 
297 		if (fw_tbl_type == FS_FT_FDB_TX || fw_tbl_type == FS_FT_FDB_RX) {
298 			/* The FW doesn't allow to go to wire in the TX/RX by JUMP_TO_VPORT */
299 			fixup_stc_attr->action_type = MLX5_IFC_STC_ACTION_TYPE_JUMP_TO_UPLINK;
300 			fixup_stc_attr->action_offset = stc_attr->action_offset;
301 			fixup_stc_attr->stc_offset = stc_attr->stc_offset;
302 			fixup_stc_attr->vport.vport_num = 0;
303 			fixup_stc_attr->vport.esw_owner_vhca_id = stc_attr->vport.esw_owner_vhca_id;
304 			fixup_stc_attr->vport.eswitch_owner_vhca_id_valid =
305 				stc_attr->vport.eswitch_owner_vhca_id_valid;
306 		}
307 		use_fixup = true;
308 		break;
309 
310 	default:
311 		break;
312 	}
313 
314 	return use_fixup;
315 }
316 
mlx5hws_action_alloc_single_stc(struct mlx5hws_context * ctx,struct mlx5hws_cmd_stc_modify_attr * stc_attr,u32 table_type,struct mlx5hws_pool_chunk * stc)317 int mlx5hws_action_alloc_single_stc(struct mlx5hws_context *ctx,
318 				    struct mlx5hws_cmd_stc_modify_attr *stc_attr,
319 				    u32 table_type,
320 				    struct mlx5hws_pool_chunk *stc)
321 __must_hold(&ctx->ctrl_lock)
322 {
323 	struct mlx5hws_cmd_stc_modify_attr cleanup_stc_attr = {0};
324 	struct mlx5hws_pool *stc_pool = ctx->stc_pool[table_type];
325 	struct mlx5hws_cmd_stc_modify_attr fixup_stc_attr = {0};
326 	bool use_fixup;
327 	u32 obj_0_id;
328 	int ret;
329 
330 	ret = mlx5hws_pool_chunk_alloc(stc_pool, stc);
331 	if (ret) {
332 		mlx5hws_err(ctx, "Failed to allocate single action STC\n");
333 		return ret;
334 	}
335 
336 	stc_attr->stc_offset = stc->offset;
337 
338 	/* Dynamic reparse not supported, overwrite and use default */
339 	if (!mlx5hws_context_cap_dynamic_reparse(ctx))
340 		stc_attr->reparse_mode = MLX5_IFC_STC_REPARSE_IGNORE;
341 
342 	obj_0_id = mlx5hws_pool_chunk_get_base_id(stc_pool, stc);
343 
344 	/* According to table/action limitation change the stc_attr */
345 	use_fixup = hws_action_fixup_stc_attr(ctx, stc_attr, &fixup_stc_attr, table_type, false);
346 	ret = mlx5hws_cmd_stc_modify(ctx->mdev, obj_0_id,
347 				     use_fixup ? &fixup_stc_attr : stc_attr);
348 	if (ret) {
349 		mlx5hws_err(ctx, "Failed to modify STC action_type %d tbl_type %d\n",
350 			    stc_attr->action_type, table_type);
351 		goto free_chunk;
352 	}
353 
354 	/* Modify the FDB peer */
355 	if (table_type == MLX5HWS_TABLE_TYPE_FDB) {
356 		u32 obj_1_id;
357 
358 		obj_1_id = mlx5hws_pool_chunk_get_base_mirror_id(stc_pool, stc);
359 
360 		use_fixup = hws_action_fixup_stc_attr(ctx, stc_attr,
361 						      &fixup_stc_attr,
362 						      table_type, true);
363 		ret = mlx5hws_cmd_stc_modify(ctx->mdev, obj_1_id,
364 					     use_fixup ? &fixup_stc_attr : stc_attr);
365 		if (ret) {
366 			mlx5hws_err(ctx,
367 				    "Failed to modify peer STC action_type %d tbl_type %d\n",
368 				    stc_attr->action_type, table_type);
369 			goto clean_obj_0;
370 		}
371 	}
372 
373 	return 0;
374 
375 clean_obj_0:
376 	cleanup_stc_attr.action_type = MLX5_IFC_STC_ACTION_TYPE_DROP;
377 	cleanup_stc_attr.action_offset = MLX5HWS_ACTION_OFFSET_HIT;
378 	cleanup_stc_attr.stc_offset = stc->offset;
379 	mlx5hws_cmd_stc_modify(ctx->mdev, obj_0_id, &cleanup_stc_attr);
380 free_chunk:
381 	mlx5hws_pool_chunk_free(stc_pool, stc);
382 	return ret;
383 }
384 
mlx5hws_action_free_single_stc(struct mlx5hws_context * ctx,u32 table_type,struct mlx5hws_pool_chunk * stc)385 void mlx5hws_action_free_single_stc(struct mlx5hws_context *ctx,
386 				    u32 table_type,
387 				    struct mlx5hws_pool_chunk *stc)
388 __must_hold(&ctx->ctrl_lock)
389 {
390 	struct mlx5hws_pool *stc_pool = ctx->stc_pool[table_type];
391 	struct mlx5hws_cmd_stc_modify_attr stc_attr = {0};
392 	u32 obj_id;
393 
394 	/* Modify the STC not to point to an object */
395 	stc_attr.action_type = MLX5_IFC_STC_ACTION_TYPE_DROP;
396 	stc_attr.action_offset = MLX5HWS_ACTION_OFFSET_HIT;
397 	stc_attr.stc_offset = stc->offset;
398 	obj_id = mlx5hws_pool_chunk_get_base_id(stc_pool, stc);
399 	mlx5hws_cmd_stc_modify(ctx->mdev, obj_id, &stc_attr);
400 
401 	if (table_type == MLX5HWS_TABLE_TYPE_FDB) {
402 		obj_id = mlx5hws_pool_chunk_get_base_mirror_id(stc_pool, stc);
403 		mlx5hws_cmd_stc_modify(ctx->mdev, obj_id, &stc_attr);
404 	}
405 
406 	mlx5hws_pool_chunk_free(stc_pool, stc);
407 }
408 
hws_action_get_mh_stc_type(struct mlx5hws_context * ctx,__be64 pattern)409 static u32 hws_action_get_mh_stc_type(struct mlx5hws_context *ctx,
410 				      __be64 pattern)
411 {
412 	u8 action_type = MLX5_GET(set_action_in, &pattern, action_type);
413 
414 	switch (action_type) {
415 	case MLX5_MODIFICATION_TYPE_SET:
416 		return MLX5_IFC_STC_ACTION_TYPE_SET;
417 	case MLX5_MODIFICATION_TYPE_ADD:
418 		return MLX5_IFC_STC_ACTION_TYPE_ADD;
419 	case MLX5_MODIFICATION_TYPE_COPY:
420 		return MLX5_IFC_STC_ACTION_TYPE_COPY;
421 	case MLX5_MODIFICATION_TYPE_ADD_FIELD:
422 		return MLX5_IFC_STC_ACTION_TYPE_ADD_FIELD;
423 	default:
424 		mlx5hws_err(ctx, "Unsupported action type: 0x%x\n", action_type);
425 		return MLX5_IFC_STC_ACTION_TYPE_NOP;
426 	}
427 }
428 
hws_action_fill_stc_attr(struct mlx5hws_action * action,u32 obj_id,struct mlx5hws_cmd_stc_modify_attr * attr)429 static void hws_action_fill_stc_attr(struct mlx5hws_action *action,
430 				     u32 obj_id,
431 				     struct mlx5hws_cmd_stc_modify_attr *attr)
432 {
433 	attr->reparse_mode = MLX5_IFC_STC_REPARSE_IGNORE;
434 
435 	switch (action->type) {
436 	case MLX5HWS_ACTION_TYP_TAG:
437 		attr->action_type = MLX5_IFC_STC_ACTION_TYPE_TAG;
438 		attr->action_offset = MLX5HWS_ACTION_OFFSET_DW5;
439 		break;
440 	case MLX5HWS_ACTION_TYP_DROP:
441 		attr->action_type = MLX5_IFC_STC_ACTION_TYPE_DROP;
442 		attr->action_offset = MLX5HWS_ACTION_OFFSET_HIT;
443 		break;
444 	case MLX5HWS_ACTION_TYP_MISS:
445 		attr->action_type = MLX5_IFC_STC_ACTION_TYPE_ALLOW;
446 		attr->action_offset = MLX5HWS_ACTION_OFFSET_HIT;
447 		break;
448 	case MLX5HWS_ACTION_TYP_CTR:
449 		attr->id = obj_id;
450 		attr->action_type = MLX5_IFC_STC_ACTION_TYPE_COUNTER;
451 		attr->action_offset = MLX5HWS_ACTION_OFFSET_DW0;
452 		break;
453 	case MLX5HWS_ACTION_TYP_REFORMAT_TNL_L3_TO_L2:
454 	case MLX5HWS_ACTION_TYP_MODIFY_HDR:
455 		attr->action_offset = MLX5HWS_ACTION_OFFSET_DW6;
456 		attr->reparse_mode = MLX5_IFC_STC_REPARSE_IGNORE;
457 		if (action->modify_header.require_reparse)
458 			attr->reparse_mode = MLX5_IFC_STC_REPARSE_ALWAYS;
459 
460 		if (action->modify_header.num_of_actions == 1) {
461 			attr->modify_action.data = action->modify_header.single_action;
462 			attr->action_type = hws_action_get_mh_stc_type(action->ctx,
463 								       attr->modify_action.data);
464 
465 			if (attr->action_type == MLX5_IFC_STC_ACTION_TYPE_ADD ||
466 			    attr->action_type == MLX5_IFC_STC_ACTION_TYPE_SET)
467 				MLX5_SET(set_action_in, &attr->modify_action.data, data, 0);
468 		} else {
469 			attr->action_type = MLX5_IFC_STC_ACTION_TYPE_ACC_MODIFY_LIST;
470 			attr->modify_header.arg_id = action->modify_header.arg_id;
471 			attr->modify_header.pattern_id = action->modify_header.pat_id;
472 		}
473 		break;
474 	case MLX5HWS_ACTION_TYP_TBL:
475 	case MLX5HWS_ACTION_TYP_DEST_ARRAY:
476 		attr->action_type = MLX5_IFC_STC_ACTION_TYPE_JUMP_TO_FT;
477 		attr->action_offset = MLX5HWS_ACTION_OFFSET_HIT;
478 		attr->dest_table_id = obj_id;
479 		break;
480 	case MLX5HWS_ACTION_TYP_REFORMAT_TNL_L2_TO_L2:
481 		attr->action_type = MLX5_IFC_STC_ACTION_TYPE_HEADER_REMOVE;
482 		attr->action_offset = MLX5HWS_ACTION_OFFSET_DW5;
483 		attr->reparse_mode = MLX5_IFC_STC_REPARSE_ALWAYS;
484 		attr->remove_header.decap = 1;
485 		attr->remove_header.start_anchor = MLX5_HEADER_ANCHOR_PACKET_START;
486 		attr->remove_header.end_anchor = MLX5_HEADER_ANCHOR_INNER_MAC;
487 		break;
488 	case MLX5HWS_ACTION_TYP_REFORMAT_L2_TO_TNL_L2:
489 	case MLX5HWS_ACTION_TYP_REFORMAT_L2_TO_TNL_L3:
490 	case MLX5HWS_ACTION_TYP_INSERT_HEADER:
491 		attr->reparse_mode = MLX5_IFC_STC_REPARSE_ALWAYS;
492 		if (!action->reformat.require_reparse)
493 			attr->reparse_mode = MLX5_IFC_STC_REPARSE_IGNORE;
494 
495 		attr->action_type = MLX5_IFC_STC_ACTION_TYPE_HEADER_INSERT;
496 		attr->action_offset = MLX5HWS_ACTION_OFFSET_DW6;
497 		attr->insert_header.encap = action->reformat.encap;
498 		attr->insert_header.insert_anchor = action->reformat.anchor;
499 		attr->insert_header.arg_id = action->reformat.arg_id;
500 		attr->insert_header.header_size = action->reformat.header_size;
501 		attr->insert_header.insert_offset = action->reformat.offset;
502 		break;
503 	case MLX5HWS_ACTION_TYP_ASO_METER:
504 		attr->action_offset = MLX5HWS_ACTION_OFFSET_DW6;
505 		attr->action_type = MLX5_IFC_STC_ACTION_TYPE_ASO;
506 		attr->aso.aso_type = ASO_OPC_MOD_POLICER;
507 		attr->aso.devx_obj_id = obj_id;
508 		attr->aso.return_reg_id = action->aso.return_reg_id;
509 		break;
510 	case MLX5HWS_ACTION_TYP_VPORT:
511 		attr->action_offset = MLX5HWS_ACTION_OFFSET_HIT;
512 		attr->action_type = MLX5_IFC_STC_ACTION_TYPE_JUMP_TO_VPORT;
513 		attr->vport.vport_num = action->vport.vport_num;
514 		attr->vport.esw_owner_vhca_id =	action->vport.esw_owner_vhca_id;
515 		attr->vport.eswitch_owner_vhca_id_valid = action->vport.esw_owner_vhca_id_valid;
516 		break;
517 	case MLX5HWS_ACTION_TYP_POP_VLAN:
518 		attr->action_type = MLX5_IFC_STC_ACTION_TYPE_REMOVE_WORDS;
519 		attr->action_offset = MLX5HWS_ACTION_OFFSET_DW5;
520 		attr->reparse_mode = MLX5_IFC_STC_REPARSE_ALWAYS;
521 		attr->remove_words.start_anchor = MLX5_HEADER_ANCHOR_FIRST_VLAN_START;
522 		attr->remove_words.num_of_words = MLX5HWS_ACTION_HDR_LEN_L2_VLAN / 2;
523 		break;
524 	case MLX5HWS_ACTION_TYP_PUSH_VLAN:
525 		attr->action_type = MLX5_IFC_STC_ACTION_TYPE_HEADER_INSERT;
526 		attr->action_offset = MLX5HWS_ACTION_OFFSET_DW6;
527 		attr->reparse_mode = MLX5_IFC_STC_REPARSE_ALWAYS;
528 		attr->insert_header.encap = 0;
529 		attr->insert_header.is_inline = 1;
530 		attr->insert_header.insert_anchor = MLX5_HEADER_ANCHOR_PACKET_START;
531 		attr->insert_header.insert_offset = MLX5HWS_ACTION_HDR_LEN_L2_MACS;
532 		attr->insert_header.header_size = MLX5HWS_ACTION_HDR_LEN_L2_VLAN;
533 		break;
534 	case MLX5HWS_ACTION_TYP_REMOVE_HEADER:
535 		attr->action_type = MLX5_IFC_STC_ACTION_TYPE_REMOVE_WORDS;
536 		attr->remove_header.decap = 0; /* the mode we support decap is 0 */
537 		attr->remove_words.start_anchor = action->remove_header.anchor;
538 		/* the size is in already in words */
539 		attr->remove_words.num_of_words = action->remove_header.size;
540 		attr->action_offset = MLX5HWS_ACTION_OFFSET_DW5;
541 		attr->reparse_mode = MLX5_IFC_STC_REPARSE_ALWAYS;
542 		break;
543 	default:
544 		mlx5hws_err(action->ctx, "Invalid action type %d\n", action->type);
545 	}
546 }
547 
548 static int
hws_action_create_stcs(struct mlx5hws_action * action,u32 obj_id)549 hws_action_create_stcs(struct mlx5hws_action *action, u32 obj_id)
550 {
551 	struct mlx5hws_cmd_stc_modify_attr stc_attr = {0};
552 	struct mlx5hws_context *ctx = action->ctx;
553 	int ret;
554 
555 	hws_action_fill_stc_attr(action, obj_id, &stc_attr);
556 
557 	/* Block unsupported parallel obj modify over the same base */
558 	mutex_lock(&ctx->ctrl_lock);
559 
560 	/* Allocate STC for FDB */
561 	if (action->flags & MLX5HWS_ACTION_FLAG_HWS_FDB) {
562 		ret = mlx5hws_action_alloc_single_stc(ctx, &stc_attr,
563 						      MLX5HWS_TABLE_TYPE_FDB,
564 						      &action->stc[MLX5HWS_TABLE_TYPE_FDB]);
565 		if (ret)
566 			goto out_err;
567 	}
568 
569 	mutex_unlock(&ctx->ctrl_lock);
570 
571 	return 0;
572 
573 out_err:
574 	mutex_unlock(&ctx->ctrl_lock);
575 	return ret;
576 }
577 
578 static void
hws_action_destroy_stcs(struct mlx5hws_action * action)579 hws_action_destroy_stcs(struct mlx5hws_action *action)
580 {
581 	struct mlx5hws_context *ctx = action->ctx;
582 
583 	/* Block unsupported parallel obj modify over the same base */
584 	mutex_lock(&ctx->ctrl_lock);
585 
586 	if (action->flags & MLX5HWS_ACTION_FLAG_HWS_FDB)
587 		mlx5hws_action_free_single_stc(ctx, MLX5HWS_TABLE_TYPE_FDB,
588 					       &action->stc[MLX5HWS_TABLE_TYPE_FDB]);
589 
590 	mutex_unlock(&ctx->ctrl_lock);
591 }
592 
hws_action_is_flag_hws_fdb(u32 flags)593 static bool hws_action_is_flag_hws_fdb(u32 flags)
594 {
595 	return flags & MLX5HWS_ACTION_FLAG_HWS_FDB;
596 }
597 
598 static bool
hws_action_validate_hws_action(struct mlx5hws_context * ctx,u32 flags)599 hws_action_validate_hws_action(struct mlx5hws_context *ctx, u32 flags)
600 {
601 	if (!(ctx->flags & MLX5HWS_CONTEXT_FLAG_HWS_SUPPORT)) {
602 		mlx5hws_err(ctx, "Cannot create HWS action since HWS is not supported\n");
603 		return false;
604 	}
605 
606 	if ((flags & MLX5HWS_ACTION_FLAG_HWS_FDB) && !ctx->caps->eswitch_manager) {
607 		mlx5hws_err(ctx, "Cannot create HWS action for FDB for non-eswitch-manager\n");
608 		return false;
609 	}
610 
611 	return true;
612 }
613 
614 static struct mlx5hws_action *
hws_action_create_generic_bulk(struct mlx5hws_context * ctx,u32 flags,enum mlx5hws_action_type action_type,u8 bulk_sz)615 hws_action_create_generic_bulk(struct mlx5hws_context *ctx,
616 			       u32 flags,
617 			       enum mlx5hws_action_type action_type,
618 			       u8 bulk_sz)
619 {
620 	struct mlx5hws_action *action;
621 	int i;
622 
623 	if (!hws_action_is_flag_hws_fdb(flags)) {
624 		mlx5hws_err(ctx,
625 			    "Action (type: %d) flags must specify only HWS FDB\n", action_type);
626 		return NULL;
627 	}
628 
629 	if (!hws_action_validate_hws_action(ctx, flags))
630 		return NULL;
631 
632 	action = kcalloc(bulk_sz, sizeof(*action), GFP_KERNEL);
633 	if (!action)
634 		return NULL;
635 
636 	for (i = 0; i < bulk_sz; i++) {
637 		action[i].ctx = ctx;
638 		action[i].flags = flags;
639 		action[i].type = action_type;
640 	}
641 
642 	return action;
643 }
644 
645 static struct mlx5hws_action *
hws_action_create_generic(struct mlx5hws_context * ctx,u32 flags,enum mlx5hws_action_type action_type)646 hws_action_create_generic(struct mlx5hws_context *ctx,
647 			  u32 flags,
648 			  enum mlx5hws_action_type action_type)
649 {
650 	return hws_action_create_generic_bulk(ctx, flags, action_type, 1);
651 }
652 
653 struct mlx5hws_action *
mlx5hws_action_create_dest_table_num(struct mlx5hws_context * ctx,u32 table_id,u32 flags)654 mlx5hws_action_create_dest_table_num(struct mlx5hws_context *ctx,
655 				     u32 table_id,
656 				     u32 flags)
657 {
658 	struct mlx5hws_action *action;
659 	int ret;
660 
661 	action = hws_action_create_generic(ctx, flags, MLX5HWS_ACTION_TYP_TBL);
662 	if (!action)
663 		return NULL;
664 
665 	ret = hws_action_create_stcs(action, table_id);
666 	if (ret)
667 		goto free_action;
668 
669 	action->dest_obj.obj_id = table_id;
670 
671 	return action;
672 
673 free_action:
674 	kfree(action);
675 	return NULL;
676 }
677 
678 struct mlx5hws_action *
mlx5hws_action_create_dest_table(struct mlx5hws_context * ctx,struct mlx5hws_table * tbl,u32 flags)679 mlx5hws_action_create_dest_table(struct mlx5hws_context *ctx,
680 				 struct mlx5hws_table *tbl,
681 				 u32 flags)
682 {
683 	return mlx5hws_action_create_dest_table_num(ctx, tbl->ft_id, flags);
684 }
685 
686 struct mlx5hws_action *
mlx5hws_action_create_dest_drop(struct mlx5hws_context * ctx,u32 flags)687 mlx5hws_action_create_dest_drop(struct mlx5hws_context *ctx, u32 flags)
688 {
689 	struct mlx5hws_action *action;
690 	int ret;
691 
692 	action = hws_action_create_generic(ctx, flags, MLX5HWS_ACTION_TYP_DROP);
693 	if (!action)
694 		return NULL;
695 
696 	ret = hws_action_create_stcs(action, 0);
697 	if (ret)
698 		goto free_action;
699 
700 	return action;
701 
702 free_action:
703 	kfree(action);
704 	return NULL;
705 }
706 
707 struct mlx5hws_action *
mlx5hws_action_create_default_miss(struct mlx5hws_context * ctx,u32 flags)708 mlx5hws_action_create_default_miss(struct mlx5hws_context *ctx, u32 flags)
709 {
710 	struct mlx5hws_action *action;
711 	int ret;
712 
713 	action = hws_action_create_generic(ctx, flags, MLX5HWS_ACTION_TYP_MISS);
714 	if (!action)
715 		return NULL;
716 
717 	ret = hws_action_create_stcs(action, 0);
718 	if (ret)
719 		goto free_action;
720 
721 	return action;
722 
723 free_action:
724 	kfree(action);
725 	return NULL;
726 }
727 
728 struct mlx5hws_action *
mlx5hws_action_create_tag(struct mlx5hws_context * ctx,u32 flags)729 mlx5hws_action_create_tag(struct mlx5hws_context *ctx, u32 flags)
730 {
731 	struct mlx5hws_action *action;
732 	int ret;
733 
734 	action = hws_action_create_generic(ctx, flags, MLX5HWS_ACTION_TYP_TAG);
735 	if (!action)
736 		return NULL;
737 
738 	ret = hws_action_create_stcs(action, 0);
739 	if (ret)
740 		goto free_action;
741 
742 	return action;
743 
744 free_action:
745 	kfree(action);
746 	return NULL;
747 }
748 
749 static struct mlx5hws_action *
hws_action_create_aso(struct mlx5hws_context * ctx,enum mlx5hws_action_type action_type,u32 obj_id,u8 return_reg_id,u32 flags)750 hws_action_create_aso(struct mlx5hws_context *ctx,
751 		      enum mlx5hws_action_type action_type,
752 		      u32 obj_id,
753 		      u8 return_reg_id,
754 		      u32 flags)
755 {
756 	struct mlx5hws_action *action;
757 	int ret;
758 
759 	action = hws_action_create_generic(ctx, flags, action_type);
760 	if (!action)
761 		return NULL;
762 
763 	action->aso.obj_id = obj_id;
764 	action->aso.return_reg_id = return_reg_id;
765 
766 	ret = hws_action_create_stcs(action, obj_id);
767 	if (ret)
768 		goto free_action;
769 
770 	return action;
771 
772 free_action:
773 	kfree(action);
774 	return NULL;
775 }
776 
777 struct mlx5hws_action *
mlx5hws_action_create_aso_meter(struct mlx5hws_context * ctx,u32 obj_id,u8 return_reg_id,u32 flags)778 mlx5hws_action_create_aso_meter(struct mlx5hws_context *ctx,
779 				u32 obj_id,
780 				u8 return_reg_id,
781 				u32 flags)
782 {
783 	return hws_action_create_aso(ctx, MLX5HWS_ACTION_TYP_ASO_METER,
784 				     obj_id, return_reg_id, flags);
785 }
786 
787 struct mlx5hws_action *
mlx5hws_action_create_counter(struct mlx5hws_context * ctx,u32 obj_id,u32 flags)788 mlx5hws_action_create_counter(struct mlx5hws_context *ctx,
789 			      u32 obj_id,
790 			      u32 flags)
791 {
792 	struct mlx5hws_action *action;
793 	int ret;
794 
795 	action = hws_action_create_generic(ctx, flags, MLX5HWS_ACTION_TYP_CTR);
796 	if (!action)
797 		return NULL;
798 
799 	ret = hws_action_create_stcs(action, obj_id);
800 	if (ret)
801 		goto free_action;
802 
803 	return action;
804 
805 free_action:
806 	kfree(action);
807 	return NULL;
808 }
809 
810 struct mlx5hws_action *
mlx5hws_action_create_dest_vport(struct mlx5hws_context * ctx,u16 vport_num,bool vhca_id_valid,u16 vhca_id,u32 flags)811 mlx5hws_action_create_dest_vport(struct mlx5hws_context *ctx,
812 				 u16 vport_num,
813 				 bool vhca_id_valid,
814 				 u16 vhca_id,
815 				 u32 flags)
816 {
817 	struct mlx5hws_action *action;
818 	int ret;
819 
820 	if (!(flags & MLX5HWS_ACTION_FLAG_HWS_FDB)) {
821 		mlx5hws_err(ctx, "Vport action is supported for FDB only\n");
822 		return NULL;
823 	}
824 
825 	action = hws_action_create_generic(ctx, flags, MLX5HWS_ACTION_TYP_VPORT);
826 	if (!action)
827 		return NULL;
828 
829 	if (!ctx->caps->merged_eswitch && vhca_id_valid && vhca_id != ctx->caps->vhca_id) {
830 		mlx5hws_err(ctx, "Non merged eswitch cannot send to other vhca\n");
831 		goto free_action;
832 	}
833 
834 	action->vport.vport_num = vport_num;
835 	action->vport.esw_owner_vhca_id_valid = vhca_id_valid;
836 
837 	if (vhca_id_valid)
838 		action->vport.esw_owner_vhca_id = vhca_id;
839 
840 	ret = hws_action_create_stcs(action, 0);
841 	if (ret) {
842 		mlx5hws_err(ctx, "Failed creating stc for vport %d\n", vport_num);
843 		goto free_action;
844 	}
845 
846 	return action;
847 
848 free_action:
849 	kfree(action);
850 	return NULL;
851 }
852 
853 struct mlx5hws_action *
mlx5hws_action_create_push_vlan(struct mlx5hws_context * ctx,u32 flags)854 mlx5hws_action_create_push_vlan(struct mlx5hws_context *ctx, u32 flags)
855 {
856 	struct mlx5hws_action *action;
857 	int ret;
858 
859 	action = hws_action_create_generic(ctx, flags, MLX5HWS_ACTION_TYP_PUSH_VLAN);
860 	if (!action)
861 		return NULL;
862 
863 	ret = hws_action_create_stcs(action, 0);
864 	if (ret) {
865 		mlx5hws_err(ctx, "Failed creating stc for push vlan\n");
866 		goto free_action;
867 	}
868 
869 	return action;
870 
871 free_action:
872 	kfree(action);
873 	return NULL;
874 }
875 
876 struct mlx5hws_action *
mlx5hws_action_create_pop_vlan(struct mlx5hws_context * ctx,u32 flags)877 mlx5hws_action_create_pop_vlan(struct mlx5hws_context *ctx, u32 flags)
878 {
879 	struct mlx5hws_action *action;
880 	int ret;
881 
882 	action = hws_action_create_generic(ctx, flags, MLX5HWS_ACTION_TYP_POP_VLAN);
883 	if (!action)
884 		return NULL;
885 
886 	ret = hws_action_get_shared_stc(action, MLX5HWS_CONTEXT_SHARED_STC_DOUBLE_POP);
887 	if (ret) {
888 		mlx5hws_err(ctx, "Failed to create remove stc for reformat\n");
889 		goto free_action;
890 	}
891 
892 	ret = hws_action_create_stcs(action, 0);
893 	if (ret) {
894 		mlx5hws_err(ctx, "Failed creating stc for pop vlan\n");
895 		goto free_shared;
896 	}
897 
898 	return action;
899 
900 free_shared:
901 	hws_action_put_shared_stc(action, MLX5HWS_CONTEXT_SHARED_STC_DOUBLE_POP);
902 free_action:
903 	kfree(action);
904 	return NULL;
905 }
906 
907 static int
hws_action_handle_insert_with_ptr(struct mlx5hws_action * action,u8 num_of_hdrs,struct mlx5hws_action_reformat_header * hdrs,u32 log_bulk_sz)908 hws_action_handle_insert_with_ptr(struct mlx5hws_action *action,
909 				  u8 num_of_hdrs,
910 				  struct mlx5hws_action_reformat_header *hdrs,
911 				  u32 log_bulk_sz)
912 {
913 	size_t max_sz = 0;
914 	u32 arg_id;
915 	int ret, i;
916 
917 	for (i = 0; i < num_of_hdrs; i++) {
918 		if (hdrs[i].sz % W_SIZE != 0) {
919 			mlx5hws_err(action->ctx,
920 				    "Header data size should be in WORD granularity\n");
921 			return -EINVAL;
922 		}
923 		max_sz = max(hdrs[i].sz, max_sz);
924 	}
925 
926 	/* Allocate single shared arg object for all headers */
927 	ret = mlx5hws_arg_create(action->ctx,
928 				 hdrs->data,
929 				 max_sz,
930 				 log_bulk_sz,
931 				 action->flags & MLX5HWS_ACTION_FLAG_SHARED,
932 				 &arg_id);
933 	if (ret)
934 		return ret;
935 
936 	for (i = 0; i < num_of_hdrs; i++) {
937 		action[i].reformat.arg_id = arg_id;
938 		action[i].reformat.header_size = hdrs[i].sz;
939 		action[i].reformat.num_of_hdrs = num_of_hdrs;
940 		action[i].reformat.max_hdr_sz = max_sz;
941 		action[i].reformat.require_reparse = true;
942 
943 		if (action[i].type == MLX5HWS_ACTION_TYP_REFORMAT_L2_TO_TNL_L2 ||
944 		    action[i].type == MLX5HWS_ACTION_TYP_REFORMAT_L2_TO_TNL_L3) {
945 			action[i].reformat.anchor = MLX5_HEADER_ANCHOR_PACKET_START;
946 			action[i].reformat.offset = 0;
947 			action[i].reformat.encap = 1;
948 		}
949 
950 		ret = hws_action_create_stcs(&action[i], 0);
951 		if (ret) {
952 			mlx5hws_err(action->ctx, "Failed to create stc for reformat\n");
953 			goto free_stc;
954 		}
955 	}
956 
957 	return 0;
958 
959 free_stc:
960 	while (i--)
961 		hws_action_destroy_stcs(&action[i]);
962 
963 	mlx5hws_arg_destroy(action->ctx, arg_id);
964 	return ret;
965 }
966 
967 static int
hws_action_handle_l2_to_tunnel_l3(struct mlx5hws_action * action,u8 num_of_hdrs,struct mlx5hws_action_reformat_header * hdrs,u32 log_bulk_sz)968 hws_action_handle_l2_to_tunnel_l3(struct mlx5hws_action *action,
969 				  u8 num_of_hdrs,
970 				  struct mlx5hws_action_reformat_header *hdrs,
971 				  u32 log_bulk_sz)
972 {
973 	int ret;
974 
975 	/* The action is remove-l2-header + insert-l3-header */
976 	ret = hws_action_get_shared_stc(action, MLX5HWS_CONTEXT_SHARED_STC_DECAP_L3);
977 	if (ret) {
978 		mlx5hws_err(action->ctx, "Failed to create remove stc for reformat\n");
979 		return ret;
980 	}
981 
982 	/* Reuse the insert with pointer for the L2L3 header */
983 	ret = hws_action_handle_insert_with_ptr(action,
984 						num_of_hdrs,
985 						hdrs,
986 						log_bulk_sz);
987 	if (ret)
988 		goto put_shared_stc;
989 
990 	return 0;
991 
992 put_shared_stc:
993 	hws_action_put_shared_stc(action, MLX5HWS_CONTEXT_SHARED_STC_DECAP_L3);
994 	return ret;
995 }
996 
hws_action_prepare_decap_l3_actions(size_t data_sz,u8 * mh_data,int * num_of_actions)997 static void hws_action_prepare_decap_l3_actions(size_t data_sz,
998 						u8 *mh_data,
999 						int *num_of_actions)
1000 {
1001 	int actions;
1002 	u32 i;
1003 
1004 	/* Remove L2L3 outer headers */
1005 	MLX5_SET(stc_ste_param_remove, mh_data, action_type,
1006 		 MLX5_MODIFICATION_TYPE_REMOVE);
1007 	MLX5_SET(stc_ste_param_remove, mh_data, decap, 0x1);
1008 	MLX5_SET(stc_ste_param_remove, mh_data, remove_start_anchor,
1009 		 MLX5_HEADER_ANCHOR_PACKET_START);
1010 	MLX5_SET(stc_ste_param_remove, mh_data, remove_end_anchor,
1011 		 MLX5_HEADER_ANCHOR_INNER_IPV6_IPV4);
1012 	mh_data += MLX5HWS_ACTION_DOUBLE_SIZE; /* Assume every action is 2 dw */
1013 	actions = 1;
1014 
1015 	/* Add the new header using inline action 4Byte at a time, the header
1016 	 * is added in reversed order to the beginning of the packet to avoid
1017 	 * incorrect parsing by the HW. Since header is 14B or 18B an extra
1018 	 * two bytes are padded and later removed.
1019 	 */
1020 	for (i = 0; i < data_sz / MLX5HWS_ACTION_INLINE_DATA_SIZE + 1; i++) {
1021 		MLX5_SET(stc_ste_param_insert, mh_data, action_type,
1022 			 MLX5_MODIFICATION_TYPE_INSERT);
1023 		MLX5_SET(stc_ste_param_insert, mh_data, inline_data, 0x1);
1024 		MLX5_SET(stc_ste_param_insert, mh_data, insert_anchor,
1025 			 MLX5_HEADER_ANCHOR_PACKET_START);
1026 		MLX5_SET(stc_ste_param_insert, mh_data, insert_size, 2);
1027 		mh_data += MLX5HWS_ACTION_DOUBLE_SIZE;
1028 		actions++;
1029 	}
1030 
1031 	/* Remove first 2 extra bytes */
1032 	MLX5_SET(stc_ste_param_remove_words, mh_data, action_type,
1033 		 MLX5_MODIFICATION_TYPE_REMOVE_WORDS);
1034 	MLX5_SET(stc_ste_param_remove_words, mh_data, remove_start_anchor,
1035 		 MLX5_HEADER_ANCHOR_PACKET_START);
1036 	/* The hardware expects here size in words (2 bytes) */
1037 	MLX5_SET(stc_ste_param_remove_words, mh_data, remove_size, 1);
1038 	actions++;
1039 
1040 	*num_of_actions = actions;
1041 }
1042 
1043 static int
hws_action_handle_tunnel_l3_to_l2(struct mlx5hws_action * action,u8 num_of_hdrs,struct mlx5hws_action_reformat_header * hdrs,u32 log_bulk_sz)1044 hws_action_handle_tunnel_l3_to_l2(struct mlx5hws_action *action,
1045 				  u8 num_of_hdrs,
1046 				  struct mlx5hws_action_reformat_header *hdrs,
1047 				  u32 log_bulk_sz)
1048 {
1049 	u8 mh_data[MLX5HWS_ACTION_REFORMAT_DATA_SIZE] = {0};
1050 	struct mlx5hws_context *ctx = action->ctx;
1051 	u32 arg_id, pat_id;
1052 	int num_of_actions;
1053 	int mh_data_size;
1054 	int ret, i;
1055 
1056 	for (i = 0; i < num_of_hdrs; i++) {
1057 		if (hdrs[i].sz != MLX5HWS_ACTION_HDR_LEN_L2 &&
1058 		    hdrs[i].sz != MLX5HWS_ACTION_HDR_LEN_L2_W_VLAN) {
1059 			mlx5hws_err(ctx, "Data size is not supported for decap-l3\n");
1060 			return -EINVAL;
1061 		}
1062 	}
1063 
1064 	/* Create a full modify header action list in case shared */
1065 	hws_action_prepare_decap_l3_actions(hdrs->sz, mh_data, &num_of_actions);
1066 	if (action->flags & MLX5HWS_ACTION_FLAG_SHARED)
1067 		mlx5hws_action_prepare_decap_l3_data(hdrs->data, mh_data, num_of_actions);
1068 
1069 	/* All DecapL3 cases require the same max arg size */
1070 	ret = mlx5hws_arg_create_modify_header_arg(ctx,
1071 						   (__be64 *)mh_data,
1072 						   num_of_actions,
1073 						   log_bulk_sz,
1074 						   action->flags & MLX5HWS_ACTION_FLAG_SHARED,
1075 						   &arg_id);
1076 	if (ret)
1077 		return ret;
1078 
1079 	for (i = 0; i < num_of_hdrs; i++) {
1080 		memset(mh_data, 0, MLX5HWS_ACTION_REFORMAT_DATA_SIZE);
1081 		hws_action_prepare_decap_l3_actions(hdrs[i].sz, mh_data, &num_of_actions);
1082 		mh_data_size = num_of_actions * MLX5HWS_MODIFY_ACTION_SIZE;
1083 
1084 		ret = mlx5hws_pat_get_pattern(ctx, (__be64 *)mh_data, mh_data_size, &pat_id);
1085 		if (ret) {
1086 			mlx5hws_err(ctx, "Failed to allocate pattern for DecapL3\n");
1087 			goto free_stc_and_pat;
1088 		}
1089 
1090 		action[i].modify_header.max_num_of_actions = num_of_actions;
1091 		action[i].modify_header.num_of_actions = num_of_actions;
1092 		action[i].modify_header.num_of_patterns = num_of_hdrs;
1093 		action[i].modify_header.arg_id = arg_id;
1094 		action[i].modify_header.pat_id = pat_id;
1095 		action[i].modify_header.require_reparse =
1096 			mlx5hws_pat_require_reparse((__be64 *)mh_data, num_of_actions);
1097 
1098 		ret = hws_action_create_stcs(&action[i], 0);
1099 		if (ret) {
1100 			mlx5hws_pat_put_pattern(ctx, pat_id);
1101 			goto free_stc_and_pat;
1102 		}
1103 	}
1104 
1105 	return 0;
1106 
1107 free_stc_and_pat:
1108 	while (i--) {
1109 		hws_action_destroy_stcs(&action[i]);
1110 		mlx5hws_pat_put_pattern(ctx, action[i].modify_header.pat_id);
1111 	}
1112 
1113 	mlx5hws_arg_destroy(action->ctx, arg_id);
1114 	return ret;
1115 }
1116 
1117 static int
hws_action_create_reformat_hws(struct mlx5hws_action * action,u8 num_of_hdrs,struct mlx5hws_action_reformat_header * hdrs,u32 bulk_size)1118 hws_action_create_reformat_hws(struct mlx5hws_action *action,
1119 			       u8 num_of_hdrs,
1120 			       struct mlx5hws_action_reformat_header *hdrs,
1121 			       u32 bulk_size)
1122 {
1123 	int ret;
1124 
1125 	switch (action->type) {
1126 	case MLX5HWS_ACTION_TYP_REFORMAT_TNL_L2_TO_L2:
1127 		ret = hws_action_create_stcs(action, 0);
1128 		break;
1129 	case MLX5HWS_ACTION_TYP_REFORMAT_L2_TO_TNL_L2:
1130 		ret = hws_action_handle_insert_with_ptr(action, num_of_hdrs, hdrs, bulk_size);
1131 		break;
1132 	case MLX5HWS_ACTION_TYP_REFORMAT_L2_TO_TNL_L3:
1133 		ret = hws_action_handle_l2_to_tunnel_l3(action, num_of_hdrs, hdrs, bulk_size);
1134 		break;
1135 	case MLX5HWS_ACTION_TYP_REFORMAT_TNL_L3_TO_L2:
1136 		ret = hws_action_handle_tunnel_l3_to_l2(action, num_of_hdrs, hdrs, bulk_size);
1137 		break;
1138 	default:
1139 		mlx5hws_err(action->ctx, "Invalid HWS reformat action type\n");
1140 		return -EINVAL;
1141 	}
1142 
1143 	return ret;
1144 }
1145 
1146 struct mlx5hws_action *
mlx5hws_action_create_reformat(struct mlx5hws_context * ctx,enum mlx5hws_action_type reformat_type,u8 num_of_hdrs,struct mlx5hws_action_reformat_header * hdrs,u32 log_bulk_size,u32 flags)1147 mlx5hws_action_create_reformat(struct mlx5hws_context *ctx,
1148 			       enum mlx5hws_action_type reformat_type,
1149 			       u8 num_of_hdrs,
1150 			       struct mlx5hws_action_reformat_header *hdrs,
1151 			       u32 log_bulk_size,
1152 			       u32 flags)
1153 {
1154 	struct mlx5hws_action *action;
1155 	int ret;
1156 
1157 	if (!num_of_hdrs) {
1158 		mlx5hws_err(ctx, "Reformat num_of_hdrs cannot be zero\n");
1159 		return NULL;
1160 	}
1161 
1162 	action = hws_action_create_generic_bulk(ctx, flags, reformat_type, num_of_hdrs);
1163 	if (!action)
1164 		return NULL;
1165 
1166 	if ((flags & MLX5HWS_ACTION_FLAG_SHARED) && (log_bulk_size || num_of_hdrs > 1)) {
1167 		mlx5hws_err(ctx, "Reformat flags don't fit HWS (flags: 0x%x)\n", flags);
1168 		goto free_action;
1169 	}
1170 
1171 	ret = hws_action_create_reformat_hws(action, num_of_hdrs, hdrs, log_bulk_size);
1172 	if (ret) {
1173 		mlx5hws_err(ctx, "Failed to create HWS reformat action\n");
1174 		goto free_action;
1175 	}
1176 
1177 	return action;
1178 
1179 free_action:
1180 	kfree(action);
1181 	return NULL;
1182 }
1183 
1184 static int
hws_action_create_modify_header_hws(struct mlx5hws_action * action,u8 num_of_patterns,struct mlx5hws_action_mh_pattern * pattern,u32 log_bulk_size)1185 hws_action_create_modify_header_hws(struct mlx5hws_action *action,
1186 				    u8 num_of_patterns,
1187 				    struct mlx5hws_action_mh_pattern *pattern,
1188 				    u32 log_bulk_size)
1189 {
1190 	struct mlx5hws_context *ctx = action->ctx;
1191 	u16 num_actions, max_mh_actions = 0;
1192 	int i, ret, size_in_bytes;
1193 	u32 pat_id, arg_id = 0;
1194 	__be64 *new_pattern;
1195 	size_t pat_max_sz;
1196 
1197 	pat_max_sz = MLX5HWS_ARG_CHUNK_SIZE_MAX * MLX5HWS_ARG_DATA_SIZE;
1198 	size_in_bytes = pat_max_sz * sizeof(__be64);
1199 	new_pattern = kcalloc(num_of_patterns, size_in_bytes, GFP_KERNEL);
1200 	if (!new_pattern)
1201 		return -ENOMEM;
1202 
1203 	/* Calculate maximum number of mh actions for shared arg allocation */
1204 	for (i = 0; i < num_of_patterns; i++) {
1205 		size_t new_num_actions;
1206 		size_t cur_num_actions;
1207 		u32 nope_location;
1208 
1209 		cur_num_actions = pattern[i].sz / MLX5HWS_MODIFY_ACTION_SIZE;
1210 
1211 		mlx5hws_pat_calc_nope(pattern[i].data, cur_num_actions,
1212 				      pat_max_sz / MLX5HWS_MODIFY_ACTION_SIZE,
1213 				      &new_num_actions, &nope_location,
1214 				      &new_pattern[i * pat_max_sz]);
1215 
1216 		action[i].modify_header.nope_locations = nope_location;
1217 		action[i].modify_header.num_of_actions = new_num_actions;
1218 
1219 		max_mh_actions = max(max_mh_actions, new_num_actions);
1220 	}
1221 
1222 	if (mlx5hws_arg_get_arg_log_size(max_mh_actions) >= MLX5HWS_ARG_CHUNK_SIZE_MAX) {
1223 		mlx5hws_err(ctx, "Num of actions (%d) bigger than allowed\n",
1224 			    max_mh_actions);
1225 		ret = -EINVAL;
1226 		goto free_new_pat;
1227 	}
1228 
1229 	/* Allocate single shared arg for all patterns based on the max size */
1230 	if (max_mh_actions > 1) {
1231 		ret = mlx5hws_arg_create_modify_header_arg(ctx,
1232 							   pattern->data,
1233 							   max_mh_actions,
1234 							   log_bulk_size,
1235 							   action->flags &
1236 							   MLX5HWS_ACTION_FLAG_SHARED,
1237 							   &arg_id);
1238 		if (ret)
1239 			goto free_new_pat;
1240 	}
1241 
1242 	for (i = 0; i < num_of_patterns; i++) {
1243 		if (!mlx5hws_pat_verify_actions(ctx, pattern[i].data, pattern[i].sz)) {
1244 			mlx5hws_err(ctx, "Fail to verify pattern modify actions\n");
1245 			ret = -EINVAL;
1246 			goto free_stc_and_pat;
1247 		}
1248 		num_actions = pattern[i].sz / MLX5HWS_MODIFY_ACTION_SIZE;
1249 		action[i].modify_header.num_of_patterns = num_of_patterns;
1250 		action[i].modify_header.max_num_of_actions = max_mh_actions;
1251 
1252 		action[i].modify_header.require_reparse =
1253 			mlx5hws_pat_require_reparse(pattern[i].data, num_actions);
1254 
1255 		if (num_actions == 1) {
1256 			pat_id = 0;
1257 			/* Optimize single modify action to be used inline */
1258 			action[i].modify_header.single_action = pattern[i].data[0];
1259 			action[i].modify_header.single_action_type =
1260 				MLX5_GET(set_action_in, pattern[i].data, action_type);
1261 		} else {
1262 			/* Multiple modify actions require a pattern */
1263 			if (unlikely(action[i].modify_header.nope_locations)) {
1264 				size_t pattern_sz;
1265 
1266 				pattern_sz = action[i].modify_header.num_of_actions *
1267 					     MLX5HWS_MODIFY_ACTION_SIZE;
1268 				ret =
1269 				mlx5hws_pat_get_pattern(ctx,
1270 							&new_pattern[i * pat_max_sz],
1271 							pattern_sz, &pat_id);
1272 			} else {
1273 				ret = mlx5hws_pat_get_pattern(ctx,
1274 							      pattern[i].data,
1275 							      pattern[i].sz,
1276 							      &pat_id);
1277 			}
1278 			if (ret) {
1279 				mlx5hws_err(ctx,
1280 					    "Failed to allocate pattern for modify header\n");
1281 				goto free_stc_and_pat;
1282 			}
1283 
1284 			action[i].modify_header.arg_id = arg_id;
1285 			action[i].modify_header.pat_id = pat_id;
1286 		}
1287 		/* Allocate STC for each action representing a header */
1288 		ret = hws_action_create_stcs(&action[i], 0);
1289 		if (ret) {
1290 			if (pat_id)
1291 				mlx5hws_pat_put_pattern(ctx, pat_id);
1292 			goto free_stc_and_pat;
1293 		}
1294 	}
1295 
1296 	kfree(new_pattern);
1297 	return 0;
1298 
1299 free_stc_and_pat:
1300 	while (i--) {
1301 		hws_action_destroy_stcs(&action[i]);
1302 		if (action[i].modify_header.pat_id)
1303 			mlx5hws_pat_put_pattern(ctx, action[i].modify_header.pat_id);
1304 	}
1305 
1306 	if (arg_id)
1307 		mlx5hws_arg_destroy(ctx, arg_id);
1308 free_new_pat:
1309 	kfree(new_pattern);
1310 	return ret;
1311 }
1312 
1313 struct mlx5hws_action *
mlx5hws_action_create_modify_header(struct mlx5hws_context * ctx,u8 num_of_patterns,struct mlx5hws_action_mh_pattern * patterns,u32 log_bulk_size,u32 flags)1314 mlx5hws_action_create_modify_header(struct mlx5hws_context *ctx,
1315 				    u8 num_of_patterns,
1316 				    struct mlx5hws_action_mh_pattern *patterns,
1317 				    u32 log_bulk_size,
1318 				    u32 flags)
1319 {
1320 	struct mlx5hws_action *action;
1321 	int ret;
1322 
1323 	if (!num_of_patterns) {
1324 		mlx5hws_err(ctx, "Invalid number of patterns\n");
1325 		return NULL;
1326 	}
1327 	action = hws_action_create_generic_bulk(ctx, flags,
1328 						MLX5HWS_ACTION_TYP_MODIFY_HDR,
1329 						num_of_patterns);
1330 	if (!action)
1331 		return NULL;
1332 
1333 	if ((flags & MLX5HWS_ACTION_FLAG_SHARED) && (log_bulk_size || num_of_patterns > 1)) {
1334 		mlx5hws_err(ctx, "Action cannot be shared with requested pattern or size\n");
1335 		goto free_action;
1336 	}
1337 
1338 	ret = hws_action_create_modify_header_hws(action,
1339 						  num_of_patterns,
1340 						  patterns,
1341 						  log_bulk_size);
1342 	if (ret)
1343 		goto free_action;
1344 
1345 	return action;
1346 
1347 free_action:
1348 	kfree(action);
1349 	return NULL;
1350 }
1351 
1352 struct mlx5hws_action *
mlx5hws_action_create_dest_array(struct mlx5hws_context * ctx,size_t num_dest,struct mlx5hws_action_dest_attr * dests,bool ignore_flow_level,u32 flow_source,u32 flags)1353 mlx5hws_action_create_dest_array(struct mlx5hws_context *ctx,
1354 				 size_t num_dest,
1355 				 struct mlx5hws_action_dest_attr *dests,
1356 				 bool ignore_flow_level,
1357 				 u32 flow_source,
1358 				 u32 flags)
1359 {
1360 	struct mlx5hws_cmd_set_fte_dest *dest_list = NULL;
1361 	struct mlx5hws_cmd_ft_create_attr ft_attr = {0};
1362 	struct mlx5hws_cmd_set_fte_attr fte_attr = {0};
1363 	struct mlx5hws_cmd_forward_tbl *fw_island;
1364 	struct mlx5hws_action *action;
1365 	u32 i /*, packet_reformat_id*/;
1366 	int ret;
1367 
1368 	if (num_dest <= 1) {
1369 		mlx5hws_err(ctx, "Action must have multiple dests\n");
1370 		return NULL;
1371 	}
1372 
1373 	if (flags == (MLX5HWS_ACTION_FLAG_HWS_FDB | MLX5HWS_ACTION_FLAG_SHARED)) {
1374 		ft_attr.type = FS_FT_FDB;
1375 		ft_attr.level = ctx->caps->fdb_ft.max_level - 1;
1376 	} else {
1377 		mlx5hws_err(ctx, "Action flags not supported\n");
1378 		return NULL;
1379 	}
1380 
1381 	dest_list = kcalloc(num_dest, sizeof(*dest_list), GFP_KERNEL);
1382 	if (!dest_list)
1383 		return NULL;
1384 
1385 	for (i = 0; i < num_dest; i++) {
1386 		enum mlx5hws_action_type action_type = dests[i].dest->type;
1387 		struct mlx5hws_action *reformat_action = dests[i].reformat;
1388 
1389 		switch (action_type) {
1390 		case MLX5HWS_ACTION_TYP_TBL:
1391 			dest_list[i].destination_type =
1392 				MLX5_FLOW_DESTINATION_TYPE_FLOW_TABLE;
1393 			dest_list[i].destination_id = dests[i].dest->dest_obj.obj_id;
1394 			fte_attr.action_flags |= MLX5_FLOW_CONTEXT_ACTION_FWD_DEST;
1395 			fte_attr.ignore_flow_level = ignore_flow_level;
1396 			/* ToDo: In SW steering we have a handling of 'go to WIRE'
1397 			 * destination here by upper layer setting 'is_wire_ft' flag
1398 			 * if the destination is wire.
1399 			 * This is because uplink should be last dest in the list.
1400 			 */
1401 			break;
1402 		case MLX5HWS_ACTION_TYP_VPORT:
1403 			dest_list[i].destination_type = MLX5_FLOW_DESTINATION_TYPE_VPORT;
1404 			dest_list[i].destination_id = dests[i].dest->vport.vport_num;
1405 			fte_attr.action_flags |= MLX5_FLOW_CONTEXT_ACTION_FWD_DEST;
1406 			if (ctx->caps->merged_eswitch) {
1407 				dest_list[i].ext_flags |=
1408 					MLX5HWS_CMD_EXT_DEST_ESW_OWNER_VHCA_ID;
1409 				dest_list[i].esw_owner_vhca_id =
1410 					dests[i].dest->vport.esw_owner_vhca_id;
1411 			}
1412 			break;
1413 		default:
1414 			mlx5hws_err(ctx, "Unsupported action in dest_array\n");
1415 			goto free_dest_list;
1416 		}
1417 
1418 		if (reformat_action) {
1419 			mlx5hws_err(ctx, "dest_array with reformat action - unsupported\n");
1420 			goto free_dest_list;
1421 		}
1422 	}
1423 
1424 	fte_attr.dests_num = num_dest;
1425 	fte_attr.dests = dest_list;
1426 
1427 	fw_island = mlx5hws_cmd_forward_tbl_create(ctx->mdev, &ft_attr, &fte_attr);
1428 	if (!fw_island)
1429 		goto free_dest_list;
1430 
1431 	action = hws_action_create_generic(ctx, flags, MLX5HWS_ACTION_TYP_DEST_ARRAY);
1432 	if (!action)
1433 		goto destroy_fw_island;
1434 
1435 	ret = hws_action_create_stcs(action, fw_island->ft_id);
1436 	if (ret)
1437 		goto free_action;
1438 
1439 	action->dest_array.fw_island = fw_island;
1440 	action->dest_array.num_dest = num_dest;
1441 	action->dest_array.dest_list = dest_list;
1442 
1443 	return action;
1444 
1445 free_action:
1446 	kfree(action);
1447 destroy_fw_island:
1448 	mlx5hws_cmd_forward_tbl_destroy(ctx->mdev, fw_island);
1449 free_dest_list:
1450 	for (i = 0; i < num_dest; i++) {
1451 		if (dest_list[i].ext_reformat_id)
1452 			mlx5hws_cmd_packet_reformat_destroy(ctx->mdev,
1453 							    dest_list[i].ext_reformat_id);
1454 	}
1455 	kfree(dest_list);
1456 	return NULL;
1457 }
1458 
1459 struct mlx5hws_action *
mlx5hws_action_create_insert_header(struct mlx5hws_context * ctx,u8 num_of_hdrs,struct mlx5hws_action_insert_header * hdrs,u32 log_bulk_size,u32 flags)1460 mlx5hws_action_create_insert_header(struct mlx5hws_context *ctx,
1461 				    u8 num_of_hdrs,
1462 				    struct mlx5hws_action_insert_header *hdrs,
1463 				    u32 log_bulk_size,
1464 				    u32 flags)
1465 {
1466 	struct mlx5hws_action_reformat_header *reformat_hdrs;
1467 	struct mlx5hws_action *action;
1468 	int ret;
1469 	int i;
1470 
1471 	action = hws_action_create_generic(ctx, flags, MLX5HWS_ACTION_TYP_INSERT_HEADER);
1472 	if (!action)
1473 		return NULL;
1474 
1475 	reformat_hdrs = kcalloc(num_of_hdrs, sizeof(*reformat_hdrs), GFP_KERNEL);
1476 	if (!reformat_hdrs)
1477 		goto free_action;
1478 
1479 	for (i = 0; i < num_of_hdrs; i++) {
1480 		if (hdrs[i].offset % W_SIZE != 0) {
1481 			mlx5hws_err(ctx, "Header offset should be in WORD granularity\n");
1482 			goto free_reformat_hdrs;
1483 		}
1484 
1485 		action[i].reformat.anchor = hdrs[i].anchor;
1486 		action[i].reformat.encap = hdrs[i].encap;
1487 		action[i].reformat.offset = hdrs[i].offset;
1488 
1489 		reformat_hdrs[i].sz = hdrs[i].hdr.sz;
1490 		reformat_hdrs[i].data = hdrs[i].hdr.data;
1491 	}
1492 
1493 	ret = hws_action_handle_insert_with_ptr(action, num_of_hdrs,
1494 						reformat_hdrs, log_bulk_size);
1495 	if (ret) {
1496 		mlx5hws_err(ctx, "Failed to create HWS reformat action\n");
1497 		goto free_reformat_hdrs;
1498 	}
1499 
1500 	kfree(reformat_hdrs);
1501 
1502 	return action;
1503 
1504 free_reformat_hdrs:
1505 	kfree(reformat_hdrs);
1506 free_action:
1507 	kfree(action);
1508 	return NULL;
1509 }
1510 
1511 struct mlx5hws_action *
mlx5hws_action_create_remove_header(struct mlx5hws_context * ctx,struct mlx5hws_action_remove_header_attr * attr,u32 flags)1512 mlx5hws_action_create_remove_header(struct mlx5hws_context *ctx,
1513 				    struct mlx5hws_action_remove_header_attr *attr,
1514 				    u32 flags)
1515 {
1516 	struct mlx5hws_action *action;
1517 
1518 	action = hws_action_create_generic(ctx, flags, MLX5HWS_ACTION_TYP_REMOVE_HEADER);
1519 	if (!action)
1520 		return NULL;
1521 
1522 	/* support only remove anchor with size */
1523 	if (attr->size % W_SIZE != 0) {
1524 		mlx5hws_err(ctx,
1525 			    "Invalid size, HW supports header remove in WORD granularity\n");
1526 		goto free_action;
1527 	}
1528 
1529 	if (attr->size > MLX5HWS_ACTION_REMOVE_HEADER_MAX_SIZE) {
1530 		mlx5hws_err(ctx, "Header removal size limited to %u bytes\n",
1531 			    MLX5HWS_ACTION_REMOVE_HEADER_MAX_SIZE);
1532 		goto free_action;
1533 	}
1534 
1535 	action->remove_header.anchor = attr->anchor;
1536 	action->remove_header.size = attr->size / W_SIZE;
1537 
1538 	if (hws_action_create_stcs(action, 0))
1539 		goto free_action;
1540 
1541 	return action;
1542 
1543 free_action:
1544 	kfree(action);
1545 	return NULL;
1546 }
1547 
1548 static struct mlx5hws_definer *
hws_action_create_dest_match_range_definer(struct mlx5hws_context * ctx)1549 hws_action_create_dest_match_range_definer(struct mlx5hws_context *ctx)
1550 {
1551 	struct mlx5hws_definer *definer;
1552 	__be32 *tag;
1553 	int ret;
1554 
1555 	definer = kzalloc(sizeof(*definer), GFP_KERNEL);
1556 	if (!definer)
1557 		return NULL;
1558 
1559 	definer->dw_selector[0] = MLX5_IFC_DEFINER_FORMAT_OFFSET_OUTER_ETH_PKT_LEN / 4;
1560 	/* Set DW0 tag mask */
1561 	tag = (__force __be32 *)definer->mask.jumbo;
1562 	tag[MLX5HWS_RULE_JUMBO_MATCH_TAG_OFFSET_DW0] = htonl(0xffffUL << 16);
1563 
1564 	mutex_lock(&ctx->ctrl_lock);
1565 
1566 	ret = mlx5hws_definer_get_obj(ctx, definer);
1567 	if (ret < 0) {
1568 		mutex_unlock(&ctx->ctrl_lock);
1569 		kfree(definer);
1570 		return NULL;
1571 	}
1572 
1573 	mutex_unlock(&ctx->ctrl_lock);
1574 	definer->obj_id = ret;
1575 
1576 	return definer;
1577 }
1578 
1579 static struct mlx5hws_matcher_action_ste *
hws_action_create_dest_match_range_table(struct mlx5hws_context * ctx,struct mlx5hws_definer * definer,u32 miss_ft_id)1580 hws_action_create_dest_match_range_table(struct mlx5hws_context *ctx,
1581 					 struct mlx5hws_definer *definer,
1582 					 u32 miss_ft_id)
1583 {
1584 	struct mlx5hws_cmd_rtc_create_attr rtc_attr = {0};
1585 	struct mlx5hws_action_default_stc *default_stc;
1586 	struct mlx5hws_matcher_action_ste *table_ste;
1587 	struct mlx5hws_pool_attr pool_attr = {0};
1588 	struct mlx5hws_pool *ste_pool, *stc_pool;
1589 	struct mlx5hws_pool_chunk *ste;
1590 	u32 *rtc_0_id, *rtc_1_id;
1591 	u32 obj_id;
1592 	int ret;
1593 
1594 	/* Check if STE range is supported */
1595 	if (!IS_BIT_SET(ctx->caps->supp_ste_format_gen_wqe, MLX5_IFC_RTC_STE_FORMAT_RANGE)) {
1596 		mlx5hws_err(ctx, "Range STE format not supported\n");
1597 		return NULL;
1598 	}
1599 
1600 	table_ste = kzalloc(sizeof(*table_ste), GFP_KERNEL);
1601 	if (!table_ste)
1602 		return NULL;
1603 
1604 	mutex_lock(&ctx->ctrl_lock);
1605 
1606 	pool_attr.table_type = MLX5HWS_TABLE_TYPE_FDB;
1607 	pool_attr.pool_type = MLX5HWS_POOL_TYPE_STE;
1608 	pool_attr.flags = MLX5HWS_POOL_FLAGS_FOR_STE_ACTION_POOL;
1609 	pool_attr.alloc_log_sz = 1;
1610 	table_ste->pool = mlx5hws_pool_create(ctx, &pool_attr);
1611 	if (!table_ste->pool) {
1612 		mlx5hws_err(ctx, "Failed to allocate memory ste pool\n");
1613 		goto free_ste;
1614 	}
1615 
1616 	/* Allocate RTC */
1617 	rtc_0_id = &table_ste->rtc_0_id;
1618 	rtc_1_id = &table_ste->rtc_1_id;
1619 	ste_pool = table_ste->pool;
1620 	ste = &table_ste->ste;
1621 	ste->order = 1;
1622 
1623 	rtc_attr.log_size = 0;
1624 	rtc_attr.log_depth = 0;
1625 	rtc_attr.miss_ft_id = miss_ft_id;
1626 	rtc_attr.num_hash_definer = 1;
1627 	rtc_attr.update_index_mode = MLX5_IFC_RTC_STE_UPDATE_MODE_BY_HASH;
1628 	rtc_attr.access_index_mode = MLX5_IFC_RTC_STE_ACCESS_MODE_BY_HASH;
1629 	rtc_attr.match_definer_0 = ctx->caps->trivial_match_definer;
1630 	rtc_attr.fw_gen_wqe = true;
1631 	rtc_attr.is_scnd_range = true;
1632 
1633 	obj_id = mlx5hws_pool_chunk_get_base_id(ste_pool, ste);
1634 
1635 	rtc_attr.pd = ctx->pd_num;
1636 	rtc_attr.ste_base = obj_id;
1637 	rtc_attr.ste_offset = ste->offset;
1638 	rtc_attr.reparse_mode = mlx5hws_context_get_reparse_mode(ctx);
1639 	rtc_attr.table_type = mlx5hws_table_get_res_fw_ft_type(MLX5HWS_TABLE_TYPE_FDB, false);
1640 
1641 	/* STC is a single resource (obj_id), use any STC for the ID */
1642 	stc_pool = ctx->stc_pool[MLX5HWS_TABLE_TYPE_FDB];
1643 	default_stc = ctx->common_res[MLX5HWS_TABLE_TYPE_FDB].default_stc;
1644 	obj_id = mlx5hws_pool_chunk_get_base_id(stc_pool, &default_stc->default_hit);
1645 	rtc_attr.stc_base = obj_id;
1646 
1647 	ret = mlx5hws_cmd_rtc_create(ctx->mdev, &rtc_attr, rtc_0_id);
1648 	if (ret) {
1649 		mlx5hws_err(ctx, "Failed to create RTC");
1650 		goto pool_destroy;
1651 	}
1652 
1653 	/* Create mirror RTC */
1654 	obj_id = mlx5hws_pool_chunk_get_base_mirror_id(ste_pool, ste);
1655 	rtc_attr.ste_base = obj_id;
1656 	rtc_attr.table_type = mlx5hws_table_get_res_fw_ft_type(MLX5HWS_TABLE_TYPE_FDB, true);
1657 
1658 	obj_id = mlx5hws_pool_chunk_get_base_mirror_id(stc_pool, &default_stc->default_hit);
1659 	rtc_attr.stc_base = obj_id;
1660 
1661 	ret = mlx5hws_cmd_rtc_create(ctx->mdev, &rtc_attr, rtc_1_id);
1662 	if (ret) {
1663 		mlx5hws_err(ctx, "Failed to create mirror RTC");
1664 		goto destroy_rtc_0;
1665 	}
1666 
1667 	mutex_unlock(&ctx->ctrl_lock);
1668 
1669 	return table_ste;
1670 
1671 destroy_rtc_0:
1672 	mlx5hws_cmd_rtc_destroy(ctx->mdev, *rtc_0_id);
1673 pool_destroy:
1674 	mlx5hws_pool_destroy(table_ste->pool);
1675 free_ste:
1676 	mutex_unlock(&ctx->ctrl_lock);
1677 	kfree(table_ste);
1678 	return NULL;
1679 }
1680 
1681 static void
hws_action_destroy_dest_match_range_table(struct mlx5hws_context * ctx,struct mlx5hws_matcher_action_ste * table_ste)1682 hws_action_destroy_dest_match_range_table(struct mlx5hws_context *ctx,
1683 					  struct mlx5hws_matcher_action_ste *table_ste)
1684 {
1685 	mutex_lock(&ctx->ctrl_lock);
1686 
1687 	mlx5hws_cmd_rtc_destroy(ctx->mdev, table_ste->rtc_1_id);
1688 	mlx5hws_cmd_rtc_destroy(ctx->mdev, table_ste->rtc_0_id);
1689 	mlx5hws_pool_destroy(table_ste->pool);
1690 	kfree(table_ste);
1691 
1692 	mutex_unlock(&ctx->ctrl_lock);
1693 }
1694 
1695 static int
hws_action_create_dest_match_range_fill_table(struct mlx5hws_context * ctx,struct mlx5hws_matcher_action_ste * table_ste,struct mlx5hws_action * hit_ft_action,struct mlx5hws_definer * range_definer,u32 min,u32 max)1696 hws_action_create_dest_match_range_fill_table(struct mlx5hws_context *ctx,
1697 					      struct mlx5hws_matcher_action_ste *table_ste,
1698 					      struct mlx5hws_action *hit_ft_action,
1699 					      struct mlx5hws_definer *range_definer,
1700 					      u32 min, u32 max)
1701 {
1702 	struct mlx5hws_wqe_gta_data_seg_ste match_wqe_data = {0};
1703 	struct mlx5hws_wqe_gta_data_seg_ste range_wqe_data = {0};
1704 	struct mlx5hws_wqe_gta_ctrl_seg wqe_ctrl = {0};
1705 	u32 no_use, used_rtc_0_id, used_rtc_1_id, ret;
1706 	struct mlx5hws_context_common_res *common_res;
1707 	struct mlx5hws_send_ste_attr ste_attr = {0};
1708 	struct mlx5hws_send_engine *queue;
1709 	__be32 *wqe_data_arr;
1710 
1711 	mutex_lock(&ctx->ctrl_lock);
1712 
1713 	/* Get the control queue */
1714 	queue = &ctx->send_queue[ctx->queues - 1];
1715 	if (unlikely(mlx5hws_send_engine_err(queue))) {
1716 		ret = -EIO;
1717 		goto error;
1718 	}
1719 
1720 	/* Init default send STE attributes */
1721 	ste_attr.gta_opcode = MLX5HWS_WQE_GTA_OP_ACTIVATE;
1722 	ste_attr.send_attr.opmod = MLX5HWS_WQE_GTA_OPMOD_STE;
1723 	ste_attr.send_attr.opcode = MLX5HWS_WQE_OPCODE_TBL_ACCESS;
1724 	ste_attr.send_attr.len = MLX5HWS_WQE_SZ_GTA_CTRL + MLX5HWS_WQE_SZ_GTA_DATA;
1725 	ste_attr.send_attr.user_data = &no_use;
1726 	ste_attr.send_attr.rule = NULL;
1727 	ste_attr.send_attr.fence = 1;
1728 	ste_attr.send_attr.notify_hw = true;
1729 	ste_attr.rtc_0 = table_ste->rtc_0_id;
1730 	ste_attr.rtc_1 = table_ste->rtc_1_id;
1731 	ste_attr.used_id_rtc_0 = &used_rtc_0_id;
1732 	ste_attr.used_id_rtc_1 = &used_rtc_1_id;
1733 
1734 	common_res = &ctx->common_res[MLX5HWS_TABLE_TYPE_FDB];
1735 
1736 	/* init an empty match STE which will always hit */
1737 	ste_attr.wqe_ctrl = &wqe_ctrl;
1738 	ste_attr.wqe_data = &match_wqe_data;
1739 	ste_attr.send_attr.match_definer_id = ctx->caps->trivial_match_definer;
1740 
1741 	/* Fill WQE control data */
1742 	wqe_ctrl.stc_ix[MLX5HWS_ACTION_STC_IDX_CTRL] =
1743 		htonl(common_res->default_stc->nop_ctr.offset);
1744 	wqe_ctrl.stc_ix[MLX5HWS_ACTION_STC_IDX_DW5] =
1745 		htonl(common_res->default_stc->nop_dw5.offset);
1746 	wqe_ctrl.stc_ix[MLX5HWS_ACTION_STC_IDX_DW6] =
1747 		htonl(common_res->default_stc->nop_dw6.offset);
1748 	wqe_ctrl.stc_ix[MLX5HWS_ACTION_STC_IDX_DW7] =
1749 		htonl(common_res->default_stc->nop_dw7.offset);
1750 	wqe_ctrl.stc_ix[MLX5HWS_ACTION_STC_IDX_CTRL] |=
1751 		htonl(MLX5HWS_ACTION_STC_IDX_LAST_COMBO2 << 29);
1752 	wqe_ctrl.stc_ix[MLX5HWS_ACTION_STC_IDX_HIT] =
1753 		htonl(hit_ft_action->stc[MLX5HWS_TABLE_TYPE_FDB].offset);
1754 
1755 	wqe_data_arr = (__force __be32 *)&range_wqe_data;
1756 
1757 	ste_attr.range_wqe_data = &range_wqe_data;
1758 	ste_attr.send_attr.len += MLX5HWS_WQE_SZ_GTA_DATA;
1759 	ste_attr.send_attr.range_definer_id = mlx5hws_definer_get_id(range_definer);
1760 
1761 	/* Fill range matching fields,
1762 	 * min/max_value_2 corresponds to match_dw_0 in its definer,
1763 	 * min_value_2 sets in DW0 in the STE and max_value_2 sets in DW1 in the STE.
1764 	 */
1765 	wqe_data_arr[MLX5HWS_MATCHER_OFFSET_TAG_DW0] = htonl(min << 16);
1766 	wqe_data_arr[MLX5HWS_MATCHER_OFFSET_TAG_DW1] = htonl(max << 16);
1767 
1768 	/* Send WQEs to FW */
1769 	mlx5hws_send_stes_fw(ctx, queue, &ste_attr);
1770 
1771 	/* Poll for completion */
1772 	ret = mlx5hws_send_queue_action(ctx, ctx->queues - 1,
1773 					MLX5HWS_SEND_QUEUE_ACTION_DRAIN_SYNC);
1774 	if (ret) {
1775 		mlx5hws_err(ctx, "Failed to drain control queue");
1776 		goto error;
1777 	}
1778 
1779 	mutex_unlock(&ctx->ctrl_lock);
1780 
1781 	return 0;
1782 
1783 error:
1784 	mutex_unlock(&ctx->ctrl_lock);
1785 	return ret;
1786 }
1787 
1788 struct mlx5hws_action *
mlx5hws_action_create_dest_match_range(struct mlx5hws_context * ctx,u32 field,struct mlx5_flow_table * hit_ft,struct mlx5_flow_table * miss_ft,u32 min,u32 max,u32 flags)1789 mlx5hws_action_create_dest_match_range(struct mlx5hws_context *ctx,
1790 				       u32 field,
1791 				       struct mlx5_flow_table *hit_ft,
1792 				       struct mlx5_flow_table *miss_ft,
1793 				       u32 min, u32 max, u32 flags)
1794 {
1795 	struct mlx5hws_cmd_stc_modify_attr stc_attr = {0};
1796 	struct mlx5hws_matcher_action_ste *table_ste;
1797 	struct mlx5hws_action *hit_ft_action;
1798 	struct mlx5hws_definer *definer;
1799 	struct mlx5hws_action *action;
1800 	u32 miss_ft_id = miss_ft->id;
1801 	u32 hit_ft_id = hit_ft->id;
1802 	int ret;
1803 
1804 	if (field != MLX5_FLOW_DEST_RANGE_FIELD_PKT_LEN ||
1805 	    min > 0xffff || max > 0xffff) {
1806 		mlx5hws_err(ctx, "Invalid match range parameters\n");
1807 		return NULL;
1808 	}
1809 
1810 	action = hws_action_create_generic(ctx, flags, MLX5HWS_ACTION_TYP_RANGE);
1811 	if (!action)
1812 		return NULL;
1813 
1814 	definer = hws_action_create_dest_match_range_definer(ctx);
1815 	if (!definer)
1816 		goto free_action;
1817 
1818 	table_ste = hws_action_create_dest_match_range_table(ctx, definer, miss_ft_id);
1819 	if (!table_ste)
1820 		goto destroy_definer;
1821 
1822 	hit_ft_action = mlx5hws_action_create_dest_table_num(ctx, hit_ft_id, flags);
1823 	if (!hit_ft_action)
1824 		goto destroy_table_ste;
1825 
1826 	ret = hws_action_create_dest_match_range_fill_table(ctx, table_ste,
1827 							    hit_ft_action,
1828 							    definer, min, max);
1829 	if (ret)
1830 		goto destroy_hit_ft_action;
1831 
1832 	action->range.table_ste = table_ste;
1833 	action->range.definer = definer;
1834 	action->range.hit_ft_action = hit_ft_action;
1835 
1836 	/* Allocate STC for jumps to STE */
1837 	mutex_lock(&ctx->ctrl_lock);
1838 	stc_attr.action_offset = MLX5HWS_ACTION_OFFSET_HIT;
1839 	stc_attr.action_type = MLX5_IFC_STC_ACTION_TYPE_JUMP_TO_STE_TABLE;
1840 	stc_attr.reparse_mode = MLX5_IFC_STC_REPARSE_IGNORE;
1841 	stc_attr.ste_table.ste = table_ste->ste;
1842 	stc_attr.ste_table.ste_pool = table_ste->pool;
1843 	stc_attr.ste_table.match_definer_id = ctx->caps->trivial_match_definer;
1844 
1845 	ret = mlx5hws_action_alloc_single_stc(ctx, &stc_attr, MLX5HWS_TABLE_TYPE_FDB,
1846 					      &action->stc[MLX5HWS_TABLE_TYPE_FDB]);
1847 	if (ret)
1848 		goto error_unlock;
1849 
1850 	mutex_unlock(&ctx->ctrl_lock);
1851 
1852 	return action;
1853 
1854 error_unlock:
1855 	mutex_unlock(&ctx->ctrl_lock);
1856 destroy_hit_ft_action:
1857 	mlx5hws_action_destroy(hit_ft_action);
1858 destroy_table_ste:
1859 	hws_action_destroy_dest_match_range_table(ctx, table_ste);
1860 destroy_definer:
1861 	mlx5hws_definer_free(ctx, definer);
1862 free_action:
1863 	kfree(action);
1864 	mlx5hws_err(ctx, "Failed to create action dest match range");
1865 	return NULL;
1866 }
1867 
1868 struct mlx5hws_action *
mlx5hws_action_create_last(struct mlx5hws_context * ctx,u32 flags)1869 mlx5hws_action_create_last(struct mlx5hws_context *ctx, u32 flags)
1870 {
1871 	return hws_action_create_generic(ctx, flags, MLX5HWS_ACTION_TYP_LAST);
1872 }
1873 
1874 struct mlx5hws_action *
mlx5hws_action_create_flow_sampler(struct mlx5hws_context * ctx,u32 sampler_id,u32 flags)1875 mlx5hws_action_create_flow_sampler(struct mlx5hws_context *ctx,
1876 				   u32 sampler_id, u32 flags)
1877 {
1878 	mlx5hws_err(ctx, "Flow sampler action - unsupported\n");
1879 	return NULL;
1880 }
1881 
hws_action_destroy_hws(struct mlx5hws_action * action)1882 static void hws_action_destroy_hws(struct mlx5hws_action *action)
1883 {
1884 	u32 ext_reformat_id;
1885 	bool shared_arg;
1886 	u32 obj_id;
1887 	u32 i;
1888 
1889 	switch (action->type) {
1890 	case MLX5HWS_ACTION_TYP_MISS:
1891 	case MLX5HWS_ACTION_TYP_TAG:
1892 	case MLX5HWS_ACTION_TYP_DROP:
1893 	case MLX5HWS_ACTION_TYP_CTR:
1894 	case MLX5HWS_ACTION_TYP_TBL:
1895 	case MLX5HWS_ACTION_TYP_REFORMAT_TNL_L2_TO_L2:
1896 	case MLX5HWS_ACTION_TYP_ASO_METER:
1897 	case MLX5HWS_ACTION_TYP_PUSH_VLAN:
1898 	case MLX5HWS_ACTION_TYP_REMOVE_HEADER:
1899 	case MLX5HWS_ACTION_TYP_VPORT:
1900 		hws_action_destroy_stcs(action);
1901 		break;
1902 	case MLX5HWS_ACTION_TYP_POP_VLAN:
1903 		hws_action_destroy_stcs(action);
1904 		hws_action_put_shared_stc(action, MLX5HWS_CONTEXT_SHARED_STC_DOUBLE_POP);
1905 		break;
1906 	case MLX5HWS_ACTION_TYP_DEST_ARRAY:
1907 		hws_action_destroy_stcs(action);
1908 		mlx5hws_cmd_forward_tbl_destroy(action->ctx->mdev, action->dest_array.fw_island);
1909 		for (i = 0; i < action->dest_array.num_dest; i++) {
1910 			ext_reformat_id = action->dest_array.dest_list[i].ext_reformat_id;
1911 			if (ext_reformat_id)
1912 				mlx5hws_cmd_packet_reformat_destroy(action->ctx->mdev,
1913 								    ext_reformat_id);
1914 		}
1915 		kfree(action->dest_array.dest_list);
1916 		break;
1917 	case MLX5HWS_ACTION_TYP_REFORMAT_TNL_L3_TO_L2:
1918 	case MLX5HWS_ACTION_TYP_MODIFY_HDR:
1919 		shared_arg = false;
1920 		for (i = 0; i < action->modify_header.num_of_patterns; i++) {
1921 			hws_action_destroy_stcs(&action[i]);
1922 			if (action[i].modify_header.num_of_actions > 1) {
1923 				mlx5hws_pat_put_pattern(action[i].ctx,
1924 							action[i].modify_header.pat_id);
1925 				/* Save shared arg object to be freed after */
1926 				obj_id = action[i].modify_header.arg_id;
1927 				shared_arg = true;
1928 			}
1929 		}
1930 		if (shared_arg)
1931 			mlx5hws_arg_destroy(action->ctx, obj_id);
1932 		break;
1933 	case MLX5HWS_ACTION_TYP_REFORMAT_L2_TO_TNL_L3:
1934 		hws_action_put_shared_stc(action, MLX5HWS_CONTEXT_SHARED_STC_DECAP_L3);
1935 		for (i = 0; i < action->reformat.num_of_hdrs; i++)
1936 			hws_action_destroy_stcs(&action[i]);
1937 		mlx5hws_arg_destroy(action->ctx, action->reformat.arg_id);
1938 		break;
1939 	case MLX5HWS_ACTION_TYP_INSERT_HEADER:
1940 	case MLX5HWS_ACTION_TYP_REFORMAT_L2_TO_TNL_L2:
1941 		for (i = 0; i < action->reformat.num_of_hdrs; i++)
1942 			hws_action_destroy_stcs(&action[i]);
1943 		mlx5hws_arg_destroy(action->ctx, action->reformat.arg_id);
1944 		break;
1945 	case MLX5HWS_ACTION_TYP_RANGE:
1946 		hws_action_destroy_stcs(action);
1947 		hws_action_destroy_dest_match_range_table(action->ctx, action->range.table_ste);
1948 		mlx5hws_definer_free(action->ctx, action->range.definer);
1949 		mlx5hws_action_destroy(action->range.hit_ft_action);
1950 		break;
1951 	case MLX5HWS_ACTION_TYP_LAST:
1952 		break;
1953 	default:
1954 		pr_warn("HWS: Invalid action type: %d\n", action->type);
1955 	}
1956 }
1957 
mlx5hws_action_destroy(struct mlx5hws_action * action)1958 int mlx5hws_action_destroy(struct mlx5hws_action *action)
1959 {
1960 	hws_action_destroy_hws(action);
1961 
1962 	kfree(action);
1963 	return 0;
1964 }
1965 
mlx5hws_action_get_default_stc(struct mlx5hws_context * ctx,u8 tbl_type)1966 int mlx5hws_action_get_default_stc(struct mlx5hws_context *ctx, u8 tbl_type)
1967 __must_hold(&ctx->ctrl_lock)
1968 {
1969 	struct mlx5hws_cmd_stc_modify_attr stc_attr = {0};
1970 	struct mlx5hws_action_default_stc *default_stc;
1971 	int ret;
1972 
1973 	if (ctx->common_res[tbl_type].default_stc) {
1974 		ctx->common_res[tbl_type].default_stc->refcount++;
1975 		return 0;
1976 	}
1977 
1978 	default_stc = kzalloc(sizeof(*default_stc), GFP_KERNEL);
1979 	if (!default_stc)
1980 		return -ENOMEM;
1981 
1982 	stc_attr.action_type = MLX5_IFC_STC_ACTION_TYPE_NOP;
1983 	stc_attr.action_offset = MLX5HWS_ACTION_OFFSET_DW0;
1984 	stc_attr.reparse_mode = MLX5_IFC_STC_REPARSE_IGNORE;
1985 	ret = mlx5hws_action_alloc_single_stc(ctx, &stc_attr, tbl_type,
1986 					      &default_stc->nop_ctr);
1987 	if (ret) {
1988 		mlx5hws_err(ctx, "Failed to allocate default counter STC\n");
1989 		goto free_default_stc;
1990 	}
1991 
1992 	stc_attr.action_offset = MLX5HWS_ACTION_OFFSET_DW5;
1993 	ret = mlx5hws_action_alloc_single_stc(ctx, &stc_attr, tbl_type,
1994 					      &default_stc->nop_dw5);
1995 	if (ret) {
1996 		mlx5hws_err(ctx, "Failed to allocate default NOP DW5 STC\n");
1997 		goto free_nop_ctr;
1998 	}
1999 
2000 	stc_attr.action_offset = MLX5HWS_ACTION_OFFSET_DW6;
2001 	ret = mlx5hws_action_alloc_single_stc(ctx, &stc_attr, tbl_type,
2002 					      &default_stc->nop_dw6);
2003 	if (ret) {
2004 		mlx5hws_err(ctx, "Failed to allocate default NOP DW6 STC\n");
2005 		goto free_nop_dw5;
2006 	}
2007 
2008 	stc_attr.action_offset = MLX5HWS_ACTION_OFFSET_DW7;
2009 	ret = mlx5hws_action_alloc_single_stc(ctx, &stc_attr, tbl_type,
2010 					      &default_stc->nop_dw7);
2011 	if (ret) {
2012 		mlx5hws_err(ctx, "Failed to allocate default NOP DW7 STC\n");
2013 		goto free_nop_dw6;
2014 	}
2015 
2016 	stc_attr.action_offset = MLX5HWS_ACTION_OFFSET_HIT;
2017 	stc_attr.action_type = MLX5_IFC_STC_ACTION_TYPE_ALLOW;
2018 
2019 	ret = mlx5hws_action_alloc_single_stc(ctx, &stc_attr, tbl_type,
2020 					      &default_stc->default_hit);
2021 	if (ret) {
2022 		mlx5hws_err(ctx, "Failed to allocate default allow STC\n");
2023 		goto free_nop_dw7;
2024 	}
2025 
2026 	ctx->common_res[tbl_type].default_stc = default_stc;
2027 	ctx->common_res[tbl_type].default_stc->refcount++;
2028 
2029 	return 0;
2030 
2031 free_nop_dw7:
2032 	mlx5hws_action_free_single_stc(ctx, tbl_type, &default_stc->nop_dw7);
2033 free_nop_dw6:
2034 	mlx5hws_action_free_single_stc(ctx, tbl_type, &default_stc->nop_dw6);
2035 free_nop_dw5:
2036 	mlx5hws_action_free_single_stc(ctx, tbl_type, &default_stc->nop_dw5);
2037 free_nop_ctr:
2038 	mlx5hws_action_free_single_stc(ctx, tbl_type, &default_stc->nop_ctr);
2039 free_default_stc:
2040 	kfree(default_stc);
2041 	return ret;
2042 }
2043 
mlx5hws_action_put_default_stc(struct mlx5hws_context * ctx,u8 tbl_type)2044 void mlx5hws_action_put_default_stc(struct mlx5hws_context *ctx, u8 tbl_type)
2045 __must_hold(&ctx->ctrl_lock)
2046 {
2047 	struct mlx5hws_action_default_stc *default_stc;
2048 
2049 	default_stc = ctx->common_res[tbl_type].default_stc;
2050 
2051 	default_stc = ctx->common_res[tbl_type].default_stc;
2052 	if (--default_stc->refcount)
2053 		return;
2054 
2055 	mlx5hws_action_free_single_stc(ctx, tbl_type, &default_stc->default_hit);
2056 	mlx5hws_action_free_single_stc(ctx, tbl_type, &default_stc->nop_dw7);
2057 	mlx5hws_action_free_single_stc(ctx, tbl_type, &default_stc->nop_dw6);
2058 	mlx5hws_action_free_single_stc(ctx, tbl_type, &default_stc->nop_dw5);
2059 	mlx5hws_action_free_single_stc(ctx, tbl_type, &default_stc->nop_ctr);
2060 	kfree(default_stc);
2061 	ctx->common_res[tbl_type].default_stc = NULL;
2062 }
2063 
hws_action_modify_write(struct mlx5hws_send_engine * queue,u32 arg_idx,u8 * arg_data,u16 num_of_actions,u32 nope_locations)2064 static void hws_action_modify_write(struct mlx5hws_send_engine *queue,
2065 				    u32 arg_idx,
2066 				    u8 *arg_data,
2067 				    u16 num_of_actions,
2068 				    u32 nope_locations)
2069 {
2070 	u8 *new_arg_data = NULL;
2071 	int i, j;
2072 
2073 	if (unlikely(nope_locations)) {
2074 		new_arg_data = kcalloc(num_of_actions,
2075 				       MLX5HWS_MODIFY_ACTION_SIZE, GFP_KERNEL);
2076 		if (unlikely(!new_arg_data))
2077 			return;
2078 
2079 		for (i = 0, j = 0; i < num_of_actions; i++, j++) {
2080 			memcpy(&new_arg_data[j], arg_data, MLX5HWS_MODIFY_ACTION_SIZE);
2081 			if (BIT(i) & nope_locations)
2082 				j++;
2083 		}
2084 	}
2085 
2086 	mlx5hws_arg_write(queue, NULL, arg_idx,
2087 			  new_arg_data ? new_arg_data : arg_data,
2088 			  num_of_actions * MLX5HWS_MODIFY_ACTION_SIZE);
2089 
2090 	kfree(new_arg_data);
2091 }
2092 
mlx5hws_action_prepare_decap_l3_data(u8 * src,u8 * dst,u16 num_of_actions)2093 void mlx5hws_action_prepare_decap_l3_data(u8 *src, u8 *dst, u16 num_of_actions)
2094 {
2095 	u8 *e_src;
2096 	int i;
2097 
2098 	/* num_of_actions = remove l3l2 + 4/5 inserts + remove extra 2 bytes
2099 	 * copy from end of src to the start of dst.
2100 	 * move to the end, 2 is the leftover from 14B or 18B
2101 	 */
2102 	if (num_of_actions == DECAP_L3_NUM_ACTIONS_W_NO_VLAN)
2103 		e_src = src + MLX5HWS_ACTION_HDR_LEN_L2;
2104 	else
2105 		e_src = src + MLX5HWS_ACTION_HDR_LEN_L2_W_VLAN;
2106 
2107 	/* Move dst over the first remove action + zero data */
2108 	dst += MLX5HWS_ACTION_DOUBLE_SIZE;
2109 	/* Move dst over the first insert ctrl action */
2110 	dst += MLX5HWS_ACTION_DOUBLE_SIZE / 2;
2111 	/* Actions:
2112 	 * no vlan: r_h-insert_4b-insert_4b-insert_4b-insert_4b-remove_2b.
2113 	 * with vlan: r_h-insert_4b-insert_4b-insert_4b-insert_4b-insert_4b-remove_2b.
2114 	 * the loop is without the last insertion.
2115 	 */
2116 	for (i = 0; i < num_of_actions - 3; i++) {
2117 		e_src -= MLX5HWS_ACTION_INLINE_DATA_SIZE;
2118 		memcpy(dst, e_src, MLX5HWS_ACTION_INLINE_DATA_SIZE); /* data */
2119 		dst += MLX5HWS_ACTION_DOUBLE_SIZE;
2120 	}
2121 	/* Copy the last 2 bytes after a gap of 2 bytes which will be removed */
2122 	e_src -= MLX5HWS_ACTION_INLINE_DATA_SIZE / 2;
2123 	dst += MLX5HWS_ACTION_INLINE_DATA_SIZE / 2;
2124 	memcpy(dst, e_src, 2);
2125 }
2126 
2127 static int
hws_action_get_shared_stc_offset(struct mlx5hws_context_common_res * common_res,enum mlx5hws_context_shared_stc_type stc_type)2128 hws_action_get_shared_stc_offset(struct mlx5hws_context_common_res *common_res,
2129 				 enum mlx5hws_context_shared_stc_type stc_type)
2130 {
2131 	return common_res->shared_stc[stc_type]->stc_chunk.offset;
2132 }
2133 
2134 static struct mlx5hws_actions_wqe_setter *
hws_action_setter_find_first(struct mlx5hws_actions_wqe_setter * setter,u8 req_flags)2135 hws_action_setter_find_first(struct mlx5hws_actions_wqe_setter *setter,
2136 			     u8 req_flags)
2137 {
2138 	/* Use a new setter if requested flags are taken */
2139 	while (setter->flags & req_flags)
2140 		setter++;
2141 
2142 	/* Use current setter in required flags are not used */
2143 	return setter;
2144 }
2145 
2146 static void
hws_action_apply_stc(struct mlx5hws_actions_apply_data * apply,enum mlx5hws_action_stc_idx stc_idx,u8 action_idx)2147 hws_action_apply_stc(struct mlx5hws_actions_apply_data *apply,
2148 		     enum mlx5hws_action_stc_idx stc_idx,
2149 		     u8 action_idx)
2150 {
2151 	struct mlx5hws_action *action = apply->rule_action[action_idx].action;
2152 
2153 	apply->wqe_ctrl->stc_ix[stc_idx] =
2154 		htonl(action->stc[apply->tbl_type].offset);
2155 }
2156 
2157 static void
hws_action_setter_push_vlan(struct mlx5hws_actions_apply_data * apply,struct mlx5hws_actions_wqe_setter * setter)2158 hws_action_setter_push_vlan(struct mlx5hws_actions_apply_data *apply,
2159 			    struct mlx5hws_actions_wqe_setter *setter)
2160 {
2161 	struct mlx5hws_rule_action *rule_action;
2162 
2163 	rule_action = &apply->rule_action[setter->idx_double];
2164 	apply->wqe_data[MLX5HWS_ACTION_OFFSET_DW6] = 0;
2165 	apply->wqe_data[MLX5HWS_ACTION_OFFSET_DW7] = rule_action->push_vlan.vlan_hdr;
2166 
2167 	hws_action_apply_stc(apply, MLX5HWS_ACTION_STC_IDX_DW6, setter->idx_double);
2168 	apply->wqe_ctrl->stc_ix[MLX5HWS_ACTION_STC_IDX_DW7] = 0;
2169 }
2170 
2171 static void
hws_action_setter_modify_header(struct mlx5hws_actions_apply_data * apply,struct mlx5hws_actions_wqe_setter * setter)2172 hws_action_setter_modify_header(struct mlx5hws_actions_apply_data *apply,
2173 				struct mlx5hws_actions_wqe_setter *setter)
2174 {
2175 	struct mlx5hws_rule_action *rule_action;
2176 	struct mlx5hws_action *action;
2177 	u32 arg_sz, arg_idx;
2178 	u8 *single_action;
2179 	__be32 stc_idx;
2180 
2181 	rule_action = &apply->rule_action[setter->idx_double];
2182 	action = rule_action->action;
2183 
2184 	stc_idx = htonl(action->stc[apply->tbl_type].offset);
2185 	apply->wqe_ctrl->stc_ix[MLX5HWS_ACTION_STC_IDX_DW6] = stc_idx;
2186 	apply->wqe_ctrl->stc_ix[MLX5HWS_ACTION_STC_IDX_DW7] = 0;
2187 
2188 	apply->wqe_data[MLX5HWS_ACTION_OFFSET_DW6] = 0;
2189 
2190 	if (action->modify_header.num_of_actions == 1) {
2191 		if (action->modify_header.single_action_type ==
2192 		    MLX5_MODIFICATION_TYPE_COPY ||
2193 		    action->modify_header.single_action_type ==
2194 		    MLX5_MODIFICATION_TYPE_ADD_FIELD) {
2195 			apply->wqe_data[MLX5HWS_ACTION_OFFSET_DW7] = 0;
2196 			return;
2197 		}
2198 
2199 		if (action->flags & MLX5HWS_ACTION_FLAG_SHARED)
2200 			single_action = (u8 *)&action->modify_header.single_action;
2201 		else
2202 			single_action = rule_action->modify_header.data;
2203 
2204 		apply->wqe_data[MLX5HWS_ACTION_OFFSET_DW7] =
2205 			*(__be32 *)MLX5_ADDR_OF(set_action_in, single_action, data);
2206 	} else {
2207 		/* Argument offset multiple with number of args per these actions */
2208 		arg_sz = mlx5hws_arg_get_arg_size(action->modify_header.max_num_of_actions);
2209 		arg_idx = rule_action->modify_header.offset * arg_sz;
2210 
2211 		apply->wqe_data[MLX5HWS_ACTION_OFFSET_DW7] = htonl(arg_idx);
2212 
2213 		if (!(action->flags & MLX5HWS_ACTION_FLAG_SHARED)) {
2214 			apply->require_dep = 1;
2215 			hws_action_modify_write(apply->queue,
2216 						action->modify_header.arg_id + arg_idx,
2217 						rule_action->modify_header.data,
2218 						action->modify_header.num_of_actions,
2219 						action->modify_header.nope_locations);
2220 		}
2221 	}
2222 }
2223 
2224 static void
hws_action_setter_insert_ptr(struct mlx5hws_actions_apply_data * apply,struct mlx5hws_actions_wqe_setter * setter)2225 hws_action_setter_insert_ptr(struct mlx5hws_actions_apply_data *apply,
2226 			     struct mlx5hws_actions_wqe_setter *setter)
2227 {
2228 	struct mlx5hws_rule_action *rule_action;
2229 	struct mlx5hws_action *action;
2230 	u32 arg_idx, arg_sz;
2231 	__be32 stc_idx;
2232 
2233 	rule_action = &apply->rule_action[setter->idx_double];
2234 	action = rule_action->action + rule_action->reformat.hdr_idx;
2235 
2236 	/* Argument offset multiple on args required for header size */
2237 	arg_sz = mlx5hws_arg_data_size_to_arg_size(action->reformat.max_hdr_sz);
2238 	arg_idx = rule_action->reformat.offset * arg_sz;
2239 
2240 	apply->wqe_data[MLX5HWS_ACTION_OFFSET_DW6] = 0;
2241 	apply->wqe_data[MLX5HWS_ACTION_OFFSET_DW7] = htonl(arg_idx);
2242 
2243 	stc_idx = htonl(action->stc[apply->tbl_type].offset);
2244 	apply->wqe_ctrl->stc_ix[MLX5HWS_ACTION_STC_IDX_DW6] = stc_idx;
2245 	apply->wqe_ctrl->stc_ix[MLX5HWS_ACTION_STC_IDX_DW7] = 0;
2246 
2247 	if (!(action->flags & MLX5HWS_ACTION_FLAG_SHARED)) {
2248 		apply->require_dep = 1;
2249 		mlx5hws_arg_write(apply->queue, NULL,
2250 				  action->reformat.arg_id + arg_idx,
2251 				  rule_action->reformat.data,
2252 				  action->reformat.header_size);
2253 	}
2254 }
2255 
2256 static void
hws_action_setter_tnl_l3_to_l2(struct mlx5hws_actions_apply_data * apply,struct mlx5hws_actions_wqe_setter * setter)2257 hws_action_setter_tnl_l3_to_l2(struct mlx5hws_actions_apply_data *apply,
2258 			       struct mlx5hws_actions_wqe_setter *setter)
2259 {
2260 	struct mlx5hws_rule_action *rule_action;
2261 	struct mlx5hws_action *action;
2262 	u32 arg_sz, arg_idx;
2263 	__be32 stc_idx;
2264 
2265 	rule_action = &apply->rule_action[setter->idx_double];
2266 	action = rule_action->action + rule_action->reformat.hdr_idx;
2267 
2268 	/* Argument offset multiple on args required for num of actions */
2269 	arg_sz = mlx5hws_arg_get_arg_size(action->modify_header.max_num_of_actions);
2270 	arg_idx = rule_action->reformat.offset * arg_sz;
2271 
2272 	apply->wqe_data[MLX5HWS_ACTION_OFFSET_DW6] = 0;
2273 	apply->wqe_data[MLX5HWS_ACTION_OFFSET_DW7] = htonl(arg_idx);
2274 
2275 	stc_idx = htonl(action->stc[apply->tbl_type].offset);
2276 	apply->wqe_ctrl->stc_ix[MLX5HWS_ACTION_STC_IDX_DW6] = stc_idx;
2277 	apply->wqe_ctrl->stc_ix[MLX5HWS_ACTION_STC_IDX_DW7] = 0;
2278 
2279 	if (!(action->flags & MLX5HWS_ACTION_FLAG_SHARED)) {
2280 		apply->require_dep = 1;
2281 		mlx5hws_arg_decapl3_write(apply->queue,
2282 					  action->modify_header.arg_id + arg_idx,
2283 					  rule_action->reformat.data,
2284 					  action->modify_header.num_of_actions);
2285 	}
2286 }
2287 
2288 static void
hws_action_setter_aso(struct mlx5hws_actions_apply_data * apply,struct mlx5hws_actions_wqe_setter * setter)2289 hws_action_setter_aso(struct mlx5hws_actions_apply_data *apply,
2290 		      struct mlx5hws_actions_wqe_setter *setter)
2291 {
2292 	struct mlx5hws_rule_action *rule_action;
2293 	u32 exe_aso_ctrl;
2294 	u32 offset;
2295 
2296 	rule_action = &apply->rule_action[setter->idx_double];
2297 
2298 	switch (rule_action->action->type) {
2299 	case MLX5HWS_ACTION_TYP_ASO_METER:
2300 		/* exe_aso_ctrl format:
2301 		 * [STC only and reserved bits 29b][init_color 2b][meter_id 1b]
2302 		 */
2303 		offset = rule_action->aso_meter.offset / MLX5_ASO_METER_NUM_PER_OBJ;
2304 		exe_aso_ctrl = rule_action->aso_meter.offset % MLX5_ASO_METER_NUM_PER_OBJ;
2305 		exe_aso_ctrl |= rule_action->aso_meter.init_color <<
2306 				MLX5HWS_ACTION_METER_INIT_COLOR_OFFSET;
2307 		break;
2308 	default:
2309 		mlx5hws_err(rule_action->action->ctx,
2310 			    "Unsupported ASO action type: %d\n", rule_action->action->type);
2311 		return;
2312 	}
2313 
2314 	/* aso_object_offset format: [24B] */
2315 	apply->wqe_data[MLX5HWS_ACTION_OFFSET_DW6] = htonl(offset);
2316 	apply->wqe_data[MLX5HWS_ACTION_OFFSET_DW7] = htonl(exe_aso_ctrl);
2317 
2318 	hws_action_apply_stc(apply, MLX5HWS_ACTION_STC_IDX_DW6, setter->idx_double);
2319 	apply->wqe_ctrl->stc_ix[MLX5HWS_ACTION_STC_IDX_DW7] = 0;
2320 }
2321 
2322 static void
hws_action_setter_tag(struct mlx5hws_actions_apply_data * apply,struct mlx5hws_actions_wqe_setter * setter)2323 hws_action_setter_tag(struct mlx5hws_actions_apply_data *apply,
2324 		      struct mlx5hws_actions_wqe_setter *setter)
2325 {
2326 	struct mlx5hws_rule_action *rule_action;
2327 
2328 	rule_action = &apply->rule_action[setter->idx_single];
2329 	apply->wqe_data[MLX5HWS_ACTION_OFFSET_DW5] = htonl(rule_action->tag.value);
2330 	hws_action_apply_stc(apply, MLX5HWS_ACTION_STC_IDX_DW5, setter->idx_single);
2331 }
2332 
2333 static void
hws_action_setter_ctrl_ctr(struct mlx5hws_actions_apply_data * apply,struct mlx5hws_actions_wqe_setter * setter)2334 hws_action_setter_ctrl_ctr(struct mlx5hws_actions_apply_data *apply,
2335 			   struct mlx5hws_actions_wqe_setter *setter)
2336 {
2337 	struct mlx5hws_rule_action *rule_action;
2338 
2339 	rule_action = &apply->rule_action[setter->idx_ctr];
2340 	apply->wqe_data[MLX5HWS_ACTION_OFFSET_DW0] = htonl(rule_action->counter.offset);
2341 	hws_action_apply_stc(apply, MLX5HWS_ACTION_STC_IDX_CTRL, setter->idx_ctr);
2342 }
2343 
2344 static void
hws_action_setter_single(struct mlx5hws_actions_apply_data * apply,struct mlx5hws_actions_wqe_setter * setter)2345 hws_action_setter_single(struct mlx5hws_actions_apply_data *apply,
2346 			 struct mlx5hws_actions_wqe_setter *setter)
2347 {
2348 	apply->wqe_data[MLX5HWS_ACTION_OFFSET_DW5] = 0;
2349 	hws_action_apply_stc(apply, MLX5HWS_ACTION_STC_IDX_DW5, setter->idx_single);
2350 }
2351 
2352 static void
hws_action_setter_single_double_pop(struct mlx5hws_actions_apply_data * apply,struct mlx5hws_actions_wqe_setter * setter)2353 hws_action_setter_single_double_pop(struct mlx5hws_actions_apply_data *apply,
2354 				    struct mlx5hws_actions_wqe_setter *setter)
2355 {
2356 	apply->wqe_data[MLX5HWS_ACTION_OFFSET_DW5] = 0;
2357 	apply->wqe_ctrl->stc_ix[MLX5HWS_ACTION_STC_IDX_DW5] =
2358 		htonl(hws_action_get_shared_stc_offset(apply->common_res,
2359 						       MLX5HWS_CONTEXT_SHARED_STC_DOUBLE_POP));
2360 }
2361 
2362 static void
hws_action_setter_hit(struct mlx5hws_actions_apply_data * apply,struct mlx5hws_actions_wqe_setter * setter)2363 hws_action_setter_hit(struct mlx5hws_actions_apply_data *apply,
2364 		      struct mlx5hws_actions_wqe_setter *setter)
2365 {
2366 	apply->wqe_data[MLX5HWS_ACTION_OFFSET_HIT_LSB] = 0;
2367 	hws_action_apply_stc(apply, MLX5HWS_ACTION_STC_IDX_HIT, setter->idx_hit);
2368 }
2369 
2370 static void
hws_action_setter_default_hit(struct mlx5hws_actions_apply_data * apply,struct mlx5hws_actions_wqe_setter * setter)2371 hws_action_setter_default_hit(struct mlx5hws_actions_apply_data *apply,
2372 			      struct mlx5hws_actions_wqe_setter *setter)
2373 {
2374 	apply->wqe_data[MLX5HWS_ACTION_OFFSET_HIT_LSB] = 0;
2375 	apply->wqe_ctrl->stc_ix[MLX5HWS_ACTION_STC_IDX_HIT] =
2376 		htonl(apply->common_res->default_stc->default_hit.offset);
2377 }
2378 
2379 static void
hws_action_setter_hit_next_action(struct mlx5hws_actions_apply_data * apply,struct mlx5hws_actions_wqe_setter * setter)2380 hws_action_setter_hit_next_action(struct mlx5hws_actions_apply_data *apply,
2381 				  struct mlx5hws_actions_wqe_setter *setter)
2382 {
2383 	apply->wqe_data[MLX5HWS_ACTION_OFFSET_HIT_LSB] = htonl(apply->next_direct_idx << 6);
2384 	apply->wqe_ctrl->stc_ix[MLX5HWS_ACTION_STC_IDX_HIT] = htonl(apply->jump_to_action_stc);
2385 }
2386 
2387 static void
hws_action_setter_common_decap(struct mlx5hws_actions_apply_data * apply,struct mlx5hws_actions_wqe_setter * setter)2388 hws_action_setter_common_decap(struct mlx5hws_actions_apply_data *apply,
2389 			       struct mlx5hws_actions_wqe_setter *setter)
2390 {
2391 	apply->wqe_data[MLX5HWS_ACTION_OFFSET_DW5] = 0;
2392 	apply->wqe_ctrl->stc_ix[MLX5HWS_ACTION_STC_IDX_DW5] =
2393 		htonl(hws_action_get_shared_stc_offset(apply->common_res,
2394 						       MLX5HWS_CONTEXT_SHARED_STC_DECAP_L3));
2395 }
2396 
2397 static void
hws_action_setter_range(struct mlx5hws_actions_apply_data * apply,struct mlx5hws_actions_wqe_setter * setter)2398 hws_action_setter_range(struct mlx5hws_actions_apply_data *apply,
2399 			struct mlx5hws_actions_wqe_setter *setter)
2400 {
2401 	/* Always jump to index zero */
2402 	apply->wqe_data[MLX5HWS_ACTION_OFFSET_HIT_LSB] = 0;
2403 	hws_action_apply_stc(apply, MLX5HWS_ACTION_STC_IDX_HIT, setter->idx_hit);
2404 }
2405 
mlx5hws_action_template_process(struct mlx5hws_action_template * at)2406 int mlx5hws_action_template_process(struct mlx5hws_action_template *at)
2407 {
2408 	struct mlx5hws_actions_wqe_setter *start_setter = at->setters + 1;
2409 	enum mlx5hws_action_type *action_type = at->action_type_arr;
2410 	struct mlx5hws_actions_wqe_setter *setter = at->setters;
2411 	struct mlx5hws_actions_wqe_setter *pop_setter = NULL;
2412 	struct mlx5hws_actions_wqe_setter *last_setter;
2413 	int i;
2414 
2415 	/* Note: Given action combination must be valid */
2416 
2417 	/* Check if action were already processed */
2418 	if (at->num_of_action_stes)
2419 		return 0;
2420 
2421 	for (i = 0; i < MLX5HWS_ACTION_MAX_STE; i++)
2422 		setter[i].set_hit = &hws_action_setter_hit_next_action;
2423 
2424 	/* The same action template setters can be used with jumbo or match
2425 	 * STE, to support both cases we reserve the first setter for cases
2426 	 * with jumbo STE to allow jump to the first action STE.
2427 	 * This extra setter can be reduced in some cases on rule creation.
2428 	 */
2429 	setter = start_setter;
2430 	last_setter = start_setter;
2431 
2432 	for (i = 0; i < at->num_actions; i++) {
2433 		switch (action_type[i]) {
2434 		case MLX5HWS_ACTION_TYP_DROP:
2435 		case MLX5HWS_ACTION_TYP_TBL:
2436 		case MLX5HWS_ACTION_TYP_DEST_ARRAY:
2437 		case MLX5HWS_ACTION_TYP_VPORT:
2438 		case MLX5HWS_ACTION_TYP_MISS:
2439 			/* Hit action */
2440 			last_setter->flags |= ASF_HIT;
2441 			last_setter->set_hit = &hws_action_setter_hit;
2442 			last_setter->idx_hit = i;
2443 			break;
2444 
2445 		case MLX5HWS_ACTION_TYP_RANGE:
2446 			last_setter->flags |= ASF_HIT;
2447 			last_setter->set_hit = &hws_action_setter_range;
2448 			last_setter->idx_hit = i;
2449 			break;
2450 
2451 		case MLX5HWS_ACTION_TYP_POP_VLAN:
2452 			/* Single remove header to header */
2453 			if (pop_setter) {
2454 				/* We have 2 pops, use the shared */
2455 				pop_setter->set_single = &hws_action_setter_single_double_pop;
2456 				break;
2457 			}
2458 			setter = hws_action_setter_find_first(last_setter,
2459 							      ASF_SINGLE1 | ASF_MODIFY |
2460 							      ASF_INSERT);
2461 			setter->flags |= ASF_SINGLE1 | ASF_REMOVE;
2462 			setter->set_single = &hws_action_setter_single;
2463 			setter->idx_single = i;
2464 			pop_setter = setter;
2465 			break;
2466 
2467 		case MLX5HWS_ACTION_TYP_PUSH_VLAN:
2468 			/* Double insert inline */
2469 			setter = hws_action_setter_find_first(last_setter, ASF_DOUBLE | ASF_REMOVE);
2470 			setter->flags |= ASF_DOUBLE | ASF_INSERT;
2471 			setter->set_double = &hws_action_setter_push_vlan;
2472 			setter->idx_double = i;
2473 			break;
2474 
2475 		case MLX5HWS_ACTION_TYP_MODIFY_HDR:
2476 			/* Double modify header list */
2477 			setter = hws_action_setter_find_first(last_setter, ASF_DOUBLE | ASF_REMOVE);
2478 			setter->flags |= ASF_DOUBLE | ASF_MODIFY;
2479 			setter->set_double = &hws_action_setter_modify_header;
2480 			setter->idx_double = i;
2481 			break;
2482 
2483 		case MLX5HWS_ACTION_TYP_ASO_METER:
2484 			/* Double ASO action */
2485 			setter = hws_action_setter_find_first(last_setter, ASF_DOUBLE);
2486 			setter->flags |= ASF_DOUBLE;
2487 			setter->set_double = &hws_action_setter_aso;
2488 			setter->idx_double = i;
2489 			break;
2490 
2491 		case MLX5HWS_ACTION_TYP_REMOVE_HEADER:
2492 		case MLX5HWS_ACTION_TYP_REFORMAT_TNL_L2_TO_L2:
2493 			/* Single remove header to header */
2494 			setter = hws_action_setter_find_first(last_setter,
2495 							      ASF_SINGLE1 | ASF_MODIFY);
2496 			setter->flags |= ASF_SINGLE1 | ASF_REMOVE;
2497 			setter->set_single = &hws_action_setter_single;
2498 			setter->idx_single = i;
2499 			break;
2500 
2501 		case MLX5HWS_ACTION_TYP_INSERT_HEADER:
2502 		case MLX5HWS_ACTION_TYP_REFORMAT_L2_TO_TNL_L2:
2503 			/* Double insert header with pointer */
2504 			setter = hws_action_setter_find_first(last_setter, ASF_DOUBLE | ASF_REMOVE);
2505 			setter->flags |= ASF_DOUBLE | ASF_INSERT;
2506 			setter->set_double = &hws_action_setter_insert_ptr;
2507 			setter->idx_double = i;
2508 			break;
2509 
2510 		case MLX5HWS_ACTION_TYP_REFORMAT_L2_TO_TNL_L3:
2511 			/* Single remove + Double insert header with pointer */
2512 			setter = hws_action_setter_find_first(last_setter,
2513 							      ASF_SINGLE1 | ASF_DOUBLE);
2514 			setter->flags |= ASF_SINGLE1 | ASF_DOUBLE;
2515 			setter->set_double = &hws_action_setter_insert_ptr;
2516 			setter->idx_double = i;
2517 			setter->set_single = &hws_action_setter_common_decap;
2518 			setter->idx_single = i;
2519 			break;
2520 
2521 		case MLX5HWS_ACTION_TYP_REFORMAT_TNL_L3_TO_L2:
2522 			/* Double modify header list with remove and push inline */
2523 			setter = hws_action_setter_find_first(last_setter, ASF_DOUBLE | ASF_REMOVE);
2524 			setter->flags |= ASF_DOUBLE | ASF_MODIFY | ASF_INSERT;
2525 			setter->set_double = &hws_action_setter_tnl_l3_to_l2;
2526 			setter->idx_double = i;
2527 			break;
2528 
2529 		case MLX5HWS_ACTION_TYP_TAG:
2530 			/* Single TAG action, search for any room from the start */
2531 			setter = hws_action_setter_find_first(start_setter, ASF_SINGLE1);
2532 			setter->flags |= ASF_SINGLE1;
2533 			setter->set_single = &hws_action_setter_tag;
2534 			setter->idx_single = i;
2535 			break;
2536 
2537 		case MLX5HWS_ACTION_TYP_CTR:
2538 			/* Control counter action
2539 			 * TODO: Current counter executed first. Support is needed
2540 			 *	 for single ation counter action which is done last.
2541 			 *	 Example: Decap + CTR
2542 			 */
2543 			setter = hws_action_setter_find_first(start_setter, ASF_CTR);
2544 			setter->flags |= ASF_CTR;
2545 			setter->set_ctr = &hws_action_setter_ctrl_ctr;
2546 			setter->idx_ctr = i;
2547 			break;
2548 		default:
2549 			pr_warn("HWS: Invalid action type in processingaction template: action_type[%d]=%d\n",
2550 				i, action_type[i]);
2551 			return -EOPNOTSUPP;
2552 		}
2553 
2554 		last_setter = max(setter, last_setter);
2555 	}
2556 
2557 	/* Set default hit on the last STE if no hit action provided */
2558 	if (!(last_setter->flags & ASF_HIT))
2559 		last_setter->set_hit = &hws_action_setter_default_hit;
2560 
2561 	at->num_of_action_stes = last_setter - start_setter + 1;
2562 
2563 	/* Check if action template doesn't require any action DWs */
2564 	at->only_term = (at->num_of_action_stes == 1) &&
2565 		!(last_setter->flags & ~(ASF_CTR | ASF_HIT));
2566 
2567 	return 0;
2568 }
2569 
2570 struct mlx5hws_action_template *
mlx5hws_action_template_create(enum mlx5hws_action_type action_type[])2571 mlx5hws_action_template_create(enum mlx5hws_action_type action_type[])
2572 {
2573 	struct mlx5hws_action_template *at;
2574 	u8 num_actions = 0;
2575 	int i;
2576 
2577 	at = kzalloc(sizeof(*at), GFP_KERNEL);
2578 	if (!at)
2579 		return NULL;
2580 
2581 	while (action_type[num_actions++] != MLX5HWS_ACTION_TYP_LAST)
2582 		;
2583 
2584 	at->num_actions = num_actions - 1;
2585 	at->action_type_arr = kcalloc(num_actions, sizeof(*action_type), GFP_KERNEL);
2586 	if (!at->action_type_arr)
2587 		goto free_at;
2588 
2589 	for (i = 0; i < num_actions; i++)
2590 		at->action_type_arr[i] = action_type[i];
2591 
2592 	return at;
2593 
2594 free_at:
2595 	kfree(at);
2596 	return NULL;
2597 }
2598 
mlx5hws_action_template_destroy(struct mlx5hws_action_template * at)2599 int mlx5hws_action_template_destroy(struct mlx5hws_action_template *at)
2600 {
2601 	kfree(at->action_type_arr);
2602 	kfree(at);
2603 	return 0;
2604 }
2605