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