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/bitfield.h>
7 #include <net/xfrm.h>
8 
9 #include "../nfp_app.h"
10 #include "../nfp_net.h"
11 #include "../nfp_net_dp.h"
12 #include "../nfp_net_xsk.h"
13 #include "../crypto/crypto.h"
14 #include "../crypto/fw.h"
15 #include "nfd3.h"
16 
17 /* Transmit processing
18  *
19  * One queue controller peripheral queue is used for transmit.  The
20  * driver en-queues packets for transmit by advancing the write
21  * pointer.  The device indicates that packets have transmitted by
22  * advancing the read pointer.  The driver maintains a local copy of
23  * the read and write pointer in @struct nfp_net_tx_ring.  The driver
24  * keeps @wr_p in sync with the queue controller write pointer and can
25  * determine how many packets have been transmitted by comparing its
26  * copy of the read pointer @rd_p with the read pointer maintained by
27  * the queue controller peripheral.
28  */
29 
30 /* Wrappers for deciding when to stop and restart TX queues */
31 static int nfp_nfd3_tx_ring_should_wake(struct nfp_net_tx_ring *tx_ring)
32 {
33 	return !nfp_net_tx_full(tx_ring, MAX_SKB_FRAGS * 4);
34 }
35 
36 static int nfp_nfd3_tx_ring_should_stop(struct nfp_net_tx_ring *tx_ring)
37 {
38 	return nfp_net_tx_full(tx_ring, MAX_SKB_FRAGS + 1);
39 }
40 
41 /**
42  * nfp_nfd3_tx_ring_stop() - stop tx ring
43  * @nd_q:    netdev queue
44  * @tx_ring: driver tx queue structure
45  *
46  * Safely stop TX ring.  Remember that while we are running .start_xmit()
47  * someone else may be cleaning the TX ring completions so we need to be
48  * extra careful here.
49  */
50 static void
51 nfp_nfd3_tx_ring_stop(struct netdev_queue *nd_q,
52 		      struct nfp_net_tx_ring *tx_ring)
53 {
54 	netif_tx_stop_queue(nd_q);
55 
56 	/* We can race with the TX completion out of NAPI so recheck */
57 	smp_mb();
58 	if (unlikely(nfp_nfd3_tx_ring_should_wake(tx_ring)))
59 		netif_tx_start_queue(nd_q);
60 }
61 
62 /**
63  * nfp_nfd3_tx_tso() - Set up Tx descriptor for LSO
64  * @r_vec: per-ring structure
65  * @txbuf: Pointer to driver soft TX descriptor
66  * @txd: Pointer to HW TX descriptor
67  * @skb: Pointer to SKB
68  * @md_bytes: Prepend length
69  *
70  * Set up Tx descriptor for LSO, do nothing for non-LSO skbs.
71  * Return error on packet header greater than maximum supported LSO header size.
72  */
73 static void
74 nfp_nfd3_tx_tso(struct nfp_net_r_vector *r_vec, struct nfp_nfd3_tx_buf *txbuf,
75 		struct nfp_nfd3_tx_desc *txd, struct sk_buff *skb, u32 md_bytes)
76 {
77 	u32 l3_offset, l4_offset, hdrlen;
78 	u16 mss;
79 
80 	if (!skb_is_gso(skb))
81 		return;
82 
83 	if (!skb->encapsulation) {
84 		l3_offset = skb_network_offset(skb);
85 		l4_offset = skb_transport_offset(skb);
86 		hdrlen = skb_tcp_all_headers(skb);
87 	} else {
88 		l3_offset = skb_inner_network_offset(skb);
89 		l4_offset = skb_inner_transport_offset(skb);
90 		hdrlen = skb_inner_tcp_all_headers(skb);
91 	}
92 
93 	txbuf->pkt_cnt = skb_shinfo(skb)->gso_segs;
94 	txbuf->real_len += hdrlen * (txbuf->pkt_cnt - 1);
95 
96 	mss = skb_shinfo(skb)->gso_size & NFD3_DESC_TX_MSS_MASK;
97 	txd->l3_offset = l3_offset - md_bytes;
98 	txd->l4_offset = l4_offset - md_bytes;
99 	txd->lso_hdrlen = hdrlen - md_bytes;
100 	txd->mss = cpu_to_le16(mss);
101 	txd->flags |= NFD3_DESC_TX_LSO;
102 
103 	u64_stats_update_begin(&r_vec->tx_sync);
104 	r_vec->tx_lso++;
105 	u64_stats_update_end(&r_vec->tx_sync);
106 }
107 
108 /**
109  * nfp_nfd3_tx_csum() - Set TX CSUM offload flags in TX descriptor
110  * @dp:  NFP Net data path struct
111  * @r_vec: per-ring structure
112  * @txbuf: Pointer to driver soft TX descriptor
113  * @txd: Pointer to TX descriptor
114  * @skb: Pointer to SKB
115  *
116  * This function sets the TX checksum flags in the TX descriptor based
117  * on the configuration and the protocol of the packet to be transmitted.
118  */
119 static void
120 nfp_nfd3_tx_csum(struct nfp_net_dp *dp, struct nfp_net_r_vector *r_vec,
121 		 struct nfp_nfd3_tx_buf *txbuf, struct nfp_nfd3_tx_desc *txd,
122 		 struct sk_buff *skb)
123 {
124 	struct ipv6hdr *ipv6h;
125 	struct iphdr *iph;
126 	u8 l4_hdr;
127 
128 	if (!(dp->ctrl & NFP_NET_CFG_CTRL_TXCSUM))
129 		return;
130 
131 	if (skb->ip_summed != CHECKSUM_PARTIAL)
132 		return;
133 
134 	txd->flags |= NFD3_DESC_TX_CSUM;
135 	if (skb->encapsulation)
136 		txd->flags |= NFD3_DESC_TX_ENCAP;
137 
138 	iph = skb->encapsulation ? inner_ip_hdr(skb) : ip_hdr(skb);
139 	ipv6h = skb->encapsulation ? inner_ipv6_hdr(skb) : ipv6_hdr(skb);
140 
141 	if (iph->version == 4) {
142 		txd->flags |= NFD3_DESC_TX_IP4_CSUM;
143 		l4_hdr = iph->protocol;
144 	} else if (ipv6h->version == 6) {
145 		l4_hdr = ipv6h->nexthdr;
146 	} else {
147 		nn_dp_warn(dp, "partial checksum but ipv=%x!\n", iph->version);
148 		return;
149 	}
150 
151 	switch (l4_hdr) {
152 	case IPPROTO_TCP:
153 		txd->flags |= NFD3_DESC_TX_TCP_CSUM;
154 		break;
155 	case IPPROTO_UDP:
156 		txd->flags |= NFD3_DESC_TX_UDP_CSUM;
157 		break;
158 	default:
159 		nn_dp_warn(dp, "partial checksum but l4 proto=%x!\n", l4_hdr);
160 		return;
161 	}
162 
163 	u64_stats_update_begin(&r_vec->tx_sync);
164 	if (skb->encapsulation)
165 		r_vec->hw_csum_tx_inner += txbuf->pkt_cnt;
166 	else
167 		r_vec->hw_csum_tx += txbuf->pkt_cnt;
168 	u64_stats_update_end(&r_vec->tx_sync);
169 }
170 
171 static int nfp_nfd3_prep_tx_meta(struct nfp_net_dp *dp, struct sk_buff *skb,
172 				 u64 tls_handle, bool *ipsec)
173 {
174 	struct metadata_dst *md_dst = skb_metadata_dst(skb);
175 	struct nfp_ipsec_offload offload_info;
176 	unsigned char *data;
177 	bool vlan_insert;
178 	u32 meta_id = 0;
179 	int md_bytes;
180 
181 #ifdef CONFIG_NFP_NET_IPSEC
182 	if (xfrm_offload(skb))
183 		*ipsec = nfp_net_ipsec_tx_prep(dp, skb, &offload_info);
184 #endif
185 
186 	if (unlikely(md_dst && md_dst->type != METADATA_HW_PORT_MUX))
187 		md_dst = NULL;
188 
189 	vlan_insert = skb_vlan_tag_present(skb) && (dp->ctrl & NFP_NET_CFG_CTRL_TXVLAN_V2);
190 
191 	if (!(md_dst || tls_handle || vlan_insert || *ipsec))
192 		return 0;
193 
194 	md_bytes = sizeof(meta_id) +
195 		   (!!md_dst ? NFP_NET_META_PORTID_SIZE : 0) +
196 		   (!!tls_handle ? NFP_NET_META_CONN_HANDLE_SIZE : 0) +
197 		   (vlan_insert ? NFP_NET_META_VLAN_SIZE : 0) +
198 		   (*ipsec ? NFP_NET_META_IPSEC_FIELD_SIZE : 0);
199 
200 	if (unlikely(skb_cow_head(skb, md_bytes)))
201 		return -ENOMEM;
202 
203 	data = skb_push(skb, md_bytes) + md_bytes;
204 	if (md_dst) {
205 		data -= NFP_NET_META_PORTID_SIZE;
206 		put_unaligned_be32(md_dst->u.port_info.port_id, data);
207 		meta_id = NFP_NET_META_PORTID;
208 	}
209 	if (tls_handle) {
210 		/* conn handle is opaque, we just use u64 to be able to quickly
211 		 * compare it to zero
212 		 */
213 		data -= NFP_NET_META_CONN_HANDLE_SIZE;
214 		memcpy(data, &tls_handle, sizeof(tls_handle));
215 		meta_id <<= NFP_NET_META_FIELD_SIZE;
216 		meta_id |= NFP_NET_META_CONN_HANDLE;
217 	}
218 	if (vlan_insert) {
219 		data -= NFP_NET_META_VLAN_SIZE;
220 		/* data type of skb->vlan_proto is __be16
221 		 * so it fills metadata without calling put_unaligned_be16
222 		 */
223 		memcpy(data, &skb->vlan_proto, sizeof(skb->vlan_proto));
224 		put_unaligned_be16(skb_vlan_tag_get(skb), data + sizeof(skb->vlan_proto));
225 		meta_id <<= NFP_NET_META_FIELD_SIZE;
226 		meta_id |= NFP_NET_META_VLAN;
227 	}
228 	if (*ipsec) {
229 		data -= NFP_NET_META_IPSEC_SIZE;
230 		put_unaligned_be32(offload_info.seq_hi, data);
231 		data -= NFP_NET_META_IPSEC_SIZE;
232 		put_unaligned_be32(offload_info.seq_low, data);
233 		data -= NFP_NET_META_IPSEC_SIZE;
234 		put_unaligned_be32(offload_info.handle - 1, data);
235 		meta_id <<= NFP_NET_META_IPSEC_FIELD_SIZE;
236 		meta_id |= NFP_NET_META_IPSEC << 8 | NFP_NET_META_IPSEC << 4 | NFP_NET_META_IPSEC;
237 	}
238 
239 	data -= sizeof(meta_id);
240 	put_unaligned_be32(meta_id, data);
241 
242 	return md_bytes;
243 }
244 
245 /**
246  * nfp_nfd3_tx() - Main transmit entry point
247  * @skb:    SKB to transmit
248  * @netdev: netdev structure
249  *
250  * Return: NETDEV_TX_OK on success.
251  */
252 netdev_tx_t nfp_nfd3_tx(struct sk_buff *skb, struct net_device *netdev)
253 {
254 	struct nfp_net *nn = netdev_priv(netdev);
255 	int f, nr_frags, wr_idx, md_bytes;
256 	struct nfp_net_tx_ring *tx_ring;
257 	struct nfp_net_r_vector *r_vec;
258 	struct nfp_nfd3_tx_buf *txbuf;
259 	struct nfp_nfd3_tx_desc *txd;
260 	struct netdev_queue *nd_q;
261 	const skb_frag_t *frag;
262 	struct nfp_net_dp *dp;
263 	dma_addr_t dma_addr;
264 	unsigned int fsize;
265 	u64 tls_handle = 0;
266 	bool ipsec = false;
267 	u16 qidx;
268 
269 	dp = &nn->dp;
270 	qidx = skb_get_queue_mapping(skb);
271 	tx_ring = &dp->tx_rings[qidx];
272 	r_vec = tx_ring->r_vec;
273 
274 	nr_frags = skb_shinfo(skb)->nr_frags;
275 
276 	if (unlikely(nfp_net_tx_full(tx_ring, nr_frags + 1))) {
277 		nn_dp_warn(dp, "TX ring %d busy. wrp=%u rdp=%u\n",
278 			   qidx, tx_ring->wr_p, tx_ring->rd_p);
279 		nd_q = netdev_get_tx_queue(dp->netdev, qidx);
280 		netif_tx_stop_queue(nd_q);
281 		nfp_net_tx_xmit_more_flush(tx_ring);
282 		u64_stats_update_begin(&r_vec->tx_sync);
283 		r_vec->tx_busy++;
284 		u64_stats_update_end(&r_vec->tx_sync);
285 		return NETDEV_TX_BUSY;
286 	}
287 
288 	skb = nfp_net_tls_tx(dp, r_vec, skb, &tls_handle, &nr_frags);
289 	if (unlikely(!skb)) {
290 		nfp_net_tx_xmit_more_flush(tx_ring);
291 		return NETDEV_TX_OK;
292 	}
293 
294 	md_bytes = nfp_nfd3_prep_tx_meta(dp, skb, tls_handle, &ipsec);
295 	if (unlikely(md_bytes < 0))
296 		goto err_flush;
297 
298 	/* Start with the head skbuf */
299 	dma_addr = dma_map_single(dp->dev, skb->data, skb_headlen(skb),
300 				  DMA_TO_DEVICE);
301 	if (dma_mapping_error(dp->dev, dma_addr))
302 		goto err_dma_err;
303 
304 	wr_idx = D_IDX(tx_ring, tx_ring->wr_p);
305 
306 	/* Stash the soft descriptor of the head then initialize it */
307 	txbuf = &tx_ring->txbufs[wr_idx];
308 	txbuf->skb = skb;
309 	txbuf->dma_addr = dma_addr;
310 	txbuf->fidx = -1;
311 	txbuf->pkt_cnt = 1;
312 	txbuf->real_len = skb->len;
313 
314 	/* Build TX descriptor */
315 	txd = &tx_ring->txds[wr_idx];
316 	txd->offset_eop = (nr_frags ? 0 : NFD3_DESC_TX_EOP) | md_bytes;
317 	txd->dma_len = cpu_to_le16(skb_headlen(skb));
318 	nfp_desc_set_dma_addr_40b(txd, dma_addr);
319 	txd->data_len = cpu_to_le16(skb->len);
320 
321 	txd->flags = 0;
322 	txd->mss = 0;
323 	txd->lso_hdrlen = 0;
324 
325 	/* Do not reorder - tso may adjust pkt cnt, vlan may override fields */
326 	nfp_nfd3_tx_tso(r_vec, txbuf, txd, skb, md_bytes);
327 	nfp_nfd3_tx_csum(dp, r_vec, txbuf, txd, skb);
328 	if (skb_vlan_tag_present(skb) && dp->ctrl & NFP_NET_CFG_CTRL_TXVLAN) {
329 		txd->flags |= NFD3_DESC_TX_VLAN;
330 		txd->vlan = cpu_to_le16(skb_vlan_tag_get(skb));
331 	}
332 
333 	if (ipsec)
334 		nfp_nfd3_ipsec_tx(txd, skb);
335 	/* Gather DMA */
336 	if (nr_frags > 0) {
337 		__le64 second_half;
338 
339 		/* all descs must match except for in addr, length and eop */
340 		second_half = txd->vals8[1];
341 
342 		for (f = 0; f < nr_frags; f++) {
343 			frag = &skb_shinfo(skb)->frags[f];
344 			fsize = skb_frag_size(frag);
345 
346 			dma_addr = skb_frag_dma_map(dp->dev, frag, 0,
347 						    fsize, DMA_TO_DEVICE);
348 			if (dma_mapping_error(dp->dev, dma_addr))
349 				goto err_unmap;
350 
351 			wr_idx = D_IDX(tx_ring, wr_idx + 1);
352 			tx_ring->txbufs[wr_idx].skb = skb;
353 			tx_ring->txbufs[wr_idx].dma_addr = dma_addr;
354 			tx_ring->txbufs[wr_idx].fidx = f;
355 
356 			txd = &tx_ring->txds[wr_idx];
357 			txd->dma_len = cpu_to_le16(fsize);
358 			nfp_desc_set_dma_addr_40b(txd, dma_addr);
359 			txd->offset_eop = md_bytes |
360 				((f == nr_frags - 1) ? NFD3_DESC_TX_EOP : 0);
361 			txd->vals8[1] = second_half;
362 		}
363 
364 		u64_stats_update_begin(&r_vec->tx_sync);
365 		r_vec->tx_gather++;
366 		u64_stats_update_end(&r_vec->tx_sync);
367 	}
368 
369 	skb_tx_timestamp(skb);
370 
371 	nd_q = netdev_get_tx_queue(dp->netdev, tx_ring->idx);
372 
373 	tx_ring->wr_p += nr_frags + 1;
374 	if (nfp_nfd3_tx_ring_should_stop(tx_ring))
375 		nfp_nfd3_tx_ring_stop(nd_q, tx_ring);
376 
377 	tx_ring->wr_ptr_add += nr_frags + 1;
378 	if (__netdev_tx_sent_queue(nd_q, txbuf->real_len, netdev_xmit_more()))
379 		nfp_net_tx_xmit_more_flush(tx_ring);
380 
381 	return NETDEV_TX_OK;
382 
383 err_unmap:
384 	while (--f >= 0) {
385 		frag = &skb_shinfo(skb)->frags[f];
386 		dma_unmap_page(dp->dev, tx_ring->txbufs[wr_idx].dma_addr,
387 			       skb_frag_size(frag), DMA_TO_DEVICE);
388 		tx_ring->txbufs[wr_idx].skb = NULL;
389 		tx_ring->txbufs[wr_idx].dma_addr = 0;
390 		tx_ring->txbufs[wr_idx].fidx = -2;
391 		wr_idx = wr_idx - 1;
392 		if (wr_idx < 0)
393 			wr_idx += tx_ring->cnt;
394 	}
395 	dma_unmap_single(dp->dev, tx_ring->txbufs[wr_idx].dma_addr,
396 			 skb_headlen(skb), DMA_TO_DEVICE);
397 	tx_ring->txbufs[wr_idx].skb = NULL;
398 	tx_ring->txbufs[wr_idx].dma_addr = 0;
399 	tx_ring->txbufs[wr_idx].fidx = -2;
400 err_dma_err:
401 	nn_dp_warn(dp, "Failed to map DMA TX buffer\n");
402 err_flush:
403 	nfp_net_tx_xmit_more_flush(tx_ring);
404 	u64_stats_update_begin(&r_vec->tx_sync);
405 	r_vec->tx_errors++;
406 	u64_stats_update_end(&r_vec->tx_sync);
407 	nfp_net_tls_tx_undo(skb, tls_handle);
408 	dev_kfree_skb_any(skb);
409 	return NETDEV_TX_OK;
410 }
411 
412 /**
413  * nfp_nfd3_tx_complete() - Handled completed TX packets
414  * @tx_ring:	TX ring structure
415  * @budget:	NAPI budget (only used as bool to determine if in NAPI context)
416  */
417 void nfp_nfd3_tx_complete(struct nfp_net_tx_ring *tx_ring, int budget)
418 {
419 	struct nfp_net_r_vector *r_vec = tx_ring->r_vec;
420 	struct nfp_net_dp *dp = &r_vec->nfp_net->dp;
421 	u32 done_pkts = 0, done_bytes = 0;
422 	struct netdev_queue *nd_q;
423 	u32 qcp_rd_p;
424 	int todo;
425 
426 	if (tx_ring->wr_p == tx_ring->rd_p)
427 		return;
428 
429 	/* Work out how many descriptors have been transmitted */
430 	qcp_rd_p = nfp_net_read_tx_cmpl(tx_ring, dp);
431 
432 	if (qcp_rd_p == tx_ring->qcp_rd_p)
433 		return;
434 
435 	todo = D_IDX(tx_ring, qcp_rd_p - tx_ring->qcp_rd_p);
436 
437 	while (todo--) {
438 		const skb_frag_t *frag;
439 		struct nfp_nfd3_tx_buf *tx_buf;
440 		struct sk_buff *skb;
441 		int fidx, nr_frags;
442 		int idx;
443 
444 		idx = D_IDX(tx_ring, tx_ring->rd_p++);
445 		tx_buf = &tx_ring->txbufs[idx];
446 
447 		skb = tx_buf->skb;
448 		if (!skb)
449 			continue;
450 
451 		nr_frags = skb_shinfo(skb)->nr_frags;
452 		fidx = tx_buf->fidx;
453 
454 		if (fidx == -1) {
455 			/* unmap head */
456 			dma_unmap_single(dp->dev, tx_buf->dma_addr,
457 					 skb_headlen(skb), DMA_TO_DEVICE);
458 
459 			done_pkts += tx_buf->pkt_cnt;
460 			done_bytes += tx_buf->real_len;
461 		} else {
462 			/* unmap fragment */
463 			frag = &skb_shinfo(skb)->frags[fidx];
464 			dma_unmap_page(dp->dev, tx_buf->dma_addr,
465 				       skb_frag_size(frag), DMA_TO_DEVICE);
466 		}
467 
468 		/* check for last gather fragment */
469 		if (fidx == nr_frags - 1)
470 			napi_consume_skb(skb, budget);
471 
472 		tx_buf->dma_addr = 0;
473 		tx_buf->skb = NULL;
474 		tx_buf->fidx = -2;
475 	}
476 
477 	tx_ring->qcp_rd_p = qcp_rd_p;
478 
479 	u64_stats_update_begin(&r_vec->tx_sync);
480 	r_vec->tx_bytes += done_bytes;
481 	r_vec->tx_pkts += done_pkts;
482 	u64_stats_update_end(&r_vec->tx_sync);
483 
484 	if (!dp->netdev)
485 		return;
486 
487 	nd_q = netdev_get_tx_queue(dp->netdev, tx_ring->idx);
488 	netdev_tx_completed_queue(nd_q, done_pkts, done_bytes);
489 	if (nfp_nfd3_tx_ring_should_wake(tx_ring)) {
490 		/* Make sure TX thread will see updated tx_ring->rd_p */
491 		smp_mb();
492 
493 		if (unlikely(netif_tx_queue_stopped(nd_q)))
494 			netif_tx_wake_queue(nd_q);
495 	}
496 
497 	WARN_ONCE(tx_ring->wr_p - tx_ring->rd_p > tx_ring->cnt,
498 		  "TX ring corruption rd_p=%u wr_p=%u cnt=%u\n",
499 		  tx_ring->rd_p, tx_ring->wr_p, tx_ring->cnt);
500 }
501 
502 static bool nfp_nfd3_xdp_complete(struct nfp_net_tx_ring *tx_ring)
503 {
504 	struct nfp_net_r_vector *r_vec = tx_ring->r_vec;
505 	struct nfp_net_dp *dp = &r_vec->nfp_net->dp;
506 	u32 done_pkts = 0, done_bytes = 0;
507 	bool done_all;
508 	int idx, todo;
509 	u32 qcp_rd_p;
510 
511 	/* Work out how many descriptors have been transmitted */
512 	qcp_rd_p = nfp_net_read_tx_cmpl(tx_ring, dp);
513 
514 	if (qcp_rd_p == tx_ring->qcp_rd_p)
515 		return true;
516 
517 	todo = D_IDX(tx_ring, qcp_rd_p - tx_ring->qcp_rd_p);
518 
519 	done_all = todo <= NFP_NET_XDP_MAX_COMPLETE;
520 	todo = min(todo, NFP_NET_XDP_MAX_COMPLETE);
521 
522 	tx_ring->qcp_rd_p = D_IDX(tx_ring, tx_ring->qcp_rd_p + todo);
523 
524 	done_pkts = todo;
525 	while (todo--) {
526 		idx = D_IDX(tx_ring, tx_ring->rd_p);
527 		tx_ring->rd_p++;
528 
529 		done_bytes += tx_ring->txbufs[idx].real_len;
530 	}
531 
532 	u64_stats_update_begin(&r_vec->tx_sync);
533 	r_vec->tx_bytes += done_bytes;
534 	r_vec->tx_pkts += done_pkts;
535 	u64_stats_update_end(&r_vec->tx_sync);
536 
537 	WARN_ONCE(tx_ring->wr_p - tx_ring->rd_p > tx_ring->cnt,
538 		  "XDP TX ring corruption rd_p=%u wr_p=%u cnt=%u\n",
539 		  tx_ring->rd_p, tx_ring->wr_p, tx_ring->cnt);
540 
541 	return done_all;
542 }
543 
544 /* Receive processing
545  */
546 
547 static void *
548 nfp_nfd3_napi_alloc_one(struct nfp_net_dp *dp, dma_addr_t *dma_addr)
549 {
550 	void *frag;
551 
552 	if (!dp->xdp_prog) {
553 		frag = napi_alloc_frag(dp->fl_bufsz);
554 		if (unlikely(!frag))
555 			return NULL;
556 	} else {
557 		struct page *page;
558 
559 		page = dev_alloc_page();
560 		if (unlikely(!page))
561 			return NULL;
562 		frag = page_address(page);
563 	}
564 
565 	*dma_addr = nfp_net_dma_map_rx(dp, frag);
566 	if (dma_mapping_error(dp->dev, *dma_addr)) {
567 		nfp_net_free_frag(frag, dp->xdp_prog);
568 		nn_dp_warn(dp, "Failed to map DMA RX buffer\n");
569 		return NULL;
570 	}
571 
572 	return frag;
573 }
574 
575 /**
576  * nfp_nfd3_rx_give_one() - Put mapped skb on the software and hardware rings
577  * @dp:		NFP Net data path struct
578  * @rx_ring:	RX ring structure
579  * @frag:	page fragment buffer
580  * @dma_addr:	DMA address of skb mapping
581  */
582 static void
583 nfp_nfd3_rx_give_one(const struct nfp_net_dp *dp,
584 		     struct nfp_net_rx_ring *rx_ring,
585 		     void *frag, dma_addr_t dma_addr)
586 {
587 	unsigned int wr_idx;
588 
589 	wr_idx = D_IDX(rx_ring, rx_ring->wr_p);
590 
591 	nfp_net_dma_sync_dev_rx(dp, dma_addr);
592 
593 	/* Stash SKB and DMA address away */
594 	rx_ring->rxbufs[wr_idx].frag = frag;
595 	rx_ring->rxbufs[wr_idx].dma_addr = dma_addr;
596 
597 	/* Fill freelist descriptor */
598 	rx_ring->rxds[wr_idx].fld.reserved = 0;
599 	rx_ring->rxds[wr_idx].fld.meta_len_dd = 0;
600 	/* DMA address is expanded to 48-bit width in freelist for NFP3800,
601 	 * so the *_48b macro is used accordingly, it's also OK to fill
602 	 * a 40-bit address since the top 8 bits are get set to 0.
603 	 */
604 	nfp_desc_set_dma_addr_48b(&rx_ring->rxds[wr_idx].fld,
605 				  dma_addr + dp->rx_dma_off);
606 
607 	rx_ring->wr_p++;
608 	if (!(rx_ring->wr_p % NFP_NET_FL_BATCH)) {
609 		/* Update write pointer of the freelist queue. Make
610 		 * sure all writes are flushed before telling the hardware.
611 		 */
612 		wmb();
613 		nfp_qcp_wr_ptr_add(rx_ring->qcp_fl, NFP_NET_FL_BATCH);
614 	}
615 }
616 
617 /**
618  * nfp_nfd3_rx_ring_fill_freelist() - Give buffers from the ring to FW
619  * @dp:	     NFP Net data path struct
620  * @rx_ring: RX ring to fill
621  */
622 void nfp_nfd3_rx_ring_fill_freelist(struct nfp_net_dp *dp,
623 				    struct nfp_net_rx_ring *rx_ring)
624 {
625 	unsigned int i;
626 
627 	if (nfp_net_has_xsk_pool_slow(dp, rx_ring->idx))
628 		return nfp_net_xsk_rx_ring_fill_freelist(rx_ring);
629 
630 	for (i = 0; i < rx_ring->cnt - 1; i++)
631 		nfp_nfd3_rx_give_one(dp, rx_ring, rx_ring->rxbufs[i].frag,
632 				     rx_ring->rxbufs[i].dma_addr);
633 }
634 
635 /**
636  * nfp_nfd3_rx_csum_has_errors() - group check if rxd has any csum errors
637  * @flags: RX descriptor flags field in CPU byte order
638  */
639 static int nfp_nfd3_rx_csum_has_errors(u16 flags)
640 {
641 	u16 csum_all_checked, csum_all_ok;
642 
643 	csum_all_checked = flags & __PCIE_DESC_RX_CSUM_ALL;
644 	csum_all_ok = flags & __PCIE_DESC_RX_CSUM_ALL_OK;
645 
646 	return csum_all_checked != (csum_all_ok << PCIE_DESC_RX_CSUM_OK_SHIFT);
647 }
648 
649 /**
650  * nfp_nfd3_rx_csum() - set SKB checksum field based on RX descriptor flags
651  * @dp:  NFP Net data path struct
652  * @r_vec: per-ring structure
653  * @rxd: Pointer to RX descriptor
654  * @meta: Parsed metadata prepend
655  * @skb: Pointer to SKB
656  */
657 void
658 nfp_nfd3_rx_csum(const struct nfp_net_dp *dp, struct nfp_net_r_vector *r_vec,
659 		 const struct nfp_net_rx_desc *rxd,
660 		 const struct nfp_meta_parsed *meta, struct sk_buff *skb)
661 {
662 	skb_checksum_none_assert(skb);
663 
664 	if (!(dp->netdev->features & NETIF_F_RXCSUM))
665 		return;
666 
667 	if (meta->csum_type) {
668 		skb->ip_summed = meta->csum_type;
669 		skb->csum = meta->csum;
670 		u64_stats_update_begin(&r_vec->rx_sync);
671 		r_vec->hw_csum_rx_complete++;
672 		u64_stats_update_end(&r_vec->rx_sync);
673 		return;
674 	}
675 
676 	if (nfp_nfd3_rx_csum_has_errors(le16_to_cpu(rxd->rxd.flags))) {
677 		u64_stats_update_begin(&r_vec->rx_sync);
678 		r_vec->hw_csum_rx_error++;
679 		u64_stats_update_end(&r_vec->rx_sync);
680 		return;
681 	}
682 
683 	/* Assume that the firmware will never report inner CSUM_OK unless outer
684 	 * L4 headers were successfully parsed. FW will always report zero UDP
685 	 * checksum as CSUM_OK.
686 	 */
687 	if (rxd->rxd.flags & PCIE_DESC_RX_TCP_CSUM_OK ||
688 	    rxd->rxd.flags & PCIE_DESC_RX_UDP_CSUM_OK) {
689 		__skb_incr_checksum_unnecessary(skb);
690 		u64_stats_update_begin(&r_vec->rx_sync);
691 		r_vec->hw_csum_rx_ok++;
692 		u64_stats_update_end(&r_vec->rx_sync);
693 	}
694 
695 	if (rxd->rxd.flags & PCIE_DESC_RX_I_TCP_CSUM_OK ||
696 	    rxd->rxd.flags & PCIE_DESC_RX_I_UDP_CSUM_OK) {
697 		__skb_incr_checksum_unnecessary(skb);
698 		u64_stats_update_begin(&r_vec->rx_sync);
699 		r_vec->hw_csum_rx_inner_ok++;
700 		u64_stats_update_end(&r_vec->rx_sync);
701 	}
702 }
703 
704 static void
705 nfp_nfd3_set_hash(struct net_device *netdev, struct nfp_meta_parsed *meta,
706 		  unsigned int type, __be32 *hash)
707 {
708 	if (!(netdev->features & NETIF_F_RXHASH))
709 		return;
710 
711 	switch (type) {
712 	case NFP_NET_RSS_IPV4:
713 	case NFP_NET_RSS_IPV6:
714 	case NFP_NET_RSS_IPV6_EX:
715 		meta->hash_type = PKT_HASH_TYPE_L3;
716 		break;
717 	default:
718 		meta->hash_type = PKT_HASH_TYPE_L4;
719 		break;
720 	}
721 
722 	meta->hash = get_unaligned_be32(hash);
723 }
724 
725 static void
726 nfp_nfd3_set_hash_desc(struct net_device *netdev, struct nfp_meta_parsed *meta,
727 		       void *data, struct nfp_net_rx_desc *rxd)
728 {
729 	struct nfp_net_rx_hash *rx_hash = data;
730 
731 	if (!(rxd->rxd.flags & PCIE_DESC_RX_RSS))
732 		return;
733 
734 	nfp_nfd3_set_hash(netdev, meta, get_unaligned_be32(&rx_hash->hash_type),
735 			  &rx_hash->hash);
736 }
737 
738 bool
739 nfp_nfd3_parse_meta(struct net_device *netdev, struct nfp_meta_parsed *meta,
740 		    void *data, void *pkt, unsigned int pkt_len, int meta_len)
741 {
742 	u32 meta_info, vlan_info;
743 
744 	meta_info = get_unaligned_be32(data);
745 	data += 4;
746 
747 	while (meta_info) {
748 		switch (meta_info & NFP_NET_META_FIELD_MASK) {
749 		case NFP_NET_META_HASH:
750 			meta_info >>= NFP_NET_META_FIELD_SIZE;
751 			nfp_nfd3_set_hash(netdev, meta,
752 					  meta_info & NFP_NET_META_FIELD_MASK,
753 					  (__be32 *)data);
754 			data += 4;
755 			break;
756 		case NFP_NET_META_MARK:
757 			meta->mark = get_unaligned_be32(data);
758 			data += 4;
759 			break;
760 		case NFP_NET_META_VLAN:
761 			vlan_info = get_unaligned_be32(data);
762 			if (FIELD_GET(NFP_NET_META_VLAN_STRIP, vlan_info)) {
763 				meta->vlan.stripped = true;
764 				meta->vlan.tpid = FIELD_GET(NFP_NET_META_VLAN_TPID_MASK,
765 							    vlan_info);
766 				meta->vlan.tci = FIELD_GET(NFP_NET_META_VLAN_TCI_MASK,
767 							   vlan_info);
768 			}
769 			data += 4;
770 			break;
771 		case NFP_NET_META_PORTID:
772 			meta->portid = get_unaligned_be32(data);
773 			data += 4;
774 			break;
775 		case NFP_NET_META_CSUM:
776 			meta->csum_type = CHECKSUM_COMPLETE;
777 			meta->csum =
778 				(__force __wsum)__get_unaligned_cpu32(data);
779 			data += 4;
780 			break;
781 		case NFP_NET_META_RESYNC_INFO:
782 			if (nfp_net_tls_rx_resync_req(netdev, data, pkt,
783 						      pkt_len))
784 				return false;
785 			data += sizeof(struct nfp_net_tls_resync_req);
786 			break;
787 #ifdef CONFIG_NFP_NET_IPSEC
788 		case NFP_NET_META_IPSEC:
789 			/* Note: IPsec packet will have zero saidx, so need add 1
790 			 * to indicate packet is IPsec packet within driver.
791 			 */
792 			meta->ipsec_saidx = get_unaligned_be32(data) + 1;
793 			data += 4;
794 			break;
795 #endif
796 		default:
797 			return true;
798 		}
799 
800 		meta_info >>= NFP_NET_META_FIELD_SIZE;
801 	}
802 
803 	return data != pkt;
804 }
805 
806 static void
807 nfp_nfd3_rx_drop(const struct nfp_net_dp *dp, struct nfp_net_r_vector *r_vec,
808 		 struct nfp_net_rx_ring *rx_ring, struct nfp_net_rx_buf *rxbuf,
809 		 struct sk_buff *skb)
810 {
811 	u64_stats_update_begin(&r_vec->rx_sync);
812 	r_vec->rx_drops++;
813 	/* If we have both skb and rxbuf the replacement buffer allocation
814 	 * must have failed, count this as an alloc failure.
815 	 */
816 	if (skb && rxbuf)
817 		r_vec->rx_replace_buf_alloc_fail++;
818 	u64_stats_update_end(&r_vec->rx_sync);
819 
820 	/* skb is build based on the frag, free_skb() would free the frag
821 	 * so to be able to reuse it we need an extra ref.
822 	 */
823 	if (skb && rxbuf && skb->head == rxbuf->frag)
824 		page_ref_inc(virt_to_head_page(rxbuf->frag));
825 	if (rxbuf)
826 		nfp_nfd3_rx_give_one(dp, rx_ring, rxbuf->frag, rxbuf->dma_addr);
827 	if (skb)
828 		dev_kfree_skb_any(skb);
829 }
830 
831 static bool
832 nfp_nfd3_tx_xdp_buf(struct nfp_net_dp *dp, struct nfp_net_rx_ring *rx_ring,
833 		    struct nfp_net_tx_ring *tx_ring,
834 		    struct nfp_net_rx_buf *rxbuf, unsigned int dma_off,
835 		    unsigned int pkt_len, bool *completed)
836 {
837 	unsigned int dma_map_sz = dp->fl_bufsz - NFP_NET_RX_BUF_NON_DATA;
838 	struct nfp_nfd3_tx_buf *txbuf;
839 	struct nfp_nfd3_tx_desc *txd;
840 	int wr_idx;
841 
842 	/* Reject if xdp_adjust_tail grow packet beyond DMA area */
843 	if (pkt_len + dma_off > dma_map_sz)
844 		return false;
845 
846 	if (unlikely(nfp_net_tx_full(tx_ring, 1))) {
847 		if (!*completed) {
848 			nfp_nfd3_xdp_complete(tx_ring);
849 			*completed = true;
850 		}
851 
852 		if (unlikely(nfp_net_tx_full(tx_ring, 1))) {
853 			nfp_nfd3_rx_drop(dp, rx_ring->r_vec, rx_ring, rxbuf,
854 					 NULL);
855 			return false;
856 		}
857 	}
858 
859 	wr_idx = D_IDX(tx_ring, tx_ring->wr_p);
860 
861 	/* Stash the soft descriptor of the head then initialize it */
862 	txbuf = &tx_ring->txbufs[wr_idx];
863 
864 	nfp_nfd3_rx_give_one(dp, rx_ring, txbuf->frag, txbuf->dma_addr);
865 
866 	txbuf->frag = rxbuf->frag;
867 	txbuf->dma_addr = rxbuf->dma_addr;
868 	txbuf->fidx = -1;
869 	txbuf->pkt_cnt = 1;
870 	txbuf->real_len = pkt_len;
871 
872 	dma_sync_single_for_device(dp->dev, rxbuf->dma_addr + dma_off,
873 				   pkt_len, DMA_BIDIRECTIONAL);
874 
875 	/* Build TX descriptor */
876 	txd = &tx_ring->txds[wr_idx];
877 	txd->offset_eop = NFD3_DESC_TX_EOP;
878 	txd->dma_len = cpu_to_le16(pkt_len);
879 	nfp_desc_set_dma_addr_40b(txd, rxbuf->dma_addr + dma_off);
880 	txd->data_len = cpu_to_le16(pkt_len);
881 
882 	txd->flags = 0;
883 	txd->mss = 0;
884 	txd->lso_hdrlen = 0;
885 
886 	tx_ring->wr_p++;
887 	tx_ring->wr_ptr_add++;
888 	return true;
889 }
890 
891 /**
892  * nfp_nfd3_rx() - receive up to @budget packets on @rx_ring
893  * @rx_ring:   RX ring to receive from
894  * @budget:    NAPI budget
895  *
896  * Note, this function is separated out from the napi poll function to
897  * more cleanly separate packet receive code from other bookkeeping
898  * functions performed in the napi poll function.
899  *
900  * Return: Number of packets received.
901  */
902 static int nfp_nfd3_rx(struct nfp_net_rx_ring *rx_ring, int budget)
903 {
904 	struct nfp_net_r_vector *r_vec = rx_ring->r_vec;
905 	struct nfp_net_dp *dp = &r_vec->nfp_net->dp;
906 	struct nfp_net_tx_ring *tx_ring;
907 	struct bpf_prog *xdp_prog;
908 	int idx, pkts_polled = 0;
909 	bool xdp_tx_cmpl = false;
910 	unsigned int true_bufsz;
911 	struct sk_buff *skb;
912 	struct xdp_buff xdp;
913 
914 	xdp_prog = READ_ONCE(dp->xdp_prog);
915 	true_bufsz = xdp_prog ? PAGE_SIZE : dp->fl_bufsz;
916 	xdp_init_buff(&xdp, PAGE_SIZE - NFP_NET_RX_BUF_HEADROOM,
917 		      &rx_ring->xdp_rxq);
918 	tx_ring = r_vec->xdp_ring;
919 
920 	while (pkts_polled < budget) {
921 		unsigned int meta_len, data_len, meta_off, pkt_len, pkt_off;
922 		struct nfp_net_rx_buf *rxbuf;
923 		struct nfp_net_rx_desc *rxd;
924 		struct nfp_meta_parsed meta;
925 		bool redir_egress = false;
926 		struct net_device *netdev;
927 		dma_addr_t new_dma_addr;
928 		u32 meta_len_xdp = 0;
929 		void *new_frag;
930 
931 		idx = D_IDX(rx_ring, rx_ring->rd_p);
932 
933 		rxd = &rx_ring->rxds[idx];
934 		if (!(rxd->rxd.meta_len_dd & PCIE_DESC_RX_DD))
935 			break;
936 
937 		/* Memory barrier to ensure that we won't do other reads
938 		 * before the DD bit.
939 		 */
940 		dma_rmb();
941 
942 		memset(&meta, 0, sizeof(meta));
943 
944 		rx_ring->rd_p++;
945 		pkts_polled++;
946 
947 		rxbuf =	&rx_ring->rxbufs[idx];
948 		/*         < meta_len >
949 		 *  <-- [rx_offset] -->
950 		 *  ---------------------------------------------------------
951 		 * | [XX] |  metadata  |             packet           | XXXX |
952 		 *  ---------------------------------------------------------
953 		 *         <---------------- data_len --------------->
954 		 *
955 		 * The rx_offset is fixed for all packets, the meta_len can vary
956 		 * on a packet by packet basis. If rx_offset is set to zero
957 		 * (_RX_OFFSET_DYNAMIC) metadata starts at the beginning of the
958 		 * buffer and is immediately followed by the packet (no [XX]).
959 		 */
960 		meta_len = rxd->rxd.meta_len_dd & PCIE_DESC_RX_META_LEN_MASK;
961 		data_len = le16_to_cpu(rxd->rxd.data_len);
962 		pkt_len = data_len - meta_len;
963 
964 		pkt_off = NFP_NET_RX_BUF_HEADROOM + dp->rx_dma_off;
965 		if (dp->rx_offset == NFP_NET_CFG_RX_OFFSET_DYNAMIC)
966 			pkt_off += meta_len;
967 		else
968 			pkt_off += dp->rx_offset;
969 		meta_off = pkt_off - meta_len;
970 
971 		/* Stats update */
972 		u64_stats_update_begin(&r_vec->rx_sync);
973 		r_vec->rx_pkts++;
974 		r_vec->rx_bytes += pkt_len;
975 		u64_stats_update_end(&r_vec->rx_sync);
976 
977 		if (unlikely(meta_len > NFP_NET_MAX_PREPEND ||
978 			     (dp->rx_offset && meta_len > dp->rx_offset))) {
979 			nn_dp_warn(dp, "oversized RX packet metadata %u\n",
980 				   meta_len);
981 			nfp_nfd3_rx_drop(dp, r_vec, rx_ring, rxbuf, NULL);
982 			continue;
983 		}
984 
985 		nfp_net_dma_sync_cpu_rx(dp, rxbuf->dma_addr + meta_off,
986 					data_len);
987 
988 		if (!dp->chained_metadata_format) {
989 			nfp_nfd3_set_hash_desc(dp->netdev, &meta,
990 					       rxbuf->frag + meta_off, rxd);
991 		} else if (meta_len) {
992 			if (unlikely(nfp_nfd3_parse_meta(dp->netdev, &meta,
993 							 rxbuf->frag + meta_off,
994 							 rxbuf->frag + pkt_off,
995 							 pkt_len, meta_len))) {
996 				nn_dp_warn(dp, "invalid RX packet metadata\n");
997 				nfp_nfd3_rx_drop(dp, r_vec, rx_ring, rxbuf,
998 						 NULL);
999 				continue;
1000 			}
1001 		}
1002 
1003 		if (xdp_prog && !meta.portid) {
1004 			void *orig_data = rxbuf->frag + pkt_off;
1005 			unsigned int dma_off;
1006 			int act;
1007 
1008 			xdp_prepare_buff(&xdp,
1009 					 rxbuf->frag + NFP_NET_RX_BUF_HEADROOM,
1010 					 pkt_off - NFP_NET_RX_BUF_HEADROOM,
1011 					 pkt_len, true);
1012 
1013 			act = bpf_prog_run_xdp(xdp_prog, &xdp);
1014 
1015 			pkt_len = xdp.data_end - xdp.data;
1016 			pkt_off += xdp.data - orig_data;
1017 
1018 			switch (act) {
1019 			case XDP_PASS:
1020 				meta_len_xdp = xdp.data - xdp.data_meta;
1021 				break;
1022 			case XDP_TX:
1023 				dma_off = pkt_off - NFP_NET_RX_BUF_HEADROOM;
1024 				if (unlikely(!nfp_nfd3_tx_xdp_buf(dp, rx_ring,
1025 								  tx_ring,
1026 								  rxbuf,
1027 								  dma_off,
1028 								  pkt_len,
1029 								  &xdp_tx_cmpl)))
1030 					trace_xdp_exception(dp->netdev,
1031 							    xdp_prog, act);
1032 				continue;
1033 			default:
1034 				bpf_warn_invalid_xdp_action(dp->netdev, xdp_prog, act);
1035 				fallthrough;
1036 			case XDP_ABORTED:
1037 				trace_xdp_exception(dp->netdev, xdp_prog, act);
1038 				fallthrough;
1039 			case XDP_DROP:
1040 				nfp_nfd3_rx_give_one(dp, rx_ring, rxbuf->frag,
1041 						     rxbuf->dma_addr);
1042 				continue;
1043 			}
1044 		}
1045 
1046 		if (likely(!meta.portid)) {
1047 			netdev = dp->netdev;
1048 		} else if (meta.portid == NFP_META_PORT_ID_CTRL) {
1049 			struct nfp_net *nn = netdev_priv(dp->netdev);
1050 
1051 			nfp_app_ctrl_rx_raw(nn->app, rxbuf->frag + pkt_off,
1052 					    pkt_len);
1053 			nfp_nfd3_rx_give_one(dp, rx_ring, rxbuf->frag,
1054 					     rxbuf->dma_addr);
1055 			continue;
1056 		} else {
1057 			struct nfp_net *nn;
1058 
1059 			nn = netdev_priv(dp->netdev);
1060 			netdev = nfp_app_dev_get(nn->app, meta.portid,
1061 						 &redir_egress);
1062 			if (unlikely(!netdev)) {
1063 				nfp_nfd3_rx_drop(dp, r_vec, rx_ring, rxbuf,
1064 						 NULL);
1065 				continue;
1066 			}
1067 
1068 			if (nfp_netdev_is_nfp_repr(netdev))
1069 				nfp_repr_inc_rx_stats(netdev, pkt_len);
1070 		}
1071 
1072 		skb = build_skb(rxbuf->frag, true_bufsz);
1073 		if (unlikely(!skb)) {
1074 			nfp_nfd3_rx_drop(dp, r_vec, rx_ring, rxbuf, NULL);
1075 			continue;
1076 		}
1077 		new_frag = nfp_nfd3_napi_alloc_one(dp, &new_dma_addr);
1078 		if (unlikely(!new_frag)) {
1079 			nfp_nfd3_rx_drop(dp, r_vec, rx_ring, rxbuf, skb);
1080 			continue;
1081 		}
1082 
1083 		nfp_net_dma_unmap_rx(dp, rxbuf->dma_addr);
1084 
1085 		nfp_nfd3_rx_give_one(dp, rx_ring, new_frag, new_dma_addr);
1086 
1087 		skb_reserve(skb, pkt_off);
1088 		skb_put(skb, pkt_len);
1089 
1090 		skb->mark = meta.mark;
1091 		skb_set_hash(skb, meta.hash, meta.hash_type);
1092 
1093 		skb_record_rx_queue(skb, rx_ring->idx);
1094 		skb->protocol = eth_type_trans(skb, netdev);
1095 
1096 		nfp_nfd3_rx_csum(dp, r_vec, rxd, &meta, skb);
1097 
1098 #ifdef CONFIG_TLS_DEVICE
1099 		if (rxd->rxd.flags & PCIE_DESC_RX_DECRYPTED) {
1100 			skb->decrypted = true;
1101 			u64_stats_update_begin(&r_vec->rx_sync);
1102 			r_vec->hw_tls_rx++;
1103 			u64_stats_update_end(&r_vec->rx_sync);
1104 		}
1105 #endif
1106 
1107 		if (unlikely(!nfp_net_vlan_strip(skb, rxd, &meta))) {
1108 			nfp_nfd3_rx_drop(dp, r_vec, rx_ring, NULL, skb);
1109 			continue;
1110 		}
1111 
1112 #ifdef CONFIG_NFP_NET_IPSEC
1113 		if (meta.ipsec_saidx != 0 && unlikely(nfp_net_ipsec_rx(&meta, skb))) {
1114 			nfp_nfd3_rx_drop(dp, r_vec, rx_ring, NULL, skb);
1115 			continue;
1116 		}
1117 #endif
1118 
1119 		if (meta_len_xdp)
1120 			skb_metadata_set(skb, meta_len_xdp);
1121 
1122 		if (likely(!redir_egress)) {
1123 			napi_gro_receive(&rx_ring->r_vec->napi, skb);
1124 		} else {
1125 			skb->dev = netdev;
1126 			skb_reset_network_header(skb);
1127 			__skb_push(skb, ETH_HLEN);
1128 			dev_queue_xmit(skb);
1129 		}
1130 	}
1131 
1132 	if (xdp_prog) {
1133 		if (tx_ring->wr_ptr_add)
1134 			nfp_net_tx_xmit_more_flush(tx_ring);
1135 		else if (unlikely(tx_ring->wr_p != tx_ring->rd_p) &&
1136 			 !xdp_tx_cmpl)
1137 			if (!nfp_nfd3_xdp_complete(tx_ring))
1138 				pkts_polled = budget;
1139 	}
1140 
1141 	return pkts_polled;
1142 }
1143 
1144 /**
1145  * nfp_nfd3_poll() - napi poll function
1146  * @napi:    NAPI structure
1147  * @budget:  NAPI budget
1148  *
1149  * Return: number of packets polled.
1150  */
1151 int nfp_nfd3_poll(struct napi_struct *napi, int budget)
1152 {
1153 	struct nfp_net_r_vector *r_vec =
1154 		container_of(napi, struct nfp_net_r_vector, napi);
1155 	unsigned int pkts_polled = 0;
1156 
1157 	if (r_vec->tx_ring)
1158 		nfp_nfd3_tx_complete(r_vec->tx_ring, budget);
1159 	if (r_vec->rx_ring)
1160 		pkts_polled = nfp_nfd3_rx(r_vec->rx_ring, budget);
1161 
1162 	if (pkts_polled < budget)
1163 		if (napi_complete_done(napi, pkts_polled))
1164 			nfp_net_irq_unmask(r_vec->nfp_net, r_vec->irq_entry);
1165 
1166 	if (r_vec->nfp_net->rx_coalesce_adapt_on && r_vec->rx_ring) {
1167 		struct dim_sample dim_sample = {};
1168 		unsigned int start;
1169 		u64 pkts, bytes;
1170 
1171 		do {
1172 			start = u64_stats_fetch_begin(&r_vec->rx_sync);
1173 			pkts = r_vec->rx_pkts;
1174 			bytes = r_vec->rx_bytes;
1175 		} while (u64_stats_fetch_retry(&r_vec->rx_sync, start));
1176 
1177 		dim_update_sample(r_vec->event_ctr, pkts, bytes, &dim_sample);
1178 		net_dim(&r_vec->rx_dim, dim_sample);
1179 	}
1180 
1181 	if (r_vec->nfp_net->tx_coalesce_adapt_on && r_vec->tx_ring) {
1182 		struct dim_sample dim_sample = {};
1183 		unsigned int start;
1184 		u64 pkts, bytes;
1185 
1186 		do {
1187 			start = u64_stats_fetch_begin(&r_vec->tx_sync);
1188 			pkts = r_vec->tx_pkts;
1189 			bytes = r_vec->tx_bytes;
1190 		} while (u64_stats_fetch_retry(&r_vec->tx_sync, start));
1191 
1192 		dim_update_sample(r_vec->event_ctr, pkts, bytes, &dim_sample);
1193 		net_dim(&r_vec->tx_dim, dim_sample);
1194 	}
1195 
1196 	return pkts_polled;
1197 }
1198 
1199 /* Control device data path
1200  */
1201 
1202 bool
1203 nfp_nfd3_ctrl_tx_one(struct nfp_net *nn, struct nfp_net_r_vector *r_vec,
1204 		     struct sk_buff *skb, bool old)
1205 {
1206 	unsigned int real_len = skb->len, meta_len = 0;
1207 	struct nfp_net_tx_ring *tx_ring;
1208 	struct nfp_nfd3_tx_buf *txbuf;
1209 	struct nfp_nfd3_tx_desc *txd;
1210 	struct nfp_net_dp *dp;
1211 	dma_addr_t dma_addr;
1212 	int wr_idx;
1213 
1214 	dp = &r_vec->nfp_net->dp;
1215 	tx_ring = r_vec->tx_ring;
1216 
1217 	if (WARN_ON_ONCE(skb_shinfo(skb)->nr_frags)) {
1218 		nn_dp_warn(dp, "Driver's CTRL TX does not implement gather\n");
1219 		goto err_free;
1220 	}
1221 
1222 	if (unlikely(nfp_net_tx_full(tx_ring, 1))) {
1223 		u64_stats_update_begin(&r_vec->tx_sync);
1224 		r_vec->tx_busy++;
1225 		u64_stats_update_end(&r_vec->tx_sync);
1226 		if (!old)
1227 			__skb_queue_tail(&r_vec->queue, skb);
1228 		else
1229 			__skb_queue_head(&r_vec->queue, skb);
1230 		return true;
1231 	}
1232 
1233 	if (nfp_app_ctrl_has_meta(nn->app)) {
1234 		if (unlikely(skb_headroom(skb) < 8)) {
1235 			nn_dp_warn(dp, "CTRL TX on skb without headroom\n");
1236 			goto err_free;
1237 		}
1238 		meta_len = 8;
1239 		put_unaligned_be32(NFP_META_PORT_ID_CTRL, skb_push(skb, 4));
1240 		put_unaligned_be32(NFP_NET_META_PORTID, skb_push(skb, 4));
1241 	}
1242 
1243 	/* Start with the head skbuf */
1244 	dma_addr = dma_map_single(dp->dev, skb->data, skb_headlen(skb),
1245 				  DMA_TO_DEVICE);
1246 	if (dma_mapping_error(dp->dev, dma_addr))
1247 		goto err_dma_warn;
1248 
1249 	wr_idx = D_IDX(tx_ring, tx_ring->wr_p);
1250 
1251 	/* Stash the soft descriptor of the head then initialize it */
1252 	txbuf = &tx_ring->txbufs[wr_idx];
1253 	txbuf->skb = skb;
1254 	txbuf->dma_addr = dma_addr;
1255 	txbuf->fidx = -1;
1256 	txbuf->pkt_cnt = 1;
1257 	txbuf->real_len = real_len;
1258 
1259 	/* Build TX descriptor */
1260 	txd = &tx_ring->txds[wr_idx];
1261 	txd->offset_eop = meta_len | NFD3_DESC_TX_EOP;
1262 	txd->dma_len = cpu_to_le16(skb_headlen(skb));
1263 	nfp_desc_set_dma_addr_40b(txd, dma_addr);
1264 	txd->data_len = cpu_to_le16(skb->len);
1265 
1266 	txd->flags = 0;
1267 	txd->mss = 0;
1268 	txd->lso_hdrlen = 0;
1269 
1270 	tx_ring->wr_p++;
1271 	tx_ring->wr_ptr_add++;
1272 	nfp_net_tx_xmit_more_flush(tx_ring);
1273 
1274 	return false;
1275 
1276 err_dma_warn:
1277 	nn_dp_warn(dp, "Failed to DMA map TX CTRL buffer\n");
1278 err_free:
1279 	u64_stats_update_begin(&r_vec->tx_sync);
1280 	r_vec->tx_errors++;
1281 	u64_stats_update_end(&r_vec->tx_sync);
1282 	dev_kfree_skb_any(skb);
1283 	return false;
1284 }
1285 
1286 static void __nfp_ctrl_tx_queued(struct nfp_net_r_vector *r_vec)
1287 {
1288 	struct sk_buff *skb;
1289 
1290 	while ((skb = __skb_dequeue(&r_vec->queue)))
1291 		if (nfp_nfd3_ctrl_tx_one(r_vec->nfp_net, r_vec, skb, true))
1292 			return;
1293 }
1294 
1295 static bool
1296 nfp_ctrl_meta_ok(struct nfp_net *nn, void *data, unsigned int meta_len)
1297 {
1298 	u32 meta_type, meta_tag;
1299 
1300 	if (!nfp_app_ctrl_has_meta(nn->app))
1301 		return !meta_len;
1302 
1303 	if (meta_len != 8)
1304 		return false;
1305 
1306 	meta_type = get_unaligned_be32(data);
1307 	meta_tag = get_unaligned_be32(data + 4);
1308 
1309 	return (meta_type == NFP_NET_META_PORTID &&
1310 		meta_tag == NFP_META_PORT_ID_CTRL);
1311 }
1312 
1313 static bool
1314 nfp_ctrl_rx_one(struct nfp_net *nn, struct nfp_net_dp *dp,
1315 		struct nfp_net_r_vector *r_vec, struct nfp_net_rx_ring *rx_ring)
1316 {
1317 	unsigned int meta_len, data_len, meta_off, pkt_len, pkt_off;
1318 	struct nfp_net_rx_buf *rxbuf;
1319 	struct nfp_net_rx_desc *rxd;
1320 	dma_addr_t new_dma_addr;
1321 	struct sk_buff *skb;
1322 	void *new_frag;
1323 	int idx;
1324 
1325 	idx = D_IDX(rx_ring, rx_ring->rd_p);
1326 
1327 	rxd = &rx_ring->rxds[idx];
1328 	if (!(rxd->rxd.meta_len_dd & PCIE_DESC_RX_DD))
1329 		return false;
1330 
1331 	/* Memory barrier to ensure that we won't do other reads
1332 	 * before the DD bit.
1333 	 */
1334 	dma_rmb();
1335 
1336 	rx_ring->rd_p++;
1337 
1338 	rxbuf =	&rx_ring->rxbufs[idx];
1339 	meta_len = rxd->rxd.meta_len_dd & PCIE_DESC_RX_META_LEN_MASK;
1340 	data_len = le16_to_cpu(rxd->rxd.data_len);
1341 	pkt_len = data_len - meta_len;
1342 
1343 	pkt_off = NFP_NET_RX_BUF_HEADROOM + dp->rx_dma_off;
1344 	if (dp->rx_offset == NFP_NET_CFG_RX_OFFSET_DYNAMIC)
1345 		pkt_off += meta_len;
1346 	else
1347 		pkt_off += dp->rx_offset;
1348 	meta_off = pkt_off - meta_len;
1349 
1350 	/* Stats update */
1351 	u64_stats_update_begin(&r_vec->rx_sync);
1352 	r_vec->rx_pkts++;
1353 	r_vec->rx_bytes += pkt_len;
1354 	u64_stats_update_end(&r_vec->rx_sync);
1355 
1356 	nfp_net_dma_sync_cpu_rx(dp, rxbuf->dma_addr + meta_off,	data_len);
1357 
1358 	if (unlikely(!nfp_ctrl_meta_ok(nn, rxbuf->frag + meta_off, meta_len))) {
1359 		nn_dp_warn(dp, "incorrect metadata for ctrl packet (%d)\n",
1360 			   meta_len);
1361 		nfp_nfd3_rx_drop(dp, r_vec, rx_ring, rxbuf, NULL);
1362 		return true;
1363 	}
1364 
1365 	skb = build_skb(rxbuf->frag, dp->fl_bufsz);
1366 	if (unlikely(!skb)) {
1367 		nfp_nfd3_rx_drop(dp, r_vec, rx_ring, rxbuf, NULL);
1368 		return true;
1369 	}
1370 	new_frag = nfp_nfd3_napi_alloc_one(dp, &new_dma_addr);
1371 	if (unlikely(!new_frag)) {
1372 		nfp_nfd3_rx_drop(dp, r_vec, rx_ring, rxbuf, skb);
1373 		return true;
1374 	}
1375 
1376 	nfp_net_dma_unmap_rx(dp, rxbuf->dma_addr);
1377 
1378 	nfp_nfd3_rx_give_one(dp, rx_ring, new_frag, new_dma_addr);
1379 
1380 	skb_reserve(skb, pkt_off);
1381 	skb_put(skb, pkt_len);
1382 
1383 	nfp_app_ctrl_rx(nn->app, skb);
1384 
1385 	return true;
1386 }
1387 
1388 static bool nfp_ctrl_rx(struct nfp_net_r_vector *r_vec)
1389 {
1390 	struct nfp_net_rx_ring *rx_ring = r_vec->rx_ring;
1391 	struct nfp_net *nn = r_vec->nfp_net;
1392 	struct nfp_net_dp *dp = &nn->dp;
1393 	unsigned int budget = 512;
1394 
1395 	while (nfp_ctrl_rx_one(nn, dp, r_vec, rx_ring) && budget--)
1396 		continue;
1397 
1398 	return budget;
1399 }
1400 
1401 void nfp_nfd3_ctrl_poll(struct tasklet_struct *t)
1402 {
1403 	struct nfp_net_r_vector *r_vec = from_tasklet(r_vec, t, tasklet);
1404 
1405 	spin_lock(&r_vec->lock);
1406 	nfp_nfd3_tx_complete(r_vec->tx_ring, 0);
1407 	__nfp_ctrl_tx_queued(r_vec);
1408 	spin_unlock(&r_vec->lock);
1409 
1410 	if (nfp_ctrl_rx(r_vec)) {
1411 		nfp_net_irq_unmask(r_vec->nfp_net, r_vec->irq_entry);
1412 	} else {
1413 		tasklet_schedule(&r_vec->tasklet);
1414 		nn_dp_warn(&r_vec->nfp_net->dp,
1415 			   "control message budget exceeded!\n");
1416 	}
1417 }
1418