1874aeea5SJeff Kirsher /**************************************************************************** 2f7a6d2c4SBen Hutchings * Driver for Solarflare network controllers and boards 3874aeea5SJeff Kirsher * Copyright 2005-2006 Fen Systems Ltd. 4f7a6d2c4SBen Hutchings * Copyright 2005-2013 Solarflare Communications Inc. 5874aeea5SJeff Kirsher * 6874aeea5SJeff Kirsher * This program is free software; you can redistribute it and/or modify it 7874aeea5SJeff Kirsher * under the terms of the GNU General Public License version 2 as published 8874aeea5SJeff Kirsher * by the Free Software Foundation, incorporated herein by reference. 9874aeea5SJeff Kirsher */ 10874aeea5SJeff Kirsher 11874aeea5SJeff Kirsher #include <linux/pci.h> 12874aeea5SJeff Kirsher #include <linux/tcp.h> 13874aeea5SJeff Kirsher #include <linux/ip.h> 14874aeea5SJeff Kirsher #include <linux/in.h> 15874aeea5SJeff Kirsher #include <linux/ipv6.h> 16874aeea5SJeff Kirsher #include <linux/slab.h> 17874aeea5SJeff Kirsher #include <net/ipv6.h> 18874aeea5SJeff Kirsher #include <linux/if_ether.h> 19874aeea5SJeff Kirsher #include <linux/highmem.h> 20183233beSBen Hutchings #include <linux/cache.h> 21874aeea5SJeff Kirsher #include "net_driver.h" 22874aeea5SJeff Kirsher #include "efx.h" 23183233beSBen Hutchings #include "io.h" 24874aeea5SJeff Kirsher #include "nic.h" 25874aeea5SJeff Kirsher #include "workarounds.h" 26dfa50be9SBen Hutchings #include "ef10_regs.h" 27874aeea5SJeff Kirsher 28183233beSBen Hutchings #ifdef EFX_USE_PIO 29183233beSBen Hutchings 30183233beSBen Hutchings #define EFX_PIOBUF_SIZE_MAX ER_DZ_TX_PIOBUF_SIZE 31183233beSBen Hutchings #define EFX_PIOBUF_SIZE_DEF ALIGN(256, L1_CACHE_BYTES) 32183233beSBen Hutchings unsigned int efx_piobuf_size __read_mostly = EFX_PIOBUF_SIZE_DEF; 33183233beSBen Hutchings 34183233beSBen Hutchings #endif /* EFX_USE_PIO */ 35183233beSBen Hutchings 360fe5565bSBen Hutchings static inline unsigned int 370fe5565bSBen Hutchings efx_tx_queue_get_insert_index(const struct efx_tx_queue *tx_queue) 380fe5565bSBen Hutchings { 390fe5565bSBen Hutchings return tx_queue->insert_count & tx_queue->ptr_mask; 400fe5565bSBen Hutchings } 410fe5565bSBen Hutchings 420fe5565bSBen Hutchings static inline struct efx_tx_buffer * 430fe5565bSBen Hutchings __efx_tx_queue_get_insert_buffer(const struct efx_tx_queue *tx_queue) 440fe5565bSBen Hutchings { 450fe5565bSBen Hutchings return &tx_queue->buffer[efx_tx_queue_get_insert_index(tx_queue)]; 460fe5565bSBen Hutchings } 470fe5565bSBen Hutchings 480fe5565bSBen Hutchings static inline struct efx_tx_buffer * 490fe5565bSBen Hutchings efx_tx_queue_get_insert_buffer(const struct efx_tx_queue *tx_queue) 500fe5565bSBen Hutchings { 510fe5565bSBen Hutchings struct efx_tx_buffer *buffer = 520fe5565bSBen Hutchings __efx_tx_queue_get_insert_buffer(tx_queue); 530fe5565bSBen Hutchings 540fe5565bSBen Hutchings EFX_BUG_ON_PARANOID(buffer->len); 550fe5565bSBen Hutchings EFX_BUG_ON_PARANOID(buffer->flags); 560fe5565bSBen Hutchings EFX_BUG_ON_PARANOID(buffer->unmap_len); 570fe5565bSBen Hutchings 580fe5565bSBen Hutchings return buffer; 590fe5565bSBen Hutchings } 600fe5565bSBen Hutchings 61874aeea5SJeff Kirsher static void efx_dequeue_buffer(struct efx_tx_queue *tx_queue, 62c3940999STom Herbert struct efx_tx_buffer *buffer, 63c3940999STom Herbert unsigned int *pkts_compl, 64c3940999STom Herbert unsigned int *bytes_compl) 65874aeea5SJeff Kirsher { 66874aeea5SJeff Kirsher if (buffer->unmap_len) { 670e33d870SBen Hutchings struct device *dma_dev = &tx_queue->efx->pci_dev->dev; 682acdb92eSAlexandre Rames dma_addr_t unmap_addr = buffer->dma_addr - buffer->dma_offset; 697668ff9cSBen Hutchings if (buffer->flags & EFX_TX_BUF_MAP_SINGLE) 700e33d870SBen Hutchings dma_unmap_single(dma_dev, unmap_addr, buffer->unmap_len, 710e33d870SBen Hutchings DMA_TO_DEVICE); 72874aeea5SJeff Kirsher else 730e33d870SBen Hutchings dma_unmap_page(dma_dev, unmap_addr, buffer->unmap_len, 740e33d870SBen Hutchings DMA_TO_DEVICE); 75874aeea5SJeff Kirsher buffer->unmap_len = 0; 76874aeea5SJeff Kirsher } 77874aeea5SJeff Kirsher 787668ff9cSBen Hutchings if (buffer->flags & EFX_TX_BUF_SKB) { 79c3940999STom Herbert (*pkts_compl)++; 80c3940999STom Herbert (*bytes_compl) += buffer->skb->len; 81874aeea5SJeff Kirsher dev_kfree_skb_any((struct sk_buff *) buffer->skb); 82874aeea5SJeff Kirsher netif_vdbg(tx_queue->efx, tx_done, tx_queue->efx->net_dev, 83874aeea5SJeff Kirsher "TX queue %d transmission id %x complete\n", 84874aeea5SJeff Kirsher tx_queue->queue, tx_queue->read_count); 85f7251a9cSBen Hutchings } else if (buffer->flags & EFX_TX_BUF_HEAP) { 86f7251a9cSBen Hutchings kfree(buffer->heap_buf); 87874aeea5SJeff Kirsher } 887668ff9cSBen Hutchings 89f7251a9cSBen Hutchings buffer->len = 0; 90f7251a9cSBen Hutchings buffer->flags = 0; 91874aeea5SJeff Kirsher } 92874aeea5SJeff Kirsher 93874aeea5SJeff Kirsher static int efx_enqueue_skb_tso(struct efx_tx_queue *tx_queue, 94874aeea5SJeff Kirsher struct sk_buff *skb); 95874aeea5SJeff Kirsher 96874aeea5SJeff Kirsher static inline unsigned 97874aeea5SJeff Kirsher efx_max_tx_len(struct efx_nic *efx, dma_addr_t dma_addr) 98874aeea5SJeff Kirsher { 99874aeea5SJeff Kirsher /* Depending on the NIC revision, we can use descriptor 100874aeea5SJeff Kirsher * lengths up to 8K or 8K-1. However, since PCI Express 101874aeea5SJeff Kirsher * devices must split read requests at 4K boundaries, there is 102874aeea5SJeff Kirsher * little benefit from using descriptors that cross those 103874aeea5SJeff Kirsher * boundaries and we keep things simple by not doing so. 104874aeea5SJeff Kirsher */ 1055b6262d0SBen Hutchings unsigned len = (~dma_addr & (EFX_PAGE_SIZE - 1)) + 1; 106874aeea5SJeff Kirsher 107874aeea5SJeff Kirsher /* Work around hardware bug for unaligned buffers. */ 108874aeea5SJeff Kirsher if (EFX_WORKAROUND_5391(efx) && (dma_addr & 0xf)) 109874aeea5SJeff Kirsher len = min_t(unsigned, len, 512 - (dma_addr & 0xf)); 110874aeea5SJeff Kirsher 111874aeea5SJeff Kirsher return len; 112874aeea5SJeff Kirsher } 113874aeea5SJeff Kirsher 1147e6d06f0SBen Hutchings unsigned int efx_tx_max_skb_descs(struct efx_nic *efx) 1157e6d06f0SBen Hutchings { 1167e6d06f0SBen Hutchings /* Header and payload descriptor for each output segment, plus 1177e6d06f0SBen Hutchings * one for every input fragment boundary within a segment 1187e6d06f0SBen Hutchings */ 1197e6d06f0SBen Hutchings unsigned int max_descs = EFX_TSO_MAX_SEGS * 2 + MAX_SKB_FRAGS; 1207e6d06f0SBen Hutchings 121dfa50be9SBen Hutchings /* Possibly one more per segment for the alignment workaround, 122dfa50be9SBen Hutchings * or for option descriptors 123dfa50be9SBen Hutchings */ 124dfa50be9SBen Hutchings if (EFX_WORKAROUND_5391(efx) || efx_nic_rev(efx) >= EFX_REV_HUNT_A0) 1257e6d06f0SBen Hutchings max_descs += EFX_TSO_MAX_SEGS; 1267e6d06f0SBen Hutchings 1277e6d06f0SBen Hutchings /* Possibly more for PCIe page boundaries within input fragments */ 1287e6d06f0SBen Hutchings if (PAGE_SIZE > EFX_PAGE_SIZE) 1297e6d06f0SBen Hutchings max_descs += max_t(unsigned int, MAX_SKB_FRAGS, 1307e6d06f0SBen Hutchings DIV_ROUND_UP(GSO_MAX_SIZE, EFX_PAGE_SIZE)); 1317e6d06f0SBen Hutchings 1327e6d06f0SBen Hutchings return max_descs; 1337e6d06f0SBen Hutchings } 1347e6d06f0SBen Hutchings 13514bf718fSBen Hutchings /* Get partner of a TX queue, seen as part of the same net core queue */ 13614bf718fSBen Hutchings static struct efx_tx_queue *efx_tx_queue_partner(struct efx_tx_queue *tx_queue) 13714bf718fSBen Hutchings { 13814bf718fSBen Hutchings if (tx_queue->queue & EFX_TXQ_TYPE_OFFLOAD) 13914bf718fSBen Hutchings return tx_queue - EFX_TXQ_TYPE_OFFLOAD; 14014bf718fSBen Hutchings else 14114bf718fSBen Hutchings return tx_queue + EFX_TXQ_TYPE_OFFLOAD; 14214bf718fSBen Hutchings } 14314bf718fSBen Hutchings 14414bf718fSBen Hutchings static void efx_tx_maybe_stop_queue(struct efx_tx_queue *txq1) 14514bf718fSBen Hutchings { 14614bf718fSBen Hutchings /* We need to consider both queues that the net core sees as one */ 14714bf718fSBen Hutchings struct efx_tx_queue *txq2 = efx_tx_queue_partner(txq1); 14814bf718fSBen Hutchings struct efx_nic *efx = txq1->efx; 14914bf718fSBen Hutchings unsigned int fill_level; 15014bf718fSBen Hutchings 15114bf718fSBen Hutchings fill_level = max(txq1->insert_count - txq1->old_read_count, 15214bf718fSBen Hutchings txq2->insert_count - txq2->old_read_count); 15314bf718fSBen Hutchings if (likely(fill_level < efx->txq_stop_thresh)) 15414bf718fSBen Hutchings return; 15514bf718fSBen Hutchings 15614bf718fSBen Hutchings /* We used the stale old_read_count above, which gives us a 15714bf718fSBen Hutchings * pessimistic estimate of the fill level (which may even 15814bf718fSBen Hutchings * validly be >= efx->txq_entries). Now try again using 15914bf718fSBen Hutchings * read_count (more likely to be a cache miss). 16014bf718fSBen Hutchings * 16114bf718fSBen Hutchings * If we read read_count and then conditionally stop the 16214bf718fSBen Hutchings * queue, it is possible for the completion path to race with 16314bf718fSBen Hutchings * us and complete all outstanding descriptors in the middle, 16414bf718fSBen Hutchings * after which there will be no more completions to wake it. 16514bf718fSBen Hutchings * Therefore we stop the queue first, then read read_count 16614bf718fSBen Hutchings * (with a memory barrier to ensure the ordering), then 16714bf718fSBen Hutchings * restart the queue if the fill level turns out to be low 16814bf718fSBen Hutchings * enough. 16914bf718fSBen Hutchings */ 17014bf718fSBen Hutchings netif_tx_stop_queue(txq1->core_txq); 17114bf718fSBen Hutchings smp_mb(); 17214bf718fSBen Hutchings txq1->old_read_count = ACCESS_ONCE(txq1->read_count); 17314bf718fSBen Hutchings txq2->old_read_count = ACCESS_ONCE(txq2->read_count); 17414bf718fSBen Hutchings 17514bf718fSBen Hutchings fill_level = max(txq1->insert_count - txq1->old_read_count, 17614bf718fSBen Hutchings txq2->insert_count - txq2->old_read_count); 17714bf718fSBen Hutchings EFX_BUG_ON_PARANOID(fill_level >= efx->txq_entries); 17814bf718fSBen Hutchings if (likely(fill_level < efx->txq_stop_thresh)) { 17914bf718fSBen Hutchings smp_mb(); 18014bf718fSBen Hutchings if (likely(!efx->loopback_selftest)) 18114bf718fSBen Hutchings netif_tx_start_queue(txq1->core_txq); 18214bf718fSBen Hutchings } 18314bf718fSBen Hutchings } 18414bf718fSBen Hutchings 185ee45fd92SJon Cooper #ifdef EFX_USE_PIO 186ee45fd92SJon Cooper 187ee45fd92SJon Cooper struct efx_short_copy_buffer { 188ee45fd92SJon Cooper int used; 189ee45fd92SJon Cooper u8 buf[L1_CACHE_BYTES]; 190ee45fd92SJon Cooper }; 191ee45fd92SJon Cooper 192ee45fd92SJon Cooper /* Copy to PIO, respecting that writes to PIO buffers must be dword aligned. 193ee45fd92SJon Cooper * Advances piobuf pointer. Leaves additional data in the copy buffer. 194ee45fd92SJon Cooper */ 195ee45fd92SJon Cooper static void efx_memcpy_toio_aligned(struct efx_nic *efx, u8 __iomem **piobuf, 196ee45fd92SJon Cooper u8 *data, int len, 197ee45fd92SJon Cooper struct efx_short_copy_buffer *copy_buf) 198ee45fd92SJon Cooper { 199ee45fd92SJon Cooper int block_len = len & ~(sizeof(copy_buf->buf) - 1); 200ee45fd92SJon Cooper 2014984c237SBen Hutchings __iowrite64_copy(*piobuf, data, block_len >> 3); 202ee45fd92SJon Cooper *piobuf += block_len; 203ee45fd92SJon Cooper len -= block_len; 204ee45fd92SJon Cooper 205ee45fd92SJon Cooper if (len) { 206ee45fd92SJon Cooper data += block_len; 207ee45fd92SJon Cooper BUG_ON(copy_buf->used); 208ee45fd92SJon Cooper BUG_ON(len > sizeof(copy_buf->buf)); 209ee45fd92SJon Cooper memcpy(copy_buf->buf, data, len); 210ee45fd92SJon Cooper copy_buf->used = len; 211ee45fd92SJon Cooper } 212ee45fd92SJon Cooper } 213ee45fd92SJon Cooper 214ee45fd92SJon Cooper /* Copy to PIO, respecting dword alignment, popping data from copy buffer first. 215ee45fd92SJon Cooper * Advances piobuf pointer. Leaves additional data in the copy buffer. 216ee45fd92SJon Cooper */ 217ee45fd92SJon Cooper static void efx_memcpy_toio_aligned_cb(struct efx_nic *efx, u8 __iomem **piobuf, 218ee45fd92SJon Cooper u8 *data, int len, 219ee45fd92SJon Cooper struct efx_short_copy_buffer *copy_buf) 220ee45fd92SJon Cooper { 221ee45fd92SJon Cooper if (copy_buf->used) { 222ee45fd92SJon Cooper /* if the copy buffer is partially full, fill it up and write */ 223ee45fd92SJon Cooper int copy_to_buf = 224ee45fd92SJon Cooper min_t(int, sizeof(copy_buf->buf) - copy_buf->used, len); 225ee45fd92SJon Cooper 226ee45fd92SJon Cooper memcpy(copy_buf->buf + copy_buf->used, data, copy_to_buf); 227ee45fd92SJon Cooper copy_buf->used += copy_to_buf; 228ee45fd92SJon Cooper 229ee45fd92SJon Cooper /* if we didn't fill it up then we're done for now */ 230ee45fd92SJon Cooper if (copy_buf->used < sizeof(copy_buf->buf)) 231ee45fd92SJon Cooper return; 232ee45fd92SJon Cooper 2334984c237SBen Hutchings __iowrite64_copy(*piobuf, copy_buf->buf, 2344984c237SBen Hutchings sizeof(copy_buf->buf) >> 3); 235ee45fd92SJon Cooper *piobuf += sizeof(copy_buf->buf); 236ee45fd92SJon Cooper data += copy_to_buf; 237ee45fd92SJon Cooper len -= copy_to_buf; 238ee45fd92SJon Cooper copy_buf->used = 0; 239ee45fd92SJon Cooper } 240ee45fd92SJon Cooper 241ee45fd92SJon Cooper efx_memcpy_toio_aligned(efx, piobuf, data, len, copy_buf); 242ee45fd92SJon Cooper } 243ee45fd92SJon Cooper 244ee45fd92SJon Cooper static void efx_flush_copy_buffer(struct efx_nic *efx, u8 __iomem *piobuf, 245ee45fd92SJon Cooper struct efx_short_copy_buffer *copy_buf) 246ee45fd92SJon Cooper { 247ee45fd92SJon Cooper /* if there's anything in it, write the whole buffer, including junk */ 248ee45fd92SJon Cooper if (copy_buf->used) 2494984c237SBen Hutchings __iowrite64_copy(piobuf, copy_buf->buf, 2504984c237SBen Hutchings sizeof(copy_buf->buf) >> 3); 251ee45fd92SJon Cooper } 252ee45fd92SJon Cooper 253ee45fd92SJon Cooper /* Traverse skb structure and copy fragments in to PIO buffer. 254ee45fd92SJon Cooper * Advances piobuf pointer. 255ee45fd92SJon Cooper */ 256ee45fd92SJon Cooper static void efx_skb_copy_bits_to_pio(struct efx_nic *efx, struct sk_buff *skb, 257ee45fd92SJon Cooper u8 __iomem **piobuf, 258ee45fd92SJon Cooper struct efx_short_copy_buffer *copy_buf) 259ee45fd92SJon Cooper { 260ee45fd92SJon Cooper int i; 261ee45fd92SJon Cooper 262ee45fd92SJon Cooper efx_memcpy_toio_aligned(efx, piobuf, skb->data, skb_headlen(skb), 263ee45fd92SJon Cooper copy_buf); 264ee45fd92SJon Cooper 265ee45fd92SJon Cooper for (i = 0; i < skb_shinfo(skb)->nr_frags; ++i) { 266ee45fd92SJon Cooper skb_frag_t *f = &skb_shinfo(skb)->frags[i]; 267ee45fd92SJon Cooper u8 *vaddr; 268ee45fd92SJon Cooper 269ee45fd92SJon Cooper vaddr = kmap_atomic(skb_frag_page(f)); 270ee45fd92SJon Cooper 271ee45fd92SJon Cooper efx_memcpy_toio_aligned_cb(efx, piobuf, vaddr + f->page_offset, 272ee45fd92SJon Cooper skb_frag_size(f), copy_buf); 273ee45fd92SJon Cooper kunmap_atomic(vaddr); 274ee45fd92SJon Cooper } 275ee45fd92SJon Cooper 276ee45fd92SJon Cooper EFX_BUG_ON_PARANOID(skb_shinfo(skb)->frag_list); 277ee45fd92SJon Cooper } 278ee45fd92SJon Cooper 279ee45fd92SJon Cooper static struct efx_tx_buffer * 280ee45fd92SJon Cooper efx_enqueue_skb_pio(struct efx_tx_queue *tx_queue, struct sk_buff *skb) 281ee45fd92SJon Cooper { 282ee45fd92SJon Cooper struct efx_tx_buffer *buffer = 283ee45fd92SJon Cooper efx_tx_queue_get_insert_buffer(tx_queue); 284ee45fd92SJon Cooper u8 __iomem *piobuf = tx_queue->piobuf; 285ee45fd92SJon Cooper 286ee45fd92SJon Cooper /* Copy to PIO buffer. Ensure the writes are padded to the end 287ee45fd92SJon Cooper * of a cache line, as this is required for write-combining to be 288ee45fd92SJon Cooper * effective on at least x86. 289ee45fd92SJon Cooper */ 290ee45fd92SJon Cooper 291ee45fd92SJon Cooper if (skb_shinfo(skb)->nr_frags) { 292ee45fd92SJon Cooper /* The size of the copy buffer will ensure all writes 293ee45fd92SJon Cooper * are the size of a cache line. 294ee45fd92SJon Cooper */ 295ee45fd92SJon Cooper struct efx_short_copy_buffer copy_buf; 296ee45fd92SJon Cooper 297ee45fd92SJon Cooper copy_buf.used = 0; 298ee45fd92SJon Cooper 299ee45fd92SJon Cooper efx_skb_copy_bits_to_pio(tx_queue->efx, skb, 300ee45fd92SJon Cooper &piobuf, ©_buf); 301ee45fd92SJon Cooper efx_flush_copy_buffer(tx_queue->efx, piobuf, ©_buf); 302ee45fd92SJon Cooper } else { 303ee45fd92SJon Cooper /* Pad the write to the size of a cache line. 304ee45fd92SJon Cooper * We can do this because we know the skb_shared_info sruct is 305ee45fd92SJon Cooper * after the source, and the destination buffer is big enough. 306ee45fd92SJon Cooper */ 307ee45fd92SJon Cooper BUILD_BUG_ON(L1_CACHE_BYTES > 308ee45fd92SJon Cooper SKB_DATA_ALIGN(sizeof(struct skb_shared_info))); 3094984c237SBen Hutchings __iowrite64_copy(tx_queue->piobuf, skb->data, 3104984c237SBen Hutchings ALIGN(skb->len, L1_CACHE_BYTES) >> 3); 311ee45fd92SJon Cooper } 312ee45fd92SJon Cooper 313ee45fd92SJon Cooper EFX_POPULATE_QWORD_5(buffer->option, 314ee45fd92SJon Cooper ESF_DZ_TX_DESC_IS_OPT, 1, 315ee45fd92SJon Cooper ESF_DZ_TX_OPTION_TYPE, ESE_DZ_TX_OPTION_DESC_PIO, 316ee45fd92SJon Cooper ESF_DZ_TX_PIO_CONT, 0, 317ee45fd92SJon Cooper ESF_DZ_TX_PIO_BYTE_CNT, skb->len, 318ee45fd92SJon Cooper ESF_DZ_TX_PIO_BUF_ADDR, 319ee45fd92SJon Cooper tx_queue->piobuf_offset); 320ee45fd92SJon Cooper ++tx_queue->pio_packets; 321ee45fd92SJon Cooper ++tx_queue->insert_count; 322ee45fd92SJon Cooper return buffer; 323ee45fd92SJon Cooper } 324ee45fd92SJon Cooper #endif /* EFX_USE_PIO */ 325ee45fd92SJon Cooper 326874aeea5SJeff Kirsher /* 327874aeea5SJeff Kirsher * Add a socket buffer to a TX queue 328874aeea5SJeff Kirsher * 329874aeea5SJeff Kirsher * This maps all fragments of a socket buffer for DMA and adds them to 330874aeea5SJeff Kirsher * the TX queue. The queue's insert pointer will be incremented by 331874aeea5SJeff Kirsher * the number of fragments in the socket buffer. 332874aeea5SJeff Kirsher * 333874aeea5SJeff Kirsher * If any DMA mapping fails, any mapped fragments will be unmapped, 334874aeea5SJeff Kirsher * the queue's insert pointer will be restored to its original value. 335874aeea5SJeff Kirsher * 336874aeea5SJeff Kirsher * This function is split out from efx_hard_start_xmit to allow the 337874aeea5SJeff Kirsher * loopback test to direct packets via specific TX queues. 338874aeea5SJeff Kirsher * 33914bf718fSBen Hutchings * Returns NETDEV_TX_OK. 340874aeea5SJeff Kirsher * You must hold netif_tx_lock() to call this function. 341874aeea5SJeff Kirsher */ 342874aeea5SJeff Kirsher netdev_tx_t efx_enqueue_skb(struct efx_tx_queue *tx_queue, struct sk_buff *skb) 343874aeea5SJeff Kirsher { 344874aeea5SJeff Kirsher struct efx_nic *efx = tx_queue->efx; 3450e33d870SBen Hutchings struct device *dma_dev = &efx->pci_dev->dev; 346874aeea5SJeff Kirsher struct efx_tx_buffer *buffer; 347874aeea5SJeff Kirsher skb_frag_t *fragment; 3480fe5565bSBen Hutchings unsigned int len, unmap_len = 0; 349874aeea5SJeff Kirsher dma_addr_t dma_addr, unmap_addr = 0; 350874aeea5SJeff Kirsher unsigned int dma_len; 3517668ff9cSBen Hutchings unsigned short dma_flags; 35214bf718fSBen Hutchings int i = 0; 353874aeea5SJeff Kirsher 354874aeea5SJeff Kirsher EFX_BUG_ON_PARANOID(tx_queue->write_count != tx_queue->insert_count); 355874aeea5SJeff Kirsher 356874aeea5SJeff Kirsher if (skb_shinfo(skb)->gso_size) 357874aeea5SJeff Kirsher return efx_enqueue_skb_tso(tx_queue, skb); 358874aeea5SJeff Kirsher 359874aeea5SJeff Kirsher /* Get size of the initial fragment */ 360874aeea5SJeff Kirsher len = skb_headlen(skb); 361874aeea5SJeff Kirsher 362874aeea5SJeff Kirsher /* Pad if necessary */ 363874aeea5SJeff Kirsher if (EFX_WORKAROUND_15592(efx) && skb->len <= 32) { 364874aeea5SJeff Kirsher EFX_BUG_ON_PARANOID(skb->data_len); 365874aeea5SJeff Kirsher len = 32 + 1; 366874aeea5SJeff Kirsher if (skb_pad(skb, len - skb->len)) 367874aeea5SJeff Kirsher return NETDEV_TX_OK; 368874aeea5SJeff Kirsher } 369874aeea5SJeff Kirsher 370ee45fd92SJon Cooper /* Consider using PIO for short packets */ 371ee45fd92SJon Cooper #ifdef EFX_USE_PIO 372ee45fd92SJon Cooper if (skb->len <= efx_piobuf_size && tx_queue->piobuf && 373ee45fd92SJon Cooper efx_nic_tx_is_empty(tx_queue) && 374ee45fd92SJon Cooper efx_nic_tx_is_empty(efx_tx_queue_partner(tx_queue))) { 375ee45fd92SJon Cooper buffer = efx_enqueue_skb_pio(tx_queue, skb); 376ee45fd92SJon Cooper dma_flags = EFX_TX_BUF_OPTION; 377ee45fd92SJon Cooper goto finish_packet; 378ee45fd92SJon Cooper } 379ee45fd92SJon Cooper #endif 380ee45fd92SJon Cooper 3810e33d870SBen Hutchings /* Map for DMA. Use dma_map_single rather than dma_map_page 382874aeea5SJeff Kirsher * since this is more efficient on machines with sparse 383874aeea5SJeff Kirsher * memory. 384874aeea5SJeff Kirsher */ 3857668ff9cSBen Hutchings dma_flags = EFX_TX_BUF_MAP_SINGLE; 3860e33d870SBen Hutchings dma_addr = dma_map_single(dma_dev, skb->data, len, PCI_DMA_TODEVICE); 387874aeea5SJeff Kirsher 388874aeea5SJeff Kirsher /* Process all fragments */ 389874aeea5SJeff Kirsher while (1) { 3900e33d870SBen Hutchings if (unlikely(dma_mapping_error(dma_dev, dma_addr))) 3910e33d870SBen Hutchings goto dma_err; 392874aeea5SJeff Kirsher 393874aeea5SJeff Kirsher /* Store fields for marking in the per-fragment final 394874aeea5SJeff Kirsher * descriptor */ 395874aeea5SJeff Kirsher unmap_len = len; 396874aeea5SJeff Kirsher unmap_addr = dma_addr; 397874aeea5SJeff Kirsher 398874aeea5SJeff Kirsher /* Add to TX queue, splitting across DMA boundaries */ 399874aeea5SJeff Kirsher do { 4000fe5565bSBen Hutchings buffer = efx_tx_queue_get_insert_buffer(tx_queue); 401874aeea5SJeff Kirsher 402874aeea5SJeff Kirsher dma_len = efx_max_tx_len(efx, dma_addr); 403874aeea5SJeff Kirsher if (likely(dma_len >= len)) 404874aeea5SJeff Kirsher dma_len = len; 405874aeea5SJeff Kirsher 406874aeea5SJeff Kirsher /* Fill out per descriptor fields */ 407874aeea5SJeff Kirsher buffer->len = dma_len; 408874aeea5SJeff Kirsher buffer->dma_addr = dma_addr; 4097668ff9cSBen Hutchings buffer->flags = EFX_TX_BUF_CONT; 410874aeea5SJeff Kirsher len -= dma_len; 411874aeea5SJeff Kirsher dma_addr += dma_len; 412874aeea5SJeff Kirsher ++tx_queue->insert_count; 413874aeea5SJeff Kirsher } while (len); 414874aeea5SJeff Kirsher 415874aeea5SJeff Kirsher /* Transfer ownership of the unmapping to the final buffer */ 4167668ff9cSBen Hutchings buffer->flags = EFX_TX_BUF_CONT | dma_flags; 417874aeea5SJeff Kirsher buffer->unmap_len = unmap_len; 4182acdb92eSAlexandre Rames buffer->dma_offset = buffer->dma_addr - unmap_addr; 419874aeea5SJeff Kirsher unmap_len = 0; 420874aeea5SJeff Kirsher 421874aeea5SJeff Kirsher /* Get address and size of next fragment */ 422874aeea5SJeff Kirsher if (i >= skb_shinfo(skb)->nr_frags) 423874aeea5SJeff Kirsher break; 424874aeea5SJeff Kirsher fragment = &skb_shinfo(skb)->frags[i]; 4259e903e08SEric Dumazet len = skb_frag_size(fragment); 426874aeea5SJeff Kirsher i++; 427874aeea5SJeff Kirsher /* Map for DMA */ 4287668ff9cSBen Hutchings dma_flags = 0; 4290e33d870SBen Hutchings dma_addr = skb_frag_dma_map(dma_dev, fragment, 0, len, 4305d6bcdfeSIan Campbell DMA_TO_DEVICE); 431874aeea5SJeff Kirsher } 432874aeea5SJeff Kirsher 433874aeea5SJeff Kirsher /* Transfer ownership of the skb to the final buffer */ 434440b87eaSPaul Gortmaker #ifdef EFX_USE_PIO 435ee45fd92SJon Cooper finish_packet: 436440b87eaSPaul Gortmaker #endif 437874aeea5SJeff Kirsher buffer->skb = skb; 4387668ff9cSBen Hutchings buffer->flags = EFX_TX_BUF_SKB | dma_flags; 439874aeea5SJeff Kirsher 440c3940999STom Herbert netdev_tx_sent_queue(tx_queue->core_txq, skb->len); 441c3940999STom Herbert 442874aeea5SJeff Kirsher /* Pass off to hardware */ 443874aeea5SJeff Kirsher efx_nic_push_buffers(tx_queue); 444874aeea5SJeff Kirsher 4458ccf3800SAndrew Rybchenko tx_queue->tx_packets++; 4468ccf3800SAndrew Rybchenko 44714bf718fSBen Hutchings efx_tx_maybe_stop_queue(tx_queue); 44814bf718fSBen Hutchings 449874aeea5SJeff Kirsher return NETDEV_TX_OK; 450874aeea5SJeff Kirsher 4510e33d870SBen Hutchings dma_err: 452874aeea5SJeff Kirsher netif_err(efx, tx_err, efx->net_dev, 453874aeea5SJeff Kirsher " TX queue %d could not map skb with %d bytes %d " 454874aeea5SJeff Kirsher "fragments for DMA\n", tx_queue->queue, skb->len, 455874aeea5SJeff Kirsher skb_shinfo(skb)->nr_frags + 1); 456874aeea5SJeff Kirsher 457874aeea5SJeff Kirsher /* Mark the packet as transmitted, and free the SKB ourselves */ 458874aeea5SJeff Kirsher dev_kfree_skb_any(skb); 459874aeea5SJeff Kirsher 460874aeea5SJeff Kirsher /* Work backwards until we hit the original insert pointer value */ 461874aeea5SJeff Kirsher while (tx_queue->insert_count != tx_queue->write_count) { 462c3940999STom Herbert unsigned int pkts_compl = 0, bytes_compl = 0; 463874aeea5SJeff Kirsher --tx_queue->insert_count; 4640fe5565bSBen Hutchings buffer = __efx_tx_queue_get_insert_buffer(tx_queue); 465c3940999STom Herbert efx_dequeue_buffer(tx_queue, buffer, &pkts_compl, &bytes_compl); 466874aeea5SJeff Kirsher } 467874aeea5SJeff Kirsher 468874aeea5SJeff Kirsher /* Free the fragment we were mid-way through pushing */ 469874aeea5SJeff Kirsher if (unmap_len) { 4707668ff9cSBen Hutchings if (dma_flags & EFX_TX_BUF_MAP_SINGLE) 4710e33d870SBen Hutchings dma_unmap_single(dma_dev, unmap_addr, unmap_len, 4720e33d870SBen Hutchings DMA_TO_DEVICE); 473874aeea5SJeff Kirsher else 4740e33d870SBen Hutchings dma_unmap_page(dma_dev, unmap_addr, unmap_len, 4750e33d870SBen Hutchings DMA_TO_DEVICE); 476874aeea5SJeff Kirsher } 477874aeea5SJeff Kirsher 47814bf718fSBen Hutchings return NETDEV_TX_OK; 479874aeea5SJeff Kirsher } 480874aeea5SJeff Kirsher 481874aeea5SJeff Kirsher /* Remove packets from the TX queue 482874aeea5SJeff Kirsher * 483874aeea5SJeff Kirsher * This removes packets from the TX queue, up to and including the 484874aeea5SJeff Kirsher * specified index. 485874aeea5SJeff Kirsher */ 486874aeea5SJeff Kirsher static void efx_dequeue_buffers(struct efx_tx_queue *tx_queue, 487c3940999STom Herbert unsigned int index, 488c3940999STom Herbert unsigned int *pkts_compl, 489c3940999STom Herbert unsigned int *bytes_compl) 490874aeea5SJeff Kirsher { 491874aeea5SJeff Kirsher struct efx_nic *efx = tx_queue->efx; 492874aeea5SJeff Kirsher unsigned int stop_index, read_ptr; 493874aeea5SJeff Kirsher 494874aeea5SJeff Kirsher stop_index = (index + 1) & tx_queue->ptr_mask; 495874aeea5SJeff Kirsher read_ptr = tx_queue->read_count & tx_queue->ptr_mask; 496874aeea5SJeff Kirsher 497874aeea5SJeff Kirsher while (read_ptr != stop_index) { 498874aeea5SJeff Kirsher struct efx_tx_buffer *buffer = &tx_queue->buffer[read_ptr]; 499ba8977bdSBen Hutchings 500ba8977bdSBen Hutchings if (!(buffer->flags & EFX_TX_BUF_OPTION) && 501ba8977bdSBen Hutchings unlikely(buffer->len == 0)) { 502874aeea5SJeff Kirsher netif_err(efx, tx_err, efx->net_dev, 503874aeea5SJeff Kirsher "TX queue %d spurious TX completion id %x\n", 504874aeea5SJeff Kirsher tx_queue->queue, read_ptr); 505874aeea5SJeff Kirsher efx_schedule_reset(efx, RESET_TYPE_TX_SKIP); 506874aeea5SJeff Kirsher return; 507874aeea5SJeff Kirsher } 508874aeea5SJeff Kirsher 509c3940999STom Herbert efx_dequeue_buffer(tx_queue, buffer, pkts_compl, bytes_compl); 510874aeea5SJeff Kirsher 511874aeea5SJeff Kirsher ++tx_queue->read_count; 512874aeea5SJeff Kirsher read_ptr = tx_queue->read_count & tx_queue->ptr_mask; 513874aeea5SJeff Kirsher } 514874aeea5SJeff Kirsher } 515874aeea5SJeff Kirsher 516874aeea5SJeff Kirsher /* Initiate a packet transmission. We use one channel per CPU 517874aeea5SJeff Kirsher * (sharing when we have more CPUs than channels). On Falcon, the TX 518874aeea5SJeff Kirsher * completion events will be directed back to the CPU that transmitted 519874aeea5SJeff Kirsher * the packet, which should be cache-efficient. 520874aeea5SJeff Kirsher * 521874aeea5SJeff Kirsher * Context: non-blocking. 522874aeea5SJeff Kirsher * Note that returning anything other than NETDEV_TX_OK will cause the 523874aeea5SJeff Kirsher * OS to free the skb. 524874aeea5SJeff Kirsher */ 525874aeea5SJeff Kirsher netdev_tx_t efx_hard_start_xmit(struct sk_buff *skb, 526874aeea5SJeff Kirsher struct net_device *net_dev) 527874aeea5SJeff Kirsher { 528874aeea5SJeff Kirsher struct efx_nic *efx = netdev_priv(net_dev); 529874aeea5SJeff Kirsher struct efx_tx_queue *tx_queue; 530874aeea5SJeff Kirsher unsigned index, type; 531874aeea5SJeff Kirsher 532874aeea5SJeff Kirsher EFX_WARN_ON_PARANOID(!netif_device_present(net_dev)); 533874aeea5SJeff Kirsher 5347c236c43SStuart Hodgson /* PTP "event" packet */ 5357c236c43SStuart Hodgson if (unlikely(efx_xmit_with_hwtstamp(skb)) && 5367c236c43SStuart Hodgson unlikely(efx_ptp_is_ptp_tx(efx, skb))) { 5377c236c43SStuart Hodgson return efx_ptp_tx(efx, skb); 5387c236c43SStuart Hodgson } 5397c236c43SStuart Hodgson 540874aeea5SJeff Kirsher index = skb_get_queue_mapping(skb); 541874aeea5SJeff Kirsher type = skb->ip_summed == CHECKSUM_PARTIAL ? EFX_TXQ_TYPE_OFFLOAD : 0; 542874aeea5SJeff Kirsher if (index >= efx->n_tx_channels) { 543874aeea5SJeff Kirsher index -= efx->n_tx_channels; 544874aeea5SJeff Kirsher type |= EFX_TXQ_TYPE_HIGHPRI; 545874aeea5SJeff Kirsher } 546874aeea5SJeff Kirsher tx_queue = efx_get_tx_queue(efx, index, type); 547874aeea5SJeff Kirsher 548874aeea5SJeff Kirsher return efx_enqueue_skb(tx_queue, skb); 549874aeea5SJeff Kirsher } 550874aeea5SJeff Kirsher 551874aeea5SJeff Kirsher void efx_init_tx_queue_core_txq(struct efx_tx_queue *tx_queue) 552874aeea5SJeff Kirsher { 553874aeea5SJeff Kirsher struct efx_nic *efx = tx_queue->efx; 554874aeea5SJeff Kirsher 555874aeea5SJeff Kirsher /* Must be inverse of queue lookup in efx_hard_start_xmit() */ 556874aeea5SJeff Kirsher tx_queue->core_txq = 557874aeea5SJeff Kirsher netdev_get_tx_queue(efx->net_dev, 558874aeea5SJeff Kirsher tx_queue->queue / EFX_TXQ_TYPES + 559874aeea5SJeff Kirsher ((tx_queue->queue & EFX_TXQ_TYPE_HIGHPRI) ? 560874aeea5SJeff Kirsher efx->n_tx_channels : 0)); 561874aeea5SJeff Kirsher } 562874aeea5SJeff Kirsher 563874aeea5SJeff Kirsher int efx_setup_tc(struct net_device *net_dev, u8 num_tc) 564874aeea5SJeff Kirsher { 565874aeea5SJeff Kirsher struct efx_nic *efx = netdev_priv(net_dev); 566874aeea5SJeff Kirsher struct efx_channel *channel; 567874aeea5SJeff Kirsher struct efx_tx_queue *tx_queue; 568874aeea5SJeff Kirsher unsigned tc; 569874aeea5SJeff Kirsher int rc; 570874aeea5SJeff Kirsher 571874aeea5SJeff Kirsher if (efx_nic_rev(efx) < EFX_REV_FALCON_B0 || num_tc > EFX_MAX_TX_TC) 572874aeea5SJeff Kirsher return -EINVAL; 573874aeea5SJeff Kirsher 574874aeea5SJeff Kirsher if (num_tc == net_dev->num_tc) 575874aeea5SJeff Kirsher return 0; 576874aeea5SJeff Kirsher 577874aeea5SJeff Kirsher for (tc = 0; tc < num_tc; tc++) { 578874aeea5SJeff Kirsher net_dev->tc_to_txq[tc].offset = tc * efx->n_tx_channels; 579874aeea5SJeff Kirsher net_dev->tc_to_txq[tc].count = efx->n_tx_channels; 580874aeea5SJeff Kirsher } 581874aeea5SJeff Kirsher 582874aeea5SJeff Kirsher if (num_tc > net_dev->num_tc) { 583874aeea5SJeff Kirsher /* Initialise high-priority queues as necessary */ 584874aeea5SJeff Kirsher efx_for_each_channel(channel, efx) { 585874aeea5SJeff Kirsher efx_for_each_possible_channel_tx_queue(tx_queue, 586874aeea5SJeff Kirsher channel) { 587874aeea5SJeff Kirsher if (!(tx_queue->queue & EFX_TXQ_TYPE_HIGHPRI)) 588874aeea5SJeff Kirsher continue; 589874aeea5SJeff Kirsher if (!tx_queue->buffer) { 590874aeea5SJeff Kirsher rc = efx_probe_tx_queue(tx_queue); 591874aeea5SJeff Kirsher if (rc) 592874aeea5SJeff Kirsher return rc; 593874aeea5SJeff Kirsher } 594874aeea5SJeff Kirsher if (!tx_queue->initialised) 595874aeea5SJeff Kirsher efx_init_tx_queue(tx_queue); 596874aeea5SJeff Kirsher efx_init_tx_queue_core_txq(tx_queue); 597874aeea5SJeff Kirsher } 598874aeea5SJeff Kirsher } 599874aeea5SJeff Kirsher } else { 600874aeea5SJeff Kirsher /* Reduce number of classes before number of queues */ 601874aeea5SJeff Kirsher net_dev->num_tc = num_tc; 602874aeea5SJeff Kirsher } 603874aeea5SJeff Kirsher 604874aeea5SJeff Kirsher rc = netif_set_real_num_tx_queues(net_dev, 605874aeea5SJeff Kirsher max_t(int, num_tc, 1) * 606874aeea5SJeff Kirsher efx->n_tx_channels); 607874aeea5SJeff Kirsher if (rc) 608874aeea5SJeff Kirsher return rc; 609874aeea5SJeff Kirsher 610874aeea5SJeff Kirsher /* Do not destroy high-priority queues when they become 611874aeea5SJeff Kirsher * unused. We would have to flush them first, and it is 612874aeea5SJeff Kirsher * fairly difficult to flush a subset of TX queues. Leave 613874aeea5SJeff Kirsher * it to efx_fini_channels(). 614874aeea5SJeff Kirsher */ 615874aeea5SJeff Kirsher 616874aeea5SJeff Kirsher net_dev->num_tc = num_tc; 617874aeea5SJeff Kirsher return 0; 618874aeea5SJeff Kirsher } 619874aeea5SJeff Kirsher 620874aeea5SJeff Kirsher void efx_xmit_done(struct efx_tx_queue *tx_queue, unsigned int index) 621874aeea5SJeff Kirsher { 622874aeea5SJeff Kirsher unsigned fill_level; 623874aeea5SJeff Kirsher struct efx_nic *efx = tx_queue->efx; 62414bf718fSBen Hutchings struct efx_tx_queue *txq2; 625c3940999STom Herbert unsigned int pkts_compl = 0, bytes_compl = 0; 626874aeea5SJeff Kirsher 627874aeea5SJeff Kirsher EFX_BUG_ON_PARANOID(index > tx_queue->ptr_mask); 628874aeea5SJeff Kirsher 629c3940999STom Herbert efx_dequeue_buffers(tx_queue, index, &pkts_compl, &bytes_compl); 630c3940999STom Herbert netdev_tx_completed_queue(tx_queue->core_txq, pkts_compl, bytes_compl); 631874aeea5SJeff Kirsher 63202e12165SBen Hutchings if (pkts_compl > 1) 63302e12165SBen Hutchings ++tx_queue->merge_events; 63402e12165SBen Hutchings 63514bf718fSBen Hutchings /* See if we need to restart the netif queue. This memory 63614bf718fSBen Hutchings * barrier ensures that we write read_count (inside 63714bf718fSBen Hutchings * efx_dequeue_buffers()) before reading the queue status. 63814bf718fSBen Hutchings */ 639874aeea5SJeff Kirsher smp_mb(); 640874aeea5SJeff Kirsher if (unlikely(netif_tx_queue_stopped(tx_queue->core_txq)) && 641874aeea5SJeff Kirsher likely(efx->port_enabled) && 642874aeea5SJeff Kirsher likely(netif_device_present(efx->net_dev))) { 64314bf718fSBen Hutchings txq2 = efx_tx_queue_partner(tx_queue); 64414bf718fSBen Hutchings fill_level = max(tx_queue->insert_count - tx_queue->read_count, 64514bf718fSBen Hutchings txq2->insert_count - txq2->read_count); 64614bf718fSBen Hutchings if (fill_level <= efx->txq_wake_thresh) 647874aeea5SJeff Kirsher netif_tx_wake_queue(tx_queue->core_txq); 648874aeea5SJeff Kirsher } 649874aeea5SJeff Kirsher 650874aeea5SJeff Kirsher /* Check whether the hardware queue is now empty */ 651874aeea5SJeff Kirsher if ((int)(tx_queue->read_count - tx_queue->old_write_count) >= 0) { 652874aeea5SJeff Kirsher tx_queue->old_write_count = ACCESS_ONCE(tx_queue->write_count); 653874aeea5SJeff Kirsher if (tx_queue->read_count == tx_queue->old_write_count) { 654874aeea5SJeff Kirsher smp_mb(); 655874aeea5SJeff Kirsher tx_queue->empty_read_count = 656874aeea5SJeff Kirsher tx_queue->read_count | EFX_EMPTY_COUNT_VALID; 657874aeea5SJeff Kirsher } 658874aeea5SJeff Kirsher } 659874aeea5SJeff Kirsher } 660874aeea5SJeff Kirsher 661f7251a9cSBen Hutchings /* Size of page-based TSO header buffers. Larger blocks must be 662f7251a9cSBen Hutchings * allocated from the heap. 663f7251a9cSBen Hutchings */ 664f7251a9cSBen Hutchings #define TSOH_STD_SIZE 128 665f7251a9cSBen Hutchings #define TSOH_PER_PAGE (PAGE_SIZE / TSOH_STD_SIZE) 666f7251a9cSBen Hutchings 667f7251a9cSBen Hutchings /* At most half the descriptors in the queue at any time will refer to 668f7251a9cSBen Hutchings * a TSO header buffer, since they must always be followed by a 669f7251a9cSBen Hutchings * payload descriptor referring to an skb. 670f7251a9cSBen Hutchings */ 671f7251a9cSBen Hutchings static unsigned int efx_tsoh_page_count(struct efx_tx_queue *tx_queue) 672f7251a9cSBen Hutchings { 673f7251a9cSBen Hutchings return DIV_ROUND_UP(tx_queue->ptr_mask + 1, 2 * TSOH_PER_PAGE); 674f7251a9cSBen Hutchings } 675f7251a9cSBen Hutchings 676874aeea5SJeff Kirsher int efx_probe_tx_queue(struct efx_tx_queue *tx_queue) 677874aeea5SJeff Kirsher { 678874aeea5SJeff Kirsher struct efx_nic *efx = tx_queue->efx; 679874aeea5SJeff Kirsher unsigned int entries; 6807668ff9cSBen Hutchings int rc; 681874aeea5SJeff Kirsher 682874aeea5SJeff Kirsher /* Create the smallest power-of-two aligned ring */ 683874aeea5SJeff Kirsher entries = max(roundup_pow_of_two(efx->txq_entries), EFX_MIN_DMAQ_SIZE); 684874aeea5SJeff Kirsher EFX_BUG_ON_PARANOID(entries > EFX_MAX_DMAQ_SIZE); 685874aeea5SJeff Kirsher tx_queue->ptr_mask = entries - 1; 686874aeea5SJeff Kirsher 687874aeea5SJeff Kirsher netif_dbg(efx, probe, efx->net_dev, 688874aeea5SJeff Kirsher "creating TX queue %d size %#x mask %#x\n", 689874aeea5SJeff Kirsher tx_queue->queue, efx->txq_entries, tx_queue->ptr_mask); 690874aeea5SJeff Kirsher 691874aeea5SJeff Kirsher /* Allocate software ring */ 692c2e4e25aSThomas Meyer tx_queue->buffer = kcalloc(entries, sizeof(*tx_queue->buffer), 693874aeea5SJeff Kirsher GFP_KERNEL); 694874aeea5SJeff Kirsher if (!tx_queue->buffer) 695874aeea5SJeff Kirsher return -ENOMEM; 696874aeea5SJeff Kirsher 697f7251a9cSBen Hutchings if (tx_queue->queue & EFX_TXQ_TYPE_OFFLOAD) { 698f7251a9cSBen Hutchings tx_queue->tsoh_page = 699f7251a9cSBen Hutchings kcalloc(efx_tsoh_page_count(tx_queue), 700f7251a9cSBen Hutchings sizeof(tx_queue->tsoh_page[0]), GFP_KERNEL); 701f7251a9cSBen Hutchings if (!tx_queue->tsoh_page) { 702f7251a9cSBen Hutchings rc = -ENOMEM; 703f7251a9cSBen Hutchings goto fail1; 704f7251a9cSBen Hutchings } 705f7251a9cSBen Hutchings } 706f7251a9cSBen Hutchings 707874aeea5SJeff Kirsher /* Allocate hardware ring */ 708874aeea5SJeff Kirsher rc = efx_nic_probe_tx(tx_queue); 709874aeea5SJeff Kirsher if (rc) 710f7251a9cSBen Hutchings goto fail2; 711874aeea5SJeff Kirsher 712874aeea5SJeff Kirsher return 0; 713874aeea5SJeff Kirsher 714f7251a9cSBen Hutchings fail2: 715f7251a9cSBen Hutchings kfree(tx_queue->tsoh_page); 716f7251a9cSBen Hutchings tx_queue->tsoh_page = NULL; 717f7251a9cSBen Hutchings fail1: 718874aeea5SJeff Kirsher kfree(tx_queue->buffer); 719874aeea5SJeff Kirsher tx_queue->buffer = NULL; 720874aeea5SJeff Kirsher return rc; 721874aeea5SJeff Kirsher } 722874aeea5SJeff Kirsher 723874aeea5SJeff Kirsher void efx_init_tx_queue(struct efx_tx_queue *tx_queue) 724874aeea5SJeff Kirsher { 725874aeea5SJeff Kirsher netif_dbg(tx_queue->efx, drv, tx_queue->efx->net_dev, 726874aeea5SJeff Kirsher "initialising TX queue %d\n", tx_queue->queue); 727874aeea5SJeff Kirsher 728874aeea5SJeff Kirsher tx_queue->insert_count = 0; 729874aeea5SJeff Kirsher tx_queue->write_count = 0; 730874aeea5SJeff Kirsher tx_queue->old_write_count = 0; 731874aeea5SJeff Kirsher tx_queue->read_count = 0; 732874aeea5SJeff Kirsher tx_queue->old_read_count = 0; 733874aeea5SJeff Kirsher tx_queue->empty_read_count = 0 | EFX_EMPTY_COUNT_VALID; 734874aeea5SJeff Kirsher 735874aeea5SJeff Kirsher /* Set up TX descriptor ring */ 736874aeea5SJeff Kirsher efx_nic_init_tx(tx_queue); 737874aeea5SJeff Kirsher 738874aeea5SJeff Kirsher tx_queue->initialised = true; 739874aeea5SJeff Kirsher } 740874aeea5SJeff Kirsher 741e42c3d85SBen Hutchings void efx_fini_tx_queue(struct efx_tx_queue *tx_queue) 742874aeea5SJeff Kirsher { 743874aeea5SJeff Kirsher struct efx_tx_buffer *buffer; 744874aeea5SJeff Kirsher 745e42c3d85SBen Hutchings netif_dbg(tx_queue->efx, drv, tx_queue->efx->net_dev, 746e42c3d85SBen Hutchings "shutting down TX queue %d\n", tx_queue->queue); 747e42c3d85SBen Hutchings 748874aeea5SJeff Kirsher if (!tx_queue->buffer) 749874aeea5SJeff Kirsher return; 750874aeea5SJeff Kirsher 751874aeea5SJeff Kirsher /* Free any buffers left in the ring */ 752874aeea5SJeff Kirsher while (tx_queue->read_count != tx_queue->write_count) { 753c3940999STom Herbert unsigned int pkts_compl = 0, bytes_compl = 0; 754874aeea5SJeff Kirsher buffer = &tx_queue->buffer[tx_queue->read_count & tx_queue->ptr_mask]; 755c3940999STom Herbert efx_dequeue_buffer(tx_queue, buffer, &pkts_compl, &bytes_compl); 756874aeea5SJeff Kirsher 757874aeea5SJeff Kirsher ++tx_queue->read_count; 758874aeea5SJeff Kirsher } 759c3940999STom Herbert netdev_tx_reset_queue(tx_queue->core_txq); 760874aeea5SJeff Kirsher } 761874aeea5SJeff Kirsher 762874aeea5SJeff Kirsher void efx_remove_tx_queue(struct efx_tx_queue *tx_queue) 763874aeea5SJeff Kirsher { 764f7251a9cSBen Hutchings int i; 765f7251a9cSBen Hutchings 766874aeea5SJeff Kirsher if (!tx_queue->buffer) 767874aeea5SJeff Kirsher return; 768874aeea5SJeff Kirsher 769874aeea5SJeff Kirsher netif_dbg(tx_queue->efx, drv, tx_queue->efx->net_dev, 770874aeea5SJeff Kirsher "destroying TX queue %d\n", tx_queue->queue); 771874aeea5SJeff Kirsher efx_nic_remove_tx(tx_queue); 772874aeea5SJeff Kirsher 773f7251a9cSBen Hutchings if (tx_queue->tsoh_page) { 774f7251a9cSBen Hutchings for (i = 0; i < efx_tsoh_page_count(tx_queue); i++) 775f7251a9cSBen Hutchings efx_nic_free_buffer(tx_queue->efx, 776f7251a9cSBen Hutchings &tx_queue->tsoh_page[i]); 777f7251a9cSBen Hutchings kfree(tx_queue->tsoh_page); 778f7251a9cSBen Hutchings tx_queue->tsoh_page = NULL; 779f7251a9cSBen Hutchings } 780f7251a9cSBen Hutchings 781874aeea5SJeff Kirsher kfree(tx_queue->buffer); 782874aeea5SJeff Kirsher tx_queue->buffer = NULL; 783874aeea5SJeff Kirsher } 784874aeea5SJeff Kirsher 785874aeea5SJeff Kirsher 786874aeea5SJeff Kirsher /* Efx TCP segmentation acceleration. 787874aeea5SJeff Kirsher * 788874aeea5SJeff Kirsher * Why? Because by doing it here in the driver we can go significantly 789874aeea5SJeff Kirsher * faster than the GSO. 790874aeea5SJeff Kirsher * 791874aeea5SJeff Kirsher * Requires TX checksum offload support. 792874aeea5SJeff Kirsher */ 793874aeea5SJeff Kirsher 794874aeea5SJeff Kirsher #define PTR_DIFF(p1, p2) ((u8 *)(p1) - (u8 *)(p2)) 795874aeea5SJeff Kirsher 796874aeea5SJeff Kirsher /** 797874aeea5SJeff Kirsher * struct tso_state - TSO state for an SKB 798874aeea5SJeff Kirsher * @out_len: Remaining length in current segment 799874aeea5SJeff Kirsher * @seqnum: Current sequence number 800874aeea5SJeff Kirsher * @ipv4_id: Current IPv4 ID, host endian 801874aeea5SJeff Kirsher * @packet_space: Remaining space in current packet 802874aeea5SJeff Kirsher * @dma_addr: DMA address of current position 803874aeea5SJeff Kirsher * @in_len: Remaining length in current SKB fragment 804874aeea5SJeff Kirsher * @unmap_len: Length of SKB fragment 805874aeea5SJeff Kirsher * @unmap_addr: DMA address of SKB fragment 8067668ff9cSBen Hutchings * @dma_flags: TX buffer flags for DMA mapping - %EFX_TX_BUF_MAP_SINGLE or 0 807874aeea5SJeff Kirsher * @protocol: Network protocol (after any VLAN header) 8089714284fSBen Hutchings * @ip_off: Offset of IP header 8099714284fSBen Hutchings * @tcp_off: Offset of TCP header 810874aeea5SJeff Kirsher * @header_len: Number of bytes of header 81153cb13c6SBen Hutchings * @ip_base_len: IPv4 tot_len or IPv6 payload_len, before TCP payload 812dfa50be9SBen Hutchings * @header_dma_addr: Header DMA address, when using option descriptors 813dfa50be9SBen Hutchings * @header_unmap_len: Header DMA mapped length, or 0 if not using option 814dfa50be9SBen Hutchings * descriptors 815874aeea5SJeff Kirsher * 816874aeea5SJeff Kirsher * The state used during segmentation. It is put into this data structure 817874aeea5SJeff Kirsher * just to make it easy to pass into inline functions. 818874aeea5SJeff Kirsher */ 819874aeea5SJeff Kirsher struct tso_state { 820874aeea5SJeff Kirsher /* Output position */ 821874aeea5SJeff Kirsher unsigned out_len; 822874aeea5SJeff Kirsher unsigned seqnum; 823dfa50be9SBen Hutchings u16 ipv4_id; 824874aeea5SJeff Kirsher unsigned packet_space; 825874aeea5SJeff Kirsher 826874aeea5SJeff Kirsher /* Input position */ 827874aeea5SJeff Kirsher dma_addr_t dma_addr; 828874aeea5SJeff Kirsher unsigned in_len; 829874aeea5SJeff Kirsher unsigned unmap_len; 830874aeea5SJeff Kirsher dma_addr_t unmap_addr; 8317668ff9cSBen Hutchings unsigned short dma_flags; 832874aeea5SJeff Kirsher 833874aeea5SJeff Kirsher __be16 protocol; 8349714284fSBen Hutchings unsigned int ip_off; 8359714284fSBen Hutchings unsigned int tcp_off; 836874aeea5SJeff Kirsher unsigned header_len; 83753cb13c6SBen Hutchings unsigned int ip_base_len; 838dfa50be9SBen Hutchings dma_addr_t header_dma_addr; 839dfa50be9SBen Hutchings unsigned int header_unmap_len; 840874aeea5SJeff Kirsher }; 841874aeea5SJeff Kirsher 842874aeea5SJeff Kirsher 843874aeea5SJeff Kirsher /* 844874aeea5SJeff Kirsher * Verify that our various assumptions about sk_buffs and the conditions 845874aeea5SJeff Kirsher * under which TSO will be attempted hold true. Return the protocol number. 846874aeea5SJeff Kirsher */ 847874aeea5SJeff Kirsher static __be16 efx_tso_check_protocol(struct sk_buff *skb) 848874aeea5SJeff Kirsher { 849874aeea5SJeff Kirsher __be16 protocol = skb->protocol; 850874aeea5SJeff Kirsher 851874aeea5SJeff Kirsher EFX_BUG_ON_PARANOID(((struct ethhdr *)skb->data)->h_proto != 852874aeea5SJeff Kirsher protocol); 853874aeea5SJeff Kirsher if (protocol == htons(ETH_P_8021Q)) { 854874aeea5SJeff Kirsher struct vlan_ethhdr *veh = (struct vlan_ethhdr *)skb->data; 855874aeea5SJeff Kirsher protocol = veh->h_vlan_encapsulated_proto; 856874aeea5SJeff Kirsher } 857874aeea5SJeff Kirsher 858874aeea5SJeff Kirsher if (protocol == htons(ETH_P_IP)) { 859874aeea5SJeff Kirsher EFX_BUG_ON_PARANOID(ip_hdr(skb)->protocol != IPPROTO_TCP); 860874aeea5SJeff Kirsher } else { 861874aeea5SJeff Kirsher EFX_BUG_ON_PARANOID(protocol != htons(ETH_P_IPV6)); 862874aeea5SJeff Kirsher EFX_BUG_ON_PARANOID(ipv6_hdr(skb)->nexthdr != NEXTHDR_TCP); 863874aeea5SJeff Kirsher } 864874aeea5SJeff Kirsher EFX_BUG_ON_PARANOID((PTR_DIFF(tcp_hdr(skb), skb->data) 865874aeea5SJeff Kirsher + (tcp_hdr(skb)->doff << 2u)) > 866874aeea5SJeff Kirsher skb_headlen(skb)); 867874aeea5SJeff Kirsher 868874aeea5SJeff Kirsher return protocol; 869874aeea5SJeff Kirsher } 870874aeea5SJeff Kirsher 871f7251a9cSBen Hutchings static u8 *efx_tsoh_get_buffer(struct efx_tx_queue *tx_queue, 872f7251a9cSBen Hutchings struct efx_tx_buffer *buffer, unsigned int len) 873874aeea5SJeff Kirsher { 874f7251a9cSBen Hutchings u8 *result; 875874aeea5SJeff Kirsher 876f7251a9cSBen Hutchings EFX_BUG_ON_PARANOID(buffer->len); 877f7251a9cSBen Hutchings EFX_BUG_ON_PARANOID(buffer->flags); 878f7251a9cSBen Hutchings EFX_BUG_ON_PARANOID(buffer->unmap_len); 879874aeea5SJeff Kirsher 8800bdadad1SBen Hutchings if (likely(len <= TSOH_STD_SIZE - NET_IP_ALIGN)) { 881f7251a9cSBen Hutchings unsigned index = 882f7251a9cSBen Hutchings (tx_queue->insert_count & tx_queue->ptr_mask) / 2; 883f7251a9cSBen Hutchings struct efx_buffer *page_buf = 884f7251a9cSBen Hutchings &tx_queue->tsoh_page[index / TSOH_PER_PAGE]; 885f7251a9cSBen Hutchings unsigned offset = 8860bdadad1SBen Hutchings TSOH_STD_SIZE * (index % TSOH_PER_PAGE) + NET_IP_ALIGN; 887874aeea5SJeff Kirsher 888f7251a9cSBen Hutchings if (unlikely(!page_buf->addr) && 8890d19a540SBen Hutchings efx_nic_alloc_buffer(tx_queue->efx, page_buf, PAGE_SIZE, 8900d19a540SBen Hutchings GFP_ATOMIC)) 891874aeea5SJeff Kirsher return NULL; 892874aeea5SJeff Kirsher 893f7251a9cSBen Hutchings result = (u8 *)page_buf->addr + offset; 894f7251a9cSBen Hutchings buffer->dma_addr = page_buf->dma_addr + offset; 895f7251a9cSBen Hutchings buffer->flags = EFX_TX_BUF_CONT; 896f7251a9cSBen Hutchings } else { 897f7251a9cSBen Hutchings tx_queue->tso_long_headers++; 898f7251a9cSBen Hutchings 8990bdadad1SBen Hutchings buffer->heap_buf = kmalloc(NET_IP_ALIGN + len, GFP_ATOMIC); 900f7251a9cSBen Hutchings if (unlikely(!buffer->heap_buf)) 901874aeea5SJeff Kirsher return NULL; 9020bdadad1SBen Hutchings result = (u8 *)buffer->heap_buf + NET_IP_ALIGN; 903f7251a9cSBen Hutchings buffer->flags = EFX_TX_BUF_CONT | EFX_TX_BUF_HEAP; 904874aeea5SJeff Kirsher } 905874aeea5SJeff Kirsher 906f7251a9cSBen Hutchings buffer->len = len; 907874aeea5SJeff Kirsher 908f7251a9cSBen Hutchings return result; 909874aeea5SJeff Kirsher } 910874aeea5SJeff Kirsher 911874aeea5SJeff Kirsher /** 912874aeea5SJeff Kirsher * efx_tx_queue_insert - push descriptors onto the TX queue 913874aeea5SJeff Kirsher * @tx_queue: Efx TX queue 914874aeea5SJeff Kirsher * @dma_addr: DMA address of fragment 915874aeea5SJeff Kirsher * @len: Length of fragment 916874aeea5SJeff Kirsher * @final_buffer: The final buffer inserted into the queue 917874aeea5SJeff Kirsher * 91814bf718fSBen Hutchings * Push descriptors onto the TX queue. 919874aeea5SJeff Kirsher */ 92014bf718fSBen Hutchings static void efx_tx_queue_insert(struct efx_tx_queue *tx_queue, 921874aeea5SJeff Kirsher dma_addr_t dma_addr, unsigned len, 922874aeea5SJeff Kirsher struct efx_tx_buffer **final_buffer) 923874aeea5SJeff Kirsher { 924874aeea5SJeff Kirsher struct efx_tx_buffer *buffer; 925874aeea5SJeff Kirsher struct efx_nic *efx = tx_queue->efx; 9260fe5565bSBen Hutchings unsigned dma_len; 927874aeea5SJeff Kirsher 928874aeea5SJeff Kirsher EFX_BUG_ON_PARANOID(len <= 0); 929874aeea5SJeff Kirsher 930874aeea5SJeff Kirsher while (1) { 9310fe5565bSBen Hutchings buffer = efx_tx_queue_get_insert_buffer(tx_queue); 932874aeea5SJeff Kirsher ++tx_queue->insert_count; 933874aeea5SJeff Kirsher 934874aeea5SJeff Kirsher EFX_BUG_ON_PARANOID(tx_queue->insert_count - 935874aeea5SJeff Kirsher tx_queue->read_count >= 936874aeea5SJeff Kirsher efx->txq_entries); 937874aeea5SJeff Kirsher 938874aeea5SJeff Kirsher buffer->dma_addr = dma_addr; 939874aeea5SJeff Kirsher 940874aeea5SJeff Kirsher dma_len = efx_max_tx_len(efx, dma_addr); 941874aeea5SJeff Kirsher 942874aeea5SJeff Kirsher /* If there is enough space to send then do so */ 943874aeea5SJeff Kirsher if (dma_len >= len) 944874aeea5SJeff Kirsher break; 945874aeea5SJeff Kirsher 9467668ff9cSBen Hutchings buffer->len = dma_len; 9477668ff9cSBen Hutchings buffer->flags = EFX_TX_BUF_CONT; 948874aeea5SJeff Kirsher dma_addr += dma_len; 949874aeea5SJeff Kirsher len -= dma_len; 950874aeea5SJeff Kirsher } 951874aeea5SJeff Kirsher 952874aeea5SJeff Kirsher EFX_BUG_ON_PARANOID(!len); 953874aeea5SJeff Kirsher buffer->len = len; 954874aeea5SJeff Kirsher *final_buffer = buffer; 955874aeea5SJeff Kirsher } 956874aeea5SJeff Kirsher 957874aeea5SJeff Kirsher 958874aeea5SJeff Kirsher /* 959874aeea5SJeff Kirsher * Put a TSO header into the TX queue. 960874aeea5SJeff Kirsher * 961874aeea5SJeff Kirsher * This is special-cased because we know that it is small enough to fit in 962874aeea5SJeff Kirsher * a single fragment, and we know it doesn't cross a page boundary. It 963874aeea5SJeff Kirsher * also allows us to not worry about end-of-packet etc. 964874aeea5SJeff Kirsher */ 965f7251a9cSBen Hutchings static int efx_tso_put_header(struct efx_tx_queue *tx_queue, 966f7251a9cSBen Hutchings struct efx_tx_buffer *buffer, u8 *header) 967874aeea5SJeff Kirsher { 968f7251a9cSBen Hutchings if (unlikely(buffer->flags & EFX_TX_BUF_HEAP)) { 969f7251a9cSBen Hutchings buffer->dma_addr = dma_map_single(&tx_queue->efx->pci_dev->dev, 970f7251a9cSBen Hutchings header, buffer->len, 971f7251a9cSBen Hutchings DMA_TO_DEVICE); 972f7251a9cSBen Hutchings if (unlikely(dma_mapping_error(&tx_queue->efx->pci_dev->dev, 973f7251a9cSBen Hutchings buffer->dma_addr))) { 974f7251a9cSBen Hutchings kfree(buffer->heap_buf); 975f7251a9cSBen Hutchings buffer->len = 0; 976f7251a9cSBen Hutchings buffer->flags = 0; 977f7251a9cSBen Hutchings return -ENOMEM; 978f7251a9cSBen Hutchings } 979f7251a9cSBen Hutchings buffer->unmap_len = buffer->len; 9802acdb92eSAlexandre Rames buffer->dma_offset = 0; 981f7251a9cSBen Hutchings buffer->flags |= EFX_TX_BUF_MAP_SINGLE; 982f7251a9cSBen Hutchings } 983874aeea5SJeff Kirsher 984874aeea5SJeff Kirsher ++tx_queue->insert_count; 985f7251a9cSBen Hutchings return 0; 986874aeea5SJeff Kirsher } 987874aeea5SJeff Kirsher 988874aeea5SJeff Kirsher 989f7251a9cSBen Hutchings /* Remove buffers put into a tx_queue. None of the buffers must have 990f7251a9cSBen Hutchings * an skb attached. 991f7251a9cSBen Hutchings */ 992874aeea5SJeff Kirsher static void efx_enqueue_unwind(struct efx_tx_queue *tx_queue) 993874aeea5SJeff Kirsher { 994874aeea5SJeff Kirsher struct efx_tx_buffer *buffer; 995874aeea5SJeff Kirsher 996874aeea5SJeff Kirsher /* Work backwards until we hit the original insert pointer value */ 997874aeea5SJeff Kirsher while (tx_queue->insert_count != tx_queue->write_count) { 998874aeea5SJeff Kirsher --tx_queue->insert_count; 9990fe5565bSBen Hutchings buffer = __efx_tx_queue_get_insert_buffer(tx_queue); 1000f7251a9cSBen Hutchings efx_dequeue_buffer(tx_queue, buffer, NULL, NULL); 1001874aeea5SJeff Kirsher } 1002874aeea5SJeff Kirsher } 1003874aeea5SJeff Kirsher 1004874aeea5SJeff Kirsher 1005874aeea5SJeff Kirsher /* Parse the SKB header and initialise state. */ 1006c78c39e6SBen Hutchings static int tso_start(struct tso_state *st, struct efx_nic *efx, 1007c78c39e6SBen Hutchings const struct sk_buff *skb) 1008874aeea5SJeff Kirsher { 100993413f50SBen Hutchings bool use_opt_desc = efx_nic_rev(efx) >= EFX_REV_HUNT_A0; 1010dfa50be9SBen Hutchings struct device *dma_dev = &efx->pci_dev->dev; 1011c78c39e6SBen Hutchings unsigned int header_len, in_len; 1012dfa50be9SBen Hutchings dma_addr_t dma_addr; 1013c78c39e6SBen Hutchings 10149714284fSBen Hutchings st->ip_off = skb_network_header(skb) - skb->data; 10159714284fSBen Hutchings st->tcp_off = skb_transport_header(skb) - skb->data; 1016c78c39e6SBen Hutchings header_len = st->tcp_off + (tcp_hdr(skb)->doff << 2u); 1017c78c39e6SBen Hutchings in_len = skb_headlen(skb) - header_len; 1018c78c39e6SBen Hutchings st->header_len = header_len; 1019c78c39e6SBen Hutchings st->in_len = in_len; 102053cb13c6SBen Hutchings if (st->protocol == htons(ETH_P_IP)) { 10219714284fSBen Hutchings st->ip_base_len = st->header_len - st->ip_off; 1022874aeea5SJeff Kirsher st->ipv4_id = ntohs(ip_hdr(skb)->id); 102353cb13c6SBen Hutchings } else { 10249714284fSBen Hutchings st->ip_base_len = st->header_len - st->tcp_off; 1025874aeea5SJeff Kirsher st->ipv4_id = 0; 102653cb13c6SBen Hutchings } 1027874aeea5SJeff Kirsher st->seqnum = ntohl(tcp_hdr(skb)->seq); 1028874aeea5SJeff Kirsher 1029874aeea5SJeff Kirsher EFX_BUG_ON_PARANOID(tcp_hdr(skb)->urg); 1030874aeea5SJeff Kirsher EFX_BUG_ON_PARANOID(tcp_hdr(skb)->syn); 1031874aeea5SJeff Kirsher EFX_BUG_ON_PARANOID(tcp_hdr(skb)->rst); 1032874aeea5SJeff Kirsher 1033c78c39e6SBen Hutchings st->out_len = skb->len - header_len; 1034c78c39e6SBen Hutchings 103593413f50SBen Hutchings if (!use_opt_desc) { 1036dfa50be9SBen Hutchings st->header_unmap_len = 0; 1037dfa50be9SBen Hutchings 1038c78c39e6SBen Hutchings if (likely(in_len == 0)) { 10397668ff9cSBen Hutchings st->dma_flags = 0; 1040dfa50be9SBen Hutchings st->unmap_len = 0; 1041c78c39e6SBen Hutchings return 0; 1042c78c39e6SBen Hutchings } 1043c78c39e6SBen Hutchings 1044dfa50be9SBen Hutchings dma_addr = dma_map_single(dma_dev, skb->data + header_len, 1045dfa50be9SBen Hutchings in_len, DMA_TO_DEVICE); 1046c78c39e6SBen Hutchings st->dma_flags = EFX_TX_BUF_MAP_SINGLE; 1047dfa50be9SBen Hutchings st->dma_addr = dma_addr; 1048dfa50be9SBen Hutchings st->unmap_addr = dma_addr; 1049c78c39e6SBen Hutchings st->unmap_len = in_len; 1050dfa50be9SBen Hutchings } else { 1051dfa50be9SBen Hutchings dma_addr = dma_map_single(dma_dev, skb->data, 1052dfa50be9SBen Hutchings skb_headlen(skb), DMA_TO_DEVICE); 1053dfa50be9SBen Hutchings st->header_dma_addr = dma_addr; 1054dfa50be9SBen Hutchings st->header_unmap_len = skb_headlen(skb); 1055dfa50be9SBen Hutchings st->dma_flags = 0; 1056dfa50be9SBen Hutchings st->dma_addr = dma_addr + header_len; 1057dfa50be9SBen Hutchings st->unmap_len = 0; 1058dfa50be9SBen Hutchings } 1059dfa50be9SBen Hutchings 1060dfa50be9SBen Hutchings return unlikely(dma_mapping_error(dma_dev, dma_addr)) ? -ENOMEM : 0; 1061874aeea5SJeff Kirsher } 1062874aeea5SJeff Kirsher 1063874aeea5SJeff Kirsher static int tso_get_fragment(struct tso_state *st, struct efx_nic *efx, 1064874aeea5SJeff Kirsher skb_frag_t *frag) 1065874aeea5SJeff Kirsher { 10664a22c4c9SIan Campbell st->unmap_addr = skb_frag_dma_map(&efx->pci_dev->dev, frag, 0, 10679e903e08SEric Dumazet skb_frag_size(frag), DMA_TO_DEVICE); 10685d6bcdfeSIan Campbell if (likely(!dma_mapping_error(&efx->pci_dev->dev, st->unmap_addr))) { 10697668ff9cSBen Hutchings st->dma_flags = 0; 10709e903e08SEric Dumazet st->unmap_len = skb_frag_size(frag); 10719e903e08SEric Dumazet st->in_len = skb_frag_size(frag); 1072874aeea5SJeff Kirsher st->dma_addr = st->unmap_addr; 1073874aeea5SJeff Kirsher return 0; 1074874aeea5SJeff Kirsher } 1075874aeea5SJeff Kirsher return -ENOMEM; 1076874aeea5SJeff Kirsher } 1077874aeea5SJeff Kirsher 1078874aeea5SJeff Kirsher 1079874aeea5SJeff Kirsher /** 1080874aeea5SJeff Kirsher * tso_fill_packet_with_fragment - form descriptors for the current fragment 1081874aeea5SJeff Kirsher * @tx_queue: Efx TX queue 1082874aeea5SJeff Kirsher * @skb: Socket buffer 1083874aeea5SJeff Kirsher * @st: TSO state 1084874aeea5SJeff Kirsher * 1085874aeea5SJeff Kirsher * Form descriptors for the current fragment, until we reach the end 108614bf718fSBen Hutchings * of fragment or end-of-packet. 1087874aeea5SJeff Kirsher */ 108814bf718fSBen Hutchings static void tso_fill_packet_with_fragment(struct efx_tx_queue *tx_queue, 1089874aeea5SJeff Kirsher const struct sk_buff *skb, 1090874aeea5SJeff Kirsher struct tso_state *st) 1091874aeea5SJeff Kirsher { 1092874aeea5SJeff Kirsher struct efx_tx_buffer *buffer; 109314bf718fSBen Hutchings int n; 1094874aeea5SJeff Kirsher 1095874aeea5SJeff Kirsher if (st->in_len == 0) 109614bf718fSBen Hutchings return; 1097874aeea5SJeff Kirsher if (st->packet_space == 0) 109814bf718fSBen Hutchings return; 1099874aeea5SJeff Kirsher 1100874aeea5SJeff Kirsher EFX_BUG_ON_PARANOID(st->in_len <= 0); 1101874aeea5SJeff Kirsher EFX_BUG_ON_PARANOID(st->packet_space <= 0); 1102874aeea5SJeff Kirsher 1103874aeea5SJeff Kirsher n = min(st->in_len, st->packet_space); 1104874aeea5SJeff Kirsher 1105874aeea5SJeff Kirsher st->packet_space -= n; 1106874aeea5SJeff Kirsher st->out_len -= n; 1107874aeea5SJeff Kirsher st->in_len -= n; 1108874aeea5SJeff Kirsher 110914bf718fSBen Hutchings efx_tx_queue_insert(tx_queue, st->dma_addr, n, &buffer); 111014bf718fSBen Hutchings 11117668ff9cSBen Hutchings if (st->out_len == 0) { 1112874aeea5SJeff Kirsher /* Transfer ownership of the skb */ 1113874aeea5SJeff Kirsher buffer->skb = skb; 11147668ff9cSBen Hutchings buffer->flags = EFX_TX_BUF_SKB; 11157668ff9cSBen Hutchings } else if (st->packet_space != 0) { 11167668ff9cSBen Hutchings buffer->flags = EFX_TX_BUF_CONT; 11177668ff9cSBen Hutchings } 1118874aeea5SJeff Kirsher 1119874aeea5SJeff Kirsher if (st->in_len == 0) { 11200e33d870SBen Hutchings /* Transfer ownership of the DMA mapping */ 1121874aeea5SJeff Kirsher buffer->unmap_len = st->unmap_len; 11222acdb92eSAlexandre Rames buffer->dma_offset = buffer->unmap_len - buffer->len; 11237668ff9cSBen Hutchings buffer->flags |= st->dma_flags; 1124874aeea5SJeff Kirsher st->unmap_len = 0; 1125874aeea5SJeff Kirsher } 1126874aeea5SJeff Kirsher 1127874aeea5SJeff Kirsher st->dma_addr += n; 1128874aeea5SJeff Kirsher } 1129874aeea5SJeff Kirsher 1130874aeea5SJeff Kirsher 1131874aeea5SJeff Kirsher /** 1132874aeea5SJeff Kirsher * tso_start_new_packet - generate a new header and prepare for the new packet 1133874aeea5SJeff Kirsher * @tx_queue: Efx TX queue 1134874aeea5SJeff Kirsher * @skb: Socket buffer 1135874aeea5SJeff Kirsher * @st: TSO state 1136874aeea5SJeff Kirsher * 1137874aeea5SJeff Kirsher * Generate a new header and prepare for the new packet. Return 0 on 1138f7251a9cSBen Hutchings * success, or -%ENOMEM if failed to alloc header. 1139874aeea5SJeff Kirsher */ 1140874aeea5SJeff Kirsher static int tso_start_new_packet(struct efx_tx_queue *tx_queue, 1141874aeea5SJeff Kirsher const struct sk_buff *skb, 1142874aeea5SJeff Kirsher struct tso_state *st) 1143874aeea5SJeff Kirsher { 1144f7251a9cSBen Hutchings struct efx_tx_buffer *buffer = 11450fe5565bSBen Hutchings efx_tx_queue_get_insert_buffer(tx_queue); 1146dfa50be9SBen Hutchings bool is_last = st->out_len <= skb_shinfo(skb)->gso_size; 1147dfa50be9SBen Hutchings u8 tcp_flags_clear; 1148dfa50be9SBen Hutchings 1149dfa50be9SBen Hutchings if (!is_last) { 1150dfa50be9SBen Hutchings st->packet_space = skb_shinfo(skb)->gso_size; 1151dfa50be9SBen Hutchings tcp_flags_clear = 0x09; /* mask out FIN and PSH */ 1152dfa50be9SBen Hutchings } else { 1153dfa50be9SBen Hutchings st->packet_space = st->out_len; 1154dfa50be9SBen Hutchings tcp_flags_clear = 0x00; 1155dfa50be9SBen Hutchings } 1156dfa50be9SBen Hutchings 1157dfa50be9SBen Hutchings if (!st->header_unmap_len) { 1158dfa50be9SBen Hutchings /* Allocate and insert a DMA-mapped header buffer. */ 1159874aeea5SJeff Kirsher struct tcphdr *tsoh_th; 1160874aeea5SJeff Kirsher unsigned ip_length; 1161874aeea5SJeff Kirsher u8 *header; 1162f7251a9cSBen Hutchings int rc; 1163874aeea5SJeff Kirsher 1164f7251a9cSBen Hutchings header = efx_tsoh_get_buffer(tx_queue, buffer, st->header_len); 1165f7251a9cSBen Hutchings if (!header) 1166f7251a9cSBen Hutchings return -ENOMEM; 1167874aeea5SJeff Kirsher 11689714284fSBen Hutchings tsoh_th = (struct tcphdr *)(header + st->tcp_off); 1169874aeea5SJeff Kirsher 1170874aeea5SJeff Kirsher /* Copy and update the headers. */ 1171874aeea5SJeff Kirsher memcpy(header, skb->data, st->header_len); 1172874aeea5SJeff Kirsher 1173874aeea5SJeff Kirsher tsoh_th->seq = htonl(st->seqnum); 1174dfa50be9SBen Hutchings ((u8 *)tsoh_th)[13] &= ~tcp_flags_clear; 1175dfa50be9SBen Hutchings 117653cb13c6SBen Hutchings ip_length = st->ip_base_len + st->packet_space; 1177874aeea5SJeff Kirsher 1178874aeea5SJeff Kirsher if (st->protocol == htons(ETH_P_IP)) { 1179dfa50be9SBen Hutchings struct iphdr *tsoh_iph = 1180dfa50be9SBen Hutchings (struct iphdr *)(header + st->ip_off); 1181874aeea5SJeff Kirsher 1182874aeea5SJeff Kirsher tsoh_iph->tot_len = htons(ip_length); 1183874aeea5SJeff Kirsher tsoh_iph->id = htons(st->ipv4_id); 1184874aeea5SJeff Kirsher } else { 1185874aeea5SJeff Kirsher struct ipv6hdr *tsoh_iph = 11869714284fSBen Hutchings (struct ipv6hdr *)(header + st->ip_off); 1187874aeea5SJeff Kirsher 118853cb13c6SBen Hutchings tsoh_iph->payload_len = htons(ip_length); 1189874aeea5SJeff Kirsher } 1190874aeea5SJeff Kirsher 1191f7251a9cSBen Hutchings rc = efx_tso_put_header(tx_queue, buffer, header); 1192f7251a9cSBen Hutchings if (unlikely(rc)) 1193f7251a9cSBen Hutchings return rc; 1194dfa50be9SBen Hutchings } else { 1195dfa50be9SBen Hutchings /* Send the original headers with a TSO option descriptor 1196dfa50be9SBen Hutchings * in front 1197dfa50be9SBen Hutchings */ 1198dfa50be9SBen Hutchings u8 tcp_flags = ((u8 *)tcp_hdr(skb))[13] & ~tcp_flags_clear; 1199dfa50be9SBen Hutchings 1200dfa50be9SBen Hutchings buffer->flags = EFX_TX_BUF_OPTION; 1201dfa50be9SBen Hutchings buffer->len = 0; 1202dfa50be9SBen Hutchings buffer->unmap_len = 0; 1203dfa50be9SBen Hutchings EFX_POPULATE_QWORD_5(buffer->option, 1204dfa50be9SBen Hutchings ESF_DZ_TX_DESC_IS_OPT, 1, 1205dfa50be9SBen Hutchings ESF_DZ_TX_OPTION_TYPE, 1206dfa50be9SBen Hutchings ESE_DZ_TX_OPTION_DESC_TSO, 1207dfa50be9SBen Hutchings ESF_DZ_TX_TSO_TCP_FLAGS, tcp_flags, 1208dfa50be9SBen Hutchings ESF_DZ_TX_TSO_IP_ID, st->ipv4_id, 1209dfa50be9SBen Hutchings ESF_DZ_TX_TSO_TCP_SEQNO, st->seqnum); 1210dfa50be9SBen Hutchings ++tx_queue->insert_count; 1211dfa50be9SBen Hutchings 1212dfa50be9SBen Hutchings /* We mapped the headers in tso_start(). Unmap them 1213dfa50be9SBen Hutchings * when the last segment is completed. 1214dfa50be9SBen Hutchings */ 12150fe5565bSBen Hutchings buffer = efx_tx_queue_get_insert_buffer(tx_queue); 1216dfa50be9SBen Hutchings buffer->dma_addr = st->header_dma_addr; 1217dfa50be9SBen Hutchings buffer->len = st->header_len; 1218dfa50be9SBen Hutchings if (is_last) { 1219dfa50be9SBen Hutchings buffer->flags = EFX_TX_BUF_CONT | EFX_TX_BUF_MAP_SINGLE; 1220dfa50be9SBen Hutchings buffer->unmap_len = st->header_unmap_len; 12212acdb92eSAlexandre Rames buffer->dma_offset = 0; 1222dfa50be9SBen Hutchings /* Ensure we only unmap them once in case of a 1223dfa50be9SBen Hutchings * later DMA mapping error and rollback 1224dfa50be9SBen Hutchings */ 1225dfa50be9SBen Hutchings st->header_unmap_len = 0; 1226dfa50be9SBen Hutchings } else { 1227dfa50be9SBen Hutchings buffer->flags = EFX_TX_BUF_CONT; 1228dfa50be9SBen Hutchings buffer->unmap_len = 0; 1229dfa50be9SBen Hutchings } 1230dfa50be9SBen Hutchings ++tx_queue->insert_count; 1231dfa50be9SBen Hutchings } 1232dfa50be9SBen Hutchings 1233dfa50be9SBen Hutchings st->seqnum += skb_shinfo(skb)->gso_size; 1234dfa50be9SBen Hutchings 1235dfa50be9SBen Hutchings /* Linux leaves suitable gaps in the IP ID space for us to fill. */ 1236dfa50be9SBen Hutchings ++st->ipv4_id; 1237f7251a9cSBen Hutchings 1238874aeea5SJeff Kirsher ++tx_queue->tso_packets; 1239874aeea5SJeff Kirsher 12408ccf3800SAndrew Rybchenko ++tx_queue->tx_packets; 12418ccf3800SAndrew Rybchenko 1242874aeea5SJeff Kirsher return 0; 1243874aeea5SJeff Kirsher } 1244874aeea5SJeff Kirsher 1245874aeea5SJeff Kirsher 1246874aeea5SJeff Kirsher /** 1247874aeea5SJeff Kirsher * efx_enqueue_skb_tso - segment and transmit a TSO socket buffer 1248874aeea5SJeff Kirsher * @tx_queue: Efx TX queue 1249874aeea5SJeff Kirsher * @skb: Socket buffer 1250874aeea5SJeff Kirsher * 1251874aeea5SJeff Kirsher * Context: You must hold netif_tx_lock() to call this function. 1252874aeea5SJeff Kirsher * 1253874aeea5SJeff Kirsher * Add socket buffer @skb to @tx_queue, doing TSO or return != 0 if 1254874aeea5SJeff Kirsher * @skb was not enqueued. In all cases @skb is consumed. Return 125514bf718fSBen Hutchings * %NETDEV_TX_OK. 1256874aeea5SJeff Kirsher */ 1257874aeea5SJeff Kirsher static int efx_enqueue_skb_tso(struct efx_tx_queue *tx_queue, 1258874aeea5SJeff Kirsher struct sk_buff *skb) 1259874aeea5SJeff Kirsher { 1260874aeea5SJeff Kirsher struct efx_nic *efx = tx_queue->efx; 126114bf718fSBen Hutchings int frag_i, rc; 1262874aeea5SJeff Kirsher struct tso_state state; 1263874aeea5SJeff Kirsher 1264874aeea5SJeff Kirsher /* Find the packet protocol and sanity-check it */ 1265874aeea5SJeff Kirsher state.protocol = efx_tso_check_protocol(skb); 1266874aeea5SJeff Kirsher 1267874aeea5SJeff Kirsher EFX_BUG_ON_PARANOID(tx_queue->write_count != tx_queue->insert_count); 1268874aeea5SJeff Kirsher 1269c78c39e6SBen Hutchings rc = tso_start(&state, efx, skb); 1270c78c39e6SBen Hutchings if (rc) 1271c78c39e6SBen Hutchings goto mem_err; 1272874aeea5SJeff Kirsher 1273c78c39e6SBen Hutchings if (likely(state.in_len == 0)) { 1274874aeea5SJeff Kirsher /* Grab the first payload fragment. */ 1275874aeea5SJeff Kirsher EFX_BUG_ON_PARANOID(skb_shinfo(skb)->nr_frags < 1); 1276874aeea5SJeff Kirsher frag_i = 0; 1277874aeea5SJeff Kirsher rc = tso_get_fragment(&state, efx, 1278874aeea5SJeff Kirsher skb_shinfo(skb)->frags + frag_i); 1279874aeea5SJeff Kirsher if (rc) 1280874aeea5SJeff Kirsher goto mem_err; 1281874aeea5SJeff Kirsher } else { 1282c78c39e6SBen Hutchings /* Payload starts in the header area. */ 1283874aeea5SJeff Kirsher frag_i = -1; 1284874aeea5SJeff Kirsher } 1285874aeea5SJeff Kirsher 1286874aeea5SJeff Kirsher if (tso_start_new_packet(tx_queue, skb, &state) < 0) 1287874aeea5SJeff Kirsher goto mem_err; 1288874aeea5SJeff Kirsher 1289874aeea5SJeff Kirsher while (1) { 129014bf718fSBen Hutchings tso_fill_packet_with_fragment(tx_queue, skb, &state); 1291874aeea5SJeff Kirsher 1292874aeea5SJeff Kirsher /* Move onto the next fragment? */ 1293874aeea5SJeff Kirsher if (state.in_len == 0) { 1294874aeea5SJeff Kirsher if (++frag_i >= skb_shinfo(skb)->nr_frags) 1295874aeea5SJeff Kirsher /* End of payload reached. */ 1296874aeea5SJeff Kirsher break; 1297874aeea5SJeff Kirsher rc = tso_get_fragment(&state, efx, 1298874aeea5SJeff Kirsher skb_shinfo(skb)->frags + frag_i); 1299874aeea5SJeff Kirsher if (rc) 1300874aeea5SJeff Kirsher goto mem_err; 1301874aeea5SJeff Kirsher } 1302874aeea5SJeff Kirsher 1303874aeea5SJeff Kirsher /* Start at new packet? */ 1304874aeea5SJeff Kirsher if (state.packet_space == 0 && 1305874aeea5SJeff Kirsher tso_start_new_packet(tx_queue, skb, &state) < 0) 1306874aeea5SJeff Kirsher goto mem_err; 1307874aeea5SJeff Kirsher } 1308874aeea5SJeff Kirsher 1309449fa023SEric Dumazet netdev_tx_sent_queue(tx_queue->core_txq, skb->len); 1310449fa023SEric Dumazet 1311874aeea5SJeff Kirsher /* Pass off to hardware */ 1312874aeea5SJeff Kirsher efx_nic_push_buffers(tx_queue); 1313874aeea5SJeff Kirsher 131414bf718fSBen Hutchings efx_tx_maybe_stop_queue(tx_queue); 131514bf718fSBen Hutchings 1316874aeea5SJeff Kirsher tx_queue->tso_bursts++; 1317874aeea5SJeff Kirsher return NETDEV_TX_OK; 1318874aeea5SJeff Kirsher 1319874aeea5SJeff Kirsher mem_err: 1320874aeea5SJeff Kirsher netif_err(efx, tx_err, efx->net_dev, 13210e33d870SBen Hutchings "Out of memory for TSO headers, or DMA mapping error\n"); 1322874aeea5SJeff Kirsher dev_kfree_skb_any(skb); 1323874aeea5SJeff Kirsher 1324874aeea5SJeff Kirsher /* Free the DMA mapping we were in the process of writing out */ 1325874aeea5SJeff Kirsher if (state.unmap_len) { 13267668ff9cSBen Hutchings if (state.dma_flags & EFX_TX_BUF_MAP_SINGLE) 13270e33d870SBen Hutchings dma_unmap_single(&efx->pci_dev->dev, state.unmap_addr, 13280e33d870SBen Hutchings state.unmap_len, DMA_TO_DEVICE); 1329874aeea5SJeff Kirsher else 13300e33d870SBen Hutchings dma_unmap_page(&efx->pci_dev->dev, state.unmap_addr, 13310e33d870SBen Hutchings state.unmap_len, DMA_TO_DEVICE); 1332874aeea5SJeff Kirsher } 1333874aeea5SJeff Kirsher 1334dfa50be9SBen Hutchings /* Free the header DMA mapping, if using option descriptors */ 1335dfa50be9SBen Hutchings if (state.header_unmap_len) 1336dfa50be9SBen Hutchings dma_unmap_single(&efx->pci_dev->dev, state.header_dma_addr, 1337dfa50be9SBen Hutchings state.header_unmap_len, DMA_TO_DEVICE); 1338dfa50be9SBen Hutchings 1339874aeea5SJeff Kirsher efx_enqueue_unwind(tx_queue); 134014bf718fSBen Hutchings return NETDEV_TX_OK; 1341874aeea5SJeff Kirsher } 1342