xref: /linux/drivers/net/ethernet/mellanox/mlx5/core/steering/hws/mlx5hws_action.h (revision 9410645520e9b820069761f3450ef6661418e279)
1 /* SPDX-License-Identifier: GPL-2.0 OR Linux-OpenIB */
2 /* Copyright (c) 2024 NVIDIA Corporation & Affiliates */
3 
4 #ifndef MLX5HWS_ACTION_H_
5 #define MLX5HWS_ACTION_H_
6 
7 /* Max number of STEs needed for a rule (including match) */
8 #define MLX5HWS_ACTION_MAX_STE 20
9 
10 /* Max number of internal subactions of ipv6_ext */
11 #define MLX5HWS_ACTION_IPV6_EXT_MAX_SA 4
12 
13 enum mlx5hws_action_stc_idx {
14 	MLX5HWS_ACTION_STC_IDX_CTRL = 0,
15 	MLX5HWS_ACTION_STC_IDX_HIT = 1,
16 	MLX5HWS_ACTION_STC_IDX_DW5 = 2,
17 	MLX5HWS_ACTION_STC_IDX_DW6 = 3,
18 	MLX5HWS_ACTION_STC_IDX_DW7 = 4,
19 	MLX5HWS_ACTION_STC_IDX_MAX = 5,
20 	/* STC Jumvo STE combo: CTR, Hit */
21 	MLX5HWS_ACTION_STC_IDX_LAST_JUMBO_STE = 1,
22 	/* STC combo1: CTR, SINGLE, DOUBLE, Hit */
23 	MLX5HWS_ACTION_STC_IDX_LAST_COMBO1 = 3,
24 	/* STC combo2: CTR, 3 x SINGLE, Hit */
25 	MLX5HWS_ACTION_STC_IDX_LAST_COMBO2 = 4,
26 	/* STC combo2: CTR, TRIPLE, Hit */
27 	MLX5HWS_ACTION_STC_IDX_LAST_COMBO3 = 2,
28 };
29 
30 enum mlx5hws_action_offset {
31 	MLX5HWS_ACTION_OFFSET_DW0 = 0,
32 	MLX5HWS_ACTION_OFFSET_DW5 = 5,
33 	MLX5HWS_ACTION_OFFSET_DW6 = 6,
34 	MLX5HWS_ACTION_OFFSET_DW7 = 7,
35 	MLX5HWS_ACTION_OFFSET_HIT = 3,
36 	MLX5HWS_ACTION_OFFSET_HIT_LSB = 4,
37 };
38 
39 enum {
40 	MLX5HWS_ACTION_DOUBLE_SIZE = 8,
41 	MLX5HWS_ACTION_INLINE_DATA_SIZE = 4,
42 	MLX5HWS_ACTION_HDR_LEN_L2_MACS = 12,
43 	MLX5HWS_ACTION_HDR_LEN_L2_VLAN = 4,
44 	MLX5HWS_ACTION_HDR_LEN_L2_ETHER = 2,
45 	MLX5HWS_ACTION_HDR_LEN_L2 = (MLX5HWS_ACTION_HDR_LEN_L2_MACS +
46 				     MLX5HWS_ACTION_HDR_LEN_L2_ETHER),
47 	MLX5HWS_ACTION_HDR_LEN_L2_W_VLAN = (MLX5HWS_ACTION_HDR_LEN_L2 +
48 					    MLX5HWS_ACTION_HDR_LEN_L2_VLAN),
49 	MLX5HWS_ACTION_REFORMAT_DATA_SIZE = 64,
50 	DECAP_L3_NUM_ACTIONS_W_NO_VLAN = 6,
51 	DECAP_L3_NUM_ACTIONS_W_VLAN = 7,
52 };
53 
54 enum mlx5hws_action_setter_flag {
55 	ASF_SINGLE1 = 1 << 0,
56 	ASF_SINGLE2 = 1 << 1,
57 	ASF_SINGLE3 = 1 << 2,
58 	ASF_DOUBLE = ASF_SINGLE2 | ASF_SINGLE3,
59 	ASF_TRIPLE = ASF_SINGLE1 | ASF_DOUBLE,
60 	ASF_INSERT = 1 << 3,
61 	ASF_REMOVE = 1 << 4,
62 	ASF_MODIFY = 1 << 5,
63 	ASF_CTR = 1 << 6,
64 	ASF_HIT = 1 << 7,
65 };
66 
67 struct mlx5hws_action_default_stc {
68 	struct mlx5hws_pool_chunk nop_ctr;
69 	struct mlx5hws_pool_chunk nop_dw5;
70 	struct mlx5hws_pool_chunk nop_dw6;
71 	struct mlx5hws_pool_chunk nop_dw7;
72 	struct mlx5hws_pool_chunk default_hit;
73 	u32 refcount;
74 };
75 
76 struct mlx5hws_action_shared_stc {
77 	struct mlx5hws_pool_chunk stc_chunk;
78 	u32 refcount;
79 };
80 
81 struct mlx5hws_actions_apply_data {
82 	struct mlx5hws_send_engine *queue;
83 	struct mlx5hws_rule_action *rule_action;
84 	__be32 *wqe_data;
85 	struct mlx5hws_wqe_gta_ctrl_seg *wqe_ctrl;
86 	u32 jump_to_action_stc;
87 	struct mlx5hws_context_common_res *common_res;
88 	enum mlx5hws_table_type tbl_type;
89 	u32 next_direct_idx;
90 	u8 require_dep;
91 };
92 
93 struct mlx5hws_actions_wqe_setter;
94 
95 typedef void (*mlx5hws_action_setter_fp)(struct mlx5hws_actions_apply_data *apply,
96 					 struct mlx5hws_actions_wqe_setter *setter);
97 
98 struct mlx5hws_actions_wqe_setter {
99 	mlx5hws_action_setter_fp set_single;
100 	mlx5hws_action_setter_fp set_double;
101 	mlx5hws_action_setter_fp set_triple;
102 	mlx5hws_action_setter_fp set_hit;
103 	mlx5hws_action_setter_fp set_ctr;
104 	u8 idx_single;
105 	u8 idx_double;
106 	u8 idx_triple;
107 	u8 idx_ctr;
108 	u8 idx_hit;
109 	u8 stage_idx;
110 	u8 flags;
111 };
112 
113 struct mlx5hws_action_template {
114 	struct mlx5hws_actions_wqe_setter setters[MLX5HWS_ACTION_MAX_STE];
115 	enum mlx5hws_action_type *action_type_arr;
116 	u8 num_of_action_stes;
117 	u8 num_actions;
118 	u8 only_term;
119 };
120 
121 struct mlx5hws_action {
122 	u8 type;
123 	u8 flags;
124 	struct mlx5hws_context *ctx;
125 	union {
126 		struct {
127 			struct mlx5hws_pool_chunk stc[MLX5HWS_TABLE_TYPE_MAX];
128 			union {
129 				struct {
130 					u32 pat_id;
131 					u32 arg_id;
132 					__be64 single_action;
133 					u32 nope_locations;
134 					u8 num_of_patterns;
135 					u8 single_action_type;
136 					u8 num_of_actions;
137 					u8 max_num_of_actions;
138 					u8 require_reparse;
139 				} modify_header;
140 				struct {
141 					u32 arg_id;
142 					u32 header_size;
143 					u16 max_hdr_sz;
144 					u8 num_of_hdrs;
145 					u8 anchor;
146 					u8 e_anchor;
147 					u8 offset;
148 					bool encap;
149 					u8 require_reparse;
150 				} reformat;
151 				struct {
152 					u32 obj_id;
153 					u8 return_reg_id;
154 				} aso;
155 				struct {
156 					u16 vport_num;
157 					u16 esw_owner_vhca_id;
158 					bool esw_owner_vhca_id_valid;
159 				} vport;
160 				struct {
161 					u32 obj_id;
162 				} dest_obj;
163 				struct {
164 					struct mlx5hws_cmd_forward_tbl *fw_island;
165 					size_t num_dest;
166 					struct mlx5hws_cmd_set_fte_dest *dest_list;
167 				} dest_array;
168 				struct {
169 					u8 type;
170 					u8 start_anchor;
171 					u8 end_anchor;
172 					u8 num_of_words;
173 					bool decap;
174 				} insert_hdr;
175 				struct {
176 					/* PRM start anchor from which header will be removed */
177 					u8 anchor;
178 					/* Header remove offset in bytes, from the start
179 					 * anchor to the location where remove header starts.
180 					 */
181 					u8 offset;
182 					/* Indicates the removed header size in bytes */
183 					size_t size;
184 				} remove_header;
185 				struct {
186 					struct mlx5hws_matcher_action_ste *table_ste;
187 					struct mlx5hws_action *hit_ft_action;
188 					struct mlx5hws_definer *definer;
189 				} range;
190 			};
191 		};
192 
193 		struct ibv_flow_action *flow_action;
194 		u32 obj_id;
195 		struct ibv_qp *qp;
196 	};
197 };
198 
199 const char *mlx5hws_action_type_to_str(enum mlx5hws_action_type action_type);
200 
201 int mlx5hws_action_get_default_stc(struct mlx5hws_context *ctx,
202 				   u8 tbl_type);
203 
204 void mlx5hws_action_put_default_stc(struct mlx5hws_context *ctx,
205 				    u8 tbl_type);
206 
207 void mlx5hws_action_prepare_decap_l3_data(u8 *src, u8 *dst,
208 					  u16 num_of_actions);
209 
210 int mlx5hws_action_template_process(struct mlx5hws_action_template *at);
211 
212 bool mlx5hws_action_check_combo(struct mlx5hws_context *ctx,
213 				enum mlx5hws_action_type *user_actions,
214 				enum mlx5hws_table_type table_type);
215 
216 int mlx5hws_action_alloc_single_stc(struct mlx5hws_context *ctx,
217 				    struct mlx5hws_cmd_stc_modify_attr *stc_attr,
218 				    u32 table_type,
219 				    struct mlx5hws_pool_chunk *stc);
220 
221 void mlx5hws_action_free_single_stc(struct mlx5hws_context *ctx,
222 				    u32 table_type,
223 				    struct mlx5hws_pool_chunk *stc);
224 
225 static inline void
mlx5hws_action_setter_default_single(struct mlx5hws_actions_apply_data * apply,struct mlx5hws_actions_wqe_setter * setter)226 mlx5hws_action_setter_default_single(struct mlx5hws_actions_apply_data *apply,
227 				     struct mlx5hws_actions_wqe_setter *setter)
228 {
229 	apply->wqe_data[MLX5HWS_ACTION_OFFSET_DW5] = 0;
230 	apply->wqe_ctrl->stc_ix[MLX5HWS_ACTION_STC_IDX_DW5] =
231 		htonl(apply->common_res->default_stc->nop_dw5.offset);
232 }
233 
234 static inline void
mlx5hws_action_setter_default_double(struct mlx5hws_actions_apply_data * apply,struct mlx5hws_actions_wqe_setter * setter)235 mlx5hws_action_setter_default_double(struct mlx5hws_actions_apply_data *apply,
236 				     struct mlx5hws_actions_wqe_setter *setter)
237 {
238 	apply->wqe_data[MLX5HWS_ACTION_OFFSET_DW6] = 0;
239 	apply->wqe_data[MLX5HWS_ACTION_OFFSET_DW7] = 0;
240 	apply->wqe_ctrl->stc_ix[MLX5HWS_ACTION_STC_IDX_DW6] =
241 		htonl(apply->common_res->default_stc->nop_dw6.offset);
242 	apply->wqe_ctrl->stc_ix[MLX5HWS_ACTION_STC_IDX_DW7] =
243 		htonl(apply->common_res->default_stc->nop_dw7.offset);
244 }
245 
246 static inline void
mlx5hws_action_setter_default_ctr(struct mlx5hws_actions_apply_data * apply,struct mlx5hws_actions_wqe_setter * setter)247 mlx5hws_action_setter_default_ctr(struct mlx5hws_actions_apply_data *apply,
248 				  struct mlx5hws_actions_wqe_setter *setter)
249 {
250 	apply->wqe_data[MLX5HWS_ACTION_OFFSET_DW0] = 0;
251 	apply->wqe_ctrl->stc_ix[MLX5HWS_ACTION_STC_IDX_CTRL] =
252 		htonl(apply->common_res->default_stc->nop_ctr.offset);
253 }
254 
255 static inline void
mlx5hws_action_apply_setter(struct mlx5hws_actions_apply_data * apply,struct mlx5hws_actions_wqe_setter * setter,bool is_jumbo)256 mlx5hws_action_apply_setter(struct mlx5hws_actions_apply_data *apply,
257 			    struct mlx5hws_actions_wqe_setter *setter,
258 			    bool is_jumbo)
259 {
260 	u8 num_of_actions;
261 
262 	/* Set control counter */
263 	if (setter->set_ctr)
264 		setter->set_ctr(apply, setter);
265 	else
266 		mlx5hws_action_setter_default_ctr(apply, setter);
267 
268 	if (!is_jumbo) {
269 		if (unlikely(setter->set_triple)) {
270 			/* Set triple on match */
271 			setter->set_triple(apply, setter);
272 			num_of_actions = MLX5HWS_ACTION_STC_IDX_LAST_COMBO3;
273 		} else {
274 			/* Set single and double on match */
275 			if (setter->set_single)
276 				setter->set_single(apply, setter);
277 			else
278 				mlx5hws_action_setter_default_single(apply, setter);
279 
280 			if (setter->set_double)
281 				setter->set_double(apply, setter);
282 			else
283 				mlx5hws_action_setter_default_double(apply, setter);
284 
285 			num_of_actions = setter->set_double ?
286 				MLX5HWS_ACTION_STC_IDX_LAST_COMBO1 :
287 				MLX5HWS_ACTION_STC_IDX_LAST_COMBO2;
288 		}
289 	} else {
290 		apply->wqe_data[MLX5HWS_ACTION_OFFSET_DW5] = 0;
291 		apply->wqe_data[MLX5HWS_ACTION_OFFSET_DW6] = 0;
292 		apply->wqe_data[MLX5HWS_ACTION_OFFSET_DW7] = 0;
293 		apply->wqe_ctrl->stc_ix[MLX5HWS_ACTION_STC_IDX_DW5] = 0;
294 		apply->wqe_ctrl->stc_ix[MLX5HWS_ACTION_STC_IDX_DW6] = 0;
295 		apply->wqe_ctrl->stc_ix[MLX5HWS_ACTION_STC_IDX_DW7] = 0;
296 		num_of_actions = MLX5HWS_ACTION_STC_IDX_LAST_JUMBO_STE;
297 	}
298 
299 	/* Set next/final hit action */
300 	setter->set_hit(apply, setter);
301 
302 	/* Set number of actions */
303 	apply->wqe_ctrl->stc_ix[MLX5HWS_ACTION_STC_IDX_CTRL] |=
304 		htonl(num_of_actions << 29);
305 }
306 
307 #endif /* MLX5HWS_ACTION_H_ */
308