2 * Copyright (c) 2015, Mellanox Technologies. All rights reserved.
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:
10 * Redistribution and use in source and binary forms, with or
11 * without modification, are permitted provided that the following
14 * - Redistributions of source code must retain the above
15 * copyright notice, this list of conditions and the following
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.
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
34 #include <linux/ipv6.h>
35 #include <linux/tcp.h>
36 #include <net/busy_poll.h>
40 static inline bool mlx5e_rx_hw_stamp(struct mlx5e_tstamp *tstamp)
42 return tstamp->hwtstamp_config.rx_filter == HWTSTAMP_FILTER_ALL;
45 static inline void mlx5e_read_cqe_slot(struct mlx5e_cq *cq, u32 cqcc,
48 u32 ci = cqcc & cq->wq.sz_m1;
50 memcpy(data, mlx5_cqwq_get_wqe(&cq->wq, ci), sizeof(struct mlx5_cqe64));
53 static inline void mlx5e_read_title_slot(struct mlx5e_rq *rq,
54 struct mlx5e_cq *cq, u32 cqcc)
56 mlx5e_read_cqe_slot(cq, cqcc, &cq->title);
57 cq->decmprs_left = be32_to_cpu(cq->title.byte_cnt);
58 cq->decmprs_wqe_counter = be16_to_cpu(cq->title.wqe_counter);
59 rq->stats.cqe_compress_blks++;
62 static inline void mlx5e_read_mini_arr_slot(struct mlx5e_cq *cq, u32 cqcc)
64 mlx5e_read_cqe_slot(cq, cqcc, cq->mini_arr);
68 static inline void mlx5e_cqes_update_owner(struct mlx5e_cq *cq, u32 cqcc, int n)
70 u8 op_own = (cqcc >> cq->wq.log_sz) & 1;
71 u32 wq_sz = 1 << cq->wq.log_sz;
72 u32 ci = cqcc & cq->wq.sz_m1;
73 u32 ci_top = min_t(u32, wq_sz, ci + n);
75 for (; ci < ci_top; ci++, n--) {
76 struct mlx5_cqe64 *cqe = mlx5_cqwq_get_wqe(&cq->wq, ci);
81 if (unlikely(ci == wq_sz)) {
83 for (ci = 0; ci < n; ci++) {
84 struct mlx5_cqe64 *cqe = mlx5_cqwq_get_wqe(&cq->wq, ci);
91 static inline void mlx5e_decompress_cqe(struct mlx5e_rq *rq,
92 struct mlx5e_cq *cq, u32 cqcc)
96 cq->title.byte_cnt = cq->mini_arr[cq->mini_arr_idx].byte_cnt;
97 cq->title.check_sum = cq->mini_arr[cq->mini_arr_idx].checksum;
98 cq->title.op_own &= 0xf0;
99 cq->title.op_own |= 0x01 & (cqcc >> cq->wq.log_sz);
100 cq->title.wqe_counter = cpu_to_be16(cq->decmprs_wqe_counter);
103 rq->wq_type == MLX5_WQ_TYPE_LINKED_LIST_STRIDING_RQ ?
104 mpwrq_get_cqe_consumed_strides(&cq->title) : 1;
105 cq->decmprs_wqe_counter =
106 (cq->decmprs_wqe_counter + wqe_cnt_step) & rq->wq.sz_m1;
109 static inline void mlx5e_decompress_cqe_no_hash(struct mlx5e_rq *rq,
110 struct mlx5e_cq *cq, u32 cqcc)
112 mlx5e_decompress_cqe(rq, cq, cqcc);
113 cq->title.rss_hash_type = 0;
114 cq->title.rss_hash_result = 0;
117 static inline u32 mlx5e_decompress_cqes_cont(struct mlx5e_rq *rq,
119 int update_owner_only,
122 u32 cqcc = cq->wq.cc + update_owner_only;
126 cqe_count = min_t(u32, cq->decmprs_left, budget_rem);
128 for (i = update_owner_only; i < cqe_count;
129 i++, cq->mini_arr_idx++, cqcc++) {
130 if (unlikely(cq->mini_arr_idx == MLX5_MINI_CQE_ARRAY_SIZE))
131 mlx5e_read_mini_arr_slot(cq, cqcc);
133 mlx5e_decompress_cqe_no_hash(rq, cq, cqcc);
134 rq->handle_rx_cqe(rq, &cq->title);
136 mlx5e_cqes_update_owner(cq, cq->wq.cc, cqcc - cq->wq.cc);
138 cq->decmprs_left -= cqe_count;
139 rq->stats.cqe_compress_pkts += cqe_count;
144 static inline u32 mlx5e_decompress_cqes_start(struct mlx5e_rq *rq,
148 mlx5e_read_title_slot(rq, cq, cq->wq.cc);
149 mlx5e_read_mini_arr_slot(cq, cq->wq.cc + 1);
150 mlx5e_decompress_cqe(rq, cq, cq->wq.cc);
151 rq->handle_rx_cqe(rq, &cq->title);
154 return mlx5e_decompress_cqes_cont(rq, cq, 1, budget_rem) - 1;
157 void mlx5e_modify_rx_cqe_compression(struct mlx5e_priv *priv, bool val)
161 if (!MLX5_CAP_GEN(priv->mdev, cqe_compression))
164 mutex_lock(&priv->state_lock);
166 if (priv->params.rx_cqe_compress == val)
169 was_opened = test_bit(MLX5E_STATE_OPENED, &priv->state);
171 mlx5e_close_locked(priv->netdev);
173 priv->params.rx_cqe_compress = val;
176 mlx5e_open_locked(priv->netdev);
179 mutex_unlock(&priv->state_lock);
182 int mlx5e_alloc_rx_wqe(struct mlx5e_rq *rq, struct mlx5e_rx_wqe *wqe, u16 ix)
187 skb = napi_alloc_skb(rq->cq.napi, rq->wqe_sz);
191 dma_addr = dma_map_single(rq->pdev,
192 /* hw start padding */
198 if (unlikely(dma_mapping_error(rq->pdev, dma_addr)))
201 *((dma_addr_t *)skb->cb) = dma_addr;
202 wqe->data.addr = cpu_to_be64(dma_addr);
203 wqe->data.lkey = rq->mkey_be;
216 mlx5e_dma_pre_sync_linear_mpwqe(struct device *pdev,
217 struct mlx5e_mpw_info *wi,
218 u32 wqe_offset, u32 len)
220 dma_sync_single_for_cpu(pdev, wi->dma_info.addr + wqe_offset,
221 len, DMA_FROM_DEVICE);
225 mlx5e_dma_pre_sync_fragmented_mpwqe(struct device *pdev,
226 struct mlx5e_mpw_info *wi,
227 u32 wqe_offset, u32 len)
229 /* No dma pre sync for fragmented MPWQE */
233 mlx5e_add_skb_frag_linear_mpwqe(struct device *pdev,
235 struct mlx5e_mpw_info *wi,
236 u32 page_idx, u32 frag_offset,
239 unsigned int truesize = ALIGN(len, MLX5_MPWRQ_STRIDE_SIZE);
241 wi->skbs_frags[page_idx]++;
242 skb_add_rx_frag(skb, skb_shinfo(skb)->nr_frags,
243 &wi->dma_info.page[page_idx], frag_offset,
248 mlx5e_add_skb_frag_fragmented_mpwqe(struct device *pdev,
250 struct mlx5e_mpw_info *wi,
251 u32 page_idx, u32 frag_offset,
254 unsigned int truesize = ALIGN(len, MLX5_MPWRQ_STRIDE_SIZE);
256 dma_sync_single_for_cpu(pdev,
257 wi->umr.dma_info[page_idx].addr + frag_offset,
258 len, DMA_FROM_DEVICE);
259 wi->skbs_frags[page_idx]++;
260 skb_add_rx_frag(skb, skb_shinfo(skb)->nr_frags,
261 wi->umr.dma_info[page_idx].page, frag_offset,
266 mlx5e_copy_skb_header_linear_mpwqe(struct device *pdev,
268 struct mlx5e_mpw_info *wi,
269 u32 page_idx, u32 offset,
272 struct page *page = &wi->dma_info.page[page_idx];
274 skb_copy_to_linear_data(skb, page_address(page) + offset,
275 ALIGN(headlen, sizeof(long)));
279 mlx5e_copy_skb_header_fragmented_mpwqe(struct device *pdev,
281 struct mlx5e_mpw_info *wi,
282 u32 page_idx, u32 offset,
285 u16 headlen_pg = min_t(u32, headlen, PAGE_SIZE - offset);
286 struct mlx5e_dma_info *dma_info = &wi->umr.dma_info[page_idx];
289 /* Aligning len to sizeof(long) optimizes memcpy performance */
290 len = ALIGN(headlen_pg, sizeof(long));
291 dma_sync_single_for_cpu(pdev, dma_info->addr + offset, len,
293 skb_copy_to_linear_data_offset(skb, 0,
294 page_address(dma_info->page) + offset,
296 #if (MLX5_MPWRQ_SMALL_PACKET_THRESHOLD >= MLX5_MPWRQ_STRIDE_SIZE)
297 if (unlikely(offset + headlen > PAGE_SIZE)) {
300 len = ALIGN(headlen - headlen_pg, sizeof(long));
301 dma_sync_single_for_cpu(pdev, dma_info->addr, len,
303 skb_copy_to_linear_data_offset(skb, headlen_pg,
304 page_address(dma_info->page),
310 static u16 mlx5e_get_wqe_mtt_offset(u16 rq_ix, u16 wqe_ix)
312 return rq_ix * MLX5_CHANNEL_MAX_NUM_MTTS +
313 wqe_ix * ALIGN(MLX5_MPWRQ_PAGES_PER_WQE, 8);
316 static void mlx5e_build_umr_wqe(struct mlx5e_rq *rq,
318 struct mlx5e_umr_wqe *wqe,
321 struct mlx5_wqe_ctrl_seg *cseg = &wqe->ctrl;
322 struct mlx5_wqe_umr_ctrl_seg *ucseg = &wqe->uctrl;
323 struct mlx5_wqe_data_seg *dseg = &wqe->data;
324 struct mlx5e_mpw_info *wi = &rq->wqe_info[ix];
325 u8 ds_cnt = DIV_ROUND_UP(sizeof(*wqe), MLX5_SEND_WQE_DS);
326 u16 umr_wqe_mtt_offset = mlx5e_get_wqe_mtt_offset(rq->ix, ix);
328 memset(wqe, 0, sizeof(*wqe));
329 cseg->opmod_idx_opcode =
330 cpu_to_be32((sq->pc << MLX5_WQE_CTRL_WQE_INDEX_SHIFT) |
332 cseg->qpn_ds = cpu_to_be32((sq->sqn << MLX5_WQE_CTRL_QPN_SHIFT) |
334 cseg->fm_ce_se = MLX5_WQE_CTRL_CQ_UPDATE;
335 cseg->imm = rq->umr_mkey_be;
337 ucseg->flags = MLX5_UMR_TRANSLATION_OFFSET_EN;
338 ucseg->klm_octowords =
339 cpu_to_be16(mlx5e_get_mtt_octw(MLX5_MPWRQ_PAGES_PER_WQE));
340 ucseg->bsf_octowords =
341 cpu_to_be16(mlx5e_get_mtt_octw(umr_wqe_mtt_offset));
342 ucseg->mkey_mask = cpu_to_be64(MLX5_MKEY_MASK_FREE);
344 dseg->lkey = sq->mkey_be;
345 dseg->addr = cpu_to_be64(wi->umr.mtt_addr);
348 static void mlx5e_post_umr_wqe(struct mlx5e_rq *rq, u16 ix)
350 struct mlx5e_sq *sq = &rq->channel->icosq;
351 struct mlx5_wq_cyc *wq = &sq->wq;
352 struct mlx5e_umr_wqe *wqe;
353 u8 num_wqebbs = DIV_ROUND_UP(sizeof(*wqe), MLX5_SEND_WQE_BB);
356 /* fill sq edge with nops to avoid wqe wrap around */
357 while ((pi = (sq->pc & wq->sz_m1)) > sq->edge) {
358 sq->ico_wqe_info[pi].opcode = MLX5_OPCODE_NOP;
359 sq->ico_wqe_info[pi].num_wqebbs = 1;
360 mlx5e_send_nop(sq, true);
363 wqe = mlx5_wq_cyc_get_wqe(wq, pi);
364 mlx5e_build_umr_wqe(rq, sq, wqe, ix);
365 sq->ico_wqe_info[pi].opcode = MLX5_OPCODE_UMR;
366 sq->ico_wqe_info[pi].num_wqebbs = num_wqebbs;
367 sq->pc += num_wqebbs;
368 mlx5e_tx_notify_hw(sq, &wqe->ctrl, 0);
371 static inline int mlx5e_get_wqe_mtt_sz(void)
373 /* UMR copies MTTs in units of MLX5_UMR_MTT_ALIGNMENT bytes.
374 * To avoid copying garbage after the mtt array, we allocate
377 return ALIGN(MLX5_MPWRQ_PAGES_PER_WQE * sizeof(__be64),
378 MLX5_UMR_MTT_ALIGNMENT);
381 static int mlx5e_alloc_and_map_page(struct mlx5e_rq *rq,
382 struct mlx5e_mpw_info *wi,
387 page = dev_alloc_page();
391 wi->umr.dma_info[i].page = page;
392 wi->umr.dma_info[i].addr = dma_map_page(rq->pdev, page, 0, PAGE_SIZE,
394 if (unlikely(dma_mapping_error(rq->pdev, wi->umr.dma_info[i].addr))) {
398 wi->umr.mtt[i] = cpu_to_be64(wi->umr.dma_info[i].addr | MLX5_EN_WR);
403 static int mlx5e_alloc_rx_fragmented_mpwqe(struct mlx5e_rq *rq,
404 struct mlx5e_rx_wqe *wqe,
407 struct mlx5e_mpw_info *wi = &rq->wqe_info[ix];
408 int mtt_sz = mlx5e_get_wqe_mtt_sz();
409 u32 dma_offset = mlx5e_get_wqe_mtt_offset(rq->ix, ix) << PAGE_SHIFT;
412 wi->umr.dma_info = kmalloc(sizeof(*wi->umr.dma_info) *
413 MLX5_MPWRQ_PAGES_PER_WQE,
415 if (unlikely(!wi->umr.dma_info))
418 /* We allocate more than mtt_sz as we will align the pointer */
419 wi->umr.mtt_no_align = kzalloc(mtt_sz + MLX5_UMR_ALIGN - 1,
421 if (unlikely(!wi->umr.mtt_no_align))
424 wi->umr.mtt = PTR_ALIGN(wi->umr.mtt_no_align, MLX5_UMR_ALIGN);
425 wi->umr.mtt_addr = dma_map_single(rq->pdev, wi->umr.mtt, mtt_sz,
427 if (unlikely(dma_mapping_error(rq->pdev, wi->umr.mtt_addr)))
430 for (i = 0; i < MLX5_MPWRQ_PAGES_PER_WQE; i++) {
431 if (unlikely(mlx5e_alloc_and_map_page(rq, wi, i)))
433 atomic_add(MLX5_MPWRQ_STRIDES_PER_PAGE,
434 &wi->umr.dma_info[i].page->_count);
435 wi->skbs_frags[i] = 0;
438 wi->consumed_strides = 0;
439 wi->dma_pre_sync = mlx5e_dma_pre_sync_fragmented_mpwqe;
440 wi->add_skb_frag = mlx5e_add_skb_frag_fragmented_mpwqe;
441 wi->copy_skb_header = mlx5e_copy_skb_header_fragmented_mpwqe;
442 wi->free_wqe = mlx5e_free_rx_fragmented_mpwqe;
443 wqe->data.lkey = rq->umr_mkey_be;
444 wqe->data.addr = cpu_to_be64(dma_offset);
450 dma_unmap_page(rq->pdev, wi->umr.dma_info[i].addr, PAGE_SIZE,
452 atomic_sub(MLX5_MPWRQ_STRIDES_PER_PAGE,
453 &wi->umr.dma_info[i].page->_count);
454 put_page(wi->umr.dma_info[i].page);
456 dma_unmap_single(rq->pdev, wi->umr.mtt_addr, mtt_sz, PCI_DMA_TODEVICE);
459 kfree(wi->umr.mtt_no_align);
462 kfree(wi->umr.dma_info);
468 void mlx5e_free_rx_fragmented_mpwqe(struct mlx5e_rq *rq,
469 struct mlx5e_mpw_info *wi)
471 int mtt_sz = mlx5e_get_wqe_mtt_sz();
474 for (i = 0; i < MLX5_MPWRQ_PAGES_PER_WQE; i++) {
475 dma_unmap_page(rq->pdev, wi->umr.dma_info[i].addr, PAGE_SIZE,
477 atomic_sub(MLX5_MPWRQ_STRIDES_PER_PAGE - wi->skbs_frags[i],
478 &wi->umr.dma_info[i].page->_count);
479 put_page(wi->umr.dma_info[i].page);
481 dma_unmap_single(rq->pdev, wi->umr.mtt_addr, mtt_sz, PCI_DMA_TODEVICE);
482 kfree(wi->umr.mtt_no_align);
483 kfree(wi->umr.dma_info);
486 void mlx5e_post_rx_fragmented_mpwqe(struct mlx5e_rq *rq)
488 struct mlx5_wq_ll *wq = &rq->wq;
489 struct mlx5e_rx_wqe *wqe = mlx5_wq_ll_get_wqe(wq, wq->head);
491 clear_bit(MLX5E_RQ_STATE_UMR_WQE_IN_PROGRESS, &rq->state);
492 mlx5_wq_ll_push(wq, be16_to_cpu(wqe->next.next_wqe_index));
493 rq->stats.mpwqe_frag++;
495 /* ensure wqes are visible to device before updating doorbell record */
498 mlx5_wq_ll_update_db_record(wq);
501 static int mlx5e_alloc_rx_linear_mpwqe(struct mlx5e_rq *rq,
502 struct mlx5e_rx_wqe *wqe,
505 struct mlx5e_mpw_info *wi = &rq->wqe_info[ix];
509 gfp_mask = GFP_ATOMIC | __GFP_COLD | __GFP_MEMALLOC;
510 wi->dma_info.page = alloc_pages_node(NUMA_NO_NODE, gfp_mask,
511 MLX5_MPWRQ_WQE_PAGE_ORDER);
512 if (unlikely(!wi->dma_info.page))
515 wi->dma_info.addr = dma_map_page(rq->pdev, wi->dma_info.page, 0,
516 rq->wqe_sz, PCI_DMA_FROMDEVICE);
517 if (unlikely(dma_mapping_error(rq->pdev, wi->dma_info.addr))) {
518 put_page(wi->dma_info.page);
522 /* We split the high-order page into order-0 ones and manage their
523 * reference counter to minimize the memory held by small skb fragments
525 split_page(wi->dma_info.page, MLX5_MPWRQ_WQE_PAGE_ORDER);
526 for (i = 0; i < MLX5_MPWRQ_PAGES_PER_WQE; i++) {
527 atomic_add(MLX5_MPWRQ_STRIDES_PER_PAGE,
528 &wi->dma_info.page[i]._count);
529 wi->skbs_frags[i] = 0;
532 wi->consumed_strides = 0;
533 wi->dma_pre_sync = mlx5e_dma_pre_sync_linear_mpwqe;
534 wi->add_skb_frag = mlx5e_add_skb_frag_linear_mpwqe;
535 wi->copy_skb_header = mlx5e_copy_skb_header_linear_mpwqe;
536 wi->free_wqe = mlx5e_free_rx_linear_mpwqe;
537 wqe->data.lkey = rq->mkey_be;
538 wqe->data.addr = cpu_to_be64(wi->dma_info.addr);
543 void mlx5e_free_rx_linear_mpwqe(struct mlx5e_rq *rq,
544 struct mlx5e_mpw_info *wi)
548 dma_unmap_page(rq->pdev, wi->dma_info.addr, rq->wqe_sz,
550 for (i = 0; i < MLX5_MPWRQ_PAGES_PER_WQE; i++) {
551 atomic_sub(MLX5_MPWRQ_STRIDES_PER_PAGE - wi->skbs_frags[i],
552 &wi->dma_info.page[i]._count);
553 put_page(&wi->dma_info.page[i]);
557 int mlx5e_alloc_rx_mpwqe(struct mlx5e_rq *rq, struct mlx5e_rx_wqe *wqe, u16 ix)
561 err = mlx5e_alloc_rx_linear_mpwqe(rq, wqe, ix);
563 err = mlx5e_alloc_rx_fragmented_mpwqe(rq, wqe, ix);
566 set_bit(MLX5E_RQ_STATE_UMR_WQE_IN_PROGRESS, &rq->state);
567 mlx5e_post_umr_wqe(rq, ix);
574 #define RQ_CANNOT_POST(rq) \
575 (!test_bit(MLX5E_RQ_STATE_POST_WQES_ENABLE, &rq->state) || \
576 test_bit(MLX5E_RQ_STATE_UMR_WQE_IN_PROGRESS, &rq->state))
578 bool mlx5e_post_rx_wqes(struct mlx5e_rq *rq)
580 struct mlx5_wq_ll *wq = &rq->wq;
582 if (unlikely(RQ_CANNOT_POST(rq)))
585 while (!mlx5_wq_ll_is_full(wq)) {
586 struct mlx5e_rx_wqe *wqe = mlx5_wq_ll_get_wqe(wq, wq->head);
589 err = rq->alloc_wqe(rq, wqe, wq->head);
592 rq->stats.buff_alloc_err++;
596 mlx5_wq_ll_push(wq, be16_to_cpu(wqe->next.next_wqe_index));
599 /* ensure wqes are visible to device before updating doorbell record */
602 mlx5_wq_ll_update_db_record(wq);
604 return !mlx5_wq_ll_is_full(wq);
607 static void mlx5e_lro_update_hdr(struct sk_buff *skb, struct mlx5_cqe64 *cqe,
610 struct ethhdr *eth = (struct ethhdr *)(skb->data);
611 struct iphdr *ipv4 = (struct iphdr *)(skb->data + ETH_HLEN);
612 struct ipv6hdr *ipv6 = (struct ipv6hdr *)(skb->data + ETH_HLEN);
615 u8 l4_hdr_type = get_cqe_l4_hdr_type(cqe);
616 int tcp_ack = ((CQE_L4_HDR_TYPE_TCP_ACK_NO_DATA == l4_hdr_type) ||
617 (CQE_L4_HDR_TYPE_TCP_ACK_AND_DATA == l4_hdr_type));
619 u16 tot_len = cqe_bcnt - ETH_HLEN;
621 if (eth->h_proto == htons(ETH_P_IP)) {
622 tcp = (struct tcphdr *)(skb->data + ETH_HLEN +
623 sizeof(struct iphdr));
625 skb_shinfo(skb)->gso_type = SKB_GSO_TCPV4;
627 tcp = (struct tcphdr *)(skb->data + ETH_HLEN +
628 sizeof(struct ipv6hdr));
630 skb_shinfo(skb)->gso_type = SKB_GSO_TCPV6;
633 if (get_cqe_lro_tcppsh(cqe))
638 tcp->ack_seq = cqe->lro_ack_seq_num;
639 tcp->window = cqe->lro_tcp_win;
643 ipv4->ttl = cqe->lro_min_ttl;
644 ipv4->tot_len = cpu_to_be16(tot_len);
646 ipv4->check = ip_fast_csum((unsigned char *)ipv4,
649 ipv6->hop_limit = cqe->lro_min_ttl;
650 ipv6->payload_len = cpu_to_be16(tot_len -
651 sizeof(struct ipv6hdr));
655 static inline void mlx5e_skb_set_hash(struct mlx5_cqe64 *cqe,
658 u8 cht = cqe->rss_hash_type;
659 int ht = (cht & CQE_RSS_HTYPE_L4) ? PKT_HASH_TYPE_L4 :
660 (cht & CQE_RSS_HTYPE_IP) ? PKT_HASH_TYPE_L3 :
662 skb_set_hash(skb, be32_to_cpu(cqe->rss_hash_result), ht);
665 static inline bool is_first_ethertype_ip(struct sk_buff *skb)
667 __be16 ethertype = ((struct ethhdr *)skb->data)->h_proto;
669 return (ethertype == htons(ETH_P_IP) || ethertype == htons(ETH_P_IPV6));
672 static inline void mlx5e_handle_csum(struct net_device *netdev,
673 struct mlx5_cqe64 *cqe,
678 if (unlikely(!(netdev->features & NETIF_F_RXCSUM)))
682 skb->ip_summed = CHECKSUM_UNNECESSARY;
686 if (is_first_ethertype_ip(skb)) {
687 skb->ip_summed = CHECKSUM_COMPLETE;
688 skb->csum = csum_unfold((__force __sum16)cqe->check_sum);
693 if (likely((cqe->hds_ip_ext & CQE_L3_OK) &&
694 (cqe->hds_ip_ext & CQE_L4_OK))) {
695 skb->ip_summed = CHECKSUM_UNNECESSARY;
696 if (cqe_is_tunneled(cqe)) {
698 skb->encapsulation = 1;
699 rq->stats.csum_inner++;
704 skb->ip_summed = CHECKSUM_NONE;
705 rq->stats.csum_none++;
708 static inline void mlx5e_build_rx_skb(struct mlx5_cqe64 *cqe,
713 struct net_device *netdev = rq->netdev;
714 struct mlx5e_tstamp *tstamp = rq->tstamp;
717 lro_num_seg = be32_to_cpu(cqe->srqn) >> 24;
718 if (lro_num_seg > 1) {
719 mlx5e_lro_update_hdr(skb, cqe, cqe_bcnt);
720 skb_shinfo(skb)->gso_size = DIV_ROUND_UP(cqe_bcnt, lro_num_seg);
721 rq->stats.lro_packets++;
722 rq->stats.lro_bytes += cqe_bcnt;
725 if (unlikely(mlx5e_rx_hw_stamp(tstamp)))
726 mlx5e_fill_hwstamp(tstamp, get_cqe_ts(cqe), skb_hwtstamps(skb));
728 skb_record_rx_queue(skb, rq->ix);
730 if (likely(netdev->features & NETIF_F_RXHASH))
731 mlx5e_skb_set_hash(cqe, skb);
733 if (cqe_has_vlan(cqe))
734 __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q),
735 be16_to_cpu(cqe->vlan_info));
737 skb->mark = be32_to_cpu(cqe->sop_drop_qpn) & MLX5E_TC_FLOW_ID_MASK;
739 mlx5e_handle_csum(netdev, cqe, rq, skb, !!lro_num_seg);
740 skb->protocol = eth_type_trans(skb, netdev);
743 static inline void mlx5e_complete_rx_cqe(struct mlx5e_rq *rq,
744 struct mlx5_cqe64 *cqe,
749 rq->stats.bytes += cqe_bcnt;
750 mlx5e_build_rx_skb(cqe, cqe_bcnt, rq, skb);
751 napi_gro_receive(rq->cq.napi, skb);
754 void mlx5e_handle_rx_cqe(struct mlx5e_rq *rq, struct mlx5_cqe64 *cqe)
756 struct mlx5e_rx_wqe *wqe;
758 __be16 wqe_counter_be;
762 wqe_counter_be = cqe->wqe_counter;
763 wqe_counter = be16_to_cpu(wqe_counter_be);
764 wqe = mlx5_wq_ll_get_wqe(&rq->wq, wqe_counter);
765 skb = rq->skb[wqe_counter];
767 rq->skb[wqe_counter] = NULL;
769 dma_unmap_single(rq->pdev,
770 *((dma_addr_t *)skb->cb),
774 if (unlikely((cqe->op_own >> 4) != MLX5_CQE_RESP_SEND)) {
780 cqe_bcnt = be32_to_cpu(cqe->byte_cnt);
781 skb_put(skb, cqe_bcnt);
783 mlx5e_complete_rx_cqe(rq, cqe, cqe_bcnt, skb);
786 mlx5_wq_ll_pop(&rq->wq, wqe_counter_be,
787 &wqe->next.next_wqe_index);
790 static inline void mlx5e_mpwqe_fill_rx_skb(struct mlx5e_rq *rq,
791 struct mlx5_cqe64 *cqe,
792 struct mlx5e_mpw_info *wi,
796 u32 consumed_bytes = ALIGN(cqe_bcnt, MLX5_MPWRQ_STRIDE_SIZE);
797 u16 stride_ix = mpwrq_get_cqe_stride_index(cqe);
798 u32 wqe_offset = stride_ix * MLX5_MPWRQ_STRIDE_SIZE;
799 u32 head_offset = wqe_offset & (PAGE_SIZE - 1);
800 u32 page_idx = wqe_offset >> PAGE_SHIFT;
801 u32 head_page_idx = page_idx;
802 u16 headlen = min_t(u16, MLX5_MPWRQ_SMALL_PACKET_THRESHOLD, cqe_bcnt);
803 u32 frag_offset = head_offset + headlen;
804 u16 byte_cnt = cqe_bcnt - headlen;
806 #if (MLX5_MPWRQ_SMALL_PACKET_THRESHOLD >= MLX5_MPWRQ_STRIDE_SIZE)
807 if (unlikely(frag_offset >= PAGE_SIZE)) {
809 frag_offset -= PAGE_SIZE;
812 wi->dma_pre_sync(rq->pdev, wi, wqe_offset, consumed_bytes);
815 u32 pg_consumed_bytes =
816 min_t(u32, PAGE_SIZE - frag_offset, byte_cnt);
818 wi->add_skb_frag(rq->pdev, skb, wi, page_idx, frag_offset,
820 byte_cnt -= pg_consumed_bytes;
825 wi->copy_skb_header(rq->pdev, skb, wi, head_page_idx, head_offset,
827 /* skb linear part was allocated with headlen and aligned to long */
828 skb->tail += headlen;
832 void mlx5e_handle_rx_cqe_mpwrq(struct mlx5e_rq *rq, struct mlx5_cqe64 *cqe)
834 u16 cstrides = mpwrq_get_cqe_consumed_strides(cqe);
835 u16 wqe_id = be16_to_cpu(cqe->wqe_id);
836 struct mlx5e_mpw_info *wi = &rq->wqe_info[wqe_id];
837 struct mlx5e_rx_wqe *wqe = mlx5_wq_ll_get_wqe(&rq->wq, wqe_id);
841 wi->consumed_strides += cstrides;
843 if (unlikely((cqe->op_own >> 4) != MLX5_CQE_RESP_SEND)) {
848 if (unlikely(mpwrq_is_filler_cqe(cqe))) {
849 rq->stats.mpwqe_filler++;
853 skb = napi_alloc_skb(rq->cq.napi,
854 ALIGN(MLX5_MPWRQ_SMALL_PACKET_THRESHOLD,
856 if (unlikely(!skb)) {
857 rq->stats.buff_alloc_err++;
862 cqe_bcnt = mpwrq_get_cqe_byte_cnt(cqe);
864 mlx5e_mpwqe_fill_rx_skb(rq, cqe, wi, cqe_bcnt, skb);
865 mlx5e_complete_rx_cqe(rq, cqe, cqe_bcnt, skb);
868 if (likely(wi->consumed_strides < MLX5_MPWRQ_NUM_STRIDES))
871 wi->free_wqe(rq, wi);
872 mlx5_wq_ll_pop(&rq->wq, cqe->wqe_id, &wqe->next.next_wqe_index);
875 int mlx5e_poll_rx_cq(struct mlx5e_cq *cq, int budget)
877 struct mlx5e_rq *rq = container_of(cq, struct mlx5e_rq, cq);
880 if (cq->decmprs_left)
881 work_done += mlx5e_decompress_cqes_cont(rq, cq, 0, budget);
883 for (; work_done < budget; work_done++) {
884 struct mlx5_cqe64 *cqe = mlx5e_get_cqe(cq);
889 if (mlx5_get_cqe_format(cqe) == MLX5_COMPRESSED) {
891 mlx5e_decompress_cqes_start(rq, cq,
896 mlx5_cqwq_pop(&cq->wq);
898 rq->handle_rx_cqe(rq, cqe);
901 mlx5_cqwq_update_db_record(&cq->wq);
903 /* ensure cq space is freed before enabling more cqes */