xref: /linux/drivers/net/ethernet/netronome/nfp/nfdk/dp.c (revision ad30469a841b50dbb541df4d6971d891f703c297)
1 // SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)
2 /* Copyright (C) 2015-2019 Netronome Systems, Inc. */
3 
4 #include <linux/bpf_trace.h>
5 #include <linux/netdevice.h>
6 #include <linux/overflow.h>
7 #include <linux/sizes.h>
8 #include <linux/bitfield.h>
9 #include <net/xfrm.h>
10 
11 #include "../nfp_app.h"
12 #include "../nfp_net.h"
13 #include "../nfp_net_dp.h"
14 #include "../crypto/crypto.h"
15 #include "../crypto/fw.h"
16 #include "nfdk.h"
17 
18 static int nfp_nfdk_tx_ring_should_wake(struct nfp_net_tx_ring *tx_ring)
19 {
20 	return !nfp_net_tx_full(tx_ring, NFDK_TX_DESC_STOP_CNT * 2);
21 }
22 
23 static int nfp_nfdk_tx_ring_should_stop(struct nfp_net_tx_ring *tx_ring)
24 {
25 	return nfp_net_tx_full(tx_ring, NFDK_TX_DESC_STOP_CNT);
26 }
27 
28 static void nfp_nfdk_tx_ring_stop(struct netdev_queue *nd_q,
29 				  struct nfp_net_tx_ring *tx_ring)
30 {
31 	netif_tx_stop_queue(nd_q);
32 
33 	/* We can race with the TX completion out of NAPI so recheck */
34 	smp_mb();
35 	if (unlikely(nfp_nfdk_tx_ring_should_wake(tx_ring)))
36 		netif_tx_start_queue(nd_q);
37 }
38 
39 static __le64
40 nfp_nfdk_tx_tso(struct nfp_net_r_vector *r_vec, struct nfp_nfdk_tx_buf *txbuf,
41 		struct sk_buff *skb)
42 {
43 	u32 segs, hdrlen, l3_offset, l4_offset;
44 	struct nfp_nfdk_tx_desc txd;
45 	u16 mss;
46 
47 	if (!skb->encapsulation) {
48 		l3_offset = skb_network_offset(skb);
49 		l4_offset = skb_transport_offset(skb);
50 		hdrlen = skb_tcp_all_headers(skb);
51 	} else {
52 		l3_offset = skb_inner_network_offset(skb);
53 		l4_offset = skb_inner_transport_offset(skb);
54 		hdrlen = skb_inner_tcp_all_headers(skb);
55 	}
56 
57 	segs = skb_shinfo(skb)->gso_segs;
58 	mss = skb_shinfo(skb)->gso_size & NFDK_DESC_TX_MSS_MASK;
59 
60 	txd.l3_offset = l3_offset;
61 	txd.l4_offset = l4_offset;
62 	txd.lso_meta_res = 0;
63 	txd.mss = cpu_to_le16(mss);
64 	txd.lso_hdrlen = hdrlen;
65 	txd.lso_totsegs = segs;
66 
67 	txbuf->pkt_cnt = segs;
68 	txbuf->real_len = skb->len + hdrlen * (txbuf->pkt_cnt - 1);
69 
70 	u64_stats_update_begin(&r_vec->tx_sync);
71 	r_vec->tx_lso++;
72 	u64_stats_update_end(&r_vec->tx_sync);
73 
74 	return txd.raw;
75 }
76 
77 static u8
78 nfp_nfdk_tx_csum(struct nfp_net_dp *dp, struct nfp_net_r_vector *r_vec,
79 		 unsigned int pkt_cnt, struct sk_buff *skb, u64 flags)
80 {
81 	struct ipv6hdr *ipv6h;
82 	struct iphdr *iph;
83 
84 	if (!(dp->ctrl & NFP_NET_CFG_CTRL_TXCSUM))
85 		return flags;
86 
87 	if (skb->ip_summed != CHECKSUM_PARTIAL)
88 		return flags;
89 
90 	flags |= NFDK_DESC_TX_L4_CSUM;
91 
92 	iph = skb->encapsulation ? inner_ip_hdr(skb) : ip_hdr(skb);
93 	ipv6h = skb->encapsulation ? inner_ipv6_hdr(skb) : ipv6_hdr(skb);
94 
95 	/* L3 checksum offloading flag is not required for ipv6 */
96 	if (iph->version == 4) {
97 		flags |= NFDK_DESC_TX_L3_CSUM;
98 	} else if (ipv6h->version != 6) {
99 		nn_dp_warn(dp, "partial checksum but ipv=%x!\n", iph->version);
100 		return flags;
101 	}
102 
103 	u64_stats_update_begin(&r_vec->tx_sync);
104 	if (!skb->encapsulation) {
105 		r_vec->hw_csum_tx += pkt_cnt;
106 	} else {
107 		flags |= NFDK_DESC_TX_ENCAP;
108 		r_vec->hw_csum_tx_inner += pkt_cnt;
109 	}
110 	u64_stats_update_end(&r_vec->tx_sync);
111 
112 	return flags;
113 }
114 
115 static int
116 nfp_nfdk_tx_maybe_close_block(struct nfp_net_tx_ring *tx_ring,
117 			      struct sk_buff *skb)
118 {
119 	unsigned int n_descs, wr_p, nop_slots;
120 	const skb_frag_t *frag, *fend;
121 	struct nfp_nfdk_tx_desc *txd;
122 	unsigned int nr_frags;
123 	unsigned int wr_idx;
124 	int err;
125 
126 recount_descs:
127 	n_descs = nfp_nfdk_headlen_to_segs(skb_headlen(skb));
128 	nr_frags = skb_shinfo(skb)->nr_frags;
129 	frag = skb_shinfo(skb)->frags;
130 	fend = frag + nr_frags;
131 	for (; frag < fend; frag++)
132 		n_descs += DIV_ROUND_UP(skb_frag_size(frag),
133 					NFDK_TX_MAX_DATA_PER_DESC);
134 
135 	if (unlikely(n_descs > NFDK_TX_DESC_GATHER_MAX)) {
136 		if (skb_is_nonlinear(skb)) {
137 			err = skb_linearize(skb);
138 			if (err)
139 				return err;
140 			goto recount_descs;
141 		}
142 		return -EINVAL;
143 	}
144 
145 	/* Under count by 1 (don't count meta) for the round down to work out */
146 	n_descs += !!skb_is_gso(skb);
147 
148 	if (round_down(tx_ring->wr_p, NFDK_TX_DESC_BLOCK_CNT) !=
149 	    round_down(tx_ring->wr_p + n_descs, NFDK_TX_DESC_BLOCK_CNT))
150 		goto close_block;
151 
152 	if ((u32)tx_ring->data_pending + skb->len > NFDK_TX_MAX_DATA_PER_BLOCK)
153 		goto close_block;
154 
155 	return 0;
156 
157 close_block:
158 	wr_p = tx_ring->wr_p;
159 	nop_slots = D_BLOCK_CPL(wr_p);
160 
161 	wr_idx = D_IDX(tx_ring, wr_p);
162 	tx_ring->ktxbufs[wr_idx].skb = NULL;
163 	txd = &tx_ring->ktxds[wr_idx];
164 
165 	memset(txd, 0, array_size(nop_slots, sizeof(struct nfp_nfdk_tx_desc)));
166 
167 	tx_ring->data_pending = 0;
168 	tx_ring->wr_p += nop_slots;
169 	tx_ring->wr_ptr_add += nop_slots;
170 
171 	return 0;
172 }
173 
174 static int
175 nfp_nfdk_prep_tx_meta(struct nfp_net_dp *dp, struct nfp_app *app,
176 		      struct sk_buff *skb, bool *ipsec)
177 {
178 	struct metadata_dst *md_dst = skb_metadata_dst(skb);
179 	struct nfp_ipsec_offload offload_info;
180 	unsigned char *data;
181 	bool vlan_insert;
182 	u32 meta_id = 0;
183 	int md_bytes;
184 
185 #ifdef CONFIG_NFP_NET_IPSEC
186 	if (xfrm_offload(skb))
187 		*ipsec = nfp_net_ipsec_tx_prep(dp, skb, &offload_info);
188 #endif
189 
190 	if (unlikely(md_dst && md_dst->type != METADATA_HW_PORT_MUX))
191 		md_dst = NULL;
192 
193 	vlan_insert = skb_vlan_tag_present(skb) && (dp->ctrl & NFP_NET_CFG_CTRL_TXVLAN_V2);
194 
195 	if (!(md_dst || vlan_insert || *ipsec))
196 		return 0;
197 
198 	md_bytes = sizeof(meta_id) +
199 		   (!!md_dst ? NFP_NET_META_PORTID_SIZE : 0) +
200 		   (vlan_insert ? NFP_NET_META_VLAN_SIZE : 0) +
201 		   (*ipsec ? NFP_NET_META_IPSEC_FIELD_SIZE : 0);
202 
203 	if (unlikely(skb_cow_head(skb, md_bytes)))
204 		return -ENOMEM;
205 
206 	data = skb_push(skb, md_bytes) + md_bytes;
207 	if (md_dst) {
208 		data -= NFP_NET_META_PORTID_SIZE;
209 		put_unaligned_be32(md_dst->u.port_info.port_id, data);
210 		meta_id = NFP_NET_META_PORTID;
211 	}
212 	if (vlan_insert) {
213 		data -= NFP_NET_META_VLAN_SIZE;
214 		/* data type of skb->vlan_proto is __be16
215 		 * so it fills metadata without calling put_unaligned_be16
216 		 */
217 		memcpy(data, &skb->vlan_proto, sizeof(skb->vlan_proto));
218 		put_unaligned_be16(skb_vlan_tag_get(skb), data + sizeof(skb->vlan_proto));
219 		meta_id <<= NFP_NET_META_FIELD_SIZE;
220 		meta_id |= NFP_NET_META_VLAN;
221 	}
222 
223 	if (*ipsec) {
224 		data -= NFP_NET_META_IPSEC_SIZE;
225 		put_unaligned_be32(offload_info.seq_hi, data);
226 		data -= NFP_NET_META_IPSEC_SIZE;
227 		put_unaligned_be32(offload_info.seq_low, data);
228 		data -= NFP_NET_META_IPSEC_SIZE;
229 		put_unaligned_be32(offload_info.handle - 1, data);
230 		meta_id <<= NFP_NET_META_IPSEC_FIELD_SIZE;
231 		meta_id |= NFP_NET_META_IPSEC << 8 | NFP_NET_META_IPSEC << 4 | NFP_NET_META_IPSEC;
232 	}
233 
234 	meta_id = FIELD_PREP(NFDK_META_LEN, md_bytes) |
235 		  FIELD_PREP(NFDK_META_FIELDS, meta_id);
236 
237 	data -= sizeof(meta_id);
238 	put_unaligned_be32(meta_id, data);
239 
240 	return NFDK_DESC_TX_CHAIN_META;
241 }
242 
243 /**
244  * nfp_nfdk_tx() - Main transmit entry point
245  * @skb:    SKB to transmit
246  * @netdev: netdev structure
247  *
248  * Return: NETDEV_TX_OK on success.
249  */
250 netdev_tx_t nfp_nfdk_tx(struct sk_buff *skb, struct net_device *netdev)
251 {
252 	struct nfp_net *nn = netdev_priv(netdev);
253 	struct nfp_nfdk_tx_buf *txbuf, *etxbuf;
254 	u32 cnt, tmp_dlen, dlen_type = 0;
255 	struct nfp_net_tx_ring *tx_ring;
256 	struct nfp_net_r_vector *r_vec;
257 	const skb_frag_t *frag, *fend;
258 	struct nfp_nfdk_tx_desc *txd;
259 	unsigned int real_len, qidx;
260 	unsigned int dma_len, type;
261 	struct netdev_queue *nd_q;
262 	struct nfp_net_dp *dp;
263 	int nr_frags, wr_idx;
264 	dma_addr_t dma_addr;
265 	bool ipsec = false;
266 	u64 metadata;
267 
268 	dp = &nn->dp;
269 	qidx = skb_get_queue_mapping(skb);
270 	tx_ring = &dp->tx_rings[qidx];
271 	r_vec = tx_ring->r_vec;
272 	nd_q = netdev_get_tx_queue(dp->netdev, qidx);
273 
274 	/* Don't bother counting frags, assume the worst */
275 	if (unlikely(nfp_net_tx_full(tx_ring, NFDK_TX_DESC_STOP_CNT))) {
276 		nn_dp_warn(dp, "TX ring %d busy. wrp=%u rdp=%u\n",
277 			   qidx, tx_ring->wr_p, tx_ring->rd_p);
278 		netif_tx_stop_queue(nd_q);
279 		nfp_net_tx_xmit_more_flush(tx_ring);
280 		u64_stats_update_begin(&r_vec->tx_sync);
281 		r_vec->tx_busy++;
282 		u64_stats_update_end(&r_vec->tx_sync);
283 		return NETDEV_TX_BUSY;
284 	}
285 
286 	metadata = nfp_nfdk_prep_tx_meta(dp, nn->app, skb, &ipsec);
287 	if (unlikely((int)metadata < 0))
288 		goto err_flush;
289 
290 	if (nfp_nfdk_tx_maybe_close_block(tx_ring, skb))
291 		goto err_flush;
292 
293 	/* nr_frags will change after skb_linearize so we get nr_frags after
294 	 * nfp_nfdk_tx_maybe_close_block function
295 	 */
296 	nr_frags = skb_shinfo(skb)->nr_frags;
297 	/* DMA map all */
298 	wr_idx = D_IDX(tx_ring, tx_ring->wr_p);
299 	txd = &tx_ring->ktxds[wr_idx];
300 	txbuf = &tx_ring->ktxbufs[wr_idx];
301 
302 	dma_len = skb_headlen(skb);
303 	if (skb_is_gso(skb))
304 		type = NFDK_DESC_TX_TYPE_TSO;
305 	else if (!nr_frags && dma_len <= NFDK_TX_MAX_DATA_PER_HEAD)
306 		type = NFDK_DESC_TX_TYPE_SIMPLE;
307 	else
308 		type = NFDK_DESC_TX_TYPE_GATHER;
309 
310 	dma_addr = dma_map_single(dp->dev, skb->data, dma_len, DMA_TO_DEVICE);
311 	if (dma_mapping_error(dp->dev, dma_addr))
312 		goto err_warn_dma;
313 
314 	txbuf->skb = skb;
315 	txbuf++;
316 
317 	txbuf->dma_addr = dma_addr;
318 	txbuf++;
319 
320 	/* FIELD_PREP() implicitly truncates to chunk */
321 	dma_len -= 1;
322 
323 	/* We will do our best to pass as much data as we can in descriptor
324 	 * and we need to make sure the first descriptor includes whole head
325 	 * since there is limitation in firmware side. Sometimes the value of
326 	 * dma_len bitwise and NFDK_DESC_TX_DMA_LEN_HEAD will less than
327 	 * headlen.
328 	 */
329 	dlen_type = FIELD_PREP(NFDK_DESC_TX_DMA_LEN_HEAD,
330 			       dma_len > NFDK_DESC_TX_DMA_LEN_HEAD ?
331 			       NFDK_DESC_TX_DMA_LEN_HEAD : dma_len) |
332 		    FIELD_PREP(NFDK_DESC_TX_TYPE_HEAD, type);
333 
334 	txd->dma_len_type = cpu_to_le16(dlen_type);
335 	nfp_desc_set_dma_addr_48b(txd, dma_addr);
336 
337 	/* starts at bit 0 */
338 	BUILD_BUG_ON(!(NFDK_DESC_TX_DMA_LEN_HEAD & 1));
339 
340 	/* Preserve the original dlen_type, this way below the EOP logic
341 	 * can use dlen_type.
342 	 */
343 	tmp_dlen = dlen_type & NFDK_DESC_TX_DMA_LEN_HEAD;
344 	dma_len -= tmp_dlen;
345 	dma_addr += tmp_dlen + 1;
346 	txd++;
347 
348 	/* The rest of the data (if any) will be in larger dma descritors
349 	 * and is handled with the fragment loop.
350 	 */
351 	frag = skb_shinfo(skb)->frags;
352 	fend = frag + nr_frags;
353 
354 	while (true) {
355 		while (dma_len > 0) {
356 			dma_len -= 1;
357 			dlen_type = FIELD_PREP(NFDK_DESC_TX_DMA_LEN, dma_len);
358 
359 			txd->dma_len_type = cpu_to_le16(dlen_type);
360 			nfp_desc_set_dma_addr_48b(txd, dma_addr);
361 
362 			dma_len -= dlen_type;
363 			dma_addr += dlen_type + 1;
364 			txd++;
365 		}
366 
367 		if (frag >= fend)
368 			break;
369 
370 		dma_len = skb_frag_size(frag);
371 		dma_addr = skb_frag_dma_map(dp->dev, frag, 0, dma_len,
372 					    DMA_TO_DEVICE);
373 		if (dma_mapping_error(dp->dev, dma_addr))
374 			goto err_unmap;
375 
376 		txbuf->dma_addr = dma_addr;
377 		txbuf++;
378 
379 		frag++;
380 	}
381 
382 	(txd - 1)->dma_len_type = cpu_to_le16(dlen_type | NFDK_DESC_TX_EOP);
383 
384 	if (ipsec)
385 		metadata = nfp_nfdk_ipsec_tx(metadata, skb);
386 
387 	if (!skb_is_gso(skb)) {
388 		real_len = skb->len;
389 		/* Metadata desc */
390 		if (!ipsec)
391 			metadata = nfp_nfdk_tx_csum(dp, r_vec, 1, skb, metadata);
392 		txd->raw = cpu_to_le64(metadata);
393 		txd++;
394 	} else {
395 		/* lso desc should be placed after metadata desc */
396 		(txd + 1)->raw = nfp_nfdk_tx_tso(r_vec, txbuf, skb);
397 		real_len = txbuf->real_len;
398 		/* Metadata desc */
399 		if (!ipsec)
400 			metadata = nfp_nfdk_tx_csum(dp, r_vec, txbuf->pkt_cnt, skb, metadata);
401 		txd->raw = cpu_to_le64(metadata);
402 		txd += 2;
403 		txbuf++;
404 	}
405 
406 	cnt = txd - tx_ring->ktxds - wr_idx;
407 	if (unlikely(round_down(wr_idx, NFDK_TX_DESC_BLOCK_CNT) !=
408 		     round_down(wr_idx + cnt - 1, NFDK_TX_DESC_BLOCK_CNT)))
409 		goto err_warn_overflow;
410 
411 	skb_tx_timestamp(skb);
412 
413 	tx_ring->wr_p += cnt;
414 	if (tx_ring->wr_p % NFDK_TX_DESC_BLOCK_CNT)
415 		tx_ring->data_pending += skb->len;
416 	else
417 		tx_ring->data_pending = 0;
418 
419 	if (nfp_nfdk_tx_ring_should_stop(tx_ring))
420 		nfp_nfdk_tx_ring_stop(nd_q, tx_ring);
421 
422 	tx_ring->wr_ptr_add += cnt;
423 	if (__netdev_tx_sent_queue(nd_q, real_len, netdev_xmit_more()))
424 		nfp_net_tx_xmit_more_flush(tx_ring);
425 
426 	return NETDEV_TX_OK;
427 
428 err_warn_overflow:
429 	WARN_ONCE(1, "unable to fit packet into a descriptor wr_idx:%d head:%d frags:%d cnt:%d",
430 		  wr_idx, skb_headlen(skb), nr_frags, cnt);
431 	if (skb_is_gso(skb))
432 		txbuf--;
433 err_unmap:
434 	/* txbuf pointed to the next-to-use */
435 	etxbuf = txbuf;
436 	/* first txbuf holds the skb */
437 	txbuf = &tx_ring->ktxbufs[wr_idx + 1];
438 	if (txbuf < etxbuf) {
439 		dma_unmap_single(dp->dev, txbuf->dma_addr,
440 				 skb_headlen(skb), DMA_TO_DEVICE);
441 		txbuf->raw = 0;
442 		txbuf++;
443 	}
444 	frag = skb_shinfo(skb)->frags;
445 	while (etxbuf < txbuf) {
446 		dma_unmap_page(dp->dev, txbuf->dma_addr,
447 			       skb_frag_size(frag), DMA_TO_DEVICE);
448 		txbuf->raw = 0;
449 		frag++;
450 		txbuf++;
451 	}
452 err_warn_dma:
453 	nn_dp_warn(dp, "Failed to map DMA TX buffer\n");
454 err_flush:
455 	nfp_net_tx_xmit_more_flush(tx_ring);
456 	u64_stats_update_begin(&r_vec->tx_sync);
457 	r_vec->tx_errors++;
458 	u64_stats_update_end(&r_vec->tx_sync);
459 	dev_kfree_skb_any(skb);
460 	return NETDEV_TX_OK;
461 }
462 
463 /**
464  * nfp_nfdk_tx_complete() - Handled completed TX packets
465  * @tx_ring:	TX ring structure
466  * @budget:	NAPI budget (only used as bool to determine if in NAPI context)
467  */
468 static void nfp_nfdk_tx_complete(struct nfp_net_tx_ring *tx_ring, int budget)
469 {
470 	struct nfp_net_r_vector *r_vec = tx_ring->r_vec;
471 	struct nfp_net_dp *dp = &r_vec->nfp_net->dp;
472 	u32 done_pkts = 0, done_bytes = 0;
473 	struct nfp_nfdk_tx_buf *ktxbufs;
474 	struct device *dev = dp->dev;
475 	struct netdev_queue *nd_q;
476 	u32 rd_p, qcp_rd_p;
477 	int todo;
478 
479 	rd_p = tx_ring->rd_p;
480 	if (tx_ring->wr_p == rd_p)
481 		return;
482 
483 	/* Work out how many descriptors have been transmitted */
484 	qcp_rd_p = nfp_net_read_tx_cmpl(tx_ring, dp);
485 
486 	if (qcp_rd_p == tx_ring->qcp_rd_p)
487 		return;
488 
489 	todo = D_IDX(tx_ring, qcp_rd_p - tx_ring->qcp_rd_p);
490 	ktxbufs = tx_ring->ktxbufs;
491 
492 	while (todo > 0) {
493 		const skb_frag_t *frag, *fend;
494 		unsigned int size, n_descs = 1;
495 		struct nfp_nfdk_tx_buf *txbuf;
496 		struct sk_buff *skb;
497 
498 		txbuf = &ktxbufs[D_IDX(tx_ring, rd_p)];
499 		skb = txbuf->skb;
500 		txbuf++;
501 
502 		/* Closed block */
503 		if (!skb) {
504 			n_descs = D_BLOCK_CPL(rd_p);
505 			goto next;
506 		}
507 
508 		/* Unmap head */
509 		size = skb_headlen(skb);
510 		n_descs += nfp_nfdk_headlen_to_segs(size);
511 		dma_unmap_single(dev, txbuf->dma_addr, size, DMA_TO_DEVICE);
512 		txbuf++;
513 
514 		/* Unmap frags */
515 		frag = skb_shinfo(skb)->frags;
516 		fend = frag + skb_shinfo(skb)->nr_frags;
517 		for (; frag < fend; frag++) {
518 			size = skb_frag_size(frag);
519 			n_descs += DIV_ROUND_UP(size,
520 						NFDK_TX_MAX_DATA_PER_DESC);
521 			dma_unmap_page(dev, txbuf->dma_addr,
522 				       skb_frag_size(frag), DMA_TO_DEVICE);
523 			txbuf++;
524 		}
525 
526 		if (!skb_is_gso(skb)) {
527 			done_bytes += skb->len;
528 			done_pkts++;
529 		} else {
530 			done_bytes += txbuf->real_len;
531 			done_pkts += txbuf->pkt_cnt;
532 			n_descs++;
533 		}
534 
535 		napi_consume_skb(skb, budget);
536 next:
537 		rd_p += n_descs;
538 		todo -= n_descs;
539 	}
540 
541 	tx_ring->rd_p = rd_p;
542 	tx_ring->qcp_rd_p = qcp_rd_p;
543 
544 	u64_stats_update_begin(&r_vec->tx_sync);
545 	r_vec->tx_bytes += done_bytes;
546 	r_vec->tx_pkts += done_pkts;
547 	u64_stats_update_end(&r_vec->tx_sync);
548 
549 	if (!dp->netdev)
550 		return;
551 
552 	nd_q = netdev_get_tx_queue(dp->netdev, tx_ring->idx);
553 	netdev_tx_completed_queue(nd_q, done_pkts, done_bytes);
554 	if (nfp_nfdk_tx_ring_should_wake(tx_ring)) {
555 		/* Make sure TX thread will see updated tx_ring->rd_p */
556 		smp_mb();
557 
558 		if (unlikely(netif_tx_queue_stopped(nd_q)))
559 			netif_tx_wake_queue(nd_q);
560 	}
561 
562 	WARN_ONCE(tx_ring->wr_p - tx_ring->rd_p > tx_ring->cnt,
563 		  "TX ring corruption rd_p=%u wr_p=%u cnt=%u\n",
564 		  tx_ring->rd_p, tx_ring->wr_p, tx_ring->cnt);
565 }
566 
567 /* Receive processing */
568 static void *
569 nfp_nfdk_napi_alloc_one(struct nfp_net_dp *dp, dma_addr_t *dma_addr)
570 {
571 	void *frag;
572 
573 	if (!dp->xdp_prog) {
574 		frag = napi_alloc_frag(dp->fl_bufsz);
575 		if (unlikely(!frag))
576 			return NULL;
577 	} else {
578 		struct page *page;
579 
580 		page = dev_alloc_page();
581 		if (unlikely(!page))
582 			return NULL;
583 		frag = page_address(page);
584 	}
585 
586 	*dma_addr = nfp_net_dma_map_rx(dp, frag);
587 	if (dma_mapping_error(dp->dev, *dma_addr)) {
588 		nfp_net_free_frag(frag, dp->xdp_prog);
589 		nn_dp_warn(dp, "Failed to map DMA RX buffer\n");
590 		return NULL;
591 	}
592 
593 	return frag;
594 }
595 
596 /**
597  * nfp_nfdk_rx_give_one() - Put mapped skb on the software and hardware rings
598  * @dp:		NFP Net data path struct
599  * @rx_ring:	RX ring structure
600  * @frag:	page fragment buffer
601  * @dma_addr:	DMA address of skb mapping
602  */
603 static void
604 nfp_nfdk_rx_give_one(const struct nfp_net_dp *dp,
605 		     struct nfp_net_rx_ring *rx_ring,
606 		     void *frag, dma_addr_t dma_addr)
607 {
608 	unsigned int wr_idx;
609 
610 	wr_idx = D_IDX(rx_ring, rx_ring->wr_p);
611 
612 	nfp_net_dma_sync_dev_rx(dp, dma_addr);
613 
614 	/* Stash SKB and DMA address away */
615 	rx_ring->rxbufs[wr_idx].frag = frag;
616 	rx_ring->rxbufs[wr_idx].dma_addr = dma_addr;
617 
618 	/* Fill freelist descriptor */
619 	rx_ring->rxds[wr_idx].fld.reserved = 0;
620 	rx_ring->rxds[wr_idx].fld.meta_len_dd = 0;
621 	nfp_desc_set_dma_addr_48b(&rx_ring->rxds[wr_idx].fld,
622 				  dma_addr + dp->rx_dma_off);
623 
624 	rx_ring->wr_p++;
625 	if (!(rx_ring->wr_p % NFP_NET_FL_BATCH)) {
626 		/* Update write pointer of the freelist queue. Make
627 		 * sure all writes are flushed before telling the hardware.
628 		 */
629 		wmb();
630 		nfp_qcp_wr_ptr_add(rx_ring->qcp_fl, NFP_NET_FL_BATCH);
631 	}
632 }
633 
634 /**
635  * nfp_nfdk_rx_ring_fill_freelist() - Give buffers from the ring to FW
636  * @dp:	     NFP Net data path struct
637  * @rx_ring: RX ring to fill
638  */
639 void nfp_nfdk_rx_ring_fill_freelist(struct nfp_net_dp *dp,
640 				    struct nfp_net_rx_ring *rx_ring)
641 {
642 	unsigned int i;
643 
644 	for (i = 0; i < rx_ring->cnt - 1; i++)
645 		nfp_nfdk_rx_give_one(dp, rx_ring, rx_ring->rxbufs[i].frag,
646 				     rx_ring->rxbufs[i].dma_addr);
647 }
648 
649 /**
650  * nfp_nfdk_rx_csum_has_errors() - group check if rxd has any csum errors
651  * @flags: RX descriptor flags field in CPU byte order
652  */
653 static int nfp_nfdk_rx_csum_has_errors(u16 flags)
654 {
655 	u16 csum_all_checked, csum_all_ok;
656 
657 	csum_all_checked = flags & __PCIE_DESC_RX_CSUM_ALL;
658 	csum_all_ok = flags & __PCIE_DESC_RX_CSUM_ALL_OK;
659 
660 	return csum_all_checked != (csum_all_ok << PCIE_DESC_RX_CSUM_OK_SHIFT);
661 }
662 
663 /**
664  * nfp_nfdk_rx_csum() - set SKB checksum field based on RX descriptor flags
665  * @dp:  NFP Net data path struct
666  * @r_vec: per-ring structure
667  * @rxd: Pointer to RX descriptor
668  * @meta: Parsed metadata prepend
669  * @skb: Pointer to SKB
670  */
671 static void
672 nfp_nfdk_rx_csum(struct nfp_net_dp *dp, struct nfp_net_r_vector *r_vec,
673 		 struct nfp_net_rx_desc *rxd, struct nfp_meta_parsed *meta,
674 		 struct sk_buff *skb)
675 {
676 	skb_checksum_none_assert(skb);
677 
678 	if (!(dp->netdev->features & NETIF_F_RXCSUM))
679 		return;
680 
681 	if (meta->csum_type) {
682 		skb->ip_summed = meta->csum_type;
683 		skb->csum = meta->csum;
684 		u64_stats_update_begin(&r_vec->rx_sync);
685 		r_vec->hw_csum_rx_complete++;
686 		u64_stats_update_end(&r_vec->rx_sync);
687 		return;
688 	}
689 
690 	if (nfp_nfdk_rx_csum_has_errors(le16_to_cpu(rxd->rxd.flags))) {
691 		u64_stats_update_begin(&r_vec->rx_sync);
692 		r_vec->hw_csum_rx_error++;
693 		u64_stats_update_end(&r_vec->rx_sync);
694 		return;
695 	}
696 
697 	/* Assume that the firmware will never report inner CSUM_OK unless outer
698 	 * L4 headers were successfully parsed. FW will always report zero UDP
699 	 * checksum as CSUM_OK.
700 	 */
701 	if (rxd->rxd.flags & PCIE_DESC_RX_TCP_CSUM_OK ||
702 	    rxd->rxd.flags & PCIE_DESC_RX_UDP_CSUM_OK) {
703 		__skb_incr_checksum_unnecessary(skb);
704 		u64_stats_update_begin(&r_vec->rx_sync);
705 		r_vec->hw_csum_rx_ok++;
706 		u64_stats_update_end(&r_vec->rx_sync);
707 	}
708 
709 	if (rxd->rxd.flags & PCIE_DESC_RX_I_TCP_CSUM_OK ||
710 	    rxd->rxd.flags & PCIE_DESC_RX_I_UDP_CSUM_OK) {
711 		__skb_incr_checksum_unnecessary(skb);
712 		u64_stats_update_begin(&r_vec->rx_sync);
713 		r_vec->hw_csum_rx_inner_ok++;
714 		u64_stats_update_end(&r_vec->rx_sync);
715 	}
716 }
717 
718 static void
719 nfp_nfdk_set_hash(struct net_device *netdev, struct nfp_meta_parsed *meta,
720 		  unsigned int type, __be32 *hash)
721 {
722 	if (!(netdev->features & NETIF_F_RXHASH))
723 		return;
724 
725 	switch (type) {
726 	case NFP_NET_RSS_IPV4:
727 	case NFP_NET_RSS_IPV6:
728 	case NFP_NET_RSS_IPV6_EX:
729 		meta->hash_type = PKT_HASH_TYPE_L3;
730 		break;
731 	default:
732 		meta->hash_type = PKT_HASH_TYPE_L4;
733 		break;
734 	}
735 
736 	meta->hash = get_unaligned_be32(hash);
737 }
738 
739 static bool
740 nfp_nfdk_parse_meta(struct net_device *netdev, struct nfp_meta_parsed *meta,
741 		    void *data, void *pkt, unsigned int pkt_len, int meta_len)
742 {
743 	u32 meta_info, vlan_info;
744 
745 	meta_info = get_unaligned_be32(data);
746 	data += 4;
747 
748 	while (meta_info) {
749 		switch (meta_info & NFP_NET_META_FIELD_MASK) {
750 		case NFP_NET_META_HASH:
751 			meta_info >>= NFP_NET_META_FIELD_SIZE;
752 			nfp_nfdk_set_hash(netdev, meta,
753 					  meta_info & NFP_NET_META_FIELD_MASK,
754 					  (__be32 *)data);
755 			data += 4;
756 			break;
757 		case NFP_NET_META_MARK:
758 			meta->mark = get_unaligned_be32(data);
759 			data += 4;
760 			break;
761 		case NFP_NET_META_VLAN:
762 			vlan_info = get_unaligned_be32(data);
763 			if (FIELD_GET(NFP_NET_META_VLAN_STRIP, vlan_info)) {
764 				meta->vlan.stripped = true;
765 				meta->vlan.tpid = FIELD_GET(NFP_NET_META_VLAN_TPID_MASK,
766 							    vlan_info);
767 				meta->vlan.tci = FIELD_GET(NFP_NET_META_VLAN_TCI_MASK,
768 							   vlan_info);
769 			}
770 			data += 4;
771 			break;
772 		case NFP_NET_META_PORTID:
773 			meta->portid = get_unaligned_be32(data);
774 			data += 4;
775 			break;
776 		case NFP_NET_META_CSUM:
777 			meta->csum_type = CHECKSUM_COMPLETE;
778 			meta->csum =
779 				(__force __wsum)__get_unaligned_cpu32(data);
780 			data += 4;
781 			break;
782 		case NFP_NET_META_RESYNC_INFO:
783 			if (nfp_net_tls_rx_resync_req(netdev, data, pkt,
784 						      pkt_len))
785 				return false;
786 			data += sizeof(struct nfp_net_tls_resync_req);
787 			break;
788 #ifdef CONFIG_NFP_NET_IPSEC
789 		case NFP_NET_META_IPSEC:
790 			/* Note: IPsec packet could have zero saidx, so need add 1
791 			 * to indicate packet is IPsec packet within driver.
792 			 */
793 			meta->ipsec_saidx = get_unaligned_be32(data) + 1;
794 			data += 4;
795 			break;
796 #endif
797 		default:
798 			return true;
799 		}
800 
801 		meta_info >>= NFP_NET_META_FIELD_SIZE;
802 	}
803 
804 	return data != pkt;
805 }
806 
807 static void
808 nfp_nfdk_rx_drop(const struct nfp_net_dp *dp, struct nfp_net_r_vector *r_vec,
809 		 struct nfp_net_rx_ring *rx_ring, struct nfp_net_rx_buf *rxbuf,
810 		 struct sk_buff *skb)
811 {
812 	u64_stats_update_begin(&r_vec->rx_sync);
813 	r_vec->rx_drops++;
814 	/* If we have both skb and rxbuf the replacement buffer allocation
815 	 * must have failed, count this as an alloc failure.
816 	 */
817 	if (skb && rxbuf)
818 		r_vec->rx_replace_buf_alloc_fail++;
819 	u64_stats_update_end(&r_vec->rx_sync);
820 
821 	/* skb is build based on the frag, free_skb() would free the frag
822 	 * so to be able to reuse it we need an extra ref.
823 	 */
824 	if (skb && rxbuf && skb->head == rxbuf->frag)
825 		page_ref_inc(virt_to_head_page(rxbuf->frag));
826 	if (rxbuf)
827 		nfp_nfdk_rx_give_one(dp, rx_ring, rxbuf->frag, rxbuf->dma_addr);
828 	if (skb)
829 		dev_kfree_skb_any(skb);
830 }
831 
832 static bool nfp_nfdk_xdp_complete(struct nfp_net_tx_ring *tx_ring)
833 {
834 	struct nfp_net_r_vector *r_vec = tx_ring->r_vec;
835 	struct nfp_net_dp *dp = &r_vec->nfp_net->dp;
836 	struct nfp_net_rx_ring *rx_ring;
837 	u32 qcp_rd_p, done = 0;
838 	bool done_all;
839 	int todo;
840 
841 	/* Work out how many descriptors have been transmitted */
842 	qcp_rd_p = nfp_net_read_tx_cmpl(tx_ring, dp);
843 	if (qcp_rd_p == tx_ring->qcp_rd_p)
844 		return true;
845 
846 	todo = D_IDX(tx_ring, qcp_rd_p - tx_ring->qcp_rd_p);
847 
848 	done_all = todo <= NFP_NET_XDP_MAX_COMPLETE;
849 	todo = min(todo, NFP_NET_XDP_MAX_COMPLETE);
850 
851 	rx_ring = r_vec->rx_ring;
852 	while (todo > 0) {
853 		int idx = D_IDX(tx_ring, tx_ring->rd_p + done);
854 		struct nfp_nfdk_tx_buf *txbuf;
855 		unsigned int step = 1;
856 
857 		txbuf = &tx_ring->ktxbufs[idx];
858 		if (!txbuf->raw)
859 			goto next;
860 
861 		if (NFDK_TX_BUF_INFO(txbuf->val) != NFDK_TX_BUF_INFO_SOP) {
862 			WARN_ONCE(1, "Unexpected TX buffer in XDP TX ring\n");
863 			goto next;
864 		}
865 
866 		/* Two successive txbufs are used to stash virtual and dma
867 		 * address respectively, recycle and clean them here.
868 		 */
869 		nfp_nfdk_rx_give_one(dp, rx_ring,
870 				     (void *)NFDK_TX_BUF_PTR(txbuf[0].val),
871 				     txbuf[1].dma_addr);
872 		txbuf[0].raw = 0;
873 		txbuf[1].raw = 0;
874 		step = 2;
875 
876 		u64_stats_update_begin(&r_vec->tx_sync);
877 		/* Note: tx_bytes not accumulated. */
878 		r_vec->tx_pkts++;
879 		u64_stats_update_end(&r_vec->tx_sync);
880 next:
881 		todo -= step;
882 		done += step;
883 	}
884 
885 	tx_ring->qcp_rd_p = D_IDX(tx_ring, tx_ring->qcp_rd_p + done);
886 	tx_ring->rd_p += done;
887 
888 	WARN_ONCE(tx_ring->wr_p - tx_ring->rd_p > tx_ring->cnt,
889 		  "XDP TX ring corruption rd_p=%u wr_p=%u cnt=%u\n",
890 		  tx_ring->rd_p, tx_ring->wr_p, tx_ring->cnt);
891 
892 	return done_all;
893 }
894 
895 static bool
896 nfp_nfdk_tx_xdp_buf(struct nfp_net_dp *dp, struct nfp_net_rx_ring *rx_ring,
897 		    struct nfp_net_tx_ring *tx_ring,
898 		    struct nfp_net_rx_buf *rxbuf, unsigned int dma_off,
899 		    unsigned int pkt_len, bool *completed)
900 {
901 	unsigned int dma_map_sz = dp->fl_bufsz - NFP_NET_RX_BUF_NON_DATA;
902 	unsigned int dma_len, type, cnt, dlen_type, tmp_dlen;
903 	struct nfp_nfdk_tx_buf *txbuf;
904 	struct nfp_nfdk_tx_desc *txd;
905 	unsigned int n_descs;
906 	dma_addr_t dma_addr;
907 	int wr_idx;
908 
909 	/* Reject if xdp_adjust_tail grow packet beyond DMA area */
910 	if (pkt_len + dma_off > dma_map_sz)
911 		return false;
912 
913 	/* Make sure there's still at least one block available after
914 	 * aligning to block boundary, so that the txds used below
915 	 * won't wrap around the tx_ring.
916 	 */
917 	if (unlikely(nfp_net_tx_full(tx_ring, NFDK_TX_DESC_STOP_CNT))) {
918 		if (!*completed) {
919 			nfp_nfdk_xdp_complete(tx_ring);
920 			*completed = true;
921 		}
922 
923 		if (unlikely(nfp_net_tx_full(tx_ring, NFDK_TX_DESC_STOP_CNT))) {
924 			nfp_nfdk_rx_drop(dp, rx_ring->r_vec, rx_ring, rxbuf,
925 					 NULL);
926 			return false;
927 		}
928 	}
929 
930 	/* Check if cross block boundary */
931 	n_descs = nfp_nfdk_headlen_to_segs(pkt_len);
932 	if ((round_down(tx_ring->wr_p, NFDK_TX_DESC_BLOCK_CNT) !=
933 	     round_down(tx_ring->wr_p + n_descs, NFDK_TX_DESC_BLOCK_CNT)) ||
934 	    ((u32)tx_ring->data_pending + pkt_len >
935 	     NFDK_TX_MAX_DATA_PER_BLOCK)) {
936 		unsigned int nop_slots = D_BLOCK_CPL(tx_ring->wr_p);
937 
938 		wr_idx = D_IDX(tx_ring, tx_ring->wr_p);
939 		txd = &tx_ring->ktxds[wr_idx];
940 		memset(txd, 0,
941 		       array_size(nop_slots, sizeof(struct nfp_nfdk_tx_desc)));
942 
943 		tx_ring->data_pending = 0;
944 		tx_ring->wr_p += nop_slots;
945 		tx_ring->wr_ptr_add += nop_slots;
946 	}
947 
948 	wr_idx = D_IDX(tx_ring, tx_ring->wr_p);
949 
950 	txbuf = &tx_ring->ktxbufs[wr_idx];
951 
952 	txbuf[0].val = (unsigned long)rxbuf->frag | NFDK_TX_BUF_INFO_SOP;
953 	txbuf[1].dma_addr = rxbuf->dma_addr;
954 	/* Note: pkt len not stored */
955 
956 	dma_sync_single_for_device(dp->dev, rxbuf->dma_addr + dma_off,
957 				   pkt_len, DMA_BIDIRECTIONAL);
958 
959 	/* Build TX descriptor */
960 	txd = &tx_ring->ktxds[wr_idx];
961 	dma_len = pkt_len;
962 	dma_addr = rxbuf->dma_addr + dma_off;
963 
964 	if (dma_len <= NFDK_TX_MAX_DATA_PER_HEAD)
965 		type = NFDK_DESC_TX_TYPE_SIMPLE;
966 	else
967 		type = NFDK_DESC_TX_TYPE_GATHER;
968 
969 	/* FIELD_PREP() implicitly truncates to chunk */
970 	dma_len -= 1;
971 	dlen_type = FIELD_PREP(NFDK_DESC_TX_DMA_LEN_HEAD,
972 			       dma_len > NFDK_DESC_TX_DMA_LEN_HEAD ?
973 			       NFDK_DESC_TX_DMA_LEN_HEAD : dma_len) |
974 		    FIELD_PREP(NFDK_DESC_TX_TYPE_HEAD, type);
975 
976 	txd->dma_len_type = cpu_to_le16(dlen_type);
977 	nfp_desc_set_dma_addr_48b(txd, dma_addr);
978 
979 	tmp_dlen = dlen_type & NFDK_DESC_TX_DMA_LEN_HEAD;
980 	dma_len -= tmp_dlen;
981 	dma_addr += tmp_dlen + 1;
982 	txd++;
983 
984 	while (dma_len > 0) {
985 		dma_len -= 1;
986 		dlen_type = FIELD_PREP(NFDK_DESC_TX_DMA_LEN, dma_len);
987 		txd->dma_len_type = cpu_to_le16(dlen_type);
988 		nfp_desc_set_dma_addr_48b(txd, dma_addr);
989 
990 		dlen_type &= NFDK_DESC_TX_DMA_LEN;
991 		dma_len -= dlen_type;
992 		dma_addr += dlen_type + 1;
993 		txd++;
994 	}
995 
996 	(txd - 1)->dma_len_type = cpu_to_le16(dlen_type | NFDK_DESC_TX_EOP);
997 
998 	/* Metadata desc */
999 	txd->raw = 0;
1000 	txd++;
1001 
1002 	cnt = txd - tx_ring->ktxds - wr_idx;
1003 	tx_ring->wr_p += cnt;
1004 	if (tx_ring->wr_p % NFDK_TX_DESC_BLOCK_CNT)
1005 		tx_ring->data_pending += pkt_len;
1006 	else
1007 		tx_ring->data_pending = 0;
1008 
1009 	tx_ring->wr_ptr_add += cnt;
1010 	return true;
1011 }
1012 
1013 /**
1014  * nfp_nfdk_rx() - receive up to @budget packets on @rx_ring
1015  * @rx_ring:   RX ring to receive from
1016  * @budget:    NAPI budget
1017  *
1018  * Note, this function is separated out from the napi poll function to
1019  * more cleanly separate packet receive code from other bookkeeping
1020  * functions performed in the napi poll function.
1021  *
1022  * Return: Number of packets received.
1023  */
1024 static int nfp_nfdk_rx(struct nfp_net_rx_ring *rx_ring, int budget)
1025 {
1026 	struct nfp_net_r_vector *r_vec = rx_ring->r_vec;
1027 	struct nfp_net_dp *dp = &r_vec->nfp_net->dp;
1028 	struct nfp_net_tx_ring *tx_ring;
1029 	struct bpf_prog *xdp_prog;
1030 	bool xdp_tx_cmpl = false;
1031 	unsigned int true_bufsz;
1032 	struct sk_buff *skb;
1033 	int pkts_polled = 0;
1034 	struct xdp_buff xdp;
1035 	int idx;
1036 
1037 	xdp_prog = READ_ONCE(dp->xdp_prog);
1038 	true_bufsz = xdp_prog ? PAGE_SIZE : dp->fl_bufsz;
1039 	xdp_init_buff(&xdp, PAGE_SIZE - NFP_NET_RX_BUF_HEADROOM,
1040 		      &rx_ring->xdp_rxq);
1041 	tx_ring = r_vec->xdp_ring;
1042 
1043 	while (pkts_polled < budget) {
1044 		unsigned int meta_len, data_len, meta_off, pkt_len, pkt_off;
1045 		struct nfp_net_rx_buf *rxbuf;
1046 		struct nfp_net_rx_desc *rxd;
1047 		struct nfp_meta_parsed meta;
1048 		bool redir_egress = false;
1049 		struct net_device *netdev;
1050 		dma_addr_t new_dma_addr;
1051 		u32 meta_len_xdp = 0;
1052 		void *new_frag;
1053 
1054 		idx = D_IDX(rx_ring, rx_ring->rd_p);
1055 
1056 		rxd = &rx_ring->rxds[idx];
1057 		if (!(rxd->rxd.meta_len_dd & PCIE_DESC_RX_DD))
1058 			break;
1059 
1060 		/* Memory barrier to ensure that we won't do other reads
1061 		 * before the DD bit.
1062 		 */
1063 		dma_rmb();
1064 
1065 		memset(&meta, 0, sizeof(meta));
1066 
1067 		rx_ring->rd_p++;
1068 		pkts_polled++;
1069 
1070 		rxbuf =	&rx_ring->rxbufs[idx];
1071 		/*         < meta_len >
1072 		 *  <-- [rx_offset] -->
1073 		 *  ---------------------------------------------------------
1074 		 * | [XX] |  metadata  |             packet           | XXXX |
1075 		 *  ---------------------------------------------------------
1076 		 *         <---------------- data_len --------------->
1077 		 *
1078 		 * The rx_offset is fixed for all packets, the meta_len can vary
1079 		 * on a packet by packet basis. If rx_offset is set to zero
1080 		 * (_RX_OFFSET_DYNAMIC) metadata starts at the beginning of the
1081 		 * buffer and is immediately followed by the packet (no [XX]).
1082 		 */
1083 		meta_len = rxd->rxd.meta_len_dd & PCIE_DESC_RX_META_LEN_MASK;
1084 		data_len = le16_to_cpu(rxd->rxd.data_len);
1085 		pkt_len = data_len - meta_len;
1086 
1087 		pkt_off = NFP_NET_RX_BUF_HEADROOM + dp->rx_dma_off;
1088 		if (dp->rx_offset == NFP_NET_CFG_RX_OFFSET_DYNAMIC)
1089 			pkt_off += meta_len;
1090 		else
1091 			pkt_off += dp->rx_offset;
1092 		meta_off = pkt_off - meta_len;
1093 
1094 		/* Stats update */
1095 		u64_stats_update_begin(&r_vec->rx_sync);
1096 		r_vec->rx_pkts++;
1097 		r_vec->rx_bytes += pkt_len;
1098 		u64_stats_update_end(&r_vec->rx_sync);
1099 
1100 		if (unlikely(meta_len > NFP_NET_MAX_PREPEND ||
1101 			     (dp->rx_offset && meta_len > dp->rx_offset))) {
1102 			nn_dp_warn(dp, "oversized RX packet metadata %u\n",
1103 				   meta_len);
1104 			nfp_nfdk_rx_drop(dp, r_vec, rx_ring, rxbuf, NULL);
1105 			continue;
1106 		}
1107 
1108 		nfp_net_dma_sync_cpu_rx(dp, rxbuf->dma_addr + meta_off,
1109 					data_len);
1110 
1111 		if (meta_len) {
1112 			if (unlikely(nfp_nfdk_parse_meta(dp->netdev, &meta,
1113 							 rxbuf->frag + meta_off,
1114 							 rxbuf->frag + pkt_off,
1115 							 pkt_len, meta_len))) {
1116 				nn_dp_warn(dp, "invalid RX packet metadata\n");
1117 				nfp_nfdk_rx_drop(dp, r_vec, rx_ring, rxbuf,
1118 						 NULL);
1119 				continue;
1120 			}
1121 		}
1122 
1123 		if (xdp_prog && !meta.portid) {
1124 			void *orig_data = rxbuf->frag + pkt_off;
1125 			unsigned int dma_off;
1126 			int act;
1127 
1128 			xdp_prepare_buff(&xdp,
1129 					 rxbuf->frag + NFP_NET_RX_BUF_HEADROOM,
1130 					 pkt_off - NFP_NET_RX_BUF_HEADROOM,
1131 					 pkt_len, true);
1132 
1133 			act = bpf_prog_run_xdp(xdp_prog, &xdp);
1134 
1135 			pkt_len = xdp.data_end - xdp.data;
1136 			pkt_off += xdp.data - orig_data;
1137 
1138 			switch (act) {
1139 			case XDP_PASS:
1140 				meta_len_xdp = xdp.data - xdp.data_meta;
1141 				break;
1142 			case XDP_TX:
1143 				dma_off = pkt_off - NFP_NET_RX_BUF_HEADROOM;
1144 				if (unlikely(!nfp_nfdk_tx_xdp_buf(dp, rx_ring,
1145 								  tx_ring,
1146 								  rxbuf,
1147 								  dma_off,
1148 								  pkt_len,
1149 								  &xdp_tx_cmpl)))
1150 					trace_xdp_exception(dp->netdev,
1151 							    xdp_prog, act);
1152 				continue;
1153 			default:
1154 				bpf_warn_invalid_xdp_action(dp->netdev, xdp_prog, act);
1155 				fallthrough;
1156 			case XDP_ABORTED:
1157 				trace_xdp_exception(dp->netdev, xdp_prog, act);
1158 				fallthrough;
1159 			case XDP_DROP:
1160 				nfp_nfdk_rx_give_one(dp, rx_ring, rxbuf->frag,
1161 						     rxbuf->dma_addr);
1162 				continue;
1163 			}
1164 		}
1165 
1166 		if (likely(!meta.portid)) {
1167 			netdev = dp->netdev;
1168 		} else if (meta.portid == NFP_META_PORT_ID_CTRL) {
1169 			struct nfp_net *nn = netdev_priv(dp->netdev);
1170 
1171 			nfp_app_ctrl_rx_raw(nn->app, rxbuf->frag + pkt_off,
1172 					    pkt_len);
1173 			nfp_nfdk_rx_give_one(dp, rx_ring, rxbuf->frag,
1174 					     rxbuf->dma_addr);
1175 			continue;
1176 		} else {
1177 			struct nfp_net *nn;
1178 
1179 			nn = netdev_priv(dp->netdev);
1180 			netdev = nfp_app_dev_get(nn->app, meta.portid,
1181 						 &redir_egress);
1182 			if (unlikely(!netdev)) {
1183 				nfp_nfdk_rx_drop(dp, r_vec, rx_ring, rxbuf,
1184 						 NULL);
1185 				continue;
1186 			}
1187 
1188 			if (nfp_netdev_is_nfp_repr(netdev))
1189 				nfp_repr_inc_rx_stats(netdev, pkt_len);
1190 		}
1191 
1192 		skb = napi_build_skb(rxbuf->frag, true_bufsz);
1193 		if (unlikely(!skb)) {
1194 			nfp_nfdk_rx_drop(dp, r_vec, rx_ring, rxbuf, NULL);
1195 			continue;
1196 		}
1197 		new_frag = nfp_nfdk_napi_alloc_one(dp, &new_dma_addr);
1198 		if (unlikely(!new_frag)) {
1199 			nfp_nfdk_rx_drop(dp, r_vec, rx_ring, rxbuf, skb);
1200 			continue;
1201 		}
1202 
1203 		nfp_net_dma_unmap_rx(dp, rxbuf->dma_addr);
1204 
1205 		nfp_nfdk_rx_give_one(dp, rx_ring, new_frag, new_dma_addr);
1206 
1207 		skb_reserve(skb, pkt_off);
1208 		skb_put(skb, pkt_len);
1209 
1210 		skb->mark = meta.mark;
1211 		skb_set_hash(skb, meta.hash, meta.hash_type);
1212 
1213 		skb_record_rx_queue(skb, rx_ring->idx);
1214 		skb->protocol = eth_type_trans(skb, netdev);
1215 
1216 		nfp_nfdk_rx_csum(dp, r_vec, rxd, &meta, skb);
1217 
1218 		if (unlikely(!nfp_net_vlan_strip(skb, rxd, &meta))) {
1219 			nfp_nfdk_rx_drop(dp, r_vec, rx_ring, NULL, skb);
1220 			continue;
1221 		}
1222 
1223 #ifdef CONFIG_NFP_NET_IPSEC
1224 		if (meta.ipsec_saidx != 0 && unlikely(nfp_net_ipsec_rx(&meta, skb))) {
1225 			nfp_nfdk_rx_drop(dp, r_vec, rx_ring, NULL, skb);
1226 			continue;
1227 		}
1228 #endif
1229 
1230 		if (meta_len_xdp)
1231 			skb_metadata_set(skb, meta_len_xdp);
1232 
1233 		if (likely(!redir_egress)) {
1234 			napi_gro_receive(&rx_ring->r_vec->napi, skb);
1235 		} else {
1236 			skb->dev = netdev;
1237 			skb_reset_network_header(skb);
1238 			__skb_push(skb, ETH_HLEN);
1239 			dev_queue_xmit(skb);
1240 		}
1241 	}
1242 
1243 	if (xdp_prog) {
1244 		if (tx_ring->wr_ptr_add)
1245 			nfp_net_tx_xmit_more_flush(tx_ring);
1246 		else if (unlikely(tx_ring->wr_p != tx_ring->rd_p) &&
1247 			 !xdp_tx_cmpl)
1248 			if (!nfp_nfdk_xdp_complete(tx_ring))
1249 				pkts_polled = budget;
1250 	}
1251 
1252 	return pkts_polled;
1253 }
1254 
1255 /**
1256  * nfp_nfdk_poll() - napi poll function
1257  * @napi:    NAPI structure
1258  * @budget:  NAPI budget
1259  *
1260  * Return: number of packets polled.
1261  */
1262 int nfp_nfdk_poll(struct napi_struct *napi, int budget)
1263 {
1264 	struct nfp_net_r_vector *r_vec =
1265 		container_of(napi, struct nfp_net_r_vector, napi);
1266 	unsigned int pkts_polled = 0;
1267 
1268 	if (r_vec->tx_ring)
1269 		nfp_nfdk_tx_complete(r_vec->tx_ring, budget);
1270 	if (r_vec->rx_ring)
1271 		pkts_polled = nfp_nfdk_rx(r_vec->rx_ring, budget);
1272 
1273 	if (pkts_polled < budget)
1274 		if (napi_complete_done(napi, pkts_polled))
1275 			nfp_net_irq_unmask(r_vec->nfp_net, r_vec->irq_entry);
1276 
1277 	if (r_vec->nfp_net->rx_coalesce_adapt_on && r_vec->rx_ring) {
1278 		struct dim_sample dim_sample = {};
1279 		unsigned int start;
1280 		u64 pkts, bytes;
1281 
1282 		do {
1283 			start = u64_stats_fetch_begin(&r_vec->rx_sync);
1284 			pkts = r_vec->rx_pkts;
1285 			bytes = r_vec->rx_bytes;
1286 		} while (u64_stats_fetch_retry(&r_vec->rx_sync, start));
1287 
1288 		dim_update_sample(r_vec->event_ctr, pkts, bytes, &dim_sample);
1289 		net_dim(&r_vec->rx_dim, dim_sample);
1290 	}
1291 
1292 	if (r_vec->nfp_net->tx_coalesce_adapt_on && r_vec->tx_ring) {
1293 		struct dim_sample dim_sample = {};
1294 		unsigned int start;
1295 		u64 pkts, bytes;
1296 
1297 		do {
1298 			start = u64_stats_fetch_begin(&r_vec->tx_sync);
1299 			pkts = r_vec->tx_pkts;
1300 			bytes = r_vec->tx_bytes;
1301 		} while (u64_stats_fetch_retry(&r_vec->tx_sync, start));
1302 
1303 		dim_update_sample(r_vec->event_ctr, pkts, bytes, &dim_sample);
1304 		net_dim(&r_vec->tx_dim, dim_sample);
1305 	}
1306 
1307 	return pkts_polled;
1308 }
1309 
1310 /* Control device data path
1311  */
1312 
1313 bool
1314 nfp_nfdk_ctrl_tx_one(struct nfp_net *nn, struct nfp_net_r_vector *r_vec,
1315 		     struct sk_buff *skb, bool old)
1316 {
1317 	u32 cnt, tmp_dlen, dlen_type = 0;
1318 	struct nfp_net_tx_ring *tx_ring;
1319 	struct nfp_nfdk_tx_buf *txbuf;
1320 	struct nfp_nfdk_tx_desc *txd;
1321 	unsigned int dma_len, type;
1322 	struct nfp_net_dp *dp;
1323 	dma_addr_t dma_addr;
1324 	u64 metadata = 0;
1325 	int wr_idx;
1326 
1327 	dp = &r_vec->nfp_net->dp;
1328 	tx_ring = r_vec->tx_ring;
1329 
1330 	if (WARN_ON_ONCE(skb_shinfo(skb)->nr_frags)) {
1331 		nn_dp_warn(dp, "Driver's CTRL TX does not implement gather\n");
1332 		goto err_free;
1333 	}
1334 
1335 	/* Don't bother counting frags, assume the worst */
1336 	if (unlikely(nfp_net_tx_full(tx_ring, NFDK_TX_DESC_STOP_CNT))) {
1337 		u64_stats_update_begin(&r_vec->tx_sync);
1338 		r_vec->tx_busy++;
1339 		u64_stats_update_end(&r_vec->tx_sync);
1340 		if (!old)
1341 			__skb_queue_tail(&r_vec->queue, skb);
1342 		else
1343 			__skb_queue_head(&r_vec->queue, skb);
1344 		return NETDEV_TX_BUSY;
1345 	}
1346 
1347 	if (nfp_app_ctrl_has_meta(nn->app)) {
1348 		if (unlikely(skb_headroom(skb) < 8)) {
1349 			nn_dp_warn(dp, "CTRL TX on skb without headroom\n");
1350 			goto err_free;
1351 		}
1352 		metadata = NFDK_DESC_TX_CHAIN_META;
1353 		put_unaligned_be32(NFP_META_PORT_ID_CTRL, skb_push(skb, 4));
1354 		put_unaligned_be32(FIELD_PREP(NFDK_META_LEN, 8) |
1355 				   FIELD_PREP(NFDK_META_FIELDS,
1356 					      NFP_NET_META_PORTID),
1357 				   skb_push(skb, 4));
1358 	}
1359 
1360 	if (nfp_nfdk_tx_maybe_close_block(tx_ring, skb))
1361 		goto err_free;
1362 
1363 	/* DMA map all */
1364 	wr_idx = D_IDX(tx_ring, tx_ring->wr_p);
1365 	txd = &tx_ring->ktxds[wr_idx];
1366 	txbuf = &tx_ring->ktxbufs[wr_idx];
1367 
1368 	dma_len = skb_headlen(skb);
1369 	if (dma_len <= NFDK_TX_MAX_DATA_PER_HEAD)
1370 		type = NFDK_DESC_TX_TYPE_SIMPLE;
1371 	else
1372 		type = NFDK_DESC_TX_TYPE_GATHER;
1373 
1374 	dma_addr = dma_map_single(dp->dev, skb->data, dma_len, DMA_TO_DEVICE);
1375 	if (dma_mapping_error(dp->dev, dma_addr))
1376 		goto err_warn_dma;
1377 
1378 	txbuf->skb = skb;
1379 	txbuf++;
1380 
1381 	txbuf->dma_addr = dma_addr;
1382 	txbuf++;
1383 
1384 	dma_len -= 1;
1385 	dlen_type = FIELD_PREP(NFDK_DESC_TX_DMA_LEN_HEAD,
1386 			       dma_len > NFDK_DESC_TX_DMA_LEN_HEAD ?
1387 			       NFDK_DESC_TX_DMA_LEN_HEAD : dma_len) |
1388 		    FIELD_PREP(NFDK_DESC_TX_TYPE_HEAD, type);
1389 
1390 	txd->dma_len_type = cpu_to_le16(dlen_type);
1391 	nfp_desc_set_dma_addr_48b(txd, dma_addr);
1392 
1393 	tmp_dlen = dlen_type & NFDK_DESC_TX_DMA_LEN_HEAD;
1394 	dma_len -= tmp_dlen;
1395 	dma_addr += tmp_dlen + 1;
1396 	txd++;
1397 
1398 	while (dma_len > 0) {
1399 		dma_len -= 1;
1400 		dlen_type = FIELD_PREP(NFDK_DESC_TX_DMA_LEN, dma_len);
1401 		txd->dma_len_type = cpu_to_le16(dlen_type);
1402 		nfp_desc_set_dma_addr_48b(txd, dma_addr);
1403 
1404 		dlen_type &= NFDK_DESC_TX_DMA_LEN;
1405 		dma_len -= dlen_type;
1406 		dma_addr += dlen_type + 1;
1407 		txd++;
1408 	}
1409 
1410 	(txd - 1)->dma_len_type = cpu_to_le16(dlen_type | NFDK_DESC_TX_EOP);
1411 
1412 	/* Metadata desc */
1413 	txd->raw = cpu_to_le64(metadata);
1414 	txd++;
1415 
1416 	cnt = txd - tx_ring->ktxds - wr_idx;
1417 	if (unlikely(round_down(wr_idx, NFDK_TX_DESC_BLOCK_CNT) !=
1418 		     round_down(wr_idx + cnt - 1, NFDK_TX_DESC_BLOCK_CNT)))
1419 		goto err_warn_overflow;
1420 
1421 	tx_ring->wr_p += cnt;
1422 	if (tx_ring->wr_p % NFDK_TX_DESC_BLOCK_CNT)
1423 		tx_ring->data_pending += skb->len;
1424 	else
1425 		tx_ring->data_pending = 0;
1426 
1427 	tx_ring->wr_ptr_add += cnt;
1428 	nfp_net_tx_xmit_more_flush(tx_ring);
1429 
1430 	return NETDEV_TX_OK;
1431 
1432 err_warn_overflow:
1433 	WARN_ONCE(1, "unable to fit packet into a descriptor wr_idx:%d head:%d frags:%d cnt:%d",
1434 		  wr_idx, skb_headlen(skb), 0, cnt);
1435 	txbuf--;
1436 	dma_unmap_single(dp->dev, txbuf->dma_addr,
1437 			 skb_headlen(skb), DMA_TO_DEVICE);
1438 	txbuf->raw = 0;
1439 err_warn_dma:
1440 	nn_dp_warn(dp, "Failed to map DMA TX buffer\n");
1441 err_free:
1442 	u64_stats_update_begin(&r_vec->tx_sync);
1443 	r_vec->tx_errors++;
1444 	u64_stats_update_end(&r_vec->tx_sync);
1445 	dev_kfree_skb_any(skb);
1446 	return NETDEV_TX_OK;
1447 }
1448 
1449 static void __nfp_ctrl_tx_queued(struct nfp_net_r_vector *r_vec)
1450 {
1451 	struct sk_buff *skb;
1452 
1453 	while ((skb = __skb_dequeue(&r_vec->queue)))
1454 		if (nfp_nfdk_ctrl_tx_one(r_vec->nfp_net, r_vec, skb, true))
1455 			return;
1456 }
1457 
1458 static bool
1459 nfp_ctrl_meta_ok(struct nfp_net *nn, void *data, unsigned int meta_len)
1460 {
1461 	u32 meta_type, meta_tag;
1462 
1463 	if (!nfp_app_ctrl_has_meta(nn->app))
1464 		return !meta_len;
1465 
1466 	if (meta_len != 8)
1467 		return false;
1468 
1469 	meta_type = get_unaligned_be32(data);
1470 	meta_tag = get_unaligned_be32(data + 4);
1471 
1472 	return (meta_type == NFP_NET_META_PORTID &&
1473 		meta_tag == NFP_META_PORT_ID_CTRL);
1474 }
1475 
1476 static bool
1477 nfp_ctrl_rx_one(struct nfp_net *nn, struct nfp_net_dp *dp,
1478 		struct nfp_net_r_vector *r_vec, struct nfp_net_rx_ring *rx_ring)
1479 {
1480 	unsigned int meta_len, data_len, meta_off, pkt_len, pkt_off;
1481 	struct nfp_net_rx_buf *rxbuf;
1482 	struct nfp_net_rx_desc *rxd;
1483 	dma_addr_t new_dma_addr;
1484 	struct sk_buff *skb;
1485 	void *new_frag;
1486 	int idx;
1487 
1488 	idx = D_IDX(rx_ring, rx_ring->rd_p);
1489 
1490 	rxd = &rx_ring->rxds[idx];
1491 	if (!(rxd->rxd.meta_len_dd & PCIE_DESC_RX_DD))
1492 		return false;
1493 
1494 	/* Memory barrier to ensure that we won't do other reads
1495 	 * before the DD bit.
1496 	 */
1497 	dma_rmb();
1498 
1499 	rx_ring->rd_p++;
1500 
1501 	rxbuf =	&rx_ring->rxbufs[idx];
1502 	meta_len = rxd->rxd.meta_len_dd & PCIE_DESC_RX_META_LEN_MASK;
1503 	data_len = le16_to_cpu(rxd->rxd.data_len);
1504 	pkt_len = data_len - meta_len;
1505 
1506 	pkt_off = NFP_NET_RX_BUF_HEADROOM + dp->rx_dma_off;
1507 	if (dp->rx_offset == NFP_NET_CFG_RX_OFFSET_DYNAMIC)
1508 		pkt_off += meta_len;
1509 	else
1510 		pkt_off += dp->rx_offset;
1511 	meta_off = pkt_off - meta_len;
1512 
1513 	/* Stats update */
1514 	u64_stats_update_begin(&r_vec->rx_sync);
1515 	r_vec->rx_pkts++;
1516 	r_vec->rx_bytes += pkt_len;
1517 	u64_stats_update_end(&r_vec->rx_sync);
1518 
1519 	nfp_net_dma_sync_cpu_rx(dp, rxbuf->dma_addr + meta_off,	data_len);
1520 
1521 	if (unlikely(!nfp_ctrl_meta_ok(nn, rxbuf->frag + meta_off, meta_len))) {
1522 		nn_dp_warn(dp, "incorrect metadata for ctrl packet (%d)\n",
1523 			   meta_len);
1524 		nfp_nfdk_rx_drop(dp, r_vec, rx_ring, rxbuf, NULL);
1525 		return true;
1526 	}
1527 
1528 	skb = build_skb(rxbuf->frag, dp->fl_bufsz);
1529 	if (unlikely(!skb)) {
1530 		nfp_nfdk_rx_drop(dp, r_vec, rx_ring, rxbuf, NULL);
1531 		return true;
1532 	}
1533 	new_frag = nfp_nfdk_napi_alloc_one(dp, &new_dma_addr);
1534 	if (unlikely(!new_frag)) {
1535 		nfp_nfdk_rx_drop(dp, r_vec, rx_ring, rxbuf, skb);
1536 		return true;
1537 	}
1538 
1539 	nfp_net_dma_unmap_rx(dp, rxbuf->dma_addr);
1540 
1541 	nfp_nfdk_rx_give_one(dp, rx_ring, new_frag, new_dma_addr);
1542 
1543 	skb_reserve(skb, pkt_off);
1544 	skb_put(skb, pkt_len);
1545 
1546 	nfp_app_ctrl_rx(nn->app, skb);
1547 
1548 	return true;
1549 }
1550 
1551 static bool nfp_ctrl_rx(struct nfp_net_r_vector *r_vec)
1552 {
1553 	struct nfp_net_rx_ring *rx_ring = r_vec->rx_ring;
1554 	struct nfp_net *nn = r_vec->nfp_net;
1555 	struct nfp_net_dp *dp = &nn->dp;
1556 	unsigned int budget = 512;
1557 
1558 	while (nfp_ctrl_rx_one(nn, dp, r_vec, rx_ring) && budget--)
1559 		continue;
1560 
1561 	return budget;
1562 }
1563 
1564 void nfp_nfdk_ctrl_poll(struct tasklet_struct *t)
1565 {
1566 	struct nfp_net_r_vector *r_vec = from_tasklet(r_vec, t, tasklet);
1567 
1568 	spin_lock(&r_vec->lock);
1569 	nfp_nfdk_tx_complete(r_vec->tx_ring, 0);
1570 	__nfp_ctrl_tx_queued(r_vec);
1571 	spin_unlock(&r_vec->lock);
1572 
1573 	if (nfp_ctrl_rx(r_vec)) {
1574 		nfp_net_irq_unmask(r_vec->nfp_net, r_vec->irq_entry);
1575 	} else {
1576 		tasklet_schedule(&r_vec->tasklet);
1577 		nn_dp_warn(&r_vec->nfp_net->dp,
1578 			   "control message budget exceeded!\n");
1579 	}
1580 }
1581