• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // SPDX-License-Identifier: GPL-2.0 OR Linux-OpenIB
2 /* Copyright (c) 2019 Mellanox Technologies. */
3 
4 #include "en/params.h"
5 #include "en/txrx.h"
6 #include "en/port.h"
7 #include "en_accel/en_accel.h"
8 #include "accel/ipsec.h"
9 #include "fpga/ipsec.h"
10 
mlx5e_rx_is_xdp(struct mlx5e_params * params,struct mlx5e_xsk_param * xsk)11 static bool mlx5e_rx_is_xdp(struct mlx5e_params *params,
12 			    struct mlx5e_xsk_param *xsk)
13 {
14 	return params->xdp_prog || xsk;
15 }
16 
mlx5e_get_linear_rq_headroom(struct mlx5e_params * params,struct mlx5e_xsk_param * xsk)17 u16 mlx5e_get_linear_rq_headroom(struct mlx5e_params *params,
18 				 struct mlx5e_xsk_param *xsk)
19 {
20 	u16 headroom;
21 
22 	if (xsk)
23 		return xsk->headroom;
24 
25 	headroom = NET_IP_ALIGN;
26 	if (mlx5e_rx_is_xdp(params, xsk))
27 		headroom += XDP_PACKET_HEADROOM;
28 	else
29 		headroom += MLX5_RX_HEADROOM;
30 
31 	return headroom;
32 }
33 
mlx5e_rx_get_min_frag_sz(struct mlx5e_params * params,struct mlx5e_xsk_param * xsk)34 u32 mlx5e_rx_get_min_frag_sz(struct mlx5e_params *params,
35 			     struct mlx5e_xsk_param *xsk)
36 {
37 	u32 hw_mtu = MLX5E_SW2HW_MTU(params, params->sw_mtu);
38 	u16 linear_rq_headroom = mlx5e_get_linear_rq_headroom(params, xsk);
39 
40 	return linear_rq_headroom + hw_mtu;
41 }
42 
mlx5e_rx_get_linear_frag_sz(struct mlx5e_params * params,struct mlx5e_xsk_param * xsk)43 static u32 mlx5e_rx_get_linear_frag_sz(struct mlx5e_params *params,
44 				       struct mlx5e_xsk_param *xsk)
45 {
46 	u32 frag_sz = mlx5e_rx_get_min_frag_sz(params, xsk);
47 
48 	/* AF_XDP doesn't build SKBs in place. */
49 	if (!xsk)
50 		frag_sz = MLX5_SKB_FRAG_SZ(frag_sz);
51 
52 	/* XDP in mlx5e doesn't support multiple packets per page. AF_XDP is a
53 	 * special case. It can run with frames smaller than a page, as it
54 	 * doesn't allocate pages dynamically. However, here we pretend that
55 	 * fragments are page-sized: it allows to treat XSK frames like pages
56 	 * by redirecting alloc and free operations to XSK rings and by using
57 	 * the fact there are no multiple packets per "page" (which is a frame).
58 	 * The latter is important, because frames may come in a random order,
59 	 * and we will have trouble assemblying a real page of multiple frames.
60 	 */
61 	if (mlx5e_rx_is_xdp(params, xsk))
62 		frag_sz = max_t(u32, frag_sz, PAGE_SIZE);
63 
64 	/* Even if we can go with a smaller fragment size, we must not put
65 	 * multiple packets into a single frame.
66 	 */
67 	if (xsk)
68 		frag_sz = max_t(u32, frag_sz, xsk->chunk_size);
69 
70 	return frag_sz;
71 }
72 
mlx5e_mpwqe_log_pkts_per_wqe(struct mlx5e_params * params,struct mlx5e_xsk_param * xsk)73 u8 mlx5e_mpwqe_log_pkts_per_wqe(struct mlx5e_params *params,
74 				struct mlx5e_xsk_param *xsk)
75 {
76 	u32 linear_frag_sz = mlx5e_rx_get_linear_frag_sz(params, xsk);
77 
78 	return MLX5_MPWRQ_LOG_WQE_SZ - order_base_2(linear_frag_sz);
79 }
80 
mlx5e_rx_is_linear_skb(struct mlx5e_params * params,struct mlx5e_xsk_param * xsk)81 bool mlx5e_rx_is_linear_skb(struct mlx5e_params *params,
82 			    struct mlx5e_xsk_param *xsk)
83 {
84 	/* AF_XDP allocates SKBs on XDP_PASS - ensure they don't occupy more
85 	 * than one page. For this, check both with and without xsk.
86 	 */
87 	u32 linear_frag_sz = max(mlx5e_rx_get_linear_frag_sz(params, xsk),
88 				 mlx5e_rx_get_linear_frag_sz(params, NULL));
89 
90 	return params->packet_merge.type == MLX5E_PACKET_MERGE_NONE &&
91 		linear_frag_sz <= PAGE_SIZE;
92 }
93 
mlx5e_verify_rx_mpwqe_strides(struct mlx5_core_dev * mdev,u8 log_stride_sz,u8 log_num_strides)94 bool mlx5e_verify_rx_mpwqe_strides(struct mlx5_core_dev *mdev,
95 				   u8 log_stride_sz, u8 log_num_strides)
96 {
97 	if (log_stride_sz + log_num_strides != MLX5_MPWRQ_LOG_WQE_SZ)
98 		return false;
99 
100 	if (log_stride_sz < MLX5_MPWQE_LOG_STRIDE_SZ_BASE ||
101 	    log_stride_sz > MLX5_MPWQE_LOG_STRIDE_SZ_MAX)
102 		return false;
103 
104 	if (log_num_strides > MLX5_MPWQE_LOG_NUM_STRIDES_MAX)
105 		return false;
106 
107 	if (MLX5_CAP_GEN(mdev, ext_stride_num_range))
108 		return log_num_strides >= MLX5_MPWQE_LOG_NUM_STRIDES_EXT_BASE;
109 
110 	return log_num_strides >= MLX5_MPWQE_LOG_NUM_STRIDES_BASE;
111 }
112 
mlx5e_rx_mpwqe_is_linear_skb(struct mlx5_core_dev * mdev,struct mlx5e_params * params,struct mlx5e_xsk_param * xsk)113 bool mlx5e_rx_mpwqe_is_linear_skb(struct mlx5_core_dev *mdev,
114 				  struct mlx5e_params *params,
115 				  struct mlx5e_xsk_param *xsk)
116 {
117 	s8 log_num_strides;
118 	u8 log_stride_sz;
119 
120 	if (!mlx5e_rx_is_linear_skb(params, xsk))
121 		return false;
122 
123 	log_stride_sz = order_base_2(mlx5e_rx_get_linear_frag_sz(params, xsk));
124 	log_num_strides = MLX5_MPWRQ_LOG_WQE_SZ - log_stride_sz;
125 
126 	return mlx5e_verify_rx_mpwqe_strides(mdev, log_stride_sz, log_num_strides);
127 }
128 
mlx5e_mpwqe_get_log_rq_size(struct mlx5e_params * params,struct mlx5e_xsk_param * xsk)129 u8 mlx5e_mpwqe_get_log_rq_size(struct mlx5e_params *params,
130 			       struct mlx5e_xsk_param *xsk)
131 {
132 	u8 log_pkts_per_wqe = mlx5e_mpwqe_log_pkts_per_wqe(params, xsk);
133 
134 	/* Numbers are unsigned, don't subtract to avoid underflow. */
135 	if (params->log_rq_mtu_frames <
136 	    log_pkts_per_wqe + MLX5E_PARAMS_MINIMUM_LOG_RQ_SIZE_MPW)
137 		return MLX5E_PARAMS_MINIMUM_LOG_RQ_SIZE_MPW;
138 
139 	return params->log_rq_mtu_frames - log_pkts_per_wqe;
140 }
141 
mlx5e_mpwqe_get_log_stride_size(struct mlx5_core_dev * mdev,struct mlx5e_params * params,struct mlx5e_xsk_param * xsk)142 u8 mlx5e_mpwqe_get_log_stride_size(struct mlx5_core_dev *mdev,
143 				   struct mlx5e_params *params,
144 				   struct mlx5e_xsk_param *xsk)
145 {
146 	if (mlx5e_rx_mpwqe_is_linear_skb(mdev, params, xsk))
147 		return order_base_2(mlx5e_rx_get_linear_frag_sz(params, xsk));
148 
149 	return MLX5_MPWRQ_DEF_LOG_STRIDE_SZ(mdev);
150 }
151 
mlx5e_mpwqe_get_log_num_strides(struct mlx5_core_dev * mdev,struct mlx5e_params * params,struct mlx5e_xsk_param * xsk)152 u8 mlx5e_mpwqe_get_log_num_strides(struct mlx5_core_dev *mdev,
153 				   struct mlx5e_params *params,
154 				   struct mlx5e_xsk_param *xsk)
155 {
156 	return MLX5_MPWRQ_LOG_WQE_SZ -
157 		mlx5e_mpwqe_get_log_stride_size(mdev, params, xsk);
158 }
159 
mlx5e_get_rq_headroom(struct mlx5_core_dev * mdev,struct mlx5e_params * params,struct mlx5e_xsk_param * xsk)160 u16 mlx5e_get_rq_headroom(struct mlx5_core_dev *mdev,
161 			  struct mlx5e_params *params,
162 			  struct mlx5e_xsk_param *xsk)
163 {
164 	bool is_linear_skb = (params->rq_wq_type == MLX5_WQ_TYPE_CYCLIC) ?
165 		mlx5e_rx_is_linear_skb(params, xsk) :
166 		mlx5e_rx_mpwqe_is_linear_skb(mdev, params, xsk);
167 
168 	return is_linear_skb || params->packet_merge.type == MLX5E_PACKET_MERGE_SHAMPO ?
169 		mlx5e_get_linear_rq_headroom(params, xsk) : 0;
170 }
171 
mlx5e_calc_sq_stop_room(struct mlx5_core_dev * mdev,struct mlx5e_params * params)172 u16 mlx5e_calc_sq_stop_room(struct mlx5_core_dev *mdev, struct mlx5e_params *params)
173 {
174 	bool is_mpwqe = MLX5E_GET_PFLAG(params, MLX5E_PFLAG_SKB_TX_MPWQE);
175 	u16 stop_room;
176 
177 	stop_room  = mlx5e_tls_get_stop_room(mdev, params);
178 	stop_room += mlx5e_stop_room_for_wqe(MLX5_SEND_WQE_MAX_WQEBBS);
179 	if (is_mpwqe)
180 		/* A MPWQE can take up to the maximum-sized WQE + all the normal
181 		 * stop room can be taken if a new packet breaks the active
182 		 * MPWQE session and allocates its WQEs right away.
183 		 */
184 		stop_room += mlx5e_stop_room_for_wqe(MLX5_SEND_WQE_MAX_WQEBBS);
185 
186 	return stop_room;
187 }
188 
mlx5e_validate_params(struct mlx5_core_dev * mdev,struct mlx5e_params * params)189 int mlx5e_validate_params(struct mlx5_core_dev *mdev, struct mlx5e_params *params)
190 {
191 	size_t sq_size = 1 << params->log_sq_size;
192 	u16 stop_room;
193 
194 	stop_room = mlx5e_calc_sq_stop_room(mdev, params);
195 	if (stop_room >= sq_size) {
196 		mlx5_core_err(mdev, "Stop room %u is bigger than the SQ size %zu\n",
197 			      stop_room, sq_size);
198 		return -EINVAL;
199 	}
200 
201 	return 0;
202 }
203 
mlx5e_get_def_tx_moderation(u8 cq_period_mode)204 static struct dim_cq_moder mlx5e_get_def_tx_moderation(u8 cq_period_mode)
205 {
206 	struct dim_cq_moder moder = {};
207 
208 	moder.cq_period_mode = cq_period_mode;
209 	moder.pkts = MLX5E_PARAMS_DEFAULT_TX_CQ_MODERATION_PKTS;
210 	moder.usec = MLX5E_PARAMS_DEFAULT_TX_CQ_MODERATION_USEC;
211 	if (cq_period_mode == MLX5_CQ_PERIOD_MODE_START_FROM_CQE)
212 		moder.usec = MLX5E_PARAMS_DEFAULT_TX_CQ_MODERATION_USEC_FROM_CQE;
213 
214 	return moder;
215 }
216 
mlx5e_get_def_rx_moderation(u8 cq_period_mode)217 static struct dim_cq_moder mlx5e_get_def_rx_moderation(u8 cq_period_mode)
218 {
219 	struct dim_cq_moder moder = {};
220 
221 	moder.cq_period_mode = cq_period_mode;
222 	moder.pkts = MLX5E_PARAMS_DEFAULT_RX_CQ_MODERATION_PKTS;
223 	moder.usec = MLX5E_PARAMS_DEFAULT_RX_CQ_MODERATION_USEC;
224 	if (cq_period_mode == MLX5_CQ_PERIOD_MODE_START_FROM_CQE)
225 		moder.usec = MLX5E_PARAMS_DEFAULT_RX_CQ_MODERATION_USEC_FROM_CQE;
226 
227 	return moder;
228 }
229 
mlx5_to_net_dim_cq_period_mode(u8 cq_period_mode)230 static u8 mlx5_to_net_dim_cq_period_mode(u8 cq_period_mode)
231 {
232 	return cq_period_mode == MLX5_CQ_PERIOD_MODE_START_FROM_CQE ?
233 		DIM_CQ_PERIOD_MODE_START_FROM_CQE :
234 		DIM_CQ_PERIOD_MODE_START_FROM_EQE;
235 }
236 
mlx5e_reset_tx_moderation(struct mlx5e_params * params,u8 cq_period_mode)237 void mlx5e_reset_tx_moderation(struct mlx5e_params *params, u8 cq_period_mode)
238 {
239 	if (params->tx_dim_enabled) {
240 		u8 dim_period_mode = mlx5_to_net_dim_cq_period_mode(cq_period_mode);
241 
242 		params->tx_cq_moderation = net_dim_get_def_tx_moderation(dim_period_mode);
243 	} else {
244 		params->tx_cq_moderation = mlx5e_get_def_tx_moderation(cq_period_mode);
245 	}
246 }
247 
mlx5e_reset_rx_moderation(struct mlx5e_params * params,u8 cq_period_mode)248 void mlx5e_reset_rx_moderation(struct mlx5e_params *params, u8 cq_period_mode)
249 {
250 	if (params->rx_dim_enabled) {
251 		u8 dim_period_mode = mlx5_to_net_dim_cq_period_mode(cq_period_mode);
252 
253 		params->rx_cq_moderation = net_dim_get_def_rx_moderation(dim_period_mode);
254 	} else {
255 		params->rx_cq_moderation = mlx5e_get_def_rx_moderation(cq_period_mode);
256 	}
257 }
258 
mlx5e_set_tx_cq_mode_params(struct mlx5e_params * params,u8 cq_period_mode)259 void mlx5e_set_tx_cq_mode_params(struct mlx5e_params *params, u8 cq_period_mode)
260 {
261 	mlx5e_reset_tx_moderation(params, cq_period_mode);
262 	MLX5E_SET_PFLAG(params, MLX5E_PFLAG_TX_CQE_BASED_MODER,
263 			params->tx_cq_moderation.cq_period_mode ==
264 				MLX5_CQ_PERIOD_MODE_START_FROM_CQE);
265 }
266 
mlx5e_set_rx_cq_mode_params(struct mlx5e_params * params,u8 cq_period_mode)267 void mlx5e_set_rx_cq_mode_params(struct mlx5e_params *params, u8 cq_period_mode)
268 {
269 	mlx5e_reset_rx_moderation(params, cq_period_mode);
270 	MLX5E_SET_PFLAG(params, MLX5E_PFLAG_RX_CQE_BASED_MODER,
271 			params->rx_cq_moderation.cq_period_mode ==
272 				MLX5_CQ_PERIOD_MODE_START_FROM_CQE);
273 }
274 
slow_pci_heuristic(struct mlx5_core_dev * mdev)275 bool slow_pci_heuristic(struct mlx5_core_dev *mdev)
276 {
277 	u32 link_speed = 0;
278 	u32 pci_bw = 0;
279 
280 	mlx5e_port_max_linkspeed(mdev, &link_speed);
281 	pci_bw = pcie_bandwidth_available(mdev->pdev, NULL, NULL, NULL);
282 	mlx5_core_dbg_once(mdev, "Max link speed = %d, PCI BW = %d\n",
283 			   link_speed, pci_bw);
284 
285 #define MLX5E_SLOW_PCI_RATIO (2)
286 
287 	return link_speed && pci_bw &&
288 		link_speed > MLX5E_SLOW_PCI_RATIO * pci_bw;
289 }
290 
mlx5e_striding_rq_possible(struct mlx5_core_dev * mdev,struct mlx5e_params * params)291 bool mlx5e_striding_rq_possible(struct mlx5_core_dev *mdev,
292 				struct mlx5e_params *params)
293 {
294 	if (!mlx5e_check_fragmented_striding_rq_cap(mdev))
295 		return false;
296 
297 	if (mlx5_fpga_is_ipsec_device(mdev))
298 		return false;
299 
300 	if (params->xdp_prog) {
301 		/* XSK params are not considered here. If striding RQ is in use,
302 		 * and an XSK is being opened, mlx5e_rx_mpwqe_is_linear_skb will
303 		 * be called with the known XSK params.
304 		 */
305 		if (!mlx5e_rx_mpwqe_is_linear_skb(mdev, params, NULL))
306 			return false;
307 	}
308 
309 	return true;
310 }
311 
mlx5e_init_rq_type_params(struct mlx5_core_dev * mdev,struct mlx5e_params * params)312 void mlx5e_init_rq_type_params(struct mlx5_core_dev *mdev,
313 			       struct mlx5e_params *params)
314 {
315 	params->log_rq_mtu_frames = is_kdump_kernel() ?
316 		MLX5E_PARAMS_MINIMUM_LOG_RQ_SIZE :
317 		MLX5E_PARAMS_DEFAULT_LOG_RQ_SIZE;
318 
319 	mlx5_core_info(mdev, "MLX5E: StrdRq(%d) RqSz(%ld) StrdSz(%ld) RxCqeCmprss(%d)\n",
320 		       params->rq_wq_type == MLX5_WQ_TYPE_LINKED_LIST_STRIDING_RQ,
321 		       params->rq_wq_type == MLX5_WQ_TYPE_LINKED_LIST_STRIDING_RQ ?
322 		       BIT(mlx5e_mpwqe_get_log_rq_size(params, NULL)) :
323 		       BIT(params->log_rq_mtu_frames),
324 		       BIT(mlx5e_mpwqe_get_log_stride_size(mdev, params, NULL)),
325 		       MLX5E_GET_PFLAG(params, MLX5E_PFLAG_RX_CQE_COMPRESS));
326 }
327 
mlx5e_set_rq_type(struct mlx5_core_dev * mdev,struct mlx5e_params * params)328 void mlx5e_set_rq_type(struct mlx5_core_dev *mdev, struct mlx5e_params *params)
329 {
330 	params->rq_wq_type = mlx5e_striding_rq_possible(mdev, params) &&
331 		MLX5E_GET_PFLAG(params, MLX5E_PFLAG_RX_STRIDING_RQ) ?
332 		MLX5_WQ_TYPE_LINKED_LIST_STRIDING_RQ :
333 		MLX5_WQ_TYPE_CYCLIC;
334 }
335 
mlx5e_build_rq_params(struct mlx5_core_dev * mdev,struct mlx5e_params * params)336 void mlx5e_build_rq_params(struct mlx5_core_dev *mdev,
337 			   struct mlx5e_params *params)
338 {
339 	/* Prefer Striding RQ, unless any of the following holds:
340 	 * - Striding RQ configuration is not possible/supported.
341 	 * - Slow PCI heuristic.
342 	 * - Legacy RQ would use linear SKB while Striding RQ would use non-linear.
343 	 *
344 	 * No XSK params: checking the availability of striding RQ in general.
345 	 */
346 	if (!slow_pci_heuristic(mdev) &&
347 	    mlx5e_striding_rq_possible(mdev, params) &&
348 	    (mlx5e_rx_mpwqe_is_linear_skb(mdev, params, NULL) ||
349 	     !mlx5e_rx_is_linear_skb(params, NULL)))
350 		MLX5E_SET_PFLAG(params, MLX5E_PFLAG_RX_STRIDING_RQ, true);
351 	mlx5e_set_rq_type(mdev, params);
352 	mlx5e_init_rq_type_params(mdev, params);
353 }
354 
355 /* Build queue parameters */
356 
mlx5e_build_create_cq_param(struct mlx5e_create_cq_param * ccp,struct mlx5e_channel * c)357 void mlx5e_build_create_cq_param(struct mlx5e_create_cq_param *ccp, struct mlx5e_channel *c)
358 {
359 	*ccp = (struct mlx5e_create_cq_param) {
360 		.napi = &c->napi,
361 		.ch_stats = c->stats,
362 		.node = cpu_to_node(c->cpu),
363 		.ix = c->ix,
364 	};
365 }
366 
367 #define DEFAULT_FRAG_SIZE (2048)
368 
mlx5e_build_rq_frags_info(struct mlx5_core_dev * mdev,struct mlx5e_params * params,struct mlx5e_xsk_param * xsk,struct mlx5e_rq_frags_info * info)369 static void mlx5e_build_rq_frags_info(struct mlx5_core_dev *mdev,
370 				      struct mlx5e_params *params,
371 				      struct mlx5e_xsk_param *xsk,
372 				      struct mlx5e_rq_frags_info *info)
373 {
374 	u32 byte_count = MLX5E_SW2HW_MTU(params, params->sw_mtu);
375 	int frag_size_max = DEFAULT_FRAG_SIZE;
376 	u32 buf_size = 0;
377 	int i;
378 
379 	if (mlx5_fpga_is_ipsec_device(mdev))
380 		byte_count += MLX5E_METADATA_ETHER_LEN;
381 
382 	if (mlx5e_rx_is_linear_skb(params, xsk)) {
383 		int frag_stride;
384 
385 		frag_stride = mlx5e_rx_get_linear_frag_sz(params, xsk);
386 		frag_stride = roundup_pow_of_two(frag_stride);
387 
388 		info->arr[0].frag_size = byte_count;
389 		info->arr[0].frag_stride = frag_stride;
390 		info->num_frags = 1;
391 		info->wqe_bulk = PAGE_SIZE / frag_stride;
392 		goto out;
393 	}
394 
395 	if (byte_count > PAGE_SIZE +
396 	    (MLX5E_MAX_RX_FRAGS - 1) * frag_size_max)
397 		frag_size_max = PAGE_SIZE;
398 
399 	i = 0;
400 	while (buf_size < byte_count) {
401 		int frag_size = byte_count - buf_size;
402 
403 		if (i < MLX5E_MAX_RX_FRAGS - 1)
404 			frag_size = min(frag_size, frag_size_max);
405 
406 		info->arr[i].frag_size = frag_size;
407 		info->arr[i].frag_stride = roundup_pow_of_two(frag_size);
408 
409 		buf_size += frag_size;
410 		i++;
411 	}
412 	info->num_frags = i;
413 	/* number of different wqes sharing a page */
414 	info->wqe_bulk = 1 + (info->num_frags % 2);
415 
416 out:
417 	info->wqe_bulk = max_t(u8, info->wqe_bulk, 8);
418 	info->log_num_frags = order_base_2(info->num_frags);
419 }
420 
mlx5e_get_rqwq_log_stride(u8 wq_type,int ndsegs)421 static u8 mlx5e_get_rqwq_log_stride(u8 wq_type, int ndsegs)
422 {
423 	int sz = sizeof(struct mlx5_wqe_data_seg) * ndsegs;
424 
425 	switch (wq_type) {
426 	case MLX5_WQ_TYPE_LINKED_LIST_STRIDING_RQ:
427 		sz += sizeof(struct mlx5e_rx_wqe_ll);
428 		break;
429 	default: /* MLX5_WQ_TYPE_CYCLIC */
430 		sz += sizeof(struct mlx5e_rx_wqe_cyc);
431 	}
432 
433 	return order_base_2(sz);
434 }
435 
mlx5e_build_common_cq_param(struct mlx5_core_dev * mdev,struct mlx5e_cq_param * param)436 static void mlx5e_build_common_cq_param(struct mlx5_core_dev *mdev,
437 					struct mlx5e_cq_param *param)
438 {
439 	void *cqc = param->cqc;
440 
441 	MLX5_SET(cqc, cqc, uar_page, mdev->priv.uar->index);
442 	if (MLX5_CAP_GEN(mdev, cqe_128_always) && cache_line_size() >= 128)
443 		MLX5_SET(cqc, cqc, cqe_sz, CQE_STRIDE_128_PAD);
444 }
445 
mlx5e_build_rx_cq_param(struct mlx5_core_dev * mdev,struct mlx5e_params * params,struct mlx5e_xsk_param * xsk,struct mlx5e_cq_param * param)446 static void mlx5e_build_rx_cq_param(struct mlx5_core_dev *mdev,
447 				    struct mlx5e_params *params,
448 				    struct mlx5e_xsk_param *xsk,
449 				    struct mlx5e_cq_param *param)
450 {
451 	bool hw_stridx = false;
452 	void *cqc = param->cqc;
453 	u8 log_cq_size;
454 
455 	switch (params->rq_wq_type) {
456 	case MLX5_WQ_TYPE_LINKED_LIST_STRIDING_RQ:
457 		log_cq_size = mlx5e_mpwqe_get_log_rq_size(params, xsk) +
458 			mlx5e_mpwqe_get_log_num_strides(mdev, params, xsk);
459 		hw_stridx = MLX5_CAP_GEN(mdev, mini_cqe_resp_stride_index);
460 		break;
461 	default: /* MLX5_WQ_TYPE_CYCLIC */
462 		log_cq_size = params->log_rq_mtu_frames;
463 	}
464 
465 	MLX5_SET(cqc, cqc, log_cq_size, log_cq_size);
466 	if (MLX5E_GET_PFLAG(params, MLX5E_PFLAG_RX_CQE_COMPRESS)) {
467 		MLX5_SET(cqc, cqc, mini_cqe_res_format, hw_stridx ?
468 			 MLX5_CQE_FORMAT_CSUM_STRIDX : MLX5_CQE_FORMAT_CSUM);
469 		MLX5_SET(cqc, cqc, cqe_comp_en, 1);
470 	}
471 
472 	mlx5e_build_common_cq_param(mdev, param);
473 	param->cq_period_mode = params->rx_cq_moderation.cq_period_mode;
474 }
475 
rq_end_pad_mode(struct mlx5_core_dev * mdev,struct mlx5e_params * params)476 static u8 rq_end_pad_mode(struct mlx5_core_dev *mdev, struct mlx5e_params *params)
477 {
478 	bool lro_en = params->packet_merge.type == MLX5E_PACKET_MERGE_LRO;
479 	bool ro = pcie_relaxed_ordering_enabled(mdev->pdev) &&
480 		MLX5_CAP_GEN(mdev, relaxed_ordering_write);
481 
482 	return ro && lro_en ?
483 		MLX5_WQ_END_PAD_MODE_NONE : MLX5_WQ_END_PAD_MODE_ALIGN;
484 }
485 
mlx5e_build_rq_param(struct mlx5_core_dev * mdev,struct mlx5e_params * params,struct mlx5e_xsk_param * xsk,u16 q_counter,struct mlx5e_rq_param * param)486 int mlx5e_build_rq_param(struct mlx5_core_dev *mdev,
487 			 struct mlx5e_params *params,
488 			 struct mlx5e_xsk_param *xsk,
489 			 u16 q_counter,
490 			 struct mlx5e_rq_param *param)
491 {
492 	void *rqc = param->rqc;
493 	void *wq = MLX5_ADDR_OF(rqc, rqc, wq);
494 	int ndsegs = 1;
495 
496 	switch (params->rq_wq_type) {
497 	case MLX5_WQ_TYPE_LINKED_LIST_STRIDING_RQ: {
498 		u8 log_wqe_num_of_strides = mlx5e_mpwqe_get_log_num_strides(mdev, params, xsk);
499 		u8 log_wqe_stride_size = mlx5e_mpwqe_get_log_stride_size(mdev, params, xsk);
500 
501 		if (!mlx5e_verify_rx_mpwqe_strides(mdev, log_wqe_stride_size,
502 						   log_wqe_num_of_strides)) {
503 			mlx5_core_err(mdev,
504 				      "Bad RX MPWQE params: log_stride_size %u, log_num_strides %u\n",
505 				      log_wqe_stride_size, log_wqe_num_of_strides);
506 			return -EINVAL;
507 		}
508 
509 		MLX5_SET(wq, wq, log_wqe_num_of_strides,
510 			 log_wqe_num_of_strides - MLX5_MPWQE_LOG_NUM_STRIDES_BASE);
511 		MLX5_SET(wq, wq, log_wqe_stride_size,
512 			 log_wqe_stride_size - MLX5_MPWQE_LOG_STRIDE_SZ_BASE);
513 		MLX5_SET(wq, wq, log_wq_sz, mlx5e_mpwqe_get_log_rq_size(params, xsk));
514 		break;
515 	}
516 	default: /* MLX5_WQ_TYPE_CYCLIC */
517 		MLX5_SET(wq, wq, log_wq_sz, params->log_rq_mtu_frames);
518 		mlx5e_build_rq_frags_info(mdev, params, xsk, &param->frags_info);
519 		ndsegs = param->frags_info.num_frags;
520 	}
521 
522 	MLX5_SET(wq, wq, wq_type,          params->rq_wq_type);
523 	MLX5_SET(wq, wq, end_padding_mode, rq_end_pad_mode(mdev, params));
524 	MLX5_SET(wq, wq, log_wq_stride,
525 		 mlx5e_get_rqwq_log_stride(params->rq_wq_type, ndsegs));
526 	MLX5_SET(wq, wq, pd,               mdev->mlx5e_res.hw_objs.pdn);
527 	MLX5_SET(rqc, rqc, counter_set_id, q_counter);
528 	MLX5_SET(rqc, rqc, vsd,            params->vlan_strip_disable);
529 	MLX5_SET(rqc, rqc, scatter_fcs,    params->scatter_fcs_en);
530 
531 	param->wq.buf_numa_node = dev_to_node(mlx5_core_dma_dev(mdev));
532 	mlx5e_build_rx_cq_param(mdev, params, xsk, &param->cqp);
533 
534 	return 0;
535 }
536 
mlx5e_build_drop_rq_param(struct mlx5_core_dev * mdev,u16 q_counter,struct mlx5e_rq_param * param)537 void mlx5e_build_drop_rq_param(struct mlx5_core_dev *mdev,
538 			       u16 q_counter,
539 			       struct mlx5e_rq_param *param)
540 {
541 	void *rqc = param->rqc;
542 	void *wq = MLX5_ADDR_OF(rqc, rqc, wq);
543 
544 	MLX5_SET(wq, wq, wq_type, MLX5_WQ_TYPE_CYCLIC);
545 	MLX5_SET(wq, wq, log_wq_stride,
546 		 mlx5e_get_rqwq_log_stride(MLX5_WQ_TYPE_CYCLIC, 1));
547 	MLX5_SET(rqc, rqc, counter_set_id, q_counter);
548 
549 	param->wq.buf_numa_node = dev_to_node(mlx5_core_dma_dev(mdev));
550 }
551 
mlx5e_build_tx_cq_param(struct mlx5_core_dev * mdev,struct mlx5e_params * params,struct mlx5e_cq_param * param)552 void mlx5e_build_tx_cq_param(struct mlx5_core_dev *mdev,
553 			     struct mlx5e_params *params,
554 			     struct mlx5e_cq_param *param)
555 {
556 	void *cqc = param->cqc;
557 
558 	MLX5_SET(cqc, cqc, log_cq_size, params->log_sq_size);
559 
560 	mlx5e_build_common_cq_param(mdev, param);
561 	param->cq_period_mode = params->tx_cq_moderation.cq_period_mode;
562 }
563 
mlx5e_build_sq_param_common(struct mlx5_core_dev * mdev,struct mlx5e_sq_param * param)564 void mlx5e_build_sq_param_common(struct mlx5_core_dev *mdev,
565 				 struct mlx5e_sq_param *param)
566 {
567 	void *sqc = param->sqc;
568 	void *wq = MLX5_ADDR_OF(sqc, sqc, wq);
569 
570 	MLX5_SET(wq, wq, log_wq_stride, ilog2(MLX5_SEND_WQE_BB));
571 	MLX5_SET(wq, wq, pd,            mdev->mlx5e_res.hw_objs.pdn);
572 
573 	param->wq.buf_numa_node = dev_to_node(mlx5_core_dma_dev(mdev));
574 }
575 
mlx5e_build_sq_param(struct mlx5_core_dev * mdev,struct mlx5e_params * params,struct mlx5e_sq_param * param)576 void mlx5e_build_sq_param(struct mlx5_core_dev *mdev,
577 			  struct mlx5e_params *params,
578 			  struct mlx5e_sq_param *param)
579 {
580 	void *sqc = param->sqc;
581 	void *wq = MLX5_ADDR_OF(sqc, sqc, wq);
582 	bool allow_swp;
583 
584 	allow_swp = mlx5_geneve_tx_allowed(mdev) ||
585 		    !!MLX5_IPSEC_DEV(mdev);
586 	mlx5e_build_sq_param_common(mdev, param);
587 	MLX5_SET(wq, wq, log_wq_sz, params->log_sq_size);
588 	MLX5_SET(sqc, sqc, allow_swp, allow_swp);
589 	param->is_mpw = MLX5E_GET_PFLAG(params, MLX5E_PFLAG_SKB_TX_MPWQE);
590 	param->stop_room = mlx5e_calc_sq_stop_room(mdev, params);
591 	mlx5e_build_tx_cq_param(mdev, params, &param->cqp);
592 }
593 
mlx5e_build_ico_cq_param(struct mlx5_core_dev * mdev,u8 log_wq_size,struct mlx5e_cq_param * param)594 static void mlx5e_build_ico_cq_param(struct mlx5_core_dev *mdev,
595 				     u8 log_wq_size,
596 				     struct mlx5e_cq_param *param)
597 {
598 	void *cqc = param->cqc;
599 
600 	MLX5_SET(cqc, cqc, log_cq_size, log_wq_size);
601 
602 	mlx5e_build_common_cq_param(mdev, param);
603 
604 	param->cq_period_mode = DIM_CQ_PERIOD_MODE_START_FROM_EQE;
605 }
606 
mlx5e_get_rq_log_wq_sz(void * rqc)607 static u8 mlx5e_get_rq_log_wq_sz(void *rqc)
608 {
609 	void *wq = MLX5_ADDR_OF(rqc, rqc, wq);
610 
611 	return MLX5_GET(wq, wq, log_wq_sz);
612 }
613 
mlx5e_build_icosq_log_wq_sz(struct mlx5e_params * params,struct mlx5e_rq_param * rqp)614 static u8 mlx5e_build_icosq_log_wq_sz(struct mlx5e_params *params,
615 				      struct mlx5e_rq_param *rqp)
616 {
617 	switch (params->rq_wq_type) {
618 	case MLX5_WQ_TYPE_LINKED_LIST_STRIDING_RQ:
619 		return max_t(u8, MLX5E_PARAMS_MINIMUM_LOG_SQ_SIZE,
620 			     order_base_2(MLX5E_UMR_WQEBBS) +
621 			     mlx5e_get_rq_log_wq_sz(rqp->rqc));
622 	default: /* MLX5_WQ_TYPE_CYCLIC */
623 		return MLX5E_PARAMS_MINIMUM_LOG_SQ_SIZE;
624 	}
625 }
626 
mlx5e_build_async_icosq_log_wq_sz(struct mlx5_core_dev * mdev)627 static u8 mlx5e_build_async_icosq_log_wq_sz(struct mlx5_core_dev *mdev)
628 {
629 	if (mlx5e_accel_is_ktls_rx(mdev))
630 		return MLX5E_PARAMS_DEFAULT_LOG_SQ_SIZE;
631 
632 	return MLX5E_PARAMS_MINIMUM_LOG_SQ_SIZE;
633 }
634 
mlx5e_build_icosq_param(struct mlx5_core_dev * mdev,u8 log_wq_size,struct mlx5e_sq_param * param)635 static void mlx5e_build_icosq_param(struct mlx5_core_dev *mdev,
636 				    u8 log_wq_size,
637 				    struct mlx5e_sq_param *param)
638 {
639 	void *sqc = param->sqc;
640 	void *wq = MLX5_ADDR_OF(sqc, sqc, wq);
641 
642 	mlx5e_build_sq_param_common(mdev, param);
643 
644 	MLX5_SET(wq, wq, log_wq_sz, log_wq_size);
645 	MLX5_SET(sqc, sqc, reg_umr, MLX5_CAP_ETH(mdev, reg_umr_sq));
646 	mlx5e_build_ico_cq_param(mdev, log_wq_size, &param->cqp);
647 }
648 
mlx5e_build_async_icosq_param(struct mlx5_core_dev * mdev,u8 log_wq_size,struct mlx5e_sq_param * param)649 static void mlx5e_build_async_icosq_param(struct mlx5_core_dev *mdev,
650 					  u8 log_wq_size,
651 					  struct mlx5e_sq_param *param)
652 {
653 	void *sqc = param->sqc;
654 	void *wq = MLX5_ADDR_OF(sqc, sqc, wq);
655 
656 	mlx5e_build_sq_param_common(mdev, param);
657 	param->stop_room = mlx5e_stop_room_for_wqe(1); /* for XSK NOP */
658 	param->is_tls = mlx5e_accel_is_ktls_rx(mdev);
659 	if (param->is_tls)
660 		param->stop_room += mlx5e_stop_room_for_wqe(1); /* for TLS RX resync NOP */
661 	MLX5_SET(sqc, sqc, reg_umr, MLX5_CAP_ETH(mdev, reg_umr_sq));
662 	MLX5_SET(wq, wq, log_wq_sz, log_wq_size);
663 	mlx5e_build_ico_cq_param(mdev, log_wq_size, &param->cqp);
664 }
665 
mlx5e_build_xdpsq_param(struct mlx5_core_dev * mdev,struct mlx5e_params * params,struct mlx5e_sq_param * param)666 void mlx5e_build_xdpsq_param(struct mlx5_core_dev *mdev,
667 			     struct mlx5e_params *params,
668 			     struct mlx5e_sq_param *param)
669 {
670 	void *sqc = param->sqc;
671 	void *wq = MLX5_ADDR_OF(sqc, sqc, wq);
672 
673 	mlx5e_build_sq_param_common(mdev, param);
674 	MLX5_SET(wq, wq, log_wq_sz, params->log_sq_size);
675 	param->is_mpw = MLX5E_GET_PFLAG(params, MLX5E_PFLAG_XDP_TX_MPWQE);
676 	mlx5e_build_tx_cq_param(mdev, params, &param->cqp);
677 }
678 
mlx5e_build_channel_param(struct mlx5_core_dev * mdev,struct mlx5e_params * params,u16 q_counter,struct mlx5e_channel_param * cparam)679 int mlx5e_build_channel_param(struct mlx5_core_dev *mdev,
680 			      struct mlx5e_params *params,
681 			      u16 q_counter,
682 			      struct mlx5e_channel_param *cparam)
683 {
684 	u8 icosq_log_wq_sz, async_icosq_log_wq_sz;
685 	int err;
686 
687 	err = mlx5e_build_rq_param(mdev, params, NULL, q_counter, &cparam->rq);
688 	if (err)
689 		return err;
690 
691 	icosq_log_wq_sz = mlx5e_build_icosq_log_wq_sz(params, &cparam->rq);
692 	async_icosq_log_wq_sz = mlx5e_build_async_icosq_log_wq_sz(mdev);
693 
694 	mlx5e_build_sq_param(mdev, params, &cparam->txq_sq);
695 	mlx5e_build_xdpsq_param(mdev, params, &cparam->xdp_sq);
696 	mlx5e_build_icosq_param(mdev, icosq_log_wq_sz, &cparam->icosq);
697 	mlx5e_build_async_icosq_param(mdev, async_icosq_log_wq_sz, &cparam->async_icosq);
698 
699 	return 0;
700 }
701