Ben Hutchings | 8ceee66 | 2008-04-27 12:55:59 +0100 | [diff] [blame] | 1 | /**************************************************************************** |
| 2 | * Driver for Solarflare Solarstorm network controllers and boards |
| 3 | * Copyright 2005-2006 Fen Systems Ltd. |
Ben Hutchings | 0a6f40c | 2011-02-25 00:01:34 +0000 | [diff] [blame] | 4 | * Copyright 2005-2010 Solarflare Communications Inc. |
Ben Hutchings | 8ceee66 | 2008-04-27 12:55:59 +0100 | [diff] [blame] | 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify it |
| 7 | * under the terms of the GNU General Public License version 2 as published |
| 8 | * by the Free Software Foundation, incorporated herein by reference. |
| 9 | */ |
| 10 | |
| 11 | #include <linux/pci.h> |
| 12 | #include <linux/tcp.h> |
| 13 | #include <linux/ip.h> |
| 14 | #include <linux/in.h> |
Ben Hutchings | 738a8f4 | 2009-11-29 15:16:05 +0000 | [diff] [blame] | 15 | #include <linux/ipv6.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 16 | #include <linux/slab.h> |
Ben Hutchings | 738a8f4 | 2009-11-29 15:16:05 +0000 | [diff] [blame] | 17 | #include <net/ipv6.h> |
Ben Hutchings | 8ceee66 | 2008-04-27 12:55:59 +0100 | [diff] [blame] | 18 | #include <linux/if_ether.h> |
| 19 | #include <linux/highmem.h> |
| 20 | #include "net_driver.h" |
Ben Hutchings | 8ceee66 | 2008-04-27 12:55:59 +0100 | [diff] [blame] | 21 | #include "efx.h" |
Ben Hutchings | 744093c | 2009-11-29 15:12:08 +0000 | [diff] [blame] | 22 | #include "nic.h" |
Ben Hutchings | 8ceee66 | 2008-04-27 12:55:59 +0100 | [diff] [blame] | 23 | #include "workarounds.h" |
| 24 | |
Ben Hutchings | 4d56606 | 2008-09-01 12:47:12 +0100 | [diff] [blame] | 25 | static void efx_dequeue_buffer(struct efx_tx_queue *tx_queue, |
Tom Herbert | c394099 | 2011-11-28 16:33:43 +0000 | [diff] [blame] | 26 | struct efx_tx_buffer *buffer, |
| 27 | unsigned int *pkts_compl, |
| 28 | unsigned int *bytes_compl) |
Ben Hutchings | 8ceee66 | 2008-04-27 12:55:59 +0100 | [diff] [blame] | 29 | { |
| 30 | if (buffer->unmap_len) { |
Ben Hutchings | 0e33d87 | 2012-05-17 17:46:55 +0100 | [diff] [blame] | 31 | struct device *dma_dev = &tx_queue->efx->pci_dev->dev; |
Ben Hutchings | cc12dac | 2008-09-01 12:46:43 +0100 | [diff] [blame] | 32 | dma_addr_t unmap_addr = (buffer->dma_addr + buffer->len - |
| 33 | buffer->unmap_len); |
Ben Hutchings | 7668ff9 | 2012-05-17 20:52:20 +0100 | [diff] [blame] | 34 | if (buffer->flags & EFX_TX_BUF_MAP_SINGLE) |
Ben Hutchings | 0e33d87 | 2012-05-17 17:46:55 +0100 | [diff] [blame] | 35 | dma_unmap_single(dma_dev, unmap_addr, buffer->unmap_len, |
| 36 | DMA_TO_DEVICE); |
Ben Hutchings | 8ceee66 | 2008-04-27 12:55:59 +0100 | [diff] [blame] | 37 | else |
Ben Hutchings | 0e33d87 | 2012-05-17 17:46:55 +0100 | [diff] [blame] | 38 | dma_unmap_page(dma_dev, unmap_addr, buffer->unmap_len, |
| 39 | DMA_TO_DEVICE); |
Ben Hutchings | 8ceee66 | 2008-04-27 12:55:59 +0100 | [diff] [blame] | 40 | buffer->unmap_len = 0; |
Ben Hutchings | 8ceee66 | 2008-04-27 12:55:59 +0100 | [diff] [blame] | 41 | } |
| 42 | |
Ben Hutchings | 7668ff9 | 2012-05-17 20:52:20 +0100 | [diff] [blame] | 43 | if (buffer->flags & EFX_TX_BUF_SKB) { |
Tom Herbert | c394099 | 2011-11-28 16:33:43 +0000 | [diff] [blame] | 44 | (*pkts_compl)++; |
| 45 | (*bytes_compl) += buffer->skb->len; |
Ben Hutchings | 8ceee66 | 2008-04-27 12:55:59 +0100 | [diff] [blame] | 46 | dev_kfree_skb_any((struct sk_buff *) buffer->skb); |
Ben Hutchings | 62776d0 | 2010-06-23 11:30:07 +0000 | [diff] [blame] | 47 | netif_vdbg(tx_queue->efx, tx_done, tx_queue->efx->net_dev, |
| 48 | "TX queue %d transmission id %x complete\n", |
| 49 | tx_queue->queue, tx_queue->read_count); |
Ben Hutchings | f7251a9 | 2012-05-17 18:40:54 +0100 | [diff] [blame] | 50 | } else if (buffer->flags & EFX_TX_BUF_HEAP) { |
| 51 | kfree(buffer->heap_buf); |
Ben Hutchings | 8ceee66 | 2008-04-27 12:55:59 +0100 | [diff] [blame] | 52 | } |
Ben Hutchings | 7668ff9 | 2012-05-17 20:52:20 +0100 | [diff] [blame] | 53 | |
Ben Hutchings | f7251a9 | 2012-05-17 18:40:54 +0100 | [diff] [blame] | 54 | buffer->len = 0; |
| 55 | buffer->flags = 0; |
Ben Hutchings | 8ceee66 | 2008-04-27 12:55:59 +0100 | [diff] [blame] | 56 | } |
| 57 | |
Ben Hutchings | b9b39b6 | 2008-05-07 12:51:12 +0100 | [diff] [blame] | 58 | static int efx_enqueue_skb_tso(struct efx_tx_queue *tx_queue, |
Ben Hutchings | 740847d | 2008-09-01 12:48:23 +0100 | [diff] [blame] | 59 | struct sk_buff *skb); |
Ben Hutchings | 8ceee66 | 2008-04-27 12:55:59 +0100 | [diff] [blame] | 60 | |
Ben Hutchings | 63f1988 | 2009-10-23 08:31:20 +0000 | [diff] [blame] | 61 | static inline unsigned |
| 62 | efx_max_tx_len(struct efx_nic *efx, dma_addr_t dma_addr) |
| 63 | { |
| 64 | /* Depending on the NIC revision, we can use descriptor |
| 65 | * lengths up to 8K or 8K-1. However, since PCI Express |
| 66 | * devices must split read requests at 4K boundaries, there is |
| 67 | * little benefit from using descriptors that cross those |
| 68 | * boundaries and we keep things simple by not doing so. |
| 69 | */ |
Ben Hutchings | 5b6262d | 2012-02-02 21:21:15 +0000 | [diff] [blame] | 70 | unsigned len = (~dma_addr & (EFX_PAGE_SIZE - 1)) + 1; |
Ben Hutchings | 63f1988 | 2009-10-23 08:31:20 +0000 | [diff] [blame] | 71 | |
| 72 | /* Work around hardware bug for unaligned buffers. */ |
| 73 | if (EFX_WORKAROUND_5391(efx) && (dma_addr & 0xf)) |
| 74 | len = min_t(unsigned, len, 512 - (dma_addr & 0xf)); |
| 75 | |
| 76 | return len; |
| 77 | } |
| 78 | |
Ben Hutchings | 7e6d06f | 2012-07-30 15:57:44 +0000 | [diff] [blame] | 79 | unsigned int efx_tx_max_skb_descs(struct efx_nic *efx) |
| 80 | { |
| 81 | /* Header and payload descriptor for each output segment, plus |
| 82 | * one for every input fragment boundary within a segment |
| 83 | */ |
| 84 | unsigned int max_descs = EFX_TSO_MAX_SEGS * 2 + MAX_SKB_FRAGS; |
| 85 | |
| 86 | /* Possibly one more per segment for the alignment workaround */ |
| 87 | if (EFX_WORKAROUND_5391(efx)) |
| 88 | max_descs += EFX_TSO_MAX_SEGS; |
| 89 | |
| 90 | /* Possibly more for PCIe page boundaries within input fragments */ |
| 91 | if (PAGE_SIZE > EFX_PAGE_SIZE) |
| 92 | max_descs += max_t(unsigned int, MAX_SKB_FRAGS, |
| 93 | DIV_ROUND_UP(GSO_MAX_SIZE, EFX_PAGE_SIZE)); |
| 94 | |
| 95 | return max_descs; |
| 96 | } |
| 97 | |
Ben Hutchings | 14bf718fb | 2012-05-22 01:27:58 +0100 | [diff] [blame] | 98 | /* Get partner of a TX queue, seen as part of the same net core queue */ |
| 99 | static struct efx_tx_queue *efx_tx_queue_partner(struct efx_tx_queue *tx_queue) |
| 100 | { |
| 101 | if (tx_queue->queue & EFX_TXQ_TYPE_OFFLOAD) |
| 102 | return tx_queue - EFX_TXQ_TYPE_OFFLOAD; |
| 103 | else |
| 104 | return tx_queue + EFX_TXQ_TYPE_OFFLOAD; |
| 105 | } |
| 106 | |
| 107 | static void efx_tx_maybe_stop_queue(struct efx_tx_queue *txq1) |
| 108 | { |
| 109 | /* We need to consider both queues that the net core sees as one */ |
| 110 | struct efx_tx_queue *txq2 = efx_tx_queue_partner(txq1); |
| 111 | struct efx_nic *efx = txq1->efx; |
| 112 | unsigned int fill_level; |
| 113 | |
| 114 | fill_level = max(txq1->insert_count - txq1->old_read_count, |
| 115 | txq2->insert_count - txq2->old_read_count); |
| 116 | if (likely(fill_level < efx->txq_stop_thresh)) |
| 117 | return; |
| 118 | |
| 119 | /* We used the stale old_read_count above, which gives us a |
| 120 | * pessimistic estimate of the fill level (which may even |
| 121 | * validly be >= efx->txq_entries). Now try again using |
| 122 | * read_count (more likely to be a cache miss). |
| 123 | * |
| 124 | * If we read read_count and then conditionally stop the |
| 125 | * queue, it is possible for the completion path to race with |
| 126 | * us and complete all outstanding descriptors in the middle, |
| 127 | * after which there will be no more completions to wake it. |
| 128 | * Therefore we stop the queue first, then read read_count |
| 129 | * (with a memory barrier to ensure the ordering), then |
| 130 | * restart the queue if the fill level turns out to be low |
| 131 | * enough. |
| 132 | */ |
| 133 | netif_tx_stop_queue(txq1->core_txq); |
| 134 | smp_mb(); |
| 135 | txq1->old_read_count = ACCESS_ONCE(txq1->read_count); |
| 136 | txq2->old_read_count = ACCESS_ONCE(txq2->read_count); |
| 137 | |
| 138 | fill_level = max(txq1->insert_count - txq1->old_read_count, |
| 139 | txq2->insert_count - txq2->old_read_count); |
| 140 | EFX_BUG_ON_PARANOID(fill_level >= efx->txq_entries); |
| 141 | if (likely(fill_level < efx->txq_stop_thresh)) { |
| 142 | smp_mb(); |
| 143 | if (likely(!efx->loopback_selftest)) |
| 144 | netif_tx_start_queue(txq1->core_txq); |
| 145 | } |
| 146 | } |
| 147 | |
Ben Hutchings | 8ceee66 | 2008-04-27 12:55:59 +0100 | [diff] [blame] | 148 | /* |
| 149 | * Add a socket buffer to a TX queue |
| 150 | * |
| 151 | * This maps all fragments of a socket buffer for DMA and adds them to |
| 152 | * the TX queue. The queue's insert pointer will be incremented by |
| 153 | * the number of fragments in the socket buffer. |
| 154 | * |
| 155 | * If any DMA mapping fails, any mapped fragments will be unmapped, |
| 156 | * the queue's insert pointer will be restored to its original value. |
| 157 | * |
Ben Hutchings | 497f5ba | 2009-11-23 16:07:05 +0000 | [diff] [blame] | 158 | * This function is split out from efx_hard_start_xmit to allow the |
| 159 | * loopback test to direct packets via specific TX queues. |
| 160 | * |
Ben Hutchings | 14bf718fb | 2012-05-22 01:27:58 +0100 | [diff] [blame] | 161 | * Returns NETDEV_TX_OK. |
Ben Hutchings | 8ceee66 | 2008-04-27 12:55:59 +0100 | [diff] [blame] | 162 | * You must hold netif_tx_lock() to call this function. |
| 163 | */ |
Ben Hutchings | 497f5ba | 2009-11-23 16:07:05 +0000 | [diff] [blame] | 164 | netdev_tx_t efx_enqueue_skb(struct efx_tx_queue *tx_queue, struct sk_buff *skb) |
Ben Hutchings | 8ceee66 | 2008-04-27 12:55:59 +0100 | [diff] [blame] | 165 | { |
| 166 | struct efx_nic *efx = tx_queue->efx; |
Ben Hutchings | 0e33d87 | 2012-05-17 17:46:55 +0100 | [diff] [blame] | 167 | struct device *dma_dev = &efx->pci_dev->dev; |
Ben Hutchings | 8ceee66 | 2008-04-27 12:55:59 +0100 | [diff] [blame] | 168 | struct efx_tx_buffer *buffer; |
| 169 | skb_frag_t *fragment; |
Ben Hutchings | 14bf718fb | 2012-05-22 01:27:58 +0100 | [diff] [blame] | 170 | unsigned int len, unmap_len = 0, insert_ptr; |
Ben Hutchings | 8ceee66 | 2008-04-27 12:55:59 +0100 | [diff] [blame] | 171 | dma_addr_t dma_addr, unmap_addr = 0; |
| 172 | unsigned int dma_len; |
Ben Hutchings | 7668ff9 | 2012-05-17 20:52:20 +0100 | [diff] [blame] | 173 | unsigned short dma_flags; |
Ben Hutchings | 14bf718fb | 2012-05-22 01:27:58 +0100 | [diff] [blame] | 174 | int i = 0; |
Ben Hutchings | 8ceee66 | 2008-04-27 12:55:59 +0100 | [diff] [blame] | 175 | |
| 176 | EFX_BUG_ON_PARANOID(tx_queue->write_count != tx_queue->insert_count); |
| 177 | |
Ben Hutchings | 9bc183d | 2009-11-23 16:06:47 +0000 | [diff] [blame] | 178 | if (skb_shinfo(skb)->gso_size) |
Ben Hutchings | b9b39b6 | 2008-05-07 12:51:12 +0100 | [diff] [blame] | 179 | return efx_enqueue_skb_tso(tx_queue, skb); |
| 180 | |
Ben Hutchings | 8ceee66 | 2008-04-27 12:55:59 +0100 | [diff] [blame] | 181 | /* Get size of the initial fragment */ |
| 182 | len = skb_headlen(skb); |
| 183 | |
Ben Hutchings | bb145a9 | 2009-03-20 13:25:39 +0000 | [diff] [blame] | 184 | /* Pad if necessary */ |
| 185 | if (EFX_WORKAROUND_15592(efx) && skb->len <= 32) { |
| 186 | EFX_BUG_ON_PARANOID(skb->data_len); |
| 187 | len = 32 + 1; |
| 188 | if (skb_pad(skb, len - skb->len)) |
| 189 | return NETDEV_TX_OK; |
| 190 | } |
| 191 | |
Ben Hutchings | 0e33d87 | 2012-05-17 17:46:55 +0100 | [diff] [blame] | 192 | /* Map for DMA. Use dma_map_single rather than dma_map_page |
Ben Hutchings | 8ceee66 | 2008-04-27 12:55:59 +0100 | [diff] [blame] | 193 | * since this is more efficient on machines with sparse |
| 194 | * memory. |
| 195 | */ |
Ben Hutchings | 7668ff9 | 2012-05-17 20:52:20 +0100 | [diff] [blame] | 196 | dma_flags = EFX_TX_BUF_MAP_SINGLE; |
Ben Hutchings | 0e33d87 | 2012-05-17 17:46:55 +0100 | [diff] [blame] | 197 | dma_addr = dma_map_single(dma_dev, skb->data, len, PCI_DMA_TODEVICE); |
Ben Hutchings | 8ceee66 | 2008-04-27 12:55:59 +0100 | [diff] [blame] | 198 | |
| 199 | /* Process all fragments */ |
| 200 | while (1) { |
Ben Hutchings | 0e33d87 | 2012-05-17 17:46:55 +0100 | [diff] [blame] | 201 | if (unlikely(dma_mapping_error(dma_dev, dma_addr))) |
| 202 | goto dma_err; |
Ben Hutchings | 8ceee66 | 2008-04-27 12:55:59 +0100 | [diff] [blame] | 203 | |
| 204 | /* Store fields for marking in the per-fragment final |
| 205 | * descriptor */ |
| 206 | unmap_len = len; |
| 207 | unmap_addr = dma_addr; |
| 208 | |
| 209 | /* Add to TX queue, splitting across DMA boundaries */ |
| 210 | do { |
Steve Hodgson | ecc910f | 2010-09-10 06:42:22 +0000 | [diff] [blame] | 211 | insert_ptr = tx_queue->insert_count & tx_queue->ptr_mask; |
Ben Hutchings | 8ceee66 | 2008-04-27 12:55:59 +0100 | [diff] [blame] | 212 | buffer = &tx_queue->buffer[insert_ptr]; |
Ben Hutchings | 7668ff9 | 2012-05-17 20:52:20 +0100 | [diff] [blame] | 213 | EFX_BUG_ON_PARANOID(buffer->flags); |
Ben Hutchings | 8ceee66 | 2008-04-27 12:55:59 +0100 | [diff] [blame] | 214 | EFX_BUG_ON_PARANOID(buffer->len); |
Ben Hutchings | 8ceee66 | 2008-04-27 12:55:59 +0100 | [diff] [blame] | 215 | EFX_BUG_ON_PARANOID(buffer->unmap_len); |
| 216 | |
Ben Hutchings | 63f1988 | 2009-10-23 08:31:20 +0000 | [diff] [blame] | 217 | dma_len = efx_max_tx_len(efx, dma_addr); |
| 218 | if (likely(dma_len >= len)) |
Ben Hutchings | 8ceee66 | 2008-04-27 12:55:59 +0100 | [diff] [blame] | 219 | dma_len = len; |
| 220 | |
Ben Hutchings | 8ceee66 | 2008-04-27 12:55:59 +0100 | [diff] [blame] | 221 | /* Fill out per descriptor fields */ |
| 222 | buffer->len = dma_len; |
| 223 | buffer->dma_addr = dma_addr; |
Ben Hutchings | 7668ff9 | 2012-05-17 20:52:20 +0100 | [diff] [blame] | 224 | buffer->flags = EFX_TX_BUF_CONT; |
Ben Hutchings | 8ceee66 | 2008-04-27 12:55:59 +0100 | [diff] [blame] | 225 | len -= dma_len; |
| 226 | dma_addr += dma_len; |
| 227 | ++tx_queue->insert_count; |
| 228 | } while (len); |
| 229 | |
| 230 | /* Transfer ownership of the unmapping to the final buffer */ |
Ben Hutchings | 7668ff9 | 2012-05-17 20:52:20 +0100 | [diff] [blame] | 231 | buffer->flags = EFX_TX_BUF_CONT | dma_flags; |
Ben Hutchings | 8ceee66 | 2008-04-27 12:55:59 +0100 | [diff] [blame] | 232 | buffer->unmap_len = unmap_len; |
| 233 | unmap_len = 0; |
| 234 | |
| 235 | /* Get address and size of next fragment */ |
| 236 | if (i >= skb_shinfo(skb)->nr_frags) |
| 237 | break; |
| 238 | fragment = &skb_shinfo(skb)->frags[i]; |
Eric Dumazet | 9e903e0 | 2011-10-18 21:00:24 +0000 | [diff] [blame] | 239 | len = skb_frag_size(fragment); |
Ben Hutchings | 8ceee66 | 2008-04-27 12:55:59 +0100 | [diff] [blame] | 240 | i++; |
| 241 | /* Map for DMA */ |
Ben Hutchings | 7668ff9 | 2012-05-17 20:52:20 +0100 | [diff] [blame] | 242 | dma_flags = 0; |
Ben Hutchings | 0e33d87 | 2012-05-17 17:46:55 +0100 | [diff] [blame] | 243 | dma_addr = skb_frag_dma_map(dma_dev, fragment, 0, len, |
Ian Campbell | 5d6bcdf | 2011-10-06 11:10:48 +0100 | [diff] [blame] | 244 | DMA_TO_DEVICE); |
Ben Hutchings | 8ceee66 | 2008-04-27 12:55:59 +0100 | [diff] [blame] | 245 | } |
| 246 | |
| 247 | /* Transfer ownership of the skb to the final buffer */ |
| 248 | buffer->skb = skb; |
Ben Hutchings | 7668ff9 | 2012-05-17 20:52:20 +0100 | [diff] [blame] | 249 | buffer->flags = EFX_TX_BUF_SKB | dma_flags; |
Ben Hutchings | 8ceee66 | 2008-04-27 12:55:59 +0100 | [diff] [blame] | 250 | |
Tom Herbert | c394099 | 2011-11-28 16:33:43 +0000 | [diff] [blame] | 251 | netdev_tx_sent_queue(tx_queue->core_txq, skb->len); |
| 252 | |
Ben Hutchings | 8ceee66 | 2008-04-27 12:55:59 +0100 | [diff] [blame] | 253 | /* Pass off to hardware */ |
Ben Hutchings | 152b6a6 | 2009-11-29 03:43:56 +0000 | [diff] [blame] | 254 | efx_nic_push_buffers(tx_queue); |
Ben Hutchings | 8ceee66 | 2008-04-27 12:55:59 +0100 | [diff] [blame] | 255 | |
Ben Hutchings | 14bf718fb | 2012-05-22 01:27:58 +0100 | [diff] [blame] | 256 | efx_tx_maybe_stop_queue(tx_queue); |
| 257 | |
Ben Hutchings | 8ceee66 | 2008-04-27 12:55:59 +0100 | [diff] [blame] | 258 | return NETDEV_TX_OK; |
| 259 | |
Ben Hutchings | 0e33d87 | 2012-05-17 17:46:55 +0100 | [diff] [blame] | 260 | dma_err: |
Ben Hutchings | 62776d0 | 2010-06-23 11:30:07 +0000 | [diff] [blame] | 261 | netif_err(efx, tx_err, efx->net_dev, |
| 262 | " TX queue %d could not map skb with %d bytes %d " |
| 263 | "fragments for DMA\n", tx_queue->queue, skb->len, |
| 264 | skb_shinfo(skb)->nr_frags + 1); |
Ben Hutchings | 8ceee66 | 2008-04-27 12:55:59 +0100 | [diff] [blame] | 265 | |
| 266 | /* Mark the packet as transmitted, and free the SKB ourselves */ |
Ben Hutchings | 9bc183d | 2009-11-23 16:06:47 +0000 | [diff] [blame] | 267 | dev_kfree_skb_any(skb); |
Ben Hutchings | 8ceee66 | 2008-04-27 12:55:59 +0100 | [diff] [blame] | 268 | |
Ben Hutchings | 8ceee66 | 2008-04-27 12:55:59 +0100 | [diff] [blame] | 269 | /* Work backwards until we hit the original insert pointer value */ |
| 270 | while (tx_queue->insert_count != tx_queue->write_count) { |
Tom Herbert | c394099 | 2011-11-28 16:33:43 +0000 | [diff] [blame] | 271 | unsigned int pkts_compl = 0, bytes_compl = 0; |
Ben Hutchings | 8ceee66 | 2008-04-27 12:55:59 +0100 | [diff] [blame] | 272 | --tx_queue->insert_count; |
Steve Hodgson | ecc910f | 2010-09-10 06:42:22 +0000 | [diff] [blame] | 273 | insert_ptr = tx_queue->insert_count & tx_queue->ptr_mask; |
Ben Hutchings | 8ceee66 | 2008-04-27 12:55:59 +0100 | [diff] [blame] | 274 | buffer = &tx_queue->buffer[insert_ptr]; |
Tom Herbert | c394099 | 2011-11-28 16:33:43 +0000 | [diff] [blame] | 275 | efx_dequeue_buffer(tx_queue, buffer, &pkts_compl, &bytes_compl); |
Ben Hutchings | 8ceee66 | 2008-04-27 12:55:59 +0100 | [diff] [blame] | 276 | } |
| 277 | |
| 278 | /* Free the fragment we were mid-way through pushing */ |
Ben Hutchings | ecbd95c | 2008-09-01 12:46:40 +0100 | [diff] [blame] | 279 | if (unmap_len) { |
Ben Hutchings | 7668ff9 | 2012-05-17 20:52:20 +0100 | [diff] [blame] | 280 | if (dma_flags & EFX_TX_BUF_MAP_SINGLE) |
Ben Hutchings | 0e33d87 | 2012-05-17 17:46:55 +0100 | [diff] [blame] | 281 | dma_unmap_single(dma_dev, unmap_addr, unmap_len, |
| 282 | DMA_TO_DEVICE); |
Ben Hutchings | ecbd95c | 2008-09-01 12:46:40 +0100 | [diff] [blame] | 283 | else |
Ben Hutchings | 0e33d87 | 2012-05-17 17:46:55 +0100 | [diff] [blame] | 284 | dma_unmap_page(dma_dev, unmap_addr, unmap_len, |
| 285 | DMA_TO_DEVICE); |
Ben Hutchings | ecbd95c | 2008-09-01 12:46:40 +0100 | [diff] [blame] | 286 | } |
Ben Hutchings | 8ceee66 | 2008-04-27 12:55:59 +0100 | [diff] [blame] | 287 | |
Ben Hutchings | 14bf718fb | 2012-05-22 01:27:58 +0100 | [diff] [blame] | 288 | return NETDEV_TX_OK; |
Ben Hutchings | 8ceee66 | 2008-04-27 12:55:59 +0100 | [diff] [blame] | 289 | } |
| 290 | |
| 291 | /* Remove packets from the TX queue |
| 292 | * |
| 293 | * This removes packets from the TX queue, up to and including the |
| 294 | * specified index. |
| 295 | */ |
Ben Hutchings | 4d56606 | 2008-09-01 12:47:12 +0100 | [diff] [blame] | 296 | static void efx_dequeue_buffers(struct efx_tx_queue *tx_queue, |
Tom Herbert | c394099 | 2011-11-28 16:33:43 +0000 | [diff] [blame] | 297 | unsigned int index, |
| 298 | unsigned int *pkts_compl, |
| 299 | unsigned int *bytes_compl) |
Ben Hutchings | 8ceee66 | 2008-04-27 12:55:59 +0100 | [diff] [blame] | 300 | { |
| 301 | struct efx_nic *efx = tx_queue->efx; |
| 302 | unsigned int stop_index, read_ptr; |
Ben Hutchings | 8ceee66 | 2008-04-27 12:55:59 +0100 | [diff] [blame] | 303 | |
Steve Hodgson | ecc910f | 2010-09-10 06:42:22 +0000 | [diff] [blame] | 304 | stop_index = (index + 1) & tx_queue->ptr_mask; |
| 305 | read_ptr = tx_queue->read_count & tx_queue->ptr_mask; |
Ben Hutchings | 8ceee66 | 2008-04-27 12:55:59 +0100 | [diff] [blame] | 306 | |
| 307 | while (read_ptr != stop_index) { |
| 308 | struct efx_tx_buffer *buffer = &tx_queue->buffer[read_ptr]; |
| 309 | if (unlikely(buffer->len == 0)) { |
Ben Hutchings | 62776d0 | 2010-06-23 11:30:07 +0000 | [diff] [blame] | 310 | netif_err(efx, tx_err, efx->net_dev, |
| 311 | "TX queue %d spurious TX completion id %x\n", |
| 312 | tx_queue->queue, read_ptr); |
Ben Hutchings | 8ceee66 | 2008-04-27 12:55:59 +0100 | [diff] [blame] | 313 | efx_schedule_reset(efx, RESET_TYPE_TX_SKIP); |
| 314 | return; |
| 315 | } |
| 316 | |
Tom Herbert | c394099 | 2011-11-28 16:33:43 +0000 | [diff] [blame] | 317 | efx_dequeue_buffer(tx_queue, buffer, pkts_compl, bytes_compl); |
Ben Hutchings | 8ceee66 | 2008-04-27 12:55:59 +0100 | [diff] [blame] | 318 | |
| 319 | ++tx_queue->read_count; |
Steve Hodgson | ecc910f | 2010-09-10 06:42:22 +0000 | [diff] [blame] | 320 | read_ptr = tx_queue->read_count & tx_queue->ptr_mask; |
Ben Hutchings | 8ceee66 | 2008-04-27 12:55:59 +0100 | [diff] [blame] | 321 | } |
| 322 | } |
| 323 | |
Ben Hutchings | 8ceee66 | 2008-04-27 12:55:59 +0100 | [diff] [blame] | 324 | /* Initiate a packet transmission. We use one channel per CPU |
| 325 | * (sharing when we have more CPUs than channels). On Falcon, the TX |
| 326 | * completion events will be directed back to the CPU that transmitted |
| 327 | * the packet, which should be cache-efficient. |
| 328 | * |
| 329 | * Context: non-blocking. |
| 330 | * Note that returning anything other than NETDEV_TX_OK will cause the |
| 331 | * OS to free the skb. |
| 332 | */ |
Stephen Hemminger | 61357325 | 2009-08-31 19:50:58 +0000 | [diff] [blame] | 333 | netdev_tx_t efx_hard_start_xmit(struct sk_buff *skb, |
Ben Hutchings | 2d0cc56 | 2012-02-17 00:10:45 +0000 | [diff] [blame] | 334 | struct net_device *net_dev) |
Ben Hutchings | 8ceee66 | 2008-04-27 12:55:59 +0100 | [diff] [blame] | 335 | { |
Ben Hutchings | 767e468 | 2008-09-01 12:43:14 +0100 | [diff] [blame] | 336 | struct efx_nic *efx = netdev_priv(net_dev); |
Ben Hutchings | 60ac106 | 2008-09-01 12:44:59 +0100 | [diff] [blame] | 337 | struct efx_tx_queue *tx_queue; |
Ben Hutchings | 94b274b | 2011-01-10 21:18:20 +0000 | [diff] [blame] | 338 | unsigned index, type; |
Ben Hutchings | 60ac106 | 2008-09-01 12:44:59 +0100 | [diff] [blame] | 339 | |
Ben Hutchings | e4abce8 | 2011-05-16 18:51:24 +0100 | [diff] [blame] | 340 | EFX_WARN_ON_PARANOID(!netif_device_present(net_dev)); |
Ben Hutchings | a7ef593 | 2009-03-04 09:52:37 +0000 | [diff] [blame] | 341 | |
Ben Hutchings | 94b274b | 2011-01-10 21:18:20 +0000 | [diff] [blame] | 342 | index = skb_get_queue_mapping(skb); |
| 343 | type = skb->ip_summed == CHECKSUM_PARTIAL ? EFX_TXQ_TYPE_OFFLOAD : 0; |
| 344 | if (index >= efx->n_tx_channels) { |
| 345 | index -= efx->n_tx_channels; |
| 346 | type |= EFX_TXQ_TYPE_HIGHPRI; |
| 347 | } |
| 348 | tx_queue = efx_get_tx_queue(efx, index, type); |
Ben Hutchings | 60ac106 | 2008-09-01 12:44:59 +0100 | [diff] [blame] | 349 | |
Ben Hutchings | 497f5ba | 2009-11-23 16:07:05 +0000 | [diff] [blame] | 350 | return efx_enqueue_skb(tx_queue, skb); |
Ben Hutchings | 8ceee66 | 2008-04-27 12:55:59 +0100 | [diff] [blame] | 351 | } |
| 352 | |
Ben Hutchings | 60031fc | 2011-01-12 18:39:40 +0000 | [diff] [blame] | 353 | void efx_init_tx_queue_core_txq(struct efx_tx_queue *tx_queue) |
| 354 | { |
Ben Hutchings | 94b274b | 2011-01-10 21:18:20 +0000 | [diff] [blame] | 355 | struct efx_nic *efx = tx_queue->efx; |
| 356 | |
Ben Hutchings | 60031fc | 2011-01-12 18:39:40 +0000 | [diff] [blame] | 357 | /* Must be inverse of queue lookup in efx_hard_start_xmit() */ |
Ben Hutchings | 94b274b | 2011-01-10 21:18:20 +0000 | [diff] [blame] | 358 | tx_queue->core_txq = |
| 359 | netdev_get_tx_queue(efx->net_dev, |
| 360 | tx_queue->queue / EFX_TXQ_TYPES + |
| 361 | ((tx_queue->queue & EFX_TXQ_TYPE_HIGHPRI) ? |
| 362 | efx->n_tx_channels : 0)); |
| 363 | } |
| 364 | |
| 365 | int efx_setup_tc(struct net_device *net_dev, u8 num_tc) |
| 366 | { |
| 367 | struct efx_nic *efx = netdev_priv(net_dev); |
| 368 | struct efx_channel *channel; |
| 369 | struct efx_tx_queue *tx_queue; |
| 370 | unsigned tc; |
| 371 | int rc; |
| 372 | |
| 373 | if (efx_nic_rev(efx) < EFX_REV_FALCON_B0 || num_tc > EFX_MAX_TX_TC) |
| 374 | return -EINVAL; |
| 375 | |
| 376 | if (num_tc == net_dev->num_tc) |
| 377 | return 0; |
| 378 | |
| 379 | for (tc = 0; tc < num_tc; tc++) { |
| 380 | net_dev->tc_to_txq[tc].offset = tc * efx->n_tx_channels; |
| 381 | net_dev->tc_to_txq[tc].count = efx->n_tx_channels; |
| 382 | } |
| 383 | |
| 384 | if (num_tc > net_dev->num_tc) { |
| 385 | /* Initialise high-priority queues as necessary */ |
| 386 | efx_for_each_channel(channel, efx) { |
| 387 | efx_for_each_possible_channel_tx_queue(tx_queue, |
| 388 | channel) { |
| 389 | if (!(tx_queue->queue & EFX_TXQ_TYPE_HIGHPRI)) |
| 390 | continue; |
| 391 | if (!tx_queue->buffer) { |
| 392 | rc = efx_probe_tx_queue(tx_queue); |
| 393 | if (rc) |
| 394 | return rc; |
| 395 | } |
| 396 | if (!tx_queue->initialised) |
| 397 | efx_init_tx_queue(tx_queue); |
| 398 | efx_init_tx_queue_core_txq(tx_queue); |
| 399 | } |
| 400 | } |
| 401 | } else { |
| 402 | /* Reduce number of classes before number of queues */ |
| 403 | net_dev->num_tc = num_tc; |
| 404 | } |
| 405 | |
| 406 | rc = netif_set_real_num_tx_queues(net_dev, |
| 407 | max_t(int, num_tc, 1) * |
| 408 | efx->n_tx_channels); |
| 409 | if (rc) |
| 410 | return rc; |
| 411 | |
| 412 | /* Do not destroy high-priority queues when they become |
| 413 | * unused. We would have to flush them first, and it is |
| 414 | * fairly difficult to flush a subset of TX queues. Leave |
| 415 | * it to efx_fini_channels(). |
| 416 | */ |
| 417 | |
| 418 | net_dev->num_tc = num_tc; |
| 419 | return 0; |
Ben Hutchings | 60031fc | 2011-01-12 18:39:40 +0000 | [diff] [blame] | 420 | } |
| 421 | |
Ben Hutchings | 8ceee66 | 2008-04-27 12:55:59 +0100 | [diff] [blame] | 422 | void efx_xmit_done(struct efx_tx_queue *tx_queue, unsigned int index) |
| 423 | { |
| 424 | unsigned fill_level; |
| 425 | struct efx_nic *efx = tx_queue->efx; |
Ben Hutchings | 14bf718fb | 2012-05-22 01:27:58 +0100 | [diff] [blame] | 426 | struct efx_tx_queue *txq2; |
Tom Herbert | c394099 | 2011-11-28 16:33:43 +0000 | [diff] [blame] | 427 | unsigned int pkts_compl = 0, bytes_compl = 0; |
Ben Hutchings | 8ceee66 | 2008-04-27 12:55:59 +0100 | [diff] [blame] | 428 | |
Steve Hodgson | ecc910f | 2010-09-10 06:42:22 +0000 | [diff] [blame] | 429 | EFX_BUG_ON_PARANOID(index > tx_queue->ptr_mask); |
Ben Hutchings | 8ceee66 | 2008-04-27 12:55:59 +0100 | [diff] [blame] | 430 | |
Tom Herbert | c394099 | 2011-11-28 16:33:43 +0000 | [diff] [blame] | 431 | efx_dequeue_buffers(tx_queue, index, &pkts_compl, &bytes_compl); |
| 432 | netdev_tx_completed_queue(tx_queue->core_txq, pkts_compl, bytes_compl); |
Ben Hutchings | 8ceee66 | 2008-04-27 12:55:59 +0100 | [diff] [blame] | 433 | |
Ben Hutchings | 14bf718fb | 2012-05-22 01:27:58 +0100 | [diff] [blame] | 434 | /* See if we need to restart the netif queue. This memory |
| 435 | * barrier ensures that we write read_count (inside |
| 436 | * efx_dequeue_buffers()) before reading the queue status. |
| 437 | */ |
Ben Hutchings | 8ceee66 | 2008-04-27 12:55:59 +0100 | [diff] [blame] | 438 | smp_mb(); |
Ben Hutchings | c04bfc6 | 2010-12-10 01:24:16 +0000 | [diff] [blame] | 439 | if (unlikely(netif_tx_queue_stopped(tx_queue->core_txq)) && |
Neil Turton | 9d1aea6 | 2011-04-04 13:46:23 +0100 | [diff] [blame] | 440 | likely(efx->port_enabled) && |
Ben Hutchings | e4abce8 | 2011-05-16 18:51:24 +0100 | [diff] [blame] | 441 | likely(netif_device_present(efx->net_dev))) { |
Ben Hutchings | 14bf718fb | 2012-05-22 01:27:58 +0100 | [diff] [blame] | 442 | txq2 = efx_tx_queue_partner(tx_queue); |
| 443 | fill_level = max(tx_queue->insert_count - tx_queue->read_count, |
| 444 | txq2->insert_count - txq2->read_count); |
| 445 | if (fill_level <= efx->txq_wake_thresh) |
Ben Hutchings | c04bfc6 | 2010-12-10 01:24:16 +0000 | [diff] [blame] | 446 | netif_tx_wake_queue(tx_queue->core_txq); |
Ben Hutchings | 8ceee66 | 2008-04-27 12:55:59 +0100 | [diff] [blame] | 447 | } |
Ben Hutchings | cd38557 | 2010-11-15 23:53:11 +0000 | [diff] [blame] | 448 | |
| 449 | /* Check whether the hardware queue is now empty */ |
| 450 | if ((int)(tx_queue->read_count - tx_queue->old_write_count) >= 0) { |
| 451 | tx_queue->old_write_count = ACCESS_ONCE(tx_queue->write_count); |
| 452 | if (tx_queue->read_count == tx_queue->old_write_count) { |
| 453 | smp_mb(); |
| 454 | tx_queue->empty_read_count = |
| 455 | tx_queue->read_count | EFX_EMPTY_COUNT_VALID; |
| 456 | } |
| 457 | } |
Ben Hutchings | 8ceee66 | 2008-04-27 12:55:59 +0100 | [diff] [blame] | 458 | } |
| 459 | |
Ben Hutchings | f7251a9 | 2012-05-17 18:40:54 +0100 | [diff] [blame] | 460 | /* Size of page-based TSO header buffers. Larger blocks must be |
| 461 | * allocated from the heap. |
| 462 | */ |
| 463 | #define TSOH_STD_SIZE 128 |
| 464 | #define TSOH_PER_PAGE (PAGE_SIZE / TSOH_STD_SIZE) |
| 465 | |
| 466 | /* At most half the descriptors in the queue at any time will refer to |
| 467 | * a TSO header buffer, since they must always be followed by a |
| 468 | * payload descriptor referring to an skb. |
| 469 | */ |
| 470 | static unsigned int efx_tsoh_page_count(struct efx_tx_queue *tx_queue) |
| 471 | { |
| 472 | return DIV_ROUND_UP(tx_queue->ptr_mask + 1, 2 * TSOH_PER_PAGE); |
| 473 | } |
| 474 | |
Ben Hutchings | 8ceee66 | 2008-04-27 12:55:59 +0100 | [diff] [blame] | 475 | int efx_probe_tx_queue(struct efx_tx_queue *tx_queue) |
| 476 | { |
| 477 | struct efx_nic *efx = tx_queue->efx; |
Steve Hodgson | ecc910f | 2010-09-10 06:42:22 +0000 | [diff] [blame] | 478 | unsigned int entries; |
Ben Hutchings | 7668ff9 | 2012-05-17 20:52:20 +0100 | [diff] [blame] | 479 | int rc; |
Ben Hutchings | 8ceee66 | 2008-04-27 12:55:59 +0100 | [diff] [blame] | 480 | |
Steve Hodgson | ecc910f | 2010-09-10 06:42:22 +0000 | [diff] [blame] | 481 | /* Create the smallest power-of-two aligned ring */ |
| 482 | entries = max(roundup_pow_of_two(efx->txq_entries), EFX_MIN_DMAQ_SIZE); |
| 483 | EFX_BUG_ON_PARANOID(entries > EFX_MAX_DMAQ_SIZE); |
| 484 | tx_queue->ptr_mask = entries - 1; |
| 485 | |
| 486 | netif_dbg(efx, probe, efx->net_dev, |
| 487 | "creating TX queue %d size %#x mask %#x\n", |
| 488 | tx_queue->queue, efx->txq_entries, tx_queue->ptr_mask); |
Ben Hutchings | 8ceee66 | 2008-04-27 12:55:59 +0100 | [diff] [blame] | 489 | |
| 490 | /* Allocate software ring */ |
Thomas Meyer | c2e4e25 | 2011-12-02 12:36:13 +0000 | [diff] [blame] | 491 | tx_queue->buffer = kcalloc(entries, sizeof(*tx_queue->buffer), |
Steve Hodgson | ecc910f | 2010-09-10 06:42:22 +0000 | [diff] [blame] | 492 | GFP_KERNEL); |
Ben Hutchings | 60ac106 | 2008-09-01 12:44:59 +0100 | [diff] [blame] | 493 | if (!tx_queue->buffer) |
| 494 | return -ENOMEM; |
Ben Hutchings | 8ceee66 | 2008-04-27 12:55:59 +0100 | [diff] [blame] | 495 | |
Ben Hutchings | f7251a9 | 2012-05-17 18:40:54 +0100 | [diff] [blame] | 496 | if (tx_queue->queue & EFX_TXQ_TYPE_OFFLOAD) { |
| 497 | tx_queue->tsoh_page = |
| 498 | kcalloc(efx_tsoh_page_count(tx_queue), |
| 499 | sizeof(tx_queue->tsoh_page[0]), GFP_KERNEL); |
| 500 | if (!tx_queue->tsoh_page) { |
| 501 | rc = -ENOMEM; |
| 502 | goto fail1; |
| 503 | } |
| 504 | } |
| 505 | |
Ben Hutchings | 8ceee66 | 2008-04-27 12:55:59 +0100 | [diff] [blame] | 506 | /* Allocate hardware ring */ |
Ben Hutchings | 152b6a6 | 2009-11-29 03:43:56 +0000 | [diff] [blame] | 507 | rc = efx_nic_probe_tx(tx_queue); |
Ben Hutchings | 8ceee66 | 2008-04-27 12:55:59 +0100 | [diff] [blame] | 508 | if (rc) |
Ben Hutchings | f7251a9 | 2012-05-17 18:40:54 +0100 | [diff] [blame] | 509 | goto fail2; |
Ben Hutchings | 8ceee66 | 2008-04-27 12:55:59 +0100 | [diff] [blame] | 510 | |
| 511 | return 0; |
| 512 | |
Ben Hutchings | f7251a9 | 2012-05-17 18:40:54 +0100 | [diff] [blame] | 513 | fail2: |
| 514 | kfree(tx_queue->tsoh_page); |
| 515 | tx_queue->tsoh_page = NULL; |
| 516 | fail1: |
Ben Hutchings | 8ceee66 | 2008-04-27 12:55:59 +0100 | [diff] [blame] | 517 | kfree(tx_queue->buffer); |
| 518 | tx_queue->buffer = NULL; |
Ben Hutchings | 8ceee66 | 2008-04-27 12:55:59 +0100 | [diff] [blame] | 519 | return rc; |
| 520 | } |
| 521 | |
Ben Hutchings | bc3c90a | 2008-09-01 12:48:46 +0100 | [diff] [blame] | 522 | void efx_init_tx_queue(struct efx_tx_queue *tx_queue) |
Ben Hutchings | 8ceee66 | 2008-04-27 12:55:59 +0100 | [diff] [blame] | 523 | { |
Ben Hutchings | 62776d0 | 2010-06-23 11:30:07 +0000 | [diff] [blame] | 524 | netif_dbg(tx_queue->efx, drv, tx_queue->efx->net_dev, |
| 525 | "initialising TX queue %d\n", tx_queue->queue); |
Ben Hutchings | 8ceee66 | 2008-04-27 12:55:59 +0100 | [diff] [blame] | 526 | |
| 527 | tx_queue->insert_count = 0; |
| 528 | tx_queue->write_count = 0; |
Ben Hutchings | cd38557 | 2010-11-15 23:53:11 +0000 | [diff] [blame] | 529 | tx_queue->old_write_count = 0; |
Ben Hutchings | 8ceee66 | 2008-04-27 12:55:59 +0100 | [diff] [blame] | 530 | tx_queue->read_count = 0; |
| 531 | tx_queue->old_read_count = 0; |
Ben Hutchings | cd38557 | 2010-11-15 23:53:11 +0000 | [diff] [blame] | 532 | tx_queue->empty_read_count = 0 | EFX_EMPTY_COUNT_VALID; |
Ben Hutchings | 8ceee66 | 2008-04-27 12:55:59 +0100 | [diff] [blame] | 533 | |
| 534 | /* Set up TX descriptor ring */ |
Ben Hutchings | 152b6a6 | 2009-11-29 03:43:56 +0000 | [diff] [blame] | 535 | efx_nic_init_tx(tx_queue); |
Ben Hutchings | 94b274b | 2011-01-10 21:18:20 +0000 | [diff] [blame] | 536 | |
| 537 | tx_queue->initialised = true; |
Ben Hutchings | 8ceee66 | 2008-04-27 12:55:59 +0100 | [diff] [blame] | 538 | } |
| 539 | |
| 540 | void efx_release_tx_buffers(struct efx_tx_queue *tx_queue) |
| 541 | { |
| 542 | struct efx_tx_buffer *buffer; |
| 543 | |
| 544 | if (!tx_queue->buffer) |
| 545 | return; |
| 546 | |
| 547 | /* Free any buffers left in the ring */ |
| 548 | while (tx_queue->read_count != tx_queue->write_count) { |
Tom Herbert | c394099 | 2011-11-28 16:33:43 +0000 | [diff] [blame] | 549 | unsigned int pkts_compl = 0, bytes_compl = 0; |
Steve Hodgson | ecc910f | 2010-09-10 06:42:22 +0000 | [diff] [blame] | 550 | buffer = &tx_queue->buffer[tx_queue->read_count & tx_queue->ptr_mask]; |
Tom Herbert | c394099 | 2011-11-28 16:33:43 +0000 | [diff] [blame] | 551 | efx_dequeue_buffer(tx_queue, buffer, &pkts_compl, &bytes_compl); |
Ben Hutchings | 8ceee66 | 2008-04-27 12:55:59 +0100 | [diff] [blame] | 552 | |
| 553 | ++tx_queue->read_count; |
| 554 | } |
Tom Herbert | c394099 | 2011-11-28 16:33:43 +0000 | [diff] [blame] | 555 | netdev_tx_reset_queue(tx_queue->core_txq); |
Ben Hutchings | 8ceee66 | 2008-04-27 12:55:59 +0100 | [diff] [blame] | 556 | } |
| 557 | |
| 558 | void efx_fini_tx_queue(struct efx_tx_queue *tx_queue) |
| 559 | { |
Ben Hutchings | 94b274b | 2011-01-10 21:18:20 +0000 | [diff] [blame] | 560 | if (!tx_queue->initialised) |
| 561 | return; |
| 562 | |
Ben Hutchings | 62776d0 | 2010-06-23 11:30:07 +0000 | [diff] [blame] | 563 | netif_dbg(tx_queue->efx, drv, tx_queue->efx->net_dev, |
| 564 | "shutting down TX queue %d\n", tx_queue->queue); |
Ben Hutchings | 8ceee66 | 2008-04-27 12:55:59 +0100 | [diff] [blame] | 565 | |
Ben Hutchings | 94b274b | 2011-01-10 21:18:20 +0000 | [diff] [blame] | 566 | tx_queue->initialised = false; |
| 567 | |
Ben Hutchings | 8ceee66 | 2008-04-27 12:55:59 +0100 | [diff] [blame] | 568 | /* Flush TX queue, remove descriptor ring */ |
Ben Hutchings | 152b6a6 | 2009-11-29 03:43:56 +0000 | [diff] [blame] | 569 | efx_nic_fini_tx(tx_queue); |
Ben Hutchings | 8ceee66 | 2008-04-27 12:55:59 +0100 | [diff] [blame] | 570 | |
| 571 | efx_release_tx_buffers(tx_queue); |
Ben Hutchings | 8ceee66 | 2008-04-27 12:55:59 +0100 | [diff] [blame] | 572 | } |
| 573 | |
| 574 | void efx_remove_tx_queue(struct efx_tx_queue *tx_queue) |
| 575 | { |
Ben Hutchings | f7251a9 | 2012-05-17 18:40:54 +0100 | [diff] [blame] | 576 | int i; |
| 577 | |
Ben Hutchings | 94b274b | 2011-01-10 21:18:20 +0000 | [diff] [blame] | 578 | if (!tx_queue->buffer) |
| 579 | return; |
| 580 | |
Ben Hutchings | 62776d0 | 2010-06-23 11:30:07 +0000 | [diff] [blame] | 581 | netif_dbg(tx_queue->efx, drv, tx_queue->efx->net_dev, |
| 582 | "destroying TX queue %d\n", tx_queue->queue); |
Ben Hutchings | 152b6a6 | 2009-11-29 03:43:56 +0000 | [diff] [blame] | 583 | efx_nic_remove_tx(tx_queue); |
Ben Hutchings | 8ceee66 | 2008-04-27 12:55:59 +0100 | [diff] [blame] | 584 | |
Ben Hutchings | f7251a9 | 2012-05-17 18:40:54 +0100 | [diff] [blame] | 585 | if (tx_queue->tsoh_page) { |
| 586 | for (i = 0; i < efx_tsoh_page_count(tx_queue); i++) |
| 587 | efx_nic_free_buffer(tx_queue->efx, |
| 588 | &tx_queue->tsoh_page[i]); |
| 589 | kfree(tx_queue->tsoh_page); |
| 590 | tx_queue->tsoh_page = NULL; |
| 591 | } |
| 592 | |
Ben Hutchings | 8ceee66 | 2008-04-27 12:55:59 +0100 | [diff] [blame] | 593 | kfree(tx_queue->buffer); |
| 594 | tx_queue->buffer = NULL; |
Ben Hutchings | 8ceee66 | 2008-04-27 12:55:59 +0100 | [diff] [blame] | 595 | } |
| 596 | |
| 597 | |
Ben Hutchings | b9b39b6 | 2008-05-07 12:51:12 +0100 | [diff] [blame] | 598 | /* Efx TCP segmentation acceleration. |
| 599 | * |
| 600 | * Why? Because by doing it here in the driver we can go significantly |
| 601 | * faster than the GSO. |
| 602 | * |
| 603 | * Requires TX checksum offload support. |
| 604 | */ |
| 605 | |
| 606 | /* Number of bytes inserted at the start of a TSO header buffer, |
| 607 | * similar to NET_IP_ALIGN. |
| 608 | */ |
Ben Hutchings | 13e9ab1 | 2008-09-01 12:50:28 +0100 | [diff] [blame] | 609 | #ifdef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS |
Ben Hutchings | b9b39b6 | 2008-05-07 12:51:12 +0100 | [diff] [blame] | 610 | #define TSOH_OFFSET 0 |
| 611 | #else |
| 612 | #define TSOH_OFFSET NET_IP_ALIGN |
| 613 | #endif |
| 614 | |
Ben Hutchings | b9b39b6 | 2008-05-07 12:51:12 +0100 | [diff] [blame] | 615 | #define PTR_DIFF(p1, p2) ((u8 *)(p1) - (u8 *)(p2)) |
Ben Hutchings | b9b39b6 | 2008-05-07 12:51:12 +0100 | [diff] [blame] | 616 | |
| 617 | /** |
| 618 | * struct tso_state - TSO state for an SKB |
Ben Hutchings | 23d9e60 | 2008-09-01 12:47:02 +0100 | [diff] [blame] | 619 | * @out_len: Remaining length in current segment |
Ben Hutchings | b9b39b6 | 2008-05-07 12:51:12 +0100 | [diff] [blame] | 620 | * @seqnum: Current sequence number |
Ben Hutchings | 23d9e60 | 2008-09-01 12:47:02 +0100 | [diff] [blame] | 621 | * @ipv4_id: Current IPv4 ID, host endian |
Ben Hutchings | b9b39b6 | 2008-05-07 12:51:12 +0100 | [diff] [blame] | 622 | * @packet_space: Remaining space in current packet |
Ben Hutchings | 23d9e60 | 2008-09-01 12:47:02 +0100 | [diff] [blame] | 623 | * @dma_addr: DMA address of current position |
| 624 | * @in_len: Remaining length in current SKB fragment |
| 625 | * @unmap_len: Length of SKB fragment |
| 626 | * @unmap_addr: DMA address of SKB fragment |
Ben Hutchings | 7668ff9 | 2012-05-17 20:52:20 +0100 | [diff] [blame] | 627 | * @dma_flags: TX buffer flags for DMA mapping - %EFX_TX_BUF_MAP_SINGLE or 0 |
Ben Hutchings | 738a8f4 | 2009-11-29 15:16:05 +0000 | [diff] [blame] | 628 | * @protocol: Network protocol (after any VLAN header) |
Ben Hutchings | 9714284 | 2012-06-22 02:44:01 +0100 | [diff] [blame^] | 629 | * @ip_off: Offset of IP header |
| 630 | * @tcp_off: Offset of TCP header |
Ben Hutchings | 23d9e60 | 2008-09-01 12:47:02 +0100 | [diff] [blame] | 631 | * @header_len: Number of bytes of header |
Ben Hutchings | 53cb13c | 2012-06-19 20:03:41 +0100 | [diff] [blame] | 632 | * @ip_base_len: IPv4 tot_len or IPv6 payload_len, before TCP payload |
Ben Hutchings | b9b39b6 | 2008-05-07 12:51:12 +0100 | [diff] [blame] | 633 | * |
| 634 | * The state used during segmentation. It is put into this data structure |
| 635 | * just to make it easy to pass into inline functions. |
| 636 | */ |
| 637 | struct tso_state { |
Ben Hutchings | 23d9e60 | 2008-09-01 12:47:02 +0100 | [diff] [blame] | 638 | /* Output position */ |
| 639 | unsigned out_len; |
Ben Hutchings | b9b39b6 | 2008-05-07 12:51:12 +0100 | [diff] [blame] | 640 | unsigned seqnum; |
Ben Hutchings | 23d9e60 | 2008-09-01 12:47:02 +0100 | [diff] [blame] | 641 | unsigned ipv4_id; |
Ben Hutchings | b9b39b6 | 2008-05-07 12:51:12 +0100 | [diff] [blame] | 642 | unsigned packet_space; |
| 643 | |
Ben Hutchings | 23d9e60 | 2008-09-01 12:47:02 +0100 | [diff] [blame] | 644 | /* Input position */ |
| 645 | dma_addr_t dma_addr; |
| 646 | unsigned in_len; |
| 647 | unsigned unmap_len; |
| 648 | dma_addr_t unmap_addr; |
Ben Hutchings | 7668ff9 | 2012-05-17 20:52:20 +0100 | [diff] [blame] | 649 | unsigned short dma_flags; |
Ben Hutchings | b9b39b6 | 2008-05-07 12:51:12 +0100 | [diff] [blame] | 650 | |
Ben Hutchings | 738a8f4 | 2009-11-29 15:16:05 +0000 | [diff] [blame] | 651 | __be16 protocol; |
Ben Hutchings | 9714284 | 2012-06-22 02:44:01 +0100 | [diff] [blame^] | 652 | unsigned int ip_off; |
| 653 | unsigned int tcp_off; |
Ben Hutchings | 23d9e60 | 2008-09-01 12:47:02 +0100 | [diff] [blame] | 654 | unsigned header_len; |
Ben Hutchings | 53cb13c | 2012-06-19 20:03:41 +0100 | [diff] [blame] | 655 | unsigned int ip_base_len; |
Ben Hutchings | b9b39b6 | 2008-05-07 12:51:12 +0100 | [diff] [blame] | 656 | }; |
| 657 | |
| 658 | |
| 659 | /* |
| 660 | * Verify that our various assumptions about sk_buffs and the conditions |
Ben Hutchings | 738a8f4 | 2009-11-29 15:16:05 +0000 | [diff] [blame] | 661 | * under which TSO will be attempted hold true. Return the protocol number. |
Ben Hutchings | b9b39b6 | 2008-05-07 12:51:12 +0100 | [diff] [blame] | 662 | */ |
Ben Hutchings | 738a8f4 | 2009-11-29 15:16:05 +0000 | [diff] [blame] | 663 | static __be16 efx_tso_check_protocol(struct sk_buff *skb) |
Ben Hutchings | b9b39b6 | 2008-05-07 12:51:12 +0100 | [diff] [blame] | 664 | { |
Ben Hutchings | 740847d | 2008-09-01 12:48:23 +0100 | [diff] [blame] | 665 | __be16 protocol = skb->protocol; |
| 666 | |
Ben Hutchings | b9b39b6 | 2008-05-07 12:51:12 +0100 | [diff] [blame] | 667 | EFX_BUG_ON_PARANOID(((struct ethhdr *)skb->data)->h_proto != |
Ben Hutchings | 740847d | 2008-09-01 12:48:23 +0100 | [diff] [blame] | 668 | protocol); |
| 669 | if (protocol == htons(ETH_P_8021Q)) { |
Ben Hutchings | 740847d | 2008-09-01 12:48:23 +0100 | [diff] [blame] | 670 | struct vlan_ethhdr *veh = (struct vlan_ethhdr *)skb->data; |
| 671 | protocol = veh->h_vlan_encapsulated_proto; |
Ben Hutchings | 740847d | 2008-09-01 12:48:23 +0100 | [diff] [blame] | 672 | } |
| 673 | |
Ben Hutchings | 738a8f4 | 2009-11-29 15:16:05 +0000 | [diff] [blame] | 674 | if (protocol == htons(ETH_P_IP)) { |
| 675 | EFX_BUG_ON_PARANOID(ip_hdr(skb)->protocol != IPPROTO_TCP); |
| 676 | } else { |
| 677 | EFX_BUG_ON_PARANOID(protocol != htons(ETH_P_IPV6)); |
| 678 | EFX_BUG_ON_PARANOID(ipv6_hdr(skb)->nexthdr != NEXTHDR_TCP); |
| 679 | } |
Ben Hutchings | b9b39b6 | 2008-05-07 12:51:12 +0100 | [diff] [blame] | 680 | EFX_BUG_ON_PARANOID((PTR_DIFF(tcp_hdr(skb), skb->data) |
| 681 | + (tcp_hdr(skb)->doff << 2u)) > |
| 682 | skb_headlen(skb)); |
Ben Hutchings | 738a8f4 | 2009-11-29 15:16:05 +0000 | [diff] [blame] | 683 | |
| 684 | return protocol; |
Ben Hutchings | b9b39b6 | 2008-05-07 12:51:12 +0100 | [diff] [blame] | 685 | } |
| 686 | |
Ben Hutchings | f7251a9 | 2012-05-17 18:40:54 +0100 | [diff] [blame] | 687 | static u8 *efx_tsoh_get_buffer(struct efx_tx_queue *tx_queue, |
| 688 | struct efx_tx_buffer *buffer, unsigned int len) |
Ben Hutchings | b9b39b6 | 2008-05-07 12:51:12 +0100 | [diff] [blame] | 689 | { |
Ben Hutchings | f7251a9 | 2012-05-17 18:40:54 +0100 | [diff] [blame] | 690 | u8 *result; |
Ben Hutchings | b9b39b6 | 2008-05-07 12:51:12 +0100 | [diff] [blame] | 691 | |
Ben Hutchings | f7251a9 | 2012-05-17 18:40:54 +0100 | [diff] [blame] | 692 | EFX_BUG_ON_PARANOID(buffer->len); |
| 693 | EFX_BUG_ON_PARANOID(buffer->flags); |
| 694 | EFX_BUG_ON_PARANOID(buffer->unmap_len); |
| 695 | |
| 696 | if (likely(len <= TSOH_STD_SIZE - TSOH_OFFSET)) { |
| 697 | unsigned index = |
| 698 | (tx_queue->insert_count & tx_queue->ptr_mask) / 2; |
| 699 | struct efx_buffer *page_buf = |
| 700 | &tx_queue->tsoh_page[index / TSOH_PER_PAGE]; |
| 701 | unsigned offset = |
| 702 | TSOH_STD_SIZE * (index % TSOH_PER_PAGE) + TSOH_OFFSET; |
| 703 | |
| 704 | if (unlikely(!page_buf->addr) && |
| 705 | efx_nic_alloc_buffer(tx_queue->efx, page_buf, PAGE_SIZE)) |
| 706 | return NULL; |
| 707 | |
| 708 | result = (u8 *)page_buf->addr + offset; |
| 709 | buffer->dma_addr = page_buf->dma_addr + offset; |
| 710 | buffer->flags = EFX_TX_BUF_CONT; |
| 711 | } else { |
| 712 | tx_queue->tso_long_headers++; |
| 713 | |
| 714 | buffer->heap_buf = kmalloc(TSOH_OFFSET + len, GFP_ATOMIC); |
| 715 | if (unlikely(!buffer->heap_buf)) |
| 716 | return NULL; |
| 717 | result = (u8 *)buffer->heap_buf + TSOH_OFFSET; |
| 718 | buffer->flags = EFX_TX_BUF_CONT | EFX_TX_BUF_HEAP; |
Ben Hutchings | b9b39b6 | 2008-05-07 12:51:12 +0100 | [diff] [blame] | 719 | } |
| 720 | |
Ben Hutchings | f7251a9 | 2012-05-17 18:40:54 +0100 | [diff] [blame] | 721 | buffer->len = len; |
Ben Hutchings | b9b39b6 | 2008-05-07 12:51:12 +0100 | [diff] [blame] | 722 | |
Ben Hutchings | f7251a9 | 2012-05-17 18:40:54 +0100 | [diff] [blame] | 723 | return result; |
Ben Hutchings | b9b39b6 | 2008-05-07 12:51:12 +0100 | [diff] [blame] | 724 | } |
| 725 | |
| 726 | /** |
| 727 | * efx_tx_queue_insert - push descriptors onto the TX queue |
| 728 | * @tx_queue: Efx TX queue |
| 729 | * @dma_addr: DMA address of fragment |
| 730 | * @len: Length of fragment |
Ben Hutchings | ecbd95c | 2008-09-01 12:46:40 +0100 | [diff] [blame] | 731 | * @final_buffer: The final buffer inserted into the queue |
Ben Hutchings | b9b39b6 | 2008-05-07 12:51:12 +0100 | [diff] [blame] | 732 | * |
Ben Hutchings | 14bf718fb | 2012-05-22 01:27:58 +0100 | [diff] [blame] | 733 | * Push descriptors onto the TX queue. |
Ben Hutchings | b9b39b6 | 2008-05-07 12:51:12 +0100 | [diff] [blame] | 734 | */ |
Ben Hutchings | 14bf718fb | 2012-05-22 01:27:58 +0100 | [diff] [blame] | 735 | static void efx_tx_queue_insert(struct efx_tx_queue *tx_queue, |
| 736 | dma_addr_t dma_addr, unsigned len, |
| 737 | struct efx_tx_buffer **final_buffer) |
Ben Hutchings | b9b39b6 | 2008-05-07 12:51:12 +0100 | [diff] [blame] | 738 | { |
| 739 | struct efx_tx_buffer *buffer; |
| 740 | struct efx_nic *efx = tx_queue->efx; |
Ben Hutchings | 14bf718fb | 2012-05-22 01:27:58 +0100 | [diff] [blame] | 741 | unsigned dma_len, insert_ptr; |
Ben Hutchings | b9b39b6 | 2008-05-07 12:51:12 +0100 | [diff] [blame] | 742 | |
| 743 | EFX_BUG_ON_PARANOID(len <= 0); |
| 744 | |
Ben Hutchings | b9b39b6 | 2008-05-07 12:51:12 +0100 | [diff] [blame] | 745 | while (1) { |
Steve Hodgson | ecc910f | 2010-09-10 06:42:22 +0000 | [diff] [blame] | 746 | insert_ptr = tx_queue->insert_count & tx_queue->ptr_mask; |
Ben Hutchings | b9b39b6 | 2008-05-07 12:51:12 +0100 | [diff] [blame] | 747 | buffer = &tx_queue->buffer[insert_ptr]; |
| 748 | ++tx_queue->insert_count; |
| 749 | |
| 750 | EFX_BUG_ON_PARANOID(tx_queue->insert_count - |
Steve Hodgson | ecc910f | 2010-09-10 06:42:22 +0000 | [diff] [blame] | 751 | tx_queue->read_count >= |
| 752 | efx->txq_entries); |
Ben Hutchings | b9b39b6 | 2008-05-07 12:51:12 +0100 | [diff] [blame] | 753 | |
Ben Hutchings | b9b39b6 | 2008-05-07 12:51:12 +0100 | [diff] [blame] | 754 | EFX_BUG_ON_PARANOID(buffer->len); |
| 755 | EFX_BUG_ON_PARANOID(buffer->unmap_len); |
Ben Hutchings | 7668ff9 | 2012-05-17 20:52:20 +0100 | [diff] [blame] | 756 | EFX_BUG_ON_PARANOID(buffer->flags); |
Ben Hutchings | b9b39b6 | 2008-05-07 12:51:12 +0100 | [diff] [blame] | 757 | |
| 758 | buffer->dma_addr = dma_addr; |
| 759 | |
Ben Hutchings | 63f1988 | 2009-10-23 08:31:20 +0000 | [diff] [blame] | 760 | dma_len = efx_max_tx_len(efx, dma_addr); |
Ben Hutchings | b9b39b6 | 2008-05-07 12:51:12 +0100 | [diff] [blame] | 761 | |
| 762 | /* If there is enough space to send then do so */ |
| 763 | if (dma_len >= len) |
| 764 | break; |
| 765 | |
Ben Hutchings | 7668ff9 | 2012-05-17 20:52:20 +0100 | [diff] [blame] | 766 | buffer->len = dma_len; |
| 767 | buffer->flags = EFX_TX_BUF_CONT; |
Ben Hutchings | b9b39b6 | 2008-05-07 12:51:12 +0100 | [diff] [blame] | 768 | dma_addr += dma_len; |
| 769 | len -= dma_len; |
| 770 | } |
| 771 | |
| 772 | EFX_BUG_ON_PARANOID(!len); |
| 773 | buffer->len = len; |
Ben Hutchings | ecbd95c | 2008-09-01 12:46:40 +0100 | [diff] [blame] | 774 | *final_buffer = buffer; |
Ben Hutchings | b9b39b6 | 2008-05-07 12:51:12 +0100 | [diff] [blame] | 775 | } |
| 776 | |
| 777 | |
| 778 | /* |
| 779 | * Put a TSO header into the TX queue. |
| 780 | * |
| 781 | * This is special-cased because we know that it is small enough to fit in |
| 782 | * a single fragment, and we know it doesn't cross a page boundary. It |
| 783 | * also allows us to not worry about end-of-packet etc. |
| 784 | */ |
Ben Hutchings | f7251a9 | 2012-05-17 18:40:54 +0100 | [diff] [blame] | 785 | static int efx_tso_put_header(struct efx_tx_queue *tx_queue, |
| 786 | struct efx_tx_buffer *buffer, u8 *header) |
Ben Hutchings | b9b39b6 | 2008-05-07 12:51:12 +0100 | [diff] [blame] | 787 | { |
Ben Hutchings | f7251a9 | 2012-05-17 18:40:54 +0100 | [diff] [blame] | 788 | if (unlikely(buffer->flags & EFX_TX_BUF_HEAP)) { |
| 789 | buffer->dma_addr = dma_map_single(&tx_queue->efx->pci_dev->dev, |
| 790 | header, buffer->len, |
| 791 | DMA_TO_DEVICE); |
| 792 | if (unlikely(dma_mapping_error(&tx_queue->efx->pci_dev->dev, |
| 793 | buffer->dma_addr))) { |
| 794 | kfree(buffer->heap_buf); |
| 795 | buffer->len = 0; |
| 796 | buffer->flags = 0; |
| 797 | return -ENOMEM; |
| 798 | } |
| 799 | buffer->unmap_len = buffer->len; |
| 800 | buffer->flags |= EFX_TX_BUF_MAP_SINGLE; |
| 801 | } |
Ben Hutchings | b9b39b6 | 2008-05-07 12:51:12 +0100 | [diff] [blame] | 802 | |
| 803 | ++tx_queue->insert_count; |
Ben Hutchings | f7251a9 | 2012-05-17 18:40:54 +0100 | [diff] [blame] | 804 | return 0; |
Ben Hutchings | b9b39b6 | 2008-05-07 12:51:12 +0100 | [diff] [blame] | 805 | } |
| 806 | |
| 807 | |
Ben Hutchings | f7251a9 | 2012-05-17 18:40:54 +0100 | [diff] [blame] | 808 | /* Remove buffers put into a tx_queue. None of the buffers must have |
| 809 | * an skb attached. |
| 810 | */ |
Ben Hutchings | b9b39b6 | 2008-05-07 12:51:12 +0100 | [diff] [blame] | 811 | static void efx_enqueue_unwind(struct efx_tx_queue *tx_queue) |
| 812 | { |
| 813 | struct efx_tx_buffer *buffer; |
| 814 | |
| 815 | /* Work backwards until we hit the original insert pointer value */ |
| 816 | while (tx_queue->insert_count != tx_queue->write_count) { |
| 817 | --tx_queue->insert_count; |
| 818 | buffer = &tx_queue->buffer[tx_queue->insert_count & |
Steve Hodgson | ecc910f | 2010-09-10 06:42:22 +0000 | [diff] [blame] | 819 | tx_queue->ptr_mask]; |
Ben Hutchings | f7251a9 | 2012-05-17 18:40:54 +0100 | [diff] [blame] | 820 | efx_dequeue_buffer(tx_queue, buffer, NULL, NULL); |
Ben Hutchings | b9b39b6 | 2008-05-07 12:51:12 +0100 | [diff] [blame] | 821 | } |
| 822 | } |
| 823 | |
| 824 | |
| 825 | /* Parse the SKB header and initialise state. */ |
Ben Hutchings | 4d56606 | 2008-09-01 12:47:12 +0100 | [diff] [blame] | 826 | static void tso_start(struct tso_state *st, const struct sk_buff *skb) |
Ben Hutchings | b9b39b6 | 2008-05-07 12:51:12 +0100 | [diff] [blame] | 827 | { |
Ben Hutchings | 9714284 | 2012-06-22 02:44:01 +0100 | [diff] [blame^] | 828 | st->ip_off = skb_network_header(skb) - skb->data; |
| 829 | st->tcp_off = skb_transport_header(skb) - skb->data; |
| 830 | st->header_len = st->tcp_off + (tcp_hdr(skb)->doff << 2u); |
Ben Hutchings | 53cb13c | 2012-06-19 20:03:41 +0100 | [diff] [blame] | 831 | if (st->protocol == htons(ETH_P_IP)) { |
Ben Hutchings | 9714284 | 2012-06-22 02:44:01 +0100 | [diff] [blame^] | 832 | st->ip_base_len = st->header_len - st->ip_off; |
Ben Hutchings | 738a8f4 | 2009-11-29 15:16:05 +0000 | [diff] [blame] | 833 | st->ipv4_id = ntohs(ip_hdr(skb)->id); |
Ben Hutchings | 53cb13c | 2012-06-19 20:03:41 +0100 | [diff] [blame] | 834 | } else { |
Ben Hutchings | 9714284 | 2012-06-22 02:44:01 +0100 | [diff] [blame^] | 835 | st->ip_base_len = st->header_len - st->tcp_off; |
Ben Hutchings | 738a8f4 | 2009-11-29 15:16:05 +0000 | [diff] [blame] | 836 | st->ipv4_id = 0; |
Ben Hutchings | 53cb13c | 2012-06-19 20:03:41 +0100 | [diff] [blame] | 837 | } |
Ben Hutchings | b9b39b6 | 2008-05-07 12:51:12 +0100 | [diff] [blame] | 838 | st->seqnum = ntohl(tcp_hdr(skb)->seq); |
| 839 | |
| 840 | EFX_BUG_ON_PARANOID(tcp_hdr(skb)->urg); |
| 841 | EFX_BUG_ON_PARANOID(tcp_hdr(skb)->syn); |
| 842 | EFX_BUG_ON_PARANOID(tcp_hdr(skb)->rst); |
| 843 | |
Ben Hutchings | 23d9e60 | 2008-09-01 12:47:02 +0100 | [diff] [blame] | 844 | st->out_len = skb->len - st->header_len; |
| 845 | st->unmap_len = 0; |
Ben Hutchings | 7668ff9 | 2012-05-17 20:52:20 +0100 | [diff] [blame] | 846 | st->dma_flags = 0; |
Ben Hutchings | b9b39b6 | 2008-05-07 12:51:12 +0100 | [diff] [blame] | 847 | } |
| 848 | |
Ben Hutchings | 4d56606 | 2008-09-01 12:47:12 +0100 | [diff] [blame] | 849 | static int tso_get_fragment(struct tso_state *st, struct efx_nic *efx, |
| 850 | skb_frag_t *frag) |
Ben Hutchings | b9b39b6 | 2008-05-07 12:51:12 +0100 | [diff] [blame] | 851 | { |
Ian Campbell | 4a22c4c | 2011-09-21 21:53:16 +0000 | [diff] [blame] | 852 | st->unmap_addr = skb_frag_dma_map(&efx->pci_dev->dev, frag, 0, |
Eric Dumazet | 9e903e0 | 2011-10-18 21:00:24 +0000 | [diff] [blame] | 853 | skb_frag_size(frag), DMA_TO_DEVICE); |
Ian Campbell | 5d6bcdf | 2011-10-06 11:10:48 +0100 | [diff] [blame] | 854 | if (likely(!dma_mapping_error(&efx->pci_dev->dev, st->unmap_addr))) { |
Ben Hutchings | 7668ff9 | 2012-05-17 20:52:20 +0100 | [diff] [blame] | 855 | st->dma_flags = 0; |
Eric Dumazet | 9e903e0 | 2011-10-18 21:00:24 +0000 | [diff] [blame] | 856 | st->unmap_len = skb_frag_size(frag); |
| 857 | st->in_len = skb_frag_size(frag); |
Ben Hutchings | 23d9e60 | 2008-09-01 12:47:02 +0100 | [diff] [blame] | 858 | st->dma_addr = st->unmap_addr; |
Ben Hutchings | ecbd95c | 2008-09-01 12:46:40 +0100 | [diff] [blame] | 859 | return 0; |
| 860 | } |
| 861 | return -ENOMEM; |
| 862 | } |
| 863 | |
Ben Hutchings | 4d56606 | 2008-09-01 12:47:12 +0100 | [diff] [blame] | 864 | static int tso_get_head_fragment(struct tso_state *st, struct efx_nic *efx, |
| 865 | const struct sk_buff *skb) |
Ben Hutchings | ecbd95c | 2008-09-01 12:46:40 +0100 | [diff] [blame] | 866 | { |
Ben Hutchings | 23d9e60 | 2008-09-01 12:47:02 +0100 | [diff] [blame] | 867 | int hl = st->header_len; |
Ben Hutchings | ecbd95c | 2008-09-01 12:46:40 +0100 | [diff] [blame] | 868 | int len = skb_headlen(skb) - hl; |
| 869 | |
Ben Hutchings | 0e33d87 | 2012-05-17 17:46:55 +0100 | [diff] [blame] | 870 | st->unmap_addr = dma_map_single(&efx->pci_dev->dev, skb->data + hl, |
| 871 | len, DMA_TO_DEVICE); |
| 872 | if (likely(!dma_mapping_error(&efx->pci_dev->dev, st->unmap_addr))) { |
Ben Hutchings | 7668ff9 | 2012-05-17 20:52:20 +0100 | [diff] [blame] | 873 | st->dma_flags = EFX_TX_BUF_MAP_SINGLE; |
Ben Hutchings | 23d9e60 | 2008-09-01 12:47:02 +0100 | [diff] [blame] | 874 | st->unmap_len = len; |
| 875 | st->in_len = len; |
| 876 | st->dma_addr = st->unmap_addr; |
Ben Hutchings | b9b39b6 | 2008-05-07 12:51:12 +0100 | [diff] [blame] | 877 | return 0; |
| 878 | } |
| 879 | return -ENOMEM; |
| 880 | } |
| 881 | |
| 882 | |
| 883 | /** |
| 884 | * tso_fill_packet_with_fragment - form descriptors for the current fragment |
| 885 | * @tx_queue: Efx TX queue |
| 886 | * @skb: Socket buffer |
| 887 | * @st: TSO state |
| 888 | * |
| 889 | * Form descriptors for the current fragment, until we reach the end |
Ben Hutchings | 14bf718fb | 2012-05-22 01:27:58 +0100 | [diff] [blame] | 890 | * of fragment or end-of-packet. |
Ben Hutchings | b9b39b6 | 2008-05-07 12:51:12 +0100 | [diff] [blame] | 891 | */ |
Ben Hutchings | 14bf718fb | 2012-05-22 01:27:58 +0100 | [diff] [blame] | 892 | static void tso_fill_packet_with_fragment(struct efx_tx_queue *tx_queue, |
| 893 | const struct sk_buff *skb, |
| 894 | struct tso_state *st) |
Ben Hutchings | b9b39b6 | 2008-05-07 12:51:12 +0100 | [diff] [blame] | 895 | { |
Ben Hutchings | ecbd95c | 2008-09-01 12:46:40 +0100 | [diff] [blame] | 896 | struct efx_tx_buffer *buffer; |
Ben Hutchings | 14bf718fb | 2012-05-22 01:27:58 +0100 | [diff] [blame] | 897 | int n; |
Ben Hutchings | b9b39b6 | 2008-05-07 12:51:12 +0100 | [diff] [blame] | 898 | |
Ben Hutchings | 23d9e60 | 2008-09-01 12:47:02 +0100 | [diff] [blame] | 899 | if (st->in_len == 0) |
Ben Hutchings | 14bf718fb | 2012-05-22 01:27:58 +0100 | [diff] [blame] | 900 | return; |
Ben Hutchings | b9b39b6 | 2008-05-07 12:51:12 +0100 | [diff] [blame] | 901 | if (st->packet_space == 0) |
Ben Hutchings | 14bf718fb | 2012-05-22 01:27:58 +0100 | [diff] [blame] | 902 | return; |
Ben Hutchings | b9b39b6 | 2008-05-07 12:51:12 +0100 | [diff] [blame] | 903 | |
Ben Hutchings | 23d9e60 | 2008-09-01 12:47:02 +0100 | [diff] [blame] | 904 | EFX_BUG_ON_PARANOID(st->in_len <= 0); |
Ben Hutchings | b9b39b6 | 2008-05-07 12:51:12 +0100 | [diff] [blame] | 905 | EFX_BUG_ON_PARANOID(st->packet_space <= 0); |
| 906 | |
Ben Hutchings | 23d9e60 | 2008-09-01 12:47:02 +0100 | [diff] [blame] | 907 | n = min(st->in_len, st->packet_space); |
Ben Hutchings | b9b39b6 | 2008-05-07 12:51:12 +0100 | [diff] [blame] | 908 | |
| 909 | st->packet_space -= n; |
Ben Hutchings | 23d9e60 | 2008-09-01 12:47:02 +0100 | [diff] [blame] | 910 | st->out_len -= n; |
| 911 | st->in_len -= n; |
Ben Hutchings | b9b39b6 | 2008-05-07 12:51:12 +0100 | [diff] [blame] | 912 | |
Ben Hutchings | 14bf718fb | 2012-05-22 01:27:58 +0100 | [diff] [blame] | 913 | efx_tx_queue_insert(tx_queue, st->dma_addr, n, &buffer); |
Ben Hutchings | ecbd95c | 2008-09-01 12:46:40 +0100 | [diff] [blame] | 914 | |
Ben Hutchings | 14bf718fb | 2012-05-22 01:27:58 +0100 | [diff] [blame] | 915 | if (st->out_len == 0) { |
| 916 | /* Transfer ownership of the skb */ |
| 917 | buffer->skb = skb; |
| 918 | buffer->flags = EFX_TX_BUF_SKB; |
| 919 | } else if (st->packet_space != 0) { |
| 920 | buffer->flags = EFX_TX_BUF_CONT; |
| 921 | } |
| 922 | |
| 923 | if (st->in_len == 0) { |
| 924 | /* Transfer ownership of the DMA mapping */ |
| 925 | buffer->unmap_len = st->unmap_len; |
| 926 | buffer->flags |= st->dma_flags; |
| 927 | st->unmap_len = 0; |
Ben Hutchings | ecbd95c | 2008-09-01 12:46:40 +0100 | [diff] [blame] | 928 | } |
Ben Hutchings | b9b39b6 | 2008-05-07 12:51:12 +0100 | [diff] [blame] | 929 | |
Ben Hutchings | 23d9e60 | 2008-09-01 12:47:02 +0100 | [diff] [blame] | 930 | st->dma_addr += n; |
Ben Hutchings | b9b39b6 | 2008-05-07 12:51:12 +0100 | [diff] [blame] | 931 | } |
| 932 | |
| 933 | |
| 934 | /** |
| 935 | * tso_start_new_packet - generate a new header and prepare for the new packet |
| 936 | * @tx_queue: Efx TX queue |
| 937 | * @skb: Socket buffer |
| 938 | * @st: TSO state |
| 939 | * |
| 940 | * Generate a new header and prepare for the new packet. Return 0 on |
Ben Hutchings | f7251a9 | 2012-05-17 18:40:54 +0100 | [diff] [blame] | 941 | * success, or -%ENOMEM if failed to alloc header. |
Ben Hutchings | b9b39b6 | 2008-05-07 12:51:12 +0100 | [diff] [blame] | 942 | */ |
Ben Hutchings | 4d56606 | 2008-09-01 12:47:12 +0100 | [diff] [blame] | 943 | static int tso_start_new_packet(struct efx_tx_queue *tx_queue, |
| 944 | const struct sk_buff *skb, |
| 945 | struct tso_state *st) |
Ben Hutchings | b9b39b6 | 2008-05-07 12:51:12 +0100 | [diff] [blame] | 946 | { |
Ben Hutchings | f7251a9 | 2012-05-17 18:40:54 +0100 | [diff] [blame] | 947 | struct efx_tx_buffer *buffer = |
| 948 | &tx_queue->buffer[tx_queue->insert_count & tx_queue->ptr_mask]; |
Ben Hutchings | b9b39b6 | 2008-05-07 12:51:12 +0100 | [diff] [blame] | 949 | struct tcphdr *tsoh_th; |
| 950 | unsigned ip_length; |
| 951 | u8 *header; |
Ben Hutchings | f7251a9 | 2012-05-17 18:40:54 +0100 | [diff] [blame] | 952 | int rc; |
Ben Hutchings | b9b39b6 | 2008-05-07 12:51:12 +0100 | [diff] [blame] | 953 | |
Ben Hutchings | f7251a9 | 2012-05-17 18:40:54 +0100 | [diff] [blame] | 954 | /* Allocate and insert a DMA-mapped header buffer. */ |
| 955 | header = efx_tsoh_get_buffer(tx_queue, buffer, st->header_len); |
| 956 | if (!header) |
| 957 | return -ENOMEM; |
Ben Hutchings | b9b39b6 | 2008-05-07 12:51:12 +0100 | [diff] [blame] | 958 | |
Ben Hutchings | 9714284 | 2012-06-22 02:44:01 +0100 | [diff] [blame^] | 959 | tsoh_th = (struct tcphdr *)(header + st->tcp_off); |
Ben Hutchings | b9b39b6 | 2008-05-07 12:51:12 +0100 | [diff] [blame] | 960 | |
| 961 | /* Copy and update the headers. */ |
Ben Hutchings | 23d9e60 | 2008-09-01 12:47:02 +0100 | [diff] [blame] | 962 | memcpy(header, skb->data, st->header_len); |
Ben Hutchings | b9b39b6 | 2008-05-07 12:51:12 +0100 | [diff] [blame] | 963 | |
| 964 | tsoh_th->seq = htonl(st->seqnum); |
| 965 | st->seqnum += skb_shinfo(skb)->gso_size; |
Ben Hutchings | 23d9e60 | 2008-09-01 12:47:02 +0100 | [diff] [blame] | 966 | if (st->out_len > skb_shinfo(skb)->gso_size) { |
Ben Hutchings | b9b39b6 | 2008-05-07 12:51:12 +0100 | [diff] [blame] | 967 | /* This packet will not finish the TSO burst. */ |
Ben Hutchings | 53cb13c | 2012-06-19 20:03:41 +0100 | [diff] [blame] | 968 | st->packet_space = skb_shinfo(skb)->gso_size; |
Ben Hutchings | b9b39b6 | 2008-05-07 12:51:12 +0100 | [diff] [blame] | 969 | tsoh_th->fin = 0; |
| 970 | tsoh_th->psh = 0; |
| 971 | } else { |
| 972 | /* This packet will be the last in the TSO burst. */ |
Ben Hutchings | 53cb13c | 2012-06-19 20:03:41 +0100 | [diff] [blame] | 973 | st->packet_space = st->out_len; |
Ben Hutchings | b9b39b6 | 2008-05-07 12:51:12 +0100 | [diff] [blame] | 974 | tsoh_th->fin = tcp_hdr(skb)->fin; |
| 975 | tsoh_th->psh = tcp_hdr(skb)->psh; |
| 976 | } |
Ben Hutchings | 53cb13c | 2012-06-19 20:03:41 +0100 | [diff] [blame] | 977 | ip_length = st->ip_base_len + st->packet_space; |
Ben Hutchings | b9b39b6 | 2008-05-07 12:51:12 +0100 | [diff] [blame] | 978 | |
Ben Hutchings | 738a8f4 | 2009-11-29 15:16:05 +0000 | [diff] [blame] | 979 | if (st->protocol == htons(ETH_P_IP)) { |
Ben Hutchings | 9714284 | 2012-06-22 02:44:01 +0100 | [diff] [blame^] | 980 | struct iphdr *tsoh_iph = (struct iphdr *)(header + st->ip_off); |
Ben Hutchings | 738a8f4 | 2009-11-29 15:16:05 +0000 | [diff] [blame] | 981 | |
| 982 | tsoh_iph->tot_len = htons(ip_length); |
| 983 | |
| 984 | /* Linux leaves suitable gaps in the IP ID space for us to fill. */ |
| 985 | tsoh_iph->id = htons(st->ipv4_id); |
| 986 | st->ipv4_id++; |
| 987 | } else { |
| 988 | struct ipv6hdr *tsoh_iph = |
Ben Hutchings | 9714284 | 2012-06-22 02:44:01 +0100 | [diff] [blame^] | 989 | (struct ipv6hdr *)(header + st->ip_off); |
Ben Hutchings | 738a8f4 | 2009-11-29 15:16:05 +0000 | [diff] [blame] | 990 | |
Ben Hutchings | 53cb13c | 2012-06-19 20:03:41 +0100 | [diff] [blame] | 991 | tsoh_iph->payload_len = htons(ip_length); |
Ben Hutchings | 738a8f4 | 2009-11-29 15:16:05 +0000 | [diff] [blame] | 992 | } |
Ben Hutchings | b9b39b6 | 2008-05-07 12:51:12 +0100 | [diff] [blame] | 993 | |
Ben Hutchings | f7251a9 | 2012-05-17 18:40:54 +0100 | [diff] [blame] | 994 | rc = efx_tso_put_header(tx_queue, buffer, header); |
| 995 | if (unlikely(rc)) |
| 996 | return rc; |
| 997 | |
Ben Hutchings | b9b39b6 | 2008-05-07 12:51:12 +0100 | [diff] [blame] | 998 | ++tx_queue->tso_packets; |
| 999 | |
Ben Hutchings | b9b39b6 | 2008-05-07 12:51:12 +0100 | [diff] [blame] | 1000 | return 0; |
| 1001 | } |
| 1002 | |
| 1003 | |
| 1004 | /** |
| 1005 | * efx_enqueue_skb_tso - segment and transmit a TSO socket buffer |
| 1006 | * @tx_queue: Efx TX queue |
| 1007 | * @skb: Socket buffer |
| 1008 | * |
| 1009 | * Context: You must hold netif_tx_lock() to call this function. |
| 1010 | * |
| 1011 | * Add socket buffer @skb to @tx_queue, doing TSO or return != 0 if |
| 1012 | * @skb was not enqueued. In all cases @skb is consumed. Return |
Ben Hutchings | 14bf718fb | 2012-05-22 01:27:58 +0100 | [diff] [blame] | 1013 | * %NETDEV_TX_OK. |
Ben Hutchings | b9b39b6 | 2008-05-07 12:51:12 +0100 | [diff] [blame] | 1014 | */ |
| 1015 | static int efx_enqueue_skb_tso(struct efx_tx_queue *tx_queue, |
Ben Hutchings | 740847d | 2008-09-01 12:48:23 +0100 | [diff] [blame] | 1016 | struct sk_buff *skb) |
Ben Hutchings | b9b39b6 | 2008-05-07 12:51:12 +0100 | [diff] [blame] | 1017 | { |
Ben Hutchings | ecbd95c | 2008-09-01 12:46:40 +0100 | [diff] [blame] | 1018 | struct efx_nic *efx = tx_queue->efx; |
Ben Hutchings | 14bf718fb | 2012-05-22 01:27:58 +0100 | [diff] [blame] | 1019 | int frag_i, rc; |
Ben Hutchings | b9b39b6 | 2008-05-07 12:51:12 +0100 | [diff] [blame] | 1020 | struct tso_state state; |
Ben Hutchings | b9b39b6 | 2008-05-07 12:51:12 +0100 | [diff] [blame] | 1021 | |
Ben Hutchings | 738a8f4 | 2009-11-29 15:16:05 +0000 | [diff] [blame] | 1022 | /* Find the packet protocol and sanity-check it */ |
| 1023 | state.protocol = efx_tso_check_protocol(skb); |
Ben Hutchings | b9b39b6 | 2008-05-07 12:51:12 +0100 | [diff] [blame] | 1024 | |
| 1025 | EFX_BUG_ON_PARANOID(tx_queue->write_count != tx_queue->insert_count); |
| 1026 | |
| 1027 | tso_start(&state, skb); |
| 1028 | |
| 1029 | /* Assume that skb header area contains exactly the headers, and |
| 1030 | * all payload is in the frag list. |
| 1031 | */ |
Ben Hutchings | 23d9e60 | 2008-09-01 12:47:02 +0100 | [diff] [blame] | 1032 | if (skb_headlen(skb) == state.header_len) { |
Ben Hutchings | b9b39b6 | 2008-05-07 12:51:12 +0100 | [diff] [blame] | 1033 | /* Grab the first payload fragment. */ |
| 1034 | EFX_BUG_ON_PARANOID(skb_shinfo(skb)->nr_frags < 1); |
| 1035 | frag_i = 0; |
Ben Hutchings | ecbd95c | 2008-09-01 12:46:40 +0100 | [diff] [blame] | 1036 | rc = tso_get_fragment(&state, efx, |
| 1037 | skb_shinfo(skb)->frags + frag_i); |
Ben Hutchings | b9b39b6 | 2008-05-07 12:51:12 +0100 | [diff] [blame] | 1038 | if (rc) |
| 1039 | goto mem_err; |
| 1040 | } else { |
Ben Hutchings | ecbd95c | 2008-09-01 12:46:40 +0100 | [diff] [blame] | 1041 | rc = tso_get_head_fragment(&state, efx, skb); |
Ben Hutchings | b9b39b6 | 2008-05-07 12:51:12 +0100 | [diff] [blame] | 1042 | if (rc) |
| 1043 | goto mem_err; |
| 1044 | frag_i = -1; |
| 1045 | } |
| 1046 | |
| 1047 | if (tso_start_new_packet(tx_queue, skb, &state) < 0) |
| 1048 | goto mem_err; |
| 1049 | |
| 1050 | while (1) { |
Ben Hutchings | 14bf718fb | 2012-05-22 01:27:58 +0100 | [diff] [blame] | 1051 | tso_fill_packet_with_fragment(tx_queue, skb, &state); |
Ben Hutchings | b9b39b6 | 2008-05-07 12:51:12 +0100 | [diff] [blame] | 1052 | |
| 1053 | /* Move onto the next fragment? */ |
Ben Hutchings | 23d9e60 | 2008-09-01 12:47:02 +0100 | [diff] [blame] | 1054 | if (state.in_len == 0) { |
Ben Hutchings | b9b39b6 | 2008-05-07 12:51:12 +0100 | [diff] [blame] | 1055 | if (++frag_i >= skb_shinfo(skb)->nr_frags) |
| 1056 | /* End of payload reached. */ |
| 1057 | break; |
Ben Hutchings | ecbd95c | 2008-09-01 12:46:40 +0100 | [diff] [blame] | 1058 | rc = tso_get_fragment(&state, efx, |
| 1059 | skb_shinfo(skb)->frags + frag_i); |
Ben Hutchings | b9b39b6 | 2008-05-07 12:51:12 +0100 | [diff] [blame] | 1060 | if (rc) |
| 1061 | goto mem_err; |
| 1062 | } |
| 1063 | |
| 1064 | /* Start at new packet? */ |
| 1065 | if (state.packet_space == 0 && |
| 1066 | tso_start_new_packet(tx_queue, skb, &state) < 0) |
| 1067 | goto mem_err; |
| 1068 | } |
| 1069 | |
Eric Dumazet | 449fa02 | 2011-11-30 17:12:27 -0500 | [diff] [blame] | 1070 | netdev_tx_sent_queue(tx_queue->core_txq, skb->len); |
| 1071 | |
Ben Hutchings | b9b39b6 | 2008-05-07 12:51:12 +0100 | [diff] [blame] | 1072 | /* Pass off to hardware */ |
Ben Hutchings | 152b6a6 | 2009-11-29 03:43:56 +0000 | [diff] [blame] | 1073 | efx_nic_push_buffers(tx_queue); |
Ben Hutchings | b9b39b6 | 2008-05-07 12:51:12 +0100 | [diff] [blame] | 1074 | |
Ben Hutchings | 14bf718fb | 2012-05-22 01:27:58 +0100 | [diff] [blame] | 1075 | efx_tx_maybe_stop_queue(tx_queue); |
| 1076 | |
Ben Hutchings | b9b39b6 | 2008-05-07 12:51:12 +0100 | [diff] [blame] | 1077 | tx_queue->tso_bursts++; |
| 1078 | return NETDEV_TX_OK; |
| 1079 | |
| 1080 | mem_err: |
Ben Hutchings | 62776d0 | 2010-06-23 11:30:07 +0000 | [diff] [blame] | 1081 | netif_err(efx, tx_err, efx->net_dev, |
Ben Hutchings | 0e33d87 | 2012-05-17 17:46:55 +0100 | [diff] [blame] | 1082 | "Out of memory for TSO headers, or DMA mapping error\n"); |
Ben Hutchings | 9bc183d | 2009-11-23 16:06:47 +0000 | [diff] [blame] | 1083 | dev_kfree_skb_any(skb); |
Ben Hutchings | b9b39b6 | 2008-05-07 12:51:12 +0100 | [diff] [blame] | 1084 | |
Ben Hutchings | 5988b63 | 2008-09-01 12:46:36 +0100 | [diff] [blame] | 1085 | /* Free the DMA mapping we were in the process of writing out */ |
Ben Hutchings | 23d9e60 | 2008-09-01 12:47:02 +0100 | [diff] [blame] | 1086 | if (state.unmap_len) { |
Ben Hutchings | 7668ff9 | 2012-05-17 20:52:20 +0100 | [diff] [blame] | 1087 | if (state.dma_flags & EFX_TX_BUF_MAP_SINGLE) |
Ben Hutchings | 0e33d87 | 2012-05-17 17:46:55 +0100 | [diff] [blame] | 1088 | dma_unmap_single(&efx->pci_dev->dev, state.unmap_addr, |
| 1089 | state.unmap_len, DMA_TO_DEVICE); |
Ben Hutchings | ecbd95c | 2008-09-01 12:46:40 +0100 | [diff] [blame] | 1090 | else |
Ben Hutchings | 0e33d87 | 2012-05-17 17:46:55 +0100 | [diff] [blame] | 1091 | dma_unmap_page(&efx->pci_dev->dev, state.unmap_addr, |
| 1092 | state.unmap_len, DMA_TO_DEVICE); |
Ben Hutchings | ecbd95c | 2008-09-01 12:46:40 +0100 | [diff] [blame] | 1093 | } |
Ben Hutchings | 5988b63 | 2008-09-01 12:46:36 +0100 | [diff] [blame] | 1094 | |
Ben Hutchings | b9b39b6 | 2008-05-07 12:51:12 +0100 | [diff] [blame] | 1095 | efx_enqueue_unwind(tx_queue); |
Ben Hutchings | 14bf718fb | 2012-05-22 01:27:58 +0100 | [diff] [blame] | 1096 | return NETDEV_TX_OK; |
Ben Hutchings | b9b39b6 | 2008-05-07 12:51:12 +0100 | [diff] [blame] | 1097 | } |