2 * Copyright (c) 2007 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 <net/busy_poll.h>
35 #include <linux/bpf.h>
36 #include <linux/bpf_trace.h>
37 #include <linux/mlx4/cq.h>
38 #include <linux/slab.h>
39 #include <linux/mlx4/qp.h>
40 #include <linux/skbuff.h>
41 #include <linux/rculist.h>
42 #include <linux/if_ether.h>
43 #include <linux/if_vlan.h>
44 #include <linux/vmalloc.h>
45 #include <linux/irq.h>
47 #if IS_ENABLED(CONFIG_IPV6)
48 #include <net/ip6_checksum.h>
53 static int mlx4_alloc_page(struct mlx4_en_priv *priv,
54 struct mlx4_en_rx_alloc *frag,
60 page = alloc_page(gfp);
63 dma = dma_map_page(priv->ddev, page, 0, PAGE_SIZE, priv->dma_dir);
64 if (unlikely(dma_mapping_error(priv->ddev, dma))) {
70 frag->page_offset = priv->rx_headroom;
74 static int mlx4_en_alloc_frags(struct mlx4_en_priv *priv,
75 struct mlx4_en_rx_ring *ring,
76 struct mlx4_en_rx_desc *rx_desc,
77 struct mlx4_en_rx_alloc *frags,
82 for (i = 0; i < priv->num_frags; i++, frags++) {
84 if (mlx4_alloc_page(priv, frags, gfp))
86 ring->rx_alloc_pages++;
88 rx_desc->data[i].addr = cpu_to_be64(frags->dma +
94 static void mlx4_en_free_frag(const struct mlx4_en_priv *priv,
95 struct mlx4_en_rx_alloc *frag)
98 dma_unmap_page(priv->ddev, frag->dma,
99 PAGE_SIZE, priv->dma_dir);
100 __free_page(frag->page);
102 /* We need to clear all fields, otherwise a change of priv->log_rx_info
103 * could lead to see garbage later in frag->page.
105 memset(frag, 0, sizeof(*frag));
108 static void mlx4_en_init_rx_desc(const struct mlx4_en_priv *priv,
109 struct mlx4_en_rx_ring *ring, int index)
111 struct mlx4_en_rx_desc *rx_desc = ring->buf + ring->stride * index;
115 /* Set size and memtype fields */
116 for (i = 0; i < priv->num_frags; i++) {
117 rx_desc->data[i].byte_count =
118 cpu_to_be32(priv->frag_info[i].frag_size);
119 rx_desc->data[i].lkey = cpu_to_be32(priv->mdev->mr.key);
122 /* If the number of used fragments does not fill up the ring stride,
123 * remaining (unused) fragments must be padded with null address/size
124 * and a special memory key */
125 possible_frags = (ring->stride - sizeof(struct mlx4_en_rx_desc)) / DS_SIZE;
126 for (i = priv->num_frags; i < possible_frags; i++) {
127 rx_desc->data[i].byte_count = 0;
128 rx_desc->data[i].lkey = cpu_to_be32(MLX4_EN_MEMTYPE_PAD);
129 rx_desc->data[i].addr = 0;
133 static int mlx4_en_prepare_rx_desc(struct mlx4_en_priv *priv,
134 struct mlx4_en_rx_ring *ring, int index,
137 struct mlx4_en_rx_desc *rx_desc = ring->buf +
138 (index << ring->log_stride);
139 struct mlx4_en_rx_alloc *frags = ring->rx_info +
140 (index << priv->log_rx_info);
141 if (likely(ring->page_cache.index > 0)) {
142 /* XDP uses a single page per frame */
144 ring->page_cache.index--;
145 frags->page = ring->page_cache.buf[ring->page_cache.index].page;
146 frags->dma = ring->page_cache.buf[ring->page_cache.index].dma;
148 frags->page_offset = XDP_PACKET_HEADROOM;
149 rx_desc->data[0].addr = cpu_to_be64(frags->dma +
150 XDP_PACKET_HEADROOM);
154 return mlx4_en_alloc_frags(priv, ring, rx_desc, frags, gfp);
157 static bool mlx4_en_is_ring_empty(const struct mlx4_en_rx_ring *ring)
159 return ring->prod == ring->cons;
162 static inline void mlx4_en_update_rx_prod_db(struct mlx4_en_rx_ring *ring)
164 *ring->wqres.db.db = cpu_to_be32(ring->prod & 0xffff);
168 static void mlx4_en_free_rx_desc(const struct mlx4_en_priv *priv,
169 struct mlx4_en_rx_ring *ring,
172 struct mlx4_en_rx_alloc *frags;
175 frags = ring->rx_info + (index << priv->log_rx_info);
176 for (nr = 0; nr < priv->num_frags; nr++) {
177 en_dbg(DRV, priv, "Freeing fragment:%d\n", nr);
178 mlx4_en_free_frag(priv, frags + nr);
182 /* Function not in fast-path */
183 static int mlx4_en_fill_rx_buffers(struct mlx4_en_priv *priv)
185 struct mlx4_en_rx_ring *ring;
190 for (buf_ind = 0; buf_ind < priv->prof->rx_ring_size; buf_ind++) {
191 for (ring_ind = 0; ring_ind < priv->rx_ring_num; ring_ind++) {
192 ring = priv->rx_ring[ring_ind];
194 if (mlx4_en_prepare_rx_desc(priv, ring,
196 GFP_KERNEL | __GFP_COLD)) {
197 if (ring->actual_size < MLX4_EN_MIN_RX_SIZE) {
198 en_err(priv, "Failed to allocate enough rx buffers\n");
201 new_size = rounddown_pow_of_two(ring->actual_size);
202 en_warn(priv, "Only %d buffers allocated reducing ring size to %d\n",
203 ring->actual_size, new_size);
214 for (ring_ind = 0; ring_ind < priv->rx_ring_num; ring_ind++) {
215 ring = priv->rx_ring[ring_ind];
216 while (ring->actual_size > new_size) {
219 mlx4_en_free_rx_desc(priv, ring, ring->actual_size);
226 static void mlx4_en_free_rx_buf(struct mlx4_en_priv *priv,
227 struct mlx4_en_rx_ring *ring)
231 en_dbg(DRV, priv, "Freeing Rx buf - cons:%d prod:%d\n",
232 ring->cons, ring->prod);
234 /* Unmap and free Rx buffers */
235 for (index = 0; index < ring->size; index++) {
236 en_dbg(DRV, priv, "Processing descriptor:%d\n", index);
237 mlx4_en_free_rx_desc(priv, ring, index);
243 void mlx4_en_set_num_rx_rings(struct mlx4_en_dev *mdev)
248 struct mlx4_dev *dev = mdev->dev;
250 mlx4_foreach_port(i, dev, MLX4_PORT_TYPE_ETH) {
251 num_of_eqs = max_t(int, MIN_RX_RINGS,
253 mlx4_get_eqs_per_port(mdev->dev, i),
256 num_rx_rings = mlx4_low_memory_profile() ? MIN_RX_RINGS :
257 min_t(int, num_of_eqs,
258 netif_get_num_default_rss_queues());
259 mdev->profile.prof[i].rx_ring_num =
260 rounddown_pow_of_two(num_rx_rings);
264 int mlx4_en_create_rx_ring(struct mlx4_en_priv *priv,
265 struct mlx4_en_rx_ring **pring,
266 u32 size, u16 stride, int node)
268 struct mlx4_en_dev *mdev = priv->mdev;
269 struct mlx4_en_rx_ring *ring;
273 ring = kzalloc_node(sizeof(*ring), GFP_KERNEL, node);
275 ring = kzalloc(sizeof(*ring), GFP_KERNEL);
277 en_err(priv, "Failed to allocate RX ring structure\n");
285 ring->size_mask = size - 1;
286 ring->stride = stride;
287 ring->log_stride = ffs(ring->stride) - 1;
288 ring->buf_size = ring->size * ring->stride + TXBB_SIZE;
290 tmp = size * roundup_pow_of_two(MLX4_EN_MAX_RX_FRAGS *
291 sizeof(struct mlx4_en_rx_alloc));
292 ring->rx_info = vzalloc_node(tmp, node);
293 if (!ring->rx_info) {
294 ring->rx_info = vzalloc(tmp);
295 if (!ring->rx_info) {
301 en_dbg(DRV, priv, "Allocated rx_info ring at addr:%p size:%d\n",
304 /* Allocate HW buffers on provided NUMA node */
305 set_dev_node(&mdev->dev->persist->pdev->dev, node);
306 err = mlx4_alloc_hwq_res(mdev->dev, &ring->wqres, ring->buf_size);
307 set_dev_node(&mdev->dev->persist->pdev->dev, mdev->dev->numa_node);
311 ring->buf = ring->wqres.buf.direct.buf;
313 ring->hwtstamp_rx_filter = priv->hwtstamp_config.rx_filter;
319 vfree(ring->rx_info);
320 ring->rx_info = NULL;
328 int mlx4_en_activate_rx_rings(struct mlx4_en_priv *priv)
330 struct mlx4_en_rx_ring *ring;
334 int stride = roundup_pow_of_two(sizeof(struct mlx4_en_rx_desc) +
335 DS_SIZE * priv->num_frags);
337 for (ring_ind = 0; ring_ind < priv->rx_ring_num; ring_ind++) {
338 ring = priv->rx_ring[ring_ind];
342 ring->actual_size = 0;
343 ring->cqn = priv->rx_cq[ring_ind]->mcq.cqn;
345 ring->stride = stride;
346 if (ring->stride <= TXBB_SIZE) {
347 /* Stamp first unused send wqe */
348 __be32 *ptr = (__be32 *)ring->buf;
349 __be32 stamp = cpu_to_be32(1 << STAMP_SHIFT);
351 /* Move pointer to start of rx section */
352 ring->buf += TXBB_SIZE;
355 ring->log_stride = ffs(ring->stride) - 1;
356 ring->buf_size = ring->size * ring->stride;
358 memset(ring->buf, 0, ring->buf_size);
359 mlx4_en_update_rx_prod_db(ring);
361 /* Initialize all descriptors */
362 for (i = 0; i < ring->size; i++)
363 mlx4_en_init_rx_desc(priv, ring, i);
365 err = mlx4_en_fill_rx_buffers(priv);
369 for (ring_ind = 0; ring_ind < priv->rx_ring_num; ring_ind++) {
370 ring = priv->rx_ring[ring_ind];
372 ring->size_mask = ring->actual_size - 1;
373 mlx4_en_update_rx_prod_db(ring);
379 for (ring_ind = 0; ring_ind < priv->rx_ring_num; ring_ind++)
380 mlx4_en_free_rx_buf(priv, priv->rx_ring[ring_ind]);
382 ring_ind = priv->rx_ring_num - 1;
383 while (ring_ind >= 0) {
384 if (priv->rx_ring[ring_ind]->stride <= TXBB_SIZE)
385 priv->rx_ring[ring_ind]->buf -= TXBB_SIZE;
391 /* We recover from out of memory by scheduling our napi poll
392 * function (mlx4_en_process_cq), which tries to allocate
393 * all missing RX buffers (call to mlx4_en_refill_rx_buffers).
395 void mlx4_en_recover_from_oom(struct mlx4_en_priv *priv)
402 for (ring = 0; ring < priv->rx_ring_num; ring++) {
403 if (mlx4_en_is_ring_empty(priv->rx_ring[ring])) {
405 napi_reschedule(&priv->rx_cq[ring]->napi);
411 /* When the rx ring is running in page-per-packet mode, a released frame can go
412 * directly into a small cache, to avoid unmapping or touching the page
413 * allocator. In bpf prog performance scenarios, buffers are either forwarded
414 * or dropped, never converted to skbs, so every page can come directly from
415 * this cache when it is sized to be a multiple of the napi budget.
417 bool mlx4_en_rx_recycle(struct mlx4_en_rx_ring *ring,
418 struct mlx4_en_rx_alloc *frame)
420 struct mlx4_en_page_cache *cache = &ring->page_cache;
422 if (cache->index >= MLX4_EN_CACHE_SIZE)
425 cache->buf[cache->index].page = frame->page;
426 cache->buf[cache->index].dma = frame->dma;
431 void mlx4_en_destroy_rx_ring(struct mlx4_en_priv *priv,
432 struct mlx4_en_rx_ring **pring,
433 u32 size, u16 stride)
435 struct mlx4_en_dev *mdev = priv->mdev;
436 struct mlx4_en_rx_ring *ring = *pring;
437 struct bpf_prog *old_prog;
439 old_prog = rcu_dereference_protected(
441 lockdep_is_held(&mdev->state_lock));
443 bpf_prog_put(old_prog);
444 mlx4_free_hwq_res(mdev->dev, &ring->wqres, size * stride + TXBB_SIZE);
445 vfree(ring->rx_info);
446 ring->rx_info = NULL;
451 void mlx4_en_deactivate_rx_ring(struct mlx4_en_priv *priv,
452 struct mlx4_en_rx_ring *ring)
456 for (i = 0; i < ring->page_cache.index; i++) {
457 dma_unmap_page(priv->ddev, ring->page_cache.buf[i].dma,
458 PAGE_SIZE, priv->dma_dir);
459 put_page(ring->page_cache.buf[i].page);
461 ring->page_cache.index = 0;
462 mlx4_en_free_rx_buf(priv, ring);
463 if (ring->stride <= TXBB_SIZE)
464 ring->buf -= TXBB_SIZE;
468 static int mlx4_en_complete_rx_desc(struct mlx4_en_priv *priv,
469 struct mlx4_en_rx_alloc *frags,
473 const struct mlx4_en_frag_info *frag_info = priv->frag_info;
474 unsigned int truesize = 0;
480 /* Collect used fragments while replacing them in the HW descriptors */
481 for (nr = 0;; frags++) {
482 frag_size = min_t(int, length, frag_info->frag_size);
489 dma_sync_single_range_for_cpu(priv->ddev, dma, frags->page_offset,
490 frag_size, priv->dma_dir);
492 __skb_fill_page_desc(skb, nr, page, frags->page_offset,
495 truesize += frag_info->frag_stride;
496 if (frag_info->frag_stride == PAGE_SIZE / 2) {
497 frags->page_offset ^= PAGE_SIZE / 2;
498 release = page_count(page) != 1 ||
499 page_is_pfmemalloc(page) ||
500 page_to_nid(page) != numa_mem_id();
501 } else if (!priv->rx_headroom) {
502 /* rx_headroom for non XDP setup is always 0.
503 * When XDP is set, the above condition will
504 * guarantee page is always released.
506 u32 sz_align = ALIGN(frag_size, SMP_CACHE_BYTES);
508 frags->page_offset += sz_align;
509 release = frags->page_offset + frag_info->frag_size > PAGE_SIZE;
512 dma_unmap_page(priv->ddev, dma, PAGE_SIZE, priv->dma_dir);
524 skb->truesize += truesize;
530 __skb_frag_unref(skb_shinfo(skb)->frags + nr);
535 static void validate_loopback(struct mlx4_en_priv *priv, void *va)
537 const unsigned char *data = va + ETH_HLEN;
540 for (i = 0; i < MLX4_LOOPBACK_TEST_PAYLOAD; i++) {
541 if (data[i] != (unsigned char)i)
545 priv->loopback_ok = 1;
548 static void mlx4_en_refill_rx_buffers(struct mlx4_en_priv *priv,
549 struct mlx4_en_rx_ring *ring)
551 u32 missing = ring->actual_size - (ring->prod - ring->cons);
553 /* Try to batch allocations, but not too much. */
557 if (mlx4_en_prepare_rx_desc(priv, ring,
558 ring->prod & ring->size_mask,
559 GFP_ATOMIC | __GFP_COLD |
563 } while (likely(--missing));
565 mlx4_en_update_rx_prod_db(ring);
568 /* When hardware doesn't strip the vlan, we need to calculate the checksum
569 * over it and add it to the hardware's checksum calculation
571 static inline __wsum get_fixed_vlan_csum(__wsum hw_checksum,
572 struct vlan_hdr *vlanh)
574 return csum_add(hw_checksum, *(__wsum *)vlanh);
577 /* Although the stack expects checksum which doesn't include the pseudo
578 * header, the HW adds it. To address that, we are subtracting the pseudo
579 * header checksum from the checksum value provided by the HW.
581 static int get_fixed_ipv4_csum(__wsum hw_checksum, struct sk_buff *skb,
584 __u16 length_for_csum = 0;
585 __wsum csum_pseudo_header = 0;
586 __u8 ipproto = iph->protocol;
588 if (unlikely(ipproto == IPPROTO_SCTP))
591 length_for_csum = (be16_to_cpu(iph->tot_len) - (iph->ihl << 2));
592 csum_pseudo_header = csum_tcpudp_nofold(iph->saddr, iph->daddr,
593 length_for_csum, ipproto, 0);
594 skb->csum = csum_sub(hw_checksum, csum_pseudo_header);
598 #if IS_ENABLED(CONFIG_IPV6)
599 /* In IPv6 packets, besides subtracting the pseudo header checksum,
600 * we also compute/add the IP header checksum which
601 * is not added by the HW.
603 static int get_fixed_ipv6_csum(__wsum hw_checksum, struct sk_buff *skb,
604 struct ipv6hdr *ipv6h)
606 __u8 nexthdr = ipv6h->nexthdr;
607 __wsum csum_pseudo_hdr = 0;
609 if (unlikely(nexthdr == IPPROTO_FRAGMENT ||
610 nexthdr == IPPROTO_HOPOPTS ||
611 nexthdr == IPPROTO_SCTP))
613 hw_checksum = csum_add(hw_checksum, (__force __wsum)htons(nexthdr));
615 csum_pseudo_hdr = csum_partial(&ipv6h->saddr,
616 sizeof(ipv6h->saddr) + sizeof(ipv6h->daddr), 0);
617 csum_pseudo_hdr = csum_add(csum_pseudo_hdr, (__force __wsum)ipv6h->payload_len);
618 csum_pseudo_hdr = csum_add(csum_pseudo_hdr,
619 (__force __wsum)htons(nexthdr));
621 skb->csum = csum_sub(hw_checksum, csum_pseudo_hdr);
622 skb->csum = csum_add(skb->csum, csum_partial(ipv6h, sizeof(struct ipv6hdr), 0));
627 #define short_frame(size) ((size) <= ETH_ZLEN + ETH_FCS_LEN)
629 static int check_csum(struct mlx4_cqe *cqe, struct sk_buff *skb, void *va,
630 netdev_features_t dev_features)
632 __wsum hw_checksum = 0;
635 /* CQE csum doesn't cover padding octets in short ethernet
636 * frames. And the pad field is appended prior to calculating
637 * and appending the FCS field.
639 * Detecting these padded frames requires to verify and parse
640 * IP headers, so we simply force all those small frames to skip
643 if (short_frame(skb->len))
646 hdr = (u8 *)va + sizeof(struct ethhdr);
647 hw_checksum = csum_unfold((__force __sum16)cqe->checksum);
649 if (cqe->vlan_my_qpn & cpu_to_be32(MLX4_CQE_CVLAN_PRESENT_MASK) &&
650 !(dev_features & NETIF_F_HW_VLAN_CTAG_RX)) {
651 hw_checksum = get_fixed_vlan_csum(hw_checksum, hdr);
652 hdr += sizeof(struct vlan_hdr);
655 if (cqe->status & cpu_to_be16(MLX4_CQE_STATUS_IPV4))
656 return get_fixed_ipv4_csum(hw_checksum, skb, hdr);
657 #if IS_ENABLED(CONFIG_IPV6)
658 if (cqe->status & cpu_to_be16(MLX4_CQE_STATUS_IPV6))
659 return get_fixed_ipv6_csum(hw_checksum, skb, hdr);
664 int mlx4_en_process_rx_cq(struct net_device *dev, struct mlx4_en_cq *cq, int budget)
666 struct mlx4_en_priv *priv = netdev_priv(dev);
667 int factor = priv->cqe_factor;
668 struct mlx4_en_rx_ring *ring;
669 struct bpf_prog *xdp_prog;
670 int cq_ring = cq->ring;
671 bool doorbell_pending;
672 struct mlx4_cqe *cqe;
676 if (unlikely(!priv->port_up))
679 if (unlikely(budget <= 0))
682 ring = priv->rx_ring[cq_ring];
684 /* Protect accesses to: ring->xdp_prog, priv->mac_hash list */
686 xdp_prog = rcu_dereference(ring->xdp_prog);
687 doorbell_pending = 0;
689 /* We assume a 1:1 mapping between CQEs and Rx descriptors, so Rx
690 * descriptor offset can be deduced from the CQE index instead of
691 * reading 'cqe->index' */
692 index = cq->mcq.cons_index & ring->size_mask;
693 cqe = mlx4_en_get_cqe(cq->buf, index, priv->cqe_size) + factor;
695 /* Process all completed CQEs */
696 while (XNOR(cqe->owner_sr_opcode & MLX4_CQE_OWNER_MASK,
697 cq->mcq.cons_index & cq->size)) {
698 struct mlx4_en_rx_alloc *frags;
699 enum pkt_hash_types hash_type;
706 frags = ring->rx_info + (index << priv->log_rx_info);
707 va = page_address(frags[0].page) + frags[0].page_offset;
710 * make sure we read the CQE after we read the ownership bit
714 /* Drop packet on bad receive or bad checksum */
715 if (unlikely((cqe->owner_sr_opcode & MLX4_CQE_OPCODE_MASK) ==
716 MLX4_CQE_OPCODE_ERROR)) {
717 en_err(priv, "CQE completed in error - vendor syndrom:%d syndrom:%d\n",
718 ((struct mlx4_err_cqe *)cqe)->vendor_err_syndrome,
719 ((struct mlx4_err_cqe *)cqe)->syndrome);
722 if (unlikely(cqe->badfcs_enc & MLX4_CQE_BAD_FCS)) {
723 en_dbg(RX_ERR, priv, "Accepted frame with bad FCS\n");
727 /* Check if we need to drop the packet if SRIOV is not enabled
728 * and not performing the selftest or flb disabled
730 if (priv->flags & MLX4_EN_FLAG_RX_FILTER_NEEDED) {
731 const struct ethhdr *ethh = va;
733 /* Get pointer to first fragment since we haven't
734 * skb yet and cast it to ethhdr struct
736 dma = frags[0].dma + frags[0].page_offset;
737 dma_sync_single_for_cpu(priv->ddev, dma, sizeof(*ethh),
740 if (is_multicast_ether_addr(ethh->h_dest)) {
741 struct mlx4_mac_entry *entry;
742 struct hlist_head *bucket;
743 unsigned int mac_hash;
745 /* Drop the packet, since HW loopback-ed it */
746 mac_hash = ethh->h_source[MLX4_EN_MAC_HASH_IDX];
747 bucket = &priv->mac_hash[mac_hash];
748 hlist_for_each_entry_rcu(entry, bucket, hlist) {
749 if (ether_addr_equal_64bits(entry->mac,
756 if (unlikely(priv->validate_loopback)) {
757 validate_loopback(priv, va);
762 * Packet is OK - process it.
764 length = be32_to_cpu(cqe->byte_cnt);
765 length -= ring->fcs_del;
767 /* A bpf program gets first chance to drop the packet. It may
768 * read bytes but not past the end of the frag.
776 dma = frags[0].dma + frags[0].page_offset;
777 dma_sync_single_for_cpu(priv->ddev, dma,
778 priv->frag_info[0].frag_size,
781 xdp.data_hard_start = va - frags[0].page_offset;
783 xdp.data_end = xdp.data + length;
784 orig_data = xdp.data;
786 act = bpf_prog_run_xdp(xdp_prog, &xdp);
788 if (xdp.data != orig_data) {
789 length = xdp.data_end - xdp.data;
790 frags[0].page_offset = xdp.data -
799 if (likely(!mlx4_en_xmit_frame(ring, frags, dev,
801 &doorbell_pending))) {
802 frags[0].page = NULL;
805 trace_xdp_exception(dev, xdp_prog, act);
806 goto xdp_drop_no_cnt; /* Drop on xmit failure */
808 bpf_warn_invalid_xdp_action(act);
810 trace_xdp_exception(dev, xdp_prog, act);
818 ring->bytes += length;
821 skb = napi_get_frags(&cq->napi);
825 if (unlikely(ring->hwtstamp_rx_filter == HWTSTAMP_FILTER_ALL)) {
826 u64 timestamp = mlx4_en_get_cqe_ts(cqe);
828 mlx4_en_fill_hwtstamps(priv->mdev, skb_hwtstamps(skb),
831 skb_record_rx_queue(skb, cq_ring);
833 if (likely(dev->features & NETIF_F_RXCSUM)) {
834 /* TODO: For IP non TCP/UDP packets when csum complete is
835 * not an option (not supported or any other reason) we can
836 * actually check cqe IPOK status bit and report
837 * CHECKSUM_UNNECESSARY rather than CHECKSUM_NONE
839 if (cqe->status & cpu_to_be16(MLX4_CQE_STATUS_TCP |
840 MLX4_CQE_STATUS_UDP)) {
841 if ((cqe->status & cpu_to_be16(MLX4_CQE_STATUS_IPOK)) &&
842 cqe->checksum == cpu_to_be16(0xffff)) {
843 bool l2_tunnel = (dev->hw_enc_features & NETIF_F_RXCSUM) &&
844 (cqe->vlan_my_qpn & cpu_to_be32(MLX4_CQE_L2_TUNNEL));
846 ip_summed = CHECKSUM_UNNECESSARY;
847 hash_type = PKT_HASH_TYPE_L4;
855 if (priv->flags & MLX4_EN_FLAG_RX_CSUM_NON_TCP_UDP &&
856 (cqe->status & cpu_to_be16(MLX4_CQE_STATUS_IPV4 |
857 MLX4_CQE_STATUS_IPV6))) {
858 if (check_csum(cqe, skb, va, dev->features)) {
861 ip_summed = CHECKSUM_COMPLETE;
862 hash_type = PKT_HASH_TYPE_L3;
863 ring->csum_complete++;
871 ip_summed = CHECKSUM_NONE;
872 hash_type = PKT_HASH_TYPE_L3;
875 skb->ip_summed = ip_summed;
876 if (dev->features & NETIF_F_RXHASH)
878 be32_to_cpu(cqe->immed_rss_invalid),
881 if ((cqe->vlan_my_qpn &
882 cpu_to_be32(MLX4_CQE_CVLAN_PRESENT_MASK)) &&
883 (dev->features & NETIF_F_HW_VLAN_CTAG_RX))
884 __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q),
885 be16_to_cpu(cqe->sl_vid));
886 else if ((cqe->vlan_my_qpn &
887 cpu_to_be32(MLX4_CQE_SVLAN_PRESENT_MASK)) &&
888 (dev->features & NETIF_F_HW_VLAN_STAG_RX))
889 __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021AD),
890 be16_to_cpu(cqe->sl_vid));
892 nr = mlx4_en_complete_rx_desc(priv, frags, skb, length);
894 skb_shinfo(skb)->nr_frags = nr;
896 skb->data_len = length;
897 napi_gro_frags(&cq->napi);
903 ++cq->mcq.cons_index;
904 index = (cq->mcq.cons_index) & ring->size_mask;
905 cqe = mlx4_en_get_cqe(cq->buf, index, priv->cqe_size) + factor;
906 if (unlikely(++polled == budget))
912 if (likely(polled)) {
913 if (doorbell_pending) {
914 priv->tx_cq[TX_XDP][cq_ring]->xdp_busy = true;
915 mlx4_en_xmit_doorbell(priv->tx_ring[TX_XDP][cq_ring]);
918 mlx4_cq_set_ci(&cq->mcq);
919 wmb(); /* ensure HW sees CQ consumer before we post new buffers */
920 ring->cons = cq->mcq.cons_index;
922 AVG_PERF_COUNTER(priv->pstats.rx_coal_avg, polled);
924 mlx4_en_refill_rx_buffers(priv, ring);
930 void mlx4_en_rx_irq(struct mlx4_cq *mcq)
932 struct mlx4_en_cq *cq = container_of(mcq, struct mlx4_en_cq, mcq);
933 struct mlx4_en_priv *priv = netdev_priv(cq->dev);
935 if (likely(priv->port_up))
936 napi_schedule_irqoff(&cq->napi);
938 mlx4_en_arm_cq(priv, cq);
941 /* Rx CQ polling - called by NAPI */
942 int mlx4_en_poll_rx_cq(struct napi_struct *napi, int budget)
944 struct mlx4_en_cq *cq = container_of(napi, struct mlx4_en_cq, napi);
945 struct net_device *dev = cq->dev;
946 struct mlx4_en_priv *priv = netdev_priv(dev);
947 struct mlx4_en_cq *xdp_tx_cq = NULL;
948 bool clean_complete = true;
954 if (priv->tx_ring_num[TX_XDP]) {
955 xdp_tx_cq = priv->tx_cq[TX_XDP][cq->ring];
956 if (xdp_tx_cq->xdp_busy) {
957 clean_complete = mlx4_en_process_tx_cq(dev, xdp_tx_cq,
959 xdp_tx_cq->xdp_busy = !clean_complete;
963 done = mlx4_en_process_rx_cq(dev, cq, budget);
965 /* If we used up all the quota - we're probably not done yet... */
966 if (done == budget || !clean_complete) {
967 const struct cpumask *aff;
968 struct irq_data *idata;
971 /* in case we got here because of !clean_complete */
974 INC_PERF_COUNTER(priv->pstats.napi_quota);
976 cpu_curr = smp_processor_id();
977 idata = irq_desc_get_irq_data(cq->irq_desc);
978 aff = irq_data_get_affinity_mask(idata);
980 if (likely(cpumask_test_cpu(cpu_curr, aff)))
983 /* Current cpu is not according to smp_irq_affinity -
984 * probably affinity changed. Need to stop this NAPI
985 * poll, and restart it on the right CPU.
986 * Try to avoid returning a too small value (like 0),
987 * to not fool net_rx_action() and its netdev_budget
993 if (likely(napi_complete_done(napi, done)))
994 mlx4_en_arm_cq(priv, cq);
998 void mlx4_en_calc_rx_buf(struct net_device *dev)
1000 struct mlx4_en_priv *priv = netdev_priv(dev);
1001 int eff_mtu = MLX4_EN_EFF_MTU(dev->mtu);
1004 /* bpf requires buffers to be set up as 1 packet per page.
1005 * This only works when num_frags == 1.
1007 if (priv->tx_ring_num[TX_XDP]) {
1008 priv->frag_info[0].frag_size = eff_mtu;
1009 /* This will gain efficient xdp frame recycling at the
1010 * expense of more costly truesize accounting
1012 priv->frag_info[0].frag_stride = PAGE_SIZE;
1013 priv->dma_dir = PCI_DMA_BIDIRECTIONAL;
1014 priv->rx_headroom = XDP_PACKET_HEADROOM;
1017 int frag_size_max = 2048, buf_size = 0;
1019 /* should not happen, right ? */
1020 if (eff_mtu > PAGE_SIZE + (MLX4_EN_MAX_RX_FRAGS - 1) * 2048)
1021 frag_size_max = PAGE_SIZE;
1023 while (buf_size < eff_mtu) {
1024 int frag_stride, frag_size = eff_mtu - buf_size;
1027 if (i < MLX4_EN_MAX_RX_FRAGS - 1)
1028 frag_size = min(frag_size, frag_size_max);
1030 priv->frag_info[i].frag_size = frag_size;
1031 frag_stride = ALIGN(frag_size, SMP_CACHE_BYTES);
1032 /* We can only pack 2 1536-bytes frames in on 4K page
1033 * Therefore, each frame would consume more bytes (truesize)
1035 nb = PAGE_SIZE / frag_stride;
1036 pad = (PAGE_SIZE - nb * frag_stride) / nb;
1037 pad &= ~(SMP_CACHE_BYTES - 1);
1038 priv->frag_info[i].frag_stride = frag_stride + pad;
1040 buf_size += frag_size;
1043 priv->dma_dir = PCI_DMA_FROMDEVICE;
1044 priv->rx_headroom = 0;
1047 priv->num_frags = i;
1048 priv->rx_skb_size = eff_mtu;
1049 priv->log_rx_info = ROUNDUP_LOG2(i * sizeof(struct mlx4_en_rx_alloc));
1051 en_dbg(DRV, priv, "Rx buffer scatter-list (effective-mtu:%d num_frags:%d):\n",
1052 eff_mtu, priv->num_frags);
1053 for (i = 0; i < priv->num_frags; i++) {
1056 " frag:%d - size:%d stride:%d\n",
1058 priv->frag_info[i].frag_size,
1059 priv->frag_info[i].frag_stride);
1063 /* RSS related functions */
1065 static int mlx4_en_config_rss_qp(struct mlx4_en_priv *priv, int qpn,
1066 struct mlx4_en_rx_ring *ring,
1067 enum mlx4_qp_state *state,
1070 struct mlx4_en_dev *mdev = priv->mdev;
1071 struct mlx4_qp_context *context;
1074 context = kmalloc(sizeof(*context), GFP_KERNEL);
1078 err = mlx4_qp_alloc(mdev->dev, qpn, qp);
1080 en_err(priv, "Failed to allocate qp #%x\n", qpn);
1083 qp->event = mlx4_en_sqp_event;
1085 memset(context, 0, sizeof(*context));
1086 mlx4_en_fill_qp_context(priv, ring->actual_size, ring->stride, 0, 0,
1087 qpn, ring->cqn, -1, context);
1088 context->db_rec_addr = cpu_to_be64(ring->wqres.db.dma);
1090 /* Cancel FCS removal if FW allows */
1091 if (mdev->dev->caps.flags & MLX4_DEV_CAP_FLAG_FCS_KEEP) {
1092 context->param3 |= cpu_to_be32(1 << 29);
1093 if (priv->dev->features & NETIF_F_RXFCS)
1096 ring->fcs_del = ETH_FCS_LEN;
1100 err = mlx4_qp_to_ready(mdev->dev, &ring->wqres.mtt, context, qp, state);
1102 mlx4_qp_remove(mdev->dev, qp);
1103 mlx4_qp_free(mdev->dev, qp);
1105 mlx4_en_update_rx_prod_db(ring);
1111 int mlx4_en_create_drop_qp(struct mlx4_en_priv *priv)
1116 err = mlx4_qp_reserve_range(priv->mdev->dev, 1, 1, &qpn,
1118 MLX4_RES_USAGE_DRIVER);
1120 en_err(priv, "Failed reserving drop qpn\n");
1123 err = mlx4_qp_alloc(priv->mdev->dev, qpn, &priv->drop_qp);
1125 en_err(priv, "Failed allocating drop qp\n");
1126 mlx4_qp_release_range(priv->mdev->dev, qpn, 1);
1133 void mlx4_en_destroy_drop_qp(struct mlx4_en_priv *priv)
1137 qpn = priv->drop_qp.qpn;
1138 mlx4_qp_remove(priv->mdev->dev, &priv->drop_qp);
1139 mlx4_qp_free(priv->mdev->dev, &priv->drop_qp);
1140 mlx4_qp_release_range(priv->mdev->dev, qpn, 1);
1143 /* Allocate rx qp's and configure them according to rss map */
1144 int mlx4_en_config_rss_steer(struct mlx4_en_priv *priv)
1146 struct mlx4_en_dev *mdev = priv->mdev;
1147 struct mlx4_en_rss_map *rss_map = &priv->rss_map;
1148 struct mlx4_qp_context context;
1149 struct mlx4_rss_context *rss_context;
1152 u8 rss_mask = (MLX4_RSS_IPV4 | MLX4_RSS_TCP_IPV4 | MLX4_RSS_IPV6 |
1159 en_dbg(DRV, priv, "Configuring rss steering\n");
1161 flags = priv->rx_ring_num == 1 ? MLX4_RESERVE_A0_QP : 0;
1162 err = mlx4_qp_reserve_range(mdev->dev, priv->rx_ring_num,
1164 &rss_map->base_qpn, flags,
1165 MLX4_RES_USAGE_DRIVER);
1167 en_err(priv, "Failed reserving %d qps\n", priv->rx_ring_num);
1171 for (i = 0; i < priv->rx_ring_num; i++) {
1172 qpn = rss_map->base_qpn + i;
1173 err = mlx4_en_config_rss_qp(priv, qpn, priv->rx_ring[i],
1182 if (priv->rx_ring_num == 1) {
1183 rss_map->indir_qp = &rss_map->qps[0];
1184 priv->base_qpn = rss_map->indir_qp->qpn;
1185 en_info(priv, "Optimized Non-RSS steering\n");
1189 rss_map->indir_qp = kzalloc(sizeof(*rss_map->indir_qp), GFP_KERNEL);
1190 if (!rss_map->indir_qp) {
1195 /* Configure RSS indirection qp */
1196 err = mlx4_qp_alloc(mdev->dev, priv->base_qpn, rss_map->indir_qp);
1198 en_err(priv, "Failed to allocate RSS indirection QP\n");
1202 rss_map->indir_qp->event = mlx4_en_sqp_event;
1203 mlx4_en_fill_qp_context(priv, 0, 0, 0, 1, priv->base_qpn,
1204 priv->rx_ring[0]->cqn, -1, &context);
1206 if (!priv->prof->rss_rings || priv->prof->rss_rings > priv->rx_ring_num)
1207 rss_rings = priv->rx_ring_num;
1209 rss_rings = priv->prof->rss_rings;
1211 ptr = ((void *) &context) + offsetof(struct mlx4_qp_context, pri_path)
1212 + MLX4_RSS_OFFSET_IN_QPC_PRI_PATH;
1214 rss_context->base_qpn = cpu_to_be32(ilog2(rss_rings) << 24 |
1215 (rss_map->base_qpn));
1216 rss_context->default_qpn = cpu_to_be32(rss_map->base_qpn);
1217 if (priv->mdev->profile.udp_rss) {
1218 rss_mask |= MLX4_RSS_UDP_IPV4 | MLX4_RSS_UDP_IPV6;
1219 rss_context->base_qpn_udp = rss_context->default_qpn;
1222 if (mdev->dev->caps.tunnel_offload_mode == MLX4_TUNNEL_OFFLOAD_MODE_VXLAN) {
1223 en_info(priv, "Setting RSS context tunnel type to RSS on inner headers\n");
1224 rss_mask |= MLX4_RSS_BY_INNER_HEADERS;
1227 rss_context->flags = rss_mask;
1228 rss_context->hash_fn = MLX4_RSS_HASH_TOP;
1229 if (priv->rss_hash_fn == ETH_RSS_HASH_XOR) {
1230 rss_context->hash_fn = MLX4_RSS_HASH_XOR;
1231 } else if (priv->rss_hash_fn == ETH_RSS_HASH_TOP) {
1232 rss_context->hash_fn = MLX4_RSS_HASH_TOP;
1233 memcpy(rss_context->rss_key, priv->rss_key,
1234 MLX4_EN_RSS_KEY_SIZE);
1236 en_err(priv, "Unknown RSS hash function requested\n");
1241 err = mlx4_qp_to_ready(mdev->dev, &priv->res.mtt, &context,
1242 rss_map->indir_qp, &rss_map->indir_state);
1249 mlx4_qp_modify(mdev->dev, NULL, rss_map->indir_state,
1250 MLX4_QP_STATE_RST, NULL, 0, 0, rss_map->indir_qp);
1251 mlx4_qp_remove(mdev->dev, rss_map->indir_qp);
1252 mlx4_qp_free(mdev->dev, rss_map->indir_qp);
1254 kfree(rss_map->indir_qp);
1255 rss_map->indir_qp = NULL;
1257 for (i = 0; i < good_qps; i++) {
1258 mlx4_qp_modify(mdev->dev, NULL, rss_map->state[i],
1259 MLX4_QP_STATE_RST, NULL, 0, 0, &rss_map->qps[i]);
1260 mlx4_qp_remove(mdev->dev, &rss_map->qps[i]);
1261 mlx4_qp_free(mdev->dev, &rss_map->qps[i]);
1263 mlx4_qp_release_range(mdev->dev, rss_map->base_qpn, priv->rx_ring_num);
1267 void mlx4_en_release_rss_steer(struct mlx4_en_priv *priv)
1269 struct mlx4_en_dev *mdev = priv->mdev;
1270 struct mlx4_en_rss_map *rss_map = &priv->rss_map;
1273 if (priv->rx_ring_num > 1) {
1274 mlx4_qp_modify(mdev->dev, NULL, rss_map->indir_state,
1275 MLX4_QP_STATE_RST, NULL, 0, 0,
1277 mlx4_qp_remove(mdev->dev, rss_map->indir_qp);
1278 mlx4_qp_free(mdev->dev, rss_map->indir_qp);
1279 kfree(rss_map->indir_qp);
1280 rss_map->indir_qp = NULL;
1283 for (i = 0; i < priv->rx_ring_num; i++) {
1284 mlx4_qp_modify(mdev->dev, NULL, rss_map->state[i],
1285 MLX4_QP_STATE_RST, NULL, 0, 0, &rss_map->qps[i]);
1286 mlx4_qp_remove(mdev->dev, &rss_map->qps[i]);
1287 mlx4_qp_free(mdev->dev, &rss_map->qps[i]);
1289 mlx4_qp_release_range(mdev->dev, rss_map->base_qpn, priv->rx_ring_num);