blob: 3089e888a08d0da02d1ee94a8c8caeebde384b47 [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"
Bert Kenwarde9117e52016-11-17 10:51:54 +000025#include "tx.h"
Ben Hutchings8ceee662008-04-27 12:55:59 +010026#include "workarounds.h"
Ben Hutchingsdfa50be2013-03-08 21:20:09 +000027#include "ef10_regs.h"
Ben Hutchings8ceee662008-04-27 12:55:59 +010028
Ben Hutchings183233b2013-06-28 21:47:12 +010029#ifdef EFX_USE_PIO
30
31#define EFX_PIOBUF_SIZE_MAX ER_DZ_TX_PIOBUF_SIZE
32#define EFX_PIOBUF_SIZE_DEF ALIGN(256, L1_CACHE_BYTES)
33unsigned int efx_piobuf_size __read_mostly = EFX_PIOBUF_SIZE_DEF;
34
35#endif /* EFX_USE_PIO */
36
Bert Kenwarde9117e52016-11-17 10:51:54 +000037static inline u8 *efx_tx_get_copy_buffer(struct efx_tx_queue *tx_queue,
38 struct efx_tx_buffer *buffer)
Ben Hutchings0fe55652013-06-28 21:47:15 +010039{
Bert Kenwarde9117e52016-11-17 10:51:54 +000040 unsigned int index = efx_tx_queue_get_insert_index(tx_queue);
41 struct efx_buffer *page_buf =
42 &tx_queue->cb_page[index >> (PAGE_SHIFT - EFX_TX_CB_ORDER)];
43 unsigned int offset =
44 ((index << EFX_TX_CB_ORDER) + NET_IP_ALIGN) & (PAGE_SIZE - 1);
45
46 if (unlikely(!page_buf->addr) &&
47 efx_nic_alloc_buffer(tx_queue->efx, page_buf, PAGE_SIZE,
48 GFP_ATOMIC))
49 return NULL;
50 buffer->dma_addr = page_buf->dma_addr + offset;
51 buffer->unmap_len = 0;
52 return (u8 *)page_buf->addr + offset;
Ben Hutchings0fe55652013-06-28 21:47:15 +010053}
54
Bert Kenwarde9117e52016-11-17 10:51:54 +000055u8 *efx_tx_get_copy_buffer_limited(struct efx_tx_queue *tx_queue,
56 struct efx_tx_buffer *buffer, size_t len)
Ben Hutchings0fe55652013-06-28 21:47:15 +010057{
Bert Kenwarde9117e52016-11-17 10:51:54 +000058 if (len > EFX_TX_CB_SIZE)
59 return NULL;
60 return efx_tx_get_copy_buffer(tx_queue, buffer);
Ben Hutchings0fe55652013-06-28 21:47:15 +010061}
62
Ben Hutchings4d566062008-09-01 12:47:12 +010063static void efx_dequeue_buffer(struct efx_tx_queue *tx_queue,
Tom Herbertc3940992011-11-28 16:33:43 +000064 struct efx_tx_buffer *buffer,
65 unsigned int *pkts_compl,
66 unsigned int *bytes_compl)
Ben Hutchings8ceee662008-04-27 12:55:59 +010067{
68 if (buffer->unmap_len) {
Ben Hutchings0e33d872012-05-17 17:46:55 +010069 struct device *dma_dev = &tx_queue->efx->pci_dev->dev;
Alexandre Rames2acdb922013-10-31 12:42:32 +000070 dma_addr_t unmap_addr = buffer->dma_addr - buffer->dma_offset;
Ben Hutchings7668ff92012-05-17 20:52:20 +010071 if (buffer->flags & EFX_TX_BUF_MAP_SINGLE)
Ben Hutchings0e33d872012-05-17 17:46:55 +010072 dma_unmap_single(dma_dev, unmap_addr, buffer->unmap_len,
73 DMA_TO_DEVICE);
Ben Hutchings8ceee662008-04-27 12:55:59 +010074 else
Ben Hutchings0e33d872012-05-17 17:46:55 +010075 dma_unmap_page(dma_dev, unmap_addr, buffer->unmap_len,
76 DMA_TO_DEVICE);
Ben Hutchings8ceee662008-04-27 12:55:59 +010077 buffer->unmap_len = 0;
Ben Hutchings8ceee662008-04-27 12:55:59 +010078 }
79
Ben Hutchings7668ff92012-05-17 20:52:20 +010080 if (buffer->flags & EFX_TX_BUF_SKB) {
Tom Herbertc3940992011-11-28 16:33:43 +000081 (*pkts_compl)++;
82 (*bytes_compl) += buffer->skb->len;
Rick Jones4ef6dae2014-09-09 14:43:27 -070083 dev_consume_skb_any((struct sk_buff *)buffer->skb);
Ben Hutchings62776d02010-06-23 11:30:07 +000084 netif_vdbg(tx_queue->efx, tx_done, tx_queue->efx->net_dev,
85 "TX queue %d transmission id %x complete\n",
86 tx_queue->queue, tx_queue->read_count);
Ben Hutchingsf7251a92012-05-17 18:40:54 +010087 } else if (buffer->flags & EFX_TX_BUF_HEAP) {
88 kfree(buffer->heap_buf);
Ben Hutchings8ceee662008-04-27 12:55:59 +010089 }
Ben Hutchings7668ff92012-05-17 20:52:20 +010090
Ben Hutchingsf7251a92012-05-17 18:40:54 +010091 buffer->len = 0;
92 buffer->flags = 0;
Ben Hutchings8ceee662008-04-27 12:55:59 +010093}
94
Ben Hutchings7e6d06f2012-07-30 15:57:44 +000095unsigned int efx_tx_max_skb_descs(struct efx_nic *efx)
96{
97 /* Header and payload descriptor for each output segment, plus
98 * one for every input fragment boundary within a segment
99 */
100 unsigned int max_descs = EFX_TSO_MAX_SEGS * 2 + MAX_SKB_FRAGS;
101
Ben Hutchingsdfa50be2013-03-08 21:20:09 +0000102 /* Possibly one more per segment for the alignment workaround,
103 * or for option descriptors
104 */
105 if (EFX_WORKAROUND_5391(efx) || efx_nic_rev(efx) >= EFX_REV_HUNT_A0)
Ben Hutchings7e6d06f2012-07-30 15:57:44 +0000106 max_descs += EFX_TSO_MAX_SEGS;
107
108 /* Possibly more for PCIe page boundaries within input fragments */
109 if (PAGE_SIZE > EFX_PAGE_SIZE)
110 max_descs += max_t(unsigned int, MAX_SKB_FRAGS,
111 DIV_ROUND_UP(GSO_MAX_SIZE, EFX_PAGE_SIZE));
112
113 return max_descs;
114}
115
Ben Hutchings14bf7182012-05-22 01:27:58 +0100116static void efx_tx_maybe_stop_queue(struct efx_tx_queue *txq1)
117{
118 /* We need to consider both queues that the net core sees as one */
119 struct efx_tx_queue *txq2 = efx_tx_queue_partner(txq1);
120 struct efx_nic *efx = txq1->efx;
121 unsigned int fill_level;
122
123 fill_level = max(txq1->insert_count - txq1->old_read_count,
124 txq2->insert_count - txq2->old_read_count);
125 if (likely(fill_level < efx->txq_stop_thresh))
126 return;
127
128 /* We used the stale old_read_count above, which gives us a
129 * pessimistic estimate of the fill level (which may even
130 * validly be >= efx->txq_entries). Now try again using
131 * read_count (more likely to be a cache miss).
132 *
133 * If we read read_count and then conditionally stop the
134 * queue, it is possible for the completion path to race with
135 * us and complete all outstanding descriptors in the middle,
136 * after which there will be no more completions to wake it.
137 * Therefore we stop the queue first, then read read_count
138 * (with a memory barrier to ensure the ordering), then
139 * restart the queue if the fill level turns out to be low
140 * enough.
141 */
142 netif_tx_stop_queue(txq1->core_txq);
143 smp_mb();
144 txq1->old_read_count = ACCESS_ONCE(txq1->read_count);
145 txq2->old_read_count = ACCESS_ONCE(txq2->read_count);
146
147 fill_level = max(txq1->insert_count - txq1->old_read_count,
148 txq2->insert_count - txq2->old_read_count);
149 EFX_BUG_ON_PARANOID(fill_level >= efx->txq_entries);
150 if (likely(fill_level < efx->txq_stop_thresh)) {
151 smp_mb();
152 if (likely(!efx->loopback_selftest))
153 netif_tx_start_queue(txq1->core_txq);
154 }
155}
156
Bert Kenwarde9117e52016-11-17 10:51:54 +0000157static int efx_enqueue_skb_copy(struct efx_tx_queue *tx_queue,
158 struct sk_buff *skb)
159{
160 unsigned int min_len = tx_queue->tx_min_size;
161 unsigned int copy_len = skb->len;
162 struct efx_tx_buffer *buffer;
163 u8 *copy_buffer;
164 int rc;
165
166 EFX_BUG_ON_PARANOID(copy_len > EFX_TX_CB_SIZE);
167
168 buffer = efx_tx_queue_get_insert_buffer(tx_queue);
169
170 copy_buffer = efx_tx_get_copy_buffer(tx_queue, buffer);
171 if (unlikely(!copy_buffer))
172 return -ENOMEM;
173
174 rc = skb_copy_bits(skb, 0, copy_buffer, copy_len);
175 EFX_WARN_ON_PARANOID(rc);
176 if (unlikely(copy_len < min_len)) {
177 memset(copy_buffer + copy_len, 0, min_len - copy_len);
178 buffer->len = min_len;
179 } else {
180 buffer->len = copy_len;
181 }
182
183 buffer->skb = skb;
184 buffer->flags = EFX_TX_BUF_SKB;
185
186 ++tx_queue->insert_count;
187 return rc;
188}
189
Jon Cooperee45fd92c2013-09-02 18:24:29 +0100190#ifdef EFX_USE_PIO
191
192struct efx_short_copy_buffer {
193 int used;
194 u8 buf[L1_CACHE_BYTES];
195};
196
197/* Copy to PIO, respecting that writes to PIO buffers must be dword aligned.
198 * Advances piobuf pointer. Leaves additional data in the copy buffer.
199 */
200static void efx_memcpy_toio_aligned(struct efx_nic *efx, u8 __iomem **piobuf,
201 u8 *data, int len,
202 struct efx_short_copy_buffer *copy_buf)
203{
204 int block_len = len & ~(sizeof(copy_buf->buf) - 1);
205
Ben Hutchings4984c232014-07-27 03:14:39 +0100206 __iowrite64_copy(*piobuf, data, block_len >> 3);
Jon Cooperee45fd92c2013-09-02 18:24:29 +0100207 *piobuf += block_len;
208 len -= block_len;
209
210 if (len) {
211 data += block_len;
212 BUG_ON(copy_buf->used);
213 BUG_ON(len > sizeof(copy_buf->buf));
214 memcpy(copy_buf->buf, data, len);
215 copy_buf->used = len;
216 }
217}
218
219/* Copy to PIO, respecting dword alignment, popping data from copy buffer first.
220 * Advances piobuf pointer. Leaves additional data in the copy buffer.
221 */
222static void efx_memcpy_toio_aligned_cb(struct efx_nic *efx, u8 __iomem **piobuf,
223 u8 *data, int len,
224 struct efx_short_copy_buffer *copy_buf)
225{
226 if (copy_buf->used) {
227 /* if the copy buffer is partially full, fill it up and write */
228 int copy_to_buf =
229 min_t(int, sizeof(copy_buf->buf) - copy_buf->used, len);
230
231 memcpy(copy_buf->buf + copy_buf->used, data, copy_to_buf);
232 copy_buf->used += copy_to_buf;
233
234 /* if we didn't fill it up then we're done for now */
235 if (copy_buf->used < sizeof(copy_buf->buf))
236 return;
237
Ben Hutchings4984c232014-07-27 03:14:39 +0100238 __iowrite64_copy(*piobuf, copy_buf->buf,
239 sizeof(copy_buf->buf) >> 3);
Jon Cooperee45fd92c2013-09-02 18:24:29 +0100240 *piobuf += sizeof(copy_buf->buf);
241 data += copy_to_buf;
242 len -= copy_to_buf;
243 copy_buf->used = 0;
244 }
245
246 efx_memcpy_toio_aligned(efx, piobuf, data, len, copy_buf);
247}
248
249static void efx_flush_copy_buffer(struct efx_nic *efx, u8 __iomem *piobuf,
250 struct efx_short_copy_buffer *copy_buf)
251{
252 /* if there's anything in it, write the whole buffer, including junk */
253 if (copy_buf->used)
Ben Hutchings4984c232014-07-27 03:14:39 +0100254 __iowrite64_copy(piobuf, copy_buf->buf,
255 sizeof(copy_buf->buf) >> 3);
Jon Cooperee45fd92c2013-09-02 18:24:29 +0100256}
257
258/* Traverse skb structure and copy fragments in to PIO buffer.
259 * Advances piobuf pointer.
260 */
261static void efx_skb_copy_bits_to_pio(struct efx_nic *efx, struct sk_buff *skb,
262 u8 __iomem **piobuf,
263 struct efx_short_copy_buffer *copy_buf)
264{
265 int i;
266
267 efx_memcpy_toio_aligned(efx, piobuf, skb->data, skb_headlen(skb),
268 copy_buf);
269
270 for (i = 0; i < skb_shinfo(skb)->nr_frags; ++i) {
271 skb_frag_t *f = &skb_shinfo(skb)->frags[i];
272 u8 *vaddr;
273
274 vaddr = kmap_atomic(skb_frag_page(f));
275
276 efx_memcpy_toio_aligned_cb(efx, piobuf, vaddr + f->page_offset,
277 skb_frag_size(f), copy_buf);
278 kunmap_atomic(vaddr);
279 }
280
281 EFX_BUG_ON_PARANOID(skb_shinfo(skb)->frag_list);
282}
283
Bert Kenwarde9117e52016-11-17 10:51:54 +0000284static int efx_enqueue_skb_pio(struct efx_tx_queue *tx_queue,
285 struct sk_buff *skb)
Jon Cooperee45fd92c2013-09-02 18:24:29 +0100286{
287 struct efx_tx_buffer *buffer =
288 efx_tx_queue_get_insert_buffer(tx_queue);
289 u8 __iomem *piobuf = tx_queue->piobuf;
290
291 /* Copy to PIO buffer. Ensure the writes are padded to the end
292 * of a cache line, as this is required for write-combining to be
293 * effective on at least x86.
294 */
295
296 if (skb_shinfo(skb)->nr_frags) {
297 /* The size of the copy buffer will ensure all writes
298 * are the size of a cache line.
299 */
300 struct efx_short_copy_buffer copy_buf;
301
302 copy_buf.used = 0;
303
304 efx_skb_copy_bits_to_pio(tx_queue->efx, skb,
305 &piobuf, &copy_buf);
306 efx_flush_copy_buffer(tx_queue->efx, piobuf, &copy_buf);
307 } else {
308 /* Pad the write to the size of a cache line.
Bert Kenwarde9117e52016-11-17 10:51:54 +0000309 * We can do this because we know the skb_shared_info struct is
Jon Cooperee45fd92c2013-09-02 18:24:29 +0100310 * after the source, and the destination buffer is big enough.
311 */
312 BUILD_BUG_ON(L1_CACHE_BYTES >
313 SKB_DATA_ALIGN(sizeof(struct skb_shared_info)));
Ben Hutchings4984c232014-07-27 03:14:39 +0100314 __iowrite64_copy(tx_queue->piobuf, skb->data,
315 ALIGN(skb->len, L1_CACHE_BYTES) >> 3);
Jon Cooperee45fd92c2013-09-02 18:24:29 +0100316 }
317
Bert Kenwarde9117e52016-11-17 10:51:54 +0000318 buffer->skb = skb;
319 buffer->flags = EFX_TX_BUF_SKB | EFX_TX_BUF_OPTION;
320
Jon Cooperee45fd92c2013-09-02 18:24:29 +0100321 EFX_POPULATE_QWORD_5(buffer->option,
322 ESF_DZ_TX_DESC_IS_OPT, 1,
323 ESF_DZ_TX_OPTION_TYPE, ESE_DZ_TX_OPTION_DESC_PIO,
324 ESF_DZ_TX_PIO_CONT, 0,
325 ESF_DZ_TX_PIO_BYTE_CNT, skb->len,
326 ESF_DZ_TX_PIO_BUF_ADDR,
327 tx_queue->piobuf_offset);
Jon Cooperee45fd92c2013-09-02 18:24:29 +0100328 ++tx_queue->insert_count;
Bert Kenwarde9117e52016-11-17 10:51:54 +0000329 return 0;
Jon Cooperee45fd92c2013-09-02 18:24:29 +0100330}
331#endif /* EFX_USE_PIO */
332
Bert Kenwarde9117e52016-11-17 10:51:54 +0000333static struct efx_tx_buffer *efx_tx_map_chunk(struct efx_tx_queue *tx_queue,
334 dma_addr_t dma_addr,
335 size_t len)
336{
337 const struct efx_nic_type *nic_type = tx_queue->efx->type;
338 struct efx_tx_buffer *buffer;
339 unsigned int dma_len;
340
341 /* Map the fragment taking account of NIC-dependent DMA limits. */
342 do {
343 buffer = efx_tx_queue_get_insert_buffer(tx_queue);
344 dma_len = nic_type->tx_limit_len(tx_queue, dma_addr, len);
345
346 buffer->len = dma_len;
347 buffer->dma_addr = dma_addr;
348 buffer->flags = EFX_TX_BUF_CONT;
349 len -= dma_len;
350 dma_addr += dma_len;
351 ++tx_queue->insert_count;
352 } while (len);
353
354 return buffer;
355}
356
357/* Map all data from an SKB for DMA and create descriptors on the queue.
358 */
359static int efx_tx_map_data(struct efx_tx_queue *tx_queue, struct sk_buff *skb,
360 unsigned int segment_count)
361{
362 struct efx_nic *efx = tx_queue->efx;
363 struct device *dma_dev = &efx->pci_dev->dev;
364 unsigned int frag_index, nr_frags;
365 dma_addr_t dma_addr, unmap_addr;
366 unsigned short dma_flags;
367 size_t len, unmap_len;
368
369 nr_frags = skb_shinfo(skb)->nr_frags;
370 frag_index = 0;
371
372 /* Map header data. */
373 len = skb_headlen(skb);
374 dma_addr = dma_map_single(dma_dev, skb->data, len, DMA_TO_DEVICE);
375 dma_flags = EFX_TX_BUF_MAP_SINGLE;
376 unmap_len = len;
377 unmap_addr = dma_addr;
378
379 if (unlikely(dma_mapping_error(dma_dev, dma_addr)))
380 return -EIO;
381
382 if (segment_count) {
383 /* For TSO we need to put the header in to a separate
384 * descriptor. Map this separately if necessary.
385 */
386 size_t header_len = skb_transport_header(skb) - skb->data +
387 (tcp_hdr(skb)->doff << 2u);
388
389 if (header_len != len) {
390 tx_queue->tso_long_headers++;
391 efx_tx_map_chunk(tx_queue, dma_addr, header_len);
392 len -= header_len;
393 dma_addr += header_len;
394 }
395 }
396
397 /* Add descriptors for each fragment. */
398 do {
399 struct efx_tx_buffer *buffer;
400 skb_frag_t *fragment;
401
402 buffer = efx_tx_map_chunk(tx_queue, dma_addr, len);
403
404 /* The final descriptor for a fragment is responsible for
405 * unmapping the whole fragment.
406 */
407 buffer->flags = EFX_TX_BUF_CONT | dma_flags;
408 buffer->unmap_len = unmap_len;
409 buffer->dma_offset = buffer->dma_addr - unmap_addr;
410
411 if (frag_index >= nr_frags) {
412 /* Store SKB details with the final buffer for
413 * the completion.
414 */
415 buffer->skb = skb;
416 buffer->flags = EFX_TX_BUF_SKB | dma_flags;
417 return 0;
418 }
419
420 /* Move on to the next fragment. */
421 fragment = &skb_shinfo(skb)->frags[frag_index++];
422 len = skb_frag_size(fragment);
423 dma_addr = skb_frag_dma_map(dma_dev, fragment,
424 0, len, DMA_TO_DEVICE);
425 dma_flags = 0;
426 unmap_len = len;
427 unmap_addr = dma_addr;
428
429 if (unlikely(dma_mapping_error(dma_dev, dma_addr)))
430 return -EIO;
431 } while (1);
432}
433
434/* Remove buffers put into a tx_queue. None of the buffers must have
435 * an skb attached.
436 */
437static void efx_enqueue_unwind(struct efx_tx_queue *tx_queue)
438{
439 struct efx_tx_buffer *buffer;
440
441 /* Work backwards until we hit the original insert pointer value */
442 while (tx_queue->insert_count != tx_queue->write_count) {
443 --tx_queue->insert_count;
444 buffer = __efx_tx_queue_get_insert_buffer(tx_queue);
445 efx_dequeue_buffer(tx_queue, buffer, NULL, NULL);
446 }
447}
448
449static int efx_tx_tso_sw(struct efx_tx_queue *tx_queue, struct sk_buff *skb,
450 bool *data_mapped)
451{
452 return efx_enqueue_skb_tso(tx_queue, skb, data_mapped);
453}
454
Ben Hutchings8ceee662008-04-27 12:55:59 +0100455/*
456 * Add a socket buffer to a TX queue
457 *
458 * This maps all fragments of a socket buffer for DMA and adds them to
459 * the TX queue. The queue's insert pointer will be incremented by
460 * the number of fragments in the socket buffer.
461 *
462 * If any DMA mapping fails, any mapped fragments will be unmapped,
463 * the queue's insert pointer will be restored to its original value.
464 *
Ben Hutchings497f5ba2009-11-23 16:07:05 +0000465 * This function is split out from efx_hard_start_xmit to allow the
466 * loopback test to direct packets via specific TX queues.
467 *
Ben Hutchings14bf7182012-05-22 01:27:58 +0100468 * Returns NETDEV_TX_OK.
Ben Hutchings8ceee662008-04-27 12:55:59 +0100469 * You must hold netif_tx_lock() to call this function.
470 */
Ben Hutchings497f5ba2009-11-23 16:07:05 +0000471netdev_tx_t efx_enqueue_skb(struct efx_tx_queue *tx_queue, struct sk_buff *skb)
Ben Hutchings8ceee662008-04-27 12:55:59 +0100472{
Bert Kenwarde9117e52016-11-17 10:51:54 +0000473 bool data_mapped = false;
474 unsigned int segments;
475 unsigned int skb_len;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100476
Bert Kenwarde9117e52016-11-17 10:51:54 +0000477 skb_len = skb->len;
478 segments = skb_is_gso(skb) ? skb_shinfo(skb)->gso_segs : 0;
479 if (segments == 1)
480 segments = 0; /* Don't use TSO for a single segment. */
Ben Hutchingsb9b39b62008-05-07 12:51:12 +0100481
Bert Kenwarde9117e52016-11-17 10:51:54 +0000482 /* Handle TSO first - it's *possible* (although unlikely) that we might
483 * be passed a packet to segment that's smaller than the copybreak/PIO
484 * size limit.
Ben Hutchings8ceee662008-04-27 12:55:59 +0100485 */
Bert Kenwarde9117e52016-11-17 10:51:54 +0000486 if (segments) {
487 EFX_BUG_ON_PARANOID(!tx_queue->handle_tso);
488 if (tx_queue->handle_tso(tx_queue, skb, &data_mapped))
489 goto err;
490#ifdef EFX_USE_PIO
491 } else if (skb_len <= efx_piobuf_size && !skb->xmit_more &&
492 efx_nic_may_tx_pio(tx_queue)) {
493 /* Use PIO for short packets with an empty queue. */
494 if (efx_enqueue_skb_pio(tx_queue, skb))
495 goto err;
496 tx_queue->pio_packets++;
497 data_mapped = true;
498#endif
499 } else if (skb_len < tx_queue->tx_min_size ||
500 (skb->data_len && skb_len <= EFX_TX_CB_SIZE)) {
501 /* Pad short packets or coalesce short fragmented packets. */
502 if (efx_enqueue_skb_copy(tx_queue, skb))
503 goto err;
504 tx_queue->cb_packets++;
505 data_mapped = true;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100506 }
507
Bert Kenwarde9117e52016-11-17 10:51:54 +0000508 /* Map for DMA and create descriptors if we haven't done so already. */
509 if (!data_mapped && (efx_tx_map_data(tx_queue, skb, segments)))
510 goto err;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100511
Bert Kenwarde9117e52016-11-17 10:51:54 +0000512 /* Update BQL */
513 netdev_tx_sent_queue(tx_queue->core_txq, skb_len);
Edward Cree70b33fb2014-10-17 15:32:25 +0100514
Ben Hutchings8ceee662008-04-27 12:55:59 +0100515 /* Pass off to hardware */
Martin Habetsb2663a42015-11-02 12:51:31 +0000516 if (!skb->xmit_more || netif_xmit_stopped(tx_queue->core_txq)) {
517 struct efx_tx_queue *txq2 = efx_tx_queue_partner(tx_queue);
518
519 /* There could be packets left on the partner queue if those
520 * SKBs had skb->xmit_more set. If we do not push those they
521 * could be left for a long time and cause a netdev watchdog.
522 */
523 if (txq2->xmit_more_available)
524 efx_nic_push_buffers(txq2);
525
Edward Cree70b33fb2014-10-17 15:32:25 +0100526 efx_nic_push_buffers(tx_queue);
Martin Habetsb2663a42015-11-02 12:51:31 +0000527 } else {
528 tx_queue->xmit_more_available = skb->xmit_more;
529 }
Ben Hutchings8ceee662008-04-27 12:55:59 +0100530
Bert Kenwarde9117e52016-11-17 10:51:54 +0000531 if (segments) {
532 tx_queue->tso_bursts++;
533 tx_queue->tso_packets += segments;
534 tx_queue->tx_packets += segments;
535 } else {
536 tx_queue->tx_packets++;
537 }
538
539 efx_tx_maybe_stop_queue(tx_queue);
Andrew Rybchenko8ccf38002014-07-17 12:10:43 +0100540
Ben Hutchings8ceee662008-04-27 12:55:59 +0100541 return NETDEV_TX_OK;
542
Ben Hutchings8ceee662008-04-27 12:55:59 +0100543
Bert Kenwarde9117e52016-11-17 10:51:54 +0000544err:
545 efx_enqueue_unwind(tx_queue);
Ben Hutchings9bc183d2009-11-23 16:06:47 +0000546 dev_kfree_skb_any(skb);
Ben Hutchings14bf7182012-05-22 01:27:58 +0100547 return NETDEV_TX_OK;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100548}
549
550/* Remove packets from the TX queue
551 *
552 * This removes packets from the TX queue, up to and including the
553 * specified index.
554 */
Ben Hutchings4d566062008-09-01 12:47:12 +0100555static void efx_dequeue_buffers(struct efx_tx_queue *tx_queue,
Tom Herbertc3940992011-11-28 16:33:43 +0000556 unsigned int index,
557 unsigned int *pkts_compl,
558 unsigned int *bytes_compl)
Ben Hutchings8ceee662008-04-27 12:55:59 +0100559{
560 struct efx_nic *efx = tx_queue->efx;
561 unsigned int stop_index, read_ptr;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100562
Steve Hodgsonecc910f2010-09-10 06:42:22 +0000563 stop_index = (index + 1) & tx_queue->ptr_mask;
564 read_ptr = tx_queue->read_count & tx_queue->ptr_mask;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100565
566 while (read_ptr != stop_index) {
567 struct efx_tx_buffer *buffer = &tx_queue->buffer[read_ptr];
Ben Hutchingsba8977b2013-01-08 23:43:19 +0000568
569 if (!(buffer->flags & EFX_TX_BUF_OPTION) &&
570 unlikely(buffer->len == 0)) {
Ben Hutchings62776d02010-06-23 11:30:07 +0000571 netif_err(efx, tx_err, efx->net_dev,
572 "TX queue %d spurious TX completion id %x\n",
573 tx_queue->queue, read_ptr);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100574 efx_schedule_reset(efx, RESET_TYPE_TX_SKIP);
575 return;
576 }
577
Tom Herbertc3940992011-11-28 16:33:43 +0000578 efx_dequeue_buffer(tx_queue, buffer, pkts_compl, bytes_compl);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100579
580 ++tx_queue->read_count;
Steve Hodgsonecc910f2010-09-10 06:42:22 +0000581 read_ptr = tx_queue->read_count & tx_queue->ptr_mask;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100582 }
583}
584
Ben Hutchings8ceee662008-04-27 12:55:59 +0100585/* Initiate a packet transmission. We use one channel per CPU
586 * (sharing when we have more CPUs than channels). On Falcon, the TX
587 * completion events will be directed back to the CPU that transmitted
588 * the packet, which should be cache-efficient.
589 *
590 * Context: non-blocking.
591 * Note that returning anything other than NETDEV_TX_OK will cause the
592 * OS to free the skb.
593 */
Stephen Hemminger613573252009-08-31 19:50:58 +0000594netdev_tx_t efx_hard_start_xmit(struct sk_buff *skb,
Ben Hutchings2d0cc562012-02-17 00:10:45 +0000595 struct net_device *net_dev)
Ben Hutchings8ceee662008-04-27 12:55:59 +0100596{
Ben Hutchings767e4682008-09-01 12:43:14 +0100597 struct efx_nic *efx = netdev_priv(net_dev);
Ben Hutchings60ac1062008-09-01 12:44:59 +0100598 struct efx_tx_queue *tx_queue;
Ben Hutchings94b274b2011-01-10 21:18:20 +0000599 unsigned index, type;
Ben Hutchings60ac1062008-09-01 12:44:59 +0100600
Ben Hutchingse4abce82011-05-16 18:51:24 +0100601 EFX_WARN_ON_PARANOID(!netif_device_present(net_dev));
Ben Hutchingsa7ef5932009-03-04 09:52:37 +0000602
Stuart Hodgson7c236c42012-09-03 11:09:36 +0100603 /* PTP "event" packet */
604 if (unlikely(efx_xmit_with_hwtstamp(skb)) &&
605 unlikely(efx_ptp_is_ptp_tx(efx, skb))) {
606 return efx_ptp_tx(efx, skb);
607 }
608
Ben Hutchings94b274b2011-01-10 21:18:20 +0000609 index = skb_get_queue_mapping(skb);
610 type = skb->ip_summed == CHECKSUM_PARTIAL ? EFX_TXQ_TYPE_OFFLOAD : 0;
611 if (index >= efx->n_tx_channels) {
612 index -= efx->n_tx_channels;
613 type |= EFX_TXQ_TYPE_HIGHPRI;
614 }
615 tx_queue = efx_get_tx_queue(efx, index, type);
Ben Hutchings60ac1062008-09-01 12:44:59 +0100616
Ben Hutchings497f5ba2009-11-23 16:07:05 +0000617 return efx_enqueue_skb(tx_queue, skb);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100618}
619
Ben Hutchings60031fc2011-01-12 18:39:40 +0000620void efx_init_tx_queue_core_txq(struct efx_tx_queue *tx_queue)
621{
Ben Hutchings94b274b2011-01-10 21:18:20 +0000622 struct efx_nic *efx = tx_queue->efx;
623
Ben Hutchings60031fc2011-01-12 18:39:40 +0000624 /* Must be inverse of queue lookup in efx_hard_start_xmit() */
Ben Hutchings94b274b2011-01-10 21:18:20 +0000625 tx_queue->core_txq =
626 netdev_get_tx_queue(efx->net_dev,
627 tx_queue->queue / EFX_TXQ_TYPES +
628 ((tx_queue->queue & EFX_TXQ_TYPE_HIGHPRI) ?
629 efx->n_tx_channels : 0));
630}
631
John Fastabend16e5cc62016-02-16 21:16:43 -0800632int efx_setup_tc(struct net_device *net_dev, u32 handle, __be16 proto,
633 struct tc_to_netdev *ntc)
Ben Hutchings94b274b2011-01-10 21:18:20 +0000634{
635 struct efx_nic *efx = netdev_priv(net_dev);
636 struct efx_channel *channel;
637 struct efx_tx_queue *tx_queue;
John Fastabend16e5cc62016-02-16 21:16:43 -0800638 unsigned tc, num_tc;
Ben Hutchings94b274b2011-01-10 21:18:20 +0000639 int rc;
640
John Fastabend5eb4dce2016-02-29 11:26:13 -0800641 if (ntc->type != TC_SETUP_MQPRIO)
John Fastabende4c67342016-02-16 21:16:15 -0800642 return -EINVAL;
643
John Fastabend16e5cc62016-02-16 21:16:43 -0800644 num_tc = ntc->tc;
645
Ben Hutchings94b274b2011-01-10 21:18:20 +0000646 if (efx_nic_rev(efx) < EFX_REV_FALCON_B0 || num_tc > EFX_MAX_TX_TC)
647 return -EINVAL;
648
649 if (num_tc == net_dev->num_tc)
650 return 0;
651
652 for (tc = 0; tc < num_tc; tc++) {
653 net_dev->tc_to_txq[tc].offset = tc * efx->n_tx_channels;
654 net_dev->tc_to_txq[tc].count = efx->n_tx_channels;
655 }
656
657 if (num_tc > net_dev->num_tc) {
658 /* Initialise high-priority queues as necessary */
659 efx_for_each_channel(channel, efx) {
660 efx_for_each_possible_channel_tx_queue(tx_queue,
661 channel) {
662 if (!(tx_queue->queue & EFX_TXQ_TYPE_HIGHPRI))
663 continue;
664 if (!tx_queue->buffer) {
665 rc = efx_probe_tx_queue(tx_queue);
666 if (rc)
667 return rc;
668 }
669 if (!tx_queue->initialised)
670 efx_init_tx_queue(tx_queue);
671 efx_init_tx_queue_core_txq(tx_queue);
672 }
673 }
674 } else {
675 /* Reduce number of classes before number of queues */
676 net_dev->num_tc = num_tc;
677 }
678
679 rc = netif_set_real_num_tx_queues(net_dev,
680 max_t(int, num_tc, 1) *
681 efx->n_tx_channels);
682 if (rc)
683 return rc;
684
685 /* Do not destroy high-priority queues when they become
686 * unused. We would have to flush them first, and it is
687 * fairly difficult to flush a subset of TX queues. Leave
688 * it to efx_fini_channels().
689 */
690
691 net_dev->num_tc = num_tc;
692 return 0;
Ben Hutchings60031fc2011-01-12 18:39:40 +0000693}
694
Ben Hutchings8ceee662008-04-27 12:55:59 +0100695void efx_xmit_done(struct efx_tx_queue *tx_queue, unsigned int index)
696{
697 unsigned fill_level;
698 struct efx_nic *efx = tx_queue->efx;
Ben Hutchings14bf7182012-05-22 01:27:58 +0100699 struct efx_tx_queue *txq2;
Tom Herbertc3940992011-11-28 16:33:43 +0000700 unsigned int pkts_compl = 0, bytes_compl = 0;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100701
Steve Hodgsonecc910f2010-09-10 06:42:22 +0000702 EFX_BUG_ON_PARANOID(index > tx_queue->ptr_mask);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100703
Tom Herbertc3940992011-11-28 16:33:43 +0000704 efx_dequeue_buffers(tx_queue, index, &pkts_compl, &bytes_compl);
Peter Dunningc9368352015-07-08 10:05:10 +0100705 tx_queue->pkts_compl += pkts_compl;
706 tx_queue->bytes_compl += bytes_compl;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100707
Ben Hutchings02e12162013-04-27 01:55:21 +0100708 if (pkts_compl > 1)
709 ++tx_queue->merge_events;
710
Ben Hutchings14bf7182012-05-22 01:27:58 +0100711 /* See if we need to restart the netif queue. This memory
712 * barrier ensures that we write read_count (inside
713 * efx_dequeue_buffers()) before reading the queue status.
714 */
Ben Hutchings8ceee662008-04-27 12:55:59 +0100715 smp_mb();
Ben Hutchingsc04bfc62010-12-10 01:24:16 +0000716 if (unlikely(netif_tx_queue_stopped(tx_queue->core_txq)) &&
Neil Turton9d1aea62011-04-04 13:46:23 +0100717 likely(efx->port_enabled) &&
Ben Hutchingse4abce82011-05-16 18:51:24 +0100718 likely(netif_device_present(efx->net_dev))) {
Ben Hutchings14bf7182012-05-22 01:27:58 +0100719 txq2 = efx_tx_queue_partner(tx_queue);
720 fill_level = max(tx_queue->insert_count - tx_queue->read_count,
721 txq2->insert_count - txq2->read_count);
722 if (fill_level <= efx->txq_wake_thresh)
Ben Hutchingsc04bfc62010-12-10 01:24:16 +0000723 netif_tx_wake_queue(tx_queue->core_txq);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100724 }
Ben Hutchingscd385572010-11-15 23:53:11 +0000725
726 /* Check whether the hardware queue is now empty */
727 if ((int)(tx_queue->read_count - tx_queue->old_write_count) >= 0) {
728 tx_queue->old_write_count = ACCESS_ONCE(tx_queue->write_count);
729 if (tx_queue->read_count == tx_queue->old_write_count) {
730 smp_mb();
731 tx_queue->empty_read_count =
732 tx_queue->read_count | EFX_EMPTY_COUNT_VALID;
733 }
734 }
Ben Hutchings8ceee662008-04-27 12:55:59 +0100735}
736
Bert Kenwarde9117e52016-11-17 10:51:54 +0000737static unsigned int efx_tx_cb_page_count(struct efx_tx_queue *tx_queue)
Ben Hutchingsf7251a92012-05-17 18:40:54 +0100738{
Bert Kenwarde9117e52016-11-17 10:51:54 +0000739 return DIV_ROUND_UP(tx_queue->ptr_mask + 1, PAGE_SIZE >> EFX_TX_CB_ORDER);
Ben Hutchingsf7251a92012-05-17 18:40:54 +0100740}
741
Ben Hutchings8ceee662008-04-27 12:55:59 +0100742int efx_probe_tx_queue(struct efx_tx_queue *tx_queue)
743{
744 struct efx_nic *efx = tx_queue->efx;
Steve Hodgsonecc910f2010-09-10 06:42:22 +0000745 unsigned int entries;
Ben Hutchings7668ff92012-05-17 20:52:20 +0100746 int rc;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100747
Steve Hodgsonecc910f2010-09-10 06:42:22 +0000748 /* Create the smallest power-of-two aligned ring */
749 entries = max(roundup_pow_of_two(efx->txq_entries), EFX_MIN_DMAQ_SIZE);
750 EFX_BUG_ON_PARANOID(entries > EFX_MAX_DMAQ_SIZE);
751 tx_queue->ptr_mask = entries - 1;
752
753 netif_dbg(efx, probe, efx->net_dev,
754 "creating TX queue %d size %#x mask %#x\n",
755 tx_queue->queue, efx->txq_entries, tx_queue->ptr_mask);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100756
757 /* Allocate software ring */
Thomas Meyerc2e4e252011-12-02 12:36:13 +0000758 tx_queue->buffer = kcalloc(entries, sizeof(*tx_queue->buffer),
Steve Hodgsonecc910f2010-09-10 06:42:22 +0000759 GFP_KERNEL);
Ben Hutchings60ac1062008-09-01 12:44:59 +0100760 if (!tx_queue->buffer)
761 return -ENOMEM;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100762
Bert Kenwarde9117e52016-11-17 10:51:54 +0000763 tx_queue->cb_page = kcalloc(efx_tx_cb_page_count(tx_queue),
764 sizeof(tx_queue->cb_page[0]), GFP_KERNEL);
765 if (!tx_queue->cb_page) {
766 rc = -ENOMEM;
767 goto fail1;
Ben Hutchingsf7251a92012-05-17 18:40:54 +0100768 }
769
Ben Hutchings8ceee662008-04-27 12:55:59 +0100770 /* Allocate hardware ring */
Ben Hutchings152b6a62009-11-29 03:43:56 +0000771 rc = efx_nic_probe_tx(tx_queue);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100772 if (rc)
Ben Hutchingsf7251a92012-05-17 18:40:54 +0100773 goto fail2;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100774
775 return 0;
776
Ben Hutchingsf7251a92012-05-17 18:40:54 +0100777fail2:
Bert Kenwarde9117e52016-11-17 10:51:54 +0000778 kfree(tx_queue->cb_page);
779 tx_queue->cb_page = NULL;
Ben Hutchingsf7251a92012-05-17 18:40:54 +0100780fail1:
Ben Hutchings8ceee662008-04-27 12:55:59 +0100781 kfree(tx_queue->buffer);
782 tx_queue->buffer = NULL;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100783 return rc;
784}
785
Ben Hutchingsbc3c90a2008-09-01 12:48:46 +0100786void efx_init_tx_queue(struct efx_tx_queue *tx_queue)
Ben Hutchings8ceee662008-04-27 12:55:59 +0100787{
Bert Kenwarde9117e52016-11-17 10:51:54 +0000788 struct efx_nic *efx = tx_queue->efx;
789
790 netif_dbg(efx, drv, efx->net_dev,
Ben Hutchings62776d02010-06-23 11:30:07 +0000791 "initialising TX queue %d\n", tx_queue->queue);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100792
793 tx_queue->insert_count = 0;
794 tx_queue->write_count = 0;
Ben Hutchingscd385572010-11-15 23:53:11 +0000795 tx_queue->old_write_count = 0;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100796 tx_queue->read_count = 0;
797 tx_queue->old_read_count = 0;
Ben Hutchingscd385572010-11-15 23:53:11 +0000798 tx_queue->empty_read_count = 0 | EFX_EMPTY_COUNT_VALID;
Martin Habetsb2663a42015-11-02 12:51:31 +0000799 tx_queue->xmit_more_available = false;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100800
Bert Kenwarde9117e52016-11-17 10:51:54 +0000801 /* Set up default function pointers. These may get replaced by
802 * efx_nic_init_tx() based off NIC/queue capabilities.
803 */
804 tx_queue->handle_tso = efx_tx_tso_sw;
805
806 /* Some older hardware requires Tx writes larger than 32. */
807 tx_queue->tx_min_size = EFX_WORKAROUND_15592(efx) ? 33 : 0;
808
Ben Hutchings8ceee662008-04-27 12:55:59 +0100809 /* Set up TX descriptor ring */
Ben Hutchings152b6a62009-11-29 03:43:56 +0000810 efx_nic_init_tx(tx_queue);
Ben Hutchings94b274b2011-01-10 21:18:20 +0000811
812 tx_queue->initialised = true;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100813}
814
Ben Hutchingse42c3d82013-05-27 16:52:54 +0100815void efx_fini_tx_queue(struct efx_tx_queue *tx_queue)
Ben Hutchings8ceee662008-04-27 12:55:59 +0100816{
817 struct efx_tx_buffer *buffer;
818
Ben Hutchingse42c3d82013-05-27 16:52:54 +0100819 netif_dbg(tx_queue->efx, drv, tx_queue->efx->net_dev,
820 "shutting down TX queue %d\n", tx_queue->queue);
821
Ben Hutchings8ceee662008-04-27 12:55:59 +0100822 if (!tx_queue->buffer)
823 return;
824
825 /* Free any buffers left in the ring */
826 while (tx_queue->read_count != tx_queue->write_count) {
Tom Herbertc3940992011-11-28 16:33:43 +0000827 unsigned int pkts_compl = 0, bytes_compl = 0;
Steve Hodgsonecc910f2010-09-10 06:42:22 +0000828 buffer = &tx_queue->buffer[tx_queue->read_count & tx_queue->ptr_mask];
Tom Herbertc3940992011-11-28 16:33:43 +0000829 efx_dequeue_buffer(tx_queue, buffer, &pkts_compl, &bytes_compl);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100830
831 ++tx_queue->read_count;
832 }
Martin Habetsb2663a42015-11-02 12:51:31 +0000833 tx_queue->xmit_more_available = false;
Tom Herbertc3940992011-11-28 16:33:43 +0000834 netdev_tx_reset_queue(tx_queue->core_txq);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100835}
836
Ben Hutchings8ceee662008-04-27 12:55:59 +0100837void efx_remove_tx_queue(struct efx_tx_queue *tx_queue)
838{
Ben Hutchingsf7251a92012-05-17 18:40:54 +0100839 int i;
840
Ben Hutchings94b274b2011-01-10 21:18:20 +0000841 if (!tx_queue->buffer)
842 return;
843
Ben Hutchings62776d02010-06-23 11:30:07 +0000844 netif_dbg(tx_queue->efx, drv, tx_queue->efx->net_dev,
845 "destroying TX queue %d\n", tx_queue->queue);
Ben Hutchings152b6a62009-11-29 03:43:56 +0000846 efx_nic_remove_tx(tx_queue);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100847
Bert Kenwarde9117e52016-11-17 10:51:54 +0000848 if (tx_queue->cb_page) {
849 for (i = 0; i < efx_tx_cb_page_count(tx_queue); i++)
Ben Hutchingsf7251a92012-05-17 18:40:54 +0100850 efx_nic_free_buffer(tx_queue->efx,
Bert Kenwarde9117e52016-11-17 10:51:54 +0000851 &tx_queue->cb_page[i]);
852 kfree(tx_queue->cb_page);
853 tx_queue->cb_page = NULL;
Ben Hutchingsf7251a92012-05-17 18:40:54 +0100854 }
855
Ben Hutchings8ceee662008-04-27 12:55:59 +0100856 kfree(tx_queue->buffer);
857 tx_queue->buffer = NULL;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100858}