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