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 		metadata = nfp_nfdk_tx_csum(dp, r_vec, 1, skb, metadata);
391 		txd->raw = cpu_to_le64(metadata);
392 		txd++;
393 	} else {
394 		/* lso desc should be placed after metadata desc */
395 		(txd + 1)->raw = nfp_nfdk_tx_tso(r_vec, txbuf, skb);
396 		real_len = txbuf->real_len;
397 		/* Metadata desc */
398 		metadata = nfp_nfdk_tx_csum(dp, r_vec, txbuf->pkt_cnt, skb, metadata);
399 		txd->raw = cpu_to_le64(metadata);
400 		txd += 2;
401 		txbuf++;
402 	}
403 
404 	cnt = txd - tx_ring->ktxds - wr_idx;
405 	if (unlikely(round_down(wr_idx, NFDK_TX_DESC_BLOCK_CNT) !=
406 		     round_down(wr_idx + cnt - 1, NFDK_TX_DESC_BLOCK_CNT)))
407 		goto err_warn_overflow;
408 
409 	skb_tx_timestamp(skb);
410 
411 	tx_ring->wr_p += cnt;
412 	if (tx_ring->wr_p % NFDK_TX_DESC_BLOCK_CNT)
413 		tx_ring->data_pending += skb->len;
414 	else
415 		tx_ring->data_pending = 0;
416 
417 	if (nfp_nfdk_tx_ring_should_stop(tx_ring))
418 		nfp_nfdk_tx_ring_stop(nd_q, tx_ring);
419 
420 	tx_ring->wr_ptr_add += cnt;
421 	if (__netdev_tx_sent_queue(nd_q, real_len, netdev_xmit_more()))
422 		nfp_net_tx_xmit_more_flush(tx_ring);
423 
424 	return NETDEV_TX_OK;
425 
426 err_warn_overflow:
427 	WARN_ONCE(1, "unable to fit packet into a descriptor wr_idx:%d head:%d frags:%d cnt:%d",
428 		  wr_idx, skb_headlen(skb), nr_frags, cnt);
429 	if (skb_is_gso(skb))
430 		txbuf--;
431 err_unmap:
432 	/* txbuf pointed to the next-to-use */
433 	etxbuf = txbuf;
434 	/* first txbuf holds the skb */
435 	txbuf = &tx_ring->ktxbufs[wr_idx + 1];
436 	if (txbuf < etxbuf) {
437 		dma_unmap_single(dp->dev, txbuf->dma_addr,
438 				 skb_headlen(skb), DMA_TO_DEVICE);
439 		txbuf->raw = 0;
440 		txbuf++;
441 	}
442 	frag = skb_shinfo(skb)->frags;
443 	while (etxbuf < txbuf) {
444 		dma_unmap_page(dp->dev, txbuf->dma_addr,
445 			       skb_frag_size(frag), DMA_TO_DEVICE);
446 		txbuf->raw = 0;
447 		frag++;
448 		txbuf++;
449 	}
450 err_warn_dma:
451 	nn_dp_warn(dp, "Failed to map DMA TX buffer\n");
452 err_flush:
453 	nfp_net_tx_xmit_more_flush(tx_ring);
454 	u64_stats_update_begin(&r_vec->tx_sync);
455 	r_vec->tx_errors++;
456 	u64_stats_update_end(&r_vec->tx_sync);
457 	dev_kfree_skb_any(skb);
458 	return NETDEV_TX_OK;
459 }
460 
461 /**
462  * nfp_nfdk_tx_complete() - Handled completed TX packets
463  * @tx_ring:	TX ring structure
464  * @budget:	NAPI budget (only used as bool to determine if in NAPI context)
465  */
466 static void nfp_nfdk_tx_complete(struct nfp_net_tx_ring *tx_ring, int budget)
467 {
468 	struct nfp_net_r_vector *r_vec = tx_ring->r_vec;
469 	struct nfp_net_dp *dp = &r_vec->nfp_net->dp;
470 	u32 done_pkts = 0, done_bytes = 0;
471 	struct nfp_nfdk_tx_buf *ktxbufs;
472 	struct device *dev = dp->dev;
473 	struct netdev_queue *nd_q;
474 	u32 rd_p, qcp_rd_p;
475 	int todo;
476 
477 	rd_p = tx_ring->rd_p;
478 	if (tx_ring->wr_p == rd_p)
479 		return;
480 
481 	/* Work out how many descriptors have been transmitted */
482 	qcp_rd_p = nfp_net_read_tx_cmpl(tx_ring, dp);
483 
484 	if (qcp_rd_p == tx_ring->qcp_rd_p)
485 		return;
486 
487 	todo = D_IDX(tx_ring, qcp_rd_p - tx_ring->qcp_rd_p);
488 	ktxbufs = tx_ring->ktxbufs;
489 
490 	while (todo > 0) {
491 		const skb_frag_t *frag, *fend;
492 		unsigned int size, n_descs = 1;
493 		struct nfp_nfdk_tx_buf *txbuf;
494 		struct sk_buff *skb;
495 
496 		txbuf = &ktxbufs[D_IDX(tx_ring, rd_p)];
497 		skb = txbuf->skb;
498 		txbuf++;
499 
500 		/* Closed block */
501 		if (!skb) {
502 			n_descs = D_BLOCK_CPL(rd_p);
503 			goto next;
504 		}
505 
506 		/* Unmap head */
507 		size = skb_headlen(skb);
508 		n_descs += nfp_nfdk_headlen_to_segs(size);
509 		dma_unmap_single(dev, txbuf->dma_addr, size, DMA_TO_DEVICE);
510 		txbuf++;
511 
512 		/* Unmap frags */
513 		frag = skb_shinfo(skb)->frags;
514 		fend = frag + skb_shinfo(skb)->nr_frags;
515 		for (; frag < fend; frag++) {
516 			size = skb_frag_size(frag);
517 			n_descs += DIV_ROUND_UP(size,
518 						NFDK_TX_MAX_DATA_PER_DESC);
519 			dma_unmap_page(dev, txbuf->dma_addr,
520 				       skb_frag_size(frag), DMA_TO_DEVICE);
521 			txbuf++;
522 		}
523 
524 		if (!skb_is_gso(skb)) {
525 			done_bytes += skb->len;
526 			done_pkts++;
527 		} else {
528 			done_bytes += txbuf->real_len;
529 			done_pkts += txbuf->pkt_cnt;
530 			n_descs++;
531 		}
532 
533 		napi_consume_skb(skb, budget);
534 next:
535 		rd_p += n_descs;
536 		todo -= n_descs;
537 	}
538 
539 	tx_ring->rd_p = rd_p;
540 	tx_ring->qcp_rd_p = qcp_rd_p;
541 
542 	u64_stats_update_begin(&r_vec->tx_sync);
543 	r_vec->tx_bytes += done_bytes;
544 	r_vec->tx_pkts += done_pkts;
545 	u64_stats_update_end(&r_vec->tx_sync);
546 
547 	if (!dp->netdev)
548 		return;
549 
550 	nd_q = netdev_get_tx_queue(dp->netdev, tx_ring->idx);
551 	netdev_tx_completed_queue(nd_q, done_pkts, done_bytes);
552 	if (nfp_nfdk_tx_ring_should_wake(tx_ring)) {
553 		/* Make sure TX thread will see updated tx_ring->rd_p */
554 		smp_mb();
555 
556 		if (unlikely(netif_tx_queue_stopped(nd_q)))
557 			netif_tx_wake_queue(nd_q);
558 	}
559 
560 	WARN_ONCE(tx_ring->wr_p - tx_ring->rd_p > tx_ring->cnt,
561 		  "TX ring corruption rd_p=%u wr_p=%u cnt=%u\n",
562 		  tx_ring->rd_p, tx_ring->wr_p, tx_ring->cnt);
563 }
564 
565 /* Receive processing */
566 static void *
567 nfp_nfdk_napi_alloc_one(struct nfp_net_dp *dp, dma_addr_t *dma_addr)
568 {
569 	void *frag;
570 
571 	if (!dp->xdp_prog) {
572 		frag = napi_alloc_frag(dp->fl_bufsz);
573 		if (unlikely(!frag))
574 			return NULL;
575 	} else {
576 		struct page *page;
577 
578 		page = dev_alloc_page();
579 		if (unlikely(!page))
580 			return NULL;
581 		frag = page_address(page);
582 	}
583 
584 	*dma_addr = nfp_net_dma_map_rx(dp, frag);
585 	if (dma_mapping_error(dp->dev, *dma_addr)) {
586 		nfp_net_free_frag(frag, dp->xdp_prog);
587 		nn_dp_warn(dp, "Failed to map DMA RX buffer\n");
588 		return NULL;
589 	}
590 
591 	return frag;
592 }
593 
594 /**
595  * nfp_nfdk_rx_give_one() - Put mapped skb on the software and hardware rings
596  * @dp:		NFP Net data path struct
597  * @rx_ring:	RX ring structure
598  * @frag:	page fragment buffer
599  * @dma_addr:	DMA address of skb mapping
600  */
601 static void
602 nfp_nfdk_rx_give_one(const struct nfp_net_dp *dp,
603 		     struct nfp_net_rx_ring *rx_ring,
604 		     void *frag, dma_addr_t dma_addr)
605 {
606 	unsigned int wr_idx;
607 
608 	wr_idx = D_IDX(rx_ring, rx_ring->wr_p);
609 
610 	nfp_net_dma_sync_dev_rx(dp, dma_addr);
611 
612 	/* Stash SKB and DMA address away */
613 	rx_ring->rxbufs[wr_idx].frag = frag;
614 	rx_ring->rxbufs[wr_idx].dma_addr = dma_addr;
615 
616 	/* Fill freelist descriptor */
617 	rx_ring->rxds[wr_idx].fld.reserved = 0;
618 	rx_ring->rxds[wr_idx].fld.meta_len_dd = 0;
619 	nfp_desc_set_dma_addr_48b(&rx_ring->rxds[wr_idx].fld,
620 				  dma_addr + dp->rx_dma_off);
621 
622 	rx_ring->wr_p++;
623 	if (!(rx_ring->wr_p % NFP_NET_FL_BATCH)) {
624 		/* Update write pointer of the freelist queue. Make
625 		 * sure all writes are flushed before telling the hardware.
626 		 */
627 		wmb();
628 		nfp_qcp_wr_ptr_add(rx_ring->qcp_fl, NFP_NET_FL_BATCH);
629 	}
630 }
631 
632 /**
633  * nfp_nfdk_rx_ring_fill_freelist() - Give buffers from the ring to FW
634  * @dp:	     NFP Net data path struct
635  * @rx_ring: RX ring to fill
636  */
637 void nfp_nfdk_rx_ring_fill_freelist(struct nfp_net_dp *dp,
638 				    struct nfp_net_rx_ring *rx_ring)
639 {
640 	unsigned int i;
641 
642 	for (i = 0; i < rx_ring->cnt - 1; i++)
643 		nfp_nfdk_rx_give_one(dp, rx_ring, rx_ring->rxbufs[i].frag,
644 				     rx_ring->rxbufs[i].dma_addr);
645 }
646 
647 /**
648  * nfp_nfdk_rx_csum_has_errors() - group check if rxd has any csum errors
649  * @flags: RX descriptor flags field in CPU byte order
650  */
651 static int nfp_nfdk_rx_csum_has_errors(u16 flags)
652 {
653 	u16 csum_all_checked, csum_all_ok;
654 
655 	csum_all_checked = flags & __PCIE_DESC_RX_CSUM_ALL;
656 	csum_all_ok = flags & __PCIE_DESC_RX_CSUM_ALL_OK;
657 
658 	return csum_all_checked != (csum_all_ok << PCIE_DESC_RX_CSUM_OK_SHIFT);
659 }
660 
661 /**
662  * nfp_nfdk_rx_csum() - set SKB checksum field based on RX descriptor flags
663  * @dp:  NFP Net data path struct
664  * @r_vec: per-ring structure
665  * @rxd: Pointer to RX descriptor
666  * @meta: Parsed metadata prepend
667  * @skb: Pointer to SKB
668  */
669 static void
670 nfp_nfdk_rx_csum(struct nfp_net_dp *dp, struct nfp_net_r_vector *r_vec,
671 		 struct nfp_net_rx_desc *rxd, struct nfp_meta_parsed *meta,
672 		 struct sk_buff *skb)
673 {
674 	skb_checksum_none_assert(skb);
675 
676 	if (!(dp->netdev->features & NETIF_F_RXCSUM))
677 		return;
678 
679 	if (meta->csum_type) {
680 		skb->ip_summed = meta->csum_type;
681 		skb->csum = meta->csum;
682 		u64_stats_update_begin(&r_vec->rx_sync);
683 		r_vec->hw_csum_rx_complete++;
684 		u64_stats_update_end(&r_vec->rx_sync);
685 		return;
686 	}
687 
688 	if (nfp_nfdk_rx_csum_has_errors(le16_to_cpu(rxd->rxd.flags))) {
689 		u64_stats_update_begin(&r_vec->rx_sync);
690 		r_vec->hw_csum_rx_error++;
691 		u64_stats_update_end(&r_vec->rx_sync);
692 		return;
693 	}
694 
695 	/* Assume that the firmware will never report inner CSUM_OK unless outer
696 	 * L4 headers were successfully parsed. FW will always report zero UDP
697 	 * checksum as CSUM_OK.
698 	 */
699 	if (rxd->rxd.flags & PCIE_DESC_RX_TCP_CSUM_OK ||
700 	    rxd->rxd.flags & PCIE_DESC_RX_UDP_CSUM_OK) {
701 		__skb_incr_checksum_unnecessary(skb);
702 		u64_stats_update_begin(&r_vec->rx_sync);
703 		r_vec->hw_csum_rx_ok++;
704 		u64_stats_update_end(&r_vec->rx_sync);
705 	}
706 
707 	if (rxd->rxd.flags & PCIE_DESC_RX_I_TCP_CSUM_OK ||
708 	    rxd->rxd.flags & PCIE_DESC_RX_I_UDP_CSUM_OK) {
709 		__skb_incr_checksum_unnecessary(skb);
710 		u64_stats_update_begin(&r_vec->rx_sync);
711 		r_vec->hw_csum_rx_inner_ok++;
712 		u64_stats_update_end(&r_vec->rx_sync);
713 	}
714 }
715 
716 static void
717 nfp_nfdk_set_hash(struct net_device *netdev, struct nfp_meta_parsed *meta,
718 		  unsigned int type, __be32 *hash)
719 {
720 	if (!(netdev->features & NETIF_F_RXHASH))
721 		return;
722 
723 	switch (type) {
724 	case NFP_NET_RSS_IPV4:
725 	case NFP_NET_RSS_IPV6:
726 	case NFP_NET_RSS_IPV6_EX:
727 		meta->hash_type = PKT_HASH_TYPE_L3;
728 		break;
729 	default:
730 		meta->hash_type = PKT_HASH_TYPE_L4;
731 		break;
732 	}
733 
734 	meta->hash = get_unaligned_be32(hash);
735 }
736 
737 static bool
738 nfp_nfdk_parse_meta(struct net_device *netdev, struct nfp_meta_parsed *meta,
739 		    void *data, void *pkt, unsigned int pkt_len, int meta_len)
740 {
741 	u32 meta_info, vlan_info;
742 
743 	meta_info = get_unaligned_be32(data);
744 	data += 4;
745 
746 	while (meta_info) {
747 		switch (meta_info & NFP_NET_META_FIELD_MASK) {
748 		case NFP_NET_META_HASH:
749 			meta_info >>= NFP_NET_META_FIELD_SIZE;
750 			nfp_nfdk_set_hash(netdev, meta,
751 					  meta_info & NFP_NET_META_FIELD_MASK,
752 					  (__be32 *)data);
753 			data += 4;
754 			break;
755 		case NFP_NET_META_MARK:
756 			meta->mark = get_unaligned_be32(data);
757 			data += 4;
758 			break;
759 		case NFP_NET_META_VLAN:
760 			vlan_info = get_unaligned_be32(data);
761 			if (FIELD_GET(NFP_NET_META_VLAN_STRIP, vlan_info)) {
762 				meta->vlan.stripped = true;
763 				meta->vlan.tpid = FIELD_GET(NFP_NET_META_VLAN_TPID_MASK,
764 							    vlan_info);
765 				meta->vlan.tci = FIELD_GET(NFP_NET_META_VLAN_TCI_MASK,
766 							   vlan_info);
767 			}
768 			data += 4;
769 			break;
770 		case NFP_NET_META_PORTID:
771 			meta->portid = get_unaligned_be32(data);
772 			data += 4;
773 			break;
774 		case NFP_NET_META_CSUM:
775 			meta->csum_type = CHECKSUM_COMPLETE;
776 			meta->csum =
777 				(__force __wsum)__get_unaligned_cpu32(data);
778 			data += 4;
779 			break;
780 		case NFP_NET_META_RESYNC_INFO:
781 			if (nfp_net_tls_rx_resync_req(netdev, data, pkt,
782 						      pkt_len))
783 				return false;
784 			data += sizeof(struct nfp_net_tls_resync_req);
785 			break;
786 #ifdef CONFIG_NFP_NET_IPSEC
787 		case NFP_NET_META_IPSEC:
788 			/* Note: IPsec packet could have zero saidx, so need add 1
789 			 * to indicate packet is IPsec packet within driver.
790 			 */
791 			meta->ipsec_saidx = get_unaligned_be32(data) + 1;
792 			data += 4;
793 			break;
794 #endif
795 		default:
796 			return true;
797 		}
798 
799 		meta_info >>= NFP_NET_META_FIELD_SIZE;
800 	}
801 
802 	return data != pkt;
803 }
804 
805 static void
806 nfp_nfdk_rx_drop(const struct nfp_net_dp *dp, struct nfp_net_r_vector *r_vec,
807 		 struct nfp_net_rx_ring *rx_ring, struct nfp_net_rx_buf *rxbuf,
808 		 struct sk_buff *skb)
809 {
810 	u64_stats_update_begin(&r_vec->rx_sync);
811 	r_vec->rx_drops++;
812 	/* If we have both skb and rxbuf the replacement buffer allocation
813 	 * must have failed, count this as an alloc failure.
814 	 */
815 	if (skb && rxbuf)
816 		r_vec->rx_replace_buf_alloc_fail++;
817 	u64_stats_update_end(&r_vec->rx_sync);
818 
819 	/* skb is build based on the frag, free_skb() would free the frag
820 	 * so to be able to reuse it we need an extra ref.
821 	 */
822 	if (skb && rxbuf && skb->head == rxbuf->frag)
823 		page_ref_inc(virt_to_head_page(rxbuf->frag));
824 	if (rxbuf)
825 		nfp_nfdk_rx_give_one(dp, rx_ring, rxbuf->frag, rxbuf->dma_addr);
826 	if (skb)
827 		dev_kfree_skb_any(skb);
828 }
829 
830 static bool nfp_nfdk_xdp_complete(struct nfp_net_tx_ring *tx_ring)
831 {
832 	struct nfp_net_r_vector *r_vec = tx_ring->r_vec;
833 	struct nfp_net_dp *dp = &r_vec->nfp_net->dp;
834 	struct nfp_net_rx_ring *rx_ring;
835 	u32 qcp_rd_p, done = 0;
836 	bool done_all;
837 	int todo;
838 
839 	/* Work out how many descriptors have been transmitted */
840 	qcp_rd_p = nfp_net_read_tx_cmpl(tx_ring, dp);
841 	if (qcp_rd_p == tx_ring->qcp_rd_p)
842 		return true;
843 
844 	todo = D_IDX(tx_ring, qcp_rd_p - tx_ring->qcp_rd_p);
845 
846 	done_all = todo <= NFP_NET_XDP_MAX_COMPLETE;
847 	todo = min(todo, NFP_NET_XDP_MAX_COMPLETE);
848 
849 	rx_ring = r_vec->rx_ring;
850 	while (todo > 0) {
851 		int idx = D_IDX(tx_ring, tx_ring->rd_p + done);
852 		struct nfp_nfdk_tx_buf *txbuf;
853 		unsigned int step = 1;
854 
855 		txbuf = &tx_ring->ktxbufs[idx];
856 		if (!txbuf->raw)
857 			goto next;
858 
859 		if (NFDK_TX_BUF_INFO(txbuf->val) != NFDK_TX_BUF_INFO_SOP) {
860 			WARN_ONCE(1, "Unexpected TX buffer in XDP TX ring\n");
861 			goto next;
862 		}
863 
864 		/* Two successive txbufs are used to stash virtual and dma
865 		 * address respectively, recycle and clean them here.
866 		 */
867 		nfp_nfdk_rx_give_one(dp, rx_ring,
868 				     (void *)NFDK_TX_BUF_PTR(txbuf[0].val),
869 				     txbuf[1].dma_addr);
870 		txbuf[0].raw = 0;
871 		txbuf[1].raw = 0;
872 		step = 2;
873 
874 		u64_stats_update_begin(&r_vec->tx_sync);
875 		/* Note: tx_bytes not accumulated. */
876 		r_vec->tx_pkts++;
877 		u64_stats_update_end(&r_vec->tx_sync);
878 next:
879 		todo -= step;
880 		done += step;
881 	}
882 
883 	tx_ring->qcp_rd_p = D_IDX(tx_ring, tx_ring->qcp_rd_p + done);
884 	tx_ring->rd_p += done;
885 
886 	WARN_ONCE(tx_ring->wr_p - tx_ring->rd_p > tx_ring->cnt,
887 		  "XDP TX ring corruption rd_p=%u wr_p=%u cnt=%u\n",
888 		  tx_ring->rd_p, tx_ring->wr_p, tx_ring->cnt);
889 
890 	return done_all;
891 }
892 
893 static bool
894 nfp_nfdk_tx_xdp_buf(struct nfp_net_dp *dp, struct nfp_net_rx_ring *rx_ring,
895 		    struct nfp_net_tx_ring *tx_ring,
896 		    struct nfp_net_rx_buf *rxbuf, unsigned int dma_off,
897 		    unsigned int pkt_len, bool *completed)
898 {
899 	unsigned int dma_map_sz = dp->fl_bufsz - NFP_NET_RX_BUF_NON_DATA;
900 	unsigned int dma_len, type, cnt, dlen_type, tmp_dlen;
901 	struct nfp_nfdk_tx_buf *txbuf;
902 	struct nfp_nfdk_tx_desc *txd;
903 	unsigned int n_descs;
904 	dma_addr_t dma_addr;
905 	int wr_idx;
906 
907 	/* Reject if xdp_adjust_tail grow packet beyond DMA area */
908 	if (pkt_len + dma_off > dma_map_sz)
909 		return false;
910 
911 	/* Make sure there's still at least one block available after
912 	 * aligning to block boundary, so that the txds used below
913 	 * won't wrap around the tx_ring.
914 	 */
915 	if (unlikely(nfp_net_tx_full(tx_ring, NFDK_TX_DESC_STOP_CNT))) {
916 		if (!*completed) {
917 			nfp_nfdk_xdp_complete(tx_ring);
918 			*completed = true;
919 		}
920 
921 		if (unlikely(nfp_net_tx_full(tx_ring, NFDK_TX_DESC_STOP_CNT))) {
922 			nfp_nfdk_rx_drop(dp, rx_ring->r_vec, rx_ring, rxbuf,
923 					 NULL);
924 			return false;
925 		}
926 	}
927 
928 	/* Check if cross block boundary */
929 	n_descs = nfp_nfdk_headlen_to_segs(pkt_len);
930 	if ((round_down(tx_ring->wr_p, NFDK_TX_DESC_BLOCK_CNT) !=
931 	     round_down(tx_ring->wr_p + n_descs, NFDK_TX_DESC_BLOCK_CNT)) ||
932 	    ((u32)tx_ring->data_pending + pkt_len >
933 	     NFDK_TX_MAX_DATA_PER_BLOCK)) {
934 		unsigned int nop_slots = D_BLOCK_CPL(tx_ring->wr_p);
935 
936 		wr_idx = D_IDX(tx_ring, tx_ring->wr_p);
937 		txd = &tx_ring->ktxds[wr_idx];
938 		memset(txd, 0,
939 		       array_size(nop_slots, sizeof(struct nfp_nfdk_tx_desc)));
940 
941 		tx_ring->data_pending = 0;
942 		tx_ring->wr_p += nop_slots;
943 		tx_ring->wr_ptr_add += nop_slots;
944 	}
945 
946 	wr_idx = D_IDX(tx_ring, tx_ring->wr_p);
947 
948 	txbuf = &tx_ring->ktxbufs[wr_idx];
949 
950 	txbuf[0].val = (unsigned long)rxbuf->frag | NFDK_TX_BUF_INFO_SOP;
951 	txbuf[1].dma_addr = rxbuf->dma_addr;
952 	/* Note: pkt len not stored */
953 
954 	dma_sync_single_for_device(dp->dev, rxbuf->dma_addr + dma_off,
955 				   pkt_len, DMA_BIDIRECTIONAL);
956 
957 	/* Build TX descriptor */
958 	txd = &tx_ring->ktxds[wr_idx];
959 	dma_len = pkt_len;
960 	dma_addr = rxbuf->dma_addr + dma_off;
961 
962 	if (dma_len <= NFDK_TX_MAX_DATA_PER_HEAD)
963 		type = NFDK_DESC_TX_TYPE_SIMPLE;
964 	else
965 		type = NFDK_DESC_TX_TYPE_GATHER;
966 
967 	/* FIELD_PREP() implicitly truncates to chunk */
968 	dma_len -= 1;
969 	dlen_type = FIELD_PREP(NFDK_DESC_TX_DMA_LEN_HEAD,
970 			       dma_len > NFDK_DESC_TX_DMA_LEN_HEAD ?
971 			       NFDK_DESC_TX_DMA_LEN_HEAD : dma_len) |
972 		    FIELD_PREP(NFDK_DESC_TX_TYPE_HEAD, type);
973 
974 	txd->dma_len_type = cpu_to_le16(dlen_type);
975 	nfp_desc_set_dma_addr_48b(txd, dma_addr);
976 
977 	tmp_dlen = dlen_type & NFDK_DESC_TX_DMA_LEN_HEAD;
978 	dma_len -= tmp_dlen;
979 	dma_addr += tmp_dlen + 1;
980 	txd++;
981 
982 	while (dma_len > 0) {
983 		dma_len -= 1;
984 		dlen_type = FIELD_PREP(NFDK_DESC_TX_DMA_LEN, dma_len);
985 		txd->dma_len_type = cpu_to_le16(dlen_type);
986 		nfp_desc_set_dma_addr_48b(txd, dma_addr);
987 
988 		dlen_type &= NFDK_DESC_TX_DMA_LEN;
989 		dma_len -= dlen_type;
990 		dma_addr += dlen_type + 1;
991 		txd++;
992 	}
993 
994 	(txd - 1)->dma_len_type = cpu_to_le16(dlen_type | NFDK_DESC_TX_EOP);
995 
996 	/* Metadata desc */
997 	txd->raw = 0;
998 	txd++;
999 
1000 	cnt = txd - tx_ring->ktxds - wr_idx;
1001 	tx_ring->wr_p += cnt;
1002 	if (tx_ring->wr_p % NFDK_TX_DESC_BLOCK_CNT)
1003 		tx_ring->data_pending += pkt_len;
1004 	else
1005 		tx_ring->data_pending = 0;
1006 
1007 	tx_ring->wr_ptr_add += cnt;
1008 	return true;
1009 }
1010 
1011 /**
1012  * nfp_nfdk_rx() - receive up to @budget packets on @rx_ring
1013  * @rx_ring:   RX ring to receive from
1014  * @budget:    NAPI budget
1015  *
1016  * Note, this function is separated out from the napi poll function to
1017  * more cleanly separate packet receive code from other bookkeeping
1018  * functions performed in the napi poll function.
1019  *
1020  * Return: Number of packets received.
1021  */
1022 static int nfp_nfdk_rx(struct nfp_net_rx_ring *rx_ring, int budget)
1023 {
1024 	struct nfp_net_r_vector *r_vec = rx_ring->r_vec;
1025 	struct nfp_net_dp *dp = &r_vec->nfp_net->dp;
1026 	struct nfp_net_tx_ring *tx_ring;
1027 	struct bpf_prog *xdp_prog;
1028 	bool xdp_tx_cmpl = false;
1029 	unsigned int true_bufsz;
1030 	struct sk_buff *skb;
1031 	int pkts_polled = 0;
1032 	struct xdp_buff xdp;
1033 	int idx;
1034 
1035 	xdp_prog = READ_ONCE(dp->xdp_prog);
1036 	true_bufsz = xdp_prog ? PAGE_SIZE : dp->fl_bufsz;
1037 	xdp_init_buff(&xdp, PAGE_SIZE - NFP_NET_RX_BUF_HEADROOM,
1038 		      &rx_ring->xdp_rxq);
1039 	tx_ring = r_vec->xdp_ring;
1040 
1041 	while (pkts_polled < budget) {
1042 		unsigned int meta_len, data_len, meta_off, pkt_len, pkt_off;
1043 		struct nfp_net_rx_buf *rxbuf;
1044 		struct nfp_net_rx_desc *rxd;
1045 		struct nfp_meta_parsed meta;
1046 		bool redir_egress = false;
1047 		struct net_device *netdev;
1048 		dma_addr_t new_dma_addr;
1049 		u32 meta_len_xdp = 0;
1050 		void *new_frag;
1051 
1052 		idx = D_IDX(rx_ring, rx_ring->rd_p);
1053 
1054 		rxd = &rx_ring->rxds[idx];
1055 		if (!(rxd->rxd.meta_len_dd & PCIE_DESC_RX_DD))
1056 			break;
1057 
1058 		/* Memory barrier to ensure that we won't do other reads
1059 		 * before the DD bit.
1060 		 */
1061 		dma_rmb();
1062 
1063 		memset(&meta, 0, sizeof(meta));
1064 
1065 		rx_ring->rd_p++;
1066 		pkts_polled++;
1067 
1068 		rxbuf =	&rx_ring->rxbufs[idx];
1069 		/*         < meta_len >
1070 		 *  <-- [rx_offset] -->
1071 		 *  ---------------------------------------------------------
1072 		 * | [XX] |  metadata  |             packet           | XXXX |
1073 		 *  ---------------------------------------------------------
1074 		 *         <---------------- data_len --------------->
1075 		 *
1076 		 * The rx_offset is fixed for all packets, the meta_len can vary
1077 		 * on a packet by packet basis. If rx_offset is set to zero
1078 		 * (_RX_OFFSET_DYNAMIC) metadata starts at the beginning of the
1079 		 * buffer and is immediately followed by the packet (no [XX]).
1080 		 */
1081 		meta_len = rxd->rxd.meta_len_dd & PCIE_DESC_RX_META_LEN_MASK;
1082 		data_len = le16_to_cpu(rxd->rxd.data_len);
1083 		pkt_len = data_len - meta_len;
1084 
1085 		pkt_off = NFP_NET_RX_BUF_HEADROOM + dp->rx_dma_off;
1086 		if (dp->rx_offset == NFP_NET_CFG_RX_OFFSET_DYNAMIC)
1087 			pkt_off += meta_len;
1088 		else
1089 			pkt_off += dp->rx_offset;
1090 		meta_off = pkt_off - meta_len;
1091 
1092 		/* Stats update */
1093 		u64_stats_update_begin(&r_vec->rx_sync);
1094 		r_vec->rx_pkts++;
1095 		r_vec->rx_bytes += pkt_len;
1096 		u64_stats_update_end(&r_vec->rx_sync);
1097 
1098 		if (unlikely(meta_len > NFP_NET_MAX_PREPEND ||
1099 			     (dp->rx_offset && meta_len > dp->rx_offset))) {
1100 			nn_dp_warn(dp, "oversized RX packet metadata %u\n",
1101 				   meta_len);
1102 			nfp_nfdk_rx_drop(dp, r_vec, rx_ring, rxbuf, NULL);
1103 			continue;
1104 		}
1105 
1106 		nfp_net_dma_sync_cpu_rx(dp, rxbuf->dma_addr + meta_off,
1107 					data_len);
1108 
1109 		if (meta_len) {
1110 			if (unlikely(nfp_nfdk_parse_meta(dp->netdev, &meta,
1111 							 rxbuf->frag + meta_off,
1112 							 rxbuf->frag + pkt_off,
1113 							 pkt_len, meta_len))) {
1114 				nn_dp_warn(dp, "invalid RX packet metadata\n");
1115 				nfp_nfdk_rx_drop(dp, r_vec, rx_ring, rxbuf,
1116 						 NULL);
1117 				continue;
1118 			}
1119 		}
1120 
1121 		if (xdp_prog && !meta.portid) {
1122 			void *orig_data = rxbuf->frag + pkt_off;
1123 			unsigned int dma_off;
1124 			int act;
1125 
1126 			xdp_prepare_buff(&xdp,
1127 					 rxbuf->frag + NFP_NET_RX_BUF_HEADROOM,
1128 					 pkt_off - NFP_NET_RX_BUF_HEADROOM,
1129 					 pkt_len, true);
1130 
1131 			act = bpf_prog_run_xdp(xdp_prog, &xdp);
1132 
1133 			pkt_len = xdp.data_end - xdp.data;
1134 			pkt_off += xdp.data - orig_data;
1135 
1136 			switch (act) {
1137 			case XDP_PASS:
1138 				meta_len_xdp = xdp.data - xdp.data_meta;
1139 				break;
1140 			case XDP_TX:
1141 				dma_off = pkt_off - NFP_NET_RX_BUF_HEADROOM;
1142 				if (unlikely(!nfp_nfdk_tx_xdp_buf(dp, rx_ring,
1143 								  tx_ring,
1144 								  rxbuf,
1145 								  dma_off,
1146 								  pkt_len,
1147 								  &xdp_tx_cmpl)))
1148 					trace_xdp_exception(dp->netdev,
1149 							    xdp_prog, act);
1150 				continue;
1151 			default:
1152 				bpf_warn_invalid_xdp_action(dp->netdev, xdp_prog, act);
1153 				fallthrough;
1154 			case XDP_ABORTED:
1155 				trace_xdp_exception(dp->netdev, xdp_prog, act);
1156 				fallthrough;
1157 			case XDP_DROP:
1158 				nfp_nfdk_rx_give_one(dp, rx_ring, rxbuf->frag,
1159 						     rxbuf->dma_addr);
1160 				continue;
1161 			}
1162 		}
1163 
1164 		if (likely(!meta.portid)) {
1165 			netdev = dp->netdev;
1166 		} else if (meta.portid == NFP_META_PORT_ID_CTRL) {
1167 			struct nfp_net *nn = netdev_priv(dp->netdev);
1168 
1169 			nfp_app_ctrl_rx_raw(nn->app, rxbuf->frag + pkt_off,
1170 					    pkt_len);
1171 			nfp_nfdk_rx_give_one(dp, rx_ring, rxbuf->frag,
1172 					     rxbuf->dma_addr);
1173 			continue;
1174 		} else {
1175 			struct nfp_net *nn;
1176 
1177 			nn = netdev_priv(dp->netdev);
1178 			netdev = nfp_app_dev_get(nn->app, meta.portid,
1179 						 &redir_egress);
1180 			if (unlikely(!netdev)) {
1181 				nfp_nfdk_rx_drop(dp, r_vec, rx_ring, rxbuf,
1182 						 NULL);
1183 				continue;
1184 			}
1185 
1186 			if (nfp_netdev_is_nfp_repr(netdev))
1187 				nfp_repr_inc_rx_stats(netdev, pkt_len);
1188 		}
1189 
1190 		skb = build_skb(rxbuf->frag, true_bufsz);
1191 		if (unlikely(!skb)) {
1192 			nfp_nfdk_rx_drop(dp, r_vec, rx_ring, rxbuf, NULL);
1193 			continue;
1194 		}
1195 		new_frag = nfp_nfdk_napi_alloc_one(dp, &new_dma_addr);
1196 		if (unlikely(!new_frag)) {
1197 			nfp_nfdk_rx_drop(dp, r_vec, rx_ring, rxbuf, skb);
1198 			continue;
1199 		}
1200 
1201 		nfp_net_dma_unmap_rx(dp, rxbuf->dma_addr);
1202 
1203 		nfp_nfdk_rx_give_one(dp, rx_ring, new_frag, new_dma_addr);
1204 
1205 		skb_reserve(skb, pkt_off);
1206 		skb_put(skb, pkt_len);
1207 
1208 		skb->mark = meta.mark;
1209 		skb_set_hash(skb, meta.hash, meta.hash_type);
1210 
1211 		skb_record_rx_queue(skb, rx_ring->idx);
1212 		skb->protocol = eth_type_trans(skb, netdev);
1213 
1214 		nfp_nfdk_rx_csum(dp, r_vec, rxd, &meta, skb);
1215 
1216 		if (unlikely(!nfp_net_vlan_strip(skb, rxd, &meta))) {
1217 			nfp_nfdk_rx_drop(dp, r_vec, rx_ring, NULL, skb);
1218 			continue;
1219 		}
1220 
1221 #ifdef CONFIG_NFP_NET_IPSEC
1222 		if (meta.ipsec_saidx != 0 && unlikely(nfp_net_ipsec_rx(&meta, skb))) {
1223 			nfp_nfdk_rx_drop(dp, r_vec, rx_ring, NULL, skb);
1224 			continue;
1225 		}
1226 #endif
1227 
1228 		if (meta_len_xdp)
1229 			skb_metadata_set(skb, meta_len_xdp);
1230 
1231 		if (likely(!redir_egress)) {
1232 			napi_gro_receive(&rx_ring->r_vec->napi, skb);
1233 		} else {
1234 			skb->dev = netdev;
1235 			skb_reset_network_header(skb);
1236 			__skb_push(skb, ETH_HLEN);
1237 			dev_queue_xmit(skb);
1238 		}
1239 	}
1240 
1241 	if (xdp_prog) {
1242 		if (tx_ring->wr_ptr_add)
1243 			nfp_net_tx_xmit_more_flush(tx_ring);
1244 		else if (unlikely(tx_ring->wr_p != tx_ring->rd_p) &&
1245 			 !xdp_tx_cmpl)
1246 			if (!nfp_nfdk_xdp_complete(tx_ring))
1247 				pkts_polled = budget;
1248 	}
1249 
1250 	return pkts_polled;
1251 }
1252 
1253 /**
1254  * nfp_nfdk_poll() - napi poll function
1255  * @napi:    NAPI structure
1256  * @budget:  NAPI budget
1257  *
1258  * Return: number of packets polled.
1259  */
1260 int nfp_nfdk_poll(struct napi_struct *napi, int budget)
1261 {
1262 	struct nfp_net_r_vector *r_vec =
1263 		container_of(napi, struct nfp_net_r_vector, napi);
1264 	unsigned int pkts_polled = 0;
1265 
1266 	if (r_vec->tx_ring)
1267 		nfp_nfdk_tx_complete(r_vec->tx_ring, budget);
1268 	if (r_vec->rx_ring)
1269 		pkts_polled = nfp_nfdk_rx(r_vec->rx_ring, budget);
1270 
1271 	if (pkts_polled < budget)
1272 		if (napi_complete_done(napi, pkts_polled))
1273 			nfp_net_irq_unmask(r_vec->nfp_net, r_vec->irq_entry);
1274 
1275 	if (r_vec->nfp_net->rx_coalesce_adapt_on && r_vec->rx_ring) {
1276 		struct dim_sample dim_sample = {};
1277 		unsigned int start;
1278 		u64 pkts, bytes;
1279 
1280 		do {
1281 			start = u64_stats_fetch_begin(&r_vec->rx_sync);
1282 			pkts = r_vec->rx_pkts;
1283 			bytes = r_vec->rx_bytes;
1284 		} while (u64_stats_fetch_retry(&r_vec->rx_sync, start));
1285 
1286 		dim_update_sample(r_vec->event_ctr, pkts, bytes, &dim_sample);
1287 		net_dim(&r_vec->rx_dim, dim_sample);
1288 	}
1289 
1290 	if (r_vec->nfp_net->tx_coalesce_adapt_on && r_vec->tx_ring) {
1291 		struct dim_sample dim_sample = {};
1292 		unsigned int start;
1293 		u64 pkts, bytes;
1294 
1295 		do {
1296 			start = u64_stats_fetch_begin(&r_vec->tx_sync);
1297 			pkts = r_vec->tx_pkts;
1298 			bytes = r_vec->tx_bytes;
1299 		} while (u64_stats_fetch_retry(&r_vec->tx_sync, start));
1300 
1301 		dim_update_sample(r_vec->event_ctr, pkts, bytes, &dim_sample);
1302 		net_dim(&r_vec->tx_dim, dim_sample);
1303 	}
1304 
1305 	return pkts_polled;
1306 }
1307 
1308 /* Control device data path
1309  */
1310 
1311 bool
1312 nfp_nfdk_ctrl_tx_one(struct nfp_net *nn, struct nfp_net_r_vector *r_vec,
1313 		     struct sk_buff *skb, bool old)
1314 {
1315 	u32 cnt, tmp_dlen, dlen_type = 0;
1316 	struct nfp_net_tx_ring *tx_ring;
1317 	struct nfp_nfdk_tx_buf *txbuf;
1318 	struct nfp_nfdk_tx_desc *txd;
1319 	unsigned int dma_len, type;
1320 	struct nfp_net_dp *dp;
1321 	dma_addr_t dma_addr;
1322 	u64 metadata = 0;
1323 	int wr_idx;
1324 
1325 	dp = &r_vec->nfp_net->dp;
1326 	tx_ring = r_vec->tx_ring;
1327 
1328 	if (WARN_ON_ONCE(skb_shinfo(skb)->nr_frags)) {
1329 		nn_dp_warn(dp, "Driver's CTRL TX does not implement gather\n");
1330 		goto err_free;
1331 	}
1332 
1333 	/* Don't bother counting frags, assume the worst */
1334 	if (unlikely(nfp_net_tx_full(tx_ring, NFDK_TX_DESC_STOP_CNT))) {
1335 		u64_stats_update_begin(&r_vec->tx_sync);
1336 		r_vec->tx_busy++;
1337 		u64_stats_update_end(&r_vec->tx_sync);
1338 		if (!old)
1339 			__skb_queue_tail(&r_vec->queue, skb);
1340 		else
1341 			__skb_queue_head(&r_vec->queue, skb);
1342 		return NETDEV_TX_BUSY;
1343 	}
1344 
1345 	if (nfp_app_ctrl_has_meta(nn->app)) {
1346 		if (unlikely(skb_headroom(skb) < 8)) {
1347 			nn_dp_warn(dp, "CTRL TX on skb without headroom\n");
1348 			goto err_free;
1349 		}
1350 		metadata = NFDK_DESC_TX_CHAIN_META;
1351 		put_unaligned_be32(NFP_META_PORT_ID_CTRL, skb_push(skb, 4));
1352 		put_unaligned_be32(FIELD_PREP(NFDK_META_LEN, 8) |
1353 				   FIELD_PREP(NFDK_META_FIELDS,
1354 					      NFP_NET_META_PORTID),
1355 				   skb_push(skb, 4));
1356 	}
1357 
1358 	if (nfp_nfdk_tx_maybe_close_block(tx_ring, skb))
1359 		goto err_free;
1360 
1361 	/* DMA map all */
1362 	wr_idx = D_IDX(tx_ring, tx_ring->wr_p);
1363 	txd = &tx_ring->ktxds[wr_idx];
1364 	txbuf = &tx_ring->ktxbufs[wr_idx];
1365 
1366 	dma_len = skb_headlen(skb);
1367 	if (dma_len <= NFDK_TX_MAX_DATA_PER_HEAD)
1368 		type = NFDK_DESC_TX_TYPE_SIMPLE;
1369 	else
1370 		type = NFDK_DESC_TX_TYPE_GATHER;
1371 
1372 	dma_addr = dma_map_single(dp->dev, skb->data, dma_len, DMA_TO_DEVICE);
1373 	if (dma_mapping_error(dp->dev, dma_addr))
1374 		goto err_warn_dma;
1375 
1376 	txbuf->skb = skb;
1377 	txbuf++;
1378 
1379 	txbuf->dma_addr = dma_addr;
1380 	txbuf++;
1381 
1382 	dma_len -= 1;
1383 	dlen_type = FIELD_PREP(NFDK_DESC_TX_DMA_LEN_HEAD,
1384 			       dma_len > NFDK_DESC_TX_DMA_LEN_HEAD ?
1385 			       NFDK_DESC_TX_DMA_LEN_HEAD : dma_len) |
1386 		    FIELD_PREP(NFDK_DESC_TX_TYPE_HEAD, type);
1387 
1388 	txd->dma_len_type = cpu_to_le16(dlen_type);
1389 	nfp_desc_set_dma_addr_48b(txd, dma_addr);
1390 
1391 	tmp_dlen = dlen_type & NFDK_DESC_TX_DMA_LEN_HEAD;
1392 	dma_len -= tmp_dlen;
1393 	dma_addr += tmp_dlen + 1;
1394 	txd++;
1395 
1396 	while (dma_len > 0) {
1397 		dma_len -= 1;
1398 		dlen_type = FIELD_PREP(NFDK_DESC_TX_DMA_LEN, dma_len);
1399 		txd->dma_len_type = cpu_to_le16(dlen_type);
1400 		nfp_desc_set_dma_addr_48b(txd, dma_addr);
1401 
1402 		dlen_type &= NFDK_DESC_TX_DMA_LEN;
1403 		dma_len -= dlen_type;
1404 		dma_addr += dlen_type + 1;
1405 		txd++;
1406 	}
1407 
1408 	(txd - 1)->dma_len_type = cpu_to_le16(dlen_type | NFDK_DESC_TX_EOP);
1409 
1410 	/* Metadata desc */
1411 	txd->raw = cpu_to_le64(metadata);
1412 	txd++;
1413 
1414 	cnt = txd - tx_ring->ktxds - wr_idx;
1415 	if (unlikely(round_down(wr_idx, NFDK_TX_DESC_BLOCK_CNT) !=
1416 		     round_down(wr_idx + cnt - 1, NFDK_TX_DESC_BLOCK_CNT)))
1417 		goto err_warn_overflow;
1418 
1419 	tx_ring->wr_p += cnt;
1420 	if (tx_ring->wr_p % NFDK_TX_DESC_BLOCK_CNT)
1421 		tx_ring->data_pending += skb->len;
1422 	else
1423 		tx_ring->data_pending = 0;
1424 
1425 	tx_ring->wr_ptr_add += cnt;
1426 	nfp_net_tx_xmit_more_flush(tx_ring);
1427 
1428 	return NETDEV_TX_OK;
1429 
1430 err_warn_overflow:
1431 	WARN_ONCE(1, "unable to fit packet into a descriptor wr_idx:%d head:%d frags:%d cnt:%d",
1432 		  wr_idx, skb_headlen(skb), 0, cnt);
1433 	txbuf--;
1434 	dma_unmap_single(dp->dev, txbuf->dma_addr,
1435 			 skb_headlen(skb), DMA_TO_DEVICE);
1436 	txbuf->raw = 0;
1437 err_warn_dma:
1438 	nn_dp_warn(dp, "Failed to map DMA TX buffer\n");
1439 err_free:
1440 	u64_stats_update_begin(&r_vec->tx_sync);
1441 	r_vec->tx_errors++;
1442 	u64_stats_update_end(&r_vec->tx_sync);
1443 	dev_kfree_skb_any(skb);
1444 	return NETDEV_TX_OK;
1445 }
1446 
1447 static void __nfp_ctrl_tx_queued(struct nfp_net_r_vector *r_vec)
1448 {
1449 	struct sk_buff *skb;
1450 
1451 	while ((skb = __skb_dequeue(&r_vec->queue)))
1452 		if (nfp_nfdk_ctrl_tx_one(r_vec->nfp_net, r_vec, skb, true))
1453 			return;
1454 }
1455 
1456 static bool
1457 nfp_ctrl_meta_ok(struct nfp_net *nn, void *data, unsigned int meta_len)
1458 {
1459 	u32 meta_type, meta_tag;
1460 
1461 	if (!nfp_app_ctrl_has_meta(nn->app))
1462 		return !meta_len;
1463 
1464 	if (meta_len != 8)
1465 		return false;
1466 
1467 	meta_type = get_unaligned_be32(data);
1468 	meta_tag = get_unaligned_be32(data + 4);
1469 
1470 	return (meta_type == NFP_NET_META_PORTID &&
1471 		meta_tag == NFP_META_PORT_ID_CTRL);
1472 }
1473 
1474 static bool
1475 nfp_ctrl_rx_one(struct nfp_net *nn, struct nfp_net_dp *dp,
1476 		struct nfp_net_r_vector *r_vec, struct nfp_net_rx_ring *rx_ring)
1477 {
1478 	unsigned int meta_len, data_len, meta_off, pkt_len, pkt_off;
1479 	struct nfp_net_rx_buf *rxbuf;
1480 	struct nfp_net_rx_desc *rxd;
1481 	dma_addr_t new_dma_addr;
1482 	struct sk_buff *skb;
1483 	void *new_frag;
1484 	int idx;
1485 
1486 	idx = D_IDX(rx_ring, rx_ring->rd_p);
1487 
1488 	rxd = &rx_ring->rxds[idx];
1489 	if (!(rxd->rxd.meta_len_dd & PCIE_DESC_RX_DD))
1490 		return false;
1491 
1492 	/* Memory barrier to ensure that we won't do other reads
1493 	 * before the DD bit.
1494 	 */
1495 	dma_rmb();
1496 
1497 	rx_ring->rd_p++;
1498 
1499 	rxbuf =	&rx_ring->rxbufs[idx];
1500 	meta_len = rxd->rxd.meta_len_dd & PCIE_DESC_RX_META_LEN_MASK;
1501 	data_len = le16_to_cpu(rxd->rxd.data_len);
1502 	pkt_len = data_len - meta_len;
1503 
1504 	pkt_off = NFP_NET_RX_BUF_HEADROOM + dp->rx_dma_off;
1505 	if (dp->rx_offset == NFP_NET_CFG_RX_OFFSET_DYNAMIC)
1506 		pkt_off += meta_len;
1507 	else
1508 		pkt_off += dp->rx_offset;
1509 	meta_off = pkt_off - meta_len;
1510 
1511 	/* Stats update */
1512 	u64_stats_update_begin(&r_vec->rx_sync);
1513 	r_vec->rx_pkts++;
1514 	r_vec->rx_bytes += pkt_len;
1515 	u64_stats_update_end(&r_vec->rx_sync);
1516 
1517 	nfp_net_dma_sync_cpu_rx(dp, rxbuf->dma_addr + meta_off,	data_len);
1518 
1519 	if (unlikely(!nfp_ctrl_meta_ok(nn, rxbuf->frag + meta_off, meta_len))) {
1520 		nn_dp_warn(dp, "incorrect metadata for ctrl packet (%d)\n",
1521 			   meta_len);
1522 		nfp_nfdk_rx_drop(dp, r_vec, rx_ring, rxbuf, NULL);
1523 		return true;
1524 	}
1525 
1526 	skb = build_skb(rxbuf->frag, dp->fl_bufsz);
1527 	if (unlikely(!skb)) {
1528 		nfp_nfdk_rx_drop(dp, r_vec, rx_ring, rxbuf, NULL);
1529 		return true;
1530 	}
1531 	new_frag = nfp_nfdk_napi_alloc_one(dp, &new_dma_addr);
1532 	if (unlikely(!new_frag)) {
1533 		nfp_nfdk_rx_drop(dp, r_vec, rx_ring, rxbuf, skb);
1534 		return true;
1535 	}
1536 
1537 	nfp_net_dma_unmap_rx(dp, rxbuf->dma_addr);
1538 
1539 	nfp_nfdk_rx_give_one(dp, rx_ring, new_frag, new_dma_addr);
1540 
1541 	skb_reserve(skb, pkt_off);
1542 	skb_put(skb, pkt_len);
1543 
1544 	nfp_app_ctrl_rx(nn->app, skb);
1545 
1546 	return true;
1547 }
1548 
1549 static bool nfp_ctrl_rx(struct nfp_net_r_vector *r_vec)
1550 {
1551 	struct nfp_net_rx_ring *rx_ring = r_vec->rx_ring;
1552 	struct nfp_net *nn = r_vec->nfp_net;
1553 	struct nfp_net_dp *dp = &nn->dp;
1554 	unsigned int budget = 512;
1555 
1556 	while (nfp_ctrl_rx_one(nn, dp, r_vec, rx_ring) && budget--)
1557 		continue;
1558 
1559 	return budget;
1560 }
1561 
1562 void nfp_nfdk_ctrl_poll(struct tasklet_struct *t)
1563 {
1564 	struct nfp_net_r_vector *r_vec = from_tasklet(r_vec, t, tasklet);
1565 
1566 	spin_lock(&r_vec->lock);
1567 	nfp_nfdk_tx_complete(r_vec->tx_ring, 0);
1568 	__nfp_ctrl_tx_queued(r_vec);
1569 	spin_unlock(&r_vec->lock);
1570 
1571 	if (nfp_ctrl_rx(r_vec)) {
1572 		nfp_net_irq_unmask(r_vec->nfp_net, r_vec->irq_entry);
1573 	} else {
1574 		tasklet_schedule(&r_vec->tasklet);
1575 		nn_dp_warn(&r_vec->nfp_net->dp,
1576 			   "control message budget exceeded!\n");
1577 	}
1578 }
1579