blob: 0e9889ca68fced1d38974e87c6bb5470d7ada084 [file] [log] [blame]
Ben Hutchings8ceee662008-04-27 12:55:59 +01001/****************************************************************************
2 * Driver for Solarflare Solarstorm network controllers and boards
3 * Copyright 2005-2006 Fen Systems Ltd.
4 * Copyright 2005-2008 Solarflare Communications Inc.
5 *
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License version 2 as published
8 * by the Free Software Foundation, incorporated herein by reference.
9 */
10
11#include <linux/pci.h>
12#include <linux/tcp.h>
13#include <linux/ip.h>
14#include <linux/in.h>
15#include <linux/if_ether.h>
16#include <linux/highmem.h>
17#include "net_driver.h"
18#include "tx.h"
19#include "efx.h"
20#include "falcon.h"
21#include "workarounds.h"
22
23/*
24 * TX descriptor ring full threshold
25 *
26 * The tx_queue descriptor ring fill-level must fall below this value
27 * before we restart the netif queue
28 */
29#define EFX_NETDEV_TX_THRESHOLD(_tx_queue) \
30 (_tx_queue->efx->type->txd_ring_mask / 2u)
31
32/* We want to be able to nest calls to netif_stop_queue(), since each
33 * channel can have an individual stop on the queue.
34 */
35void efx_stop_queue(struct efx_nic *efx)
36{
37 spin_lock_bh(&efx->netif_stop_lock);
38 EFX_TRACE(efx, "stop TX queue\n");
39
40 atomic_inc(&efx->netif_stop_count);
41 netif_stop_queue(efx->net_dev);
42
43 spin_unlock_bh(&efx->netif_stop_lock);
44}
45
46/* Wake netif's TX queue
47 * We want to be able to nest calls to netif_stop_queue(), since each
48 * channel can have an individual stop on the queue.
49 */
Ben Hutchings4d566062008-09-01 12:47:12 +010050void efx_wake_queue(struct efx_nic *efx)
Ben Hutchings8ceee662008-04-27 12:55:59 +010051{
52 local_bh_disable();
53 if (atomic_dec_and_lock(&efx->netif_stop_count,
54 &efx->netif_stop_lock)) {
55 EFX_TRACE(efx, "waking TX queue\n");
56 netif_wake_queue(efx->net_dev);
57 spin_unlock(&efx->netif_stop_lock);
58 }
59 local_bh_enable();
60}
61
Ben Hutchings4d566062008-09-01 12:47:12 +010062static void efx_dequeue_buffer(struct efx_tx_queue *tx_queue,
63 struct efx_tx_buffer *buffer)
Ben Hutchings8ceee662008-04-27 12:55:59 +010064{
65 if (buffer->unmap_len) {
66 struct pci_dev *pci_dev = tx_queue->efx->pci_dev;
Ben Hutchingscc12dac2008-09-01 12:46:43 +010067 dma_addr_t unmap_addr = (buffer->dma_addr + buffer->len -
68 buffer->unmap_len);
Ben Hutchings8ceee662008-04-27 12:55:59 +010069 if (buffer->unmap_single)
Ben Hutchingscc12dac2008-09-01 12:46:43 +010070 pci_unmap_single(pci_dev, unmap_addr, buffer->unmap_len,
71 PCI_DMA_TODEVICE);
Ben Hutchings8ceee662008-04-27 12:55:59 +010072 else
Ben Hutchingscc12dac2008-09-01 12:46:43 +010073 pci_unmap_page(pci_dev, unmap_addr, buffer->unmap_len,
74 PCI_DMA_TODEVICE);
Ben Hutchings8ceee662008-04-27 12:55:59 +010075 buffer->unmap_len = 0;
Ben Hutchingsdc8cfa52008-09-01 12:46:50 +010076 buffer->unmap_single = false;
Ben Hutchings8ceee662008-04-27 12:55:59 +010077 }
78
79 if (buffer->skb) {
80 dev_kfree_skb_any((struct sk_buff *) buffer->skb);
81 buffer->skb = NULL;
82 EFX_TRACE(tx_queue->efx, "TX queue %d transmission id %x "
83 "complete\n", tx_queue->queue, read_ptr);
84 }
85}
86
Ben Hutchingsb9b39b62008-05-07 12:51:12 +010087/**
88 * struct efx_tso_header - a DMA mapped buffer for packet headers
89 * @next: Linked list of free ones.
90 * The list is protected by the TX queue lock.
91 * @dma_unmap_len: Length to unmap for an oversize buffer, or 0.
92 * @dma_addr: The DMA address of the header below.
93 *
94 * This controls the memory used for a TSO header. Use TSOH_DATA()
95 * to find the packet header data. Use TSOH_SIZE() to calculate the
96 * total size required for a given packet header length. TSO headers
97 * in the free list are exactly %TSOH_STD_SIZE bytes in size.
98 */
99struct efx_tso_header {
100 union {
101 struct efx_tso_header *next;
102 size_t unmap_len;
103 };
104 dma_addr_t dma_addr;
105};
106
107static int efx_enqueue_skb_tso(struct efx_tx_queue *tx_queue,
108 const struct sk_buff *skb);
109static void efx_fini_tso(struct efx_tx_queue *tx_queue);
110static void efx_tsoh_heap_free(struct efx_tx_queue *tx_queue,
111 struct efx_tso_header *tsoh);
112
Ben Hutchings4d566062008-09-01 12:47:12 +0100113static void efx_tsoh_free(struct efx_tx_queue *tx_queue,
114 struct efx_tx_buffer *buffer)
Ben Hutchingsb9b39b62008-05-07 12:51:12 +0100115{
116 if (buffer->tsoh) {
117 if (likely(!buffer->tsoh->unmap_len)) {
118 buffer->tsoh->next = tx_queue->tso_headers_free;
119 tx_queue->tso_headers_free = buffer->tsoh;
120 } else {
121 efx_tsoh_heap_free(tx_queue, buffer->tsoh);
122 }
123 buffer->tsoh = NULL;
124 }
125}
126
Ben Hutchings8ceee662008-04-27 12:55:59 +0100127
128/*
129 * Add a socket buffer to a TX queue
130 *
131 * This maps all fragments of a socket buffer for DMA and adds them to
132 * the TX queue. The queue's insert pointer will be incremented by
133 * the number of fragments in the socket buffer.
134 *
135 * If any DMA mapping fails, any mapped fragments will be unmapped,
136 * the queue's insert pointer will be restored to its original value.
137 *
138 * Returns NETDEV_TX_OK or NETDEV_TX_BUSY
139 * You must hold netif_tx_lock() to call this function.
140 */
Ben Hutchings4d566062008-09-01 12:47:12 +0100141static int efx_enqueue_skb(struct efx_tx_queue *tx_queue,
142 const struct sk_buff *skb)
Ben Hutchings8ceee662008-04-27 12:55:59 +0100143{
144 struct efx_nic *efx = tx_queue->efx;
145 struct pci_dev *pci_dev = efx->pci_dev;
146 struct efx_tx_buffer *buffer;
147 skb_frag_t *fragment;
148 struct page *page;
149 int page_offset;
150 unsigned int len, unmap_len = 0, fill_level, insert_ptr, misalign;
151 dma_addr_t dma_addr, unmap_addr = 0;
152 unsigned int dma_len;
Ben Hutchingsdc8cfa52008-09-01 12:46:50 +0100153 bool unmap_single;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100154 int q_space, i = 0;
155 int rc = NETDEV_TX_OK;
156
157 EFX_BUG_ON_PARANOID(tx_queue->write_count != tx_queue->insert_count);
158
Ben Hutchingsb9b39b62008-05-07 12:51:12 +0100159 if (skb_shinfo((struct sk_buff *)skb)->gso_size)
160 return efx_enqueue_skb_tso(tx_queue, skb);
161
Ben Hutchings8ceee662008-04-27 12:55:59 +0100162 /* Get size of the initial fragment */
163 len = skb_headlen(skb);
164
165 fill_level = tx_queue->insert_count - tx_queue->old_read_count;
166 q_space = efx->type->txd_ring_mask - 1 - fill_level;
167
168 /* Map for DMA. Use pci_map_single rather than pci_map_page
169 * since this is more efficient on machines with sparse
170 * memory.
171 */
Ben Hutchingsdc8cfa52008-09-01 12:46:50 +0100172 unmap_single = true;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100173 dma_addr = pci_map_single(pci_dev, skb->data, len, PCI_DMA_TODEVICE);
174
175 /* Process all fragments */
176 while (1) {
FUJITA Tomonori8d8bb392008-07-25 19:44:49 -0700177 if (unlikely(pci_dma_mapping_error(pci_dev, dma_addr)))
Ben Hutchings8ceee662008-04-27 12:55:59 +0100178 goto pci_err;
179
180 /* Store fields for marking in the per-fragment final
181 * descriptor */
182 unmap_len = len;
183 unmap_addr = dma_addr;
184
185 /* Add to TX queue, splitting across DMA boundaries */
186 do {
187 if (unlikely(q_space-- <= 0)) {
188 /* It might be that completions have
189 * happened since the xmit path last
190 * checked. Update the xmit path's
191 * copy of read_count.
192 */
193 ++tx_queue->stopped;
194 /* This memory barrier protects the
195 * change of stopped from the access
196 * of read_count. */
197 smp_mb();
198 tx_queue->old_read_count =
199 *(volatile unsigned *)
200 &tx_queue->read_count;
201 fill_level = (tx_queue->insert_count
202 - tx_queue->old_read_count);
203 q_space = (efx->type->txd_ring_mask - 1 -
204 fill_level);
205 if (unlikely(q_space-- <= 0))
206 goto stop;
207 smp_mb();
208 --tx_queue->stopped;
209 }
210
211 insert_ptr = (tx_queue->insert_count &
212 efx->type->txd_ring_mask);
213 buffer = &tx_queue->buffer[insert_ptr];
Ben Hutchingsb9b39b62008-05-07 12:51:12 +0100214 efx_tsoh_free(tx_queue, buffer);
215 EFX_BUG_ON_PARANOID(buffer->tsoh);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100216 EFX_BUG_ON_PARANOID(buffer->skb);
217 EFX_BUG_ON_PARANOID(buffer->len);
Ben Hutchingsdc8cfa52008-09-01 12:46:50 +0100218 EFX_BUG_ON_PARANOID(!buffer->continuation);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100219 EFX_BUG_ON_PARANOID(buffer->unmap_len);
220
221 dma_len = (((~dma_addr) & efx->type->tx_dma_mask) + 1);
222 if (likely(dma_len > len))
223 dma_len = len;
224
225 misalign = (unsigned)dma_addr & efx->type->bug5391_mask;
226 if (misalign && dma_len + misalign > 512)
227 dma_len = 512 - misalign;
228
229 /* Fill out per descriptor fields */
230 buffer->len = dma_len;
231 buffer->dma_addr = dma_addr;
232 len -= dma_len;
233 dma_addr += dma_len;
234 ++tx_queue->insert_count;
235 } while (len);
236
237 /* Transfer ownership of the unmapping to the final buffer */
Ben Hutchings8ceee662008-04-27 12:55:59 +0100238 buffer->unmap_single = unmap_single;
239 buffer->unmap_len = unmap_len;
240 unmap_len = 0;
241
242 /* Get address and size of next fragment */
243 if (i >= skb_shinfo(skb)->nr_frags)
244 break;
245 fragment = &skb_shinfo(skb)->frags[i];
246 len = fragment->size;
247 page = fragment->page;
248 page_offset = fragment->page_offset;
249 i++;
250 /* Map for DMA */
Ben Hutchingsdc8cfa52008-09-01 12:46:50 +0100251 unmap_single = false;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100252 dma_addr = pci_map_page(pci_dev, page, page_offset, len,
253 PCI_DMA_TODEVICE);
254 }
255
256 /* Transfer ownership of the skb to the final buffer */
257 buffer->skb = skb;
Ben Hutchingsdc8cfa52008-09-01 12:46:50 +0100258 buffer->continuation = false;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100259
260 /* Pass off to hardware */
261 falcon_push_buffers(tx_queue);
262
263 return NETDEV_TX_OK;
264
265 pci_err:
266 EFX_ERR_RL(efx, " TX queue %d could not map skb with %d bytes %d "
267 "fragments for DMA\n", tx_queue->queue, skb->len,
268 skb_shinfo(skb)->nr_frags + 1);
269
270 /* Mark the packet as transmitted, and free the SKB ourselves */
271 dev_kfree_skb_any((struct sk_buff *)skb);
272 goto unwind;
273
274 stop:
275 rc = NETDEV_TX_BUSY;
276
277 if (tx_queue->stopped == 1)
278 efx_stop_queue(efx);
279
280 unwind:
281 /* Work backwards until we hit the original insert pointer value */
282 while (tx_queue->insert_count != tx_queue->write_count) {
283 --tx_queue->insert_count;
284 insert_ptr = tx_queue->insert_count & efx->type->txd_ring_mask;
285 buffer = &tx_queue->buffer[insert_ptr];
286 efx_dequeue_buffer(tx_queue, buffer);
287 buffer->len = 0;
288 }
289
290 /* Free the fragment we were mid-way through pushing */
Ben Hutchingsecbd95c2008-09-01 12:46:40 +0100291 if (unmap_len) {
292 if (unmap_single)
293 pci_unmap_single(pci_dev, unmap_addr, unmap_len,
294 PCI_DMA_TODEVICE);
295 else
296 pci_unmap_page(pci_dev, unmap_addr, unmap_len,
297 PCI_DMA_TODEVICE);
298 }
Ben Hutchings8ceee662008-04-27 12:55:59 +0100299
300 return rc;
301}
302
303/* Remove packets from the TX queue
304 *
305 * This removes packets from the TX queue, up to and including the
306 * specified index.
307 */
Ben Hutchings4d566062008-09-01 12:47:12 +0100308static void efx_dequeue_buffers(struct efx_tx_queue *tx_queue,
309 unsigned int index)
Ben Hutchings8ceee662008-04-27 12:55:59 +0100310{
311 struct efx_nic *efx = tx_queue->efx;
312 unsigned int stop_index, read_ptr;
313 unsigned int mask = tx_queue->efx->type->txd_ring_mask;
314
315 stop_index = (index + 1) & mask;
316 read_ptr = tx_queue->read_count & mask;
317
318 while (read_ptr != stop_index) {
319 struct efx_tx_buffer *buffer = &tx_queue->buffer[read_ptr];
320 if (unlikely(buffer->len == 0)) {
321 EFX_ERR(tx_queue->efx, "TX queue %d spurious TX "
322 "completion id %x\n", tx_queue->queue,
323 read_ptr);
324 efx_schedule_reset(efx, RESET_TYPE_TX_SKIP);
325 return;
326 }
327
328 efx_dequeue_buffer(tx_queue, buffer);
Ben Hutchingsdc8cfa52008-09-01 12:46:50 +0100329 buffer->continuation = true;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100330 buffer->len = 0;
331
332 ++tx_queue->read_count;
333 read_ptr = tx_queue->read_count & mask;
334 }
335}
336
337/* Initiate a packet transmission on the specified TX queue.
338 * Note that returning anything other than NETDEV_TX_OK will cause the
339 * OS to free the skb.
340 *
341 * This function is split out from efx_hard_start_xmit to allow the
342 * loopback test to direct packets via specific TX queues. It is
343 * therefore a non-static inline, so as not to penalise performance
344 * for non-loopback transmissions.
345 *
346 * Context: netif_tx_lock held
347 */
348inline int efx_xmit(struct efx_nic *efx,
349 struct efx_tx_queue *tx_queue, struct sk_buff *skb)
350{
351 int rc;
352
353 /* Map fragments for DMA and add to TX queue */
354 rc = efx_enqueue_skb(tx_queue, skb);
355 if (unlikely(rc != NETDEV_TX_OK))
356 goto out;
357
358 /* Update last TX timer */
359 efx->net_dev->trans_start = jiffies;
360
361 out:
362 return rc;
363}
364
365/* Initiate a packet transmission. We use one channel per CPU
366 * (sharing when we have more CPUs than channels). On Falcon, the TX
367 * completion events will be directed back to the CPU that transmitted
368 * the packet, which should be cache-efficient.
369 *
370 * Context: non-blocking.
371 * Note that returning anything other than NETDEV_TX_OK will cause the
372 * OS to free the skb.
373 */
374int efx_hard_start_xmit(struct sk_buff *skb, struct net_device *net_dev)
375{
Ben Hutchings767e4682008-09-01 12:43:14 +0100376 struct efx_nic *efx = netdev_priv(net_dev);
Ben Hutchings60ac1062008-09-01 12:44:59 +0100377 struct efx_tx_queue *tx_queue;
378
379 if (likely(skb->ip_summed == CHECKSUM_PARTIAL))
380 tx_queue = &efx->tx_queue[EFX_TX_QUEUE_OFFLOAD_CSUM];
381 else
382 tx_queue = &efx->tx_queue[EFX_TX_QUEUE_NO_CSUM];
383
384 return efx_xmit(efx, tx_queue, skb);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100385}
386
387void efx_xmit_done(struct efx_tx_queue *tx_queue, unsigned int index)
388{
389 unsigned fill_level;
390 struct efx_nic *efx = tx_queue->efx;
391
392 EFX_BUG_ON_PARANOID(index > efx->type->txd_ring_mask);
393
394 efx_dequeue_buffers(tx_queue, index);
395
396 /* See if we need to restart the netif queue. This barrier
397 * separates the update of read_count from the test of
398 * stopped. */
399 smp_mb();
400 if (unlikely(tx_queue->stopped)) {
401 fill_level = tx_queue->insert_count - tx_queue->read_count;
402 if (fill_level < EFX_NETDEV_TX_THRESHOLD(tx_queue)) {
Ben Hutchings55668612008-05-16 21:16:10 +0100403 EFX_BUG_ON_PARANOID(!efx_dev_registered(efx));
Ben Hutchings8ceee662008-04-27 12:55:59 +0100404
405 /* Do this under netif_tx_lock(), to avoid racing
406 * with efx_xmit(). */
407 netif_tx_lock(efx->net_dev);
408 if (tx_queue->stopped) {
409 tx_queue->stopped = 0;
410 efx_wake_queue(efx);
411 }
412 netif_tx_unlock(efx->net_dev);
413 }
414 }
415}
416
417int efx_probe_tx_queue(struct efx_tx_queue *tx_queue)
418{
419 struct efx_nic *efx = tx_queue->efx;
420 unsigned int txq_size;
421 int i, rc;
422
423 EFX_LOG(efx, "creating TX queue %d\n", tx_queue->queue);
424
425 /* Allocate software ring */
426 txq_size = (efx->type->txd_ring_mask + 1) * sizeof(*tx_queue->buffer);
427 tx_queue->buffer = kzalloc(txq_size, GFP_KERNEL);
Ben Hutchings60ac1062008-09-01 12:44:59 +0100428 if (!tx_queue->buffer)
429 return -ENOMEM;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100430 for (i = 0; i <= efx->type->txd_ring_mask; ++i)
Ben Hutchingsdc8cfa52008-09-01 12:46:50 +0100431 tx_queue->buffer[i].continuation = true;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100432
433 /* Allocate hardware ring */
434 rc = falcon_probe_tx(tx_queue);
435 if (rc)
Ben Hutchings60ac1062008-09-01 12:44:59 +0100436 goto fail;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100437
438 return 0;
439
Ben Hutchings60ac1062008-09-01 12:44:59 +0100440 fail:
Ben Hutchings8ceee662008-04-27 12:55:59 +0100441 kfree(tx_queue->buffer);
442 tx_queue->buffer = NULL;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100443 return rc;
444}
445
446int efx_init_tx_queue(struct efx_tx_queue *tx_queue)
447{
448 EFX_LOG(tx_queue->efx, "initialising TX queue %d\n", tx_queue->queue);
449
450 tx_queue->insert_count = 0;
451 tx_queue->write_count = 0;
452 tx_queue->read_count = 0;
453 tx_queue->old_read_count = 0;
454 BUG_ON(tx_queue->stopped);
455
456 /* Set up TX descriptor ring */
457 return falcon_init_tx(tx_queue);
458}
459
460void efx_release_tx_buffers(struct efx_tx_queue *tx_queue)
461{
462 struct efx_tx_buffer *buffer;
463
464 if (!tx_queue->buffer)
465 return;
466
467 /* Free any buffers left in the ring */
468 while (tx_queue->read_count != tx_queue->write_count) {
469 buffer = &tx_queue->buffer[tx_queue->read_count &
470 tx_queue->efx->type->txd_ring_mask];
471 efx_dequeue_buffer(tx_queue, buffer);
Ben Hutchingsdc8cfa52008-09-01 12:46:50 +0100472 buffer->continuation = true;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100473 buffer->len = 0;
474
475 ++tx_queue->read_count;
476 }
477}
478
479void efx_fini_tx_queue(struct efx_tx_queue *tx_queue)
480{
481 EFX_LOG(tx_queue->efx, "shutting down TX queue %d\n", tx_queue->queue);
482
483 /* Flush TX queue, remove descriptor ring */
484 falcon_fini_tx(tx_queue);
485
486 efx_release_tx_buffers(tx_queue);
487
Ben Hutchingsb9b39b62008-05-07 12:51:12 +0100488 /* Free up TSO header cache */
489 efx_fini_tso(tx_queue);
490
Ben Hutchings8ceee662008-04-27 12:55:59 +0100491 /* Release queue's stop on port, if any */
492 if (tx_queue->stopped) {
493 tx_queue->stopped = 0;
494 efx_wake_queue(tx_queue->efx);
495 }
496}
497
498void efx_remove_tx_queue(struct efx_tx_queue *tx_queue)
499{
500 EFX_LOG(tx_queue->efx, "destroying TX queue %d\n", tx_queue->queue);
501 falcon_remove_tx(tx_queue);
502
503 kfree(tx_queue->buffer);
504 tx_queue->buffer = NULL;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100505}
506
507
Ben Hutchingsb9b39b62008-05-07 12:51:12 +0100508/* Efx TCP segmentation acceleration.
509 *
510 * Why? Because by doing it here in the driver we can go significantly
511 * faster than the GSO.
512 *
513 * Requires TX checksum offload support.
514 */
515
516/* Number of bytes inserted at the start of a TSO header buffer,
517 * similar to NET_IP_ALIGN.
518 */
519#if defined(__i386__) || defined(__x86_64__)
520#define TSOH_OFFSET 0
521#else
522#define TSOH_OFFSET NET_IP_ALIGN
523#endif
524
525#define TSOH_BUFFER(tsoh) ((u8 *)(tsoh + 1) + TSOH_OFFSET)
526
527/* Total size of struct efx_tso_header, buffer and padding */
528#define TSOH_SIZE(hdr_len) \
529 (sizeof(struct efx_tso_header) + TSOH_OFFSET + hdr_len)
530
531/* Size of blocks on free list. Larger blocks must be allocated from
532 * the heap.
533 */
534#define TSOH_STD_SIZE 128
535
536#define PTR_DIFF(p1, p2) ((u8 *)(p1) - (u8 *)(p2))
537#define ETH_HDR_LEN(skb) (skb_network_header(skb) - (skb)->data)
538#define SKB_TCP_OFF(skb) PTR_DIFF(tcp_hdr(skb), (skb)->data)
539#define SKB_IPV4_OFF(skb) PTR_DIFF(ip_hdr(skb), (skb)->data)
540
541/**
542 * struct tso_state - TSO state for an SKB
Ben Hutchings23d9e602008-09-01 12:47:02 +0100543 * @out_len: Remaining length in current segment
Ben Hutchingsb9b39b62008-05-07 12:51:12 +0100544 * @seqnum: Current sequence number
Ben Hutchings23d9e602008-09-01 12:47:02 +0100545 * @ipv4_id: Current IPv4 ID, host endian
Ben Hutchingsb9b39b62008-05-07 12:51:12 +0100546 * @packet_space: Remaining space in current packet
Ben Hutchings23d9e602008-09-01 12:47:02 +0100547 * @dma_addr: DMA address of current position
548 * @in_len: Remaining length in current SKB fragment
549 * @unmap_len: Length of SKB fragment
550 * @unmap_addr: DMA address of SKB fragment
551 * @unmap_single: DMA single vs page mapping flag
552 * @header_len: Number of bytes of header
553 * @full_packet_size: Number of bytes to put in each outgoing segment
Ben Hutchingsb9b39b62008-05-07 12:51:12 +0100554 *
555 * The state used during segmentation. It is put into this data structure
556 * just to make it easy to pass into inline functions.
557 */
558struct tso_state {
Ben Hutchings23d9e602008-09-01 12:47:02 +0100559 /* Output position */
560 unsigned out_len;
Ben Hutchingsb9b39b62008-05-07 12:51:12 +0100561 unsigned seqnum;
Ben Hutchings23d9e602008-09-01 12:47:02 +0100562 unsigned ipv4_id;
Ben Hutchingsb9b39b62008-05-07 12:51:12 +0100563 unsigned packet_space;
564
Ben Hutchings23d9e602008-09-01 12:47:02 +0100565 /* Input position */
566 dma_addr_t dma_addr;
567 unsigned in_len;
568 unsigned unmap_len;
569 dma_addr_t unmap_addr;
570 bool unmap_single;
Ben Hutchingsb9b39b62008-05-07 12:51:12 +0100571
Ben Hutchings23d9e602008-09-01 12:47:02 +0100572 unsigned header_len;
573 int full_packet_size;
Ben Hutchingsb9b39b62008-05-07 12:51:12 +0100574};
575
576
577/*
578 * Verify that our various assumptions about sk_buffs and the conditions
579 * under which TSO will be attempted hold true.
580 */
Ben Hutchings4d566062008-09-01 12:47:12 +0100581static void efx_tso_check_safe(const struct sk_buff *skb)
Ben Hutchingsb9b39b62008-05-07 12:51:12 +0100582{
583 EFX_BUG_ON_PARANOID(skb->protocol != htons(ETH_P_IP));
584 EFX_BUG_ON_PARANOID(((struct ethhdr *)skb->data)->h_proto !=
585 skb->protocol);
586 EFX_BUG_ON_PARANOID(ip_hdr(skb)->protocol != IPPROTO_TCP);
587 EFX_BUG_ON_PARANOID((PTR_DIFF(tcp_hdr(skb), skb->data)
588 + (tcp_hdr(skb)->doff << 2u)) >
589 skb_headlen(skb));
590}
591
592
593/*
594 * Allocate a page worth of efx_tso_header structures, and string them
595 * into the tx_queue->tso_headers_free linked list. Return 0 or -ENOMEM.
596 */
597static int efx_tsoh_block_alloc(struct efx_tx_queue *tx_queue)
598{
599
600 struct pci_dev *pci_dev = tx_queue->efx->pci_dev;
601 struct efx_tso_header *tsoh;
602 dma_addr_t dma_addr;
603 u8 *base_kva, *kva;
604
605 base_kva = pci_alloc_consistent(pci_dev, PAGE_SIZE, &dma_addr);
606 if (base_kva == NULL) {
607 EFX_ERR(tx_queue->efx, "Unable to allocate page for TSO"
608 " headers\n");
609 return -ENOMEM;
610 }
611
612 /* pci_alloc_consistent() allocates pages. */
613 EFX_BUG_ON_PARANOID(dma_addr & (PAGE_SIZE - 1u));
614
615 for (kva = base_kva; kva < base_kva + PAGE_SIZE; kva += TSOH_STD_SIZE) {
616 tsoh = (struct efx_tso_header *)kva;
617 tsoh->dma_addr = dma_addr + (TSOH_BUFFER(tsoh) - base_kva);
618 tsoh->next = tx_queue->tso_headers_free;
619 tx_queue->tso_headers_free = tsoh;
620 }
621
622 return 0;
623}
624
625
626/* Free up a TSO header, and all others in the same page. */
627static void efx_tsoh_block_free(struct efx_tx_queue *tx_queue,
628 struct efx_tso_header *tsoh,
629 struct pci_dev *pci_dev)
630{
631 struct efx_tso_header **p;
632 unsigned long base_kva;
633 dma_addr_t base_dma;
634
635 base_kva = (unsigned long)tsoh & PAGE_MASK;
636 base_dma = tsoh->dma_addr & PAGE_MASK;
637
638 p = &tx_queue->tso_headers_free;
Ben Hutchingsb3475642008-05-16 21:15:49 +0100639 while (*p != NULL) {
Ben Hutchingsb9b39b62008-05-07 12:51:12 +0100640 if (((unsigned long)*p & PAGE_MASK) == base_kva)
641 *p = (*p)->next;
642 else
643 p = &(*p)->next;
Ben Hutchingsb3475642008-05-16 21:15:49 +0100644 }
Ben Hutchingsb9b39b62008-05-07 12:51:12 +0100645
646 pci_free_consistent(pci_dev, PAGE_SIZE, (void *)base_kva, base_dma);
647}
648
649static struct efx_tso_header *
650efx_tsoh_heap_alloc(struct efx_tx_queue *tx_queue, size_t header_len)
651{
652 struct efx_tso_header *tsoh;
653
654 tsoh = kmalloc(TSOH_SIZE(header_len), GFP_ATOMIC | GFP_DMA);
655 if (unlikely(!tsoh))
656 return NULL;
657
658 tsoh->dma_addr = pci_map_single(tx_queue->efx->pci_dev,
659 TSOH_BUFFER(tsoh), header_len,
660 PCI_DMA_TODEVICE);
FUJITA Tomonori8d8bb392008-07-25 19:44:49 -0700661 if (unlikely(pci_dma_mapping_error(tx_queue->efx->pci_dev,
662 tsoh->dma_addr))) {
Ben Hutchingsb9b39b62008-05-07 12:51:12 +0100663 kfree(tsoh);
664 return NULL;
665 }
666
667 tsoh->unmap_len = header_len;
668 return tsoh;
669}
670
671static void
672efx_tsoh_heap_free(struct efx_tx_queue *tx_queue, struct efx_tso_header *tsoh)
673{
674 pci_unmap_single(tx_queue->efx->pci_dev,
675 tsoh->dma_addr, tsoh->unmap_len,
676 PCI_DMA_TODEVICE);
677 kfree(tsoh);
678}
679
680/**
681 * efx_tx_queue_insert - push descriptors onto the TX queue
682 * @tx_queue: Efx TX queue
683 * @dma_addr: DMA address of fragment
684 * @len: Length of fragment
Ben Hutchingsecbd95c2008-09-01 12:46:40 +0100685 * @final_buffer: The final buffer inserted into the queue
Ben Hutchingsb9b39b62008-05-07 12:51:12 +0100686 *
687 * Push descriptors onto the TX queue. Return 0 on success or 1 if
688 * @tx_queue full.
689 */
690static int efx_tx_queue_insert(struct efx_tx_queue *tx_queue,
691 dma_addr_t dma_addr, unsigned len,
Ben Hutchingsecbd95c2008-09-01 12:46:40 +0100692 struct efx_tx_buffer **final_buffer)
Ben Hutchingsb9b39b62008-05-07 12:51:12 +0100693{
694 struct efx_tx_buffer *buffer;
695 struct efx_nic *efx = tx_queue->efx;
696 unsigned dma_len, fill_level, insert_ptr, misalign;
697 int q_space;
698
699 EFX_BUG_ON_PARANOID(len <= 0);
700
701 fill_level = tx_queue->insert_count - tx_queue->old_read_count;
702 /* -1 as there is no way to represent all descriptors used */
703 q_space = efx->type->txd_ring_mask - 1 - fill_level;
704
705 while (1) {
706 if (unlikely(q_space-- <= 0)) {
707 /* It might be that completions have happened
708 * since the xmit path last checked. Update
709 * the xmit path's copy of read_count.
710 */
711 ++tx_queue->stopped;
712 /* This memory barrier protects the change of
713 * stopped from the access of read_count. */
714 smp_mb();
715 tx_queue->old_read_count =
716 *(volatile unsigned *)&tx_queue->read_count;
717 fill_level = (tx_queue->insert_count
718 - tx_queue->old_read_count);
719 q_space = efx->type->txd_ring_mask - 1 - fill_level;
Ben Hutchingsecbd95c2008-09-01 12:46:40 +0100720 if (unlikely(q_space-- <= 0)) {
721 *final_buffer = NULL;
Ben Hutchingsb9b39b62008-05-07 12:51:12 +0100722 return 1;
Ben Hutchingsecbd95c2008-09-01 12:46:40 +0100723 }
Ben Hutchingsb9b39b62008-05-07 12:51:12 +0100724 smp_mb();
725 --tx_queue->stopped;
726 }
727
728 insert_ptr = tx_queue->insert_count & efx->type->txd_ring_mask;
729 buffer = &tx_queue->buffer[insert_ptr];
730 ++tx_queue->insert_count;
731
732 EFX_BUG_ON_PARANOID(tx_queue->insert_count -
733 tx_queue->read_count >
734 efx->type->txd_ring_mask);
735
736 efx_tsoh_free(tx_queue, buffer);
737 EFX_BUG_ON_PARANOID(buffer->len);
738 EFX_BUG_ON_PARANOID(buffer->unmap_len);
739 EFX_BUG_ON_PARANOID(buffer->skb);
Ben Hutchingsdc8cfa52008-09-01 12:46:50 +0100740 EFX_BUG_ON_PARANOID(!buffer->continuation);
Ben Hutchingsb9b39b62008-05-07 12:51:12 +0100741 EFX_BUG_ON_PARANOID(buffer->tsoh);
742
743 buffer->dma_addr = dma_addr;
744
745 /* Ensure we do not cross a boundary unsupported by H/W */
746 dma_len = (~dma_addr & efx->type->tx_dma_mask) + 1;
747
748 misalign = (unsigned)dma_addr & efx->type->bug5391_mask;
749 if (misalign && dma_len + misalign > 512)
750 dma_len = 512 - misalign;
751
752 /* If there is enough space to send then do so */
753 if (dma_len >= len)
754 break;
755
756 buffer->len = dma_len; /* Don't set the other members */
757 dma_addr += dma_len;
758 len -= dma_len;
759 }
760
761 EFX_BUG_ON_PARANOID(!len);
762 buffer->len = len;
Ben Hutchingsecbd95c2008-09-01 12:46:40 +0100763 *final_buffer = buffer;
Ben Hutchingsb9b39b62008-05-07 12:51:12 +0100764 return 0;
765}
766
767
768/*
769 * Put a TSO header into the TX queue.
770 *
771 * This is special-cased because we know that it is small enough to fit in
772 * a single fragment, and we know it doesn't cross a page boundary. It
773 * also allows us to not worry about end-of-packet etc.
774 */
Ben Hutchings4d566062008-09-01 12:47:12 +0100775static void efx_tso_put_header(struct efx_tx_queue *tx_queue,
776 struct efx_tso_header *tsoh, unsigned len)
Ben Hutchingsb9b39b62008-05-07 12:51:12 +0100777{
778 struct efx_tx_buffer *buffer;
779
780 buffer = &tx_queue->buffer[tx_queue->insert_count &
781 tx_queue->efx->type->txd_ring_mask];
782 efx_tsoh_free(tx_queue, buffer);
783 EFX_BUG_ON_PARANOID(buffer->len);
784 EFX_BUG_ON_PARANOID(buffer->unmap_len);
785 EFX_BUG_ON_PARANOID(buffer->skb);
Ben Hutchingsdc8cfa52008-09-01 12:46:50 +0100786 EFX_BUG_ON_PARANOID(!buffer->continuation);
Ben Hutchingsb9b39b62008-05-07 12:51:12 +0100787 EFX_BUG_ON_PARANOID(buffer->tsoh);
788 buffer->len = len;
789 buffer->dma_addr = tsoh->dma_addr;
790 buffer->tsoh = tsoh;
791
792 ++tx_queue->insert_count;
793}
794
795
796/* Remove descriptors put into a tx_queue. */
797static void efx_enqueue_unwind(struct efx_tx_queue *tx_queue)
798{
799 struct efx_tx_buffer *buffer;
Ben Hutchingscc12dac2008-09-01 12:46:43 +0100800 dma_addr_t unmap_addr;
Ben Hutchingsb9b39b62008-05-07 12:51:12 +0100801
802 /* Work backwards until we hit the original insert pointer value */
803 while (tx_queue->insert_count != tx_queue->write_count) {
804 --tx_queue->insert_count;
805 buffer = &tx_queue->buffer[tx_queue->insert_count &
806 tx_queue->efx->type->txd_ring_mask];
807 efx_tsoh_free(tx_queue, buffer);
808 EFX_BUG_ON_PARANOID(buffer->skb);
809 buffer->len = 0;
Ben Hutchingsdc8cfa52008-09-01 12:46:50 +0100810 buffer->continuation = true;
Ben Hutchingsb9b39b62008-05-07 12:51:12 +0100811 if (buffer->unmap_len) {
Ben Hutchingscc12dac2008-09-01 12:46:43 +0100812 unmap_addr = (buffer->dma_addr + buffer->len -
813 buffer->unmap_len);
Ben Hutchingsecbd95c2008-09-01 12:46:40 +0100814 if (buffer->unmap_single)
815 pci_unmap_single(tx_queue->efx->pci_dev,
Ben Hutchingscc12dac2008-09-01 12:46:43 +0100816 unmap_addr, buffer->unmap_len,
Ben Hutchingsecbd95c2008-09-01 12:46:40 +0100817 PCI_DMA_TODEVICE);
818 else
819 pci_unmap_page(tx_queue->efx->pci_dev,
Ben Hutchingscc12dac2008-09-01 12:46:43 +0100820 unmap_addr, buffer->unmap_len,
Ben Hutchingsecbd95c2008-09-01 12:46:40 +0100821 PCI_DMA_TODEVICE);
Ben Hutchingsb9b39b62008-05-07 12:51:12 +0100822 buffer->unmap_len = 0;
823 }
824 }
825}
826
827
828/* Parse the SKB header and initialise state. */
Ben Hutchings4d566062008-09-01 12:47:12 +0100829static void tso_start(struct tso_state *st, const struct sk_buff *skb)
Ben Hutchingsb9b39b62008-05-07 12:51:12 +0100830{
831 /* All ethernet/IP/TCP headers combined size is TCP header size
832 * plus offset of TCP header relative to start of packet.
833 */
Ben Hutchings23d9e602008-09-01 12:47:02 +0100834 st->header_len = ((tcp_hdr(skb)->doff << 2u)
835 + PTR_DIFF(tcp_hdr(skb), skb->data));
836 st->full_packet_size = st->header_len + skb_shinfo(skb)->gso_size;
Ben Hutchingsb9b39b62008-05-07 12:51:12 +0100837
Ben Hutchings23d9e602008-09-01 12:47:02 +0100838 st->ipv4_id = ntohs(ip_hdr(skb)->id);
Ben Hutchingsb9b39b62008-05-07 12:51:12 +0100839 st->seqnum = ntohl(tcp_hdr(skb)->seq);
840
841 EFX_BUG_ON_PARANOID(tcp_hdr(skb)->urg);
842 EFX_BUG_ON_PARANOID(tcp_hdr(skb)->syn);
843 EFX_BUG_ON_PARANOID(tcp_hdr(skb)->rst);
844
Ben Hutchings23d9e602008-09-01 12:47:02 +0100845 st->packet_space = st->full_packet_size;
846 st->out_len = skb->len - st->header_len;
847 st->unmap_len = 0;
848 st->unmap_single = false;
Ben Hutchingsb9b39b62008-05-07 12:51:12 +0100849}
850
Ben Hutchings4d566062008-09-01 12:47:12 +0100851static int tso_get_fragment(struct tso_state *st, struct efx_nic *efx,
852 skb_frag_t *frag)
Ben Hutchingsb9b39b62008-05-07 12:51:12 +0100853{
Ben Hutchings23d9e602008-09-01 12:47:02 +0100854 st->unmap_addr = pci_map_page(efx->pci_dev, frag->page,
855 frag->page_offset, frag->size,
856 PCI_DMA_TODEVICE);
857 if (likely(!pci_dma_mapping_error(efx->pci_dev, st->unmap_addr))) {
858 st->unmap_single = false;
859 st->unmap_len = frag->size;
860 st->in_len = frag->size;
861 st->dma_addr = st->unmap_addr;
Ben Hutchingsecbd95c2008-09-01 12:46:40 +0100862 return 0;
863 }
864 return -ENOMEM;
865}
866
Ben Hutchings4d566062008-09-01 12:47:12 +0100867static int tso_get_head_fragment(struct tso_state *st, struct efx_nic *efx,
868 const struct sk_buff *skb)
Ben Hutchingsecbd95c2008-09-01 12:46:40 +0100869{
Ben Hutchings23d9e602008-09-01 12:47:02 +0100870 int hl = st->header_len;
Ben Hutchingsecbd95c2008-09-01 12:46:40 +0100871 int len = skb_headlen(skb) - hl;
872
Ben Hutchings23d9e602008-09-01 12:47:02 +0100873 st->unmap_addr = pci_map_single(efx->pci_dev, skb->data + hl,
874 len, PCI_DMA_TODEVICE);
875 if (likely(!pci_dma_mapping_error(efx->pci_dev, st->unmap_addr))) {
876 st->unmap_single = true;
877 st->unmap_len = len;
878 st->in_len = len;
879 st->dma_addr = st->unmap_addr;
Ben Hutchingsb9b39b62008-05-07 12:51:12 +0100880 return 0;
881 }
882 return -ENOMEM;
883}
884
885
886/**
887 * tso_fill_packet_with_fragment - form descriptors for the current fragment
888 * @tx_queue: Efx TX queue
889 * @skb: Socket buffer
890 * @st: TSO state
891 *
892 * Form descriptors for the current fragment, until we reach the end
893 * of fragment or end-of-packet. Return 0 on success, 1 if not enough
894 * space in @tx_queue.
895 */
Ben Hutchings4d566062008-09-01 12:47:12 +0100896static int tso_fill_packet_with_fragment(struct efx_tx_queue *tx_queue,
897 const struct sk_buff *skb,
898 struct tso_state *st)
Ben Hutchingsb9b39b62008-05-07 12:51:12 +0100899{
Ben Hutchingsecbd95c2008-09-01 12:46:40 +0100900 struct efx_tx_buffer *buffer;
Ben Hutchingsb9b39b62008-05-07 12:51:12 +0100901 int n, end_of_packet, rc;
902
Ben Hutchings23d9e602008-09-01 12:47:02 +0100903 if (st->in_len == 0)
Ben Hutchingsb9b39b62008-05-07 12:51:12 +0100904 return 0;
905 if (st->packet_space == 0)
906 return 0;
907
Ben Hutchings23d9e602008-09-01 12:47:02 +0100908 EFX_BUG_ON_PARANOID(st->in_len <= 0);
Ben Hutchingsb9b39b62008-05-07 12:51:12 +0100909 EFX_BUG_ON_PARANOID(st->packet_space <= 0);
910
Ben Hutchings23d9e602008-09-01 12:47:02 +0100911 n = min(st->in_len, st->packet_space);
Ben Hutchingsb9b39b62008-05-07 12:51:12 +0100912
913 st->packet_space -= n;
Ben Hutchings23d9e602008-09-01 12:47:02 +0100914 st->out_len -= n;
915 st->in_len -= n;
Ben Hutchingsb9b39b62008-05-07 12:51:12 +0100916
Ben Hutchings23d9e602008-09-01 12:47:02 +0100917 rc = efx_tx_queue_insert(tx_queue, st->dma_addr, n, &buffer);
Ben Hutchingsecbd95c2008-09-01 12:46:40 +0100918 if (likely(rc == 0)) {
Ben Hutchings23d9e602008-09-01 12:47:02 +0100919 if (st->out_len == 0)
Ben Hutchingsecbd95c2008-09-01 12:46:40 +0100920 /* Transfer ownership of the skb */
921 buffer->skb = skb;
922
Ben Hutchings23d9e602008-09-01 12:47:02 +0100923 end_of_packet = st->out_len == 0 || st->packet_space == 0;
Ben Hutchingsecbd95c2008-09-01 12:46:40 +0100924 buffer->continuation = !end_of_packet;
925
Ben Hutchings23d9e602008-09-01 12:47:02 +0100926 if (st->in_len == 0) {
Ben Hutchingsecbd95c2008-09-01 12:46:40 +0100927 /* Transfer ownership of the pci mapping */
Ben Hutchings23d9e602008-09-01 12:47:02 +0100928 buffer->unmap_len = st->unmap_len;
929 buffer->unmap_single = st->unmap_single;
930 st->unmap_len = 0;
Ben Hutchingsecbd95c2008-09-01 12:46:40 +0100931 }
932 }
Ben Hutchingsb9b39b62008-05-07 12:51:12 +0100933
Ben Hutchings23d9e602008-09-01 12:47:02 +0100934 st->dma_addr += n;
Ben Hutchingsb9b39b62008-05-07 12:51:12 +0100935 return rc;
936}
937
938
939/**
940 * tso_start_new_packet - generate a new header and prepare for the new packet
941 * @tx_queue: Efx TX queue
942 * @skb: Socket buffer
943 * @st: TSO state
944 *
945 * Generate a new header and prepare for the new packet. Return 0 on
946 * success, or -1 if failed to alloc header.
947 */
Ben Hutchings4d566062008-09-01 12:47:12 +0100948static int tso_start_new_packet(struct efx_tx_queue *tx_queue,
949 const struct sk_buff *skb,
950 struct tso_state *st)
Ben Hutchingsb9b39b62008-05-07 12:51:12 +0100951{
952 struct efx_tso_header *tsoh;
953 struct iphdr *tsoh_iph;
954 struct tcphdr *tsoh_th;
955 unsigned ip_length;
956 u8 *header;
957
958 /* Allocate a DMA-mapped header buffer. */
Ben Hutchings23d9e602008-09-01 12:47:02 +0100959 if (likely(TSOH_SIZE(st->header_len) <= TSOH_STD_SIZE)) {
Ben Hutchingsb3475642008-05-16 21:15:49 +0100960 if (tx_queue->tso_headers_free == NULL) {
Ben Hutchingsb9b39b62008-05-07 12:51:12 +0100961 if (efx_tsoh_block_alloc(tx_queue))
962 return -1;
Ben Hutchingsb3475642008-05-16 21:15:49 +0100963 }
Ben Hutchingsb9b39b62008-05-07 12:51:12 +0100964 EFX_BUG_ON_PARANOID(!tx_queue->tso_headers_free);
965 tsoh = tx_queue->tso_headers_free;
966 tx_queue->tso_headers_free = tsoh->next;
967 tsoh->unmap_len = 0;
968 } else {
969 tx_queue->tso_long_headers++;
Ben Hutchings23d9e602008-09-01 12:47:02 +0100970 tsoh = efx_tsoh_heap_alloc(tx_queue, st->header_len);
Ben Hutchingsb9b39b62008-05-07 12:51:12 +0100971 if (unlikely(!tsoh))
972 return -1;
973 }
974
975 header = TSOH_BUFFER(tsoh);
976 tsoh_th = (struct tcphdr *)(header + SKB_TCP_OFF(skb));
977 tsoh_iph = (struct iphdr *)(header + SKB_IPV4_OFF(skb));
978
979 /* Copy and update the headers. */
Ben Hutchings23d9e602008-09-01 12:47:02 +0100980 memcpy(header, skb->data, st->header_len);
Ben Hutchingsb9b39b62008-05-07 12:51:12 +0100981
982 tsoh_th->seq = htonl(st->seqnum);
983 st->seqnum += skb_shinfo(skb)->gso_size;
Ben Hutchings23d9e602008-09-01 12:47:02 +0100984 if (st->out_len > skb_shinfo(skb)->gso_size) {
Ben Hutchingsb9b39b62008-05-07 12:51:12 +0100985 /* This packet will not finish the TSO burst. */
Ben Hutchings23d9e602008-09-01 12:47:02 +0100986 ip_length = st->full_packet_size - ETH_HDR_LEN(skb);
Ben Hutchingsb9b39b62008-05-07 12:51:12 +0100987 tsoh_th->fin = 0;
988 tsoh_th->psh = 0;
989 } else {
990 /* This packet will be the last in the TSO burst. */
Ben Hutchings23d9e602008-09-01 12:47:02 +0100991 ip_length = st->header_len - ETH_HDR_LEN(skb) + st->out_len;
Ben Hutchingsb9b39b62008-05-07 12:51:12 +0100992 tsoh_th->fin = tcp_hdr(skb)->fin;
993 tsoh_th->psh = tcp_hdr(skb)->psh;
994 }
995 tsoh_iph->tot_len = htons(ip_length);
996
997 /* Linux leaves suitable gaps in the IP ID space for us to fill. */
Ben Hutchings23d9e602008-09-01 12:47:02 +0100998 tsoh_iph->id = htons(st->ipv4_id);
999 st->ipv4_id++;
Ben Hutchingsb9b39b62008-05-07 12:51:12 +01001000
1001 st->packet_space = skb_shinfo(skb)->gso_size;
1002 ++tx_queue->tso_packets;
1003
1004 /* Form a descriptor for this header. */
Ben Hutchings23d9e602008-09-01 12:47:02 +01001005 efx_tso_put_header(tx_queue, tsoh, st->header_len);
Ben Hutchingsb9b39b62008-05-07 12:51:12 +01001006
1007 return 0;
1008}
1009
1010
1011/**
1012 * efx_enqueue_skb_tso - segment and transmit a TSO socket buffer
1013 * @tx_queue: Efx TX queue
1014 * @skb: Socket buffer
1015 *
1016 * Context: You must hold netif_tx_lock() to call this function.
1017 *
1018 * Add socket buffer @skb to @tx_queue, doing TSO or return != 0 if
1019 * @skb was not enqueued. In all cases @skb is consumed. Return
1020 * %NETDEV_TX_OK or %NETDEV_TX_BUSY.
1021 */
1022static int efx_enqueue_skb_tso(struct efx_tx_queue *tx_queue,
1023 const struct sk_buff *skb)
1024{
Ben Hutchingsecbd95c2008-09-01 12:46:40 +01001025 struct efx_nic *efx = tx_queue->efx;
Ben Hutchingsb9b39b62008-05-07 12:51:12 +01001026 int frag_i, rc, rc2 = NETDEV_TX_OK;
1027 struct tso_state state;
Ben Hutchingsb9b39b62008-05-07 12:51:12 +01001028
1029 /* Verify TSO is safe - these checks should never fail. */
1030 efx_tso_check_safe(skb);
1031
1032 EFX_BUG_ON_PARANOID(tx_queue->write_count != tx_queue->insert_count);
1033
1034 tso_start(&state, skb);
1035
1036 /* Assume that skb header area contains exactly the headers, and
1037 * all payload is in the frag list.
1038 */
Ben Hutchings23d9e602008-09-01 12:47:02 +01001039 if (skb_headlen(skb) == state.header_len) {
Ben Hutchingsb9b39b62008-05-07 12:51:12 +01001040 /* Grab the first payload fragment. */
1041 EFX_BUG_ON_PARANOID(skb_shinfo(skb)->nr_frags < 1);
1042 frag_i = 0;
Ben Hutchingsecbd95c2008-09-01 12:46:40 +01001043 rc = tso_get_fragment(&state, efx,
1044 skb_shinfo(skb)->frags + frag_i);
Ben Hutchingsb9b39b62008-05-07 12:51:12 +01001045 if (rc)
1046 goto mem_err;
1047 } else {
Ben Hutchingsecbd95c2008-09-01 12:46:40 +01001048 rc = tso_get_head_fragment(&state, efx, skb);
Ben Hutchingsb9b39b62008-05-07 12:51:12 +01001049 if (rc)
1050 goto mem_err;
1051 frag_i = -1;
1052 }
1053
1054 if (tso_start_new_packet(tx_queue, skb, &state) < 0)
1055 goto mem_err;
1056
1057 while (1) {
1058 rc = tso_fill_packet_with_fragment(tx_queue, skb, &state);
1059 if (unlikely(rc))
1060 goto stop;
1061
1062 /* Move onto the next fragment? */
Ben Hutchings23d9e602008-09-01 12:47:02 +01001063 if (state.in_len == 0) {
Ben Hutchingsb9b39b62008-05-07 12:51:12 +01001064 if (++frag_i >= skb_shinfo(skb)->nr_frags)
1065 /* End of payload reached. */
1066 break;
Ben Hutchingsecbd95c2008-09-01 12:46:40 +01001067 rc = tso_get_fragment(&state, efx,
1068 skb_shinfo(skb)->frags + frag_i);
Ben Hutchingsb9b39b62008-05-07 12:51:12 +01001069 if (rc)
1070 goto mem_err;
1071 }
1072
1073 /* Start at new packet? */
1074 if (state.packet_space == 0 &&
1075 tso_start_new_packet(tx_queue, skb, &state) < 0)
1076 goto mem_err;
1077 }
1078
1079 /* Pass off to hardware */
1080 falcon_push_buffers(tx_queue);
1081
1082 tx_queue->tso_bursts++;
1083 return NETDEV_TX_OK;
1084
1085 mem_err:
Ben Hutchingsecbd95c2008-09-01 12:46:40 +01001086 EFX_ERR(efx, "Out of memory for TSO headers, or PCI mapping error\n");
Ben Hutchingsb9b39b62008-05-07 12:51:12 +01001087 dev_kfree_skb_any((struct sk_buff *)skb);
1088 goto unwind;
1089
1090 stop:
1091 rc2 = NETDEV_TX_BUSY;
1092
1093 /* Stop the queue if it wasn't stopped before. */
1094 if (tx_queue->stopped == 1)
Ben Hutchingsecbd95c2008-09-01 12:46:40 +01001095 efx_stop_queue(efx);
Ben Hutchingsb9b39b62008-05-07 12:51:12 +01001096
1097 unwind:
Ben Hutchings5988b632008-09-01 12:46:36 +01001098 /* Free the DMA mapping we were in the process of writing out */
Ben Hutchings23d9e602008-09-01 12:47:02 +01001099 if (state.unmap_len) {
1100 if (state.unmap_single)
1101 pci_unmap_single(efx->pci_dev, state.unmap_addr,
1102 state.unmap_len, PCI_DMA_TODEVICE);
Ben Hutchingsecbd95c2008-09-01 12:46:40 +01001103 else
Ben Hutchings23d9e602008-09-01 12:47:02 +01001104 pci_unmap_page(efx->pci_dev, state.unmap_addr,
1105 state.unmap_len, PCI_DMA_TODEVICE);
Ben Hutchingsecbd95c2008-09-01 12:46:40 +01001106 }
Ben Hutchings5988b632008-09-01 12:46:36 +01001107
Ben Hutchingsb9b39b62008-05-07 12:51:12 +01001108 efx_enqueue_unwind(tx_queue);
1109 return rc2;
1110}
1111
1112
1113/*
1114 * Free up all TSO datastructures associated with tx_queue. This
1115 * routine should be called only once the tx_queue is both empty and
1116 * will no longer be used.
1117 */
1118static void efx_fini_tso(struct efx_tx_queue *tx_queue)
1119{
1120 unsigned i;
1121
Ben Hutchingsb3475642008-05-16 21:15:49 +01001122 if (tx_queue->buffer) {
Ben Hutchingsb9b39b62008-05-07 12:51:12 +01001123 for (i = 0; i <= tx_queue->efx->type->txd_ring_mask; ++i)
1124 efx_tsoh_free(tx_queue, &tx_queue->buffer[i]);
Ben Hutchingsb3475642008-05-16 21:15:49 +01001125 }
Ben Hutchingsb9b39b62008-05-07 12:51:12 +01001126
1127 while (tx_queue->tso_headers_free != NULL)
1128 efx_tsoh_block_free(tx_queue, tx_queue->tso_headers_free,
1129 tx_queue->efx->pci_dev);
1130}