blob: 9b225a7769f765a1f1bf3ef024ae3b73a18a66c2 [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 Hutchings8ceee662008-04-27 12:55:59 +0100122/*
123 * Add a socket buffer to a TX queue
124 *
125 * This maps all fragments of a socket buffer for DMA and adds them to
126 * the TX queue. The queue's insert pointer will be incremented by
127 * the number of fragments in the socket buffer.
128 *
129 * If any DMA mapping fails, any mapped fragments will be unmapped,
130 * the queue's insert pointer will be restored to its original value.
131 *
Ben Hutchings497f5ba2009-11-23 16:07:05 +0000132 * This function is split out from efx_hard_start_xmit to allow the
133 * loopback test to direct packets via specific TX queues.
134 *
Ben Hutchings8ceee662008-04-27 12:55:59 +0100135 * Returns NETDEV_TX_OK or NETDEV_TX_BUSY
136 * You must hold netif_tx_lock() to call this function.
137 */
Ben Hutchings497f5ba2009-11-23 16:07:05 +0000138netdev_tx_t efx_enqueue_skb(struct efx_tx_queue *tx_queue, struct sk_buff *skb)
Ben Hutchings8ceee662008-04-27 12:55:59 +0100139{
140 struct efx_nic *efx = tx_queue->efx;
Ben Hutchings0e33d872012-05-17 17:46:55 +0100141 struct device *dma_dev = &efx->pci_dev->dev;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100142 struct efx_tx_buffer *buffer;
143 skb_frag_t *fragment;
Ben Hutchings63f19882009-10-23 08:31:20 +0000144 unsigned int len, unmap_len = 0, fill_level, insert_ptr;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100145 dma_addr_t dma_addr, unmap_addr = 0;
146 unsigned int dma_len;
Ben Hutchingsdc8cfa52008-09-01 12:46:50 +0100147 bool unmap_single;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100148 int q_space, i = 0;
Stephen Hemminger613573252009-08-31 19:50:58 +0000149 netdev_tx_t rc = NETDEV_TX_OK;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100150
151 EFX_BUG_ON_PARANOID(tx_queue->write_count != tx_queue->insert_count);
152
Ben Hutchings9bc183d2009-11-23 16:06:47 +0000153 if (skb_shinfo(skb)->gso_size)
Ben Hutchingsb9b39b62008-05-07 12:51:12 +0100154 return efx_enqueue_skb_tso(tx_queue, skb);
155
Ben Hutchings8ceee662008-04-27 12:55:59 +0100156 /* Get size of the initial fragment */
157 len = skb_headlen(skb);
158
Ben Hutchingsbb145a92009-03-20 13:25:39 +0000159 /* Pad if necessary */
160 if (EFX_WORKAROUND_15592(efx) && skb->len <= 32) {
161 EFX_BUG_ON_PARANOID(skb->data_len);
162 len = 32 + 1;
163 if (skb_pad(skb, len - skb->len))
164 return NETDEV_TX_OK;
165 }
166
Ben Hutchings8ceee662008-04-27 12:55:59 +0100167 fill_level = tx_queue->insert_count - tx_queue->old_read_count;
Steve Hodgsonecc910f2010-09-10 06:42:22 +0000168 q_space = efx->txq_entries - 1 - fill_level;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100169
Ben Hutchings0e33d872012-05-17 17:46:55 +0100170 /* Map for DMA. Use dma_map_single rather than dma_map_page
Ben Hutchings8ceee662008-04-27 12:55:59 +0100171 * since this is more efficient on machines with sparse
172 * memory.
173 */
Ben Hutchingsdc8cfa52008-09-01 12:46:50 +0100174 unmap_single = true;
Ben Hutchings0e33d872012-05-17 17:46:55 +0100175 dma_addr = dma_map_single(dma_dev, skb->data, len, PCI_DMA_TODEVICE);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100176
177 /* Process all fragments */
178 while (1) {
Ben Hutchings0e33d872012-05-17 17:46:55 +0100179 if (unlikely(dma_mapping_error(dma_dev, dma_addr)))
180 goto dma_err;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100181
182 /* Store fields for marking in the per-fragment final
183 * descriptor */
184 unmap_len = len;
185 unmap_addr = dma_addr;
186
187 /* Add to TX queue, splitting across DMA boundaries */
188 do {
189 if (unlikely(q_space-- <= 0)) {
190 /* It might be that completions have
191 * happened since the xmit path last
192 * checked. Update the xmit path's
193 * copy of read_count.
194 */
Ben Hutchingsc04bfc62010-12-10 01:24:16 +0000195 netif_tx_stop_queue(tx_queue->core_txq);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100196 /* This memory barrier protects the
Ben Hutchingsc04bfc62010-12-10 01:24:16 +0000197 * change of queue state from the access
Ben Hutchings8ceee662008-04-27 12:55:59 +0100198 * of read_count. */
199 smp_mb();
200 tx_queue->old_read_count =
Ben Hutchings51c56f42010-11-10 18:46:40 +0000201 ACCESS_ONCE(tx_queue->read_count);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100202 fill_level = (tx_queue->insert_count
203 - tx_queue->old_read_count);
Steve Hodgsonecc910f2010-09-10 06:42:22 +0000204 q_space = efx->txq_entries - 1 - fill_level;
Ben Hutchingsc04bfc62010-12-10 01:24:16 +0000205 if (unlikely(q_space-- <= 0)) {
206 rc = NETDEV_TX_BUSY;
207 goto unwind;
208 }
Ben Hutchings8ceee662008-04-27 12:55:59 +0100209 smp_mb();
Ben Hutchingse4abce82011-05-16 18:51:24 +0100210 if (likely(!efx->loopback_selftest))
211 netif_tx_start_queue(
212 tx_queue->core_txq);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100213 }
214
Steve Hodgsonecc910f2010-09-10 06:42:22 +0000215 insert_ptr = tx_queue->insert_count & tx_queue->ptr_mask;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100216 buffer = &tx_queue->buffer[insert_ptr];
Ben Hutchingsb9b39b62008-05-07 12:51:12 +0100217 efx_tsoh_free(tx_queue, buffer);
218 EFX_BUG_ON_PARANOID(buffer->tsoh);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100219 EFX_BUG_ON_PARANOID(buffer->skb);
220 EFX_BUG_ON_PARANOID(buffer->len);
Ben Hutchingsdc8cfa52008-09-01 12:46:50 +0100221 EFX_BUG_ON_PARANOID(!buffer->continuation);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100222 EFX_BUG_ON_PARANOID(buffer->unmap_len);
223
Ben Hutchings63f19882009-10-23 08:31:20 +0000224 dma_len = efx_max_tx_len(efx, dma_addr);
225 if (likely(dma_len >= len))
Ben Hutchings8ceee662008-04-27 12:55:59 +0100226 dma_len = len;
227
Ben Hutchings8ceee662008-04-27 12:55:59 +0100228 /* Fill out per descriptor fields */
229 buffer->len = dma_len;
230 buffer->dma_addr = dma_addr;
231 len -= dma_len;
232 dma_addr += dma_len;
233 ++tx_queue->insert_count;
234 } while (len);
235
236 /* Transfer ownership of the unmapping to the final buffer */
Ben Hutchings8ceee662008-04-27 12:55:59 +0100237 buffer->unmap_single = unmap_single;
238 buffer->unmap_len = unmap_len;
239 unmap_len = 0;
240
241 /* Get address and size of next fragment */
242 if (i >= skb_shinfo(skb)->nr_frags)
243 break;
244 fragment = &skb_shinfo(skb)->frags[i];
Eric Dumazet9e903e02011-10-18 21:00:24 +0000245 len = skb_frag_size(fragment);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100246 i++;
247 /* Map for DMA */
Ben Hutchingsdc8cfa52008-09-01 12:46:50 +0100248 unmap_single = false;
Ben Hutchings0e33d872012-05-17 17:46:55 +0100249 dma_addr = skb_frag_dma_map(dma_dev, fragment, 0, len,
Ian Campbell5d6bcdf2011-10-06 11:10:48 +0100250 DMA_TO_DEVICE);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100251 }
252
253 /* Transfer ownership of the skb to the final buffer */
254 buffer->skb = skb;
Ben Hutchingsdc8cfa52008-09-01 12:46:50 +0100255 buffer->continuation = false;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100256
Tom Herbertc3940992011-11-28 16:33:43 +0000257 netdev_tx_sent_queue(tx_queue->core_txq, skb->len);
258
Ben Hutchings8ceee662008-04-27 12:55:59 +0100259 /* Pass off to hardware */
Ben Hutchings152b6a62009-11-29 03:43:56 +0000260 efx_nic_push_buffers(tx_queue);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100261
262 return NETDEV_TX_OK;
263
Ben Hutchings0e33d872012-05-17 17:46:55 +0100264 dma_err:
Ben Hutchings62776d02010-06-23 11:30:07 +0000265 netif_err(efx, tx_err, efx->net_dev,
266 " TX queue %d could not map skb with %d bytes %d "
267 "fragments for DMA\n", tx_queue->queue, skb->len,
268 skb_shinfo(skb)->nr_frags + 1);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100269
270 /* Mark the packet as transmitted, and free the SKB ourselves */
Ben Hutchings9bc183d2009-11-23 16:06:47 +0000271 dev_kfree_skb_any(skb);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100272
273 unwind:
274 /* Work backwards until we hit the original insert pointer value */
275 while (tx_queue->insert_count != tx_queue->write_count) {
Tom Herbertc3940992011-11-28 16:33:43 +0000276 unsigned int pkts_compl = 0, bytes_compl = 0;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100277 --tx_queue->insert_count;
Steve Hodgsonecc910f2010-09-10 06:42:22 +0000278 insert_ptr = tx_queue->insert_count & tx_queue->ptr_mask;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100279 buffer = &tx_queue->buffer[insert_ptr];
Tom Herbertc3940992011-11-28 16:33:43 +0000280 efx_dequeue_buffer(tx_queue, buffer, &pkts_compl, &bytes_compl);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100281 buffer->len = 0;
282 }
283
284 /* Free the fragment we were mid-way through pushing */
Ben Hutchingsecbd95c2008-09-01 12:46:40 +0100285 if (unmap_len) {
286 if (unmap_single)
Ben Hutchings0e33d872012-05-17 17:46:55 +0100287 dma_unmap_single(dma_dev, unmap_addr, unmap_len,
288 DMA_TO_DEVICE);
Ben Hutchingsecbd95c2008-09-01 12:46:40 +0100289 else
Ben Hutchings0e33d872012-05-17 17:46:55 +0100290 dma_unmap_page(dma_dev, unmap_addr, unmap_len,
291 DMA_TO_DEVICE);
Ben Hutchingsecbd95c2008-09-01 12:46:40 +0100292 }
Ben Hutchings8ceee662008-04-27 12:55:59 +0100293
294 return rc;
295}
296
297/* Remove packets from the TX queue
298 *
299 * This removes packets from the TX queue, up to and including the
300 * specified index.
301 */
Ben Hutchings4d566062008-09-01 12:47:12 +0100302static void efx_dequeue_buffers(struct efx_tx_queue *tx_queue,
Tom Herbertc3940992011-11-28 16:33:43 +0000303 unsigned int index,
304 unsigned int *pkts_compl,
305 unsigned int *bytes_compl)
Ben Hutchings8ceee662008-04-27 12:55:59 +0100306{
307 struct efx_nic *efx = tx_queue->efx;
308 unsigned int stop_index, read_ptr;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100309
Steve Hodgsonecc910f2010-09-10 06:42:22 +0000310 stop_index = (index + 1) & tx_queue->ptr_mask;
311 read_ptr = tx_queue->read_count & tx_queue->ptr_mask;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100312
313 while (read_ptr != stop_index) {
314 struct efx_tx_buffer *buffer = &tx_queue->buffer[read_ptr];
315 if (unlikely(buffer->len == 0)) {
Ben Hutchings62776d02010-06-23 11:30:07 +0000316 netif_err(efx, tx_err, efx->net_dev,
317 "TX queue %d spurious TX completion id %x\n",
318 tx_queue->queue, read_ptr);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100319 efx_schedule_reset(efx, RESET_TYPE_TX_SKIP);
320 return;
321 }
322
Tom Herbertc3940992011-11-28 16:33:43 +0000323 efx_dequeue_buffer(tx_queue, buffer, pkts_compl, bytes_compl);
Ben Hutchingsdc8cfa52008-09-01 12:46:50 +0100324 buffer->continuation = true;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100325 buffer->len = 0;
326
327 ++tx_queue->read_count;
Steve Hodgsonecc910f2010-09-10 06:42:22 +0000328 read_ptr = tx_queue->read_count & tx_queue->ptr_mask;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100329 }
330}
331
Ben Hutchings8ceee662008-04-27 12:55:59 +0100332/* Initiate a packet transmission. We use one channel per CPU
333 * (sharing when we have more CPUs than channels). On Falcon, the TX
334 * completion events will be directed back to the CPU that transmitted
335 * the packet, which should be cache-efficient.
336 *
337 * Context: non-blocking.
338 * Note that returning anything other than NETDEV_TX_OK will cause the
339 * OS to free the skb.
340 */
Stephen Hemminger613573252009-08-31 19:50:58 +0000341netdev_tx_t efx_hard_start_xmit(struct sk_buff *skb,
Ben Hutchings2d0cc562012-02-17 00:10:45 +0000342 struct net_device *net_dev)
Ben Hutchings8ceee662008-04-27 12:55:59 +0100343{
Ben Hutchings767e4682008-09-01 12:43:14 +0100344 struct efx_nic *efx = netdev_priv(net_dev);
Ben Hutchings60ac1062008-09-01 12:44:59 +0100345 struct efx_tx_queue *tx_queue;
Ben Hutchings94b274b2011-01-10 21:18:20 +0000346 unsigned index, type;
Ben Hutchings60ac1062008-09-01 12:44:59 +0100347
Ben Hutchingse4abce82011-05-16 18:51:24 +0100348 EFX_WARN_ON_PARANOID(!netif_device_present(net_dev));
Ben Hutchingsa7ef5932009-03-04 09:52:37 +0000349
Ben Hutchings94b274b2011-01-10 21:18:20 +0000350 index = skb_get_queue_mapping(skb);
351 type = skb->ip_summed == CHECKSUM_PARTIAL ? EFX_TXQ_TYPE_OFFLOAD : 0;
352 if (index >= efx->n_tx_channels) {
353 index -= efx->n_tx_channels;
354 type |= EFX_TXQ_TYPE_HIGHPRI;
355 }
356 tx_queue = efx_get_tx_queue(efx, index, type);
Ben Hutchings60ac1062008-09-01 12:44:59 +0100357
Ben Hutchings497f5ba2009-11-23 16:07:05 +0000358 return efx_enqueue_skb(tx_queue, skb);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100359}
360
Ben Hutchings60031fc2011-01-12 18:39:40 +0000361void efx_init_tx_queue_core_txq(struct efx_tx_queue *tx_queue)
362{
Ben Hutchings94b274b2011-01-10 21:18:20 +0000363 struct efx_nic *efx = tx_queue->efx;
364
Ben Hutchings60031fc2011-01-12 18:39:40 +0000365 /* Must be inverse of queue lookup in efx_hard_start_xmit() */
Ben Hutchings94b274b2011-01-10 21:18:20 +0000366 tx_queue->core_txq =
367 netdev_get_tx_queue(efx->net_dev,
368 tx_queue->queue / EFX_TXQ_TYPES +
369 ((tx_queue->queue & EFX_TXQ_TYPE_HIGHPRI) ?
370 efx->n_tx_channels : 0));
371}
372
373int efx_setup_tc(struct net_device *net_dev, u8 num_tc)
374{
375 struct efx_nic *efx = netdev_priv(net_dev);
376 struct efx_channel *channel;
377 struct efx_tx_queue *tx_queue;
378 unsigned tc;
379 int rc;
380
381 if (efx_nic_rev(efx) < EFX_REV_FALCON_B0 || num_tc > EFX_MAX_TX_TC)
382 return -EINVAL;
383
384 if (num_tc == net_dev->num_tc)
385 return 0;
386
387 for (tc = 0; tc < num_tc; tc++) {
388 net_dev->tc_to_txq[tc].offset = tc * efx->n_tx_channels;
389 net_dev->tc_to_txq[tc].count = efx->n_tx_channels;
390 }
391
392 if (num_tc > net_dev->num_tc) {
393 /* Initialise high-priority queues as necessary */
394 efx_for_each_channel(channel, efx) {
395 efx_for_each_possible_channel_tx_queue(tx_queue,
396 channel) {
397 if (!(tx_queue->queue & EFX_TXQ_TYPE_HIGHPRI))
398 continue;
399 if (!tx_queue->buffer) {
400 rc = efx_probe_tx_queue(tx_queue);
401 if (rc)
402 return rc;
403 }
404 if (!tx_queue->initialised)
405 efx_init_tx_queue(tx_queue);
406 efx_init_tx_queue_core_txq(tx_queue);
407 }
408 }
409 } else {
410 /* Reduce number of classes before number of queues */
411 net_dev->num_tc = num_tc;
412 }
413
414 rc = netif_set_real_num_tx_queues(net_dev,
415 max_t(int, num_tc, 1) *
416 efx->n_tx_channels);
417 if (rc)
418 return rc;
419
420 /* Do not destroy high-priority queues when they become
421 * unused. We would have to flush them first, and it is
422 * fairly difficult to flush a subset of TX queues. Leave
423 * it to efx_fini_channels().
424 */
425
426 net_dev->num_tc = num_tc;
427 return 0;
Ben Hutchings60031fc2011-01-12 18:39:40 +0000428}
429
Ben Hutchings8ceee662008-04-27 12:55:59 +0100430void efx_xmit_done(struct efx_tx_queue *tx_queue, unsigned int index)
431{
432 unsigned fill_level;
433 struct efx_nic *efx = tx_queue->efx;
Tom Herbertc3940992011-11-28 16:33:43 +0000434 unsigned int pkts_compl = 0, bytes_compl = 0;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100435
Steve Hodgsonecc910f2010-09-10 06:42:22 +0000436 EFX_BUG_ON_PARANOID(index > tx_queue->ptr_mask);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100437
Tom Herbertc3940992011-11-28 16:33:43 +0000438 efx_dequeue_buffers(tx_queue, index, &pkts_compl, &bytes_compl);
439 netdev_tx_completed_queue(tx_queue->core_txq, pkts_compl, bytes_compl);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100440
441 /* See if we need to restart the netif queue. This barrier
Ben Hutchingsc04bfc62010-12-10 01:24:16 +0000442 * separates the update of read_count from the test of the
443 * queue state. */
Ben Hutchings8ceee662008-04-27 12:55:59 +0100444 smp_mb();
Ben Hutchingsc04bfc62010-12-10 01:24:16 +0000445 if (unlikely(netif_tx_queue_stopped(tx_queue->core_txq)) &&
Neil Turton9d1aea62011-04-04 13:46:23 +0100446 likely(efx->port_enabled) &&
Ben Hutchingse4abce82011-05-16 18:51:24 +0100447 likely(netif_device_present(efx->net_dev))) {
Ben Hutchings8ceee662008-04-27 12:55:59 +0100448 fill_level = tx_queue->insert_count - tx_queue->read_count;
Ben Hutchings73ba7b62012-01-09 19:47:08 +0000449 if (fill_level < EFX_TXQ_THRESHOLD(efx))
Ben Hutchingsc04bfc62010-12-10 01:24:16 +0000450 netif_tx_wake_queue(tx_queue->core_txq);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100451 }
Ben Hutchingscd385572010-11-15 23:53:11 +0000452
453 /* Check whether the hardware queue is now empty */
454 if ((int)(tx_queue->read_count - tx_queue->old_write_count) >= 0) {
455 tx_queue->old_write_count = ACCESS_ONCE(tx_queue->write_count);
456 if (tx_queue->read_count == tx_queue->old_write_count) {
457 smp_mb();
458 tx_queue->empty_read_count =
459 tx_queue->read_count | EFX_EMPTY_COUNT_VALID;
460 }
461 }
Ben Hutchings8ceee662008-04-27 12:55:59 +0100462}
463
464int efx_probe_tx_queue(struct efx_tx_queue *tx_queue)
465{
466 struct efx_nic *efx = tx_queue->efx;
Steve Hodgsonecc910f2010-09-10 06:42:22 +0000467 unsigned int entries;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100468 int i, rc;
469
Steve Hodgsonecc910f2010-09-10 06:42:22 +0000470 /* Create the smallest power-of-two aligned ring */
471 entries = max(roundup_pow_of_two(efx->txq_entries), EFX_MIN_DMAQ_SIZE);
472 EFX_BUG_ON_PARANOID(entries > EFX_MAX_DMAQ_SIZE);
473 tx_queue->ptr_mask = entries - 1;
474
475 netif_dbg(efx, probe, efx->net_dev,
476 "creating TX queue %d size %#x mask %#x\n",
477 tx_queue->queue, efx->txq_entries, tx_queue->ptr_mask);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100478
479 /* Allocate software ring */
Thomas Meyerc2e4e252011-12-02 12:36:13 +0000480 tx_queue->buffer = kcalloc(entries, sizeof(*tx_queue->buffer),
Steve Hodgsonecc910f2010-09-10 06:42:22 +0000481 GFP_KERNEL);
Ben Hutchings60ac1062008-09-01 12:44:59 +0100482 if (!tx_queue->buffer)
483 return -ENOMEM;
Steve Hodgsonecc910f2010-09-10 06:42:22 +0000484 for (i = 0; i <= tx_queue->ptr_mask; ++i)
Ben Hutchingsdc8cfa52008-09-01 12:46:50 +0100485 tx_queue->buffer[i].continuation = true;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100486
487 /* Allocate hardware ring */
Ben Hutchings152b6a62009-11-29 03:43:56 +0000488 rc = efx_nic_probe_tx(tx_queue);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100489 if (rc)
Ben Hutchings60ac1062008-09-01 12:44:59 +0100490 goto fail;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100491
492 return 0;
493
Ben Hutchings60ac1062008-09-01 12:44:59 +0100494 fail:
Ben Hutchings8ceee662008-04-27 12:55:59 +0100495 kfree(tx_queue->buffer);
496 tx_queue->buffer = NULL;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100497 return rc;
498}
499
Ben Hutchingsbc3c90a2008-09-01 12:48:46 +0100500void efx_init_tx_queue(struct efx_tx_queue *tx_queue)
Ben Hutchings8ceee662008-04-27 12:55:59 +0100501{
Ben Hutchings62776d02010-06-23 11:30:07 +0000502 netif_dbg(tx_queue->efx, drv, tx_queue->efx->net_dev,
503 "initialising TX queue %d\n", tx_queue->queue);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100504
505 tx_queue->insert_count = 0;
506 tx_queue->write_count = 0;
Ben Hutchingscd385572010-11-15 23:53:11 +0000507 tx_queue->old_write_count = 0;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100508 tx_queue->read_count = 0;
509 tx_queue->old_read_count = 0;
Ben Hutchingscd385572010-11-15 23:53:11 +0000510 tx_queue->empty_read_count = 0 | EFX_EMPTY_COUNT_VALID;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100511
512 /* Set up TX descriptor ring */
Ben Hutchings152b6a62009-11-29 03:43:56 +0000513 efx_nic_init_tx(tx_queue);
Ben Hutchings94b274b2011-01-10 21:18:20 +0000514
515 tx_queue->initialised = true;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100516}
517
518void efx_release_tx_buffers(struct efx_tx_queue *tx_queue)
519{
520 struct efx_tx_buffer *buffer;
521
522 if (!tx_queue->buffer)
523 return;
524
525 /* Free any buffers left in the ring */
526 while (tx_queue->read_count != tx_queue->write_count) {
Tom Herbertc3940992011-11-28 16:33:43 +0000527 unsigned int pkts_compl = 0, bytes_compl = 0;
Steve Hodgsonecc910f2010-09-10 06:42:22 +0000528 buffer = &tx_queue->buffer[tx_queue->read_count & tx_queue->ptr_mask];
Tom Herbertc3940992011-11-28 16:33:43 +0000529 efx_dequeue_buffer(tx_queue, buffer, &pkts_compl, &bytes_compl);
Ben Hutchingsdc8cfa52008-09-01 12:46:50 +0100530 buffer->continuation = true;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100531 buffer->len = 0;
532
533 ++tx_queue->read_count;
534 }
Tom Herbertc3940992011-11-28 16:33:43 +0000535 netdev_tx_reset_queue(tx_queue->core_txq);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100536}
537
538void efx_fini_tx_queue(struct efx_tx_queue *tx_queue)
539{
Ben Hutchings94b274b2011-01-10 21:18:20 +0000540 if (!tx_queue->initialised)
541 return;
542
Ben Hutchings62776d02010-06-23 11:30:07 +0000543 netif_dbg(tx_queue->efx, drv, tx_queue->efx->net_dev,
544 "shutting down TX queue %d\n", tx_queue->queue);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100545
Ben Hutchings94b274b2011-01-10 21:18:20 +0000546 tx_queue->initialised = false;
547
Ben Hutchings8ceee662008-04-27 12:55:59 +0100548 /* Flush TX queue, remove descriptor ring */
Ben Hutchings152b6a62009-11-29 03:43:56 +0000549 efx_nic_fini_tx(tx_queue);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100550
551 efx_release_tx_buffers(tx_queue);
552
Ben Hutchingsb9b39b62008-05-07 12:51:12 +0100553 /* Free up TSO header cache */
554 efx_fini_tso(tx_queue);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100555}
556
557void efx_remove_tx_queue(struct efx_tx_queue *tx_queue)
558{
Ben Hutchings94b274b2011-01-10 21:18:20 +0000559 if (!tx_queue->buffer)
560 return;
561
Ben Hutchings62776d02010-06-23 11:30:07 +0000562 netif_dbg(tx_queue->efx, drv, tx_queue->efx->net_dev,
563 "destroying TX queue %d\n", tx_queue->queue);
Ben Hutchings152b6a62009-11-29 03:43:56 +0000564 efx_nic_remove_tx(tx_queue);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100565
566 kfree(tx_queue->buffer);
567 tx_queue->buffer = NULL;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100568}
569
570
Ben Hutchingsb9b39b62008-05-07 12:51:12 +0100571/* Efx TCP segmentation acceleration.
572 *
573 * Why? Because by doing it here in the driver we can go significantly
574 * faster than the GSO.
575 *
576 * Requires TX checksum offload support.
577 */
578
579/* Number of bytes inserted at the start of a TSO header buffer,
580 * similar to NET_IP_ALIGN.
581 */
Ben Hutchings13e9ab12008-09-01 12:50:28 +0100582#ifdef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
Ben Hutchingsb9b39b62008-05-07 12:51:12 +0100583#define TSOH_OFFSET 0
584#else
585#define TSOH_OFFSET NET_IP_ALIGN
586#endif
587
588#define TSOH_BUFFER(tsoh) ((u8 *)(tsoh + 1) + TSOH_OFFSET)
589
590/* Total size of struct efx_tso_header, buffer and padding */
591#define TSOH_SIZE(hdr_len) \
592 (sizeof(struct efx_tso_header) + TSOH_OFFSET + hdr_len)
593
594/* Size of blocks on free list. Larger blocks must be allocated from
595 * the heap.
596 */
597#define TSOH_STD_SIZE 128
598
599#define PTR_DIFF(p1, p2) ((u8 *)(p1) - (u8 *)(p2))
600#define ETH_HDR_LEN(skb) (skb_network_header(skb) - (skb)->data)
601#define SKB_TCP_OFF(skb) PTR_DIFF(tcp_hdr(skb), (skb)->data)
602#define SKB_IPV4_OFF(skb) PTR_DIFF(ip_hdr(skb), (skb)->data)
Ben Hutchings738a8f42009-11-29 15:16:05 +0000603#define SKB_IPV6_OFF(skb) PTR_DIFF(ipv6_hdr(skb), (skb)->data)
Ben Hutchingsb9b39b62008-05-07 12:51:12 +0100604
605/**
606 * struct tso_state - TSO state for an SKB
Ben Hutchings23d9e602008-09-01 12:47:02 +0100607 * @out_len: Remaining length in current segment
Ben Hutchingsb9b39b62008-05-07 12:51:12 +0100608 * @seqnum: Current sequence number
Ben Hutchings23d9e602008-09-01 12:47:02 +0100609 * @ipv4_id: Current IPv4 ID, host endian
Ben Hutchingsb9b39b62008-05-07 12:51:12 +0100610 * @packet_space: Remaining space in current packet
Ben Hutchings23d9e602008-09-01 12:47:02 +0100611 * @dma_addr: DMA address of current position
612 * @in_len: Remaining length in current SKB fragment
613 * @unmap_len: Length of SKB fragment
614 * @unmap_addr: DMA address of SKB fragment
615 * @unmap_single: DMA single vs page mapping flag
Ben Hutchings738a8f42009-11-29 15:16:05 +0000616 * @protocol: Network protocol (after any VLAN header)
Ben Hutchings23d9e602008-09-01 12:47:02 +0100617 * @header_len: Number of bytes of header
618 * @full_packet_size: Number of bytes to put in each outgoing segment
Ben Hutchingsb9b39b62008-05-07 12:51:12 +0100619 *
620 * The state used during segmentation. It is put into this data structure
621 * just to make it easy to pass into inline functions.
622 */
623struct tso_state {
Ben Hutchings23d9e602008-09-01 12:47:02 +0100624 /* Output position */
625 unsigned out_len;
Ben Hutchingsb9b39b62008-05-07 12:51:12 +0100626 unsigned seqnum;
Ben Hutchings23d9e602008-09-01 12:47:02 +0100627 unsigned ipv4_id;
Ben Hutchingsb9b39b62008-05-07 12:51:12 +0100628 unsigned packet_space;
629
Ben Hutchings23d9e602008-09-01 12:47:02 +0100630 /* Input position */
631 dma_addr_t dma_addr;
632 unsigned in_len;
633 unsigned unmap_len;
634 dma_addr_t unmap_addr;
635 bool unmap_single;
Ben Hutchingsb9b39b62008-05-07 12:51:12 +0100636
Ben Hutchings738a8f42009-11-29 15:16:05 +0000637 __be16 protocol;
Ben Hutchings23d9e602008-09-01 12:47:02 +0100638 unsigned header_len;
639 int full_packet_size;
Ben Hutchingsb9b39b62008-05-07 12:51:12 +0100640};
641
642
643/*
644 * Verify that our various assumptions about sk_buffs and the conditions
Ben Hutchings738a8f42009-11-29 15:16:05 +0000645 * under which TSO will be attempted hold true. Return the protocol number.
Ben Hutchingsb9b39b62008-05-07 12:51:12 +0100646 */
Ben Hutchings738a8f42009-11-29 15:16:05 +0000647static __be16 efx_tso_check_protocol(struct sk_buff *skb)
Ben Hutchingsb9b39b62008-05-07 12:51:12 +0100648{
Ben Hutchings740847d2008-09-01 12:48:23 +0100649 __be16 protocol = skb->protocol;
650
Ben Hutchingsb9b39b62008-05-07 12:51:12 +0100651 EFX_BUG_ON_PARANOID(((struct ethhdr *)skb->data)->h_proto !=
Ben Hutchings740847d2008-09-01 12:48:23 +0100652 protocol);
653 if (protocol == htons(ETH_P_8021Q)) {
Ben Hutchings740847d2008-09-01 12:48:23 +0100654 struct vlan_ethhdr *veh = (struct vlan_ethhdr *)skb->data;
655 protocol = veh->h_vlan_encapsulated_proto;
Ben Hutchings740847d2008-09-01 12:48:23 +0100656 }
657
Ben Hutchings738a8f42009-11-29 15:16:05 +0000658 if (protocol == htons(ETH_P_IP)) {
659 EFX_BUG_ON_PARANOID(ip_hdr(skb)->protocol != IPPROTO_TCP);
660 } else {
661 EFX_BUG_ON_PARANOID(protocol != htons(ETH_P_IPV6));
662 EFX_BUG_ON_PARANOID(ipv6_hdr(skb)->nexthdr != NEXTHDR_TCP);
663 }
Ben Hutchingsb9b39b62008-05-07 12:51:12 +0100664 EFX_BUG_ON_PARANOID((PTR_DIFF(tcp_hdr(skb), skb->data)
665 + (tcp_hdr(skb)->doff << 2u)) >
666 skb_headlen(skb));
Ben Hutchings738a8f42009-11-29 15:16:05 +0000667
668 return protocol;
Ben Hutchingsb9b39b62008-05-07 12:51:12 +0100669}
670
671
672/*
673 * Allocate a page worth of efx_tso_header structures, and string them
674 * into the tx_queue->tso_headers_free linked list. Return 0 or -ENOMEM.
675 */
676static int efx_tsoh_block_alloc(struct efx_tx_queue *tx_queue)
677{
Ben Hutchings0e33d872012-05-17 17:46:55 +0100678 struct device *dma_dev = &tx_queue->efx->pci_dev->dev;
Ben Hutchingsb9b39b62008-05-07 12:51:12 +0100679 struct efx_tso_header *tsoh;
680 dma_addr_t dma_addr;
681 u8 *base_kva, *kva;
682
Ben Hutchings0e33d872012-05-17 17:46:55 +0100683 base_kva = dma_alloc_coherent(dma_dev, PAGE_SIZE, &dma_addr, GFP_ATOMIC);
Ben Hutchingsb9b39b62008-05-07 12:51:12 +0100684 if (base_kva == NULL) {
Ben Hutchings62776d02010-06-23 11:30:07 +0000685 netif_err(tx_queue->efx, tx_err, tx_queue->efx->net_dev,
686 "Unable to allocate page for TSO headers\n");
Ben Hutchingsb9b39b62008-05-07 12:51:12 +0100687 return -ENOMEM;
688 }
689
Ben Hutchings0e33d872012-05-17 17:46:55 +0100690 /* dma_alloc_coherent() allocates pages. */
Ben Hutchingsb9b39b62008-05-07 12:51:12 +0100691 EFX_BUG_ON_PARANOID(dma_addr & (PAGE_SIZE - 1u));
692
693 for (kva = base_kva; kva < base_kva + PAGE_SIZE; kva += TSOH_STD_SIZE) {
694 tsoh = (struct efx_tso_header *)kva;
695 tsoh->dma_addr = dma_addr + (TSOH_BUFFER(tsoh) - base_kva);
696 tsoh->next = tx_queue->tso_headers_free;
697 tx_queue->tso_headers_free = tsoh;
698 }
699
700 return 0;
701}
702
703
704/* Free up a TSO header, and all others in the same page. */
705static void efx_tsoh_block_free(struct efx_tx_queue *tx_queue,
706 struct efx_tso_header *tsoh,
Ben Hutchings0e33d872012-05-17 17:46:55 +0100707 struct device *dma_dev)
Ben Hutchingsb9b39b62008-05-07 12:51:12 +0100708{
709 struct efx_tso_header **p;
710 unsigned long base_kva;
711 dma_addr_t base_dma;
712
713 base_kva = (unsigned long)tsoh & PAGE_MASK;
714 base_dma = tsoh->dma_addr & PAGE_MASK;
715
716 p = &tx_queue->tso_headers_free;
Ben Hutchingsb3475642008-05-16 21:15:49 +0100717 while (*p != NULL) {
Ben Hutchingsb9b39b62008-05-07 12:51:12 +0100718 if (((unsigned long)*p & PAGE_MASK) == base_kva)
719 *p = (*p)->next;
720 else
721 p = &(*p)->next;
Ben Hutchingsb3475642008-05-16 21:15:49 +0100722 }
Ben Hutchingsb9b39b62008-05-07 12:51:12 +0100723
Ben Hutchings0e33d872012-05-17 17:46:55 +0100724 dma_free_coherent(dma_dev, PAGE_SIZE, (void *)base_kva, base_dma);
Ben Hutchingsb9b39b62008-05-07 12:51:12 +0100725}
726
727static struct efx_tso_header *
728efx_tsoh_heap_alloc(struct efx_tx_queue *tx_queue, size_t header_len)
729{
730 struct efx_tso_header *tsoh;
731
732 tsoh = kmalloc(TSOH_SIZE(header_len), GFP_ATOMIC | GFP_DMA);
733 if (unlikely(!tsoh))
734 return NULL;
735
Ben Hutchings0e33d872012-05-17 17:46:55 +0100736 tsoh->dma_addr = dma_map_single(&tx_queue->efx->pci_dev->dev,
Ben Hutchingsb9b39b62008-05-07 12:51:12 +0100737 TSOH_BUFFER(tsoh), header_len,
Ben Hutchings0e33d872012-05-17 17:46:55 +0100738 DMA_TO_DEVICE);
739 if (unlikely(dma_mapping_error(&tx_queue->efx->pci_dev->dev,
740 tsoh->dma_addr))) {
Ben Hutchingsb9b39b62008-05-07 12:51:12 +0100741 kfree(tsoh);
742 return NULL;
743 }
744
745 tsoh->unmap_len = header_len;
746 return tsoh;
747}
748
749static void
750efx_tsoh_heap_free(struct efx_tx_queue *tx_queue, struct efx_tso_header *tsoh)
751{
Ben Hutchings0e33d872012-05-17 17:46:55 +0100752 dma_unmap_single(&tx_queue->efx->pci_dev->dev,
Ben Hutchingsb9b39b62008-05-07 12:51:12 +0100753 tsoh->dma_addr, tsoh->unmap_len,
Ben Hutchings0e33d872012-05-17 17:46:55 +0100754 DMA_TO_DEVICE);
Ben Hutchingsb9b39b62008-05-07 12:51:12 +0100755 kfree(tsoh);
756}
757
758/**
759 * efx_tx_queue_insert - push descriptors onto the TX queue
760 * @tx_queue: Efx TX queue
761 * @dma_addr: DMA address of fragment
762 * @len: Length of fragment
Ben Hutchingsecbd95c2008-09-01 12:46:40 +0100763 * @final_buffer: The final buffer inserted into the queue
Ben Hutchingsb9b39b62008-05-07 12:51:12 +0100764 *
765 * Push descriptors onto the TX queue. Return 0 on success or 1 if
766 * @tx_queue full.
767 */
768static int efx_tx_queue_insert(struct efx_tx_queue *tx_queue,
769 dma_addr_t dma_addr, unsigned len,
Ben Hutchingsecbd95c2008-09-01 12:46:40 +0100770 struct efx_tx_buffer **final_buffer)
Ben Hutchingsb9b39b62008-05-07 12:51:12 +0100771{
772 struct efx_tx_buffer *buffer;
773 struct efx_nic *efx = tx_queue->efx;
Ben Hutchings63f19882009-10-23 08:31:20 +0000774 unsigned dma_len, fill_level, insert_ptr;
Ben Hutchingsb9b39b62008-05-07 12:51:12 +0100775 int q_space;
776
777 EFX_BUG_ON_PARANOID(len <= 0);
778
779 fill_level = tx_queue->insert_count - tx_queue->old_read_count;
780 /* -1 as there is no way to represent all descriptors used */
Steve Hodgsonecc910f2010-09-10 06:42:22 +0000781 q_space = efx->txq_entries - 1 - fill_level;
Ben Hutchingsb9b39b62008-05-07 12:51:12 +0100782
783 while (1) {
784 if (unlikely(q_space-- <= 0)) {
785 /* It might be that completions have happened
786 * since the xmit path last checked. Update
787 * the xmit path's copy of read_count.
788 */
Ben Hutchingsc04bfc62010-12-10 01:24:16 +0000789 netif_tx_stop_queue(tx_queue->core_txq);
Ben Hutchingsb9b39b62008-05-07 12:51:12 +0100790 /* This memory barrier protects the change of
Ben Hutchingsc04bfc62010-12-10 01:24:16 +0000791 * queue state from the access of read_count. */
Ben Hutchingsb9b39b62008-05-07 12:51:12 +0100792 smp_mb();
793 tx_queue->old_read_count =
Ben Hutchings51c56f42010-11-10 18:46:40 +0000794 ACCESS_ONCE(tx_queue->read_count);
Ben Hutchingsb9b39b62008-05-07 12:51:12 +0100795 fill_level = (tx_queue->insert_count
796 - tx_queue->old_read_count);
Steve Hodgsonecc910f2010-09-10 06:42:22 +0000797 q_space = efx->txq_entries - 1 - fill_level;
Ben Hutchingsecbd95c2008-09-01 12:46:40 +0100798 if (unlikely(q_space-- <= 0)) {
799 *final_buffer = NULL;
Ben Hutchingsb9b39b62008-05-07 12:51:12 +0100800 return 1;
Ben Hutchingsecbd95c2008-09-01 12:46:40 +0100801 }
Ben Hutchingsb9b39b62008-05-07 12:51:12 +0100802 smp_mb();
Ben Hutchingsc04bfc62010-12-10 01:24:16 +0000803 netif_tx_start_queue(tx_queue->core_txq);
Ben Hutchingsb9b39b62008-05-07 12:51:12 +0100804 }
805
Steve Hodgsonecc910f2010-09-10 06:42:22 +0000806 insert_ptr = tx_queue->insert_count & tx_queue->ptr_mask;
Ben Hutchingsb9b39b62008-05-07 12:51:12 +0100807 buffer = &tx_queue->buffer[insert_ptr];
808 ++tx_queue->insert_count;
809
810 EFX_BUG_ON_PARANOID(tx_queue->insert_count -
Steve Hodgsonecc910f2010-09-10 06:42:22 +0000811 tx_queue->read_count >=
812 efx->txq_entries);
Ben Hutchingsb9b39b62008-05-07 12:51:12 +0100813
814 efx_tsoh_free(tx_queue, buffer);
815 EFX_BUG_ON_PARANOID(buffer->len);
816 EFX_BUG_ON_PARANOID(buffer->unmap_len);
817 EFX_BUG_ON_PARANOID(buffer->skb);
Ben Hutchingsdc8cfa52008-09-01 12:46:50 +0100818 EFX_BUG_ON_PARANOID(!buffer->continuation);
Ben Hutchingsb9b39b62008-05-07 12:51:12 +0100819 EFX_BUG_ON_PARANOID(buffer->tsoh);
820
821 buffer->dma_addr = dma_addr;
822
Ben Hutchings63f19882009-10-23 08:31:20 +0000823 dma_len = efx_max_tx_len(efx, dma_addr);
Ben Hutchingsb9b39b62008-05-07 12:51:12 +0100824
825 /* If there is enough space to send then do so */
826 if (dma_len >= len)
827 break;
828
829 buffer->len = dma_len; /* Don't set the other members */
830 dma_addr += dma_len;
831 len -= dma_len;
832 }
833
834 EFX_BUG_ON_PARANOID(!len);
835 buffer->len = len;
Ben Hutchingsecbd95c2008-09-01 12:46:40 +0100836 *final_buffer = buffer;
Ben Hutchingsb9b39b62008-05-07 12:51:12 +0100837 return 0;
838}
839
840
841/*
842 * Put a TSO header into the TX queue.
843 *
844 * This is special-cased because we know that it is small enough to fit in
845 * a single fragment, and we know it doesn't cross a page boundary. It
846 * also allows us to not worry about end-of-packet etc.
847 */
Ben Hutchings4d566062008-09-01 12:47:12 +0100848static void efx_tso_put_header(struct efx_tx_queue *tx_queue,
849 struct efx_tso_header *tsoh, unsigned len)
Ben Hutchingsb9b39b62008-05-07 12:51:12 +0100850{
851 struct efx_tx_buffer *buffer;
852
Steve Hodgsonecc910f2010-09-10 06:42:22 +0000853 buffer = &tx_queue->buffer[tx_queue->insert_count & tx_queue->ptr_mask];
Ben Hutchingsb9b39b62008-05-07 12:51:12 +0100854 efx_tsoh_free(tx_queue, buffer);
855 EFX_BUG_ON_PARANOID(buffer->len);
856 EFX_BUG_ON_PARANOID(buffer->unmap_len);
857 EFX_BUG_ON_PARANOID(buffer->skb);
Ben Hutchingsdc8cfa52008-09-01 12:46:50 +0100858 EFX_BUG_ON_PARANOID(!buffer->continuation);
Ben Hutchingsb9b39b62008-05-07 12:51:12 +0100859 EFX_BUG_ON_PARANOID(buffer->tsoh);
860 buffer->len = len;
861 buffer->dma_addr = tsoh->dma_addr;
862 buffer->tsoh = tsoh;
863
864 ++tx_queue->insert_count;
865}
866
867
868/* Remove descriptors put into a tx_queue. */
869static void efx_enqueue_unwind(struct efx_tx_queue *tx_queue)
870{
871 struct efx_tx_buffer *buffer;
Ben Hutchingscc12dac2008-09-01 12:46:43 +0100872 dma_addr_t unmap_addr;
Ben Hutchingsb9b39b62008-05-07 12:51:12 +0100873
874 /* Work backwards until we hit the original insert pointer value */
875 while (tx_queue->insert_count != tx_queue->write_count) {
876 --tx_queue->insert_count;
877 buffer = &tx_queue->buffer[tx_queue->insert_count &
Steve Hodgsonecc910f2010-09-10 06:42:22 +0000878 tx_queue->ptr_mask];
Ben Hutchingsb9b39b62008-05-07 12:51:12 +0100879 efx_tsoh_free(tx_queue, buffer);
880 EFX_BUG_ON_PARANOID(buffer->skb);
Ben Hutchingsb9b39b62008-05-07 12:51:12 +0100881 if (buffer->unmap_len) {
Ben Hutchingscc12dac2008-09-01 12:46:43 +0100882 unmap_addr = (buffer->dma_addr + buffer->len -
883 buffer->unmap_len);
Ben Hutchingsecbd95c2008-09-01 12:46:40 +0100884 if (buffer->unmap_single)
Ben Hutchings0e33d872012-05-17 17:46:55 +0100885 dma_unmap_single(&tx_queue->efx->pci_dev->dev,
Ben Hutchingscc12dac2008-09-01 12:46:43 +0100886 unmap_addr, buffer->unmap_len,
Ben Hutchings0e33d872012-05-17 17:46:55 +0100887 DMA_TO_DEVICE);
Ben Hutchingsecbd95c2008-09-01 12:46:40 +0100888 else
Ben Hutchings0e33d872012-05-17 17:46:55 +0100889 dma_unmap_page(&tx_queue->efx->pci_dev->dev,
Ben Hutchingscc12dac2008-09-01 12:46:43 +0100890 unmap_addr, buffer->unmap_len,
Ben Hutchings0e33d872012-05-17 17:46:55 +0100891 DMA_TO_DEVICE);
Ben Hutchingsb9b39b62008-05-07 12:51:12 +0100892 buffer->unmap_len = 0;
893 }
Neil Turtona7ebd272009-12-23 13:47:13 +0000894 buffer->len = 0;
895 buffer->continuation = true;
Ben Hutchingsb9b39b62008-05-07 12:51:12 +0100896 }
897}
898
899
900/* Parse the SKB header and initialise state. */
Ben Hutchings4d566062008-09-01 12:47:12 +0100901static void tso_start(struct tso_state *st, const struct sk_buff *skb)
Ben Hutchingsb9b39b62008-05-07 12:51:12 +0100902{
903 /* All ethernet/IP/TCP headers combined size is TCP header size
904 * plus offset of TCP header relative to start of packet.
905 */
Ben Hutchings23d9e602008-09-01 12:47:02 +0100906 st->header_len = ((tcp_hdr(skb)->doff << 2u)
907 + PTR_DIFF(tcp_hdr(skb), skb->data));
908 st->full_packet_size = st->header_len + skb_shinfo(skb)->gso_size;
Ben Hutchingsb9b39b62008-05-07 12:51:12 +0100909
Ben Hutchings738a8f42009-11-29 15:16:05 +0000910 if (st->protocol == htons(ETH_P_IP))
911 st->ipv4_id = ntohs(ip_hdr(skb)->id);
912 else
913 st->ipv4_id = 0;
Ben Hutchingsb9b39b62008-05-07 12:51:12 +0100914 st->seqnum = ntohl(tcp_hdr(skb)->seq);
915
916 EFX_BUG_ON_PARANOID(tcp_hdr(skb)->urg);
917 EFX_BUG_ON_PARANOID(tcp_hdr(skb)->syn);
918 EFX_BUG_ON_PARANOID(tcp_hdr(skb)->rst);
919
Ben Hutchings23d9e602008-09-01 12:47:02 +0100920 st->out_len = skb->len - st->header_len;
921 st->unmap_len = 0;
922 st->unmap_single = false;
Ben Hutchingsb9b39b62008-05-07 12:51:12 +0100923}
924
Ben Hutchings4d566062008-09-01 12:47:12 +0100925static int tso_get_fragment(struct tso_state *st, struct efx_nic *efx,
926 skb_frag_t *frag)
Ben Hutchingsb9b39b62008-05-07 12:51:12 +0100927{
Ian Campbell4a22c4c2011-09-21 21:53:16 +0000928 st->unmap_addr = skb_frag_dma_map(&efx->pci_dev->dev, frag, 0,
Eric Dumazet9e903e02011-10-18 21:00:24 +0000929 skb_frag_size(frag), DMA_TO_DEVICE);
Ian Campbell5d6bcdf2011-10-06 11:10:48 +0100930 if (likely(!dma_mapping_error(&efx->pci_dev->dev, st->unmap_addr))) {
Ben Hutchings23d9e602008-09-01 12:47:02 +0100931 st->unmap_single = false;
Eric Dumazet9e903e02011-10-18 21:00:24 +0000932 st->unmap_len = skb_frag_size(frag);
933 st->in_len = skb_frag_size(frag);
Ben Hutchings23d9e602008-09-01 12:47:02 +0100934 st->dma_addr = st->unmap_addr;
Ben Hutchingsecbd95c2008-09-01 12:46:40 +0100935 return 0;
936 }
937 return -ENOMEM;
938}
939
Ben Hutchings4d566062008-09-01 12:47:12 +0100940static int tso_get_head_fragment(struct tso_state *st, struct efx_nic *efx,
941 const struct sk_buff *skb)
Ben Hutchingsecbd95c2008-09-01 12:46:40 +0100942{
Ben Hutchings23d9e602008-09-01 12:47:02 +0100943 int hl = st->header_len;
Ben Hutchingsecbd95c2008-09-01 12:46:40 +0100944 int len = skb_headlen(skb) - hl;
945
Ben Hutchings0e33d872012-05-17 17:46:55 +0100946 st->unmap_addr = dma_map_single(&efx->pci_dev->dev, skb->data + hl,
947 len, DMA_TO_DEVICE);
948 if (likely(!dma_mapping_error(&efx->pci_dev->dev, st->unmap_addr))) {
Ben Hutchings23d9e602008-09-01 12:47:02 +0100949 st->unmap_single = true;
950 st->unmap_len = len;
951 st->in_len = len;
952 st->dma_addr = st->unmap_addr;
Ben Hutchingsb9b39b62008-05-07 12:51:12 +0100953 return 0;
954 }
955 return -ENOMEM;
956}
957
958
959/**
960 * tso_fill_packet_with_fragment - form descriptors for the current fragment
961 * @tx_queue: Efx TX queue
962 * @skb: Socket buffer
963 * @st: TSO state
964 *
965 * Form descriptors for the current fragment, until we reach the end
966 * of fragment or end-of-packet. Return 0 on success, 1 if not enough
967 * space in @tx_queue.
968 */
Ben Hutchings4d566062008-09-01 12:47:12 +0100969static int tso_fill_packet_with_fragment(struct efx_tx_queue *tx_queue,
970 const struct sk_buff *skb,
971 struct tso_state *st)
Ben Hutchingsb9b39b62008-05-07 12:51:12 +0100972{
Ben Hutchingsecbd95c2008-09-01 12:46:40 +0100973 struct efx_tx_buffer *buffer;
Ben Hutchingsb9b39b62008-05-07 12:51:12 +0100974 int n, end_of_packet, rc;
975
Ben Hutchings23d9e602008-09-01 12:47:02 +0100976 if (st->in_len == 0)
Ben Hutchingsb9b39b62008-05-07 12:51:12 +0100977 return 0;
978 if (st->packet_space == 0)
979 return 0;
980
Ben Hutchings23d9e602008-09-01 12:47:02 +0100981 EFX_BUG_ON_PARANOID(st->in_len <= 0);
Ben Hutchingsb9b39b62008-05-07 12:51:12 +0100982 EFX_BUG_ON_PARANOID(st->packet_space <= 0);
983
Ben Hutchings23d9e602008-09-01 12:47:02 +0100984 n = min(st->in_len, st->packet_space);
Ben Hutchingsb9b39b62008-05-07 12:51:12 +0100985
986 st->packet_space -= n;
Ben Hutchings23d9e602008-09-01 12:47:02 +0100987 st->out_len -= n;
988 st->in_len -= n;
Ben Hutchingsb9b39b62008-05-07 12:51:12 +0100989
Ben Hutchings23d9e602008-09-01 12:47:02 +0100990 rc = efx_tx_queue_insert(tx_queue, st->dma_addr, n, &buffer);
Ben Hutchingsecbd95c2008-09-01 12:46:40 +0100991 if (likely(rc == 0)) {
Ben Hutchings23d9e602008-09-01 12:47:02 +0100992 if (st->out_len == 0)
Ben Hutchingsecbd95c2008-09-01 12:46:40 +0100993 /* Transfer ownership of the skb */
994 buffer->skb = skb;
995
Ben Hutchings23d9e602008-09-01 12:47:02 +0100996 end_of_packet = st->out_len == 0 || st->packet_space == 0;
Ben Hutchingsecbd95c2008-09-01 12:46:40 +0100997 buffer->continuation = !end_of_packet;
998
Ben Hutchings23d9e602008-09-01 12:47:02 +0100999 if (st->in_len == 0) {
Ben Hutchings0e33d872012-05-17 17:46:55 +01001000 /* Transfer ownership of the DMA mapping */
Ben Hutchings23d9e602008-09-01 12:47:02 +01001001 buffer->unmap_len = st->unmap_len;
1002 buffer->unmap_single = st->unmap_single;
1003 st->unmap_len = 0;
Ben Hutchingsecbd95c2008-09-01 12:46:40 +01001004 }
1005 }
Ben Hutchingsb9b39b62008-05-07 12:51:12 +01001006
Ben Hutchings23d9e602008-09-01 12:47:02 +01001007 st->dma_addr += n;
Ben Hutchingsb9b39b62008-05-07 12:51:12 +01001008 return rc;
1009}
1010
1011
1012/**
1013 * tso_start_new_packet - generate a new header and prepare for the new packet
1014 * @tx_queue: Efx TX queue
1015 * @skb: Socket buffer
1016 * @st: TSO state
1017 *
1018 * Generate a new header and prepare for the new packet. Return 0 on
1019 * success, or -1 if failed to alloc header.
1020 */
Ben Hutchings4d566062008-09-01 12:47:12 +01001021static int tso_start_new_packet(struct efx_tx_queue *tx_queue,
1022 const struct sk_buff *skb,
1023 struct tso_state *st)
Ben Hutchingsb9b39b62008-05-07 12:51:12 +01001024{
1025 struct efx_tso_header *tsoh;
Ben Hutchingsb9b39b62008-05-07 12:51:12 +01001026 struct tcphdr *tsoh_th;
1027 unsigned ip_length;
1028 u8 *header;
1029
1030 /* Allocate a DMA-mapped header buffer. */
Ben Hutchings23d9e602008-09-01 12:47:02 +01001031 if (likely(TSOH_SIZE(st->header_len) <= TSOH_STD_SIZE)) {
Ben Hutchingsb3475642008-05-16 21:15:49 +01001032 if (tx_queue->tso_headers_free == NULL) {
Ben Hutchingsb9b39b62008-05-07 12:51:12 +01001033 if (efx_tsoh_block_alloc(tx_queue))
1034 return -1;
Ben Hutchingsb3475642008-05-16 21:15:49 +01001035 }
Ben Hutchingsb9b39b62008-05-07 12:51:12 +01001036 EFX_BUG_ON_PARANOID(!tx_queue->tso_headers_free);
1037 tsoh = tx_queue->tso_headers_free;
1038 tx_queue->tso_headers_free = tsoh->next;
1039 tsoh->unmap_len = 0;
1040 } else {
1041 tx_queue->tso_long_headers++;
Ben Hutchings23d9e602008-09-01 12:47:02 +01001042 tsoh = efx_tsoh_heap_alloc(tx_queue, st->header_len);
Ben Hutchingsb9b39b62008-05-07 12:51:12 +01001043 if (unlikely(!tsoh))
1044 return -1;
1045 }
1046
1047 header = TSOH_BUFFER(tsoh);
1048 tsoh_th = (struct tcphdr *)(header + SKB_TCP_OFF(skb));
Ben Hutchingsb9b39b62008-05-07 12:51:12 +01001049
1050 /* Copy and update the headers. */
Ben Hutchings23d9e602008-09-01 12:47:02 +01001051 memcpy(header, skb->data, st->header_len);
Ben Hutchingsb9b39b62008-05-07 12:51:12 +01001052
1053 tsoh_th->seq = htonl(st->seqnum);
1054 st->seqnum += skb_shinfo(skb)->gso_size;
Ben Hutchings23d9e602008-09-01 12:47:02 +01001055 if (st->out_len > skb_shinfo(skb)->gso_size) {
Ben Hutchingsb9b39b62008-05-07 12:51:12 +01001056 /* This packet will not finish the TSO burst. */
Ben Hutchings23d9e602008-09-01 12:47:02 +01001057 ip_length = st->full_packet_size - ETH_HDR_LEN(skb);
Ben Hutchingsb9b39b62008-05-07 12:51:12 +01001058 tsoh_th->fin = 0;
1059 tsoh_th->psh = 0;
1060 } else {
1061 /* This packet will be the last in the TSO burst. */
Ben Hutchings23d9e602008-09-01 12:47:02 +01001062 ip_length = st->header_len - ETH_HDR_LEN(skb) + st->out_len;
Ben Hutchingsb9b39b62008-05-07 12:51:12 +01001063 tsoh_th->fin = tcp_hdr(skb)->fin;
1064 tsoh_th->psh = tcp_hdr(skb)->psh;
1065 }
Ben Hutchingsb9b39b62008-05-07 12:51:12 +01001066
Ben Hutchings738a8f42009-11-29 15:16:05 +00001067 if (st->protocol == htons(ETH_P_IP)) {
1068 struct iphdr *tsoh_iph =
1069 (struct iphdr *)(header + SKB_IPV4_OFF(skb));
1070
1071 tsoh_iph->tot_len = htons(ip_length);
1072
1073 /* Linux leaves suitable gaps in the IP ID space for us to fill. */
1074 tsoh_iph->id = htons(st->ipv4_id);
1075 st->ipv4_id++;
1076 } else {
1077 struct ipv6hdr *tsoh_iph =
1078 (struct ipv6hdr *)(header + SKB_IPV6_OFF(skb));
1079
1080 tsoh_iph->payload_len = htons(ip_length - sizeof(*tsoh_iph));
1081 }
Ben Hutchingsb9b39b62008-05-07 12:51:12 +01001082
1083 st->packet_space = skb_shinfo(skb)->gso_size;
1084 ++tx_queue->tso_packets;
1085
1086 /* Form a descriptor for this header. */
Ben Hutchings23d9e602008-09-01 12:47:02 +01001087 efx_tso_put_header(tx_queue, tsoh, st->header_len);
Ben Hutchingsb9b39b62008-05-07 12:51:12 +01001088
1089 return 0;
1090}
1091
1092
1093/**
1094 * efx_enqueue_skb_tso - segment and transmit a TSO socket buffer
1095 * @tx_queue: Efx TX queue
1096 * @skb: Socket buffer
1097 *
1098 * Context: You must hold netif_tx_lock() to call this function.
1099 *
1100 * Add socket buffer @skb to @tx_queue, doing TSO or return != 0 if
1101 * @skb was not enqueued. In all cases @skb is consumed. Return
1102 * %NETDEV_TX_OK or %NETDEV_TX_BUSY.
1103 */
1104static int efx_enqueue_skb_tso(struct efx_tx_queue *tx_queue,
Ben Hutchings740847d2008-09-01 12:48:23 +01001105 struct sk_buff *skb)
Ben Hutchingsb9b39b62008-05-07 12:51:12 +01001106{
Ben Hutchingsecbd95c2008-09-01 12:46:40 +01001107 struct efx_nic *efx = tx_queue->efx;
Ben Hutchingsb9b39b62008-05-07 12:51:12 +01001108 int frag_i, rc, rc2 = NETDEV_TX_OK;
1109 struct tso_state state;
Ben Hutchingsb9b39b62008-05-07 12:51:12 +01001110
Ben Hutchings738a8f42009-11-29 15:16:05 +00001111 /* Find the packet protocol and sanity-check it */
1112 state.protocol = efx_tso_check_protocol(skb);
Ben Hutchingsb9b39b62008-05-07 12:51:12 +01001113
1114 EFX_BUG_ON_PARANOID(tx_queue->write_count != tx_queue->insert_count);
1115
1116 tso_start(&state, skb);
1117
1118 /* Assume that skb header area contains exactly the headers, and
1119 * all payload is in the frag list.
1120 */
Ben Hutchings23d9e602008-09-01 12:47:02 +01001121 if (skb_headlen(skb) == state.header_len) {
Ben Hutchingsb9b39b62008-05-07 12:51:12 +01001122 /* Grab the first payload fragment. */
1123 EFX_BUG_ON_PARANOID(skb_shinfo(skb)->nr_frags < 1);
1124 frag_i = 0;
Ben Hutchingsecbd95c2008-09-01 12:46:40 +01001125 rc = tso_get_fragment(&state, efx,
1126 skb_shinfo(skb)->frags + frag_i);
Ben Hutchingsb9b39b62008-05-07 12:51:12 +01001127 if (rc)
1128 goto mem_err;
1129 } else {
Ben Hutchingsecbd95c2008-09-01 12:46:40 +01001130 rc = tso_get_head_fragment(&state, efx, skb);
Ben Hutchingsb9b39b62008-05-07 12:51:12 +01001131 if (rc)
1132 goto mem_err;
1133 frag_i = -1;
1134 }
1135
1136 if (tso_start_new_packet(tx_queue, skb, &state) < 0)
1137 goto mem_err;
1138
1139 while (1) {
1140 rc = tso_fill_packet_with_fragment(tx_queue, skb, &state);
Ben Hutchingsc04bfc62010-12-10 01:24:16 +00001141 if (unlikely(rc)) {
1142 rc2 = NETDEV_TX_BUSY;
1143 goto unwind;
1144 }
Ben Hutchingsb9b39b62008-05-07 12:51:12 +01001145
1146 /* Move onto the next fragment? */
Ben Hutchings23d9e602008-09-01 12:47:02 +01001147 if (state.in_len == 0) {
Ben Hutchingsb9b39b62008-05-07 12:51:12 +01001148 if (++frag_i >= skb_shinfo(skb)->nr_frags)
1149 /* End of payload reached. */
1150 break;
Ben Hutchingsecbd95c2008-09-01 12:46:40 +01001151 rc = tso_get_fragment(&state, efx,
1152 skb_shinfo(skb)->frags + frag_i);
Ben Hutchingsb9b39b62008-05-07 12:51:12 +01001153 if (rc)
1154 goto mem_err;
1155 }
1156
1157 /* Start at new packet? */
1158 if (state.packet_space == 0 &&
1159 tso_start_new_packet(tx_queue, skb, &state) < 0)
1160 goto mem_err;
1161 }
1162
Eric Dumazet449fa022011-11-30 17:12:27 -05001163 netdev_tx_sent_queue(tx_queue->core_txq, skb->len);
1164
Ben Hutchingsb9b39b62008-05-07 12:51:12 +01001165 /* Pass off to hardware */
Ben Hutchings152b6a62009-11-29 03:43:56 +00001166 efx_nic_push_buffers(tx_queue);
Ben Hutchingsb9b39b62008-05-07 12:51:12 +01001167
1168 tx_queue->tso_bursts++;
1169 return NETDEV_TX_OK;
1170
1171 mem_err:
Ben Hutchings62776d02010-06-23 11:30:07 +00001172 netif_err(efx, tx_err, efx->net_dev,
Ben Hutchings0e33d872012-05-17 17:46:55 +01001173 "Out of memory for TSO headers, or DMA mapping error\n");
Ben Hutchings9bc183d2009-11-23 16:06:47 +00001174 dev_kfree_skb_any(skb);
Ben Hutchingsb9b39b62008-05-07 12:51:12 +01001175
1176 unwind:
Ben Hutchings5988b632008-09-01 12:46:36 +01001177 /* Free the DMA mapping we were in the process of writing out */
Ben Hutchings23d9e602008-09-01 12:47:02 +01001178 if (state.unmap_len) {
1179 if (state.unmap_single)
Ben Hutchings0e33d872012-05-17 17:46:55 +01001180 dma_unmap_single(&efx->pci_dev->dev, state.unmap_addr,
1181 state.unmap_len, DMA_TO_DEVICE);
Ben Hutchingsecbd95c2008-09-01 12:46:40 +01001182 else
Ben Hutchings0e33d872012-05-17 17:46:55 +01001183 dma_unmap_page(&efx->pci_dev->dev, state.unmap_addr,
1184 state.unmap_len, DMA_TO_DEVICE);
Ben Hutchingsecbd95c2008-09-01 12:46:40 +01001185 }
Ben Hutchings5988b632008-09-01 12:46:36 +01001186
Ben Hutchingsb9b39b62008-05-07 12:51:12 +01001187 efx_enqueue_unwind(tx_queue);
1188 return rc2;
1189}
1190
1191
1192/*
1193 * Free up all TSO datastructures associated with tx_queue. This
1194 * routine should be called only once the tx_queue is both empty and
1195 * will no longer be used.
1196 */
1197static void efx_fini_tso(struct efx_tx_queue *tx_queue)
1198{
1199 unsigned i;
1200
Ben Hutchingsb3475642008-05-16 21:15:49 +01001201 if (tx_queue->buffer) {
Steve Hodgsonecc910f2010-09-10 06:42:22 +00001202 for (i = 0; i <= tx_queue->ptr_mask; ++i)
Ben Hutchingsb9b39b62008-05-07 12:51:12 +01001203 efx_tsoh_free(tx_queue, &tx_queue->buffer[i]);
Ben Hutchingsb3475642008-05-16 21:15:49 +01001204 }
Ben Hutchingsb9b39b62008-05-07 12:51:12 +01001205
1206 while (tx_queue->tso_headers_free != NULL)
1207 efx_tsoh_block_free(tx_queue, tx_queue->tso_headers_free,
Ben Hutchings0e33d872012-05-17 17:46:55 +01001208 &tx_queue->efx->pci_dev->dev);
Ben Hutchingsb9b39b62008-05-07 12:51:12 +01001209}