• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <net/ip6_checksum.h>
37 #include <net/page_pool.h>
38 #include <net/inet_ecn.h>
39 #include "en.h"
40 #include "en/txrx.h"
41 #include "en_tc.h"
42 #include "eswitch.h"
43 #include "en_rep.h"
44 #include "en/rep/tc.h"
45 #include "ipoib/ipoib.h"
46 #include "accel/ipsec.h"
47 #include "fpga/ipsec.h"
48 #include "en_accel/ipsec_rxtx.h"
49 #include "en_accel/tls_rxtx.h"
50 #include "lib/clock.h"
51 #include "en/xdp.h"
52 #include "en/xsk/rx.h"
53 #include "en/health.h"
54 #include "en/params.h"
55 #include "en/txrx.h"
56 
57 static struct sk_buff *
58 mlx5e_skb_from_cqe_mpwrq_linear(struct mlx5e_rq *rq, struct mlx5e_mpw_info *wi,
59 				u16 cqe_bcnt, u32 head_offset, u32 page_idx);
60 static struct sk_buff *
61 mlx5e_skb_from_cqe_mpwrq_nonlinear(struct mlx5e_rq *rq, struct mlx5e_mpw_info *wi,
62 				   u16 cqe_bcnt, u32 head_offset, u32 page_idx);
63 static void mlx5e_handle_rx_cqe(struct mlx5e_rq *rq, struct mlx5_cqe64 *cqe);
64 static void mlx5e_handle_rx_cqe_mpwrq(struct mlx5e_rq *rq, struct mlx5_cqe64 *cqe);
65 
66 const struct mlx5e_rx_handlers mlx5e_rx_handlers_nic = {
67 	.handle_rx_cqe       = mlx5e_handle_rx_cqe,
68 	.handle_rx_cqe_mpwqe = mlx5e_handle_rx_cqe_mpwrq,
69 };
70 
mlx5e_rx_hw_stamp(struct hwtstamp_config * config)71 static inline bool mlx5e_rx_hw_stamp(struct hwtstamp_config *config)
72 {
73 	return config->rx_filter == HWTSTAMP_FILTER_ALL;
74 }
75 
mlx5e_read_cqe_slot(struct mlx5_cqwq * wq,u32 cqcc,void * data)76 static inline void mlx5e_read_cqe_slot(struct mlx5_cqwq *wq,
77 				       u32 cqcc, void *data)
78 {
79 	u32 ci = mlx5_cqwq_ctr2ix(wq, cqcc);
80 
81 	memcpy(data, mlx5_cqwq_get_wqe(wq, ci), sizeof(struct mlx5_cqe64));
82 }
83 
mlx5e_read_title_slot(struct mlx5e_rq * rq,struct mlx5_cqwq * wq,u32 cqcc)84 static inline void mlx5e_read_title_slot(struct mlx5e_rq *rq,
85 					 struct mlx5_cqwq *wq,
86 					 u32 cqcc)
87 {
88 	struct mlx5e_cq_decomp *cqd = &rq->cqd;
89 	struct mlx5_cqe64 *title = &cqd->title;
90 
91 	mlx5e_read_cqe_slot(wq, cqcc, title);
92 	cqd->left        = be32_to_cpu(title->byte_cnt);
93 	cqd->wqe_counter = be16_to_cpu(title->wqe_counter);
94 	rq->stats->cqe_compress_blks++;
95 }
96 
mlx5e_read_mini_arr_slot(struct mlx5_cqwq * wq,struct mlx5e_cq_decomp * cqd,u32 cqcc)97 static inline void mlx5e_read_mini_arr_slot(struct mlx5_cqwq *wq,
98 					    struct mlx5e_cq_decomp *cqd,
99 					    u32 cqcc)
100 {
101 	mlx5e_read_cqe_slot(wq, cqcc, cqd->mini_arr);
102 	cqd->mini_arr_idx = 0;
103 }
104 
mlx5e_cqes_update_owner(struct mlx5_cqwq * wq,int n)105 static inline void mlx5e_cqes_update_owner(struct mlx5_cqwq *wq, int n)
106 {
107 	u32 cqcc   = wq->cc;
108 	u8  op_own = mlx5_cqwq_get_ctr_wrap_cnt(wq, cqcc) & 1;
109 	u32 ci     = mlx5_cqwq_ctr2ix(wq, cqcc);
110 	u32 wq_sz  = mlx5_cqwq_get_size(wq);
111 	u32 ci_top = min_t(u32, wq_sz, ci + n);
112 
113 	for (; ci < ci_top; ci++, n--) {
114 		struct mlx5_cqe64 *cqe = mlx5_cqwq_get_wqe(wq, ci);
115 
116 		cqe->op_own = op_own;
117 	}
118 
119 	if (unlikely(ci == wq_sz)) {
120 		op_own = !op_own;
121 		for (ci = 0; ci < n; ci++) {
122 			struct mlx5_cqe64 *cqe = mlx5_cqwq_get_wqe(wq, ci);
123 
124 			cqe->op_own = op_own;
125 		}
126 	}
127 }
128 
mlx5e_decompress_cqe(struct mlx5e_rq * rq,struct mlx5_cqwq * wq,u32 cqcc)129 static inline void mlx5e_decompress_cqe(struct mlx5e_rq *rq,
130 					struct mlx5_cqwq *wq,
131 					u32 cqcc)
132 {
133 	struct mlx5e_cq_decomp *cqd = &rq->cqd;
134 	struct mlx5_mini_cqe8 *mini_cqe = &cqd->mini_arr[cqd->mini_arr_idx];
135 	struct mlx5_cqe64 *title = &cqd->title;
136 
137 	title->byte_cnt     = mini_cqe->byte_cnt;
138 	title->check_sum    = mini_cqe->checksum;
139 	title->op_own      &= 0xf0;
140 	title->op_own      |= 0x01 & (cqcc >> wq->fbc.log_sz);
141 
142 	/* state bit set implies linked-list striding RQ wq type and
143 	 * HW stride index capability supported
144 	 */
145 	if (test_bit(MLX5E_RQ_STATE_MINI_CQE_HW_STRIDX, &rq->state)) {
146 		title->wqe_counter = mini_cqe->stridx;
147 		return;
148 	}
149 
150 	/* HW stride index capability not supported */
151 	title->wqe_counter = cpu_to_be16(cqd->wqe_counter);
152 	if (rq->wq_type == MLX5_WQ_TYPE_LINKED_LIST_STRIDING_RQ)
153 		cqd->wqe_counter += mpwrq_get_cqe_consumed_strides(title);
154 	else
155 		cqd->wqe_counter =
156 			mlx5_wq_cyc_ctr2ix(&rq->wqe.wq, cqd->wqe_counter + 1);
157 }
158 
mlx5e_decompress_cqe_no_hash(struct mlx5e_rq * rq,struct mlx5_cqwq * wq,u32 cqcc)159 static inline void mlx5e_decompress_cqe_no_hash(struct mlx5e_rq *rq,
160 						struct mlx5_cqwq *wq,
161 						u32 cqcc)
162 {
163 	struct mlx5e_cq_decomp *cqd = &rq->cqd;
164 
165 	mlx5e_decompress_cqe(rq, wq, cqcc);
166 	cqd->title.rss_hash_type   = 0;
167 	cqd->title.rss_hash_result = 0;
168 }
169 
mlx5e_decompress_cqes_cont(struct mlx5e_rq * rq,struct mlx5_cqwq * wq,int update_owner_only,int budget_rem)170 static inline u32 mlx5e_decompress_cqes_cont(struct mlx5e_rq *rq,
171 					     struct mlx5_cqwq *wq,
172 					     int update_owner_only,
173 					     int budget_rem)
174 {
175 	struct mlx5e_cq_decomp *cqd = &rq->cqd;
176 	u32 cqcc = wq->cc + update_owner_only;
177 	u32 cqe_count;
178 	u32 i;
179 
180 	cqe_count = min_t(u32, cqd->left, budget_rem);
181 
182 	for (i = update_owner_only; i < cqe_count;
183 	     i++, cqd->mini_arr_idx++, cqcc++) {
184 		if (cqd->mini_arr_idx == MLX5_MINI_CQE_ARRAY_SIZE)
185 			mlx5e_read_mini_arr_slot(wq, cqd, cqcc);
186 
187 		mlx5e_decompress_cqe_no_hash(rq, wq, cqcc);
188 		INDIRECT_CALL_2(rq->handle_rx_cqe, mlx5e_handle_rx_cqe_mpwrq,
189 				mlx5e_handle_rx_cqe, rq, &cqd->title);
190 	}
191 	mlx5e_cqes_update_owner(wq, cqcc - wq->cc);
192 	wq->cc = cqcc;
193 	cqd->left -= cqe_count;
194 	rq->stats->cqe_compress_pkts += cqe_count;
195 
196 	return cqe_count;
197 }
198 
mlx5e_decompress_cqes_start(struct mlx5e_rq * rq,struct mlx5_cqwq * wq,int budget_rem)199 static inline u32 mlx5e_decompress_cqes_start(struct mlx5e_rq *rq,
200 					      struct mlx5_cqwq *wq,
201 					      int budget_rem)
202 {
203 	struct mlx5e_cq_decomp *cqd = &rq->cqd;
204 	u32 cc = wq->cc;
205 
206 	mlx5e_read_title_slot(rq, wq, cc);
207 	mlx5e_read_mini_arr_slot(wq, cqd, cc + 1);
208 	mlx5e_decompress_cqe(rq, wq, cc);
209 	INDIRECT_CALL_2(rq->handle_rx_cqe, mlx5e_handle_rx_cqe_mpwrq,
210 			mlx5e_handle_rx_cqe, rq, &cqd->title);
211 	cqd->mini_arr_idx++;
212 
213 	return mlx5e_decompress_cqes_cont(rq, wq, 1, budget_rem) - 1;
214 }
215 
mlx5e_page_is_reserved(struct page * page)216 static inline bool mlx5e_page_is_reserved(struct page *page)
217 {
218 	return page_is_pfmemalloc(page) || page_to_nid(page) != numa_mem_id();
219 }
220 
mlx5e_rx_cache_put(struct mlx5e_rq * rq,struct mlx5e_dma_info * dma_info)221 static inline bool mlx5e_rx_cache_put(struct mlx5e_rq *rq,
222 				      struct mlx5e_dma_info *dma_info)
223 {
224 	struct mlx5e_page_cache *cache = &rq->page_cache;
225 	u32 tail_next = (cache->tail + 1) & (MLX5E_CACHE_SIZE - 1);
226 	struct mlx5e_rq_stats *stats = rq->stats;
227 
228 	if (tail_next == cache->head) {
229 		stats->cache_full++;
230 		return false;
231 	}
232 
233 	if (unlikely(mlx5e_page_is_reserved(dma_info->page))) {
234 		stats->cache_waive++;
235 		return false;
236 	}
237 
238 	cache->page_cache[cache->tail] = *dma_info;
239 	cache->tail = tail_next;
240 	return true;
241 }
242 
mlx5e_rx_cache_get(struct mlx5e_rq * rq,struct mlx5e_dma_info * dma_info)243 static inline bool mlx5e_rx_cache_get(struct mlx5e_rq *rq,
244 				      struct mlx5e_dma_info *dma_info)
245 {
246 	struct mlx5e_page_cache *cache = &rq->page_cache;
247 	struct mlx5e_rq_stats *stats = rq->stats;
248 
249 	if (unlikely(cache->head == cache->tail)) {
250 		stats->cache_empty++;
251 		return false;
252 	}
253 
254 	if (page_ref_count(cache->page_cache[cache->head].page) != 1) {
255 		stats->cache_busy++;
256 		return false;
257 	}
258 
259 	*dma_info = cache->page_cache[cache->head];
260 	cache->head = (cache->head + 1) & (MLX5E_CACHE_SIZE - 1);
261 	stats->cache_reuse++;
262 
263 	dma_sync_single_for_device(rq->pdev, dma_info->addr,
264 				   PAGE_SIZE,
265 				   DMA_FROM_DEVICE);
266 	return true;
267 }
268 
mlx5e_page_alloc_pool(struct mlx5e_rq * rq,struct mlx5e_dma_info * dma_info)269 static inline int mlx5e_page_alloc_pool(struct mlx5e_rq *rq,
270 					struct mlx5e_dma_info *dma_info)
271 {
272 	if (mlx5e_rx_cache_get(rq, dma_info))
273 		return 0;
274 
275 	dma_info->page = page_pool_dev_alloc_pages(rq->page_pool);
276 	if (unlikely(!dma_info->page))
277 		return -ENOMEM;
278 
279 	dma_info->addr = dma_map_page_attrs(rq->pdev, dma_info->page, 0, PAGE_SIZE,
280 					    rq->buff.map_dir, DMA_ATTR_SKIP_CPU_SYNC);
281 	if (unlikely(dma_mapping_error(rq->pdev, dma_info->addr))) {
282 		page_pool_recycle_direct(rq->page_pool, dma_info->page);
283 		dma_info->page = NULL;
284 		return -ENOMEM;
285 	}
286 
287 	return 0;
288 }
289 
mlx5e_page_alloc(struct mlx5e_rq * rq,struct mlx5e_dma_info * dma_info)290 static inline int mlx5e_page_alloc(struct mlx5e_rq *rq,
291 				   struct mlx5e_dma_info *dma_info)
292 {
293 	if (rq->xsk_pool)
294 		return mlx5e_xsk_page_alloc_pool(rq, dma_info);
295 	else
296 		return mlx5e_page_alloc_pool(rq, dma_info);
297 }
298 
mlx5e_page_dma_unmap(struct mlx5e_rq * rq,struct mlx5e_dma_info * dma_info)299 void mlx5e_page_dma_unmap(struct mlx5e_rq *rq, struct mlx5e_dma_info *dma_info)
300 {
301 	dma_unmap_page_attrs(rq->pdev, dma_info->addr, PAGE_SIZE, rq->buff.map_dir,
302 			     DMA_ATTR_SKIP_CPU_SYNC);
303 }
304 
mlx5e_page_release_dynamic(struct mlx5e_rq * rq,struct mlx5e_dma_info * dma_info,bool recycle)305 void mlx5e_page_release_dynamic(struct mlx5e_rq *rq,
306 				struct mlx5e_dma_info *dma_info,
307 				bool recycle)
308 {
309 	if (likely(recycle)) {
310 		if (mlx5e_rx_cache_put(rq, dma_info))
311 			return;
312 
313 		mlx5e_page_dma_unmap(rq, dma_info);
314 		page_pool_recycle_direct(rq->page_pool, dma_info->page);
315 	} else {
316 		mlx5e_page_dma_unmap(rq, dma_info);
317 		page_pool_release_page(rq->page_pool, dma_info->page);
318 		put_page(dma_info->page);
319 	}
320 }
321 
mlx5e_page_release(struct mlx5e_rq * rq,struct mlx5e_dma_info * dma_info,bool recycle)322 static inline void mlx5e_page_release(struct mlx5e_rq *rq,
323 				      struct mlx5e_dma_info *dma_info,
324 				      bool recycle)
325 {
326 	if (rq->xsk_pool)
327 		/* The `recycle` parameter is ignored, and the page is always
328 		 * put into the Reuse Ring, because there is no way to return
329 		 * the page to the userspace when the interface goes down.
330 		 */
331 		xsk_buff_free(dma_info->xsk);
332 	else
333 		mlx5e_page_release_dynamic(rq, dma_info, recycle);
334 }
335 
mlx5e_get_rx_frag(struct mlx5e_rq * rq,struct mlx5e_wqe_frag_info * frag)336 static inline int mlx5e_get_rx_frag(struct mlx5e_rq *rq,
337 				    struct mlx5e_wqe_frag_info *frag)
338 {
339 	int err = 0;
340 
341 	if (!frag->offset)
342 		/* On first frag (offset == 0), replenish page (dma_info actually).
343 		 * Other frags that point to the same dma_info (with a different
344 		 * offset) should just use the new one without replenishing again
345 		 * by themselves.
346 		 */
347 		err = mlx5e_page_alloc(rq, frag->di);
348 
349 	return err;
350 }
351 
mlx5e_put_rx_frag(struct mlx5e_rq * rq,struct mlx5e_wqe_frag_info * frag,bool recycle)352 static inline void mlx5e_put_rx_frag(struct mlx5e_rq *rq,
353 				     struct mlx5e_wqe_frag_info *frag,
354 				     bool recycle)
355 {
356 	if (frag->last_in_page)
357 		mlx5e_page_release(rq, frag->di, recycle);
358 }
359 
get_frag(struct mlx5e_rq * rq,u16 ix)360 static inline struct mlx5e_wqe_frag_info *get_frag(struct mlx5e_rq *rq, u16 ix)
361 {
362 	return &rq->wqe.frags[ix << rq->wqe.info.log_num_frags];
363 }
364 
mlx5e_alloc_rx_wqe(struct mlx5e_rq * rq,struct mlx5e_rx_wqe_cyc * wqe,u16 ix)365 static int mlx5e_alloc_rx_wqe(struct mlx5e_rq *rq, struct mlx5e_rx_wqe_cyc *wqe,
366 			      u16 ix)
367 {
368 	struct mlx5e_wqe_frag_info *frag = get_frag(rq, ix);
369 	int err;
370 	int i;
371 
372 	for (i = 0; i < rq->wqe.info.num_frags; i++, frag++) {
373 		err = mlx5e_get_rx_frag(rq, frag);
374 		if (unlikely(err))
375 			goto free_frags;
376 
377 		wqe->data[i].addr = cpu_to_be64(frag->di->addr +
378 						frag->offset + rq->buff.headroom);
379 	}
380 
381 	return 0;
382 
383 free_frags:
384 	while (--i >= 0)
385 		mlx5e_put_rx_frag(rq, --frag, true);
386 
387 	return err;
388 }
389 
mlx5e_free_rx_wqe(struct mlx5e_rq * rq,struct mlx5e_wqe_frag_info * wi,bool recycle)390 static inline void mlx5e_free_rx_wqe(struct mlx5e_rq *rq,
391 				     struct mlx5e_wqe_frag_info *wi,
392 				     bool recycle)
393 {
394 	int i;
395 
396 	for (i = 0; i < rq->wqe.info.num_frags; i++, wi++)
397 		mlx5e_put_rx_frag(rq, wi, recycle);
398 }
399 
mlx5e_dealloc_rx_wqe(struct mlx5e_rq * rq,u16 ix)400 static void mlx5e_dealloc_rx_wqe(struct mlx5e_rq *rq, u16 ix)
401 {
402 	struct mlx5e_wqe_frag_info *wi = get_frag(rq, ix);
403 
404 	mlx5e_free_rx_wqe(rq, wi, false);
405 }
406 
mlx5e_alloc_rx_wqes(struct mlx5e_rq * rq,u16 ix,u8 wqe_bulk)407 static int mlx5e_alloc_rx_wqes(struct mlx5e_rq *rq, u16 ix, u8 wqe_bulk)
408 {
409 	struct mlx5_wq_cyc *wq = &rq->wqe.wq;
410 	int err;
411 	int i;
412 
413 	if (rq->xsk_pool) {
414 		int pages_desired = wqe_bulk << rq->wqe.info.log_num_frags;
415 
416 		/* Check in advance that we have enough frames, instead of
417 		 * allocating one-by-one, failing and moving frames to the
418 		 * Reuse Ring.
419 		 */
420 		if (unlikely(!xsk_buff_can_alloc(rq->xsk_pool, pages_desired)))
421 			return -ENOMEM;
422 	}
423 
424 	for (i = 0; i < wqe_bulk; i++) {
425 		struct mlx5e_rx_wqe_cyc *wqe = mlx5_wq_cyc_get_wqe(wq, ix + i);
426 
427 		err = mlx5e_alloc_rx_wqe(rq, wqe, ix + i);
428 		if (unlikely(err))
429 			goto free_wqes;
430 	}
431 
432 	return 0;
433 
434 free_wqes:
435 	while (--i >= 0)
436 		mlx5e_dealloc_rx_wqe(rq, ix + i);
437 
438 	return err;
439 }
440 
441 static inline void
mlx5e_add_skb_frag(struct mlx5e_rq * rq,struct sk_buff * skb,struct mlx5e_dma_info * di,u32 frag_offset,u32 len,unsigned int truesize)442 mlx5e_add_skb_frag(struct mlx5e_rq *rq, struct sk_buff *skb,
443 		   struct mlx5e_dma_info *di, u32 frag_offset, u32 len,
444 		   unsigned int truesize)
445 {
446 	dma_sync_single_for_cpu(rq->pdev,
447 				di->addr + frag_offset,
448 				len, DMA_FROM_DEVICE);
449 	page_ref_inc(di->page);
450 	skb_add_rx_frag(skb, skb_shinfo(skb)->nr_frags,
451 			di->page, frag_offset, len, truesize);
452 }
453 
454 static inline void
mlx5e_copy_skb_header(struct device * pdev,struct sk_buff * skb,struct mlx5e_dma_info * dma_info,int offset_from,u32 headlen)455 mlx5e_copy_skb_header(struct device *pdev, struct sk_buff *skb,
456 		      struct mlx5e_dma_info *dma_info,
457 		      int offset_from, u32 headlen)
458 {
459 	const void *from = page_address(dma_info->page) + offset_from;
460 	/* Aligning len to sizeof(long) optimizes memcpy performance */
461 	unsigned int len = ALIGN(headlen, sizeof(long));
462 
463 	dma_sync_single_for_cpu(pdev, dma_info->addr + offset_from, len,
464 				DMA_FROM_DEVICE);
465 	skb_copy_to_linear_data(skb, from, len);
466 }
467 
468 static void
mlx5e_free_rx_mpwqe(struct mlx5e_rq * rq,struct mlx5e_mpw_info * wi,bool recycle)469 mlx5e_free_rx_mpwqe(struct mlx5e_rq *rq, struct mlx5e_mpw_info *wi, bool recycle)
470 {
471 	bool no_xdp_xmit;
472 	struct mlx5e_dma_info *dma_info = wi->umr.dma_info;
473 	int i;
474 
475 	/* A common case for AF_XDP. */
476 	if (bitmap_full(wi->xdp_xmit_bitmap, MLX5_MPWRQ_PAGES_PER_WQE))
477 		return;
478 
479 	no_xdp_xmit = bitmap_empty(wi->xdp_xmit_bitmap,
480 				   MLX5_MPWRQ_PAGES_PER_WQE);
481 
482 	for (i = 0; i < MLX5_MPWRQ_PAGES_PER_WQE; i++)
483 		if (no_xdp_xmit || !test_bit(i, wi->xdp_xmit_bitmap))
484 			mlx5e_page_release(rq, &dma_info[i], recycle);
485 }
486 
mlx5e_post_rx_mpwqe(struct mlx5e_rq * rq,u8 n)487 static void mlx5e_post_rx_mpwqe(struct mlx5e_rq *rq, u8 n)
488 {
489 	struct mlx5_wq_ll *wq = &rq->mpwqe.wq;
490 
491 	do {
492 		u16 next_wqe_index = mlx5_wq_ll_get_wqe_next_ix(wq, wq->head);
493 
494 		mlx5_wq_ll_push(wq, next_wqe_index);
495 	} while (--n);
496 
497 	/* ensure wqes are visible to device before updating doorbell record */
498 	dma_wmb();
499 
500 	mlx5_wq_ll_update_db_record(wq);
501 }
502 
mlx5e_alloc_rx_mpwqe(struct mlx5e_rq * rq,u16 ix)503 static int mlx5e_alloc_rx_mpwqe(struct mlx5e_rq *rq, u16 ix)
504 {
505 	struct mlx5e_mpw_info *wi = &rq->mpwqe.info[ix];
506 	struct mlx5e_dma_info *dma_info = &wi->umr.dma_info[0];
507 	struct mlx5e_icosq *sq = &rq->channel->icosq;
508 	struct mlx5_wq_cyc *wq = &sq->wq;
509 	struct mlx5e_umr_wqe *umr_wqe;
510 	u16 pi;
511 	int err;
512 	int i;
513 
514 	/* Check in advance that we have enough frames, instead of allocating
515 	 * one-by-one, failing and moving frames to the Reuse Ring.
516 	 */
517 	if (rq->xsk_pool &&
518 	    unlikely(!xsk_buff_can_alloc(rq->xsk_pool, MLX5_MPWRQ_PAGES_PER_WQE))) {
519 		err = -ENOMEM;
520 		goto err;
521 	}
522 
523 	pi = mlx5e_icosq_get_next_pi(sq, MLX5E_UMR_WQEBBS);
524 	umr_wqe = mlx5_wq_cyc_get_wqe(wq, pi);
525 	memcpy(umr_wqe, &rq->mpwqe.umr_wqe, offsetof(struct mlx5e_umr_wqe, inline_mtts));
526 
527 	for (i = 0; i < MLX5_MPWRQ_PAGES_PER_WQE; i++, dma_info++) {
528 		err = mlx5e_page_alloc(rq, dma_info);
529 		if (unlikely(err))
530 			goto err_unmap;
531 		umr_wqe->inline_mtts[i].ptag = cpu_to_be64(dma_info->addr | MLX5_EN_WR);
532 	}
533 
534 	bitmap_zero(wi->xdp_xmit_bitmap, MLX5_MPWRQ_PAGES_PER_WQE);
535 	wi->consumed_strides = 0;
536 
537 	umr_wqe->ctrl.opmod_idx_opcode =
538 		cpu_to_be32((sq->pc << MLX5_WQE_CTRL_WQE_INDEX_SHIFT) |
539 			    MLX5_OPCODE_UMR);
540 	umr_wqe->uctrl.xlt_offset =
541 		cpu_to_be16(MLX5_ALIGNED_MTTS_OCTW(MLX5E_REQUIRED_MTTS(ix)));
542 
543 	sq->db.wqe_info[pi] = (struct mlx5e_icosq_wqe_info) {
544 		.wqe_type   = MLX5E_ICOSQ_WQE_UMR_RX,
545 		.num_wqebbs = MLX5E_UMR_WQEBBS,
546 		.umr.rq     = rq,
547 	};
548 
549 	sq->pc += MLX5E_UMR_WQEBBS;
550 
551 	sq->doorbell_cseg = &umr_wqe->ctrl;
552 
553 	return 0;
554 
555 err_unmap:
556 	while (--i >= 0) {
557 		dma_info--;
558 		mlx5e_page_release(rq, dma_info, true);
559 	}
560 
561 err:
562 	rq->stats->buff_alloc_err++;
563 
564 	return err;
565 }
566 
mlx5e_dealloc_rx_mpwqe(struct mlx5e_rq * rq,u16 ix)567 static void mlx5e_dealloc_rx_mpwqe(struct mlx5e_rq *rq, u16 ix)
568 {
569 	struct mlx5e_mpw_info *wi = &rq->mpwqe.info[ix];
570 	/* Don't recycle, this function is called on rq/netdev close */
571 	mlx5e_free_rx_mpwqe(rq, wi, false);
572 }
573 
mlx5e_post_rx_wqes(struct mlx5e_rq * rq)574 INDIRECT_CALLABLE_SCOPE bool mlx5e_post_rx_wqes(struct mlx5e_rq *rq)
575 {
576 	struct mlx5_wq_cyc *wq = &rq->wqe.wq;
577 	u8 wqe_bulk;
578 	int err;
579 
580 	if (unlikely(!test_bit(MLX5E_RQ_STATE_ENABLED, &rq->state)))
581 		return false;
582 
583 	wqe_bulk = rq->wqe.info.wqe_bulk;
584 
585 	if (mlx5_wq_cyc_missing(wq) < wqe_bulk)
586 		return false;
587 
588 	do {
589 		u16 head = mlx5_wq_cyc_get_head(wq);
590 
591 		err = mlx5e_alloc_rx_wqes(rq, head, wqe_bulk);
592 		if (unlikely(err)) {
593 			rq->stats->buff_alloc_err++;
594 			break;
595 		}
596 
597 		mlx5_wq_cyc_push_n(wq, wqe_bulk);
598 	} while (mlx5_wq_cyc_missing(wq) >= wqe_bulk);
599 
600 	/* ensure wqes are visible to device before updating doorbell record */
601 	dma_wmb();
602 
603 	mlx5_wq_cyc_update_db_record(wq);
604 
605 	return !!err;
606 }
607 
mlx5e_free_icosq_descs(struct mlx5e_icosq * sq)608 void mlx5e_free_icosq_descs(struct mlx5e_icosq *sq)
609 {
610 	u16 sqcc;
611 
612 	sqcc = sq->cc;
613 
614 	while (sqcc != sq->pc) {
615 		struct mlx5e_icosq_wqe_info *wi;
616 		u16 ci;
617 
618 		ci = mlx5_wq_cyc_ctr2ix(&sq->wq, sqcc);
619 		wi = &sq->db.wqe_info[ci];
620 		sqcc += wi->num_wqebbs;
621 #ifdef CONFIG_MLX5_EN_TLS
622 		switch (wi->wqe_type) {
623 		case MLX5E_ICOSQ_WQE_SET_PSV_TLS:
624 			mlx5e_ktls_handle_ctx_completion(wi);
625 			break;
626 		case MLX5E_ICOSQ_WQE_GET_PSV_TLS:
627 			mlx5e_ktls_handle_get_psv_completion(wi, sq);
628 			break;
629 		}
630 #endif
631 	}
632 	sq->cc = sqcc;
633 }
634 
mlx5e_poll_ico_cq(struct mlx5e_cq * cq)635 int mlx5e_poll_ico_cq(struct mlx5e_cq *cq)
636 {
637 	struct mlx5e_icosq *sq = container_of(cq, struct mlx5e_icosq, cq);
638 	struct mlx5_cqe64 *cqe;
639 	u16 sqcc;
640 	int i;
641 
642 	if (unlikely(!test_bit(MLX5E_SQ_STATE_ENABLED, &sq->state)))
643 		return 0;
644 
645 	cqe = mlx5_cqwq_get_cqe(&cq->wq);
646 	if (likely(!cqe))
647 		return 0;
648 
649 	/* sq->cc must be updated only after mlx5_cqwq_update_db_record(),
650 	 * otherwise a cq overrun may occur
651 	 */
652 	sqcc = sq->cc;
653 
654 	i = 0;
655 	do {
656 		u16 wqe_counter;
657 		bool last_wqe;
658 
659 		mlx5_cqwq_pop(&cq->wq);
660 
661 		wqe_counter = be16_to_cpu(cqe->wqe_counter);
662 
663 		do {
664 			struct mlx5e_icosq_wqe_info *wi;
665 			u16 ci;
666 
667 			last_wqe = (sqcc == wqe_counter);
668 
669 			ci = mlx5_wq_cyc_ctr2ix(&sq->wq, sqcc);
670 			wi = &sq->db.wqe_info[ci];
671 			sqcc += wi->num_wqebbs;
672 
673 			if (last_wqe && unlikely(get_cqe_opcode(cqe) != MLX5_CQE_REQ)) {
674 				netdev_WARN_ONCE(cq->channel->netdev,
675 						 "Bad OP in ICOSQ CQE: 0x%x\n",
676 						 get_cqe_opcode(cqe));
677 				mlx5e_dump_error_cqe(&sq->cq, sq->sqn,
678 						     (struct mlx5_err_cqe *)cqe);
679 				if (!test_and_set_bit(MLX5E_SQ_STATE_RECOVERING, &sq->state))
680 					queue_work(cq->channel->priv->wq, &sq->recover_work);
681 				break;
682 			}
683 
684 			switch (wi->wqe_type) {
685 			case MLX5E_ICOSQ_WQE_UMR_RX:
686 				wi->umr.rq->mpwqe.umr_completed++;
687 				break;
688 			case MLX5E_ICOSQ_WQE_NOP:
689 				break;
690 #ifdef CONFIG_MLX5_EN_TLS
691 			case MLX5E_ICOSQ_WQE_UMR_TLS:
692 				break;
693 			case MLX5E_ICOSQ_WQE_SET_PSV_TLS:
694 				mlx5e_ktls_handle_ctx_completion(wi);
695 				break;
696 			case MLX5E_ICOSQ_WQE_GET_PSV_TLS:
697 				mlx5e_ktls_handle_get_psv_completion(wi, sq);
698 				break;
699 #endif
700 			default:
701 				netdev_WARN_ONCE(cq->channel->netdev,
702 						 "Bad WQE type in ICOSQ WQE info: 0x%x\n",
703 						 wi->wqe_type);
704 			}
705 		} while (!last_wqe);
706 	} while ((++i < MLX5E_TX_CQ_POLL_BUDGET) && (cqe = mlx5_cqwq_get_cqe(&cq->wq)));
707 
708 	sq->cc = sqcc;
709 
710 	mlx5_cqwq_update_db_record(&cq->wq);
711 
712 	return i;
713 }
714 
mlx5e_post_rx_mpwqes(struct mlx5e_rq * rq)715 INDIRECT_CALLABLE_SCOPE bool mlx5e_post_rx_mpwqes(struct mlx5e_rq *rq)
716 {
717 	struct mlx5e_icosq *sq = &rq->channel->icosq;
718 	struct mlx5_wq_ll *wq = &rq->mpwqe.wq;
719 	u8  umr_completed = rq->mpwqe.umr_completed;
720 	int alloc_err = 0;
721 	u8  missing, i;
722 	u16 head;
723 
724 	if (unlikely(!test_bit(MLX5E_RQ_STATE_ENABLED, &rq->state)))
725 		return false;
726 
727 	if (umr_completed) {
728 		mlx5e_post_rx_mpwqe(rq, umr_completed);
729 		rq->mpwqe.umr_in_progress -= umr_completed;
730 		rq->mpwqe.umr_completed = 0;
731 	}
732 
733 	missing = mlx5_wq_ll_missing(wq) - rq->mpwqe.umr_in_progress;
734 
735 	if (unlikely(rq->mpwqe.umr_in_progress > rq->mpwqe.umr_last_bulk))
736 		rq->stats->congst_umr++;
737 
738 #define UMR_WQE_BULK (2)
739 	if (likely(missing < UMR_WQE_BULK))
740 		return false;
741 
742 	head = rq->mpwqe.actual_wq_head;
743 	i = missing;
744 	do {
745 		alloc_err = mlx5e_alloc_rx_mpwqe(rq, head);
746 
747 		if (unlikely(alloc_err))
748 			break;
749 		head = mlx5_wq_ll_get_wqe_next_ix(wq, head);
750 	} while (--i);
751 
752 	rq->mpwqe.umr_last_bulk    = missing - i;
753 	if (sq->doorbell_cseg) {
754 		mlx5e_notify_hw(&sq->wq, sq->pc, sq->uar_map, sq->doorbell_cseg);
755 		sq->doorbell_cseg = NULL;
756 	}
757 
758 	rq->mpwqe.umr_in_progress += rq->mpwqe.umr_last_bulk;
759 	rq->mpwqe.actual_wq_head   = head;
760 
761 	/* If XSK Fill Ring doesn't have enough frames, report the error, so
762 	 * that one of the actions can be performed:
763 	 * 1. If need_wakeup is used, signal that the application has to kick
764 	 * the driver when it refills the Fill Ring.
765 	 * 2. Otherwise, busy poll by rescheduling the NAPI poll.
766 	 */
767 	if (unlikely(alloc_err == -ENOMEM && rq->xsk_pool))
768 		return true;
769 
770 	return false;
771 }
772 
mlx5e_lro_update_tcp_hdr(struct mlx5_cqe64 * cqe,struct tcphdr * tcp)773 static void mlx5e_lro_update_tcp_hdr(struct mlx5_cqe64 *cqe, struct tcphdr *tcp)
774 {
775 	u8 l4_hdr_type = get_cqe_l4_hdr_type(cqe);
776 	u8 tcp_ack     = (l4_hdr_type == CQE_L4_HDR_TYPE_TCP_ACK_NO_DATA) ||
777 			 (l4_hdr_type == CQE_L4_HDR_TYPE_TCP_ACK_AND_DATA);
778 
779 	tcp->check                      = 0;
780 	tcp->psh                        = get_cqe_lro_tcppsh(cqe);
781 
782 	if (tcp_ack) {
783 		tcp->ack                = 1;
784 		tcp->ack_seq            = cqe->lro_ack_seq_num;
785 		tcp->window             = cqe->lro_tcp_win;
786 	}
787 }
788 
mlx5e_lro_update_hdr(struct sk_buff * skb,struct mlx5_cqe64 * cqe,u32 cqe_bcnt)789 static void mlx5e_lro_update_hdr(struct sk_buff *skb, struct mlx5_cqe64 *cqe,
790 				 u32 cqe_bcnt)
791 {
792 	struct ethhdr	*eth = (struct ethhdr *)(skb->data);
793 	struct tcphdr	*tcp;
794 	int network_depth = 0;
795 	__wsum check;
796 	__be16 proto;
797 	u16 tot_len;
798 	void *ip_p;
799 
800 	proto = __vlan_get_protocol(skb, eth->h_proto, &network_depth);
801 
802 	tot_len = cqe_bcnt - network_depth;
803 	ip_p = skb->data + network_depth;
804 
805 	if (proto == htons(ETH_P_IP)) {
806 		struct iphdr *ipv4 = ip_p;
807 
808 		tcp = ip_p + sizeof(struct iphdr);
809 		skb_shinfo(skb)->gso_type = SKB_GSO_TCPV4;
810 
811 		ipv4->ttl               = cqe->lro_min_ttl;
812 		ipv4->tot_len           = cpu_to_be16(tot_len);
813 		ipv4->check             = 0;
814 		ipv4->check             = ip_fast_csum((unsigned char *)ipv4,
815 						       ipv4->ihl);
816 
817 		mlx5e_lro_update_tcp_hdr(cqe, tcp);
818 		check = csum_partial(tcp, tcp->doff * 4,
819 				     csum_unfold((__force __sum16)cqe->check_sum));
820 		/* Almost done, don't forget the pseudo header */
821 		tcp->check = csum_tcpudp_magic(ipv4->saddr, ipv4->daddr,
822 					       tot_len - sizeof(struct iphdr),
823 					       IPPROTO_TCP, check);
824 	} else {
825 		u16 payload_len = tot_len - sizeof(struct ipv6hdr);
826 		struct ipv6hdr *ipv6 = ip_p;
827 
828 		tcp = ip_p + sizeof(struct ipv6hdr);
829 		skb_shinfo(skb)->gso_type = SKB_GSO_TCPV6;
830 
831 		ipv6->hop_limit         = cqe->lro_min_ttl;
832 		ipv6->payload_len       = cpu_to_be16(payload_len);
833 
834 		mlx5e_lro_update_tcp_hdr(cqe, tcp);
835 		check = csum_partial(tcp, tcp->doff * 4,
836 				     csum_unfold((__force __sum16)cqe->check_sum));
837 		/* Almost done, don't forget the pseudo header */
838 		tcp->check = csum_ipv6_magic(&ipv6->saddr, &ipv6->daddr, payload_len,
839 					     IPPROTO_TCP, check);
840 	}
841 }
842 
mlx5e_skb_set_hash(struct mlx5_cqe64 * cqe,struct sk_buff * skb)843 static inline void mlx5e_skb_set_hash(struct mlx5_cqe64 *cqe,
844 				      struct sk_buff *skb)
845 {
846 	u8 cht = cqe->rss_hash_type;
847 	int ht = (cht & CQE_RSS_HTYPE_L4) ? PKT_HASH_TYPE_L4 :
848 		 (cht & CQE_RSS_HTYPE_IP) ? PKT_HASH_TYPE_L3 :
849 					    PKT_HASH_TYPE_NONE;
850 	skb_set_hash(skb, be32_to_cpu(cqe->rss_hash_result), ht);
851 }
852 
is_last_ethertype_ip(struct sk_buff * skb,int * network_depth,__be16 * proto)853 static inline bool is_last_ethertype_ip(struct sk_buff *skb, int *network_depth,
854 					__be16 *proto)
855 {
856 	*proto = ((struct ethhdr *)skb->data)->h_proto;
857 	*proto = __vlan_get_protocol(skb, *proto, network_depth);
858 
859 	if (*proto == htons(ETH_P_IP))
860 		return pskb_may_pull(skb, *network_depth + sizeof(struct iphdr));
861 
862 	if (*proto == htons(ETH_P_IPV6))
863 		return pskb_may_pull(skb, *network_depth + sizeof(struct ipv6hdr));
864 
865 	return false;
866 }
867 
mlx5e_enable_ecn(struct mlx5e_rq * rq,struct sk_buff * skb)868 static inline void mlx5e_enable_ecn(struct mlx5e_rq *rq, struct sk_buff *skb)
869 {
870 	int network_depth = 0;
871 	__be16 proto;
872 	void *ip;
873 	int rc;
874 
875 	if (unlikely(!is_last_ethertype_ip(skb, &network_depth, &proto)))
876 		return;
877 
878 	ip = skb->data + network_depth;
879 	rc = ((proto == htons(ETH_P_IP)) ? IP_ECN_set_ce((struct iphdr *)ip) :
880 					 IP6_ECN_set_ce(skb, (struct ipv6hdr *)ip));
881 
882 	rq->stats->ecn_mark += !!rc;
883 }
884 
get_ip_proto(struct sk_buff * skb,int network_depth,__be16 proto)885 static u8 get_ip_proto(struct sk_buff *skb, int network_depth, __be16 proto)
886 {
887 	void *ip_p = skb->data + network_depth;
888 
889 	return (proto == htons(ETH_P_IP)) ? ((struct iphdr *)ip_p)->protocol :
890 					    ((struct ipv6hdr *)ip_p)->nexthdr;
891 }
892 
893 #define short_frame(size) ((size) <= ETH_ZLEN + ETH_FCS_LEN)
894 
895 #define MAX_PADDING 8
896 
897 static void
tail_padding_csum_slow(struct sk_buff * skb,int offset,int len,struct mlx5e_rq_stats * stats)898 tail_padding_csum_slow(struct sk_buff *skb, int offset, int len,
899 		       struct mlx5e_rq_stats *stats)
900 {
901 	stats->csum_complete_tail_slow++;
902 	skb->csum = csum_block_add(skb->csum,
903 				   skb_checksum(skb, offset, len, 0),
904 				   offset);
905 }
906 
907 static void
tail_padding_csum(struct sk_buff * skb,int offset,struct mlx5e_rq_stats * stats)908 tail_padding_csum(struct sk_buff *skb, int offset,
909 		  struct mlx5e_rq_stats *stats)
910 {
911 	u8 tail_padding[MAX_PADDING];
912 	int len = skb->len - offset;
913 	void *tail;
914 
915 	if (unlikely(len > MAX_PADDING)) {
916 		tail_padding_csum_slow(skb, offset, len, stats);
917 		return;
918 	}
919 
920 	tail = skb_header_pointer(skb, offset, len, tail_padding);
921 	if (unlikely(!tail)) {
922 		tail_padding_csum_slow(skb, offset, len, stats);
923 		return;
924 	}
925 
926 	stats->csum_complete_tail++;
927 	skb->csum = csum_block_add(skb->csum, csum_partial(tail, len, 0), offset);
928 }
929 
930 static void
mlx5e_skb_csum_fixup(struct sk_buff * skb,int network_depth,__be16 proto,struct mlx5e_rq_stats * stats)931 mlx5e_skb_csum_fixup(struct sk_buff *skb, int network_depth, __be16 proto,
932 		     struct mlx5e_rq_stats *stats)
933 {
934 	struct ipv6hdr *ip6;
935 	struct iphdr   *ip4;
936 	int pkt_len;
937 
938 	/* Fixup vlan headers, if any */
939 	if (network_depth > ETH_HLEN)
940 		/* CQE csum is calculated from the IP header and does
941 		 * not cover VLAN headers (if present). This will add
942 		 * the checksum manually.
943 		 */
944 		skb->csum = csum_partial(skb->data + ETH_HLEN,
945 					 network_depth - ETH_HLEN,
946 					 skb->csum);
947 
948 	/* Fixup tail padding, if any */
949 	switch (proto) {
950 	case htons(ETH_P_IP):
951 		ip4 = (struct iphdr *)(skb->data + network_depth);
952 		pkt_len = network_depth + ntohs(ip4->tot_len);
953 		break;
954 	case htons(ETH_P_IPV6):
955 		ip6 = (struct ipv6hdr *)(skb->data + network_depth);
956 		pkt_len = network_depth + sizeof(*ip6) + ntohs(ip6->payload_len);
957 		break;
958 	default:
959 		return;
960 	}
961 
962 	if (likely(pkt_len >= skb->len))
963 		return;
964 
965 	tail_padding_csum(skb, pkt_len, stats);
966 }
967 
mlx5e_handle_csum(struct net_device * netdev,struct mlx5_cqe64 * cqe,struct mlx5e_rq * rq,struct sk_buff * skb,bool lro)968 static inline void mlx5e_handle_csum(struct net_device *netdev,
969 				     struct mlx5_cqe64 *cqe,
970 				     struct mlx5e_rq *rq,
971 				     struct sk_buff *skb,
972 				     bool   lro)
973 {
974 	struct mlx5e_rq_stats *stats = rq->stats;
975 	int network_depth = 0;
976 	__be16 proto;
977 
978 	if (unlikely(!(netdev->features & NETIF_F_RXCSUM)))
979 		goto csum_none;
980 
981 	if (lro) {
982 		skb->ip_summed = CHECKSUM_UNNECESSARY;
983 		stats->csum_unnecessary++;
984 		return;
985 	}
986 
987 	/* True when explicitly set via priv flag, or XDP prog is loaded */
988 	if (test_bit(MLX5E_RQ_STATE_NO_CSUM_COMPLETE, &rq->state) ||
989 	    get_cqe_tls_offload(cqe))
990 		goto csum_unnecessary;
991 
992 	/* CQE csum doesn't cover padding octets in short ethernet
993 	 * frames. And the pad field is appended prior to calculating
994 	 * and appending the FCS field.
995 	 *
996 	 * Detecting these padded frames requires to verify and parse
997 	 * IP headers, so we simply force all those small frames to be
998 	 * CHECKSUM_UNNECESSARY even if they are not padded.
999 	 */
1000 	if (short_frame(skb->len))
1001 		goto csum_unnecessary;
1002 
1003 	if (likely(is_last_ethertype_ip(skb, &network_depth, &proto))) {
1004 		if (unlikely(get_ip_proto(skb, network_depth, proto) == IPPROTO_SCTP))
1005 			goto csum_unnecessary;
1006 
1007 		stats->csum_complete++;
1008 		skb->ip_summed = CHECKSUM_COMPLETE;
1009 		skb->csum = csum_unfold((__force __sum16)cqe->check_sum);
1010 
1011 		if (test_bit(MLX5E_RQ_STATE_CSUM_FULL, &rq->state))
1012 			return; /* CQE csum covers all received bytes */
1013 
1014 		/* csum might need some fixups ...*/
1015 		mlx5e_skb_csum_fixup(skb, network_depth, proto, stats);
1016 		return;
1017 	}
1018 
1019 csum_unnecessary:
1020 	if (likely((cqe->hds_ip_ext & CQE_L3_OK) &&
1021 		   (cqe->hds_ip_ext & CQE_L4_OK))) {
1022 		skb->ip_summed = CHECKSUM_UNNECESSARY;
1023 		if (cqe_is_tunneled(cqe)) {
1024 			skb->csum_level = 1;
1025 			skb->encapsulation = 1;
1026 			stats->csum_unnecessary_inner++;
1027 			return;
1028 		}
1029 		stats->csum_unnecessary++;
1030 		return;
1031 	}
1032 csum_none:
1033 	skb->ip_summed = CHECKSUM_NONE;
1034 	stats->csum_none++;
1035 }
1036 
1037 #define MLX5E_CE_BIT_MASK 0x80
1038 
mlx5e_build_rx_skb(struct mlx5_cqe64 * cqe,u32 cqe_bcnt,struct mlx5e_rq * rq,struct sk_buff * skb)1039 static inline void mlx5e_build_rx_skb(struct mlx5_cqe64 *cqe,
1040 				      u32 cqe_bcnt,
1041 				      struct mlx5e_rq *rq,
1042 				      struct sk_buff *skb)
1043 {
1044 	u8 lro_num_seg = be32_to_cpu(cqe->srqn) >> 24;
1045 	struct mlx5e_rq_stats *stats = rq->stats;
1046 	struct net_device *netdev = rq->netdev;
1047 
1048 	skb->mac_len = ETH_HLEN;
1049 
1050 	mlx5e_tls_handle_rx_skb(rq, skb, cqe, &cqe_bcnt);
1051 
1052 	if (unlikely(mlx5_ipsec_is_rx_flow(cqe)))
1053 		mlx5e_ipsec_offload_handle_rx_skb(netdev, skb, cqe);
1054 
1055 	if (lro_num_seg > 1) {
1056 		mlx5e_lro_update_hdr(skb, cqe, cqe_bcnt);
1057 		skb_shinfo(skb)->gso_size = DIV_ROUND_UP(cqe_bcnt, lro_num_seg);
1058 		/* Subtract one since we already counted this as one
1059 		 * "regular" packet in mlx5e_complete_rx_cqe()
1060 		 */
1061 		stats->packets += lro_num_seg - 1;
1062 		stats->lro_packets++;
1063 		stats->lro_bytes += cqe_bcnt;
1064 	}
1065 
1066 	if (unlikely(mlx5e_rx_hw_stamp(rq->tstamp)))
1067 		skb_hwtstamps(skb)->hwtstamp =
1068 				mlx5_timecounter_cyc2time(rq->clock, get_cqe_ts(cqe));
1069 
1070 	skb_record_rx_queue(skb, rq->ix);
1071 
1072 	if (likely(netdev->features & NETIF_F_RXHASH))
1073 		mlx5e_skb_set_hash(cqe, skb);
1074 
1075 	if (cqe_has_vlan(cqe)) {
1076 		__vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q),
1077 				       be16_to_cpu(cqe->vlan_info));
1078 		stats->removed_vlan_packets++;
1079 	}
1080 
1081 	skb->mark = be32_to_cpu(cqe->sop_drop_qpn) & MLX5E_TC_FLOW_ID_MASK;
1082 
1083 	mlx5e_handle_csum(netdev, cqe, rq, skb, !!lro_num_seg);
1084 	/* checking CE bit in cqe - MSB in ml_path field */
1085 	if (unlikely(cqe->ml_path & MLX5E_CE_BIT_MASK))
1086 		mlx5e_enable_ecn(rq, skb);
1087 
1088 	skb->protocol = eth_type_trans(skb, netdev);
1089 
1090 	if (unlikely(mlx5e_skb_is_multicast(skb)))
1091 		stats->mcast_packets++;
1092 }
1093 
mlx5e_complete_rx_cqe(struct mlx5e_rq * rq,struct mlx5_cqe64 * cqe,u32 cqe_bcnt,struct sk_buff * skb)1094 static inline void mlx5e_complete_rx_cqe(struct mlx5e_rq *rq,
1095 					 struct mlx5_cqe64 *cqe,
1096 					 u32 cqe_bcnt,
1097 					 struct sk_buff *skb)
1098 {
1099 	struct mlx5e_rq_stats *stats = rq->stats;
1100 
1101 	stats->packets++;
1102 	stats->bytes += cqe_bcnt;
1103 	mlx5e_build_rx_skb(cqe, cqe_bcnt, rq, skb);
1104 }
1105 
1106 static inline
mlx5e_build_linear_skb(struct mlx5e_rq * rq,void * va,u32 frag_size,u16 headroom,u32 cqe_bcnt)1107 struct sk_buff *mlx5e_build_linear_skb(struct mlx5e_rq *rq, void *va,
1108 				       u32 frag_size, u16 headroom,
1109 				       u32 cqe_bcnt)
1110 {
1111 	struct sk_buff *skb = build_skb(va, frag_size);
1112 
1113 	if (unlikely(!skb)) {
1114 		rq->stats->buff_alloc_err++;
1115 		return NULL;
1116 	}
1117 
1118 	skb_reserve(skb, headroom);
1119 	skb_put(skb, cqe_bcnt);
1120 
1121 	return skb;
1122 }
1123 
mlx5e_fill_xdp_buff(struct mlx5e_rq * rq,void * va,u16 headroom,u32 len,struct xdp_buff * xdp)1124 static void mlx5e_fill_xdp_buff(struct mlx5e_rq *rq, void *va, u16 headroom,
1125 				u32 len, struct xdp_buff *xdp)
1126 {
1127 	xdp->data_hard_start = va;
1128 	xdp->data = va + headroom;
1129 	xdp_set_data_meta_invalid(xdp);
1130 	xdp->data_end = xdp->data + len;
1131 	xdp->rxq = &rq->xdp_rxq;
1132 	xdp->frame_sz = rq->buff.frame0_sz;
1133 }
1134 
1135 static struct sk_buff *
mlx5e_skb_from_cqe_linear(struct mlx5e_rq * rq,struct mlx5_cqe64 * cqe,struct mlx5e_wqe_frag_info * wi,u32 cqe_bcnt)1136 mlx5e_skb_from_cqe_linear(struct mlx5e_rq *rq, struct mlx5_cqe64 *cqe,
1137 			  struct mlx5e_wqe_frag_info *wi, u32 cqe_bcnt)
1138 {
1139 	struct mlx5e_dma_info *di = wi->di;
1140 	u16 rx_headroom = rq->buff.headroom;
1141 	struct xdp_buff xdp;
1142 	struct sk_buff *skb;
1143 	void *va, *data;
1144 	u32 frag_size;
1145 
1146 	va             = page_address(di->page) + wi->offset;
1147 	data           = va + rx_headroom;
1148 	frag_size      = MLX5_SKB_FRAG_SZ(rx_headroom + cqe_bcnt);
1149 
1150 	dma_sync_single_range_for_cpu(rq->pdev, di->addr, wi->offset,
1151 				      frag_size, DMA_FROM_DEVICE);
1152 	net_prefetchw(va); /* xdp_frame data area */
1153 	net_prefetch(data);
1154 
1155 	mlx5e_fill_xdp_buff(rq, va, rx_headroom, cqe_bcnt, &xdp);
1156 	if (mlx5e_xdp_handle(rq, di, &cqe_bcnt, &xdp))
1157 		return NULL; /* page/packet was consumed by XDP */
1158 
1159 	rx_headroom = xdp.data - xdp.data_hard_start;
1160 	frag_size = MLX5_SKB_FRAG_SZ(rx_headroom + cqe_bcnt);
1161 	skb = mlx5e_build_linear_skb(rq, va, frag_size, rx_headroom, cqe_bcnt);
1162 	if (unlikely(!skb))
1163 		return NULL;
1164 
1165 	/* queue up for recycling/reuse */
1166 	page_ref_inc(di->page);
1167 
1168 	return skb;
1169 }
1170 
1171 static struct sk_buff *
mlx5e_skb_from_cqe_nonlinear(struct mlx5e_rq * rq,struct mlx5_cqe64 * cqe,struct mlx5e_wqe_frag_info * wi,u32 cqe_bcnt)1172 mlx5e_skb_from_cqe_nonlinear(struct mlx5e_rq *rq, struct mlx5_cqe64 *cqe,
1173 			     struct mlx5e_wqe_frag_info *wi, u32 cqe_bcnt)
1174 {
1175 	struct mlx5e_rq_frag_info *frag_info = &rq->wqe.info.arr[0];
1176 	struct mlx5e_wqe_frag_info *head_wi = wi;
1177 	u16 headlen      = min_t(u32, MLX5E_RX_MAX_HEAD, cqe_bcnt);
1178 	u16 frag_headlen = headlen;
1179 	u16 byte_cnt     = cqe_bcnt - headlen;
1180 	struct sk_buff *skb;
1181 
1182 	/* XDP is not supported in this configuration, as incoming packets
1183 	 * might spread among multiple pages.
1184 	 */
1185 	skb = napi_alloc_skb(rq->cq.napi,
1186 			     ALIGN(MLX5E_RX_MAX_HEAD, sizeof(long)));
1187 	if (unlikely(!skb)) {
1188 		rq->stats->buff_alloc_err++;
1189 		return NULL;
1190 	}
1191 
1192 	net_prefetchw(skb->data);
1193 
1194 	while (byte_cnt) {
1195 		u16 frag_consumed_bytes =
1196 			min_t(u16, frag_info->frag_size - frag_headlen, byte_cnt);
1197 
1198 		mlx5e_add_skb_frag(rq, skb, wi->di, wi->offset + frag_headlen,
1199 				   frag_consumed_bytes, frag_info->frag_stride);
1200 		byte_cnt -= frag_consumed_bytes;
1201 		frag_headlen = 0;
1202 		frag_info++;
1203 		wi++;
1204 	}
1205 
1206 	/* copy header */
1207 	mlx5e_copy_skb_header(rq->pdev, skb, head_wi->di, head_wi->offset, headlen);
1208 	/* skb linear part was allocated with headlen and aligned to long */
1209 	skb->tail += headlen;
1210 	skb->len  += headlen;
1211 
1212 	return skb;
1213 }
1214 
trigger_report(struct mlx5e_rq * rq,struct mlx5_cqe64 * cqe)1215 static void trigger_report(struct mlx5e_rq *rq, struct mlx5_cqe64 *cqe)
1216 {
1217 	struct mlx5_err_cqe *err_cqe = (struct mlx5_err_cqe *)cqe;
1218 
1219 	if (cqe_syndrome_needs_recover(err_cqe->syndrome) &&
1220 	    !test_and_set_bit(MLX5E_RQ_STATE_RECOVERING, &rq->state)) {
1221 		mlx5e_dump_error_cqe(&rq->cq, rq->rqn, err_cqe);
1222 		queue_work(rq->channel->priv->wq, &rq->recover_work);
1223 	}
1224 }
1225 
mlx5e_handle_rx_cqe(struct mlx5e_rq * rq,struct mlx5_cqe64 * cqe)1226 static void mlx5e_handle_rx_cqe(struct mlx5e_rq *rq, struct mlx5_cqe64 *cqe)
1227 {
1228 	struct mlx5_wq_cyc *wq = &rq->wqe.wq;
1229 	struct mlx5e_wqe_frag_info *wi;
1230 	struct sk_buff *skb;
1231 	u32 cqe_bcnt;
1232 	u16 ci;
1233 
1234 	ci       = mlx5_wq_cyc_ctr2ix(wq, be16_to_cpu(cqe->wqe_counter));
1235 	wi       = get_frag(rq, ci);
1236 	cqe_bcnt = be32_to_cpu(cqe->byte_cnt);
1237 
1238 	if (unlikely(MLX5E_RX_ERR_CQE(cqe))) {
1239 		trigger_report(rq, cqe);
1240 		rq->stats->wqe_err++;
1241 		goto free_wqe;
1242 	}
1243 
1244 	skb = INDIRECT_CALL_2(rq->wqe.skb_from_cqe,
1245 			      mlx5e_skb_from_cqe_linear,
1246 			      mlx5e_skb_from_cqe_nonlinear,
1247 			      rq, cqe, wi, cqe_bcnt);
1248 	if (!skb) {
1249 		/* probably for XDP */
1250 		if (__test_and_clear_bit(MLX5E_RQ_FLAG_XDP_XMIT, rq->flags)) {
1251 			/* do not return page to cache,
1252 			 * it will be returned on XDP_TX completion.
1253 			 */
1254 			goto wq_cyc_pop;
1255 		}
1256 		goto free_wqe;
1257 	}
1258 
1259 	mlx5e_complete_rx_cqe(rq, cqe, cqe_bcnt, skb);
1260 
1261 	if (mlx5e_cqe_regb_chain(cqe))
1262 		if (!mlx5e_tc_update_skb(cqe, skb)) {
1263 			dev_kfree_skb_any(skb);
1264 			goto free_wqe;
1265 		}
1266 
1267 	napi_gro_receive(rq->cq.napi, skb);
1268 
1269 free_wqe:
1270 	mlx5e_free_rx_wqe(rq, wi, true);
1271 wq_cyc_pop:
1272 	mlx5_wq_cyc_pop(wq);
1273 }
1274 
1275 #ifdef CONFIG_MLX5_ESWITCH
mlx5e_handle_rx_cqe_rep(struct mlx5e_rq * rq,struct mlx5_cqe64 * cqe)1276 static void mlx5e_handle_rx_cqe_rep(struct mlx5e_rq *rq, struct mlx5_cqe64 *cqe)
1277 {
1278 	struct net_device *netdev = rq->netdev;
1279 	struct mlx5e_priv *priv = netdev_priv(netdev);
1280 	struct mlx5e_rep_priv *rpriv  = priv->ppriv;
1281 	struct mlx5_eswitch_rep *rep = rpriv->rep;
1282 	struct mlx5e_tc_update_priv tc_priv = {};
1283 	struct mlx5_wq_cyc *wq = &rq->wqe.wq;
1284 	struct mlx5e_wqe_frag_info *wi;
1285 	struct sk_buff *skb;
1286 	u32 cqe_bcnt;
1287 	u16 ci;
1288 
1289 	ci       = mlx5_wq_cyc_ctr2ix(wq, be16_to_cpu(cqe->wqe_counter));
1290 	wi       = get_frag(rq, ci);
1291 	cqe_bcnt = be32_to_cpu(cqe->byte_cnt);
1292 
1293 	if (unlikely(MLX5E_RX_ERR_CQE(cqe))) {
1294 		rq->stats->wqe_err++;
1295 		goto free_wqe;
1296 	}
1297 
1298 	skb = INDIRECT_CALL_2(rq->wqe.skb_from_cqe,
1299 			      mlx5e_skb_from_cqe_linear,
1300 			      mlx5e_skb_from_cqe_nonlinear,
1301 			      rq, cqe, wi, cqe_bcnt);
1302 	if (!skb) {
1303 		/* probably for XDP */
1304 		if (__test_and_clear_bit(MLX5E_RQ_FLAG_XDP_XMIT, rq->flags)) {
1305 			/* do not return page to cache,
1306 			 * it will be returned on XDP_TX completion.
1307 			 */
1308 			goto wq_cyc_pop;
1309 		}
1310 		goto free_wqe;
1311 	}
1312 
1313 	mlx5e_complete_rx_cqe(rq, cqe, cqe_bcnt, skb);
1314 
1315 	if (rep->vlan && skb_vlan_tag_present(skb))
1316 		skb_vlan_pop(skb);
1317 
1318 	if (unlikely(!mlx5_ipsec_is_rx_flow(cqe) &&
1319 		     !mlx5e_rep_tc_update_skb(cqe, skb, &tc_priv))) {
1320 		dev_kfree_skb_any(skb);
1321 		goto free_wqe;
1322 	}
1323 
1324 	napi_gro_receive(rq->cq.napi, skb);
1325 
1326 	mlx5_rep_tc_post_napi_receive(&tc_priv);
1327 
1328 free_wqe:
1329 	mlx5e_free_rx_wqe(rq, wi, true);
1330 wq_cyc_pop:
1331 	mlx5_wq_cyc_pop(wq);
1332 }
1333 
mlx5e_handle_rx_cqe_mpwrq_rep(struct mlx5e_rq * rq,struct mlx5_cqe64 * cqe)1334 static void mlx5e_handle_rx_cqe_mpwrq_rep(struct mlx5e_rq *rq, struct mlx5_cqe64 *cqe)
1335 {
1336 	u16 cstrides       = mpwrq_get_cqe_consumed_strides(cqe);
1337 	u16 wqe_id         = be16_to_cpu(cqe->wqe_id);
1338 	struct mlx5e_mpw_info *wi = &rq->mpwqe.info[wqe_id];
1339 	u16 stride_ix      = mpwrq_get_cqe_stride_index(cqe);
1340 	u32 wqe_offset     = stride_ix << rq->mpwqe.log_stride_sz;
1341 	u32 head_offset    = wqe_offset & (PAGE_SIZE - 1);
1342 	u32 page_idx       = wqe_offset >> PAGE_SHIFT;
1343 	struct mlx5e_tc_update_priv tc_priv = {};
1344 	struct mlx5e_rx_wqe_ll *wqe;
1345 	struct mlx5_wq_ll *wq;
1346 	struct sk_buff *skb;
1347 	u16 cqe_bcnt;
1348 
1349 	wi->consumed_strides += cstrides;
1350 
1351 	if (unlikely(MLX5E_RX_ERR_CQE(cqe))) {
1352 		trigger_report(rq, cqe);
1353 		rq->stats->wqe_err++;
1354 		goto mpwrq_cqe_out;
1355 	}
1356 
1357 	if (unlikely(mpwrq_is_filler_cqe(cqe))) {
1358 		struct mlx5e_rq_stats *stats = rq->stats;
1359 
1360 		stats->mpwqe_filler_cqes++;
1361 		stats->mpwqe_filler_strides += cstrides;
1362 		goto mpwrq_cqe_out;
1363 	}
1364 
1365 	cqe_bcnt = mpwrq_get_cqe_byte_cnt(cqe);
1366 
1367 	skb = INDIRECT_CALL_2(rq->mpwqe.skb_from_cqe_mpwrq,
1368 			      mlx5e_skb_from_cqe_mpwrq_linear,
1369 			      mlx5e_skb_from_cqe_mpwrq_nonlinear,
1370 			      rq, wi, cqe_bcnt, head_offset, page_idx);
1371 	if (!skb)
1372 		goto mpwrq_cqe_out;
1373 
1374 	mlx5e_complete_rx_cqe(rq, cqe, cqe_bcnt, skb);
1375 
1376 	if (unlikely(!mlx5_ipsec_is_rx_flow(cqe) &&
1377 		     !mlx5e_rep_tc_update_skb(cqe, skb, &tc_priv))) {
1378 		dev_kfree_skb_any(skb);
1379 		goto mpwrq_cqe_out;
1380 	}
1381 
1382 	napi_gro_receive(rq->cq.napi, skb);
1383 
1384 	mlx5_rep_tc_post_napi_receive(&tc_priv);
1385 
1386 mpwrq_cqe_out:
1387 	if (likely(wi->consumed_strides < rq->mpwqe.num_strides))
1388 		return;
1389 
1390 	wq  = &rq->mpwqe.wq;
1391 	wqe = mlx5_wq_ll_get_wqe(wq, wqe_id);
1392 	mlx5e_free_rx_mpwqe(rq, wi, true);
1393 	mlx5_wq_ll_pop(wq, cqe->wqe_id, &wqe->next.next_wqe_index);
1394 }
1395 
1396 const struct mlx5e_rx_handlers mlx5e_rx_handlers_rep = {
1397 	.handle_rx_cqe       = mlx5e_handle_rx_cqe_rep,
1398 	.handle_rx_cqe_mpwqe = mlx5e_handle_rx_cqe_mpwrq_rep,
1399 };
1400 #endif
1401 
1402 static struct sk_buff *
mlx5e_skb_from_cqe_mpwrq_nonlinear(struct mlx5e_rq * rq,struct mlx5e_mpw_info * wi,u16 cqe_bcnt,u32 head_offset,u32 page_idx)1403 mlx5e_skb_from_cqe_mpwrq_nonlinear(struct mlx5e_rq *rq, struct mlx5e_mpw_info *wi,
1404 				   u16 cqe_bcnt, u32 head_offset, u32 page_idx)
1405 {
1406 	u16 headlen = min_t(u16, MLX5E_RX_MAX_HEAD, cqe_bcnt);
1407 	struct mlx5e_dma_info *di = &wi->umr.dma_info[page_idx];
1408 	u32 frag_offset    = head_offset + headlen;
1409 	u32 byte_cnt       = cqe_bcnt - headlen;
1410 	struct mlx5e_dma_info *head_di = di;
1411 	struct sk_buff *skb;
1412 
1413 	skb = napi_alloc_skb(rq->cq.napi,
1414 			     ALIGN(MLX5E_RX_MAX_HEAD, sizeof(long)));
1415 	if (unlikely(!skb)) {
1416 		rq->stats->buff_alloc_err++;
1417 		return NULL;
1418 	}
1419 
1420 	net_prefetchw(skb->data);
1421 
1422 	if (unlikely(frag_offset >= PAGE_SIZE)) {
1423 		di++;
1424 		frag_offset -= PAGE_SIZE;
1425 	}
1426 
1427 	while (byte_cnt) {
1428 		u32 pg_consumed_bytes =
1429 			min_t(u32, PAGE_SIZE - frag_offset, byte_cnt);
1430 		unsigned int truesize =
1431 			ALIGN(pg_consumed_bytes, BIT(rq->mpwqe.log_stride_sz));
1432 
1433 		mlx5e_add_skb_frag(rq, skb, di, frag_offset,
1434 				   pg_consumed_bytes, truesize);
1435 		byte_cnt -= pg_consumed_bytes;
1436 		frag_offset = 0;
1437 		di++;
1438 	}
1439 	/* copy header */
1440 	mlx5e_copy_skb_header(rq->pdev, skb, head_di, head_offset, headlen);
1441 	/* skb linear part was allocated with headlen and aligned to long */
1442 	skb->tail += headlen;
1443 	skb->len  += headlen;
1444 
1445 	return skb;
1446 }
1447 
1448 static struct sk_buff *
mlx5e_skb_from_cqe_mpwrq_linear(struct mlx5e_rq * rq,struct mlx5e_mpw_info * wi,u16 cqe_bcnt,u32 head_offset,u32 page_idx)1449 mlx5e_skb_from_cqe_mpwrq_linear(struct mlx5e_rq *rq, struct mlx5e_mpw_info *wi,
1450 				u16 cqe_bcnt, u32 head_offset, u32 page_idx)
1451 {
1452 	struct mlx5e_dma_info *di = &wi->umr.dma_info[page_idx];
1453 	u16 rx_headroom = rq->buff.headroom;
1454 	u32 cqe_bcnt32 = cqe_bcnt;
1455 	struct xdp_buff xdp;
1456 	struct sk_buff *skb;
1457 	void *va, *data;
1458 	u32 frag_size;
1459 
1460 	/* Check packet size. Note LRO doesn't use linear SKB */
1461 	if (unlikely(cqe_bcnt > rq->hw_mtu)) {
1462 		rq->stats->oversize_pkts_sw_drop++;
1463 		return NULL;
1464 	}
1465 
1466 	va             = page_address(di->page) + head_offset;
1467 	data           = va + rx_headroom;
1468 	frag_size      = MLX5_SKB_FRAG_SZ(rx_headroom + cqe_bcnt32);
1469 
1470 	dma_sync_single_range_for_cpu(rq->pdev, di->addr, head_offset,
1471 				      frag_size, DMA_FROM_DEVICE);
1472 	net_prefetchw(va); /* xdp_frame data area */
1473 	net_prefetch(data);
1474 
1475 	mlx5e_fill_xdp_buff(rq, va, rx_headroom, cqe_bcnt32, &xdp);
1476 	if (mlx5e_xdp_handle(rq, di, &cqe_bcnt32, &xdp)) {
1477 		if (__test_and_clear_bit(MLX5E_RQ_FLAG_XDP_XMIT, rq->flags))
1478 			__set_bit(page_idx, wi->xdp_xmit_bitmap); /* non-atomic */
1479 		return NULL; /* page/packet was consumed by XDP */
1480 	}
1481 
1482 	rx_headroom = xdp.data - xdp.data_hard_start;
1483 	frag_size = MLX5_SKB_FRAG_SZ(rx_headroom + cqe_bcnt32);
1484 	skb = mlx5e_build_linear_skb(rq, va, frag_size, rx_headroom, cqe_bcnt32);
1485 	if (unlikely(!skb))
1486 		return NULL;
1487 
1488 	/* queue up for recycling/reuse */
1489 	page_ref_inc(di->page);
1490 
1491 	return skb;
1492 }
1493 
mlx5e_handle_rx_cqe_mpwrq(struct mlx5e_rq * rq,struct mlx5_cqe64 * cqe)1494 static void mlx5e_handle_rx_cqe_mpwrq(struct mlx5e_rq *rq, struct mlx5_cqe64 *cqe)
1495 {
1496 	u16 cstrides       = mpwrq_get_cqe_consumed_strides(cqe);
1497 	u16 wqe_id         = be16_to_cpu(cqe->wqe_id);
1498 	struct mlx5e_mpw_info *wi = &rq->mpwqe.info[wqe_id];
1499 	u16 stride_ix      = mpwrq_get_cqe_stride_index(cqe);
1500 	u32 wqe_offset     = stride_ix << rq->mpwqe.log_stride_sz;
1501 	u32 head_offset    = wqe_offset & (PAGE_SIZE - 1);
1502 	u32 page_idx       = wqe_offset >> PAGE_SHIFT;
1503 	struct mlx5e_rx_wqe_ll *wqe;
1504 	struct mlx5_wq_ll *wq;
1505 	struct sk_buff *skb;
1506 	u16 cqe_bcnt;
1507 
1508 	wi->consumed_strides += cstrides;
1509 
1510 	if (unlikely(MLX5E_RX_ERR_CQE(cqe))) {
1511 		trigger_report(rq, cqe);
1512 		rq->stats->wqe_err++;
1513 		goto mpwrq_cqe_out;
1514 	}
1515 
1516 	if (unlikely(mpwrq_is_filler_cqe(cqe))) {
1517 		struct mlx5e_rq_stats *stats = rq->stats;
1518 
1519 		stats->mpwqe_filler_cqes++;
1520 		stats->mpwqe_filler_strides += cstrides;
1521 		goto mpwrq_cqe_out;
1522 	}
1523 
1524 	cqe_bcnt = mpwrq_get_cqe_byte_cnt(cqe);
1525 
1526 	skb = INDIRECT_CALL_2(rq->mpwqe.skb_from_cqe_mpwrq,
1527 			      mlx5e_skb_from_cqe_mpwrq_linear,
1528 			      mlx5e_skb_from_cqe_mpwrq_nonlinear,
1529 			      rq, wi, cqe_bcnt, head_offset, page_idx);
1530 	if (!skb)
1531 		goto mpwrq_cqe_out;
1532 
1533 	mlx5e_complete_rx_cqe(rq, cqe, cqe_bcnt, skb);
1534 
1535 	if (mlx5e_cqe_regb_chain(cqe))
1536 		if (!mlx5e_tc_update_skb(cqe, skb)) {
1537 			dev_kfree_skb_any(skb);
1538 			goto mpwrq_cqe_out;
1539 		}
1540 
1541 	napi_gro_receive(rq->cq.napi, skb);
1542 
1543 mpwrq_cqe_out:
1544 	if (likely(wi->consumed_strides < rq->mpwqe.num_strides))
1545 		return;
1546 
1547 	wq  = &rq->mpwqe.wq;
1548 	wqe = mlx5_wq_ll_get_wqe(wq, wqe_id);
1549 	mlx5e_free_rx_mpwqe(rq, wi, true);
1550 	mlx5_wq_ll_pop(wq, cqe->wqe_id, &wqe->next.next_wqe_index);
1551 }
1552 
mlx5e_poll_rx_cq(struct mlx5e_cq * cq,int budget)1553 int mlx5e_poll_rx_cq(struct mlx5e_cq *cq, int budget)
1554 {
1555 	struct mlx5e_rq *rq = container_of(cq, struct mlx5e_rq, cq);
1556 	struct mlx5_cqwq *cqwq = &cq->wq;
1557 	struct mlx5_cqe64 *cqe;
1558 	int work_done = 0;
1559 
1560 	if (unlikely(!test_bit(MLX5E_RQ_STATE_ENABLED, &rq->state)))
1561 		return 0;
1562 
1563 	if (rq->page_pool)
1564 		page_pool_nid_changed(rq->page_pool, numa_mem_id());
1565 
1566 	if (rq->cqd.left) {
1567 		work_done += mlx5e_decompress_cqes_cont(rq, cqwq, 0, budget);
1568 		if (rq->cqd.left || work_done >= budget)
1569 			goto out;
1570 	}
1571 
1572 	cqe = mlx5_cqwq_get_cqe(cqwq);
1573 	if (!cqe) {
1574 		if (unlikely(work_done))
1575 			goto out;
1576 		return 0;
1577 	}
1578 
1579 	do {
1580 		if (mlx5_get_cqe_format(cqe) == MLX5_COMPRESSED) {
1581 			work_done +=
1582 				mlx5e_decompress_cqes_start(rq, cqwq,
1583 							    budget - work_done);
1584 			continue;
1585 		}
1586 
1587 		mlx5_cqwq_pop(cqwq);
1588 
1589 		INDIRECT_CALL_2(rq->handle_rx_cqe, mlx5e_handle_rx_cqe_mpwrq,
1590 				mlx5e_handle_rx_cqe, rq, cqe);
1591 	} while ((++work_done < budget) && (cqe = mlx5_cqwq_get_cqe(cqwq)));
1592 
1593 out:
1594 	if (rcu_access_pointer(rq->xdp_prog))
1595 		mlx5e_xdp_rx_poll_complete(rq);
1596 
1597 	mlx5_cqwq_update_db_record(cqwq);
1598 
1599 	/* ensure cq space is freed before enabling more cqes */
1600 	wmb();
1601 
1602 	return work_done;
1603 }
1604 
1605 #ifdef CONFIG_MLX5_CORE_IPOIB
1606 
1607 #define MLX5_IB_GRH_SGID_OFFSET 8
1608 #define MLX5_IB_GRH_DGID_OFFSET 24
1609 #define MLX5_GID_SIZE           16
1610 
mlx5i_complete_rx_cqe(struct mlx5e_rq * rq,struct mlx5_cqe64 * cqe,u32 cqe_bcnt,struct sk_buff * skb)1611 static inline void mlx5i_complete_rx_cqe(struct mlx5e_rq *rq,
1612 					 struct mlx5_cqe64 *cqe,
1613 					 u32 cqe_bcnt,
1614 					 struct sk_buff *skb)
1615 {
1616 	struct hwtstamp_config *tstamp;
1617 	struct mlx5e_rq_stats *stats;
1618 	struct net_device *netdev;
1619 	struct mlx5e_priv *priv;
1620 	char *pseudo_header;
1621 	u32 flags_rqpn;
1622 	u32 qpn;
1623 	u8 *dgid;
1624 	u8 g;
1625 
1626 	qpn = be32_to_cpu(cqe->sop_drop_qpn) & 0xffffff;
1627 	netdev = mlx5i_pkey_get_netdev(rq->netdev, qpn);
1628 
1629 	/* No mapping present, cannot process SKB. This might happen if a child
1630 	 * interface is going down while having unprocessed CQEs on parent RQ
1631 	 */
1632 	if (unlikely(!netdev)) {
1633 		/* TODO: add drop counters support */
1634 		skb->dev = NULL;
1635 		pr_warn_once("Unable to map QPN %u to dev - dropping skb\n", qpn);
1636 		return;
1637 	}
1638 
1639 	priv = mlx5i_epriv(netdev);
1640 	tstamp = &priv->tstamp;
1641 	stats = &priv->channel_stats[rq->ix].rq;
1642 
1643 	flags_rqpn = be32_to_cpu(cqe->flags_rqpn);
1644 	g = (flags_rqpn >> 28) & 3;
1645 	dgid = skb->data + MLX5_IB_GRH_DGID_OFFSET;
1646 	if ((!g) || dgid[0] != 0xff)
1647 		skb->pkt_type = PACKET_HOST;
1648 	else if (memcmp(dgid, netdev->broadcast + 4, MLX5_GID_SIZE) == 0)
1649 		skb->pkt_type = PACKET_BROADCAST;
1650 	else
1651 		skb->pkt_type = PACKET_MULTICAST;
1652 
1653 	/* Drop packets that this interface sent, ie multicast packets
1654 	 * that the HCA has replicated.
1655 	 */
1656 	if (g && (qpn == (flags_rqpn & 0xffffff)) &&
1657 	    (memcmp(netdev->dev_addr + 4, skb->data + MLX5_IB_GRH_SGID_OFFSET,
1658 		    MLX5_GID_SIZE) == 0)) {
1659 		skb->dev = NULL;
1660 		return;
1661 	}
1662 
1663 	skb_pull(skb, MLX5_IB_GRH_BYTES);
1664 
1665 	skb->protocol = *((__be16 *)(skb->data));
1666 
1667 	if (netdev->features & NETIF_F_RXCSUM) {
1668 		skb->ip_summed = CHECKSUM_COMPLETE;
1669 		skb->csum = csum_unfold((__force __sum16)cqe->check_sum);
1670 		stats->csum_complete++;
1671 	} else {
1672 		skb->ip_summed = CHECKSUM_NONE;
1673 		stats->csum_none++;
1674 	}
1675 
1676 	if (unlikely(mlx5e_rx_hw_stamp(tstamp)))
1677 		skb_hwtstamps(skb)->hwtstamp =
1678 				mlx5_timecounter_cyc2time(rq->clock, get_cqe_ts(cqe));
1679 
1680 	skb_record_rx_queue(skb, rq->ix);
1681 
1682 	if (likely(netdev->features & NETIF_F_RXHASH))
1683 		mlx5e_skb_set_hash(cqe, skb);
1684 
1685 	/* 20 bytes of ipoib header and 4 for encap existing */
1686 	pseudo_header = skb_push(skb, MLX5_IPOIB_PSEUDO_LEN);
1687 	memset(pseudo_header, 0, MLX5_IPOIB_PSEUDO_LEN);
1688 	skb_reset_mac_header(skb);
1689 	skb_pull(skb, MLX5_IPOIB_HARD_LEN);
1690 
1691 	skb->dev = netdev;
1692 
1693 	stats->packets++;
1694 	stats->bytes += cqe_bcnt;
1695 }
1696 
mlx5i_handle_rx_cqe(struct mlx5e_rq * rq,struct mlx5_cqe64 * cqe)1697 static void mlx5i_handle_rx_cqe(struct mlx5e_rq *rq, struct mlx5_cqe64 *cqe)
1698 {
1699 	struct mlx5_wq_cyc *wq = &rq->wqe.wq;
1700 	struct mlx5e_wqe_frag_info *wi;
1701 	struct sk_buff *skb;
1702 	u32 cqe_bcnt;
1703 	u16 ci;
1704 
1705 	ci       = mlx5_wq_cyc_ctr2ix(wq, be16_to_cpu(cqe->wqe_counter));
1706 	wi       = get_frag(rq, ci);
1707 	cqe_bcnt = be32_to_cpu(cqe->byte_cnt);
1708 
1709 	if (unlikely(MLX5E_RX_ERR_CQE(cqe))) {
1710 		rq->stats->wqe_err++;
1711 		goto wq_free_wqe;
1712 	}
1713 
1714 	skb = INDIRECT_CALL_2(rq->wqe.skb_from_cqe,
1715 			      mlx5e_skb_from_cqe_linear,
1716 			      mlx5e_skb_from_cqe_nonlinear,
1717 			      rq, cqe, wi, cqe_bcnt);
1718 	if (!skb)
1719 		goto wq_free_wqe;
1720 
1721 	mlx5i_complete_rx_cqe(rq, cqe, cqe_bcnt, skb);
1722 	if (unlikely(!skb->dev)) {
1723 		dev_kfree_skb_any(skb);
1724 		goto wq_free_wqe;
1725 	}
1726 	napi_gro_receive(rq->cq.napi, skb);
1727 
1728 wq_free_wqe:
1729 	mlx5e_free_rx_wqe(rq, wi, true);
1730 	mlx5_wq_cyc_pop(wq);
1731 }
1732 
1733 const struct mlx5e_rx_handlers mlx5i_rx_handlers = {
1734 	.handle_rx_cqe       = mlx5i_handle_rx_cqe,
1735 	.handle_rx_cqe_mpwqe = NULL, /* Not supported */
1736 };
1737 #endif /* CONFIG_MLX5_CORE_IPOIB */
1738 
1739 #ifdef CONFIG_MLX5_EN_IPSEC
1740 
mlx5e_ipsec_handle_rx_cqe(struct mlx5e_rq * rq,struct mlx5_cqe64 * cqe)1741 static void mlx5e_ipsec_handle_rx_cqe(struct mlx5e_rq *rq, struct mlx5_cqe64 *cqe)
1742 {
1743 	struct mlx5_wq_cyc *wq = &rq->wqe.wq;
1744 	struct mlx5e_wqe_frag_info *wi;
1745 	struct sk_buff *skb;
1746 	u32 cqe_bcnt;
1747 	u16 ci;
1748 
1749 	ci       = mlx5_wq_cyc_ctr2ix(wq, be16_to_cpu(cqe->wqe_counter));
1750 	wi       = get_frag(rq, ci);
1751 	cqe_bcnt = be32_to_cpu(cqe->byte_cnt);
1752 
1753 	if (unlikely(MLX5E_RX_ERR_CQE(cqe))) {
1754 		rq->stats->wqe_err++;
1755 		goto wq_free_wqe;
1756 	}
1757 
1758 	skb = INDIRECT_CALL_2(rq->wqe.skb_from_cqe,
1759 			      mlx5e_skb_from_cqe_linear,
1760 			      mlx5e_skb_from_cqe_nonlinear,
1761 			      rq, cqe, wi, cqe_bcnt);
1762 	if (unlikely(!skb)) /* a DROP, save the page-reuse checks */
1763 		goto wq_free_wqe;
1764 
1765 	skb = mlx5e_ipsec_handle_rx_skb(rq->netdev, skb, &cqe_bcnt);
1766 	if (unlikely(!skb))
1767 		goto wq_free_wqe;
1768 
1769 	mlx5e_complete_rx_cqe(rq, cqe, cqe_bcnt, skb);
1770 	napi_gro_receive(rq->cq.napi, skb);
1771 
1772 wq_free_wqe:
1773 	mlx5e_free_rx_wqe(rq, wi, true);
1774 	mlx5_wq_cyc_pop(wq);
1775 }
1776 
1777 #endif /* CONFIG_MLX5_EN_IPSEC */
1778 
mlx5e_rq_set_handlers(struct mlx5e_rq * rq,struct mlx5e_params * params,bool xsk)1779 int mlx5e_rq_set_handlers(struct mlx5e_rq *rq, struct mlx5e_params *params, bool xsk)
1780 {
1781 	struct mlx5_core_dev *mdev = rq->mdev;
1782 	struct mlx5e_channel *c = rq->channel;
1783 
1784 	switch (rq->wq_type) {
1785 	case MLX5_WQ_TYPE_LINKED_LIST_STRIDING_RQ:
1786 		rq->mpwqe.skb_from_cqe_mpwrq = xsk ?
1787 			mlx5e_xsk_skb_from_cqe_mpwrq_linear :
1788 			mlx5e_rx_mpwqe_is_linear_skb(mdev, params, NULL) ?
1789 				mlx5e_skb_from_cqe_mpwrq_linear :
1790 				mlx5e_skb_from_cqe_mpwrq_nonlinear;
1791 		rq->post_wqes = mlx5e_post_rx_mpwqes;
1792 		rq->dealloc_wqe = mlx5e_dealloc_rx_mpwqe;
1793 
1794 		rq->handle_rx_cqe = c->priv->profile->rx_handlers->handle_rx_cqe_mpwqe;
1795 #ifdef CONFIG_MLX5_EN_IPSEC
1796 		if (MLX5_IPSEC_DEV(mdev)) {
1797 			netdev_err(c->netdev, "MPWQE RQ with IPSec offload not supported\n");
1798 			return -EINVAL;
1799 		}
1800 #endif
1801 		if (!rq->handle_rx_cqe) {
1802 			netdev_err(c->netdev, "RX handler of MPWQE RQ is not set\n");
1803 			return -EINVAL;
1804 		}
1805 		break;
1806 	default: /* MLX5_WQ_TYPE_CYCLIC */
1807 		rq->wqe.skb_from_cqe = xsk ?
1808 			mlx5e_xsk_skb_from_cqe_linear :
1809 			mlx5e_rx_is_linear_skb(params, NULL) ?
1810 				mlx5e_skb_from_cqe_linear :
1811 				mlx5e_skb_from_cqe_nonlinear;
1812 		rq->post_wqes = mlx5e_post_rx_wqes;
1813 		rq->dealloc_wqe = mlx5e_dealloc_rx_wqe;
1814 
1815 #ifdef CONFIG_MLX5_EN_IPSEC
1816 		if ((mlx5_fpga_ipsec_device_caps(mdev) & MLX5_ACCEL_IPSEC_CAP_DEVICE) &&
1817 		    c->priv->ipsec)
1818 			rq->handle_rx_cqe = mlx5e_ipsec_handle_rx_cqe;
1819 		else
1820 #endif
1821 			rq->handle_rx_cqe = c->priv->profile->rx_handlers->handle_rx_cqe;
1822 		if (!rq->handle_rx_cqe) {
1823 			netdev_err(c->netdev, "RX handler of RQ is not set\n");
1824 			return -EINVAL;
1825 		}
1826 	}
1827 
1828 	return 0;
1829 }
1830