1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0-only
2*4882a593Smuzhiyun /****************************************************************************
3*4882a593Smuzhiyun * Driver for Solarflare network controllers and boards
4*4882a593Smuzhiyun * Copyright 2005-2006 Fen Systems Ltd.
5*4882a593Smuzhiyun * Copyright 2005-2013 Solarflare Communications Inc.
6*4882a593Smuzhiyun */
7*4882a593Smuzhiyun
8*4882a593Smuzhiyun #include <linux/pci.h>
9*4882a593Smuzhiyun #include <linux/tcp.h>
10*4882a593Smuzhiyun #include <linux/ip.h>
11*4882a593Smuzhiyun #include <linux/in.h>
12*4882a593Smuzhiyun #include <linux/ipv6.h>
13*4882a593Smuzhiyun #include <linux/slab.h>
14*4882a593Smuzhiyun #include <net/ipv6.h>
15*4882a593Smuzhiyun #include <linux/if_ether.h>
16*4882a593Smuzhiyun #include <linux/highmem.h>
17*4882a593Smuzhiyun #include <linux/cache.h>
18*4882a593Smuzhiyun #include "net_driver.h"
19*4882a593Smuzhiyun #include "efx.h"
20*4882a593Smuzhiyun #include "io.h"
21*4882a593Smuzhiyun #include "nic.h"
22*4882a593Smuzhiyun #include "tx.h"
23*4882a593Smuzhiyun #include "tx_common.h"
24*4882a593Smuzhiyun #include "workarounds.h"
25*4882a593Smuzhiyun #include "ef10_regs.h"
26*4882a593Smuzhiyun
27*4882a593Smuzhiyun #ifdef EFX_USE_PIO
28*4882a593Smuzhiyun
29*4882a593Smuzhiyun #define EFX_PIOBUF_SIZE_DEF ALIGN(256, L1_CACHE_BYTES)
30*4882a593Smuzhiyun unsigned int efx_piobuf_size __read_mostly = EFX_PIOBUF_SIZE_DEF;
31*4882a593Smuzhiyun
32*4882a593Smuzhiyun #endif /* EFX_USE_PIO */
33*4882a593Smuzhiyun
efx_tx_get_copy_buffer(struct efx_tx_queue * tx_queue,struct efx_tx_buffer * buffer)34*4882a593Smuzhiyun static inline u8 *efx_tx_get_copy_buffer(struct efx_tx_queue *tx_queue,
35*4882a593Smuzhiyun struct efx_tx_buffer *buffer)
36*4882a593Smuzhiyun {
37*4882a593Smuzhiyun unsigned int index = efx_tx_queue_get_insert_index(tx_queue);
38*4882a593Smuzhiyun struct efx_buffer *page_buf =
39*4882a593Smuzhiyun &tx_queue->cb_page[index >> (PAGE_SHIFT - EFX_TX_CB_ORDER)];
40*4882a593Smuzhiyun unsigned int offset =
41*4882a593Smuzhiyun ((index << EFX_TX_CB_ORDER) + NET_IP_ALIGN) & (PAGE_SIZE - 1);
42*4882a593Smuzhiyun
43*4882a593Smuzhiyun if (unlikely(!page_buf->addr) &&
44*4882a593Smuzhiyun efx_nic_alloc_buffer(tx_queue->efx, page_buf, PAGE_SIZE,
45*4882a593Smuzhiyun GFP_ATOMIC))
46*4882a593Smuzhiyun return NULL;
47*4882a593Smuzhiyun buffer->dma_addr = page_buf->dma_addr + offset;
48*4882a593Smuzhiyun buffer->unmap_len = 0;
49*4882a593Smuzhiyun return (u8 *)page_buf->addr + offset;
50*4882a593Smuzhiyun }
51*4882a593Smuzhiyun
efx_tx_get_copy_buffer_limited(struct efx_tx_queue * tx_queue,struct efx_tx_buffer * buffer,size_t len)52*4882a593Smuzhiyun u8 *efx_tx_get_copy_buffer_limited(struct efx_tx_queue *tx_queue,
53*4882a593Smuzhiyun struct efx_tx_buffer *buffer, size_t len)
54*4882a593Smuzhiyun {
55*4882a593Smuzhiyun if (len > EFX_TX_CB_SIZE)
56*4882a593Smuzhiyun return NULL;
57*4882a593Smuzhiyun return efx_tx_get_copy_buffer(tx_queue, buffer);
58*4882a593Smuzhiyun }
59*4882a593Smuzhiyun
efx_tx_maybe_stop_queue(struct efx_tx_queue * txq1)60*4882a593Smuzhiyun static void efx_tx_maybe_stop_queue(struct efx_tx_queue *txq1)
61*4882a593Smuzhiyun {
62*4882a593Smuzhiyun /* We need to consider all queues that the net core sees as one */
63*4882a593Smuzhiyun struct efx_nic *efx = txq1->efx;
64*4882a593Smuzhiyun struct efx_tx_queue *txq2;
65*4882a593Smuzhiyun unsigned int fill_level;
66*4882a593Smuzhiyun
67*4882a593Smuzhiyun fill_level = efx_channel_tx_old_fill_level(txq1->channel);
68*4882a593Smuzhiyun if (likely(fill_level < efx->txq_stop_thresh))
69*4882a593Smuzhiyun return;
70*4882a593Smuzhiyun
71*4882a593Smuzhiyun /* We used the stale old_read_count above, which gives us a
72*4882a593Smuzhiyun * pessimistic estimate of the fill level (which may even
73*4882a593Smuzhiyun * validly be >= efx->txq_entries). Now try again using
74*4882a593Smuzhiyun * read_count (more likely to be a cache miss).
75*4882a593Smuzhiyun *
76*4882a593Smuzhiyun * If we read read_count and then conditionally stop the
77*4882a593Smuzhiyun * queue, it is possible for the completion path to race with
78*4882a593Smuzhiyun * us and complete all outstanding descriptors in the middle,
79*4882a593Smuzhiyun * after which there will be no more completions to wake it.
80*4882a593Smuzhiyun * Therefore we stop the queue first, then read read_count
81*4882a593Smuzhiyun * (with a memory barrier to ensure the ordering), then
82*4882a593Smuzhiyun * restart the queue if the fill level turns out to be low
83*4882a593Smuzhiyun * enough.
84*4882a593Smuzhiyun */
85*4882a593Smuzhiyun netif_tx_stop_queue(txq1->core_txq);
86*4882a593Smuzhiyun smp_mb();
87*4882a593Smuzhiyun efx_for_each_channel_tx_queue(txq2, txq1->channel)
88*4882a593Smuzhiyun txq2->old_read_count = READ_ONCE(txq2->read_count);
89*4882a593Smuzhiyun
90*4882a593Smuzhiyun fill_level = efx_channel_tx_old_fill_level(txq1->channel);
91*4882a593Smuzhiyun EFX_WARN_ON_ONCE_PARANOID(fill_level >= efx->txq_entries);
92*4882a593Smuzhiyun if (likely(fill_level < efx->txq_stop_thresh)) {
93*4882a593Smuzhiyun smp_mb();
94*4882a593Smuzhiyun if (likely(!efx->loopback_selftest))
95*4882a593Smuzhiyun netif_tx_start_queue(txq1->core_txq);
96*4882a593Smuzhiyun }
97*4882a593Smuzhiyun }
98*4882a593Smuzhiyun
efx_enqueue_skb_copy(struct efx_tx_queue * tx_queue,struct sk_buff * skb)99*4882a593Smuzhiyun static int efx_enqueue_skb_copy(struct efx_tx_queue *tx_queue,
100*4882a593Smuzhiyun struct sk_buff *skb)
101*4882a593Smuzhiyun {
102*4882a593Smuzhiyun unsigned int copy_len = skb->len;
103*4882a593Smuzhiyun struct efx_tx_buffer *buffer;
104*4882a593Smuzhiyun u8 *copy_buffer;
105*4882a593Smuzhiyun int rc;
106*4882a593Smuzhiyun
107*4882a593Smuzhiyun EFX_WARN_ON_ONCE_PARANOID(copy_len > EFX_TX_CB_SIZE);
108*4882a593Smuzhiyun
109*4882a593Smuzhiyun buffer = efx_tx_queue_get_insert_buffer(tx_queue);
110*4882a593Smuzhiyun
111*4882a593Smuzhiyun copy_buffer = efx_tx_get_copy_buffer(tx_queue, buffer);
112*4882a593Smuzhiyun if (unlikely(!copy_buffer))
113*4882a593Smuzhiyun return -ENOMEM;
114*4882a593Smuzhiyun
115*4882a593Smuzhiyun rc = skb_copy_bits(skb, 0, copy_buffer, copy_len);
116*4882a593Smuzhiyun EFX_WARN_ON_PARANOID(rc);
117*4882a593Smuzhiyun buffer->len = copy_len;
118*4882a593Smuzhiyun
119*4882a593Smuzhiyun buffer->skb = skb;
120*4882a593Smuzhiyun buffer->flags = EFX_TX_BUF_SKB;
121*4882a593Smuzhiyun
122*4882a593Smuzhiyun ++tx_queue->insert_count;
123*4882a593Smuzhiyun return rc;
124*4882a593Smuzhiyun }
125*4882a593Smuzhiyun
126*4882a593Smuzhiyun #ifdef EFX_USE_PIO
127*4882a593Smuzhiyun
128*4882a593Smuzhiyun struct efx_short_copy_buffer {
129*4882a593Smuzhiyun int used;
130*4882a593Smuzhiyun u8 buf[L1_CACHE_BYTES];
131*4882a593Smuzhiyun };
132*4882a593Smuzhiyun
133*4882a593Smuzhiyun /* Copy to PIO, respecting that writes to PIO buffers must be dword aligned.
134*4882a593Smuzhiyun * Advances piobuf pointer. Leaves additional data in the copy buffer.
135*4882a593Smuzhiyun */
efx_memcpy_toio_aligned(struct efx_nic * efx,u8 __iomem ** piobuf,u8 * data,int len,struct efx_short_copy_buffer * copy_buf)136*4882a593Smuzhiyun static void efx_memcpy_toio_aligned(struct efx_nic *efx, u8 __iomem **piobuf,
137*4882a593Smuzhiyun u8 *data, int len,
138*4882a593Smuzhiyun struct efx_short_copy_buffer *copy_buf)
139*4882a593Smuzhiyun {
140*4882a593Smuzhiyun int block_len = len & ~(sizeof(copy_buf->buf) - 1);
141*4882a593Smuzhiyun
142*4882a593Smuzhiyun __iowrite64_copy(*piobuf, data, block_len >> 3);
143*4882a593Smuzhiyun *piobuf += block_len;
144*4882a593Smuzhiyun len -= block_len;
145*4882a593Smuzhiyun
146*4882a593Smuzhiyun if (len) {
147*4882a593Smuzhiyun data += block_len;
148*4882a593Smuzhiyun BUG_ON(copy_buf->used);
149*4882a593Smuzhiyun BUG_ON(len > sizeof(copy_buf->buf));
150*4882a593Smuzhiyun memcpy(copy_buf->buf, data, len);
151*4882a593Smuzhiyun copy_buf->used = len;
152*4882a593Smuzhiyun }
153*4882a593Smuzhiyun }
154*4882a593Smuzhiyun
155*4882a593Smuzhiyun /* Copy to PIO, respecting dword alignment, popping data from copy buffer first.
156*4882a593Smuzhiyun * Advances piobuf pointer. Leaves additional data in the copy buffer.
157*4882a593Smuzhiyun */
efx_memcpy_toio_aligned_cb(struct efx_nic * efx,u8 __iomem ** piobuf,u8 * data,int len,struct efx_short_copy_buffer * copy_buf)158*4882a593Smuzhiyun static void efx_memcpy_toio_aligned_cb(struct efx_nic *efx, u8 __iomem **piobuf,
159*4882a593Smuzhiyun u8 *data, int len,
160*4882a593Smuzhiyun struct efx_short_copy_buffer *copy_buf)
161*4882a593Smuzhiyun {
162*4882a593Smuzhiyun if (copy_buf->used) {
163*4882a593Smuzhiyun /* if the copy buffer is partially full, fill it up and write */
164*4882a593Smuzhiyun int copy_to_buf =
165*4882a593Smuzhiyun min_t(int, sizeof(copy_buf->buf) - copy_buf->used, len);
166*4882a593Smuzhiyun
167*4882a593Smuzhiyun memcpy(copy_buf->buf + copy_buf->used, data, copy_to_buf);
168*4882a593Smuzhiyun copy_buf->used += copy_to_buf;
169*4882a593Smuzhiyun
170*4882a593Smuzhiyun /* if we didn't fill it up then we're done for now */
171*4882a593Smuzhiyun if (copy_buf->used < sizeof(copy_buf->buf))
172*4882a593Smuzhiyun return;
173*4882a593Smuzhiyun
174*4882a593Smuzhiyun __iowrite64_copy(*piobuf, copy_buf->buf,
175*4882a593Smuzhiyun sizeof(copy_buf->buf) >> 3);
176*4882a593Smuzhiyun *piobuf += sizeof(copy_buf->buf);
177*4882a593Smuzhiyun data += copy_to_buf;
178*4882a593Smuzhiyun len -= copy_to_buf;
179*4882a593Smuzhiyun copy_buf->used = 0;
180*4882a593Smuzhiyun }
181*4882a593Smuzhiyun
182*4882a593Smuzhiyun efx_memcpy_toio_aligned(efx, piobuf, data, len, copy_buf);
183*4882a593Smuzhiyun }
184*4882a593Smuzhiyun
efx_flush_copy_buffer(struct efx_nic * efx,u8 __iomem * piobuf,struct efx_short_copy_buffer * copy_buf)185*4882a593Smuzhiyun static void efx_flush_copy_buffer(struct efx_nic *efx, u8 __iomem *piobuf,
186*4882a593Smuzhiyun struct efx_short_copy_buffer *copy_buf)
187*4882a593Smuzhiyun {
188*4882a593Smuzhiyun /* if there's anything in it, write the whole buffer, including junk */
189*4882a593Smuzhiyun if (copy_buf->used)
190*4882a593Smuzhiyun __iowrite64_copy(piobuf, copy_buf->buf,
191*4882a593Smuzhiyun sizeof(copy_buf->buf) >> 3);
192*4882a593Smuzhiyun }
193*4882a593Smuzhiyun
194*4882a593Smuzhiyun /* Traverse skb structure and copy fragments in to PIO buffer.
195*4882a593Smuzhiyun * Advances piobuf pointer.
196*4882a593Smuzhiyun */
efx_skb_copy_bits_to_pio(struct efx_nic * efx,struct sk_buff * skb,u8 __iomem ** piobuf,struct efx_short_copy_buffer * copy_buf)197*4882a593Smuzhiyun static void efx_skb_copy_bits_to_pio(struct efx_nic *efx, struct sk_buff *skb,
198*4882a593Smuzhiyun u8 __iomem **piobuf,
199*4882a593Smuzhiyun struct efx_short_copy_buffer *copy_buf)
200*4882a593Smuzhiyun {
201*4882a593Smuzhiyun int i;
202*4882a593Smuzhiyun
203*4882a593Smuzhiyun efx_memcpy_toio_aligned(efx, piobuf, skb->data, skb_headlen(skb),
204*4882a593Smuzhiyun copy_buf);
205*4882a593Smuzhiyun
206*4882a593Smuzhiyun for (i = 0; i < skb_shinfo(skb)->nr_frags; ++i) {
207*4882a593Smuzhiyun skb_frag_t *f = &skb_shinfo(skb)->frags[i];
208*4882a593Smuzhiyun u8 *vaddr;
209*4882a593Smuzhiyun
210*4882a593Smuzhiyun vaddr = kmap_atomic(skb_frag_page(f));
211*4882a593Smuzhiyun
212*4882a593Smuzhiyun efx_memcpy_toio_aligned_cb(efx, piobuf, vaddr + skb_frag_off(f),
213*4882a593Smuzhiyun skb_frag_size(f), copy_buf);
214*4882a593Smuzhiyun kunmap_atomic(vaddr);
215*4882a593Smuzhiyun }
216*4882a593Smuzhiyun
217*4882a593Smuzhiyun EFX_WARN_ON_ONCE_PARANOID(skb_shinfo(skb)->frag_list);
218*4882a593Smuzhiyun }
219*4882a593Smuzhiyun
efx_enqueue_skb_pio(struct efx_tx_queue * tx_queue,struct sk_buff * skb)220*4882a593Smuzhiyun static int efx_enqueue_skb_pio(struct efx_tx_queue *tx_queue,
221*4882a593Smuzhiyun struct sk_buff *skb)
222*4882a593Smuzhiyun {
223*4882a593Smuzhiyun struct efx_tx_buffer *buffer =
224*4882a593Smuzhiyun efx_tx_queue_get_insert_buffer(tx_queue);
225*4882a593Smuzhiyun u8 __iomem *piobuf = tx_queue->piobuf;
226*4882a593Smuzhiyun
227*4882a593Smuzhiyun /* Copy to PIO buffer. Ensure the writes are padded to the end
228*4882a593Smuzhiyun * of a cache line, as this is required for write-combining to be
229*4882a593Smuzhiyun * effective on at least x86.
230*4882a593Smuzhiyun */
231*4882a593Smuzhiyun
232*4882a593Smuzhiyun if (skb_shinfo(skb)->nr_frags) {
233*4882a593Smuzhiyun /* The size of the copy buffer will ensure all writes
234*4882a593Smuzhiyun * are the size of a cache line.
235*4882a593Smuzhiyun */
236*4882a593Smuzhiyun struct efx_short_copy_buffer copy_buf;
237*4882a593Smuzhiyun
238*4882a593Smuzhiyun copy_buf.used = 0;
239*4882a593Smuzhiyun
240*4882a593Smuzhiyun efx_skb_copy_bits_to_pio(tx_queue->efx, skb,
241*4882a593Smuzhiyun &piobuf, ©_buf);
242*4882a593Smuzhiyun efx_flush_copy_buffer(tx_queue->efx, piobuf, ©_buf);
243*4882a593Smuzhiyun } else {
244*4882a593Smuzhiyun /* Pad the write to the size of a cache line.
245*4882a593Smuzhiyun * We can do this because we know the skb_shared_info struct is
246*4882a593Smuzhiyun * after the source, and the destination buffer is big enough.
247*4882a593Smuzhiyun */
248*4882a593Smuzhiyun BUILD_BUG_ON(L1_CACHE_BYTES >
249*4882a593Smuzhiyun SKB_DATA_ALIGN(sizeof(struct skb_shared_info)));
250*4882a593Smuzhiyun __iowrite64_copy(tx_queue->piobuf, skb->data,
251*4882a593Smuzhiyun ALIGN(skb->len, L1_CACHE_BYTES) >> 3);
252*4882a593Smuzhiyun }
253*4882a593Smuzhiyun
254*4882a593Smuzhiyun buffer->skb = skb;
255*4882a593Smuzhiyun buffer->flags = EFX_TX_BUF_SKB | EFX_TX_BUF_OPTION;
256*4882a593Smuzhiyun
257*4882a593Smuzhiyun EFX_POPULATE_QWORD_5(buffer->option,
258*4882a593Smuzhiyun ESF_DZ_TX_DESC_IS_OPT, 1,
259*4882a593Smuzhiyun ESF_DZ_TX_OPTION_TYPE, ESE_DZ_TX_OPTION_DESC_PIO,
260*4882a593Smuzhiyun ESF_DZ_TX_PIO_CONT, 0,
261*4882a593Smuzhiyun ESF_DZ_TX_PIO_BYTE_CNT, skb->len,
262*4882a593Smuzhiyun ESF_DZ_TX_PIO_BUF_ADDR,
263*4882a593Smuzhiyun tx_queue->piobuf_offset);
264*4882a593Smuzhiyun ++tx_queue->insert_count;
265*4882a593Smuzhiyun return 0;
266*4882a593Smuzhiyun }
267*4882a593Smuzhiyun
268*4882a593Smuzhiyun /* Decide whether we can use TX PIO, ie. write packet data directly into
269*4882a593Smuzhiyun * a buffer on the device. This can reduce latency at the expense of
270*4882a593Smuzhiyun * throughput, so we only do this if both hardware and software TX rings
271*4882a593Smuzhiyun * are empty, including all queues for the channel. This also ensures that
272*4882a593Smuzhiyun * only one packet at a time can be using the PIO buffer. If the xmit_more
273*4882a593Smuzhiyun * flag is set then we don't use this - there'll be another packet along
274*4882a593Smuzhiyun * shortly and we want to hold off the doorbell.
275*4882a593Smuzhiyun */
efx_tx_may_pio(struct efx_tx_queue * tx_queue)276*4882a593Smuzhiyun static bool efx_tx_may_pio(struct efx_tx_queue *tx_queue)
277*4882a593Smuzhiyun {
278*4882a593Smuzhiyun struct efx_channel *channel = tx_queue->channel;
279*4882a593Smuzhiyun
280*4882a593Smuzhiyun if (!tx_queue->piobuf)
281*4882a593Smuzhiyun return false;
282*4882a593Smuzhiyun
283*4882a593Smuzhiyun EFX_WARN_ON_ONCE_PARANOID(!channel->efx->type->option_descriptors);
284*4882a593Smuzhiyun
285*4882a593Smuzhiyun efx_for_each_channel_tx_queue(tx_queue, channel)
286*4882a593Smuzhiyun if (!efx_nic_tx_is_empty(tx_queue, tx_queue->packet_write_count))
287*4882a593Smuzhiyun return false;
288*4882a593Smuzhiyun
289*4882a593Smuzhiyun return true;
290*4882a593Smuzhiyun }
291*4882a593Smuzhiyun #endif /* EFX_USE_PIO */
292*4882a593Smuzhiyun
293*4882a593Smuzhiyun /* Send any pending traffic for a channel. xmit_more is shared across all
294*4882a593Smuzhiyun * queues for a channel, so we must check all of them.
295*4882a593Smuzhiyun */
efx_tx_send_pending(struct efx_channel * channel)296*4882a593Smuzhiyun static void efx_tx_send_pending(struct efx_channel *channel)
297*4882a593Smuzhiyun {
298*4882a593Smuzhiyun struct efx_tx_queue *q;
299*4882a593Smuzhiyun
300*4882a593Smuzhiyun efx_for_each_channel_tx_queue(q, channel) {
301*4882a593Smuzhiyun if (q->xmit_pending)
302*4882a593Smuzhiyun efx_nic_push_buffers(q);
303*4882a593Smuzhiyun }
304*4882a593Smuzhiyun }
305*4882a593Smuzhiyun
306*4882a593Smuzhiyun /*
307*4882a593Smuzhiyun * Add a socket buffer to a TX queue
308*4882a593Smuzhiyun *
309*4882a593Smuzhiyun * This maps all fragments of a socket buffer for DMA and adds them to
310*4882a593Smuzhiyun * the TX queue. The queue's insert pointer will be incremented by
311*4882a593Smuzhiyun * the number of fragments in the socket buffer.
312*4882a593Smuzhiyun *
313*4882a593Smuzhiyun * If any DMA mapping fails, any mapped fragments will be unmapped,
314*4882a593Smuzhiyun * the queue's insert pointer will be restored to its original value.
315*4882a593Smuzhiyun *
316*4882a593Smuzhiyun * This function is split out from efx_hard_start_xmit to allow the
317*4882a593Smuzhiyun * loopback test to direct packets via specific TX queues.
318*4882a593Smuzhiyun *
319*4882a593Smuzhiyun * Returns NETDEV_TX_OK.
320*4882a593Smuzhiyun * You must hold netif_tx_lock() to call this function.
321*4882a593Smuzhiyun */
__efx_enqueue_skb(struct efx_tx_queue * tx_queue,struct sk_buff * skb)322*4882a593Smuzhiyun netdev_tx_t __efx_enqueue_skb(struct efx_tx_queue *tx_queue, struct sk_buff *skb)
323*4882a593Smuzhiyun {
324*4882a593Smuzhiyun unsigned int old_insert_count = tx_queue->insert_count;
325*4882a593Smuzhiyun bool xmit_more = netdev_xmit_more();
326*4882a593Smuzhiyun bool data_mapped = false;
327*4882a593Smuzhiyun unsigned int segments;
328*4882a593Smuzhiyun unsigned int skb_len;
329*4882a593Smuzhiyun int rc;
330*4882a593Smuzhiyun
331*4882a593Smuzhiyun skb_len = skb->len;
332*4882a593Smuzhiyun segments = skb_is_gso(skb) ? skb_shinfo(skb)->gso_segs : 0;
333*4882a593Smuzhiyun if (segments == 1)
334*4882a593Smuzhiyun segments = 0; /* Don't use TSO for a single segment. */
335*4882a593Smuzhiyun
336*4882a593Smuzhiyun /* Handle TSO first - it's *possible* (although unlikely) that we might
337*4882a593Smuzhiyun * be passed a packet to segment that's smaller than the copybreak/PIO
338*4882a593Smuzhiyun * size limit.
339*4882a593Smuzhiyun */
340*4882a593Smuzhiyun if (segments) {
341*4882a593Smuzhiyun switch (tx_queue->tso_version) {
342*4882a593Smuzhiyun case 1:
343*4882a593Smuzhiyun rc = efx_enqueue_skb_tso(tx_queue, skb, &data_mapped);
344*4882a593Smuzhiyun break;
345*4882a593Smuzhiyun case 2:
346*4882a593Smuzhiyun rc = efx_ef10_tx_tso_desc(tx_queue, skb, &data_mapped);
347*4882a593Smuzhiyun break;
348*4882a593Smuzhiyun case 0: /* No TSO on this queue, SW fallback needed */
349*4882a593Smuzhiyun default:
350*4882a593Smuzhiyun rc = -EINVAL;
351*4882a593Smuzhiyun break;
352*4882a593Smuzhiyun }
353*4882a593Smuzhiyun if (rc == -EINVAL) {
354*4882a593Smuzhiyun rc = efx_tx_tso_fallback(tx_queue, skb);
355*4882a593Smuzhiyun tx_queue->tso_fallbacks++;
356*4882a593Smuzhiyun if (rc == 0)
357*4882a593Smuzhiyun return 0;
358*4882a593Smuzhiyun }
359*4882a593Smuzhiyun if (rc)
360*4882a593Smuzhiyun goto err;
361*4882a593Smuzhiyun #ifdef EFX_USE_PIO
362*4882a593Smuzhiyun } else if (skb_len <= efx_piobuf_size && !xmit_more &&
363*4882a593Smuzhiyun efx_tx_may_pio(tx_queue)) {
364*4882a593Smuzhiyun /* Use PIO for short packets with an empty queue. */
365*4882a593Smuzhiyun if (efx_enqueue_skb_pio(tx_queue, skb))
366*4882a593Smuzhiyun goto err;
367*4882a593Smuzhiyun tx_queue->pio_packets++;
368*4882a593Smuzhiyun data_mapped = true;
369*4882a593Smuzhiyun #endif
370*4882a593Smuzhiyun } else if (skb->data_len && skb_len <= EFX_TX_CB_SIZE) {
371*4882a593Smuzhiyun /* Pad short packets or coalesce short fragmented packets. */
372*4882a593Smuzhiyun if (efx_enqueue_skb_copy(tx_queue, skb))
373*4882a593Smuzhiyun goto err;
374*4882a593Smuzhiyun tx_queue->cb_packets++;
375*4882a593Smuzhiyun data_mapped = true;
376*4882a593Smuzhiyun }
377*4882a593Smuzhiyun
378*4882a593Smuzhiyun /* Map for DMA and create descriptors if we haven't done so already. */
379*4882a593Smuzhiyun if (!data_mapped && (efx_tx_map_data(tx_queue, skb, segments)))
380*4882a593Smuzhiyun goto err;
381*4882a593Smuzhiyun
382*4882a593Smuzhiyun efx_tx_maybe_stop_queue(tx_queue);
383*4882a593Smuzhiyun
384*4882a593Smuzhiyun tx_queue->xmit_pending = true;
385*4882a593Smuzhiyun
386*4882a593Smuzhiyun /* Pass off to hardware */
387*4882a593Smuzhiyun if (__netdev_tx_sent_queue(tx_queue->core_txq, skb_len, xmit_more))
388*4882a593Smuzhiyun efx_tx_send_pending(tx_queue->channel);
389*4882a593Smuzhiyun
390*4882a593Smuzhiyun if (segments) {
391*4882a593Smuzhiyun tx_queue->tso_bursts++;
392*4882a593Smuzhiyun tx_queue->tso_packets += segments;
393*4882a593Smuzhiyun tx_queue->tx_packets += segments;
394*4882a593Smuzhiyun } else {
395*4882a593Smuzhiyun tx_queue->tx_packets++;
396*4882a593Smuzhiyun }
397*4882a593Smuzhiyun
398*4882a593Smuzhiyun return NETDEV_TX_OK;
399*4882a593Smuzhiyun
400*4882a593Smuzhiyun
401*4882a593Smuzhiyun err:
402*4882a593Smuzhiyun efx_enqueue_unwind(tx_queue, old_insert_count);
403*4882a593Smuzhiyun dev_kfree_skb_any(skb);
404*4882a593Smuzhiyun
405*4882a593Smuzhiyun /* If we're not expecting another transmit and we had something to push
406*4882a593Smuzhiyun * on this queue or a partner queue then we need to push here to get the
407*4882a593Smuzhiyun * previous packets out.
408*4882a593Smuzhiyun */
409*4882a593Smuzhiyun if (!xmit_more)
410*4882a593Smuzhiyun efx_tx_send_pending(tx_queue->channel);
411*4882a593Smuzhiyun
412*4882a593Smuzhiyun return NETDEV_TX_OK;
413*4882a593Smuzhiyun }
414*4882a593Smuzhiyun
efx_xdp_return_frames(int n,struct xdp_frame ** xdpfs)415*4882a593Smuzhiyun static void efx_xdp_return_frames(int n, struct xdp_frame **xdpfs)
416*4882a593Smuzhiyun {
417*4882a593Smuzhiyun int i;
418*4882a593Smuzhiyun
419*4882a593Smuzhiyun for (i = 0; i < n; i++)
420*4882a593Smuzhiyun xdp_return_frame_rx_napi(xdpfs[i]);
421*4882a593Smuzhiyun }
422*4882a593Smuzhiyun
423*4882a593Smuzhiyun /* Transmit a packet from an XDP buffer
424*4882a593Smuzhiyun *
425*4882a593Smuzhiyun * Returns number of packets sent on success, error code otherwise.
426*4882a593Smuzhiyun * Runs in NAPI context, either in our poll (for XDP TX) or a different NIC
427*4882a593Smuzhiyun * (for XDP redirect).
428*4882a593Smuzhiyun */
efx_xdp_tx_buffers(struct efx_nic * efx,int n,struct xdp_frame ** xdpfs,bool flush)429*4882a593Smuzhiyun int efx_xdp_tx_buffers(struct efx_nic *efx, int n, struct xdp_frame **xdpfs,
430*4882a593Smuzhiyun bool flush)
431*4882a593Smuzhiyun {
432*4882a593Smuzhiyun struct efx_tx_buffer *tx_buffer;
433*4882a593Smuzhiyun struct efx_tx_queue *tx_queue;
434*4882a593Smuzhiyun struct xdp_frame *xdpf;
435*4882a593Smuzhiyun dma_addr_t dma_addr;
436*4882a593Smuzhiyun unsigned int len;
437*4882a593Smuzhiyun int space;
438*4882a593Smuzhiyun int cpu;
439*4882a593Smuzhiyun int i;
440*4882a593Smuzhiyun
441*4882a593Smuzhiyun cpu = raw_smp_processor_id();
442*4882a593Smuzhiyun
443*4882a593Smuzhiyun if (!efx->xdp_tx_queue_count ||
444*4882a593Smuzhiyun unlikely(cpu >= efx->xdp_tx_queue_count))
445*4882a593Smuzhiyun return -EINVAL;
446*4882a593Smuzhiyun
447*4882a593Smuzhiyun tx_queue = efx->xdp_tx_queues[cpu];
448*4882a593Smuzhiyun if (unlikely(!tx_queue))
449*4882a593Smuzhiyun return -EINVAL;
450*4882a593Smuzhiyun
451*4882a593Smuzhiyun if (unlikely(n && !xdpfs))
452*4882a593Smuzhiyun return -EINVAL;
453*4882a593Smuzhiyun
454*4882a593Smuzhiyun if (!n)
455*4882a593Smuzhiyun return 0;
456*4882a593Smuzhiyun
457*4882a593Smuzhiyun /* Check for available space. We should never need multiple
458*4882a593Smuzhiyun * descriptors per frame.
459*4882a593Smuzhiyun */
460*4882a593Smuzhiyun space = efx->txq_entries +
461*4882a593Smuzhiyun tx_queue->read_count - tx_queue->insert_count;
462*4882a593Smuzhiyun
463*4882a593Smuzhiyun for (i = 0; i < n; i++) {
464*4882a593Smuzhiyun xdpf = xdpfs[i];
465*4882a593Smuzhiyun
466*4882a593Smuzhiyun if (i >= space)
467*4882a593Smuzhiyun break;
468*4882a593Smuzhiyun
469*4882a593Smuzhiyun /* We'll want a descriptor for this tx. */
470*4882a593Smuzhiyun prefetchw(__efx_tx_queue_get_insert_buffer(tx_queue));
471*4882a593Smuzhiyun
472*4882a593Smuzhiyun len = xdpf->len;
473*4882a593Smuzhiyun
474*4882a593Smuzhiyun /* Map for DMA. */
475*4882a593Smuzhiyun dma_addr = dma_map_single(&efx->pci_dev->dev,
476*4882a593Smuzhiyun xdpf->data, len,
477*4882a593Smuzhiyun DMA_TO_DEVICE);
478*4882a593Smuzhiyun if (dma_mapping_error(&efx->pci_dev->dev, dma_addr))
479*4882a593Smuzhiyun break;
480*4882a593Smuzhiyun
481*4882a593Smuzhiyun /* Create descriptor and set up for unmapping DMA. */
482*4882a593Smuzhiyun tx_buffer = efx_tx_map_chunk(tx_queue, dma_addr, len);
483*4882a593Smuzhiyun tx_buffer->xdpf = xdpf;
484*4882a593Smuzhiyun tx_buffer->flags = EFX_TX_BUF_XDP |
485*4882a593Smuzhiyun EFX_TX_BUF_MAP_SINGLE;
486*4882a593Smuzhiyun tx_buffer->dma_offset = 0;
487*4882a593Smuzhiyun tx_buffer->unmap_len = len;
488*4882a593Smuzhiyun tx_queue->tx_packets++;
489*4882a593Smuzhiyun }
490*4882a593Smuzhiyun
491*4882a593Smuzhiyun /* Pass mapped frames to hardware. */
492*4882a593Smuzhiyun if (flush && i > 0)
493*4882a593Smuzhiyun efx_nic_push_buffers(tx_queue);
494*4882a593Smuzhiyun
495*4882a593Smuzhiyun if (i == 0)
496*4882a593Smuzhiyun return -EIO;
497*4882a593Smuzhiyun
498*4882a593Smuzhiyun efx_xdp_return_frames(n - i, xdpfs + i);
499*4882a593Smuzhiyun
500*4882a593Smuzhiyun return i;
501*4882a593Smuzhiyun }
502*4882a593Smuzhiyun
503*4882a593Smuzhiyun /* Initiate a packet transmission. We use one channel per CPU
504*4882a593Smuzhiyun * (sharing when we have more CPUs than channels).
505*4882a593Smuzhiyun *
506*4882a593Smuzhiyun * Context: non-blocking.
507*4882a593Smuzhiyun * Should always return NETDEV_TX_OK and consume the skb.
508*4882a593Smuzhiyun */
efx_hard_start_xmit(struct sk_buff * skb,struct net_device * net_dev)509*4882a593Smuzhiyun netdev_tx_t efx_hard_start_xmit(struct sk_buff *skb,
510*4882a593Smuzhiyun struct net_device *net_dev)
511*4882a593Smuzhiyun {
512*4882a593Smuzhiyun struct efx_nic *efx = netdev_priv(net_dev);
513*4882a593Smuzhiyun struct efx_tx_queue *tx_queue;
514*4882a593Smuzhiyun unsigned index, type;
515*4882a593Smuzhiyun
516*4882a593Smuzhiyun EFX_WARN_ON_PARANOID(!netif_device_present(net_dev));
517*4882a593Smuzhiyun
518*4882a593Smuzhiyun index = skb_get_queue_mapping(skb);
519*4882a593Smuzhiyun type = efx_tx_csum_type_skb(skb);
520*4882a593Smuzhiyun if (index >= efx->n_tx_channels) {
521*4882a593Smuzhiyun index -= efx->n_tx_channels;
522*4882a593Smuzhiyun type |= EFX_TXQ_TYPE_HIGHPRI;
523*4882a593Smuzhiyun }
524*4882a593Smuzhiyun
525*4882a593Smuzhiyun /* PTP "event" packet */
526*4882a593Smuzhiyun if (unlikely(efx_xmit_with_hwtstamp(skb)) &&
527*4882a593Smuzhiyun unlikely(efx_ptp_is_ptp_tx(efx, skb))) {
528*4882a593Smuzhiyun /* There may be existing transmits on the channel that are
529*4882a593Smuzhiyun * waiting for this packet to trigger the doorbell write.
530*4882a593Smuzhiyun * We need to send the packets at this point.
531*4882a593Smuzhiyun */
532*4882a593Smuzhiyun efx_tx_send_pending(efx_get_tx_channel(efx, index));
533*4882a593Smuzhiyun return efx_ptp_tx(efx, skb);
534*4882a593Smuzhiyun }
535*4882a593Smuzhiyun
536*4882a593Smuzhiyun tx_queue = efx_get_tx_queue(efx, index, type);
537*4882a593Smuzhiyun if (WARN_ON_ONCE(!tx_queue)) {
538*4882a593Smuzhiyun /* We don't have a TXQ of the right type.
539*4882a593Smuzhiyun * This should never happen, as we don't advertise offload
540*4882a593Smuzhiyun * features unless we can support them.
541*4882a593Smuzhiyun */
542*4882a593Smuzhiyun dev_kfree_skb_any(skb);
543*4882a593Smuzhiyun /* If we're not expecting another transmit and we had something to push
544*4882a593Smuzhiyun * on this queue or a partner queue then we need to push here to get the
545*4882a593Smuzhiyun * previous packets out.
546*4882a593Smuzhiyun */
547*4882a593Smuzhiyun if (!netdev_xmit_more())
548*4882a593Smuzhiyun efx_tx_send_pending(efx_get_tx_channel(efx, index));
549*4882a593Smuzhiyun return NETDEV_TX_OK;
550*4882a593Smuzhiyun }
551*4882a593Smuzhiyun
552*4882a593Smuzhiyun return __efx_enqueue_skb(tx_queue, skb);
553*4882a593Smuzhiyun }
554*4882a593Smuzhiyun
efx_xmit_done_single(struct efx_tx_queue * tx_queue)555*4882a593Smuzhiyun void efx_xmit_done_single(struct efx_tx_queue *tx_queue)
556*4882a593Smuzhiyun {
557*4882a593Smuzhiyun unsigned int pkts_compl = 0, bytes_compl = 0;
558*4882a593Smuzhiyun unsigned int read_ptr;
559*4882a593Smuzhiyun bool finished = false;
560*4882a593Smuzhiyun
561*4882a593Smuzhiyun read_ptr = tx_queue->read_count & tx_queue->ptr_mask;
562*4882a593Smuzhiyun
563*4882a593Smuzhiyun while (!finished) {
564*4882a593Smuzhiyun struct efx_tx_buffer *buffer = &tx_queue->buffer[read_ptr];
565*4882a593Smuzhiyun
566*4882a593Smuzhiyun if (!efx_tx_buffer_in_use(buffer)) {
567*4882a593Smuzhiyun struct efx_nic *efx = tx_queue->efx;
568*4882a593Smuzhiyun
569*4882a593Smuzhiyun netif_err(efx, hw, efx->net_dev,
570*4882a593Smuzhiyun "TX queue %d spurious single TX completion\n",
571*4882a593Smuzhiyun tx_queue->queue);
572*4882a593Smuzhiyun efx_schedule_reset(efx, RESET_TYPE_TX_SKIP);
573*4882a593Smuzhiyun return;
574*4882a593Smuzhiyun }
575*4882a593Smuzhiyun
576*4882a593Smuzhiyun /* Need to check the flag before dequeueing. */
577*4882a593Smuzhiyun if (buffer->flags & EFX_TX_BUF_SKB)
578*4882a593Smuzhiyun finished = true;
579*4882a593Smuzhiyun efx_dequeue_buffer(tx_queue, buffer, &pkts_compl, &bytes_compl);
580*4882a593Smuzhiyun
581*4882a593Smuzhiyun ++tx_queue->read_count;
582*4882a593Smuzhiyun read_ptr = tx_queue->read_count & tx_queue->ptr_mask;
583*4882a593Smuzhiyun }
584*4882a593Smuzhiyun
585*4882a593Smuzhiyun tx_queue->pkts_compl += pkts_compl;
586*4882a593Smuzhiyun tx_queue->bytes_compl += bytes_compl;
587*4882a593Smuzhiyun
588*4882a593Smuzhiyun EFX_WARN_ON_PARANOID(pkts_compl != 1);
589*4882a593Smuzhiyun
590*4882a593Smuzhiyun efx_xmit_done_check_empty(tx_queue);
591*4882a593Smuzhiyun }
592*4882a593Smuzhiyun
efx_init_tx_queue_core_txq(struct efx_tx_queue * tx_queue)593*4882a593Smuzhiyun void efx_init_tx_queue_core_txq(struct efx_tx_queue *tx_queue)
594*4882a593Smuzhiyun {
595*4882a593Smuzhiyun struct efx_nic *efx = tx_queue->efx;
596*4882a593Smuzhiyun
597*4882a593Smuzhiyun /* Must be inverse of queue lookup in efx_hard_start_xmit() */
598*4882a593Smuzhiyun tx_queue->core_txq =
599*4882a593Smuzhiyun netdev_get_tx_queue(efx->net_dev,
600*4882a593Smuzhiyun tx_queue->channel->channel +
601*4882a593Smuzhiyun ((tx_queue->type & EFX_TXQ_TYPE_HIGHPRI) ?
602*4882a593Smuzhiyun efx->n_tx_channels : 0));
603*4882a593Smuzhiyun }
604*4882a593Smuzhiyun
efx_setup_tc(struct net_device * net_dev,enum tc_setup_type type,void * type_data)605*4882a593Smuzhiyun int efx_setup_tc(struct net_device *net_dev, enum tc_setup_type type,
606*4882a593Smuzhiyun void *type_data)
607*4882a593Smuzhiyun {
608*4882a593Smuzhiyun struct efx_nic *efx = netdev_priv(net_dev);
609*4882a593Smuzhiyun struct tc_mqprio_qopt *mqprio = type_data;
610*4882a593Smuzhiyun unsigned tc, num_tc;
611*4882a593Smuzhiyun
612*4882a593Smuzhiyun if (type != TC_SETUP_QDISC_MQPRIO)
613*4882a593Smuzhiyun return -EOPNOTSUPP;
614*4882a593Smuzhiyun
615*4882a593Smuzhiyun /* Only Siena supported highpri queues */
616*4882a593Smuzhiyun if (efx_nic_rev(efx) > EFX_REV_SIENA_A0)
617*4882a593Smuzhiyun return -EOPNOTSUPP;
618*4882a593Smuzhiyun
619*4882a593Smuzhiyun num_tc = mqprio->num_tc;
620*4882a593Smuzhiyun
621*4882a593Smuzhiyun if (num_tc > EFX_MAX_TX_TC)
622*4882a593Smuzhiyun return -EINVAL;
623*4882a593Smuzhiyun
624*4882a593Smuzhiyun mqprio->hw = TC_MQPRIO_HW_OFFLOAD_TCS;
625*4882a593Smuzhiyun
626*4882a593Smuzhiyun if (num_tc == net_dev->num_tc)
627*4882a593Smuzhiyun return 0;
628*4882a593Smuzhiyun
629*4882a593Smuzhiyun for (tc = 0; tc < num_tc; tc++) {
630*4882a593Smuzhiyun net_dev->tc_to_txq[tc].offset = tc * efx->n_tx_channels;
631*4882a593Smuzhiyun net_dev->tc_to_txq[tc].count = efx->n_tx_channels;
632*4882a593Smuzhiyun }
633*4882a593Smuzhiyun
634*4882a593Smuzhiyun net_dev->num_tc = num_tc;
635*4882a593Smuzhiyun
636*4882a593Smuzhiyun return netif_set_real_num_tx_queues(net_dev,
637*4882a593Smuzhiyun max_t(int, num_tc, 1) *
638*4882a593Smuzhiyun efx->n_tx_channels);
639*4882a593Smuzhiyun }
640