xref: /openbmc/linux/drivers/net/ethernet/sfc/tx.c (revision 5b4cb650)
1 /****************************************************************************
2  * Driver for Solarflare network controllers and boards
3  * Copyright 2005-2006 Fen Systems Ltd.
4  * Copyright 2005-2013 Solarflare Communications Inc.
5  *
6  * This program is free software; you can redistribute it and/or modify it
7  * under the terms of the GNU General Public License version 2 as published
8  * by the Free Software Foundation, incorporated herein by reference.
9  */
10 
11 #include <linux/pci.h>
12 #include <linux/tcp.h>
13 #include <linux/ip.h>
14 #include <linux/in.h>
15 #include <linux/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"
28 
29 #ifdef EFX_USE_PIO
30 
31 #define EFX_PIOBUF_SIZE_DEF ALIGN(256, L1_CACHE_BYTES)
32 unsigned int efx_piobuf_size __read_mostly = EFX_PIOBUF_SIZE_DEF;
33 
34 #endif /* EFX_USE_PIO */
35 
36 static inline u8 *efx_tx_get_copy_buffer(struct efx_tx_queue *tx_queue,
37 					 struct efx_tx_buffer *buffer)
38 {
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);
44 
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;
52 }
53 
54 u8 *efx_tx_get_copy_buffer_limited(struct efx_tx_queue *tx_queue,
55 				   struct efx_tx_buffer *buffer, size_t len)
56 {
57 	if (len > EFX_TX_CB_SIZE)
58 		return NULL;
59 	return efx_tx_get_copy_buffer(tx_queue, buffer);
60 }
61 
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)
66 {
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;
77 	}
78 
79 	if (buffer->flags & EFX_TX_BUF_SKB) {
80 		struct sk_buff *skb = (struct sk_buff *)buffer->skb;
81 
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;
89 
90 			hwtstamp.hwtstamp =
91 				efx_ptp_nic_to_kernel_time(tx_queue);
92 			skb_tstamp_tx(skb, &hwtstamp);
93 
94 			tx_queue->completed_timestamp_major = 0;
95 			tx_queue->completed_timestamp_minor = 0;
96 		}
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);
101 	}
102 
103 	buffer->len = 0;
104 	buffer->flags = 0;
105 }
106 
107 unsigned int efx_tx_max_skb_descs(struct efx_nic *efx)
108 {
109 	/* Header and payload descriptor for each output segment, plus
110 	 * one for every input fragment boundary within a segment
111 	 */
112 	unsigned int max_descs = EFX_TSO_MAX_SEGS * 2 + MAX_SKB_FRAGS;
113 
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;
117 
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));
122 
123 	return max_descs;
124 }
125 
126 static void efx_tx_maybe_stop_queue(struct efx_tx_queue *txq1)
127 {
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;
132 
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;
137 
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).
142 	 *
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.
151 	 */
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);
156 
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);
164 	}
165 }
166 
167 static int efx_enqueue_skb_copy(struct efx_tx_queue *tx_queue,
168 				struct sk_buff *skb)
169 {
170 	unsigned int copy_len = skb->len;
171 	struct efx_tx_buffer *buffer;
172 	u8 *copy_buffer;
173 	int rc;
174 
175 	EFX_WARN_ON_ONCE_PARANOID(copy_len > EFX_TX_CB_SIZE);
176 
177 	buffer = efx_tx_queue_get_insert_buffer(tx_queue);
178 
179 	copy_buffer = efx_tx_get_copy_buffer(tx_queue, buffer);
180 	if (unlikely(!copy_buffer))
181 		return -ENOMEM;
182 
183 	rc = skb_copy_bits(skb, 0, copy_buffer, copy_len);
184 	EFX_WARN_ON_PARANOID(rc);
185 	buffer->len = copy_len;
186 
187 	buffer->skb = skb;
188 	buffer->flags = EFX_TX_BUF_SKB;
189 
190 	++tx_queue->insert_count;
191 	return rc;
192 }
193 
194 #ifdef EFX_USE_PIO
195 
196 struct efx_short_copy_buffer {
197 	int used;
198 	u8 buf[L1_CACHE_BYTES];
199 };
200 
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.
203  */
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)
207 {
208 	int block_len = len & ~(sizeof(copy_buf->buf) - 1);
209 
210 	__iowrite64_copy(*piobuf, data, block_len >> 3);
211 	*piobuf += block_len;
212 	len -= block_len;
213 
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;
220 	}
221 }
222 
223 /* Copy to PIO, respecting dword alignment, popping data from copy buffer first.
224  * Advances piobuf pointer. Leaves additional data in the copy buffer.
225  */
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)
229 {
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);
234 
235 		memcpy(copy_buf->buf + copy_buf->used, data, copy_to_buf);
236 		copy_buf->used += copy_to_buf;
237 
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;
241 
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;
248 	}
249 
250 	efx_memcpy_toio_aligned(efx, piobuf, data, len, copy_buf);
251 }
252 
253 static void efx_flush_copy_buffer(struct efx_nic *efx, u8 __iomem *piobuf,
254 				  struct efx_short_copy_buffer *copy_buf)
255 {
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);
260 }
261 
262 /* Traverse skb structure and copy fragments in to PIO buffer.
263  * Advances piobuf pointer.
264  */
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)
268 {
269 	int i;
270 
271 	efx_memcpy_toio_aligned(efx, piobuf, skb->data, skb_headlen(skb),
272 				copy_buf);
273 
274 	for (i = 0; i < skb_shinfo(skb)->nr_frags; ++i) {
275 		skb_frag_t *f = &skb_shinfo(skb)->frags[i];
276 		u8 *vaddr;
277 
278 		vaddr = kmap_atomic(skb_frag_page(f));
279 
280 		efx_memcpy_toio_aligned_cb(efx, piobuf, vaddr + f->page_offset,
281 					   skb_frag_size(f), copy_buf);
282 		kunmap_atomic(vaddr);
283 	}
284 
285 	EFX_WARN_ON_ONCE_PARANOID(skb_shinfo(skb)->frag_list);
286 }
287 
288 static int efx_enqueue_skb_pio(struct efx_tx_queue *tx_queue,
289 			       struct sk_buff *skb)
290 {
291 	struct efx_tx_buffer *buffer =
292 		efx_tx_queue_get_insert_buffer(tx_queue);
293 	u8 __iomem *piobuf = tx_queue->piobuf;
294 
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.
298 	 */
299 
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.
303 		 */
304 		struct efx_short_copy_buffer copy_buf;
305 
306 		copy_buf.used = 0;
307 
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.
315 		 */
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);
320 	}
321 
322 	buffer->skb = skb;
323 	buffer->flags = EFX_TX_BUF_SKB | EFX_TX_BUF_OPTION;
324 
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;
334 }
335 #endif /* EFX_USE_PIO */
336 
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)
340 {
341 	const struct efx_nic_type *nic_type = tx_queue->efx->type;
342 	struct efx_tx_buffer *buffer;
343 	unsigned int dma_len;
344 
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);
349 
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);
357 
358 	return buffer;
359 }
360 
361 /* Map all data from an SKB for DMA and create descriptors on the queue.
362  */
363 static int efx_tx_map_data(struct efx_tx_queue *tx_queue, struct sk_buff *skb,
364 			   unsigned int segment_count)
365 {
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;
372 
373 	nr_frags = skb_shinfo(skb)->nr_frags;
374 	frag_index = 0;
375 
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;
382 
383 	if (unlikely(dma_mapping_error(dma_dev, dma_addr)))
384 		return -EIO;
385 
386 	if (segment_count) {
387 		/* For TSO we need to put the header in to a separate
388 		 * descriptor. Map this separately if necessary.
389 		 */
390 		size_t header_len = skb_transport_header(skb) - skb->data +
391 				(tcp_hdr(skb)->doff << 2u);
392 
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;
398 		}
399 	}
400 
401 	/* Add descriptors for each fragment. */
402 	do {
403 		struct efx_tx_buffer *buffer;
404 		skb_frag_t *fragment;
405 
406 		buffer = efx_tx_map_chunk(tx_queue, dma_addr, len);
407 
408 		/* The final descriptor for a fragment is responsible for
409 		 * unmapping the whole fragment.
410 		 */
411 		buffer->flags = EFX_TX_BUF_CONT | dma_flags;
412 		buffer->unmap_len = unmap_len;
413 		buffer->dma_offset = buffer->dma_addr - unmap_addr;
414 
415 		if (frag_index >= nr_frags) {
416 			/* Store SKB details with the final buffer for
417 			 * the completion.
418 			 */
419 			buffer->skb = skb;
420 			buffer->flags = EFX_TX_BUF_SKB | dma_flags;
421 			return 0;
422 		}
423 
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;
432 
433 		if (unlikely(dma_mapping_error(dma_dev, dma_addr)))
434 			return -EIO;
435 	} while (1);
436 }
437 
438 /* Remove buffers put into a tx_queue for the current packet.
439  * None of the buffers must have an skb attached.
440  */
441 static void efx_enqueue_unwind(struct efx_tx_queue *tx_queue,
442 			       unsigned int insert_count)
443 {
444 	struct efx_tx_buffer *buffer;
445 	unsigned int bytes_compl = 0;
446 	unsigned int pkts_compl = 0;
447 
448 	/* Work backwards until we hit the original insert pointer value */
449 	while (tx_queue->insert_count != insert_count) {
450 		--tx_queue->insert_count;
451 		buffer = __efx_tx_queue_get_insert_buffer(tx_queue);
452 		efx_dequeue_buffer(tx_queue, buffer, &pkts_compl, &bytes_compl);
453 	}
454 }
455 
456 /*
457  * Fallback to software TSO.
458  *
459  * This is used if we are unable to send a GSO packet through hardware TSO.
460  * This should only ever happen due to per-queue restrictions - unsupported
461  * packets should first be filtered by the feature flags.
462  *
463  * Returns 0 on success, error code otherwise.
464  */
465 static int efx_tx_tso_fallback(struct efx_tx_queue *tx_queue,
466 			       struct sk_buff *skb)
467 {
468 	struct sk_buff *segments, *next;
469 
470 	segments = skb_gso_segment(skb, 0);
471 	if (IS_ERR(segments))
472 		return PTR_ERR(segments);
473 
474 	dev_kfree_skb_any(skb);
475 	skb = segments;
476 
477 	while (skb) {
478 		next = skb->next;
479 		skb->next = NULL;
480 
481 		if (next)
482 			skb->xmit_more = true;
483 		efx_enqueue_skb(tx_queue, skb);
484 		skb = next;
485 	}
486 
487 	return 0;
488 }
489 
490 /*
491  * Add a socket buffer to a TX queue
492  *
493  * This maps all fragments of a socket buffer for DMA and adds them to
494  * the TX queue.  The queue's insert pointer will be incremented by
495  * the number of fragments in the socket buffer.
496  *
497  * If any DMA mapping fails, any mapped fragments will be unmapped,
498  * the queue's insert pointer will be restored to its original value.
499  *
500  * This function is split out from efx_hard_start_xmit to allow the
501  * loopback test to direct packets via specific TX queues.
502  *
503  * Returns NETDEV_TX_OK.
504  * You must hold netif_tx_lock() to call this function.
505  */
506 netdev_tx_t efx_enqueue_skb(struct efx_tx_queue *tx_queue, struct sk_buff *skb)
507 {
508 	unsigned int old_insert_count = tx_queue->insert_count;
509 	bool xmit_more = skb->xmit_more;
510 	bool data_mapped = false;
511 	unsigned int segments;
512 	unsigned int skb_len;
513 	int rc;
514 
515 	skb_len = skb->len;
516 	segments = skb_is_gso(skb) ? skb_shinfo(skb)->gso_segs : 0;
517 	if (segments == 1)
518 		segments = 0; /* Don't use TSO for a single segment. */
519 
520 	/* Handle TSO first - it's *possible* (although unlikely) that we might
521 	 * be passed a packet to segment that's smaller than the copybreak/PIO
522 	 * size limit.
523 	 */
524 	if (segments) {
525 		EFX_WARN_ON_ONCE_PARANOID(!tx_queue->handle_tso);
526 		rc = tx_queue->handle_tso(tx_queue, skb, &data_mapped);
527 		if (rc == -EINVAL) {
528 			rc = efx_tx_tso_fallback(tx_queue, skb);
529 			tx_queue->tso_fallbacks++;
530 			if (rc == 0)
531 				return 0;
532 		}
533 		if (rc)
534 			goto err;
535 #ifdef EFX_USE_PIO
536 	} else if (skb_len <= efx_piobuf_size && !skb->xmit_more &&
537 		   efx_nic_may_tx_pio(tx_queue)) {
538 		/* Use PIO for short packets with an empty queue. */
539 		if (efx_enqueue_skb_pio(tx_queue, skb))
540 			goto err;
541 		tx_queue->pio_packets++;
542 		data_mapped = true;
543 #endif
544 	} else if (skb->data_len && skb_len <= EFX_TX_CB_SIZE) {
545 		/* Pad short packets or coalesce short fragmented packets. */
546 		if (efx_enqueue_skb_copy(tx_queue, skb))
547 			goto err;
548 		tx_queue->cb_packets++;
549 		data_mapped = true;
550 	}
551 
552 	/* Map for DMA and create descriptors if we haven't done so already. */
553 	if (!data_mapped && (efx_tx_map_data(tx_queue, skb, segments)))
554 		goto err;
555 
556 	efx_tx_maybe_stop_queue(tx_queue);
557 
558 	/* Pass off to hardware */
559 	if (__netdev_tx_sent_queue(tx_queue->core_txq, skb_len, xmit_more)) {
560 		struct efx_tx_queue *txq2 = efx_tx_queue_partner(tx_queue);
561 
562 		/* There could be packets left on the partner queue if those
563 		 * SKBs had skb->xmit_more set. If we do not push those they
564 		 * could be left for a long time and cause a netdev watchdog.
565 		 */
566 		if (txq2->xmit_more_available)
567 			efx_nic_push_buffers(txq2);
568 
569 		efx_nic_push_buffers(tx_queue);
570 	} else {
571 		tx_queue->xmit_more_available = skb->xmit_more;
572 	}
573 
574 	if (segments) {
575 		tx_queue->tso_bursts++;
576 		tx_queue->tso_packets += segments;
577 		tx_queue->tx_packets  += segments;
578 	} else {
579 		tx_queue->tx_packets++;
580 	}
581 
582 	return NETDEV_TX_OK;
583 
584 
585 err:
586 	efx_enqueue_unwind(tx_queue, old_insert_count);
587 	dev_kfree_skb_any(skb);
588 
589 	/* If we're not expecting another transmit and we had something to push
590 	 * on this queue or a partner queue then we need to push here to get the
591 	 * previous packets out.
592 	 */
593 	if (!xmit_more) {
594 		struct efx_tx_queue *txq2 = efx_tx_queue_partner(tx_queue);
595 
596 		if (txq2->xmit_more_available)
597 			efx_nic_push_buffers(txq2);
598 
599 		efx_nic_push_buffers(tx_queue);
600 	}
601 
602 	return NETDEV_TX_OK;
603 }
604 
605 /* Remove packets from the TX queue
606  *
607  * This removes packets from the TX queue, up to and including the
608  * specified index.
609  */
610 static void efx_dequeue_buffers(struct efx_tx_queue *tx_queue,
611 				unsigned int index,
612 				unsigned int *pkts_compl,
613 				unsigned int *bytes_compl)
614 {
615 	struct efx_nic *efx = tx_queue->efx;
616 	unsigned int stop_index, read_ptr;
617 
618 	stop_index = (index + 1) & tx_queue->ptr_mask;
619 	read_ptr = tx_queue->read_count & tx_queue->ptr_mask;
620 
621 	while (read_ptr != stop_index) {
622 		struct efx_tx_buffer *buffer = &tx_queue->buffer[read_ptr];
623 
624 		if (!(buffer->flags & EFX_TX_BUF_OPTION) &&
625 		    unlikely(buffer->len == 0)) {
626 			netif_err(efx, tx_err, efx->net_dev,
627 				  "TX queue %d spurious TX completion id %x\n",
628 				  tx_queue->queue, read_ptr);
629 			efx_schedule_reset(efx, RESET_TYPE_TX_SKIP);
630 			return;
631 		}
632 
633 		efx_dequeue_buffer(tx_queue, buffer, pkts_compl, bytes_compl);
634 
635 		++tx_queue->read_count;
636 		read_ptr = tx_queue->read_count & tx_queue->ptr_mask;
637 	}
638 }
639 
640 /* Initiate a packet transmission.  We use one channel per CPU
641  * (sharing when we have more CPUs than channels).  On Falcon, the TX
642  * completion events will be directed back to the CPU that transmitted
643  * the packet, which should be cache-efficient.
644  *
645  * Context: non-blocking.
646  * Note that returning anything other than NETDEV_TX_OK will cause the
647  * OS to free the skb.
648  */
649 netdev_tx_t efx_hard_start_xmit(struct sk_buff *skb,
650 				struct net_device *net_dev)
651 {
652 	struct efx_nic *efx = netdev_priv(net_dev);
653 	struct efx_tx_queue *tx_queue;
654 	unsigned index, type;
655 
656 	EFX_WARN_ON_PARANOID(!netif_device_present(net_dev));
657 
658 	/* PTP "event" packet */
659 	if (unlikely(efx_xmit_with_hwtstamp(skb)) &&
660 	    unlikely(efx_ptp_is_ptp_tx(efx, skb))) {
661 		return efx_ptp_tx(efx, skb);
662 	}
663 
664 	index = skb_get_queue_mapping(skb);
665 	type = skb->ip_summed == CHECKSUM_PARTIAL ? EFX_TXQ_TYPE_OFFLOAD : 0;
666 	if (index >= efx->n_tx_channels) {
667 		index -= efx->n_tx_channels;
668 		type |= EFX_TXQ_TYPE_HIGHPRI;
669 	}
670 	tx_queue = efx_get_tx_queue(efx, index, type);
671 
672 	return efx_enqueue_skb(tx_queue, skb);
673 }
674 
675 void efx_init_tx_queue_core_txq(struct efx_tx_queue *tx_queue)
676 {
677 	struct efx_nic *efx = tx_queue->efx;
678 
679 	/* Must be inverse of queue lookup in efx_hard_start_xmit() */
680 	tx_queue->core_txq =
681 		netdev_get_tx_queue(efx->net_dev,
682 				    tx_queue->queue / EFX_TXQ_TYPES +
683 				    ((tx_queue->queue & EFX_TXQ_TYPE_HIGHPRI) ?
684 				     efx->n_tx_channels : 0));
685 }
686 
687 int efx_setup_tc(struct net_device *net_dev, enum tc_setup_type type,
688 		 void *type_data)
689 {
690 	struct efx_nic *efx = netdev_priv(net_dev);
691 	struct tc_mqprio_qopt *mqprio = type_data;
692 	struct efx_channel *channel;
693 	struct efx_tx_queue *tx_queue;
694 	unsigned tc, num_tc;
695 	int rc;
696 
697 	if (type != TC_SETUP_QDISC_MQPRIO)
698 		return -EOPNOTSUPP;
699 
700 	num_tc = mqprio->num_tc;
701 
702 	if (num_tc > EFX_MAX_TX_TC)
703 		return -EINVAL;
704 
705 	mqprio->hw = TC_MQPRIO_HW_OFFLOAD_TCS;
706 
707 	if (num_tc == net_dev->num_tc)
708 		return 0;
709 
710 	for (tc = 0; tc < num_tc; tc++) {
711 		net_dev->tc_to_txq[tc].offset = tc * efx->n_tx_channels;
712 		net_dev->tc_to_txq[tc].count = efx->n_tx_channels;
713 	}
714 
715 	if (num_tc > net_dev->num_tc) {
716 		/* Initialise high-priority queues as necessary */
717 		efx_for_each_channel(channel, efx) {
718 			efx_for_each_possible_channel_tx_queue(tx_queue,
719 							       channel) {
720 				if (!(tx_queue->queue & EFX_TXQ_TYPE_HIGHPRI))
721 					continue;
722 				if (!tx_queue->buffer) {
723 					rc = efx_probe_tx_queue(tx_queue);
724 					if (rc)
725 						return rc;
726 				}
727 				if (!tx_queue->initialised)
728 					efx_init_tx_queue(tx_queue);
729 				efx_init_tx_queue_core_txq(tx_queue);
730 			}
731 		}
732 	} else {
733 		/* Reduce number of classes before number of queues */
734 		net_dev->num_tc = num_tc;
735 	}
736 
737 	rc = netif_set_real_num_tx_queues(net_dev,
738 					  max_t(int, num_tc, 1) *
739 					  efx->n_tx_channels);
740 	if (rc)
741 		return rc;
742 
743 	/* Do not destroy high-priority queues when they become
744 	 * unused.  We would have to flush them first, and it is
745 	 * fairly difficult to flush a subset of TX queues.  Leave
746 	 * it to efx_fini_channels().
747 	 */
748 
749 	net_dev->num_tc = num_tc;
750 	return 0;
751 }
752 
753 void efx_xmit_done(struct efx_tx_queue *tx_queue, unsigned int index)
754 {
755 	unsigned fill_level;
756 	struct efx_nic *efx = tx_queue->efx;
757 	struct efx_tx_queue *txq2;
758 	unsigned int pkts_compl = 0, bytes_compl = 0;
759 
760 	EFX_WARN_ON_ONCE_PARANOID(index > tx_queue->ptr_mask);
761 
762 	efx_dequeue_buffers(tx_queue, index, &pkts_compl, &bytes_compl);
763 	tx_queue->pkts_compl += pkts_compl;
764 	tx_queue->bytes_compl += bytes_compl;
765 
766 	if (pkts_compl > 1)
767 		++tx_queue->merge_events;
768 
769 	/* See if we need to restart the netif queue.  This memory
770 	 * barrier ensures that we write read_count (inside
771 	 * efx_dequeue_buffers()) before reading the queue status.
772 	 */
773 	smp_mb();
774 	if (unlikely(netif_tx_queue_stopped(tx_queue->core_txq)) &&
775 	    likely(efx->port_enabled) &&
776 	    likely(netif_device_present(efx->net_dev))) {
777 		txq2 = efx_tx_queue_partner(tx_queue);
778 		fill_level = max(tx_queue->insert_count - tx_queue->read_count,
779 				 txq2->insert_count - txq2->read_count);
780 		if (fill_level <= efx->txq_wake_thresh)
781 			netif_tx_wake_queue(tx_queue->core_txq);
782 	}
783 
784 	/* Check whether the hardware queue is now empty */
785 	if ((int)(tx_queue->read_count - tx_queue->old_write_count) >= 0) {
786 		tx_queue->old_write_count = READ_ONCE(tx_queue->write_count);
787 		if (tx_queue->read_count == tx_queue->old_write_count) {
788 			smp_mb();
789 			tx_queue->empty_read_count =
790 				tx_queue->read_count | EFX_EMPTY_COUNT_VALID;
791 		}
792 	}
793 }
794 
795 static unsigned int efx_tx_cb_page_count(struct efx_tx_queue *tx_queue)
796 {
797 	return DIV_ROUND_UP(tx_queue->ptr_mask + 1, PAGE_SIZE >> EFX_TX_CB_ORDER);
798 }
799 
800 int efx_probe_tx_queue(struct efx_tx_queue *tx_queue)
801 {
802 	struct efx_nic *efx = tx_queue->efx;
803 	unsigned int entries;
804 	int rc;
805 
806 	/* Create the smallest power-of-two aligned ring */
807 	entries = max(roundup_pow_of_two(efx->txq_entries), EFX_MIN_DMAQ_SIZE);
808 	EFX_WARN_ON_PARANOID(entries > EFX_MAX_DMAQ_SIZE);
809 	tx_queue->ptr_mask = entries - 1;
810 
811 	netif_dbg(efx, probe, efx->net_dev,
812 		  "creating TX queue %d size %#x mask %#x\n",
813 		  tx_queue->queue, efx->txq_entries, tx_queue->ptr_mask);
814 
815 	/* Allocate software ring */
816 	tx_queue->buffer = kcalloc(entries, sizeof(*tx_queue->buffer),
817 				   GFP_KERNEL);
818 	if (!tx_queue->buffer)
819 		return -ENOMEM;
820 
821 	tx_queue->cb_page = kcalloc(efx_tx_cb_page_count(tx_queue),
822 				    sizeof(tx_queue->cb_page[0]), GFP_KERNEL);
823 	if (!tx_queue->cb_page) {
824 		rc = -ENOMEM;
825 		goto fail1;
826 	}
827 
828 	/* Allocate hardware ring */
829 	rc = efx_nic_probe_tx(tx_queue);
830 	if (rc)
831 		goto fail2;
832 
833 	return 0;
834 
835 fail2:
836 	kfree(tx_queue->cb_page);
837 	tx_queue->cb_page = NULL;
838 fail1:
839 	kfree(tx_queue->buffer);
840 	tx_queue->buffer = NULL;
841 	return rc;
842 }
843 
844 void efx_init_tx_queue(struct efx_tx_queue *tx_queue)
845 {
846 	struct efx_nic *efx = tx_queue->efx;
847 
848 	netif_dbg(efx, drv, efx->net_dev,
849 		  "initialising TX queue %d\n", tx_queue->queue);
850 
851 	tx_queue->insert_count = 0;
852 	tx_queue->write_count = 0;
853 	tx_queue->packet_write_count = 0;
854 	tx_queue->old_write_count = 0;
855 	tx_queue->read_count = 0;
856 	tx_queue->old_read_count = 0;
857 	tx_queue->empty_read_count = 0 | EFX_EMPTY_COUNT_VALID;
858 	tx_queue->xmit_more_available = false;
859 	tx_queue->timestamping = (efx_ptp_use_mac_tx_timestamps(efx) &&
860 				  tx_queue->channel == efx_ptp_channel(efx));
861 	tx_queue->completed_desc_ptr = tx_queue->ptr_mask;
862 	tx_queue->completed_timestamp_major = 0;
863 	tx_queue->completed_timestamp_minor = 0;
864 
865 	/* Set up default function pointers. These may get replaced by
866 	 * efx_nic_init_tx() based off NIC/queue capabilities.
867 	 */
868 	tx_queue->handle_tso = efx_enqueue_skb_tso;
869 
870 	/* Set up TX descriptor ring */
871 	efx_nic_init_tx(tx_queue);
872 
873 	tx_queue->initialised = true;
874 }
875 
876 void efx_fini_tx_queue(struct efx_tx_queue *tx_queue)
877 {
878 	struct efx_tx_buffer *buffer;
879 
880 	netif_dbg(tx_queue->efx, drv, tx_queue->efx->net_dev,
881 		  "shutting down TX queue %d\n", tx_queue->queue);
882 
883 	if (!tx_queue->buffer)
884 		return;
885 
886 	/* Free any buffers left in the ring */
887 	while (tx_queue->read_count != tx_queue->write_count) {
888 		unsigned int pkts_compl = 0, bytes_compl = 0;
889 		buffer = &tx_queue->buffer[tx_queue->read_count & tx_queue->ptr_mask];
890 		efx_dequeue_buffer(tx_queue, buffer, &pkts_compl, &bytes_compl);
891 
892 		++tx_queue->read_count;
893 	}
894 	tx_queue->xmit_more_available = false;
895 	netdev_tx_reset_queue(tx_queue->core_txq);
896 }
897 
898 void efx_remove_tx_queue(struct efx_tx_queue *tx_queue)
899 {
900 	int i;
901 
902 	if (!tx_queue->buffer)
903 		return;
904 
905 	netif_dbg(tx_queue->efx, drv, tx_queue->efx->net_dev,
906 		  "destroying TX queue %d\n", tx_queue->queue);
907 	efx_nic_remove_tx(tx_queue);
908 
909 	if (tx_queue->cb_page) {
910 		for (i = 0; i < efx_tx_cb_page_count(tx_queue); i++)
911 			efx_nic_free_buffer(tx_queue->efx,
912 					    &tx_queue->cb_page[i]);
913 		kfree(tx_queue->cb_page);
914 		tx_queue->cb_page = NULL;
915 	}
916 
917 	kfree(tx_queue->buffer);
918 	tx_queue->buffer = NULL;
919 }
920