| 1 |
// SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause) |
| 2 |
/* Copyright (C) 2015-2019 Netronome Systems, Inc. */ |
| 3 |
|
| 4 |
#include <linux/seq_file.h> |
| 5 |
|
| 6 |
#include "../nfp_net.h" |
| 7 |
#include "../nfp_net_dp.h" |
| 8 |
#include "../nfp_net_xsk.h" |
| 9 |
#include "nfd3.h" |
| 10 |
|
| 11 |
static void nfp_nfd3_xsk_tx_bufs_free(struct nfp_net_tx_ring *tx_ring) |
| 12 |
{ |
| 13 |
struct nfp_nfd3_tx_buf *txbuf; |
| 14 |
unsigned int idx; |
| 15 |
|
| 16 |
while (tx_ring->rd_p != tx_ring->wr_p) { |
| 17 |
idx = D_IDX(tx_ring, tx_ring->rd_p); |
| 18 |
txbuf = &tx_ring->txbufs[idx]; |
| 19 |
|
| 20 |
txbuf->real_len = 0; |
| 21 |
|
| 22 |
tx_ring->qcp_rd_p++; |
| 23 |
tx_ring->rd_p++; |
| 24 |
|
| 25 |
if (tx_ring->r_vec->xsk_pool) { |
| 26 |
if (txbuf->is_xsk_tx) |
| 27 |
nfp_nfd3_xsk_tx_free(txbuf); |
| 28 |
|
| 29 |
xsk_tx_completed(tx_ring->r_vec->xsk_pool, 1); |
| 30 |
} |
| 31 |
} |
| 32 |
} |
| 33 |
|
| 34 |
/** |
| 35 |
* nfp_nfd3_tx_ring_reset() - Free any untransmitted buffers and reset pointers |
| 36 |
* @dp: NFP Net data path struct |
| 37 |
* @tx_ring: TX ring structure |
| 38 |
* |
| 39 |
* Assumes that the device is stopped, must be idempotent. |
| 40 |
*/ |
| 41 |
static void |
| 42 |
nfp_nfd3_tx_ring_reset(struct nfp_net_dp *dp, struct nfp_net_tx_ring *tx_ring) |
| 43 |
{ |
| 44 |
struct netdev_queue *nd_q; |
| 45 |
const skb_frag_t *frag; |
| 46 |
|
| 47 |
while (!tx_ring->is_xdp && tx_ring->rd_p != tx_ring->wr_p) { |
| 48 |
struct nfp_nfd3_tx_buf *tx_buf; |
| 49 |
struct sk_buff *skb; |
| 50 |
int idx, nr_frags; |
| 51 |
|
| 52 |
idx = D_IDX(tx_ring, tx_ring->rd_p); |
| 53 |
tx_buf = &tx_ring->txbufs[idx]; |
| 54 |
|
| 55 |
skb = tx_ring->txbufs[idx].skb; |
| 56 |
nr_frags = skb_shinfo(skb)->nr_frags; |
| 57 |
|
| 58 |
if (tx_buf->fidx == -1) { |
| 59 |
/* unmap head */ |
| 60 |
dma_unmap_single(dp->dev, tx_buf->dma_addr, |
| 61 |
skb_headlen(skb), DMA_TO_DEVICE); |
| 62 |
} else { |
| 63 |
/* unmap fragment */ |
| 64 |
frag = &skb_shinfo(skb)->frags[tx_buf->fidx]; |
| 65 |
dma_unmap_page(dp->dev, tx_buf->dma_addr, |
| 66 |
skb_frag_size(frag), DMA_TO_DEVICE); |
| 67 |
} |
| 68 |
|
| 69 |
/* check for last gather fragment */ |
| 70 |
if (tx_buf->fidx == nr_frags - 1) |
| 71 |
dev_kfree_skb_any(skb); |
| 72 |
|
| 73 |
tx_buf->dma_addr = 0; |
| 74 |
tx_buf->skb = NULL; |
| 75 |
tx_buf->fidx = -2; |
| 76 |
|
| 77 |
tx_ring->qcp_rd_p++; |
| 78 |
tx_ring->rd_p++; |
| 79 |
} |
| 80 |
|
| 81 |
if (tx_ring->is_xdp) |
| 82 |
nfp_nfd3_xsk_tx_bufs_free(tx_ring); |
| 83 |
|
| 84 |
memset(tx_ring->txds, 0, tx_ring->size); |
| 85 |
tx_ring->wr_p = 0; |
| 86 |
tx_ring->rd_p = 0; |
| 87 |
tx_ring->qcp_rd_p = 0; |
| 88 |
tx_ring->wr_ptr_add = 0; |
| 89 |
|
| 90 |
if (tx_ring->is_xdp || !dp->netdev) |
| 91 |
return; |
| 92 |
|
| 93 |
nd_q = netdev_get_tx_queue(dp->netdev, tx_ring->idx); |
| 94 |
netdev_tx_reset_queue(nd_q); |
| 95 |
} |
| 96 |
|
| 97 |
/** |
| 98 |
* nfp_nfd3_tx_ring_free() - Free resources allocated to a TX ring |
| 99 |
* @tx_ring: TX ring to free |
| 100 |
*/ |
| 101 |
static void nfp_nfd3_tx_ring_free(struct nfp_net_tx_ring *tx_ring) |
| 102 |
{ |
| 103 |
struct nfp_net_r_vector *r_vec = tx_ring->r_vec; |
| 104 |
struct nfp_net_dp *dp = &r_vec->nfp_net->dp; |
| 105 |
|
| 106 |
kvfree(tx_ring->txbufs); |
| 107 |
|
| 108 |
if (tx_ring->txds) |
| 109 |
dma_free_coherent(dp->dev, tx_ring->size, |
| 110 |
tx_ring->txds, tx_ring->dma); |
| 111 |
|
| 112 |
tx_ring->cnt = 0; |
| 113 |
tx_ring->txbufs = NULL; |
| 114 |
tx_ring->txds = NULL; |
| 115 |
tx_ring->dma = 0; |
| 116 |
tx_ring->size = 0; |
| 117 |
} |
| 118 |
|
| 119 |
/** |
| 120 |
* nfp_nfd3_tx_ring_alloc() - Allocate resource for a TX ring |
| 121 |
* @dp: NFP Net data path struct |
| 122 |
* @tx_ring: TX Ring structure to allocate |
| 123 |
* |
| 124 |
* Return: 0 on success, negative errno otherwise. |
| 125 |
*/ |
| 126 |
static int |
| 127 |
nfp_nfd3_tx_ring_alloc(struct nfp_net_dp *dp, struct nfp_net_tx_ring *tx_ring) |
| 128 |
{ |
| 129 |
struct nfp_net_r_vector *r_vec = tx_ring->r_vec; |
| 130 |
|
| 131 |
tx_ring->cnt = dp->txd_cnt; |
| 132 |
|
| 133 |
tx_ring->size = array_size(tx_ring->cnt, sizeof(*tx_ring->txds)); |
| 134 |
tx_ring->txds = dma_alloc_coherent(dp->dev, tx_ring->size, |
| 135 |
&tx_ring->dma, |
| 136 |
GFP_KERNEL | __GFP_NOWARN); |
| 137 |
if (!tx_ring->txds) { |
| 138 |
netdev_warn(dp->netdev, "failed to allocate TX descriptor ring memory, requested descriptor count: %d, consider lowering descriptor count\n", |
| 139 |
tx_ring->cnt); |
| 140 |
goto err_alloc; |
| 141 |
} |
| 142 |
|
| 143 |
tx_ring->txbufs = kvzalloc_objs(*tx_ring->txbufs, tx_ring->cnt); |
| 144 |
if (!tx_ring->txbufs) |
| 145 |
goto err_alloc; |
| 146 |
|
| 147 |
if (!tx_ring->is_xdp && dp->netdev) |
| 148 |
netif_set_xps_queue(dp->netdev, &r_vec->affinity_mask, |
| 149 |
tx_ring->idx); |
| 150 |
|
| 151 |
return 0; |
| 152 |
|
| 153 |
err_alloc: |
| 154 |
nfp_nfd3_tx_ring_free(tx_ring); |
| 155 |
return -ENOMEM; |
| 156 |
} |
| 157 |
|
| 158 |
static void |
| 159 |
nfp_nfd3_tx_ring_bufs_free(struct nfp_net_dp *dp, |
| 160 |
struct nfp_net_tx_ring *tx_ring) |
| 161 |
{ |
| 162 |
unsigned int i; |
| 163 |
|
| 164 |
if (!tx_ring->is_xdp) |
| 165 |
return; |
| 166 |
|
| 167 |
for (i = 0; i < tx_ring->cnt; i++) { |
| 168 |
if (!tx_ring->txbufs[i].frag) |
| 169 |
return; |
| 170 |
|
| 171 |
nfp_net_dma_unmap_rx(dp, tx_ring->txbufs[i].dma_addr); |
| 172 |
__free_page(virt_to_page(tx_ring->txbufs[i].frag)); |
| 173 |
} |
| 174 |
} |
| 175 |
|
| 176 |
static int |
| 177 |
nfp_nfd3_tx_ring_bufs_alloc(struct nfp_net_dp *dp, |
| 178 |
struct nfp_net_tx_ring *tx_ring) |
| 179 |
{ |
| 180 |
struct nfp_nfd3_tx_buf *txbufs = tx_ring->txbufs; |
| 181 |
unsigned int i; |
| 182 |
|
| 183 |
if (!tx_ring->is_xdp) |
| 184 |
return 0; |
| 185 |
|
| 186 |
for (i = 0; i < tx_ring->cnt; i++) { |
| 187 |
txbufs[i].frag = nfp_net_rx_alloc_one(dp, &txbufs[i].dma_addr); |
| 188 |
if (!txbufs[i].frag) { |
| 189 |
nfp_nfd3_tx_ring_bufs_free(dp, tx_ring); |
| 190 |
return -ENOMEM; |
| 191 |
} |
| 192 |
} |
| 193 |
|
| 194 |
return 0; |
| 195 |
} |
| 196 |
|
| 197 |
static void |
| 198 |
nfp_nfd3_print_tx_descs(struct seq_file *file, |
| 199 |
struct nfp_net_r_vector *r_vec, |
| 200 |
struct nfp_net_tx_ring *tx_ring, |
| 201 |
u32 d_rd_p, u32 d_wr_p) |
| 202 |
{ |
| 203 |
struct nfp_nfd3_tx_desc *txd; |
| 204 |
u32 txd_cnt = tx_ring->cnt; |
| 205 |
int i; |
| 206 |
|
| 207 |
for (i = 0; i < txd_cnt; i++) { |
| 208 |
struct xdp_buff *xdp; |
| 209 |
struct sk_buff *skb; |
| 210 |
|
| 211 |
txd = &tx_ring->txds[i]; |
| 212 |
seq_printf(file, "%04d: 0x%08x 0x%08x 0x%08x 0x%08x", i, |
| 213 |
txd->vals[0], txd->vals[1], |
| 214 |
txd->vals[2], txd->vals[3]); |
| 215 |
|
| 216 |
if (!tx_ring->is_xdp) { |
| 217 |
skb = READ_ONCE(tx_ring->txbufs[i].skb); |
| 218 |
if (skb) |
| 219 |
seq_printf(file, " skb->head=%p skb->data=%p", |
| 220 |
skb->head, skb->data); |
| 221 |
} else { |
| 222 |
xdp = READ_ONCE(tx_ring->txbufs[i].xdp); |
| 223 |
if (xdp) |
| 224 |
seq_printf(file, " xdp->data=%p", xdp->data); |
| 225 |
} |
| 226 |
|
| 227 |
if (tx_ring->txbufs[i].dma_addr) |
| 228 |
seq_printf(file, " dma_addr=%pad", |
| 229 |
&tx_ring->txbufs[i].dma_addr); |
| 230 |
|
| 231 |
if (i == tx_ring->rd_p % txd_cnt) |
| 232 |
seq_puts(file, " H_RD"); |
| 233 |
if (i == tx_ring->wr_p % txd_cnt) |
| 234 |
seq_puts(file, " H_WR"); |
| 235 |
if (i == d_rd_p % txd_cnt) |
| 236 |
seq_puts(file, " D_RD"); |
| 237 |
if (i == d_wr_p % txd_cnt) |
| 238 |
seq_puts(file, " D_WR"); |
| 239 |
|
| 240 |
seq_putc(file, '\n'); |
| 241 |
} |
| 242 |
} |
| 243 |
|
| 244 |
#define NFP_NFD3_CFG_CTRL_SUPPORTED \ |
| 245 |
(NFP_NET_CFG_CTRL_ENABLE | NFP_NET_CFG_CTRL_PROMISC | \ |
| 246 |
NFP_NET_CFG_CTRL_L2BC | NFP_NET_CFG_CTRL_L2MC | \ |
| 247 |
NFP_NET_CFG_CTRL_RXCSUM | NFP_NET_CFG_CTRL_TXCSUM | \ |
| 248 |
NFP_NET_CFG_CTRL_RXVLAN | NFP_NET_CFG_CTRL_TXVLAN | \ |
| 249 |
NFP_NET_CFG_CTRL_RXVLAN_V2 | NFP_NET_CFG_CTRL_RXQINQ | \ |
| 250 |
NFP_NET_CFG_CTRL_TXVLAN_V2 | \ |
| 251 |
NFP_NET_CFG_CTRL_GATHER | NFP_NET_CFG_CTRL_LSO | \ |
| 252 |
NFP_NET_CFG_CTRL_CTAG_FILTER | NFP_NET_CFG_CTRL_CMSG_DATA | \ |
| 253 |
NFP_NET_CFG_CTRL_RINGCFG | NFP_NET_CFG_CTRL_RSS | \ |
| 254 |
NFP_NET_CFG_CTRL_IRQMOD | NFP_NET_CFG_CTRL_TXRWB | \ |
| 255 |
NFP_NET_CFG_CTRL_VEPA | \ |
| 256 |
NFP_NET_CFG_CTRL_VXLAN | NFP_NET_CFG_CTRL_NVGRE | \ |
| 257 |
NFP_NET_CFG_CTRL_BPF | NFP_NET_CFG_CTRL_LSO2 | \ |
| 258 |
NFP_NET_CFG_CTRL_RSS2 | NFP_NET_CFG_CTRL_CSUM_COMPLETE | \ |
| 259 |
NFP_NET_CFG_CTRL_LIVE_ADDR) |
| 260 |
|
| 261 |
const struct nfp_dp_ops nfp_nfd3_ops = { |
| 262 |
.version = NFP_NFD_VER_NFD3, |
| 263 |
.tx_min_desc_per_pkt = 1, |
| 264 |
.cap_mask = NFP_NFD3_CFG_CTRL_SUPPORTED, |
| 265 |
.dma_mask = DMA_BIT_MASK(40), |
| 266 |
.poll = nfp_nfd3_poll, |
| 267 |
.xsk_poll = nfp_nfd3_xsk_poll, |
| 268 |
.ctrl_poll = nfp_nfd3_ctrl_poll, |
| 269 |
.xmit = nfp_nfd3_tx, |
| 270 |
.ctrl_tx_one = nfp_nfd3_ctrl_tx_one, |
| 271 |
.rx_ring_fill_freelist = nfp_nfd3_rx_ring_fill_freelist, |
| 272 |
.tx_ring_alloc = nfp_nfd3_tx_ring_alloc, |
| 273 |
.tx_ring_reset = nfp_nfd3_tx_ring_reset, |
| 274 |
.tx_ring_free = nfp_nfd3_tx_ring_free, |
| 275 |
.tx_ring_bufs_alloc = nfp_nfd3_tx_ring_bufs_alloc, |
| 276 |
.tx_ring_bufs_free = nfp_nfd3_tx_ring_bufs_free, |
| 277 |
.print_tx_descs = nfp_nfd3_print_tx_descs |
| 278 |
}; |