]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blob - drivers/net/ethernet/mellanox/mlx5/core/en_rx.c
net/mlx5e: Set ECN for received packets using CQE indication
[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 #define short_frame(size) ((size) <= ETH_ZLEN + ETH_FCS_LEN)
669
670 static inline void mlx5e_handle_csum(struct net_device *netdev,
671 struct mlx5_cqe64 *cqe,
672 struct mlx5e_rq *rq,
673 struct sk_buff *skb,
674 bool lro)
675 {
676 int network_depth = 0;
677 __be16 proto;
678
679 if (unlikely(!(netdev->features & NETIF_F_RXCSUM)))
680 goto csum_none;
681
682 if (lro) {
683 skb->ip_summed = CHECKSUM_UNNECESSARY;
684 rq->stats.csum_unnecessary++;
685 return;
686 }
687
688 /* CQE csum doesn't cover padding octets in short ethernet
689 * frames. And the pad field is appended prior to calculating
690 * and appending the FCS field.
691 *
692 * Detecting these padded frames requires to verify and parse
693 * IP headers, so we simply force all those small frames to be
694 * CHECKSUM_UNNECESSARY even if they are not padded.
695 */
696 if (short_frame(skb->len))
697 goto csum_unnecessary;
698
699 if (likely(is_last_ethertype_ip(skb, &network_depth, &proto))) {
700 skb->ip_summed = CHECKSUM_COMPLETE;
701 skb->csum = csum_unfold((__force __sum16)cqe->check_sum);
702 if (network_depth > ETH_HLEN)
703 /* CQE csum is calculated from the IP header and does
704 * not cover VLAN headers (if present). This will add
705 * the checksum manually.
706 */
707 skb->csum = csum_partial(skb->data + ETH_HLEN,
708 network_depth - ETH_HLEN,
709 skb->csum);
710 if (unlikely(netdev->features & NETIF_F_RXFCS))
711 skb->csum = csum_block_add(skb->csum,
712 (__force __wsum)mlx5e_get_fcs(skb),
713 skb->len - ETH_FCS_LEN);
714 rq->stats.csum_complete++;
715 return;
716 }
717
718 csum_unnecessary:
719 if (likely((cqe->hds_ip_ext & CQE_L3_OK) &&
720 (cqe->hds_ip_ext & CQE_L4_OK))) {
721 skb->ip_summed = CHECKSUM_UNNECESSARY;
722 if (cqe_is_tunneled(cqe)) {
723 skb->csum_level = 1;
724 skb->encapsulation = 1;
725 rq->stats.csum_unnecessary_inner++;
726 return;
727 }
728 rq->stats.csum_unnecessary++;
729 return;
730 }
731 csum_none:
732 skb->ip_summed = CHECKSUM_NONE;
733 rq->stats.csum_none++;
734 }
735
736 #define MLX5E_CE_BIT_MASK 0x80
737
738 static inline void mlx5e_build_rx_skb(struct mlx5_cqe64 *cqe,
739 u32 cqe_bcnt,
740 struct mlx5e_rq *rq,
741 struct sk_buff *skb)
742 {
743 struct net_device *netdev = rq->netdev;
744 int lro_num_seg;
745
746 skb->mac_len = ETH_HLEN;
747 lro_num_seg = be32_to_cpu(cqe->srqn) >> 24;
748 if (lro_num_seg > 1) {
749 mlx5e_lro_update_hdr(skb, cqe, cqe_bcnt);
750 skb_shinfo(skb)->gso_size = DIV_ROUND_UP(cqe_bcnt, lro_num_seg);
751 /* Subtract one since we already counted this as one
752 * "regular" packet in mlx5e_complete_rx_cqe()
753 */
754 rq->stats.packets += lro_num_seg - 1;
755 rq->stats.lro_packets++;
756 rq->stats.lro_bytes += cqe_bcnt;
757 }
758
759 if (unlikely(mlx5e_rx_hw_stamp(rq->tstamp)))
760 skb_hwtstamps(skb)->hwtstamp =
761 mlx5_timecounter_cyc2time(rq->clock, get_cqe_ts(cqe));
762
763 skb_record_rx_queue(skb, rq->ix);
764
765 if (likely(netdev->features & NETIF_F_RXHASH))
766 mlx5e_skb_set_hash(cqe, skb);
767
768 if (cqe_has_vlan(cqe)) {
769 __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q),
770 be16_to_cpu(cqe->vlan_info));
771 rq->stats.removed_vlan_packets++;
772 }
773
774 skb->mark = be32_to_cpu(cqe->sop_drop_qpn) & MLX5E_TC_FLOW_ID_MASK;
775
776 mlx5e_handle_csum(netdev, cqe, rq, skb, !!lro_num_seg);
777 /* checking CE bit in cqe - MSB in ml_path field */
778 if (unlikely(cqe->ml_path & MLX5E_CE_BIT_MASK))
779 mlx5e_enable_ecn(rq, skb);
780
781 skb->protocol = eth_type_trans(skb, netdev);
782 }
783
784 static inline void mlx5e_complete_rx_cqe(struct mlx5e_rq *rq,
785 struct mlx5_cqe64 *cqe,
786 u32 cqe_bcnt,
787 struct sk_buff *skb)
788 {
789 rq->stats.packets++;
790 rq->stats.bytes += cqe_bcnt;
791 mlx5e_build_rx_skb(cqe, cqe_bcnt, rq, skb);
792 }
793
794 static inline void mlx5e_xmit_xdp_doorbell(struct mlx5e_xdpsq *sq)
795 {
796 struct mlx5_wq_cyc *wq = &sq->wq;
797 struct mlx5e_tx_wqe *wqe;
798 u16 pi = (sq->pc - 1) & wq->sz_m1; /* last pi */
799
800 wqe = mlx5_wq_cyc_get_wqe(wq, pi);
801
802 mlx5e_notify_hw(wq, sq->pc, sq->uar_map, &wqe->ctrl);
803 }
804
805 static inline bool mlx5e_xmit_xdp_frame(struct mlx5e_rq *rq,
806 struct mlx5e_dma_info *di,
807 const struct xdp_buff *xdp)
808 {
809 struct mlx5e_xdpsq *sq = &rq->xdpsq;
810 struct mlx5_wq_cyc *wq = &sq->wq;
811 u16 pi = sq->pc & wq->sz_m1;
812 struct mlx5e_tx_wqe *wqe = mlx5_wq_cyc_get_wqe(wq, pi);
813
814 struct mlx5_wqe_ctrl_seg *cseg = &wqe->ctrl;
815 struct mlx5_wqe_eth_seg *eseg = &wqe->eth;
816 struct mlx5_wqe_data_seg *dseg;
817
818 ptrdiff_t data_offset = xdp->data - xdp->data_hard_start;
819 dma_addr_t dma_addr = di->addr + data_offset;
820 unsigned int dma_len = xdp->data_end - xdp->data;
821
822 prefetchw(wqe);
823
824 if (unlikely(dma_len < MLX5E_XDP_MIN_INLINE ||
825 MLX5E_SW2HW_MTU(rq->channel->priv, rq->netdev->mtu) < dma_len)) {
826 rq->stats.xdp_drop++;
827 return false;
828 }
829
830 if (unlikely(!mlx5e_wqc_has_room_for(wq, sq->cc, sq->pc, 1))) {
831 if (sq->db.doorbell) {
832 /* SQ is full, ring doorbell */
833 mlx5e_xmit_xdp_doorbell(sq);
834 sq->db.doorbell = false;
835 }
836 rq->stats.xdp_tx_full++;
837 return false;
838 }
839
840 dma_sync_single_for_device(sq->pdev, dma_addr, dma_len, PCI_DMA_TODEVICE);
841
842 cseg->fm_ce_se = 0;
843
844 dseg = (struct mlx5_wqe_data_seg *)eseg + 1;
845
846 /* copy the inline part if required */
847 if (sq->min_inline_mode != MLX5_INLINE_MODE_NONE) {
848 memcpy(eseg->inline_hdr.start, xdp->data, MLX5E_XDP_MIN_INLINE);
849 eseg->inline_hdr.sz = cpu_to_be16(MLX5E_XDP_MIN_INLINE);
850 dma_len -= MLX5E_XDP_MIN_INLINE;
851 dma_addr += MLX5E_XDP_MIN_INLINE;
852 dseg++;
853 }
854
855 /* write the dma part */
856 dseg->addr = cpu_to_be64(dma_addr);
857 dseg->byte_count = cpu_to_be32(dma_len);
858
859 cseg->opmod_idx_opcode = cpu_to_be32((sq->pc << 8) | MLX5_OPCODE_SEND);
860
861 /* move page to reference to sq responsibility,
862 * and mark so it's not put back in page-cache.
863 */
864 rq->wqe.xdp_xmit = true;
865 sq->db.di[pi] = *di;
866 sq->pc++;
867
868 sq->db.doorbell = true;
869
870 rq->stats.xdp_tx++;
871 return true;
872 }
873
874 /* returns true if packet was consumed by xdp */
875 static inline int mlx5e_xdp_handle(struct mlx5e_rq *rq,
876 struct mlx5e_dma_info *di,
877 void *va, u16 *rx_headroom, u32 *len)
878 {
879 const struct bpf_prog *prog = READ_ONCE(rq->xdp_prog);
880 struct xdp_buff xdp;
881 u32 act;
882
883 if (!prog)
884 return false;
885
886 xdp.data = va + *rx_headroom;
887 xdp_set_data_meta_invalid(&xdp);
888 xdp.data_end = xdp.data + *len;
889 xdp.data_hard_start = va;
890
891 act = bpf_prog_run_xdp(prog, &xdp);
892 switch (act) {
893 case XDP_PASS:
894 *rx_headroom = xdp.data - xdp.data_hard_start;
895 *len = xdp.data_end - xdp.data;
896 return false;
897 case XDP_TX:
898 if (unlikely(!mlx5e_xmit_xdp_frame(rq, di, &xdp)))
899 trace_xdp_exception(rq->netdev, prog, act);
900 return true;
901 default:
902 bpf_warn_invalid_xdp_action(act);
903 case XDP_ABORTED:
904 trace_xdp_exception(rq->netdev, prog, act);
905 case XDP_DROP:
906 rq->stats.xdp_drop++;
907 return true;
908 }
909 }
910
911 static inline
912 struct sk_buff *skb_from_cqe(struct mlx5e_rq *rq, struct mlx5_cqe64 *cqe,
913 struct mlx5e_wqe_frag_info *wi, u32 cqe_bcnt)
914 {
915 struct mlx5e_dma_info *di = &wi->di;
916 u16 rx_headroom = rq->buff.headroom;
917 struct sk_buff *skb;
918 void *va, *data;
919 bool consumed;
920 u32 frag_size;
921
922 va = page_address(di->page) + wi->offset;
923 data = va + rx_headroom;
924 frag_size = MLX5_SKB_FRAG_SZ(rx_headroom + cqe_bcnt);
925
926 dma_sync_single_range_for_cpu(rq->pdev,
927 di->addr + wi->offset,
928 0, frag_size,
929 DMA_FROM_DEVICE);
930 prefetch(data);
931 wi->offset += frag_size;
932
933 if (unlikely((cqe->op_own >> 4) != MLX5_CQE_RESP_SEND)) {
934 rq->stats.wqe_err++;
935 return NULL;
936 }
937
938 rcu_read_lock();
939 consumed = mlx5e_xdp_handle(rq, di, va, &rx_headroom, &cqe_bcnt);
940 rcu_read_unlock();
941 if (consumed)
942 return NULL; /* page/packet was consumed by XDP */
943
944 skb = build_skb(va, frag_size);
945 if (unlikely(!skb)) {
946 rq->stats.buff_alloc_err++;
947 return NULL;
948 }
949
950 /* queue up for recycling/reuse */
951 page_ref_inc(di->page);
952
953 skb_reserve(skb, rx_headroom);
954 skb_put(skb, cqe_bcnt);
955
956 return skb;
957 }
958
959 void mlx5e_handle_rx_cqe(struct mlx5e_rq *rq, struct mlx5_cqe64 *cqe)
960 {
961 struct mlx5e_wqe_frag_info *wi;
962 struct mlx5e_rx_wqe *wqe;
963 __be16 wqe_counter_be;
964 struct sk_buff *skb;
965 u16 wqe_counter;
966 u32 cqe_bcnt;
967
968 wqe_counter_be = cqe->wqe_counter;
969 wqe_counter = be16_to_cpu(wqe_counter_be);
970 wqe = mlx5_wq_ll_get_wqe(&rq->wq, wqe_counter);
971 wi = &rq->wqe.frag_info[wqe_counter];
972 cqe_bcnt = be32_to_cpu(cqe->byte_cnt);
973
974 skb = skb_from_cqe(rq, cqe, wi, cqe_bcnt);
975 if (!skb) {
976 /* probably for XDP */
977 if (rq->wqe.xdp_xmit) {
978 wi->di.page = NULL;
979 rq->wqe.xdp_xmit = false;
980 /* do not return page to cache, it will be returned on XDP_TX completion */
981 goto wq_ll_pop;
982 }
983 /* probably an XDP_DROP, save the page-reuse checks */
984 mlx5e_free_rx_wqe(rq, wi);
985 goto wq_ll_pop;
986 }
987
988 mlx5e_complete_rx_cqe(rq, cqe, cqe_bcnt, skb);
989 napi_gro_receive(rq->cq.napi, skb);
990
991 mlx5e_free_rx_wqe_reuse(rq, wi);
992 wq_ll_pop:
993 mlx5_wq_ll_pop(&rq->wq, wqe_counter_be,
994 &wqe->next.next_wqe_index);
995 }
996
997 #ifdef CONFIG_MLX5_ESWITCH
998 void mlx5e_handle_rx_cqe_rep(struct mlx5e_rq *rq, struct mlx5_cqe64 *cqe)
999 {
1000 struct net_device *netdev = rq->netdev;
1001 struct mlx5e_priv *priv = netdev_priv(netdev);
1002 struct mlx5e_rep_priv *rpriv = priv->ppriv;
1003 struct mlx5_eswitch_rep *rep = rpriv->rep;
1004 struct mlx5e_wqe_frag_info *wi;
1005 struct mlx5e_rx_wqe *wqe;
1006 struct sk_buff *skb;
1007 __be16 wqe_counter_be;
1008 u16 wqe_counter;
1009 u32 cqe_bcnt;
1010
1011 wqe_counter_be = cqe->wqe_counter;
1012 wqe_counter = be16_to_cpu(wqe_counter_be);
1013 wqe = mlx5_wq_ll_get_wqe(&rq->wq, wqe_counter);
1014 wi = &rq->wqe.frag_info[wqe_counter];
1015 cqe_bcnt = be32_to_cpu(cqe->byte_cnt);
1016
1017 skb = skb_from_cqe(rq, cqe, wi, cqe_bcnt);
1018 if (!skb) {
1019 if (rq->wqe.xdp_xmit) {
1020 wi->di.page = NULL;
1021 rq->wqe.xdp_xmit = false;
1022 /* do not return page to cache, it will be returned on XDP_TX completion */
1023 goto wq_ll_pop;
1024 }
1025 /* probably an XDP_DROP, save the page-reuse checks */
1026 mlx5e_free_rx_wqe(rq, wi);
1027 goto wq_ll_pop;
1028 }
1029
1030 mlx5e_complete_rx_cqe(rq, cqe, cqe_bcnt, skb);
1031
1032 if (rep->vlan && skb_vlan_tag_present(skb))
1033 skb_vlan_pop(skb);
1034
1035 napi_gro_receive(rq->cq.napi, skb);
1036
1037 mlx5e_free_rx_wqe_reuse(rq, wi);
1038 wq_ll_pop:
1039 mlx5_wq_ll_pop(&rq->wq, wqe_counter_be,
1040 &wqe->next.next_wqe_index);
1041 }
1042 #endif
1043
1044 static inline void mlx5e_mpwqe_fill_rx_skb(struct mlx5e_rq *rq,
1045 struct mlx5_cqe64 *cqe,
1046 struct mlx5e_mpw_info *wi,
1047 u32 cqe_bcnt,
1048 struct sk_buff *skb)
1049 {
1050 u16 stride_ix = mpwrq_get_cqe_stride_index(cqe);
1051 u32 wqe_offset = stride_ix << rq->mpwqe.log_stride_sz;
1052 u32 head_offset = wqe_offset & (PAGE_SIZE - 1);
1053 u32 page_idx = wqe_offset >> PAGE_SHIFT;
1054 u32 head_page_idx = page_idx;
1055 u16 headlen = min_t(u16, MLX5_MPWRQ_SMALL_PACKET_THRESHOLD, cqe_bcnt);
1056 u32 frag_offset = head_offset + headlen;
1057 u16 byte_cnt = cqe_bcnt - headlen;
1058
1059 if (unlikely(frag_offset >= PAGE_SIZE)) {
1060 page_idx++;
1061 frag_offset -= PAGE_SIZE;
1062 }
1063
1064 while (byte_cnt) {
1065 u32 pg_consumed_bytes =
1066 min_t(u32, PAGE_SIZE - frag_offset, byte_cnt);
1067
1068 mlx5e_add_skb_frag_mpwqe(rq, skb, wi, page_idx, frag_offset,
1069 pg_consumed_bytes);
1070 byte_cnt -= pg_consumed_bytes;
1071 frag_offset = 0;
1072 page_idx++;
1073 }
1074 /* copy header */
1075 mlx5e_copy_skb_header_mpwqe(rq->pdev, skb, wi, head_page_idx,
1076 head_offset, headlen);
1077 /* skb linear part was allocated with headlen and aligned to long */
1078 skb->tail += headlen;
1079 skb->len += headlen;
1080 }
1081
1082 void mlx5e_handle_rx_cqe_mpwrq(struct mlx5e_rq *rq, struct mlx5_cqe64 *cqe)
1083 {
1084 u16 cstrides = mpwrq_get_cqe_consumed_strides(cqe);
1085 u16 wqe_id = be16_to_cpu(cqe->wqe_id);
1086 struct mlx5e_mpw_info *wi = &rq->mpwqe.info[wqe_id];
1087 struct mlx5e_rx_wqe *wqe = mlx5_wq_ll_get_wqe(&rq->wq, wqe_id);
1088 struct sk_buff *skb;
1089 u16 cqe_bcnt;
1090
1091 wi->consumed_strides += cstrides;
1092
1093 if (unlikely((cqe->op_own >> 4) != MLX5_CQE_RESP_SEND)) {
1094 rq->stats.wqe_err++;
1095 goto mpwrq_cqe_out;
1096 }
1097
1098 if (unlikely(mpwrq_is_filler_cqe(cqe))) {
1099 rq->stats.mpwqe_filler++;
1100 goto mpwrq_cqe_out;
1101 }
1102
1103 skb = napi_alloc_skb(rq->cq.napi,
1104 ALIGN(MLX5_MPWRQ_SMALL_PACKET_THRESHOLD,
1105 sizeof(long)));
1106 if (unlikely(!skb)) {
1107 rq->stats.buff_alloc_err++;
1108 goto mpwrq_cqe_out;
1109 }
1110
1111 prefetchw(skb->data);
1112 cqe_bcnt = mpwrq_get_cqe_byte_cnt(cqe);
1113
1114 mlx5e_mpwqe_fill_rx_skb(rq, cqe, wi, cqe_bcnt, skb);
1115 mlx5e_complete_rx_cqe(rq, cqe, cqe_bcnt, skb);
1116 napi_gro_receive(rq->cq.napi, skb);
1117
1118 mpwrq_cqe_out:
1119 if (likely(wi->consumed_strides < rq->mpwqe.num_strides))
1120 return;
1121
1122 mlx5e_free_rx_mpwqe(rq, wi);
1123 mlx5_wq_ll_pop(&rq->wq, cqe->wqe_id, &wqe->next.next_wqe_index);
1124 }
1125
1126 int mlx5e_poll_rx_cq(struct mlx5e_cq *cq, int budget)
1127 {
1128 struct mlx5e_rq *rq = container_of(cq, struct mlx5e_rq, cq);
1129 struct mlx5e_xdpsq *xdpsq = &rq->xdpsq;
1130 struct mlx5_cqe64 *cqe;
1131 int work_done = 0;
1132
1133 if (unlikely(!MLX5E_TEST_BIT(rq->state, MLX5E_RQ_STATE_ENABLED)))
1134 return 0;
1135
1136 if (cq->decmprs_left)
1137 work_done += mlx5e_decompress_cqes_cont(rq, cq, 0, budget);
1138
1139 cqe = mlx5_cqwq_get_cqe(&cq->wq);
1140 if (!cqe) {
1141 if (unlikely(work_done))
1142 goto out;
1143 return 0;
1144 }
1145
1146 do {
1147 if (mlx5_get_cqe_format(cqe) == MLX5_COMPRESSED) {
1148 work_done +=
1149 mlx5e_decompress_cqes_start(rq, cq,
1150 budget - work_done);
1151 continue;
1152 }
1153
1154 mlx5_cqwq_pop(&cq->wq);
1155
1156 rq->handle_rx_cqe(rq, cqe);
1157 } while ((++work_done < budget) && (cqe = mlx5_cqwq_get_cqe(&cq->wq)));
1158
1159 out:
1160 if (xdpsq->db.doorbell) {
1161 mlx5e_xmit_xdp_doorbell(xdpsq);
1162 xdpsq->db.doorbell = false;
1163 }
1164
1165 mlx5_cqwq_update_db_record(&cq->wq);
1166
1167 /* ensure cq space is freed before enabling more cqes */
1168 wmb();
1169
1170 return work_done;
1171 }
1172
1173 bool mlx5e_poll_xdpsq_cq(struct mlx5e_cq *cq)
1174 {
1175 struct mlx5e_xdpsq *sq;
1176 struct mlx5_cqe64 *cqe;
1177 struct mlx5e_rq *rq;
1178 u16 sqcc;
1179 int i;
1180
1181 sq = container_of(cq, struct mlx5e_xdpsq, cq);
1182
1183 if (unlikely(!MLX5E_TEST_BIT(sq->state, MLX5E_SQ_STATE_ENABLED)))
1184 return false;
1185
1186 cqe = mlx5_cqwq_get_cqe(&cq->wq);
1187 if (!cqe)
1188 return false;
1189
1190 rq = container_of(sq, struct mlx5e_rq, xdpsq);
1191
1192 /* sq->cc must be updated only after mlx5_cqwq_update_db_record(),
1193 * otherwise a cq overrun may occur
1194 */
1195 sqcc = sq->cc;
1196
1197 i = 0;
1198 do {
1199 u16 wqe_counter;
1200 bool last_wqe;
1201
1202 mlx5_cqwq_pop(&cq->wq);
1203
1204 wqe_counter = be16_to_cpu(cqe->wqe_counter);
1205
1206 do {
1207 struct mlx5e_dma_info *di;
1208 u16 ci;
1209
1210 last_wqe = (sqcc == wqe_counter);
1211
1212 ci = sqcc & sq->wq.sz_m1;
1213 di = &sq->db.di[ci];
1214
1215 sqcc++;
1216 /* Recycle RX page */
1217 mlx5e_page_release(rq, di, true);
1218 } while (!last_wqe);
1219 } while ((++i < MLX5E_TX_CQ_POLL_BUDGET) && (cqe = mlx5_cqwq_get_cqe(&cq->wq)));
1220
1221 mlx5_cqwq_update_db_record(&cq->wq);
1222
1223 /* ensure cq space is freed before enabling more cqes */
1224 wmb();
1225
1226 sq->cc = sqcc;
1227 return (i == MLX5E_TX_CQ_POLL_BUDGET);
1228 }
1229
1230 void mlx5e_free_xdpsq_descs(struct mlx5e_xdpsq *sq)
1231 {
1232 struct mlx5e_rq *rq = container_of(sq, struct mlx5e_rq, xdpsq);
1233 struct mlx5e_dma_info *di;
1234 u16 ci;
1235
1236 while (sq->cc != sq->pc) {
1237 ci = sq->cc & sq->wq.sz_m1;
1238 di = &sq->db.di[ci];
1239 sq->cc++;
1240
1241 mlx5e_page_release(rq, di, false);
1242 }
1243 }
1244
1245 #ifdef CONFIG_MLX5_CORE_IPOIB
1246
1247 #define MLX5_IB_GRH_DGID_OFFSET 24
1248 #define MLX5_GID_SIZE 16
1249
1250 static inline void mlx5i_complete_rx_cqe(struct mlx5e_rq *rq,
1251 struct mlx5_cqe64 *cqe,
1252 u32 cqe_bcnt,
1253 struct sk_buff *skb)
1254 {
1255 struct hwtstamp_config *tstamp;
1256 struct net_device *netdev;
1257 struct mlx5e_priv *priv;
1258 char *pseudo_header;
1259 u32 qpn;
1260 u8 *dgid;
1261 u8 g;
1262
1263 qpn = be32_to_cpu(cqe->sop_drop_qpn) & 0xffffff;
1264 netdev = mlx5i_pkey_get_netdev(rq->netdev, qpn);
1265
1266 /* No mapping present, cannot process SKB. This might happen if a child
1267 * interface is going down while having unprocessed CQEs on parent RQ
1268 */
1269 if (unlikely(!netdev)) {
1270 /* TODO: add drop counters support */
1271 skb->dev = NULL;
1272 pr_warn_once("Unable to map QPN %u to dev - dropping skb\n", qpn);
1273 return;
1274 }
1275
1276 priv = mlx5i_epriv(netdev);
1277 tstamp = &priv->tstamp;
1278
1279 g = (be32_to_cpu(cqe->flags_rqpn) >> 28) & 3;
1280 dgid = skb->data + MLX5_IB_GRH_DGID_OFFSET;
1281 if ((!g) || dgid[0] != 0xff)
1282 skb->pkt_type = PACKET_HOST;
1283 else if (memcmp(dgid, netdev->broadcast + 4, MLX5_GID_SIZE) == 0)
1284 skb->pkt_type = PACKET_BROADCAST;
1285 else
1286 skb->pkt_type = PACKET_MULTICAST;
1287
1288 /* TODO: IB/ipoib: Allow mcast packets from other VFs
1289 * 68996a6e760e5c74654723eeb57bf65628ae87f4
1290 */
1291
1292 skb_pull(skb, MLX5_IB_GRH_BYTES);
1293
1294 skb->protocol = *((__be16 *)(skb->data));
1295
1296 skb->ip_summed = CHECKSUM_COMPLETE;
1297 skb->csum = csum_unfold((__force __sum16)cqe->check_sum);
1298
1299 if (unlikely(mlx5e_rx_hw_stamp(tstamp)))
1300 skb_hwtstamps(skb)->hwtstamp =
1301 mlx5_timecounter_cyc2time(rq->clock, get_cqe_ts(cqe));
1302
1303 skb_record_rx_queue(skb, rq->ix);
1304
1305 if (likely(netdev->features & NETIF_F_RXHASH))
1306 mlx5e_skb_set_hash(cqe, skb);
1307
1308 /* 20 bytes of ipoib header and 4 for encap existing */
1309 pseudo_header = skb_push(skb, MLX5_IPOIB_PSEUDO_LEN);
1310 memset(pseudo_header, 0, MLX5_IPOIB_PSEUDO_LEN);
1311 skb_reset_mac_header(skb);
1312 skb_pull(skb, MLX5_IPOIB_HARD_LEN);
1313
1314 skb->dev = netdev;
1315
1316 rq->stats.csum_complete++;
1317 rq->stats.packets++;
1318 rq->stats.bytes += cqe_bcnt;
1319 }
1320
1321 void mlx5i_handle_rx_cqe(struct mlx5e_rq *rq, struct mlx5_cqe64 *cqe)
1322 {
1323 struct mlx5e_wqe_frag_info *wi;
1324 struct mlx5e_rx_wqe *wqe;
1325 __be16 wqe_counter_be;
1326 struct sk_buff *skb;
1327 u16 wqe_counter;
1328 u32 cqe_bcnt;
1329
1330 wqe_counter_be = cqe->wqe_counter;
1331 wqe_counter = be16_to_cpu(wqe_counter_be);
1332 wqe = mlx5_wq_ll_get_wqe(&rq->wq, wqe_counter);
1333 wi = &rq->wqe.frag_info[wqe_counter];
1334 cqe_bcnt = be32_to_cpu(cqe->byte_cnt);
1335
1336 skb = skb_from_cqe(rq, cqe, wi, cqe_bcnt);
1337 if (!skb)
1338 goto wq_free_wqe;
1339
1340 mlx5i_complete_rx_cqe(rq, cqe, cqe_bcnt, skb);
1341 if (unlikely(!skb->dev)) {
1342 dev_kfree_skb_any(skb);
1343 goto wq_free_wqe;
1344 }
1345 napi_gro_receive(rq->cq.napi, skb);
1346
1347 wq_free_wqe:
1348 mlx5e_free_rx_wqe_reuse(rq, wi);
1349 mlx5_wq_ll_pop(&rq->wq, wqe_counter_be,
1350 &wqe->next.next_wqe_index);
1351 }
1352
1353 #endif /* CONFIG_MLX5_CORE_IPOIB */
1354
1355 #ifdef CONFIG_MLX5_EN_IPSEC
1356
1357 void mlx5e_ipsec_handle_rx_cqe(struct mlx5e_rq *rq, struct mlx5_cqe64 *cqe)
1358 {
1359 struct mlx5e_wqe_frag_info *wi;
1360 struct mlx5e_rx_wqe *wqe;
1361 __be16 wqe_counter_be;
1362 struct sk_buff *skb;
1363 u16 wqe_counter;
1364 u32 cqe_bcnt;
1365
1366 wqe_counter_be = cqe->wqe_counter;
1367 wqe_counter = be16_to_cpu(wqe_counter_be);
1368 wqe = mlx5_wq_ll_get_wqe(&rq->wq, wqe_counter);
1369 wi = &rq->wqe.frag_info[wqe_counter];
1370 cqe_bcnt = be32_to_cpu(cqe->byte_cnt);
1371
1372 skb = skb_from_cqe(rq, cqe, wi, cqe_bcnt);
1373 if (unlikely(!skb)) {
1374 /* a DROP, save the page-reuse checks */
1375 mlx5e_free_rx_wqe(rq, wi);
1376 goto wq_ll_pop;
1377 }
1378 skb = mlx5e_ipsec_handle_rx_skb(rq->netdev, skb);
1379 if (unlikely(!skb)) {
1380 mlx5e_free_rx_wqe(rq, wi);
1381 goto wq_ll_pop;
1382 }
1383
1384 mlx5e_complete_rx_cqe(rq, cqe, cqe_bcnt, skb);
1385 napi_gro_receive(rq->cq.napi, skb);
1386
1387 mlx5e_free_rx_wqe_reuse(rq, wi);
1388 wq_ll_pop:
1389 mlx5_wq_ll_pop(&rq->wq, wqe_counter_be,
1390 &wqe->next.next_wqe_index);
1391 }
1392
1393 #endif /* CONFIG_MLX5_EN_IPSEC */