18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-only
28c2ecf20Sopenharmony_ci/****************************************************************************
38c2ecf20Sopenharmony_ci * Driver for Solarflare network controllers and boards
48c2ecf20Sopenharmony_ci * Copyright 2005-2006 Fen Systems Ltd.
58c2ecf20Sopenharmony_ci * Copyright 2005-2013 Solarflare Communications Inc.
68c2ecf20Sopenharmony_ci */
78c2ecf20Sopenharmony_ci
88c2ecf20Sopenharmony_ci#include <linux/socket.h>
98c2ecf20Sopenharmony_ci#include <linux/in.h>
108c2ecf20Sopenharmony_ci#include <linux/slab.h>
118c2ecf20Sopenharmony_ci#include <linux/ip.h>
128c2ecf20Sopenharmony_ci#include <linux/ipv6.h>
138c2ecf20Sopenharmony_ci#include <linux/tcp.h>
148c2ecf20Sopenharmony_ci#include <linux/udp.h>
158c2ecf20Sopenharmony_ci#include <linux/prefetch.h>
168c2ecf20Sopenharmony_ci#include <linux/moduleparam.h>
178c2ecf20Sopenharmony_ci#include <linux/iommu.h>
188c2ecf20Sopenharmony_ci#include <net/ip.h>
198c2ecf20Sopenharmony_ci#include <net/checksum.h>
208c2ecf20Sopenharmony_ci#include <net/xdp.h>
218c2ecf20Sopenharmony_ci#include <linux/bpf_trace.h>
228c2ecf20Sopenharmony_ci#include "net_driver.h"
238c2ecf20Sopenharmony_ci#include "efx.h"
248c2ecf20Sopenharmony_ci#include "rx_common.h"
258c2ecf20Sopenharmony_ci#include "filter.h"
268c2ecf20Sopenharmony_ci#include "nic.h"
278c2ecf20Sopenharmony_ci#include "selftest.h"
288c2ecf20Sopenharmony_ci#include "workarounds.h"
298c2ecf20Sopenharmony_ci
308c2ecf20Sopenharmony_ci/* Preferred number of descriptors to fill at once */
318c2ecf20Sopenharmony_ci#define EFX_RX_PREFERRED_BATCH 8U
328c2ecf20Sopenharmony_ci
338c2ecf20Sopenharmony_ci/* Maximum rx prefix used by any architecture. */
348c2ecf20Sopenharmony_ci#define EFX_MAX_RX_PREFIX_SIZE 16
358c2ecf20Sopenharmony_ci
368c2ecf20Sopenharmony_ci/* Size of buffer allocated for skb header area. */
378c2ecf20Sopenharmony_ci#define EFX_SKB_HEADERS  128u
388c2ecf20Sopenharmony_ci
398c2ecf20Sopenharmony_ci/* Each packet can consume up to ceil(max_frame_len / buffer_size) buffers */
408c2ecf20Sopenharmony_ci#define EFX_RX_MAX_FRAGS DIV_ROUND_UP(EFX_MAX_FRAME_LEN(EFX_MAX_MTU), \
418c2ecf20Sopenharmony_ci				      EFX_RX_USR_BUF_SIZE)
428c2ecf20Sopenharmony_ci
438c2ecf20Sopenharmony_cistatic void efx_rx_packet__check_len(struct efx_rx_queue *rx_queue,
448c2ecf20Sopenharmony_ci				     struct efx_rx_buffer *rx_buf,
458c2ecf20Sopenharmony_ci				     int len)
468c2ecf20Sopenharmony_ci{
478c2ecf20Sopenharmony_ci	struct efx_nic *efx = rx_queue->efx;
488c2ecf20Sopenharmony_ci	unsigned max_len = rx_buf->len - efx->type->rx_buffer_padding;
498c2ecf20Sopenharmony_ci
508c2ecf20Sopenharmony_ci	if (likely(len <= max_len))
518c2ecf20Sopenharmony_ci		return;
528c2ecf20Sopenharmony_ci
538c2ecf20Sopenharmony_ci	/* The packet must be discarded, but this is only a fatal error
548c2ecf20Sopenharmony_ci	 * if the caller indicated it was
558c2ecf20Sopenharmony_ci	 */
568c2ecf20Sopenharmony_ci	rx_buf->flags |= EFX_RX_PKT_DISCARD;
578c2ecf20Sopenharmony_ci
588c2ecf20Sopenharmony_ci	if (net_ratelimit())
598c2ecf20Sopenharmony_ci		netif_err(efx, rx_err, efx->net_dev,
608c2ecf20Sopenharmony_ci			  "RX queue %d overlength RX event (%#x > %#x)\n",
618c2ecf20Sopenharmony_ci			  efx_rx_queue_index(rx_queue), len, max_len);
628c2ecf20Sopenharmony_ci
638c2ecf20Sopenharmony_ci	efx_rx_queue_channel(rx_queue)->n_rx_overlength++;
648c2ecf20Sopenharmony_ci}
658c2ecf20Sopenharmony_ci
668c2ecf20Sopenharmony_ci/* Allocate and construct an SKB around page fragments */
678c2ecf20Sopenharmony_cistatic struct sk_buff *efx_rx_mk_skb(struct efx_channel *channel,
688c2ecf20Sopenharmony_ci				     struct efx_rx_buffer *rx_buf,
698c2ecf20Sopenharmony_ci				     unsigned int n_frags,
708c2ecf20Sopenharmony_ci				     u8 *eh, int hdr_len)
718c2ecf20Sopenharmony_ci{
728c2ecf20Sopenharmony_ci	struct efx_nic *efx = channel->efx;
738c2ecf20Sopenharmony_ci	struct sk_buff *skb;
748c2ecf20Sopenharmony_ci
758c2ecf20Sopenharmony_ci	/* Allocate an SKB to store the headers */
768c2ecf20Sopenharmony_ci	skb = netdev_alloc_skb(efx->net_dev,
778c2ecf20Sopenharmony_ci			       efx->rx_ip_align + efx->rx_prefix_size +
788c2ecf20Sopenharmony_ci			       hdr_len);
798c2ecf20Sopenharmony_ci	if (unlikely(skb == NULL)) {
808c2ecf20Sopenharmony_ci		atomic_inc(&efx->n_rx_noskb_drops);
818c2ecf20Sopenharmony_ci		return NULL;
828c2ecf20Sopenharmony_ci	}
838c2ecf20Sopenharmony_ci
848c2ecf20Sopenharmony_ci	EFX_WARN_ON_ONCE_PARANOID(rx_buf->len < hdr_len);
858c2ecf20Sopenharmony_ci
868c2ecf20Sopenharmony_ci	memcpy(skb->data + efx->rx_ip_align, eh - efx->rx_prefix_size,
878c2ecf20Sopenharmony_ci	       efx->rx_prefix_size + hdr_len);
888c2ecf20Sopenharmony_ci	skb_reserve(skb, efx->rx_ip_align + efx->rx_prefix_size);
898c2ecf20Sopenharmony_ci	__skb_put(skb, hdr_len);
908c2ecf20Sopenharmony_ci
918c2ecf20Sopenharmony_ci	/* Append the remaining page(s) onto the frag list */
928c2ecf20Sopenharmony_ci	if (rx_buf->len > hdr_len) {
938c2ecf20Sopenharmony_ci		rx_buf->page_offset += hdr_len;
948c2ecf20Sopenharmony_ci		rx_buf->len -= hdr_len;
958c2ecf20Sopenharmony_ci
968c2ecf20Sopenharmony_ci		for (;;) {
978c2ecf20Sopenharmony_ci			skb_fill_page_desc(skb, skb_shinfo(skb)->nr_frags,
988c2ecf20Sopenharmony_ci					   rx_buf->page, rx_buf->page_offset,
998c2ecf20Sopenharmony_ci					   rx_buf->len);
1008c2ecf20Sopenharmony_ci			rx_buf->page = NULL;
1018c2ecf20Sopenharmony_ci			skb->len += rx_buf->len;
1028c2ecf20Sopenharmony_ci			skb->data_len += rx_buf->len;
1038c2ecf20Sopenharmony_ci			if (skb_shinfo(skb)->nr_frags == n_frags)
1048c2ecf20Sopenharmony_ci				break;
1058c2ecf20Sopenharmony_ci
1068c2ecf20Sopenharmony_ci			rx_buf = efx_rx_buf_next(&channel->rx_queue, rx_buf);
1078c2ecf20Sopenharmony_ci		}
1088c2ecf20Sopenharmony_ci	} else {
1098c2ecf20Sopenharmony_ci		__free_pages(rx_buf->page, efx->rx_buffer_order);
1108c2ecf20Sopenharmony_ci		rx_buf->page = NULL;
1118c2ecf20Sopenharmony_ci		n_frags = 0;
1128c2ecf20Sopenharmony_ci	}
1138c2ecf20Sopenharmony_ci
1148c2ecf20Sopenharmony_ci	skb->truesize += n_frags * efx->rx_buffer_truesize;
1158c2ecf20Sopenharmony_ci
1168c2ecf20Sopenharmony_ci	/* Move past the ethernet header */
1178c2ecf20Sopenharmony_ci	skb->protocol = eth_type_trans(skb, efx->net_dev);
1188c2ecf20Sopenharmony_ci
1198c2ecf20Sopenharmony_ci	skb_mark_napi_id(skb, &channel->napi_str);
1208c2ecf20Sopenharmony_ci
1218c2ecf20Sopenharmony_ci	return skb;
1228c2ecf20Sopenharmony_ci}
1238c2ecf20Sopenharmony_ci
1248c2ecf20Sopenharmony_civoid efx_rx_packet(struct efx_rx_queue *rx_queue, unsigned int index,
1258c2ecf20Sopenharmony_ci		   unsigned int n_frags, unsigned int len, u16 flags)
1268c2ecf20Sopenharmony_ci{
1278c2ecf20Sopenharmony_ci	struct efx_nic *efx = rx_queue->efx;
1288c2ecf20Sopenharmony_ci	struct efx_channel *channel = efx_rx_queue_channel(rx_queue);
1298c2ecf20Sopenharmony_ci	struct efx_rx_buffer *rx_buf;
1308c2ecf20Sopenharmony_ci
1318c2ecf20Sopenharmony_ci	rx_queue->rx_packets++;
1328c2ecf20Sopenharmony_ci
1338c2ecf20Sopenharmony_ci	rx_buf = efx_rx_buffer(rx_queue, index);
1348c2ecf20Sopenharmony_ci	rx_buf->flags |= flags;
1358c2ecf20Sopenharmony_ci
1368c2ecf20Sopenharmony_ci	/* Validate the number of fragments and completed length */
1378c2ecf20Sopenharmony_ci	if (n_frags == 1) {
1388c2ecf20Sopenharmony_ci		if (!(flags & EFX_RX_PKT_PREFIX_LEN))
1398c2ecf20Sopenharmony_ci			efx_rx_packet__check_len(rx_queue, rx_buf, len);
1408c2ecf20Sopenharmony_ci	} else if (unlikely(n_frags > EFX_RX_MAX_FRAGS) ||
1418c2ecf20Sopenharmony_ci		   unlikely(len <= (n_frags - 1) * efx->rx_dma_len) ||
1428c2ecf20Sopenharmony_ci		   unlikely(len > n_frags * efx->rx_dma_len) ||
1438c2ecf20Sopenharmony_ci		   unlikely(!efx->rx_scatter)) {
1448c2ecf20Sopenharmony_ci		/* If this isn't an explicit discard request, either
1458c2ecf20Sopenharmony_ci		 * the hardware or the driver is broken.
1468c2ecf20Sopenharmony_ci		 */
1478c2ecf20Sopenharmony_ci		WARN_ON(!(len == 0 && rx_buf->flags & EFX_RX_PKT_DISCARD));
1488c2ecf20Sopenharmony_ci		rx_buf->flags |= EFX_RX_PKT_DISCARD;
1498c2ecf20Sopenharmony_ci	}
1508c2ecf20Sopenharmony_ci
1518c2ecf20Sopenharmony_ci	netif_vdbg(efx, rx_status, efx->net_dev,
1528c2ecf20Sopenharmony_ci		   "RX queue %d received ids %x-%x len %d %s%s\n",
1538c2ecf20Sopenharmony_ci		   efx_rx_queue_index(rx_queue), index,
1548c2ecf20Sopenharmony_ci		   (index + n_frags - 1) & rx_queue->ptr_mask, len,
1558c2ecf20Sopenharmony_ci		   (rx_buf->flags & EFX_RX_PKT_CSUMMED) ? " [SUMMED]" : "",
1568c2ecf20Sopenharmony_ci		   (rx_buf->flags & EFX_RX_PKT_DISCARD) ? " [DISCARD]" : "");
1578c2ecf20Sopenharmony_ci
1588c2ecf20Sopenharmony_ci	/* Discard packet, if instructed to do so.  Process the
1598c2ecf20Sopenharmony_ci	 * previous receive first.
1608c2ecf20Sopenharmony_ci	 */
1618c2ecf20Sopenharmony_ci	if (unlikely(rx_buf->flags & EFX_RX_PKT_DISCARD)) {
1628c2ecf20Sopenharmony_ci		efx_rx_flush_packet(channel);
1638c2ecf20Sopenharmony_ci		efx_discard_rx_packet(channel, rx_buf, n_frags);
1648c2ecf20Sopenharmony_ci		return;
1658c2ecf20Sopenharmony_ci	}
1668c2ecf20Sopenharmony_ci
1678c2ecf20Sopenharmony_ci	if (n_frags == 1 && !(flags & EFX_RX_PKT_PREFIX_LEN))
1688c2ecf20Sopenharmony_ci		rx_buf->len = len;
1698c2ecf20Sopenharmony_ci
1708c2ecf20Sopenharmony_ci	/* Release and/or sync the DMA mapping - assumes all RX buffers
1718c2ecf20Sopenharmony_ci	 * consumed in-order per RX queue.
1728c2ecf20Sopenharmony_ci	 */
1738c2ecf20Sopenharmony_ci	efx_sync_rx_buffer(efx, rx_buf, rx_buf->len);
1748c2ecf20Sopenharmony_ci
1758c2ecf20Sopenharmony_ci	/* Prefetch nice and early so data will (hopefully) be in cache by
1768c2ecf20Sopenharmony_ci	 * the time we look at it.
1778c2ecf20Sopenharmony_ci	 */
1788c2ecf20Sopenharmony_ci	prefetch(efx_rx_buf_va(rx_buf));
1798c2ecf20Sopenharmony_ci
1808c2ecf20Sopenharmony_ci	rx_buf->page_offset += efx->rx_prefix_size;
1818c2ecf20Sopenharmony_ci	rx_buf->len -= efx->rx_prefix_size;
1828c2ecf20Sopenharmony_ci
1838c2ecf20Sopenharmony_ci	if (n_frags > 1) {
1848c2ecf20Sopenharmony_ci		/* Release/sync DMA mapping for additional fragments.
1858c2ecf20Sopenharmony_ci		 * Fix length for last fragment.
1868c2ecf20Sopenharmony_ci		 */
1878c2ecf20Sopenharmony_ci		unsigned int tail_frags = n_frags - 1;
1888c2ecf20Sopenharmony_ci
1898c2ecf20Sopenharmony_ci		for (;;) {
1908c2ecf20Sopenharmony_ci			rx_buf = efx_rx_buf_next(rx_queue, rx_buf);
1918c2ecf20Sopenharmony_ci			if (--tail_frags == 0)
1928c2ecf20Sopenharmony_ci				break;
1938c2ecf20Sopenharmony_ci			efx_sync_rx_buffer(efx, rx_buf, efx->rx_dma_len);
1948c2ecf20Sopenharmony_ci		}
1958c2ecf20Sopenharmony_ci		rx_buf->len = len - (n_frags - 1) * efx->rx_dma_len;
1968c2ecf20Sopenharmony_ci		efx_sync_rx_buffer(efx, rx_buf, rx_buf->len);
1978c2ecf20Sopenharmony_ci	}
1988c2ecf20Sopenharmony_ci
1998c2ecf20Sopenharmony_ci	/* All fragments have been DMA-synced, so recycle pages. */
2008c2ecf20Sopenharmony_ci	rx_buf = efx_rx_buffer(rx_queue, index);
2018c2ecf20Sopenharmony_ci	efx_recycle_rx_pages(channel, rx_buf, n_frags);
2028c2ecf20Sopenharmony_ci
2038c2ecf20Sopenharmony_ci	/* Pipeline receives so that we give time for packet headers to be
2048c2ecf20Sopenharmony_ci	 * prefetched into cache.
2058c2ecf20Sopenharmony_ci	 */
2068c2ecf20Sopenharmony_ci	efx_rx_flush_packet(channel);
2078c2ecf20Sopenharmony_ci	channel->rx_pkt_n_frags = n_frags;
2088c2ecf20Sopenharmony_ci	channel->rx_pkt_index = index;
2098c2ecf20Sopenharmony_ci}
2108c2ecf20Sopenharmony_ci
2118c2ecf20Sopenharmony_cistatic void efx_rx_deliver(struct efx_channel *channel, u8 *eh,
2128c2ecf20Sopenharmony_ci			   struct efx_rx_buffer *rx_buf,
2138c2ecf20Sopenharmony_ci			   unsigned int n_frags)
2148c2ecf20Sopenharmony_ci{
2158c2ecf20Sopenharmony_ci	struct sk_buff *skb;
2168c2ecf20Sopenharmony_ci	u16 hdr_len = min_t(u16, rx_buf->len, EFX_SKB_HEADERS);
2178c2ecf20Sopenharmony_ci
2188c2ecf20Sopenharmony_ci	skb = efx_rx_mk_skb(channel, rx_buf, n_frags, eh, hdr_len);
2198c2ecf20Sopenharmony_ci	if (unlikely(skb == NULL)) {
2208c2ecf20Sopenharmony_ci		struct efx_rx_queue *rx_queue;
2218c2ecf20Sopenharmony_ci
2228c2ecf20Sopenharmony_ci		rx_queue = efx_channel_get_rx_queue(channel);
2238c2ecf20Sopenharmony_ci		efx_free_rx_buffers(rx_queue, rx_buf, n_frags);
2248c2ecf20Sopenharmony_ci		return;
2258c2ecf20Sopenharmony_ci	}
2268c2ecf20Sopenharmony_ci	skb_record_rx_queue(skb, channel->rx_queue.core_index);
2278c2ecf20Sopenharmony_ci
2288c2ecf20Sopenharmony_ci	/* Set the SKB flags */
2298c2ecf20Sopenharmony_ci	skb_checksum_none_assert(skb);
2308c2ecf20Sopenharmony_ci	if (likely(rx_buf->flags & EFX_RX_PKT_CSUMMED)) {
2318c2ecf20Sopenharmony_ci		skb->ip_summed = CHECKSUM_UNNECESSARY;
2328c2ecf20Sopenharmony_ci		skb->csum_level = !!(rx_buf->flags & EFX_RX_PKT_CSUM_LEVEL);
2338c2ecf20Sopenharmony_ci	}
2348c2ecf20Sopenharmony_ci
2358c2ecf20Sopenharmony_ci	efx_rx_skb_attach_timestamp(channel, skb);
2368c2ecf20Sopenharmony_ci
2378c2ecf20Sopenharmony_ci	if (channel->type->receive_skb)
2388c2ecf20Sopenharmony_ci		if (channel->type->receive_skb(channel, skb))
2398c2ecf20Sopenharmony_ci			return;
2408c2ecf20Sopenharmony_ci
2418c2ecf20Sopenharmony_ci	/* Pass the packet up */
2428c2ecf20Sopenharmony_ci	if (channel->rx_list != NULL)
2438c2ecf20Sopenharmony_ci		/* Add to list, will pass up later */
2448c2ecf20Sopenharmony_ci		list_add_tail(&skb->list, channel->rx_list);
2458c2ecf20Sopenharmony_ci	else
2468c2ecf20Sopenharmony_ci		/* No list, so pass it up now */
2478c2ecf20Sopenharmony_ci		netif_receive_skb(skb);
2488c2ecf20Sopenharmony_ci}
2498c2ecf20Sopenharmony_ci
2508c2ecf20Sopenharmony_ci/** efx_do_xdp: perform XDP processing on a received packet
2518c2ecf20Sopenharmony_ci *
2528c2ecf20Sopenharmony_ci * Returns true if packet should still be delivered.
2538c2ecf20Sopenharmony_ci */
2548c2ecf20Sopenharmony_cistatic bool efx_do_xdp(struct efx_nic *efx, struct efx_channel *channel,
2558c2ecf20Sopenharmony_ci		       struct efx_rx_buffer *rx_buf, u8 **ehp)
2568c2ecf20Sopenharmony_ci{
2578c2ecf20Sopenharmony_ci	u8 rx_prefix[EFX_MAX_RX_PREFIX_SIZE];
2588c2ecf20Sopenharmony_ci	struct efx_rx_queue *rx_queue;
2598c2ecf20Sopenharmony_ci	struct bpf_prog *xdp_prog;
2608c2ecf20Sopenharmony_ci	struct xdp_frame *xdpf;
2618c2ecf20Sopenharmony_ci	struct xdp_buff xdp;
2628c2ecf20Sopenharmony_ci	u32 xdp_act;
2638c2ecf20Sopenharmony_ci	s16 offset;
2648c2ecf20Sopenharmony_ci	int err;
2658c2ecf20Sopenharmony_ci
2668c2ecf20Sopenharmony_ci	rcu_read_lock();
2678c2ecf20Sopenharmony_ci	xdp_prog = rcu_dereference(efx->xdp_prog);
2688c2ecf20Sopenharmony_ci	if (!xdp_prog) {
2698c2ecf20Sopenharmony_ci		rcu_read_unlock();
2708c2ecf20Sopenharmony_ci		return true;
2718c2ecf20Sopenharmony_ci	}
2728c2ecf20Sopenharmony_ci
2738c2ecf20Sopenharmony_ci	rx_queue = efx_channel_get_rx_queue(channel);
2748c2ecf20Sopenharmony_ci
2758c2ecf20Sopenharmony_ci	if (unlikely(channel->rx_pkt_n_frags > 1)) {
2768c2ecf20Sopenharmony_ci		/* We can't do XDP on fragmented packets - drop. */
2778c2ecf20Sopenharmony_ci		rcu_read_unlock();
2788c2ecf20Sopenharmony_ci		efx_free_rx_buffers(rx_queue, rx_buf,
2798c2ecf20Sopenharmony_ci				    channel->rx_pkt_n_frags);
2808c2ecf20Sopenharmony_ci		if (net_ratelimit())
2818c2ecf20Sopenharmony_ci			netif_err(efx, rx_err, efx->net_dev,
2828c2ecf20Sopenharmony_ci				  "XDP is not possible with multiple receive fragments (%d)\n",
2838c2ecf20Sopenharmony_ci				  channel->rx_pkt_n_frags);
2848c2ecf20Sopenharmony_ci		channel->n_rx_xdp_bad_drops++;
2858c2ecf20Sopenharmony_ci		return false;
2868c2ecf20Sopenharmony_ci	}
2878c2ecf20Sopenharmony_ci
2888c2ecf20Sopenharmony_ci	dma_sync_single_for_cpu(&efx->pci_dev->dev, rx_buf->dma_addr,
2898c2ecf20Sopenharmony_ci				rx_buf->len, DMA_FROM_DEVICE);
2908c2ecf20Sopenharmony_ci
2918c2ecf20Sopenharmony_ci	/* Save the rx prefix. */
2928c2ecf20Sopenharmony_ci	EFX_WARN_ON_PARANOID(efx->rx_prefix_size > EFX_MAX_RX_PREFIX_SIZE);
2938c2ecf20Sopenharmony_ci	memcpy(rx_prefix, *ehp - efx->rx_prefix_size,
2948c2ecf20Sopenharmony_ci	       efx->rx_prefix_size);
2958c2ecf20Sopenharmony_ci
2968c2ecf20Sopenharmony_ci	xdp.data = *ehp;
2978c2ecf20Sopenharmony_ci	xdp.data_hard_start = xdp.data - EFX_XDP_HEADROOM;
2988c2ecf20Sopenharmony_ci
2998c2ecf20Sopenharmony_ci	/* No support yet for XDP metadata */
3008c2ecf20Sopenharmony_ci	xdp_set_data_meta_invalid(&xdp);
3018c2ecf20Sopenharmony_ci	xdp.data_end = xdp.data + rx_buf->len;
3028c2ecf20Sopenharmony_ci	xdp.rxq = &rx_queue->xdp_rxq_info;
3038c2ecf20Sopenharmony_ci	xdp.frame_sz = efx->rx_page_buf_step;
3048c2ecf20Sopenharmony_ci
3058c2ecf20Sopenharmony_ci	xdp_act = bpf_prog_run_xdp(xdp_prog, &xdp);
3068c2ecf20Sopenharmony_ci	rcu_read_unlock();
3078c2ecf20Sopenharmony_ci
3088c2ecf20Sopenharmony_ci	offset = (u8 *)xdp.data - *ehp;
3098c2ecf20Sopenharmony_ci
3108c2ecf20Sopenharmony_ci	switch (xdp_act) {
3118c2ecf20Sopenharmony_ci	case XDP_PASS:
3128c2ecf20Sopenharmony_ci		/* Fix up rx prefix. */
3138c2ecf20Sopenharmony_ci		if (offset) {
3148c2ecf20Sopenharmony_ci			*ehp += offset;
3158c2ecf20Sopenharmony_ci			rx_buf->page_offset += offset;
3168c2ecf20Sopenharmony_ci			rx_buf->len -= offset;
3178c2ecf20Sopenharmony_ci			memcpy(*ehp - efx->rx_prefix_size, rx_prefix,
3188c2ecf20Sopenharmony_ci			       efx->rx_prefix_size);
3198c2ecf20Sopenharmony_ci		}
3208c2ecf20Sopenharmony_ci		break;
3218c2ecf20Sopenharmony_ci
3228c2ecf20Sopenharmony_ci	case XDP_TX:
3238c2ecf20Sopenharmony_ci		/* Buffer ownership passes to tx on success. */
3248c2ecf20Sopenharmony_ci		xdpf = xdp_convert_buff_to_frame(&xdp);
3258c2ecf20Sopenharmony_ci		err = efx_xdp_tx_buffers(efx, 1, &xdpf, true);
3268c2ecf20Sopenharmony_ci		if (unlikely(err != 1)) {
3278c2ecf20Sopenharmony_ci			efx_free_rx_buffers(rx_queue, rx_buf, 1);
3288c2ecf20Sopenharmony_ci			if (net_ratelimit())
3298c2ecf20Sopenharmony_ci				netif_err(efx, rx_err, efx->net_dev,
3308c2ecf20Sopenharmony_ci					  "XDP TX failed (%d)\n", err);
3318c2ecf20Sopenharmony_ci			channel->n_rx_xdp_bad_drops++;
3328c2ecf20Sopenharmony_ci			trace_xdp_exception(efx->net_dev, xdp_prog, xdp_act);
3338c2ecf20Sopenharmony_ci		} else {
3348c2ecf20Sopenharmony_ci			channel->n_rx_xdp_tx++;
3358c2ecf20Sopenharmony_ci		}
3368c2ecf20Sopenharmony_ci		break;
3378c2ecf20Sopenharmony_ci
3388c2ecf20Sopenharmony_ci	case XDP_REDIRECT:
3398c2ecf20Sopenharmony_ci		err = xdp_do_redirect(efx->net_dev, &xdp, xdp_prog);
3408c2ecf20Sopenharmony_ci		if (unlikely(err)) {
3418c2ecf20Sopenharmony_ci			efx_free_rx_buffers(rx_queue, rx_buf, 1);
3428c2ecf20Sopenharmony_ci			if (net_ratelimit())
3438c2ecf20Sopenharmony_ci				netif_err(efx, rx_err, efx->net_dev,
3448c2ecf20Sopenharmony_ci					  "XDP redirect failed (%d)\n", err);
3458c2ecf20Sopenharmony_ci			channel->n_rx_xdp_bad_drops++;
3468c2ecf20Sopenharmony_ci			trace_xdp_exception(efx->net_dev, xdp_prog, xdp_act);
3478c2ecf20Sopenharmony_ci		} else {
3488c2ecf20Sopenharmony_ci			channel->n_rx_xdp_redirect++;
3498c2ecf20Sopenharmony_ci		}
3508c2ecf20Sopenharmony_ci		break;
3518c2ecf20Sopenharmony_ci
3528c2ecf20Sopenharmony_ci	default:
3538c2ecf20Sopenharmony_ci		bpf_warn_invalid_xdp_action(xdp_act);
3548c2ecf20Sopenharmony_ci		efx_free_rx_buffers(rx_queue, rx_buf, 1);
3558c2ecf20Sopenharmony_ci		channel->n_rx_xdp_bad_drops++;
3568c2ecf20Sopenharmony_ci		trace_xdp_exception(efx->net_dev, xdp_prog, xdp_act);
3578c2ecf20Sopenharmony_ci		break;
3588c2ecf20Sopenharmony_ci
3598c2ecf20Sopenharmony_ci	case XDP_ABORTED:
3608c2ecf20Sopenharmony_ci		trace_xdp_exception(efx->net_dev, xdp_prog, xdp_act);
3618c2ecf20Sopenharmony_ci		fallthrough;
3628c2ecf20Sopenharmony_ci	case XDP_DROP:
3638c2ecf20Sopenharmony_ci		efx_free_rx_buffers(rx_queue, rx_buf, 1);
3648c2ecf20Sopenharmony_ci		channel->n_rx_xdp_drops++;
3658c2ecf20Sopenharmony_ci		break;
3668c2ecf20Sopenharmony_ci	}
3678c2ecf20Sopenharmony_ci
3688c2ecf20Sopenharmony_ci	return xdp_act == XDP_PASS;
3698c2ecf20Sopenharmony_ci}
3708c2ecf20Sopenharmony_ci
3718c2ecf20Sopenharmony_ci/* Handle a received packet.  Second half: Touches packet payload. */
3728c2ecf20Sopenharmony_civoid __efx_rx_packet(struct efx_channel *channel)
3738c2ecf20Sopenharmony_ci{
3748c2ecf20Sopenharmony_ci	struct efx_nic *efx = channel->efx;
3758c2ecf20Sopenharmony_ci	struct efx_rx_buffer *rx_buf =
3768c2ecf20Sopenharmony_ci		efx_rx_buffer(&channel->rx_queue, channel->rx_pkt_index);
3778c2ecf20Sopenharmony_ci	u8 *eh = efx_rx_buf_va(rx_buf);
3788c2ecf20Sopenharmony_ci
3798c2ecf20Sopenharmony_ci	/* Read length from the prefix if necessary.  This already
3808c2ecf20Sopenharmony_ci	 * excludes the length of the prefix itself.
3818c2ecf20Sopenharmony_ci	 */
3828c2ecf20Sopenharmony_ci	if (rx_buf->flags & EFX_RX_PKT_PREFIX_LEN)
3838c2ecf20Sopenharmony_ci		rx_buf->len = le16_to_cpup((__le16 *)
3848c2ecf20Sopenharmony_ci					   (eh + efx->rx_packet_len_offset));
3858c2ecf20Sopenharmony_ci
3868c2ecf20Sopenharmony_ci	/* If we're in loopback test, then pass the packet directly to the
3878c2ecf20Sopenharmony_ci	 * loopback layer, and free the rx_buf here
3888c2ecf20Sopenharmony_ci	 */
3898c2ecf20Sopenharmony_ci	if (unlikely(efx->loopback_selftest)) {
3908c2ecf20Sopenharmony_ci		struct efx_rx_queue *rx_queue;
3918c2ecf20Sopenharmony_ci
3928c2ecf20Sopenharmony_ci		efx_loopback_rx_packet(efx, eh, rx_buf->len);
3938c2ecf20Sopenharmony_ci		rx_queue = efx_channel_get_rx_queue(channel);
3948c2ecf20Sopenharmony_ci		efx_free_rx_buffers(rx_queue, rx_buf,
3958c2ecf20Sopenharmony_ci				    channel->rx_pkt_n_frags);
3968c2ecf20Sopenharmony_ci		goto out;
3978c2ecf20Sopenharmony_ci	}
3988c2ecf20Sopenharmony_ci
3998c2ecf20Sopenharmony_ci	if (!efx_do_xdp(efx, channel, rx_buf, &eh))
4008c2ecf20Sopenharmony_ci		goto out;
4018c2ecf20Sopenharmony_ci
4028c2ecf20Sopenharmony_ci	if (unlikely(!(efx->net_dev->features & NETIF_F_RXCSUM)))
4038c2ecf20Sopenharmony_ci		rx_buf->flags &= ~EFX_RX_PKT_CSUMMED;
4048c2ecf20Sopenharmony_ci
4058c2ecf20Sopenharmony_ci	if ((rx_buf->flags & EFX_RX_PKT_TCP) && !channel->type->receive_skb)
4068c2ecf20Sopenharmony_ci		efx_rx_packet_gro(channel, rx_buf, channel->rx_pkt_n_frags, eh, 0);
4078c2ecf20Sopenharmony_ci	else
4088c2ecf20Sopenharmony_ci		efx_rx_deliver(channel, eh, rx_buf, channel->rx_pkt_n_frags);
4098c2ecf20Sopenharmony_ciout:
4108c2ecf20Sopenharmony_ci	channel->rx_pkt_n_frags = 0;
4118c2ecf20Sopenharmony_ci}
412