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 "en_accel/ipsec.h"
9 #include <linux/dim.h>
10 #include <net/page_pool/types.h>
11 #include <net/xdp_sock_drv.h>
12
mlx5e_mpwrq_min_page_shift(struct mlx5_core_dev * mdev)13 static u8 mlx5e_mpwrq_min_page_shift(struct mlx5_core_dev *mdev)
14 {
15 u8 min_page_shift = MLX5_CAP_GEN_2(mdev, log_min_mkey_entity_size);
16
17 return min_page_shift ? : 12;
18 }
19
mlx5e_mpwrq_page_shift(struct mlx5_core_dev * mdev,struct mlx5e_xsk_param * xsk)20 u8 mlx5e_mpwrq_page_shift(struct mlx5_core_dev *mdev, struct mlx5e_xsk_param *xsk)
21 {
22 u8 req_page_shift = xsk ? order_base_2(xsk->chunk_size) : PAGE_SHIFT;
23 u8 min_page_shift = mlx5e_mpwrq_min_page_shift(mdev);
24
25 /* Regular RQ uses order-0 pages, the NIC must be able to map them. */
26 if (WARN_ON_ONCE(!xsk && req_page_shift < min_page_shift))
27 min_page_shift = req_page_shift;
28
29 return max(req_page_shift, min_page_shift);
30 }
31
32 enum mlx5e_mpwrq_umr_mode
mlx5e_mpwrq_umr_mode(struct mlx5_core_dev * mdev,struct mlx5e_xsk_param * xsk)33 mlx5e_mpwrq_umr_mode(struct mlx5_core_dev *mdev, struct mlx5e_xsk_param *xsk)
34 {
35 /* Different memory management schemes use different mechanisms to map
36 * user-mode memory. The stricter guarantees we have, the faster
37 * mechanisms we use:
38 * 1. MTT - direct mapping in page granularity.
39 * 2. KSM - indirect mapping to another MKey to arbitrary addresses, but
40 * all mappings have the same size.
41 * 3. KLM - indirect mapping to another MKey to arbitrary addresses, and
42 * mappings can have different sizes.
43 */
44 u8 page_shift = mlx5e_mpwrq_page_shift(mdev, xsk);
45 bool unaligned = xsk ? xsk->unaligned : false;
46 bool oversized = false;
47
48 if (xsk) {
49 oversized = xsk->chunk_size < (1 << page_shift);
50 WARN_ON_ONCE(xsk->chunk_size > (1 << page_shift));
51 }
52
53 /* XSK frame size doesn't match the UMR page size, either because the
54 * frame size is not a power of two, or it's smaller than the minimal
55 * page size supported by the firmware.
56 * It's possible to receive packets bigger than MTU in certain setups.
57 * To avoid writing over the XSK frame boundary, the top region of each
58 * stride is mapped to a garbage page, resulting in two mappings of
59 * different sizes per frame.
60 */
61 if (oversized) {
62 /* An optimization for frame sizes equal to 3 * power_of_two.
63 * 3 KSMs point to the frame, and one KSM points to the garbage
64 * page, which works faster than KLM.
65 */
66 if (xsk->chunk_size % 3 == 0 && is_power_of_2(xsk->chunk_size / 3))
67 return MLX5E_MPWRQ_UMR_MODE_TRIPLE;
68
69 return MLX5E_MPWRQ_UMR_MODE_OVERSIZED;
70 }
71
72 /* XSK frames can start at arbitrary unaligned locations, but they all
73 * have the same size which is a power of two. It allows to optimize to
74 * one KSM per frame.
75 */
76 if (unaligned)
77 return MLX5E_MPWRQ_UMR_MODE_UNALIGNED;
78
79 /* XSK: frames are naturally aligned, MTT can be used.
80 * Non-XSK: Allocations happen in units of CPU pages, therefore, the
81 * mappings are naturally aligned.
82 */
83 return MLX5E_MPWRQ_UMR_MODE_ALIGNED;
84 }
85
mlx5e_mpwrq_umr_entry_size(enum mlx5e_mpwrq_umr_mode mode)86 u8 mlx5e_mpwrq_umr_entry_size(enum mlx5e_mpwrq_umr_mode mode)
87 {
88 switch (mode) {
89 case MLX5E_MPWRQ_UMR_MODE_ALIGNED:
90 return sizeof(struct mlx5_mtt);
91 case MLX5E_MPWRQ_UMR_MODE_UNALIGNED:
92 return sizeof(struct mlx5_ksm);
93 case MLX5E_MPWRQ_UMR_MODE_OVERSIZED:
94 return sizeof(struct mlx5_klm) * 2;
95 case MLX5E_MPWRQ_UMR_MODE_TRIPLE:
96 return sizeof(struct mlx5_ksm) * 4;
97 }
98 WARN_ONCE(1, "MPWRQ UMR mode %d is not known\n", mode);
99 return 0;
100 }
101
mlx5e_mpwrq_log_wqe_sz(struct mlx5_core_dev * mdev,u8 page_shift,enum mlx5e_mpwrq_umr_mode umr_mode)102 u8 mlx5e_mpwrq_log_wqe_sz(struct mlx5_core_dev *mdev, u8 page_shift,
103 enum mlx5e_mpwrq_umr_mode umr_mode)
104 {
105 u8 umr_entry_size = mlx5e_mpwrq_umr_entry_size(umr_mode);
106 u8 max_pages_per_wqe, max_log_mpwqe_size;
107 u16 max_wqe_size;
108
109 /* Keep in sync with MLX5_MPWRQ_MAX_PAGES_PER_WQE. */
110 max_wqe_size = mlx5e_get_max_sq_aligned_wqebbs(mdev) * MLX5_SEND_WQE_BB;
111 max_pages_per_wqe = ALIGN_DOWN(max_wqe_size - sizeof(struct mlx5e_umr_wqe),
112 MLX5_UMR_FLEX_ALIGNMENT) / umr_entry_size;
113 max_log_mpwqe_size = ilog2(max_pages_per_wqe) + page_shift;
114
115 WARN_ON_ONCE(max_log_mpwqe_size < MLX5E_ORDER2_MAX_PACKET_MTU);
116
117 return min_t(u8, max_log_mpwqe_size, MLX5_MPWRQ_MAX_LOG_WQE_SZ);
118 }
119
mlx5e_mpwrq_pages_per_wqe(struct mlx5_core_dev * mdev,u8 page_shift,enum mlx5e_mpwrq_umr_mode umr_mode)120 u8 mlx5e_mpwrq_pages_per_wqe(struct mlx5_core_dev *mdev, u8 page_shift,
121 enum mlx5e_mpwrq_umr_mode umr_mode)
122 {
123 u8 log_wqe_sz = mlx5e_mpwrq_log_wqe_sz(mdev, page_shift, umr_mode);
124 u8 pages_per_wqe;
125
126 pages_per_wqe = log_wqe_sz > page_shift ? (1 << (log_wqe_sz - page_shift)) : 1;
127
128 /* Two MTTs are needed to form an octword. The number of MTTs is encoded
129 * in octwords in a UMR WQE, so we need at least two to avoid mapping
130 * garbage addresses.
131 */
132 if (WARN_ON_ONCE(pages_per_wqe < 2 && umr_mode == MLX5E_MPWRQ_UMR_MODE_ALIGNED))
133 pages_per_wqe = 2;
134
135 /* Sanity check for further calculations to succeed. */
136 BUILD_BUG_ON(MLX5_MPWRQ_MAX_PAGES_PER_WQE > 64);
137 if (WARN_ON_ONCE(pages_per_wqe > MLX5_MPWRQ_MAX_PAGES_PER_WQE))
138 return MLX5_MPWRQ_MAX_PAGES_PER_WQE;
139
140 return pages_per_wqe;
141 }
142
mlx5e_mpwrq_umr_wqe_sz(struct mlx5_core_dev * mdev,u8 page_shift,enum mlx5e_mpwrq_umr_mode umr_mode)143 u16 mlx5e_mpwrq_umr_wqe_sz(struct mlx5_core_dev *mdev, u8 page_shift,
144 enum mlx5e_mpwrq_umr_mode umr_mode)
145 {
146 u8 pages_per_wqe = mlx5e_mpwrq_pages_per_wqe(mdev, page_shift, umr_mode);
147 u8 umr_entry_size = mlx5e_mpwrq_umr_entry_size(umr_mode);
148 u16 umr_wqe_sz;
149
150 umr_wqe_sz = sizeof(struct mlx5e_umr_wqe) +
151 ALIGN(pages_per_wqe * umr_entry_size, MLX5_UMR_FLEX_ALIGNMENT);
152
153 WARN_ON_ONCE(DIV_ROUND_UP(umr_wqe_sz, MLX5_SEND_WQE_DS) > MLX5_WQE_CTRL_DS_MASK);
154
155 return umr_wqe_sz;
156 }
157
mlx5e_mpwrq_umr_wqebbs(struct mlx5_core_dev * mdev,u8 page_shift,enum mlx5e_mpwrq_umr_mode umr_mode)158 u8 mlx5e_mpwrq_umr_wqebbs(struct mlx5_core_dev *mdev, u8 page_shift,
159 enum mlx5e_mpwrq_umr_mode umr_mode)
160 {
161 return DIV_ROUND_UP(mlx5e_mpwrq_umr_wqe_sz(mdev, page_shift, umr_mode),
162 MLX5_SEND_WQE_BB);
163 }
164
mlx5e_mpwrq_mtts_per_wqe(struct mlx5_core_dev * mdev,u8 page_shift,enum mlx5e_mpwrq_umr_mode umr_mode)165 u8 mlx5e_mpwrq_mtts_per_wqe(struct mlx5_core_dev *mdev, u8 page_shift,
166 enum mlx5e_mpwrq_umr_mode umr_mode)
167 {
168 u8 pages_per_wqe = mlx5e_mpwrq_pages_per_wqe(mdev, page_shift, umr_mode);
169
170 /* Add another page as a buffer between WQEs. This page will absorb
171 * write overflow by the hardware, when receiving packets larger than
172 * MTU. These oversize packets are dropped by the driver at a later
173 * stage.
174 */
175 return ALIGN(pages_per_wqe + 1,
176 MLX5_SEND_WQE_BB / mlx5e_mpwrq_umr_entry_size(umr_mode));
177 }
178
mlx5e_mpwrq_max_num_entries(struct mlx5_core_dev * mdev,enum mlx5e_mpwrq_umr_mode umr_mode)179 u32 mlx5e_mpwrq_max_num_entries(struct mlx5_core_dev *mdev,
180 enum mlx5e_mpwrq_umr_mode umr_mode)
181 {
182 /* Same limits apply to KSMs and KLMs. */
183 u32 klm_limit = min(MLX5E_MAX_RQ_NUM_KSMS,
184 1 << MLX5_CAP_GEN(mdev, log_max_klm_list_size));
185
186 switch (umr_mode) {
187 case MLX5E_MPWRQ_UMR_MODE_ALIGNED:
188 return MLX5E_MAX_RQ_NUM_MTTS;
189 case MLX5E_MPWRQ_UMR_MODE_UNALIGNED:
190 return klm_limit;
191 case MLX5E_MPWRQ_UMR_MODE_OVERSIZED:
192 /* Each entry is two KLMs. */
193 return klm_limit / 2;
194 case MLX5E_MPWRQ_UMR_MODE_TRIPLE:
195 /* Each entry is four KSMs. */
196 return klm_limit / 4;
197 }
198 WARN_ONCE(1, "MPWRQ UMR mode %d is not known\n", umr_mode);
199 return 0;
200 }
201
mlx5e_mpwrq_max_log_rq_size(struct mlx5_core_dev * mdev,u8 page_shift,enum mlx5e_mpwrq_umr_mode umr_mode)202 static u8 mlx5e_mpwrq_max_log_rq_size(struct mlx5_core_dev *mdev, u8 page_shift,
203 enum mlx5e_mpwrq_umr_mode umr_mode)
204 {
205 u8 mtts_per_wqe = mlx5e_mpwrq_mtts_per_wqe(mdev, page_shift, umr_mode);
206 u32 max_entries = mlx5e_mpwrq_max_num_entries(mdev, umr_mode);
207
208 return ilog2(max_entries / mtts_per_wqe);
209 }
210
mlx5e_mpwrq_max_log_rq_pkts(struct mlx5_core_dev * mdev,u8 page_shift,enum mlx5e_mpwrq_umr_mode umr_mode)211 u8 mlx5e_mpwrq_max_log_rq_pkts(struct mlx5_core_dev *mdev, u8 page_shift,
212 enum mlx5e_mpwrq_umr_mode umr_mode)
213 {
214 return mlx5e_mpwrq_max_log_rq_size(mdev, page_shift, umr_mode) +
215 mlx5e_mpwrq_log_wqe_sz(mdev, page_shift, umr_mode) -
216 MLX5E_ORDER2_MAX_PACKET_MTU;
217 }
218
mlx5e_get_linear_rq_headroom(struct mlx5e_params * params,struct mlx5e_xsk_param * xsk)219 u16 mlx5e_get_linear_rq_headroom(struct mlx5e_params *params,
220 struct mlx5e_xsk_param *xsk)
221 {
222 u16 headroom;
223
224 if (xsk)
225 return xsk->headroom;
226
227 headroom = NET_IP_ALIGN;
228 if (params->xdp_prog)
229 headroom += XDP_PACKET_HEADROOM;
230 else
231 headroom += MLX5_RX_HEADROOM;
232
233 return headroom;
234 }
235
mlx5e_rx_get_linear_sz_xsk(struct mlx5e_params * params,struct mlx5e_xsk_param * xsk)236 static u32 mlx5e_rx_get_linear_sz_xsk(struct mlx5e_params *params,
237 struct mlx5e_xsk_param *xsk)
238 {
239 u32 hw_mtu = MLX5E_SW2HW_MTU(params, params->sw_mtu);
240
241 return xsk->headroom + hw_mtu;
242 }
243
mlx5e_rx_get_linear_sz_skb(struct mlx5e_params * params,bool no_head_tail_room)244 static u32 mlx5e_rx_get_linear_sz_skb(struct mlx5e_params *params, bool no_head_tail_room)
245 {
246 u32 hw_mtu = MLX5E_SW2HW_MTU(params, params->sw_mtu);
247 u16 headroom;
248
249 if (no_head_tail_room)
250 return SKB_DATA_ALIGN(hw_mtu);
251 headroom = mlx5e_get_linear_rq_headroom(params, NULL);
252
253 return MLX5_SKB_FRAG_SZ(headroom + hw_mtu);
254 }
255
mlx5e_rx_get_linear_stride_sz(struct mlx5_core_dev * mdev,struct mlx5e_params * params,struct mlx5e_xsk_param * xsk,bool mpwqe)256 static u32 mlx5e_rx_get_linear_stride_sz(struct mlx5_core_dev *mdev,
257 struct mlx5e_params *params,
258 struct mlx5e_xsk_param *xsk,
259 bool mpwqe)
260 {
261 bool no_head_tail_room;
262 u32 sz;
263
264 /* XSK frames are mapped as individual pages, because frames may come in
265 * an arbitrary order from random locations in the UMEM.
266 */
267 if (xsk)
268 return mpwqe ? 1 << mlx5e_mpwrq_page_shift(mdev, xsk) : PAGE_SIZE;
269
270 no_head_tail_room = params->xdp_prog && mpwqe && !mlx5e_rx_is_linear_skb(mdev, params, xsk);
271
272 /* When no_head_tail_room is set, headroom and tailroom are excluded from skb calculations.
273 * no_head_tail_room should be set in the case of XDP with Striding RQ
274 * when SKB is not linear. This is because another page is allocated for the linear part.
275 */
276 sz = roundup_pow_of_two(mlx5e_rx_get_linear_sz_skb(params, no_head_tail_room));
277
278 /* XDP in mlx5e doesn't support multiple packets per page.
279 * Do not assume sz <= PAGE_SIZE if params->xdp_prog is set.
280 */
281 return params->xdp_prog && sz < PAGE_SIZE ? PAGE_SIZE : sz;
282 }
283
mlx5e_mpwqe_log_pkts_per_wqe(struct mlx5_core_dev * mdev,struct mlx5e_params * params,struct mlx5e_xsk_param * xsk)284 static u8 mlx5e_mpwqe_log_pkts_per_wqe(struct mlx5_core_dev *mdev,
285 struct mlx5e_params *params,
286 struct mlx5e_xsk_param *xsk)
287 {
288 u32 linear_stride_sz = mlx5e_rx_get_linear_stride_sz(mdev, params, xsk, true);
289 enum mlx5e_mpwrq_umr_mode umr_mode = mlx5e_mpwrq_umr_mode(mdev, xsk);
290 u8 page_shift = mlx5e_mpwrq_page_shift(mdev, xsk);
291
292 return mlx5e_mpwrq_log_wqe_sz(mdev, page_shift, umr_mode) -
293 order_base_2(linear_stride_sz);
294 }
295
mlx5e_rx_is_linear_skb(struct mlx5_core_dev * mdev,struct mlx5e_params * params,struct mlx5e_xsk_param * xsk)296 bool mlx5e_rx_is_linear_skb(struct mlx5_core_dev *mdev,
297 struct mlx5e_params *params,
298 struct mlx5e_xsk_param *xsk)
299 {
300 if (params->packet_merge.type != MLX5E_PACKET_MERGE_NONE)
301 return false;
302
303 /* Call mlx5e_rx_get_linear_sz_skb with the no_head_tail_room parameter set
304 * to exclude headroom and tailroom from calculations.
305 * no_head_tail_room is true when SKB is built on XDP_PASS on XSK RQs
306 * since packet data buffers don't have headroom and tailroom resreved for the SKB.
307 * Both XSK and non-XSK cases allocate an SKB on XDP_PASS. Packet data
308 * must fit into a CPU page.
309 */
310 if (mlx5e_rx_get_linear_sz_skb(params, xsk) > PAGE_SIZE)
311 return false;
312
313 /* XSK frames must be big enough to hold the packet data. */
314 if (xsk && mlx5e_rx_get_linear_sz_xsk(params, xsk) > xsk->chunk_size)
315 return false;
316
317 return true;
318 }
319
mlx5e_verify_rx_mpwqe_strides(struct mlx5_core_dev * mdev,u8 log_stride_sz,u8 log_num_strides,u8 page_shift,enum mlx5e_mpwrq_umr_mode umr_mode)320 static bool mlx5e_verify_rx_mpwqe_strides(struct mlx5_core_dev *mdev,
321 u8 log_stride_sz, u8 log_num_strides,
322 u8 page_shift,
323 enum mlx5e_mpwrq_umr_mode umr_mode)
324 {
325 if (log_stride_sz + log_num_strides !=
326 mlx5e_mpwrq_log_wqe_sz(mdev, page_shift, umr_mode))
327 return false;
328
329 if (log_stride_sz < MLX5_MPWQE_LOG_STRIDE_SZ_BASE ||
330 log_stride_sz > MLX5_MPWQE_LOG_STRIDE_SZ_MAX)
331 return false;
332
333 if (log_num_strides > MLX5_MPWQE_LOG_NUM_STRIDES_MAX)
334 return false;
335
336 if (MLX5_CAP_GEN(mdev, ext_stride_num_range))
337 return log_num_strides >= MLX5_MPWQE_LOG_NUM_STRIDES_EXT_BASE;
338
339 return log_num_strides >= MLX5_MPWQE_LOG_NUM_STRIDES_BASE;
340 }
341
mlx5e_verify_params_rx_mpwqe_strides(struct mlx5_core_dev * mdev,struct mlx5e_params * params,struct mlx5e_xsk_param * xsk)342 bool mlx5e_verify_params_rx_mpwqe_strides(struct mlx5_core_dev *mdev,
343 struct mlx5e_params *params,
344 struct mlx5e_xsk_param *xsk)
345 {
346 u8 log_wqe_num_of_strides = mlx5e_mpwqe_get_log_num_strides(mdev, params, xsk);
347 u8 log_wqe_stride_size = mlx5e_mpwqe_get_log_stride_size(mdev, params, xsk);
348 enum mlx5e_mpwrq_umr_mode umr_mode = mlx5e_mpwrq_umr_mode(mdev, xsk);
349 u8 page_shift = mlx5e_mpwrq_page_shift(mdev, xsk);
350
351 return mlx5e_verify_rx_mpwqe_strides(mdev, log_wqe_stride_size,
352 log_wqe_num_of_strides,
353 page_shift, umr_mode);
354 }
355
mlx5e_rx_mpwqe_is_linear_skb(struct mlx5_core_dev * mdev,struct mlx5e_params * params,struct mlx5e_xsk_param * xsk)356 bool mlx5e_rx_mpwqe_is_linear_skb(struct mlx5_core_dev *mdev,
357 struct mlx5e_params *params,
358 struct mlx5e_xsk_param *xsk)
359 {
360 enum mlx5e_mpwrq_umr_mode umr_mode = mlx5e_mpwrq_umr_mode(mdev, xsk);
361 u8 page_shift = mlx5e_mpwrq_page_shift(mdev, xsk);
362 u8 log_num_strides;
363 u8 log_stride_sz;
364 u8 log_wqe_sz;
365
366 if (!mlx5e_rx_is_linear_skb(mdev, params, xsk))
367 return false;
368
369 log_stride_sz = order_base_2(mlx5e_rx_get_linear_stride_sz(mdev, params, xsk, true));
370 log_wqe_sz = mlx5e_mpwrq_log_wqe_sz(mdev, page_shift, umr_mode);
371
372 if (log_wqe_sz < log_stride_sz)
373 return false;
374
375 log_num_strides = log_wqe_sz - log_stride_sz;
376
377 return mlx5e_verify_rx_mpwqe_strides(mdev, log_stride_sz,
378 log_num_strides, page_shift,
379 umr_mode);
380 }
381
mlx5e_mpwqe_get_log_rq_size(struct mlx5_core_dev * mdev,struct mlx5e_params * params,struct mlx5e_xsk_param * xsk)382 u8 mlx5e_mpwqe_get_log_rq_size(struct mlx5_core_dev *mdev,
383 struct mlx5e_params *params,
384 struct mlx5e_xsk_param *xsk)
385 {
386 enum mlx5e_mpwrq_umr_mode umr_mode = mlx5e_mpwrq_umr_mode(mdev, xsk);
387 u8 log_pkts_per_wqe, page_shift, max_log_rq_size;
388
389 log_pkts_per_wqe = mlx5e_mpwqe_log_pkts_per_wqe(mdev, params, xsk);
390 page_shift = mlx5e_mpwrq_page_shift(mdev, xsk);
391 max_log_rq_size = mlx5e_mpwrq_max_log_rq_size(mdev, page_shift, umr_mode);
392
393 /* Numbers are unsigned, don't subtract to avoid underflow. */
394 if (params->log_rq_mtu_frames <
395 log_pkts_per_wqe + MLX5E_PARAMS_MINIMUM_LOG_RQ_SIZE_MPW)
396 return MLX5E_PARAMS_MINIMUM_LOG_RQ_SIZE_MPW;
397
398 /* Ethtool's rx_max_pending is calculated for regular RQ, that uses
399 * pages of PAGE_SIZE. Max length of an XSK RQ might differ if it uses a
400 * frame size not equal to PAGE_SIZE.
401 * A stricter condition is checked in mlx5e_mpwrq_validate_xsk, WARN on
402 * unexpected failure.
403 */
404 if (WARN_ON_ONCE(params->log_rq_mtu_frames > log_pkts_per_wqe + max_log_rq_size))
405 return max_log_rq_size;
406
407 return params->log_rq_mtu_frames - log_pkts_per_wqe;
408 }
409
mlx5e_shampo_get_log_hd_entry_size(struct mlx5_core_dev * mdev,struct mlx5e_params * params)410 u8 mlx5e_shampo_get_log_hd_entry_size(struct mlx5_core_dev *mdev,
411 struct mlx5e_params *params)
412 {
413 return order_base_2(DIV_ROUND_UP(MLX5E_RX_MAX_HEAD, MLX5E_SHAMPO_WQ_BASE_HEAD_ENTRY_SIZE));
414 }
415
mlx5e_shampo_get_log_rsrv_size(struct mlx5_core_dev * mdev,struct mlx5e_params * params)416 u8 mlx5e_shampo_get_log_rsrv_size(struct mlx5_core_dev *mdev,
417 struct mlx5e_params *params)
418 {
419 return order_base_2(MLX5E_SHAMPO_WQ_RESRV_SIZE / MLX5E_SHAMPO_WQ_BASE_RESRV_SIZE);
420 }
421
mlx5e_shampo_get_log_pkt_per_rsrv(struct mlx5_core_dev * mdev,struct mlx5e_params * params)422 u8 mlx5e_shampo_get_log_pkt_per_rsrv(struct mlx5_core_dev *mdev,
423 struct mlx5e_params *params)
424 {
425 u32 resrv_size = BIT(mlx5e_shampo_get_log_rsrv_size(mdev, params)) *
426 PAGE_SIZE;
427
428 return order_base_2(DIV_ROUND_UP(resrv_size, params->sw_mtu));
429 }
430
mlx5e_mpwqe_get_log_stride_size(struct mlx5_core_dev * mdev,struct mlx5e_params * params,struct mlx5e_xsk_param * xsk)431 u8 mlx5e_mpwqe_get_log_stride_size(struct mlx5_core_dev *mdev,
432 struct mlx5e_params *params,
433 struct mlx5e_xsk_param *xsk)
434 {
435 if (mlx5e_rx_mpwqe_is_linear_skb(mdev, params, xsk))
436 return order_base_2(mlx5e_rx_get_linear_stride_sz(mdev, params, xsk, true));
437
438 /* XDP in mlx5e doesn't support multiple packets per page. */
439 if (params->xdp_prog)
440 return PAGE_SHIFT;
441
442 return MLX5_MPWRQ_DEF_LOG_STRIDE_SZ(mdev);
443 }
444
mlx5e_mpwqe_get_log_num_strides(struct mlx5_core_dev * mdev,struct mlx5e_params * params,struct mlx5e_xsk_param * xsk)445 u8 mlx5e_mpwqe_get_log_num_strides(struct mlx5_core_dev *mdev,
446 struct mlx5e_params *params,
447 struct mlx5e_xsk_param *xsk)
448 {
449 enum mlx5e_mpwrq_umr_mode umr_mode = mlx5e_mpwrq_umr_mode(mdev, xsk);
450 u8 page_shift = mlx5e_mpwrq_page_shift(mdev, xsk);
451 u8 log_wqe_size, log_stride_size;
452
453 log_wqe_size = mlx5e_mpwrq_log_wqe_sz(mdev, page_shift, umr_mode);
454 log_stride_size = mlx5e_mpwqe_get_log_stride_size(mdev, params, xsk);
455 WARN(log_wqe_size < log_stride_size,
456 "Log WQE size %u < log stride size %u (page shift %u, umr mode %d, xsk on? %d)\n",
457 log_wqe_size, log_stride_size, page_shift, umr_mode, !!xsk);
458 return log_wqe_size - log_stride_size;
459 }
460
mlx5e_mpwqe_get_min_wqe_bulk(unsigned int wq_sz)461 u8 mlx5e_mpwqe_get_min_wqe_bulk(unsigned int wq_sz)
462 {
463 #define UMR_WQE_BULK (2)
464 return min_t(unsigned int, UMR_WQE_BULK, wq_sz / 2 - 1);
465 }
466
mlx5e_get_rq_headroom(struct mlx5_core_dev * mdev,struct mlx5e_params * params,struct mlx5e_xsk_param * xsk)467 u16 mlx5e_get_rq_headroom(struct mlx5_core_dev *mdev,
468 struct mlx5e_params *params,
469 struct mlx5e_xsk_param *xsk)
470 {
471 u16 linear_headroom = mlx5e_get_linear_rq_headroom(params, xsk);
472
473 if (params->rq_wq_type == MLX5_WQ_TYPE_CYCLIC)
474 return linear_headroom;
475
476 if (mlx5e_rx_mpwqe_is_linear_skb(mdev, params, xsk))
477 return linear_headroom;
478
479 if (params->packet_merge.type == MLX5E_PACKET_MERGE_SHAMPO)
480 return linear_headroom;
481
482 return 0;
483 }
484
mlx5e_calc_sq_stop_room(struct mlx5_core_dev * mdev,struct mlx5e_params * params)485 u16 mlx5e_calc_sq_stop_room(struct mlx5_core_dev *mdev, struct mlx5e_params *params)
486 {
487 bool is_mpwqe = MLX5E_GET_PFLAG(params, MLX5E_PFLAG_SKB_TX_MPWQE);
488 u16 stop_room;
489
490 stop_room = mlx5e_ktls_get_stop_room(mdev, params);
491 stop_room += mlx5e_stop_room_for_max_wqe(mdev);
492 if (is_mpwqe)
493 /* A MPWQE can take up to the maximum cacheline-aligned WQE +
494 * all the normal stop room can be taken if a new packet breaks
495 * the active MPWQE session and allocates its WQEs right away.
496 */
497 stop_room += mlx5e_stop_room_for_mpwqe(mdev);
498
499 return stop_room;
500 }
501
mlx5e_validate_params(struct mlx5_core_dev * mdev,struct mlx5e_params * params)502 int mlx5e_validate_params(struct mlx5_core_dev *mdev, struct mlx5e_params *params)
503 {
504 size_t sq_size = 1 << params->log_sq_size;
505 u16 stop_room;
506
507 stop_room = mlx5e_calc_sq_stop_room(mdev, params);
508 if (stop_room >= sq_size) {
509 mlx5_core_err(mdev, "Stop room %u is bigger than the SQ size %zu\n",
510 stop_room, sq_size);
511 return -EINVAL;
512 }
513
514 return 0;
515 }
516
slow_pci_heuristic(struct mlx5_core_dev * mdev)517 bool slow_pci_heuristic(struct mlx5_core_dev *mdev)
518 {
519 u32 link_speed = 0;
520 u32 pci_bw = 0;
521
522 mlx5_port_max_linkspeed(mdev, &link_speed);
523 pci_bw = pcie_bandwidth_available(mdev->pdev, NULL, NULL, NULL);
524 mlx5_core_dbg_once(mdev, "Max link speed = %d, PCI BW = %d\n",
525 link_speed, pci_bw);
526
527 #define MLX5E_SLOW_PCI_RATIO (2)
528
529 return link_speed && pci_bw &&
530 link_speed > MLX5E_SLOW_PCI_RATIO * pci_bw;
531 }
532
mlx5e_mpwrq_validate_regular(struct mlx5_core_dev * mdev,struct mlx5e_params * params)533 int mlx5e_mpwrq_validate_regular(struct mlx5_core_dev *mdev, struct mlx5e_params *params)
534 {
535 enum mlx5e_mpwrq_umr_mode umr_mode = mlx5e_mpwrq_umr_mode(mdev, NULL);
536 u8 page_shift = mlx5e_mpwrq_page_shift(mdev, NULL);
537
538 if (!mlx5e_check_fragmented_striding_rq_cap(mdev, page_shift, umr_mode))
539 return -EOPNOTSUPP;
540
541 return 0;
542 }
543
mlx5e_mpwrq_validate_xsk(struct mlx5_core_dev * mdev,struct mlx5e_params * params,struct mlx5e_xsk_param * xsk)544 int mlx5e_mpwrq_validate_xsk(struct mlx5_core_dev *mdev, struct mlx5e_params *params,
545 struct mlx5e_xsk_param *xsk)
546 {
547 enum mlx5e_mpwrq_umr_mode umr_mode = mlx5e_mpwrq_umr_mode(mdev, xsk);
548 u8 page_shift = mlx5e_mpwrq_page_shift(mdev, xsk);
549 u16 max_mtu_pkts;
550
551 if (!mlx5e_check_fragmented_striding_rq_cap(mdev, page_shift, umr_mode)) {
552 mlx5_core_err(mdev, "Striding RQ for XSK can't be activated with page_shift %u and umr_mode %d\n",
553 page_shift, umr_mode);
554 return -EOPNOTSUPP;
555 }
556
557 if (!mlx5e_rx_mpwqe_is_linear_skb(mdev, params, xsk)) {
558 mlx5_core_err(mdev, "Striding RQ linear mode for XSK can't be activated with current params\n");
559 return -EINVAL;
560 }
561
562 /* Current RQ length is too big for the given frame size, the
563 * needed number of WQEs exceeds the maximum.
564 */
565 max_mtu_pkts = min_t(u8, MLX5E_PARAMS_MAXIMUM_LOG_RQ_SIZE,
566 mlx5e_mpwrq_max_log_rq_pkts(mdev, page_shift, xsk->unaligned));
567 if (params->log_rq_mtu_frames > max_mtu_pkts) {
568 mlx5_core_err(mdev, "Current RQ length %d is too big for XSK with given frame size %u\n",
569 1 << params->log_rq_mtu_frames, xsk->chunk_size);
570 return -EINVAL;
571 }
572
573 return 0;
574 }
575
mlx5e_init_rq_type_params(struct mlx5_core_dev * mdev,struct mlx5e_params * params)576 void mlx5e_init_rq_type_params(struct mlx5_core_dev *mdev,
577 struct mlx5e_params *params)
578 {
579 params->log_rq_mtu_frames = is_kdump_kernel() ?
580 MLX5E_PARAMS_MINIMUM_LOG_RQ_SIZE :
581 MLX5E_PARAMS_DEFAULT_LOG_RQ_SIZE;
582 }
583
mlx5e_set_rq_type(struct mlx5_core_dev * mdev,struct mlx5e_params * params)584 void mlx5e_set_rq_type(struct mlx5_core_dev *mdev, struct mlx5e_params *params)
585 {
586 params->rq_wq_type = MLX5E_GET_PFLAG(params, MLX5E_PFLAG_RX_STRIDING_RQ) ?
587 MLX5_WQ_TYPE_LINKED_LIST_STRIDING_RQ :
588 MLX5_WQ_TYPE_CYCLIC;
589 }
590
mlx5e_build_rq_params(struct mlx5_core_dev * mdev,struct mlx5e_params * params)591 void mlx5e_build_rq_params(struct mlx5_core_dev *mdev,
592 struct mlx5e_params *params)
593 {
594 /* Prefer Striding RQ, unless any of the following holds:
595 * - Striding RQ configuration is not possible/supported.
596 * - CQE compression is ON, and stride_index mini_cqe layout is not supported.
597 * - Legacy RQ would use linear SKB while Striding RQ would use non-linear.
598 *
599 * No XSK params: checking the availability of striding RQ in general.
600 */
601 if ((!MLX5E_GET_PFLAG(params, MLX5E_PFLAG_RX_CQE_COMPRESS) ||
602 MLX5_CAP_GEN(mdev, mini_cqe_resp_stride_index)) &&
603 !mlx5e_mpwrq_validate_regular(mdev, params) &&
604 (mlx5e_rx_mpwqe_is_linear_skb(mdev, params, NULL) ||
605 !mlx5e_rx_is_linear_skb(mdev, params, NULL)))
606 MLX5E_SET_PFLAG(params, MLX5E_PFLAG_RX_STRIDING_RQ, true);
607 mlx5e_set_rq_type(mdev, params);
608 mlx5e_init_rq_type_params(mdev, params);
609 }
610
611 /* Build queue parameters */
612
mlx5e_build_create_cq_param(struct mlx5e_create_cq_param * ccp,struct mlx5e_channel * c)613 void mlx5e_build_create_cq_param(struct mlx5e_create_cq_param *ccp, struct mlx5e_channel *c)
614 {
615 *ccp = (struct mlx5e_create_cq_param) {
616 .netdev = c->netdev,
617 .wq = c->priv->wq,
618 .napi = &c->napi,
619 .ch_stats = c->stats,
620 .node = cpu_to_node(c->cpu),
621 .ix = c->vec_ix,
622 };
623 }
624
mlx5e_max_nonlinear_mtu(int first_frag_size,int frag_size,bool xdp)625 static int mlx5e_max_nonlinear_mtu(int first_frag_size, int frag_size, bool xdp)
626 {
627 if (xdp)
628 /* XDP requires all fragments to be of the same size. */
629 return first_frag_size + (MLX5E_MAX_RX_FRAGS - 1) * frag_size;
630
631 /* Optimization for small packets: the last fragment is bigger than the others. */
632 return first_frag_size + (MLX5E_MAX_RX_FRAGS - 2) * frag_size + PAGE_SIZE;
633 }
634
mlx5e_rx_compute_wqe_bulk_params(struct mlx5e_params * params,struct mlx5e_rq_frags_info * info)635 static void mlx5e_rx_compute_wqe_bulk_params(struct mlx5e_params *params,
636 struct mlx5e_rq_frags_info *info)
637 {
638 u16 bulk_bound_rq_size = (1 << params->log_rq_mtu_frames) / 4;
639 u32 bulk_bound_rq_size_in_bytes;
640 u32 sum_frag_strides = 0;
641 u32 wqe_bulk_in_bytes;
642 u16 split_factor;
643 u32 wqe_bulk;
644 int i;
645
646 for (i = 0; i < info->num_frags; i++)
647 sum_frag_strides += info->arr[i].frag_stride;
648
649 /* For MTUs larger than PAGE_SIZE, align to PAGE_SIZE to reflect
650 * amount of consumed pages per wqe in bytes.
651 */
652 if (sum_frag_strides > PAGE_SIZE)
653 sum_frag_strides = ALIGN(sum_frag_strides, PAGE_SIZE);
654
655 bulk_bound_rq_size_in_bytes = bulk_bound_rq_size * sum_frag_strides;
656
657 #define MAX_WQE_BULK_BYTES(xdp) ((xdp ? 256 : 512) * 1024)
658
659 /* A WQE bulk should not exceed min(512KB, 1/4 of rq size). For XDP
660 * keep bulk size smaller to avoid filling the page_pool cache on
661 * every bulk refill.
662 */
663 wqe_bulk_in_bytes = min_t(u32, MAX_WQE_BULK_BYTES(params->xdp_prog),
664 bulk_bound_rq_size_in_bytes);
665 wqe_bulk = DIV_ROUND_UP(wqe_bulk_in_bytes, sum_frag_strides);
666
667 /* Make sure that allocations don't start when the page is still used
668 * by older WQEs.
669 */
670 info->wqe_bulk = max_t(u16, info->wqe_index_mask + 1, wqe_bulk);
671
672 split_factor = DIV_ROUND_UP(MAX_WQE_BULK_BYTES(params->xdp_prog),
673 PP_ALLOC_CACHE_REFILL * PAGE_SIZE);
674 info->refill_unit = DIV_ROUND_UP(info->wqe_bulk, split_factor);
675 }
676
677 #define DEFAULT_FRAG_SIZE (2048)
678
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,u32 * xdp_frag_size)679 static int mlx5e_build_rq_frags_info(struct mlx5_core_dev *mdev,
680 struct mlx5e_params *params,
681 struct mlx5e_xsk_param *xsk,
682 struct mlx5e_rq_frags_info *info,
683 u32 *xdp_frag_size)
684 {
685 u32 byte_count = MLX5E_SW2HW_MTU(params, params->sw_mtu);
686 int frag_size_max = DEFAULT_FRAG_SIZE;
687 int first_frag_size_max;
688 u32 buf_size = 0;
689 u16 headroom;
690 int max_mtu;
691 int i;
692
693 if (mlx5e_rx_is_linear_skb(mdev, params, xsk)) {
694 int frag_stride;
695
696 frag_stride = mlx5e_rx_get_linear_stride_sz(mdev, params, xsk, false);
697
698 info->arr[0].frag_size = byte_count;
699 info->arr[0].frag_stride = frag_stride;
700 info->num_frags = 1;
701
702 /* N WQEs share the same page, N = PAGE_SIZE / frag_stride. The
703 * first WQE in the page is responsible for allocation of this
704 * page, this WQE's index is k*N. If WQEs [k*N+1; k*N+N-1] are
705 * still not completed, the allocation must stop before k*N.
706 */
707 info->wqe_index_mask = (PAGE_SIZE / frag_stride) - 1;
708
709 goto out;
710 }
711
712 headroom = mlx5e_get_linear_rq_headroom(params, xsk);
713 first_frag_size_max = SKB_WITH_OVERHEAD(frag_size_max - headroom);
714
715 max_mtu = mlx5e_max_nonlinear_mtu(first_frag_size_max, frag_size_max,
716 params->xdp_prog);
717 if (byte_count > max_mtu || params->xdp_prog) {
718 frag_size_max = PAGE_SIZE;
719 first_frag_size_max = SKB_WITH_OVERHEAD(frag_size_max - headroom);
720
721 max_mtu = mlx5e_max_nonlinear_mtu(first_frag_size_max, frag_size_max,
722 params->xdp_prog);
723 if (byte_count > max_mtu) {
724 mlx5_core_err(mdev, "MTU %u is too big for non-linear legacy RQ (max %d)\n",
725 params->sw_mtu, max_mtu);
726 return -EINVAL;
727 }
728 }
729
730 i = 0;
731 while (buf_size < byte_count) {
732 int frag_size = byte_count - buf_size;
733
734 if (i == 0)
735 frag_size = min(frag_size, first_frag_size_max);
736 else if (i < MLX5E_MAX_RX_FRAGS - 1)
737 frag_size = min(frag_size, frag_size_max);
738
739 info->arr[i].frag_size = frag_size;
740 buf_size += frag_size;
741
742 if (params->xdp_prog) {
743 /* XDP multi buffer expects fragments of the same size. */
744 info->arr[i].frag_stride = frag_size_max;
745 } else {
746 if (i == 0) {
747 /* Ensure that headroom and tailroom are included. */
748 frag_size += headroom;
749 frag_size += SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
750 }
751 info->arr[i].frag_stride = roundup_pow_of_two(frag_size);
752 }
753
754 i++;
755 }
756 info->num_frags = i;
757
758 /* The last fragment of WQE with index 2*N may share the page with the
759 * first fragment of WQE with index 2*N+1 in certain cases. If WQE 2*N+1
760 * is not completed yet, WQE 2*N must not be allocated, as it's
761 * responsible for allocating a new page.
762 */
763 if (frag_size_max == PAGE_SIZE) {
764 /* No WQE can start in the middle of a page. */
765 info->wqe_index_mask = 0;
766 } else {
767 /* PAGE_SIZEs starting from 8192 don't use 2K-sized fragments,
768 * because there would be more than MLX5E_MAX_RX_FRAGS of them.
769 */
770 WARN_ON(PAGE_SIZE != 2 * DEFAULT_FRAG_SIZE);
771
772 /* Odd number of fragments allows to pack the last fragment of
773 * the previous WQE and the first fragment of the next WQE into
774 * the same page.
775 * As long as DEFAULT_FRAG_SIZE is 2048, and MLX5E_MAX_RX_FRAGS
776 * is 4, the last fragment can be bigger than the rest only if
777 * it's the fourth one, so WQEs consisting of 3 fragments will
778 * always share a page.
779 * When a page is shared, WQE bulk size is 2, otherwise just 1.
780 */
781 info->wqe_index_mask = info->num_frags % 2;
782 }
783
784 out:
785 /* Bulking optimization to skip allocation until a large enough number
786 * of WQEs can be allocated in a row. Bulking also influences how well
787 * deferred page release works.
788 */
789 mlx5e_rx_compute_wqe_bulk_params(params, info);
790
791 mlx5_core_dbg(mdev, "%s: wqe_bulk = %u, wqe_bulk_refill_unit = %u\n",
792 __func__, info->wqe_bulk, info->refill_unit);
793
794 info->log_num_frags = order_base_2(info->num_frags);
795
796 *xdp_frag_size = info->num_frags > 1 && params->xdp_prog ? PAGE_SIZE : 0;
797
798 return 0;
799 }
800
mlx5e_get_rqwq_log_stride(u8 wq_type,int ndsegs)801 static u8 mlx5e_get_rqwq_log_stride(u8 wq_type, int ndsegs)
802 {
803 int sz = sizeof(struct mlx5_wqe_data_seg) * ndsegs;
804
805 switch (wq_type) {
806 case MLX5_WQ_TYPE_LINKED_LIST_STRIDING_RQ:
807 sz += sizeof(struct mlx5e_rx_wqe_ll);
808 break;
809 default: /* MLX5_WQ_TYPE_CYCLIC */
810 sz += sizeof(struct mlx5e_rx_wqe_cyc);
811 }
812
813 return order_base_2(sz);
814 }
815
mlx5e_build_common_cq_param(struct mlx5_core_dev * mdev,struct mlx5e_cq_param * param)816 static void mlx5e_build_common_cq_param(struct mlx5_core_dev *mdev,
817 struct mlx5e_cq_param *param)
818 {
819 void *cqc = param->cqc;
820
821 MLX5_SET(cqc, cqc, uar_page, mdev->priv.uar->index);
822 if (MLX5_CAP_GEN(mdev, cqe_128_always) && cache_line_size() >= 128)
823 MLX5_SET(cqc, cqc, cqe_sz, CQE_STRIDE_128_PAD);
824 }
825
mlx5e_shampo_get_log_cq_size(struct mlx5_core_dev * mdev,struct mlx5e_params * params,struct mlx5e_xsk_param * xsk)826 static u32 mlx5e_shampo_get_log_cq_size(struct mlx5_core_dev *mdev,
827 struct mlx5e_params *params,
828 struct mlx5e_xsk_param *xsk)
829 {
830 int rsrv_size = BIT(mlx5e_shampo_get_log_rsrv_size(mdev, params)) * PAGE_SIZE;
831 u16 num_strides = BIT(mlx5e_mpwqe_get_log_num_strides(mdev, params, xsk));
832 int pkt_per_rsrv = BIT(mlx5e_shampo_get_log_pkt_per_rsrv(mdev, params));
833 u8 log_stride_sz = mlx5e_mpwqe_get_log_stride_size(mdev, params, xsk);
834 int wq_size = BIT(mlx5e_mpwqe_get_log_rq_size(mdev, params, xsk));
835 int wqe_size = BIT(log_stride_sz) * num_strides;
836
837 /* +1 is for the case that the pkt_per_rsrv dont consume the reservation
838 * so we get a filler cqe for the rest of the reservation.
839 */
840 return order_base_2((wqe_size / rsrv_size) * wq_size * (pkt_per_rsrv + 1));
841 }
842
mlx5e_build_rx_cq_param(struct mlx5_core_dev * mdev,struct mlx5e_params * params,struct mlx5e_xsk_param * xsk,struct mlx5e_cq_param * param)843 static void mlx5e_build_rx_cq_param(struct mlx5_core_dev *mdev,
844 struct mlx5e_params *params,
845 struct mlx5e_xsk_param *xsk,
846 struct mlx5e_cq_param *param)
847 {
848 bool hw_stridx = false;
849 void *cqc = param->cqc;
850 u8 log_cq_size;
851
852 switch (params->rq_wq_type) {
853 case MLX5_WQ_TYPE_LINKED_LIST_STRIDING_RQ:
854 hw_stridx = MLX5_CAP_GEN(mdev, mini_cqe_resp_stride_index);
855 if (params->packet_merge.type == MLX5E_PACKET_MERGE_SHAMPO)
856 log_cq_size = mlx5e_shampo_get_log_cq_size(mdev, params, xsk);
857 else
858 log_cq_size = mlx5e_mpwqe_get_log_rq_size(mdev, params, xsk) +
859 mlx5e_mpwqe_get_log_num_strides(mdev, params, xsk);
860 break;
861 default: /* MLX5_WQ_TYPE_CYCLIC */
862 log_cq_size = params->log_rq_mtu_frames;
863 }
864
865 MLX5_SET(cqc, cqc, log_cq_size, log_cq_size);
866 if (MLX5E_GET_PFLAG(params, MLX5E_PFLAG_RX_CQE_COMPRESS)) {
867 MLX5_SET(cqc, cqc, mini_cqe_res_format, hw_stridx ?
868 MLX5_CQE_FORMAT_CSUM_STRIDX : MLX5_CQE_FORMAT_CSUM);
869 MLX5_SET(cqc, cqc, cqe_compression_layout,
870 MLX5_CAP_GEN(mdev, enhanced_cqe_compression) ?
871 MLX5_CQE_COMPRESS_LAYOUT_ENHANCED :
872 MLX5_CQE_COMPRESS_LAYOUT_BASIC);
873 MLX5_SET(cqc, cqc, cqe_comp_en, 1);
874 }
875
876 mlx5e_build_common_cq_param(mdev, param);
877 param->cq_period_mode = params->rx_cq_moderation.cq_period_mode;
878 }
879
rq_end_pad_mode(struct mlx5_core_dev * mdev,struct mlx5e_params * params)880 static u8 rq_end_pad_mode(struct mlx5_core_dev *mdev, struct mlx5e_params *params)
881 {
882 bool lro_en = params->packet_merge.type == MLX5E_PACKET_MERGE_LRO;
883 bool ro = MLX5_CAP_GEN(mdev, relaxed_ordering_write);
884
885 return ro && lro_en ?
886 MLX5_WQ_END_PAD_MODE_NONE : MLX5_WQ_END_PAD_MODE_ALIGN;
887 }
888
mlx5e_build_rq_param(struct mlx5_core_dev * mdev,struct mlx5e_params * params,struct mlx5e_xsk_param * xsk,struct mlx5e_rq_param * param)889 int mlx5e_build_rq_param(struct mlx5_core_dev *mdev,
890 struct mlx5e_params *params,
891 struct mlx5e_xsk_param *xsk,
892 struct mlx5e_rq_param *param)
893 {
894 void *rqc = param->rqc;
895 void *wq = MLX5_ADDR_OF(rqc, rqc, wq);
896 int ndsegs = 1;
897 int err;
898
899 switch (params->rq_wq_type) {
900 case MLX5_WQ_TYPE_LINKED_LIST_STRIDING_RQ: {
901 u8 log_wqe_num_of_strides = mlx5e_mpwqe_get_log_num_strides(mdev, params, xsk);
902 u8 log_wqe_stride_size = mlx5e_mpwqe_get_log_stride_size(mdev, params, xsk);
903 enum mlx5e_mpwrq_umr_mode umr_mode = mlx5e_mpwrq_umr_mode(mdev, xsk);
904 u8 page_shift = mlx5e_mpwrq_page_shift(mdev, xsk);
905
906 if (!mlx5e_verify_rx_mpwqe_strides(mdev, log_wqe_stride_size,
907 log_wqe_num_of_strides,
908 page_shift, umr_mode)) {
909 mlx5_core_err(mdev,
910 "Bad RX MPWQE params: log_stride_size %u, log_num_strides %u, umr_mode %d\n",
911 log_wqe_stride_size, log_wqe_num_of_strides,
912 umr_mode);
913 return -EINVAL;
914 }
915
916 MLX5_SET(wq, wq, log_wqe_num_of_strides,
917 log_wqe_num_of_strides - MLX5_MPWQE_LOG_NUM_STRIDES_BASE);
918 MLX5_SET(wq, wq, log_wqe_stride_size,
919 log_wqe_stride_size - MLX5_MPWQE_LOG_STRIDE_SZ_BASE);
920 MLX5_SET(wq, wq, log_wq_sz, mlx5e_mpwqe_get_log_rq_size(mdev, params, xsk));
921 if (params->packet_merge.type == MLX5E_PACKET_MERGE_SHAMPO) {
922 MLX5_SET(wq, wq, shampo_enable, true);
923 MLX5_SET(wq, wq, log_reservation_size,
924 mlx5e_shampo_get_log_rsrv_size(mdev, params));
925 MLX5_SET(wq, wq,
926 log_max_num_of_packets_per_reservation,
927 mlx5e_shampo_get_log_pkt_per_rsrv(mdev, params));
928 MLX5_SET(wq, wq, log_headers_entry_size,
929 mlx5e_shampo_get_log_hd_entry_size(mdev, params));
930 MLX5_SET(rqc, rqc, reservation_timeout,
931 mlx5e_choose_lro_timeout(mdev, MLX5E_DEFAULT_SHAMPO_TIMEOUT));
932 MLX5_SET(rqc, rqc, shampo_match_criteria_type,
933 params->packet_merge.shampo.match_criteria_type);
934 MLX5_SET(rqc, rqc, shampo_no_match_alignment_granularity,
935 params->packet_merge.shampo.alignment_granularity);
936 }
937 break;
938 }
939 default: /* MLX5_WQ_TYPE_CYCLIC */
940 MLX5_SET(wq, wq, log_wq_sz, params->log_rq_mtu_frames);
941 err = mlx5e_build_rq_frags_info(mdev, params, xsk, ¶m->frags_info,
942 ¶m->xdp_frag_size);
943 if (err)
944 return err;
945 ndsegs = param->frags_info.num_frags;
946 }
947
948 MLX5_SET(wq, wq, wq_type, params->rq_wq_type);
949 MLX5_SET(wq, wq, end_padding_mode, rq_end_pad_mode(mdev, params));
950 MLX5_SET(wq, wq, log_wq_stride,
951 mlx5e_get_rqwq_log_stride(params->rq_wq_type, ndsegs));
952 MLX5_SET(wq, wq, pd, mdev->mlx5e_res.hw_objs.pdn);
953 MLX5_SET(rqc, rqc, vsd, params->vlan_strip_disable);
954 MLX5_SET(rqc, rqc, scatter_fcs, params->scatter_fcs_en);
955
956 param->wq.buf_numa_node = dev_to_node(mlx5_core_dma_dev(mdev));
957 mlx5e_build_rx_cq_param(mdev, params, xsk, ¶m->cqp);
958
959 return 0;
960 }
961
mlx5e_build_drop_rq_param(struct mlx5_core_dev * mdev,struct mlx5e_rq_param * param)962 void mlx5e_build_drop_rq_param(struct mlx5_core_dev *mdev,
963 struct mlx5e_rq_param *param)
964 {
965 void *rqc = param->rqc;
966 void *wq = MLX5_ADDR_OF(rqc, rqc, wq);
967
968 MLX5_SET(wq, wq, wq_type, MLX5_WQ_TYPE_CYCLIC);
969 MLX5_SET(wq, wq, log_wq_stride,
970 mlx5e_get_rqwq_log_stride(MLX5_WQ_TYPE_CYCLIC, 1));
971
972 param->wq.buf_numa_node = dev_to_node(mlx5_core_dma_dev(mdev));
973 }
974
mlx5e_build_tx_cq_param(struct mlx5_core_dev * mdev,struct mlx5e_params * params,struct mlx5e_cq_param * param)975 void mlx5e_build_tx_cq_param(struct mlx5_core_dev *mdev,
976 struct mlx5e_params *params,
977 struct mlx5e_cq_param *param)
978 {
979 void *cqc = param->cqc;
980
981 MLX5_SET(cqc, cqc, log_cq_size, params->log_sq_size);
982
983 mlx5e_build_common_cq_param(mdev, param);
984 param->cq_period_mode = params->tx_cq_moderation.cq_period_mode;
985 }
986
mlx5e_build_sq_param_common(struct mlx5_core_dev * mdev,struct mlx5e_sq_param * param)987 void mlx5e_build_sq_param_common(struct mlx5_core_dev *mdev,
988 struct mlx5e_sq_param *param)
989 {
990 void *sqc = param->sqc;
991 void *wq = MLX5_ADDR_OF(sqc, sqc, wq);
992
993 MLX5_SET(wq, wq, log_wq_stride, ilog2(MLX5_SEND_WQE_BB));
994 MLX5_SET(wq, wq, pd, mdev->mlx5e_res.hw_objs.pdn);
995
996 param->wq.buf_numa_node = dev_to_node(mlx5_core_dma_dev(mdev));
997 }
998
mlx5e_build_sq_param(struct mlx5_core_dev * mdev,struct mlx5e_params * params,struct mlx5e_sq_param * param)999 void mlx5e_build_sq_param(struct mlx5_core_dev *mdev,
1000 struct mlx5e_params *params,
1001 struct mlx5e_sq_param *param)
1002 {
1003 void *sqc = param->sqc;
1004 void *wq = MLX5_ADDR_OF(sqc, sqc, wq);
1005 bool allow_swp;
1006
1007 allow_swp = mlx5_geneve_tx_allowed(mdev) ||
1008 (mlx5_ipsec_device_caps(mdev) & MLX5_IPSEC_CAP_CRYPTO);
1009 mlx5e_build_sq_param_common(mdev, param);
1010 MLX5_SET(wq, wq, log_wq_sz, params->log_sq_size);
1011 MLX5_SET(sqc, sqc, allow_swp, allow_swp);
1012 param->is_mpw = MLX5E_GET_PFLAG(params, MLX5E_PFLAG_SKB_TX_MPWQE);
1013 param->stop_room = mlx5e_calc_sq_stop_room(mdev, params);
1014 mlx5e_build_tx_cq_param(mdev, params, ¶m->cqp);
1015 }
1016
mlx5e_build_ico_cq_param(struct mlx5_core_dev * mdev,u8 log_wq_size,struct mlx5e_cq_param * param)1017 static void mlx5e_build_ico_cq_param(struct mlx5_core_dev *mdev,
1018 u8 log_wq_size,
1019 struct mlx5e_cq_param *param)
1020 {
1021 void *cqc = param->cqc;
1022
1023 MLX5_SET(cqc, cqc, log_cq_size, log_wq_size);
1024
1025 mlx5e_build_common_cq_param(mdev, param);
1026
1027 param->cq_period_mode = DIM_CQ_PERIOD_MODE_START_FROM_EQE;
1028 }
1029
1030 /* This function calculates the maximum number of headers entries that are needed
1031 * per WQE, the formula is based on the size of the reservations and the
1032 * restriction we have about max packets for reservation that is equal to max
1033 * headers per reservation.
1034 */
mlx5e_shampo_hd_per_wqe(struct mlx5_core_dev * mdev,struct mlx5e_params * params,struct mlx5e_rq_param * rq_param)1035 u32 mlx5e_shampo_hd_per_wqe(struct mlx5_core_dev *mdev,
1036 struct mlx5e_params *params,
1037 struct mlx5e_rq_param *rq_param)
1038 {
1039 int resv_size = BIT(mlx5e_shampo_get_log_rsrv_size(mdev, params)) * PAGE_SIZE;
1040 u16 num_strides = BIT(mlx5e_mpwqe_get_log_num_strides(mdev, params, NULL));
1041 int pkt_per_resv = BIT(mlx5e_shampo_get_log_pkt_per_rsrv(mdev, params));
1042 u8 log_stride_sz = mlx5e_mpwqe_get_log_stride_size(mdev, params, NULL);
1043 int wqe_size = BIT(log_stride_sz) * num_strides;
1044 u32 hd_per_wqe;
1045
1046 /* Assumption: hd_per_wqe % 8 == 0. */
1047 hd_per_wqe = (wqe_size / resv_size) * pkt_per_resv;
1048 mlx5_core_dbg(mdev, "%s hd_per_wqe = %d rsrv_size = %d wqe_size = %d pkt_per_resv = %d\n",
1049 __func__, hd_per_wqe, resv_size, wqe_size, pkt_per_resv);
1050 return hd_per_wqe;
1051 }
1052
1053 /* This function calculates the maximum number of headers entries that are needed
1054 * for the WQ, this value is uesed to allocate the header buffer in HW, thus
1055 * must be a pow of 2.
1056 */
mlx5e_shampo_hd_per_wq(struct mlx5_core_dev * mdev,struct mlx5e_params * params,struct mlx5e_rq_param * rq_param)1057 u32 mlx5e_shampo_hd_per_wq(struct mlx5_core_dev *mdev,
1058 struct mlx5e_params *params,
1059 struct mlx5e_rq_param *rq_param)
1060 {
1061 void *wqc = MLX5_ADDR_OF(rqc, rq_param->rqc, wq);
1062 int wq_size = BIT(MLX5_GET(wq, wqc, log_wq_sz));
1063 u32 hd_per_wqe, hd_per_wq;
1064
1065 hd_per_wqe = mlx5e_shampo_hd_per_wqe(mdev, params, rq_param);
1066 hd_per_wq = roundup_pow_of_two(hd_per_wqe * wq_size);
1067 return hd_per_wq;
1068 }
1069
mlx5e_shampo_icosq_sz(struct mlx5_core_dev * mdev,struct mlx5e_params * params,struct mlx5e_rq_param * rq_param)1070 static u32 mlx5e_shampo_icosq_sz(struct mlx5_core_dev *mdev,
1071 struct mlx5e_params *params,
1072 struct mlx5e_rq_param *rq_param)
1073 {
1074 int max_num_of_umr_per_wqe, max_hd_per_wqe, max_ksm_per_umr, rest;
1075 void *wqc = MLX5_ADDR_OF(rqc, rq_param->rqc, wq);
1076 int wq_size = BIT(MLX5_GET(wq, wqc, log_wq_sz));
1077 u32 wqebbs;
1078
1079 max_ksm_per_umr = MLX5E_MAX_KSM_PER_WQE(mdev);
1080 max_hd_per_wqe = mlx5e_shampo_hd_per_wqe(mdev, params, rq_param);
1081 max_num_of_umr_per_wqe = max_hd_per_wqe / max_ksm_per_umr;
1082 rest = max_hd_per_wqe % max_ksm_per_umr;
1083 wqebbs = MLX5E_KSM_UMR_WQEBBS(max_ksm_per_umr) * max_num_of_umr_per_wqe;
1084 if (rest)
1085 wqebbs += MLX5E_KSM_UMR_WQEBBS(rest);
1086 wqebbs *= wq_size;
1087 return wqebbs;
1088 }
1089
1090 #define MLX5E_LRO_TIMEOUT_ARR_SIZE 4
1091
mlx5e_choose_lro_timeout(struct mlx5_core_dev * mdev,u32 wanted_timeout)1092 u32 mlx5e_choose_lro_timeout(struct mlx5_core_dev *mdev, u32 wanted_timeout)
1093 {
1094 int i;
1095
1096 /* The supported periods are organized in ascending order */
1097 for (i = 0; i < MLX5E_LRO_TIMEOUT_ARR_SIZE - 1; i++)
1098 if (MLX5_CAP_ETH(mdev, lro_timer_supported_periods[i]) >= wanted_timeout)
1099 break;
1100
1101 return MLX5_CAP_ETH(mdev, lro_timer_supported_periods[i]);
1102 }
1103
mlx5e_mpwrq_total_umr_wqebbs(struct mlx5_core_dev * mdev,struct mlx5e_params * params,struct mlx5e_xsk_param * xsk)1104 static u32 mlx5e_mpwrq_total_umr_wqebbs(struct mlx5_core_dev *mdev,
1105 struct mlx5e_params *params,
1106 struct mlx5e_xsk_param *xsk)
1107 {
1108 enum mlx5e_mpwrq_umr_mode umr_mode = mlx5e_mpwrq_umr_mode(mdev, xsk);
1109 u8 page_shift = mlx5e_mpwrq_page_shift(mdev, xsk);
1110 u8 umr_wqebbs;
1111
1112 umr_wqebbs = mlx5e_mpwrq_umr_wqebbs(mdev, page_shift, umr_mode);
1113
1114 return umr_wqebbs * (1 << mlx5e_mpwqe_get_log_rq_size(mdev, params, xsk));
1115 }
1116
mlx5e_build_icosq_log_wq_sz(struct mlx5_core_dev * mdev,struct mlx5e_params * params,struct mlx5e_rq_param * rqp)1117 static u8 mlx5e_build_icosq_log_wq_sz(struct mlx5_core_dev *mdev,
1118 struct mlx5e_params *params,
1119 struct mlx5e_rq_param *rqp)
1120 {
1121 u32 wqebbs, total_pages, useful_space;
1122
1123 /* MLX5_WQ_TYPE_CYCLIC */
1124 if (params->rq_wq_type != MLX5_WQ_TYPE_LINKED_LIST_STRIDING_RQ)
1125 return MLX5E_PARAMS_MINIMUM_LOG_SQ_SIZE;
1126
1127 /* UMR WQEs for the regular RQ. */
1128 wqebbs = mlx5e_mpwrq_total_umr_wqebbs(mdev, params, NULL);
1129
1130 /* If XDP program is attached, XSK may be turned on at any time without
1131 * restarting the channel. ICOSQ must be big enough to fit UMR WQEs of
1132 * both regular RQ and XSK RQ.
1133 *
1134 * XSK uses different values of page_shift, and the total number of UMR
1135 * WQEBBs depends on it. This dependency is complex and not monotonic,
1136 * especially taking into consideration that some of the parameters come
1137 * from capabilities. Hence, we have to try all valid values of XSK
1138 * frame size (and page_shift) to find the maximum.
1139 */
1140 if (params->xdp_prog) {
1141 u32 max_xsk_wqebbs = 0;
1142 u8 frame_shift;
1143
1144 for (frame_shift = XDP_UMEM_MIN_CHUNK_SHIFT;
1145 frame_shift <= PAGE_SHIFT; frame_shift++) {
1146 /* The headroom doesn't affect the calculation. */
1147 struct mlx5e_xsk_param xsk = {
1148 .chunk_size = 1 << frame_shift,
1149 .unaligned = false,
1150 };
1151
1152 /* XSK aligned mode. */
1153 max_xsk_wqebbs = max(max_xsk_wqebbs,
1154 mlx5e_mpwrq_total_umr_wqebbs(mdev, params, &xsk));
1155
1156 /* XSK unaligned mode, frame size is a power of two. */
1157 xsk.unaligned = true;
1158 max_xsk_wqebbs = max(max_xsk_wqebbs,
1159 mlx5e_mpwrq_total_umr_wqebbs(mdev, params, &xsk));
1160
1161 /* XSK unaligned mode, frame size is not equal to stride size. */
1162 xsk.chunk_size -= 1;
1163 max_xsk_wqebbs = max(max_xsk_wqebbs,
1164 mlx5e_mpwrq_total_umr_wqebbs(mdev, params, &xsk));
1165
1166 /* XSK unaligned mode, frame size is a triple power of two. */
1167 xsk.chunk_size = (1 << frame_shift) / 4 * 3;
1168 max_xsk_wqebbs = max(max_xsk_wqebbs,
1169 mlx5e_mpwrq_total_umr_wqebbs(mdev, params, &xsk));
1170 }
1171
1172 wqebbs += max_xsk_wqebbs;
1173 }
1174
1175 if (params->packet_merge.type == MLX5E_PACKET_MERGE_SHAMPO)
1176 wqebbs += mlx5e_shampo_icosq_sz(mdev, params, rqp);
1177
1178 /* UMR WQEs don't cross the page boundary, they are padded with NOPs.
1179 * This padding is always smaller than the max WQE size. That gives us
1180 * at least (PAGE_SIZE - (max WQE size - MLX5_SEND_WQE_BB)) useful bytes
1181 * per page. The number of pages is estimated as the total size of WQEs
1182 * divided by the useful space in page, rounding up. If some WQEs don't
1183 * fully fit into the useful space, they can occupy part of the padding,
1184 * which proves this estimation to be correct (reserve enough space).
1185 */
1186 useful_space = PAGE_SIZE - mlx5e_get_max_sq_wqebbs(mdev) + MLX5_SEND_WQE_BB;
1187 total_pages = DIV_ROUND_UP(wqebbs * MLX5_SEND_WQE_BB, useful_space);
1188 wqebbs = total_pages * (PAGE_SIZE / MLX5_SEND_WQE_BB);
1189
1190 return max_t(u8, MLX5E_PARAMS_MINIMUM_LOG_SQ_SIZE, order_base_2(wqebbs));
1191 }
1192
mlx5e_build_async_icosq_log_wq_sz(struct mlx5_core_dev * mdev)1193 static u8 mlx5e_build_async_icosq_log_wq_sz(struct mlx5_core_dev *mdev)
1194 {
1195 if (mlx5e_is_ktls_rx(mdev))
1196 return MLX5E_PARAMS_DEFAULT_LOG_SQ_SIZE;
1197
1198 return MLX5E_PARAMS_MINIMUM_LOG_SQ_SIZE;
1199 }
1200
mlx5e_build_icosq_param(struct mlx5_core_dev * mdev,u8 log_wq_size,struct mlx5e_sq_param * param)1201 static void mlx5e_build_icosq_param(struct mlx5_core_dev *mdev,
1202 u8 log_wq_size,
1203 struct mlx5e_sq_param *param)
1204 {
1205 void *sqc = param->sqc;
1206 void *wq = MLX5_ADDR_OF(sqc, sqc, wq);
1207
1208 mlx5e_build_sq_param_common(mdev, param);
1209
1210 MLX5_SET(wq, wq, log_wq_sz, log_wq_size);
1211 MLX5_SET(sqc, sqc, reg_umr, MLX5_CAP_ETH(mdev, reg_umr_sq));
1212 mlx5e_build_ico_cq_param(mdev, log_wq_size, ¶m->cqp);
1213 }
1214
mlx5e_build_async_icosq_param(struct mlx5_core_dev * mdev,u8 log_wq_size,struct mlx5e_sq_param * param)1215 static void mlx5e_build_async_icosq_param(struct mlx5_core_dev *mdev,
1216 u8 log_wq_size,
1217 struct mlx5e_sq_param *param)
1218 {
1219 void *sqc = param->sqc;
1220 void *wq = MLX5_ADDR_OF(sqc, sqc, wq);
1221
1222 mlx5e_build_sq_param_common(mdev, param);
1223 param->stop_room = mlx5e_stop_room_for_wqe(mdev, 1); /* for XSK NOP */
1224 param->is_tls = mlx5e_is_ktls_rx(mdev);
1225 if (param->is_tls)
1226 param->stop_room += mlx5e_stop_room_for_wqe(mdev, 1); /* for TLS RX resync NOP */
1227 MLX5_SET(sqc, sqc, reg_umr, MLX5_CAP_ETH(mdev, reg_umr_sq));
1228 MLX5_SET(wq, wq, log_wq_sz, log_wq_size);
1229 mlx5e_build_ico_cq_param(mdev, log_wq_size, ¶m->cqp);
1230 }
1231
mlx5e_build_xdpsq_param(struct mlx5_core_dev * mdev,struct mlx5e_params * params,struct mlx5e_xsk_param * xsk,struct mlx5e_sq_param * param)1232 void mlx5e_build_xdpsq_param(struct mlx5_core_dev *mdev,
1233 struct mlx5e_params *params,
1234 struct mlx5e_xsk_param *xsk,
1235 struct mlx5e_sq_param *param)
1236 {
1237 void *sqc = param->sqc;
1238 void *wq = MLX5_ADDR_OF(sqc, sqc, wq);
1239
1240 mlx5e_build_sq_param_common(mdev, param);
1241 MLX5_SET(wq, wq, log_wq_sz, params->log_sq_size);
1242 param->is_mpw = MLX5E_GET_PFLAG(params, MLX5E_PFLAG_XDP_TX_MPWQE);
1243 param->is_xdp_mb = !mlx5e_rx_is_linear_skb(mdev, params, xsk);
1244 mlx5e_build_tx_cq_param(mdev, params, ¶m->cqp);
1245 }
1246
mlx5e_build_channel_param(struct mlx5_core_dev * mdev,struct mlx5e_params * params,struct mlx5e_channel_param * cparam)1247 int mlx5e_build_channel_param(struct mlx5_core_dev *mdev,
1248 struct mlx5e_params *params,
1249 struct mlx5e_channel_param *cparam)
1250 {
1251 u8 icosq_log_wq_sz, async_icosq_log_wq_sz;
1252 int err;
1253
1254 err = mlx5e_build_rq_param(mdev, params, NULL, &cparam->rq);
1255 if (err)
1256 return err;
1257
1258 icosq_log_wq_sz = mlx5e_build_icosq_log_wq_sz(mdev, params, &cparam->rq);
1259 async_icosq_log_wq_sz = mlx5e_build_async_icosq_log_wq_sz(mdev);
1260
1261 mlx5e_build_sq_param(mdev, params, &cparam->txq_sq);
1262 mlx5e_build_xdpsq_param(mdev, params, NULL, &cparam->xdp_sq);
1263 mlx5e_build_icosq_param(mdev, icosq_log_wq_sz, &cparam->icosq);
1264 mlx5e_build_async_icosq_param(mdev, async_icosq_log_wq_sz, &cparam->async_icosq);
1265
1266 return 0;
1267 }
1268