blob: 18713436b44345a110ed263d54c5411d1e93c6fa [file] [log] [blame]
Ben Hutchings8ceee662008-04-27 12:55:59 +01001/****************************************************************************
2 * Driver for Solarflare Solarstorm network controllers and boards
3 * Copyright 2005-2006 Fen Systems Ltd.
Ben Hutchings0a6f40c2011-02-25 00:01:34 +00004 * Copyright 2005-2010 Solarflare Communications Inc.
Ben Hutchings8ceee662008-04-27 12:55:59 +01005 *
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 Hutchings738a8f42009-11-29 15:16:05 +000015#include <linux/ipv6.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090016#include <linux/slab.h>
Ben Hutchings738a8f42009-11-29 15:16:05 +000017#include <net/ipv6.h>
Ben Hutchings8ceee662008-04-27 12:55:59 +010018#include <linux/if_ether.h>
19#include <linux/highmem.h>
20#include "net_driver.h"
Ben Hutchings8ceee662008-04-27 12:55:59 +010021#include "efx.h"
Ben Hutchings744093c2009-11-29 15:12:08 +000022#include "nic.h"
Ben Hutchings8ceee662008-04-27 12:55:59 +010023#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 Hodgsonecc910f2010-09-10 06:42:22 +000031#define EFX_TXQ_THRESHOLD(_efx) ((_efx)->txq_entries / 2u)
Ben Hutchings8ceee662008-04-27 12:55:59 +010032
Ben Hutchings4d566062008-09-01 12:47:12 +010033static void efx_dequeue_buffer(struct efx_tx_queue *tx_queue,
Tom Herbertc3940992011-11-28 16:33:43 +000034 struct efx_tx_buffer *buffer,
35 unsigned int *pkts_compl,
36 unsigned int *bytes_compl)
Ben Hutchings8ceee662008-04-27 12:55:59 +010037{
38 if (buffer->unmap_len) {
Ben Hutchings0e33d872012-05-17 17:46:55 +010039 struct device *dma_dev = &tx_queue->efx->pci_dev->dev;
Ben Hutchingscc12dac2008-09-01 12:46:43 +010040 dma_addr_t unmap_addr = (buffer->dma_addr + buffer->len -
41 buffer->unmap_len);
Ben Hutchings8ceee662008-04-27 12:55:59 +010042 if (buffer->unmap_single)
Ben Hutchings0e33d872012-05-17 17:46:55 +010043 dma_unmap_single(dma_dev, unmap_addr, buffer->unmap_len,
44 DMA_TO_DEVICE);
Ben Hutchings8ceee662008-04-27 12:55:59 +010045 else
Ben Hutchings0e33d872012-05-17 17:46:55 +010046 dma_unmap_page(dma_dev, unmap_addr, buffer->unmap_len,
47 DMA_TO_DEVICE);
Ben Hutchings8ceee662008-04-27 12:55:59 +010048 buffer->unmap_len = 0;
Ben Hutchingsdc8cfa52008-09-01 12:46:50 +010049 buffer->unmap_single = false;
Ben Hutchings8ceee662008-04-27 12:55:59 +010050 }
51
52 if (buffer->skb) {
Tom Herbertc3940992011-11-28 16:33:43 +000053 (*pkts_compl)++;
54 (*bytes_compl) += buffer->skb->len;
Ben Hutchings8ceee662008-04-27 12:55:59 +010055 dev_kfree_skb_any((struct sk_buff *) buffer->skb);
56 buffer->skb = NULL;
Ben Hutchings62776d02010-06-23 11:30:07 +000057 netif_vdbg(tx_queue->efx, tx_done, tx_queue->efx->net_dev,
58 "TX queue %d transmission id %x complete\n",
59 tx_queue->queue, tx_queue->read_count);
Ben Hutchings8ceee662008-04-27 12:55:59 +010060 }
61}
62
Ben Hutchingsb9b39b62008-05-07 12:51:12 +010063/**
64 * struct efx_tso_header - a DMA mapped buffer for packet headers
65 * @next: Linked list of free ones.
66 * The list is protected by the TX queue lock.
67 * @dma_unmap_len: Length to unmap for an oversize buffer, or 0.
68 * @dma_addr: The DMA address of the header below.
69 *
70 * This controls the memory used for a TSO header. Use TSOH_DATA()
71 * to find the packet header data. Use TSOH_SIZE() to calculate the
72 * total size required for a given packet header length. TSO headers
73 * in the free list are exactly %TSOH_STD_SIZE bytes in size.
74 */
75struct efx_tso_header {
76 union {
77 struct efx_tso_header *next;
78 size_t unmap_len;
79 };
80 dma_addr_t dma_addr;
81};
82
83static int efx_enqueue_skb_tso(struct efx_tx_queue *tx_queue,
Ben Hutchings740847d2008-09-01 12:48:23 +010084 struct sk_buff *skb);
Ben Hutchingsb9b39b62008-05-07 12:51:12 +010085static void efx_fini_tso(struct efx_tx_queue *tx_queue);
86static void efx_tsoh_heap_free(struct efx_tx_queue *tx_queue,
87 struct efx_tso_header *tsoh);
88
Ben Hutchings4d566062008-09-01 12:47:12 +010089static void efx_tsoh_free(struct efx_tx_queue *tx_queue,
90 struct efx_tx_buffer *buffer)
Ben Hutchingsb9b39b62008-05-07 12:51:12 +010091{
92 if (buffer->tsoh) {
93 if (likely(!buffer->tsoh->unmap_len)) {
94 buffer->tsoh->next = tx_queue->tso_headers_free;
95 tx_queue->tso_headers_free = buffer->tsoh;
96 } else {
97 efx_tsoh_heap_free(tx_queue, buffer->tsoh);
98 }
99 buffer->tsoh = NULL;
100 }
101}
102
Ben Hutchings8ceee662008-04-27 12:55:59 +0100103
Ben Hutchings63f19882009-10-23 08:31:20 +0000104static inline unsigned
105efx_max_tx_len(struct efx_nic *efx, dma_addr_t dma_addr)
106{
107 /* Depending on the NIC revision, we can use descriptor
108 * lengths up to 8K or 8K-1. However, since PCI Express
109 * devices must split read requests at 4K boundaries, there is
110 * little benefit from using descriptors that cross those
111 * boundaries and we keep things simple by not doing so.
112 */
Ben Hutchings5b6262d2012-02-02 21:21:15 +0000113 unsigned len = (~dma_addr & (EFX_PAGE_SIZE - 1)) + 1;
Ben Hutchings63f19882009-10-23 08:31:20 +0000114
115 /* Work around hardware bug for unaligned buffers. */
116 if (EFX_WORKAROUND_5391(efx) && (dma_addr & 0xf))
117 len = min_t(unsigned, len, 512 - (dma_addr & 0xf));
118
119 return len;
120}
121
Ben Hutchings7e6d06f2012-07-30 15:57:44 +0000122unsigned int efx_tx_max_skb_descs(struct efx_nic *efx)
123{
124 /* Header and payload descriptor for each output segment, plus
125 * one for every input fragment boundary within a segment
126 */
127 unsigned int max_descs = EFX_TSO_MAX_SEGS * 2 + MAX_SKB_FRAGS;
128
129 /* Possibly one more per segment for the alignment workaround */
130 if (EFX_WORKAROUND_5391(efx))
131 max_descs += EFX_TSO_MAX_SEGS;
132
133 /* Possibly more for PCIe page boundaries within input fragments */
134 if (PAGE_SIZE > EFX_PAGE_SIZE)
135 max_descs += max_t(unsigned int, MAX_SKB_FRAGS,
136 DIV_ROUND_UP(GSO_MAX_SIZE, EFX_PAGE_SIZE));
137
138 return max_descs;
139}
140
Ben Hutchings8ceee662008-04-27 12:55:59 +0100141/*
142 * Add a socket buffer to a TX queue
143 *
144 * This maps all fragments of a socket buffer for DMA and adds them to
145 * the TX queue. The queue's insert pointer will be incremented by
146 * the number of fragments in the socket buffer.
147 *
148 * If any DMA mapping fails, any mapped fragments will be unmapped,
149 * the queue's insert pointer will be restored to its original value.
150 *
Ben Hutchings497f5ba2009-11-23 16:07:05 +0000151 * This function is split out from efx_hard_start_xmit to allow the
152 * loopback test to direct packets via specific TX queues.
153 *
Ben Hutchings8ceee662008-04-27 12:55:59 +0100154 * Returns NETDEV_TX_OK or NETDEV_TX_BUSY
155 * You must hold netif_tx_lock() to call this function.
156 */
Ben Hutchings497f5ba2009-11-23 16:07:05 +0000157netdev_tx_t efx_enqueue_skb(struct efx_tx_queue *tx_queue, struct sk_buff *skb)
Ben Hutchings8ceee662008-04-27 12:55:59 +0100158{
159 struct efx_nic *efx = tx_queue->efx;
Ben Hutchings0e33d872012-05-17 17:46:55 +0100160 struct device *dma_dev = &efx->pci_dev->dev;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100161 struct efx_tx_buffer *buffer;
162 skb_frag_t *fragment;
Ben Hutchings63f19882009-10-23 08:31:20 +0000163 unsigned int len, unmap_len = 0, fill_level, insert_ptr;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100164 dma_addr_t dma_addr, unmap_addr = 0;
165 unsigned int dma_len;
Ben Hutchingsdc8cfa52008-09-01 12:46:50 +0100166 bool unmap_single;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100167 int q_space, i = 0;
Stephen Hemminger613573252009-08-31 19:50:58 +0000168 netdev_tx_t rc = NETDEV_TX_OK;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100169
170 EFX_BUG_ON_PARANOID(tx_queue->write_count != tx_queue->insert_count);
171
Ben Hutchings9bc183d2009-11-23 16:06:47 +0000172 if (skb_shinfo(skb)->gso_size)
Ben Hutchingsb9b39b62008-05-07 12:51:12 +0100173 return efx_enqueue_skb_tso(tx_queue, skb);
174
Ben Hutchings8ceee662008-04-27 12:55:59 +0100175 /* Get size of the initial fragment */
176 len = skb_headlen(skb);
177
Ben Hutchingsbb145a92009-03-20 13:25:39 +0000178 /* Pad if necessary */
179 if (EFX_WORKAROUND_15592(efx) && skb->len <= 32) {
180 EFX_BUG_ON_PARANOID(skb->data_len);
181 len = 32 + 1;
182 if (skb_pad(skb, len - skb->len))
183 return NETDEV_TX_OK;
184 }
185
Ben Hutchings8ceee662008-04-27 12:55:59 +0100186 fill_level = tx_queue->insert_count - tx_queue->old_read_count;
Steve Hodgsonecc910f2010-09-10 06:42:22 +0000187 q_space = efx->txq_entries - 1 - fill_level;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100188
Ben Hutchings0e33d872012-05-17 17:46:55 +0100189 /* Map for DMA. Use dma_map_single rather than dma_map_page
Ben Hutchings8ceee662008-04-27 12:55:59 +0100190 * since this is more efficient on machines with sparse
191 * memory.
192 */
Ben Hutchingsdc8cfa52008-09-01 12:46:50 +0100193 unmap_single = true;
Ben Hutchings0e33d872012-05-17 17:46:55 +0100194 dma_addr = dma_map_single(dma_dev, skb->data, len, PCI_DMA_TODEVICE);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100195
196 /* Process all fragments */
197 while (1) {
Ben Hutchings0e33d872012-05-17 17:46:55 +0100198 if (unlikely(dma_mapping_error(dma_dev, dma_addr)))
199 goto dma_err;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100200
201 /* Store fields for marking in the per-fragment final
202 * descriptor */
203 unmap_len = len;
204 unmap_addr = dma_addr;
205
206 /* Add to TX queue, splitting across DMA boundaries */
207 do {
208 if (unlikely(q_space-- <= 0)) {
209 /* It might be that completions have
210 * happened since the xmit path last
211 * checked. Update the xmit path's
212 * copy of read_count.
213 */
Ben Hutchingsc04bfc62010-12-10 01:24:16 +0000214 netif_tx_stop_queue(tx_queue->core_txq);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100215 /* This memory barrier protects the
Ben Hutchingsc04bfc62010-12-10 01:24:16 +0000216 * change of queue state from the access
Ben Hutchings8ceee662008-04-27 12:55:59 +0100217 * of read_count. */
218 smp_mb();
219 tx_queue->old_read_count =
Ben Hutchings51c56f42010-11-10 18:46:40 +0000220 ACCESS_ONCE(tx_queue->read_count);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100221 fill_level = (tx_queue->insert_count
222 - tx_queue->old_read_count);
Steve Hodgsonecc910f2010-09-10 06:42:22 +0000223 q_space = efx->txq_entries - 1 - fill_level;
Ben Hutchingsc04bfc62010-12-10 01:24:16 +0000224 if (unlikely(q_space-- <= 0)) {
225 rc = NETDEV_TX_BUSY;
226 goto unwind;
227 }
Ben Hutchings8ceee662008-04-27 12:55:59 +0100228 smp_mb();
Ben Hutchingse4abce82011-05-16 18:51:24 +0100229 if (likely(!efx->loopback_selftest))
230 netif_tx_start_queue(
231 tx_queue->core_txq);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100232 }
233
Steve Hodgsonecc910f2010-09-10 06:42:22 +0000234 insert_ptr = tx_queue->insert_count & tx_queue->ptr_mask;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100235 buffer = &tx_queue->buffer[insert_ptr];
Ben Hutchingsb9b39b62008-05-07 12:51:12 +0100236 efx_tsoh_free(tx_queue, buffer);
237 EFX_BUG_ON_PARANOID(buffer->tsoh);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100238 EFX_BUG_ON_PARANOID(buffer->skb);
239 EFX_BUG_ON_PARANOID(buffer->len);
Ben Hutchingsdc8cfa52008-09-01 12:46:50 +0100240 EFX_BUG_ON_PARANOID(!buffer->continuation);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100241 EFX_BUG_ON_PARANOID(buffer->unmap_len);
242
Ben Hutchings63f19882009-10-23 08:31:20 +0000243 dma_len = efx_max_tx_len(efx, dma_addr);
244 if (likely(dma_len >= len))
Ben Hutchings8ceee662008-04-27 12:55:59 +0100245 dma_len = len;
246
Ben Hutchings8ceee662008-04-27 12:55:59 +0100247 /* Fill out per descriptor fields */
248 buffer->len = dma_len;
249 buffer->dma_addr = dma_addr;
250 len -= dma_len;
251 dma_addr += dma_len;
252 ++tx_queue->insert_count;
253 } while (len);
254
255 /* Transfer ownership of the unmapping to the final buffer */
Ben Hutchings8ceee662008-04-27 12:55:59 +0100256 buffer->unmap_single = unmap_single;
257 buffer->unmap_len = unmap_len;
258 unmap_len = 0;
259
260 /* Get address and size of next fragment */
261 if (i >= skb_shinfo(skb)->nr_frags)
262 break;
263 fragment = &skb_shinfo(skb)->frags[i];
Eric Dumazet9e903e02011-10-18 21:00:24 +0000264 len = skb_frag_size(fragment);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100265 i++;
266 /* Map for DMA */
Ben Hutchingsdc8cfa52008-09-01 12:46:50 +0100267 unmap_single = false;
Ben Hutchings0e33d872012-05-17 17:46:55 +0100268 dma_addr = skb_frag_dma_map(dma_dev, fragment, 0, len,
Ian Campbell5d6bcdf2011-10-06 11:10:48 +0100269 DMA_TO_DEVICE);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100270 }
271
272 /* Transfer ownership of the skb to the final buffer */
273 buffer->skb = skb;
Ben Hutchingsdc8cfa52008-09-01 12:46:50 +0100274 buffer->continuation = false;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100275
Tom Herbertc3940992011-11-28 16:33:43 +0000276 netdev_tx_sent_queue(tx_queue->core_txq, skb->len);
277
Ben Hutchings8ceee662008-04-27 12:55:59 +0100278 /* Pass off to hardware */
Ben Hutchings152b6a62009-11-29 03:43:56 +0000279 efx_nic_push_buffers(tx_queue);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100280
281 return NETDEV_TX_OK;
282
Ben Hutchings0e33d872012-05-17 17:46:55 +0100283 dma_err:
Ben Hutchings62776d02010-06-23 11:30:07 +0000284 netif_err(efx, tx_err, efx->net_dev,
285 " TX queue %d could not map skb with %d bytes %d "
286 "fragments for DMA\n", tx_queue->queue, skb->len,
287 skb_shinfo(skb)->nr_frags + 1);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100288
289 /* Mark the packet as transmitted, and free the SKB ourselves */
Ben Hutchings9bc183d2009-11-23 16:06:47 +0000290 dev_kfree_skb_any(skb);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100291
292 unwind:
293 /* Work backwards until we hit the original insert pointer value */
294 while (tx_queue->insert_count != tx_queue->write_count) {
Tom Herbertc3940992011-11-28 16:33:43 +0000295 unsigned int pkts_compl = 0, bytes_compl = 0;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100296 --tx_queue->insert_count;
Steve Hodgsonecc910f2010-09-10 06:42:22 +0000297 insert_ptr = tx_queue->insert_count & tx_queue->ptr_mask;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100298 buffer = &tx_queue->buffer[insert_ptr];
Tom Herbertc3940992011-11-28 16:33:43 +0000299 efx_dequeue_buffer(tx_queue, buffer, &pkts_compl, &bytes_compl);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100300 buffer->len = 0;
301 }
302
303 /* Free the fragment we were mid-way through pushing */
Ben Hutchingsecbd95c2008-09-01 12:46:40 +0100304 if (unmap_len) {
305 if (unmap_single)
Ben Hutchings0e33d872012-05-17 17:46:55 +0100306 dma_unmap_single(dma_dev, unmap_addr, unmap_len,
307 DMA_TO_DEVICE);
Ben Hutchingsecbd95c2008-09-01 12:46:40 +0100308 else
Ben Hutchings0e33d872012-05-17 17:46:55 +0100309 dma_unmap_page(dma_dev, unmap_addr, unmap_len,
310 DMA_TO_DEVICE);
Ben Hutchingsecbd95c2008-09-01 12:46:40 +0100311 }
Ben Hutchings8ceee662008-04-27 12:55:59 +0100312
313 return rc;
314}
315
316/* Remove packets from the TX queue
317 *
318 * This removes packets from the TX queue, up to and including the
319 * specified index.
320 */
Ben Hutchings4d566062008-09-01 12:47:12 +0100321static void efx_dequeue_buffers(struct efx_tx_queue *tx_queue,
Tom Herbertc3940992011-11-28 16:33:43 +0000322 unsigned int index,
323 unsigned int *pkts_compl,
324 unsigned int *bytes_compl)
Ben Hutchings8ceee662008-04-27 12:55:59 +0100325{
326 struct efx_nic *efx = tx_queue->efx;
327 unsigned int stop_index, read_ptr;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100328
Steve Hodgsonecc910f2010-09-10 06:42:22 +0000329 stop_index = (index + 1) & tx_queue->ptr_mask;
330 read_ptr = tx_queue->read_count & tx_queue->ptr_mask;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100331
332 while (read_ptr != stop_index) {
333 struct efx_tx_buffer *buffer = &tx_queue->buffer[read_ptr];
334 if (unlikely(buffer->len == 0)) {
Ben Hutchings62776d02010-06-23 11:30:07 +0000335 netif_err(efx, tx_err, efx->net_dev,
336 "TX queue %d spurious TX completion id %x\n",
337 tx_queue->queue, read_ptr);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100338 efx_schedule_reset(efx, RESET_TYPE_TX_SKIP);
339 return;
340 }
341
Tom Herbertc3940992011-11-28 16:33:43 +0000342 efx_dequeue_buffer(tx_queue, buffer, pkts_compl, bytes_compl);
Ben Hutchingsdc8cfa52008-09-01 12:46:50 +0100343 buffer->continuation = true;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100344 buffer->len = 0;
345
346 ++tx_queue->read_count;
Steve Hodgsonecc910f2010-09-10 06:42:22 +0000347 read_ptr = tx_queue->read_count & tx_queue->ptr_mask;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100348 }
349}
350
Ben Hutchings8ceee662008-04-27 12:55:59 +0100351/* Initiate a packet transmission. We use one channel per CPU
352 * (sharing when we have more CPUs than channels). On Falcon, the TX
353 * completion events will be directed back to the CPU that transmitted
354 * the packet, which should be cache-efficient.
355 *
356 * Context: non-blocking.
357 * Note that returning anything other than NETDEV_TX_OK will cause the
358 * OS to free the skb.
359 */
Stephen Hemminger613573252009-08-31 19:50:58 +0000360netdev_tx_t efx_hard_start_xmit(struct sk_buff *skb,
Ben Hutchings2d0cc562012-02-17 00:10:45 +0000361 struct net_device *net_dev)
Ben Hutchings8ceee662008-04-27 12:55:59 +0100362{
Ben Hutchings767e4682008-09-01 12:43:14 +0100363 struct efx_nic *efx = netdev_priv(net_dev);
Ben Hutchings60ac1062008-09-01 12:44:59 +0100364 struct efx_tx_queue *tx_queue;
Ben Hutchings94b274b2011-01-10 21:18:20 +0000365 unsigned index, type;
Ben Hutchings60ac1062008-09-01 12:44:59 +0100366
Ben Hutchingse4abce82011-05-16 18:51:24 +0100367 EFX_WARN_ON_PARANOID(!netif_device_present(net_dev));
Ben Hutchingsa7ef5932009-03-04 09:52:37 +0000368
Ben Hutchings94b274b2011-01-10 21:18:20 +0000369 index = skb_get_queue_mapping(skb);
370 type = skb->ip_summed == CHECKSUM_PARTIAL ? EFX_TXQ_TYPE_OFFLOAD : 0;
371 if (index >= efx->n_tx_channels) {
372 index -= efx->n_tx_channels;
373 type |= EFX_TXQ_TYPE_HIGHPRI;
374 }
375 tx_queue = efx_get_tx_queue(efx, index, type);
Ben Hutchings60ac1062008-09-01 12:44:59 +0100376
Ben Hutchings497f5ba2009-11-23 16:07:05 +0000377 return efx_enqueue_skb(tx_queue, skb);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100378}
379
Ben Hutchings60031fc2011-01-12 18:39:40 +0000380void efx_init_tx_queue_core_txq(struct efx_tx_queue *tx_queue)
381{
Ben Hutchings94b274b2011-01-10 21:18:20 +0000382 struct efx_nic *efx = tx_queue->efx;
383
Ben Hutchings60031fc2011-01-12 18:39:40 +0000384 /* Must be inverse of queue lookup in efx_hard_start_xmit() */
Ben Hutchings94b274b2011-01-10 21:18:20 +0000385 tx_queue->core_txq =
386 netdev_get_tx_queue(efx->net_dev,
387 tx_queue->queue / EFX_TXQ_TYPES +
388 ((tx_queue->queue & EFX_TXQ_TYPE_HIGHPRI) ?
389 efx->n_tx_channels : 0));
390}
391
392int efx_setup_tc(struct net_device *net_dev, u8 num_tc)
393{
394 struct efx_nic *efx = netdev_priv(net_dev);
395 struct efx_channel *channel;
396 struct efx_tx_queue *tx_queue;
397 unsigned tc;
398 int rc;
399
400 if (efx_nic_rev(efx) < EFX_REV_FALCON_B0 || num_tc > EFX_MAX_TX_TC)
401 return -EINVAL;
402
403 if (num_tc == net_dev->num_tc)
404 return 0;
405
406 for (tc = 0; tc < num_tc; tc++) {
407 net_dev->tc_to_txq[tc].offset = tc * efx->n_tx_channels;
408 net_dev->tc_to_txq[tc].count = efx->n_tx_channels;
409 }
410
411 if (num_tc > net_dev->num_tc) {
412 /* Initialise high-priority queues as necessary */
413 efx_for_each_channel(channel, efx) {
414 efx_for_each_possible_channel_tx_queue(tx_queue,
415 channel) {
416 if (!(tx_queue->queue & EFX_TXQ_TYPE_HIGHPRI))
417 continue;
418 if (!tx_queue->buffer) {
419 rc = efx_probe_tx_queue(tx_queue);
420 if (rc)
421 return rc;
422 }
423 if (!tx_queue->initialised)
424 efx_init_tx_queue(tx_queue);
425 efx_init_tx_queue_core_txq(tx_queue);
426 }
427 }
428 } else {
429 /* Reduce number of classes before number of queues */
430 net_dev->num_tc = num_tc;
431 }
432
433 rc = netif_set_real_num_tx_queues(net_dev,
434 max_t(int, num_tc, 1) *
435 efx->n_tx_channels);
436 if (rc)
437 return rc;
438
439 /* Do not destroy high-priority queues when they become
440 * unused. We would have to flush them first, and it is
441 * fairly difficult to flush a subset of TX queues. Leave
442 * it to efx_fini_channels().
443 */
444
445 net_dev->num_tc = num_tc;
446 return 0;
Ben Hutchings60031fc2011-01-12 18:39:40 +0000447}
448
Ben Hutchings8ceee662008-04-27 12:55:59 +0100449void efx_xmit_done(struct efx_tx_queue *tx_queue, unsigned int index)
450{
451 unsigned fill_level;
452 struct efx_nic *efx = tx_queue->efx;
Tom Herbertc3940992011-11-28 16:33:43 +0000453 unsigned int pkts_compl = 0, bytes_compl = 0;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100454
Steve Hodgsonecc910f2010-09-10 06:42:22 +0000455 EFX_BUG_ON_PARANOID(index > tx_queue->ptr_mask);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100456
Tom Herbertc3940992011-11-28 16:33:43 +0000457 efx_dequeue_buffers(tx_queue, index, &pkts_compl, &bytes_compl);
458 netdev_tx_completed_queue(tx_queue->core_txq, pkts_compl, bytes_compl);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100459
460 /* See if we need to restart the netif queue. This barrier
Ben Hutchingsc04bfc62010-12-10 01:24:16 +0000461 * separates the update of read_count from the test of the
462 * queue state. */
Ben Hutchings8ceee662008-04-27 12:55:59 +0100463 smp_mb();
Ben Hutchingsc04bfc62010-12-10 01:24:16 +0000464 if (unlikely(netif_tx_queue_stopped(tx_queue->core_txq)) &&
Neil Turton9d1aea62011-04-04 13:46:23 +0100465 likely(efx->port_enabled) &&
Ben Hutchingse4abce82011-05-16 18:51:24 +0100466 likely(netif_device_present(efx->net_dev))) {
Ben Hutchings8ceee662008-04-27 12:55:59 +0100467 fill_level = tx_queue->insert_count - tx_queue->read_count;
Ben Hutchings73ba7b62012-01-09 19:47:08 +0000468 if (fill_level < EFX_TXQ_THRESHOLD(efx))
Ben Hutchingsc04bfc62010-12-10 01:24:16 +0000469 netif_tx_wake_queue(tx_queue->core_txq);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100470 }
Ben Hutchingscd385572010-11-15 23:53:11 +0000471
472 /* Check whether the hardware queue is now empty */
473 if ((int)(tx_queue->read_count - tx_queue->old_write_count) >= 0) {
474 tx_queue->old_write_count = ACCESS_ONCE(tx_queue->write_count);
475 if (tx_queue->read_count == tx_queue->old_write_count) {
476 smp_mb();
477 tx_queue->empty_read_count =
478 tx_queue->read_count | EFX_EMPTY_COUNT_VALID;
479 }
480 }
Ben Hutchings8ceee662008-04-27 12:55:59 +0100481}
482
483int efx_probe_tx_queue(struct efx_tx_queue *tx_queue)
484{
485 struct efx_nic *efx = tx_queue->efx;
Steve Hodgsonecc910f2010-09-10 06:42:22 +0000486 unsigned int entries;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100487 int i, rc;
488
Steve Hodgsonecc910f2010-09-10 06:42:22 +0000489 /* Create the smallest power-of-two aligned ring */
490 entries = max(roundup_pow_of_two(efx->txq_entries), EFX_MIN_DMAQ_SIZE);
491 EFX_BUG_ON_PARANOID(entries > EFX_MAX_DMAQ_SIZE);
492 tx_queue->ptr_mask = entries - 1;
493
494 netif_dbg(efx, probe, efx->net_dev,
495 "creating TX queue %d size %#x mask %#x\n",
496 tx_queue->queue, efx->txq_entries, tx_queue->ptr_mask);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100497
498 /* Allocate software ring */
Thomas Meyerc2e4e252011-12-02 12:36:13 +0000499 tx_queue->buffer = kcalloc(entries, sizeof(*tx_queue->buffer),
Steve Hodgsonecc910f2010-09-10 06:42:22 +0000500 GFP_KERNEL);
Ben Hutchings60ac1062008-09-01 12:44:59 +0100501 if (!tx_queue->buffer)
502 return -ENOMEM;
Steve Hodgsonecc910f2010-09-10 06:42:22 +0000503 for (i = 0; i <= tx_queue->ptr_mask; ++i)
Ben Hutchingsdc8cfa52008-09-01 12:46:50 +0100504 tx_queue->buffer[i].continuation = true;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100505
506 /* Allocate hardware ring */
Ben Hutchings152b6a62009-11-29 03:43:56 +0000507 rc = efx_nic_probe_tx(tx_queue);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100508 if (rc)
Ben Hutchings60ac1062008-09-01 12:44:59 +0100509 goto fail;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100510
511 return 0;
512
Ben Hutchings60ac1062008-09-01 12:44:59 +0100513 fail:
Ben Hutchings8ceee662008-04-27 12:55:59 +0100514 kfree(tx_queue->buffer);
515 tx_queue->buffer = NULL;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100516 return rc;
517}
518
Ben Hutchingsbc3c90a2008-09-01 12:48:46 +0100519void efx_init_tx_queue(struct efx_tx_queue *tx_queue)
Ben Hutchings8ceee662008-04-27 12:55:59 +0100520{
Ben Hutchings62776d02010-06-23 11:30:07 +0000521 netif_dbg(tx_queue->efx, drv, tx_queue->efx->net_dev,
522 "initialising TX queue %d\n", tx_queue->queue);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100523
524 tx_queue->insert_count = 0;
525 tx_queue->write_count = 0;
Ben Hutchingscd385572010-11-15 23:53:11 +0000526 tx_queue->old_write_count = 0;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100527 tx_queue->read_count = 0;
528 tx_queue->old_read_count = 0;
Ben Hutchingscd385572010-11-15 23:53:11 +0000529 tx_queue->empty_read_count = 0 | EFX_EMPTY_COUNT_VALID;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100530
531 /* Set up TX descriptor ring */
Ben Hutchings152b6a62009-11-29 03:43:56 +0000532 efx_nic_init_tx(tx_queue);
Ben Hutchings94b274b2011-01-10 21:18:20 +0000533
534 tx_queue->initialised = true;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100535}
536
537void efx_release_tx_buffers(struct efx_tx_queue *tx_queue)
538{
539 struct efx_tx_buffer *buffer;
540
541 if (!tx_queue->buffer)
542 return;
543
544 /* Free any buffers left in the ring */
545 while (tx_queue->read_count != tx_queue->write_count) {
Tom Herbertc3940992011-11-28 16:33:43 +0000546 unsigned int pkts_compl = 0, bytes_compl = 0;
Steve Hodgsonecc910f2010-09-10 06:42:22 +0000547 buffer = &tx_queue->buffer[tx_queue->read_count & tx_queue->ptr_mask];
Tom Herbertc3940992011-11-28 16:33:43 +0000548 efx_dequeue_buffer(tx_queue, buffer, &pkts_compl, &bytes_compl);
Ben Hutchingsdc8cfa52008-09-01 12:46:50 +0100549 buffer->continuation = true;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100550 buffer->len = 0;
551
552 ++tx_queue->read_count;
553 }
Tom Herbertc3940992011-11-28 16:33:43 +0000554 netdev_tx_reset_queue(tx_queue->core_txq);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100555}
556
557void efx_fini_tx_queue(struct efx_tx_queue *tx_queue)
558{
Ben Hutchings94b274b2011-01-10 21:18:20 +0000559 if (!tx_queue->initialised)
560 return;
561
Ben Hutchings62776d02010-06-23 11:30:07 +0000562 netif_dbg(tx_queue->efx, drv, tx_queue->efx->net_dev,
563 "shutting down TX queue %d\n", tx_queue->queue);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100564
Ben Hutchings94b274b2011-01-10 21:18:20 +0000565 tx_queue->initialised = false;
566
Ben Hutchings8ceee662008-04-27 12:55:59 +0100567 /* Flush TX queue, remove descriptor ring */
Ben Hutchings152b6a62009-11-29 03:43:56 +0000568 efx_nic_fini_tx(tx_queue);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100569
570 efx_release_tx_buffers(tx_queue);
571
Ben Hutchingsb9b39b62008-05-07 12:51:12 +0100572 /* Free up TSO header cache */
573 efx_fini_tso(tx_queue);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100574}
575
576void efx_remove_tx_queue(struct efx_tx_queue *tx_queue)
577{
Ben Hutchings94b274b2011-01-10 21:18:20 +0000578 if (!tx_queue->buffer)
579 return;
580
Ben Hutchings62776d02010-06-23 11:30:07 +0000581 netif_dbg(tx_queue->efx, drv, tx_queue->efx->net_dev,
582 "destroying TX queue %d\n", tx_queue->queue);
Ben Hutchings152b6a62009-11-29 03:43:56 +0000583 efx_nic_remove_tx(tx_queue);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100584
585 kfree(tx_queue->buffer);
586 tx_queue->buffer = NULL;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100587}
588
589
Ben Hutchingsb9b39b62008-05-07 12:51:12 +0100590/* Efx TCP segmentation acceleration.
591 *
592 * Why? Because by doing it here in the driver we can go significantly
593 * faster than the GSO.
594 *
595 * Requires TX checksum offload support.
596 */
597
598/* Number of bytes inserted at the start of a TSO header buffer,
599 * similar to NET_IP_ALIGN.
600 */
Ben Hutchings13e9ab12008-09-01 12:50:28 +0100601#ifdef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
Ben Hutchingsb9b39b62008-05-07 12:51:12 +0100602#define TSOH_OFFSET 0
603#else
604#define TSOH_OFFSET NET_IP_ALIGN
605#endif
606
607#define TSOH_BUFFER(tsoh) ((u8 *)(tsoh + 1) + TSOH_OFFSET)
608
609/* Total size of struct efx_tso_header, buffer and padding */
610#define TSOH_SIZE(hdr_len) \
611 (sizeof(struct efx_tso_header) + TSOH_OFFSET + hdr_len)
612
613/* Size of blocks on free list. Larger blocks must be allocated from
614 * the heap.
615 */
616#define TSOH_STD_SIZE 128
617
618#define PTR_DIFF(p1, p2) ((u8 *)(p1) - (u8 *)(p2))
619#define ETH_HDR_LEN(skb) (skb_network_header(skb) - (skb)->data)
620#define SKB_TCP_OFF(skb) PTR_DIFF(tcp_hdr(skb), (skb)->data)
621#define SKB_IPV4_OFF(skb) PTR_DIFF(ip_hdr(skb), (skb)->data)
Ben Hutchings738a8f42009-11-29 15:16:05 +0000622#define SKB_IPV6_OFF(skb) PTR_DIFF(ipv6_hdr(skb), (skb)->data)
Ben Hutchingsb9b39b62008-05-07 12:51:12 +0100623
624/**
625 * struct tso_state - TSO state for an SKB
Ben Hutchings23d9e602008-09-01 12:47:02 +0100626 * @out_len: Remaining length in current segment
Ben Hutchingsb9b39b62008-05-07 12:51:12 +0100627 * @seqnum: Current sequence number
Ben Hutchings23d9e602008-09-01 12:47:02 +0100628 * @ipv4_id: Current IPv4 ID, host endian
Ben Hutchingsb9b39b62008-05-07 12:51:12 +0100629 * @packet_space: Remaining space in current packet
Ben Hutchings23d9e602008-09-01 12:47:02 +0100630 * @dma_addr: DMA address of current position
631 * @in_len: Remaining length in current SKB fragment
632 * @unmap_len: Length of SKB fragment
633 * @unmap_addr: DMA address of SKB fragment
634 * @unmap_single: DMA single vs page mapping flag
Ben Hutchings738a8f42009-11-29 15:16:05 +0000635 * @protocol: Network protocol (after any VLAN header)
Ben Hutchings23d9e602008-09-01 12:47:02 +0100636 * @header_len: Number of bytes of header
637 * @full_packet_size: Number of bytes to put in each outgoing segment
Ben Hutchingsb9b39b62008-05-07 12:51:12 +0100638 *
639 * The state used during segmentation. It is put into this data structure
640 * just to make it easy to pass into inline functions.
641 */
642struct tso_state {
Ben Hutchings23d9e602008-09-01 12:47:02 +0100643 /* Output position */
644 unsigned out_len;
Ben Hutchingsb9b39b62008-05-07 12:51:12 +0100645 unsigned seqnum;
Ben Hutchings23d9e602008-09-01 12:47:02 +0100646 unsigned ipv4_id;
Ben Hutchingsb9b39b62008-05-07 12:51:12 +0100647 unsigned packet_space;
648
Ben Hutchings23d9e602008-09-01 12:47:02 +0100649 /* Input position */
650 dma_addr_t dma_addr;
651 unsigned in_len;
652 unsigned unmap_len;
653 dma_addr_t unmap_addr;
654 bool unmap_single;
Ben Hutchingsb9b39b62008-05-07 12:51:12 +0100655
Ben Hutchings738a8f42009-11-29 15:16:05 +0000656 __be16 protocol;
Ben Hutchings23d9e602008-09-01 12:47:02 +0100657 unsigned header_len;
658 int full_packet_size;
Ben Hutchingsb9b39b62008-05-07 12:51:12 +0100659};
660
661
662/*
663 * Verify that our various assumptions about sk_buffs and the conditions
Ben Hutchings738a8f42009-11-29 15:16:05 +0000664 * under which TSO will be attempted hold true. Return the protocol number.
Ben Hutchingsb9b39b62008-05-07 12:51:12 +0100665 */
Ben Hutchings738a8f42009-11-29 15:16:05 +0000666static __be16 efx_tso_check_protocol(struct sk_buff *skb)
Ben Hutchingsb9b39b62008-05-07 12:51:12 +0100667{
Ben Hutchings740847d2008-09-01 12:48:23 +0100668 __be16 protocol = skb->protocol;
669
Ben Hutchingsb9b39b62008-05-07 12:51:12 +0100670 EFX_BUG_ON_PARANOID(((struct ethhdr *)skb->data)->h_proto !=
Ben Hutchings740847d2008-09-01 12:48:23 +0100671 protocol);
672 if (protocol == htons(ETH_P_8021Q)) {
Ben Hutchings740847d2008-09-01 12:48:23 +0100673 struct vlan_ethhdr *veh = (struct vlan_ethhdr *)skb->data;
674 protocol = veh->h_vlan_encapsulated_proto;
Ben Hutchings740847d2008-09-01 12:48:23 +0100675 }
676
Ben Hutchings738a8f42009-11-29 15:16:05 +0000677 if (protocol == htons(ETH_P_IP)) {
678 EFX_BUG_ON_PARANOID(ip_hdr(skb)->protocol != IPPROTO_TCP);
679 } else {
680 EFX_BUG_ON_PARANOID(protocol != htons(ETH_P_IPV6));
681 EFX_BUG_ON_PARANOID(ipv6_hdr(skb)->nexthdr != NEXTHDR_TCP);
682 }
Ben Hutchingsb9b39b62008-05-07 12:51:12 +0100683 EFX_BUG_ON_PARANOID((PTR_DIFF(tcp_hdr(skb), skb->data)
684 + (tcp_hdr(skb)->doff << 2u)) >
685 skb_headlen(skb));
Ben Hutchings738a8f42009-11-29 15:16:05 +0000686
687 return protocol;
Ben Hutchingsb9b39b62008-05-07 12:51:12 +0100688}
689
690
691/*
692 * Allocate a page worth of efx_tso_header structures, and string them
693 * into the tx_queue->tso_headers_free linked list. Return 0 or -ENOMEM.
694 */
695static int efx_tsoh_block_alloc(struct efx_tx_queue *tx_queue)
696{
Ben Hutchings0e33d872012-05-17 17:46:55 +0100697 struct device *dma_dev = &tx_queue->efx->pci_dev->dev;
Ben Hutchingsb9b39b62008-05-07 12:51:12 +0100698 struct efx_tso_header *tsoh;
699 dma_addr_t dma_addr;
700 u8 *base_kva, *kva;
701
Ben Hutchings0e33d872012-05-17 17:46:55 +0100702 base_kva = dma_alloc_coherent(dma_dev, PAGE_SIZE, &dma_addr, GFP_ATOMIC);
Ben Hutchingsb9b39b62008-05-07 12:51:12 +0100703 if (base_kva == NULL) {
Ben Hutchings62776d02010-06-23 11:30:07 +0000704 netif_err(tx_queue->efx, tx_err, tx_queue->efx->net_dev,
705 "Unable to allocate page for TSO headers\n");
Ben Hutchingsb9b39b62008-05-07 12:51:12 +0100706 return -ENOMEM;
707 }
708
Ben Hutchings0e33d872012-05-17 17:46:55 +0100709 /* dma_alloc_coherent() allocates pages. */
Ben Hutchingsb9b39b62008-05-07 12:51:12 +0100710 EFX_BUG_ON_PARANOID(dma_addr & (PAGE_SIZE - 1u));
711
712 for (kva = base_kva; kva < base_kva + PAGE_SIZE; kva += TSOH_STD_SIZE) {
713 tsoh = (struct efx_tso_header *)kva;
714 tsoh->dma_addr = dma_addr + (TSOH_BUFFER(tsoh) - base_kva);
715 tsoh->next = tx_queue->tso_headers_free;
716 tx_queue->tso_headers_free = tsoh;
717 }
718
719 return 0;
720}
721
722
723/* Free up a TSO header, and all others in the same page. */
724static void efx_tsoh_block_free(struct efx_tx_queue *tx_queue,
725 struct efx_tso_header *tsoh,
Ben Hutchings0e33d872012-05-17 17:46:55 +0100726 struct device *dma_dev)
Ben Hutchingsb9b39b62008-05-07 12:51:12 +0100727{
728 struct efx_tso_header **p;
729 unsigned long base_kva;
730 dma_addr_t base_dma;
731
732 base_kva = (unsigned long)tsoh & PAGE_MASK;
733 base_dma = tsoh->dma_addr & PAGE_MASK;
734
735 p = &tx_queue->tso_headers_free;
Ben Hutchingsb3475642008-05-16 21:15:49 +0100736 while (*p != NULL) {
Ben Hutchingsb9b39b62008-05-07 12:51:12 +0100737 if (((unsigned long)*p & PAGE_MASK) == base_kva)
738 *p = (*p)->next;
739 else
740 p = &(*p)->next;
Ben Hutchingsb3475642008-05-16 21:15:49 +0100741 }
Ben Hutchingsb9b39b62008-05-07 12:51:12 +0100742
Ben Hutchings0e33d872012-05-17 17:46:55 +0100743 dma_free_coherent(dma_dev, PAGE_SIZE, (void *)base_kva, base_dma);
Ben Hutchingsb9b39b62008-05-07 12:51:12 +0100744}
745
746static struct efx_tso_header *
747efx_tsoh_heap_alloc(struct efx_tx_queue *tx_queue, size_t header_len)
748{
749 struct efx_tso_header *tsoh;
750
751 tsoh = kmalloc(TSOH_SIZE(header_len), GFP_ATOMIC | GFP_DMA);
752 if (unlikely(!tsoh))
753 return NULL;
754
Ben Hutchings0e33d872012-05-17 17:46:55 +0100755 tsoh->dma_addr = dma_map_single(&tx_queue->efx->pci_dev->dev,
Ben Hutchingsb9b39b62008-05-07 12:51:12 +0100756 TSOH_BUFFER(tsoh), header_len,
Ben Hutchings0e33d872012-05-17 17:46:55 +0100757 DMA_TO_DEVICE);
758 if (unlikely(dma_mapping_error(&tx_queue->efx->pci_dev->dev,
759 tsoh->dma_addr))) {
Ben Hutchingsb9b39b62008-05-07 12:51:12 +0100760 kfree(tsoh);
761 return NULL;
762 }
763
764 tsoh->unmap_len = header_len;
765 return tsoh;
766}
767
768static void
769efx_tsoh_heap_free(struct efx_tx_queue *tx_queue, struct efx_tso_header *tsoh)
770{
Ben Hutchings0e33d872012-05-17 17:46:55 +0100771 dma_unmap_single(&tx_queue->efx->pci_dev->dev,
Ben Hutchingsb9b39b62008-05-07 12:51:12 +0100772 tsoh->dma_addr, tsoh->unmap_len,
Ben Hutchings0e33d872012-05-17 17:46:55 +0100773 DMA_TO_DEVICE);
Ben Hutchingsb9b39b62008-05-07 12:51:12 +0100774 kfree(tsoh);
775}
776
777/**
778 * efx_tx_queue_insert - push descriptors onto the TX queue
779 * @tx_queue: Efx TX queue
780 * @dma_addr: DMA address of fragment
781 * @len: Length of fragment
Ben Hutchingsecbd95c2008-09-01 12:46:40 +0100782 * @final_buffer: The final buffer inserted into the queue
Ben Hutchingsb9b39b62008-05-07 12:51:12 +0100783 *
784 * Push descriptors onto the TX queue. Return 0 on success or 1 if
785 * @tx_queue full.
786 */
787static int efx_tx_queue_insert(struct efx_tx_queue *tx_queue,
788 dma_addr_t dma_addr, unsigned len,
Ben Hutchingsecbd95c2008-09-01 12:46:40 +0100789 struct efx_tx_buffer **final_buffer)
Ben Hutchingsb9b39b62008-05-07 12:51:12 +0100790{
791 struct efx_tx_buffer *buffer;
792 struct efx_nic *efx = tx_queue->efx;
Ben Hutchings63f19882009-10-23 08:31:20 +0000793 unsigned dma_len, fill_level, insert_ptr;
Ben Hutchingsb9b39b62008-05-07 12:51:12 +0100794 int q_space;
795
796 EFX_BUG_ON_PARANOID(len <= 0);
797
798 fill_level = tx_queue->insert_count - tx_queue->old_read_count;
799 /* -1 as there is no way to represent all descriptors used */
Steve Hodgsonecc910f2010-09-10 06:42:22 +0000800 q_space = efx->txq_entries - 1 - fill_level;
Ben Hutchingsb9b39b62008-05-07 12:51:12 +0100801
802 while (1) {
803 if (unlikely(q_space-- <= 0)) {
804 /* It might be that completions have happened
805 * since the xmit path last checked. Update
806 * the xmit path's copy of read_count.
807 */
Ben Hutchingsc04bfc62010-12-10 01:24:16 +0000808 netif_tx_stop_queue(tx_queue->core_txq);
Ben Hutchingsb9b39b62008-05-07 12:51:12 +0100809 /* This memory barrier protects the change of
Ben Hutchingsc04bfc62010-12-10 01:24:16 +0000810 * queue state from the access of read_count. */
Ben Hutchingsb9b39b62008-05-07 12:51:12 +0100811 smp_mb();
812 tx_queue->old_read_count =
Ben Hutchings51c56f42010-11-10 18:46:40 +0000813 ACCESS_ONCE(tx_queue->read_count);
Ben Hutchingsb9b39b62008-05-07 12:51:12 +0100814 fill_level = (tx_queue->insert_count
815 - tx_queue->old_read_count);
Steve Hodgsonecc910f2010-09-10 06:42:22 +0000816 q_space = efx->txq_entries - 1 - fill_level;
Ben Hutchingsecbd95c2008-09-01 12:46:40 +0100817 if (unlikely(q_space-- <= 0)) {
818 *final_buffer = NULL;
Ben Hutchingsb9b39b62008-05-07 12:51:12 +0100819 return 1;
Ben Hutchingsecbd95c2008-09-01 12:46:40 +0100820 }
Ben Hutchingsb9b39b62008-05-07 12:51:12 +0100821 smp_mb();
Ben Hutchingsc04bfc62010-12-10 01:24:16 +0000822 netif_tx_start_queue(tx_queue->core_txq);
Ben Hutchingsb9b39b62008-05-07 12:51:12 +0100823 }
824
Steve Hodgsonecc910f2010-09-10 06:42:22 +0000825 insert_ptr = tx_queue->insert_count & tx_queue->ptr_mask;
Ben Hutchingsb9b39b62008-05-07 12:51:12 +0100826 buffer = &tx_queue->buffer[insert_ptr];
827 ++tx_queue->insert_count;
828
829 EFX_BUG_ON_PARANOID(tx_queue->insert_count -
Steve Hodgsonecc910f2010-09-10 06:42:22 +0000830 tx_queue->read_count >=
831 efx->txq_entries);
Ben Hutchingsb9b39b62008-05-07 12:51:12 +0100832
833 efx_tsoh_free(tx_queue, buffer);
834 EFX_BUG_ON_PARANOID(buffer->len);
835 EFX_BUG_ON_PARANOID(buffer->unmap_len);
836 EFX_BUG_ON_PARANOID(buffer->skb);
Ben Hutchingsdc8cfa52008-09-01 12:46:50 +0100837 EFX_BUG_ON_PARANOID(!buffer->continuation);
Ben Hutchingsb9b39b62008-05-07 12:51:12 +0100838 EFX_BUG_ON_PARANOID(buffer->tsoh);
839
840 buffer->dma_addr = dma_addr;
841
Ben Hutchings63f19882009-10-23 08:31:20 +0000842 dma_len = efx_max_tx_len(efx, dma_addr);
Ben Hutchingsb9b39b62008-05-07 12:51:12 +0100843
844 /* If there is enough space to send then do so */
845 if (dma_len >= len)
846 break;
847
848 buffer->len = dma_len; /* Don't set the other members */
849 dma_addr += dma_len;
850 len -= dma_len;
851 }
852
853 EFX_BUG_ON_PARANOID(!len);
854 buffer->len = len;
Ben Hutchingsecbd95c2008-09-01 12:46:40 +0100855 *final_buffer = buffer;
Ben Hutchingsb9b39b62008-05-07 12:51:12 +0100856 return 0;
857}
858
859
860/*
861 * Put a TSO header into the TX queue.
862 *
863 * This is special-cased because we know that it is small enough to fit in
864 * a single fragment, and we know it doesn't cross a page boundary. It
865 * also allows us to not worry about end-of-packet etc.
866 */
Ben Hutchings4d566062008-09-01 12:47:12 +0100867static void efx_tso_put_header(struct efx_tx_queue *tx_queue,
868 struct efx_tso_header *tsoh, unsigned len)
Ben Hutchingsb9b39b62008-05-07 12:51:12 +0100869{
870 struct efx_tx_buffer *buffer;
871
Steve Hodgsonecc910f2010-09-10 06:42:22 +0000872 buffer = &tx_queue->buffer[tx_queue->insert_count & tx_queue->ptr_mask];
Ben Hutchingsb9b39b62008-05-07 12:51:12 +0100873 efx_tsoh_free(tx_queue, buffer);
874 EFX_BUG_ON_PARANOID(buffer->len);
875 EFX_BUG_ON_PARANOID(buffer->unmap_len);
876 EFX_BUG_ON_PARANOID(buffer->skb);
Ben Hutchingsdc8cfa52008-09-01 12:46:50 +0100877 EFX_BUG_ON_PARANOID(!buffer->continuation);
Ben Hutchingsb9b39b62008-05-07 12:51:12 +0100878 EFX_BUG_ON_PARANOID(buffer->tsoh);
879 buffer->len = len;
880 buffer->dma_addr = tsoh->dma_addr;
881 buffer->tsoh = tsoh;
882
883 ++tx_queue->insert_count;
884}
885
886
887/* Remove descriptors put into a tx_queue. */
888static void efx_enqueue_unwind(struct efx_tx_queue *tx_queue)
889{
890 struct efx_tx_buffer *buffer;
Ben Hutchingscc12dac2008-09-01 12:46:43 +0100891 dma_addr_t unmap_addr;
Ben Hutchingsb9b39b62008-05-07 12:51:12 +0100892
893 /* Work backwards until we hit the original insert pointer value */
894 while (tx_queue->insert_count != tx_queue->write_count) {
895 --tx_queue->insert_count;
896 buffer = &tx_queue->buffer[tx_queue->insert_count &
Steve Hodgsonecc910f2010-09-10 06:42:22 +0000897 tx_queue->ptr_mask];
Ben Hutchingsb9b39b62008-05-07 12:51:12 +0100898 efx_tsoh_free(tx_queue, buffer);
899 EFX_BUG_ON_PARANOID(buffer->skb);
Ben Hutchingsb9b39b62008-05-07 12:51:12 +0100900 if (buffer->unmap_len) {
Ben Hutchingscc12dac2008-09-01 12:46:43 +0100901 unmap_addr = (buffer->dma_addr + buffer->len -
902 buffer->unmap_len);
Ben Hutchingsecbd95c2008-09-01 12:46:40 +0100903 if (buffer->unmap_single)
Ben Hutchings0e33d872012-05-17 17:46:55 +0100904 dma_unmap_single(&tx_queue->efx->pci_dev->dev,
Ben Hutchingscc12dac2008-09-01 12:46:43 +0100905 unmap_addr, buffer->unmap_len,
Ben Hutchings0e33d872012-05-17 17:46:55 +0100906 DMA_TO_DEVICE);
Ben Hutchingsecbd95c2008-09-01 12:46:40 +0100907 else
Ben Hutchings0e33d872012-05-17 17:46:55 +0100908 dma_unmap_page(&tx_queue->efx->pci_dev->dev,
Ben Hutchingscc12dac2008-09-01 12:46:43 +0100909 unmap_addr, buffer->unmap_len,
Ben Hutchings0e33d872012-05-17 17:46:55 +0100910 DMA_TO_DEVICE);
Ben Hutchingsb9b39b62008-05-07 12:51:12 +0100911 buffer->unmap_len = 0;
912 }
Neil Turtona7ebd272009-12-23 13:47:13 +0000913 buffer->len = 0;
914 buffer->continuation = true;
Ben Hutchingsb9b39b62008-05-07 12:51:12 +0100915 }
916}
917
918
919/* Parse the SKB header and initialise state. */
Ben Hutchings4d566062008-09-01 12:47:12 +0100920static void tso_start(struct tso_state *st, const struct sk_buff *skb)
Ben Hutchingsb9b39b62008-05-07 12:51:12 +0100921{
922 /* All ethernet/IP/TCP headers combined size is TCP header size
923 * plus offset of TCP header relative to start of packet.
924 */
Ben Hutchings23d9e602008-09-01 12:47:02 +0100925 st->header_len = ((tcp_hdr(skb)->doff << 2u)
926 + PTR_DIFF(tcp_hdr(skb), skb->data));
927 st->full_packet_size = st->header_len + skb_shinfo(skb)->gso_size;
Ben Hutchingsb9b39b62008-05-07 12:51:12 +0100928
Ben Hutchings738a8f42009-11-29 15:16:05 +0000929 if (st->protocol == htons(ETH_P_IP))
930 st->ipv4_id = ntohs(ip_hdr(skb)->id);
931 else
932 st->ipv4_id = 0;
Ben Hutchingsb9b39b62008-05-07 12:51:12 +0100933 st->seqnum = ntohl(tcp_hdr(skb)->seq);
934
935 EFX_BUG_ON_PARANOID(tcp_hdr(skb)->urg);
936 EFX_BUG_ON_PARANOID(tcp_hdr(skb)->syn);
937 EFX_BUG_ON_PARANOID(tcp_hdr(skb)->rst);
938
Ben Hutchings23d9e602008-09-01 12:47:02 +0100939 st->out_len = skb->len - st->header_len;
940 st->unmap_len = 0;
941 st->unmap_single = false;
Ben Hutchingsb9b39b62008-05-07 12:51:12 +0100942}
943
Ben Hutchings4d566062008-09-01 12:47:12 +0100944static int tso_get_fragment(struct tso_state *st, struct efx_nic *efx,
945 skb_frag_t *frag)
Ben Hutchingsb9b39b62008-05-07 12:51:12 +0100946{
Ian Campbell4a22c4c2011-09-21 21:53:16 +0000947 st->unmap_addr = skb_frag_dma_map(&efx->pci_dev->dev, frag, 0,
Eric Dumazet9e903e02011-10-18 21:00:24 +0000948 skb_frag_size(frag), DMA_TO_DEVICE);
Ian Campbell5d6bcdf2011-10-06 11:10:48 +0100949 if (likely(!dma_mapping_error(&efx->pci_dev->dev, st->unmap_addr))) {
Ben Hutchings23d9e602008-09-01 12:47:02 +0100950 st->unmap_single = false;
Eric Dumazet9e903e02011-10-18 21:00:24 +0000951 st->unmap_len = skb_frag_size(frag);
952 st->in_len = skb_frag_size(frag);
Ben Hutchings23d9e602008-09-01 12:47:02 +0100953 st->dma_addr = st->unmap_addr;
Ben Hutchingsecbd95c2008-09-01 12:46:40 +0100954 return 0;
955 }
956 return -ENOMEM;
957}
958
Ben Hutchings4d566062008-09-01 12:47:12 +0100959static int tso_get_head_fragment(struct tso_state *st, struct efx_nic *efx,
960 const struct sk_buff *skb)
Ben Hutchingsecbd95c2008-09-01 12:46:40 +0100961{
Ben Hutchings23d9e602008-09-01 12:47:02 +0100962 int hl = st->header_len;
Ben Hutchingsecbd95c2008-09-01 12:46:40 +0100963 int len = skb_headlen(skb) - hl;
964
Ben Hutchings0e33d872012-05-17 17:46:55 +0100965 st->unmap_addr = dma_map_single(&efx->pci_dev->dev, skb->data + hl,
966 len, DMA_TO_DEVICE);
967 if (likely(!dma_mapping_error(&efx->pci_dev->dev, st->unmap_addr))) {
Ben Hutchings23d9e602008-09-01 12:47:02 +0100968 st->unmap_single = true;
969 st->unmap_len = len;
970 st->in_len = len;
971 st->dma_addr = st->unmap_addr;
Ben Hutchingsb9b39b62008-05-07 12:51:12 +0100972 return 0;
973 }
974 return -ENOMEM;
975}
976
977
978/**
979 * tso_fill_packet_with_fragment - form descriptors for the current fragment
980 * @tx_queue: Efx TX queue
981 * @skb: Socket buffer
982 * @st: TSO state
983 *
984 * Form descriptors for the current fragment, until we reach the end
985 * of fragment or end-of-packet. Return 0 on success, 1 if not enough
986 * space in @tx_queue.
987 */
Ben Hutchings4d566062008-09-01 12:47:12 +0100988static int tso_fill_packet_with_fragment(struct efx_tx_queue *tx_queue,
989 const struct sk_buff *skb,
990 struct tso_state *st)
Ben Hutchingsb9b39b62008-05-07 12:51:12 +0100991{
Ben Hutchingsecbd95c2008-09-01 12:46:40 +0100992 struct efx_tx_buffer *buffer;
Ben Hutchingsb9b39b62008-05-07 12:51:12 +0100993 int n, end_of_packet, rc;
994
Ben Hutchings23d9e602008-09-01 12:47:02 +0100995 if (st->in_len == 0)
Ben Hutchingsb9b39b62008-05-07 12:51:12 +0100996 return 0;
997 if (st->packet_space == 0)
998 return 0;
999
Ben Hutchings23d9e602008-09-01 12:47:02 +01001000 EFX_BUG_ON_PARANOID(st->in_len <= 0);
Ben Hutchingsb9b39b62008-05-07 12:51:12 +01001001 EFX_BUG_ON_PARANOID(st->packet_space <= 0);
1002
Ben Hutchings23d9e602008-09-01 12:47:02 +01001003 n = min(st->in_len, st->packet_space);
Ben Hutchingsb9b39b62008-05-07 12:51:12 +01001004
1005 st->packet_space -= n;
Ben Hutchings23d9e602008-09-01 12:47:02 +01001006 st->out_len -= n;
1007 st->in_len -= n;
Ben Hutchingsb9b39b62008-05-07 12:51:12 +01001008
Ben Hutchings23d9e602008-09-01 12:47:02 +01001009 rc = efx_tx_queue_insert(tx_queue, st->dma_addr, n, &buffer);
Ben Hutchingsecbd95c2008-09-01 12:46:40 +01001010 if (likely(rc == 0)) {
Ben Hutchings23d9e602008-09-01 12:47:02 +01001011 if (st->out_len == 0)
Ben Hutchingsecbd95c2008-09-01 12:46:40 +01001012 /* Transfer ownership of the skb */
1013 buffer->skb = skb;
1014
Ben Hutchings23d9e602008-09-01 12:47:02 +01001015 end_of_packet = st->out_len == 0 || st->packet_space == 0;
Ben Hutchingsecbd95c2008-09-01 12:46:40 +01001016 buffer->continuation = !end_of_packet;
1017
Ben Hutchings23d9e602008-09-01 12:47:02 +01001018 if (st->in_len == 0) {
Ben Hutchings0e33d872012-05-17 17:46:55 +01001019 /* Transfer ownership of the DMA mapping */
Ben Hutchings23d9e602008-09-01 12:47:02 +01001020 buffer->unmap_len = st->unmap_len;
1021 buffer->unmap_single = st->unmap_single;
1022 st->unmap_len = 0;
Ben Hutchingsecbd95c2008-09-01 12:46:40 +01001023 }
1024 }
Ben Hutchingsb9b39b62008-05-07 12:51:12 +01001025
Ben Hutchings23d9e602008-09-01 12:47:02 +01001026 st->dma_addr += n;
Ben Hutchingsb9b39b62008-05-07 12:51:12 +01001027 return rc;
1028}
1029
1030
1031/**
1032 * tso_start_new_packet - generate a new header and prepare for the new packet
1033 * @tx_queue: Efx TX queue
1034 * @skb: Socket buffer
1035 * @st: TSO state
1036 *
1037 * Generate a new header and prepare for the new packet. Return 0 on
1038 * success, or -1 if failed to alloc header.
1039 */
Ben Hutchings4d566062008-09-01 12:47:12 +01001040static int tso_start_new_packet(struct efx_tx_queue *tx_queue,
1041 const struct sk_buff *skb,
1042 struct tso_state *st)
Ben Hutchingsb9b39b62008-05-07 12:51:12 +01001043{
1044 struct efx_tso_header *tsoh;
Ben Hutchingsb9b39b62008-05-07 12:51:12 +01001045 struct tcphdr *tsoh_th;
1046 unsigned ip_length;
1047 u8 *header;
1048
1049 /* Allocate a DMA-mapped header buffer. */
Ben Hutchings23d9e602008-09-01 12:47:02 +01001050 if (likely(TSOH_SIZE(st->header_len) <= TSOH_STD_SIZE)) {
Ben Hutchingsb3475642008-05-16 21:15:49 +01001051 if (tx_queue->tso_headers_free == NULL) {
Ben Hutchingsb9b39b62008-05-07 12:51:12 +01001052 if (efx_tsoh_block_alloc(tx_queue))
1053 return -1;
Ben Hutchingsb3475642008-05-16 21:15:49 +01001054 }
Ben Hutchingsb9b39b62008-05-07 12:51:12 +01001055 EFX_BUG_ON_PARANOID(!tx_queue->tso_headers_free);
1056 tsoh = tx_queue->tso_headers_free;
1057 tx_queue->tso_headers_free = tsoh->next;
1058 tsoh->unmap_len = 0;
1059 } else {
1060 tx_queue->tso_long_headers++;
Ben Hutchings23d9e602008-09-01 12:47:02 +01001061 tsoh = efx_tsoh_heap_alloc(tx_queue, st->header_len);
Ben Hutchingsb9b39b62008-05-07 12:51:12 +01001062 if (unlikely(!tsoh))
1063 return -1;
1064 }
1065
1066 header = TSOH_BUFFER(tsoh);
1067 tsoh_th = (struct tcphdr *)(header + SKB_TCP_OFF(skb));
Ben Hutchingsb9b39b62008-05-07 12:51:12 +01001068
1069 /* Copy and update the headers. */
Ben Hutchings23d9e602008-09-01 12:47:02 +01001070 memcpy(header, skb->data, st->header_len);
Ben Hutchingsb9b39b62008-05-07 12:51:12 +01001071
1072 tsoh_th->seq = htonl(st->seqnum);
1073 st->seqnum += skb_shinfo(skb)->gso_size;
Ben Hutchings23d9e602008-09-01 12:47:02 +01001074 if (st->out_len > skb_shinfo(skb)->gso_size) {
Ben Hutchingsb9b39b62008-05-07 12:51:12 +01001075 /* This packet will not finish the TSO burst. */
Ben Hutchings23d9e602008-09-01 12:47:02 +01001076 ip_length = st->full_packet_size - ETH_HDR_LEN(skb);
Ben Hutchingsb9b39b62008-05-07 12:51:12 +01001077 tsoh_th->fin = 0;
1078 tsoh_th->psh = 0;
1079 } else {
1080 /* This packet will be the last in the TSO burst. */
Ben Hutchings23d9e602008-09-01 12:47:02 +01001081 ip_length = st->header_len - ETH_HDR_LEN(skb) + st->out_len;
Ben Hutchingsb9b39b62008-05-07 12:51:12 +01001082 tsoh_th->fin = tcp_hdr(skb)->fin;
1083 tsoh_th->psh = tcp_hdr(skb)->psh;
1084 }
Ben Hutchingsb9b39b62008-05-07 12:51:12 +01001085
Ben Hutchings738a8f42009-11-29 15:16:05 +00001086 if (st->protocol == htons(ETH_P_IP)) {
1087 struct iphdr *tsoh_iph =
1088 (struct iphdr *)(header + SKB_IPV4_OFF(skb));
1089
1090 tsoh_iph->tot_len = htons(ip_length);
1091
1092 /* Linux leaves suitable gaps in the IP ID space for us to fill. */
1093 tsoh_iph->id = htons(st->ipv4_id);
1094 st->ipv4_id++;
1095 } else {
1096 struct ipv6hdr *tsoh_iph =
1097 (struct ipv6hdr *)(header + SKB_IPV6_OFF(skb));
1098
1099 tsoh_iph->payload_len = htons(ip_length - sizeof(*tsoh_iph));
1100 }
Ben Hutchingsb9b39b62008-05-07 12:51:12 +01001101
1102 st->packet_space = skb_shinfo(skb)->gso_size;
1103 ++tx_queue->tso_packets;
1104
1105 /* Form a descriptor for this header. */
Ben Hutchings23d9e602008-09-01 12:47:02 +01001106 efx_tso_put_header(tx_queue, tsoh, st->header_len);
Ben Hutchingsb9b39b62008-05-07 12:51:12 +01001107
1108 return 0;
1109}
1110
1111
1112/**
1113 * efx_enqueue_skb_tso - segment and transmit a TSO socket buffer
1114 * @tx_queue: Efx TX queue
1115 * @skb: Socket buffer
1116 *
1117 * Context: You must hold netif_tx_lock() to call this function.
1118 *
1119 * Add socket buffer @skb to @tx_queue, doing TSO or return != 0 if
1120 * @skb was not enqueued. In all cases @skb is consumed. Return
1121 * %NETDEV_TX_OK or %NETDEV_TX_BUSY.
1122 */
1123static int efx_enqueue_skb_tso(struct efx_tx_queue *tx_queue,
Ben Hutchings740847d2008-09-01 12:48:23 +01001124 struct sk_buff *skb)
Ben Hutchingsb9b39b62008-05-07 12:51:12 +01001125{
Ben Hutchingsecbd95c2008-09-01 12:46:40 +01001126 struct efx_nic *efx = tx_queue->efx;
Ben Hutchingsb9b39b62008-05-07 12:51:12 +01001127 int frag_i, rc, rc2 = NETDEV_TX_OK;
1128 struct tso_state state;
Ben Hutchingsb9b39b62008-05-07 12:51:12 +01001129
Ben Hutchings738a8f42009-11-29 15:16:05 +00001130 /* Find the packet protocol and sanity-check it */
1131 state.protocol = efx_tso_check_protocol(skb);
Ben Hutchingsb9b39b62008-05-07 12:51:12 +01001132
1133 EFX_BUG_ON_PARANOID(tx_queue->write_count != tx_queue->insert_count);
1134
1135 tso_start(&state, skb);
1136
1137 /* Assume that skb header area contains exactly the headers, and
1138 * all payload is in the frag list.
1139 */
Ben Hutchings23d9e602008-09-01 12:47:02 +01001140 if (skb_headlen(skb) == state.header_len) {
Ben Hutchingsb9b39b62008-05-07 12:51:12 +01001141 /* Grab the first payload fragment. */
1142 EFX_BUG_ON_PARANOID(skb_shinfo(skb)->nr_frags < 1);
1143 frag_i = 0;
Ben Hutchingsecbd95c2008-09-01 12:46:40 +01001144 rc = tso_get_fragment(&state, efx,
1145 skb_shinfo(skb)->frags + frag_i);
Ben Hutchingsb9b39b62008-05-07 12:51:12 +01001146 if (rc)
1147 goto mem_err;
1148 } else {
Ben Hutchingsecbd95c2008-09-01 12:46:40 +01001149 rc = tso_get_head_fragment(&state, efx, skb);
Ben Hutchingsb9b39b62008-05-07 12:51:12 +01001150 if (rc)
1151 goto mem_err;
1152 frag_i = -1;
1153 }
1154
1155 if (tso_start_new_packet(tx_queue, skb, &state) < 0)
1156 goto mem_err;
1157
1158 while (1) {
1159 rc = tso_fill_packet_with_fragment(tx_queue, skb, &state);
Ben Hutchingsc04bfc62010-12-10 01:24:16 +00001160 if (unlikely(rc)) {
1161 rc2 = NETDEV_TX_BUSY;
1162 goto unwind;
1163 }
Ben Hutchingsb9b39b62008-05-07 12:51:12 +01001164
1165 /* Move onto the next fragment? */
Ben Hutchings23d9e602008-09-01 12:47:02 +01001166 if (state.in_len == 0) {
Ben Hutchingsb9b39b62008-05-07 12:51:12 +01001167 if (++frag_i >= skb_shinfo(skb)->nr_frags)
1168 /* End of payload reached. */
1169 break;
Ben Hutchingsecbd95c2008-09-01 12:46:40 +01001170 rc = tso_get_fragment(&state, efx,
1171 skb_shinfo(skb)->frags + frag_i);
Ben Hutchingsb9b39b62008-05-07 12:51:12 +01001172 if (rc)
1173 goto mem_err;
1174 }
1175
1176 /* Start at new packet? */
1177 if (state.packet_space == 0 &&
1178 tso_start_new_packet(tx_queue, skb, &state) < 0)
1179 goto mem_err;
1180 }
1181
Eric Dumazet449fa022011-11-30 17:12:27 -05001182 netdev_tx_sent_queue(tx_queue->core_txq, skb->len);
1183
Ben Hutchingsb9b39b62008-05-07 12:51:12 +01001184 /* Pass off to hardware */
Ben Hutchings152b6a62009-11-29 03:43:56 +00001185 efx_nic_push_buffers(tx_queue);
Ben Hutchingsb9b39b62008-05-07 12:51:12 +01001186
1187 tx_queue->tso_bursts++;
1188 return NETDEV_TX_OK;
1189
1190 mem_err:
Ben Hutchings62776d02010-06-23 11:30:07 +00001191 netif_err(efx, tx_err, efx->net_dev,
Ben Hutchings0e33d872012-05-17 17:46:55 +01001192 "Out of memory for TSO headers, or DMA mapping error\n");
Ben Hutchings9bc183d2009-11-23 16:06:47 +00001193 dev_kfree_skb_any(skb);
Ben Hutchingsb9b39b62008-05-07 12:51:12 +01001194
1195 unwind:
Ben Hutchings5988b632008-09-01 12:46:36 +01001196 /* Free the DMA mapping we were in the process of writing out */
Ben Hutchings23d9e602008-09-01 12:47:02 +01001197 if (state.unmap_len) {
1198 if (state.unmap_single)
Ben Hutchings0e33d872012-05-17 17:46:55 +01001199 dma_unmap_single(&efx->pci_dev->dev, state.unmap_addr,
1200 state.unmap_len, DMA_TO_DEVICE);
Ben Hutchingsecbd95c2008-09-01 12:46:40 +01001201 else
Ben Hutchings0e33d872012-05-17 17:46:55 +01001202 dma_unmap_page(&efx->pci_dev->dev, state.unmap_addr,
1203 state.unmap_len, DMA_TO_DEVICE);
Ben Hutchingsecbd95c2008-09-01 12:46:40 +01001204 }
Ben Hutchings5988b632008-09-01 12:46:36 +01001205
Ben Hutchingsb9b39b62008-05-07 12:51:12 +01001206 efx_enqueue_unwind(tx_queue);
1207 return rc2;
1208}
1209
1210
1211/*
1212 * Free up all TSO datastructures associated with tx_queue. This
1213 * routine should be called only once the tx_queue is both empty and
1214 * will no longer be used.
1215 */
1216static void efx_fini_tso(struct efx_tx_queue *tx_queue)
1217{
1218 unsigned i;
1219
Ben Hutchingsb3475642008-05-16 21:15:49 +01001220 if (tx_queue->buffer) {
Steve Hodgsonecc910f2010-09-10 06:42:22 +00001221 for (i = 0; i <= tx_queue->ptr_mask; ++i)
Ben Hutchingsb9b39b62008-05-07 12:51:12 +01001222 efx_tsoh_free(tx_queue, &tx_queue->buffer[i]);
Ben Hutchingsb3475642008-05-16 21:15:49 +01001223 }
Ben Hutchingsb9b39b62008-05-07 12:51:12 +01001224
1225 while (tx_queue->tso_headers_free != NULL)
1226 efx_tsoh_block_free(tx_queue, tx_queue->tso_headers_free,
Ben Hutchings0e33d872012-05-17 17:46:55 +01001227 &tx_queue->efx->pci_dev->dev);
Ben Hutchingsb9b39b62008-05-07 12:51:12 +01001228}