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