xref: /linux/drivers/net/ethernet/sfc/rx.c (revision 03c11eb3b16dc0058589751dfd91f254be2be613)
1d2912cb1SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
2874aeea5SJeff Kirsher /****************************************************************************
3f7a6d2c4SBen Hutchings  * Driver for Solarflare network controllers and boards
4874aeea5SJeff Kirsher  * Copyright 2005-2006 Fen Systems Ltd.
5f7a6d2c4SBen Hutchings  * Copyright 2005-2013 Solarflare Communications Inc.
6874aeea5SJeff Kirsher  */
7874aeea5SJeff Kirsher 
8874aeea5SJeff Kirsher #include <linux/socket.h>
9874aeea5SJeff Kirsher #include <linux/in.h>
10874aeea5SJeff Kirsher #include <linux/slab.h>
11874aeea5SJeff Kirsher #include <linux/ip.h>
12c47b2d9dSBen Hutchings #include <linux/ipv6.h>
13874aeea5SJeff Kirsher #include <linux/tcp.h>
14874aeea5SJeff Kirsher #include <linux/udp.h>
15874aeea5SJeff Kirsher #include <linux/prefetch.h>
166eb07cafSPaul Gortmaker #include <linux/moduleparam.h>
172768935aSDaniel Pieczko #include <linux/iommu.h>
18874aeea5SJeff Kirsher #include <net/ip.h>
19874aeea5SJeff Kirsher #include <net/checksum.h>
20eb9a36beSCharles McLachlan #include <net/xdp.h>
21eb9a36beSCharles McLachlan #include <linux/bpf_trace.h>
22874aeea5SJeff Kirsher #include "net_driver.h"
23874aeea5SJeff Kirsher #include "efx.h"
24e1253f39SAlex Maftei (amaftei) #include "rx_common.h"
25add72477SBen Hutchings #include "filter.h"
26874aeea5SJeff Kirsher #include "nic.h"
27874aeea5SJeff Kirsher #include "selftest.h"
28874aeea5SJeff Kirsher #include "workarounds.h"
29874aeea5SJeff Kirsher 
301648a23fSDaniel Pieczko /* Preferred number of descriptors to fill at once */
311648a23fSDaniel Pieczko #define EFX_RX_PREFERRED_BATCH 8U
32874aeea5SJeff Kirsher 
33eb9a36beSCharles McLachlan /* Maximum rx prefix used by any architecture. */
34eb9a36beSCharles McLachlan #define EFX_MAX_RX_PREFIX_SIZE 16
35eb9a36beSCharles McLachlan 
36874aeea5SJeff Kirsher /* Size of buffer allocated for skb header area. */
37d4ef5b6fSJon Cooper #define EFX_SKB_HEADERS  128u
38874aeea5SJeff Kirsher 
3985740cdfSBen Hutchings /* Each packet can consume up to ceil(max_frame_len / buffer_size) buffers */
4085740cdfSBen Hutchings #define EFX_RX_MAX_FRAGS DIV_ROUND_UP(EFX_MAX_FRAME_LEN(EFX_MAX_MTU), \
4185740cdfSBen Hutchings 				      EFX_RX_USR_BUF_SIZE)
4285740cdfSBen Hutchings 
efx_rx_packet__check_len(struct efx_rx_queue * rx_queue,struct efx_rx_buffer * rx_buf,int len)43874aeea5SJeff Kirsher static void efx_rx_packet__check_len(struct efx_rx_queue *rx_queue,
44874aeea5SJeff Kirsher 				     struct efx_rx_buffer *rx_buf,
4597d48a10SAlexandre Rames 				     int len)
46874aeea5SJeff Kirsher {
47874aeea5SJeff Kirsher 	struct efx_nic *efx = rx_queue->efx;
48874aeea5SJeff Kirsher 	unsigned max_len = rx_buf->len - efx->type->rx_buffer_padding;
49874aeea5SJeff Kirsher 
50874aeea5SJeff Kirsher 	if (likely(len <= max_len))
51874aeea5SJeff Kirsher 		return;
52874aeea5SJeff Kirsher 
53874aeea5SJeff Kirsher 	/* The packet must be discarded, but this is only a fatal error
54874aeea5SJeff Kirsher 	 * if the caller indicated it was
55874aeea5SJeff Kirsher 	 */
56db339569SBen Hutchings 	rx_buf->flags |= EFX_RX_PKT_DISCARD;
57874aeea5SJeff Kirsher 
58874aeea5SJeff Kirsher 	if (net_ratelimit())
59874aeea5SJeff Kirsher 		netif_err(efx, rx_err, efx->net_dev,
605a6681e2SEdward Cree 			  "RX queue %d overlength RX event (%#x > %#x)\n",
61874aeea5SJeff Kirsher 			  efx_rx_queue_index(rx_queue), len, max_len);
62874aeea5SJeff Kirsher 
63874aeea5SJeff Kirsher 	efx_rx_queue_channel(rx_queue)->n_rx_overlength++;
64874aeea5SJeff Kirsher }
65874aeea5SJeff Kirsher 
6685740cdfSBen Hutchings /* Allocate and construct an SKB around page fragments */
efx_rx_mk_skb(struct efx_channel * channel,struct efx_rx_buffer * rx_buf,unsigned int n_frags,u8 * eh,int hdr_len)6797d48a10SAlexandre Rames static struct sk_buff *efx_rx_mk_skb(struct efx_channel *channel,
6897d48a10SAlexandre Rames 				     struct efx_rx_buffer *rx_buf,
6985740cdfSBen Hutchings 				     unsigned int n_frags,
7097d48a10SAlexandre Rames 				     u8 *eh, int hdr_len)
7197d48a10SAlexandre Rames {
7297d48a10SAlexandre Rames 	struct efx_nic *efx = channel->efx;
7397d48a10SAlexandre Rames 	struct sk_buff *skb;
7497d48a10SAlexandre Rames 
7597d48a10SAlexandre Rames 	/* Allocate an SKB to store the headers */
762ccd0b19SBen Hutchings 	skb = netdev_alloc_skb(efx->net_dev,
772ccd0b19SBen Hutchings 			       efx->rx_ip_align + efx->rx_prefix_size +
782ccd0b19SBen Hutchings 			       hdr_len);
79e4d112e4SEdward Cree 	if (unlikely(skb == NULL)) {
80e4d112e4SEdward Cree 		atomic_inc(&efx->n_rx_noskb_drops);
8197d48a10SAlexandre Rames 		return NULL;
82e4d112e4SEdward Cree 	}
8397d48a10SAlexandre Rames 
84e01b16a7SEdward Cree 	EFX_WARN_ON_ONCE_PARANOID(rx_buf->len < hdr_len);
8597d48a10SAlexandre Rames 
862ccd0b19SBen Hutchings 	memcpy(skb->data + efx->rx_ip_align, eh - efx->rx_prefix_size,
872ccd0b19SBen Hutchings 	       efx->rx_prefix_size + hdr_len);
882ccd0b19SBen Hutchings 	skb_reserve(skb, efx->rx_ip_align + efx->rx_prefix_size);
892ccd0b19SBen Hutchings 	__skb_put(skb, hdr_len);
9097d48a10SAlexandre Rames 
9185740cdfSBen Hutchings 	/* Append the remaining page(s) onto the frag list */
9297d48a10SAlexandre Rames 	if (rx_buf->len > hdr_len) {
9385740cdfSBen Hutchings 		rx_buf->page_offset += hdr_len;
9485740cdfSBen Hutchings 		rx_buf->len -= hdr_len;
9585740cdfSBen Hutchings 
9685740cdfSBen Hutchings 		for (;;) {
97c438a801SChristophe JAILLET 			skb_add_rx_frag(skb, skb_shinfo(skb)->nr_frags,
9885740cdfSBen Hutchings 					rx_buf->page, rx_buf->page_offset,
99c438a801SChristophe JAILLET 					rx_buf->len, efx->rx_buffer_truesize);
10085740cdfSBen Hutchings 			rx_buf->page = NULL;
101c438a801SChristophe JAILLET 
10285740cdfSBen Hutchings 			if (skb_shinfo(skb)->nr_frags == n_frags)
10385740cdfSBen Hutchings 				break;
10485740cdfSBen Hutchings 
10585740cdfSBen Hutchings 			rx_buf = efx_rx_buf_next(&channel->rx_queue, rx_buf);
10685740cdfSBen Hutchings 		}
10797d48a10SAlexandre Rames 	} else {
10897d48a10SAlexandre Rames 		__free_pages(rx_buf->page, efx->rx_buffer_order);
10985740cdfSBen Hutchings 		rx_buf->page = NULL;
11085740cdfSBen Hutchings 		n_frags = 0;
11197d48a10SAlexandre Rames 	}
11297d48a10SAlexandre Rames 
11397d48a10SAlexandre Rames 	/* Move past the ethernet header */
11497d48a10SAlexandre Rames 	skb->protocol = eth_type_trans(skb, efx->net_dev);
11597d48a10SAlexandre Rames 
11636763266SAlexandre Rames 	skb_mark_napi_id(skb, &channel->napi_str);
11736763266SAlexandre Rames 
11897d48a10SAlexandre Rames 	return skb;
119874aeea5SJeff Kirsher }
120874aeea5SJeff Kirsher 
efx_rx_packet(struct efx_rx_queue * rx_queue,unsigned int index,unsigned int n_frags,unsigned int len,u16 flags)121874aeea5SJeff Kirsher void efx_rx_packet(struct efx_rx_queue *rx_queue, unsigned int index,
12285740cdfSBen Hutchings 		   unsigned int n_frags, unsigned int len, u16 flags)
123874aeea5SJeff Kirsher {
124874aeea5SJeff Kirsher 	struct efx_nic *efx = rx_queue->efx;
125874aeea5SJeff Kirsher 	struct efx_channel *channel = efx_rx_queue_channel(rx_queue);
126874aeea5SJeff Kirsher 	struct efx_rx_buffer *rx_buf;
127874aeea5SJeff Kirsher 
1288ccf3800SAndrew Rybchenko 	rx_queue->rx_packets++;
1298ccf3800SAndrew Rybchenko 
130874aeea5SJeff Kirsher 	rx_buf = efx_rx_buffer(rx_queue, index);
131179ea7f0SBen Hutchings 	rx_buf->flags |= flags;
132874aeea5SJeff Kirsher 
13385740cdfSBen Hutchings 	/* Validate the number of fragments and completed length */
13485740cdfSBen Hutchings 	if (n_frags == 1) {
1353dced740SBen Hutchings 		if (!(flags & EFX_RX_PKT_PREFIX_LEN))
13697d48a10SAlexandre Rames 			efx_rx_packet__check_len(rx_queue, rx_buf, len);
13785740cdfSBen Hutchings 	} else if (unlikely(n_frags > EFX_RX_MAX_FRAGS) ||
138e8c68c0aSJon Cooper 		   unlikely(len <= (n_frags - 1) * efx->rx_dma_len) ||
139e8c68c0aSJon Cooper 		   unlikely(len > n_frags * efx->rx_dma_len) ||
14085740cdfSBen Hutchings 		   unlikely(!efx->rx_scatter)) {
14185740cdfSBen Hutchings 		/* If this isn't an explicit discard request, either
14285740cdfSBen Hutchings 		 * the hardware or the driver is broken.
14385740cdfSBen Hutchings 		 */
14485740cdfSBen Hutchings 		WARN_ON(!(len == 0 && rx_buf->flags & EFX_RX_PKT_DISCARD));
14585740cdfSBen Hutchings 		rx_buf->flags |= EFX_RX_PKT_DISCARD;
14685740cdfSBen Hutchings 	}
147874aeea5SJeff Kirsher 
148874aeea5SJeff Kirsher 	netif_vdbg(efx, rx_status, efx->net_dev,
14985740cdfSBen Hutchings 		   "RX queue %d received ids %x-%x len %d %s%s\n",
150874aeea5SJeff Kirsher 		   efx_rx_queue_index(rx_queue), index,
15185740cdfSBen Hutchings 		   (index + n_frags - 1) & rx_queue->ptr_mask, len,
152db339569SBen Hutchings 		   (rx_buf->flags & EFX_RX_PKT_CSUMMED) ? " [SUMMED]" : "",
153db339569SBen Hutchings 		   (rx_buf->flags & EFX_RX_PKT_DISCARD) ? " [DISCARD]" : "");
154874aeea5SJeff Kirsher 
15585740cdfSBen Hutchings 	/* Discard packet, if instructed to do so.  Process the
15685740cdfSBen Hutchings 	 * previous receive first.
15785740cdfSBen Hutchings 	 */
158db339569SBen Hutchings 	if (unlikely(rx_buf->flags & EFX_RX_PKT_DISCARD)) {
15985740cdfSBen Hutchings 		efx_rx_flush_packet(channel);
160734d4e15SBen Hutchings 		efx_discard_rx_packet(channel, rx_buf, n_frags);
16185740cdfSBen Hutchings 		return;
162874aeea5SJeff Kirsher 	}
163874aeea5SJeff Kirsher 
1643dced740SBen Hutchings 	if (n_frags == 1 && !(flags & EFX_RX_PKT_PREFIX_LEN))
16585740cdfSBen Hutchings 		rx_buf->len = len;
16685740cdfSBen Hutchings 
1672768935aSDaniel Pieczko 	/* Release and/or sync the DMA mapping - assumes all RX buffers
1682768935aSDaniel Pieczko 	 * consumed in-order per RX queue.
169874aeea5SJeff Kirsher 	 */
1702768935aSDaniel Pieczko 	efx_sync_rx_buffer(efx, rx_buf, rx_buf->len);
171874aeea5SJeff Kirsher 
172874aeea5SJeff Kirsher 	/* Prefetch nice and early so data will (hopefully) be in cache by
173874aeea5SJeff Kirsher 	 * the time we look at it.
174874aeea5SJeff Kirsher 	 */
1755036b7c7SBen Hutchings 	prefetch(efx_rx_buf_va(rx_buf));
176874aeea5SJeff Kirsher 
17743a3739dSJon Cooper 	rx_buf->page_offset += efx->rx_prefix_size;
17843a3739dSJon Cooper 	rx_buf->len -= efx->rx_prefix_size;
17985740cdfSBen Hutchings 
18085740cdfSBen Hutchings 	if (n_frags > 1) {
18185740cdfSBen Hutchings 		/* Release/sync DMA mapping for additional fragments.
18285740cdfSBen Hutchings 		 * Fix length for last fragment.
18385740cdfSBen Hutchings 		 */
18485740cdfSBen Hutchings 		unsigned int tail_frags = n_frags - 1;
18585740cdfSBen Hutchings 
18685740cdfSBen Hutchings 		for (;;) {
18785740cdfSBen Hutchings 			rx_buf = efx_rx_buf_next(rx_queue, rx_buf);
18885740cdfSBen Hutchings 			if (--tail_frags == 0)
18985740cdfSBen Hutchings 				break;
190e8c68c0aSJon Cooper 			efx_sync_rx_buffer(efx, rx_buf, efx->rx_dma_len);
19185740cdfSBen Hutchings 		}
192e8c68c0aSJon Cooper 		rx_buf->len = len - (n_frags - 1) * efx->rx_dma_len;
1932768935aSDaniel Pieczko 		efx_sync_rx_buffer(efx, rx_buf, rx_buf->len);
19485740cdfSBen Hutchings 	}
195b74e3e8cSBen Hutchings 
196734d4e15SBen Hutchings 	/* All fragments have been DMA-synced, so recycle pages. */
1972768935aSDaniel Pieczko 	rx_buf = efx_rx_buffer(rx_queue, index);
198734d4e15SBen Hutchings 	efx_recycle_rx_pages(channel, rx_buf, n_frags);
1992768935aSDaniel Pieczko 
200874aeea5SJeff Kirsher 	/* Pipeline receives so that we give time for packet headers to be
201874aeea5SJeff Kirsher 	 * prefetched into cache.
202874aeea5SJeff Kirsher 	 */
203ff734ef4SBen Hutchings 	efx_rx_flush_packet(channel);
20485740cdfSBen Hutchings 	channel->rx_pkt_n_frags = n_frags;
20585740cdfSBen Hutchings 	channel->rx_pkt_index = index;
206874aeea5SJeff Kirsher }
207874aeea5SJeff Kirsher 
efx_rx_deliver(struct efx_channel * channel,u8 * eh,struct efx_rx_buffer * rx_buf,unsigned int n_frags)20897d48a10SAlexandre Rames static void efx_rx_deliver(struct efx_channel *channel, u8 *eh,
20985740cdfSBen Hutchings 			   struct efx_rx_buffer *rx_buf,
21085740cdfSBen Hutchings 			   unsigned int n_frags)
2111ddceb4cSBen Hutchings {
2121ddceb4cSBen Hutchings 	struct sk_buff *skb;
21397d48a10SAlexandre Rames 	u16 hdr_len = min_t(u16, rx_buf->len, EFX_SKB_HEADERS);
2141ddceb4cSBen Hutchings 
21585740cdfSBen Hutchings 	skb = efx_rx_mk_skb(channel, rx_buf, n_frags, eh, hdr_len);
21697d48a10SAlexandre Rames 	if (unlikely(skb == NULL)) {
2179eb0a5d1SDaniel Pieczko 		struct efx_rx_queue *rx_queue;
2189eb0a5d1SDaniel Pieczko 
2199eb0a5d1SDaniel Pieczko 		rx_queue = efx_channel_get_rx_queue(channel);
2209eb0a5d1SDaniel Pieczko 		efx_free_rx_buffers(rx_queue, rx_buf, n_frags);
22197d48a10SAlexandre Rames 		return;
22297d48a10SAlexandre Rames 	}
22397d48a10SAlexandre Rames 	skb_record_rx_queue(skb, channel->rx_queue.core_index);
2241ddceb4cSBen Hutchings 
2251ddceb4cSBen Hutchings 	/* Set the SKB flags */
2261ddceb4cSBen Hutchings 	skb_checksum_none_assert(skb);
227da50ae2eSJon Cooper 	if (likely(rx_buf->flags & EFX_RX_PKT_CSUMMED)) {
228c99dffc4SJon Cooper 		skb->ip_summed = CHECKSUM_UNNECESSARY;
229da50ae2eSJon Cooper 		skb->csum_level = !!(rx_buf->flags & EFX_RX_PKT_CSUM_LEVEL);
230da50ae2eSJon Cooper 	}
2311ddceb4cSBen Hutchings 
232bd9a265dSJon Cooper 	efx_rx_skb_attach_timestamp(channel, skb);
233bd9a265dSJon Cooper 
234c31e5f9fSStuart Hodgson 	if (channel->type->receive_skb)
2354a74dc65SBen Hutchings 		if (channel->type->receive_skb(channel, skb))
23697d48a10SAlexandre Rames 			return;
2374a74dc65SBen Hutchings 
2384a74dc65SBen Hutchings 	/* Pass the packet up */
239e090bfb9SEdward Cree 	if (channel->rx_list != NULL)
240e090bfb9SEdward Cree 		/* Add to list, will pass up later */
241e090bfb9SEdward Cree 		list_add_tail(&skb->list, channel->rx_list);
242e090bfb9SEdward Cree 	else
243e090bfb9SEdward Cree 		/* No list, so pass it up now */
2441ddceb4cSBen Hutchings 		netif_receive_skb(skb);
2451ddceb4cSBen Hutchings }
2461ddceb4cSBen Hutchings 
247eb9a36beSCharles McLachlan /** efx_do_xdp: perform XDP processing on a received packet
248eb9a36beSCharles McLachlan  *
249eb9a36beSCharles McLachlan  * Returns true if packet should still be delivered.
250eb9a36beSCharles McLachlan  */
efx_do_xdp(struct efx_nic * efx,struct efx_channel * channel,struct efx_rx_buffer * rx_buf,u8 ** ehp)251eb9a36beSCharles McLachlan static bool efx_do_xdp(struct efx_nic *efx, struct efx_channel *channel,
252eb9a36beSCharles McLachlan 		       struct efx_rx_buffer *rx_buf, u8 **ehp)
253eb9a36beSCharles McLachlan {
254eb9a36beSCharles McLachlan 	u8 rx_prefix[EFX_MAX_RX_PREFIX_SIZE];
255eb9a36beSCharles McLachlan 	struct efx_rx_queue *rx_queue;
256eb9a36beSCharles McLachlan 	struct bpf_prog *xdp_prog;
257dfe44c1fSCharles McLachlan 	struct xdp_frame *xdpf;
258eb9a36beSCharles McLachlan 	struct xdp_buff xdp;
259eb9a36beSCharles McLachlan 	u32 xdp_act;
260eb9a36beSCharles McLachlan 	s16 offset;
261eb9a36beSCharles McLachlan 	int err;
262eb9a36beSCharles McLachlan 
2634eb14e3fSToke Høiland-Jørgensen 	xdp_prog = rcu_dereference_bh(efx->xdp_prog);
2644eb14e3fSToke Høiland-Jørgensen 	if (!xdp_prog)
265eb9a36beSCharles McLachlan 		return true;
266eb9a36beSCharles McLachlan 
267eb9a36beSCharles McLachlan 	rx_queue = efx_channel_get_rx_queue(channel);
268eb9a36beSCharles McLachlan 
269eb9a36beSCharles McLachlan 	if (unlikely(channel->rx_pkt_n_frags > 1)) {
270eb9a36beSCharles McLachlan 		/* We can't do XDP on fragmented packets - drop. */
271eb9a36beSCharles McLachlan 		efx_free_rx_buffers(rx_queue, rx_buf,
272eb9a36beSCharles McLachlan 				    channel->rx_pkt_n_frags);
273eb9a36beSCharles McLachlan 		if (net_ratelimit())
274eb9a36beSCharles McLachlan 			netif_err(efx, rx_err, efx->net_dev,
275eb9a36beSCharles McLachlan 				  "XDP is not possible with multiple receive fragments (%d)\n",
276eb9a36beSCharles McLachlan 				  channel->rx_pkt_n_frags);
277cd846befSCharles McLachlan 		channel->n_rx_xdp_bad_drops++;
278eb9a36beSCharles McLachlan 		return false;
279eb9a36beSCharles McLachlan 	}
280eb9a36beSCharles McLachlan 
281eb9a36beSCharles McLachlan 	dma_sync_single_for_cpu(&efx->pci_dev->dev, rx_buf->dma_addr,
282eb9a36beSCharles McLachlan 				rx_buf->len, DMA_FROM_DEVICE);
283eb9a36beSCharles McLachlan 
284eb9a36beSCharles McLachlan 	/* Save the rx prefix. */
285eb9a36beSCharles McLachlan 	EFX_WARN_ON_PARANOID(efx->rx_prefix_size > EFX_MAX_RX_PREFIX_SIZE);
286eb9a36beSCharles McLachlan 	memcpy(rx_prefix, *ehp - efx->rx_prefix_size,
287eb9a36beSCharles McLachlan 	       efx->rx_prefix_size);
288eb9a36beSCharles McLachlan 
28943b5169dSLorenzo Bianconi 	xdp_init_buff(&xdp, efx->rx_page_buf_step, &rx_queue->xdp_rxq_info);
290eb9a36beSCharles McLachlan 	/* No support yet for XDP metadata */
291be9df4afSLorenzo Bianconi 	xdp_prepare_buff(&xdp, *ehp - EFX_XDP_HEADROOM, EFX_XDP_HEADROOM,
292be9df4afSLorenzo Bianconi 			 rx_buf->len, false);
293eb9a36beSCharles McLachlan 
294eb9a36beSCharles McLachlan 	xdp_act = bpf_prog_run_xdp(xdp_prog, &xdp);
295eb9a36beSCharles McLachlan 
296eb9a36beSCharles McLachlan 	offset = (u8 *)xdp.data - *ehp;
297eb9a36beSCharles McLachlan 
298eb9a36beSCharles McLachlan 	switch (xdp_act) {
299eb9a36beSCharles McLachlan 	case XDP_PASS:
300eb9a36beSCharles McLachlan 		/* Fix up rx prefix. */
301eb9a36beSCharles McLachlan 		if (offset) {
302eb9a36beSCharles McLachlan 			*ehp += offset;
303eb9a36beSCharles McLachlan 			rx_buf->page_offset += offset;
304eb9a36beSCharles McLachlan 			rx_buf->len -= offset;
305eb9a36beSCharles McLachlan 			memcpy(*ehp - efx->rx_prefix_size, rx_prefix,
306eb9a36beSCharles McLachlan 			       efx->rx_prefix_size);
307eb9a36beSCharles McLachlan 		}
308eb9a36beSCharles McLachlan 		break;
309eb9a36beSCharles McLachlan 
310eb9a36beSCharles McLachlan 	case XDP_TX:
311dfe44c1fSCharles McLachlan 		/* Buffer ownership passes to tx on success. */
3121b698fa5SLorenzo Bianconi 		xdpf = xdp_convert_buff_to_frame(&xdp);
313dfe44c1fSCharles McLachlan 		err = efx_xdp_tx_buffers(efx, 1, &xdpf, true);
314dfe44c1fSCharles McLachlan 		if (unlikely(err != 1)) {
315dfe44c1fSCharles McLachlan 			efx_free_rx_buffers(rx_queue, rx_buf, 1);
316dfe44c1fSCharles McLachlan 			if (net_ratelimit())
317dfe44c1fSCharles McLachlan 				netif_err(efx, rx_err, efx->net_dev,
318dfe44c1fSCharles McLachlan 					  "XDP TX failed (%d)\n", err);
319cd846befSCharles McLachlan 			channel->n_rx_xdp_bad_drops++;
3209440a875SArthur Fabre 			trace_xdp_exception(efx->net_dev, xdp_prog, xdp_act);
321cd846befSCharles McLachlan 		} else {
322cd846befSCharles McLachlan 			channel->n_rx_xdp_tx++;
323dfe44c1fSCharles McLachlan 		}
324dfe44c1fSCharles McLachlan 		break;
325eb9a36beSCharles McLachlan 
326eb9a36beSCharles McLachlan 	case XDP_REDIRECT:
327eb9a36beSCharles McLachlan 		err = xdp_do_redirect(efx->net_dev, &xdp, xdp_prog);
328eb9a36beSCharles McLachlan 		if (unlikely(err)) {
329eb9a36beSCharles McLachlan 			efx_free_rx_buffers(rx_queue, rx_buf, 1);
330eb9a36beSCharles McLachlan 			if (net_ratelimit())
331eb9a36beSCharles McLachlan 				netif_err(efx, rx_err, efx->net_dev,
332eb9a36beSCharles McLachlan 					  "XDP redirect failed (%d)\n", err);
333cd846befSCharles McLachlan 			channel->n_rx_xdp_bad_drops++;
3349440a875SArthur Fabre 			trace_xdp_exception(efx->net_dev, xdp_prog, xdp_act);
335cd846befSCharles McLachlan 		} else {
336cd846befSCharles McLachlan 			channel->n_rx_xdp_redirect++;
337eb9a36beSCharles McLachlan 		}
338eb9a36beSCharles McLachlan 		break;
339eb9a36beSCharles McLachlan 
340eb9a36beSCharles McLachlan 	default:
341c8064e5bSPaolo Abeni 		bpf_warn_invalid_xdp_action(efx->net_dev, xdp_prog, xdp_act);
342eb9a36beSCharles McLachlan 		efx_free_rx_buffers(rx_queue, rx_buf, 1);
343cd846befSCharles McLachlan 		channel->n_rx_xdp_bad_drops++;
3449440a875SArthur Fabre 		trace_xdp_exception(efx->net_dev, xdp_prog, xdp_act);
345eb9a36beSCharles McLachlan 		break;
346eb9a36beSCharles McLachlan 
347eb9a36beSCharles McLachlan 	case XDP_ABORTED:
348eb9a36beSCharles McLachlan 		trace_xdp_exception(efx->net_dev, xdp_prog, xdp_act);
349df561f66SGustavo A. R. Silva 		fallthrough;
350eb9a36beSCharles McLachlan 	case XDP_DROP:
351eb9a36beSCharles McLachlan 		efx_free_rx_buffers(rx_queue, rx_buf, 1);
352cd846befSCharles McLachlan 		channel->n_rx_xdp_drops++;
353eb9a36beSCharles McLachlan 		break;
354eb9a36beSCharles McLachlan 	}
355eb9a36beSCharles McLachlan 
356eb9a36beSCharles McLachlan 	return xdp_act == XDP_PASS;
357eb9a36beSCharles McLachlan }
358eb9a36beSCharles McLachlan 
359874aeea5SJeff Kirsher /* Handle a received packet.  Second half: Touches packet payload. */
__efx_rx_packet(struct efx_channel * channel)36085740cdfSBen Hutchings void __efx_rx_packet(struct efx_channel *channel)
361874aeea5SJeff Kirsher {
362*ae074e2bSEdward Cree 	struct efx_rx_queue *rx_queue = efx_channel_get_rx_queue(channel);
363874aeea5SJeff Kirsher 	struct efx_nic *efx = channel->efx;
36485740cdfSBen Hutchings 	struct efx_rx_buffer *rx_buf =
365*ae074e2bSEdward Cree 		efx_rx_buffer(rx_queue, channel->rx_pkt_index);
366b74e3e8cSBen Hutchings 	u8 *eh = efx_rx_buf_va(rx_buf);
367874aeea5SJeff Kirsher 
3683dced740SBen Hutchings 	/* Read length from the prefix if necessary.  This already
3693dced740SBen Hutchings 	 * excludes the length of the prefix itself.
3703dced740SBen Hutchings 	 */
371*ae074e2bSEdward Cree 	if (rx_buf->flags & EFX_RX_PKT_PREFIX_LEN) {
3723dced740SBen Hutchings 		rx_buf->len = le16_to_cpup((__le16 *)
3733dced740SBen Hutchings 					   (eh + efx->rx_packet_len_offset));
374*ae074e2bSEdward Cree 		/* A known issue may prevent this being filled in;
375*ae074e2bSEdward Cree 		 * if that happens, just drop the packet.
376*ae074e2bSEdward Cree 		 * Must do that in the driver since passing a zero-length
377*ae074e2bSEdward Cree 		 * packet up to the stack may cause a crash.
378*ae074e2bSEdward Cree 		 */
379*ae074e2bSEdward Cree 		if (unlikely(!rx_buf->len)) {
380*ae074e2bSEdward Cree 			efx_free_rx_buffers(rx_queue, rx_buf,
381*ae074e2bSEdward Cree 					    channel->rx_pkt_n_frags);
382*ae074e2bSEdward Cree 			channel->n_rx_frm_trunc++;
383*ae074e2bSEdward Cree 			goto out;
384*ae074e2bSEdward Cree 		}
385*ae074e2bSEdward Cree 	}
3863dced740SBen Hutchings 
387874aeea5SJeff Kirsher 	/* If we're in loopback test, then pass the packet directly to the
388874aeea5SJeff Kirsher 	 * loopback layer, and free the rx_buf here
389874aeea5SJeff Kirsher 	 */
390874aeea5SJeff Kirsher 	if (unlikely(efx->loopback_selftest)) {
391874aeea5SJeff Kirsher 		efx_loopback_rx_packet(efx, eh, rx_buf->len);
3929eb0a5d1SDaniel Pieczko 		efx_free_rx_buffers(rx_queue, rx_buf,
3939eb0a5d1SDaniel Pieczko 				    channel->rx_pkt_n_frags);
39485740cdfSBen Hutchings 		goto out;
395874aeea5SJeff Kirsher 	}
396874aeea5SJeff Kirsher 
397eb9a36beSCharles McLachlan 	if (!efx_do_xdp(efx, channel, rx_buf, &eh))
398eb9a36beSCharles McLachlan 		goto out;
399eb9a36beSCharles McLachlan 
400874aeea5SJeff Kirsher 	if (unlikely(!(efx->net_dev->features & NETIF_F_RXCSUM)))
401db339569SBen Hutchings 		rx_buf->flags &= ~EFX_RX_PKT_CSUMMED;
402874aeea5SJeff Kirsher 
403e7fe9491SEric Dumazet 	if ((rx_buf->flags & EFX_RX_PKT_TCP) && !channel->type->receive_skb)
4044d9c0a2dSEdward Cree 		efx_rx_packet_gro(channel, rx_buf, channel->rx_pkt_n_frags, eh, 0);
4051ddceb4cSBen Hutchings 	else
40685740cdfSBen Hutchings 		efx_rx_deliver(channel, eh, rx_buf, channel->rx_pkt_n_frags);
40785740cdfSBen Hutchings out:
40885740cdfSBen Hutchings 	channel->rx_pkt_n_frags = 0;
409874aeea5SJeff Kirsher }
410