blob: 282692c48e6b6de94949e814ecdadf848186edc6 [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;
Ben Hutchingscc12dac2008-09-01 12:46:43 +010068 dma_addr_t unmap_addr = (buffer->dma_addr + buffer->len -
69 buffer->unmap_len);
Ben Hutchings7668ff92012-05-17 20:52:20 +010070 if (buffer->flags & EFX_TX_BUF_MAP_SINGLE)
Ben Hutchings0e33d872012-05-17 17:46:55 +010071 dma_unmap_single(dma_dev, unmap_addr, buffer->unmap_len,
72 DMA_TO_DEVICE);
Ben Hutchings8ceee662008-04-27 12:55:59 +010073 else
Ben Hutchings0e33d872012-05-17 17:46:55 +010074 dma_unmap_page(dma_dev, unmap_addr, buffer->unmap_len,
75 DMA_TO_DEVICE);
Ben Hutchings8ceee662008-04-27 12:55:59 +010076 buffer->unmap_len = 0;
Ben Hutchings8ceee662008-04-27 12:55:59 +010077 }
78
Ben Hutchings7668ff92012-05-17 20:52:20 +010079 if (buffer->flags & EFX_TX_BUF_SKB) {
Tom Herbertc3940992011-11-28 16:33:43 +000080 (*pkts_compl)++;
81 (*bytes_compl) += buffer->skb->len;
Ben Hutchings8ceee662008-04-27 12:55:59 +010082 dev_kfree_skb_any((struct sk_buff *) buffer->skb);
Ben Hutchings62776d02010-06-23 11:30:07 +000083 netif_vdbg(tx_queue->efx, tx_done, tx_queue->efx->net_dev,
84 "TX queue %d transmission id %x complete\n",
85 tx_queue->queue, tx_queue->read_count);
Ben Hutchingsf7251a92012-05-17 18:40:54 +010086 } else if (buffer->flags & EFX_TX_BUF_HEAP) {
87 kfree(buffer->heap_buf);
Ben Hutchings8ceee662008-04-27 12:55:59 +010088 }
Ben Hutchings7668ff92012-05-17 20:52:20 +010089
Ben Hutchingsf7251a92012-05-17 18:40:54 +010090 buffer->len = 0;
91 buffer->flags = 0;
Ben Hutchings8ceee662008-04-27 12:55:59 +010092}
93
Ben Hutchingsb9b39b62008-05-07 12:51:12 +010094static int efx_enqueue_skb_tso(struct efx_tx_queue *tx_queue,
Ben Hutchings740847d2008-09-01 12:48:23 +010095 struct sk_buff *skb);
Ben Hutchings8ceee662008-04-27 12:55:59 +010096
Ben Hutchings63f19882009-10-23 08:31:20 +000097static inline unsigned
98efx_max_tx_len(struct efx_nic *efx, dma_addr_t dma_addr)
99{
100 /* Depending on the NIC revision, we can use descriptor
101 * lengths up to 8K or 8K-1. However, since PCI Express
102 * devices must split read requests at 4K boundaries, there is
103 * little benefit from using descriptors that cross those
104 * boundaries and we keep things simple by not doing so.
105 */
Ben Hutchings5b6262d2012-02-02 21:21:15 +0000106 unsigned len = (~dma_addr & (EFX_PAGE_SIZE - 1)) + 1;
Ben Hutchings63f19882009-10-23 08:31:20 +0000107
108 /* Work around hardware bug for unaligned buffers. */
109 if (EFX_WORKAROUND_5391(efx) && (dma_addr & 0xf))
110 len = min_t(unsigned, len, 512 - (dma_addr & 0xf));
111
112 return len;
113}
114
Ben Hutchings7e6d06f2012-07-30 15:57:44 +0000115unsigned int efx_tx_max_skb_descs(struct efx_nic *efx)
116{
117 /* Header and payload descriptor for each output segment, plus
118 * one for every input fragment boundary within a segment
119 */
120 unsigned int max_descs = EFX_TSO_MAX_SEGS * 2 + MAX_SKB_FRAGS;
121
Ben Hutchingsdfa50be2013-03-08 21:20:09 +0000122 /* Possibly one more per segment for the alignment workaround,
123 * or for option descriptors
124 */
125 if (EFX_WORKAROUND_5391(efx) || efx_nic_rev(efx) >= EFX_REV_HUNT_A0)
Ben Hutchings7e6d06f2012-07-30 15:57:44 +0000126 max_descs += EFX_TSO_MAX_SEGS;
127
128 /* Possibly more for PCIe page boundaries within input fragments */
129 if (PAGE_SIZE > EFX_PAGE_SIZE)
130 max_descs += max_t(unsigned int, MAX_SKB_FRAGS,
131 DIV_ROUND_UP(GSO_MAX_SIZE, EFX_PAGE_SIZE));
132
133 return max_descs;
134}
135
Ben Hutchings14bf7182012-05-22 01:27:58 +0100136/* Get partner of a TX queue, seen as part of the same net core queue */
137static struct efx_tx_queue *efx_tx_queue_partner(struct efx_tx_queue *tx_queue)
138{
139 if (tx_queue->queue & EFX_TXQ_TYPE_OFFLOAD)
140 return tx_queue - EFX_TXQ_TYPE_OFFLOAD;
141 else
142 return tx_queue + EFX_TXQ_TYPE_OFFLOAD;
143}
144
145static void efx_tx_maybe_stop_queue(struct efx_tx_queue *txq1)
146{
147 /* We need to consider both queues that the net core sees as one */
148 struct efx_tx_queue *txq2 = efx_tx_queue_partner(txq1);
149 struct efx_nic *efx = txq1->efx;
150 unsigned int fill_level;
151
152 fill_level = max(txq1->insert_count - txq1->old_read_count,
153 txq2->insert_count - txq2->old_read_count);
154 if (likely(fill_level < efx->txq_stop_thresh))
155 return;
156
157 /* We used the stale old_read_count above, which gives us a
158 * pessimistic estimate of the fill level (which may even
159 * validly be >= efx->txq_entries). Now try again using
160 * read_count (more likely to be a cache miss).
161 *
162 * If we read read_count and then conditionally stop the
163 * queue, it is possible for the completion path to race with
164 * us and complete all outstanding descriptors in the middle,
165 * after which there will be no more completions to wake it.
166 * Therefore we stop the queue first, then read read_count
167 * (with a memory barrier to ensure the ordering), then
168 * restart the queue if the fill level turns out to be low
169 * enough.
170 */
171 netif_tx_stop_queue(txq1->core_txq);
172 smp_mb();
173 txq1->old_read_count = ACCESS_ONCE(txq1->read_count);
174 txq2->old_read_count = ACCESS_ONCE(txq2->read_count);
175
176 fill_level = max(txq1->insert_count - txq1->old_read_count,
177 txq2->insert_count - txq2->old_read_count);
178 EFX_BUG_ON_PARANOID(fill_level >= efx->txq_entries);
179 if (likely(fill_level < efx->txq_stop_thresh)) {
180 smp_mb();
181 if (likely(!efx->loopback_selftest))
182 netif_tx_start_queue(txq1->core_txq);
183 }
184}
185
Jon Cooperee45fd92c2013-09-02 18:24:29 +0100186#ifdef EFX_USE_PIO
187
188struct efx_short_copy_buffer {
189 int used;
190 u8 buf[L1_CACHE_BYTES];
191};
192
193/* Copy to PIO, respecting that writes to PIO buffers must be dword aligned.
194 * Advances piobuf pointer. Leaves additional data in the copy buffer.
195 */
196static void efx_memcpy_toio_aligned(struct efx_nic *efx, u8 __iomem **piobuf,
197 u8 *data, int len,
198 struct efx_short_copy_buffer *copy_buf)
199{
200 int block_len = len & ~(sizeof(copy_buf->buf) - 1);
201
202 memcpy_toio(*piobuf, data, block_len);
203 *piobuf += block_len;
204 len -= block_len;
205
206 if (len) {
207 data += block_len;
208 BUG_ON(copy_buf->used);
209 BUG_ON(len > sizeof(copy_buf->buf));
210 memcpy(copy_buf->buf, data, len);
211 copy_buf->used = len;
212 }
213}
214
215/* Copy to PIO, respecting dword alignment, popping data from copy buffer first.
216 * Advances piobuf pointer. Leaves additional data in the copy buffer.
217 */
218static void efx_memcpy_toio_aligned_cb(struct efx_nic *efx, u8 __iomem **piobuf,
219 u8 *data, int len,
220 struct efx_short_copy_buffer *copy_buf)
221{
222 if (copy_buf->used) {
223 /* if the copy buffer is partially full, fill it up and write */
224 int copy_to_buf =
225 min_t(int, sizeof(copy_buf->buf) - copy_buf->used, len);
226
227 memcpy(copy_buf->buf + copy_buf->used, data, copy_to_buf);
228 copy_buf->used += copy_to_buf;
229
230 /* if we didn't fill it up then we're done for now */
231 if (copy_buf->used < sizeof(copy_buf->buf))
232 return;
233
234 memcpy_toio(*piobuf, copy_buf->buf, sizeof(copy_buf->buf));
235 *piobuf += sizeof(copy_buf->buf);
236 data += copy_to_buf;
237 len -= copy_to_buf;
238 copy_buf->used = 0;
239 }
240
241 efx_memcpy_toio_aligned(efx, piobuf, data, len, copy_buf);
242}
243
244static void efx_flush_copy_buffer(struct efx_nic *efx, u8 __iomem *piobuf,
245 struct efx_short_copy_buffer *copy_buf)
246{
247 /* if there's anything in it, write the whole buffer, including junk */
248 if (copy_buf->used)
249 memcpy_toio(piobuf, copy_buf->buf, sizeof(copy_buf->buf));
250}
251
252/* Traverse skb structure and copy fragments in to PIO buffer.
253 * Advances piobuf pointer.
254 */
255static void efx_skb_copy_bits_to_pio(struct efx_nic *efx, struct sk_buff *skb,
256 u8 __iomem **piobuf,
257 struct efx_short_copy_buffer *copy_buf)
258{
259 int i;
260
261 efx_memcpy_toio_aligned(efx, piobuf, skb->data, skb_headlen(skb),
262 copy_buf);
263
264 for (i = 0; i < skb_shinfo(skb)->nr_frags; ++i) {
265 skb_frag_t *f = &skb_shinfo(skb)->frags[i];
266 u8 *vaddr;
267
268 vaddr = kmap_atomic(skb_frag_page(f));
269
270 efx_memcpy_toio_aligned_cb(efx, piobuf, vaddr + f->page_offset,
271 skb_frag_size(f), copy_buf);
272 kunmap_atomic(vaddr);
273 }
274
275 EFX_BUG_ON_PARANOID(skb_shinfo(skb)->frag_list);
276}
277
278static struct efx_tx_buffer *
279efx_enqueue_skb_pio(struct efx_tx_queue *tx_queue, struct sk_buff *skb)
280{
281 struct efx_tx_buffer *buffer =
282 efx_tx_queue_get_insert_buffer(tx_queue);
283 u8 __iomem *piobuf = tx_queue->piobuf;
284
285 /* Copy to PIO buffer. Ensure the writes are padded to the end
286 * of a cache line, as this is required for write-combining to be
287 * effective on at least x86.
288 */
289
290 if (skb_shinfo(skb)->nr_frags) {
291 /* The size of the copy buffer will ensure all writes
292 * are the size of a cache line.
293 */
294 struct efx_short_copy_buffer copy_buf;
295
296 copy_buf.used = 0;
297
298 efx_skb_copy_bits_to_pio(tx_queue->efx, skb,
299 &piobuf, &copy_buf);
300 efx_flush_copy_buffer(tx_queue->efx, piobuf, &copy_buf);
301 } else {
302 /* Pad the write to the size of a cache line.
303 * We can do this because we know the skb_shared_info sruct is
304 * after the source, and the destination buffer is big enough.
305 */
306 BUILD_BUG_ON(L1_CACHE_BYTES >
307 SKB_DATA_ALIGN(sizeof(struct skb_shared_info)));
308 memcpy_toio(tx_queue->piobuf, skb->data,
309 ALIGN(skb->len, L1_CACHE_BYTES));
310 }
311
312 EFX_POPULATE_QWORD_5(buffer->option,
313 ESF_DZ_TX_DESC_IS_OPT, 1,
314 ESF_DZ_TX_OPTION_TYPE, ESE_DZ_TX_OPTION_DESC_PIO,
315 ESF_DZ_TX_PIO_CONT, 0,
316 ESF_DZ_TX_PIO_BYTE_CNT, skb->len,
317 ESF_DZ_TX_PIO_BUF_ADDR,
318 tx_queue->piobuf_offset);
319 ++tx_queue->pio_packets;
320 ++tx_queue->insert_count;
321 return buffer;
322}
323#endif /* EFX_USE_PIO */
324
Ben Hutchings8ceee662008-04-27 12:55:59 +0100325/*
326 * Add a socket buffer to a TX queue
327 *
328 * This maps all fragments of a socket buffer for DMA and adds them to
329 * the TX queue. The queue's insert pointer will be incremented by
330 * the number of fragments in the socket buffer.
331 *
332 * If any DMA mapping fails, any mapped fragments will be unmapped,
333 * the queue's insert pointer will be restored to its original value.
334 *
Ben Hutchings497f5ba2009-11-23 16:07:05 +0000335 * This function is split out from efx_hard_start_xmit to allow the
336 * loopback test to direct packets via specific TX queues.
337 *
Ben Hutchings14bf7182012-05-22 01:27:58 +0100338 * Returns NETDEV_TX_OK.
Ben Hutchings8ceee662008-04-27 12:55:59 +0100339 * You must hold netif_tx_lock() to call this function.
340 */
Ben Hutchings497f5ba2009-11-23 16:07:05 +0000341netdev_tx_t efx_enqueue_skb(struct efx_tx_queue *tx_queue, struct sk_buff *skb)
Ben Hutchings8ceee662008-04-27 12:55:59 +0100342{
343 struct efx_nic *efx = tx_queue->efx;
Ben Hutchings0e33d872012-05-17 17:46:55 +0100344 struct device *dma_dev = &efx->pci_dev->dev;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100345 struct efx_tx_buffer *buffer;
346 skb_frag_t *fragment;
Ben Hutchings0fe55652013-06-28 21:47:15 +0100347 unsigned int len, unmap_len = 0;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100348 dma_addr_t dma_addr, unmap_addr = 0;
349 unsigned int dma_len;
Ben Hutchings7668ff92012-05-17 20:52:20 +0100350 unsigned short dma_flags;
Ben Hutchings14bf7182012-05-22 01:27:58 +0100351 int i = 0;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100352
353 EFX_BUG_ON_PARANOID(tx_queue->write_count != tx_queue->insert_count);
354
Ben Hutchings9bc183d2009-11-23 16:06:47 +0000355 if (skb_shinfo(skb)->gso_size)
Ben Hutchingsb9b39b62008-05-07 12:51:12 +0100356 return efx_enqueue_skb_tso(tx_queue, skb);
357
Ben Hutchings8ceee662008-04-27 12:55:59 +0100358 /* Get size of the initial fragment */
359 len = skb_headlen(skb);
360
Ben Hutchingsbb145a92009-03-20 13:25:39 +0000361 /* Pad if necessary */
362 if (EFX_WORKAROUND_15592(efx) && skb->len <= 32) {
363 EFX_BUG_ON_PARANOID(skb->data_len);
364 len = 32 + 1;
365 if (skb_pad(skb, len - skb->len))
366 return NETDEV_TX_OK;
367 }
368
Jon Cooperee45fd92c2013-09-02 18:24:29 +0100369 /* Consider using PIO for short packets */
370#ifdef EFX_USE_PIO
371 if (skb->len <= efx_piobuf_size && tx_queue->piobuf &&
372 efx_nic_tx_is_empty(tx_queue) &&
373 efx_nic_tx_is_empty(efx_tx_queue_partner(tx_queue))) {
374 buffer = efx_enqueue_skb_pio(tx_queue, skb);
375 dma_flags = EFX_TX_BUF_OPTION;
376 goto finish_packet;
377 }
378#endif
379
Ben Hutchings0e33d872012-05-17 17:46:55 +0100380 /* Map for DMA. Use dma_map_single rather than dma_map_page
Ben Hutchings8ceee662008-04-27 12:55:59 +0100381 * since this is more efficient on machines with sparse
382 * memory.
383 */
Ben Hutchings7668ff92012-05-17 20:52:20 +0100384 dma_flags = EFX_TX_BUF_MAP_SINGLE;
Ben Hutchings0e33d872012-05-17 17:46:55 +0100385 dma_addr = dma_map_single(dma_dev, skb->data, len, PCI_DMA_TODEVICE);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100386
387 /* Process all fragments */
388 while (1) {
Ben Hutchings0e33d872012-05-17 17:46:55 +0100389 if (unlikely(dma_mapping_error(dma_dev, dma_addr)))
390 goto dma_err;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100391
392 /* Store fields for marking in the per-fragment final
393 * descriptor */
394 unmap_len = len;
395 unmap_addr = dma_addr;
396
397 /* Add to TX queue, splitting across DMA boundaries */
398 do {
Ben Hutchings0fe55652013-06-28 21:47:15 +0100399 buffer = efx_tx_queue_get_insert_buffer(tx_queue);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100400
Ben Hutchings63f19882009-10-23 08:31:20 +0000401 dma_len = efx_max_tx_len(efx, dma_addr);
402 if (likely(dma_len >= len))
Ben Hutchings8ceee662008-04-27 12:55:59 +0100403 dma_len = len;
404
Ben Hutchings8ceee662008-04-27 12:55:59 +0100405 /* Fill out per descriptor fields */
406 buffer->len = dma_len;
407 buffer->dma_addr = dma_addr;
Ben Hutchings7668ff92012-05-17 20:52:20 +0100408 buffer->flags = EFX_TX_BUF_CONT;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100409 len -= dma_len;
410 dma_addr += dma_len;
411 ++tx_queue->insert_count;
412 } while (len);
413
414 /* Transfer ownership of the unmapping to the final buffer */
Ben Hutchings7668ff92012-05-17 20:52:20 +0100415 buffer->flags = EFX_TX_BUF_CONT | dma_flags;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100416 buffer->unmap_len = unmap_len;
417 unmap_len = 0;
418
419 /* Get address and size of next fragment */
420 if (i >= skb_shinfo(skb)->nr_frags)
421 break;
422 fragment = &skb_shinfo(skb)->frags[i];
Eric Dumazet9e903e02011-10-18 21:00:24 +0000423 len = skb_frag_size(fragment);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100424 i++;
425 /* Map for DMA */
Ben Hutchings7668ff92012-05-17 20:52:20 +0100426 dma_flags = 0;
Ben Hutchings0e33d872012-05-17 17:46:55 +0100427 dma_addr = skb_frag_dma_map(dma_dev, fragment, 0, len,
Ian Campbell5d6bcdf2011-10-06 11:10:48 +0100428 DMA_TO_DEVICE);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100429 }
430
431 /* Transfer ownership of the skb to the final buffer */
Jon Cooperee45fd92c2013-09-02 18:24:29 +0100432finish_packet:
Ben Hutchings8ceee662008-04-27 12:55:59 +0100433 buffer->skb = skb;
Ben Hutchings7668ff92012-05-17 20:52:20 +0100434 buffer->flags = EFX_TX_BUF_SKB | dma_flags;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100435
Tom Herbertc3940992011-11-28 16:33:43 +0000436 netdev_tx_sent_queue(tx_queue->core_txq, skb->len);
437
Ben Hutchings8ceee662008-04-27 12:55:59 +0100438 /* Pass off to hardware */
Ben Hutchings152b6a62009-11-29 03:43:56 +0000439 efx_nic_push_buffers(tx_queue);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100440
Ben Hutchings14bf7182012-05-22 01:27:58 +0100441 efx_tx_maybe_stop_queue(tx_queue);
442
Ben Hutchings8ceee662008-04-27 12:55:59 +0100443 return NETDEV_TX_OK;
444
Ben Hutchings0e33d872012-05-17 17:46:55 +0100445 dma_err:
Ben Hutchings62776d02010-06-23 11:30:07 +0000446 netif_err(efx, tx_err, efx->net_dev,
447 " TX queue %d could not map skb with %d bytes %d "
448 "fragments for DMA\n", tx_queue->queue, skb->len,
449 skb_shinfo(skb)->nr_frags + 1);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100450
451 /* Mark the packet as transmitted, and free the SKB ourselves */
Ben Hutchings9bc183d2009-11-23 16:06:47 +0000452 dev_kfree_skb_any(skb);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100453
Ben Hutchings8ceee662008-04-27 12:55:59 +0100454 /* Work backwards until we hit the original insert pointer value */
455 while (tx_queue->insert_count != tx_queue->write_count) {
Tom Herbertc3940992011-11-28 16:33:43 +0000456 unsigned int pkts_compl = 0, bytes_compl = 0;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100457 --tx_queue->insert_count;
Ben Hutchings0fe55652013-06-28 21:47:15 +0100458 buffer = __efx_tx_queue_get_insert_buffer(tx_queue);
Tom Herbertc3940992011-11-28 16:33:43 +0000459 efx_dequeue_buffer(tx_queue, buffer, &pkts_compl, &bytes_compl);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100460 }
461
462 /* Free the fragment we were mid-way through pushing */
Ben Hutchingsecbd95c2008-09-01 12:46:40 +0100463 if (unmap_len) {
Ben Hutchings7668ff92012-05-17 20:52:20 +0100464 if (dma_flags & EFX_TX_BUF_MAP_SINGLE)
Ben Hutchings0e33d872012-05-17 17:46:55 +0100465 dma_unmap_single(dma_dev, unmap_addr, unmap_len,
466 DMA_TO_DEVICE);
Ben Hutchingsecbd95c2008-09-01 12:46:40 +0100467 else
Ben Hutchings0e33d872012-05-17 17:46:55 +0100468 dma_unmap_page(dma_dev, unmap_addr, unmap_len,
469 DMA_TO_DEVICE);
Ben Hutchingsecbd95c2008-09-01 12:46:40 +0100470 }
Ben Hutchings8ceee662008-04-27 12:55:59 +0100471
Ben Hutchings14bf7182012-05-22 01:27:58 +0100472 return NETDEV_TX_OK;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100473}
474
475/* Remove packets from the TX queue
476 *
477 * This removes packets from the TX queue, up to and including the
478 * specified index.
479 */
Ben Hutchings4d566062008-09-01 12:47:12 +0100480static void efx_dequeue_buffers(struct efx_tx_queue *tx_queue,
Tom Herbertc3940992011-11-28 16:33:43 +0000481 unsigned int index,
482 unsigned int *pkts_compl,
483 unsigned int *bytes_compl)
Ben Hutchings8ceee662008-04-27 12:55:59 +0100484{
485 struct efx_nic *efx = tx_queue->efx;
486 unsigned int stop_index, read_ptr;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100487
Steve Hodgsonecc910f2010-09-10 06:42:22 +0000488 stop_index = (index + 1) & tx_queue->ptr_mask;
489 read_ptr = tx_queue->read_count & tx_queue->ptr_mask;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100490
491 while (read_ptr != stop_index) {
492 struct efx_tx_buffer *buffer = &tx_queue->buffer[read_ptr];
Ben Hutchingsba8977b2013-01-08 23:43:19 +0000493
494 if (!(buffer->flags & EFX_TX_BUF_OPTION) &&
495 unlikely(buffer->len == 0)) {
Ben Hutchings62776d02010-06-23 11:30:07 +0000496 netif_err(efx, tx_err, efx->net_dev,
497 "TX queue %d spurious TX completion id %x\n",
498 tx_queue->queue, read_ptr);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100499 efx_schedule_reset(efx, RESET_TYPE_TX_SKIP);
500 return;
501 }
502
Tom Herbertc3940992011-11-28 16:33:43 +0000503 efx_dequeue_buffer(tx_queue, buffer, pkts_compl, bytes_compl);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100504
505 ++tx_queue->read_count;
Steve Hodgsonecc910f2010-09-10 06:42:22 +0000506 read_ptr = tx_queue->read_count & tx_queue->ptr_mask;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100507 }
508}
509
Ben Hutchings8ceee662008-04-27 12:55:59 +0100510/* Initiate a packet transmission. We use one channel per CPU
511 * (sharing when we have more CPUs than channels). On Falcon, the TX
512 * completion events will be directed back to the CPU that transmitted
513 * the packet, which should be cache-efficient.
514 *
515 * Context: non-blocking.
516 * Note that returning anything other than NETDEV_TX_OK will cause the
517 * OS to free the skb.
518 */
Stephen Hemminger613573252009-08-31 19:50:58 +0000519netdev_tx_t efx_hard_start_xmit(struct sk_buff *skb,
Ben Hutchings2d0cc562012-02-17 00:10:45 +0000520 struct net_device *net_dev)
Ben Hutchings8ceee662008-04-27 12:55:59 +0100521{
Ben Hutchings767e4682008-09-01 12:43:14 +0100522 struct efx_nic *efx = netdev_priv(net_dev);
Ben Hutchings60ac1062008-09-01 12:44:59 +0100523 struct efx_tx_queue *tx_queue;
Ben Hutchings94b274b2011-01-10 21:18:20 +0000524 unsigned index, type;
Ben Hutchings60ac1062008-09-01 12:44:59 +0100525
Ben Hutchingse4abce82011-05-16 18:51:24 +0100526 EFX_WARN_ON_PARANOID(!netif_device_present(net_dev));
Ben Hutchingsa7ef5932009-03-04 09:52:37 +0000527
Stuart Hodgson7c236c42012-09-03 11:09:36 +0100528 /* PTP "event" packet */
529 if (unlikely(efx_xmit_with_hwtstamp(skb)) &&
530 unlikely(efx_ptp_is_ptp_tx(efx, skb))) {
531 return efx_ptp_tx(efx, skb);
532 }
533
Ben Hutchings94b274b2011-01-10 21:18:20 +0000534 index = skb_get_queue_mapping(skb);
535 type = skb->ip_summed == CHECKSUM_PARTIAL ? EFX_TXQ_TYPE_OFFLOAD : 0;
536 if (index >= efx->n_tx_channels) {
537 index -= efx->n_tx_channels;
538 type |= EFX_TXQ_TYPE_HIGHPRI;
539 }
540 tx_queue = efx_get_tx_queue(efx, index, type);
Ben Hutchings60ac1062008-09-01 12:44:59 +0100541
Ben Hutchings497f5ba2009-11-23 16:07:05 +0000542 return efx_enqueue_skb(tx_queue, skb);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100543}
544
Ben Hutchings60031fc2011-01-12 18:39:40 +0000545void efx_init_tx_queue_core_txq(struct efx_tx_queue *tx_queue)
546{
Ben Hutchings94b274b2011-01-10 21:18:20 +0000547 struct efx_nic *efx = tx_queue->efx;
548
Ben Hutchings60031fc2011-01-12 18:39:40 +0000549 /* Must be inverse of queue lookup in efx_hard_start_xmit() */
Ben Hutchings94b274b2011-01-10 21:18:20 +0000550 tx_queue->core_txq =
551 netdev_get_tx_queue(efx->net_dev,
552 tx_queue->queue / EFX_TXQ_TYPES +
553 ((tx_queue->queue & EFX_TXQ_TYPE_HIGHPRI) ?
554 efx->n_tx_channels : 0));
555}
556
557int efx_setup_tc(struct net_device *net_dev, u8 num_tc)
558{
559 struct efx_nic *efx = netdev_priv(net_dev);
560 struct efx_channel *channel;
561 struct efx_tx_queue *tx_queue;
562 unsigned tc;
563 int rc;
564
565 if (efx_nic_rev(efx) < EFX_REV_FALCON_B0 || num_tc > EFX_MAX_TX_TC)
566 return -EINVAL;
567
568 if (num_tc == net_dev->num_tc)
569 return 0;
570
571 for (tc = 0; tc < num_tc; tc++) {
572 net_dev->tc_to_txq[tc].offset = tc * efx->n_tx_channels;
573 net_dev->tc_to_txq[tc].count = efx->n_tx_channels;
574 }
575
576 if (num_tc > net_dev->num_tc) {
577 /* Initialise high-priority queues as necessary */
578 efx_for_each_channel(channel, efx) {
579 efx_for_each_possible_channel_tx_queue(tx_queue,
580 channel) {
581 if (!(tx_queue->queue & EFX_TXQ_TYPE_HIGHPRI))
582 continue;
583 if (!tx_queue->buffer) {
584 rc = efx_probe_tx_queue(tx_queue);
585 if (rc)
586 return rc;
587 }
588 if (!tx_queue->initialised)
589 efx_init_tx_queue(tx_queue);
590 efx_init_tx_queue_core_txq(tx_queue);
591 }
592 }
593 } else {
594 /* Reduce number of classes before number of queues */
595 net_dev->num_tc = num_tc;
596 }
597
598 rc = netif_set_real_num_tx_queues(net_dev,
599 max_t(int, num_tc, 1) *
600 efx->n_tx_channels);
601 if (rc)
602 return rc;
603
604 /* Do not destroy high-priority queues when they become
605 * unused. We would have to flush them first, and it is
606 * fairly difficult to flush a subset of TX queues. Leave
607 * it to efx_fini_channels().
608 */
609
610 net_dev->num_tc = num_tc;
611 return 0;
Ben Hutchings60031fc2011-01-12 18:39:40 +0000612}
613
Ben Hutchings8ceee662008-04-27 12:55:59 +0100614void efx_xmit_done(struct efx_tx_queue *tx_queue, unsigned int index)
615{
616 unsigned fill_level;
617 struct efx_nic *efx = tx_queue->efx;
Ben Hutchings14bf7182012-05-22 01:27:58 +0100618 struct efx_tx_queue *txq2;
Tom Herbertc3940992011-11-28 16:33:43 +0000619 unsigned int pkts_compl = 0, bytes_compl = 0;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100620
Steve Hodgsonecc910f2010-09-10 06:42:22 +0000621 EFX_BUG_ON_PARANOID(index > tx_queue->ptr_mask);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100622
Tom Herbertc3940992011-11-28 16:33:43 +0000623 efx_dequeue_buffers(tx_queue, index, &pkts_compl, &bytes_compl);
624 netdev_tx_completed_queue(tx_queue->core_txq, pkts_compl, bytes_compl);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100625
Ben Hutchings02e12162013-04-27 01:55:21 +0100626 if (pkts_compl > 1)
627 ++tx_queue->merge_events;
628
Ben Hutchings14bf7182012-05-22 01:27:58 +0100629 /* See if we need to restart the netif queue. This memory
630 * barrier ensures that we write read_count (inside
631 * efx_dequeue_buffers()) before reading the queue status.
632 */
Ben Hutchings8ceee662008-04-27 12:55:59 +0100633 smp_mb();
Ben Hutchingsc04bfc62010-12-10 01:24:16 +0000634 if (unlikely(netif_tx_queue_stopped(tx_queue->core_txq)) &&
Neil Turton9d1aea62011-04-04 13:46:23 +0100635 likely(efx->port_enabled) &&
Ben Hutchingse4abce82011-05-16 18:51:24 +0100636 likely(netif_device_present(efx->net_dev))) {
Ben Hutchings14bf7182012-05-22 01:27:58 +0100637 txq2 = efx_tx_queue_partner(tx_queue);
638 fill_level = max(tx_queue->insert_count - tx_queue->read_count,
639 txq2->insert_count - txq2->read_count);
640 if (fill_level <= efx->txq_wake_thresh)
Ben Hutchingsc04bfc62010-12-10 01:24:16 +0000641 netif_tx_wake_queue(tx_queue->core_txq);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100642 }
Ben Hutchingscd385572010-11-15 23:53:11 +0000643
644 /* Check whether the hardware queue is now empty */
645 if ((int)(tx_queue->read_count - tx_queue->old_write_count) >= 0) {
646 tx_queue->old_write_count = ACCESS_ONCE(tx_queue->write_count);
647 if (tx_queue->read_count == tx_queue->old_write_count) {
648 smp_mb();
649 tx_queue->empty_read_count =
650 tx_queue->read_count | EFX_EMPTY_COUNT_VALID;
651 }
652 }
Ben Hutchings8ceee662008-04-27 12:55:59 +0100653}
654
Ben Hutchingsf7251a92012-05-17 18:40:54 +0100655/* Size of page-based TSO header buffers. Larger blocks must be
656 * allocated from the heap.
657 */
658#define TSOH_STD_SIZE 128
659#define TSOH_PER_PAGE (PAGE_SIZE / TSOH_STD_SIZE)
660
661/* At most half the descriptors in the queue at any time will refer to
662 * a TSO header buffer, since they must always be followed by a
663 * payload descriptor referring to an skb.
664 */
665static unsigned int efx_tsoh_page_count(struct efx_tx_queue *tx_queue)
666{
667 return DIV_ROUND_UP(tx_queue->ptr_mask + 1, 2 * TSOH_PER_PAGE);
668}
669
Ben Hutchings8ceee662008-04-27 12:55:59 +0100670int efx_probe_tx_queue(struct efx_tx_queue *tx_queue)
671{
672 struct efx_nic *efx = tx_queue->efx;
Steve Hodgsonecc910f2010-09-10 06:42:22 +0000673 unsigned int entries;
Ben Hutchings7668ff92012-05-17 20:52:20 +0100674 int rc;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100675
Steve Hodgsonecc910f2010-09-10 06:42:22 +0000676 /* Create the smallest power-of-two aligned ring */
677 entries = max(roundup_pow_of_two(efx->txq_entries), EFX_MIN_DMAQ_SIZE);
678 EFX_BUG_ON_PARANOID(entries > EFX_MAX_DMAQ_SIZE);
679 tx_queue->ptr_mask = entries - 1;
680
681 netif_dbg(efx, probe, efx->net_dev,
682 "creating TX queue %d size %#x mask %#x\n",
683 tx_queue->queue, efx->txq_entries, tx_queue->ptr_mask);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100684
685 /* Allocate software ring */
Thomas Meyerc2e4e252011-12-02 12:36:13 +0000686 tx_queue->buffer = kcalloc(entries, sizeof(*tx_queue->buffer),
Steve Hodgsonecc910f2010-09-10 06:42:22 +0000687 GFP_KERNEL);
Ben Hutchings60ac1062008-09-01 12:44:59 +0100688 if (!tx_queue->buffer)
689 return -ENOMEM;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100690
Ben Hutchingsf7251a92012-05-17 18:40:54 +0100691 if (tx_queue->queue & EFX_TXQ_TYPE_OFFLOAD) {
692 tx_queue->tsoh_page =
693 kcalloc(efx_tsoh_page_count(tx_queue),
694 sizeof(tx_queue->tsoh_page[0]), GFP_KERNEL);
695 if (!tx_queue->tsoh_page) {
696 rc = -ENOMEM;
697 goto fail1;
698 }
699 }
700
Ben Hutchings8ceee662008-04-27 12:55:59 +0100701 /* Allocate hardware ring */
Ben Hutchings152b6a62009-11-29 03:43:56 +0000702 rc = efx_nic_probe_tx(tx_queue);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100703 if (rc)
Ben Hutchingsf7251a92012-05-17 18:40:54 +0100704 goto fail2;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100705
706 return 0;
707
Ben Hutchingsf7251a92012-05-17 18:40:54 +0100708fail2:
709 kfree(tx_queue->tsoh_page);
710 tx_queue->tsoh_page = NULL;
711fail1:
Ben Hutchings8ceee662008-04-27 12:55:59 +0100712 kfree(tx_queue->buffer);
713 tx_queue->buffer = NULL;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100714 return rc;
715}
716
Ben Hutchingsbc3c90a2008-09-01 12:48:46 +0100717void efx_init_tx_queue(struct efx_tx_queue *tx_queue)
Ben Hutchings8ceee662008-04-27 12:55:59 +0100718{
Ben Hutchings62776d02010-06-23 11:30:07 +0000719 netif_dbg(tx_queue->efx, drv, tx_queue->efx->net_dev,
720 "initialising TX queue %d\n", tx_queue->queue);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100721
722 tx_queue->insert_count = 0;
723 tx_queue->write_count = 0;
Ben Hutchingscd385572010-11-15 23:53:11 +0000724 tx_queue->old_write_count = 0;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100725 tx_queue->read_count = 0;
726 tx_queue->old_read_count = 0;
Ben Hutchingscd385572010-11-15 23:53:11 +0000727 tx_queue->empty_read_count = 0 | EFX_EMPTY_COUNT_VALID;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100728
729 /* Set up TX descriptor ring */
Ben Hutchings152b6a62009-11-29 03:43:56 +0000730 efx_nic_init_tx(tx_queue);
Ben Hutchings94b274b2011-01-10 21:18:20 +0000731
732 tx_queue->initialised = true;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100733}
734
Ben Hutchingse42c3d82013-05-27 16:52:54 +0100735void efx_fini_tx_queue(struct efx_tx_queue *tx_queue)
Ben Hutchings8ceee662008-04-27 12:55:59 +0100736{
737 struct efx_tx_buffer *buffer;
738
Ben Hutchingse42c3d82013-05-27 16:52:54 +0100739 netif_dbg(tx_queue->efx, drv, tx_queue->efx->net_dev,
740 "shutting down TX queue %d\n", tx_queue->queue);
741
Ben Hutchings8ceee662008-04-27 12:55:59 +0100742 if (!tx_queue->buffer)
743 return;
744
745 /* Free any buffers left in the ring */
746 while (tx_queue->read_count != tx_queue->write_count) {
Tom Herbertc3940992011-11-28 16:33:43 +0000747 unsigned int pkts_compl = 0, bytes_compl = 0;
Steve Hodgsonecc910f2010-09-10 06:42:22 +0000748 buffer = &tx_queue->buffer[tx_queue->read_count & tx_queue->ptr_mask];
Tom Herbertc3940992011-11-28 16:33:43 +0000749 efx_dequeue_buffer(tx_queue, buffer, &pkts_compl, &bytes_compl);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100750
751 ++tx_queue->read_count;
752 }
Tom Herbertc3940992011-11-28 16:33:43 +0000753 netdev_tx_reset_queue(tx_queue->core_txq);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100754}
755
Ben Hutchings8ceee662008-04-27 12:55:59 +0100756void efx_remove_tx_queue(struct efx_tx_queue *tx_queue)
757{
Ben Hutchingsf7251a92012-05-17 18:40:54 +0100758 int i;
759
Ben Hutchings94b274b2011-01-10 21:18:20 +0000760 if (!tx_queue->buffer)
761 return;
762
Ben Hutchings62776d02010-06-23 11:30:07 +0000763 netif_dbg(tx_queue->efx, drv, tx_queue->efx->net_dev,
764 "destroying TX queue %d\n", tx_queue->queue);
Ben Hutchings152b6a62009-11-29 03:43:56 +0000765 efx_nic_remove_tx(tx_queue);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100766
Ben Hutchingsf7251a92012-05-17 18:40:54 +0100767 if (tx_queue->tsoh_page) {
768 for (i = 0; i < efx_tsoh_page_count(tx_queue); i++)
769 efx_nic_free_buffer(tx_queue->efx,
770 &tx_queue->tsoh_page[i]);
771 kfree(tx_queue->tsoh_page);
772 tx_queue->tsoh_page = NULL;
773 }
774
Ben Hutchings8ceee662008-04-27 12:55:59 +0100775 kfree(tx_queue->buffer);
776 tx_queue->buffer = NULL;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100777}
778
779
Ben Hutchingsb9b39b62008-05-07 12:51:12 +0100780/* Efx TCP segmentation acceleration.
781 *
782 * Why? Because by doing it here in the driver we can go significantly
783 * faster than the GSO.
784 *
785 * Requires TX checksum offload support.
786 */
787
788/* Number of bytes inserted at the start of a TSO header buffer,
789 * similar to NET_IP_ALIGN.
790 */
Ben Hutchings13e9ab12008-09-01 12:50:28 +0100791#ifdef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
Ben Hutchingsb9b39b62008-05-07 12:51:12 +0100792#define TSOH_OFFSET 0
793#else
794#define TSOH_OFFSET NET_IP_ALIGN
795#endif
796
Ben Hutchingsb9b39b62008-05-07 12:51:12 +0100797#define PTR_DIFF(p1, p2) ((u8 *)(p1) - (u8 *)(p2))
Ben Hutchingsb9b39b62008-05-07 12:51:12 +0100798
799/**
800 * struct tso_state - TSO state for an SKB
Ben Hutchings23d9e602008-09-01 12:47:02 +0100801 * @out_len: Remaining length in current segment
Ben Hutchingsb9b39b62008-05-07 12:51:12 +0100802 * @seqnum: Current sequence number
Ben Hutchings23d9e602008-09-01 12:47:02 +0100803 * @ipv4_id: Current IPv4 ID, host endian
Ben Hutchingsb9b39b62008-05-07 12:51:12 +0100804 * @packet_space: Remaining space in current packet
Ben Hutchings23d9e602008-09-01 12:47:02 +0100805 * @dma_addr: DMA address of current position
806 * @in_len: Remaining length in current SKB fragment
807 * @unmap_len: Length of SKB fragment
808 * @unmap_addr: DMA address of SKB fragment
Ben Hutchings7668ff92012-05-17 20:52:20 +0100809 * @dma_flags: TX buffer flags for DMA mapping - %EFX_TX_BUF_MAP_SINGLE or 0
Ben Hutchings738a8f42009-11-29 15:16:05 +0000810 * @protocol: Network protocol (after any VLAN header)
Ben Hutchings97142842012-06-22 02:44:01 +0100811 * @ip_off: Offset of IP header
812 * @tcp_off: Offset of TCP header
Ben Hutchings23d9e602008-09-01 12:47:02 +0100813 * @header_len: Number of bytes of header
Ben Hutchings53cb13c2012-06-19 20:03:41 +0100814 * @ip_base_len: IPv4 tot_len or IPv6 payload_len, before TCP payload
Ben Hutchingsdfa50be2013-03-08 21:20:09 +0000815 * @header_dma_addr: Header DMA address, when using option descriptors
816 * @header_unmap_len: Header DMA mapped length, or 0 if not using option
817 * descriptors
Ben Hutchingsb9b39b62008-05-07 12:51:12 +0100818 *
819 * The state used during segmentation. It is put into this data structure
820 * just to make it easy to pass into inline functions.
821 */
822struct tso_state {
Ben Hutchings23d9e602008-09-01 12:47:02 +0100823 /* Output position */
824 unsigned out_len;
Ben Hutchingsb9b39b62008-05-07 12:51:12 +0100825 unsigned seqnum;
Ben Hutchingsdfa50be2013-03-08 21:20:09 +0000826 u16 ipv4_id;
Ben Hutchingsb9b39b62008-05-07 12:51:12 +0100827 unsigned packet_space;
828
Ben Hutchings23d9e602008-09-01 12:47:02 +0100829 /* Input position */
830 dma_addr_t dma_addr;
831 unsigned in_len;
832 unsigned unmap_len;
833 dma_addr_t unmap_addr;
Ben Hutchings7668ff92012-05-17 20:52:20 +0100834 unsigned short dma_flags;
Ben Hutchingsb9b39b62008-05-07 12:51:12 +0100835
Ben Hutchings738a8f42009-11-29 15:16:05 +0000836 __be16 protocol;
Ben Hutchings97142842012-06-22 02:44:01 +0100837 unsigned int ip_off;
838 unsigned int tcp_off;
Ben Hutchings23d9e602008-09-01 12:47:02 +0100839 unsigned header_len;
Ben Hutchings53cb13c2012-06-19 20:03:41 +0100840 unsigned int ip_base_len;
Ben Hutchingsdfa50be2013-03-08 21:20:09 +0000841 dma_addr_t header_dma_addr;
842 unsigned int header_unmap_len;
Ben Hutchingsb9b39b62008-05-07 12:51:12 +0100843};
844
845
846/*
847 * Verify that our various assumptions about sk_buffs and the conditions
Ben Hutchings738a8f42009-11-29 15:16:05 +0000848 * under which TSO will be attempted hold true. Return the protocol number.
Ben Hutchingsb9b39b62008-05-07 12:51:12 +0100849 */
Ben Hutchings738a8f42009-11-29 15:16:05 +0000850static __be16 efx_tso_check_protocol(struct sk_buff *skb)
Ben Hutchingsb9b39b62008-05-07 12:51:12 +0100851{
Ben Hutchings740847d2008-09-01 12:48:23 +0100852 __be16 protocol = skb->protocol;
853
Ben Hutchingsb9b39b62008-05-07 12:51:12 +0100854 EFX_BUG_ON_PARANOID(((struct ethhdr *)skb->data)->h_proto !=
Ben Hutchings740847d2008-09-01 12:48:23 +0100855 protocol);
856 if (protocol == htons(ETH_P_8021Q)) {
Ben Hutchings740847d2008-09-01 12:48:23 +0100857 struct vlan_ethhdr *veh = (struct vlan_ethhdr *)skb->data;
858 protocol = veh->h_vlan_encapsulated_proto;
Ben Hutchings740847d2008-09-01 12:48:23 +0100859 }
860
Ben Hutchings738a8f42009-11-29 15:16:05 +0000861 if (protocol == htons(ETH_P_IP)) {
862 EFX_BUG_ON_PARANOID(ip_hdr(skb)->protocol != IPPROTO_TCP);
863 } else {
864 EFX_BUG_ON_PARANOID(protocol != htons(ETH_P_IPV6));
865 EFX_BUG_ON_PARANOID(ipv6_hdr(skb)->nexthdr != NEXTHDR_TCP);
866 }
Ben Hutchingsb9b39b62008-05-07 12:51:12 +0100867 EFX_BUG_ON_PARANOID((PTR_DIFF(tcp_hdr(skb), skb->data)
868 + (tcp_hdr(skb)->doff << 2u)) >
869 skb_headlen(skb));
Ben Hutchings738a8f42009-11-29 15:16:05 +0000870
871 return protocol;
Ben Hutchingsb9b39b62008-05-07 12:51:12 +0100872}
873
Ben Hutchingsf7251a92012-05-17 18:40:54 +0100874static u8 *efx_tsoh_get_buffer(struct efx_tx_queue *tx_queue,
875 struct efx_tx_buffer *buffer, unsigned int len)
Ben Hutchingsb9b39b62008-05-07 12:51:12 +0100876{
Ben Hutchingsf7251a92012-05-17 18:40:54 +0100877 u8 *result;
Ben Hutchingsb9b39b62008-05-07 12:51:12 +0100878
Ben Hutchingsf7251a92012-05-17 18:40:54 +0100879 EFX_BUG_ON_PARANOID(buffer->len);
880 EFX_BUG_ON_PARANOID(buffer->flags);
881 EFX_BUG_ON_PARANOID(buffer->unmap_len);
882
883 if (likely(len <= TSOH_STD_SIZE - TSOH_OFFSET)) {
884 unsigned index =
885 (tx_queue->insert_count & tx_queue->ptr_mask) / 2;
886 struct efx_buffer *page_buf =
887 &tx_queue->tsoh_page[index / TSOH_PER_PAGE];
888 unsigned offset =
889 TSOH_STD_SIZE * (index % TSOH_PER_PAGE) + TSOH_OFFSET;
890
891 if (unlikely(!page_buf->addr) &&
Ben Hutchings0d19a542012-09-18 21:59:52 +0100892 efx_nic_alloc_buffer(tx_queue->efx, page_buf, PAGE_SIZE,
893 GFP_ATOMIC))
Ben Hutchingsf7251a92012-05-17 18:40:54 +0100894 return NULL;
895
896 result = (u8 *)page_buf->addr + offset;
897 buffer->dma_addr = page_buf->dma_addr + offset;
898 buffer->flags = EFX_TX_BUF_CONT;
899 } else {
900 tx_queue->tso_long_headers++;
901
902 buffer->heap_buf = kmalloc(TSOH_OFFSET + len, GFP_ATOMIC);
903 if (unlikely(!buffer->heap_buf))
904 return NULL;
905 result = (u8 *)buffer->heap_buf + TSOH_OFFSET;
906 buffer->flags = EFX_TX_BUF_CONT | EFX_TX_BUF_HEAP;
Ben Hutchingsb9b39b62008-05-07 12:51:12 +0100907 }
908
Ben Hutchingsf7251a92012-05-17 18:40:54 +0100909 buffer->len = len;
Ben Hutchingsb9b39b62008-05-07 12:51:12 +0100910
Ben Hutchingsf7251a92012-05-17 18:40:54 +0100911 return result;
Ben Hutchingsb9b39b62008-05-07 12:51:12 +0100912}
913
914/**
915 * efx_tx_queue_insert - push descriptors onto the TX queue
916 * @tx_queue: Efx TX queue
917 * @dma_addr: DMA address of fragment
918 * @len: Length of fragment
Ben Hutchingsecbd95c2008-09-01 12:46:40 +0100919 * @final_buffer: The final buffer inserted into the queue
Ben Hutchingsb9b39b62008-05-07 12:51:12 +0100920 *
Ben Hutchings14bf7182012-05-22 01:27:58 +0100921 * Push descriptors onto the TX queue.
Ben Hutchingsb9b39b62008-05-07 12:51:12 +0100922 */
Ben Hutchings14bf7182012-05-22 01:27:58 +0100923static void efx_tx_queue_insert(struct efx_tx_queue *tx_queue,
924 dma_addr_t dma_addr, unsigned len,
925 struct efx_tx_buffer **final_buffer)
Ben Hutchingsb9b39b62008-05-07 12:51:12 +0100926{
927 struct efx_tx_buffer *buffer;
928 struct efx_nic *efx = tx_queue->efx;
Ben Hutchings0fe55652013-06-28 21:47:15 +0100929 unsigned dma_len;
Ben Hutchingsb9b39b62008-05-07 12:51:12 +0100930
931 EFX_BUG_ON_PARANOID(len <= 0);
932
Ben Hutchingsb9b39b62008-05-07 12:51:12 +0100933 while (1) {
Ben Hutchings0fe55652013-06-28 21:47:15 +0100934 buffer = efx_tx_queue_get_insert_buffer(tx_queue);
Ben Hutchingsb9b39b62008-05-07 12:51:12 +0100935 ++tx_queue->insert_count;
936
937 EFX_BUG_ON_PARANOID(tx_queue->insert_count -
Steve Hodgsonecc910f2010-09-10 06:42:22 +0000938 tx_queue->read_count >=
939 efx->txq_entries);
Ben Hutchingsb9b39b62008-05-07 12:51:12 +0100940
Ben Hutchingsb9b39b62008-05-07 12:51:12 +0100941 buffer->dma_addr = dma_addr;
942
Ben Hutchings63f19882009-10-23 08:31:20 +0000943 dma_len = efx_max_tx_len(efx, dma_addr);
Ben Hutchingsb9b39b62008-05-07 12:51:12 +0100944
945 /* If there is enough space to send then do so */
946 if (dma_len >= len)
947 break;
948
Ben Hutchings7668ff92012-05-17 20:52:20 +0100949 buffer->len = dma_len;
950 buffer->flags = EFX_TX_BUF_CONT;
Ben Hutchingsb9b39b62008-05-07 12:51:12 +0100951 dma_addr += dma_len;
952 len -= dma_len;
953 }
954
955 EFX_BUG_ON_PARANOID(!len);
956 buffer->len = len;
Ben Hutchingsecbd95c2008-09-01 12:46:40 +0100957 *final_buffer = buffer;
Ben Hutchingsb9b39b62008-05-07 12:51:12 +0100958}
959
960
961/*
962 * Put a TSO header into the TX queue.
963 *
964 * This is special-cased because we know that it is small enough to fit in
965 * a single fragment, and we know it doesn't cross a page boundary. It
966 * also allows us to not worry about end-of-packet etc.
967 */
Ben Hutchingsf7251a92012-05-17 18:40:54 +0100968static int efx_tso_put_header(struct efx_tx_queue *tx_queue,
969 struct efx_tx_buffer *buffer, u8 *header)
Ben Hutchingsb9b39b62008-05-07 12:51:12 +0100970{
Ben Hutchingsf7251a92012-05-17 18:40:54 +0100971 if (unlikely(buffer->flags & EFX_TX_BUF_HEAP)) {
972 buffer->dma_addr = dma_map_single(&tx_queue->efx->pci_dev->dev,
973 header, buffer->len,
974 DMA_TO_DEVICE);
975 if (unlikely(dma_mapping_error(&tx_queue->efx->pci_dev->dev,
976 buffer->dma_addr))) {
977 kfree(buffer->heap_buf);
978 buffer->len = 0;
979 buffer->flags = 0;
980 return -ENOMEM;
981 }
982 buffer->unmap_len = buffer->len;
983 buffer->flags |= EFX_TX_BUF_MAP_SINGLE;
984 }
Ben Hutchingsb9b39b62008-05-07 12:51:12 +0100985
986 ++tx_queue->insert_count;
Ben Hutchingsf7251a92012-05-17 18:40:54 +0100987 return 0;
Ben Hutchingsb9b39b62008-05-07 12:51:12 +0100988}
989
990
Ben Hutchingsf7251a92012-05-17 18:40:54 +0100991/* Remove buffers put into a tx_queue. None of the buffers must have
992 * an skb attached.
993 */
Ben Hutchingsb9b39b62008-05-07 12:51:12 +0100994static void efx_enqueue_unwind(struct efx_tx_queue *tx_queue)
995{
996 struct efx_tx_buffer *buffer;
997
998 /* Work backwards until we hit the original insert pointer value */
999 while (tx_queue->insert_count != tx_queue->write_count) {
1000 --tx_queue->insert_count;
Ben Hutchings0fe55652013-06-28 21:47:15 +01001001 buffer = __efx_tx_queue_get_insert_buffer(tx_queue);
Ben Hutchingsf7251a92012-05-17 18:40:54 +01001002 efx_dequeue_buffer(tx_queue, buffer, NULL, NULL);
Ben Hutchingsb9b39b62008-05-07 12:51:12 +01001003 }
1004}
1005
1006
1007/* Parse the SKB header and initialise state. */
Ben Hutchingsc78c39e2013-03-08 20:03:17 +00001008static int tso_start(struct tso_state *st, struct efx_nic *efx,
1009 const struct sk_buff *skb)
Ben Hutchingsb9b39b62008-05-07 12:51:12 +01001010{
Ben Hutchingsdfa50be2013-03-08 21:20:09 +00001011 bool use_options = efx_nic_rev(efx) >= EFX_REV_HUNT_A0;
1012 struct device *dma_dev = &efx->pci_dev->dev;
Ben Hutchingsc78c39e2013-03-08 20:03:17 +00001013 unsigned int header_len, in_len;
Ben Hutchingsdfa50be2013-03-08 21:20:09 +00001014 dma_addr_t dma_addr;
Ben Hutchingsc78c39e2013-03-08 20:03:17 +00001015
Ben Hutchings97142842012-06-22 02:44:01 +01001016 st->ip_off = skb_network_header(skb) - skb->data;
1017 st->tcp_off = skb_transport_header(skb) - skb->data;
Ben Hutchingsc78c39e2013-03-08 20:03:17 +00001018 header_len = st->tcp_off + (tcp_hdr(skb)->doff << 2u);
1019 in_len = skb_headlen(skb) - header_len;
1020 st->header_len = header_len;
1021 st->in_len = in_len;
Ben Hutchings53cb13c2012-06-19 20:03:41 +01001022 if (st->protocol == htons(ETH_P_IP)) {
Ben Hutchings97142842012-06-22 02:44:01 +01001023 st->ip_base_len = st->header_len - st->ip_off;
Ben Hutchings738a8f42009-11-29 15:16:05 +00001024 st->ipv4_id = ntohs(ip_hdr(skb)->id);
Ben Hutchings53cb13c2012-06-19 20:03:41 +01001025 } else {
Ben Hutchings97142842012-06-22 02:44:01 +01001026 st->ip_base_len = st->header_len - st->tcp_off;
Ben Hutchings738a8f42009-11-29 15:16:05 +00001027 st->ipv4_id = 0;
Ben Hutchings53cb13c2012-06-19 20:03:41 +01001028 }
Ben Hutchingsb9b39b62008-05-07 12:51:12 +01001029 st->seqnum = ntohl(tcp_hdr(skb)->seq);
1030
1031 EFX_BUG_ON_PARANOID(tcp_hdr(skb)->urg);
1032 EFX_BUG_ON_PARANOID(tcp_hdr(skb)->syn);
1033 EFX_BUG_ON_PARANOID(tcp_hdr(skb)->rst);
1034
Ben Hutchingsc78c39e2013-03-08 20:03:17 +00001035 st->out_len = skb->len - header_len;
1036
Ben Hutchingsdfa50be2013-03-08 21:20:09 +00001037 if (!use_options) {
1038 st->header_unmap_len = 0;
1039
1040 if (likely(in_len == 0)) {
1041 st->dma_flags = 0;
1042 st->unmap_len = 0;
1043 return 0;
1044 }
1045
1046 dma_addr = dma_map_single(dma_dev, skb->data + header_len,
1047 in_len, DMA_TO_DEVICE);
1048 st->dma_flags = EFX_TX_BUF_MAP_SINGLE;
1049 st->dma_addr = dma_addr;
1050 st->unmap_addr = dma_addr;
1051 st->unmap_len = in_len;
1052 } else {
1053 dma_addr = dma_map_single(dma_dev, skb->data,
1054 skb_headlen(skb), DMA_TO_DEVICE);
1055 st->header_dma_addr = dma_addr;
1056 st->header_unmap_len = skb_headlen(skb);
Ben Hutchingsc78c39e2013-03-08 20:03:17 +00001057 st->dma_flags = 0;
Ben Hutchingsdfa50be2013-03-08 21:20:09 +00001058 st->dma_addr = dma_addr + header_len;
1059 st->unmap_len = 0;
Ben Hutchingsc78c39e2013-03-08 20:03:17 +00001060 }
1061
Ben Hutchingsdfa50be2013-03-08 21:20:09 +00001062 return unlikely(dma_mapping_error(dma_dev, dma_addr)) ? -ENOMEM : 0;
Ben Hutchingsb9b39b62008-05-07 12:51:12 +01001063}
1064
Ben Hutchings4d566062008-09-01 12:47:12 +01001065static int tso_get_fragment(struct tso_state *st, struct efx_nic *efx,
1066 skb_frag_t *frag)
Ben Hutchingsb9b39b62008-05-07 12:51:12 +01001067{
Ian Campbell4a22c4c2011-09-21 21:53:16 +00001068 st->unmap_addr = skb_frag_dma_map(&efx->pci_dev->dev, frag, 0,
Eric Dumazet9e903e02011-10-18 21:00:24 +00001069 skb_frag_size(frag), DMA_TO_DEVICE);
Ian Campbell5d6bcdf2011-10-06 11:10:48 +01001070 if (likely(!dma_mapping_error(&efx->pci_dev->dev, st->unmap_addr))) {
Ben Hutchings7668ff92012-05-17 20:52:20 +01001071 st->dma_flags = 0;
Eric Dumazet9e903e02011-10-18 21:00:24 +00001072 st->unmap_len = skb_frag_size(frag);
1073 st->in_len = skb_frag_size(frag);
Ben Hutchings23d9e602008-09-01 12:47:02 +01001074 st->dma_addr = st->unmap_addr;
Ben Hutchingsecbd95c2008-09-01 12:46:40 +01001075 return 0;
1076 }
1077 return -ENOMEM;
1078}
1079
Ben Hutchingsb9b39b62008-05-07 12:51:12 +01001080
1081/**
1082 * tso_fill_packet_with_fragment - form descriptors for the current fragment
1083 * @tx_queue: Efx TX queue
1084 * @skb: Socket buffer
1085 * @st: TSO state
1086 *
1087 * Form descriptors for the current fragment, until we reach the end
Ben Hutchings14bf7182012-05-22 01:27:58 +01001088 * of fragment or end-of-packet.
Ben Hutchingsb9b39b62008-05-07 12:51:12 +01001089 */
Ben Hutchings14bf7182012-05-22 01:27:58 +01001090static void tso_fill_packet_with_fragment(struct efx_tx_queue *tx_queue,
1091 const struct sk_buff *skb,
1092 struct tso_state *st)
Ben Hutchingsb9b39b62008-05-07 12:51:12 +01001093{
Ben Hutchingsecbd95c2008-09-01 12:46:40 +01001094 struct efx_tx_buffer *buffer;
Ben Hutchings14bf7182012-05-22 01:27:58 +01001095 int n;
Ben Hutchingsb9b39b62008-05-07 12:51:12 +01001096
Ben Hutchings23d9e602008-09-01 12:47:02 +01001097 if (st->in_len == 0)
Ben Hutchings14bf7182012-05-22 01:27:58 +01001098 return;
Ben Hutchingsb9b39b62008-05-07 12:51:12 +01001099 if (st->packet_space == 0)
Ben Hutchings14bf7182012-05-22 01:27:58 +01001100 return;
Ben Hutchingsb9b39b62008-05-07 12:51:12 +01001101
Ben Hutchings23d9e602008-09-01 12:47:02 +01001102 EFX_BUG_ON_PARANOID(st->in_len <= 0);
Ben Hutchingsb9b39b62008-05-07 12:51:12 +01001103 EFX_BUG_ON_PARANOID(st->packet_space <= 0);
1104
Ben Hutchings23d9e602008-09-01 12:47:02 +01001105 n = min(st->in_len, st->packet_space);
Ben Hutchingsb9b39b62008-05-07 12:51:12 +01001106
1107 st->packet_space -= n;
Ben Hutchings23d9e602008-09-01 12:47:02 +01001108 st->out_len -= n;
1109 st->in_len -= n;
Ben Hutchingsb9b39b62008-05-07 12:51:12 +01001110
Ben Hutchings14bf7182012-05-22 01:27:58 +01001111 efx_tx_queue_insert(tx_queue, st->dma_addr, n, &buffer);
Ben Hutchingsecbd95c2008-09-01 12:46:40 +01001112
Ben Hutchings14bf7182012-05-22 01:27:58 +01001113 if (st->out_len == 0) {
1114 /* Transfer ownership of the skb */
1115 buffer->skb = skb;
1116 buffer->flags = EFX_TX_BUF_SKB;
1117 } else if (st->packet_space != 0) {
1118 buffer->flags = EFX_TX_BUF_CONT;
1119 }
1120
1121 if (st->in_len == 0) {
1122 /* Transfer ownership of the DMA mapping */
1123 buffer->unmap_len = st->unmap_len;
1124 buffer->flags |= st->dma_flags;
1125 st->unmap_len = 0;
Ben Hutchingsecbd95c2008-09-01 12:46:40 +01001126 }
Ben Hutchingsb9b39b62008-05-07 12:51:12 +01001127
Ben Hutchings23d9e602008-09-01 12:47:02 +01001128 st->dma_addr += n;
Ben Hutchingsb9b39b62008-05-07 12:51:12 +01001129}
1130
1131
1132/**
1133 * tso_start_new_packet - generate a new header and prepare for the new packet
1134 * @tx_queue: Efx TX queue
1135 * @skb: Socket buffer
1136 * @st: TSO state
1137 *
1138 * Generate a new header and prepare for the new packet. Return 0 on
Ben Hutchingsf7251a92012-05-17 18:40:54 +01001139 * success, or -%ENOMEM if failed to alloc header.
Ben Hutchingsb9b39b62008-05-07 12:51:12 +01001140 */
Ben Hutchings4d566062008-09-01 12:47:12 +01001141static int tso_start_new_packet(struct efx_tx_queue *tx_queue,
1142 const struct sk_buff *skb,
1143 struct tso_state *st)
Ben Hutchingsb9b39b62008-05-07 12:51:12 +01001144{
Ben Hutchingsf7251a92012-05-17 18:40:54 +01001145 struct efx_tx_buffer *buffer =
Ben Hutchings0fe55652013-06-28 21:47:15 +01001146 efx_tx_queue_get_insert_buffer(tx_queue);
Ben Hutchingsdfa50be2013-03-08 21:20:09 +00001147 bool is_last = st->out_len <= skb_shinfo(skb)->gso_size;
1148 u8 tcp_flags_clear;
Ben Hutchingsb9b39b62008-05-07 12:51:12 +01001149
Ben Hutchingsdfa50be2013-03-08 21:20:09 +00001150 if (!is_last) {
Ben Hutchings53cb13c2012-06-19 20:03:41 +01001151 st->packet_space = skb_shinfo(skb)->gso_size;
Ben Hutchingsdfa50be2013-03-08 21:20:09 +00001152 tcp_flags_clear = 0x09; /* mask out FIN and PSH */
Ben Hutchingsb9b39b62008-05-07 12:51:12 +01001153 } else {
Ben Hutchings53cb13c2012-06-19 20:03:41 +01001154 st->packet_space = st->out_len;
Ben Hutchingsdfa50be2013-03-08 21:20:09 +00001155 tcp_flags_clear = 0x00;
Ben Hutchingsb9b39b62008-05-07 12:51:12 +01001156 }
Ben Hutchingsb9b39b62008-05-07 12:51:12 +01001157
Ben Hutchingsdfa50be2013-03-08 21:20:09 +00001158 if (!st->header_unmap_len) {
1159 /* Allocate and insert a DMA-mapped header buffer. */
1160 struct tcphdr *tsoh_th;
1161 unsigned ip_length;
1162 u8 *header;
1163 int rc;
Ben Hutchings738a8f42009-11-29 15:16:05 +00001164
Ben Hutchingsdfa50be2013-03-08 21:20:09 +00001165 header = efx_tsoh_get_buffer(tx_queue, buffer, st->header_len);
1166 if (!header)
1167 return -ENOMEM;
Ben Hutchings738a8f42009-11-29 15:16:05 +00001168
Ben Hutchingsdfa50be2013-03-08 21:20:09 +00001169 tsoh_th = (struct tcphdr *)(header + st->tcp_off);
1170
1171 /* Copy and update the headers. */
1172 memcpy(header, skb->data, st->header_len);
1173
1174 tsoh_th->seq = htonl(st->seqnum);
1175 ((u8 *)tsoh_th)[13] &= ~tcp_flags_clear;
1176
1177 ip_length = st->ip_base_len + st->packet_space;
1178
1179 if (st->protocol == htons(ETH_P_IP)) {
1180 struct iphdr *tsoh_iph =
1181 (struct iphdr *)(header + st->ip_off);
1182
1183 tsoh_iph->tot_len = htons(ip_length);
1184 tsoh_iph->id = htons(st->ipv4_id);
1185 } else {
1186 struct ipv6hdr *tsoh_iph =
1187 (struct ipv6hdr *)(header + st->ip_off);
1188
1189 tsoh_iph->payload_len = htons(ip_length);
1190 }
1191
1192 rc = efx_tso_put_header(tx_queue, buffer, header);
1193 if (unlikely(rc))
1194 return rc;
Ben Hutchings738a8f42009-11-29 15:16:05 +00001195 } else {
Ben Hutchingsdfa50be2013-03-08 21:20:09 +00001196 /* Send the original headers with a TSO option descriptor
1197 * in front
1198 */
1199 u8 tcp_flags = ((u8 *)tcp_hdr(skb))[13] & ~tcp_flags_clear;
Ben Hutchings738a8f42009-11-29 15:16:05 +00001200
Ben Hutchingsdfa50be2013-03-08 21:20:09 +00001201 buffer->flags = EFX_TX_BUF_OPTION;
1202 buffer->len = 0;
1203 buffer->unmap_len = 0;
1204 EFX_POPULATE_QWORD_5(buffer->option,
1205 ESF_DZ_TX_DESC_IS_OPT, 1,
1206 ESF_DZ_TX_OPTION_TYPE,
1207 ESE_DZ_TX_OPTION_DESC_TSO,
1208 ESF_DZ_TX_TSO_TCP_FLAGS, tcp_flags,
1209 ESF_DZ_TX_TSO_IP_ID, st->ipv4_id,
1210 ESF_DZ_TX_TSO_TCP_SEQNO, st->seqnum);
1211 ++tx_queue->insert_count;
1212
1213 /* We mapped the headers in tso_start(). Unmap them
1214 * when the last segment is completed.
1215 */
Ben Hutchings0fe55652013-06-28 21:47:15 +01001216 buffer = efx_tx_queue_get_insert_buffer(tx_queue);
Ben Hutchingsdfa50be2013-03-08 21:20:09 +00001217 buffer->dma_addr = st->header_dma_addr;
1218 buffer->len = st->header_len;
1219 if (is_last) {
1220 buffer->flags = EFX_TX_BUF_CONT | EFX_TX_BUF_MAP_SINGLE;
1221 buffer->unmap_len = st->header_unmap_len;
1222 /* Ensure we only unmap them once in case of a
1223 * later DMA mapping error and rollback
1224 */
1225 st->header_unmap_len = 0;
1226 } else {
1227 buffer->flags = EFX_TX_BUF_CONT;
1228 buffer->unmap_len = 0;
1229 }
1230 ++tx_queue->insert_count;
Ben Hutchings738a8f42009-11-29 15:16:05 +00001231 }
Ben Hutchingsb9b39b62008-05-07 12:51:12 +01001232
Ben Hutchingsdfa50be2013-03-08 21:20:09 +00001233 st->seqnum += skb_shinfo(skb)->gso_size;
1234
1235 /* Linux leaves suitable gaps in the IP ID space for us to fill. */
1236 ++st->ipv4_id;
Ben Hutchingsf7251a92012-05-17 18:40:54 +01001237
Ben Hutchingsb9b39b62008-05-07 12:51:12 +01001238 ++tx_queue->tso_packets;
1239
Ben Hutchingsb9b39b62008-05-07 12:51:12 +01001240 return 0;
1241}
1242
1243
1244/**
1245 * efx_enqueue_skb_tso - segment and transmit a TSO socket buffer
1246 * @tx_queue: Efx TX queue
1247 * @skb: Socket buffer
1248 *
1249 * Context: You must hold netif_tx_lock() to call this function.
1250 *
1251 * Add socket buffer @skb to @tx_queue, doing TSO or return != 0 if
1252 * @skb was not enqueued. In all cases @skb is consumed. Return
Ben Hutchings14bf7182012-05-22 01:27:58 +01001253 * %NETDEV_TX_OK.
Ben Hutchingsb9b39b62008-05-07 12:51:12 +01001254 */
1255static int efx_enqueue_skb_tso(struct efx_tx_queue *tx_queue,
Ben Hutchings740847d2008-09-01 12:48:23 +01001256 struct sk_buff *skb)
Ben Hutchingsb9b39b62008-05-07 12:51:12 +01001257{
Ben Hutchingsecbd95c2008-09-01 12:46:40 +01001258 struct efx_nic *efx = tx_queue->efx;
Ben Hutchings14bf7182012-05-22 01:27:58 +01001259 int frag_i, rc;
Ben Hutchingsb9b39b62008-05-07 12:51:12 +01001260 struct tso_state state;
Ben Hutchingsb9b39b62008-05-07 12:51:12 +01001261
Ben Hutchings738a8f42009-11-29 15:16:05 +00001262 /* Find the packet protocol and sanity-check it */
1263 state.protocol = efx_tso_check_protocol(skb);
Ben Hutchingsb9b39b62008-05-07 12:51:12 +01001264
1265 EFX_BUG_ON_PARANOID(tx_queue->write_count != tx_queue->insert_count);
1266
Ben Hutchingsc78c39e2013-03-08 20:03:17 +00001267 rc = tso_start(&state, efx, skb);
1268 if (rc)
1269 goto mem_err;
Ben Hutchingsb9b39b62008-05-07 12:51:12 +01001270
Ben Hutchingsc78c39e2013-03-08 20:03:17 +00001271 if (likely(state.in_len == 0)) {
Ben Hutchingsb9b39b62008-05-07 12:51:12 +01001272 /* Grab the first payload fragment. */
1273 EFX_BUG_ON_PARANOID(skb_shinfo(skb)->nr_frags < 1);
1274 frag_i = 0;
Ben Hutchingsecbd95c2008-09-01 12:46:40 +01001275 rc = tso_get_fragment(&state, efx,
1276 skb_shinfo(skb)->frags + frag_i);
Ben Hutchingsb9b39b62008-05-07 12:51:12 +01001277 if (rc)
1278 goto mem_err;
1279 } else {
Ben Hutchingsc78c39e2013-03-08 20:03:17 +00001280 /* Payload starts in the header area. */
Ben Hutchingsb9b39b62008-05-07 12:51:12 +01001281 frag_i = -1;
1282 }
1283
1284 if (tso_start_new_packet(tx_queue, skb, &state) < 0)
1285 goto mem_err;
1286
1287 while (1) {
Ben Hutchings14bf7182012-05-22 01:27:58 +01001288 tso_fill_packet_with_fragment(tx_queue, skb, &state);
Ben Hutchingsb9b39b62008-05-07 12:51:12 +01001289
1290 /* Move onto the next fragment? */
Ben Hutchings23d9e602008-09-01 12:47:02 +01001291 if (state.in_len == 0) {
Ben Hutchingsb9b39b62008-05-07 12:51:12 +01001292 if (++frag_i >= skb_shinfo(skb)->nr_frags)
1293 /* End of payload reached. */
1294 break;
Ben Hutchingsecbd95c2008-09-01 12:46:40 +01001295 rc = tso_get_fragment(&state, efx,
1296 skb_shinfo(skb)->frags + frag_i);
Ben Hutchingsb9b39b62008-05-07 12:51:12 +01001297 if (rc)
1298 goto mem_err;
1299 }
1300
1301 /* Start at new packet? */
1302 if (state.packet_space == 0 &&
1303 tso_start_new_packet(tx_queue, skb, &state) < 0)
1304 goto mem_err;
1305 }
1306
Eric Dumazet449fa022011-11-30 17:12:27 -05001307 netdev_tx_sent_queue(tx_queue->core_txq, skb->len);
1308
Ben Hutchingsb9b39b62008-05-07 12:51:12 +01001309 /* Pass off to hardware */
Ben Hutchings152b6a62009-11-29 03:43:56 +00001310 efx_nic_push_buffers(tx_queue);
Ben Hutchingsb9b39b62008-05-07 12:51:12 +01001311
Ben Hutchings14bf7182012-05-22 01:27:58 +01001312 efx_tx_maybe_stop_queue(tx_queue);
1313
Ben Hutchingsb9b39b62008-05-07 12:51:12 +01001314 tx_queue->tso_bursts++;
1315 return NETDEV_TX_OK;
1316
1317 mem_err:
Ben Hutchings62776d02010-06-23 11:30:07 +00001318 netif_err(efx, tx_err, efx->net_dev,
Ben Hutchings0e33d872012-05-17 17:46:55 +01001319 "Out of memory for TSO headers, or DMA mapping error\n");
Ben Hutchings9bc183d2009-11-23 16:06:47 +00001320 dev_kfree_skb_any(skb);
Ben Hutchingsb9b39b62008-05-07 12:51:12 +01001321
Ben Hutchings5988b632008-09-01 12:46:36 +01001322 /* Free the DMA mapping we were in the process of writing out */
Ben Hutchings23d9e602008-09-01 12:47:02 +01001323 if (state.unmap_len) {
Ben Hutchings7668ff92012-05-17 20:52:20 +01001324 if (state.dma_flags & EFX_TX_BUF_MAP_SINGLE)
Ben Hutchings0e33d872012-05-17 17:46:55 +01001325 dma_unmap_single(&efx->pci_dev->dev, state.unmap_addr,
1326 state.unmap_len, DMA_TO_DEVICE);
Ben Hutchingsecbd95c2008-09-01 12:46:40 +01001327 else
Ben Hutchings0e33d872012-05-17 17:46:55 +01001328 dma_unmap_page(&efx->pci_dev->dev, state.unmap_addr,
1329 state.unmap_len, DMA_TO_DEVICE);
Ben Hutchingsecbd95c2008-09-01 12:46:40 +01001330 }
Ben Hutchings5988b632008-09-01 12:46:36 +01001331
Ben Hutchingsdfa50be2013-03-08 21:20:09 +00001332 /* Free the header DMA mapping, if using option descriptors */
1333 if (state.header_unmap_len)
1334 dma_unmap_single(&efx->pci_dev->dev, state.header_dma_addr,
1335 state.header_unmap_len, DMA_TO_DEVICE);
1336
Ben Hutchingsb9b39b62008-05-07 12:51:12 +01001337 efx_enqueue_unwind(tx_queue);
Ben Hutchings14bf7182012-05-22 01:27:58 +01001338 return NETDEV_TX_OK;
Ben Hutchingsb9b39b62008-05-07 12:51:12 +01001339}