1d2912cb1SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
25a6681e2SEdward Cree /****************************************************************************
35a6681e2SEdward Cree * Driver for Solarflare network controllers and boards
45a6681e2SEdward Cree * Copyright 2005-2006 Fen Systems Ltd.
55a6681e2SEdward Cree * Copyright 2005-2013 Solarflare Communications Inc.
65a6681e2SEdward Cree */
75a6681e2SEdward Cree
85a6681e2SEdward Cree #include <linux/pci.h>
95a6681e2SEdward Cree #include <linux/tcp.h>
105a6681e2SEdward Cree #include <linux/ip.h>
115a6681e2SEdward Cree #include <linux/in.h>
125a6681e2SEdward Cree #include <linux/ipv6.h>
135a6681e2SEdward Cree #include <linux/slab.h>
145a6681e2SEdward Cree #include <net/ipv6.h>
155a6681e2SEdward Cree #include <linux/if_ether.h>
165a6681e2SEdward Cree #include <linux/highmem.h>
175a6681e2SEdward Cree #include <linux/cache.h>
185a6681e2SEdward Cree #include "net_driver.h"
195a6681e2SEdward Cree #include "efx.h"
205a6681e2SEdward Cree #include "io.h"
215a6681e2SEdward Cree #include "nic.h"
225a6681e2SEdward Cree #include "tx.h"
235a6681e2SEdward Cree #include "workarounds.h"
245a6681e2SEdward Cree
ef4_tx_get_copy_buffer(struct ef4_tx_queue * tx_queue,struct ef4_tx_buffer * buffer)255a6681e2SEdward Cree static inline u8 *ef4_tx_get_copy_buffer(struct ef4_tx_queue *tx_queue,
265a6681e2SEdward Cree struct ef4_tx_buffer *buffer)
275a6681e2SEdward Cree {
285a6681e2SEdward Cree unsigned int index = ef4_tx_queue_get_insert_index(tx_queue);
295a6681e2SEdward Cree struct ef4_buffer *page_buf =
305a6681e2SEdward Cree &tx_queue->cb_page[index >> (PAGE_SHIFT - EF4_TX_CB_ORDER)];
315a6681e2SEdward Cree unsigned int offset =
325a6681e2SEdward Cree ((index << EF4_TX_CB_ORDER) + NET_IP_ALIGN) & (PAGE_SIZE - 1);
335a6681e2SEdward Cree
345a6681e2SEdward Cree if (unlikely(!page_buf->addr) &&
355a6681e2SEdward Cree ef4_nic_alloc_buffer(tx_queue->efx, page_buf, PAGE_SIZE,
365a6681e2SEdward Cree GFP_ATOMIC))
375a6681e2SEdward Cree return NULL;
385a6681e2SEdward Cree buffer->dma_addr = page_buf->dma_addr + offset;
395a6681e2SEdward Cree buffer->unmap_len = 0;
405a6681e2SEdward Cree return (u8 *)page_buf->addr + offset;
415a6681e2SEdward Cree }
425a6681e2SEdward Cree
ef4_dequeue_buffer(struct ef4_tx_queue * tx_queue,struct ef4_tx_buffer * buffer,unsigned int * pkts_compl,unsigned int * bytes_compl)435a6681e2SEdward Cree static void ef4_dequeue_buffer(struct ef4_tx_queue *tx_queue,
445a6681e2SEdward Cree struct ef4_tx_buffer *buffer,
455a6681e2SEdward Cree unsigned int *pkts_compl,
465a6681e2SEdward Cree unsigned int *bytes_compl)
475a6681e2SEdward Cree {
485a6681e2SEdward Cree if (buffer->unmap_len) {
495a6681e2SEdward Cree struct device *dma_dev = &tx_queue->efx->pci_dev->dev;
505a6681e2SEdward Cree dma_addr_t unmap_addr = buffer->dma_addr - buffer->dma_offset;
515a6681e2SEdward Cree if (buffer->flags & EF4_TX_BUF_MAP_SINGLE)
525a6681e2SEdward Cree dma_unmap_single(dma_dev, unmap_addr, buffer->unmap_len,
535a6681e2SEdward Cree DMA_TO_DEVICE);
545a6681e2SEdward Cree else
555a6681e2SEdward Cree dma_unmap_page(dma_dev, unmap_addr, buffer->unmap_len,
565a6681e2SEdward Cree DMA_TO_DEVICE);
575a6681e2SEdward Cree buffer->unmap_len = 0;
585a6681e2SEdward Cree }
595a6681e2SEdward Cree
605a6681e2SEdward Cree if (buffer->flags & EF4_TX_BUF_SKB) {
615a6681e2SEdward Cree (*pkts_compl)++;
625a6681e2SEdward Cree (*bytes_compl) += buffer->skb->len;
635a6681e2SEdward Cree dev_consume_skb_any((struct sk_buff *)buffer->skb);
645a6681e2SEdward Cree netif_vdbg(tx_queue->efx, tx_done, tx_queue->efx->net_dev,
655a6681e2SEdward Cree "TX queue %d transmission id %x complete\n",
665a6681e2SEdward Cree tx_queue->queue, tx_queue->read_count);
675a6681e2SEdward Cree }
685a6681e2SEdward Cree
695a6681e2SEdward Cree buffer->len = 0;
705a6681e2SEdward Cree buffer->flags = 0;
715a6681e2SEdward Cree }
725a6681e2SEdward Cree
ef4_tx_max_skb_descs(struct ef4_nic * efx)735a6681e2SEdward Cree unsigned int ef4_tx_max_skb_descs(struct ef4_nic *efx)
745a6681e2SEdward Cree {
755a6681e2SEdward Cree /* This is probably too much since we don't have any TSO support;
765a6681e2SEdward Cree * it's a left-over from when we had Software TSO. But it's safer
775a6681e2SEdward Cree * to leave it as-is than try to determine a new bound.
785a6681e2SEdward Cree */
795a6681e2SEdward Cree /* Header and payload descriptor for each output segment, plus
805a6681e2SEdward Cree * one for every input fragment boundary within a segment
815a6681e2SEdward Cree */
825a6681e2SEdward Cree unsigned int max_descs = EF4_TSO_MAX_SEGS * 2 + MAX_SKB_FRAGS;
835a6681e2SEdward Cree
845a6681e2SEdward Cree /* Possibly one more per segment for the alignment workaround,
855a6681e2SEdward Cree * or for option descriptors
865a6681e2SEdward Cree */
875a6681e2SEdward Cree if (EF4_WORKAROUND_5391(efx))
885a6681e2SEdward Cree max_descs += EF4_TSO_MAX_SEGS;
895a6681e2SEdward Cree
905a6681e2SEdward Cree /* Possibly more for PCIe page boundaries within input fragments */
915a6681e2SEdward Cree if (PAGE_SIZE > EF4_PAGE_SIZE)
925a6681e2SEdward Cree max_descs += max_t(unsigned int, MAX_SKB_FRAGS,
93*7c4e983cSAlexander Duyck DIV_ROUND_UP(GSO_LEGACY_MAX_SIZE,
94*7c4e983cSAlexander Duyck EF4_PAGE_SIZE));
955a6681e2SEdward Cree
965a6681e2SEdward Cree return max_descs;
975a6681e2SEdward Cree }
985a6681e2SEdward Cree
ef4_tx_maybe_stop_queue(struct ef4_tx_queue * txq1)995a6681e2SEdward Cree static void ef4_tx_maybe_stop_queue(struct ef4_tx_queue *txq1)
1005a6681e2SEdward Cree {
1015a6681e2SEdward Cree /* We need to consider both queues that the net core sees as one */
1025a6681e2SEdward Cree struct ef4_tx_queue *txq2 = ef4_tx_queue_partner(txq1);
1035a6681e2SEdward Cree struct ef4_nic *efx = txq1->efx;
1045a6681e2SEdward Cree unsigned int fill_level;
1055a6681e2SEdward Cree
1065a6681e2SEdward Cree fill_level = max(txq1->insert_count - txq1->old_read_count,
1075a6681e2SEdward Cree txq2->insert_count - txq2->old_read_count);
1085a6681e2SEdward Cree if (likely(fill_level < efx->txq_stop_thresh))
1095a6681e2SEdward Cree return;
1105a6681e2SEdward Cree
1115a6681e2SEdward Cree /* We used the stale old_read_count above, which gives us a
1125a6681e2SEdward Cree * pessimistic estimate of the fill level (which may even
1135a6681e2SEdward Cree * validly be >= efx->txq_entries). Now try again using
1145a6681e2SEdward Cree * read_count (more likely to be a cache miss).
1155a6681e2SEdward Cree *
1165a6681e2SEdward Cree * If we read read_count and then conditionally stop the
1175a6681e2SEdward Cree * queue, it is possible for the completion path to race with
1185a6681e2SEdward Cree * us and complete all outstanding descriptors in the middle,
1195a6681e2SEdward Cree * after which there will be no more completions to wake it.
1205a6681e2SEdward Cree * Therefore we stop the queue first, then read read_count
1215a6681e2SEdward Cree * (with a memory barrier to ensure the ordering), then
1225a6681e2SEdward Cree * restart the queue if the fill level turns out to be low
1235a6681e2SEdward Cree * enough.
1245a6681e2SEdward Cree */
1255a6681e2SEdward Cree netif_tx_stop_queue(txq1->core_txq);
1265a6681e2SEdward Cree smp_mb();
1276aa7de05SMark Rutland txq1->old_read_count = READ_ONCE(txq1->read_count);
1286aa7de05SMark Rutland txq2->old_read_count = READ_ONCE(txq2->read_count);
1295a6681e2SEdward Cree
1305a6681e2SEdward Cree fill_level = max(txq1->insert_count - txq1->old_read_count,
1315a6681e2SEdward Cree txq2->insert_count - txq2->old_read_count);
1325a6681e2SEdward Cree EF4_BUG_ON_PARANOID(fill_level >= efx->txq_entries);
1335a6681e2SEdward Cree if (likely(fill_level < efx->txq_stop_thresh)) {
1345a6681e2SEdward Cree smp_mb();
1355a6681e2SEdward Cree if (likely(!efx->loopback_selftest))
1365a6681e2SEdward Cree netif_tx_start_queue(txq1->core_txq);
1375a6681e2SEdward Cree }
1385a6681e2SEdward Cree }
1395a6681e2SEdward Cree
ef4_enqueue_skb_copy(struct ef4_tx_queue * tx_queue,struct sk_buff * skb)1405a6681e2SEdward Cree static int ef4_enqueue_skb_copy(struct ef4_tx_queue *tx_queue,
1415a6681e2SEdward Cree struct sk_buff *skb)
1425a6681e2SEdward Cree {
1435a6681e2SEdward Cree unsigned int min_len = tx_queue->tx_min_size;
1445a6681e2SEdward Cree unsigned int copy_len = skb->len;
1455a6681e2SEdward Cree struct ef4_tx_buffer *buffer;
1465a6681e2SEdward Cree u8 *copy_buffer;
1475a6681e2SEdward Cree int rc;
1485a6681e2SEdward Cree
1495a6681e2SEdward Cree EF4_BUG_ON_PARANOID(copy_len > EF4_TX_CB_SIZE);
1505a6681e2SEdward Cree
1515a6681e2SEdward Cree buffer = ef4_tx_queue_get_insert_buffer(tx_queue);
1525a6681e2SEdward Cree
1535a6681e2SEdward Cree copy_buffer = ef4_tx_get_copy_buffer(tx_queue, buffer);
1545a6681e2SEdward Cree if (unlikely(!copy_buffer))
1555a6681e2SEdward Cree return -ENOMEM;
1565a6681e2SEdward Cree
1575a6681e2SEdward Cree rc = skb_copy_bits(skb, 0, copy_buffer, copy_len);
1585a6681e2SEdward Cree EF4_WARN_ON_PARANOID(rc);
1595a6681e2SEdward Cree if (unlikely(copy_len < min_len)) {
1605a6681e2SEdward Cree memset(copy_buffer + copy_len, 0, min_len - copy_len);
1615a6681e2SEdward Cree buffer->len = min_len;
1625a6681e2SEdward Cree } else {
1635a6681e2SEdward Cree buffer->len = copy_len;
1645a6681e2SEdward Cree }
1655a6681e2SEdward Cree
1665a6681e2SEdward Cree buffer->skb = skb;
1675a6681e2SEdward Cree buffer->flags = EF4_TX_BUF_SKB;
1685a6681e2SEdward Cree
1695a6681e2SEdward Cree ++tx_queue->insert_count;
1705a6681e2SEdward Cree return rc;
1715a6681e2SEdward Cree }
1725a6681e2SEdward Cree
ef4_tx_map_chunk(struct ef4_tx_queue * tx_queue,dma_addr_t dma_addr,size_t len)1735a6681e2SEdward Cree static struct ef4_tx_buffer *ef4_tx_map_chunk(struct ef4_tx_queue *tx_queue,
1745a6681e2SEdward Cree dma_addr_t dma_addr,
1755a6681e2SEdward Cree size_t len)
1765a6681e2SEdward Cree {
1775a6681e2SEdward Cree const struct ef4_nic_type *nic_type = tx_queue->efx->type;
1785a6681e2SEdward Cree struct ef4_tx_buffer *buffer;
1795a6681e2SEdward Cree unsigned int dma_len;
1805a6681e2SEdward Cree
1815a6681e2SEdward Cree /* Map the fragment taking account of NIC-dependent DMA limits. */
1825a6681e2SEdward Cree do {
1835a6681e2SEdward Cree buffer = ef4_tx_queue_get_insert_buffer(tx_queue);
1845a6681e2SEdward Cree dma_len = nic_type->tx_limit_len(tx_queue, dma_addr, len);
1855a6681e2SEdward Cree
1865a6681e2SEdward Cree buffer->len = dma_len;
1875a6681e2SEdward Cree buffer->dma_addr = dma_addr;
1885a6681e2SEdward Cree buffer->flags = EF4_TX_BUF_CONT;
1895a6681e2SEdward Cree len -= dma_len;
1905a6681e2SEdward Cree dma_addr += dma_len;
1915a6681e2SEdward Cree ++tx_queue->insert_count;
1925a6681e2SEdward Cree } while (len);
1935a6681e2SEdward Cree
1945a6681e2SEdward Cree return buffer;
1955a6681e2SEdward Cree }
1965a6681e2SEdward Cree
1975a6681e2SEdward Cree /* Map all data from an SKB for DMA and create descriptors on the queue.
1985a6681e2SEdward Cree */
ef4_tx_map_data(struct ef4_tx_queue * tx_queue,struct sk_buff * skb)1995a6681e2SEdward Cree static int ef4_tx_map_data(struct ef4_tx_queue *tx_queue, struct sk_buff *skb)
2005a6681e2SEdward Cree {
2015a6681e2SEdward Cree struct ef4_nic *efx = tx_queue->efx;
2025a6681e2SEdward Cree struct device *dma_dev = &efx->pci_dev->dev;
2035a6681e2SEdward Cree unsigned int frag_index, nr_frags;
2045a6681e2SEdward Cree dma_addr_t dma_addr, unmap_addr;
2055a6681e2SEdward Cree unsigned short dma_flags;
2065a6681e2SEdward Cree size_t len, unmap_len;
2075a6681e2SEdward Cree
2085a6681e2SEdward Cree nr_frags = skb_shinfo(skb)->nr_frags;
2095a6681e2SEdward Cree frag_index = 0;
2105a6681e2SEdward Cree
2115a6681e2SEdward Cree /* Map header data. */
2125a6681e2SEdward Cree len = skb_headlen(skb);
2135a6681e2SEdward Cree dma_addr = dma_map_single(dma_dev, skb->data, len, DMA_TO_DEVICE);
2145a6681e2SEdward Cree dma_flags = EF4_TX_BUF_MAP_SINGLE;
2155a6681e2SEdward Cree unmap_len = len;
2165a6681e2SEdward Cree unmap_addr = dma_addr;
2175a6681e2SEdward Cree
2185a6681e2SEdward Cree if (unlikely(dma_mapping_error(dma_dev, dma_addr)))
2195a6681e2SEdward Cree return -EIO;
2205a6681e2SEdward Cree
2215a6681e2SEdward Cree /* Add descriptors for each fragment. */
2225a6681e2SEdward Cree do {
2235a6681e2SEdward Cree struct ef4_tx_buffer *buffer;
2245a6681e2SEdward Cree skb_frag_t *fragment;
2255a6681e2SEdward Cree
2265a6681e2SEdward Cree buffer = ef4_tx_map_chunk(tx_queue, dma_addr, len);
2275a6681e2SEdward Cree
2285a6681e2SEdward Cree /* The final descriptor for a fragment is responsible for
2295a6681e2SEdward Cree * unmapping the whole fragment.
2305a6681e2SEdward Cree */
2315a6681e2SEdward Cree buffer->flags = EF4_TX_BUF_CONT | dma_flags;
2325a6681e2SEdward Cree buffer->unmap_len = unmap_len;
2335a6681e2SEdward Cree buffer->dma_offset = buffer->dma_addr - unmap_addr;
2345a6681e2SEdward Cree
2355a6681e2SEdward Cree if (frag_index >= nr_frags) {
2365a6681e2SEdward Cree /* Store SKB details with the final buffer for
2375a6681e2SEdward Cree * the completion.
2385a6681e2SEdward Cree */
2395a6681e2SEdward Cree buffer->skb = skb;
2405a6681e2SEdward Cree buffer->flags = EF4_TX_BUF_SKB | dma_flags;
2415a6681e2SEdward Cree return 0;
2425a6681e2SEdward Cree }
2435a6681e2SEdward Cree
2445a6681e2SEdward Cree /* Move on to the next fragment. */
2455a6681e2SEdward Cree fragment = &skb_shinfo(skb)->frags[frag_index++];
2465a6681e2SEdward Cree len = skb_frag_size(fragment);
2475a6681e2SEdward Cree dma_addr = skb_frag_dma_map(dma_dev, fragment,
2485a6681e2SEdward Cree 0, len, DMA_TO_DEVICE);
2495a6681e2SEdward Cree dma_flags = 0;
2505a6681e2SEdward Cree unmap_len = len;
2515a6681e2SEdward Cree unmap_addr = dma_addr;
2525a6681e2SEdward Cree
2535a6681e2SEdward Cree if (unlikely(dma_mapping_error(dma_dev, dma_addr)))
2545a6681e2SEdward Cree return -EIO;
2555a6681e2SEdward Cree } while (1);
2565a6681e2SEdward Cree }
2575a6681e2SEdward Cree
2585a6681e2SEdward Cree /* Remove buffers put into a tx_queue. None of the buffers must have
2595a6681e2SEdward Cree * an skb attached.
2605a6681e2SEdward Cree */
ef4_enqueue_unwind(struct ef4_tx_queue * tx_queue)2615a6681e2SEdward Cree static void ef4_enqueue_unwind(struct ef4_tx_queue *tx_queue)
2625a6681e2SEdward Cree {
2635a6681e2SEdward Cree struct ef4_tx_buffer *buffer;
2645a6681e2SEdward Cree
2655a6681e2SEdward Cree /* Work backwards until we hit the original insert pointer value */
2665a6681e2SEdward Cree while (tx_queue->insert_count != tx_queue->write_count) {
2675a6681e2SEdward Cree --tx_queue->insert_count;
2685a6681e2SEdward Cree buffer = __ef4_tx_queue_get_insert_buffer(tx_queue);
2695a6681e2SEdward Cree ef4_dequeue_buffer(tx_queue, buffer, NULL, NULL);
2705a6681e2SEdward Cree }
2715a6681e2SEdward Cree }
2725a6681e2SEdward Cree
2735a6681e2SEdward Cree /*
2745a6681e2SEdward Cree * Add a socket buffer to a TX queue
2755a6681e2SEdward Cree *
2765a6681e2SEdward Cree * This maps all fragments of a socket buffer for DMA and adds them to
2775a6681e2SEdward Cree * the TX queue. The queue's insert pointer will be incremented by
2785a6681e2SEdward Cree * the number of fragments in the socket buffer.
2795a6681e2SEdward Cree *
2805a6681e2SEdward Cree * If any DMA mapping fails, any mapped fragments will be unmapped,
2815a6681e2SEdward Cree * the queue's insert pointer will be restored to its original value.
2825a6681e2SEdward Cree *
2835a6681e2SEdward Cree * This function is split out from ef4_hard_start_xmit to allow the
2845a6681e2SEdward Cree * loopback test to direct packets via specific TX queues.
2855a6681e2SEdward Cree *
2865a6681e2SEdward Cree * Returns NETDEV_TX_OK.
2875a6681e2SEdward Cree * You must hold netif_tx_lock() to call this function.
2885a6681e2SEdward Cree */
ef4_enqueue_skb(struct ef4_tx_queue * tx_queue,struct sk_buff * skb)2895a6681e2SEdward Cree netdev_tx_t ef4_enqueue_skb(struct ef4_tx_queue *tx_queue, struct sk_buff *skb)
2905a6681e2SEdward Cree {
2915a6681e2SEdward Cree bool data_mapped = false;
2925a6681e2SEdward Cree unsigned int skb_len;
2935a6681e2SEdward Cree
2945a6681e2SEdward Cree skb_len = skb->len;
2955a6681e2SEdward Cree EF4_WARN_ON_PARANOID(skb_is_gso(skb));
2965a6681e2SEdward Cree
2975a6681e2SEdward Cree if (skb_len < tx_queue->tx_min_size ||
2985a6681e2SEdward Cree (skb->data_len && skb_len <= EF4_TX_CB_SIZE)) {
2995a6681e2SEdward Cree /* Pad short packets or coalesce short fragmented packets. */
3005a6681e2SEdward Cree if (ef4_enqueue_skb_copy(tx_queue, skb))
3015a6681e2SEdward Cree goto err;
3025a6681e2SEdward Cree tx_queue->cb_packets++;
3035a6681e2SEdward Cree data_mapped = true;
3045a6681e2SEdward Cree }
3055a6681e2SEdward Cree
3065a6681e2SEdward Cree /* Map for DMA and create descriptors if we haven't done so already. */
3075a6681e2SEdward Cree if (!data_mapped && (ef4_tx_map_data(tx_queue, skb)))
3085a6681e2SEdward Cree goto err;
3095a6681e2SEdward Cree
3105a6681e2SEdward Cree /* Update BQL */
3115a6681e2SEdward Cree netdev_tx_sent_queue(tx_queue->core_txq, skb_len);
3125a6681e2SEdward Cree
3135a6681e2SEdward Cree /* Pass off to hardware */
314f79c957aSFlorian Westphal if (!netdev_xmit_more() || netif_xmit_stopped(tx_queue->core_txq)) {
3155a6681e2SEdward Cree struct ef4_tx_queue *txq2 = ef4_tx_queue_partner(tx_queue);
3165a6681e2SEdward Cree
3175a6681e2SEdward Cree /* There could be packets left on the partner queue if those
3185a6681e2SEdward Cree * SKBs had skb->xmit_more set. If we do not push those they
3195a6681e2SEdward Cree * could be left for a long time and cause a netdev watchdog.
3205a6681e2SEdward Cree */
3215a6681e2SEdward Cree if (txq2->xmit_more_available)
3225a6681e2SEdward Cree ef4_nic_push_buffers(txq2);
3235a6681e2SEdward Cree
3245a6681e2SEdward Cree ef4_nic_push_buffers(tx_queue);
3255a6681e2SEdward Cree } else {
326f79c957aSFlorian Westphal tx_queue->xmit_more_available = netdev_xmit_more();
3275a6681e2SEdward Cree }
3285a6681e2SEdward Cree
3295a6681e2SEdward Cree tx_queue->tx_packets++;
3305a6681e2SEdward Cree
3315a6681e2SEdward Cree ef4_tx_maybe_stop_queue(tx_queue);
3325a6681e2SEdward Cree
3335a6681e2SEdward Cree return NETDEV_TX_OK;
3345a6681e2SEdward Cree
3355a6681e2SEdward Cree
3365a6681e2SEdward Cree err:
3375a6681e2SEdward Cree ef4_enqueue_unwind(tx_queue);
3385a6681e2SEdward Cree dev_kfree_skb_any(skb);
3395a6681e2SEdward Cree return NETDEV_TX_OK;
3405a6681e2SEdward Cree }
3415a6681e2SEdward Cree
3425a6681e2SEdward Cree /* Remove packets from the TX queue
3435a6681e2SEdward Cree *
3445a6681e2SEdward Cree * This removes packets from the TX queue, up to and including the
3455a6681e2SEdward Cree * specified index.
3465a6681e2SEdward Cree */
ef4_dequeue_buffers(struct ef4_tx_queue * tx_queue,unsigned int index,unsigned int * pkts_compl,unsigned int * bytes_compl)3475a6681e2SEdward Cree static void ef4_dequeue_buffers(struct ef4_tx_queue *tx_queue,
3485a6681e2SEdward Cree unsigned int index,
3495a6681e2SEdward Cree unsigned int *pkts_compl,
3505a6681e2SEdward Cree unsigned int *bytes_compl)
3515a6681e2SEdward Cree {
3525a6681e2SEdward Cree struct ef4_nic *efx = tx_queue->efx;
3535a6681e2SEdward Cree unsigned int stop_index, read_ptr;
3545a6681e2SEdward Cree
3555a6681e2SEdward Cree stop_index = (index + 1) & tx_queue->ptr_mask;
3565a6681e2SEdward Cree read_ptr = tx_queue->read_count & tx_queue->ptr_mask;
3575a6681e2SEdward Cree
3585a6681e2SEdward Cree while (read_ptr != stop_index) {
3595a6681e2SEdward Cree struct ef4_tx_buffer *buffer = &tx_queue->buffer[read_ptr];
3605a6681e2SEdward Cree
3615a6681e2SEdward Cree if (!(buffer->flags & EF4_TX_BUF_OPTION) &&
3625a6681e2SEdward Cree unlikely(buffer->len == 0)) {
3635a6681e2SEdward Cree netif_err(efx, tx_err, efx->net_dev,
3645a6681e2SEdward Cree "TX queue %d spurious TX completion id %x\n",
3655a6681e2SEdward Cree tx_queue->queue, read_ptr);
3665a6681e2SEdward Cree ef4_schedule_reset(efx, RESET_TYPE_TX_SKIP);
3675a6681e2SEdward Cree return;
3685a6681e2SEdward Cree }
3695a6681e2SEdward Cree
3705a6681e2SEdward Cree ef4_dequeue_buffer(tx_queue, buffer, pkts_compl, bytes_compl);
3715a6681e2SEdward Cree
3725a6681e2SEdward Cree ++tx_queue->read_count;
3735a6681e2SEdward Cree read_ptr = tx_queue->read_count & tx_queue->ptr_mask;
3745a6681e2SEdward Cree }
3755a6681e2SEdward Cree }
3765a6681e2SEdward Cree
3775a6681e2SEdward Cree /* Initiate a packet transmission. We use one channel per CPU
3785a6681e2SEdward Cree * (sharing when we have more CPUs than channels). On Falcon, the TX
3795a6681e2SEdward Cree * completion events will be directed back to the CPU that transmitted
3805a6681e2SEdward Cree * the packet, which should be cache-efficient.
3815a6681e2SEdward Cree *
3825a6681e2SEdward Cree * Context: non-blocking.
3835a6681e2SEdward Cree * Note that returning anything other than NETDEV_TX_OK will cause the
3845a6681e2SEdward Cree * OS to free the skb.
3855a6681e2SEdward Cree */
ef4_hard_start_xmit(struct sk_buff * skb,struct net_device * net_dev)3865a6681e2SEdward Cree netdev_tx_t ef4_hard_start_xmit(struct sk_buff *skb,
3875a6681e2SEdward Cree struct net_device *net_dev)
3885a6681e2SEdward Cree {
3895a6681e2SEdward Cree struct ef4_nic *efx = netdev_priv(net_dev);
3905a6681e2SEdward Cree struct ef4_tx_queue *tx_queue;
3915a6681e2SEdward Cree unsigned index, type;
3925a6681e2SEdward Cree
3935a6681e2SEdward Cree EF4_WARN_ON_PARANOID(!netif_device_present(net_dev));
3945a6681e2SEdward Cree
3955a6681e2SEdward Cree index = skb_get_queue_mapping(skb);
3965a6681e2SEdward Cree type = skb->ip_summed == CHECKSUM_PARTIAL ? EF4_TXQ_TYPE_OFFLOAD : 0;
3975a6681e2SEdward Cree if (index >= efx->n_tx_channels) {
3985a6681e2SEdward Cree index -= efx->n_tx_channels;
3995a6681e2SEdward Cree type |= EF4_TXQ_TYPE_HIGHPRI;
4005a6681e2SEdward Cree }
4015a6681e2SEdward Cree tx_queue = ef4_get_tx_queue(efx, index, type);
4025a6681e2SEdward Cree
4035a6681e2SEdward Cree return ef4_enqueue_skb(tx_queue, skb);
4045a6681e2SEdward Cree }
4055a6681e2SEdward Cree
ef4_init_tx_queue_core_txq(struct ef4_tx_queue * tx_queue)4065a6681e2SEdward Cree void ef4_init_tx_queue_core_txq(struct ef4_tx_queue *tx_queue)
4075a6681e2SEdward Cree {
4085a6681e2SEdward Cree struct ef4_nic *efx = tx_queue->efx;
4095a6681e2SEdward Cree
4105a6681e2SEdward Cree /* Must be inverse of queue lookup in ef4_hard_start_xmit() */
4115a6681e2SEdward Cree tx_queue->core_txq =
4125a6681e2SEdward Cree netdev_get_tx_queue(efx->net_dev,
4135a6681e2SEdward Cree tx_queue->queue / EF4_TXQ_TYPES +
4145a6681e2SEdward Cree ((tx_queue->queue & EF4_TXQ_TYPE_HIGHPRI) ?
4155a6681e2SEdward Cree efx->n_tx_channels : 0));
4165a6681e2SEdward Cree }
4175a6681e2SEdward Cree
ef4_setup_tc(struct net_device * net_dev,enum tc_setup_type type,void * type_data)4182572ac53SJiri Pirko int ef4_setup_tc(struct net_device *net_dev, enum tc_setup_type type,
419de4784caSJiri Pirko void *type_data)
4205a6681e2SEdward Cree {
4215a6681e2SEdward Cree struct ef4_nic *efx = netdev_priv(net_dev);
422de4784caSJiri Pirko struct tc_mqprio_qopt *mqprio = type_data;
4235a6681e2SEdward Cree struct ef4_channel *channel;
4245a6681e2SEdward Cree struct ef4_tx_queue *tx_queue;
4255a6681e2SEdward Cree unsigned tc, num_tc;
4265a6681e2SEdward Cree int rc;
4275a6681e2SEdward Cree
428575ed7d3SNogah Frankel if (type != TC_SETUP_QDISC_MQPRIO)
42938cf0426SJiri Pirko return -EOPNOTSUPP;
4305a6681e2SEdward Cree
431de4784caSJiri Pirko num_tc = mqprio->num_tc;
4325a6681e2SEdward Cree
4335a6681e2SEdward Cree if (ef4_nic_rev(efx) < EF4_REV_FALCON_B0 || num_tc > EF4_MAX_TX_TC)
4345a6681e2SEdward Cree return -EINVAL;
4355a6681e2SEdward Cree
436de4784caSJiri Pirko mqprio->hw = TC_MQPRIO_HW_OFFLOAD_TCS;
43756f36acdSAmritha Nambiar
4385a6681e2SEdward Cree if (num_tc == net_dev->num_tc)
4395a6681e2SEdward Cree return 0;
4405a6681e2SEdward Cree
4415a6681e2SEdward Cree for (tc = 0; tc < num_tc; tc++) {
4425a6681e2SEdward Cree net_dev->tc_to_txq[tc].offset = tc * efx->n_tx_channels;
4435a6681e2SEdward Cree net_dev->tc_to_txq[tc].count = efx->n_tx_channels;
4445a6681e2SEdward Cree }
4455a6681e2SEdward Cree
4465a6681e2SEdward Cree if (num_tc > net_dev->num_tc) {
4475a6681e2SEdward Cree /* Initialise high-priority queues as necessary */
4485a6681e2SEdward Cree ef4_for_each_channel(channel, efx) {
4495a6681e2SEdward Cree ef4_for_each_possible_channel_tx_queue(tx_queue,
4505a6681e2SEdward Cree channel) {
4515a6681e2SEdward Cree if (!(tx_queue->queue & EF4_TXQ_TYPE_HIGHPRI))
4525a6681e2SEdward Cree continue;
4535a6681e2SEdward Cree if (!tx_queue->buffer) {
4545a6681e2SEdward Cree rc = ef4_probe_tx_queue(tx_queue);
4555a6681e2SEdward Cree if (rc)
4565a6681e2SEdward Cree return rc;
4575a6681e2SEdward Cree }
4585a6681e2SEdward Cree if (!tx_queue->initialised)
4595a6681e2SEdward Cree ef4_init_tx_queue(tx_queue);
4605a6681e2SEdward Cree ef4_init_tx_queue_core_txq(tx_queue);
4615a6681e2SEdward Cree }
4625a6681e2SEdward Cree }
4635a6681e2SEdward Cree } else {
4645a6681e2SEdward Cree /* Reduce number of classes before number of queues */
4655a6681e2SEdward Cree net_dev->num_tc = num_tc;
4665a6681e2SEdward Cree }
4675a6681e2SEdward Cree
4685a6681e2SEdward Cree rc = netif_set_real_num_tx_queues(net_dev,
4695a6681e2SEdward Cree max_t(int, num_tc, 1) *
4705a6681e2SEdward Cree efx->n_tx_channels);
4715a6681e2SEdward Cree if (rc)
4725a6681e2SEdward Cree return rc;
4735a6681e2SEdward Cree
4745a6681e2SEdward Cree /* Do not destroy high-priority queues when they become
4755a6681e2SEdward Cree * unused. We would have to flush them first, and it is
4765a6681e2SEdward Cree * fairly difficult to flush a subset of TX queues. Leave
4775a6681e2SEdward Cree * it to ef4_fini_channels().
4785a6681e2SEdward Cree */
4795a6681e2SEdward Cree
4805a6681e2SEdward Cree net_dev->num_tc = num_tc;
4815a6681e2SEdward Cree return 0;
4825a6681e2SEdward Cree }
4835a6681e2SEdward Cree
ef4_xmit_done(struct ef4_tx_queue * tx_queue,unsigned int index)4845a6681e2SEdward Cree void ef4_xmit_done(struct ef4_tx_queue *tx_queue, unsigned int index)
4855a6681e2SEdward Cree {
4865a6681e2SEdward Cree unsigned fill_level;
4875a6681e2SEdward Cree struct ef4_nic *efx = tx_queue->efx;
4885a6681e2SEdward Cree struct ef4_tx_queue *txq2;
4895a6681e2SEdward Cree unsigned int pkts_compl = 0, bytes_compl = 0;
4905a6681e2SEdward Cree
4915a6681e2SEdward Cree EF4_BUG_ON_PARANOID(index > tx_queue->ptr_mask);
4925a6681e2SEdward Cree
4935a6681e2SEdward Cree ef4_dequeue_buffers(tx_queue, index, &pkts_compl, &bytes_compl);
4945a6681e2SEdward Cree tx_queue->pkts_compl += pkts_compl;
4955a6681e2SEdward Cree tx_queue->bytes_compl += bytes_compl;
4965a6681e2SEdward Cree
4975a6681e2SEdward Cree if (pkts_compl > 1)
4985a6681e2SEdward Cree ++tx_queue->merge_events;
4995a6681e2SEdward Cree
5005a6681e2SEdward Cree /* See if we need to restart the netif queue. This memory
5015a6681e2SEdward Cree * barrier ensures that we write read_count (inside
5025a6681e2SEdward Cree * ef4_dequeue_buffers()) before reading the queue status.
5035a6681e2SEdward Cree */
5045a6681e2SEdward Cree smp_mb();
5055a6681e2SEdward Cree if (unlikely(netif_tx_queue_stopped(tx_queue->core_txq)) &&
5065a6681e2SEdward Cree likely(efx->port_enabled) &&
5075a6681e2SEdward Cree likely(netif_device_present(efx->net_dev))) {
5085a6681e2SEdward Cree txq2 = ef4_tx_queue_partner(tx_queue);
5095a6681e2SEdward Cree fill_level = max(tx_queue->insert_count - tx_queue->read_count,
5105a6681e2SEdward Cree txq2->insert_count - txq2->read_count);
5115a6681e2SEdward Cree if (fill_level <= efx->txq_wake_thresh)
5125a6681e2SEdward Cree netif_tx_wake_queue(tx_queue->core_txq);
5135a6681e2SEdward Cree }
5145a6681e2SEdward Cree
5155a6681e2SEdward Cree /* Check whether the hardware queue is now empty */
5165a6681e2SEdward Cree if ((int)(tx_queue->read_count - tx_queue->old_write_count) >= 0) {
5176aa7de05SMark Rutland tx_queue->old_write_count = READ_ONCE(tx_queue->write_count);
5185a6681e2SEdward Cree if (tx_queue->read_count == tx_queue->old_write_count) {
5195a6681e2SEdward Cree smp_mb();
5205a6681e2SEdward Cree tx_queue->empty_read_count =
5215a6681e2SEdward Cree tx_queue->read_count | EF4_EMPTY_COUNT_VALID;
5225a6681e2SEdward Cree }
5235a6681e2SEdward Cree }
5245a6681e2SEdward Cree }
5255a6681e2SEdward Cree
ef4_tx_cb_page_count(struct ef4_tx_queue * tx_queue)5265a6681e2SEdward Cree static unsigned int ef4_tx_cb_page_count(struct ef4_tx_queue *tx_queue)
5275a6681e2SEdward Cree {
5285a6681e2SEdward Cree return DIV_ROUND_UP(tx_queue->ptr_mask + 1, PAGE_SIZE >> EF4_TX_CB_ORDER);
5295a6681e2SEdward Cree }
5305a6681e2SEdward Cree
ef4_probe_tx_queue(struct ef4_tx_queue * tx_queue)5315a6681e2SEdward Cree int ef4_probe_tx_queue(struct ef4_tx_queue *tx_queue)
5325a6681e2SEdward Cree {
5335a6681e2SEdward Cree struct ef4_nic *efx = tx_queue->efx;
5345a6681e2SEdward Cree unsigned int entries;
5355a6681e2SEdward Cree int rc;
5365a6681e2SEdward Cree
5375a6681e2SEdward Cree /* Create the smallest power-of-two aligned ring */
5385a6681e2SEdward Cree entries = max(roundup_pow_of_two(efx->txq_entries), EF4_MIN_DMAQ_SIZE);
5395a6681e2SEdward Cree EF4_BUG_ON_PARANOID(entries > EF4_MAX_DMAQ_SIZE);
5405a6681e2SEdward Cree tx_queue->ptr_mask = entries - 1;
5415a6681e2SEdward Cree
5425a6681e2SEdward Cree netif_dbg(efx, probe, efx->net_dev,
5435a6681e2SEdward Cree "creating TX queue %d size %#x mask %#x\n",
5445a6681e2SEdward Cree tx_queue->queue, efx->txq_entries, tx_queue->ptr_mask);
5455a6681e2SEdward Cree
5465a6681e2SEdward Cree /* Allocate software ring */
5475a6681e2SEdward Cree tx_queue->buffer = kcalloc(entries, sizeof(*tx_queue->buffer),
5485a6681e2SEdward Cree GFP_KERNEL);
5495a6681e2SEdward Cree if (!tx_queue->buffer)
5505a6681e2SEdward Cree return -ENOMEM;
5515a6681e2SEdward Cree
5525a6681e2SEdward Cree tx_queue->cb_page = kcalloc(ef4_tx_cb_page_count(tx_queue),
5535a6681e2SEdward Cree sizeof(tx_queue->cb_page[0]), GFP_KERNEL);
5545a6681e2SEdward Cree if (!tx_queue->cb_page) {
5555a6681e2SEdward Cree rc = -ENOMEM;
5565a6681e2SEdward Cree goto fail1;
5575a6681e2SEdward Cree }
5585a6681e2SEdward Cree
5595a6681e2SEdward Cree /* Allocate hardware ring */
5605a6681e2SEdward Cree rc = ef4_nic_probe_tx(tx_queue);
5615a6681e2SEdward Cree if (rc)
5625a6681e2SEdward Cree goto fail2;
5635a6681e2SEdward Cree
5645a6681e2SEdward Cree return 0;
5655a6681e2SEdward Cree
5665a6681e2SEdward Cree fail2:
5675a6681e2SEdward Cree kfree(tx_queue->cb_page);
5685a6681e2SEdward Cree tx_queue->cb_page = NULL;
5695a6681e2SEdward Cree fail1:
5705a6681e2SEdward Cree kfree(tx_queue->buffer);
5715a6681e2SEdward Cree tx_queue->buffer = NULL;
5725a6681e2SEdward Cree return rc;
5735a6681e2SEdward Cree }
5745a6681e2SEdward Cree
ef4_init_tx_queue(struct ef4_tx_queue * tx_queue)5755a6681e2SEdward Cree void ef4_init_tx_queue(struct ef4_tx_queue *tx_queue)
5765a6681e2SEdward Cree {
5775a6681e2SEdward Cree struct ef4_nic *efx = tx_queue->efx;
5785a6681e2SEdward Cree
5795a6681e2SEdward Cree netif_dbg(efx, drv, efx->net_dev,
5805a6681e2SEdward Cree "initialising TX queue %d\n", tx_queue->queue);
5815a6681e2SEdward Cree
5825a6681e2SEdward Cree tx_queue->insert_count = 0;
5835a6681e2SEdward Cree tx_queue->write_count = 0;
5845a6681e2SEdward Cree tx_queue->old_write_count = 0;
5855a6681e2SEdward Cree tx_queue->read_count = 0;
5865a6681e2SEdward Cree tx_queue->old_read_count = 0;
5875a6681e2SEdward Cree tx_queue->empty_read_count = 0 | EF4_EMPTY_COUNT_VALID;
5885a6681e2SEdward Cree tx_queue->xmit_more_available = false;
5895a6681e2SEdward Cree
5905a6681e2SEdward Cree /* Some older hardware requires Tx writes larger than 32. */
5915a6681e2SEdward Cree tx_queue->tx_min_size = EF4_WORKAROUND_15592(efx) ? 33 : 0;
5925a6681e2SEdward Cree
5935a6681e2SEdward Cree /* Set up TX descriptor ring */
5945a6681e2SEdward Cree ef4_nic_init_tx(tx_queue);
5955a6681e2SEdward Cree
5965a6681e2SEdward Cree tx_queue->initialised = true;
5975a6681e2SEdward Cree }
5985a6681e2SEdward Cree
ef4_fini_tx_queue(struct ef4_tx_queue * tx_queue)5995a6681e2SEdward Cree void ef4_fini_tx_queue(struct ef4_tx_queue *tx_queue)
6005a6681e2SEdward Cree {
6015a6681e2SEdward Cree struct ef4_tx_buffer *buffer;
6025a6681e2SEdward Cree
6035a6681e2SEdward Cree netif_dbg(tx_queue->efx, drv, tx_queue->efx->net_dev,
6045a6681e2SEdward Cree "shutting down TX queue %d\n", tx_queue->queue);
6055a6681e2SEdward Cree
6065a6681e2SEdward Cree if (!tx_queue->buffer)
6075a6681e2SEdward Cree return;
6085a6681e2SEdward Cree
6095a6681e2SEdward Cree /* Free any buffers left in the ring */
6105a6681e2SEdward Cree while (tx_queue->read_count != tx_queue->write_count) {
6115a6681e2SEdward Cree unsigned int pkts_compl = 0, bytes_compl = 0;
6125a6681e2SEdward Cree buffer = &tx_queue->buffer[tx_queue->read_count & tx_queue->ptr_mask];
6135a6681e2SEdward Cree ef4_dequeue_buffer(tx_queue, buffer, &pkts_compl, &bytes_compl);
6145a6681e2SEdward Cree
6155a6681e2SEdward Cree ++tx_queue->read_count;
6165a6681e2SEdward Cree }
6175a6681e2SEdward Cree tx_queue->xmit_more_available = false;
6185a6681e2SEdward Cree netdev_tx_reset_queue(tx_queue->core_txq);
6195a6681e2SEdward Cree }
6205a6681e2SEdward Cree
ef4_remove_tx_queue(struct ef4_tx_queue * tx_queue)6215a6681e2SEdward Cree void ef4_remove_tx_queue(struct ef4_tx_queue *tx_queue)
6225a6681e2SEdward Cree {
6235a6681e2SEdward Cree int i;
6245a6681e2SEdward Cree
6255a6681e2SEdward Cree if (!tx_queue->buffer)
6265a6681e2SEdward Cree return;
6275a6681e2SEdward Cree
6285a6681e2SEdward Cree netif_dbg(tx_queue->efx, drv, tx_queue->efx->net_dev,
6295a6681e2SEdward Cree "destroying TX queue %d\n", tx_queue->queue);
6305a6681e2SEdward Cree ef4_nic_remove_tx(tx_queue);
6315a6681e2SEdward Cree
6325a6681e2SEdward Cree if (tx_queue->cb_page) {
6335a6681e2SEdward Cree for (i = 0; i < ef4_tx_cb_page_count(tx_queue); i++)
6345a6681e2SEdward Cree ef4_nic_free_buffer(tx_queue->efx,
6355a6681e2SEdward Cree &tx_queue->cb_page[i]);
6365a6681e2SEdward Cree kfree(tx_queue->cb_page);
6375a6681e2SEdward Cree tx_queue->cb_page = NULL;
6385a6681e2SEdward Cree }
6395a6681e2SEdward Cree
6405a6681e2SEdward Cree kfree(tx_queue->buffer);
6415a6681e2SEdward Cree tx_queue->buffer = NULL;
6425a6681e2SEdward Cree }
643