cachepc-linux

Fork of AMDESE/linux with modifications for CachePC side-channel attack
git clone https://git.sinitax.com/sinitax/cachepc-linux
Log | Files | Refs | README | LICENSE | sfeed.txt

dp.c (35826B)


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