blob: 8f1d53e2aca73946317acb9f00bf1f904254655e [file] [log] [blame]
Ben Hutchings8ceee662008-04-27 12:55:59 +01001/****************************************************************************
Ben Hutchingsf7a6d2c2013-08-29 23:32:48 +01002 * Driver for Solarflare network controllers and boards
Ben Hutchings8ceee662008-04-27 12:55:59 +01003 * Copyright 2005-2006 Fen Systems Ltd.
Ben Hutchingsf7a6d2c2013-08-29 23:32:48 +01004 * Copyright 2005-2013 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>
Ben Hutchings183233b2013-06-28 21:47:12 +010020#include <linux/cache.h>
Ben Hutchings8ceee662008-04-27 12:55:59 +010021#include "net_driver.h"
Ben Hutchings8ceee662008-04-27 12:55:59 +010022#include "efx.h"
Ben Hutchings183233b2013-06-28 21:47:12 +010023#include "io.h"
Ben Hutchings744093c2009-11-29 15:12:08 +000024#include "nic.h"
Ben Hutchings8ceee662008-04-27 12:55:59 +010025#include "workarounds.h"
Ben Hutchingsdfa50be2013-03-08 21:20:09 +000026#include "ef10_regs.h"
Ben Hutchings8ceee662008-04-27 12:55:59 +010027
Ben Hutchings183233b2013-06-28 21:47:12 +010028#ifdef EFX_USE_PIO
29
30#define EFX_PIOBUF_SIZE_MAX ER_DZ_TX_PIOBUF_SIZE
31#define EFX_PIOBUF_SIZE_DEF ALIGN(256, L1_CACHE_BYTES)
32unsigned int efx_piobuf_size __read_mostly = EFX_PIOBUF_SIZE_DEF;
33
34#endif /* EFX_USE_PIO */
35
Ben Hutchings0fe55652013-06-28 21:47:15 +010036static inline unsigned int
37efx_tx_queue_get_insert_index(const struct efx_tx_queue *tx_queue)
38{
39 return tx_queue->insert_count & tx_queue->ptr_mask;
40}
41
42static inline struct efx_tx_buffer *
43__efx_tx_queue_get_insert_buffer(const struct efx_tx_queue *tx_queue)
44{
45 return &tx_queue->buffer[efx_tx_queue_get_insert_index(tx_queue)];
46}
47
48static inline struct efx_tx_buffer *
49efx_tx_queue_get_insert_buffer(const struct efx_tx_queue *tx_queue)
50{
51 struct efx_tx_buffer *buffer =
52 __efx_tx_queue_get_insert_buffer(tx_queue);
53
54 EFX_BUG_ON_PARANOID(buffer->len);
55 EFX_BUG_ON_PARANOID(buffer->flags);
56 EFX_BUG_ON_PARANOID(buffer->unmap_len);
57
58 return buffer;
59}
60
Ben Hutchings4d566062008-09-01 12:47:12 +010061static void efx_dequeue_buffer(struct efx_tx_queue *tx_queue,
Tom Herbertc3940992011-11-28 16:33:43 +000062 struct efx_tx_buffer *buffer,
63 unsigned int *pkts_compl,
64 unsigned int *bytes_compl)
Ben Hutchings8ceee662008-04-27 12:55:59 +010065{
66 if (buffer->unmap_len) {
Ben Hutchings0e33d872012-05-17 17:46:55 +010067 struct device *dma_dev = &tx_queue->efx->pci_dev->dev;
Alexandre Rames2acdb922013-10-31 12:42:32 +000068 dma_addr_t unmap_addr = buffer->dma_addr - buffer->dma_offset;
Ben Hutchings7668ff92012-05-17 20:52:20 +010069 if (buffer->flags & EFX_TX_BUF_MAP_SINGLE)
Ben Hutchings0e33d872012-05-17 17:46:55 +010070 dma_unmap_single(dma_dev, unmap_addr, buffer->unmap_len,
71 DMA_TO_DEVICE);
Ben Hutchings8ceee662008-04-27 12:55:59 +010072 else
Ben Hutchings0e33d872012-05-17 17:46:55 +010073 dma_unmap_page(dma_dev, unmap_addr, buffer->unmap_len,
74 DMA_TO_DEVICE);
Ben Hutchings8ceee662008-04-27 12:55:59 +010075 buffer->unmap_len = 0;
Ben Hutchings8ceee662008-04-27 12:55:59 +010076 }
77
Ben Hutchings7668ff92012-05-17 20:52:20 +010078 if (buffer->flags & EFX_TX_BUF_SKB) {
Tom Herbertc3940992011-11-28 16:33:43 +000079 (*pkts_compl)++;
80 (*bytes_compl) += buffer->skb->len;
Rick Jones4ef6dae2014-09-09 14:43:27 -070081 dev_consume_skb_any((struct sk_buff *)buffer->skb);
Ben Hutchings62776d02010-06-23 11:30:07 +000082 netif_vdbg(tx_queue->efx, tx_done, tx_queue->efx->net_dev,
83 "TX queue %d transmission id %x complete\n",
84 tx_queue->queue, tx_queue->read_count);
Ben Hutchingsf7251a92012-05-17 18:40:54 +010085 } else if (buffer->flags & EFX_TX_BUF_HEAP) {
86 kfree(buffer->heap_buf);
Ben Hutchings8ceee662008-04-27 12:55:59 +010087 }
Ben Hutchings7668ff92012-05-17 20:52:20 +010088
Ben Hutchingsf7251a92012-05-17 18:40:54 +010089 buffer->len = 0;
90 buffer->flags = 0;
Ben Hutchings8ceee662008-04-27 12:55:59 +010091}
92
Ben Hutchingsb9b39b62008-05-07 12:51:12 +010093static int efx_enqueue_skb_tso(struct efx_tx_queue *tx_queue,
Ben Hutchings740847d2008-09-01 12:48:23 +010094 struct sk_buff *skb);
Ben Hutchings8ceee662008-04-27 12:55:59 +010095
Ben Hutchings63f19882009-10-23 08:31:20 +000096static inline unsigned
97efx_max_tx_len(struct efx_nic *efx, dma_addr_t dma_addr)
98{
99 /* Depending on the NIC revision, we can use descriptor
100 * lengths up to 8K or 8K-1. However, since PCI Express
101 * devices must split read requests at 4K boundaries, there is
102 * little benefit from using descriptors that cross those
103 * boundaries and we keep things simple by not doing so.
104 */
Ben Hutchings5b6262d2012-02-02 21:21:15 +0000105 unsigned len = (~dma_addr & (EFX_PAGE_SIZE - 1)) + 1;
Ben Hutchings63f19882009-10-23 08:31:20 +0000106
107 /* Work around hardware bug for unaligned buffers. */
108 if (EFX_WORKAROUND_5391(efx) && (dma_addr & 0xf))
109 len = min_t(unsigned, len, 512 - (dma_addr & 0xf));
110
111 return len;
112}
113
Ben Hutchings7e6d06f2012-07-30 15:57:44 +0000114unsigned int efx_tx_max_skb_descs(struct efx_nic *efx)
115{
116 /* Header and payload descriptor for each output segment, plus
117 * one for every input fragment boundary within a segment
118 */
119 unsigned int max_descs = EFX_TSO_MAX_SEGS * 2 + MAX_SKB_FRAGS;
120
Ben Hutchingsdfa50be2013-03-08 21:20:09 +0000121 /* Possibly one more per segment for the alignment workaround,
122 * or for option descriptors
123 */
124 if (EFX_WORKAROUND_5391(efx) || efx_nic_rev(efx) >= EFX_REV_HUNT_A0)
Ben Hutchings7e6d06f2012-07-30 15:57:44 +0000125 max_descs += EFX_TSO_MAX_SEGS;
126
127 /* Possibly more for PCIe page boundaries within input fragments */
128 if (PAGE_SIZE > EFX_PAGE_SIZE)
129 max_descs += max_t(unsigned int, MAX_SKB_FRAGS,
130 DIV_ROUND_UP(GSO_MAX_SIZE, EFX_PAGE_SIZE));
131
132 return max_descs;
133}
134
Ben Hutchings14bf7182012-05-22 01:27:58 +0100135static void efx_tx_maybe_stop_queue(struct efx_tx_queue *txq1)
136{
137 /* We need to consider both queues that the net core sees as one */
138 struct efx_tx_queue *txq2 = efx_tx_queue_partner(txq1);
139 struct efx_nic *efx = txq1->efx;
140 unsigned int fill_level;
141
142 fill_level = max(txq1->insert_count - txq1->old_read_count,
143 txq2->insert_count - txq2->old_read_count);
144 if (likely(fill_level < efx->txq_stop_thresh))
145 return;
146
147 /* We used the stale old_read_count above, which gives us a
148 * pessimistic estimate of the fill level (which may even
149 * validly be >= efx->txq_entries). Now try again using
150 * read_count (more likely to be a cache miss).
151 *
152 * If we read read_count and then conditionally stop the
153 * queue, it is possible for the completion path to race with
154 * us and complete all outstanding descriptors in the middle,
155 * after which there will be no more completions to wake it.
156 * Therefore we stop the queue first, then read read_count
157 * (with a memory barrier to ensure the ordering), then
158 * restart the queue if the fill level turns out to be low
159 * enough.
160 */
161 netif_tx_stop_queue(txq1->core_txq);
162 smp_mb();
163 txq1->old_read_count = ACCESS_ONCE(txq1->read_count);
164 txq2->old_read_count = ACCESS_ONCE(txq2->read_count);
165
166 fill_level = max(txq1->insert_count - txq1->old_read_count,
167 txq2->insert_count - txq2->old_read_count);
168 EFX_BUG_ON_PARANOID(fill_level >= efx->txq_entries);
169 if (likely(fill_level < efx->txq_stop_thresh)) {
170 smp_mb();
171 if (likely(!efx->loopback_selftest))
172 netif_tx_start_queue(txq1->core_txq);
173 }
174}
175
Jon Cooperee45fd92c2013-09-02 18:24:29 +0100176#ifdef EFX_USE_PIO
177
178struct efx_short_copy_buffer {
179 int used;
180 u8 buf[L1_CACHE_BYTES];
181};
182
183/* Copy to PIO, respecting that writes to PIO buffers must be dword aligned.
184 * Advances piobuf pointer. Leaves additional data in the copy buffer.
185 */
186static void efx_memcpy_toio_aligned(struct efx_nic *efx, u8 __iomem **piobuf,
187 u8 *data, int len,
188 struct efx_short_copy_buffer *copy_buf)
189{
190 int block_len = len & ~(sizeof(copy_buf->buf) - 1);
191
Ben Hutchings4984c232014-07-27 03:14:39 +0100192 __iowrite64_copy(*piobuf, data, block_len >> 3);
Jon Cooperee45fd92c2013-09-02 18:24:29 +0100193 *piobuf += block_len;
194 len -= block_len;
195
196 if (len) {
197 data += block_len;
198 BUG_ON(copy_buf->used);
199 BUG_ON(len > sizeof(copy_buf->buf));
200 memcpy(copy_buf->buf, data, len);
201 copy_buf->used = len;
202 }
203}
204
205/* Copy to PIO, respecting dword alignment, popping data from copy buffer first.
206 * Advances piobuf pointer. Leaves additional data in the copy buffer.
207 */
208static void efx_memcpy_toio_aligned_cb(struct efx_nic *efx, u8 __iomem **piobuf,
209 u8 *data, int len,
210 struct efx_short_copy_buffer *copy_buf)
211{
212 if (copy_buf->used) {
213 /* if the copy buffer is partially full, fill it up and write */
214 int copy_to_buf =
215 min_t(int, sizeof(copy_buf->buf) - copy_buf->used, len);
216
217 memcpy(copy_buf->buf + copy_buf->used, data, copy_to_buf);
218 copy_buf->used += copy_to_buf;
219
220 /* if we didn't fill it up then we're done for now */
221 if (copy_buf->used < sizeof(copy_buf->buf))
222 return;
223
Ben Hutchings4984c232014-07-27 03:14:39 +0100224 __iowrite64_copy(*piobuf, copy_buf->buf,
225 sizeof(copy_buf->buf) >> 3);
Jon Cooperee45fd92c2013-09-02 18:24:29 +0100226 *piobuf += sizeof(copy_buf->buf);
227 data += copy_to_buf;
228 len -= copy_to_buf;
229 copy_buf->used = 0;
230 }
231
232 efx_memcpy_toio_aligned(efx, piobuf, data, len, copy_buf);
233}
234
235static void efx_flush_copy_buffer(struct efx_nic *efx, u8 __iomem *piobuf,
236 struct efx_short_copy_buffer *copy_buf)
237{
238 /* if there's anything in it, write the whole buffer, including junk */
239 if (copy_buf->used)
Ben Hutchings4984c232014-07-27 03:14:39 +0100240 __iowrite64_copy(piobuf, copy_buf->buf,
241 sizeof(copy_buf->buf) >> 3);
Jon Cooperee45fd92c2013-09-02 18:24:29 +0100242}
243
244/* Traverse skb structure and copy fragments in to PIO buffer.
245 * Advances piobuf pointer.
246 */
247static void efx_skb_copy_bits_to_pio(struct efx_nic *efx, struct sk_buff *skb,
248 u8 __iomem **piobuf,
249 struct efx_short_copy_buffer *copy_buf)
250{
251 int i;
252
253 efx_memcpy_toio_aligned(efx, piobuf, skb->data, skb_headlen(skb),
254 copy_buf);
255
256 for (i = 0; i < skb_shinfo(skb)->nr_frags; ++i) {
257 skb_frag_t *f = &skb_shinfo(skb)->frags[i];
258 u8 *vaddr;
259
260 vaddr = kmap_atomic(skb_frag_page(f));
261
262 efx_memcpy_toio_aligned_cb(efx, piobuf, vaddr + f->page_offset,
263 skb_frag_size(f), copy_buf);
264 kunmap_atomic(vaddr);
265 }
266
267 EFX_BUG_ON_PARANOID(skb_shinfo(skb)->frag_list);
268}
269
270static struct efx_tx_buffer *
271efx_enqueue_skb_pio(struct efx_tx_queue *tx_queue, struct sk_buff *skb)
272{
273 struct efx_tx_buffer *buffer =
274 efx_tx_queue_get_insert_buffer(tx_queue);
275 u8 __iomem *piobuf = tx_queue->piobuf;
276
277 /* Copy to PIO buffer. Ensure the writes are padded to the end
278 * of a cache line, as this is required for write-combining to be
279 * effective on at least x86.
280 */
281
282 if (skb_shinfo(skb)->nr_frags) {
283 /* The size of the copy buffer will ensure all writes
284 * are the size of a cache line.
285 */
286 struct efx_short_copy_buffer copy_buf;
287
288 copy_buf.used = 0;
289
290 efx_skb_copy_bits_to_pio(tx_queue->efx, skb,
291 &piobuf, &copy_buf);
292 efx_flush_copy_buffer(tx_queue->efx, piobuf, &copy_buf);
293 } else {
294 /* Pad the write to the size of a cache line.
295 * We can do this because we know the skb_shared_info sruct is
296 * after the source, and the destination buffer is big enough.
297 */
298 BUILD_BUG_ON(L1_CACHE_BYTES >
299 SKB_DATA_ALIGN(sizeof(struct skb_shared_info)));
Ben Hutchings4984c232014-07-27 03:14:39 +0100300 __iowrite64_copy(tx_queue->piobuf, skb->data,
301 ALIGN(skb->len, L1_CACHE_BYTES) >> 3);
Jon Cooperee45fd92c2013-09-02 18:24:29 +0100302 }
303
304 EFX_POPULATE_QWORD_5(buffer->option,
305 ESF_DZ_TX_DESC_IS_OPT, 1,
306 ESF_DZ_TX_OPTION_TYPE, ESE_DZ_TX_OPTION_DESC_PIO,
307 ESF_DZ_TX_PIO_CONT, 0,
308 ESF_DZ_TX_PIO_BYTE_CNT, skb->len,
309 ESF_DZ_TX_PIO_BUF_ADDR,
310 tx_queue->piobuf_offset);
311 ++tx_queue->pio_packets;
312 ++tx_queue->insert_count;
313 return buffer;
314}
315#endif /* EFX_USE_PIO */
316
Ben Hutchings8ceee662008-04-27 12:55:59 +0100317/*
318 * Add a socket buffer to a TX queue
319 *
320 * This maps all fragments of a socket buffer for DMA and adds them to
321 * the TX queue. The queue's insert pointer will be incremented by
322 * the number of fragments in the socket buffer.
323 *
324 * If any DMA mapping fails, any mapped fragments will be unmapped,
325 * the queue's insert pointer will be restored to its original value.
326 *
Ben Hutchings497f5ba2009-11-23 16:07:05 +0000327 * This function is split out from efx_hard_start_xmit to allow the
328 * loopback test to direct packets via specific TX queues.
329 *
Ben Hutchings14bf7182012-05-22 01:27:58 +0100330 * Returns NETDEV_TX_OK.
Ben Hutchings8ceee662008-04-27 12:55:59 +0100331 * You must hold netif_tx_lock() to call this function.
332 */
Ben Hutchings497f5ba2009-11-23 16:07:05 +0000333netdev_tx_t efx_enqueue_skb(struct efx_tx_queue *tx_queue, struct sk_buff *skb)
Ben Hutchings8ceee662008-04-27 12:55:59 +0100334{
335 struct efx_nic *efx = tx_queue->efx;
Ben Hutchings0e33d872012-05-17 17:46:55 +0100336 struct device *dma_dev = &efx->pci_dev->dev;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100337 struct efx_tx_buffer *buffer;
Edward Cree70b33fb2014-10-17 15:32:25 +0100338 unsigned int old_insert_count = tx_queue->insert_count;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100339 skb_frag_t *fragment;
Ben Hutchings0fe55652013-06-28 21:47:15 +0100340 unsigned int len, unmap_len = 0;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100341 dma_addr_t dma_addr, unmap_addr = 0;
342 unsigned int dma_len;
Ben Hutchings7668ff92012-05-17 20:52:20 +0100343 unsigned short dma_flags;
Ben Hutchings14bf7182012-05-22 01:27:58 +0100344 int i = 0;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100345
Ben Hutchings9bc183d2009-11-23 16:06:47 +0000346 if (skb_shinfo(skb)->gso_size)
Ben Hutchingsb9b39b62008-05-07 12:51:12 +0100347 return efx_enqueue_skb_tso(tx_queue, skb);
348
Ben Hutchings8ceee662008-04-27 12:55:59 +0100349 /* Get size of the initial fragment */
350 len = skb_headlen(skb);
351
Ben Hutchingsbb145a92009-03-20 13:25:39 +0000352 /* Pad if necessary */
353 if (EFX_WORKAROUND_15592(efx) && skb->len <= 32) {
354 EFX_BUG_ON_PARANOID(skb->data_len);
355 len = 32 + 1;
356 if (skb_pad(skb, len - skb->len))
357 return NETDEV_TX_OK;
358 }
359
Jon Cooperee45fd92c2013-09-02 18:24:29 +0100360 /* Consider using PIO for short packets */
361#ifdef EFX_USE_PIO
Edward Cree70b33fb2014-10-17 15:32:25 +0100362 if (skb->len <= efx_piobuf_size && !skb->xmit_more &&
363 efx_nic_may_tx_pio(tx_queue)) {
Jon Cooperee45fd92c2013-09-02 18:24:29 +0100364 buffer = efx_enqueue_skb_pio(tx_queue, skb);
365 dma_flags = EFX_TX_BUF_OPTION;
366 goto finish_packet;
367 }
368#endif
369
Ben Hutchings0e33d872012-05-17 17:46:55 +0100370 /* Map for DMA. Use dma_map_single rather than dma_map_page
Ben Hutchings8ceee662008-04-27 12:55:59 +0100371 * since this is more efficient on machines with sparse
372 * memory.
373 */
Ben Hutchings7668ff92012-05-17 20:52:20 +0100374 dma_flags = EFX_TX_BUF_MAP_SINGLE;
Ben Hutchings0e33d872012-05-17 17:46:55 +0100375 dma_addr = dma_map_single(dma_dev, skb->data, len, PCI_DMA_TODEVICE);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100376
377 /* Process all fragments */
378 while (1) {
Ben Hutchings0e33d872012-05-17 17:46:55 +0100379 if (unlikely(dma_mapping_error(dma_dev, dma_addr)))
380 goto dma_err;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100381
382 /* Store fields for marking in the per-fragment final
383 * descriptor */
384 unmap_len = len;
385 unmap_addr = dma_addr;
386
387 /* Add to TX queue, splitting across DMA boundaries */
388 do {
Ben Hutchings0fe55652013-06-28 21:47:15 +0100389 buffer = efx_tx_queue_get_insert_buffer(tx_queue);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100390
Ben Hutchings63f19882009-10-23 08:31:20 +0000391 dma_len = efx_max_tx_len(efx, dma_addr);
392 if (likely(dma_len >= len))
Ben Hutchings8ceee662008-04-27 12:55:59 +0100393 dma_len = len;
394
Ben Hutchings8ceee662008-04-27 12:55:59 +0100395 /* Fill out per descriptor fields */
396 buffer->len = dma_len;
397 buffer->dma_addr = dma_addr;
Ben Hutchings7668ff92012-05-17 20:52:20 +0100398 buffer->flags = EFX_TX_BUF_CONT;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100399 len -= dma_len;
400 dma_addr += dma_len;
401 ++tx_queue->insert_count;
402 } while (len);
403
404 /* Transfer ownership of the unmapping to the final buffer */
Ben Hutchings7668ff92012-05-17 20:52:20 +0100405 buffer->flags = EFX_TX_BUF_CONT | dma_flags;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100406 buffer->unmap_len = unmap_len;
Alexandre Rames2acdb922013-10-31 12:42:32 +0000407 buffer->dma_offset = buffer->dma_addr - unmap_addr;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100408 unmap_len = 0;
409
410 /* Get address and size of next fragment */
411 if (i >= skb_shinfo(skb)->nr_frags)
412 break;
413 fragment = &skb_shinfo(skb)->frags[i];
Eric Dumazet9e903e02011-10-18 21:00:24 +0000414 len = skb_frag_size(fragment);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100415 i++;
416 /* Map for DMA */
Ben Hutchings7668ff92012-05-17 20:52:20 +0100417 dma_flags = 0;
Ben Hutchings0e33d872012-05-17 17:46:55 +0100418 dma_addr = skb_frag_dma_map(dma_dev, fragment, 0, len,
Ian Campbell5d6bcdf2011-10-06 11:10:48 +0100419 DMA_TO_DEVICE);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100420 }
421
422 /* Transfer ownership of the skb to the final buffer */
Paul Gortmaker440b87e2014-02-06 11:45:12 -0500423#ifdef EFX_USE_PIO
Jon Cooperee45fd92c2013-09-02 18:24:29 +0100424finish_packet:
Paul Gortmaker440b87e2014-02-06 11:45:12 -0500425#endif
Ben Hutchings8ceee662008-04-27 12:55:59 +0100426 buffer->skb = skb;
Ben Hutchings7668ff92012-05-17 20:52:20 +0100427 buffer->flags = EFX_TX_BUF_SKB | dma_flags;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100428
Tom Herbertc3940992011-11-28 16:33:43 +0000429 netdev_tx_sent_queue(tx_queue->core_txq, skb->len);
430
Edward Cree70b33fb2014-10-17 15:32:25 +0100431 efx_tx_maybe_stop_queue(tx_queue);
432
Ben Hutchings8ceee662008-04-27 12:55:59 +0100433 /* Pass off to hardware */
Martin Habetsb2663a42015-11-02 12:51:31 +0000434 if (!skb->xmit_more || netif_xmit_stopped(tx_queue->core_txq)) {
435 struct efx_tx_queue *txq2 = efx_tx_queue_partner(tx_queue);
436
437 /* There could be packets left on the partner queue if those
438 * SKBs had skb->xmit_more set. If we do not push those they
439 * could be left for a long time and cause a netdev watchdog.
440 */
441 if (txq2->xmit_more_available)
442 efx_nic_push_buffers(txq2);
443
Edward Cree70b33fb2014-10-17 15:32:25 +0100444 efx_nic_push_buffers(tx_queue);
Martin Habetsb2663a42015-11-02 12:51:31 +0000445 } else {
446 tx_queue->xmit_more_available = skb->xmit_more;
447 }
Ben Hutchings8ceee662008-04-27 12:55:59 +0100448
Andrew Rybchenko8ccf38002014-07-17 12:10:43 +0100449 tx_queue->tx_packets++;
450
Ben Hutchings8ceee662008-04-27 12:55:59 +0100451 return NETDEV_TX_OK;
452
Ben Hutchings0e33d872012-05-17 17:46:55 +0100453 dma_err:
Ben Hutchings62776d02010-06-23 11:30:07 +0000454 netif_err(efx, tx_err, efx->net_dev,
455 " TX queue %d could not map skb with %d bytes %d "
456 "fragments for DMA\n", tx_queue->queue, skb->len,
457 skb_shinfo(skb)->nr_frags + 1);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100458
459 /* Mark the packet as transmitted, and free the SKB ourselves */
Ben Hutchings9bc183d2009-11-23 16:06:47 +0000460 dev_kfree_skb_any(skb);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100461
Ben Hutchings8ceee662008-04-27 12:55:59 +0100462 /* Work backwards until we hit the original insert pointer value */
Edward Cree70b33fb2014-10-17 15:32:25 +0100463 while (tx_queue->insert_count != old_insert_count) {
Tom Herbertc3940992011-11-28 16:33:43 +0000464 unsigned int pkts_compl = 0, bytes_compl = 0;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100465 --tx_queue->insert_count;
Ben Hutchings0fe55652013-06-28 21:47:15 +0100466 buffer = __efx_tx_queue_get_insert_buffer(tx_queue);
Tom Herbertc3940992011-11-28 16:33:43 +0000467 efx_dequeue_buffer(tx_queue, buffer, &pkts_compl, &bytes_compl);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100468 }
469
470 /* Free the fragment we were mid-way through pushing */
Ben Hutchingsecbd95c2008-09-01 12:46:40 +0100471 if (unmap_len) {
Ben Hutchings7668ff92012-05-17 20:52:20 +0100472 if (dma_flags & EFX_TX_BUF_MAP_SINGLE)
Ben Hutchings0e33d872012-05-17 17:46:55 +0100473 dma_unmap_single(dma_dev, unmap_addr, unmap_len,
474 DMA_TO_DEVICE);
Ben Hutchingsecbd95c2008-09-01 12:46:40 +0100475 else
Ben Hutchings0e33d872012-05-17 17:46:55 +0100476 dma_unmap_page(dma_dev, unmap_addr, unmap_len,
477 DMA_TO_DEVICE);
Ben Hutchingsecbd95c2008-09-01 12:46:40 +0100478 }
Ben Hutchings8ceee662008-04-27 12:55:59 +0100479
Ben Hutchings14bf7182012-05-22 01:27:58 +0100480 return NETDEV_TX_OK;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100481}
482
483/* Remove packets from the TX queue
484 *
485 * This removes packets from the TX queue, up to and including the
486 * specified index.
487 */
Ben Hutchings4d566062008-09-01 12:47:12 +0100488static void efx_dequeue_buffers(struct efx_tx_queue *tx_queue,
Tom Herbertc3940992011-11-28 16:33:43 +0000489 unsigned int index,
490 unsigned int *pkts_compl,
491 unsigned int *bytes_compl)
Ben Hutchings8ceee662008-04-27 12:55:59 +0100492{
493 struct efx_nic *efx = tx_queue->efx;
494 unsigned int stop_index, read_ptr;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100495
Steve Hodgsonecc910f2010-09-10 06:42:22 +0000496 stop_index = (index + 1) & tx_queue->ptr_mask;
497 read_ptr = tx_queue->read_count & tx_queue->ptr_mask;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100498
499 while (read_ptr != stop_index) {
500 struct efx_tx_buffer *buffer = &tx_queue->buffer[read_ptr];
Ben Hutchingsba8977b2013-01-08 23:43:19 +0000501
502 if (!(buffer->flags & EFX_TX_BUF_OPTION) &&
503 unlikely(buffer->len == 0)) {
Ben Hutchings62776d02010-06-23 11:30:07 +0000504 netif_err(efx, tx_err, efx->net_dev,
505 "TX queue %d spurious TX completion id %x\n",
506 tx_queue->queue, read_ptr);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100507 efx_schedule_reset(efx, RESET_TYPE_TX_SKIP);
508 return;
509 }
510
Tom Herbertc3940992011-11-28 16:33:43 +0000511 efx_dequeue_buffer(tx_queue, buffer, pkts_compl, bytes_compl);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100512
513 ++tx_queue->read_count;
Steve Hodgsonecc910f2010-09-10 06:42:22 +0000514 read_ptr = tx_queue->read_count & tx_queue->ptr_mask;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100515 }
516}
517
Ben Hutchings8ceee662008-04-27 12:55:59 +0100518/* Initiate a packet transmission. We use one channel per CPU
519 * (sharing when we have more CPUs than channels). On Falcon, the TX
520 * completion events will be directed back to the CPU that transmitted
521 * the packet, which should be cache-efficient.
522 *
523 * Context: non-blocking.
524 * Note that returning anything other than NETDEV_TX_OK will cause the
525 * OS to free the skb.
526 */
Stephen Hemminger613573252009-08-31 19:50:58 +0000527netdev_tx_t efx_hard_start_xmit(struct sk_buff *skb,
Ben Hutchings2d0cc562012-02-17 00:10:45 +0000528 struct net_device *net_dev)
Ben Hutchings8ceee662008-04-27 12:55:59 +0100529{
Ben Hutchings767e4682008-09-01 12:43:14 +0100530 struct efx_nic *efx = netdev_priv(net_dev);
Ben Hutchings60ac1062008-09-01 12:44:59 +0100531 struct efx_tx_queue *tx_queue;
Ben Hutchings94b274b2011-01-10 21:18:20 +0000532 unsigned index, type;
Ben Hutchings60ac1062008-09-01 12:44:59 +0100533
Ben Hutchingse4abce82011-05-16 18:51:24 +0100534 EFX_WARN_ON_PARANOID(!netif_device_present(net_dev));
Ben Hutchingsa7ef5932009-03-04 09:52:37 +0000535
Stuart Hodgson7c236c42012-09-03 11:09:36 +0100536 /* PTP "event" packet */
537 if (unlikely(efx_xmit_with_hwtstamp(skb)) &&
538 unlikely(efx_ptp_is_ptp_tx(efx, skb))) {
539 return efx_ptp_tx(efx, skb);
540 }
541
Ben Hutchings94b274b2011-01-10 21:18:20 +0000542 index = skb_get_queue_mapping(skb);
543 type = skb->ip_summed == CHECKSUM_PARTIAL ? EFX_TXQ_TYPE_OFFLOAD : 0;
544 if (index >= efx->n_tx_channels) {
545 index -= efx->n_tx_channels;
546 type |= EFX_TXQ_TYPE_HIGHPRI;
547 }
548 tx_queue = efx_get_tx_queue(efx, index, type);
Ben Hutchings60ac1062008-09-01 12:44:59 +0100549
Ben Hutchings497f5ba2009-11-23 16:07:05 +0000550 return efx_enqueue_skb(tx_queue, skb);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100551}
552
Ben Hutchings60031fc2011-01-12 18:39:40 +0000553void efx_init_tx_queue_core_txq(struct efx_tx_queue *tx_queue)
554{
Ben Hutchings94b274b2011-01-10 21:18:20 +0000555 struct efx_nic *efx = tx_queue->efx;
556
Ben Hutchings60031fc2011-01-12 18:39:40 +0000557 /* Must be inverse of queue lookup in efx_hard_start_xmit() */
Ben Hutchings94b274b2011-01-10 21:18:20 +0000558 tx_queue->core_txq =
559 netdev_get_tx_queue(efx->net_dev,
560 tx_queue->queue / EFX_TXQ_TYPES +
561 ((tx_queue->queue & EFX_TXQ_TYPE_HIGHPRI) ?
562 efx->n_tx_channels : 0));
563}
564
John Fastabende4c67342016-02-16 21:16:15 -0800565int efx_setup_tc(struct net_device *net_dev, u32 handle, u8 num_tc)
Ben Hutchings94b274b2011-01-10 21:18:20 +0000566{
567 struct efx_nic *efx = netdev_priv(net_dev);
568 struct efx_channel *channel;
569 struct efx_tx_queue *tx_queue;
570 unsigned tc;
571 int rc;
572
John Fastabende4c67342016-02-16 21:16:15 -0800573 if (handle != TC_H_ROOT)
574 return -EINVAL;
575
Ben Hutchings94b274b2011-01-10 21:18:20 +0000576 if (efx_nic_rev(efx) < EFX_REV_FALCON_B0 || num_tc > EFX_MAX_TX_TC)
577 return -EINVAL;
578
579 if (num_tc == net_dev->num_tc)
580 return 0;
581
582 for (tc = 0; tc < num_tc; tc++) {
583 net_dev->tc_to_txq[tc].offset = tc * efx->n_tx_channels;
584 net_dev->tc_to_txq[tc].count = efx->n_tx_channels;
585 }
586
587 if (num_tc > net_dev->num_tc) {
588 /* Initialise high-priority queues as necessary */
589 efx_for_each_channel(channel, efx) {
590 efx_for_each_possible_channel_tx_queue(tx_queue,
591 channel) {
592 if (!(tx_queue->queue & EFX_TXQ_TYPE_HIGHPRI))
593 continue;
594 if (!tx_queue->buffer) {
595 rc = efx_probe_tx_queue(tx_queue);
596 if (rc)
597 return rc;
598 }
599 if (!tx_queue->initialised)
600 efx_init_tx_queue(tx_queue);
601 efx_init_tx_queue_core_txq(tx_queue);
602 }
603 }
604 } else {
605 /* Reduce number of classes before number of queues */
606 net_dev->num_tc = num_tc;
607 }
608
609 rc = netif_set_real_num_tx_queues(net_dev,
610 max_t(int, num_tc, 1) *
611 efx->n_tx_channels);
612 if (rc)
613 return rc;
614
615 /* Do not destroy high-priority queues when they become
616 * unused. We would have to flush them first, and it is
617 * fairly difficult to flush a subset of TX queues. Leave
618 * it to efx_fini_channels().
619 */
620
621 net_dev->num_tc = num_tc;
622 return 0;
Ben Hutchings60031fc2011-01-12 18:39:40 +0000623}
624
Ben Hutchings8ceee662008-04-27 12:55:59 +0100625void efx_xmit_done(struct efx_tx_queue *tx_queue, unsigned int index)
626{
627 unsigned fill_level;
628 struct efx_nic *efx = tx_queue->efx;
Ben Hutchings14bf7182012-05-22 01:27:58 +0100629 struct efx_tx_queue *txq2;
Tom Herbertc3940992011-11-28 16:33:43 +0000630 unsigned int pkts_compl = 0, bytes_compl = 0;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100631
Steve Hodgsonecc910f2010-09-10 06:42:22 +0000632 EFX_BUG_ON_PARANOID(index > tx_queue->ptr_mask);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100633
Tom Herbertc3940992011-11-28 16:33:43 +0000634 efx_dequeue_buffers(tx_queue, index, &pkts_compl, &bytes_compl);
Peter Dunningc9368352015-07-08 10:05:10 +0100635 tx_queue->pkts_compl += pkts_compl;
636 tx_queue->bytes_compl += bytes_compl;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100637
Ben Hutchings02e12162013-04-27 01:55:21 +0100638 if (pkts_compl > 1)
639 ++tx_queue->merge_events;
640
Ben Hutchings14bf7182012-05-22 01:27:58 +0100641 /* See if we need to restart the netif queue. This memory
642 * barrier ensures that we write read_count (inside
643 * efx_dequeue_buffers()) before reading the queue status.
644 */
Ben Hutchings8ceee662008-04-27 12:55:59 +0100645 smp_mb();
Ben Hutchingsc04bfc62010-12-10 01:24:16 +0000646 if (unlikely(netif_tx_queue_stopped(tx_queue->core_txq)) &&
Neil Turton9d1aea62011-04-04 13:46:23 +0100647 likely(efx->port_enabled) &&
Ben Hutchingse4abce82011-05-16 18:51:24 +0100648 likely(netif_device_present(efx->net_dev))) {
Ben Hutchings14bf7182012-05-22 01:27:58 +0100649 txq2 = efx_tx_queue_partner(tx_queue);
650 fill_level = max(tx_queue->insert_count - tx_queue->read_count,
651 txq2->insert_count - txq2->read_count);
652 if (fill_level <= efx->txq_wake_thresh)
Ben Hutchingsc04bfc62010-12-10 01:24:16 +0000653 netif_tx_wake_queue(tx_queue->core_txq);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100654 }
Ben Hutchingscd385572010-11-15 23:53:11 +0000655
656 /* Check whether the hardware queue is now empty */
657 if ((int)(tx_queue->read_count - tx_queue->old_write_count) >= 0) {
658 tx_queue->old_write_count = ACCESS_ONCE(tx_queue->write_count);
659 if (tx_queue->read_count == tx_queue->old_write_count) {
660 smp_mb();
661 tx_queue->empty_read_count =
662 tx_queue->read_count | EFX_EMPTY_COUNT_VALID;
663 }
664 }
Ben Hutchings8ceee662008-04-27 12:55:59 +0100665}
666
Ben Hutchingsf7251a92012-05-17 18:40:54 +0100667/* Size of page-based TSO header buffers. Larger blocks must be
668 * allocated from the heap.
669 */
670#define TSOH_STD_SIZE 128
671#define TSOH_PER_PAGE (PAGE_SIZE / TSOH_STD_SIZE)
672
673/* At most half the descriptors in the queue at any time will refer to
674 * a TSO header buffer, since they must always be followed by a
675 * payload descriptor referring to an skb.
676 */
677static unsigned int efx_tsoh_page_count(struct efx_tx_queue *tx_queue)
678{
679 return DIV_ROUND_UP(tx_queue->ptr_mask + 1, 2 * TSOH_PER_PAGE);
680}
681
Ben Hutchings8ceee662008-04-27 12:55:59 +0100682int efx_probe_tx_queue(struct efx_tx_queue *tx_queue)
683{
684 struct efx_nic *efx = tx_queue->efx;
Steve Hodgsonecc910f2010-09-10 06:42:22 +0000685 unsigned int entries;
Ben Hutchings7668ff92012-05-17 20:52:20 +0100686 int rc;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100687
Steve Hodgsonecc910f2010-09-10 06:42:22 +0000688 /* Create the smallest power-of-two aligned ring */
689 entries = max(roundup_pow_of_two(efx->txq_entries), EFX_MIN_DMAQ_SIZE);
690 EFX_BUG_ON_PARANOID(entries > EFX_MAX_DMAQ_SIZE);
691 tx_queue->ptr_mask = entries - 1;
692
693 netif_dbg(efx, probe, efx->net_dev,
694 "creating TX queue %d size %#x mask %#x\n",
695 tx_queue->queue, efx->txq_entries, tx_queue->ptr_mask);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100696
697 /* Allocate software ring */
Thomas Meyerc2e4e252011-12-02 12:36:13 +0000698 tx_queue->buffer = kcalloc(entries, sizeof(*tx_queue->buffer),
Steve Hodgsonecc910f2010-09-10 06:42:22 +0000699 GFP_KERNEL);
Ben Hutchings60ac1062008-09-01 12:44:59 +0100700 if (!tx_queue->buffer)
701 return -ENOMEM;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100702
Ben Hutchingsf7251a92012-05-17 18:40:54 +0100703 if (tx_queue->queue & EFX_TXQ_TYPE_OFFLOAD) {
704 tx_queue->tsoh_page =
705 kcalloc(efx_tsoh_page_count(tx_queue),
706 sizeof(tx_queue->tsoh_page[0]), GFP_KERNEL);
707 if (!tx_queue->tsoh_page) {
708 rc = -ENOMEM;
709 goto fail1;
710 }
711 }
712
Ben Hutchings8ceee662008-04-27 12:55:59 +0100713 /* Allocate hardware ring */
Ben Hutchings152b6a62009-11-29 03:43:56 +0000714 rc = efx_nic_probe_tx(tx_queue);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100715 if (rc)
Ben Hutchingsf7251a92012-05-17 18:40:54 +0100716 goto fail2;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100717
718 return 0;
719
Ben Hutchingsf7251a92012-05-17 18:40:54 +0100720fail2:
721 kfree(tx_queue->tsoh_page);
722 tx_queue->tsoh_page = NULL;
723fail1:
Ben Hutchings8ceee662008-04-27 12:55:59 +0100724 kfree(tx_queue->buffer);
725 tx_queue->buffer = NULL;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100726 return rc;
727}
728
Ben Hutchingsbc3c90a2008-09-01 12:48:46 +0100729void efx_init_tx_queue(struct efx_tx_queue *tx_queue)
Ben Hutchings8ceee662008-04-27 12:55:59 +0100730{
Ben Hutchings62776d02010-06-23 11:30:07 +0000731 netif_dbg(tx_queue->efx, drv, tx_queue->efx->net_dev,
732 "initialising TX queue %d\n", tx_queue->queue);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100733
734 tx_queue->insert_count = 0;
735 tx_queue->write_count = 0;
Ben Hutchingscd385572010-11-15 23:53:11 +0000736 tx_queue->old_write_count = 0;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100737 tx_queue->read_count = 0;
738 tx_queue->old_read_count = 0;
Ben Hutchingscd385572010-11-15 23:53:11 +0000739 tx_queue->empty_read_count = 0 | EFX_EMPTY_COUNT_VALID;
Martin Habetsb2663a42015-11-02 12:51:31 +0000740 tx_queue->xmit_more_available = false;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100741
742 /* Set up TX descriptor ring */
Ben Hutchings152b6a62009-11-29 03:43:56 +0000743 efx_nic_init_tx(tx_queue);
Ben Hutchings94b274b2011-01-10 21:18:20 +0000744
745 tx_queue->initialised = true;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100746}
747
Ben Hutchingse42c3d82013-05-27 16:52:54 +0100748void efx_fini_tx_queue(struct efx_tx_queue *tx_queue)
Ben Hutchings8ceee662008-04-27 12:55:59 +0100749{
750 struct efx_tx_buffer *buffer;
751
Ben Hutchingse42c3d82013-05-27 16:52:54 +0100752 netif_dbg(tx_queue->efx, drv, tx_queue->efx->net_dev,
753 "shutting down TX queue %d\n", tx_queue->queue);
754
Ben Hutchings8ceee662008-04-27 12:55:59 +0100755 if (!tx_queue->buffer)
756 return;
757
758 /* Free any buffers left in the ring */
759 while (tx_queue->read_count != tx_queue->write_count) {
Tom Herbertc3940992011-11-28 16:33:43 +0000760 unsigned int pkts_compl = 0, bytes_compl = 0;
Steve Hodgsonecc910f2010-09-10 06:42:22 +0000761 buffer = &tx_queue->buffer[tx_queue->read_count & tx_queue->ptr_mask];
Tom Herbertc3940992011-11-28 16:33:43 +0000762 efx_dequeue_buffer(tx_queue, buffer, &pkts_compl, &bytes_compl);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100763
764 ++tx_queue->read_count;
765 }
Martin Habetsb2663a42015-11-02 12:51:31 +0000766 tx_queue->xmit_more_available = false;
Tom Herbertc3940992011-11-28 16:33:43 +0000767 netdev_tx_reset_queue(tx_queue->core_txq);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100768}
769
Ben Hutchings8ceee662008-04-27 12:55:59 +0100770void efx_remove_tx_queue(struct efx_tx_queue *tx_queue)
771{
Ben Hutchingsf7251a92012-05-17 18:40:54 +0100772 int i;
773
Ben Hutchings94b274b2011-01-10 21:18:20 +0000774 if (!tx_queue->buffer)
775 return;
776
Ben Hutchings62776d02010-06-23 11:30:07 +0000777 netif_dbg(tx_queue->efx, drv, tx_queue->efx->net_dev,
778 "destroying TX queue %d\n", tx_queue->queue);
Ben Hutchings152b6a62009-11-29 03:43:56 +0000779 efx_nic_remove_tx(tx_queue);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100780
Ben Hutchingsf7251a92012-05-17 18:40:54 +0100781 if (tx_queue->tsoh_page) {
782 for (i = 0; i < efx_tsoh_page_count(tx_queue); i++)
783 efx_nic_free_buffer(tx_queue->efx,
784 &tx_queue->tsoh_page[i]);
785 kfree(tx_queue->tsoh_page);
786 tx_queue->tsoh_page = NULL;
787 }
788
Ben Hutchings8ceee662008-04-27 12:55:59 +0100789 kfree(tx_queue->buffer);
790 tx_queue->buffer = NULL;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100791}
792
793
Ben Hutchingsb9b39b62008-05-07 12:51:12 +0100794/* Efx TCP segmentation acceleration.
795 *
796 * Why? Because by doing it here in the driver we can go significantly
797 * faster than the GSO.
798 *
799 * Requires TX checksum offload support.
800 */
801
Ben Hutchingsb9b39b62008-05-07 12:51:12 +0100802#define PTR_DIFF(p1, p2) ((u8 *)(p1) - (u8 *)(p2))
Ben Hutchingsb9b39b62008-05-07 12:51:12 +0100803
804/**
805 * struct tso_state - TSO state for an SKB
Ben Hutchings23d9e602008-09-01 12:47:02 +0100806 * @out_len: Remaining length in current segment
Ben Hutchingsb9b39b62008-05-07 12:51:12 +0100807 * @seqnum: Current sequence number
Ben Hutchings23d9e602008-09-01 12:47:02 +0100808 * @ipv4_id: Current IPv4 ID, host endian
Ben Hutchingsb9b39b62008-05-07 12:51:12 +0100809 * @packet_space: Remaining space in current packet
Ben Hutchings23d9e602008-09-01 12:47:02 +0100810 * @dma_addr: DMA address of current position
811 * @in_len: Remaining length in current SKB fragment
812 * @unmap_len: Length of SKB fragment
813 * @unmap_addr: DMA address of SKB fragment
Ben Hutchings7668ff92012-05-17 20:52:20 +0100814 * @dma_flags: TX buffer flags for DMA mapping - %EFX_TX_BUF_MAP_SINGLE or 0
Ben Hutchings738a8f42009-11-29 15:16:05 +0000815 * @protocol: Network protocol (after any VLAN header)
Ben Hutchings97142842012-06-22 02:44:01 +0100816 * @ip_off: Offset of IP header
817 * @tcp_off: Offset of TCP header
Ben Hutchings23d9e602008-09-01 12:47:02 +0100818 * @header_len: Number of bytes of header
Ben Hutchings53cb13c2012-06-19 20:03:41 +0100819 * @ip_base_len: IPv4 tot_len or IPv6 payload_len, before TCP payload
Ben Hutchingsdfa50be2013-03-08 21:20:09 +0000820 * @header_dma_addr: Header DMA address, when using option descriptors
821 * @header_unmap_len: Header DMA mapped length, or 0 if not using option
822 * descriptors
Ben Hutchingsb9b39b62008-05-07 12:51:12 +0100823 *
824 * The state used during segmentation. It is put into this data structure
825 * just to make it easy to pass into inline functions.
826 */
827struct tso_state {
Ben Hutchings23d9e602008-09-01 12:47:02 +0100828 /* Output position */
829 unsigned out_len;
Ben Hutchingsb9b39b62008-05-07 12:51:12 +0100830 unsigned seqnum;
Ben Hutchingsdfa50be2013-03-08 21:20:09 +0000831 u16 ipv4_id;
Ben Hutchingsb9b39b62008-05-07 12:51:12 +0100832 unsigned packet_space;
833
Ben Hutchings23d9e602008-09-01 12:47:02 +0100834 /* Input position */
835 dma_addr_t dma_addr;
836 unsigned in_len;
837 unsigned unmap_len;
838 dma_addr_t unmap_addr;
Ben Hutchings7668ff92012-05-17 20:52:20 +0100839 unsigned short dma_flags;
Ben Hutchingsb9b39b62008-05-07 12:51:12 +0100840
Ben Hutchings738a8f42009-11-29 15:16:05 +0000841 __be16 protocol;
Ben Hutchings97142842012-06-22 02:44:01 +0100842 unsigned int ip_off;
843 unsigned int tcp_off;
Ben Hutchings23d9e602008-09-01 12:47:02 +0100844 unsigned header_len;
Ben Hutchings53cb13c2012-06-19 20:03:41 +0100845 unsigned int ip_base_len;
Ben Hutchingsdfa50be2013-03-08 21:20:09 +0000846 dma_addr_t header_dma_addr;
847 unsigned int header_unmap_len;
Ben Hutchingsb9b39b62008-05-07 12:51:12 +0100848};
849
850
851/*
852 * Verify that our various assumptions about sk_buffs and the conditions
Ben Hutchings738a8f42009-11-29 15:16:05 +0000853 * under which TSO will be attempted hold true. Return the protocol number.
Ben Hutchingsb9b39b62008-05-07 12:51:12 +0100854 */
Ben Hutchings738a8f42009-11-29 15:16:05 +0000855static __be16 efx_tso_check_protocol(struct sk_buff *skb)
Ben Hutchingsb9b39b62008-05-07 12:51:12 +0100856{
Ben Hutchings740847d2008-09-01 12:48:23 +0100857 __be16 protocol = skb->protocol;
858
Ben Hutchingsb9b39b62008-05-07 12:51:12 +0100859 EFX_BUG_ON_PARANOID(((struct ethhdr *)skb->data)->h_proto !=
Ben Hutchings740847d2008-09-01 12:48:23 +0100860 protocol);
861 if (protocol == htons(ETH_P_8021Q)) {
Ben Hutchings740847d2008-09-01 12:48:23 +0100862 struct vlan_ethhdr *veh = (struct vlan_ethhdr *)skb->data;
863 protocol = veh->h_vlan_encapsulated_proto;
Ben Hutchings740847d2008-09-01 12:48:23 +0100864 }
865
Ben Hutchings738a8f42009-11-29 15:16:05 +0000866 if (protocol == htons(ETH_P_IP)) {
867 EFX_BUG_ON_PARANOID(ip_hdr(skb)->protocol != IPPROTO_TCP);
868 } else {
869 EFX_BUG_ON_PARANOID(protocol != htons(ETH_P_IPV6));
870 EFX_BUG_ON_PARANOID(ipv6_hdr(skb)->nexthdr != NEXTHDR_TCP);
871 }
Ben Hutchingsb9b39b62008-05-07 12:51:12 +0100872 EFX_BUG_ON_PARANOID((PTR_DIFF(tcp_hdr(skb), skb->data)
873 + (tcp_hdr(skb)->doff << 2u)) >
874 skb_headlen(skb));
Ben Hutchings738a8f42009-11-29 15:16:05 +0000875
876 return protocol;
Ben Hutchingsb9b39b62008-05-07 12:51:12 +0100877}
878
Ben Hutchingsf7251a92012-05-17 18:40:54 +0100879static u8 *efx_tsoh_get_buffer(struct efx_tx_queue *tx_queue,
880 struct efx_tx_buffer *buffer, unsigned int len)
Ben Hutchingsb9b39b62008-05-07 12:51:12 +0100881{
Ben Hutchingsf7251a92012-05-17 18:40:54 +0100882 u8 *result;
Ben Hutchingsb9b39b62008-05-07 12:51:12 +0100883
Ben Hutchingsf7251a92012-05-17 18:40:54 +0100884 EFX_BUG_ON_PARANOID(buffer->len);
885 EFX_BUG_ON_PARANOID(buffer->flags);
886 EFX_BUG_ON_PARANOID(buffer->unmap_len);
887
Ben Hutchings0bdadad2014-02-12 18:58:57 +0000888 if (likely(len <= TSOH_STD_SIZE - NET_IP_ALIGN)) {
Ben Hutchingsf7251a92012-05-17 18:40:54 +0100889 unsigned index =
890 (tx_queue->insert_count & tx_queue->ptr_mask) / 2;
891 struct efx_buffer *page_buf =
892 &tx_queue->tsoh_page[index / TSOH_PER_PAGE];
893 unsigned offset =
Ben Hutchings0bdadad2014-02-12 18:58:57 +0000894 TSOH_STD_SIZE * (index % TSOH_PER_PAGE) + NET_IP_ALIGN;
Ben Hutchingsf7251a92012-05-17 18:40:54 +0100895
896 if (unlikely(!page_buf->addr) &&
Ben Hutchings0d19a542012-09-18 21:59:52 +0100897 efx_nic_alloc_buffer(tx_queue->efx, page_buf, PAGE_SIZE,
898 GFP_ATOMIC))
Ben Hutchingsf7251a92012-05-17 18:40:54 +0100899 return NULL;
900
901 result = (u8 *)page_buf->addr + offset;
902 buffer->dma_addr = page_buf->dma_addr + offset;
903 buffer->flags = EFX_TX_BUF_CONT;
904 } else {
905 tx_queue->tso_long_headers++;
906
Ben Hutchings0bdadad2014-02-12 18:58:57 +0000907 buffer->heap_buf = kmalloc(NET_IP_ALIGN + len, GFP_ATOMIC);
Ben Hutchingsf7251a92012-05-17 18:40:54 +0100908 if (unlikely(!buffer->heap_buf))
909 return NULL;
Ben Hutchings0bdadad2014-02-12 18:58:57 +0000910 result = (u8 *)buffer->heap_buf + NET_IP_ALIGN;
Ben Hutchingsf7251a92012-05-17 18:40:54 +0100911 buffer->flags = EFX_TX_BUF_CONT | EFX_TX_BUF_HEAP;
Ben Hutchingsb9b39b62008-05-07 12:51:12 +0100912 }
913
Ben Hutchingsf7251a92012-05-17 18:40:54 +0100914 buffer->len = len;
Ben Hutchingsb9b39b62008-05-07 12:51:12 +0100915
Ben Hutchingsf7251a92012-05-17 18:40:54 +0100916 return result;
Ben Hutchingsb9b39b62008-05-07 12:51:12 +0100917}
918
919/**
920 * efx_tx_queue_insert - push descriptors onto the TX queue
921 * @tx_queue: Efx TX queue
922 * @dma_addr: DMA address of fragment
923 * @len: Length of fragment
Ben Hutchingsecbd95c2008-09-01 12:46:40 +0100924 * @final_buffer: The final buffer inserted into the queue
Ben Hutchingsb9b39b62008-05-07 12:51:12 +0100925 *
Ben Hutchings14bf7182012-05-22 01:27:58 +0100926 * Push descriptors onto the TX queue.
Ben Hutchingsb9b39b62008-05-07 12:51:12 +0100927 */
Ben Hutchings14bf7182012-05-22 01:27:58 +0100928static void efx_tx_queue_insert(struct efx_tx_queue *tx_queue,
929 dma_addr_t dma_addr, unsigned len,
930 struct efx_tx_buffer **final_buffer)
Ben Hutchingsb9b39b62008-05-07 12:51:12 +0100931{
932 struct efx_tx_buffer *buffer;
933 struct efx_nic *efx = tx_queue->efx;
Ben Hutchings0fe55652013-06-28 21:47:15 +0100934 unsigned dma_len;
Ben Hutchingsb9b39b62008-05-07 12:51:12 +0100935
936 EFX_BUG_ON_PARANOID(len <= 0);
937
Ben Hutchingsb9b39b62008-05-07 12:51:12 +0100938 while (1) {
Ben Hutchings0fe55652013-06-28 21:47:15 +0100939 buffer = efx_tx_queue_get_insert_buffer(tx_queue);
Ben Hutchingsb9b39b62008-05-07 12:51:12 +0100940 ++tx_queue->insert_count;
941
942 EFX_BUG_ON_PARANOID(tx_queue->insert_count -
Steve Hodgsonecc910f2010-09-10 06:42:22 +0000943 tx_queue->read_count >=
944 efx->txq_entries);
Ben Hutchingsb9b39b62008-05-07 12:51:12 +0100945
Ben Hutchingsb9b39b62008-05-07 12:51:12 +0100946 buffer->dma_addr = dma_addr;
947
Ben Hutchings63f19882009-10-23 08:31:20 +0000948 dma_len = efx_max_tx_len(efx, dma_addr);
Ben Hutchingsb9b39b62008-05-07 12:51:12 +0100949
950 /* If there is enough space to send then do so */
951 if (dma_len >= len)
952 break;
953
Ben Hutchings7668ff92012-05-17 20:52:20 +0100954 buffer->len = dma_len;
955 buffer->flags = EFX_TX_BUF_CONT;
Ben Hutchingsb9b39b62008-05-07 12:51:12 +0100956 dma_addr += dma_len;
957 len -= dma_len;
958 }
959
960 EFX_BUG_ON_PARANOID(!len);
961 buffer->len = len;
Ben Hutchingsecbd95c2008-09-01 12:46:40 +0100962 *final_buffer = buffer;
Ben Hutchingsb9b39b62008-05-07 12:51:12 +0100963}
964
965
966/*
967 * Put a TSO header into the TX queue.
968 *
969 * This is special-cased because we know that it is small enough to fit in
970 * a single fragment, and we know it doesn't cross a page boundary. It
971 * also allows us to not worry about end-of-packet etc.
972 */
Ben Hutchingsf7251a92012-05-17 18:40:54 +0100973static int efx_tso_put_header(struct efx_tx_queue *tx_queue,
974 struct efx_tx_buffer *buffer, u8 *header)
Ben Hutchingsb9b39b62008-05-07 12:51:12 +0100975{
Ben Hutchingsf7251a92012-05-17 18:40:54 +0100976 if (unlikely(buffer->flags & EFX_TX_BUF_HEAP)) {
977 buffer->dma_addr = dma_map_single(&tx_queue->efx->pci_dev->dev,
978 header, buffer->len,
979 DMA_TO_DEVICE);
980 if (unlikely(dma_mapping_error(&tx_queue->efx->pci_dev->dev,
981 buffer->dma_addr))) {
982 kfree(buffer->heap_buf);
983 buffer->len = 0;
984 buffer->flags = 0;
985 return -ENOMEM;
986 }
987 buffer->unmap_len = buffer->len;
Alexandre Rames2acdb922013-10-31 12:42:32 +0000988 buffer->dma_offset = 0;
Ben Hutchingsf7251a92012-05-17 18:40:54 +0100989 buffer->flags |= EFX_TX_BUF_MAP_SINGLE;
990 }
Ben Hutchingsb9b39b62008-05-07 12:51:12 +0100991
992 ++tx_queue->insert_count;
Ben Hutchingsf7251a92012-05-17 18:40:54 +0100993 return 0;
Ben Hutchingsb9b39b62008-05-07 12:51:12 +0100994}
995
996
Ben Hutchingsf7251a92012-05-17 18:40:54 +0100997/* Remove buffers put into a tx_queue. None of the buffers must have
998 * an skb attached.
999 */
Edward Cree70b33fb2014-10-17 15:32:25 +01001000static void efx_enqueue_unwind(struct efx_tx_queue *tx_queue,
1001 unsigned int insert_count)
Ben Hutchingsb9b39b62008-05-07 12:51:12 +01001002{
1003 struct efx_tx_buffer *buffer;
1004
1005 /* Work backwards until we hit the original insert pointer value */
Edward Cree70b33fb2014-10-17 15:32:25 +01001006 while (tx_queue->insert_count != insert_count) {
Ben Hutchingsb9b39b62008-05-07 12:51:12 +01001007 --tx_queue->insert_count;
Ben Hutchings0fe55652013-06-28 21:47:15 +01001008 buffer = __efx_tx_queue_get_insert_buffer(tx_queue);
Ben Hutchingsf7251a92012-05-17 18:40:54 +01001009 efx_dequeue_buffer(tx_queue, buffer, NULL, NULL);
Ben Hutchingsb9b39b62008-05-07 12:51:12 +01001010 }
1011}
1012
1013
1014/* Parse the SKB header and initialise state. */
Ben Hutchingsc78c39e2013-03-08 20:03:17 +00001015static int tso_start(struct tso_state *st, struct efx_nic *efx,
Bert Kenward93171b12015-11-30 09:05:35 +00001016 struct efx_tx_queue *tx_queue,
Ben Hutchingsc78c39e2013-03-08 20:03:17 +00001017 const struct sk_buff *skb)
Ben Hutchingsb9b39b62008-05-07 12:51:12 +01001018{
Ben Hutchingsdfa50be2013-03-08 21:20:09 +00001019 struct device *dma_dev = &efx->pci_dev->dev;
Ben Hutchingsc78c39e2013-03-08 20:03:17 +00001020 unsigned int header_len, in_len;
Bert Kenward93171b12015-11-30 09:05:35 +00001021 bool use_opt_desc = false;
Ben Hutchingsdfa50be2013-03-08 21:20:09 +00001022 dma_addr_t dma_addr;
Ben Hutchingsc78c39e2013-03-08 20:03:17 +00001023
Bert Kenward93171b12015-11-30 09:05:35 +00001024 if (tx_queue->tso_version == 1)
1025 use_opt_desc = true;
1026
Ben Hutchings97142842012-06-22 02:44:01 +01001027 st->ip_off = skb_network_header(skb) - skb->data;
1028 st->tcp_off = skb_transport_header(skb) - skb->data;
Ben Hutchingsc78c39e2013-03-08 20:03:17 +00001029 header_len = st->tcp_off + (tcp_hdr(skb)->doff << 2u);
1030 in_len = skb_headlen(skb) - header_len;
1031 st->header_len = header_len;
1032 st->in_len = in_len;
Ben Hutchings53cb13c2012-06-19 20:03:41 +01001033 if (st->protocol == htons(ETH_P_IP)) {
Ben Hutchings97142842012-06-22 02:44:01 +01001034 st->ip_base_len = st->header_len - st->ip_off;
Ben Hutchings738a8f42009-11-29 15:16:05 +00001035 st->ipv4_id = ntohs(ip_hdr(skb)->id);
Ben Hutchings53cb13c2012-06-19 20:03:41 +01001036 } else {
Ben Hutchings97142842012-06-22 02:44:01 +01001037 st->ip_base_len = st->header_len - st->tcp_off;
Ben Hutchings738a8f42009-11-29 15:16:05 +00001038 st->ipv4_id = 0;
Ben Hutchings53cb13c2012-06-19 20:03:41 +01001039 }
Ben Hutchingsb9b39b62008-05-07 12:51:12 +01001040 st->seqnum = ntohl(tcp_hdr(skb)->seq);
1041
1042 EFX_BUG_ON_PARANOID(tcp_hdr(skb)->urg);
1043 EFX_BUG_ON_PARANOID(tcp_hdr(skb)->syn);
1044 EFX_BUG_ON_PARANOID(tcp_hdr(skb)->rst);
1045
Ben Hutchingsc78c39e2013-03-08 20:03:17 +00001046 st->out_len = skb->len - header_len;
1047
Ben Hutchings93413f52014-02-12 18:59:41 +00001048 if (!use_opt_desc) {
Ben Hutchingsdfa50be2013-03-08 21:20:09 +00001049 st->header_unmap_len = 0;
1050
1051 if (likely(in_len == 0)) {
1052 st->dma_flags = 0;
1053 st->unmap_len = 0;
1054 return 0;
1055 }
1056
1057 dma_addr = dma_map_single(dma_dev, skb->data + header_len,
1058 in_len, DMA_TO_DEVICE);
1059 st->dma_flags = EFX_TX_BUF_MAP_SINGLE;
1060 st->dma_addr = dma_addr;
1061 st->unmap_addr = dma_addr;
1062 st->unmap_len = in_len;
1063 } else {
1064 dma_addr = dma_map_single(dma_dev, skb->data,
1065 skb_headlen(skb), DMA_TO_DEVICE);
1066 st->header_dma_addr = dma_addr;
1067 st->header_unmap_len = skb_headlen(skb);
Ben Hutchingsc78c39e2013-03-08 20:03:17 +00001068 st->dma_flags = 0;
Ben Hutchingsdfa50be2013-03-08 21:20:09 +00001069 st->dma_addr = dma_addr + header_len;
1070 st->unmap_len = 0;
Ben Hutchingsc78c39e2013-03-08 20:03:17 +00001071 }
1072
Ben Hutchingsdfa50be2013-03-08 21:20:09 +00001073 return unlikely(dma_mapping_error(dma_dev, dma_addr)) ? -ENOMEM : 0;
Ben Hutchingsb9b39b62008-05-07 12:51:12 +01001074}
1075
Ben Hutchings4d566062008-09-01 12:47:12 +01001076static int tso_get_fragment(struct tso_state *st, struct efx_nic *efx,
1077 skb_frag_t *frag)
Ben Hutchingsb9b39b62008-05-07 12:51:12 +01001078{
Ian Campbell4a22c4c2011-09-21 21:53:16 +00001079 st->unmap_addr = skb_frag_dma_map(&efx->pci_dev->dev, frag, 0,
Eric Dumazet9e903e02011-10-18 21:00:24 +00001080 skb_frag_size(frag), DMA_TO_DEVICE);
Ian Campbell5d6bcdf2011-10-06 11:10:48 +01001081 if (likely(!dma_mapping_error(&efx->pci_dev->dev, st->unmap_addr))) {
Ben Hutchings7668ff92012-05-17 20:52:20 +01001082 st->dma_flags = 0;
Eric Dumazet9e903e02011-10-18 21:00:24 +00001083 st->unmap_len = skb_frag_size(frag);
1084 st->in_len = skb_frag_size(frag);
Ben Hutchings23d9e602008-09-01 12:47:02 +01001085 st->dma_addr = st->unmap_addr;
Ben Hutchingsecbd95c2008-09-01 12:46:40 +01001086 return 0;
1087 }
1088 return -ENOMEM;
1089}
1090
Ben Hutchingsb9b39b62008-05-07 12:51:12 +01001091
1092/**
1093 * tso_fill_packet_with_fragment - form descriptors for the current fragment
1094 * @tx_queue: Efx TX queue
1095 * @skb: Socket buffer
1096 * @st: TSO state
1097 *
1098 * Form descriptors for the current fragment, until we reach the end
Ben Hutchings14bf7182012-05-22 01:27:58 +01001099 * of fragment or end-of-packet.
Ben Hutchingsb9b39b62008-05-07 12:51:12 +01001100 */
Ben Hutchings14bf7182012-05-22 01:27:58 +01001101static void tso_fill_packet_with_fragment(struct efx_tx_queue *tx_queue,
1102 const struct sk_buff *skb,
1103 struct tso_state *st)
Ben Hutchingsb9b39b62008-05-07 12:51:12 +01001104{
Ben Hutchingsecbd95c2008-09-01 12:46:40 +01001105 struct efx_tx_buffer *buffer;
Ben Hutchings14bf7182012-05-22 01:27:58 +01001106 int n;
Ben Hutchingsb9b39b62008-05-07 12:51:12 +01001107
Ben Hutchings23d9e602008-09-01 12:47:02 +01001108 if (st->in_len == 0)
Ben Hutchings14bf7182012-05-22 01:27:58 +01001109 return;
Ben Hutchingsb9b39b62008-05-07 12:51:12 +01001110 if (st->packet_space == 0)
Ben Hutchings14bf7182012-05-22 01:27:58 +01001111 return;
Ben Hutchingsb9b39b62008-05-07 12:51:12 +01001112
Ben Hutchings23d9e602008-09-01 12:47:02 +01001113 EFX_BUG_ON_PARANOID(st->in_len <= 0);
Ben Hutchingsb9b39b62008-05-07 12:51:12 +01001114 EFX_BUG_ON_PARANOID(st->packet_space <= 0);
1115
Ben Hutchings23d9e602008-09-01 12:47:02 +01001116 n = min(st->in_len, st->packet_space);
Ben Hutchingsb9b39b62008-05-07 12:51:12 +01001117
1118 st->packet_space -= n;
Ben Hutchings23d9e602008-09-01 12:47:02 +01001119 st->out_len -= n;
1120 st->in_len -= n;
Ben Hutchingsb9b39b62008-05-07 12:51:12 +01001121
Ben Hutchings14bf7182012-05-22 01:27:58 +01001122 efx_tx_queue_insert(tx_queue, st->dma_addr, n, &buffer);
Ben Hutchingsecbd95c2008-09-01 12:46:40 +01001123
Ben Hutchings14bf7182012-05-22 01:27:58 +01001124 if (st->out_len == 0) {
1125 /* Transfer ownership of the skb */
1126 buffer->skb = skb;
1127 buffer->flags = EFX_TX_BUF_SKB;
1128 } else if (st->packet_space != 0) {
1129 buffer->flags = EFX_TX_BUF_CONT;
1130 }
1131
1132 if (st->in_len == 0) {
1133 /* Transfer ownership of the DMA mapping */
1134 buffer->unmap_len = st->unmap_len;
Alexandre Rames2acdb922013-10-31 12:42:32 +00001135 buffer->dma_offset = buffer->unmap_len - buffer->len;
Ben Hutchings14bf7182012-05-22 01:27:58 +01001136 buffer->flags |= st->dma_flags;
1137 st->unmap_len = 0;
Ben Hutchingsecbd95c2008-09-01 12:46:40 +01001138 }
Ben Hutchingsb9b39b62008-05-07 12:51:12 +01001139
Ben Hutchings23d9e602008-09-01 12:47:02 +01001140 st->dma_addr += n;
Ben Hutchingsb9b39b62008-05-07 12:51:12 +01001141}
1142
1143
1144/**
1145 * tso_start_new_packet - generate a new header and prepare for the new packet
1146 * @tx_queue: Efx TX queue
1147 * @skb: Socket buffer
1148 * @st: TSO state
1149 *
1150 * Generate a new header and prepare for the new packet. Return 0 on
Ben Hutchingsf7251a92012-05-17 18:40:54 +01001151 * success, or -%ENOMEM if failed to alloc header.
Ben Hutchingsb9b39b62008-05-07 12:51:12 +01001152 */
Ben Hutchings4d566062008-09-01 12:47:12 +01001153static int tso_start_new_packet(struct efx_tx_queue *tx_queue,
1154 const struct sk_buff *skb,
1155 struct tso_state *st)
Ben Hutchingsb9b39b62008-05-07 12:51:12 +01001156{
Ben Hutchingsf7251a92012-05-17 18:40:54 +01001157 struct efx_tx_buffer *buffer =
Ben Hutchings0fe55652013-06-28 21:47:15 +01001158 efx_tx_queue_get_insert_buffer(tx_queue);
Ben Hutchingsdfa50be2013-03-08 21:20:09 +00001159 bool is_last = st->out_len <= skb_shinfo(skb)->gso_size;
1160 u8 tcp_flags_clear;
Ben Hutchingsb9b39b62008-05-07 12:51:12 +01001161
Ben Hutchingsdfa50be2013-03-08 21:20:09 +00001162 if (!is_last) {
Ben Hutchings53cb13c2012-06-19 20:03:41 +01001163 st->packet_space = skb_shinfo(skb)->gso_size;
Ben Hutchingsdfa50be2013-03-08 21:20:09 +00001164 tcp_flags_clear = 0x09; /* mask out FIN and PSH */
Ben Hutchingsb9b39b62008-05-07 12:51:12 +01001165 } else {
Ben Hutchings53cb13c2012-06-19 20:03:41 +01001166 st->packet_space = st->out_len;
Ben Hutchingsdfa50be2013-03-08 21:20:09 +00001167 tcp_flags_clear = 0x00;
Ben Hutchingsb9b39b62008-05-07 12:51:12 +01001168 }
Ben Hutchingsb9b39b62008-05-07 12:51:12 +01001169
Ben Hutchingsdfa50be2013-03-08 21:20:09 +00001170 if (!st->header_unmap_len) {
1171 /* Allocate and insert a DMA-mapped header buffer. */
1172 struct tcphdr *tsoh_th;
1173 unsigned ip_length;
1174 u8 *header;
1175 int rc;
Ben Hutchings738a8f42009-11-29 15:16:05 +00001176
Ben Hutchingsdfa50be2013-03-08 21:20:09 +00001177 header = efx_tsoh_get_buffer(tx_queue, buffer, st->header_len);
1178 if (!header)
1179 return -ENOMEM;
Ben Hutchings738a8f42009-11-29 15:16:05 +00001180
Ben Hutchingsdfa50be2013-03-08 21:20:09 +00001181 tsoh_th = (struct tcphdr *)(header + st->tcp_off);
1182
1183 /* Copy and update the headers. */
1184 memcpy(header, skb->data, st->header_len);
1185
1186 tsoh_th->seq = htonl(st->seqnum);
1187 ((u8 *)tsoh_th)[13] &= ~tcp_flags_clear;
1188
1189 ip_length = st->ip_base_len + st->packet_space;
1190
1191 if (st->protocol == htons(ETH_P_IP)) {
1192 struct iphdr *tsoh_iph =
1193 (struct iphdr *)(header + st->ip_off);
1194
1195 tsoh_iph->tot_len = htons(ip_length);
1196 tsoh_iph->id = htons(st->ipv4_id);
1197 } else {
1198 struct ipv6hdr *tsoh_iph =
1199 (struct ipv6hdr *)(header + st->ip_off);
1200
1201 tsoh_iph->payload_len = htons(ip_length);
1202 }
1203
1204 rc = efx_tso_put_header(tx_queue, buffer, header);
1205 if (unlikely(rc))
1206 return rc;
Ben Hutchings738a8f42009-11-29 15:16:05 +00001207 } else {
Ben Hutchingsdfa50be2013-03-08 21:20:09 +00001208 /* Send the original headers with a TSO option descriptor
1209 * in front
1210 */
1211 u8 tcp_flags = ((u8 *)tcp_hdr(skb))[13] & ~tcp_flags_clear;
Ben Hutchings738a8f42009-11-29 15:16:05 +00001212
Ben Hutchingsdfa50be2013-03-08 21:20:09 +00001213 buffer->flags = EFX_TX_BUF_OPTION;
1214 buffer->len = 0;
1215 buffer->unmap_len = 0;
1216 EFX_POPULATE_QWORD_5(buffer->option,
1217 ESF_DZ_TX_DESC_IS_OPT, 1,
1218 ESF_DZ_TX_OPTION_TYPE,
1219 ESE_DZ_TX_OPTION_DESC_TSO,
1220 ESF_DZ_TX_TSO_TCP_FLAGS, tcp_flags,
1221 ESF_DZ_TX_TSO_IP_ID, st->ipv4_id,
1222 ESF_DZ_TX_TSO_TCP_SEQNO, st->seqnum);
1223 ++tx_queue->insert_count;
1224
1225 /* We mapped the headers in tso_start(). Unmap them
1226 * when the last segment is completed.
1227 */
Ben Hutchings0fe55652013-06-28 21:47:15 +01001228 buffer = efx_tx_queue_get_insert_buffer(tx_queue);
Ben Hutchingsdfa50be2013-03-08 21:20:09 +00001229 buffer->dma_addr = st->header_dma_addr;
1230 buffer->len = st->header_len;
1231 if (is_last) {
1232 buffer->flags = EFX_TX_BUF_CONT | EFX_TX_BUF_MAP_SINGLE;
1233 buffer->unmap_len = st->header_unmap_len;
Alexandre Rames2acdb922013-10-31 12:42:32 +00001234 buffer->dma_offset = 0;
Ben Hutchingsdfa50be2013-03-08 21:20:09 +00001235 /* Ensure we only unmap them once in case of a
1236 * later DMA mapping error and rollback
1237 */
1238 st->header_unmap_len = 0;
1239 } else {
1240 buffer->flags = EFX_TX_BUF_CONT;
1241 buffer->unmap_len = 0;
1242 }
1243 ++tx_queue->insert_count;
Ben Hutchings738a8f42009-11-29 15:16:05 +00001244 }
Ben Hutchingsb9b39b62008-05-07 12:51:12 +01001245
Ben Hutchingsdfa50be2013-03-08 21:20:09 +00001246 st->seqnum += skb_shinfo(skb)->gso_size;
1247
1248 /* Linux leaves suitable gaps in the IP ID space for us to fill. */
1249 ++st->ipv4_id;
Ben Hutchingsf7251a92012-05-17 18:40:54 +01001250
Ben Hutchingsb9b39b62008-05-07 12:51:12 +01001251 ++tx_queue->tso_packets;
1252
Andrew Rybchenko8ccf38002014-07-17 12:10:43 +01001253 ++tx_queue->tx_packets;
1254
Ben Hutchingsb9b39b62008-05-07 12:51:12 +01001255 return 0;
1256}
1257
1258
1259/**
1260 * efx_enqueue_skb_tso - segment and transmit a TSO socket buffer
1261 * @tx_queue: Efx TX queue
1262 * @skb: Socket buffer
1263 *
1264 * Context: You must hold netif_tx_lock() to call this function.
1265 *
1266 * Add socket buffer @skb to @tx_queue, doing TSO or return != 0 if
1267 * @skb was not enqueued. In all cases @skb is consumed. Return
Ben Hutchings14bf7182012-05-22 01:27:58 +01001268 * %NETDEV_TX_OK.
Ben Hutchingsb9b39b62008-05-07 12:51:12 +01001269 */
1270static int efx_enqueue_skb_tso(struct efx_tx_queue *tx_queue,
Ben Hutchings740847d2008-09-01 12:48:23 +01001271 struct sk_buff *skb)
Ben Hutchingsb9b39b62008-05-07 12:51:12 +01001272{
Ben Hutchingsecbd95c2008-09-01 12:46:40 +01001273 struct efx_nic *efx = tx_queue->efx;
Edward Cree70b33fb2014-10-17 15:32:25 +01001274 unsigned int old_insert_count = tx_queue->insert_count;
Ben Hutchings14bf7182012-05-22 01:27:58 +01001275 int frag_i, rc;
Ben Hutchingsb9b39b62008-05-07 12:51:12 +01001276 struct tso_state state;
Ben Hutchingsb9b39b62008-05-07 12:51:12 +01001277
Ben Hutchings738a8f42009-11-29 15:16:05 +00001278 /* Find the packet protocol and sanity-check it */
1279 state.protocol = efx_tso_check_protocol(skb);
Ben Hutchingsb9b39b62008-05-07 12:51:12 +01001280
Bert Kenward93171b12015-11-30 09:05:35 +00001281 rc = tso_start(&state, efx, tx_queue, skb);
Ben Hutchingsc78c39e2013-03-08 20:03:17 +00001282 if (rc)
1283 goto mem_err;
Ben Hutchingsb9b39b62008-05-07 12:51:12 +01001284
Ben Hutchingsc78c39e2013-03-08 20:03:17 +00001285 if (likely(state.in_len == 0)) {
Ben Hutchingsb9b39b62008-05-07 12:51:12 +01001286 /* Grab the first payload fragment. */
1287 EFX_BUG_ON_PARANOID(skb_shinfo(skb)->nr_frags < 1);
1288 frag_i = 0;
Ben Hutchingsecbd95c2008-09-01 12:46:40 +01001289 rc = tso_get_fragment(&state, efx,
1290 skb_shinfo(skb)->frags + frag_i);
Ben Hutchingsb9b39b62008-05-07 12:51:12 +01001291 if (rc)
1292 goto mem_err;
1293 } else {
Ben Hutchingsc78c39e2013-03-08 20:03:17 +00001294 /* Payload starts in the header area. */
Ben Hutchingsb9b39b62008-05-07 12:51:12 +01001295 frag_i = -1;
1296 }
1297
1298 if (tso_start_new_packet(tx_queue, skb, &state) < 0)
1299 goto mem_err;
1300
1301 while (1) {
Ben Hutchings14bf7182012-05-22 01:27:58 +01001302 tso_fill_packet_with_fragment(tx_queue, skb, &state);
Ben Hutchingsb9b39b62008-05-07 12:51:12 +01001303
1304 /* Move onto the next fragment? */
Ben Hutchings23d9e602008-09-01 12:47:02 +01001305 if (state.in_len == 0) {
Ben Hutchingsb9b39b62008-05-07 12:51:12 +01001306 if (++frag_i >= skb_shinfo(skb)->nr_frags)
1307 /* End of payload reached. */
1308 break;
Ben Hutchingsecbd95c2008-09-01 12:46:40 +01001309 rc = tso_get_fragment(&state, efx,
1310 skb_shinfo(skb)->frags + frag_i);
Ben Hutchingsb9b39b62008-05-07 12:51:12 +01001311 if (rc)
1312 goto mem_err;
1313 }
1314
1315 /* Start at new packet? */
1316 if (state.packet_space == 0 &&
1317 tso_start_new_packet(tx_queue, skb, &state) < 0)
1318 goto mem_err;
1319 }
1320
Eric Dumazet449fa022011-11-30 17:12:27 -05001321 netdev_tx_sent_queue(tx_queue->core_txq, skb->len);
1322
Ben Hutchings14bf7182012-05-22 01:27:58 +01001323 efx_tx_maybe_stop_queue(tx_queue);
1324
Edward Cree70b33fb2014-10-17 15:32:25 +01001325 /* Pass off to hardware */
Martin Habetsb2663a42015-11-02 12:51:31 +00001326 if (!skb->xmit_more || netif_xmit_stopped(tx_queue->core_txq)) {
1327 struct efx_tx_queue *txq2 = efx_tx_queue_partner(tx_queue);
1328
1329 /* There could be packets left on the partner queue if those
1330 * SKBs had skb->xmit_more set. If we do not push those they
1331 * could be left for a long time and cause a netdev watchdog.
1332 */
1333 if (txq2->xmit_more_available)
1334 efx_nic_push_buffers(txq2);
1335
Edward Cree70b33fb2014-10-17 15:32:25 +01001336 efx_nic_push_buffers(tx_queue);
Martin Habetsb2663a42015-11-02 12:51:31 +00001337 } else {
1338 tx_queue->xmit_more_available = skb->xmit_more;
1339 }
Edward Cree70b33fb2014-10-17 15:32:25 +01001340
Ben Hutchingsb9b39b62008-05-07 12:51:12 +01001341 tx_queue->tso_bursts++;
1342 return NETDEV_TX_OK;
1343
1344 mem_err:
Ben Hutchings62776d02010-06-23 11:30:07 +00001345 netif_err(efx, tx_err, efx->net_dev,
Ben Hutchings0e33d872012-05-17 17:46:55 +01001346 "Out of memory for TSO headers, or DMA mapping error\n");
Ben Hutchings9bc183d2009-11-23 16:06:47 +00001347 dev_kfree_skb_any(skb);
Ben Hutchingsb9b39b62008-05-07 12:51:12 +01001348
Ben Hutchings5988b632008-09-01 12:46:36 +01001349 /* Free the DMA mapping we were in the process of writing out */
Ben Hutchings23d9e602008-09-01 12:47:02 +01001350 if (state.unmap_len) {
Ben Hutchings7668ff92012-05-17 20:52:20 +01001351 if (state.dma_flags & EFX_TX_BUF_MAP_SINGLE)
Ben Hutchings0e33d872012-05-17 17:46:55 +01001352 dma_unmap_single(&efx->pci_dev->dev, state.unmap_addr,
1353 state.unmap_len, DMA_TO_DEVICE);
Ben Hutchingsecbd95c2008-09-01 12:46:40 +01001354 else
Ben Hutchings0e33d872012-05-17 17:46:55 +01001355 dma_unmap_page(&efx->pci_dev->dev, state.unmap_addr,
1356 state.unmap_len, DMA_TO_DEVICE);
Ben Hutchingsecbd95c2008-09-01 12:46:40 +01001357 }
Ben Hutchings5988b632008-09-01 12:46:36 +01001358
Ben Hutchingsdfa50be2013-03-08 21:20:09 +00001359 /* Free the header DMA mapping, if using option descriptors */
1360 if (state.header_unmap_len)
1361 dma_unmap_single(&efx->pci_dev->dev, state.header_dma_addr,
1362 state.header_unmap_len, DMA_TO_DEVICE);
1363
Edward Cree70b33fb2014-10-17 15:32:25 +01001364 efx_enqueue_unwind(tx_queue, old_insert_count);
Ben Hutchings14bf7182012-05-22 01:27:58 +01001365 return NETDEV_TX_OK;
Ben Hutchingsb9b39b62008-05-07 12:51:12 +01001366}