Linux 4.16.11
[linux/fpc-iii.git] / drivers / net / ethernet / sfc / tx.c
blobcece961f2e82a4e9f156828a1e6151d6e366955c
1 /****************************************************************************
2 * Driver for Solarflare network controllers and boards
3 * Copyright 2005-2006 Fen Systems Ltd.
4 * Copyright 2005-2013 Solarflare Communications Inc.
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 */
11 #include <linux/pci.h>
12 #include <linux/tcp.h>
13 #include <linux/ip.h>
14 #include <linux/in.h>
15 #include <linux/ipv6.h>
16 #include <linux/slab.h>
17 #include <net/ipv6.h>
18 #include <linux/if_ether.h>
19 #include <linux/highmem.h>
20 #include <linux/cache.h>
21 #include "net_driver.h"
22 #include "efx.h"
23 #include "io.h"
24 #include "nic.h"
25 #include "tx.h"
26 #include "workarounds.h"
27 #include "ef10_regs.h"
29 #ifdef EFX_USE_PIO
31 #define EFX_PIOBUF_SIZE_DEF ALIGN(256, L1_CACHE_BYTES)
32 unsigned int efx_piobuf_size __read_mostly = EFX_PIOBUF_SIZE_DEF;
34 #endif /* EFX_USE_PIO */
36 static inline u8 *efx_tx_get_copy_buffer(struct efx_tx_queue *tx_queue,
37 struct efx_tx_buffer *buffer)
39 unsigned int index = efx_tx_queue_get_insert_index(tx_queue);
40 struct efx_buffer *page_buf =
41 &tx_queue->cb_page[index >> (PAGE_SHIFT - EFX_TX_CB_ORDER)];
42 unsigned int offset =
43 ((index << EFX_TX_CB_ORDER) + NET_IP_ALIGN) & (PAGE_SIZE - 1);
45 if (unlikely(!page_buf->addr) &&
46 efx_nic_alloc_buffer(tx_queue->efx, page_buf, PAGE_SIZE,
47 GFP_ATOMIC))
48 return NULL;
49 buffer->dma_addr = page_buf->dma_addr + offset;
50 buffer->unmap_len = 0;
51 return (u8 *)page_buf->addr + offset;
54 u8 *efx_tx_get_copy_buffer_limited(struct efx_tx_queue *tx_queue,
55 struct efx_tx_buffer *buffer, size_t len)
57 if (len > EFX_TX_CB_SIZE)
58 return NULL;
59 return efx_tx_get_copy_buffer(tx_queue, buffer);
62 static void efx_dequeue_buffer(struct efx_tx_queue *tx_queue,
63 struct efx_tx_buffer *buffer,
64 unsigned int *pkts_compl,
65 unsigned int *bytes_compl)
67 if (buffer->unmap_len) {
68 struct device *dma_dev = &tx_queue->efx->pci_dev->dev;
69 dma_addr_t unmap_addr = buffer->dma_addr - buffer->dma_offset;
70 if (buffer->flags & EFX_TX_BUF_MAP_SINGLE)
71 dma_unmap_single(dma_dev, unmap_addr, buffer->unmap_len,
72 DMA_TO_DEVICE);
73 else
74 dma_unmap_page(dma_dev, unmap_addr, buffer->unmap_len,
75 DMA_TO_DEVICE);
76 buffer->unmap_len = 0;
79 if (buffer->flags & EFX_TX_BUF_SKB) {
80 struct sk_buff *skb = (struct sk_buff *)buffer->skb;
82 EFX_WARN_ON_PARANOID(!pkts_compl || !bytes_compl);
83 (*pkts_compl)++;
84 (*bytes_compl) += skb->len;
85 if (tx_queue->timestamping &&
86 (tx_queue->completed_timestamp_major ||
87 tx_queue->completed_timestamp_minor)) {
88 struct skb_shared_hwtstamps hwtstamp;
90 hwtstamp.hwtstamp =
91 efx_ptp_nic_to_kernel_time(tx_queue);
92 skb_tstamp_tx(skb, &hwtstamp);
94 tx_queue->completed_timestamp_major = 0;
95 tx_queue->completed_timestamp_minor = 0;
97 dev_consume_skb_any((struct sk_buff *)buffer->skb);
98 netif_vdbg(tx_queue->efx, tx_done, tx_queue->efx->net_dev,
99 "TX queue %d transmission id %x complete\n",
100 tx_queue->queue, tx_queue->read_count);
103 buffer->len = 0;
104 buffer->flags = 0;
107 unsigned int efx_tx_max_skb_descs(struct efx_nic *efx)
109 /* Header and payload descriptor for each output segment, plus
110 * one for every input fragment boundary within a segment
112 unsigned int max_descs = EFX_TSO_MAX_SEGS * 2 + MAX_SKB_FRAGS;
114 /* Possibly one more per segment for option descriptors */
115 if (efx_nic_rev(efx) >= EFX_REV_HUNT_A0)
116 max_descs += EFX_TSO_MAX_SEGS;
118 /* Possibly more for PCIe page boundaries within input fragments */
119 if (PAGE_SIZE > EFX_PAGE_SIZE)
120 max_descs += max_t(unsigned int, MAX_SKB_FRAGS,
121 DIV_ROUND_UP(GSO_MAX_SIZE, EFX_PAGE_SIZE));
123 return max_descs;
126 static void efx_tx_maybe_stop_queue(struct efx_tx_queue *txq1)
128 /* We need to consider both queues that the net core sees as one */
129 struct efx_tx_queue *txq2 = efx_tx_queue_partner(txq1);
130 struct efx_nic *efx = txq1->efx;
131 unsigned int fill_level;
133 fill_level = max(txq1->insert_count - txq1->old_read_count,
134 txq2->insert_count - txq2->old_read_count);
135 if (likely(fill_level < efx->txq_stop_thresh))
136 return;
138 /* We used the stale old_read_count above, which gives us a
139 * pessimistic estimate of the fill level (which may even
140 * validly be >= efx->txq_entries). Now try again using
141 * read_count (more likely to be a cache miss).
143 * If we read read_count and then conditionally stop the
144 * queue, it is possible for the completion path to race with
145 * us and complete all outstanding descriptors in the middle,
146 * after which there will be no more completions to wake it.
147 * Therefore we stop the queue first, then read read_count
148 * (with a memory barrier to ensure the ordering), then
149 * restart the queue if the fill level turns out to be low
150 * enough.
152 netif_tx_stop_queue(txq1->core_txq);
153 smp_mb();
154 txq1->old_read_count = READ_ONCE(txq1->read_count);
155 txq2->old_read_count = READ_ONCE(txq2->read_count);
157 fill_level = max(txq1->insert_count - txq1->old_read_count,
158 txq2->insert_count - txq2->old_read_count);
159 EFX_WARN_ON_ONCE_PARANOID(fill_level >= efx->txq_entries);
160 if (likely(fill_level < efx->txq_stop_thresh)) {
161 smp_mb();
162 if (likely(!efx->loopback_selftest))
163 netif_tx_start_queue(txq1->core_txq);
167 static int efx_enqueue_skb_copy(struct efx_tx_queue *tx_queue,
168 struct sk_buff *skb)
170 unsigned int copy_len = skb->len;
171 struct efx_tx_buffer *buffer;
172 u8 *copy_buffer;
173 int rc;
175 EFX_WARN_ON_ONCE_PARANOID(copy_len > EFX_TX_CB_SIZE);
177 buffer = efx_tx_queue_get_insert_buffer(tx_queue);
179 copy_buffer = efx_tx_get_copy_buffer(tx_queue, buffer);
180 if (unlikely(!copy_buffer))
181 return -ENOMEM;
183 rc = skb_copy_bits(skb, 0, copy_buffer, copy_len);
184 EFX_WARN_ON_PARANOID(rc);
185 buffer->len = copy_len;
187 buffer->skb = skb;
188 buffer->flags = EFX_TX_BUF_SKB;
190 ++tx_queue->insert_count;
191 return rc;
194 #ifdef EFX_USE_PIO
196 struct efx_short_copy_buffer {
197 int used;
198 u8 buf[L1_CACHE_BYTES];
201 /* Copy to PIO, respecting that writes to PIO buffers must be dword aligned.
202 * Advances piobuf pointer. Leaves additional data in the copy buffer.
204 static void efx_memcpy_toio_aligned(struct efx_nic *efx, u8 __iomem **piobuf,
205 u8 *data, int len,
206 struct efx_short_copy_buffer *copy_buf)
208 int block_len = len & ~(sizeof(copy_buf->buf) - 1);
210 __iowrite64_copy(*piobuf, data, block_len >> 3);
211 *piobuf += block_len;
212 len -= block_len;
214 if (len) {
215 data += block_len;
216 BUG_ON(copy_buf->used);
217 BUG_ON(len > sizeof(copy_buf->buf));
218 memcpy(copy_buf->buf, data, len);
219 copy_buf->used = len;
223 /* Copy to PIO, respecting dword alignment, popping data from copy buffer first.
224 * Advances piobuf pointer. Leaves additional data in the copy buffer.
226 static void efx_memcpy_toio_aligned_cb(struct efx_nic *efx, u8 __iomem **piobuf,
227 u8 *data, int len,
228 struct efx_short_copy_buffer *copy_buf)
230 if (copy_buf->used) {
231 /* if the copy buffer is partially full, fill it up and write */
232 int copy_to_buf =
233 min_t(int, sizeof(copy_buf->buf) - copy_buf->used, len);
235 memcpy(copy_buf->buf + copy_buf->used, data, copy_to_buf);
236 copy_buf->used += copy_to_buf;
238 /* if we didn't fill it up then we're done for now */
239 if (copy_buf->used < sizeof(copy_buf->buf))
240 return;
242 __iowrite64_copy(*piobuf, copy_buf->buf,
243 sizeof(copy_buf->buf) >> 3);
244 *piobuf += sizeof(copy_buf->buf);
245 data += copy_to_buf;
246 len -= copy_to_buf;
247 copy_buf->used = 0;
250 efx_memcpy_toio_aligned(efx, piobuf, data, len, copy_buf);
253 static void efx_flush_copy_buffer(struct efx_nic *efx, u8 __iomem *piobuf,
254 struct efx_short_copy_buffer *copy_buf)
256 /* if there's anything in it, write the whole buffer, including junk */
257 if (copy_buf->used)
258 __iowrite64_copy(piobuf, copy_buf->buf,
259 sizeof(copy_buf->buf) >> 3);
262 /* Traverse skb structure and copy fragments in to PIO buffer.
263 * Advances piobuf pointer.
265 static void efx_skb_copy_bits_to_pio(struct efx_nic *efx, struct sk_buff *skb,
266 u8 __iomem **piobuf,
267 struct efx_short_copy_buffer *copy_buf)
269 int i;
271 efx_memcpy_toio_aligned(efx, piobuf, skb->data, skb_headlen(skb),
272 copy_buf);
274 for (i = 0; i < skb_shinfo(skb)->nr_frags; ++i) {
275 skb_frag_t *f = &skb_shinfo(skb)->frags[i];
276 u8 *vaddr;
278 vaddr = kmap_atomic(skb_frag_page(f));
280 efx_memcpy_toio_aligned_cb(efx, piobuf, vaddr + f->page_offset,
281 skb_frag_size(f), copy_buf);
282 kunmap_atomic(vaddr);
285 EFX_WARN_ON_ONCE_PARANOID(skb_shinfo(skb)->frag_list);
288 static int efx_enqueue_skb_pio(struct efx_tx_queue *tx_queue,
289 struct sk_buff *skb)
291 struct efx_tx_buffer *buffer =
292 efx_tx_queue_get_insert_buffer(tx_queue);
293 u8 __iomem *piobuf = tx_queue->piobuf;
295 /* Copy to PIO buffer. Ensure the writes are padded to the end
296 * of a cache line, as this is required for write-combining to be
297 * effective on at least x86.
300 if (skb_shinfo(skb)->nr_frags) {
301 /* The size of the copy buffer will ensure all writes
302 * are the size of a cache line.
304 struct efx_short_copy_buffer copy_buf;
306 copy_buf.used = 0;
308 efx_skb_copy_bits_to_pio(tx_queue->efx, skb,
309 &piobuf, &copy_buf);
310 efx_flush_copy_buffer(tx_queue->efx, piobuf, &copy_buf);
311 } else {
312 /* Pad the write to the size of a cache line.
313 * We can do this because we know the skb_shared_info struct is
314 * after the source, and the destination buffer is big enough.
316 BUILD_BUG_ON(L1_CACHE_BYTES >
317 SKB_DATA_ALIGN(sizeof(struct skb_shared_info)));
318 __iowrite64_copy(tx_queue->piobuf, skb->data,
319 ALIGN(skb->len, L1_CACHE_BYTES) >> 3);
322 buffer->skb = skb;
323 buffer->flags = EFX_TX_BUF_SKB | EFX_TX_BUF_OPTION;
325 EFX_POPULATE_QWORD_5(buffer->option,
326 ESF_DZ_TX_DESC_IS_OPT, 1,
327 ESF_DZ_TX_OPTION_TYPE, ESE_DZ_TX_OPTION_DESC_PIO,
328 ESF_DZ_TX_PIO_CONT, 0,
329 ESF_DZ_TX_PIO_BYTE_CNT, skb->len,
330 ESF_DZ_TX_PIO_BUF_ADDR,
331 tx_queue->piobuf_offset);
332 ++tx_queue->insert_count;
333 return 0;
335 #endif /* EFX_USE_PIO */
337 static struct efx_tx_buffer *efx_tx_map_chunk(struct efx_tx_queue *tx_queue,
338 dma_addr_t dma_addr,
339 size_t len)
341 const struct efx_nic_type *nic_type = tx_queue->efx->type;
342 struct efx_tx_buffer *buffer;
343 unsigned int dma_len;
345 /* Map the fragment taking account of NIC-dependent DMA limits. */
346 do {
347 buffer = efx_tx_queue_get_insert_buffer(tx_queue);
348 dma_len = nic_type->tx_limit_len(tx_queue, dma_addr, len);
350 buffer->len = dma_len;
351 buffer->dma_addr = dma_addr;
352 buffer->flags = EFX_TX_BUF_CONT;
353 len -= dma_len;
354 dma_addr += dma_len;
355 ++tx_queue->insert_count;
356 } while (len);
358 return buffer;
361 /* Map all data from an SKB for DMA and create descriptors on the queue.
363 static int efx_tx_map_data(struct efx_tx_queue *tx_queue, struct sk_buff *skb,
364 unsigned int segment_count)
366 struct efx_nic *efx = tx_queue->efx;
367 struct device *dma_dev = &efx->pci_dev->dev;
368 unsigned int frag_index, nr_frags;
369 dma_addr_t dma_addr, unmap_addr;
370 unsigned short dma_flags;
371 size_t len, unmap_len;
373 nr_frags = skb_shinfo(skb)->nr_frags;
374 frag_index = 0;
376 /* Map header data. */
377 len = skb_headlen(skb);
378 dma_addr = dma_map_single(dma_dev, skb->data, len, DMA_TO_DEVICE);
379 dma_flags = EFX_TX_BUF_MAP_SINGLE;
380 unmap_len = len;
381 unmap_addr = dma_addr;
383 if (unlikely(dma_mapping_error(dma_dev, dma_addr)))
384 return -EIO;
386 if (segment_count) {
387 /* For TSO we need to put the header in to a separate
388 * descriptor. Map this separately if necessary.
390 size_t header_len = skb_transport_header(skb) - skb->data +
391 (tcp_hdr(skb)->doff << 2u);
393 if (header_len != len) {
394 tx_queue->tso_long_headers++;
395 efx_tx_map_chunk(tx_queue, dma_addr, header_len);
396 len -= header_len;
397 dma_addr += header_len;
401 /* Add descriptors for each fragment. */
402 do {
403 struct efx_tx_buffer *buffer;
404 skb_frag_t *fragment;
406 buffer = efx_tx_map_chunk(tx_queue, dma_addr, len);
408 /* The final descriptor for a fragment is responsible for
409 * unmapping the whole fragment.
411 buffer->flags = EFX_TX_BUF_CONT | dma_flags;
412 buffer->unmap_len = unmap_len;
413 buffer->dma_offset = buffer->dma_addr - unmap_addr;
415 if (frag_index >= nr_frags) {
416 /* Store SKB details with the final buffer for
417 * the completion.
419 buffer->skb = skb;
420 buffer->flags = EFX_TX_BUF_SKB | dma_flags;
421 return 0;
424 /* Move on to the next fragment. */
425 fragment = &skb_shinfo(skb)->frags[frag_index++];
426 len = skb_frag_size(fragment);
427 dma_addr = skb_frag_dma_map(dma_dev, fragment,
428 0, len, DMA_TO_DEVICE);
429 dma_flags = 0;
430 unmap_len = len;
431 unmap_addr = dma_addr;
433 if (unlikely(dma_mapping_error(dma_dev, dma_addr)))
434 return -EIO;
435 } while (1);
438 /* Remove buffers put into a tx_queue. None of the buffers must have
439 * an skb attached.
441 static void efx_enqueue_unwind(struct efx_tx_queue *tx_queue)
443 struct efx_tx_buffer *buffer;
444 unsigned int bytes_compl = 0;
445 unsigned int pkts_compl = 0;
447 /* Work backwards until we hit the original insert pointer value */
448 while (tx_queue->insert_count != tx_queue->write_count) {
449 --tx_queue->insert_count;
450 buffer = __efx_tx_queue_get_insert_buffer(tx_queue);
451 efx_dequeue_buffer(tx_queue, buffer, &pkts_compl, &bytes_compl);
456 * Fallback to software TSO.
458 * This is used if we are unable to send a GSO packet through hardware TSO.
459 * This should only ever happen due to per-queue restrictions - unsupported
460 * packets should first be filtered by the feature flags.
462 * Returns 0 on success, error code otherwise.
464 static int efx_tx_tso_fallback(struct efx_tx_queue *tx_queue,
465 struct sk_buff *skb)
467 struct sk_buff *segments, *next;
469 segments = skb_gso_segment(skb, 0);
470 if (IS_ERR(segments))
471 return PTR_ERR(segments);
473 dev_kfree_skb_any(skb);
474 skb = segments;
476 while (skb) {
477 next = skb->next;
478 skb->next = NULL;
480 if (next)
481 skb->xmit_more = true;
482 efx_enqueue_skb(tx_queue, skb);
483 skb = next;
486 return 0;
490 * Add a socket buffer to a TX queue
492 * This maps all fragments of a socket buffer for DMA and adds them to
493 * the TX queue. The queue's insert pointer will be incremented by
494 * the number of fragments in the socket buffer.
496 * If any DMA mapping fails, any mapped fragments will be unmapped,
497 * the queue's insert pointer will be restored to its original value.
499 * This function is split out from efx_hard_start_xmit to allow the
500 * loopback test to direct packets via specific TX queues.
502 * Returns NETDEV_TX_OK.
503 * You must hold netif_tx_lock() to call this function.
505 netdev_tx_t efx_enqueue_skb(struct efx_tx_queue *tx_queue, struct sk_buff *skb)
507 bool data_mapped = false;
508 unsigned int segments;
509 unsigned int skb_len;
510 int rc;
512 skb_len = skb->len;
513 segments = skb_is_gso(skb) ? skb_shinfo(skb)->gso_segs : 0;
514 if (segments == 1)
515 segments = 0; /* Don't use TSO for a single segment. */
517 /* Handle TSO first - it's *possible* (although unlikely) that we might
518 * be passed a packet to segment that's smaller than the copybreak/PIO
519 * size limit.
521 if (segments) {
522 EFX_WARN_ON_ONCE_PARANOID(!tx_queue->handle_tso);
523 rc = tx_queue->handle_tso(tx_queue, skb, &data_mapped);
524 if (rc == -EINVAL) {
525 rc = efx_tx_tso_fallback(tx_queue, skb);
526 tx_queue->tso_fallbacks++;
527 if (rc == 0)
528 return 0;
530 if (rc)
531 goto err;
532 #ifdef EFX_USE_PIO
533 } else if (skb_len <= efx_piobuf_size && !skb->xmit_more &&
534 efx_nic_may_tx_pio(tx_queue)) {
535 /* Use PIO for short packets with an empty queue. */
536 if (efx_enqueue_skb_pio(tx_queue, skb))
537 goto err;
538 tx_queue->pio_packets++;
539 data_mapped = true;
540 #endif
541 } else if (skb->data_len && skb_len <= EFX_TX_CB_SIZE) {
542 /* Pad short packets or coalesce short fragmented packets. */
543 if (efx_enqueue_skb_copy(tx_queue, skb))
544 goto err;
545 tx_queue->cb_packets++;
546 data_mapped = true;
549 /* Map for DMA and create descriptors if we haven't done so already. */
550 if (!data_mapped && (efx_tx_map_data(tx_queue, skb, segments)))
551 goto err;
553 /* Update BQL */
554 netdev_tx_sent_queue(tx_queue->core_txq, skb_len);
556 /* Pass off to hardware */
557 if (!skb->xmit_more || netif_xmit_stopped(tx_queue->core_txq)) {
558 struct efx_tx_queue *txq2 = efx_tx_queue_partner(tx_queue);
560 /* There could be packets left on the partner queue if those
561 * SKBs had skb->xmit_more set. If we do not push those they
562 * could be left for a long time and cause a netdev watchdog.
564 if (txq2->xmit_more_available)
565 efx_nic_push_buffers(txq2);
567 efx_nic_push_buffers(tx_queue);
568 } else {
569 tx_queue->xmit_more_available = skb->xmit_more;
572 if (segments) {
573 tx_queue->tso_bursts++;
574 tx_queue->tso_packets += segments;
575 tx_queue->tx_packets += segments;
576 } else {
577 tx_queue->tx_packets++;
580 efx_tx_maybe_stop_queue(tx_queue);
582 return NETDEV_TX_OK;
585 err:
586 efx_enqueue_unwind(tx_queue);
587 dev_kfree_skb_any(skb);
588 return NETDEV_TX_OK;
591 /* Remove packets from the TX queue
593 * This removes packets from the TX queue, up to and including the
594 * specified index.
596 static void efx_dequeue_buffers(struct efx_tx_queue *tx_queue,
597 unsigned int index,
598 unsigned int *pkts_compl,
599 unsigned int *bytes_compl)
601 struct efx_nic *efx = tx_queue->efx;
602 unsigned int stop_index, read_ptr;
604 stop_index = (index + 1) & tx_queue->ptr_mask;
605 read_ptr = tx_queue->read_count & tx_queue->ptr_mask;
607 while (read_ptr != stop_index) {
608 struct efx_tx_buffer *buffer = &tx_queue->buffer[read_ptr];
610 if (!(buffer->flags & EFX_TX_BUF_OPTION) &&
611 unlikely(buffer->len == 0)) {
612 netif_err(efx, tx_err, efx->net_dev,
613 "TX queue %d spurious TX completion id %x\n",
614 tx_queue->queue, read_ptr);
615 efx_schedule_reset(efx, RESET_TYPE_TX_SKIP);
616 return;
619 efx_dequeue_buffer(tx_queue, buffer, pkts_compl, bytes_compl);
621 ++tx_queue->read_count;
622 read_ptr = tx_queue->read_count & tx_queue->ptr_mask;
626 /* Initiate a packet transmission. We use one channel per CPU
627 * (sharing when we have more CPUs than channels). On Falcon, the TX
628 * completion events will be directed back to the CPU that transmitted
629 * the packet, which should be cache-efficient.
631 * Context: non-blocking.
632 * Note that returning anything other than NETDEV_TX_OK will cause the
633 * OS to free the skb.
635 netdev_tx_t efx_hard_start_xmit(struct sk_buff *skb,
636 struct net_device *net_dev)
638 struct efx_nic *efx = netdev_priv(net_dev);
639 struct efx_tx_queue *tx_queue;
640 unsigned index, type;
642 EFX_WARN_ON_PARANOID(!netif_device_present(net_dev));
644 /* PTP "event" packet */
645 if (unlikely(efx_xmit_with_hwtstamp(skb)) &&
646 unlikely(efx_ptp_is_ptp_tx(efx, skb))) {
647 return efx_ptp_tx(efx, skb);
650 index = skb_get_queue_mapping(skb);
651 type = skb->ip_summed == CHECKSUM_PARTIAL ? EFX_TXQ_TYPE_OFFLOAD : 0;
652 if (index >= efx->n_tx_channels) {
653 index -= efx->n_tx_channels;
654 type |= EFX_TXQ_TYPE_HIGHPRI;
656 tx_queue = efx_get_tx_queue(efx, index, type);
658 return efx_enqueue_skb(tx_queue, skb);
661 void efx_init_tx_queue_core_txq(struct efx_tx_queue *tx_queue)
663 struct efx_nic *efx = tx_queue->efx;
665 /* Must be inverse of queue lookup in efx_hard_start_xmit() */
666 tx_queue->core_txq =
667 netdev_get_tx_queue(efx->net_dev,
668 tx_queue->queue / EFX_TXQ_TYPES +
669 ((tx_queue->queue & EFX_TXQ_TYPE_HIGHPRI) ?
670 efx->n_tx_channels : 0));
673 int efx_setup_tc(struct net_device *net_dev, enum tc_setup_type type,
674 void *type_data)
676 struct efx_nic *efx = netdev_priv(net_dev);
677 struct tc_mqprio_qopt *mqprio = type_data;
678 struct efx_channel *channel;
679 struct efx_tx_queue *tx_queue;
680 unsigned tc, num_tc;
681 int rc;
683 if (type != TC_SETUP_QDISC_MQPRIO)
684 return -EOPNOTSUPP;
686 num_tc = mqprio->num_tc;
688 if (num_tc > EFX_MAX_TX_TC)
689 return -EINVAL;
691 mqprio->hw = TC_MQPRIO_HW_OFFLOAD_TCS;
693 if (num_tc == net_dev->num_tc)
694 return 0;
696 for (tc = 0; tc < num_tc; tc++) {
697 net_dev->tc_to_txq[tc].offset = tc * efx->n_tx_channels;
698 net_dev->tc_to_txq[tc].count = efx->n_tx_channels;
701 if (num_tc > net_dev->num_tc) {
702 /* Initialise high-priority queues as necessary */
703 efx_for_each_channel(channel, efx) {
704 efx_for_each_possible_channel_tx_queue(tx_queue,
705 channel) {
706 if (!(tx_queue->queue & EFX_TXQ_TYPE_HIGHPRI))
707 continue;
708 if (!tx_queue->buffer) {
709 rc = efx_probe_tx_queue(tx_queue);
710 if (rc)
711 return rc;
713 if (!tx_queue->initialised)
714 efx_init_tx_queue(tx_queue);
715 efx_init_tx_queue_core_txq(tx_queue);
718 } else {
719 /* Reduce number of classes before number of queues */
720 net_dev->num_tc = num_tc;
723 rc = netif_set_real_num_tx_queues(net_dev,
724 max_t(int, num_tc, 1) *
725 efx->n_tx_channels);
726 if (rc)
727 return rc;
729 /* Do not destroy high-priority queues when they become
730 * unused. We would have to flush them first, and it is
731 * fairly difficult to flush a subset of TX queues. Leave
732 * it to efx_fini_channels().
735 net_dev->num_tc = num_tc;
736 return 0;
739 void efx_xmit_done(struct efx_tx_queue *tx_queue, unsigned int index)
741 unsigned fill_level;
742 struct efx_nic *efx = tx_queue->efx;
743 struct efx_tx_queue *txq2;
744 unsigned int pkts_compl = 0, bytes_compl = 0;
746 EFX_WARN_ON_ONCE_PARANOID(index > tx_queue->ptr_mask);
748 efx_dequeue_buffers(tx_queue, index, &pkts_compl, &bytes_compl);
749 tx_queue->pkts_compl += pkts_compl;
750 tx_queue->bytes_compl += bytes_compl;
752 if (pkts_compl > 1)
753 ++tx_queue->merge_events;
755 /* See if we need to restart the netif queue. This memory
756 * barrier ensures that we write read_count (inside
757 * efx_dequeue_buffers()) before reading the queue status.
759 smp_mb();
760 if (unlikely(netif_tx_queue_stopped(tx_queue->core_txq)) &&
761 likely(efx->port_enabled) &&
762 likely(netif_device_present(efx->net_dev))) {
763 txq2 = efx_tx_queue_partner(tx_queue);
764 fill_level = max(tx_queue->insert_count - tx_queue->read_count,
765 txq2->insert_count - txq2->read_count);
766 if (fill_level <= efx->txq_wake_thresh)
767 netif_tx_wake_queue(tx_queue->core_txq);
770 /* Check whether the hardware queue is now empty */
771 if ((int)(tx_queue->read_count - tx_queue->old_write_count) >= 0) {
772 tx_queue->old_write_count = READ_ONCE(tx_queue->write_count);
773 if (tx_queue->read_count == tx_queue->old_write_count) {
774 smp_mb();
775 tx_queue->empty_read_count =
776 tx_queue->read_count | EFX_EMPTY_COUNT_VALID;
781 static unsigned int efx_tx_cb_page_count(struct efx_tx_queue *tx_queue)
783 return DIV_ROUND_UP(tx_queue->ptr_mask + 1, PAGE_SIZE >> EFX_TX_CB_ORDER);
786 int efx_probe_tx_queue(struct efx_tx_queue *tx_queue)
788 struct efx_nic *efx = tx_queue->efx;
789 unsigned int entries;
790 int rc;
792 /* Create the smallest power-of-two aligned ring */
793 entries = max(roundup_pow_of_two(efx->txq_entries), EFX_MIN_DMAQ_SIZE);
794 EFX_WARN_ON_PARANOID(entries > EFX_MAX_DMAQ_SIZE);
795 tx_queue->ptr_mask = entries - 1;
797 netif_dbg(efx, probe, efx->net_dev,
798 "creating TX queue %d size %#x mask %#x\n",
799 tx_queue->queue, efx->txq_entries, tx_queue->ptr_mask);
801 /* Allocate software ring */
802 tx_queue->buffer = kcalloc(entries, sizeof(*tx_queue->buffer),
803 GFP_KERNEL);
804 if (!tx_queue->buffer)
805 return -ENOMEM;
807 tx_queue->cb_page = kcalloc(efx_tx_cb_page_count(tx_queue),
808 sizeof(tx_queue->cb_page[0]), GFP_KERNEL);
809 if (!tx_queue->cb_page) {
810 rc = -ENOMEM;
811 goto fail1;
814 /* Allocate hardware ring */
815 rc = efx_nic_probe_tx(tx_queue);
816 if (rc)
817 goto fail2;
819 return 0;
821 fail2:
822 kfree(tx_queue->cb_page);
823 tx_queue->cb_page = NULL;
824 fail1:
825 kfree(tx_queue->buffer);
826 tx_queue->buffer = NULL;
827 return rc;
830 void efx_init_tx_queue(struct efx_tx_queue *tx_queue)
832 struct efx_nic *efx = tx_queue->efx;
834 netif_dbg(efx, drv, efx->net_dev,
835 "initialising TX queue %d\n", tx_queue->queue);
837 tx_queue->insert_count = 0;
838 tx_queue->write_count = 0;
839 tx_queue->packet_write_count = 0;
840 tx_queue->old_write_count = 0;
841 tx_queue->read_count = 0;
842 tx_queue->old_read_count = 0;
843 tx_queue->empty_read_count = 0 | EFX_EMPTY_COUNT_VALID;
844 tx_queue->xmit_more_available = false;
845 tx_queue->timestamping = (efx_ptp_use_mac_tx_timestamps(efx) &&
846 tx_queue->channel == efx_ptp_channel(efx));
847 tx_queue->completed_desc_ptr = tx_queue->ptr_mask;
848 tx_queue->completed_timestamp_major = 0;
849 tx_queue->completed_timestamp_minor = 0;
851 /* Set up default function pointers. These may get replaced by
852 * efx_nic_init_tx() based off NIC/queue capabilities.
854 tx_queue->handle_tso = efx_enqueue_skb_tso;
856 /* Set up TX descriptor ring */
857 efx_nic_init_tx(tx_queue);
859 tx_queue->initialised = true;
862 void efx_fini_tx_queue(struct efx_tx_queue *tx_queue)
864 struct efx_tx_buffer *buffer;
866 netif_dbg(tx_queue->efx, drv, tx_queue->efx->net_dev,
867 "shutting down TX queue %d\n", tx_queue->queue);
869 if (!tx_queue->buffer)
870 return;
872 /* Free any buffers left in the ring */
873 while (tx_queue->read_count != tx_queue->write_count) {
874 unsigned int pkts_compl = 0, bytes_compl = 0;
875 buffer = &tx_queue->buffer[tx_queue->read_count & tx_queue->ptr_mask];
876 efx_dequeue_buffer(tx_queue, buffer, &pkts_compl, &bytes_compl);
878 ++tx_queue->read_count;
880 tx_queue->xmit_more_available = false;
881 netdev_tx_reset_queue(tx_queue->core_txq);
884 void efx_remove_tx_queue(struct efx_tx_queue *tx_queue)
886 int i;
888 if (!tx_queue->buffer)
889 return;
891 netif_dbg(tx_queue->efx, drv, tx_queue->efx->net_dev,
892 "destroying TX queue %d\n", tx_queue->queue);
893 efx_nic_remove_tx(tx_queue);
895 if (tx_queue->cb_page) {
896 for (i = 0; i < efx_tx_cb_page_count(tx_queue); i++)
897 efx_nic_free_buffer(tx_queue->efx,
898 &tx_queue->cb_page[i]);
899 kfree(tx_queue->cb_page);
900 tx_queue->cb_page = NULL;
903 kfree(tx_queue->buffer);
904 tx_queue->buffer = NULL;