/* CREATE_TRACE_POINTS only needs to be defined once. Other dpa files * using trace events only need to #include <trace/events/sched.h>
*/ #define CREATE_TRACE_POINTS #include"dpaa2-eth-trace.h"
/* Inform the stack there's no need to compute L3/L4 csum anymore */
skb->ip_summed = CHECKSUM_UNNECESSARY;
}
/* Free a received FD. * Not to be used for Tx conf FDs or on any other paths.
*/ staticvoid dpaa2_eth_free_rx_fd(struct dpaa2_eth_priv *priv, conststruct dpaa2_fd *fd, void *vaddr)
{ struct device *dev = priv->net_dev->dev.parent;
dma_addr_t addr = dpaa2_fd_get_addr(fd);
u8 fd_format = dpaa2_fd_get_format(fd); struct dpaa2_sg_entry *sgt; void *sg_vaddr; int i;
/* If single buffer frame, just free the data buffer */ if (fd_format == dpaa2_fd_single) goto free_buf; elseif (fd_format != dpaa2_fd_sg) /* We don't support any other format */ return;
/* For S/G frames, we first need to free all SG entries * except the first one, which was taken care of already
*/
sgt = vaddr + dpaa2_fd_get_offset(fd); for (i = 1; i < DPAA2_ETH_MAX_SG_ENTRIES; i++) {
addr = dpaa2_sg_get_addr(&sgt[i]);
sg_vaddr = dpaa2_iova_to_virt(priv->iommu_domain, addr);
dma_unmap_page(dev, addr, priv->rx_buf_size,
DMA_BIDIRECTIONAL);
free_pages((unsignedlong)sg_vaddr, 0); if (dpaa2_sg_is_final(&sgt[i])) break;
}
free_buf:
free_pages((unsignedlong)vaddr, 0);
}
/* Build a linear skb based on a single-buffer frame descriptor */ staticstruct sk_buff *dpaa2_eth_build_linear_skb(struct dpaa2_eth_channel *ch, conststruct dpaa2_fd *fd, void *fd_vaddr)
{ struct sk_buff *skb = NULL;
u16 fd_offset = dpaa2_fd_get_offset(fd);
u32 fd_length = dpaa2_fd_get_len(fd);
ch->buf_count--;
skb = build_skb(fd_vaddr, DPAA2_ETH_RX_BUF_RAW_SIZE); if (unlikely(!skb)) return NULL;
/* Build a non linear (fragmented) skb based on a S/G table */ staticstruct sk_buff *dpaa2_eth_build_frag_skb(struct dpaa2_eth_priv *priv, struct dpaa2_eth_channel *ch, struct dpaa2_sg_entry *sgt)
{ struct sk_buff *skb = NULL; struct device *dev = priv->net_dev->dev.parent; void *sg_vaddr;
dma_addr_t sg_addr;
u16 sg_offset;
u32 sg_length; struct page *page, *head_page; int page_offset; int i;
for (i = 0; i < DPAA2_ETH_MAX_SG_ENTRIES; i++) { struct dpaa2_sg_entry *sge = &sgt[i];
/* NOTE: We only support SG entries in dpaa2_sg_single format, * but this is the only format we may receive from HW anyway
*/
/* Get the address and length from the S/G entry */
sg_addr = dpaa2_sg_get_addr(sge);
sg_vaddr = dpaa2_iova_to_virt(priv->iommu_domain, sg_addr);
dma_unmap_page(dev, sg_addr, priv->rx_buf_size,
DMA_BIDIRECTIONAL);
sg_length = dpaa2_sg_get_len(sge);
if (i == 0) { /* We build the skb around the first data buffer */
skb = build_skb(sg_vaddr, DPAA2_ETH_RX_BUF_RAW_SIZE); if (unlikely(!skb)) { /* Free the first SG entry now, since we already * unmapped it and obtained the virtual address
*/
free_pages((unsignedlong)sg_vaddr, 0);
/* We still need to subtract the buffers used * by this FD from our software counter
*/ while (!dpaa2_sg_is_final(&sgt[i]) &&
i < DPAA2_ETH_MAX_SG_ENTRIES)
i++; break;
}
sg_offset = dpaa2_sg_get_offset(sge);
skb_reserve(skb, sg_offset);
skb_put(skb, sg_length);
} else { /* Rest of the data buffers are stored as skb frags */
page = virt_to_page(sg_vaddr);
head_page = virt_to_head_page(sg_vaddr);
/* Offset in page (which may be compound). * Data in subsequent SG entries is stored from the * beginning of the buffer, so we don't need to add the * sg_offset.
*/
page_offset = ((unsignedlong)sg_vaddr &
(PAGE_SIZE - 1)) +
(page_address(page) - page_address(head_page));
skb_add_rx_frag(skb, i - 1, head_page, page_offset,
sg_length, priv->rx_buf_size);
}
if (dpaa2_sg_is_final(sge)) break;
}
WARN_ONCE(i == DPAA2_ETH_MAX_SG_ENTRIES, "Final bit not set in SGT");
/* Count all data buffers + SG table buffer */
ch->buf_count -= i + 2;
return skb;
}
/* Free buffers acquired from the buffer pool or which were meant to * be released in the pool
*/ staticvoid dpaa2_eth_free_bufs(struct dpaa2_eth_priv *priv, u64 *buf_array, int count, bool xsk_zc)
{ struct device *dev = priv->net_dev->dev.parent; struct dpaa2_eth_swa *swa; struct xdp_buff *xdp_buff; void *vaddr; int i;
for (i = 0; i < count; i++) {
vaddr = dpaa2_iova_to_virt(priv->iommu_domain, buf_array[i]);
/* Mark the egress frame hardware annotation area as valid */
frc = dpaa2_fd_get_frc(fd);
dpaa2_fd_set_frc(fd, frc | DPAA2_FD_FRC_FAEADV);
dpaa2_fd_set_ctrl(fd, DPAA2_FD_CTRL_ASAL);
/* Instruct hardware to release the FD buffer directly into * the buffer pool once transmission is completed, instead of * sending a Tx confirmation frame to us
*/
ctrl = DPAA2_FAEAD_A4V | DPAA2_FAEAD_A2V | DPAA2_FAEAD_EBDDV;
faead = dpaa2_get_faead(buf_start, false);
faead->ctrl = cpu_to_le32(ctrl);
faead->conf_fqid = 0;
/* Check if we need to validate the L4 csum */ if (likely(dpaa2_fd_get_frc(fd) & DPAA2_FD_FRC_FASV)) {
status = le32_to_cpu(fas->status);
dpaa2_eth_validate_rx_csum(priv, status, skb);
}
/* Consume all frames pull-dequeued into the store. This is the simplest way to * make sure we don't accidentally issue another volatile dequeue which would * overwrite (leak) frames already in the store. * * Observance of NAPI budget is not our concern, leaving that to the caller.
*/ staticint dpaa2_eth_consume_frames(struct dpaa2_eth_channel *ch, struct dpaa2_eth_fq **src)
{ struct dpaa2_eth_priv *priv = ch->priv; struct dpaa2_eth_fq *fq = NULL; struct dpaa2_dq *dq; conststruct dpaa2_fd *fd; int cleaned = 0, retries = 0; int is_last;
do {
dq = dpaa2_io_store_next(ch->store, &is_last); if (unlikely(!dq)) { /* If we're here, we *must* have placed a * volatile dequeue comnmand, so keep reading through * the store until we get some sort of valid response * token (either a valid frame or an "empty dequeue")
*/ if (retries++ >= DPAA2_ETH_SWP_BUSY_RETRIES) {
netdev_err_once(priv->net_dev, "Unable to read a valid dequeue response\n"); return -ETIMEDOUT;
} continue;
}
/* Create a frame descriptor based on a fragmented skb */ staticint dpaa2_eth_build_sg_fd(struct dpaa2_eth_priv *priv, struct sk_buff *skb, struct dpaa2_fd *fd, void **swa_addr)
{ struct device *dev = priv->net_dev->dev.parent; void *sgt_buf = NULL;
dma_addr_t addr; int nr_frags = skb_shinfo(skb)->nr_frags; struct dpaa2_sg_entry *sgt; int i, err; int sgt_buf_size; struct scatterlist *scl, *crt_scl; int num_sg; int num_dma_bufs; struct dpaa2_eth_swa *swa;
/* Create and map scatterlist. * We don't advertise NETIF_F_FRAGLIST, so skb_to_sgvec() will not have * to go beyond nr_frags+1. * Note: We don't support chained scatterlists
*/ if (unlikely(PAGE_SIZE / sizeof(struct scatterlist) < nr_frags + 1)) return -EINVAL;
/* Fill in the HW SGT structure. * * sgt_buf is zeroed out, so the following fields are implicit * in all sgt entries: * - offset is 0 * - format is 'dpaa2_sg_single'
*/
for_each_sg(scl, crt_scl, num_dma_bufs, i) {
dpaa2_sg_set_addr(&sgt[i], sg_dma_address(crt_scl));
dpaa2_sg_set_len(&sgt[i], sg_dma_len(crt_scl));
}
dpaa2_sg_set_final(&sgt[i - 1], true);
/* Store the skb backpointer in the SGT buffer. * Fit the scatterlist and the number of buffers alongside the * skb backpointer in the software annotation area. We'll need * all of them on Tx Conf.
*/
*swa_addr = (void *)sgt_buf;
swa = (struct dpaa2_eth_swa *)sgt_buf;
swa->type = DPAA2_ETH_SWA_SG;
swa->sg.skb = skb;
swa->sg.scl = scl;
swa->sg.num_sg = num_sg;
swa->sg.sgt_size = sgt_buf_size;
/* Create a SG frame descriptor based on a linear skb. * * This function is used on the Tx path when the skb headroom is not large * enough for the HW requirements, thus instead of realloc-ing the skb we * create a SG frame descriptor with only one entry.
*/ staticint dpaa2_eth_build_sg_fd_single_buf(struct dpaa2_eth_priv *priv, struct sk_buff *skb, struct dpaa2_fd *fd, void **swa_addr)
{ struct device *dev = priv->net_dev->dev.parent; struct dpaa2_sg_entry *sgt; struct dpaa2_eth_swa *swa;
dma_addr_t addr, sgt_addr; void *sgt_buf = NULL; int sgt_buf_size; int err;
/* Store a backpointer to the skb at the beginning of the buffer * (in the private data area) such that we can release it * on Tx confirm
*/
*swa_addr = (void *)buffer_start;
swa = (struct dpaa2_eth_swa *)buffer_start;
swa->type = DPAA2_ETH_SWA_SINGLE;
swa->single.skb = skb;
/* FD freeing routine on the Tx path * * DMA-unmap and free FD and possibly SGT buffer allocated on Tx. The skb * back-pointed to is also freed. * This can be called either from dpaa2_eth_tx_conf() or on the error path of * dpaa2_eth_tx().
*/ void dpaa2_eth_free_tx_fd(struct dpaa2_eth_priv *priv, struct dpaa2_eth_channel *ch, struct dpaa2_eth_fq *fq, conststruct dpaa2_fd *fd, bool in_napi)
{ struct device *dev = priv->net_dev->dev.parent;
dma_addr_t fd_addr, sg_addr; struct sk_buff *skb = NULL; unsignedchar *buffer_start; struct dpaa2_eth_swa *swa;
u8 fd_format = dpaa2_fd_get_format(fd);
u32 fd_len = dpaa2_fd_get_len(fd); struct dpaa2_sg_entry *sgt; int should_free_skb = 1; void *tso_hdr; int i;
/* Unmap the SGT buffer */
dma_unmap_single(dev, fd_addr, swa->tso.sgt_size,
DMA_BIDIRECTIONAL);
/* Unmap and free the header */
tso_hdr = dpaa2_iova_to_virt(priv->iommu_domain, dpaa2_sg_get_addr(sgt));
dma_unmap_single(dev, dpaa2_sg_get_addr(sgt), TSO_HEADER_SIZE,
DMA_TO_DEVICE);
kfree(tso_hdr);
/* Unmap the other SG entries for the data */ for (i = 1; i < swa->tso.num_sg; i++)
dma_unmap_single(dev, dpaa2_sg_get_addr(&sgt[i]),
dpaa2_sg_get_len(&sgt[i]), DMA_TO_DEVICE);
/* Free SGT buffer allocated on tx */ if (fd_format != dpaa2_fd_single)
dpaa2_eth_sgt_recycle(priv, buffer_start);
/* Move on with skb release. If we are just confirming multiple FDs * from the same TSO skb then only the last one will need to free the * skb.
*/ if (should_free_skb)
napi_consume_skb(skb, in_napi);
}
*total_fds_len += fd_len; /* Advance to the next frame descriptor */
fd++;
index++;
}
*num_fds = index;
return 0;
err_map_sgt:
err_map_data: /* Unmap all the data S/G entries for the current FD */
sgt = (struct dpaa2_sg_entry *)(sgt_buf + priv->tx_data_offset); for (i = 1; i < num_sge; i++)
dma_unmap_single(dev, dpaa2_sg_get_addr(&sgt[i]),
dpaa2_sg_get_len(&sgt[i]), DMA_TO_DEVICE);
/* Unmap the header entry */
dma_unmap_single(dev, tso_hdr_dma, TSO_HEADER_SIZE, DMA_TO_DEVICE);
err_map_tso_hdr:
kfree(tso_hdr);
err_alloc_tso_hdr:
dpaa2_eth_sgt_recycle(priv, sgt_buf);
err_sgt_get: /* Free all the other FDs that were already fully created */ for (i = 0; i < index; i++)
dpaa2_eth_free_tx_fd(priv, NULL, NULL, &fd_start[i], false);
/* We'll be holding a back-reference to the skb until Tx Confirmation; * we don't want that overwritten by a concurrent Tx with a cloned skb.
*/
skb = skb_unshare(skb, GFP_ATOMIC); if (unlikely(!skb)) { /* skb_unshare() has already freed the skb */
percpu_stats->tx_dropped++; return NETDEV_TX_OK;
}
if (unlikely(err)) {
percpu_stats->tx_dropped++; goto err_build_fd;
}
if (swa && skb->cb[0])
dpaa2_eth_enable_tx_tstamp(priv, fd, swa, skb);
/* Tracing point */ for (i = 0; i < num_fds; i++)
trace_dpaa2_tx_fd(net_dev, &fd[i]);
/* TxConf FQ selection relies on queue id from the stack. * In case of a forwarded frame from another DPNI interface, we choose * a queue affined to the same core that processed the Rx frame
*/
queue_mapping = skb_get_queue_mapping(skb);
if (net_dev->num_tc) {
prio = netdev_txq_to_tc(net_dev, queue_mapping); /* Hardware interprets priority level 0 as being the highest, * so we need to do a reverse mapping to the netdev tc index
*/
prio = net_dev->num_tc - prio - 1; /* We have only one FQ array entry for all Tx hardware queues * with the same flow id (but different priority levels)
*/
queue_mapping %= dpaa2_eth_queue_count(priv);
}
fq = &priv->fq[queue_mapping];
nq = netdev_get_tx_queue(net_dev, queue_mapping);
netdev_tx_sent_queue(nq, fd_len);
/* Everything that happens after this enqueues might race with * the Tx confirmation callback for this frame
*/
max_retries = num_fds * DPAA2_ETH_ENQUEUE_RETRIES; while (total_enqueued < num_fds && retries < max_retries) {
err = priv->enqueue(priv, fq, &fd[total_enqueued],
prio, num_fds - total_enqueued, &enqueued); if (err == -EBUSY) {
retries++; continue;
}
while (true) {
skb = skb_dequeue(&priv->tx_skbs); if (!skb) return;
/* Lock just before TX one-step timestamping packet, * and release the lock in dpaa2_eth_free_tx_fd when * confirm the packet has been sent on hardware, or * when clean up during transmit failure.
*/
mutex_lock(&priv->onestep_tstamp_lock);
__dpaa2_eth_tx(skb, priv->net_dev);
}
}
/* Perform a single release command to add buffers * to the specified buffer pool
*/ staticint dpaa2_eth_add_bufs(struct dpaa2_eth_priv *priv, struct dpaa2_eth_channel *ch)
{ struct xdp_buff *xdp_buffs[DPAA2_ETH_BUFS_PER_CMD]; struct device *dev = priv->net_dev->dev.parent;
u64 buf_array[DPAA2_ETH_BUFS_PER_CMD]; struct dpaa2_eth_swa *swa; struct page *page;
dma_addr_t addr; int retries = 0; int i = 0, err;
u32 batch;
/* Allocate buffers visible to WRIOP */ if (!ch->xsk_zc) { for (i = 0; i < DPAA2_ETH_BUFS_PER_CMD; i++) { /* Also allocate skb shared info and alignment padding. * There is one page for each Rx buffer. WRIOP sees * the entire page except for a tailroom reserved for * skb shared info
*/
page = dev_alloc_pages(0); if (!page) goto err_alloc;
/* tracing point */
trace_dpaa2_eth_buf_seed(priv->net_dev,
page_address(page),
DPAA2_ETH_RX_BUF_RAW_SIZE,
addr, priv->rx_buf_size,
ch->bp->bpid);
}
} elseif (xsk_buff_can_alloc(ch->xsk_pool, DPAA2_ETH_BUFS_PER_CMD)) { /* Allocate XSK buffers for AF_XDP fast path in batches * of DPAA2_ETH_BUFS_PER_CMD. Bail out if the UMEM cannot * provide enough buffers at the moment
*/
batch = xsk_buff_alloc_batch(ch->xsk_pool, xdp_buffs,
DPAA2_ETH_BUFS_PER_CMD); if (!batch) goto err_alloc;
for (i = 0; i < batch; i++) {
swa = (struct dpaa2_eth_swa *)(xdp_buffs[i]->data_hard_start +
DPAA2_ETH_RX_HWA_SIZE);
swa->xsk.xdp_buff = xdp_buffs[i];
addr = xsk_buff_xdp_get_frame_dma(xdp_buffs[i]); if (unlikely(dma_mapping_error(dev, addr))) goto err_map;
release_bufs: /* In case the portal is busy, retry until successful */ while ((err = dpaa2_io_service_release(ch->dpio, ch->bp->bpid,
buf_array, i)) == -EBUSY) { if (retries++ >= DPAA2_ETH_SWP_BUSY_RETRIES) break;
cpu_relax();
}
/* If release command failed, clean up and bail out; * not much else we can do about it
*/ if (err) {
dpaa2_eth_free_bufs(priv, buf_array, i, ch->xsk_zc); return 0;
}
return i;
err_map: if (!ch->xsk_zc) {
__free_pages(page, 0);
} else { for (; i < batch; i++)
xsk_buff_free(xdp_buffs[i]);
}
err_alloc: /* If we managed to allocate at least some buffers, * release them to hardware
*/ if (i) goto release_bufs;
return 0;
}
staticint dpaa2_eth_seed_pool(struct dpaa2_eth_priv *priv, struct dpaa2_eth_channel *ch)
{ int i; int new_count;
for (i = 0; i < DPAA2_ETH_NUM_BUFS; i += DPAA2_ETH_BUFS_PER_CMD) {
new_count = dpaa2_eth_add_bufs(priv, ch);
ch->buf_count += new_count;
if (new_count < DPAA2_ETH_BUFS_PER_CMD) return -ENOMEM;
}
return 0;
}
staticvoid dpaa2_eth_seed_pools(struct dpaa2_eth_priv *priv)
{ struct net_device *net_dev = priv->net_dev; struct dpaa2_eth_channel *channel; int i, err = 0;
for (i = 0; i < priv->num_channels; i++) {
channel = priv->channel[i];
err = dpaa2_eth_seed_pool(priv, channel);
/* Not much to do; the buffer pool, though not filled up, * may still contain some buffers which would enable us * to limp on.
*/ if (err)
netdev_err(net_dev, "Buffer seeding failed for DPBP %d (bpid=%d)\n",
channel->bp->dev->obj_desc.id,
channel->bp->bpid);
}
}
/* * Drain the specified number of buffers from one of the DPNI's private buffer * pools. * @count must not exceeed DPAA2_ETH_BUFS_PER_CMD
*/ staticvoid dpaa2_eth_drain_bufs(struct dpaa2_eth_priv *priv, int bpid, int count)
{
u64 buf_array[DPAA2_ETH_BUFS_PER_CMD]; bool xsk_zc = false; int retries = 0; int i, ret;
for (i = 0; i < priv->num_channels; i++) if (priv->channel[i]->bp->bpid == bpid)
xsk_zc = priv->channel[i]->xsk_zc;
do {
ret = dpaa2_io_service_acquire(NULL, bpid, buf_array, count); if (ret < 0) { if (ret == -EBUSY &&
retries++ < DPAA2_ETH_SWP_BUSY_RETRIES) continue;
netdev_err(priv->net_dev, "dpaa2_io_service_acquire() failed\n"); return;
}
dpaa2_eth_free_bufs(priv, buf_array, ret, xsk_zc);
retries = 0;
} while (ret);
}
staticvoid dpaa2_eth_drain_pool(struct dpaa2_eth_priv *priv, int bpid)
{ int i;
/* Drain the buffer pool */
dpaa2_eth_drain_bufs(priv, bpid, DPAA2_ETH_BUFS_PER_CMD);
dpaa2_eth_drain_bufs(priv, bpid, 1);
/* Setup to zero the buffer count of all channels which were * using this buffer pool.
*/ for (i = 0; i < priv->num_channels; i++) if (priv->channel[i]->bp->bpid == bpid)
priv->channel[i]->buf_count = 0;
}
staticvoid dpaa2_eth_drain_pools(struct dpaa2_eth_priv *priv)
{ int i;
for (i = 0; i < priv->num_bps; i++)
dpaa2_eth_drain_pool(priv, priv->bp[i]->bpid);
}
/* Function is called from softirq context only, so we don't need to guard * the access to percpu count
*/ staticint dpaa2_eth_refill_pool(struct dpaa2_eth_priv *priv, struct dpaa2_eth_channel *ch)
{ int new_count;
if (likely(ch->buf_count >= DPAA2_ETH_REFILL_THRESH)) return 0;
do {
new_count = dpaa2_eth_add_bufs(priv, ch); if (unlikely(!new_count)) { /* Out of memory; abort for now, we'll try later on */ break;
}
ch->buf_count += new_count;
} while (ch->buf_count < DPAA2_ETH_NUM_BUFS);
if (unlikely(ch->buf_count < DPAA2_ETH_NUM_BUFS)) return -ENOMEM;
for (i = 0; i < count; i++)
skb_free_frag(sgt_cache->buf[i]);
sgt_cache->count = 0;
}
}
staticint dpaa2_eth_pull_channel(struct dpaa2_eth_channel *ch)
{ int err; int dequeues = -1;
/* Retry while portal is busy */ do {
err = dpaa2_io_service_pull_channel(ch->dpio, ch->ch_id,
ch->store);
dequeues++;
cpu_relax();
} while (err == -EBUSY && dequeues < DPAA2_ETH_SWP_BUSY_RETRIES);
ch->stats.dequeue_portal_busy += dequeues; if (unlikely(err))
ch->stats.pull_err++;
return err;
}
/* NAPI poll routine * * Frames are dequeued from the QMan channel associated with this NAPI context. * Rx, Tx confirmation and (if configured) Rx error frames all count * towards the NAPI budget.
*/ staticint dpaa2_eth_poll(struct napi_struct *napi, int budget)
{ struct dpaa2_eth_channel *ch; struct dpaa2_eth_priv *priv; int rx_cleaned = 0, txconf_cleaned = 0; struct dpaa2_eth_fq *fq, *txc_fq = NULL; struct netdev_queue *nq; int store_cleaned, work_done; bool work_done_zc = false; struct list_head rx_list; int retries = 0;
u16 flowid; int err;
if (ch->xsk_zc) {
work_done_zc = dpaa2_xsk_tx(priv, ch); /* If we reached the XSK Tx per NAPI threshold, we're done */ if (work_done_zc) {
work_done = budget; goto out;
}
}
do {
err = dpaa2_eth_pull_channel(ch); if (unlikely(err)) break;
/* Refill pool if appropriate */
dpaa2_eth_refill_pool(priv, ch);
store_cleaned = dpaa2_eth_consume_frames(ch, &fq); if (store_cleaned <= 0) break; if (fq->type == DPAA2_RX_FQ) {
rx_cleaned += store_cleaned;
flowid = fq->flowid;
} else {
txconf_cleaned += store_cleaned; /* We have a single Tx conf FQ on this channel */
txc_fq = fq;
}
/* If we either consumed the whole NAPI budget with Rx frames * or we reached the Tx confirmations threshold, we're done.
*/ if (rx_cleaned >= budget ||
txconf_cleaned >= DPAA2_ETH_TXCONF_PER_NAPI) {
work_done = budget; if (ch->xdp.res & XDP_REDIRECT)
xdp_do_flush(); goto out;
}
} while (store_cleaned);
if (ch->xdp.res & XDP_REDIRECT)
xdp_do_flush();
/* Update NET DIM with the values for this CDAN */
dpaa2_io_update_net_dim(ch->dpio, ch->stats.frames_per_cdan,
ch->stats.bytes_per_cdan);
ch->stats.frames_per_cdan = 0;
ch->stats.bytes_per_cdan = 0;
/* We didn't consume the entire budget, so finish napi and * re-enable data availability notifications
*/
napi_complete_done(napi, rx_cleaned); do {
err = dpaa2_io_service_rearm(ch->dpio, &ch->nctx);
cpu_relax();
} while (err == -EBUSY && retries++ < DPAA2_ETH_SWP_BUSY_RETRIES);
WARN_ONCE(err, "CDAN notifications rearm failed on core %d",
ch->nctx.desired_cpu);
work_done = max(rx_cleaned, 1);
out:
netif_receive_skb_list(ch->rx_list);
if (ch->xsk_tx_pkts_sent) {
xsk_tx_completed(ch->xsk_pool, ch->xsk_tx_pkts_sent);
ch->xsk_tx_pkts_sent = 0;
}
if (rx_cleaned && ch->xdp.res & XDP_TX)
dpaa2_eth_xdp_tx_flush(priv, ch, &priv->fq[flowid]);
return work_done;
}
staticvoid dpaa2_eth_enable_ch_napi(struct dpaa2_eth_priv *priv)
{ struct dpaa2_eth_channel *ch; int i;
for (i = 0; i < priv->num_channels; i++) {
ch = priv->channel[i];
napi_enable(&ch->napi);
}
}
staticvoid dpaa2_eth_disable_ch_napi(struct dpaa2_eth_priv *priv)
{ struct dpaa2_eth_channel *ch; int i;
for (i = 0; i < priv->num_channels; i++) {
ch = priv->channel[i];
napi_disable(&ch->napi);
}
}
void dpaa2_eth_set_rx_taildrop(struct dpaa2_eth_priv *priv, bool tx_pause, bool pfc)
{ struct dpni_taildrop td = {0}; struct dpaa2_eth_fq *fq; int i, err;
/* FQ taildrop: threshold is in bytes, per frame queue. Enabled if * flow control is disabled (as it might interfere with either the * buffer pool depletion trigger for pause frames or with the group * congestion trigger for PFC frames)
*/
td.enable = !tx_pause; if (priv->rx_fqtd_enabled == td.enable) goto set_cgtd;
for (i = 0; i < priv->num_fqs; i++) {
fq = &priv->fq[i]; if (fq->type != DPAA2_RX_FQ) continue;
err = dpni_set_taildrop(priv->mc_io, 0, priv->mc_token,
DPNI_CP_QUEUE, DPNI_QUEUE_RX,
fq->tc, fq->flowid, &td); if (err) {
netdev_err(priv->net_dev, "dpni_set_taildrop(FQ) failed\n"); return;
}
}
priv->rx_fqtd_enabled = td.enable;
set_cgtd: /* Congestion group taildrop: threshold is in frames, per group * of FQs belonging to the same traffic class * Enabled if general Tx pause disabled or if PFCs are enabled * (congestion group threhsold for PFC generation is lower than the * CG taildrop threshold, so it won't interfere with it; we also * want frames in non-PFC enabled traffic classes to be kept in check)
*/
td.enable = !tx_pause || pfc; if (priv->rx_cgtd_enabled == td.enable) return;
td.threshold = DPAA2_ETH_CG_TAILDROP_THRESH(priv);
td.units = DPNI_CONGESTION_UNIT_FRAMES; for (i = 0; i < dpaa2_eth_tc_count(priv); i++) {
err = dpni_set_taildrop(priv->mc_io, 0, priv->mc_token,
DPNI_CP_GROUP, DPNI_QUEUE_RX,
i, 0, &td); if (err) {
netdev_err(priv->net_dev, "dpni_set_taildrop(CG) failed\n"); return;
}
}
priv->rx_cgtd_enabled = td.enable;
}
staticint dpaa2_eth_link_state_update(struct dpaa2_eth_priv *priv)
{ struct dpni_link_state state = {0}; bool tx_pause; int err;
/* If Tx pause frame settings have changed, we need to update * Rx FQ taildrop configuration as well. We configure taildrop * only when pause frame generation is disabled.
*/
tx_pause = dpaa2_eth_tx_pause_enabled(state.options);
dpaa2_eth_set_rx_taildrop(priv, tx_pause, priv->pfc_enabled);
/* When we manage the MAC/PHY using phylink there is no need * to manually update the netif_carrier. * We can avoid locking because we are called from the "link changed" * IRQ handler, which is the same as the "endpoint changed" IRQ handler * (the writer to priv->mac), so we cannot race with it.
*/ if (dpaa2_mac_is_type_phy(priv->mac)) goto out;
/* Chech link state; speed / duplex changes are not treated yet */ if (priv->link_state.up == state.up) goto out;
if (!dpaa2_eth_is_type_phy(priv)) { /* We'll only start the txqs when the link is actually ready; * make sure we don't race against the link up notification, * which may come immediately after dpni_enable();
*/
netif_tx_stop_all_queues(net_dev);
/* Also, explicitly set carrier off, otherwise * netif_carrier_ok() will return true and cause 'ip link show' * to report the LOWER_UP flag, even though the link * notification wasn't even received.
*/
netif_carrier_off(net_dev);
}
dpaa2_eth_enable_ch_napi(priv);
/* Total number of in-flight frames on ingress queues */ static u32 dpaa2_eth_ingress_fq_count(struct dpaa2_eth_priv *priv)
{ struct dpaa2_eth_fq *fq;
u32 fcnt = 0, bcnt = 0, total = 0; int i, err;
for (i = 0; i < priv->num_fqs; i++) {
fq = &priv->fq[i];
err = dpaa2_io_query_fq_count(NULL, fq->fqid, &fcnt, &bcnt); if (err) {
netdev_warn(priv->net_dev, "query_fq_count failed"); break;
}
total += fcnt;
}
do {
pending = dpaa2_eth_ingress_fq_count(priv); if (pending)
msleep(100);
} while (pending && --retries);
}
#define DPNI_TX_PENDING_VER_MAJOR 7 #define DPNI_TX_PENDING_VER_MINOR 13 staticvoid dpaa2_eth_wait_for_egress_fq_empty(struct dpaa2_eth_priv *priv)
{ union dpni_statistics stats; int retries = 10; int err;
if (dpaa2_eth_cmp_dpni_ver(priv, DPNI_TX_PENDING_VER_MAJOR,
DPNI_TX_PENDING_VER_MINOR) < 0) goto out;
do {
err = dpni_get_statistics(priv->mc_io, 0, priv->mc_token, 6,
&stats); if (err) goto out; if (stats.page_6.tx_pending_frames == 0) return;
} while (--retries);
out:
msleep(500);
}
staticint dpaa2_eth_stop(struct net_device *net_dev)
{ struct dpaa2_eth_priv *priv = netdev_priv(net_dev); int dpni_enabled = 0; int retries = 10;
mutex_lock(&priv->mac_lock);
if (dpaa2_eth_is_type_phy(priv)) {
dpaa2_mac_stop(priv->mac);
} else {
netif_tx_stop_all_queues(net_dev);
netif_carrier_off(net_dev);
}
mutex_unlock(&priv->mac_lock);
/* On dpni_disable(), the MC firmware will: * - stop MAC Rx and wait for all Rx frames to be enqueued to software * - cut off WRIOP dequeues from egress FQs and wait until transmission * of all in flight Tx frames is finished (and corresponding Tx conf * frames are enqueued back to software) * * Before calling dpni_disable(), we wait for all Tx frames to arrive * on WRIOP. After it finishes, wait until all remaining frames on Rx * and Tx conf queues are consumed on NAPI poll.
*/
dpaa2_eth_wait_for_egress_fq_empty(priv);
do {
dpni_disable(priv->mc_io, 0, priv->mc_token);
dpni_is_enabled(priv->mc_io, 0, priv->mc_token, &dpni_enabled); if (dpni_enabled) /* Allow the hardware some slack */
msleep(100);
} while (dpni_enabled && --retries); if (!retries) {
netdev_warn(net_dev, "Retry count exceeded disabling DPNI\n"); /* Must go on and disable NAPI nonetheless, so we don't crash at * the next "ifconfig up"
*/
}
/** Fill in counters maintained by the GPP driver. These may be different from * the hardware counters obtained by ethtool.
*/ staticvoid dpaa2_eth_get_stats(struct net_device *net_dev, struct rtnl_link_stats64 *stats)
{ struct dpaa2_eth_priv *priv = netdev_priv(net_dev); struct rtnl_link_stats64 *percpu_stats;
u64 *cpustats;
u64 *netstats = (u64 *)stats; int i, j; int num = sizeof(struct rtnl_link_stats64) / sizeof(u64);
/* Copy mac unicast addresses from @net_dev to @priv. * Its sole purpose is to make dpaa2_eth_set_rx_mode() more readable.
*/ staticvoid dpaa2_eth_add_uc_hw_addr(conststruct net_device *net_dev, struct dpaa2_eth_priv *priv)
{ struct netdev_hw_addr *ha; int err;
netdev_for_each_uc_addr(ha, net_dev) {
err = dpni_add_mac_addr(priv->mc_io, 0, priv->mc_token,
ha->addr); if (err)
netdev_warn(priv->net_dev, "Could not add ucast MAC %pM to the filtering table (err %d)\n",
ha->addr, err);
}
}
/* Copy mac multicast addresses from @net_dev to @priv * Its sole purpose is to make dpaa2_eth_set_rx_mode() more readable.
*/ staticvoid dpaa2_eth_add_mc_hw_addr(conststruct net_device *net_dev, struct dpaa2_eth_priv *priv)
{ struct netdev_hw_addr *ha; int err;
netdev_for_each_mc_addr(ha, net_dev) {
err = dpni_add_mac_addr(priv->mc_io, 0, priv->mc_token,
ha->addr); if (err)
netdev_warn(priv->net_dev, "Could not add mcast MAC %pM to the filtering table (err %d)\n",
ha->addr, err);
}
}
/* Basic sanity checks; these probably indicate a misconfiguration */ if (options & DPNI_OPT_NO_MAC_FILTER && max_mac != 0)
netdev_info(net_dev, "mac_filter_entries=%d, DPNI_OPT_NO_MAC_FILTER option must be disabled\n",
max_mac);
/* Force promiscuous if the uc or mc counts exceed our capabilities. */ if (uc_count > max_mac) {
netdev_info(net_dev, "Unicast addr count reached %d, max allowed is %d; forcing promisc\n",
uc_count, max_mac); goto force_promisc;
} if (mc_count + uc_count > max_mac) {
netdev_info(net_dev, "Unicast + multicast addr count reached %d, max allowed is %d; forcing promisc\n",
uc_count + mc_count, max_mac); goto force_mc_promisc;
}
/* Adjust promisc settings due to flag combinations */ if (net_dev->flags & IFF_PROMISC) goto force_promisc; if (net_dev->flags & IFF_ALLMULTI) { /* First, rebuild unicast filtering table. This should be done * in promisc mode, in order to avoid frame loss while we * progressively add entries to the table. * We don't know whether we had been in promisc already, and * making an MC call to find out is expensive; so set uc promisc * nonetheless.
*/
err = dpni_set_unicast_promisc(mc_io, 0, mc_token, 1); if (err)
netdev_warn(net_dev, "Can't set uc promisc\n");
/* Finally, clear uc promisc and set mc promisc as requested. */
err = dpni_set_unicast_promisc(mc_io, 0, mc_token, 0); if (err)
netdev_warn(net_dev, "Can't clear uc promisc\n"); goto force_mc_promisc;
}
/* Neither unicast, nor multicast promisc will be on... eventually. * For now, rebuild mac filtering tables while forcing both of them on.
*/
err = dpni_set_unicast_promisc(mc_io, 0, mc_token, 1); if (err)
netdev_warn(net_dev, "Can't set uc promisc (%d)\n", err);
err = dpni_set_multicast_promisc(mc_io, 0, mc_token, 1); if (err)
netdev_warn(net_dev, "Can't set mc promisc (%d)\n", err);
/* Actual mac filtering tables reconstruction */
err = dpni_clear_mac_filters(mc_io, 0, mc_token, 1, 1); if (err)
netdev_warn(net_dev, "Can't clear mac filters\n");
dpaa2_eth_add_mc_hw_addr(net_dev, priv);
dpaa2_eth_add_uc_hw_addr(net_dev, priv);
/* Now we can clear both ucast and mcast promisc, without risking * to drop legitimate frames anymore.
*/
err = dpni_set_unicast_promisc(mc_io, 0, mc_token, 0); if (err)
netdev_warn(net_dev, "Can't clear ucast promisc\n");
err = dpni_set_multicast_promisc(mc_io, 0, mc_token, 0); if (err)
netdev_warn(net_dev, "Can't clear mcast promisc\n");
return;
force_promisc:
err = dpni_set_unicast_promisc(mc_io, 0, mc_token, 1); if (err)
netdev_warn(net_dev, "Can't set ucast promisc\n");
force_mc_promisc:
err = dpni_set_multicast_promisc(mc_io, 0, mc_token, 1); if (err)
netdev_warn(net_dev, "Can't set mcast promisc\n");
}
switch (config->tx_type) { case HWTSTAMP_TX_OFF: case HWTSTAMP_TX_ON: case HWTSTAMP_TX_ONESTEP_SYNC:
priv->tx_tstamp_type = config->tx_type; break; default: return -ERANGE;
}
if (config->rx_filter == HWTSTAMP_FILTER_NONE) {
priv->rx_tstamp = false;
} else {
priv->rx_tstamp = true; /* TS is set for all frame types, not only those requested */
config->rx_filter = HWTSTAMP_FILTER_ALL;
}
if (priv->tx_tstamp_type == HWTSTAMP_TX_ONESTEP_SYNC)
dpaa2_ptp_onestep_reg_update_method(priv);
if (mfl > linear_mfl) {
netdev_warn(priv->net_dev, "Maximum MTU for XDP is %d\n",
linear_mfl - VLAN_ETH_HLEN); returnfalse;
}
returntrue;
}
staticint dpaa2_eth_set_rx_mfl(struct dpaa2_eth_priv *priv, int mtu, bool has_xdp)
{ int mfl, err;
/* We enforce a maximum Rx frame length based on MTU only if we have * an XDP program attached (in order to avoid Rx S/G frames). * Otherwise, we accept all incoming frames as long as they are not * larger than maximum size supported in hardware
*/ if (has_xdp)
mfl = DPAA2_ETH_L2_MAX_FRM(mtu); else
mfl = DPAA2_ETH_MFL;
if (prog && !xdp_mtu_valid(priv, dev->mtu)) return -EINVAL;
if (prog)
bpf_prog_add(prog, priv->num_channels);
up = netif_running(dev);
need_update = (!!priv->xdp_prog != !!prog);
if (up)
dev_close(dev);
/* While in xdp mode, enforce a maximum Rx frame size based on MTU. * Also, when switching between xdp/non-xdp modes we need to reconfigure * our Rx buffer layout. Buffer pool was drained on dpaa2_eth_stop, * so we are sure no old format buffers will be used from now on.
*/ if (need_update) {
err = dpaa2_eth_set_rx_mfl(priv, dev->mtu, !!prog); if (err) goto out_err;
err = dpaa2_eth_update_rx_buffer_headroom(priv, !!prog); if (err) goto out_err;
}
old = xchg(&priv->xdp_prog, prog); if (old)
bpf_prog_put(old);
for (i = 0; i < priv->num_channels; i++) {
ch = priv->channel[i];
old = xchg(&ch->xdp.prog, prog); if (old)
bpf_prog_put(old);
}
if (up) {
err = dev_open(dev, NULL); if (err) return err;
}
return 0;
out_err: if (prog)
bpf_prog_sub(prog, priv->num_channels); if (up)
dev_open(dev, NULL);
/* We require a minimum headroom to be able to transmit the frame. * Otherwise return an error and let the original net_device handle it
*/
needed_headroom = dpaa2_eth_needed_headroom(NULL); if (xdpf->headroom < needed_headroom) return -EINVAL;
/* Setup the FD fields */
memset(fd, 0, sizeof(*fd));
/* Align FD address, if possible */
buffer_start = xdpf->data - needed_headroom;
aligned_start = PTR_ALIGN(buffer_start - DPAA2_ETH_TX_BUF_ALIGN,
DPAA2_ETH_TX_BUF_ALIGN); if (aligned_start >= xdpf->data - xdpf->headroom)
buffer_start = aligned_start;
swa = (struct dpaa2_eth_swa *)buffer_start; /* fill in necessary fields here */
swa->type = DPAA2_ETH_SWA_XDP;
swa->xdp.dma_size = xdpf->data + xdpf->len - buffer_start;
swa->xdp.xdpf = xdpf;
/* create a FD for each xdp_frame in the list received */ for (i = 0; i < n; i++) {
err = dpaa2_eth_xdp_create_fd(net_dev, frames[i], &fds[i]); if (err) break;
}
xdp_redirect_fds->num = i;
/* enqueue all the frame descriptors */
enqueued = dpaa2_eth_xdp_flush(priv, fq, xdp_redirect_fds);
/* update statistics */
percpu_stats->tx_packets += enqueued; for (i = 0; i < enqueued; i++)
percpu_stats->tx_bytes += dpaa2_fd_get_len(&fds[i]);
return enqueued;
}
staticint update_xps(struct dpaa2_eth_priv *priv)
{ struct net_device *net_dev = priv->net_dev; int i, num_queues, netdev_queues; struct dpaa2_eth_fq *fq;
cpumask_var_t xps_mask; int err = 0;
if (!alloc_cpumask_var(&xps_mask, GFP_KERNEL)) return -ENOMEM;
/* The first <num_queues> entries in priv->fq array are Tx/Tx conf * queues, so only process those
*/ for (i = 0; i < netdev_queues; i++) {
fq = &priv->fq[i % num_queues];
if (p->command == TC_TBF_STATS) return -EOPNOTSUPP;
/* Only per port Tx shaping */ if (p->parent != TC_H_ROOT) return -EOPNOTSUPP;
if (p->command == TC_TBF_REPLACE) { if (cfg->max_size > DPAA2_ETH_MAX_BURST_SIZE) {
netdev_err(net_dev, "burst size cannot be greater than %d\n",
DPAA2_ETH_MAX_BURST_SIZE); return -EINVAL;
}
tx_cr_shaper.max_burst_size = cfg->max_size; /* The TBF interface is in bytes/s, whereas DPAA2 expects the * rate in Mbits/s
*/
tx_cr_shaper.rate_limit = bps_to_mbits(cfg->rate.rate_bytes_ps);
}
/* NAPI can also be scheduled from the AF_XDP Tx path. Mark a missed * so that it can be rescheduled again.
*/ if (!napi_if_scheduled_mark_missed(&ch->napi))
napi_schedule(&ch->napi);
}
/* Allocate and configure a DPCON object */ staticstruct fsl_mc_device *dpaa2_eth_setup_dpcon(struct dpaa2_eth_priv *priv)
{ struct fsl_mc_device *dpcon; struct device *dev = priv->net_dev->dev.parent; int err;
err = fsl_mc_object_allocate(to_fsl_mc_device(dev),
FSL_MC_POOL_DPCON, &dpcon); if (err) { if (err == -ENXIO) {
dev_dbg(dev, "Waiting for DPCON\n");
err = -EPROBE_DEFER;
} else {
dev_info(dev, "Not enough DPCONs, will go on as-is\n");
} return ERR_PTR(err);
}
/* DPIO setup: allocate and configure QBMan channels, setup core affinity * and register data availability notifications
*/ staticint dpaa2_eth_setup_dpio(struct dpaa2_eth_priv *priv)
{ struct dpaa2_io_notification_ctx *nctx; struct dpaa2_eth_channel *channel; struct dpcon_notification_cfg dpcon_notif_cfg; struct device *dev = priv->net_dev->dev.parent; int i, err;
/* We want the ability to spread ingress traffic (RX, TX conf) to as * many cores as possible, so we need one channel for each core * (unless there's fewer queues than cores, in which case the extra * channels would be wasted). * Allocate one channel per core and register it to the core's * affine DPIO. If not enough channels are available for all cores * or if some cores don't have an affine DPIO, there will be no * ingress frame processing on those cores.
*/
cpumask_clear(&priv->dpio_cpumask);
for_each_online_cpu(i) { /* Try to allocate a channel */
channel = dpaa2_eth_alloc_channel(priv); if (IS_ERR_OR_NULL(channel)) {
err = PTR_ERR_OR_ZERO(channel); if (err == -EPROBE_DEFER)
dev_dbg(dev, "waiting for affine channel\n"); else
dev_info(dev, "No affine channel for cpu %d and above\n", i); goto err_alloc_ch;
}
/* Register the new context */
channel->dpio = dpaa2_io_service_select(i);
err = dpaa2_io_service_register(channel->dpio, nctx, dev); if (err) {
dev_dbg(dev, "No affine DPIO for cpu %d\n", i); /* If no affine DPIO for this core, there's probably * none available for next cores either. Signal we want * to retry later, in case the DPIO devices weren't * probed yet.
*/
err = -EPROBE_DEFER; goto err_service_reg;
}
/* If we managed to allocate a channel and also found an affine * DPIO for this core, add it to the final mask
*/
cpumask_set_cpu(i, &priv->dpio_cpumask);
priv->num_channels++;
/* Stop if we already have enough channels to accommodate all * RX and TX conf queues
*/ if (priv->num_channels == priv->dpni_attrs.num_queues) break;
}
return 0;
err_set_cdan:
dpaa2_io_service_deregister(channel->dpio, nctx, dev);
err_service_reg:
dpaa2_eth_free_channel(priv, channel);
err_alloc_ch: if (err == -EPROBE_DEFER) { for (i = 0; i < priv->num_channels; i++) {
channel = priv->channel[i];
nctx = &channel->nctx;
dpaa2_io_service_deregister(channel->dpio, nctx, dev);
dpaa2_eth_free_channel(priv, channel);
}
priv->num_channels = 0; return err;
}
if (cpumask_empty(&priv->dpio_cpumask)) {
dev_err(dev, "No cpu with an affine DPIO/DPCON\n"); return -ENODEV;
}
dev_info(dev, "Cores %*pbl available for processing ingress traffic\n",
cpumask_pr_args(&priv->dpio_cpumask));
/* deregister CDAN notifications and free channels */ for (i = 0; i < priv->num_channels; i++) {
ch = priv->channel[i];
dpaa2_io_service_deregister(ch->dpio, &ch->nctx, dev);
dpaa2_eth_free_channel(priv, ch);
}
}
staticstruct dpaa2_eth_channel *dpaa2_eth_get_affine_channel(struct dpaa2_eth_priv *priv, int cpu)
{ struct device *dev = priv->net_dev->dev.parent; int i;
for (i = 0; i < priv->num_channels; i++) if (priv->channel[i]->nctx.desired_cpu == cpu) return priv->channel[i];
/* We should never get here. Issue a warning and return * the first channel, because it's still better than nothing
*/
dev_warn(dev, "No affine channel found for cpu %d\n", cpu);
return priv->channel[0];
}
staticvoid dpaa2_eth_set_fq_affinity(struct dpaa2_eth_priv *priv)
{ struct device *dev = priv->net_dev->dev.parent; struct dpaa2_eth_fq *fq; int rx_cpu, txc_cpu; int i;
/* For each FQ, pick one channel/CPU to deliver frames to. * This may well change at runtime, either through irqbalance or * through direct user intervention.
*/
rx_cpu = txc_cpu = cpumask_first(&priv->dpio_cpumask);
for (i = 0; i < priv->num_fqs; i++) {
fq = &priv->fq[i]; switch (fq->type) { case DPAA2_RX_FQ: case DPAA2_RX_ERR_FQ:
fq->target_cpu = rx_cpu;
rx_cpu = cpumask_next(rx_cpu, &priv->dpio_cpumask); if (rx_cpu >= nr_cpu_ids)
rx_cpu = cpumask_first(&priv->dpio_cpumask); break; case DPAA2_TX_CONF_FQ:
fq->target_cpu = txc_cpu;
txc_cpu = cpumask_next(txc_cpu, &priv->dpio_cpumask); if (txc_cpu >= nr_cpu_ids)
txc_cpu = cpumask_first(&priv->dpio_cpumask); break; default:
dev_err(dev, "Unknown FQ type: %d\n", fq->type);
}
fq->channel = dpaa2_eth_get_affine_channel(priv, fq->target_cpu);
}
update_xps(priv);
}
staticvoid dpaa2_eth_setup_fqs(struct dpaa2_eth_priv *priv)
{ int i, j;
/* We have one TxConf FQ per Tx flow. * The number of Tx and Rx queues is the same. * Tx queues come first in the fq array.
*/ for (i = 0; i < dpaa2_eth_queue_count(priv); i++) {
priv->fq[priv->num_fqs].type = DPAA2_TX_CONF_FQ;
priv->fq[priv->num_fqs].consume = dpaa2_eth_tx_conf;
priv->fq[priv->num_fqs++].flowid = (u16)i;
}
for (j = 0; j < dpaa2_eth_tc_count(priv); j++) { for (i = 0; i < dpaa2_eth_queue_count(priv); i++) {
priv->fq[priv->num_fqs].type = DPAA2_RX_FQ;
priv->fq[priv->num_fqs].consume = dpaa2_eth_rx;
priv->fq[priv->num_fqs].tc = (u8)j;
priv->fq[priv->num_fqs++].flowid = (u16)i;
}
}
/* We have exactly one Rx error queue per DPNI */
priv->fq[priv->num_fqs].type = DPAA2_RX_ERR_FQ;
priv->fq[priv->num_fqs++].consume = dpaa2_eth_rx_err;
/* For each FQ, decide on which core to process incoming frames */
dpaa2_eth_set_fq_affinity(priv);
}
/* Allocate and configure a buffer pool */ struct dpaa2_eth_bp *dpaa2_eth_allocate_dpbp(struct dpaa2_eth_priv *priv)
{ struct device *dev = priv->net_dev->dev.parent; struct fsl_mc_device *dpbp_dev; struct dpbp_attr dpbp_attrs; struct dpaa2_eth_bp *bp; int err;
for (i = 0; i < priv->num_channels; i++)
priv->channel[i]->bp = bp;
return 0;
}
void dpaa2_eth_free_dpbp(struct dpaa2_eth_priv *priv, struct dpaa2_eth_bp *bp)
{ int idx_bp;
/* Find the index at which this BP is stored */ for (idx_bp = 0; idx_bp < priv->num_bps; idx_bp++) if (priv->bp[idx_bp] == bp) break;
/* Drain the pool and disable the associated MC object */
dpaa2_eth_drain_pool(priv, bp->bpid);
dpbp_disable(priv->mc_io, 0, bp->dev->mc_handle);
dpbp_close(priv->mc_io, 0, bp->dev->mc_handle);
fsl_mc_object_free(bp->dev);
kfree(bp);
/* Move the last in use DPBP over in this position */
priv->bp[idx_bp] = priv->bp[priv->num_bps - 1];
priv->num_bps--;
}
staticvoid dpaa2_eth_free_dpbps(struct dpaa2_eth_priv *priv)
{ int i;
for (i = 0; i < priv->num_bps; i++)
dpaa2_eth_free_dpbp(priv, priv->bp[i]);
}
/* We need to check for WRIOP version 1.0.0, but depending on the MC * version, this number is not always provided correctly on rev1. * We need to check for both alternatives in this situation.
*/ if (priv->dpni_attrs.wriop_version == DPAA2_WRIOP_VERSION(0, 0, 0) ||
priv->dpni_attrs.wriop_version == DPAA2_WRIOP_VERSION(1, 0, 0))
rx_buf_align = DPAA2_ETH_RX_BUF_ALIGN_REV1; else
rx_buf_align = DPAA2_ETH_RX_BUF_ALIGN;
/* We need to ensure that the buffer size seen by WRIOP is a multiple * of 64 or 256 bytes depending on the WRIOP version.
*/
priv->rx_buf_size = ALIGN_DOWN(DPAA2_ETH_RX_BUF_SIZE, rx_buf_align);
/* Get the default link options so we don't override other flags */
err = dpni_get_link_cfg(priv->mc_io, 0, priv->mc_token, &link_cfg); if (err) {
dev_err(dev, "dpni_get_link_cfg() failed\n"); return err;
}
/* By default, enable both Rx and Tx pause frames */
link_cfg.options |= DPNI_LINK_OPT_PAUSE;
link_cfg.options &= ~DPNI_LINK_OPT_ASYM_PAUSE;
err = dpni_set_link_cfg(priv->mc_io, 0, priv->mc_token, &link_cfg); if (err) {
dev_err(dev, "dpni_set_link_cfg() failed\n"); return err;
}
priv->link_state.options = link_cfg.options;
return 0;
}
staticvoid dpaa2_eth_update_tx_fqids(struct dpaa2_eth_priv *priv)
{ struct dpni_queue_id qid = {0}; struct dpaa2_eth_fq *fq; struct dpni_queue queue; int i, j, err;
/* We only use Tx FQIDs for FQID-based enqueue, so check * if DPNI version supports it before updating FQIDs
*/ if (dpaa2_eth_cmp_dpni_ver(priv, DPNI_ENQUEUE_FQID_VER_MAJOR,
DPNI_ENQUEUE_FQID_VER_MINOR) < 0) return;
for (i = 0; i < priv->num_fqs; i++) {
fq = &priv->fq[i]; if (fq->type != DPAA2_TX_CONF_FQ) continue; for (j = 0; j < dpaa2_eth_tc_count(priv); j++) {
err = dpni_get_queue(priv->mc_io, 0, priv->mc_token,
DPNI_QUEUE_TX, j, fq->flowid,
&queue, &qid); if (err) goto out_err;
/* Configure ingress classification based on VLAN PCP */ staticint dpaa2_eth_set_vlan_qos(struct dpaa2_eth_priv *priv)
{ struct device *dev = priv->net_dev->dev.parent; struct dpkg_profile_cfg kg_cfg = {0}; struct dpni_qos_tbl_cfg qos_cfg = {0}; struct dpni_rule_cfg key_params; void *dma_mem, *key, *mask;
u8 key_size = 2; /* VLAN TCI field */ int i, pcp, err;
/* VLAN-based classification only makes sense if we have multiple * traffic classes. * Also, we need to extract just the 3-bit PCP field from the VLAN * header and we can only do that by using a mask
*/ if (dpaa2_eth_tc_count(priv) == 1 || !dpaa2_eth_fs_mask_enabled(priv)) {
dev_dbg(dev, "VLAN-based QoS classification not supported\n"); return -EOPNOTSUPP;
}
dma_mem = kzalloc(DPAA2_CLASSIFIER_DMA_SIZE, GFP_KERNEL); if (!dma_mem) return -ENOMEM;
/* We add rules for PCP-based distribution starting with highest * priority (VLAN PCP = 7). If this DPNI doesn't have enough traffic * classes to accommodate all priority levels, the lowest ones end up * on TC 0 which was configured as default
*/ for (i = dpaa2_eth_tc_count(priv) - 1, pcp = 7; i >= 0; i--, pcp--) {
*(__be16 *)key = cpu_to_be16(pcp << VLAN_PRIO_SHIFT);
dma_sync_single_for_device(dev, key_params.key_iova,
key_size * 2, DMA_TO_DEVICE);
/* get a handle for the DPNI object */
err = dpni_open(priv->mc_io, 0, ls_dev->obj_desc.id, &priv->mc_token); if (err) {
dev_err(dev, "dpni_open() failed\n"); return err;
}
/* Check if we can work with this DPNI object */
err = dpni_get_api_version(priv->mc_io, 0, &priv->dpni_ver_major,
&priv->dpni_ver_minor); if (err) {
dev_err(dev, "dpni_get_api_version() failed\n"); goto close;
} if (dpaa2_eth_cmp_dpni_ver(priv, DPNI_VER_MAJOR, DPNI_VER_MINOR) < 0) {
dev_err(dev, "DPNI version %u.%u not supported, need >= %u.%u\n",
priv->dpni_ver_major, priv->dpni_ver_minor,
DPNI_VER_MAJOR, DPNI_VER_MINOR);
err = -EOPNOTSUPP; goto close;
}
for (i = 0; i < dpaa2_eth_tc_count(priv); i++) {
err = dpni_set_rx_tc_dist(priv->mc_io, 0, priv->mc_token,
i, &dist_cfg); if (err) {
dev_err(dev, "dpni_set_rx_tc_dist failed\n"); break;
}
}
return err;
}
/* Configure the Rx hash key using the new API */ staticint dpaa2_eth_config_hash_key(struct dpaa2_eth_priv *priv, dma_addr_t key)
{ struct device *dev = priv->net_dev->dev.parent; struct dpni_rx_dist_cfg dist_cfg; int i, err = 0;
for (i = 0; i < dpaa2_eth_tc_count(priv); i++) {
dist_cfg.tc = i;
err = dpni_set_rx_hash_dist(priv->mc_io, 0, priv->mc_token,
&dist_cfg); if (err) {
dev_err(dev, "dpni_set_rx_hash_dist failed\n"); break;
}
/* If the flow steering / hashing key is shared between all * traffic classes, install it just once
*/ if (priv->dpni_attrs.options & DPNI_OPT_SHARED_FS) break;
}
return err;
}
/* Configure the Rx flow classification key */ staticint dpaa2_eth_config_cls_key(struct dpaa2_eth_priv *priv, dma_addr_t key)
{ struct device *dev = priv->net_dev->dev.parent; struct dpni_rx_dist_cfg dist_cfg; int i, err = 0;
for (i = 0; i < dpaa2_eth_tc_count(priv); i++) {
dist_cfg.tc = i;
err = dpni_set_rx_fs_dist(priv->mc_io, 0, priv->mc_token,
&dist_cfg); if (err) {
dev_err(dev, "dpni_set_rx_fs_dist failed\n"); break;
}
/* If the flow steering / hashing key is shared between all * traffic classes, install it just once
*/ if (priv->dpni_attrs.options & DPNI_OPT_SHARED_FS) break;
}
return err;
}
/* Size of the Rx flow classification key */ int dpaa2_eth_cls_key_size(u64 fields)
{ int i, size = 0;
for (i = 0; i < ARRAY_SIZE(dist_fields); i++) { if (!(fields & dist_fields[i].id)) continue;
size += dist_fields[i].size;
}
return size;
}
/* Offset of header field in Rx classification key */ int dpaa2_eth_cls_fld_off(int prot, int field)
{ int i, off = 0;
for (i = 0; i < ARRAY_SIZE(dist_fields); i++) { if (dist_fields[i].cls_prot == prot &&
dist_fields[i].cls_field == field) return off;
off += dist_fields[i].size;
}
WARN_ONCE(1, "Unsupported header field used for Rx flow cls\n"); return 0;
}
/* Prune unused fields from the classification rule. * Used when masking is not supported
*/ void dpaa2_eth_cls_trim_rule(void *key_mem, u64 fields)
{ int off = 0, new_off = 0; int i, size;
for (i = 0; i < ARRAY_SIZE(dist_fields); i++) {
size = dist_fields[i].size; if (dist_fields[i].id & fields) {
memcpy(key_mem + new_off, key_mem + off, size);
new_off += size;
}
off += size;
}
}
/* Set Rx distribution (hash or flow classification) key * flags is a combination of RXH_ bits
*/ staticint dpaa2_eth_set_dist_key(struct net_device *net_dev, enum dpaa2_eth_rx_dist type, u64 flags)
{ struct device *dev = net_dev->dev.parent; struct dpaa2_eth_priv *priv = netdev_priv(net_dev); struct dpkg_profile_cfg cls_cfg;
u32 rx_hash_fields = 0;
dma_addr_t key_iova;
u8 *dma_mem; int i; int err = 0;
memset(&cls_cfg, 0, sizeof(cls_cfg));
for (i = 0; i < ARRAY_SIZE(dist_fields); i++) { struct dpkg_extract *key =
&cls_cfg.extracts[cls_cfg.num_extracts];
/* For both Rx hashing and classification keys * we set only the selected fields.
*/ if (!(flags & dist_fields[i].id)) continue; if (type == DPAA2_ETH_RX_DIST_HASH)
rx_hash_fields |= dist_fields[i].rxnfc_field;
if (cls_cfg.num_extracts >= DPKG_MAX_NUM_OF_EXTRACTS) {
dev_err(dev, "error adding key extraction rule, too many rules?\n"); return -E2BIG;
}
/* Check if we actually support Rx flow classification */ if (dpaa2_eth_has_legacy_dist(priv)) {
dev_dbg(dev, "Rx cls not supported by current MC version\n"); return -EOPNOTSUPP;
}
if (!dpaa2_eth_fs_enabled(priv)) {
dev_dbg(dev, "Rx cls disabled in DPNI options\n"); return -EOPNOTSUPP;
}
if (!dpaa2_eth_hash_enabled(priv)) {
dev_dbg(dev, "Rx cls disabled for single queue DPNIs\n"); return -EOPNOTSUPP;
}
/* If there is no support for masking in the classification table, * we don't set a default key, as it will depend on the rules * added by the user at runtime.
*/ if (!dpaa2_eth_fs_mask_enabled(priv)) goto out;
err = dpaa2_eth_set_cls(priv->net_dev, DPAA2_ETH_DIST_ALL); if (err) return err;
out:
priv->rx_cls_enabled = 1;
return 0;
}
/* Bind the DPNI to its needed objects and resources: buffer pool, DPIOs, * frame queues and channels
*/ staticint dpaa2_eth_bind_dpni(struct dpaa2_eth_priv *priv)
{ struct dpaa2_eth_bp *bp = priv->bp[DPAA2_ETH_DEFAULT_BP_IDX]; struct net_device *net_dev = priv->net_dev; struct dpni_pools_cfg pools_params = { 0 }; struct device *dev = net_dev->dev.parent; struct dpni_error_cfg err_cfg; int err = 0; int i;
/* have the interface implicitly distribute traffic based on * the default hash key
*/
err = dpaa2_eth_set_hash(net_dev, DPAA2_RXH_DEFAULT); if (err && err != -EOPNOTSUPP)
dev_err(dev, "Failed to configure hashing\n");
/* Configure the flow classification key; it includes all * supported header fields and cannot be modified at runtime
*/
err = dpaa2_eth_set_default_cls(priv); if (err && err != -EOPNOTSUPP)
dev_err(dev, "Failed to configure Rx classification key\n");
/* Get firmware address, if any */
err = dpni_get_port_mac_addr(priv->mc_io, 0, priv->mc_token, mac_addr); if (err) {
dev_err(dev, "dpni_get_port_mac_addr() failed\n"); return err;
}
/* Get DPNI attributes address, if any */
err = dpni_get_primary_mac_addr(priv->mc_io, 0, priv->mc_token,
dpni_mac_addr); if (err) {
dev_err(dev, "dpni_get_primary_mac_addr() failed\n"); return err;
}
/* First check if firmware has any address configured by bootloader */ if (!is_zero_ether_addr(mac_addr)) { /* If the DPMAC addr != DPNI addr, update it */ if (!ether_addr_equal(mac_addr, dpni_mac_addr)) {
err = dpni_set_primary_mac_addr(priv->mc_io, 0,
priv->mc_token,
mac_addr); if (err) {
dev_err(dev, "dpni_set_primary_mac_addr() failed\n"); return err;
}
}
eth_hw_addr_set(net_dev, mac_addr);
} elseif (is_zero_ether_addr(dpni_mac_addr)) { /* No MAC address configured, fill in net_dev->dev_addr * with a random one
*/
eth_hw_addr_random(net_dev);
dev_dbg_once(dev, "device(s) have all-zero hwaddr, replaced with random\n");
/* Override NET_ADDR_RANDOM set by eth_hw_addr_random(); for all * practical purposes, this will be our "permanent" mac address, * at least until the next reboot. This move will also permit * register_netdevice() to properly fill up net_dev->perm_addr.
*/
net_dev->addr_assign_type = NET_ADDR_PERM;
} else { /* NET_ADDR_PERM is default, all we have to do is * fill in the device addr.
*/
eth_hw_addr_set(net_dev, dpni_mac_addr);
}
err = dpaa2_eth_set_mac_addr(priv); if (err) return err;
/* Explicitly add the broadcast address to the MAC filtering table */
eth_broadcast_addr(bcast_addr);
err = dpni_add_mac_addr(priv->mc_io, 0, priv->mc_token, bcast_addr); if (err) {
dev_err(dev, "dpni_add_mac_addr() failed\n"); return err;
}
/* Set MTU upper limit; lower limit is 68B (default value) */
net_dev->max_mtu = DPAA2_ETH_MAX_MTU;
err = dpni_set_max_frame_length(priv->mc_io, 0, priv->mc_token,
DPAA2_ETH_MFL); if (err) {
dev_err(dev, "dpni_set_max_frame_length() failed\n"); return err;
}
/* Set actual number of queues in the net device */
num_queues = dpaa2_eth_queue_count(priv);
err = netif_set_real_num_tx_queues(net_dev, num_queues); if (err) {
dev_err(dev, "netif_set_real_num_tx_queues() failed\n"); return err;
}
err = netif_set_real_num_rx_queues(net_dev, num_queues); if (err) {
dev_err(dev, "netif_set_real_num_rx_queues() failed\n"); return err;
}
err = dpaa2_mac_open(mac); if (err) goto err_free_mac;
if (dpaa2_mac_is_type_phy(mac)) {
err = dpaa2_mac_connect(mac); if (err) { if (err == -EPROBE_DEFER)
netdev_dbg(priv->net_dev, "could not connect to MAC\n"); else
netdev_err(priv->net_dev, "Error connecting to the MAC endpoint: %pe",
ERR_PTR(err)); goto err_close_mac;
}
}
err = dpni_get_irq_status(dpni_dev->mc_io, 0, dpni_dev->mc_handle,
DPNI_IRQ_INDEX, &status); if (unlikely(err)) {
netdev_err(net_dev, "Can't get irq status (err %d)\n", err); return IRQ_HANDLED;
}
if (status & DPNI_IRQ_EVENT_LINK_CHANGED)
dpaa2_eth_link_state_update(netdev_priv(net_dev));
if (status & DPNI_IRQ_EVENT_ENDPOINT_CHANGED) {
dpaa2_eth_set_mac_addr(netdev_priv(net_dev));
dpaa2_eth_update_tx_fqids(priv);
/* We can avoid locking because the "endpoint changed" IRQ * handler is the only one who changes priv->mac at runtime, * so we are not racing with anyone.
*/
had_mac = !!priv->mac; if (had_mac)
dpaa2_eth_disconnect_mac(priv); else
dpaa2_eth_connect_mac(priv);
}
staticvoid dpaa2_eth_add_ch_napi(struct dpaa2_eth_priv *priv)
{ int i; struct dpaa2_eth_channel *ch;
for (i = 0; i < priv->num_channels; i++) {
ch = priv->channel[i]; /* NAPI weight *MUST* be a multiple of DPAA2_ETH_STORE_SIZE */
netif_napi_add(priv->net_dev, &ch->napi, dpaa2_eth_poll);
}
}
staticvoid dpaa2_eth_del_ch_napi(struct dpaa2_eth_priv *priv)
{ int i; struct dpaa2_eth_channel *ch;
for (i = 0; i < priv->num_channels; i++) {
ch = priv->channel[i];
netif_napi_del(&ch->napi);
}
}
staticvoid dpaa2_eth_free_rx_xdp_rxq(struct dpaa2_eth_priv *priv)
{ int i;
for (i = 0; i < priv->num_fqs; i++) { if (priv->fq[i].type == DPAA2_RX_FQ &&
xdp_rxq_info_is_reg(&priv->fq[i].channel->xdp_rxq))
xdp_rxq_info_unreg(&priv->fq[i].channel->xdp_rxq);
}
}
err = dpaa2_eth_netdev_init(net_dev); if (err) goto err_netdev_init;
/* Configure checksum offload based on current interface flags */
err = dpaa2_eth_set_rx_csum(priv, !!(net_dev->features & NETIF_F_RXCSUM)); if (err) goto err_csum;
err = dpaa2_eth_connect_mac(priv); if (err) goto err_connect_mac;
err = dpaa2_eth_setup_irqs(dpni_dev); if (err) {
netdev_warn(net_dev, "Failed to set link interrupt, fall back to polling\n");
priv->poll_thread = kthread_run(dpaa2_eth_poll_link_state, priv, "%s_poll_link", net_dev->name); if (IS_ERR(priv->poll_thread)) {
dev_err(dev, "Error starting polling thread\n"); goto err_poll_thread;
}
priv->do_link_poll = true;
}
err = dpaa2_eth_dl_alloc(priv); if (err) goto err_dl_register;
err = dpaa2_eth_dl_traps_register(priv); if (err) goto err_dl_trap_register;
err = dpaa2_eth_dl_port_add(priv); if (err) goto err_dl_port_add;
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.