1
0
mirror of https://github.com/corundum/corundum.git synced 2025-01-16 08:12:53 +08:00
corundum/modules/mqnic/mqnic_tx.c

524 lines
12 KiB
C
Raw Normal View History

// SPDX-License-Identifier: BSD-2-Clause-Views
2019-07-17 18:13:51 -07:00
/*
* Copyright (c) 2019-2023 The Regents of the University of California
*/
2019-07-17 18:13:51 -07:00
2019-08-19 18:38:01 -07:00
#include <linux/version.h>
2019-07-17 18:13:51 -07:00
#include "mqnic.h"
struct mqnic_ring *mqnic_create_tx_ring(struct mqnic_if *interface)
2019-07-17 18:13:51 -07:00
{
2021-10-08 18:31:53 -07:00
struct mqnic_ring *ring;
ring = kzalloc(sizeof(*ring), GFP_KERNEL);
if (!ring)
return ERR_PTR(-ENOMEM);
2021-12-12 17:28:43 -08:00
ring->dev = interface->dev;
ring->interface = interface;
2021-12-10 20:59:44 -08:00
ring->index = -1;
ring->enabled = 0;
2021-12-10 21:03:46 -08:00
ring->hw_addr = NULL;
ring->prod_ptr = 0;
ring->cons_ptr = 0;
return ring;
}
void mqnic_destroy_tx_ring(struct mqnic_ring *ring)
{
mqnic_close_tx_ring(ring);
kfree(ring);
}
int mqnic_open_tx_ring(struct mqnic_ring *ring, struct mqnic_priv *priv,
struct mqnic_cq *cq, int size, int desc_block_size)
{
int ret = 0;
if (ring->enabled || ring->hw_addr || ring->buf || !priv || !cq)
return -EINVAL;
ring->index = mqnic_res_alloc(ring->interface->txq_res);
if (ring->index < 0)
return -ENOMEM;
ring->log_desc_block_size = desc_block_size < 2 ? 0 : ilog2(desc_block_size - 1) + 1;
ring->desc_block_size = 1 << ring->log_desc_block_size;
2021-10-08 18:31:53 -07:00
ring->size = roundup_pow_of_two(size);
ring->full_size = ring->size >> 1;
ring->size_mask = ring->size - 1;
ring->stride = roundup_pow_of_two(MQNIC_DESC_SIZE * ring->desc_block_size);
2021-10-08 18:31:53 -07:00
ring->tx_info = kvzalloc(sizeof(*ring->tx_info) * ring->size, GFP_KERNEL);
if (!ring->tx_info) {
ret = -ENOMEM;
goto fail;
}
2021-10-08 18:31:53 -07:00
ring->buf_size = ring->size * ring->stride;
ring->buf = dma_alloc_coherent(ring->dev, ring->buf_size, &ring->buf_dma_addr, GFP_KERNEL);
2021-10-08 18:31:53 -07:00
if (!ring->buf) {
ret = -ENOMEM;
goto fail;
2021-10-08 18:31:53 -07:00
}
ring->priv = priv;
ring->cq = cq;
cq->src_ring = ring;
cq->handler = mqnic_tx_irq;
ring->hw_addr = mqnic_res_get_addr(ring->interface->txq_res, ring->index);
ring->prod_ptr = 0;
ring->cons_ptr = 0;
2021-10-08 18:31:53 -07:00
// deactivate queue
iowrite32(MQNIC_QUEUE_CMD_SET_ENABLE | 0,
ring->hw_addr + MQNIC_QUEUE_CTRL_STATUS_REG);
2021-10-08 18:31:53 -07:00
// set base address
iowrite32((ring->buf_dma_addr & 0xfffff000),
ring->hw_addr + MQNIC_QUEUE_BASE_ADDR_VF_REG + 0);
iowrite32(ring->buf_dma_addr >> 32,
ring->hw_addr + MQNIC_QUEUE_BASE_ADDR_VF_REG + 4);
// set size
iowrite32(MQNIC_QUEUE_CMD_SET_SIZE | ilog2(ring->size) | (ring->log_desc_block_size << 8),
ring->hw_addr + MQNIC_QUEUE_CTRL_STATUS_REG);
// set CQN
iowrite32(MQNIC_QUEUE_CMD_SET_CQN | ring->cq->cqn,
ring->hw_addr + MQNIC_QUEUE_CTRL_STATUS_REG);
2021-10-08 18:31:53 -07:00
// set pointers
iowrite32(MQNIC_QUEUE_CMD_SET_PROD_PTR | (ring->prod_ptr & MQNIC_QUEUE_PTR_MASK),
ring->hw_addr + MQNIC_QUEUE_CTRL_STATUS_REG);
iowrite32(MQNIC_QUEUE_CMD_SET_CONS_PTR | (ring->cons_ptr & MQNIC_QUEUE_PTR_MASK),
ring->hw_addr + MQNIC_QUEUE_CTRL_STATUS_REG);
2021-10-08 18:31:53 -07:00
return 0;
2019-07-17 18:13:51 -07:00
fail:
mqnic_close_tx_ring(ring);
2021-10-08 18:31:53 -07:00
return ret;
2019-07-17 18:13:51 -07:00
}
void mqnic_close_tx_ring(struct mqnic_ring *ring)
2019-07-17 18:13:51 -07:00
{
mqnic_disable_tx_ring(ring);
if (ring->cq) {
ring->cq->src_ring = NULL;
ring->cq->handler = NULL;
}
ring->priv = NULL;
ring->cq = NULL;
ring->hw_addr = NULL;
2019-07-17 18:13:51 -07:00
if (ring->buf) {
mqnic_free_tx_buf(ring);
2019-07-17 18:13:51 -07:00
dma_free_coherent(ring->dev, ring->buf_size, ring->buf, ring->buf_dma_addr);
ring->buf = NULL;
ring->buf_dma_addr = 0;
}
if (ring->tx_info) {
kvfree(ring->tx_info);
ring->tx_info = NULL;
}
mqnic_res_free(ring->interface->txq_res, ring->index);
ring->index = -1;
2019-07-17 18:13:51 -07:00
}
int mqnic_enable_tx_ring(struct mqnic_ring *ring)
2019-07-17 18:13:51 -07:00
{
if (!ring->hw_addr)
return -EINVAL;
// enable queue
iowrite32(MQNIC_QUEUE_CMD_SET_ENABLE | 1,
ring->hw_addr + MQNIC_QUEUE_CTRL_STATUS_REG);
2021-10-08 18:31:53 -07:00
ring->enabled = 1;
2021-12-10 21:04:52 -08:00
2021-10-08 18:31:53 -07:00
return 0;
2019-07-17 18:13:51 -07:00
}
void mqnic_disable_tx_ring(struct mqnic_ring *ring)
2019-07-17 18:13:51 -07:00
{
// disable queue
if (ring->hw_addr) {
iowrite32(MQNIC_QUEUE_CMD_SET_ENABLE | 0,
ring->hw_addr + MQNIC_QUEUE_CTRL_STATUS_REG);
2021-12-12 14:20:56 -08:00
}
ring->enabled = 0;
2019-07-17 18:13:51 -07:00
}
bool mqnic_is_tx_ring_empty(const struct mqnic_ring *ring)
{
return ring->prod_ptr == ring->cons_ptr;
2019-07-17 18:13:51 -07:00
}
bool mqnic_is_tx_ring_full(const struct mqnic_ring *ring)
{
return ring->prod_ptr - ring->cons_ptr >= ring->full_size;
2019-07-17 18:13:51 -07:00
}
void mqnic_tx_read_cons_ptr(struct mqnic_ring *ring)
2019-07-17 18:13:51 -07:00
{
ring->cons_ptr += ((ioread32(ring->hw_addr + MQNIC_QUEUE_PTR_REG) >> 16) - ring->cons_ptr) & MQNIC_QUEUE_PTR_MASK;
2019-07-17 18:13:51 -07:00
}
void mqnic_tx_write_prod_ptr(struct mqnic_ring *ring)
2019-07-17 18:13:51 -07:00
{
iowrite32(MQNIC_QUEUE_CMD_SET_PROD_PTR | (ring->prod_ptr & MQNIC_QUEUE_PTR_MASK),
ring->hw_addr + MQNIC_QUEUE_CTRL_STATUS_REG);
2019-07-17 18:13:51 -07:00
}
2021-12-10 20:59:44 -08:00
void mqnic_free_tx_desc(struct mqnic_ring *ring, int index, int napi_budget)
2019-07-17 18:13:51 -07:00
{
2021-10-08 18:31:53 -07:00
struct mqnic_tx_info *tx_info = &ring->tx_info[index];
struct sk_buff *skb = tx_info->skb;
u32 i;
2020-04-21 17:18:58 -07:00
2021-10-08 18:31:53 -07:00
prefetchw(&skb->users);
2019-07-17 18:13:51 -07:00
2021-12-10 20:59:44 -08:00
dma_unmap_single(ring->dev, dma_unmap_addr(tx_info, dma_addr),
dma_unmap_len(tx_info, len), DMA_TO_DEVICE);
2021-10-08 18:31:53 -07:00
dma_unmap_addr_set(tx_info, dma_addr, 0);
2020-04-21 17:18:58 -07:00
2021-10-08 18:31:53 -07:00
// unmap frags
for (i = 0; i < tx_info->frag_count; i++)
2021-12-10 20:59:44 -08:00
dma_unmap_page(ring->dev, tx_info->frags[i].dma_addr,
tx_info->frags[i].len, DMA_TO_DEVICE);
2020-04-21 17:18:58 -07:00
2021-10-08 18:31:53 -07:00
napi_consume_skb(skb, napi_budget);
tx_info->skb = NULL;
2019-07-17 18:13:51 -07:00
}
2021-12-10 20:59:44 -08:00
int mqnic_free_tx_buf(struct mqnic_ring *ring)
2019-07-17 18:13:51 -07:00
{
2021-10-08 18:31:53 -07:00
u32 index;
int cnt = 0;
while (!mqnic_is_tx_ring_empty(ring)) {
index = ring->cons_ptr & ring->size_mask;
2021-12-10 20:59:44 -08:00
mqnic_free_tx_desc(ring, index, 0);
ring->cons_ptr++;
2021-10-08 18:31:53 -07:00
cnt++;
}
return cnt;
2019-07-17 18:13:51 -07:00
}
int mqnic_process_tx_cq(struct mqnic_cq *cq, int napi_budget)
2019-07-17 18:13:51 -07:00
{
struct mqnic_if *interface = cq->interface;
struct mqnic_ring *tx_ring = cq->src_ring;
2021-12-12 17:28:43 -08:00
struct mqnic_priv *priv = tx_ring->priv;
2021-10-08 18:31:53 -07:00
struct mqnic_tx_info *tx_info;
struct mqnic_cpl *cpl;
struct skb_shared_hwtstamps hwts;
2021-10-08 18:31:53 -07:00
u32 cq_index;
u32 cq_cons_ptr;
2021-10-08 18:31:53 -07:00
u32 ring_index;
u32 ring_cons_ptr;
2021-10-08 18:31:53 -07:00
u32 packets = 0;
u32 bytes = 0;
int done = 0;
int budget = napi_budget;
2021-12-12 17:28:43 -08:00
if (unlikely(!priv || !priv->port_up))
2021-10-08 18:31:53 -07:00
return done;
// prefetch for BQL
2021-12-12 01:37:55 -08:00
netdev_txq_bql_complete_prefetchw(tx_ring->tx_queue);
2021-10-08 18:31:53 -07:00
// process completion queue
cq_cons_ptr = cq->cons_ptr;
cq_index = cq_cons_ptr & cq->size_mask;
2021-10-08 18:31:53 -07:00
while (done < budget) {
cpl = (struct mqnic_cpl *)(cq->buf + cq_index * cq->stride);
if (!!(cpl->phase & cpu_to_le32(0x80000000)) == !!(cq_cons_ptr & cq->size))
break;
dma_rmb();
2021-12-12 01:37:55 -08:00
ring_index = le16_to_cpu(cpl->index) & tx_ring->size_mask;
tx_info = &tx_ring->tx_info[ring_index];
2021-10-08 18:31:53 -07:00
// TX hardware timestamp
if (unlikely(tx_info->ts_requested)) {
netdev_dbg(priv->ndev, "%s: TX TS requested", __func__);
2021-12-12 17:28:43 -08:00
hwts.hwtstamp = mqnic_read_cpl_ts(interface->mdev, tx_ring, cpl);
2021-10-08 18:31:53 -07:00
skb_tstamp_tx(tx_info->skb, &hwts);
}
// free TX descriptor
2021-12-12 01:37:55 -08:00
mqnic_free_tx_desc(tx_ring, ring_index, napi_budget);
2021-10-08 18:31:53 -07:00
packets++;
bytes += le16_to_cpu(cpl->len);
done++;
cq_cons_ptr++;
cq_index = cq_cons_ptr & cq->size_mask;
2021-10-08 18:31:53 -07:00
}
// update CQ consumer pointer
cq->cons_ptr = cq_cons_ptr;
mqnic_cq_write_cons_ptr(cq);
2021-10-08 18:31:53 -07:00
// process ring
ring_cons_ptr = READ_ONCE(tx_ring->cons_ptr);
ring_index = ring_cons_ptr & tx_ring->size_mask;
2021-10-08 18:31:53 -07:00
while (ring_cons_ptr != tx_ring->prod_ptr) {
2021-12-12 01:37:55 -08:00
tx_info = &tx_ring->tx_info[ring_index];
2021-10-08 18:31:53 -07:00
if (tx_info->skb)
break;
ring_cons_ptr++;
ring_index = ring_cons_ptr & tx_ring->size_mask;
2021-10-08 18:31:53 -07:00
}
// update consumer pointer
WRITE_ONCE(tx_ring->cons_ptr, ring_cons_ptr);
2021-10-08 18:31:53 -07:00
// BQL
2021-12-12 01:37:55 -08:00
//netdev_tx_completed_queue(tx_ring->tx_queue, packets, bytes);
2021-10-08 18:31:53 -07:00
// wake queue if it is stopped
2021-12-12 01:37:55 -08:00
if (netif_tx_queue_stopped(tx_ring->tx_queue) && !mqnic_is_tx_ring_full(tx_ring))
netif_tx_wake_queue(tx_ring->tx_queue);
2021-10-08 18:31:53 -07:00
return done;
2019-07-17 18:13:51 -07:00
}
void mqnic_tx_irq(struct mqnic_cq *cq)
2019-07-17 18:13:51 -07:00
{
2021-12-12 17:28:43 -08:00
napi_schedule_irqoff(&cq->napi);
2019-07-17 18:13:51 -07:00
}
int mqnic_poll_tx_cq(struct napi_struct *napi, int budget)
{
struct mqnic_cq *cq = container_of(napi, struct mqnic_cq, napi);
2021-10-08 18:31:53 -07:00
int done;
2019-07-17 18:13:51 -07:00
done = mqnic_process_tx_cq(cq, budget);
2020-03-10 22:06:02 -07:00
2021-10-08 18:31:53 -07:00
if (done == budget)
return done;
2019-07-17 18:13:51 -07:00
2021-10-08 18:31:53 -07:00
napi_complete(napi);
2019-07-17 18:13:51 -07:00
mqnic_arm_cq(cq);
2019-07-17 18:13:51 -07:00
2021-10-08 18:31:53 -07:00
return done;
2019-07-17 18:13:51 -07:00
}
2021-12-10 20:59:44 -08:00
static bool mqnic_map_skb(struct mqnic_ring *ring, struct mqnic_tx_info *tx_info,
struct mqnic_desc *tx_desc, struct sk_buff *skb)
2020-04-21 17:18:58 -07:00
{
2021-10-08 18:31:53 -07:00
struct skb_shared_info *shinfo = skb_shinfo(skb);
const skb_frag_t *frag;
2021-10-08 18:31:53 -07:00
u32 i;
u32 len;
dma_addr_t dma_addr;
// update tx_info
tx_info->skb = skb;
tx_info->frag_count = 0;
for (i = 0; i < shinfo->nr_frags; i++) {
frag = &shinfo->frags[i];
2021-10-08 18:31:53 -07:00
len = skb_frag_size(frag);
2021-12-10 20:59:44 -08:00
dma_addr = skb_frag_dma_map(ring->dev, frag, 0, len, DMA_TO_DEVICE);
if (unlikely(dma_mapping_error(ring->dev, dma_addr)))
2021-10-08 18:31:53 -07:00
// mapping failed
goto map_error;
// write descriptor
tx_desc[i + 1].len = cpu_to_le32(len);
tx_desc[i + 1].addr = cpu_to_le64(dma_addr);
// update tx_info
tx_info->frag_count = i + 1;
tx_info->frags[i].len = len;
tx_info->frags[i].dma_addr = dma_addr;
}
for (i = tx_info->frag_count; i < ring->desc_block_size - 1; i++) {
tx_desc[i + 1].len = 0;
tx_desc[i + 1].addr = 0;
}
// map skb
len = skb_headlen(skb);
dma_addr = dma_map_single(ring->dev, skb->data, len, DMA_TO_DEVICE);
2021-10-08 18:31:53 -07:00
2021-12-10 20:59:44 -08:00
if (unlikely(dma_mapping_error(ring->dev, dma_addr)))
2021-10-08 18:31:53 -07:00
// mapping failed
goto map_error;
// write descriptor
tx_desc[0].len = cpu_to_le32(len);
tx_desc[0].addr = cpu_to_le64(dma_addr);
// update tx_info
dma_unmap_addr_set(tx_info, dma_addr, dma_addr);
dma_unmap_len_set(tx_info, len, len);
return true;
2020-04-21 17:18:58 -07:00
map_error:
2021-12-10 20:59:44 -08:00
dev_err(ring->dev, "%s: DMA mapping failed", __func__);
2020-04-21 17:18:58 -07:00
2021-10-08 18:31:53 -07:00
// unmap frags
for (i = 0; i < tx_info->frag_count; i++)
2021-12-10 20:59:44 -08:00
dma_unmap_page(ring->dev, tx_info->frags[i].dma_addr,
tx_info->frags[i].len, DMA_TO_DEVICE);
2020-04-21 17:18:58 -07:00
2021-10-08 18:31:53 -07:00
// update tx_info
tx_info->skb = NULL;
tx_info->frag_count = 0;
2020-04-21 17:18:58 -07:00
2021-10-08 18:31:53 -07:00
return false;
2020-04-21 17:18:58 -07:00
}
2019-07-17 18:13:51 -07:00
netdev_tx_t mqnic_start_xmit(struct sk_buff *skb, struct net_device *ndev)
{
2021-10-08 18:31:53 -07:00
struct skb_shared_info *shinfo = skb_shinfo(skb);
struct mqnic_priv *priv = netdev_priv(ndev);
struct mqnic_ring *ring;
struct mqnic_tx_info *tx_info;
struct mqnic_desc *tx_desc;
int ring_index;
u32 index;
bool stop_queue;
u32 cons_ptr;
2021-10-08 18:31:53 -07:00
if (unlikely(!priv->port_up))
goto tx_drop;
ring_index = skb_get_queue_mapping(skb);
rcu_read_lock();
ring = radix_tree_lookup(&priv->txq_table, ring_index);
rcu_read_unlock();
2021-10-08 18:31:53 -07:00
if (unlikely(!ring))
// unknown TX queue
goto tx_drop;
2021-10-08 18:31:53 -07:00
cons_ptr = READ_ONCE(ring->cons_ptr);
2021-10-08 18:31:53 -07:00
// prefetch for BQL
netdev_txq_bql_enqueue_prefetchw(ring->tx_queue);
index = ring->prod_ptr & ring->size_mask;
2021-10-08 18:31:53 -07:00
tx_desc = (struct mqnic_desc *)(ring->buf + index * ring->stride);
tx_info = &ring->tx_info[index];
// TX hardware timestamp
tx_info->ts_requested = 0;
2022-01-15 21:53:13 -08:00
if (unlikely(priv->if_features & MQNIC_IF_FEATURE_PTP_TS && shinfo->tx_flags & SKBTX_HW_TSTAMP)) {
netdev_dbg(ndev, "%s: TX TS requested", __func__);
2021-10-08 18:31:53 -07:00
shinfo->tx_flags |= SKBTX_IN_PROGRESS;
tx_info->ts_requested = 1;
}
// TX hardware checksum
if (skb->ip_summed == CHECKSUM_PARTIAL) {
unsigned int csum_start = skb_checksum_start_offset(skb);
unsigned int csum_offset = skb->csum_offset;
if (csum_start > 255 || csum_offset > 127) {
netdev_info(ndev, "%s: Hardware checksum fallback start %d offset %d",
__func__, csum_start, csum_offset);
2021-10-08 18:31:53 -07:00
// offset out of range, fall back on software checksum
if (skb_checksum_help(skb)) {
// software checksumming failed
goto tx_drop_count;
}
tx_desc->tx_csum_cmd = 0;
} else {
tx_desc->tx_csum_cmd = cpu_to_le16(0x8000 | (csum_offset << 8) | (csum_start));
}
} else {
tx_desc->tx_csum_cmd = 0;
}
if (shinfo->nr_frags > ring->desc_block_size - 1 || (skb->data_len && skb->data_len < 32)) {
// too many frags or very short data portion; linearize
if (skb_linearize(skb))
goto tx_drop_count;
}
// map skb
2021-12-10 20:59:44 -08:00
if (!mqnic_map_skb(ring, tx_info, tx_desc, skb))
2021-10-08 18:31:53 -07:00
// map failed
goto tx_drop_count;
// count packet
ring->packets++;
ring->bytes += skb->len;
// enqueue
ring->prod_ptr++;
2021-10-08 18:31:53 -07:00
skb_tx_timestamp(skb);
stop_queue = mqnic_is_tx_ring_full(ring);
if (unlikely(stop_queue)) {
netdev_dbg(ndev, "%s: TX ring %d full on port %d",
__func__, ring_index, priv->index);
2021-10-08 18:31:53 -07:00
netif_tx_stop_queue(ring->tx_queue);
}
// BQL
//netdev_tx_sent_queue(ring->tx_queue, tx_info->len);
//__netdev_tx_sent_queue(ring->tx_queue, tx_info->len, skb->xmit_more);
// enqueue on NIC
#if LINUX_VERSION_CODE >= KERNEL_VERSION(5, 2, 0)
if (unlikely(!netdev_xmit_more() || stop_queue)) {
2019-08-19 18:38:01 -07:00
#else
2021-10-08 18:31:53 -07:00
if (unlikely(!skb->xmit_more || stop_queue)) {
2019-08-19 18:38:01 -07:00
#endif
2021-10-08 18:31:53 -07:00
dma_wmb();
mqnic_tx_write_prod_ptr(ring);
2021-10-08 18:31:53 -07:00
}
2019-07-17 18:13:51 -07:00
2021-10-08 18:31:53 -07:00
// check if queue restarted
if (unlikely(stop_queue)) {
smp_rmb();
2019-07-17 18:13:51 -07:00
cons_ptr = READ_ONCE(ring->cons_ptr);
2019-07-17 18:13:51 -07:00
2021-10-08 18:31:53 -07:00
if (unlikely(!mqnic_is_tx_ring_full(ring)))
netif_tx_wake_queue(ring->tx_queue);
}
2019-07-17 18:13:51 -07:00
2021-10-08 18:31:53 -07:00
return NETDEV_TX_OK;
2019-07-17 18:13:51 -07:00
tx_drop_count:
2021-10-08 18:31:53 -07:00
ring->dropped_packets++;
2019-07-17 18:13:51 -07:00
tx_drop:
2021-10-08 18:31:53 -07:00
dev_kfree_skb_any(skb);
return NETDEV_TX_OK;
2019-07-17 18:13:51 -07:00
}