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 MLX5E_SHAMPO_WQ_BASE_RESRV_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)) *
831 MLX5E_SHAMPO_WQ_BASE_RESRV_SIZE;
832 u16 num_strides = BIT(mlx5e_mpwqe_get_log_num_strides(mdev, params, xsk));
833 int pkt_per_rsrv = BIT(mlx5e_shampo_get_log_pkt_per_rsrv(mdev, params));
834 u8 log_stride_sz = mlx5e_mpwqe_get_log_stride_size(mdev, params, xsk);
835 int wq_size = BIT(mlx5e_mpwqe_get_log_rq_size(mdev, params, xsk));
836 int wqe_size = BIT(log_stride_sz) * num_strides;
837
838 /* +1 is for the case that the pkt_per_rsrv dont consume the reservation
839 * so we get a filler cqe for the rest of the reservation.
840 */
841 return order_base_2((wqe_size / rsrv_size) * wq_size * (pkt_per_rsrv + 1));
842 }
843
mlx5e_build_rx_cq_param(struct mlx5_core_dev * mdev,struct mlx5e_params * params,struct mlx5e_xsk_param * xsk,struct mlx5e_cq_param * param)844 static void mlx5e_build_rx_cq_param(struct mlx5_core_dev *mdev,
845 struct mlx5e_params *params,
846 struct mlx5e_xsk_param *xsk,
847 struct mlx5e_cq_param *param)
848 {
849 bool hw_stridx = false;
850 void *cqc = param->cqc;
851 u8 log_cq_size;
852
853 switch (params->rq_wq_type) {
854 case MLX5_WQ_TYPE_LINKED_LIST_STRIDING_RQ:
855 hw_stridx = MLX5_CAP_GEN(mdev, mini_cqe_resp_stride_index);
856 if (params->packet_merge.type == MLX5E_PACKET_MERGE_SHAMPO)
857 log_cq_size = mlx5e_shampo_get_log_cq_size(mdev, params, xsk);
858 else
859 log_cq_size = mlx5e_mpwqe_get_log_rq_size(mdev, params, xsk) +
860 mlx5e_mpwqe_get_log_num_strides(mdev, params, xsk);
861 break;
862 default: /* MLX5_WQ_TYPE_CYCLIC */
863 log_cq_size = params->log_rq_mtu_frames;
864 }
865
866 MLX5_SET(cqc, cqc, log_cq_size, log_cq_size);
867 if (MLX5E_GET_PFLAG(params, MLX5E_PFLAG_RX_CQE_COMPRESS)) {
868 MLX5_SET(cqc, cqc, mini_cqe_res_format, hw_stridx ?
869 MLX5_CQE_FORMAT_CSUM_STRIDX : MLX5_CQE_FORMAT_CSUM);
870 MLX5_SET(cqc, cqc, cqe_compression_layout,
871 MLX5_CAP_GEN(mdev, enhanced_cqe_compression) ?
872 MLX5_CQE_COMPRESS_LAYOUT_ENHANCED :
873 MLX5_CQE_COMPRESS_LAYOUT_BASIC);
874 MLX5_SET(cqc, cqc, cqe_comp_en, 1);
875 }
876
877 mlx5e_build_common_cq_param(mdev, param);
878 param->cq_period_mode = params->rx_cq_moderation.cq_period_mode;
879 }
880
rq_end_pad_mode(struct mlx5_core_dev * mdev,struct mlx5e_params * params)881 static u8 rq_end_pad_mode(struct mlx5_core_dev *mdev, struct mlx5e_params *params)
882 {
883 bool lro_en = params->packet_merge.type == MLX5E_PACKET_MERGE_LRO;
884 bool ro = MLX5_CAP_GEN(mdev, relaxed_ordering_write);
885
886 return ro && lro_en ?
887 MLX5_WQ_END_PAD_MODE_NONE : MLX5_WQ_END_PAD_MODE_ALIGN;
888 }
889
mlx5e_build_rq_param(struct mlx5_core_dev * mdev,struct mlx5e_params * params,struct mlx5e_xsk_param * xsk,struct mlx5e_rq_param * param)890 int mlx5e_build_rq_param(struct mlx5_core_dev *mdev,
891 struct mlx5e_params *params,
892 struct mlx5e_xsk_param *xsk,
893 struct mlx5e_rq_param *param)
894 {
895 void *rqc = param->rqc;
896 void *wq = MLX5_ADDR_OF(rqc, rqc, wq);
897 int ndsegs = 1;
898 int err;
899
900 switch (params->rq_wq_type) {
901 case MLX5_WQ_TYPE_LINKED_LIST_STRIDING_RQ: {
902 u8 log_wqe_num_of_strides = mlx5e_mpwqe_get_log_num_strides(mdev, params, xsk);
903 u8 log_wqe_stride_size = mlx5e_mpwqe_get_log_stride_size(mdev, params, xsk);
904 enum mlx5e_mpwrq_umr_mode umr_mode = mlx5e_mpwrq_umr_mode(mdev, xsk);
905 u8 page_shift = mlx5e_mpwrq_page_shift(mdev, xsk);
906
907 if (!mlx5e_verify_rx_mpwqe_strides(mdev, log_wqe_stride_size,
908 log_wqe_num_of_strides,
909 page_shift, umr_mode)) {
910 mlx5_core_err(mdev,
911 "Bad RX MPWQE params: log_stride_size %u, log_num_strides %u, umr_mode %d\n",
912 log_wqe_stride_size, log_wqe_num_of_strides,
913 umr_mode);
914 return -EINVAL;
915 }
916
917 MLX5_SET(wq, wq, log_wqe_num_of_strides,
918 log_wqe_num_of_strides - MLX5_MPWQE_LOG_NUM_STRIDES_BASE);
919 MLX5_SET(wq, wq, log_wqe_stride_size,
920 log_wqe_stride_size - MLX5_MPWQE_LOG_STRIDE_SZ_BASE);
921 MLX5_SET(wq, wq, log_wq_sz, mlx5e_mpwqe_get_log_rq_size(mdev, params, xsk));
922 if (params->packet_merge.type == MLX5E_PACKET_MERGE_SHAMPO) {
923 MLX5_SET(wq, wq, shampo_enable, true);
924 MLX5_SET(wq, wq, log_reservation_size,
925 mlx5e_shampo_get_log_rsrv_size(mdev, params));
926 MLX5_SET(wq, wq,
927 log_max_num_of_packets_per_reservation,
928 mlx5e_shampo_get_log_pkt_per_rsrv(mdev, params));
929 MLX5_SET(wq, wq, log_headers_entry_size,
930 mlx5e_shampo_get_log_hd_entry_size(mdev, params));
931 MLX5_SET(rqc, rqc, reservation_timeout,
932 mlx5e_choose_lro_timeout(mdev, MLX5E_DEFAULT_SHAMPO_TIMEOUT));
933 MLX5_SET(rqc, rqc, shampo_match_criteria_type,
934 params->packet_merge.shampo.match_criteria_type);
935 MLX5_SET(rqc, rqc, shampo_no_match_alignment_granularity,
936 params->packet_merge.shampo.alignment_granularity);
937 }
938 break;
939 }
940 default: /* MLX5_WQ_TYPE_CYCLIC */
941 MLX5_SET(wq, wq, log_wq_sz, params->log_rq_mtu_frames);
942 err = mlx5e_build_rq_frags_info(mdev, params, xsk, ¶m->frags_info,
943 ¶m->xdp_frag_size);
944 if (err)
945 return err;
946 ndsegs = param->frags_info.num_frags;
947 }
948
949 MLX5_SET(wq, wq, wq_type, params->rq_wq_type);
950 MLX5_SET(wq, wq, end_padding_mode, rq_end_pad_mode(mdev, params));
951 MLX5_SET(wq, wq, log_wq_stride,
952 mlx5e_get_rqwq_log_stride(params->rq_wq_type, ndsegs));
953 MLX5_SET(wq, wq, pd, mdev->mlx5e_res.hw_objs.pdn);
954 MLX5_SET(rqc, rqc, vsd, params->vlan_strip_disable);
955 MLX5_SET(rqc, rqc, scatter_fcs, params->scatter_fcs_en);
956
957 param->wq.buf_numa_node = dev_to_node(mlx5_core_dma_dev(mdev));
958 mlx5e_build_rx_cq_param(mdev, params, xsk, ¶m->cqp);
959
960 return 0;
961 }
962
mlx5e_build_drop_rq_param(struct mlx5_core_dev * mdev,struct mlx5e_rq_param * param)963 void mlx5e_build_drop_rq_param(struct mlx5_core_dev *mdev,
964 struct mlx5e_rq_param *param)
965 {
966 void *rqc = param->rqc;
967 void *wq = MLX5_ADDR_OF(rqc, rqc, wq);
968
969 MLX5_SET(wq, wq, wq_type, MLX5_WQ_TYPE_CYCLIC);
970 MLX5_SET(wq, wq, log_wq_stride,
971 mlx5e_get_rqwq_log_stride(MLX5_WQ_TYPE_CYCLIC, 1));
972
973 param->wq.buf_numa_node = dev_to_node(mlx5_core_dma_dev(mdev));
974 }
975
mlx5e_build_tx_cq_param(struct mlx5_core_dev * mdev,struct mlx5e_params * params,struct mlx5e_cq_param * param)976 void mlx5e_build_tx_cq_param(struct mlx5_core_dev *mdev,
977 struct mlx5e_params *params,
978 struct mlx5e_cq_param *param)
979 {
980 void *cqc = param->cqc;
981
982 MLX5_SET(cqc, cqc, log_cq_size, params->log_sq_size);
983
984 mlx5e_build_common_cq_param(mdev, param);
985 param->cq_period_mode = params->tx_cq_moderation.cq_period_mode;
986 }
987
mlx5e_build_sq_param_common(struct mlx5_core_dev * mdev,struct mlx5e_sq_param * param)988 void mlx5e_build_sq_param_common(struct mlx5_core_dev *mdev,
989 struct mlx5e_sq_param *param)
990 {
991 void *sqc = param->sqc;
992 void *wq = MLX5_ADDR_OF(sqc, sqc, wq);
993
994 MLX5_SET(wq, wq, log_wq_stride, ilog2(MLX5_SEND_WQE_BB));
995 MLX5_SET(wq, wq, pd, mdev->mlx5e_res.hw_objs.pdn);
996
997 param->wq.buf_numa_node = dev_to_node(mlx5_core_dma_dev(mdev));
998 }
999
mlx5e_build_sq_param(struct mlx5_core_dev * mdev,struct mlx5e_params * params,struct mlx5e_sq_param * param)1000 void mlx5e_build_sq_param(struct mlx5_core_dev *mdev,
1001 struct mlx5e_params *params,
1002 struct mlx5e_sq_param *param)
1003 {
1004 void *sqc = param->sqc;
1005 void *wq = MLX5_ADDR_OF(sqc, sqc, wq);
1006 bool allow_swp;
1007
1008 allow_swp = mlx5_geneve_tx_allowed(mdev) ||
1009 (mlx5_ipsec_device_caps(mdev) & MLX5_IPSEC_CAP_CRYPTO);
1010 mlx5e_build_sq_param_common(mdev, param);
1011 MLX5_SET(wq, wq, log_wq_sz, params->log_sq_size);
1012 MLX5_SET(sqc, sqc, allow_swp, allow_swp);
1013 param->is_mpw = MLX5E_GET_PFLAG(params, MLX5E_PFLAG_SKB_TX_MPWQE);
1014 param->stop_room = mlx5e_calc_sq_stop_room(mdev, params);
1015 mlx5e_build_tx_cq_param(mdev, params, ¶m->cqp);
1016 }
1017
mlx5e_build_ico_cq_param(struct mlx5_core_dev * mdev,u8 log_wq_size,struct mlx5e_cq_param * param)1018 static void mlx5e_build_ico_cq_param(struct mlx5_core_dev *mdev,
1019 u8 log_wq_size,
1020 struct mlx5e_cq_param *param)
1021 {
1022 void *cqc = param->cqc;
1023
1024 MLX5_SET(cqc, cqc, log_cq_size, log_wq_size);
1025
1026 mlx5e_build_common_cq_param(mdev, param);
1027
1028 param->cq_period_mode = DIM_CQ_PERIOD_MODE_START_FROM_EQE;
1029 }
1030
1031 /* This function calculates the maximum number of headers entries that are needed
1032 * per WQE, the formula is based on the size of the reservations and the
1033 * restriction we have about max packets for reservation that is equal to max
1034 * headers per reservation.
1035 */
mlx5e_shampo_hd_per_wqe(struct mlx5_core_dev * mdev,struct mlx5e_params * params,struct mlx5e_rq_param * rq_param)1036 u32 mlx5e_shampo_hd_per_wqe(struct mlx5_core_dev *mdev,
1037 struct mlx5e_params *params,
1038 struct mlx5e_rq_param *rq_param)
1039 {
1040 int resv_size = BIT(mlx5e_shampo_get_log_rsrv_size(mdev, params)) *
1041 MLX5E_SHAMPO_WQ_BASE_RESRV_SIZE;
1042 u16 num_strides = BIT(mlx5e_mpwqe_get_log_num_strides(mdev, params, NULL));
1043 int pkt_per_resv = BIT(mlx5e_shampo_get_log_pkt_per_rsrv(mdev, params));
1044 u8 log_stride_sz = mlx5e_mpwqe_get_log_stride_size(mdev, params, NULL);
1045 int wqe_size = BIT(log_stride_sz) * num_strides;
1046 u32 hd_per_wqe;
1047
1048 /* Assumption: hd_per_wqe % 8 == 0. */
1049 hd_per_wqe = (wqe_size / resv_size) * pkt_per_resv;
1050 mlx5_core_dbg(mdev, "%s hd_per_wqe = %d rsrv_size = %d wqe_size = %d pkt_per_resv = %d\n",
1051 __func__, hd_per_wqe, resv_size, wqe_size, pkt_per_resv);
1052 return hd_per_wqe;
1053 }
1054
1055 /* This function calculates the maximum number of headers entries that are needed
1056 * for the WQ, this value is uesed to allocate the header buffer in HW, thus
1057 * must be a pow of 2.
1058 */
mlx5e_shampo_hd_per_wq(struct mlx5_core_dev * mdev,struct mlx5e_params * params,struct mlx5e_rq_param * rq_param)1059 u32 mlx5e_shampo_hd_per_wq(struct mlx5_core_dev *mdev,
1060 struct mlx5e_params *params,
1061 struct mlx5e_rq_param *rq_param)
1062 {
1063 void *wqc = MLX5_ADDR_OF(rqc, rq_param->rqc, wq);
1064 int wq_size = BIT(MLX5_GET(wq, wqc, log_wq_sz));
1065 u32 hd_per_wqe, hd_per_wq;
1066
1067 hd_per_wqe = mlx5e_shampo_hd_per_wqe(mdev, params, rq_param);
1068 hd_per_wq = roundup_pow_of_two(hd_per_wqe * wq_size);
1069 return hd_per_wq;
1070 }
1071
mlx5e_shampo_icosq_sz(struct mlx5_core_dev * mdev,struct mlx5e_params * params,struct mlx5e_rq_param * rq_param)1072 static u32 mlx5e_shampo_icosq_sz(struct mlx5_core_dev *mdev,
1073 struct mlx5e_params *params,
1074 struct mlx5e_rq_param *rq_param)
1075 {
1076 int max_num_of_umr_per_wqe, max_hd_per_wqe, max_ksm_per_umr, rest;
1077 void *wqc = MLX5_ADDR_OF(rqc, rq_param->rqc, wq);
1078 int wq_size = BIT(MLX5_GET(wq, wqc, log_wq_sz));
1079 u32 wqebbs;
1080
1081 max_ksm_per_umr = MLX5E_MAX_KSM_PER_WQE(mdev);
1082 max_hd_per_wqe = mlx5e_shampo_hd_per_wqe(mdev, params, rq_param);
1083 max_num_of_umr_per_wqe = max_hd_per_wqe / max_ksm_per_umr;
1084 rest = max_hd_per_wqe % max_ksm_per_umr;
1085 wqebbs = MLX5E_KSM_UMR_WQEBBS(max_ksm_per_umr) * max_num_of_umr_per_wqe;
1086 if (rest)
1087 wqebbs += MLX5E_KSM_UMR_WQEBBS(rest);
1088 wqebbs *= wq_size;
1089 return wqebbs;
1090 }
1091
1092 #define MLX5E_LRO_TIMEOUT_ARR_SIZE 4
1093
mlx5e_choose_lro_timeout(struct mlx5_core_dev * mdev,u32 wanted_timeout)1094 u32 mlx5e_choose_lro_timeout(struct mlx5_core_dev *mdev, u32 wanted_timeout)
1095 {
1096 int i;
1097
1098 /* The supported periods are organized in ascending order */
1099 for (i = 0; i < MLX5E_LRO_TIMEOUT_ARR_SIZE - 1; i++)
1100 if (MLX5_CAP_ETH(mdev, lro_timer_supported_periods[i]) >= wanted_timeout)
1101 break;
1102
1103 return MLX5_CAP_ETH(mdev, lro_timer_supported_periods[i]);
1104 }
1105
mlx5e_mpwrq_total_umr_wqebbs(struct mlx5_core_dev * mdev,struct mlx5e_params * params,struct mlx5e_xsk_param * xsk)1106 static u32 mlx5e_mpwrq_total_umr_wqebbs(struct mlx5_core_dev *mdev,
1107 struct mlx5e_params *params,
1108 struct mlx5e_xsk_param *xsk)
1109 {
1110 enum mlx5e_mpwrq_umr_mode umr_mode = mlx5e_mpwrq_umr_mode(mdev, xsk);
1111 u8 page_shift = mlx5e_mpwrq_page_shift(mdev, xsk);
1112 u8 umr_wqebbs;
1113
1114 umr_wqebbs = mlx5e_mpwrq_umr_wqebbs(mdev, page_shift, umr_mode);
1115
1116 return umr_wqebbs * (1 << mlx5e_mpwqe_get_log_rq_size(mdev, params, xsk));
1117 }
1118
mlx5e_build_icosq_log_wq_sz(struct mlx5_core_dev * mdev,struct mlx5e_params * params,struct mlx5e_rq_param * rqp)1119 static u8 mlx5e_build_icosq_log_wq_sz(struct mlx5_core_dev *mdev,
1120 struct mlx5e_params *params,
1121 struct mlx5e_rq_param *rqp)
1122 {
1123 u32 wqebbs, total_pages, useful_space;
1124
1125 /* MLX5_WQ_TYPE_CYCLIC */
1126 if (params->rq_wq_type != MLX5_WQ_TYPE_LINKED_LIST_STRIDING_RQ)
1127 return MLX5E_PARAMS_MINIMUM_LOG_SQ_SIZE;
1128
1129 /* UMR WQEs for the regular RQ. */
1130 wqebbs = mlx5e_mpwrq_total_umr_wqebbs(mdev, params, NULL);
1131
1132 /* If XDP program is attached, XSK may be turned on at any time without
1133 * restarting the channel. ICOSQ must be big enough to fit UMR WQEs of
1134 * both regular RQ and XSK RQ.
1135 *
1136 * XSK uses different values of page_shift, and the total number of UMR
1137 * WQEBBs depends on it. This dependency is complex and not monotonic,
1138 * especially taking into consideration that some of the parameters come
1139 * from capabilities. Hence, we have to try all valid values of XSK
1140 * frame size (and page_shift) to find the maximum.
1141 */
1142 if (params->xdp_prog) {
1143 u32 max_xsk_wqebbs = 0;
1144 u8 frame_shift;
1145
1146 for (frame_shift = XDP_UMEM_MIN_CHUNK_SHIFT;
1147 frame_shift <= PAGE_SHIFT; frame_shift++) {
1148 /* The headroom doesn't affect the calculation. */
1149 struct mlx5e_xsk_param xsk = {
1150 .chunk_size = 1 << frame_shift,
1151 .unaligned = false,
1152 };
1153
1154 /* XSK aligned mode. */
1155 max_xsk_wqebbs = max(max_xsk_wqebbs,
1156 mlx5e_mpwrq_total_umr_wqebbs(mdev, params, &xsk));
1157
1158 /* XSK unaligned mode, frame size is a power of two. */
1159 xsk.unaligned = true;
1160 max_xsk_wqebbs = max(max_xsk_wqebbs,
1161 mlx5e_mpwrq_total_umr_wqebbs(mdev, params, &xsk));
1162
1163 /* XSK unaligned mode, frame size is not equal to stride size. */
1164 xsk.chunk_size -= 1;
1165 max_xsk_wqebbs = max(max_xsk_wqebbs,
1166 mlx5e_mpwrq_total_umr_wqebbs(mdev, params, &xsk));
1167
1168 /* XSK unaligned mode, frame size is a triple power of two. */
1169 xsk.chunk_size = (1 << frame_shift) / 4 * 3;
1170 max_xsk_wqebbs = max(max_xsk_wqebbs,
1171 mlx5e_mpwrq_total_umr_wqebbs(mdev, params, &xsk));
1172 }
1173
1174 wqebbs += max_xsk_wqebbs;
1175 }
1176
1177 if (params->packet_merge.type == MLX5E_PACKET_MERGE_SHAMPO)
1178 wqebbs += mlx5e_shampo_icosq_sz(mdev, params, rqp);
1179
1180 /* UMR WQEs don't cross the page boundary, they are padded with NOPs.
1181 * This padding is always smaller than the max WQE size. That gives us
1182 * at least (PAGE_SIZE - (max WQE size - MLX5_SEND_WQE_BB)) useful bytes
1183 * per page. The number of pages is estimated as the total size of WQEs
1184 * divided by the useful space in page, rounding up. If some WQEs don't
1185 * fully fit into the useful space, they can occupy part of the padding,
1186 * which proves this estimation to be correct (reserve enough space).
1187 */
1188 useful_space = PAGE_SIZE - mlx5e_get_max_sq_wqebbs(mdev) + MLX5_SEND_WQE_BB;
1189 total_pages = DIV_ROUND_UP(wqebbs * MLX5_SEND_WQE_BB, useful_space);
1190 wqebbs = total_pages * (PAGE_SIZE / MLX5_SEND_WQE_BB);
1191
1192 return max_t(u8, MLX5E_PARAMS_MINIMUM_LOG_SQ_SIZE, order_base_2(wqebbs));
1193 }
1194
mlx5e_build_async_icosq_log_wq_sz(struct mlx5_core_dev * mdev)1195 static u8 mlx5e_build_async_icosq_log_wq_sz(struct mlx5_core_dev *mdev)
1196 {
1197 if (mlx5e_is_ktls_rx(mdev))
1198 return MLX5E_PARAMS_DEFAULT_LOG_SQ_SIZE;
1199
1200 return MLX5E_PARAMS_MINIMUM_LOG_SQ_SIZE;
1201 }
1202
mlx5e_build_icosq_param(struct mlx5_core_dev * mdev,u8 log_wq_size,struct mlx5e_sq_param * param)1203 static void mlx5e_build_icosq_param(struct mlx5_core_dev *mdev,
1204 u8 log_wq_size,
1205 struct mlx5e_sq_param *param)
1206 {
1207 void *sqc = param->sqc;
1208 void *wq = MLX5_ADDR_OF(sqc, sqc, wq);
1209
1210 mlx5e_build_sq_param_common(mdev, param);
1211
1212 MLX5_SET(wq, wq, log_wq_sz, log_wq_size);
1213 MLX5_SET(sqc, sqc, reg_umr, MLX5_CAP_ETH(mdev, reg_umr_sq));
1214 mlx5e_build_ico_cq_param(mdev, log_wq_size, ¶m->cqp);
1215 }
1216
mlx5e_build_async_icosq_param(struct mlx5_core_dev * mdev,u8 log_wq_size,struct mlx5e_sq_param * param)1217 static void mlx5e_build_async_icosq_param(struct mlx5_core_dev *mdev,
1218 u8 log_wq_size,
1219 struct mlx5e_sq_param *param)
1220 {
1221 void *sqc = param->sqc;
1222 void *wq = MLX5_ADDR_OF(sqc, sqc, wq);
1223
1224 mlx5e_build_sq_param_common(mdev, param);
1225 param->stop_room = mlx5e_stop_room_for_wqe(mdev, 1); /* for XSK NOP */
1226 param->is_tls = mlx5e_is_ktls_rx(mdev);
1227 if (param->is_tls)
1228 param->stop_room += mlx5e_stop_room_for_wqe(mdev, 1); /* for TLS RX resync NOP */
1229 MLX5_SET(sqc, sqc, reg_umr, MLX5_CAP_ETH(mdev, reg_umr_sq));
1230 MLX5_SET(wq, wq, log_wq_sz, log_wq_size);
1231 mlx5e_build_ico_cq_param(mdev, log_wq_size, ¶m->cqp);
1232 }
1233
mlx5e_build_xdpsq_param(struct mlx5_core_dev * mdev,struct mlx5e_params * params,struct mlx5e_xsk_param * xsk,struct mlx5e_sq_param * param)1234 void mlx5e_build_xdpsq_param(struct mlx5_core_dev *mdev,
1235 struct mlx5e_params *params,
1236 struct mlx5e_xsk_param *xsk,
1237 struct mlx5e_sq_param *param)
1238 {
1239 void *sqc = param->sqc;
1240 void *wq = MLX5_ADDR_OF(sqc, sqc, wq);
1241
1242 mlx5e_build_sq_param_common(mdev, param);
1243 MLX5_SET(wq, wq, log_wq_sz, params->log_sq_size);
1244 param->is_mpw = MLX5E_GET_PFLAG(params, MLX5E_PFLAG_XDP_TX_MPWQE);
1245 param->is_xdp_mb = !mlx5e_rx_is_linear_skb(mdev, params, xsk);
1246 mlx5e_build_tx_cq_param(mdev, params, ¶m->cqp);
1247 }
1248
mlx5e_build_channel_param(struct mlx5_core_dev * mdev,struct mlx5e_params * params,struct mlx5e_channel_param * cparam)1249 int mlx5e_build_channel_param(struct mlx5_core_dev *mdev,
1250 struct mlx5e_params *params,
1251 struct mlx5e_channel_param *cparam)
1252 {
1253 u8 icosq_log_wq_sz, async_icosq_log_wq_sz;
1254 int err;
1255
1256 err = mlx5e_build_rq_param(mdev, params, NULL, &cparam->rq);
1257 if (err)
1258 return err;
1259
1260 icosq_log_wq_sz = mlx5e_build_icosq_log_wq_sz(mdev, params, &cparam->rq);
1261 async_icosq_log_wq_sz = mlx5e_build_async_icosq_log_wq_sz(mdev);
1262
1263 mlx5e_build_sq_param(mdev, params, &cparam->txq_sq);
1264 mlx5e_build_xdpsq_param(mdev, params, NULL, &cparam->xdp_sq);
1265 mlx5e_build_icosq_param(mdev, icosq_log_wq_sz, &cparam->icosq);
1266 mlx5e_build_async_icosq_param(mdev, async_icosq_log_wq_sz, &cparam->async_icosq);
1267
1268 return 0;
1269 }
1270