]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blob - drivers/net/ethernet/mellanox/mlx5/core/en_rx.c
341258b64ec105142d3b14315546e666f2c80908
[mirror_ubuntu-bionic-kernel.git] / drivers / net / ethernet / mellanox / mlx5 / core / en_rx.c
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/prefetch.h>
34 #include <linux/ip.h>
35 #include <linux/ipv6.h>
36 #include <linux/tcp.h>
37 #include <linux/bpf_trace.h>
38 #include <net/busy_poll.h>
39 #include <net/ip6_checksum.h>
40 #include <net/inet_ecn.h>
41 #include "en.h"
42 #include "en_tc.h"
43 #include "eswitch.h"
44 #include "en_rep.h"
45 #include "ipoib/ipoib.h"
46 #include "en_accel/ipsec_rxtx.h"
47 #include "lib/clock.h"
48
49 static inline bool mlx5e_rx_hw_stamp(struct hwtstamp_config *config)
50 {
51 return config->rx_filter == HWTSTAMP_FILTER_ALL;
52 }
53
54 static inline void mlx5e_read_cqe_slot(struct mlx5e_cq *cq, u32 cqcc,
55 void *data)
56 {
57 u32 ci = cqcc & cq->wq.sz_m1;
58
59 memcpy(data, mlx5_cqwq_get_wqe(&cq->wq, ci), sizeof(struct mlx5_cqe64));
60 }
61
62 static inline void mlx5e_read_title_slot(struct mlx5e_rq *rq,
63 struct mlx5e_cq *cq, u32 cqcc)
64 {
65 mlx5e_read_cqe_slot(cq, cqcc, &cq->title);
66 cq->decmprs_left = be32_to_cpu(cq->title.byte_cnt);
67 cq->decmprs_wqe_counter = be16_to_cpu(cq->title.wqe_counter);
68 rq->stats.cqe_compress_blks++;
69 }
70
71 static inline void mlx5e_read_mini_arr_slot(struct mlx5e_cq *cq, u32 cqcc)
72 {
73 mlx5e_read_cqe_slot(cq, cqcc, cq->mini_arr);
74 cq->mini_arr_idx = 0;
75 }
76
77 static inline void mlx5e_cqes_update_owner(struct mlx5e_cq *cq, u32 cqcc, int n)
78 {
79 u8 op_own = (cqcc >> cq->wq.log_sz) & 1;
80 u32 wq_sz = 1 << cq->wq.log_sz;
81 u32 ci = cqcc & cq->wq.sz_m1;
82 u32 ci_top = min_t(u32, wq_sz, ci + n);
83
84 for (; ci < ci_top; ci++, n--) {
85 struct mlx5_cqe64 *cqe = mlx5_cqwq_get_wqe(&cq->wq, ci);
86
87 cqe->op_own = op_own;
88 }
89
90 if (unlikely(ci == wq_sz)) {
91 op_own = !op_own;
92 for (ci = 0; ci < n; ci++) {
93 struct mlx5_cqe64 *cqe = mlx5_cqwq_get_wqe(&cq->wq, ci);
94
95 cqe->op_own = op_own;
96 }
97 }
98 }
99
100 static inline void mlx5e_decompress_cqe(struct mlx5e_rq *rq,
101 struct mlx5e_cq *cq, u32 cqcc)
102 {
103 cq->title.byte_cnt = cq->mini_arr[cq->mini_arr_idx].byte_cnt;
104 cq->title.check_sum = cq->mini_arr[cq->mini_arr_idx].checksum;
105 cq->title.op_own &= 0xf0;
106 cq->title.op_own |= 0x01 & (cqcc >> cq->wq.log_sz);
107 cq->title.wqe_counter = cpu_to_be16(cq->decmprs_wqe_counter);
108
109 if (rq->wq_type == MLX5_WQ_TYPE_LINKED_LIST_STRIDING_RQ)
110 cq->decmprs_wqe_counter +=
111 mpwrq_get_cqe_consumed_strides(&cq->title);
112 else
113 cq->decmprs_wqe_counter =
114 (cq->decmprs_wqe_counter + 1) & rq->wq.sz_m1;
115 }
116
117 static inline void mlx5e_decompress_cqe_no_hash(struct mlx5e_rq *rq,
118 struct mlx5e_cq *cq, u32 cqcc)
119 {
120 mlx5e_decompress_cqe(rq, cq, cqcc);
121 cq->title.rss_hash_type = 0;
122 cq->title.rss_hash_result = 0;
123 }
124
125 static inline u32 mlx5e_decompress_cqes_cont(struct mlx5e_rq *rq,
126 struct mlx5e_cq *cq,
127 int update_owner_only,
128 int budget_rem)
129 {
130 u32 cqcc = cq->wq.cc + update_owner_only;
131 u32 cqe_count;
132 u32 i;
133
134 cqe_count = min_t(u32, cq->decmprs_left, budget_rem);
135
136 for (i = update_owner_only; i < cqe_count;
137 i++, cq->mini_arr_idx++, cqcc++) {
138 if (cq->mini_arr_idx == MLX5_MINI_CQE_ARRAY_SIZE)
139 mlx5e_read_mini_arr_slot(cq, cqcc);
140
141 mlx5e_decompress_cqe_no_hash(rq, cq, cqcc);
142 rq->handle_rx_cqe(rq, &cq->title);
143 }
144 mlx5e_cqes_update_owner(cq, cq->wq.cc, cqcc - cq->wq.cc);
145 cq->wq.cc = cqcc;
146 cq->decmprs_left -= cqe_count;
147 rq->stats.cqe_compress_pkts += cqe_count;
148
149 return cqe_count;
150 }
151
152 static inline u32 mlx5e_decompress_cqes_start(struct mlx5e_rq *rq,
153 struct mlx5e_cq *cq,
154 int budget_rem)
155 {
156 mlx5e_read_title_slot(rq, cq, cq->wq.cc);
157 mlx5e_read_mini_arr_slot(cq, cq->wq.cc + 1);
158 mlx5e_decompress_cqe(rq, cq, cq->wq.cc);
159 rq->handle_rx_cqe(rq, &cq->title);
160 cq->mini_arr_idx++;
161
162 return mlx5e_decompress_cqes_cont(rq, cq, 1, budget_rem) - 1;
163 }
164
165 #define RQ_PAGE_SIZE(rq) ((1 << rq->buff.page_order) << PAGE_SHIFT)
166
167 static inline bool mlx5e_page_is_reserved(struct page *page)
168 {
169 return page_is_pfmemalloc(page) || page_to_nid(page) != numa_mem_id();
170 }
171
172 static inline bool mlx5e_rx_cache_put(struct mlx5e_rq *rq,
173 struct mlx5e_dma_info *dma_info)
174 {
175 struct mlx5e_page_cache *cache = &rq->page_cache;
176 u32 tail_next = (cache->tail + 1) & (MLX5E_CACHE_SIZE - 1);
177
178 if (tail_next == cache->head) {
179 rq->stats.cache_full++;
180 return false;
181 }
182
183 if (unlikely(mlx5e_page_is_reserved(dma_info->page))) {
184 rq->stats.cache_waive++;
185 return false;
186 }
187
188 cache->page_cache[cache->tail] = *dma_info;
189 cache->tail = tail_next;
190 return true;
191 }
192
193 static inline bool mlx5e_rx_cache_get(struct mlx5e_rq *rq,
194 struct mlx5e_dma_info *dma_info)
195 {
196 struct mlx5e_page_cache *cache = &rq->page_cache;
197
198 if (unlikely(cache->head == cache->tail)) {
199 rq->stats.cache_empty++;
200 return false;
201 }
202
203 if (page_ref_count(cache->page_cache[cache->head].page) != 1) {
204 rq->stats.cache_busy++;
205 return false;
206 }
207
208 *dma_info = cache->page_cache[cache->head];
209 cache->head = (cache->head + 1) & (MLX5E_CACHE_SIZE - 1);
210 rq->stats.cache_reuse++;
211
212 dma_sync_single_for_device(rq->pdev, dma_info->addr,
213 RQ_PAGE_SIZE(rq),
214 DMA_FROM_DEVICE);
215 return true;
216 }
217
218 static inline int mlx5e_page_alloc_mapped(struct mlx5e_rq *rq,
219 struct mlx5e_dma_info *dma_info)
220 {
221 if (mlx5e_rx_cache_get(rq, dma_info))
222 return 0;
223
224 dma_info->page = dev_alloc_pages(rq->buff.page_order);
225 if (unlikely(!dma_info->page))
226 return -ENOMEM;
227
228 dma_info->addr = dma_map_page(rq->pdev, dma_info->page, 0,
229 RQ_PAGE_SIZE(rq), rq->buff.map_dir);
230 if (unlikely(dma_mapping_error(rq->pdev, dma_info->addr))) {
231 put_page(dma_info->page);
232 dma_info->page = NULL;
233 return -ENOMEM;
234 }
235
236 return 0;
237 }
238
239 void mlx5e_page_release(struct mlx5e_rq *rq, struct mlx5e_dma_info *dma_info,
240 bool recycle)
241 {
242 if (likely(recycle) && mlx5e_rx_cache_put(rq, dma_info))
243 return;
244
245 dma_unmap_page(rq->pdev, dma_info->addr, RQ_PAGE_SIZE(rq),
246 rq->buff.map_dir);
247 put_page(dma_info->page);
248 }
249
250 static inline bool mlx5e_page_reuse(struct mlx5e_rq *rq,
251 struct mlx5e_wqe_frag_info *wi)
252 {
253 return rq->wqe.page_reuse && wi->di.page &&
254 (wi->offset + rq->wqe.frag_sz <= RQ_PAGE_SIZE(rq)) &&
255 !mlx5e_page_is_reserved(wi->di.page);
256 }
257
258 static int mlx5e_alloc_rx_wqe(struct mlx5e_rq *rq, struct mlx5e_rx_wqe *wqe, u16 ix)
259 {
260 struct mlx5e_wqe_frag_info *wi = &rq->wqe.frag_info[ix];
261
262 /* check if page exists, hence can be reused */
263 if (!wi->di.page) {
264 if (unlikely(mlx5e_page_alloc_mapped(rq, &wi->di)))
265 return -ENOMEM;
266 wi->offset = 0;
267 }
268
269 wqe->data.addr = cpu_to_be64(wi->di.addr + wi->offset + rq->buff.headroom);
270 return 0;
271 }
272
273 static inline void mlx5e_free_rx_wqe(struct mlx5e_rq *rq,
274 struct mlx5e_wqe_frag_info *wi)
275 {
276 mlx5e_page_release(rq, &wi->di, true);
277 wi->di.page = NULL;
278 }
279
280 static inline void mlx5e_free_rx_wqe_reuse(struct mlx5e_rq *rq,
281 struct mlx5e_wqe_frag_info *wi)
282 {
283 if (mlx5e_page_reuse(rq, wi)) {
284 rq->stats.page_reuse++;
285 return;
286 }
287
288 mlx5e_free_rx_wqe(rq, wi);
289 }
290
291 void mlx5e_dealloc_rx_wqe(struct mlx5e_rq *rq, u16 ix)
292 {
293 struct mlx5e_wqe_frag_info *wi = &rq->wqe.frag_info[ix];
294
295 if (wi->di.page)
296 mlx5e_free_rx_wqe(rq, wi);
297 }
298
299 static inline int mlx5e_mpwqe_strides_per_page(struct mlx5e_rq *rq)
300 {
301 return rq->mpwqe.num_strides >> MLX5_MPWRQ_WQE_PAGE_ORDER;
302 }
303
304 static inline void mlx5e_add_skb_frag_mpwqe(struct mlx5e_rq *rq,
305 struct sk_buff *skb,
306 struct mlx5e_mpw_info *wi,
307 u32 page_idx, u32 frag_offset,
308 u32 len)
309 {
310 unsigned int truesize = ALIGN(len, BIT(rq->mpwqe.log_stride_sz));
311
312 dma_sync_single_for_cpu(rq->pdev,
313 wi->umr.dma_info[page_idx].addr + frag_offset,
314 len, DMA_FROM_DEVICE);
315 wi->skbs_frags[page_idx]++;
316 skb_add_rx_frag(skb, skb_shinfo(skb)->nr_frags,
317 wi->umr.dma_info[page_idx].page, frag_offset,
318 len, truesize);
319 }
320
321 static inline void
322 mlx5e_copy_skb_header_mpwqe(struct device *pdev,
323 struct sk_buff *skb,
324 struct mlx5e_mpw_info *wi,
325 u32 page_idx, u32 offset,
326 u32 headlen)
327 {
328 u16 headlen_pg = min_t(u32, headlen, PAGE_SIZE - offset);
329 struct mlx5e_dma_info *dma_info = &wi->umr.dma_info[page_idx];
330 unsigned int len;
331
332 /* Aligning len to sizeof(long) optimizes memcpy performance */
333 len = ALIGN(headlen_pg, sizeof(long));
334 dma_sync_single_for_cpu(pdev, dma_info->addr + offset, len,
335 DMA_FROM_DEVICE);
336 skb_copy_to_linear_data_offset(skb, 0,
337 page_address(dma_info->page) + offset,
338 len);
339 if (unlikely(offset + headlen > PAGE_SIZE)) {
340 dma_info++;
341 headlen_pg = len;
342 len = ALIGN(headlen - headlen_pg, sizeof(long));
343 dma_sync_single_for_cpu(pdev, dma_info->addr, len,
344 DMA_FROM_DEVICE);
345 skb_copy_to_linear_data_offset(skb, headlen_pg,
346 page_address(dma_info->page),
347 len);
348 }
349 }
350
351 static inline void mlx5e_post_umr_wqe(struct mlx5e_rq *rq, u16 ix)
352 {
353 struct mlx5e_mpw_info *wi = &rq->mpwqe.info[ix];
354 struct mlx5e_icosq *sq = &rq->channel->icosq;
355 struct mlx5_wq_cyc *wq = &sq->wq;
356 struct mlx5e_umr_wqe *wqe;
357 u8 num_wqebbs = DIV_ROUND_UP(sizeof(*wqe), MLX5_SEND_WQE_BB);
358 u16 pi;
359
360 /* fill sq edge with nops to avoid wqe wrap around */
361 while ((pi = (sq->pc & wq->sz_m1)) > sq->edge) {
362 sq->db.ico_wqe[pi].opcode = MLX5_OPCODE_NOP;
363 mlx5e_post_nop(wq, sq->sqn, &sq->pc);
364 }
365
366 wqe = mlx5_wq_cyc_get_wqe(wq, pi);
367 memcpy(wqe, &wi->umr.wqe, sizeof(*wqe));
368 wqe->ctrl.opmod_idx_opcode =
369 cpu_to_be32((sq->pc << MLX5_WQE_CTRL_WQE_INDEX_SHIFT) |
370 MLX5_OPCODE_UMR);
371
372 sq->db.ico_wqe[pi].opcode = MLX5_OPCODE_UMR;
373 sq->pc += num_wqebbs;
374 mlx5e_notify_hw(&sq->wq, sq->pc, sq->uar_map, &wqe->ctrl);
375 }
376
377 static int mlx5e_alloc_rx_umr_mpwqe(struct mlx5e_rq *rq,
378 u16 ix)
379 {
380 struct mlx5e_mpw_info *wi = &rq->mpwqe.info[ix];
381 int pg_strides = mlx5e_mpwqe_strides_per_page(rq);
382 struct mlx5e_dma_info *dma_info = &wi->umr.dma_info[0];
383 int err;
384 int i;
385
386 for (i = 0; i < MLX5_MPWRQ_PAGES_PER_WQE; i++, dma_info++) {
387 err = mlx5e_page_alloc_mapped(rq, dma_info);
388 if (unlikely(err))
389 goto err_unmap;
390 wi->umr.mtt[i] = cpu_to_be64(dma_info->addr | MLX5_EN_WR);
391 page_ref_add(dma_info->page, pg_strides);
392 }
393
394 memset(wi->skbs_frags, 0, sizeof(*wi->skbs_frags) * MLX5_MPWRQ_PAGES_PER_WQE);
395 wi->consumed_strides = 0;
396
397 return 0;
398
399 err_unmap:
400 while (--i >= 0) {
401 dma_info--;
402 page_ref_sub(dma_info->page, pg_strides);
403 mlx5e_page_release(rq, dma_info, true);
404 }
405
406 return err;
407 }
408
409 void mlx5e_free_rx_mpwqe(struct mlx5e_rq *rq, struct mlx5e_mpw_info *wi)
410 {
411 int pg_strides = mlx5e_mpwqe_strides_per_page(rq);
412 struct mlx5e_dma_info *dma_info = &wi->umr.dma_info[0];
413 int i;
414
415 for (i = 0; i < MLX5_MPWRQ_PAGES_PER_WQE; i++, dma_info++) {
416 page_ref_sub(dma_info->page, pg_strides - wi->skbs_frags[i]);
417 mlx5e_page_release(rq, dma_info, true);
418 }
419 }
420
421 static void mlx5e_post_rx_mpwqe(struct mlx5e_rq *rq)
422 {
423 struct mlx5_wq_ll *wq = &rq->wq;
424 struct mlx5e_rx_wqe *wqe = mlx5_wq_ll_get_wqe(wq, wq->head);
425
426 rq->mpwqe.umr_in_progress = false;
427
428 mlx5_wq_ll_push(wq, be16_to_cpu(wqe->next.next_wqe_index));
429
430 /* ensure wqes are visible to device before updating doorbell record */
431 dma_wmb();
432
433 mlx5_wq_ll_update_db_record(wq);
434 }
435
436 static int mlx5e_alloc_rx_mpwqe(struct mlx5e_rq *rq, u16 ix)
437 {
438 int err;
439
440 err = mlx5e_alloc_rx_umr_mpwqe(rq, ix);
441 if (unlikely(err)) {
442 rq->stats.buff_alloc_err++;
443 return err;
444 }
445 rq->mpwqe.umr_in_progress = true;
446 mlx5e_post_umr_wqe(rq, ix);
447 return 0;
448 }
449
450 void mlx5e_dealloc_rx_mpwqe(struct mlx5e_rq *rq, u16 ix)
451 {
452 struct mlx5e_mpw_info *wi = &rq->mpwqe.info[ix];
453
454 mlx5e_free_rx_mpwqe(rq, wi);
455 }
456
457 bool mlx5e_post_rx_wqes(struct mlx5e_rq *rq)
458 {
459 struct mlx5_wq_ll *wq = &rq->wq;
460 int err;
461
462 if (unlikely(!MLX5E_TEST_BIT(rq->state, MLX5E_RQ_STATE_ENABLED)))
463 return false;
464
465 if (mlx5_wq_ll_is_full(wq))
466 return false;
467
468 do {
469 struct mlx5e_rx_wqe *wqe = mlx5_wq_ll_get_wqe(wq, wq->head);
470
471 err = mlx5e_alloc_rx_wqe(rq, wqe, wq->head);
472 if (unlikely(err)) {
473 rq->stats.buff_alloc_err++;
474 break;
475 }
476
477 mlx5_wq_ll_push(wq, be16_to_cpu(wqe->next.next_wqe_index));
478 } while (!mlx5_wq_ll_is_full(wq));
479
480 /* ensure wqes are visible to device before updating doorbell record */
481 dma_wmb();
482
483 mlx5_wq_ll_update_db_record(wq);
484
485 return !!err;
486 }
487
488 static inline void mlx5e_poll_ico_single_cqe(struct mlx5e_cq *cq,
489 struct mlx5e_icosq *sq,
490 struct mlx5e_rq *rq,
491 struct mlx5_cqe64 *cqe)
492 {
493 struct mlx5_wq_cyc *wq = &sq->wq;
494 u16 ci = be16_to_cpu(cqe->wqe_counter) & wq->sz_m1;
495 struct mlx5e_sq_wqe_info *icowi = &sq->db.ico_wqe[ci];
496
497 mlx5_cqwq_pop(&cq->wq);
498
499 if (unlikely((cqe->op_own >> 4) != MLX5_CQE_REQ)) {
500 WARN_ONCE(true, "mlx5e: Bad OP in ICOSQ CQE: 0x%x\n",
501 cqe->op_own);
502 return;
503 }
504
505 if (likely(icowi->opcode == MLX5_OPCODE_UMR)) {
506 mlx5e_post_rx_mpwqe(rq);
507 return;
508 }
509
510 if (unlikely(icowi->opcode != MLX5_OPCODE_NOP))
511 WARN_ONCE(true,
512 "mlx5e: Bad OPCODE in ICOSQ WQE info: 0x%x\n",
513 icowi->opcode);
514 }
515
516 static void mlx5e_poll_ico_cq(struct mlx5e_cq *cq, struct mlx5e_rq *rq)
517 {
518 struct mlx5e_icosq *sq = container_of(cq, struct mlx5e_icosq, cq);
519 struct mlx5_cqe64 *cqe;
520
521 if (unlikely(!MLX5E_TEST_BIT(sq->state, MLX5E_SQ_STATE_ENABLED)))
522 return;
523
524 cqe = mlx5_cqwq_get_cqe(&cq->wq);
525 if (likely(!cqe))
526 return;
527
528 /* by design, there's only a single cqe */
529 mlx5e_poll_ico_single_cqe(cq, sq, rq, cqe);
530
531 mlx5_cqwq_update_db_record(&cq->wq);
532 }
533
534 bool mlx5e_post_rx_mpwqes(struct mlx5e_rq *rq)
535 {
536 struct mlx5_wq_ll *wq = &rq->wq;
537
538 if (unlikely(!MLX5E_TEST_BIT(rq->state, MLX5E_RQ_STATE_ENABLED)))
539 return false;
540
541 mlx5e_poll_ico_cq(&rq->channel->icosq.cq, rq);
542
543 if (mlx5_wq_ll_is_full(wq))
544 return false;
545
546 if (!rq->mpwqe.umr_in_progress)
547 mlx5e_alloc_rx_mpwqe(rq, wq->head);
548
549 return true;
550 }
551
552 static void mlx5e_lro_update_tcp_hdr(struct mlx5_cqe64 *cqe, struct tcphdr *tcp)
553 {
554 u8 l4_hdr_type = get_cqe_l4_hdr_type(cqe);
555 u8 tcp_ack = (l4_hdr_type == CQE_L4_HDR_TYPE_TCP_ACK_NO_DATA) ||
556 (l4_hdr_type == CQE_L4_HDR_TYPE_TCP_ACK_AND_DATA);
557
558 tcp->check = 0;
559 tcp->psh = get_cqe_lro_tcppsh(cqe);
560
561 if (tcp_ack) {
562 tcp->ack = 1;
563 tcp->ack_seq = cqe->lro_ack_seq_num;
564 tcp->window = cqe->lro_tcp_win;
565 }
566 }
567
568 static void mlx5e_lro_update_hdr(struct sk_buff *skb, struct mlx5_cqe64 *cqe,
569 u32 cqe_bcnt)
570 {
571 struct ethhdr *eth = (struct ethhdr *)(skb->data);
572 struct tcphdr *tcp;
573 int network_depth = 0;
574 __wsum check;
575 __be16 proto;
576 u16 tot_len;
577 void *ip_p;
578
579 proto = __vlan_get_protocol(skb, eth->h_proto, &network_depth);
580
581 tot_len = cqe_bcnt - network_depth;
582 ip_p = skb->data + network_depth;
583
584 if (proto == htons(ETH_P_IP)) {
585 struct iphdr *ipv4 = ip_p;
586
587 tcp = ip_p + sizeof(struct iphdr);
588 skb_shinfo(skb)->gso_type = SKB_GSO_TCPV4;
589
590 ipv4->ttl = cqe->lro_min_ttl;
591 ipv4->tot_len = cpu_to_be16(tot_len);
592 ipv4->check = 0;
593 ipv4->check = ip_fast_csum((unsigned char *)ipv4,
594 ipv4->ihl);
595
596 mlx5e_lro_update_tcp_hdr(cqe, tcp);
597 check = csum_partial(tcp, tcp->doff * 4,
598 csum_unfold((__force __sum16)cqe->check_sum));
599 /* Almost done, don't forget the pseudo header */
600 tcp->check = csum_tcpudp_magic(ipv4->saddr, ipv4->daddr,
601 tot_len - sizeof(struct iphdr),
602 IPPROTO_TCP, check);
603 } else {
604 u16 payload_len = tot_len - sizeof(struct ipv6hdr);
605 struct ipv6hdr *ipv6 = ip_p;
606
607 tcp = ip_p + sizeof(struct ipv6hdr);
608 skb_shinfo(skb)->gso_type = SKB_GSO_TCPV6;
609
610 ipv6->hop_limit = cqe->lro_min_ttl;
611 ipv6->payload_len = cpu_to_be16(payload_len);
612
613 mlx5e_lro_update_tcp_hdr(cqe, tcp);
614 check = csum_partial(tcp, tcp->doff * 4,
615 csum_unfold((__force __sum16)cqe->check_sum));
616 /* Almost done, don't forget the pseudo header */
617 tcp->check = csum_ipv6_magic(&ipv6->saddr, &ipv6->daddr, payload_len,
618 IPPROTO_TCP, check);
619 }
620 }
621
622 static inline void mlx5e_skb_set_hash(struct mlx5_cqe64 *cqe,
623 struct sk_buff *skb)
624 {
625 u8 cht = cqe->rss_hash_type;
626 int ht = (cht & CQE_RSS_HTYPE_L4) ? PKT_HASH_TYPE_L4 :
627 (cht & CQE_RSS_HTYPE_IP) ? PKT_HASH_TYPE_L3 :
628 PKT_HASH_TYPE_NONE;
629 skb_set_hash(skb, be32_to_cpu(cqe->rss_hash_result), ht);
630 }
631
632 static inline bool is_last_ethertype_ip(struct sk_buff *skb, int *network_depth,
633 __be16 *proto)
634 {
635 *proto = ((struct ethhdr *)skb->data)->h_proto;
636 *proto = __vlan_get_protocol(skb, *proto, network_depth);
637 return (*proto == htons(ETH_P_IP) || *proto == htons(ETH_P_IPV6));
638 }
639
640 static inline void mlx5e_enable_ecn(struct mlx5e_rq *rq, struct sk_buff *skb)
641 {
642 int network_depth = 0;
643 __be16 proto;
644 void *ip;
645 int rc;
646
647 if (unlikely(!is_last_ethertype_ip(skb, &network_depth, &proto)))
648 return;
649
650 ip = skb->data + network_depth;
651 rc = ((proto == htons(ETH_P_IP)) ? IP_ECN_set_ce((struct iphdr *)ip) :
652 IP6_ECN_set_ce(skb, (struct ipv6hdr *)ip));
653
654 rq->stats.ecn_mark += !!rc;
655 }
656
657 static u32 mlx5e_get_fcs(const struct sk_buff *skb)
658 {
659 const void *fcs_bytes;
660 u32 _fcs_bytes;
661
662 fcs_bytes = skb_header_pointer(skb, skb->len - ETH_FCS_LEN,
663 ETH_FCS_LEN, &_fcs_bytes);
664
665 return __get_unaligned_cpu32(fcs_bytes);
666 }
667
668 static u8 get_ip_proto(struct sk_buff *skb, int network_depth, __be16 proto)
669 {
670 void *ip_p = skb->data + network_depth;
671
672 return (proto == htons(ETH_P_IP)) ? ((struct iphdr *)ip_p)->protocol :
673 ((struct ipv6hdr *)ip_p)->nexthdr;
674 }
675
676 #define short_frame(size) ((size) <= ETH_ZLEN + ETH_FCS_LEN)
677
678 static inline void mlx5e_handle_csum(struct net_device *netdev,
679 struct mlx5_cqe64 *cqe,
680 struct mlx5e_rq *rq,
681 struct sk_buff *skb,
682 bool lro)
683 {
684 int network_depth = 0;
685 __be16 proto;
686
687 if (unlikely(!(netdev->features & NETIF_F_RXCSUM)))
688 goto csum_none;
689
690 if (lro) {
691 skb->ip_summed = CHECKSUM_UNNECESSARY;
692 rq->stats.csum_unnecessary++;
693 return;
694 }
695
696 /* True when explicitly set via priv flag, or XDP prog is loaded */
697 if (test_bit(MLX5E_RQ_STATE_NO_CSUM_COMPLETE, &rq->state))
698 goto csum_unnecessary;
699
700 /* CQE csum doesn't cover padding octets in short ethernet
701 * frames. And the pad field is appended prior to calculating
702 * and appending the FCS field.
703 *
704 * Detecting these padded frames requires to verify and parse
705 * IP headers, so we simply force all those small frames to be
706 * CHECKSUM_UNNECESSARY even if they are not padded.
707 */
708 if (short_frame(skb->len))
709 goto csum_unnecessary;
710
711 if (likely(is_last_ethertype_ip(skb, &network_depth, &proto))) {
712 if (unlikely(get_ip_proto(skb, network_depth, proto) == IPPROTO_SCTP))
713 goto csum_unnecessary;
714
715 skb->ip_summed = CHECKSUM_COMPLETE;
716 skb->csum = csum_unfold((__force __sum16)cqe->check_sum);
717 if (network_depth > ETH_HLEN)
718 /* CQE csum is calculated from the IP header and does
719 * not cover VLAN headers (if present). This will add
720 * the checksum manually.
721 */
722 skb->csum = csum_partial(skb->data + ETH_HLEN,
723 network_depth - ETH_HLEN,
724 skb->csum);
725 if (unlikely(netdev->features & NETIF_F_RXFCS))
726 skb->csum = csum_block_add(skb->csum,
727 (__force __wsum)mlx5e_get_fcs(skb),
728 skb->len - ETH_FCS_LEN);
729 rq->stats.csum_complete++;
730 return;
731 }
732
733 csum_unnecessary:
734 if (likely((cqe->hds_ip_ext & CQE_L3_OK) &&
735 (cqe->hds_ip_ext & CQE_L4_OK))) {
736 skb->ip_summed = CHECKSUM_UNNECESSARY;
737 if (cqe_is_tunneled(cqe)) {
738 skb->csum_level = 1;
739 skb->encapsulation = 1;
740 rq->stats.csum_unnecessary_inner++;
741 return;
742 }
743 rq->stats.csum_unnecessary++;
744 return;
745 }
746 csum_none:
747 skb->ip_summed = CHECKSUM_NONE;
748 rq->stats.csum_none++;
749 }
750
751 #define MLX5E_CE_BIT_MASK 0x80
752
753 static inline void mlx5e_build_rx_skb(struct mlx5_cqe64 *cqe,
754 u32 cqe_bcnt,
755 struct mlx5e_rq *rq,
756 struct sk_buff *skb)
757 {
758 struct net_device *netdev = rq->netdev;
759 int lro_num_seg;
760
761 skb->mac_len = ETH_HLEN;
762 lro_num_seg = be32_to_cpu(cqe->srqn) >> 24;
763 if (lro_num_seg > 1) {
764 mlx5e_lro_update_hdr(skb, cqe, cqe_bcnt);
765 skb_shinfo(skb)->gso_size = DIV_ROUND_UP(cqe_bcnt, lro_num_seg);
766 /* Subtract one since we already counted this as one
767 * "regular" packet in mlx5e_complete_rx_cqe()
768 */
769 rq->stats.packets += lro_num_seg - 1;
770 rq->stats.lro_packets++;
771 rq->stats.lro_bytes += cqe_bcnt;
772 }
773
774 if (unlikely(mlx5e_rx_hw_stamp(rq->tstamp)))
775 skb_hwtstamps(skb)->hwtstamp =
776 mlx5_timecounter_cyc2time(rq->clock, get_cqe_ts(cqe));
777
778 skb_record_rx_queue(skb, rq->ix);
779
780 if (likely(netdev->features & NETIF_F_RXHASH))
781 mlx5e_skb_set_hash(cqe, skb);
782
783 if (cqe_has_vlan(cqe)) {
784 __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q),
785 be16_to_cpu(cqe->vlan_info));
786 rq->stats.removed_vlan_packets++;
787 }
788
789 skb->mark = be32_to_cpu(cqe->sop_drop_qpn) & MLX5E_TC_FLOW_ID_MASK;
790
791 mlx5e_handle_csum(netdev, cqe, rq, skb, !!lro_num_seg);
792 /* checking CE bit in cqe - MSB in ml_path field */
793 if (unlikely(cqe->ml_path & MLX5E_CE_BIT_MASK))
794 mlx5e_enable_ecn(rq, skb);
795
796 skb->protocol = eth_type_trans(skb, netdev);
797 }
798
799 static inline void mlx5e_complete_rx_cqe(struct mlx5e_rq *rq,
800 struct mlx5_cqe64 *cqe,
801 u32 cqe_bcnt,
802 struct sk_buff *skb)
803 {
804 rq->stats.packets++;
805 rq->stats.bytes += cqe_bcnt;
806 mlx5e_build_rx_skb(cqe, cqe_bcnt, rq, skb);
807 }
808
809 static inline void mlx5e_xmit_xdp_doorbell(struct mlx5e_xdpsq *sq)
810 {
811 struct mlx5_wq_cyc *wq = &sq->wq;
812 struct mlx5e_tx_wqe *wqe;
813 u16 pi = (sq->pc - 1) & wq->sz_m1; /* last pi */
814
815 wqe = mlx5_wq_cyc_get_wqe(wq, pi);
816
817 mlx5e_notify_hw(wq, sq->pc, sq->uar_map, &wqe->ctrl);
818 }
819
820 static inline bool mlx5e_xmit_xdp_frame(struct mlx5e_rq *rq,
821 struct mlx5e_dma_info *di,
822 const struct xdp_buff *xdp)
823 {
824 struct mlx5e_xdpsq *sq = &rq->xdpsq;
825 struct mlx5_wq_cyc *wq = &sq->wq;
826 u16 pi = sq->pc & wq->sz_m1;
827 struct mlx5e_tx_wqe *wqe = mlx5_wq_cyc_get_wqe(wq, pi);
828
829 struct mlx5_wqe_ctrl_seg *cseg = &wqe->ctrl;
830 struct mlx5_wqe_eth_seg *eseg = &wqe->eth;
831 struct mlx5_wqe_data_seg *dseg;
832
833 ptrdiff_t data_offset = xdp->data - xdp->data_hard_start;
834 dma_addr_t dma_addr = di->addr + data_offset;
835 unsigned int dma_len = xdp->data_end - xdp->data;
836
837 prefetchw(wqe);
838
839 if (unlikely(dma_len < MLX5E_XDP_MIN_INLINE ||
840 MLX5E_SW2HW_MTU(rq->channel->priv, rq->netdev->mtu) < dma_len)) {
841 rq->stats.xdp_drop++;
842 return false;
843 }
844
845 if (unlikely(!mlx5e_wqc_has_room_for(wq, sq->cc, sq->pc, 1))) {
846 if (sq->db.doorbell) {
847 /* SQ is full, ring doorbell */
848 mlx5e_xmit_xdp_doorbell(sq);
849 sq->db.doorbell = false;
850 }
851 rq->stats.xdp_tx_full++;
852 return false;
853 }
854
855 dma_sync_single_for_device(sq->pdev, dma_addr, dma_len, PCI_DMA_TODEVICE);
856
857 cseg->fm_ce_se = 0;
858
859 dseg = (struct mlx5_wqe_data_seg *)eseg + 1;
860
861 /* copy the inline part if required */
862 if (sq->min_inline_mode != MLX5_INLINE_MODE_NONE) {
863 memcpy(eseg->inline_hdr.start, xdp->data, MLX5E_XDP_MIN_INLINE);
864 eseg->inline_hdr.sz = cpu_to_be16(MLX5E_XDP_MIN_INLINE);
865 dma_len -= MLX5E_XDP_MIN_INLINE;
866 dma_addr += MLX5E_XDP_MIN_INLINE;
867 dseg++;
868 }
869
870 /* write the dma part */
871 dseg->addr = cpu_to_be64(dma_addr);
872 dseg->byte_count = cpu_to_be32(dma_len);
873
874 cseg->opmod_idx_opcode = cpu_to_be32((sq->pc << 8) | MLX5_OPCODE_SEND);
875
876 /* move page to reference to sq responsibility,
877 * and mark so it's not put back in page-cache.
878 */
879 rq->wqe.xdp_xmit = true;
880 sq->db.di[pi] = *di;
881 sq->pc++;
882
883 sq->db.doorbell = true;
884
885 rq->stats.xdp_tx++;
886 return true;
887 }
888
889 /* returns true if packet was consumed by xdp */
890 static inline int mlx5e_xdp_handle(struct mlx5e_rq *rq,
891 struct mlx5e_dma_info *di,
892 void *va, u16 *rx_headroom, u32 *len)
893 {
894 const struct bpf_prog *prog = READ_ONCE(rq->xdp_prog);
895 struct xdp_buff xdp;
896 u32 act;
897
898 if (!prog)
899 return false;
900
901 xdp.data = va + *rx_headroom;
902 xdp_set_data_meta_invalid(&xdp);
903 xdp.data_end = xdp.data + *len;
904 xdp.data_hard_start = va;
905
906 act = bpf_prog_run_xdp(prog, &xdp);
907 switch (act) {
908 case XDP_PASS:
909 *rx_headroom = xdp.data - xdp.data_hard_start;
910 *len = xdp.data_end - xdp.data;
911 return false;
912 case XDP_TX:
913 if (unlikely(!mlx5e_xmit_xdp_frame(rq, di, &xdp)))
914 trace_xdp_exception(rq->netdev, prog, act);
915 return true;
916 default:
917 bpf_warn_invalid_xdp_action(act);
918 case XDP_ABORTED:
919 trace_xdp_exception(rq->netdev, prog, act);
920 case XDP_DROP:
921 rq->stats.xdp_drop++;
922 return true;
923 }
924 }
925
926 static inline
927 struct sk_buff *skb_from_cqe(struct mlx5e_rq *rq, struct mlx5_cqe64 *cqe,
928 struct mlx5e_wqe_frag_info *wi, u32 cqe_bcnt)
929 {
930 struct mlx5e_dma_info *di = &wi->di;
931 u16 rx_headroom = rq->buff.headroom;
932 struct sk_buff *skb;
933 void *va, *data;
934 bool consumed;
935 u32 frag_size;
936
937 va = page_address(di->page) + wi->offset;
938 data = va + rx_headroom;
939 frag_size = MLX5_SKB_FRAG_SZ(rx_headroom + cqe_bcnt);
940
941 dma_sync_single_range_for_cpu(rq->pdev,
942 di->addr + wi->offset,
943 0, frag_size,
944 DMA_FROM_DEVICE);
945 prefetch(data);
946 wi->offset += frag_size;
947
948 if (unlikely((cqe->op_own >> 4) != MLX5_CQE_RESP_SEND)) {
949 rq->stats.wqe_err++;
950 return NULL;
951 }
952
953 rcu_read_lock();
954 consumed = mlx5e_xdp_handle(rq, di, va, &rx_headroom, &cqe_bcnt);
955 rcu_read_unlock();
956 if (consumed)
957 return NULL; /* page/packet was consumed by XDP */
958
959 skb = build_skb(va, frag_size);
960 if (unlikely(!skb)) {
961 rq->stats.buff_alloc_err++;
962 return NULL;
963 }
964
965 /* queue up for recycling/reuse */
966 page_ref_inc(di->page);
967
968 skb_reserve(skb, rx_headroom);
969 skb_put(skb, cqe_bcnt);
970
971 return skb;
972 }
973
974 void mlx5e_handle_rx_cqe(struct mlx5e_rq *rq, struct mlx5_cqe64 *cqe)
975 {
976 struct mlx5e_wqe_frag_info *wi;
977 struct mlx5e_rx_wqe *wqe;
978 __be16 wqe_counter_be;
979 struct sk_buff *skb;
980 u16 wqe_counter;
981 u32 cqe_bcnt;
982
983 wqe_counter_be = cqe->wqe_counter;
984 wqe_counter = be16_to_cpu(wqe_counter_be);
985 wqe = mlx5_wq_ll_get_wqe(&rq->wq, wqe_counter);
986 wi = &rq->wqe.frag_info[wqe_counter];
987 cqe_bcnt = be32_to_cpu(cqe->byte_cnt);
988
989 skb = skb_from_cqe(rq, cqe, wi, cqe_bcnt);
990 if (!skb) {
991 /* probably for XDP */
992 if (rq->wqe.xdp_xmit) {
993 wi->di.page = NULL;
994 rq->wqe.xdp_xmit = false;
995 /* do not return page to cache, it will be returned on XDP_TX completion */
996 goto wq_ll_pop;
997 }
998 /* probably an XDP_DROP, save the page-reuse checks */
999 mlx5e_free_rx_wqe(rq, wi);
1000 goto wq_ll_pop;
1001 }
1002
1003 mlx5e_complete_rx_cqe(rq, cqe, cqe_bcnt, skb);
1004 napi_gro_receive(rq->cq.napi, skb);
1005
1006 mlx5e_free_rx_wqe_reuse(rq, wi);
1007 wq_ll_pop:
1008 mlx5_wq_ll_pop(&rq->wq, wqe_counter_be,
1009 &wqe->next.next_wqe_index);
1010 }
1011
1012 #ifdef CONFIG_MLX5_ESWITCH
1013 void mlx5e_handle_rx_cqe_rep(struct mlx5e_rq *rq, struct mlx5_cqe64 *cqe)
1014 {
1015 struct net_device *netdev = rq->netdev;
1016 struct mlx5e_priv *priv = netdev_priv(netdev);
1017 struct mlx5e_rep_priv *rpriv = priv->ppriv;
1018 struct mlx5_eswitch_rep *rep = rpriv->rep;
1019 struct mlx5e_wqe_frag_info *wi;
1020 struct mlx5e_rx_wqe *wqe;
1021 struct sk_buff *skb;
1022 __be16 wqe_counter_be;
1023 u16 wqe_counter;
1024 u32 cqe_bcnt;
1025
1026 wqe_counter_be = cqe->wqe_counter;
1027 wqe_counter = be16_to_cpu(wqe_counter_be);
1028 wqe = mlx5_wq_ll_get_wqe(&rq->wq, wqe_counter);
1029 wi = &rq->wqe.frag_info[wqe_counter];
1030 cqe_bcnt = be32_to_cpu(cqe->byte_cnt);
1031
1032 skb = skb_from_cqe(rq, cqe, wi, cqe_bcnt);
1033 if (!skb) {
1034 if (rq->wqe.xdp_xmit) {
1035 wi->di.page = NULL;
1036 rq->wqe.xdp_xmit = false;
1037 /* do not return page to cache, it will be returned on XDP_TX completion */
1038 goto wq_ll_pop;
1039 }
1040 /* probably an XDP_DROP, save the page-reuse checks */
1041 mlx5e_free_rx_wqe(rq, wi);
1042 goto wq_ll_pop;
1043 }
1044
1045 mlx5e_complete_rx_cqe(rq, cqe, cqe_bcnt, skb);
1046
1047 if (rep->vlan && skb_vlan_tag_present(skb))
1048 skb_vlan_pop(skb);
1049
1050 napi_gro_receive(rq->cq.napi, skb);
1051
1052 mlx5e_free_rx_wqe_reuse(rq, wi);
1053 wq_ll_pop:
1054 mlx5_wq_ll_pop(&rq->wq, wqe_counter_be,
1055 &wqe->next.next_wqe_index);
1056 }
1057 #endif
1058
1059 static inline void mlx5e_mpwqe_fill_rx_skb(struct mlx5e_rq *rq,
1060 struct mlx5_cqe64 *cqe,
1061 struct mlx5e_mpw_info *wi,
1062 u32 cqe_bcnt,
1063 struct sk_buff *skb)
1064 {
1065 u16 stride_ix = mpwrq_get_cqe_stride_index(cqe);
1066 u32 wqe_offset = stride_ix << rq->mpwqe.log_stride_sz;
1067 u32 head_offset = wqe_offset & (PAGE_SIZE - 1);
1068 u32 page_idx = wqe_offset >> PAGE_SHIFT;
1069 u32 head_page_idx = page_idx;
1070 u16 headlen = min_t(u16, MLX5_MPWRQ_SMALL_PACKET_THRESHOLD, cqe_bcnt);
1071 u32 frag_offset = head_offset + headlen;
1072 u16 byte_cnt = cqe_bcnt - headlen;
1073
1074 if (unlikely(frag_offset >= PAGE_SIZE)) {
1075 page_idx++;
1076 frag_offset -= PAGE_SIZE;
1077 }
1078
1079 while (byte_cnt) {
1080 u32 pg_consumed_bytes =
1081 min_t(u32, PAGE_SIZE - frag_offset, byte_cnt);
1082
1083 mlx5e_add_skb_frag_mpwqe(rq, skb, wi, page_idx, frag_offset,
1084 pg_consumed_bytes);
1085 byte_cnt -= pg_consumed_bytes;
1086 frag_offset = 0;
1087 page_idx++;
1088 }
1089 /* copy header */
1090 mlx5e_copy_skb_header_mpwqe(rq->pdev, skb, wi, head_page_idx,
1091 head_offset, headlen);
1092 /* skb linear part was allocated with headlen and aligned to long */
1093 skb->tail += headlen;
1094 skb->len += headlen;
1095 }
1096
1097 void mlx5e_handle_rx_cqe_mpwrq(struct mlx5e_rq *rq, struct mlx5_cqe64 *cqe)
1098 {
1099 u16 cstrides = mpwrq_get_cqe_consumed_strides(cqe);
1100 u16 wqe_id = be16_to_cpu(cqe->wqe_id);
1101 struct mlx5e_mpw_info *wi = &rq->mpwqe.info[wqe_id];
1102 struct mlx5e_rx_wqe *wqe = mlx5_wq_ll_get_wqe(&rq->wq, wqe_id);
1103 struct sk_buff *skb;
1104 u16 cqe_bcnt;
1105
1106 wi->consumed_strides += cstrides;
1107
1108 if (unlikely((cqe->op_own >> 4) != MLX5_CQE_RESP_SEND)) {
1109 rq->stats.wqe_err++;
1110 goto mpwrq_cqe_out;
1111 }
1112
1113 if (unlikely(mpwrq_is_filler_cqe(cqe))) {
1114 rq->stats.mpwqe_filler++;
1115 goto mpwrq_cqe_out;
1116 }
1117
1118 skb = napi_alloc_skb(rq->cq.napi,
1119 ALIGN(MLX5_MPWRQ_SMALL_PACKET_THRESHOLD,
1120 sizeof(long)));
1121 if (unlikely(!skb)) {
1122 rq->stats.buff_alloc_err++;
1123 goto mpwrq_cqe_out;
1124 }
1125
1126 prefetchw(skb->data);
1127 cqe_bcnt = mpwrq_get_cqe_byte_cnt(cqe);
1128
1129 mlx5e_mpwqe_fill_rx_skb(rq, cqe, wi, cqe_bcnt, skb);
1130 mlx5e_complete_rx_cqe(rq, cqe, cqe_bcnt, skb);
1131 napi_gro_receive(rq->cq.napi, skb);
1132
1133 mpwrq_cqe_out:
1134 if (likely(wi->consumed_strides < rq->mpwqe.num_strides))
1135 return;
1136
1137 mlx5e_free_rx_mpwqe(rq, wi);
1138 mlx5_wq_ll_pop(&rq->wq, cqe->wqe_id, &wqe->next.next_wqe_index);
1139 }
1140
1141 int mlx5e_poll_rx_cq(struct mlx5e_cq *cq, int budget)
1142 {
1143 struct mlx5e_rq *rq = container_of(cq, struct mlx5e_rq, cq);
1144 struct mlx5e_xdpsq *xdpsq = &rq->xdpsq;
1145 struct mlx5_cqe64 *cqe;
1146 int work_done = 0;
1147
1148 if (unlikely(!MLX5E_TEST_BIT(rq->state, MLX5E_RQ_STATE_ENABLED)))
1149 return 0;
1150
1151 if (cq->decmprs_left)
1152 work_done += mlx5e_decompress_cqes_cont(rq, cq, 0, budget);
1153
1154 cqe = mlx5_cqwq_get_cqe(&cq->wq);
1155 if (!cqe) {
1156 if (unlikely(work_done))
1157 goto out;
1158 return 0;
1159 }
1160
1161 do {
1162 if (mlx5_get_cqe_format(cqe) == MLX5_COMPRESSED) {
1163 work_done +=
1164 mlx5e_decompress_cqes_start(rq, cq,
1165 budget - work_done);
1166 continue;
1167 }
1168
1169 mlx5_cqwq_pop(&cq->wq);
1170
1171 rq->handle_rx_cqe(rq, cqe);
1172 } while ((++work_done < budget) && (cqe = mlx5_cqwq_get_cqe(&cq->wq)));
1173
1174 out:
1175 if (xdpsq->db.doorbell) {
1176 mlx5e_xmit_xdp_doorbell(xdpsq);
1177 xdpsq->db.doorbell = false;
1178 }
1179
1180 mlx5_cqwq_update_db_record(&cq->wq);
1181
1182 /* ensure cq space is freed before enabling more cqes */
1183 wmb();
1184
1185 return work_done;
1186 }
1187
1188 bool mlx5e_poll_xdpsq_cq(struct mlx5e_cq *cq)
1189 {
1190 struct mlx5e_xdpsq *sq;
1191 struct mlx5_cqe64 *cqe;
1192 struct mlx5e_rq *rq;
1193 u16 sqcc;
1194 int i;
1195
1196 sq = container_of(cq, struct mlx5e_xdpsq, cq);
1197
1198 if (unlikely(!MLX5E_TEST_BIT(sq->state, MLX5E_SQ_STATE_ENABLED)))
1199 return false;
1200
1201 cqe = mlx5_cqwq_get_cqe(&cq->wq);
1202 if (!cqe)
1203 return false;
1204
1205 rq = container_of(sq, struct mlx5e_rq, xdpsq);
1206
1207 /* sq->cc must be updated only after mlx5_cqwq_update_db_record(),
1208 * otherwise a cq overrun may occur
1209 */
1210 sqcc = sq->cc;
1211
1212 i = 0;
1213 do {
1214 u16 wqe_counter;
1215 bool last_wqe;
1216
1217 mlx5_cqwq_pop(&cq->wq);
1218
1219 wqe_counter = be16_to_cpu(cqe->wqe_counter);
1220
1221 do {
1222 struct mlx5e_dma_info *di;
1223 u16 ci;
1224
1225 last_wqe = (sqcc == wqe_counter);
1226
1227 ci = sqcc & sq->wq.sz_m1;
1228 di = &sq->db.di[ci];
1229
1230 sqcc++;
1231 /* Recycle RX page */
1232 mlx5e_page_release(rq, di, true);
1233 } while (!last_wqe);
1234 } while ((++i < MLX5E_TX_CQ_POLL_BUDGET) && (cqe = mlx5_cqwq_get_cqe(&cq->wq)));
1235
1236 mlx5_cqwq_update_db_record(&cq->wq);
1237
1238 /* ensure cq space is freed before enabling more cqes */
1239 wmb();
1240
1241 sq->cc = sqcc;
1242 return (i == MLX5E_TX_CQ_POLL_BUDGET);
1243 }
1244
1245 void mlx5e_free_xdpsq_descs(struct mlx5e_xdpsq *sq)
1246 {
1247 struct mlx5e_rq *rq = container_of(sq, struct mlx5e_rq, xdpsq);
1248 struct mlx5e_dma_info *di;
1249 u16 ci;
1250
1251 while (sq->cc != sq->pc) {
1252 ci = sq->cc & sq->wq.sz_m1;
1253 di = &sq->db.di[ci];
1254 sq->cc++;
1255
1256 mlx5e_page_release(rq, di, false);
1257 }
1258 }
1259
1260 #ifdef CONFIG_MLX5_CORE_IPOIB
1261
1262 #define MLX5_IB_GRH_DGID_OFFSET 24
1263 #define MLX5_GID_SIZE 16
1264
1265 static inline void mlx5i_complete_rx_cqe(struct mlx5e_rq *rq,
1266 struct mlx5_cqe64 *cqe,
1267 u32 cqe_bcnt,
1268 struct sk_buff *skb)
1269 {
1270 struct hwtstamp_config *tstamp;
1271 struct net_device *netdev;
1272 struct mlx5e_priv *priv;
1273 char *pseudo_header;
1274 u32 qpn;
1275 u8 *dgid;
1276 u8 g;
1277
1278 qpn = be32_to_cpu(cqe->sop_drop_qpn) & 0xffffff;
1279 netdev = mlx5i_pkey_get_netdev(rq->netdev, qpn);
1280
1281 /* No mapping present, cannot process SKB. This might happen if a child
1282 * interface is going down while having unprocessed CQEs on parent RQ
1283 */
1284 if (unlikely(!netdev)) {
1285 /* TODO: add drop counters support */
1286 skb->dev = NULL;
1287 pr_warn_once("Unable to map QPN %u to dev - dropping skb\n", qpn);
1288 return;
1289 }
1290
1291 priv = mlx5i_epriv(netdev);
1292 tstamp = &priv->tstamp;
1293
1294 g = (be32_to_cpu(cqe->flags_rqpn) >> 28) & 3;
1295 dgid = skb->data + MLX5_IB_GRH_DGID_OFFSET;
1296 if ((!g) || dgid[0] != 0xff)
1297 skb->pkt_type = PACKET_HOST;
1298 else if (memcmp(dgid, netdev->broadcast + 4, MLX5_GID_SIZE) == 0)
1299 skb->pkt_type = PACKET_BROADCAST;
1300 else
1301 skb->pkt_type = PACKET_MULTICAST;
1302
1303 /* TODO: IB/ipoib: Allow mcast packets from other VFs
1304 * 68996a6e760e5c74654723eeb57bf65628ae87f4
1305 */
1306
1307 skb_pull(skb, MLX5_IB_GRH_BYTES);
1308
1309 skb->protocol = *((__be16 *)(skb->data));
1310
1311 skb->ip_summed = CHECKSUM_COMPLETE;
1312 skb->csum = csum_unfold((__force __sum16)cqe->check_sum);
1313
1314 if (unlikely(mlx5e_rx_hw_stamp(tstamp)))
1315 skb_hwtstamps(skb)->hwtstamp =
1316 mlx5_timecounter_cyc2time(rq->clock, get_cqe_ts(cqe));
1317
1318 skb_record_rx_queue(skb, rq->ix);
1319
1320 if (likely(netdev->features & NETIF_F_RXHASH))
1321 mlx5e_skb_set_hash(cqe, skb);
1322
1323 /* 20 bytes of ipoib header and 4 for encap existing */
1324 pseudo_header = skb_push(skb, MLX5_IPOIB_PSEUDO_LEN);
1325 memset(pseudo_header, 0, MLX5_IPOIB_PSEUDO_LEN);
1326 skb_reset_mac_header(skb);
1327 skb_pull(skb, MLX5_IPOIB_HARD_LEN);
1328
1329 skb->dev = netdev;
1330
1331 rq->stats.csum_complete++;
1332 rq->stats.packets++;
1333 rq->stats.bytes += cqe_bcnt;
1334 }
1335
1336 void mlx5i_handle_rx_cqe(struct mlx5e_rq *rq, struct mlx5_cqe64 *cqe)
1337 {
1338 struct mlx5e_wqe_frag_info *wi;
1339 struct mlx5e_rx_wqe *wqe;
1340 __be16 wqe_counter_be;
1341 struct sk_buff *skb;
1342 u16 wqe_counter;
1343 u32 cqe_bcnt;
1344
1345 wqe_counter_be = cqe->wqe_counter;
1346 wqe_counter = be16_to_cpu(wqe_counter_be);
1347 wqe = mlx5_wq_ll_get_wqe(&rq->wq, wqe_counter);
1348 wi = &rq->wqe.frag_info[wqe_counter];
1349 cqe_bcnt = be32_to_cpu(cqe->byte_cnt);
1350
1351 skb = skb_from_cqe(rq, cqe, wi, cqe_bcnt);
1352 if (!skb)
1353 goto wq_free_wqe;
1354
1355 mlx5i_complete_rx_cqe(rq, cqe, cqe_bcnt, skb);
1356 if (unlikely(!skb->dev)) {
1357 dev_kfree_skb_any(skb);
1358 goto wq_free_wqe;
1359 }
1360 napi_gro_receive(rq->cq.napi, skb);
1361
1362 wq_free_wqe:
1363 mlx5e_free_rx_wqe_reuse(rq, wi);
1364 mlx5_wq_ll_pop(&rq->wq, wqe_counter_be,
1365 &wqe->next.next_wqe_index);
1366 }
1367
1368 #endif /* CONFIG_MLX5_CORE_IPOIB */
1369
1370 #ifdef CONFIG_MLX5_EN_IPSEC
1371
1372 void mlx5e_ipsec_handle_rx_cqe(struct mlx5e_rq *rq, struct mlx5_cqe64 *cqe)
1373 {
1374 struct mlx5e_wqe_frag_info *wi;
1375 struct mlx5e_rx_wqe *wqe;
1376 __be16 wqe_counter_be;
1377 struct sk_buff *skb;
1378 u16 wqe_counter;
1379 u32 cqe_bcnt;
1380
1381 wqe_counter_be = cqe->wqe_counter;
1382 wqe_counter = be16_to_cpu(wqe_counter_be);
1383 wqe = mlx5_wq_ll_get_wqe(&rq->wq, wqe_counter);
1384 wi = &rq->wqe.frag_info[wqe_counter];
1385 cqe_bcnt = be32_to_cpu(cqe->byte_cnt);
1386
1387 skb = skb_from_cqe(rq, cqe, wi, cqe_bcnt);
1388 if (unlikely(!skb)) {
1389 /* a DROP, save the page-reuse checks */
1390 mlx5e_free_rx_wqe(rq, wi);
1391 goto wq_ll_pop;
1392 }
1393 skb = mlx5e_ipsec_handle_rx_skb(rq->netdev, skb);
1394 if (unlikely(!skb)) {
1395 mlx5e_free_rx_wqe(rq, wi);
1396 goto wq_ll_pop;
1397 }
1398
1399 mlx5e_complete_rx_cqe(rq, cqe, cqe_bcnt, skb);
1400 napi_gro_receive(rq->cq.napi, skb);
1401
1402 mlx5e_free_rx_wqe_reuse(rq, wi);
1403 wq_ll_pop:
1404 mlx5_wq_ll_pop(&rq->wq, wqe_counter_be,
1405 &wqe->next.next_wqe_index);
1406 }
1407
1408 #endif /* CONFIG_MLX5_EN_IPSEC */