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