xref: /linux/drivers/net/ethernet/mellanox/mlx5/core/en_rx.c (revision 3fd6c59042dbba50391e30862beac979491145fe)
1 /*
2  * Copyright (c) 2015, Mellanox Technologies. All rights reserved.
3  *
4  * This software is available to you under a choice of one of two
5  * licenses.  You may choose to be licensed under the terms of the GNU
6  * General Public License (GPL) Version 2, available from the file
7  * COPYING in the main directory of this source tree, or the
8  * OpenIB.org BSD license below:
9  *
10  *     Redistribution and use in source and binary forms, with or
11  *     without modification, are permitted provided that the following
12  *     conditions are met:
13  *
14  *      - Redistributions of source code must retain the above
15  *        copyright notice, this list of conditions and the following
16  *        disclaimer.
17  *
18  *      - Redistributions in binary form must reproduce the above
19  *        copyright notice, this list of conditions and the following
20  *        disclaimer in the documentation and/or other materials
21  *        provided with the distribution.
22  *
23  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
27  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
28  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
29  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
30  * SOFTWARE.
31  */
32 
33 #include <linux/ip.h>
34 #include <linux/ipv6.h>
35 #include <linux/tcp.h>
36 #include <linux/bitmap.h>
37 #include <linux/filter.h>
38 #include <net/ip6_checksum.h>
39 #include <net/page_pool/helpers.h>
40 #include <net/inet_ecn.h>
41 #include <net/gro.h>
42 #include <net/udp.h>
43 #include <net/tcp.h>
44 #include <net/xdp_sock_drv.h>
45 #include "en.h"
46 #include "en/txrx.h"
47 #include "en_tc.h"
48 #include "eswitch.h"
49 #include "en_rep.h"
50 #include "en/rep/tc.h"
51 #include "ipoib/ipoib.h"
52 #include "en_accel/ipsec.h"
53 #include "en_accel/macsec.h"
54 #include "en_accel/ipsec_rxtx.h"
55 #include "en_accel/ktls_txrx.h"
56 #include "en/xdp.h"
57 #include "en/xsk/rx.h"
58 #include "en/health.h"
59 #include "en/params.h"
60 #include "devlink.h"
61 #include "en/devlink.h"
62 
63 static struct sk_buff *
64 mlx5e_skb_from_cqe_mpwrq_linear(struct mlx5e_rq *rq, struct mlx5e_mpw_info *wi,
65 				struct mlx5_cqe64 *cqe, u16 cqe_bcnt, u32 head_offset,
66 				u32 page_idx);
67 static struct sk_buff *
68 mlx5e_skb_from_cqe_mpwrq_nonlinear(struct mlx5e_rq *rq, struct mlx5e_mpw_info *wi,
69 				   struct mlx5_cqe64 *cqe, u16 cqe_bcnt, u32 head_offset,
70 				   u32 page_idx);
71 static void mlx5e_handle_rx_cqe(struct mlx5e_rq *rq, struct mlx5_cqe64 *cqe);
72 static void mlx5e_handle_rx_cqe_mpwrq(struct mlx5e_rq *rq, struct mlx5_cqe64 *cqe);
73 static void mlx5e_handle_rx_cqe_mpwrq_shampo(struct mlx5e_rq *rq, struct mlx5_cqe64 *cqe);
74 
75 const struct mlx5e_rx_handlers mlx5e_rx_handlers_nic = {
76 	.handle_rx_cqe       = mlx5e_handle_rx_cqe,
77 	.handle_rx_cqe_mpwqe = mlx5e_handle_rx_cqe_mpwrq,
78 	.handle_rx_cqe_mpwqe_shampo = mlx5e_handle_rx_cqe_mpwrq_shampo,
79 };
80 
mlx5e_read_cqe_slot(struct mlx5_cqwq * wq,u32 cqcc,void * data)81 static inline void mlx5e_read_cqe_slot(struct mlx5_cqwq *wq,
82 				       u32 cqcc, void *data)
83 {
84 	u32 ci = mlx5_cqwq_ctr2ix(wq, cqcc);
85 
86 	memcpy(data, mlx5_cqwq_get_wqe(wq, ci), sizeof(struct mlx5_cqe64));
87 }
88 
mlx5e_read_enhanced_title_slot(struct mlx5e_rq * rq,struct mlx5_cqe64 * cqe)89 static void mlx5e_read_enhanced_title_slot(struct mlx5e_rq *rq,
90 					   struct mlx5_cqe64 *cqe)
91 {
92 	struct mlx5e_cq_decomp *cqd = &rq->cqd;
93 	struct mlx5_cqe64 *title = &cqd->title;
94 
95 	memcpy(title, cqe, sizeof(struct mlx5_cqe64));
96 
97 	if (likely(test_bit(MLX5E_RQ_STATE_MINI_CQE_HW_STRIDX, &rq->state)))
98 		return;
99 
100 	if (rq->wq_type == MLX5_WQ_TYPE_LINKED_LIST_STRIDING_RQ)
101 		cqd->wqe_counter = mpwrq_get_cqe_stride_index(title) +
102 			mpwrq_get_cqe_consumed_strides(title);
103 	else
104 		cqd->wqe_counter =
105 			mlx5_wq_cyc_ctr2ix(&rq->wqe.wq, be16_to_cpu(title->wqe_counter) + 1);
106 }
107 
mlx5e_read_title_slot(struct mlx5e_rq * rq,struct mlx5_cqwq * wq,u32 cqcc)108 static inline void mlx5e_read_title_slot(struct mlx5e_rq *rq,
109 					 struct mlx5_cqwq *wq,
110 					 u32 cqcc)
111 {
112 	struct mlx5e_cq_decomp *cqd = &rq->cqd;
113 	struct mlx5_cqe64 *title = &cqd->title;
114 
115 	mlx5e_read_cqe_slot(wq, cqcc, title);
116 	cqd->left        = be32_to_cpu(title->byte_cnt);
117 	cqd->wqe_counter = be16_to_cpu(title->wqe_counter);
118 	rq->stats->cqe_compress_blks++;
119 }
120 
mlx5e_read_mini_arr_slot(struct mlx5_cqwq * wq,struct mlx5e_cq_decomp * cqd,u32 cqcc)121 static inline void mlx5e_read_mini_arr_slot(struct mlx5_cqwq *wq,
122 					    struct mlx5e_cq_decomp *cqd,
123 					    u32 cqcc)
124 {
125 	mlx5e_read_cqe_slot(wq, cqcc, cqd->mini_arr);
126 	cqd->mini_arr_idx = 0;
127 }
128 
mlx5e_cqes_update_owner(struct mlx5_cqwq * wq,int n)129 static inline void mlx5e_cqes_update_owner(struct mlx5_cqwq *wq, int n)
130 {
131 	u32 cqcc   = wq->cc;
132 	u8  op_own = mlx5_cqwq_get_ctr_wrap_cnt(wq, cqcc) & 1;
133 	u32 ci     = mlx5_cqwq_ctr2ix(wq, cqcc);
134 	u32 wq_sz  = mlx5_cqwq_get_size(wq);
135 	u32 ci_top = min_t(u32, wq_sz, ci + n);
136 
137 	for (; ci < ci_top; ci++, n--) {
138 		struct mlx5_cqe64 *cqe = mlx5_cqwq_get_wqe(wq, ci);
139 
140 		cqe->op_own = op_own;
141 	}
142 
143 	if (unlikely(ci == wq_sz)) {
144 		op_own = !op_own;
145 		for (ci = 0; ci < n; ci++) {
146 			struct mlx5_cqe64 *cqe = mlx5_cqwq_get_wqe(wq, ci);
147 
148 			cqe->op_own = op_own;
149 		}
150 	}
151 }
152 
mlx5e_decompress_cqe(struct mlx5e_rq * rq,struct mlx5_cqwq * wq,u32 cqcc)153 static inline void mlx5e_decompress_cqe(struct mlx5e_rq *rq,
154 					struct mlx5_cqwq *wq,
155 					u32 cqcc)
156 {
157 	struct mlx5e_cq_decomp *cqd = &rq->cqd;
158 	struct mlx5_mini_cqe8 *mini_cqe = &cqd->mini_arr[cqd->mini_arr_idx];
159 	struct mlx5_cqe64 *title = &cqd->title;
160 
161 	title->byte_cnt     = mini_cqe->byte_cnt;
162 	title->check_sum    = mini_cqe->checksum;
163 	title->op_own      &= 0xf0;
164 	title->op_own      |= 0x01 & (cqcc >> wq->fbc.log_sz);
165 
166 	/* state bit set implies linked-list striding RQ wq type and
167 	 * HW stride index capability supported
168 	 */
169 	if (test_bit(MLX5E_RQ_STATE_MINI_CQE_HW_STRIDX, &rq->state)) {
170 		title->wqe_counter = mini_cqe->stridx;
171 		return;
172 	}
173 
174 	/* HW stride index capability not supported */
175 	title->wqe_counter = cpu_to_be16(cqd->wqe_counter);
176 	if (rq->wq_type == MLX5_WQ_TYPE_LINKED_LIST_STRIDING_RQ)
177 		cqd->wqe_counter += mpwrq_get_cqe_consumed_strides(title);
178 	else
179 		cqd->wqe_counter =
180 			mlx5_wq_cyc_ctr2ix(&rq->wqe.wq, cqd->wqe_counter + 1);
181 }
182 
mlx5e_decompress_cqe_no_hash(struct mlx5e_rq * rq,struct mlx5_cqwq * wq,u32 cqcc)183 static inline void mlx5e_decompress_cqe_no_hash(struct mlx5e_rq *rq,
184 						struct mlx5_cqwq *wq,
185 						u32 cqcc)
186 {
187 	struct mlx5e_cq_decomp *cqd = &rq->cqd;
188 
189 	mlx5e_decompress_cqe(rq, wq, cqcc);
190 	cqd->title.rss_hash_type   = 0;
191 	cqd->title.rss_hash_result = 0;
192 }
193 
mlx5e_decompress_enhanced_cqe(struct mlx5e_rq * rq,struct mlx5_cqwq * wq,struct mlx5_cqe64 * cqe,int budget_rem)194 static u32 mlx5e_decompress_enhanced_cqe(struct mlx5e_rq *rq,
195 					 struct mlx5_cqwq *wq,
196 					 struct mlx5_cqe64 *cqe,
197 					 int budget_rem)
198 {
199 	struct mlx5e_cq_decomp *cqd = &rq->cqd;
200 	u32 cqcc, left;
201 	u32 i;
202 
203 	left = get_cqe_enhanced_num_mini_cqes(cqe);
204 	/* Here we avoid breaking the cqe compression session in the middle
205 	 * in case budget is not sufficient to handle all of it. In this case
206 	 * we return work_done == budget_rem to give 'busy' napi indication.
207 	 */
208 	if (unlikely(left > budget_rem))
209 		return budget_rem;
210 
211 	cqcc = wq->cc;
212 	cqd->mini_arr_idx = 0;
213 	memcpy(cqd->mini_arr, cqe, sizeof(struct mlx5_cqe64));
214 	for (i = 0; i < left; i++, cqd->mini_arr_idx++, cqcc++) {
215 		mlx5e_decompress_cqe_no_hash(rq, wq, cqcc);
216 		INDIRECT_CALL_3(rq->handle_rx_cqe, mlx5e_handle_rx_cqe_mpwrq,
217 				mlx5e_handle_rx_cqe, mlx5e_handle_rx_cqe_mpwrq_shampo,
218 				rq, &cqd->title);
219 	}
220 	wq->cc = cqcc;
221 	rq->stats->cqe_compress_pkts += left;
222 
223 	return left;
224 }
225 
mlx5e_decompress_cqes_cont(struct mlx5e_rq * rq,struct mlx5_cqwq * wq,int update_owner_only,int budget_rem)226 static inline u32 mlx5e_decompress_cqes_cont(struct mlx5e_rq *rq,
227 					     struct mlx5_cqwq *wq,
228 					     int update_owner_only,
229 					     int budget_rem)
230 {
231 	struct mlx5e_cq_decomp *cqd = &rq->cqd;
232 	u32 cqcc = wq->cc + update_owner_only;
233 	u32 cqe_count;
234 	u32 i;
235 
236 	cqe_count = min_t(u32, cqd->left, budget_rem);
237 
238 	for (i = update_owner_only; i < cqe_count;
239 	     i++, cqd->mini_arr_idx++, cqcc++) {
240 		if (cqd->mini_arr_idx == MLX5_MINI_CQE_ARRAY_SIZE)
241 			mlx5e_read_mini_arr_slot(wq, cqd, cqcc);
242 
243 		mlx5e_decompress_cqe_no_hash(rq, wq, cqcc);
244 		INDIRECT_CALL_3(rq->handle_rx_cqe, mlx5e_handle_rx_cqe_mpwrq,
245 				mlx5e_handle_rx_cqe_mpwrq_shampo, mlx5e_handle_rx_cqe,
246 				rq, &cqd->title);
247 	}
248 	mlx5e_cqes_update_owner(wq, cqcc - wq->cc);
249 	wq->cc = cqcc;
250 	cqd->left -= cqe_count;
251 	rq->stats->cqe_compress_pkts += cqe_count;
252 
253 	return cqe_count;
254 }
255 
mlx5e_decompress_cqes_start(struct mlx5e_rq * rq,struct mlx5_cqwq * wq,int budget_rem)256 static inline u32 mlx5e_decompress_cqes_start(struct mlx5e_rq *rq,
257 					      struct mlx5_cqwq *wq,
258 					      int budget_rem)
259 {
260 	struct mlx5e_cq_decomp *cqd = &rq->cqd;
261 	u32 cc = wq->cc;
262 
263 	mlx5e_read_title_slot(rq, wq, cc);
264 	mlx5e_read_mini_arr_slot(wq, cqd, cc + 1);
265 	mlx5e_decompress_cqe(rq, wq, cc);
266 	INDIRECT_CALL_3(rq->handle_rx_cqe, mlx5e_handle_rx_cqe_mpwrq,
267 			mlx5e_handle_rx_cqe_mpwrq_shampo, mlx5e_handle_rx_cqe,
268 			rq, &cqd->title);
269 	cqd->mini_arr_idx++;
270 
271 	return mlx5e_decompress_cqes_cont(rq, wq, 1, budget_rem);
272 }
273 
274 #define MLX5E_PAGECNT_BIAS_MAX (PAGE_SIZE / 64)
275 
mlx5e_page_alloc_fragmented(struct mlx5e_rq * rq,struct mlx5e_frag_page * frag_page)276 static int mlx5e_page_alloc_fragmented(struct mlx5e_rq *rq,
277 				       struct mlx5e_frag_page *frag_page)
278 {
279 	struct page *page;
280 
281 	page = page_pool_dev_alloc_pages(rq->page_pool);
282 	if (unlikely(!page))
283 		return -ENOMEM;
284 
285 	page_pool_fragment_page(page, MLX5E_PAGECNT_BIAS_MAX);
286 
287 	*frag_page = (struct mlx5e_frag_page) {
288 		.page	= page,
289 		.frags	= 0,
290 	};
291 
292 	return 0;
293 }
294 
mlx5e_page_release_fragmented(struct mlx5e_rq * rq,struct mlx5e_frag_page * frag_page)295 static void mlx5e_page_release_fragmented(struct mlx5e_rq *rq,
296 					  struct mlx5e_frag_page *frag_page)
297 {
298 	u16 drain_count = MLX5E_PAGECNT_BIAS_MAX - frag_page->frags;
299 	struct page *page = frag_page->page;
300 
301 	if (page_pool_unref_page(page, drain_count) == 0)
302 		page_pool_put_unrefed_page(rq->page_pool, page, -1, true);
303 }
304 
mlx5e_get_rx_frag(struct mlx5e_rq * rq,struct mlx5e_wqe_frag_info * frag)305 static inline int mlx5e_get_rx_frag(struct mlx5e_rq *rq,
306 				    struct mlx5e_wqe_frag_info *frag)
307 {
308 	int err = 0;
309 
310 	if (!frag->offset)
311 		/* On first frag (offset == 0), replenish page.
312 		 * Other frags that point to the same page (with a different
313 		 * offset) should just use the new one without replenishing again
314 		 * by themselves.
315 		 */
316 		err = mlx5e_page_alloc_fragmented(rq, frag->frag_page);
317 
318 	return err;
319 }
320 
mlx5e_frag_can_release(struct mlx5e_wqe_frag_info * frag)321 static bool mlx5e_frag_can_release(struct mlx5e_wqe_frag_info *frag)
322 {
323 #define CAN_RELEASE_MASK \
324 	(BIT(MLX5E_WQE_FRAG_LAST_IN_PAGE) | BIT(MLX5E_WQE_FRAG_SKIP_RELEASE))
325 
326 #define CAN_RELEASE_VALUE BIT(MLX5E_WQE_FRAG_LAST_IN_PAGE)
327 
328 	return (frag->flags & CAN_RELEASE_MASK) == CAN_RELEASE_VALUE;
329 }
330 
mlx5e_put_rx_frag(struct mlx5e_rq * rq,struct mlx5e_wqe_frag_info * frag)331 static inline void mlx5e_put_rx_frag(struct mlx5e_rq *rq,
332 				     struct mlx5e_wqe_frag_info *frag)
333 {
334 	if (mlx5e_frag_can_release(frag))
335 		mlx5e_page_release_fragmented(rq, frag->frag_page);
336 }
337 
get_frag(struct mlx5e_rq * rq,u16 ix)338 static inline struct mlx5e_wqe_frag_info *get_frag(struct mlx5e_rq *rq, u16 ix)
339 {
340 	return &rq->wqe.frags[ix << rq->wqe.info.log_num_frags];
341 }
342 
mlx5e_alloc_rx_wqe(struct mlx5e_rq * rq,struct mlx5e_rx_wqe_cyc * wqe,u16 ix)343 static int mlx5e_alloc_rx_wqe(struct mlx5e_rq *rq, struct mlx5e_rx_wqe_cyc *wqe,
344 			      u16 ix)
345 {
346 	struct mlx5e_wqe_frag_info *frag = get_frag(rq, ix);
347 	int err;
348 	int i;
349 
350 	for (i = 0; i < rq->wqe.info.num_frags; i++, frag++) {
351 		dma_addr_t addr;
352 		u16 headroom;
353 
354 		err = mlx5e_get_rx_frag(rq, frag);
355 		if (unlikely(err))
356 			goto free_frags;
357 
358 		frag->flags &= ~BIT(MLX5E_WQE_FRAG_SKIP_RELEASE);
359 
360 		headroom = i == 0 ? rq->buff.headroom : 0;
361 		addr = page_pool_get_dma_addr(frag->frag_page->page);
362 		wqe->data[i].addr = cpu_to_be64(addr + frag->offset + headroom);
363 	}
364 
365 	return 0;
366 
367 free_frags:
368 	while (--i >= 0)
369 		mlx5e_put_rx_frag(rq, --frag);
370 
371 	return err;
372 }
373 
mlx5e_free_rx_wqe(struct mlx5e_rq * rq,struct mlx5e_wqe_frag_info * wi)374 static inline void mlx5e_free_rx_wqe(struct mlx5e_rq *rq,
375 				     struct mlx5e_wqe_frag_info *wi)
376 {
377 	int i;
378 
379 	for (i = 0; i < rq->wqe.info.num_frags; i++, wi++)
380 		mlx5e_put_rx_frag(rq, wi);
381 }
382 
mlx5e_xsk_free_rx_wqe(struct mlx5e_wqe_frag_info * wi)383 static void mlx5e_xsk_free_rx_wqe(struct mlx5e_wqe_frag_info *wi)
384 {
385 	if (!(wi->flags & BIT(MLX5E_WQE_FRAG_SKIP_RELEASE)))
386 		xsk_buff_free(*wi->xskp);
387 }
388 
mlx5e_dealloc_rx_wqe(struct mlx5e_rq * rq,u16 ix)389 static void mlx5e_dealloc_rx_wqe(struct mlx5e_rq *rq, u16 ix)
390 {
391 	struct mlx5e_wqe_frag_info *wi = get_frag(rq, ix);
392 
393 	if (rq->xsk_pool) {
394 		mlx5e_xsk_free_rx_wqe(wi);
395 	} else {
396 		mlx5e_free_rx_wqe(rq, wi);
397 
398 		/* Avoid a second release of the wqe pages: dealloc is called
399 		 * for the same missing wqes on regular RQ flush and on regular
400 		 * RQ close. This happens when XSK RQs come into play.
401 		 */
402 		for (int i = 0; i < rq->wqe.info.num_frags; i++, wi++)
403 			wi->flags |= BIT(MLX5E_WQE_FRAG_SKIP_RELEASE);
404 	}
405 }
406 
mlx5e_xsk_free_rx_wqes(struct mlx5e_rq * rq,u16 ix,int wqe_bulk)407 static void mlx5e_xsk_free_rx_wqes(struct mlx5e_rq *rq, u16 ix, int wqe_bulk)
408 {
409 	struct mlx5_wq_cyc *wq = &rq->wqe.wq;
410 	int i;
411 
412 	for (i = 0; i < wqe_bulk; i++) {
413 		int j = mlx5_wq_cyc_ctr2ix(wq, ix + i);
414 		struct mlx5e_wqe_frag_info *wi;
415 
416 		wi = get_frag(rq, j);
417 		/* The page is always put into the Reuse Ring, because there
418 		 * is no way to return the page to the userspace when the
419 		 * interface goes down.
420 		 */
421 		mlx5e_xsk_free_rx_wqe(wi);
422 	}
423 }
424 
mlx5e_free_rx_wqes(struct mlx5e_rq * rq,u16 ix,int wqe_bulk)425 static void mlx5e_free_rx_wqes(struct mlx5e_rq *rq, u16 ix, int wqe_bulk)
426 {
427 	struct mlx5_wq_cyc *wq = &rq->wqe.wq;
428 	int i;
429 
430 	for (i = 0; i < wqe_bulk; i++) {
431 		int j = mlx5_wq_cyc_ctr2ix(wq, ix + i);
432 		struct mlx5e_wqe_frag_info *wi;
433 
434 		wi = get_frag(rq, j);
435 		mlx5e_free_rx_wqe(rq, wi);
436 	}
437 }
438 
mlx5e_alloc_rx_wqes(struct mlx5e_rq * rq,u16 ix,int wqe_bulk)439 static int mlx5e_alloc_rx_wqes(struct mlx5e_rq *rq, u16 ix, int wqe_bulk)
440 {
441 	struct mlx5_wq_cyc *wq = &rq->wqe.wq;
442 	int i;
443 
444 	for (i = 0; i < wqe_bulk; i++) {
445 		int j = mlx5_wq_cyc_ctr2ix(wq, ix + i);
446 		struct mlx5e_rx_wqe_cyc *wqe;
447 
448 		wqe = mlx5_wq_cyc_get_wqe(wq, j);
449 
450 		if (unlikely(mlx5e_alloc_rx_wqe(rq, wqe, j)))
451 			break;
452 	}
453 
454 	return i;
455 }
456 
mlx5e_refill_rx_wqes(struct mlx5e_rq * rq,u16 ix,int wqe_bulk)457 static int mlx5e_refill_rx_wqes(struct mlx5e_rq *rq, u16 ix, int wqe_bulk)
458 {
459 	int remaining = wqe_bulk;
460 	int total_alloc = 0;
461 	int refill_alloc;
462 	int refill;
463 
464 	/* The WQE bulk is split into smaller bulks that are sized
465 	 * according to the page pool cache refill size to avoid overflowing
466 	 * the page pool cache due to too many page releases at once.
467 	 */
468 	do {
469 		refill = min_t(u16, rq->wqe.info.refill_unit, remaining);
470 
471 		mlx5e_free_rx_wqes(rq, ix + total_alloc, refill);
472 		refill_alloc = mlx5e_alloc_rx_wqes(rq, ix + total_alloc, refill);
473 		if (unlikely(refill_alloc != refill))
474 			goto err_free;
475 
476 		total_alloc += refill_alloc;
477 		remaining -= refill;
478 	} while (remaining);
479 
480 	return total_alloc;
481 
482 err_free:
483 	mlx5e_free_rx_wqes(rq, ix, total_alloc + refill_alloc);
484 
485 	for (int i = 0; i < total_alloc + refill; i++) {
486 		int j = mlx5_wq_cyc_ctr2ix(&rq->wqe.wq, ix + i);
487 		struct mlx5e_wqe_frag_info *frag;
488 
489 		frag = get_frag(rq, j);
490 		for (int k = 0; k < rq->wqe.info.num_frags; k++, frag++)
491 			frag->flags |= BIT(MLX5E_WQE_FRAG_SKIP_RELEASE);
492 	}
493 
494 	return 0;
495 }
496 
497 static void
mlx5e_add_skb_shared_info_frag(struct mlx5e_rq * rq,struct skb_shared_info * sinfo,struct xdp_buff * xdp,struct mlx5e_frag_page * frag_page,u32 frag_offset,u32 len)498 mlx5e_add_skb_shared_info_frag(struct mlx5e_rq *rq, struct skb_shared_info *sinfo,
499 			       struct xdp_buff *xdp, struct mlx5e_frag_page *frag_page,
500 			       u32 frag_offset, u32 len)
501 {
502 	skb_frag_t *frag;
503 
504 	dma_addr_t addr = page_pool_get_dma_addr(frag_page->page);
505 
506 	dma_sync_single_for_cpu(rq->pdev, addr + frag_offset, len, rq->buff.map_dir);
507 	if (!xdp_buff_has_frags(xdp)) {
508 		/* Init on the first fragment to avoid cold cache access
509 		 * when possible.
510 		 */
511 		sinfo->nr_frags = 0;
512 		sinfo->xdp_frags_size = 0;
513 		xdp_buff_set_frags_flag(xdp);
514 	}
515 
516 	frag = &sinfo->frags[sinfo->nr_frags++];
517 	skb_frag_fill_page_desc(frag, frag_page->page, frag_offset, len);
518 
519 	if (page_is_pfmemalloc(frag_page->page))
520 		xdp_buff_set_frag_pfmemalloc(xdp);
521 	sinfo->xdp_frags_size += len;
522 }
523 
524 static inline void
mlx5e_add_skb_frag(struct mlx5e_rq * rq,struct sk_buff * skb,struct mlx5e_frag_page * frag_page,u32 frag_offset,u32 len,unsigned int truesize)525 mlx5e_add_skb_frag(struct mlx5e_rq *rq, struct sk_buff *skb,
526 		   struct mlx5e_frag_page *frag_page,
527 		   u32 frag_offset, u32 len,
528 		   unsigned int truesize)
529 {
530 	dma_addr_t addr = page_pool_get_dma_addr(frag_page->page);
531 	u8 next_frag = skb_shinfo(skb)->nr_frags;
532 
533 	dma_sync_single_for_cpu(rq->pdev, addr + frag_offset, len,
534 				rq->buff.map_dir);
535 
536 	if (skb_can_coalesce(skb, next_frag, frag_page->page, frag_offset)) {
537 		skb_coalesce_rx_frag(skb, next_frag - 1, len, truesize);
538 	} else {
539 		frag_page->frags++;
540 		skb_add_rx_frag(skb, next_frag, frag_page->page,
541 				frag_offset, len, truesize);
542 	}
543 }
544 
545 static inline void
mlx5e_copy_skb_header(struct mlx5e_rq * rq,struct sk_buff * skb,struct page * page,dma_addr_t addr,int offset_from,int dma_offset,u32 headlen)546 mlx5e_copy_skb_header(struct mlx5e_rq *rq, struct sk_buff *skb,
547 		      struct page *page, dma_addr_t addr,
548 		      int offset_from, int dma_offset, u32 headlen)
549 {
550 	const void *from = page_address(page) + offset_from;
551 	/* Aligning len to sizeof(long) optimizes memcpy performance */
552 	unsigned int len = ALIGN(headlen, sizeof(long));
553 
554 	dma_sync_single_for_cpu(rq->pdev, addr + dma_offset, len,
555 				rq->buff.map_dir);
556 	skb_copy_to_linear_data(skb, from, len);
557 }
558 
559 static void
mlx5e_free_rx_mpwqe(struct mlx5e_rq * rq,struct mlx5e_mpw_info * wi)560 mlx5e_free_rx_mpwqe(struct mlx5e_rq *rq, struct mlx5e_mpw_info *wi)
561 {
562 	bool no_xdp_xmit;
563 	int i;
564 
565 	/* A common case for AF_XDP. */
566 	if (bitmap_full(wi->skip_release_bitmap, rq->mpwqe.pages_per_wqe))
567 		return;
568 
569 	no_xdp_xmit = bitmap_empty(wi->skip_release_bitmap, rq->mpwqe.pages_per_wqe);
570 
571 	if (rq->xsk_pool) {
572 		struct xdp_buff **xsk_buffs = wi->alloc_units.xsk_buffs;
573 
574 		/* The page is always put into the Reuse Ring, because there
575 		 * is no way to return the page to userspace when the interface
576 		 * goes down.
577 		 */
578 		for (i = 0; i < rq->mpwqe.pages_per_wqe; i++)
579 			if (no_xdp_xmit || !test_bit(i, wi->skip_release_bitmap))
580 				xsk_buff_free(xsk_buffs[i]);
581 	} else {
582 		for (i = 0; i < rq->mpwqe.pages_per_wqe; i++) {
583 			if (no_xdp_xmit || !test_bit(i, wi->skip_release_bitmap)) {
584 				struct mlx5e_frag_page *frag_page;
585 
586 				frag_page = &wi->alloc_units.frag_pages[i];
587 				mlx5e_page_release_fragmented(rq, frag_page);
588 			}
589 		}
590 	}
591 }
592 
mlx5e_post_rx_mpwqe(struct mlx5e_rq * rq,u8 n)593 static void mlx5e_post_rx_mpwqe(struct mlx5e_rq *rq, u8 n)
594 {
595 	struct mlx5_wq_ll *wq = &rq->mpwqe.wq;
596 
597 	do {
598 		u16 next_wqe_index = mlx5_wq_ll_get_wqe_next_ix(wq, wq->head);
599 
600 		mlx5_wq_ll_push(wq, next_wqe_index);
601 	} while (--n);
602 
603 	/* ensure wqes are visible to device before updating doorbell record */
604 	dma_wmb();
605 
606 	mlx5_wq_ll_update_db_record(wq);
607 }
608 
609 /* This function returns the size of the continuous free space inside a bitmap
610  * that starts from first and no longer than len including circular ones.
611  */
bitmap_find_window(unsigned long * bitmap,int len,int bitmap_size,int first)612 static int bitmap_find_window(unsigned long *bitmap, int len,
613 			      int bitmap_size, int first)
614 {
615 	int next_one, count;
616 
617 	next_one = find_next_bit(bitmap, bitmap_size, first);
618 	if (next_one == bitmap_size) {
619 		if (bitmap_size - first >= len)
620 			return len;
621 		next_one = find_next_bit(bitmap, bitmap_size, 0);
622 		count = next_one + bitmap_size - first;
623 	} else {
624 		count = next_one - first;
625 	}
626 
627 	return min(len, count);
628 }
629 
build_ksm_umr(struct mlx5e_icosq * sq,struct mlx5e_umr_wqe * umr_wqe,__be32 key,u16 offset,u16 ksm_len)630 static void build_ksm_umr(struct mlx5e_icosq *sq, struct mlx5e_umr_wqe *umr_wqe,
631 			  __be32 key, u16 offset, u16 ksm_len)
632 {
633 	memset(umr_wqe, 0, offsetof(struct mlx5e_umr_wqe, inline_ksms));
634 	umr_wqe->ctrl.opmod_idx_opcode =
635 		cpu_to_be32((sq->pc << MLX5_WQE_CTRL_WQE_INDEX_SHIFT) |
636 			     MLX5_OPCODE_UMR);
637 	umr_wqe->ctrl.umr_mkey = key;
638 	umr_wqe->ctrl.qpn_ds = cpu_to_be32((sq->sqn << MLX5_WQE_CTRL_QPN_SHIFT)
639 					    | MLX5E_KSM_UMR_DS_CNT(ksm_len));
640 	umr_wqe->uctrl.flags = MLX5_UMR_TRANSLATION_OFFSET_EN | MLX5_UMR_INLINE;
641 	umr_wqe->uctrl.xlt_offset = cpu_to_be16(offset);
642 	umr_wqe->uctrl.xlt_octowords = cpu_to_be16(ksm_len);
643 	umr_wqe->uctrl.mkey_mask     = cpu_to_be64(MLX5_MKEY_MASK_FREE);
644 }
645 
mlx5e_shampo_hd_to_frag_page(struct mlx5e_rq * rq,int header_index)646 static struct mlx5e_frag_page *mlx5e_shampo_hd_to_frag_page(struct mlx5e_rq *rq, int header_index)
647 {
648 	BUILD_BUG_ON(MLX5E_SHAMPO_LOG_MAX_HEADER_ENTRY_SIZE > PAGE_SHIFT);
649 
650 	return &rq->mpwqe.shampo->pages[header_index >> MLX5E_SHAMPO_LOG_WQ_HEADER_PER_PAGE];
651 }
652 
mlx5e_shampo_hd_offset(int header_index)653 static u64 mlx5e_shampo_hd_offset(int header_index)
654 {
655 	return (header_index & (MLX5E_SHAMPO_WQ_HEADER_PER_PAGE - 1)) <<
656 		MLX5E_SHAMPO_LOG_MAX_HEADER_ENTRY_SIZE;
657 }
658 
659 static void mlx5e_free_rx_shampo_hd_entry(struct mlx5e_rq *rq, u16 header_index);
660 
mlx5e_build_shampo_hd_umr(struct mlx5e_rq * rq,struct mlx5e_icosq * sq,u16 ksm_entries,u16 index)661 static int mlx5e_build_shampo_hd_umr(struct mlx5e_rq *rq,
662 				     struct mlx5e_icosq *sq,
663 				     u16 ksm_entries, u16 index)
664 {
665 	struct mlx5e_shampo_hd *shampo = rq->mpwqe.shampo;
666 	u16 pi, header_offset, err, wqe_bbs;
667 	u32 lkey = rq->mdev->mlx5e_res.hw_objs.mkey;
668 	struct mlx5e_umr_wqe *umr_wqe;
669 	int headroom, i = 0;
670 
671 	headroom = rq->buff.headroom;
672 	wqe_bbs = MLX5E_KSM_UMR_WQEBBS(ksm_entries);
673 	pi = mlx5e_icosq_get_next_pi(sq, wqe_bbs);
674 	umr_wqe = mlx5_wq_cyc_get_wqe(&sq->wq, pi);
675 	build_ksm_umr(sq, umr_wqe, shampo->key, index, ksm_entries);
676 
677 	WARN_ON_ONCE(ksm_entries & (MLX5E_SHAMPO_WQ_HEADER_PER_PAGE - 1));
678 	while (i < ksm_entries) {
679 		struct mlx5e_frag_page *frag_page = mlx5e_shampo_hd_to_frag_page(rq, index);
680 		u64 addr;
681 
682 		err = mlx5e_page_alloc_fragmented(rq, frag_page);
683 		if (unlikely(err))
684 			goto err_unmap;
685 
686 
687 		addr = page_pool_get_dma_addr(frag_page->page);
688 
689 		for (int j = 0; j < MLX5E_SHAMPO_WQ_HEADER_PER_PAGE; j++) {
690 			header_offset = mlx5e_shampo_hd_offset(index++);
691 
692 			umr_wqe->inline_ksms[i++] = (struct mlx5_ksm) {
693 				.key = cpu_to_be32(lkey),
694 				.va  = cpu_to_be64(addr + header_offset + headroom),
695 			};
696 		}
697 	}
698 
699 	sq->db.wqe_info[pi] = (struct mlx5e_icosq_wqe_info) {
700 		.wqe_type	= MLX5E_ICOSQ_WQE_SHAMPO_HD_UMR,
701 		.num_wqebbs	= wqe_bbs,
702 		.shampo.len	= ksm_entries,
703 	};
704 
705 	shampo->pi = (shampo->pi + ksm_entries) & (shampo->hd_per_wq - 1);
706 	sq->pc += wqe_bbs;
707 	sq->doorbell_cseg = &umr_wqe->ctrl;
708 
709 	return 0;
710 
711 err_unmap:
712 	while (--i) {
713 		--index;
714 		header_offset = mlx5e_shampo_hd_offset(index);
715 		if (!header_offset) {
716 			struct mlx5e_frag_page *frag_page = mlx5e_shampo_hd_to_frag_page(rq, index);
717 
718 			mlx5e_page_release_fragmented(rq, frag_page);
719 		}
720 	}
721 
722 	rq->stats->buff_alloc_err++;
723 	return err;
724 }
725 
mlx5e_alloc_rx_hd_mpwqe(struct mlx5e_rq * rq)726 static int mlx5e_alloc_rx_hd_mpwqe(struct mlx5e_rq *rq)
727 {
728 	struct mlx5e_shampo_hd *shampo = rq->mpwqe.shampo;
729 	u16 ksm_entries, num_wqe, index, entries_before;
730 	struct mlx5e_icosq *sq = rq->icosq;
731 	int i, err, max_ksm_entries, len;
732 
733 	max_ksm_entries = ALIGN_DOWN(MLX5E_MAX_KSM_PER_WQE(rq->mdev),
734 				     MLX5E_SHAMPO_WQ_HEADER_PER_PAGE);
735 	ksm_entries = bitmap_find_window(shampo->bitmap,
736 					 shampo->hd_per_wqe,
737 					 shampo->hd_per_wq, shampo->pi);
738 	ksm_entries = ALIGN_DOWN(ksm_entries, MLX5E_SHAMPO_WQ_HEADER_PER_PAGE);
739 	if (!ksm_entries)
740 		return 0;
741 
742 	/* pi is aligned to MLX5E_SHAMPO_WQ_HEADER_PER_PAGE */
743 	index = shampo->pi;
744 	entries_before = shampo->hd_per_wq - index;
745 
746 	if (unlikely(entries_before < ksm_entries))
747 		num_wqe = DIV_ROUND_UP(entries_before, max_ksm_entries) +
748 			  DIV_ROUND_UP(ksm_entries - entries_before, max_ksm_entries);
749 	else
750 		num_wqe = DIV_ROUND_UP(ksm_entries, max_ksm_entries);
751 
752 	for (i = 0; i < num_wqe; i++) {
753 		len = (ksm_entries > max_ksm_entries) ? max_ksm_entries :
754 							ksm_entries;
755 		if (unlikely(index + len > shampo->hd_per_wq))
756 			len = shampo->hd_per_wq - index;
757 		err = mlx5e_build_shampo_hd_umr(rq, sq, len, index);
758 		if (unlikely(err))
759 			return err;
760 		index = (index + len) & (rq->mpwqe.shampo->hd_per_wq - 1);
761 		ksm_entries -= len;
762 	}
763 
764 	return 0;
765 }
766 
mlx5e_alloc_rx_mpwqe(struct mlx5e_rq * rq,u16 ix)767 static int mlx5e_alloc_rx_mpwqe(struct mlx5e_rq *rq, u16 ix)
768 {
769 	struct mlx5e_mpw_info *wi = mlx5e_get_mpw_info(rq, ix);
770 	struct mlx5e_icosq *sq = rq->icosq;
771 	struct mlx5e_frag_page *frag_page;
772 	struct mlx5_wq_cyc *wq = &sq->wq;
773 	struct mlx5e_umr_wqe *umr_wqe;
774 	u32 offset; /* 17-bit value with MTT. */
775 	u16 pi;
776 	int err;
777 	int i;
778 
779 	if (test_bit(MLX5E_RQ_STATE_SHAMPO, &rq->state)) {
780 		err = mlx5e_alloc_rx_hd_mpwqe(rq);
781 		if (unlikely(err))
782 			goto err;
783 	}
784 
785 	pi = mlx5e_icosq_get_next_pi(sq, rq->mpwqe.umr_wqebbs);
786 	umr_wqe = mlx5_wq_cyc_get_wqe(wq, pi);
787 	memcpy(umr_wqe, &rq->mpwqe.umr_wqe, sizeof(struct mlx5e_umr_wqe));
788 
789 	frag_page = &wi->alloc_units.frag_pages[0];
790 
791 	for (i = 0; i < rq->mpwqe.pages_per_wqe; i++, frag_page++) {
792 		dma_addr_t addr;
793 
794 		err = mlx5e_page_alloc_fragmented(rq, frag_page);
795 		if (unlikely(err))
796 			goto err_unmap;
797 		addr = page_pool_get_dma_addr(frag_page->page);
798 		umr_wqe->inline_mtts[i] = (struct mlx5_mtt) {
799 			.ptag = cpu_to_be64(addr | MLX5_EN_WR),
800 		};
801 	}
802 
803 	/* Pad if needed, in case the value set to ucseg->xlt_octowords
804 	 * in mlx5e_build_umr_wqe() needed alignment.
805 	 */
806 	if (rq->mpwqe.pages_per_wqe & (MLX5_UMR_MTT_NUM_ENTRIES_ALIGNMENT - 1)) {
807 		int pad = ALIGN(rq->mpwqe.pages_per_wqe, MLX5_UMR_MTT_NUM_ENTRIES_ALIGNMENT) -
808 			rq->mpwqe.pages_per_wqe;
809 
810 		memset(&umr_wqe->inline_mtts[rq->mpwqe.pages_per_wqe], 0,
811 		       sizeof(*umr_wqe->inline_mtts) * pad);
812 	}
813 
814 	bitmap_zero(wi->skip_release_bitmap, rq->mpwqe.pages_per_wqe);
815 	wi->consumed_strides = 0;
816 
817 	umr_wqe->ctrl.opmod_idx_opcode =
818 		cpu_to_be32((sq->pc << MLX5_WQE_CTRL_WQE_INDEX_SHIFT) |
819 			    MLX5_OPCODE_UMR);
820 
821 	offset = (ix * rq->mpwqe.mtts_per_wqe) * sizeof(struct mlx5_mtt) / MLX5_OCTWORD;
822 	umr_wqe->uctrl.xlt_offset = cpu_to_be16(offset);
823 
824 	sq->db.wqe_info[pi] = (struct mlx5e_icosq_wqe_info) {
825 		.wqe_type   = MLX5E_ICOSQ_WQE_UMR_RX,
826 		.num_wqebbs = rq->mpwqe.umr_wqebbs,
827 		.umr.rq     = rq,
828 	};
829 
830 	sq->pc += rq->mpwqe.umr_wqebbs;
831 
832 	sq->doorbell_cseg = &umr_wqe->ctrl;
833 
834 	return 0;
835 
836 err_unmap:
837 	while (--i >= 0) {
838 		frag_page--;
839 		mlx5e_page_release_fragmented(rq, frag_page);
840 	}
841 
842 	bitmap_fill(wi->skip_release_bitmap, rq->mpwqe.pages_per_wqe);
843 
844 err:
845 	rq->stats->buff_alloc_err++;
846 
847 	return err;
848 }
849 
850 static void
mlx5e_free_rx_shampo_hd_entry(struct mlx5e_rq * rq,u16 header_index)851 mlx5e_free_rx_shampo_hd_entry(struct mlx5e_rq *rq, u16 header_index)
852 {
853 	struct mlx5e_shampo_hd *shampo = rq->mpwqe.shampo;
854 
855 	if (((header_index + 1) & (MLX5E_SHAMPO_WQ_HEADER_PER_PAGE - 1)) == 0) {
856 		struct mlx5e_frag_page *frag_page = mlx5e_shampo_hd_to_frag_page(rq, header_index);
857 
858 		mlx5e_page_release_fragmented(rq, frag_page);
859 	}
860 	clear_bit(header_index, shampo->bitmap);
861 }
862 
mlx5e_shampo_dealloc_hd(struct mlx5e_rq * rq)863 void mlx5e_shampo_dealloc_hd(struct mlx5e_rq *rq)
864 {
865 	struct mlx5e_shampo_hd *shampo = rq->mpwqe.shampo;
866 	int i;
867 
868 	for_each_set_bit(i, shampo->bitmap, rq->mpwqe.shampo->hd_per_wq)
869 		mlx5e_free_rx_shampo_hd_entry(rq, i);
870 }
871 
mlx5e_dealloc_rx_mpwqe(struct mlx5e_rq * rq,u16 ix)872 static void mlx5e_dealloc_rx_mpwqe(struct mlx5e_rq *rq, u16 ix)
873 {
874 	struct mlx5e_mpw_info *wi = mlx5e_get_mpw_info(rq, ix);
875 	/* This function is called on rq/netdev close. */
876 	mlx5e_free_rx_mpwqe(rq, wi);
877 
878 	/* Avoid a second release of the wqe pages: dealloc is called also
879 	 * for missing wqes on an already flushed RQ.
880 	 */
881 	bitmap_fill(wi->skip_release_bitmap, rq->mpwqe.pages_per_wqe);
882 }
883 
mlx5e_post_rx_wqes(struct mlx5e_rq * rq)884 INDIRECT_CALLABLE_SCOPE bool mlx5e_post_rx_wqes(struct mlx5e_rq *rq)
885 {
886 	struct mlx5_wq_cyc *wq = &rq->wqe.wq;
887 	int wqe_bulk, count;
888 	bool busy = false;
889 	u16 head;
890 
891 	if (unlikely(!test_bit(MLX5E_RQ_STATE_ENABLED, &rq->state)))
892 		return false;
893 
894 	if (mlx5_wq_cyc_missing(wq) < rq->wqe.info.wqe_bulk)
895 		return false;
896 
897 	if (rq->page_pool)
898 		page_pool_nid_changed(rq->page_pool, numa_mem_id());
899 
900 	wqe_bulk = mlx5_wq_cyc_missing(wq);
901 	head = mlx5_wq_cyc_get_head(wq);
902 
903 	/* Don't allow any newly allocated WQEs to share the same page with old
904 	 * WQEs that aren't completed yet. Stop earlier.
905 	 */
906 	wqe_bulk -= (head + wqe_bulk) & rq->wqe.info.wqe_index_mask;
907 
908 	if (!rq->xsk_pool) {
909 		count = mlx5e_refill_rx_wqes(rq, head, wqe_bulk);
910 	} else if (likely(!dma_dev_need_sync(rq->pdev))) {
911 		mlx5e_xsk_free_rx_wqes(rq, head, wqe_bulk);
912 		count = mlx5e_xsk_alloc_rx_wqes_batched(rq, head, wqe_bulk);
913 	} else {
914 		mlx5e_xsk_free_rx_wqes(rq, head, wqe_bulk);
915 		/* If dma_need_sync is true, it's more efficient to call
916 		 * xsk_buff_alloc in a loop, rather than xsk_buff_alloc_batch,
917 		 * because the latter does the same check and returns only one
918 		 * frame.
919 		 */
920 		count = mlx5e_xsk_alloc_rx_wqes(rq, head, wqe_bulk);
921 	}
922 
923 	mlx5_wq_cyc_push_n(wq, count);
924 	if (unlikely(count != wqe_bulk)) {
925 		rq->stats->buff_alloc_err++;
926 		busy = true;
927 	}
928 
929 	/* ensure wqes are visible to device before updating doorbell record */
930 	dma_wmb();
931 
932 	mlx5_wq_cyc_update_db_record(wq);
933 
934 	return busy;
935 }
936 
mlx5e_free_icosq_descs(struct mlx5e_icosq * sq)937 void mlx5e_free_icosq_descs(struct mlx5e_icosq *sq)
938 {
939 	u16 sqcc;
940 
941 	sqcc = sq->cc;
942 
943 	while (sqcc != sq->pc) {
944 		struct mlx5e_icosq_wqe_info *wi;
945 		u16 ci;
946 
947 		ci = mlx5_wq_cyc_ctr2ix(&sq->wq, sqcc);
948 		wi = &sq->db.wqe_info[ci];
949 		sqcc += wi->num_wqebbs;
950 #ifdef CONFIG_MLX5_EN_TLS
951 		switch (wi->wqe_type) {
952 		case MLX5E_ICOSQ_WQE_SET_PSV_TLS:
953 			mlx5e_ktls_handle_ctx_completion(wi);
954 			break;
955 		case MLX5E_ICOSQ_WQE_GET_PSV_TLS:
956 			mlx5e_ktls_handle_get_psv_completion(wi, sq);
957 			break;
958 		}
959 #endif
960 	}
961 	sq->cc = sqcc;
962 }
963 
mlx5e_shampo_fill_umr(struct mlx5e_rq * rq,int len)964 void mlx5e_shampo_fill_umr(struct mlx5e_rq *rq, int len)
965 {
966 	struct mlx5e_shampo_hd *shampo = rq->mpwqe.shampo;
967 	int end, from, full_len = len;
968 
969 	end = shampo->hd_per_wq;
970 	from = shampo->ci;
971 	if (from + len > end) {
972 		len -= end - from;
973 		bitmap_set(shampo->bitmap, from, end - from);
974 		from = 0;
975 	}
976 
977 	bitmap_set(shampo->bitmap, from, len);
978 	shampo->ci = (shampo->ci + full_len) & (shampo->hd_per_wq - 1);
979 }
980 
mlx5e_handle_shampo_hd_umr(struct mlx5e_shampo_umr umr,struct mlx5e_icosq * sq)981 static void mlx5e_handle_shampo_hd_umr(struct mlx5e_shampo_umr umr,
982 				       struct mlx5e_icosq *sq)
983 {
984 	struct mlx5e_channel *c = container_of(sq, struct mlx5e_channel, icosq);
985 	/* assume 1:1 relationship between RQ and icosq */
986 	struct mlx5e_rq *rq = &c->rq;
987 
988 	mlx5e_shampo_fill_umr(rq, umr.len);
989 }
990 
mlx5e_poll_ico_cq(struct mlx5e_cq * cq)991 int mlx5e_poll_ico_cq(struct mlx5e_cq *cq)
992 {
993 	struct mlx5e_icosq *sq = container_of(cq, struct mlx5e_icosq, cq);
994 	struct mlx5_cqe64 *cqe;
995 	u16 sqcc;
996 	int i;
997 
998 	if (unlikely(!test_bit(MLX5E_SQ_STATE_ENABLED, &sq->state)))
999 		return 0;
1000 
1001 	cqe = mlx5_cqwq_get_cqe(&cq->wq);
1002 	if (likely(!cqe))
1003 		return 0;
1004 
1005 	/* sq->cc must be updated only after mlx5_cqwq_update_db_record(),
1006 	 * otherwise a cq overrun may occur
1007 	 */
1008 	sqcc = sq->cc;
1009 
1010 	i = 0;
1011 	do {
1012 		u16 wqe_counter;
1013 		bool last_wqe;
1014 
1015 		mlx5_cqwq_pop(&cq->wq);
1016 
1017 		wqe_counter = be16_to_cpu(cqe->wqe_counter);
1018 
1019 		do {
1020 			struct mlx5e_icosq_wqe_info *wi;
1021 			u16 ci;
1022 
1023 			last_wqe = (sqcc == wqe_counter);
1024 
1025 			ci = mlx5_wq_cyc_ctr2ix(&sq->wq, sqcc);
1026 			wi = &sq->db.wqe_info[ci];
1027 			sqcc += wi->num_wqebbs;
1028 
1029 			if (last_wqe && unlikely(get_cqe_opcode(cqe) != MLX5_CQE_REQ)) {
1030 				netdev_WARN_ONCE(cq->netdev,
1031 						 "Bad OP in ICOSQ CQE: 0x%x\n",
1032 						 get_cqe_opcode(cqe));
1033 				mlx5e_dump_error_cqe(&sq->cq, sq->sqn,
1034 						     (struct mlx5_err_cqe *)cqe);
1035 				mlx5_wq_cyc_wqe_dump(&sq->wq, ci, wi->num_wqebbs);
1036 				if (!test_and_set_bit(MLX5E_SQ_STATE_RECOVERING, &sq->state))
1037 					queue_work(cq->workqueue, &sq->recover_work);
1038 				break;
1039 			}
1040 
1041 			switch (wi->wqe_type) {
1042 			case MLX5E_ICOSQ_WQE_UMR_RX:
1043 				wi->umr.rq->mpwqe.umr_completed++;
1044 				break;
1045 			case MLX5E_ICOSQ_WQE_NOP:
1046 				break;
1047 			case MLX5E_ICOSQ_WQE_SHAMPO_HD_UMR:
1048 				mlx5e_handle_shampo_hd_umr(wi->shampo, sq);
1049 				break;
1050 #ifdef CONFIG_MLX5_EN_TLS
1051 			case MLX5E_ICOSQ_WQE_UMR_TLS:
1052 				break;
1053 			case MLX5E_ICOSQ_WQE_SET_PSV_TLS:
1054 				mlx5e_ktls_handle_ctx_completion(wi);
1055 				break;
1056 			case MLX5E_ICOSQ_WQE_GET_PSV_TLS:
1057 				mlx5e_ktls_handle_get_psv_completion(wi, sq);
1058 				break;
1059 #endif
1060 			default:
1061 				netdev_WARN_ONCE(cq->netdev,
1062 						 "Bad WQE type in ICOSQ WQE info: 0x%x\n",
1063 						 wi->wqe_type);
1064 			}
1065 		} while (!last_wqe);
1066 	} while ((++i < MLX5E_TX_CQ_POLL_BUDGET) && (cqe = mlx5_cqwq_get_cqe(&cq->wq)));
1067 
1068 	sq->cc = sqcc;
1069 
1070 	mlx5_cqwq_update_db_record(&cq->wq);
1071 
1072 	return i;
1073 }
1074 
mlx5e_post_rx_mpwqes(struct mlx5e_rq * rq)1075 INDIRECT_CALLABLE_SCOPE bool mlx5e_post_rx_mpwqes(struct mlx5e_rq *rq)
1076 {
1077 	struct mlx5_wq_ll *wq = &rq->mpwqe.wq;
1078 	u8  umr_completed = rq->mpwqe.umr_completed;
1079 	struct mlx5e_icosq *sq = rq->icosq;
1080 	int alloc_err = 0;
1081 	u8  missing, i;
1082 	u16 head;
1083 
1084 	if (unlikely(!test_bit(MLX5E_RQ_STATE_ENABLED, &rq->state)))
1085 		return false;
1086 
1087 	if (umr_completed) {
1088 		mlx5e_post_rx_mpwqe(rq, umr_completed);
1089 		rq->mpwqe.umr_in_progress -= umr_completed;
1090 		rq->mpwqe.umr_completed = 0;
1091 	}
1092 
1093 	missing = mlx5_wq_ll_missing(wq) - rq->mpwqe.umr_in_progress;
1094 
1095 	if (unlikely(rq->mpwqe.umr_in_progress > rq->mpwqe.umr_last_bulk))
1096 		rq->stats->congst_umr++;
1097 
1098 	if (likely(missing < rq->mpwqe.min_wqe_bulk))
1099 		return false;
1100 
1101 	if (rq->page_pool)
1102 		page_pool_nid_changed(rq->page_pool, numa_mem_id());
1103 
1104 	head = rq->mpwqe.actual_wq_head;
1105 	i = missing;
1106 	do {
1107 		struct mlx5e_mpw_info *wi = mlx5e_get_mpw_info(rq, head);
1108 
1109 		/* Deferred free for better page pool cache usage. */
1110 		mlx5e_free_rx_mpwqe(rq, wi);
1111 
1112 		alloc_err = rq->xsk_pool ? mlx5e_xsk_alloc_rx_mpwqe(rq, head) :
1113 					   mlx5e_alloc_rx_mpwqe(rq, head);
1114 
1115 		if (unlikely(alloc_err))
1116 			break;
1117 		head = mlx5_wq_ll_get_wqe_next_ix(wq, head);
1118 	} while (--i);
1119 
1120 	rq->mpwqe.umr_last_bulk    = missing - i;
1121 	if (sq->doorbell_cseg) {
1122 		mlx5e_notify_hw(&sq->wq, sq->pc, sq->uar_map, sq->doorbell_cseg);
1123 		sq->doorbell_cseg = NULL;
1124 	}
1125 
1126 	rq->mpwqe.umr_in_progress += rq->mpwqe.umr_last_bulk;
1127 	rq->mpwqe.actual_wq_head   = head;
1128 
1129 	/* If XSK Fill Ring doesn't have enough frames, report the error, so
1130 	 * that one of the actions can be performed:
1131 	 * 1. If need_wakeup is used, signal that the application has to kick
1132 	 * the driver when it refills the Fill Ring.
1133 	 * 2. Otherwise, busy poll by rescheduling the NAPI poll.
1134 	 */
1135 	if (unlikely(alloc_err == -ENOMEM && rq->xsk_pool))
1136 		return true;
1137 
1138 	return false;
1139 }
1140 
mlx5e_lro_update_tcp_hdr(struct mlx5_cqe64 * cqe,struct tcphdr * tcp)1141 static void mlx5e_lro_update_tcp_hdr(struct mlx5_cqe64 *cqe, struct tcphdr *tcp)
1142 {
1143 	u8 l4_hdr_type = get_cqe_l4_hdr_type(cqe);
1144 	u8 tcp_ack     = (l4_hdr_type == CQE_L4_HDR_TYPE_TCP_ACK_NO_DATA) ||
1145 			 (l4_hdr_type == CQE_L4_HDR_TYPE_TCP_ACK_AND_DATA);
1146 
1147 	tcp->check                      = 0;
1148 	tcp->psh                        = get_cqe_lro_tcppsh(cqe);
1149 
1150 	if (tcp_ack) {
1151 		tcp->ack                = 1;
1152 		tcp->ack_seq            = cqe->lro.ack_seq_num;
1153 		tcp->window             = cqe->lro.tcp_win;
1154 	}
1155 }
1156 
mlx5e_lro_update_hdr(struct sk_buff * skb,struct mlx5_cqe64 * cqe,u32 cqe_bcnt)1157 static void mlx5e_lro_update_hdr(struct sk_buff *skb, struct mlx5_cqe64 *cqe,
1158 				 u32 cqe_bcnt)
1159 {
1160 	struct ethhdr	*eth = (struct ethhdr *)(skb->data);
1161 	struct tcphdr	*tcp;
1162 	int network_depth = 0;
1163 	__wsum check;
1164 	__be16 proto;
1165 	u16 tot_len;
1166 	void *ip_p;
1167 
1168 	proto = __vlan_get_protocol(skb, eth->h_proto, &network_depth);
1169 
1170 	tot_len = cqe_bcnt - network_depth;
1171 	ip_p = skb->data + network_depth;
1172 
1173 	if (proto == htons(ETH_P_IP)) {
1174 		struct iphdr *ipv4 = ip_p;
1175 
1176 		tcp = ip_p + sizeof(struct iphdr);
1177 		skb_shinfo(skb)->gso_type = SKB_GSO_TCPV4;
1178 
1179 		ipv4->ttl               = cqe->lro.min_ttl;
1180 		ipv4->tot_len           = cpu_to_be16(tot_len);
1181 		ipv4->check             = 0;
1182 		ipv4->check             = ip_fast_csum((unsigned char *)ipv4,
1183 						       ipv4->ihl);
1184 
1185 		mlx5e_lro_update_tcp_hdr(cqe, tcp);
1186 		check = csum_partial(tcp, tcp->doff * 4,
1187 				     csum_unfold((__force __sum16)cqe->check_sum));
1188 		/* Almost done, don't forget the pseudo header */
1189 		tcp->check = tcp_v4_check(tot_len - sizeof(struct iphdr),
1190 					  ipv4->saddr, ipv4->daddr, check);
1191 	} else {
1192 		u16 payload_len = tot_len - sizeof(struct ipv6hdr);
1193 		struct ipv6hdr *ipv6 = ip_p;
1194 
1195 		tcp = ip_p + sizeof(struct ipv6hdr);
1196 		skb_shinfo(skb)->gso_type = SKB_GSO_TCPV6;
1197 
1198 		ipv6->hop_limit         = cqe->lro.min_ttl;
1199 		ipv6->payload_len       = cpu_to_be16(payload_len);
1200 
1201 		mlx5e_lro_update_tcp_hdr(cqe, tcp);
1202 		check = csum_partial(tcp, tcp->doff * 4,
1203 				     csum_unfold((__force __sum16)cqe->check_sum));
1204 		/* Almost done, don't forget the pseudo header */
1205 		tcp->check = tcp_v6_check(payload_len, &ipv6->saddr,
1206 					  &ipv6->daddr, check);
1207 	}
1208 }
1209 
mlx5e_shampo_get_packet_hd(struct mlx5e_rq * rq,u16 header_index)1210 static void *mlx5e_shampo_get_packet_hd(struct mlx5e_rq *rq, u16 header_index)
1211 {
1212 	struct mlx5e_frag_page *frag_page = mlx5e_shampo_hd_to_frag_page(rq, header_index);
1213 	u16 head_offset = mlx5e_shampo_hd_offset(header_index) + rq->buff.headroom;
1214 
1215 	return page_address(frag_page->page) + head_offset;
1216 }
1217 
mlx5e_shampo_update_ipv4_udp_hdr(struct mlx5e_rq * rq,struct iphdr * ipv4)1218 static void mlx5e_shampo_update_ipv4_udp_hdr(struct mlx5e_rq *rq, struct iphdr *ipv4)
1219 {
1220 	int udp_off = rq->hw_gro_data->fk.control.thoff;
1221 	struct sk_buff *skb = rq->hw_gro_data->skb;
1222 	struct udphdr *uh;
1223 
1224 	uh = (struct udphdr *)(skb->data + udp_off);
1225 	uh->len = htons(skb->len - udp_off);
1226 
1227 	if (uh->check)
1228 		uh->check = ~udp_v4_check(skb->len - udp_off, ipv4->saddr,
1229 					  ipv4->daddr, 0);
1230 
1231 	skb->csum_start = (unsigned char *)uh - skb->head;
1232 	skb->csum_offset = offsetof(struct udphdr, check);
1233 
1234 	skb_shinfo(skb)->gso_type |= SKB_GSO_UDP_L4;
1235 }
1236 
mlx5e_shampo_update_ipv6_udp_hdr(struct mlx5e_rq * rq,struct ipv6hdr * ipv6)1237 static void mlx5e_shampo_update_ipv6_udp_hdr(struct mlx5e_rq *rq, struct ipv6hdr *ipv6)
1238 {
1239 	int udp_off = rq->hw_gro_data->fk.control.thoff;
1240 	struct sk_buff *skb = rq->hw_gro_data->skb;
1241 	struct udphdr *uh;
1242 
1243 	uh = (struct udphdr *)(skb->data + udp_off);
1244 	uh->len = htons(skb->len - udp_off);
1245 
1246 	if (uh->check)
1247 		uh->check = ~udp_v6_check(skb->len - udp_off, &ipv6->saddr,
1248 					  &ipv6->daddr, 0);
1249 
1250 	skb->csum_start = (unsigned char *)uh - skb->head;
1251 	skb->csum_offset = offsetof(struct udphdr, check);
1252 
1253 	skb_shinfo(skb)->gso_type |= SKB_GSO_UDP_L4;
1254 }
1255 
mlx5e_shampo_update_fin_psh_flags(struct mlx5e_rq * rq,struct mlx5_cqe64 * cqe,struct tcphdr * skb_tcp_hd)1256 static void mlx5e_shampo_update_fin_psh_flags(struct mlx5e_rq *rq, struct mlx5_cqe64 *cqe,
1257 					      struct tcphdr *skb_tcp_hd)
1258 {
1259 	u16 header_index = mlx5e_shampo_get_cqe_header_index(rq, cqe);
1260 	struct tcphdr *last_tcp_hd;
1261 	void *last_hd_addr;
1262 
1263 	last_hd_addr = mlx5e_shampo_get_packet_hd(rq, header_index);
1264 	last_tcp_hd =  last_hd_addr + ETH_HLEN + rq->hw_gro_data->fk.control.thoff;
1265 	tcp_flag_word(skb_tcp_hd) |= tcp_flag_word(last_tcp_hd) & (TCP_FLAG_FIN | TCP_FLAG_PSH);
1266 }
1267 
mlx5e_shampo_update_ipv4_tcp_hdr(struct mlx5e_rq * rq,struct iphdr * ipv4,struct mlx5_cqe64 * cqe,bool match)1268 static void mlx5e_shampo_update_ipv4_tcp_hdr(struct mlx5e_rq *rq, struct iphdr *ipv4,
1269 					     struct mlx5_cqe64 *cqe, bool match)
1270 {
1271 	int tcp_off = rq->hw_gro_data->fk.control.thoff;
1272 	struct sk_buff *skb = rq->hw_gro_data->skb;
1273 	struct tcphdr *tcp;
1274 
1275 	tcp = (struct tcphdr *)(skb->data + tcp_off);
1276 	if (match)
1277 		mlx5e_shampo_update_fin_psh_flags(rq, cqe, tcp);
1278 
1279 	tcp->check = ~tcp_v4_check(skb->len - tcp_off, ipv4->saddr,
1280 				   ipv4->daddr, 0);
1281 	skb_shinfo(skb)->gso_type |= SKB_GSO_TCPV4;
1282 	if (ntohs(ipv4->id) == rq->hw_gro_data->second_ip_id)
1283 		skb_shinfo(skb)->gso_type |= SKB_GSO_TCP_FIXEDID;
1284 
1285 	skb->csum_start = (unsigned char *)tcp - skb->head;
1286 	skb->csum_offset = offsetof(struct tcphdr, check);
1287 
1288 	if (tcp->cwr)
1289 		skb_shinfo(skb)->gso_type |= SKB_GSO_TCP_ECN;
1290 }
1291 
mlx5e_shampo_update_ipv6_tcp_hdr(struct mlx5e_rq * rq,struct ipv6hdr * ipv6,struct mlx5_cqe64 * cqe,bool match)1292 static void mlx5e_shampo_update_ipv6_tcp_hdr(struct mlx5e_rq *rq, struct ipv6hdr *ipv6,
1293 					     struct mlx5_cqe64 *cqe, bool match)
1294 {
1295 	int tcp_off = rq->hw_gro_data->fk.control.thoff;
1296 	struct sk_buff *skb = rq->hw_gro_data->skb;
1297 	struct tcphdr *tcp;
1298 
1299 	tcp = (struct tcphdr *)(skb->data + tcp_off);
1300 	if (match)
1301 		mlx5e_shampo_update_fin_psh_flags(rq, cqe, tcp);
1302 
1303 	tcp->check = ~tcp_v6_check(skb->len - tcp_off, &ipv6->saddr,
1304 				   &ipv6->daddr, 0);
1305 	skb_shinfo(skb)->gso_type |= SKB_GSO_TCPV6;
1306 	skb->csum_start = (unsigned char *)tcp - skb->head;
1307 	skb->csum_offset = offsetof(struct tcphdr, check);
1308 
1309 	if (tcp->cwr)
1310 		skb_shinfo(skb)->gso_type |= SKB_GSO_TCP_ECN;
1311 }
1312 
mlx5e_shampo_update_hdr(struct mlx5e_rq * rq,struct mlx5_cqe64 * cqe,bool match)1313 static void mlx5e_shampo_update_hdr(struct mlx5e_rq *rq, struct mlx5_cqe64 *cqe, bool match)
1314 {
1315 	bool is_ipv4 = (rq->hw_gro_data->fk.basic.n_proto == htons(ETH_P_IP));
1316 	struct sk_buff *skb = rq->hw_gro_data->skb;
1317 
1318 	skb_shinfo(skb)->gso_segs = NAPI_GRO_CB(skb)->count;
1319 	skb->ip_summed = CHECKSUM_PARTIAL;
1320 
1321 	if (is_ipv4) {
1322 		int nhoff = rq->hw_gro_data->fk.control.thoff - sizeof(struct iphdr);
1323 		struct iphdr *ipv4 = (struct iphdr *)(skb->data + nhoff);
1324 		__be16 newlen = htons(skb->len - nhoff);
1325 
1326 		csum_replace2(&ipv4->check, ipv4->tot_len, newlen);
1327 		ipv4->tot_len = newlen;
1328 
1329 		if (ipv4->protocol == IPPROTO_TCP)
1330 			mlx5e_shampo_update_ipv4_tcp_hdr(rq, ipv4, cqe, match);
1331 		else
1332 			mlx5e_shampo_update_ipv4_udp_hdr(rq, ipv4);
1333 	} else {
1334 		int nhoff = rq->hw_gro_data->fk.control.thoff - sizeof(struct ipv6hdr);
1335 		struct ipv6hdr *ipv6 = (struct ipv6hdr *)(skb->data + nhoff);
1336 
1337 		ipv6->payload_len = htons(skb->len - nhoff - sizeof(*ipv6));
1338 
1339 		if (ipv6->nexthdr == IPPROTO_TCP)
1340 			mlx5e_shampo_update_ipv6_tcp_hdr(rq, ipv6, cqe, match);
1341 		else
1342 			mlx5e_shampo_update_ipv6_udp_hdr(rq, ipv6);
1343 	}
1344 }
1345 
mlx5e_skb_set_hash(struct mlx5_cqe64 * cqe,struct sk_buff * skb)1346 static inline void mlx5e_skb_set_hash(struct mlx5_cqe64 *cqe,
1347 				      struct sk_buff *skb)
1348 {
1349 	u8 cht = cqe->rss_hash_type;
1350 	int ht = (cht & CQE_RSS_HTYPE_L4) ? PKT_HASH_TYPE_L4 :
1351 		 (cht & CQE_RSS_HTYPE_IP) ? PKT_HASH_TYPE_L3 :
1352 					    PKT_HASH_TYPE_NONE;
1353 	skb_set_hash(skb, be32_to_cpu(cqe->rss_hash_result), ht);
1354 }
1355 
is_last_ethertype_ip(struct sk_buff * skb,int * network_depth,__be16 * proto)1356 static inline bool is_last_ethertype_ip(struct sk_buff *skb, int *network_depth,
1357 					__be16 *proto)
1358 {
1359 	*proto = ((struct ethhdr *)skb->data)->h_proto;
1360 	*proto = __vlan_get_protocol(skb, *proto, network_depth);
1361 
1362 	if (*proto == htons(ETH_P_IP))
1363 		return pskb_may_pull(skb, *network_depth + sizeof(struct iphdr));
1364 
1365 	if (*proto == htons(ETH_P_IPV6))
1366 		return pskb_may_pull(skb, *network_depth + sizeof(struct ipv6hdr));
1367 
1368 	return false;
1369 }
1370 
mlx5e_enable_ecn(struct mlx5e_rq * rq,struct sk_buff * skb)1371 static inline void mlx5e_enable_ecn(struct mlx5e_rq *rq, struct sk_buff *skb)
1372 {
1373 	int network_depth = 0;
1374 	__be16 proto;
1375 	void *ip;
1376 	int rc;
1377 
1378 	if (unlikely(!is_last_ethertype_ip(skb, &network_depth, &proto)))
1379 		return;
1380 
1381 	ip = skb->data + network_depth;
1382 	rc = ((proto == htons(ETH_P_IP)) ? IP_ECN_set_ce((struct iphdr *)ip) :
1383 					 IP6_ECN_set_ce(skb, (struct ipv6hdr *)ip));
1384 
1385 	rq->stats->ecn_mark += !!rc;
1386 }
1387 
get_ip_proto(struct sk_buff * skb,int network_depth,__be16 proto)1388 static u8 get_ip_proto(struct sk_buff *skb, int network_depth, __be16 proto)
1389 {
1390 	void *ip_p = skb->data + network_depth;
1391 
1392 	return (proto == htons(ETH_P_IP)) ? ((struct iphdr *)ip_p)->protocol :
1393 					    ((struct ipv6hdr *)ip_p)->nexthdr;
1394 }
1395 
1396 #define short_frame(size) ((size) <= ETH_ZLEN + ETH_FCS_LEN)
1397 
1398 #define MAX_PADDING 8
1399 
1400 static void
tail_padding_csum_slow(struct sk_buff * skb,int offset,int len,struct mlx5e_rq_stats * stats)1401 tail_padding_csum_slow(struct sk_buff *skb, int offset, int len,
1402 		       struct mlx5e_rq_stats *stats)
1403 {
1404 	stats->csum_complete_tail_slow++;
1405 	skb->csum = csum_block_add(skb->csum,
1406 				   skb_checksum(skb, offset, len, 0),
1407 				   offset);
1408 }
1409 
1410 static void
tail_padding_csum(struct sk_buff * skb,int offset,struct mlx5e_rq_stats * stats)1411 tail_padding_csum(struct sk_buff *skb, int offset,
1412 		  struct mlx5e_rq_stats *stats)
1413 {
1414 	u8 tail_padding[MAX_PADDING];
1415 	int len = skb->len - offset;
1416 	void *tail;
1417 
1418 	if (unlikely(len > MAX_PADDING)) {
1419 		tail_padding_csum_slow(skb, offset, len, stats);
1420 		return;
1421 	}
1422 
1423 	tail = skb_header_pointer(skb, offset, len, tail_padding);
1424 	if (unlikely(!tail)) {
1425 		tail_padding_csum_slow(skb, offset, len, stats);
1426 		return;
1427 	}
1428 
1429 	stats->csum_complete_tail++;
1430 	skb->csum = csum_block_add(skb->csum, csum_partial(tail, len, 0), offset);
1431 }
1432 
1433 static void
mlx5e_skb_csum_fixup(struct sk_buff * skb,int network_depth,__be16 proto,struct mlx5e_rq_stats * stats)1434 mlx5e_skb_csum_fixup(struct sk_buff *skb, int network_depth, __be16 proto,
1435 		     struct mlx5e_rq_stats *stats)
1436 {
1437 	struct ipv6hdr *ip6;
1438 	struct iphdr   *ip4;
1439 	int pkt_len;
1440 
1441 	/* Fixup vlan headers, if any */
1442 	if (network_depth > ETH_HLEN)
1443 		/* CQE csum is calculated from the IP header and does
1444 		 * not cover VLAN headers (if present). This will add
1445 		 * the checksum manually.
1446 		 */
1447 		skb->csum = csum_partial(skb->data + ETH_HLEN,
1448 					 network_depth - ETH_HLEN,
1449 					 skb->csum);
1450 
1451 	/* Fixup tail padding, if any */
1452 	switch (proto) {
1453 	case htons(ETH_P_IP):
1454 		ip4 = (struct iphdr *)(skb->data + network_depth);
1455 		pkt_len = network_depth + ntohs(ip4->tot_len);
1456 		break;
1457 	case htons(ETH_P_IPV6):
1458 		ip6 = (struct ipv6hdr *)(skb->data + network_depth);
1459 		pkt_len = network_depth + sizeof(*ip6) + ntohs(ip6->payload_len);
1460 		break;
1461 	default:
1462 		return;
1463 	}
1464 
1465 	if (likely(pkt_len >= skb->len))
1466 		return;
1467 
1468 	tail_padding_csum(skb, pkt_len, stats);
1469 }
1470 
mlx5e_handle_csum(struct net_device * netdev,struct mlx5_cqe64 * cqe,struct mlx5e_rq * rq,struct sk_buff * skb,bool lro)1471 static inline void mlx5e_handle_csum(struct net_device *netdev,
1472 				     struct mlx5_cqe64 *cqe,
1473 				     struct mlx5e_rq *rq,
1474 				     struct sk_buff *skb,
1475 				     bool   lro)
1476 {
1477 	struct mlx5e_rq_stats *stats = rq->stats;
1478 	int network_depth = 0;
1479 	__be16 proto;
1480 
1481 	if (unlikely(!(netdev->features & NETIF_F_RXCSUM)))
1482 		goto csum_none;
1483 
1484 	if (lro) {
1485 		skb->ip_summed = CHECKSUM_UNNECESSARY;
1486 		stats->csum_unnecessary++;
1487 		return;
1488 	}
1489 
1490 	/* True when explicitly set via priv flag, or XDP prog is loaded */
1491 	if (test_bit(MLX5E_RQ_STATE_NO_CSUM_COMPLETE, &rq->state) ||
1492 	    get_cqe_tls_offload(cqe))
1493 		goto csum_unnecessary;
1494 
1495 	/* CQE csum doesn't cover padding octets in short ethernet
1496 	 * frames. And the pad field is appended prior to calculating
1497 	 * and appending the FCS field.
1498 	 *
1499 	 * Detecting these padded frames requires to verify and parse
1500 	 * IP headers, so we simply force all those small frames to be
1501 	 * CHECKSUM_UNNECESSARY even if they are not padded.
1502 	 */
1503 	if (short_frame(skb->len))
1504 		goto csum_unnecessary;
1505 
1506 	if (likely(is_last_ethertype_ip(skb, &network_depth, &proto))) {
1507 		if (unlikely(get_ip_proto(skb, network_depth, proto) == IPPROTO_SCTP))
1508 			goto csum_unnecessary;
1509 
1510 		stats->csum_complete++;
1511 		skb->ip_summed = CHECKSUM_COMPLETE;
1512 		skb->csum = csum_unfold((__force __sum16)cqe->check_sum);
1513 
1514 		if (test_bit(MLX5E_RQ_STATE_CSUM_FULL, &rq->state))
1515 			return; /* CQE csum covers all received bytes */
1516 
1517 		/* csum might need some fixups ...*/
1518 		mlx5e_skb_csum_fixup(skb, network_depth, proto, stats);
1519 		return;
1520 	}
1521 
1522 csum_unnecessary:
1523 	if (likely((cqe->hds_ip_ext & CQE_L3_OK) &&
1524 		   (cqe->hds_ip_ext & CQE_L4_OK))) {
1525 		skb->ip_summed = CHECKSUM_UNNECESSARY;
1526 		if (cqe_is_tunneled(cqe)) {
1527 			skb->csum_level = 1;
1528 			skb->encapsulation = 1;
1529 			stats->csum_unnecessary_inner++;
1530 			return;
1531 		}
1532 		stats->csum_unnecessary++;
1533 		return;
1534 	}
1535 csum_none:
1536 	skb->ip_summed = CHECKSUM_NONE;
1537 	stats->csum_none++;
1538 }
1539 
1540 #define MLX5E_CE_BIT_MASK 0x80
1541 
mlx5e_build_rx_skb(struct mlx5_cqe64 * cqe,u32 cqe_bcnt,struct mlx5e_rq * rq,struct sk_buff * skb)1542 static inline void mlx5e_build_rx_skb(struct mlx5_cqe64 *cqe,
1543 				      u32 cqe_bcnt,
1544 				      struct mlx5e_rq *rq,
1545 				      struct sk_buff *skb)
1546 {
1547 	u8 lro_num_seg = be32_to_cpu(cqe->srqn) >> 24;
1548 	struct mlx5e_rq_stats *stats = rq->stats;
1549 	struct net_device *netdev = rq->netdev;
1550 
1551 	skb->mac_len = ETH_HLEN;
1552 
1553 	if (unlikely(get_cqe_tls_offload(cqe)))
1554 		mlx5e_ktls_handle_rx_skb(rq, skb, cqe, &cqe_bcnt);
1555 
1556 	if (unlikely(mlx5_ipsec_is_rx_flow(cqe)))
1557 		mlx5e_ipsec_offload_handle_rx_skb(netdev, skb,
1558 						  be32_to_cpu(cqe->ft_metadata));
1559 
1560 	if (unlikely(mlx5e_macsec_is_rx_flow(cqe)))
1561 		mlx5e_macsec_offload_handle_rx_skb(netdev, skb, cqe);
1562 
1563 	if (lro_num_seg > 1) {
1564 		mlx5e_lro_update_hdr(skb, cqe, cqe_bcnt);
1565 		skb_shinfo(skb)->gso_size = DIV_ROUND_UP(cqe_bcnt, lro_num_seg);
1566 		/* Subtract one since we already counted this as one
1567 		 * "regular" packet in mlx5e_complete_rx_cqe()
1568 		 */
1569 		stats->packets += lro_num_seg - 1;
1570 		stats->lro_packets++;
1571 		stats->lro_bytes += cqe_bcnt;
1572 	}
1573 
1574 	if (unlikely(mlx5e_rx_hw_stamp(rq->tstamp)))
1575 		skb_hwtstamps(skb)->hwtstamp = mlx5e_cqe_ts_to_ns(rq->ptp_cyc2time,
1576 								  rq->clock, get_cqe_ts(cqe));
1577 	skb_record_rx_queue(skb, rq->ix);
1578 
1579 	if (likely(netdev->features & NETIF_F_RXHASH))
1580 		mlx5e_skb_set_hash(cqe, skb);
1581 
1582 	if (cqe_has_vlan(cqe)) {
1583 		__vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q),
1584 				       be16_to_cpu(cqe->vlan_info));
1585 		stats->removed_vlan_packets++;
1586 	}
1587 
1588 	skb->mark = be32_to_cpu(cqe->sop_drop_qpn) & MLX5E_TC_FLOW_ID_MASK;
1589 
1590 	mlx5e_handle_csum(netdev, cqe, rq, skb, !!lro_num_seg);
1591 	/* checking CE bit in cqe - MSB in ml_path field */
1592 	if (unlikely(cqe->ml_path & MLX5E_CE_BIT_MASK))
1593 		mlx5e_enable_ecn(rq, skb);
1594 
1595 	skb->protocol = eth_type_trans(skb, netdev);
1596 
1597 	if (unlikely(mlx5e_skb_is_multicast(skb)))
1598 		stats->mcast_packets++;
1599 }
1600 
mlx5e_shampo_complete_rx_cqe(struct mlx5e_rq * rq,struct mlx5_cqe64 * cqe,u32 cqe_bcnt,struct sk_buff * skb)1601 static void mlx5e_shampo_complete_rx_cqe(struct mlx5e_rq *rq,
1602 					 struct mlx5_cqe64 *cqe,
1603 					 u32 cqe_bcnt,
1604 					 struct sk_buff *skb)
1605 {
1606 	struct mlx5e_rq_stats *stats = rq->stats;
1607 
1608 	stats->packets++;
1609 	stats->bytes += cqe_bcnt;
1610 	if (NAPI_GRO_CB(skb)->count != 1)
1611 		return;
1612 	mlx5e_build_rx_skb(cqe, cqe_bcnt, rq, skb);
1613 	skb_reset_network_header(skb);
1614 	if (!skb_flow_dissect_flow_keys(skb, &rq->hw_gro_data->fk, 0)) {
1615 		napi_gro_receive(rq->cq.napi, skb);
1616 		rq->hw_gro_data->skb = NULL;
1617 	}
1618 }
1619 
mlx5e_complete_rx_cqe(struct mlx5e_rq * rq,struct mlx5_cqe64 * cqe,u32 cqe_bcnt,struct sk_buff * skb)1620 static inline void mlx5e_complete_rx_cqe(struct mlx5e_rq *rq,
1621 					 struct mlx5_cqe64 *cqe,
1622 					 u32 cqe_bcnt,
1623 					 struct sk_buff *skb)
1624 {
1625 	struct mlx5e_rq_stats *stats = rq->stats;
1626 
1627 	stats->packets++;
1628 	stats->bytes += cqe_bcnt;
1629 	mlx5e_build_rx_skb(cqe, cqe_bcnt, rq, skb);
1630 }
1631 
1632 static inline
mlx5e_build_linear_skb(struct mlx5e_rq * rq,void * va,u32 frag_size,u16 headroom,u32 cqe_bcnt,u32 metasize)1633 struct sk_buff *mlx5e_build_linear_skb(struct mlx5e_rq *rq, void *va,
1634 				       u32 frag_size, u16 headroom,
1635 				       u32 cqe_bcnt, u32 metasize)
1636 {
1637 	struct sk_buff *skb = napi_build_skb(va, frag_size);
1638 
1639 	if (unlikely(!skb)) {
1640 		rq->stats->buff_alloc_err++;
1641 		return NULL;
1642 	}
1643 
1644 	skb_reserve(skb, headroom);
1645 	skb_put(skb, cqe_bcnt);
1646 
1647 	if (metasize)
1648 		skb_metadata_set(skb, metasize);
1649 
1650 	return skb;
1651 }
1652 
mlx5e_fill_mxbuf(struct mlx5e_rq * rq,struct mlx5_cqe64 * cqe,void * va,u16 headroom,u32 frame_sz,u32 len,struct mlx5e_xdp_buff * mxbuf)1653 static void mlx5e_fill_mxbuf(struct mlx5e_rq *rq, struct mlx5_cqe64 *cqe,
1654 			     void *va, u16 headroom, u32 frame_sz, u32 len,
1655 			     struct mlx5e_xdp_buff *mxbuf)
1656 {
1657 	xdp_init_buff(&mxbuf->xdp, frame_sz, &rq->xdp_rxq);
1658 	xdp_prepare_buff(&mxbuf->xdp, va, headroom, len, true);
1659 	mxbuf->cqe = cqe;
1660 	mxbuf->rq = rq;
1661 }
1662 
1663 static struct sk_buff *
mlx5e_skb_from_cqe_linear(struct mlx5e_rq * rq,struct mlx5e_wqe_frag_info * wi,struct mlx5_cqe64 * cqe,u32 cqe_bcnt)1664 mlx5e_skb_from_cqe_linear(struct mlx5e_rq *rq, struct mlx5e_wqe_frag_info *wi,
1665 			  struct mlx5_cqe64 *cqe, u32 cqe_bcnt)
1666 {
1667 	struct mlx5e_frag_page *frag_page = wi->frag_page;
1668 	u16 rx_headroom = rq->buff.headroom;
1669 	struct bpf_prog *prog;
1670 	struct sk_buff *skb;
1671 	u32 metasize = 0;
1672 	void *va, *data;
1673 	dma_addr_t addr;
1674 	u32 frag_size;
1675 
1676 	va             = page_address(frag_page->page) + wi->offset;
1677 	data           = va + rx_headroom;
1678 	frag_size      = MLX5_SKB_FRAG_SZ(rx_headroom + cqe_bcnt);
1679 
1680 	addr = page_pool_get_dma_addr(frag_page->page);
1681 	dma_sync_single_range_for_cpu(rq->pdev, addr, wi->offset,
1682 				      frag_size, rq->buff.map_dir);
1683 	net_prefetch(data);
1684 
1685 	prog = rcu_dereference(rq->xdp_prog);
1686 	if (prog) {
1687 		struct mlx5e_xdp_buff mxbuf;
1688 
1689 		net_prefetchw(va); /* xdp_frame data area */
1690 		mlx5e_fill_mxbuf(rq, cqe, va, rx_headroom, rq->buff.frame0_sz,
1691 				 cqe_bcnt, &mxbuf);
1692 		if (mlx5e_xdp_handle(rq, prog, &mxbuf))
1693 			return NULL; /* page/packet was consumed by XDP */
1694 
1695 		rx_headroom = mxbuf.xdp.data - mxbuf.xdp.data_hard_start;
1696 		metasize = mxbuf.xdp.data - mxbuf.xdp.data_meta;
1697 		cqe_bcnt = mxbuf.xdp.data_end - mxbuf.xdp.data;
1698 	}
1699 	frag_size = MLX5_SKB_FRAG_SZ(rx_headroom + cqe_bcnt);
1700 	skb = mlx5e_build_linear_skb(rq, va, frag_size, rx_headroom, cqe_bcnt, metasize);
1701 	if (unlikely(!skb))
1702 		return NULL;
1703 
1704 	/* queue up for recycling/reuse */
1705 	skb_mark_for_recycle(skb);
1706 	frag_page->frags++;
1707 
1708 	return skb;
1709 }
1710 
1711 static struct sk_buff *
mlx5e_skb_from_cqe_nonlinear(struct mlx5e_rq * rq,struct mlx5e_wqe_frag_info * wi,struct mlx5_cqe64 * cqe,u32 cqe_bcnt)1712 mlx5e_skb_from_cqe_nonlinear(struct mlx5e_rq *rq, struct mlx5e_wqe_frag_info *wi,
1713 			     struct mlx5_cqe64 *cqe, u32 cqe_bcnt)
1714 {
1715 	struct mlx5e_rq_frag_info *frag_info = &rq->wqe.info.arr[0];
1716 	struct mlx5e_wqe_frag_info *head_wi = wi;
1717 	u16 rx_headroom = rq->buff.headroom;
1718 	struct mlx5e_frag_page *frag_page;
1719 	struct skb_shared_info *sinfo;
1720 	struct mlx5e_xdp_buff mxbuf;
1721 	u32 frag_consumed_bytes;
1722 	struct bpf_prog *prog;
1723 	struct sk_buff *skb;
1724 	dma_addr_t addr;
1725 	u32 truesize;
1726 	void *va;
1727 
1728 	frag_page = wi->frag_page;
1729 
1730 	va = page_address(frag_page->page) + wi->offset;
1731 	frag_consumed_bytes = min_t(u32, frag_info->frag_size, cqe_bcnt);
1732 
1733 	addr = page_pool_get_dma_addr(frag_page->page);
1734 	dma_sync_single_range_for_cpu(rq->pdev, addr, wi->offset,
1735 				      rq->buff.frame0_sz, rq->buff.map_dir);
1736 	net_prefetchw(va); /* xdp_frame data area */
1737 	net_prefetch(va + rx_headroom);
1738 
1739 	mlx5e_fill_mxbuf(rq, cqe, va, rx_headroom, rq->buff.frame0_sz,
1740 			 frag_consumed_bytes, &mxbuf);
1741 	sinfo = xdp_get_shared_info_from_buff(&mxbuf.xdp);
1742 	truesize = 0;
1743 
1744 	cqe_bcnt -= frag_consumed_bytes;
1745 	frag_info++;
1746 	wi++;
1747 
1748 	while (cqe_bcnt) {
1749 		frag_page = wi->frag_page;
1750 
1751 		frag_consumed_bytes = min_t(u32, frag_info->frag_size, cqe_bcnt);
1752 
1753 		mlx5e_add_skb_shared_info_frag(rq, sinfo, &mxbuf.xdp, frag_page,
1754 					       wi->offset, frag_consumed_bytes);
1755 		truesize += frag_info->frag_stride;
1756 
1757 		cqe_bcnt -= frag_consumed_bytes;
1758 		frag_info++;
1759 		wi++;
1760 	}
1761 
1762 	prog = rcu_dereference(rq->xdp_prog);
1763 	if (prog && mlx5e_xdp_handle(rq, prog, &mxbuf)) {
1764 		if (__test_and_clear_bit(MLX5E_RQ_FLAG_XDP_XMIT, rq->flags)) {
1765 			struct mlx5e_wqe_frag_info *pwi;
1766 
1767 			for (pwi = head_wi; pwi < wi; pwi++)
1768 				pwi->frag_page->frags++;
1769 		}
1770 		return NULL; /* page/packet was consumed by XDP */
1771 	}
1772 
1773 	skb = mlx5e_build_linear_skb(rq, mxbuf.xdp.data_hard_start, rq->buff.frame0_sz,
1774 				     mxbuf.xdp.data - mxbuf.xdp.data_hard_start,
1775 				     mxbuf.xdp.data_end - mxbuf.xdp.data,
1776 				     mxbuf.xdp.data - mxbuf.xdp.data_meta);
1777 	if (unlikely(!skb))
1778 		return NULL;
1779 
1780 	skb_mark_for_recycle(skb);
1781 	head_wi->frag_page->frags++;
1782 
1783 	if (xdp_buff_has_frags(&mxbuf.xdp)) {
1784 		/* sinfo->nr_frags is reset by build_skb, calculate again. */
1785 		xdp_update_skb_shared_info(skb, wi - head_wi - 1,
1786 					   sinfo->xdp_frags_size, truesize,
1787 					   xdp_buff_is_frag_pfmemalloc(&mxbuf.xdp));
1788 
1789 		for (struct mlx5e_wqe_frag_info *pwi = head_wi + 1; pwi < wi; pwi++)
1790 			pwi->frag_page->frags++;
1791 	}
1792 
1793 	return skb;
1794 }
1795 
trigger_report(struct mlx5e_rq * rq,struct mlx5_cqe64 * cqe)1796 static void trigger_report(struct mlx5e_rq *rq, struct mlx5_cqe64 *cqe)
1797 {
1798 	struct mlx5_err_cqe *err_cqe = (struct mlx5_err_cqe *)cqe;
1799 	struct mlx5e_priv *priv = rq->priv;
1800 
1801 	if (cqe_syndrome_needs_recover(err_cqe->syndrome) &&
1802 	    !test_and_set_bit(MLX5E_RQ_STATE_RECOVERING, &rq->state)) {
1803 		mlx5e_dump_error_cqe(&rq->cq, rq->rqn, err_cqe);
1804 		queue_work(priv->wq, &rq->recover_work);
1805 	}
1806 }
1807 
mlx5e_handle_rx_err_cqe(struct mlx5e_rq * rq,struct mlx5_cqe64 * cqe)1808 static void mlx5e_handle_rx_err_cqe(struct mlx5e_rq *rq, struct mlx5_cqe64 *cqe)
1809 {
1810 	trigger_report(rq, cqe);
1811 	rq->stats->wqe_err++;
1812 }
1813 
mlx5e_handle_rx_cqe(struct mlx5e_rq * rq,struct mlx5_cqe64 * cqe)1814 static void mlx5e_handle_rx_cqe(struct mlx5e_rq *rq, struct mlx5_cqe64 *cqe)
1815 {
1816 	struct mlx5_wq_cyc *wq = &rq->wqe.wq;
1817 	struct mlx5e_wqe_frag_info *wi;
1818 	struct sk_buff *skb;
1819 	u32 cqe_bcnt;
1820 	u16 ci;
1821 
1822 	ci       = mlx5_wq_cyc_ctr2ix(wq, be16_to_cpu(cqe->wqe_counter));
1823 	wi       = get_frag(rq, ci);
1824 	cqe_bcnt = be32_to_cpu(cqe->byte_cnt);
1825 
1826 	if (unlikely(MLX5E_RX_ERR_CQE(cqe))) {
1827 		mlx5e_handle_rx_err_cqe(rq, cqe);
1828 		goto wq_cyc_pop;
1829 	}
1830 
1831 	skb = INDIRECT_CALL_3(rq->wqe.skb_from_cqe,
1832 			      mlx5e_skb_from_cqe_linear,
1833 			      mlx5e_skb_from_cqe_nonlinear,
1834 			      mlx5e_xsk_skb_from_cqe_linear,
1835 			      rq, wi, cqe, cqe_bcnt);
1836 	if (!skb) {
1837 		/* probably for XDP */
1838 		if (__test_and_clear_bit(MLX5E_RQ_FLAG_XDP_XMIT, rq->flags))
1839 			wi->frag_page->frags++;
1840 		goto wq_cyc_pop;
1841 	}
1842 
1843 	mlx5e_complete_rx_cqe(rq, cqe, cqe_bcnt, skb);
1844 
1845 	if (mlx5e_cqe_regb_chain(cqe))
1846 		if (!mlx5e_tc_update_skb_nic(cqe, skb)) {
1847 			dev_kfree_skb_any(skb);
1848 			goto wq_cyc_pop;
1849 		}
1850 
1851 	napi_gro_receive(rq->cq.napi, skb);
1852 
1853 wq_cyc_pop:
1854 	mlx5_wq_cyc_pop(wq);
1855 }
1856 
1857 #ifdef CONFIG_MLX5_ESWITCH
mlx5e_handle_rx_cqe_rep(struct mlx5e_rq * rq,struct mlx5_cqe64 * cqe)1858 static void mlx5e_handle_rx_cqe_rep(struct mlx5e_rq *rq, struct mlx5_cqe64 *cqe)
1859 {
1860 	struct net_device *netdev = rq->netdev;
1861 	struct mlx5e_priv *priv = netdev_priv(netdev);
1862 	struct mlx5e_rep_priv *rpriv  = priv->ppriv;
1863 	struct mlx5_eswitch_rep *rep = rpriv->rep;
1864 	struct mlx5_wq_cyc *wq = &rq->wqe.wq;
1865 	struct mlx5e_wqe_frag_info *wi;
1866 	struct sk_buff *skb;
1867 	u32 cqe_bcnt;
1868 	u16 ci;
1869 
1870 	ci       = mlx5_wq_cyc_ctr2ix(wq, be16_to_cpu(cqe->wqe_counter));
1871 	wi       = get_frag(rq, ci);
1872 	cqe_bcnt = be32_to_cpu(cqe->byte_cnt);
1873 
1874 	if (unlikely(MLX5E_RX_ERR_CQE(cqe))) {
1875 		mlx5e_handle_rx_err_cqe(rq, cqe);
1876 		goto wq_cyc_pop;
1877 	}
1878 
1879 	skb = INDIRECT_CALL_2(rq->wqe.skb_from_cqe,
1880 			      mlx5e_skb_from_cqe_linear,
1881 			      mlx5e_skb_from_cqe_nonlinear,
1882 			      rq, wi, cqe, cqe_bcnt);
1883 	if (!skb) {
1884 		/* probably for XDP */
1885 		if (__test_and_clear_bit(MLX5E_RQ_FLAG_XDP_XMIT, rq->flags))
1886 			wi->frag_page->frags++;
1887 		goto wq_cyc_pop;
1888 	}
1889 
1890 	mlx5e_complete_rx_cqe(rq, cqe, cqe_bcnt, skb);
1891 
1892 	if (rep->vlan && skb_vlan_tag_present(skb))
1893 		skb_vlan_pop(skb);
1894 
1895 	mlx5e_rep_tc_receive(cqe, rq, skb);
1896 
1897 wq_cyc_pop:
1898 	mlx5_wq_cyc_pop(wq);
1899 }
1900 
mlx5e_handle_rx_cqe_mpwrq_rep(struct mlx5e_rq * rq,struct mlx5_cqe64 * cqe)1901 static void mlx5e_handle_rx_cqe_mpwrq_rep(struct mlx5e_rq *rq, struct mlx5_cqe64 *cqe)
1902 {
1903 	u16 cstrides       = mpwrq_get_cqe_consumed_strides(cqe);
1904 	u16 wqe_id         = be16_to_cpu(cqe->wqe_id);
1905 	struct mlx5e_mpw_info *wi = mlx5e_get_mpw_info(rq, wqe_id);
1906 	u16 stride_ix      = mpwrq_get_cqe_stride_index(cqe);
1907 	u32 wqe_offset     = stride_ix << rq->mpwqe.log_stride_sz;
1908 	u32 head_offset    = wqe_offset & ((1 << rq->mpwqe.page_shift) - 1);
1909 	u32 page_idx       = wqe_offset >> rq->mpwqe.page_shift;
1910 	struct mlx5e_rx_wqe_ll *wqe;
1911 	struct mlx5_wq_ll *wq;
1912 	struct sk_buff *skb;
1913 	u16 cqe_bcnt;
1914 
1915 	wi->consumed_strides += cstrides;
1916 
1917 	if (unlikely(MLX5E_RX_ERR_CQE(cqe))) {
1918 		mlx5e_handle_rx_err_cqe(rq, cqe);
1919 		goto mpwrq_cqe_out;
1920 	}
1921 
1922 	if (unlikely(mpwrq_is_filler_cqe(cqe))) {
1923 		struct mlx5e_rq_stats *stats = rq->stats;
1924 
1925 		stats->mpwqe_filler_cqes++;
1926 		stats->mpwqe_filler_strides += cstrides;
1927 		goto mpwrq_cqe_out;
1928 	}
1929 
1930 	cqe_bcnt = mpwrq_get_cqe_byte_cnt(cqe);
1931 
1932 	skb = INDIRECT_CALL_2(rq->mpwqe.skb_from_cqe_mpwrq,
1933 			      mlx5e_skb_from_cqe_mpwrq_linear,
1934 			      mlx5e_skb_from_cqe_mpwrq_nonlinear,
1935 			      rq, wi, cqe, cqe_bcnt, head_offset, page_idx);
1936 	if (!skb)
1937 		goto mpwrq_cqe_out;
1938 
1939 	mlx5e_complete_rx_cqe(rq, cqe, cqe_bcnt, skb);
1940 
1941 	mlx5e_rep_tc_receive(cqe, rq, skb);
1942 
1943 mpwrq_cqe_out:
1944 	if (likely(wi->consumed_strides < rq->mpwqe.num_strides))
1945 		return;
1946 
1947 	wq  = &rq->mpwqe.wq;
1948 	wqe = mlx5_wq_ll_get_wqe(wq, wqe_id);
1949 	mlx5_wq_ll_pop(wq, cqe->wqe_id, &wqe->next.next_wqe_index);
1950 }
1951 
1952 const struct mlx5e_rx_handlers mlx5e_rx_handlers_rep = {
1953 	.handle_rx_cqe       = mlx5e_handle_rx_cqe_rep,
1954 	.handle_rx_cqe_mpwqe = mlx5e_handle_rx_cqe_mpwrq_rep,
1955 };
1956 #endif
1957 
1958 static void
mlx5e_shampo_fill_skb_data(struct sk_buff * skb,struct mlx5e_rq * rq,struct mlx5e_frag_page * frag_page,u32 data_bcnt,u32 data_offset)1959 mlx5e_shampo_fill_skb_data(struct sk_buff *skb, struct mlx5e_rq *rq,
1960 			   struct mlx5e_frag_page *frag_page,
1961 			   u32 data_bcnt, u32 data_offset)
1962 {
1963 	net_prefetchw(skb->data);
1964 
1965 	do {
1966 		/* Non-linear mode, hence non-XSK, which always uses PAGE_SIZE. */
1967 		u32 pg_consumed_bytes = min_t(u32, PAGE_SIZE - data_offset, data_bcnt);
1968 		unsigned int truesize = pg_consumed_bytes;
1969 
1970 		mlx5e_add_skb_frag(rq, skb, frag_page, data_offset,
1971 				   pg_consumed_bytes, truesize);
1972 
1973 		data_bcnt -= pg_consumed_bytes;
1974 		data_offset = 0;
1975 		frag_page++;
1976 	} while (data_bcnt);
1977 }
1978 
1979 static struct sk_buff *
mlx5e_skb_from_cqe_mpwrq_nonlinear(struct mlx5e_rq * rq,struct mlx5e_mpw_info * wi,struct mlx5_cqe64 * cqe,u16 cqe_bcnt,u32 head_offset,u32 page_idx)1980 mlx5e_skb_from_cqe_mpwrq_nonlinear(struct mlx5e_rq *rq, struct mlx5e_mpw_info *wi,
1981 				   struct mlx5_cqe64 *cqe, u16 cqe_bcnt, u32 head_offset,
1982 				   u32 page_idx)
1983 {
1984 	struct mlx5e_frag_page *frag_page = &wi->alloc_units.frag_pages[page_idx];
1985 	u16 headlen = min_t(u16, MLX5E_RX_MAX_HEAD, cqe_bcnt);
1986 	struct mlx5e_frag_page *head_page = frag_page;
1987 	u32 frag_offset    = head_offset;
1988 	u32 byte_cnt       = cqe_bcnt;
1989 	struct skb_shared_info *sinfo;
1990 	struct mlx5e_xdp_buff mxbuf;
1991 	unsigned int truesize = 0;
1992 	struct bpf_prog *prog;
1993 	struct sk_buff *skb;
1994 	u32 linear_frame_sz;
1995 	u16 linear_data_len;
1996 	u16 linear_hr;
1997 	void *va;
1998 
1999 	prog = rcu_dereference(rq->xdp_prog);
2000 
2001 	if (prog) {
2002 		/* area for bpf_xdp_[store|load]_bytes */
2003 		net_prefetchw(page_address(frag_page->page) + frag_offset);
2004 		if (unlikely(mlx5e_page_alloc_fragmented(rq, &wi->linear_page))) {
2005 			rq->stats->buff_alloc_err++;
2006 			return NULL;
2007 		}
2008 		va = page_address(wi->linear_page.page);
2009 		net_prefetchw(va); /* xdp_frame data area */
2010 		linear_hr = XDP_PACKET_HEADROOM;
2011 		linear_data_len = 0;
2012 		linear_frame_sz = MLX5_SKB_FRAG_SZ(linear_hr + MLX5E_RX_MAX_HEAD);
2013 	} else {
2014 		skb = napi_alloc_skb(rq->cq.napi,
2015 				     ALIGN(MLX5E_RX_MAX_HEAD, sizeof(long)));
2016 		if (unlikely(!skb)) {
2017 			rq->stats->buff_alloc_err++;
2018 			return NULL;
2019 		}
2020 		skb_mark_for_recycle(skb);
2021 		va = skb->head;
2022 		net_prefetchw(va); /* xdp_frame data area */
2023 		net_prefetchw(skb->data);
2024 
2025 		frag_offset += headlen;
2026 		byte_cnt -= headlen;
2027 		linear_hr = skb_headroom(skb);
2028 		linear_data_len = headlen;
2029 		linear_frame_sz = MLX5_SKB_FRAG_SZ(skb_end_offset(skb));
2030 		if (unlikely(frag_offset >= PAGE_SIZE)) {
2031 			frag_page++;
2032 			frag_offset -= PAGE_SIZE;
2033 		}
2034 	}
2035 
2036 	mlx5e_fill_mxbuf(rq, cqe, va, linear_hr, linear_frame_sz, linear_data_len, &mxbuf);
2037 
2038 	sinfo = xdp_get_shared_info_from_buff(&mxbuf.xdp);
2039 
2040 	while (byte_cnt) {
2041 		/* Non-linear mode, hence non-XSK, which always uses PAGE_SIZE. */
2042 		u32 pg_consumed_bytes = min_t(u32, PAGE_SIZE - frag_offset, byte_cnt);
2043 
2044 		if (test_bit(MLX5E_RQ_STATE_SHAMPO, &rq->state))
2045 			truesize += pg_consumed_bytes;
2046 		else
2047 			truesize += ALIGN(pg_consumed_bytes, BIT(rq->mpwqe.log_stride_sz));
2048 
2049 		mlx5e_add_skb_shared_info_frag(rq, sinfo, &mxbuf.xdp, frag_page, frag_offset,
2050 					       pg_consumed_bytes);
2051 		byte_cnt -= pg_consumed_bytes;
2052 		frag_offset = 0;
2053 		frag_page++;
2054 	}
2055 
2056 	if (prog) {
2057 		if (mlx5e_xdp_handle(rq, prog, &mxbuf)) {
2058 			if (__test_and_clear_bit(MLX5E_RQ_FLAG_XDP_XMIT, rq->flags)) {
2059 				struct mlx5e_frag_page *pfp;
2060 
2061 				for (pfp = head_page; pfp < frag_page; pfp++)
2062 					pfp->frags++;
2063 
2064 				wi->linear_page.frags++;
2065 			}
2066 			mlx5e_page_release_fragmented(rq, &wi->linear_page);
2067 			return NULL; /* page/packet was consumed by XDP */
2068 		}
2069 
2070 		skb = mlx5e_build_linear_skb(rq, mxbuf.xdp.data_hard_start,
2071 					     linear_frame_sz,
2072 					     mxbuf.xdp.data - mxbuf.xdp.data_hard_start, 0,
2073 					     mxbuf.xdp.data - mxbuf.xdp.data_meta);
2074 		if (unlikely(!skb)) {
2075 			mlx5e_page_release_fragmented(rq, &wi->linear_page);
2076 			return NULL;
2077 		}
2078 
2079 		skb_mark_for_recycle(skb);
2080 		wi->linear_page.frags++;
2081 		mlx5e_page_release_fragmented(rq, &wi->linear_page);
2082 
2083 		if (xdp_buff_has_frags(&mxbuf.xdp)) {
2084 			struct mlx5e_frag_page *pagep;
2085 
2086 			/* sinfo->nr_frags is reset by build_skb, calculate again. */
2087 			xdp_update_skb_shared_info(skb, frag_page - head_page,
2088 						   sinfo->xdp_frags_size, truesize,
2089 						   xdp_buff_is_frag_pfmemalloc(&mxbuf.xdp));
2090 
2091 			pagep = head_page;
2092 			do
2093 				pagep->frags++;
2094 			while (++pagep < frag_page);
2095 		}
2096 		__pskb_pull_tail(skb, headlen);
2097 	} else {
2098 		dma_addr_t addr;
2099 
2100 		if (xdp_buff_has_frags(&mxbuf.xdp)) {
2101 			struct mlx5e_frag_page *pagep;
2102 
2103 			xdp_update_skb_shared_info(skb, sinfo->nr_frags,
2104 						   sinfo->xdp_frags_size, truesize,
2105 						   xdp_buff_is_frag_pfmemalloc(&mxbuf.xdp));
2106 
2107 			pagep = frag_page - sinfo->nr_frags;
2108 			do
2109 				pagep->frags++;
2110 			while (++pagep < frag_page);
2111 		}
2112 		/* copy header */
2113 		addr = page_pool_get_dma_addr(head_page->page);
2114 		mlx5e_copy_skb_header(rq, skb, head_page->page, addr,
2115 				      head_offset, head_offset, headlen);
2116 		/* skb linear part was allocated with headlen and aligned to long */
2117 		skb->tail += headlen;
2118 		skb->len  += headlen;
2119 	}
2120 
2121 	return skb;
2122 }
2123 
2124 static struct sk_buff *
mlx5e_skb_from_cqe_mpwrq_linear(struct mlx5e_rq * rq,struct mlx5e_mpw_info * wi,struct mlx5_cqe64 * cqe,u16 cqe_bcnt,u32 head_offset,u32 page_idx)2125 mlx5e_skb_from_cqe_mpwrq_linear(struct mlx5e_rq *rq, struct mlx5e_mpw_info *wi,
2126 				struct mlx5_cqe64 *cqe, u16 cqe_bcnt, u32 head_offset,
2127 				u32 page_idx)
2128 {
2129 	struct mlx5e_frag_page *frag_page = &wi->alloc_units.frag_pages[page_idx];
2130 	u16 rx_headroom = rq->buff.headroom;
2131 	struct bpf_prog *prog;
2132 	struct sk_buff *skb;
2133 	u32 metasize = 0;
2134 	void *va, *data;
2135 	dma_addr_t addr;
2136 	u32 frag_size;
2137 
2138 	/* Check packet size. Note LRO doesn't use linear SKB */
2139 	if (unlikely(cqe_bcnt > rq->hw_mtu)) {
2140 		rq->stats->oversize_pkts_sw_drop++;
2141 		return NULL;
2142 	}
2143 
2144 	va             = page_address(frag_page->page) + head_offset;
2145 	data           = va + rx_headroom;
2146 	frag_size      = MLX5_SKB_FRAG_SZ(rx_headroom + cqe_bcnt);
2147 
2148 	addr = page_pool_get_dma_addr(frag_page->page);
2149 	dma_sync_single_range_for_cpu(rq->pdev, addr, head_offset,
2150 				      frag_size, rq->buff.map_dir);
2151 	net_prefetch(data);
2152 
2153 	prog = rcu_dereference(rq->xdp_prog);
2154 	if (prog) {
2155 		struct mlx5e_xdp_buff mxbuf;
2156 
2157 		net_prefetchw(va); /* xdp_frame data area */
2158 		mlx5e_fill_mxbuf(rq, cqe, va, rx_headroom, rq->buff.frame0_sz,
2159 				 cqe_bcnt, &mxbuf);
2160 		if (mlx5e_xdp_handle(rq, prog, &mxbuf)) {
2161 			if (__test_and_clear_bit(MLX5E_RQ_FLAG_XDP_XMIT, rq->flags))
2162 				frag_page->frags++;
2163 			return NULL; /* page/packet was consumed by XDP */
2164 		}
2165 
2166 		rx_headroom = mxbuf.xdp.data - mxbuf.xdp.data_hard_start;
2167 		metasize = mxbuf.xdp.data - mxbuf.xdp.data_meta;
2168 		cqe_bcnt = mxbuf.xdp.data_end - mxbuf.xdp.data;
2169 	}
2170 	frag_size = MLX5_SKB_FRAG_SZ(rx_headroom + cqe_bcnt);
2171 	skb = mlx5e_build_linear_skb(rq, va, frag_size, rx_headroom, cqe_bcnt, metasize);
2172 	if (unlikely(!skb))
2173 		return NULL;
2174 
2175 	/* queue up for recycling/reuse */
2176 	skb_mark_for_recycle(skb);
2177 	frag_page->frags++;
2178 
2179 	return skb;
2180 }
2181 
2182 static struct sk_buff *
mlx5e_skb_from_cqe_shampo(struct mlx5e_rq * rq,struct mlx5e_mpw_info * wi,struct mlx5_cqe64 * cqe,u16 header_index)2183 mlx5e_skb_from_cqe_shampo(struct mlx5e_rq *rq, struct mlx5e_mpw_info *wi,
2184 			  struct mlx5_cqe64 *cqe, u16 header_index)
2185 {
2186 	struct mlx5e_frag_page *frag_page = mlx5e_shampo_hd_to_frag_page(rq, header_index);
2187 	dma_addr_t page_dma_addr = page_pool_get_dma_addr(frag_page->page);
2188 	u16 head_offset = mlx5e_shampo_hd_offset(header_index);
2189 	dma_addr_t dma_addr = page_dma_addr + head_offset;
2190 	u16 head_size = cqe->shampo.header_size;
2191 	u16 rx_headroom = rq->buff.headroom;
2192 	struct sk_buff *skb = NULL;
2193 	void *hdr, *data;
2194 	u32 frag_size;
2195 
2196 	hdr		= page_address(frag_page->page) + head_offset;
2197 	data		= hdr + rx_headroom;
2198 	frag_size	= MLX5_SKB_FRAG_SZ(rx_headroom + head_size);
2199 
2200 	if (likely(frag_size <= BIT(MLX5E_SHAMPO_LOG_MAX_HEADER_ENTRY_SIZE))) {
2201 		/* build SKB around header */
2202 		dma_sync_single_range_for_cpu(rq->pdev, dma_addr, 0, frag_size, rq->buff.map_dir);
2203 		net_prefetchw(hdr);
2204 		net_prefetch(data);
2205 		skb = mlx5e_build_linear_skb(rq, hdr, frag_size, rx_headroom, head_size, 0);
2206 		if (unlikely(!skb))
2207 			return NULL;
2208 
2209 		frag_page->frags++;
2210 	} else {
2211 		/* allocate SKB and copy header for large header */
2212 		rq->stats->gro_large_hds++;
2213 		skb = napi_alloc_skb(rq->cq.napi,
2214 				     ALIGN(head_size, sizeof(long)));
2215 		if (unlikely(!skb)) {
2216 			rq->stats->buff_alloc_err++;
2217 			return NULL;
2218 		}
2219 
2220 		net_prefetchw(skb->data);
2221 		mlx5e_copy_skb_header(rq, skb, frag_page->page, dma_addr,
2222 				      head_offset + rx_headroom,
2223 				      rx_headroom, head_size);
2224 		/* skb linear part was allocated with headlen and aligned to long */
2225 		skb->tail += head_size;
2226 		skb->len  += head_size;
2227 	}
2228 
2229 	/* queue up for recycling/reuse */
2230 	skb_mark_for_recycle(skb);
2231 
2232 	return skb;
2233 }
2234 
2235 static void
mlx5e_shampo_align_fragment(struct sk_buff * skb,u8 log_stride_sz)2236 mlx5e_shampo_align_fragment(struct sk_buff *skb, u8 log_stride_sz)
2237 {
2238 	skb_frag_t *last_frag = &skb_shinfo(skb)->frags[skb_shinfo(skb)->nr_frags - 1];
2239 	unsigned int frag_size = skb_frag_size(last_frag);
2240 	unsigned int frag_truesize;
2241 
2242 	frag_truesize = ALIGN(frag_size, BIT(log_stride_sz));
2243 	skb->truesize += frag_truesize - frag_size;
2244 }
2245 
2246 static void
mlx5e_shampo_flush_skb(struct mlx5e_rq * rq,struct mlx5_cqe64 * cqe,bool match)2247 mlx5e_shampo_flush_skb(struct mlx5e_rq *rq, struct mlx5_cqe64 *cqe, bool match)
2248 {
2249 	struct sk_buff *skb = rq->hw_gro_data->skb;
2250 	struct mlx5e_rq_stats *stats = rq->stats;
2251 	u16 gro_count = NAPI_GRO_CB(skb)->count;
2252 
2253 	if (likely(skb_shinfo(skb)->nr_frags))
2254 		mlx5e_shampo_align_fragment(skb, rq->mpwqe.log_stride_sz);
2255 	if (gro_count > 1) {
2256 		stats->gro_skbs++;
2257 		stats->gro_packets += gro_count;
2258 		stats->gro_bytes += skb->data_len + skb_headlen(skb) * gro_count;
2259 
2260 		mlx5e_shampo_update_hdr(rq, cqe, match);
2261 	} else {
2262 		skb_shinfo(skb)->gso_size = 0;
2263 	}
2264 	napi_gro_receive(rq->cq.napi, skb);
2265 	rq->hw_gro_data->skb = NULL;
2266 }
2267 
2268 static bool
mlx5e_hw_gro_skb_has_enough_space(struct sk_buff * skb,u16 data_bcnt)2269 mlx5e_hw_gro_skb_has_enough_space(struct sk_buff *skb, u16 data_bcnt)
2270 {
2271 	int nr_frags = skb_shinfo(skb)->nr_frags;
2272 
2273 	return PAGE_SIZE * nr_frags + data_bcnt <= GRO_LEGACY_MAX_SIZE;
2274 }
2275 
mlx5e_handle_rx_cqe_mpwrq_shampo(struct mlx5e_rq * rq,struct mlx5_cqe64 * cqe)2276 static void mlx5e_handle_rx_cqe_mpwrq_shampo(struct mlx5e_rq *rq, struct mlx5_cqe64 *cqe)
2277 {
2278 	u16 data_bcnt		= mpwrq_get_cqe_byte_cnt(cqe) - cqe->shampo.header_size;
2279 	u16 header_index	= mlx5e_shampo_get_cqe_header_index(rq, cqe);
2280 	u32 wqe_offset		= be32_to_cpu(cqe->shampo.data_offset);
2281 	u16 cstrides		= mpwrq_get_cqe_consumed_strides(cqe);
2282 	u32 data_offset		= wqe_offset & (PAGE_SIZE - 1);
2283 	u32 cqe_bcnt		= mpwrq_get_cqe_byte_cnt(cqe);
2284 	u16 wqe_id		= be16_to_cpu(cqe->wqe_id);
2285 	u32 page_idx		= wqe_offset >> PAGE_SHIFT;
2286 	u16 head_size		= cqe->shampo.header_size;
2287 	struct sk_buff **skb	= &rq->hw_gro_data->skb;
2288 	bool flush		= cqe->shampo.flush;
2289 	bool match		= cqe->shampo.match;
2290 	struct mlx5e_rq_stats *stats = rq->stats;
2291 	struct mlx5e_rx_wqe_ll *wqe;
2292 	struct mlx5e_mpw_info *wi;
2293 	struct mlx5_wq_ll *wq;
2294 
2295 	wi = mlx5e_get_mpw_info(rq, wqe_id);
2296 	wi->consumed_strides += cstrides;
2297 
2298 	if (unlikely(MLX5E_RX_ERR_CQE(cqe))) {
2299 		mlx5e_handle_rx_err_cqe(rq, cqe);
2300 		goto mpwrq_cqe_out;
2301 	}
2302 
2303 	if (unlikely(mpwrq_is_filler_cqe(cqe))) {
2304 		stats->mpwqe_filler_cqes++;
2305 		stats->mpwqe_filler_strides += cstrides;
2306 		goto mpwrq_cqe_out;
2307 	}
2308 
2309 	if (*skb && (!match || !(mlx5e_hw_gro_skb_has_enough_space(*skb, data_bcnt)))) {
2310 		match = false;
2311 		mlx5e_shampo_flush_skb(rq, cqe, match);
2312 	}
2313 
2314 	if (!*skb) {
2315 		if (likely(head_size))
2316 			*skb = mlx5e_skb_from_cqe_shampo(rq, wi, cqe, header_index);
2317 		else
2318 			*skb = mlx5e_skb_from_cqe_mpwrq_nonlinear(rq, wi, cqe, cqe_bcnt,
2319 								  data_offset, page_idx);
2320 		if (unlikely(!*skb))
2321 			goto free_hd_entry;
2322 
2323 		NAPI_GRO_CB(*skb)->count = 1;
2324 		skb_shinfo(*skb)->gso_size = cqe_bcnt - head_size;
2325 	} else {
2326 		NAPI_GRO_CB(*skb)->count++;
2327 		if (NAPI_GRO_CB(*skb)->count == 2 &&
2328 		    rq->hw_gro_data->fk.basic.n_proto == htons(ETH_P_IP)) {
2329 			void *hd_addr = mlx5e_shampo_get_packet_hd(rq, header_index);
2330 			int nhoff = ETH_HLEN + rq->hw_gro_data->fk.control.thoff -
2331 				    sizeof(struct iphdr);
2332 			struct iphdr *iph = (struct iphdr *)(hd_addr + nhoff);
2333 
2334 			rq->hw_gro_data->second_ip_id = ntohs(iph->id);
2335 		}
2336 	}
2337 
2338 	if (likely(head_size)) {
2339 		if (data_bcnt) {
2340 			struct mlx5e_frag_page *frag_page;
2341 
2342 			frag_page = &wi->alloc_units.frag_pages[page_idx];
2343 			mlx5e_shampo_fill_skb_data(*skb, rq, frag_page, data_bcnt, data_offset);
2344 		} else {
2345 			stats->hds_nodata_packets++;
2346 			stats->hds_nodata_bytes += head_size;
2347 		}
2348 	} else {
2349 		stats->hds_nosplit_packets++;
2350 		stats->hds_nosplit_bytes += data_bcnt;
2351 	}
2352 
2353 	mlx5e_shampo_complete_rx_cqe(rq, cqe, cqe_bcnt, *skb);
2354 	if (flush && rq->hw_gro_data->skb)
2355 		mlx5e_shampo_flush_skb(rq, cqe, match);
2356 free_hd_entry:
2357 	if (likely(head_size))
2358 		mlx5e_free_rx_shampo_hd_entry(rq, header_index);
2359 mpwrq_cqe_out:
2360 	if (likely(wi->consumed_strides < rq->mpwqe.num_strides))
2361 		return;
2362 
2363 	if (unlikely(!cstrides))
2364 		return;
2365 
2366 	wq  = &rq->mpwqe.wq;
2367 	wqe = mlx5_wq_ll_get_wqe(wq, wqe_id);
2368 	mlx5_wq_ll_pop(wq, cqe->wqe_id, &wqe->next.next_wqe_index);
2369 }
2370 
mlx5e_handle_rx_cqe_mpwrq(struct mlx5e_rq * rq,struct mlx5_cqe64 * cqe)2371 static void mlx5e_handle_rx_cqe_mpwrq(struct mlx5e_rq *rq, struct mlx5_cqe64 *cqe)
2372 {
2373 	u16 cstrides       = mpwrq_get_cqe_consumed_strides(cqe);
2374 	u16 wqe_id         = be16_to_cpu(cqe->wqe_id);
2375 	struct mlx5e_mpw_info *wi = mlx5e_get_mpw_info(rq, wqe_id);
2376 	u16 stride_ix      = mpwrq_get_cqe_stride_index(cqe);
2377 	u32 wqe_offset     = stride_ix << rq->mpwqe.log_stride_sz;
2378 	u32 head_offset    = wqe_offset & ((1 << rq->mpwqe.page_shift) - 1);
2379 	u32 page_idx       = wqe_offset >> rq->mpwqe.page_shift;
2380 	struct mlx5e_rx_wqe_ll *wqe;
2381 	struct mlx5_wq_ll *wq;
2382 	struct sk_buff *skb;
2383 	u16 cqe_bcnt;
2384 
2385 	wi->consumed_strides += cstrides;
2386 
2387 	if (unlikely(MLX5E_RX_ERR_CQE(cqe))) {
2388 		mlx5e_handle_rx_err_cqe(rq, cqe);
2389 		goto mpwrq_cqe_out;
2390 	}
2391 
2392 	if (unlikely(mpwrq_is_filler_cqe(cqe))) {
2393 		struct mlx5e_rq_stats *stats = rq->stats;
2394 
2395 		stats->mpwqe_filler_cqes++;
2396 		stats->mpwqe_filler_strides += cstrides;
2397 		goto mpwrq_cqe_out;
2398 	}
2399 
2400 	cqe_bcnt = mpwrq_get_cqe_byte_cnt(cqe);
2401 
2402 	skb = INDIRECT_CALL_3(rq->mpwqe.skb_from_cqe_mpwrq,
2403 			      mlx5e_skb_from_cqe_mpwrq_linear,
2404 			      mlx5e_skb_from_cqe_mpwrq_nonlinear,
2405 			      mlx5e_xsk_skb_from_cqe_mpwrq_linear,
2406 			      rq, wi, cqe, cqe_bcnt, head_offset,
2407 			      page_idx);
2408 	if (!skb)
2409 		goto mpwrq_cqe_out;
2410 
2411 	mlx5e_complete_rx_cqe(rq, cqe, cqe_bcnt, skb);
2412 
2413 	if (mlx5e_cqe_regb_chain(cqe))
2414 		if (!mlx5e_tc_update_skb_nic(cqe, skb)) {
2415 			dev_kfree_skb_any(skb);
2416 			goto mpwrq_cqe_out;
2417 		}
2418 
2419 	napi_gro_receive(rq->cq.napi, skb);
2420 
2421 mpwrq_cqe_out:
2422 	if (likely(wi->consumed_strides < rq->mpwqe.num_strides))
2423 		return;
2424 
2425 	wq  = &rq->mpwqe.wq;
2426 	wqe = mlx5_wq_ll_get_wqe(wq, wqe_id);
2427 	mlx5_wq_ll_pop(wq, cqe->wqe_id, &wqe->next.next_wqe_index);
2428 }
2429 
mlx5e_rx_cq_process_enhanced_cqe_comp(struct mlx5e_rq * rq,struct mlx5_cqwq * cqwq,int budget_rem)2430 static int mlx5e_rx_cq_process_enhanced_cqe_comp(struct mlx5e_rq *rq,
2431 						 struct mlx5_cqwq *cqwq,
2432 						 int budget_rem)
2433 {
2434 	struct mlx5_cqe64 *cqe, *title_cqe = NULL;
2435 	struct mlx5e_cq_decomp *cqd = &rq->cqd;
2436 	int work_done = 0;
2437 
2438 	cqe = mlx5_cqwq_get_cqe_enhanced_comp(cqwq);
2439 	if (!cqe)
2440 		return work_done;
2441 
2442 	if (cqd->last_cqe_title &&
2443 	    (mlx5_get_cqe_format(cqe) == MLX5_COMPRESSED)) {
2444 		rq->stats->cqe_compress_blks++;
2445 		cqd->last_cqe_title = false;
2446 	}
2447 
2448 	do {
2449 		if (mlx5_get_cqe_format(cqe) == MLX5_COMPRESSED) {
2450 			if (title_cqe) {
2451 				mlx5e_read_enhanced_title_slot(rq, title_cqe);
2452 				title_cqe = NULL;
2453 				rq->stats->cqe_compress_blks++;
2454 			}
2455 			work_done +=
2456 				mlx5e_decompress_enhanced_cqe(rq, cqwq, cqe,
2457 							      budget_rem - work_done);
2458 			continue;
2459 		}
2460 		title_cqe = cqe;
2461 		mlx5_cqwq_pop(cqwq);
2462 
2463 		INDIRECT_CALL_3(rq->handle_rx_cqe, mlx5e_handle_rx_cqe_mpwrq,
2464 				mlx5e_handle_rx_cqe, mlx5e_handle_rx_cqe_mpwrq_shampo,
2465 				rq, cqe);
2466 		work_done++;
2467 	} while (work_done < budget_rem &&
2468 		 (cqe = mlx5_cqwq_get_cqe_enhanced_comp(cqwq)));
2469 
2470 	/* last cqe might be title on next poll bulk */
2471 	if (title_cqe) {
2472 		mlx5e_read_enhanced_title_slot(rq, title_cqe);
2473 		cqd->last_cqe_title = true;
2474 	}
2475 
2476 	return work_done;
2477 }
2478 
mlx5e_rx_cq_process_basic_cqe_comp(struct mlx5e_rq * rq,struct mlx5_cqwq * cqwq,int budget_rem)2479 static int mlx5e_rx_cq_process_basic_cqe_comp(struct mlx5e_rq *rq,
2480 					      struct mlx5_cqwq *cqwq,
2481 					      int budget_rem)
2482 {
2483 	struct mlx5_cqe64 *cqe;
2484 	int work_done = 0;
2485 
2486 	if (rq->cqd.left)
2487 		work_done += mlx5e_decompress_cqes_cont(rq, cqwq, 0, budget_rem);
2488 
2489 	while (work_done < budget_rem && (cqe = mlx5_cqwq_get_cqe(cqwq))) {
2490 		if (mlx5_get_cqe_format(cqe) == MLX5_COMPRESSED) {
2491 			work_done +=
2492 				mlx5e_decompress_cqes_start(rq, cqwq,
2493 							    budget_rem - work_done);
2494 			continue;
2495 		}
2496 
2497 		mlx5_cqwq_pop(cqwq);
2498 		INDIRECT_CALL_3(rq->handle_rx_cqe, mlx5e_handle_rx_cqe_mpwrq,
2499 				mlx5e_handle_rx_cqe, mlx5e_handle_rx_cqe_mpwrq_shampo,
2500 				rq, cqe);
2501 		work_done++;
2502 	}
2503 
2504 	return work_done;
2505 }
2506 
mlx5e_poll_rx_cq(struct mlx5e_cq * cq,int budget)2507 int mlx5e_poll_rx_cq(struct mlx5e_cq *cq, int budget)
2508 {
2509 	struct mlx5e_rq *rq = container_of(cq, struct mlx5e_rq, cq);
2510 	struct mlx5_cqwq *cqwq = &cq->wq;
2511 	int work_done;
2512 
2513 	if (unlikely(!test_bit(MLX5E_RQ_STATE_ENABLED, &rq->state)))
2514 		return 0;
2515 
2516 	if (test_bit(MLX5E_RQ_STATE_MINI_CQE_ENHANCED, &rq->state))
2517 		work_done = mlx5e_rx_cq_process_enhanced_cqe_comp(rq, cqwq,
2518 								  budget);
2519 	else
2520 		work_done = mlx5e_rx_cq_process_basic_cqe_comp(rq, cqwq,
2521 							       budget);
2522 
2523 	if (work_done == 0)
2524 		return 0;
2525 
2526 	if (test_bit(MLX5E_RQ_STATE_SHAMPO, &rq->state) && rq->hw_gro_data->skb)
2527 		mlx5e_shampo_flush_skb(rq, NULL, false);
2528 
2529 	if (rcu_access_pointer(rq->xdp_prog))
2530 		mlx5e_xdp_rx_poll_complete(rq);
2531 
2532 	mlx5_cqwq_update_db_record(cqwq);
2533 
2534 	/* ensure cq space is freed before enabling more cqes */
2535 	wmb();
2536 
2537 	return work_done;
2538 }
2539 
2540 #ifdef CONFIG_MLX5_CORE_IPOIB
2541 
2542 #define MLX5_IB_GRH_SGID_OFFSET 8
2543 #define MLX5_IB_GRH_DGID_OFFSET 24
2544 #define MLX5_GID_SIZE           16
2545 
mlx5i_complete_rx_cqe(struct mlx5e_rq * rq,struct mlx5_cqe64 * cqe,u32 cqe_bcnt,struct sk_buff * skb)2546 static inline void mlx5i_complete_rx_cqe(struct mlx5e_rq *rq,
2547 					 struct mlx5_cqe64 *cqe,
2548 					 u32 cqe_bcnt,
2549 					 struct sk_buff *skb)
2550 {
2551 	struct hwtstamp_config *tstamp;
2552 	struct mlx5e_rq_stats *stats;
2553 	struct net_device *netdev;
2554 	struct mlx5e_priv *priv;
2555 	char *pseudo_header;
2556 	u32 flags_rqpn;
2557 	u32 qpn;
2558 	u8 *dgid;
2559 	u8 g;
2560 
2561 	qpn = be32_to_cpu(cqe->sop_drop_qpn) & 0xffffff;
2562 	netdev = mlx5i_pkey_get_netdev(rq->netdev, qpn);
2563 
2564 	/* No mapping present, cannot process SKB. This might happen if a child
2565 	 * interface is going down while having unprocessed CQEs on parent RQ
2566 	 */
2567 	if (unlikely(!netdev)) {
2568 		/* TODO: add drop counters support */
2569 		skb->dev = NULL;
2570 		pr_warn_once("Unable to map QPN %u to dev - dropping skb\n", qpn);
2571 		return;
2572 	}
2573 
2574 	priv = mlx5i_epriv(netdev);
2575 	tstamp = &priv->tstamp;
2576 	stats = &priv->channel_stats[rq->ix]->rq;
2577 
2578 	flags_rqpn = be32_to_cpu(cqe->flags_rqpn);
2579 	g = (flags_rqpn >> 28) & 3;
2580 	dgid = skb->data + MLX5_IB_GRH_DGID_OFFSET;
2581 	if ((!g) || dgid[0] != 0xff)
2582 		skb->pkt_type = PACKET_HOST;
2583 	else if (memcmp(dgid, netdev->broadcast + 4, MLX5_GID_SIZE) == 0)
2584 		skb->pkt_type = PACKET_BROADCAST;
2585 	else
2586 		skb->pkt_type = PACKET_MULTICAST;
2587 
2588 	/* Drop packets that this interface sent, ie multicast packets
2589 	 * that the HCA has replicated.
2590 	 */
2591 	if (g && (qpn == (flags_rqpn & 0xffffff)) &&
2592 	    (memcmp(netdev->dev_addr + 4, skb->data + MLX5_IB_GRH_SGID_OFFSET,
2593 		    MLX5_GID_SIZE) == 0)) {
2594 		skb->dev = NULL;
2595 		return;
2596 	}
2597 
2598 	skb_pull(skb, MLX5_IB_GRH_BYTES);
2599 
2600 	skb->protocol = *((__be16 *)(skb->data));
2601 
2602 	if (netdev->features & NETIF_F_RXCSUM) {
2603 		skb->ip_summed = CHECKSUM_COMPLETE;
2604 		skb->csum = csum_unfold((__force __sum16)cqe->check_sum);
2605 		stats->csum_complete++;
2606 	} else {
2607 		skb->ip_summed = CHECKSUM_NONE;
2608 		stats->csum_none++;
2609 	}
2610 
2611 	if (unlikely(mlx5e_rx_hw_stamp(tstamp)))
2612 		skb_hwtstamps(skb)->hwtstamp = mlx5e_cqe_ts_to_ns(rq->ptp_cyc2time,
2613 								  rq->clock, get_cqe_ts(cqe));
2614 	skb_record_rx_queue(skb, rq->ix);
2615 
2616 	if (likely(netdev->features & NETIF_F_RXHASH))
2617 		mlx5e_skb_set_hash(cqe, skb);
2618 
2619 	/* 20 bytes of ipoib header and 4 for encap existing */
2620 	pseudo_header = skb_push(skb, MLX5_IPOIB_PSEUDO_LEN);
2621 	memset(pseudo_header, 0, MLX5_IPOIB_PSEUDO_LEN);
2622 	skb_reset_mac_header(skb);
2623 	skb_pull(skb, MLX5_IPOIB_HARD_LEN);
2624 
2625 	skb->dev = netdev;
2626 
2627 	stats->packets++;
2628 	stats->bytes += cqe_bcnt;
2629 }
2630 
mlx5i_handle_rx_cqe(struct mlx5e_rq * rq,struct mlx5_cqe64 * cqe)2631 static void mlx5i_handle_rx_cqe(struct mlx5e_rq *rq, struct mlx5_cqe64 *cqe)
2632 {
2633 	struct mlx5_wq_cyc *wq = &rq->wqe.wq;
2634 	struct mlx5e_wqe_frag_info *wi;
2635 	struct sk_buff *skb;
2636 	u32 cqe_bcnt;
2637 	u16 ci;
2638 
2639 	ci       = mlx5_wq_cyc_ctr2ix(wq, be16_to_cpu(cqe->wqe_counter));
2640 	wi       = get_frag(rq, ci);
2641 	cqe_bcnt = be32_to_cpu(cqe->byte_cnt);
2642 
2643 	if (unlikely(MLX5E_RX_ERR_CQE(cqe))) {
2644 		rq->stats->wqe_err++;
2645 		goto wq_cyc_pop;
2646 	}
2647 
2648 	skb = INDIRECT_CALL_2(rq->wqe.skb_from_cqe,
2649 			      mlx5e_skb_from_cqe_linear,
2650 			      mlx5e_skb_from_cqe_nonlinear,
2651 			      rq, wi, cqe, cqe_bcnt);
2652 	if (!skb)
2653 		goto wq_cyc_pop;
2654 
2655 	mlx5i_complete_rx_cqe(rq, cqe, cqe_bcnt, skb);
2656 	if (unlikely(!skb->dev)) {
2657 		dev_kfree_skb_any(skb);
2658 		goto wq_cyc_pop;
2659 	}
2660 	napi_gro_receive(rq->cq.napi, skb);
2661 
2662 wq_cyc_pop:
2663 	mlx5_wq_cyc_pop(wq);
2664 }
2665 
2666 const struct mlx5e_rx_handlers mlx5i_rx_handlers = {
2667 	.handle_rx_cqe       = mlx5i_handle_rx_cqe,
2668 	.handle_rx_cqe_mpwqe = NULL, /* Not supported */
2669 };
2670 #endif /* CONFIG_MLX5_CORE_IPOIB */
2671 
mlx5e_rq_set_handlers(struct mlx5e_rq * rq,struct mlx5e_params * params,bool xsk)2672 int mlx5e_rq_set_handlers(struct mlx5e_rq *rq, struct mlx5e_params *params, bool xsk)
2673 {
2674 	struct net_device *netdev = rq->netdev;
2675 	struct mlx5_core_dev *mdev = rq->mdev;
2676 	struct mlx5e_priv *priv = rq->priv;
2677 
2678 	switch (rq->wq_type) {
2679 	case MLX5_WQ_TYPE_LINKED_LIST_STRIDING_RQ:
2680 		rq->mpwqe.skb_from_cqe_mpwrq = xsk ?
2681 			mlx5e_xsk_skb_from_cqe_mpwrq_linear :
2682 			mlx5e_rx_mpwqe_is_linear_skb(mdev, params, NULL) ?
2683 				mlx5e_skb_from_cqe_mpwrq_linear :
2684 				mlx5e_skb_from_cqe_mpwrq_nonlinear;
2685 		rq->post_wqes = mlx5e_post_rx_mpwqes;
2686 		rq->dealloc_wqe = mlx5e_dealloc_rx_mpwqe;
2687 
2688 		if (params->packet_merge.type == MLX5E_PACKET_MERGE_SHAMPO) {
2689 			rq->handle_rx_cqe = priv->profile->rx_handlers->handle_rx_cqe_mpwqe_shampo;
2690 			if (!rq->handle_rx_cqe) {
2691 				netdev_err(netdev, "RX handler of SHAMPO MPWQE RQ is not set\n");
2692 				return -EINVAL;
2693 			}
2694 		} else {
2695 			rq->handle_rx_cqe = priv->profile->rx_handlers->handle_rx_cqe_mpwqe;
2696 			if (!rq->handle_rx_cqe) {
2697 				netdev_err(netdev, "RX handler of MPWQE RQ is not set\n");
2698 				return -EINVAL;
2699 			}
2700 		}
2701 
2702 		break;
2703 	default: /* MLX5_WQ_TYPE_CYCLIC */
2704 		rq->wqe.skb_from_cqe = xsk ?
2705 			mlx5e_xsk_skb_from_cqe_linear :
2706 			mlx5e_rx_is_linear_skb(mdev, params, NULL) ?
2707 				mlx5e_skb_from_cqe_linear :
2708 				mlx5e_skb_from_cqe_nonlinear;
2709 		rq->post_wqes = mlx5e_post_rx_wqes;
2710 		rq->dealloc_wqe = mlx5e_dealloc_rx_wqe;
2711 		rq->handle_rx_cqe = priv->profile->rx_handlers->handle_rx_cqe;
2712 		if (!rq->handle_rx_cqe) {
2713 			netdev_err(netdev, "RX handler of RQ is not set\n");
2714 			return -EINVAL;
2715 		}
2716 	}
2717 
2718 	return 0;
2719 }
2720 
mlx5e_trap_handle_rx_cqe(struct mlx5e_rq * rq,struct mlx5_cqe64 * cqe)2721 static void mlx5e_trap_handle_rx_cqe(struct mlx5e_rq *rq, struct mlx5_cqe64 *cqe)
2722 {
2723 	struct mlx5_wq_cyc *wq = &rq->wqe.wq;
2724 	struct mlx5e_wqe_frag_info *wi;
2725 	struct sk_buff *skb;
2726 	u32 cqe_bcnt;
2727 	u16 trap_id;
2728 	u16 ci;
2729 
2730 	trap_id  = get_cqe_flow_tag(cqe);
2731 	ci       = mlx5_wq_cyc_ctr2ix(wq, be16_to_cpu(cqe->wqe_counter));
2732 	wi       = get_frag(rq, ci);
2733 	cqe_bcnt = be32_to_cpu(cqe->byte_cnt);
2734 
2735 	if (unlikely(MLX5E_RX_ERR_CQE(cqe))) {
2736 		rq->stats->wqe_err++;
2737 		goto wq_cyc_pop;
2738 	}
2739 
2740 	skb = mlx5e_skb_from_cqe_nonlinear(rq, wi, cqe, cqe_bcnt);
2741 	if (!skb)
2742 		goto wq_cyc_pop;
2743 
2744 	mlx5e_complete_rx_cqe(rq, cqe, cqe_bcnt, skb);
2745 	skb_push(skb, ETH_HLEN);
2746 
2747 	mlx5_devlink_trap_report(rq->mdev, trap_id, skb,
2748 				 rq->netdev->devlink_port);
2749 	dev_kfree_skb_any(skb);
2750 
2751 wq_cyc_pop:
2752 	mlx5_wq_cyc_pop(wq);
2753 }
2754 
mlx5e_rq_set_trap_handlers(struct mlx5e_rq * rq,struct mlx5e_params * params)2755 void mlx5e_rq_set_trap_handlers(struct mlx5e_rq *rq, struct mlx5e_params *params)
2756 {
2757 	rq->wqe.skb_from_cqe = mlx5e_rx_is_linear_skb(rq->mdev, params, NULL) ?
2758 			       mlx5e_skb_from_cqe_linear :
2759 			       mlx5e_skb_from_cqe_nonlinear;
2760 	rq->post_wqes = mlx5e_post_rx_wqes;
2761 	rq->dealloc_wqe = mlx5e_dealloc_rx_wqe;
2762 	rq->handle_rx_cqe = mlx5e_trap_handle_rx_cqe;
2763 }
2764