162306a36Sopenharmony_ci// SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)
262306a36Sopenharmony_ci/* Copyright (C) 2015-2019 Netronome Systems, Inc. */
362306a36Sopenharmony_ci
462306a36Sopenharmony_ci#include <linux/seq_file.h>
562306a36Sopenharmony_ci
662306a36Sopenharmony_ci#include "../nfp_net.h"
762306a36Sopenharmony_ci#include "../nfp_net_dp.h"
862306a36Sopenharmony_ci#include "../nfp_net_xsk.h"
962306a36Sopenharmony_ci#include "nfd3.h"
1062306a36Sopenharmony_ci
1162306a36Sopenharmony_cistatic void nfp_nfd3_xsk_tx_bufs_free(struct nfp_net_tx_ring *tx_ring)
1262306a36Sopenharmony_ci{
1362306a36Sopenharmony_ci	struct nfp_nfd3_tx_buf *txbuf;
1462306a36Sopenharmony_ci	unsigned int idx;
1562306a36Sopenharmony_ci
1662306a36Sopenharmony_ci	while (tx_ring->rd_p != tx_ring->wr_p) {
1762306a36Sopenharmony_ci		idx = D_IDX(tx_ring, tx_ring->rd_p);
1862306a36Sopenharmony_ci		txbuf = &tx_ring->txbufs[idx];
1962306a36Sopenharmony_ci
2062306a36Sopenharmony_ci		txbuf->real_len = 0;
2162306a36Sopenharmony_ci
2262306a36Sopenharmony_ci		tx_ring->qcp_rd_p++;
2362306a36Sopenharmony_ci		tx_ring->rd_p++;
2462306a36Sopenharmony_ci
2562306a36Sopenharmony_ci		if (tx_ring->r_vec->xsk_pool) {
2662306a36Sopenharmony_ci			if (txbuf->is_xsk_tx)
2762306a36Sopenharmony_ci				nfp_nfd3_xsk_tx_free(txbuf);
2862306a36Sopenharmony_ci
2962306a36Sopenharmony_ci			xsk_tx_completed(tx_ring->r_vec->xsk_pool, 1);
3062306a36Sopenharmony_ci		}
3162306a36Sopenharmony_ci	}
3262306a36Sopenharmony_ci}
3362306a36Sopenharmony_ci
3462306a36Sopenharmony_ci/**
3562306a36Sopenharmony_ci * nfp_nfd3_tx_ring_reset() - Free any untransmitted buffers and reset pointers
3662306a36Sopenharmony_ci * @dp:		NFP Net data path struct
3762306a36Sopenharmony_ci * @tx_ring:	TX ring structure
3862306a36Sopenharmony_ci *
3962306a36Sopenharmony_ci * Assumes that the device is stopped, must be idempotent.
4062306a36Sopenharmony_ci */
4162306a36Sopenharmony_cistatic void
4262306a36Sopenharmony_cinfp_nfd3_tx_ring_reset(struct nfp_net_dp *dp, struct nfp_net_tx_ring *tx_ring)
4362306a36Sopenharmony_ci{
4462306a36Sopenharmony_ci	struct netdev_queue *nd_q;
4562306a36Sopenharmony_ci	const skb_frag_t *frag;
4662306a36Sopenharmony_ci
4762306a36Sopenharmony_ci	while (!tx_ring->is_xdp && tx_ring->rd_p != tx_ring->wr_p) {
4862306a36Sopenharmony_ci		struct nfp_nfd3_tx_buf *tx_buf;
4962306a36Sopenharmony_ci		struct sk_buff *skb;
5062306a36Sopenharmony_ci		int idx, nr_frags;
5162306a36Sopenharmony_ci
5262306a36Sopenharmony_ci		idx = D_IDX(tx_ring, tx_ring->rd_p);
5362306a36Sopenharmony_ci		tx_buf = &tx_ring->txbufs[idx];
5462306a36Sopenharmony_ci
5562306a36Sopenharmony_ci		skb = tx_ring->txbufs[idx].skb;
5662306a36Sopenharmony_ci		nr_frags = skb_shinfo(skb)->nr_frags;
5762306a36Sopenharmony_ci
5862306a36Sopenharmony_ci		if (tx_buf->fidx == -1) {
5962306a36Sopenharmony_ci			/* unmap head */
6062306a36Sopenharmony_ci			dma_unmap_single(dp->dev, tx_buf->dma_addr,
6162306a36Sopenharmony_ci					 skb_headlen(skb), DMA_TO_DEVICE);
6262306a36Sopenharmony_ci		} else {
6362306a36Sopenharmony_ci			/* unmap fragment */
6462306a36Sopenharmony_ci			frag = &skb_shinfo(skb)->frags[tx_buf->fidx];
6562306a36Sopenharmony_ci			dma_unmap_page(dp->dev, tx_buf->dma_addr,
6662306a36Sopenharmony_ci				       skb_frag_size(frag), DMA_TO_DEVICE);
6762306a36Sopenharmony_ci		}
6862306a36Sopenharmony_ci
6962306a36Sopenharmony_ci		/* check for last gather fragment */
7062306a36Sopenharmony_ci		if (tx_buf->fidx == nr_frags - 1)
7162306a36Sopenharmony_ci			dev_kfree_skb_any(skb);
7262306a36Sopenharmony_ci
7362306a36Sopenharmony_ci		tx_buf->dma_addr = 0;
7462306a36Sopenharmony_ci		tx_buf->skb = NULL;
7562306a36Sopenharmony_ci		tx_buf->fidx = -2;
7662306a36Sopenharmony_ci
7762306a36Sopenharmony_ci		tx_ring->qcp_rd_p++;
7862306a36Sopenharmony_ci		tx_ring->rd_p++;
7962306a36Sopenharmony_ci	}
8062306a36Sopenharmony_ci
8162306a36Sopenharmony_ci	if (tx_ring->is_xdp)
8262306a36Sopenharmony_ci		nfp_nfd3_xsk_tx_bufs_free(tx_ring);
8362306a36Sopenharmony_ci
8462306a36Sopenharmony_ci	memset(tx_ring->txds, 0, tx_ring->size);
8562306a36Sopenharmony_ci	tx_ring->wr_p = 0;
8662306a36Sopenharmony_ci	tx_ring->rd_p = 0;
8762306a36Sopenharmony_ci	tx_ring->qcp_rd_p = 0;
8862306a36Sopenharmony_ci	tx_ring->wr_ptr_add = 0;
8962306a36Sopenharmony_ci
9062306a36Sopenharmony_ci	if (tx_ring->is_xdp || !dp->netdev)
9162306a36Sopenharmony_ci		return;
9262306a36Sopenharmony_ci
9362306a36Sopenharmony_ci	nd_q = netdev_get_tx_queue(dp->netdev, tx_ring->idx);
9462306a36Sopenharmony_ci	netdev_tx_reset_queue(nd_q);
9562306a36Sopenharmony_ci}
9662306a36Sopenharmony_ci
9762306a36Sopenharmony_ci/**
9862306a36Sopenharmony_ci * nfp_nfd3_tx_ring_free() - Free resources allocated to a TX ring
9962306a36Sopenharmony_ci * @tx_ring:   TX ring to free
10062306a36Sopenharmony_ci */
10162306a36Sopenharmony_cistatic void nfp_nfd3_tx_ring_free(struct nfp_net_tx_ring *tx_ring)
10262306a36Sopenharmony_ci{
10362306a36Sopenharmony_ci	struct nfp_net_r_vector *r_vec = tx_ring->r_vec;
10462306a36Sopenharmony_ci	struct nfp_net_dp *dp = &r_vec->nfp_net->dp;
10562306a36Sopenharmony_ci
10662306a36Sopenharmony_ci	kvfree(tx_ring->txbufs);
10762306a36Sopenharmony_ci
10862306a36Sopenharmony_ci	if (tx_ring->txds)
10962306a36Sopenharmony_ci		dma_free_coherent(dp->dev, tx_ring->size,
11062306a36Sopenharmony_ci				  tx_ring->txds, tx_ring->dma);
11162306a36Sopenharmony_ci
11262306a36Sopenharmony_ci	tx_ring->cnt = 0;
11362306a36Sopenharmony_ci	tx_ring->txbufs = NULL;
11462306a36Sopenharmony_ci	tx_ring->txds = NULL;
11562306a36Sopenharmony_ci	tx_ring->dma = 0;
11662306a36Sopenharmony_ci	tx_ring->size = 0;
11762306a36Sopenharmony_ci}
11862306a36Sopenharmony_ci
11962306a36Sopenharmony_ci/**
12062306a36Sopenharmony_ci * nfp_nfd3_tx_ring_alloc() - Allocate resource for a TX ring
12162306a36Sopenharmony_ci * @dp:        NFP Net data path struct
12262306a36Sopenharmony_ci * @tx_ring:   TX Ring structure to allocate
12362306a36Sopenharmony_ci *
12462306a36Sopenharmony_ci * Return: 0 on success, negative errno otherwise.
12562306a36Sopenharmony_ci */
12662306a36Sopenharmony_cistatic int
12762306a36Sopenharmony_cinfp_nfd3_tx_ring_alloc(struct nfp_net_dp *dp, struct nfp_net_tx_ring *tx_ring)
12862306a36Sopenharmony_ci{
12962306a36Sopenharmony_ci	struct nfp_net_r_vector *r_vec = tx_ring->r_vec;
13062306a36Sopenharmony_ci
13162306a36Sopenharmony_ci	tx_ring->cnt = dp->txd_cnt;
13262306a36Sopenharmony_ci
13362306a36Sopenharmony_ci	tx_ring->size = array_size(tx_ring->cnt, sizeof(*tx_ring->txds));
13462306a36Sopenharmony_ci	tx_ring->txds = dma_alloc_coherent(dp->dev, tx_ring->size,
13562306a36Sopenharmony_ci					   &tx_ring->dma,
13662306a36Sopenharmony_ci					   GFP_KERNEL | __GFP_NOWARN);
13762306a36Sopenharmony_ci	if (!tx_ring->txds) {
13862306a36Sopenharmony_ci		netdev_warn(dp->netdev, "failed to allocate TX descriptor ring memory, requested descriptor count: %d, consider lowering descriptor count\n",
13962306a36Sopenharmony_ci			    tx_ring->cnt);
14062306a36Sopenharmony_ci		goto err_alloc;
14162306a36Sopenharmony_ci	}
14262306a36Sopenharmony_ci
14362306a36Sopenharmony_ci	tx_ring->txbufs = kvcalloc(tx_ring->cnt, sizeof(*tx_ring->txbufs),
14462306a36Sopenharmony_ci				   GFP_KERNEL);
14562306a36Sopenharmony_ci	if (!tx_ring->txbufs)
14662306a36Sopenharmony_ci		goto err_alloc;
14762306a36Sopenharmony_ci
14862306a36Sopenharmony_ci	if (!tx_ring->is_xdp && dp->netdev)
14962306a36Sopenharmony_ci		netif_set_xps_queue(dp->netdev, &r_vec->affinity_mask,
15062306a36Sopenharmony_ci				    tx_ring->idx);
15162306a36Sopenharmony_ci
15262306a36Sopenharmony_ci	return 0;
15362306a36Sopenharmony_ci
15462306a36Sopenharmony_cierr_alloc:
15562306a36Sopenharmony_ci	nfp_nfd3_tx_ring_free(tx_ring);
15662306a36Sopenharmony_ci	return -ENOMEM;
15762306a36Sopenharmony_ci}
15862306a36Sopenharmony_ci
15962306a36Sopenharmony_cistatic void
16062306a36Sopenharmony_cinfp_nfd3_tx_ring_bufs_free(struct nfp_net_dp *dp,
16162306a36Sopenharmony_ci			   struct nfp_net_tx_ring *tx_ring)
16262306a36Sopenharmony_ci{
16362306a36Sopenharmony_ci	unsigned int i;
16462306a36Sopenharmony_ci
16562306a36Sopenharmony_ci	if (!tx_ring->is_xdp)
16662306a36Sopenharmony_ci		return;
16762306a36Sopenharmony_ci
16862306a36Sopenharmony_ci	for (i = 0; i < tx_ring->cnt; i++) {
16962306a36Sopenharmony_ci		if (!tx_ring->txbufs[i].frag)
17062306a36Sopenharmony_ci			return;
17162306a36Sopenharmony_ci
17262306a36Sopenharmony_ci		nfp_net_dma_unmap_rx(dp, tx_ring->txbufs[i].dma_addr);
17362306a36Sopenharmony_ci		__free_page(virt_to_page(tx_ring->txbufs[i].frag));
17462306a36Sopenharmony_ci	}
17562306a36Sopenharmony_ci}
17662306a36Sopenharmony_ci
17762306a36Sopenharmony_cistatic int
17862306a36Sopenharmony_cinfp_nfd3_tx_ring_bufs_alloc(struct nfp_net_dp *dp,
17962306a36Sopenharmony_ci			    struct nfp_net_tx_ring *tx_ring)
18062306a36Sopenharmony_ci{
18162306a36Sopenharmony_ci	struct nfp_nfd3_tx_buf *txbufs = tx_ring->txbufs;
18262306a36Sopenharmony_ci	unsigned int i;
18362306a36Sopenharmony_ci
18462306a36Sopenharmony_ci	if (!tx_ring->is_xdp)
18562306a36Sopenharmony_ci		return 0;
18662306a36Sopenharmony_ci
18762306a36Sopenharmony_ci	for (i = 0; i < tx_ring->cnt; i++) {
18862306a36Sopenharmony_ci		txbufs[i].frag = nfp_net_rx_alloc_one(dp, &txbufs[i].dma_addr);
18962306a36Sopenharmony_ci		if (!txbufs[i].frag) {
19062306a36Sopenharmony_ci			nfp_nfd3_tx_ring_bufs_free(dp, tx_ring);
19162306a36Sopenharmony_ci			return -ENOMEM;
19262306a36Sopenharmony_ci		}
19362306a36Sopenharmony_ci	}
19462306a36Sopenharmony_ci
19562306a36Sopenharmony_ci	return 0;
19662306a36Sopenharmony_ci}
19762306a36Sopenharmony_ci
19862306a36Sopenharmony_cistatic void
19962306a36Sopenharmony_cinfp_nfd3_print_tx_descs(struct seq_file *file,
20062306a36Sopenharmony_ci			struct nfp_net_r_vector *r_vec,
20162306a36Sopenharmony_ci			struct nfp_net_tx_ring *tx_ring,
20262306a36Sopenharmony_ci			u32 d_rd_p, u32 d_wr_p)
20362306a36Sopenharmony_ci{
20462306a36Sopenharmony_ci	struct nfp_nfd3_tx_desc *txd;
20562306a36Sopenharmony_ci	u32 txd_cnt = tx_ring->cnt;
20662306a36Sopenharmony_ci	int i;
20762306a36Sopenharmony_ci
20862306a36Sopenharmony_ci	for (i = 0; i < txd_cnt; i++) {
20962306a36Sopenharmony_ci		struct xdp_buff *xdp;
21062306a36Sopenharmony_ci		struct sk_buff *skb;
21162306a36Sopenharmony_ci
21262306a36Sopenharmony_ci		txd = &tx_ring->txds[i];
21362306a36Sopenharmony_ci		seq_printf(file, "%04d: 0x%08x 0x%08x 0x%08x 0x%08x", i,
21462306a36Sopenharmony_ci			   txd->vals[0], txd->vals[1],
21562306a36Sopenharmony_ci			   txd->vals[2], txd->vals[3]);
21662306a36Sopenharmony_ci
21762306a36Sopenharmony_ci		if (!tx_ring->is_xdp) {
21862306a36Sopenharmony_ci			skb = READ_ONCE(tx_ring->txbufs[i].skb);
21962306a36Sopenharmony_ci			if (skb)
22062306a36Sopenharmony_ci				seq_printf(file, " skb->head=%p skb->data=%p",
22162306a36Sopenharmony_ci					   skb->head, skb->data);
22262306a36Sopenharmony_ci		} else {
22362306a36Sopenharmony_ci			xdp = READ_ONCE(tx_ring->txbufs[i].xdp);
22462306a36Sopenharmony_ci			if (xdp)
22562306a36Sopenharmony_ci				seq_printf(file, " xdp->data=%p", xdp->data);
22662306a36Sopenharmony_ci		}
22762306a36Sopenharmony_ci
22862306a36Sopenharmony_ci		if (tx_ring->txbufs[i].dma_addr)
22962306a36Sopenharmony_ci			seq_printf(file, " dma_addr=%pad",
23062306a36Sopenharmony_ci				   &tx_ring->txbufs[i].dma_addr);
23162306a36Sopenharmony_ci
23262306a36Sopenharmony_ci		if (i == tx_ring->rd_p % txd_cnt)
23362306a36Sopenharmony_ci			seq_puts(file, " H_RD");
23462306a36Sopenharmony_ci		if (i == tx_ring->wr_p % txd_cnt)
23562306a36Sopenharmony_ci			seq_puts(file, " H_WR");
23662306a36Sopenharmony_ci		if (i == d_rd_p % txd_cnt)
23762306a36Sopenharmony_ci			seq_puts(file, " D_RD");
23862306a36Sopenharmony_ci		if (i == d_wr_p % txd_cnt)
23962306a36Sopenharmony_ci			seq_puts(file, " D_WR");
24062306a36Sopenharmony_ci
24162306a36Sopenharmony_ci		seq_putc(file, '\n');
24262306a36Sopenharmony_ci	}
24362306a36Sopenharmony_ci}
24462306a36Sopenharmony_ci
24562306a36Sopenharmony_ci#define NFP_NFD3_CFG_CTRL_SUPPORTED					\
24662306a36Sopenharmony_ci	(NFP_NET_CFG_CTRL_ENABLE | NFP_NET_CFG_CTRL_PROMISC |		\
24762306a36Sopenharmony_ci	 NFP_NET_CFG_CTRL_L2BC | NFP_NET_CFG_CTRL_L2MC |		\
24862306a36Sopenharmony_ci	 NFP_NET_CFG_CTRL_RXCSUM | NFP_NET_CFG_CTRL_TXCSUM |		\
24962306a36Sopenharmony_ci	 NFP_NET_CFG_CTRL_RXVLAN | NFP_NET_CFG_CTRL_TXVLAN |		\
25062306a36Sopenharmony_ci	 NFP_NET_CFG_CTRL_RXVLAN_V2 | NFP_NET_CFG_CTRL_RXQINQ |		\
25162306a36Sopenharmony_ci	 NFP_NET_CFG_CTRL_TXVLAN_V2 |					\
25262306a36Sopenharmony_ci	 NFP_NET_CFG_CTRL_GATHER | NFP_NET_CFG_CTRL_LSO |		\
25362306a36Sopenharmony_ci	 NFP_NET_CFG_CTRL_CTAG_FILTER | NFP_NET_CFG_CTRL_CMSG_DATA |	\
25462306a36Sopenharmony_ci	 NFP_NET_CFG_CTRL_RINGCFG | NFP_NET_CFG_CTRL_RSS |		\
25562306a36Sopenharmony_ci	 NFP_NET_CFG_CTRL_IRQMOD | NFP_NET_CFG_CTRL_TXRWB |		\
25662306a36Sopenharmony_ci	 NFP_NET_CFG_CTRL_VEPA |					\
25762306a36Sopenharmony_ci	 NFP_NET_CFG_CTRL_VXLAN | NFP_NET_CFG_CTRL_NVGRE |		\
25862306a36Sopenharmony_ci	 NFP_NET_CFG_CTRL_BPF | NFP_NET_CFG_CTRL_LSO2 |			\
25962306a36Sopenharmony_ci	 NFP_NET_CFG_CTRL_RSS2 | NFP_NET_CFG_CTRL_CSUM_COMPLETE |	\
26062306a36Sopenharmony_ci	 NFP_NET_CFG_CTRL_LIVE_ADDR)
26162306a36Sopenharmony_ci
26262306a36Sopenharmony_ciconst struct nfp_dp_ops nfp_nfd3_ops = {
26362306a36Sopenharmony_ci	.version		= NFP_NFD_VER_NFD3,
26462306a36Sopenharmony_ci	.tx_min_desc_per_pkt	= 1,
26562306a36Sopenharmony_ci	.cap_mask		= NFP_NFD3_CFG_CTRL_SUPPORTED,
26662306a36Sopenharmony_ci	.dma_mask		= DMA_BIT_MASK(40),
26762306a36Sopenharmony_ci	.poll			= nfp_nfd3_poll,
26862306a36Sopenharmony_ci	.xsk_poll		= nfp_nfd3_xsk_poll,
26962306a36Sopenharmony_ci	.ctrl_poll		= nfp_nfd3_ctrl_poll,
27062306a36Sopenharmony_ci	.xmit			= nfp_nfd3_tx,
27162306a36Sopenharmony_ci	.ctrl_tx_one		= nfp_nfd3_ctrl_tx_one,
27262306a36Sopenharmony_ci	.rx_ring_fill_freelist	= nfp_nfd3_rx_ring_fill_freelist,
27362306a36Sopenharmony_ci	.tx_ring_alloc		= nfp_nfd3_tx_ring_alloc,
27462306a36Sopenharmony_ci	.tx_ring_reset		= nfp_nfd3_tx_ring_reset,
27562306a36Sopenharmony_ci	.tx_ring_free		= nfp_nfd3_tx_ring_free,
27662306a36Sopenharmony_ci	.tx_ring_bufs_alloc	= nfp_nfd3_tx_ring_bufs_alloc,
27762306a36Sopenharmony_ci	.tx_ring_bufs_free	= nfp_nfd3_tx_ring_bufs_free,
27862306a36Sopenharmony_ci	.print_tx_descs		= nfp_nfd3_print_tx_descs
27962306a36Sopenharmony_ci};
280