blob: ae554eec0563af66362558db9a2c75de5c22929a [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 */
Ben Hutchings3ffeabd2009-10-23 08:30:58 +000029#define EFX_TXQ_THRESHOLD (EFX_TXQ_MASK / 2u)
Ben Hutchings8ceee662008-04-27 12:55:59 +010030
31/* We want to be able to nest calls to netif_stop_queue(), since each
32 * channel can have an individual stop on the queue.
33 */
34void efx_stop_queue(struct efx_nic *efx)
35{
36 spin_lock_bh(&efx->netif_stop_lock);
37 EFX_TRACE(efx, "stop TX queue\n");
38
39 atomic_inc(&efx->netif_stop_count);
40 netif_stop_queue(efx->net_dev);
41
42 spin_unlock_bh(&efx->netif_stop_lock);
43}
44
45/* Wake netif's TX queue
46 * We want to be able to nest calls to netif_stop_queue(), since each
47 * channel can have an individual stop on the queue.
48 */
Ben Hutchings4d566062008-09-01 12:47:12 +010049void efx_wake_queue(struct efx_nic *efx)
Ben Hutchings8ceee662008-04-27 12:55:59 +010050{
51 local_bh_disable();
52 if (atomic_dec_and_lock(&efx->netif_stop_count,
53 &efx->netif_stop_lock)) {
54 EFX_TRACE(efx, "waking TX queue\n");
55 netif_wake_queue(efx->net_dev);
56 spin_unlock(&efx->netif_stop_lock);
57 }
58 local_bh_enable();
59}
60
Ben Hutchings4d566062008-09-01 12:47:12 +010061static void efx_dequeue_buffer(struct efx_tx_queue *tx_queue,
62 struct efx_tx_buffer *buffer)
Ben Hutchings8ceee662008-04-27 12:55:59 +010063{
64 if (buffer->unmap_len) {
65 struct pci_dev *pci_dev = tx_queue->efx->pci_dev;
Ben Hutchingscc12dac2008-09-01 12:46:43 +010066 dma_addr_t unmap_addr = (buffer->dma_addr + buffer->len -
67 buffer->unmap_len);
Ben Hutchings8ceee662008-04-27 12:55:59 +010068 if (buffer->unmap_single)
Ben Hutchingscc12dac2008-09-01 12:46:43 +010069 pci_unmap_single(pci_dev, unmap_addr, buffer->unmap_len,
70 PCI_DMA_TODEVICE);
Ben Hutchings8ceee662008-04-27 12:55:59 +010071 else
Ben Hutchingscc12dac2008-09-01 12:46:43 +010072 pci_unmap_page(pci_dev, unmap_addr, buffer->unmap_len,
73 PCI_DMA_TODEVICE);
Ben Hutchings8ceee662008-04-27 12:55:59 +010074 buffer->unmap_len = 0;
Ben Hutchingsdc8cfa52008-09-01 12:46:50 +010075 buffer->unmap_single = false;
Ben Hutchings8ceee662008-04-27 12:55:59 +010076 }
77
78 if (buffer->skb) {
79 dev_kfree_skb_any((struct sk_buff *) buffer->skb);
80 buffer->skb = NULL;
81 EFX_TRACE(tx_queue->efx, "TX queue %d transmission id %x "
82 "complete\n", tx_queue->queue, read_ptr);
83 }
84}
85
Ben Hutchingsb9b39b62008-05-07 12:51:12 +010086/**
87 * struct efx_tso_header - a DMA mapped buffer for packet headers
88 * @next: Linked list of free ones.
89 * The list is protected by the TX queue lock.
90 * @dma_unmap_len: Length to unmap for an oversize buffer, or 0.
91 * @dma_addr: The DMA address of the header below.
92 *
93 * This controls the memory used for a TSO header. Use TSOH_DATA()
94 * to find the packet header data. Use TSOH_SIZE() to calculate the
95 * total size required for a given packet header length. TSO headers
96 * in the free list are exactly %TSOH_STD_SIZE bytes in size.
97 */
98struct efx_tso_header {
99 union {
100 struct efx_tso_header *next;
101 size_t unmap_len;
102 };
103 dma_addr_t dma_addr;
104};
105
106static int efx_enqueue_skb_tso(struct efx_tx_queue *tx_queue,
Ben Hutchings740847d2008-09-01 12:48:23 +0100107 struct sk_buff *skb);
Ben Hutchingsb9b39b62008-05-07 12:51:12 +0100108static void efx_fini_tso(struct efx_tx_queue *tx_queue);
109static void efx_tsoh_heap_free(struct efx_tx_queue *tx_queue,
110 struct efx_tso_header *tsoh);
111
Ben Hutchings4d566062008-09-01 12:47:12 +0100112static void efx_tsoh_free(struct efx_tx_queue *tx_queue,
113 struct efx_tx_buffer *buffer)
Ben Hutchingsb9b39b62008-05-07 12:51:12 +0100114{
115 if (buffer->tsoh) {
116 if (likely(!buffer->tsoh->unmap_len)) {
117 buffer->tsoh->next = tx_queue->tso_headers_free;
118 tx_queue->tso_headers_free = buffer->tsoh;
119 } else {
120 efx_tsoh_heap_free(tx_queue, buffer->tsoh);
121 }
122 buffer->tsoh = NULL;
123 }
124}
125
Ben Hutchings8ceee662008-04-27 12:55:59 +0100126
127/*
128 * Add a socket buffer to a TX queue
129 *
130 * This maps all fragments of a socket buffer for DMA and adds them to
131 * the TX queue. The queue's insert pointer will be incremented by
132 * the number of fragments in the socket buffer.
133 *
134 * If any DMA mapping fails, any mapped fragments will be unmapped,
135 * the queue's insert pointer will be restored to its original value.
136 *
137 * Returns NETDEV_TX_OK or NETDEV_TX_BUSY
138 * You must hold netif_tx_lock() to call this function.
139 */
Stephen Hemminger613573252009-08-31 19:50:58 +0000140static netdev_tx_t efx_enqueue_skb(struct efx_tx_queue *tx_queue,
141 struct sk_buff *skb)
Ben Hutchings8ceee662008-04-27 12:55:59 +0100142{
143 struct efx_nic *efx = tx_queue->efx;
144 struct pci_dev *pci_dev = efx->pci_dev;
145 struct efx_tx_buffer *buffer;
146 skb_frag_t *fragment;
147 struct page *page;
148 int page_offset;
149 unsigned int len, unmap_len = 0, fill_level, insert_ptr, misalign;
150 dma_addr_t dma_addr, unmap_addr = 0;
151 unsigned int dma_len;
Ben Hutchingsdc8cfa52008-09-01 12:46:50 +0100152 bool unmap_single;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100153 int q_space, i = 0;
Stephen Hemminger613573252009-08-31 19:50:58 +0000154 netdev_tx_t rc = NETDEV_TX_OK;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100155
156 EFX_BUG_ON_PARANOID(tx_queue->write_count != tx_queue->insert_count);
157
Ben Hutchingsb9b39b62008-05-07 12:51:12 +0100158 if (skb_shinfo((struct sk_buff *)skb)->gso_size)
159 return efx_enqueue_skb_tso(tx_queue, skb);
160
Ben Hutchings8ceee662008-04-27 12:55:59 +0100161 /* Get size of the initial fragment */
162 len = skb_headlen(skb);
163
Ben Hutchingsbb145a92009-03-20 13:25:39 +0000164 /* Pad if necessary */
165 if (EFX_WORKAROUND_15592(efx) && skb->len <= 32) {
166 EFX_BUG_ON_PARANOID(skb->data_len);
167 len = 32 + 1;
168 if (skb_pad(skb, len - skb->len))
169 return NETDEV_TX_OK;
170 }
171
Ben Hutchings8ceee662008-04-27 12:55:59 +0100172 fill_level = tx_queue->insert_count - tx_queue->old_read_count;
Ben Hutchings3ffeabd2009-10-23 08:30:58 +0000173 q_space = EFX_TXQ_MASK - 1 - fill_level;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100174
175 /* Map for DMA. Use pci_map_single rather than pci_map_page
176 * since this is more efficient on machines with sparse
177 * memory.
178 */
Ben Hutchingsdc8cfa52008-09-01 12:46:50 +0100179 unmap_single = true;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100180 dma_addr = pci_map_single(pci_dev, skb->data, len, PCI_DMA_TODEVICE);
181
182 /* Process all fragments */
183 while (1) {
FUJITA Tomonori8d8bb392008-07-25 19:44:49 -0700184 if (unlikely(pci_dma_mapping_error(pci_dev, dma_addr)))
Ben Hutchings8ceee662008-04-27 12:55:59 +0100185 goto pci_err;
186
187 /* Store fields for marking in the per-fragment final
188 * descriptor */
189 unmap_len = len;
190 unmap_addr = dma_addr;
191
192 /* Add to TX queue, splitting across DMA boundaries */
193 do {
194 if (unlikely(q_space-- <= 0)) {
195 /* It might be that completions have
196 * happened since the xmit path last
197 * checked. Update the xmit path's
198 * copy of read_count.
199 */
200 ++tx_queue->stopped;
201 /* This memory barrier protects the
202 * change of stopped from the access
203 * of read_count. */
204 smp_mb();
205 tx_queue->old_read_count =
206 *(volatile unsigned *)
207 &tx_queue->read_count;
208 fill_level = (tx_queue->insert_count
209 - tx_queue->old_read_count);
Ben Hutchings3ffeabd2009-10-23 08:30:58 +0000210 q_space = EFX_TXQ_MASK - 1 - fill_level;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100211 if (unlikely(q_space-- <= 0))
212 goto stop;
213 smp_mb();
214 --tx_queue->stopped;
215 }
216
Ben Hutchings3ffeabd2009-10-23 08:30:58 +0000217 insert_ptr = tx_queue->insert_count & EFX_TXQ_MASK;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100218 buffer = &tx_queue->buffer[insert_ptr];
Ben Hutchingsb9b39b62008-05-07 12:51:12 +0100219 efx_tsoh_free(tx_queue, buffer);
220 EFX_BUG_ON_PARANOID(buffer->tsoh);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100221 EFX_BUG_ON_PARANOID(buffer->skb);
222 EFX_BUG_ON_PARANOID(buffer->len);
Ben Hutchingsdc8cfa52008-09-01 12:46:50 +0100223 EFX_BUG_ON_PARANOID(!buffer->continuation);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100224 EFX_BUG_ON_PARANOID(buffer->unmap_len);
225
226 dma_len = (((~dma_addr) & efx->type->tx_dma_mask) + 1);
227 if (likely(dma_len > len))
228 dma_len = len;
229
230 misalign = (unsigned)dma_addr & efx->type->bug5391_mask;
231 if (misalign && dma_len + misalign > 512)
232 dma_len = 512 - misalign;
233
234 /* Fill out per descriptor fields */
235 buffer->len = dma_len;
236 buffer->dma_addr = dma_addr;
237 len -= dma_len;
238 dma_addr += dma_len;
239 ++tx_queue->insert_count;
240 } while (len);
241
242 /* Transfer ownership of the unmapping to the final buffer */
Ben Hutchings8ceee662008-04-27 12:55:59 +0100243 buffer->unmap_single = unmap_single;
244 buffer->unmap_len = unmap_len;
245 unmap_len = 0;
246
247 /* Get address and size of next fragment */
248 if (i >= skb_shinfo(skb)->nr_frags)
249 break;
250 fragment = &skb_shinfo(skb)->frags[i];
251 len = fragment->size;
252 page = fragment->page;
253 page_offset = fragment->page_offset;
254 i++;
255 /* Map for DMA */
Ben Hutchingsdc8cfa52008-09-01 12:46:50 +0100256 unmap_single = false;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100257 dma_addr = pci_map_page(pci_dev, page, page_offset, len,
258 PCI_DMA_TODEVICE);
259 }
260
261 /* Transfer ownership of the skb to the final buffer */
262 buffer->skb = skb;
Ben Hutchingsdc8cfa52008-09-01 12:46:50 +0100263 buffer->continuation = false;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100264
265 /* Pass off to hardware */
266 falcon_push_buffers(tx_queue);
267
268 return NETDEV_TX_OK;
269
270 pci_err:
271 EFX_ERR_RL(efx, " TX queue %d could not map skb with %d bytes %d "
272 "fragments for DMA\n", tx_queue->queue, skb->len,
273 skb_shinfo(skb)->nr_frags + 1);
274
275 /* Mark the packet as transmitted, and free the SKB ourselves */
276 dev_kfree_skb_any((struct sk_buff *)skb);
277 goto unwind;
278
279 stop:
280 rc = NETDEV_TX_BUSY;
281
282 if (tx_queue->stopped == 1)
283 efx_stop_queue(efx);
284
285 unwind:
286 /* Work backwards until we hit the original insert pointer value */
287 while (tx_queue->insert_count != tx_queue->write_count) {
288 --tx_queue->insert_count;
Ben Hutchings3ffeabd2009-10-23 08:30:58 +0000289 insert_ptr = tx_queue->insert_count & EFX_TXQ_MASK;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100290 buffer = &tx_queue->buffer[insert_ptr];
291 efx_dequeue_buffer(tx_queue, buffer);
292 buffer->len = 0;
293 }
294
295 /* Free the fragment we were mid-way through pushing */
Ben Hutchingsecbd95c2008-09-01 12:46:40 +0100296 if (unmap_len) {
297 if (unmap_single)
298 pci_unmap_single(pci_dev, unmap_addr, unmap_len,
299 PCI_DMA_TODEVICE);
300 else
301 pci_unmap_page(pci_dev, unmap_addr, unmap_len,
302 PCI_DMA_TODEVICE);
303 }
Ben Hutchings8ceee662008-04-27 12:55:59 +0100304
305 return rc;
306}
307
308/* Remove packets from the TX queue
309 *
310 * This removes packets from the TX queue, up to and including the
311 * specified index.
312 */
Ben Hutchings4d566062008-09-01 12:47:12 +0100313static void efx_dequeue_buffers(struct efx_tx_queue *tx_queue,
314 unsigned int index)
Ben Hutchings8ceee662008-04-27 12:55:59 +0100315{
316 struct efx_nic *efx = tx_queue->efx;
317 unsigned int stop_index, read_ptr;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100318
Ben Hutchings3ffeabd2009-10-23 08:30:58 +0000319 stop_index = (index + 1) & EFX_TXQ_MASK;
320 read_ptr = tx_queue->read_count & EFX_TXQ_MASK;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100321
322 while (read_ptr != stop_index) {
323 struct efx_tx_buffer *buffer = &tx_queue->buffer[read_ptr];
324 if (unlikely(buffer->len == 0)) {
325 EFX_ERR(tx_queue->efx, "TX queue %d spurious TX "
326 "completion id %x\n", tx_queue->queue,
327 read_ptr);
328 efx_schedule_reset(efx, RESET_TYPE_TX_SKIP);
329 return;
330 }
331
332 efx_dequeue_buffer(tx_queue, buffer);
Ben Hutchingsdc8cfa52008-09-01 12:46:50 +0100333 buffer->continuation = true;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100334 buffer->len = 0;
335
336 ++tx_queue->read_count;
Ben Hutchings3ffeabd2009-10-23 08:30:58 +0000337 read_ptr = tx_queue->read_count & EFX_TXQ_MASK;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100338 }
339}
340
341/* Initiate a packet transmission on the specified TX queue.
342 * Note that returning anything other than NETDEV_TX_OK will cause the
343 * OS to free the skb.
344 *
345 * This function is split out from efx_hard_start_xmit to allow the
346 * loopback test to direct packets via specific TX queues. It is
347 * therefore a non-static inline, so as not to penalise performance
348 * for non-loopback transmissions.
349 *
350 * Context: netif_tx_lock held
351 */
Stephen Hemminger613573252009-08-31 19:50:58 +0000352inline netdev_tx_t efx_xmit(struct efx_nic *efx,
353 struct efx_tx_queue *tx_queue, struct sk_buff *skb)
Ben Hutchings8ceee662008-04-27 12:55:59 +0100354{
Ben Hutchings8ceee662008-04-27 12:55:59 +0100355 /* Map fragments for DMA and add to TX queue */
Stephen Hemminger613573252009-08-31 19:50:58 +0000356 return efx_enqueue_skb(tx_queue, skb);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100357}
358
359/* Initiate a packet transmission. We use one channel per CPU
360 * (sharing when we have more CPUs than channels). On Falcon, the TX
361 * completion events will be directed back to the CPU that transmitted
362 * the packet, which should be cache-efficient.
363 *
364 * Context: non-blocking.
365 * Note that returning anything other than NETDEV_TX_OK will cause the
366 * OS to free the skb.
367 */
Stephen Hemminger613573252009-08-31 19:50:58 +0000368netdev_tx_t efx_hard_start_xmit(struct sk_buff *skb,
369 struct net_device *net_dev)
Ben Hutchings8ceee662008-04-27 12:55:59 +0100370{
Ben Hutchings767e4682008-09-01 12:43:14 +0100371 struct efx_nic *efx = netdev_priv(net_dev);
Ben Hutchings60ac1062008-09-01 12:44:59 +0100372 struct efx_tx_queue *tx_queue;
373
Ben Hutchingsa7ef5932009-03-04 09:52:37 +0000374 if (unlikely(efx->port_inhibited))
375 return NETDEV_TX_BUSY;
376
Ben Hutchings60ac1062008-09-01 12:44:59 +0100377 if (likely(skb->ip_summed == CHECKSUM_PARTIAL))
378 tx_queue = &efx->tx_queue[EFX_TX_QUEUE_OFFLOAD_CSUM];
379 else
380 tx_queue = &efx->tx_queue[EFX_TX_QUEUE_NO_CSUM];
381
382 return efx_xmit(efx, tx_queue, skb);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100383}
384
385void efx_xmit_done(struct efx_tx_queue *tx_queue, unsigned int index)
386{
387 unsigned fill_level;
388 struct efx_nic *efx = tx_queue->efx;
389
Ben Hutchings3ffeabd2009-10-23 08:30:58 +0000390 EFX_BUG_ON_PARANOID(index > EFX_TXQ_MASK);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100391
392 efx_dequeue_buffers(tx_queue, index);
393
394 /* See if we need to restart the netif queue. This barrier
395 * separates the update of read_count from the test of
396 * stopped. */
397 smp_mb();
Ben Hutchings32d76002009-03-04 09:53:15 +0000398 if (unlikely(tx_queue->stopped) && likely(efx->port_enabled)) {
Ben Hutchings8ceee662008-04-27 12:55:59 +0100399 fill_level = tx_queue->insert_count - tx_queue->read_count;
Ben Hutchings3ffeabd2009-10-23 08:30:58 +0000400 if (fill_level < EFX_TXQ_THRESHOLD) {
Ben Hutchings55668612008-05-16 21:16:10 +0100401 EFX_BUG_ON_PARANOID(!efx_dev_registered(efx));
Ben Hutchings8ceee662008-04-27 12:55:59 +0100402
403 /* Do this under netif_tx_lock(), to avoid racing
404 * with efx_xmit(). */
405 netif_tx_lock(efx->net_dev);
406 if (tx_queue->stopped) {
407 tx_queue->stopped = 0;
408 efx_wake_queue(efx);
409 }
410 netif_tx_unlock(efx->net_dev);
411 }
412 }
413}
414
415int efx_probe_tx_queue(struct efx_tx_queue *tx_queue)
416{
417 struct efx_nic *efx = tx_queue->efx;
418 unsigned int txq_size;
419 int i, rc;
420
421 EFX_LOG(efx, "creating TX queue %d\n", tx_queue->queue);
422
423 /* Allocate software ring */
Ben Hutchings3ffeabd2009-10-23 08:30:58 +0000424 txq_size = EFX_TXQ_SIZE * sizeof(*tx_queue->buffer);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100425 tx_queue->buffer = kzalloc(txq_size, GFP_KERNEL);
Ben Hutchings60ac1062008-09-01 12:44:59 +0100426 if (!tx_queue->buffer)
427 return -ENOMEM;
Ben Hutchings3ffeabd2009-10-23 08:30:58 +0000428 for (i = 0; i <= EFX_TXQ_MASK; ++i)
Ben Hutchingsdc8cfa52008-09-01 12:46:50 +0100429 tx_queue->buffer[i].continuation = true;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100430
431 /* Allocate hardware ring */
432 rc = falcon_probe_tx(tx_queue);
433 if (rc)
Ben Hutchings60ac1062008-09-01 12:44:59 +0100434 goto fail;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100435
436 return 0;
437
Ben Hutchings60ac1062008-09-01 12:44:59 +0100438 fail:
Ben Hutchings8ceee662008-04-27 12:55:59 +0100439 kfree(tx_queue->buffer);
440 tx_queue->buffer = NULL;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100441 return rc;
442}
443
Ben Hutchingsbc3c90a2008-09-01 12:48:46 +0100444void efx_init_tx_queue(struct efx_tx_queue *tx_queue)
Ben Hutchings8ceee662008-04-27 12:55:59 +0100445{
446 EFX_LOG(tx_queue->efx, "initialising TX queue %d\n", tx_queue->queue);
447
448 tx_queue->insert_count = 0;
449 tx_queue->write_count = 0;
450 tx_queue->read_count = 0;
451 tx_queue->old_read_count = 0;
452 BUG_ON(tx_queue->stopped);
453
454 /* Set up TX descriptor ring */
Ben Hutchingsbc3c90a2008-09-01 12:48:46 +0100455 falcon_init_tx(tx_queue);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100456}
457
458void efx_release_tx_buffers(struct efx_tx_queue *tx_queue)
459{
460 struct efx_tx_buffer *buffer;
461
462 if (!tx_queue->buffer)
463 return;
464
465 /* Free any buffers left in the ring */
466 while (tx_queue->read_count != tx_queue->write_count) {
Ben Hutchings3ffeabd2009-10-23 08:30:58 +0000467 buffer = &tx_queue->buffer[tx_queue->read_count & EFX_TXQ_MASK];
Ben Hutchings8ceee662008-04-27 12:55:59 +0100468 efx_dequeue_buffer(tx_queue, buffer);
Ben Hutchingsdc8cfa52008-09-01 12:46:50 +0100469 buffer->continuation = true;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100470 buffer->len = 0;
471
472 ++tx_queue->read_count;
473 }
474}
475
476void efx_fini_tx_queue(struct efx_tx_queue *tx_queue)
477{
478 EFX_LOG(tx_queue->efx, "shutting down TX queue %d\n", tx_queue->queue);
479
480 /* Flush TX queue, remove descriptor ring */
481 falcon_fini_tx(tx_queue);
482
483 efx_release_tx_buffers(tx_queue);
484
Ben Hutchingsb9b39b62008-05-07 12:51:12 +0100485 /* Free up TSO header cache */
486 efx_fini_tso(tx_queue);
487
Ben Hutchings8ceee662008-04-27 12:55:59 +0100488 /* Release queue's stop on port, if any */
489 if (tx_queue->stopped) {
490 tx_queue->stopped = 0;
491 efx_wake_queue(tx_queue->efx);
492 }
493}
494
495void efx_remove_tx_queue(struct efx_tx_queue *tx_queue)
496{
497 EFX_LOG(tx_queue->efx, "destroying TX queue %d\n", tx_queue->queue);
498 falcon_remove_tx(tx_queue);
499
500 kfree(tx_queue->buffer);
501 tx_queue->buffer = NULL;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100502}
503
504
Ben Hutchingsb9b39b62008-05-07 12:51:12 +0100505/* Efx TCP segmentation acceleration.
506 *
507 * Why? Because by doing it here in the driver we can go significantly
508 * faster than the GSO.
509 *
510 * Requires TX checksum offload support.
511 */
512
513/* Number of bytes inserted at the start of a TSO header buffer,
514 * similar to NET_IP_ALIGN.
515 */
Ben Hutchings13e9ab12008-09-01 12:50:28 +0100516#ifdef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
Ben Hutchingsb9b39b62008-05-07 12:51:12 +0100517#define TSOH_OFFSET 0
518#else
519#define TSOH_OFFSET NET_IP_ALIGN
520#endif
521
522#define TSOH_BUFFER(tsoh) ((u8 *)(tsoh + 1) + TSOH_OFFSET)
523
524/* Total size of struct efx_tso_header, buffer and padding */
525#define TSOH_SIZE(hdr_len) \
526 (sizeof(struct efx_tso_header) + TSOH_OFFSET + hdr_len)
527
528/* Size of blocks on free list. Larger blocks must be allocated from
529 * the heap.
530 */
531#define TSOH_STD_SIZE 128
532
533#define PTR_DIFF(p1, p2) ((u8 *)(p1) - (u8 *)(p2))
534#define ETH_HDR_LEN(skb) (skb_network_header(skb) - (skb)->data)
535#define SKB_TCP_OFF(skb) PTR_DIFF(tcp_hdr(skb), (skb)->data)
536#define SKB_IPV4_OFF(skb) PTR_DIFF(ip_hdr(skb), (skb)->data)
537
538/**
539 * struct tso_state - TSO state for an SKB
Ben Hutchings23d9e602008-09-01 12:47:02 +0100540 * @out_len: Remaining length in current segment
Ben Hutchingsb9b39b62008-05-07 12:51:12 +0100541 * @seqnum: Current sequence number
Ben Hutchings23d9e602008-09-01 12:47:02 +0100542 * @ipv4_id: Current IPv4 ID, host endian
Ben Hutchingsb9b39b62008-05-07 12:51:12 +0100543 * @packet_space: Remaining space in current packet
Ben Hutchings23d9e602008-09-01 12:47:02 +0100544 * @dma_addr: DMA address of current position
545 * @in_len: Remaining length in current SKB fragment
546 * @unmap_len: Length of SKB fragment
547 * @unmap_addr: DMA address of SKB fragment
548 * @unmap_single: DMA single vs page mapping flag
549 * @header_len: Number of bytes of header
550 * @full_packet_size: Number of bytes to put in each outgoing segment
Ben Hutchingsb9b39b62008-05-07 12:51:12 +0100551 *
552 * The state used during segmentation. It is put into this data structure
553 * just to make it easy to pass into inline functions.
554 */
555struct tso_state {
Ben Hutchings23d9e602008-09-01 12:47:02 +0100556 /* Output position */
557 unsigned out_len;
Ben Hutchingsb9b39b62008-05-07 12:51:12 +0100558 unsigned seqnum;
Ben Hutchings23d9e602008-09-01 12:47:02 +0100559 unsigned ipv4_id;
Ben Hutchingsb9b39b62008-05-07 12:51:12 +0100560 unsigned packet_space;
561
Ben Hutchings23d9e602008-09-01 12:47:02 +0100562 /* Input position */
563 dma_addr_t dma_addr;
564 unsigned in_len;
565 unsigned unmap_len;
566 dma_addr_t unmap_addr;
567 bool unmap_single;
Ben Hutchingsb9b39b62008-05-07 12:51:12 +0100568
Ben Hutchings23d9e602008-09-01 12:47:02 +0100569 unsigned header_len;
570 int full_packet_size;
Ben Hutchingsb9b39b62008-05-07 12:51:12 +0100571};
572
573
574/*
575 * Verify that our various assumptions about sk_buffs and the conditions
576 * under which TSO will be attempted hold true.
577 */
Ben Hutchings740847d2008-09-01 12:48:23 +0100578static void efx_tso_check_safe(struct sk_buff *skb)
Ben Hutchingsb9b39b62008-05-07 12:51:12 +0100579{
Ben Hutchings740847d2008-09-01 12:48:23 +0100580 __be16 protocol = skb->protocol;
581
Ben Hutchingsb9b39b62008-05-07 12:51:12 +0100582 EFX_BUG_ON_PARANOID(((struct ethhdr *)skb->data)->h_proto !=
Ben Hutchings740847d2008-09-01 12:48:23 +0100583 protocol);
584 if (protocol == htons(ETH_P_8021Q)) {
585 /* Find the encapsulated protocol; reset network header
586 * and transport header based on that. */
587 struct vlan_ethhdr *veh = (struct vlan_ethhdr *)skb->data;
588 protocol = veh->h_vlan_encapsulated_proto;
589 skb_set_network_header(skb, sizeof(*veh));
590 if (protocol == htons(ETH_P_IP))
591 skb_set_transport_header(skb, sizeof(*veh) +
592 4 * ip_hdr(skb)->ihl);
593 }
594
595 EFX_BUG_ON_PARANOID(protocol != htons(ETH_P_IP));
Ben Hutchingsb9b39b62008-05-07 12:51:12 +0100596 EFX_BUG_ON_PARANOID(ip_hdr(skb)->protocol != IPPROTO_TCP);
597 EFX_BUG_ON_PARANOID((PTR_DIFF(tcp_hdr(skb), skb->data)
598 + (tcp_hdr(skb)->doff << 2u)) >
599 skb_headlen(skb));
600}
601
602
603/*
604 * Allocate a page worth of efx_tso_header structures, and string them
605 * into the tx_queue->tso_headers_free linked list. Return 0 or -ENOMEM.
606 */
607static int efx_tsoh_block_alloc(struct efx_tx_queue *tx_queue)
608{
609
610 struct pci_dev *pci_dev = tx_queue->efx->pci_dev;
611 struct efx_tso_header *tsoh;
612 dma_addr_t dma_addr;
613 u8 *base_kva, *kva;
614
615 base_kva = pci_alloc_consistent(pci_dev, PAGE_SIZE, &dma_addr);
616 if (base_kva == NULL) {
617 EFX_ERR(tx_queue->efx, "Unable to allocate page for TSO"
618 " headers\n");
619 return -ENOMEM;
620 }
621
622 /* pci_alloc_consistent() allocates pages. */
623 EFX_BUG_ON_PARANOID(dma_addr & (PAGE_SIZE - 1u));
624
625 for (kva = base_kva; kva < base_kva + PAGE_SIZE; kva += TSOH_STD_SIZE) {
626 tsoh = (struct efx_tso_header *)kva;
627 tsoh->dma_addr = dma_addr + (TSOH_BUFFER(tsoh) - base_kva);
628 tsoh->next = tx_queue->tso_headers_free;
629 tx_queue->tso_headers_free = tsoh;
630 }
631
632 return 0;
633}
634
635
636/* Free up a TSO header, and all others in the same page. */
637static void efx_tsoh_block_free(struct efx_tx_queue *tx_queue,
638 struct efx_tso_header *tsoh,
639 struct pci_dev *pci_dev)
640{
641 struct efx_tso_header **p;
642 unsigned long base_kva;
643 dma_addr_t base_dma;
644
645 base_kva = (unsigned long)tsoh & PAGE_MASK;
646 base_dma = tsoh->dma_addr & PAGE_MASK;
647
648 p = &tx_queue->tso_headers_free;
Ben Hutchingsb3475642008-05-16 21:15:49 +0100649 while (*p != NULL) {
Ben Hutchingsb9b39b62008-05-07 12:51:12 +0100650 if (((unsigned long)*p & PAGE_MASK) == base_kva)
651 *p = (*p)->next;
652 else
653 p = &(*p)->next;
Ben Hutchingsb3475642008-05-16 21:15:49 +0100654 }
Ben Hutchingsb9b39b62008-05-07 12:51:12 +0100655
656 pci_free_consistent(pci_dev, PAGE_SIZE, (void *)base_kva, base_dma);
657}
658
659static struct efx_tso_header *
660efx_tsoh_heap_alloc(struct efx_tx_queue *tx_queue, size_t header_len)
661{
662 struct efx_tso_header *tsoh;
663
664 tsoh = kmalloc(TSOH_SIZE(header_len), GFP_ATOMIC | GFP_DMA);
665 if (unlikely(!tsoh))
666 return NULL;
667
668 tsoh->dma_addr = pci_map_single(tx_queue->efx->pci_dev,
669 TSOH_BUFFER(tsoh), header_len,
670 PCI_DMA_TODEVICE);
FUJITA Tomonori8d8bb392008-07-25 19:44:49 -0700671 if (unlikely(pci_dma_mapping_error(tx_queue->efx->pci_dev,
672 tsoh->dma_addr))) {
Ben Hutchingsb9b39b62008-05-07 12:51:12 +0100673 kfree(tsoh);
674 return NULL;
675 }
676
677 tsoh->unmap_len = header_len;
678 return tsoh;
679}
680
681static void
682efx_tsoh_heap_free(struct efx_tx_queue *tx_queue, struct efx_tso_header *tsoh)
683{
684 pci_unmap_single(tx_queue->efx->pci_dev,
685 tsoh->dma_addr, tsoh->unmap_len,
686 PCI_DMA_TODEVICE);
687 kfree(tsoh);
688}
689
690/**
691 * efx_tx_queue_insert - push descriptors onto the TX queue
692 * @tx_queue: Efx TX queue
693 * @dma_addr: DMA address of fragment
694 * @len: Length of fragment
Ben Hutchingsecbd95c2008-09-01 12:46:40 +0100695 * @final_buffer: The final buffer inserted into the queue
Ben Hutchingsb9b39b62008-05-07 12:51:12 +0100696 *
697 * Push descriptors onto the TX queue. Return 0 on success or 1 if
698 * @tx_queue full.
699 */
700static int efx_tx_queue_insert(struct efx_tx_queue *tx_queue,
701 dma_addr_t dma_addr, unsigned len,
Ben Hutchingsecbd95c2008-09-01 12:46:40 +0100702 struct efx_tx_buffer **final_buffer)
Ben Hutchingsb9b39b62008-05-07 12:51:12 +0100703{
704 struct efx_tx_buffer *buffer;
705 struct efx_nic *efx = tx_queue->efx;
706 unsigned dma_len, fill_level, insert_ptr, misalign;
707 int q_space;
708
709 EFX_BUG_ON_PARANOID(len <= 0);
710
711 fill_level = tx_queue->insert_count - tx_queue->old_read_count;
712 /* -1 as there is no way to represent all descriptors used */
Ben Hutchings3ffeabd2009-10-23 08:30:58 +0000713 q_space = EFX_TXQ_MASK - 1 - fill_level;
Ben Hutchingsb9b39b62008-05-07 12:51:12 +0100714
715 while (1) {
716 if (unlikely(q_space-- <= 0)) {
717 /* It might be that completions have happened
718 * since the xmit path last checked. Update
719 * the xmit path's copy of read_count.
720 */
721 ++tx_queue->stopped;
722 /* This memory barrier protects the change of
723 * stopped from the access of read_count. */
724 smp_mb();
725 tx_queue->old_read_count =
726 *(volatile unsigned *)&tx_queue->read_count;
727 fill_level = (tx_queue->insert_count
728 - tx_queue->old_read_count);
Ben Hutchings3ffeabd2009-10-23 08:30:58 +0000729 q_space = EFX_TXQ_MASK - 1 - fill_level;
Ben Hutchingsecbd95c2008-09-01 12:46:40 +0100730 if (unlikely(q_space-- <= 0)) {
731 *final_buffer = NULL;
Ben Hutchingsb9b39b62008-05-07 12:51:12 +0100732 return 1;
Ben Hutchingsecbd95c2008-09-01 12:46:40 +0100733 }
Ben Hutchingsb9b39b62008-05-07 12:51:12 +0100734 smp_mb();
735 --tx_queue->stopped;
736 }
737
Ben Hutchings3ffeabd2009-10-23 08:30:58 +0000738 insert_ptr = tx_queue->insert_count & EFX_TXQ_MASK;
Ben Hutchingsb9b39b62008-05-07 12:51:12 +0100739 buffer = &tx_queue->buffer[insert_ptr];
740 ++tx_queue->insert_count;
741
742 EFX_BUG_ON_PARANOID(tx_queue->insert_count -
743 tx_queue->read_count >
Ben Hutchings3ffeabd2009-10-23 08:30:58 +0000744 EFX_TXQ_MASK);
Ben Hutchingsb9b39b62008-05-07 12:51:12 +0100745
746 efx_tsoh_free(tx_queue, buffer);
747 EFX_BUG_ON_PARANOID(buffer->len);
748 EFX_BUG_ON_PARANOID(buffer->unmap_len);
749 EFX_BUG_ON_PARANOID(buffer->skb);
Ben Hutchingsdc8cfa52008-09-01 12:46:50 +0100750 EFX_BUG_ON_PARANOID(!buffer->continuation);
Ben Hutchingsb9b39b62008-05-07 12:51:12 +0100751 EFX_BUG_ON_PARANOID(buffer->tsoh);
752
753 buffer->dma_addr = dma_addr;
754
755 /* Ensure we do not cross a boundary unsupported by H/W */
756 dma_len = (~dma_addr & efx->type->tx_dma_mask) + 1;
757
758 misalign = (unsigned)dma_addr & efx->type->bug5391_mask;
759 if (misalign && dma_len + misalign > 512)
760 dma_len = 512 - misalign;
761
762 /* If there is enough space to send then do so */
763 if (dma_len >= len)
764 break;
765
766 buffer->len = dma_len; /* Don't set the other members */
767 dma_addr += dma_len;
768 len -= dma_len;
769 }
770
771 EFX_BUG_ON_PARANOID(!len);
772 buffer->len = len;
Ben Hutchingsecbd95c2008-09-01 12:46:40 +0100773 *final_buffer = buffer;
Ben Hutchingsb9b39b62008-05-07 12:51:12 +0100774 return 0;
775}
776
777
778/*
779 * Put a TSO header into the TX queue.
780 *
781 * This is special-cased because we know that it is small enough to fit in
782 * a single fragment, and we know it doesn't cross a page boundary. It
783 * also allows us to not worry about end-of-packet etc.
784 */
Ben Hutchings4d566062008-09-01 12:47:12 +0100785static void efx_tso_put_header(struct efx_tx_queue *tx_queue,
786 struct efx_tso_header *tsoh, unsigned len)
Ben Hutchingsb9b39b62008-05-07 12:51:12 +0100787{
788 struct efx_tx_buffer *buffer;
789
Ben Hutchings3ffeabd2009-10-23 08:30:58 +0000790 buffer = &tx_queue->buffer[tx_queue->insert_count & EFX_TXQ_MASK];
Ben Hutchingsb9b39b62008-05-07 12:51:12 +0100791 efx_tsoh_free(tx_queue, buffer);
792 EFX_BUG_ON_PARANOID(buffer->len);
793 EFX_BUG_ON_PARANOID(buffer->unmap_len);
794 EFX_BUG_ON_PARANOID(buffer->skb);
Ben Hutchingsdc8cfa52008-09-01 12:46:50 +0100795 EFX_BUG_ON_PARANOID(!buffer->continuation);
Ben Hutchingsb9b39b62008-05-07 12:51:12 +0100796 EFX_BUG_ON_PARANOID(buffer->tsoh);
797 buffer->len = len;
798 buffer->dma_addr = tsoh->dma_addr;
799 buffer->tsoh = tsoh;
800
801 ++tx_queue->insert_count;
802}
803
804
805/* Remove descriptors put into a tx_queue. */
806static void efx_enqueue_unwind(struct efx_tx_queue *tx_queue)
807{
808 struct efx_tx_buffer *buffer;
Ben Hutchingscc12dac2008-09-01 12:46:43 +0100809 dma_addr_t unmap_addr;
Ben Hutchingsb9b39b62008-05-07 12:51:12 +0100810
811 /* Work backwards until we hit the original insert pointer value */
812 while (tx_queue->insert_count != tx_queue->write_count) {
813 --tx_queue->insert_count;
814 buffer = &tx_queue->buffer[tx_queue->insert_count &
Ben Hutchings3ffeabd2009-10-23 08:30:58 +0000815 EFX_TXQ_MASK];
Ben Hutchingsb9b39b62008-05-07 12:51:12 +0100816 efx_tsoh_free(tx_queue, buffer);
817 EFX_BUG_ON_PARANOID(buffer->skb);
818 buffer->len = 0;
Ben Hutchingsdc8cfa52008-09-01 12:46:50 +0100819 buffer->continuation = true;
Ben Hutchingsb9b39b62008-05-07 12:51:12 +0100820 if (buffer->unmap_len) {
Ben Hutchingscc12dac2008-09-01 12:46:43 +0100821 unmap_addr = (buffer->dma_addr + buffer->len -
822 buffer->unmap_len);
Ben Hutchingsecbd95c2008-09-01 12:46:40 +0100823 if (buffer->unmap_single)
824 pci_unmap_single(tx_queue->efx->pci_dev,
Ben Hutchingscc12dac2008-09-01 12:46:43 +0100825 unmap_addr, buffer->unmap_len,
Ben Hutchingsecbd95c2008-09-01 12:46:40 +0100826 PCI_DMA_TODEVICE);
827 else
828 pci_unmap_page(tx_queue->efx->pci_dev,
Ben Hutchingscc12dac2008-09-01 12:46:43 +0100829 unmap_addr, buffer->unmap_len,
Ben Hutchingsecbd95c2008-09-01 12:46:40 +0100830 PCI_DMA_TODEVICE);
Ben Hutchingsb9b39b62008-05-07 12:51:12 +0100831 buffer->unmap_len = 0;
832 }
833 }
834}
835
836
837/* Parse the SKB header and initialise state. */
Ben Hutchings4d566062008-09-01 12:47:12 +0100838static void tso_start(struct tso_state *st, const struct sk_buff *skb)
Ben Hutchingsb9b39b62008-05-07 12:51:12 +0100839{
840 /* All ethernet/IP/TCP headers combined size is TCP header size
841 * plus offset of TCP header relative to start of packet.
842 */
Ben Hutchings23d9e602008-09-01 12:47:02 +0100843 st->header_len = ((tcp_hdr(skb)->doff << 2u)
844 + PTR_DIFF(tcp_hdr(skb), skb->data));
845 st->full_packet_size = st->header_len + skb_shinfo(skb)->gso_size;
Ben Hutchingsb9b39b62008-05-07 12:51:12 +0100846
Ben Hutchings23d9e602008-09-01 12:47:02 +0100847 st->ipv4_id = ntohs(ip_hdr(skb)->id);
Ben Hutchingsb9b39b62008-05-07 12:51:12 +0100848 st->seqnum = ntohl(tcp_hdr(skb)->seq);
849
850 EFX_BUG_ON_PARANOID(tcp_hdr(skb)->urg);
851 EFX_BUG_ON_PARANOID(tcp_hdr(skb)->syn);
852 EFX_BUG_ON_PARANOID(tcp_hdr(skb)->rst);
853
Ben Hutchings23d9e602008-09-01 12:47:02 +0100854 st->packet_space = st->full_packet_size;
855 st->out_len = skb->len - st->header_len;
856 st->unmap_len = 0;
857 st->unmap_single = false;
Ben Hutchingsb9b39b62008-05-07 12:51:12 +0100858}
859
Ben Hutchings4d566062008-09-01 12:47:12 +0100860static int tso_get_fragment(struct tso_state *st, struct efx_nic *efx,
861 skb_frag_t *frag)
Ben Hutchingsb9b39b62008-05-07 12:51:12 +0100862{
Ben Hutchings23d9e602008-09-01 12:47:02 +0100863 st->unmap_addr = pci_map_page(efx->pci_dev, frag->page,
864 frag->page_offset, frag->size,
865 PCI_DMA_TODEVICE);
866 if (likely(!pci_dma_mapping_error(efx->pci_dev, st->unmap_addr))) {
867 st->unmap_single = false;
868 st->unmap_len = frag->size;
869 st->in_len = frag->size;
870 st->dma_addr = st->unmap_addr;
Ben Hutchingsecbd95c2008-09-01 12:46:40 +0100871 return 0;
872 }
873 return -ENOMEM;
874}
875
Ben Hutchings4d566062008-09-01 12:47:12 +0100876static int tso_get_head_fragment(struct tso_state *st, struct efx_nic *efx,
877 const struct sk_buff *skb)
Ben Hutchingsecbd95c2008-09-01 12:46:40 +0100878{
Ben Hutchings23d9e602008-09-01 12:47:02 +0100879 int hl = st->header_len;
Ben Hutchingsecbd95c2008-09-01 12:46:40 +0100880 int len = skb_headlen(skb) - hl;
881
Ben Hutchings23d9e602008-09-01 12:47:02 +0100882 st->unmap_addr = pci_map_single(efx->pci_dev, skb->data + hl,
883 len, PCI_DMA_TODEVICE);
884 if (likely(!pci_dma_mapping_error(efx->pci_dev, st->unmap_addr))) {
885 st->unmap_single = true;
886 st->unmap_len = len;
887 st->in_len = len;
888 st->dma_addr = st->unmap_addr;
Ben Hutchingsb9b39b62008-05-07 12:51:12 +0100889 return 0;
890 }
891 return -ENOMEM;
892}
893
894
895/**
896 * tso_fill_packet_with_fragment - form descriptors for the current fragment
897 * @tx_queue: Efx TX queue
898 * @skb: Socket buffer
899 * @st: TSO state
900 *
901 * Form descriptors for the current fragment, until we reach the end
902 * of fragment or end-of-packet. Return 0 on success, 1 if not enough
903 * space in @tx_queue.
904 */
Ben Hutchings4d566062008-09-01 12:47:12 +0100905static int tso_fill_packet_with_fragment(struct efx_tx_queue *tx_queue,
906 const struct sk_buff *skb,
907 struct tso_state *st)
Ben Hutchingsb9b39b62008-05-07 12:51:12 +0100908{
Ben Hutchingsecbd95c2008-09-01 12:46:40 +0100909 struct efx_tx_buffer *buffer;
Ben Hutchingsb9b39b62008-05-07 12:51:12 +0100910 int n, end_of_packet, rc;
911
Ben Hutchings23d9e602008-09-01 12:47:02 +0100912 if (st->in_len == 0)
Ben Hutchingsb9b39b62008-05-07 12:51:12 +0100913 return 0;
914 if (st->packet_space == 0)
915 return 0;
916
Ben Hutchings23d9e602008-09-01 12:47:02 +0100917 EFX_BUG_ON_PARANOID(st->in_len <= 0);
Ben Hutchingsb9b39b62008-05-07 12:51:12 +0100918 EFX_BUG_ON_PARANOID(st->packet_space <= 0);
919
Ben Hutchings23d9e602008-09-01 12:47:02 +0100920 n = min(st->in_len, st->packet_space);
Ben Hutchingsb9b39b62008-05-07 12:51:12 +0100921
922 st->packet_space -= n;
Ben Hutchings23d9e602008-09-01 12:47:02 +0100923 st->out_len -= n;
924 st->in_len -= n;
Ben Hutchingsb9b39b62008-05-07 12:51:12 +0100925
Ben Hutchings23d9e602008-09-01 12:47:02 +0100926 rc = efx_tx_queue_insert(tx_queue, st->dma_addr, n, &buffer);
Ben Hutchingsecbd95c2008-09-01 12:46:40 +0100927 if (likely(rc == 0)) {
Ben Hutchings23d9e602008-09-01 12:47:02 +0100928 if (st->out_len == 0)
Ben Hutchingsecbd95c2008-09-01 12:46:40 +0100929 /* Transfer ownership of the skb */
930 buffer->skb = skb;
931
Ben Hutchings23d9e602008-09-01 12:47:02 +0100932 end_of_packet = st->out_len == 0 || st->packet_space == 0;
Ben Hutchingsecbd95c2008-09-01 12:46:40 +0100933 buffer->continuation = !end_of_packet;
934
Ben Hutchings23d9e602008-09-01 12:47:02 +0100935 if (st->in_len == 0) {
Ben Hutchingsecbd95c2008-09-01 12:46:40 +0100936 /* Transfer ownership of the pci mapping */
Ben Hutchings23d9e602008-09-01 12:47:02 +0100937 buffer->unmap_len = st->unmap_len;
938 buffer->unmap_single = st->unmap_single;
939 st->unmap_len = 0;
Ben Hutchingsecbd95c2008-09-01 12:46:40 +0100940 }
941 }
Ben Hutchingsb9b39b62008-05-07 12:51:12 +0100942
Ben Hutchings23d9e602008-09-01 12:47:02 +0100943 st->dma_addr += n;
Ben Hutchingsb9b39b62008-05-07 12:51:12 +0100944 return rc;
945}
946
947
948/**
949 * tso_start_new_packet - generate a new header and prepare for the new packet
950 * @tx_queue: Efx TX queue
951 * @skb: Socket buffer
952 * @st: TSO state
953 *
954 * Generate a new header and prepare for the new packet. Return 0 on
955 * success, or -1 if failed to alloc header.
956 */
Ben Hutchings4d566062008-09-01 12:47:12 +0100957static int tso_start_new_packet(struct efx_tx_queue *tx_queue,
958 const struct sk_buff *skb,
959 struct tso_state *st)
Ben Hutchingsb9b39b62008-05-07 12:51:12 +0100960{
961 struct efx_tso_header *tsoh;
962 struct iphdr *tsoh_iph;
963 struct tcphdr *tsoh_th;
964 unsigned ip_length;
965 u8 *header;
966
967 /* Allocate a DMA-mapped header buffer. */
Ben Hutchings23d9e602008-09-01 12:47:02 +0100968 if (likely(TSOH_SIZE(st->header_len) <= TSOH_STD_SIZE)) {
Ben Hutchingsb3475642008-05-16 21:15:49 +0100969 if (tx_queue->tso_headers_free == NULL) {
Ben Hutchingsb9b39b62008-05-07 12:51:12 +0100970 if (efx_tsoh_block_alloc(tx_queue))
971 return -1;
Ben Hutchingsb3475642008-05-16 21:15:49 +0100972 }
Ben Hutchingsb9b39b62008-05-07 12:51:12 +0100973 EFX_BUG_ON_PARANOID(!tx_queue->tso_headers_free);
974 tsoh = tx_queue->tso_headers_free;
975 tx_queue->tso_headers_free = tsoh->next;
976 tsoh->unmap_len = 0;
977 } else {
978 tx_queue->tso_long_headers++;
Ben Hutchings23d9e602008-09-01 12:47:02 +0100979 tsoh = efx_tsoh_heap_alloc(tx_queue, st->header_len);
Ben Hutchingsb9b39b62008-05-07 12:51:12 +0100980 if (unlikely(!tsoh))
981 return -1;
982 }
983
984 header = TSOH_BUFFER(tsoh);
985 tsoh_th = (struct tcphdr *)(header + SKB_TCP_OFF(skb));
986 tsoh_iph = (struct iphdr *)(header + SKB_IPV4_OFF(skb));
987
988 /* Copy and update the headers. */
Ben Hutchings23d9e602008-09-01 12:47:02 +0100989 memcpy(header, skb->data, st->header_len);
Ben Hutchingsb9b39b62008-05-07 12:51:12 +0100990
991 tsoh_th->seq = htonl(st->seqnum);
992 st->seqnum += skb_shinfo(skb)->gso_size;
Ben Hutchings23d9e602008-09-01 12:47:02 +0100993 if (st->out_len > skb_shinfo(skb)->gso_size) {
Ben Hutchingsb9b39b62008-05-07 12:51:12 +0100994 /* This packet will not finish the TSO burst. */
Ben Hutchings23d9e602008-09-01 12:47:02 +0100995 ip_length = st->full_packet_size - ETH_HDR_LEN(skb);
Ben Hutchingsb9b39b62008-05-07 12:51:12 +0100996 tsoh_th->fin = 0;
997 tsoh_th->psh = 0;
998 } else {
999 /* This packet will be the last in the TSO burst. */
Ben Hutchings23d9e602008-09-01 12:47:02 +01001000 ip_length = st->header_len - ETH_HDR_LEN(skb) + st->out_len;
Ben Hutchingsb9b39b62008-05-07 12:51:12 +01001001 tsoh_th->fin = tcp_hdr(skb)->fin;
1002 tsoh_th->psh = tcp_hdr(skb)->psh;
1003 }
1004 tsoh_iph->tot_len = htons(ip_length);
1005
1006 /* Linux leaves suitable gaps in the IP ID space for us to fill. */
Ben Hutchings23d9e602008-09-01 12:47:02 +01001007 tsoh_iph->id = htons(st->ipv4_id);
1008 st->ipv4_id++;
Ben Hutchingsb9b39b62008-05-07 12:51:12 +01001009
1010 st->packet_space = skb_shinfo(skb)->gso_size;
1011 ++tx_queue->tso_packets;
1012
1013 /* Form a descriptor for this header. */
Ben Hutchings23d9e602008-09-01 12:47:02 +01001014 efx_tso_put_header(tx_queue, tsoh, st->header_len);
Ben Hutchingsb9b39b62008-05-07 12:51:12 +01001015
1016 return 0;
1017}
1018
1019
1020/**
1021 * efx_enqueue_skb_tso - segment and transmit a TSO socket buffer
1022 * @tx_queue: Efx TX queue
1023 * @skb: Socket buffer
1024 *
1025 * Context: You must hold netif_tx_lock() to call this function.
1026 *
1027 * Add socket buffer @skb to @tx_queue, doing TSO or return != 0 if
1028 * @skb was not enqueued. In all cases @skb is consumed. Return
1029 * %NETDEV_TX_OK or %NETDEV_TX_BUSY.
1030 */
1031static int efx_enqueue_skb_tso(struct efx_tx_queue *tx_queue,
Ben Hutchings740847d2008-09-01 12:48:23 +01001032 struct sk_buff *skb)
Ben Hutchingsb9b39b62008-05-07 12:51:12 +01001033{
Ben Hutchingsecbd95c2008-09-01 12:46:40 +01001034 struct efx_nic *efx = tx_queue->efx;
Ben Hutchingsb9b39b62008-05-07 12:51:12 +01001035 int frag_i, rc, rc2 = NETDEV_TX_OK;
1036 struct tso_state state;
Ben Hutchingsb9b39b62008-05-07 12:51:12 +01001037
1038 /* Verify TSO is safe - these checks should never fail. */
1039 efx_tso_check_safe(skb);
1040
1041 EFX_BUG_ON_PARANOID(tx_queue->write_count != tx_queue->insert_count);
1042
1043 tso_start(&state, skb);
1044
1045 /* Assume that skb header area contains exactly the headers, and
1046 * all payload is in the frag list.
1047 */
Ben Hutchings23d9e602008-09-01 12:47:02 +01001048 if (skb_headlen(skb) == state.header_len) {
Ben Hutchingsb9b39b62008-05-07 12:51:12 +01001049 /* Grab the first payload fragment. */
1050 EFX_BUG_ON_PARANOID(skb_shinfo(skb)->nr_frags < 1);
1051 frag_i = 0;
Ben Hutchingsecbd95c2008-09-01 12:46:40 +01001052 rc = tso_get_fragment(&state, efx,
1053 skb_shinfo(skb)->frags + frag_i);
Ben Hutchingsb9b39b62008-05-07 12:51:12 +01001054 if (rc)
1055 goto mem_err;
1056 } else {
Ben Hutchingsecbd95c2008-09-01 12:46:40 +01001057 rc = tso_get_head_fragment(&state, efx, skb);
Ben Hutchingsb9b39b62008-05-07 12:51:12 +01001058 if (rc)
1059 goto mem_err;
1060 frag_i = -1;
1061 }
1062
1063 if (tso_start_new_packet(tx_queue, skb, &state) < 0)
1064 goto mem_err;
1065
1066 while (1) {
1067 rc = tso_fill_packet_with_fragment(tx_queue, skb, &state);
1068 if (unlikely(rc))
1069 goto stop;
1070
1071 /* Move onto the next fragment? */
Ben Hutchings23d9e602008-09-01 12:47:02 +01001072 if (state.in_len == 0) {
Ben Hutchingsb9b39b62008-05-07 12:51:12 +01001073 if (++frag_i >= skb_shinfo(skb)->nr_frags)
1074 /* End of payload reached. */
1075 break;
Ben Hutchingsecbd95c2008-09-01 12:46:40 +01001076 rc = tso_get_fragment(&state, efx,
1077 skb_shinfo(skb)->frags + frag_i);
Ben Hutchingsb9b39b62008-05-07 12:51:12 +01001078 if (rc)
1079 goto mem_err;
1080 }
1081
1082 /* Start at new packet? */
1083 if (state.packet_space == 0 &&
1084 tso_start_new_packet(tx_queue, skb, &state) < 0)
1085 goto mem_err;
1086 }
1087
1088 /* Pass off to hardware */
1089 falcon_push_buffers(tx_queue);
1090
1091 tx_queue->tso_bursts++;
1092 return NETDEV_TX_OK;
1093
1094 mem_err:
Ben Hutchingsecbd95c2008-09-01 12:46:40 +01001095 EFX_ERR(efx, "Out of memory for TSO headers, or PCI mapping error\n");
Ben Hutchingsb9b39b62008-05-07 12:51:12 +01001096 dev_kfree_skb_any((struct sk_buff *)skb);
1097 goto unwind;
1098
1099 stop:
1100 rc2 = NETDEV_TX_BUSY;
1101
1102 /* Stop the queue if it wasn't stopped before. */
1103 if (tx_queue->stopped == 1)
Ben Hutchingsecbd95c2008-09-01 12:46:40 +01001104 efx_stop_queue(efx);
Ben Hutchingsb9b39b62008-05-07 12:51:12 +01001105
1106 unwind:
Ben Hutchings5988b632008-09-01 12:46:36 +01001107 /* Free the DMA mapping we were in the process of writing out */
Ben Hutchings23d9e602008-09-01 12:47:02 +01001108 if (state.unmap_len) {
1109 if (state.unmap_single)
1110 pci_unmap_single(efx->pci_dev, state.unmap_addr,
1111 state.unmap_len, PCI_DMA_TODEVICE);
Ben Hutchingsecbd95c2008-09-01 12:46:40 +01001112 else
Ben Hutchings23d9e602008-09-01 12:47:02 +01001113 pci_unmap_page(efx->pci_dev, state.unmap_addr,
1114 state.unmap_len, PCI_DMA_TODEVICE);
Ben Hutchingsecbd95c2008-09-01 12:46:40 +01001115 }
Ben Hutchings5988b632008-09-01 12:46:36 +01001116
Ben Hutchingsb9b39b62008-05-07 12:51:12 +01001117 efx_enqueue_unwind(tx_queue);
1118 return rc2;
1119}
1120
1121
1122/*
1123 * Free up all TSO datastructures associated with tx_queue. This
1124 * routine should be called only once the tx_queue is both empty and
1125 * will no longer be used.
1126 */
1127static void efx_fini_tso(struct efx_tx_queue *tx_queue)
1128{
1129 unsigned i;
1130
Ben Hutchingsb3475642008-05-16 21:15:49 +01001131 if (tx_queue->buffer) {
Ben Hutchings3ffeabd2009-10-23 08:30:58 +00001132 for (i = 0; i <= EFX_TXQ_MASK; ++i)
Ben Hutchingsb9b39b62008-05-07 12:51:12 +01001133 efx_tsoh_free(tx_queue, &tx_queue->buffer[i]);
Ben Hutchingsb3475642008-05-16 21:15:49 +01001134 }
Ben Hutchingsb9b39b62008-05-07 12:51:12 +01001135
1136 while (tx_queue->tso_headers_free != NULL)
1137 efx_tsoh_block_free(tx_queue, tx_queue->tso_headers_free,
1138 tx_queue->efx->pci_dev);
1139}